xref: /freebsd/sys/vm/vm_map.c (revision c606ab59e7f9423f7027320e9a4514c7db39658d)
160727d8bSWarner Losh /*-
2796df753SPedro F. Giffuni  * SPDX-License-Identifier: (BSD-3-Clause AND MIT-CMU)
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1991, 1993
5df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6df8bae1dSRodney W. Grimes  *
7df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
8df8bae1dSRodney W. Grimes  * The Mach Operating System project at Carnegie-Mellon University.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
19df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
20df8bae1dSRodney W. Grimes  *    without specific prior written permission.
21df8bae1dSRodney W. Grimes  *
22df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
33df8bae1dSRodney W. Grimes  *
343c4dd356SDavid Greenman  *	from: @(#)vm_map.c	8.3 (Berkeley) 1/12/94
35df8bae1dSRodney W. Grimes  *
36df8bae1dSRodney W. Grimes  *
37df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
38df8bae1dSRodney W. Grimes  * All rights reserved.
39df8bae1dSRodney W. Grimes  *
40df8bae1dSRodney W. Grimes  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
41df8bae1dSRodney W. Grimes  *
42df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
43df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
44df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
45df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
46df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
47df8bae1dSRodney W. Grimes  *
48df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
49df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
50df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
51df8bae1dSRodney W. Grimes  *
52df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
53df8bae1dSRodney W. Grimes  *
54df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
55df8bae1dSRodney W. Grimes  *  School of Computer Science
56df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
57df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
58df8bae1dSRodney W. Grimes  *
59df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
60df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
61df8bae1dSRodney W. Grimes  */
62df8bae1dSRodney W. Grimes 
63df8bae1dSRodney W. Grimes /*
64df8bae1dSRodney W. Grimes  *	Virtual memory mapping module.
65df8bae1dSRodney W. Grimes  */
66df8bae1dSRodney W. Grimes 
67874651b1SDavid E. O'Brien #include <sys/cdefs.h>
68874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
69874651b1SDavid E. O'Brien 
70df8bae1dSRodney W. Grimes #include <sys/param.h>
71df8bae1dSRodney W. Grimes #include <sys/systm.h>
72156e8654SKonstantin Belousov #include <sys/elf.h>
739a6d144fSKonstantin Belousov #include <sys/kernel.h>
7461d80e90SJohn Baldwin #include <sys/ktr.h>
75fb919e4dSMark Murray #include <sys/lock.h>
76fb919e4dSMark Murray #include <sys/mutex.h>
77b5e8ce9fSBruce Evans #include <sys/proc.h>
78efeaf95aSDavid Greenman #include <sys/vmmeter.h>
79867a482dSJohn Dyson #include <sys/mman.h>
801efb74fbSJohn Dyson #include <sys/vnode.h>
811ba5ad42SEdward Tomasz Napierala #include <sys/racct.h>
822267af78SJulian Elischer #include <sys/resourcevar.h>
8389f6b863SAttilio Rao #include <sys/rwlock.h>
843fde38dfSMike Silbersack #include <sys/file.h>
859a6d144fSKonstantin Belousov #include <sys/sysctl.h>
8605ba50f5SJake Burkholder #include <sys/sysent.h>
873db161e0SMatthew Dillon #include <sys/shm.h>
88df8bae1dSRodney W. Grimes 
89df8bae1dSRodney W. Grimes #include <vm/vm.h>
90efeaf95aSDavid Greenman #include <vm/vm_param.h>
91efeaf95aSDavid Greenman #include <vm/pmap.h>
92efeaf95aSDavid Greenman #include <vm/vm_map.h>
93df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
9454a3a114SMark Johnston #include <vm/vm_pageout.h>
95df8bae1dSRodney W. Grimes #include <vm/vm_object.h>
9647221757SJohn Dyson #include <vm/vm_pager.h>
9726f9a767SRodney W. Grimes #include <vm/vm_kern.h>
98efeaf95aSDavid Greenman #include <vm/vm_extern.h>
9984110e7eSKonstantin Belousov #include <vm/vnode_pager.h>
10021cd6e62SSeigo Tanimura #include <vm/swap_pager.h>
101670d17b5SJeff Roberson #include <vm/uma.h>
102df8bae1dSRodney W. Grimes 
103df8bae1dSRodney W. Grimes /*
104df8bae1dSRodney W. Grimes  *	Virtual memory maps provide for the mapping, protection,
105df8bae1dSRodney W. Grimes  *	and sharing of virtual memory objects.  In addition,
106df8bae1dSRodney W. Grimes  *	this module provides for an efficient virtual copy of
107df8bae1dSRodney W. Grimes  *	memory from one map to another.
108df8bae1dSRodney W. Grimes  *
109df8bae1dSRodney W. Grimes  *	Synchronization is required prior to most operations.
110df8bae1dSRodney W. Grimes  *
111df8bae1dSRodney W. Grimes  *	Maps consist of an ordered doubly-linked list of simple
112e2abaaaaSAlan Cox  *	entries; a self-adjusting binary search tree of these
113e2abaaaaSAlan Cox  *	entries is used to speed up lookups.
114df8bae1dSRodney W. Grimes  *
115956f3135SPhilippe Charnier  *	Since portions of maps are specified by start/end addresses,
116df8bae1dSRodney W. Grimes  *	which may not align with existing map entries, all
117df8bae1dSRodney W. Grimes  *	routines merely "clip" entries to these start/end values.
118df8bae1dSRodney W. Grimes  *	[That is, an entry is split into two, bordering at a
119df8bae1dSRodney W. Grimes  *	start or end value.]  Note that these clippings may not
120df8bae1dSRodney W. Grimes  *	always be necessary (as the two resulting entries are then
121df8bae1dSRodney W. Grimes  *	not changed); however, the clipping is done for convenience.
122df8bae1dSRodney W. Grimes  *
123df8bae1dSRodney W. Grimes  *	As mentioned above, virtual copy operations are performed
124ad5fca3bSAlan Cox  *	by copying VM object references from one map to
125df8bae1dSRodney W. Grimes  *	another, and then marking both regions as copy-on-write.
126df8bae1dSRodney W. Grimes  */
127df8bae1dSRodney W. Grimes 
1283a92e5d5SAlan Cox static struct mtx map_sleep_mtx;
1298355f576SJeff Roberson static uma_zone_t mapentzone;
1308355f576SJeff Roberson static uma_zone_t kmapentzone;
1318355f576SJeff Roberson static uma_zone_t vmspace_zone;
132b23f72e9SBrian Feldman static int vmspace_zinit(void *mem, int size, int flags);
13392351f16SAlan Cox static void _vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min,
13492351f16SAlan Cox     vm_offset_t max);
1350b367bd8SKonstantin Belousov static void vm_map_entry_deallocate(vm_map_entry_t entry, boolean_t system_map);
136655c3490SKonstantin Belousov static void vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry);
13703462509SAlan Cox static void vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry);
13819bd0d9cSKonstantin Belousov static int vm_map_growstack(vm_map_t map, vm_offset_t addr,
13919bd0d9cSKonstantin Belousov     vm_map_entry_t gap_entry);
140077ec27cSAlan Cox static void vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
141077ec27cSAlan Cox     vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags);
1428355f576SJeff Roberson #ifdef INVARIANTS
1438355f576SJeff Roberson static void vmspace_zdtor(void *mem, int size, void *arg);
1448355f576SJeff Roberson #endif
1454648ba0aSKonstantin Belousov static int vm_map_stack_locked(vm_map_t map, vm_offset_t addrbos,
1464648ba0aSKonstantin Belousov     vm_size_t max_ssize, vm_size_t growsize, vm_prot_t prot, vm_prot_t max,
1474648ba0aSKonstantin Belousov     int cow);
14866cd575bSAlan Cox static void vm_map_wire_entry_failure(vm_map_t map, vm_map_entry_t entry,
14966cd575bSAlan Cox     vm_offset_t failed_addr);
150b18bfc3dSJohn Dyson 
151ef694c1aSEdward Tomasz Napierala #define	ENTRY_CHARGED(e) ((e)->cred != NULL || \
152ef694c1aSEdward Tomasz Napierala     ((e)->object.vm_object != NULL && (e)->object.vm_object->cred != NULL && \
1533364c323SKonstantin Belousov      !((e)->eflags & MAP_ENTRY_NEEDS_COPY)))
1543364c323SKonstantin Belousov 
15557051fdcSTor Egge /*
15657051fdcSTor Egge  * PROC_VMSPACE_{UN,}LOCK() can be a noop as long as vmspaces are type
15757051fdcSTor Egge  * stable.
15857051fdcSTor Egge  */
15957051fdcSTor Egge #define PROC_VMSPACE_LOCK(p) do { } while (0)
16057051fdcSTor Egge #define PROC_VMSPACE_UNLOCK(p) do { } while (0)
16157051fdcSTor Egge 
162d239bd3cSKonstantin Belousov /*
163d239bd3cSKonstantin Belousov  *	VM_MAP_RANGE_CHECK:	[ internal use only ]
164d239bd3cSKonstantin Belousov  *
165d239bd3cSKonstantin Belousov  *	Asserts that the starting and ending region
166d239bd3cSKonstantin Belousov  *	addresses fall within the valid range of the map.
167d239bd3cSKonstantin Belousov  */
168d239bd3cSKonstantin Belousov #define	VM_MAP_RANGE_CHECK(map, start, end)		\
169d239bd3cSKonstantin Belousov 		{					\
170d239bd3cSKonstantin Belousov 		if (start < vm_map_min(map))		\
171d239bd3cSKonstantin Belousov 			start = vm_map_min(map);	\
172d239bd3cSKonstantin Belousov 		if (end > vm_map_max(map))		\
173d239bd3cSKonstantin Belousov 			end = vm_map_max(map);		\
174d239bd3cSKonstantin Belousov 		if (start > end)			\
175d239bd3cSKonstantin Belousov 			start = end;			\
176d239bd3cSKonstantin Belousov 		}
177d239bd3cSKonstantin Belousov 
17820f02659SMark Johnston #ifndef UMA_MD_SMALL_ALLOC
17920f02659SMark Johnston 
18020f02659SMark Johnston /*
18120f02659SMark Johnston  * Allocate a new slab for kernel map entries.  The kernel map may be locked or
18220f02659SMark Johnston  * unlocked, depending on whether the request is coming from the kernel map or a
18320f02659SMark Johnston  * submap.  This function allocates a virtual address range directly from the
18420f02659SMark Johnston  * kernel map instead of the kmem_* layer to avoid recursion on the kernel map
18520f02659SMark Johnston  * lock and also to avoid triggering allocator recursion in the vmem boundary
18620f02659SMark Johnston  * tag allocator.
18720f02659SMark Johnston  */
18820f02659SMark Johnston static void *
18920f02659SMark Johnston kmapent_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag,
19020f02659SMark Johnston     int wait)
19120f02659SMark Johnston {
19220f02659SMark Johnston 	vm_offset_t addr;
19320f02659SMark Johnston 	int error, locked;
19420f02659SMark Johnston 
19520f02659SMark Johnston 	*pflag = UMA_SLAB_PRIV;
19620f02659SMark Johnston 
19720f02659SMark Johnston 	if (!(locked = vm_map_locked(kernel_map)))
19820f02659SMark Johnston 		vm_map_lock(kernel_map);
19920f02659SMark Johnston 	addr = vm_map_findspace(kernel_map, vm_map_min(kernel_map), bytes);
20020f02659SMark Johnston 	if (addr + bytes < addr || addr + bytes > vm_map_max(kernel_map))
20120f02659SMark Johnston 		panic("%s: kernel map is exhausted", __func__);
20220f02659SMark Johnston 	error = vm_map_insert(kernel_map, NULL, 0, addr, addr + bytes,
20320f02659SMark Johnston 	    VM_PROT_RW, VM_PROT_RW, MAP_NOFAULT);
20420f02659SMark Johnston 	if (error != KERN_SUCCESS)
20520f02659SMark Johnston 		panic("%s: vm_map_insert() failed: %d", __func__, error);
20620f02659SMark Johnston 	if (!locked)
20720f02659SMark Johnston 		vm_map_unlock(kernel_map);
20820f02659SMark Johnston 	error = kmem_back_domain(domain, kernel_object, addr, bytes, M_NOWAIT |
20920f02659SMark Johnston 	    M_USE_RESERVE | (wait & M_ZERO));
21020f02659SMark Johnston 	if (error == KERN_SUCCESS) {
21120f02659SMark Johnston 		return ((void *)addr);
21220f02659SMark Johnston 	} else {
21320f02659SMark Johnston 		if (!locked)
21420f02659SMark Johnston 			vm_map_lock(kernel_map);
21520f02659SMark Johnston 		vm_map_delete(kernel_map, addr, bytes);
21620f02659SMark Johnston 		if (!locked)
21720f02659SMark Johnston 			vm_map_unlock(kernel_map);
21820f02659SMark Johnston 		return (NULL);
21920f02659SMark Johnston 	}
22020f02659SMark Johnston }
22120f02659SMark Johnston 
22220f02659SMark Johnston static void
22320f02659SMark Johnston kmapent_free(void *item, vm_size_t size, uint8_t pflag)
22420f02659SMark Johnston {
22520f02659SMark Johnston 	vm_offset_t addr;
22620f02659SMark Johnston 	int error;
22720f02659SMark Johnston 
22820f02659SMark Johnston 	if ((pflag & UMA_SLAB_PRIV) == 0)
22920f02659SMark Johnston 		/* XXX leaked */
23020f02659SMark Johnston 		return;
23120f02659SMark Johnston 
23220f02659SMark Johnston 	addr = (vm_offset_t)item;
23320f02659SMark Johnston 	kmem_unback(kernel_object, addr, size);
23420f02659SMark Johnston 	error = vm_map_remove(kernel_map, addr, addr + size);
23520f02659SMark Johnston 	KASSERT(error == KERN_SUCCESS,
23620f02659SMark Johnston 	    ("%s: vm_map_remove failed: %d", __func__, error));
23720f02659SMark Johnston }
23820f02659SMark Johnston 
23920f02659SMark Johnston /*
24020f02659SMark Johnston  * The worst-case upper bound on the number of kernel map entries that may be
24120f02659SMark Johnston  * created before the zone must be replenished in _vm_map_unlock().
24220f02659SMark Johnston  */
24320f02659SMark Johnston #define	KMAPENT_RESERVE		1
24420f02659SMark Johnston 
24520f02659SMark Johnston #endif /* !UMD_MD_SMALL_ALLOC */
24620f02659SMark Johnston 
2476fecb26bSKonstantin Belousov /*
2486fecb26bSKonstantin Belousov  *	vm_map_startup:
2496fecb26bSKonstantin Belousov  *
25020f02659SMark Johnston  *	Initialize the vm_map module.  Must be called before any other vm_map
25120f02659SMark Johnston  *	routines.
2526fecb26bSKonstantin Belousov  *
25320f02659SMark Johnston  *	User map and entry structures are allocated from the general purpose
25420f02659SMark Johnston  *	memory pool.  Kernel maps are statically defined.  Kernel map entries
25520f02659SMark Johnston  *	require special handling to avoid recursion; see the comments above
25620f02659SMark Johnston  *	kmapent_alloc() and in vm_map_entry_create().
2576fecb26bSKonstantin Belousov  */
2580d94caffSDavid Greenman void
2591b40f8c0SMatthew Dillon vm_map_startup(void)
260df8bae1dSRodney W. Grimes {
2613a92e5d5SAlan Cox 	mtx_init(&map_sleep_mtx, "vm map sleep mutex", NULL, MTX_DEF);
26220f02659SMark Johnston 
26320f02659SMark Johnston 	/*
26420f02659SMark Johnston 	 * Disable the use of per-CPU buckets: map entry allocation is
26520f02659SMark Johnston 	 * serialized by the kernel map lock.
26620f02659SMark Johnston 	 */
267670d17b5SJeff Roberson 	kmapentzone = uma_zcreate("KMAP ENTRY", sizeof(struct vm_map_entry),
26818aa2de5SJeff Roberson 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
26920f02659SMark Johnston 	    UMA_ZONE_VM | UMA_ZONE_NOBUCKET);
27020f02659SMark Johnston #ifndef UMA_MD_SMALL_ALLOC
27120f02659SMark Johnston 	/* Reserve an extra map entry for use when replenishing the reserve. */
27220f02659SMark Johnston 	uma_zone_reserve(kmapentzone, KMAPENT_RESERVE + 1);
27320f02659SMark Johnston 	uma_prealloc(kmapentzone, KMAPENT_RESERVE + 1);
27420f02659SMark Johnston 	uma_zone_set_allocf(kmapentzone, kmapent_alloc);
27520f02659SMark Johnston 	uma_zone_set_freef(kmapentzone, kmapent_free);
27620f02659SMark Johnston #endif
27720f02659SMark Johnston 
278670d17b5SJeff Roberson 	mapentzone = uma_zcreate("MAP ENTRY", sizeof(struct vm_map_entry),
279670d17b5SJeff Roberson 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
2805df87b21SJeff Roberson 	vmspace_zone = uma_zcreate("VMSPACE", sizeof(struct vmspace), NULL,
2815df87b21SJeff Roberson #ifdef INVARIANTS
2825df87b21SJeff Roberson 	    vmspace_zdtor,
2835df87b21SJeff Roberson #else
2845df87b21SJeff Roberson 	    NULL,
2855df87b21SJeff Roberson #endif
286f872f6eaSAlan Cox 	    vmspace_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
2878355f576SJeff Roberson }
2888355f576SJeff Roberson 
289b23f72e9SBrian Feldman static int
290b23f72e9SBrian Feldman vmspace_zinit(void *mem, int size, int flags)
2918355f576SJeff Roberson {
2928355f576SJeff Roberson 	struct vmspace *vm;
2938355f576SJeff Roberson 	vm_map_t map;
2948355f576SJeff Roberson 
2957dd979dfSMark Johnston 	vm = (struct vmspace *)mem;
2967dd979dfSMark Johnston 	map = &vm->vm_map;
2977dd979dfSMark Johnston 
298763d9566STim Kientzle 	memset(map, 0, sizeof(*map));
2997dd979dfSMark Johnston 	mtx_init(&map->system_mtx, "vm map (system)", NULL,
3007dd979dfSMark Johnston 	    MTX_DEF | MTX_DUPOK);
301e30df26eSAlan Cox 	sx_init(&map->lock, "vm map (user)");
3027dd979dfSMark Johnston 	PMAP_LOCK_INIT(vmspace_pmap(vm));
303b23f72e9SBrian Feldman 	return (0);
3048355f576SJeff Roberson }
3058355f576SJeff Roberson 
3068355f576SJeff Roberson #ifdef INVARIANTS
3078355f576SJeff Roberson static void
3088355f576SJeff Roberson vmspace_zdtor(void *mem, int size, void *arg)
3098355f576SJeff Roberson {
3108355f576SJeff Roberson 	struct vmspace *vm;
3118355f576SJeff Roberson 
3128355f576SJeff Roberson 	vm = (struct vmspace *)mem;
3137dd979dfSMark Johnston 	KASSERT(vm->vm_map.nentries == 0,
3147dd979dfSMark Johnston 	    ("vmspace %p nentries == %d on free", vm, vm->vm_map.nentries));
3157dd979dfSMark Johnston 	KASSERT(vm->vm_map.size == 0,
3167dd979dfSMark Johnston 	    ("vmspace %p size == %ju on free", vm, (uintmax_t)vm->vm_map.size));
3178355f576SJeff Roberson }
3188355f576SJeff Roberson #endif	/* INVARIANTS */
3198355f576SJeff Roberson 
320df8bae1dSRodney W. Grimes /*
321df8bae1dSRodney W. Grimes  * Allocate a vmspace structure, including a vm_map and pmap,
322df8bae1dSRodney W. Grimes  * and initialize those structures.  The refcnt is set to 1.
323df8bae1dSRodney W. Grimes  */
324df8bae1dSRodney W. Grimes struct vmspace *
32574d1d2b7SNeel Natu vmspace_alloc(vm_offset_t min, vm_offset_t max, pmap_pinit_t pinit)
326df8bae1dSRodney W. Grimes {
327c0877f10SJohn Dyson 	struct vmspace *vm;
3280d94caffSDavid Greenman 
329a163d034SWarner Losh 	vm = uma_zalloc(vmspace_zone, M_WAITOK);
33074d1d2b7SNeel Natu 	KASSERT(vm->vm_map.pmap == NULL, ("vm_map.pmap must be NULL"));
33174d1d2b7SNeel Natu 	if (!pinit(vmspace_pmap(vm))) {
33289b57fcfSKonstantin Belousov 		uma_zfree(vmspace_zone, vm);
33389b57fcfSKonstantin Belousov 		return (NULL);
33489b57fcfSKonstantin Belousov 	}
33521c641b2SJohn Baldwin 	CTR1(KTR_VM, "vmspace_alloc: %p", vm);
33692351f16SAlan Cox 	_vm_map_init(&vm->vm_map, vmspace_pmap(vm), min, max);
337f7db0c95SMark Johnston 	refcount_init(&vm->vm_refcnt, 1);
3382d8acc0fSJohn Dyson 	vm->vm_shm = NULL;
33951ab6c28SAlan Cox 	vm->vm_swrss = 0;
34051ab6c28SAlan Cox 	vm->vm_tsize = 0;
34151ab6c28SAlan Cox 	vm->vm_dsize = 0;
34251ab6c28SAlan Cox 	vm->vm_ssize = 0;
34351ab6c28SAlan Cox 	vm->vm_taddr = 0;
34451ab6c28SAlan Cox 	vm->vm_daddr = 0;
34551ab6c28SAlan Cox 	vm->vm_maxsaddr = 0;
346889b56c8SDawid Gorecki 	vm->vm_stkgap = 0;
347df8bae1dSRodney W. Grimes 	return (vm);
348df8bae1dSRodney W. Grimes }
349df8bae1dSRodney W. Grimes 
3504b5c9cf6SEdward Tomasz Napierala #ifdef RACCT
3511ba5ad42SEdward Tomasz Napierala static void
3521ba5ad42SEdward Tomasz Napierala vmspace_container_reset(struct proc *p)
3531ba5ad42SEdward Tomasz Napierala {
3541ba5ad42SEdward Tomasz Napierala 
3551ba5ad42SEdward Tomasz Napierala 	PROC_LOCK(p);
3561ba5ad42SEdward Tomasz Napierala 	racct_set(p, RACCT_DATA, 0);
3571ba5ad42SEdward Tomasz Napierala 	racct_set(p, RACCT_STACK, 0);
3581ba5ad42SEdward Tomasz Napierala 	racct_set(p, RACCT_RSS, 0);
3591ba5ad42SEdward Tomasz Napierala 	racct_set(p, RACCT_MEMLOCK, 0);
3601ba5ad42SEdward Tomasz Napierala 	racct_set(p, RACCT_VMEM, 0);
3611ba5ad42SEdward Tomasz Napierala 	PROC_UNLOCK(p);
3621ba5ad42SEdward Tomasz Napierala }
3634b5c9cf6SEdward Tomasz Napierala #endif
3641ba5ad42SEdward Tomasz Napierala 
36562a59e8fSWarner Losh static inline void
366582ec34cSAlfred Perlstein vmspace_dofree(struct vmspace *vm)
367df8bae1dSRodney W. Grimes {
3680ef12795SAlan Cox 
36921c641b2SJohn Baldwin 	CTR1(KTR_VM, "vmspace_free: %p", vm);
3703db161e0SMatthew Dillon 
3713db161e0SMatthew Dillon 	/*
3723db161e0SMatthew Dillon 	 * Make sure any SysV shm is freed, it might not have been in
3733db161e0SMatthew Dillon 	 * exit1().
3743db161e0SMatthew Dillon 	 */
3753db161e0SMatthew Dillon 	shmexit(vm);
3763db161e0SMatthew Dillon 
37730dcfc09SJohn Dyson 	/*
378df8bae1dSRodney W. Grimes 	 * Lock the map, to wait out all other references to it.
3790d94caffSDavid Greenman 	 * Delete all of the mappings and pages they hold, then call
3800d94caffSDavid Greenman 	 * the pmap module to reclaim anything left.
381df8bae1dSRodney W. Grimes 	 */
382f0165b1cSKonstantin Belousov 	(void)vm_map_remove(&vm->vm_map, vm_map_min(&vm->vm_map),
383f0165b1cSKonstantin Belousov 	    vm_map_max(&vm->vm_map));
3848355f576SJeff Roberson 
3850ef12795SAlan Cox 	pmap_release(vmspace_pmap(vm));
3860ef12795SAlan Cox 	vm->vm_map.pmap = NULL;
3878355f576SJeff Roberson 	uma_zfree(vmspace_zone, vm);
388df8bae1dSRodney W. Grimes }
389582ec34cSAlfred Perlstein 
390582ec34cSAlfred Perlstein void
391582ec34cSAlfred Perlstein vmspace_free(struct vmspace *vm)
392582ec34cSAlfred Perlstein {
393582ec34cSAlfred Perlstein 
394423521aaSRyan Stone 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
395164a37a5SJohn Baldwin 	    "vmspace_free() called");
396423521aaSRyan Stone 
397f7db0c95SMark Johnston 	if (refcount_release(&vm->vm_refcnt))
398582ec34cSAlfred Perlstein 		vmspace_dofree(vm);
399582ec34cSAlfred Perlstein }
400582ec34cSAlfred Perlstein 
401582ec34cSAlfred Perlstein void
402582ec34cSAlfred Perlstein vmspace_exitfree(struct proc *p)
403582ec34cSAlfred Perlstein {
404334f7061SPeter Wemm 	struct vmspace *vm;
405582ec34cSAlfred Perlstein 
40657051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
407334f7061SPeter Wemm 	vm = p->p_vmspace;
408334f7061SPeter Wemm 	p->p_vmspace = NULL;
40957051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
41057051fdcSTor Egge 	KASSERT(vm == &vmspace0, ("vmspace_exitfree: wrong vmspace"));
41157051fdcSTor Egge 	vmspace_free(vm);
41257051fdcSTor Egge }
41357051fdcSTor Egge 
41457051fdcSTor Egge void
41557051fdcSTor Egge vmspace_exit(struct thread *td)
41657051fdcSTor Egge {
41757051fdcSTor Egge 	struct vmspace *vm;
41857051fdcSTor Egge 	struct proc *p;
419f7db0c95SMark Johnston 	bool released;
42057051fdcSTor Egge 
42157051fdcSTor Egge 	p = td->td_proc;
42257051fdcSTor Egge 	vm = p->p_vmspace;
423f7db0c95SMark Johnston 
424f7db0c95SMark Johnston 	/*
425f7db0c95SMark Johnston 	 * Prepare to release the vmspace reference.  The thread that releases
426f7db0c95SMark Johnston 	 * the last reference is responsible for tearing down the vmspace.
427f7db0c95SMark Johnston 	 * However, threads not releasing the final reference must switch to the
428f7db0c95SMark Johnston 	 * kernel's vmspace0 before the decrement so that the subsequent pmap
429f7db0c95SMark Johnston 	 * deactivation does not modify a freed vmspace.
430f7db0c95SMark Johnston 	 */
431f7db0c95SMark Johnston 	refcount_acquire(&vmspace0.vm_refcnt);
432f7db0c95SMark Johnston 	if (!(released = refcount_release_if_last(&vm->vm_refcnt))) {
433f7db0c95SMark Johnston 		if (p->p_vmspace != &vmspace0) {
43457051fdcSTor Egge 			PROC_VMSPACE_LOCK(p);
43557051fdcSTor Egge 			p->p_vmspace = &vmspace0;
43657051fdcSTor Egge 			PROC_VMSPACE_UNLOCK(p);
43757051fdcSTor Egge 			pmap_activate(td);
43857051fdcSTor Egge 		}
439f7db0c95SMark Johnston 		released = refcount_release(&vm->vm_refcnt);
440f7db0c95SMark Johnston 	}
441f7db0c95SMark Johnston 	if (released) {
442f7db0c95SMark Johnston 		/*
443f7db0c95SMark Johnston 		 * pmap_remove_pages() expects the pmap to be active, so switch
444f7db0c95SMark Johnston 		 * back first if necessary.
445f7db0c95SMark Johnston 		 */
44657051fdcSTor Egge 		if (p->p_vmspace != vm) {
44757051fdcSTor Egge 			PROC_VMSPACE_LOCK(p);
44857051fdcSTor Egge 			p->p_vmspace = vm;
44957051fdcSTor Egge 			PROC_VMSPACE_UNLOCK(p);
45057051fdcSTor Egge 			pmap_activate(td);
45157051fdcSTor Egge 		}
45257051fdcSTor Egge 		pmap_remove_pages(vmspace_pmap(vm));
45357051fdcSTor Egge 		PROC_VMSPACE_LOCK(p);
45457051fdcSTor Egge 		p->p_vmspace = &vmspace0;
45557051fdcSTor Egge 		PROC_VMSPACE_UNLOCK(p);
45657051fdcSTor Egge 		pmap_activate(td);
457334f7061SPeter Wemm 		vmspace_dofree(vm);
458334f7061SPeter Wemm 	}
4594b5c9cf6SEdward Tomasz Napierala #ifdef RACCT
4604b5c9cf6SEdward Tomasz Napierala 	if (racct_enable)
4611ba5ad42SEdward Tomasz Napierala 		vmspace_container_reset(p);
4624b5c9cf6SEdward Tomasz Napierala #endif
46357051fdcSTor Egge }
46457051fdcSTor Egge 
46557051fdcSTor Egge /* Acquire reference to vmspace owned by another process. */
46657051fdcSTor Egge 
46757051fdcSTor Egge struct vmspace *
46857051fdcSTor Egge vmspace_acquire_ref(struct proc *p)
46957051fdcSTor Egge {
47057051fdcSTor Egge 	struct vmspace *vm;
47157051fdcSTor Egge 
47257051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
47357051fdcSTor Egge 	vm = p->p_vmspace;
474f7db0c95SMark Johnston 	if (vm == NULL || !refcount_acquire_if_not_zero(&vm->vm_refcnt)) {
47557051fdcSTor Egge 		PROC_VMSPACE_UNLOCK(p);
47657051fdcSTor Egge 		return (NULL);
47757051fdcSTor Egge 	}
47857051fdcSTor Egge 	if (vm != p->p_vmspace) {
47957051fdcSTor Egge 		PROC_VMSPACE_UNLOCK(p);
48057051fdcSTor Egge 		vmspace_free(vm);
48157051fdcSTor Egge 		return (NULL);
48257051fdcSTor Egge 	}
48357051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
48457051fdcSTor Egge 	return (vm);
48557051fdcSTor Egge }
486df8bae1dSRodney W. Grimes 
4878a4dc40fSJohn Baldwin /*
4888a4dc40fSJohn Baldwin  * Switch between vmspaces in an AIO kernel process.
4898a4dc40fSJohn Baldwin  *
4900b96ca33SJohn Baldwin  * The new vmspace is either the vmspace of a user process obtained
4910b96ca33SJohn Baldwin  * from an active AIO request or the initial vmspace of the AIO kernel
4920b96ca33SJohn Baldwin  * process (when it is idling).  Because user processes will block to
4930b96ca33SJohn Baldwin  * drain any active AIO requests before proceeding in exit() or
4940b96ca33SJohn Baldwin  * execve(), the reference count for vmspaces from AIO requests can
4950b96ca33SJohn Baldwin  * never be 0.  Similarly, AIO kernel processes hold an extra
4960b96ca33SJohn Baldwin  * reference on their initial vmspace for the life of the process.  As
4970b96ca33SJohn Baldwin  * a result, the 'newvm' vmspace always has a non-zero reference
4980b96ca33SJohn Baldwin  * count.  This permits an additional reference on 'newvm' to be
4990b96ca33SJohn Baldwin  * acquired via a simple atomic increment rather than the loop in
5000b96ca33SJohn Baldwin  * vmspace_acquire_ref() above.
5018a4dc40fSJohn Baldwin  */
5028a4dc40fSJohn Baldwin void
5038a4dc40fSJohn Baldwin vmspace_switch_aio(struct vmspace *newvm)
5048a4dc40fSJohn Baldwin {
5058a4dc40fSJohn Baldwin 	struct vmspace *oldvm;
5068a4dc40fSJohn Baldwin 
5078a4dc40fSJohn Baldwin 	/* XXX: Need some way to assert that this is an aio daemon. */
5088a4dc40fSJohn Baldwin 
509f7db0c95SMark Johnston 	KASSERT(refcount_load(&newvm->vm_refcnt) > 0,
5108a4dc40fSJohn Baldwin 	    ("vmspace_switch_aio: newvm unreferenced"));
5118a4dc40fSJohn Baldwin 
5128a4dc40fSJohn Baldwin 	oldvm = curproc->p_vmspace;
5138a4dc40fSJohn Baldwin 	if (oldvm == newvm)
5148a4dc40fSJohn Baldwin 		return;
5158a4dc40fSJohn Baldwin 
5168a4dc40fSJohn Baldwin 	/*
5178a4dc40fSJohn Baldwin 	 * Point to the new address space and refer to it.
5188a4dc40fSJohn Baldwin 	 */
5198a4dc40fSJohn Baldwin 	curproc->p_vmspace = newvm;
520f7db0c95SMark Johnston 	refcount_acquire(&newvm->vm_refcnt);
5218a4dc40fSJohn Baldwin 
5228a4dc40fSJohn Baldwin 	/* Activate the new mapping. */
5238a4dc40fSJohn Baldwin 	pmap_activate(curthread);
5248a4dc40fSJohn Baldwin 
5258a4dc40fSJohn Baldwin 	vmspace_free(oldvm);
5268a4dc40fSJohn Baldwin }
5278a4dc40fSJohn Baldwin 
5281b40f8c0SMatthew Dillon void
529780b1c09SAlan Cox _vm_map_lock(vm_map_t map, const char *file, int line)
5301b40f8c0SMatthew Dillon {
531bc91c510SAlan Cox 
53293bc4879SAlan Cox 	if (map->system_map)
533ccdf2333SAttilio Rao 		mtx_lock_flags_(&map->system_mtx, 0, file, line);
53412c64974SMaxime Henrion 	else
5359fde98bbSAttilio Rao 		sx_xlock_(&map->lock, file, line);
5361b40f8c0SMatthew Dillon 	map->timestamp++;
5371b40f8c0SMatthew Dillon }
5381b40f8c0SMatthew Dillon 
53978022527SKonstantin Belousov void
54078022527SKonstantin Belousov vm_map_entry_set_vnode_text(vm_map_entry_t entry, bool add)
54178022527SKonstantin Belousov {
54267388836SKonstantin Belousov 	vm_object_t object;
54378022527SKonstantin Belousov 	struct vnode *vp;
54467388836SKonstantin Belousov 	bool vp_held;
54578022527SKonstantin Belousov 
54678022527SKonstantin Belousov 	if ((entry->eflags & MAP_ENTRY_VN_EXEC) == 0)
54778022527SKonstantin Belousov 		return;
54878022527SKonstantin Belousov 	KASSERT((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0,
54978022527SKonstantin Belousov 	    ("Submap with execs"));
55078022527SKonstantin Belousov 	object = entry->object.vm_object;
55178022527SKonstantin Belousov 	KASSERT(object != NULL, ("No object for text, entry %p", entry));
55267388836SKonstantin Belousov 	if ((object->flags & OBJ_ANON) != 0)
55367388836SKonstantin Belousov 		object = object->handle;
55467388836SKonstantin Belousov 	else
55567388836SKonstantin Belousov 		KASSERT(object->backing_object == NULL,
55667388836SKonstantin Belousov 		    ("non-anon object %p shadows", object));
55767388836SKonstantin Belousov 	KASSERT(object != NULL, ("No content object for text, entry %p obj %p",
55867388836SKonstantin Belousov 	    entry, entry->object.vm_object));
55978022527SKonstantin Belousov 
56067388836SKonstantin Belousov 	/*
56167388836SKonstantin Belousov 	 * Mostly, we do not lock the backing object.  It is
56267388836SKonstantin Belousov 	 * referenced by the entry we are processing, so it cannot go
56367388836SKonstantin Belousov 	 * away.
56467388836SKonstantin Belousov 	 */
565192112b7SKonstantin Belousov 	vm_pager_getvp(object, &vp, &vp_held);
56632d2014dSKonstantin Belousov 	if (vp != NULL) {
567bb9e2184SKonstantin Belousov 		if (add) {
56878022527SKonstantin Belousov 			VOP_SET_TEXT_CHECKED(vp);
569bb9e2184SKonstantin Belousov 		} else {
570bb9e2184SKonstantin Belousov 			vn_lock(vp, LK_SHARED | LK_RETRY);
571bb9e2184SKonstantin Belousov 			VOP_UNSET_TEXT_CHECKED(vp);
572b249ce48SMateusz Guzik 			VOP_UNLOCK(vp);
573bb9e2184SKonstantin Belousov 		}
57467388836SKonstantin Belousov 		if (vp_held)
57567388836SKonstantin Belousov 			vdrop(vp);
576bb9e2184SKonstantin Belousov 	}
57778022527SKonstantin Belousov }
57878022527SKonstantin Belousov 
5797cdcf863SDoug Moore /*
5807cdcf863SDoug Moore  * Use a different name for this vm_map_entry field when it's use
5817cdcf863SDoug Moore  * is not consistent with its use as part of an ordered search tree.
5827cdcf863SDoug Moore  */
5837cdcf863SDoug Moore #define defer_next right
5847cdcf863SDoug Moore 
5850b367bd8SKonstantin Belousov static void
5860b367bd8SKonstantin Belousov vm_map_process_deferred(void)
5870e0af8ecSBrian Feldman {
5880b367bd8SKonstantin Belousov 	struct thread *td;
5896fbe60faSJohn Baldwin 	vm_map_entry_t entry, next;
59084110e7eSKonstantin Belousov 	vm_object_t object;
591655c3490SKonstantin Belousov 
5920b367bd8SKonstantin Belousov 	td = curthread;
5936fbe60faSJohn Baldwin 	entry = td->td_map_def_user;
5946fbe60faSJohn Baldwin 	td->td_map_def_user = NULL;
5956fbe60faSJohn Baldwin 	while (entry != NULL) {
5967cdcf863SDoug Moore 		next = entry->defer_next;
597fe7bcbafSKyle Evans 		MPASS((entry->eflags & (MAP_ENTRY_WRITECNT |
598fe7bcbafSKyle Evans 		    MAP_ENTRY_VN_EXEC)) != (MAP_ENTRY_WRITECNT |
59978022527SKonstantin Belousov 		    MAP_ENTRY_VN_EXEC));
600fe7bcbafSKyle Evans 		if ((entry->eflags & MAP_ENTRY_WRITECNT) != 0) {
60184110e7eSKonstantin Belousov 			/*
60284110e7eSKonstantin Belousov 			 * Decrement the object's writemappings and
60384110e7eSKonstantin Belousov 			 * possibly the vnode's v_writecount.
60484110e7eSKonstantin Belousov 			 */
60584110e7eSKonstantin Belousov 			KASSERT((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0,
60684110e7eSKonstantin Belousov 			    ("Submap with writecount"));
60784110e7eSKonstantin Belousov 			object = entry->object.vm_object;
60884110e7eSKonstantin Belousov 			KASSERT(object != NULL, ("No object for writecount"));
609fe7bcbafSKyle Evans 			vm_pager_release_writecount(object, entry->start,
61084110e7eSKonstantin Belousov 			    entry->end);
61184110e7eSKonstantin Belousov 		}
61278022527SKonstantin Belousov 		vm_map_entry_set_vnode_text(entry, false);
6130b367bd8SKonstantin Belousov 		vm_map_entry_deallocate(entry, FALSE);
6146fbe60faSJohn Baldwin 		entry = next;
6150b367bd8SKonstantin Belousov 	}
6160b367bd8SKonstantin Belousov }
6170b367bd8SKonstantin Belousov 
618461587dcSDoug Moore #ifdef INVARIANTS
619461587dcSDoug Moore static void
620461587dcSDoug Moore _vm_map_assert_locked(vm_map_t map, const char *file, int line)
621461587dcSDoug Moore {
622461587dcSDoug Moore 
623461587dcSDoug Moore 	if (map->system_map)
624461587dcSDoug Moore 		mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
625461587dcSDoug Moore 	else
626461587dcSDoug Moore 		sx_assert_(&map->lock, SA_XLOCKED, file, line);
627461587dcSDoug Moore }
628461587dcSDoug Moore 
629461587dcSDoug Moore #define	VM_MAP_ASSERT_LOCKED(map) \
630461587dcSDoug Moore     _vm_map_assert_locked(map, LOCK_FILE, LOCK_LINE)
631461587dcSDoug Moore 
632461587dcSDoug Moore enum { VMMAP_CHECK_NONE, VMMAP_CHECK_UNLOCK, VMMAP_CHECK_ALL };
633461587dcSDoug Moore #ifdef DIAGNOSTIC
634461587dcSDoug Moore static int enable_vmmap_check = VMMAP_CHECK_UNLOCK;
635461587dcSDoug Moore #else
636461587dcSDoug Moore static int enable_vmmap_check = VMMAP_CHECK_NONE;
637461587dcSDoug Moore #endif
638461587dcSDoug Moore SYSCTL_INT(_debug, OID_AUTO, vmmap_check, CTLFLAG_RWTUN,
639461587dcSDoug Moore     &enable_vmmap_check, 0, "Enable vm map consistency checking");
640461587dcSDoug Moore 
641461587dcSDoug Moore static void _vm_map_assert_consistent(vm_map_t map, int check);
642461587dcSDoug Moore 
643461587dcSDoug Moore #define VM_MAP_ASSERT_CONSISTENT(map) \
644461587dcSDoug Moore     _vm_map_assert_consistent(map, VMMAP_CHECK_ALL)
645461587dcSDoug Moore #ifdef DIAGNOSTIC
646461587dcSDoug Moore #define VM_MAP_UNLOCK_CONSISTENT(map) do {				\
647461587dcSDoug Moore 	if (map->nupdates > map->nentries) {				\
648461587dcSDoug Moore 		_vm_map_assert_consistent(map, VMMAP_CHECK_UNLOCK);	\
649461587dcSDoug Moore 		map->nupdates = 0;					\
650461587dcSDoug Moore 	}								\
651461587dcSDoug Moore } while (0)
652461587dcSDoug Moore #else
653461587dcSDoug Moore #define VM_MAP_UNLOCK_CONSISTENT(map)
654461587dcSDoug Moore #endif
655461587dcSDoug Moore #else
656461587dcSDoug Moore #define	VM_MAP_ASSERT_LOCKED(map)
657461587dcSDoug Moore #define VM_MAP_ASSERT_CONSISTENT(map)
658461587dcSDoug Moore #define VM_MAP_UNLOCK_CONSISTENT(map)
659461587dcSDoug Moore #endif /* INVARIANTS */
660461587dcSDoug Moore 
6610b367bd8SKonstantin Belousov void
6620b367bd8SKonstantin Belousov _vm_map_unlock(vm_map_t map, const char *file, int line)
6630b367bd8SKonstantin Belousov {
6640b367bd8SKonstantin Belousov 
665461587dcSDoug Moore 	VM_MAP_UNLOCK_CONSISTENT(map);
66620f02659SMark Johnston 	if (map->system_map) {
66720f02659SMark Johnston #ifndef UMA_MD_SMALL_ALLOC
66820f02659SMark Johnston 		if (map == kernel_map && (map->flags & MAP_REPLENISH) != 0) {
66920f02659SMark Johnston 			uma_prealloc(kmapentzone, 1);
67020f02659SMark Johnston 			map->flags &= ~MAP_REPLENISH;
67120f02659SMark Johnston 		}
67220f02659SMark Johnston #endif
673ccdf2333SAttilio Rao 		mtx_unlock_flags_(&map->system_mtx, 0, file, line);
67420f02659SMark Johnston 	} else {
6759fde98bbSAttilio Rao 		sx_xunlock_(&map->lock, file, line);
6760b367bd8SKonstantin Belousov 		vm_map_process_deferred();
677655c3490SKonstantin Belousov 	}
6780e0af8ecSBrian Feldman }
6790e0af8ecSBrian Feldman 
6800e0af8ecSBrian Feldman void
681780b1c09SAlan Cox _vm_map_lock_read(vm_map_t map, const char *file, int line)
6820e0af8ecSBrian Feldman {
683bc91c510SAlan Cox 
68493bc4879SAlan Cox 	if (map->system_map)
685ccdf2333SAttilio Rao 		mtx_lock_flags_(&map->system_mtx, 0, file, line);
68612c64974SMaxime Henrion 	else
6879fde98bbSAttilio Rao 		sx_slock_(&map->lock, file, line);
68836daaecdSAlan Cox }
6890e0af8ecSBrian Feldman 
6900e0af8ecSBrian Feldman void
691780b1c09SAlan Cox _vm_map_unlock_read(vm_map_t map, const char *file, int line)
6920e0af8ecSBrian Feldman {
693bc91c510SAlan Cox 
69420f02659SMark Johnston 	if (map->system_map) {
69520f02659SMark Johnston 		KASSERT((map->flags & MAP_REPLENISH) == 0,
69620f02659SMark Johnston 		    ("%s: MAP_REPLENISH leaked", __func__));
697ccdf2333SAttilio Rao 		mtx_unlock_flags_(&map->system_mtx, 0, file, line);
69820f02659SMark Johnston 	} else {
6999fde98bbSAttilio Rao 		sx_sunlock_(&map->lock, file, line);
7000b367bd8SKonstantin Belousov 		vm_map_process_deferred();
7010b367bd8SKonstantin Belousov 	}
70225adb370SBrian Feldman }
70325adb370SBrian Feldman 
704d974f03cSAlan Cox int
705780b1c09SAlan Cox _vm_map_trylock(vm_map_t map, const char *file, int line)
706d974f03cSAlan Cox {
70725adb370SBrian Feldman 	int error;
70825adb370SBrian Feldman 
70936daaecdSAlan Cox 	error = map->system_map ?
710ccdf2333SAttilio Rao 	    !mtx_trylock_flags_(&map->system_mtx, 0, file, line) :
7119fde98bbSAttilio Rao 	    !sx_try_xlock_(&map->lock, file, line);
7123a92e5d5SAlan Cox 	if (error == 0)
7133a92e5d5SAlan Cox 		map->timestamp++;
714bc91c510SAlan Cox 	return (error == 0);
7150e0af8ecSBrian Feldman }
7160e0af8ecSBrian Feldman 
7170e0af8ecSBrian Feldman int
71872d97679SDavid Schultz _vm_map_trylock_read(vm_map_t map, const char *file, int line)
71972d97679SDavid Schultz {
72072d97679SDavid Schultz 	int error;
72172d97679SDavid Schultz 
72272d97679SDavid Schultz 	error = map->system_map ?
723ccdf2333SAttilio Rao 	    !mtx_trylock_flags_(&map->system_mtx, 0, file, line) :
7249fde98bbSAttilio Rao 	    !sx_try_slock_(&map->lock, file, line);
72572d97679SDavid Schultz 	return (error == 0);
72672d97679SDavid Schultz }
72772d97679SDavid Schultz 
72805a8c414SAlan Cox /*
72905a8c414SAlan Cox  *	_vm_map_lock_upgrade:	[ internal use only ]
73005a8c414SAlan Cox  *
73105a8c414SAlan Cox  *	Tries to upgrade a read (shared) lock on the specified map to a write
73205a8c414SAlan Cox  *	(exclusive) lock.  Returns the value "0" if the upgrade succeeds and a
73305a8c414SAlan Cox  *	non-zero value if the upgrade fails.  If the upgrade fails, the map is
73405a8c414SAlan Cox  *	returned without a read or write lock held.
73505a8c414SAlan Cox  *
73605a8c414SAlan Cox  *	Requires that the map be read locked.
73705a8c414SAlan Cox  */
73872d97679SDavid Schultz int
739780b1c09SAlan Cox _vm_map_lock_upgrade(vm_map_t map, const char *file, int line)
7400e0af8ecSBrian Feldman {
74105a8c414SAlan Cox 	unsigned int last_timestamp;
742bc91c510SAlan Cox 
74312c64974SMaxime Henrion 	if (map->system_map) {
744ccdf2333SAttilio Rao 		mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
74505a8c414SAlan Cox 	} else {
7469fde98bbSAttilio Rao 		if (!sx_try_upgrade_(&map->lock, file, line)) {
74705a8c414SAlan Cox 			last_timestamp = map->timestamp;
7489fde98bbSAttilio Rao 			sx_sunlock_(&map->lock, file, line);
7490b367bd8SKonstantin Belousov 			vm_map_process_deferred();
75005a8c414SAlan Cox 			/*
75105a8c414SAlan Cox 			 * If the map's timestamp does not change while the
75205a8c414SAlan Cox 			 * map is unlocked, then the upgrade succeeds.
75305a8c414SAlan Cox 			 */
7549fde98bbSAttilio Rao 			sx_xlock_(&map->lock, file, line);
75505a8c414SAlan Cox 			if (last_timestamp != map->timestamp) {
7569fde98bbSAttilio Rao 				sx_xunlock_(&map->lock, file, line);
75705a8c414SAlan Cox 				return (1);
75805a8c414SAlan Cox 			}
75905a8c414SAlan Cox 		}
76005a8c414SAlan Cox 	}
761bc91c510SAlan Cox 	map->timestamp++;
762bc91c510SAlan Cox 	return (0);
7630e0af8ecSBrian Feldman }
7640e0af8ecSBrian Feldman 
7650e0af8ecSBrian Feldman void
766780b1c09SAlan Cox _vm_map_lock_downgrade(vm_map_t map, const char *file, int line)
7671b40f8c0SMatthew Dillon {
768bc91c510SAlan Cox 
76912c64974SMaxime Henrion 	if (map->system_map) {
77020f02659SMark Johnston 		KASSERT((map->flags & MAP_REPLENISH) == 0,
77120f02659SMark Johnston 		    ("%s: MAP_REPLENISH leaked", __func__));
772ccdf2333SAttilio Rao 		mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
773461587dcSDoug Moore 	} else {
774461587dcSDoug Moore 		VM_MAP_UNLOCK_CONSISTENT(map);
7759fde98bbSAttilio Rao 		sx_downgrade_(&map->lock, file, line);
77605a8c414SAlan Cox 	}
777461587dcSDoug Moore }
77805a8c414SAlan Cox 
77905a8c414SAlan Cox /*
78005a8c414SAlan Cox  *	vm_map_locked:
78105a8c414SAlan Cox  *
78205a8c414SAlan Cox  *	Returns a non-zero value if the caller holds a write (exclusive) lock
78305a8c414SAlan Cox  *	on the specified map and the value "0" otherwise.
78405a8c414SAlan Cox  */
78505a8c414SAlan Cox int
78605a8c414SAlan Cox vm_map_locked(vm_map_t map)
78705a8c414SAlan Cox {
78805a8c414SAlan Cox 
78905a8c414SAlan Cox 	if (map->system_map)
79005a8c414SAlan Cox 		return (mtx_owned(&map->system_mtx));
79105a8c414SAlan Cox 	else
79205a8c414SAlan Cox 		return (sx_xlocked(&map->lock));
79325adb370SBrian Feldman }
79425adb370SBrian Feldman 
795acd9a301SAlan Cox /*
7968304adaaSAlan Cox  *	_vm_map_unlock_and_wait:
7978304adaaSAlan Cox  *
7988304adaaSAlan Cox  *	Atomically releases the lock on the specified map and puts the calling
7998304adaaSAlan Cox  *	thread to sleep.  The calling thread will remain asleep until either
8008304adaaSAlan Cox  *	vm_map_wakeup() is performed on the map or the specified timeout is
8018304adaaSAlan Cox  *	exceeded.
8028304adaaSAlan Cox  *
8038304adaaSAlan Cox  *	WARNING!  This function does not perform deferred deallocations of
8048304adaaSAlan Cox  *	objects and map	entries.  Therefore, the calling thread is expected to
8058304adaaSAlan Cox  *	reacquire the map lock after reawakening and later perform an ordinary
8068304adaaSAlan Cox  *	unlock operation, such as vm_map_unlock(), before completing its
8078304adaaSAlan Cox  *	operation on the map.
808acd9a301SAlan Cox  */
8099688f931SAlan Cox int
8108304adaaSAlan Cox _vm_map_unlock_and_wait(vm_map_t map, int timo, const char *file, int line)
811acd9a301SAlan Cox {
812acd9a301SAlan Cox 
813461587dcSDoug Moore 	VM_MAP_UNLOCK_CONSISTENT(map);
8143a92e5d5SAlan Cox 	mtx_lock(&map_sleep_mtx);
81520f02659SMark Johnston 	if (map->system_map) {
81620f02659SMark Johnston 		KASSERT((map->flags & MAP_REPLENISH) == 0,
81720f02659SMark Johnston 		    ("%s: MAP_REPLENISH leaked", __func__));
818ccdf2333SAttilio Rao 		mtx_unlock_flags_(&map->system_mtx, 0, file, line);
81920f02659SMark Johnston 	} else {
8209fde98bbSAttilio Rao 		sx_xunlock_(&map->lock, file, line);
82120f02659SMark Johnston 	}
8228304adaaSAlan Cox 	return (msleep(&map->root, &map_sleep_mtx, PDROP | PVM, "vmmaps",
8238304adaaSAlan Cox 	    timo));
824acd9a301SAlan Cox }
825acd9a301SAlan Cox 
826acd9a301SAlan Cox /*
827acd9a301SAlan Cox  *	vm_map_wakeup:
8288304adaaSAlan Cox  *
8298304adaaSAlan Cox  *	Awaken any threads that have slept on the map using
8308304adaaSAlan Cox  *	vm_map_unlock_and_wait().
831acd9a301SAlan Cox  */
8329688f931SAlan Cox void
833acd9a301SAlan Cox vm_map_wakeup(vm_map_t map)
834acd9a301SAlan Cox {
835acd9a301SAlan Cox 
836b49ecb86SAlan Cox 	/*
8373a92e5d5SAlan Cox 	 * Acquire and release map_sleep_mtx to prevent a wakeup()
8388304adaaSAlan Cox 	 * from being performed (and lost) between the map unlock
8398304adaaSAlan Cox 	 * and the msleep() in _vm_map_unlock_and_wait().
840b49ecb86SAlan Cox 	 */
8413a92e5d5SAlan Cox 	mtx_lock(&map_sleep_mtx);
8423a92e5d5SAlan Cox 	mtx_unlock(&map_sleep_mtx);
843acd9a301SAlan Cox 	wakeup(&map->root);
844acd9a301SAlan Cox }
845acd9a301SAlan Cox 
846a5db445dSMax Laier void
847a5db445dSMax Laier vm_map_busy(vm_map_t map)
848a5db445dSMax Laier {
849a5db445dSMax Laier 
850a5db445dSMax Laier 	VM_MAP_ASSERT_LOCKED(map);
851a5db445dSMax Laier 	map->busy++;
852a5db445dSMax Laier }
853a5db445dSMax Laier 
854a5db445dSMax Laier void
855a5db445dSMax Laier vm_map_unbusy(vm_map_t map)
856a5db445dSMax Laier {
857a5db445dSMax Laier 
858a5db445dSMax Laier 	VM_MAP_ASSERT_LOCKED(map);
859a5db445dSMax Laier 	KASSERT(map->busy, ("vm_map_unbusy: not busy"));
860a5db445dSMax Laier 	if (--map->busy == 0 && (map->flags & MAP_BUSY_WAKEUP)) {
861a5db445dSMax Laier 		vm_map_modflags(map, 0, MAP_BUSY_WAKEUP);
862a5db445dSMax Laier 		wakeup(&map->busy);
863a5db445dSMax Laier 	}
864a5db445dSMax Laier }
865a5db445dSMax Laier 
866a5db445dSMax Laier void
867a5db445dSMax Laier vm_map_wait_busy(vm_map_t map)
868a5db445dSMax Laier {
869a5db445dSMax Laier 
870a5db445dSMax Laier 	VM_MAP_ASSERT_LOCKED(map);
871a5db445dSMax Laier 	while (map->busy) {
872a5db445dSMax Laier 		vm_map_modflags(map, MAP_BUSY_WAKEUP, 0);
873a5db445dSMax Laier 		if (map->system_map)
874a5db445dSMax Laier 			msleep(&map->busy, &map->system_mtx, 0, "mbusy", 0);
875a5db445dSMax Laier 		else
876a5db445dSMax Laier 			sx_sleep(&map->busy, &map->lock, 0, "mbusy", 0);
877a5db445dSMax Laier 	}
878a5db445dSMax Laier 	map->timestamp++;
879a5db445dSMax Laier }
880a5db445dSMax Laier 
8811b40f8c0SMatthew Dillon long
8821b40f8c0SMatthew Dillon vmspace_resident_count(struct vmspace *vmspace)
8831b40f8c0SMatthew Dillon {
8841b40f8c0SMatthew Dillon 	return pmap_resident_count(vmspace_pmap(vmspace));
8851b40f8c0SMatthew Dillon }
8861b40f8c0SMatthew Dillon 
887ff2b5645SMatthew Dillon /*
888df8bae1dSRodney W. Grimes  * Initialize an existing vm_map structure
889df8bae1dSRodney W. Grimes  * such as that in the vmspace structure.
890df8bae1dSRodney W. Grimes  */
8918355f576SJeff Roberson static void
89292351f16SAlan Cox _vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min, vm_offset_t max)
893df8bae1dSRodney W. Grimes {
89421c641b2SJohn Baldwin 
8952203c46dSMark Johnston 	map->header.eflags = MAP_ENTRY_HEADER;
8969688f931SAlan Cox 	map->needs_wakeup = FALSE;
8973075778bSJohn Dyson 	map->system_map = 0;
89892351f16SAlan Cox 	map->pmap = pmap;
899f0165b1cSKonstantin Belousov 	map->header.end = min;
900f0165b1cSKonstantin Belousov 	map->header.start = max;
901af7cd0c5SBrian Feldman 	map->flags = 0;
902c1ad5342SDoug Moore 	map->header.left = map->header.right = &map->header;
9034e94f402SAlan Cox 	map->root = NULL;
904df8bae1dSRodney W. Grimes 	map->timestamp = 0;
905a5db445dSMax Laier 	map->busy = 0;
906fa50a355SKonstantin Belousov 	map->anon_loc = 0;
907461587dcSDoug Moore #ifdef DIAGNOSTIC
908461587dcSDoug Moore 	map->nupdates = 0;
909461587dcSDoug Moore #endif
910df8bae1dSRodney W. Grimes }
911df8bae1dSRodney W. Grimes 
912a18b1f1dSJason Evans void
91392351f16SAlan Cox vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min, vm_offset_t max)
914a18b1f1dSJason Evans {
91592351f16SAlan Cox 
91692351f16SAlan Cox 	_vm_map_init(map, pmap, min, max);
9177dd979dfSMark Johnston 	mtx_init(&map->system_mtx, "vm map (system)", NULL,
9187dd979dfSMark Johnston 	    MTX_DEF | MTX_DUPOK);
9197dd979dfSMark Johnston 	sx_init(&map->lock, "vm map (user)");
920a18b1f1dSJason Evans }
921a18b1f1dSJason Evans 
922df8bae1dSRodney W. Grimes /*
923b18bfc3dSJohn Dyson  *	vm_map_entry_dispose:	[ internal use only ]
924b18bfc3dSJohn Dyson  *
925b18bfc3dSJohn Dyson  *	Inverse of vm_map_entry_create.
926b18bfc3dSJohn Dyson  */
92762487bb4SJohn Dyson static void
9281b40f8c0SMatthew Dillon vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry)
929b18bfc3dSJohn Dyson {
9302b4a2c27SAlan Cox 	uma_zfree(map->system_map ? kmapentzone : mapentzone, entry);
931b18bfc3dSJohn Dyson }
932b18bfc3dSJohn Dyson 
933b18bfc3dSJohn Dyson /*
934df8bae1dSRodney W. Grimes  *	vm_map_entry_create:	[ internal use only ]
935df8bae1dSRodney W. Grimes  *
936df8bae1dSRodney W. Grimes  *	Allocates a VM map entry for insertion.
937b28cb1caSAlfred Perlstein  *	No entry fields are filled in.
938df8bae1dSRodney W. Grimes  */
939f708ef1bSPoul-Henning Kamp static vm_map_entry_t
9401b40f8c0SMatthew Dillon vm_map_entry_create(vm_map_t map)
941df8bae1dSRodney W. Grimes {
9421f6889a1SMatthew Dillon 	vm_map_entry_t new_entry;
9431f6889a1SMatthew Dillon 
94420f02659SMark Johnston #ifndef UMA_MD_SMALL_ALLOC
94520f02659SMark Johnston 	if (map == kernel_map) {
94620f02659SMark Johnston 		VM_MAP_ASSERT_LOCKED(map);
94720f02659SMark Johnston 
94820f02659SMark Johnston 		/*
94920f02659SMark Johnston 		 * A new slab of kernel map entries cannot be allocated at this
95020f02659SMark Johnston 		 * point because the kernel map has not yet been updated to
95120f02659SMark Johnston 		 * reflect the caller's request.  Therefore, we allocate a new
95220f02659SMark Johnston 		 * map entry, dipping into the reserve if necessary, and set a
95320f02659SMark Johnston 		 * flag indicating that the reserve must be replenished before
95420f02659SMark Johnston 		 * the map is unlocked.
95520f02659SMark Johnston 		 */
95620f02659SMark Johnston 		new_entry = uma_zalloc(kmapentzone, M_NOWAIT | M_NOVM);
95720f02659SMark Johnston 		if (new_entry == NULL) {
95820f02659SMark Johnston 			new_entry = uma_zalloc(kmapentzone,
95920f02659SMark Johnston 			    M_NOWAIT | M_NOVM | M_USE_RESERVE);
96020f02659SMark Johnston 			kernel_map->flags |= MAP_REPLENISH;
96120f02659SMark Johnston 		}
96220f02659SMark Johnston 	} else
96320f02659SMark Johnston #endif
96420f02659SMark Johnston 	if (map->system_map) {
9652b4a2c27SAlan Cox 		new_entry = uma_zalloc(kmapentzone, M_NOWAIT);
96620f02659SMark Johnston 	} else {
967a163d034SWarner Losh 		new_entry = uma_zalloc(mapentzone, M_WAITOK);
96820f02659SMark Johnston 	}
96920f02659SMark Johnston 	KASSERT(new_entry != NULL,
97020f02659SMark Johnston 	    ("vm_map_entry_create: kernel resources exhausted"));
9711f6889a1SMatthew Dillon 	return (new_entry);
972df8bae1dSRodney W. Grimes }
973df8bae1dSRodney W. Grimes 
974df8bae1dSRodney W. Grimes /*
975794316a8SAlan Cox  *	vm_map_entry_set_behavior:
976794316a8SAlan Cox  *
977794316a8SAlan Cox  *	Set the expected access behavior, either normal, random, or
978794316a8SAlan Cox  *	sequential.
979794316a8SAlan Cox  */
98062a59e8fSWarner Losh static inline void
981794316a8SAlan Cox vm_map_entry_set_behavior(vm_map_entry_t entry, u_char behavior)
982794316a8SAlan Cox {
983794316a8SAlan Cox 	entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) |
984794316a8SAlan Cox 	    (behavior & MAP_ENTRY_BEHAV_MASK);
985794316a8SAlan Cox }
986794316a8SAlan Cox 
987794316a8SAlan Cox /*
9885a0879daSDoug Moore  *	vm_map_entry_max_free_{left,right}:
9890164e057SAlan Cox  *
9905a0879daSDoug Moore  *	Compute the size of the largest free gap between two entries,
9915a0879daSDoug Moore  *	one the root of a tree and the other the ancestor of that root
9925a0879daSDoug Moore  *	that is the least or greatest ancestor found on the search path.
9930164e057SAlan Cox  */
9945a0879daSDoug Moore static inline vm_size_t
9955a0879daSDoug Moore vm_map_entry_max_free_left(vm_map_entry_t root, vm_map_entry_t left_ancestor)
9960164e057SAlan Cox {
9970164e057SAlan Cox 
998c1ad5342SDoug Moore 	return (root->left != left_ancestor ?
9995a0879daSDoug Moore 	    root->left->max_free : root->start - left_ancestor->end);
10005a0879daSDoug Moore }
10015a0879daSDoug Moore 
10025a0879daSDoug Moore static inline vm_size_t
10035a0879daSDoug Moore vm_map_entry_max_free_right(vm_map_entry_t root, vm_map_entry_t right_ancestor)
10045a0879daSDoug Moore {
10055a0879daSDoug Moore 
1006c1ad5342SDoug Moore 	return (root->right != right_ancestor ?
10075a0879daSDoug Moore 	    root->right->max_free : right_ancestor->start - root->end);
10080164e057SAlan Cox }
10090164e057SAlan Cox 
101083704cc2SDoug Moore /*
101183704cc2SDoug Moore  *	vm_map_entry_{pred,succ}:
101283704cc2SDoug Moore  *
101383704cc2SDoug Moore  *	Find the {predecessor, successor} of the entry by taking one step
101483704cc2SDoug Moore  *	in the appropriate direction and backtracking as much as necessary.
1015c1ad5342SDoug Moore  *	vm_map_entry_succ is defined in vm_map.h.
101683704cc2SDoug Moore  */
101783704cc2SDoug Moore static inline vm_map_entry_t
101883704cc2SDoug Moore vm_map_entry_pred(vm_map_entry_t entry)
101983704cc2SDoug Moore {
1020c1ad5342SDoug Moore 	vm_map_entry_t prior;
102183704cc2SDoug Moore 
1022c1ad5342SDoug Moore 	prior = entry->left;
1023c1ad5342SDoug Moore 	if (prior->right->start < entry->start) {
1024c1ad5342SDoug Moore 		do
1025c1ad5342SDoug Moore 			prior = prior->right;
1026c1ad5342SDoug Moore 		while (prior->right != entry);
102783704cc2SDoug Moore 	}
1028c1ad5342SDoug Moore 	return (prior);
1029c1ad5342SDoug Moore }
103083704cc2SDoug Moore 
103185b7bedbSDoug Moore static inline vm_size_t
103285b7bedbSDoug Moore vm_size_max(vm_size_t a, vm_size_t b)
103385b7bedbSDoug Moore {
103485b7bedbSDoug Moore 
103585b7bedbSDoug Moore 	return (a > b ? a : b);
103685b7bedbSDoug Moore }
103785b7bedbSDoug Moore 
1038c1ad5342SDoug Moore #define SPLAY_LEFT_STEP(root, y, llist, rlist, test) do {		\
1039c1ad5342SDoug Moore 	vm_map_entry_t z;						\
10405a0879daSDoug Moore 	vm_size_t max_free;						\
10415a0879daSDoug Moore 									\
10425a0879daSDoug Moore 	/*								\
10435a0879daSDoug Moore 	 * Infer root->right->max_free == root->max_free when		\
10445a0879daSDoug Moore 	 * y->max_free < root->max_free || root->max_free == 0.		\
10455a0879daSDoug Moore 	 * Otherwise, look right to find it.				\
10465a0879daSDoug Moore 	 */								\
10479f701172SKonstantin Belousov 	y = root->left;							\
10485a0879daSDoug Moore 	max_free = root->max_free;					\
1049668a8aa8SDoug Moore 	KASSERT(max_free == vm_size_max(				\
1050668a8aa8SDoug Moore 	    vm_map_entry_max_free_left(root, llist),			\
1051668a8aa8SDoug Moore 	    vm_map_entry_max_free_right(root, rlist)),			\
10525a0879daSDoug Moore 	    ("%s: max_free invariant fails", __func__));		\
1053668a8aa8SDoug Moore 	if (max_free - 1 < vm_map_entry_max_free_left(root, llist))	\
10545a0879daSDoug Moore 		max_free = vm_map_entry_max_free_right(root, rlist);	\
1055c1ad5342SDoug Moore 	if (y != llist && (test)) {					\
10569f701172SKonstantin Belousov 		/* Rotate right and make y root. */			\
1057c1ad5342SDoug Moore 		z = y->right;						\
1058c1ad5342SDoug Moore 		if (z != root) {					\
1059c1ad5342SDoug Moore 			root->left = z;					\
10609f701172SKonstantin Belousov 			y->right = root;				\
10615a0879daSDoug Moore 			if (max_free < y->max_free)			\
106285b7bedbSDoug Moore 			    root->max_free = max_free =			\
1063c1ad5342SDoug Moore 			    vm_size_max(max_free, z->max_free);		\
1064c1ad5342SDoug Moore 		} else if (max_free < y->max_free)			\
1065c1ad5342SDoug Moore 			root->max_free = max_free =			\
1066c1ad5342SDoug Moore 			    vm_size_max(max_free, root->start - y->end);\
10679f701172SKonstantin Belousov 		root = y;						\
10689f701172SKonstantin Belousov 		y = root->left;						\
10699f701172SKonstantin Belousov 	}								\
10705a0879daSDoug Moore 	/* Copy right->max_free.  Put root on rlist. */			\
10715a0879daSDoug Moore 	root->max_free = max_free;					\
10725a0879daSDoug Moore 	KASSERT(max_free == vm_map_entry_max_free_right(root, rlist),	\
10735a0879daSDoug Moore 	    ("%s: max_free not copied from right", __func__));		\
10749f701172SKonstantin Belousov 	root->left = rlist;						\
10759f701172SKonstantin Belousov 	rlist = root;							\
1076c1ad5342SDoug Moore 	root = y != llist ? y : NULL;					\
10779f701172SKonstantin Belousov } while (0)
10789f701172SKonstantin Belousov 
1079c1ad5342SDoug Moore #define SPLAY_RIGHT_STEP(root, y, llist, rlist, test) do {		\
1080c1ad5342SDoug Moore 	vm_map_entry_t z;						\
10815a0879daSDoug Moore 	vm_size_t max_free;						\
10825a0879daSDoug Moore 									\
10835a0879daSDoug Moore 	/*								\
10845a0879daSDoug Moore 	 * Infer root->left->max_free == root->max_free when		\
10855a0879daSDoug Moore 	 * y->max_free < root->max_free || root->max_free == 0.		\
10865a0879daSDoug Moore 	 * Otherwise, look left to find it.				\
10875a0879daSDoug Moore 	 */								\
10889f701172SKonstantin Belousov 	y = root->right;						\
10895a0879daSDoug Moore 	max_free = root->max_free;					\
1090668a8aa8SDoug Moore 	KASSERT(max_free == vm_size_max(				\
1091668a8aa8SDoug Moore 	    vm_map_entry_max_free_left(root, llist),			\
1092668a8aa8SDoug Moore 	    vm_map_entry_max_free_right(root, rlist)),			\
10935a0879daSDoug Moore 	    ("%s: max_free invariant fails", __func__));		\
1094668a8aa8SDoug Moore 	if (max_free - 1 < vm_map_entry_max_free_right(root, rlist))	\
10955a0879daSDoug Moore 		max_free = vm_map_entry_max_free_left(root, llist);	\
1096c1ad5342SDoug Moore 	if (y != rlist && (test)) {					\
10979f701172SKonstantin Belousov 		/* Rotate left and make y root. */			\
1098c1ad5342SDoug Moore 		z = y->left;						\
1099c1ad5342SDoug Moore 		if (z != root) {					\
1100c1ad5342SDoug Moore 			root->right = z;				\
11019f701172SKonstantin Belousov 			y->left = root;					\
11025a0879daSDoug Moore 			if (max_free < y->max_free)			\
110385b7bedbSDoug Moore 			    root->max_free = max_free =			\
1104c1ad5342SDoug Moore 			    vm_size_max(max_free, z->max_free);		\
1105c1ad5342SDoug Moore 		} else if (max_free < y->max_free)			\
1106c1ad5342SDoug Moore 			root->max_free = max_free =			\
1107c1ad5342SDoug Moore 			    vm_size_max(max_free, y->start - root->end);\
11089f701172SKonstantin Belousov 		root = y;						\
11099f701172SKonstantin Belousov 		y = root->right;					\
11109f701172SKonstantin Belousov 	}								\
11115a0879daSDoug Moore 	/* Copy left->max_free.  Put root on llist. */			\
11125a0879daSDoug Moore 	root->max_free = max_free;					\
11135a0879daSDoug Moore 	KASSERT(max_free == vm_map_entry_max_free_left(root, llist),	\
11145a0879daSDoug Moore 	    ("%s: max_free not copied from left", __func__));		\
11159f701172SKonstantin Belousov 	root->right = llist;						\
11169f701172SKonstantin Belousov 	llist = root;							\
1117c1ad5342SDoug Moore 	root = y != rlist ? y : NULL;					\
11189f701172SKonstantin Belousov } while (0)
11199f701172SKonstantin Belousov 
11200164e057SAlan Cox /*
1121c1ad5342SDoug Moore  * Walk down the tree until we find addr or a gap where addr would go, breaking
1122c1ad5342SDoug Moore  * off left and right subtrees of nodes less than, or greater than addr.  Treat
1123c1ad5342SDoug Moore  * subtrees with root->max_free < length as empty trees.  llist and rlist are
1124c1ad5342SDoug Moore  * the two sides in reverse order (bottom-up), with llist linked by the right
1125c1ad5342SDoug Moore  * pointer and rlist linked by the left pointer in the vm_map_entry, and both
1126c1ad5342SDoug Moore  * lists terminated by &map->header.  This function, and the subsequent call to
1127c1ad5342SDoug Moore  * vm_map_splay_merge_{left,right,pred,succ}, rely on the start and end address
11285a0879daSDoug Moore  * values in &map->header.
11294e94f402SAlan Cox  */
11301867d2f2SDoug Moore static __always_inline vm_map_entry_t
11315a0879daSDoug Moore vm_map_splay_split(vm_map_t map, vm_offset_t addr, vm_size_t length,
11321867d2f2SDoug Moore     vm_map_entry_t *llist, vm_map_entry_t *rlist)
11334e94f402SAlan Cox {
1134c1ad5342SDoug Moore 	vm_map_entry_t left, right, root, y;
11354e94f402SAlan Cox 
1136c1ad5342SDoug Moore 	left = right = &map->header;
11375a0879daSDoug Moore 	root = map->root;
11389f701172SKonstantin Belousov 	while (root != NULL && root->max_free >= length) {
1139c1ad5342SDoug Moore 		KASSERT(left->end <= root->start &&
1140c1ad5342SDoug Moore 		    root->end <= right->start,
11415a0879daSDoug Moore 		    ("%s: root not within tree bounds", __func__));
11420164e057SAlan Cox 		if (addr < root->start) {
1143c1ad5342SDoug Moore 			SPLAY_LEFT_STEP(root, y, left, right,
11449f701172SKonstantin Belousov 			    y->max_free >= length && addr < y->start);
11457438d60bSAlan Cox 		} else if (addr >= root->end) {
1146c1ad5342SDoug Moore 			SPLAY_RIGHT_STEP(root, y, left, right,
11479f701172SKonstantin Belousov 			    y->max_free >= length && addr >= y->end);
11487438d60bSAlan Cox 		} else
11497438d60bSAlan Cox 			break;
11500164e057SAlan Cox 	}
1151c1ad5342SDoug Moore 	*llist = left;
1152c1ad5342SDoug Moore 	*rlist = right;
11539f701172SKonstantin Belousov 	return (root);
11549f701172SKonstantin Belousov }
11559f701172SKonstantin Belousov 
11561867d2f2SDoug Moore static __always_inline void
11571867d2f2SDoug Moore vm_map_splay_findnext(vm_map_entry_t root, vm_map_entry_t *rlist)
11589f701172SKonstantin Belousov {
1159c1ad5342SDoug Moore 	vm_map_entry_t hi, right, y;
11609f701172SKonstantin Belousov 
1161c1ad5342SDoug Moore 	right = *rlist;
1162c1ad5342SDoug Moore 	hi = root->right == right ? NULL : root->right;
1163c1ad5342SDoug Moore 	if (hi == NULL)
1164c1ad5342SDoug Moore 		return;
1165c1ad5342SDoug Moore 	do
1166c1ad5342SDoug Moore 		SPLAY_LEFT_STEP(hi, y, root, right, true);
1167c1ad5342SDoug Moore 	while (hi != NULL);
1168c1ad5342SDoug Moore 	*rlist = right;
11699f701172SKonstantin Belousov }
11709f701172SKonstantin Belousov 
11711867d2f2SDoug Moore static __always_inline void
11721867d2f2SDoug Moore vm_map_splay_findprev(vm_map_entry_t root, vm_map_entry_t *llist)
11739f701172SKonstantin Belousov {
1174c1ad5342SDoug Moore 	vm_map_entry_t left, lo, y;
11759f701172SKonstantin Belousov 
1176c1ad5342SDoug Moore 	left = *llist;
1177c1ad5342SDoug Moore 	lo = root->left == left ? NULL : root->left;
1178c1ad5342SDoug Moore 	if (lo == NULL)
1179c1ad5342SDoug Moore 		return;
1180c1ad5342SDoug Moore 	do
1181c1ad5342SDoug Moore 		SPLAY_RIGHT_STEP(lo, y, left, root, true);
1182c1ad5342SDoug Moore 	while (lo != NULL);
1183c1ad5342SDoug Moore 	*llist = left;
11849f701172SKonstantin Belousov }
11850164e057SAlan Cox 
11865a0879daSDoug Moore static inline void
11875a0879daSDoug Moore vm_map_entry_swap(vm_map_entry_t *a, vm_map_entry_t *b)
11885a0879daSDoug Moore {
11895a0879daSDoug Moore 	vm_map_entry_t tmp;
11905a0879daSDoug Moore 
11915a0879daSDoug Moore 	tmp = *b;
11925a0879daSDoug Moore 	*b = *a;
11935a0879daSDoug Moore 	*a = tmp;
11945a0879daSDoug Moore }
11955a0879daSDoug Moore 
11960164e057SAlan Cox /*
11979f701172SKonstantin Belousov  * Walk back up the two spines, flip the pointers and set max_free.  The
11989f701172SKonstantin Belousov  * subtrees of the root go at the bottom of llist and rlist.
11990164e057SAlan Cox  */
120085b7bedbSDoug Moore static vm_size_t
120185b7bedbSDoug Moore vm_map_splay_merge_left_walk(vm_map_entry_t header, vm_map_entry_t root,
120285b7bedbSDoug Moore     vm_map_entry_t tail, vm_size_t max_free, vm_map_entry_t llist)
12039f701172SKonstantin Belousov {
12045a0879daSDoug Moore 	do {
12050164e057SAlan Cox 		/*
12065a0879daSDoug Moore 		 * The max_free values of the children of llist are in
120785b7bedbSDoug Moore 		 * llist->max_free and max_free.  Update with the
12085a0879daSDoug Moore 		 * max value.
12090164e057SAlan Cox 		 */
121085b7bedbSDoug Moore 		llist->max_free = max_free =
121185b7bedbSDoug Moore 		    vm_size_max(llist->max_free, max_free);
121285b7bedbSDoug Moore 		vm_map_entry_swap(&llist->right, &tail);
121385b7bedbSDoug Moore 		vm_map_entry_swap(&tail, &llist);
121485b7bedbSDoug Moore 	} while (llist != header);
121585b7bedbSDoug Moore 	root->left = tail;
121685b7bedbSDoug Moore 	return (max_free);
12175a0879daSDoug Moore }
121885b7bedbSDoug Moore 
121985b7bedbSDoug Moore /*
122085b7bedbSDoug Moore  * When llist is known to be the predecessor of root.
122185b7bedbSDoug Moore  */
122285b7bedbSDoug Moore static inline vm_size_t
122385b7bedbSDoug Moore vm_map_splay_merge_pred(vm_map_entry_t header, vm_map_entry_t root,
122485b7bedbSDoug Moore     vm_map_entry_t llist)
122585b7bedbSDoug Moore {
122685b7bedbSDoug Moore 	vm_size_t max_free;
122785b7bedbSDoug Moore 
122885b7bedbSDoug Moore 	max_free = root->start - llist->end;
122985b7bedbSDoug Moore 	if (llist != header) {
123085b7bedbSDoug Moore 		max_free = vm_map_splay_merge_left_walk(header, root,
1231c1ad5342SDoug Moore 		    root, max_free, llist);
123285b7bedbSDoug Moore 	} else {
1233c1ad5342SDoug Moore 		root->left = header;
1234c1ad5342SDoug Moore 		header->right = root;
123585b7bedbSDoug Moore 	}
123685b7bedbSDoug Moore 	return (max_free);
123785b7bedbSDoug Moore }
123885b7bedbSDoug Moore 
123985b7bedbSDoug Moore /*
124085b7bedbSDoug Moore  * When llist may or may not be the predecessor of root.
124185b7bedbSDoug Moore  */
124285b7bedbSDoug Moore static inline vm_size_t
124385b7bedbSDoug Moore vm_map_splay_merge_left(vm_map_entry_t header, vm_map_entry_t root,
124485b7bedbSDoug Moore     vm_map_entry_t llist)
124585b7bedbSDoug Moore {
124685b7bedbSDoug Moore 	vm_size_t max_free;
124785b7bedbSDoug Moore 
124885b7bedbSDoug Moore 	max_free = vm_map_entry_max_free_left(root, llist);
124985b7bedbSDoug Moore 	if (llist != header) {
125085b7bedbSDoug Moore 		max_free = vm_map_splay_merge_left_walk(header, root,
1251c1ad5342SDoug Moore 		    root->left == llist ? root : root->left,
1252c1ad5342SDoug Moore 		    max_free, llist);
125385b7bedbSDoug Moore 	}
125485b7bedbSDoug Moore 	return (max_free);
125585b7bedbSDoug Moore }
125685b7bedbSDoug Moore 
125785b7bedbSDoug Moore static vm_size_t
125885b7bedbSDoug Moore vm_map_splay_merge_right_walk(vm_map_entry_t header, vm_map_entry_t root,
125985b7bedbSDoug Moore     vm_map_entry_t tail, vm_size_t max_free, vm_map_entry_t rlist)
126085b7bedbSDoug Moore {
12615a0879daSDoug Moore 	do {
12625a0879daSDoug Moore 		/*
12635a0879daSDoug Moore 		 * The max_free values of the children of rlist are in
126485b7bedbSDoug Moore 		 * rlist->max_free and max_free.  Update with the
12655a0879daSDoug Moore 		 * max value.
12665a0879daSDoug Moore 		 */
126785b7bedbSDoug Moore 		rlist->max_free = max_free =
126885b7bedbSDoug Moore 		    vm_size_max(rlist->max_free, max_free);
126985b7bedbSDoug Moore 		vm_map_entry_swap(&rlist->left, &tail);
127085b7bedbSDoug Moore 		vm_map_entry_swap(&tail, &rlist);
127185b7bedbSDoug Moore 	} while (rlist != header);
127285b7bedbSDoug Moore 	root->right = tail;
127385b7bedbSDoug Moore 	return (max_free);
12745a0879daSDoug Moore }
127585b7bedbSDoug Moore 
127685b7bedbSDoug Moore /*
127785b7bedbSDoug Moore  * When rlist is known to be the succecessor of root.
127885b7bedbSDoug Moore  */
127985b7bedbSDoug Moore static inline vm_size_t
128085b7bedbSDoug Moore vm_map_splay_merge_succ(vm_map_entry_t header, vm_map_entry_t root,
128185b7bedbSDoug Moore     vm_map_entry_t rlist)
128285b7bedbSDoug Moore {
128385b7bedbSDoug Moore 	vm_size_t max_free;
128485b7bedbSDoug Moore 
128585b7bedbSDoug Moore 	max_free = rlist->start - root->end;
128685b7bedbSDoug Moore 	if (rlist != header) {
128785b7bedbSDoug Moore 		max_free = vm_map_splay_merge_right_walk(header, root,
1288c1ad5342SDoug Moore 		    root, max_free, rlist);
128985b7bedbSDoug Moore 	} else {
1290c1ad5342SDoug Moore 		root->right = header;
1291c1ad5342SDoug Moore 		header->left = root;
129285b7bedbSDoug Moore 	}
129385b7bedbSDoug Moore 	return (max_free);
129485b7bedbSDoug Moore }
129585b7bedbSDoug Moore 
129685b7bedbSDoug Moore /*
129785b7bedbSDoug Moore  * When rlist may or may not be the succecessor of root.
129885b7bedbSDoug Moore  */
129985b7bedbSDoug Moore static inline vm_size_t
130085b7bedbSDoug Moore vm_map_splay_merge_right(vm_map_entry_t header, vm_map_entry_t root,
130185b7bedbSDoug Moore     vm_map_entry_t rlist)
130285b7bedbSDoug Moore {
130385b7bedbSDoug Moore 	vm_size_t max_free;
130485b7bedbSDoug Moore 
130585b7bedbSDoug Moore 	max_free = vm_map_entry_max_free_right(root, rlist);
130685b7bedbSDoug Moore 	if (rlist != header) {
130785b7bedbSDoug Moore 		max_free = vm_map_splay_merge_right_walk(header, root,
1308c1ad5342SDoug Moore 		    root->right == rlist ? root : root->right,
1309c1ad5342SDoug Moore 		    max_free, rlist);
131085b7bedbSDoug Moore 	}
131185b7bedbSDoug Moore 	return (max_free);
13124e94f402SAlan Cox }
13134e94f402SAlan Cox 
13144e94f402SAlan Cox /*
1315d1d3f7e1SDoug Moore  *	vm_map_splay:
1316d1d3f7e1SDoug Moore  *
1317d1d3f7e1SDoug Moore  *	The Sleator and Tarjan top-down splay algorithm with the
1318d1d3f7e1SDoug Moore  *	following variation.  Max_free must be computed bottom-up, so
1319d1d3f7e1SDoug Moore  *	on the downward pass, maintain the left and right spines in
1320d1d3f7e1SDoug Moore  *	reverse order.  Then, make a second pass up each side to fix
1321d1d3f7e1SDoug Moore  *	the pointers and compute max_free.  The time bound is O(log n)
1322d1d3f7e1SDoug Moore  *	amortized.
1323d1d3f7e1SDoug Moore  *
1324c1ad5342SDoug Moore  *	The tree is threaded, which means that there are no null pointers.
1325c1ad5342SDoug Moore  *	When a node has no left child, its left pointer points to its
1326c1ad5342SDoug Moore  *	predecessor, which the last ancestor on the search path from the root
1327c1ad5342SDoug Moore  *	where the search branched right.  Likewise, when a node has no right
1328c1ad5342SDoug Moore  *	child, its right pointer points to its successor.  The map header node
1329c1ad5342SDoug Moore  *	is the predecessor of the first map entry, and the successor of the
1330c1ad5342SDoug Moore  *	last.
1331c1ad5342SDoug Moore  *
1332d1d3f7e1SDoug Moore  *	The new root is the vm_map_entry containing "addr", or else an
1333d1d3f7e1SDoug Moore  *	adjacent entry (lower if possible) if addr is not in the tree.
1334d1d3f7e1SDoug Moore  *
1335d1d3f7e1SDoug Moore  *	The map must be locked, and leaves it so.
1336d1d3f7e1SDoug Moore  *
1337d1d3f7e1SDoug Moore  *	Returns: the new root.
1338d1d3f7e1SDoug Moore  */
1339d1d3f7e1SDoug Moore static vm_map_entry_t
1340d1d3f7e1SDoug Moore vm_map_splay(vm_map_t map, vm_offset_t addr)
1341d1d3f7e1SDoug Moore {
134285b7bedbSDoug Moore 	vm_map_entry_t header, llist, rlist, root;
134385b7bedbSDoug Moore 	vm_size_t max_free_left, max_free_right;
1344d1d3f7e1SDoug Moore 
134585b7bedbSDoug Moore 	header = &map->header;
1346d1d3f7e1SDoug Moore 	root = vm_map_splay_split(map, addr, 0, &llist, &rlist);
1347d1d3f7e1SDoug Moore 	if (root != NULL) {
134885b7bedbSDoug Moore 		max_free_left = vm_map_splay_merge_left(header, root, llist);
134985b7bedbSDoug Moore 		max_free_right = vm_map_splay_merge_right(header, root, rlist);
135085b7bedbSDoug Moore 	} else if (llist != header) {
1351d1d3f7e1SDoug Moore 		/*
1352d1d3f7e1SDoug Moore 		 * Recover the greatest node in the left
1353d1d3f7e1SDoug Moore 		 * subtree and make it the root.
1354d1d3f7e1SDoug Moore 		 */
1355d1d3f7e1SDoug Moore 		root = llist;
1356d1d3f7e1SDoug Moore 		llist = root->right;
135785b7bedbSDoug Moore 		max_free_left = vm_map_splay_merge_left(header, root, llist);
135885b7bedbSDoug Moore 		max_free_right = vm_map_splay_merge_succ(header, root, rlist);
135985b7bedbSDoug Moore 	} else if (rlist != header) {
1360d1d3f7e1SDoug Moore 		/*
1361d1d3f7e1SDoug Moore 		 * Recover the least node in the right
1362d1d3f7e1SDoug Moore 		 * subtree and make it the root.
1363d1d3f7e1SDoug Moore 		 */
1364d1d3f7e1SDoug Moore 		root = rlist;
1365d1d3f7e1SDoug Moore 		rlist = root->left;
136685b7bedbSDoug Moore 		max_free_left = vm_map_splay_merge_pred(header, root, llist);
136785b7bedbSDoug Moore 		max_free_right = vm_map_splay_merge_right(header, root, rlist);
1368d1d3f7e1SDoug Moore 	} else {
1369d1d3f7e1SDoug Moore 		/* There is no root. */
1370d1d3f7e1SDoug Moore 		return (NULL);
1371d1d3f7e1SDoug Moore 	}
137285b7bedbSDoug Moore 	root->max_free = vm_size_max(max_free_left, max_free_right);
137385b7bedbSDoug Moore 	map->root = root;
1374d1d3f7e1SDoug Moore 	VM_MAP_ASSERT_CONSISTENT(map);
1375d1d3f7e1SDoug Moore 	return (root);
1376d1d3f7e1SDoug Moore }
1377d1d3f7e1SDoug Moore 
1378d1d3f7e1SDoug Moore /*
1379df8bae1dSRodney W. Grimes  *	vm_map_entry_{un,}link:
1380df8bae1dSRodney W. Grimes  *
1381668a8aa8SDoug Moore  *	Insert/remove entries from maps.  On linking, if new entry clips
1382668a8aa8SDoug Moore  *	existing entry, trim existing entry to avoid overlap, and manage
1383668a8aa8SDoug Moore  *	offsets.  On unlinking, merge disappearing entry with neighbor, if
1384668a8aa8SDoug Moore  *	called for, and manage offsets.  Callers should not modify fields in
1385668a8aa8SDoug Moore  *	entries already mapped.
1386df8bae1dSRodney W. Grimes  */
13874e94f402SAlan Cox static void
13885a0879daSDoug Moore vm_map_entry_link(vm_map_t map, vm_map_entry_t entry)
138999c81ca9SAlan Cox {
139085b7bedbSDoug Moore 	vm_map_entry_t header, llist, rlist, root;
1391668a8aa8SDoug Moore 	vm_size_t max_free_left, max_free_right;
139221c641b2SJohn Baldwin 
13939f701172SKonstantin Belousov 	CTR3(KTR_VM,
13949f701172SKonstantin Belousov 	    "vm_map_entry_link: map %p, nentries %d, entry %p", map,
13959f701172SKonstantin Belousov 	    map->nentries, entry);
13963a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
139799c81ca9SAlan Cox 	map->nentries++;
139885b7bedbSDoug Moore 	header = &map->header;
13995a0879daSDoug Moore 	root = vm_map_splay_split(map, entry->start, 0, &llist, &rlist);
1400668a8aa8SDoug Moore 	if (root == NULL) {
1401668a8aa8SDoug Moore 		/*
1402668a8aa8SDoug Moore 		 * The new entry does not overlap any existing entry in the
1403668a8aa8SDoug Moore 		 * map, so it becomes the new root of the map tree.
1404668a8aa8SDoug Moore 		 */
1405668a8aa8SDoug Moore 		max_free_left = vm_map_splay_merge_pred(header, entry, llist);
1406668a8aa8SDoug Moore 		max_free_right = vm_map_splay_merge_succ(header, entry, rlist);
1407668a8aa8SDoug Moore 	} else if (entry->start == root->start) {
1408668a8aa8SDoug Moore 		/*
1409668a8aa8SDoug Moore 		 * The new entry is a clone of root, with only the end field
1410668a8aa8SDoug Moore 		 * changed.  The root entry will be shrunk to abut the new
1411668a8aa8SDoug Moore 		 * entry, and will be the right child of the new root entry in
1412668a8aa8SDoug Moore 		 * the modified map.
1413668a8aa8SDoug Moore 		 */
1414668a8aa8SDoug Moore 		KASSERT(entry->end < root->end,
1415668a8aa8SDoug Moore 		    ("%s: clip_start not within entry", __func__));
1416668a8aa8SDoug Moore 		vm_map_splay_findprev(root, &llist);
1417668a8aa8SDoug Moore 		root->offset += entry->end - root->start;
1418668a8aa8SDoug Moore 		root->start = entry->end;
1419668a8aa8SDoug Moore 		max_free_left = vm_map_splay_merge_pred(header, entry, llist);
1420668a8aa8SDoug Moore 		max_free_right = root->max_free = vm_size_max(
1421668a8aa8SDoug Moore 		    vm_map_splay_merge_pred(entry, root, entry),
1422668a8aa8SDoug Moore 		    vm_map_splay_merge_right(header, root, rlist));
1423668a8aa8SDoug Moore 	} else {
1424668a8aa8SDoug Moore 		/*
1425668a8aa8SDoug Moore 		 * The new entry is a clone of root, with only the start field
1426668a8aa8SDoug Moore 		 * changed.  The root entry will be shrunk to abut the new
1427668a8aa8SDoug Moore 		 * entry, and will be the left child of the new root entry in
1428668a8aa8SDoug Moore 		 * the modified map.
1429668a8aa8SDoug Moore 		 */
1430668a8aa8SDoug Moore 		KASSERT(entry->end == root->end,
1431668a8aa8SDoug Moore 		    ("%s: clip_start not within entry", __func__));
1432668a8aa8SDoug Moore 		vm_map_splay_findnext(root, &rlist);
1433668a8aa8SDoug Moore 		entry->offset += entry->start - root->start;
1434668a8aa8SDoug Moore 		root->end = entry->start;
1435668a8aa8SDoug Moore 		max_free_left = root->max_free = vm_size_max(
1436668a8aa8SDoug Moore 		    vm_map_splay_merge_left(header, root, llist),
1437668a8aa8SDoug Moore 		    vm_map_splay_merge_succ(entry, root, entry));
1438668a8aa8SDoug Moore 		max_free_right = vm_map_splay_merge_succ(header, entry, rlist);
1439668a8aa8SDoug Moore 	}
1440668a8aa8SDoug Moore 	entry->max_free = vm_size_max(max_free_left, max_free_right);
1441668a8aa8SDoug Moore 	map->root = entry;
14429f701172SKonstantin Belousov 	VM_MAP_ASSERT_CONSISTENT(map);
1443df8bae1dSRodney W. Grimes }
144499c81ca9SAlan Cox 
14459f701172SKonstantin Belousov enum unlink_merge_type {
14469f701172SKonstantin Belousov 	UNLINK_MERGE_NONE,
14479f701172SKonstantin Belousov 	UNLINK_MERGE_NEXT
14489f701172SKonstantin Belousov };
14499f701172SKonstantin Belousov 
14504e94f402SAlan Cox static void
14515a0879daSDoug Moore vm_map_entry_unlink(vm_map_t map, vm_map_entry_t entry,
14529f701172SKonstantin Belousov     enum unlink_merge_type op)
145399c81ca9SAlan Cox {
1454c1ad5342SDoug Moore 	vm_map_entry_t header, llist, rlist, root;
145585b7bedbSDoug Moore 	vm_size_t max_free_left, max_free_right;
145699c81ca9SAlan Cox 
14573a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
145885b7bedbSDoug Moore 	header = &map->header;
14595a0879daSDoug Moore 	root = vm_map_splay_split(map, entry->start, 0, &llist, &rlist);
14609f701172SKonstantin Belousov 	KASSERT(root != NULL,
14619f701172SKonstantin Belousov 	    ("vm_map_entry_unlink: unlink object not mapped"));
14624e94f402SAlan Cox 
14631867d2f2SDoug Moore 	vm_map_splay_findprev(root, &llist);
14649f701172SKonstantin Belousov 	vm_map_splay_findnext(root, &rlist);
14651867d2f2SDoug Moore 	if (op == UNLINK_MERGE_NEXT) {
14669f701172SKonstantin Belousov 		rlist->start = root->start;
14679f701172SKonstantin Belousov 		rlist->offset = root->offset;
14681867d2f2SDoug Moore 	}
146985b7bedbSDoug Moore 	if (llist != header) {
14709f701172SKonstantin Belousov 		root = llist;
14719f701172SKonstantin Belousov 		llist = root->right;
147285b7bedbSDoug Moore 		max_free_left = vm_map_splay_merge_left(header, root, llist);
147385b7bedbSDoug Moore 		max_free_right = vm_map_splay_merge_succ(header, root, rlist);
147485b7bedbSDoug Moore 	} else if (rlist != header) {
14759f701172SKonstantin Belousov 		root = rlist;
14769f701172SKonstantin Belousov 		rlist = root->left;
147785b7bedbSDoug Moore 		max_free_left = vm_map_splay_merge_pred(header, root, llist);
147885b7bedbSDoug Moore 		max_free_right = vm_map_splay_merge_right(header, root, rlist);
1479c1ad5342SDoug Moore 	} else {
1480c1ad5342SDoug Moore 		header->left = header->right = header;
14819f701172SKonstantin Belousov 		root = NULL;
1482c1ad5342SDoug Moore 	}
14839f701172SKonstantin Belousov 	if (root != NULL)
148485b7bedbSDoug Moore 		root->max_free = vm_size_max(max_free_left, max_free_right);
148585b7bedbSDoug Moore 	map->root = root;
14869f701172SKonstantin Belousov 	VM_MAP_ASSERT_CONSISTENT(map);
148799c81ca9SAlan Cox 	map->nentries--;
148821c641b2SJohn Baldwin 	CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map,
148921c641b2SJohn Baldwin 	    map->nentries, entry);
1490df8bae1dSRodney W. Grimes }
1491df8bae1dSRodney W. Grimes 
1492df8bae1dSRodney W. Grimes /*
1493fa581662SDoug Moore  *	vm_map_entry_resize:
14940164e057SAlan Cox  *
1495fa581662SDoug Moore  *	Resize a vm_map_entry, recompute the amount of free space that
1496fa581662SDoug Moore  *	follows it and propagate that value up the tree.
14970164e057SAlan Cox  *
14980164e057SAlan Cox  *	The map must be locked, and leaves it so.
14990164e057SAlan Cox  */
15000164e057SAlan Cox static void
1501fa581662SDoug Moore vm_map_entry_resize(vm_map_t map, vm_map_entry_t entry, vm_size_t grow_amount)
15020164e057SAlan Cox {
150385b7bedbSDoug Moore 	vm_map_entry_t header, llist, rlist, root;
15040164e057SAlan Cox 
15059f701172SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
150685b7bedbSDoug Moore 	header = &map->header;
15075a0879daSDoug Moore 	root = vm_map_splay_split(map, entry->start, 0, &llist, &rlist);
15081867d2f2SDoug Moore 	KASSERT(root != NULL, ("%s: resize object not mapped", __func__));
15099f701172SKonstantin Belousov 	vm_map_splay_findnext(root, &rlist);
15101895f520SDoug Moore 	entry->end += grow_amount;
151185b7bedbSDoug Moore 	root->max_free = vm_size_max(
151285b7bedbSDoug Moore 	    vm_map_splay_merge_left(header, root, llist),
151385b7bedbSDoug Moore 	    vm_map_splay_merge_succ(header, root, rlist));
151485b7bedbSDoug Moore 	map->root = root;
15159f701172SKonstantin Belousov 	VM_MAP_ASSERT_CONSISTENT(map);
1516fa581662SDoug Moore 	CTR4(KTR_VM, "%s: map %p, nentries %d, entry %p",
151773f11451SDoug Moore 	    __func__, map, map->nentries, entry);
15180164e057SAlan Cox }
15190164e057SAlan Cox 
15200164e057SAlan Cox /*
1521d1d3f7e1SDoug Moore  *	vm_map_lookup_entry:	[ internal use only ]
1522df8bae1dSRodney W. Grimes  *
1523d1d3f7e1SDoug Moore  *	Finds the map entry containing (or
1524d1d3f7e1SDoug Moore  *	immediately preceding) the specified address
1525d1d3f7e1SDoug Moore  *	in the given map; the entry is returned
1526d1d3f7e1SDoug Moore  *	in the "entry" parameter.  The boolean
1527d1d3f7e1SDoug Moore  *	result indicates whether the address is
1528d1d3f7e1SDoug Moore  *	actually contained in the map.
1529df8bae1dSRodney W. Grimes  */
1530d1d3f7e1SDoug Moore boolean_t
1531d1d3f7e1SDoug Moore vm_map_lookup_entry(
1532d1d3f7e1SDoug Moore 	vm_map_t map,
1533d1d3f7e1SDoug Moore 	vm_offset_t address,
1534d1d3f7e1SDoug Moore 	vm_map_entry_t *entry)	/* OUT */
1535df8bae1dSRodney W. Grimes {
1536c1ad5342SDoug Moore 	vm_map_entry_t cur, header, lbound, ubound;
1537d1d3f7e1SDoug Moore 	boolean_t locked;
1538df8bae1dSRodney W. Grimes 
15394c3ef59eSAlan Cox 	/*
15404c3ef59eSAlan Cox 	 * If the map is empty, then the map entry immediately preceding
1541d1d3f7e1SDoug Moore 	 * "address" is the map's header.
15424c3ef59eSAlan Cox 	 */
154385b7bedbSDoug Moore 	header = &map->header;
1544d1d3f7e1SDoug Moore 	cur = map->root;
1545d1d3f7e1SDoug Moore 	if (cur == NULL) {
154685b7bedbSDoug Moore 		*entry = header;
1547d1d3f7e1SDoug Moore 		return (FALSE);
1548d1d3f7e1SDoug Moore 	}
1549d1d3f7e1SDoug Moore 	if (address >= cur->start && cur->end > address) {
1550d1d3f7e1SDoug Moore 		*entry = cur;
1551d1d3f7e1SDoug Moore 		return (TRUE);
15529f701172SKonstantin Belousov 	}
15539f701172SKonstantin Belousov 	if ((locked = vm_map_locked(map)) ||
155405a8c414SAlan Cox 	    sx_try_upgrade(&map->lock)) {
155505a8c414SAlan Cox 		/*
155605a8c414SAlan Cox 		 * Splay requires a write lock on the map.  However, it only
155705a8c414SAlan Cox 		 * restructures the binary search tree; it does not otherwise
155805a8c414SAlan Cox 		 * change the map.  Thus, the map's timestamp need not change
155905a8c414SAlan Cox 		 * on a temporary upgrade.
156005a8c414SAlan Cox 		 */
1561d1d3f7e1SDoug Moore 		cur = vm_map_splay(map, address);
1562461587dcSDoug Moore 		if (!locked) {
1563461587dcSDoug Moore 			VM_MAP_UNLOCK_CONSISTENT(map);
156405a8c414SAlan Cox 			sx_downgrade(&map->lock);
1565461587dcSDoug Moore 		}
1566d1d3f7e1SDoug Moore 
1567d1d3f7e1SDoug Moore 		/*
1568d1d3f7e1SDoug Moore 		 * If "address" is contained within a map entry, the new root
1569d1d3f7e1SDoug Moore 		 * is that map entry.  Otherwise, the new root is a map entry
1570d1d3f7e1SDoug Moore 		 * immediately before or after "address".
1571d1d3f7e1SDoug Moore 		 */
1572d1d3f7e1SDoug Moore 		if (address < cur->start) {
157385b7bedbSDoug Moore 			*entry = header;
1574d1d3f7e1SDoug Moore 			return (FALSE);
1575d1d3f7e1SDoug Moore 		}
1576d1d3f7e1SDoug Moore 		*entry = cur;
1577d1d3f7e1SDoug Moore 		return (address < cur->end);
15789f701172SKonstantin Belousov 	}
157905a8c414SAlan Cox 	/*
158005a8c414SAlan Cox 	 * Since the map is only locked for read access, perform a
1581d1d3f7e1SDoug Moore 	 * standard binary search tree lookup for "address".
158205a8c414SAlan Cox 	 */
1583c1ad5342SDoug Moore 	lbound = ubound = header;
1584c1ad5342SDoug Moore 	for (;;) {
1585d1d3f7e1SDoug Moore 		if (address < cur->start) {
1586c1ad5342SDoug Moore 			ubound = cur;
1587d1d3f7e1SDoug Moore 			cur = cur->left;
1588c1ad5342SDoug Moore 			if (cur == lbound)
1589c1ad5342SDoug Moore 				break;
1590d1d3f7e1SDoug Moore 		} else if (cur->end <= address) {
1591d1d3f7e1SDoug Moore 			lbound = cur;
1592d1d3f7e1SDoug Moore 			cur = cur->right;
1593c1ad5342SDoug Moore 			if (cur == ubound)
1594c1ad5342SDoug Moore 				break;
15959f701172SKonstantin Belousov 		} else {
1596d1d3f7e1SDoug Moore 			*entry = cur;
1597d1d3f7e1SDoug Moore 			return (TRUE);
159805a8c414SAlan Cox 		}
1599c1ad5342SDoug Moore 	}
1600d1d3f7e1SDoug Moore 	*entry = lbound;
1601d1d3f7e1SDoug Moore 	return (FALSE);
1602df8bae1dSRodney W. Grimes }
1603df8bae1dSRodney W. Grimes 
1604df8bae1dSRodney W. Grimes /*
160530dcfc09SJohn Dyson  *	vm_map_insert:
160630dcfc09SJohn Dyson  *
160730dcfc09SJohn Dyson  *	Inserts the given whole VM object into the target
160830dcfc09SJohn Dyson  *	map at the specified address range.  The object's
160930dcfc09SJohn Dyson  *	size should match that of the address range.
161030dcfc09SJohn Dyson  *
161130dcfc09SJohn Dyson  *	Requires that the map be locked, and leaves it so.
16122aaeadf8SMatthew Dillon  *
16132aaeadf8SMatthew Dillon  *	If object is non-NULL, ref count must be bumped by caller
16142aaeadf8SMatthew Dillon  *	prior to making call to account for the new entry.
161530dcfc09SJohn Dyson  */
161630dcfc09SJohn Dyson int
1617b9dcd593SBruce Evans vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
161833314db0SAlan Cox     vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max, int cow)
161930dcfc09SJohn Dyson {
162083704cc2SDoug Moore 	vm_map_entry_t new_entry, next_entry, prev_entry;
1621ef694c1aSEdward Tomasz Napierala 	struct ucred *cred;
16221569205fSKonstantin Belousov 	vm_eflags_t protoeflags;
16238211bd45SKonstantin Belousov 	vm_inherit_t inheritance;
1624e2e80fb3SKonstantin Belousov 	u_long bdry;
1625e2e80fb3SKonstantin Belousov 	u_int bidx;
162630dcfc09SJohn Dyson 
16273a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
16282e47807cSJeff Roberson 	KASSERT(object != kernel_object ||
162933314db0SAlan Cox 	    (cow & MAP_COPY_ON_WRITE) == 0,
16302e47807cSJeff Roberson 	    ("vm_map_insert: kernel object and COW"));
1631e2e80fb3SKonstantin Belousov 	KASSERT(object == NULL || (cow & MAP_NOFAULT) == 0 ||
1632e2e80fb3SKonstantin Belousov 	    (cow & MAP_SPLIT_BOUNDARY_MASK) != 0,
1633e2e80fb3SKonstantin Belousov 	    ("vm_map_insert: paradoxical MAP_NOFAULT request, obj %p cow %#x",
1634e2e80fb3SKonstantin Belousov 	    object, cow));
163500de6773SKonstantin Belousov 	KASSERT((prot & ~max) == 0,
163600de6773SKonstantin Belousov 	    ("prot %#x is not subset of max_prot %#x", prot, max));
16373a0916b8SKonstantin Belousov 
163830dcfc09SJohn Dyson 	/*
163930dcfc09SJohn Dyson 	 * Check that the start and end points are not bogus.
164030dcfc09SJohn Dyson 	 */
1641f0340740SMark Johnston 	if (start == end || !vm_map_range_valid(map, start, end))
164230dcfc09SJohn Dyson 		return (KERN_INVALID_ADDRESS);
164330dcfc09SJohn Dyson 
16442e1c94aaSKonstantin Belousov 	if ((map->flags & MAP_WXORX) != 0 && (prot & (VM_PROT_WRITE |
16452e1c94aaSKonstantin Belousov 	    VM_PROT_EXECUTE)) == (VM_PROT_WRITE | VM_PROT_EXECUTE))
16462e1c94aaSKonstantin Belousov 		return (KERN_PROTECTION_FAILURE);
16472e1c94aaSKonstantin Belousov 
164830dcfc09SJohn Dyson 	/*
164930dcfc09SJohn Dyson 	 * Find the entry prior to the proposed starting address; if it's part
165030dcfc09SJohn Dyson 	 * of an existing entry, this range is bogus.
165130dcfc09SJohn Dyson 	 */
1652723413beSDoug Moore 	if (vm_map_lookup_entry(map, start, &prev_entry))
165330dcfc09SJohn Dyson 		return (KERN_NO_SPACE);
165430dcfc09SJohn Dyson 
165530dcfc09SJohn Dyson 	/*
165630dcfc09SJohn Dyson 	 * Assert that the next entry doesn't overlap the end point.
165730dcfc09SJohn Dyson 	 */
165883704cc2SDoug Moore 	next_entry = vm_map_entry_succ(prev_entry);
165983704cc2SDoug Moore 	if (next_entry->start < end)
166030dcfc09SJohn Dyson 		return (KERN_NO_SPACE);
166130dcfc09SJohn Dyson 
166219bd0d9cSKonstantin Belousov 	if ((cow & MAP_CREATE_GUARD) != 0 && (object != NULL ||
166319bd0d9cSKonstantin Belousov 	    max != VM_PROT_NONE))
166419bd0d9cSKonstantin Belousov 		return (KERN_INVALID_ARGUMENT);
166519bd0d9cSKonstantin Belousov 
1666afa07f7eSJohn Dyson 	protoeflags = 0;
1667afa07f7eSJohn Dyson 	if (cow & MAP_COPY_ON_WRITE)
1668e5f13bddSAlan Cox 		protoeflags |= MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY;
166933314db0SAlan Cox 	if (cow & MAP_NOFAULT)
1670afa07f7eSJohn Dyson 		protoeflags |= MAP_ENTRY_NOFAULT;
16714f79d873SMatthew Dillon 	if (cow & MAP_DISABLE_SYNCER)
16724f79d873SMatthew Dillon 		protoeflags |= MAP_ENTRY_NOSYNC;
16739730a5daSPaul Saab 	if (cow & MAP_DISABLE_COREDUMP)
16749730a5daSPaul Saab 		protoeflags |= MAP_ENTRY_NOCOREDUMP;
1675712efe66SAlan Cox 	if (cow & MAP_STACK_GROWS_DOWN)
1676712efe66SAlan Cox 		protoeflags |= MAP_ENTRY_GROWS_DOWN;
1677712efe66SAlan Cox 	if (cow & MAP_STACK_GROWS_UP)
1678712efe66SAlan Cox 		protoeflags |= MAP_ENTRY_GROWS_UP;
1679fe7bcbafSKyle Evans 	if (cow & MAP_WRITECOUNT)
1680fe7bcbafSKyle Evans 		protoeflags |= MAP_ENTRY_WRITECNT;
168178022527SKonstantin Belousov 	if (cow & MAP_VN_EXEC)
168278022527SKonstantin Belousov 		protoeflags |= MAP_ENTRY_VN_EXEC;
168319bd0d9cSKonstantin Belousov 	if ((cow & MAP_CREATE_GUARD) != 0)
168419bd0d9cSKonstantin Belousov 		protoeflags |= MAP_ENTRY_GUARD;
168519bd0d9cSKonstantin Belousov 	if ((cow & MAP_CREATE_STACK_GAP_DN) != 0)
168619bd0d9cSKonstantin Belousov 		protoeflags |= MAP_ENTRY_STACK_GAP_DN;
168719bd0d9cSKonstantin Belousov 	if ((cow & MAP_CREATE_STACK_GAP_UP) != 0)
168819bd0d9cSKonstantin Belousov 		protoeflags |= MAP_ENTRY_STACK_GAP_UP;
16898211bd45SKonstantin Belousov 	if (cow & MAP_INHERIT_SHARE)
16908211bd45SKonstantin Belousov 		inheritance = VM_INHERIT_SHARE;
16918211bd45SKonstantin Belousov 	else
16928211bd45SKonstantin Belousov 		inheritance = VM_INHERIT_DEFAULT;
1693e2e80fb3SKonstantin Belousov 	if ((cow & MAP_SPLIT_BOUNDARY_MASK) != 0) {
1694e2e80fb3SKonstantin Belousov 		/* This magically ignores index 0, for usual page size. */
1695e2e80fb3SKonstantin Belousov 		bidx = (cow & MAP_SPLIT_BOUNDARY_MASK) >>
1696e2e80fb3SKonstantin Belousov 		    MAP_SPLIT_BOUNDARY_SHIFT;
1697e2e80fb3SKonstantin Belousov 		if (bidx >= MAXPAGESIZES)
1698e2e80fb3SKonstantin Belousov 			return (KERN_INVALID_ARGUMENT);
1699e2e80fb3SKonstantin Belousov 		bdry = pagesizes[bidx] - 1;
1700e2e80fb3SKonstantin Belousov 		if ((start & bdry) != 0 || (end & bdry) != 0)
1701e2e80fb3SKonstantin Belousov 			return (KERN_INVALID_ARGUMENT);
1702e2e80fb3SKonstantin Belousov 		protoeflags |= bidx << MAP_ENTRY_SPLIT_BOUNDARY_SHIFT;
1703e2e80fb3SKonstantin Belousov 	}
17044f79d873SMatthew Dillon 
1705ef694c1aSEdward Tomasz Napierala 	cred = NULL;
170619bd0d9cSKonstantin Belousov 	if ((cow & (MAP_ACC_NO_CHARGE | MAP_NOFAULT | MAP_CREATE_GUARD)) != 0)
17073364c323SKonstantin Belousov 		goto charged;
17083364c323SKonstantin Belousov 	if ((cow & MAP_ACC_CHARGED) || ((prot & VM_PROT_WRITE) &&
17093364c323SKonstantin Belousov 	    ((protoeflags & MAP_ENTRY_NEEDS_COPY) || object == NULL))) {
17103364c323SKonstantin Belousov 		if (!(cow & MAP_ACC_CHARGED) && !swap_reserve(end - start))
17113364c323SKonstantin Belousov 			return (KERN_RESOURCE_SHORTAGE);
17121569205fSKonstantin Belousov 		KASSERT(object == NULL ||
17131569205fSKonstantin Belousov 		    (protoeflags & MAP_ENTRY_NEEDS_COPY) != 0 ||
1714ef694c1aSEdward Tomasz Napierala 		    object->cred == NULL,
17151569205fSKonstantin Belousov 		    ("overcommit: vm_map_insert o %p", object));
1716ef694c1aSEdward Tomasz Napierala 		cred = curthread->td_ucred;
17173364c323SKonstantin Belousov 	}
17183364c323SKonstantin Belousov 
17193364c323SKonstantin Belousov charged:
1720f8616ebfSAlan Cox 	/* Expand the kernel pmap, if necessary. */
1721f8616ebfSAlan Cox 	if (map == kernel_map && end > kernel_vm_end)
1722f8616ebfSAlan Cox 		pmap_growkernel(end);
17231d284e00SAlan Cox 	if (object != NULL) {
172430dcfc09SJohn Dyson 		/*
17251d284e00SAlan Cox 		 * OBJ_ONEMAPPING must be cleared unless this mapping
17261d284e00SAlan Cox 		 * is trivially proven to be the only mapping for any
17271d284e00SAlan Cox 		 * of the object's pages.  (Object granularity
17281d284e00SAlan Cox 		 * reference counting is insufficient to recognize
17291d284e00SAlan Cox 		 * aliases with precision.)
173030dcfc09SJohn Dyson 		 */
173163967687SJeff Roberson 		if ((object->flags & OBJ_ANON) != 0) {
173289f6b863SAttilio Rao 			VM_OBJECT_WLOCK(object);
17331d284e00SAlan Cox 			if (object->ref_count > 1 || object->shadow_count != 0)
17342aaeadf8SMatthew Dillon 				vm_object_clear_flag(object, OBJ_ONEMAPPING);
173589f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
173663967687SJeff Roberson 		}
17372203c46dSMark Johnston 	} else if ((prev_entry->eflags & ~MAP_ENTRY_USER_WIRED) ==
17382203c46dSMark Johnston 	    protoeflags &&
173978022527SKonstantin Belousov 	    (cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP |
174078022527SKonstantin Belousov 	    MAP_VN_EXEC)) == 0 &&
1741737e25f7SAlan Cox 	    prev_entry->end == start && (prev_entry->cred == cred ||
17423364c323SKonstantin Belousov 	    (prev_entry->object.vm_object != NULL &&
17431569205fSKonstantin Belousov 	    prev_entry->object.vm_object->cred == cred)) &&
17448cc7e047SJohn Dyson 	    vm_object_coalesce(prev_entry->object.vm_object,
174557a21abaSAlan Cox 	    prev_entry->offset,
17468cc7e047SJohn Dyson 	    (vm_size_t)(prev_entry->end - prev_entry->start),
174760169c88SAlan Cox 	    (vm_size_t)(end - prev_entry->end), cred != NULL &&
174860169c88SAlan Cox 	    (protoeflags & MAP_ENTRY_NEEDS_COPY) == 0)) {
174930dcfc09SJohn Dyson 		/*
17502aaeadf8SMatthew Dillon 		 * We were able to extend the object.  Determine if we
17512aaeadf8SMatthew Dillon 		 * can extend the previous map entry to include the
17522aaeadf8SMatthew Dillon 		 * new range as well.
175330dcfc09SJohn Dyson 		 */
17541569205fSKonstantin Belousov 		if (prev_entry->inheritance == inheritance &&
17551569205fSKonstantin Belousov 		    prev_entry->protection == prot &&
1756737e25f7SAlan Cox 		    prev_entry->max_protection == max &&
1757737e25f7SAlan Cox 		    prev_entry->wired_count == 0) {
1758737e25f7SAlan Cox 			KASSERT((prev_entry->eflags & MAP_ENTRY_USER_WIRED) ==
1759737e25f7SAlan Cox 			    0, ("prev_entry %p has incoherent wiring",
1760737e25f7SAlan Cox 			    prev_entry));
176119bd0d9cSKonstantin Belousov 			if ((prev_entry->eflags & MAP_ENTRY_GUARD) == 0)
17621569205fSKonstantin Belousov 				map->size += end - prev_entry->end;
1763fa581662SDoug Moore 			vm_map_entry_resize(map, prev_entry,
17641895f520SDoug Moore 			    end - prev_entry->end);
176583704cc2SDoug Moore 			vm_map_try_merge_entries(map, prev_entry, next_entry);
176630dcfc09SJohn Dyson 			return (KERN_SUCCESS);
176730dcfc09SJohn Dyson 		}
17688cc7e047SJohn Dyson 
17692aaeadf8SMatthew Dillon 		/*
17702aaeadf8SMatthew Dillon 		 * If we can extend the object but cannot extend the
17712aaeadf8SMatthew Dillon 		 * map entry, we have to create a new map entry.  We
17722aaeadf8SMatthew Dillon 		 * must bump the ref count on the extended object to
17734e71e795SMatthew Dillon 		 * account for it.  object may be NULL.
17742aaeadf8SMatthew Dillon 		 */
17752aaeadf8SMatthew Dillon 		object = prev_entry->object.vm_object;
17762aaeadf8SMatthew Dillon 		offset = prev_entry->offset +
17772aaeadf8SMatthew Dillon 		    (prev_entry->end - prev_entry->start);
17788cc7e047SJohn Dyson 		vm_object_reference(object);
1779ef694c1aSEdward Tomasz Napierala 		if (cred != NULL && object != NULL && object->cred != NULL &&
17803364c323SKonstantin Belousov 		    !(prev_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
17813364c323SKonstantin Belousov 			/* Object already accounts for this uid. */
1782ef694c1aSEdward Tomasz Napierala 			cred = NULL;
17833364c323SKonstantin Belousov 		}
1784b18bfc3dSJohn Dyson 	}
178560169c88SAlan Cox 	if (cred != NULL)
178660169c88SAlan Cox 		crhold(cred);
17872aaeadf8SMatthew Dillon 
17882aaeadf8SMatthew Dillon 	/*
178930dcfc09SJohn Dyson 	 * Create a new entry
179030dcfc09SJohn Dyson 	 */
179130dcfc09SJohn Dyson 	new_entry = vm_map_entry_create(map);
179230dcfc09SJohn Dyson 	new_entry->start = start;
179330dcfc09SJohn Dyson 	new_entry->end = end;
1794ef694c1aSEdward Tomasz Napierala 	new_entry->cred = NULL;
179530dcfc09SJohn Dyson 
1796afa07f7eSJohn Dyson 	new_entry->eflags = protoeflags;
179730dcfc09SJohn Dyson 	new_entry->object.vm_object = object;
179830dcfc09SJohn Dyson 	new_entry->offset = offset;
17992267af78SJulian Elischer 
18008211bd45SKonstantin Belousov 	new_entry->inheritance = inheritance;
180130dcfc09SJohn Dyson 	new_entry->protection = prot;
180230dcfc09SJohn Dyson 	new_entry->max_protection = max;
180330dcfc09SJohn Dyson 	new_entry->wired_count = 0;
1804997ac690SKonstantin Belousov 	new_entry->wiring_thread = NULL;
180513458803SAlan Cox 	new_entry->read_ahead = VM_FAULT_READ_AHEAD_INIT;
1806381b7242SAlan Cox 	new_entry->next_read = start;
1807e5f251d2SAlan Cox 
1808ef694c1aSEdward Tomasz Napierala 	KASSERT(cred == NULL || !ENTRY_CHARGED(new_entry),
18091569205fSKonstantin Belousov 	    ("overcommit: vm_map_insert leaks vm_map %p", new_entry));
1810ef694c1aSEdward Tomasz Napierala 	new_entry->cred = cred;
18113364c323SKonstantin Belousov 
181230dcfc09SJohn Dyson 	/*
181330dcfc09SJohn Dyson 	 * Insert the new entry into the list
181430dcfc09SJohn Dyson 	 */
18159f701172SKonstantin Belousov 	vm_map_entry_link(map, new_entry);
181619bd0d9cSKonstantin Belousov 	if ((new_entry->eflags & MAP_ENTRY_GUARD) == 0)
181730dcfc09SJohn Dyson 		map->size += new_entry->end - new_entry->start;
181830dcfc09SJohn Dyson 
18191a484d28SMatthew Dillon 	/*
1820eaaf9f7fSAlan Cox 	 * Try to coalesce the new entry with both the previous and next
1821eaaf9f7fSAlan Cox 	 * entries in the list.  Previously, we only attempted to coalesce
1822eaaf9f7fSAlan Cox 	 * with the previous entry when object is NULL.  Here, we handle the
1823eaaf9f7fSAlan Cox 	 * other cases, which are less common.
18241a484d28SMatthew Dillon 	 */
182583ea714fSDoug Moore 	vm_map_try_merge_entries(map, prev_entry, new_entry);
182683704cc2SDoug Moore 	vm_map_try_merge_entries(map, new_entry, next_entry);
18274e71e795SMatthew Dillon 
18281569205fSKonstantin Belousov 	if ((cow & (MAP_PREFAULT | MAP_PREFAULT_PARTIAL)) != 0) {
18291569205fSKonstantin Belousov 		vm_map_pmap_enter(map, start, prot, object, OFF_TO_IDX(offset),
18301569205fSKonstantin Belousov 		    end - start, cow & MAP_PREFAULT_PARTIAL);
18314f79d873SMatthew Dillon 	}
1832e972780aSAlan Cox 
183330dcfc09SJohn Dyson 	return (KERN_SUCCESS);
183430dcfc09SJohn Dyson }
183530dcfc09SJohn Dyson 
183630dcfc09SJohn Dyson /*
18370164e057SAlan Cox  *	vm_map_findspace:
18380164e057SAlan Cox  *
18390164e057SAlan Cox  *	Find the first fit (lowest VM address) for "length" free bytes
18400164e057SAlan Cox  *	beginning at address >= start in the given map.
18410164e057SAlan Cox  *
18429f701172SKonstantin Belousov  *	In a vm_map_entry, "max_free" is the maximum amount of
18439f701172SKonstantin Belousov  *	contiguous free space between an entry in its subtree and a
18449f701172SKonstantin Belousov  *	neighbor of that entry.  This allows finding a free region in
18459f701172SKonstantin Belousov  *	one path down the tree, so O(log n) amortized with splay
18469f701172SKonstantin Belousov  *	trees.
18470164e057SAlan Cox  *
18480164e057SAlan Cox  *	The map must be locked, and leaves it so.
18490164e057SAlan Cox  *
18509f701172SKonstantin Belousov  *	Returns: starting address if sufficient space,
18519f701172SKonstantin Belousov  *		 vm_map_max(map)-length+1 if insufficient space.
1852df8bae1dSRodney W. Grimes  */
18539f701172SKonstantin Belousov vm_offset_t
18549f701172SKonstantin Belousov vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length)
1855df8bae1dSRodney W. Grimes {
185685b7bedbSDoug Moore 	vm_map_entry_t header, llist, rlist, root, y;
185785b7bedbSDoug Moore 	vm_size_t left_length, max_free_left, max_free_right;
1858e65d58a0SDoug Moore 	vm_offset_t gap_end;
1859df8bae1dSRodney W. Grimes 
186020f02659SMark Johnston 	VM_MAP_ASSERT_LOCKED(map);
186120f02659SMark Johnston 
1862986b43f8SAlan Cox 	/*
1863986b43f8SAlan Cox 	 * Request must fit within min/max VM address and must avoid
1864986b43f8SAlan Cox 	 * address wrap.
1865986b43f8SAlan Cox 	 */
1866f0165b1cSKonstantin Belousov 	start = MAX(start, vm_map_min(map));
1867e65d58a0SDoug Moore 	if (start >= vm_map_max(map) || length > vm_map_max(map) - start)
18689f701172SKonstantin Belousov 		return (vm_map_max(map) - length + 1);
1869df8bae1dSRodney W. Grimes 
18700164e057SAlan Cox 	/* Empty tree means wide open address space. */
18719f701172SKonstantin Belousov 	if (map->root == NULL)
18729f701172SKonstantin Belousov 		return (start);
18730164e057SAlan Cox 
18740164e057SAlan Cox 	/*
1875e65d58a0SDoug Moore 	 * After splay_split, if start is within an entry, push it to the start
1876e65d58a0SDoug Moore 	 * of the following gap.  If rlist is at the end of the gap containing
1877e65d58a0SDoug Moore 	 * start, save the end of that gap in gap_end to see if the gap is big
1878e65d58a0SDoug Moore 	 * enough; otherwise set gap_end to start skip gap-checking and move
1879e65d58a0SDoug Moore 	 * directly to a search of the right subtree.
18800164e057SAlan Cox 	 */
188185b7bedbSDoug Moore 	header = &map->header;
18825a0879daSDoug Moore 	root = vm_map_splay_split(map, start, length, &llist, &rlist);
1883e65d58a0SDoug Moore 	gap_end = rlist->start;
1884e65d58a0SDoug Moore 	if (root != NULL) {
18859f701172SKonstantin Belousov 		start = root->end;
1886c1ad5342SDoug Moore 		if (root->right != rlist)
1887e65d58a0SDoug Moore 			gap_end = start;
188885b7bedbSDoug Moore 		max_free_left = vm_map_splay_merge_left(header, root, llist);
188985b7bedbSDoug Moore 		max_free_right = vm_map_splay_merge_right(header, root, rlist);
189085b7bedbSDoug Moore 	} else if (rlist != header) {
18919f701172SKonstantin Belousov 		root = rlist;
18929f701172SKonstantin Belousov 		rlist = root->left;
189385b7bedbSDoug Moore 		max_free_left = vm_map_splay_merge_pred(header, root, llist);
189485b7bedbSDoug Moore 		max_free_right = vm_map_splay_merge_right(header, root, rlist);
18959f701172SKonstantin Belousov 	} else {
18969f701172SKonstantin Belousov 		root = llist;
18979f701172SKonstantin Belousov 		llist = root->right;
189885b7bedbSDoug Moore 		max_free_left = vm_map_splay_merge_left(header, root, llist);
189985b7bedbSDoug Moore 		max_free_right = vm_map_splay_merge_succ(header, root, rlist);
19000164e057SAlan Cox 	}
190185b7bedbSDoug Moore 	root->max_free = vm_size_max(max_free_left, max_free_right);
190285b7bedbSDoug Moore 	map->root = root;
19039f701172SKonstantin Belousov 	VM_MAP_ASSERT_CONSISTENT(map);
1904e65d58a0SDoug Moore 	if (length <= gap_end - start)
19059f701172SKonstantin Belousov 		return (start);
19060164e057SAlan Cox 
19070164e057SAlan Cox 	/* With max_free, can immediately tell if no solution. */
1908c1ad5342SDoug Moore 	if (root->right == header || length > root->right->max_free)
19099f701172SKonstantin Belousov 		return (vm_map_max(map) - length + 1);
19100164e057SAlan Cox 
19110164e057SAlan Cox 	/*
19129f701172SKonstantin Belousov 	 * Splay for the least large-enough gap in the right subtree.
19130164e057SAlan Cox 	 */
191485b7bedbSDoug Moore 	llist = rlist = header;
19159f701172SKonstantin Belousov 	for (left_length = 0;;
19165a0879daSDoug Moore 	    left_length = vm_map_entry_max_free_left(root, llist)) {
19179f701172SKonstantin Belousov 		if (length <= left_length)
1918c1ad5342SDoug Moore 			SPLAY_LEFT_STEP(root, y, llist, rlist,
19195a0879daSDoug Moore 			    length <= vm_map_entry_max_free_left(y, llist));
19209f701172SKonstantin Belousov 		else
1921c1ad5342SDoug Moore 			SPLAY_RIGHT_STEP(root, y, llist, rlist,
19225a0879daSDoug Moore 			    length > vm_map_entry_max_free_left(y, root));
19239f701172SKonstantin Belousov 		if (root == NULL)
19249f701172SKonstantin Belousov 			break;
19250164e057SAlan Cox 	}
19269f701172SKonstantin Belousov 	root = llist;
19279f701172SKonstantin Belousov 	llist = root->right;
192885b7bedbSDoug Moore 	max_free_left = vm_map_splay_merge_left(header, root, llist);
192985b7bedbSDoug Moore 	if (rlist == header) {
193085b7bedbSDoug Moore 		root->max_free = vm_size_max(max_free_left,
193185b7bedbSDoug Moore 		    vm_map_splay_merge_succ(header, root, rlist));
193285b7bedbSDoug Moore 	} else {
19335a0879daSDoug Moore 		y = rlist;
19349f701172SKonstantin Belousov 		rlist = y->left;
193585b7bedbSDoug Moore 		y->max_free = vm_size_max(
193685b7bedbSDoug Moore 		    vm_map_splay_merge_pred(root, y, root),
193785b7bedbSDoug Moore 		    vm_map_splay_merge_right(header, y, rlist));
193885b7bedbSDoug Moore 		root->max_free = vm_size_max(max_free_left, y->max_free);
19399f701172SKonstantin Belousov 	}
194085b7bedbSDoug Moore 	map->root = root;
19419f701172SKonstantin Belousov 	VM_MAP_ASSERT_CONSISTENT(map);
19429f701172SKonstantin Belousov 	return (root->end);
1943df8bae1dSRodney W. Grimes }
1944df8bae1dSRodney W. Grimes 
1945d239bd3cSKonstantin Belousov int
1946d239bd3cSKonstantin Belousov vm_map_fixed(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1947b8ca4ef2SAlan Cox     vm_offset_t start, vm_size_t length, vm_prot_t prot,
1948d239bd3cSKonstantin Belousov     vm_prot_t max, int cow)
1949d239bd3cSKonstantin Belousov {
1950b8ca4ef2SAlan Cox 	vm_offset_t end;
1951d239bd3cSKonstantin Belousov 	int result;
1952d239bd3cSKonstantin Belousov 
1953d239bd3cSKonstantin Belousov 	end = start + length;
19544648ba0aSKonstantin Belousov 	KASSERT((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0 ||
19554648ba0aSKonstantin Belousov 	    object == NULL,
19564648ba0aSKonstantin Belousov 	    ("vm_map_fixed: non-NULL backing object for stack"));
1957897d81a0SKonstantin Belousov 	vm_map_lock(map);
1958d239bd3cSKonstantin Belousov 	VM_MAP_RANGE_CHECK(map, start, end);
1959e8f77c20SKonstantin Belousov 	if ((cow & MAP_CHECK_EXCL) == 0) {
1960e8f77c20SKonstantin Belousov 		result = vm_map_delete(map, start, end);
1961e8f77c20SKonstantin Belousov 		if (result != KERN_SUCCESS)
1962e8f77c20SKonstantin Belousov 			goto out;
1963e8f77c20SKonstantin Belousov 	}
19644648ba0aSKonstantin Belousov 	if ((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) != 0) {
19654648ba0aSKonstantin Belousov 		result = vm_map_stack_locked(map, start, length, sgrowsiz,
19664648ba0aSKonstantin Belousov 		    prot, max, cow);
19674648ba0aSKonstantin Belousov 	} else {
19684648ba0aSKonstantin Belousov 		result = vm_map_insert(map, object, offset, start, end,
19694648ba0aSKonstantin Belousov 		    prot, max, cow);
19704648ba0aSKonstantin Belousov 	}
1971e8f77c20SKonstantin Belousov out:
1972d239bd3cSKonstantin Belousov 	vm_map_unlock(map);
1973d239bd3cSKonstantin Belousov 	return (result);
1974d239bd3cSKonstantin Belousov }
1975d239bd3cSKonstantin Belousov 
1976fa50a355SKonstantin Belousov static const int aslr_pages_rnd_64[2] = {0x1000, 0x10};
1977fa50a355SKonstantin Belousov static const int aslr_pages_rnd_32[2] = {0x100, 0x4};
1978fa50a355SKonstantin Belousov 
1979fa50a355SKonstantin Belousov static int cluster_anon = 1;
1980fa50a355SKonstantin Belousov SYSCTL_INT(_vm, OID_AUTO, cluster_anon, CTLFLAG_RW,
1981fa50a355SKonstantin Belousov     &cluster_anon, 0,
1982484e9d03SKonstantin Belousov     "Cluster anonymous mappings: 0 = no, 1 = yes if no hint, 2 = always");
1983484e9d03SKonstantin Belousov 
1984484e9d03SKonstantin Belousov static bool
1985484e9d03SKonstantin Belousov clustering_anon_allowed(vm_offset_t addr)
1986484e9d03SKonstantin Belousov {
1987484e9d03SKonstantin Belousov 
1988484e9d03SKonstantin Belousov 	switch (cluster_anon) {
1989484e9d03SKonstantin Belousov 	case 0:
1990484e9d03SKonstantin Belousov 		return (false);
1991484e9d03SKonstantin Belousov 	case 1:
1992484e9d03SKonstantin Belousov 		return (addr == 0);
1993484e9d03SKonstantin Belousov 	case 2:
1994484e9d03SKonstantin Belousov 	default:
1995484e9d03SKonstantin Belousov 		return (true);
1996484e9d03SKonstantin Belousov 	}
1997484e9d03SKonstantin Belousov }
1998fa50a355SKonstantin Belousov 
1999fa50a355SKonstantin Belousov static long aslr_restarts;
2000fa50a355SKonstantin Belousov SYSCTL_LONG(_vm, OID_AUTO, aslr_restarts, CTLFLAG_RD,
2001fa50a355SKonstantin Belousov     &aslr_restarts, 0,
2002fa50a355SKonstantin Belousov     "Number of aslr failures");
2003fa50a355SKonstantin Belousov 
2004df8bae1dSRodney W. Grimes /*
2005fec29688SAlan Cox  * Searches for the specified amount of free space in the given map with the
2006fec29688SAlan Cox  * specified alignment.  Performs an address-ordered, first-fit search from
2007fec29688SAlan Cox  * the given address "*addr", with an optional upper bound "max_addr".  If the
2008fec29688SAlan Cox  * parameter "alignment" is zero, then the alignment is computed from the
2009fec29688SAlan Cox  * given (object, offset) pair so as to enable the greatest possible use of
2010fec29688SAlan Cox  * superpage mappings.  Returns KERN_SUCCESS and the address of the free space
2011fec29688SAlan Cox  * in "*addr" if successful.  Otherwise, returns KERN_NO_SPACE.
2012fec29688SAlan Cox  *
2013fec29688SAlan Cox  * The map must be locked.  Initially, there must be at least "length" bytes
2014fec29688SAlan Cox  * of free space at the given address.
2015fec29688SAlan Cox  */
2016fec29688SAlan Cox static int
2017fec29688SAlan Cox vm_map_alignspace(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
2018fec29688SAlan Cox     vm_offset_t *addr, vm_size_t length, vm_offset_t max_addr,
2019fec29688SAlan Cox     vm_offset_t alignment)
2020fec29688SAlan Cox {
2021fec29688SAlan Cox 	vm_offset_t aligned_addr, free_addr;
2022fec29688SAlan Cox 
2023fec29688SAlan Cox 	VM_MAP_ASSERT_LOCKED(map);
2024fec29688SAlan Cox 	free_addr = *addr;
20259f701172SKonstantin Belousov 	KASSERT(free_addr == vm_map_findspace(map, free_addr, length),
2026e65d58a0SDoug Moore 	    ("caller failed to provide space %#jx at address %p",
2027e65d58a0SDoug Moore 	     (uintmax_t)length, (void *)free_addr));
2028fec29688SAlan Cox 	for (;;) {
2029fec29688SAlan Cox 		/*
2030fec29688SAlan Cox 		 * At the start of every iteration, the free space at address
2031fec29688SAlan Cox 		 * "*addr" is at least "length" bytes.
2032fec29688SAlan Cox 		 */
2033fec29688SAlan Cox 		if (alignment == 0)
2034fec29688SAlan Cox 			pmap_align_superpage(object, offset, addr, length);
2035*c606ab59SDoug Moore 		else
2036*c606ab59SDoug Moore 			*addr = roundup2(*addr, alignment);
2037fec29688SAlan Cox 		aligned_addr = *addr;
2038fec29688SAlan Cox 		if (aligned_addr == free_addr) {
2039fec29688SAlan Cox 			/*
2040fec29688SAlan Cox 			 * Alignment did not change "*addr", so "*addr" must
2041fec29688SAlan Cox 			 * still provide sufficient free space.
2042fec29688SAlan Cox 			 */
2043fec29688SAlan Cox 			return (KERN_SUCCESS);
2044fec29688SAlan Cox 		}
2045fec29688SAlan Cox 
2046fec29688SAlan Cox 		/*
2047fec29688SAlan Cox 		 * Test for address wrap on "*addr".  A wrapped "*addr" could
2048fec29688SAlan Cox 		 * be a valid address, in which case vm_map_findspace() cannot
2049fec29688SAlan Cox 		 * be relied upon to fail.
2050fec29688SAlan Cox 		 */
20519f701172SKonstantin Belousov 		if (aligned_addr < free_addr)
20529f701172SKonstantin Belousov 			return (KERN_NO_SPACE);
20539f701172SKonstantin Belousov 		*addr = vm_map_findspace(map, aligned_addr, length);
20549f701172SKonstantin Belousov 		if (*addr + length > vm_map_max(map) ||
2055fec29688SAlan Cox 		    (max_addr != 0 && *addr + length > max_addr))
2056fec29688SAlan Cox 			return (KERN_NO_SPACE);
2057fec29688SAlan Cox 		free_addr = *addr;
2058fec29688SAlan Cox 		if (free_addr == aligned_addr) {
2059fec29688SAlan Cox 			/*
2060fec29688SAlan Cox 			 * If a successful call to vm_map_findspace() did not
2061fec29688SAlan Cox 			 * change "*addr", then "*addr" must still be aligned
2062fec29688SAlan Cox 			 * and provide sufficient free space.
2063fec29688SAlan Cox 			 */
2064fec29688SAlan Cox 			return (KERN_SUCCESS);
2065fec29688SAlan Cox 		}
2066fec29688SAlan Cox 	}
2067fec29688SAlan Cox }
2068fec29688SAlan Cox 
20697a9f2da3SKonstantin Belousov int
20707a9f2da3SKonstantin Belousov vm_map_find_aligned(vm_map_t map, vm_offset_t *addr, vm_size_t length,
20717a9f2da3SKonstantin Belousov     vm_offset_t max_addr, vm_offset_t alignment)
20727a9f2da3SKonstantin Belousov {
20737a9f2da3SKonstantin Belousov 	/* XXXKIB ASLR eh ? */
20747a9f2da3SKonstantin Belousov 	*addr = vm_map_findspace(map, *addr, length);
20757a9f2da3SKonstantin Belousov 	if (*addr + length > vm_map_max(map) ||
20767a9f2da3SKonstantin Belousov 	    (max_addr != 0 && *addr + length > max_addr))
20777a9f2da3SKonstantin Belousov 		return (KERN_NO_SPACE);
20787a9f2da3SKonstantin Belousov 	return (vm_map_alignspace(map, NULL, 0, addr, length, max_addr,
20797a9f2da3SKonstantin Belousov 	    alignment));
20807a9f2da3SKonstantin Belousov }
20817a9f2da3SKonstantin Belousov 
2082fec29688SAlan Cox /*
2083df8bae1dSRodney W. Grimes  *	vm_map_find finds an unallocated region in the target address
2084df8bae1dSRodney W. Grimes  *	map with the given length.  The search is defined to be
2085df8bae1dSRodney W. Grimes  *	first-fit from the specified address; the region found is
2086df8bae1dSRodney W. Grimes  *	returned in the same parameter.
2087df8bae1dSRodney W. Grimes  *
20882aaeadf8SMatthew Dillon  *	If object is non-NULL, ref count must be bumped by caller
20892aaeadf8SMatthew Dillon  *	prior to making call to account for the new entry.
2090df8bae1dSRodney W. Grimes  */
2091df8bae1dSRodney W. Grimes int
2092b9dcd593SBruce Evans vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
2093b9dcd593SBruce Evans 	    vm_offset_t *addr,	/* IN/OUT */
2094edb572a3SJohn Baldwin 	    vm_size_t length, vm_offset_t max_addr, int find_space,
2095edb572a3SJohn Baldwin 	    vm_prot_t prot, vm_prot_t max, int cow)
2096df8bae1dSRodney W. Grimes {
2097fa50a355SKonstantin Belousov 	vm_offset_t alignment, curr_min_addr, min_addr;
2098fa50a355SKonstantin Belousov 	int gap, pidx, rv, try;
2099fa50a355SKonstantin Belousov 	bool cluster, en_aslr, update_anon;
2100df8bae1dSRodney W. Grimes 
21014648ba0aSKonstantin Belousov 	KASSERT((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0 ||
21024648ba0aSKonstantin Belousov 	    object == NULL,
21034648ba0aSKonstantin Belousov 	    ("vm_map_find: non-NULL backing object for stack"));
2104ea7e7006SKonstantin Belousov 	MPASS((cow & MAP_REMAP) == 0 || (find_space == VMFS_NO_SPACE &&
2105ea7e7006SKonstantin Belousov 	    (cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0));
2106ff74a3faSJohn Baldwin 	if (find_space == VMFS_OPTIMAL_SPACE && (object == NULL ||
2107ff74a3faSJohn Baldwin 	    (object->flags & OBJ_COLORED) == 0))
2108ff74a3faSJohn Baldwin 		find_space = VMFS_ANY_SPACE;
21095aa60b6fSJohn Baldwin 	if (find_space >> 8 != 0) {
21105aa60b6fSJohn Baldwin 		KASSERT((find_space & 0xff) == 0, ("bad VMFS flags"));
21115aa60b6fSJohn Baldwin 		alignment = (vm_offset_t)1 << (find_space >> 8);
21125aa60b6fSJohn Baldwin 	} else
21135aa60b6fSJohn Baldwin 		alignment = 0;
2114fa50a355SKonstantin Belousov 	en_aslr = (map->flags & MAP_ASLR) != 0;
2115484e9d03SKonstantin Belousov 	update_anon = cluster = clustering_anon_allowed(*addr) &&
2116fa50a355SKonstantin Belousov 	    (map->flags & MAP_IS_SUB_MAP) == 0 && max_addr == 0 &&
2117fa50a355SKonstantin Belousov 	    find_space != VMFS_NO_SPACE && object == NULL &&
2118fa50a355SKonstantin Belousov 	    (cow & (MAP_INHERIT_SHARE | MAP_STACK_GROWS_UP |
2119fa50a355SKonstantin Belousov 	    MAP_STACK_GROWS_DOWN)) == 0 && prot != PROT_NONE;
2120fa50a355SKonstantin Belousov 	curr_min_addr = min_addr = *addr;
2121fa50a355SKonstantin Belousov 	if (en_aslr && min_addr == 0 && !cluster &&
2122fa50a355SKonstantin Belousov 	    find_space != VMFS_NO_SPACE &&
2123fa50a355SKonstantin Belousov 	    (map->flags & MAP_ASLR_IGNSTART) != 0)
2124fa50a355SKonstantin Belousov 		curr_min_addr = min_addr = vm_map_min(map);
2125fa50a355SKonstantin Belousov 	try = 0;
21264d572bb3SAlan Cox 	vm_map_lock(map);
2127fa50a355SKonstantin Belousov 	if (cluster) {
2128fa50a355SKonstantin Belousov 		curr_min_addr = map->anon_loc;
2129fa50a355SKonstantin Belousov 		if (curr_min_addr == 0)
2130fa50a355SKonstantin Belousov 			cluster = false;
2131fa50a355SKonstantin Belousov 	}
213226c538ffSAlan Cox 	if (find_space != VMFS_NO_SPACE) {
2133fec29688SAlan Cox 		KASSERT(find_space == VMFS_ANY_SPACE ||
2134fec29688SAlan Cox 		    find_space == VMFS_OPTIMAL_SPACE ||
2135fec29688SAlan Cox 		    find_space == VMFS_SUPER_SPACE ||
2136fec29688SAlan Cox 		    alignment != 0, ("unexpected VMFS flag"));
2137fec29688SAlan Cox again:
2138fa50a355SKonstantin Belousov 		/*
2139fa50a355SKonstantin Belousov 		 * When creating an anonymous mapping, try clustering
2140fa50a355SKonstantin Belousov 		 * with an existing anonymous mapping first.
2141fa50a355SKonstantin Belousov 		 *
2142fa50a355SKonstantin Belousov 		 * We make up to two attempts to find address space
2143fa50a355SKonstantin Belousov 		 * for a given find_space value. The first attempt may
2144fa50a355SKonstantin Belousov 		 * apply randomization or may cluster with an existing
2145fa50a355SKonstantin Belousov 		 * anonymous mapping. If this first attempt fails,
2146fa50a355SKonstantin Belousov 		 * perform a first-fit search of the available address
2147fa50a355SKonstantin Belousov 		 * space.
2148fa50a355SKonstantin Belousov 		 *
2149fa50a355SKonstantin Belousov 		 * If all tries failed, and find_space is
2150fa50a355SKonstantin Belousov 		 * VMFS_OPTIMAL_SPACE, fallback to VMFS_ANY_SPACE.
2151fa50a355SKonstantin Belousov 		 * Again enable clustering and randomization.
2152fa50a355SKonstantin Belousov 		 */
2153fa50a355SKonstantin Belousov 		try++;
2154fa50a355SKonstantin Belousov 		MPASS(try <= 2);
2155fa50a355SKonstantin Belousov 
2156fa50a355SKonstantin Belousov 		if (try == 2) {
2157fa50a355SKonstantin Belousov 			/*
2158fa50a355SKonstantin Belousov 			 * Second try: we failed either to find a
2159fa50a355SKonstantin Belousov 			 * suitable region for randomizing the
2160fa50a355SKonstantin Belousov 			 * allocation, or to cluster with an existing
2161fa50a355SKonstantin Belousov 			 * mapping.  Retry with free run.
2162fa50a355SKonstantin Belousov 			 */
2163fa50a355SKonstantin Belousov 			curr_min_addr = (map->flags & MAP_ASLR_IGNSTART) != 0 ?
2164fa50a355SKonstantin Belousov 			    vm_map_min(map) : min_addr;
2165fa50a355SKonstantin Belousov 			atomic_add_long(&aslr_restarts, 1);
2166fa50a355SKonstantin Belousov 		}
2167fa50a355SKonstantin Belousov 
2168fa50a355SKonstantin Belousov 		if (try == 1 && en_aslr && !cluster) {
2169fa50a355SKonstantin Belousov 			/*
2170fa50a355SKonstantin Belousov 			 * Find space for allocation, including
2171fa50a355SKonstantin Belousov 			 * gap needed for later randomization.
2172fa50a355SKonstantin Belousov 			 */
2173fa50a355SKonstantin Belousov 			pidx = MAXPAGESIZES > 1 && pagesizes[1] != 0 &&
2174fa50a355SKonstantin Belousov 			    (find_space == VMFS_SUPER_SPACE || find_space ==
2175fa50a355SKonstantin Belousov 			    VMFS_OPTIMAL_SPACE) ? 1 : 0;
2176fa50a355SKonstantin Belousov 			gap = vm_map_max(map) > MAP_32BIT_MAX_ADDR &&
2177fa50a355SKonstantin Belousov 			    (max_addr == 0 || max_addr > MAP_32BIT_MAX_ADDR) ?
2178fa50a355SKonstantin Belousov 			    aslr_pages_rnd_64[pidx] : aslr_pages_rnd_32[pidx];
21799f701172SKonstantin Belousov 			*addr = vm_map_findspace(map, curr_min_addr,
21809f701172SKonstantin Belousov 			    length + gap * pagesizes[pidx]);
21819f701172SKonstantin Belousov 			if (*addr + length + gap * pagesizes[pidx] >
2182a5a02ef4SKonstantin Belousov 			    vm_map_max(map))
2183fa50a355SKonstantin Belousov 				goto again;
2184fa50a355SKonstantin Belousov 			/* And randomize the start address. */
2185fa50a355SKonstantin Belousov 			*addr += (arc4random() % gap) * pagesizes[pidx];
21865019dac9SKonstantin Belousov 			if (max_addr != 0 && *addr + length > max_addr)
21875019dac9SKonstantin Belousov 				goto again;
21889f701172SKonstantin Belousov 		} else {
21899f701172SKonstantin Belousov 			*addr = vm_map_findspace(map, curr_min_addr, length);
21909f701172SKonstantin Belousov 			if (*addr + length > vm_map_max(map) ||
2191edb572a3SJohn Baldwin 			    (max_addr != 0 && *addr + length > max_addr)) {
2192fa50a355SKonstantin Belousov 				if (cluster) {
2193fa50a355SKonstantin Belousov 					cluster = false;
2194fa50a355SKonstantin Belousov 					MPASS(try == 1);
2195fa50a355SKonstantin Belousov 					goto again;
2196fa50a355SKonstantin Belousov 				}
2197fec29688SAlan Cox 				rv = KERN_NO_SPACE;
2198fec29688SAlan Cox 				goto done;
2199fec29688SAlan Cox 			}
22009f701172SKonstantin Belousov 		}
2201fa50a355SKonstantin Belousov 
2202fec29688SAlan Cox 		if (find_space != VMFS_ANY_SPACE &&
2203fec29688SAlan Cox 		    (rv = vm_map_alignspace(map, object, offset, addr, length,
2204fec29688SAlan Cox 		    max_addr, alignment)) != KERN_SUCCESS) {
2205ff74a3faSJohn Baldwin 			if (find_space == VMFS_OPTIMAL_SPACE) {
2206ff74a3faSJohn Baldwin 				find_space = VMFS_ANY_SPACE;
2207fa50a355SKonstantin Belousov 				curr_min_addr = min_addr;
2208fa50a355SKonstantin Belousov 				cluster = update_anon;
2209fa50a355SKonstantin Belousov 				try = 0;
2210ff74a3faSJohn Baldwin 				goto again;
2211ff74a3faSJohn Baldwin 			}
2212fec29688SAlan Cox 			goto done;
2213df8bae1dSRodney W. Grimes 		}
2214ea7e7006SKonstantin Belousov 	} else if ((cow & MAP_REMAP) != 0) {
22150f1e6ec5SMark Johnston 		if (!vm_map_range_valid(map, *addr, *addr + length)) {
2216ea7e7006SKonstantin Belousov 			rv = KERN_INVALID_ADDRESS;
2217ea7e7006SKonstantin Belousov 			goto done;
2218ea7e7006SKonstantin Belousov 		}
2219e8f77c20SKonstantin Belousov 		rv = vm_map_delete(map, *addr, *addr + length);
2220e8f77c20SKonstantin Belousov 		if (rv != KERN_SUCCESS)
2221e8f77c20SKonstantin Belousov 			goto done;
2222df8bae1dSRodney W. Grimes 	}
22234648ba0aSKonstantin Belousov 	if ((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) != 0) {
2224fec29688SAlan Cox 		rv = vm_map_stack_locked(map, *addr, length, sgrowsiz, prot,
2225fec29688SAlan Cox 		    max, cow);
22264648ba0aSKonstantin Belousov 	} else {
2227fec29688SAlan Cox 		rv = vm_map_insert(map, object, offset, *addr, *addr + length,
2228fec29688SAlan Cox 		    prot, max, cow);
22294648ba0aSKonstantin Belousov 	}
2230fa50a355SKonstantin Belousov 	if (rv == KERN_SUCCESS && update_anon)
2231fa50a355SKonstantin Belousov 		map->anon_loc = *addr + length;
2232fec29688SAlan Cox done:
2233df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
2234fec29688SAlan Cox 	return (rv);
2235df8bae1dSRodney W. Grimes }
2236df8bae1dSRodney W. Grimes 
2237e8502826SKonstantin Belousov /*
2238e8502826SKonstantin Belousov  *	vm_map_find_min() is a variant of vm_map_find() that takes an
2239e8502826SKonstantin Belousov  *	additional parameter (min_addr) and treats the given address
2240e8502826SKonstantin Belousov  *	(*addr) differently.  Specifically, it treats *addr as a hint
2241e8502826SKonstantin Belousov  *	and not as the minimum address where the mapping is created.
2242e8502826SKonstantin Belousov  *
2243e8502826SKonstantin Belousov  *	This function works in two phases.  First, it tries to
2244e8502826SKonstantin Belousov  *	allocate above the hint.  If that fails and the hint is
2245e8502826SKonstantin Belousov  *	greater than min_addr, it performs a second pass, replacing
2246e8502826SKonstantin Belousov  *	the hint with min_addr as the minimum address for the
2247e8502826SKonstantin Belousov  *	allocation.
2248e8502826SKonstantin Belousov  */
22496a97a3f7SKonstantin Belousov int
22506a97a3f7SKonstantin Belousov vm_map_find_min(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
22516a97a3f7SKonstantin Belousov     vm_offset_t *addr, vm_size_t length, vm_offset_t min_addr,
22526a97a3f7SKonstantin Belousov     vm_offset_t max_addr, int find_space, vm_prot_t prot, vm_prot_t max,
22536a97a3f7SKonstantin Belousov     int cow)
22546a97a3f7SKonstantin Belousov {
22556a97a3f7SKonstantin Belousov 	vm_offset_t hint;
22566a97a3f7SKonstantin Belousov 	int rv;
22576a97a3f7SKonstantin Belousov 
22586a97a3f7SKonstantin Belousov 	hint = *addr;
22596a97a3f7SKonstantin Belousov 	for (;;) {
22606a97a3f7SKonstantin Belousov 		rv = vm_map_find(map, object, offset, addr, length, max_addr,
22616a97a3f7SKonstantin Belousov 		    find_space, prot, max, cow);
22626a97a3f7SKonstantin Belousov 		if (rv == KERN_SUCCESS || min_addr >= hint)
22636a97a3f7SKonstantin Belousov 			return (rv);
22647683ad70SKonstantin Belousov 		*addr = hint = min_addr;
22656a97a3f7SKonstantin Belousov 	}
22666a97a3f7SKonstantin Belousov }
22676a97a3f7SKonstantin Belousov 
226892e78c10SAlan Cox /*
226992e78c10SAlan Cox  * A map entry with any of the following flags set must not be merged with
227092e78c10SAlan Cox  * another entry.
227192e78c10SAlan Cox  */
227292e78c10SAlan Cox #define	MAP_ENTRY_NOMERGE_MASK	(MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP | \
227378022527SKonstantin Belousov 	    MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP | MAP_ENTRY_VN_EXEC)
227492e78c10SAlan Cox 
227507424462SKonstantin Belousov static bool
227607424462SKonstantin Belousov vm_map_mergeable_neighbors(vm_map_entry_t prev, vm_map_entry_t entry)
227707424462SKonstantin Belousov {
227807424462SKonstantin Belousov 
227992e78c10SAlan Cox 	KASSERT((prev->eflags & MAP_ENTRY_NOMERGE_MASK) == 0 ||
228092e78c10SAlan Cox 	    (entry->eflags & MAP_ENTRY_NOMERGE_MASK) == 0,
228192e78c10SAlan Cox 	    ("vm_map_mergeable_neighbors: neither %p nor %p are mergeable",
228292e78c10SAlan Cox 	    prev, entry));
228307424462SKonstantin Belousov 	return (prev->end == entry->start &&
228407424462SKonstantin Belousov 	    prev->object.vm_object == entry->object.vm_object &&
228507424462SKonstantin Belousov 	    (prev->object.vm_object == NULL ||
228692e78c10SAlan Cox 	    prev->offset + (prev->end - prev->start) == entry->offset) &&
228707424462SKonstantin Belousov 	    prev->eflags == entry->eflags &&
228807424462SKonstantin Belousov 	    prev->protection == entry->protection &&
228907424462SKonstantin Belousov 	    prev->max_protection == entry->max_protection &&
229007424462SKonstantin Belousov 	    prev->inheritance == entry->inheritance &&
229107424462SKonstantin Belousov 	    prev->wired_count == entry->wired_count &&
229207424462SKonstantin Belousov 	    prev->cred == entry->cred);
229307424462SKonstantin Belousov }
229407424462SKonstantin Belousov 
229507424462SKonstantin Belousov static void
229607424462SKonstantin Belousov vm_map_merged_neighbor_dispose(vm_map_t map, vm_map_entry_t entry)
229707424462SKonstantin Belousov {
229807424462SKonstantin Belousov 
229907424462SKonstantin Belousov 	/*
230092e78c10SAlan Cox 	 * If the backing object is a vnode object, vm_object_deallocate()
230192e78c10SAlan Cox 	 * calls vrele().  However, vrele() does not lock the vnode because
230292e78c10SAlan Cox 	 * the vnode has additional references.  Thus, the map lock can be
230392e78c10SAlan Cox 	 * kept without causing a lock-order reversal with the vnode lock.
230407424462SKonstantin Belousov 	 *
230592e78c10SAlan Cox 	 * Since we count the number of virtual page mappings in
230692e78c10SAlan Cox 	 * object->un_pager.vnp.writemappings, the writemappings value
230792e78c10SAlan Cox 	 * should not be adjusted when the entry is disposed of.
230807424462SKonstantin Belousov 	 */
230907424462SKonstantin Belousov 	if (entry->object.vm_object != NULL)
231007424462SKonstantin Belousov 		vm_object_deallocate(entry->object.vm_object);
231107424462SKonstantin Belousov 	if (entry->cred != NULL)
231207424462SKonstantin Belousov 		crfree(entry->cred);
231307424462SKonstantin Belousov 	vm_map_entry_dispose(map, entry);
231407424462SKonstantin Belousov }
231507424462SKonstantin Belousov 
2316df8bae1dSRodney W. Grimes /*
231783ea714fSDoug Moore  *	vm_map_try_merge_entries:
231867bf6868SJohn Dyson  *
231983ea714fSDoug Moore  *	Compare the given map entry to its predecessor, and merge its precessor
232083ea714fSDoug Moore  *	into it if possible.  The entry remains valid, and may be extended.
232183ea714fSDoug Moore  *	The predecessor may be deleted.
23224e71e795SMatthew Dillon  *
23234e71e795SMatthew Dillon  *	The map must be locked.
2324df8bae1dSRodney W. Grimes  */
23250afcd3afSAlan Cox void
23262767c9f3SDoug Moore vm_map_try_merge_entries(vm_map_t map, vm_map_entry_t prev_entry,
23272767c9f3SDoug Moore     vm_map_entry_t entry)
2328df8bae1dSRodney W. Grimes {
2329df8bae1dSRodney W. Grimes 
233083ea714fSDoug Moore 	VM_MAP_ASSERT_LOCKED(map);
233183ea714fSDoug Moore 	if ((entry->eflags & MAP_ENTRY_NOMERGE_MASK) == 0 &&
23322767c9f3SDoug Moore 	    vm_map_mergeable_neighbors(prev_entry, entry)) {
23332767c9f3SDoug Moore 		vm_map_entry_unlink(map, prev_entry, UNLINK_MERGE_NEXT);
23342767c9f3SDoug Moore 		vm_map_merged_neighbor_dispose(map, prev_entry);
2335308c24baSJohn Dyson 	}
2336df8bae1dSRodney W. Grimes }
233792e78c10SAlan Cox 
2338df8bae1dSRodney W. Grimes /*
2339af1d6d6aSDoug Moore  *	vm_map_entry_back:
2340af1d6d6aSDoug Moore  *
2341af1d6d6aSDoug Moore  *	Allocate an object to back a map entry.
2342af1d6d6aSDoug Moore  */
2343af1d6d6aSDoug Moore static inline void
2344af1d6d6aSDoug Moore vm_map_entry_back(vm_map_entry_t entry)
2345af1d6d6aSDoug Moore {
2346af1d6d6aSDoug Moore 	vm_object_t object;
2347af1d6d6aSDoug Moore 
2348af1d6d6aSDoug Moore 	KASSERT(entry->object.vm_object == NULL,
2349af1d6d6aSDoug Moore 	    ("map entry %p has backing object", entry));
2350af1d6d6aSDoug Moore 	KASSERT((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0,
2351af1d6d6aSDoug Moore 	    ("map entry %p is a submap", entry));
235267388836SKonstantin Belousov 	object = vm_object_allocate_anon(atop(entry->end - entry->start), NULL,
235367388836SKonstantin Belousov 	    entry->cred, entry->end - entry->start);
2354af1d6d6aSDoug Moore 	entry->object.vm_object = object;
2355af1d6d6aSDoug Moore 	entry->offset = 0;
2356af1d6d6aSDoug Moore 	entry->cred = NULL;
2357af1d6d6aSDoug Moore }
2358af1d6d6aSDoug Moore 
2359af1d6d6aSDoug Moore /*
2360af1d6d6aSDoug Moore  *	vm_map_entry_charge_object
2361af1d6d6aSDoug Moore  *
2362af1d6d6aSDoug Moore  *	If there is no object backing this entry, create one.  Otherwise, if
2363af1d6d6aSDoug Moore  *	the entry has cred, give it to the backing object.
2364af1d6d6aSDoug Moore  */
2365af1d6d6aSDoug Moore static inline void
2366af1d6d6aSDoug Moore vm_map_entry_charge_object(vm_map_t map, vm_map_entry_t entry)
2367af1d6d6aSDoug Moore {
2368af1d6d6aSDoug Moore 
2369af1d6d6aSDoug Moore 	VM_MAP_ASSERT_LOCKED(map);
2370af1d6d6aSDoug Moore 	KASSERT((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0,
2371af1d6d6aSDoug Moore 	    ("map entry %p is a submap", entry));
2372af1d6d6aSDoug Moore 	if (entry->object.vm_object == NULL && !map->system_map &&
2373af1d6d6aSDoug Moore 	    (entry->eflags & MAP_ENTRY_GUARD) == 0)
2374af1d6d6aSDoug Moore 		vm_map_entry_back(entry);
2375af1d6d6aSDoug Moore 	else if (entry->object.vm_object != NULL &&
2376af1d6d6aSDoug Moore 	    ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) &&
2377af1d6d6aSDoug Moore 	    entry->cred != NULL) {
2378af1d6d6aSDoug Moore 		VM_OBJECT_WLOCK(entry->object.vm_object);
2379af1d6d6aSDoug Moore 		KASSERT(entry->object.vm_object->cred == NULL,
2380af1d6d6aSDoug Moore 		    ("OVERCOMMIT: %s: both cred e %p", __func__, entry));
2381af1d6d6aSDoug Moore 		entry->object.vm_object->cred = entry->cred;
2382af1d6d6aSDoug Moore 		entry->object.vm_object->charge = entry->end - entry->start;
2383af1d6d6aSDoug Moore 		VM_OBJECT_WUNLOCK(entry->object.vm_object);
2384af1d6d6aSDoug Moore 		entry->cred = NULL;
2385af1d6d6aSDoug Moore 	}
2386af1d6d6aSDoug Moore }
2387af1d6d6aSDoug Moore 
2388af1d6d6aSDoug Moore /*
2389037c0994SDoug Moore  *	vm_map_entry_clone
2390037c0994SDoug Moore  *
2391037c0994SDoug Moore  *	Create a duplicate map entry for clipping.
2392037c0994SDoug Moore  */
2393037c0994SDoug Moore static vm_map_entry_t
2394037c0994SDoug Moore vm_map_entry_clone(vm_map_t map, vm_map_entry_t entry)
2395037c0994SDoug Moore {
2396037c0994SDoug Moore 	vm_map_entry_t new_entry;
2397037c0994SDoug Moore 
2398037c0994SDoug Moore 	VM_MAP_ASSERT_LOCKED(map);
2399037c0994SDoug Moore 
2400037c0994SDoug Moore 	/*
2401037c0994SDoug Moore 	 * Create a backing object now, if none exists, so that more individual
2402037c0994SDoug Moore 	 * objects won't be created after the map entry is split.
2403037c0994SDoug Moore 	 */
2404037c0994SDoug Moore 	vm_map_entry_charge_object(map, entry);
2405037c0994SDoug Moore 
2406037c0994SDoug Moore 	/* Clone the entry. */
2407037c0994SDoug Moore 	new_entry = vm_map_entry_create(map);
2408037c0994SDoug Moore 	*new_entry = *entry;
2409037c0994SDoug Moore 	if (new_entry->cred != NULL)
2410037c0994SDoug Moore 		crhold(entry->cred);
2411037c0994SDoug Moore 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
2412037c0994SDoug Moore 		vm_object_reference(new_entry->object.vm_object);
2413037c0994SDoug Moore 		vm_map_entry_set_vnode_text(new_entry, true);
2414037c0994SDoug Moore 		/*
2415037c0994SDoug Moore 		 * The object->un_pager.vnp.writemappings for the object of
2416037c0994SDoug Moore 		 * MAP_ENTRY_WRITECNT type entry shall be kept as is here.  The
2417037c0994SDoug Moore 		 * virtual pages are re-distributed among the clipped entries,
2418037c0994SDoug Moore 		 * so the sum is left the same.
2419037c0994SDoug Moore 		 */
2420037c0994SDoug Moore 	}
2421037c0994SDoug Moore 	return (new_entry);
2422037c0994SDoug Moore }
2423037c0994SDoug Moore 
2424037c0994SDoug Moore /*
2425df8bae1dSRodney W. Grimes  *	vm_map_clip_start:	[ internal use only ]
2426df8bae1dSRodney W. Grimes  *
2427df8bae1dSRodney W. Grimes  *	Asserts that the given entry begins at or after
2428df8bae1dSRodney W. Grimes  *	the specified address; if necessary,
2429df8bae1dSRodney W. Grimes  *	it splits the entry into two.
2430df8bae1dSRodney W. Grimes  */
2431e2e80fb3SKonstantin Belousov static int
2432e2e80fb3SKonstantin Belousov vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t startaddr)
2433df8bae1dSRodney W. Grimes {
2434c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
2435e2e80fb3SKonstantin Belousov 	int bdry_idx;
2436df8bae1dSRodney W. Grimes 
24378a64110eSConrad Meyer 	if (!map->system_map)
24388a64110eSConrad Meyer 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
24398a64110eSConrad Meyer 		    "%s: map %p entry %p start 0x%jx", __func__, map, entry,
2440e2e80fb3SKonstantin Belousov 		    (uintmax_t)startaddr);
24418a64110eSConrad Meyer 
2442e2e80fb3SKonstantin Belousov 	if (startaddr <= entry->start)
2443e2e80fb3SKonstantin Belousov 		return (KERN_SUCCESS);
2444a116b5d3SConrad Meyer 
24453a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
2446e2e80fb3SKonstantin Belousov 	KASSERT(entry->end > startaddr && entry->start < startaddr,
2447a116b5d3SConrad Meyer 	    ("%s: invalid clip of entry %p", __func__, entry));
24483a0916b8SKonstantin Belousov 
2449e2e80fb3SKonstantin Belousov 	bdry_idx = (entry->eflags & MAP_ENTRY_SPLIT_BOUNDARY_MASK) >>
2450e2e80fb3SKonstantin Belousov 	    MAP_ENTRY_SPLIT_BOUNDARY_SHIFT;
2451e2e80fb3SKonstantin Belousov 	if (bdry_idx != 0) {
2452e2e80fb3SKonstantin Belousov 		if ((startaddr & (pagesizes[bdry_idx] - 1)) != 0)
2453e2e80fb3SKonstantin Belousov 			return (KERN_INVALID_ARGUMENT);
2454e2e80fb3SKonstantin Belousov 	}
2455e2e80fb3SKonstantin Belousov 
2456037c0994SDoug Moore 	new_entry = vm_map_entry_clone(map, entry);
2457df8bae1dSRodney W. Grimes 
24584766eba1SDoug Moore 	/*
24594766eba1SDoug Moore 	 * Split off the front portion.  Insert the new entry BEFORE this one,
24604766eba1SDoug Moore 	 * so that this entry has the specified starting address.
24614766eba1SDoug Moore 	 */
2462e2e80fb3SKonstantin Belousov 	new_entry->end = startaddr;
24639f701172SKonstantin Belousov 	vm_map_entry_link(map, new_entry);
2464e2e80fb3SKonstantin Belousov 	return (KERN_SUCCESS);
2465c0877f10SJohn Dyson }
2466df8bae1dSRodney W. Grimes 
2467df8bae1dSRodney W. Grimes /*
2468c7b23459SDoug Moore  *	vm_map_lookup_clip_start:
2469c7b23459SDoug Moore  *
2470c7b23459SDoug Moore  *	Find the entry at or just after 'start', and clip it if 'start' is in
2471c7b23459SDoug Moore  *	the interior of the entry.  Return entry after 'start', and in
2472c7b23459SDoug Moore  *	prev_entry set the entry before 'start'.
2473c7b23459SDoug Moore  */
2474e2e80fb3SKonstantin Belousov static int
2475c7b23459SDoug Moore vm_map_lookup_clip_start(vm_map_t map, vm_offset_t start,
2476e2e80fb3SKonstantin Belousov     vm_map_entry_t *res_entry, vm_map_entry_t *prev_entry)
2477c7b23459SDoug Moore {
2478c7b23459SDoug Moore 	vm_map_entry_t entry;
2479e2e80fb3SKonstantin Belousov 	int rv;
2480c7b23459SDoug Moore 
24818a64110eSConrad Meyer 	if (!map->system_map)
24828a64110eSConrad Meyer 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
24838a64110eSConrad Meyer 		    "%s: map %p start 0x%jx prev %p", __func__, map,
24848a64110eSConrad Meyer 		    (uintmax_t)start, prev_entry);
24858a64110eSConrad Meyer 
2486c7b23459SDoug Moore 	if (vm_map_lookup_entry(map, start, prev_entry)) {
2487c7b23459SDoug Moore 		entry = *prev_entry;
2488e2e80fb3SKonstantin Belousov 		rv = vm_map_clip_start(map, entry, start);
2489e2e80fb3SKonstantin Belousov 		if (rv != KERN_SUCCESS)
2490e2e80fb3SKonstantin Belousov 			return (rv);
2491c7b23459SDoug Moore 		*prev_entry = vm_map_entry_pred(entry);
2492c7b23459SDoug Moore 	} else
2493c7b23459SDoug Moore 		entry = vm_map_entry_succ(*prev_entry);
2494e2e80fb3SKonstantin Belousov 	*res_entry = entry;
2495e2e80fb3SKonstantin Belousov 	return (KERN_SUCCESS);
2496c7b23459SDoug Moore }
2497c7b23459SDoug Moore 
2498c7b23459SDoug Moore /*
2499df8bae1dSRodney W. Grimes  *	vm_map_clip_end:	[ internal use only ]
2500df8bae1dSRodney W. Grimes  *
2501df8bae1dSRodney W. Grimes  *	Asserts that the given entry ends at or before
2502df8bae1dSRodney W. Grimes  *	the specified address; if necessary,
2503df8bae1dSRodney W. Grimes  *	it splits the entry into two.
2504df8bae1dSRodney W. Grimes  */
2505e2e80fb3SKonstantin Belousov static int
2506e2e80fb3SKonstantin Belousov vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t endaddr)
2507df8bae1dSRodney W. Grimes {
2508c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
2509e2e80fb3SKonstantin Belousov 	int bdry_idx;
2510df8bae1dSRodney W. Grimes 
25118a64110eSConrad Meyer 	if (!map->system_map)
25128a64110eSConrad Meyer 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
25138a64110eSConrad Meyer 		    "%s: map %p entry %p end 0x%jx", __func__, map, entry,
2514e2e80fb3SKonstantin Belousov 		    (uintmax_t)endaddr);
25158a64110eSConrad Meyer 
2516e2e80fb3SKonstantin Belousov 	if (endaddr >= entry->end)
2517e2e80fb3SKonstantin Belousov 		return (KERN_SUCCESS);
2518a116b5d3SConrad Meyer 
25193a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
2520e2e80fb3SKonstantin Belousov 	KASSERT(entry->start < endaddr && entry->end > endaddr,
2521a116b5d3SConrad Meyer 	    ("%s: invalid clip of entry %p", __func__, entry));
25223a0916b8SKonstantin Belousov 
2523e2e80fb3SKonstantin Belousov 	bdry_idx = (entry->eflags & MAP_ENTRY_SPLIT_BOUNDARY_MASK) >>
2524e2e80fb3SKonstantin Belousov 	    MAP_ENTRY_SPLIT_BOUNDARY_SHIFT;
2525e2e80fb3SKonstantin Belousov 	if (bdry_idx != 0) {
2526e2e80fb3SKonstantin Belousov 		if ((endaddr & (pagesizes[bdry_idx] - 1)) != 0)
2527e2e80fb3SKonstantin Belousov 			return (KERN_INVALID_ARGUMENT);
2528e2e80fb3SKonstantin Belousov 	}
2529e2e80fb3SKonstantin Belousov 
2530037c0994SDoug Moore 	new_entry = vm_map_entry_clone(map, entry);
2531df8bae1dSRodney W. Grimes 
25324766eba1SDoug Moore 	/*
25334766eba1SDoug Moore 	 * Split off the back portion.  Insert the new entry AFTER this one,
25344766eba1SDoug Moore 	 * so that this entry has the specified ending address.
25354766eba1SDoug Moore 	 */
2536e2e80fb3SKonstantin Belousov 	new_entry->start = endaddr;
25379f701172SKonstantin Belousov 	vm_map_entry_link(map, new_entry);
2538e2e80fb3SKonstantin Belousov 
2539e2e80fb3SKonstantin Belousov 	return (KERN_SUCCESS);
2540c0877f10SJohn Dyson }
2541df8bae1dSRodney W. Grimes 
2542df8bae1dSRodney W. Grimes /*
2543df8bae1dSRodney W. Grimes  *	vm_map_submap:		[ kernel use only ]
2544df8bae1dSRodney W. Grimes  *
2545df8bae1dSRodney W. Grimes  *	Mark the given range as handled by a subordinate map.
2546df8bae1dSRodney W. Grimes  *
2547df8bae1dSRodney W. Grimes  *	This range must have been created with vm_map_find,
2548df8bae1dSRodney W. Grimes  *	and no other operations may have been performed on this
2549df8bae1dSRodney W. Grimes  *	range prior to calling vm_map_submap.
2550df8bae1dSRodney W. Grimes  *
2551df8bae1dSRodney W. Grimes  *	Only a limited number of operations can be performed
2552df8bae1dSRodney W. Grimes  *	within this rage after calling vm_map_submap:
2553df8bae1dSRodney W. Grimes  *		vm_fault
2554df8bae1dSRodney W. Grimes  *	[Don't try vm_map_copy!]
2555df8bae1dSRodney W. Grimes  *
2556df8bae1dSRodney W. Grimes  *	To remove a submapping, one must first remove the
2557df8bae1dSRodney W. Grimes  *	range from the superior map, and then destroy the
2558df8bae1dSRodney W. Grimes  *	submap (if desired).  [Better yet, don't try it.]
2559df8bae1dSRodney W. Grimes  */
2560df8bae1dSRodney W. Grimes int
25611b40f8c0SMatthew Dillon vm_map_submap(
25621b40f8c0SMatthew Dillon 	vm_map_t map,
25631b40f8c0SMatthew Dillon 	vm_offset_t start,
25641b40f8c0SMatthew Dillon 	vm_offset_t end,
25651b40f8c0SMatthew Dillon 	vm_map_t submap)
2566df8bae1dSRodney W. Grimes {
2567df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
2568fa50a355SKonstantin Belousov 	int result;
2569fa50a355SKonstantin Belousov 
2570fa50a355SKonstantin Belousov 	result = KERN_INVALID_ARGUMENT;
2571fa50a355SKonstantin Belousov 
2572fa50a355SKonstantin Belousov 	vm_map_lock(submap);
2573fa50a355SKonstantin Belousov 	submap->flags |= MAP_IS_SUB_MAP;
2574fa50a355SKonstantin Belousov 	vm_map_unlock(submap);
2575df8bae1dSRodney W. Grimes 
2576df8bae1dSRodney W. Grimes 	vm_map_lock(map);
2577df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
2578e6bd3a81SMark Johnston 	if (vm_map_lookup_entry(map, start, &entry) && entry->end >= end &&
2579e6bd3a81SMark Johnston 	    (entry->eflags & MAP_ENTRY_COW) == 0 &&
2580e6bd3a81SMark Johnston 	    entry->object.vm_object == NULL) {
2581e2e80fb3SKonstantin Belousov 		result = vm_map_clip_start(map, entry, start);
2582e2e80fb3SKonstantin Belousov 		if (result != KERN_SUCCESS)
2583e2e80fb3SKonstantin Belousov 			goto unlock;
2584e2e80fb3SKonstantin Belousov 		result = vm_map_clip_end(map, entry, end);
2585e2e80fb3SKonstantin Belousov 		if (result != KERN_SUCCESS)
2586e2e80fb3SKonstantin Belousov 			goto unlock;
25872d8acc0fSJohn Dyson 		entry->object.sub_map = submap;
2588afa07f7eSJohn Dyson 		entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
2589df8bae1dSRodney W. Grimes 		result = KERN_SUCCESS;
2590df8bae1dSRodney W. Grimes 	}
2591e2e80fb3SKonstantin Belousov unlock:
2592df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
2593df8bae1dSRodney W. Grimes 
2594fa50a355SKonstantin Belousov 	if (result != KERN_SUCCESS) {
2595fa50a355SKonstantin Belousov 		vm_map_lock(submap);
2596fa50a355SKonstantin Belousov 		submap->flags &= ~MAP_IS_SUB_MAP;
2597fa50a355SKonstantin Belousov 		vm_map_unlock(submap);
2598fa50a355SKonstantin Belousov 	}
2599df8bae1dSRodney W. Grimes 	return (result);
2600df8bae1dSRodney W. Grimes }
2601df8bae1dSRodney W. Grimes 
2602df8bae1dSRodney W. Grimes /*
2603dd05fa19SAlan Cox  * The maximum number of pages to map if MAP_PREFAULT_PARTIAL is specified
26041f78f902SAlan Cox  */
26051f78f902SAlan Cox #define	MAX_INIT_PT	96
26061f78f902SAlan Cox 
26071f78f902SAlan Cox /*
26080551c08dSAlan Cox  *	vm_map_pmap_enter:
26090551c08dSAlan Cox  *
2610dd05fa19SAlan Cox  *	Preload the specified map's pmap with mappings to the specified
2611dd05fa19SAlan Cox  *	object's memory-resident pages.  No further physical pages are
2612dd05fa19SAlan Cox  *	allocated, and no further virtual pages are retrieved from secondary
2613dd05fa19SAlan Cox  *	storage.  If the specified flags include MAP_PREFAULT_PARTIAL, then a
2614dd05fa19SAlan Cox  *	limited number of page mappings are created at the low-end of the
2615dd05fa19SAlan Cox  *	specified address range.  (For this purpose, a superpage mapping
2616dd05fa19SAlan Cox  *	counts as one page mapping.)  Otherwise, all resident pages within
26173453bca8SAlan Cox  *	the specified address range are mapped.
26180551c08dSAlan Cox  */
2619077ec27cSAlan Cox static void
26204da4d293SAlan Cox vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
26210551c08dSAlan Cox     vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags)
26220551c08dSAlan Cox {
26238fece8c3SAlan Cox 	vm_offset_t start;
2624ce142d9eSAlan Cox 	vm_page_t p, p_start;
2625dd05fa19SAlan Cox 	vm_pindex_t mask, psize, threshold, tmpidx;
26260551c08dSAlan Cox 
2627ba8bca61SAlan Cox 	if ((prot & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 || object == NULL)
26281f78f902SAlan Cox 		return;
26299af6d512SAttilio Rao 	if (object->type == OBJT_DEVICE || object->type == OBJT_SG) {
263089f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
263101381811SJohn Baldwin 		if (object->type == OBJT_DEVICE || object->type == OBJT_SG) {
26329af6d512SAttilio Rao 			pmap_object_init_pt(map->pmap, addr, object, pindex,
26339af6d512SAttilio Rao 			    size);
26349af6d512SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
26359af6d512SAttilio Rao 			return;
26369af6d512SAttilio Rao 		}
26379af6d512SAttilio Rao 		VM_OBJECT_LOCK_DOWNGRADE(object);
2638886b9021SJeff Roberson 	} else
2639886b9021SJeff Roberson 		VM_OBJECT_RLOCK(object);
26401f78f902SAlan Cox 
26411f78f902SAlan Cox 	psize = atop(size);
26421f78f902SAlan Cox 	if (psize + pindex > object->size) {
2643ed2f945aSMark Johnston 		if (pindex >= object->size) {
26449af6d512SAttilio Rao 			VM_OBJECT_RUNLOCK(object);
26459af6d512SAttilio Rao 			return;
26469af6d512SAttilio Rao 		}
26471f78f902SAlan Cox 		psize = object->size - pindex;
26481f78f902SAlan Cox 	}
26491f78f902SAlan Cox 
2650ce142d9eSAlan Cox 	start = 0;
2651ce142d9eSAlan Cox 	p_start = NULL;
2652dd05fa19SAlan Cox 	threshold = MAX_INIT_PT;
26531f78f902SAlan Cox 
2654b382c10aSKonstantin Belousov 	p = vm_page_find_least(object, pindex);
26551f78f902SAlan Cox 	/*
26561f78f902SAlan Cox 	 * Assert: the variable p is either (1) the page with the
26571f78f902SAlan Cox 	 * least pindex greater than or equal to the parameter pindex
26581f78f902SAlan Cox 	 * or (2) NULL.
26591f78f902SAlan Cox 	 */
26601f78f902SAlan Cox 	for (;
26611f78f902SAlan Cox 	     p != NULL && (tmpidx = p->pindex - pindex) < psize;
26621f78f902SAlan Cox 	     p = TAILQ_NEXT(p, listq)) {
26631f78f902SAlan Cox 		/*
26641f78f902SAlan Cox 		 * don't allow an madvise to blow away our really
26651f78f902SAlan Cox 		 * free pages allocating pv entries.
26661f78f902SAlan Cox 		 */
2667dd05fa19SAlan Cox 		if (((flags & MAP_PREFAULT_MADVISE) != 0 &&
2668e2068d0bSJeff Roberson 		    vm_page_count_severe()) ||
2669dd05fa19SAlan Cox 		    ((flags & MAP_PREFAULT_PARTIAL) != 0 &&
2670dd05fa19SAlan Cox 		    tmpidx >= threshold)) {
2671379fb642SAlan Cox 			psize = tmpidx;
26721f78f902SAlan Cox 			break;
26731f78f902SAlan Cox 		}
26740012f373SJeff Roberson 		if (vm_page_all_valid(p)) {
2675ce142d9eSAlan Cox 			if (p_start == NULL) {
2676ce142d9eSAlan Cox 				start = addr + ptoa(tmpidx);
2677ce142d9eSAlan Cox 				p_start = p;
2678ce142d9eSAlan Cox 			}
2679dd05fa19SAlan Cox 			/* Jump ahead if a superpage mapping is possible. */
2680dd05fa19SAlan Cox 			if (p->psind > 0 && ((addr + ptoa(tmpidx)) &
2681dd05fa19SAlan Cox 			    (pagesizes[p->psind] - 1)) == 0) {
2682dd05fa19SAlan Cox 				mask = atop(pagesizes[p->psind]) - 1;
2683dd05fa19SAlan Cox 				if (tmpidx + mask < psize &&
268488302601SAlan Cox 				    vm_page_ps_test(p, PS_ALL_VALID, NULL)) {
2685dd05fa19SAlan Cox 					p += mask;
2686dd05fa19SAlan Cox 					threshold += mask;
2687dd05fa19SAlan Cox 				}
2688dd05fa19SAlan Cox 			}
26897bfda801SAlan Cox 		} else if (p_start != NULL) {
2690cf4682aeSAlan Cox 			pmap_enter_object(map->pmap, start, addr +
2691cf4682aeSAlan Cox 			    ptoa(tmpidx), p_start, prot);
2692cf4682aeSAlan Cox 			p_start = NULL;
2693cf4682aeSAlan Cox 		}
2694cf4682aeSAlan Cox 	}
2695c46b90e9SAlan Cox 	if (p_start != NULL)
2696379fb642SAlan Cox 		pmap_enter_object(map->pmap, start, addr + ptoa(psize),
2697379fb642SAlan Cox 		    p_start, prot);
26989af6d512SAttilio Rao 	VM_OBJECT_RUNLOCK(object);
26990551c08dSAlan Cox }
27000551c08dSAlan Cox 
27010551c08dSAlan Cox /*
2702df8bae1dSRodney W. Grimes  *	vm_map_protect:
2703df8bae1dSRodney W. Grimes  *
27040659df6fSKonstantin Belousov  *	Sets the protection and/or the maximum protection of the
27050659df6fSKonstantin Belousov  *	specified address region in the target map.
2706df8bae1dSRodney W. Grimes  */
2707df8bae1dSRodney W. Grimes int
2708b9dcd593SBruce Evans vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
27090659df6fSKonstantin Belousov     vm_prot_t new_prot, vm_prot_t new_maxprot, int flags)
2710df8bae1dSRodney W. Grimes {
27112767c9f3SDoug Moore 	vm_map_entry_t entry, first_entry, in_tran, prev_entry;
27123364c323SKonstantin Belousov 	vm_object_t obj;
2713ef694c1aSEdward Tomasz Napierala 	struct ucred *cred;
2714210a6886SKonstantin Belousov 	vm_prot_t old_prot;
2715a72dce34SDoug Moore 	int rv;
2716df8bae1dSRodney W. Grimes 
271779e9451fSKonstantin Belousov 	if (start == end)
271879e9451fSKonstantin Belousov 		return (KERN_SUCCESS);
271979e9451fSKonstantin Belousov 
27200659df6fSKonstantin Belousov 	if ((flags & (VM_MAP_PROTECT_SET_PROT | VM_MAP_PROTECT_SET_MAXPROT)) ==
27210659df6fSKonstantin Belousov 	    (VM_MAP_PROTECT_SET_PROT | VM_MAP_PROTECT_SET_MAXPROT) &&
27220659df6fSKonstantin Belousov 	    (new_prot & new_maxprot) != new_prot)
27230659df6fSKonstantin Belousov 		return (KERN_OUT_OF_BOUNDS);
27240659df6fSKonstantin Belousov 
272519f5d9f2SKonstantin Belousov again:
272619f5d9f2SKonstantin Belousov 	in_tran = NULL;
2727df8bae1dSRodney W. Grimes 	vm_map_lock(map);
2728df8bae1dSRodney W. Grimes 
27290659df6fSKonstantin Belousov 	if ((map->flags & MAP_WXORX) != 0 &&
27300659df6fSKonstantin Belousov 	    (flags & VM_MAP_PROTECT_SET_PROT) != 0 &&
27310659df6fSKonstantin Belousov 	    (new_prot & (VM_PROT_WRITE | VM_PROT_EXECUTE)) == (VM_PROT_WRITE |
27322e1c94aaSKonstantin Belousov 	    VM_PROT_EXECUTE)) {
27332e1c94aaSKonstantin Belousov 		vm_map_unlock(map);
27342e1c94aaSKonstantin Belousov 		return (KERN_PROTECTION_FAILURE);
27352e1c94aaSKonstantin Belousov 	}
27362e1c94aaSKonstantin Belousov 
2737e1cb9d37SMark Johnston 	/*
2738e1cb9d37SMark Johnston 	 * Ensure that we are not concurrently wiring pages.  vm_map_wire() may
2739e1cb9d37SMark Johnston 	 * need to fault pages into the map and will drop the map lock while
2740e1cb9d37SMark Johnston 	 * doing so, and the VM object may end up in an inconsistent state if we
2741e1cb9d37SMark Johnston 	 * update the protection on the map entry in between faults.
2742e1cb9d37SMark Johnston 	 */
2743e1cb9d37SMark Johnston 	vm_map_wait_busy(map);
2744e1cb9d37SMark Johnston 
2745df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
2746df8bae1dSRodney W. Grimes 
27472767c9f3SDoug Moore 	if (!vm_map_lookup_entry(map, start, &first_entry))
27482767c9f3SDoug Moore 		first_entry = vm_map_entry_succ(first_entry);
2749df8bae1dSRodney W. Grimes 
2750df8bae1dSRodney W. Grimes 	/*
27510d94caffSDavid Greenman 	 * Make a first pass to check for protection violations.
2752df8bae1dSRodney W. Grimes 	 */
27532767c9f3SDoug Moore 	for (entry = first_entry; entry->start < end;
27542767c9f3SDoug Moore 	    entry = vm_map_entry_succ(entry)) {
27552767c9f3SDoug Moore 		if ((entry->eflags & MAP_ENTRY_GUARD) != 0)
27568a89ca94SKonstantin Belousov 			continue;
27572767c9f3SDoug Moore 		if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0) {
2758a1f6d91cSDavid Greenman 			vm_map_unlock(map);
2759df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ARGUMENT);
2760a1f6d91cSDavid Greenman 		}
27610659df6fSKonstantin Belousov 		if ((flags & VM_MAP_PROTECT_SET_PROT) == 0)
27620659df6fSKonstantin Belousov 			new_prot = entry->protection;
27630659df6fSKonstantin Belousov 		if ((flags & VM_MAP_PROTECT_SET_MAXPROT) == 0)
27640659df6fSKonstantin Belousov 			new_maxprot = entry->max_protection;
27650659df6fSKonstantin Belousov 		if ((new_prot & entry->max_protection) != new_prot ||
27660659df6fSKonstantin Belousov 		    (new_maxprot & entry->max_protection) != new_maxprot) {
2767df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
2768df8bae1dSRodney W. Grimes 			return (KERN_PROTECTION_FAILURE);
2769df8bae1dSRodney W. Grimes 		}
27702767c9f3SDoug Moore 		if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0)
27712767c9f3SDoug Moore 			in_tran = entry;
277219f5d9f2SKonstantin Belousov 	}
277319f5d9f2SKonstantin Belousov 
277419f5d9f2SKonstantin Belousov 	/*
2775bdb90e76SDoug Moore 	 * Postpone the operation until all in-transition map entries have
2776bdb90e76SDoug Moore 	 * stabilized.  An in-transition entry might already have its pages
2777bdb90e76SDoug Moore 	 * wired and wired_count incremented, but not yet have its
2778bdb90e76SDoug Moore 	 * MAP_ENTRY_USER_WIRED flag set.  In which case, we would fail to call
2779bdb90e76SDoug Moore 	 * vm_fault_copy_entry() in the final loop below.
278019f5d9f2SKonstantin Belousov 	 */
278119f5d9f2SKonstantin Belousov 	if (in_tran != NULL) {
278219f5d9f2SKonstantin Belousov 		in_tran->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
278319f5d9f2SKonstantin Belousov 		vm_map_unlock_and_wait(map, 0);
278419f5d9f2SKonstantin Belousov 		goto again;
2785df8bae1dSRodney W. Grimes 	}
2786df8bae1dSRodney W. Grimes 
27873364c323SKonstantin Belousov 	/*
2788a72dce34SDoug Moore 	 * Before changing the protections, try to reserve swap space for any
2789a72dce34SDoug Moore 	 * private (i.e., copy-on-write) mappings that are transitioning from
2790a72dce34SDoug Moore 	 * read-only to read/write access.  If a reservation fails, break out
2791a72dce34SDoug Moore 	 * of this loop early and let the next loop simplify the entries, since
2792a72dce34SDoug Moore 	 * some may now be mergeable.
27933364c323SKonstantin Belousov 	 */
2794e2e80fb3SKonstantin Belousov 	rv = vm_map_clip_start(map, first_entry, start);
2795e2e80fb3SKonstantin Belousov 	if (rv != KERN_SUCCESS) {
2796e2e80fb3SKonstantin Belousov 		vm_map_unlock(map);
2797e2e80fb3SKonstantin Belousov 		return (rv);
2798e2e80fb3SKonstantin Belousov 	}
27992767c9f3SDoug Moore 	for (entry = first_entry; entry->start < end;
28002767c9f3SDoug Moore 	    entry = vm_map_entry_succ(entry)) {
2801e2e80fb3SKonstantin Belousov 		rv = vm_map_clip_end(map, entry, end);
2802e2e80fb3SKonstantin Belousov 		if (rv != KERN_SUCCESS) {
2803e2e80fb3SKonstantin Belousov 			vm_map_unlock(map);
2804e2e80fb3SKonstantin Belousov 			return (rv);
2805e2e80fb3SKonstantin Belousov 		}
28063364c323SKonstantin Belousov 
28070659df6fSKonstantin Belousov 		if ((flags & VM_MAP_PROTECT_SET_PROT) == 0 ||
28082767c9f3SDoug Moore 		    ((new_prot & ~entry->protection) & VM_PROT_WRITE) == 0 ||
28092767c9f3SDoug Moore 		    ENTRY_CHARGED(entry) ||
28100659df6fSKonstantin Belousov 		    (entry->eflags & MAP_ENTRY_GUARD) != 0)
28113364c323SKonstantin Belousov 			continue;
28123364c323SKonstantin Belousov 
2813ef694c1aSEdward Tomasz Napierala 		cred = curthread->td_ucred;
28142767c9f3SDoug Moore 		obj = entry->object.vm_object;
28153364c323SKonstantin Belousov 
28162767c9f3SDoug Moore 		if (obj == NULL ||
28172767c9f3SDoug Moore 		    (entry->eflags & MAP_ENTRY_NEEDS_COPY) != 0) {
28182767c9f3SDoug Moore 			if (!swap_reserve(entry->end - entry->start)) {
2819a72dce34SDoug Moore 				rv = KERN_RESOURCE_SHORTAGE;
28202767c9f3SDoug Moore 				end = entry->end;
2821a72dce34SDoug Moore 				break;
28223364c323SKonstantin Belousov 			}
2823ef694c1aSEdward Tomasz Napierala 			crhold(cred);
28242767c9f3SDoug Moore 			entry->cred = cred;
28253364c323SKonstantin Belousov 			continue;
28263364c323SKonstantin Belousov 		}
28273364c323SKonstantin Belousov 
28284b8365d7SKonstantin Belousov 		if (obj->type != OBJT_DEFAULT &&
28294b8365d7SKonstantin Belousov 		    (obj->flags & OBJ_SWAP) == 0)
2830886b9021SJeff Roberson 			continue;
283189f6b863SAttilio Rao 		VM_OBJECT_WLOCK(obj);
28324b8365d7SKonstantin Belousov 		if (obj->type != OBJT_DEFAULT &&
28334b8365d7SKonstantin Belousov 		    (obj->flags & OBJ_SWAP) == 0) {
283489f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(obj);
28353364c323SKonstantin Belousov 			continue;
28363364c323SKonstantin Belousov 		}
28373364c323SKonstantin Belousov 
28383364c323SKonstantin Belousov 		/*
28393364c323SKonstantin Belousov 		 * Charge for the whole object allocation now, since
28403364c323SKonstantin Belousov 		 * we cannot distinguish between non-charged and
28413364c323SKonstantin Belousov 		 * charged clipped mapping of the same object later.
28423364c323SKonstantin Belousov 		 */
28433364c323SKonstantin Belousov 		KASSERT(obj->charge == 0,
28443d95614fSKonstantin Belousov 		    ("vm_map_protect: object %p overcharged (entry %p)",
28452767c9f3SDoug Moore 		    obj, entry));
28463364c323SKonstantin Belousov 		if (!swap_reserve(ptoa(obj->size))) {
284789f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(obj);
2848a72dce34SDoug Moore 			rv = KERN_RESOURCE_SHORTAGE;
28492767c9f3SDoug Moore 			end = entry->end;
2850a72dce34SDoug Moore 			break;
28513364c323SKonstantin Belousov 		}
28523364c323SKonstantin Belousov 
2853ef694c1aSEdward Tomasz Napierala 		crhold(cred);
2854ef694c1aSEdward Tomasz Napierala 		obj->cred = cred;
28553364c323SKonstantin Belousov 		obj->charge = ptoa(obj->size);
285689f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(obj);
28573364c323SKonstantin Belousov 	}
28583364c323SKonstantin Belousov 
2859df8bae1dSRodney W. Grimes 	/*
2860a72dce34SDoug Moore 	 * If enough swap space was available, go back and fix up protections.
2861a72dce34SDoug Moore 	 * Otherwise, just simplify entries, since some may have been modified.
2862a72dce34SDoug Moore 	 * [Note that clipping is not necessary the second time.]
2863df8bae1dSRodney W. Grimes 	 */
28642767c9f3SDoug Moore 	for (prev_entry = vm_map_entry_pred(first_entry), entry = first_entry;
28652767c9f3SDoug Moore 	    entry->start < end;
28662767c9f3SDoug Moore 	    vm_map_try_merge_entries(map, prev_entry, entry),
28672767c9f3SDoug Moore 	    prev_entry = entry, entry = vm_map_entry_succ(entry)) {
2868a72dce34SDoug Moore 		if (rv != KERN_SUCCESS ||
28692767c9f3SDoug Moore 		    (entry->eflags & MAP_ENTRY_GUARD) != 0)
287019bd0d9cSKonstantin Belousov 			continue;
287119bd0d9cSKonstantin Belousov 
28722767c9f3SDoug Moore 		old_prot = entry->protection;
2873210a6886SKonstantin Belousov 
28740659df6fSKonstantin Belousov 		if ((flags & VM_MAP_PROTECT_SET_MAXPROT) != 0) {
28750659df6fSKonstantin Belousov 			entry->max_protection = new_maxprot;
28760659df6fSKonstantin Belousov 			entry->protection = new_maxprot & old_prot;
28770659df6fSKonstantin Belousov 		}
28780659df6fSKonstantin Belousov 		if ((flags & VM_MAP_PROTECT_SET_PROT) != 0)
28792767c9f3SDoug Moore 			entry->protection = new_prot;
2880df8bae1dSRodney W. Grimes 
2881dd006a1bSAlan Cox 		/*
2882dd006a1bSAlan Cox 		 * For user wired map entries, the normal lazy evaluation of
2883dd006a1bSAlan Cox 		 * write access upgrades through soft page faults is
2884dd006a1bSAlan Cox 		 * undesirable.  Instead, immediately copy any pages that are
2885dd006a1bSAlan Cox 		 * copy-on-write and enable write access in the physical map.
2886dd006a1bSAlan Cox 		 */
28872767c9f3SDoug Moore 		if ((entry->eflags & MAP_ENTRY_USER_WIRED) != 0 &&
28882767c9f3SDoug Moore 		    (entry->protection & VM_PROT_WRITE) != 0 &&
28895930251aSKonstantin Belousov 		    (old_prot & VM_PROT_WRITE) == 0)
28902767c9f3SDoug Moore 			vm_fault_copy_entry(map, map, entry, entry, NULL);
2891210a6886SKonstantin Belousov 
2892df8bae1dSRodney W. Grimes 		/*
28932fafce9eSAlan Cox 		 * When restricting access, update the physical map.  Worry
28942fafce9eSAlan Cox 		 * about copy-on-write here.
2895df8bae1dSRodney W. Grimes 		 */
28962767c9f3SDoug Moore 		if ((old_prot & ~entry->protection) != 0) {
2897afa07f7eSJohn Dyson #define MASK(entry)	(((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
2898df8bae1dSRodney W. Grimes 							VM_PROT_ALL)
28992767c9f3SDoug Moore 			pmap_protect(map->pmap, entry->start,
29002767c9f3SDoug Moore 			    entry->end,
29012767c9f3SDoug Moore 			    entry->protection & MASK(entry));
2902df8bae1dSRodney W. Grimes #undef	MASK
2903df8bae1dSRodney W. Grimes 		}
2904df8bae1dSRodney W. Grimes 	}
29052767c9f3SDoug Moore 	vm_map_try_merge_entries(map, prev_entry, entry);
2906df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
2907a72dce34SDoug Moore 	return (rv);
2908df8bae1dSRodney W. Grimes }
2909df8bae1dSRodney W. Grimes 
2910df8bae1dSRodney W. Grimes /*
2911867a482dSJohn Dyson  *	vm_map_madvise:
2912867a482dSJohn Dyson  *
2913867a482dSJohn Dyson  *	This routine traverses a processes map handling the madvise
2914f7fc307aSAlan Cox  *	system call.  Advisories are classified as either those effecting
2915f7fc307aSAlan Cox  *	the vm_map_entry structure, or those effecting the underlying
2916f7fc307aSAlan Cox  *	objects.
2917867a482dSJohn Dyson  */
2918b4309055SMatthew Dillon int
29191b40f8c0SMatthew Dillon vm_map_madvise(
29201b40f8c0SMatthew Dillon 	vm_map_t map,
29211b40f8c0SMatthew Dillon 	vm_offset_t start,
29221b40f8c0SMatthew Dillon 	vm_offset_t end,
29231b40f8c0SMatthew Dillon 	int behav)
2924867a482dSJohn Dyson {
29252767c9f3SDoug Moore 	vm_map_entry_t entry, prev_entry;
2926e2e80fb3SKonstantin Belousov 	int rv;
29273e7cb27cSAlan Cox 	bool modify_map;
2928867a482dSJohn Dyson 
2929b4309055SMatthew Dillon 	/*
2930b4309055SMatthew Dillon 	 * Some madvise calls directly modify the vm_map_entry, in which case
2931b4309055SMatthew Dillon 	 * we need to use an exclusive lock on the map and we need to perform
2932b4309055SMatthew Dillon 	 * various clipping operations.  Otherwise we only need a read-lock
2933b4309055SMatthew Dillon 	 * on the map.
2934b4309055SMatthew Dillon 	 */
2935b4309055SMatthew Dillon 	switch(behav) {
2936b4309055SMatthew Dillon 	case MADV_NORMAL:
2937b4309055SMatthew Dillon 	case MADV_SEQUENTIAL:
2938b4309055SMatthew Dillon 	case MADV_RANDOM:
29394f79d873SMatthew Dillon 	case MADV_NOSYNC:
29404f79d873SMatthew Dillon 	case MADV_AUTOSYNC:
29419730a5daSPaul Saab 	case MADV_NOCORE:
29429730a5daSPaul Saab 	case MADV_CORE:
294379e9451fSKonstantin Belousov 		if (start == end)
29443e7cb27cSAlan Cox 			return (0);
29453e7cb27cSAlan Cox 		modify_map = true;
2946867a482dSJohn Dyson 		vm_map_lock(map);
2947b4309055SMatthew Dillon 		break;
2948b4309055SMatthew Dillon 	case MADV_WILLNEED:
2949b4309055SMatthew Dillon 	case MADV_DONTNEED:
2950b4309055SMatthew Dillon 	case MADV_FREE:
295179e9451fSKonstantin Belousov 		if (start == end)
29523e7cb27cSAlan Cox 			return (0);
29533e7cb27cSAlan Cox 		modify_map = false;
2954f7fc307aSAlan Cox 		vm_map_lock_read(map);
2955b4309055SMatthew Dillon 		break;
2956b4309055SMatthew Dillon 	default:
29573e7cb27cSAlan Cox 		return (EINVAL);
2958b4309055SMatthew Dillon 	}
2959b4309055SMatthew Dillon 
2960b4309055SMatthew Dillon 	/*
2961b4309055SMatthew Dillon 	 * Locate starting entry and clip if necessary.
2962b4309055SMatthew Dillon 	 */
2963867a482dSJohn Dyson 	VM_MAP_RANGE_CHECK(map, start, end);
2964867a482dSJohn Dyson 
2965f7fc307aSAlan Cox 	if (modify_map) {
2966f7fc307aSAlan Cox 		/*
2967f7fc307aSAlan Cox 		 * madvise behaviors that are implemented in the vm_map_entry.
2968f7fc307aSAlan Cox 		 *
2969f7fc307aSAlan Cox 		 * We clip the vm_map_entry so that behavioral changes are
2970f7fc307aSAlan Cox 		 * limited to the specified address range.
2971f7fc307aSAlan Cox 		 */
2972e2e80fb3SKonstantin Belousov 		rv = vm_map_lookup_clip_start(map, start, &entry, &prev_entry);
2973e2e80fb3SKonstantin Belousov 		if (rv != KERN_SUCCESS) {
2974e2e80fb3SKonstantin Belousov 			vm_map_unlock(map);
2975e2e80fb3SKonstantin Belousov 			return (vm_mmap_to_errno(rv));
2976e2e80fb3SKonstantin Belousov 		}
2977e2e80fb3SKonstantin Belousov 
2978e2e80fb3SKonstantin Belousov 		for (; entry->start < end; prev_entry = entry,
2979e2e80fb3SKonstantin Belousov 		    entry = vm_map_entry_succ(entry)) {
29802767c9f3SDoug Moore 			if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0)
2981867a482dSJohn Dyson 				continue;
2982fed9a903SJohn Dyson 
2983e2e80fb3SKonstantin Belousov 			rv = vm_map_clip_end(map, entry, end);
2984e2e80fb3SKonstantin Belousov 			if (rv != KERN_SUCCESS) {
2985e2e80fb3SKonstantin Belousov 				vm_map_unlock(map);
2986e2e80fb3SKonstantin Belousov 				return (vm_mmap_to_errno(rv));
2987e2e80fb3SKonstantin Belousov 			}
2988fed9a903SJohn Dyson 
2989f7fc307aSAlan Cox 			switch (behav) {
2990867a482dSJohn Dyson 			case MADV_NORMAL:
29912767c9f3SDoug Moore 				vm_map_entry_set_behavior(entry,
29922767c9f3SDoug Moore 				    MAP_ENTRY_BEHAV_NORMAL);
2993867a482dSJohn Dyson 				break;
2994867a482dSJohn Dyson 			case MADV_SEQUENTIAL:
29952767c9f3SDoug Moore 				vm_map_entry_set_behavior(entry,
29962767c9f3SDoug Moore 				    MAP_ENTRY_BEHAV_SEQUENTIAL);
2997867a482dSJohn Dyson 				break;
2998867a482dSJohn Dyson 			case MADV_RANDOM:
29992767c9f3SDoug Moore 				vm_map_entry_set_behavior(entry,
30002767c9f3SDoug Moore 				    MAP_ENTRY_BEHAV_RANDOM);
3001867a482dSJohn Dyson 				break;
30024f79d873SMatthew Dillon 			case MADV_NOSYNC:
30032767c9f3SDoug Moore 				entry->eflags |= MAP_ENTRY_NOSYNC;
30044f79d873SMatthew Dillon 				break;
30054f79d873SMatthew Dillon 			case MADV_AUTOSYNC:
30062767c9f3SDoug Moore 				entry->eflags &= ~MAP_ENTRY_NOSYNC;
30074f79d873SMatthew Dillon 				break;
30089730a5daSPaul Saab 			case MADV_NOCORE:
30092767c9f3SDoug Moore 				entry->eflags |= MAP_ENTRY_NOCOREDUMP;
30109730a5daSPaul Saab 				break;
30119730a5daSPaul Saab 			case MADV_CORE:
30122767c9f3SDoug Moore 				entry->eflags &= ~MAP_ENTRY_NOCOREDUMP;
30139730a5daSPaul Saab 				break;
3014867a482dSJohn Dyson 			default:
3015867a482dSJohn Dyson 				break;
3016867a482dSJohn Dyson 			}
30172767c9f3SDoug Moore 			vm_map_try_merge_entries(map, prev_entry, entry);
3018867a482dSJohn Dyson 		}
30192767c9f3SDoug Moore 		vm_map_try_merge_entries(map, prev_entry, entry);
3020867a482dSJohn Dyson 		vm_map_unlock(map);
3021b4309055SMatthew Dillon 	} else {
302292a59946SJohn Baldwin 		vm_pindex_t pstart, pend;
3023f7fc307aSAlan Cox 
3024f7fc307aSAlan Cox 		/*
3025f7fc307aSAlan Cox 		 * madvise behaviors that are implemented in the underlying
3026f7fc307aSAlan Cox 		 * vm_object.
3027f7fc307aSAlan Cox 		 *
3028f7fc307aSAlan Cox 		 * Since we don't clip the vm_map_entry, we have to clip
3029f7fc307aSAlan Cox 		 * the vm_object pindex and count.
3030f7fc307aSAlan Cox 		 */
3031c7b23459SDoug Moore 		if (!vm_map_lookup_entry(map, start, &entry))
3032c7b23459SDoug Moore 			entry = vm_map_entry_succ(entry);
30332767c9f3SDoug Moore 		for (; entry->start < end;
30342767c9f3SDoug Moore 		    entry = vm_map_entry_succ(entry)) {
303551321f7cSAlan Cox 			vm_offset_t useEnd, useStart;
30365f99b57cSMatthew Dillon 
30372767c9f3SDoug Moore 			if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0)
3038f7fc307aSAlan Cox 				continue;
3039f7fc307aSAlan Cox 
3040bf5661f4SKonstantin Belousov 			/*
3041bf5661f4SKonstantin Belousov 			 * MADV_FREE would otherwise rewind time to
3042bf5661f4SKonstantin Belousov 			 * the creation of the shadow object.  Because
3043bf5661f4SKonstantin Belousov 			 * we hold the VM map read-locked, neither the
3044bf5661f4SKonstantin Belousov 			 * entry's object nor the presence of a
3045bf5661f4SKonstantin Belousov 			 * backing object can change.
3046bf5661f4SKonstantin Belousov 			 */
3047bf5661f4SKonstantin Belousov 			if (behav == MADV_FREE &&
30482767c9f3SDoug Moore 			    entry->object.vm_object != NULL &&
30492767c9f3SDoug Moore 			    entry->object.vm_object->backing_object != NULL)
3050bf5661f4SKonstantin Belousov 				continue;
3051bf5661f4SKonstantin Belousov 
30522767c9f3SDoug Moore 			pstart = OFF_TO_IDX(entry->offset);
30532767c9f3SDoug Moore 			pend = pstart + atop(entry->end - entry->start);
30542767c9f3SDoug Moore 			useStart = entry->start;
30552767c9f3SDoug Moore 			useEnd = entry->end;
3056f7fc307aSAlan Cox 
30572767c9f3SDoug Moore 			if (entry->start < start) {
30582767c9f3SDoug Moore 				pstart += atop(start - entry->start);
30595f99b57cSMatthew Dillon 				useStart = start;
3060f7fc307aSAlan Cox 			}
30612767c9f3SDoug Moore 			if (entry->end > end) {
30622767c9f3SDoug Moore 				pend -= atop(entry->end - end);
306351321f7cSAlan Cox 				useEnd = end;
306451321f7cSAlan Cox 			}
3065f7fc307aSAlan Cox 
306692a59946SJohn Baldwin 			if (pstart >= pend)
3067f7fc307aSAlan Cox 				continue;
3068f7fc307aSAlan Cox 
306951321f7cSAlan Cox 			/*
307051321f7cSAlan Cox 			 * Perform the pmap_advise() before clearing
307151321f7cSAlan Cox 			 * PGA_REFERENCED in vm_page_advise().  Otherwise, a
307251321f7cSAlan Cox 			 * concurrent pmap operation, such as pmap_remove(),
307351321f7cSAlan Cox 			 * could clear a reference in the pmap and set
307451321f7cSAlan Cox 			 * PGA_REFERENCED on the page before the pmap_advise()
307551321f7cSAlan Cox 			 * had completed.  Consequently, the page would appear
307651321f7cSAlan Cox 			 * referenced based upon an old reference that
307751321f7cSAlan Cox 			 * occurred before this pmap_advise() ran.
307851321f7cSAlan Cox 			 */
307951321f7cSAlan Cox 			if (behav == MADV_DONTNEED || behav == MADV_FREE)
308051321f7cSAlan Cox 				pmap_advise(map->pmap, useStart, useEnd,
308151321f7cSAlan Cox 				    behav);
308251321f7cSAlan Cox 
30832767c9f3SDoug Moore 			vm_object_madvise(entry->object.vm_object, pstart,
308492a59946SJohn Baldwin 			    pend, behav);
308554432196SKonstantin Belousov 
308654432196SKonstantin Belousov 			/*
308754432196SKonstantin Belousov 			 * Pre-populate paging structures in the
308854432196SKonstantin Belousov 			 * WILLNEED case.  For wired entries, the
308954432196SKonstantin Belousov 			 * paging structures are already populated.
309054432196SKonstantin Belousov 			 */
309154432196SKonstantin Belousov 			if (behav == MADV_WILLNEED &&
30922767c9f3SDoug Moore 			    entry->wired_count == 0) {
30930551c08dSAlan Cox 				vm_map_pmap_enter(map,
30945f99b57cSMatthew Dillon 				    useStart,
30952767c9f3SDoug Moore 				    entry->protection,
30962767c9f3SDoug Moore 				    entry->object.vm_object,
309792a59946SJohn Baldwin 				    pstart,
309892a59946SJohn Baldwin 				    ptoa(pend - pstart),
3099e3026983SMatthew Dillon 				    MAP_PREFAULT_MADVISE
3100b4309055SMatthew Dillon 				);
3101f7fc307aSAlan Cox 			}
3102f7fc307aSAlan Cox 		}
3103f7fc307aSAlan Cox 		vm_map_unlock_read(map);
3104f7fc307aSAlan Cox 	}
3105b4309055SMatthew Dillon 	return (0);
3106867a482dSJohn Dyson }
3107867a482dSJohn Dyson 
3108867a482dSJohn Dyson /*
3109df8bae1dSRodney W. Grimes  *	vm_map_inherit:
3110df8bae1dSRodney W. Grimes  *
3111df8bae1dSRodney W. Grimes  *	Sets the inheritance of the specified address
3112df8bae1dSRodney W. Grimes  *	range in the target map.  Inheritance
3113df8bae1dSRodney W. Grimes  *	affects how the map will be shared with
3114e2abaaaaSAlan Cox  *	child maps at the time of vmspace_fork.
3115df8bae1dSRodney W. Grimes  */
3116df8bae1dSRodney W. Grimes int
3117b9dcd593SBruce Evans vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
3118b9dcd593SBruce Evans 	       vm_inherit_t new_inheritance)
3119df8bae1dSRodney W. Grimes {
3120e2e80fb3SKonstantin Belousov 	vm_map_entry_t entry, lentry, prev_entry, start_entry;
3121e2e80fb3SKonstantin Belousov 	int rv;
3122df8bae1dSRodney W. Grimes 
3123df8bae1dSRodney W. Grimes 	switch (new_inheritance) {
3124df8bae1dSRodney W. Grimes 	case VM_INHERIT_NONE:
3125df8bae1dSRodney W. Grimes 	case VM_INHERIT_COPY:
3126df8bae1dSRodney W. Grimes 	case VM_INHERIT_SHARE:
312778d7964bSXin LI 	case VM_INHERIT_ZERO:
3128df8bae1dSRodney W. Grimes 		break;
3129df8bae1dSRodney W. Grimes 	default:
3130df8bae1dSRodney W. Grimes 		return (KERN_INVALID_ARGUMENT);
3131df8bae1dSRodney W. Grimes 	}
313279e9451fSKonstantin Belousov 	if (start == end)
313379e9451fSKonstantin Belousov 		return (KERN_SUCCESS);
3134df8bae1dSRodney W. Grimes 	vm_map_lock(map);
3135df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
3136e2e80fb3SKonstantin Belousov 	rv = vm_map_lookup_clip_start(map, start, &start_entry, &prev_entry);
3137e2e80fb3SKonstantin Belousov 	if (rv != KERN_SUCCESS)
3138e2e80fb3SKonstantin Belousov 		goto unlock;
3139e2e80fb3SKonstantin Belousov 	if (vm_map_lookup_entry(map, end - 1, &lentry)) {
3140e2e80fb3SKonstantin Belousov 		rv = vm_map_clip_end(map, lentry, end);
3141e2e80fb3SKonstantin Belousov 		if (rv != KERN_SUCCESS)
3142e2e80fb3SKonstantin Belousov 			goto unlock;
3143e2e80fb3SKonstantin Belousov 	}
3144e2e80fb3SKonstantin Belousov 	if (new_inheritance == VM_INHERIT_COPY) {
3145e2e80fb3SKonstantin Belousov 		for (entry = start_entry; entry->start < end;
314683704cc2SDoug Moore 		    prev_entry = entry, entry = vm_map_entry_succ(entry)) {
3147e2e80fb3SKonstantin Belousov 			if ((entry->eflags & MAP_ENTRY_SPLIT_BOUNDARY_MASK)
3148e2e80fb3SKonstantin Belousov 			    != 0) {
3149e2e80fb3SKonstantin Belousov 				rv = KERN_INVALID_ARGUMENT;
3150e2e80fb3SKonstantin Belousov 				goto unlock;
3151e2e80fb3SKonstantin Belousov 			}
3152e2e80fb3SKonstantin Belousov 		}
3153e2e80fb3SKonstantin Belousov 	}
3154e2e80fb3SKonstantin Belousov 	for (entry = start_entry; entry->start < end; prev_entry = entry,
3155e2e80fb3SKonstantin Belousov 	    entry = vm_map_entry_succ(entry)) {
3156e2e80fb3SKonstantin Belousov 		KASSERT(entry->end <= end, ("non-clipped entry %p end %jx %jx",
3157e2e80fb3SKonstantin Belousov 		    entry, (uintmax_t)entry->end, (uintmax_t)end));
315819bd0d9cSKonstantin Belousov 		if ((entry->eflags & MAP_ENTRY_GUARD) == 0 ||
315919bd0d9cSKonstantin Belousov 		    new_inheritance != VM_INHERIT_ZERO)
3160df8bae1dSRodney W. Grimes 			entry->inheritance = new_inheritance;
316183704cc2SDoug Moore 		vm_map_try_merge_entries(map, prev_entry, entry);
3162df8bae1dSRodney W. Grimes 	}
316383704cc2SDoug Moore 	vm_map_try_merge_entries(map, prev_entry, entry);
3164e2e80fb3SKonstantin Belousov unlock:
3165df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
3166e2e80fb3SKonstantin Belousov 	return (rv);
3167df8bae1dSRodney W. Grimes }
3168df8bae1dSRodney W. Grimes 
3169df8bae1dSRodney W. Grimes /*
3170312df2c1SDoug Moore  *	vm_map_entry_in_transition:
3171312df2c1SDoug Moore  *
3172312df2c1SDoug Moore  *	Release the map lock, and sleep until the entry is no longer in
3173312df2c1SDoug Moore  *	transition.  Awake and acquire the map lock.  If the map changed while
3174312df2c1SDoug Moore  *	another held the lock, lookup a possibly-changed entry at or after the
3175312df2c1SDoug Moore  *	'start' position of the old entry.
3176312df2c1SDoug Moore  */
3177312df2c1SDoug Moore static vm_map_entry_t
3178312df2c1SDoug Moore vm_map_entry_in_transition(vm_map_t map, vm_offset_t in_start,
3179312df2c1SDoug Moore     vm_offset_t *io_end, bool holes_ok, vm_map_entry_t in_entry)
3180312df2c1SDoug Moore {
3181312df2c1SDoug Moore 	vm_map_entry_t entry;
3182312df2c1SDoug Moore 	vm_offset_t start;
3183312df2c1SDoug Moore 	u_int last_timestamp;
3184312df2c1SDoug Moore 
3185312df2c1SDoug Moore 	VM_MAP_ASSERT_LOCKED(map);
3186312df2c1SDoug Moore 	KASSERT((in_entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0,
3187312df2c1SDoug Moore 	    ("not in-tranition map entry %p", in_entry));
3188312df2c1SDoug Moore 	/*
3189312df2c1SDoug Moore 	 * We have not yet clipped the entry.
3190312df2c1SDoug Moore 	 */
3191312df2c1SDoug Moore 	start = MAX(in_start, in_entry->start);
3192312df2c1SDoug Moore 	in_entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
3193312df2c1SDoug Moore 	last_timestamp = map->timestamp;
3194312df2c1SDoug Moore 	if (vm_map_unlock_and_wait(map, 0)) {
3195312df2c1SDoug Moore 		/*
3196312df2c1SDoug Moore 		 * Allow interruption of user wiring/unwiring?
3197312df2c1SDoug Moore 		 */
3198312df2c1SDoug Moore 	}
3199312df2c1SDoug Moore 	vm_map_lock(map);
3200312df2c1SDoug Moore 	if (last_timestamp + 1 == map->timestamp)
3201312df2c1SDoug Moore 		return (in_entry);
3202312df2c1SDoug Moore 
3203312df2c1SDoug Moore 	/*
3204312df2c1SDoug Moore 	 * Look again for the entry because the map was modified while it was
3205312df2c1SDoug Moore 	 * unlocked.  Specifically, the entry may have been clipped, merged, or
3206312df2c1SDoug Moore 	 * deleted.
3207312df2c1SDoug Moore 	 */
3208312df2c1SDoug Moore 	if (!vm_map_lookup_entry(map, start, &entry)) {
3209312df2c1SDoug Moore 		if (!holes_ok) {
3210312df2c1SDoug Moore 			*io_end = start;
3211312df2c1SDoug Moore 			return (NULL);
3212312df2c1SDoug Moore 		}
32137cdcf863SDoug Moore 		entry = vm_map_entry_succ(entry);
3214312df2c1SDoug Moore 	}
3215312df2c1SDoug Moore 	return (entry);
3216312df2c1SDoug Moore }
3217312df2c1SDoug Moore 
3218312df2c1SDoug Moore /*
3219acd9a301SAlan Cox  *	vm_map_unwire:
3220acd9a301SAlan Cox  *
3221e27e17b7SAlan Cox  *	Implements both kernel and user unwiring.
3222acd9a301SAlan Cox  */
3223acd9a301SAlan Cox int
3224acd9a301SAlan Cox vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
3225abd498aaSBruce M Simpson     int flags)
3226acd9a301SAlan Cox {
322783704cc2SDoug Moore 	vm_map_entry_t entry, first_entry, next_entry, prev_entry;
3228acd9a301SAlan Cox 	int rv;
322983704cc2SDoug Moore 	bool holes_ok, need_wakeup, user_unwire;
3230acd9a301SAlan Cox 
323179e9451fSKonstantin Belousov 	if (start == end)
323279e9451fSKonstantin Belousov 		return (KERN_SUCCESS);
32339a0cdf94SDoug Moore 	holes_ok = (flags & VM_MAP_WIRE_HOLESOK) != 0;
32349a0cdf94SDoug Moore 	user_unwire = (flags & VM_MAP_WIRE_USER) != 0;
3235acd9a301SAlan Cox 	vm_map_lock(map);
3236acd9a301SAlan Cox 	VM_MAP_RANGE_CHECK(map, start, end);
3237d1d3f7e1SDoug Moore 	if (!vm_map_lookup_entry(map, start, &first_entry)) {
32389a0cdf94SDoug Moore 		if (holes_ok)
32397cdcf863SDoug Moore 			first_entry = vm_map_entry_succ(first_entry);
3240d1d3f7e1SDoug Moore 		else {
3241acd9a301SAlan Cox 			vm_map_unlock(map);
3242acd9a301SAlan Cox 			return (KERN_INVALID_ADDRESS);
3243acd9a301SAlan Cox 		}
3244abd498aaSBruce M Simpson 	}
3245d2860f22SDoug Moore 	rv = KERN_SUCCESS;
324683704cc2SDoug Moore 	for (entry = first_entry; entry->start < end; entry = next_entry) {
3247acd9a301SAlan Cox 		if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
3248acd9a301SAlan Cox 			/*
3249acd9a301SAlan Cox 			 * We have not yet clipped the entry.
3250acd9a301SAlan Cox 			 */
325183704cc2SDoug Moore 			next_entry = vm_map_entry_in_transition(map, start,
325283704cc2SDoug Moore 			    &end, holes_ok, entry);
325383704cc2SDoug Moore 			if (next_entry == NULL) {
325483704cc2SDoug Moore 				if (entry == first_entry) {
3255acd9a301SAlan Cox 					vm_map_unlock(map);
3256acd9a301SAlan Cox 					return (KERN_INVALID_ADDRESS);
3257acd9a301SAlan Cox 				}
3258acd9a301SAlan Cox 				rv = KERN_INVALID_ADDRESS;
3259d2860f22SDoug Moore 				break;
3260acd9a301SAlan Cox 			}
326183704cc2SDoug Moore 			first_entry = (entry == first_entry) ?
326283704cc2SDoug Moore 			    next_entry : NULL;
3263acd9a301SAlan Cox 			continue;
3264acd9a301SAlan Cox 		}
3265e2e80fb3SKonstantin Belousov 		rv = vm_map_clip_start(map, entry, start);
3266e2e80fb3SKonstantin Belousov 		if (rv != KERN_SUCCESS)
3267e2e80fb3SKonstantin Belousov 			break;
3268e2e80fb3SKonstantin Belousov 		rv = vm_map_clip_end(map, entry, end);
3269e2e80fb3SKonstantin Belousov 		if (rv != KERN_SUCCESS)
3270e2e80fb3SKonstantin Belousov 			break;
3271e2e80fb3SKonstantin Belousov 
3272acd9a301SAlan Cox 		/*
3273acd9a301SAlan Cox 		 * Mark the entry in case the map lock is released.  (See
3274acd9a301SAlan Cox 		 * above.)
3275acd9a301SAlan Cox 		 */
3276ff3ae454SKonstantin Belousov 		KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 &&
3277ff3ae454SKonstantin Belousov 		    entry->wiring_thread == NULL,
3278ff3ae454SKonstantin Belousov 		    ("owned map entry %p", entry));
3279acd9a301SAlan Cox 		entry->eflags |= MAP_ENTRY_IN_TRANSITION;
32800acea7dfSKonstantin Belousov 		entry->wiring_thread = curthread;
328183704cc2SDoug Moore 		next_entry = vm_map_entry_succ(entry);
3282acd9a301SAlan Cox 		/*
3283acd9a301SAlan Cox 		 * Check the map for holes in the specified region.
32849a0cdf94SDoug Moore 		 * If holes_ok, skip this check.
3285acd9a301SAlan Cox 		 */
32869a0cdf94SDoug Moore 		if (!holes_ok &&
328783704cc2SDoug Moore 		    entry->end < end && next_entry->start > entry->end) {
3288acd9a301SAlan Cox 			end = entry->end;
3289acd9a301SAlan Cox 			rv = KERN_INVALID_ADDRESS;
3290d2860f22SDoug Moore 			break;
3291acd9a301SAlan Cox 		}
3292acd9a301SAlan Cox 		/*
32933ffbc0cdSAlan Cox 		 * If system unwiring, require that the entry is system wired.
3294acd9a301SAlan Cox 		 */
32950ada205eSBrian Feldman 		if (!user_unwire &&
32960ada205eSBrian Feldman 		    vm_map_entry_system_wired_count(entry) == 0) {
3297acd9a301SAlan Cox 			end = entry->end;
3298acd9a301SAlan Cox 			rv = KERN_INVALID_ARGUMENT;
3299d2860f22SDoug Moore 			break;
3300acd9a301SAlan Cox 		}
3301acd9a301SAlan Cox 	}
33029a0cdf94SDoug Moore 	need_wakeup = false;
33039a0cdf94SDoug Moore 	if (first_entry == NULL &&
33049a0cdf94SDoug Moore 	    !vm_map_lookup_entry(map, start, &first_entry)) {
33059a0cdf94SDoug Moore 		KASSERT(holes_ok, ("vm_map_unwire: lookup failed"));
330683704cc2SDoug Moore 		prev_entry = first_entry;
330783704cc2SDoug Moore 		entry = vm_map_entry_succ(first_entry);
330883704cc2SDoug Moore 	} else {
330983704cc2SDoug Moore 		prev_entry = vm_map_entry_pred(first_entry);
331083704cc2SDoug Moore 		entry = first_entry;
3311acd9a301SAlan Cox 	}
331283704cc2SDoug Moore 	for (; entry->start < end;
331383704cc2SDoug Moore 	    prev_entry = entry, entry = vm_map_entry_succ(entry)) {
33140acea7dfSKonstantin Belousov 		/*
33159a0cdf94SDoug Moore 		 * If holes_ok was specified, an empty
33160acea7dfSKonstantin Belousov 		 * space in the unwired region could have been mapped
33170acea7dfSKonstantin Belousov 		 * while the map lock was dropped for draining
33180acea7dfSKonstantin Belousov 		 * MAP_ENTRY_IN_TRANSITION.  Moreover, another thread
33190acea7dfSKonstantin Belousov 		 * could be simultaneously wiring this new mapping
33200acea7dfSKonstantin Belousov 		 * entry.  Detect these cases and skip any entries
33210acea7dfSKonstantin Belousov 		 * marked as in transition by us.
33220acea7dfSKonstantin Belousov 		 */
33230acea7dfSKonstantin Belousov 		if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 ||
33240acea7dfSKonstantin Belousov 		    entry->wiring_thread != curthread) {
33259a0cdf94SDoug Moore 			KASSERT(holes_ok,
33260acea7dfSKonstantin Belousov 			    ("vm_map_unwire: !HOLESOK and new/changed entry"));
33270acea7dfSKonstantin Belousov 			continue;
33280acea7dfSKonstantin Belousov 		}
33290acea7dfSKonstantin Belousov 
33303ffbc0cdSAlan Cox 		if (rv == KERN_SUCCESS && (!user_unwire ||
33313ffbc0cdSAlan Cox 		    (entry->eflags & MAP_ENTRY_USER_WIRED))) {
333203462509SAlan Cox 			if (entry->wired_count == 1)
333303462509SAlan Cox 				vm_map_entry_unwire(map, entry);
333403462509SAlan Cox 			else
3335b2f3846aSAlan Cox 				entry->wired_count--;
333654a3a114SMark Johnston 			if (user_unwire)
333754a3a114SMark Johnston 				entry->eflags &= ~MAP_ENTRY_USER_WIRED;
3338b2f3846aSAlan Cox 		}
33390acea7dfSKonstantin Belousov 		KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0,
3340ff3ae454SKonstantin Belousov 		    ("vm_map_unwire: in-transition flag missing %p", entry));
3341ff3ae454SKonstantin Belousov 		KASSERT(entry->wiring_thread == curthread,
3342ff3ae454SKonstantin Belousov 		    ("vm_map_unwire: alien wire %p", entry));
3343acd9a301SAlan Cox 		entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
33440acea7dfSKonstantin Belousov 		entry->wiring_thread = NULL;
3345acd9a301SAlan Cox 		if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
3346acd9a301SAlan Cox 			entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
33479a0cdf94SDoug Moore 			need_wakeup = true;
3348acd9a301SAlan Cox 		}
334983704cc2SDoug Moore 		vm_map_try_merge_entries(map, prev_entry, entry);
3350acd9a301SAlan Cox 	}
335183704cc2SDoug Moore 	vm_map_try_merge_entries(map, prev_entry, entry);
3352acd9a301SAlan Cox 	vm_map_unlock(map);
3353acd9a301SAlan Cox 	if (need_wakeup)
3354acd9a301SAlan Cox 		vm_map_wakeup(map);
3355acd9a301SAlan Cox 	return (rv);
3356acd9a301SAlan Cox }
3357acd9a301SAlan Cox 
335854a3a114SMark Johnston static void
335954a3a114SMark Johnston vm_map_wire_user_count_sub(u_long npages)
336054a3a114SMark Johnston {
336154a3a114SMark Johnston 
336254a3a114SMark Johnston 	atomic_subtract_long(&vm_user_wire_count, npages);
336354a3a114SMark Johnston }
336454a3a114SMark Johnston 
336554a3a114SMark Johnston static bool
336654a3a114SMark Johnston vm_map_wire_user_count_add(u_long npages)
336754a3a114SMark Johnston {
336854a3a114SMark Johnston 	u_long wired;
336954a3a114SMark Johnston 
337054a3a114SMark Johnston 	wired = vm_user_wire_count;
337154a3a114SMark Johnston 	do {
337254a3a114SMark Johnston 		if (npages + wired > vm_page_max_user_wired)
337354a3a114SMark Johnston 			return (false);
337454a3a114SMark Johnston 	} while (!atomic_fcmpset_long(&vm_user_wire_count, &wired,
337554a3a114SMark Johnston 	    npages + wired));
337654a3a114SMark Johnston 
337754a3a114SMark Johnston 	return (true);
337854a3a114SMark Johnston }
337954a3a114SMark Johnston 
3380acd9a301SAlan Cox /*
338166cd575bSAlan Cox  *	vm_map_wire_entry_failure:
338266cd575bSAlan Cox  *
338366cd575bSAlan Cox  *	Handle a wiring failure on the given entry.
338466cd575bSAlan Cox  *
338566cd575bSAlan Cox  *	The map should be locked.
338666cd575bSAlan Cox  */
338766cd575bSAlan Cox static void
338866cd575bSAlan Cox vm_map_wire_entry_failure(vm_map_t map, vm_map_entry_t entry,
338966cd575bSAlan Cox     vm_offset_t failed_addr)
339066cd575bSAlan Cox {
339166cd575bSAlan Cox 
339266cd575bSAlan Cox 	VM_MAP_ASSERT_LOCKED(map);
339366cd575bSAlan Cox 	KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 &&
339466cd575bSAlan Cox 	    entry->wired_count == 1,
339566cd575bSAlan Cox 	    ("vm_map_wire_entry_failure: entry %p isn't being wired", entry));
339666cd575bSAlan Cox 	KASSERT(failed_addr < entry->end,
339766cd575bSAlan Cox 	    ("vm_map_wire_entry_failure: entry %p was fully wired", entry));
339866cd575bSAlan Cox 
339966cd575bSAlan Cox 	/*
340066cd575bSAlan Cox 	 * If any pages at the start of this entry were successfully wired,
340166cd575bSAlan Cox 	 * then unwire them.
340266cd575bSAlan Cox 	 */
340366cd575bSAlan Cox 	if (failed_addr > entry->start) {
340466cd575bSAlan Cox 		pmap_unwire(map->pmap, entry->start, failed_addr);
340566cd575bSAlan Cox 		vm_object_unwire(entry->object.vm_object, entry->offset,
340666cd575bSAlan Cox 		    failed_addr - entry->start, PQ_ACTIVE);
340766cd575bSAlan Cox 	}
340866cd575bSAlan Cox 
340966cd575bSAlan Cox 	/*
341066cd575bSAlan Cox 	 * Assign an out-of-range value to represent the failure to wire this
341166cd575bSAlan Cox 	 * entry.
341266cd575bSAlan Cox 	 */
341366cd575bSAlan Cox 	entry->wired_count = -1;
341466cd575bSAlan Cox }
341566cd575bSAlan Cox 
341654a3a114SMark Johnston int
341754a3a114SMark Johnston vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end, int flags)
341854a3a114SMark Johnston {
341954a3a114SMark Johnston 	int rv;
342054a3a114SMark Johnston 
342154a3a114SMark Johnston 	vm_map_lock(map);
342254a3a114SMark Johnston 	rv = vm_map_wire_locked(map, start, end, flags);
342354a3a114SMark Johnston 	vm_map_unlock(map);
342454a3a114SMark Johnston 	return (rv);
342554a3a114SMark Johnston }
342654a3a114SMark Johnston 
342766cd575bSAlan Cox /*
342854a3a114SMark Johnston  *	vm_map_wire_locked:
3429e27e17b7SAlan Cox  *
343054a3a114SMark Johnston  *	Implements both kernel and user wiring.  Returns with the map locked,
343154a3a114SMark Johnston  *	the map lock may be dropped.
3432e27e17b7SAlan Cox  */
3433e27e17b7SAlan Cox int
343454a3a114SMark Johnston vm_map_wire_locked(vm_map_t map, vm_offset_t start, vm_offset_t end, int flags)
3435e27e17b7SAlan Cox {
343683704cc2SDoug Moore 	vm_map_entry_t entry, first_entry, next_entry, prev_entry;
343766cd575bSAlan Cox 	vm_offset_t faddr, saved_end, saved_start;
3438e2e80fb3SKonstantin Belousov 	u_long incr, npages;
3439e2e80fb3SKonstantin Belousov 	u_int bidx, last_timestamp;
344012d7cc84SAlan Cox 	int rv;
344183704cc2SDoug Moore 	bool holes_ok, need_wakeup, user_wire;
3442e4cd31ddSJeff Roberson 	vm_prot_t prot;
3443e27e17b7SAlan Cox 
344454a3a114SMark Johnston 	VM_MAP_ASSERT_LOCKED(map);
344554a3a114SMark Johnston 
344679e9451fSKonstantin Belousov 	if (start == end)
344779e9451fSKonstantin Belousov 		return (KERN_SUCCESS);
3448e4cd31ddSJeff Roberson 	prot = 0;
3449e4cd31ddSJeff Roberson 	if (flags & VM_MAP_WIRE_WRITE)
3450e4cd31ddSJeff Roberson 		prot |= VM_PROT_WRITE;
34519a0cdf94SDoug Moore 	holes_ok = (flags & VM_MAP_WIRE_HOLESOK) != 0;
34529a0cdf94SDoug Moore 	user_wire = (flags & VM_MAP_WIRE_USER) != 0;
345312d7cc84SAlan Cox 	VM_MAP_RANGE_CHECK(map, start, end);
3454d1d3f7e1SDoug Moore 	if (!vm_map_lookup_entry(map, start, &first_entry)) {
34559a0cdf94SDoug Moore 		if (holes_ok)
34567cdcf863SDoug Moore 			first_entry = vm_map_entry_succ(first_entry);
3457d1d3f7e1SDoug Moore 		else
345812d7cc84SAlan Cox 			return (KERN_INVALID_ADDRESS);
345912d7cc84SAlan Cox 	}
346083704cc2SDoug Moore 	for (entry = first_entry; entry->start < end; entry = next_entry) {
346112d7cc84SAlan Cox 		if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
346212d7cc84SAlan Cox 			/*
346312d7cc84SAlan Cox 			 * We have not yet clipped the entry.
346412d7cc84SAlan Cox 			 */
346583704cc2SDoug Moore 			next_entry = vm_map_entry_in_transition(map, start,
346683704cc2SDoug Moore 			    &end, holes_ok, entry);
346783704cc2SDoug Moore 			if (next_entry == NULL) {
346883704cc2SDoug Moore 				if (entry == first_entry)
346912d7cc84SAlan Cox 					return (KERN_INVALID_ADDRESS);
347012d7cc84SAlan Cox 				rv = KERN_INVALID_ADDRESS;
347112d7cc84SAlan Cox 				goto done;
347212d7cc84SAlan Cox 			}
347383704cc2SDoug Moore 			first_entry = (entry == first_entry) ?
347483704cc2SDoug Moore 			    next_entry : NULL;
347512d7cc84SAlan Cox 			continue;
347612d7cc84SAlan Cox 		}
3477e2e80fb3SKonstantin Belousov 		rv = vm_map_clip_start(map, entry, start);
3478e2e80fb3SKonstantin Belousov 		if (rv != KERN_SUCCESS)
3479e2e80fb3SKonstantin Belousov 			goto done;
3480e2e80fb3SKonstantin Belousov 		rv = vm_map_clip_end(map, entry, end);
3481e2e80fb3SKonstantin Belousov 		if (rv != KERN_SUCCESS)
3482e2e80fb3SKonstantin Belousov 			goto done;
3483e2e80fb3SKonstantin Belousov 
348412d7cc84SAlan Cox 		/*
348512d7cc84SAlan Cox 		 * Mark the entry in case the map lock is released.  (See
348612d7cc84SAlan Cox 		 * above.)
348712d7cc84SAlan Cox 		 */
3488ff3ae454SKonstantin Belousov 		KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 &&
3489ff3ae454SKonstantin Belousov 		    entry->wiring_thread == NULL,
3490ff3ae454SKonstantin Belousov 		    ("owned map entry %p", entry));
349112d7cc84SAlan Cox 		entry->eflags |= MAP_ENTRY_IN_TRANSITION;
34920acea7dfSKonstantin Belousov 		entry->wiring_thread = curthread;
3493e4cd31ddSJeff Roberson 		if ((entry->protection & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0
3494e4cd31ddSJeff Roberson 		    || (entry->protection & prot) != prot) {
3495529ab57bSKonstantin Belousov 			entry->eflags |= MAP_ENTRY_WIRE_SKIPPED;
34969a0cdf94SDoug Moore 			if (!holes_ok) {
34976d7e8091SKonstantin Belousov 				end = entry->end;
34986d7e8091SKonstantin Belousov 				rv = KERN_INVALID_ADDRESS;
34996d7e8091SKonstantin Belousov 				goto done;
35006d7e8091SKonstantin Belousov 			}
350138e220e8SDoug Moore 		} else if (entry->wired_count == 0) {
35020ada205eSBrian Feldman 			entry->wired_count++;
350354a3a114SMark Johnston 
350454a3a114SMark Johnston 			npages = atop(entry->end - entry->start);
350554a3a114SMark Johnston 			if (user_wire && !vm_map_wire_user_count_add(npages)) {
350654a3a114SMark Johnston 				vm_map_wire_entry_failure(map, entry,
350754a3a114SMark Johnston 				    entry->start);
350854a3a114SMark Johnston 				end = entry->end;
350954a3a114SMark Johnston 				rv = KERN_RESOURCE_SHORTAGE;
351054a3a114SMark Johnston 				goto done;
351154a3a114SMark Johnston 			}
351266cd575bSAlan Cox 
351312d7cc84SAlan Cox 			/*
351412d7cc84SAlan Cox 			 * Release the map lock, relying on the in-transition
3515a5db445dSMax Laier 			 * mark.  Mark the map busy for fork.
351612d7cc84SAlan Cox 			 */
351754a3a114SMark Johnston 			saved_start = entry->start;
351854a3a114SMark Johnston 			saved_end = entry->end;
3519312df2c1SDoug Moore 			last_timestamp = map->timestamp;
3520e2e80fb3SKonstantin Belousov 			bidx = (entry->eflags & MAP_ENTRY_SPLIT_BOUNDARY_MASK)
3521e2e80fb3SKonstantin Belousov 			    >> MAP_ENTRY_SPLIT_BOUNDARY_SHIFT;
3522e2e80fb3SKonstantin Belousov 			incr =  pagesizes[bidx];
3523a5db445dSMax Laier 			vm_map_busy(map);
352412d7cc84SAlan Cox 			vm_map_unlock(map);
352566cd575bSAlan Cox 
3526e2e80fb3SKonstantin Belousov 			for (faddr = saved_start; faddr < saved_end;
3527e2e80fb3SKonstantin Belousov 			    faddr += incr) {
352866cd575bSAlan Cox 				/*
352966cd575bSAlan Cox 				 * Simulate a fault to get the page and enter
353066cd575bSAlan Cox 				 * it into the physical map.
353166cd575bSAlan Cox 				 */
3532e2e80fb3SKonstantin Belousov 				rv = vm_fault(map, faddr, VM_PROT_NONE,
3533e2e80fb3SKonstantin Belousov 				    VM_FAULT_WIRE, NULL);
3534e2e80fb3SKonstantin Belousov 				if (rv != KERN_SUCCESS)
353566cd575bSAlan Cox 					break;
3536e2e80fb3SKonstantin Belousov 			}
353712d7cc84SAlan Cox 			vm_map_lock(map);
3538a5db445dSMax Laier 			vm_map_unbusy(map);
353912d7cc84SAlan Cox 			if (last_timestamp + 1 != map->timestamp) {
354012d7cc84SAlan Cox 				/*
354112d7cc84SAlan Cox 				 * Look again for the entry because the map was
354212d7cc84SAlan Cox 				 * modified while it was unlocked.  The entry
354312d7cc84SAlan Cox 				 * may have been clipped, but NOT merged or
354412d7cc84SAlan Cox 				 * deleted.
354512d7cc84SAlan Cox 				 */
35469a0cdf94SDoug Moore 				if (!vm_map_lookup_entry(map, saved_start,
354783704cc2SDoug Moore 				    &next_entry))
35489a0cdf94SDoug Moore 					KASSERT(false,
35499a0cdf94SDoug Moore 					    ("vm_map_wire: lookup failed"));
355083704cc2SDoug Moore 				first_entry = (entry == first_entry) ?
355183704cc2SDoug Moore 				    next_entry : NULL;
355283704cc2SDoug Moore 				for (entry = next_entry; entry->end < saved_end;
355383704cc2SDoug Moore 				    entry = vm_map_entry_succ(entry)) {
355466cd575bSAlan Cox 					/*
355566cd575bSAlan Cox 					 * In case of failure, handle entries
355666cd575bSAlan Cox 					 * that were not fully wired here;
355766cd575bSAlan Cox 					 * fully wired entries are handled
355866cd575bSAlan Cox 					 * later.
355966cd575bSAlan Cox 					 */
356066cd575bSAlan Cox 					if (rv != KERN_SUCCESS &&
356166cd575bSAlan Cox 					    faddr < entry->end)
356266cd575bSAlan Cox 						vm_map_wire_entry_failure(map,
356366cd575bSAlan Cox 						    entry, faddr);
356412d7cc84SAlan Cox 				}
356528c58286SAlan Cox 			}
356612d7cc84SAlan Cox 			if (rv != KERN_SUCCESS) {
356766cd575bSAlan Cox 				vm_map_wire_entry_failure(map, entry, faddr);
356854a3a114SMark Johnston 				if (user_wire)
356954a3a114SMark Johnston 					vm_map_wire_user_count_sub(npages);
357012d7cc84SAlan Cox 				end = entry->end;
357112d7cc84SAlan Cox 				goto done;
357212d7cc84SAlan Cox 			}
35730ada205eSBrian Feldman 		} else if (!user_wire ||
35740ada205eSBrian Feldman 			   (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
35750ada205eSBrian Feldman 			entry->wired_count++;
357612d7cc84SAlan Cox 		}
357712d7cc84SAlan Cox 		/*
357812d7cc84SAlan Cox 		 * Check the map for holes in the specified region.
35799a0cdf94SDoug Moore 		 * If holes_ok was specified, skip this check.
358012d7cc84SAlan Cox 		 */
358183704cc2SDoug Moore 		next_entry = vm_map_entry_succ(entry);
35829a0cdf94SDoug Moore 		if (!holes_ok &&
358383704cc2SDoug Moore 		    entry->end < end && next_entry->start > entry->end) {
358412d7cc84SAlan Cox 			end = entry->end;
358512d7cc84SAlan Cox 			rv = KERN_INVALID_ADDRESS;
358612d7cc84SAlan Cox 			goto done;
358712d7cc84SAlan Cox 		}
358812d7cc84SAlan Cox 	}
358912d7cc84SAlan Cox 	rv = KERN_SUCCESS;
359012d7cc84SAlan Cox done:
35919a0cdf94SDoug Moore 	need_wakeup = false;
35929a0cdf94SDoug Moore 	if (first_entry == NULL &&
35939a0cdf94SDoug Moore 	    !vm_map_lookup_entry(map, start, &first_entry)) {
35949a0cdf94SDoug Moore 		KASSERT(holes_ok, ("vm_map_wire: lookup failed"));
359583704cc2SDoug Moore 		prev_entry = first_entry;
359683704cc2SDoug Moore 		entry = vm_map_entry_succ(first_entry);
359783704cc2SDoug Moore 	} else {
359883704cc2SDoug Moore 		prev_entry = vm_map_entry_pred(first_entry);
359983704cc2SDoug Moore 		entry = first_entry;
360012d7cc84SAlan Cox 	}
360183704cc2SDoug Moore 	for (; entry->start < end;
360283704cc2SDoug Moore 	    prev_entry = entry, entry = vm_map_entry_succ(entry)) {
36030acea7dfSKonstantin Belousov 		/*
36049a0cdf94SDoug Moore 		 * If holes_ok was specified, an empty
36050acea7dfSKonstantin Belousov 		 * space in the unwired region could have been mapped
36060acea7dfSKonstantin Belousov 		 * while the map lock was dropped for faulting in the
36070acea7dfSKonstantin Belousov 		 * pages or draining MAP_ENTRY_IN_TRANSITION.
36080acea7dfSKonstantin Belousov 		 * Moreover, another thread could be simultaneously
36090acea7dfSKonstantin Belousov 		 * wiring this new mapping entry.  Detect these cases
3610546bb2d7SKonstantin Belousov 		 * and skip any entries marked as in transition not by us.
3611e2e80fb3SKonstantin Belousov 		 *
3612e2e80fb3SKonstantin Belousov 		 * Another way to get an entry not marked with
3613e2e80fb3SKonstantin Belousov 		 * MAP_ENTRY_IN_TRANSITION is after failed clipping,
3614e2e80fb3SKonstantin Belousov 		 * which set rv to KERN_INVALID_ARGUMENT.
36150acea7dfSKonstantin Belousov 		 */
36160acea7dfSKonstantin Belousov 		if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 ||
36170acea7dfSKonstantin Belousov 		    entry->wiring_thread != curthread) {
3618e2e80fb3SKonstantin Belousov 			KASSERT(holes_ok || rv == KERN_INVALID_ARGUMENT,
36190acea7dfSKonstantin Belousov 			    ("vm_map_wire: !HOLESOK and new/changed entry"));
36200acea7dfSKonstantin Belousov 			continue;
36210acea7dfSKonstantin Belousov 		}
36220acea7dfSKonstantin Belousov 
3623b71f9b0dSDoug Moore 		if ((entry->eflags & MAP_ENTRY_WIRE_SKIPPED) != 0) {
3624b71f9b0dSDoug Moore 			/* do nothing */
3625b71f9b0dSDoug Moore 		} else if (rv == KERN_SUCCESS) {
362612d7cc84SAlan Cox 			if (user_wire)
362712d7cc84SAlan Cox 				entry->eflags |= MAP_ENTRY_USER_WIRED;
362828c58286SAlan Cox 		} else if (entry->wired_count == -1) {
362928c58286SAlan Cox 			/*
363028c58286SAlan Cox 			 * Wiring failed on this entry.  Thus, unwiring is
363128c58286SAlan Cox 			 * unnecessary.
363228c58286SAlan Cox 			 */
363328c58286SAlan Cox 			entry->wired_count = 0;
363403462509SAlan Cox 		} else if (!user_wire ||
363503462509SAlan Cox 		    (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
363666cd575bSAlan Cox 			/*
363766cd575bSAlan Cox 			 * Undo the wiring.  Wiring succeeded on this entry
363866cd575bSAlan Cox 			 * but failed on a later entry.
363966cd575bSAlan Cox 			 */
364054a3a114SMark Johnston 			if (entry->wired_count == 1) {
364103462509SAlan Cox 				vm_map_entry_unwire(map, entry);
364254a3a114SMark Johnston 				if (user_wire)
364354a3a114SMark Johnston 					vm_map_wire_user_count_sub(
364454a3a114SMark Johnston 					    atop(entry->end - entry->start));
364554a3a114SMark Johnston 			} else
364612d7cc84SAlan Cox 				entry->wired_count--;
364712d7cc84SAlan Cox 		}
36480acea7dfSKonstantin Belousov 		KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0,
36490acea7dfSKonstantin Belousov 		    ("vm_map_wire: in-transition flag missing %p", entry));
36500acea7dfSKonstantin Belousov 		KASSERT(entry->wiring_thread == curthread,
36510acea7dfSKonstantin Belousov 		    ("vm_map_wire: alien wire %p", entry));
36520acea7dfSKonstantin Belousov 		entry->eflags &= ~(MAP_ENTRY_IN_TRANSITION |
36530acea7dfSKonstantin Belousov 		    MAP_ENTRY_WIRE_SKIPPED);
36540acea7dfSKonstantin Belousov 		entry->wiring_thread = NULL;
365512d7cc84SAlan Cox 		if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
365612d7cc84SAlan Cox 			entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
36579a0cdf94SDoug Moore 			need_wakeup = true;
365812d7cc84SAlan Cox 		}
365983704cc2SDoug Moore 		vm_map_try_merge_entries(map, prev_entry, entry);
366012d7cc84SAlan Cox 	}
366183704cc2SDoug Moore 	vm_map_try_merge_entries(map, prev_entry, entry);
366212d7cc84SAlan Cox 	if (need_wakeup)
366312d7cc84SAlan Cox 		vm_map_wakeup(map);
366412d7cc84SAlan Cox 	return (rv);
3665e27e17b7SAlan Cox }
3666e27e17b7SAlan Cox 
3667e27e17b7SAlan Cox /*
3668950f8459SAlan Cox  * vm_map_sync
3669df8bae1dSRodney W. Grimes  *
3670df8bae1dSRodney W. Grimes  * Push any dirty cached pages in the address range to their pager.
3671df8bae1dSRodney W. Grimes  * If syncio is TRUE, dirty pages are written synchronously.
3672df8bae1dSRodney W. Grimes  * If invalidate is TRUE, any cached pages are freed as well.
3673df8bae1dSRodney W. Grimes  *
3674637315edSAlan Cox  * If the size of the region from start to end is zero, we are
3675637315edSAlan Cox  * supposed to flush all modified pages within the region containing
3676637315edSAlan Cox  * start.  Unfortunately, a region can be split or coalesced with
3677637315edSAlan Cox  * neighboring regions, making it difficult to determine what the
3678637315edSAlan Cox  * original region was.  Therefore, we approximate this requirement by
3679637315edSAlan Cox  * flushing the current region containing start.
3680637315edSAlan Cox  *
3681df8bae1dSRodney W. Grimes  * Returns an error if any part of the specified range is not mapped.
3682df8bae1dSRodney W. Grimes  */
3683df8bae1dSRodney W. Grimes int
3684950f8459SAlan Cox vm_map_sync(
36851b40f8c0SMatthew Dillon 	vm_map_t map,
36861b40f8c0SMatthew Dillon 	vm_offset_t start,
36871b40f8c0SMatthew Dillon 	vm_offset_t end,
36881b40f8c0SMatthew Dillon 	boolean_t syncio,
36891b40f8c0SMatthew Dillon 	boolean_t invalidate)
3690df8bae1dSRodney W. Grimes {
36912767c9f3SDoug Moore 	vm_map_entry_t entry, first_entry, next_entry;
3692df8bae1dSRodney W. Grimes 	vm_size_t size;
3693df8bae1dSRodney W. Grimes 	vm_object_t object;
3694a316d390SJohn Dyson 	vm_ooffset_t offset;
3695e53fa61bSKonstantin Belousov 	unsigned int last_timestamp;
3696e2e80fb3SKonstantin Belousov 	int bdry_idx;
3697126d6082SKonstantin Belousov 	boolean_t failed;
3698df8bae1dSRodney W. Grimes 
3699df8bae1dSRodney W. Grimes 	vm_map_lock_read(map);
3700df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
37012767c9f3SDoug Moore 	if (!vm_map_lookup_entry(map, start, &first_entry)) {
3702df8bae1dSRodney W. Grimes 		vm_map_unlock_read(map);
3703df8bae1dSRodney W. Grimes 		return (KERN_INVALID_ADDRESS);
3704d1d3f7e1SDoug Moore 	} else if (start == end) {
37052767c9f3SDoug Moore 		start = first_entry->start;
37062767c9f3SDoug Moore 		end = first_entry->end;
3707df8bae1dSRodney W. Grimes 	}
3708e2e80fb3SKonstantin Belousov 
3709df8bae1dSRodney W. Grimes 	/*
3710e2e80fb3SKonstantin Belousov 	 * Make a first pass to check for user-wired memory, holes,
3711e2e80fb3SKonstantin Belousov 	 * and partial invalidation of largepage mappings.
3712df8bae1dSRodney W. Grimes 	 */
37132767c9f3SDoug Moore 	for (entry = first_entry; entry->start < end; entry = next_entry) {
3714e2e80fb3SKonstantin Belousov 		if (invalidate) {
3715e2e80fb3SKonstantin Belousov 			if ((entry->eflags & MAP_ENTRY_USER_WIRED) != 0) {
3716df8bae1dSRodney W. Grimes 				vm_map_unlock_read(map);
3717df8bae1dSRodney W. Grimes 				return (KERN_INVALID_ARGUMENT);
3718df8bae1dSRodney W. Grimes 			}
3719e2e80fb3SKonstantin Belousov 			bdry_idx = (entry->eflags &
3720e2e80fb3SKonstantin Belousov 			    MAP_ENTRY_SPLIT_BOUNDARY_MASK) >>
3721e2e80fb3SKonstantin Belousov 			    MAP_ENTRY_SPLIT_BOUNDARY_SHIFT;
3722e2e80fb3SKonstantin Belousov 			if (bdry_idx != 0 &&
3723e2e80fb3SKonstantin Belousov 			    ((start & (pagesizes[bdry_idx] - 1)) != 0 ||
3724e2e80fb3SKonstantin Belousov 			    (end & (pagesizes[bdry_idx] - 1)) != 0)) {
3725e2e80fb3SKonstantin Belousov 				vm_map_unlock_read(map);
3726e2e80fb3SKonstantin Belousov 				return (KERN_INVALID_ARGUMENT);
3727e2e80fb3SKonstantin Belousov 			}
3728e2e80fb3SKonstantin Belousov 		}
37292767c9f3SDoug Moore 		next_entry = vm_map_entry_succ(entry);
37302767c9f3SDoug Moore 		if (end > entry->end &&
37312767c9f3SDoug Moore 		    entry->end != next_entry->start) {
3732df8bae1dSRodney W. Grimes 			vm_map_unlock_read(map);
3733df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ADDRESS);
3734df8bae1dSRodney W. Grimes 		}
3735df8bae1dSRodney W. Grimes 	}
3736df8bae1dSRodney W. Grimes 
37372cf13952SAlan Cox 	if (invalidate)
3738bc105a67SAlan Cox 		pmap_remove(map->pmap, start, end);
3739126d6082SKonstantin Belousov 	failed = FALSE;
37402cf13952SAlan Cox 
3741df8bae1dSRodney W. Grimes 	/*
3742df8bae1dSRodney W. Grimes 	 * Make a second pass, cleaning/uncaching pages from the indicated
3743df8bae1dSRodney W. Grimes 	 * objects as we go.
3744df8bae1dSRodney W. Grimes 	 */
37452767c9f3SDoug Moore 	for (entry = first_entry; entry->start < end;) {
37462767c9f3SDoug Moore 		offset = entry->offset + (start - entry->start);
37472767c9f3SDoug Moore 		size = (end <= entry->end ? end : entry->end) - start;
37482767c9f3SDoug Moore 		if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0) {
3749c0877f10SJohn Dyson 			vm_map_t smap;
3750df8bae1dSRodney W. Grimes 			vm_map_entry_t tentry;
3751df8bae1dSRodney W. Grimes 			vm_size_t tsize;
3752df8bae1dSRodney W. Grimes 
37532767c9f3SDoug Moore 			smap = entry->object.sub_map;
3754df8bae1dSRodney W. Grimes 			vm_map_lock_read(smap);
3755df8bae1dSRodney W. Grimes 			(void) vm_map_lookup_entry(smap, offset, &tentry);
3756df8bae1dSRodney W. Grimes 			tsize = tentry->end - offset;
3757df8bae1dSRodney W. Grimes 			if (tsize < size)
3758df8bae1dSRodney W. Grimes 				size = tsize;
3759df8bae1dSRodney W. Grimes 			object = tentry->object.vm_object;
3760df8bae1dSRodney W. Grimes 			offset = tentry->offset + (offset - tentry->start);
3761df8bae1dSRodney W. Grimes 			vm_map_unlock_read(smap);
3762df8bae1dSRodney W. Grimes 		} else {
37632767c9f3SDoug Moore 			object = entry->object.vm_object;
3764df8bae1dSRodney W. Grimes 		}
3765e53fa61bSKonstantin Belousov 		vm_object_reference(object);
3766e53fa61bSKonstantin Belousov 		last_timestamp = map->timestamp;
3767e53fa61bSKonstantin Belousov 		vm_map_unlock_read(map);
3768126d6082SKonstantin Belousov 		if (!vm_object_sync(object, offset, size, syncio, invalidate))
3769126d6082SKonstantin Belousov 			failed = TRUE;
3770df8bae1dSRodney W. Grimes 		start += size;
3771e53fa61bSKonstantin Belousov 		vm_object_deallocate(object);
3772e53fa61bSKonstantin Belousov 		vm_map_lock_read(map);
3773d1d3f7e1SDoug Moore 		if (last_timestamp == map->timestamp ||
37742767c9f3SDoug Moore 		    !vm_map_lookup_entry(map, start, &entry))
37752767c9f3SDoug Moore 			entry = vm_map_entry_succ(entry);
3776df8bae1dSRodney W. Grimes 	}
3777df8bae1dSRodney W. Grimes 
3778df8bae1dSRodney W. Grimes 	vm_map_unlock_read(map);
3779126d6082SKonstantin Belousov 	return (failed ? KERN_FAILURE : KERN_SUCCESS);
3780df8bae1dSRodney W. Grimes }
3781df8bae1dSRodney W. Grimes 
3782df8bae1dSRodney W. Grimes /*
3783df8bae1dSRodney W. Grimes  *	vm_map_entry_unwire:	[ internal use only ]
3784df8bae1dSRodney W. Grimes  *
3785df8bae1dSRodney W. Grimes  *	Make the region specified by this entry pageable.
3786df8bae1dSRodney W. Grimes  *
3787df8bae1dSRodney W. Grimes  *	The map in question should be locked.
3788df8bae1dSRodney W. Grimes  *	[This is the reason for this routine's existence.]
3789df8bae1dSRodney W. Grimes  */
37900362d7d7SJohn Dyson static void
37911b40f8c0SMatthew Dillon vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
3792df8bae1dSRodney W. Grimes {
379354a3a114SMark Johnston 	vm_size_t size;
379403462509SAlan Cox 
379503462509SAlan Cox 	VM_MAP_ASSERT_LOCKED(map);
379603462509SAlan Cox 	KASSERT(entry->wired_count > 0,
379703462509SAlan Cox 	    ("vm_map_entry_unwire: entry %p isn't wired", entry));
379854a3a114SMark Johnston 
379954a3a114SMark Johnston 	size = entry->end - entry->start;
380054a3a114SMark Johnston 	if ((entry->eflags & MAP_ENTRY_USER_WIRED) != 0)
380154a3a114SMark Johnston 		vm_map_wire_user_count_sub(atop(size));
380203462509SAlan Cox 	pmap_unwire(map->pmap, entry->start, entry->end);
380354a3a114SMark Johnston 	vm_object_unwire(entry->object.vm_object, entry->offset, size,
380454a3a114SMark Johnston 	    PQ_ACTIVE);
3805df8bae1dSRodney W. Grimes 	entry->wired_count = 0;
3806df8bae1dSRodney W. Grimes }
3807df8bae1dSRodney W. Grimes 
38080b367bd8SKonstantin Belousov static void
38090b367bd8SKonstantin Belousov vm_map_entry_deallocate(vm_map_entry_t entry, boolean_t system_map)
38100b367bd8SKonstantin Belousov {
38110b367bd8SKonstantin Belousov 
38120b367bd8SKonstantin Belousov 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0)
38130b367bd8SKonstantin Belousov 		vm_object_deallocate(entry->object.vm_object);
38140b367bd8SKonstantin Belousov 	uma_zfree(system_map ? kmapentzone : mapentzone, entry);
38150b367bd8SKonstantin Belousov }
38160b367bd8SKonstantin Belousov 
3817df8bae1dSRodney W. Grimes /*
3818df8bae1dSRodney W. Grimes  *	vm_map_entry_delete:	[ internal use only ]
3819df8bae1dSRodney W. Grimes  *
3820df8bae1dSRodney W. Grimes  *	Deallocate the given entry from the target map.
3821df8bae1dSRodney W. Grimes  */
38220362d7d7SJohn Dyson static void
38231b40f8c0SMatthew Dillon vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry)
3824df8bae1dSRodney W. Grimes {
382532a89c32SAlan Cox 	vm_object_t object;
382684242cf6SMark Johnston 	vm_pindex_t offidxstart, offidxend, size1;
3827d1780e8dSKonstantin Belousov 	vm_size_t size;
382832a89c32SAlan Cox 
38299f701172SKonstantin Belousov 	vm_map_entry_unlink(map, entry, UNLINK_MERGE_NONE);
38303364c323SKonstantin Belousov 	object = entry->object.vm_object;
383119bd0d9cSKonstantin Belousov 
383219bd0d9cSKonstantin Belousov 	if ((entry->eflags & MAP_ENTRY_GUARD) != 0) {
383319bd0d9cSKonstantin Belousov 		MPASS(entry->cred == NULL);
383419bd0d9cSKonstantin Belousov 		MPASS((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0);
383519bd0d9cSKonstantin Belousov 		MPASS(object == NULL);
383619bd0d9cSKonstantin Belousov 		vm_map_entry_deallocate(entry, map->system_map);
383719bd0d9cSKonstantin Belousov 		return;
383819bd0d9cSKonstantin Belousov 	}
383919bd0d9cSKonstantin Belousov 
38403364c323SKonstantin Belousov 	size = entry->end - entry->start;
38413364c323SKonstantin Belousov 	map->size -= size;
38423364c323SKonstantin Belousov 
3843ef694c1aSEdward Tomasz Napierala 	if (entry->cred != NULL) {
3844ef694c1aSEdward Tomasz Napierala 		swap_release_by_cred(size, entry->cred);
3845ef694c1aSEdward Tomasz Napierala 		crfree(entry->cred);
38463364c323SKonstantin Belousov 	}
3847df8bae1dSRodney W. Grimes 
384863967687SJeff Roberson 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0 || object == NULL) {
384963967687SJeff Roberson 		entry->object.vm_object = NULL;
385063967687SJeff Roberson 	} else if ((object->flags & OBJ_ANON) != 0 ||
385163967687SJeff Roberson 	    object == kernel_object) {
3852ef694c1aSEdward Tomasz Napierala 		KASSERT(entry->cred == NULL || object->cred == NULL ||
38533364c323SKonstantin Belousov 		    (entry->eflags & MAP_ENTRY_NEEDS_COPY),
3854ef694c1aSEdward Tomasz Napierala 		    ("OVERCOMMIT vm_map_entry_delete: both cred %p", entry));
385532a89c32SAlan Cox 		offidxstart = OFF_TO_IDX(entry->offset);
385684242cf6SMark Johnston 		offidxend = offidxstart + atop(size);
385789f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
385863967687SJeff Roberson 		if (object->ref_count != 1 &&
385963967687SJeff Roberson 		    ((object->flags & OBJ_ONEMAPPING) != 0 ||
38602e47807cSJeff Roberson 		    object == kernel_object)) {
386132a89c32SAlan Cox 			vm_object_collapse(object);
38626bbee8e2SAlan Cox 
38636bbee8e2SAlan Cox 			/*
38646bbee8e2SAlan Cox 			 * The option OBJPR_NOTMAPPED can be passed here
38656bbee8e2SAlan Cox 			 * because vm_map_delete() already performed
38666bbee8e2SAlan Cox 			 * pmap_remove() on the only mapping to this range
38676bbee8e2SAlan Cox 			 * of pages.
38686bbee8e2SAlan Cox 			 */
38696bbee8e2SAlan Cox 			vm_object_page_remove(object, offidxstart, offidxend,
38706bbee8e2SAlan Cox 			    OBJPR_NOTMAPPED);
387132a89c32SAlan Cox 			if (offidxend >= object->size &&
38723364c323SKonstantin Belousov 			    offidxstart < object->size) {
38733364c323SKonstantin Belousov 				size1 = object->size;
387432a89c32SAlan Cox 				object->size = offidxstart;
3875ef694c1aSEdward Tomasz Napierala 				if (object->cred != NULL) {
38763364c323SKonstantin Belousov 					size1 -= object->size;
38773364c323SKonstantin Belousov 					KASSERT(object->charge >= ptoa(size1),
38789a4ee196SKonstantin Belousov 					    ("object %p charge < 0", object));
38799a4ee196SKonstantin Belousov 					swap_release_by_cred(ptoa(size1),
38809a4ee196SKonstantin Belousov 					    object->cred);
38813364c323SKonstantin Belousov 					object->charge -= ptoa(size1);
38823364c323SKonstantin Belousov 				}
38833364c323SKonstantin Belousov 			}
388432a89c32SAlan Cox 		}
388589f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
388663967687SJeff Roberson 	}
38870b367bd8SKonstantin Belousov 	if (map->system_map)
38880b367bd8SKonstantin Belousov 		vm_map_entry_deallocate(entry, TRUE);
38890b367bd8SKonstantin Belousov 	else {
38907cdcf863SDoug Moore 		entry->defer_next = curthread->td_map_def_user;
38910b367bd8SKonstantin Belousov 		curthread->td_map_def_user = entry;
38920b367bd8SKonstantin Belousov 	}
3893df8bae1dSRodney W. Grimes }
3894df8bae1dSRodney W. Grimes 
3895df8bae1dSRodney W. Grimes /*
3896df8bae1dSRodney W. Grimes  *	vm_map_delete:	[ internal use only ]
3897df8bae1dSRodney W. Grimes  *
3898df8bae1dSRodney W. Grimes  *	Deallocates the given address range from the target
3899df8bae1dSRodney W. Grimes  *	map.
3900df8bae1dSRodney W. Grimes  */
3901df8bae1dSRodney W. Grimes int
3902655c3490SKonstantin Belousov vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end)
3903df8bae1dSRodney W. Grimes {
3904e2e80fb3SKonstantin Belousov 	vm_map_entry_t entry, next_entry, scratch_entry;
3905e2e80fb3SKonstantin Belousov 	int rv;
3906df8bae1dSRodney W. Grimes 
39073a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
39088a64110eSConrad Meyer 
390979e9451fSKonstantin Belousov 	if (start == end)
391079e9451fSKonstantin Belousov 		return (KERN_SUCCESS);
39113a0916b8SKonstantin Belousov 
3912df8bae1dSRodney W. Grimes 	/*
3913c7b23459SDoug Moore 	 * Find the start of the region, and clip it.
3914c7b23459SDoug Moore 	 * Step through all entries in this region.
3915df8bae1dSRodney W. Grimes 	 */
3916e2e80fb3SKonstantin Belousov 	rv = vm_map_lookup_clip_start(map, start, &entry, &scratch_entry);
3917e2e80fb3SKonstantin Belousov 	if (rv != KERN_SUCCESS)
3918e2e80fb3SKonstantin Belousov 		return (rv);
3919e2e80fb3SKonstantin Belousov 	for (; entry->start < end; entry = next_entry) {
392073b2baceSAlan Cox 		/*
392173b2baceSAlan Cox 		 * Wait for wiring or unwiring of an entry to complete.
39227c938963SBrian Feldman 		 * Also wait for any system wirings to disappear on
39237c938963SBrian Feldman 		 * user maps.
392473b2baceSAlan Cox 		 */
39257c938963SBrian Feldman 		if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 ||
39267c938963SBrian Feldman 		    (vm_map_pmap(map) != kernel_pmap &&
39277c938963SBrian Feldman 		    vm_map_entry_system_wired_count(entry) != 0)) {
392873b2baceSAlan Cox 			unsigned int last_timestamp;
392973b2baceSAlan Cox 			vm_offset_t saved_start;
393073b2baceSAlan Cox 
393173b2baceSAlan Cox 			saved_start = entry->start;
393273b2baceSAlan Cox 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
393373b2baceSAlan Cox 			last_timestamp = map->timestamp;
39348ce2d00aSPawel Jakub Dawidek 			(void) vm_map_unlock_and_wait(map, 0);
393573b2baceSAlan Cox 			vm_map_lock(map);
3936d1d3f7e1SDoug Moore 			if (last_timestamp + 1 != map->timestamp) {
393773b2baceSAlan Cox 				/*
393873b2baceSAlan Cox 				 * Look again for the entry because the map was
3939d1d3f7e1SDoug Moore 				 * modified while it was unlocked.
3940d1d3f7e1SDoug Moore 				 * Specifically, the entry may have been
3941d1d3f7e1SDoug Moore 				 * clipped, merged, or deleted.
394273b2baceSAlan Cox 				 */
3943e2e80fb3SKonstantin Belousov 				rv = vm_map_lookup_clip_start(map, saved_start,
3944e2e80fb3SKonstantin Belousov 				    &next_entry, &scratch_entry);
3945e2e80fb3SKonstantin Belousov 				if (rv != KERN_SUCCESS)
3946e2e80fb3SKonstantin Belousov 					break;
3947c7b23459SDoug Moore 			} else
3948c7b23459SDoug Moore 				next_entry = entry;
394973b2baceSAlan Cox 			continue;
395073b2baceSAlan Cox 		}
3951e2e80fb3SKonstantin Belousov 
3952e2e80fb3SKonstantin Belousov 		/* XXXKIB or delete to the upper superpage boundary ? */
3953e2e80fb3SKonstantin Belousov 		rv = vm_map_clip_end(map, entry, end);
3954e2e80fb3SKonstantin Belousov 		if (rv != KERN_SUCCESS)
3955e2e80fb3SKonstantin Belousov 			break;
3956c7b23459SDoug Moore 		next_entry = vm_map_entry_succ(entry);
3957df8bae1dSRodney W. Grimes 
3958df8bae1dSRodney W. Grimes 		/*
39590d94caffSDavid Greenman 		 * Unwire before removing addresses from the pmap; otherwise,
39600d94caffSDavid Greenman 		 * unwiring will put the entries back in the pmap.
3961df8bae1dSRodney W. Grimes 		 */
3962be7be412SKonstantin Belousov 		if (entry->wired_count != 0)
3963df8bae1dSRodney W. Grimes 			vm_map_entry_unwire(map, entry);
3964df8bae1dSRodney W. Grimes 
396532f0fefcSKonstantin Belousov 		/*
396632f0fefcSKonstantin Belousov 		 * Remove mappings for the pages, but only if the
396732f0fefcSKonstantin Belousov 		 * mappings could exist.  For instance, it does not
396832f0fefcSKonstantin Belousov 		 * make sense to call pmap_remove() for guard entries.
396932f0fefcSKonstantin Belousov 		 */
397032f0fefcSKonstantin Belousov 		if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0 ||
397132f0fefcSKonstantin Belousov 		    entry->object.vm_object != NULL)
397232a89c32SAlan Cox 			pmap_remove(map->pmap, entry->start, entry->end);
3973df8bae1dSRodney W. Grimes 
3974fa50a355SKonstantin Belousov 		if (entry->end == map->anon_loc)
3975fa50a355SKonstantin Belousov 			map->anon_loc = entry->start;
3976fa50a355SKonstantin Belousov 
3977df8bae1dSRodney W. Grimes 		/*
3978e608cc3cSKonstantin Belousov 		 * Delete the entry only after removing all pmap
3979e608cc3cSKonstantin Belousov 		 * entries pointing to its pages.  (Otherwise, its
3980e608cc3cSKonstantin Belousov 		 * page frames may be reallocated, and any modify bits
3981e608cc3cSKonstantin Belousov 		 * will be set in the wrong object!)
3982df8bae1dSRodney W. Grimes 		 */
3983df8bae1dSRodney W. Grimes 		vm_map_entry_delete(map, entry);
3984df8bae1dSRodney W. Grimes 	}
3985e2e80fb3SKonstantin Belousov 	return (rv);
3986df8bae1dSRodney W. Grimes }
3987df8bae1dSRodney W. Grimes 
3988df8bae1dSRodney W. Grimes /*
3989df8bae1dSRodney W. Grimes  *	vm_map_remove:
3990df8bae1dSRodney W. Grimes  *
3991df8bae1dSRodney W. Grimes  *	Remove the given address range from the target map.
3992df8bae1dSRodney W. Grimes  *	This is the exported form of vm_map_delete.
3993df8bae1dSRodney W. Grimes  */
3994df8bae1dSRodney W. Grimes int
39951b40f8c0SMatthew Dillon vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
3996df8bae1dSRodney W. Grimes {
39976eaee3feSAlan Cox 	int result;
3998df8bae1dSRodney W. Grimes 
3999df8bae1dSRodney W. Grimes 	vm_map_lock(map);
4000df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
4001655c3490SKonstantin Belousov 	result = vm_map_delete(map, start, end);
4002df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
4003df8bae1dSRodney W. Grimes 	return (result);
4004df8bae1dSRodney W. Grimes }
4005df8bae1dSRodney W. Grimes 
4006df8bae1dSRodney W. Grimes /*
4007df8bae1dSRodney W. Grimes  *	vm_map_check_protection:
4008df8bae1dSRodney W. Grimes  *
40092d5c7e45SMatthew Dillon  *	Assert that the target map allows the specified privilege on the
40102d5c7e45SMatthew Dillon  *	entire address region given.  The entire region must be allocated.
40112d5c7e45SMatthew Dillon  *
40122d5c7e45SMatthew Dillon  *	WARNING!  This code does not and should not check whether the
40132d5c7e45SMatthew Dillon  *	contents of the region is accessible.  For example a smaller file
40142d5c7e45SMatthew Dillon  *	might be mapped into a larger address space.
40152d5c7e45SMatthew Dillon  *
40162d5c7e45SMatthew Dillon  *	NOTE!  This code is also called by munmap().
4017d8834602SAlan Cox  *
4018d8834602SAlan Cox  *	The map must be locked.  A read lock is sufficient.
4019df8bae1dSRodney W. Grimes  */
40200d94caffSDavid Greenman boolean_t
4021b9dcd593SBruce Evans vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
4022b9dcd593SBruce Evans 			vm_prot_t protection)
4023df8bae1dSRodney W. Grimes {
4024c0877f10SJohn Dyson 	vm_map_entry_t entry;
4025d1d3f7e1SDoug Moore 	vm_map_entry_t tmp_entry;
4026df8bae1dSRodney W. Grimes 
4027d1d3f7e1SDoug Moore 	if (!vm_map_lookup_entry(map, start, &tmp_entry))
4028df8bae1dSRodney W. Grimes 		return (FALSE);
4029d1d3f7e1SDoug Moore 	entry = tmp_entry;
4030df8bae1dSRodney W. Grimes 
4031df8bae1dSRodney W. Grimes 	while (start < end) {
4032df8bae1dSRodney W. Grimes 		/*
4033df8bae1dSRodney W. Grimes 		 * No holes allowed!
4034df8bae1dSRodney W. Grimes 		 */
4035d8834602SAlan Cox 		if (start < entry->start)
4036df8bae1dSRodney W. Grimes 			return (FALSE);
4037df8bae1dSRodney W. Grimes 		/*
4038df8bae1dSRodney W. Grimes 		 * Check protection associated with entry.
4039df8bae1dSRodney W. Grimes 		 */
4040d8834602SAlan Cox 		if ((entry->protection & protection) != protection)
4041df8bae1dSRodney W. Grimes 			return (FALSE);
4042df8bae1dSRodney W. Grimes 		/* go to next entry */
4043df8bae1dSRodney W. Grimes 		start = entry->end;
40447cdcf863SDoug Moore 		entry = vm_map_entry_succ(entry);
4045df8bae1dSRodney W. Grimes 	}
4046df8bae1dSRodney W. Grimes 	return (TRUE);
4047df8bae1dSRodney W. Grimes }
4048df8bae1dSRodney W. Grimes 
40494d987866SJeff Roberson /*
40504d987866SJeff Roberson  *
4051886b9021SJeff Roberson  *	vm_map_copy_swap_object:
40524d987866SJeff Roberson  *
4053886b9021SJeff Roberson  *	Copies a swap-backed object from an existing map entry to a
40544d987866SJeff Roberson  *	new one.  Carries forward the swap charge.  May change the
40554d987866SJeff Roberson  *	src object on return.
40564d987866SJeff Roberson  */
40574d987866SJeff Roberson static void
4058886b9021SJeff Roberson vm_map_copy_swap_object(vm_map_entry_t src_entry, vm_map_entry_t dst_entry,
40594d987866SJeff Roberson     vm_offset_t size, vm_ooffset_t *fork_charge)
40604d987866SJeff Roberson {
40614d987866SJeff Roberson 	vm_object_t src_object;
40624d987866SJeff Roberson 	struct ucred *cred;
40634d987866SJeff Roberson 	int charged;
40644d987866SJeff Roberson 
40654d987866SJeff Roberson 	src_object = src_entry->object.vm_object;
40664d987866SJeff Roberson 	charged = ENTRY_CHARGED(src_entry);
4067d966c761SJeff Roberson 	if ((src_object->flags & OBJ_ANON) != 0) {
4068d966c761SJeff Roberson 		VM_OBJECT_WLOCK(src_object);
40694d987866SJeff Roberson 		vm_object_collapse(src_object);
40704d987866SJeff Roberson 		if ((src_object->flags & OBJ_ONEMAPPING) != 0) {
40714d987866SJeff Roberson 			vm_object_split(src_entry);
40724d987866SJeff Roberson 			src_object = src_entry->object.vm_object;
40734d987866SJeff Roberson 		}
40744d987866SJeff Roberson 		vm_object_reference_locked(src_object);
40754d987866SJeff Roberson 		vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
4076d966c761SJeff Roberson 		VM_OBJECT_WUNLOCK(src_object);
4077d966c761SJeff Roberson 	} else
4078d966c761SJeff Roberson 		vm_object_reference(src_object);
40794d987866SJeff Roberson 	if (src_entry->cred != NULL &&
40804d987866SJeff Roberson 	    !(src_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
40814d987866SJeff Roberson 		KASSERT(src_object->cred == NULL,
40824d987866SJeff Roberson 		    ("OVERCOMMIT: vm_map_copy_anon_entry: cred %p",
40834d987866SJeff Roberson 		     src_object));
40844d987866SJeff Roberson 		src_object->cred = src_entry->cred;
40854d987866SJeff Roberson 		src_object->charge = size;
40864d987866SJeff Roberson 	}
40874d987866SJeff Roberson 	dst_entry->object.vm_object = src_object;
40884d987866SJeff Roberson 	if (charged) {
40894d987866SJeff Roberson 		cred = curthread->td_ucred;
40904d987866SJeff Roberson 		crhold(cred);
40914d987866SJeff Roberson 		dst_entry->cred = cred;
40924d987866SJeff Roberson 		*fork_charge += size;
40934d987866SJeff Roberson 		if (!(src_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
40944d987866SJeff Roberson 			crhold(cred);
40954d987866SJeff Roberson 			src_entry->cred = cred;
40964d987866SJeff Roberson 			*fork_charge += size;
40974d987866SJeff Roberson 		}
40984d987866SJeff Roberson 	}
40994d987866SJeff Roberson }
41004d987866SJeff Roberson 
410186524867SJohn Dyson /*
4102df8bae1dSRodney W. Grimes  *	vm_map_copy_entry:
4103df8bae1dSRodney W. Grimes  *
4104df8bae1dSRodney W. Grimes  *	Copies the contents of the source entry to the destination
4105df8bae1dSRodney W. Grimes  *	entry.  The entries *must* be aligned properly.
4106df8bae1dSRodney W. Grimes  */
4107f708ef1bSPoul-Henning Kamp static void
41081b40f8c0SMatthew Dillon vm_map_copy_entry(
41091b40f8c0SMatthew Dillon 	vm_map_t src_map,
41101b40f8c0SMatthew Dillon 	vm_map_t dst_map,
41111b40f8c0SMatthew Dillon 	vm_map_entry_t src_entry,
41123364c323SKonstantin Belousov 	vm_map_entry_t dst_entry,
41133364c323SKonstantin Belousov 	vm_ooffset_t *fork_charge)
4114df8bae1dSRodney W. Grimes {
4115c0877f10SJohn Dyson 	vm_object_t src_object;
411684110e7eSKonstantin Belousov 	vm_map_entry_t fake_entry;
41173364c323SKonstantin Belousov 	vm_offset_t size;
4118c0877f10SJohn Dyson 
41193a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(dst_map);
41203a0916b8SKonstantin Belousov 
41219fdfe602SMatthew Dillon 	if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
4122df8bae1dSRodney W. Grimes 		return;
4123df8bae1dSRodney W. Grimes 
4124afaa41f6SAlan Cox 	if (src_entry->wired_count == 0 ||
4125afaa41f6SAlan Cox 	    (src_entry->protection & VM_PROT_WRITE) == 0) {
4126df8bae1dSRodney W. Grimes 		/*
41270d94caffSDavid Greenman 		 * If the source entry is marked needs_copy, it is already
41280d94caffSDavid Greenman 		 * write-protected.
4129df8bae1dSRodney W. Grimes 		 */
4130d9a9209aSAlan Cox 		if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0 &&
4131d9a9209aSAlan Cox 		    (src_entry->protection & VM_PROT_WRITE) != 0) {
4132df8bae1dSRodney W. Grimes 			pmap_protect(src_map->pmap,
4133df8bae1dSRodney W. Grimes 			    src_entry->start,
4134df8bae1dSRodney W. Grimes 			    src_entry->end,
4135df8bae1dSRodney W. Grimes 			    src_entry->protection & ~VM_PROT_WRITE);
4136df8bae1dSRodney W. Grimes 		}
4137b18bfc3dSJohn Dyson 
4138df8bae1dSRodney W. Grimes 		/*
4139df8bae1dSRodney W. Grimes 		 * Make a copy of the object.
4140df8bae1dSRodney W. Grimes 		 */
41413364c323SKonstantin Belousov 		size = src_entry->end - src_entry->start;
41428aef1712SMatthew Dillon 		if ((src_object = src_entry->object.vm_object) != NULL) {
4143886b9021SJeff Roberson 			if (src_object->type == OBJT_DEFAULT ||
41444b8365d7SKonstantin Belousov 			    (src_object->flags & OBJ_SWAP) != 0) {
4145886b9021SJeff Roberson 				vm_map_copy_swap_object(src_entry, dst_entry,
41464d987866SJeff Roberson 				    size, fork_charge);
41474d987866SJeff Roberson 				/* May have split/collapsed, reload obj. */
41484d987866SJeff Roberson 				src_object = src_entry->object.vm_object;
41494d987866SJeff Roberson 			} else {
41504d987866SJeff Roberson 				vm_object_reference(src_object);
4151c0877f10SJohn Dyson 				dst_entry->object.vm_object = src_object;
41523364c323SKonstantin Belousov 			}
41539a4ee196SKonstantin Belousov 			src_entry->eflags |= MAP_ENTRY_COW |
41549a4ee196SKonstantin Belousov 			    MAP_ENTRY_NEEDS_COPY;
41559a4ee196SKonstantin Belousov 			dst_entry->eflags |= MAP_ENTRY_COW |
41569a4ee196SKonstantin Belousov 			    MAP_ENTRY_NEEDS_COPY;
4157b18bfc3dSJohn Dyson 			dst_entry->offset = src_entry->offset;
4158fe7bcbafSKyle Evans 			if (src_entry->eflags & MAP_ENTRY_WRITECNT) {
415984110e7eSKonstantin Belousov 				/*
4160fe7bcbafSKyle Evans 				 * MAP_ENTRY_WRITECNT cannot
416184110e7eSKonstantin Belousov 				 * indicate write reference from
416284110e7eSKonstantin Belousov 				 * src_entry, since the entry is
416384110e7eSKonstantin Belousov 				 * marked as needs copy.  Allocate a
416484110e7eSKonstantin Belousov 				 * fake entry that is used to
4165fe7bcbafSKyle Evans 				 * decrement object->un_pager writecount
416684110e7eSKonstantin Belousov 				 * at the appropriate time.  Attach
416784110e7eSKonstantin Belousov 				 * fake_entry to the deferred list.
416884110e7eSKonstantin Belousov 				 */
416984110e7eSKonstantin Belousov 				fake_entry = vm_map_entry_create(dst_map);
4170fe7bcbafSKyle Evans 				fake_entry->eflags = MAP_ENTRY_WRITECNT;
4171fe7bcbafSKyle Evans 				src_entry->eflags &= ~MAP_ENTRY_WRITECNT;
417284110e7eSKonstantin Belousov 				vm_object_reference(src_object);
417384110e7eSKonstantin Belousov 				fake_entry->object.vm_object = src_object;
417484110e7eSKonstantin Belousov 				fake_entry->start = src_entry->start;
417584110e7eSKonstantin Belousov 				fake_entry->end = src_entry->end;
41767cdcf863SDoug Moore 				fake_entry->defer_next =
41777cdcf863SDoug Moore 				    curthread->td_map_def_user;
417884110e7eSKonstantin Belousov 				curthread->td_map_def_user = fake_entry;
417984110e7eSKonstantin Belousov 			}
41800ec97ffcSKonstantin Belousov 
41810ec97ffcSKonstantin Belousov 			pmap_copy(dst_map->pmap, src_map->pmap,
41820ec97ffcSKonstantin Belousov 			    dst_entry->start, dst_entry->end - dst_entry->start,
41830ec97ffcSKonstantin Belousov 			    src_entry->start);
4184b18bfc3dSJohn Dyson 		} else {
4185b18bfc3dSJohn Dyson 			dst_entry->object.vm_object = NULL;
4186b18bfc3dSJohn Dyson 			dst_entry->offset = 0;
4187ef694c1aSEdward Tomasz Napierala 			if (src_entry->cred != NULL) {
4188ef694c1aSEdward Tomasz Napierala 				dst_entry->cred = curthread->td_ucred;
4189ef694c1aSEdward Tomasz Napierala 				crhold(dst_entry->cred);
41903364c323SKonstantin Belousov 				*fork_charge += size;
41913364c323SKonstantin Belousov 			}
4192b18bfc3dSJohn Dyson 		}
41930d94caffSDavid Greenman 	} else {
4194df8bae1dSRodney W. Grimes 		/*
4195afaa41f6SAlan Cox 		 * We don't want to make writeable wired pages copy-on-write.
4196afaa41f6SAlan Cox 		 * Immediately copy these pages into the new map by simulating
4197afaa41f6SAlan Cox 		 * page faults.  The new pages are pageable.
4198df8bae1dSRodney W. Grimes 		 */
4199121fd461SKonstantin Belousov 		vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry,
4200121fd461SKonstantin Belousov 		    fork_charge);
4201df8bae1dSRodney W. Grimes 	}
4202df8bae1dSRodney W. Grimes }
4203df8bae1dSRodney W. Grimes 
4204df8bae1dSRodney W. Grimes /*
42052a7be1b6SBrian Feldman  * vmspace_map_entry_forked:
42062a7be1b6SBrian Feldman  * Update the newly-forked vmspace each time a map entry is inherited
42072a7be1b6SBrian Feldman  * or copied.  The values for vm_dsize and vm_tsize are approximate
42082a7be1b6SBrian Feldman  * (and mostly-obsolete ideas in the face of mmap(2) et al.)
42092a7be1b6SBrian Feldman  */
42102a7be1b6SBrian Feldman static void
42112a7be1b6SBrian Feldman vmspace_map_entry_forked(const struct vmspace *vm1, struct vmspace *vm2,
42122a7be1b6SBrian Feldman     vm_map_entry_t entry)
42132a7be1b6SBrian Feldman {
42142a7be1b6SBrian Feldman 	vm_size_t entrysize;
42152a7be1b6SBrian Feldman 	vm_offset_t newend;
42162a7be1b6SBrian Feldman 
421719bd0d9cSKonstantin Belousov 	if ((entry->eflags & MAP_ENTRY_GUARD) != 0)
421819bd0d9cSKonstantin Belousov 		return;
42192a7be1b6SBrian Feldman 	entrysize = entry->end - entry->start;
42202a7be1b6SBrian Feldman 	vm2->vm_map.size += entrysize;
42212a7be1b6SBrian Feldman 	if (entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP)) {
42222a7be1b6SBrian Feldman 		vm2->vm_ssize += btoc(entrysize);
42232a7be1b6SBrian Feldman 	} else if (entry->start >= (vm_offset_t)vm1->vm_daddr &&
42242a7be1b6SBrian Feldman 	    entry->start < (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)) {
4225b351299cSAndrew Gallatin 		newend = MIN(entry->end,
42262a7be1b6SBrian Feldman 		    (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize));
42272a7be1b6SBrian Feldman 		vm2->vm_dsize += btoc(newend - entry->start);
42282a7be1b6SBrian Feldman 	} else if (entry->start >= (vm_offset_t)vm1->vm_taddr &&
42292a7be1b6SBrian Feldman 	    entry->start < (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)) {
4230b351299cSAndrew Gallatin 		newend = MIN(entry->end,
42312a7be1b6SBrian Feldman 		    (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize));
42322a7be1b6SBrian Feldman 		vm2->vm_tsize += btoc(newend - entry->start);
42332a7be1b6SBrian Feldman 	}
42342a7be1b6SBrian Feldman }
42352a7be1b6SBrian Feldman 
42362a7be1b6SBrian Feldman /*
4237df8bae1dSRodney W. Grimes  * vmspace_fork:
4238df8bae1dSRodney W. Grimes  * Create a new process vmspace structure and vm_map
4239df8bae1dSRodney W. Grimes  * based on those of an existing process.  The new map
4240df8bae1dSRodney W. Grimes  * is based on the old map, according to the inheritance
4241df8bae1dSRodney W. Grimes  * values on the regions in that map.
4242df8bae1dSRodney W. Grimes  *
42432a7be1b6SBrian Feldman  * XXX It might be worth coalescing the entries added to the new vmspace.
42442a7be1b6SBrian Feldman  *
4245df8bae1dSRodney W. Grimes  * The source map must not be locked.
4246df8bae1dSRodney W. Grimes  */
4247df8bae1dSRodney W. Grimes struct vmspace *
42483364c323SKonstantin Belousov vmspace_fork(struct vmspace *vm1, vm_ooffset_t *fork_charge)
4249df8bae1dSRodney W. Grimes {
4250c0877f10SJohn Dyson 	struct vmspace *vm2;
425179e53838SAlan Cox 	vm_map_t new_map, old_map;
425279e53838SAlan Cox 	vm_map_entry_t new_entry, old_entry;
4253de5f6a77SJohn Dyson 	vm_object_t object;
4254e7a9df16SKonstantin Belousov 	int error, locked;
425519bd0d9cSKonstantin Belousov 	vm_inherit_t inh;
4256df8bae1dSRodney W. Grimes 
425779e53838SAlan Cox 	old_map = &vm1->vm_map;
425879e53838SAlan Cox 	/* Copy immutable fields of vm1 to vm2. */
42596e00f3a3SKonstantin Belousov 	vm2 = vmspace_alloc(vm_map_min(old_map), vm_map_max(old_map),
42606e00f3a3SKonstantin Belousov 	    pmap_pinit);
426189b57fcfSKonstantin Belousov 	if (vm2 == NULL)
426279e53838SAlan Cox 		return (NULL);
4263e7a9df16SKonstantin Belousov 
42642a7be1b6SBrian Feldman 	vm2->vm_taddr = vm1->vm_taddr;
42652a7be1b6SBrian Feldman 	vm2->vm_daddr = vm1->vm_daddr;
42662a7be1b6SBrian Feldman 	vm2->vm_maxsaddr = vm1->vm_maxsaddr;
4267889b56c8SDawid Gorecki 	vm2->vm_stkgap = vm1->vm_stkgap;
426879e53838SAlan Cox 	vm_map_lock(old_map);
426979e53838SAlan Cox 	if (old_map->busy)
427079e53838SAlan Cox 		vm_map_wait_busy(old_map);
427179e53838SAlan Cox 	new_map = &vm2->vm_map;
42721fac7d7fSKonstantin Belousov 	locked = vm_map_trylock(new_map); /* trylock to silence WITNESS */
42731fac7d7fSKonstantin Belousov 	KASSERT(locked, ("vmspace_fork: lock failed"));
4274df8bae1dSRodney W. Grimes 
4275e7a9df16SKonstantin Belousov 	error = pmap_vmspace_copy(new_map->pmap, old_map->pmap);
4276e7a9df16SKonstantin Belousov 	if (error != 0) {
4277e7a9df16SKonstantin Belousov 		sx_xunlock(&old_map->lock);
4278e7a9df16SKonstantin Belousov 		sx_xunlock(&new_map->lock);
4279e7a9df16SKonstantin Belousov 		vm_map_process_deferred();
4280e7a9df16SKonstantin Belousov 		vmspace_free(vm2);
4281e7a9df16SKonstantin Belousov 		return (NULL);
4282e7a9df16SKonstantin Belousov 	}
4283e7a9df16SKonstantin Belousov 
4284fa50a355SKonstantin Belousov 	new_map->anon_loc = old_map->anon_loc;
42859402bb44SKonstantin Belousov 	new_map->flags |= old_map->flags & (MAP_ASLR | MAP_ASLR_IGNSTART |
42869402bb44SKonstantin Belousov 	    MAP_WXORX);
4287e7a9df16SKonstantin Belousov 
42882767c9f3SDoug Moore 	VM_MAP_ENTRY_FOREACH(old_entry, old_map) {
42892767c9f3SDoug Moore 		if ((old_entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0)
4290df8bae1dSRodney W. Grimes 			panic("vm_map_fork: encountered a submap");
4291df8bae1dSRodney W. Grimes 
429219bd0d9cSKonstantin Belousov 		inh = old_entry->inheritance;
429319bd0d9cSKonstantin Belousov 		if ((old_entry->eflags & MAP_ENTRY_GUARD) != 0 &&
429419bd0d9cSKonstantin Belousov 		    inh != VM_INHERIT_NONE)
429519bd0d9cSKonstantin Belousov 			inh = VM_INHERIT_COPY;
429619bd0d9cSKonstantin Belousov 
429719bd0d9cSKonstantin Belousov 		switch (inh) {
4298df8bae1dSRodney W. Grimes 		case VM_INHERIT_NONE:
4299df8bae1dSRodney W. Grimes 			break;
4300df8bae1dSRodney W. Grimes 
4301df8bae1dSRodney W. Grimes 		case VM_INHERIT_SHARE:
4302df8bae1dSRodney W. Grimes 			/*
43032767c9f3SDoug Moore 			 * Clone the entry, creating the shared object if
43042767c9f3SDoug Moore 			 * necessary.
4305fed9a903SJohn Dyson 			 */
4306fed9a903SJohn Dyson 			object = old_entry->object.vm_object;
4307fed9a903SJohn Dyson 			if (object == NULL) {
4308af1d6d6aSDoug Moore 				vm_map_entry_back(old_entry);
4309af1d6d6aSDoug Moore 				object = old_entry->object.vm_object;
43109a2f6362SAlan Cox 			}
43119a2f6362SAlan Cox 
43129a2f6362SAlan Cox 			/*
43139a2f6362SAlan Cox 			 * Add the reference before calling vm_object_shadow
43149a2f6362SAlan Cox 			 * to insure that a shadow object is created.
43159a2f6362SAlan Cox 			 */
43169a2f6362SAlan Cox 			vm_object_reference(object);
43179a2f6362SAlan Cox 			if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
43185069bf57SJohn Dyson 				vm_object_shadow(&old_entry->object.vm_object,
43195069bf57SJohn Dyson 				    &old_entry->offset,
432067388836SKonstantin Belousov 				    old_entry->end - old_entry->start,
432167388836SKonstantin Belousov 				    old_entry->cred,
4322d30344bdSIan Dowse 				    /* Transfer the second reference too. */
432367388836SKonstantin Belousov 				    true);
432467388836SKonstantin Belousov 				old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
432567388836SKonstantin Belousov 				old_entry->cred = NULL;
43267fd10fb3SKonstantin Belousov 
43277fd10fb3SKonstantin Belousov 				/*
432883ea714fSDoug Moore 				 * As in vm_map_merged_neighbor_dispose(),
432983ea714fSDoug Moore 				 * the vnode lock will not be acquired in
43307fd10fb3SKonstantin Belousov 				 * this call to vm_object_deallocate().
43317fd10fb3SKonstantin Belousov 				 */
4332d30344bdSIan Dowse 				vm_object_deallocate(object);
43335069bf57SJohn Dyson 				object = old_entry->object.vm_object;
433467388836SKonstantin Belousov 			} else {
433589f6b863SAttilio Rao 				VM_OBJECT_WLOCK(object);
4336069e9bc1SDoug Rabson 				vm_object_clear_flag(object, OBJ_ONEMAPPING);
4337ef694c1aSEdward Tomasz Napierala 				if (old_entry->cred != NULL) {
433867388836SKonstantin Belousov 					KASSERT(object->cred == NULL,
433967388836SKonstantin Belousov 					    ("vmspace_fork both cred"));
4340ef694c1aSEdward Tomasz Napierala 					object->cred = old_entry->cred;
434167388836SKonstantin Belousov 					object->charge = old_entry->end -
434267388836SKonstantin Belousov 					    old_entry->start;
4343ef694c1aSEdward Tomasz Napierala 					old_entry->cred = NULL;
43443364c323SKonstantin Belousov 				}
4345b9781cf6SKonstantin Belousov 
4346b9781cf6SKonstantin Belousov 				/*
4347b9781cf6SKonstantin Belousov 				 * Assert the correct state of the vnode
4348b9781cf6SKonstantin Belousov 				 * v_writecount while the object is locked, to
4349b9781cf6SKonstantin Belousov 				 * not relock it later for the assertion
4350b9781cf6SKonstantin Belousov 				 * correctness.
4351b9781cf6SKonstantin Belousov 				 */
4352fe7bcbafSKyle Evans 				if (old_entry->eflags & MAP_ENTRY_WRITECNT &&
4353b9781cf6SKonstantin Belousov 				    object->type == OBJT_VNODE) {
435467388836SKonstantin Belousov 					KASSERT(((struct vnode *)object->
435567388836SKonstantin Belousov 					    handle)->v_writecount > 0,
435667388836SKonstantin Belousov 					    ("vmspace_fork: v_writecount %p",
435767388836SKonstantin Belousov 					    object));
435867388836SKonstantin Belousov 					KASSERT(object->un_pager.vnp.
435967388836SKonstantin Belousov 					    writemappings > 0,
4360b9781cf6SKonstantin Belousov 					    ("vmspace_fork: vnp.writecount %p",
4361b9781cf6SKonstantin Belousov 					    object));
4362b9781cf6SKonstantin Belousov 				}
436389f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(object);
436467388836SKonstantin Belousov 			}
4365fed9a903SJohn Dyson 
4366fed9a903SJohn Dyson 			/*
4367ad5fca3bSAlan Cox 			 * Clone the entry, referencing the shared object.
4368df8bae1dSRodney W. Grimes 			 */
4369df8bae1dSRodney W. Grimes 			new_entry = vm_map_entry_create(new_map);
4370df8bae1dSRodney W. Grimes 			*new_entry = *old_entry;
43719f6acfd1SKonstantin Belousov 			new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
43729f6acfd1SKonstantin Belousov 			    MAP_ENTRY_IN_TRANSITION);
43730acea7dfSKonstantin Belousov 			new_entry->wiring_thread = NULL;
4374df8bae1dSRodney W. Grimes 			new_entry->wired_count = 0;
4375fe7bcbafSKyle Evans 			if (new_entry->eflags & MAP_ENTRY_WRITECNT) {
4376fe7bcbafSKyle Evans 				vm_pager_update_writecount(object,
437784110e7eSKonstantin Belousov 				    new_entry->start, new_entry->end);
437884110e7eSKonstantin Belousov 			}
437978022527SKonstantin Belousov 			vm_map_entry_set_vnode_text(new_entry, true);
4380df8bae1dSRodney W. Grimes 
4381df8bae1dSRodney W. Grimes 			/*
43820d94caffSDavid Greenman 			 * Insert the entry into the new map -- we know we're
43830d94caffSDavid Greenman 			 * inserting at the end of the new map.
4384df8bae1dSRodney W. Grimes 			 */
43859f701172SKonstantin Belousov 			vm_map_entry_link(new_map, new_entry);
43862a7be1b6SBrian Feldman 			vmspace_map_entry_forked(vm1, vm2, new_entry);
4387df8bae1dSRodney W. Grimes 
4388df8bae1dSRodney W. Grimes 			/*
4389df8bae1dSRodney W. Grimes 			 * Update the physical map
4390df8bae1dSRodney W. Grimes 			 */
4391df8bae1dSRodney W. Grimes 			pmap_copy(new_map->pmap, old_map->pmap,
4392df8bae1dSRodney W. Grimes 			    new_entry->start,
4393df8bae1dSRodney W. Grimes 			    (old_entry->end - old_entry->start),
4394df8bae1dSRodney W. Grimes 			    old_entry->start);
4395df8bae1dSRodney W. Grimes 			break;
4396df8bae1dSRodney W. Grimes 
4397df8bae1dSRodney W. Grimes 		case VM_INHERIT_COPY:
4398df8bae1dSRodney W. Grimes 			/*
4399df8bae1dSRodney W. Grimes 			 * Clone the entry and link into the map.
4400df8bae1dSRodney W. Grimes 			 */
4401df8bae1dSRodney W. Grimes 			new_entry = vm_map_entry_create(new_map);
4402df8bae1dSRodney W. Grimes 			*new_entry = *old_entry;
440384110e7eSKonstantin Belousov 			/*
440484110e7eSKonstantin Belousov 			 * Copied entry is COW over the old object.
440584110e7eSKonstantin Belousov 			 */
44069f6acfd1SKonstantin Belousov 			new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
4407fe7bcbafSKyle Evans 			    MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_WRITECNT);
44080acea7dfSKonstantin Belousov 			new_entry->wiring_thread = NULL;
4409df8bae1dSRodney W. Grimes 			new_entry->wired_count = 0;
4410df8bae1dSRodney W. Grimes 			new_entry->object.vm_object = NULL;
4411ef694c1aSEdward Tomasz Napierala 			new_entry->cred = NULL;
44129f701172SKonstantin Belousov 			vm_map_entry_link(new_map, new_entry);
44132a7be1b6SBrian Feldman 			vmspace_map_entry_forked(vm1, vm2, new_entry);
4414bd7e5f99SJohn Dyson 			vm_map_copy_entry(old_map, new_map, old_entry,
44153364c323SKonstantin Belousov 			    new_entry, fork_charge);
441678022527SKonstantin Belousov 			vm_map_entry_set_vnode_text(new_entry, true);
4417df8bae1dSRodney W. Grimes 			break;
441878d7964bSXin LI 
441978d7964bSXin LI 		case VM_INHERIT_ZERO:
442078d7964bSXin LI 			/*
442178d7964bSXin LI 			 * Create a new anonymous mapping entry modelled from
442278d7964bSXin LI 			 * the old one.
442378d7964bSXin LI 			 */
442478d7964bSXin LI 			new_entry = vm_map_entry_create(new_map);
442578d7964bSXin LI 			memset(new_entry, 0, sizeof(*new_entry));
442678d7964bSXin LI 
442778d7964bSXin LI 			new_entry->start = old_entry->start;
442878d7964bSXin LI 			new_entry->end = old_entry->end;
442978d7964bSXin LI 			new_entry->eflags = old_entry->eflags &
443078d7964bSXin LI 			    ~(MAP_ENTRY_USER_WIRED | MAP_ENTRY_IN_TRANSITION |
4431e2e80fb3SKonstantin Belousov 			    MAP_ENTRY_WRITECNT | MAP_ENTRY_VN_EXEC |
4432e2e80fb3SKonstantin Belousov 			    MAP_ENTRY_SPLIT_BOUNDARY_MASK);
443378d7964bSXin LI 			new_entry->protection = old_entry->protection;
443478d7964bSXin LI 			new_entry->max_protection = old_entry->max_protection;
443578d7964bSXin LI 			new_entry->inheritance = VM_INHERIT_ZERO;
443678d7964bSXin LI 
44379f701172SKonstantin Belousov 			vm_map_entry_link(new_map, new_entry);
443878d7964bSXin LI 			vmspace_map_entry_forked(vm1, vm2, new_entry);
443978d7964bSXin LI 
444078d7964bSXin LI 			new_entry->cred = curthread->td_ucred;
444178d7964bSXin LI 			crhold(new_entry->cred);
444278d7964bSXin LI 			*fork_charge += (new_entry->end - new_entry->start);
444378d7964bSXin LI 
444478d7964bSXin LI 			break;
4445df8bae1dSRodney W. Grimes 		}
4446df8bae1dSRodney W. Grimes 	}
444784110e7eSKonstantin Belousov 	/*
444884110e7eSKonstantin Belousov 	 * Use inlined vm_map_unlock() to postpone handling the deferred
444984110e7eSKonstantin Belousov 	 * map entries, which cannot be done until both old_map and
445084110e7eSKonstantin Belousov 	 * new_map locks are released.
445184110e7eSKonstantin Belousov 	 */
445284110e7eSKonstantin Belousov 	sx_xunlock(&old_map->lock);
445384110e7eSKonstantin Belousov 	sx_xunlock(&new_map->lock);
445484110e7eSKonstantin Belousov 	vm_map_process_deferred();
4455df8bae1dSRodney W. Grimes 
4456df8bae1dSRodney W. Grimes 	return (vm2);
4457df8bae1dSRodney W. Grimes }
4458df8bae1dSRodney W. Grimes 
44598056df6eSAlan Cox /*
44608056df6eSAlan Cox  * Create a process's stack for exec_new_vmspace().  This function is never
44618056df6eSAlan Cox  * asked to wire the newly created stack.
44628056df6eSAlan Cox  */
446394f7e29aSAlan Cox int
446494f7e29aSAlan Cox vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
446594f7e29aSAlan Cox     vm_prot_t prot, vm_prot_t max, int cow)
446694f7e29aSAlan Cox {
44674648ba0aSKonstantin Belousov 	vm_size_t growsize, init_ssize;
44688056df6eSAlan Cox 	rlim_t vmemlim;
44694648ba0aSKonstantin Belousov 	int rv;
44704648ba0aSKonstantin Belousov 
44718056df6eSAlan Cox 	MPASS((map->flags & MAP_WIREFUTURE) == 0);
44724648ba0aSKonstantin Belousov 	growsize = sgrowsiz;
44734648ba0aSKonstantin Belousov 	init_ssize = (max_ssize < growsize) ? max_ssize : growsize;
44744648ba0aSKonstantin Belousov 	vm_map_lock(map);
4475f6f6d240SMateusz Guzik 	vmemlim = lim_cur(curthread, RLIMIT_VMEM);
44764648ba0aSKonstantin Belousov 	/* If we would blow our VMEM resource limit, no go */
44774648ba0aSKonstantin Belousov 	if (map->size + init_ssize > vmemlim) {
44784648ba0aSKonstantin Belousov 		rv = KERN_NO_SPACE;
44794648ba0aSKonstantin Belousov 		goto out;
44804648ba0aSKonstantin Belousov 	}
4481e1f92cccSAlan Cox 	rv = vm_map_stack_locked(map, addrbos, max_ssize, growsize, prot,
44824648ba0aSKonstantin Belousov 	    max, cow);
44834648ba0aSKonstantin Belousov out:
44844648ba0aSKonstantin Belousov 	vm_map_unlock(map);
44854648ba0aSKonstantin Belousov 	return (rv);
44864648ba0aSKonstantin Belousov }
44874648ba0aSKonstantin Belousov 
448819f49ad3SKonstantin Belousov static int stack_guard_page = 1;
448919f49ad3SKonstantin Belousov SYSCTL_INT(_security_bsd, OID_AUTO, stack_guard_page, CTLFLAG_RWTUN,
449019f49ad3SKonstantin Belousov     &stack_guard_page, 0,
449119f49ad3SKonstantin Belousov     "Specifies the number of guard pages for a stack that grows");
449219f49ad3SKonstantin Belousov 
44934648ba0aSKonstantin Belousov static int
44944648ba0aSKonstantin Belousov vm_map_stack_locked(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
44954648ba0aSKonstantin Belousov     vm_size_t growsize, vm_prot_t prot, vm_prot_t max, int cow)
44964648ba0aSKonstantin Belousov {
4497d1d3f7e1SDoug Moore 	vm_map_entry_t new_entry, prev_entry;
449819bd0d9cSKonstantin Belousov 	vm_offset_t bot, gap_bot, gap_top, top;
449919f49ad3SKonstantin Belousov 	vm_size_t init_ssize, sgp;
4500fd75d710SMarcel Moolenaar 	int orient, rv;
450194f7e29aSAlan Cox 
4502fd75d710SMarcel Moolenaar 	/*
4503fd75d710SMarcel Moolenaar 	 * The stack orientation is piggybacked with the cow argument.
4504fd75d710SMarcel Moolenaar 	 * Extract it into orient and mask the cow argument so that we
4505fd75d710SMarcel Moolenaar 	 * don't pass it around further.
4506fd75d710SMarcel Moolenaar 	 */
4507fd75d710SMarcel Moolenaar 	orient = cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP);
4508fd75d710SMarcel Moolenaar 	KASSERT(orient != 0, ("No stack grow direction"));
450919bd0d9cSKonstantin Belousov 	KASSERT(orient != (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP),
451019bd0d9cSKonstantin Belousov 	    ("bi-dir stack"));
4511fd75d710SMarcel Moolenaar 
45120f1e6ec5SMark Johnston 	if (max_ssize == 0 ||
45130f1e6ec5SMark Johnston 	    !vm_map_range_valid(map, addrbos, addrbos + max_ssize))
45149410cd7dSKonstantin Belousov 		return (KERN_INVALID_ADDRESS);
4515156e8654SKonstantin Belousov 	sgp = ((curproc->p_flag2 & P2_STKGAP_DISABLE) != 0 ||
4516156e8654SKonstantin Belousov 	    (curproc->p_fctl0 & NT_FREEBSD_FCTL_STKGAP_DISABLE) != 0) ? 0 :
4517fe69291fSKonstantin Belousov 	    (vm_size_t)stack_guard_page * PAGE_SIZE;
45189410cd7dSKonstantin Belousov 	if (sgp >= max_ssize)
45199410cd7dSKonstantin Belousov 		return (KERN_INVALID_ARGUMENT);
4520fd75d710SMarcel Moolenaar 
452119f49ad3SKonstantin Belousov 	init_ssize = growsize;
452219f49ad3SKonstantin Belousov 	if (max_ssize < init_ssize + sgp)
452319f49ad3SKonstantin Belousov 		init_ssize = max_ssize - sgp;
452494f7e29aSAlan Cox 
452594f7e29aSAlan Cox 	/* If addr is already mapped, no go */
4526d1d3f7e1SDoug Moore 	if (vm_map_lookup_entry(map, addrbos, &prev_entry))
452794f7e29aSAlan Cox 		return (KERN_NO_SPACE);
4528a69ac174SMatthew Dillon 
4529fd75d710SMarcel Moolenaar 	/*
4530763df3ecSPedro F. Giffuni 	 * If we can't accommodate max_ssize in the current mapping, no go.
453194f7e29aSAlan Cox 	 */
45327cdcf863SDoug Moore 	if (vm_map_entry_succ(prev_entry)->start < addrbos + max_ssize)
453394f7e29aSAlan Cox 		return (KERN_NO_SPACE);
453494f7e29aSAlan Cox 
4535fd75d710SMarcel Moolenaar 	/*
4536fd75d710SMarcel Moolenaar 	 * We initially map a stack of only init_ssize.  We will grow as
4537fd75d710SMarcel Moolenaar 	 * needed later.  Depending on the orientation of the stack (i.e.
4538fd75d710SMarcel Moolenaar 	 * the grow direction) we either map at the top of the range, the
4539fd75d710SMarcel Moolenaar 	 * bottom of the range or in the middle.
454094f7e29aSAlan Cox 	 *
4541fd75d710SMarcel Moolenaar 	 * Note: we would normally expect prot and max to be VM_PROT_ALL,
4542fd75d710SMarcel Moolenaar 	 * and cow to be 0.  Possibly we should eliminate these as input
4543fd75d710SMarcel Moolenaar 	 * parameters, and just pass these values here in the insert call.
454494f7e29aSAlan Cox 	 */
454519bd0d9cSKonstantin Belousov 	if (orient == MAP_STACK_GROWS_DOWN) {
4546fd75d710SMarcel Moolenaar 		bot = addrbos + max_ssize - init_ssize;
4547fd75d710SMarcel Moolenaar 		top = bot + init_ssize;
454819bd0d9cSKonstantin Belousov 		gap_bot = addrbos;
454919bd0d9cSKonstantin Belousov 		gap_top = bot;
455019bd0d9cSKonstantin Belousov 	} else /* if (orient == MAP_STACK_GROWS_UP) */ {
455119bd0d9cSKonstantin Belousov 		bot = addrbos;
455219bd0d9cSKonstantin Belousov 		top = bot + init_ssize;
455319bd0d9cSKonstantin Belousov 		gap_bot = top;
455419bd0d9cSKonstantin Belousov 		gap_top = addrbos + max_ssize;
455519bd0d9cSKonstantin Belousov 	}
4556fd75d710SMarcel Moolenaar 	rv = vm_map_insert(map, NULL, 0, bot, top, prot, max, cow);
455719bd0d9cSKonstantin Belousov 	if (rv != KERN_SUCCESS)
455819bd0d9cSKonstantin Belousov 		return (rv);
45597cdcf863SDoug Moore 	new_entry = vm_map_entry_succ(prev_entry);
456019bd0d9cSKonstantin Belousov 	KASSERT(new_entry->end == top || new_entry->start == bot,
456119bd0d9cSKonstantin Belousov 	    ("Bad entry start/end for new stack entry"));
4562712efe66SAlan Cox 	KASSERT((orient & MAP_STACK_GROWS_DOWN) == 0 ||
4563712efe66SAlan Cox 	    (new_entry->eflags & MAP_ENTRY_GROWS_DOWN) != 0,
4564712efe66SAlan Cox 	    ("new entry lacks MAP_ENTRY_GROWS_DOWN"));
4565712efe66SAlan Cox 	KASSERT((orient & MAP_STACK_GROWS_UP) == 0 ||
4566712efe66SAlan Cox 	    (new_entry->eflags & MAP_ENTRY_GROWS_UP) != 0,
4567712efe66SAlan Cox 	    ("new entry lacks MAP_ENTRY_GROWS_UP"));
4568fe69291fSKonstantin Belousov 	if (gap_bot == gap_top)
4569fe69291fSKonstantin Belousov 		return (KERN_SUCCESS);
457019bd0d9cSKonstantin Belousov 	rv = vm_map_insert(map, NULL, 0, gap_bot, gap_top, VM_PROT_NONE,
457119bd0d9cSKonstantin Belousov 	    VM_PROT_NONE, MAP_CREATE_GUARD | (orient == MAP_STACK_GROWS_DOWN ?
457219bd0d9cSKonstantin Belousov 	    MAP_CREATE_STACK_GAP_DN : MAP_CREATE_STACK_GAP_UP));
4573a7751d32SKonstantin Belousov 	if (rv == KERN_SUCCESS) {
4574a7751d32SKonstantin Belousov 		/*
4575a7751d32SKonstantin Belousov 		 * Gap can never successfully handle a fault, so
4576a7751d32SKonstantin Belousov 		 * read-ahead logic is never used for it.  Re-use
4577a7751d32SKonstantin Belousov 		 * next_read of the gap entry to store
4578a7751d32SKonstantin Belousov 		 * stack_guard_page for vm_map_growstack().
4579a7751d32SKonstantin Belousov 		 */
4580a7751d32SKonstantin Belousov 		if (orient == MAP_STACK_GROWS_DOWN)
45817cdcf863SDoug Moore 			vm_map_entry_pred(new_entry)->next_read = sgp;
4582a7751d32SKonstantin Belousov 		else
45837cdcf863SDoug Moore 			vm_map_entry_succ(new_entry)->next_read = sgp;
4584a7751d32SKonstantin Belousov 	} else {
458519bd0d9cSKonstantin Belousov 		(void)vm_map_delete(map, bot, top);
4586a7751d32SKonstantin Belousov 	}
458794f7e29aSAlan Cox 	return (rv);
458894f7e29aSAlan Cox }
458994f7e29aSAlan Cox 
459019bd0d9cSKonstantin Belousov /*
459119bd0d9cSKonstantin Belousov  * Attempts to grow a vm stack entry.  Returns KERN_SUCCESS if we
459219bd0d9cSKonstantin Belousov  * successfully grow the stack.
459394f7e29aSAlan Cox  */
459419bd0d9cSKonstantin Belousov static int
459519bd0d9cSKonstantin Belousov vm_map_growstack(vm_map_t map, vm_offset_t addr, vm_map_entry_t gap_entry)
459694f7e29aSAlan Cox {
459719bd0d9cSKonstantin Belousov 	vm_map_entry_t stack_entry;
459819bd0d9cSKonstantin Belousov 	struct proc *p;
459919bd0d9cSKonstantin Belousov 	struct vmspace *vm;
460019bd0d9cSKonstantin Belousov 	struct ucred *cred;
460119bd0d9cSKonstantin Belousov 	vm_offset_t gap_end, gap_start, grow_start;
4602fa581662SDoug Moore 	vm_size_t grow_amount, guard, max_grow;
46037e19eda4SAndrey Zonov 	rlim_t lmemlim, stacklim, vmemlim;
460419bd0d9cSKonstantin Belousov 	int rv, rv1;
460519bd0d9cSKonstantin Belousov 	bool gap_deleted, grow_down, is_procstack;
46061ba5ad42SEdward Tomasz Napierala #ifdef notyet
46071ba5ad42SEdward Tomasz Napierala 	uint64_t limit;
46081ba5ad42SEdward Tomasz Napierala #endif
4609afcc55f3SEdward Tomasz Napierala #ifdef RACCT
46101ba5ad42SEdward Tomasz Napierala 	int error;
4611afcc55f3SEdward Tomasz Napierala #endif
461223955314SAlfred Perlstein 
461319bd0d9cSKonstantin Belousov 	p = curproc;
461419bd0d9cSKonstantin Belousov 	vm = p->p_vmspace;
4615eb5ea878SKonstantin Belousov 
4616eb5ea878SKonstantin Belousov 	/*
4617eb5ea878SKonstantin Belousov 	 * Disallow stack growth when the access is performed by a
4618eb5ea878SKonstantin Belousov 	 * debugger or AIO daemon.  The reason is that the wrong
4619eb5ea878SKonstantin Belousov 	 * resource limits are applied.
4620eb5ea878SKonstantin Belousov 	 */
462110ae16c7SKonstantin Belousov 	if (p != initproc && (map != &p->p_vmspace->vm_map ||
462210ae16c7SKonstantin Belousov 	    p->p_textvp == NULL))
4623f758aaddSKonstantin Belousov 		return (KERN_FAILURE);
4624eb5ea878SKonstantin Belousov 
462519bd0d9cSKonstantin Belousov 	MPASS(!map->system_map);
462619bd0d9cSKonstantin Belousov 
4627f6f6d240SMateusz Guzik 	lmemlim = lim_cur(curthread, RLIMIT_MEMLOCK);
4628f6f6d240SMateusz Guzik 	stacklim = lim_cur(curthread, RLIMIT_STACK);
4629f6f6d240SMateusz Guzik 	vmemlim = lim_cur(curthread, RLIMIT_VMEM);
463019bd0d9cSKonstantin Belousov retry:
463119bd0d9cSKonstantin Belousov 	/* If addr is not in a hole for a stack grow area, no need to grow. */
4632d1d3f7e1SDoug Moore 	if (gap_entry == NULL && !vm_map_lookup_entry(map, addr, &gap_entry))
463319bd0d9cSKonstantin Belousov 		return (KERN_FAILURE);
463419bd0d9cSKonstantin Belousov 	if ((gap_entry->eflags & MAP_ENTRY_GUARD) == 0)
46350cddd8f0SMatthew Dillon 		return (KERN_SUCCESS);
463619bd0d9cSKonstantin Belousov 	if ((gap_entry->eflags & MAP_ENTRY_STACK_GAP_DN) != 0) {
46377cdcf863SDoug Moore 		stack_entry = vm_map_entry_succ(gap_entry);
463819bd0d9cSKonstantin Belousov 		if ((stack_entry->eflags & MAP_ENTRY_GROWS_DOWN) == 0 ||
463919bd0d9cSKonstantin Belousov 		    stack_entry->start != gap_entry->end)
464019bd0d9cSKonstantin Belousov 			return (KERN_FAILURE);
464119bd0d9cSKonstantin Belousov 		grow_amount = round_page(stack_entry->start - addr);
464219bd0d9cSKonstantin Belousov 		grow_down = true;
464319bd0d9cSKonstantin Belousov 	} else if ((gap_entry->eflags & MAP_ENTRY_STACK_GAP_UP) != 0) {
46447cdcf863SDoug Moore 		stack_entry = vm_map_entry_pred(gap_entry);
464519bd0d9cSKonstantin Belousov 		if ((stack_entry->eflags & MAP_ENTRY_GROWS_UP) == 0 ||
464619bd0d9cSKonstantin Belousov 		    stack_entry->end != gap_entry->start)
464719bd0d9cSKonstantin Belousov 			return (KERN_FAILURE);
464819bd0d9cSKonstantin Belousov 		grow_amount = round_page(addr + 1 - stack_entry->end);
464919bd0d9cSKonstantin Belousov 		grow_down = false;
4650b21a0008SMarcel Moolenaar 	} else {
465119bd0d9cSKonstantin Belousov 		return (KERN_FAILURE);
4652b21a0008SMarcel Moolenaar 	}
4653156e8654SKonstantin Belousov 	guard = ((curproc->p_flag2 & P2_STKGAP_DISABLE) != 0 ||
4654156e8654SKonstantin Belousov 	    (curproc->p_fctl0 & NT_FREEBSD_FCTL_STKGAP_DISABLE) != 0) ? 0 :
4655fe69291fSKonstantin Belousov 	    gap_entry->next_read;
4656201f03b8SAlan Cox 	max_grow = gap_entry->end - gap_entry->start;
4657201f03b8SAlan Cox 	if (guard > max_grow)
4658201f03b8SAlan Cox 		return (KERN_NO_SPACE);
4659201f03b8SAlan Cox 	max_grow -= guard;
466019bd0d9cSKonstantin Belousov 	if (grow_amount > max_grow)
46610cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
466294f7e29aSAlan Cox 
4663b21a0008SMarcel Moolenaar 	/*
4664b21a0008SMarcel Moolenaar 	 * If this is the main process stack, see if we're over the stack
4665b21a0008SMarcel Moolenaar 	 * limit.
466694f7e29aSAlan Cox 	 */
466719bd0d9cSKonstantin Belousov 	is_procstack = addr >= (vm_offset_t)vm->vm_maxsaddr &&
466819bd0d9cSKonstantin Belousov 	    addr < (vm_offset_t)p->p_sysent->sv_usrstack;
466919bd0d9cSKonstantin Belousov 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim))
46700cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
467119bd0d9cSKonstantin Belousov 
4672afcc55f3SEdward Tomasz Napierala #ifdef RACCT
46734b5c9cf6SEdward Tomasz Napierala 	if (racct_enable) {
46741ba5ad42SEdward Tomasz Napierala 		PROC_LOCK(p);
46754b5c9cf6SEdward Tomasz Napierala 		if (is_procstack && racct_set(p, RACCT_STACK,
46764b5c9cf6SEdward Tomasz Napierala 		    ctob(vm->vm_ssize) + grow_amount)) {
46771ba5ad42SEdward Tomasz Napierala 			PROC_UNLOCK(p);
46781ba5ad42SEdward Tomasz Napierala 			return (KERN_NO_SPACE);
46791ba5ad42SEdward Tomasz Napierala 		}
46801ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(p);
46814b5c9cf6SEdward Tomasz Napierala 	}
4682afcc55f3SEdward Tomasz Napierala #endif
468394f7e29aSAlan Cox 
468419bd0d9cSKonstantin Belousov 	grow_amount = roundup(grow_amount, sgrowsiz);
468519bd0d9cSKonstantin Belousov 	if (grow_amount > max_grow)
468619bd0d9cSKonstantin Belousov 		grow_amount = max_grow;
468791d5354aSJohn Baldwin 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
4688e4826248SAlan Cox 		grow_amount = trunc_page((vm_size_t)stacklim) -
4689e4826248SAlan Cox 		    ctob(vm->vm_ssize);
469094f7e29aSAlan Cox 	}
469119bd0d9cSKonstantin Belousov 
46921ba5ad42SEdward Tomasz Napierala #ifdef notyet
46931ba5ad42SEdward Tomasz Napierala 	PROC_LOCK(p);
46941ba5ad42SEdward Tomasz Napierala 	limit = racct_get_available(p, RACCT_STACK);
46951ba5ad42SEdward Tomasz Napierala 	PROC_UNLOCK(p);
46961ba5ad42SEdward Tomasz Napierala 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > limit))
46971ba5ad42SEdward Tomasz Napierala 		grow_amount = limit - ctob(vm->vm_ssize);
46981ba5ad42SEdward Tomasz Napierala #endif
469919bd0d9cSKonstantin Belousov 
470019bd0d9cSKonstantin Belousov 	if (!old_mlock && (map->flags & MAP_WIREFUTURE) != 0) {
47013ac7d297SAndrey Zonov 		if (ptoa(pmap_wired_count(map->pmap)) + grow_amount > lmemlim) {
47027e19eda4SAndrey Zonov 			rv = KERN_NO_SPACE;
47037e19eda4SAndrey Zonov 			goto out;
47047e19eda4SAndrey Zonov 		}
47057e19eda4SAndrey Zonov #ifdef RACCT
47064b5c9cf6SEdward Tomasz Napierala 		if (racct_enable) {
47077e19eda4SAndrey Zonov 			PROC_LOCK(p);
47087e19eda4SAndrey Zonov 			if (racct_set(p, RACCT_MEMLOCK,
47093ac7d297SAndrey Zonov 			    ptoa(pmap_wired_count(map->pmap)) + grow_amount)) {
47107e19eda4SAndrey Zonov 				PROC_UNLOCK(p);
47117e19eda4SAndrey Zonov 				rv = KERN_NO_SPACE;
47127e19eda4SAndrey Zonov 				goto out;
47137e19eda4SAndrey Zonov 			}
47147e19eda4SAndrey Zonov 			PROC_UNLOCK(p);
47154b5c9cf6SEdward Tomasz Napierala 		}
47167e19eda4SAndrey Zonov #endif
47177e19eda4SAndrey Zonov 	}
471819bd0d9cSKonstantin Belousov 
4719a69ac174SMatthew Dillon 	/* If we would blow our VMEM resource limit, no go */
472091d5354aSJohn Baldwin 	if (map->size + grow_amount > vmemlim) {
47211ba5ad42SEdward Tomasz Napierala 		rv = KERN_NO_SPACE;
47221ba5ad42SEdward Tomasz Napierala 		goto out;
4723a69ac174SMatthew Dillon 	}
4724afcc55f3SEdward Tomasz Napierala #ifdef RACCT
47254b5c9cf6SEdward Tomasz Napierala 	if (racct_enable) {
47261ba5ad42SEdward Tomasz Napierala 		PROC_LOCK(p);
47271ba5ad42SEdward Tomasz Napierala 		if (racct_set(p, RACCT_VMEM, map->size + grow_amount)) {
47281ba5ad42SEdward Tomasz Napierala 			PROC_UNLOCK(p);
47291ba5ad42SEdward Tomasz Napierala 			rv = KERN_NO_SPACE;
47301ba5ad42SEdward Tomasz Napierala 			goto out;
47311ba5ad42SEdward Tomasz Napierala 		}
47321ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(p);
47334b5c9cf6SEdward Tomasz Napierala 	}
4734afcc55f3SEdward Tomasz Napierala #endif
4735a69ac174SMatthew Dillon 
473619bd0d9cSKonstantin Belousov 	if (vm_map_lock_upgrade(map)) {
473719bd0d9cSKonstantin Belousov 		gap_entry = NULL;
473819bd0d9cSKonstantin Belousov 		vm_map_lock_read(map);
473919bd0d9cSKonstantin Belousov 		goto retry;
474094f7e29aSAlan Cox 	}
474194f7e29aSAlan Cox 
474219bd0d9cSKonstantin Belousov 	if (grow_down) {
474319bd0d9cSKonstantin Belousov 		grow_start = gap_entry->end - grow_amount;
474419bd0d9cSKonstantin Belousov 		if (gap_entry->start + grow_amount == gap_entry->end) {
474519bd0d9cSKonstantin Belousov 			gap_start = gap_entry->start;
474619bd0d9cSKonstantin Belousov 			gap_end = gap_entry->end;
474719bd0d9cSKonstantin Belousov 			vm_map_entry_delete(map, gap_entry);
474819bd0d9cSKonstantin Belousov 			gap_deleted = true;
474919bd0d9cSKonstantin Belousov 		} else {
475019bd0d9cSKonstantin Belousov 			MPASS(gap_entry->start < gap_entry->end - grow_amount);
4751fa581662SDoug Moore 			vm_map_entry_resize(map, gap_entry, -grow_amount);
475219bd0d9cSKonstantin Belousov 			gap_deleted = false;
475319bd0d9cSKonstantin Belousov 		}
475419bd0d9cSKonstantin Belousov 		rv = vm_map_insert(map, NULL, 0, grow_start,
475519bd0d9cSKonstantin Belousov 		    grow_start + grow_amount,
475619bd0d9cSKonstantin Belousov 		    stack_entry->protection, stack_entry->max_protection,
4757712efe66SAlan Cox 		    MAP_STACK_GROWS_DOWN);
475819bd0d9cSKonstantin Belousov 		if (rv != KERN_SUCCESS) {
475919bd0d9cSKonstantin Belousov 			if (gap_deleted) {
476019bd0d9cSKonstantin Belousov 				rv1 = vm_map_insert(map, NULL, 0, gap_start,
476119bd0d9cSKonstantin Belousov 				    gap_end, VM_PROT_NONE, VM_PROT_NONE,
476219bd0d9cSKonstantin Belousov 				    MAP_CREATE_GUARD | MAP_CREATE_STACK_GAP_DN);
476319bd0d9cSKonstantin Belousov 				MPASS(rv1 == KERN_SUCCESS);
47641895f520SDoug Moore 			} else
4765fa581662SDoug Moore 				vm_map_entry_resize(map, gap_entry,
47661895f520SDoug Moore 				    grow_amount);
476794f7e29aSAlan Cox 		}
4768b21a0008SMarcel Moolenaar 	} else {
476919bd0d9cSKonstantin Belousov 		grow_start = stack_entry->end;
4770ef694c1aSEdward Tomasz Napierala 		cred = stack_entry->cred;
4771ef694c1aSEdward Tomasz Napierala 		if (cred == NULL && stack_entry->object.vm_object != NULL)
4772ef694c1aSEdward Tomasz Napierala 			cred = stack_entry->object.vm_object->cred;
4773ef694c1aSEdward Tomasz Napierala 		if (cred != NULL && !swap_reserve_by_cred(grow_amount, cred))
47743364c323SKonstantin Belousov 			rv = KERN_NO_SPACE;
4775b21a0008SMarcel Moolenaar 		/* Grow the underlying object if applicable. */
47763364c323SKonstantin Belousov 		else if (stack_entry->object.vm_object == NULL ||
4777b21a0008SMarcel Moolenaar 		    vm_object_coalesce(stack_entry->object.vm_object,
477857a21abaSAlan Cox 		    stack_entry->offset,
4779b21a0008SMarcel Moolenaar 		    (vm_size_t)(stack_entry->end - stack_entry->start),
4780fa581662SDoug Moore 		    grow_amount, cred != NULL)) {
4781fa581662SDoug Moore 			if (gap_entry->start + grow_amount == gap_entry->end) {
478219bd0d9cSKonstantin Belousov 				vm_map_entry_delete(map, gap_entry);
4783fa581662SDoug Moore 				vm_map_entry_resize(map, stack_entry,
4784fa581662SDoug Moore 				    grow_amount);
4785fa581662SDoug Moore 			} else {
478619bd0d9cSKonstantin Belousov 				gap_entry->start += grow_amount;
4787fa581662SDoug Moore 				stack_entry->end += grow_amount;
4788fa581662SDoug Moore 			}
478919bd0d9cSKonstantin Belousov 			map->size += grow_amount;
4790b21a0008SMarcel Moolenaar 			rv = KERN_SUCCESS;
4791b21a0008SMarcel Moolenaar 		} else
4792b21a0008SMarcel Moolenaar 			rv = KERN_FAILURE;
4793b21a0008SMarcel Moolenaar 	}
4794b21a0008SMarcel Moolenaar 	if (rv == KERN_SUCCESS && is_procstack)
4795b21a0008SMarcel Moolenaar 		vm->vm_ssize += btoc(grow_amount);
4796b21a0008SMarcel Moolenaar 
4797abd498aaSBruce M Simpson 	/*
4798abd498aaSBruce M Simpson 	 * Heed the MAP_WIREFUTURE flag if it was set for this process.
4799abd498aaSBruce M Simpson 	 */
480019bd0d9cSKonstantin Belousov 	if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE) != 0) {
480154a3a114SMark Johnston 		rv = vm_map_wire_locked(map, grow_start,
480254a3a114SMark Johnston 		    grow_start + grow_amount,
4803212e02c8SKonstantin Belousov 		    VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
480454a3a114SMark Johnston 	}
480519bd0d9cSKonstantin Belousov 	vm_map_lock_downgrade(map);
4806abd498aaSBruce M Simpson 
48071ba5ad42SEdward Tomasz Napierala out:
4808afcc55f3SEdward Tomasz Napierala #ifdef RACCT
48094b5c9cf6SEdward Tomasz Napierala 	if (racct_enable && rv != KERN_SUCCESS) {
48101ba5ad42SEdward Tomasz Napierala 		PROC_LOCK(p);
48111ba5ad42SEdward Tomasz Napierala 		error = racct_set(p, RACCT_VMEM, map->size);
48121ba5ad42SEdward Tomasz Napierala 		KASSERT(error == 0, ("decreasing RACCT_VMEM failed"));
48137e19eda4SAndrey Zonov 		if (!old_mlock) {
48147e19eda4SAndrey Zonov 			error = racct_set(p, RACCT_MEMLOCK,
48153ac7d297SAndrey Zonov 			    ptoa(pmap_wired_count(map->pmap)));
48167e19eda4SAndrey Zonov 			KASSERT(error == 0, ("decreasing RACCT_MEMLOCK failed"));
48177e19eda4SAndrey Zonov 		}
48181ba5ad42SEdward Tomasz Napierala 	    	error = racct_set(p, RACCT_STACK, ctob(vm->vm_ssize));
48191ba5ad42SEdward Tomasz Napierala 		KASSERT(error == 0, ("decreasing RACCT_STACK failed"));
48201ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(p);
48211ba5ad42SEdward Tomasz Napierala 	}
4822afcc55f3SEdward Tomasz Napierala #endif
48231ba5ad42SEdward Tomasz Napierala 
48240cddd8f0SMatthew Dillon 	return (rv);
482594f7e29aSAlan Cox }
482694f7e29aSAlan Cox 
4827df8bae1dSRodney W. Grimes /*
48285856e12eSJohn Dyson  * Unshare the specified VM space for exec.  If other processes are
48295856e12eSJohn Dyson  * mapped to it, then create a new one.  The new vmspace is null.
48305856e12eSJohn Dyson  */
483189b57fcfSKonstantin Belousov int
48323ebc1248SPeter Wemm vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser)
48331b40f8c0SMatthew Dillon {
48345856e12eSJohn Dyson 	struct vmspace *oldvmspace = p->p_vmspace;
48355856e12eSJohn Dyson 	struct vmspace *newvmspace;
48365856e12eSJohn Dyson 
48377032434eSKonstantin Belousov 	KASSERT((curthread->td_pflags & TDP_EXECVMSPC) == 0,
48387032434eSKonstantin Belousov 	    ("vmspace_exec recursed"));
48396e00f3a3SKonstantin Belousov 	newvmspace = vmspace_alloc(minuser, maxuser, pmap_pinit);
484089b57fcfSKonstantin Belousov 	if (newvmspace == NULL)
484189b57fcfSKonstantin Belousov 		return (ENOMEM);
484251ab6c28SAlan Cox 	newvmspace->vm_swrss = oldvmspace->vm_swrss;
48435856e12eSJohn Dyson 	/*
48445856e12eSJohn Dyson 	 * This code is written like this for prototype purposes.  The
48455856e12eSJohn Dyson 	 * goal is to avoid running down the vmspace here, but let the
48465856e12eSJohn Dyson 	 * other process's that are still using the vmspace to finally
48475856e12eSJohn Dyson 	 * run it down.  Even though there is little or no chance of blocking
48485856e12eSJohn Dyson 	 * here, it is a good idea to keep this form for future mods.
48495856e12eSJohn Dyson 	 */
485057051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
48515856e12eSJohn Dyson 	p->p_vmspace = newvmspace;
485257051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
48536617724cSJeff Roberson 	if (p == curthread->td_proc)
4854b40ce416SJulian Elischer 		pmap_activate(curthread);
48557032434eSKonstantin Belousov 	curthread->td_pflags |= TDP_EXECVMSPC;
485689b57fcfSKonstantin Belousov 	return (0);
48575856e12eSJohn Dyson }
48585856e12eSJohn Dyson 
48595856e12eSJohn Dyson /*
48605856e12eSJohn Dyson  * Unshare the specified VM space for forcing COW.  This
48615856e12eSJohn Dyson  * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
48625856e12eSJohn Dyson  */
486389b57fcfSKonstantin Belousov int
48641b40f8c0SMatthew Dillon vmspace_unshare(struct proc *p)
48651b40f8c0SMatthew Dillon {
48665856e12eSJohn Dyson 	struct vmspace *oldvmspace = p->p_vmspace;
48675856e12eSJohn Dyson 	struct vmspace *newvmspace;
48683364c323SKonstantin Belousov 	vm_ooffset_t fork_charge;
48695856e12eSJohn Dyson 
48709246b309SMark Johnston 	/*
48719246b309SMark Johnston 	 * The caller is responsible for ensuring that the reference count
48729246b309SMark Johnston 	 * cannot concurrently transition 1 -> 2.
48739246b309SMark Johnston 	 */
4874f7db0c95SMark Johnston 	if (refcount_load(&oldvmspace->vm_refcnt) == 1)
487589b57fcfSKonstantin Belousov 		return (0);
48763364c323SKonstantin Belousov 	fork_charge = 0;
48773364c323SKonstantin Belousov 	newvmspace = vmspace_fork(oldvmspace, &fork_charge);
487889b57fcfSKonstantin Belousov 	if (newvmspace == NULL)
487989b57fcfSKonstantin Belousov 		return (ENOMEM);
4880ef694c1aSEdward Tomasz Napierala 	if (!swap_reserve_by_cred(fork_charge, p->p_ucred)) {
48813364c323SKonstantin Belousov 		vmspace_free(newvmspace);
48823364c323SKonstantin Belousov 		return (ENOMEM);
48833364c323SKonstantin Belousov 	}
488457051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
48855856e12eSJohn Dyson 	p->p_vmspace = newvmspace;
488657051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
48876617724cSJeff Roberson 	if (p == curthread->td_proc)
4888b40ce416SJulian Elischer 		pmap_activate(curthread);
4889b56ef1c1SJohn Baldwin 	vmspace_free(oldvmspace);
489089b57fcfSKonstantin Belousov 	return (0);
48915856e12eSJohn Dyson }
48925856e12eSJohn Dyson 
48935856e12eSJohn Dyson /*
4894df8bae1dSRodney W. Grimes  *	vm_map_lookup:
4895df8bae1dSRodney W. Grimes  *
4896df8bae1dSRodney W. Grimes  *	Finds the VM object, offset, and
4897df8bae1dSRodney W. Grimes  *	protection for a given virtual address in the
4898df8bae1dSRodney W. Grimes  *	specified map, assuming a page fault of the
4899df8bae1dSRodney W. Grimes  *	type specified.
4900df8bae1dSRodney W. Grimes  *
4901df8bae1dSRodney W. Grimes  *	Leaves the map in question locked for read; return
4902df8bae1dSRodney W. Grimes  *	values are guaranteed until a vm_map_lookup_done
4903df8bae1dSRodney W. Grimes  *	call is performed.  Note that the map argument
4904df8bae1dSRodney W. Grimes  *	is in/out; the returned map must be used in
4905df8bae1dSRodney W. Grimes  *	the call to vm_map_lookup_done.
4906df8bae1dSRodney W. Grimes  *
4907df8bae1dSRodney W. Grimes  *	A handle (out_entry) is returned for use in
4908df8bae1dSRodney W. Grimes  *	vm_map_lookup_done, to make that fast.
4909df8bae1dSRodney W. Grimes  *
4910df8bae1dSRodney W. Grimes  *	If a lookup is requested with "write protection"
4911df8bae1dSRodney W. Grimes  *	specified, the map may be changed to perform virtual
4912df8bae1dSRodney W. Grimes  *	copying operations, although the data referenced will
4913df8bae1dSRodney W. Grimes  *	remain the same.
4914df8bae1dSRodney W. Grimes  */
4915df8bae1dSRodney W. Grimes int
4916b9dcd593SBruce Evans vm_map_lookup(vm_map_t *var_map,		/* IN/OUT */
4917b9dcd593SBruce Evans 	      vm_offset_t vaddr,
491847221757SJohn Dyson 	      vm_prot_t fault_typea,
4919b9dcd593SBruce Evans 	      vm_map_entry_t *out_entry,	/* OUT */
4920b9dcd593SBruce Evans 	      vm_object_t *object,		/* OUT */
4921b9dcd593SBruce Evans 	      vm_pindex_t *pindex,		/* OUT */
4922b9dcd593SBruce Evans 	      vm_prot_t *out_prot,		/* OUT */
49232d8acc0fSJohn Dyson 	      boolean_t *wired)			/* OUT */
4924df8bae1dSRodney W. Grimes {
4925c0877f10SJohn Dyson 	vm_map_entry_t entry;
4926c0877f10SJohn Dyson 	vm_map_t map = *var_map;
4927c0877f10SJohn Dyson 	vm_prot_t prot;
4928a6f21d15SMark Johnston 	vm_prot_t fault_type;
49293364c323SKonstantin Belousov 	vm_object_t eobject;
49300cc74f14SAlan Cox 	vm_size_t size;
4931ef694c1aSEdward Tomasz Napierala 	struct ucred *cred;
4932df8bae1dSRodney W. Grimes 
493319bd0d9cSKonstantin Belousov RetryLookup:
4934df8bae1dSRodney W. Grimes 
4935df8bae1dSRodney W. Grimes 	vm_map_lock_read(map);
4936df8bae1dSRodney W. Grimes 
493719bd0d9cSKonstantin Belousov RetryLookupLocked:
4938df8bae1dSRodney W. Grimes 	/*
49394c3ef59eSAlan Cox 	 * Lookup the faulting address.
4940df8bae1dSRodney W. Grimes 	 */
4941095104acSAlan Cox 	if (!vm_map_lookup_entry(map, vaddr, out_entry)) {
4942095104acSAlan Cox 		vm_map_unlock_read(map);
4943095104acSAlan Cox 		return (KERN_INVALID_ADDRESS);
4944095104acSAlan Cox 	}
4945df8bae1dSRodney W. Grimes 
49464e94f402SAlan Cox 	entry = *out_entry;
4947b7b2aac2SJohn Dyson 
4948df8bae1dSRodney W. Grimes 	/*
4949df8bae1dSRodney W. Grimes 	 * Handle submaps.
4950df8bae1dSRodney W. Grimes 	 */
4951afa07f7eSJohn Dyson 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
4952df8bae1dSRodney W. Grimes 		vm_map_t old_map = map;
4953df8bae1dSRodney W. Grimes 
4954df8bae1dSRodney W. Grimes 		*var_map = map = entry->object.sub_map;
4955df8bae1dSRodney W. Grimes 		vm_map_unlock_read(old_map);
4956df8bae1dSRodney W. Grimes 		goto RetryLookup;
4957df8bae1dSRodney W. Grimes 	}
4958a04c970aSJohn Dyson 
4959df8bae1dSRodney W. Grimes 	/*
49600d94caffSDavid Greenman 	 * Check whether this task is allowed to have this page.
4961df8bae1dSRodney W. Grimes 	 */
4962df8bae1dSRodney W. Grimes 	prot = entry->protection;
496319bd0d9cSKonstantin Belousov 	if ((fault_typea & VM_PROT_FAULT_LOOKUP) != 0) {
496419bd0d9cSKonstantin Belousov 		fault_typea &= ~VM_PROT_FAULT_LOOKUP;
496519bd0d9cSKonstantin Belousov 		if (prot == VM_PROT_NONE && map != kernel_map &&
496619bd0d9cSKonstantin Belousov 		    (entry->eflags & MAP_ENTRY_GUARD) != 0 &&
496719bd0d9cSKonstantin Belousov 		    (entry->eflags & (MAP_ENTRY_STACK_GAP_DN |
496819bd0d9cSKonstantin Belousov 		    MAP_ENTRY_STACK_GAP_UP)) != 0 &&
496919bd0d9cSKonstantin Belousov 		    vm_map_growstack(map, vaddr, entry) == KERN_SUCCESS)
497019bd0d9cSKonstantin Belousov 			goto RetryLookupLocked;
497119bd0d9cSKonstantin Belousov 	}
4972a6f21d15SMark Johnston 	fault_type = fault_typea & VM_PROT_ALL;
49732db65ab4SAlan Cox 	if ((fault_type & prot) != fault_type || prot == VM_PROT_NONE) {
4974095104acSAlan Cox 		vm_map_unlock_read(map);
4975095104acSAlan Cox 		return (KERN_PROTECTION_FAILURE);
497647221757SJohn Dyson 	}
4977b8db9776SKonstantin Belousov 	KASSERT((prot & VM_PROT_WRITE) == 0 || (entry->eflags &
4978b8db9776SKonstantin Belousov 	    (MAP_ENTRY_USER_WIRED | MAP_ENTRY_NEEDS_COPY)) !=
4979b8db9776SKonstantin Belousov 	    (MAP_ENTRY_USER_WIRED | MAP_ENTRY_NEEDS_COPY),
4980b8db9776SKonstantin Belousov 	    ("entry %p flags %x", entry, entry->eflags));
49815b3e0257SDag-Erling Smørgrav 	if ((fault_typea & VM_PROT_COPY) != 0 &&
49825b3e0257SDag-Erling Smørgrav 	    (entry->max_protection & VM_PROT_WRITE) == 0 &&
49835b3e0257SDag-Erling Smørgrav 	    (entry->eflags & MAP_ENTRY_COW) == 0) {
49845b3e0257SDag-Erling Smørgrav 		vm_map_unlock_read(map);
49855b3e0257SDag-Erling Smørgrav 		return (KERN_PROTECTION_FAILURE);
49865b3e0257SDag-Erling Smørgrav 	}
4987df8bae1dSRodney W. Grimes 
4988df8bae1dSRodney W. Grimes 	/*
49890d94caffSDavid Greenman 	 * If this page is not pageable, we have to get it for all possible
49900d94caffSDavid Greenman 	 * accesses.
4991df8bae1dSRodney W. Grimes 	 */
499205f0fdd2SPoul-Henning Kamp 	*wired = (entry->wired_count != 0);
499305f0fdd2SPoul-Henning Kamp 	if (*wired)
4994a6d42a0dSAlan Cox 		fault_type = entry->protection;
49953364c323SKonstantin Belousov 	size = entry->end - entry->start;
499667388836SKonstantin Belousov 
4997df8bae1dSRodney W. Grimes 	/*
4998df8bae1dSRodney W. Grimes 	 * If the entry was copy-on-write, we either ...
4999df8bae1dSRodney W. Grimes 	 */
5000afa07f7eSJohn Dyson 	if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
5001df8bae1dSRodney W. Grimes 		/*
50020d94caffSDavid Greenman 		 * If we want to write the page, we may as well handle that
5003ad5fca3bSAlan Cox 		 * now since we've got the map locked.
5004df8bae1dSRodney W. Grimes 		 *
50050d94caffSDavid Greenman 		 * If we don't need to write the page, we just demote the
50060d94caffSDavid Greenman 		 * permissions allowed.
5007df8bae1dSRodney W. Grimes 		 */
5008a6d42a0dSAlan Cox 		if ((fault_type & VM_PROT_WRITE) != 0 ||
5009a6d42a0dSAlan Cox 		    (fault_typea & VM_PROT_COPY) != 0) {
5010df8bae1dSRodney W. Grimes 			/*
50110d94caffSDavid Greenman 			 * Make a new object, and place it in the object
50120d94caffSDavid Greenman 			 * chain.  Note that no new references have appeared
5013ad5fca3bSAlan Cox 			 * -- one just moved from the map to the new
50140d94caffSDavid Greenman 			 * object.
5015df8bae1dSRodney W. Grimes 			 */
501625adb370SBrian Feldman 			if (vm_map_lock_upgrade(map))
5017df8bae1dSRodney W. Grimes 				goto RetryLookup;
50189917e010SAlan Cox 
5019ef694c1aSEdward Tomasz Napierala 			if (entry->cred == NULL) {
50203364c323SKonstantin Belousov 				/*
50213364c323SKonstantin Belousov 				 * The debugger owner is charged for
50223364c323SKonstantin Belousov 				 * the memory.
50233364c323SKonstantin Belousov 				 */
5024ef694c1aSEdward Tomasz Napierala 				cred = curthread->td_ucred;
5025ef694c1aSEdward Tomasz Napierala 				crhold(cred);
5026ef694c1aSEdward Tomasz Napierala 				if (!swap_reserve_by_cred(size, cred)) {
5027ef694c1aSEdward Tomasz Napierala 					crfree(cred);
50283364c323SKonstantin Belousov 					vm_map_unlock(map);
50293364c323SKonstantin Belousov 					return (KERN_RESOURCE_SHORTAGE);
50303364c323SKonstantin Belousov 				}
5031ef694c1aSEdward Tomasz Napierala 				entry->cred = cred;
50323364c323SKonstantin Belousov 			}
50333364c323SKonstantin Belousov 			eobject = entry->object.vm_object;
503467388836SKonstantin Belousov 			vm_object_shadow(&entry->object.vm_object,
503567388836SKonstantin Belousov 			    &entry->offset, size, entry->cred, false);
503667388836SKonstantin Belousov 			if (eobject == entry->object.vm_object) {
50373364c323SKonstantin Belousov 				/*
50383364c323SKonstantin Belousov 				 * The object was not shadowed.
50393364c323SKonstantin Belousov 				 */
5040ef694c1aSEdward Tomasz Napierala 				swap_release_by_cred(size, entry->cred);
5041ef694c1aSEdward Tomasz Napierala 				crfree(entry->cred);
50423364c323SKonstantin Belousov 			}
504367388836SKonstantin Belousov 			entry->cred = NULL;
504467388836SKonstantin Belousov 			entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
50459917e010SAlan Cox 
50469b09b6c7SMatthew Dillon 			vm_map_lock_downgrade(map);
50470d94caffSDavid Greenman 		} else {
5048df8bae1dSRodney W. Grimes 			/*
50490d94caffSDavid Greenman 			 * We're attempting to read a copy-on-write page --
50500d94caffSDavid Greenman 			 * don't allow writes.
5051df8bae1dSRodney W. Grimes 			 */
50522d8acc0fSJohn Dyson 			prot &= ~VM_PROT_WRITE;
5053df8bae1dSRodney W. Grimes 		}
5054df8bae1dSRodney W. Grimes 	}
50552d8acc0fSJohn Dyson 
5056df8bae1dSRodney W. Grimes 	/*
5057df8bae1dSRodney W. Grimes 	 * Create an object if necessary.
5058df8bae1dSRodney W. Grimes 	 */
505967388836SKonstantin Belousov 	if (entry->object.vm_object == NULL && !map->system_map) {
506025adb370SBrian Feldman 		if (vm_map_lock_upgrade(map))
5061df8bae1dSRodney W. Grimes 			goto RetryLookup;
506267388836SKonstantin Belousov 		entry->object.vm_object = vm_object_allocate_anon(atop(size),
506367388836SKonstantin Belousov 		    NULL, entry->cred, entry->cred != NULL ? size : 0);
5064df8bae1dSRodney W. Grimes 		entry->offset = 0;
5065ef694c1aSEdward Tomasz Napierala 		entry->cred = NULL;
50669b09b6c7SMatthew Dillon 		vm_map_lock_downgrade(map);
5067df8bae1dSRodney W. Grimes 	}
5068b5b40fa6SJohn Dyson 
5069df8bae1dSRodney W. Grimes 	/*
50700d94caffSDavid Greenman 	 * Return the object/offset from this entry.  If the entry was
50710d94caffSDavid Greenman 	 * copy-on-write or empty, it has been fixed up.
5072df8bae1dSRodney W. Grimes 	 */
507310d9120cSKonstantin Belousov 	*pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
5074df8bae1dSRodney W. Grimes 	*object = entry->object.vm_object;
5075df8bae1dSRodney W. Grimes 
5076df8bae1dSRodney W. Grimes 	*out_prot = prot;
5077df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
5078df8bae1dSRodney W. Grimes }
5079df8bae1dSRodney W. Grimes 
5080df8bae1dSRodney W. Grimes /*
508119dc5607STor Egge  *	vm_map_lookup_locked:
508219dc5607STor Egge  *
508319dc5607STor Egge  *	Lookup the faulting address.  A version of vm_map_lookup that returns
508419dc5607STor Egge  *      KERN_FAILURE instead of blocking on map lock or memory allocation.
508519dc5607STor Egge  */
508619dc5607STor Egge int
508719dc5607STor Egge vm_map_lookup_locked(vm_map_t *var_map,		/* IN/OUT */
508819dc5607STor Egge 		     vm_offset_t vaddr,
508919dc5607STor Egge 		     vm_prot_t fault_typea,
509019dc5607STor Egge 		     vm_map_entry_t *out_entry,	/* OUT */
509119dc5607STor Egge 		     vm_object_t *object,	/* OUT */
509219dc5607STor Egge 		     vm_pindex_t *pindex,	/* OUT */
509319dc5607STor Egge 		     vm_prot_t *out_prot,	/* OUT */
509419dc5607STor Egge 		     boolean_t *wired)		/* OUT */
509519dc5607STor Egge {
509619dc5607STor Egge 	vm_map_entry_t entry;
509719dc5607STor Egge 	vm_map_t map = *var_map;
509819dc5607STor Egge 	vm_prot_t prot;
509919dc5607STor Egge 	vm_prot_t fault_type = fault_typea;
510019dc5607STor Egge 
510119dc5607STor Egge 	/*
51024c3ef59eSAlan Cox 	 * Lookup the faulting address.
510319dc5607STor Egge 	 */
510419dc5607STor Egge 	if (!vm_map_lookup_entry(map, vaddr, out_entry))
510519dc5607STor Egge 		return (KERN_INVALID_ADDRESS);
510619dc5607STor Egge 
510719dc5607STor Egge 	entry = *out_entry;
510819dc5607STor Egge 
510919dc5607STor Egge 	/*
511019dc5607STor Egge 	 * Fail if the entry refers to a submap.
511119dc5607STor Egge 	 */
511219dc5607STor Egge 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
511319dc5607STor Egge 		return (KERN_FAILURE);
511419dc5607STor Egge 
511519dc5607STor Egge 	/*
511619dc5607STor Egge 	 * Check whether this task is allowed to have this page.
511719dc5607STor Egge 	 */
511819dc5607STor Egge 	prot = entry->protection;
511919dc5607STor Egge 	fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
512019dc5607STor Egge 	if ((fault_type & prot) != fault_type)
512119dc5607STor Egge 		return (KERN_PROTECTION_FAILURE);
512219dc5607STor Egge 
512319dc5607STor Egge 	/*
512419dc5607STor Egge 	 * If this page is not pageable, we have to get it for all possible
512519dc5607STor Egge 	 * accesses.
512619dc5607STor Egge 	 */
512719dc5607STor Egge 	*wired = (entry->wired_count != 0);
512819dc5607STor Egge 	if (*wired)
5129a6d42a0dSAlan Cox 		fault_type = entry->protection;
513019dc5607STor Egge 
513119dc5607STor Egge 	if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
513219dc5607STor Egge 		/*
513319dc5607STor Egge 		 * Fail if the entry was copy-on-write for a write fault.
513419dc5607STor Egge 		 */
513519dc5607STor Egge 		if (fault_type & VM_PROT_WRITE)
513619dc5607STor Egge 			return (KERN_FAILURE);
513719dc5607STor Egge 		/*
513819dc5607STor Egge 		 * We're attempting to read a copy-on-write page --
513919dc5607STor Egge 		 * don't allow writes.
514019dc5607STor Egge 		 */
514119dc5607STor Egge 		prot &= ~VM_PROT_WRITE;
514219dc5607STor Egge 	}
514319dc5607STor Egge 
514419dc5607STor Egge 	/*
514519dc5607STor Egge 	 * Fail if an object should be created.
514619dc5607STor Egge 	 */
514719dc5607STor Egge 	if (entry->object.vm_object == NULL && !map->system_map)
514819dc5607STor Egge 		return (KERN_FAILURE);
514919dc5607STor Egge 
515019dc5607STor Egge 	/*
515119dc5607STor Egge 	 * Return the object/offset from this entry.  If the entry was
515219dc5607STor Egge 	 * copy-on-write or empty, it has been fixed up.
515319dc5607STor Egge 	 */
515410d9120cSKonstantin Belousov 	*pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
515519dc5607STor Egge 	*object = entry->object.vm_object;
515619dc5607STor Egge 
515719dc5607STor Egge 	*out_prot = prot;
515819dc5607STor Egge 	return (KERN_SUCCESS);
515919dc5607STor Egge }
516019dc5607STor Egge 
516119dc5607STor Egge /*
5162df8bae1dSRodney W. Grimes  *	vm_map_lookup_done:
5163df8bae1dSRodney W. Grimes  *
5164df8bae1dSRodney W. Grimes  *	Releases locks acquired by a vm_map_lookup
5165df8bae1dSRodney W. Grimes  *	(according to the handle returned by that lookup).
5166df8bae1dSRodney W. Grimes  */
51670d94caffSDavid Greenman void
51681b40f8c0SMatthew Dillon vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry)
5169df8bae1dSRodney W. Grimes {
5170df8bae1dSRodney W. Grimes 	/*
5171df8bae1dSRodney W. Grimes 	 * Unlock the main-level map
5172df8bae1dSRodney W. Grimes 	 */
5173df8bae1dSRodney W. Grimes 	vm_map_unlock_read(map);
5174df8bae1dSRodney W. Grimes }
5175df8bae1dSRodney W. Grimes 
517619ea042eSKonstantin Belousov vm_offset_t
517719ea042eSKonstantin Belousov vm_map_max_KBI(const struct vm_map *map)
517819ea042eSKonstantin Belousov {
517919ea042eSKonstantin Belousov 
5180f0165b1cSKonstantin Belousov 	return (vm_map_max(map));
518119ea042eSKonstantin Belousov }
518219ea042eSKonstantin Belousov 
518319ea042eSKonstantin Belousov vm_offset_t
518419ea042eSKonstantin Belousov vm_map_min_KBI(const struct vm_map *map)
518519ea042eSKonstantin Belousov {
518619ea042eSKonstantin Belousov 
5187f0165b1cSKonstantin Belousov 	return (vm_map_min(map));
518819ea042eSKonstantin Belousov }
518919ea042eSKonstantin Belousov 
519019ea042eSKonstantin Belousov pmap_t
519119ea042eSKonstantin Belousov vm_map_pmap_KBI(vm_map_t map)
519219ea042eSKonstantin Belousov {
519319ea042eSKonstantin Belousov 
519419ea042eSKonstantin Belousov 	return (map->pmap);
519519ea042eSKonstantin Belousov }
519619ea042eSKonstantin Belousov 
5197a7752896SMark Johnston bool
5198a7752896SMark Johnston vm_map_range_valid_KBI(vm_map_t map, vm_offset_t start, vm_offset_t end)
5199a7752896SMark Johnston {
5200a7752896SMark Johnston 
5201a7752896SMark Johnston 	return (vm_map_range_valid(map, start, end));
5202a7752896SMark Johnston }
5203a7752896SMark Johnston 
5204721899b1SDoug Moore #ifdef INVARIANTS
5205721899b1SDoug Moore static void
5206461587dcSDoug Moore _vm_map_assert_consistent(vm_map_t map, int check)
5207721899b1SDoug Moore {
5208721899b1SDoug Moore 	vm_map_entry_t entry, prev;
5209c1ad5342SDoug Moore 	vm_map_entry_t cur, header, lbound, ubound;
5210721899b1SDoug Moore 	vm_size_t max_left, max_right;
5211721899b1SDoug Moore 
521285b7bedbSDoug Moore #ifdef DIAGNOSTIC
521385b7bedbSDoug Moore 	++map->nupdates;
521485b7bedbSDoug Moore #endif
5215461587dcSDoug Moore 	if (enable_vmmap_check != check)
5216721899b1SDoug Moore 		return;
5217721899b1SDoug Moore 
5218c1ad5342SDoug Moore 	header = prev = &map->header;
5219721899b1SDoug Moore 	VM_MAP_ENTRY_FOREACH(entry, map) {
5220721899b1SDoug Moore 		KASSERT(prev->end <= entry->start,
5221721899b1SDoug Moore 		    ("map %p prev->end = %jx, start = %jx", map,
5222721899b1SDoug Moore 		    (uintmax_t)prev->end, (uintmax_t)entry->start));
5223721899b1SDoug Moore 		KASSERT(entry->start < entry->end,
5224721899b1SDoug Moore 		    ("map %p start = %jx, end = %jx", map,
5225721899b1SDoug Moore 		    (uintmax_t)entry->start, (uintmax_t)entry->end));
5226c1ad5342SDoug Moore 		KASSERT(entry->left == header ||
5227721899b1SDoug Moore 		    entry->left->start < entry->start,
5228721899b1SDoug Moore 		    ("map %p left->start = %jx, start = %jx", map,
5229721899b1SDoug Moore 		    (uintmax_t)entry->left->start, (uintmax_t)entry->start));
5230c1ad5342SDoug Moore 		KASSERT(entry->right == header ||
5231721899b1SDoug Moore 		    entry->start < entry->right->start,
5232721899b1SDoug Moore 		    ("map %p start = %jx, right->start = %jx", map,
5233721899b1SDoug Moore 		    (uintmax_t)entry->start, (uintmax_t)entry->right->start));
5234c1ad5342SDoug Moore 		cur = map->root;
5235c1ad5342SDoug Moore 		lbound = ubound = header;
5236c1ad5342SDoug Moore 		for (;;) {
5237c1ad5342SDoug Moore 			if (entry->start < cur->start) {
5238c1ad5342SDoug Moore 				ubound = cur;
5239c1ad5342SDoug Moore 				cur = cur->left;
5240c1ad5342SDoug Moore 				KASSERT(cur != lbound,
5241c1ad5342SDoug Moore 				    ("map %p cannot find %jx",
5242c0829bb1SMark Johnston 				    map, (uintmax_t)entry->start));
5243c1ad5342SDoug Moore 			} else if (cur->end <= entry->start) {
5244c1ad5342SDoug Moore 				lbound = cur;
5245c1ad5342SDoug Moore 				cur = cur->right;
5246c1ad5342SDoug Moore 				KASSERT(cur != ubound,
5247c1ad5342SDoug Moore 				    ("map %p cannot find %jx",
5248c0829bb1SMark Johnston 				    map, (uintmax_t)entry->start));
5249c1ad5342SDoug Moore 			} else {
5250c1ad5342SDoug Moore 				KASSERT(cur == entry,
5251c1ad5342SDoug Moore 				    ("map %p cannot find %jx",
5252c0829bb1SMark Johnston 				    map, (uintmax_t)entry->start));
5253c1ad5342SDoug Moore 				break;
5254c1ad5342SDoug Moore 			}
5255c1ad5342SDoug Moore 		}
5256c1ad5342SDoug Moore 		max_left = vm_map_entry_max_free_left(entry, lbound);
5257c1ad5342SDoug Moore 		max_right = vm_map_entry_max_free_right(entry, ubound);
5258c1ad5342SDoug Moore 		KASSERT(entry->max_free == vm_size_max(max_left, max_right),
5259721899b1SDoug Moore 		    ("map %p max = %jx, max_left = %jx, max_right = %jx", map,
5260721899b1SDoug Moore 		    (uintmax_t)entry->max_free,
5261721899b1SDoug Moore 		    (uintmax_t)max_left, (uintmax_t)max_right));
5262721899b1SDoug Moore 		prev = entry;
5263721899b1SDoug Moore 	}
5264721899b1SDoug Moore 	KASSERT(prev->end <= entry->start,
5265721899b1SDoug Moore 	    ("map %p prev->end = %jx, start = %jx", map,
5266721899b1SDoug Moore 	    (uintmax_t)prev->end, (uintmax_t)entry->start));
5267721899b1SDoug Moore }
5268721899b1SDoug Moore #endif
5269721899b1SDoug Moore 
5270c7c34a24SBruce Evans #include "opt_ddb.h"
5271c3cb3e12SDavid Greenman #ifdef DDB
5272c7c34a24SBruce Evans #include <sys/kernel.h>
5273c7c34a24SBruce Evans 
5274c7c34a24SBruce Evans #include <ddb/ddb.h>
5275c7c34a24SBruce Evans 
52762ebcd458SAttilio Rao static void
52772ebcd458SAttilio Rao vm_map_print(vm_map_t map)
5278df8bae1dSRodney W. Grimes {
527977131528SDoug Moore 	vm_map_entry_t entry, prev;
5280c7c34a24SBruce Evans 
5281e5f251d2SAlan Cox 	db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
5282e5f251d2SAlan Cox 	    (void *)map,
5283101eeb7fSBruce Evans 	    (void *)map->pmap, map->nentries, map->timestamp);
5284df8bae1dSRodney W. Grimes 
5285c7c34a24SBruce Evans 	db_indent += 2;
5286721899b1SDoug Moore 	prev = &map->header;
5287721899b1SDoug Moore 	VM_MAP_ENTRY_FOREACH(entry, map) {
528819bd0d9cSKonstantin Belousov 		db_iprintf("map entry %p: start=%p, end=%p, eflags=%#x, \n",
528919bd0d9cSKonstantin Belousov 		    (void *)entry, (void *)entry->start, (void *)entry->end,
529019bd0d9cSKonstantin Belousov 		    entry->eflags);
5291e5f251d2SAlan Cox 		{
5292eaa17d42SRyan Libby 			static const char * const inheritance_name[4] =
5293df8bae1dSRodney W. Grimes 			{"share", "copy", "none", "donate_copy"};
52940d94caffSDavid Greenman 
529595e5e988SJohn Dyson 			db_iprintf(" prot=%x/%x/%s",
5296df8bae1dSRodney W. Grimes 			    entry->protection,
5297df8bae1dSRodney W. Grimes 			    entry->max_protection,
529877131528SDoug Moore 			    inheritance_name[(int)(unsigned char)
529977131528SDoug Moore 			    entry->inheritance]);
5300df8bae1dSRodney W. Grimes 			if (entry->wired_count != 0)
530195e5e988SJohn Dyson 				db_printf(", wired");
5302df8bae1dSRodney W. Grimes 		}
53039fdfe602SMatthew Dillon 		if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
5304cd034a5bSMaxime Henrion 			db_printf(", share=%p, offset=0x%jx\n",
53059fdfe602SMatthew Dillon 			    (void *)entry->object.sub_map,
5306cd034a5bSMaxime Henrion 			    (uintmax_t)entry->offset);
530777131528SDoug Moore 			if (prev == &map->header ||
530877131528SDoug Moore 			    prev->object.sub_map !=
530977131528SDoug Moore 				entry->object.sub_map) {
5310c7c34a24SBruce Evans 				db_indent += 2;
53112ebcd458SAttilio Rao 				vm_map_print((vm_map_t)entry->object.sub_map);
5312c7c34a24SBruce Evans 				db_indent -= 2;
5313df8bae1dSRodney W. Grimes 			}
53140d94caffSDavid Greenman 		} else {
5315ef694c1aSEdward Tomasz Napierala 			if (entry->cred != NULL)
5316ef694c1aSEdward Tomasz Napierala 				db_printf(", ruid %d", entry->cred->cr_ruid);
5317cd034a5bSMaxime Henrion 			db_printf(", object=%p, offset=0x%jx",
5318101eeb7fSBruce Evans 			    (void *)entry->object.vm_object,
5319cd034a5bSMaxime Henrion 			    (uintmax_t)entry->offset);
5320ef694c1aSEdward Tomasz Napierala 			if (entry->object.vm_object && entry->object.vm_object->cred)
5321ef694c1aSEdward Tomasz Napierala 				db_printf(", obj ruid %d charge %jx",
5322ef694c1aSEdward Tomasz Napierala 				    entry->object.vm_object->cred->cr_ruid,
53233364c323SKonstantin Belousov 				    (uintmax_t)entry->object.vm_object->charge);
5324afa07f7eSJohn Dyson 			if (entry->eflags & MAP_ENTRY_COW)
5325c7c34a24SBruce Evans 				db_printf(", copy (%s)",
5326afa07f7eSJohn Dyson 				    (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
5327c7c34a24SBruce Evans 			db_printf("\n");
5328df8bae1dSRodney W. Grimes 
532977131528SDoug Moore 			if (prev == &map->header ||
533077131528SDoug Moore 			    prev->object.vm_object !=
533177131528SDoug Moore 				entry->object.vm_object) {
5332c7c34a24SBruce Evans 				db_indent += 2;
5333101eeb7fSBruce Evans 				vm_object_print((db_expr_t)(intptr_t)
5334101eeb7fSBruce Evans 						entry->object.vm_object,
533544bbc3b7SKonstantin Belousov 						0, 0, (char *)0);
5336c7c34a24SBruce Evans 				db_indent -= 2;
5337df8bae1dSRodney W. Grimes 			}
5338df8bae1dSRodney W. Grimes 		}
5339721899b1SDoug Moore 		prev = entry;
5340df8bae1dSRodney W. Grimes 	}
5341c7c34a24SBruce Evans 	db_indent -= 2;
5342df8bae1dSRodney W. Grimes }
534395e5e988SJohn Dyson 
53442ebcd458SAttilio Rao DB_SHOW_COMMAND(map, map)
53452ebcd458SAttilio Rao {
53462ebcd458SAttilio Rao 
53472ebcd458SAttilio Rao 	if (!have_addr) {
53482ebcd458SAttilio Rao 		db_printf("usage: show map <addr>\n");
53492ebcd458SAttilio Rao 		return;
53502ebcd458SAttilio Rao 	}
53512ebcd458SAttilio Rao 	vm_map_print((vm_map_t)addr);
53522ebcd458SAttilio Rao }
535395e5e988SJohn Dyson 
535495e5e988SJohn Dyson DB_SHOW_COMMAND(procvm, procvm)
535595e5e988SJohn Dyson {
535695e5e988SJohn Dyson 	struct proc *p;
535795e5e988SJohn Dyson 
535895e5e988SJohn Dyson 	if (have_addr) {
5359a9546a6bSJohn Baldwin 		p = db_lookup_proc(addr);
536095e5e988SJohn Dyson 	} else {
536195e5e988SJohn Dyson 		p = curproc;
536295e5e988SJohn Dyson 	}
536395e5e988SJohn Dyson 
5364ac1e407bSBruce Evans 	db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
5365ac1e407bSBruce Evans 	    (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
5366b1028ad1SLuoqi Chen 	    (void *)vmspace_pmap(p->p_vmspace));
536795e5e988SJohn Dyson 
53682ebcd458SAttilio Rao 	vm_map_print((vm_map_t)&p->p_vmspace->vm_map);
536995e5e988SJohn Dyson }
537095e5e988SJohn Dyson 
5371c7c34a24SBruce Evans #endif /* DDB */
5372