xref: /freebsd/sys/vm/vm_map.c (revision 9b55fc042996c9c9ed8fab6e0eea9239937919b7)
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);
131655c3490SKonstantin Belousov static void vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry);
1328355f576SJeff Roberson #ifdef INVARIANTS
1338355f576SJeff Roberson static void vm_map_zdtor(void *mem, int size, void *arg);
1348355f576SJeff Roberson static void vmspace_zdtor(void *mem, int size, void *arg);
1358355f576SJeff Roberson #endif
136b18bfc3dSJohn Dyson 
1373364c323SKonstantin Belousov #define	ENTRY_CHARGED(e) ((e)->uip != NULL || \
1383364c323SKonstantin Belousov     ((e)->object.vm_object != NULL && (e)->object.vm_object->uip != NULL && \
1393364c323SKonstantin Belousov      !((e)->eflags & MAP_ENTRY_NEEDS_COPY)))
1403364c323SKonstantin Belousov 
14157051fdcSTor Egge /*
14257051fdcSTor Egge  * PROC_VMSPACE_{UN,}LOCK() can be a noop as long as vmspaces are type
14357051fdcSTor Egge  * stable.
14457051fdcSTor Egge  */
14557051fdcSTor Egge #define PROC_VMSPACE_LOCK(p) do { } while (0)
14657051fdcSTor Egge #define PROC_VMSPACE_UNLOCK(p) do { } while (0)
14757051fdcSTor Egge 
148d239bd3cSKonstantin Belousov /*
149d239bd3cSKonstantin Belousov  *	VM_MAP_RANGE_CHECK:	[ internal use only ]
150d239bd3cSKonstantin Belousov  *
151d239bd3cSKonstantin Belousov  *	Asserts that the starting and ending region
152d239bd3cSKonstantin Belousov  *	addresses fall within the valid range of the map.
153d239bd3cSKonstantin Belousov  */
154d239bd3cSKonstantin Belousov #define	VM_MAP_RANGE_CHECK(map, start, end)		\
155d239bd3cSKonstantin Belousov 		{					\
156d239bd3cSKonstantin Belousov 		if (start < vm_map_min(map))		\
157d239bd3cSKonstantin Belousov 			start = vm_map_min(map);	\
158d239bd3cSKonstantin Belousov 		if (end > vm_map_max(map))		\
159d239bd3cSKonstantin Belousov 			end = vm_map_max(map);		\
160d239bd3cSKonstantin Belousov 		if (start > end)			\
161d239bd3cSKonstantin Belousov 			start = end;			\
162d239bd3cSKonstantin Belousov 		}
163d239bd3cSKonstantin Belousov 
1646fecb26bSKonstantin Belousov /*
1656fecb26bSKonstantin Belousov  *	vm_map_startup:
1666fecb26bSKonstantin Belousov  *
1676fecb26bSKonstantin Belousov  *	Initialize the vm_map module.  Must be called before
1686fecb26bSKonstantin Belousov  *	any other vm_map routines.
1696fecb26bSKonstantin Belousov  *
1706fecb26bSKonstantin Belousov  *	Map and entry structures are allocated from the general
1716fecb26bSKonstantin Belousov  *	purpose memory pool with some exceptions:
1726fecb26bSKonstantin Belousov  *
1736fecb26bSKonstantin Belousov  *	- The kernel map and kmem submap are allocated statically.
1746fecb26bSKonstantin Belousov  *	- Kernel map entries are allocated out of a static pool.
1756fecb26bSKonstantin Belousov  *
1766fecb26bSKonstantin Belousov  *	These restrictions are necessary since malloc() uses the
1776fecb26bSKonstantin Belousov  *	maps and requires map entries.
1786fecb26bSKonstantin Belousov  */
1796fecb26bSKonstantin Belousov 
1800d94caffSDavid Greenman void
1811b40f8c0SMatthew Dillon vm_map_startup(void)
182df8bae1dSRodney W. Grimes {
1833a92e5d5SAlan Cox 	mtx_init(&map_sleep_mtx, "vm map sleep mutex", NULL, MTX_DEF);
1848355f576SJeff Roberson 	mapzone = uma_zcreate("MAP", sizeof(struct vm_map), NULL,
1858355f576SJeff Roberson #ifdef INVARIANTS
1868355f576SJeff Roberson 	    vm_map_zdtor,
1878355f576SJeff Roberson #else
1888355f576SJeff Roberson 	    NULL,
1898355f576SJeff Roberson #endif
1908355f576SJeff Roberson 	    vm_map_zinit, vm_map_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
1918355f576SJeff Roberson 	uma_prealloc(mapzone, MAX_KMAP);
192670d17b5SJeff Roberson 	kmapentzone = uma_zcreate("KMAP ENTRY", sizeof(struct vm_map_entry),
19318aa2de5SJeff Roberson 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
19418aa2de5SJeff Roberson 	    UMA_ZONE_MTXCLASS | UMA_ZONE_VM);
195670d17b5SJeff Roberson 	uma_prealloc(kmapentzone, MAX_KMAPENT);
196670d17b5SJeff Roberson 	mapentzone = uma_zcreate("MAP ENTRY", sizeof(struct vm_map_entry),
197670d17b5SJeff Roberson 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
198df8bae1dSRodney W. Grimes }
199df8bae1dSRodney W. Grimes 
2008355f576SJeff Roberson static void
2018355f576SJeff Roberson vmspace_zfini(void *mem, int size)
2028355f576SJeff Roberson {
2038355f576SJeff Roberson 	struct vmspace *vm;
2048355f576SJeff Roberson 
2058355f576SJeff Roberson 	vm = (struct vmspace *)mem;
2068355f576SJeff Roberson 	vm_map_zfini(&vm->vm_map, sizeof(vm->vm_map));
2078355f576SJeff Roberson }
2088355f576SJeff Roberson 
209b23f72e9SBrian Feldman static int
210b23f72e9SBrian Feldman vmspace_zinit(void *mem, int size, int flags)
2118355f576SJeff Roberson {
2128355f576SJeff Roberson 	struct vmspace *vm;
2138355f576SJeff Roberson 
2148355f576SJeff Roberson 	vm = (struct vmspace *)mem;
2158355f576SJeff Roberson 
21689b57fcfSKonstantin Belousov 	vm->vm_map.pmap = NULL;
217b23f72e9SBrian Feldman 	(void)vm_map_zinit(&vm->vm_map, sizeof(vm->vm_map), flags);
218b23f72e9SBrian Feldman 	return (0);
2198355f576SJeff Roberson }
2208355f576SJeff Roberson 
2218355f576SJeff Roberson static void
2228355f576SJeff Roberson vm_map_zfini(void *mem, int size)
2238355f576SJeff Roberson {
2248355f576SJeff Roberson 	vm_map_t map;
2258355f576SJeff Roberson 
2268355f576SJeff Roberson 	map = (vm_map_t)mem;
22736daaecdSAlan Cox 	mtx_destroy(&map->system_mtx);
22812c64974SMaxime Henrion 	sx_destroy(&map->lock);
2298355f576SJeff Roberson }
2308355f576SJeff Roberson 
231b23f72e9SBrian Feldman static int
232b23f72e9SBrian Feldman vm_map_zinit(void *mem, int size, int flags)
2338355f576SJeff Roberson {
2348355f576SJeff Roberson 	vm_map_t map;
2358355f576SJeff Roberson 
2368355f576SJeff Roberson 	map = (vm_map_t)mem;
2378355f576SJeff Roberson 	map->nentries = 0;
2388355f576SJeff Roberson 	map->size = 0;
239d923c598SAlan Cox 	mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
24012c64974SMaxime Henrion 	sx_init(&map->lock, "user map");
241b23f72e9SBrian Feldman 	return (0);
2428355f576SJeff Roberson }
2438355f576SJeff Roberson 
2448355f576SJeff Roberson #ifdef INVARIANTS
2458355f576SJeff Roberson static void
2468355f576SJeff Roberson vmspace_zdtor(void *mem, int size, void *arg)
2478355f576SJeff Roberson {
2488355f576SJeff Roberson 	struct vmspace *vm;
2498355f576SJeff Roberson 
2508355f576SJeff Roberson 	vm = (struct vmspace *)mem;
2518355f576SJeff Roberson 
2528355f576SJeff Roberson 	vm_map_zdtor(&vm->vm_map, sizeof(vm->vm_map), arg);
2538355f576SJeff Roberson }
2548355f576SJeff Roberson static void
2558355f576SJeff Roberson vm_map_zdtor(void *mem, int size, void *arg)
2568355f576SJeff Roberson {
2578355f576SJeff Roberson 	vm_map_t map;
2588355f576SJeff Roberson 
2598355f576SJeff Roberson 	map = (vm_map_t)mem;
2608355f576SJeff Roberson 	KASSERT(map->nentries == 0,
2618355f576SJeff Roberson 	    ("map %p nentries == %d on free.",
2628355f576SJeff Roberson 	    map, map->nentries));
2638355f576SJeff Roberson 	KASSERT(map->size == 0,
2648355f576SJeff Roberson 	    ("map %p size == %lu on free.",
2659eb6e519SJeff Roberson 	    map, (unsigned long)map->size));
2668355f576SJeff Roberson }
2678355f576SJeff Roberson #endif	/* INVARIANTS */
2688355f576SJeff Roberson 
269df8bae1dSRodney W. Grimes /*
270df8bae1dSRodney W. Grimes  * Allocate a vmspace structure, including a vm_map and pmap,
271df8bae1dSRodney W. Grimes  * and initialize those structures.  The refcnt is set to 1.
272df8bae1dSRodney W. Grimes  */
273df8bae1dSRodney W. Grimes struct vmspace *
2742d8acc0fSJohn Dyson vmspace_alloc(min, max)
275df8bae1dSRodney W. Grimes 	vm_offset_t min, max;
276df8bae1dSRodney W. Grimes {
277c0877f10SJohn Dyson 	struct vmspace *vm;
2780d94caffSDavid Greenman 
279a163d034SWarner Losh 	vm = uma_zalloc(vmspace_zone, M_WAITOK);
28089b57fcfSKonstantin Belousov 	if (vm->vm_map.pmap == NULL && !pmap_pinit(vmspace_pmap(vm))) {
28189b57fcfSKonstantin Belousov 		uma_zfree(vmspace_zone, vm);
28289b57fcfSKonstantin Belousov 		return (NULL);
28389b57fcfSKonstantin Belousov 	}
28421c641b2SJohn Baldwin 	CTR1(KTR_VM, "vmspace_alloc: %p", vm);
28592351f16SAlan Cox 	_vm_map_init(&vm->vm_map, vmspace_pmap(vm), min, max);
286df8bae1dSRodney W. Grimes 	vm->vm_refcnt = 1;
2872d8acc0fSJohn Dyson 	vm->vm_shm = NULL;
28851ab6c28SAlan Cox 	vm->vm_swrss = 0;
28951ab6c28SAlan Cox 	vm->vm_tsize = 0;
29051ab6c28SAlan Cox 	vm->vm_dsize = 0;
29151ab6c28SAlan Cox 	vm->vm_ssize = 0;
29251ab6c28SAlan Cox 	vm->vm_taddr = 0;
29351ab6c28SAlan Cox 	vm->vm_daddr = 0;
29451ab6c28SAlan Cox 	vm->vm_maxsaddr = 0;
295df8bae1dSRodney W. Grimes 	return (vm);
296df8bae1dSRodney W. Grimes }
297df8bae1dSRodney W. Grimes 
298df8bae1dSRodney W. Grimes void
2991b40f8c0SMatthew Dillon vm_init2(void)
3001b40f8c0SMatthew Dillon {
3012feb50bfSAttilio Rao 	uma_zone_set_obj(kmapentzone, &kmapentobj, lmin(cnt.v_page_count,
302c1f02198SAlan Cox 	    (VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS) / PAGE_SIZE) / 8 +
3033fde38dfSMike Silbersack 	     maxproc * 2 + maxfiles);
3048355f576SJeff Roberson 	vmspace_zone = uma_zcreate("VMSPACE", sizeof(struct vmspace), NULL,
3058355f576SJeff Roberson #ifdef INVARIANTS
3068355f576SJeff Roberson 	    vmspace_zdtor,
3078355f576SJeff Roberson #else
3088355f576SJeff Roberson 	    NULL,
3098355f576SJeff Roberson #endif
3108355f576SJeff Roberson 	    vmspace_zinit, vmspace_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
3113075778bSJohn Dyson }
3123075778bSJohn Dyson 
31362a59e8fSWarner Losh static inline void
314582ec34cSAlfred Perlstein vmspace_dofree(struct vmspace *vm)
315df8bae1dSRodney W. Grimes {
3160ef12795SAlan Cox 
31721c641b2SJohn Baldwin 	CTR1(KTR_VM, "vmspace_free: %p", vm);
3183db161e0SMatthew Dillon 
3193db161e0SMatthew Dillon 	/*
3203db161e0SMatthew Dillon 	 * Make sure any SysV shm is freed, it might not have been in
3213db161e0SMatthew Dillon 	 * exit1().
3223db161e0SMatthew Dillon 	 */
3233db161e0SMatthew Dillon 	shmexit(vm);
3243db161e0SMatthew Dillon 
32530dcfc09SJohn Dyson 	/*
326df8bae1dSRodney W. Grimes 	 * Lock the map, to wait out all other references to it.
3270d94caffSDavid Greenman 	 * Delete all of the mappings and pages they hold, then call
3280d94caffSDavid Greenman 	 * the pmap module to reclaim anything left.
329df8bae1dSRodney W. Grimes 	 */
330717f7d59SAlan Cox 	(void)vm_map_remove(&vm->vm_map, vm->vm_map.min_offset,
331df8bae1dSRodney W. Grimes 	    vm->vm_map.max_offset);
3328355f576SJeff Roberson 
3330ef12795SAlan Cox 	pmap_release(vmspace_pmap(vm));
3340ef12795SAlan Cox 	vm->vm_map.pmap = NULL;
3358355f576SJeff Roberson 	uma_zfree(vmspace_zone, vm);
336df8bae1dSRodney W. Grimes }
337582ec34cSAlfred Perlstein 
338582ec34cSAlfred Perlstein void
339582ec34cSAlfred Perlstein vmspace_free(struct vmspace *vm)
340582ec34cSAlfred Perlstein {
3411a276a3fSAlan Cox 	int refcnt;
342582ec34cSAlfred Perlstein 
343582ec34cSAlfred Perlstein 	if (vm->vm_refcnt == 0)
344582ec34cSAlfred Perlstein 		panic("vmspace_free: attempt to free already freed vmspace");
345582ec34cSAlfred Perlstein 
3461a276a3fSAlan Cox 	do
3471a276a3fSAlan Cox 		refcnt = vm->vm_refcnt;
3481a276a3fSAlan Cox 	while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1));
34957051fdcSTor Egge 	if (refcnt == 1)
350582ec34cSAlfred Perlstein 		vmspace_dofree(vm);
351582ec34cSAlfred Perlstein }
352582ec34cSAlfred Perlstein 
353582ec34cSAlfred Perlstein void
354582ec34cSAlfred Perlstein vmspace_exitfree(struct proc *p)
355582ec34cSAlfred Perlstein {
356334f7061SPeter Wemm 	struct vmspace *vm;
357582ec34cSAlfred Perlstein 
35857051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
359334f7061SPeter Wemm 	vm = p->p_vmspace;
360334f7061SPeter Wemm 	p->p_vmspace = NULL;
36157051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
36257051fdcSTor Egge 	KASSERT(vm == &vmspace0, ("vmspace_exitfree: wrong vmspace"));
36357051fdcSTor Egge 	vmspace_free(vm);
36457051fdcSTor Egge }
36557051fdcSTor Egge 
36657051fdcSTor Egge void
36757051fdcSTor Egge vmspace_exit(struct thread *td)
36857051fdcSTor Egge {
36957051fdcSTor Egge 	int refcnt;
37057051fdcSTor Egge 	struct vmspace *vm;
37157051fdcSTor Egge 	struct proc *p;
372389d2b6eSMatthew Dillon 
373389d2b6eSMatthew Dillon 	/*
37457051fdcSTor Egge 	 * Release user portion of address space.
37557051fdcSTor Egge 	 * This releases references to vnodes,
37657051fdcSTor Egge 	 * which could cause I/O if the file has been unlinked.
37757051fdcSTor Egge 	 * Need to do this early enough that we can still sleep.
378389d2b6eSMatthew Dillon 	 *
37957051fdcSTor Egge 	 * The last exiting process to reach this point releases as
38057051fdcSTor Egge 	 * much of the environment as it can. vmspace_dofree() is the
38157051fdcSTor Egge 	 * slower fallback in case another process had a temporary
38257051fdcSTor Egge 	 * reference to the vmspace.
383389d2b6eSMatthew Dillon 	 */
38457051fdcSTor Egge 
38557051fdcSTor Egge 	p = td->td_proc;
38657051fdcSTor Egge 	vm = p->p_vmspace;
38757051fdcSTor Egge 	atomic_add_int(&vmspace0.vm_refcnt, 1);
38857051fdcSTor Egge 	do {
38957051fdcSTor Egge 		refcnt = vm->vm_refcnt;
39057051fdcSTor Egge 		if (refcnt > 1 && p->p_vmspace != &vmspace0) {
39157051fdcSTor Egge 			/* Switch now since other proc might free vmspace */
39257051fdcSTor Egge 			PROC_VMSPACE_LOCK(p);
39357051fdcSTor Egge 			p->p_vmspace = &vmspace0;
39457051fdcSTor Egge 			PROC_VMSPACE_UNLOCK(p);
39557051fdcSTor Egge 			pmap_activate(td);
39657051fdcSTor Egge 		}
39757051fdcSTor Egge 	} while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1));
39857051fdcSTor Egge 	if (refcnt == 1) {
39957051fdcSTor Egge 		if (p->p_vmspace != vm) {
40057051fdcSTor Egge 			/* vmspace not yet freed, switch back */
40157051fdcSTor Egge 			PROC_VMSPACE_LOCK(p);
40257051fdcSTor Egge 			p->p_vmspace = vm;
40357051fdcSTor Egge 			PROC_VMSPACE_UNLOCK(p);
40457051fdcSTor Egge 			pmap_activate(td);
40557051fdcSTor Egge 		}
40657051fdcSTor Egge 		pmap_remove_pages(vmspace_pmap(vm));
40757051fdcSTor Egge 		/* Switch now since this proc will free vmspace */
40857051fdcSTor Egge 		PROC_VMSPACE_LOCK(p);
40957051fdcSTor Egge 		p->p_vmspace = &vmspace0;
41057051fdcSTor Egge 		PROC_VMSPACE_UNLOCK(p);
41157051fdcSTor Egge 		pmap_activate(td);
412334f7061SPeter Wemm 		vmspace_dofree(vm);
413334f7061SPeter Wemm 	}
41457051fdcSTor Egge }
41557051fdcSTor Egge 
41657051fdcSTor Egge /* Acquire reference to vmspace owned by another process. */
41757051fdcSTor Egge 
41857051fdcSTor Egge struct vmspace *
41957051fdcSTor Egge vmspace_acquire_ref(struct proc *p)
42057051fdcSTor Egge {
42157051fdcSTor Egge 	struct vmspace *vm;
42257051fdcSTor Egge 	int refcnt;
42357051fdcSTor Egge 
42457051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
42557051fdcSTor Egge 	vm = p->p_vmspace;
42657051fdcSTor Egge 	if (vm == NULL) {
42757051fdcSTor Egge 		PROC_VMSPACE_UNLOCK(p);
42857051fdcSTor Egge 		return (NULL);
42957051fdcSTor Egge 	}
43057051fdcSTor Egge 	do {
43157051fdcSTor Egge 		refcnt = vm->vm_refcnt;
43257051fdcSTor Egge 		if (refcnt <= 0) { 	/* Avoid 0->1 transition */
43357051fdcSTor Egge 			PROC_VMSPACE_UNLOCK(p);
43457051fdcSTor Egge 			return (NULL);
43557051fdcSTor Egge 		}
43657051fdcSTor Egge 	} while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt + 1));
43757051fdcSTor Egge 	if (vm != p->p_vmspace) {
43857051fdcSTor Egge 		PROC_VMSPACE_UNLOCK(p);
43957051fdcSTor Egge 		vmspace_free(vm);
44057051fdcSTor Egge 		return (NULL);
44157051fdcSTor Egge 	}
44257051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
44357051fdcSTor Egge 	return (vm);
44457051fdcSTor Egge }
445df8bae1dSRodney W. Grimes 
4461b40f8c0SMatthew Dillon void
447780b1c09SAlan Cox _vm_map_lock(vm_map_t map, const char *file, int line)
4481b40f8c0SMatthew Dillon {
449bc91c510SAlan Cox 
45093bc4879SAlan Cox 	if (map->system_map)
45136daaecdSAlan Cox 		_mtx_lock_flags(&map->system_mtx, 0, file, line);
45212c64974SMaxime Henrion 	else
453f9819486SAttilio Rao 		(void)_sx_xlock(&map->lock, 0, file, line);
4541b40f8c0SMatthew Dillon 	map->timestamp++;
4551b40f8c0SMatthew Dillon }
4561b40f8c0SMatthew Dillon 
4571b40f8c0SMatthew Dillon void
458780b1c09SAlan Cox _vm_map_unlock(vm_map_t map, const char *file, int line)
4590e0af8ecSBrian Feldman {
460655c3490SKonstantin Belousov 	vm_map_entry_t free_entry, entry;
461655c3490SKonstantin Belousov 	vm_object_t object;
462655c3490SKonstantin Belousov 
463655c3490SKonstantin Belousov 	free_entry = map->deferred_freelist;
464655c3490SKonstantin Belousov 	map->deferred_freelist = NULL;
465bc91c510SAlan Cox 
46636daaecdSAlan Cox 	if (map->system_map)
46736daaecdSAlan Cox 		_mtx_unlock_flags(&map->system_mtx, 0, file, line);
46836daaecdSAlan Cox 	else
46912c64974SMaxime Henrion 		_sx_xunlock(&map->lock, file, line);
470655c3490SKonstantin Belousov 
471655c3490SKonstantin Belousov 	while (free_entry != NULL) {
472655c3490SKonstantin Belousov 		entry = free_entry;
473655c3490SKonstantin Belousov 		free_entry = free_entry->next;
474655c3490SKonstantin Belousov 
475655c3490SKonstantin Belousov 		if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
476655c3490SKonstantin Belousov 			object = entry->object.vm_object;
477655c3490SKonstantin Belousov 			vm_object_deallocate(object);
478655c3490SKonstantin Belousov 		}
479655c3490SKonstantin Belousov 
480655c3490SKonstantin Belousov 		vm_map_entry_dispose(map, entry);
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);
50036daaecdSAlan Cox 	else
50105a8c414SAlan Cox 		_sx_sunlock(&map->lock, file, line);
50225adb370SBrian Feldman }
50325adb370SBrian Feldman 
504d974f03cSAlan Cox int
505780b1c09SAlan Cox _vm_map_trylock(vm_map_t map, const char *file, int line)
506d974f03cSAlan Cox {
50725adb370SBrian Feldman 	int error;
50825adb370SBrian Feldman 
50936daaecdSAlan Cox 	error = map->system_map ?
51036daaecdSAlan Cox 	    !_mtx_trylock(&map->system_mtx, 0, file, line) :
51112c64974SMaxime Henrion 	    !_sx_try_xlock(&map->lock, file, line);
5123a92e5d5SAlan Cox 	if (error == 0)
5133a92e5d5SAlan Cox 		map->timestamp++;
514bc91c510SAlan Cox 	return (error == 0);
5150e0af8ecSBrian Feldman }
5160e0af8ecSBrian Feldman 
5170e0af8ecSBrian Feldman int
51872d97679SDavid Schultz _vm_map_trylock_read(vm_map_t map, const char *file, int line)
51972d97679SDavid Schultz {
52072d97679SDavid Schultz 	int error;
52172d97679SDavid Schultz 
52272d97679SDavid Schultz 	error = map->system_map ?
52372d97679SDavid Schultz 	    !_mtx_trylock(&map->system_mtx, 0, file, line) :
52405a8c414SAlan Cox 	    !_sx_try_slock(&map->lock, file, line);
52572d97679SDavid Schultz 	return (error == 0);
52672d97679SDavid Schultz }
52772d97679SDavid Schultz 
52805a8c414SAlan Cox /*
52905a8c414SAlan Cox  *	_vm_map_lock_upgrade:	[ internal use only ]
53005a8c414SAlan Cox  *
53105a8c414SAlan Cox  *	Tries to upgrade a read (shared) lock on the specified map to a write
53205a8c414SAlan Cox  *	(exclusive) lock.  Returns the value "0" if the upgrade succeeds and a
53305a8c414SAlan Cox  *	non-zero value if the upgrade fails.  If the upgrade fails, the map is
53405a8c414SAlan Cox  *	returned without a read or write lock held.
53505a8c414SAlan Cox  *
53605a8c414SAlan Cox  *	Requires that the map be read locked.
53705a8c414SAlan Cox  */
53872d97679SDavid Schultz int
539780b1c09SAlan Cox _vm_map_lock_upgrade(vm_map_t map, const char *file, int line)
5400e0af8ecSBrian Feldman {
54105a8c414SAlan Cox 	unsigned int last_timestamp;
542bc91c510SAlan Cox 
54312c64974SMaxime Henrion 	if (map->system_map) {
54405a8c414SAlan Cox #ifdef INVARIANTS
54536daaecdSAlan Cox 		_mtx_assert(&map->system_mtx, MA_OWNED, file, line);
54612c64974SMaxime Henrion #endif
54705a8c414SAlan Cox 	} else {
54805a8c414SAlan Cox 		if (!_sx_try_upgrade(&map->lock, file, line)) {
54905a8c414SAlan Cox 			last_timestamp = map->timestamp;
55005a8c414SAlan Cox 			_sx_sunlock(&map->lock, file, line);
55105a8c414SAlan Cox 			/*
55205a8c414SAlan Cox 			 * If the map's timestamp does not change while the
55305a8c414SAlan Cox 			 * map is unlocked, then the upgrade succeeds.
55405a8c414SAlan Cox 			 */
55505a8c414SAlan Cox 			(void)_sx_xlock(&map->lock, 0, file, line);
55605a8c414SAlan Cox 			if (last_timestamp != map->timestamp) {
55705a8c414SAlan Cox 				_sx_xunlock(&map->lock, file, line);
55805a8c414SAlan Cox 				return (1);
55905a8c414SAlan Cox 			}
56005a8c414SAlan Cox 		}
56105a8c414SAlan Cox 	}
562bc91c510SAlan Cox 	map->timestamp++;
563bc91c510SAlan Cox 	return (0);
5640e0af8ecSBrian Feldman }
5650e0af8ecSBrian Feldman 
5660e0af8ecSBrian Feldman void
567780b1c09SAlan Cox _vm_map_lock_downgrade(vm_map_t map, const char *file, int line)
5681b40f8c0SMatthew Dillon {
569bc91c510SAlan Cox 
57012c64974SMaxime Henrion 	if (map->system_map) {
57105a8c414SAlan Cox #ifdef INVARIANTS
57236daaecdSAlan Cox 		_mtx_assert(&map->system_mtx, MA_OWNED, file, line);
57312c64974SMaxime Henrion #endif
57405a8c414SAlan Cox 	} else
57505a8c414SAlan Cox 		_sx_downgrade(&map->lock, file, line);
57605a8c414SAlan Cox }
57705a8c414SAlan Cox 
57805a8c414SAlan Cox /*
57905a8c414SAlan Cox  *	vm_map_locked:
58005a8c414SAlan Cox  *
58105a8c414SAlan Cox  *	Returns a non-zero value if the caller holds a write (exclusive) lock
58205a8c414SAlan Cox  *	on the specified map and the value "0" otherwise.
58305a8c414SAlan Cox  */
58405a8c414SAlan Cox int
58505a8c414SAlan Cox vm_map_locked(vm_map_t map)
58605a8c414SAlan Cox {
58705a8c414SAlan Cox 
58805a8c414SAlan Cox 	if (map->system_map)
58905a8c414SAlan Cox 		return (mtx_owned(&map->system_mtx));
59005a8c414SAlan Cox 	else
59105a8c414SAlan Cox 		return (sx_xlocked(&map->lock));
59225adb370SBrian Feldman }
59325adb370SBrian Feldman 
5943a0916b8SKonstantin Belousov #ifdef INVARIANTS
5953a0916b8SKonstantin Belousov static void
5963a0916b8SKonstantin Belousov _vm_map_assert_locked(vm_map_t map, const char *file, int line)
5973a0916b8SKonstantin Belousov {
5983a0916b8SKonstantin Belousov 
5993a0916b8SKonstantin Belousov 	if (map->system_map)
6003a0916b8SKonstantin Belousov 		_mtx_assert(&map->system_mtx, MA_OWNED, file, line);
6013a0916b8SKonstantin Belousov 	else
6023a0916b8SKonstantin Belousov 		_sx_assert(&map->lock, SA_XLOCKED, file, line);
6033a0916b8SKonstantin Belousov }
6043a0916b8SKonstantin Belousov 
6053a0916b8SKonstantin Belousov #if 0
6063a0916b8SKonstantin Belousov static void
6073a0916b8SKonstantin Belousov _vm_map_assert_locked_read(vm_map_t map, const char *file, int line)
6083a0916b8SKonstantin Belousov {
6093a0916b8SKonstantin Belousov 
6103a0916b8SKonstantin Belousov 	if (map->system_map)
6113a0916b8SKonstantin Belousov 		_mtx_assert(&map->system_mtx, MA_OWNED, file, line);
6123a0916b8SKonstantin Belousov 	else
6133a0916b8SKonstantin Belousov 		_sx_assert(&map->lock, SA_SLOCKED, file, line);
6143a0916b8SKonstantin Belousov }
6153a0916b8SKonstantin Belousov #endif
6163a0916b8SKonstantin Belousov 
6173a0916b8SKonstantin Belousov #define	VM_MAP_ASSERT_LOCKED(map) \
6183a0916b8SKonstantin Belousov     _vm_map_assert_locked(map, LOCK_FILE, LOCK_LINE)
6193a0916b8SKonstantin Belousov #define	VM_MAP_ASSERT_LOCKED_READ(map) \
6203a0916b8SKonstantin Belousov     _vm_map_assert_locked_read(map, LOCK_FILE, LOCK_LINE)
6213a0916b8SKonstantin Belousov #else
6223a0916b8SKonstantin Belousov #define	VM_MAP_ASSERT_LOCKED(map)
6233a0916b8SKonstantin Belousov #define	VM_MAP_ASSERT_LOCKED_READ(map)
6243a0916b8SKonstantin Belousov #endif
6253a0916b8SKonstantin Belousov 
626acd9a301SAlan Cox /*
627acd9a301SAlan Cox  *	vm_map_unlock_and_wait:
628acd9a301SAlan Cox  */
6299688f931SAlan Cox int
6308ce2d00aSPawel Jakub Dawidek vm_map_unlock_and_wait(vm_map_t map, int timo)
631acd9a301SAlan Cox {
632acd9a301SAlan Cox 
6333a92e5d5SAlan Cox 	mtx_lock(&map_sleep_mtx);
634acd9a301SAlan Cox 	vm_map_unlock(map);
6358ce2d00aSPawel Jakub Dawidek 	return (msleep(&map->root, &map_sleep_mtx, PDROP | PVM, "vmmaps", timo));
636acd9a301SAlan Cox }
637acd9a301SAlan Cox 
638acd9a301SAlan Cox /*
639acd9a301SAlan Cox  *	vm_map_wakeup:
640acd9a301SAlan Cox  */
6419688f931SAlan Cox void
642acd9a301SAlan Cox vm_map_wakeup(vm_map_t map)
643acd9a301SAlan Cox {
644acd9a301SAlan Cox 
645b49ecb86SAlan Cox 	/*
6463a92e5d5SAlan Cox 	 * Acquire and release map_sleep_mtx to prevent a wakeup()
6473a92e5d5SAlan Cox 	 * from being performed (and lost) between the vm_map_unlock()
6483a92e5d5SAlan Cox 	 * and the msleep() in vm_map_unlock_and_wait().
649b49ecb86SAlan Cox 	 */
6503a92e5d5SAlan Cox 	mtx_lock(&map_sleep_mtx);
6513a92e5d5SAlan Cox 	mtx_unlock(&map_sleep_mtx);
652acd9a301SAlan Cox 	wakeup(&map->root);
653acd9a301SAlan Cox }
654acd9a301SAlan Cox 
6551b40f8c0SMatthew Dillon long
6561b40f8c0SMatthew Dillon vmspace_resident_count(struct vmspace *vmspace)
6571b40f8c0SMatthew Dillon {
6581b40f8c0SMatthew Dillon 	return pmap_resident_count(vmspace_pmap(vmspace));
6591b40f8c0SMatthew Dillon }
6601b40f8c0SMatthew Dillon 
6612bc7dd56SBruce M Simpson long
6622bc7dd56SBruce M Simpson vmspace_wired_count(struct vmspace *vmspace)
6632bc7dd56SBruce M Simpson {
6642bc7dd56SBruce M Simpson 	return pmap_wired_count(vmspace_pmap(vmspace));
6652bc7dd56SBruce M Simpson }
6662bc7dd56SBruce M Simpson 
667ff2b5645SMatthew Dillon /*
668df8bae1dSRodney W. Grimes  *	vm_map_create:
669df8bae1dSRodney W. Grimes  *
670df8bae1dSRodney W. Grimes  *	Creates and returns a new empty VM map with
671df8bae1dSRodney W. Grimes  *	the given physical map structure, and having
672df8bae1dSRodney W. Grimes  *	the given lower and upper address bounds.
673df8bae1dSRodney W. Grimes  */
6740d94caffSDavid Greenman vm_map_t
6751b40f8c0SMatthew Dillon vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max)
676df8bae1dSRodney W. Grimes {
677c0877f10SJohn Dyson 	vm_map_t result;
678df8bae1dSRodney W. Grimes 
679a163d034SWarner Losh 	result = uma_zalloc(mapzone, M_WAITOK);
68021c641b2SJohn Baldwin 	CTR1(KTR_VM, "vm_map_create: %p", result);
68192351f16SAlan Cox 	_vm_map_init(result, pmap, min, max);
682df8bae1dSRodney W. Grimes 	return (result);
683df8bae1dSRodney W. Grimes }
684df8bae1dSRodney W. Grimes 
685df8bae1dSRodney W. Grimes /*
686df8bae1dSRodney W. Grimes  * Initialize an existing vm_map structure
687df8bae1dSRodney W. Grimes  * such as that in the vmspace structure.
688df8bae1dSRodney W. Grimes  */
6898355f576SJeff Roberson static void
69092351f16SAlan Cox _vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min, vm_offset_t max)
691df8bae1dSRodney W. Grimes {
69221c641b2SJohn Baldwin 
693df8bae1dSRodney W. Grimes 	map->header.next = map->header.prev = &map->header;
6949688f931SAlan Cox 	map->needs_wakeup = FALSE;
6953075778bSJohn Dyson 	map->system_map = 0;
69692351f16SAlan Cox 	map->pmap = pmap;
697df8bae1dSRodney W. Grimes 	map->min_offset = min;
698df8bae1dSRodney W. Grimes 	map->max_offset = max;
699af7cd0c5SBrian Feldman 	map->flags = 0;
7004e94f402SAlan Cox 	map->root = NULL;
701df8bae1dSRodney W. Grimes 	map->timestamp = 0;
702655c3490SKonstantin Belousov 	map->deferred_freelist = NULL;
703df8bae1dSRodney W. Grimes }
704df8bae1dSRodney W. Grimes 
705a18b1f1dSJason Evans void
70692351f16SAlan Cox vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min, vm_offset_t max)
707a18b1f1dSJason Evans {
70892351f16SAlan Cox 
70992351f16SAlan Cox 	_vm_map_init(map, pmap, min, max);
710d923c598SAlan Cox 	mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
71112c64974SMaxime Henrion 	sx_init(&map->lock, "user map");
712a18b1f1dSJason Evans }
713a18b1f1dSJason Evans 
714df8bae1dSRodney W. Grimes /*
715b18bfc3dSJohn Dyson  *	vm_map_entry_dispose:	[ internal use only ]
716b18bfc3dSJohn Dyson  *
717b18bfc3dSJohn Dyson  *	Inverse of vm_map_entry_create.
718b18bfc3dSJohn Dyson  */
71962487bb4SJohn Dyson static void
7201b40f8c0SMatthew Dillon vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry)
721b18bfc3dSJohn Dyson {
7222b4a2c27SAlan Cox 	uma_zfree(map->system_map ? kmapentzone : mapentzone, entry);
723b18bfc3dSJohn Dyson }
724b18bfc3dSJohn Dyson 
725b18bfc3dSJohn Dyson /*
726df8bae1dSRodney W. Grimes  *	vm_map_entry_create:	[ internal use only ]
727df8bae1dSRodney W. Grimes  *
728df8bae1dSRodney W. Grimes  *	Allocates a VM map entry for insertion.
729b28cb1caSAlfred Perlstein  *	No entry fields are filled in.
730df8bae1dSRodney W. Grimes  */
731f708ef1bSPoul-Henning Kamp static vm_map_entry_t
7321b40f8c0SMatthew Dillon vm_map_entry_create(vm_map_t map)
733df8bae1dSRodney W. Grimes {
7341f6889a1SMatthew Dillon 	vm_map_entry_t new_entry;
7351f6889a1SMatthew Dillon 
7362b4a2c27SAlan Cox 	if (map->system_map)
7372b4a2c27SAlan Cox 		new_entry = uma_zalloc(kmapentzone, M_NOWAIT);
7382b4a2c27SAlan Cox 	else
739a163d034SWarner Losh 		new_entry = uma_zalloc(mapentzone, M_WAITOK);
7401f6889a1SMatthew Dillon 	if (new_entry == NULL)
7411f6889a1SMatthew Dillon 		panic("vm_map_entry_create: kernel resources exhausted");
7421f6889a1SMatthew Dillon 	return (new_entry);
743df8bae1dSRodney W. Grimes }
744df8bae1dSRodney W. Grimes 
745df8bae1dSRodney W. Grimes /*
746794316a8SAlan Cox  *	vm_map_entry_set_behavior:
747794316a8SAlan Cox  *
748794316a8SAlan Cox  *	Set the expected access behavior, either normal, random, or
749794316a8SAlan Cox  *	sequential.
750794316a8SAlan Cox  */
75162a59e8fSWarner Losh static inline void
752794316a8SAlan Cox vm_map_entry_set_behavior(vm_map_entry_t entry, u_char behavior)
753794316a8SAlan Cox {
754794316a8SAlan Cox 	entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) |
755794316a8SAlan Cox 	    (behavior & MAP_ENTRY_BEHAV_MASK);
756794316a8SAlan Cox }
757794316a8SAlan Cox 
758794316a8SAlan Cox /*
7590164e057SAlan Cox  *	vm_map_entry_set_max_free:
7600164e057SAlan Cox  *
7610164e057SAlan Cox  *	Set the max_free field in a vm_map_entry.
7620164e057SAlan Cox  */
76362a59e8fSWarner Losh static inline void
7640164e057SAlan Cox vm_map_entry_set_max_free(vm_map_entry_t entry)
7650164e057SAlan Cox {
7660164e057SAlan Cox 
7670164e057SAlan Cox 	entry->max_free = entry->adj_free;
7680164e057SAlan Cox 	if (entry->left != NULL && entry->left->max_free > entry->max_free)
7690164e057SAlan Cox 		entry->max_free = entry->left->max_free;
7700164e057SAlan Cox 	if (entry->right != NULL && entry->right->max_free > entry->max_free)
7710164e057SAlan Cox 		entry->max_free = entry->right->max_free;
7720164e057SAlan Cox }
7730164e057SAlan Cox 
7740164e057SAlan Cox /*
7754e94f402SAlan Cox  *	vm_map_entry_splay:
7764e94f402SAlan Cox  *
7770164e057SAlan Cox  *	The Sleator and Tarjan top-down splay algorithm with the
7780164e057SAlan Cox  *	following variation.  Max_free must be computed bottom-up, so
7790164e057SAlan Cox  *	on the downward pass, maintain the left and right spines in
7800164e057SAlan Cox  *	reverse order.  Then, make a second pass up each side to fix
7810164e057SAlan Cox  *	the pointers and compute max_free.  The time bound is O(log n)
7820164e057SAlan Cox  *	amortized.
7830164e057SAlan Cox  *
7840164e057SAlan Cox  *	The new root is the vm_map_entry containing "addr", or else an
7850164e057SAlan Cox  *	adjacent entry (lower or higher) if addr is not in the tree.
7860164e057SAlan Cox  *
7870164e057SAlan Cox  *	The map must be locked, and leaves it so.
7880164e057SAlan Cox  *
7890164e057SAlan Cox  *	Returns: the new root.
7904e94f402SAlan Cox  */
7914e94f402SAlan Cox static vm_map_entry_t
7920164e057SAlan Cox vm_map_entry_splay(vm_offset_t addr, vm_map_entry_t root)
7934e94f402SAlan Cox {
7940164e057SAlan Cox 	vm_map_entry_t llist, rlist;
7950164e057SAlan Cox 	vm_map_entry_t ltree, rtree;
7960164e057SAlan Cox 	vm_map_entry_t y;
7974e94f402SAlan Cox 
7980164e057SAlan Cox 	/* Special case of empty tree. */
7994e94f402SAlan Cox 	if (root == NULL)
8004e94f402SAlan Cox 		return (root);
8010164e057SAlan Cox 
8020164e057SAlan Cox 	/*
8030164e057SAlan Cox 	 * Pass One: Splay down the tree until we find addr or a NULL
8040164e057SAlan Cox 	 * pointer where addr would go.  llist and rlist are the two
8050164e057SAlan Cox 	 * sides in reverse order (bottom-up), with llist linked by
8060164e057SAlan Cox 	 * the right pointer and rlist linked by the left pointer in
8070164e057SAlan Cox 	 * the vm_map_entry.  Wait until Pass Two to set max_free on
8080164e057SAlan Cox 	 * the two spines.
8090164e057SAlan Cox 	 */
8100164e057SAlan Cox 	llist = NULL;
8110164e057SAlan Cox 	rlist = NULL;
8120164e057SAlan Cox 	for (;;) {
8130164e057SAlan Cox 		/* root is never NULL in here. */
8140164e057SAlan Cox 		if (addr < root->start) {
8150164e057SAlan Cox 			y = root->left;
8160164e057SAlan Cox 			if (y == NULL)
8174e94f402SAlan Cox 				break;
8180164e057SAlan Cox 			if (addr < y->start && y->left != NULL) {
8190164e057SAlan Cox 				/* Rotate right and put y on rlist. */
8204e94f402SAlan Cox 				root->left = y->right;
8214e94f402SAlan Cox 				y->right = root;
8220164e057SAlan Cox 				vm_map_entry_set_max_free(root);
8230164e057SAlan Cox 				root = y->left;
8240164e057SAlan Cox 				y->left = rlist;
8250164e057SAlan Cox 				rlist = y;
8260164e057SAlan Cox 			} else {
8270164e057SAlan Cox 				/* Put root on rlist. */
8280164e057SAlan Cox 				root->left = rlist;
8290164e057SAlan Cox 				rlist = root;
8304e94f402SAlan Cox 				root = y;
8314e94f402SAlan Cox 			}
8327438d60bSAlan Cox 		} else if (addr >= root->end) {
8330164e057SAlan Cox 			y = root->right;
8347438d60bSAlan Cox 			if (y == NULL)
8354e94f402SAlan Cox 				break;
8360164e057SAlan Cox 			if (addr >= y->end && y->right != NULL) {
8370164e057SAlan Cox 				/* Rotate left and put y on llist. */
8384e94f402SAlan Cox 				root->right = y->left;
8394e94f402SAlan Cox 				y->left = root;
8400164e057SAlan Cox 				vm_map_entry_set_max_free(root);
8410164e057SAlan Cox 				root = y->right;
8420164e057SAlan Cox 				y->right = llist;
8430164e057SAlan Cox 				llist = y;
8440164e057SAlan Cox 			} else {
8450164e057SAlan Cox 				/* Put root on llist. */
8460164e057SAlan Cox 				root->right = llist;
8470164e057SAlan Cox 				llist = root;
8484e94f402SAlan Cox 				root = y;
8494e94f402SAlan Cox 			}
8507438d60bSAlan Cox 		} else
8517438d60bSAlan Cox 			break;
8520164e057SAlan Cox 	}
8530164e057SAlan Cox 
8540164e057SAlan Cox 	/*
8550164e057SAlan Cox 	 * Pass Two: Walk back up the two spines, flip the pointers
8560164e057SAlan Cox 	 * and set max_free.  The subtrees of the root go at the
8570164e057SAlan Cox 	 * bottom of llist and rlist.
8580164e057SAlan Cox 	 */
8590164e057SAlan Cox 	ltree = root->left;
8600164e057SAlan Cox 	while (llist != NULL) {
8610164e057SAlan Cox 		y = llist->right;
8620164e057SAlan Cox 		llist->right = ltree;
8630164e057SAlan Cox 		vm_map_entry_set_max_free(llist);
8640164e057SAlan Cox 		ltree = llist;
8650164e057SAlan Cox 		llist = y;
8660164e057SAlan Cox 	}
8670164e057SAlan Cox 	rtree = root->right;
8680164e057SAlan Cox 	while (rlist != NULL) {
8690164e057SAlan Cox 		y = rlist->left;
8700164e057SAlan Cox 		rlist->left = rtree;
8710164e057SAlan Cox 		vm_map_entry_set_max_free(rlist);
8720164e057SAlan Cox 		rtree = rlist;
8730164e057SAlan Cox 		rlist = y;
8740164e057SAlan Cox 	}
8750164e057SAlan Cox 
8760164e057SAlan Cox 	/*
8770164e057SAlan Cox 	 * Final assembly: add ltree and rtree as subtrees of root.
8780164e057SAlan Cox 	 */
8790164e057SAlan Cox 	root->left = ltree;
8800164e057SAlan Cox 	root->right = rtree;
8810164e057SAlan Cox 	vm_map_entry_set_max_free(root);
8820164e057SAlan Cox 
8834e94f402SAlan Cox 	return (root);
8844e94f402SAlan Cox }
8854e94f402SAlan Cox 
8864e94f402SAlan Cox /*
887df8bae1dSRodney W. Grimes  *	vm_map_entry_{un,}link:
888df8bae1dSRodney W. Grimes  *
889df8bae1dSRodney W. Grimes  *	Insert/remove entries from maps.
890df8bae1dSRodney W. Grimes  */
8914e94f402SAlan Cox static void
89299c81ca9SAlan Cox vm_map_entry_link(vm_map_t map,
89399c81ca9SAlan Cox 		  vm_map_entry_t after_where,
89499c81ca9SAlan Cox 		  vm_map_entry_t entry)
89599c81ca9SAlan Cox {
89621c641b2SJohn Baldwin 
89721c641b2SJohn Baldwin 	CTR4(KTR_VM,
89821c641b2SJohn Baldwin 	    "vm_map_entry_link: map %p, nentries %d, entry %p, after %p", map,
89921c641b2SJohn Baldwin 	    map->nentries, entry, after_where);
9003a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
90199c81ca9SAlan Cox 	map->nentries++;
90299c81ca9SAlan Cox 	entry->prev = after_where;
90399c81ca9SAlan Cox 	entry->next = after_where->next;
90499c81ca9SAlan Cox 	entry->next->prev = entry;
90599c81ca9SAlan Cox 	after_where->next = entry;
9064e94f402SAlan Cox 
9074e94f402SAlan Cox 	if (after_where != &map->header) {
9084e94f402SAlan Cox 		if (after_where != map->root)
9094e94f402SAlan Cox 			vm_map_entry_splay(after_where->start, map->root);
9104e94f402SAlan Cox 		entry->right = after_where->right;
9114e94f402SAlan Cox 		entry->left = after_where;
9124e94f402SAlan Cox 		after_where->right = NULL;
9130164e057SAlan Cox 		after_where->adj_free = entry->start - after_where->end;
9140164e057SAlan Cox 		vm_map_entry_set_max_free(after_where);
9154e94f402SAlan Cox 	} else {
9164e94f402SAlan Cox 		entry->right = map->root;
9174e94f402SAlan Cox 		entry->left = NULL;
9184e94f402SAlan Cox 	}
9190164e057SAlan Cox 	entry->adj_free = (entry->next == &map->header ? map->max_offset :
9200164e057SAlan Cox 	    entry->next->start) - entry->end;
9210164e057SAlan Cox 	vm_map_entry_set_max_free(entry);
9224e94f402SAlan Cox 	map->root = entry;
923df8bae1dSRodney W. Grimes }
92499c81ca9SAlan Cox 
9254e94f402SAlan Cox static void
92699c81ca9SAlan Cox vm_map_entry_unlink(vm_map_t map,
92799c81ca9SAlan Cox 		    vm_map_entry_t entry)
92899c81ca9SAlan Cox {
9294e94f402SAlan Cox 	vm_map_entry_t next, prev, root;
93099c81ca9SAlan Cox 
9313a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
9324e94f402SAlan Cox 	if (entry != map->root)
9334e94f402SAlan Cox 		vm_map_entry_splay(entry->start, map->root);
9344e94f402SAlan Cox 	if (entry->left == NULL)
9354e94f402SAlan Cox 		root = entry->right;
9364e94f402SAlan Cox 	else {
9374e94f402SAlan Cox 		root = vm_map_entry_splay(entry->start, entry->left);
9384e94f402SAlan Cox 		root->right = entry->right;
9390164e057SAlan Cox 		root->adj_free = (entry->next == &map->header ? map->max_offset :
9400164e057SAlan Cox 		    entry->next->start) - root->end;
9410164e057SAlan Cox 		vm_map_entry_set_max_free(root);
9424e94f402SAlan Cox 	}
9434e94f402SAlan Cox 	map->root = root;
9444e94f402SAlan Cox 
9454e94f402SAlan Cox 	prev = entry->prev;
9464e94f402SAlan Cox 	next = entry->next;
94799c81ca9SAlan Cox 	next->prev = prev;
94899c81ca9SAlan Cox 	prev->next = next;
94999c81ca9SAlan Cox 	map->nentries--;
95021c641b2SJohn Baldwin 	CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map,
95121c641b2SJohn Baldwin 	    map->nentries, entry);
952df8bae1dSRodney W. Grimes }
953df8bae1dSRodney W. Grimes 
954df8bae1dSRodney W. Grimes /*
9550164e057SAlan Cox  *	vm_map_entry_resize_free:
9560164e057SAlan Cox  *
9570164e057SAlan Cox  *	Recompute the amount of free space following a vm_map_entry
9580164e057SAlan Cox  *	and propagate that value up the tree.  Call this function after
9590164e057SAlan Cox  *	resizing a map entry in-place, that is, without a call to
9600164e057SAlan Cox  *	vm_map_entry_link() or _unlink().
9610164e057SAlan Cox  *
9620164e057SAlan Cox  *	The map must be locked, and leaves it so.
9630164e057SAlan Cox  */
9640164e057SAlan Cox static void
9650164e057SAlan Cox vm_map_entry_resize_free(vm_map_t map, vm_map_entry_t entry)
9660164e057SAlan Cox {
9670164e057SAlan Cox 
9680164e057SAlan Cox 	/*
9690164e057SAlan Cox 	 * Using splay trees without parent pointers, propagating
9700164e057SAlan Cox 	 * max_free up the tree is done by moving the entry to the
9710164e057SAlan Cox 	 * root and making the change there.
9720164e057SAlan Cox 	 */
9730164e057SAlan Cox 	if (entry != map->root)
9740164e057SAlan Cox 		map->root = vm_map_entry_splay(entry->start, map->root);
9750164e057SAlan Cox 
9760164e057SAlan Cox 	entry->adj_free = (entry->next == &map->header ? map->max_offset :
9770164e057SAlan Cox 	    entry->next->start) - entry->end;
9780164e057SAlan Cox 	vm_map_entry_set_max_free(entry);
9790164e057SAlan Cox }
9800164e057SAlan Cox 
9810164e057SAlan Cox /*
982df8bae1dSRodney W. Grimes  *	vm_map_lookup_entry:	[ internal use only ]
983df8bae1dSRodney W. Grimes  *
984df8bae1dSRodney W. Grimes  *	Finds the map entry containing (or
985df8bae1dSRodney W. Grimes  *	immediately preceding) the specified address
986df8bae1dSRodney W. Grimes  *	in the given map; the entry is returned
987df8bae1dSRodney W. Grimes  *	in the "entry" parameter.  The boolean
988df8bae1dSRodney W. Grimes  *	result indicates whether the address is
989df8bae1dSRodney W. Grimes  *	actually contained in the map.
990df8bae1dSRodney W. Grimes  */
9910d94caffSDavid Greenman boolean_t
9921b40f8c0SMatthew Dillon vm_map_lookup_entry(
9931b40f8c0SMatthew Dillon 	vm_map_t map,
9941b40f8c0SMatthew Dillon 	vm_offset_t address,
9951b40f8c0SMatthew Dillon 	vm_map_entry_t *entry)	/* OUT */
996df8bae1dSRodney W. Grimes {
997c0877f10SJohn Dyson 	vm_map_entry_t cur;
99805a8c414SAlan Cox 	boolean_t locked;
999df8bae1dSRodney W. Grimes 
10004c3ef59eSAlan Cox 	/*
10014c3ef59eSAlan Cox 	 * If the map is empty, then the map entry immediately preceding
10024c3ef59eSAlan Cox 	 * "address" is the map's header.
10034c3ef59eSAlan Cox 	 */
10044c3ef59eSAlan Cox 	cur = map->root;
10054e94f402SAlan Cox 	if (cur == NULL)
10064e94f402SAlan Cox 		*entry = &map->header;
10074c3ef59eSAlan Cox 	else if (address >= cur->start && cur->end > address) {
10084c3ef59eSAlan Cox 		*entry = cur;
10094c3ef59eSAlan Cox 		return (TRUE);
101005a8c414SAlan Cox 	} else if ((locked = vm_map_locked(map)) ||
101105a8c414SAlan Cox 	    sx_try_upgrade(&map->lock)) {
101205a8c414SAlan Cox 		/*
101305a8c414SAlan Cox 		 * Splay requires a write lock on the map.  However, it only
101405a8c414SAlan Cox 		 * restructures the binary search tree; it does not otherwise
101505a8c414SAlan Cox 		 * change the map.  Thus, the map's timestamp need not change
101605a8c414SAlan Cox 		 * on a temporary upgrade.
101705a8c414SAlan Cox 		 */
10184c3ef59eSAlan Cox 		map->root = cur = vm_map_entry_splay(address, cur);
101905a8c414SAlan Cox 		if (!locked)
102005a8c414SAlan Cox 			sx_downgrade(&map->lock);
1021df8bae1dSRodney W. Grimes 
10224c3ef59eSAlan Cox 		/*
10234c3ef59eSAlan Cox 		 * If "address" is contained within a map entry, the new root
10244c3ef59eSAlan Cox 		 * is that map entry.  Otherwise, the new root is a map entry
10254c3ef59eSAlan Cox 		 * immediately before or after "address".
10264c3ef59eSAlan Cox 		 */
1027df8bae1dSRodney W. Grimes 		if (address >= cur->start) {
1028df8bae1dSRodney W. Grimes 			*entry = cur;
10294e94f402SAlan Cox 			if (cur->end > address)
1030df8bae1dSRodney W. Grimes 				return (TRUE);
10314e94f402SAlan Cox 		} else
1032df8bae1dSRodney W. Grimes 			*entry = cur->prev;
103305a8c414SAlan Cox 	} else
103405a8c414SAlan Cox 		/*
103505a8c414SAlan Cox 		 * Since the map is only locked for read access, perform a
103605a8c414SAlan Cox 		 * standard binary search tree lookup for "address".
103705a8c414SAlan Cox 		 */
103805a8c414SAlan Cox 		for (;;) {
103905a8c414SAlan Cox 			if (address < cur->start) {
104005a8c414SAlan Cox 				if (cur->left == NULL) {
104105a8c414SAlan Cox 					*entry = cur->prev;
104205a8c414SAlan Cox 					break;
104305a8c414SAlan Cox 				}
104405a8c414SAlan Cox 				cur = cur->left;
104505a8c414SAlan Cox 			} else if (cur->end > address) {
104605a8c414SAlan Cox 				*entry = cur;
104705a8c414SAlan Cox 				return (TRUE);
104805a8c414SAlan Cox 			} else {
104905a8c414SAlan Cox 				if (cur->right == NULL) {
105005a8c414SAlan Cox 					*entry = cur;
105105a8c414SAlan Cox 					break;
105205a8c414SAlan Cox 				}
105305a8c414SAlan Cox 				cur = cur->right;
105405a8c414SAlan Cox 			}
10554e94f402SAlan Cox 		}
1056df8bae1dSRodney W. Grimes 	return (FALSE);
1057df8bae1dSRodney W. Grimes }
1058df8bae1dSRodney W. Grimes 
1059df8bae1dSRodney W. Grimes /*
106030dcfc09SJohn Dyson  *	vm_map_insert:
106130dcfc09SJohn Dyson  *
106230dcfc09SJohn Dyson  *	Inserts the given whole VM object into the target
106330dcfc09SJohn Dyson  *	map at the specified address range.  The object's
106430dcfc09SJohn Dyson  *	size should match that of the address range.
106530dcfc09SJohn Dyson  *
106630dcfc09SJohn Dyson  *	Requires that the map be locked, and leaves it so.
10672aaeadf8SMatthew Dillon  *
10682aaeadf8SMatthew Dillon  *	If object is non-NULL, ref count must be bumped by caller
10692aaeadf8SMatthew Dillon  *	prior to making call to account for the new entry.
107030dcfc09SJohn Dyson  */
107130dcfc09SJohn Dyson int
1072b9dcd593SBruce Evans vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1073b9dcd593SBruce Evans 	      vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max,
1074b9dcd593SBruce Evans 	      int cow)
107530dcfc09SJohn Dyson {
1076c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
1077c0877f10SJohn Dyson 	vm_map_entry_t prev_entry;
107830dcfc09SJohn Dyson 	vm_map_entry_t temp_entry;
10799730a5daSPaul Saab 	vm_eflags_t protoeflags;
10803364c323SKonstantin Belousov 	struct uidinfo *uip;
10813364c323SKonstantin Belousov 	boolean_t charge_prev_obj;
108230dcfc09SJohn Dyson 
10833a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
10843a0916b8SKonstantin Belousov 
108530dcfc09SJohn Dyson 	/*
108630dcfc09SJohn Dyson 	 * Check that the start and end points are not bogus.
108730dcfc09SJohn Dyson 	 */
108830dcfc09SJohn Dyson 	if ((start < map->min_offset) || (end > map->max_offset) ||
108930dcfc09SJohn Dyson 	    (start >= end))
109030dcfc09SJohn Dyson 		return (KERN_INVALID_ADDRESS);
109130dcfc09SJohn Dyson 
109230dcfc09SJohn Dyson 	/*
109330dcfc09SJohn Dyson 	 * Find the entry prior to the proposed starting address; if it's part
109430dcfc09SJohn Dyson 	 * of an existing entry, this range is bogus.
109530dcfc09SJohn Dyson 	 */
109630dcfc09SJohn Dyson 	if (vm_map_lookup_entry(map, start, &temp_entry))
109730dcfc09SJohn Dyson 		return (KERN_NO_SPACE);
109830dcfc09SJohn Dyson 
109930dcfc09SJohn Dyson 	prev_entry = temp_entry;
110030dcfc09SJohn Dyson 
110130dcfc09SJohn Dyson 	/*
110230dcfc09SJohn Dyson 	 * Assert that the next entry doesn't overlap the end point.
110330dcfc09SJohn Dyson 	 */
110430dcfc09SJohn Dyson 	if ((prev_entry->next != &map->header) &&
110530dcfc09SJohn Dyson 	    (prev_entry->next->start < end))
110630dcfc09SJohn Dyson 		return (KERN_NO_SPACE);
110730dcfc09SJohn Dyson 
1108afa07f7eSJohn Dyson 	protoeflags = 0;
11093364c323SKonstantin Belousov 	charge_prev_obj = FALSE;
1110afa07f7eSJohn Dyson 
1111afa07f7eSJohn Dyson 	if (cow & MAP_COPY_ON_WRITE)
1112e5f13bddSAlan Cox 		protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
1113afa07f7eSJohn Dyson 
11144e045f93SAlan Cox 	if (cow & MAP_NOFAULT) {
1115afa07f7eSJohn Dyson 		protoeflags |= MAP_ENTRY_NOFAULT;
1116afa07f7eSJohn Dyson 
11174e045f93SAlan Cox 		KASSERT(object == NULL,
11184e045f93SAlan Cox 			("vm_map_insert: paradoxical MAP_NOFAULT request"));
11194e045f93SAlan Cox 	}
11204f79d873SMatthew Dillon 	if (cow & MAP_DISABLE_SYNCER)
11214f79d873SMatthew Dillon 		protoeflags |= MAP_ENTRY_NOSYNC;
11229730a5daSPaul Saab 	if (cow & MAP_DISABLE_COREDUMP)
11239730a5daSPaul Saab 		protoeflags |= MAP_ENTRY_NOCOREDUMP;
11244f79d873SMatthew Dillon 
11253364c323SKonstantin Belousov 	uip = NULL;
11263364c323SKonstantin Belousov 	KASSERT((object != kmem_object && object != kernel_object) ||
11273364c323SKonstantin Belousov 	    ((object == kmem_object || object == kernel_object) &&
11283364c323SKonstantin Belousov 		!(protoeflags & MAP_ENTRY_NEEDS_COPY)),
11293364c323SKonstantin Belousov 	    ("kmem or kernel object and cow"));
11303364c323SKonstantin Belousov 	if (cow & (MAP_ACC_NO_CHARGE | MAP_NOFAULT))
11313364c323SKonstantin Belousov 		goto charged;
11323364c323SKonstantin Belousov 	if ((cow & MAP_ACC_CHARGED) || ((prot & VM_PROT_WRITE) &&
11333364c323SKonstantin Belousov 	    ((protoeflags & MAP_ENTRY_NEEDS_COPY) || object == NULL))) {
11343364c323SKonstantin Belousov 		if (!(cow & MAP_ACC_CHARGED) && !swap_reserve(end - start))
11353364c323SKonstantin Belousov 			return (KERN_RESOURCE_SHORTAGE);
113641c22744SKonstantin Belousov 		KASSERT(object == NULL || (protoeflags & MAP_ENTRY_NEEDS_COPY) ||
11373364c323SKonstantin Belousov 		    object->uip == NULL,
11383364c323SKonstantin Belousov 		    ("OVERCOMMIT: vm_map_insert o %p", object));
11393364c323SKonstantin Belousov 		uip = curthread->td_ucred->cr_ruidinfo;
11403364c323SKonstantin Belousov 		uihold(uip);
11413364c323SKonstantin Belousov 		if (object == NULL && !(protoeflags & MAP_ENTRY_NEEDS_COPY))
11423364c323SKonstantin Belousov 			charge_prev_obj = TRUE;
11433364c323SKonstantin Belousov 	}
11443364c323SKonstantin Belousov 
11453364c323SKonstantin Belousov charged:
11461d284e00SAlan Cox 	if (object != NULL) {
114730dcfc09SJohn Dyson 		/*
11481d284e00SAlan Cox 		 * OBJ_ONEMAPPING must be cleared unless this mapping
11491d284e00SAlan Cox 		 * is trivially proven to be the only mapping for any
11501d284e00SAlan Cox 		 * of the object's pages.  (Object granularity
11511d284e00SAlan Cox 		 * reference counting is insufficient to recognize
11521d284e00SAlan Cox 		 * aliases with precision.)
115330dcfc09SJohn Dyson 		 */
11541d284e00SAlan Cox 		VM_OBJECT_LOCK(object);
11551d284e00SAlan Cox 		if (object->ref_count > 1 || object->shadow_count != 0)
11562aaeadf8SMatthew Dillon 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
11571d284e00SAlan Cox 		VM_OBJECT_UNLOCK(object);
11584e045f93SAlan Cox 	}
11594e045f93SAlan Cox 	else if ((prev_entry != &map->header) &&
11604e045f93SAlan Cox 		 (prev_entry->eflags == protoeflags) &&
11618cc7e047SJohn Dyson 		 (prev_entry->end == start) &&
11624e045f93SAlan Cox 		 (prev_entry->wired_count == 0) &&
11633364c323SKonstantin Belousov 		 (prev_entry->uip == uip ||
11643364c323SKonstantin Belousov 		  (prev_entry->object.vm_object != NULL &&
11653364c323SKonstantin Belousov 		   (prev_entry->object.vm_object->uip == uip))) &&
11668cc7e047SJohn Dyson 		   vm_object_coalesce(prev_entry->object.vm_object,
116757a21abaSAlan Cox 		       prev_entry->offset,
11688cc7e047SJohn Dyson 		       (vm_size_t)(prev_entry->end - prev_entry->start),
11693364c323SKonstantin Belousov 		       (vm_size_t)(end - prev_entry->end), charge_prev_obj)) {
117030dcfc09SJohn Dyson 		/*
11712aaeadf8SMatthew Dillon 		 * We were able to extend the object.  Determine if we
11722aaeadf8SMatthew Dillon 		 * can extend the previous map entry to include the
11732aaeadf8SMatthew Dillon 		 * new range as well.
117430dcfc09SJohn Dyson 		 */
11758cc7e047SJohn Dyson 		if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
11768cc7e047SJohn Dyson 		    (prev_entry->protection == prot) &&
11778cc7e047SJohn Dyson 		    (prev_entry->max_protection == max)) {
117830dcfc09SJohn Dyson 			map->size += (end - prev_entry->end);
117930dcfc09SJohn Dyson 			prev_entry->end = end;
11800164e057SAlan Cox 			vm_map_entry_resize_free(map, prev_entry);
11814e71e795SMatthew Dillon 			vm_map_simplify_entry(map, prev_entry);
11823364c323SKonstantin Belousov 			if (uip != NULL)
11833364c323SKonstantin Belousov 				uifree(uip);
118430dcfc09SJohn Dyson 			return (KERN_SUCCESS);
118530dcfc09SJohn Dyson 		}
11868cc7e047SJohn Dyson 
11872aaeadf8SMatthew Dillon 		/*
11882aaeadf8SMatthew Dillon 		 * If we can extend the object but cannot extend the
11892aaeadf8SMatthew Dillon 		 * map entry, we have to create a new map entry.  We
11902aaeadf8SMatthew Dillon 		 * must bump the ref count on the extended object to
11914e71e795SMatthew Dillon 		 * account for it.  object may be NULL.
11922aaeadf8SMatthew Dillon 		 */
11932aaeadf8SMatthew Dillon 		object = prev_entry->object.vm_object;
11942aaeadf8SMatthew Dillon 		offset = prev_entry->offset +
11952aaeadf8SMatthew Dillon 			(prev_entry->end - prev_entry->start);
11968cc7e047SJohn Dyson 		vm_object_reference(object);
11973364c323SKonstantin Belousov 		if (uip != NULL && object != NULL && object->uip != NULL &&
11983364c323SKonstantin Belousov 		    !(prev_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
11993364c323SKonstantin Belousov 			/* Object already accounts for this uid. */
12003364c323SKonstantin Belousov 			uifree(uip);
12013364c323SKonstantin Belousov 			uip = NULL;
12023364c323SKonstantin Belousov 		}
1203b18bfc3dSJohn Dyson 	}
12042aaeadf8SMatthew Dillon 
12052aaeadf8SMatthew Dillon 	/*
12062aaeadf8SMatthew Dillon 	 * NOTE: if conditionals fail, object can be NULL here.  This occurs
12072aaeadf8SMatthew Dillon 	 * in things like the buffer map where we manage kva but do not manage
12082aaeadf8SMatthew Dillon 	 * backing objects.
12092aaeadf8SMatthew Dillon 	 */
12108cc7e047SJohn Dyson 
121130dcfc09SJohn Dyson 	/*
121230dcfc09SJohn Dyson 	 * Create a new entry
121330dcfc09SJohn Dyson 	 */
121430dcfc09SJohn Dyson 	new_entry = vm_map_entry_create(map);
121530dcfc09SJohn Dyson 	new_entry->start = start;
121630dcfc09SJohn Dyson 	new_entry->end = end;
12173364c323SKonstantin Belousov 	new_entry->uip = NULL;
121830dcfc09SJohn Dyson 
1219afa07f7eSJohn Dyson 	new_entry->eflags = protoeflags;
122030dcfc09SJohn Dyson 	new_entry->object.vm_object = object;
122130dcfc09SJohn Dyson 	new_entry->offset = offset;
12222267af78SJulian Elischer 	new_entry->avail_ssize = 0;
12232267af78SJulian Elischer 
122430dcfc09SJohn Dyson 	new_entry->inheritance = VM_INHERIT_DEFAULT;
122530dcfc09SJohn Dyson 	new_entry->protection = prot;
122630dcfc09SJohn Dyson 	new_entry->max_protection = max;
122730dcfc09SJohn Dyson 	new_entry->wired_count = 0;
1228e5f251d2SAlan Cox 
12293364c323SKonstantin Belousov 	KASSERT(uip == NULL || !ENTRY_CHARGED(new_entry),
12303364c323SKonstantin Belousov 	    ("OVERCOMMIT: vm_map_insert leaks vm_map %p", new_entry));
12313364c323SKonstantin Belousov 	new_entry->uip = uip;
12323364c323SKonstantin Belousov 
123330dcfc09SJohn Dyson 	/*
123430dcfc09SJohn Dyson 	 * Insert the new entry into the list
123530dcfc09SJohn Dyson 	 */
123630dcfc09SJohn Dyson 	vm_map_entry_link(map, prev_entry, new_entry);
123730dcfc09SJohn Dyson 	map->size += new_entry->end - new_entry->start;
123830dcfc09SJohn Dyson 
12391a484d28SMatthew Dillon #if 0
12401a484d28SMatthew Dillon 	/*
12411a484d28SMatthew Dillon 	 * Temporarily removed to avoid MAP_STACK panic, due to
12421a484d28SMatthew Dillon 	 * MAP_STACK being a huge hack.  Will be added back in
12431a484d28SMatthew Dillon 	 * when MAP_STACK (and the user stack mapping) is fixed.
12441a484d28SMatthew Dillon 	 */
12454e71e795SMatthew Dillon 	/*
12464e71e795SMatthew Dillon 	 * It may be possible to simplify the entry
12474e71e795SMatthew Dillon 	 */
12484e71e795SMatthew Dillon 	vm_map_simplify_entry(map, new_entry);
12491a484d28SMatthew Dillon #endif
12504e71e795SMatthew Dillon 
12514f79d873SMatthew Dillon 	if (cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) {
12524da4d293SAlan Cox 		vm_map_pmap_enter(map, start, prot,
1253e972780aSAlan Cox 				    object, OFF_TO_IDX(offset), end - start,
1254e972780aSAlan Cox 				    cow & MAP_PREFAULT_PARTIAL);
12554f79d873SMatthew Dillon 	}
1256e972780aSAlan Cox 
125730dcfc09SJohn Dyson 	return (KERN_SUCCESS);
125830dcfc09SJohn Dyson }
125930dcfc09SJohn Dyson 
126030dcfc09SJohn Dyson /*
12610164e057SAlan Cox  *	vm_map_findspace:
12620164e057SAlan Cox  *
12630164e057SAlan Cox  *	Find the first fit (lowest VM address) for "length" free bytes
12640164e057SAlan Cox  *	beginning at address >= start in the given map.
12650164e057SAlan Cox  *
12660164e057SAlan Cox  *	In a vm_map_entry, "adj_free" is the amount of free space
12670164e057SAlan Cox  *	adjacent (higher address) to this entry, and "max_free" is the
12680164e057SAlan Cox  *	maximum amount of contiguous free space in its subtree.  This
12690164e057SAlan Cox  *	allows finding a free region in one path down the tree, so
12700164e057SAlan Cox  *	O(log n) amortized with splay trees.
12710164e057SAlan Cox  *
12720164e057SAlan Cox  *	The map must be locked, and leaves it so.
12730164e057SAlan Cox  *
12740164e057SAlan Cox  *	Returns: 0 on success, and starting address in *addr,
12750164e057SAlan Cox  *		 1 if insufficient space.
1276df8bae1dSRodney W. Grimes  */
1277df8bae1dSRodney W. Grimes int
12780164e057SAlan Cox vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length,
12790164e057SAlan Cox     vm_offset_t *addr)	/* OUT */
1280df8bae1dSRodney W. Grimes {
12810164e057SAlan Cox 	vm_map_entry_t entry;
12820164e057SAlan Cox 	vm_offset_t end, st;
1283df8bae1dSRodney W. Grimes 
1284986b43f8SAlan Cox 	/*
1285986b43f8SAlan Cox 	 * Request must fit within min/max VM address and must avoid
1286986b43f8SAlan Cox 	 * address wrap.
1287986b43f8SAlan Cox 	 */
1288df8bae1dSRodney W. Grimes 	if (start < map->min_offset)
1289df8bae1dSRodney W. Grimes 		start = map->min_offset;
1290986b43f8SAlan Cox 	if (start + length > map->max_offset || start + length < start)
1291df8bae1dSRodney W. Grimes 		return (1);
1292df8bae1dSRodney W. Grimes 
12930164e057SAlan Cox 	/* Empty tree means wide open address space. */
12940164e057SAlan Cox 	if (map->root == NULL) {
1295df8bae1dSRodney W. Grimes 		*addr = start;
12960164e057SAlan Cox 		goto found;
129799448ed1SJohn Dyson 	}
12980164e057SAlan Cox 
12990164e057SAlan Cox 	/*
13000164e057SAlan Cox 	 * After splay, if start comes before root node, then there
13010164e057SAlan Cox 	 * must be a gap from start to the root.
13020164e057SAlan Cox 	 */
13030164e057SAlan Cox 	map->root = vm_map_entry_splay(start, map->root);
13040164e057SAlan Cox 	if (start + length <= map->root->start) {
13050164e057SAlan Cox 		*addr = start;
13060164e057SAlan Cox 		goto found;
13070164e057SAlan Cox 	}
13080164e057SAlan Cox 
13090164e057SAlan Cox 	/*
13100164e057SAlan Cox 	 * Root is the last node that might begin its gap before
1311986b43f8SAlan Cox 	 * start, and this is the last comparison where address
1312986b43f8SAlan Cox 	 * wrap might be a problem.
13130164e057SAlan Cox 	 */
13140164e057SAlan Cox 	st = (start > map->root->end) ? start : map->root->end;
1315986b43f8SAlan Cox 	if (length <= map->root->end + map->root->adj_free - st) {
13160164e057SAlan Cox 		*addr = st;
13170164e057SAlan Cox 		goto found;
13180164e057SAlan Cox 	}
13190164e057SAlan Cox 
13200164e057SAlan Cox 	/* With max_free, can immediately tell if no solution. */
13210164e057SAlan Cox 	entry = map->root->right;
13220164e057SAlan Cox 	if (entry == NULL || length > entry->max_free)
13230164e057SAlan Cox 		return (1);
13240164e057SAlan Cox 
13250164e057SAlan Cox 	/*
13260164e057SAlan Cox 	 * Search the right subtree in the order: left subtree, root,
13270164e057SAlan Cox 	 * right subtree (first fit).  The previous splay implies that
13280164e057SAlan Cox 	 * all regions in the right subtree have addresses > start.
13290164e057SAlan Cox 	 */
13300164e057SAlan Cox 	while (entry != NULL) {
13310164e057SAlan Cox 		if (entry->left != NULL && entry->left->max_free >= length)
13320164e057SAlan Cox 			entry = entry->left;
13330164e057SAlan Cox 		else if (entry->adj_free >= length) {
13340164e057SAlan Cox 			*addr = entry->end;
13350164e057SAlan Cox 			goto found;
13360164e057SAlan Cox 		} else
13370164e057SAlan Cox 			entry = entry->right;
13380164e057SAlan Cox 	}
13390164e057SAlan Cox 
13400164e057SAlan Cox 	/* Can't get here, so panic if we do. */
13410164e057SAlan Cox 	panic("vm_map_findspace: max_free corrupt");
13420164e057SAlan Cox 
13430164e057SAlan Cox found:
13440164e057SAlan Cox 	/* Expand the kernel pmap, if necessary. */
13450164e057SAlan Cox 	if (map == kernel_map) {
13460164e057SAlan Cox 		end = round_page(*addr + length);
13470164e057SAlan Cox 		if (end > kernel_vm_end)
13480164e057SAlan Cox 			pmap_growkernel(end);
134999448ed1SJohn Dyson 	}
1350df8bae1dSRodney W. Grimes 	return (0);
1351df8bae1dSRodney W. Grimes }
1352df8bae1dSRodney W. Grimes 
1353d239bd3cSKonstantin Belousov int
1354d239bd3cSKonstantin Belousov vm_map_fixed(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1355b8ca4ef2SAlan Cox     vm_offset_t start, vm_size_t length, vm_prot_t prot,
1356d239bd3cSKonstantin Belousov     vm_prot_t max, int cow)
1357d239bd3cSKonstantin Belousov {
1358b8ca4ef2SAlan Cox 	vm_offset_t end;
1359d239bd3cSKonstantin Belousov 	int result;
1360d239bd3cSKonstantin Belousov 
1361d239bd3cSKonstantin Belousov 	end = start + length;
1362897d81a0SKonstantin Belousov 	vm_map_lock(map);
1363d239bd3cSKonstantin Belousov 	VM_MAP_RANGE_CHECK(map, start, end);
1364655c3490SKonstantin Belousov 	(void) vm_map_delete(map, start, end);
1365d239bd3cSKonstantin Belousov 	result = vm_map_insert(map, object, offset, start, end, prot,
1366d239bd3cSKonstantin Belousov 	    max, cow);
1367d239bd3cSKonstantin Belousov 	vm_map_unlock(map);
1368d239bd3cSKonstantin Belousov 	return (result);
1369d239bd3cSKonstantin Belousov }
1370d239bd3cSKonstantin Belousov 
1371df8bae1dSRodney W. Grimes /*
1372df8bae1dSRodney W. Grimes  *	vm_map_find finds an unallocated region in the target address
1373df8bae1dSRodney W. Grimes  *	map with the given length.  The search is defined to be
1374df8bae1dSRodney W. Grimes  *	first-fit from the specified address; the region found is
1375df8bae1dSRodney W. Grimes  *	returned in the same parameter.
1376df8bae1dSRodney W. Grimes  *
13772aaeadf8SMatthew Dillon  *	If object is non-NULL, ref count must be bumped by caller
13782aaeadf8SMatthew Dillon  *	prior to making call to account for the new entry.
1379df8bae1dSRodney W. Grimes  */
1380df8bae1dSRodney W. Grimes int
1381b9dcd593SBruce Evans vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1382b9dcd593SBruce Evans 	    vm_offset_t *addr,	/* IN/OUT */
138326c538ffSAlan Cox 	    vm_size_t length, int find_space, vm_prot_t prot,
1384b9dcd593SBruce Evans 	    vm_prot_t max, int cow)
1385df8bae1dSRodney W. Grimes {
1386c0877f10SJohn Dyson 	vm_offset_t start;
13876eaee3feSAlan Cox 	int result;
1388df8bae1dSRodney W. Grimes 
1389df8bae1dSRodney W. Grimes 	start = *addr;
1390bea41bcfSDavid Greenman 	vm_map_lock(map);
139126c538ffSAlan Cox 	do {
139226c538ffSAlan Cox 		if (find_space != VMFS_NO_SPACE) {
1393df8bae1dSRodney W. Grimes 			if (vm_map_findspace(map, start, length, addr)) {
1394df8bae1dSRodney W. Grimes 				vm_map_unlock(map);
1395df8bae1dSRodney W. Grimes 				return (KERN_NO_SPACE);
1396df8bae1dSRodney W. Grimes 			}
1397ca596a25SJuli Mallett 			switch (find_space) {
1398ca596a25SJuli Mallett 			case VMFS_ALIGNED_SPACE:
139926c538ffSAlan Cox 				pmap_align_superpage(object, offset, addr,
140026c538ffSAlan Cox 				    length);
1401ca596a25SJuli Mallett 				break;
1402ca596a25SJuli Mallett #ifdef VMFS_TLB_ALIGNED_SPACE
1403ca596a25SJuli Mallett 			case VMFS_TLB_ALIGNED_SPACE:
1404ca596a25SJuli Mallett 				pmap_align_tlb(addr);
1405ca596a25SJuli Mallett 				break;
1406ca596a25SJuli Mallett #endif
1407ca596a25SJuli Mallett 			default:
1408ca596a25SJuli Mallett 				break;
1409ca596a25SJuli Mallett 			}
1410ca596a25SJuli Mallett 
1411df8bae1dSRodney W. Grimes 			start = *addr;
1412df8bae1dSRodney W. Grimes 		}
141326c538ffSAlan Cox 		result = vm_map_insert(map, object, offset, start, start +
141426c538ffSAlan Cox 		    length, prot, max, cow);
1415*9b55fc04SAlan Cox 	} while (result == KERN_NO_SPACE && (find_space == VMFS_ALIGNED_SPACE
1416*9b55fc04SAlan Cox #ifdef VMFS_TLB_ALIGNED_SPACE
1417*9b55fc04SAlan Cox 	    || find_space == VMFS_TLB_ALIGNED_SPACE
1418*9b55fc04SAlan Cox #endif
1419*9b55fc04SAlan Cox 	    ));
1420df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1421df8bae1dSRodney W. Grimes 	return (result);
1422df8bae1dSRodney W. Grimes }
1423df8bae1dSRodney W. Grimes 
1424df8bae1dSRodney W. Grimes /*
1425b7b2aac2SJohn Dyson  *	vm_map_simplify_entry:
142667bf6868SJohn Dyson  *
14274e71e795SMatthew Dillon  *	Simplify the given map entry by merging with either neighbor.  This
14284e71e795SMatthew Dillon  *	routine also has the ability to merge with both neighbors.
14294e71e795SMatthew Dillon  *
14304e71e795SMatthew Dillon  *	The map must be locked.
14314e71e795SMatthew Dillon  *
14324e71e795SMatthew Dillon  *	This routine guarentees that the passed entry remains valid (though
14334e71e795SMatthew Dillon  *	possibly extended).  When merging, this routine may delete one or
14344e71e795SMatthew Dillon  *	both neighbors.
1435df8bae1dSRodney W. Grimes  */
1436b7b2aac2SJohn Dyson void
14371b40f8c0SMatthew Dillon vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry)
1438df8bae1dSRodney W. Grimes {
1439308c24baSJohn Dyson 	vm_map_entry_t next, prev;
1440b7b2aac2SJohn Dyson 	vm_size_t prevsize, esize;
1441df8bae1dSRodney W. Grimes 
1442acd9a301SAlan Cox 	if (entry->eflags & (MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP))
1443df8bae1dSRodney W. Grimes 		return;
1444308c24baSJohn Dyson 
1445308c24baSJohn Dyson 	prev = entry->prev;
1446308c24baSJohn Dyson 	if (prev != &map->header) {
144767bf6868SJohn Dyson 		prevsize = prev->end - prev->start;
144867bf6868SJohn Dyson 		if ( (prev->end == entry->start) &&
144967bf6868SJohn Dyson 		     (prev->object.vm_object == entry->object.vm_object) &&
145095e5e988SJohn Dyson 		     (!prev->object.vm_object ||
145167bf6868SJohn Dyson 			(prev->offset + prevsize == entry->offset)) &&
1452afa07f7eSJohn Dyson 		     (prev->eflags == entry->eflags) &&
145367bf6868SJohn Dyson 		     (prev->protection == entry->protection) &&
145467bf6868SJohn Dyson 		     (prev->max_protection == entry->max_protection) &&
145567bf6868SJohn Dyson 		     (prev->inheritance == entry->inheritance) &&
14563364c323SKonstantin Belousov 		     (prev->wired_count == entry->wired_count) &&
14573364c323SKonstantin Belousov 		     (prev->uip == entry->uip)) {
1458308c24baSJohn Dyson 			vm_map_entry_unlink(map, prev);
1459308c24baSJohn Dyson 			entry->start = prev->start;
1460308c24baSJohn Dyson 			entry->offset = prev->offset;
14610164e057SAlan Cox 			if (entry->prev != &map->header)
14620164e057SAlan Cox 				vm_map_entry_resize_free(map, entry->prev);
14637fd10fb3SKonstantin Belousov 
14647fd10fb3SKonstantin Belousov 			/*
1465b0994946SKonstantin Belousov 			 * If the backing object is a vnode object,
1466b0994946SKonstantin Belousov 			 * vm_object_deallocate() calls vrele().
1467b0994946SKonstantin Belousov 			 * However, vrele() does not lock the vnode
1468b0994946SKonstantin Belousov 			 * because the vnode has additional
1469b0994946SKonstantin Belousov 			 * references.  Thus, the map lock can be kept
1470b0994946SKonstantin Belousov 			 * without causing a lock-order reversal with
1471b0994946SKonstantin Belousov 			 * the vnode lock.
14727fd10fb3SKonstantin Belousov 			 */
1473b18bfc3dSJohn Dyson 			if (prev->object.vm_object)
1474308c24baSJohn Dyson 				vm_object_deallocate(prev->object.vm_object);
14753364c323SKonstantin Belousov 			if (prev->uip != NULL)
14763364c323SKonstantin Belousov 				uifree(prev->uip);
1477308c24baSJohn Dyson 			vm_map_entry_dispose(map, prev);
1478308c24baSJohn Dyson 		}
1479308c24baSJohn Dyson 	}
1480de5f6a77SJohn Dyson 
1481de5f6a77SJohn Dyson 	next = entry->next;
1482308c24baSJohn Dyson 	if (next != &map->header) {
148367bf6868SJohn Dyson 		esize = entry->end - entry->start;
148467bf6868SJohn Dyson 		if ((entry->end == next->start) &&
148567bf6868SJohn Dyson 		    (next->object.vm_object == entry->object.vm_object) &&
148667bf6868SJohn Dyson 		     (!entry->object.vm_object ||
148767bf6868SJohn Dyson 			(entry->offset + esize == next->offset)) &&
1488afa07f7eSJohn Dyson 		    (next->eflags == entry->eflags) &&
148967bf6868SJohn Dyson 		    (next->protection == entry->protection) &&
149067bf6868SJohn Dyson 		    (next->max_protection == entry->max_protection) &&
149167bf6868SJohn Dyson 		    (next->inheritance == entry->inheritance) &&
14923364c323SKonstantin Belousov 		    (next->wired_count == entry->wired_count) &&
14933364c323SKonstantin Belousov 		    (next->uip == entry->uip)) {
1494de5f6a77SJohn Dyson 			vm_map_entry_unlink(map, next);
1495de5f6a77SJohn Dyson 			entry->end = next->end;
14960164e057SAlan Cox 			vm_map_entry_resize_free(map, entry);
14977fd10fb3SKonstantin Belousov 
14987fd10fb3SKonstantin Belousov 			/*
14997fd10fb3SKonstantin Belousov 			 * See comment above.
15007fd10fb3SKonstantin Belousov 			 */
1501b18bfc3dSJohn Dyson 			if (next->object.vm_object)
1502de5f6a77SJohn Dyson 				vm_object_deallocate(next->object.vm_object);
15033364c323SKonstantin Belousov 			if (next->uip != NULL)
15043364c323SKonstantin Belousov 				uifree(next->uip);
1505de5f6a77SJohn Dyson 			vm_map_entry_dispose(map, next);
1506df8bae1dSRodney W. Grimes 		}
1507df8bae1dSRodney W. Grimes 	}
1508de5f6a77SJohn Dyson }
1509df8bae1dSRodney W. Grimes /*
1510df8bae1dSRodney W. Grimes  *	vm_map_clip_start:	[ internal use only ]
1511df8bae1dSRodney W. Grimes  *
1512df8bae1dSRodney W. Grimes  *	Asserts that the given entry begins at or after
1513df8bae1dSRodney W. Grimes  *	the specified address; if necessary,
1514df8bae1dSRodney W. Grimes  *	it splits the entry into two.
1515df8bae1dSRodney W. Grimes  */
1516df8bae1dSRodney W. Grimes #define vm_map_clip_start(map, entry, startaddr) \
1517df8bae1dSRodney W. Grimes { \
1518df8bae1dSRodney W. Grimes 	if (startaddr > entry->start) \
1519df8bae1dSRodney W. Grimes 		_vm_map_clip_start(map, entry, startaddr); \
1520df8bae1dSRodney W. Grimes }
1521df8bae1dSRodney W. Grimes 
1522df8bae1dSRodney W. Grimes /*
1523df8bae1dSRodney W. Grimes  *	This routine is called only when it is known that
1524df8bae1dSRodney W. Grimes  *	the entry must be split.
1525df8bae1dSRodney W. Grimes  */
15260d94caffSDavid Greenman static void
15271b40f8c0SMatthew Dillon _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start)
1528df8bae1dSRodney W. Grimes {
1529c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
1530df8bae1dSRodney W. Grimes 
15313a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
15323a0916b8SKonstantin Belousov 
1533df8bae1dSRodney W. Grimes 	/*
15340d94caffSDavid Greenman 	 * Split off the front portion -- note that we must insert the new
15350d94caffSDavid Greenman 	 * entry BEFORE this one, so that this entry has the specified
15360d94caffSDavid Greenman 	 * starting address.
1537df8bae1dSRodney W. Grimes 	 */
1538f32dbbeeSJohn Dyson 	vm_map_simplify_entry(map, entry);
1539f32dbbeeSJohn Dyson 
154011cccda1SJohn Dyson 	/*
154111cccda1SJohn Dyson 	 * If there is no object backing this entry, we might as well create
154211cccda1SJohn Dyson 	 * one now.  If we defer it, an object can get created after the map
154311cccda1SJohn Dyson 	 * is clipped, and individual objects will be created for the split-up
154411cccda1SJohn Dyson 	 * map.  This is a bit of a hack, but is also about the best place to
154511cccda1SJohn Dyson 	 * put this improvement.
154611cccda1SJohn Dyson 	 */
15474e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL && !map->system_map) {
154811cccda1SJohn Dyson 		vm_object_t object;
154911cccda1SJohn Dyson 		object = vm_object_allocate(OBJT_DEFAULT,
1550c2e11a03SJohn Dyson 				atop(entry->end - entry->start));
155111cccda1SJohn Dyson 		entry->object.vm_object = object;
155211cccda1SJohn Dyson 		entry->offset = 0;
15533364c323SKonstantin Belousov 		if (entry->uip != NULL) {
15543364c323SKonstantin Belousov 			object->uip = entry->uip;
15553364c323SKonstantin Belousov 			object->charge = entry->end - entry->start;
15563364c323SKonstantin Belousov 			entry->uip = NULL;
15573364c323SKonstantin Belousov 		}
15583364c323SKonstantin Belousov 	} else if (entry->object.vm_object != NULL &&
15593364c323SKonstantin Belousov 		   ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) &&
15603364c323SKonstantin Belousov 		   entry->uip != NULL) {
15613364c323SKonstantin Belousov 		VM_OBJECT_LOCK(entry->object.vm_object);
15623364c323SKonstantin Belousov 		KASSERT(entry->object.vm_object->uip == NULL,
15633364c323SKonstantin Belousov 		    ("OVERCOMMIT: vm_entry_clip_start: both uip e %p", entry));
15643364c323SKonstantin Belousov 		entry->object.vm_object->uip = entry->uip;
15653364c323SKonstantin Belousov 		entry->object.vm_object->charge = entry->end - entry->start;
15663364c323SKonstantin Belousov 		VM_OBJECT_UNLOCK(entry->object.vm_object);
15673364c323SKonstantin Belousov 		entry->uip = NULL;
156811cccda1SJohn Dyson 	}
156911cccda1SJohn Dyson 
1570df8bae1dSRodney W. Grimes 	new_entry = vm_map_entry_create(map);
1571df8bae1dSRodney W. Grimes 	*new_entry = *entry;
1572df8bae1dSRodney W. Grimes 
1573df8bae1dSRodney W. Grimes 	new_entry->end = start;
1574df8bae1dSRodney W. Grimes 	entry->offset += (start - entry->start);
1575df8bae1dSRodney W. Grimes 	entry->start = start;
15763364c323SKonstantin Belousov 	if (new_entry->uip != NULL)
15773364c323SKonstantin Belousov 		uihold(entry->uip);
1578df8bae1dSRodney W. Grimes 
1579df8bae1dSRodney W. Grimes 	vm_map_entry_link(map, entry->prev, new_entry);
1580df8bae1dSRodney W. Grimes 
15819fdfe602SMatthew Dillon 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1582df8bae1dSRodney W. Grimes 		vm_object_reference(new_entry->object.vm_object);
1583df8bae1dSRodney W. Grimes 	}
1584c0877f10SJohn Dyson }
1585df8bae1dSRodney W. Grimes 
1586df8bae1dSRodney W. Grimes /*
1587df8bae1dSRodney W. Grimes  *	vm_map_clip_end:	[ internal use only ]
1588df8bae1dSRodney W. Grimes  *
1589df8bae1dSRodney W. Grimes  *	Asserts that the given entry ends at or before
1590df8bae1dSRodney W. Grimes  *	the specified address; if necessary,
1591df8bae1dSRodney W. Grimes  *	it splits the entry into two.
1592df8bae1dSRodney W. Grimes  */
1593df8bae1dSRodney W. Grimes #define vm_map_clip_end(map, entry, endaddr) \
1594df8bae1dSRodney W. Grimes { \
1595af045176SPoul-Henning Kamp 	if ((endaddr) < (entry->end)) \
1596af045176SPoul-Henning Kamp 		_vm_map_clip_end((map), (entry), (endaddr)); \
1597df8bae1dSRodney W. Grimes }
1598df8bae1dSRodney W. Grimes 
1599df8bae1dSRodney W. Grimes /*
1600df8bae1dSRodney W. Grimes  *	This routine is called only when it is known that
1601df8bae1dSRodney W. Grimes  *	the entry must be split.
1602df8bae1dSRodney W. Grimes  */
16030d94caffSDavid Greenman static void
16041b40f8c0SMatthew Dillon _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end)
1605df8bae1dSRodney W. Grimes {
1606c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
1607df8bae1dSRodney W. Grimes 
16083a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
16093a0916b8SKonstantin Belousov 
1610df8bae1dSRodney W. Grimes 	/*
161111cccda1SJohn Dyson 	 * If there is no object backing this entry, we might as well create
161211cccda1SJohn Dyson 	 * one now.  If we defer it, an object can get created after the map
161311cccda1SJohn Dyson 	 * is clipped, and individual objects will be created for the split-up
161411cccda1SJohn Dyson 	 * map.  This is a bit of a hack, but is also about the best place to
161511cccda1SJohn Dyson 	 * put this improvement.
161611cccda1SJohn Dyson 	 */
16174e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL && !map->system_map) {
161811cccda1SJohn Dyson 		vm_object_t object;
161911cccda1SJohn Dyson 		object = vm_object_allocate(OBJT_DEFAULT,
1620c2e11a03SJohn Dyson 				atop(entry->end - entry->start));
162111cccda1SJohn Dyson 		entry->object.vm_object = object;
162211cccda1SJohn Dyson 		entry->offset = 0;
16233364c323SKonstantin Belousov 		if (entry->uip != NULL) {
16243364c323SKonstantin Belousov 			object->uip = entry->uip;
16253364c323SKonstantin Belousov 			object->charge = entry->end - entry->start;
16263364c323SKonstantin Belousov 			entry->uip = NULL;
16273364c323SKonstantin Belousov 		}
16283364c323SKonstantin Belousov 	} else if (entry->object.vm_object != NULL &&
16293364c323SKonstantin Belousov 		   ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) &&
16303364c323SKonstantin Belousov 		   entry->uip != NULL) {
16313364c323SKonstantin Belousov 		VM_OBJECT_LOCK(entry->object.vm_object);
16323364c323SKonstantin Belousov 		KASSERT(entry->object.vm_object->uip == NULL,
16333364c323SKonstantin Belousov 		    ("OVERCOMMIT: vm_entry_clip_end: both uip e %p", entry));
16343364c323SKonstantin Belousov 		entry->object.vm_object->uip = entry->uip;
16353364c323SKonstantin Belousov 		entry->object.vm_object->charge = entry->end - entry->start;
16363364c323SKonstantin Belousov 		VM_OBJECT_UNLOCK(entry->object.vm_object);
16373364c323SKonstantin Belousov 		entry->uip = NULL;
163811cccda1SJohn Dyson 	}
163911cccda1SJohn Dyson 
164011cccda1SJohn Dyson 	/*
16410d94caffSDavid Greenman 	 * Create a new entry and insert it AFTER the specified entry
1642df8bae1dSRodney W. Grimes 	 */
1643df8bae1dSRodney W. Grimes 	new_entry = vm_map_entry_create(map);
1644df8bae1dSRodney W. Grimes 	*new_entry = *entry;
1645df8bae1dSRodney W. Grimes 
1646df8bae1dSRodney W. Grimes 	new_entry->start = entry->end = end;
1647df8bae1dSRodney W. Grimes 	new_entry->offset += (end - entry->start);
16483364c323SKonstantin Belousov 	if (new_entry->uip != NULL)
16493364c323SKonstantin Belousov 		uihold(entry->uip);
1650df8bae1dSRodney W. Grimes 
1651df8bae1dSRodney W. Grimes 	vm_map_entry_link(map, entry, new_entry);
1652df8bae1dSRodney W. Grimes 
16539fdfe602SMatthew Dillon 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1654df8bae1dSRodney W. Grimes 		vm_object_reference(new_entry->object.vm_object);
1655df8bae1dSRodney W. Grimes 	}
1656c0877f10SJohn Dyson }
1657df8bae1dSRodney W. Grimes 
1658df8bae1dSRodney W. Grimes /*
1659df8bae1dSRodney W. Grimes  *	vm_map_submap:		[ kernel use only ]
1660df8bae1dSRodney W. Grimes  *
1661df8bae1dSRodney W. Grimes  *	Mark the given range as handled by a subordinate map.
1662df8bae1dSRodney W. Grimes  *
1663df8bae1dSRodney W. Grimes  *	This range must have been created with vm_map_find,
1664df8bae1dSRodney W. Grimes  *	and no other operations may have been performed on this
1665df8bae1dSRodney W. Grimes  *	range prior to calling vm_map_submap.
1666df8bae1dSRodney W. Grimes  *
1667df8bae1dSRodney W. Grimes  *	Only a limited number of operations can be performed
1668df8bae1dSRodney W. Grimes  *	within this rage after calling vm_map_submap:
1669df8bae1dSRodney W. Grimes  *		vm_fault
1670df8bae1dSRodney W. Grimes  *	[Don't try vm_map_copy!]
1671df8bae1dSRodney W. Grimes  *
1672df8bae1dSRodney W. Grimes  *	To remove a submapping, one must first remove the
1673df8bae1dSRodney W. Grimes  *	range from the superior map, and then destroy the
1674df8bae1dSRodney W. Grimes  *	submap (if desired).  [Better yet, don't try it.]
1675df8bae1dSRodney W. Grimes  */
1676df8bae1dSRodney W. Grimes int
16771b40f8c0SMatthew Dillon vm_map_submap(
16781b40f8c0SMatthew Dillon 	vm_map_t map,
16791b40f8c0SMatthew Dillon 	vm_offset_t start,
16801b40f8c0SMatthew Dillon 	vm_offset_t end,
16811b40f8c0SMatthew Dillon 	vm_map_t submap)
1682df8bae1dSRodney W. Grimes {
1683df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
1684c0877f10SJohn Dyson 	int result = KERN_INVALID_ARGUMENT;
1685df8bae1dSRodney W. Grimes 
1686df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1687df8bae1dSRodney W. Grimes 
1688df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1689df8bae1dSRodney W. Grimes 
1690df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &entry)) {
1691df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
16920d94caffSDavid Greenman 	} else
1693df8bae1dSRodney W. Grimes 		entry = entry->next;
1694df8bae1dSRodney W. Grimes 
1695df8bae1dSRodney W. Grimes 	vm_map_clip_end(map, entry, end);
1696df8bae1dSRodney W. Grimes 
1697df8bae1dSRodney W. Grimes 	if ((entry->start == start) && (entry->end == end) &&
16989fdfe602SMatthew Dillon 	    ((entry->eflags & MAP_ENTRY_COW) == 0) &&
1699afa07f7eSJohn Dyson 	    (entry->object.vm_object == NULL)) {
17002d8acc0fSJohn Dyson 		entry->object.sub_map = submap;
1701afa07f7eSJohn Dyson 		entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
1702df8bae1dSRodney W. Grimes 		result = KERN_SUCCESS;
1703df8bae1dSRodney W. Grimes 	}
1704df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1705df8bae1dSRodney W. Grimes 
1706df8bae1dSRodney W. Grimes 	return (result);
1707df8bae1dSRodney W. Grimes }
1708df8bae1dSRodney W. Grimes 
1709df8bae1dSRodney W. Grimes /*
17101f78f902SAlan Cox  * The maximum number of pages to map
17111f78f902SAlan Cox  */
17121f78f902SAlan Cox #define	MAX_INIT_PT	96
17131f78f902SAlan Cox 
17141f78f902SAlan Cox /*
17150551c08dSAlan Cox  *	vm_map_pmap_enter:
17160551c08dSAlan Cox  *
1717cf4682aeSAlan Cox  *	Preload read-only mappings for the given object's resident pages into
1718cf4682aeSAlan Cox  *	the given map.  This eliminates the soft faults on process startup and
1719c4169725SAlan Cox  *	immediately after an mmap(2).  Because these are speculative mappings,
1720c4169725SAlan Cox  *	cached pages are not reactivated and mapped.
17210551c08dSAlan Cox  */
17220551c08dSAlan Cox void
17234da4d293SAlan Cox vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
17240551c08dSAlan Cox     vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags)
17250551c08dSAlan Cox {
17268fece8c3SAlan Cox 	vm_offset_t start;
1727ce142d9eSAlan Cox 	vm_page_t p, p_start;
17288fece8c3SAlan Cox 	vm_pindex_t psize, tmpidx;
17291f70d622SAlan Cox 	boolean_t are_queues_locked;
17300551c08dSAlan Cox 
1731ba8bca61SAlan Cox 	if ((prot & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 || object == NULL)
17321f78f902SAlan Cox 		return;
17331f78f902SAlan Cox 	VM_OBJECT_LOCK(object);
173401381811SJohn Baldwin 	if (object->type == OBJT_DEVICE || object->type == OBJT_SG) {
17351f78f902SAlan Cox 		pmap_object_init_pt(map->pmap, addr, object, pindex, size);
17361f78f902SAlan Cox 		goto unlock_return;
17371f78f902SAlan Cox 	}
17381f78f902SAlan Cox 
17391f78f902SAlan Cox 	psize = atop(size);
17401f78f902SAlan Cox 
17411136ed06SAlan Cox 	if ((flags & MAP_PREFAULT_PARTIAL) && psize > MAX_INIT_PT &&
17421136ed06SAlan Cox 	    object->resident_page_count > MAX_INIT_PT)
17431f78f902SAlan Cox 		goto unlock_return;
17441f78f902SAlan Cox 
17451f78f902SAlan Cox 	if (psize + pindex > object->size) {
17461f78f902SAlan Cox 		if (object->size < pindex)
17471f78f902SAlan Cox 			goto unlock_return;
17481f78f902SAlan Cox 		psize = object->size - pindex;
17491f78f902SAlan Cox 	}
17501f78f902SAlan Cox 
17511f70d622SAlan Cox 	are_queues_locked = FALSE;
1752ce142d9eSAlan Cox 	start = 0;
1753ce142d9eSAlan Cox 	p_start = NULL;
17541f78f902SAlan Cox 
17551f78f902SAlan Cox 	if ((p = TAILQ_FIRST(&object->memq)) != NULL) {
17561f78f902SAlan Cox 		if (p->pindex < pindex) {
17571f78f902SAlan Cox 			p = vm_page_splay(pindex, object->root);
17581f78f902SAlan Cox 			if ((object->root = p)->pindex < pindex)
17591f78f902SAlan Cox 				p = TAILQ_NEXT(p, listq);
17601f78f902SAlan Cox 		}
17611f78f902SAlan Cox 	}
17621f78f902SAlan Cox 	/*
17631f78f902SAlan Cox 	 * Assert: the variable p is either (1) the page with the
17641f78f902SAlan Cox 	 * least pindex greater than or equal to the parameter pindex
17651f78f902SAlan Cox 	 * or (2) NULL.
17661f78f902SAlan Cox 	 */
17671f78f902SAlan Cox 	for (;
17681f78f902SAlan Cox 	     p != NULL && (tmpidx = p->pindex - pindex) < psize;
17691f78f902SAlan Cox 	     p = TAILQ_NEXT(p, listq)) {
17701f78f902SAlan Cox 		/*
17711f78f902SAlan Cox 		 * don't allow an madvise to blow away our really
17721f78f902SAlan Cox 		 * free pages allocating pv entries.
17731f78f902SAlan Cox 		 */
17741f78f902SAlan Cox 		if ((flags & MAP_PREFAULT_MADVISE) &&
17752feb50bfSAttilio Rao 		    cnt.v_free_count < cnt.v_free_reserved) {
1776379fb642SAlan Cox 			psize = tmpidx;
17771f78f902SAlan Cox 			break;
17781f78f902SAlan Cox 		}
17790a2e596aSAlan Cox 		if (p->valid == VM_PAGE_BITS_ALL) {
1780ce142d9eSAlan Cox 			if (p_start == NULL) {
1781ce142d9eSAlan Cox 				start = addr + ptoa(tmpidx);
1782ce142d9eSAlan Cox 				p_start = p;
1783ce142d9eSAlan Cox 			}
17847bfda801SAlan Cox 		} else if (p_start != NULL) {
17851f70d622SAlan Cox 			if (!are_queues_locked) {
17861f70d622SAlan Cox 				are_queues_locked = TRUE;
178785f5b245SAlan Cox 				vm_page_lock_queues();
178885f5b245SAlan Cox 			}
1789cf4682aeSAlan Cox 			pmap_enter_object(map->pmap, start, addr +
1790cf4682aeSAlan Cox 			    ptoa(tmpidx), p_start, prot);
1791cf4682aeSAlan Cox 			p_start = NULL;
1792cf4682aeSAlan Cox 		}
1793cf4682aeSAlan Cox 	}
17947bfda801SAlan Cox 	if (p_start != NULL) {
17957bfda801SAlan Cox 		if (!are_queues_locked) {
17967bfda801SAlan Cox 			are_queues_locked = TRUE;
17977bfda801SAlan Cox 			vm_page_lock_queues();
17981f78f902SAlan Cox 		}
1799379fb642SAlan Cox 		pmap_enter_object(map->pmap, start, addr + ptoa(psize),
1800379fb642SAlan Cox 		    p_start, prot);
18017bfda801SAlan Cox 	}
18021f70d622SAlan Cox 	if (are_queues_locked)
18031f70d622SAlan Cox 		vm_page_unlock_queues();
18041f78f902SAlan Cox unlock_return:
18051f78f902SAlan Cox 	VM_OBJECT_UNLOCK(object);
18060551c08dSAlan Cox }
18070551c08dSAlan Cox 
18080551c08dSAlan Cox /*
1809df8bae1dSRodney W. Grimes  *	vm_map_protect:
1810df8bae1dSRodney W. Grimes  *
1811df8bae1dSRodney W. Grimes  *	Sets the protection of the specified address
1812df8bae1dSRodney W. Grimes  *	region in the target map.  If "set_max" is
1813df8bae1dSRodney W. Grimes  *	specified, the maximum protection is to be set;
1814df8bae1dSRodney W. Grimes  *	otherwise, only the current protection is affected.
1815df8bae1dSRodney W. Grimes  */
1816df8bae1dSRodney W. Grimes int
1817b9dcd593SBruce Evans vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
1818b9dcd593SBruce Evans 	       vm_prot_t new_prot, boolean_t set_max)
1819df8bae1dSRodney W. Grimes {
1820210a6886SKonstantin Belousov 	vm_map_entry_t current, entry;
18213364c323SKonstantin Belousov 	vm_object_t obj;
18223364c323SKonstantin Belousov 	struct uidinfo *uip;
1823210a6886SKonstantin Belousov 	vm_prot_t old_prot;
1824df8bae1dSRodney W. Grimes 
1825df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1826df8bae1dSRodney W. Grimes 
1827df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1828df8bae1dSRodney W. Grimes 
1829df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &entry)) {
1830df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
1831b7b2aac2SJohn Dyson 	} else {
1832df8bae1dSRodney W. Grimes 		entry = entry->next;
1833b7b2aac2SJohn Dyson 	}
1834df8bae1dSRodney W. Grimes 
1835df8bae1dSRodney W. Grimes 	/*
18360d94caffSDavid Greenman 	 * Make a first pass to check for protection violations.
1837df8bae1dSRodney W. Grimes 	 */
1838df8bae1dSRodney W. Grimes 	current = entry;
1839df8bae1dSRodney W. Grimes 	while ((current != &map->header) && (current->start < end)) {
1840afa07f7eSJohn Dyson 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1841a1f6d91cSDavid Greenman 			vm_map_unlock(map);
1842df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ARGUMENT);
1843a1f6d91cSDavid Greenman 		}
1844df8bae1dSRodney W. Grimes 		if ((new_prot & current->max_protection) != new_prot) {
1845df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
1846df8bae1dSRodney W. Grimes 			return (KERN_PROTECTION_FAILURE);
1847df8bae1dSRodney W. Grimes 		}
1848df8bae1dSRodney W. Grimes 		current = current->next;
1849df8bae1dSRodney W. Grimes 	}
1850df8bae1dSRodney W. Grimes 
18513364c323SKonstantin Belousov 
18523364c323SKonstantin Belousov 	/*
18533364c323SKonstantin Belousov 	 * Do an accounting pass for private read-only mappings that
18543364c323SKonstantin Belousov 	 * now will do cow due to allowed write (e.g. debugger sets
18553364c323SKonstantin Belousov 	 * breakpoint on text segment)
18563364c323SKonstantin Belousov 	 */
18573364c323SKonstantin Belousov 	for (current = entry; (current != &map->header) &&
18583364c323SKonstantin Belousov 	     (current->start < end); current = current->next) {
18593364c323SKonstantin Belousov 
18603364c323SKonstantin Belousov 		vm_map_clip_end(map, current, end);
18613364c323SKonstantin Belousov 
18623364c323SKonstantin Belousov 		if (set_max ||
18633364c323SKonstantin Belousov 		    ((new_prot & ~(current->protection)) & VM_PROT_WRITE) == 0 ||
18643364c323SKonstantin Belousov 		    ENTRY_CHARGED(current)) {
18653364c323SKonstantin Belousov 			continue;
18663364c323SKonstantin Belousov 		}
18673364c323SKonstantin Belousov 
18683364c323SKonstantin Belousov 		uip = curthread->td_ucred->cr_ruidinfo;
18693364c323SKonstantin Belousov 		obj = current->object.vm_object;
18703364c323SKonstantin Belousov 
18713364c323SKonstantin Belousov 		if (obj == NULL || (current->eflags & MAP_ENTRY_NEEDS_COPY)) {
18723364c323SKonstantin Belousov 			if (!swap_reserve(current->end - current->start)) {
18733364c323SKonstantin Belousov 				vm_map_unlock(map);
18743364c323SKonstantin Belousov 				return (KERN_RESOURCE_SHORTAGE);
18753364c323SKonstantin Belousov 			}
18763364c323SKonstantin Belousov 			uihold(uip);
18773364c323SKonstantin Belousov 			current->uip = uip;
18783364c323SKonstantin Belousov 			continue;
18793364c323SKonstantin Belousov 		}
18803364c323SKonstantin Belousov 
18813364c323SKonstantin Belousov 		VM_OBJECT_LOCK(obj);
18823364c323SKonstantin Belousov 		if (obj->type != OBJT_DEFAULT && obj->type != OBJT_SWAP) {
18833364c323SKonstantin Belousov 			VM_OBJECT_UNLOCK(obj);
18843364c323SKonstantin Belousov 			continue;
18853364c323SKonstantin Belousov 		}
18863364c323SKonstantin Belousov 
18873364c323SKonstantin Belousov 		/*
18883364c323SKonstantin Belousov 		 * Charge for the whole object allocation now, since
18893364c323SKonstantin Belousov 		 * we cannot distinguish between non-charged and
18903364c323SKonstantin Belousov 		 * charged clipped mapping of the same object later.
18913364c323SKonstantin Belousov 		 */
18923364c323SKonstantin Belousov 		KASSERT(obj->charge == 0,
18933364c323SKonstantin Belousov 		    ("vm_map_protect: object %p overcharged\n", obj));
18943364c323SKonstantin Belousov 		if (!swap_reserve(ptoa(obj->size))) {
18953364c323SKonstantin Belousov 			VM_OBJECT_UNLOCK(obj);
18963364c323SKonstantin Belousov 			vm_map_unlock(map);
18973364c323SKonstantin Belousov 			return (KERN_RESOURCE_SHORTAGE);
18983364c323SKonstantin Belousov 		}
18993364c323SKonstantin Belousov 
19003364c323SKonstantin Belousov 		uihold(uip);
19013364c323SKonstantin Belousov 		obj->uip = uip;
19023364c323SKonstantin Belousov 		obj->charge = ptoa(obj->size);
19033364c323SKonstantin Belousov 		VM_OBJECT_UNLOCK(obj);
19043364c323SKonstantin Belousov 	}
19053364c323SKonstantin Belousov 
1906df8bae1dSRodney W. Grimes 	/*
19070d94caffSDavid Greenman 	 * Go back and fix up protections. [Note that clipping is not
19080d94caffSDavid Greenman 	 * necessary the second time.]
1909df8bae1dSRodney W. Grimes 	 */
1910df8bae1dSRodney W. Grimes 	current = entry;
1911df8bae1dSRodney W. Grimes 	while ((current != &map->header) && (current->start < end)) {
1912df8bae1dSRodney W. Grimes 		old_prot = current->protection;
1913210a6886SKonstantin Belousov 
1914df8bae1dSRodney W. Grimes 		if (set_max)
1915df8bae1dSRodney W. Grimes 			current->protection =
1916df8bae1dSRodney W. Grimes 			    (current->max_protection = new_prot) &
1917df8bae1dSRodney W. Grimes 			    old_prot;
1918df8bae1dSRodney W. Grimes 		else
1919df8bae1dSRodney W. Grimes 			current->protection = new_prot;
1920df8bae1dSRodney W. Grimes 
1921210a6886SKonstantin Belousov 		if ((current->eflags & (MAP_ENTRY_COW | MAP_ENTRY_USER_WIRED))
1922210a6886SKonstantin Belousov 		     == (MAP_ENTRY_COW | MAP_ENTRY_USER_WIRED) &&
1923210a6886SKonstantin Belousov 		    (current->protection & VM_PROT_WRITE) != 0 &&
1924210a6886SKonstantin Belousov 		    (old_prot & VM_PROT_WRITE) == 0) {
1925210a6886SKonstantin Belousov 			vm_fault_copy_entry(map, map, current, current, NULL);
1926210a6886SKonstantin Belousov 		}
1927210a6886SKonstantin Belousov 
1928df8bae1dSRodney W. Grimes 		/*
19292fafce9eSAlan Cox 		 * When restricting access, update the physical map.  Worry
19302fafce9eSAlan Cox 		 * about copy-on-write here.
1931df8bae1dSRodney W. Grimes 		 */
19322fafce9eSAlan Cox 		if ((old_prot & ~current->protection) != 0) {
1933afa07f7eSJohn Dyson #define MASK(entry)	(((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1934df8bae1dSRodney W. Grimes 							VM_PROT_ALL)
1935df8bae1dSRodney W. Grimes 			pmap_protect(map->pmap, current->start,
1936df8bae1dSRodney W. Grimes 			    current->end,
19371c85e3dfSAlan Cox 			    current->protection & MASK(current));
1938df8bae1dSRodney W. Grimes #undef	MASK
1939df8bae1dSRodney W. Grimes 		}
19407d78abc9SJohn Dyson 		vm_map_simplify_entry(map, current);
1941df8bae1dSRodney W. Grimes 		current = current->next;
1942df8bae1dSRodney W. Grimes 	}
1943df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1944df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
1945df8bae1dSRodney W. Grimes }
1946df8bae1dSRodney W. Grimes 
1947df8bae1dSRodney W. Grimes /*
1948867a482dSJohn Dyson  *	vm_map_madvise:
1949867a482dSJohn Dyson  *
1950867a482dSJohn Dyson  *	This routine traverses a processes map handling the madvise
1951f7fc307aSAlan Cox  *	system call.  Advisories are classified as either those effecting
1952f7fc307aSAlan Cox  *	the vm_map_entry structure, or those effecting the underlying
1953f7fc307aSAlan Cox  *	objects.
1954867a482dSJohn Dyson  */
1955b4309055SMatthew Dillon int
19561b40f8c0SMatthew Dillon vm_map_madvise(
19571b40f8c0SMatthew Dillon 	vm_map_t map,
19581b40f8c0SMatthew Dillon 	vm_offset_t start,
19591b40f8c0SMatthew Dillon 	vm_offset_t end,
19601b40f8c0SMatthew Dillon 	int behav)
1961867a482dSJohn Dyson {
1962f7fc307aSAlan Cox 	vm_map_entry_t current, entry;
1963b4309055SMatthew Dillon 	int modify_map = 0;
1964867a482dSJohn Dyson 
1965b4309055SMatthew Dillon 	/*
1966b4309055SMatthew Dillon 	 * Some madvise calls directly modify the vm_map_entry, in which case
1967b4309055SMatthew Dillon 	 * we need to use an exclusive lock on the map and we need to perform
1968b4309055SMatthew Dillon 	 * various clipping operations.  Otherwise we only need a read-lock
1969b4309055SMatthew Dillon 	 * on the map.
1970b4309055SMatthew Dillon 	 */
1971b4309055SMatthew Dillon 	switch(behav) {
1972b4309055SMatthew Dillon 	case MADV_NORMAL:
1973b4309055SMatthew Dillon 	case MADV_SEQUENTIAL:
1974b4309055SMatthew Dillon 	case MADV_RANDOM:
19754f79d873SMatthew Dillon 	case MADV_NOSYNC:
19764f79d873SMatthew Dillon 	case MADV_AUTOSYNC:
19779730a5daSPaul Saab 	case MADV_NOCORE:
19789730a5daSPaul Saab 	case MADV_CORE:
1979b4309055SMatthew Dillon 		modify_map = 1;
1980867a482dSJohn Dyson 		vm_map_lock(map);
1981b4309055SMatthew Dillon 		break;
1982b4309055SMatthew Dillon 	case MADV_WILLNEED:
1983b4309055SMatthew Dillon 	case MADV_DONTNEED:
1984b4309055SMatthew Dillon 	case MADV_FREE:
1985f7fc307aSAlan Cox 		vm_map_lock_read(map);
1986b4309055SMatthew Dillon 		break;
1987b4309055SMatthew Dillon 	default:
1988b4309055SMatthew Dillon 		return (KERN_INVALID_ARGUMENT);
1989b4309055SMatthew Dillon 	}
1990b4309055SMatthew Dillon 
1991b4309055SMatthew Dillon 	/*
1992b4309055SMatthew Dillon 	 * Locate starting entry and clip if necessary.
1993b4309055SMatthew Dillon 	 */
1994867a482dSJohn Dyson 	VM_MAP_RANGE_CHECK(map, start, end);
1995867a482dSJohn Dyson 
1996867a482dSJohn Dyson 	if (vm_map_lookup_entry(map, start, &entry)) {
1997f7fc307aSAlan Cox 		if (modify_map)
1998867a482dSJohn Dyson 			vm_map_clip_start(map, entry, start);
1999b4309055SMatthew Dillon 	} else {
2000867a482dSJohn Dyson 		entry = entry->next;
2001b4309055SMatthew Dillon 	}
2002867a482dSJohn Dyson 
2003f7fc307aSAlan Cox 	if (modify_map) {
2004f7fc307aSAlan Cox 		/*
2005f7fc307aSAlan Cox 		 * madvise behaviors that are implemented in the vm_map_entry.
2006f7fc307aSAlan Cox 		 *
2007f7fc307aSAlan Cox 		 * We clip the vm_map_entry so that behavioral changes are
2008f7fc307aSAlan Cox 		 * limited to the specified address range.
2009f7fc307aSAlan Cox 		 */
2010867a482dSJohn Dyson 		for (current = entry;
2011867a482dSJohn Dyson 		     (current != &map->header) && (current->start < end);
2012b4309055SMatthew Dillon 		     current = current->next
2013b4309055SMatthew Dillon 		) {
2014f7fc307aSAlan Cox 			if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
2015867a482dSJohn Dyson 				continue;
2016fed9a903SJohn Dyson 
201747221757SJohn Dyson 			vm_map_clip_end(map, current, end);
2018fed9a903SJohn Dyson 
2019f7fc307aSAlan Cox 			switch (behav) {
2020867a482dSJohn Dyson 			case MADV_NORMAL:
20217f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
2022867a482dSJohn Dyson 				break;
2023867a482dSJohn Dyson 			case MADV_SEQUENTIAL:
20247f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
2025867a482dSJohn Dyson 				break;
2026867a482dSJohn Dyson 			case MADV_RANDOM:
20277f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
2028867a482dSJohn Dyson 				break;
20294f79d873SMatthew Dillon 			case MADV_NOSYNC:
20304f79d873SMatthew Dillon 				current->eflags |= MAP_ENTRY_NOSYNC;
20314f79d873SMatthew Dillon 				break;
20324f79d873SMatthew Dillon 			case MADV_AUTOSYNC:
20334f79d873SMatthew Dillon 				current->eflags &= ~MAP_ENTRY_NOSYNC;
20344f79d873SMatthew Dillon 				break;
20359730a5daSPaul Saab 			case MADV_NOCORE:
20369730a5daSPaul Saab 				current->eflags |= MAP_ENTRY_NOCOREDUMP;
20379730a5daSPaul Saab 				break;
20389730a5daSPaul Saab 			case MADV_CORE:
20399730a5daSPaul Saab 				current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
20409730a5daSPaul Saab 				break;
2041867a482dSJohn Dyson 			default:
2042867a482dSJohn Dyson 				break;
2043867a482dSJohn Dyson 			}
2044f7fc307aSAlan Cox 			vm_map_simplify_entry(map, current);
2045867a482dSJohn Dyson 		}
2046867a482dSJohn Dyson 		vm_map_unlock(map);
2047b4309055SMatthew Dillon 	} else {
2048f7fc307aSAlan Cox 		vm_pindex_t pindex;
2049f7fc307aSAlan Cox 		int count;
2050f7fc307aSAlan Cox 
2051f7fc307aSAlan Cox 		/*
2052f7fc307aSAlan Cox 		 * madvise behaviors that are implemented in the underlying
2053f7fc307aSAlan Cox 		 * vm_object.
2054f7fc307aSAlan Cox 		 *
2055f7fc307aSAlan Cox 		 * Since we don't clip the vm_map_entry, we have to clip
2056f7fc307aSAlan Cox 		 * the vm_object pindex and count.
2057f7fc307aSAlan Cox 		 */
2058f7fc307aSAlan Cox 		for (current = entry;
2059f7fc307aSAlan Cox 		     (current != &map->header) && (current->start < end);
2060b4309055SMatthew Dillon 		     current = current->next
2061b4309055SMatthew Dillon 		) {
20625f99b57cSMatthew Dillon 			vm_offset_t useStart;
20635f99b57cSMatthew Dillon 
2064f7fc307aSAlan Cox 			if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
2065f7fc307aSAlan Cox 				continue;
2066f7fc307aSAlan Cox 
2067f7fc307aSAlan Cox 			pindex = OFF_TO_IDX(current->offset);
2068f7fc307aSAlan Cox 			count = atop(current->end - current->start);
20695f99b57cSMatthew Dillon 			useStart = current->start;
2070f7fc307aSAlan Cox 
2071f7fc307aSAlan Cox 			if (current->start < start) {
2072f7fc307aSAlan Cox 				pindex += atop(start - current->start);
2073f7fc307aSAlan Cox 				count -= atop(start - current->start);
20745f99b57cSMatthew Dillon 				useStart = start;
2075f7fc307aSAlan Cox 			}
2076f7fc307aSAlan Cox 			if (current->end > end)
2077f7fc307aSAlan Cox 				count -= atop(current->end - end);
2078f7fc307aSAlan Cox 
2079f7fc307aSAlan Cox 			if (count <= 0)
2080f7fc307aSAlan Cox 				continue;
2081f7fc307aSAlan Cox 
2082f7fc307aSAlan Cox 			vm_object_madvise(current->object.vm_object,
2083f7fc307aSAlan Cox 					  pindex, count, behav);
2084b4309055SMatthew Dillon 			if (behav == MADV_WILLNEED) {
20850551c08dSAlan Cox 				vm_map_pmap_enter(map,
20865f99b57cSMatthew Dillon 				    useStart,
20874da4d293SAlan Cox 				    current->protection,
2088f7fc307aSAlan Cox 				    current->object.vm_object,
2089b4309055SMatthew Dillon 				    pindex,
2090b4309055SMatthew Dillon 				    (count << PAGE_SHIFT),
2091e3026983SMatthew Dillon 				    MAP_PREFAULT_MADVISE
2092b4309055SMatthew Dillon 				);
2093f7fc307aSAlan Cox 			}
2094f7fc307aSAlan Cox 		}
2095f7fc307aSAlan Cox 		vm_map_unlock_read(map);
2096f7fc307aSAlan Cox 	}
2097b4309055SMatthew Dillon 	return (0);
2098867a482dSJohn Dyson }
2099867a482dSJohn Dyson 
2100867a482dSJohn Dyson 
2101867a482dSJohn Dyson /*
2102df8bae1dSRodney W. Grimes  *	vm_map_inherit:
2103df8bae1dSRodney W. Grimes  *
2104df8bae1dSRodney W. Grimes  *	Sets the inheritance of the specified address
2105df8bae1dSRodney W. Grimes  *	range in the target map.  Inheritance
2106df8bae1dSRodney W. Grimes  *	affects how the map will be shared with
2107e2abaaaaSAlan Cox  *	child maps at the time of vmspace_fork.
2108df8bae1dSRodney W. Grimes  */
2109df8bae1dSRodney W. Grimes int
2110b9dcd593SBruce Evans vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
2111b9dcd593SBruce Evans 	       vm_inherit_t new_inheritance)
2112df8bae1dSRodney W. Grimes {
2113c0877f10SJohn Dyson 	vm_map_entry_t entry;
2114df8bae1dSRodney W. Grimes 	vm_map_entry_t temp_entry;
2115df8bae1dSRodney W. Grimes 
2116df8bae1dSRodney W. Grimes 	switch (new_inheritance) {
2117df8bae1dSRodney W. Grimes 	case VM_INHERIT_NONE:
2118df8bae1dSRodney W. Grimes 	case VM_INHERIT_COPY:
2119df8bae1dSRodney W. Grimes 	case VM_INHERIT_SHARE:
2120df8bae1dSRodney W. Grimes 		break;
2121df8bae1dSRodney W. Grimes 	default:
2122df8bae1dSRodney W. Grimes 		return (KERN_INVALID_ARGUMENT);
2123df8bae1dSRodney W. Grimes 	}
2124df8bae1dSRodney W. Grimes 	vm_map_lock(map);
2125df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
2126df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &temp_entry)) {
2127df8bae1dSRodney W. Grimes 		entry = temp_entry;
2128df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
21290d94caffSDavid Greenman 	} else
2130df8bae1dSRodney W. Grimes 		entry = temp_entry->next;
2131df8bae1dSRodney W. Grimes 	while ((entry != &map->header) && (entry->start < end)) {
2132df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, entry, end);
2133df8bae1dSRodney W. Grimes 		entry->inheritance = new_inheritance;
213444428f62SAlan Cox 		vm_map_simplify_entry(map, entry);
2135df8bae1dSRodney W. Grimes 		entry = entry->next;
2136df8bae1dSRodney W. Grimes 	}
2137df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
2138df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
2139df8bae1dSRodney W. Grimes }
2140df8bae1dSRodney W. Grimes 
2141df8bae1dSRodney W. Grimes /*
2142acd9a301SAlan Cox  *	vm_map_unwire:
2143acd9a301SAlan Cox  *
2144e27e17b7SAlan Cox  *	Implements both kernel and user unwiring.
2145acd9a301SAlan Cox  */
2146acd9a301SAlan Cox int
2147acd9a301SAlan Cox vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
2148abd498aaSBruce M Simpson     int flags)
2149acd9a301SAlan Cox {
2150acd9a301SAlan Cox 	vm_map_entry_t entry, first_entry, tmp_entry;
2151acd9a301SAlan Cox 	vm_offset_t saved_start;
2152acd9a301SAlan Cox 	unsigned int last_timestamp;
2153acd9a301SAlan Cox 	int rv;
2154abd498aaSBruce M Simpson 	boolean_t need_wakeup, result, user_unwire;
2155acd9a301SAlan Cox 
2156abd498aaSBruce M Simpson 	user_unwire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
2157acd9a301SAlan Cox 	vm_map_lock(map);
2158acd9a301SAlan Cox 	VM_MAP_RANGE_CHECK(map, start, end);
2159acd9a301SAlan Cox 	if (!vm_map_lookup_entry(map, start, &first_entry)) {
2160abd498aaSBruce M Simpson 		if (flags & VM_MAP_WIRE_HOLESOK)
2161cbef13d8SAlan Cox 			first_entry = first_entry->next;
2162abd498aaSBruce M Simpson 		else {
2163acd9a301SAlan Cox 			vm_map_unlock(map);
2164acd9a301SAlan Cox 			return (KERN_INVALID_ADDRESS);
2165acd9a301SAlan Cox 		}
2166abd498aaSBruce M Simpson 	}
2167acd9a301SAlan Cox 	last_timestamp = map->timestamp;
2168acd9a301SAlan Cox 	entry = first_entry;
2169acd9a301SAlan Cox 	while (entry != &map->header && entry->start < end) {
2170acd9a301SAlan Cox 		if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
2171acd9a301SAlan Cox 			/*
2172acd9a301SAlan Cox 			 * We have not yet clipped the entry.
2173acd9a301SAlan Cox 			 */
2174acd9a301SAlan Cox 			saved_start = (start >= entry->start) ? start :
2175acd9a301SAlan Cox 			    entry->start;
2176acd9a301SAlan Cox 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
21778ce2d00aSPawel Jakub Dawidek 			if (vm_map_unlock_and_wait(map, 0)) {
2178acd9a301SAlan Cox 				/*
2179acd9a301SAlan Cox 				 * Allow interruption of user unwiring?
2180acd9a301SAlan Cox 				 */
2181acd9a301SAlan Cox 			}
2182acd9a301SAlan Cox 			vm_map_lock(map);
2183acd9a301SAlan Cox 			if (last_timestamp+1 != map->timestamp) {
2184acd9a301SAlan Cox 				/*
2185acd9a301SAlan Cox 				 * Look again for the entry because the map was
2186acd9a301SAlan Cox 				 * modified while it was unlocked.
2187acd9a301SAlan Cox 				 * Specifically, the entry may have been
2188acd9a301SAlan Cox 				 * clipped, merged, or deleted.
2189acd9a301SAlan Cox 				 */
2190acd9a301SAlan Cox 				if (!vm_map_lookup_entry(map, saved_start,
2191acd9a301SAlan Cox 				    &tmp_entry)) {
2192cbef13d8SAlan Cox 					if (flags & VM_MAP_WIRE_HOLESOK)
2193cbef13d8SAlan Cox 						tmp_entry = tmp_entry->next;
2194cbef13d8SAlan Cox 					else {
2195acd9a301SAlan Cox 						if (saved_start == start) {
2196acd9a301SAlan Cox 							/*
2197acd9a301SAlan Cox 							 * First_entry has been deleted.
2198acd9a301SAlan Cox 							 */
2199acd9a301SAlan Cox 							vm_map_unlock(map);
2200acd9a301SAlan Cox 							return (KERN_INVALID_ADDRESS);
2201acd9a301SAlan Cox 						}
2202acd9a301SAlan Cox 						end = saved_start;
2203acd9a301SAlan Cox 						rv = KERN_INVALID_ADDRESS;
2204acd9a301SAlan Cox 						goto done;
2205acd9a301SAlan Cox 					}
2206cbef13d8SAlan Cox 				}
2207acd9a301SAlan Cox 				if (entry == first_entry)
2208acd9a301SAlan Cox 					first_entry = tmp_entry;
2209acd9a301SAlan Cox 				else
2210acd9a301SAlan Cox 					first_entry = NULL;
2211acd9a301SAlan Cox 				entry = tmp_entry;
2212acd9a301SAlan Cox 			}
2213acd9a301SAlan Cox 			last_timestamp = map->timestamp;
2214acd9a301SAlan Cox 			continue;
2215acd9a301SAlan Cox 		}
2216acd9a301SAlan Cox 		vm_map_clip_start(map, entry, start);
2217acd9a301SAlan Cox 		vm_map_clip_end(map, entry, end);
2218acd9a301SAlan Cox 		/*
2219acd9a301SAlan Cox 		 * Mark the entry in case the map lock is released.  (See
2220acd9a301SAlan Cox 		 * above.)
2221acd9a301SAlan Cox 		 */
2222acd9a301SAlan Cox 		entry->eflags |= MAP_ENTRY_IN_TRANSITION;
2223acd9a301SAlan Cox 		/*
2224acd9a301SAlan Cox 		 * Check the map for holes in the specified region.
2225abd498aaSBruce M Simpson 		 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
2226acd9a301SAlan Cox 		 */
2227abd498aaSBruce M Simpson 		if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
2228abd498aaSBruce M Simpson 		    (entry->end < end && (entry->next == &map->header ||
2229abd498aaSBruce M Simpson 		    entry->next->start > entry->end))) {
2230acd9a301SAlan Cox 			end = entry->end;
2231acd9a301SAlan Cox 			rv = KERN_INVALID_ADDRESS;
2232acd9a301SAlan Cox 			goto done;
2233acd9a301SAlan Cox 		}
2234acd9a301SAlan Cox 		/*
22353ffbc0cdSAlan Cox 		 * If system unwiring, require that the entry is system wired.
2236acd9a301SAlan Cox 		 */
22370ada205eSBrian Feldman 		if (!user_unwire &&
22380ada205eSBrian Feldman 		    vm_map_entry_system_wired_count(entry) == 0) {
2239acd9a301SAlan Cox 			end = entry->end;
2240acd9a301SAlan Cox 			rv = KERN_INVALID_ARGUMENT;
2241acd9a301SAlan Cox 			goto done;
2242acd9a301SAlan Cox 		}
2243acd9a301SAlan Cox 		entry = entry->next;
2244acd9a301SAlan Cox 	}
2245acd9a301SAlan Cox 	rv = KERN_SUCCESS;
2246acd9a301SAlan Cox done:
2247e27e17b7SAlan Cox 	need_wakeup = FALSE;
2248acd9a301SAlan Cox 	if (first_entry == NULL) {
2249acd9a301SAlan Cox 		result = vm_map_lookup_entry(map, start, &first_entry);
2250cbef13d8SAlan Cox 		if (!result && (flags & VM_MAP_WIRE_HOLESOK))
2251cbef13d8SAlan Cox 			first_entry = first_entry->next;
2252cbef13d8SAlan Cox 		else
2253acd9a301SAlan Cox 			KASSERT(result, ("vm_map_unwire: lookup failed"));
2254acd9a301SAlan Cox 	}
2255acd9a301SAlan Cox 	entry = first_entry;
2256acd9a301SAlan Cox 	while (entry != &map->header && entry->start < end) {
22573ffbc0cdSAlan Cox 		if (rv == KERN_SUCCESS && (!user_unwire ||
22583ffbc0cdSAlan Cox 		    (entry->eflags & MAP_ENTRY_USER_WIRED))) {
2259b2f3846aSAlan Cox 			if (user_unwire)
2260b2f3846aSAlan Cox 				entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2261b2f3846aSAlan Cox 			entry->wired_count--;
22620ada205eSBrian Feldman 			if (entry->wired_count == 0) {
2263b2f3846aSAlan Cox 				/*
2264b2f3846aSAlan Cox 				 * Retain the map lock.
2265b2f3846aSAlan Cox 				 */
22664be14af9SAlan Cox 				vm_fault_unwire(map, entry->start, entry->end,
22674be14af9SAlan Cox 				    entry->object.vm_object != NULL &&
226801381811SJohn Baldwin 				    (entry->object.vm_object->type == OBJT_DEVICE ||
226901381811SJohn Baldwin 				    entry->object.vm_object->type == OBJT_SG));
2270b2f3846aSAlan Cox 			}
2271b2f3846aSAlan Cox 		}
2272acd9a301SAlan Cox 		KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
2273acd9a301SAlan Cox 			("vm_map_unwire: in-transition flag missing"));
2274acd9a301SAlan Cox 		entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
2275acd9a301SAlan Cox 		if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
2276acd9a301SAlan Cox 			entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
2277acd9a301SAlan Cox 			need_wakeup = TRUE;
2278acd9a301SAlan Cox 		}
2279acd9a301SAlan Cox 		vm_map_simplify_entry(map, entry);
2280acd9a301SAlan Cox 		entry = entry->next;
2281acd9a301SAlan Cox 	}
2282acd9a301SAlan Cox 	vm_map_unlock(map);
2283acd9a301SAlan Cox 	if (need_wakeup)
2284acd9a301SAlan Cox 		vm_map_wakeup(map);
2285acd9a301SAlan Cox 	return (rv);
2286acd9a301SAlan Cox }
2287acd9a301SAlan Cox 
2288acd9a301SAlan Cox /*
2289e27e17b7SAlan Cox  *	vm_map_wire:
2290e27e17b7SAlan Cox  *
2291e27e17b7SAlan Cox  *	Implements both kernel and user wiring.
2292e27e17b7SAlan Cox  */
2293e27e17b7SAlan Cox int
2294e27e17b7SAlan Cox vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
2295abd498aaSBruce M Simpson     int flags)
2296e27e17b7SAlan Cox {
229712d7cc84SAlan Cox 	vm_map_entry_t entry, first_entry, tmp_entry;
229812d7cc84SAlan Cox 	vm_offset_t saved_end, saved_start;
229912d7cc84SAlan Cox 	unsigned int last_timestamp;
230012d7cc84SAlan Cox 	int rv;
23014be14af9SAlan Cox 	boolean_t fictitious, need_wakeup, result, user_wire;
2302e27e17b7SAlan Cox 
2303abd498aaSBruce M Simpson 	user_wire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
230412d7cc84SAlan Cox 	vm_map_lock(map);
230512d7cc84SAlan Cox 	VM_MAP_RANGE_CHECK(map, start, end);
230612d7cc84SAlan Cox 	if (!vm_map_lookup_entry(map, start, &first_entry)) {
2307abd498aaSBruce M Simpson 		if (flags & VM_MAP_WIRE_HOLESOK)
2308cbef13d8SAlan Cox 			first_entry = first_entry->next;
2309abd498aaSBruce M Simpson 		else {
231012d7cc84SAlan Cox 			vm_map_unlock(map);
231112d7cc84SAlan Cox 			return (KERN_INVALID_ADDRESS);
231212d7cc84SAlan Cox 		}
2313abd498aaSBruce M Simpson 	}
231412d7cc84SAlan Cox 	last_timestamp = map->timestamp;
231512d7cc84SAlan Cox 	entry = first_entry;
231612d7cc84SAlan Cox 	while (entry != &map->header && entry->start < end) {
231712d7cc84SAlan Cox 		if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
231812d7cc84SAlan Cox 			/*
231912d7cc84SAlan Cox 			 * We have not yet clipped the entry.
232012d7cc84SAlan Cox 			 */
232112d7cc84SAlan Cox 			saved_start = (start >= entry->start) ? start :
232212d7cc84SAlan Cox 			    entry->start;
232312d7cc84SAlan Cox 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
23248ce2d00aSPawel Jakub Dawidek 			if (vm_map_unlock_and_wait(map, 0)) {
232512d7cc84SAlan Cox 				/*
232612d7cc84SAlan Cox 				 * Allow interruption of user wiring?
232712d7cc84SAlan Cox 				 */
232812d7cc84SAlan Cox 			}
232912d7cc84SAlan Cox 			vm_map_lock(map);
233012d7cc84SAlan Cox 			if (last_timestamp + 1 != map->timestamp) {
233112d7cc84SAlan Cox 				/*
233212d7cc84SAlan Cox 				 * Look again for the entry because the map was
233312d7cc84SAlan Cox 				 * modified while it was unlocked.
233412d7cc84SAlan Cox 				 * Specifically, the entry may have been
233512d7cc84SAlan Cox 				 * clipped, merged, or deleted.
233612d7cc84SAlan Cox 				 */
233712d7cc84SAlan Cox 				if (!vm_map_lookup_entry(map, saved_start,
233812d7cc84SAlan Cox 				    &tmp_entry)) {
2339cbef13d8SAlan Cox 					if (flags & VM_MAP_WIRE_HOLESOK)
2340cbef13d8SAlan Cox 						tmp_entry = tmp_entry->next;
2341cbef13d8SAlan Cox 					else {
234212d7cc84SAlan Cox 						if (saved_start == start) {
234312d7cc84SAlan Cox 							/*
234412d7cc84SAlan Cox 							 * first_entry has been deleted.
234512d7cc84SAlan Cox 							 */
234612d7cc84SAlan Cox 							vm_map_unlock(map);
234712d7cc84SAlan Cox 							return (KERN_INVALID_ADDRESS);
234812d7cc84SAlan Cox 						}
234912d7cc84SAlan Cox 						end = saved_start;
235012d7cc84SAlan Cox 						rv = KERN_INVALID_ADDRESS;
235112d7cc84SAlan Cox 						goto done;
235212d7cc84SAlan Cox 					}
2353cbef13d8SAlan Cox 				}
235412d7cc84SAlan Cox 				if (entry == first_entry)
235512d7cc84SAlan Cox 					first_entry = tmp_entry;
235612d7cc84SAlan Cox 				else
235712d7cc84SAlan Cox 					first_entry = NULL;
235812d7cc84SAlan Cox 				entry = tmp_entry;
235912d7cc84SAlan Cox 			}
236012d7cc84SAlan Cox 			last_timestamp = map->timestamp;
236112d7cc84SAlan Cox 			continue;
236212d7cc84SAlan Cox 		}
236312d7cc84SAlan Cox 		vm_map_clip_start(map, entry, start);
236412d7cc84SAlan Cox 		vm_map_clip_end(map, entry, end);
236512d7cc84SAlan Cox 		/*
236612d7cc84SAlan Cox 		 * Mark the entry in case the map lock is released.  (See
236712d7cc84SAlan Cox 		 * above.)
236812d7cc84SAlan Cox 		 */
236912d7cc84SAlan Cox 		entry->eflags |= MAP_ENTRY_IN_TRANSITION;
237012d7cc84SAlan Cox 		/*
23710ada205eSBrian Feldman 		 *
237212d7cc84SAlan Cox 		 */
23730ada205eSBrian Feldman 		if (entry->wired_count == 0) {
23746d7e8091SKonstantin Belousov 			if ((entry->protection & (VM_PROT_READ|VM_PROT_EXECUTE))
23756d7e8091SKonstantin Belousov 			    == 0) {
2376529ab57bSKonstantin Belousov 				entry->eflags |= MAP_ENTRY_WIRE_SKIPPED;
23776d7e8091SKonstantin Belousov 				if ((flags & VM_MAP_WIRE_HOLESOK) == 0) {
23786d7e8091SKonstantin Belousov 					end = entry->end;
23796d7e8091SKonstantin Belousov 					rv = KERN_INVALID_ADDRESS;
23806d7e8091SKonstantin Belousov 					goto done;
23816d7e8091SKonstantin Belousov 				}
23826d7e8091SKonstantin Belousov 				goto next_entry;
23836d7e8091SKonstantin Belousov 			}
23840ada205eSBrian Feldman 			entry->wired_count++;
238512d7cc84SAlan Cox 			saved_start = entry->start;
238612d7cc84SAlan Cox 			saved_end = entry->end;
23874be14af9SAlan Cox 			fictitious = entry->object.vm_object != NULL &&
238801381811SJohn Baldwin 			    (entry->object.vm_object->type == OBJT_DEVICE ||
238901381811SJohn Baldwin 			    entry->object.vm_object->type == OBJT_SG);
239012d7cc84SAlan Cox 			/*
239112d7cc84SAlan Cox 			 * Release the map lock, relying on the in-transition
239212d7cc84SAlan Cox 			 * mark.
239312d7cc84SAlan Cox 			 */
239412d7cc84SAlan Cox 			vm_map_unlock(map);
2395ef594d31SAlan Cox 			rv = vm_fault_wire(map, saved_start, saved_end,
23962db65ab4SAlan Cox 			    fictitious);
239712d7cc84SAlan Cox 			vm_map_lock(map);
239812d7cc84SAlan Cox 			if (last_timestamp + 1 != map->timestamp) {
239912d7cc84SAlan Cox 				/*
240012d7cc84SAlan Cox 				 * Look again for the entry because the map was
240112d7cc84SAlan Cox 				 * modified while it was unlocked.  The entry
240212d7cc84SAlan Cox 				 * may have been clipped, but NOT merged or
240312d7cc84SAlan Cox 				 * deleted.
240412d7cc84SAlan Cox 				 */
240512d7cc84SAlan Cox 				result = vm_map_lookup_entry(map, saved_start,
240612d7cc84SAlan Cox 				    &tmp_entry);
240712d7cc84SAlan Cox 				KASSERT(result, ("vm_map_wire: lookup failed"));
240812d7cc84SAlan Cox 				if (entry == first_entry)
240912d7cc84SAlan Cox 					first_entry = tmp_entry;
241012d7cc84SAlan Cox 				else
241112d7cc84SAlan Cox 					first_entry = NULL;
241212d7cc84SAlan Cox 				entry = tmp_entry;
241328c58286SAlan Cox 				while (entry->end < saved_end) {
241428c58286SAlan Cox 					if (rv != KERN_SUCCESS) {
241528c58286SAlan Cox 						KASSERT(entry->wired_count == 1,
241628c58286SAlan Cox 						    ("vm_map_wire: bad count"));
241728c58286SAlan Cox 						entry->wired_count = -1;
241828c58286SAlan Cox 					}
241912d7cc84SAlan Cox 					entry = entry->next;
242012d7cc84SAlan Cox 				}
242128c58286SAlan Cox 			}
242212d7cc84SAlan Cox 			last_timestamp = map->timestamp;
242312d7cc84SAlan Cox 			if (rv != KERN_SUCCESS) {
242428c58286SAlan Cox 				KASSERT(entry->wired_count == 1,
242528c58286SAlan Cox 				    ("vm_map_wire: bad count"));
242612d7cc84SAlan Cox 				/*
242728c58286SAlan Cox 				 * Assign an out-of-range value to represent
242828c58286SAlan Cox 				 * the failure to wire this entry.
242912d7cc84SAlan Cox 				 */
243028c58286SAlan Cox 				entry->wired_count = -1;
243112d7cc84SAlan Cox 				end = entry->end;
243212d7cc84SAlan Cox 				goto done;
243312d7cc84SAlan Cox 			}
24340ada205eSBrian Feldman 		} else if (!user_wire ||
24350ada205eSBrian Feldman 			   (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
24360ada205eSBrian Feldman 			entry->wired_count++;
243712d7cc84SAlan Cox 		}
243812d7cc84SAlan Cox 		/*
243912d7cc84SAlan Cox 		 * Check the map for holes in the specified region.
2440abd498aaSBruce M Simpson 		 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
244112d7cc84SAlan Cox 		 */
24426d7e8091SKonstantin Belousov 	next_entry:
2443abd498aaSBruce M Simpson 		if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
2444abd498aaSBruce M Simpson 		    (entry->end < end && (entry->next == &map->header ||
2445abd498aaSBruce M Simpson 		    entry->next->start > entry->end))) {
244612d7cc84SAlan Cox 			end = entry->end;
244712d7cc84SAlan Cox 			rv = KERN_INVALID_ADDRESS;
244812d7cc84SAlan Cox 			goto done;
244912d7cc84SAlan Cox 		}
245012d7cc84SAlan Cox 		entry = entry->next;
245112d7cc84SAlan Cox 	}
245212d7cc84SAlan Cox 	rv = KERN_SUCCESS;
245312d7cc84SAlan Cox done:
245412d7cc84SAlan Cox 	need_wakeup = FALSE;
245512d7cc84SAlan Cox 	if (first_entry == NULL) {
245612d7cc84SAlan Cox 		result = vm_map_lookup_entry(map, start, &first_entry);
2457cbef13d8SAlan Cox 		if (!result && (flags & VM_MAP_WIRE_HOLESOK))
2458cbef13d8SAlan Cox 			first_entry = first_entry->next;
2459cbef13d8SAlan Cox 		else
246012d7cc84SAlan Cox 			KASSERT(result, ("vm_map_wire: lookup failed"));
246112d7cc84SAlan Cox 	}
246212d7cc84SAlan Cox 	entry = first_entry;
246312d7cc84SAlan Cox 	while (entry != &map->header && entry->start < end) {
24646d7e8091SKonstantin Belousov 		if ((entry->eflags & MAP_ENTRY_WIRE_SKIPPED) != 0)
24656d7e8091SKonstantin Belousov 			goto next_entry_done;
246612d7cc84SAlan Cox 		if (rv == KERN_SUCCESS) {
246712d7cc84SAlan Cox 			if (user_wire)
246812d7cc84SAlan Cox 				entry->eflags |= MAP_ENTRY_USER_WIRED;
246928c58286SAlan Cox 		} else if (entry->wired_count == -1) {
247028c58286SAlan Cox 			/*
247128c58286SAlan Cox 			 * Wiring failed on this entry.  Thus, unwiring is
247228c58286SAlan Cox 			 * unnecessary.
247328c58286SAlan Cox 			 */
247428c58286SAlan Cox 			entry->wired_count = 0;
247512d7cc84SAlan Cox 		} else {
24760ada205eSBrian Feldman 			if (!user_wire ||
24770ada205eSBrian Feldman 			    (entry->eflags & MAP_ENTRY_USER_WIRED) == 0)
247812d7cc84SAlan Cox 				entry->wired_count--;
24790ada205eSBrian Feldman 			if (entry->wired_count == 0) {
248012d7cc84SAlan Cox 				/*
248112d7cc84SAlan Cox 				 * Retain the map lock.
248212d7cc84SAlan Cox 				 */
24834be14af9SAlan Cox 				vm_fault_unwire(map, entry->start, entry->end,
24844be14af9SAlan Cox 				    entry->object.vm_object != NULL &&
248501381811SJohn Baldwin 				    (entry->object.vm_object->type == OBJT_DEVICE ||
248601381811SJohn Baldwin 				    entry->object.vm_object->type == OBJT_SG));
248712d7cc84SAlan Cox 			}
248812d7cc84SAlan Cox 		}
24896d7e8091SKonstantin Belousov 	next_entry_done:
249012d7cc84SAlan Cox 		KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
249112d7cc84SAlan Cox 			("vm_map_wire: in-transition flag missing"));
24926d7e8091SKonstantin Belousov 		entry->eflags &= ~(MAP_ENTRY_IN_TRANSITION|MAP_ENTRY_WIRE_SKIPPED);
249312d7cc84SAlan Cox 		if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
249412d7cc84SAlan Cox 			entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
249512d7cc84SAlan Cox 			need_wakeup = TRUE;
249612d7cc84SAlan Cox 		}
249712d7cc84SAlan Cox 		vm_map_simplify_entry(map, entry);
249812d7cc84SAlan Cox 		entry = entry->next;
249912d7cc84SAlan Cox 	}
250012d7cc84SAlan Cox 	vm_map_unlock(map);
250112d7cc84SAlan Cox 	if (need_wakeup)
250212d7cc84SAlan Cox 		vm_map_wakeup(map);
250312d7cc84SAlan Cox 	return (rv);
2504e27e17b7SAlan Cox }
2505e27e17b7SAlan Cox 
2506e27e17b7SAlan Cox /*
2507950f8459SAlan Cox  * vm_map_sync
2508df8bae1dSRodney W. Grimes  *
2509df8bae1dSRodney W. Grimes  * Push any dirty cached pages in the address range to their pager.
2510df8bae1dSRodney W. Grimes  * If syncio is TRUE, dirty pages are written synchronously.
2511df8bae1dSRodney W. Grimes  * If invalidate is TRUE, any cached pages are freed as well.
2512df8bae1dSRodney W. Grimes  *
2513637315edSAlan Cox  * If the size of the region from start to end is zero, we are
2514637315edSAlan Cox  * supposed to flush all modified pages within the region containing
2515637315edSAlan Cox  * start.  Unfortunately, a region can be split or coalesced with
2516637315edSAlan Cox  * neighboring regions, making it difficult to determine what the
2517637315edSAlan Cox  * original region was.  Therefore, we approximate this requirement by
2518637315edSAlan Cox  * flushing the current region containing start.
2519637315edSAlan Cox  *
2520df8bae1dSRodney W. Grimes  * Returns an error if any part of the specified range is not mapped.
2521df8bae1dSRodney W. Grimes  */
2522df8bae1dSRodney W. Grimes int
2523950f8459SAlan Cox vm_map_sync(
25241b40f8c0SMatthew Dillon 	vm_map_t map,
25251b40f8c0SMatthew Dillon 	vm_offset_t start,
25261b40f8c0SMatthew Dillon 	vm_offset_t end,
25271b40f8c0SMatthew Dillon 	boolean_t syncio,
25281b40f8c0SMatthew Dillon 	boolean_t invalidate)
2529df8bae1dSRodney W. Grimes {
2530c0877f10SJohn Dyson 	vm_map_entry_t current;
2531df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
2532df8bae1dSRodney W. Grimes 	vm_size_t size;
2533df8bae1dSRodney W. Grimes 	vm_object_t object;
2534a316d390SJohn Dyson 	vm_ooffset_t offset;
2535e53fa61bSKonstantin Belousov 	unsigned int last_timestamp;
2536df8bae1dSRodney W. Grimes 
2537df8bae1dSRodney W. Grimes 	vm_map_lock_read(map);
2538df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
2539df8bae1dSRodney W. Grimes 	if (!vm_map_lookup_entry(map, start, &entry)) {
2540df8bae1dSRodney W. Grimes 		vm_map_unlock_read(map);
2541df8bae1dSRodney W. Grimes 		return (KERN_INVALID_ADDRESS);
2542637315edSAlan Cox 	} else if (start == end) {
2543637315edSAlan Cox 		start = entry->start;
2544637315edSAlan Cox 		end = entry->end;
2545df8bae1dSRodney W. Grimes 	}
2546df8bae1dSRodney W. Grimes 	/*
2547b7b7cd44SAlan Cox 	 * Make a first pass to check for user-wired memory and holes.
2548df8bae1dSRodney W. Grimes 	 */
25497b0e72d1SAlan Cox 	for (current = entry; current != &map->header && current->start < end;
25507b0e72d1SAlan Cox 	    current = current->next) {
2551b7b7cd44SAlan Cox 		if (invalidate && (current->eflags & MAP_ENTRY_USER_WIRED)) {
2552df8bae1dSRodney W. Grimes 			vm_map_unlock_read(map);
2553df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ARGUMENT);
2554df8bae1dSRodney W. Grimes 		}
2555df8bae1dSRodney W. Grimes 		if (end > current->end &&
2556df8bae1dSRodney W. Grimes 		    (current->next == &map->header ||
2557df8bae1dSRodney W. Grimes 			current->end != current->next->start)) {
2558df8bae1dSRodney W. Grimes 			vm_map_unlock_read(map);
2559df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ADDRESS);
2560df8bae1dSRodney W. Grimes 		}
2561df8bae1dSRodney W. Grimes 	}
2562df8bae1dSRodney W. Grimes 
25632cf13952SAlan Cox 	if (invalidate)
2564bc105a67SAlan Cox 		pmap_remove(map->pmap, start, end);
25652cf13952SAlan Cox 
2566df8bae1dSRodney W. Grimes 	/*
2567df8bae1dSRodney W. Grimes 	 * Make a second pass, cleaning/uncaching pages from the indicated
2568df8bae1dSRodney W. Grimes 	 * objects as we go.
2569df8bae1dSRodney W. Grimes 	 */
2570e53fa61bSKonstantin Belousov 	for (current = entry; current != &map->header && current->start < end;) {
2571df8bae1dSRodney W. Grimes 		offset = current->offset + (start - current->start);
2572df8bae1dSRodney W. Grimes 		size = (end <= current->end ? end : current->end) - start;
25739fdfe602SMatthew Dillon 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
2574c0877f10SJohn Dyson 			vm_map_t smap;
2575df8bae1dSRodney W. Grimes 			vm_map_entry_t tentry;
2576df8bae1dSRodney W. Grimes 			vm_size_t tsize;
2577df8bae1dSRodney W. Grimes 
25789fdfe602SMatthew Dillon 			smap = current->object.sub_map;
2579df8bae1dSRodney W. Grimes 			vm_map_lock_read(smap);
2580df8bae1dSRodney W. Grimes 			(void) vm_map_lookup_entry(smap, offset, &tentry);
2581df8bae1dSRodney W. Grimes 			tsize = tentry->end - offset;
2582df8bae1dSRodney W. Grimes 			if (tsize < size)
2583df8bae1dSRodney W. Grimes 				size = tsize;
2584df8bae1dSRodney W. Grimes 			object = tentry->object.vm_object;
2585df8bae1dSRodney W. Grimes 			offset = tentry->offset + (offset - tentry->start);
2586df8bae1dSRodney W. Grimes 			vm_map_unlock_read(smap);
2587df8bae1dSRodney W. Grimes 		} else {
2588df8bae1dSRodney W. Grimes 			object = current->object.vm_object;
2589df8bae1dSRodney W. Grimes 		}
2590e53fa61bSKonstantin Belousov 		vm_object_reference(object);
2591e53fa61bSKonstantin Belousov 		last_timestamp = map->timestamp;
2592e53fa61bSKonstantin Belousov 		vm_map_unlock_read(map);
2593950f8459SAlan Cox 		vm_object_sync(object, offset, size, syncio, invalidate);
2594df8bae1dSRodney W. Grimes 		start += size;
2595e53fa61bSKonstantin Belousov 		vm_object_deallocate(object);
2596e53fa61bSKonstantin Belousov 		vm_map_lock_read(map);
2597e53fa61bSKonstantin Belousov 		if (last_timestamp == map->timestamp ||
2598e53fa61bSKonstantin Belousov 		    !vm_map_lookup_entry(map, start, &current))
2599e53fa61bSKonstantin Belousov 			current = current->next;
2600df8bae1dSRodney W. Grimes 	}
2601df8bae1dSRodney W. Grimes 
2602df8bae1dSRodney W. Grimes 	vm_map_unlock_read(map);
2603df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
2604df8bae1dSRodney W. Grimes }
2605df8bae1dSRodney W. Grimes 
2606df8bae1dSRodney W. Grimes /*
2607df8bae1dSRodney W. Grimes  *	vm_map_entry_unwire:	[ internal use only ]
2608df8bae1dSRodney W. Grimes  *
2609df8bae1dSRodney W. Grimes  *	Make the region specified by this entry pageable.
2610df8bae1dSRodney W. Grimes  *
2611df8bae1dSRodney W. Grimes  *	The map in question should be locked.
2612df8bae1dSRodney W. Grimes  *	[This is the reason for this routine's existence.]
2613df8bae1dSRodney W. Grimes  */
26140362d7d7SJohn Dyson static void
26151b40f8c0SMatthew Dillon vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
2616df8bae1dSRodney W. Grimes {
26174be14af9SAlan Cox 	vm_fault_unwire(map, entry->start, entry->end,
26184be14af9SAlan Cox 	    entry->object.vm_object != NULL &&
261901381811SJohn Baldwin 	    (entry->object.vm_object->type == OBJT_DEVICE ||
262001381811SJohn Baldwin 	    entry->object.vm_object->type == OBJT_SG));
2621df8bae1dSRodney W. Grimes 	entry->wired_count = 0;
2622df8bae1dSRodney W. Grimes }
2623df8bae1dSRodney W. Grimes 
2624df8bae1dSRodney W. Grimes /*
2625df8bae1dSRodney W. Grimes  *	vm_map_entry_delete:	[ internal use only ]
2626df8bae1dSRodney W. Grimes  *
2627df8bae1dSRodney W. Grimes  *	Deallocate the given entry from the target map.
2628df8bae1dSRodney W. Grimes  */
26290362d7d7SJohn Dyson static void
26301b40f8c0SMatthew Dillon vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry)
2631df8bae1dSRodney W. Grimes {
263232a89c32SAlan Cox 	vm_object_t object;
26333364c323SKonstantin Belousov 	vm_pindex_t offidxstart, offidxend, count, size1;
26343364c323SKonstantin Belousov 	vm_ooffset_t size;
263532a89c32SAlan Cox 
2636df8bae1dSRodney W. Grimes 	vm_map_entry_unlink(map, entry);
26373364c323SKonstantin Belousov 	object = entry->object.vm_object;
26383364c323SKonstantin Belousov 	size = entry->end - entry->start;
26393364c323SKonstantin Belousov 	map->size -= size;
26403364c323SKonstantin Belousov 
26413364c323SKonstantin Belousov 	if (entry->uip != NULL) {
26423364c323SKonstantin Belousov 		swap_release_by_uid(size, entry->uip);
26433364c323SKonstantin Belousov 		uifree(entry->uip);
26443364c323SKonstantin Belousov 	}
2645df8bae1dSRodney W. Grimes 
264632a89c32SAlan Cox 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
26473364c323SKonstantin Belousov 	    (object != NULL)) {
26483364c323SKonstantin Belousov 		KASSERT(entry->uip == NULL || object->uip == NULL ||
26493364c323SKonstantin Belousov 		    (entry->eflags & MAP_ENTRY_NEEDS_COPY),
26503364c323SKonstantin Belousov 		    ("OVERCOMMIT vm_map_entry_delete: both uip %p", entry));
26513364c323SKonstantin Belousov 		count = OFF_TO_IDX(size);
265232a89c32SAlan Cox 		offidxstart = OFF_TO_IDX(entry->offset);
265332a89c32SAlan Cox 		offidxend = offidxstart + count;
265432a89c32SAlan Cox 		VM_OBJECT_LOCK(object);
265532a89c32SAlan Cox 		if (object->ref_count != 1 &&
265632a89c32SAlan Cox 		    ((object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING ||
26579f5c801bSAlan Cox 		    object == kernel_object || object == kmem_object)) {
265832a89c32SAlan Cox 			vm_object_collapse(object);
2659b103b948SDag-Erling Smørgrav 			vm_object_page_remove(object, offidxstart, offidxend, FALSE);
266032a89c32SAlan Cox 			if (object->type == OBJT_SWAP)
266132a89c32SAlan Cox 				swap_pager_freespace(object, offidxstart, count);
266232a89c32SAlan Cox 			if (offidxend >= object->size &&
26633364c323SKonstantin Belousov 			    offidxstart < object->size) {
26643364c323SKonstantin Belousov 				size1 = object->size;
266532a89c32SAlan Cox 				object->size = offidxstart;
26663364c323SKonstantin Belousov 				if (object->uip != NULL) {
26673364c323SKonstantin Belousov 					size1 -= object->size;
26683364c323SKonstantin Belousov 					KASSERT(object->charge >= ptoa(size1),
26693364c323SKonstantin Belousov 					    ("vm_map_entry_delete: object->charge < 0"));
26703364c323SKonstantin Belousov 					swap_release_by_uid(ptoa(size1), object->uip);
26713364c323SKonstantin Belousov 					object->charge -= ptoa(size1);
26723364c323SKonstantin Belousov 				}
26733364c323SKonstantin Belousov 			}
267432a89c32SAlan Cox 		}
267532a89c32SAlan Cox 		VM_OBJECT_UNLOCK(object);
2676897d81a0SKonstantin Belousov 	} else
2677897d81a0SKonstantin Belousov 		entry->object.vm_object = NULL;
2678df8bae1dSRodney W. Grimes }
2679df8bae1dSRodney W. Grimes 
2680df8bae1dSRodney W. Grimes /*
2681df8bae1dSRodney W. Grimes  *	vm_map_delete:	[ internal use only ]
2682df8bae1dSRodney W. Grimes  *
2683df8bae1dSRodney W. Grimes  *	Deallocates the given address range from the target
2684df8bae1dSRodney W. Grimes  *	map.
2685df8bae1dSRodney W. Grimes  */
2686df8bae1dSRodney W. Grimes int
2687655c3490SKonstantin Belousov vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end)
2688df8bae1dSRodney W. Grimes {
2689c0877f10SJohn Dyson 	vm_map_entry_t entry;
2690df8bae1dSRodney W. Grimes 	vm_map_entry_t first_entry;
2691df8bae1dSRodney W. Grimes 
26923a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
26933a0916b8SKonstantin Belousov 
2694df8bae1dSRodney W. Grimes 	/*
2695df8bae1dSRodney W. Grimes 	 * Find the start of the region, and clip it
2696df8bae1dSRodney W. Grimes 	 */
2697876318ecSAlan Cox 	if (!vm_map_lookup_entry(map, start, &first_entry))
2698df8bae1dSRodney W. Grimes 		entry = first_entry->next;
2699876318ecSAlan Cox 	else {
2700df8bae1dSRodney W. Grimes 		entry = first_entry;
2701df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
2702df8bae1dSRodney W. Grimes 	}
2703df8bae1dSRodney W. Grimes 
2704df8bae1dSRodney W. Grimes 	/*
2705df8bae1dSRodney W. Grimes 	 * Step through all entries in this region
2706df8bae1dSRodney W. Grimes 	 */
2707df8bae1dSRodney W. Grimes 	while ((entry != &map->header) && (entry->start < end)) {
2708df8bae1dSRodney W. Grimes 		vm_map_entry_t next;
2709df8bae1dSRodney W. Grimes 
271073b2baceSAlan Cox 		/*
271173b2baceSAlan Cox 		 * Wait for wiring or unwiring of an entry to complete.
27127c938963SBrian Feldman 		 * Also wait for any system wirings to disappear on
27137c938963SBrian Feldman 		 * user maps.
271473b2baceSAlan Cox 		 */
27157c938963SBrian Feldman 		if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 ||
27167c938963SBrian Feldman 		    (vm_map_pmap(map) != kernel_pmap &&
27177c938963SBrian Feldman 		    vm_map_entry_system_wired_count(entry) != 0)) {
271873b2baceSAlan Cox 			unsigned int last_timestamp;
271973b2baceSAlan Cox 			vm_offset_t saved_start;
272073b2baceSAlan Cox 			vm_map_entry_t tmp_entry;
272173b2baceSAlan Cox 
272273b2baceSAlan Cox 			saved_start = entry->start;
272373b2baceSAlan Cox 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
272473b2baceSAlan Cox 			last_timestamp = map->timestamp;
27258ce2d00aSPawel Jakub Dawidek 			(void) vm_map_unlock_and_wait(map, 0);
272673b2baceSAlan Cox 			vm_map_lock(map);
272773b2baceSAlan Cox 			if (last_timestamp + 1 != map->timestamp) {
272873b2baceSAlan Cox 				/*
272973b2baceSAlan Cox 				 * Look again for the entry because the map was
273073b2baceSAlan Cox 				 * modified while it was unlocked.
273173b2baceSAlan Cox 				 * Specifically, the entry may have been
273273b2baceSAlan Cox 				 * clipped, merged, or deleted.
273373b2baceSAlan Cox 				 */
273473b2baceSAlan Cox 				if (!vm_map_lookup_entry(map, saved_start,
273573b2baceSAlan Cox 							 &tmp_entry))
273673b2baceSAlan Cox 					entry = tmp_entry->next;
273773b2baceSAlan Cox 				else {
273873b2baceSAlan Cox 					entry = tmp_entry;
273973b2baceSAlan Cox 					vm_map_clip_start(map, entry,
274073b2baceSAlan Cox 							  saved_start);
274173b2baceSAlan Cox 				}
274273b2baceSAlan Cox 			}
274373b2baceSAlan Cox 			continue;
274473b2baceSAlan Cox 		}
2745df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, entry, end);
2746df8bae1dSRodney W. Grimes 
2747c0877f10SJohn Dyson 		next = entry->next;
2748df8bae1dSRodney W. Grimes 
2749df8bae1dSRodney W. Grimes 		/*
27500d94caffSDavid Greenman 		 * Unwire before removing addresses from the pmap; otherwise,
27510d94caffSDavid Greenman 		 * unwiring will put the entries back in the pmap.
2752df8bae1dSRodney W. Grimes 		 */
2753c0877f10SJohn Dyson 		if (entry->wired_count != 0) {
2754df8bae1dSRodney W. Grimes 			vm_map_entry_unwire(map, entry);
2755c0877f10SJohn Dyson 		}
2756df8bae1dSRodney W. Grimes 
275732a89c32SAlan Cox 		pmap_remove(map->pmap, entry->start, entry->end);
2758df8bae1dSRodney W. Grimes 
2759df8bae1dSRodney W. Grimes 		/*
2760e608cc3cSKonstantin Belousov 		 * Delete the entry only after removing all pmap
2761e608cc3cSKonstantin Belousov 		 * entries pointing to its pages.  (Otherwise, its
2762e608cc3cSKonstantin Belousov 		 * page frames may be reallocated, and any modify bits
2763e608cc3cSKonstantin Belousov 		 * will be set in the wrong object!)
2764df8bae1dSRodney W. Grimes 		 */
2765df8bae1dSRodney W. Grimes 		vm_map_entry_delete(map, entry);
2766655c3490SKonstantin Belousov 		entry->next = map->deferred_freelist;
2767655c3490SKonstantin Belousov 		map->deferred_freelist = entry;
2768df8bae1dSRodney W. Grimes 		entry = next;
2769df8bae1dSRodney W. Grimes 	}
2770df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
2771df8bae1dSRodney W. Grimes }
2772df8bae1dSRodney W. Grimes 
2773df8bae1dSRodney W. Grimes /*
2774df8bae1dSRodney W. Grimes  *	vm_map_remove:
2775df8bae1dSRodney W. Grimes  *
2776df8bae1dSRodney W. Grimes  *	Remove the given address range from the target map.
2777df8bae1dSRodney W. Grimes  *	This is the exported form of vm_map_delete.
2778df8bae1dSRodney W. Grimes  */
2779df8bae1dSRodney W. Grimes int
27801b40f8c0SMatthew Dillon vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
2781df8bae1dSRodney W. Grimes {
27826eaee3feSAlan Cox 	int result;
2783df8bae1dSRodney W. Grimes 
2784df8bae1dSRodney W. Grimes 	vm_map_lock(map);
2785df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
2786655c3490SKonstantin Belousov 	result = vm_map_delete(map, start, end);
2787df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
2788df8bae1dSRodney W. Grimes 	return (result);
2789df8bae1dSRodney W. Grimes }
2790df8bae1dSRodney W. Grimes 
2791df8bae1dSRodney W. Grimes /*
2792df8bae1dSRodney W. Grimes  *	vm_map_check_protection:
2793df8bae1dSRodney W. Grimes  *
27942d5c7e45SMatthew Dillon  *	Assert that the target map allows the specified privilege on the
27952d5c7e45SMatthew Dillon  *	entire address region given.  The entire region must be allocated.
27962d5c7e45SMatthew Dillon  *
27972d5c7e45SMatthew Dillon  *	WARNING!  This code does not and should not check whether the
27982d5c7e45SMatthew Dillon  *	contents of the region is accessible.  For example a smaller file
27992d5c7e45SMatthew Dillon  *	might be mapped into a larger address space.
28002d5c7e45SMatthew Dillon  *
28012d5c7e45SMatthew Dillon  *	NOTE!  This code is also called by munmap().
2802d8834602SAlan Cox  *
2803d8834602SAlan Cox  *	The map must be locked.  A read lock is sufficient.
2804df8bae1dSRodney W. Grimes  */
28050d94caffSDavid Greenman boolean_t
2806b9dcd593SBruce Evans vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
2807b9dcd593SBruce Evans 			vm_prot_t protection)
2808df8bae1dSRodney W. Grimes {
2809c0877f10SJohn Dyson 	vm_map_entry_t entry;
2810df8bae1dSRodney W. Grimes 	vm_map_entry_t tmp_entry;
2811df8bae1dSRodney W. Grimes 
2812d8834602SAlan Cox 	if (!vm_map_lookup_entry(map, start, &tmp_entry))
2813df8bae1dSRodney W. Grimes 		return (FALSE);
2814df8bae1dSRodney W. Grimes 	entry = tmp_entry;
2815df8bae1dSRodney W. Grimes 
2816df8bae1dSRodney W. Grimes 	while (start < end) {
2817d8834602SAlan Cox 		if (entry == &map->header)
2818df8bae1dSRodney W. Grimes 			return (FALSE);
2819df8bae1dSRodney W. Grimes 		/*
2820df8bae1dSRodney W. Grimes 		 * No holes allowed!
2821df8bae1dSRodney W. Grimes 		 */
2822d8834602SAlan Cox 		if (start < entry->start)
2823df8bae1dSRodney W. Grimes 			return (FALSE);
2824df8bae1dSRodney W. Grimes 		/*
2825df8bae1dSRodney W. Grimes 		 * Check protection associated with entry.
2826df8bae1dSRodney W. Grimes 		 */
2827d8834602SAlan Cox 		if ((entry->protection & protection) != protection)
2828df8bae1dSRodney W. Grimes 			return (FALSE);
2829df8bae1dSRodney W. Grimes 		/* go to next entry */
2830df8bae1dSRodney W. Grimes 		start = entry->end;
2831df8bae1dSRodney W. Grimes 		entry = entry->next;
2832df8bae1dSRodney W. Grimes 	}
2833df8bae1dSRodney W. Grimes 	return (TRUE);
2834df8bae1dSRodney W. Grimes }
2835df8bae1dSRodney W. Grimes 
283686524867SJohn Dyson /*
2837df8bae1dSRodney W. Grimes  *	vm_map_copy_entry:
2838df8bae1dSRodney W. Grimes  *
2839df8bae1dSRodney W. Grimes  *	Copies the contents of the source entry to the destination
2840df8bae1dSRodney W. Grimes  *	entry.  The entries *must* be aligned properly.
2841df8bae1dSRodney W. Grimes  */
2842f708ef1bSPoul-Henning Kamp static void
28431b40f8c0SMatthew Dillon vm_map_copy_entry(
28441b40f8c0SMatthew Dillon 	vm_map_t src_map,
28451b40f8c0SMatthew Dillon 	vm_map_t dst_map,
28461b40f8c0SMatthew Dillon 	vm_map_entry_t src_entry,
28473364c323SKonstantin Belousov 	vm_map_entry_t dst_entry,
28483364c323SKonstantin Belousov 	vm_ooffset_t *fork_charge)
2849df8bae1dSRodney W. Grimes {
2850c0877f10SJohn Dyson 	vm_object_t src_object;
28513364c323SKonstantin Belousov 	vm_offset_t size;
28523364c323SKonstantin Belousov 	struct uidinfo *uip;
28533364c323SKonstantin Belousov 	int charged;
2854c0877f10SJohn Dyson 
28553a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(dst_map);
28563a0916b8SKonstantin Belousov 
28579fdfe602SMatthew Dillon 	if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
2858df8bae1dSRodney W. Grimes 		return;
2859df8bae1dSRodney W. Grimes 
2860df8bae1dSRodney W. Grimes 	if (src_entry->wired_count == 0) {
2861df8bae1dSRodney W. Grimes 
2862df8bae1dSRodney W. Grimes 		/*
28630d94caffSDavid Greenman 		 * If the source entry is marked needs_copy, it is already
28640d94caffSDavid Greenman 		 * write-protected.
2865df8bae1dSRodney W. Grimes 		 */
2866afa07f7eSJohn Dyson 		if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
2867df8bae1dSRodney W. Grimes 			pmap_protect(src_map->pmap,
2868df8bae1dSRodney W. Grimes 			    src_entry->start,
2869df8bae1dSRodney W. Grimes 			    src_entry->end,
2870df8bae1dSRodney W. Grimes 			    src_entry->protection & ~VM_PROT_WRITE);
2871df8bae1dSRodney W. Grimes 		}
2872b18bfc3dSJohn Dyson 
2873df8bae1dSRodney W. Grimes 		/*
2874df8bae1dSRodney W. Grimes 		 * Make a copy of the object.
2875df8bae1dSRodney W. Grimes 		 */
28763364c323SKonstantin Belousov 		size = src_entry->end - src_entry->start;
28778aef1712SMatthew Dillon 		if ((src_object = src_entry->object.vm_object) != NULL) {
2878a89c6258SAlan Cox 			VM_OBJECT_LOCK(src_object);
28793364c323SKonstantin Belousov 			charged = ENTRY_CHARGED(src_entry);
2880c0877f10SJohn Dyson 			if ((src_object->handle == NULL) &&
2881c0877f10SJohn Dyson 				(src_object->type == OBJT_DEFAULT ||
2882c0877f10SJohn Dyson 				 src_object->type == OBJT_SWAP)) {
2883c0877f10SJohn Dyson 				vm_object_collapse(src_object);
288496fb8cf2SJohn Dyson 				if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
2885c5aaa06dSAlan Cox 					vm_object_split(src_entry);
2886c0877f10SJohn Dyson 					src_object = src_entry->object.vm_object;
2887a89c6258SAlan Cox 				}
2888a89c6258SAlan Cox 			}
2889b921a12bSAlan Cox 			vm_object_reference_locked(src_object);
2890069e9bc1SDoug Rabson 			vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
28913364c323SKonstantin Belousov 			if (src_entry->uip != NULL &&
28923364c323SKonstantin Belousov 			    !(src_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
28933364c323SKonstantin Belousov 				KASSERT(src_object->uip == NULL,
28943364c323SKonstantin Belousov 				    ("OVERCOMMIT: vm_map_copy_entry: uip %p",
28953364c323SKonstantin Belousov 				     src_object));
28963364c323SKonstantin Belousov 				src_object->uip = src_entry->uip;
28973364c323SKonstantin Belousov 				src_object->charge = size;
28983364c323SKonstantin Belousov 			}
2899e2479b4fSAlan Cox 			VM_OBJECT_UNLOCK(src_object);
2900c0877f10SJohn Dyson 			dst_entry->object.vm_object = src_object;
29013364c323SKonstantin Belousov 			if (charged) {
29023364c323SKonstantin Belousov 				uip = curthread->td_ucred->cr_ruidinfo;
29033364c323SKonstantin Belousov 				uihold(uip);
29043364c323SKonstantin Belousov 				dst_entry->uip = uip;
29053364c323SKonstantin Belousov 				*fork_charge += size;
29063364c323SKonstantin Belousov 				if (!(src_entry->eflags &
29073364c323SKonstantin Belousov 				      MAP_ENTRY_NEEDS_COPY)) {
29083364c323SKonstantin Belousov 					uihold(uip);
29093364c323SKonstantin Belousov 					src_entry->uip = uip;
29103364c323SKonstantin Belousov 					*fork_charge += size;
29113364c323SKonstantin Belousov 				}
29123364c323SKonstantin Belousov 			}
2913afa07f7eSJohn Dyson 			src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2914afa07f7eSJohn Dyson 			dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2915b18bfc3dSJohn Dyson 			dst_entry->offset = src_entry->offset;
2916b18bfc3dSJohn Dyson 		} else {
2917b18bfc3dSJohn Dyson 			dst_entry->object.vm_object = NULL;
2918b18bfc3dSJohn Dyson 			dst_entry->offset = 0;
29193364c323SKonstantin Belousov 			if (src_entry->uip != NULL) {
29203364c323SKonstantin Belousov 				dst_entry->uip = curthread->td_ucred->cr_ruidinfo;
29213364c323SKonstantin Belousov 				uihold(dst_entry->uip);
29223364c323SKonstantin Belousov 				*fork_charge += size;
29233364c323SKonstantin Belousov 			}
2924b18bfc3dSJohn Dyson 		}
2925df8bae1dSRodney W. Grimes 
2926df8bae1dSRodney W. Grimes 		pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
2927df8bae1dSRodney W. Grimes 		    dst_entry->end - dst_entry->start, src_entry->start);
29280d94caffSDavid Greenman 	} else {
2929df8bae1dSRodney W. Grimes 		/*
2930df8bae1dSRodney W. Grimes 		 * Of course, wired down pages can't be set copy-on-write.
29310d94caffSDavid Greenman 		 * Cause wired pages to be copied into the new map by
29320d94caffSDavid Greenman 		 * simulating faults (the new pages are pageable)
2933df8bae1dSRodney W. Grimes 		 */
2934121fd461SKonstantin Belousov 		vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry,
2935121fd461SKonstantin Belousov 		    fork_charge);
2936df8bae1dSRodney W. Grimes 	}
2937df8bae1dSRodney W. Grimes }
2938df8bae1dSRodney W. Grimes 
2939df8bae1dSRodney W. Grimes /*
29402a7be1b6SBrian Feldman  * vmspace_map_entry_forked:
29412a7be1b6SBrian Feldman  * Update the newly-forked vmspace each time a map entry is inherited
29422a7be1b6SBrian Feldman  * or copied.  The values for vm_dsize and vm_tsize are approximate
29432a7be1b6SBrian Feldman  * (and mostly-obsolete ideas in the face of mmap(2) et al.)
29442a7be1b6SBrian Feldman  */
29452a7be1b6SBrian Feldman static void
29462a7be1b6SBrian Feldman vmspace_map_entry_forked(const struct vmspace *vm1, struct vmspace *vm2,
29472a7be1b6SBrian Feldman     vm_map_entry_t entry)
29482a7be1b6SBrian Feldman {
29492a7be1b6SBrian Feldman 	vm_size_t entrysize;
29502a7be1b6SBrian Feldman 	vm_offset_t newend;
29512a7be1b6SBrian Feldman 
29522a7be1b6SBrian Feldman 	entrysize = entry->end - entry->start;
29532a7be1b6SBrian Feldman 	vm2->vm_map.size += entrysize;
29542a7be1b6SBrian Feldman 	if (entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP)) {
29552a7be1b6SBrian Feldman 		vm2->vm_ssize += btoc(entrysize);
29562a7be1b6SBrian Feldman 	} else if (entry->start >= (vm_offset_t)vm1->vm_daddr &&
29572a7be1b6SBrian Feldman 	    entry->start < (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)) {
2958b351299cSAndrew Gallatin 		newend = MIN(entry->end,
29592a7be1b6SBrian Feldman 		    (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize));
29602a7be1b6SBrian Feldman 		vm2->vm_dsize += btoc(newend - entry->start);
29612a7be1b6SBrian Feldman 	} else if (entry->start >= (vm_offset_t)vm1->vm_taddr &&
29622a7be1b6SBrian Feldman 	    entry->start < (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)) {
2963b351299cSAndrew Gallatin 		newend = MIN(entry->end,
29642a7be1b6SBrian Feldman 		    (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize));
29652a7be1b6SBrian Feldman 		vm2->vm_tsize += btoc(newend - entry->start);
29662a7be1b6SBrian Feldman 	}
29672a7be1b6SBrian Feldman }
29682a7be1b6SBrian Feldman 
29692a7be1b6SBrian Feldman /*
2970df8bae1dSRodney W. Grimes  * vmspace_fork:
2971df8bae1dSRodney W. Grimes  * Create a new process vmspace structure and vm_map
2972df8bae1dSRodney W. Grimes  * based on those of an existing process.  The new map
2973df8bae1dSRodney W. Grimes  * is based on the old map, according to the inheritance
2974df8bae1dSRodney W. Grimes  * values on the regions in that map.
2975df8bae1dSRodney W. Grimes  *
29762a7be1b6SBrian Feldman  * XXX It might be worth coalescing the entries added to the new vmspace.
29772a7be1b6SBrian Feldman  *
2978df8bae1dSRodney W. Grimes  * The source map must not be locked.
2979df8bae1dSRodney W. Grimes  */
2980df8bae1dSRodney W. Grimes struct vmspace *
29813364c323SKonstantin Belousov vmspace_fork(struct vmspace *vm1, vm_ooffset_t *fork_charge)
2982df8bae1dSRodney W. Grimes {
2983c0877f10SJohn Dyson 	struct vmspace *vm2;
2984df8bae1dSRodney W. Grimes 	vm_map_t old_map = &vm1->vm_map;
2985df8bae1dSRodney W. Grimes 	vm_map_t new_map;
2986df8bae1dSRodney W. Grimes 	vm_map_entry_t old_entry;
2987df8bae1dSRodney W. Grimes 	vm_map_entry_t new_entry;
2988de5f6a77SJohn Dyson 	vm_object_t object;
29891fac7d7fSKonstantin Belousov 	int locked;
2990df8bae1dSRodney W. Grimes 
2991df8bae1dSRodney W. Grimes 	vm_map_lock(old_map);
29922d8acc0fSJohn Dyson 	vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
299389b57fcfSKonstantin Belousov 	if (vm2 == NULL)
299489b57fcfSKonstantin Belousov 		goto unlock_and_return;
29952a7be1b6SBrian Feldman 	vm2->vm_taddr = vm1->vm_taddr;
29962a7be1b6SBrian Feldman 	vm2->vm_daddr = vm1->vm_daddr;
29972a7be1b6SBrian Feldman 	vm2->vm_maxsaddr = vm1->vm_maxsaddr;
2998df8bae1dSRodney W. Grimes 	new_map = &vm2->vm_map;	/* XXX */
29991fac7d7fSKonstantin Belousov 	locked = vm_map_trylock(new_map); /* trylock to silence WITNESS */
30001fac7d7fSKonstantin Belousov 	KASSERT(locked, ("vmspace_fork: lock failed"));
300147221757SJohn Dyson 	new_map->timestamp = 1;
3002df8bae1dSRodney W. Grimes 
3003df8bae1dSRodney W. Grimes 	old_entry = old_map->header.next;
3004df8bae1dSRodney W. Grimes 
3005df8bae1dSRodney W. Grimes 	while (old_entry != &old_map->header) {
3006afa07f7eSJohn Dyson 		if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
3007df8bae1dSRodney W. Grimes 			panic("vm_map_fork: encountered a submap");
3008df8bae1dSRodney W. Grimes 
3009df8bae1dSRodney W. Grimes 		switch (old_entry->inheritance) {
3010df8bae1dSRodney W. Grimes 		case VM_INHERIT_NONE:
3011df8bae1dSRodney W. Grimes 			break;
3012df8bae1dSRodney W. Grimes 
3013df8bae1dSRodney W. Grimes 		case VM_INHERIT_SHARE:
3014df8bae1dSRodney W. Grimes 			/*
3015fed9a903SJohn Dyson 			 * Clone the entry, creating the shared object if necessary.
3016fed9a903SJohn Dyson 			 */
3017fed9a903SJohn Dyson 			object = old_entry->object.vm_object;
3018fed9a903SJohn Dyson 			if (object == NULL) {
3019fed9a903SJohn Dyson 				object = vm_object_allocate(OBJT_DEFAULT,
3020c2e11a03SJohn Dyson 					atop(old_entry->end - old_entry->start));
3021fed9a903SJohn Dyson 				old_entry->object.vm_object = object;
302215d2d313SAlan Cox 				old_entry->offset = 0;
30233364c323SKonstantin Belousov 				if (old_entry->uip != NULL) {
30243364c323SKonstantin Belousov 					object->uip = old_entry->uip;
30253364c323SKonstantin Belousov 					object->charge = old_entry->end -
30263364c323SKonstantin Belousov 					    old_entry->start;
30273364c323SKonstantin Belousov 					old_entry->uip = NULL;
30283364c323SKonstantin Belousov 				}
30299a2f6362SAlan Cox 			}
30309a2f6362SAlan Cox 
30319a2f6362SAlan Cox 			/*
30329a2f6362SAlan Cox 			 * Add the reference before calling vm_object_shadow
30339a2f6362SAlan Cox 			 * to insure that a shadow object is created.
30349a2f6362SAlan Cox 			 */
30359a2f6362SAlan Cox 			vm_object_reference(object);
30369a2f6362SAlan Cox 			if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
30375069bf57SJohn Dyson 				vm_object_shadow(&old_entry->object.vm_object,
30385069bf57SJohn Dyson 					&old_entry->offset,
3039c2e11a03SJohn Dyson 					atop(old_entry->end - old_entry->start));
30405069bf57SJohn Dyson 				old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
3041d30344bdSIan Dowse 				/* Transfer the second reference too. */
3042d30344bdSIan Dowse 				vm_object_reference(
3043d30344bdSIan Dowse 				    old_entry->object.vm_object);
30447fd10fb3SKonstantin Belousov 
30457fd10fb3SKonstantin Belousov 				/*
30467fd10fb3SKonstantin Belousov 				 * As in vm_map_simplify_entry(), the
3047b0994946SKonstantin Belousov 				 * vnode lock will not be acquired in
30487fd10fb3SKonstantin Belousov 				 * this call to vm_object_deallocate().
30497fd10fb3SKonstantin Belousov 				 */
3050d30344bdSIan Dowse 				vm_object_deallocate(object);
30515069bf57SJohn Dyson 				object = old_entry->object.vm_object;
3052fed9a903SJohn Dyson 			}
3053e2479b4fSAlan Cox 			VM_OBJECT_LOCK(object);
3054069e9bc1SDoug Rabson 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
30553364c323SKonstantin Belousov 			if (old_entry->uip != NULL) {
30563364c323SKonstantin Belousov 				KASSERT(object->uip == NULL, ("vmspace_fork both uip"));
30573364c323SKonstantin Belousov 				object->uip = old_entry->uip;
30583364c323SKonstantin Belousov 				object->charge = old_entry->end - old_entry->start;
30593364c323SKonstantin Belousov 				old_entry->uip = NULL;
30603364c323SKonstantin Belousov 			}
3061e2479b4fSAlan Cox 			VM_OBJECT_UNLOCK(object);
3062fed9a903SJohn Dyson 
3063fed9a903SJohn Dyson 			/*
3064ad5fca3bSAlan Cox 			 * Clone the entry, referencing the shared object.
3065df8bae1dSRodney W. Grimes 			 */
3066df8bae1dSRodney W. Grimes 			new_entry = vm_map_entry_create(new_map);
3067df8bae1dSRodney W. Grimes 			*new_entry = *old_entry;
30689f6acfd1SKonstantin Belousov 			new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
30699f6acfd1SKonstantin Belousov 			    MAP_ENTRY_IN_TRANSITION);
3070df8bae1dSRodney W. Grimes 			new_entry->wired_count = 0;
3071df8bae1dSRodney W. Grimes 
3072df8bae1dSRodney W. Grimes 			/*
30730d94caffSDavid Greenman 			 * Insert the entry into the new map -- we know we're
30740d94caffSDavid Greenman 			 * inserting at the end of the new map.
3075df8bae1dSRodney W. Grimes 			 */
3076df8bae1dSRodney W. Grimes 			vm_map_entry_link(new_map, new_map->header.prev,
3077df8bae1dSRodney W. Grimes 			    new_entry);
30782a7be1b6SBrian Feldman 			vmspace_map_entry_forked(vm1, vm2, new_entry);
3079df8bae1dSRodney W. Grimes 
3080df8bae1dSRodney W. Grimes 			/*
3081df8bae1dSRodney W. Grimes 			 * Update the physical map
3082df8bae1dSRodney W. Grimes 			 */
3083df8bae1dSRodney W. Grimes 			pmap_copy(new_map->pmap, old_map->pmap,
3084df8bae1dSRodney W. Grimes 			    new_entry->start,
3085df8bae1dSRodney W. Grimes 			    (old_entry->end - old_entry->start),
3086df8bae1dSRodney W. Grimes 			    old_entry->start);
3087df8bae1dSRodney W. Grimes 			break;
3088df8bae1dSRodney W. Grimes 
3089df8bae1dSRodney W. Grimes 		case VM_INHERIT_COPY:
3090df8bae1dSRodney W. Grimes 			/*
3091df8bae1dSRodney W. Grimes 			 * Clone the entry and link into the map.
3092df8bae1dSRodney W. Grimes 			 */
3093df8bae1dSRodney W. Grimes 			new_entry = vm_map_entry_create(new_map);
3094df8bae1dSRodney W. Grimes 			*new_entry = *old_entry;
30959f6acfd1SKonstantin Belousov 			new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
30969f6acfd1SKonstantin Belousov 			    MAP_ENTRY_IN_TRANSITION);
3097df8bae1dSRodney W. Grimes 			new_entry->wired_count = 0;
3098df8bae1dSRodney W. Grimes 			new_entry->object.vm_object = NULL;
3099121fd461SKonstantin Belousov 			new_entry->uip = NULL;
3100df8bae1dSRodney W. Grimes 			vm_map_entry_link(new_map, new_map->header.prev,
3101df8bae1dSRodney W. Grimes 			    new_entry);
31022a7be1b6SBrian Feldman 			vmspace_map_entry_forked(vm1, vm2, new_entry);
3103bd7e5f99SJohn Dyson 			vm_map_copy_entry(old_map, new_map, old_entry,
31043364c323SKonstantin Belousov 			    new_entry, fork_charge);
3105df8bae1dSRodney W. Grimes 			break;
3106df8bae1dSRodney W. Grimes 		}
3107df8bae1dSRodney W. Grimes 		old_entry = old_entry->next;
3108df8bae1dSRodney W. Grimes 	}
310989b57fcfSKonstantin Belousov unlock_and_return:
3110df8bae1dSRodney W. Grimes 	vm_map_unlock(old_map);
31111fac7d7fSKonstantin Belousov 	if (vm2 != NULL)
31121fac7d7fSKonstantin Belousov 		vm_map_unlock(new_map);
3113df8bae1dSRodney W. Grimes 
3114df8bae1dSRodney W. Grimes 	return (vm2);
3115df8bae1dSRodney W. Grimes }
3116df8bae1dSRodney W. Grimes 
311794f7e29aSAlan Cox int
311894f7e29aSAlan Cox vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
311994f7e29aSAlan Cox     vm_prot_t prot, vm_prot_t max, int cow)
312094f7e29aSAlan Cox {
3121fd75d710SMarcel Moolenaar 	vm_map_entry_t new_entry, prev_entry;
3122fd75d710SMarcel Moolenaar 	vm_offset_t bot, top;
312394f7e29aSAlan Cox 	vm_size_t init_ssize;
3124fd75d710SMarcel Moolenaar 	int orient, rv;
312591d5354aSJohn Baldwin 	rlim_t vmemlim;
312694f7e29aSAlan Cox 
3127fd75d710SMarcel Moolenaar 	/*
3128fd75d710SMarcel Moolenaar 	 * The stack orientation is piggybacked with the cow argument.
3129fd75d710SMarcel Moolenaar 	 * Extract it into orient and mask the cow argument so that we
3130fd75d710SMarcel Moolenaar 	 * don't pass it around further.
3131fd75d710SMarcel Moolenaar 	 * NOTE: We explicitly allow bi-directional stacks.
3132fd75d710SMarcel Moolenaar 	 */
3133fd75d710SMarcel Moolenaar 	orient = cow & (MAP_STACK_GROWS_DOWN|MAP_STACK_GROWS_UP);
3134fd75d710SMarcel Moolenaar 	cow &= ~orient;
3135fd75d710SMarcel Moolenaar 	KASSERT(orient != 0, ("No stack grow direction"));
3136fd75d710SMarcel Moolenaar 
313777bc7900SKonstantin Belousov 	if (addrbos < vm_map_min(map) ||
313877bc7900SKonstantin Belousov 	    addrbos > vm_map_max(map) ||
313977bc7900SKonstantin Belousov 	    addrbos + max_ssize < addrbos)
314094f7e29aSAlan Cox 		return (KERN_NO_SPACE);
3141fd75d710SMarcel Moolenaar 
3142fd75d710SMarcel Moolenaar 	init_ssize = (max_ssize < sgrowsiz) ? max_ssize : sgrowsiz;
314394f7e29aSAlan Cox 
314491d5354aSJohn Baldwin 	PROC_LOCK(curthread->td_proc);
314591d5354aSJohn Baldwin 	vmemlim = lim_cur(curthread->td_proc, RLIMIT_VMEM);
314691d5354aSJohn Baldwin 	PROC_UNLOCK(curthread->td_proc);
314791d5354aSJohn Baldwin 
314894f7e29aSAlan Cox 	vm_map_lock(map);
314994f7e29aSAlan Cox 
315094f7e29aSAlan Cox 	/* If addr is already mapped, no go */
315194f7e29aSAlan Cox 	if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
315294f7e29aSAlan Cox 		vm_map_unlock(map);
315394f7e29aSAlan Cox 		return (KERN_NO_SPACE);
315494f7e29aSAlan Cox 	}
315594f7e29aSAlan Cox 
3156a69ac174SMatthew Dillon 	/* If we would blow our VMEM resource limit, no go */
315791d5354aSJohn Baldwin 	if (map->size + init_ssize > vmemlim) {
3158a69ac174SMatthew Dillon 		vm_map_unlock(map);
3159a69ac174SMatthew Dillon 		return (KERN_NO_SPACE);
3160a69ac174SMatthew Dillon 	}
3161a69ac174SMatthew Dillon 
3162fd75d710SMarcel Moolenaar 	/*
3163fd75d710SMarcel Moolenaar 	 * If we can't accomodate max_ssize in the current mapping, no go.
3164fd75d710SMarcel Moolenaar 	 * However, we need to be aware that subsequent user mappings might
3165fd75d710SMarcel Moolenaar 	 * map into the space we have reserved for stack, and currently this
3166fd75d710SMarcel Moolenaar 	 * space is not protected.
316794f7e29aSAlan Cox 	 *
3168fd75d710SMarcel Moolenaar 	 * Hopefully we will at least detect this condition when we try to
3169fd75d710SMarcel Moolenaar 	 * grow the stack.
317094f7e29aSAlan Cox 	 */
317194f7e29aSAlan Cox 	if ((prev_entry->next != &map->header) &&
317294f7e29aSAlan Cox 	    (prev_entry->next->start < addrbos + max_ssize)) {
317394f7e29aSAlan Cox 		vm_map_unlock(map);
317494f7e29aSAlan Cox 		return (KERN_NO_SPACE);
317594f7e29aSAlan Cox 	}
317694f7e29aSAlan Cox 
3177fd75d710SMarcel Moolenaar 	/*
3178fd75d710SMarcel Moolenaar 	 * We initially map a stack of only init_ssize.  We will grow as
3179fd75d710SMarcel Moolenaar 	 * needed later.  Depending on the orientation of the stack (i.e.
3180fd75d710SMarcel Moolenaar 	 * the grow direction) we either map at the top of the range, the
3181fd75d710SMarcel Moolenaar 	 * bottom of the range or in the middle.
318294f7e29aSAlan Cox 	 *
3183fd75d710SMarcel Moolenaar 	 * Note: we would normally expect prot and max to be VM_PROT_ALL,
3184fd75d710SMarcel Moolenaar 	 * and cow to be 0.  Possibly we should eliminate these as input
3185fd75d710SMarcel Moolenaar 	 * parameters, and just pass these values here in the insert call.
318694f7e29aSAlan Cox 	 */
3187fd75d710SMarcel Moolenaar 	if (orient == MAP_STACK_GROWS_DOWN)
3188fd75d710SMarcel Moolenaar 		bot = addrbos + max_ssize - init_ssize;
3189fd75d710SMarcel Moolenaar 	else if (orient == MAP_STACK_GROWS_UP)
3190fd75d710SMarcel Moolenaar 		bot = addrbos;
3191fd75d710SMarcel Moolenaar 	else
3192fd75d710SMarcel Moolenaar 		bot = round_page(addrbos + max_ssize/2 - init_ssize/2);
3193fd75d710SMarcel Moolenaar 	top = bot + init_ssize;
3194fd75d710SMarcel Moolenaar 	rv = vm_map_insert(map, NULL, 0, bot, top, prot, max, cow);
319594f7e29aSAlan Cox 
3196fd75d710SMarcel Moolenaar 	/* Now set the avail_ssize amount. */
319794f7e29aSAlan Cox 	if (rv == KERN_SUCCESS) {
319829b45e9eSAlan Cox 		if (prev_entry != &map->header)
3199fd75d710SMarcel Moolenaar 			vm_map_clip_end(map, prev_entry, bot);
3200fd75d710SMarcel Moolenaar 		new_entry = prev_entry->next;
3201fd75d710SMarcel Moolenaar 		if (new_entry->end != top || new_entry->start != bot)
320294f7e29aSAlan Cox 			panic("Bad entry start/end for new stack entry");
3203b21a0008SMarcel Moolenaar 
3204fd75d710SMarcel Moolenaar 		new_entry->avail_ssize = max_ssize - init_ssize;
3205fd75d710SMarcel Moolenaar 		if (orient & MAP_STACK_GROWS_DOWN)
3206fd75d710SMarcel Moolenaar 			new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
3207fd75d710SMarcel Moolenaar 		if (orient & MAP_STACK_GROWS_UP)
3208fd75d710SMarcel Moolenaar 			new_entry->eflags |= MAP_ENTRY_GROWS_UP;
320994f7e29aSAlan Cox 	}
321094f7e29aSAlan Cox 
321194f7e29aSAlan Cox 	vm_map_unlock(map);
321294f7e29aSAlan Cox 	return (rv);
321394f7e29aSAlan Cox }
321494f7e29aSAlan Cox 
321594f7e29aSAlan Cox /* Attempts to grow a vm stack entry.  Returns KERN_SUCCESS if the
321694f7e29aSAlan Cox  * desired address is already mapped, or if we successfully grow
321794f7e29aSAlan Cox  * the stack.  Also returns KERN_SUCCESS if addr is outside the
321894f7e29aSAlan Cox  * stack range (this is strange, but preserves compatibility with
321994f7e29aSAlan Cox  * the grow function in vm_machdep.c).
322094f7e29aSAlan Cox  */
322194f7e29aSAlan Cox int
322294f7e29aSAlan Cox vm_map_growstack(struct proc *p, vm_offset_t addr)
322394f7e29aSAlan Cox {
3224b21a0008SMarcel Moolenaar 	vm_map_entry_t next_entry, prev_entry;
3225b21a0008SMarcel Moolenaar 	vm_map_entry_t new_entry, stack_entry;
322694f7e29aSAlan Cox 	struct vmspace *vm = p->p_vmspace;
322794f7e29aSAlan Cox 	vm_map_t map = &vm->vm_map;
322894f7e29aSAlan Cox 	vm_offset_t end;
3229b21a0008SMarcel Moolenaar 	size_t grow_amount, max_grow;
323091d5354aSJohn Baldwin 	rlim_t stacklim, vmemlim;
3231b21a0008SMarcel Moolenaar 	int is_procstack, rv;
32323364c323SKonstantin Belousov 	struct uidinfo *uip;
323323955314SAlfred Perlstein 
323494f7e29aSAlan Cox Retry:
323591d5354aSJohn Baldwin 	PROC_LOCK(p);
323691d5354aSJohn Baldwin 	stacklim = lim_cur(p, RLIMIT_STACK);
3237bfee999dSAlan Cox 	vmemlim = lim_cur(p, RLIMIT_VMEM);
323891d5354aSJohn Baldwin 	PROC_UNLOCK(p);
323991d5354aSJohn Baldwin 
324094f7e29aSAlan Cox 	vm_map_lock_read(map);
324194f7e29aSAlan Cox 
324294f7e29aSAlan Cox 	/* If addr is already in the entry range, no need to grow.*/
324394f7e29aSAlan Cox 	if (vm_map_lookup_entry(map, addr, &prev_entry)) {
324494f7e29aSAlan Cox 		vm_map_unlock_read(map);
32450cddd8f0SMatthew Dillon 		return (KERN_SUCCESS);
324694f7e29aSAlan Cox 	}
324794f7e29aSAlan Cox 
3248b21a0008SMarcel Moolenaar 	next_entry = prev_entry->next;
3249b21a0008SMarcel Moolenaar 	if (!(prev_entry->eflags & MAP_ENTRY_GROWS_UP)) {
3250b21a0008SMarcel Moolenaar 		/*
3251b21a0008SMarcel Moolenaar 		 * This entry does not grow upwards. Since the address lies
3252b21a0008SMarcel Moolenaar 		 * beyond this entry, the next entry (if one exists) has to
3253b21a0008SMarcel Moolenaar 		 * be a downward growable entry. The entry list header is
3254b21a0008SMarcel Moolenaar 		 * never a growable entry, so it suffices to check the flags.
325594f7e29aSAlan Cox 		 */
3256b21a0008SMarcel Moolenaar 		if (!(next_entry->eflags & MAP_ENTRY_GROWS_DOWN)) {
325794f7e29aSAlan Cox 			vm_map_unlock_read(map);
32580cddd8f0SMatthew Dillon 			return (KERN_SUCCESS);
325994f7e29aSAlan Cox 		}
3260b21a0008SMarcel Moolenaar 		stack_entry = next_entry;
3261b21a0008SMarcel Moolenaar 	} else {
3262b21a0008SMarcel Moolenaar 		/*
3263b21a0008SMarcel Moolenaar 		 * This entry grows upward. If the next entry does not at
3264b21a0008SMarcel Moolenaar 		 * least grow downwards, this is the entry we need to grow.
3265b21a0008SMarcel Moolenaar 		 * otherwise we have two possible choices and we have to
3266b21a0008SMarcel Moolenaar 		 * select one.
3267b21a0008SMarcel Moolenaar 		 */
3268b21a0008SMarcel Moolenaar 		if (next_entry->eflags & MAP_ENTRY_GROWS_DOWN) {
3269b21a0008SMarcel Moolenaar 			/*
3270b21a0008SMarcel Moolenaar 			 * We have two choices; grow the entry closest to
3271b21a0008SMarcel Moolenaar 			 * the address to minimize the amount of growth.
3272b21a0008SMarcel Moolenaar 			 */
3273b21a0008SMarcel Moolenaar 			if (addr - prev_entry->end <= next_entry->start - addr)
3274b21a0008SMarcel Moolenaar 				stack_entry = prev_entry;
3275b21a0008SMarcel Moolenaar 			else
3276b21a0008SMarcel Moolenaar 				stack_entry = next_entry;
3277b21a0008SMarcel Moolenaar 		} else
3278b21a0008SMarcel Moolenaar 			stack_entry = prev_entry;
3279b21a0008SMarcel Moolenaar 	}
328094f7e29aSAlan Cox 
3281b21a0008SMarcel Moolenaar 	if (stack_entry == next_entry) {
3282b21a0008SMarcel Moolenaar 		KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_DOWN, ("foo"));
3283b21a0008SMarcel Moolenaar 		KASSERT(addr < stack_entry->start, ("foo"));
3284b21a0008SMarcel Moolenaar 		end = (prev_entry != &map->header) ? prev_entry->end :
3285b21a0008SMarcel Moolenaar 		    stack_entry->start - stack_entry->avail_ssize;
328694f7e29aSAlan Cox 		grow_amount = roundup(stack_entry->start - addr, PAGE_SIZE);
3287b21a0008SMarcel Moolenaar 		max_grow = stack_entry->start - end;
3288b21a0008SMarcel Moolenaar 	} else {
3289b21a0008SMarcel Moolenaar 		KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_UP, ("foo"));
329008667f6dSMarcel Moolenaar 		KASSERT(addr >= stack_entry->end, ("foo"));
3291b21a0008SMarcel Moolenaar 		end = (next_entry != &map->header) ? next_entry->start :
3292b21a0008SMarcel Moolenaar 		    stack_entry->end + stack_entry->avail_ssize;
3293fd75d710SMarcel Moolenaar 		grow_amount = roundup(addr + 1 - stack_entry->end, PAGE_SIZE);
3294b21a0008SMarcel Moolenaar 		max_grow = end - stack_entry->end;
3295b21a0008SMarcel Moolenaar 	}
3296b21a0008SMarcel Moolenaar 
329794f7e29aSAlan Cox 	if (grow_amount > stack_entry->avail_ssize) {
329894f7e29aSAlan Cox 		vm_map_unlock_read(map);
32990cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
330094f7e29aSAlan Cox 	}
330194f7e29aSAlan Cox 
3302b21a0008SMarcel Moolenaar 	/*
3303b21a0008SMarcel Moolenaar 	 * If there is no longer enough space between the entries nogo, and
3304b21a0008SMarcel Moolenaar 	 * adjust the available space.  Note: this  should only happen if the
3305b21a0008SMarcel Moolenaar 	 * user has mapped into the stack area after the stack was created,
3306b21a0008SMarcel Moolenaar 	 * and is probably an error.
330794f7e29aSAlan Cox 	 *
3308b21a0008SMarcel Moolenaar 	 * This also effectively destroys any guard page the user might have
3309b21a0008SMarcel Moolenaar 	 * intended by limiting the stack size.
331094f7e29aSAlan Cox 	 */
3311b21a0008SMarcel Moolenaar 	if (grow_amount > max_grow) {
331225adb370SBrian Feldman 		if (vm_map_lock_upgrade(map))
331394f7e29aSAlan Cox 			goto Retry;
331494f7e29aSAlan Cox 
3315b21a0008SMarcel Moolenaar 		stack_entry->avail_ssize = max_grow;
331694f7e29aSAlan Cox 
331794f7e29aSAlan Cox 		vm_map_unlock(map);
33180cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
331994f7e29aSAlan Cox 	}
332094f7e29aSAlan Cox 
3321b21a0008SMarcel Moolenaar 	is_procstack = (addr >= (vm_offset_t)vm->vm_maxsaddr) ? 1 : 0;
332294f7e29aSAlan Cox 
3323b21a0008SMarcel Moolenaar 	/*
3324b21a0008SMarcel Moolenaar 	 * If this is the main process stack, see if we're over the stack
3325b21a0008SMarcel Moolenaar 	 * limit.
332694f7e29aSAlan Cox 	 */
332791d5354aSJohn Baldwin 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
332894f7e29aSAlan Cox 		vm_map_unlock_read(map);
33290cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
333094f7e29aSAlan Cox 	}
333194f7e29aSAlan Cox 
333294f7e29aSAlan Cox 	/* Round up the grow amount modulo SGROWSIZ */
3333cbc89bfbSPaul Saab 	grow_amount = roundup (grow_amount, sgrowsiz);
3334b21a0008SMarcel Moolenaar 	if (grow_amount > stack_entry->avail_ssize)
333594f7e29aSAlan Cox 		grow_amount = stack_entry->avail_ssize;
333691d5354aSJohn Baldwin 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
333791d5354aSJohn Baldwin 		grow_amount = stacklim - ctob(vm->vm_ssize);
333894f7e29aSAlan Cox 	}
333994f7e29aSAlan Cox 
3340a69ac174SMatthew Dillon 	/* If we would blow our VMEM resource limit, no go */
334191d5354aSJohn Baldwin 	if (map->size + grow_amount > vmemlim) {
3342a69ac174SMatthew Dillon 		vm_map_unlock_read(map);
3343a69ac174SMatthew Dillon 		return (KERN_NO_SPACE);
3344a69ac174SMatthew Dillon 	}
3345a69ac174SMatthew Dillon 
334625adb370SBrian Feldman 	if (vm_map_lock_upgrade(map))
334794f7e29aSAlan Cox 		goto Retry;
334894f7e29aSAlan Cox 
3349b21a0008SMarcel Moolenaar 	if (stack_entry == next_entry) {
3350b21a0008SMarcel Moolenaar 		/*
3351b21a0008SMarcel Moolenaar 		 * Growing downward.
3352b21a0008SMarcel Moolenaar 		 */
335394f7e29aSAlan Cox 		/* Get the preliminary new entry start value */
335494f7e29aSAlan Cox 		addr = stack_entry->start - grow_amount;
335594f7e29aSAlan Cox 
3356b21a0008SMarcel Moolenaar 		/*
3357b21a0008SMarcel Moolenaar 		 * If this puts us into the previous entry, cut back our
3358b21a0008SMarcel Moolenaar 		 * growth to the available space. Also, see the note above.
335994f7e29aSAlan Cox 		 */
336094f7e29aSAlan Cox 		if (addr < end) {
3361b21a0008SMarcel Moolenaar 			stack_entry->avail_ssize = max_grow;
336294f7e29aSAlan Cox 			addr = end;
336394f7e29aSAlan Cox 		}
336494f7e29aSAlan Cox 
336594f7e29aSAlan Cox 		rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start,
336605ba50f5SJake Burkholder 		    p->p_sysent->sv_stackprot, VM_PROT_ALL, 0);
336794f7e29aSAlan Cox 
336894f7e29aSAlan Cox 		/* Adjust the available stack space by the amount we grew. */
336994f7e29aSAlan Cox 		if (rv == KERN_SUCCESS) {
337029b45e9eSAlan Cox 			if (prev_entry != &map->header)
337129b45e9eSAlan Cox 				vm_map_clip_end(map, prev_entry, addr);
3372b21a0008SMarcel Moolenaar 			new_entry = prev_entry->next;
3373b21a0008SMarcel Moolenaar 			KASSERT(new_entry == stack_entry->prev, ("foo"));
3374b21a0008SMarcel Moolenaar 			KASSERT(new_entry->end == stack_entry->start, ("foo"));
3375b21a0008SMarcel Moolenaar 			KASSERT(new_entry->start == addr, ("foo"));
3376b21a0008SMarcel Moolenaar 			grow_amount = new_entry->end - new_entry->start;
3377b21a0008SMarcel Moolenaar 			new_entry->avail_ssize = stack_entry->avail_ssize -
3378b21a0008SMarcel Moolenaar 			    grow_amount;
3379b21a0008SMarcel Moolenaar 			stack_entry->eflags &= ~MAP_ENTRY_GROWS_DOWN;
3380b21a0008SMarcel Moolenaar 			new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
338194f7e29aSAlan Cox 		}
3382b21a0008SMarcel Moolenaar 	} else {
3383b21a0008SMarcel Moolenaar 		/*
3384b21a0008SMarcel Moolenaar 		 * Growing upward.
3385b21a0008SMarcel Moolenaar 		 */
3386b21a0008SMarcel Moolenaar 		addr = stack_entry->end + grow_amount;
3387b21a0008SMarcel Moolenaar 
3388b21a0008SMarcel Moolenaar 		/*
3389b21a0008SMarcel Moolenaar 		 * If this puts us into the next entry, cut back our growth
3390b21a0008SMarcel Moolenaar 		 * to the available space. Also, see the note above.
3391b21a0008SMarcel Moolenaar 		 */
3392b21a0008SMarcel Moolenaar 		if (addr > end) {
3393b21a0008SMarcel Moolenaar 			stack_entry->avail_ssize = end - stack_entry->end;
3394b21a0008SMarcel Moolenaar 			addr = end;
339594f7e29aSAlan Cox 		}
339694f7e29aSAlan Cox 
3397b21a0008SMarcel Moolenaar 		grow_amount = addr - stack_entry->end;
33983364c323SKonstantin Belousov 		uip = stack_entry->uip;
33993364c323SKonstantin Belousov 		if (uip == NULL && stack_entry->object.vm_object != NULL)
34003364c323SKonstantin Belousov 			uip = stack_entry->object.vm_object->uip;
34013364c323SKonstantin Belousov 		if (uip != NULL && !swap_reserve_by_uid(grow_amount, uip))
34023364c323SKonstantin Belousov 			rv = KERN_NO_SPACE;
3403b21a0008SMarcel Moolenaar 		/* Grow the underlying object if applicable. */
34043364c323SKonstantin Belousov 		else if (stack_entry->object.vm_object == NULL ||
3405b21a0008SMarcel Moolenaar 			 vm_object_coalesce(stack_entry->object.vm_object,
340657a21abaSAlan Cox 			 stack_entry->offset,
3407b21a0008SMarcel Moolenaar 			 (vm_size_t)(stack_entry->end - stack_entry->start),
34083364c323SKonstantin Belousov 			 (vm_size_t)grow_amount, uip != NULL)) {
340908667f6dSMarcel Moolenaar 			map->size += (addr - stack_entry->end);
3410b21a0008SMarcel Moolenaar 			/* Update the current entry. */
3411b21a0008SMarcel Moolenaar 			stack_entry->end = addr;
3412199c91abSMarcel Moolenaar 			stack_entry->avail_ssize -= grow_amount;
34130164e057SAlan Cox 			vm_map_entry_resize_free(map, stack_entry);
3414b21a0008SMarcel Moolenaar 			rv = KERN_SUCCESS;
3415b21a0008SMarcel Moolenaar 
3416b21a0008SMarcel Moolenaar 			if (next_entry != &map->header)
3417b21a0008SMarcel Moolenaar 				vm_map_clip_start(map, next_entry, addr);
3418b21a0008SMarcel Moolenaar 		} else
3419b21a0008SMarcel Moolenaar 			rv = KERN_FAILURE;
3420b21a0008SMarcel Moolenaar 	}
3421b21a0008SMarcel Moolenaar 
3422b21a0008SMarcel Moolenaar 	if (rv == KERN_SUCCESS && is_procstack)
3423b21a0008SMarcel Moolenaar 		vm->vm_ssize += btoc(grow_amount);
3424b21a0008SMarcel Moolenaar 
342594f7e29aSAlan Cox 	vm_map_unlock(map);
3426b21a0008SMarcel Moolenaar 
3427abd498aaSBruce M Simpson 	/*
3428abd498aaSBruce M Simpson 	 * Heed the MAP_WIREFUTURE flag if it was set for this process.
3429abd498aaSBruce M Simpson 	 */
3430b21a0008SMarcel Moolenaar 	if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE)) {
3431b21a0008SMarcel Moolenaar 		vm_map_wire(map,
3432b21a0008SMarcel Moolenaar 		    (stack_entry == next_entry) ? addr : addr - grow_amount,
3433b21a0008SMarcel Moolenaar 		    (stack_entry == next_entry) ? stack_entry->start : addr,
3434b21a0008SMarcel Moolenaar 		    (p->p_flag & P_SYSTEM)
3435b21a0008SMarcel Moolenaar 		    ? VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES
3436b21a0008SMarcel Moolenaar 		    : VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
3437b21a0008SMarcel Moolenaar 	}
3438abd498aaSBruce M Simpson 
34390cddd8f0SMatthew Dillon 	return (rv);
344094f7e29aSAlan Cox }
344194f7e29aSAlan Cox 
3442df8bae1dSRodney W. Grimes /*
34435856e12eSJohn Dyson  * Unshare the specified VM space for exec.  If other processes are
34445856e12eSJohn Dyson  * mapped to it, then create a new one.  The new vmspace is null.
34455856e12eSJohn Dyson  */
344689b57fcfSKonstantin Belousov int
34473ebc1248SPeter Wemm vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser)
34481b40f8c0SMatthew Dillon {
34495856e12eSJohn Dyson 	struct vmspace *oldvmspace = p->p_vmspace;
34505856e12eSJohn Dyson 	struct vmspace *newvmspace;
34515856e12eSJohn Dyson 
34523ebc1248SPeter Wemm 	newvmspace = vmspace_alloc(minuser, maxuser);
345389b57fcfSKonstantin Belousov 	if (newvmspace == NULL)
345489b57fcfSKonstantin Belousov 		return (ENOMEM);
345551ab6c28SAlan Cox 	newvmspace->vm_swrss = oldvmspace->vm_swrss;
34565856e12eSJohn Dyson 	/*
34575856e12eSJohn Dyson 	 * This code is written like this for prototype purposes.  The
34585856e12eSJohn Dyson 	 * goal is to avoid running down the vmspace here, but let the
34595856e12eSJohn Dyson 	 * other process's that are still using the vmspace to finally
34605856e12eSJohn Dyson 	 * run it down.  Even though there is little or no chance of blocking
34615856e12eSJohn Dyson 	 * here, it is a good idea to keep this form for future mods.
34625856e12eSJohn Dyson 	 */
346357051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
34645856e12eSJohn Dyson 	p->p_vmspace = newvmspace;
346557051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
34666617724cSJeff Roberson 	if (p == curthread->td_proc)
3467b40ce416SJulian Elischer 		pmap_activate(curthread);
3468b56ef1c1SJohn Baldwin 	vmspace_free(oldvmspace);
346989b57fcfSKonstantin Belousov 	return (0);
34705856e12eSJohn Dyson }
34715856e12eSJohn Dyson 
34725856e12eSJohn Dyson /*
34735856e12eSJohn Dyson  * Unshare the specified VM space for forcing COW.  This
34745856e12eSJohn Dyson  * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
34755856e12eSJohn Dyson  */
347689b57fcfSKonstantin Belousov int
34771b40f8c0SMatthew Dillon vmspace_unshare(struct proc *p)
34781b40f8c0SMatthew Dillon {
34795856e12eSJohn Dyson 	struct vmspace *oldvmspace = p->p_vmspace;
34805856e12eSJohn Dyson 	struct vmspace *newvmspace;
34813364c323SKonstantin Belousov 	vm_ooffset_t fork_charge;
34825856e12eSJohn Dyson 
34835856e12eSJohn Dyson 	if (oldvmspace->vm_refcnt == 1)
348489b57fcfSKonstantin Belousov 		return (0);
34853364c323SKonstantin Belousov 	fork_charge = 0;
34863364c323SKonstantin Belousov 	newvmspace = vmspace_fork(oldvmspace, &fork_charge);
348789b57fcfSKonstantin Belousov 	if (newvmspace == NULL)
348889b57fcfSKonstantin Belousov 		return (ENOMEM);
34893364c323SKonstantin Belousov 	if (!swap_reserve_by_uid(fork_charge, p->p_ucred->cr_ruidinfo)) {
34903364c323SKonstantin Belousov 		vmspace_free(newvmspace);
34913364c323SKonstantin Belousov 		return (ENOMEM);
34923364c323SKonstantin Belousov 	}
349357051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
34945856e12eSJohn Dyson 	p->p_vmspace = newvmspace;
349557051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
34966617724cSJeff Roberson 	if (p == curthread->td_proc)
3497b40ce416SJulian Elischer 		pmap_activate(curthread);
3498b56ef1c1SJohn Baldwin 	vmspace_free(oldvmspace);
349989b57fcfSKonstantin Belousov 	return (0);
35005856e12eSJohn Dyson }
35015856e12eSJohn Dyson 
35025856e12eSJohn Dyson /*
3503df8bae1dSRodney W. Grimes  *	vm_map_lookup:
3504df8bae1dSRodney W. Grimes  *
3505df8bae1dSRodney W. Grimes  *	Finds the VM object, offset, and
3506df8bae1dSRodney W. Grimes  *	protection for a given virtual address in the
3507df8bae1dSRodney W. Grimes  *	specified map, assuming a page fault of the
3508df8bae1dSRodney W. Grimes  *	type specified.
3509df8bae1dSRodney W. Grimes  *
3510df8bae1dSRodney W. Grimes  *	Leaves the map in question locked for read; return
3511df8bae1dSRodney W. Grimes  *	values are guaranteed until a vm_map_lookup_done
3512df8bae1dSRodney W. Grimes  *	call is performed.  Note that the map argument
3513df8bae1dSRodney W. Grimes  *	is in/out; the returned map must be used in
3514df8bae1dSRodney W. Grimes  *	the call to vm_map_lookup_done.
3515df8bae1dSRodney W. Grimes  *
3516df8bae1dSRodney W. Grimes  *	A handle (out_entry) is returned for use in
3517df8bae1dSRodney W. Grimes  *	vm_map_lookup_done, to make that fast.
3518df8bae1dSRodney W. Grimes  *
3519df8bae1dSRodney W. Grimes  *	If a lookup is requested with "write protection"
3520df8bae1dSRodney W. Grimes  *	specified, the map may be changed to perform virtual
3521df8bae1dSRodney W. Grimes  *	copying operations, although the data referenced will
3522df8bae1dSRodney W. Grimes  *	remain the same.
3523df8bae1dSRodney W. Grimes  */
3524df8bae1dSRodney W. Grimes int
3525b9dcd593SBruce Evans vm_map_lookup(vm_map_t *var_map,		/* IN/OUT */
3526b9dcd593SBruce Evans 	      vm_offset_t vaddr,
352747221757SJohn Dyson 	      vm_prot_t fault_typea,
3528b9dcd593SBruce Evans 	      vm_map_entry_t *out_entry,	/* OUT */
3529b9dcd593SBruce Evans 	      vm_object_t *object,		/* OUT */
3530b9dcd593SBruce Evans 	      vm_pindex_t *pindex,		/* OUT */
3531b9dcd593SBruce Evans 	      vm_prot_t *out_prot,		/* OUT */
35322d8acc0fSJohn Dyson 	      boolean_t *wired)			/* OUT */
3533df8bae1dSRodney W. Grimes {
3534c0877f10SJohn Dyson 	vm_map_entry_t entry;
3535c0877f10SJohn Dyson 	vm_map_t map = *var_map;
3536c0877f10SJohn Dyson 	vm_prot_t prot;
353747221757SJohn Dyson 	vm_prot_t fault_type = fault_typea;
35383364c323SKonstantin Belousov 	vm_object_t eobject;
35393364c323SKonstantin Belousov 	struct uidinfo *uip;
35403364c323SKonstantin Belousov 	vm_ooffset_t size;
3541df8bae1dSRodney W. Grimes 
3542df8bae1dSRodney W. Grimes RetryLookup:;
3543df8bae1dSRodney W. Grimes 
3544df8bae1dSRodney W. Grimes 	vm_map_lock_read(map);
3545df8bae1dSRodney W. Grimes 
3546df8bae1dSRodney W. Grimes 	/*
35474c3ef59eSAlan Cox 	 * Lookup the faulting address.
3548df8bae1dSRodney W. Grimes 	 */
3549095104acSAlan Cox 	if (!vm_map_lookup_entry(map, vaddr, out_entry)) {
3550095104acSAlan Cox 		vm_map_unlock_read(map);
3551095104acSAlan Cox 		return (KERN_INVALID_ADDRESS);
3552095104acSAlan Cox 	}
3553df8bae1dSRodney W. Grimes 
35544e94f402SAlan Cox 	entry = *out_entry;
3555b7b2aac2SJohn Dyson 
3556df8bae1dSRodney W. Grimes 	/*
3557df8bae1dSRodney W. Grimes 	 * Handle submaps.
3558df8bae1dSRodney W. Grimes 	 */
3559afa07f7eSJohn Dyson 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3560df8bae1dSRodney W. Grimes 		vm_map_t old_map = map;
3561df8bae1dSRodney W. Grimes 
3562df8bae1dSRodney W. Grimes 		*var_map = map = entry->object.sub_map;
3563df8bae1dSRodney W. Grimes 		vm_map_unlock_read(old_map);
3564df8bae1dSRodney W. Grimes 		goto RetryLookup;
3565df8bae1dSRodney W. Grimes 	}
3566a04c970aSJohn Dyson 
3567df8bae1dSRodney W. Grimes 	/*
35680d94caffSDavid Greenman 	 * Check whether this task is allowed to have this page.
3569df8bae1dSRodney W. Grimes 	 */
3570df8bae1dSRodney W. Grimes 	prot = entry->protection;
357147221757SJohn Dyson 	fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
35722db65ab4SAlan Cox 	if ((fault_type & prot) != fault_type || prot == VM_PROT_NONE) {
3573095104acSAlan Cox 		vm_map_unlock_read(map);
3574095104acSAlan Cox 		return (KERN_PROTECTION_FAILURE);
357547221757SJohn Dyson 	}
35762ed14a92SAlan Cox 	if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
357747221757SJohn Dyson 	    (entry->eflags & MAP_ENTRY_COW) &&
3578a6d42a0dSAlan Cox 	    (fault_type & VM_PROT_WRITE)) {
3579095104acSAlan Cox 		vm_map_unlock_read(map);
3580095104acSAlan Cox 		return (KERN_PROTECTION_FAILURE);
3581a04c970aSJohn Dyson 	}
3582df8bae1dSRodney W. Grimes 
3583df8bae1dSRodney W. Grimes 	/*
35840d94caffSDavid Greenman 	 * If this page is not pageable, we have to get it for all possible
35850d94caffSDavid Greenman 	 * accesses.
3586df8bae1dSRodney W. Grimes 	 */
358705f0fdd2SPoul-Henning Kamp 	*wired = (entry->wired_count != 0);
358805f0fdd2SPoul-Henning Kamp 	if (*wired)
3589a6d42a0dSAlan Cox 		fault_type = entry->protection;
35903364c323SKonstantin Belousov 	size = entry->end - entry->start;
3591df8bae1dSRodney W. Grimes 	/*
3592df8bae1dSRodney W. Grimes 	 * If the entry was copy-on-write, we either ...
3593df8bae1dSRodney W. Grimes 	 */
3594afa07f7eSJohn Dyson 	if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3595df8bae1dSRodney W. Grimes 		/*
35960d94caffSDavid Greenman 		 * If we want to write the page, we may as well handle that
3597ad5fca3bSAlan Cox 		 * now since we've got the map locked.
3598df8bae1dSRodney W. Grimes 		 *
35990d94caffSDavid Greenman 		 * If we don't need to write the page, we just demote the
36000d94caffSDavid Greenman 		 * permissions allowed.
3601df8bae1dSRodney W. Grimes 		 */
3602a6d42a0dSAlan Cox 		if ((fault_type & VM_PROT_WRITE) != 0 ||
3603a6d42a0dSAlan Cox 		    (fault_typea & VM_PROT_COPY) != 0) {
3604df8bae1dSRodney W. Grimes 			/*
36050d94caffSDavid Greenman 			 * Make a new object, and place it in the object
36060d94caffSDavid Greenman 			 * chain.  Note that no new references have appeared
3607ad5fca3bSAlan Cox 			 * -- one just moved from the map to the new
36080d94caffSDavid Greenman 			 * object.
3609df8bae1dSRodney W. Grimes 			 */
361025adb370SBrian Feldman 			if (vm_map_lock_upgrade(map))
3611df8bae1dSRodney W. Grimes 				goto RetryLookup;
36129917e010SAlan Cox 
36133364c323SKonstantin Belousov 			if (entry->uip == NULL) {
36143364c323SKonstantin Belousov 				/*
36153364c323SKonstantin Belousov 				 * The debugger owner is charged for
36163364c323SKonstantin Belousov 				 * the memory.
36173364c323SKonstantin Belousov 				 */
36183364c323SKonstantin Belousov 				uip = curthread->td_ucred->cr_ruidinfo;
36193364c323SKonstantin Belousov 				uihold(uip);
36203364c323SKonstantin Belousov 				if (!swap_reserve_by_uid(size, uip)) {
36213364c323SKonstantin Belousov 					uifree(uip);
36223364c323SKonstantin Belousov 					vm_map_unlock(map);
36233364c323SKonstantin Belousov 					return (KERN_RESOURCE_SHORTAGE);
36243364c323SKonstantin Belousov 				}
36253364c323SKonstantin Belousov 				entry->uip = uip;
36263364c323SKonstantin Belousov 			}
3627df8bae1dSRodney W. Grimes 			vm_object_shadow(
3628df8bae1dSRodney W. Grimes 			    &entry->object.vm_object,
3629df8bae1dSRodney W. Grimes 			    &entry->offset,
36303364c323SKonstantin Belousov 			    atop(size));
3631afa07f7eSJohn Dyson 			entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
36323364c323SKonstantin Belousov 			eobject = entry->object.vm_object;
36333364c323SKonstantin Belousov 			if (eobject->uip != NULL) {
36343364c323SKonstantin Belousov 				/*
36353364c323SKonstantin Belousov 				 * The object was not shadowed.
36363364c323SKonstantin Belousov 				 */
36373364c323SKonstantin Belousov 				swap_release_by_uid(size, entry->uip);
36383364c323SKonstantin Belousov 				uifree(entry->uip);
36393364c323SKonstantin Belousov 				entry->uip = NULL;
36403364c323SKonstantin Belousov 			} else if (entry->uip != NULL) {
36413364c323SKonstantin Belousov 				VM_OBJECT_LOCK(eobject);
36423364c323SKonstantin Belousov 				eobject->uip = entry->uip;
36433364c323SKonstantin Belousov 				eobject->charge = size;
36443364c323SKonstantin Belousov 				VM_OBJECT_UNLOCK(eobject);
36453364c323SKonstantin Belousov 				entry->uip = NULL;
36463364c323SKonstantin Belousov 			}
36479917e010SAlan Cox 
36489b09b6c7SMatthew Dillon 			vm_map_lock_downgrade(map);
36490d94caffSDavid Greenman 		} else {
3650df8bae1dSRodney W. Grimes 			/*
36510d94caffSDavid Greenman 			 * We're attempting to read a copy-on-write page --
36520d94caffSDavid Greenman 			 * don't allow writes.
3653df8bae1dSRodney W. Grimes 			 */
36542d8acc0fSJohn Dyson 			prot &= ~VM_PROT_WRITE;
3655df8bae1dSRodney W. Grimes 		}
3656df8bae1dSRodney W. Grimes 	}
36572d8acc0fSJohn Dyson 
3658df8bae1dSRodney W. Grimes 	/*
3659df8bae1dSRodney W. Grimes 	 * Create an object if necessary.
3660df8bae1dSRodney W. Grimes 	 */
36614e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL &&
36624e71e795SMatthew Dillon 	    !map->system_map) {
366325adb370SBrian Feldman 		if (vm_map_lock_upgrade(map))
3664df8bae1dSRodney W. Grimes 			goto RetryLookup;
366524a1cce3SDavid Greenman 		entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
36663364c323SKonstantin Belousov 		    atop(size));
3667df8bae1dSRodney W. Grimes 		entry->offset = 0;
36683364c323SKonstantin Belousov 		if (entry->uip != NULL) {
36693364c323SKonstantin Belousov 			VM_OBJECT_LOCK(entry->object.vm_object);
36703364c323SKonstantin Belousov 			entry->object.vm_object->uip = entry->uip;
36713364c323SKonstantin Belousov 			entry->object.vm_object->charge = size;
36723364c323SKonstantin Belousov 			VM_OBJECT_UNLOCK(entry->object.vm_object);
36733364c323SKonstantin Belousov 			entry->uip = NULL;
36743364c323SKonstantin Belousov 		}
36759b09b6c7SMatthew Dillon 		vm_map_lock_downgrade(map);
3676df8bae1dSRodney W. Grimes 	}
3677b5b40fa6SJohn Dyson 
3678df8bae1dSRodney W. Grimes 	/*
36790d94caffSDavid Greenman 	 * Return the object/offset from this entry.  If the entry was
36800d94caffSDavid Greenman 	 * copy-on-write or empty, it has been fixed up.
3681df8bae1dSRodney W. Grimes 	 */
36829b09b6c7SMatthew Dillon 	*pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
3683df8bae1dSRodney W. Grimes 	*object = entry->object.vm_object;
3684df8bae1dSRodney W. Grimes 
3685df8bae1dSRodney W. Grimes 	*out_prot = prot;
3686df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
3687df8bae1dSRodney W. Grimes }
3688df8bae1dSRodney W. Grimes 
3689df8bae1dSRodney W. Grimes /*
369019dc5607STor Egge  *	vm_map_lookup_locked:
369119dc5607STor Egge  *
369219dc5607STor Egge  *	Lookup the faulting address.  A version of vm_map_lookup that returns
369319dc5607STor Egge  *      KERN_FAILURE instead of blocking on map lock or memory allocation.
369419dc5607STor Egge  */
369519dc5607STor Egge int
369619dc5607STor Egge vm_map_lookup_locked(vm_map_t *var_map,		/* IN/OUT */
369719dc5607STor Egge 		     vm_offset_t vaddr,
369819dc5607STor Egge 		     vm_prot_t fault_typea,
369919dc5607STor Egge 		     vm_map_entry_t *out_entry,	/* OUT */
370019dc5607STor Egge 		     vm_object_t *object,	/* OUT */
370119dc5607STor Egge 		     vm_pindex_t *pindex,	/* OUT */
370219dc5607STor Egge 		     vm_prot_t *out_prot,	/* OUT */
370319dc5607STor Egge 		     boolean_t *wired)		/* OUT */
370419dc5607STor Egge {
370519dc5607STor Egge 	vm_map_entry_t entry;
370619dc5607STor Egge 	vm_map_t map = *var_map;
370719dc5607STor Egge 	vm_prot_t prot;
370819dc5607STor Egge 	vm_prot_t fault_type = fault_typea;
370919dc5607STor Egge 
371019dc5607STor Egge 	/*
37114c3ef59eSAlan Cox 	 * Lookup the faulting address.
371219dc5607STor Egge 	 */
371319dc5607STor Egge 	if (!vm_map_lookup_entry(map, vaddr, out_entry))
371419dc5607STor Egge 		return (KERN_INVALID_ADDRESS);
371519dc5607STor Egge 
371619dc5607STor Egge 	entry = *out_entry;
371719dc5607STor Egge 
371819dc5607STor Egge 	/*
371919dc5607STor Egge 	 * Fail if the entry refers to a submap.
372019dc5607STor Egge 	 */
372119dc5607STor Egge 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
372219dc5607STor Egge 		return (KERN_FAILURE);
372319dc5607STor Egge 
372419dc5607STor Egge 	/*
372519dc5607STor Egge 	 * Check whether this task is allowed to have this page.
372619dc5607STor Egge 	 */
372719dc5607STor Egge 	prot = entry->protection;
372819dc5607STor Egge 	fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
372919dc5607STor Egge 	if ((fault_type & prot) != fault_type)
373019dc5607STor Egge 		return (KERN_PROTECTION_FAILURE);
373119dc5607STor Egge 	if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
373219dc5607STor Egge 	    (entry->eflags & MAP_ENTRY_COW) &&
3733a6d42a0dSAlan Cox 	    (fault_type & VM_PROT_WRITE))
373419dc5607STor Egge 		return (KERN_PROTECTION_FAILURE);
373519dc5607STor Egge 
373619dc5607STor Egge 	/*
373719dc5607STor Egge 	 * If this page is not pageable, we have to get it for all possible
373819dc5607STor Egge 	 * accesses.
373919dc5607STor Egge 	 */
374019dc5607STor Egge 	*wired = (entry->wired_count != 0);
374119dc5607STor Egge 	if (*wired)
3742a6d42a0dSAlan Cox 		fault_type = entry->protection;
374319dc5607STor Egge 
374419dc5607STor Egge 	if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
374519dc5607STor Egge 		/*
374619dc5607STor Egge 		 * Fail if the entry was copy-on-write for a write fault.
374719dc5607STor Egge 		 */
374819dc5607STor Egge 		if (fault_type & VM_PROT_WRITE)
374919dc5607STor Egge 			return (KERN_FAILURE);
375019dc5607STor Egge 		/*
375119dc5607STor Egge 		 * We're attempting to read a copy-on-write page --
375219dc5607STor Egge 		 * don't allow writes.
375319dc5607STor Egge 		 */
375419dc5607STor Egge 		prot &= ~VM_PROT_WRITE;
375519dc5607STor Egge 	}
375619dc5607STor Egge 
375719dc5607STor Egge 	/*
375819dc5607STor Egge 	 * Fail if an object should be created.
375919dc5607STor Egge 	 */
376019dc5607STor Egge 	if (entry->object.vm_object == NULL && !map->system_map)
376119dc5607STor Egge 		return (KERN_FAILURE);
376219dc5607STor Egge 
376319dc5607STor Egge 	/*
376419dc5607STor Egge 	 * Return the object/offset from this entry.  If the entry was
376519dc5607STor Egge 	 * copy-on-write or empty, it has been fixed up.
376619dc5607STor Egge 	 */
376719dc5607STor Egge 	*pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
376819dc5607STor Egge 	*object = entry->object.vm_object;
376919dc5607STor Egge 
377019dc5607STor Egge 	*out_prot = prot;
377119dc5607STor Egge 	return (KERN_SUCCESS);
377219dc5607STor Egge }
377319dc5607STor Egge 
377419dc5607STor Egge /*
3775df8bae1dSRodney W. Grimes  *	vm_map_lookup_done:
3776df8bae1dSRodney W. Grimes  *
3777df8bae1dSRodney W. Grimes  *	Releases locks acquired by a vm_map_lookup
3778df8bae1dSRodney W. Grimes  *	(according to the handle returned by that lookup).
3779df8bae1dSRodney W. Grimes  */
37800d94caffSDavid Greenman void
37811b40f8c0SMatthew Dillon vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry)
3782df8bae1dSRodney W. Grimes {
3783df8bae1dSRodney W. Grimes 	/*
3784df8bae1dSRodney W. Grimes 	 * Unlock the main-level map
3785df8bae1dSRodney W. Grimes 	 */
3786df8bae1dSRodney W. Grimes 	vm_map_unlock_read(map);
3787df8bae1dSRodney W. Grimes }
3788df8bae1dSRodney W. Grimes 
3789c7c34a24SBruce Evans #include "opt_ddb.h"
3790c3cb3e12SDavid Greenman #ifdef DDB
3791c7c34a24SBruce Evans #include <sys/kernel.h>
3792c7c34a24SBruce Evans 
3793c7c34a24SBruce Evans #include <ddb/ddb.h>
3794c7c34a24SBruce Evans 
3795df8bae1dSRodney W. Grimes /*
3796df8bae1dSRodney W. Grimes  *	vm_map_print:	[ debug ]
3797df8bae1dSRodney W. Grimes  */
3798c7c34a24SBruce Evans DB_SHOW_COMMAND(map, vm_map_print)
3799df8bae1dSRodney W. Grimes {
380095e5e988SJohn Dyson 	static int nlines;
3801c7c34a24SBruce Evans 	/* XXX convert args. */
3802c0877f10SJohn Dyson 	vm_map_t map = (vm_map_t)addr;
3803c7c34a24SBruce Evans 	boolean_t full = have_addr;
3804df8bae1dSRodney W. Grimes 
3805c0877f10SJohn Dyson 	vm_map_entry_t entry;
3806c7c34a24SBruce Evans 
3807e5f251d2SAlan Cox 	db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
3808e5f251d2SAlan Cox 	    (void *)map,
3809101eeb7fSBruce Evans 	    (void *)map->pmap, map->nentries, map->timestamp);
381095e5e988SJohn Dyson 	nlines++;
3811df8bae1dSRodney W. Grimes 
3812c7c34a24SBruce Evans 	if (!full && db_indent)
3813df8bae1dSRodney W. Grimes 		return;
3814df8bae1dSRodney W. Grimes 
3815c7c34a24SBruce Evans 	db_indent += 2;
3816df8bae1dSRodney W. Grimes 	for (entry = map->header.next; entry != &map->header;
3817df8bae1dSRodney W. Grimes 	    entry = entry->next) {
3818fc62ef1fSBruce Evans 		db_iprintf("map entry %p: start=%p, end=%p\n",
3819fc62ef1fSBruce Evans 		    (void *)entry, (void *)entry->start, (void *)entry->end);
382095e5e988SJohn Dyson 		nlines++;
3821e5f251d2SAlan Cox 		{
3822df8bae1dSRodney W. Grimes 			static char *inheritance_name[4] =
3823df8bae1dSRodney W. Grimes 			{"share", "copy", "none", "donate_copy"};
38240d94caffSDavid Greenman 
382595e5e988SJohn Dyson 			db_iprintf(" prot=%x/%x/%s",
3826df8bae1dSRodney W. Grimes 			    entry->protection,
3827df8bae1dSRodney W. Grimes 			    entry->max_protection,
38288aef1712SMatthew Dillon 			    inheritance_name[(int)(unsigned char)entry->inheritance]);
3829df8bae1dSRodney W. Grimes 			if (entry->wired_count != 0)
383095e5e988SJohn Dyson 				db_printf(", wired");
3831df8bae1dSRodney W. Grimes 		}
38329fdfe602SMatthew Dillon 		if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3833cd034a5bSMaxime Henrion 			db_printf(", share=%p, offset=0x%jx\n",
38349fdfe602SMatthew Dillon 			    (void *)entry->object.sub_map,
3835cd034a5bSMaxime Henrion 			    (uintmax_t)entry->offset);
383695e5e988SJohn Dyson 			nlines++;
3837df8bae1dSRodney W. Grimes 			if ((entry->prev == &map->header) ||
38389fdfe602SMatthew Dillon 			    (entry->prev->object.sub_map !=
38399fdfe602SMatthew Dillon 				entry->object.sub_map)) {
3840c7c34a24SBruce Evans 				db_indent += 2;
3841101eeb7fSBruce Evans 				vm_map_print((db_expr_t)(intptr_t)
38429fdfe602SMatthew Dillon 					     entry->object.sub_map,
3843914181e7SBruce Evans 					     full, 0, (char *)0);
3844c7c34a24SBruce Evans 				db_indent -= 2;
3845df8bae1dSRodney W. Grimes 			}
38460d94caffSDavid Greenman 		} else {
38473364c323SKonstantin Belousov 			if (entry->uip != NULL)
38483364c323SKonstantin Belousov 				db_printf(", uip %d", entry->uip->ui_uid);
3849cd034a5bSMaxime Henrion 			db_printf(", object=%p, offset=0x%jx",
3850101eeb7fSBruce Evans 			    (void *)entry->object.vm_object,
3851cd034a5bSMaxime Henrion 			    (uintmax_t)entry->offset);
38523364c323SKonstantin Belousov 			if (entry->object.vm_object && entry->object.vm_object->uip)
38533364c323SKonstantin Belousov 				db_printf(", obj uip %d charge %jx",
38543364c323SKonstantin Belousov 				    entry->object.vm_object->uip->ui_uid,
38553364c323SKonstantin Belousov 				    (uintmax_t)entry->object.vm_object->charge);
3856afa07f7eSJohn Dyson 			if (entry->eflags & MAP_ENTRY_COW)
3857c7c34a24SBruce Evans 				db_printf(", copy (%s)",
3858afa07f7eSJohn Dyson 				    (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
3859c7c34a24SBruce Evans 			db_printf("\n");
386095e5e988SJohn Dyson 			nlines++;
3861df8bae1dSRodney W. Grimes 
3862df8bae1dSRodney W. Grimes 			if ((entry->prev == &map->header) ||
3863df8bae1dSRodney W. Grimes 			    (entry->prev->object.vm_object !=
3864df8bae1dSRodney W. Grimes 				entry->object.vm_object)) {
3865c7c34a24SBruce Evans 				db_indent += 2;
3866101eeb7fSBruce Evans 				vm_object_print((db_expr_t)(intptr_t)
3867101eeb7fSBruce Evans 						entry->object.vm_object,
3868914181e7SBruce Evans 						full, 0, (char *)0);
386995e5e988SJohn Dyson 				nlines += 4;
3870c7c34a24SBruce Evans 				db_indent -= 2;
3871df8bae1dSRodney W. Grimes 			}
3872df8bae1dSRodney W. Grimes 		}
3873df8bae1dSRodney W. Grimes 	}
3874c7c34a24SBruce Evans 	db_indent -= 2;
387595e5e988SJohn Dyson 	if (db_indent == 0)
387695e5e988SJohn Dyson 		nlines = 0;
3877df8bae1dSRodney W. Grimes }
387895e5e988SJohn Dyson 
387995e5e988SJohn Dyson 
388095e5e988SJohn Dyson DB_SHOW_COMMAND(procvm, procvm)
388195e5e988SJohn Dyson {
388295e5e988SJohn Dyson 	struct proc *p;
388395e5e988SJohn Dyson 
388495e5e988SJohn Dyson 	if (have_addr) {
388595e5e988SJohn Dyson 		p = (struct proc *) addr;
388695e5e988SJohn Dyson 	} else {
388795e5e988SJohn Dyson 		p = curproc;
388895e5e988SJohn Dyson 	}
388995e5e988SJohn Dyson 
3890ac1e407bSBruce Evans 	db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
3891ac1e407bSBruce Evans 	    (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
3892b1028ad1SLuoqi Chen 	    (void *)vmspace_pmap(p->p_vmspace));
389395e5e988SJohn Dyson 
3894101eeb7fSBruce Evans 	vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);
389595e5e988SJohn Dyson }
389695e5e988SJohn Dyson 
3897c7c34a24SBruce Evans #endif /* DDB */
3898