xref: /freebsd/sys/vm/vm_map.c (revision f8616ebfae00ba18bf0345d20ce8fd6038e5a65d)
160727d8bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
6df8bae1dSRodney W. Grimes  * The Mach Operating System project at Carnegie-Mellon University.
7df8bae1dSRodney W. Grimes  *
8df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
9df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
10df8bae1dSRodney W. Grimes  * are met:
11df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
12df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
13df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
15df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
16df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
17df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
18df8bae1dSRodney W. Grimes  *    without specific prior written permission.
19df8bae1dSRodney W. Grimes  *
20df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
31df8bae1dSRodney W. Grimes  *
323c4dd356SDavid Greenman  *	from: @(#)vm_map.c	8.3 (Berkeley) 1/12/94
33df8bae1dSRodney W. Grimes  *
34df8bae1dSRodney W. Grimes  *
35df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
36df8bae1dSRodney W. Grimes  * All rights reserved.
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
39df8bae1dSRodney W. Grimes  *
40df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
41df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
42df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
43df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
44df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
45df8bae1dSRodney W. Grimes  *
46df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
47df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
48df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
49df8bae1dSRodney W. Grimes  *
50df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
51df8bae1dSRodney W. Grimes  *
52df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
53df8bae1dSRodney W. Grimes  *  School of Computer Science
54df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
55df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
56df8bae1dSRodney W. Grimes  *
57df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
58df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
59df8bae1dSRodney W. Grimes  */
60df8bae1dSRodney W. Grimes 
61df8bae1dSRodney W. Grimes /*
62df8bae1dSRodney W. Grimes  *	Virtual memory mapping module.
63df8bae1dSRodney W. Grimes  */
64df8bae1dSRodney W. Grimes 
65874651b1SDavid E. O'Brien #include <sys/cdefs.h>
66874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
67874651b1SDavid E. O'Brien 
68df8bae1dSRodney W. Grimes #include <sys/param.h>
69df8bae1dSRodney W. Grimes #include <sys/systm.h>
7061d80e90SJohn Baldwin #include <sys/ktr.h>
71fb919e4dSMark Murray #include <sys/lock.h>
72fb919e4dSMark Murray #include <sys/mutex.h>
73b5e8ce9fSBruce Evans #include <sys/proc.h>
74efeaf95aSDavid Greenman #include <sys/vmmeter.h>
75867a482dSJohn Dyson #include <sys/mman.h>
761efb74fbSJohn Dyson #include <sys/vnode.h>
772267af78SJulian Elischer #include <sys/resourcevar.h>
783fde38dfSMike Silbersack #include <sys/file.h>
7905ba50f5SJake Burkholder #include <sys/sysent.h>
803db161e0SMatthew Dillon #include <sys/shm.h>
81df8bae1dSRodney W. Grimes 
82df8bae1dSRodney W. Grimes #include <vm/vm.h>
83efeaf95aSDavid Greenman #include <vm/vm_param.h>
84efeaf95aSDavid Greenman #include <vm/pmap.h>
85efeaf95aSDavid Greenman #include <vm/vm_map.h>
86df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
87df8bae1dSRodney W. Grimes #include <vm/vm_object.h>
8847221757SJohn Dyson #include <vm/vm_pager.h>
8926f9a767SRodney W. Grimes #include <vm/vm_kern.h>
90efeaf95aSDavid Greenman #include <vm/vm_extern.h>
9121cd6e62SSeigo Tanimura #include <vm/swap_pager.h>
92670d17b5SJeff Roberson #include <vm/uma.h>
93df8bae1dSRodney W. Grimes 
94df8bae1dSRodney W. Grimes /*
95df8bae1dSRodney W. Grimes  *	Virtual memory maps provide for the mapping, protection,
96df8bae1dSRodney W. Grimes  *	and sharing of virtual memory objects.  In addition,
97df8bae1dSRodney W. Grimes  *	this module provides for an efficient virtual copy of
98df8bae1dSRodney W. Grimes  *	memory from one map to another.
99df8bae1dSRodney W. Grimes  *
100df8bae1dSRodney W. Grimes  *	Synchronization is required prior to most operations.
101df8bae1dSRodney W. Grimes  *
102df8bae1dSRodney W. Grimes  *	Maps consist of an ordered doubly-linked list of simple
103e2abaaaaSAlan Cox  *	entries; a self-adjusting binary search tree of these
104e2abaaaaSAlan Cox  *	entries is used to speed up lookups.
105df8bae1dSRodney W. Grimes  *
106956f3135SPhilippe Charnier  *	Since portions of maps are specified by start/end addresses,
107df8bae1dSRodney W. Grimes  *	which may not align with existing map entries, all
108df8bae1dSRodney W. Grimes  *	routines merely "clip" entries to these start/end values.
109df8bae1dSRodney W. Grimes  *	[That is, an entry is split into two, bordering at a
110df8bae1dSRodney W. Grimes  *	start or end value.]  Note that these clippings may not
111df8bae1dSRodney W. Grimes  *	always be necessary (as the two resulting entries are then
112df8bae1dSRodney W. Grimes  *	not changed); however, the clipping is done for convenience.
113df8bae1dSRodney W. Grimes  *
114df8bae1dSRodney W. Grimes  *	As mentioned above, virtual copy operations are performed
115ad5fca3bSAlan Cox  *	by copying VM object references from one map to
116df8bae1dSRodney W. Grimes  *	another, and then marking both regions as copy-on-write.
117df8bae1dSRodney W. Grimes  */
118df8bae1dSRodney W. Grimes 
1193a92e5d5SAlan Cox static struct mtx map_sleep_mtx;
1208355f576SJeff Roberson static uma_zone_t mapentzone;
1218355f576SJeff Roberson static uma_zone_t kmapentzone;
1228355f576SJeff Roberson static uma_zone_t mapzone;
1238355f576SJeff Roberson static uma_zone_t vmspace_zone;
1248355f576SJeff Roberson static struct vm_object kmapentobj;
125b23f72e9SBrian Feldman static int vmspace_zinit(void *mem, int size, int flags);
1268355f576SJeff Roberson static void vmspace_zfini(void *mem, int size);
127b23f72e9SBrian Feldman static int vm_map_zinit(void *mem, int ize, int flags);
1288355f576SJeff Roberson static void vm_map_zfini(void *mem, int size);
12992351f16SAlan Cox static void _vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min,
13092351f16SAlan Cox     vm_offset_t max);
1310b367bd8SKonstantin Belousov static void vm_map_entry_deallocate(vm_map_entry_t entry, boolean_t system_map);
132655c3490SKonstantin Belousov static void vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry);
1338355f576SJeff Roberson #ifdef INVARIANTS
1348355f576SJeff Roberson static void vm_map_zdtor(void *mem, int size, void *arg);
1358355f576SJeff Roberson static void vmspace_zdtor(void *mem, int size, void *arg);
1368355f576SJeff Roberson #endif
137b18bfc3dSJohn Dyson 
1383364c323SKonstantin Belousov #define	ENTRY_CHARGED(e) ((e)->uip != NULL || \
1393364c323SKonstantin Belousov     ((e)->object.vm_object != NULL && (e)->object.vm_object->uip != NULL && \
1403364c323SKonstantin Belousov      !((e)->eflags & MAP_ENTRY_NEEDS_COPY)))
1413364c323SKonstantin Belousov 
14257051fdcSTor Egge /*
14357051fdcSTor Egge  * PROC_VMSPACE_{UN,}LOCK() can be a noop as long as vmspaces are type
14457051fdcSTor Egge  * stable.
14557051fdcSTor Egge  */
14657051fdcSTor Egge #define PROC_VMSPACE_LOCK(p) do { } while (0)
14757051fdcSTor Egge #define PROC_VMSPACE_UNLOCK(p) do { } while (0)
14857051fdcSTor Egge 
149d239bd3cSKonstantin Belousov /*
150d239bd3cSKonstantin Belousov  *	VM_MAP_RANGE_CHECK:	[ internal use only ]
151d239bd3cSKonstantin Belousov  *
152d239bd3cSKonstantin Belousov  *	Asserts that the starting and ending region
153d239bd3cSKonstantin Belousov  *	addresses fall within the valid range of the map.
154d239bd3cSKonstantin Belousov  */
155d239bd3cSKonstantin Belousov #define	VM_MAP_RANGE_CHECK(map, start, end)		\
156d239bd3cSKonstantin Belousov 		{					\
157d239bd3cSKonstantin Belousov 		if (start < vm_map_min(map))		\
158d239bd3cSKonstantin Belousov 			start = vm_map_min(map);	\
159d239bd3cSKonstantin Belousov 		if (end > vm_map_max(map))		\
160d239bd3cSKonstantin Belousov 			end = vm_map_max(map);		\
161d239bd3cSKonstantin Belousov 		if (start > end)			\
162d239bd3cSKonstantin Belousov 			start = end;			\
163d239bd3cSKonstantin Belousov 		}
164d239bd3cSKonstantin Belousov 
1656fecb26bSKonstantin Belousov /*
1666fecb26bSKonstantin Belousov  *	vm_map_startup:
1676fecb26bSKonstantin Belousov  *
1686fecb26bSKonstantin Belousov  *	Initialize the vm_map module.  Must be called before
1696fecb26bSKonstantin Belousov  *	any other vm_map routines.
1706fecb26bSKonstantin Belousov  *
1716fecb26bSKonstantin Belousov  *	Map and entry structures are allocated from the general
1726fecb26bSKonstantin Belousov  *	purpose memory pool with some exceptions:
1736fecb26bSKonstantin Belousov  *
1746fecb26bSKonstantin Belousov  *	- The kernel map and kmem submap are allocated statically.
1756fecb26bSKonstantin Belousov  *	- Kernel map entries are allocated out of a static pool.
1766fecb26bSKonstantin Belousov  *
1776fecb26bSKonstantin Belousov  *	These restrictions are necessary since malloc() uses the
1786fecb26bSKonstantin Belousov  *	maps and requires map entries.
1796fecb26bSKonstantin Belousov  */
1806fecb26bSKonstantin Belousov 
1810d94caffSDavid Greenman void
1821b40f8c0SMatthew Dillon vm_map_startup(void)
183df8bae1dSRodney W. Grimes {
1843a92e5d5SAlan Cox 	mtx_init(&map_sleep_mtx, "vm map sleep mutex", NULL, MTX_DEF);
1858355f576SJeff Roberson 	mapzone = uma_zcreate("MAP", sizeof(struct vm_map), NULL,
1868355f576SJeff Roberson #ifdef INVARIANTS
1878355f576SJeff Roberson 	    vm_map_zdtor,
1888355f576SJeff Roberson #else
1898355f576SJeff Roberson 	    NULL,
1908355f576SJeff Roberson #endif
1918355f576SJeff Roberson 	    vm_map_zinit, vm_map_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
1928355f576SJeff Roberson 	uma_prealloc(mapzone, MAX_KMAP);
193670d17b5SJeff Roberson 	kmapentzone = uma_zcreate("KMAP ENTRY", sizeof(struct vm_map_entry),
19418aa2de5SJeff Roberson 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
19518aa2de5SJeff Roberson 	    UMA_ZONE_MTXCLASS | UMA_ZONE_VM);
196670d17b5SJeff Roberson 	uma_prealloc(kmapentzone, MAX_KMAPENT);
197670d17b5SJeff Roberson 	mapentzone = uma_zcreate("MAP ENTRY", sizeof(struct vm_map_entry),
198670d17b5SJeff Roberson 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
199df8bae1dSRodney W. Grimes }
200df8bae1dSRodney W. Grimes 
2018355f576SJeff Roberson static void
2028355f576SJeff Roberson vmspace_zfini(void *mem, int size)
2038355f576SJeff Roberson {
2048355f576SJeff Roberson 	struct vmspace *vm;
2058355f576SJeff Roberson 
2068355f576SJeff Roberson 	vm = (struct vmspace *)mem;
2078355f576SJeff Roberson 	vm_map_zfini(&vm->vm_map, sizeof(vm->vm_map));
2088355f576SJeff Roberson }
2098355f576SJeff Roberson 
210b23f72e9SBrian Feldman static int
211b23f72e9SBrian Feldman vmspace_zinit(void *mem, int size, int flags)
2128355f576SJeff Roberson {
2138355f576SJeff Roberson 	struct vmspace *vm;
2148355f576SJeff Roberson 
2158355f576SJeff Roberson 	vm = (struct vmspace *)mem;
2168355f576SJeff Roberson 
21789b57fcfSKonstantin Belousov 	vm->vm_map.pmap = NULL;
218b23f72e9SBrian Feldman 	(void)vm_map_zinit(&vm->vm_map, sizeof(vm->vm_map), flags);
219b23f72e9SBrian Feldman 	return (0);
2208355f576SJeff Roberson }
2218355f576SJeff Roberson 
2228355f576SJeff Roberson static void
2238355f576SJeff Roberson vm_map_zfini(void *mem, int size)
2248355f576SJeff Roberson {
2258355f576SJeff Roberson 	vm_map_t map;
2268355f576SJeff Roberson 
2278355f576SJeff Roberson 	map = (vm_map_t)mem;
22836daaecdSAlan Cox 	mtx_destroy(&map->system_mtx);
22912c64974SMaxime Henrion 	sx_destroy(&map->lock);
2308355f576SJeff Roberson }
2318355f576SJeff Roberson 
232b23f72e9SBrian Feldman static int
233b23f72e9SBrian Feldman vm_map_zinit(void *mem, int size, int flags)
2348355f576SJeff Roberson {
2358355f576SJeff Roberson 	vm_map_t map;
2368355f576SJeff Roberson 
2378355f576SJeff Roberson 	map = (vm_map_t)mem;
2388355f576SJeff Roberson 	map->nentries = 0;
2398355f576SJeff Roberson 	map->size = 0;
240d923c598SAlan Cox 	mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
24112c64974SMaxime Henrion 	sx_init(&map->lock, "user map");
242b23f72e9SBrian Feldman 	return (0);
2438355f576SJeff Roberson }
2448355f576SJeff Roberson 
2458355f576SJeff Roberson #ifdef INVARIANTS
2468355f576SJeff Roberson static void
2478355f576SJeff Roberson vmspace_zdtor(void *mem, int size, void *arg)
2488355f576SJeff Roberson {
2498355f576SJeff Roberson 	struct vmspace *vm;
2508355f576SJeff Roberson 
2518355f576SJeff Roberson 	vm = (struct vmspace *)mem;
2528355f576SJeff Roberson 
2538355f576SJeff Roberson 	vm_map_zdtor(&vm->vm_map, sizeof(vm->vm_map), arg);
2548355f576SJeff Roberson }
2558355f576SJeff Roberson static void
2568355f576SJeff Roberson vm_map_zdtor(void *mem, int size, void *arg)
2578355f576SJeff Roberson {
2588355f576SJeff Roberson 	vm_map_t map;
2598355f576SJeff Roberson 
2608355f576SJeff Roberson 	map = (vm_map_t)mem;
2618355f576SJeff Roberson 	KASSERT(map->nentries == 0,
2628355f576SJeff Roberson 	    ("map %p nentries == %d on free.",
2638355f576SJeff Roberson 	    map, map->nentries));
2648355f576SJeff Roberson 	KASSERT(map->size == 0,
2658355f576SJeff Roberson 	    ("map %p size == %lu on free.",
2669eb6e519SJeff Roberson 	    map, (unsigned long)map->size));
2678355f576SJeff Roberson }
2688355f576SJeff Roberson #endif	/* INVARIANTS */
2698355f576SJeff Roberson 
270df8bae1dSRodney W. Grimes /*
271df8bae1dSRodney W. Grimes  * Allocate a vmspace structure, including a vm_map and pmap,
272df8bae1dSRodney W. Grimes  * and initialize those structures.  The refcnt is set to 1.
273df8bae1dSRodney W. Grimes  */
274df8bae1dSRodney W. Grimes struct vmspace *
2752d8acc0fSJohn Dyson vmspace_alloc(min, max)
276df8bae1dSRodney W. Grimes 	vm_offset_t min, max;
277df8bae1dSRodney W. Grimes {
278c0877f10SJohn Dyson 	struct vmspace *vm;
2790d94caffSDavid Greenman 
280a163d034SWarner Losh 	vm = uma_zalloc(vmspace_zone, M_WAITOK);
28189b57fcfSKonstantin Belousov 	if (vm->vm_map.pmap == NULL && !pmap_pinit(vmspace_pmap(vm))) {
28289b57fcfSKonstantin Belousov 		uma_zfree(vmspace_zone, vm);
28389b57fcfSKonstantin Belousov 		return (NULL);
28489b57fcfSKonstantin Belousov 	}
28521c641b2SJohn Baldwin 	CTR1(KTR_VM, "vmspace_alloc: %p", vm);
28692351f16SAlan Cox 	_vm_map_init(&vm->vm_map, vmspace_pmap(vm), min, max);
287df8bae1dSRodney W. Grimes 	vm->vm_refcnt = 1;
2882d8acc0fSJohn Dyson 	vm->vm_shm = NULL;
28951ab6c28SAlan Cox 	vm->vm_swrss = 0;
29051ab6c28SAlan Cox 	vm->vm_tsize = 0;
29151ab6c28SAlan Cox 	vm->vm_dsize = 0;
29251ab6c28SAlan Cox 	vm->vm_ssize = 0;
29351ab6c28SAlan Cox 	vm->vm_taddr = 0;
29451ab6c28SAlan Cox 	vm->vm_daddr = 0;
29551ab6c28SAlan Cox 	vm->vm_maxsaddr = 0;
296df8bae1dSRodney W. Grimes 	return (vm);
297df8bae1dSRodney W. Grimes }
298df8bae1dSRodney W. Grimes 
299df8bae1dSRodney W. Grimes void
3001b40f8c0SMatthew Dillon vm_init2(void)
3011b40f8c0SMatthew Dillon {
3022feb50bfSAttilio Rao 	uma_zone_set_obj(kmapentzone, &kmapentobj, lmin(cnt.v_page_count,
303c1f02198SAlan Cox 	    (VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS) / PAGE_SIZE) / 8 +
3043fde38dfSMike Silbersack 	     maxproc * 2 + maxfiles);
3058355f576SJeff Roberson 	vmspace_zone = uma_zcreate("VMSPACE", sizeof(struct vmspace), NULL,
3068355f576SJeff Roberson #ifdef INVARIANTS
3078355f576SJeff Roberson 	    vmspace_zdtor,
3088355f576SJeff Roberson #else
3098355f576SJeff Roberson 	    NULL,
3108355f576SJeff Roberson #endif
3118355f576SJeff Roberson 	    vmspace_zinit, vmspace_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
3123075778bSJohn Dyson }
3133075778bSJohn Dyson 
31462a59e8fSWarner Losh static inline void
315582ec34cSAlfred Perlstein vmspace_dofree(struct vmspace *vm)
316df8bae1dSRodney W. Grimes {
3170ef12795SAlan Cox 
31821c641b2SJohn Baldwin 	CTR1(KTR_VM, "vmspace_free: %p", vm);
3193db161e0SMatthew Dillon 
3203db161e0SMatthew Dillon 	/*
3213db161e0SMatthew Dillon 	 * Make sure any SysV shm is freed, it might not have been in
3223db161e0SMatthew Dillon 	 * exit1().
3233db161e0SMatthew Dillon 	 */
3243db161e0SMatthew Dillon 	shmexit(vm);
3253db161e0SMatthew Dillon 
32630dcfc09SJohn Dyson 	/*
327df8bae1dSRodney W. Grimes 	 * Lock the map, to wait out all other references to it.
3280d94caffSDavid Greenman 	 * Delete all of the mappings and pages they hold, then call
3290d94caffSDavid Greenman 	 * the pmap module to reclaim anything left.
330df8bae1dSRodney W. Grimes 	 */
331717f7d59SAlan Cox 	(void)vm_map_remove(&vm->vm_map, vm->vm_map.min_offset,
332df8bae1dSRodney W. Grimes 	    vm->vm_map.max_offset);
3338355f576SJeff Roberson 
3340ef12795SAlan Cox 	pmap_release(vmspace_pmap(vm));
3350ef12795SAlan Cox 	vm->vm_map.pmap = NULL;
3368355f576SJeff Roberson 	uma_zfree(vmspace_zone, vm);
337df8bae1dSRodney W. Grimes }
338582ec34cSAlfred Perlstein 
339582ec34cSAlfred Perlstein void
340582ec34cSAlfred Perlstein vmspace_free(struct vmspace *vm)
341582ec34cSAlfred Perlstein {
3421a276a3fSAlan Cox 	int refcnt;
343582ec34cSAlfred Perlstein 
344582ec34cSAlfred Perlstein 	if (vm->vm_refcnt == 0)
345582ec34cSAlfred Perlstein 		panic("vmspace_free: attempt to free already freed vmspace");
346582ec34cSAlfred Perlstein 
3471a276a3fSAlan Cox 	do
3481a276a3fSAlan Cox 		refcnt = vm->vm_refcnt;
3491a276a3fSAlan Cox 	while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1));
35057051fdcSTor Egge 	if (refcnt == 1)
351582ec34cSAlfred Perlstein 		vmspace_dofree(vm);
352582ec34cSAlfred Perlstein }
353582ec34cSAlfred Perlstein 
354582ec34cSAlfred Perlstein void
355582ec34cSAlfred Perlstein vmspace_exitfree(struct proc *p)
356582ec34cSAlfred Perlstein {
357334f7061SPeter Wemm 	struct vmspace *vm;
358582ec34cSAlfred Perlstein 
35957051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
360334f7061SPeter Wemm 	vm = p->p_vmspace;
361334f7061SPeter Wemm 	p->p_vmspace = NULL;
36257051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
36357051fdcSTor Egge 	KASSERT(vm == &vmspace0, ("vmspace_exitfree: wrong vmspace"));
36457051fdcSTor Egge 	vmspace_free(vm);
36557051fdcSTor Egge }
36657051fdcSTor Egge 
36757051fdcSTor Egge void
36857051fdcSTor Egge vmspace_exit(struct thread *td)
36957051fdcSTor Egge {
37057051fdcSTor Egge 	int refcnt;
37157051fdcSTor Egge 	struct vmspace *vm;
37257051fdcSTor Egge 	struct proc *p;
373389d2b6eSMatthew Dillon 
374389d2b6eSMatthew Dillon 	/*
37557051fdcSTor Egge 	 * Release user portion of address space.
37657051fdcSTor Egge 	 * This releases references to vnodes,
37757051fdcSTor Egge 	 * which could cause I/O if the file has been unlinked.
37857051fdcSTor Egge 	 * Need to do this early enough that we can still sleep.
379389d2b6eSMatthew Dillon 	 *
38057051fdcSTor Egge 	 * The last exiting process to reach this point releases as
38157051fdcSTor Egge 	 * much of the environment as it can. vmspace_dofree() is the
38257051fdcSTor Egge 	 * slower fallback in case another process had a temporary
38357051fdcSTor Egge 	 * reference to the vmspace.
384389d2b6eSMatthew Dillon 	 */
38557051fdcSTor Egge 
38657051fdcSTor Egge 	p = td->td_proc;
38757051fdcSTor Egge 	vm = p->p_vmspace;
38857051fdcSTor Egge 	atomic_add_int(&vmspace0.vm_refcnt, 1);
38957051fdcSTor Egge 	do {
39057051fdcSTor Egge 		refcnt = vm->vm_refcnt;
39157051fdcSTor Egge 		if (refcnt > 1 && p->p_vmspace != &vmspace0) {
39257051fdcSTor Egge 			/* Switch now since other proc might free vmspace */
39357051fdcSTor Egge 			PROC_VMSPACE_LOCK(p);
39457051fdcSTor Egge 			p->p_vmspace = &vmspace0;
39557051fdcSTor Egge 			PROC_VMSPACE_UNLOCK(p);
39657051fdcSTor Egge 			pmap_activate(td);
39757051fdcSTor Egge 		}
39857051fdcSTor Egge 	} while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1));
39957051fdcSTor Egge 	if (refcnt == 1) {
40057051fdcSTor Egge 		if (p->p_vmspace != vm) {
40157051fdcSTor Egge 			/* vmspace not yet freed, switch back */
40257051fdcSTor Egge 			PROC_VMSPACE_LOCK(p);
40357051fdcSTor Egge 			p->p_vmspace = vm;
40457051fdcSTor Egge 			PROC_VMSPACE_UNLOCK(p);
40557051fdcSTor Egge 			pmap_activate(td);
40657051fdcSTor Egge 		}
40757051fdcSTor Egge 		pmap_remove_pages(vmspace_pmap(vm));
40857051fdcSTor Egge 		/* Switch now since this proc will free vmspace */
40957051fdcSTor Egge 		PROC_VMSPACE_LOCK(p);
41057051fdcSTor Egge 		p->p_vmspace = &vmspace0;
41157051fdcSTor Egge 		PROC_VMSPACE_UNLOCK(p);
41257051fdcSTor Egge 		pmap_activate(td);
413334f7061SPeter Wemm 		vmspace_dofree(vm);
414334f7061SPeter Wemm 	}
41557051fdcSTor Egge }
41657051fdcSTor Egge 
41757051fdcSTor Egge /* Acquire reference to vmspace owned by another process. */
41857051fdcSTor Egge 
41957051fdcSTor Egge struct vmspace *
42057051fdcSTor Egge vmspace_acquire_ref(struct proc *p)
42157051fdcSTor Egge {
42257051fdcSTor Egge 	struct vmspace *vm;
42357051fdcSTor Egge 	int refcnt;
42457051fdcSTor Egge 
42557051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
42657051fdcSTor Egge 	vm = p->p_vmspace;
42757051fdcSTor Egge 	if (vm == NULL) {
42857051fdcSTor Egge 		PROC_VMSPACE_UNLOCK(p);
42957051fdcSTor Egge 		return (NULL);
43057051fdcSTor Egge 	}
43157051fdcSTor Egge 	do {
43257051fdcSTor Egge 		refcnt = vm->vm_refcnt;
43357051fdcSTor Egge 		if (refcnt <= 0) { 	/* Avoid 0->1 transition */
43457051fdcSTor Egge 			PROC_VMSPACE_UNLOCK(p);
43557051fdcSTor Egge 			return (NULL);
43657051fdcSTor Egge 		}
43757051fdcSTor Egge 	} while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt + 1));
43857051fdcSTor Egge 	if (vm != p->p_vmspace) {
43957051fdcSTor Egge 		PROC_VMSPACE_UNLOCK(p);
44057051fdcSTor Egge 		vmspace_free(vm);
44157051fdcSTor Egge 		return (NULL);
44257051fdcSTor Egge 	}
44357051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
44457051fdcSTor Egge 	return (vm);
44557051fdcSTor Egge }
446df8bae1dSRodney W. Grimes 
4471b40f8c0SMatthew Dillon void
448780b1c09SAlan Cox _vm_map_lock(vm_map_t map, const char *file, int line)
4491b40f8c0SMatthew Dillon {
450bc91c510SAlan Cox 
45193bc4879SAlan Cox 	if (map->system_map)
45236daaecdSAlan Cox 		_mtx_lock_flags(&map->system_mtx, 0, file, line);
45312c64974SMaxime Henrion 	else
454f9819486SAttilio Rao 		(void)_sx_xlock(&map->lock, 0, file, line);
4551b40f8c0SMatthew Dillon 	map->timestamp++;
4561b40f8c0SMatthew Dillon }
4571b40f8c0SMatthew Dillon 
4580b367bd8SKonstantin Belousov static void
4590b367bd8SKonstantin Belousov vm_map_process_deferred(void)
4600e0af8ecSBrian Feldman {
4610b367bd8SKonstantin Belousov 	struct thread *td;
4620b367bd8SKonstantin Belousov 	vm_map_entry_t entry;
463655c3490SKonstantin Belousov 
4640b367bd8SKonstantin Belousov 	td = curthread;
4650b367bd8SKonstantin Belousov 
4660b367bd8SKonstantin Belousov 	while ((entry = td->td_map_def_user) != NULL) {
4670b367bd8SKonstantin Belousov 		td->td_map_def_user = entry->next;
4680b367bd8SKonstantin Belousov 		vm_map_entry_deallocate(entry, FALSE);
4690b367bd8SKonstantin Belousov 	}
4700b367bd8SKonstantin Belousov }
4710b367bd8SKonstantin Belousov 
4720b367bd8SKonstantin Belousov void
4730b367bd8SKonstantin Belousov _vm_map_unlock(vm_map_t map, const char *file, int line)
4740b367bd8SKonstantin Belousov {
4750b367bd8SKonstantin Belousov 
4760b367bd8SKonstantin Belousov 	if (map->system_map)
4770b367bd8SKonstantin Belousov 		_mtx_unlock_flags(&map->system_mtx, 0, file, line);
4780b367bd8SKonstantin Belousov 	else {
4790b367bd8SKonstantin Belousov 		_sx_xunlock(&map->lock, file, line);
4800b367bd8SKonstantin Belousov 		vm_map_process_deferred();
481655c3490SKonstantin Belousov 	}
4820e0af8ecSBrian Feldman }
4830e0af8ecSBrian Feldman 
4840e0af8ecSBrian Feldman void
485780b1c09SAlan Cox _vm_map_lock_read(vm_map_t map, const char *file, int line)
4860e0af8ecSBrian Feldman {
487bc91c510SAlan Cox 
48893bc4879SAlan Cox 	if (map->system_map)
48936daaecdSAlan Cox 		_mtx_lock_flags(&map->system_mtx, 0, file, line);
49012c64974SMaxime Henrion 	else
49105a8c414SAlan Cox 		(void)_sx_slock(&map->lock, 0, file, line);
49236daaecdSAlan Cox }
4930e0af8ecSBrian Feldman 
4940e0af8ecSBrian Feldman void
495780b1c09SAlan Cox _vm_map_unlock_read(vm_map_t map, const char *file, int line)
4960e0af8ecSBrian Feldman {
497bc91c510SAlan Cox 
49836daaecdSAlan Cox 	if (map->system_map)
49936daaecdSAlan Cox 		_mtx_unlock_flags(&map->system_mtx, 0, file, line);
5000b367bd8SKonstantin Belousov 	else {
50105a8c414SAlan Cox 		_sx_sunlock(&map->lock, file, line);
5020b367bd8SKonstantin Belousov 		vm_map_process_deferred();
5030b367bd8SKonstantin Belousov 	}
50425adb370SBrian Feldman }
50525adb370SBrian Feldman 
506d974f03cSAlan Cox int
507780b1c09SAlan Cox _vm_map_trylock(vm_map_t map, const char *file, int line)
508d974f03cSAlan Cox {
50925adb370SBrian Feldman 	int error;
51025adb370SBrian Feldman 
51136daaecdSAlan Cox 	error = map->system_map ?
51236daaecdSAlan Cox 	    !_mtx_trylock(&map->system_mtx, 0, file, line) :
51312c64974SMaxime Henrion 	    !_sx_try_xlock(&map->lock, file, line);
5143a92e5d5SAlan Cox 	if (error == 0)
5153a92e5d5SAlan Cox 		map->timestamp++;
516bc91c510SAlan Cox 	return (error == 0);
5170e0af8ecSBrian Feldman }
5180e0af8ecSBrian Feldman 
5190e0af8ecSBrian Feldman int
52072d97679SDavid Schultz _vm_map_trylock_read(vm_map_t map, const char *file, int line)
52172d97679SDavid Schultz {
52272d97679SDavid Schultz 	int error;
52372d97679SDavid Schultz 
52472d97679SDavid Schultz 	error = map->system_map ?
52572d97679SDavid Schultz 	    !_mtx_trylock(&map->system_mtx, 0, file, line) :
52605a8c414SAlan Cox 	    !_sx_try_slock(&map->lock, file, line);
52772d97679SDavid Schultz 	return (error == 0);
52872d97679SDavid Schultz }
52972d97679SDavid Schultz 
53005a8c414SAlan Cox /*
53105a8c414SAlan Cox  *	_vm_map_lock_upgrade:	[ internal use only ]
53205a8c414SAlan Cox  *
53305a8c414SAlan Cox  *	Tries to upgrade a read (shared) lock on the specified map to a write
53405a8c414SAlan Cox  *	(exclusive) lock.  Returns the value "0" if the upgrade succeeds and a
53505a8c414SAlan Cox  *	non-zero value if the upgrade fails.  If the upgrade fails, the map is
53605a8c414SAlan Cox  *	returned without a read or write lock held.
53705a8c414SAlan Cox  *
53805a8c414SAlan Cox  *	Requires that the map be read locked.
53905a8c414SAlan Cox  */
54072d97679SDavid Schultz int
541780b1c09SAlan Cox _vm_map_lock_upgrade(vm_map_t map, const char *file, int line)
5420e0af8ecSBrian Feldman {
54305a8c414SAlan Cox 	unsigned int last_timestamp;
544bc91c510SAlan Cox 
54512c64974SMaxime Henrion 	if (map->system_map) {
54605a8c414SAlan Cox #ifdef INVARIANTS
54736daaecdSAlan Cox 		_mtx_assert(&map->system_mtx, MA_OWNED, file, line);
54812c64974SMaxime Henrion #endif
54905a8c414SAlan Cox 	} else {
55005a8c414SAlan Cox 		if (!_sx_try_upgrade(&map->lock, file, line)) {
55105a8c414SAlan Cox 			last_timestamp = map->timestamp;
55205a8c414SAlan Cox 			_sx_sunlock(&map->lock, file, line);
5530b367bd8SKonstantin Belousov 			vm_map_process_deferred();
55405a8c414SAlan Cox 			/*
55505a8c414SAlan Cox 			 * If the map's timestamp does not change while the
55605a8c414SAlan Cox 			 * map is unlocked, then the upgrade succeeds.
55705a8c414SAlan Cox 			 */
55805a8c414SAlan Cox 			(void)_sx_xlock(&map->lock, 0, file, line);
55905a8c414SAlan Cox 			if (last_timestamp != map->timestamp) {
56005a8c414SAlan Cox 				_sx_xunlock(&map->lock, file, line);
56105a8c414SAlan Cox 				return (1);
56205a8c414SAlan Cox 			}
56305a8c414SAlan Cox 		}
56405a8c414SAlan Cox 	}
565bc91c510SAlan Cox 	map->timestamp++;
566bc91c510SAlan Cox 	return (0);
5670e0af8ecSBrian Feldman }
5680e0af8ecSBrian Feldman 
5690e0af8ecSBrian Feldman void
570780b1c09SAlan Cox _vm_map_lock_downgrade(vm_map_t map, const char *file, int line)
5711b40f8c0SMatthew Dillon {
572bc91c510SAlan Cox 
57312c64974SMaxime Henrion 	if (map->system_map) {
57405a8c414SAlan Cox #ifdef INVARIANTS
57536daaecdSAlan Cox 		_mtx_assert(&map->system_mtx, MA_OWNED, file, line);
57612c64974SMaxime Henrion #endif
57705a8c414SAlan Cox 	} else
57805a8c414SAlan Cox 		_sx_downgrade(&map->lock, file, line);
57905a8c414SAlan Cox }
58005a8c414SAlan Cox 
58105a8c414SAlan Cox /*
58205a8c414SAlan Cox  *	vm_map_locked:
58305a8c414SAlan Cox  *
58405a8c414SAlan Cox  *	Returns a non-zero value if the caller holds a write (exclusive) lock
58505a8c414SAlan Cox  *	on the specified map and the value "0" otherwise.
58605a8c414SAlan Cox  */
58705a8c414SAlan Cox int
58805a8c414SAlan Cox vm_map_locked(vm_map_t map)
58905a8c414SAlan Cox {
59005a8c414SAlan Cox 
59105a8c414SAlan Cox 	if (map->system_map)
59205a8c414SAlan Cox 		return (mtx_owned(&map->system_mtx));
59305a8c414SAlan Cox 	else
59405a8c414SAlan Cox 		return (sx_xlocked(&map->lock));
59525adb370SBrian Feldman }
59625adb370SBrian Feldman 
5973a0916b8SKonstantin Belousov #ifdef INVARIANTS
5983a0916b8SKonstantin Belousov static void
5993a0916b8SKonstantin Belousov _vm_map_assert_locked(vm_map_t map, const char *file, int line)
6003a0916b8SKonstantin Belousov {
6013a0916b8SKonstantin Belousov 
6023a0916b8SKonstantin Belousov 	if (map->system_map)
6033a0916b8SKonstantin Belousov 		_mtx_assert(&map->system_mtx, MA_OWNED, file, line);
6043a0916b8SKonstantin Belousov 	else
6053a0916b8SKonstantin Belousov 		_sx_assert(&map->lock, SA_XLOCKED, file, line);
6063a0916b8SKonstantin Belousov }
6073a0916b8SKonstantin Belousov 
6083a0916b8SKonstantin Belousov #if 0
6093a0916b8SKonstantin Belousov static void
6103a0916b8SKonstantin Belousov _vm_map_assert_locked_read(vm_map_t map, const char *file, int line)
6113a0916b8SKonstantin Belousov {
6123a0916b8SKonstantin Belousov 
6133a0916b8SKonstantin Belousov 	if (map->system_map)
6143a0916b8SKonstantin Belousov 		_mtx_assert(&map->system_mtx, MA_OWNED, file, line);
6153a0916b8SKonstantin Belousov 	else
6163a0916b8SKonstantin Belousov 		_sx_assert(&map->lock, SA_SLOCKED, file, line);
6173a0916b8SKonstantin Belousov }
6183a0916b8SKonstantin Belousov #endif
6193a0916b8SKonstantin Belousov 
6203a0916b8SKonstantin Belousov #define	VM_MAP_ASSERT_LOCKED(map) \
6213a0916b8SKonstantin Belousov     _vm_map_assert_locked(map, LOCK_FILE, LOCK_LINE)
6223a0916b8SKonstantin Belousov #define	VM_MAP_ASSERT_LOCKED_READ(map) \
6233a0916b8SKonstantin Belousov     _vm_map_assert_locked_read(map, LOCK_FILE, LOCK_LINE)
6243a0916b8SKonstantin Belousov #else
6253a0916b8SKonstantin Belousov #define	VM_MAP_ASSERT_LOCKED(map)
6263a0916b8SKonstantin Belousov #define	VM_MAP_ASSERT_LOCKED_READ(map)
6273a0916b8SKonstantin Belousov #endif
6283a0916b8SKonstantin Belousov 
629acd9a301SAlan Cox /*
6308304adaaSAlan Cox  *	_vm_map_unlock_and_wait:
6318304adaaSAlan Cox  *
6328304adaaSAlan Cox  *	Atomically releases the lock on the specified map and puts the calling
6338304adaaSAlan Cox  *	thread to sleep.  The calling thread will remain asleep until either
6348304adaaSAlan Cox  *	vm_map_wakeup() is performed on the map or the specified timeout is
6358304adaaSAlan Cox  *	exceeded.
6368304adaaSAlan Cox  *
6378304adaaSAlan Cox  *	WARNING!  This function does not perform deferred deallocations of
6388304adaaSAlan Cox  *	objects and map	entries.  Therefore, the calling thread is expected to
6398304adaaSAlan Cox  *	reacquire the map lock after reawakening and later perform an ordinary
6408304adaaSAlan Cox  *	unlock operation, such as vm_map_unlock(), before completing its
6418304adaaSAlan Cox  *	operation on the map.
642acd9a301SAlan Cox  */
6439688f931SAlan Cox int
6448304adaaSAlan Cox _vm_map_unlock_and_wait(vm_map_t map, int timo, const char *file, int line)
645acd9a301SAlan Cox {
646acd9a301SAlan Cox 
6473a92e5d5SAlan Cox 	mtx_lock(&map_sleep_mtx);
6488304adaaSAlan Cox 	if (map->system_map)
6498304adaaSAlan Cox 		_mtx_unlock_flags(&map->system_mtx, 0, file, line);
6508304adaaSAlan Cox 	else
6518304adaaSAlan Cox 		_sx_xunlock(&map->lock, file, line);
6528304adaaSAlan Cox 	return (msleep(&map->root, &map_sleep_mtx, PDROP | PVM, "vmmaps",
6538304adaaSAlan Cox 	    timo));
654acd9a301SAlan Cox }
655acd9a301SAlan Cox 
656acd9a301SAlan Cox /*
657acd9a301SAlan Cox  *	vm_map_wakeup:
6588304adaaSAlan Cox  *
6598304adaaSAlan Cox  *	Awaken any threads that have slept on the map using
6608304adaaSAlan Cox  *	vm_map_unlock_and_wait().
661acd9a301SAlan Cox  */
6629688f931SAlan Cox void
663acd9a301SAlan Cox vm_map_wakeup(vm_map_t map)
664acd9a301SAlan Cox {
665acd9a301SAlan Cox 
666b49ecb86SAlan Cox 	/*
6673a92e5d5SAlan Cox 	 * Acquire and release map_sleep_mtx to prevent a wakeup()
6688304adaaSAlan Cox 	 * from being performed (and lost) between the map unlock
6698304adaaSAlan Cox 	 * and the msleep() in _vm_map_unlock_and_wait().
670b49ecb86SAlan Cox 	 */
6713a92e5d5SAlan Cox 	mtx_lock(&map_sleep_mtx);
6723a92e5d5SAlan Cox 	mtx_unlock(&map_sleep_mtx);
673acd9a301SAlan Cox 	wakeup(&map->root);
674acd9a301SAlan Cox }
675acd9a301SAlan Cox 
6761b40f8c0SMatthew Dillon long
6771b40f8c0SMatthew Dillon vmspace_resident_count(struct vmspace *vmspace)
6781b40f8c0SMatthew Dillon {
6791b40f8c0SMatthew Dillon 	return pmap_resident_count(vmspace_pmap(vmspace));
6801b40f8c0SMatthew Dillon }
6811b40f8c0SMatthew Dillon 
6822bc7dd56SBruce M Simpson long
6832bc7dd56SBruce M Simpson vmspace_wired_count(struct vmspace *vmspace)
6842bc7dd56SBruce M Simpson {
6852bc7dd56SBruce M Simpson 	return pmap_wired_count(vmspace_pmap(vmspace));
6862bc7dd56SBruce M Simpson }
6872bc7dd56SBruce M Simpson 
688ff2b5645SMatthew Dillon /*
689df8bae1dSRodney W. Grimes  *	vm_map_create:
690df8bae1dSRodney W. Grimes  *
691df8bae1dSRodney W. Grimes  *	Creates and returns a new empty VM map with
692df8bae1dSRodney W. Grimes  *	the given physical map structure, and having
693df8bae1dSRodney W. Grimes  *	the given lower and upper address bounds.
694df8bae1dSRodney W. Grimes  */
6950d94caffSDavid Greenman vm_map_t
6961b40f8c0SMatthew Dillon vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max)
697df8bae1dSRodney W. Grimes {
698c0877f10SJohn Dyson 	vm_map_t result;
699df8bae1dSRodney W. Grimes 
700a163d034SWarner Losh 	result = uma_zalloc(mapzone, M_WAITOK);
70121c641b2SJohn Baldwin 	CTR1(KTR_VM, "vm_map_create: %p", result);
70292351f16SAlan Cox 	_vm_map_init(result, pmap, min, max);
703df8bae1dSRodney W. Grimes 	return (result);
704df8bae1dSRodney W. Grimes }
705df8bae1dSRodney W. Grimes 
706df8bae1dSRodney W. Grimes /*
707df8bae1dSRodney W. Grimes  * Initialize an existing vm_map structure
708df8bae1dSRodney W. Grimes  * such as that in the vmspace structure.
709df8bae1dSRodney W. Grimes  */
7108355f576SJeff Roberson static void
71192351f16SAlan Cox _vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min, vm_offset_t max)
712df8bae1dSRodney W. Grimes {
71321c641b2SJohn Baldwin 
714df8bae1dSRodney W. Grimes 	map->header.next = map->header.prev = &map->header;
7159688f931SAlan Cox 	map->needs_wakeup = FALSE;
7163075778bSJohn Dyson 	map->system_map = 0;
71792351f16SAlan Cox 	map->pmap = pmap;
718df8bae1dSRodney W. Grimes 	map->min_offset = min;
719df8bae1dSRodney W. Grimes 	map->max_offset = max;
720af7cd0c5SBrian Feldman 	map->flags = 0;
7214e94f402SAlan Cox 	map->root = NULL;
722df8bae1dSRodney W. Grimes 	map->timestamp = 0;
723df8bae1dSRodney W. Grimes }
724df8bae1dSRodney W. Grimes 
725a18b1f1dSJason Evans void
72692351f16SAlan Cox vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min, vm_offset_t max)
727a18b1f1dSJason Evans {
72892351f16SAlan Cox 
72992351f16SAlan Cox 	_vm_map_init(map, pmap, min, max);
730d923c598SAlan Cox 	mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
73112c64974SMaxime Henrion 	sx_init(&map->lock, "user map");
732a18b1f1dSJason Evans }
733a18b1f1dSJason Evans 
734df8bae1dSRodney W. Grimes /*
735b18bfc3dSJohn Dyson  *	vm_map_entry_dispose:	[ internal use only ]
736b18bfc3dSJohn Dyson  *
737b18bfc3dSJohn Dyson  *	Inverse of vm_map_entry_create.
738b18bfc3dSJohn Dyson  */
73962487bb4SJohn Dyson static void
7401b40f8c0SMatthew Dillon vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry)
741b18bfc3dSJohn Dyson {
7422b4a2c27SAlan Cox 	uma_zfree(map->system_map ? kmapentzone : mapentzone, entry);
743b18bfc3dSJohn Dyson }
744b18bfc3dSJohn Dyson 
745b18bfc3dSJohn Dyson /*
746df8bae1dSRodney W. Grimes  *	vm_map_entry_create:	[ internal use only ]
747df8bae1dSRodney W. Grimes  *
748df8bae1dSRodney W. Grimes  *	Allocates a VM map entry for insertion.
749b28cb1caSAlfred Perlstein  *	No entry fields are filled in.
750df8bae1dSRodney W. Grimes  */
751f708ef1bSPoul-Henning Kamp static vm_map_entry_t
7521b40f8c0SMatthew Dillon vm_map_entry_create(vm_map_t map)
753df8bae1dSRodney W. Grimes {
7541f6889a1SMatthew Dillon 	vm_map_entry_t new_entry;
7551f6889a1SMatthew Dillon 
7562b4a2c27SAlan Cox 	if (map->system_map)
7572b4a2c27SAlan Cox 		new_entry = uma_zalloc(kmapentzone, M_NOWAIT);
7582b4a2c27SAlan Cox 	else
759a163d034SWarner Losh 		new_entry = uma_zalloc(mapentzone, M_WAITOK);
7601f6889a1SMatthew Dillon 	if (new_entry == NULL)
7611f6889a1SMatthew Dillon 		panic("vm_map_entry_create: kernel resources exhausted");
7621f6889a1SMatthew Dillon 	return (new_entry);
763df8bae1dSRodney W. Grimes }
764df8bae1dSRodney W. Grimes 
765df8bae1dSRodney W. Grimes /*
766794316a8SAlan Cox  *	vm_map_entry_set_behavior:
767794316a8SAlan Cox  *
768794316a8SAlan Cox  *	Set the expected access behavior, either normal, random, or
769794316a8SAlan Cox  *	sequential.
770794316a8SAlan Cox  */
77162a59e8fSWarner Losh static inline void
772794316a8SAlan Cox vm_map_entry_set_behavior(vm_map_entry_t entry, u_char behavior)
773794316a8SAlan Cox {
774794316a8SAlan Cox 	entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) |
775794316a8SAlan Cox 	    (behavior & MAP_ENTRY_BEHAV_MASK);
776794316a8SAlan Cox }
777794316a8SAlan Cox 
778794316a8SAlan Cox /*
7790164e057SAlan Cox  *	vm_map_entry_set_max_free:
7800164e057SAlan Cox  *
7810164e057SAlan Cox  *	Set the max_free field in a vm_map_entry.
7820164e057SAlan Cox  */
78362a59e8fSWarner Losh static inline void
7840164e057SAlan Cox vm_map_entry_set_max_free(vm_map_entry_t entry)
7850164e057SAlan Cox {
7860164e057SAlan Cox 
7870164e057SAlan Cox 	entry->max_free = entry->adj_free;
7880164e057SAlan Cox 	if (entry->left != NULL && entry->left->max_free > entry->max_free)
7890164e057SAlan Cox 		entry->max_free = entry->left->max_free;
7900164e057SAlan Cox 	if (entry->right != NULL && entry->right->max_free > entry->max_free)
7910164e057SAlan Cox 		entry->max_free = entry->right->max_free;
7920164e057SAlan Cox }
7930164e057SAlan Cox 
7940164e057SAlan Cox /*
7954e94f402SAlan Cox  *	vm_map_entry_splay:
7964e94f402SAlan Cox  *
7970164e057SAlan Cox  *	The Sleator and Tarjan top-down splay algorithm with the
7980164e057SAlan Cox  *	following variation.  Max_free must be computed bottom-up, so
7990164e057SAlan Cox  *	on the downward pass, maintain the left and right spines in
8000164e057SAlan Cox  *	reverse order.  Then, make a second pass up each side to fix
8010164e057SAlan Cox  *	the pointers and compute max_free.  The time bound is O(log n)
8020164e057SAlan Cox  *	amortized.
8030164e057SAlan Cox  *
8040164e057SAlan Cox  *	The new root is the vm_map_entry containing "addr", or else an
8050164e057SAlan Cox  *	adjacent entry (lower or higher) if addr is not in the tree.
8060164e057SAlan Cox  *
8070164e057SAlan Cox  *	The map must be locked, and leaves it so.
8080164e057SAlan Cox  *
8090164e057SAlan Cox  *	Returns: the new root.
8104e94f402SAlan Cox  */
8114e94f402SAlan Cox static vm_map_entry_t
8120164e057SAlan Cox vm_map_entry_splay(vm_offset_t addr, vm_map_entry_t root)
8134e94f402SAlan Cox {
8140164e057SAlan Cox 	vm_map_entry_t llist, rlist;
8150164e057SAlan Cox 	vm_map_entry_t ltree, rtree;
8160164e057SAlan Cox 	vm_map_entry_t y;
8174e94f402SAlan Cox 
8180164e057SAlan Cox 	/* Special case of empty tree. */
8194e94f402SAlan Cox 	if (root == NULL)
8204e94f402SAlan Cox 		return (root);
8210164e057SAlan Cox 
8220164e057SAlan Cox 	/*
8230164e057SAlan Cox 	 * Pass One: Splay down the tree until we find addr or a NULL
8240164e057SAlan Cox 	 * pointer where addr would go.  llist and rlist are the two
8250164e057SAlan Cox 	 * sides in reverse order (bottom-up), with llist linked by
8260164e057SAlan Cox 	 * the right pointer and rlist linked by the left pointer in
8270164e057SAlan Cox 	 * the vm_map_entry.  Wait until Pass Two to set max_free on
8280164e057SAlan Cox 	 * the two spines.
8290164e057SAlan Cox 	 */
8300164e057SAlan Cox 	llist = NULL;
8310164e057SAlan Cox 	rlist = NULL;
8320164e057SAlan Cox 	for (;;) {
8330164e057SAlan Cox 		/* root is never NULL in here. */
8340164e057SAlan Cox 		if (addr < root->start) {
8350164e057SAlan Cox 			y = root->left;
8360164e057SAlan Cox 			if (y == NULL)
8374e94f402SAlan Cox 				break;
8380164e057SAlan Cox 			if (addr < y->start && y->left != NULL) {
8390164e057SAlan Cox 				/* Rotate right and put y on rlist. */
8404e94f402SAlan Cox 				root->left = y->right;
8414e94f402SAlan Cox 				y->right = root;
8420164e057SAlan Cox 				vm_map_entry_set_max_free(root);
8430164e057SAlan Cox 				root = y->left;
8440164e057SAlan Cox 				y->left = rlist;
8450164e057SAlan Cox 				rlist = y;
8460164e057SAlan Cox 			} else {
8470164e057SAlan Cox 				/* Put root on rlist. */
8480164e057SAlan Cox 				root->left = rlist;
8490164e057SAlan Cox 				rlist = root;
8504e94f402SAlan Cox 				root = y;
8514e94f402SAlan Cox 			}
8527438d60bSAlan Cox 		} else if (addr >= root->end) {
8530164e057SAlan Cox 			y = root->right;
8547438d60bSAlan Cox 			if (y == NULL)
8554e94f402SAlan Cox 				break;
8560164e057SAlan Cox 			if (addr >= y->end && y->right != NULL) {
8570164e057SAlan Cox 				/* Rotate left and put y on llist. */
8584e94f402SAlan Cox 				root->right = y->left;
8594e94f402SAlan Cox 				y->left = root;
8600164e057SAlan Cox 				vm_map_entry_set_max_free(root);
8610164e057SAlan Cox 				root = y->right;
8620164e057SAlan Cox 				y->right = llist;
8630164e057SAlan Cox 				llist = y;
8640164e057SAlan Cox 			} else {
8650164e057SAlan Cox 				/* Put root on llist. */
8660164e057SAlan Cox 				root->right = llist;
8670164e057SAlan Cox 				llist = root;
8684e94f402SAlan Cox 				root = y;
8694e94f402SAlan Cox 			}
8707438d60bSAlan Cox 		} else
8717438d60bSAlan Cox 			break;
8720164e057SAlan Cox 	}
8730164e057SAlan Cox 
8740164e057SAlan Cox 	/*
8750164e057SAlan Cox 	 * Pass Two: Walk back up the two spines, flip the pointers
8760164e057SAlan Cox 	 * and set max_free.  The subtrees of the root go at the
8770164e057SAlan Cox 	 * bottom of llist and rlist.
8780164e057SAlan Cox 	 */
8790164e057SAlan Cox 	ltree = root->left;
8800164e057SAlan Cox 	while (llist != NULL) {
8810164e057SAlan Cox 		y = llist->right;
8820164e057SAlan Cox 		llist->right = ltree;
8830164e057SAlan Cox 		vm_map_entry_set_max_free(llist);
8840164e057SAlan Cox 		ltree = llist;
8850164e057SAlan Cox 		llist = y;
8860164e057SAlan Cox 	}
8870164e057SAlan Cox 	rtree = root->right;
8880164e057SAlan Cox 	while (rlist != NULL) {
8890164e057SAlan Cox 		y = rlist->left;
8900164e057SAlan Cox 		rlist->left = rtree;
8910164e057SAlan Cox 		vm_map_entry_set_max_free(rlist);
8920164e057SAlan Cox 		rtree = rlist;
8930164e057SAlan Cox 		rlist = y;
8940164e057SAlan Cox 	}
8950164e057SAlan Cox 
8960164e057SAlan Cox 	/*
8970164e057SAlan Cox 	 * Final assembly: add ltree and rtree as subtrees of root.
8980164e057SAlan Cox 	 */
8990164e057SAlan Cox 	root->left = ltree;
9000164e057SAlan Cox 	root->right = rtree;
9010164e057SAlan Cox 	vm_map_entry_set_max_free(root);
9020164e057SAlan Cox 
9034e94f402SAlan Cox 	return (root);
9044e94f402SAlan Cox }
9054e94f402SAlan Cox 
9064e94f402SAlan Cox /*
907df8bae1dSRodney W. Grimes  *	vm_map_entry_{un,}link:
908df8bae1dSRodney W. Grimes  *
909df8bae1dSRodney W. Grimes  *	Insert/remove entries from maps.
910df8bae1dSRodney W. Grimes  */
9114e94f402SAlan Cox static void
91299c81ca9SAlan Cox vm_map_entry_link(vm_map_t map,
91399c81ca9SAlan Cox 		  vm_map_entry_t after_where,
91499c81ca9SAlan Cox 		  vm_map_entry_t entry)
91599c81ca9SAlan Cox {
91621c641b2SJohn Baldwin 
91721c641b2SJohn Baldwin 	CTR4(KTR_VM,
91821c641b2SJohn Baldwin 	    "vm_map_entry_link: map %p, nentries %d, entry %p, after %p", map,
91921c641b2SJohn Baldwin 	    map->nentries, entry, after_where);
9203a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
92199c81ca9SAlan Cox 	map->nentries++;
92299c81ca9SAlan Cox 	entry->prev = after_where;
92399c81ca9SAlan Cox 	entry->next = after_where->next;
92499c81ca9SAlan Cox 	entry->next->prev = entry;
92599c81ca9SAlan Cox 	after_where->next = entry;
9264e94f402SAlan Cox 
9274e94f402SAlan Cox 	if (after_where != &map->header) {
9284e94f402SAlan Cox 		if (after_where != map->root)
9294e94f402SAlan Cox 			vm_map_entry_splay(after_where->start, map->root);
9304e94f402SAlan Cox 		entry->right = after_where->right;
9314e94f402SAlan Cox 		entry->left = after_where;
9324e94f402SAlan Cox 		after_where->right = NULL;
9330164e057SAlan Cox 		after_where->adj_free = entry->start - after_where->end;
9340164e057SAlan Cox 		vm_map_entry_set_max_free(after_where);
9354e94f402SAlan Cox 	} else {
9364e94f402SAlan Cox 		entry->right = map->root;
9374e94f402SAlan Cox 		entry->left = NULL;
9384e94f402SAlan Cox 	}
9390164e057SAlan Cox 	entry->adj_free = (entry->next == &map->header ? map->max_offset :
9400164e057SAlan Cox 	    entry->next->start) - entry->end;
9410164e057SAlan Cox 	vm_map_entry_set_max_free(entry);
9424e94f402SAlan Cox 	map->root = entry;
943df8bae1dSRodney W. Grimes }
94499c81ca9SAlan Cox 
9454e94f402SAlan Cox static void
94699c81ca9SAlan Cox vm_map_entry_unlink(vm_map_t map,
94799c81ca9SAlan Cox 		    vm_map_entry_t entry)
94899c81ca9SAlan Cox {
9494e94f402SAlan Cox 	vm_map_entry_t next, prev, root;
95099c81ca9SAlan Cox 
9513a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
9524e94f402SAlan Cox 	if (entry != map->root)
9534e94f402SAlan Cox 		vm_map_entry_splay(entry->start, map->root);
9544e94f402SAlan Cox 	if (entry->left == NULL)
9554e94f402SAlan Cox 		root = entry->right;
9564e94f402SAlan Cox 	else {
9574e94f402SAlan Cox 		root = vm_map_entry_splay(entry->start, entry->left);
9584e94f402SAlan Cox 		root->right = entry->right;
9590164e057SAlan Cox 		root->adj_free = (entry->next == &map->header ? map->max_offset :
9600164e057SAlan Cox 		    entry->next->start) - root->end;
9610164e057SAlan Cox 		vm_map_entry_set_max_free(root);
9624e94f402SAlan Cox 	}
9634e94f402SAlan Cox 	map->root = root;
9644e94f402SAlan Cox 
9654e94f402SAlan Cox 	prev = entry->prev;
9664e94f402SAlan Cox 	next = entry->next;
96799c81ca9SAlan Cox 	next->prev = prev;
96899c81ca9SAlan Cox 	prev->next = next;
96999c81ca9SAlan Cox 	map->nentries--;
97021c641b2SJohn Baldwin 	CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map,
97121c641b2SJohn Baldwin 	    map->nentries, entry);
972df8bae1dSRodney W. Grimes }
973df8bae1dSRodney W. Grimes 
974df8bae1dSRodney W. Grimes /*
9750164e057SAlan Cox  *	vm_map_entry_resize_free:
9760164e057SAlan Cox  *
9770164e057SAlan Cox  *	Recompute the amount of free space following a vm_map_entry
9780164e057SAlan Cox  *	and propagate that value up the tree.  Call this function after
9790164e057SAlan Cox  *	resizing a map entry in-place, that is, without a call to
9800164e057SAlan Cox  *	vm_map_entry_link() or _unlink().
9810164e057SAlan Cox  *
9820164e057SAlan Cox  *	The map must be locked, and leaves it so.
9830164e057SAlan Cox  */
9840164e057SAlan Cox static void
9850164e057SAlan Cox vm_map_entry_resize_free(vm_map_t map, vm_map_entry_t entry)
9860164e057SAlan Cox {
9870164e057SAlan Cox 
9880164e057SAlan Cox 	/*
9890164e057SAlan Cox 	 * Using splay trees without parent pointers, propagating
9900164e057SAlan Cox 	 * max_free up the tree is done by moving the entry to the
9910164e057SAlan Cox 	 * root and making the change there.
9920164e057SAlan Cox 	 */
9930164e057SAlan Cox 	if (entry != map->root)
9940164e057SAlan Cox 		map->root = vm_map_entry_splay(entry->start, map->root);
9950164e057SAlan Cox 
9960164e057SAlan Cox 	entry->adj_free = (entry->next == &map->header ? map->max_offset :
9970164e057SAlan Cox 	    entry->next->start) - entry->end;
9980164e057SAlan Cox 	vm_map_entry_set_max_free(entry);
9990164e057SAlan Cox }
10000164e057SAlan Cox 
10010164e057SAlan Cox /*
1002df8bae1dSRodney W. Grimes  *	vm_map_lookup_entry:	[ internal use only ]
1003df8bae1dSRodney W. Grimes  *
1004df8bae1dSRodney W. Grimes  *	Finds the map entry containing (or
1005df8bae1dSRodney W. Grimes  *	immediately preceding) the specified address
1006df8bae1dSRodney W. Grimes  *	in the given map; the entry is returned
1007df8bae1dSRodney W. Grimes  *	in the "entry" parameter.  The boolean
1008df8bae1dSRodney W. Grimes  *	result indicates whether the address is
1009df8bae1dSRodney W. Grimes  *	actually contained in the map.
1010df8bae1dSRodney W. Grimes  */
10110d94caffSDavid Greenman boolean_t
10121b40f8c0SMatthew Dillon vm_map_lookup_entry(
10131b40f8c0SMatthew Dillon 	vm_map_t map,
10141b40f8c0SMatthew Dillon 	vm_offset_t address,
10151b40f8c0SMatthew Dillon 	vm_map_entry_t *entry)	/* OUT */
1016df8bae1dSRodney W. Grimes {
1017c0877f10SJohn Dyson 	vm_map_entry_t cur;
101805a8c414SAlan Cox 	boolean_t locked;
1019df8bae1dSRodney W. Grimes 
10204c3ef59eSAlan Cox 	/*
10214c3ef59eSAlan Cox 	 * If the map is empty, then the map entry immediately preceding
10224c3ef59eSAlan Cox 	 * "address" is the map's header.
10234c3ef59eSAlan Cox 	 */
10244c3ef59eSAlan Cox 	cur = map->root;
10254e94f402SAlan Cox 	if (cur == NULL)
10264e94f402SAlan Cox 		*entry = &map->header;
10274c3ef59eSAlan Cox 	else if (address >= cur->start && cur->end > address) {
10284c3ef59eSAlan Cox 		*entry = cur;
10294c3ef59eSAlan Cox 		return (TRUE);
103005a8c414SAlan Cox 	} else if ((locked = vm_map_locked(map)) ||
103105a8c414SAlan Cox 	    sx_try_upgrade(&map->lock)) {
103205a8c414SAlan Cox 		/*
103305a8c414SAlan Cox 		 * Splay requires a write lock on the map.  However, it only
103405a8c414SAlan Cox 		 * restructures the binary search tree; it does not otherwise
103505a8c414SAlan Cox 		 * change the map.  Thus, the map's timestamp need not change
103605a8c414SAlan Cox 		 * on a temporary upgrade.
103705a8c414SAlan Cox 		 */
10384c3ef59eSAlan Cox 		map->root = cur = vm_map_entry_splay(address, cur);
103905a8c414SAlan Cox 		if (!locked)
104005a8c414SAlan Cox 			sx_downgrade(&map->lock);
1041df8bae1dSRodney W. Grimes 
10424c3ef59eSAlan Cox 		/*
10434c3ef59eSAlan Cox 		 * If "address" is contained within a map entry, the new root
10444c3ef59eSAlan Cox 		 * is that map entry.  Otherwise, the new root is a map entry
10454c3ef59eSAlan Cox 		 * immediately before or after "address".
10464c3ef59eSAlan Cox 		 */
1047df8bae1dSRodney W. Grimes 		if (address >= cur->start) {
1048df8bae1dSRodney W. Grimes 			*entry = cur;
10494e94f402SAlan Cox 			if (cur->end > address)
1050df8bae1dSRodney W. Grimes 				return (TRUE);
10514e94f402SAlan Cox 		} else
1052df8bae1dSRodney W. Grimes 			*entry = cur->prev;
105305a8c414SAlan Cox 	} else
105405a8c414SAlan Cox 		/*
105505a8c414SAlan Cox 		 * Since the map is only locked for read access, perform a
105605a8c414SAlan Cox 		 * standard binary search tree lookup for "address".
105705a8c414SAlan Cox 		 */
105805a8c414SAlan Cox 		for (;;) {
105905a8c414SAlan Cox 			if (address < cur->start) {
106005a8c414SAlan Cox 				if (cur->left == NULL) {
106105a8c414SAlan Cox 					*entry = cur->prev;
106205a8c414SAlan Cox 					break;
106305a8c414SAlan Cox 				}
106405a8c414SAlan Cox 				cur = cur->left;
106505a8c414SAlan Cox 			} else if (cur->end > address) {
106605a8c414SAlan Cox 				*entry = cur;
106705a8c414SAlan Cox 				return (TRUE);
106805a8c414SAlan Cox 			} else {
106905a8c414SAlan Cox 				if (cur->right == NULL) {
107005a8c414SAlan Cox 					*entry = cur;
107105a8c414SAlan Cox 					break;
107205a8c414SAlan Cox 				}
107305a8c414SAlan Cox 				cur = cur->right;
107405a8c414SAlan Cox 			}
10754e94f402SAlan Cox 		}
1076df8bae1dSRodney W. Grimes 	return (FALSE);
1077df8bae1dSRodney W. Grimes }
1078df8bae1dSRodney W. Grimes 
1079df8bae1dSRodney W. Grimes /*
108030dcfc09SJohn Dyson  *	vm_map_insert:
108130dcfc09SJohn Dyson  *
108230dcfc09SJohn Dyson  *	Inserts the given whole VM object into the target
108330dcfc09SJohn Dyson  *	map at the specified address range.  The object's
108430dcfc09SJohn Dyson  *	size should match that of the address range.
108530dcfc09SJohn Dyson  *
108630dcfc09SJohn Dyson  *	Requires that the map be locked, and leaves it so.
10872aaeadf8SMatthew Dillon  *
10882aaeadf8SMatthew Dillon  *	If object is non-NULL, ref count must be bumped by caller
10892aaeadf8SMatthew Dillon  *	prior to making call to account for the new entry.
109030dcfc09SJohn Dyson  */
109130dcfc09SJohn Dyson int
1092b9dcd593SBruce Evans vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1093b9dcd593SBruce Evans 	      vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max,
1094b9dcd593SBruce Evans 	      int cow)
109530dcfc09SJohn Dyson {
1096c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
1097c0877f10SJohn Dyson 	vm_map_entry_t prev_entry;
109830dcfc09SJohn Dyson 	vm_map_entry_t temp_entry;
10999730a5daSPaul Saab 	vm_eflags_t protoeflags;
11003364c323SKonstantin Belousov 	struct uidinfo *uip;
11013364c323SKonstantin Belousov 	boolean_t charge_prev_obj;
110230dcfc09SJohn Dyson 
11033a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
11043a0916b8SKonstantin Belousov 
110530dcfc09SJohn Dyson 	/*
110630dcfc09SJohn Dyson 	 * Check that the start and end points are not bogus.
110730dcfc09SJohn Dyson 	 */
110830dcfc09SJohn Dyson 	if ((start < map->min_offset) || (end > map->max_offset) ||
110930dcfc09SJohn Dyson 	    (start >= end))
111030dcfc09SJohn Dyson 		return (KERN_INVALID_ADDRESS);
111130dcfc09SJohn Dyson 
111230dcfc09SJohn Dyson 	/*
111330dcfc09SJohn Dyson 	 * Find the entry prior to the proposed starting address; if it's part
111430dcfc09SJohn Dyson 	 * of an existing entry, this range is bogus.
111530dcfc09SJohn Dyson 	 */
111630dcfc09SJohn Dyson 	if (vm_map_lookup_entry(map, start, &temp_entry))
111730dcfc09SJohn Dyson 		return (KERN_NO_SPACE);
111830dcfc09SJohn Dyson 
111930dcfc09SJohn Dyson 	prev_entry = temp_entry;
112030dcfc09SJohn Dyson 
112130dcfc09SJohn Dyson 	/*
112230dcfc09SJohn Dyson 	 * Assert that the next entry doesn't overlap the end point.
112330dcfc09SJohn Dyson 	 */
112430dcfc09SJohn Dyson 	if ((prev_entry->next != &map->header) &&
112530dcfc09SJohn Dyson 	    (prev_entry->next->start < end))
112630dcfc09SJohn Dyson 		return (KERN_NO_SPACE);
112730dcfc09SJohn Dyson 
1128afa07f7eSJohn Dyson 	protoeflags = 0;
11293364c323SKonstantin Belousov 	charge_prev_obj = FALSE;
1130afa07f7eSJohn Dyson 
1131afa07f7eSJohn Dyson 	if (cow & MAP_COPY_ON_WRITE)
1132e5f13bddSAlan Cox 		protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
1133afa07f7eSJohn Dyson 
11344e045f93SAlan Cox 	if (cow & MAP_NOFAULT) {
1135afa07f7eSJohn Dyson 		protoeflags |= MAP_ENTRY_NOFAULT;
1136afa07f7eSJohn Dyson 
11374e045f93SAlan Cox 		KASSERT(object == NULL,
11384e045f93SAlan Cox 			("vm_map_insert: paradoxical MAP_NOFAULT request"));
11394e045f93SAlan Cox 	}
11404f79d873SMatthew Dillon 	if (cow & MAP_DISABLE_SYNCER)
11414f79d873SMatthew Dillon 		protoeflags |= MAP_ENTRY_NOSYNC;
11429730a5daSPaul Saab 	if (cow & MAP_DISABLE_COREDUMP)
11439730a5daSPaul Saab 		protoeflags |= MAP_ENTRY_NOCOREDUMP;
11444f79d873SMatthew Dillon 
11453364c323SKonstantin Belousov 	uip = NULL;
11463364c323SKonstantin Belousov 	KASSERT((object != kmem_object && object != kernel_object) ||
11473364c323SKonstantin Belousov 	    ((object == kmem_object || object == kernel_object) &&
11483364c323SKonstantin Belousov 		!(protoeflags & MAP_ENTRY_NEEDS_COPY)),
11493364c323SKonstantin Belousov 	    ("kmem or kernel object and cow"));
11503364c323SKonstantin Belousov 	if (cow & (MAP_ACC_NO_CHARGE | MAP_NOFAULT))
11513364c323SKonstantin Belousov 		goto charged;
11523364c323SKonstantin Belousov 	if ((cow & MAP_ACC_CHARGED) || ((prot & VM_PROT_WRITE) &&
11533364c323SKonstantin Belousov 	    ((protoeflags & MAP_ENTRY_NEEDS_COPY) || object == NULL))) {
11543364c323SKonstantin Belousov 		if (!(cow & MAP_ACC_CHARGED) && !swap_reserve(end - start))
11553364c323SKonstantin Belousov 			return (KERN_RESOURCE_SHORTAGE);
115641c22744SKonstantin Belousov 		KASSERT(object == NULL || (protoeflags & MAP_ENTRY_NEEDS_COPY) ||
11573364c323SKonstantin Belousov 		    object->uip == NULL,
11583364c323SKonstantin Belousov 		    ("OVERCOMMIT: vm_map_insert o %p", object));
11593364c323SKonstantin Belousov 		uip = curthread->td_ucred->cr_ruidinfo;
11603364c323SKonstantin Belousov 		uihold(uip);
11613364c323SKonstantin Belousov 		if (object == NULL && !(protoeflags & MAP_ENTRY_NEEDS_COPY))
11623364c323SKonstantin Belousov 			charge_prev_obj = TRUE;
11633364c323SKonstantin Belousov 	}
11643364c323SKonstantin Belousov 
11653364c323SKonstantin Belousov charged:
1166*f8616ebfSAlan Cox 	/* Expand the kernel pmap, if necessary. */
1167*f8616ebfSAlan Cox 	if (map == kernel_map && end > kernel_vm_end)
1168*f8616ebfSAlan Cox 		pmap_growkernel(end);
11691d284e00SAlan Cox 	if (object != NULL) {
117030dcfc09SJohn Dyson 		/*
11711d284e00SAlan Cox 		 * OBJ_ONEMAPPING must be cleared unless this mapping
11721d284e00SAlan Cox 		 * is trivially proven to be the only mapping for any
11731d284e00SAlan Cox 		 * of the object's pages.  (Object granularity
11741d284e00SAlan Cox 		 * reference counting is insufficient to recognize
11751d284e00SAlan Cox 		 * aliases with precision.)
117630dcfc09SJohn Dyson 		 */
11771d284e00SAlan Cox 		VM_OBJECT_LOCK(object);
11781d284e00SAlan Cox 		if (object->ref_count > 1 || object->shadow_count != 0)
11792aaeadf8SMatthew Dillon 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
11801d284e00SAlan Cox 		VM_OBJECT_UNLOCK(object);
11814e045f93SAlan Cox 	}
11824e045f93SAlan Cox 	else if ((prev_entry != &map->header) &&
11834e045f93SAlan Cox 		 (prev_entry->eflags == protoeflags) &&
11848cc7e047SJohn Dyson 		 (prev_entry->end == start) &&
11854e045f93SAlan Cox 		 (prev_entry->wired_count == 0) &&
11863364c323SKonstantin Belousov 		 (prev_entry->uip == uip ||
11873364c323SKonstantin Belousov 		  (prev_entry->object.vm_object != NULL &&
11883364c323SKonstantin Belousov 		   (prev_entry->object.vm_object->uip == uip))) &&
11898cc7e047SJohn Dyson 		   vm_object_coalesce(prev_entry->object.vm_object,
119057a21abaSAlan Cox 		       prev_entry->offset,
11918cc7e047SJohn Dyson 		       (vm_size_t)(prev_entry->end - prev_entry->start),
11923364c323SKonstantin Belousov 		       (vm_size_t)(end - prev_entry->end), charge_prev_obj)) {
119330dcfc09SJohn Dyson 		/*
11942aaeadf8SMatthew Dillon 		 * We were able to extend the object.  Determine if we
11952aaeadf8SMatthew Dillon 		 * can extend the previous map entry to include the
11962aaeadf8SMatthew Dillon 		 * new range as well.
119730dcfc09SJohn Dyson 		 */
11988cc7e047SJohn Dyson 		if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
11998cc7e047SJohn Dyson 		    (prev_entry->protection == prot) &&
12008cc7e047SJohn Dyson 		    (prev_entry->max_protection == max)) {
120130dcfc09SJohn Dyson 			map->size += (end - prev_entry->end);
120230dcfc09SJohn Dyson 			prev_entry->end = end;
12030164e057SAlan Cox 			vm_map_entry_resize_free(map, prev_entry);
12044e71e795SMatthew Dillon 			vm_map_simplify_entry(map, prev_entry);
12053364c323SKonstantin Belousov 			if (uip != NULL)
12063364c323SKonstantin Belousov 				uifree(uip);
120730dcfc09SJohn Dyson 			return (KERN_SUCCESS);
120830dcfc09SJohn Dyson 		}
12098cc7e047SJohn Dyson 
12102aaeadf8SMatthew Dillon 		/*
12112aaeadf8SMatthew Dillon 		 * If we can extend the object but cannot extend the
12122aaeadf8SMatthew Dillon 		 * map entry, we have to create a new map entry.  We
12132aaeadf8SMatthew Dillon 		 * must bump the ref count on the extended object to
12144e71e795SMatthew Dillon 		 * account for it.  object may be NULL.
12152aaeadf8SMatthew Dillon 		 */
12162aaeadf8SMatthew Dillon 		object = prev_entry->object.vm_object;
12172aaeadf8SMatthew Dillon 		offset = prev_entry->offset +
12182aaeadf8SMatthew Dillon 			(prev_entry->end - prev_entry->start);
12198cc7e047SJohn Dyson 		vm_object_reference(object);
12203364c323SKonstantin Belousov 		if (uip != NULL && object != NULL && object->uip != NULL &&
12213364c323SKonstantin Belousov 		    !(prev_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
12223364c323SKonstantin Belousov 			/* Object already accounts for this uid. */
12233364c323SKonstantin Belousov 			uifree(uip);
12243364c323SKonstantin Belousov 			uip = NULL;
12253364c323SKonstantin Belousov 		}
1226b18bfc3dSJohn Dyson 	}
12272aaeadf8SMatthew Dillon 
12282aaeadf8SMatthew Dillon 	/*
12292aaeadf8SMatthew Dillon 	 * NOTE: if conditionals fail, object can be NULL here.  This occurs
12302aaeadf8SMatthew Dillon 	 * in things like the buffer map where we manage kva but do not manage
12312aaeadf8SMatthew Dillon 	 * backing objects.
12322aaeadf8SMatthew Dillon 	 */
12338cc7e047SJohn Dyson 
123430dcfc09SJohn Dyson 	/*
123530dcfc09SJohn Dyson 	 * Create a new entry
123630dcfc09SJohn Dyson 	 */
123730dcfc09SJohn Dyson 	new_entry = vm_map_entry_create(map);
123830dcfc09SJohn Dyson 	new_entry->start = start;
123930dcfc09SJohn Dyson 	new_entry->end = end;
12403364c323SKonstantin Belousov 	new_entry->uip = NULL;
124130dcfc09SJohn Dyson 
1242afa07f7eSJohn Dyson 	new_entry->eflags = protoeflags;
124330dcfc09SJohn Dyson 	new_entry->object.vm_object = object;
124430dcfc09SJohn Dyson 	new_entry->offset = offset;
12452267af78SJulian Elischer 	new_entry->avail_ssize = 0;
12462267af78SJulian Elischer 
124730dcfc09SJohn Dyson 	new_entry->inheritance = VM_INHERIT_DEFAULT;
124830dcfc09SJohn Dyson 	new_entry->protection = prot;
124930dcfc09SJohn Dyson 	new_entry->max_protection = max;
125030dcfc09SJohn Dyson 	new_entry->wired_count = 0;
1251e5f251d2SAlan Cox 
12523364c323SKonstantin Belousov 	KASSERT(uip == NULL || !ENTRY_CHARGED(new_entry),
12533364c323SKonstantin Belousov 	    ("OVERCOMMIT: vm_map_insert leaks vm_map %p", new_entry));
12543364c323SKonstantin Belousov 	new_entry->uip = uip;
12553364c323SKonstantin Belousov 
125630dcfc09SJohn Dyson 	/*
125730dcfc09SJohn Dyson 	 * Insert the new entry into the list
125830dcfc09SJohn Dyson 	 */
125930dcfc09SJohn Dyson 	vm_map_entry_link(map, prev_entry, new_entry);
126030dcfc09SJohn Dyson 	map->size += new_entry->end - new_entry->start;
126130dcfc09SJohn Dyson 
12621a484d28SMatthew Dillon #if 0
12631a484d28SMatthew Dillon 	/*
12641a484d28SMatthew Dillon 	 * Temporarily removed to avoid MAP_STACK panic, due to
12651a484d28SMatthew Dillon 	 * MAP_STACK being a huge hack.  Will be added back in
12661a484d28SMatthew Dillon 	 * when MAP_STACK (and the user stack mapping) is fixed.
12671a484d28SMatthew Dillon 	 */
12684e71e795SMatthew Dillon 	/*
12694e71e795SMatthew Dillon 	 * It may be possible to simplify the entry
12704e71e795SMatthew Dillon 	 */
12714e71e795SMatthew Dillon 	vm_map_simplify_entry(map, new_entry);
12721a484d28SMatthew Dillon #endif
12734e71e795SMatthew Dillon 
12744f79d873SMatthew Dillon 	if (cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) {
12754da4d293SAlan Cox 		vm_map_pmap_enter(map, start, prot,
1276e972780aSAlan Cox 				    object, OFF_TO_IDX(offset), end - start,
1277e972780aSAlan Cox 				    cow & MAP_PREFAULT_PARTIAL);
12784f79d873SMatthew Dillon 	}
1279e972780aSAlan Cox 
128030dcfc09SJohn Dyson 	return (KERN_SUCCESS);
128130dcfc09SJohn Dyson }
128230dcfc09SJohn Dyson 
128330dcfc09SJohn Dyson /*
12840164e057SAlan Cox  *	vm_map_findspace:
12850164e057SAlan Cox  *
12860164e057SAlan Cox  *	Find the first fit (lowest VM address) for "length" free bytes
12870164e057SAlan Cox  *	beginning at address >= start in the given map.
12880164e057SAlan Cox  *
12890164e057SAlan Cox  *	In a vm_map_entry, "adj_free" is the amount of free space
12900164e057SAlan Cox  *	adjacent (higher address) to this entry, and "max_free" is the
12910164e057SAlan Cox  *	maximum amount of contiguous free space in its subtree.  This
12920164e057SAlan Cox  *	allows finding a free region in one path down the tree, so
12930164e057SAlan Cox  *	O(log n) amortized with splay trees.
12940164e057SAlan Cox  *
12950164e057SAlan Cox  *	The map must be locked, and leaves it so.
12960164e057SAlan Cox  *
12970164e057SAlan Cox  *	Returns: 0 on success, and starting address in *addr,
12980164e057SAlan Cox  *		 1 if insufficient space.
1299df8bae1dSRodney W. Grimes  */
1300df8bae1dSRodney W. Grimes int
13010164e057SAlan Cox vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length,
13020164e057SAlan Cox     vm_offset_t *addr)	/* OUT */
1303df8bae1dSRodney W. Grimes {
13040164e057SAlan Cox 	vm_map_entry_t entry;
1305*f8616ebfSAlan Cox 	vm_offset_t st;
1306df8bae1dSRodney W. Grimes 
1307986b43f8SAlan Cox 	/*
1308986b43f8SAlan Cox 	 * Request must fit within min/max VM address and must avoid
1309986b43f8SAlan Cox 	 * address wrap.
1310986b43f8SAlan Cox 	 */
1311df8bae1dSRodney W. Grimes 	if (start < map->min_offset)
1312df8bae1dSRodney W. Grimes 		start = map->min_offset;
1313986b43f8SAlan Cox 	if (start + length > map->max_offset || start + length < start)
1314df8bae1dSRodney W. Grimes 		return (1);
1315df8bae1dSRodney W. Grimes 
13160164e057SAlan Cox 	/* Empty tree means wide open address space. */
13170164e057SAlan Cox 	if (map->root == NULL) {
1318df8bae1dSRodney W. Grimes 		*addr = start;
1319*f8616ebfSAlan Cox 		return (0);
132099448ed1SJohn Dyson 	}
13210164e057SAlan Cox 
13220164e057SAlan Cox 	/*
13230164e057SAlan Cox 	 * After splay, if start comes before root node, then there
13240164e057SAlan Cox 	 * must be a gap from start to the root.
13250164e057SAlan Cox 	 */
13260164e057SAlan Cox 	map->root = vm_map_entry_splay(start, map->root);
13270164e057SAlan Cox 	if (start + length <= map->root->start) {
13280164e057SAlan Cox 		*addr = start;
1329*f8616ebfSAlan Cox 		return (0);
13300164e057SAlan Cox 	}
13310164e057SAlan Cox 
13320164e057SAlan Cox 	/*
13330164e057SAlan Cox 	 * Root is the last node that might begin its gap before
1334986b43f8SAlan Cox 	 * start, and this is the last comparison where address
1335986b43f8SAlan Cox 	 * wrap might be a problem.
13360164e057SAlan Cox 	 */
13370164e057SAlan Cox 	st = (start > map->root->end) ? start : map->root->end;
1338986b43f8SAlan Cox 	if (length <= map->root->end + map->root->adj_free - st) {
13390164e057SAlan Cox 		*addr = st;
1340*f8616ebfSAlan Cox 		return (0);
13410164e057SAlan Cox 	}
13420164e057SAlan Cox 
13430164e057SAlan Cox 	/* With max_free, can immediately tell if no solution. */
13440164e057SAlan Cox 	entry = map->root->right;
13450164e057SAlan Cox 	if (entry == NULL || length > entry->max_free)
13460164e057SAlan Cox 		return (1);
13470164e057SAlan Cox 
13480164e057SAlan Cox 	/*
13490164e057SAlan Cox 	 * Search the right subtree in the order: left subtree, root,
13500164e057SAlan Cox 	 * right subtree (first fit).  The previous splay implies that
13510164e057SAlan Cox 	 * all regions in the right subtree have addresses > start.
13520164e057SAlan Cox 	 */
13530164e057SAlan Cox 	while (entry != NULL) {
13540164e057SAlan Cox 		if (entry->left != NULL && entry->left->max_free >= length)
13550164e057SAlan Cox 			entry = entry->left;
13560164e057SAlan Cox 		else if (entry->adj_free >= length) {
13570164e057SAlan Cox 			*addr = entry->end;
1358*f8616ebfSAlan Cox 			return (0);
13590164e057SAlan Cox 		} else
13600164e057SAlan Cox 			entry = entry->right;
13610164e057SAlan Cox 	}
13620164e057SAlan Cox 
13630164e057SAlan Cox 	/* Can't get here, so panic if we do. */
13640164e057SAlan Cox 	panic("vm_map_findspace: max_free corrupt");
1365df8bae1dSRodney W. Grimes }
1366df8bae1dSRodney W. Grimes 
1367d239bd3cSKonstantin Belousov int
1368d239bd3cSKonstantin Belousov vm_map_fixed(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1369b8ca4ef2SAlan Cox     vm_offset_t start, vm_size_t length, vm_prot_t prot,
1370d239bd3cSKonstantin Belousov     vm_prot_t max, int cow)
1371d239bd3cSKonstantin Belousov {
1372b8ca4ef2SAlan Cox 	vm_offset_t end;
1373d239bd3cSKonstantin Belousov 	int result;
1374d239bd3cSKonstantin Belousov 
1375d239bd3cSKonstantin Belousov 	end = start + length;
1376897d81a0SKonstantin Belousov 	vm_map_lock(map);
1377d239bd3cSKonstantin Belousov 	VM_MAP_RANGE_CHECK(map, start, end);
1378655c3490SKonstantin Belousov 	(void) vm_map_delete(map, start, end);
1379d239bd3cSKonstantin Belousov 	result = vm_map_insert(map, object, offset, start, end, prot,
1380d239bd3cSKonstantin Belousov 	    max, cow);
1381d239bd3cSKonstantin Belousov 	vm_map_unlock(map);
1382d239bd3cSKonstantin Belousov 	return (result);
1383d239bd3cSKonstantin Belousov }
1384d239bd3cSKonstantin Belousov 
1385df8bae1dSRodney W. Grimes /*
1386df8bae1dSRodney W. Grimes  *	vm_map_find finds an unallocated region in the target address
1387df8bae1dSRodney W. Grimes  *	map with the given length.  The search is defined to be
1388df8bae1dSRodney W. Grimes  *	first-fit from the specified address; the region found is
1389df8bae1dSRodney W. Grimes  *	returned in the same parameter.
1390df8bae1dSRodney W. Grimes  *
13912aaeadf8SMatthew Dillon  *	If object is non-NULL, ref count must be bumped by caller
13922aaeadf8SMatthew Dillon  *	prior to making call to account for the new entry.
1393df8bae1dSRodney W. Grimes  */
1394df8bae1dSRodney W. Grimes int
1395b9dcd593SBruce Evans vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1396b9dcd593SBruce Evans 	    vm_offset_t *addr,	/* IN/OUT */
139726c538ffSAlan Cox 	    vm_size_t length, int find_space, vm_prot_t prot,
1398b9dcd593SBruce Evans 	    vm_prot_t max, int cow)
1399df8bae1dSRodney W. Grimes {
1400c0877f10SJohn Dyson 	vm_offset_t start;
14016eaee3feSAlan Cox 	int result;
1402df8bae1dSRodney W. Grimes 
1403df8bae1dSRodney W. Grimes 	start = *addr;
1404bea41bcfSDavid Greenman 	vm_map_lock(map);
140526c538ffSAlan Cox 	do {
140626c538ffSAlan Cox 		if (find_space != VMFS_NO_SPACE) {
1407df8bae1dSRodney W. Grimes 			if (vm_map_findspace(map, start, length, addr)) {
1408df8bae1dSRodney W. Grimes 				vm_map_unlock(map);
1409df8bae1dSRodney W. Grimes 				return (KERN_NO_SPACE);
1410df8bae1dSRodney W. Grimes 			}
1411ca596a25SJuli Mallett 			switch (find_space) {
1412ca596a25SJuli Mallett 			case VMFS_ALIGNED_SPACE:
141326c538ffSAlan Cox 				pmap_align_superpage(object, offset, addr,
141426c538ffSAlan Cox 				    length);
1415ca596a25SJuli Mallett 				break;
1416ca596a25SJuli Mallett #ifdef VMFS_TLB_ALIGNED_SPACE
1417ca596a25SJuli Mallett 			case VMFS_TLB_ALIGNED_SPACE:
1418ca596a25SJuli Mallett 				pmap_align_tlb(addr);
1419ca596a25SJuli Mallett 				break;
1420ca596a25SJuli Mallett #endif
1421ca596a25SJuli Mallett 			default:
1422ca596a25SJuli Mallett 				break;
1423ca596a25SJuli Mallett 			}
1424ca596a25SJuli Mallett 
1425df8bae1dSRodney W. Grimes 			start = *addr;
1426df8bae1dSRodney W. Grimes 		}
142726c538ffSAlan Cox 		result = vm_map_insert(map, object, offset, start, start +
142826c538ffSAlan Cox 		    length, prot, max, cow);
14299b55fc04SAlan Cox 	} while (result == KERN_NO_SPACE && (find_space == VMFS_ALIGNED_SPACE
14309b55fc04SAlan Cox #ifdef VMFS_TLB_ALIGNED_SPACE
14319b55fc04SAlan Cox 	    || find_space == VMFS_TLB_ALIGNED_SPACE
14329b55fc04SAlan Cox #endif
14339b55fc04SAlan Cox 	    ));
1434df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1435df8bae1dSRodney W. Grimes 	return (result);
1436df8bae1dSRodney W. Grimes }
1437df8bae1dSRodney W. Grimes 
1438df8bae1dSRodney W. Grimes /*
1439b7b2aac2SJohn Dyson  *	vm_map_simplify_entry:
144067bf6868SJohn Dyson  *
14414e71e795SMatthew Dillon  *	Simplify the given map entry by merging with either neighbor.  This
14424e71e795SMatthew Dillon  *	routine also has the ability to merge with both neighbors.
14434e71e795SMatthew Dillon  *
14444e71e795SMatthew Dillon  *	The map must be locked.
14454e71e795SMatthew Dillon  *
14464e71e795SMatthew Dillon  *	This routine guarentees that the passed entry remains valid (though
14474e71e795SMatthew Dillon  *	possibly extended).  When merging, this routine may delete one or
14484e71e795SMatthew Dillon  *	both neighbors.
1449df8bae1dSRodney W. Grimes  */
1450b7b2aac2SJohn Dyson void
14511b40f8c0SMatthew Dillon vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry)
1452df8bae1dSRodney W. Grimes {
1453308c24baSJohn Dyson 	vm_map_entry_t next, prev;
1454b7b2aac2SJohn Dyson 	vm_size_t prevsize, esize;
1455df8bae1dSRodney W. Grimes 
1456acd9a301SAlan Cox 	if (entry->eflags & (MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP))
1457df8bae1dSRodney W. Grimes 		return;
1458308c24baSJohn Dyson 
1459308c24baSJohn Dyson 	prev = entry->prev;
1460308c24baSJohn Dyson 	if (prev != &map->header) {
146167bf6868SJohn Dyson 		prevsize = prev->end - prev->start;
146267bf6868SJohn Dyson 		if ( (prev->end == entry->start) &&
146367bf6868SJohn Dyson 		     (prev->object.vm_object == entry->object.vm_object) &&
146495e5e988SJohn Dyson 		     (!prev->object.vm_object ||
146567bf6868SJohn Dyson 			(prev->offset + prevsize == entry->offset)) &&
1466afa07f7eSJohn Dyson 		     (prev->eflags == entry->eflags) &&
146767bf6868SJohn Dyson 		     (prev->protection == entry->protection) &&
146867bf6868SJohn Dyson 		     (prev->max_protection == entry->max_protection) &&
146967bf6868SJohn Dyson 		     (prev->inheritance == entry->inheritance) &&
14703364c323SKonstantin Belousov 		     (prev->wired_count == entry->wired_count) &&
14713364c323SKonstantin Belousov 		     (prev->uip == entry->uip)) {
1472308c24baSJohn Dyson 			vm_map_entry_unlink(map, prev);
1473308c24baSJohn Dyson 			entry->start = prev->start;
1474308c24baSJohn Dyson 			entry->offset = prev->offset;
14750164e057SAlan Cox 			if (entry->prev != &map->header)
14760164e057SAlan Cox 				vm_map_entry_resize_free(map, entry->prev);
14777fd10fb3SKonstantin Belousov 
14787fd10fb3SKonstantin Belousov 			/*
1479b0994946SKonstantin Belousov 			 * If the backing object is a vnode object,
1480b0994946SKonstantin Belousov 			 * vm_object_deallocate() calls vrele().
1481b0994946SKonstantin Belousov 			 * However, vrele() does not lock the vnode
1482b0994946SKonstantin Belousov 			 * because the vnode has additional
1483b0994946SKonstantin Belousov 			 * references.  Thus, the map lock can be kept
1484b0994946SKonstantin Belousov 			 * without causing a lock-order reversal with
1485b0994946SKonstantin Belousov 			 * the vnode lock.
14867fd10fb3SKonstantin Belousov 			 */
1487b18bfc3dSJohn Dyson 			if (prev->object.vm_object)
1488308c24baSJohn Dyson 				vm_object_deallocate(prev->object.vm_object);
14893364c323SKonstantin Belousov 			if (prev->uip != NULL)
14903364c323SKonstantin Belousov 				uifree(prev->uip);
1491308c24baSJohn Dyson 			vm_map_entry_dispose(map, prev);
1492308c24baSJohn Dyson 		}
1493308c24baSJohn Dyson 	}
1494de5f6a77SJohn Dyson 
1495de5f6a77SJohn Dyson 	next = entry->next;
1496308c24baSJohn Dyson 	if (next != &map->header) {
149767bf6868SJohn Dyson 		esize = entry->end - entry->start;
149867bf6868SJohn Dyson 		if ((entry->end == next->start) &&
149967bf6868SJohn Dyson 		    (next->object.vm_object == entry->object.vm_object) &&
150067bf6868SJohn Dyson 		     (!entry->object.vm_object ||
150167bf6868SJohn Dyson 			(entry->offset + esize == next->offset)) &&
1502afa07f7eSJohn Dyson 		    (next->eflags == entry->eflags) &&
150367bf6868SJohn Dyson 		    (next->protection == entry->protection) &&
150467bf6868SJohn Dyson 		    (next->max_protection == entry->max_protection) &&
150567bf6868SJohn Dyson 		    (next->inheritance == entry->inheritance) &&
15063364c323SKonstantin Belousov 		    (next->wired_count == entry->wired_count) &&
15073364c323SKonstantin Belousov 		    (next->uip == entry->uip)) {
1508de5f6a77SJohn Dyson 			vm_map_entry_unlink(map, next);
1509de5f6a77SJohn Dyson 			entry->end = next->end;
15100164e057SAlan Cox 			vm_map_entry_resize_free(map, entry);
15117fd10fb3SKonstantin Belousov 
15127fd10fb3SKonstantin Belousov 			/*
15137fd10fb3SKonstantin Belousov 			 * See comment above.
15147fd10fb3SKonstantin Belousov 			 */
1515b18bfc3dSJohn Dyson 			if (next->object.vm_object)
1516de5f6a77SJohn Dyson 				vm_object_deallocate(next->object.vm_object);
15173364c323SKonstantin Belousov 			if (next->uip != NULL)
15183364c323SKonstantin Belousov 				uifree(next->uip);
1519de5f6a77SJohn Dyson 			vm_map_entry_dispose(map, next);
1520df8bae1dSRodney W. Grimes 		}
1521df8bae1dSRodney W. Grimes 	}
1522de5f6a77SJohn Dyson }
1523df8bae1dSRodney W. Grimes /*
1524df8bae1dSRodney W. Grimes  *	vm_map_clip_start:	[ internal use only ]
1525df8bae1dSRodney W. Grimes  *
1526df8bae1dSRodney W. Grimes  *	Asserts that the given entry begins at or after
1527df8bae1dSRodney W. Grimes  *	the specified address; if necessary,
1528df8bae1dSRodney W. Grimes  *	it splits the entry into two.
1529df8bae1dSRodney W. Grimes  */
1530df8bae1dSRodney W. Grimes #define vm_map_clip_start(map, entry, startaddr) \
1531df8bae1dSRodney W. Grimes { \
1532df8bae1dSRodney W. Grimes 	if (startaddr > entry->start) \
1533df8bae1dSRodney W. Grimes 		_vm_map_clip_start(map, entry, startaddr); \
1534df8bae1dSRodney W. Grimes }
1535df8bae1dSRodney W. Grimes 
1536df8bae1dSRodney W. Grimes /*
1537df8bae1dSRodney W. Grimes  *	This routine is called only when it is known that
1538df8bae1dSRodney W. Grimes  *	the entry must be split.
1539df8bae1dSRodney W. Grimes  */
15400d94caffSDavid Greenman static void
15411b40f8c0SMatthew Dillon _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start)
1542df8bae1dSRodney W. Grimes {
1543c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
1544df8bae1dSRodney W. Grimes 
15453a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
15463a0916b8SKonstantin Belousov 
1547df8bae1dSRodney W. Grimes 	/*
15480d94caffSDavid Greenman 	 * Split off the front portion -- note that we must insert the new
15490d94caffSDavid Greenman 	 * entry BEFORE this one, so that this entry has the specified
15500d94caffSDavid Greenman 	 * starting address.
1551df8bae1dSRodney W. Grimes 	 */
1552f32dbbeeSJohn Dyson 	vm_map_simplify_entry(map, entry);
1553f32dbbeeSJohn Dyson 
155411cccda1SJohn Dyson 	/*
155511cccda1SJohn Dyson 	 * If there is no object backing this entry, we might as well create
155611cccda1SJohn Dyson 	 * one now.  If we defer it, an object can get created after the map
155711cccda1SJohn Dyson 	 * is clipped, and individual objects will be created for the split-up
155811cccda1SJohn Dyson 	 * map.  This is a bit of a hack, but is also about the best place to
155911cccda1SJohn Dyson 	 * put this improvement.
156011cccda1SJohn Dyson 	 */
15614e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL && !map->system_map) {
156211cccda1SJohn Dyson 		vm_object_t object;
156311cccda1SJohn Dyson 		object = vm_object_allocate(OBJT_DEFAULT,
1564c2e11a03SJohn Dyson 				atop(entry->end - entry->start));
156511cccda1SJohn Dyson 		entry->object.vm_object = object;
156611cccda1SJohn Dyson 		entry->offset = 0;
15673364c323SKonstantin Belousov 		if (entry->uip != NULL) {
15683364c323SKonstantin Belousov 			object->uip = entry->uip;
15693364c323SKonstantin Belousov 			object->charge = entry->end - entry->start;
15703364c323SKonstantin Belousov 			entry->uip = NULL;
15713364c323SKonstantin Belousov 		}
15723364c323SKonstantin Belousov 	} else if (entry->object.vm_object != NULL &&
15733364c323SKonstantin Belousov 		   ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) &&
15743364c323SKonstantin Belousov 		   entry->uip != NULL) {
15753364c323SKonstantin Belousov 		VM_OBJECT_LOCK(entry->object.vm_object);
15763364c323SKonstantin Belousov 		KASSERT(entry->object.vm_object->uip == NULL,
15773364c323SKonstantin Belousov 		    ("OVERCOMMIT: vm_entry_clip_start: both uip e %p", entry));
15783364c323SKonstantin Belousov 		entry->object.vm_object->uip = entry->uip;
15793364c323SKonstantin Belousov 		entry->object.vm_object->charge = entry->end - entry->start;
15803364c323SKonstantin Belousov 		VM_OBJECT_UNLOCK(entry->object.vm_object);
15813364c323SKonstantin Belousov 		entry->uip = NULL;
158211cccda1SJohn Dyson 	}
158311cccda1SJohn Dyson 
1584df8bae1dSRodney W. Grimes 	new_entry = vm_map_entry_create(map);
1585df8bae1dSRodney W. Grimes 	*new_entry = *entry;
1586df8bae1dSRodney W. Grimes 
1587df8bae1dSRodney W. Grimes 	new_entry->end = start;
1588df8bae1dSRodney W. Grimes 	entry->offset += (start - entry->start);
1589df8bae1dSRodney W. Grimes 	entry->start = start;
15903364c323SKonstantin Belousov 	if (new_entry->uip != NULL)
15913364c323SKonstantin Belousov 		uihold(entry->uip);
1592df8bae1dSRodney W. Grimes 
1593df8bae1dSRodney W. Grimes 	vm_map_entry_link(map, entry->prev, new_entry);
1594df8bae1dSRodney W. Grimes 
15959fdfe602SMatthew Dillon 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1596df8bae1dSRodney W. Grimes 		vm_object_reference(new_entry->object.vm_object);
1597df8bae1dSRodney W. Grimes 	}
1598c0877f10SJohn Dyson }
1599df8bae1dSRodney W. Grimes 
1600df8bae1dSRodney W. Grimes /*
1601df8bae1dSRodney W. Grimes  *	vm_map_clip_end:	[ internal use only ]
1602df8bae1dSRodney W. Grimes  *
1603df8bae1dSRodney W. Grimes  *	Asserts that the given entry ends at or before
1604df8bae1dSRodney W. Grimes  *	the specified address; if necessary,
1605df8bae1dSRodney W. Grimes  *	it splits the entry into two.
1606df8bae1dSRodney W. Grimes  */
1607df8bae1dSRodney W. Grimes #define vm_map_clip_end(map, entry, endaddr) \
1608df8bae1dSRodney W. Grimes { \
1609af045176SPoul-Henning Kamp 	if ((endaddr) < (entry->end)) \
1610af045176SPoul-Henning Kamp 		_vm_map_clip_end((map), (entry), (endaddr)); \
1611df8bae1dSRodney W. Grimes }
1612df8bae1dSRodney W. Grimes 
1613df8bae1dSRodney W. Grimes /*
1614df8bae1dSRodney W. Grimes  *	This routine is called only when it is known that
1615df8bae1dSRodney W. Grimes  *	the entry must be split.
1616df8bae1dSRodney W. Grimes  */
16170d94caffSDavid Greenman static void
16181b40f8c0SMatthew Dillon _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end)
1619df8bae1dSRodney W. Grimes {
1620c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
1621df8bae1dSRodney W. Grimes 
16223a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
16233a0916b8SKonstantin Belousov 
1624df8bae1dSRodney W. Grimes 	/*
162511cccda1SJohn Dyson 	 * If there is no object backing this entry, we might as well create
162611cccda1SJohn Dyson 	 * one now.  If we defer it, an object can get created after the map
162711cccda1SJohn Dyson 	 * is clipped, and individual objects will be created for the split-up
162811cccda1SJohn Dyson 	 * map.  This is a bit of a hack, but is also about the best place to
162911cccda1SJohn Dyson 	 * put this improvement.
163011cccda1SJohn Dyson 	 */
16314e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL && !map->system_map) {
163211cccda1SJohn Dyson 		vm_object_t object;
163311cccda1SJohn Dyson 		object = vm_object_allocate(OBJT_DEFAULT,
1634c2e11a03SJohn Dyson 				atop(entry->end - entry->start));
163511cccda1SJohn Dyson 		entry->object.vm_object = object;
163611cccda1SJohn Dyson 		entry->offset = 0;
16373364c323SKonstantin Belousov 		if (entry->uip != NULL) {
16383364c323SKonstantin Belousov 			object->uip = entry->uip;
16393364c323SKonstantin Belousov 			object->charge = entry->end - entry->start;
16403364c323SKonstantin Belousov 			entry->uip = NULL;
16413364c323SKonstantin Belousov 		}
16423364c323SKonstantin Belousov 	} else if (entry->object.vm_object != NULL &&
16433364c323SKonstantin Belousov 		   ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) &&
16443364c323SKonstantin Belousov 		   entry->uip != NULL) {
16453364c323SKonstantin Belousov 		VM_OBJECT_LOCK(entry->object.vm_object);
16463364c323SKonstantin Belousov 		KASSERT(entry->object.vm_object->uip == NULL,
16473364c323SKonstantin Belousov 		    ("OVERCOMMIT: vm_entry_clip_end: both uip e %p", entry));
16483364c323SKonstantin Belousov 		entry->object.vm_object->uip = entry->uip;
16493364c323SKonstantin Belousov 		entry->object.vm_object->charge = entry->end - entry->start;
16503364c323SKonstantin Belousov 		VM_OBJECT_UNLOCK(entry->object.vm_object);
16513364c323SKonstantin Belousov 		entry->uip = NULL;
165211cccda1SJohn Dyson 	}
165311cccda1SJohn Dyson 
165411cccda1SJohn Dyson 	/*
16550d94caffSDavid Greenman 	 * Create a new entry and insert it AFTER the specified entry
1656df8bae1dSRodney W. Grimes 	 */
1657df8bae1dSRodney W. Grimes 	new_entry = vm_map_entry_create(map);
1658df8bae1dSRodney W. Grimes 	*new_entry = *entry;
1659df8bae1dSRodney W. Grimes 
1660df8bae1dSRodney W. Grimes 	new_entry->start = entry->end = end;
1661df8bae1dSRodney W. Grimes 	new_entry->offset += (end - entry->start);
16623364c323SKonstantin Belousov 	if (new_entry->uip != NULL)
16633364c323SKonstantin Belousov 		uihold(entry->uip);
1664df8bae1dSRodney W. Grimes 
1665df8bae1dSRodney W. Grimes 	vm_map_entry_link(map, entry, new_entry);
1666df8bae1dSRodney W. Grimes 
16679fdfe602SMatthew Dillon 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1668df8bae1dSRodney W. Grimes 		vm_object_reference(new_entry->object.vm_object);
1669df8bae1dSRodney W. Grimes 	}
1670c0877f10SJohn Dyson }
1671df8bae1dSRodney W. Grimes 
1672df8bae1dSRodney W. Grimes /*
1673df8bae1dSRodney W. Grimes  *	vm_map_submap:		[ kernel use only ]
1674df8bae1dSRodney W. Grimes  *
1675df8bae1dSRodney W. Grimes  *	Mark the given range as handled by a subordinate map.
1676df8bae1dSRodney W. Grimes  *
1677df8bae1dSRodney W. Grimes  *	This range must have been created with vm_map_find,
1678df8bae1dSRodney W. Grimes  *	and no other operations may have been performed on this
1679df8bae1dSRodney W. Grimes  *	range prior to calling vm_map_submap.
1680df8bae1dSRodney W. Grimes  *
1681df8bae1dSRodney W. Grimes  *	Only a limited number of operations can be performed
1682df8bae1dSRodney W. Grimes  *	within this rage after calling vm_map_submap:
1683df8bae1dSRodney W. Grimes  *		vm_fault
1684df8bae1dSRodney W. Grimes  *	[Don't try vm_map_copy!]
1685df8bae1dSRodney W. Grimes  *
1686df8bae1dSRodney W. Grimes  *	To remove a submapping, one must first remove the
1687df8bae1dSRodney W. Grimes  *	range from the superior map, and then destroy the
1688df8bae1dSRodney W. Grimes  *	submap (if desired).  [Better yet, don't try it.]
1689df8bae1dSRodney W. Grimes  */
1690df8bae1dSRodney W. Grimes int
16911b40f8c0SMatthew Dillon vm_map_submap(
16921b40f8c0SMatthew Dillon 	vm_map_t map,
16931b40f8c0SMatthew Dillon 	vm_offset_t start,
16941b40f8c0SMatthew Dillon 	vm_offset_t end,
16951b40f8c0SMatthew Dillon 	vm_map_t submap)
1696df8bae1dSRodney W. Grimes {
1697df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
1698c0877f10SJohn Dyson 	int result = KERN_INVALID_ARGUMENT;
1699df8bae1dSRodney W. Grimes 
1700df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1701df8bae1dSRodney W. Grimes 
1702df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1703df8bae1dSRodney W. Grimes 
1704df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &entry)) {
1705df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
17060d94caffSDavid Greenman 	} else
1707df8bae1dSRodney W. Grimes 		entry = entry->next;
1708df8bae1dSRodney W. Grimes 
1709df8bae1dSRodney W. Grimes 	vm_map_clip_end(map, entry, end);
1710df8bae1dSRodney W. Grimes 
1711df8bae1dSRodney W. Grimes 	if ((entry->start == start) && (entry->end == end) &&
17129fdfe602SMatthew Dillon 	    ((entry->eflags & MAP_ENTRY_COW) == 0) &&
1713afa07f7eSJohn Dyson 	    (entry->object.vm_object == NULL)) {
17142d8acc0fSJohn Dyson 		entry->object.sub_map = submap;
1715afa07f7eSJohn Dyson 		entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
1716df8bae1dSRodney W. Grimes 		result = KERN_SUCCESS;
1717df8bae1dSRodney W. Grimes 	}
1718df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1719df8bae1dSRodney W. Grimes 
1720df8bae1dSRodney W. Grimes 	return (result);
1721df8bae1dSRodney W. Grimes }
1722df8bae1dSRodney W. Grimes 
1723df8bae1dSRodney W. Grimes /*
17241f78f902SAlan Cox  * The maximum number of pages to map
17251f78f902SAlan Cox  */
17261f78f902SAlan Cox #define	MAX_INIT_PT	96
17271f78f902SAlan Cox 
17281f78f902SAlan Cox /*
17290551c08dSAlan Cox  *	vm_map_pmap_enter:
17300551c08dSAlan Cox  *
1731cf4682aeSAlan Cox  *	Preload read-only mappings for the given object's resident pages into
1732cf4682aeSAlan Cox  *	the given map.  This eliminates the soft faults on process startup and
1733c4169725SAlan Cox  *	immediately after an mmap(2).  Because these are speculative mappings,
1734c4169725SAlan Cox  *	cached pages are not reactivated and mapped.
17350551c08dSAlan Cox  */
17360551c08dSAlan Cox void
17374da4d293SAlan Cox vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
17380551c08dSAlan Cox     vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags)
17390551c08dSAlan Cox {
17408fece8c3SAlan Cox 	vm_offset_t start;
1741ce142d9eSAlan Cox 	vm_page_t p, p_start;
17428fece8c3SAlan Cox 	vm_pindex_t psize, tmpidx;
17430551c08dSAlan Cox 
1744ba8bca61SAlan Cox 	if ((prot & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 || object == NULL)
17451f78f902SAlan Cox 		return;
17461f78f902SAlan Cox 	VM_OBJECT_LOCK(object);
174701381811SJohn Baldwin 	if (object->type == OBJT_DEVICE || object->type == OBJT_SG) {
17481f78f902SAlan Cox 		pmap_object_init_pt(map->pmap, addr, object, pindex, size);
17491f78f902SAlan Cox 		goto unlock_return;
17501f78f902SAlan Cox 	}
17511f78f902SAlan Cox 
17521f78f902SAlan Cox 	psize = atop(size);
17531f78f902SAlan Cox 
17541136ed06SAlan Cox 	if ((flags & MAP_PREFAULT_PARTIAL) && psize > MAX_INIT_PT &&
17551136ed06SAlan Cox 	    object->resident_page_count > MAX_INIT_PT)
17561f78f902SAlan Cox 		goto unlock_return;
17571f78f902SAlan Cox 
17581f78f902SAlan Cox 	if (psize + pindex > object->size) {
17591f78f902SAlan Cox 		if (object->size < pindex)
17601f78f902SAlan Cox 			goto unlock_return;
17611f78f902SAlan Cox 		psize = object->size - pindex;
17621f78f902SAlan Cox 	}
17631f78f902SAlan Cox 
1764ce142d9eSAlan Cox 	start = 0;
1765ce142d9eSAlan Cox 	p_start = NULL;
17661f78f902SAlan Cox 
1767b382c10aSKonstantin Belousov 	p = vm_page_find_least(object, pindex);
17681f78f902SAlan Cox 	/*
17691f78f902SAlan Cox 	 * Assert: the variable p is either (1) the page with the
17701f78f902SAlan Cox 	 * least pindex greater than or equal to the parameter pindex
17711f78f902SAlan Cox 	 * or (2) NULL.
17721f78f902SAlan Cox 	 */
17731f78f902SAlan Cox 	for (;
17741f78f902SAlan Cox 	     p != NULL && (tmpidx = p->pindex - pindex) < psize;
17751f78f902SAlan Cox 	     p = TAILQ_NEXT(p, listq)) {
17761f78f902SAlan Cox 		/*
17771f78f902SAlan Cox 		 * don't allow an madvise to blow away our really
17781f78f902SAlan Cox 		 * free pages allocating pv entries.
17791f78f902SAlan Cox 		 */
17801f78f902SAlan Cox 		if ((flags & MAP_PREFAULT_MADVISE) &&
17812feb50bfSAttilio Rao 		    cnt.v_free_count < cnt.v_free_reserved) {
1782379fb642SAlan Cox 			psize = tmpidx;
17831f78f902SAlan Cox 			break;
17841f78f902SAlan Cox 		}
17850a2e596aSAlan Cox 		if (p->valid == VM_PAGE_BITS_ALL) {
1786ce142d9eSAlan Cox 			if (p_start == NULL) {
1787ce142d9eSAlan Cox 				start = addr + ptoa(tmpidx);
1788ce142d9eSAlan Cox 				p_start = p;
1789ce142d9eSAlan Cox 			}
17907bfda801SAlan Cox 		} else if (p_start != NULL) {
1791cf4682aeSAlan Cox 			pmap_enter_object(map->pmap, start, addr +
1792cf4682aeSAlan Cox 			    ptoa(tmpidx), p_start, prot);
1793cf4682aeSAlan Cox 			p_start = NULL;
1794cf4682aeSAlan Cox 		}
1795cf4682aeSAlan Cox 	}
1796c46b90e9SAlan Cox 	if (p_start != NULL)
1797379fb642SAlan Cox 		pmap_enter_object(map->pmap, start, addr + ptoa(psize),
1798379fb642SAlan Cox 		    p_start, prot);
17991f78f902SAlan Cox unlock_return:
18001f78f902SAlan Cox 	VM_OBJECT_UNLOCK(object);
18010551c08dSAlan Cox }
18020551c08dSAlan Cox 
18030551c08dSAlan Cox /*
1804df8bae1dSRodney W. Grimes  *	vm_map_protect:
1805df8bae1dSRodney W. Grimes  *
1806df8bae1dSRodney W. Grimes  *	Sets the protection of the specified address
1807df8bae1dSRodney W. Grimes  *	region in the target map.  If "set_max" is
1808df8bae1dSRodney W. Grimes  *	specified, the maximum protection is to be set;
1809df8bae1dSRodney W. Grimes  *	otherwise, only the current protection is affected.
1810df8bae1dSRodney W. Grimes  */
1811df8bae1dSRodney W. Grimes int
1812b9dcd593SBruce Evans vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
1813b9dcd593SBruce Evans 	       vm_prot_t new_prot, boolean_t set_max)
1814df8bae1dSRodney W. Grimes {
1815210a6886SKonstantin Belousov 	vm_map_entry_t current, entry;
18163364c323SKonstantin Belousov 	vm_object_t obj;
18173364c323SKonstantin Belousov 	struct uidinfo *uip;
1818210a6886SKonstantin Belousov 	vm_prot_t old_prot;
1819df8bae1dSRodney W. Grimes 
1820df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1821df8bae1dSRodney W. Grimes 
1822df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1823df8bae1dSRodney W. Grimes 
1824df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &entry)) {
1825df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
1826b7b2aac2SJohn Dyson 	} else {
1827df8bae1dSRodney W. Grimes 		entry = entry->next;
1828b7b2aac2SJohn Dyson 	}
1829df8bae1dSRodney W. Grimes 
1830df8bae1dSRodney W. Grimes 	/*
18310d94caffSDavid Greenman 	 * Make a first pass to check for protection violations.
1832df8bae1dSRodney W. Grimes 	 */
1833df8bae1dSRodney W. Grimes 	current = entry;
1834df8bae1dSRodney W. Grimes 	while ((current != &map->header) && (current->start < end)) {
1835afa07f7eSJohn Dyson 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1836a1f6d91cSDavid Greenman 			vm_map_unlock(map);
1837df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ARGUMENT);
1838a1f6d91cSDavid Greenman 		}
1839df8bae1dSRodney W. Grimes 		if ((new_prot & current->max_protection) != new_prot) {
1840df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
1841df8bae1dSRodney W. Grimes 			return (KERN_PROTECTION_FAILURE);
1842df8bae1dSRodney W. Grimes 		}
1843df8bae1dSRodney W. Grimes 		current = current->next;
1844df8bae1dSRodney W. Grimes 	}
1845df8bae1dSRodney W. Grimes 
18463364c323SKonstantin Belousov 
18473364c323SKonstantin Belousov 	/*
18483364c323SKonstantin Belousov 	 * Do an accounting pass for private read-only mappings that
18493364c323SKonstantin Belousov 	 * now will do cow due to allowed write (e.g. debugger sets
18503364c323SKonstantin Belousov 	 * breakpoint on text segment)
18513364c323SKonstantin Belousov 	 */
18523364c323SKonstantin Belousov 	for (current = entry; (current != &map->header) &&
18533364c323SKonstantin Belousov 	     (current->start < end); current = current->next) {
18543364c323SKonstantin Belousov 
18553364c323SKonstantin Belousov 		vm_map_clip_end(map, current, end);
18563364c323SKonstantin Belousov 
18573364c323SKonstantin Belousov 		if (set_max ||
18583364c323SKonstantin Belousov 		    ((new_prot & ~(current->protection)) & VM_PROT_WRITE) == 0 ||
18593364c323SKonstantin Belousov 		    ENTRY_CHARGED(current)) {
18603364c323SKonstantin Belousov 			continue;
18613364c323SKonstantin Belousov 		}
18623364c323SKonstantin Belousov 
18633364c323SKonstantin Belousov 		uip = curthread->td_ucred->cr_ruidinfo;
18643364c323SKonstantin Belousov 		obj = current->object.vm_object;
18653364c323SKonstantin Belousov 
18663364c323SKonstantin Belousov 		if (obj == NULL || (current->eflags & MAP_ENTRY_NEEDS_COPY)) {
18673364c323SKonstantin Belousov 			if (!swap_reserve(current->end - current->start)) {
18683364c323SKonstantin Belousov 				vm_map_unlock(map);
18693364c323SKonstantin Belousov 				return (KERN_RESOURCE_SHORTAGE);
18703364c323SKonstantin Belousov 			}
18713364c323SKonstantin Belousov 			uihold(uip);
18723364c323SKonstantin Belousov 			current->uip = uip;
18733364c323SKonstantin Belousov 			continue;
18743364c323SKonstantin Belousov 		}
18753364c323SKonstantin Belousov 
18763364c323SKonstantin Belousov 		VM_OBJECT_LOCK(obj);
18773364c323SKonstantin Belousov 		if (obj->type != OBJT_DEFAULT && obj->type != OBJT_SWAP) {
18783364c323SKonstantin Belousov 			VM_OBJECT_UNLOCK(obj);
18793364c323SKonstantin Belousov 			continue;
18803364c323SKonstantin Belousov 		}
18813364c323SKonstantin Belousov 
18823364c323SKonstantin Belousov 		/*
18833364c323SKonstantin Belousov 		 * Charge for the whole object allocation now, since
18843364c323SKonstantin Belousov 		 * we cannot distinguish between non-charged and
18853364c323SKonstantin Belousov 		 * charged clipped mapping of the same object later.
18863364c323SKonstantin Belousov 		 */
18873364c323SKonstantin Belousov 		KASSERT(obj->charge == 0,
18883364c323SKonstantin Belousov 		    ("vm_map_protect: object %p overcharged\n", obj));
18893364c323SKonstantin Belousov 		if (!swap_reserve(ptoa(obj->size))) {
18903364c323SKonstantin Belousov 			VM_OBJECT_UNLOCK(obj);
18913364c323SKonstantin Belousov 			vm_map_unlock(map);
18923364c323SKonstantin Belousov 			return (KERN_RESOURCE_SHORTAGE);
18933364c323SKonstantin Belousov 		}
18943364c323SKonstantin Belousov 
18953364c323SKonstantin Belousov 		uihold(uip);
18963364c323SKonstantin Belousov 		obj->uip = uip;
18973364c323SKonstantin Belousov 		obj->charge = ptoa(obj->size);
18983364c323SKonstantin Belousov 		VM_OBJECT_UNLOCK(obj);
18993364c323SKonstantin Belousov 	}
19003364c323SKonstantin Belousov 
1901df8bae1dSRodney W. Grimes 	/*
19020d94caffSDavid Greenman 	 * Go back and fix up protections. [Note that clipping is not
19030d94caffSDavid Greenman 	 * necessary the second time.]
1904df8bae1dSRodney W. Grimes 	 */
1905df8bae1dSRodney W. Grimes 	current = entry;
1906df8bae1dSRodney W. Grimes 	while ((current != &map->header) && (current->start < end)) {
1907df8bae1dSRodney W. Grimes 		old_prot = current->protection;
1908210a6886SKonstantin Belousov 
1909df8bae1dSRodney W. Grimes 		if (set_max)
1910df8bae1dSRodney W. Grimes 			current->protection =
1911df8bae1dSRodney W. Grimes 			    (current->max_protection = new_prot) &
1912df8bae1dSRodney W. Grimes 			    old_prot;
1913df8bae1dSRodney W. Grimes 		else
1914df8bae1dSRodney W. Grimes 			current->protection = new_prot;
1915df8bae1dSRodney W. Grimes 
1916210a6886SKonstantin Belousov 		if ((current->eflags & (MAP_ENTRY_COW | MAP_ENTRY_USER_WIRED))
1917210a6886SKonstantin Belousov 		     == (MAP_ENTRY_COW | MAP_ENTRY_USER_WIRED) &&
1918210a6886SKonstantin Belousov 		    (current->protection & VM_PROT_WRITE) != 0 &&
1919210a6886SKonstantin Belousov 		    (old_prot & VM_PROT_WRITE) == 0) {
1920210a6886SKonstantin Belousov 			vm_fault_copy_entry(map, map, current, current, NULL);
1921210a6886SKonstantin Belousov 		}
1922210a6886SKonstantin Belousov 
1923df8bae1dSRodney W. Grimes 		/*
19242fafce9eSAlan Cox 		 * When restricting access, update the physical map.  Worry
19252fafce9eSAlan Cox 		 * about copy-on-write here.
1926df8bae1dSRodney W. Grimes 		 */
19272fafce9eSAlan Cox 		if ((old_prot & ~current->protection) != 0) {
1928afa07f7eSJohn Dyson #define MASK(entry)	(((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1929df8bae1dSRodney W. Grimes 							VM_PROT_ALL)
1930df8bae1dSRodney W. Grimes 			pmap_protect(map->pmap, current->start,
1931df8bae1dSRodney W. Grimes 			    current->end,
19321c85e3dfSAlan Cox 			    current->protection & MASK(current));
1933df8bae1dSRodney W. Grimes #undef	MASK
1934df8bae1dSRodney W. Grimes 		}
19357d78abc9SJohn Dyson 		vm_map_simplify_entry(map, current);
1936df8bae1dSRodney W. Grimes 		current = current->next;
1937df8bae1dSRodney W. Grimes 	}
1938df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1939df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
1940df8bae1dSRodney W. Grimes }
1941df8bae1dSRodney W. Grimes 
1942df8bae1dSRodney W. Grimes /*
1943867a482dSJohn Dyson  *	vm_map_madvise:
1944867a482dSJohn Dyson  *
1945867a482dSJohn Dyson  *	This routine traverses a processes map handling the madvise
1946f7fc307aSAlan Cox  *	system call.  Advisories are classified as either those effecting
1947f7fc307aSAlan Cox  *	the vm_map_entry structure, or those effecting the underlying
1948f7fc307aSAlan Cox  *	objects.
1949867a482dSJohn Dyson  */
1950b4309055SMatthew Dillon int
19511b40f8c0SMatthew Dillon vm_map_madvise(
19521b40f8c0SMatthew Dillon 	vm_map_t map,
19531b40f8c0SMatthew Dillon 	vm_offset_t start,
19541b40f8c0SMatthew Dillon 	vm_offset_t end,
19551b40f8c0SMatthew Dillon 	int behav)
1956867a482dSJohn Dyson {
1957f7fc307aSAlan Cox 	vm_map_entry_t current, entry;
1958b4309055SMatthew Dillon 	int modify_map = 0;
1959867a482dSJohn Dyson 
1960b4309055SMatthew Dillon 	/*
1961b4309055SMatthew Dillon 	 * Some madvise calls directly modify the vm_map_entry, in which case
1962b4309055SMatthew Dillon 	 * we need to use an exclusive lock on the map and we need to perform
1963b4309055SMatthew Dillon 	 * various clipping operations.  Otherwise we only need a read-lock
1964b4309055SMatthew Dillon 	 * on the map.
1965b4309055SMatthew Dillon 	 */
1966b4309055SMatthew Dillon 	switch(behav) {
1967b4309055SMatthew Dillon 	case MADV_NORMAL:
1968b4309055SMatthew Dillon 	case MADV_SEQUENTIAL:
1969b4309055SMatthew Dillon 	case MADV_RANDOM:
19704f79d873SMatthew Dillon 	case MADV_NOSYNC:
19714f79d873SMatthew Dillon 	case MADV_AUTOSYNC:
19729730a5daSPaul Saab 	case MADV_NOCORE:
19739730a5daSPaul Saab 	case MADV_CORE:
1974b4309055SMatthew Dillon 		modify_map = 1;
1975867a482dSJohn Dyson 		vm_map_lock(map);
1976b4309055SMatthew Dillon 		break;
1977b4309055SMatthew Dillon 	case MADV_WILLNEED:
1978b4309055SMatthew Dillon 	case MADV_DONTNEED:
1979b4309055SMatthew Dillon 	case MADV_FREE:
1980f7fc307aSAlan Cox 		vm_map_lock_read(map);
1981b4309055SMatthew Dillon 		break;
1982b4309055SMatthew Dillon 	default:
1983b4309055SMatthew Dillon 		return (KERN_INVALID_ARGUMENT);
1984b4309055SMatthew Dillon 	}
1985b4309055SMatthew Dillon 
1986b4309055SMatthew Dillon 	/*
1987b4309055SMatthew Dillon 	 * Locate starting entry and clip if necessary.
1988b4309055SMatthew Dillon 	 */
1989867a482dSJohn Dyson 	VM_MAP_RANGE_CHECK(map, start, end);
1990867a482dSJohn Dyson 
1991867a482dSJohn Dyson 	if (vm_map_lookup_entry(map, start, &entry)) {
1992f7fc307aSAlan Cox 		if (modify_map)
1993867a482dSJohn Dyson 			vm_map_clip_start(map, entry, start);
1994b4309055SMatthew Dillon 	} else {
1995867a482dSJohn Dyson 		entry = entry->next;
1996b4309055SMatthew Dillon 	}
1997867a482dSJohn Dyson 
1998f7fc307aSAlan Cox 	if (modify_map) {
1999f7fc307aSAlan Cox 		/*
2000f7fc307aSAlan Cox 		 * madvise behaviors that are implemented in the vm_map_entry.
2001f7fc307aSAlan Cox 		 *
2002f7fc307aSAlan Cox 		 * We clip the vm_map_entry so that behavioral changes are
2003f7fc307aSAlan Cox 		 * limited to the specified address range.
2004f7fc307aSAlan Cox 		 */
2005867a482dSJohn Dyson 		for (current = entry;
2006867a482dSJohn Dyson 		     (current != &map->header) && (current->start < end);
2007b4309055SMatthew Dillon 		     current = current->next
2008b4309055SMatthew Dillon 		) {
2009f7fc307aSAlan Cox 			if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
2010867a482dSJohn Dyson 				continue;
2011fed9a903SJohn Dyson 
201247221757SJohn Dyson 			vm_map_clip_end(map, current, end);
2013fed9a903SJohn Dyson 
2014f7fc307aSAlan Cox 			switch (behav) {
2015867a482dSJohn Dyson 			case MADV_NORMAL:
20167f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
2017867a482dSJohn Dyson 				break;
2018867a482dSJohn Dyson 			case MADV_SEQUENTIAL:
20197f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
2020867a482dSJohn Dyson 				break;
2021867a482dSJohn Dyson 			case MADV_RANDOM:
20227f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
2023867a482dSJohn Dyson 				break;
20244f79d873SMatthew Dillon 			case MADV_NOSYNC:
20254f79d873SMatthew Dillon 				current->eflags |= MAP_ENTRY_NOSYNC;
20264f79d873SMatthew Dillon 				break;
20274f79d873SMatthew Dillon 			case MADV_AUTOSYNC:
20284f79d873SMatthew Dillon 				current->eflags &= ~MAP_ENTRY_NOSYNC;
20294f79d873SMatthew Dillon 				break;
20309730a5daSPaul Saab 			case MADV_NOCORE:
20319730a5daSPaul Saab 				current->eflags |= MAP_ENTRY_NOCOREDUMP;
20329730a5daSPaul Saab 				break;
20339730a5daSPaul Saab 			case MADV_CORE:
20349730a5daSPaul Saab 				current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
20359730a5daSPaul Saab 				break;
2036867a482dSJohn Dyson 			default:
2037867a482dSJohn Dyson 				break;
2038867a482dSJohn Dyson 			}
2039f7fc307aSAlan Cox 			vm_map_simplify_entry(map, current);
2040867a482dSJohn Dyson 		}
2041867a482dSJohn Dyson 		vm_map_unlock(map);
2042b4309055SMatthew Dillon 	} else {
2043f7fc307aSAlan Cox 		vm_pindex_t pindex;
2044f7fc307aSAlan Cox 		int count;
2045f7fc307aSAlan Cox 
2046f7fc307aSAlan Cox 		/*
2047f7fc307aSAlan Cox 		 * madvise behaviors that are implemented in the underlying
2048f7fc307aSAlan Cox 		 * vm_object.
2049f7fc307aSAlan Cox 		 *
2050f7fc307aSAlan Cox 		 * Since we don't clip the vm_map_entry, we have to clip
2051f7fc307aSAlan Cox 		 * the vm_object pindex and count.
2052f7fc307aSAlan Cox 		 */
2053f7fc307aSAlan Cox 		for (current = entry;
2054f7fc307aSAlan Cox 		     (current != &map->header) && (current->start < end);
2055b4309055SMatthew Dillon 		     current = current->next
2056b4309055SMatthew Dillon 		) {
20575f99b57cSMatthew Dillon 			vm_offset_t useStart;
20585f99b57cSMatthew Dillon 
2059f7fc307aSAlan Cox 			if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
2060f7fc307aSAlan Cox 				continue;
2061f7fc307aSAlan Cox 
2062f7fc307aSAlan Cox 			pindex = OFF_TO_IDX(current->offset);
2063f7fc307aSAlan Cox 			count = atop(current->end - current->start);
20645f99b57cSMatthew Dillon 			useStart = current->start;
2065f7fc307aSAlan Cox 
2066f7fc307aSAlan Cox 			if (current->start < start) {
2067f7fc307aSAlan Cox 				pindex += atop(start - current->start);
2068f7fc307aSAlan Cox 				count -= atop(start - current->start);
20695f99b57cSMatthew Dillon 				useStart = start;
2070f7fc307aSAlan Cox 			}
2071f7fc307aSAlan Cox 			if (current->end > end)
2072f7fc307aSAlan Cox 				count -= atop(current->end - end);
2073f7fc307aSAlan Cox 
2074f7fc307aSAlan Cox 			if (count <= 0)
2075f7fc307aSAlan Cox 				continue;
2076f7fc307aSAlan Cox 
2077f7fc307aSAlan Cox 			vm_object_madvise(current->object.vm_object,
2078f7fc307aSAlan Cox 					  pindex, count, behav);
2079b4309055SMatthew Dillon 			if (behav == MADV_WILLNEED) {
20800551c08dSAlan Cox 				vm_map_pmap_enter(map,
20815f99b57cSMatthew Dillon 				    useStart,
20824da4d293SAlan Cox 				    current->protection,
2083f7fc307aSAlan Cox 				    current->object.vm_object,
2084b4309055SMatthew Dillon 				    pindex,
2085b4309055SMatthew Dillon 				    (count << PAGE_SHIFT),
2086e3026983SMatthew Dillon 				    MAP_PREFAULT_MADVISE
2087b4309055SMatthew Dillon 				);
2088f7fc307aSAlan Cox 			}
2089f7fc307aSAlan Cox 		}
2090f7fc307aSAlan Cox 		vm_map_unlock_read(map);
2091f7fc307aSAlan Cox 	}
2092b4309055SMatthew Dillon 	return (0);
2093867a482dSJohn Dyson }
2094867a482dSJohn Dyson 
2095867a482dSJohn Dyson 
2096867a482dSJohn Dyson /*
2097df8bae1dSRodney W. Grimes  *	vm_map_inherit:
2098df8bae1dSRodney W. Grimes  *
2099df8bae1dSRodney W. Grimes  *	Sets the inheritance of the specified address
2100df8bae1dSRodney W. Grimes  *	range in the target map.  Inheritance
2101df8bae1dSRodney W. Grimes  *	affects how the map will be shared with
2102e2abaaaaSAlan Cox  *	child maps at the time of vmspace_fork.
2103df8bae1dSRodney W. Grimes  */
2104df8bae1dSRodney W. Grimes int
2105b9dcd593SBruce Evans vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
2106b9dcd593SBruce Evans 	       vm_inherit_t new_inheritance)
2107df8bae1dSRodney W. Grimes {
2108c0877f10SJohn Dyson 	vm_map_entry_t entry;
2109df8bae1dSRodney W. Grimes 	vm_map_entry_t temp_entry;
2110df8bae1dSRodney W. Grimes 
2111df8bae1dSRodney W. Grimes 	switch (new_inheritance) {
2112df8bae1dSRodney W. Grimes 	case VM_INHERIT_NONE:
2113df8bae1dSRodney W. Grimes 	case VM_INHERIT_COPY:
2114df8bae1dSRodney W. Grimes 	case VM_INHERIT_SHARE:
2115df8bae1dSRodney W. Grimes 		break;
2116df8bae1dSRodney W. Grimes 	default:
2117df8bae1dSRodney W. Grimes 		return (KERN_INVALID_ARGUMENT);
2118df8bae1dSRodney W. Grimes 	}
2119df8bae1dSRodney W. Grimes 	vm_map_lock(map);
2120df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
2121df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &temp_entry)) {
2122df8bae1dSRodney W. Grimes 		entry = temp_entry;
2123df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
21240d94caffSDavid Greenman 	} else
2125df8bae1dSRodney W. Grimes 		entry = temp_entry->next;
2126df8bae1dSRodney W. Grimes 	while ((entry != &map->header) && (entry->start < end)) {
2127df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, entry, end);
2128df8bae1dSRodney W. Grimes 		entry->inheritance = new_inheritance;
212944428f62SAlan Cox 		vm_map_simplify_entry(map, entry);
2130df8bae1dSRodney W. Grimes 		entry = entry->next;
2131df8bae1dSRodney W. Grimes 	}
2132df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
2133df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
2134df8bae1dSRodney W. Grimes }
2135df8bae1dSRodney W. Grimes 
2136df8bae1dSRodney W. Grimes /*
2137acd9a301SAlan Cox  *	vm_map_unwire:
2138acd9a301SAlan Cox  *
2139e27e17b7SAlan Cox  *	Implements both kernel and user unwiring.
2140acd9a301SAlan Cox  */
2141acd9a301SAlan Cox int
2142acd9a301SAlan Cox vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
2143abd498aaSBruce M Simpson     int flags)
2144acd9a301SAlan Cox {
2145acd9a301SAlan Cox 	vm_map_entry_t entry, first_entry, tmp_entry;
2146acd9a301SAlan Cox 	vm_offset_t saved_start;
2147acd9a301SAlan Cox 	unsigned int last_timestamp;
2148acd9a301SAlan Cox 	int rv;
2149abd498aaSBruce M Simpson 	boolean_t need_wakeup, result, user_unwire;
2150acd9a301SAlan Cox 
2151abd498aaSBruce M Simpson 	user_unwire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
2152acd9a301SAlan Cox 	vm_map_lock(map);
2153acd9a301SAlan Cox 	VM_MAP_RANGE_CHECK(map, start, end);
2154acd9a301SAlan Cox 	if (!vm_map_lookup_entry(map, start, &first_entry)) {
2155abd498aaSBruce M Simpson 		if (flags & VM_MAP_WIRE_HOLESOK)
2156cbef13d8SAlan Cox 			first_entry = first_entry->next;
2157abd498aaSBruce M Simpson 		else {
2158acd9a301SAlan Cox 			vm_map_unlock(map);
2159acd9a301SAlan Cox 			return (KERN_INVALID_ADDRESS);
2160acd9a301SAlan Cox 		}
2161abd498aaSBruce M Simpson 	}
2162acd9a301SAlan Cox 	last_timestamp = map->timestamp;
2163acd9a301SAlan Cox 	entry = first_entry;
2164acd9a301SAlan Cox 	while (entry != &map->header && entry->start < end) {
2165acd9a301SAlan Cox 		if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
2166acd9a301SAlan Cox 			/*
2167acd9a301SAlan Cox 			 * We have not yet clipped the entry.
2168acd9a301SAlan Cox 			 */
2169acd9a301SAlan Cox 			saved_start = (start >= entry->start) ? start :
2170acd9a301SAlan Cox 			    entry->start;
2171acd9a301SAlan Cox 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
21728ce2d00aSPawel Jakub Dawidek 			if (vm_map_unlock_and_wait(map, 0)) {
2173acd9a301SAlan Cox 				/*
2174acd9a301SAlan Cox 				 * Allow interruption of user unwiring?
2175acd9a301SAlan Cox 				 */
2176acd9a301SAlan Cox 			}
2177acd9a301SAlan Cox 			vm_map_lock(map);
2178acd9a301SAlan Cox 			if (last_timestamp+1 != map->timestamp) {
2179acd9a301SAlan Cox 				/*
2180acd9a301SAlan Cox 				 * Look again for the entry because the map was
2181acd9a301SAlan Cox 				 * modified while it was unlocked.
2182acd9a301SAlan Cox 				 * Specifically, the entry may have been
2183acd9a301SAlan Cox 				 * clipped, merged, or deleted.
2184acd9a301SAlan Cox 				 */
2185acd9a301SAlan Cox 				if (!vm_map_lookup_entry(map, saved_start,
2186acd9a301SAlan Cox 				    &tmp_entry)) {
2187cbef13d8SAlan Cox 					if (flags & VM_MAP_WIRE_HOLESOK)
2188cbef13d8SAlan Cox 						tmp_entry = tmp_entry->next;
2189cbef13d8SAlan Cox 					else {
2190acd9a301SAlan Cox 						if (saved_start == start) {
2191acd9a301SAlan Cox 							/*
2192acd9a301SAlan Cox 							 * First_entry has been deleted.
2193acd9a301SAlan Cox 							 */
2194acd9a301SAlan Cox 							vm_map_unlock(map);
2195acd9a301SAlan Cox 							return (KERN_INVALID_ADDRESS);
2196acd9a301SAlan Cox 						}
2197acd9a301SAlan Cox 						end = saved_start;
2198acd9a301SAlan Cox 						rv = KERN_INVALID_ADDRESS;
2199acd9a301SAlan Cox 						goto done;
2200acd9a301SAlan Cox 					}
2201cbef13d8SAlan Cox 				}
2202acd9a301SAlan Cox 				if (entry == first_entry)
2203acd9a301SAlan Cox 					first_entry = tmp_entry;
2204acd9a301SAlan Cox 				else
2205acd9a301SAlan Cox 					first_entry = NULL;
2206acd9a301SAlan Cox 				entry = tmp_entry;
2207acd9a301SAlan Cox 			}
2208acd9a301SAlan Cox 			last_timestamp = map->timestamp;
2209acd9a301SAlan Cox 			continue;
2210acd9a301SAlan Cox 		}
2211acd9a301SAlan Cox 		vm_map_clip_start(map, entry, start);
2212acd9a301SAlan Cox 		vm_map_clip_end(map, entry, end);
2213acd9a301SAlan Cox 		/*
2214acd9a301SAlan Cox 		 * Mark the entry in case the map lock is released.  (See
2215acd9a301SAlan Cox 		 * above.)
2216acd9a301SAlan Cox 		 */
2217acd9a301SAlan Cox 		entry->eflags |= MAP_ENTRY_IN_TRANSITION;
2218acd9a301SAlan Cox 		/*
2219acd9a301SAlan Cox 		 * Check the map for holes in the specified region.
2220abd498aaSBruce M Simpson 		 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
2221acd9a301SAlan Cox 		 */
2222abd498aaSBruce M Simpson 		if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
2223abd498aaSBruce M Simpson 		    (entry->end < end && (entry->next == &map->header ||
2224abd498aaSBruce M Simpson 		    entry->next->start > entry->end))) {
2225acd9a301SAlan Cox 			end = entry->end;
2226acd9a301SAlan Cox 			rv = KERN_INVALID_ADDRESS;
2227acd9a301SAlan Cox 			goto done;
2228acd9a301SAlan Cox 		}
2229acd9a301SAlan Cox 		/*
22303ffbc0cdSAlan Cox 		 * If system unwiring, require that the entry is system wired.
2231acd9a301SAlan Cox 		 */
22320ada205eSBrian Feldman 		if (!user_unwire &&
22330ada205eSBrian Feldman 		    vm_map_entry_system_wired_count(entry) == 0) {
2234acd9a301SAlan Cox 			end = entry->end;
2235acd9a301SAlan Cox 			rv = KERN_INVALID_ARGUMENT;
2236acd9a301SAlan Cox 			goto done;
2237acd9a301SAlan Cox 		}
2238acd9a301SAlan Cox 		entry = entry->next;
2239acd9a301SAlan Cox 	}
2240acd9a301SAlan Cox 	rv = KERN_SUCCESS;
2241acd9a301SAlan Cox done:
2242e27e17b7SAlan Cox 	need_wakeup = FALSE;
2243acd9a301SAlan Cox 	if (first_entry == NULL) {
2244acd9a301SAlan Cox 		result = vm_map_lookup_entry(map, start, &first_entry);
2245cbef13d8SAlan Cox 		if (!result && (flags & VM_MAP_WIRE_HOLESOK))
2246cbef13d8SAlan Cox 			first_entry = first_entry->next;
2247cbef13d8SAlan Cox 		else
2248acd9a301SAlan Cox 			KASSERT(result, ("vm_map_unwire: lookup failed"));
2249acd9a301SAlan Cox 	}
2250acd9a301SAlan Cox 	entry = first_entry;
2251acd9a301SAlan Cox 	while (entry != &map->header && entry->start < end) {
22523ffbc0cdSAlan Cox 		if (rv == KERN_SUCCESS && (!user_unwire ||
22533ffbc0cdSAlan Cox 		    (entry->eflags & MAP_ENTRY_USER_WIRED))) {
2254b2f3846aSAlan Cox 			if (user_unwire)
2255b2f3846aSAlan Cox 				entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2256b2f3846aSAlan Cox 			entry->wired_count--;
22570ada205eSBrian Feldman 			if (entry->wired_count == 0) {
2258b2f3846aSAlan Cox 				/*
2259b2f3846aSAlan Cox 				 * Retain the map lock.
2260b2f3846aSAlan Cox 				 */
22614be14af9SAlan Cox 				vm_fault_unwire(map, entry->start, entry->end,
22624be14af9SAlan Cox 				    entry->object.vm_object != NULL &&
226301381811SJohn Baldwin 				    (entry->object.vm_object->type == OBJT_DEVICE ||
226401381811SJohn Baldwin 				    entry->object.vm_object->type == OBJT_SG));
2265b2f3846aSAlan Cox 			}
2266b2f3846aSAlan Cox 		}
2267acd9a301SAlan Cox 		KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
2268acd9a301SAlan Cox 			("vm_map_unwire: in-transition flag missing"));
2269acd9a301SAlan Cox 		entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
2270acd9a301SAlan Cox 		if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
2271acd9a301SAlan Cox 			entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
2272acd9a301SAlan Cox 			need_wakeup = TRUE;
2273acd9a301SAlan Cox 		}
2274acd9a301SAlan Cox 		vm_map_simplify_entry(map, entry);
2275acd9a301SAlan Cox 		entry = entry->next;
2276acd9a301SAlan Cox 	}
2277acd9a301SAlan Cox 	vm_map_unlock(map);
2278acd9a301SAlan Cox 	if (need_wakeup)
2279acd9a301SAlan Cox 		vm_map_wakeup(map);
2280acd9a301SAlan Cox 	return (rv);
2281acd9a301SAlan Cox }
2282acd9a301SAlan Cox 
2283acd9a301SAlan Cox /*
2284e27e17b7SAlan Cox  *	vm_map_wire:
2285e27e17b7SAlan Cox  *
2286e27e17b7SAlan Cox  *	Implements both kernel and user wiring.
2287e27e17b7SAlan Cox  */
2288e27e17b7SAlan Cox int
2289e27e17b7SAlan Cox vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
2290abd498aaSBruce M Simpson     int flags)
2291e27e17b7SAlan Cox {
229212d7cc84SAlan Cox 	vm_map_entry_t entry, first_entry, tmp_entry;
229312d7cc84SAlan Cox 	vm_offset_t saved_end, saved_start;
229412d7cc84SAlan Cox 	unsigned int last_timestamp;
229512d7cc84SAlan Cox 	int rv;
22964be14af9SAlan Cox 	boolean_t fictitious, need_wakeup, result, user_wire;
2297e27e17b7SAlan Cox 
2298abd498aaSBruce M Simpson 	user_wire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
229912d7cc84SAlan Cox 	vm_map_lock(map);
230012d7cc84SAlan Cox 	VM_MAP_RANGE_CHECK(map, start, end);
230112d7cc84SAlan Cox 	if (!vm_map_lookup_entry(map, start, &first_entry)) {
2302abd498aaSBruce M Simpson 		if (flags & VM_MAP_WIRE_HOLESOK)
2303cbef13d8SAlan Cox 			first_entry = first_entry->next;
2304abd498aaSBruce M Simpson 		else {
230512d7cc84SAlan Cox 			vm_map_unlock(map);
230612d7cc84SAlan Cox 			return (KERN_INVALID_ADDRESS);
230712d7cc84SAlan Cox 		}
2308abd498aaSBruce M Simpson 	}
230912d7cc84SAlan Cox 	last_timestamp = map->timestamp;
231012d7cc84SAlan Cox 	entry = first_entry;
231112d7cc84SAlan Cox 	while (entry != &map->header && entry->start < end) {
231212d7cc84SAlan Cox 		if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
231312d7cc84SAlan Cox 			/*
231412d7cc84SAlan Cox 			 * We have not yet clipped the entry.
231512d7cc84SAlan Cox 			 */
231612d7cc84SAlan Cox 			saved_start = (start >= entry->start) ? start :
231712d7cc84SAlan Cox 			    entry->start;
231812d7cc84SAlan Cox 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
23198ce2d00aSPawel Jakub Dawidek 			if (vm_map_unlock_and_wait(map, 0)) {
232012d7cc84SAlan Cox 				/*
232112d7cc84SAlan Cox 				 * Allow interruption of user wiring?
232212d7cc84SAlan Cox 				 */
232312d7cc84SAlan Cox 			}
232412d7cc84SAlan Cox 			vm_map_lock(map);
232512d7cc84SAlan Cox 			if (last_timestamp + 1 != map->timestamp) {
232612d7cc84SAlan Cox 				/*
232712d7cc84SAlan Cox 				 * Look again for the entry because the map was
232812d7cc84SAlan Cox 				 * modified while it was unlocked.
232912d7cc84SAlan Cox 				 * Specifically, the entry may have been
233012d7cc84SAlan Cox 				 * clipped, merged, or deleted.
233112d7cc84SAlan Cox 				 */
233212d7cc84SAlan Cox 				if (!vm_map_lookup_entry(map, saved_start,
233312d7cc84SAlan Cox 				    &tmp_entry)) {
2334cbef13d8SAlan Cox 					if (flags & VM_MAP_WIRE_HOLESOK)
2335cbef13d8SAlan Cox 						tmp_entry = tmp_entry->next;
2336cbef13d8SAlan Cox 					else {
233712d7cc84SAlan Cox 						if (saved_start == start) {
233812d7cc84SAlan Cox 							/*
233912d7cc84SAlan Cox 							 * first_entry has been deleted.
234012d7cc84SAlan Cox 							 */
234112d7cc84SAlan Cox 							vm_map_unlock(map);
234212d7cc84SAlan Cox 							return (KERN_INVALID_ADDRESS);
234312d7cc84SAlan Cox 						}
234412d7cc84SAlan Cox 						end = saved_start;
234512d7cc84SAlan Cox 						rv = KERN_INVALID_ADDRESS;
234612d7cc84SAlan Cox 						goto done;
234712d7cc84SAlan Cox 					}
2348cbef13d8SAlan Cox 				}
234912d7cc84SAlan Cox 				if (entry == first_entry)
235012d7cc84SAlan Cox 					first_entry = tmp_entry;
235112d7cc84SAlan Cox 				else
235212d7cc84SAlan Cox 					first_entry = NULL;
235312d7cc84SAlan Cox 				entry = tmp_entry;
235412d7cc84SAlan Cox 			}
235512d7cc84SAlan Cox 			last_timestamp = map->timestamp;
235612d7cc84SAlan Cox 			continue;
235712d7cc84SAlan Cox 		}
235812d7cc84SAlan Cox 		vm_map_clip_start(map, entry, start);
235912d7cc84SAlan Cox 		vm_map_clip_end(map, entry, end);
236012d7cc84SAlan Cox 		/*
236112d7cc84SAlan Cox 		 * Mark the entry in case the map lock is released.  (See
236212d7cc84SAlan Cox 		 * above.)
236312d7cc84SAlan Cox 		 */
236412d7cc84SAlan Cox 		entry->eflags |= MAP_ENTRY_IN_TRANSITION;
236512d7cc84SAlan Cox 		/*
23660ada205eSBrian Feldman 		 *
236712d7cc84SAlan Cox 		 */
23680ada205eSBrian Feldman 		if (entry->wired_count == 0) {
23696d7e8091SKonstantin Belousov 			if ((entry->protection & (VM_PROT_READ|VM_PROT_EXECUTE))
23706d7e8091SKonstantin Belousov 			    == 0) {
2371529ab57bSKonstantin Belousov 				entry->eflags |= MAP_ENTRY_WIRE_SKIPPED;
23726d7e8091SKonstantin Belousov 				if ((flags & VM_MAP_WIRE_HOLESOK) == 0) {
23736d7e8091SKonstantin Belousov 					end = entry->end;
23746d7e8091SKonstantin Belousov 					rv = KERN_INVALID_ADDRESS;
23756d7e8091SKonstantin Belousov 					goto done;
23766d7e8091SKonstantin Belousov 				}
23776d7e8091SKonstantin Belousov 				goto next_entry;
23786d7e8091SKonstantin Belousov 			}
23790ada205eSBrian Feldman 			entry->wired_count++;
238012d7cc84SAlan Cox 			saved_start = entry->start;
238112d7cc84SAlan Cox 			saved_end = entry->end;
23824be14af9SAlan Cox 			fictitious = entry->object.vm_object != NULL &&
238301381811SJohn Baldwin 			    (entry->object.vm_object->type == OBJT_DEVICE ||
238401381811SJohn Baldwin 			    entry->object.vm_object->type == OBJT_SG);
238512d7cc84SAlan Cox 			/*
238612d7cc84SAlan Cox 			 * Release the map lock, relying on the in-transition
238712d7cc84SAlan Cox 			 * mark.
238812d7cc84SAlan Cox 			 */
238912d7cc84SAlan Cox 			vm_map_unlock(map);
2390ef594d31SAlan Cox 			rv = vm_fault_wire(map, saved_start, saved_end,
23912db65ab4SAlan Cox 			    fictitious);
239212d7cc84SAlan Cox 			vm_map_lock(map);
239312d7cc84SAlan Cox 			if (last_timestamp + 1 != map->timestamp) {
239412d7cc84SAlan Cox 				/*
239512d7cc84SAlan Cox 				 * Look again for the entry because the map was
239612d7cc84SAlan Cox 				 * modified while it was unlocked.  The entry
239712d7cc84SAlan Cox 				 * may have been clipped, but NOT merged or
239812d7cc84SAlan Cox 				 * deleted.
239912d7cc84SAlan Cox 				 */
240012d7cc84SAlan Cox 				result = vm_map_lookup_entry(map, saved_start,
240112d7cc84SAlan Cox 				    &tmp_entry);
240212d7cc84SAlan Cox 				KASSERT(result, ("vm_map_wire: lookup failed"));
240312d7cc84SAlan Cox 				if (entry == first_entry)
240412d7cc84SAlan Cox 					first_entry = tmp_entry;
240512d7cc84SAlan Cox 				else
240612d7cc84SAlan Cox 					first_entry = NULL;
240712d7cc84SAlan Cox 				entry = tmp_entry;
240828c58286SAlan Cox 				while (entry->end < saved_end) {
240928c58286SAlan Cox 					if (rv != KERN_SUCCESS) {
241028c58286SAlan Cox 						KASSERT(entry->wired_count == 1,
241128c58286SAlan Cox 						    ("vm_map_wire: bad count"));
241228c58286SAlan Cox 						entry->wired_count = -1;
241328c58286SAlan Cox 					}
241412d7cc84SAlan Cox 					entry = entry->next;
241512d7cc84SAlan Cox 				}
241628c58286SAlan Cox 			}
241712d7cc84SAlan Cox 			last_timestamp = map->timestamp;
241812d7cc84SAlan Cox 			if (rv != KERN_SUCCESS) {
241928c58286SAlan Cox 				KASSERT(entry->wired_count == 1,
242028c58286SAlan Cox 				    ("vm_map_wire: bad count"));
242112d7cc84SAlan Cox 				/*
242228c58286SAlan Cox 				 * Assign an out-of-range value to represent
242328c58286SAlan Cox 				 * the failure to wire this entry.
242412d7cc84SAlan Cox 				 */
242528c58286SAlan Cox 				entry->wired_count = -1;
242612d7cc84SAlan Cox 				end = entry->end;
242712d7cc84SAlan Cox 				goto done;
242812d7cc84SAlan Cox 			}
24290ada205eSBrian Feldman 		} else if (!user_wire ||
24300ada205eSBrian Feldman 			   (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
24310ada205eSBrian Feldman 			entry->wired_count++;
243212d7cc84SAlan Cox 		}
243312d7cc84SAlan Cox 		/*
243412d7cc84SAlan Cox 		 * Check the map for holes in the specified region.
2435abd498aaSBruce M Simpson 		 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
243612d7cc84SAlan Cox 		 */
24376d7e8091SKonstantin Belousov 	next_entry:
2438abd498aaSBruce M Simpson 		if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
2439abd498aaSBruce M Simpson 		    (entry->end < end && (entry->next == &map->header ||
2440abd498aaSBruce M Simpson 		    entry->next->start > entry->end))) {
244112d7cc84SAlan Cox 			end = entry->end;
244212d7cc84SAlan Cox 			rv = KERN_INVALID_ADDRESS;
244312d7cc84SAlan Cox 			goto done;
244412d7cc84SAlan Cox 		}
244512d7cc84SAlan Cox 		entry = entry->next;
244612d7cc84SAlan Cox 	}
244712d7cc84SAlan Cox 	rv = KERN_SUCCESS;
244812d7cc84SAlan Cox done:
244912d7cc84SAlan Cox 	need_wakeup = FALSE;
245012d7cc84SAlan Cox 	if (first_entry == NULL) {
245112d7cc84SAlan Cox 		result = vm_map_lookup_entry(map, start, &first_entry);
2452cbef13d8SAlan Cox 		if (!result && (flags & VM_MAP_WIRE_HOLESOK))
2453cbef13d8SAlan Cox 			first_entry = first_entry->next;
2454cbef13d8SAlan Cox 		else
245512d7cc84SAlan Cox 			KASSERT(result, ("vm_map_wire: lookup failed"));
245612d7cc84SAlan Cox 	}
245712d7cc84SAlan Cox 	entry = first_entry;
245812d7cc84SAlan Cox 	while (entry != &map->header && entry->start < end) {
24596d7e8091SKonstantin Belousov 		if ((entry->eflags & MAP_ENTRY_WIRE_SKIPPED) != 0)
24606d7e8091SKonstantin Belousov 			goto next_entry_done;
246112d7cc84SAlan Cox 		if (rv == KERN_SUCCESS) {
246212d7cc84SAlan Cox 			if (user_wire)
246312d7cc84SAlan Cox 				entry->eflags |= MAP_ENTRY_USER_WIRED;
246428c58286SAlan Cox 		} else if (entry->wired_count == -1) {
246528c58286SAlan Cox 			/*
246628c58286SAlan Cox 			 * Wiring failed on this entry.  Thus, unwiring is
246728c58286SAlan Cox 			 * unnecessary.
246828c58286SAlan Cox 			 */
246928c58286SAlan Cox 			entry->wired_count = 0;
247012d7cc84SAlan Cox 		} else {
24710ada205eSBrian Feldman 			if (!user_wire ||
24720ada205eSBrian Feldman 			    (entry->eflags & MAP_ENTRY_USER_WIRED) == 0)
247312d7cc84SAlan Cox 				entry->wired_count--;
24740ada205eSBrian Feldman 			if (entry->wired_count == 0) {
247512d7cc84SAlan Cox 				/*
247612d7cc84SAlan Cox 				 * Retain the map lock.
247712d7cc84SAlan Cox 				 */
24784be14af9SAlan Cox 				vm_fault_unwire(map, entry->start, entry->end,
24794be14af9SAlan Cox 				    entry->object.vm_object != NULL &&
248001381811SJohn Baldwin 				    (entry->object.vm_object->type == OBJT_DEVICE ||
248101381811SJohn Baldwin 				    entry->object.vm_object->type == OBJT_SG));
248212d7cc84SAlan Cox 			}
248312d7cc84SAlan Cox 		}
24846d7e8091SKonstantin Belousov 	next_entry_done:
248512d7cc84SAlan Cox 		KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
248612d7cc84SAlan Cox 			("vm_map_wire: in-transition flag missing"));
24876d7e8091SKonstantin Belousov 		entry->eflags &= ~(MAP_ENTRY_IN_TRANSITION|MAP_ENTRY_WIRE_SKIPPED);
248812d7cc84SAlan Cox 		if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
248912d7cc84SAlan Cox 			entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
249012d7cc84SAlan Cox 			need_wakeup = TRUE;
249112d7cc84SAlan Cox 		}
249212d7cc84SAlan Cox 		vm_map_simplify_entry(map, entry);
249312d7cc84SAlan Cox 		entry = entry->next;
249412d7cc84SAlan Cox 	}
249512d7cc84SAlan Cox 	vm_map_unlock(map);
249612d7cc84SAlan Cox 	if (need_wakeup)
249712d7cc84SAlan Cox 		vm_map_wakeup(map);
249812d7cc84SAlan Cox 	return (rv);
2499e27e17b7SAlan Cox }
2500e27e17b7SAlan Cox 
2501e27e17b7SAlan Cox /*
2502950f8459SAlan Cox  * vm_map_sync
2503df8bae1dSRodney W. Grimes  *
2504df8bae1dSRodney W. Grimes  * Push any dirty cached pages in the address range to their pager.
2505df8bae1dSRodney W. Grimes  * If syncio is TRUE, dirty pages are written synchronously.
2506df8bae1dSRodney W. Grimes  * If invalidate is TRUE, any cached pages are freed as well.
2507df8bae1dSRodney W. Grimes  *
2508637315edSAlan Cox  * If the size of the region from start to end is zero, we are
2509637315edSAlan Cox  * supposed to flush all modified pages within the region containing
2510637315edSAlan Cox  * start.  Unfortunately, a region can be split or coalesced with
2511637315edSAlan Cox  * neighboring regions, making it difficult to determine what the
2512637315edSAlan Cox  * original region was.  Therefore, we approximate this requirement by
2513637315edSAlan Cox  * flushing the current region containing start.
2514637315edSAlan Cox  *
2515df8bae1dSRodney W. Grimes  * Returns an error if any part of the specified range is not mapped.
2516df8bae1dSRodney W. Grimes  */
2517df8bae1dSRodney W. Grimes int
2518950f8459SAlan Cox vm_map_sync(
25191b40f8c0SMatthew Dillon 	vm_map_t map,
25201b40f8c0SMatthew Dillon 	vm_offset_t start,
25211b40f8c0SMatthew Dillon 	vm_offset_t end,
25221b40f8c0SMatthew Dillon 	boolean_t syncio,
25231b40f8c0SMatthew Dillon 	boolean_t invalidate)
2524df8bae1dSRodney W. Grimes {
2525c0877f10SJohn Dyson 	vm_map_entry_t current;
2526df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
2527df8bae1dSRodney W. Grimes 	vm_size_t size;
2528df8bae1dSRodney W. Grimes 	vm_object_t object;
2529a316d390SJohn Dyson 	vm_ooffset_t offset;
2530e53fa61bSKonstantin Belousov 	unsigned int last_timestamp;
2531df8bae1dSRodney W. Grimes 
2532df8bae1dSRodney W. Grimes 	vm_map_lock_read(map);
2533df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
2534df8bae1dSRodney W. Grimes 	if (!vm_map_lookup_entry(map, start, &entry)) {
2535df8bae1dSRodney W. Grimes 		vm_map_unlock_read(map);
2536df8bae1dSRodney W. Grimes 		return (KERN_INVALID_ADDRESS);
2537637315edSAlan Cox 	} else if (start == end) {
2538637315edSAlan Cox 		start = entry->start;
2539637315edSAlan Cox 		end = entry->end;
2540df8bae1dSRodney W. Grimes 	}
2541df8bae1dSRodney W. Grimes 	/*
2542b7b7cd44SAlan Cox 	 * Make a first pass to check for user-wired memory and holes.
2543df8bae1dSRodney W. Grimes 	 */
25447b0e72d1SAlan Cox 	for (current = entry; current != &map->header && current->start < end;
25457b0e72d1SAlan Cox 	    current = current->next) {
2546b7b7cd44SAlan Cox 		if (invalidate && (current->eflags & MAP_ENTRY_USER_WIRED)) {
2547df8bae1dSRodney W. Grimes 			vm_map_unlock_read(map);
2548df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ARGUMENT);
2549df8bae1dSRodney W. Grimes 		}
2550df8bae1dSRodney W. Grimes 		if (end > current->end &&
2551df8bae1dSRodney W. Grimes 		    (current->next == &map->header ||
2552df8bae1dSRodney W. Grimes 			current->end != current->next->start)) {
2553df8bae1dSRodney W. Grimes 			vm_map_unlock_read(map);
2554df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ADDRESS);
2555df8bae1dSRodney W. Grimes 		}
2556df8bae1dSRodney W. Grimes 	}
2557df8bae1dSRodney W. Grimes 
25582cf13952SAlan Cox 	if (invalidate)
2559bc105a67SAlan Cox 		pmap_remove(map->pmap, start, end);
25602cf13952SAlan Cox 
2561df8bae1dSRodney W. Grimes 	/*
2562df8bae1dSRodney W. Grimes 	 * Make a second pass, cleaning/uncaching pages from the indicated
2563df8bae1dSRodney W. Grimes 	 * objects as we go.
2564df8bae1dSRodney W. Grimes 	 */
2565e53fa61bSKonstantin Belousov 	for (current = entry; current != &map->header && current->start < end;) {
2566df8bae1dSRodney W. Grimes 		offset = current->offset + (start - current->start);
2567df8bae1dSRodney W. Grimes 		size = (end <= current->end ? end : current->end) - start;
25689fdfe602SMatthew Dillon 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
2569c0877f10SJohn Dyson 			vm_map_t smap;
2570df8bae1dSRodney W. Grimes 			vm_map_entry_t tentry;
2571df8bae1dSRodney W. Grimes 			vm_size_t tsize;
2572df8bae1dSRodney W. Grimes 
25739fdfe602SMatthew Dillon 			smap = current->object.sub_map;
2574df8bae1dSRodney W. Grimes 			vm_map_lock_read(smap);
2575df8bae1dSRodney W. Grimes 			(void) vm_map_lookup_entry(smap, offset, &tentry);
2576df8bae1dSRodney W. Grimes 			tsize = tentry->end - offset;
2577df8bae1dSRodney W. Grimes 			if (tsize < size)
2578df8bae1dSRodney W. Grimes 				size = tsize;
2579df8bae1dSRodney W. Grimes 			object = tentry->object.vm_object;
2580df8bae1dSRodney W. Grimes 			offset = tentry->offset + (offset - tentry->start);
2581df8bae1dSRodney W. Grimes 			vm_map_unlock_read(smap);
2582df8bae1dSRodney W. Grimes 		} else {
2583df8bae1dSRodney W. Grimes 			object = current->object.vm_object;
2584df8bae1dSRodney W. Grimes 		}
2585e53fa61bSKonstantin Belousov 		vm_object_reference(object);
2586e53fa61bSKonstantin Belousov 		last_timestamp = map->timestamp;
2587e53fa61bSKonstantin Belousov 		vm_map_unlock_read(map);
2588950f8459SAlan Cox 		vm_object_sync(object, offset, size, syncio, invalidate);
2589df8bae1dSRodney W. Grimes 		start += size;
2590e53fa61bSKonstantin Belousov 		vm_object_deallocate(object);
2591e53fa61bSKonstantin Belousov 		vm_map_lock_read(map);
2592e53fa61bSKonstantin Belousov 		if (last_timestamp == map->timestamp ||
2593e53fa61bSKonstantin Belousov 		    !vm_map_lookup_entry(map, start, &current))
2594e53fa61bSKonstantin Belousov 			current = current->next;
2595df8bae1dSRodney W. Grimes 	}
2596df8bae1dSRodney W. Grimes 
2597df8bae1dSRodney W. Grimes 	vm_map_unlock_read(map);
2598df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
2599df8bae1dSRodney W. Grimes }
2600df8bae1dSRodney W. Grimes 
2601df8bae1dSRodney W. Grimes /*
2602df8bae1dSRodney W. Grimes  *	vm_map_entry_unwire:	[ internal use only ]
2603df8bae1dSRodney W. Grimes  *
2604df8bae1dSRodney W. Grimes  *	Make the region specified by this entry pageable.
2605df8bae1dSRodney W. Grimes  *
2606df8bae1dSRodney W. Grimes  *	The map in question should be locked.
2607df8bae1dSRodney W. Grimes  *	[This is the reason for this routine's existence.]
2608df8bae1dSRodney W. Grimes  */
26090362d7d7SJohn Dyson static void
26101b40f8c0SMatthew Dillon vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
2611df8bae1dSRodney W. Grimes {
26124be14af9SAlan Cox 	vm_fault_unwire(map, entry->start, entry->end,
26134be14af9SAlan Cox 	    entry->object.vm_object != NULL &&
261401381811SJohn Baldwin 	    (entry->object.vm_object->type == OBJT_DEVICE ||
261501381811SJohn Baldwin 	    entry->object.vm_object->type == OBJT_SG));
2616df8bae1dSRodney W. Grimes 	entry->wired_count = 0;
2617df8bae1dSRodney W. Grimes }
2618df8bae1dSRodney W. Grimes 
26190b367bd8SKonstantin Belousov static void
26200b367bd8SKonstantin Belousov vm_map_entry_deallocate(vm_map_entry_t entry, boolean_t system_map)
26210b367bd8SKonstantin Belousov {
26220b367bd8SKonstantin Belousov 
26230b367bd8SKonstantin Belousov 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0)
26240b367bd8SKonstantin Belousov 		vm_object_deallocate(entry->object.vm_object);
26250b367bd8SKonstantin Belousov 	uma_zfree(system_map ? kmapentzone : mapentzone, entry);
26260b367bd8SKonstantin Belousov }
26270b367bd8SKonstantin Belousov 
2628df8bae1dSRodney W. Grimes /*
2629df8bae1dSRodney W. Grimes  *	vm_map_entry_delete:	[ internal use only ]
2630df8bae1dSRodney W. Grimes  *
2631df8bae1dSRodney W. Grimes  *	Deallocate the given entry from the target map.
2632df8bae1dSRodney W. Grimes  */
26330362d7d7SJohn Dyson static void
26341b40f8c0SMatthew Dillon vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry)
2635df8bae1dSRodney W. Grimes {
263632a89c32SAlan Cox 	vm_object_t object;
26373364c323SKonstantin Belousov 	vm_pindex_t offidxstart, offidxend, count, size1;
26383364c323SKonstantin Belousov 	vm_ooffset_t size;
263932a89c32SAlan Cox 
2640df8bae1dSRodney W. Grimes 	vm_map_entry_unlink(map, entry);
26413364c323SKonstantin Belousov 	object = entry->object.vm_object;
26423364c323SKonstantin Belousov 	size = entry->end - entry->start;
26433364c323SKonstantin Belousov 	map->size -= size;
26443364c323SKonstantin Belousov 
26453364c323SKonstantin Belousov 	if (entry->uip != NULL) {
26463364c323SKonstantin Belousov 		swap_release_by_uid(size, entry->uip);
26473364c323SKonstantin Belousov 		uifree(entry->uip);
26483364c323SKonstantin Belousov 	}
2649df8bae1dSRodney W. Grimes 
265032a89c32SAlan Cox 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
26513364c323SKonstantin Belousov 	    (object != NULL)) {
26523364c323SKonstantin Belousov 		KASSERT(entry->uip == NULL || object->uip == NULL ||
26533364c323SKonstantin Belousov 		    (entry->eflags & MAP_ENTRY_NEEDS_COPY),
26543364c323SKonstantin Belousov 		    ("OVERCOMMIT vm_map_entry_delete: both uip %p", entry));
26553364c323SKonstantin Belousov 		count = OFF_TO_IDX(size);
265632a89c32SAlan Cox 		offidxstart = OFF_TO_IDX(entry->offset);
265732a89c32SAlan Cox 		offidxend = offidxstart + count;
265832a89c32SAlan Cox 		VM_OBJECT_LOCK(object);
265932a89c32SAlan Cox 		if (object->ref_count != 1 &&
266032a89c32SAlan Cox 		    ((object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING ||
26619f5c801bSAlan Cox 		    object == kernel_object || object == kmem_object)) {
266232a89c32SAlan Cox 			vm_object_collapse(object);
2663b103b948SDag-Erling Smørgrav 			vm_object_page_remove(object, offidxstart, offidxend, FALSE);
266432a89c32SAlan Cox 			if (object->type == OBJT_SWAP)
266532a89c32SAlan Cox 				swap_pager_freespace(object, offidxstart, count);
266632a89c32SAlan Cox 			if (offidxend >= object->size &&
26673364c323SKonstantin Belousov 			    offidxstart < object->size) {
26683364c323SKonstantin Belousov 				size1 = object->size;
266932a89c32SAlan Cox 				object->size = offidxstart;
26703364c323SKonstantin Belousov 				if (object->uip != NULL) {
26713364c323SKonstantin Belousov 					size1 -= object->size;
26723364c323SKonstantin Belousov 					KASSERT(object->charge >= ptoa(size1),
26733364c323SKonstantin Belousov 					    ("vm_map_entry_delete: object->charge < 0"));
26743364c323SKonstantin Belousov 					swap_release_by_uid(ptoa(size1), object->uip);
26753364c323SKonstantin Belousov 					object->charge -= ptoa(size1);
26763364c323SKonstantin Belousov 				}
26773364c323SKonstantin Belousov 			}
267832a89c32SAlan Cox 		}
267932a89c32SAlan Cox 		VM_OBJECT_UNLOCK(object);
2680897d81a0SKonstantin Belousov 	} else
2681897d81a0SKonstantin Belousov 		entry->object.vm_object = NULL;
26820b367bd8SKonstantin Belousov 	if (map->system_map)
26830b367bd8SKonstantin Belousov 		vm_map_entry_deallocate(entry, TRUE);
26840b367bd8SKonstantin Belousov 	else {
26850b367bd8SKonstantin Belousov 		entry->next = curthread->td_map_def_user;
26860b367bd8SKonstantin Belousov 		curthread->td_map_def_user = entry;
26870b367bd8SKonstantin Belousov 	}
2688df8bae1dSRodney W. Grimes }
2689df8bae1dSRodney W. Grimes 
2690df8bae1dSRodney W. Grimes /*
2691df8bae1dSRodney W. Grimes  *	vm_map_delete:	[ internal use only ]
2692df8bae1dSRodney W. Grimes  *
2693df8bae1dSRodney W. Grimes  *	Deallocates the given address range from the target
2694df8bae1dSRodney W. Grimes  *	map.
2695df8bae1dSRodney W. Grimes  */
2696df8bae1dSRodney W. Grimes int
2697655c3490SKonstantin Belousov vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end)
2698df8bae1dSRodney W. Grimes {
2699c0877f10SJohn Dyson 	vm_map_entry_t entry;
2700df8bae1dSRodney W. Grimes 	vm_map_entry_t first_entry;
2701df8bae1dSRodney W. Grimes 
27023a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
27033a0916b8SKonstantin Belousov 
2704df8bae1dSRodney W. Grimes 	/*
2705df8bae1dSRodney W. Grimes 	 * Find the start of the region, and clip it
2706df8bae1dSRodney W. Grimes 	 */
2707876318ecSAlan Cox 	if (!vm_map_lookup_entry(map, start, &first_entry))
2708df8bae1dSRodney W. Grimes 		entry = first_entry->next;
2709876318ecSAlan Cox 	else {
2710df8bae1dSRodney W. Grimes 		entry = first_entry;
2711df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
2712df8bae1dSRodney W. Grimes 	}
2713df8bae1dSRodney W. Grimes 
2714df8bae1dSRodney W. Grimes 	/*
2715df8bae1dSRodney W. Grimes 	 * Step through all entries in this region
2716df8bae1dSRodney W. Grimes 	 */
2717df8bae1dSRodney W. Grimes 	while ((entry != &map->header) && (entry->start < end)) {
2718df8bae1dSRodney W. Grimes 		vm_map_entry_t next;
2719df8bae1dSRodney W. Grimes 
272073b2baceSAlan Cox 		/*
272173b2baceSAlan Cox 		 * Wait for wiring or unwiring of an entry to complete.
27227c938963SBrian Feldman 		 * Also wait for any system wirings to disappear on
27237c938963SBrian Feldman 		 * user maps.
272473b2baceSAlan Cox 		 */
27257c938963SBrian Feldman 		if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 ||
27267c938963SBrian Feldman 		    (vm_map_pmap(map) != kernel_pmap &&
27277c938963SBrian Feldman 		    vm_map_entry_system_wired_count(entry) != 0)) {
272873b2baceSAlan Cox 			unsigned int last_timestamp;
272973b2baceSAlan Cox 			vm_offset_t saved_start;
273073b2baceSAlan Cox 			vm_map_entry_t tmp_entry;
273173b2baceSAlan Cox 
273273b2baceSAlan Cox 			saved_start = entry->start;
273373b2baceSAlan Cox 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
273473b2baceSAlan Cox 			last_timestamp = map->timestamp;
27358ce2d00aSPawel Jakub Dawidek 			(void) vm_map_unlock_and_wait(map, 0);
273673b2baceSAlan Cox 			vm_map_lock(map);
273773b2baceSAlan Cox 			if (last_timestamp + 1 != map->timestamp) {
273873b2baceSAlan Cox 				/*
273973b2baceSAlan Cox 				 * Look again for the entry because the map was
274073b2baceSAlan Cox 				 * modified while it was unlocked.
274173b2baceSAlan Cox 				 * Specifically, the entry may have been
274273b2baceSAlan Cox 				 * clipped, merged, or deleted.
274373b2baceSAlan Cox 				 */
274473b2baceSAlan Cox 				if (!vm_map_lookup_entry(map, saved_start,
274573b2baceSAlan Cox 							 &tmp_entry))
274673b2baceSAlan Cox 					entry = tmp_entry->next;
274773b2baceSAlan Cox 				else {
274873b2baceSAlan Cox 					entry = tmp_entry;
274973b2baceSAlan Cox 					vm_map_clip_start(map, entry,
275073b2baceSAlan Cox 							  saved_start);
275173b2baceSAlan Cox 				}
275273b2baceSAlan Cox 			}
275373b2baceSAlan Cox 			continue;
275473b2baceSAlan Cox 		}
2755df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, entry, end);
2756df8bae1dSRodney W. Grimes 
2757c0877f10SJohn Dyson 		next = entry->next;
2758df8bae1dSRodney W. Grimes 
2759df8bae1dSRodney W. Grimes 		/*
27600d94caffSDavid Greenman 		 * Unwire before removing addresses from the pmap; otherwise,
27610d94caffSDavid Greenman 		 * unwiring will put the entries back in the pmap.
2762df8bae1dSRodney W. Grimes 		 */
2763c0877f10SJohn Dyson 		if (entry->wired_count != 0) {
2764df8bae1dSRodney W. Grimes 			vm_map_entry_unwire(map, entry);
2765c0877f10SJohn Dyson 		}
2766df8bae1dSRodney W. Grimes 
276732a89c32SAlan Cox 		pmap_remove(map->pmap, entry->start, entry->end);
2768df8bae1dSRodney W. Grimes 
2769df8bae1dSRodney W. Grimes 		/*
2770e608cc3cSKonstantin Belousov 		 * Delete the entry only after removing all pmap
2771e608cc3cSKonstantin Belousov 		 * entries pointing to its pages.  (Otherwise, its
2772e608cc3cSKonstantin Belousov 		 * page frames may be reallocated, and any modify bits
2773e608cc3cSKonstantin Belousov 		 * will be set in the wrong object!)
2774df8bae1dSRodney W. Grimes 		 */
2775df8bae1dSRodney W. Grimes 		vm_map_entry_delete(map, entry);
2776df8bae1dSRodney W. Grimes 		entry = next;
2777df8bae1dSRodney W. Grimes 	}
2778df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
2779df8bae1dSRodney W. Grimes }
2780df8bae1dSRodney W. Grimes 
2781df8bae1dSRodney W. Grimes /*
2782df8bae1dSRodney W. Grimes  *	vm_map_remove:
2783df8bae1dSRodney W. Grimes  *
2784df8bae1dSRodney W. Grimes  *	Remove the given address range from the target map.
2785df8bae1dSRodney W. Grimes  *	This is the exported form of vm_map_delete.
2786df8bae1dSRodney W. Grimes  */
2787df8bae1dSRodney W. Grimes int
27881b40f8c0SMatthew Dillon vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
2789df8bae1dSRodney W. Grimes {
27906eaee3feSAlan Cox 	int result;
2791df8bae1dSRodney W. Grimes 
2792df8bae1dSRodney W. Grimes 	vm_map_lock(map);
2793df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
2794655c3490SKonstantin Belousov 	result = vm_map_delete(map, start, end);
2795df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
2796df8bae1dSRodney W. Grimes 	return (result);
2797df8bae1dSRodney W. Grimes }
2798df8bae1dSRodney W. Grimes 
2799df8bae1dSRodney W. Grimes /*
2800df8bae1dSRodney W. Grimes  *	vm_map_check_protection:
2801df8bae1dSRodney W. Grimes  *
28022d5c7e45SMatthew Dillon  *	Assert that the target map allows the specified privilege on the
28032d5c7e45SMatthew Dillon  *	entire address region given.  The entire region must be allocated.
28042d5c7e45SMatthew Dillon  *
28052d5c7e45SMatthew Dillon  *	WARNING!  This code does not and should not check whether the
28062d5c7e45SMatthew Dillon  *	contents of the region is accessible.  For example a smaller file
28072d5c7e45SMatthew Dillon  *	might be mapped into a larger address space.
28082d5c7e45SMatthew Dillon  *
28092d5c7e45SMatthew Dillon  *	NOTE!  This code is also called by munmap().
2810d8834602SAlan Cox  *
2811d8834602SAlan Cox  *	The map must be locked.  A read lock is sufficient.
2812df8bae1dSRodney W. Grimes  */
28130d94caffSDavid Greenman boolean_t
2814b9dcd593SBruce Evans vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
2815b9dcd593SBruce Evans 			vm_prot_t protection)
2816df8bae1dSRodney W. Grimes {
2817c0877f10SJohn Dyson 	vm_map_entry_t entry;
2818df8bae1dSRodney W. Grimes 	vm_map_entry_t tmp_entry;
2819df8bae1dSRodney W. Grimes 
2820d8834602SAlan Cox 	if (!vm_map_lookup_entry(map, start, &tmp_entry))
2821df8bae1dSRodney W. Grimes 		return (FALSE);
2822df8bae1dSRodney W. Grimes 	entry = tmp_entry;
2823df8bae1dSRodney W. Grimes 
2824df8bae1dSRodney W. Grimes 	while (start < end) {
2825d8834602SAlan Cox 		if (entry == &map->header)
2826df8bae1dSRodney W. Grimes 			return (FALSE);
2827df8bae1dSRodney W. Grimes 		/*
2828df8bae1dSRodney W. Grimes 		 * No holes allowed!
2829df8bae1dSRodney W. Grimes 		 */
2830d8834602SAlan Cox 		if (start < entry->start)
2831df8bae1dSRodney W. Grimes 			return (FALSE);
2832df8bae1dSRodney W. Grimes 		/*
2833df8bae1dSRodney W. Grimes 		 * Check protection associated with entry.
2834df8bae1dSRodney W. Grimes 		 */
2835d8834602SAlan Cox 		if ((entry->protection & protection) != protection)
2836df8bae1dSRodney W. Grimes 			return (FALSE);
2837df8bae1dSRodney W. Grimes 		/* go to next entry */
2838df8bae1dSRodney W. Grimes 		start = entry->end;
2839df8bae1dSRodney W. Grimes 		entry = entry->next;
2840df8bae1dSRodney W. Grimes 	}
2841df8bae1dSRodney W. Grimes 	return (TRUE);
2842df8bae1dSRodney W. Grimes }
2843df8bae1dSRodney W. Grimes 
284486524867SJohn Dyson /*
2845df8bae1dSRodney W. Grimes  *	vm_map_copy_entry:
2846df8bae1dSRodney W. Grimes  *
2847df8bae1dSRodney W. Grimes  *	Copies the contents of the source entry to the destination
2848df8bae1dSRodney W. Grimes  *	entry.  The entries *must* be aligned properly.
2849df8bae1dSRodney W. Grimes  */
2850f708ef1bSPoul-Henning Kamp static void
28511b40f8c0SMatthew Dillon vm_map_copy_entry(
28521b40f8c0SMatthew Dillon 	vm_map_t src_map,
28531b40f8c0SMatthew Dillon 	vm_map_t dst_map,
28541b40f8c0SMatthew Dillon 	vm_map_entry_t src_entry,
28553364c323SKonstantin Belousov 	vm_map_entry_t dst_entry,
28563364c323SKonstantin Belousov 	vm_ooffset_t *fork_charge)
2857df8bae1dSRodney W. Grimes {
2858c0877f10SJohn Dyson 	vm_object_t src_object;
28593364c323SKonstantin Belousov 	vm_offset_t size;
28603364c323SKonstantin Belousov 	struct uidinfo *uip;
28613364c323SKonstantin Belousov 	int charged;
2862c0877f10SJohn Dyson 
28633a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(dst_map);
28643a0916b8SKonstantin Belousov 
28659fdfe602SMatthew Dillon 	if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
2866df8bae1dSRodney W. Grimes 		return;
2867df8bae1dSRodney W. Grimes 
2868df8bae1dSRodney W. Grimes 	if (src_entry->wired_count == 0) {
2869df8bae1dSRodney W. Grimes 
2870df8bae1dSRodney W. Grimes 		/*
28710d94caffSDavid Greenman 		 * If the source entry is marked needs_copy, it is already
28720d94caffSDavid Greenman 		 * write-protected.
2873df8bae1dSRodney W. Grimes 		 */
2874afa07f7eSJohn Dyson 		if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
2875df8bae1dSRodney W. Grimes 			pmap_protect(src_map->pmap,
2876df8bae1dSRodney W. Grimes 			    src_entry->start,
2877df8bae1dSRodney W. Grimes 			    src_entry->end,
2878df8bae1dSRodney W. Grimes 			    src_entry->protection & ~VM_PROT_WRITE);
2879df8bae1dSRodney W. Grimes 		}
2880b18bfc3dSJohn Dyson 
2881df8bae1dSRodney W. Grimes 		/*
2882df8bae1dSRodney W. Grimes 		 * Make a copy of the object.
2883df8bae1dSRodney W. Grimes 		 */
28843364c323SKonstantin Belousov 		size = src_entry->end - src_entry->start;
28858aef1712SMatthew Dillon 		if ((src_object = src_entry->object.vm_object) != NULL) {
2886a89c6258SAlan Cox 			VM_OBJECT_LOCK(src_object);
28873364c323SKonstantin Belousov 			charged = ENTRY_CHARGED(src_entry);
2888c0877f10SJohn Dyson 			if ((src_object->handle == NULL) &&
2889c0877f10SJohn Dyson 				(src_object->type == OBJT_DEFAULT ||
2890c0877f10SJohn Dyson 				 src_object->type == OBJT_SWAP)) {
2891c0877f10SJohn Dyson 				vm_object_collapse(src_object);
289296fb8cf2SJohn Dyson 				if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
2893c5aaa06dSAlan Cox 					vm_object_split(src_entry);
2894c0877f10SJohn Dyson 					src_object = src_entry->object.vm_object;
2895a89c6258SAlan Cox 				}
2896a89c6258SAlan Cox 			}
2897b921a12bSAlan Cox 			vm_object_reference_locked(src_object);
2898069e9bc1SDoug Rabson 			vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
28993364c323SKonstantin Belousov 			if (src_entry->uip != NULL &&
29003364c323SKonstantin Belousov 			    !(src_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
29013364c323SKonstantin Belousov 				KASSERT(src_object->uip == NULL,
29023364c323SKonstantin Belousov 				    ("OVERCOMMIT: vm_map_copy_entry: uip %p",
29033364c323SKonstantin Belousov 				     src_object));
29043364c323SKonstantin Belousov 				src_object->uip = src_entry->uip;
29053364c323SKonstantin Belousov 				src_object->charge = size;
29063364c323SKonstantin Belousov 			}
2907e2479b4fSAlan Cox 			VM_OBJECT_UNLOCK(src_object);
2908c0877f10SJohn Dyson 			dst_entry->object.vm_object = src_object;
29093364c323SKonstantin Belousov 			if (charged) {
29103364c323SKonstantin Belousov 				uip = curthread->td_ucred->cr_ruidinfo;
29113364c323SKonstantin Belousov 				uihold(uip);
29123364c323SKonstantin Belousov 				dst_entry->uip = uip;
29133364c323SKonstantin Belousov 				*fork_charge += size;
29143364c323SKonstantin Belousov 				if (!(src_entry->eflags &
29153364c323SKonstantin Belousov 				      MAP_ENTRY_NEEDS_COPY)) {
29163364c323SKonstantin Belousov 					uihold(uip);
29173364c323SKonstantin Belousov 					src_entry->uip = uip;
29183364c323SKonstantin Belousov 					*fork_charge += size;
29193364c323SKonstantin Belousov 				}
29203364c323SKonstantin Belousov 			}
2921afa07f7eSJohn Dyson 			src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2922afa07f7eSJohn Dyson 			dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2923b18bfc3dSJohn Dyson 			dst_entry->offset = src_entry->offset;
2924b18bfc3dSJohn Dyson 		} else {
2925b18bfc3dSJohn Dyson 			dst_entry->object.vm_object = NULL;
2926b18bfc3dSJohn Dyson 			dst_entry->offset = 0;
29273364c323SKonstantin Belousov 			if (src_entry->uip != NULL) {
29283364c323SKonstantin Belousov 				dst_entry->uip = curthread->td_ucred->cr_ruidinfo;
29293364c323SKonstantin Belousov 				uihold(dst_entry->uip);
29303364c323SKonstantin Belousov 				*fork_charge += size;
29313364c323SKonstantin Belousov 			}
2932b18bfc3dSJohn Dyson 		}
2933df8bae1dSRodney W. Grimes 
2934df8bae1dSRodney W. Grimes 		pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
2935df8bae1dSRodney W. Grimes 		    dst_entry->end - dst_entry->start, src_entry->start);
29360d94caffSDavid Greenman 	} else {
2937df8bae1dSRodney W. Grimes 		/*
2938df8bae1dSRodney W. Grimes 		 * Of course, wired down pages can't be set copy-on-write.
29390d94caffSDavid Greenman 		 * Cause wired pages to be copied into the new map by
29400d94caffSDavid Greenman 		 * simulating faults (the new pages are pageable)
2941df8bae1dSRodney W. Grimes 		 */
2942121fd461SKonstantin Belousov 		vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry,
2943121fd461SKonstantin Belousov 		    fork_charge);
2944df8bae1dSRodney W. Grimes 	}
2945df8bae1dSRodney W. Grimes }
2946df8bae1dSRodney W. Grimes 
2947df8bae1dSRodney W. Grimes /*
29482a7be1b6SBrian Feldman  * vmspace_map_entry_forked:
29492a7be1b6SBrian Feldman  * Update the newly-forked vmspace each time a map entry is inherited
29502a7be1b6SBrian Feldman  * or copied.  The values for vm_dsize and vm_tsize are approximate
29512a7be1b6SBrian Feldman  * (and mostly-obsolete ideas in the face of mmap(2) et al.)
29522a7be1b6SBrian Feldman  */
29532a7be1b6SBrian Feldman static void
29542a7be1b6SBrian Feldman vmspace_map_entry_forked(const struct vmspace *vm1, struct vmspace *vm2,
29552a7be1b6SBrian Feldman     vm_map_entry_t entry)
29562a7be1b6SBrian Feldman {
29572a7be1b6SBrian Feldman 	vm_size_t entrysize;
29582a7be1b6SBrian Feldman 	vm_offset_t newend;
29592a7be1b6SBrian Feldman 
29602a7be1b6SBrian Feldman 	entrysize = entry->end - entry->start;
29612a7be1b6SBrian Feldman 	vm2->vm_map.size += entrysize;
29622a7be1b6SBrian Feldman 	if (entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP)) {
29632a7be1b6SBrian Feldman 		vm2->vm_ssize += btoc(entrysize);
29642a7be1b6SBrian Feldman 	} else if (entry->start >= (vm_offset_t)vm1->vm_daddr &&
29652a7be1b6SBrian Feldman 	    entry->start < (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)) {
2966b351299cSAndrew Gallatin 		newend = MIN(entry->end,
29672a7be1b6SBrian Feldman 		    (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize));
29682a7be1b6SBrian Feldman 		vm2->vm_dsize += btoc(newend - entry->start);
29692a7be1b6SBrian Feldman 	} else if (entry->start >= (vm_offset_t)vm1->vm_taddr &&
29702a7be1b6SBrian Feldman 	    entry->start < (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)) {
2971b351299cSAndrew Gallatin 		newend = MIN(entry->end,
29722a7be1b6SBrian Feldman 		    (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize));
29732a7be1b6SBrian Feldman 		vm2->vm_tsize += btoc(newend - entry->start);
29742a7be1b6SBrian Feldman 	}
29752a7be1b6SBrian Feldman }
29762a7be1b6SBrian Feldman 
29772a7be1b6SBrian Feldman /*
2978df8bae1dSRodney W. Grimes  * vmspace_fork:
2979df8bae1dSRodney W. Grimes  * Create a new process vmspace structure and vm_map
2980df8bae1dSRodney W. Grimes  * based on those of an existing process.  The new map
2981df8bae1dSRodney W. Grimes  * is based on the old map, according to the inheritance
2982df8bae1dSRodney W. Grimes  * values on the regions in that map.
2983df8bae1dSRodney W. Grimes  *
29842a7be1b6SBrian Feldman  * XXX It might be worth coalescing the entries added to the new vmspace.
29852a7be1b6SBrian Feldman  *
2986df8bae1dSRodney W. Grimes  * The source map must not be locked.
2987df8bae1dSRodney W. Grimes  */
2988df8bae1dSRodney W. Grimes struct vmspace *
29893364c323SKonstantin Belousov vmspace_fork(struct vmspace *vm1, vm_ooffset_t *fork_charge)
2990df8bae1dSRodney W. Grimes {
2991c0877f10SJohn Dyson 	struct vmspace *vm2;
2992df8bae1dSRodney W. Grimes 	vm_map_t old_map = &vm1->vm_map;
2993df8bae1dSRodney W. Grimes 	vm_map_t new_map;
2994df8bae1dSRodney W. Grimes 	vm_map_entry_t old_entry;
2995df8bae1dSRodney W. Grimes 	vm_map_entry_t new_entry;
2996de5f6a77SJohn Dyson 	vm_object_t object;
29971fac7d7fSKonstantin Belousov 	int locked;
2998df8bae1dSRodney W. Grimes 
2999df8bae1dSRodney W. Grimes 	vm_map_lock(old_map);
30002d8acc0fSJohn Dyson 	vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
300189b57fcfSKonstantin Belousov 	if (vm2 == NULL)
300289b57fcfSKonstantin Belousov 		goto unlock_and_return;
30032a7be1b6SBrian Feldman 	vm2->vm_taddr = vm1->vm_taddr;
30042a7be1b6SBrian Feldman 	vm2->vm_daddr = vm1->vm_daddr;
30052a7be1b6SBrian Feldman 	vm2->vm_maxsaddr = vm1->vm_maxsaddr;
3006df8bae1dSRodney W. Grimes 	new_map = &vm2->vm_map;	/* XXX */
30071fac7d7fSKonstantin Belousov 	locked = vm_map_trylock(new_map); /* trylock to silence WITNESS */
30081fac7d7fSKonstantin Belousov 	KASSERT(locked, ("vmspace_fork: lock failed"));
300947221757SJohn Dyson 	new_map->timestamp = 1;
3010df8bae1dSRodney W. Grimes 
3011df8bae1dSRodney W. Grimes 	old_entry = old_map->header.next;
3012df8bae1dSRodney W. Grimes 
3013df8bae1dSRodney W. Grimes 	while (old_entry != &old_map->header) {
3014afa07f7eSJohn Dyson 		if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
3015df8bae1dSRodney W. Grimes 			panic("vm_map_fork: encountered a submap");
3016df8bae1dSRodney W. Grimes 
3017df8bae1dSRodney W. Grimes 		switch (old_entry->inheritance) {
3018df8bae1dSRodney W. Grimes 		case VM_INHERIT_NONE:
3019df8bae1dSRodney W. Grimes 			break;
3020df8bae1dSRodney W. Grimes 
3021df8bae1dSRodney W. Grimes 		case VM_INHERIT_SHARE:
3022df8bae1dSRodney W. Grimes 			/*
3023fed9a903SJohn Dyson 			 * Clone the entry, creating the shared object if necessary.
3024fed9a903SJohn Dyson 			 */
3025fed9a903SJohn Dyson 			object = old_entry->object.vm_object;
3026fed9a903SJohn Dyson 			if (object == NULL) {
3027fed9a903SJohn Dyson 				object = vm_object_allocate(OBJT_DEFAULT,
3028c2e11a03SJohn Dyson 					atop(old_entry->end - old_entry->start));
3029fed9a903SJohn Dyson 				old_entry->object.vm_object = object;
303015d2d313SAlan Cox 				old_entry->offset = 0;
30313364c323SKonstantin Belousov 				if (old_entry->uip != NULL) {
30323364c323SKonstantin Belousov 					object->uip = old_entry->uip;
30333364c323SKonstantin Belousov 					object->charge = old_entry->end -
30343364c323SKonstantin Belousov 					    old_entry->start;
30353364c323SKonstantin Belousov 					old_entry->uip = NULL;
30363364c323SKonstantin Belousov 				}
30379a2f6362SAlan Cox 			}
30389a2f6362SAlan Cox 
30399a2f6362SAlan Cox 			/*
30409a2f6362SAlan Cox 			 * Add the reference before calling vm_object_shadow
30419a2f6362SAlan Cox 			 * to insure that a shadow object is created.
30429a2f6362SAlan Cox 			 */
30439a2f6362SAlan Cox 			vm_object_reference(object);
30449a2f6362SAlan Cox 			if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
30455069bf57SJohn Dyson 				vm_object_shadow(&old_entry->object.vm_object,
30465069bf57SJohn Dyson 					&old_entry->offset,
3047c2e11a03SJohn Dyson 					atop(old_entry->end - old_entry->start));
30485069bf57SJohn Dyson 				old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
3049d30344bdSIan Dowse 				/* Transfer the second reference too. */
3050d30344bdSIan Dowse 				vm_object_reference(
3051d30344bdSIan Dowse 				    old_entry->object.vm_object);
30527fd10fb3SKonstantin Belousov 
30537fd10fb3SKonstantin Belousov 				/*
30547fd10fb3SKonstantin Belousov 				 * As in vm_map_simplify_entry(), the
3055b0994946SKonstantin Belousov 				 * vnode lock will not be acquired in
30567fd10fb3SKonstantin Belousov 				 * this call to vm_object_deallocate().
30577fd10fb3SKonstantin Belousov 				 */
3058d30344bdSIan Dowse 				vm_object_deallocate(object);
30595069bf57SJohn Dyson 				object = old_entry->object.vm_object;
3060fed9a903SJohn Dyson 			}
3061e2479b4fSAlan Cox 			VM_OBJECT_LOCK(object);
3062069e9bc1SDoug Rabson 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
30633364c323SKonstantin Belousov 			if (old_entry->uip != NULL) {
30643364c323SKonstantin Belousov 				KASSERT(object->uip == NULL, ("vmspace_fork both uip"));
30653364c323SKonstantin Belousov 				object->uip = old_entry->uip;
30663364c323SKonstantin Belousov 				object->charge = old_entry->end - old_entry->start;
30673364c323SKonstantin Belousov 				old_entry->uip = NULL;
30683364c323SKonstantin Belousov 			}
3069e2479b4fSAlan Cox 			VM_OBJECT_UNLOCK(object);
3070fed9a903SJohn Dyson 
3071fed9a903SJohn Dyson 			/*
3072ad5fca3bSAlan Cox 			 * Clone the entry, referencing the shared object.
3073df8bae1dSRodney W. Grimes 			 */
3074df8bae1dSRodney W. Grimes 			new_entry = vm_map_entry_create(new_map);
3075df8bae1dSRodney W. Grimes 			*new_entry = *old_entry;
30769f6acfd1SKonstantin Belousov 			new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
30779f6acfd1SKonstantin Belousov 			    MAP_ENTRY_IN_TRANSITION);
3078df8bae1dSRodney W. Grimes 			new_entry->wired_count = 0;
3079df8bae1dSRodney W. Grimes 
3080df8bae1dSRodney W. Grimes 			/*
30810d94caffSDavid Greenman 			 * Insert the entry into the new map -- we know we're
30820d94caffSDavid Greenman 			 * inserting at the end of the new map.
3083df8bae1dSRodney W. Grimes 			 */
3084df8bae1dSRodney W. Grimes 			vm_map_entry_link(new_map, new_map->header.prev,
3085df8bae1dSRodney W. Grimes 			    new_entry);
30862a7be1b6SBrian Feldman 			vmspace_map_entry_forked(vm1, vm2, new_entry);
3087df8bae1dSRodney W. Grimes 
3088df8bae1dSRodney W. Grimes 			/*
3089df8bae1dSRodney W. Grimes 			 * Update the physical map
3090df8bae1dSRodney W. Grimes 			 */
3091df8bae1dSRodney W. Grimes 			pmap_copy(new_map->pmap, old_map->pmap,
3092df8bae1dSRodney W. Grimes 			    new_entry->start,
3093df8bae1dSRodney W. Grimes 			    (old_entry->end - old_entry->start),
3094df8bae1dSRodney W. Grimes 			    old_entry->start);
3095df8bae1dSRodney W. Grimes 			break;
3096df8bae1dSRodney W. Grimes 
3097df8bae1dSRodney W. Grimes 		case VM_INHERIT_COPY:
3098df8bae1dSRodney W. Grimes 			/*
3099df8bae1dSRodney W. Grimes 			 * Clone the entry and link into the map.
3100df8bae1dSRodney W. Grimes 			 */
3101df8bae1dSRodney W. Grimes 			new_entry = vm_map_entry_create(new_map);
3102df8bae1dSRodney W. Grimes 			*new_entry = *old_entry;
31039f6acfd1SKonstantin Belousov 			new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
31049f6acfd1SKonstantin Belousov 			    MAP_ENTRY_IN_TRANSITION);
3105df8bae1dSRodney W. Grimes 			new_entry->wired_count = 0;
3106df8bae1dSRodney W. Grimes 			new_entry->object.vm_object = NULL;
3107121fd461SKonstantin Belousov 			new_entry->uip = NULL;
3108df8bae1dSRodney W. Grimes 			vm_map_entry_link(new_map, new_map->header.prev,
3109df8bae1dSRodney W. Grimes 			    new_entry);
31102a7be1b6SBrian Feldman 			vmspace_map_entry_forked(vm1, vm2, new_entry);
3111bd7e5f99SJohn Dyson 			vm_map_copy_entry(old_map, new_map, old_entry,
31123364c323SKonstantin Belousov 			    new_entry, fork_charge);
3113df8bae1dSRodney W. Grimes 			break;
3114df8bae1dSRodney W. Grimes 		}
3115df8bae1dSRodney W. Grimes 		old_entry = old_entry->next;
3116df8bae1dSRodney W. Grimes 	}
311789b57fcfSKonstantin Belousov unlock_and_return:
3118df8bae1dSRodney W. Grimes 	vm_map_unlock(old_map);
31191fac7d7fSKonstantin Belousov 	if (vm2 != NULL)
31201fac7d7fSKonstantin Belousov 		vm_map_unlock(new_map);
3121df8bae1dSRodney W. Grimes 
3122df8bae1dSRodney W. Grimes 	return (vm2);
3123df8bae1dSRodney W. Grimes }
3124df8bae1dSRodney W. Grimes 
312594f7e29aSAlan Cox int
312694f7e29aSAlan Cox vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
312794f7e29aSAlan Cox     vm_prot_t prot, vm_prot_t max, int cow)
312894f7e29aSAlan Cox {
3129fd75d710SMarcel Moolenaar 	vm_map_entry_t new_entry, prev_entry;
3130fd75d710SMarcel Moolenaar 	vm_offset_t bot, top;
313194f7e29aSAlan Cox 	vm_size_t init_ssize;
3132fd75d710SMarcel Moolenaar 	int orient, rv;
313391d5354aSJohn Baldwin 	rlim_t vmemlim;
313494f7e29aSAlan Cox 
3135fd75d710SMarcel Moolenaar 	/*
3136fd75d710SMarcel Moolenaar 	 * The stack orientation is piggybacked with the cow argument.
3137fd75d710SMarcel Moolenaar 	 * Extract it into orient and mask the cow argument so that we
3138fd75d710SMarcel Moolenaar 	 * don't pass it around further.
3139fd75d710SMarcel Moolenaar 	 * NOTE: We explicitly allow bi-directional stacks.
3140fd75d710SMarcel Moolenaar 	 */
3141fd75d710SMarcel Moolenaar 	orient = cow & (MAP_STACK_GROWS_DOWN|MAP_STACK_GROWS_UP);
3142fd75d710SMarcel Moolenaar 	cow &= ~orient;
3143fd75d710SMarcel Moolenaar 	KASSERT(orient != 0, ("No stack grow direction"));
3144fd75d710SMarcel Moolenaar 
314577bc7900SKonstantin Belousov 	if (addrbos < vm_map_min(map) ||
314677bc7900SKonstantin Belousov 	    addrbos > vm_map_max(map) ||
314777bc7900SKonstantin Belousov 	    addrbos + max_ssize < addrbos)
314894f7e29aSAlan Cox 		return (KERN_NO_SPACE);
3149fd75d710SMarcel Moolenaar 
3150fd75d710SMarcel Moolenaar 	init_ssize = (max_ssize < sgrowsiz) ? max_ssize : sgrowsiz;
315194f7e29aSAlan Cox 
315291d5354aSJohn Baldwin 	PROC_LOCK(curthread->td_proc);
315391d5354aSJohn Baldwin 	vmemlim = lim_cur(curthread->td_proc, RLIMIT_VMEM);
315491d5354aSJohn Baldwin 	PROC_UNLOCK(curthread->td_proc);
315591d5354aSJohn Baldwin 
315694f7e29aSAlan Cox 	vm_map_lock(map);
315794f7e29aSAlan Cox 
315894f7e29aSAlan Cox 	/* If addr is already mapped, no go */
315994f7e29aSAlan Cox 	if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
316094f7e29aSAlan Cox 		vm_map_unlock(map);
316194f7e29aSAlan Cox 		return (KERN_NO_SPACE);
316294f7e29aSAlan Cox 	}
316394f7e29aSAlan Cox 
3164a69ac174SMatthew Dillon 	/* If we would blow our VMEM resource limit, no go */
316591d5354aSJohn Baldwin 	if (map->size + init_ssize > vmemlim) {
3166a69ac174SMatthew Dillon 		vm_map_unlock(map);
3167a69ac174SMatthew Dillon 		return (KERN_NO_SPACE);
3168a69ac174SMatthew Dillon 	}
3169a69ac174SMatthew Dillon 
3170fd75d710SMarcel Moolenaar 	/*
3171fd75d710SMarcel Moolenaar 	 * If we can't accomodate max_ssize in the current mapping, no go.
3172fd75d710SMarcel Moolenaar 	 * However, we need to be aware that subsequent user mappings might
3173fd75d710SMarcel Moolenaar 	 * map into the space we have reserved for stack, and currently this
3174fd75d710SMarcel Moolenaar 	 * space is not protected.
317594f7e29aSAlan Cox 	 *
3176fd75d710SMarcel Moolenaar 	 * Hopefully we will at least detect this condition when we try to
3177fd75d710SMarcel Moolenaar 	 * grow the stack.
317894f7e29aSAlan Cox 	 */
317994f7e29aSAlan Cox 	if ((prev_entry->next != &map->header) &&
318094f7e29aSAlan Cox 	    (prev_entry->next->start < addrbos + max_ssize)) {
318194f7e29aSAlan Cox 		vm_map_unlock(map);
318294f7e29aSAlan Cox 		return (KERN_NO_SPACE);
318394f7e29aSAlan Cox 	}
318494f7e29aSAlan Cox 
3185fd75d710SMarcel Moolenaar 	/*
3186fd75d710SMarcel Moolenaar 	 * We initially map a stack of only init_ssize.  We will grow as
3187fd75d710SMarcel Moolenaar 	 * needed later.  Depending on the orientation of the stack (i.e.
3188fd75d710SMarcel Moolenaar 	 * the grow direction) we either map at the top of the range, the
3189fd75d710SMarcel Moolenaar 	 * bottom of the range or in the middle.
319094f7e29aSAlan Cox 	 *
3191fd75d710SMarcel Moolenaar 	 * Note: we would normally expect prot and max to be VM_PROT_ALL,
3192fd75d710SMarcel Moolenaar 	 * and cow to be 0.  Possibly we should eliminate these as input
3193fd75d710SMarcel Moolenaar 	 * parameters, and just pass these values here in the insert call.
319494f7e29aSAlan Cox 	 */
3195fd75d710SMarcel Moolenaar 	if (orient == MAP_STACK_GROWS_DOWN)
3196fd75d710SMarcel Moolenaar 		bot = addrbos + max_ssize - init_ssize;
3197fd75d710SMarcel Moolenaar 	else if (orient == MAP_STACK_GROWS_UP)
3198fd75d710SMarcel Moolenaar 		bot = addrbos;
3199fd75d710SMarcel Moolenaar 	else
3200fd75d710SMarcel Moolenaar 		bot = round_page(addrbos + max_ssize/2 - init_ssize/2);
3201fd75d710SMarcel Moolenaar 	top = bot + init_ssize;
3202fd75d710SMarcel Moolenaar 	rv = vm_map_insert(map, NULL, 0, bot, top, prot, max, cow);
320394f7e29aSAlan Cox 
3204fd75d710SMarcel Moolenaar 	/* Now set the avail_ssize amount. */
320594f7e29aSAlan Cox 	if (rv == KERN_SUCCESS) {
320629b45e9eSAlan Cox 		if (prev_entry != &map->header)
3207fd75d710SMarcel Moolenaar 			vm_map_clip_end(map, prev_entry, bot);
3208fd75d710SMarcel Moolenaar 		new_entry = prev_entry->next;
3209fd75d710SMarcel Moolenaar 		if (new_entry->end != top || new_entry->start != bot)
321094f7e29aSAlan Cox 			panic("Bad entry start/end for new stack entry");
3211b21a0008SMarcel Moolenaar 
3212fd75d710SMarcel Moolenaar 		new_entry->avail_ssize = max_ssize - init_ssize;
3213fd75d710SMarcel Moolenaar 		if (orient & MAP_STACK_GROWS_DOWN)
3214fd75d710SMarcel Moolenaar 			new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
3215fd75d710SMarcel Moolenaar 		if (orient & MAP_STACK_GROWS_UP)
3216fd75d710SMarcel Moolenaar 			new_entry->eflags |= MAP_ENTRY_GROWS_UP;
321794f7e29aSAlan Cox 	}
321894f7e29aSAlan Cox 
321994f7e29aSAlan Cox 	vm_map_unlock(map);
322094f7e29aSAlan Cox 	return (rv);
322194f7e29aSAlan Cox }
322294f7e29aSAlan Cox 
322394f7e29aSAlan Cox /* Attempts to grow a vm stack entry.  Returns KERN_SUCCESS if the
322494f7e29aSAlan Cox  * desired address is already mapped, or if we successfully grow
322594f7e29aSAlan Cox  * the stack.  Also returns KERN_SUCCESS if addr is outside the
322694f7e29aSAlan Cox  * stack range (this is strange, but preserves compatibility with
322794f7e29aSAlan Cox  * the grow function in vm_machdep.c).
322894f7e29aSAlan Cox  */
322994f7e29aSAlan Cox int
323094f7e29aSAlan Cox vm_map_growstack(struct proc *p, vm_offset_t addr)
323194f7e29aSAlan Cox {
3232b21a0008SMarcel Moolenaar 	vm_map_entry_t next_entry, prev_entry;
3233b21a0008SMarcel Moolenaar 	vm_map_entry_t new_entry, stack_entry;
323494f7e29aSAlan Cox 	struct vmspace *vm = p->p_vmspace;
323594f7e29aSAlan Cox 	vm_map_t map = &vm->vm_map;
323694f7e29aSAlan Cox 	vm_offset_t end;
3237b21a0008SMarcel Moolenaar 	size_t grow_amount, max_grow;
323891d5354aSJohn Baldwin 	rlim_t stacklim, vmemlim;
3239b21a0008SMarcel Moolenaar 	int is_procstack, rv;
32403364c323SKonstantin Belousov 	struct uidinfo *uip;
324123955314SAlfred Perlstein 
324294f7e29aSAlan Cox Retry:
324391d5354aSJohn Baldwin 	PROC_LOCK(p);
324491d5354aSJohn Baldwin 	stacklim = lim_cur(p, RLIMIT_STACK);
3245bfee999dSAlan Cox 	vmemlim = lim_cur(p, RLIMIT_VMEM);
324691d5354aSJohn Baldwin 	PROC_UNLOCK(p);
324791d5354aSJohn Baldwin 
324894f7e29aSAlan Cox 	vm_map_lock_read(map);
324994f7e29aSAlan Cox 
325094f7e29aSAlan Cox 	/* If addr is already in the entry range, no need to grow.*/
325194f7e29aSAlan Cox 	if (vm_map_lookup_entry(map, addr, &prev_entry)) {
325294f7e29aSAlan Cox 		vm_map_unlock_read(map);
32530cddd8f0SMatthew Dillon 		return (KERN_SUCCESS);
325494f7e29aSAlan Cox 	}
325594f7e29aSAlan Cox 
3256b21a0008SMarcel Moolenaar 	next_entry = prev_entry->next;
3257b21a0008SMarcel Moolenaar 	if (!(prev_entry->eflags & MAP_ENTRY_GROWS_UP)) {
3258b21a0008SMarcel Moolenaar 		/*
3259b21a0008SMarcel Moolenaar 		 * This entry does not grow upwards. Since the address lies
3260b21a0008SMarcel Moolenaar 		 * beyond this entry, the next entry (if one exists) has to
3261b21a0008SMarcel Moolenaar 		 * be a downward growable entry. The entry list header is
3262b21a0008SMarcel Moolenaar 		 * never a growable entry, so it suffices to check the flags.
326394f7e29aSAlan Cox 		 */
3264b21a0008SMarcel Moolenaar 		if (!(next_entry->eflags & MAP_ENTRY_GROWS_DOWN)) {
326594f7e29aSAlan Cox 			vm_map_unlock_read(map);
32660cddd8f0SMatthew Dillon 			return (KERN_SUCCESS);
326794f7e29aSAlan Cox 		}
3268b21a0008SMarcel Moolenaar 		stack_entry = next_entry;
3269b21a0008SMarcel Moolenaar 	} else {
3270b21a0008SMarcel Moolenaar 		/*
3271b21a0008SMarcel Moolenaar 		 * This entry grows upward. If the next entry does not at
3272b21a0008SMarcel Moolenaar 		 * least grow downwards, this is the entry we need to grow.
3273b21a0008SMarcel Moolenaar 		 * otherwise we have two possible choices and we have to
3274b21a0008SMarcel Moolenaar 		 * select one.
3275b21a0008SMarcel Moolenaar 		 */
3276b21a0008SMarcel Moolenaar 		if (next_entry->eflags & MAP_ENTRY_GROWS_DOWN) {
3277b21a0008SMarcel Moolenaar 			/*
3278b21a0008SMarcel Moolenaar 			 * We have two choices; grow the entry closest to
3279b21a0008SMarcel Moolenaar 			 * the address to minimize the amount of growth.
3280b21a0008SMarcel Moolenaar 			 */
3281b21a0008SMarcel Moolenaar 			if (addr - prev_entry->end <= next_entry->start - addr)
3282b21a0008SMarcel Moolenaar 				stack_entry = prev_entry;
3283b21a0008SMarcel Moolenaar 			else
3284b21a0008SMarcel Moolenaar 				stack_entry = next_entry;
3285b21a0008SMarcel Moolenaar 		} else
3286b21a0008SMarcel Moolenaar 			stack_entry = prev_entry;
3287b21a0008SMarcel Moolenaar 	}
328894f7e29aSAlan Cox 
3289b21a0008SMarcel Moolenaar 	if (stack_entry == next_entry) {
3290b21a0008SMarcel Moolenaar 		KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_DOWN, ("foo"));
3291b21a0008SMarcel Moolenaar 		KASSERT(addr < stack_entry->start, ("foo"));
3292b21a0008SMarcel Moolenaar 		end = (prev_entry != &map->header) ? prev_entry->end :
3293b21a0008SMarcel Moolenaar 		    stack_entry->start - stack_entry->avail_ssize;
329494f7e29aSAlan Cox 		grow_amount = roundup(stack_entry->start - addr, PAGE_SIZE);
3295b21a0008SMarcel Moolenaar 		max_grow = stack_entry->start - end;
3296b21a0008SMarcel Moolenaar 	} else {
3297b21a0008SMarcel Moolenaar 		KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_UP, ("foo"));
329808667f6dSMarcel Moolenaar 		KASSERT(addr >= stack_entry->end, ("foo"));
3299b21a0008SMarcel Moolenaar 		end = (next_entry != &map->header) ? next_entry->start :
3300b21a0008SMarcel Moolenaar 		    stack_entry->end + stack_entry->avail_ssize;
3301fd75d710SMarcel Moolenaar 		grow_amount = roundup(addr + 1 - stack_entry->end, PAGE_SIZE);
3302b21a0008SMarcel Moolenaar 		max_grow = end - stack_entry->end;
3303b21a0008SMarcel Moolenaar 	}
3304b21a0008SMarcel Moolenaar 
330594f7e29aSAlan Cox 	if (grow_amount > stack_entry->avail_ssize) {
330694f7e29aSAlan Cox 		vm_map_unlock_read(map);
33070cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
330894f7e29aSAlan Cox 	}
330994f7e29aSAlan Cox 
3310b21a0008SMarcel Moolenaar 	/*
3311b21a0008SMarcel Moolenaar 	 * If there is no longer enough space between the entries nogo, and
3312b21a0008SMarcel Moolenaar 	 * adjust the available space.  Note: this  should only happen if the
3313b21a0008SMarcel Moolenaar 	 * user has mapped into the stack area after the stack was created,
3314b21a0008SMarcel Moolenaar 	 * and is probably an error.
331594f7e29aSAlan Cox 	 *
3316b21a0008SMarcel Moolenaar 	 * This also effectively destroys any guard page the user might have
3317b21a0008SMarcel Moolenaar 	 * intended by limiting the stack size.
331894f7e29aSAlan Cox 	 */
3319b21a0008SMarcel Moolenaar 	if (grow_amount > max_grow) {
332025adb370SBrian Feldman 		if (vm_map_lock_upgrade(map))
332194f7e29aSAlan Cox 			goto Retry;
332294f7e29aSAlan Cox 
3323b21a0008SMarcel Moolenaar 		stack_entry->avail_ssize = max_grow;
332494f7e29aSAlan Cox 
332594f7e29aSAlan Cox 		vm_map_unlock(map);
33260cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
332794f7e29aSAlan Cox 	}
332894f7e29aSAlan Cox 
3329b21a0008SMarcel Moolenaar 	is_procstack = (addr >= (vm_offset_t)vm->vm_maxsaddr) ? 1 : 0;
333094f7e29aSAlan Cox 
3331b21a0008SMarcel Moolenaar 	/*
3332b21a0008SMarcel Moolenaar 	 * If this is the main process stack, see if we're over the stack
3333b21a0008SMarcel Moolenaar 	 * limit.
333494f7e29aSAlan Cox 	 */
333591d5354aSJohn Baldwin 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
333694f7e29aSAlan Cox 		vm_map_unlock_read(map);
33370cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
333894f7e29aSAlan Cox 	}
333994f7e29aSAlan Cox 
334094f7e29aSAlan Cox 	/* Round up the grow amount modulo SGROWSIZ */
3341cbc89bfbSPaul Saab 	grow_amount = roundup (grow_amount, sgrowsiz);
3342b21a0008SMarcel Moolenaar 	if (grow_amount > stack_entry->avail_ssize)
334394f7e29aSAlan Cox 		grow_amount = stack_entry->avail_ssize;
334491d5354aSJohn Baldwin 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
334591d5354aSJohn Baldwin 		grow_amount = stacklim - ctob(vm->vm_ssize);
334694f7e29aSAlan Cox 	}
334794f7e29aSAlan Cox 
3348a69ac174SMatthew Dillon 	/* If we would blow our VMEM resource limit, no go */
334991d5354aSJohn Baldwin 	if (map->size + grow_amount > vmemlim) {
3350a69ac174SMatthew Dillon 		vm_map_unlock_read(map);
3351a69ac174SMatthew Dillon 		return (KERN_NO_SPACE);
3352a69ac174SMatthew Dillon 	}
3353a69ac174SMatthew Dillon 
335425adb370SBrian Feldman 	if (vm_map_lock_upgrade(map))
335594f7e29aSAlan Cox 		goto Retry;
335694f7e29aSAlan Cox 
3357b21a0008SMarcel Moolenaar 	if (stack_entry == next_entry) {
3358b21a0008SMarcel Moolenaar 		/*
3359b21a0008SMarcel Moolenaar 		 * Growing downward.
3360b21a0008SMarcel Moolenaar 		 */
336194f7e29aSAlan Cox 		/* Get the preliminary new entry start value */
336294f7e29aSAlan Cox 		addr = stack_entry->start - grow_amount;
336394f7e29aSAlan Cox 
3364b21a0008SMarcel Moolenaar 		/*
3365b21a0008SMarcel Moolenaar 		 * If this puts us into the previous entry, cut back our
3366b21a0008SMarcel Moolenaar 		 * growth to the available space. Also, see the note above.
336794f7e29aSAlan Cox 		 */
336894f7e29aSAlan Cox 		if (addr < end) {
3369b21a0008SMarcel Moolenaar 			stack_entry->avail_ssize = max_grow;
337094f7e29aSAlan Cox 			addr = end;
337194f7e29aSAlan Cox 		}
337294f7e29aSAlan Cox 
337394f7e29aSAlan Cox 		rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start,
337405ba50f5SJake Burkholder 		    p->p_sysent->sv_stackprot, VM_PROT_ALL, 0);
337594f7e29aSAlan Cox 
337694f7e29aSAlan Cox 		/* Adjust the available stack space by the amount we grew. */
337794f7e29aSAlan Cox 		if (rv == KERN_SUCCESS) {
337829b45e9eSAlan Cox 			if (prev_entry != &map->header)
337929b45e9eSAlan Cox 				vm_map_clip_end(map, prev_entry, addr);
3380b21a0008SMarcel Moolenaar 			new_entry = prev_entry->next;
3381b21a0008SMarcel Moolenaar 			KASSERT(new_entry == stack_entry->prev, ("foo"));
3382b21a0008SMarcel Moolenaar 			KASSERT(new_entry->end == stack_entry->start, ("foo"));
3383b21a0008SMarcel Moolenaar 			KASSERT(new_entry->start == addr, ("foo"));
3384b21a0008SMarcel Moolenaar 			grow_amount = new_entry->end - new_entry->start;
3385b21a0008SMarcel Moolenaar 			new_entry->avail_ssize = stack_entry->avail_ssize -
3386b21a0008SMarcel Moolenaar 			    grow_amount;
3387b21a0008SMarcel Moolenaar 			stack_entry->eflags &= ~MAP_ENTRY_GROWS_DOWN;
3388b21a0008SMarcel Moolenaar 			new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
338994f7e29aSAlan Cox 		}
3390b21a0008SMarcel Moolenaar 	} else {
3391b21a0008SMarcel Moolenaar 		/*
3392b21a0008SMarcel Moolenaar 		 * Growing upward.
3393b21a0008SMarcel Moolenaar 		 */
3394b21a0008SMarcel Moolenaar 		addr = stack_entry->end + grow_amount;
3395b21a0008SMarcel Moolenaar 
3396b21a0008SMarcel Moolenaar 		/*
3397b21a0008SMarcel Moolenaar 		 * If this puts us into the next entry, cut back our growth
3398b21a0008SMarcel Moolenaar 		 * to the available space. Also, see the note above.
3399b21a0008SMarcel Moolenaar 		 */
3400b21a0008SMarcel Moolenaar 		if (addr > end) {
3401b21a0008SMarcel Moolenaar 			stack_entry->avail_ssize = end - stack_entry->end;
3402b21a0008SMarcel Moolenaar 			addr = end;
340394f7e29aSAlan Cox 		}
340494f7e29aSAlan Cox 
3405b21a0008SMarcel Moolenaar 		grow_amount = addr - stack_entry->end;
34063364c323SKonstantin Belousov 		uip = stack_entry->uip;
34073364c323SKonstantin Belousov 		if (uip == NULL && stack_entry->object.vm_object != NULL)
34083364c323SKonstantin Belousov 			uip = stack_entry->object.vm_object->uip;
34093364c323SKonstantin Belousov 		if (uip != NULL && !swap_reserve_by_uid(grow_amount, uip))
34103364c323SKonstantin Belousov 			rv = KERN_NO_SPACE;
3411b21a0008SMarcel Moolenaar 		/* Grow the underlying object if applicable. */
34123364c323SKonstantin Belousov 		else if (stack_entry->object.vm_object == NULL ||
3413b21a0008SMarcel Moolenaar 			 vm_object_coalesce(stack_entry->object.vm_object,
341457a21abaSAlan Cox 			 stack_entry->offset,
3415b21a0008SMarcel Moolenaar 			 (vm_size_t)(stack_entry->end - stack_entry->start),
34163364c323SKonstantin Belousov 			 (vm_size_t)grow_amount, uip != NULL)) {
341708667f6dSMarcel Moolenaar 			map->size += (addr - stack_entry->end);
3418b21a0008SMarcel Moolenaar 			/* Update the current entry. */
3419b21a0008SMarcel Moolenaar 			stack_entry->end = addr;
3420199c91abSMarcel Moolenaar 			stack_entry->avail_ssize -= grow_amount;
34210164e057SAlan Cox 			vm_map_entry_resize_free(map, stack_entry);
3422b21a0008SMarcel Moolenaar 			rv = KERN_SUCCESS;
3423b21a0008SMarcel Moolenaar 
3424b21a0008SMarcel Moolenaar 			if (next_entry != &map->header)
3425b21a0008SMarcel Moolenaar 				vm_map_clip_start(map, next_entry, addr);
3426b21a0008SMarcel Moolenaar 		} else
3427b21a0008SMarcel Moolenaar 			rv = KERN_FAILURE;
3428b21a0008SMarcel Moolenaar 	}
3429b21a0008SMarcel Moolenaar 
3430b21a0008SMarcel Moolenaar 	if (rv == KERN_SUCCESS && is_procstack)
3431b21a0008SMarcel Moolenaar 		vm->vm_ssize += btoc(grow_amount);
3432b21a0008SMarcel Moolenaar 
343394f7e29aSAlan Cox 	vm_map_unlock(map);
3434b21a0008SMarcel Moolenaar 
3435abd498aaSBruce M Simpson 	/*
3436abd498aaSBruce M Simpson 	 * Heed the MAP_WIREFUTURE flag if it was set for this process.
3437abd498aaSBruce M Simpson 	 */
3438b21a0008SMarcel Moolenaar 	if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE)) {
3439b21a0008SMarcel Moolenaar 		vm_map_wire(map,
3440b21a0008SMarcel Moolenaar 		    (stack_entry == next_entry) ? addr : addr - grow_amount,
3441b21a0008SMarcel Moolenaar 		    (stack_entry == next_entry) ? stack_entry->start : addr,
3442b21a0008SMarcel Moolenaar 		    (p->p_flag & P_SYSTEM)
3443b21a0008SMarcel Moolenaar 		    ? VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES
3444b21a0008SMarcel Moolenaar 		    : VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
3445b21a0008SMarcel Moolenaar 	}
3446abd498aaSBruce M Simpson 
34470cddd8f0SMatthew Dillon 	return (rv);
344894f7e29aSAlan Cox }
344994f7e29aSAlan Cox 
3450df8bae1dSRodney W. Grimes /*
34515856e12eSJohn Dyson  * Unshare the specified VM space for exec.  If other processes are
34525856e12eSJohn Dyson  * mapped to it, then create a new one.  The new vmspace is null.
34535856e12eSJohn Dyson  */
345489b57fcfSKonstantin Belousov int
34553ebc1248SPeter Wemm vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser)
34561b40f8c0SMatthew Dillon {
34575856e12eSJohn Dyson 	struct vmspace *oldvmspace = p->p_vmspace;
34585856e12eSJohn Dyson 	struct vmspace *newvmspace;
34595856e12eSJohn Dyson 
34603ebc1248SPeter Wemm 	newvmspace = vmspace_alloc(minuser, maxuser);
346189b57fcfSKonstantin Belousov 	if (newvmspace == NULL)
346289b57fcfSKonstantin Belousov 		return (ENOMEM);
346351ab6c28SAlan Cox 	newvmspace->vm_swrss = oldvmspace->vm_swrss;
34645856e12eSJohn Dyson 	/*
34655856e12eSJohn Dyson 	 * This code is written like this for prototype purposes.  The
34665856e12eSJohn Dyson 	 * goal is to avoid running down the vmspace here, but let the
34675856e12eSJohn Dyson 	 * other process's that are still using the vmspace to finally
34685856e12eSJohn Dyson 	 * run it down.  Even though there is little or no chance of blocking
34695856e12eSJohn Dyson 	 * here, it is a good idea to keep this form for future mods.
34705856e12eSJohn Dyson 	 */
347157051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
34725856e12eSJohn Dyson 	p->p_vmspace = newvmspace;
347357051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
34746617724cSJeff Roberson 	if (p == curthread->td_proc)
3475b40ce416SJulian Elischer 		pmap_activate(curthread);
3476b56ef1c1SJohn Baldwin 	vmspace_free(oldvmspace);
347789b57fcfSKonstantin Belousov 	return (0);
34785856e12eSJohn Dyson }
34795856e12eSJohn Dyson 
34805856e12eSJohn Dyson /*
34815856e12eSJohn Dyson  * Unshare the specified VM space for forcing COW.  This
34825856e12eSJohn Dyson  * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
34835856e12eSJohn Dyson  */
348489b57fcfSKonstantin Belousov int
34851b40f8c0SMatthew Dillon vmspace_unshare(struct proc *p)
34861b40f8c0SMatthew Dillon {
34875856e12eSJohn Dyson 	struct vmspace *oldvmspace = p->p_vmspace;
34885856e12eSJohn Dyson 	struct vmspace *newvmspace;
34893364c323SKonstantin Belousov 	vm_ooffset_t fork_charge;
34905856e12eSJohn Dyson 
34915856e12eSJohn Dyson 	if (oldvmspace->vm_refcnt == 1)
349289b57fcfSKonstantin Belousov 		return (0);
34933364c323SKonstantin Belousov 	fork_charge = 0;
34943364c323SKonstantin Belousov 	newvmspace = vmspace_fork(oldvmspace, &fork_charge);
349589b57fcfSKonstantin Belousov 	if (newvmspace == NULL)
349689b57fcfSKonstantin Belousov 		return (ENOMEM);
34973364c323SKonstantin Belousov 	if (!swap_reserve_by_uid(fork_charge, p->p_ucred->cr_ruidinfo)) {
34983364c323SKonstantin Belousov 		vmspace_free(newvmspace);
34993364c323SKonstantin Belousov 		return (ENOMEM);
35003364c323SKonstantin Belousov 	}
350157051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
35025856e12eSJohn Dyson 	p->p_vmspace = newvmspace;
350357051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
35046617724cSJeff Roberson 	if (p == curthread->td_proc)
3505b40ce416SJulian Elischer 		pmap_activate(curthread);
3506b56ef1c1SJohn Baldwin 	vmspace_free(oldvmspace);
350789b57fcfSKonstantin Belousov 	return (0);
35085856e12eSJohn Dyson }
35095856e12eSJohn Dyson 
35105856e12eSJohn Dyson /*
3511df8bae1dSRodney W. Grimes  *	vm_map_lookup:
3512df8bae1dSRodney W. Grimes  *
3513df8bae1dSRodney W. Grimes  *	Finds the VM object, offset, and
3514df8bae1dSRodney W. Grimes  *	protection for a given virtual address in the
3515df8bae1dSRodney W. Grimes  *	specified map, assuming a page fault of the
3516df8bae1dSRodney W. Grimes  *	type specified.
3517df8bae1dSRodney W. Grimes  *
3518df8bae1dSRodney W. Grimes  *	Leaves the map in question locked for read; return
3519df8bae1dSRodney W. Grimes  *	values are guaranteed until a vm_map_lookup_done
3520df8bae1dSRodney W. Grimes  *	call is performed.  Note that the map argument
3521df8bae1dSRodney W. Grimes  *	is in/out; the returned map must be used in
3522df8bae1dSRodney W. Grimes  *	the call to vm_map_lookup_done.
3523df8bae1dSRodney W. Grimes  *
3524df8bae1dSRodney W. Grimes  *	A handle (out_entry) is returned for use in
3525df8bae1dSRodney W. Grimes  *	vm_map_lookup_done, to make that fast.
3526df8bae1dSRodney W. Grimes  *
3527df8bae1dSRodney W. Grimes  *	If a lookup is requested with "write protection"
3528df8bae1dSRodney W. Grimes  *	specified, the map may be changed to perform virtual
3529df8bae1dSRodney W. Grimes  *	copying operations, although the data referenced will
3530df8bae1dSRodney W. Grimes  *	remain the same.
3531df8bae1dSRodney W. Grimes  */
3532df8bae1dSRodney W. Grimes int
3533b9dcd593SBruce Evans vm_map_lookup(vm_map_t *var_map,		/* IN/OUT */
3534b9dcd593SBruce Evans 	      vm_offset_t vaddr,
353547221757SJohn Dyson 	      vm_prot_t fault_typea,
3536b9dcd593SBruce Evans 	      vm_map_entry_t *out_entry,	/* OUT */
3537b9dcd593SBruce Evans 	      vm_object_t *object,		/* OUT */
3538b9dcd593SBruce Evans 	      vm_pindex_t *pindex,		/* OUT */
3539b9dcd593SBruce Evans 	      vm_prot_t *out_prot,		/* OUT */
35402d8acc0fSJohn Dyson 	      boolean_t *wired)			/* OUT */
3541df8bae1dSRodney W. Grimes {
3542c0877f10SJohn Dyson 	vm_map_entry_t entry;
3543c0877f10SJohn Dyson 	vm_map_t map = *var_map;
3544c0877f10SJohn Dyson 	vm_prot_t prot;
354547221757SJohn Dyson 	vm_prot_t fault_type = fault_typea;
35463364c323SKonstantin Belousov 	vm_object_t eobject;
35473364c323SKonstantin Belousov 	struct uidinfo *uip;
35483364c323SKonstantin Belousov 	vm_ooffset_t size;
3549df8bae1dSRodney W. Grimes 
3550df8bae1dSRodney W. Grimes RetryLookup:;
3551df8bae1dSRodney W. Grimes 
3552df8bae1dSRodney W. Grimes 	vm_map_lock_read(map);
3553df8bae1dSRodney W. Grimes 
3554df8bae1dSRodney W. Grimes 	/*
35554c3ef59eSAlan Cox 	 * Lookup the faulting address.
3556df8bae1dSRodney W. Grimes 	 */
3557095104acSAlan Cox 	if (!vm_map_lookup_entry(map, vaddr, out_entry)) {
3558095104acSAlan Cox 		vm_map_unlock_read(map);
3559095104acSAlan Cox 		return (KERN_INVALID_ADDRESS);
3560095104acSAlan Cox 	}
3561df8bae1dSRodney W. Grimes 
35624e94f402SAlan Cox 	entry = *out_entry;
3563b7b2aac2SJohn Dyson 
3564df8bae1dSRodney W. Grimes 	/*
3565df8bae1dSRodney W. Grimes 	 * Handle submaps.
3566df8bae1dSRodney W. Grimes 	 */
3567afa07f7eSJohn Dyson 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3568df8bae1dSRodney W. Grimes 		vm_map_t old_map = map;
3569df8bae1dSRodney W. Grimes 
3570df8bae1dSRodney W. Grimes 		*var_map = map = entry->object.sub_map;
3571df8bae1dSRodney W. Grimes 		vm_map_unlock_read(old_map);
3572df8bae1dSRodney W. Grimes 		goto RetryLookup;
3573df8bae1dSRodney W. Grimes 	}
3574a04c970aSJohn Dyson 
3575df8bae1dSRodney W. Grimes 	/*
35760d94caffSDavid Greenman 	 * Check whether this task is allowed to have this page.
3577df8bae1dSRodney W. Grimes 	 */
3578df8bae1dSRodney W. Grimes 	prot = entry->protection;
357947221757SJohn Dyson 	fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
35802db65ab4SAlan Cox 	if ((fault_type & prot) != fault_type || prot == VM_PROT_NONE) {
3581095104acSAlan Cox 		vm_map_unlock_read(map);
3582095104acSAlan Cox 		return (KERN_PROTECTION_FAILURE);
358347221757SJohn Dyson 	}
35842ed14a92SAlan Cox 	if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
358547221757SJohn Dyson 	    (entry->eflags & MAP_ENTRY_COW) &&
3586a6d42a0dSAlan Cox 	    (fault_type & VM_PROT_WRITE)) {
3587095104acSAlan Cox 		vm_map_unlock_read(map);
3588095104acSAlan Cox 		return (KERN_PROTECTION_FAILURE);
3589a04c970aSJohn Dyson 	}
3590df8bae1dSRodney W. Grimes 
3591df8bae1dSRodney W. Grimes 	/*
35920d94caffSDavid Greenman 	 * If this page is not pageable, we have to get it for all possible
35930d94caffSDavid Greenman 	 * accesses.
3594df8bae1dSRodney W. Grimes 	 */
359505f0fdd2SPoul-Henning Kamp 	*wired = (entry->wired_count != 0);
359605f0fdd2SPoul-Henning Kamp 	if (*wired)
3597a6d42a0dSAlan Cox 		fault_type = entry->protection;
35983364c323SKonstantin Belousov 	size = entry->end - entry->start;
3599df8bae1dSRodney W. Grimes 	/*
3600df8bae1dSRodney W. Grimes 	 * If the entry was copy-on-write, we either ...
3601df8bae1dSRodney W. Grimes 	 */
3602afa07f7eSJohn Dyson 	if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3603df8bae1dSRodney W. Grimes 		/*
36040d94caffSDavid Greenman 		 * If we want to write the page, we may as well handle that
3605ad5fca3bSAlan Cox 		 * now since we've got the map locked.
3606df8bae1dSRodney W. Grimes 		 *
36070d94caffSDavid Greenman 		 * If we don't need to write the page, we just demote the
36080d94caffSDavid Greenman 		 * permissions allowed.
3609df8bae1dSRodney W. Grimes 		 */
3610a6d42a0dSAlan Cox 		if ((fault_type & VM_PROT_WRITE) != 0 ||
3611a6d42a0dSAlan Cox 		    (fault_typea & VM_PROT_COPY) != 0) {
3612df8bae1dSRodney W. Grimes 			/*
36130d94caffSDavid Greenman 			 * Make a new object, and place it in the object
36140d94caffSDavid Greenman 			 * chain.  Note that no new references have appeared
3615ad5fca3bSAlan Cox 			 * -- one just moved from the map to the new
36160d94caffSDavid Greenman 			 * object.
3617df8bae1dSRodney W. Grimes 			 */
361825adb370SBrian Feldman 			if (vm_map_lock_upgrade(map))
3619df8bae1dSRodney W. Grimes 				goto RetryLookup;
36209917e010SAlan Cox 
36213364c323SKonstantin Belousov 			if (entry->uip == NULL) {
36223364c323SKonstantin Belousov 				/*
36233364c323SKonstantin Belousov 				 * The debugger owner is charged for
36243364c323SKonstantin Belousov 				 * the memory.
36253364c323SKonstantin Belousov 				 */
36263364c323SKonstantin Belousov 				uip = curthread->td_ucred->cr_ruidinfo;
36273364c323SKonstantin Belousov 				uihold(uip);
36283364c323SKonstantin Belousov 				if (!swap_reserve_by_uid(size, uip)) {
36293364c323SKonstantin Belousov 					uifree(uip);
36303364c323SKonstantin Belousov 					vm_map_unlock(map);
36313364c323SKonstantin Belousov 					return (KERN_RESOURCE_SHORTAGE);
36323364c323SKonstantin Belousov 				}
36333364c323SKonstantin Belousov 				entry->uip = uip;
36343364c323SKonstantin Belousov 			}
3635df8bae1dSRodney W. Grimes 			vm_object_shadow(
3636df8bae1dSRodney W. Grimes 			    &entry->object.vm_object,
3637df8bae1dSRodney W. Grimes 			    &entry->offset,
36383364c323SKonstantin Belousov 			    atop(size));
3639afa07f7eSJohn Dyson 			entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
36403364c323SKonstantin Belousov 			eobject = entry->object.vm_object;
36413364c323SKonstantin Belousov 			if (eobject->uip != NULL) {
36423364c323SKonstantin Belousov 				/*
36433364c323SKonstantin Belousov 				 * The object was not shadowed.
36443364c323SKonstantin Belousov 				 */
36453364c323SKonstantin Belousov 				swap_release_by_uid(size, entry->uip);
36463364c323SKonstantin Belousov 				uifree(entry->uip);
36473364c323SKonstantin Belousov 				entry->uip = NULL;
36483364c323SKonstantin Belousov 			} else if (entry->uip != NULL) {
36493364c323SKonstantin Belousov 				VM_OBJECT_LOCK(eobject);
36503364c323SKonstantin Belousov 				eobject->uip = entry->uip;
36513364c323SKonstantin Belousov 				eobject->charge = size;
36523364c323SKonstantin Belousov 				VM_OBJECT_UNLOCK(eobject);
36533364c323SKonstantin Belousov 				entry->uip = NULL;
36543364c323SKonstantin Belousov 			}
36559917e010SAlan Cox 
36569b09b6c7SMatthew Dillon 			vm_map_lock_downgrade(map);
36570d94caffSDavid Greenman 		} else {
3658df8bae1dSRodney W. Grimes 			/*
36590d94caffSDavid Greenman 			 * We're attempting to read a copy-on-write page --
36600d94caffSDavid Greenman 			 * don't allow writes.
3661df8bae1dSRodney W. Grimes 			 */
36622d8acc0fSJohn Dyson 			prot &= ~VM_PROT_WRITE;
3663df8bae1dSRodney W. Grimes 		}
3664df8bae1dSRodney W. Grimes 	}
36652d8acc0fSJohn Dyson 
3666df8bae1dSRodney W. Grimes 	/*
3667df8bae1dSRodney W. Grimes 	 * Create an object if necessary.
3668df8bae1dSRodney W. Grimes 	 */
36694e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL &&
36704e71e795SMatthew Dillon 	    !map->system_map) {
367125adb370SBrian Feldman 		if (vm_map_lock_upgrade(map))
3672df8bae1dSRodney W. Grimes 			goto RetryLookup;
367324a1cce3SDavid Greenman 		entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
36743364c323SKonstantin Belousov 		    atop(size));
3675df8bae1dSRodney W. Grimes 		entry->offset = 0;
36763364c323SKonstantin Belousov 		if (entry->uip != NULL) {
36773364c323SKonstantin Belousov 			VM_OBJECT_LOCK(entry->object.vm_object);
36783364c323SKonstantin Belousov 			entry->object.vm_object->uip = entry->uip;
36793364c323SKonstantin Belousov 			entry->object.vm_object->charge = size;
36803364c323SKonstantin Belousov 			VM_OBJECT_UNLOCK(entry->object.vm_object);
36813364c323SKonstantin Belousov 			entry->uip = NULL;
36823364c323SKonstantin Belousov 		}
36839b09b6c7SMatthew Dillon 		vm_map_lock_downgrade(map);
3684df8bae1dSRodney W. Grimes 	}
3685b5b40fa6SJohn Dyson 
3686df8bae1dSRodney W. Grimes 	/*
36870d94caffSDavid Greenman 	 * Return the object/offset from this entry.  If the entry was
36880d94caffSDavid Greenman 	 * copy-on-write or empty, it has been fixed up.
3689df8bae1dSRodney W. Grimes 	 */
36909b09b6c7SMatthew Dillon 	*pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
3691df8bae1dSRodney W. Grimes 	*object = entry->object.vm_object;
3692df8bae1dSRodney W. Grimes 
3693df8bae1dSRodney W. Grimes 	*out_prot = prot;
3694df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
3695df8bae1dSRodney W. Grimes }
3696df8bae1dSRodney W. Grimes 
3697df8bae1dSRodney W. Grimes /*
369819dc5607STor Egge  *	vm_map_lookup_locked:
369919dc5607STor Egge  *
370019dc5607STor Egge  *	Lookup the faulting address.  A version of vm_map_lookup that returns
370119dc5607STor Egge  *      KERN_FAILURE instead of blocking on map lock or memory allocation.
370219dc5607STor Egge  */
370319dc5607STor Egge int
370419dc5607STor Egge vm_map_lookup_locked(vm_map_t *var_map,		/* IN/OUT */
370519dc5607STor Egge 		     vm_offset_t vaddr,
370619dc5607STor Egge 		     vm_prot_t fault_typea,
370719dc5607STor Egge 		     vm_map_entry_t *out_entry,	/* OUT */
370819dc5607STor Egge 		     vm_object_t *object,	/* OUT */
370919dc5607STor Egge 		     vm_pindex_t *pindex,	/* OUT */
371019dc5607STor Egge 		     vm_prot_t *out_prot,	/* OUT */
371119dc5607STor Egge 		     boolean_t *wired)		/* OUT */
371219dc5607STor Egge {
371319dc5607STor Egge 	vm_map_entry_t entry;
371419dc5607STor Egge 	vm_map_t map = *var_map;
371519dc5607STor Egge 	vm_prot_t prot;
371619dc5607STor Egge 	vm_prot_t fault_type = fault_typea;
371719dc5607STor Egge 
371819dc5607STor Egge 	/*
37194c3ef59eSAlan Cox 	 * Lookup the faulting address.
372019dc5607STor Egge 	 */
372119dc5607STor Egge 	if (!vm_map_lookup_entry(map, vaddr, out_entry))
372219dc5607STor Egge 		return (KERN_INVALID_ADDRESS);
372319dc5607STor Egge 
372419dc5607STor Egge 	entry = *out_entry;
372519dc5607STor Egge 
372619dc5607STor Egge 	/*
372719dc5607STor Egge 	 * Fail if the entry refers to a submap.
372819dc5607STor Egge 	 */
372919dc5607STor Egge 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
373019dc5607STor Egge 		return (KERN_FAILURE);
373119dc5607STor Egge 
373219dc5607STor Egge 	/*
373319dc5607STor Egge 	 * Check whether this task is allowed to have this page.
373419dc5607STor Egge 	 */
373519dc5607STor Egge 	prot = entry->protection;
373619dc5607STor Egge 	fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
373719dc5607STor Egge 	if ((fault_type & prot) != fault_type)
373819dc5607STor Egge 		return (KERN_PROTECTION_FAILURE);
373919dc5607STor Egge 	if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
374019dc5607STor Egge 	    (entry->eflags & MAP_ENTRY_COW) &&
3741a6d42a0dSAlan Cox 	    (fault_type & VM_PROT_WRITE))
374219dc5607STor Egge 		return (KERN_PROTECTION_FAILURE);
374319dc5607STor Egge 
374419dc5607STor Egge 	/*
374519dc5607STor Egge 	 * If this page is not pageable, we have to get it for all possible
374619dc5607STor Egge 	 * accesses.
374719dc5607STor Egge 	 */
374819dc5607STor Egge 	*wired = (entry->wired_count != 0);
374919dc5607STor Egge 	if (*wired)
3750a6d42a0dSAlan Cox 		fault_type = entry->protection;
375119dc5607STor Egge 
375219dc5607STor Egge 	if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
375319dc5607STor Egge 		/*
375419dc5607STor Egge 		 * Fail if the entry was copy-on-write for a write fault.
375519dc5607STor Egge 		 */
375619dc5607STor Egge 		if (fault_type & VM_PROT_WRITE)
375719dc5607STor Egge 			return (KERN_FAILURE);
375819dc5607STor Egge 		/*
375919dc5607STor Egge 		 * We're attempting to read a copy-on-write page --
376019dc5607STor Egge 		 * don't allow writes.
376119dc5607STor Egge 		 */
376219dc5607STor Egge 		prot &= ~VM_PROT_WRITE;
376319dc5607STor Egge 	}
376419dc5607STor Egge 
376519dc5607STor Egge 	/*
376619dc5607STor Egge 	 * Fail if an object should be created.
376719dc5607STor Egge 	 */
376819dc5607STor Egge 	if (entry->object.vm_object == NULL && !map->system_map)
376919dc5607STor Egge 		return (KERN_FAILURE);
377019dc5607STor Egge 
377119dc5607STor Egge 	/*
377219dc5607STor Egge 	 * Return the object/offset from this entry.  If the entry was
377319dc5607STor Egge 	 * copy-on-write or empty, it has been fixed up.
377419dc5607STor Egge 	 */
377519dc5607STor Egge 	*pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
377619dc5607STor Egge 	*object = entry->object.vm_object;
377719dc5607STor Egge 
377819dc5607STor Egge 	*out_prot = prot;
377919dc5607STor Egge 	return (KERN_SUCCESS);
378019dc5607STor Egge }
378119dc5607STor Egge 
378219dc5607STor Egge /*
3783df8bae1dSRodney W. Grimes  *	vm_map_lookup_done:
3784df8bae1dSRodney W. Grimes  *
3785df8bae1dSRodney W. Grimes  *	Releases locks acquired by a vm_map_lookup
3786df8bae1dSRodney W. Grimes  *	(according to the handle returned by that lookup).
3787df8bae1dSRodney W. Grimes  */
37880d94caffSDavid Greenman void
37891b40f8c0SMatthew Dillon vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry)
3790df8bae1dSRodney W. Grimes {
3791df8bae1dSRodney W. Grimes 	/*
3792df8bae1dSRodney W. Grimes 	 * Unlock the main-level map
3793df8bae1dSRodney W. Grimes 	 */
3794df8bae1dSRodney W. Grimes 	vm_map_unlock_read(map);
3795df8bae1dSRodney W. Grimes }
3796df8bae1dSRodney W. Grimes 
3797c7c34a24SBruce Evans #include "opt_ddb.h"
3798c3cb3e12SDavid Greenman #ifdef DDB
3799c7c34a24SBruce Evans #include <sys/kernel.h>
3800c7c34a24SBruce Evans 
3801c7c34a24SBruce Evans #include <ddb/ddb.h>
3802c7c34a24SBruce Evans 
3803df8bae1dSRodney W. Grimes /*
3804df8bae1dSRodney W. Grimes  *	vm_map_print:	[ debug ]
3805df8bae1dSRodney W. Grimes  */
3806c7c34a24SBruce Evans DB_SHOW_COMMAND(map, vm_map_print)
3807df8bae1dSRodney W. Grimes {
380895e5e988SJohn Dyson 	static int nlines;
3809c7c34a24SBruce Evans 	/* XXX convert args. */
3810c0877f10SJohn Dyson 	vm_map_t map = (vm_map_t)addr;
3811c7c34a24SBruce Evans 	boolean_t full = have_addr;
3812df8bae1dSRodney W. Grimes 
3813c0877f10SJohn Dyson 	vm_map_entry_t entry;
3814c7c34a24SBruce Evans 
3815e5f251d2SAlan Cox 	db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
3816e5f251d2SAlan Cox 	    (void *)map,
3817101eeb7fSBruce Evans 	    (void *)map->pmap, map->nentries, map->timestamp);
381895e5e988SJohn Dyson 	nlines++;
3819df8bae1dSRodney W. Grimes 
3820c7c34a24SBruce Evans 	if (!full && db_indent)
3821df8bae1dSRodney W. Grimes 		return;
3822df8bae1dSRodney W. Grimes 
3823c7c34a24SBruce Evans 	db_indent += 2;
3824df8bae1dSRodney W. Grimes 	for (entry = map->header.next; entry != &map->header;
3825df8bae1dSRodney W. Grimes 	    entry = entry->next) {
3826fc62ef1fSBruce Evans 		db_iprintf("map entry %p: start=%p, end=%p\n",
3827fc62ef1fSBruce Evans 		    (void *)entry, (void *)entry->start, (void *)entry->end);
382895e5e988SJohn Dyson 		nlines++;
3829e5f251d2SAlan Cox 		{
3830df8bae1dSRodney W. Grimes 			static char *inheritance_name[4] =
3831df8bae1dSRodney W. Grimes 			{"share", "copy", "none", "donate_copy"};
38320d94caffSDavid Greenman 
383395e5e988SJohn Dyson 			db_iprintf(" prot=%x/%x/%s",
3834df8bae1dSRodney W. Grimes 			    entry->protection,
3835df8bae1dSRodney W. Grimes 			    entry->max_protection,
38368aef1712SMatthew Dillon 			    inheritance_name[(int)(unsigned char)entry->inheritance]);
3837df8bae1dSRodney W. Grimes 			if (entry->wired_count != 0)
383895e5e988SJohn Dyson 				db_printf(", wired");
3839df8bae1dSRodney W. Grimes 		}
38409fdfe602SMatthew Dillon 		if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3841cd034a5bSMaxime Henrion 			db_printf(", share=%p, offset=0x%jx\n",
38429fdfe602SMatthew Dillon 			    (void *)entry->object.sub_map,
3843cd034a5bSMaxime Henrion 			    (uintmax_t)entry->offset);
384495e5e988SJohn Dyson 			nlines++;
3845df8bae1dSRodney W. Grimes 			if ((entry->prev == &map->header) ||
38469fdfe602SMatthew Dillon 			    (entry->prev->object.sub_map !=
38479fdfe602SMatthew Dillon 				entry->object.sub_map)) {
3848c7c34a24SBruce Evans 				db_indent += 2;
3849101eeb7fSBruce Evans 				vm_map_print((db_expr_t)(intptr_t)
38509fdfe602SMatthew Dillon 					     entry->object.sub_map,
3851914181e7SBruce Evans 					     full, 0, (char *)0);
3852c7c34a24SBruce Evans 				db_indent -= 2;
3853df8bae1dSRodney W. Grimes 			}
38540d94caffSDavid Greenman 		} else {
38553364c323SKonstantin Belousov 			if (entry->uip != NULL)
38563364c323SKonstantin Belousov 				db_printf(", uip %d", entry->uip->ui_uid);
3857cd034a5bSMaxime Henrion 			db_printf(", object=%p, offset=0x%jx",
3858101eeb7fSBruce Evans 			    (void *)entry->object.vm_object,
3859cd034a5bSMaxime Henrion 			    (uintmax_t)entry->offset);
38603364c323SKonstantin Belousov 			if (entry->object.vm_object && entry->object.vm_object->uip)
38613364c323SKonstantin Belousov 				db_printf(", obj uip %d charge %jx",
38623364c323SKonstantin Belousov 				    entry->object.vm_object->uip->ui_uid,
38633364c323SKonstantin Belousov 				    (uintmax_t)entry->object.vm_object->charge);
3864afa07f7eSJohn Dyson 			if (entry->eflags & MAP_ENTRY_COW)
3865c7c34a24SBruce Evans 				db_printf(", copy (%s)",
3866afa07f7eSJohn Dyson 				    (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
3867c7c34a24SBruce Evans 			db_printf("\n");
386895e5e988SJohn Dyson 			nlines++;
3869df8bae1dSRodney W. Grimes 
3870df8bae1dSRodney W. Grimes 			if ((entry->prev == &map->header) ||
3871df8bae1dSRodney W. Grimes 			    (entry->prev->object.vm_object !=
3872df8bae1dSRodney W. Grimes 				entry->object.vm_object)) {
3873c7c34a24SBruce Evans 				db_indent += 2;
3874101eeb7fSBruce Evans 				vm_object_print((db_expr_t)(intptr_t)
3875101eeb7fSBruce Evans 						entry->object.vm_object,
3876914181e7SBruce Evans 						full, 0, (char *)0);
387795e5e988SJohn Dyson 				nlines += 4;
3878c7c34a24SBruce Evans 				db_indent -= 2;
3879df8bae1dSRodney W. Grimes 			}
3880df8bae1dSRodney W. Grimes 		}
3881df8bae1dSRodney W. Grimes 	}
3882c7c34a24SBruce Evans 	db_indent -= 2;
388395e5e988SJohn Dyson 	if (db_indent == 0)
388495e5e988SJohn Dyson 		nlines = 0;
3885df8bae1dSRodney W. Grimes }
388695e5e988SJohn Dyson 
388795e5e988SJohn Dyson 
388895e5e988SJohn Dyson DB_SHOW_COMMAND(procvm, procvm)
388995e5e988SJohn Dyson {
389095e5e988SJohn Dyson 	struct proc *p;
389195e5e988SJohn Dyson 
389295e5e988SJohn Dyson 	if (have_addr) {
389395e5e988SJohn Dyson 		p = (struct proc *) addr;
389495e5e988SJohn Dyson 	} else {
389595e5e988SJohn Dyson 		p = curproc;
389695e5e988SJohn Dyson 	}
389795e5e988SJohn Dyson 
3898ac1e407bSBruce Evans 	db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
3899ac1e407bSBruce Evans 	    (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
3900b1028ad1SLuoqi Chen 	    (void *)vmspace_pmap(p->p_vmspace));
390195e5e988SJohn Dyson 
3902101eeb7fSBruce Evans 	vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);
390395e5e988SJohn Dyson }
390495e5e988SJohn Dyson 
3905c7c34a24SBruce Evans #endif /* DDB */
3906