xref: /freebsd/sys/vm/vm_map.c (revision 582ec34cd83de37af4dab174f7c1f61ef9e043fe)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
6df8bae1dSRodney W. Grimes  * The Mach Operating System project at Carnegie-Mellon University.
7df8bae1dSRodney W. Grimes  *
8df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
9df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
10df8bae1dSRodney W. Grimes  * are met:
11df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
12df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
13df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
15df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
16df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
175929bcfaSPhilippe Charnier  *    must display the following acknowledgement:
18df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
19df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
20df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
21df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
22df8bae1dSRodney W. Grimes  *    without specific prior written permission.
23df8bae1dSRodney W. Grimes  *
24df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
35df8bae1dSRodney W. Grimes  *
363c4dd356SDavid Greenman  *	from: @(#)vm_map.c	8.3 (Berkeley) 1/12/94
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  *
39df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
40df8bae1dSRodney W. Grimes  * All rights reserved.
41df8bae1dSRodney W. Grimes  *
42df8bae1dSRodney W. Grimes  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
43df8bae1dSRodney W. Grimes  *
44df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
45df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
46df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
47df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
48df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
49df8bae1dSRodney W. Grimes  *
50df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
51df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
52df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
53df8bae1dSRodney W. Grimes  *
54df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
55df8bae1dSRodney W. Grimes  *
56df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
57df8bae1dSRodney W. Grimes  *  School of Computer Science
58df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
59df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
60df8bae1dSRodney W. Grimes  *
61df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
62df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
633c4dd356SDavid Greenman  *
64c3aac50fSPeter Wemm  * $FreeBSD$
65df8bae1dSRodney W. Grimes  */
66df8bae1dSRodney W. Grimes 
67df8bae1dSRodney W. Grimes /*
68df8bae1dSRodney W. Grimes  *	Virtual memory mapping module.
69df8bae1dSRodney W. Grimes  */
70df8bae1dSRodney W. Grimes 
71df8bae1dSRodney W. Grimes #include <sys/param.h>
72df8bae1dSRodney W. Grimes #include <sys/systm.h>
7361d80e90SJohn Baldwin #include <sys/ktr.h>
74fb919e4dSMark Murray #include <sys/lock.h>
75fb919e4dSMark Murray #include <sys/mutex.h>
76b5e8ce9fSBruce Evans #include <sys/proc.h>
77efeaf95aSDavid Greenman #include <sys/vmmeter.h>
78867a482dSJohn Dyson #include <sys/mman.h>
791efb74fbSJohn Dyson #include <sys/vnode.h>
802267af78SJulian Elischer #include <sys/resourcevar.h>
81df8bae1dSRodney W. Grimes 
82df8bae1dSRodney W. Grimes #include <vm/vm.h>
83efeaf95aSDavid Greenman #include <vm/vm_param.h>
84efeaf95aSDavid Greenman #include <vm/pmap.h>
85efeaf95aSDavid Greenman #include <vm/vm_map.h>
86df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
87df8bae1dSRodney W. Grimes #include <vm/vm_object.h>
8847221757SJohn Dyson #include <vm/vm_pager.h>
8926f9a767SRodney W. Grimes #include <vm/vm_kern.h>
90efeaf95aSDavid Greenman #include <vm/vm_extern.h>
913075778bSJohn Dyson #include <vm/vm_zone.h>
9221cd6e62SSeigo Tanimura #include <vm/swap_pager.h>
93df8bae1dSRodney W. Grimes 
94df8bae1dSRodney W. Grimes /*
95df8bae1dSRodney W. Grimes  *	Virtual memory maps provide for the mapping, protection,
96df8bae1dSRodney W. Grimes  *	and sharing of virtual memory objects.  In addition,
97df8bae1dSRodney W. Grimes  *	this module provides for an efficient virtual copy of
98df8bae1dSRodney W. Grimes  *	memory from one map to another.
99df8bae1dSRodney W. Grimes  *
100df8bae1dSRodney W. Grimes  *	Synchronization is required prior to most operations.
101df8bae1dSRodney W. Grimes  *
102df8bae1dSRodney W. Grimes  *	Maps consist of an ordered doubly-linked list of simple
103df8bae1dSRodney W. Grimes  *	entries; a single hint is used to speed up lookups.
104df8bae1dSRodney W. Grimes  *
105956f3135SPhilippe Charnier  *	Since portions of maps are specified by start/end addresses,
106df8bae1dSRodney W. Grimes  *	which may not align with existing map entries, all
107df8bae1dSRodney W. Grimes  *	routines merely "clip" entries to these start/end values.
108df8bae1dSRodney W. Grimes  *	[That is, an entry is split into two, bordering at a
109df8bae1dSRodney W. Grimes  *	start or end value.]  Note that these clippings may not
110df8bae1dSRodney W. Grimes  *	always be necessary (as the two resulting entries are then
111df8bae1dSRodney W. Grimes  *	not changed); however, the clipping is done for convenience.
112df8bae1dSRodney W. Grimes  *
113df8bae1dSRodney W. Grimes  *	As mentioned above, virtual copy operations are performed
114ad5fca3bSAlan Cox  *	by copying VM object references from one map to
115df8bae1dSRodney W. Grimes  *	another, and then marking both regions as copy-on-write.
116df8bae1dSRodney W. Grimes  */
117df8bae1dSRodney W. Grimes 
118df8bae1dSRodney W. Grimes /*
119df8bae1dSRodney W. Grimes  *	vm_map_startup:
120df8bae1dSRodney W. Grimes  *
121df8bae1dSRodney W. Grimes  *	Initialize the vm_map module.  Must be called before
122df8bae1dSRodney W. Grimes  *	any other vm_map routines.
123df8bae1dSRodney W. Grimes  *
124df8bae1dSRodney W. Grimes  *	Map and entry structures are allocated from the general
125df8bae1dSRodney W. Grimes  *	purpose memory pool with some exceptions:
126df8bae1dSRodney W. Grimes  *
127df8bae1dSRodney W. Grimes  *	- The kernel map and kmem submap are allocated statically.
128df8bae1dSRodney W. Grimes  *	- Kernel map entries are allocated out of a static pool.
129df8bae1dSRodney W. Grimes  *
130df8bae1dSRodney W. Grimes  *	These restrictions are necessary since malloc() uses the
131df8bae1dSRodney W. Grimes  *	maps and requires map entries.
132df8bae1dSRodney W. Grimes  */
133df8bae1dSRodney W. Grimes 
1343075778bSJohn Dyson static struct vm_zone kmapentzone_store, mapentzone_store, mapzone_store;
1352d8acc0fSJohn Dyson static vm_zone_t mapentzone, kmapentzone, mapzone, vmspace_zone;
1363075778bSJohn Dyson static struct vm_object kmapentobj, mapentobj, mapobj;
1371fc43fd1SAlan Cox 
138303b270bSEivind Eklund static struct vm_map_entry map_entry_init[MAX_MAPENT];
139303b270bSEivind Eklund static struct vm_map_entry kmap_entry_init[MAX_KMAPENT];
140303b270bSEivind Eklund static struct vm_map map_init[MAX_KMAP];
141b18bfc3dSJohn Dyson 
1420d94caffSDavid Greenman void
1431b40f8c0SMatthew Dillon vm_map_startup(void)
144df8bae1dSRodney W. Grimes {
1453075778bSJohn Dyson 	mapzone = &mapzone_store;
1460d65e566SJohn Dyson 	zbootinit(mapzone, "MAP", sizeof (struct vm_map),
1473075778bSJohn Dyson 		map_init, MAX_KMAP);
1483075778bSJohn Dyson 	kmapentzone = &kmapentzone_store;
1490d65e566SJohn Dyson 	zbootinit(kmapentzone, "KMAP ENTRY", sizeof (struct vm_map_entry),
1503075778bSJohn Dyson 		kmap_entry_init, MAX_KMAPENT);
1513075778bSJohn Dyson 	mapentzone = &mapentzone_store;
1520d65e566SJohn Dyson 	zbootinit(mapentzone, "MAP ENTRY", sizeof (struct vm_map_entry),
1533075778bSJohn Dyson 		map_entry_init, MAX_MAPENT);
154df8bae1dSRodney W. Grimes }
155df8bae1dSRodney W. Grimes 
156df8bae1dSRodney W. Grimes /*
157df8bae1dSRodney W. Grimes  * Allocate a vmspace structure, including a vm_map and pmap,
158df8bae1dSRodney W. Grimes  * and initialize those structures.  The refcnt is set to 1.
159df8bae1dSRodney W. Grimes  * The remaining fields must be initialized by the caller.
160df8bae1dSRodney W. Grimes  */
161df8bae1dSRodney W. Grimes struct vmspace *
1622d8acc0fSJohn Dyson vmspace_alloc(min, max)
163df8bae1dSRodney W. Grimes 	vm_offset_t min, max;
164df8bae1dSRodney W. Grimes {
165c0877f10SJohn Dyson 	struct vmspace *vm;
1660d94caffSDavid Greenman 
1670cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
1682d8acc0fSJohn Dyson 	vm = zalloc(vmspace_zone);
16921c641b2SJohn Baldwin 	CTR1(KTR_VM, "vmspace_alloc: %p", vm);
1702d8acc0fSJohn Dyson 	vm_map_init(&vm->vm_map, min, max);
171b1028ad1SLuoqi Chen 	pmap_pinit(vmspace_pmap(vm));
172b1028ad1SLuoqi Chen 	vm->vm_map.pmap = vmspace_pmap(vm);		/* XXX */
173df8bae1dSRodney W. Grimes 	vm->vm_refcnt = 1;
1742d8acc0fSJohn Dyson 	vm->vm_shm = NULL;
175582ec34cSAlfred Perlstein 	vm->vm_freer = NULL;
176df8bae1dSRodney W. Grimes 	return (vm);
177df8bae1dSRodney W. Grimes }
178df8bae1dSRodney W. Grimes 
179df8bae1dSRodney W. Grimes void
1801b40f8c0SMatthew Dillon vm_init2(void)
1811b40f8c0SMatthew Dillon {
1820d65e566SJohn Dyson 	zinitna(kmapentzone, &kmapentobj,
1830a80f406SJohn Dyson 		NULL, 0, cnt.v_page_count / 4, ZONE_INTERRUPT, 1);
1840d65e566SJohn Dyson 	zinitna(mapentzone, &mapentobj,
1850a80f406SJohn Dyson 		NULL, 0, 0, 0, 1);
1860d65e566SJohn Dyson 	zinitna(mapzone, &mapobj,
1870a80f406SJohn Dyson 		NULL, 0, 0, 0, 1);
1882d8acc0fSJohn Dyson 	vmspace_zone = zinit("VMSPACE", sizeof (struct vmspace), 0, 0, 3);
189ba9be04cSJohn Dyson 	pmap_init2();
19099448ed1SJohn Dyson 	vm_object_init2();
1913075778bSJohn Dyson }
1923075778bSJohn Dyson 
193582ec34cSAlfred Perlstein static __inline void
194582ec34cSAlfred Perlstein vmspace_dofree( struct vmspace *vm)
195df8bae1dSRodney W. Grimes {
19621c641b2SJohn Baldwin 	CTR1(KTR_VM, "vmspace_free: %p", vm);
19730dcfc09SJohn Dyson 	/*
198df8bae1dSRodney W. Grimes 	 * Lock the map, to wait out all other references to it.
1990d94caffSDavid Greenman 	 * Delete all of the mappings and pages they hold, then call
2000d94caffSDavid Greenman 	 * the pmap module to reclaim anything left.
201df8bae1dSRodney W. Grimes 	 */
202df8bae1dSRodney W. Grimes 	vm_map_lock(&vm->vm_map);
203df8bae1dSRodney W. Grimes 	(void) vm_map_delete(&vm->vm_map, vm->vm_map.min_offset,
204df8bae1dSRodney W. Grimes 	    vm->vm_map.max_offset);
205a1f6d91cSDavid Greenman 	vm_map_unlock(&vm->vm_map);
206b1028ad1SLuoqi Chen 	pmap_release(vmspace_pmap(vm));
207a18b1f1dSJason Evans 	vm_map_destroy(&vm->vm_map);
2082d8acc0fSJohn Dyson 	zfree(vmspace_zone, vm);
209df8bae1dSRodney W. Grimes }
210582ec34cSAlfred Perlstein 
211582ec34cSAlfred Perlstein void
212582ec34cSAlfred Perlstein vmspace_free(struct vmspace *vm)
213582ec34cSAlfred Perlstein {
214582ec34cSAlfred Perlstein 	GIANT_REQUIRED;
215582ec34cSAlfred Perlstein 
216582ec34cSAlfred Perlstein 	if (vm->vm_refcnt == 0)
217582ec34cSAlfred Perlstein 		panic("vmspace_free: attempt to free already freed vmspace");
218582ec34cSAlfred Perlstein 
219582ec34cSAlfred Perlstein 	if (--vm->vm_refcnt == 0)
220582ec34cSAlfred Perlstein 		vmspace_dofree(vm);
221582ec34cSAlfred Perlstein }
222582ec34cSAlfred Perlstein 
223582ec34cSAlfred Perlstein void
224582ec34cSAlfred Perlstein vmspace_exitfree(struct proc *p)
225582ec34cSAlfred Perlstein {
226582ec34cSAlfred Perlstein 	GIANT_REQUIRED;
227582ec34cSAlfred Perlstein 
228582ec34cSAlfred Perlstein 	if (p == p->p_vmspace->vm_freer)
229582ec34cSAlfred Perlstein 		vmspace_dofree(p->p_vmspace);
230df8bae1dSRodney W. Grimes }
231df8bae1dSRodney W. Grimes 
232df8bae1dSRodney W. Grimes /*
233ff2b5645SMatthew Dillon  * vmspace_swap_count() - count the approximate swap useage in pages for a
234ff2b5645SMatthew Dillon  *			  vmspace.
235ff2b5645SMatthew Dillon  *
236ff2b5645SMatthew Dillon  *	Swap useage is determined by taking the proportional swap used by
237ff2b5645SMatthew Dillon  *	VM objects backing the VM map.  To make up for fractional losses,
238ff2b5645SMatthew Dillon  *	if the VM object has any swap use at all the associated map entries
239ff2b5645SMatthew Dillon  *	count for at least 1 swap page.
240ff2b5645SMatthew Dillon  */
241ff2b5645SMatthew Dillon int
242ff2b5645SMatthew Dillon vmspace_swap_count(struct vmspace *vmspace)
243ff2b5645SMatthew Dillon {
244ff2b5645SMatthew Dillon 	vm_map_t map = &vmspace->vm_map;
245ff2b5645SMatthew Dillon 	vm_map_entry_t cur;
246ff2b5645SMatthew Dillon 	int count = 0;
247ff2b5645SMatthew Dillon 
248ff2b5645SMatthew Dillon 	for (cur = map->header.next; cur != &map->header; cur = cur->next) {
249ff2b5645SMatthew Dillon 		vm_object_t object;
250ff2b5645SMatthew Dillon 
251ff2b5645SMatthew Dillon 		if ((cur->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
252ff2b5645SMatthew Dillon 		    (object = cur->object.vm_object) != NULL &&
253ff2b5645SMatthew Dillon 		    object->type == OBJT_SWAP
254ff2b5645SMatthew Dillon 		) {
255ff2b5645SMatthew Dillon 			int n = (cur->end - cur->start) / PAGE_SIZE;
256ff2b5645SMatthew Dillon 
257ff2b5645SMatthew Dillon 			if (object->un_pager.swp.swp_bcount) {
258ef6a93efSMatthew Dillon 				count += object->un_pager.swp.swp_bcount *
259ef6a93efSMatthew Dillon 				    SWAP_META_PAGES * n / object->size + 1;
260ff2b5645SMatthew Dillon 			}
261ff2b5645SMatthew Dillon 		}
262ff2b5645SMatthew Dillon 	}
263ff2b5645SMatthew Dillon 	return(count);
264ff2b5645SMatthew Dillon }
265ff2b5645SMatthew Dillon 
2661b40f8c0SMatthew Dillon u_char
2671b40f8c0SMatthew Dillon vm_map_entry_behavior(struct vm_map_entry *entry)
2681b40f8c0SMatthew Dillon {
2691b40f8c0SMatthew Dillon 	return entry->eflags & MAP_ENTRY_BEHAV_MASK;
2701b40f8c0SMatthew Dillon }
2711b40f8c0SMatthew Dillon 
2721b40f8c0SMatthew Dillon void
2731b40f8c0SMatthew Dillon vm_map_entry_set_behavior(struct vm_map_entry *entry, u_char behavior)
2741b40f8c0SMatthew Dillon {
2751b40f8c0SMatthew Dillon 	entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) |
2761b40f8c0SMatthew Dillon 		(behavior & MAP_ENTRY_BEHAV_MASK);
2771b40f8c0SMatthew Dillon }
2781b40f8c0SMatthew Dillon 
2791b40f8c0SMatthew Dillon void
2801b40f8c0SMatthew Dillon vm_map_lock(vm_map_t map)
2811b40f8c0SMatthew Dillon {
2821b40f8c0SMatthew Dillon 	vm_map_printf("locking map LK_EXCLUSIVE: %p\n", map);
283b40ce416SJulian Elischer 	if (lockmgr(&map->lock, LK_EXCLUSIVE, NULL, curthread) != 0)
2841b40f8c0SMatthew Dillon 		panic("vm_map_lock: failed to get lock");
2851b40f8c0SMatthew Dillon 	map->timestamp++;
2861b40f8c0SMatthew Dillon }
2871b40f8c0SMatthew Dillon 
2881b40f8c0SMatthew Dillon void
2891b40f8c0SMatthew Dillon vm_map_unlock(vm_map_t map)
2901b40f8c0SMatthew Dillon {
2911b40f8c0SMatthew Dillon 	vm_map_printf("locking map LK_RELEASE: %p\n", map);
292b40ce416SJulian Elischer 	lockmgr(&(map)->lock, LK_RELEASE, NULL, curthread);
2931b40f8c0SMatthew Dillon }
2941b40f8c0SMatthew Dillon 
2951b40f8c0SMatthew Dillon void
2961b40f8c0SMatthew Dillon vm_map_lock_read(vm_map_t map)
2971b40f8c0SMatthew Dillon {
2981b40f8c0SMatthew Dillon 	vm_map_printf("locking map LK_SHARED: %p\n", map);
299b40ce416SJulian Elischer 	lockmgr(&(map)->lock, LK_SHARED, NULL, curthread);
3001b40f8c0SMatthew Dillon }
3011b40f8c0SMatthew Dillon 
3021b40f8c0SMatthew Dillon void
3031b40f8c0SMatthew Dillon vm_map_unlock_read(vm_map_t map)
3041b40f8c0SMatthew Dillon {
3051b40f8c0SMatthew Dillon 	vm_map_printf("locking map LK_RELEASE: %p\n", map);
306b40ce416SJulian Elischer 	lockmgr(&(map)->lock, LK_RELEASE, NULL, curthread);
3071b40f8c0SMatthew Dillon }
3081b40f8c0SMatthew Dillon 
3091b40f8c0SMatthew Dillon static __inline__ int
310b40ce416SJulian Elischer _vm_map_lock_upgrade(vm_map_t map, struct thread *td) {
3111b40f8c0SMatthew Dillon 	int error;
3121b40f8c0SMatthew Dillon 
3131b40f8c0SMatthew Dillon 	vm_map_printf("locking map LK_EXCLUPGRADE: %p\n", map);
314b40ce416SJulian Elischer 	error = lockmgr(&map->lock, LK_EXCLUPGRADE, NULL, td);
3151b40f8c0SMatthew Dillon 	if (error == 0)
3161b40f8c0SMatthew Dillon 		map->timestamp++;
3171b40f8c0SMatthew Dillon 	return error;
3181b40f8c0SMatthew Dillon }
3191b40f8c0SMatthew Dillon 
3201b40f8c0SMatthew Dillon int
3211b40f8c0SMatthew Dillon vm_map_lock_upgrade(vm_map_t map)
3221b40f8c0SMatthew Dillon {
323b40ce416SJulian Elischer     return(_vm_map_lock_upgrade(map, curthread));
3241b40f8c0SMatthew Dillon }
3251b40f8c0SMatthew Dillon 
3261b40f8c0SMatthew Dillon void
3271b40f8c0SMatthew Dillon vm_map_lock_downgrade(vm_map_t map)
3281b40f8c0SMatthew Dillon {
3291b40f8c0SMatthew Dillon 	vm_map_printf("locking map LK_DOWNGRADE: %p\n", map);
330b40ce416SJulian Elischer 	lockmgr(&map->lock, LK_DOWNGRADE, NULL, curthread);
3311b40f8c0SMatthew Dillon }
3321b40f8c0SMatthew Dillon 
3331b40f8c0SMatthew Dillon void
3341b40f8c0SMatthew Dillon vm_map_set_recursive(vm_map_t map)
3351b40f8c0SMatthew Dillon {
3361b40f8c0SMatthew Dillon 	mtx_lock((map)->lock.lk_interlock);
3371b40f8c0SMatthew Dillon 	map->lock.lk_flags |= LK_CANRECURSE;
3381b40f8c0SMatthew Dillon 	mtx_unlock((map)->lock.lk_interlock);
3391b40f8c0SMatthew Dillon }
3401b40f8c0SMatthew Dillon 
3411b40f8c0SMatthew Dillon void
3421b40f8c0SMatthew Dillon vm_map_clear_recursive(vm_map_t map)
3431b40f8c0SMatthew Dillon {
3441b40f8c0SMatthew Dillon 	mtx_lock((map)->lock.lk_interlock);
3451b40f8c0SMatthew Dillon 	map->lock.lk_flags &= ~LK_CANRECURSE;
3461b40f8c0SMatthew Dillon 	mtx_unlock((map)->lock.lk_interlock);
3471b40f8c0SMatthew Dillon }
3481b40f8c0SMatthew Dillon 
3491b40f8c0SMatthew Dillon vm_offset_t
3501b40f8c0SMatthew Dillon vm_map_min(vm_map_t map)
3511b40f8c0SMatthew Dillon {
3521b40f8c0SMatthew Dillon 	return(map->min_offset);
3531b40f8c0SMatthew Dillon }
3541b40f8c0SMatthew Dillon 
3551b40f8c0SMatthew Dillon vm_offset_t
3561b40f8c0SMatthew Dillon vm_map_max(vm_map_t map)
3571b40f8c0SMatthew Dillon {
3581b40f8c0SMatthew Dillon 	return(map->max_offset);
3591b40f8c0SMatthew Dillon }
3601b40f8c0SMatthew Dillon 
3611b40f8c0SMatthew Dillon struct pmap *
3621b40f8c0SMatthew Dillon vm_map_pmap(vm_map_t map)
3631b40f8c0SMatthew Dillon {
3641b40f8c0SMatthew Dillon 	return(map->pmap);
3651b40f8c0SMatthew Dillon }
3661b40f8c0SMatthew Dillon 
3671b40f8c0SMatthew Dillon struct pmap *
3681b40f8c0SMatthew Dillon vmspace_pmap(struct vmspace *vmspace)
3691b40f8c0SMatthew Dillon {
3701b40f8c0SMatthew Dillon 	return &vmspace->vm_pmap;
3711b40f8c0SMatthew Dillon }
3721b40f8c0SMatthew Dillon 
3731b40f8c0SMatthew Dillon long
3741b40f8c0SMatthew Dillon vmspace_resident_count(struct vmspace *vmspace)
3751b40f8c0SMatthew Dillon {
3761b40f8c0SMatthew Dillon 	return pmap_resident_count(vmspace_pmap(vmspace));
3771b40f8c0SMatthew Dillon }
3781b40f8c0SMatthew Dillon 
379ff2b5645SMatthew Dillon /*
380df8bae1dSRodney W. Grimes  *	vm_map_create:
381df8bae1dSRodney W. Grimes  *
382df8bae1dSRodney W. Grimes  *	Creates and returns a new empty VM map with
383df8bae1dSRodney W. Grimes  *	the given physical map structure, and having
384df8bae1dSRodney W. Grimes  *	the given lower and upper address bounds.
385df8bae1dSRodney W. Grimes  */
3860d94caffSDavid Greenman vm_map_t
3871b40f8c0SMatthew Dillon vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max)
388df8bae1dSRodney W. Grimes {
389c0877f10SJohn Dyson 	vm_map_t result;
390df8bae1dSRodney W. Grimes 
3910cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
3920cddd8f0SMatthew Dillon 
3933075778bSJohn Dyson 	result = zalloc(mapzone);
39421c641b2SJohn Baldwin 	CTR1(KTR_VM, "vm_map_create: %p", result);
3952d8acc0fSJohn Dyson 	vm_map_init(result, min, max);
396df8bae1dSRodney W. Grimes 	result->pmap = pmap;
397df8bae1dSRodney W. Grimes 	return (result);
398df8bae1dSRodney W. Grimes }
399df8bae1dSRodney W. Grimes 
400df8bae1dSRodney W. Grimes /*
401df8bae1dSRodney W. Grimes  * Initialize an existing vm_map structure
402df8bae1dSRodney W. Grimes  * such as that in the vmspace structure.
403df8bae1dSRodney W. Grimes  * The pmap is set elsewhere.
404df8bae1dSRodney W. Grimes  */
405df8bae1dSRodney W. Grimes void
4061b40f8c0SMatthew Dillon vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max)
407df8bae1dSRodney W. Grimes {
4080cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
40921c641b2SJohn Baldwin 
410df8bae1dSRodney W. Grimes 	map->header.next = map->header.prev = &map->header;
411df8bae1dSRodney W. Grimes 	map->nentries = 0;
412df8bae1dSRodney W. Grimes 	map->size = 0;
4133075778bSJohn Dyson 	map->system_map = 0;
414b823bbd6SMatthew Dillon 	map->infork = 0;
415df8bae1dSRodney W. Grimes 	map->min_offset = min;
416df8bae1dSRodney W. Grimes 	map->max_offset = max;
417df8bae1dSRodney W. Grimes 	map->first_free = &map->header;
418df8bae1dSRodney W. Grimes 	map->hint = &map->header;
419df8bae1dSRodney W. Grimes 	map->timestamp = 0;
4208f9110f6SJohn Dyson 	lockinit(&map->lock, PVM, "thrd_sleep", 0, LK_NOPAUSE);
421df8bae1dSRodney W. Grimes }
422df8bae1dSRodney W. Grimes 
423a18b1f1dSJason Evans void
424a18b1f1dSJason Evans vm_map_destroy(map)
425a18b1f1dSJason Evans 	struct vm_map *map;
426a18b1f1dSJason Evans {
4270cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
428a18b1f1dSJason Evans 	lockdestroy(&map->lock);
429a18b1f1dSJason Evans }
430a18b1f1dSJason Evans 
431df8bae1dSRodney W. Grimes /*
432b18bfc3dSJohn Dyson  *	vm_map_entry_dispose:	[ internal use only ]
433b18bfc3dSJohn Dyson  *
434b18bfc3dSJohn Dyson  *	Inverse of vm_map_entry_create.
435b18bfc3dSJohn Dyson  */
43662487bb4SJohn Dyson static void
4371b40f8c0SMatthew Dillon vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry)
438b18bfc3dSJohn Dyson {
439b79933ebSJohn Dyson 	zfree((map->system_map || !mapentzone) ? kmapentzone : mapentzone, entry);
440b18bfc3dSJohn Dyson }
441b18bfc3dSJohn Dyson 
442b18bfc3dSJohn Dyson /*
443df8bae1dSRodney W. Grimes  *	vm_map_entry_create:	[ internal use only ]
444df8bae1dSRodney W. Grimes  *
445df8bae1dSRodney W. Grimes  *	Allocates a VM map entry for insertion.
446b28cb1caSAlfred Perlstein  *	No entry fields are filled in.
447df8bae1dSRodney W. Grimes  */
448f708ef1bSPoul-Henning Kamp static vm_map_entry_t
4491b40f8c0SMatthew Dillon vm_map_entry_create(vm_map_t map)
450df8bae1dSRodney W. Grimes {
4511f6889a1SMatthew Dillon 	vm_map_entry_t new_entry;
4521f6889a1SMatthew Dillon 
4531f6889a1SMatthew Dillon 	new_entry = zalloc((map->system_map || !mapentzone) ?
4541f6889a1SMatthew Dillon 		kmapentzone : mapentzone);
4551f6889a1SMatthew Dillon 	if (new_entry == NULL)
4561f6889a1SMatthew Dillon 	    panic("vm_map_entry_create: kernel resources exhausted");
4571f6889a1SMatthew Dillon 	return(new_entry);
458df8bae1dSRodney W. Grimes }
459df8bae1dSRodney W. Grimes 
460df8bae1dSRodney W. Grimes /*
461df8bae1dSRodney W. Grimes  *	vm_map_entry_{un,}link:
462df8bae1dSRodney W. Grimes  *
463df8bae1dSRodney W. Grimes  *	Insert/remove entries from maps.
464df8bae1dSRodney W. Grimes  */
46599c81ca9SAlan Cox static __inline void
46699c81ca9SAlan Cox vm_map_entry_link(vm_map_t map,
46799c81ca9SAlan Cox 		  vm_map_entry_t after_where,
46899c81ca9SAlan Cox 		  vm_map_entry_t entry)
46999c81ca9SAlan Cox {
47021c641b2SJohn Baldwin 
47121c641b2SJohn Baldwin 	CTR4(KTR_VM,
47221c641b2SJohn Baldwin 	    "vm_map_entry_link: map %p, nentries %d, entry %p, after %p", map,
47321c641b2SJohn Baldwin 	    map->nentries, entry, after_where);
47499c81ca9SAlan Cox 	map->nentries++;
47599c81ca9SAlan Cox 	entry->prev = after_where;
47699c81ca9SAlan Cox 	entry->next = after_where->next;
47799c81ca9SAlan Cox 	entry->next->prev = entry;
47899c81ca9SAlan Cox 	after_where->next = entry;
479df8bae1dSRodney W. Grimes }
48099c81ca9SAlan Cox 
48199c81ca9SAlan Cox static __inline void
48299c81ca9SAlan Cox vm_map_entry_unlink(vm_map_t map,
48399c81ca9SAlan Cox 		    vm_map_entry_t entry)
48499c81ca9SAlan Cox {
48599c81ca9SAlan Cox 	vm_map_entry_t prev = entry->prev;
48699c81ca9SAlan Cox 	vm_map_entry_t next = entry->next;
48799c81ca9SAlan Cox 
48899c81ca9SAlan Cox 	next->prev = prev;
48999c81ca9SAlan Cox 	prev->next = next;
49099c81ca9SAlan Cox 	map->nentries--;
49121c641b2SJohn Baldwin 	CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map,
49221c641b2SJohn Baldwin 	    map->nentries, entry);
493df8bae1dSRodney W. Grimes }
494df8bae1dSRodney W. Grimes 
495df8bae1dSRodney W. Grimes /*
496df8bae1dSRodney W. Grimes  *	SAVE_HINT:
497df8bae1dSRodney W. Grimes  *
498df8bae1dSRodney W. Grimes  *	Saves the specified entry as the hint for
49924a1cce3SDavid Greenman  *	future lookups.
500df8bae1dSRodney W. Grimes  */
501df8bae1dSRodney W. Grimes #define	SAVE_HINT(map,value) \
50224a1cce3SDavid Greenman 		(map)->hint = (value);
503df8bae1dSRodney W. Grimes 
504df8bae1dSRodney W. Grimes /*
505df8bae1dSRodney W. Grimes  *	vm_map_lookup_entry:	[ internal use only ]
506df8bae1dSRodney W. Grimes  *
507df8bae1dSRodney W. Grimes  *	Finds the map entry containing (or
508df8bae1dSRodney W. Grimes  *	immediately preceding) the specified address
509df8bae1dSRodney W. Grimes  *	in the given map; the entry is returned
510df8bae1dSRodney W. Grimes  *	in the "entry" parameter.  The boolean
511df8bae1dSRodney W. Grimes  *	result indicates whether the address is
512df8bae1dSRodney W. Grimes  *	actually contained in the map.
513df8bae1dSRodney W. Grimes  */
5140d94caffSDavid Greenman boolean_t
5151b40f8c0SMatthew Dillon vm_map_lookup_entry(
5161b40f8c0SMatthew Dillon 	vm_map_t map,
5171b40f8c0SMatthew Dillon 	vm_offset_t address,
5181b40f8c0SMatthew Dillon 	vm_map_entry_t *entry)	/* OUT */
519df8bae1dSRodney W. Grimes {
520c0877f10SJohn Dyson 	vm_map_entry_t cur;
521c0877f10SJohn Dyson 	vm_map_entry_t last;
522df8bae1dSRodney W. Grimes 
5230cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
524df8bae1dSRodney W. Grimes 	/*
5250d94caffSDavid Greenman 	 * Start looking either from the head of the list, or from the hint.
526df8bae1dSRodney W. Grimes 	 */
527df8bae1dSRodney W. Grimes 
528df8bae1dSRodney W. Grimes 	cur = map->hint;
529df8bae1dSRodney W. Grimes 
530df8bae1dSRodney W. Grimes 	if (cur == &map->header)
531df8bae1dSRodney W. Grimes 		cur = cur->next;
532df8bae1dSRodney W. Grimes 
533df8bae1dSRodney W. Grimes 	if (address >= cur->start) {
534df8bae1dSRodney W. Grimes 		/*
535df8bae1dSRodney W. Grimes 		 * Go from hint to end of list.
536df8bae1dSRodney W. Grimes 		 *
5370d94caffSDavid Greenman 		 * But first, make a quick check to see if we are already looking
5380d94caffSDavid Greenman 		 * at the entry we want (which is usually the case). Note also
5390d94caffSDavid Greenman 		 * that we don't need to save the hint here... it is the same
5400d94caffSDavid Greenman 		 * hint (unless we are at the header, in which case the hint
5410d94caffSDavid Greenman 		 * didn't buy us anything anyway).
542df8bae1dSRodney W. Grimes 		 */
543df8bae1dSRodney W. Grimes 		last = &map->header;
544df8bae1dSRodney W. Grimes 		if ((cur != last) && (cur->end > address)) {
545df8bae1dSRodney W. Grimes 			*entry = cur;
546df8bae1dSRodney W. Grimes 			return (TRUE);
547df8bae1dSRodney W. Grimes 		}
5480d94caffSDavid Greenman 	} else {
549df8bae1dSRodney W. Grimes 		/*
550df8bae1dSRodney W. Grimes 		 * Go from start to hint, *inclusively*
551df8bae1dSRodney W. Grimes 		 */
552df8bae1dSRodney W. Grimes 		last = cur->next;
553df8bae1dSRodney W. Grimes 		cur = map->header.next;
554df8bae1dSRodney W. Grimes 	}
555df8bae1dSRodney W. Grimes 
556df8bae1dSRodney W. Grimes 	/*
557df8bae1dSRodney W. Grimes 	 * Search linearly
558df8bae1dSRodney W. Grimes 	 */
559df8bae1dSRodney W. Grimes 
560df8bae1dSRodney W. Grimes 	while (cur != last) {
561df8bae1dSRodney W. Grimes 		if (cur->end > address) {
562df8bae1dSRodney W. Grimes 			if (address >= cur->start) {
563df8bae1dSRodney W. Grimes 				/*
5640d94caffSDavid Greenman 				 * Save this lookup for future hints, and
5650d94caffSDavid Greenman 				 * return
566df8bae1dSRodney W. Grimes 				 */
567df8bae1dSRodney W. Grimes 
568df8bae1dSRodney W. Grimes 				*entry = cur;
569df8bae1dSRodney W. Grimes 				SAVE_HINT(map, cur);
570df8bae1dSRodney W. Grimes 				return (TRUE);
571df8bae1dSRodney W. Grimes 			}
572df8bae1dSRodney W. Grimes 			break;
573df8bae1dSRodney W. Grimes 		}
574df8bae1dSRodney W. Grimes 		cur = cur->next;
575df8bae1dSRodney W. Grimes 	}
576df8bae1dSRodney W. Grimes 	*entry = cur->prev;
577df8bae1dSRodney W. Grimes 	SAVE_HINT(map, *entry);
578df8bae1dSRodney W. Grimes 	return (FALSE);
579df8bae1dSRodney W. Grimes }
580df8bae1dSRodney W. Grimes 
581df8bae1dSRodney W. Grimes /*
58230dcfc09SJohn Dyson  *	vm_map_insert:
58330dcfc09SJohn Dyson  *
58430dcfc09SJohn Dyson  *	Inserts the given whole VM object into the target
58530dcfc09SJohn Dyson  *	map at the specified address range.  The object's
58630dcfc09SJohn Dyson  *	size should match that of the address range.
58730dcfc09SJohn Dyson  *
58830dcfc09SJohn Dyson  *	Requires that the map be locked, and leaves it so.
5892aaeadf8SMatthew Dillon  *
5902aaeadf8SMatthew Dillon  *	If object is non-NULL, ref count must be bumped by caller
5912aaeadf8SMatthew Dillon  *	prior to making call to account for the new entry.
59230dcfc09SJohn Dyson  */
59330dcfc09SJohn Dyson int
594b9dcd593SBruce Evans vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
595b9dcd593SBruce Evans 	      vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max,
596b9dcd593SBruce Evans 	      int cow)
59730dcfc09SJohn Dyson {
598c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
599c0877f10SJohn Dyson 	vm_map_entry_t prev_entry;
60030dcfc09SJohn Dyson 	vm_map_entry_t temp_entry;
6019730a5daSPaul Saab 	vm_eflags_t protoeflags;
60230dcfc09SJohn Dyson 
6030cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
6040cddd8f0SMatthew Dillon 
60530dcfc09SJohn Dyson 	/*
60630dcfc09SJohn Dyson 	 * Check that the start and end points are not bogus.
60730dcfc09SJohn Dyson 	 */
60830dcfc09SJohn Dyson 
60930dcfc09SJohn Dyson 	if ((start < map->min_offset) || (end > map->max_offset) ||
61030dcfc09SJohn Dyson 	    (start >= end))
61130dcfc09SJohn Dyson 		return (KERN_INVALID_ADDRESS);
61230dcfc09SJohn Dyson 
61330dcfc09SJohn Dyson 	/*
61430dcfc09SJohn Dyson 	 * Find the entry prior to the proposed starting address; if it's part
61530dcfc09SJohn Dyson 	 * of an existing entry, this range is bogus.
61630dcfc09SJohn Dyson 	 */
61730dcfc09SJohn Dyson 
61830dcfc09SJohn Dyson 	if (vm_map_lookup_entry(map, start, &temp_entry))
61930dcfc09SJohn Dyson 		return (KERN_NO_SPACE);
62030dcfc09SJohn Dyson 
62130dcfc09SJohn Dyson 	prev_entry = temp_entry;
62230dcfc09SJohn Dyson 
62330dcfc09SJohn Dyson 	/*
62430dcfc09SJohn Dyson 	 * Assert that the next entry doesn't overlap the end point.
62530dcfc09SJohn Dyson 	 */
62630dcfc09SJohn Dyson 
62730dcfc09SJohn Dyson 	if ((prev_entry->next != &map->header) &&
62830dcfc09SJohn Dyson 	    (prev_entry->next->start < end))
62930dcfc09SJohn Dyson 		return (KERN_NO_SPACE);
63030dcfc09SJohn Dyson 
631afa07f7eSJohn Dyson 	protoeflags = 0;
632afa07f7eSJohn Dyson 
633afa07f7eSJohn Dyson 	if (cow & MAP_COPY_ON_WRITE)
634e5f13bddSAlan Cox 		protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
635afa07f7eSJohn Dyson 
6364e045f93SAlan Cox 	if (cow & MAP_NOFAULT) {
637afa07f7eSJohn Dyson 		protoeflags |= MAP_ENTRY_NOFAULT;
638afa07f7eSJohn Dyson 
6394e045f93SAlan Cox 		KASSERT(object == NULL,
6404e045f93SAlan Cox 			("vm_map_insert: paradoxical MAP_NOFAULT request"));
6414e045f93SAlan Cox 	}
6424f79d873SMatthew Dillon 	if (cow & MAP_DISABLE_SYNCER)
6434f79d873SMatthew Dillon 		protoeflags |= MAP_ENTRY_NOSYNC;
6449730a5daSPaul Saab 	if (cow & MAP_DISABLE_COREDUMP)
6459730a5daSPaul Saab 		protoeflags |= MAP_ENTRY_NOCOREDUMP;
6464f79d873SMatthew Dillon 
6472aaeadf8SMatthew Dillon 	if (object) {
64830dcfc09SJohn Dyson 		/*
6492aaeadf8SMatthew Dillon 		 * When object is non-NULL, it could be shared with another
6502aaeadf8SMatthew Dillon 		 * process.  We have to set or clear OBJ_ONEMAPPING
6512aaeadf8SMatthew Dillon 		 * appropriately.
65230dcfc09SJohn Dyson 		 */
6532aaeadf8SMatthew Dillon 		if ((object->ref_count > 1) || (object->shadow_count != 0)) {
6542aaeadf8SMatthew Dillon 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
6552aaeadf8SMatthew Dillon 		}
6564e045f93SAlan Cox 	}
6574e045f93SAlan Cox 	else if ((prev_entry != &map->header) &&
6584e045f93SAlan Cox 		 (prev_entry->eflags == protoeflags) &&
6598cc7e047SJohn Dyson 		 (prev_entry->end == start) &&
6604e045f93SAlan Cox 		 (prev_entry->wired_count == 0) &&
6614e045f93SAlan Cox 		 ((prev_entry->object.vm_object == NULL) ||
6628cc7e047SJohn Dyson 		  vm_object_coalesce(prev_entry->object.vm_object,
66330dcfc09SJohn Dyson 				     OFF_TO_IDX(prev_entry->offset),
6648cc7e047SJohn Dyson 				     (vm_size_t)(prev_entry->end - prev_entry->start),
665cdc2c291SJohn Dyson 				     (vm_size_t)(end - prev_entry->end)))) {
66630dcfc09SJohn Dyson 		/*
6672aaeadf8SMatthew Dillon 		 * We were able to extend the object.  Determine if we
6682aaeadf8SMatthew Dillon 		 * can extend the previous map entry to include the
6692aaeadf8SMatthew Dillon 		 * new range as well.
67030dcfc09SJohn Dyson 		 */
6718cc7e047SJohn Dyson 		if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
6728cc7e047SJohn Dyson 		    (prev_entry->protection == prot) &&
6738cc7e047SJohn Dyson 		    (prev_entry->max_protection == max)) {
67430dcfc09SJohn Dyson 			map->size += (end - prev_entry->end);
67530dcfc09SJohn Dyson 			prev_entry->end = end;
6764e71e795SMatthew Dillon 			vm_map_simplify_entry(map, prev_entry);
67730dcfc09SJohn Dyson 			return (KERN_SUCCESS);
67830dcfc09SJohn Dyson 		}
6798cc7e047SJohn Dyson 
6802aaeadf8SMatthew Dillon 		/*
6812aaeadf8SMatthew Dillon 		 * If we can extend the object but cannot extend the
6822aaeadf8SMatthew Dillon 		 * map entry, we have to create a new map entry.  We
6832aaeadf8SMatthew Dillon 		 * must bump the ref count on the extended object to
6844e71e795SMatthew Dillon 		 * account for it.  object may be NULL.
6852aaeadf8SMatthew Dillon 		 */
6862aaeadf8SMatthew Dillon 		object = prev_entry->object.vm_object;
6872aaeadf8SMatthew Dillon 		offset = prev_entry->offset +
6882aaeadf8SMatthew Dillon 			(prev_entry->end - prev_entry->start);
6898cc7e047SJohn Dyson 		vm_object_reference(object);
690b18bfc3dSJohn Dyson 	}
6912aaeadf8SMatthew Dillon 
6922aaeadf8SMatthew Dillon 	/*
6932aaeadf8SMatthew Dillon 	 * NOTE: if conditionals fail, object can be NULL here.  This occurs
6942aaeadf8SMatthew Dillon 	 * in things like the buffer map where we manage kva but do not manage
6952aaeadf8SMatthew Dillon 	 * backing objects.
6962aaeadf8SMatthew Dillon 	 */
6978cc7e047SJohn Dyson 
69830dcfc09SJohn Dyson 	/*
69930dcfc09SJohn Dyson 	 * Create a new entry
70030dcfc09SJohn Dyson 	 */
70130dcfc09SJohn Dyson 
70230dcfc09SJohn Dyson 	new_entry = vm_map_entry_create(map);
70330dcfc09SJohn Dyson 	new_entry->start = start;
70430dcfc09SJohn Dyson 	new_entry->end = end;
70530dcfc09SJohn Dyson 
706afa07f7eSJohn Dyson 	new_entry->eflags = protoeflags;
70730dcfc09SJohn Dyson 	new_entry->object.vm_object = object;
70830dcfc09SJohn Dyson 	new_entry->offset = offset;
7092267af78SJulian Elischer 	new_entry->avail_ssize = 0;
7102267af78SJulian Elischer 
71130dcfc09SJohn Dyson 	new_entry->inheritance = VM_INHERIT_DEFAULT;
71230dcfc09SJohn Dyson 	new_entry->protection = prot;
71330dcfc09SJohn Dyson 	new_entry->max_protection = max;
71430dcfc09SJohn Dyson 	new_entry->wired_count = 0;
715e5f251d2SAlan Cox 
71630dcfc09SJohn Dyson 	/*
71730dcfc09SJohn Dyson 	 * Insert the new entry into the list
71830dcfc09SJohn Dyson 	 */
71930dcfc09SJohn Dyson 
72030dcfc09SJohn Dyson 	vm_map_entry_link(map, prev_entry, new_entry);
72130dcfc09SJohn Dyson 	map->size += new_entry->end - new_entry->start;
72230dcfc09SJohn Dyson 
72330dcfc09SJohn Dyson 	/*
72430dcfc09SJohn Dyson 	 * Update the free space hint
72530dcfc09SJohn Dyson 	 */
72667bf6868SJohn Dyson 	if ((map->first_free == prev_entry) &&
7274f79d873SMatthew Dillon 	    (prev_entry->end >= new_entry->start)) {
72830dcfc09SJohn Dyson 		map->first_free = new_entry;
7294f79d873SMatthew Dillon 	}
73030dcfc09SJohn Dyson 
7311a484d28SMatthew Dillon #if 0
7321a484d28SMatthew Dillon 	/*
7331a484d28SMatthew Dillon 	 * Temporarily removed to avoid MAP_STACK panic, due to
7341a484d28SMatthew Dillon 	 * MAP_STACK being a huge hack.  Will be added back in
7351a484d28SMatthew Dillon 	 * when MAP_STACK (and the user stack mapping) is fixed.
7361a484d28SMatthew Dillon 	 */
7374e71e795SMatthew Dillon 	/*
7384e71e795SMatthew Dillon 	 * It may be possible to simplify the entry
7394e71e795SMatthew Dillon 	 */
7404e71e795SMatthew Dillon 	vm_map_simplify_entry(map, new_entry);
7411a484d28SMatthew Dillon #endif
7424e71e795SMatthew Dillon 
7434f79d873SMatthew Dillon 	if (cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) {
744e972780aSAlan Cox 		pmap_object_init_pt(map->pmap, start,
745e972780aSAlan Cox 				    object, OFF_TO_IDX(offset), end - start,
746e972780aSAlan Cox 				    cow & MAP_PREFAULT_PARTIAL);
7474f79d873SMatthew Dillon 	}
748e972780aSAlan Cox 
74930dcfc09SJohn Dyson 	return (KERN_SUCCESS);
75030dcfc09SJohn Dyson }
75130dcfc09SJohn Dyson 
75230dcfc09SJohn Dyson /*
753df8bae1dSRodney W. Grimes  * Find sufficient space for `length' bytes in the given map, starting at
754df8bae1dSRodney W. Grimes  * `start'.  The map must be locked.  Returns 0 on success, 1 on no space.
755df8bae1dSRodney W. Grimes  */
756df8bae1dSRodney W. Grimes int
7571b40f8c0SMatthew Dillon vm_map_findspace(
7581b40f8c0SMatthew Dillon 	vm_map_t map,
7591b40f8c0SMatthew Dillon 	vm_offset_t start,
7601b40f8c0SMatthew Dillon 	vm_size_t length,
7611b40f8c0SMatthew Dillon 	vm_offset_t *addr)
762df8bae1dSRodney W. Grimes {
763c0877f10SJohn Dyson 	vm_map_entry_t entry, next;
764c0877f10SJohn Dyson 	vm_offset_t end;
765df8bae1dSRodney W. Grimes 
7660cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
767df8bae1dSRodney W. Grimes 	if (start < map->min_offset)
768df8bae1dSRodney W. Grimes 		start = map->min_offset;
769df8bae1dSRodney W. Grimes 	if (start > map->max_offset)
770df8bae1dSRodney W. Grimes 		return (1);
771df8bae1dSRodney W. Grimes 
772df8bae1dSRodney W. Grimes 	/*
7730d94caffSDavid Greenman 	 * Look for the first possible address; if there's already something
7740d94caffSDavid Greenman 	 * at this address, we have to start after it.
775df8bae1dSRodney W. Grimes 	 */
776df8bae1dSRodney W. Grimes 	if (start == map->min_offset) {
77767bf6868SJohn Dyson 		if ((entry = map->first_free) != &map->header)
778df8bae1dSRodney W. Grimes 			start = entry->end;
779df8bae1dSRodney W. Grimes 	} else {
780df8bae1dSRodney W. Grimes 		vm_map_entry_t tmp;
7810d94caffSDavid Greenman 
782df8bae1dSRodney W. Grimes 		if (vm_map_lookup_entry(map, start, &tmp))
783df8bae1dSRodney W. Grimes 			start = tmp->end;
784df8bae1dSRodney W. Grimes 		entry = tmp;
785df8bae1dSRodney W. Grimes 	}
786df8bae1dSRodney W. Grimes 
787df8bae1dSRodney W. Grimes 	/*
7880d94caffSDavid Greenman 	 * Look through the rest of the map, trying to fit a new region in the
7890d94caffSDavid Greenman 	 * gap between existing regions, or after the very last region.
790df8bae1dSRodney W. Grimes 	 */
791df8bae1dSRodney W. Grimes 	for (;; start = (entry = next)->end) {
792df8bae1dSRodney W. Grimes 		/*
793df8bae1dSRodney W. Grimes 		 * Find the end of the proposed new region.  Be sure we didn't
794df8bae1dSRodney W. Grimes 		 * go beyond the end of the map, or wrap around the address;
795df8bae1dSRodney W. Grimes 		 * if so, we lose.  Otherwise, if this is the last entry, or
796df8bae1dSRodney W. Grimes 		 * if the proposed new region fits before the next entry, we
797df8bae1dSRodney W. Grimes 		 * win.
798df8bae1dSRodney W. Grimes 		 */
799df8bae1dSRodney W. Grimes 		end = start + length;
800df8bae1dSRodney W. Grimes 		if (end > map->max_offset || end < start)
801df8bae1dSRodney W. Grimes 			return (1);
802df8bae1dSRodney W. Grimes 		next = entry->next;
803df8bae1dSRodney W. Grimes 		if (next == &map->header || next->start >= end)
804df8bae1dSRodney W. Grimes 			break;
805df8bae1dSRodney W. Grimes 	}
806df8bae1dSRodney W. Grimes 	SAVE_HINT(map, entry);
807df8bae1dSRodney W. Grimes 	*addr = start;
80899448ed1SJohn Dyson 	if (map == kernel_map) {
80999448ed1SJohn Dyson 		vm_offset_t ksize;
81099448ed1SJohn Dyson 		if ((ksize = round_page(start + length)) > kernel_vm_end) {
81199448ed1SJohn Dyson 			pmap_growkernel(ksize);
81299448ed1SJohn Dyson 		}
81399448ed1SJohn Dyson 	}
814df8bae1dSRodney W. Grimes 	return (0);
815df8bae1dSRodney W. Grimes }
816df8bae1dSRodney W. Grimes 
817df8bae1dSRodney W. Grimes /*
818df8bae1dSRodney W. Grimes  *	vm_map_find finds an unallocated region in the target address
819df8bae1dSRodney W. Grimes  *	map with the given length.  The search is defined to be
820df8bae1dSRodney W. Grimes  *	first-fit from the specified address; the region found is
821df8bae1dSRodney W. Grimes  *	returned in the same parameter.
822df8bae1dSRodney W. Grimes  *
8232aaeadf8SMatthew Dillon  *	If object is non-NULL, ref count must be bumped by caller
8242aaeadf8SMatthew Dillon  *	prior to making call to account for the new entry.
825df8bae1dSRodney W. Grimes  */
826df8bae1dSRodney W. Grimes int
827b9dcd593SBruce Evans vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
828b9dcd593SBruce Evans 	    vm_offset_t *addr,	/* IN/OUT */
829b9dcd593SBruce Evans 	    vm_size_t length, boolean_t find_space, vm_prot_t prot,
830b9dcd593SBruce Evans 	    vm_prot_t max, int cow)
831df8bae1dSRodney W. Grimes {
832c0877f10SJohn Dyson 	vm_offset_t start;
8338d6e8edeSDavid Greenman 	int result, s = 0;
834df8bae1dSRodney W. Grimes 
8350cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
8360cddd8f0SMatthew Dillon 
837df8bae1dSRodney W. Grimes 	start = *addr;
8388d6e8edeSDavid Greenman 
83908442f8aSBosko Milekic 	if (map == kmem_map)
840b18bfc3dSJohn Dyson 		s = splvm();
8418d6e8edeSDavid Greenman 
842bea41bcfSDavid Greenman 	vm_map_lock(map);
843df8bae1dSRodney W. Grimes 	if (find_space) {
844df8bae1dSRodney W. Grimes 		if (vm_map_findspace(map, start, length, addr)) {
845df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
84608442f8aSBosko Milekic 			if (map == kmem_map)
8478d6e8edeSDavid Greenman 				splx(s);
848df8bae1dSRodney W. Grimes 			return (KERN_NO_SPACE);
849df8bae1dSRodney W. Grimes 		}
850df8bae1dSRodney W. Grimes 		start = *addr;
851df8bae1dSRodney W. Grimes 	}
852bd7e5f99SJohn Dyson 	result = vm_map_insert(map, object, offset,
853bd7e5f99SJohn Dyson 		start, start + length, prot, max, cow);
854df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
8558d6e8edeSDavid Greenman 
85608442f8aSBosko Milekic 	if (map == kmem_map)
8578d6e8edeSDavid Greenman 		splx(s);
8588d6e8edeSDavid Greenman 
859df8bae1dSRodney W. Grimes 	return (result);
860df8bae1dSRodney W. Grimes }
861df8bae1dSRodney W. Grimes 
862df8bae1dSRodney W. Grimes /*
863b7b2aac2SJohn Dyson  *	vm_map_simplify_entry:
86467bf6868SJohn Dyson  *
8654e71e795SMatthew Dillon  *	Simplify the given map entry by merging with either neighbor.  This
8664e71e795SMatthew Dillon  *	routine also has the ability to merge with both neighbors.
8674e71e795SMatthew Dillon  *
8684e71e795SMatthew Dillon  *	The map must be locked.
8694e71e795SMatthew Dillon  *
8704e71e795SMatthew Dillon  *	This routine guarentees that the passed entry remains valid (though
8714e71e795SMatthew Dillon  *	possibly extended).  When merging, this routine may delete one or
8724e71e795SMatthew Dillon  *	both neighbors.
873df8bae1dSRodney W. Grimes  */
874b7b2aac2SJohn Dyson void
8751b40f8c0SMatthew Dillon vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry)
876df8bae1dSRodney W. Grimes {
877308c24baSJohn Dyson 	vm_map_entry_t next, prev;
878b7b2aac2SJohn Dyson 	vm_size_t prevsize, esize;
879df8bae1dSRodney W. Grimes 
8800cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
8810cddd8f0SMatthew Dillon 
8829fdfe602SMatthew Dillon 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
883df8bae1dSRodney W. Grimes 		return;
884308c24baSJohn Dyson 
885308c24baSJohn Dyson 	prev = entry->prev;
886308c24baSJohn Dyson 	if (prev != &map->header) {
88767bf6868SJohn Dyson 		prevsize = prev->end - prev->start;
88867bf6868SJohn Dyson 		if ( (prev->end == entry->start) &&
88967bf6868SJohn Dyson 		     (prev->object.vm_object == entry->object.vm_object) &&
89095e5e988SJohn Dyson 		     (!prev->object.vm_object ||
89167bf6868SJohn Dyson 			(prev->offset + prevsize == entry->offset)) &&
892afa07f7eSJohn Dyson 		     (prev->eflags == entry->eflags) &&
89367bf6868SJohn Dyson 		     (prev->protection == entry->protection) &&
89467bf6868SJohn Dyson 		     (prev->max_protection == entry->max_protection) &&
89567bf6868SJohn Dyson 		     (prev->inheritance == entry->inheritance) &&
896b7b2aac2SJohn Dyson 		     (prev->wired_count == entry->wired_count)) {
897308c24baSJohn Dyson 			if (map->first_free == prev)
898308c24baSJohn Dyson 				map->first_free = entry;
899b18bfc3dSJohn Dyson 			if (map->hint == prev)
900b18bfc3dSJohn Dyson 				map->hint = entry;
901308c24baSJohn Dyson 			vm_map_entry_unlink(map, prev);
902308c24baSJohn Dyson 			entry->start = prev->start;
903308c24baSJohn Dyson 			entry->offset = prev->offset;
904b18bfc3dSJohn Dyson 			if (prev->object.vm_object)
905308c24baSJohn Dyson 				vm_object_deallocate(prev->object.vm_object);
906308c24baSJohn Dyson 			vm_map_entry_dispose(map, prev);
907308c24baSJohn Dyson 		}
908308c24baSJohn Dyson 	}
909de5f6a77SJohn Dyson 
910de5f6a77SJohn Dyson 	next = entry->next;
911308c24baSJohn Dyson 	if (next != &map->header) {
91267bf6868SJohn Dyson 		esize = entry->end - entry->start;
91367bf6868SJohn Dyson 		if ((entry->end == next->start) &&
91467bf6868SJohn Dyson 		    (next->object.vm_object == entry->object.vm_object) &&
91567bf6868SJohn Dyson 		     (!entry->object.vm_object ||
91667bf6868SJohn Dyson 			(entry->offset + esize == next->offset)) &&
917afa07f7eSJohn Dyson 		    (next->eflags == entry->eflags) &&
91867bf6868SJohn Dyson 		    (next->protection == entry->protection) &&
91967bf6868SJohn Dyson 		    (next->max_protection == entry->max_protection) &&
92067bf6868SJohn Dyson 		    (next->inheritance == entry->inheritance) &&
921b7b2aac2SJohn Dyson 		    (next->wired_count == entry->wired_count)) {
922308c24baSJohn Dyson 			if (map->first_free == next)
923308c24baSJohn Dyson 				map->first_free = entry;
924b18bfc3dSJohn Dyson 			if (map->hint == next)
925b18bfc3dSJohn Dyson 				map->hint = entry;
926de5f6a77SJohn Dyson 			vm_map_entry_unlink(map, next);
927de5f6a77SJohn Dyson 			entry->end = next->end;
928b18bfc3dSJohn Dyson 			if (next->object.vm_object)
929de5f6a77SJohn Dyson 				vm_object_deallocate(next->object.vm_object);
930de5f6a77SJohn Dyson 			vm_map_entry_dispose(map, next);
931df8bae1dSRodney W. Grimes 	        }
932df8bae1dSRodney W. Grimes 	}
933de5f6a77SJohn Dyson }
934df8bae1dSRodney W. Grimes /*
935df8bae1dSRodney W. Grimes  *	vm_map_clip_start:	[ internal use only ]
936df8bae1dSRodney W. Grimes  *
937df8bae1dSRodney W. Grimes  *	Asserts that the given entry begins at or after
938df8bae1dSRodney W. Grimes  *	the specified address; if necessary,
939df8bae1dSRodney W. Grimes  *	it splits the entry into two.
940df8bae1dSRodney W. Grimes  */
941df8bae1dSRodney W. Grimes #define vm_map_clip_start(map, entry, startaddr) \
942df8bae1dSRodney W. Grimes { \
943df8bae1dSRodney W. Grimes 	if (startaddr > entry->start) \
944df8bae1dSRodney W. Grimes 		_vm_map_clip_start(map, entry, startaddr); \
945df8bae1dSRodney W. Grimes }
946df8bae1dSRodney W. Grimes 
947df8bae1dSRodney W. Grimes /*
948df8bae1dSRodney W. Grimes  *	This routine is called only when it is known that
949df8bae1dSRodney W. Grimes  *	the entry must be split.
950df8bae1dSRodney W. Grimes  */
9510d94caffSDavid Greenman static void
9521b40f8c0SMatthew Dillon _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start)
953df8bae1dSRodney W. Grimes {
954c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
955df8bae1dSRodney W. Grimes 
956df8bae1dSRodney W. Grimes 	/*
9570d94caffSDavid Greenman 	 * Split off the front portion -- note that we must insert the new
9580d94caffSDavid Greenman 	 * entry BEFORE this one, so that this entry has the specified
9590d94caffSDavid Greenman 	 * starting address.
960df8bae1dSRodney W. Grimes 	 */
961df8bae1dSRodney W. Grimes 
962f32dbbeeSJohn Dyson 	vm_map_simplify_entry(map, entry);
963f32dbbeeSJohn Dyson 
96411cccda1SJohn Dyson 	/*
96511cccda1SJohn Dyson 	 * If there is no object backing this entry, we might as well create
96611cccda1SJohn Dyson 	 * one now.  If we defer it, an object can get created after the map
96711cccda1SJohn Dyson 	 * is clipped, and individual objects will be created for the split-up
96811cccda1SJohn Dyson 	 * map.  This is a bit of a hack, but is also about the best place to
96911cccda1SJohn Dyson 	 * put this improvement.
97011cccda1SJohn Dyson 	 */
97111cccda1SJohn Dyson 
9724e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL && !map->system_map) {
97311cccda1SJohn Dyson 		vm_object_t object;
97411cccda1SJohn Dyson 		object = vm_object_allocate(OBJT_DEFAULT,
975c2e11a03SJohn Dyson 				atop(entry->end - entry->start));
97611cccda1SJohn Dyson 		entry->object.vm_object = object;
97711cccda1SJohn Dyson 		entry->offset = 0;
97811cccda1SJohn Dyson 	}
97911cccda1SJohn Dyson 
980df8bae1dSRodney W. Grimes 	new_entry = vm_map_entry_create(map);
981df8bae1dSRodney W. Grimes 	*new_entry = *entry;
982df8bae1dSRodney W. Grimes 
983df8bae1dSRodney W. Grimes 	new_entry->end = start;
984df8bae1dSRodney W. Grimes 	entry->offset += (start - entry->start);
985df8bae1dSRodney W. Grimes 	entry->start = start;
986df8bae1dSRodney W. Grimes 
987df8bae1dSRodney W. Grimes 	vm_map_entry_link(map, entry->prev, new_entry);
988df8bae1dSRodney W. Grimes 
9899fdfe602SMatthew Dillon 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
990df8bae1dSRodney W. Grimes 		vm_object_reference(new_entry->object.vm_object);
991df8bae1dSRodney W. Grimes 	}
992c0877f10SJohn Dyson }
993df8bae1dSRodney W. Grimes 
994df8bae1dSRodney W. Grimes /*
995df8bae1dSRodney W. Grimes  *	vm_map_clip_end:	[ internal use only ]
996df8bae1dSRodney W. Grimes  *
997df8bae1dSRodney W. Grimes  *	Asserts that the given entry ends at or before
998df8bae1dSRodney W. Grimes  *	the specified address; if necessary,
999df8bae1dSRodney W. Grimes  *	it splits the entry into two.
1000df8bae1dSRodney W. Grimes  */
1001df8bae1dSRodney W. Grimes 
1002df8bae1dSRodney W. Grimes #define vm_map_clip_end(map, entry, endaddr) \
1003df8bae1dSRodney W. Grimes { \
1004df8bae1dSRodney W. Grimes 	if (endaddr < entry->end) \
1005df8bae1dSRodney W. Grimes 		_vm_map_clip_end(map, entry, endaddr); \
1006df8bae1dSRodney W. Grimes }
1007df8bae1dSRodney W. Grimes 
1008df8bae1dSRodney W. Grimes /*
1009df8bae1dSRodney W. Grimes  *	This routine is called only when it is known that
1010df8bae1dSRodney W. Grimes  *	the entry must be split.
1011df8bae1dSRodney W. Grimes  */
10120d94caffSDavid Greenman static void
10131b40f8c0SMatthew Dillon _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end)
1014df8bae1dSRodney W. Grimes {
1015c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
1016df8bae1dSRodney W. Grimes 
1017df8bae1dSRodney W. Grimes 	/*
101811cccda1SJohn Dyson 	 * If there is no object backing this entry, we might as well create
101911cccda1SJohn Dyson 	 * one now.  If we defer it, an object can get created after the map
102011cccda1SJohn Dyson 	 * is clipped, and individual objects will be created for the split-up
102111cccda1SJohn Dyson 	 * map.  This is a bit of a hack, but is also about the best place to
102211cccda1SJohn Dyson 	 * put this improvement.
102311cccda1SJohn Dyson 	 */
102411cccda1SJohn Dyson 
10254e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL && !map->system_map) {
102611cccda1SJohn Dyson 		vm_object_t object;
102711cccda1SJohn Dyson 		object = vm_object_allocate(OBJT_DEFAULT,
1028c2e11a03SJohn Dyson 				atop(entry->end - entry->start));
102911cccda1SJohn Dyson 		entry->object.vm_object = object;
103011cccda1SJohn Dyson 		entry->offset = 0;
103111cccda1SJohn Dyson 	}
103211cccda1SJohn Dyson 
103311cccda1SJohn Dyson 	/*
10340d94caffSDavid Greenman 	 * Create a new entry and insert it AFTER the specified entry
1035df8bae1dSRodney W. Grimes 	 */
1036df8bae1dSRodney W. Grimes 
1037df8bae1dSRodney W. Grimes 	new_entry = vm_map_entry_create(map);
1038df8bae1dSRodney W. Grimes 	*new_entry = *entry;
1039df8bae1dSRodney W. Grimes 
1040df8bae1dSRodney W. Grimes 	new_entry->start = entry->end = end;
1041df8bae1dSRodney W. Grimes 	new_entry->offset += (end - entry->start);
1042df8bae1dSRodney W. Grimes 
1043df8bae1dSRodney W. Grimes 	vm_map_entry_link(map, entry, new_entry);
1044df8bae1dSRodney W. Grimes 
10459fdfe602SMatthew Dillon 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1046df8bae1dSRodney W. Grimes 		vm_object_reference(new_entry->object.vm_object);
1047df8bae1dSRodney W. Grimes 	}
1048c0877f10SJohn Dyson }
1049df8bae1dSRodney W. Grimes 
1050df8bae1dSRodney W. Grimes /*
1051df8bae1dSRodney W. Grimes  *	VM_MAP_RANGE_CHECK:	[ internal use only ]
1052df8bae1dSRodney W. Grimes  *
1053df8bae1dSRodney W. Grimes  *	Asserts that the starting and ending region
1054df8bae1dSRodney W. Grimes  *	addresses fall within the valid range of the map.
1055df8bae1dSRodney W. Grimes  */
1056df8bae1dSRodney W. Grimes #define	VM_MAP_RANGE_CHECK(map, start, end)		\
1057df8bae1dSRodney W. Grimes 		{					\
1058df8bae1dSRodney W. Grimes 		if (start < vm_map_min(map))		\
1059df8bae1dSRodney W. Grimes 			start = vm_map_min(map);	\
1060df8bae1dSRodney W. Grimes 		if (end > vm_map_max(map))		\
1061df8bae1dSRodney W. Grimes 			end = vm_map_max(map);		\
1062df8bae1dSRodney W. Grimes 		if (start > end)			\
1063df8bae1dSRodney W. Grimes 			start = end;			\
1064df8bae1dSRodney W. Grimes 		}
1065df8bae1dSRodney W. Grimes 
1066df8bae1dSRodney W. Grimes /*
1067df8bae1dSRodney W. Grimes  *	vm_map_submap:		[ kernel use only ]
1068df8bae1dSRodney W. Grimes  *
1069df8bae1dSRodney W. Grimes  *	Mark the given range as handled by a subordinate map.
1070df8bae1dSRodney W. Grimes  *
1071df8bae1dSRodney W. Grimes  *	This range must have been created with vm_map_find,
1072df8bae1dSRodney W. Grimes  *	and no other operations may have been performed on this
1073df8bae1dSRodney W. Grimes  *	range prior to calling vm_map_submap.
1074df8bae1dSRodney W. Grimes  *
1075df8bae1dSRodney W. Grimes  *	Only a limited number of operations can be performed
1076df8bae1dSRodney W. Grimes  *	within this rage after calling vm_map_submap:
1077df8bae1dSRodney W. Grimes  *		vm_fault
1078df8bae1dSRodney W. Grimes  *	[Don't try vm_map_copy!]
1079df8bae1dSRodney W. Grimes  *
1080df8bae1dSRodney W. Grimes  *	To remove a submapping, one must first remove the
1081df8bae1dSRodney W. Grimes  *	range from the superior map, and then destroy the
1082df8bae1dSRodney W. Grimes  *	submap (if desired).  [Better yet, don't try it.]
1083df8bae1dSRodney W. Grimes  */
1084df8bae1dSRodney W. Grimes int
10851b40f8c0SMatthew Dillon vm_map_submap(
10861b40f8c0SMatthew Dillon 	vm_map_t map,
10871b40f8c0SMatthew Dillon 	vm_offset_t start,
10881b40f8c0SMatthew Dillon 	vm_offset_t end,
10891b40f8c0SMatthew Dillon 	vm_map_t submap)
1090df8bae1dSRodney W. Grimes {
1091df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
1092c0877f10SJohn Dyson 	int result = KERN_INVALID_ARGUMENT;
1093df8bae1dSRodney W. Grimes 
10940cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
10950cddd8f0SMatthew Dillon 
1096df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1097df8bae1dSRodney W. Grimes 
1098df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1099df8bae1dSRodney W. Grimes 
1100df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &entry)) {
1101df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
11020d94caffSDavid Greenman 	} else
1103df8bae1dSRodney W. Grimes 		entry = entry->next;
1104df8bae1dSRodney W. Grimes 
1105df8bae1dSRodney W. Grimes 	vm_map_clip_end(map, entry, end);
1106df8bae1dSRodney W. Grimes 
1107df8bae1dSRodney W. Grimes 	if ((entry->start == start) && (entry->end == end) &&
11089fdfe602SMatthew Dillon 	    ((entry->eflags & MAP_ENTRY_COW) == 0) &&
1109afa07f7eSJohn Dyson 	    (entry->object.vm_object == NULL)) {
11102d8acc0fSJohn Dyson 		entry->object.sub_map = submap;
1111afa07f7eSJohn Dyson 		entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
1112df8bae1dSRodney W. Grimes 		result = KERN_SUCCESS;
1113df8bae1dSRodney W. Grimes 	}
1114df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1115df8bae1dSRodney W. Grimes 
1116df8bae1dSRodney W. Grimes 	return (result);
1117df8bae1dSRodney W. Grimes }
1118df8bae1dSRodney W. Grimes 
1119df8bae1dSRodney W. Grimes /*
1120df8bae1dSRodney W. Grimes  *	vm_map_protect:
1121df8bae1dSRodney W. Grimes  *
1122df8bae1dSRodney W. Grimes  *	Sets the protection of the specified address
1123df8bae1dSRodney W. Grimes  *	region in the target map.  If "set_max" is
1124df8bae1dSRodney W. Grimes  *	specified, the maximum protection is to be set;
1125df8bae1dSRodney W. Grimes  *	otherwise, only the current protection is affected.
1126df8bae1dSRodney W. Grimes  */
1127df8bae1dSRodney W. Grimes int
1128b9dcd593SBruce Evans vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
1129b9dcd593SBruce Evans 	       vm_prot_t new_prot, boolean_t set_max)
1130df8bae1dSRodney W. Grimes {
1131c0877f10SJohn Dyson 	vm_map_entry_t current;
1132df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
1133df8bae1dSRodney W. Grimes 
11340cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
1135df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1136df8bae1dSRodney W. Grimes 
1137df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1138df8bae1dSRodney W. Grimes 
1139df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &entry)) {
1140df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
1141b7b2aac2SJohn Dyson 	} else {
1142df8bae1dSRodney W. Grimes 		entry = entry->next;
1143b7b2aac2SJohn Dyson 	}
1144df8bae1dSRodney W. Grimes 
1145df8bae1dSRodney W. Grimes 	/*
11460d94caffSDavid Greenman 	 * Make a first pass to check for protection violations.
1147df8bae1dSRodney W. Grimes 	 */
1148df8bae1dSRodney W. Grimes 
1149df8bae1dSRodney W. Grimes 	current = entry;
1150df8bae1dSRodney W. Grimes 	while ((current != &map->header) && (current->start < end)) {
1151afa07f7eSJohn Dyson 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1152a1f6d91cSDavid Greenman 			vm_map_unlock(map);
1153df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ARGUMENT);
1154a1f6d91cSDavid Greenman 		}
1155df8bae1dSRodney W. Grimes 		if ((new_prot & current->max_protection) != new_prot) {
1156df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
1157df8bae1dSRodney W. Grimes 			return (KERN_PROTECTION_FAILURE);
1158df8bae1dSRodney W. Grimes 		}
1159df8bae1dSRodney W. Grimes 		current = current->next;
1160df8bae1dSRodney W. Grimes 	}
1161df8bae1dSRodney W. Grimes 
1162df8bae1dSRodney W. Grimes 	/*
11630d94caffSDavid Greenman 	 * Go back and fix up protections. [Note that clipping is not
11640d94caffSDavid Greenman 	 * necessary the second time.]
1165df8bae1dSRodney W. Grimes 	 */
1166df8bae1dSRodney W. Grimes 
1167df8bae1dSRodney W. Grimes 	current = entry;
1168df8bae1dSRodney W. Grimes 
1169df8bae1dSRodney W. Grimes 	while ((current != &map->header) && (current->start < end)) {
1170df8bae1dSRodney W. Grimes 		vm_prot_t old_prot;
1171df8bae1dSRodney W. Grimes 
1172df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, current, end);
1173df8bae1dSRodney W. Grimes 
1174df8bae1dSRodney W. Grimes 		old_prot = current->protection;
1175df8bae1dSRodney W. Grimes 		if (set_max)
1176df8bae1dSRodney W. Grimes 			current->protection =
1177df8bae1dSRodney W. Grimes 			    (current->max_protection = new_prot) &
1178df8bae1dSRodney W. Grimes 			    old_prot;
1179df8bae1dSRodney W. Grimes 		else
1180df8bae1dSRodney W. Grimes 			current->protection = new_prot;
1181df8bae1dSRodney W. Grimes 
1182df8bae1dSRodney W. Grimes 		/*
11830d94caffSDavid Greenman 		 * Update physical map if necessary. Worry about copy-on-write
11840d94caffSDavid Greenman 		 * here -- CHECK THIS XXX
1185df8bae1dSRodney W. Grimes 		 */
1186df8bae1dSRodney W. Grimes 
1187df8bae1dSRodney W. Grimes 		if (current->protection != old_prot) {
1188afa07f7eSJohn Dyson #define MASK(entry)	(((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1189df8bae1dSRodney W. Grimes 							VM_PROT_ALL)
1190df8bae1dSRodney W. Grimes 
1191df8bae1dSRodney W. Grimes 			pmap_protect(map->pmap, current->start,
1192df8bae1dSRodney W. Grimes 			    current->end,
11931c85e3dfSAlan Cox 			    current->protection & MASK(current));
1194df8bae1dSRodney W. Grimes #undef	MASK
1195df8bae1dSRodney W. Grimes 		}
11967d78abc9SJohn Dyson 
11977d78abc9SJohn Dyson 		vm_map_simplify_entry(map, current);
11987d78abc9SJohn Dyson 
1199df8bae1dSRodney W. Grimes 		current = current->next;
1200df8bae1dSRodney W. Grimes 	}
1201df8bae1dSRodney W. Grimes 
1202df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1203df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
1204df8bae1dSRodney W. Grimes }
1205df8bae1dSRodney W. Grimes 
1206df8bae1dSRodney W. Grimes /*
1207867a482dSJohn Dyson  *	vm_map_madvise:
1208867a482dSJohn Dyson  *
1209867a482dSJohn Dyson  * 	This routine traverses a processes map handling the madvise
1210f7fc307aSAlan Cox  *	system call.  Advisories are classified as either those effecting
1211f7fc307aSAlan Cox  *	the vm_map_entry structure, or those effecting the underlying
1212f7fc307aSAlan Cox  *	objects.
1213867a482dSJohn Dyson  */
1214b4309055SMatthew Dillon 
1215b4309055SMatthew Dillon int
12161b40f8c0SMatthew Dillon vm_map_madvise(
12171b40f8c0SMatthew Dillon 	vm_map_t map,
12181b40f8c0SMatthew Dillon 	vm_offset_t start,
12191b40f8c0SMatthew Dillon 	vm_offset_t end,
12201b40f8c0SMatthew Dillon 	int behav)
1221867a482dSJohn Dyson {
1222f7fc307aSAlan Cox 	vm_map_entry_t current, entry;
1223b4309055SMatthew Dillon 	int modify_map = 0;
1224867a482dSJohn Dyson 
12250cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
12260cddd8f0SMatthew Dillon 
1227b4309055SMatthew Dillon 	/*
1228b4309055SMatthew Dillon 	 * Some madvise calls directly modify the vm_map_entry, in which case
1229b4309055SMatthew Dillon 	 * we need to use an exclusive lock on the map and we need to perform
1230b4309055SMatthew Dillon 	 * various clipping operations.  Otherwise we only need a read-lock
1231b4309055SMatthew Dillon 	 * on the map.
1232b4309055SMatthew Dillon 	 */
1233f7fc307aSAlan Cox 
1234b4309055SMatthew Dillon 	switch(behav) {
1235b4309055SMatthew Dillon 	case MADV_NORMAL:
1236b4309055SMatthew Dillon 	case MADV_SEQUENTIAL:
1237b4309055SMatthew Dillon 	case MADV_RANDOM:
12384f79d873SMatthew Dillon 	case MADV_NOSYNC:
12394f79d873SMatthew Dillon 	case MADV_AUTOSYNC:
12409730a5daSPaul Saab 	case MADV_NOCORE:
12419730a5daSPaul Saab 	case MADV_CORE:
1242b4309055SMatthew Dillon 		modify_map = 1;
1243867a482dSJohn Dyson 		vm_map_lock(map);
1244b4309055SMatthew Dillon 		break;
1245b4309055SMatthew Dillon 	case MADV_WILLNEED:
1246b4309055SMatthew Dillon 	case MADV_DONTNEED:
1247b4309055SMatthew Dillon 	case MADV_FREE:
1248f7fc307aSAlan Cox 		vm_map_lock_read(map);
1249b4309055SMatthew Dillon 		break;
1250b4309055SMatthew Dillon 	default:
1251b4309055SMatthew Dillon 		return (KERN_INVALID_ARGUMENT);
1252b4309055SMatthew Dillon 	}
1253b4309055SMatthew Dillon 
1254b4309055SMatthew Dillon 	/*
1255b4309055SMatthew Dillon 	 * Locate starting entry and clip if necessary.
1256b4309055SMatthew Dillon 	 */
1257867a482dSJohn Dyson 
1258867a482dSJohn Dyson 	VM_MAP_RANGE_CHECK(map, start, end);
1259867a482dSJohn Dyson 
1260867a482dSJohn Dyson 	if (vm_map_lookup_entry(map, start, &entry)) {
1261f7fc307aSAlan Cox 		if (modify_map)
1262867a482dSJohn Dyson 			vm_map_clip_start(map, entry, start);
1263b4309055SMatthew Dillon 	} else {
1264867a482dSJohn Dyson 		entry = entry->next;
1265b4309055SMatthew Dillon 	}
1266867a482dSJohn Dyson 
1267f7fc307aSAlan Cox 	if (modify_map) {
1268f7fc307aSAlan Cox 		/*
1269f7fc307aSAlan Cox 		 * madvise behaviors that are implemented in the vm_map_entry.
1270f7fc307aSAlan Cox 		 *
1271f7fc307aSAlan Cox 		 * We clip the vm_map_entry so that behavioral changes are
1272f7fc307aSAlan Cox 		 * limited to the specified address range.
1273f7fc307aSAlan Cox 		 */
1274867a482dSJohn Dyson 		for (current = entry;
1275867a482dSJohn Dyson 		     (current != &map->header) && (current->start < end);
1276b4309055SMatthew Dillon 		     current = current->next
1277b4309055SMatthew Dillon 		) {
1278f7fc307aSAlan Cox 			if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1279867a482dSJohn Dyson 				continue;
1280fed9a903SJohn Dyson 
128147221757SJohn Dyson 			vm_map_clip_end(map, current, end);
1282fed9a903SJohn Dyson 
1283f7fc307aSAlan Cox 			switch (behav) {
1284867a482dSJohn Dyson 			case MADV_NORMAL:
12857f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
1286867a482dSJohn Dyson 				break;
1287867a482dSJohn Dyson 			case MADV_SEQUENTIAL:
12887f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
1289867a482dSJohn Dyson 				break;
1290867a482dSJohn Dyson 			case MADV_RANDOM:
12917f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
1292867a482dSJohn Dyson 				break;
12934f79d873SMatthew Dillon 			case MADV_NOSYNC:
12944f79d873SMatthew Dillon 				current->eflags |= MAP_ENTRY_NOSYNC;
12954f79d873SMatthew Dillon 				break;
12964f79d873SMatthew Dillon 			case MADV_AUTOSYNC:
12974f79d873SMatthew Dillon 				current->eflags &= ~MAP_ENTRY_NOSYNC;
12984f79d873SMatthew Dillon 				break;
12999730a5daSPaul Saab 			case MADV_NOCORE:
13009730a5daSPaul Saab 				current->eflags |= MAP_ENTRY_NOCOREDUMP;
13019730a5daSPaul Saab 				break;
13029730a5daSPaul Saab 			case MADV_CORE:
13039730a5daSPaul Saab 				current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
13049730a5daSPaul Saab 				break;
1305867a482dSJohn Dyson 			default:
1306867a482dSJohn Dyson 				break;
1307867a482dSJohn Dyson 			}
1308f7fc307aSAlan Cox 			vm_map_simplify_entry(map, current);
1309867a482dSJohn Dyson 		}
1310867a482dSJohn Dyson 		vm_map_unlock(map);
1311b4309055SMatthew Dillon 	} else {
1312f7fc307aSAlan Cox 		vm_pindex_t pindex;
1313f7fc307aSAlan Cox 		int count;
1314f7fc307aSAlan Cox 
1315f7fc307aSAlan Cox 		/*
1316f7fc307aSAlan Cox 		 * madvise behaviors that are implemented in the underlying
1317f7fc307aSAlan Cox 		 * vm_object.
1318f7fc307aSAlan Cox 		 *
1319f7fc307aSAlan Cox 		 * Since we don't clip the vm_map_entry, we have to clip
1320f7fc307aSAlan Cox 		 * the vm_object pindex and count.
1321f7fc307aSAlan Cox 		 */
1322f7fc307aSAlan Cox 		for (current = entry;
1323f7fc307aSAlan Cox 		     (current != &map->header) && (current->start < end);
1324b4309055SMatthew Dillon 		     current = current->next
1325b4309055SMatthew Dillon 		) {
13265f99b57cSMatthew Dillon 			vm_offset_t useStart;
13275f99b57cSMatthew Dillon 
1328f7fc307aSAlan Cox 			if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1329f7fc307aSAlan Cox 				continue;
1330f7fc307aSAlan Cox 
1331f7fc307aSAlan Cox 			pindex = OFF_TO_IDX(current->offset);
1332f7fc307aSAlan Cox 			count = atop(current->end - current->start);
13335f99b57cSMatthew Dillon 			useStart = current->start;
1334f7fc307aSAlan Cox 
1335f7fc307aSAlan Cox 			if (current->start < start) {
1336f7fc307aSAlan Cox 				pindex += atop(start - current->start);
1337f7fc307aSAlan Cox 				count -= atop(start - current->start);
13385f99b57cSMatthew Dillon 				useStart = start;
1339f7fc307aSAlan Cox 			}
1340f7fc307aSAlan Cox 			if (current->end > end)
1341f7fc307aSAlan Cox 				count -= atop(current->end - end);
1342f7fc307aSAlan Cox 
1343f7fc307aSAlan Cox 			if (count <= 0)
1344f7fc307aSAlan Cox 				continue;
1345f7fc307aSAlan Cox 
1346f7fc307aSAlan Cox 			vm_object_madvise(current->object.vm_object,
1347f7fc307aSAlan Cox 					  pindex, count, behav);
1348b4309055SMatthew Dillon 			if (behav == MADV_WILLNEED) {
1349b4309055SMatthew Dillon 				pmap_object_init_pt(
1350b4309055SMatthew Dillon 				    map->pmap,
13515f99b57cSMatthew Dillon 				    useStart,
1352f7fc307aSAlan Cox 				    current->object.vm_object,
1353b4309055SMatthew Dillon 				    pindex,
1354b4309055SMatthew Dillon 				    (count << PAGE_SHIFT),
1355e3026983SMatthew Dillon 				    MAP_PREFAULT_MADVISE
1356b4309055SMatthew Dillon 				);
1357f7fc307aSAlan Cox 			}
1358f7fc307aSAlan Cox 		}
1359f7fc307aSAlan Cox 		vm_map_unlock_read(map);
1360f7fc307aSAlan Cox 	}
1361b4309055SMatthew Dillon 	return(0);
1362867a482dSJohn Dyson }
1363867a482dSJohn Dyson 
1364867a482dSJohn Dyson 
1365867a482dSJohn Dyson /*
1366df8bae1dSRodney W. Grimes  *	vm_map_inherit:
1367df8bae1dSRodney W. Grimes  *
1368df8bae1dSRodney W. Grimes  *	Sets the inheritance of the specified address
1369df8bae1dSRodney W. Grimes  *	range in the target map.  Inheritance
1370df8bae1dSRodney W. Grimes  *	affects how the map will be shared with
1371df8bae1dSRodney W. Grimes  *	child maps at the time of vm_map_fork.
1372df8bae1dSRodney W. Grimes  */
1373df8bae1dSRodney W. Grimes int
1374b9dcd593SBruce Evans vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
1375b9dcd593SBruce Evans 	       vm_inherit_t new_inheritance)
1376df8bae1dSRodney W. Grimes {
1377c0877f10SJohn Dyson 	vm_map_entry_t entry;
1378df8bae1dSRodney W. Grimes 	vm_map_entry_t temp_entry;
1379df8bae1dSRodney W. Grimes 
13800cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
13810cddd8f0SMatthew Dillon 
1382df8bae1dSRodney W. Grimes 	switch (new_inheritance) {
1383df8bae1dSRodney W. Grimes 	case VM_INHERIT_NONE:
1384df8bae1dSRodney W. Grimes 	case VM_INHERIT_COPY:
1385df8bae1dSRodney W. Grimes 	case VM_INHERIT_SHARE:
1386df8bae1dSRodney W. Grimes 		break;
1387df8bae1dSRodney W. Grimes 	default:
1388df8bae1dSRodney W. Grimes 		return (KERN_INVALID_ARGUMENT);
1389df8bae1dSRodney W. Grimes 	}
1390df8bae1dSRodney W. Grimes 
1391df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1392df8bae1dSRodney W. Grimes 
1393df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1394df8bae1dSRodney W. Grimes 
1395df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &temp_entry)) {
1396df8bae1dSRodney W. Grimes 		entry = temp_entry;
1397df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
13980d94caffSDavid Greenman 	} else
1399df8bae1dSRodney W. Grimes 		entry = temp_entry->next;
1400df8bae1dSRodney W. Grimes 
1401df8bae1dSRodney W. Grimes 	while ((entry != &map->header) && (entry->start < end)) {
1402df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, entry, end);
1403df8bae1dSRodney W. Grimes 
1404df8bae1dSRodney W. Grimes 		entry->inheritance = new_inheritance;
1405df8bae1dSRodney W. Grimes 
140644428f62SAlan Cox 		vm_map_simplify_entry(map, entry);
140744428f62SAlan Cox 
1408df8bae1dSRodney W. Grimes 		entry = entry->next;
1409df8bae1dSRodney W. Grimes 	}
1410df8bae1dSRodney W. Grimes 
1411df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1412df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
1413df8bae1dSRodney W. Grimes }
1414df8bae1dSRodney W. Grimes 
1415df8bae1dSRodney W. Grimes /*
14167aaaa4fdSJohn Dyson  * Implement the semantics of mlock
14177aaaa4fdSJohn Dyson  */
14187aaaa4fdSJohn Dyson int
14191b40f8c0SMatthew Dillon vm_map_user_pageable(
14201b40f8c0SMatthew Dillon 	vm_map_t map,
14211b40f8c0SMatthew Dillon 	vm_offset_t start,
14221b40f8c0SMatthew Dillon 	vm_offset_t end,
14231b40f8c0SMatthew Dillon 	boolean_t new_pageable)
14247aaaa4fdSJohn Dyson {
1425b44959ceSTor Egge 	vm_map_entry_t entry;
14267aaaa4fdSJohn Dyson 	vm_map_entry_t start_entry;
1427b44959ceSTor Egge 	vm_offset_t estart;
1428e7673b84STor Egge 	vm_offset_t eend;
14297aaaa4fdSJohn Dyson 	int rv;
14307aaaa4fdSJohn Dyson 
14317aaaa4fdSJohn Dyson 	vm_map_lock(map);
14327aaaa4fdSJohn Dyson 	VM_MAP_RANGE_CHECK(map, start, end);
14337aaaa4fdSJohn Dyson 
14347aaaa4fdSJohn Dyson 	if (vm_map_lookup_entry(map, start, &start_entry) == FALSE) {
14357aaaa4fdSJohn Dyson 		vm_map_unlock(map);
14367aaaa4fdSJohn Dyson 		return (KERN_INVALID_ADDRESS);
14377aaaa4fdSJohn Dyson 	}
14387aaaa4fdSJohn Dyson 
14397aaaa4fdSJohn Dyson 	if (new_pageable) {
14407aaaa4fdSJohn Dyson 
14417aaaa4fdSJohn Dyson 		entry = start_entry;
14427aaaa4fdSJohn Dyson 		vm_map_clip_start(map, entry, start);
14437aaaa4fdSJohn Dyson 
14447aaaa4fdSJohn Dyson 		/*
14457aaaa4fdSJohn Dyson 		 * Now decrement the wiring count for each region. If a region
14467aaaa4fdSJohn Dyson 		 * becomes completely unwired, unwire its physical pages and
14477aaaa4fdSJohn Dyson 		 * mappings.
14487aaaa4fdSJohn Dyson 		 */
14497aaaa4fdSJohn Dyson 		while ((entry != &map->header) && (entry->start < end)) {
1450afa07f7eSJohn Dyson 			if (entry->eflags & MAP_ENTRY_USER_WIRED) {
14517aaaa4fdSJohn Dyson 				vm_map_clip_end(map, entry, end);
1452afa07f7eSJohn Dyson 				entry->eflags &= ~MAP_ENTRY_USER_WIRED;
14537aaaa4fdSJohn Dyson 				entry->wired_count--;
14547aaaa4fdSJohn Dyson 				if (entry->wired_count == 0)
14557aaaa4fdSJohn Dyson 					vm_fault_unwire(map, entry->start, entry->end);
14567aaaa4fdSJohn Dyson 			}
1457b44959ceSTor Egge 			vm_map_simplify_entry(map,entry);
14587aaaa4fdSJohn Dyson 			entry = entry->next;
14597aaaa4fdSJohn Dyson 		}
14607aaaa4fdSJohn Dyson 	} else {
14617aaaa4fdSJohn Dyson 
14627aaaa4fdSJohn Dyson 		entry = start_entry;
14637aaaa4fdSJohn Dyson 
14647aaaa4fdSJohn Dyson 		while ((entry != &map->header) && (entry->start < end)) {
14657aaaa4fdSJohn Dyson 
1466afa07f7eSJohn Dyson 			if (entry->eflags & MAP_ENTRY_USER_WIRED) {
14677aaaa4fdSJohn Dyson 				entry = entry->next;
14687aaaa4fdSJohn Dyson 				continue;
14697aaaa4fdSJohn Dyson 			}
14707aaaa4fdSJohn Dyson 
14717aaaa4fdSJohn Dyson 			if (entry->wired_count != 0) {
14727aaaa4fdSJohn Dyson 				entry->wired_count++;
1473afa07f7eSJohn Dyson 				entry->eflags |= MAP_ENTRY_USER_WIRED;
14747aaaa4fdSJohn Dyson 				entry = entry->next;
14757aaaa4fdSJohn Dyson 				continue;
14767aaaa4fdSJohn Dyson 			}
14777aaaa4fdSJohn Dyson 
14787aaaa4fdSJohn Dyson 			/* Here on entry being newly wired */
14797aaaa4fdSJohn Dyson 
14809fdfe602SMatthew Dillon 			if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1481afa07f7eSJohn Dyson 				int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY;
14827aaaa4fdSJohn Dyson 				if (copyflag && ((entry->protection & VM_PROT_WRITE) != 0)) {
14837aaaa4fdSJohn Dyson 
14847aaaa4fdSJohn Dyson 					vm_object_shadow(&entry->object.vm_object,
14857aaaa4fdSJohn Dyson 					    &entry->offset,
1486c2e11a03SJohn Dyson 					    atop(entry->end - entry->start));
1487afa07f7eSJohn Dyson 					entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
14887aaaa4fdSJohn Dyson 
14894e71e795SMatthew Dillon 				} else if (entry->object.vm_object == NULL &&
14904e71e795SMatthew Dillon 					   !map->system_map) {
14917aaaa4fdSJohn Dyson 
14927aaaa4fdSJohn Dyson 					entry->object.vm_object =
14937aaaa4fdSJohn Dyson 					    vm_object_allocate(OBJT_DEFAULT,
1494c2e11a03SJohn Dyson 						atop(entry->end - entry->start));
14957aaaa4fdSJohn Dyson 					entry->offset = (vm_offset_t) 0;
14967aaaa4fdSJohn Dyson 
14977aaaa4fdSJohn Dyson 				}
14987aaaa4fdSJohn Dyson 			}
14997aaaa4fdSJohn Dyson 
15007aaaa4fdSJohn Dyson 			vm_map_clip_start(map, entry, start);
15017aaaa4fdSJohn Dyson 			vm_map_clip_end(map, entry, end);
15027aaaa4fdSJohn Dyson 
15037aaaa4fdSJohn Dyson 			entry->wired_count++;
1504afa07f7eSJohn Dyson 			entry->eflags |= MAP_ENTRY_USER_WIRED;
1505b44959ceSTor Egge 			estart = entry->start;
1506e7673b84STor Egge 			eend = entry->end;
15077aaaa4fdSJohn Dyson 
15087aaaa4fdSJohn Dyson 			/* First we need to allow map modifications */
1509996c772fSJohn Dyson 			vm_map_set_recursive(map);
151003e9c6c1SJohn Dyson 			vm_map_lock_downgrade(map);
151147221757SJohn Dyson 			map->timestamp++;
15127aaaa4fdSJohn Dyson 
15137aaaa4fdSJohn Dyson 			rv = vm_fault_user_wire(map, entry->start, entry->end);
15147aaaa4fdSJohn Dyson 			if (rv) {
15157aaaa4fdSJohn Dyson 
15167aaaa4fdSJohn Dyson 				entry->wired_count--;
1517afa07f7eSJohn Dyson 				entry->eflags &= ~MAP_ENTRY_USER_WIRED;
15187aaaa4fdSJohn Dyson 
1519996c772fSJohn Dyson 				vm_map_clear_recursive(map);
15207aaaa4fdSJohn Dyson 				vm_map_unlock(map);
15217aaaa4fdSJohn Dyson 
1522e7673b84STor Egge 				/*
1523e7673b84STor Egge 				 * At this point, the map is unlocked, and
1524e7673b84STor Egge 				 * entry might no longer be valid.  Use copy
1525e7673b84STor Egge 				 * of entry start value obtained while entry
1526e7673b84STor Egge 				 * was valid.
1527e7673b84STor Egge 				 */
1528e7673b84STor Egge 				(void) vm_map_user_pageable(map, start, estart,
1529e7673b84STor Egge 							    TRUE);
15307aaaa4fdSJohn Dyson 				return rv;
15317aaaa4fdSJohn Dyson 			}
15327aaaa4fdSJohn Dyson 
1533996c772fSJohn Dyson 			vm_map_clear_recursive(map);
1534b44959ceSTor Egge 			if (vm_map_lock_upgrade(map)) {
1535b44959ceSTor Egge 				vm_map_lock(map);
1536b44959ceSTor Egge 				if (vm_map_lookup_entry(map, estart, &entry)
1537b44959ceSTor Egge 				    == FALSE) {
1538b44959ceSTor Egge 					vm_map_unlock(map);
1539e7673b84STor Egge 					/*
1540e7673b84STor Egge 					 * vm_fault_user_wire succeded, thus
1541e7673b84STor Egge 					 * the area between start and eend
1542e7673b84STor Egge 					 * is wired and has to be unwired
1543e7673b84STor Egge 					 * here as part of the cleanup.
1544e7673b84STor Egge 					 */
1545b44959ceSTor Egge 					(void) vm_map_user_pageable(map,
1546b44959ceSTor Egge 								    start,
1547e7673b84STor Egge 								    eend,
1548b44959ceSTor Egge 								    TRUE);
1549b44959ceSTor Egge 					return (KERN_INVALID_ADDRESS);
1550b44959ceSTor Egge 				}
1551b44959ceSTor Egge 			}
1552b44959ceSTor Egge 			vm_map_simplify_entry(map,entry);
15537aaaa4fdSJohn Dyson 		}
15547aaaa4fdSJohn Dyson 	}
155547221757SJohn Dyson 	map->timestamp++;
15567aaaa4fdSJohn Dyson 	vm_map_unlock(map);
15577aaaa4fdSJohn Dyson 	return KERN_SUCCESS;
15587aaaa4fdSJohn Dyson }
15597aaaa4fdSJohn Dyson 
15607aaaa4fdSJohn Dyson /*
1561df8bae1dSRodney W. Grimes  *	vm_map_pageable:
1562df8bae1dSRodney W. Grimes  *
1563df8bae1dSRodney W. Grimes  *	Sets the pageability of the specified address
1564df8bae1dSRodney W. Grimes  *	range in the target map.  Regions specified
1565df8bae1dSRodney W. Grimes  *	as not pageable require locked-down physical
1566df8bae1dSRodney W. Grimes  *	memory and physical page maps.
1567df8bae1dSRodney W. Grimes  *
1568df8bae1dSRodney W. Grimes  *	The map must not be locked, but a reference
1569df8bae1dSRodney W. Grimes  *	must remain to the map throughout the call.
1570df8bae1dSRodney W. Grimes  */
1571df8bae1dSRodney W. Grimes int
15721b40f8c0SMatthew Dillon vm_map_pageable(
15731b40f8c0SMatthew Dillon 	vm_map_t map,
15741b40f8c0SMatthew Dillon 	vm_offset_t start,
15751b40f8c0SMatthew Dillon 	vm_offset_t end,
15761b40f8c0SMatthew Dillon 	boolean_t new_pageable)
1577df8bae1dSRodney W. Grimes {
1578c0877f10SJohn Dyson 	vm_map_entry_t entry;
1579df8bae1dSRodney W. Grimes 	vm_map_entry_t start_entry;
1580c0877f10SJohn Dyson 	vm_offset_t failed = 0;
1581df8bae1dSRodney W. Grimes 	int rv;
1582df8bae1dSRodney W. Grimes 
15830cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
15840cddd8f0SMatthew Dillon 
1585df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1586df8bae1dSRodney W. Grimes 
1587df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1588df8bae1dSRodney W. Grimes 
1589df8bae1dSRodney W. Grimes 	/*
15900d94caffSDavid Greenman 	 * Only one pageability change may take place at one time, since
15910d94caffSDavid Greenman 	 * vm_fault assumes it will be called only once for each
15920d94caffSDavid Greenman 	 * wiring/unwiring.  Therefore, we have to make sure we're actually
15930d94caffSDavid Greenman 	 * changing the pageability for the entire region.  We do so before
15940d94caffSDavid Greenman 	 * making any changes.
1595df8bae1dSRodney W. Grimes 	 */
1596df8bae1dSRodney W. Grimes 
1597df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &start_entry) == FALSE) {
1598df8bae1dSRodney W. Grimes 		vm_map_unlock(map);
1599df8bae1dSRodney W. Grimes 		return (KERN_INVALID_ADDRESS);
1600df8bae1dSRodney W. Grimes 	}
1601df8bae1dSRodney W. Grimes 	entry = start_entry;
1602df8bae1dSRodney W. Grimes 
1603df8bae1dSRodney W. Grimes 	/*
16040d94caffSDavid Greenman 	 * Actions are rather different for wiring and unwiring, so we have
16050d94caffSDavid Greenman 	 * two separate cases.
1606df8bae1dSRodney W. Grimes 	 */
1607df8bae1dSRodney W. Grimes 
1608df8bae1dSRodney W. Grimes 	if (new_pageable) {
1609df8bae1dSRodney W. Grimes 
1610df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
1611df8bae1dSRodney W. Grimes 
1612df8bae1dSRodney W. Grimes 		/*
16130d94caffSDavid Greenman 		 * Unwiring.  First ensure that the range to be unwired is
16140d94caffSDavid Greenman 		 * really wired down and that there are no holes.
1615df8bae1dSRodney W. Grimes 		 */
1616df8bae1dSRodney W. Grimes 		while ((entry != &map->header) && (entry->start < end)) {
1617df8bae1dSRodney W. Grimes 
1618df8bae1dSRodney W. Grimes 			if (entry->wired_count == 0 ||
1619df8bae1dSRodney W. Grimes 			    (entry->end < end &&
1620df8bae1dSRodney W. Grimes 				(entry->next == &map->header ||
1621df8bae1dSRodney W. Grimes 				    entry->next->start > entry->end))) {
1622df8bae1dSRodney W. Grimes 				vm_map_unlock(map);
1623df8bae1dSRodney W. Grimes 				return (KERN_INVALID_ARGUMENT);
1624df8bae1dSRodney W. Grimes 			}
1625df8bae1dSRodney W. Grimes 			entry = entry->next;
1626df8bae1dSRodney W. Grimes 		}
1627df8bae1dSRodney W. Grimes 
1628df8bae1dSRodney W. Grimes 		/*
16290d94caffSDavid Greenman 		 * Now decrement the wiring count for each region. If a region
16300d94caffSDavid Greenman 		 * becomes completely unwired, unwire its physical pages and
16310d94caffSDavid Greenman 		 * mappings.
1632df8bae1dSRodney W. Grimes 		 */
1633df8bae1dSRodney W. Grimes 		entry = start_entry;
1634df8bae1dSRodney W. Grimes 		while ((entry != &map->header) && (entry->start < end)) {
1635df8bae1dSRodney W. Grimes 			vm_map_clip_end(map, entry, end);
1636df8bae1dSRodney W. Grimes 
1637df8bae1dSRodney W. Grimes 			entry->wired_count--;
1638df8bae1dSRodney W. Grimes 			if (entry->wired_count == 0)
1639df8bae1dSRodney W. Grimes 				vm_fault_unwire(map, entry->start, entry->end);
1640df8bae1dSRodney W. Grimes 
164144428f62SAlan Cox 			vm_map_simplify_entry(map, entry);
164244428f62SAlan Cox 
1643df8bae1dSRodney W. Grimes 			entry = entry->next;
1644df8bae1dSRodney W. Grimes 		}
16450d94caffSDavid Greenman 	} else {
1646df8bae1dSRodney W. Grimes 		/*
1647df8bae1dSRodney W. Grimes 		 * Wiring.  We must do this in two passes:
1648df8bae1dSRodney W. Grimes 		 *
16490d94caffSDavid Greenman 		 * 1.  Holding the write lock, we create any shadow or zero-fill
16500d94caffSDavid Greenman 		 * objects that need to be created. Then we clip each map
16510d94caffSDavid Greenman 		 * entry to the region to be wired and increment its wiring
16520d94caffSDavid Greenman 		 * count.  We create objects before clipping the map entries
1653df8bae1dSRodney W. Grimes 		 * to avoid object proliferation.
1654df8bae1dSRodney W. Grimes 		 *
16550d94caffSDavid Greenman 		 * 2.  We downgrade to a read lock, and call vm_fault_wire to
16560d94caffSDavid Greenman 		 * fault in the pages for any newly wired area (wired_count is
16570d94caffSDavid Greenman 		 * 1).
1658df8bae1dSRodney W. Grimes 		 *
16590d94caffSDavid Greenman 		 * Downgrading to a read lock for vm_fault_wire avoids a possible
166024a1cce3SDavid Greenman 		 * deadlock with another process that may have faulted on one
16610d94caffSDavid Greenman 		 * of the pages to be wired (it would mark the page busy,
16620d94caffSDavid Greenman 		 * blocking us, then in turn block on the map lock that we
16630d94caffSDavid Greenman 		 * hold).  Because of problems in the recursive lock package,
16640d94caffSDavid Greenman 		 * we cannot upgrade to a write lock in vm_map_lookup.  Thus,
16650d94caffSDavid Greenman 		 * any actions that require the write lock must be done
16660d94caffSDavid Greenman 		 * beforehand.  Because we keep the read lock on the map, the
16670d94caffSDavid Greenman 		 * copy-on-write status of the entries we modify here cannot
16680d94caffSDavid Greenman 		 * change.
1669df8bae1dSRodney W. Grimes 		 */
1670df8bae1dSRodney W. Grimes 
1671df8bae1dSRodney W. Grimes 		/*
1672df8bae1dSRodney W. Grimes 		 * Pass 1.
1673df8bae1dSRodney W. Grimes 		 */
1674df8bae1dSRodney W. Grimes 		while ((entry != &map->header) && (entry->start < end)) {
1675df8bae1dSRodney W. Grimes 			if (entry->wired_count == 0) {
1676df8bae1dSRodney W. Grimes 
1677df8bae1dSRodney W. Grimes 				/*
1678df8bae1dSRodney W. Grimes 				 * Perform actions of vm_map_lookup that need
1679df8bae1dSRodney W. Grimes 				 * the write lock on the map: create a shadow
1680df8bae1dSRodney W. Grimes 				 * object for a copy-on-write region, or an
1681df8bae1dSRodney W. Grimes 				 * object for a zero-fill region.
1682df8bae1dSRodney W. Grimes 				 *
1683df8bae1dSRodney W. Grimes 				 * We don't have to do this for entries that
1684ad5fca3bSAlan Cox 				 * point to sub maps, because we won't
1685ad5fca3bSAlan Cox 				 * hold the lock on the sub map.
1686df8bae1dSRodney W. Grimes 				 */
16879fdfe602SMatthew Dillon 				if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1688afa07f7eSJohn Dyson 					int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY;
1689b5b40fa6SJohn Dyson 					if (copyflag &&
1690df8bae1dSRodney W. Grimes 					    ((entry->protection & VM_PROT_WRITE) != 0)) {
1691df8bae1dSRodney W. Grimes 
1692df8bae1dSRodney W. Grimes 						vm_object_shadow(&entry->object.vm_object,
1693df8bae1dSRodney W. Grimes 						    &entry->offset,
1694c2e11a03SJohn Dyson 						    atop(entry->end - entry->start));
1695afa07f7eSJohn Dyson 						entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
16964e71e795SMatthew Dillon 					} else if (entry->object.vm_object == NULL &&
16974e71e795SMatthew Dillon 						   !map->system_map) {
1698df8bae1dSRodney W. Grimes 						entry->object.vm_object =
1699a316d390SJohn Dyson 						    vm_object_allocate(OBJT_DEFAULT,
1700c2e11a03SJohn Dyson 							atop(entry->end - entry->start));
1701df8bae1dSRodney W. Grimes 						entry->offset = (vm_offset_t) 0;
1702df8bae1dSRodney W. Grimes 					}
1703df8bae1dSRodney W. Grimes 				}
1704df8bae1dSRodney W. Grimes 			}
1705df8bae1dSRodney W. Grimes 			vm_map_clip_start(map, entry, start);
1706df8bae1dSRodney W. Grimes 			vm_map_clip_end(map, entry, end);
1707df8bae1dSRodney W. Grimes 			entry->wired_count++;
1708df8bae1dSRodney W. Grimes 
1709df8bae1dSRodney W. Grimes 			/*
1710df8bae1dSRodney W. Grimes 			 * Check for holes
1711df8bae1dSRodney W. Grimes 			 */
1712df8bae1dSRodney W. Grimes 			if (entry->end < end &&
1713df8bae1dSRodney W. Grimes 			    (entry->next == &map->header ||
1714df8bae1dSRodney W. Grimes 				entry->next->start > entry->end)) {
1715df8bae1dSRodney W. Grimes 				/*
17160d94caffSDavid Greenman 				 * Found one.  Object creation actions do not
17170d94caffSDavid Greenman 				 * need to be undone, but the wired counts
17180d94caffSDavid Greenman 				 * need to be restored.
1719df8bae1dSRodney W. Grimes 				 */
1720df8bae1dSRodney W. Grimes 				while (entry != &map->header && entry->end > start) {
1721df8bae1dSRodney W. Grimes 					entry->wired_count--;
1722df8bae1dSRodney W. Grimes 					entry = entry->prev;
1723df8bae1dSRodney W. Grimes 				}
1724df8bae1dSRodney W. Grimes 				vm_map_unlock(map);
1725df8bae1dSRodney W. Grimes 				return (KERN_INVALID_ARGUMENT);
1726df8bae1dSRodney W. Grimes 			}
1727df8bae1dSRodney W. Grimes 			entry = entry->next;
1728df8bae1dSRodney W. Grimes 		}
1729df8bae1dSRodney W. Grimes 
1730df8bae1dSRodney W. Grimes 		/*
1731df8bae1dSRodney W. Grimes 		 * Pass 2.
1732df8bae1dSRodney W. Grimes 		 */
1733df8bae1dSRodney W. Grimes 
1734df8bae1dSRodney W. Grimes 		/*
1735df8bae1dSRodney W. Grimes 		 * HACK HACK HACK HACK
1736df8bae1dSRodney W. Grimes 		 *
173724a1cce3SDavid Greenman 		 * If we are wiring in the kernel map or a submap of it,
173824a1cce3SDavid Greenman 		 * unlock the map to avoid deadlocks.  We trust that the
173924a1cce3SDavid Greenman 		 * kernel is well-behaved, and therefore will not do
174024a1cce3SDavid Greenman 		 * anything destructive to this region of the map while
174124a1cce3SDavid Greenman 		 * we have it unlocked.  We cannot trust user processes
174224a1cce3SDavid Greenman 		 * to do the same.
1743df8bae1dSRodney W. Grimes 		 *
1744df8bae1dSRodney W. Grimes 		 * HACK HACK HACK HACK
1745df8bae1dSRodney W. Grimes 		 */
1746df8bae1dSRodney W. Grimes 		if (vm_map_pmap(map) == kernel_pmap) {
1747df8bae1dSRodney W. Grimes 			vm_map_unlock(map);	/* trust me ... */
17480d94caffSDavid Greenman 		} else {
174903e9c6c1SJohn Dyson 			vm_map_lock_downgrade(map);
1750df8bae1dSRodney W. Grimes 		}
1751df8bae1dSRodney W. Grimes 
1752df8bae1dSRodney W. Grimes 		rv = 0;
1753df8bae1dSRodney W. Grimes 		entry = start_entry;
1754df8bae1dSRodney W. Grimes 		while (entry != &map->header && entry->start < end) {
1755df8bae1dSRodney W. Grimes 			/*
17560d94caffSDavid Greenman 			 * If vm_fault_wire fails for any page we need to undo
17570d94caffSDavid Greenman 			 * what has been done.  We decrement the wiring count
17580d94caffSDavid Greenman 			 * for those pages which have not yet been wired (now)
17590d94caffSDavid Greenman 			 * and unwire those that have (later).
1760df8bae1dSRodney W. Grimes 			 *
1761df8bae1dSRodney W. Grimes 			 * XXX this violates the locking protocol on the map,
1762df8bae1dSRodney W. Grimes 			 * needs to be fixed.
1763df8bae1dSRodney W. Grimes 			 */
1764df8bae1dSRodney W. Grimes 			if (rv)
1765df8bae1dSRodney W. Grimes 				entry->wired_count--;
1766df8bae1dSRodney W. Grimes 			else if (entry->wired_count == 1) {
1767df8bae1dSRodney W. Grimes 				rv = vm_fault_wire(map, entry->start, entry->end);
1768df8bae1dSRodney W. Grimes 				if (rv) {
1769df8bae1dSRodney W. Grimes 					failed = entry->start;
1770df8bae1dSRodney W. Grimes 					entry->wired_count--;
1771df8bae1dSRodney W. Grimes 				}
1772df8bae1dSRodney W. Grimes 			}
1773df8bae1dSRodney W. Grimes 			entry = entry->next;
1774df8bae1dSRodney W. Grimes 		}
1775df8bae1dSRodney W. Grimes 
1776df8bae1dSRodney W. Grimes 		if (vm_map_pmap(map) == kernel_pmap) {
1777df8bae1dSRodney W. Grimes 			vm_map_lock(map);
1778df8bae1dSRodney W. Grimes 		}
1779df8bae1dSRodney W. Grimes 		if (rv) {
1780df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
1781df8bae1dSRodney W. Grimes 			(void) vm_map_pageable(map, start, failed, TRUE);
1782df8bae1dSRodney W. Grimes 			return (rv);
1783df8bae1dSRodney W. Grimes 		}
1784e7673b84STor Egge 		/*
1785e7673b84STor Egge 		 * An exclusive lock on the map is needed in order to call
1786e7673b84STor Egge 		 * vm_map_simplify_entry().  If the current lock on the map
1787e7673b84STor Egge 		 * is only a shared lock, an upgrade is needed.
1788e7673b84STor Egge 		 */
1789e7673b84STor Egge 		if (vm_map_pmap(map) != kernel_pmap &&
1790e7673b84STor Egge 		    vm_map_lock_upgrade(map)) {
1791e7673b84STor Egge 			vm_map_lock(map);
1792e7673b84STor Egge 			if (vm_map_lookup_entry(map, start, &start_entry) ==
1793e7673b84STor Egge 			    FALSE) {
1794e7673b84STor Egge 				vm_map_unlock(map);
1795e7673b84STor Egge 				return KERN_SUCCESS;
1796e7673b84STor Egge 			}
1797e7673b84STor Egge 		}
1798b7b2aac2SJohn Dyson 		vm_map_simplify_entry(map, start_entry);
1799df8bae1dSRodney W. Grimes 	}
1800df8bae1dSRodney W. Grimes 
1801df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1802df8bae1dSRodney W. Grimes 
1803df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
1804df8bae1dSRodney W. Grimes }
1805df8bae1dSRodney W. Grimes 
1806df8bae1dSRodney W. Grimes /*
1807df8bae1dSRodney W. Grimes  * vm_map_clean
1808df8bae1dSRodney W. Grimes  *
1809df8bae1dSRodney W. Grimes  * Push any dirty cached pages in the address range to their pager.
1810df8bae1dSRodney W. Grimes  * If syncio is TRUE, dirty pages are written synchronously.
1811df8bae1dSRodney W. Grimes  * If invalidate is TRUE, any cached pages are freed as well.
1812df8bae1dSRodney W. Grimes  *
1813df8bae1dSRodney W. Grimes  * Returns an error if any part of the specified range is not mapped.
1814df8bae1dSRodney W. Grimes  */
1815df8bae1dSRodney W. Grimes int
18161b40f8c0SMatthew Dillon vm_map_clean(
18171b40f8c0SMatthew Dillon 	vm_map_t map,
18181b40f8c0SMatthew Dillon 	vm_offset_t start,
18191b40f8c0SMatthew Dillon 	vm_offset_t end,
18201b40f8c0SMatthew Dillon 	boolean_t syncio,
18211b40f8c0SMatthew Dillon 	boolean_t invalidate)
1822df8bae1dSRodney W. Grimes {
1823c0877f10SJohn Dyson 	vm_map_entry_t current;
1824df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
1825df8bae1dSRodney W. Grimes 	vm_size_t size;
1826df8bae1dSRodney W. Grimes 	vm_object_t object;
1827a316d390SJohn Dyson 	vm_ooffset_t offset;
1828df8bae1dSRodney W. Grimes 
18290cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
18300cddd8f0SMatthew Dillon 
1831df8bae1dSRodney W. Grimes 	vm_map_lock_read(map);
1832df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1833df8bae1dSRodney W. Grimes 	if (!vm_map_lookup_entry(map, start, &entry)) {
1834df8bae1dSRodney W. Grimes 		vm_map_unlock_read(map);
1835df8bae1dSRodney W. Grimes 		return (KERN_INVALID_ADDRESS);
1836df8bae1dSRodney W. Grimes 	}
1837df8bae1dSRodney W. Grimes 	/*
1838df8bae1dSRodney W. Grimes 	 * Make a first pass to check for holes.
1839df8bae1dSRodney W. Grimes 	 */
1840df8bae1dSRodney W. Grimes 	for (current = entry; current->start < end; current = current->next) {
1841afa07f7eSJohn Dyson 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1842df8bae1dSRodney W. Grimes 			vm_map_unlock_read(map);
1843df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ARGUMENT);
1844df8bae1dSRodney W. Grimes 		}
1845df8bae1dSRodney W. Grimes 		if (end > current->end &&
1846df8bae1dSRodney W. Grimes 		    (current->next == &map->header ||
1847df8bae1dSRodney W. Grimes 			current->end != current->next->start)) {
1848df8bae1dSRodney W. Grimes 			vm_map_unlock_read(map);
1849df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ADDRESS);
1850df8bae1dSRodney W. Grimes 		}
1851df8bae1dSRodney W. Grimes 	}
1852df8bae1dSRodney W. Grimes 
1853cf2819ccSJohn Dyson 	if (invalidate)
1854cf2819ccSJohn Dyson 		pmap_remove(vm_map_pmap(map), start, end);
1855df8bae1dSRodney W. Grimes 	/*
1856df8bae1dSRodney W. Grimes 	 * Make a second pass, cleaning/uncaching pages from the indicated
1857df8bae1dSRodney W. Grimes 	 * objects as we go.
1858df8bae1dSRodney W. Grimes 	 */
1859df8bae1dSRodney W. Grimes 	for (current = entry; current->start < end; current = current->next) {
1860df8bae1dSRodney W. Grimes 		offset = current->offset + (start - current->start);
1861df8bae1dSRodney W. Grimes 		size = (end <= current->end ? end : current->end) - start;
18629fdfe602SMatthew Dillon 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1863c0877f10SJohn Dyson 			vm_map_t smap;
1864df8bae1dSRodney W. Grimes 			vm_map_entry_t tentry;
1865df8bae1dSRodney W. Grimes 			vm_size_t tsize;
1866df8bae1dSRodney W. Grimes 
18679fdfe602SMatthew Dillon 			smap = current->object.sub_map;
1868df8bae1dSRodney W. Grimes 			vm_map_lock_read(smap);
1869df8bae1dSRodney W. Grimes 			(void) vm_map_lookup_entry(smap, offset, &tentry);
1870df8bae1dSRodney W. Grimes 			tsize = tentry->end - offset;
1871df8bae1dSRodney W. Grimes 			if (tsize < size)
1872df8bae1dSRodney W. Grimes 				size = tsize;
1873df8bae1dSRodney W. Grimes 			object = tentry->object.vm_object;
1874df8bae1dSRodney W. Grimes 			offset = tentry->offset + (offset - tentry->start);
1875df8bae1dSRodney W. Grimes 			vm_map_unlock_read(smap);
1876df8bae1dSRodney W. Grimes 		} else {
1877df8bae1dSRodney W. Grimes 			object = current->object.vm_object;
1878df8bae1dSRodney W. Grimes 		}
18798a02c104SJohn Dyson 		/*
18808a02c104SJohn Dyson 		 * Note that there is absolutely no sense in writing out
18818a02c104SJohn Dyson 		 * anonymous objects, so we track down the vnode object
18828a02c104SJohn Dyson 		 * to write out.
18838a02c104SJohn Dyson 		 * We invalidate (remove) all pages from the address space
18848a02c104SJohn Dyson 		 * anyway, for semantic correctness.
18858a02c104SJohn Dyson 		 */
18868a02c104SJohn Dyson 		while (object->backing_object) {
18878a02c104SJohn Dyson 			object = object->backing_object;
18888a02c104SJohn Dyson 			offset += object->backing_object_offset;
18898a02c104SJohn Dyson 			if (object->size < OFF_TO_IDX( offset + size))
18908a02c104SJohn Dyson 				size = IDX_TO_OFF(object->size) - offset;
18918a02c104SJohn Dyson 		}
1892ff359f84SMatthew Dillon 		if (object && (object->type == OBJT_VNODE) &&
1893ff359f84SMatthew Dillon 		    (current->protection & VM_PROT_WRITE)) {
1894df8bae1dSRodney W. Grimes 			/*
1895ff359f84SMatthew Dillon 			 * Flush pages if writing is allowed, invalidate them
1896ff359f84SMatthew Dillon 			 * if invalidation requested.  Pages undergoing I/O
1897ff359f84SMatthew Dillon 			 * will be ignored by vm_object_page_remove().
1898f5cf85d4SDavid Greenman 			 *
1899ff359f84SMatthew Dillon 			 * We cannot lock the vnode and then wait for paging
1900ff359f84SMatthew Dillon 			 * to complete without deadlocking against vm_fault.
1901ff359f84SMatthew Dillon 			 * Instead we simply call vm_object_page_remove() and
1902ff359f84SMatthew Dillon 			 * allow it to block internally on a page-by-page
1903ff359f84SMatthew Dillon 			 * basis when it encounters pages undergoing async
1904ff359f84SMatthew Dillon 			 * I/O.
1905df8bae1dSRodney W. Grimes 			 */
19068f9110f6SJohn Dyson 			int flags;
1907ff359f84SMatthew Dillon 
1908ff359f84SMatthew Dillon 			vm_object_reference(object);
1909b40ce416SJulian Elischer 			vn_lock(object->handle, LK_EXCLUSIVE | LK_RETRY, curthread);
19108f9110f6SJohn Dyson 			flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
19118f9110f6SJohn Dyson 			flags |= invalidate ? OBJPC_INVAL : 0;
1912a316d390SJohn Dyson 			vm_object_page_clean(object,
1913a316d390SJohn Dyson 			    OFF_TO_IDX(offset),
19142be70f79SJohn Dyson 			    OFF_TO_IDX(offset + size + PAGE_MASK),
19158f9110f6SJohn Dyson 			    flags);
1916cf2819ccSJohn Dyson 			if (invalidate) {
1917ff359f84SMatthew Dillon 				/*vm_object_pip_wait(object, "objmcl");*/
1918a316d390SJohn Dyson 				vm_object_page_remove(object,
1919a316d390SJohn Dyson 				    OFF_TO_IDX(offset),
19202be70f79SJohn Dyson 				    OFF_TO_IDX(offset + size + PAGE_MASK),
1921a316d390SJohn Dyson 				    FALSE);
1922cf2819ccSJohn Dyson 			}
1923b40ce416SJulian Elischer 			VOP_UNLOCK(object->handle, 0, curthread);
1924ff359f84SMatthew Dillon 			vm_object_deallocate(object);
1925a02051c3SJohn Dyson 		}
1926df8bae1dSRodney W. Grimes 		start += size;
1927df8bae1dSRodney W. Grimes 	}
1928df8bae1dSRodney W. Grimes 
1929df8bae1dSRodney W. Grimes 	vm_map_unlock_read(map);
1930df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
1931df8bae1dSRodney W. Grimes }
1932df8bae1dSRodney W. Grimes 
1933df8bae1dSRodney W. Grimes /*
1934df8bae1dSRodney W. Grimes  *	vm_map_entry_unwire:	[ internal use only ]
1935df8bae1dSRodney W. Grimes  *
1936df8bae1dSRodney W. Grimes  *	Make the region specified by this entry pageable.
1937df8bae1dSRodney W. Grimes  *
1938df8bae1dSRodney W. Grimes  *	The map in question should be locked.
1939df8bae1dSRodney W. Grimes  *	[This is the reason for this routine's existence.]
1940df8bae1dSRodney W. Grimes  */
19410362d7d7SJohn Dyson static void
19421b40f8c0SMatthew Dillon vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
1943df8bae1dSRodney W. Grimes {
1944df8bae1dSRodney W. Grimes 	vm_fault_unwire(map, entry->start, entry->end);
1945df8bae1dSRodney W. Grimes 	entry->wired_count = 0;
1946df8bae1dSRodney W. Grimes }
1947df8bae1dSRodney W. Grimes 
1948df8bae1dSRodney W. Grimes /*
1949df8bae1dSRodney W. Grimes  *	vm_map_entry_delete:	[ internal use only ]
1950df8bae1dSRodney W. Grimes  *
1951df8bae1dSRodney W. Grimes  *	Deallocate the given entry from the target map.
1952df8bae1dSRodney W. Grimes  */
19530362d7d7SJohn Dyson static void
19541b40f8c0SMatthew Dillon vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry)
1955df8bae1dSRodney W. Grimes {
1956df8bae1dSRodney W. Grimes 	vm_map_entry_unlink(map, entry);
1957df8bae1dSRodney W. Grimes 	map->size -= entry->end - entry->start;
1958df8bae1dSRodney W. Grimes 
19599fdfe602SMatthew Dillon 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1960df8bae1dSRodney W. Grimes 		vm_object_deallocate(entry->object.vm_object);
1961b5b40fa6SJohn Dyson 	}
1962df8bae1dSRodney W. Grimes 
1963df8bae1dSRodney W. Grimes 	vm_map_entry_dispose(map, entry);
1964df8bae1dSRodney W. Grimes }
1965df8bae1dSRodney W. Grimes 
1966df8bae1dSRodney W. Grimes /*
1967df8bae1dSRodney W. Grimes  *	vm_map_delete:	[ internal use only ]
1968df8bae1dSRodney W. Grimes  *
1969df8bae1dSRodney W. Grimes  *	Deallocates the given address range from the target
1970df8bae1dSRodney W. Grimes  *	map.
1971df8bae1dSRodney W. Grimes  */
1972df8bae1dSRodney W. Grimes int
19731b40f8c0SMatthew Dillon vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end)
1974df8bae1dSRodney W. Grimes {
1975cbd8ec09SJohn Dyson 	vm_object_t object;
1976c0877f10SJohn Dyson 	vm_map_entry_t entry;
1977df8bae1dSRodney W. Grimes 	vm_map_entry_t first_entry;
1978df8bae1dSRodney W. Grimes 
19790cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
19800cddd8f0SMatthew Dillon 
1981df8bae1dSRodney W. Grimes 	/*
1982df8bae1dSRodney W. Grimes 	 * Find the start of the region, and clip it
1983df8bae1dSRodney W. Grimes 	 */
1984df8bae1dSRodney W. Grimes 
1985876318ecSAlan Cox 	if (!vm_map_lookup_entry(map, start, &first_entry))
1986df8bae1dSRodney W. Grimes 		entry = first_entry->next;
1987876318ecSAlan Cox 	else {
1988df8bae1dSRodney W. Grimes 		entry = first_entry;
1989df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
1990df8bae1dSRodney W. Grimes 		/*
19910d94caffSDavid Greenman 		 * Fix the lookup hint now, rather than each time though the
19920d94caffSDavid Greenman 		 * loop.
1993df8bae1dSRodney W. Grimes 		 */
1994df8bae1dSRodney W. Grimes 		SAVE_HINT(map, entry->prev);
1995df8bae1dSRodney W. Grimes 	}
1996df8bae1dSRodney W. Grimes 
1997df8bae1dSRodney W. Grimes 	/*
1998df8bae1dSRodney W. Grimes 	 * Save the free space hint
1999df8bae1dSRodney W. Grimes 	 */
2000df8bae1dSRodney W. Grimes 
2001b18bfc3dSJohn Dyson 	if (entry == &map->header) {
2002b18bfc3dSJohn Dyson 		map->first_free = &map->header;
20032dbea5d2SJohn Dyson 	} else if (map->first_free->start >= start) {
2004df8bae1dSRodney W. Grimes 		map->first_free = entry->prev;
20052dbea5d2SJohn Dyson 	}
2006df8bae1dSRodney W. Grimes 
2007df8bae1dSRodney W. Grimes 	/*
2008df8bae1dSRodney W. Grimes 	 * Step through all entries in this region
2009df8bae1dSRodney W. Grimes 	 */
2010df8bae1dSRodney W. Grimes 
2011df8bae1dSRodney W. Grimes 	while ((entry != &map->header) && (entry->start < end)) {
2012df8bae1dSRodney W. Grimes 		vm_map_entry_t next;
2013b18bfc3dSJohn Dyson 		vm_offset_t s, e;
2014cbd8ec09SJohn Dyson 		vm_pindex_t offidxstart, offidxend, count;
2015df8bae1dSRodney W. Grimes 
2016df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, entry, end);
2017df8bae1dSRodney W. Grimes 
2018df8bae1dSRodney W. Grimes 		s = entry->start;
2019df8bae1dSRodney W. Grimes 		e = entry->end;
2020c0877f10SJohn Dyson 		next = entry->next;
2021df8bae1dSRodney W. Grimes 
2022cbd8ec09SJohn Dyson 		offidxstart = OFF_TO_IDX(entry->offset);
2023cbd8ec09SJohn Dyson 		count = OFF_TO_IDX(e - s);
2024cbd8ec09SJohn Dyson 		object = entry->object.vm_object;
20252dbea5d2SJohn Dyson 
2026df8bae1dSRodney W. Grimes 		/*
20270d94caffSDavid Greenman 		 * Unwire before removing addresses from the pmap; otherwise,
20280d94caffSDavid Greenman 		 * unwiring will put the entries back in the pmap.
2029df8bae1dSRodney W. Grimes 		 */
2030c0877f10SJohn Dyson 		if (entry->wired_count != 0) {
2031df8bae1dSRodney W. Grimes 			vm_map_entry_unwire(map, entry);
2032c0877f10SJohn Dyson 		}
2033df8bae1dSRodney W. Grimes 
2034cbd8ec09SJohn Dyson 		offidxend = offidxstart + count;
2035df8bae1dSRodney W. Grimes 
2036c0877f10SJohn Dyson 		if ((object == kernel_object) || (object == kmem_object)) {
20372dbea5d2SJohn Dyson 			vm_object_page_remove(object, offidxstart, offidxend, FALSE);
2038b18bfc3dSJohn Dyson 		} else {
2039df8bae1dSRodney W. Grimes 			pmap_remove(map->pmap, s, e);
2040876318ecSAlan Cox 			if (object != NULL &&
2041876318ecSAlan Cox 			    object->ref_count != 1 &&
2042876318ecSAlan Cox 			    (object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING &&
2043876318ecSAlan Cox 			    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
20442dbea5d2SJohn Dyson 				vm_object_collapse(object);
20452dbea5d2SJohn Dyson 				vm_object_page_remove(object, offidxstart, offidxend, FALSE);
20462dbea5d2SJohn Dyson 				if (object->type == OBJT_SWAP) {
2047cbd8ec09SJohn Dyson 					swap_pager_freespace(object, offidxstart, count);
20482dbea5d2SJohn Dyson 				}
2049876318ecSAlan Cox 				if (offidxend >= object->size &&
2050876318ecSAlan Cox 				    offidxstart < object->size) {
2051c0877f10SJohn Dyson 					object->size = offidxstart;
2052c0877f10SJohn Dyson 				}
20532dbea5d2SJohn Dyson 			}
2054b18bfc3dSJohn Dyson 		}
2055df8bae1dSRodney W. Grimes 
2056df8bae1dSRodney W. Grimes 		/*
20570d94caffSDavid Greenman 		 * Delete the entry (which may delete the object) only after
20580d94caffSDavid Greenman 		 * removing all pmap entries pointing to its pages.
20590d94caffSDavid Greenman 		 * (Otherwise, its page frames may be reallocated, and any
20600d94caffSDavid Greenman 		 * modify bits will be set in the wrong object!)
2061df8bae1dSRodney W. Grimes 		 */
2062df8bae1dSRodney W. Grimes 		vm_map_entry_delete(map, entry);
2063df8bae1dSRodney W. Grimes 		entry = next;
2064df8bae1dSRodney W. Grimes 	}
2065df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
2066df8bae1dSRodney W. Grimes }
2067df8bae1dSRodney W. Grimes 
2068df8bae1dSRodney W. Grimes /*
2069df8bae1dSRodney W. Grimes  *	vm_map_remove:
2070df8bae1dSRodney W. Grimes  *
2071df8bae1dSRodney W. Grimes  *	Remove the given address range from the target map.
2072df8bae1dSRodney W. Grimes  *	This is the exported form of vm_map_delete.
2073df8bae1dSRodney W. Grimes  */
2074df8bae1dSRodney W. Grimes int
20751b40f8c0SMatthew Dillon vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
2076df8bae1dSRodney W. Grimes {
2077c0877f10SJohn Dyson 	int result, s = 0;
20788d6e8edeSDavid Greenman 
20790cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
20800cddd8f0SMatthew Dillon 
208108442f8aSBosko Milekic 	if (map == kmem_map)
2082b18bfc3dSJohn Dyson 		s = splvm();
2083df8bae1dSRodney W. Grimes 
2084df8bae1dSRodney W. Grimes 	vm_map_lock(map);
2085df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
2086df8bae1dSRodney W. Grimes 	result = vm_map_delete(map, start, end);
2087df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
2088df8bae1dSRodney W. Grimes 
208908442f8aSBosko Milekic 	if (map == kmem_map)
20908d6e8edeSDavid Greenman 		splx(s);
20918d6e8edeSDavid Greenman 
2092df8bae1dSRodney W. Grimes 	return (result);
2093df8bae1dSRodney W. Grimes }
2094df8bae1dSRodney W. Grimes 
2095df8bae1dSRodney W. Grimes /*
2096df8bae1dSRodney W. Grimes  *	vm_map_check_protection:
2097df8bae1dSRodney W. Grimes  *
2098df8bae1dSRodney W. Grimes  *	Assert that the target map allows the specified
2099df8bae1dSRodney W. Grimes  *	privilege on the entire address region given.
2100df8bae1dSRodney W. Grimes  *	The entire region must be allocated.
2101df8bae1dSRodney W. Grimes  */
21020d94caffSDavid Greenman boolean_t
2103b9dcd593SBruce Evans vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
2104b9dcd593SBruce Evans 			vm_prot_t protection)
2105df8bae1dSRodney W. Grimes {
2106c0877f10SJohn Dyson 	vm_map_entry_t entry;
2107df8bae1dSRodney W. Grimes 	vm_map_entry_t tmp_entry;
2108df8bae1dSRodney W. Grimes 
21090cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
21100cddd8f0SMatthew Dillon 
2111df8bae1dSRodney W. Grimes 	if (!vm_map_lookup_entry(map, start, &tmp_entry)) {
2112df8bae1dSRodney W. Grimes 		return (FALSE);
2113df8bae1dSRodney W. Grimes 	}
2114df8bae1dSRodney W. Grimes 	entry = tmp_entry;
2115df8bae1dSRodney W. Grimes 
2116df8bae1dSRodney W. Grimes 	while (start < end) {
2117df8bae1dSRodney W. Grimes 		if (entry == &map->header) {
2118df8bae1dSRodney W. Grimes 			return (FALSE);
2119df8bae1dSRodney W. Grimes 		}
2120df8bae1dSRodney W. Grimes 		/*
2121df8bae1dSRodney W. Grimes 		 * No holes allowed!
2122df8bae1dSRodney W. Grimes 		 */
2123df8bae1dSRodney W. Grimes 
2124df8bae1dSRodney W. Grimes 		if (start < entry->start) {
2125df8bae1dSRodney W. Grimes 			return (FALSE);
2126df8bae1dSRodney W. Grimes 		}
2127df8bae1dSRodney W. Grimes 		/*
2128df8bae1dSRodney W. Grimes 		 * Check protection associated with entry.
2129df8bae1dSRodney W. Grimes 		 */
2130df8bae1dSRodney W. Grimes 
2131df8bae1dSRodney W. Grimes 		if ((entry->protection & protection) != protection) {
2132df8bae1dSRodney W. Grimes 			return (FALSE);
2133df8bae1dSRodney W. Grimes 		}
2134df8bae1dSRodney W. Grimes 		/* go to next entry */
2135df8bae1dSRodney W. Grimes 
2136df8bae1dSRodney W. Grimes 		start = entry->end;
2137df8bae1dSRodney W. Grimes 		entry = entry->next;
2138df8bae1dSRodney W. Grimes 	}
2139df8bae1dSRodney W. Grimes 	return (TRUE);
2140df8bae1dSRodney W. Grimes }
2141df8bae1dSRodney W. Grimes 
214286524867SJohn Dyson /*
214386524867SJohn Dyson  * Split the pages in a map entry into a new object.  This affords
214486524867SJohn Dyson  * easier removal of unused pages, and keeps object inheritance from
214586524867SJohn Dyson  * being a negative impact on memory usage.
214686524867SJohn Dyson  */
2147c0877f10SJohn Dyson static void
21481b40f8c0SMatthew Dillon vm_map_split(vm_map_entry_t entry)
2149c0877f10SJohn Dyson {
215086524867SJohn Dyson 	vm_page_t m;
2151bd6be915SJohn Dyson 	vm_object_t orig_object, new_object, source;
2152c0877f10SJohn Dyson 	vm_offset_t s, e;
2153c0877f10SJohn Dyson 	vm_pindex_t offidxstart, offidxend, idx;
2154c0877f10SJohn Dyson 	vm_size_t size;
2155c0877f10SJohn Dyson 	vm_ooffset_t offset;
2156c0877f10SJohn Dyson 
21570cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
21580cddd8f0SMatthew Dillon 
2159c0877f10SJohn Dyson 	orig_object = entry->object.vm_object;
2160c0877f10SJohn Dyson 	if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
2161c0877f10SJohn Dyson 		return;
2162c0877f10SJohn Dyson 	if (orig_object->ref_count <= 1)
2163c0877f10SJohn Dyson 		return;
2164c0877f10SJohn Dyson 
2165c0877f10SJohn Dyson 	offset = entry->offset;
2166c0877f10SJohn Dyson 	s = entry->start;
2167c0877f10SJohn Dyson 	e = entry->end;
2168c0877f10SJohn Dyson 
2169c0877f10SJohn Dyson 	offidxstart = OFF_TO_IDX(offset);
2170c0877f10SJohn Dyson 	offidxend = offidxstart + OFF_TO_IDX(e - s);
2171c0877f10SJohn Dyson 	size = offidxend - offidxstart;
2172c0877f10SJohn Dyson 
2173c0877f10SJohn Dyson 	new_object = vm_pager_allocate(orig_object->type,
21746cde7a16SDavid Greenman 		NULL, IDX_TO_OFF(size), VM_PROT_ALL, 0LL);
2175c0877f10SJohn Dyson 	if (new_object == NULL)
2176c0877f10SJohn Dyson 		return;
2177c0877f10SJohn Dyson 
2178bd6be915SJohn Dyson 	source = orig_object->backing_object;
2179bd6be915SJohn Dyson 	if (source != NULL) {
2180bd6be915SJohn Dyson 		vm_object_reference(source);	/* Referenced by new_object */
2181bd6be915SJohn Dyson 		TAILQ_INSERT_TAIL(&source->shadow_head,
2182bd6be915SJohn Dyson 				  new_object, shadow_list);
2183069e9bc1SDoug Rabson 		vm_object_clear_flag(source, OBJ_ONEMAPPING);
2184bd6be915SJohn Dyson 		new_object->backing_object_offset =
2185a0fce827SJohn Polstra 			orig_object->backing_object_offset + IDX_TO_OFF(offidxstart);
2186bd6be915SJohn Dyson 		new_object->backing_object = source;
2187bd6be915SJohn Dyson 		source->shadow_count++;
2188bd6be915SJohn Dyson 		source->generation++;
2189bd6be915SJohn Dyson 	}
2190bd6be915SJohn Dyson 
2191c0877f10SJohn Dyson 	for (idx = 0; idx < size; idx++) {
2192c0877f10SJohn Dyson 		vm_page_t m;
2193c0877f10SJohn Dyson 
2194c0877f10SJohn Dyson 	retry:
2195c0877f10SJohn Dyson 		m = vm_page_lookup(orig_object, offidxstart + idx);
2196c0877f10SJohn Dyson 		if (m == NULL)
2197c0877f10SJohn Dyson 			continue;
21981c7c3c6aSMatthew Dillon 
21991c7c3c6aSMatthew Dillon 		/*
22001c7c3c6aSMatthew Dillon 		 * We must wait for pending I/O to complete before we can
22011c7c3c6aSMatthew Dillon 		 * rename the page.
2202d1bf5d56SMatthew Dillon 		 *
2203d1bf5d56SMatthew Dillon 		 * We do not have to VM_PROT_NONE the page as mappings should
2204d1bf5d56SMatthew Dillon 		 * not be changed by this operation.
22051c7c3c6aSMatthew Dillon 		 */
22061c7c3c6aSMatthew Dillon 		if (vm_page_sleep_busy(m, TRUE, "spltwt"))
2207c0877f10SJohn Dyson 			goto retry;
2208c0877f10SJohn Dyson 
2209e69763a3SDoug Rabson 		vm_page_busy(m);
2210c0877f10SJohn Dyson 		vm_page_rename(m, new_object, idx);
22117dbf82dcSMatthew Dillon 		/* page automatically made dirty by rename and cache handled */
2212e69763a3SDoug Rabson 		vm_page_busy(m);
2213c0877f10SJohn Dyson 	}
2214c0877f10SJohn Dyson 
2215c0877f10SJohn Dyson 	if (orig_object->type == OBJT_SWAP) {
2216d474eaaaSDoug Rabson 		vm_object_pip_add(orig_object, 1);
2217c0877f10SJohn Dyson 		/*
2218c0877f10SJohn Dyson 		 * copy orig_object pages into new_object
2219c0877f10SJohn Dyson 		 * and destroy unneeded pages in
2220c0877f10SJohn Dyson 		 * shadow object.
2221c0877f10SJohn Dyson 		 */
22221c7c3c6aSMatthew Dillon 		swap_pager_copy(orig_object, new_object, offidxstart, 0);
2223c0877f10SJohn Dyson 		vm_object_pip_wakeup(orig_object);
2224c0877f10SJohn Dyson 	}
2225c0877f10SJohn Dyson 
222686524867SJohn Dyson 	for (idx = 0; idx < size; idx++) {
222786524867SJohn Dyson 		m = vm_page_lookup(new_object, idx);
222886524867SJohn Dyson 		if (m) {
2229e69763a3SDoug Rabson 			vm_page_wakeup(m);
223086524867SJohn Dyson 		}
223186524867SJohn Dyson 	}
223286524867SJohn Dyson 
2233c0877f10SJohn Dyson 	entry->object.vm_object = new_object;
2234c0877f10SJohn Dyson 	entry->offset = 0LL;
2235c0877f10SJohn Dyson 	vm_object_deallocate(orig_object);
2236c0877f10SJohn Dyson }
2237c0877f10SJohn Dyson 
2238df8bae1dSRodney W. Grimes /*
2239df8bae1dSRodney W. Grimes  *	vm_map_copy_entry:
2240df8bae1dSRodney W. Grimes  *
2241df8bae1dSRodney W. Grimes  *	Copies the contents of the source entry to the destination
2242df8bae1dSRodney W. Grimes  *	entry.  The entries *must* be aligned properly.
2243df8bae1dSRodney W. Grimes  */
2244f708ef1bSPoul-Henning Kamp static void
22451b40f8c0SMatthew Dillon vm_map_copy_entry(
22461b40f8c0SMatthew Dillon 	vm_map_t src_map,
22471b40f8c0SMatthew Dillon 	vm_map_t dst_map,
22481b40f8c0SMatthew Dillon 	vm_map_entry_t src_entry,
22491b40f8c0SMatthew Dillon 	vm_map_entry_t dst_entry)
2250df8bae1dSRodney W. Grimes {
2251c0877f10SJohn Dyson 	vm_object_t src_object;
2252c0877f10SJohn Dyson 
22539fdfe602SMatthew Dillon 	if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
2254df8bae1dSRodney W. Grimes 		return;
2255df8bae1dSRodney W. Grimes 
2256df8bae1dSRodney W. Grimes 	if (src_entry->wired_count == 0) {
2257df8bae1dSRodney W. Grimes 
2258df8bae1dSRodney W. Grimes 		/*
22590d94caffSDavid Greenman 		 * If the source entry is marked needs_copy, it is already
22600d94caffSDavid Greenman 		 * write-protected.
2261df8bae1dSRodney W. Grimes 		 */
2262afa07f7eSJohn Dyson 		if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
2263df8bae1dSRodney W. Grimes 			pmap_protect(src_map->pmap,
2264df8bae1dSRodney W. Grimes 			    src_entry->start,
2265df8bae1dSRodney W. Grimes 			    src_entry->end,
2266df8bae1dSRodney W. Grimes 			    src_entry->protection & ~VM_PROT_WRITE);
2267df8bae1dSRodney W. Grimes 		}
2268b18bfc3dSJohn Dyson 
2269df8bae1dSRodney W. Grimes 		/*
2270df8bae1dSRodney W. Grimes 		 * Make a copy of the object.
2271df8bae1dSRodney W. Grimes 		 */
22728aef1712SMatthew Dillon 		if ((src_object = src_entry->object.vm_object) != NULL) {
2273c0877f10SJohn Dyson 
2274c0877f10SJohn Dyson 			if ((src_object->handle == NULL) &&
2275c0877f10SJohn Dyson 				(src_object->type == OBJT_DEFAULT ||
2276c0877f10SJohn Dyson 				 src_object->type == OBJT_SWAP)) {
2277c0877f10SJohn Dyson 				vm_object_collapse(src_object);
227896fb8cf2SJohn Dyson 				if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
2279c0877f10SJohn Dyson 					vm_map_split(src_entry);
2280c0877f10SJohn Dyson 					src_object = src_entry->object.vm_object;
2281c0877f10SJohn Dyson 				}
2282c0877f10SJohn Dyson 			}
2283c0877f10SJohn Dyson 
2284c0877f10SJohn Dyson 			vm_object_reference(src_object);
2285069e9bc1SDoug Rabson 			vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
2286c0877f10SJohn Dyson 			dst_entry->object.vm_object = src_object;
2287afa07f7eSJohn Dyson 			src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2288afa07f7eSJohn Dyson 			dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2289b18bfc3dSJohn Dyson 			dst_entry->offset = src_entry->offset;
2290b18bfc3dSJohn Dyson 		} else {
2291b18bfc3dSJohn Dyson 			dst_entry->object.vm_object = NULL;
2292b18bfc3dSJohn Dyson 			dst_entry->offset = 0;
2293b18bfc3dSJohn Dyson 		}
2294df8bae1dSRodney W. Grimes 
2295df8bae1dSRodney W. Grimes 		pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
2296df8bae1dSRodney W. Grimes 		    dst_entry->end - dst_entry->start, src_entry->start);
22970d94caffSDavid Greenman 	} else {
2298df8bae1dSRodney W. Grimes 		/*
2299df8bae1dSRodney W. Grimes 		 * Of course, wired down pages can't be set copy-on-write.
23000d94caffSDavid Greenman 		 * Cause wired pages to be copied into the new map by
23010d94caffSDavid Greenman 		 * simulating faults (the new pages are pageable)
2302df8bae1dSRodney W. Grimes 		 */
2303df8bae1dSRodney W. Grimes 		vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry);
2304df8bae1dSRodney W. Grimes 	}
2305df8bae1dSRodney W. Grimes }
2306df8bae1dSRodney W. Grimes 
2307df8bae1dSRodney W. Grimes /*
2308df8bae1dSRodney W. Grimes  * vmspace_fork:
2309df8bae1dSRodney W. Grimes  * Create a new process vmspace structure and vm_map
2310df8bae1dSRodney W. Grimes  * based on those of an existing process.  The new map
2311df8bae1dSRodney W. Grimes  * is based on the old map, according to the inheritance
2312df8bae1dSRodney W. Grimes  * values on the regions in that map.
2313df8bae1dSRodney W. Grimes  *
2314df8bae1dSRodney W. Grimes  * The source map must not be locked.
2315df8bae1dSRodney W. Grimes  */
2316df8bae1dSRodney W. Grimes struct vmspace *
23171b40f8c0SMatthew Dillon vmspace_fork(struct vmspace *vm1)
2318df8bae1dSRodney W. Grimes {
2319c0877f10SJohn Dyson 	struct vmspace *vm2;
2320df8bae1dSRodney W. Grimes 	vm_map_t old_map = &vm1->vm_map;
2321df8bae1dSRodney W. Grimes 	vm_map_t new_map;
2322df8bae1dSRodney W. Grimes 	vm_map_entry_t old_entry;
2323df8bae1dSRodney W. Grimes 	vm_map_entry_t new_entry;
2324de5f6a77SJohn Dyson 	vm_object_t object;
2325df8bae1dSRodney W. Grimes 
23260cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
23270cddd8f0SMatthew Dillon 
2328df8bae1dSRodney W. Grimes 	vm_map_lock(old_map);
2329b823bbd6SMatthew Dillon 	old_map->infork = 1;
2330df8bae1dSRodney W. Grimes 
23312d8acc0fSJohn Dyson 	vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
2332df8bae1dSRodney W. Grimes 	bcopy(&vm1->vm_startcopy, &vm2->vm_startcopy,
2333582ec34cSAlfred Perlstein 	    (caddr_t) &vm1->vm_endcopy - (caddr_t) &vm1->vm_startcopy);
2334df8bae1dSRodney W. Grimes 	new_map = &vm2->vm_map;	/* XXX */
233547221757SJohn Dyson 	new_map->timestamp = 1;
2336df8bae1dSRodney W. Grimes 
2337df8bae1dSRodney W. Grimes 	old_entry = old_map->header.next;
2338df8bae1dSRodney W. Grimes 
2339df8bae1dSRodney W. Grimes 	while (old_entry != &old_map->header) {
2340afa07f7eSJohn Dyson 		if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2341df8bae1dSRodney W. Grimes 			panic("vm_map_fork: encountered a submap");
2342df8bae1dSRodney W. Grimes 
2343df8bae1dSRodney W. Grimes 		switch (old_entry->inheritance) {
2344df8bae1dSRodney W. Grimes 		case VM_INHERIT_NONE:
2345df8bae1dSRodney W. Grimes 			break;
2346df8bae1dSRodney W. Grimes 
2347df8bae1dSRodney W. Grimes 		case VM_INHERIT_SHARE:
2348df8bae1dSRodney W. Grimes 			/*
2349fed9a903SJohn Dyson 			 * Clone the entry, creating the shared object if necessary.
2350fed9a903SJohn Dyson 			 */
2351fed9a903SJohn Dyson 			object = old_entry->object.vm_object;
2352fed9a903SJohn Dyson 			if (object == NULL) {
2353fed9a903SJohn Dyson 				object = vm_object_allocate(OBJT_DEFAULT,
2354c2e11a03SJohn Dyson 					atop(old_entry->end - old_entry->start));
2355fed9a903SJohn Dyson 				old_entry->object.vm_object = object;
2356fed9a903SJohn Dyson 				old_entry->offset = (vm_offset_t) 0;
23579a2f6362SAlan Cox 			}
23589a2f6362SAlan Cox 
23599a2f6362SAlan Cox 			/*
23609a2f6362SAlan Cox 			 * Add the reference before calling vm_object_shadow
23619a2f6362SAlan Cox 			 * to insure that a shadow object is created.
23629a2f6362SAlan Cox 			 */
23639a2f6362SAlan Cox 			vm_object_reference(object);
23649a2f6362SAlan Cox 			if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
23655069bf57SJohn Dyson 				vm_object_shadow(&old_entry->object.vm_object,
23665069bf57SJohn Dyson 					&old_entry->offset,
2367c2e11a03SJohn Dyson 					atop(old_entry->end - old_entry->start));
23685069bf57SJohn Dyson 				old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
2369d30344bdSIan Dowse 				/* Transfer the second reference too. */
2370d30344bdSIan Dowse 				vm_object_reference(
2371d30344bdSIan Dowse 				    old_entry->object.vm_object);
2372d30344bdSIan Dowse 				vm_object_deallocate(object);
23735069bf57SJohn Dyson 				object = old_entry->object.vm_object;
2374fed9a903SJohn Dyson 			}
2375069e9bc1SDoug Rabson 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
2376fed9a903SJohn Dyson 
2377fed9a903SJohn Dyson 			/*
2378ad5fca3bSAlan Cox 			 * Clone the entry, referencing the shared object.
2379df8bae1dSRodney W. Grimes 			 */
2380df8bae1dSRodney W. Grimes 			new_entry = vm_map_entry_create(new_map);
2381df8bae1dSRodney W. Grimes 			*new_entry = *old_entry;
2382028fe6ecSTor Egge 			new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2383df8bae1dSRodney W. Grimes 			new_entry->wired_count = 0;
2384df8bae1dSRodney W. Grimes 
2385df8bae1dSRodney W. Grimes 			/*
23860d94caffSDavid Greenman 			 * Insert the entry into the new map -- we know we're
23870d94caffSDavid Greenman 			 * inserting at the end of the new map.
2388df8bae1dSRodney W. Grimes 			 */
2389df8bae1dSRodney W. Grimes 
2390df8bae1dSRodney W. Grimes 			vm_map_entry_link(new_map, new_map->header.prev,
2391df8bae1dSRodney W. Grimes 			    new_entry);
2392df8bae1dSRodney W. Grimes 
2393df8bae1dSRodney W. Grimes 			/*
2394df8bae1dSRodney W. Grimes 			 * Update the physical map
2395df8bae1dSRodney W. Grimes 			 */
2396df8bae1dSRodney W. Grimes 
2397df8bae1dSRodney W. Grimes 			pmap_copy(new_map->pmap, old_map->pmap,
2398df8bae1dSRodney W. Grimes 			    new_entry->start,
2399df8bae1dSRodney W. Grimes 			    (old_entry->end - old_entry->start),
2400df8bae1dSRodney W. Grimes 			    old_entry->start);
2401df8bae1dSRodney W. Grimes 			break;
2402df8bae1dSRodney W. Grimes 
2403df8bae1dSRodney W. Grimes 		case VM_INHERIT_COPY:
2404df8bae1dSRodney W. Grimes 			/*
2405df8bae1dSRodney W. Grimes 			 * Clone the entry and link into the map.
2406df8bae1dSRodney W. Grimes 			 */
2407df8bae1dSRodney W. Grimes 			new_entry = vm_map_entry_create(new_map);
2408df8bae1dSRodney W. Grimes 			*new_entry = *old_entry;
2409028fe6ecSTor Egge 			new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2410df8bae1dSRodney W. Grimes 			new_entry->wired_count = 0;
2411df8bae1dSRodney W. Grimes 			new_entry->object.vm_object = NULL;
2412df8bae1dSRodney W. Grimes 			vm_map_entry_link(new_map, new_map->header.prev,
2413df8bae1dSRodney W. Grimes 			    new_entry);
2414bd7e5f99SJohn Dyson 			vm_map_copy_entry(old_map, new_map, old_entry,
2415bd7e5f99SJohn Dyson 			    new_entry);
2416df8bae1dSRodney W. Grimes 			break;
2417df8bae1dSRodney W. Grimes 		}
2418df8bae1dSRodney W. Grimes 		old_entry = old_entry->next;
2419df8bae1dSRodney W. Grimes 	}
2420df8bae1dSRodney W. Grimes 
2421df8bae1dSRodney W. Grimes 	new_map->size = old_map->size;
2422b823bbd6SMatthew Dillon 	old_map->infork = 0;
2423df8bae1dSRodney W. Grimes 	vm_map_unlock(old_map);
2424df8bae1dSRodney W. Grimes 
2425df8bae1dSRodney W. Grimes 	return (vm2);
2426df8bae1dSRodney W. Grimes }
2427df8bae1dSRodney W. Grimes 
242894f7e29aSAlan Cox int
242994f7e29aSAlan Cox vm_map_stack (vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
243094f7e29aSAlan Cox 	      vm_prot_t prot, vm_prot_t max, int cow)
243194f7e29aSAlan Cox {
243294f7e29aSAlan Cox 	vm_map_entry_t prev_entry;
243394f7e29aSAlan Cox 	vm_map_entry_t new_stack_entry;
243494f7e29aSAlan Cox 	vm_size_t      init_ssize;
243594f7e29aSAlan Cox 	int            rv;
243694f7e29aSAlan Cox 
24370cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
24380cddd8f0SMatthew Dillon 
243994f7e29aSAlan Cox 	if (VM_MIN_ADDRESS > 0 && addrbos < VM_MIN_ADDRESS)
244094f7e29aSAlan Cox 		return (KERN_NO_SPACE);
244194f7e29aSAlan Cox 
2442cbc89bfbSPaul Saab 	if (max_ssize < sgrowsiz)
244394f7e29aSAlan Cox 		init_ssize = max_ssize;
244494f7e29aSAlan Cox 	else
2445cbc89bfbSPaul Saab 		init_ssize = sgrowsiz;
244694f7e29aSAlan Cox 
244794f7e29aSAlan Cox 	vm_map_lock(map);
244894f7e29aSAlan Cox 
244994f7e29aSAlan Cox 	/* If addr is already mapped, no go */
245094f7e29aSAlan Cox 	if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
245194f7e29aSAlan Cox 		vm_map_unlock(map);
245294f7e29aSAlan Cox 		return (KERN_NO_SPACE);
245394f7e29aSAlan Cox 	}
245494f7e29aSAlan Cox 
245594f7e29aSAlan Cox 	/* If we can't accomodate max_ssize in the current mapping,
245694f7e29aSAlan Cox 	 * no go.  However, we need to be aware that subsequent user
245794f7e29aSAlan Cox 	 * mappings might map into the space we have reserved for
245894f7e29aSAlan Cox 	 * stack, and currently this space is not protected.
245994f7e29aSAlan Cox 	 *
246094f7e29aSAlan Cox 	 * Hopefully we will at least detect this condition
246194f7e29aSAlan Cox 	 * when we try to grow the stack.
246294f7e29aSAlan Cox 	 */
246394f7e29aSAlan Cox 	if ((prev_entry->next != &map->header) &&
246494f7e29aSAlan Cox 	    (prev_entry->next->start < addrbos + max_ssize)) {
246594f7e29aSAlan Cox 		vm_map_unlock(map);
246694f7e29aSAlan Cox 		return (KERN_NO_SPACE);
246794f7e29aSAlan Cox 	}
246894f7e29aSAlan Cox 
246994f7e29aSAlan Cox 	/* We initially map a stack of only init_ssize.  We will
247094f7e29aSAlan Cox 	 * grow as needed later.  Since this is to be a grow
247194f7e29aSAlan Cox 	 * down stack, we map at the top of the range.
247294f7e29aSAlan Cox 	 *
247394f7e29aSAlan Cox 	 * Note: we would normally expect prot and max to be
247494f7e29aSAlan Cox 	 * VM_PROT_ALL, and cow to be 0.  Possibly we should
247594f7e29aSAlan Cox 	 * eliminate these as input parameters, and just
247694f7e29aSAlan Cox 	 * pass these values here in the insert call.
247794f7e29aSAlan Cox 	 */
247894f7e29aSAlan Cox 	rv = vm_map_insert(map, NULL, 0, addrbos + max_ssize - init_ssize,
247994f7e29aSAlan Cox 	                   addrbos + max_ssize, prot, max, cow);
248094f7e29aSAlan Cox 
248194f7e29aSAlan Cox 	/* Now set the avail_ssize amount */
248294f7e29aSAlan Cox 	if (rv == KERN_SUCCESS){
248329b45e9eSAlan Cox 		if (prev_entry != &map->header)
248429b45e9eSAlan Cox 			vm_map_clip_end(map, prev_entry, addrbos + max_ssize - init_ssize);
248594f7e29aSAlan Cox 		new_stack_entry = prev_entry->next;
248694f7e29aSAlan Cox 		if (new_stack_entry->end   != addrbos + max_ssize ||
248794f7e29aSAlan Cox 		    new_stack_entry->start != addrbos + max_ssize - init_ssize)
248894f7e29aSAlan Cox 			panic ("Bad entry start/end for new stack entry");
248994f7e29aSAlan Cox 		else
249094f7e29aSAlan Cox 			new_stack_entry->avail_ssize = max_ssize - init_ssize;
249194f7e29aSAlan Cox 	}
249294f7e29aSAlan Cox 
249394f7e29aSAlan Cox 	vm_map_unlock(map);
249494f7e29aSAlan Cox 	return (rv);
249594f7e29aSAlan Cox }
249694f7e29aSAlan Cox 
249794f7e29aSAlan Cox /* Attempts to grow a vm stack entry.  Returns KERN_SUCCESS if the
249894f7e29aSAlan Cox  * desired address is already mapped, or if we successfully grow
249994f7e29aSAlan Cox  * the stack.  Also returns KERN_SUCCESS if addr is outside the
250094f7e29aSAlan Cox  * stack range (this is strange, but preserves compatibility with
250194f7e29aSAlan Cox  * the grow function in vm_machdep.c).
250294f7e29aSAlan Cox  */
250394f7e29aSAlan Cox int
250494f7e29aSAlan Cox vm_map_growstack (struct proc *p, vm_offset_t addr)
250594f7e29aSAlan Cox {
250694f7e29aSAlan Cox 	vm_map_entry_t prev_entry;
250794f7e29aSAlan Cox 	vm_map_entry_t stack_entry;
250894f7e29aSAlan Cox 	vm_map_entry_t new_stack_entry;
250994f7e29aSAlan Cox 	struct vmspace *vm = p->p_vmspace;
251094f7e29aSAlan Cox 	vm_map_t map = &vm->vm_map;
251194f7e29aSAlan Cox 	vm_offset_t    end;
251294f7e29aSAlan Cox 	int      grow_amount;
251394f7e29aSAlan Cox 	int      rv;
251494f7e29aSAlan Cox 	int      is_procstack;
251523955314SAlfred Perlstein 
25160cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
251723955314SAlfred Perlstein 
251894f7e29aSAlan Cox Retry:
251994f7e29aSAlan Cox 	vm_map_lock_read(map);
252094f7e29aSAlan Cox 
252194f7e29aSAlan Cox 	/* If addr is already in the entry range, no need to grow.*/
252294f7e29aSAlan Cox 	if (vm_map_lookup_entry(map, addr, &prev_entry)) {
252394f7e29aSAlan Cox 		vm_map_unlock_read(map);
25240cddd8f0SMatthew Dillon 		return (KERN_SUCCESS);
252594f7e29aSAlan Cox 	}
252694f7e29aSAlan Cox 
252794f7e29aSAlan Cox 	if ((stack_entry = prev_entry->next) == &map->header) {
252894f7e29aSAlan Cox 		vm_map_unlock_read(map);
25290cddd8f0SMatthew Dillon 		return (KERN_SUCCESS);
253094f7e29aSAlan Cox 	}
253194f7e29aSAlan Cox 	if (prev_entry == &map->header)
253294f7e29aSAlan Cox 		end = stack_entry->start - stack_entry->avail_ssize;
253394f7e29aSAlan Cox 	else
253494f7e29aSAlan Cox 		end = prev_entry->end;
253594f7e29aSAlan Cox 
253694f7e29aSAlan Cox 	/* This next test mimics the old grow function in vm_machdep.c.
253794f7e29aSAlan Cox 	 * It really doesn't quite make sense, but we do it anyway
253894f7e29aSAlan Cox 	 * for compatibility.
253994f7e29aSAlan Cox 	 *
254094f7e29aSAlan Cox 	 * If not growable stack, return success.  This signals the
254194f7e29aSAlan Cox 	 * caller to proceed as he would normally with normal vm.
254294f7e29aSAlan Cox 	 */
254394f7e29aSAlan Cox 	if (stack_entry->avail_ssize < 1 ||
254494f7e29aSAlan Cox 	    addr >= stack_entry->start ||
254594f7e29aSAlan Cox 	    addr <  stack_entry->start - stack_entry->avail_ssize) {
254694f7e29aSAlan Cox 		vm_map_unlock_read(map);
25470cddd8f0SMatthew Dillon 		return (KERN_SUCCESS);
254894f7e29aSAlan Cox 	}
254994f7e29aSAlan Cox 
255094f7e29aSAlan Cox 	/* Find the minimum grow amount */
255194f7e29aSAlan Cox 	grow_amount = roundup (stack_entry->start - addr, PAGE_SIZE);
255294f7e29aSAlan Cox 	if (grow_amount > stack_entry->avail_ssize) {
255394f7e29aSAlan Cox 		vm_map_unlock_read(map);
25540cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
255594f7e29aSAlan Cox 	}
255694f7e29aSAlan Cox 
255794f7e29aSAlan Cox 	/* If there is no longer enough space between the entries
255894f7e29aSAlan Cox 	 * nogo, and adjust the available space.  Note: this
255994f7e29aSAlan Cox 	 * should only happen if the user has mapped into the
256094f7e29aSAlan Cox 	 * stack area after the stack was created, and is
256194f7e29aSAlan Cox 	 * probably an error.
256294f7e29aSAlan Cox 	 *
256394f7e29aSAlan Cox 	 * This also effectively destroys any guard page the user
256494f7e29aSAlan Cox 	 * might have intended by limiting the stack size.
256594f7e29aSAlan Cox 	 */
256694f7e29aSAlan Cox 	if (grow_amount > stack_entry->start - end) {
256794f7e29aSAlan Cox 		if (vm_map_lock_upgrade(map))
256894f7e29aSAlan Cox 			goto Retry;
256994f7e29aSAlan Cox 
257094f7e29aSAlan Cox 		stack_entry->avail_ssize = stack_entry->start - end;
257194f7e29aSAlan Cox 
257294f7e29aSAlan Cox 		vm_map_unlock(map);
25730cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
257494f7e29aSAlan Cox 	}
257594f7e29aSAlan Cox 
257694f7e29aSAlan Cox 	is_procstack = addr >= (vm_offset_t)vm->vm_maxsaddr;
257794f7e29aSAlan Cox 
257894f7e29aSAlan Cox 	/* If this is the main process stack, see if we're over the
257994f7e29aSAlan Cox 	 * stack limit.
258094f7e29aSAlan Cox 	 */
25816389da78SAlan Cox 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount >
258294f7e29aSAlan Cox 			     p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
258394f7e29aSAlan Cox 		vm_map_unlock_read(map);
25840cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
258594f7e29aSAlan Cox 	}
258694f7e29aSAlan Cox 
258794f7e29aSAlan Cox 	/* Round up the grow amount modulo SGROWSIZ */
2588cbc89bfbSPaul Saab 	grow_amount = roundup (grow_amount, sgrowsiz);
258994f7e29aSAlan Cox 	if (grow_amount > stack_entry->avail_ssize) {
259094f7e29aSAlan Cox 		grow_amount = stack_entry->avail_ssize;
259194f7e29aSAlan Cox 	}
25926389da78SAlan Cox 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount >
259394f7e29aSAlan Cox 	                     p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
259494f7e29aSAlan Cox 		grow_amount = p->p_rlimit[RLIMIT_STACK].rlim_cur -
25956389da78SAlan Cox 		              ctob(vm->vm_ssize);
259694f7e29aSAlan Cox 	}
259794f7e29aSAlan Cox 
259894f7e29aSAlan Cox 	if (vm_map_lock_upgrade(map))
259994f7e29aSAlan Cox 		goto Retry;
260094f7e29aSAlan Cox 
260194f7e29aSAlan Cox 	/* Get the preliminary new entry start value */
260294f7e29aSAlan Cox 	addr = stack_entry->start - grow_amount;
260394f7e29aSAlan Cox 
260494f7e29aSAlan Cox 	/* If this puts us into the previous entry, cut back our growth
260594f7e29aSAlan Cox 	 * to the available space.  Also, see the note above.
260694f7e29aSAlan Cox 	 */
260794f7e29aSAlan Cox 	if (addr < end) {
260894f7e29aSAlan Cox 		stack_entry->avail_ssize = stack_entry->start - end;
260994f7e29aSAlan Cox 		addr = end;
261094f7e29aSAlan Cox 	}
261194f7e29aSAlan Cox 
261294f7e29aSAlan Cox 	rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start,
261329b45e9eSAlan Cox 			   VM_PROT_ALL,
261429b45e9eSAlan Cox 			   VM_PROT_ALL,
261594f7e29aSAlan Cox 			   0);
261694f7e29aSAlan Cox 
261794f7e29aSAlan Cox 	/* Adjust the available stack space by the amount we grew. */
261894f7e29aSAlan Cox 	if (rv == KERN_SUCCESS) {
261929b45e9eSAlan Cox 		if (prev_entry != &map->header)
262029b45e9eSAlan Cox 			vm_map_clip_end(map, prev_entry, addr);
262194f7e29aSAlan Cox 		new_stack_entry = prev_entry->next;
262294f7e29aSAlan Cox 		if (new_stack_entry->end   != stack_entry->start  ||
262394f7e29aSAlan Cox 		    new_stack_entry->start != addr)
262494f7e29aSAlan Cox 			panic ("Bad stack grow start/end in new stack entry");
262594f7e29aSAlan Cox 		else {
262694f7e29aSAlan Cox 			new_stack_entry->avail_ssize = stack_entry->avail_ssize -
262794f7e29aSAlan Cox 							(new_stack_entry->end -
262894f7e29aSAlan Cox 							 new_stack_entry->start);
262994f7e29aSAlan Cox 			if (is_procstack)
26306389da78SAlan Cox 				vm->vm_ssize += btoc(new_stack_entry->end -
26316389da78SAlan Cox 						     new_stack_entry->start);
263294f7e29aSAlan Cox 		}
263394f7e29aSAlan Cox 	}
263494f7e29aSAlan Cox 
263594f7e29aSAlan Cox 	vm_map_unlock(map);
26360cddd8f0SMatthew Dillon 	return (rv);
263794f7e29aSAlan Cox }
263894f7e29aSAlan Cox 
2639df8bae1dSRodney W. Grimes /*
26405856e12eSJohn Dyson  * Unshare the specified VM space for exec.  If other processes are
26415856e12eSJohn Dyson  * mapped to it, then create a new one.  The new vmspace is null.
26425856e12eSJohn Dyson  */
26435856e12eSJohn Dyson 
26445856e12eSJohn Dyson void
26451b40f8c0SMatthew Dillon vmspace_exec(struct proc *p)
26461b40f8c0SMatthew Dillon {
26475856e12eSJohn Dyson 	struct vmspace *oldvmspace = p->p_vmspace;
26485856e12eSJohn Dyson 	struct vmspace *newvmspace;
26495856e12eSJohn Dyson 	vm_map_t map = &p->p_vmspace->vm_map;
26505856e12eSJohn Dyson 
26510cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
26522d8acc0fSJohn Dyson 	newvmspace = vmspace_alloc(map->min_offset, map->max_offset);
26535856e12eSJohn Dyson 	bcopy(&oldvmspace->vm_startcopy, &newvmspace->vm_startcopy,
26545856e12eSJohn Dyson 	    (caddr_t) (newvmspace + 1) - (caddr_t) &newvmspace->vm_startcopy);
26555856e12eSJohn Dyson 	/*
26565856e12eSJohn Dyson 	 * This code is written like this for prototype purposes.  The
26575856e12eSJohn Dyson 	 * goal is to avoid running down the vmspace here, but let the
26585856e12eSJohn Dyson 	 * other process's that are still using the vmspace to finally
26595856e12eSJohn Dyson 	 * run it down.  Even though there is little or no chance of blocking
26605856e12eSJohn Dyson 	 * here, it is a good idea to keep this form for future mods.
26615856e12eSJohn Dyson 	 */
26625856e12eSJohn Dyson 	p->p_vmspace = newvmspace;
2663d4da2dbaSAlan Cox 	pmap_pinit2(vmspace_pmap(newvmspace));
266421c641b2SJohn Baldwin 	vmspace_free(oldvmspace);
2665b40ce416SJulian Elischer 	if (p == curthread->td_proc)		/* XXXKSE ? */
2666b40ce416SJulian Elischer 		pmap_activate(curthread);
26675856e12eSJohn Dyson }
26685856e12eSJohn Dyson 
26695856e12eSJohn Dyson /*
26705856e12eSJohn Dyson  * Unshare the specified VM space for forcing COW.  This
26715856e12eSJohn Dyson  * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
26725856e12eSJohn Dyson  */
26735856e12eSJohn Dyson 
26745856e12eSJohn Dyson void
26751b40f8c0SMatthew Dillon vmspace_unshare(struct proc *p)
26761b40f8c0SMatthew Dillon {
26775856e12eSJohn Dyson 	struct vmspace *oldvmspace = p->p_vmspace;
26785856e12eSJohn Dyson 	struct vmspace *newvmspace;
26795856e12eSJohn Dyson 
26800cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
26815856e12eSJohn Dyson 	if (oldvmspace->vm_refcnt == 1)
26825856e12eSJohn Dyson 		return;
26835856e12eSJohn Dyson 	newvmspace = vmspace_fork(oldvmspace);
26845856e12eSJohn Dyson 	p->p_vmspace = newvmspace;
2685d4da2dbaSAlan Cox 	pmap_pinit2(vmspace_pmap(newvmspace));
268621c641b2SJohn Baldwin 	vmspace_free(oldvmspace);
2687b40ce416SJulian Elischer 	if (p == curthread->td_proc)		/* XXXKSE ? */
2688b40ce416SJulian Elischer 		pmap_activate(curthread);
26895856e12eSJohn Dyson }
26905856e12eSJohn Dyson 
26915856e12eSJohn Dyson 
26925856e12eSJohn Dyson /*
2693df8bae1dSRodney W. Grimes  *	vm_map_lookup:
2694df8bae1dSRodney W. Grimes  *
2695df8bae1dSRodney W. Grimes  *	Finds the VM object, offset, and
2696df8bae1dSRodney W. Grimes  *	protection for a given virtual address in the
2697df8bae1dSRodney W. Grimes  *	specified map, assuming a page fault of the
2698df8bae1dSRodney W. Grimes  *	type specified.
2699df8bae1dSRodney W. Grimes  *
2700df8bae1dSRodney W. Grimes  *	Leaves the map in question locked for read; return
2701df8bae1dSRodney W. Grimes  *	values are guaranteed until a vm_map_lookup_done
2702df8bae1dSRodney W. Grimes  *	call is performed.  Note that the map argument
2703df8bae1dSRodney W. Grimes  *	is in/out; the returned map must be used in
2704df8bae1dSRodney W. Grimes  *	the call to vm_map_lookup_done.
2705df8bae1dSRodney W. Grimes  *
2706df8bae1dSRodney W. Grimes  *	A handle (out_entry) is returned for use in
2707df8bae1dSRodney W. Grimes  *	vm_map_lookup_done, to make that fast.
2708df8bae1dSRodney W. Grimes  *
2709df8bae1dSRodney W. Grimes  *	If a lookup is requested with "write protection"
2710df8bae1dSRodney W. Grimes  *	specified, the map may be changed to perform virtual
2711df8bae1dSRodney W. Grimes  *	copying operations, although the data referenced will
2712df8bae1dSRodney W. Grimes  *	remain the same.
2713df8bae1dSRodney W. Grimes  */
2714df8bae1dSRodney W. Grimes int
2715b9dcd593SBruce Evans vm_map_lookup(vm_map_t *var_map,		/* IN/OUT */
2716b9dcd593SBruce Evans 	      vm_offset_t vaddr,
271747221757SJohn Dyson 	      vm_prot_t fault_typea,
2718b9dcd593SBruce Evans 	      vm_map_entry_t *out_entry,	/* OUT */
2719b9dcd593SBruce Evans 	      vm_object_t *object,		/* OUT */
2720b9dcd593SBruce Evans 	      vm_pindex_t *pindex,		/* OUT */
2721b9dcd593SBruce Evans 	      vm_prot_t *out_prot,		/* OUT */
27222d8acc0fSJohn Dyson 	      boolean_t *wired)			/* OUT */
2723df8bae1dSRodney W. Grimes {
2724c0877f10SJohn Dyson 	vm_map_entry_t entry;
2725c0877f10SJohn Dyson 	vm_map_t map = *var_map;
2726c0877f10SJohn Dyson 	vm_prot_t prot;
272747221757SJohn Dyson 	vm_prot_t fault_type = fault_typea;
2728df8bae1dSRodney W. Grimes 
27290cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
2730df8bae1dSRodney W. Grimes RetryLookup:;
2731df8bae1dSRodney W. Grimes 
2732df8bae1dSRodney W. Grimes 	/*
2733df8bae1dSRodney W. Grimes 	 * Lookup the faulting address.
2734df8bae1dSRodney W. Grimes 	 */
2735df8bae1dSRodney W. Grimes 
2736df8bae1dSRodney W. Grimes 	vm_map_lock_read(map);
2737df8bae1dSRodney W. Grimes 
2738df8bae1dSRodney W. Grimes #define	RETURN(why) \
2739df8bae1dSRodney W. Grimes 		{ \
2740df8bae1dSRodney W. Grimes 		vm_map_unlock_read(map); \
2741df8bae1dSRodney W. Grimes 		return(why); \
2742df8bae1dSRodney W. Grimes 		}
2743df8bae1dSRodney W. Grimes 
2744df8bae1dSRodney W. Grimes 	/*
27450d94caffSDavid Greenman 	 * If the map has an interesting hint, try it before calling full
27460d94caffSDavid Greenman 	 * blown lookup routine.
2747df8bae1dSRodney W. Grimes 	 */
2748df8bae1dSRodney W. Grimes 
2749df8bae1dSRodney W. Grimes 	entry = map->hint;
2750df8bae1dSRodney W. Grimes 
2751df8bae1dSRodney W. Grimes 	*out_entry = entry;
2752df8bae1dSRodney W. Grimes 
2753df8bae1dSRodney W. Grimes 	if ((entry == &map->header) ||
2754df8bae1dSRodney W. Grimes 	    (vaddr < entry->start) || (vaddr >= entry->end)) {
2755df8bae1dSRodney W. Grimes 		vm_map_entry_t tmp_entry;
2756df8bae1dSRodney W. Grimes 
2757df8bae1dSRodney W. Grimes 		/*
27580d94caffSDavid Greenman 		 * Entry was either not a valid hint, or the vaddr was not
27590d94caffSDavid Greenman 		 * contained in the entry, so do a full lookup.
2760df8bae1dSRodney W. Grimes 		 */
2761df8bae1dSRodney W. Grimes 		if (!vm_map_lookup_entry(map, vaddr, &tmp_entry))
2762df8bae1dSRodney W. Grimes 			RETURN(KERN_INVALID_ADDRESS);
2763df8bae1dSRodney W. Grimes 
2764df8bae1dSRodney W. Grimes 		entry = tmp_entry;
2765df8bae1dSRodney W. Grimes 		*out_entry = entry;
2766df8bae1dSRodney W. Grimes 	}
2767b7b2aac2SJohn Dyson 
2768df8bae1dSRodney W. Grimes 	/*
2769df8bae1dSRodney W. Grimes 	 * Handle submaps.
2770df8bae1dSRodney W. Grimes 	 */
2771df8bae1dSRodney W. Grimes 
2772afa07f7eSJohn Dyson 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
2773df8bae1dSRodney W. Grimes 		vm_map_t old_map = map;
2774df8bae1dSRodney W. Grimes 
2775df8bae1dSRodney W. Grimes 		*var_map = map = entry->object.sub_map;
2776df8bae1dSRodney W. Grimes 		vm_map_unlock_read(old_map);
2777df8bae1dSRodney W. Grimes 		goto RetryLookup;
2778df8bae1dSRodney W. Grimes 	}
2779a04c970aSJohn Dyson 
2780df8bae1dSRodney W. Grimes 	/*
27810d94caffSDavid Greenman 	 * Check whether this task is allowed to have this page.
2782a04c970aSJohn Dyson 	 * Note the special case for MAP_ENTRY_COW
2783a04c970aSJohn Dyson 	 * pages with an override.  This is to implement a forced
2784a04c970aSJohn Dyson 	 * COW for debuggers.
2785df8bae1dSRodney W. Grimes 	 */
2786df8bae1dSRodney W. Grimes 
2787480ba2f5SJohn Dyson 	if (fault_type & VM_PROT_OVERRIDE_WRITE)
2788480ba2f5SJohn Dyson 		prot = entry->max_protection;
2789480ba2f5SJohn Dyson 	else
2790df8bae1dSRodney W. Grimes 		prot = entry->protection;
279147221757SJohn Dyson 
279247221757SJohn Dyson 	fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
279347221757SJohn Dyson 	if ((fault_type & prot) != fault_type) {
279447221757SJohn Dyson 			RETURN(KERN_PROTECTION_FAILURE);
279547221757SJohn Dyson 	}
279647221757SJohn Dyson 
27972ed14a92SAlan Cox 	if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
279847221757SJohn Dyson 	    (entry->eflags & MAP_ENTRY_COW) &&
27992ed14a92SAlan Cox 	    (fault_type & VM_PROT_WRITE) &&
280047221757SJohn Dyson 	    (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
2801df8bae1dSRodney W. Grimes 		RETURN(KERN_PROTECTION_FAILURE);
2802a04c970aSJohn Dyson 	}
2803df8bae1dSRodney W. Grimes 
2804df8bae1dSRodney W. Grimes 	/*
28050d94caffSDavid Greenman 	 * If this page is not pageable, we have to get it for all possible
28060d94caffSDavid Greenman 	 * accesses.
2807df8bae1dSRodney W. Grimes 	 */
2808df8bae1dSRodney W. Grimes 
280905f0fdd2SPoul-Henning Kamp 	*wired = (entry->wired_count != 0);
281005f0fdd2SPoul-Henning Kamp 	if (*wired)
2811df8bae1dSRodney W. Grimes 		prot = fault_type = entry->protection;
2812df8bae1dSRodney W. Grimes 
2813df8bae1dSRodney W. Grimes 	/*
2814df8bae1dSRodney W. Grimes 	 * If the entry was copy-on-write, we either ...
2815df8bae1dSRodney W. Grimes 	 */
2816df8bae1dSRodney W. Grimes 
2817afa07f7eSJohn Dyson 	if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
2818df8bae1dSRodney W. Grimes 		/*
28190d94caffSDavid Greenman 		 * If we want to write the page, we may as well handle that
2820ad5fca3bSAlan Cox 		 * now since we've got the map locked.
2821df8bae1dSRodney W. Grimes 		 *
28220d94caffSDavid Greenman 		 * If we don't need to write the page, we just demote the
28230d94caffSDavid Greenman 		 * permissions allowed.
2824df8bae1dSRodney W. Grimes 		 */
2825df8bae1dSRodney W. Grimes 
2826df8bae1dSRodney W. Grimes 		if (fault_type & VM_PROT_WRITE) {
2827df8bae1dSRodney W. Grimes 			/*
28280d94caffSDavid Greenman 			 * Make a new object, and place it in the object
28290d94caffSDavid Greenman 			 * chain.  Note that no new references have appeared
2830ad5fca3bSAlan Cox 			 * -- one just moved from the map to the new
28310d94caffSDavid Greenman 			 * object.
2832df8bae1dSRodney W. Grimes 			 */
2833df8bae1dSRodney W. Grimes 
28349b09b6c7SMatthew Dillon 			if (vm_map_lock_upgrade(map))
2835df8bae1dSRodney W. Grimes 				goto RetryLookup;
28369b09b6c7SMatthew Dillon 
2837df8bae1dSRodney W. Grimes 			vm_object_shadow(
2838df8bae1dSRodney W. Grimes 			    &entry->object.vm_object,
2839df8bae1dSRodney W. Grimes 			    &entry->offset,
2840c2e11a03SJohn Dyson 			    atop(entry->end - entry->start));
2841df8bae1dSRodney W. Grimes 
2842afa07f7eSJohn Dyson 			entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
28439b09b6c7SMatthew Dillon 			vm_map_lock_downgrade(map);
28440d94caffSDavid Greenman 		} else {
2845df8bae1dSRodney W. Grimes 			/*
28460d94caffSDavid Greenman 			 * We're attempting to read a copy-on-write page --
28470d94caffSDavid Greenman 			 * don't allow writes.
2848df8bae1dSRodney W. Grimes 			 */
2849df8bae1dSRodney W. Grimes 
28502d8acc0fSJohn Dyson 			prot &= ~VM_PROT_WRITE;
2851df8bae1dSRodney W. Grimes 		}
2852df8bae1dSRodney W. Grimes 	}
28532d8acc0fSJohn Dyson 
2854df8bae1dSRodney W. Grimes 	/*
2855df8bae1dSRodney W. Grimes 	 * Create an object if necessary.
2856df8bae1dSRodney W. Grimes 	 */
28574e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL &&
28584e71e795SMatthew Dillon 	    !map->system_map) {
28599b09b6c7SMatthew Dillon 		if (vm_map_lock_upgrade(map))
2860df8bae1dSRodney W. Grimes 			goto RetryLookup;
28619b09b6c7SMatthew Dillon 
286224a1cce3SDavid Greenman 		entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
2863c2e11a03SJohn Dyson 		    atop(entry->end - entry->start));
2864df8bae1dSRodney W. Grimes 		entry->offset = 0;
28659b09b6c7SMatthew Dillon 		vm_map_lock_downgrade(map);
2866df8bae1dSRodney W. Grimes 	}
2867b5b40fa6SJohn Dyson 
2868df8bae1dSRodney W. Grimes 	/*
28690d94caffSDavid Greenman 	 * Return the object/offset from this entry.  If the entry was
28700d94caffSDavid Greenman 	 * copy-on-write or empty, it has been fixed up.
2871df8bae1dSRodney W. Grimes 	 */
2872df8bae1dSRodney W. Grimes 
28739b09b6c7SMatthew Dillon 	*pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
2874df8bae1dSRodney W. Grimes 	*object = entry->object.vm_object;
2875df8bae1dSRodney W. Grimes 
2876df8bae1dSRodney W. Grimes 	/*
2877df8bae1dSRodney W. Grimes 	 * Return whether this is the only map sharing this data.
2878df8bae1dSRodney W. Grimes 	 */
2879df8bae1dSRodney W. Grimes 
2880df8bae1dSRodney W. Grimes 	*out_prot = prot;
2881df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
2882df8bae1dSRodney W. Grimes 
2883df8bae1dSRodney W. Grimes #undef	RETURN
2884df8bae1dSRodney W. Grimes }
2885df8bae1dSRodney W. Grimes 
2886df8bae1dSRodney W. Grimes /*
2887df8bae1dSRodney W. Grimes  *	vm_map_lookup_done:
2888df8bae1dSRodney W. Grimes  *
2889df8bae1dSRodney W. Grimes  *	Releases locks acquired by a vm_map_lookup
2890df8bae1dSRodney W. Grimes  *	(according to the handle returned by that lookup).
2891df8bae1dSRodney W. Grimes  */
2892df8bae1dSRodney W. Grimes 
28930d94caffSDavid Greenman void
28941b40f8c0SMatthew Dillon vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry)
2895df8bae1dSRodney W. Grimes {
2896df8bae1dSRodney W. Grimes 	/*
2897df8bae1dSRodney W. Grimes 	 * Unlock the main-level map
2898df8bae1dSRodney W. Grimes 	 */
28990cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
2900df8bae1dSRodney W. Grimes 	vm_map_unlock_read(map);
2901df8bae1dSRodney W. Grimes }
2902df8bae1dSRodney W. Grimes 
29031efb74fbSJohn Dyson /*
29041efb74fbSJohn Dyson  * Implement uiomove with VM operations.  This handles (and collateral changes)
29051efb74fbSJohn Dyson  * support every combination of source object modification, and COW type
29061efb74fbSJohn Dyson  * operations.
29071efb74fbSJohn Dyson  */
29081efb74fbSJohn Dyson int
29091b40f8c0SMatthew Dillon vm_uiomove(
29101b40f8c0SMatthew Dillon 	vm_map_t mapa,
29111b40f8c0SMatthew Dillon 	vm_object_t srcobject,
29121b40f8c0SMatthew Dillon 	off_t cp,
29131b40f8c0SMatthew Dillon 	int cnta,
29141b40f8c0SMatthew Dillon 	vm_offset_t uaddra,
29151b40f8c0SMatthew Dillon 	int *npages)
29161efb74fbSJohn Dyson {
29171efb74fbSJohn Dyson 	vm_map_t map;
291847221757SJohn Dyson 	vm_object_t first_object, oldobject, object;
29192d8acc0fSJohn Dyson 	vm_map_entry_t entry;
29201efb74fbSJohn Dyson 	vm_prot_t prot;
29212d8acc0fSJohn Dyson 	boolean_t wired;
29221efb74fbSJohn Dyson 	int tcnt, rv;
29232d8acc0fSJohn Dyson 	vm_offset_t uaddr, start, end, tend;
29241efb74fbSJohn Dyson 	vm_pindex_t first_pindex, osize, oindex;
29251efb74fbSJohn Dyson 	off_t ooffset;
292647221757SJohn Dyson 	int cnt;
29271efb74fbSJohn Dyson 
29280cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
29290cddd8f0SMatthew Dillon 
293095e5e988SJohn Dyson 	if (npages)
293195e5e988SJohn Dyson 		*npages = 0;
293295e5e988SJohn Dyson 
293347221757SJohn Dyson 	cnt = cnta;
29342d8acc0fSJohn Dyson 	uaddr = uaddra;
29352d8acc0fSJohn Dyson 
29361efb74fbSJohn Dyson 	while (cnt > 0) {
29371efb74fbSJohn Dyson 		map = mapa;
29381efb74fbSJohn Dyson 
29391efb74fbSJohn Dyson 		if ((vm_map_lookup(&map, uaddr,
29402d8acc0fSJohn Dyson 			VM_PROT_READ, &entry, &first_object,
29412d8acc0fSJohn Dyson 			&first_pindex, &prot, &wired)) != KERN_SUCCESS) {
29421efb74fbSJohn Dyson 			return EFAULT;
29431efb74fbSJohn Dyson 		}
29441efb74fbSJohn Dyson 
29452d8acc0fSJohn Dyson 		vm_map_clip_start(map, entry, uaddr);
29461efb74fbSJohn Dyson 
29471efb74fbSJohn Dyson 		tcnt = cnt;
29482d8acc0fSJohn Dyson 		tend = uaddr + tcnt;
29492d8acc0fSJohn Dyson 		if (tend > entry->end) {
29502d8acc0fSJohn Dyson 			tcnt = entry->end - uaddr;
29512d8acc0fSJohn Dyson 			tend = entry->end;
29522d8acc0fSJohn Dyson 		}
29531efb74fbSJohn Dyson 
29542d8acc0fSJohn Dyson 		vm_map_clip_end(map, entry, tend);
29551efb74fbSJohn Dyson 
29562d8acc0fSJohn Dyson 		start = entry->start;
29572d8acc0fSJohn Dyson 		end = entry->end;
29581efb74fbSJohn Dyson 
2959c2e11a03SJohn Dyson 		osize = atop(tcnt);
296095e5e988SJohn Dyson 
2961925a3a41SJohn Dyson 		oindex = OFF_TO_IDX(cp);
296295e5e988SJohn Dyson 		if (npages) {
2963925a3a41SJohn Dyson 			vm_pindex_t idx;
296495e5e988SJohn Dyson 			for (idx = 0; idx < osize; idx++) {
296595e5e988SJohn Dyson 				vm_page_t m;
2966925a3a41SJohn Dyson 				if ((m = vm_page_lookup(srcobject, oindex + idx)) == NULL) {
29672d8acc0fSJohn Dyson 					vm_map_lookup_done(map, entry);
296895e5e988SJohn Dyson 					return 0;
296995e5e988SJohn Dyson 				}
29701c7c3c6aSMatthew Dillon 				/*
29711c7c3c6aSMatthew Dillon 				 * disallow busy or invalid pages, but allow
29721c7c3c6aSMatthew Dillon 				 * m->busy pages if they are entirely valid.
29731c7c3c6aSMatthew Dillon 				 */
2974925a3a41SJohn Dyson 				if ((m->flags & PG_BUSY) ||
297595e5e988SJohn Dyson 					((m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL)) {
29762d8acc0fSJohn Dyson 					vm_map_lookup_done(map, entry);
297795e5e988SJohn Dyson 					return 0;
297895e5e988SJohn Dyson 				}
297995e5e988SJohn Dyson 			}
298095e5e988SJohn Dyson 		}
298195e5e988SJohn Dyson 
29821efb74fbSJohn Dyson /*
29831efb74fbSJohn Dyson  * If we are changing an existing map entry, just redirect
29841efb74fbSJohn Dyson  * the object, and change mappings.
29851efb74fbSJohn Dyson  */
29862d8acc0fSJohn Dyson 		if ((first_object->type == OBJT_VNODE) &&
29872d8acc0fSJohn Dyson 			((oldobject = entry->object.vm_object) == first_object)) {
29882d8acc0fSJohn Dyson 
29892d8acc0fSJohn Dyson 			if ((entry->offset != cp) || (oldobject != srcobject)) {
29902d8acc0fSJohn Dyson 				/*
29912d8acc0fSJohn Dyson    				* Remove old window into the file
29922d8acc0fSJohn Dyson    				*/
29932d8acc0fSJohn Dyson 				pmap_remove (map->pmap, uaddr, tend);
29942d8acc0fSJohn Dyson 
29952d8acc0fSJohn Dyson 				/*
29962d8acc0fSJohn Dyson    				* Force copy on write for mmaped regions
29972d8acc0fSJohn Dyson    				*/
29982d8acc0fSJohn Dyson 				vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
29992d8acc0fSJohn Dyson 
30002d8acc0fSJohn Dyson 				/*
30012d8acc0fSJohn Dyson    				* Point the object appropriately
30022d8acc0fSJohn Dyson    				*/
30032d8acc0fSJohn Dyson 				if (oldobject != srcobject) {
30042d8acc0fSJohn Dyson 
30052d8acc0fSJohn Dyson 				/*
30062d8acc0fSJohn Dyson    				* Set the object optimization hint flag
30072d8acc0fSJohn Dyson    				*/
3008069e9bc1SDoug Rabson 					vm_object_set_flag(srcobject, OBJ_OPT);
30092d8acc0fSJohn Dyson 					vm_object_reference(srcobject);
30102d8acc0fSJohn Dyson 					entry->object.vm_object = srcobject;
30112d8acc0fSJohn Dyson 
30122d8acc0fSJohn Dyson 					if (oldobject) {
30132d8acc0fSJohn Dyson 						vm_object_deallocate(oldobject);
30142d8acc0fSJohn Dyson 					}
30152d8acc0fSJohn Dyson 				}
30162d8acc0fSJohn Dyson 
30172d8acc0fSJohn Dyson 				entry->offset = cp;
30182d8acc0fSJohn Dyson 				map->timestamp++;
30192d8acc0fSJohn Dyson 			} else {
30202d8acc0fSJohn Dyson 				pmap_remove (map->pmap, uaddr, tend);
30212d8acc0fSJohn Dyson 			}
30222d8acc0fSJohn Dyson 
30232d8acc0fSJohn Dyson 		} else if ((first_object->ref_count == 1) &&
3024925a3a41SJohn Dyson 			(first_object->size == osize) &&
302547221757SJohn Dyson 			((first_object->type == OBJT_DEFAULT) ||
302647221757SJohn Dyson 				(first_object->type == OBJT_SWAP)) ) {
3027925a3a41SJohn Dyson 
3028925a3a41SJohn Dyson 			oldobject = first_object->backing_object;
3029925a3a41SJohn Dyson 
3030925a3a41SJohn Dyson 			if ((first_object->backing_object_offset != cp) ||
3031925a3a41SJohn Dyson 				(oldobject != srcobject)) {
3032925a3a41SJohn Dyson 				/*
3033925a3a41SJohn Dyson    				* Remove old window into the file
3034925a3a41SJohn Dyson    				*/
30352d8acc0fSJohn Dyson 				pmap_remove (map->pmap, uaddr, tend);
3036925a3a41SJohn Dyson 
3037925a3a41SJohn Dyson 				/*
303847221757SJohn Dyson 				 * Remove unneeded old pages
303947221757SJohn Dyson 				 */
304047221757SJohn Dyson 				vm_object_page_remove(first_object, 0, 0, 0);
304147221757SJohn Dyson 
304247221757SJohn Dyson 				/*
304347221757SJohn Dyson 				 * Invalidate swap space
304447221757SJohn Dyson 				 */
304547221757SJohn Dyson 				if (first_object->type == OBJT_SWAP) {
304647221757SJohn Dyson 					swap_pager_freespace(first_object,
30471c7c3c6aSMatthew Dillon 						0,
304847221757SJohn Dyson 						first_object->size);
304947221757SJohn Dyson 				}
305047221757SJohn Dyson 
305147221757SJohn Dyson 				/*
3052925a3a41SJohn Dyson    				* Force copy on write for mmaped regions
3053925a3a41SJohn Dyson    				*/
305447221757SJohn Dyson 				vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
30551efb74fbSJohn Dyson 
30561efb74fbSJohn Dyson 				/*
30571efb74fbSJohn Dyson    				* Point the object appropriately
30581efb74fbSJohn Dyson    				*/
3059925a3a41SJohn Dyson 				if (oldobject != srcobject) {
306047221757SJohn Dyson 
3061925a3a41SJohn Dyson 				/*
3062925a3a41SJohn Dyson    				* Set the object optimization hint flag
3063925a3a41SJohn Dyson    				*/
3064069e9bc1SDoug Rabson 					vm_object_set_flag(srcobject, OBJ_OPT);
3065925a3a41SJohn Dyson 					vm_object_reference(srcobject);
3066925a3a41SJohn Dyson 
3067925a3a41SJohn Dyson 					if (oldobject) {
3068925a3a41SJohn Dyson 						TAILQ_REMOVE(&oldobject->shadow_head,
3069925a3a41SJohn Dyson 							first_object, shadow_list);
3070925a3a41SJohn Dyson 						oldobject->shadow_count--;
3071b4309055SMatthew Dillon 						/* XXX bump generation? */
3072925a3a41SJohn Dyson 						vm_object_deallocate(oldobject);
3073925a3a41SJohn Dyson 					}
3074925a3a41SJohn Dyson 
3075925a3a41SJohn Dyson 					TAILQ_INSERT_TAIL(&srcobject->shadow_head,
3076925a3a41SJohn Dyson 						first_object, shadow_list);
3077925a3a41SJohn Dyson 					srcobject->shadow_count++;
3078b4309055SMatthew Dillon 					/* XXX bump generation? */
3079925a3a41SJohn Dyson 
3080925a3a41SJohn Dyson 					first_object->backing_object = srcobject;
3081925a3a41SJohn Dyson 				}
30821efb74fbSJohn Dyson 				first_object->backing_object_offset = cp;
30832d8acc0fSJohn Dyson 				map->timestamp++;
3084925a3a41SJohn Dyson 			} else {
30852d8acc0fSJohn Dyson 				pmap_remove (map->pmap, uaddr, tend);
3086925a3a41SJohn Dyson 			}
30871efb74fbSJohn Dyson /*
30881efb74fbSJohn Dyson  * Otherwise, we have to do a logical mmap.
30891efb74fbSJohn Dyson  */
30901efb74fbSJohn Dyson 		} else {
30911efb74fbSJohn Dyson 
3092069e9bc1SDoug Rabson 			vm_object_set_flag(srcobject, OBJ_OPT);
3093925a3a41SJohn Dyson 			vm_object_reference(srcobject);
30941efb74fbSJohn Dyson 
30952d8acc0fSJohn Dyson 			pmap_remove (map->pmap, uaddr, tend);
30961efb74fbSJohn Dyson 
309747221757SJohn Dyson 			vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
309847221757SJohn Dyson 			vm_map_lock_upgrade(map);
30991efb74fbSJohn Dyson 
31002d8acc0fSJohn Dyson 			if (entry == &map->header) {
31011efb74fbSJohn Dyson 				map->first_free = &map->header;
31021efb74fbSJohn Dyson 			} else if (map->first_free->start >= start) {
31032d8acc0fSJohn Dyson 				map->first_free = entry->prev;
31041efb74fbSJohn Dyson 			}
31051efb74fbSJohn Dyson 
31062d8acc0fSJohn Dyson 			SAVE_HINT(map, entry->prev);
31072d8acc0fSJohn Dyson 			vm_map_entry_delete(map, entry);
31081efb74fbSJohn Dyson 
31092d8acc0fSJohn Dyson 			object = srcobject;
31102d8acc0fSJohn Dyson 			ooffset = cp;
31112d8acc0fSJohn Dyson 
31122d8acc0fSJohn Dyson 			rv = vm_map_insert(map, object, ooffset, start, tend,
3113e5f13bddSAlan Cox 				VM_PROT_ALL, VM_PROT_ALL, MAP_COPY_ON_WRITE);
31141efb74fbSJohn Dyson 
31151efb74fbSJohn Dyson 			if (rv != KERN_SUCCESS)
31161efb74fbSJohn Dyson 				panic("vm_uiomove: could not insert new entry: %d", rv);
31171efb74fbSJohn Dyson 		}
31181efb74fbSJohn Dyson 
31191efb74fbSJohn Dyson /*
31201efb74fbSJohn Dyson  * Map the window directly, if it is already in memory
31211efb74fbSJohn Dyson  */
31222d8acc0fSJohn Dyson 		pmap_object_init_pt(map->pmap, uaddr,
31232d8acc0fSJohn Dyson 			srcobject, oindex, tcnt, 0);
31241efb74fbSJohn Dyson 
312547221757SJohn Dyson 		map->timestamp++;
31261efb74fbSJohn Dyson 		vm_map_unlock(map);
31271efb74fbSJohn Dyson 
31281efb74fbSJohn Dyson 		cnt -= tcnt;
31292d8acc0fSJohn Dyson 		uaddr += tcnt;
31301efb74fbSJohn Dyson 		cp += tcnt;
313195e5e988SJohn Dyson 		if (npages)
313295e5e988SJohn Dyson 			*npages += osize;
31331efb74fbSJohn Dyson 	}
31341efb74fbSJohn Dyson 	return 0;
31351efb74fbSJohn Dyson }
31361efb74fbSJohn Dyson 
31371efb74fbSJohn Dyson /*
31381efb74fbSJohn Dyson  * Performs the copy_on_write operations necessary to allow the virtual copies
31391efb74fbSJohn Dyson  * into user space to work.  This has to be called for write(2) system calls
31401efb74fbSJohn Dyson  * from other processes, file unlinking, and file size shrinkage.
31411efb74fbSJohn Dyson  */
31421efb74fbSJohn Dyson void
31431b40f8c0SMatthew Dillon vm_freeze_copyopts(vm_object_t object, vm_pindex_t froma, vm_pindex_t toa)
31441efb74fbSJohn Dyson {
3145f5ef029eSPoul-Henning Kamp 	int rv;
3146f5ef029eSPoul-Henning Kamp 	vm_object_t robject;
3147f5ef029eSPoul-Henning Kamp 	vm_pindex_t idx;
31481efb74fbSJohn Dyson 
31490cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
31502d8acc0fSJohn Dyson 	if ((object == NULL) ||
315195e5e988SJohn Dyson 		((object->flags & OBJ_OPT) == 0))
315295e5e988SJohn Dyson 		return;
31531efb74fbSJohn Dyson 
31541efb74fbSJohn Dyson 	if (object->shadow_count > object->ref_count)
31551efb74fbSJohn Dyson 		panic("vm_freeze_copyopts: sc > rc");
31561efb74fbSJohn Dyson 
31578aef1712SMatthew Dillon 	while((robject = TAILQ_FIRST(&object->shadow_head)) != NULL) {
31581efb74fbSJohn Dyson 		vm_pindex_t bo_pindex;
31591efb74fbSJohn Dyson 		vm_page_t m_in, m_out;
31601efb74fbSJohn Dyson 
31611efb74fbSJohn Dyson 		bo_pindex = OFF_TO_IDX(robject->backing_object_offset);
31621efb74fbSJohn Dyson 
316395e5e988SJohn Dyson 		vm_object_reference(robject);
3164925a3a41SJohn Dyson 
316566095752SJohn Dyson 		vm_object_pip_wait(robject, "objfrz");
3166925a3a41SJohn Dyson 
31671efb74fbSJohn Dyson 		if (robject->ref_count == 1) {
31681efb74fbSJohn Dyson 			vm_object_deallocate(robject);
31691efb74fbSJohn Dyson 			continue;
31701efb74fbSJohn Dyson 		}
31711efb74fbSJohn Dyson 
3172d474eaaaSDoug Rabson 		vm_object_pip_add(robject, 1);
31731efb74fbSJohn Dyson 
317447221757SJohn Dyson 		for (idx = 0; idx < robject->size; idx++) {
31751efb74fbSJohn Dyson 
317695461b45SJohn Dyson 			m_out = vm_page_grab(robject, idx,
317795461b45SJohn Dyson 						VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
31781efb74fbSJohn Dyson 
31791efb74fbSJohn Dyson 			if (m_out->valid == 0) {
318095461b45SJohn Dyson 				m_in = vm_page_grab(object, bo_pindex + idx,
318195461b45SJohn Dyson 						VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
318295461b45SJohn Dyson 				if (m_in->valid == 0) {
318347221757SJohn Dyson 					rv = vm_pager_get_pages(object, &m_in, 1, 0);
318447221757SJohn Dyson 					if (rv != VM_PAGER_OK) {
31853efc015bSPeter Wemm 						printf("vm_freeze_copyopts: cannot read page from file: %lx\n", (long)m_in->pindex);
318647221757SJohn Dyson 						continue;
31871efb74fbSJohn Dyson 					}
318895461b45SJohn Dyson 					vm_page_deactivate(m_in);
318947221757SJohn Dyson 				}
319047221757SJohn Dyson 
319147221757SJohn Dyson 				vm_page_protect(m_in, VM_PROT_NONE);
319247221757SJohn Dyson 				pmap_copy_page(VM_PAGE_TO_PHYS(m_in), VM_PAGE_TO_PHYS(m_out));
319395461b45SJohn Dyson 				m_out->valid = m_in->valid;
31947dbf82dcSMatthew Dillon 				vm_page_dirty(m_out);
319595461b45SJohn Dyson 				vm_page_activate(m_out);
3196e69763a3SDoug Rabson 				vm_page_wakeup(m_in);
31971efb74fbSJohn Dyson 			}
3198e69763a3SDoug Rabson 			vm_page_wakeup(m_out);
319947221757SJohn Dyson 		}
3200925a3a41SJohn Dyson 
32011efb74fbSJohn Dyson 		object->shadow_count--;
320247221757SJohn Dyson 		object->ref_count--;
32031efb74fbSJohn Dyson 		TAILQ_REMOVE(&object->shadow_head, robject, shadow_list);
32041efb74fbSJohn Dyson 		robject->backing_object = NULL;
32051efb74fbSJohn Dyson 		robject->backing_object_offset = 0;
32061efb74fbSJohn Dyson 
320747221757SJohn Dyson 		vm_object_pip_wakeup(robject);
32081efb74fbSJohn Dyson 		vm_object_deallocate(robject);
32091efb74fbSJohn Dyson 	}
321047221757SJohn Dyson 
3211069e9bc1SDoug Rabson 	vm_object_clear_flag(object, OBJ_OPT);
32121efb74fbSJohn Dyson }
32131efb74fbSJohn Dyson 
3214c7c34a24SBruce Evans #include "opt_ddb.h"
3215c3cb3e12SDavid Greenman #ifdef DDB
3216c7c34a24SBruce Evans #include <sys/kernel.h>
3217c7c34a24SBruce Evans 
3218c7c34a24SBruce Evans #include <ddb/ddb.h>
3219c7c34a24SBruce Evans 
3220df8bae1dSRodney W. Grimes /*
3221df8bae1dSRodney W. Grimes  *	vm_map_print:	[ debug ]
3222df8bae1dSRodney W. Grimes  */
3223c7c34a24SBruce Evans DB_SHOW_COMMAND(map, vm_map_print)
3224df8bae1dSRodney W. Grimes {
322595e5e988SJohn Dyson 	static int nlines;
3226c7c34a24SBruce Evans 	/* XXX convert args. */
3227c0877f10SJohn Dyson 	vm_map_t map = (vm_map_t)addr;
3228c7c34a24SBruce Evans 	boolean_t full = have_addr;
3229df8bae1dSRodney W. Grimes 
3230c0877f10SJohn Dyson 	vm_map_entry_t entry;
3231c7c34a24SBruce Evans 
3232e5f251d2SAlan Cox 	db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
3233e5f251d2SAlan Cox 	    (void *)map,
3234101eeb7fSBruce Evans 	    (void *)map->pmap, map->nentries, map->timestamp);
323595e5e988SJohn Dyson 	nlines++;
3236df8bae1dSRodney W. Grimes 
3237c7c34a24SBruce Evans 	if (!full && db_indent)
3238df8bae1dSRodney W. Grimes 		return;
3239df8bae1dSRodney W. Grimes 
3240c7c34a24SBruce Evans 	db_indent += 2;
3241df8bae1dSRodney W. Grimes 	for (entry = map->header.next; entry != &map->header;
3242df8bae1dSRodney W. Grimes 	    entry = entry->next) {
3243fc62ef1fSBruce Evans 		db_iprintf("map entry %p: start=%p, end=%p\n",
3244fc62ef1fSBruce Evans 		    (void *)entry, (void *)entry->start, (void *)entry->end);
324595e5e988SJohn Dyson 		nlines++;
3246e5f251d2SAlan Cox 		{
3247df8bae1dSRodney W. Grimes 			static char *inheritance_name[4] =
3248df8bae1dSRodney W. Grimes 			{"share", "copy", "none", "donate_copy"};
32490d94caffSDavid Greenman 
325095e5e988SJohn Dyson 			db_iprintf(" prot=%x/%x/%s",
3251df8bae1dSRodney W. Grimes 			    entry->protection,
3252df8bae1dSRodney W. Grimes 			    entry->max_protection,
32538aef1712SMatthew Dillon 			    inheritance_name[(int)(unsigned char)entry->inheritance]);
3254df8bae1dSRodney W. Grimes 			if (entry->wired_count != 0)
325595e5e988SJohn Dyson 				db_printf(", wired");
3256df8bae1dSRodney W. Grimes 		}
32579fdfe602SMatthew Dillon 		if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3258101eeb7fSBruce Evans 			/* XXX no %qd in kernel.  Truncate entry->offset. */
3259101eeb7fSBruce Evans 			db_printf(", share=%p, offset=0x%lx\n",
32609fdfe602SMatthew Dillon 			    (void *)entry->object.sub_map,
3261ecbb00a2SDoug Rabson 			    (long)entry->offset);
326295e5e988SJohn Dyson 			nlines++;
3263df8bae1dSRodney W. Grimes 			if ((entry->prev == &map->header) ||
32649fdfe602SMatthew Dillon 			    (entry->prev->object.sub_map !=
32659fdfe602SMatthew Dillon 				entry->object.sub_map)) {
3266c7c34a24SBruce Evans 				db_indent += 2;
3267101eeb7fSBruce Evans 				vm_map_print((db_expr_t)(intptr_t)
32689fdfe602SMatthew Dillon 					     entry->object.sub_map,
3269914181e7SBruce Evans 					     full, 0, (char *)0);
3270c7c34a24SBruce Evans 				db_indent -= 2;
3271df8bae1dSRodney W. Grimes 			}
32720d94caffSDavid Greenman 		} else {
3273101eeb7fSBruce Evans 			/* XXX no %qd in kernel.  Truncate entry->offset. */
3274101eeb7fSBruce Evans 			db_printf(", object=%p, offset=0x%lx",
3275101eeb7fSBruce Evans 			    (void *)entry->object.vm_object,
3276ecbb00a2SDoug Rabson 			    (long)entry->offset);
3277afa07f7eSJohn Dyson 			if (entry->eflags & MAP_ENTRY_COW)
3278c7c34a24SBruce Evans 				db_printf(", copy (%s)",
3279afa07f7eSJohn Dyson 				    (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
3280c7c34a24SBruce Evans 			db_printf("\n");
328195e5e988SJohn Dyson 			nlines++;
3282df8bae1dSRodney W. Grimes 
3283df8bae1dSRodney W. Grimes 			if ((entry->prev == &map->header) ||
3284df8bae1dSRodney W. Grimes 			    (entry->prev->object.vm_object !=
3285df8bae1dSRodney W. Grimes 				entry->object.vm_object)) {
3286c7c34a24SBruce Evans 				db_indent += 2;
3287101eeb7fSBruce Evans 				vm_object_print((db_expr_t)(intptr_t)
3288101eeb7fSBruce Evans 						entry->object.vm_object,
3289914181e7SBruce Evans 						full, 0, (char *)0);
329095e5e988SJohn Dyson 				nlines += 4;
3291c7c34a24SBruce Evans 				db_indent -= 2;
3292df8bae1dSRodney W. Grimes 			}
3293df8bae1dSRodney W. Grimes 		}
3294df8bae1dSRodney W. Grimes 	}
3295c7c34a24SBruce Evans 	db_indent -= 2;
329695e5e988SJohn Dyson 	if (db_indent == 0)
329795e5e988SJohn Dyson 		nlines = 0;
3298df8bae1dSRodney W. Grimes }
329995e5e988SJohn Dyson 
330095e5e988SJohn Dyson 
330195e5e988SJohn Dyson DB_SHOW_COMMAND(procvm, procvm)
330295e5e988SJohn Dyson {
330395e5e988SJohn Dyson 	struct proc *p;
330495e5e988SJohn Dyson 
330595e5e988SJohn Dyson 	if (have_addr) {
330695e5e988SJohn Dyson 		p = (struct proc *) addr;
330795e5e988SJohn Dyson 	} else {
330895e5e988SJohn Dyson 		p = curproc;
330995e5e988SJohn Dyson 	}
331095e5e988SJohn Dyson 
3311ac1e407bSBruce Evans 	db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
3312ac1e407bSBruce Evans 	    (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
3313b1028ad1SLuoqi Chen 	    (void *)vmspace_pmap(p->p_vmspace));
331495e5e988SJohn Dyson 
3315101eeb7fSBruce Evans 	vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);
331695e5e988SJohn Dyson }
331795e5e988SJohn Dyson 
3318c7c34a24SBruce Evans #endif /* DDB */
3319