xref: /freebsd/sys/vm/vm_map.c (revision e302698320af97d3fb66f92986d904d2b0f4db24)
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;
175df8bae1dSRodney W. Grimes 	return (vm);
176df8bae1dSRodney W. Grimes }
177df8bae1dSRodney W. Grimes 
178df8bae1dSRodney W. Grimes void
1791b40f8c0SMatthew Dillon vm_init2(void)
1801b40f8c0SMatthew Dillon {
1810d65e566SJohn Dyson 	zinitna(kmapentzone, &kmapentobj,
1820a80f406SJohn Dyson 		NULL, 0, cnt.v_page_count / 4, ZONE_INTERRUPT, 1);
1830d65e566SJohn Dyson 	zinitna(mapentzone, &mapentobj,
1840a80f406SJohn Dyson 		NULL, 0, 0, 0, 1);
1850d65e566SJohn Dyson 	zinitna(mapzone, &mapobj,
1860a80f406SJohn Dyson 		NULL, 0, 0, 0, 1);
1872d8acc0fSJohn Dyson 	vmspace_zone = zinit("VMSPACE", sizeof (struct vmspace), 0, 0, 3);
188ba9be04cSJohn Dyson 	pmap_init2();
18999448ed1SJohn Dyson 	vm_object_init2();
1903075778bSJohn Dyson }
1913075778bSJohn Dyson 
1923075778bSJohn Dyson void
1931b40f8c0SMatthew Dillon vmspace_free(struct vmspace *vm)
194df8bae1dSRodney W. Grimes {
1950cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
196df8bae1dSRodney W. Grimes 
197a1f6d91cSDavid Greenman 	if (vm->vm_refcnt == 0)
198a1f6d91cSDavid Greenman 		panic("vmspace_free: attempt to free already freed vmspace");
199a1f6d91cSDavid Greenman 
200df8bae1dSRodney W. Grimes 	if (--vm->vm_refcnt == 0) {
201bd7e5f99SJohn Dyson 
20221c641b2SJohn Baldwin 		CTR1(KTR_VM, "vmspace_free: %p", vm);
20330dcfc09SJohn Dyson 		/*
204df8bae1dSRodney W. Grimes 		 * Lock the map, to wait out all other references to it.
2050d94caffSDavid Greenman 		 * Delete all of the mappings and pages they hold, then call
2060d94caffSDavid Greenman 		 * the pmap module to reclaim anything left.
207df8bae1dSRodney W. Grimes 		 */
208df8bae1dSRodney W. Grimes 		vm_map_lock(&vm->vm_map);
209df8bae1dSRodney W. Grimes 		(void) vm_map_delete(&vm->vm_map, vm->vm_map.min_offset,
210df8bae1dSRodney W. Grimes 		    vm->vm_map.max_offset);
211a1f6d91cSDavid Greenman 		vm_map_unlock(&vm->vm_map);
212b18bfc3dSJohn Dyson 
213b1028ad1SLuoqi Chen 		pmap_release(vmspace_pmap(vm));
214a18b1f1dSJason Evans 		vm_map_destroy(&vm->vm_map);
2152d8acc0fSJohn Dyson 		zfree(vmspace_zone, vm);
216df8bae1dSRodney W. Grimes 	}
217df8bae1dSRodney W. Grimes }
218df8bae1dSRodney W. Grimes 
219df8bae1dSRodney W. Grimes /*
220ff2b5645SMatthew Dillon  * vmspace_swap_count() - count the approximate swap useage in pages for a
221ff2b5645SMatthew Dillon  *			  vmspace.
222ff2b5645SMatthew Dillon  *
223ff2b5645SMatthew Dillon  *	Swap useage is determined by taking the proportional swap used by
224ff2b5645SMatthew Dillon  *	VM objects backing the VM map.  To make up for fractional losses,
225ff2b5645SMatthew Dillon  *	if the VM object has any swap use at all the associated map entries
226ff2b5645SMatthew Dillon  *	count for at least 1 swap page.
227ff2b5645SMatthew Dillon  */
228ff2b5645SMatthew Dillon int
229ff2b5645SMatthew Dillon vmspace_swap_count(struct vmspace *vmspace)
230ff2b5645SMatthew Dillon {
231ff2b5645SMatthew Dillon 	vm_map_t map = &vmspace->vm_map;
232ff2b5645SMatthew Dillon 	vm_map_entry_t cur;
233ff2b5645SMatthew Dillon 	int count = 0;
234ff2b5645SMatthew Dillon 
235ff2b5645SMatthew Dillon 	for (cur = map->header.next; cur != &map->header; cur = cur->next) {
236ff2b5645SMatthew Dillon 		vm_object_t object;
237ff2b5645SMatthew Dillon 
238ff2b5645SMatthew Dillon 		if ((cur->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
239ff2b5645SMatthew Dillon 		    (object = cur->object.vm_object) != NULL &&
240ff2b5645SMatthew Dillon 		    object->type == OBJT_SWAP
241ff2b5645SMatthew Dillon 		) {
242ff2b5645SMatthew Dillon 			int n = (cur->end - cur->start) / PAGE_SIZE;
243ff2b5645SMatthew Dillon 
244ff2b5645SMatthew Dillon 			if (object->un_pager.swp.swp_bcount) {
245ef6a93efSMatthew Dillon 				count += object->un_pager.swp.swp_bcount *
246ef6a93efSMatthew Dillon 				    SWAP_META_PAGES * n / object->size + 1;
247ff2b5645SMatthew Dillon 			}
248ff2b5645SMatthew Dillon 		}
249ff2b5645SMatthew Dillon 	}
250ff2b5645SMatthew Dillon 	return(count);
251ff2b5645SMatthew Dillon }
252ff2b5645SMatthew Dillon 
2531b40f8c0SMatthew Dillon u_char
2541b40f8c0SMatthew Dillon vm_map_entry_behavior(struct vm_map_entry *entry)
2551b40f8c0SMatthew Dillon {
2561b40f8c0SMatthew Dillon 	return entry->eflags & MAP_ENTRY_BEHAV_MASK;
2571b40f8c0SMatthew Dillon }
2581b40f8c0SMatthew Dillon 
2591b40f8c0SMatthew Dillon void
2601b40f8c0SMatthew Dillon vm_map_entry_set_behavior(struct vm_map_entry *entry, u_char behavior)
2611b40f8c0SMatthew Dillon {
2621b40f8c0SMatthew Dillon 	entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) |
2631b40f8c0SMatthew Dillon 		(behavior & MAP_ENTRY_BEHAV_MASK);
2641b40f8c0SMatthew Dillon }
2651b40f8c0SMatthew Dillon 
2661b40f8c0SMatthew Dillon void
2671b40f8c0SMatthew Dillon vm_map_lock(vm_map_t map)
2681b40f8c0SMatthew Dillon {
2691b40f8c0SMatthew Dillon 	vm_map_printf("locking map LK_EXCLUSIVE: %p\n", map);
270b40ce416SJulian Elischer 	if (lockmgr(&map->lock, LK_EXCLUSIVE, NULL, curthread) != 0)
2711b40f8c0SMatthew Dillon 		panic("vm_map_lock: failed to get lock");
2721b40f8c0SMatthew Dillon 	map->timestamp++;
2731b40f8c0SMatthew Dillon }
2741b40f8c0SMatthew Dillon 
2751b40f8c0SMatthew Dillon void
2761b40f8c0SMatthew Dillon vm_map_unlock(vm_map_t map)
2771b40f8c0SMatthew Dillon {
2781b40f8c0SMatthew Dillon 	vm_map_printf("locking map LK_RELEASE: %p\n", map);
279b40ce416SJulian Elischer 	lockmgr(&(map)->lock, LK_RELEASE, NULL, curthread);
2801b40f8c0SMatthew Dillon }
2811b40f8c0SMatthew Dillon 
2821b40f8c0SMatthew Dillon void
2831b40f8c0SMatthew Dillon vm_map_lock_read(vm_map_t map)
2841b40f8c0SMatthew Dillon {
2851b40f8c0SMatthew Dillon 	vm_map_printf("locking map LK_SHARED: %p\n", map);
286b40ce416SJulian Elischer 	lockmgr(&(map)->lock, LK_SHARED, NULL, curthread);
2871b40f8c0SMatthew Dillon }
2881b40f8c0SMatthew Dillon 
2891b40f8c0SMatthew Dillon void
2901b40f8c0SMatthew Dillon vm_map_unlock_read(vm_map_t map)
2911b40f8c0SMatthew Dillon {
2921b40f8c0SMatthew Dillon 	vm_map_printf("locking map LK_RELEASE: %p\n", map);
293b40ce416SJulian Elischer 	lockmgr(&(map)->lock, LK_RELEASE, NULL, curthread);
2941b40f8c0SMatthew Dillon }
2951b40f8c0SMatthew Dillon 
2961b40f8c0SMatthew Dillon static __inline__ int
297b40ce416SJulian Elischer _vm_map_lock_upgrade(vm_map_t map, struct thread *td) {
2981b40f8c0SMatthew Dillon 	int error;
2991b40f8c0SMatthew Dillon 
3001b40f8c0SMatthew Dillon 	vm_map_printf("locking map LK_EXCLUPGRADE: %p\n", map);
301b40ce416SJulian Elischer 	error = lockmgr(&map->lock, LK_EXCLUPGRADE, NULL, td);
3021b40f8c0SMatthew Dillon 	if (error == 0)
3031b40f8c0SMatthew Dillon 		map->timestamp++;
3041b40f8c0SMatthew Dillon 	return error;
3051b40f8c0SMatthew Dillon }
3061b40f8c0SMatthew Dillon 
3071b40f8c0SMatthew Dillon int
3081b40f8c0SMatthew Dillon vm_map_lock_upgrade(vm_map_t map)
3091b40f8c0SMatthew Dillon {
310b40ce416SJulian Elischer     return(_vm_map_lock_upgrade(map, curthread));
3111b40f8c0SMatthew Dillon }
3121b40f8c0SMatthew Dillon 
3131b40f8c0SMatthew Dillon void
3141b40f8c0SMatthew Dillon vm_map_lock_downgrade(vm_map_t map)
3151b40f8c0SMatthew Dillon {
3161b40f8c0SMatthew Dillon 	vm_map_printf("locking map LK_DOWNGRADE: %p\n", map);
317b40ce416SJulian Elischer 	lockmgr(&map->lock, LK_DOWNGRADE, NULL, curthread);
3181b40f8c0SMatthew Dillon }
3191b40f8c0SMatthew Dillon 
3201b40f8c0SMatthew Dillon void
3211b40f8c0SMatthew Dillon vm_map_set_recursive(vm_map_t map)
3221b40f8c0SMatthew Dillon {
3231b40f8c0SMatthew Dillon 	mtx_lock((map)->lock.lk_interlock);
3241b40f8c0SMatthew Dillon 	map->lock.lk_flags |= LK_CANRECURSE;
3251b40f8c0SMatthew Dillon 	mtx_unlock((map)->lock.lk_interlock);
3261b40f8c0SMatthew Dillon }
3271b40f8c0SMatthew Dillon 
3281b40f8c0SMatthew Dillon void
3291b40f8c0SMatthew Dillon vm_map_clear_recursive(vm_map_t map)
3301b40f8c0SMatthew Dillon {
3311b40f8c0SMatthew Dillon 	mtx_lock((map)->lock.lk_interlock);
3321b40f8c0SMatthew Dillon 	map->lock.lk_flags &= ~LK_CANRECURSE;
3331b40f8c0SMatthew Dillon 	mtx_unlock((map)->lock.lk_interlock);
3341b40f8c0SMatthew Dillon }
3351b40f8c0SMatthew Dillon 
3361b40f8c0SMatthew Dillon vm_offset_t
3371b40f8c0SMatthew Dillon vm_map_min(vm_map_t map)
3381b40f8c0SMatthew Dillon {
3391b40f8c0SMatthew Dillon 	return(map->min_offset);
3401b40f8c0SMatthew Dillon }
3411b40f8c0SMatthew Dillon 
3421b40f8c0SMatthew Dillon vm_offset_t
3431b40f8c0SMatthew Dillon vm_map_max(vm_map_t map)
3441b40f8c0SMatthew Dillon {
3451b40f8c0SMatthew Dillon 	return(map->max_offset);
3461b40f8c0SMatthew Dillon }
3471b40f8c0SMatthew Dillon 
3481b40f8c0SMatthew Dillon struct pmap *
3491b40f8c0SMatthew Dillon vm_map_pmap(vm_map_t map)
3501b40f8c0SMatthew Dillon {
3511b40f8c0SMatthew Dillon 	return(map->pmap);
3521b40f8c0SMatthew Dillon }
3531b40f8c0SMatthew Dillon 
3541b40f8c0SMatthew Dillon struct pmap *
3551b40f8c0SMatthew Dillon vmspace_pmap(struct vmspace *vmspace)
3561b40f8c0SMatthew Dillon {
3571b40f8c0SMatthew Dillon 	return &vmspace->vm_pmap;
3581b40f8c0SMatthew Dillon }
3591b40f8c0SMatthew Dillon 
3601b40f8c0SMatthew Dillon long
3611b40f8c0SMatthew Dillon vmspace_resident_count(struct vmspace *vmspace)
3621b40f8c0SMatthew Dillon {
3631b40f8c0SMatthew Dillon 	return pmap_resident_count(vmspace_pmap(vmspace));
3641b40f8c0SMatthew Dillon }
3651b40f8c0SMatthew Dillon 
366ff2b5645SMatthew Dillon /*
367df8bae1dSRodney W. Grimes  *	vm_map_create:
368df8bae1dSRodney W. Grimes  *
369df8bae1dSRodney W. Grimes  *	Creates and returns a new empty VM map with
370df8bae1dSRodney W. Grimes  *	the given physical map structure, and having
371df8bae1dSRodney W. Grimes  *	the given lower and upper address bounds.
372df8bae1dSRodney W. Grimes  */
3730d94caffSDavid Greenman vm_map_t
3741b40f8c0SMatthew Dillon vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max)
375df8bae1dSRodney W. Grimes {
376c0877f10SJohn Dyson 	vm_map_t result;
377df8bae1dSRodney W. Grimes 
3780cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
3790cddd8f0SMatthew Dillon 
3803075778bSJohn Dyson 	result = zalloc(mapzone);
38121c641b2SJohn Baldwin 	CTR1(KTR_VM, "vm_map_create: %p", result);
3822d8acc0fSJohn Dyson 	vm_map_init(result, min, max);
383df8bae1dSRodney W. Grimes 	result->pmap = pmap;
384df8bae1dSRodney W. Grimes 	return (result);
385df8bae1dSRodney W. Grimes }
386df8bae1dSRodney W. Grimes 
387df8bae1dSRodney W. Grimes /*
388df8bae1dSRodney W. Grimes  * Initialize an existing vm_map structure
389df8bae1dSRodney W. Grimes  * such as that in the vmspace structure.
390df8bae1dSRodney W. Grimes  * The pmap is set elsewhere.
391df8bae1dSRodney W. Grimes  */
392df8bae1dSRodney W. Grimes void
3931b40f8c0SMatthew Dillon vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max)
394df8bae1dSRodney W. Grimes {
3950cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
39621c641b2SJohn Baldwin 
397df8bae1dSRodney W. Grimes 	map->header.next = map->header.prev = &map->header;
398df8bae1dSRodney W. Grimes 	map->nentries = 0;
399df8bae1dSRodney W. Grimes 	map->size = 0;
4003075778bSJohn Dyson 	map->system_map = 0;
401b823bbd6SMatthew Dillon 	map->infork = 0;
402df8bae1dSRodney W. Grimes 	map->min_offset = min;
403df8bae1dSRodney W. Grimes 	map->max_offset = max;
404df8bae1dSRodney W. Grimes 	map->first_free = &map->header;
405df8bae1dSRodney W. Grimes 	map->hint = &map->header;
406df8bae1dSRodney W. Grimes 	map->timestamp = 0;
4078f9110f6SJohn Dyson 	lockinit(&map->lock, PVM, "thrd_sleep", 0, LK_NOPAUSE);
408df8bae1dSRodney W. Grimes }
409df8bae1dSRodney W. Grimes 
410a18b1f1dSJason Evans void
411a18b1f1dSJason Evans vm_map_destroy(map)
412a18b1f1dSJason Evans 	struct vm_map *map;
413a18b1f1dSJason Evans {
4140cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
415a18b1f1dSJason Evans 	lockdestroy(&map->lock);
416a18b1f1dSJason Evans }
417a18b1f1dSJason Evans 
418df8bae1dSRodney W. Grimes /*
419b18bfc3dSJohn Dyson  *	vm_map_entry_dispose:	[ internal use only ]
420b18bfc3dSJohn Dyson  *
421b18bfc3dSJohn Dyson  *	Inverse of vm_map_entry_create.
422b18bfc3dSJohn Dyson  */
42362487bb4SJohn Dyson static void
4241b40f8c0SMatthew Dillon vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry)
425b18bfc3dSJohn Dyson {
426b79933ebSJohn Dyson 	zfree((map->system_map || !mapentzone) ? kmapentzone : mapentzone, entry);
427b18bfc3dSJohn Dyson }
428b18bfc3dSJohn Dyson 
429b18bfc3dSJohn Dyson /*
430df8bae1dSRodney W. Grimes  *	vm_map_entry_create:	[ internal use only ]
431df8bae1dSRodney W. Grimes  *
432df8bae1dSRodney W. Grimes  *	Allocates a VM map entry for insertion.
433b28cb1caSAlfred Perlstein  *	No entry fields are filled in.
434df8bae1dSRodney W. Grimes  */
435f708ef1bSPoul-Henning Kamp static vm_map_entry_t
4361b40f8c0SMatthew Dillon vm_map_entry_create(vm_map_t map)
437df8bae1dSRodney W. Grimes {
4381f6889a1SMatthew Dillon 	vm_map_entry_t new_entry;
4391f6889a1SMatthew Dillon 
4401f6889a1SMatthew Dillon 	new_entry = zalloc((map->system_map || !mapentzone) ?
4411f6889a1SMatthew Dillon 		kmapentzone : mapentzone);
4421f6889a1SMatthew Dillon 	if (new_entry == NULL)
4431f6889a1SMatthew Dillon 	    panic("vm_map_entry_create: kernel resources exhausted");
4441f6889a1SMatthew Dillon 	return(new_entry);
445df8bae1dSRodney W. Grimes }
446df8bae1dSRodney W. Grimes 
447df8bae1dSRodney W. Grimes /*
448df8bae1dSRodney W. Grimes  *	vm_map_entry_{un,}link:
449df8bae1dSRodney W. Grimes  *
450df8bae1dSRodney W. Grimes  *	Insert/remove entries from maps.
451df8bae1dSRodney W. Grimes  */
45299c81ca9SAlan Cox static __inline void
45399c81ca9SAlan Cox vm_map_entry_link(vm_map_t map,
45499c81ca9SAlan Cox 		  vm_map_entry_t after_where,
45599c81ca9SAlan Cox 		  vm_map_entry_t entry)
45699c81ca9SAlan Cox {
45721c641b2SJohn Baldwin 
45821c641b2SJohn Baldwin 	CTR4(KTR_VM,
45921c641b2SJohn Baldwin 	    "vm_map_entry_link: map %p, nentries %d, entry %p, after %p", map,
46021c641b2SJohn Baldwin 	    map->nentries, entry, after_where);
46199c81ca9SAlan Cox 	map->nentries++;
46299c81ca9SAlan Cox 	entry->prev = after_where;
46399c81ca9SAlan Cox 	entry->next = after_where->next;
46499c81ca9SAlan Cox 	entry->next->prev = entry;
46599c81ca9SAlan Cox 	after_where->next = entry;
466df8bae1dSRodney W. Grimes }
46799c81ca9SAlan Cox 
46899c81ca9SAlan Cox static __inline void
46999c81ca9SAlan Cox vm_map_entry_unlink(vm_map_t map,
47099c81ca9SAlan Cox 		    vm_map_entry_t entry)
47199c81ca9SAlan Cox {
47299c81ca9SAlan Cox 	vm_map_entry_t prev = entry->prev;
47399c81ca9SAlan Cox 	vm_map_entry_t next = entry->next;
47499c81ca9SAlan Cox 
47599c81ca9SAlan Cox 	next->prev = prev;
47699c81ca9SAlan Cox 	prev->next = next;
47799c81ca9SAlan Cox 	map->nentries--;
47821c641b2SJohn Baldwin 	CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map,
47921c641b2SJohn Baldwin 	    map->nentries, entry);
480df8bae1dSRodney W. Grimes }
481df8bae1dSRodney W. Grimes 
482df8bae1dSRodney W. Grimes /*
483df8bae1dSRodney W. Grimes  *	SAVE_HINT:
484df8bae1dSRodney W. Grimes  *
485df8bae1dSRodney W. Grimes  *	Saves the specified entry as the hint for
48624a1cce3SDavid Greenman  *	future lookups.
487df8bae1dSRodney W. Grimes  */
488df8bae1dSRodney W. Grimes #define	SAVE_HINT(map,value) \
48924a1cce3SDavid Greenman 		(map)->hint = (value);
490df8bae1dSRodney W. Grimes 
491df8bae1dSRodney W. Grimes /*
492df8bae1dSRodney W. Grimes  *	vm_map_lookup_entry:	[ internal use only ]
493df8bae1dSRodney W. Grimes  *
494df8bae1dSRodney W. Grimes  *	Finds the map entry containing (or
495df8bae1dSRodney W. Grimes  *	immediately preceding) the specified address
496df8bae1dSRodney W. Grimes  *	in the given map; the entry is returned
497df8bae1dSRodney W. Grimes  *	in the "entry" parameter.  The boolean
498df8bae1dSRodney W. Grimes  *	result indicates whether the address is
499df8bae1dSRodney W. Grimes  *	actually contained in the map.
500df8bae1dSRodney W. Grimes  */
5010d94caffSDavid Greenman boolean_t
5021b40f8c0SMatthew Dillon vm_map_lookup_entry(
5031b40f8c0SMatthew Dillon 	vm_map_t map,
5041b40f8c0SMatthew Dillon 	vm_offset_t address,
5051b40f8c0SMatthew Dillon 	vm_map_entry_t *entry)	/* OUT */
506df8bae1dSRodney W. Grimes {
507c0877f10SJohn Dyson 	vm_map_entry_t cur;
508c0877f10SJohn Dyson 	vm_map_entry_t last;
509df8bae1dSRodney W. Grimes 
5100cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
511df8bae1dSRodney W. Grimes 	/*
5120d94caffSDavid Greenman 	 * Start looking either from the head of the list, or from the hint.
513df8bae1dSRodney W. Grimes 	 */
514df8bae1dSRodney W. Grimes 
515df8bae1dSRodney W. Grimes 	cur = map->hint;
516df8bae1dSRodney W. Grimes 
517df8bae1dSRodney W. Grimes 	if (cur == &map->header)
518df8bae1dSRodney W. Grimes 		cur = cur->next;
519df8bae1dSRodney W. Grimes 
520df8bae1dSRodney W. Grimes 	if (address >= cur->start) {
521df8bae1dSRodney W. Grimes 		/*
522df8bae1dSRodney W. Grimes 		 * Go from hint to end of list.
523df8bae1dSRodney W. Grimes 		 *
5240d94caffSDavid Greenman 		 * But first, make a quick check to see if we are already looking
5250d94caffSDavid Greenman 		 * at the entry we want (which is usually the case). Note also
5260d94caffSDavid Greenman 		 * that we don't need to save the hint here... it is the same
5270d94caffSDavid Greenman 		 * hint (unless we are at the header, in which case the hint
5280d94caffSDavid Greenman 		 * didn't buy us anything anyway).
529df8bae1dSRodney W. Grimes 		 */
530df8bae1dSRodney W. Grimes 		last = &map->header;
531df8bae1dSRodney W. Grimes 		if ((cur != last) && (cur->end > address)) {
532df8bae1dSRodney W. Grimes 			*entry = cur;
533df8bae1dSRodney W. Grimes 			return (TRUE);
534df8bae1dSRodney W. Grimes 		}
5350d94caffSDavid Greenman 	} else {
536df8bae1dSRodney W. Grimes 		/*
537df8bae1dSRodney W. Grimes 		 * Go from start to hint, *inclusively*
538df8bae1dSRodney W. Grimes 		 */
539df8bae1dSRodney W. Grimes 		last = cur->next;
540df8bae1dSRodney W. Grimes 		cur = map->header.next;
541df8bae1dSRodney W. Grimes 	}
542df8bae1dSRodney W. Grimes 
543df8bae1dSRodney W. Grimes 	/*
544df8bae1dSRodney W. Grimes 	 * Search linearly
545df8bae1dSRodney W. Grimes 	 */
546df8bae1dSRodney W. Grimes 
547df8bae1dSRodney W. Grimes 	while (cur != last) {
548df8bae1dSRodney W. Grimes 		if (cur->end > address) {
549df8bae1dSRodney W. Grimes 			if (address >= cur->start) {
550df8bae1dSRodney W. Grimes 				/*
5510d94caffSDavid Greenman 				 * Save this lookup for future hints, and
5520d94caffSDavid Greenman 				 * return
553df8bae1dSRodney W. Grimes 				 */
554df8bae1dSRodney W. Grimes 
555df8bae1dSRodney W. Grimes 				*entry = cur;
556df8bae1dSRodney W. Grimes 				SAVE_HINT(map, cur);
557df8bae1dSRodney W. Grimes 				return (TRUE);
558df8bae1dSRodney W. Grimes 			}
559df8bae1dSRodney W. Grimes 			break;
560df8bae1dSRodney W. Grimes 		}
561df8bae1dSRodney W. Grimes 		cur = cur->next;
562df8bae1dSRodney W. Grimes 	}
563df8bae1dSRodney W. Grimes 	*entry = cur->prev;
564df8bae1dSRodney W. Grimes 	SAVE_HINT(map, *entry);
565df8bae1dSRodney W. Grimes 	return (FALSE);
566df8bae1dSRodney W. Grimes }
567df8bae1dSRodney W. Grimes 
568df8bae1dSRodney W. Grimes /*
56930dcfc09SJohn Dyson  *	vm_map_insert:
57030dcfc09SJohn Dyson  *
57130dcfc09SJohn Dyson  *	Inserts the given whole VM object into the target
57230dcfc09SJohn Dyson  *	map at the specified address range.  The object's
57330dcfc09SJohn Dyson  *	size should match that of the address range.
57430dcfc09SJohn Dyson  *
57530dcfc09SJohn Dyson  *	Requires that the map be locked, and leaves it so.
5762aaeadf8SMatthew Dillon  *
5772aaeadf8SMatthew Dillon  *	If object is non-NULL, ref count must be bumped by caller
5782aaeadf8SMatthew Dillon  *	prior to making call to account for the new entry.
57930dcfc09SJohn Dyson  */
58030dcfc09SJohn Dyson int
581b9dcd593SBruce Evans vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
582b9dcd593SBruce Evans 	      vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max,
583b9dcd593SBruce Evans 	      int cow)
58430dcfc09SJohn Dyson {
585c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
586c0877f10SJohn Dyson 	vm_map_entry_t prev_entry;
58730dcfc09SJohn Dyson 	vm_map_entry_t temp_entry;
5889730a5daSPaul Saab 	vm_eflags_t protoeflags;
58930dcfc09SJohn Dyson 
5900cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
5910cddd8f0SMatthew Dillon 
59230dcfc09SJohn Dyson 	/*
59330dcfc09SJohn Dyson 	 * Check that the start and end points are not bogus.
59430dcfc09SJohn Dyson 	 */
59530dcfc09SJohn Dyson 
59630dcfc09SJohn Dyson 	if ((start < map->min_offset) || (end > map->max_offset) ||
59730dcfc09SJohn Dyson 	    (start >= end))
59830dcfc09SJohn Dyson 		return (KERN_INVALID_ADDRESS);
59930dcfc09SJohn Dyson 
60030dcfc09SJohn Dyson 	/*
60130dcfc09SJohn Dyson 	 * Find the entry prior to the proposed starting address; if it's part
60230dcfc09SJohn Dyson 	 * of an existing entry, this range is bogus.
60330dcfc09SJohn Dyson 	 */
60430dcfc09SJohn Dyson 
60530dcfc09SJohn Dyson 	if (vm_map_lookup_entry(map, start, &temp_entry))
60630dcfc09SJohn Dyson 		return (KERN_NO_SPACE);
60730dcfc09SJohn Dyson 
60830dcfc09SJohn Dyson 	prev_entry = temp_entry;
60930dcfc09SJohn Dyson 
61030dcfc09SJohn Dyson 	/*
61130dcfc09SJohn Dyson 	 * Assert that the next entry doesn't overlap the end point.
61230dcfc09SJohn Dyson 	 */
61330dcfc09SJohn Dyson 
61430dcfc09SJohn Dyson 	if ((prev_entry->next != &map->header) &&
61530dcfc09SJohn Dyson 	    (prev_entry->next->start < end))
61630dcfc09SJohn Dyson 		return (KERN_NO_SPACE);
61730dcfc09SJohn Dyson 
618afa07f7eSJohn Dyson 	protoeflags = 0;
619afa07f7eSJohn Dyson 
620afa07f7eSJohn Dyson 	if (cow & MAP_COPY_ON_WRITE)
621e5f13bddSAlan Cox 		protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
622afa07f7eSJohn Dyson 
6234e045f93SAlan Cox 	if (cow & MAP_NOFAULT) {
624afa07f7eSJohn Dyson 		protoeflags |= MAP_ENTRY_NOFAULT;
625afa07f7eSJohn Dyson 
6264e045f93SAlan Cox 		KASSERT(object == NULL,
6274e045f93SAlan Cox 			("vm_map_insert: paradoxical MAP_NOFAULT request"));
6284e045f93SAlan Cox 	}
6294f79d873SMatthew Dillon 	if (cow & MAP_DISABLE_SYNCER)
6304f79d873SMatthew Dillon 		protoeflags |= MAP_ENTRY_NOSYNC;
6319730a5daSPaul Saab 	if (cow & MAP_DISABLE_COREDUMP)
6329730a5daSPaul Saab 		protoeflags |= MAP_ENTRY_NOCOREDUMP;
6334f79d873SMatthew Dillon 
6342aaeadf8SMatthew Dillon 	if (object) {
63530dcfc09SJohn Dyson 		/*
6362aaeadf8SMatthew Dillon 		 * When object is non-NULL, it could be shared with another
6372aaeadf8SMatthew Dillon 		 * process.  We have to set or clear OBJ_ONEMAPPING
6382aaeadf8SMatthew Dillon 		 * appropriately.
63930dcfc09SJohn Dyson 		 */
6402aaeadf8SMatthew Dillon 		if ((object->ref_count > 1) || (object->shadow_count != 0)) {
6412aaeadf8SMatthew Dillon 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
6422aaeadf8SMatthew Dillon 		}
6434e045f93SAlan Cox 	}
6444e045f93SAlan Cox 	else if ((prev_entry != &map->header) &&
6454e045f93SAlan Cox 		 (prev_entry->eflags == protoeflags) &&
6468cc7e047SJohn Dyson 		 (prev_entry->end == start) &&
6474e045f93SAlan Cox 		 (prev_entry->wired_count == 0) &&
6484e045f93SAlan Cox 		 ((prev_entry->object.vm_object == NULL) ||
6498cc7e047SJohn Dyson 		  vm_object_coalesce(prev_entry->object.vm_object,
65030dcfc09SJohn Dyson 				     OFF_TO_IDX(prev_entry->offset),
6518cc7e047SJohn Dyson 				     (vm_size_t)(prev_entry->end - prev_entry->start),
652cdc2c291SJohn Dyson 				     (vm_size_t)(end - prev_entry->end)))) {
65330dcfc09SJohn Dyson 		/*
6542aaeadf8SMatthew Dillon 		 * We were able to extend the object.  Determine if we
6552aaeadf8SMatthew Dillon 		 * can extend the previous map entry to include the
6562aaeadf8SMatthew Dillon 		 * new range as well.
65730dcfc09SJohn Dyson 		 */
6588cc7e047SJohn Dyson 		if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
6598cc7e047SJohn Dyson 		    (prev_entry->protection == prot) &&
6608cc7e047SJohn Dyson 		    (prev_entry->max_protection == max)) {
66130dcfc09SJohn Dyson 			map->size += (end - prev_entry->end);
66230dcfc09SJohn Dyson 			prev_entry->end = end;
6634e71e795SMatthew Dillon 			vm_map_simplify_entry(map, prev_entry);
66430dcfc09SJohn Dyson 			return (KERN_SUCCESS);
66530dcfc09SJohn Dyson 		}
6668cc7e047SJohn Dyson 
6672aaeadf8SMatthew Dillon 		/*
6682aaeadf8SMatthew Dillon 		 * If we can extend the object but cannot extend the
6692aaeadf8SMatthew Dillon 		 * map entry, we have to create a new map entry.  We
6702aaeadf8SMatthew Dillon 		 * must bump the ref count on the extended object to
6714e71e795SMatthew Dillon 		 * account for it.  object may be NULL.
6722aaeadf8SMatthew Dillon 		 */
6732aaeadf8SMatthew Dillon 		object = prev_entry->object.vm_object;
6742aaeadf8SMatthew Dillon 		offset = prev_entry->offset +
6752aaeadf8SMatthew Dillon 			(prev_entry->end - prev_entry->start);
6768cc7e047SJohn Dyson 		vm_object_reference(object);
677b18bfc3dSJohn Dyson 	}
6782aaeadf8SMatthew Dillon 
6792aaeadf8SMatthew Dillon 	/*
6802aaeadf8SMatthew Dillon 	 * NOTE: if conditionals fail, object can be NULL here.  This occurs
6812aaeadf8SMatthew Dillon 	 * in things like the buffer map where we manage kva but do not manage
6822aaeadf8SMatthew Dillon 	 * backing objects.
6832aaeadf8SMatthew Dillon 	 */
6848cc7e047SJohn Dyson 
68530dcfc09SJohn Dyson 	/*
68630dcfc09SJohn Dyson 	 * Create a new entry
68730dcfc09SJohn Dyson 	 */
68830dcfc09SJohn Dyson 
68930dcfc09SJohn Dyson 	new_entry = vm_map_entry_create(map);
69030dcfc09SJohn Dyson 	new_entry->start = start;
69130dcfc09SJohn Dyson 	new_entry->end = end;
69230dcfc09SJohn Dyson 
693afa07f7eSJohn Dyson 	new_entry->eflags = protoeflags;
69430dcfc09SJohn Dyson 	new_entry->object.vm_object = object;
69530dcfc09SJohn Dyson 	new_entry->offset = offset;
6962267af78SJulian Elischer 	new_entry->avail_ssize = 0;
6972267af78SJulian Elischer 
69830dcfc09SJohn Dyson 	new_entry->inheritance = VM_INHERIT_DEFAULT;
69930dcfc09SJohn Dyson 	new_entry->protection = prot;
70030dcfc09SJohn Dyson 	new_entry->max_protection = max;
70130dcfc09SJohn Dyson 	new_entry->wired_count = 0;
702e5f251d2SAlan Cox 
70330dcfc09SJohn Dyson 	/*
70430dcfc09SJohn Dyson 	 * Insert the new entry into the list
70530dcfc09SJohn Dyson 	 */
70630dcfc09SJohn Dyson 
70730dcfc09SJohn Dyson 	vm_map_entry_link(map, prev_entry, new_entry);
70830dcfc09SJohn Dyson 	map->size += new_entry->end - new_entry->start;
70930dcfc09SJohn Dyson 
71030dcfc09SJohn Dyson 	/*
71130dcfc09SJohn Dyson 	 * Update the free space hint
71230dcfc09SJohn Dyson 	 */
71367bf6868SJohn Dyson 	if ((map->first_free == prev_entry) &&
7144f79d873SMatthew Dillon 	    (prev_entry->end >= new_entry->start)) {
71530dcfc09SJohn Dyson 		map->first_free = new_entry;
7164f79d873SMatthew Dillon 	}
71730dcfc09SJohn Dyson 
7181a484d28SMatthew Dillon #if 0
7191a484d28SMatthew Dillon 	/*
7201a484d28SMatthew Dillon 	 * Temporarily removed to avoid MAP_STACK panic, due to
7211a484d28SMatthew Dillon 	 * MAP_STACK being a huge hack.  Will be added back in
7221a484d28SMatthew Dillon 	 * when MAP_STACK (and the user stack mapping) is fixed.
7231a484d28SMatthew Dillon 	 */
7244e71e795SMatthew Dillon 	/*
7254e71e795SMatthew Dillon 	 * It may be possible to simplify the entry
7264e71e795SMatthew Dillon 	 */
7274e71e795SMatthew Dillon 	vm_map_simplify_entry(map, new_entry);
7281a484d28SMatthew Dillon #endif
7294e71e795SMatthew Dillon 
7304f79d873SMatthew Dillon 	if (cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) {
731e972780aSAlan Cox 		pmap_object_init_pt(map->pmap, start,
732e972780aSAlan Cox 				    object, OFF_TO_IDX(offset), end - start,
733e972780aSAlan Cox 				    cow & MAP_PREFAULT_PARTIAL);
7344f79d873SMatthew Dillon 	}
735e972780aSAlan Cox 
73630dcfc09SJohn Dyson 	return (KERN_SUCCESS);
73730dcfc09SJohn Dyson }
73830dcfc09SJohn Dyson 
73930dcfc09SJohn Dyson /*
740df8bae1dSRodney W. Grimes  * Find sufficient space for `length' bytes in the given map, starting at
741df8bae1dSRodney W. Grimes  * `start'.  The map must be locked.  Returns 0 on success, 1 on no space.
742df8bae1dSRodney W. Grimes  */
743df8bae1dSRodney W. Grimes int
7441b40f8c0SMatthew Dillon vm_map_findspace(
7451b40f8c0SMatthew Dillon 	vm_map_t map,
7461b40f8c0SMatthew Dillon 	vm_offset_t start,
7471b40f8c0SMatthew Dillon 	vm_size_t length,
7481b40f8c0SMatthew Dillon 	vm_offset_t *addr)
749df8bae1dSRodney W. Grimes {
750c0877f10SJohn Dyson 	vm_map_entry_t entry, next;
751c0877f10SJohn Dyson 	vm_offset_t end;
752df8bae1dSRodney W. Grimes 
7530cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
754df8bae1dSRodney W. Grimes 	if (start < map->min_offset)
755df8bae1dSRodney W. Grimes 		start = map->min_offset;
756df8bae1dSRodney W. Grimes 	if (start > map->max_offset)
757df8bae1dSRodney W. Grimes 		return (1);
758df8bae1dSRodney W. Grimes 
759df8bae1dSRodney W. Grimes 	/*
7600d94caffSDavid Greenman 	 * Look for the first possible address; if there's already something
7610d94caffSDavid Greenman 	 * at this address, we have to start after it.
762df8bae1dSRodney W. Grimes 	 */
763df8bae1dSRodney W. Grimes 	if (start == map->min_offset) {
76467bf6868SJohn Dyson 		if ((entry = map->first_free) != &map->header)
765df8bae1dSRodney W. Grimes 			start = entry->end;
766df8bae1dSRodney W. Grimes 	} else {
767df8bae1dSRodney W. Grimes 		vm_map_entry_t tmp;
7680d94caffSDavid Greenman 
769df8bae1dSRodney W. Grimes 		if (vm_map_lookup_entry(map, start, &tmp))
770df8bae1dSRodney W. Grimes 			start = tmp->end;
771df8bae1dSRodney W. Grimes 		entry = tmp;
772df8bae1dSRodney W. Grimes 	}
773df8bae1dSRodney W. Grimes 
774df8bae1dSRodney W. Grimes 	/*
7750d94caffSDavid Greenman 	 * Look through the rest of the map, trying to fit a new region in the
7760d94caffSDavid Greenman 	 * gap between existing regions, or after the very last region.
777df8bae1dSRodney W. Grimes 	 */
778df8bae1dSRodney W. Grimes 	for (;; start = (entry = next)->end) {
779df8bae1dSRodney W. Grimes 		/*
780df8bae1dSRodney W. Grimes 		 * Find the end of the proposed new region.  Be sure we didn't
781df8bae1dSRodney W. Grimes 		 * go beyond the end of the map, or wrap around the address;
782df8bae1dSRodney W. Grimes 		 * if so, we lose.  Otherwise, if this is the last entry, or
783df8bae1dSRodney W. Grimes 		 * if the proposed new region fits before the next entry, we
784df8bae1dSRodney W. Grimes 		 * win.
785df8bae1dSRodney W. Grimes 		 */
786df8bae1dSRodney W. Grimes 		end = start + length;
787df8bae1dSRodney W. Grimes 		if (end > map->max_offset || end < start)
788df8bae1dSRodney W. Grimes 			return (1);
789df8bae1dSRodney W. Grimes 		next = entry->next;
790df8bae1dSRodney W. Grimes 		if (next == &map->header || next->start >= end)
791df8bae1dSRodney W. Grimes 			break;
792df8bae1dSRodney W. Grimes 	}
793df8bae1dSRodney W. Grimes 	SAVE_HINT(map, entry);
794df8bae1dSRodney W. Grimes 	*addr = start;
79599448ed1SJohn Dyson 	if (map == kernel_map) {
79699448ed1SJohn Dyson 		vm_offset_t ksize;
79799448ed1SJohn Dyson 		if ((ksize = round_page(start + length)) > kernel_vm_end) {
79899448ed1SJohn Dyson 			pmap_growkernel(ksize);
79999448ed1SJohn Dyson 		}
80099448ed1SJohn Dyson 	}
801df8bae1dSRodney W. Grimes 	return (0);
802df8bae1dSRodney W. Grimes }
803df8bae1dSRodney W. Grimes 
804df8bae1dSRodney W. Grimes /*
805df8bae1dSRodney W. Grimes  *	vm_map_find finds an unallocated region in the target address
806df8bae1dSRodney W. Grimes  *	map with the given length.  The search is defined to be
807df8bae1dSRodney W. Grimes  *	first-fit from the specified address; the region found is
808df8bae1dSRodney W. Grimes  *	returned in the same parameter.
809df8bae1dSRodney W. Grimes  *
8102aaeadf8SMatthew Dillon  *	If object is non-NULL, ref count must be bumped by caller
8112aaeadf8SMatthew Dillon  *	prior to making call to account for the new entry.
812df8bae1dSRodney W. Grimes  */
813df8bae1dSRodney W. Grimes int
814b9dcd593SBruce Evans vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
815b9dcd593SBruce Evans 	    vm_offset_t *addr,	/* IN/OUT */
816b9dcd593SBruce Evans 	    vm_size_t length, boolean_t find_space, vm_prot_t prot,
817b9dcd593SBruce Evans 	    vm_prot_t max, int cow)
818df8bae1dSRodney W. Grimes {
819c0877f10SJohn Dyson 	vm_offset_t start;
8208d6e8edeSDavid Greenman 	int result, s = 0;
821df8bae1dSRodney W. Grimes 
8220cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
8230cddd8f0SMatthew Dillon 
824df8bae1dSRodney W. Grimes 	start = *addr;
8258d6e8edeSDavid Greenman 
82608442f8aSBosko Milekic 	if (map == kmem_map)
827b18bfc3dSJohn Dyson 		s = splvm();
8288d6e8edeSDavid Greenman 
829bea41bcfSDavid Greenman 	vm_map_lock(map);
830df8bae1dSRodney W. Grimes 	if (find_space) {
831df8bae1dSRodney W. Grimes 		if (vm_map_findspace(map, start, length, addr)) {
832df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
83308442f8aSBosko Milekic 			if (map == kmem_map)
8348d6e8edeSDavid Greenman 				splx(s);
835df8bae1dSRodney W. Grimes 			return (KERN_NO_SPACE);
836df8bae1dSRodney W. Grimes 		}
837df8bae1dSRodney W. Grimes 		start = *addr;
838df8bae1dSRodney W. Grimes 	}
839bd7e5f99SJohn Dyson 	result = vm_map_insert(map, object, offset,
840bd7e5f99SJohn Dyson 		start, start + length, prot, max, cow);
841df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
8428d6e8edeSDavid Greenman 
84308442f8aSBosko Milekic 	if (map == kmem_map)
8448d6e8edeSDavid Greenman 		splx(s);
8458d6e8edeSDavid Greenman 
846df8bae1dSRodney W. Grimes 	return (result);
847df8bae1dSRodney W. Grimes }
848df8bae1dSRodney W. Grimes 
849df8bae1dSRodney W. Grimes /*
850b7b2aac2SJohn Dyson  *	vm_map_simplify_entry:
85167bf6868SJohn Dyson  *
8524e71e795SMatthew Dillon  *	Simplify the given map entry by merging with either neighbor.  This
8534e71e795SMatthew Dillon  *	routine also has the ability to merge with both neighbors.
8544e71e795SMatthew Dillon  *
8554e71e795SMatthew Dillon  *	The map must be locked.
8564e71e795SMatthew Dillon  *
8574e71e795SMatthew Dillon  *	This routine guarentees that the passed entry remains valid (though
8584e71e795SMatthew Dillon  *	possibly extended).  When merging, this routine may delete one or
8594e71e795SMatthew Dillon  *	both neighbors.
860df8bae1dSRodney W. Grimes  */
861b7b2aac2SJohn Dyson void
8621b40f8c0SMatthew Dillon vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry)
863df8bae1dSRodney W. Grimes {
864308c24baSJohn Dyson 	vm_map_entry_t next, prev;
865b7b2aac2SJohn Dyson 	vm_size_t prevsize, esize;
866df8bae1dSRodney W. Grimes 
8670cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
8680cddd8f0SMatthew Dillon 
8699fdfe602SMatthew Dillon 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
870df8bae1dSRodney W. Grimes 		return;
871308c24baSJohn Dyson 
872308c24baSJohn Dyson 	prev = entry->prev;
873308c24baSJohn Dyson 	if (prev != &map->header) {
87467bf6868SJohn Dyson 		prevsize = prev->end - prev->start;
87567bf6868SJohn Dyson 		if ( (prev->end == entry->start) &&
87667bf6868SJohn Dyson 		     (prev->object.vm_object == entry->object.vm_object) &&
87795e5e988SJohn Dyson 		     (!prev->object.vm_object ||
87867bf6868SJohn Dyson 			(prev->offset + prevsize == entry->offset)) &&
879afa07f7eSJohn Dyson 		     (prev->eflags == entry->eflags) &&
88067bf6868SJohn Dyson 		     (prev->protection == entry->protection) &&
88167bf6868SJohn Dyson 		     (prev->max_protection == entry->max_protection) &&
88267bf6868SJohn Dyson 		     (prev->inheritance == entry->inheritance) &&
883b7b2aac2SJohn Dyson 		     (prev->wired_count == entry->wired_count)) {
884308c24baSJohn Dyson 			if (map->first_free == prev)
885308c24baSJohn Dyson 				map->first_free = entry;
886b18bfc3dSJohn Dyson 			if (map->hint == prev)
887b18bfc3dSJohn Dyson 				map->hint = entry;
888308c24baSJohn Dyson 			vm_map_entry_unlink(map, prev);
889308c24baSJohn Dyson 			entry->start = prev->start;
890308c24baSJohn Dyson 			entry->offset = prev->offset;
891b18bfc3dSJohn Dyson 			if (prev->object.vm_object)
892308c24baSJohn Dyson 				vm_object_deallocate(prev->object.vm_object);
893308c24baSJohn Dyson 			vm_map_entry_dispose(map, prev);
894308c24baSJohn Dyson 		}
895308c24baSJohn Dyson 	}
896de5f6a77SJohn Dyson 
897de5f6a77SJohn Dyson 	next = entry->next;
898308c24baSJohn Dyson 	if (next != &map->header) {
89967bf6868SJohn Dyson 		esize = entry->end - entry->start;
90067bf6868SJohn Dyson 		if ((entry->end == next->start) &&
90167bf6868SJohn Dyson 		    (next->object.vm_object == entry->object.vm_object) &&
90267bf6868SJohn Dyson 		     (!entry->object.vm_object ||
90367bf6868SJohn Dyson 			(entry->offset + esize == next->offset)) &&
904afa07f7eSJohn Dyson 		    (next->eflags == entry->eflags) &&
90567bf6868SJohn Dyson 		    (next->protection == entry->protection) &&
90667bf6868SJohn Dyson 		    (next->max_protection == entry->max_protection) &&
90767bf6868SJohn Dyson 		    (next->inheritance == entry->inheritance) &&
908b7b2aac2SJohn Dyson 		    (next->wired_count == entry->wired_count)) {
909308c24baSJohn Dyson 			if (map->first_free == next)
910308c24baSJohn Dyson 				map->first_free = entry;
911b18bfc3dSJohn Dyson 			if (map->hint == next)
912b18bfc3dSJohn Dyson 				map->hint = entry;
913de5f6a77SJohn Dyson 			vm_map_entry_unlink(map, next);
914de5f6a77SJohn Dyson 			entry->end = next->end;
915b18bfc3dSJohn Dyson 			if (next->object.vm_object)
916de5f6a77SJohn Dyson 				vm_object_deallocate(next->object.vm_object);
917de5f6a77SJohn Dyson 			vm_map_entry_dispose(map, next);
918df8bae1dSRodney W. Grimes 	        }
919df8bae1dSRodney W. Grimes 	}
920de5f6a77SJohn Dyson }
921df8bae1dSRodney W. Grimes /*
922df8bae1dSRodney W. Grimes  *	vm_map_clip_start:	[ internal use only ]
923df8bae1dSRodney W. Grimes  *
924df8bae1dSRodney W. Grimes  *	Asserts that the given entry begins at or after
925df8bae1dSRodney W. Grimes  *	the specified address; if necessary,
926df8bae1dSRodney W. Grimes  *	it splits the entry into two.
927df8bae1dSRodney W. Grimes  */
928df8bae1dSRodney W. Grimes #define vm_map_clip_start(map, entry, startaddr) \
929df8bae1dSRodney W. Grimes { \
930df8bae1dSRodney W. Grimes 	if (startaddr > entry->start) \
931df8bae1dSRodney W. Grimes 		_vm_map_clip_start(map, entry, startaddr); \
932df8bae1dSRodney W. Grimes }
933df8bae1dSRodney W. Grimes 
934df8bae1dSRodney W. Grimes /*
935df8bae1dSRodney W. Grimes  *	This routine is called only when it is known that
936df8bae1dSRodney W. Grimes  *	the entry must be split.
937df8bae1dSRodney W. Grimes  */
9380d94caffSDavid Greenman static void
9391b40f8c0SMatthew Dillon _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start)
940df8bae1dSRodney W. Grimes {
941c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
942df8bae1dSRodney W. Grimes 
943df8bae1dSRodney W. Grimes 	/*
9440d94caffSDavid Greenman 	 * Split off the front portion -- note that we must insert the new
9450d94caffSDavid Greenman 	 * entry BEFORE this one, so that this entry has the specified
9460d94caffSDavid Greenman 	 * starting address.
947df8bae1dSRodney W. Grimes 	 */
948df8bae1dSRodney W. Grimes 
949f32dbbeeSJohn Dyson 	vm_map_simplify_entry(map, entry);
950f32dbbeeSJohn Dyson 
95111cccda1SJohn Dyson 	/*
95211cccda1SJohn Dyson 	 * If there is no object backing this entry, we might as well create
95311cccda1SJohn Dyson 	 * one now.  If we defer it, an object can get created after the map
95411cccda1SJohn Dyson 	 * is clipped, and individual objects will be created for the split-up
95511cccda1SJohn Dyson 	 * map.  This is a bit of a hack, but is also about the best place to
95611cccda1SJohn Dyson 	 * put this improvement.
95711cccda1SJohn Dyson 	 */
95811cccda1SJohn Dyson 
9594e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL && !map->system_map) {
96011cccda1SJohn Dyson 		vm_object_t object;
96111cccda1SJohn Dyson 		object = vm_object_allocate(OBJT_DEFAULT,
962c2e11a03SJohn Dyson 				atop(entry->end - entry->start));
96311cccda1SJohn Dyson 		entry->object.vm_object = object;
96411cccda1SJohn Dyson 		entry->offset = 0;
96511cccda1SJohn Dyson 	}
96611cccda1SJohn Dyson 
967df8bae1dSRodney W. Grimes 	new_entry = vm_map_entry_create(map);
968df8bae1dSRodney W. Grimes 	*new_entry = *entry;
969df8bae1dSRodney W. Grimes 
970df8bae1dSRodney W. Grimes 	new_entry->end = start;
971df8bae1dSRodney W. Grimes 	entry->offset += (start - entry->start);
972df8bae1dSRodney W. Grimes 	entry->start = start;
973df8bae1dSRodney W. Grimes 
974df8bae1dSRodney W. Grimes 	vm_map_entry_link(map, entry->prev, new_entry);
975df8bae1dSRodney W. Grimes 
9769fdfe602SMatthew Dillon 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
977df8bae1dSRodney W. Grimes 		vm_object_reference(new_entry->object.vm_object);
978df8bae1dSRodney W. Grimes 	}
979c0877f10SJohn Dyson }
980df8bae1dSRodney W. Grimes 
981df8bae1dSRodney W. Grimes /*
982df8bae1dSRodney W. Grimes  *	vm_map_clip_end:	[ internal use only ]
983df8bae1dSRodney W. Grimes  *
984df8bae1dSRodney W. Grimes  *	Asserts that the given entry ends at or before
985df8bae1dSRodney W. Grimes  *	the specified address; if necessary,
986df8bae1dSRodney W. Grimes  *	it splits the entry into two.
987df8bae1dSRodney W. Grimes  */
988df8bae1dSRodney W. Grimes 
989df8bae1dSRodney W. Grimes #define vm_map_clip_end(map, entry, endaddr) \
990df8bae1dSRodney W. Grimes { \
991df8bae1dSRodney W. Grimes 	if (endaddr < entry->end) \
992df8bae1dSRodney W. Grimes 		_vm_map_clip_end(map, entry, endaddr); \
993df8bae1dSRodney W. Grimes }
994df8bae1dSRodney W. Grimes 
995df8bae1dSRodney W. Grimes /*
996df8bae1dSRodney W. Grimes  *	This routine is called only when it is known that
997df8bae1dSRodney W. Grimes  *	the entry must be split.
998df8bae1dSRodney W. Grimes  */
9990d94caffSDavid Greenman static void
10001b40f8c0SMatthew Dillon _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end)
1001df8bae1dSRodney W. Grimes {
1002c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
1003df8bae1dSRodney W. Grimes 
1004df8bae1dSRodney W. Grimes 	/*
100511cccda1SJohn Dyson 	 * If there is no object backing this entry, we might as well create
100611cccda1SJohn Dyson 	 * one now.  If we defer it, an object can get created after the map
100711cccda1SJohn Dyson 	 * is clipped, and individual objects will be created for the split-up
100811cccda1SJohn Dyson 	 * map.  This is a bit of a hack, but is also about the best place to
100911cccda1SJohn Dyson 	 * put this improvement.
101011cccda1SJohn Dyson 	 */
101111cccda1SJohn Dyson 
10124e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL && !map->system_map) {
101311cccda1SJohn Dyson 		vm_object_t object;
101411cccda1SJohn Dyson 		object = vm_object_allocate(OBJT_DEFAULT,
1015c2e11a03SJohn Dyson 				atop(entry->end - entry->start));
101611cccda1SJohn Dyson 		entry->object.vm_object = object;
101711cccda1SJohn Dyson 		entry->offset = 0;
101811cccda1SJohn Dyson 	}
101911cccda1SJohn Dyson 
102011cccda1SJohn Dyson 	/*
10210d94caffSDavid Greenman 	 * Create a new entry and insert it AFTER the specified entry
1022df8bae1dSRodney W. Grimes 	 */
1023df8bae1dSRodney W. Grimes 
1024df8bae1dSRodney W. Grimes 	new_entry = vm_map_entry_create(map);
1025df8bae1dSRodney W. Grimes 	*new_entry = *entry;
1026df8bae1dSRodney W. Grimes 
1027df8bae1dSRodney W. Grimes 	new_entry->start = entry->end = end;
1028df8bae1dSRodney W. Grimes 	new_entry->offset += (end - entry->start);
1029df8bae1dSRodney W. Grimes 
1030df8bae1dSRodney W. Grimes 	vm_map_entry_link(map, entry, new_entry);
1031df8bae1dSRodney W. Grimes 
10329fdfe602SMatthew Dillon 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1033df8bae1dSRodney W. Grimes 		vm_object_reference(new_entry->object.vm_object);
1034df8bae1dSRodney W. Grimes 	}
1035c0877f10SJohn Dyson }
1036df8bae1dSRodney W. Grimes 
1037df8bae1dSRodney W. Grimes /*
1038df8bae1dSRodney W. Grimes  *	VM_MAP_RANGE_CHECK:	[ internal use only ]
1039df8bae1dSRodney W. Grimes  *
1040df8bae1dSRodney W. Grimes  *	Asserts that the starting and ending region
1041df8bae1dSRodney W. Grimes  *	addresses fall within the valid range of the map.
1042df8bae1dSRodney W. Grimes  */
1043df8bae1dSRodney W. Grimes #define	VM_MAP_RANGE_CHECK(map, start, end)		\
1044df8bae1dSRodney W. Grimes 		{					\
1045df8bae1dSRodney W. Grimes 		if (start < vm_map_min(map))		\
1046df8bae1dSRodney W. Grimes 			start = vm_map_min(map);	\
1047df8bae1dSRodney W. Grimes 		if (end > vm_map_max(map))		\
1048df8bae1dSRodney W. Grimes 			end = vm_map_max(map);		\
1049df8bae1dSRodney W. Grimes 		if (start > end)			\
1050df8bae1dSRodney W. Grimes 			start = end;			\
1051df8bae1dSRodney W. Grimes 		}
1052df8bae1dSRodney W. Grimes 
1053df8bae1dSRodney W. Grimes /*
1054df8bae1dSRodney W. Grimes  *	vm_map_submap:		[ kernel use only ]
1055df8bae1dSRodney W. Grimes  *
1056df8bae1dSRodney W. Grimes  *	Mark the given range as handled by a subordinate map.
1057df8bae1dSRodney W. Grimes  *
1058df8bae1dSRodney W. Grimes  *	This range must have been created with vm_map_find,
1059df8bae1dSRodney W. Grimes  *	and no other operations may have been performed on this
1060df8bae1dSRodney W. Grimes  *	range prior to calling vm_map_submap.
1061df8bae1dSRodney W. Grimes  *
1062df8bae1dSRodney W. Grimes  *	Only a limited number of operations can be performed
1063df8bae1dSRodney W. Grimes  *	within this rage after calling vm_map_submap:
1064df8bae1dSRodney W. Grimes  *		vm_fault
1065df8bae1dSRodney W. Grimes  *	[Don't try vm_map_copy!]
1066df8bae1dSRodney W. Grimes  *
1067df8bae1dSRodney W. Grimes  *	To remove a submapping, one must first remove the
1068df8bae1dSRodney W. Grimes  *	range from the superior map, and then destroy the
1069df8bae1dSRodney W. Grimes  *	submap (if desired).  [Better yet, don't try it.]
1070df8bae1dSRodney W. Grimes  */
1071df8bae1dSRodney W. Grimes int
10721b40f8c0SMatthew Dillon vm_map_submap(
10731b40f8c0SMatthew Dillon 	vm_map_t map,
10741b40f8c0SMatthew Dillon 	vm_offset_t start,
10751b40f8c0SMatthew Dillon 	vm_offset_t end,
10761b40f8c0SMatthew Dillon 	vm_map_t submap)
1077df8bae1dSRodney W. Grimes {
1078df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
1079c0877f10SJohn Dyson 	int result = KERN_INVALID_ARGUMENT;
1080df8bae1dSRodney W. Grimes 
10810cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
10820cddd8f0SMatthew Dillon 
1083df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1084df8bae1dSRodney W. Grimes 
1085df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1086df8bae1dSRodney W. Grimes 
1087df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &entry)) {
1088df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
10890d94caffSDavid Greenman 	} else
1090df8bae1dSRodney W. Grimes 		entry = entry->next;
1091df8bae1dSRodney W. Grimes 
1092df8bae1dSRodney W. Grimes 	vm_map_clip_end(map, entry, end);
1093df8bae1dSRodney W. Grimes 
1094df8bae1dSRodney W. Grimes 	if ((entry->start == start) && (entry->end == end) &&
10959fdfe602SMatthew Dillon 	    ((entry->eflags & MAP_ENTRY_COW) == 0) &&
1096afa07f7eSJohn Dyson 	    (entry->object.vm_object == NULL)) {
10972d8acc0fSJohn Dyson 		entry->object.sub_map = submap;
1098afa07f7eSJohn Dyson 		entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
1099df8bae1dSRodney W. Grimes 		result = KERN_SUCCESS;
1100df8bae1dSRodney W. Grimes 	}
1101df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1102df8bae1dSRodney W. Grimes 
1103df8bae1dSRodney W. Grimes 	return (result);
1104df8bae1dSRodney W. Grimes }
1105df8bae1dSRodney W. Grimes 
1106df8bae1dSRodney W. Grimes /*
1107df8bae1dSRodney W. Grimes  *	vm_map_protect:
1108df8bae1dSRodney W. Grimes  *
1109df8bae1dSRodney W. Grimes  *	Sets the protection of the specified address
1110df8bae1dSRodney W. Grimes  *	region in the target map.  If "set_max" is
1111df8bae1dSRodney W. Grimes  *	specified, the maximum protection is to be set;
1112df8bae1dSRodney W. Grimes  *	otherwise, only the current protection is affected.
1113df8bae1dSRodney W. Grimes  */
1114df8bae1dSRodney W. Grimes int
1115b9dcd593SBruce Evans vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
1116b9dcd593SBruce Evans 	       vm_prot_t new_prot, boolean_t set_max)
1117df8bae1dSRodney W. Grimes {
1118c0877f10SJohn Dyson 	vm_map_entry_t current;
1119df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
1120df8bae1dSRodney W. Grimes 
11210cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
1122df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1123df8bae1dSRodney W. Grimes 
1124df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1125df8bae1dSRodney W. Grimes 
1126df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &entry)) {
1127df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
1128b7b2aac2SJohn Dyson 	} else {
1129df8bae1dSRodney W. Grimes 		entry = entry->next;
1130b7b2aac2SJohn Dyson 	}
1131df8bae1dSRodney W. Grimes 
1132df8bae1dSRodney W. Grimes 	/*
11330d94caffSDavid Greenman 	 * Make a first pass to check for protection violations.
1134df8bae1dSRodney W. Grimes 	 */
1135df8bae1dSRodney W. Grimes 
1136df8bae1dSRodney W. Grimes 	current = entry;
1137df8bae1dSRodney W. Grimes 	while ((current != &map->header) && (current->start < end)) {
1138afa07f7eSJohn Dyson 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1139a1f6d91cSDavid Greenman 			vm_map_unlock(map);
1140df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ARGUMENT);
1141a1f6d91cSDavid Greenman 		}
1142df8bae1dSRodney W. Grimes 		if ((new_prot & current->max_protection) != new_prot) {
1143df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
1144df8bae1dSRodney W. Grimes 			return (KERN_PROTECTION_FAILURE);
1145df8bae1dSRodney W. Grimes 		}
1146df8bae1dSRodney W. Grimes 		current = current->next;
1147df8bae1dSRodney W. Grimes 	}
1148df8bae1dSRodney W. Grimes 
1149df8bae1dSRodney W. Grimes 	/*
11500d94caffSDavid Greenman 	 * Go back and fix up protections. [Note that clipping is not
11510d94caffSDavid Greenman 	 * necessary the second time.]
1152df8bae1dSRodney W. Grimes 	 */
1153df8bae1dSRodney W. Grimes 
1154df8bae1dSRodney W. Grimes 	current = entry;
1155df8bae1dSRodney W. Grimes 
1156df8bae1dSRodney W. Grimes 	while ((current != &map->header) && (current->start < end)) {
1157df8bae1dSRodney W. Grimes 		vm_prot_t old_prot;
1158df8bae1dSRodney W. Grimes 
1159df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, current, end);
1160df8bae1dSRodney W. Grimes 
1161df8bae1dSRodney W. Grimes 		old_prot = current->protection;
1162df8bae1dSRodney W. Grimes 		if (set_max)
1163df8bae1dSRodney W. Grimes 			current->protection =
1164df8bae1dSRodney W. Grimes 			    (current->max_protection = new_prot) &
1165df8bae1dSRodney W. Grimes 			    old_prot;
1166df8bae1dSRodney W. Grimes 		else
1167df8bae1dSRodney W. Grimes 			current->protection = new_prot;
1168df8bae1dSRodney W. Grimes 
1169df8bae1dSRodney W. Grimes 		/*
11700d94caffSDavid Greenman 		 * Update physical map if necessary. Worry about copy-on-write
11710d94caffSDavid Greenman 		 * here -- CHECK THIS XXX
1172df8bae1dSRodney W. Grimes 		 */
1173df8bae1dSRodney W. Grimes 
1174df8bae1dSRodney W. Grimes 		if (current->protection != old_prot) {
1175afa07f7eSJohn Dyson #define MASK(entry)	(((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1176df8bae1dSRodney W. Grimes 							VM_PROT_ALL)
1177df8bae1dSRodney W. Grimes 
1178df8bae1dSRodney W. Grimes 			pmap_protect(map->pmap, current->start,
1179df8bae1dSRodney W. Grimes 			    current->end,
11801c85e3dfSAlan Cox 			    current->protection & MASK(current));
1181df8bae1dSRodney W. Grimes #undef	MASK
1182df8bae1dSRodney W. Grimes 		}
11837d78abc9SJohn Dyson 
11847d78abc9SJohn Dyson 		vm_map_simplify_entry(map, current);
11857d78abc9SJohn Dyson 
1186df8bae1dSRodney W. Grimes 		current = current->next;
1187df8bae1dSRodney W. Grimes 	}
1188df8bae1dSRodney W. Grimes 
1189df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1190df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
1191df8bae1dSRodney W. Grimes }
1192df8bae1dSRodney W. Grimes 
1193df8bae1dSRodney W. Grimes /*
1194867a482dSJohn Dyson  *	vm_map_madvise:
1195867a482dSJohn Dyson  *
1196867a482dSJohn Dyson  * 	This routine traverses a processes map handling the madvise
1197f7fc307aSAlan Cox  *	system call.  Advisories are classified as either those effecting
1198f7fc307aSAlan Cox  *	the vm_map_entry structure, or those effecting the underlying
1199f7fc307aSAlan Cox  *	objects.
1200867a482dSJohn Dyson  */
1201b4309055SMatthew Dillon 
1202b4309055SMatthew Dillon int
12031b40f8c0SMatthew Dillon vm_map_madvise(
12041b40f8c0SMatthew Dillon 	vm_map_t map,
12051b40f8c0SMatthew Dillon 	vm_offset_t start,
12061b40f8c0SMatthew Dillon 	vm_offset_t end,
12071b40f8c0SMatthew Dillon 	int behav)
1208867a482dSJohn Dyson {
1209f7fc307aSAlan Cox 	vm_map_entry_t current, entry;
1210b4309055SMatthew Dillon 	int modify_map = 0;
1211867a482dSJohn Dyson 
12120cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
12130cddd8f0SMatthew Dillon 
1214b4309055SMatthew Dillon 	/*
1215b4309055SMatthew Dillon 	 * Some madvise calls directly modify the vm_map_entry, in which case
1216b4309055SMatthew Dillon 	 * we need to use an exclusive lock on the map and we need to perform
1217b4309055SMatthew Dillon 	 * various clipping operations.  Otherwise we only need a read-lock
1218b4309055SMatthew Dillon 	 * on the map.
1219b4309055SMatthew Dillon 	 */
1220f7fc307aSAlan Cox 
1221b4309055SMatthew Dillon 	switch(behav) {
1222b4309055SMatthew Dillon 	case MADV_NORMAL:
1223b4309055SMatthew Dillon 	case MADV_SEQUENTIAL:
1224b4309055SMatthew Dillon 	case MADV_RANDOM:
12254f79d873SMatthew Dillon 	case MADV_NOSYNC:
12264f79d873SMatthew Dillon 	case MADV_AUTOSYNC:
12279730a5daSPaul Saab 	case MADV_NOCORE:
12289730a5daSPaul Saab 	case MADV_CORE:
1229b4309055SMatthew Dillon 		modify_map = 1;
1230867a482dSJohn Dyson 		vm_map_lock(map);
1231b4309055SMatthew Dillon 		break;
1232b4309055SMatthew Dillon 	case MADV_WILLNEED:
1233b4309055SMatthew Dillon 	case MADV_DONTNEED:
1234b4309055SMatthew Dillon 	case MADV_FREE:
1235f7fc307aSAlan Cox 		vm_map_lock_read(map);
1236b4309055SMatthew Dillon 		break;
1237b4309055SMatthew Dillon 	default:
1238b4309055SMatthew Dillon 		return (KERN_INVALID_ARGUMENT);
1239b4309055SMatthew Dillon 	}
1240b4309055SMatthew Dillon 
1241b4309055SMatthew Dillon 	/*
1242b4309055SMatthew Dillon 	 * Locate starting entry and clip if necessary.
1243b4309055SMatthew Dillon 	 */
1244867a482dSJohn Dyson 
1245867a482dSJohn Dyson 	VM_MAP_RANGE_CHECK(map, start, end);
1246867a482dSJohn Dyson 
1247867a482dSJohn Dyson 	if (vm_map_lookup_entry(map, start, &entry)) {
1248f7fc307aSAlan Cox 		if (modify_map)
1249867a482dSJohn Dyson 			vm_map_clip_start(map, entry, start);
1250b4309055SMatthew Dillon 	} else {
1251867a482dSJohn Dyson 		entry = entry->next;
1252b4309055SMatthew Dillon 	}
1253867a482dSJohn Dyson 
1254f7fc307aSAlan Cox 	if (modify_map) {
1255f7fc307aSAlan Cox 		/*
1256f7fc307aSAlan Cox 		 * madvise behaviors that are implemented in the vm_map_entry.
1257f7fc307aSAlan Cox 		 *
1258f7fc307aSAlan Cox 		 * We clip the vm_map_entry so that behavioral changes are
1259f7fc307aSAlan Cox 		 * limited to the specified address range.
1260f7fc307aSAlan Cox 		 */
1261867a482dSJohn Dyson 		for (current = entry;
1262867a482dSJohn Dyson 		     (current != &map->header) && (current->start < end);
1263b4309055SMatthew Dillon 		     current = current->next
1264b4309055SMatthew Dillon 		) {
1265f7fc307aSAlan Cox 			if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1266867a482dSJohn Dyson 				continue;
1267fed9a903SJohn Dyson 
126847221757SJohn Dyson 			vm_map_clip_end(map, current, end);
1269fed9a903SJohn Dyson 
1270f7fc307aSAlan Cox 			switch (behav) {
1271867a482dSJohn Dyson 			case MADV_NORMAL:
12727f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
1273867a482dSJohn Dyson 				break;
1274867a482dSJohn Dyson 			case MADV_SEQUENTIAL:
12757f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
1276867a482dSJohn Dyson 				break;
1277867a482dSJohn Dyson 			case MADV_RANDOM:
12787f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
1279867a482dSJohn Dyson 				break;
12804f79d873SMatthew Dillon 			case MADV_NOSYNC:
12814f79d873SMatthew Dillon 				current->eflags |= MAP_ENTRY_NOSYNC;
12824f79d873SMatthew Dillon 				break;
12834f79d873SMatthew Dillon 			case MADV_AUTOSYNC:
12844f79d873SMatthew Dillon 				current->eflags &= ~MAP_ENTRY_NOSYNC;
12854f79d873SMatthew Dillon 				break;
12869730a5daSPaul Saab 			case MADV_NOCORE:
12879730a5daSPaul Saab 				current->eflags |= MAP_ENTRY_NOCOREDUMP;
12889730a5daSPaul Saab 				break;
12899730a5daSPaul Saab 			case MADV_CORE:
12909730a5daSPaul Saab 				current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
12919730a5daSPaul Saab 				break;
1292867a482dSJohn Dyson 			default:
1293867a482dSJohn Dyson 				break;
1294867a482dSJohn Dyson 			}
1295f7fc307aSAlan Cox 			vm_map_simplify_entry(map, current);
1296867a482dSJohn Dyson 		}
1297867a482dSJohn Dyson 		vm_map_unlock(map);
1298b4309055SMatthew Dillon 	} else {
1299f7fc307aSAlan Cox 		vm_pindex_t pindex;
1300f7fc307aSAlan Cox 		int count;
1301f7fc307aSAlan Cox 
1302f7fc307aSAlan Cox 		/*
1303f7fc307aSAlan Cox 		 * madvise behaviors that are implemented in the underlying
1304f7fc307aSAlan Cox 		 * vm_object.
1305f7fc307aSAlan Cox 		 *
1306f7fc307aSAlan Cox 		 * Since we don't clip the vm_map_entry, we have to clip
1307f7fc307aSAlan Cox 		 * the vm_object pindex and count.
1308f7fc307aSAlan Cox 		 */
1309f7fc307aSAlan Cox 		for (current = entry;
1310f7fc307aSAlan Cox 		     (current != &map->header) && (current->start < end);
1311b4309055SMatthew Dillon 		     current = current->next
1312b4309055SMatthew Dillon 		) {
13135f99b57cSMatthew Dillon 			vm_offset_t useStart;
13145f99b57cSMatthew Dillon 
1315f7fc307aSAlan Cox 			if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1316f7fc307aSAlan Cox 				continue;
1317f7fc307aSAlan Cox 
1318f7fc307aSAlan Cox 			pindex = OFF_TO_IDX(current->offset);
1319f7fc307aSAlan Cox 			count = atop(current->end - current->start);
13205f99b57cSMatthew Dillon 			useStart = current->start;
1321f7fc307aSAlan Cox 
1322f7fc307aSAlan Cox 			if (current->start < start) {
1323f7fc307aSAlan Cox 				pindex += atop(start - current->start);
1324f7fc307aSAlan Cox 				count -= atop(start - current->start);
13255f99b57cSMatthew Dillon 				useStart = start;
1326f7fc307aSAlan Cox 			}
1327f7fc307aSAlan Cox 			if (current->end > end)
1328f7fc307aSAlan Cox 				count -= atop(current->end - end);
1329f7fc307aSAlan Cox 
1330f7fc307aSAlan Cox 			if (count <= 0)
1331f7fc307aSAlan Cox 				continue;
1332f7fc307aSAlan Cox 
1333f7fc307aSAlan Cox 			vm_object_madvise(current->object.vm_object,
1334f7fc307aSAlan Cox 					  pindex, count, behav);
1335b4309055SMatthew Dillon 			if (behav == MADV_WILLNEED) {
1336b4309055SMatthew Dillon 				pmap_object_init_pt(
1337b4309055SMatthew Dillon 				    map->pmap,
13385f99b57cSMatthew Dillon 				    useStart,
1339f7fc307aSAlan Cox 				    current->object.vm_object,
1340b4309055SMatthew Dillon 				    pindex,
1341b4309055SMatthew Dillon 				    (count << PAGE_SHIFT),
1342e3026983SMatthew Dillon 				    MAP_PREFAULT_MADVISE
1343b4309055SMatthew Dillon 				);
1344f7fc307aSAlan Cox 			}
1345f7fc307aSAlan Cox 		}
1346f7fc307aSAlan Cox 		vm_map_unlock_read(map);
1347f7fc307aSAlan Cox 	}
1348b4309055SMatthew Dillon 	return(0);
1349867a482dSJohn Dyson }
1350867a482dSJohn Dyson 
1351867a482dSJohn Dyson 
1352867a482dSJohn Dyson /*
1353df8bae1dSRodney W. Grimes  *	vm_map_inherit:
1354df8bae1dSRodney W. Grimes  *
1355df8bae1dSRodney W. Grimes  *	Sets the inheritance of the specified address
1356df8bae1dSRodney W. Grimes  *	range in the target map.  Inheritance
1357df8bae1dSRodney W. Grimes  *	affects how the map will be shared with
1358df8bae1dSRodney W. Grimes  *	child maps at the time of vm_map_fork.
1359df8bae1dSRodney W. Grimes  */
1360df8bae1dSRodney W. Grimes int
1361b9dcd593SBruce Evans vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
1362b9dcd593SBruce Evans 	       vm_inherit_t new_inheritance)
1363df8bae1dSRodney W. Grimes {
1364c0877f10SJohn Dyson 	vm_map_entry_t entry;
1365df8bae1dSRodney W. Grimes 	vm_map_entry_t temp_entry;
1366df8bae1dSRodney W. Grimes 
13670cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
13680cddd8f0SMatthew Dillon 
1369df8bae1dSRodney W. Grimes 	switch (new_inheritance) {
1370df8bae1dSRodney W. Grimes 	case VM_INHERIT_NONE:
1371df8bae1dSRodney W. Grimes 	case VM_INHERIT_COPY:
1372df8bae1dSRodney W. Grimes 	case VM_INHERIT_SHARE:
1373df8bae1dSRodney W. Grimes 		break;
1374df8bae1dSRodney W. Grimes 	default:
1375df8bae1dSRodney W. Grimes 		return (KERN_INVALID_ARGUMENT);
1376df8bae1dSRodney W. Grimes 	}
1377df8bae1dSRodney W. Grimes 
1378df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1379df8bae1dSRodney W. Grimes 
1380df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1381df8bae1dSRodney W. Grimes 
1382df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &temp_entry)) {
1383df8bae1dSRodney W. Grimes 		entry = temp_entry;
1384df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
13850d94caffSDavid Greenman 	} else
1386df8bae1dSRodney W. Grimes 		entry = temp_entry->next;
1387df8bae1dSRodney W. Grimes 
1388df8bae1dSRodney W. Grimes 	while ((entry != &map->header) && (entry->start < end)) {
1389df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, entry, end);
1390df8bae1dSRodney W. Grimes 
1391df8bae1dSRodney W. Grimes 		entry->inheritance = new_inheritance;
1392df8bae1dSRodney W. Grimes 
139344428f62SAlan Cox 		vm_map_simplify_entry(map, entry);
139444428f62SAlan Cox 
1395df8bae1dSRodney W. Grimes 		entry = entry->next;
1396df8bae1dSRodney W. Grimes 	}
1397df8bae1dSRodney W. Grimes 
1398df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1399df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
1400df8bae1dSRodney W. Grimes }
1401df8bae1dSRodney W. Grimes 
1402df8bae1dSRodney W. Grimes /*
14037aaaa4fdSJohn Dyson  * Implement the semantics of mlock
14047aaaa4fdSJohn Dyson  */
14057aaaa4fdSJohn Dyson int
14061b40f8c0SMatthew Dillon vm_map_user_pageable(
14071b40f8c0SMatthew Dillon 	vm_map_t map,
14081b40f8c0SMatthew Dillon 	vm_offset_t start,
14091b40f8c0SMatthew Dillon 	vm_offset_t end,
14101b40f8c0SMatthew Dillon 	boolean_t new_pageable)
14117aaaa4fdSJohn Dyson {
1412b44959ceSTor Egge 	vm_map_entry_t entry;
14137aaaa4fdSJohn Dyson 	vm_map_entry_t start_entry;
1414b44959ceSTor Egge 	vm_offset_t estart;
1415e7673b84STor Egge 	vm_offset_t eend;
14167aaaa4fdSJohn Dyson 	int rv;
14177aaaa4fdSJohn Dyson 
14187aaaa4fdSJohn Dyson 	vm_map_lock(map);
14197aaaa4fdSJohn Dyson 	VM_MAP_RANGE_CHECK(map, start, end);
14207aaaa4fdSJohn Dyson 
14217aaaa4fdSJohn Dyson 	if (vm_map_lookup_entry(map, start, &start_entry) == FALSE) {
14227aaaa4fdSJohn Dyson 		vm_map_unlock(map);
14237aaaa4fdSJohn Dyson 		return (KERN_INVALID_ADDRESS);
14247aaaa4fdSJohn Dyson 	}
14257aaaa4fdSJohn Dyson 
14267aaaa4fdSJohn Dyson 	if (new_pageable) {
14277aaaa4fdSJohn Dyson 
14287aaaa4fdSJohn Dyson 		entry = start_entry;
14297aaaa4fdSJohn Dyson 		vm_map_clip_start(map, entry, start);
14307aaaa4fdSJohn Dyson 
14317aaaa4fdSJohn Dyson 		/*
14327aaaa4fdSJohn Dyson 		 * Now decrement the wiring count for each region. If a region
14337aaaa4fdSJohn Dyson 		 * becomes completely unwired, unwire its physical pages and
14347aaaa4fdSJohn Dyson 		 * mappings.
14357aaaa4fdSJohn Dyson 		 */
14367aaaa4fdSJohn Dyson 		while ((entry != &map->header) && (entry->start < end)) {
1437afa07f7eSJohn Dyson 			if (entry->eflags & MAP_ENTRY_USER_WIRED) {
14387aaaa4fdSJohn Dyson 				vm_map_clip_end(map, entry, end);
1439afa07f7eSJohn Dyson 				entry->eflags &= ~MAP_ENTRY_USER_WIRED;
14407aaaa4fdSJohn Dyson 				entry->wired_count--;
14417aaaa4fdSJohn Dyson 				if (entry->wired_count == 0)
14427aaaa4fdSJohn Dyson 					vm_fault_unwire(map, entry->start, entry->end);
14437aaaa4fdSJohn Dyson 			}
1444b44959ceSTor Egge 			vm_map_simplify_entry(map,entry);
14457aaaa4fdSJohn Dyson 			entry = entry->next;
14467aaaa4fdSJohn Dyson 		}
14477aaaa4fdSJohn Dyson 	} else {
14487aaaa4fdSJohn Dyson 
14497aaaa4fdSJohn Dyson 		entry = start_entry;
14507aaaa4fdSJohn Dyson 
14517aaaa4fdSJohn Dyson 		while ((entry != &map->header) && (entry->start < end)) {
14527aaaa4fdSJohn Dyson 
1453afa07f7eSJohn Dyson 			if (entry->eflags & MAP_ENTRY_USER_WIRED) {
14547aaaa4fdSJohn Dyson 				entry = entry->next;
14557aaaa4fdSJohn Dyson 				continue;
14567aaaa4fdSJohn Dyson 			}
14577aaaa4fdSJohn Dyson 
14587aaaa4fdSJohn Dyson 			if (entry->wired_count != 0) {
14597aaaa4fdSJohn Dyson 				entry->wired_count++;
1460afa07f7eSJohn Dyson 				entry->eflags |= MAP_ENTRY_USER_WIRED;
14617aaaa4fdSJohn Dyson 				entry = entry->next;
14627aaaa4fdSJohn Dyson 				continue;
14637aaaa4fdSJohn Dyson 			}
14647aaaa4fdSJohn Dyson 
14657aaaa4fdSJohn Dyson 			/* Here on entry being newly wired */
14667aaaa4fdSJohn Dyson 
14679fdfe602SMatthew Dillon 			if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1468afa07f7eSJohn Dyson 				int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY;
14697aaaa4fdSJohn Dyson 				if (copyflag && ((entry->protection & VM_PROT_WRITE) != 0)) {
14707aaaa4fdSJohn Dyson 
14717aaaa4fdSJohn Dyson 					vm_object_shadow(&entry->object.vm_object,
14727aaaa4fdSJohn Dyson 					    &entry->offset,
1473c2e11a03SJohn Dyson 					    atop(entry->end - entry->start));
1474afa07f7eSJohn Dyson 					entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
14757aaaa4fdSJohn Dyson 
14764e71e795SMatthew Dillon 				} else if (entry->object.vm_object == NULL &&
14774e71e795SMatthew Dillon 					   !map->system_map) {
14787aaaa4fdSJohn Dyson 
14797aaaa4fdSJohn Dyson 					entry->object.vm_object =
14807aaaa4fdSJohn Dyson 					    vm_object_allocate(OBJT_DEFAULT,
1481c2e11a03SJohn Dyson 						atop(entry->end - entry->start));
14827aaaa4fdSJohn Dyson 					entry->offset = (vm_offset_t) 0;
14837aaaa4fdSJohn Dyson 
14847aaaa4fdSJohn Dyson 				}
14857aaaa4fdSJohn Dyson 			}
14867aaaa4fdSJohn Dyson 
14877aaaa4fdSJohn Dyson 			vm_map_clip_start(map, entry, start);
14887aaaa4fdSJohn Dyson 			vm_map_clip_end(map, entry, end);
14897aaaa4fdSJohn Dyson 
14907aaaa4fdSJohn Dyson 			entry->wired_count++;
1491afa07f7eSJohn Dyson 			entry->eflags |= MAP_ENTRY_USER_WIRED;
1492b44959ceSTor Egge 			estart = entry->start;
1493e7673b84STor Egge 			eend = entry->end;
14947aaaa4fdSJohn Dyson 
14957aaaa4fdSJohn Dyson 			/* First we need to allow map modifications */
1496996c772fSJohn Dyson 			vm_map_set_recursive(map);
149703e9c6c1SJohn Dyson 			vm_map_lock_downgrade(map);
149847221757SJohn Dyson 			map->timestamp++;
14997aaaa4fdSJohn Dyson 
15007aaaa4fdSJohn Dyson 			rv = vm_fault_user_wire(map, entry->start, entry->end);
15017aaaa4fdSJohn Dyson 			if (rv) {
15027aaaa4fdSJohn Dyson 
15037aaaa4fdSJohn Dyson 				entry->wired_count--;
1504afa07f7eSJohn Dyson 				entry->eflags &= ~MAP_ENTRY_USER_WIRED;
15057aaaa4fdSJohn Dyson 
1506996c772fSJohn Dyson 				vm_map_clear_recursive(map);
15077aaaa4fdSJohn Dyson 				vm_map_unlock(map);
15087aaaa4fdSJohn Dyson 
1509e7673b84STor Egge 				/*
1510e7673b84STor Egge 				 * At this point, the map is unlocked, and
1511e7673b84STor Egge 				 * entry might no longer be valid.  Use copy
1512e7673b84STor Egge 				 * of entry start value obtained while entry
1513e7673b84STor Egge 				 * was valid.
1514e7673b84STor Egge 				 */
1515e7673b84STor Egge 				(void) vm_map_user_pageable(map, start, estart,
1516e7673b84STor Egge 							    TRUE);
15177aaaa4fdSJohn Dyson 				return rv;
15187aaaa4fdSJohn Dyson 			}
15197aaaa4fdSJohn Dyson 
1520996c772fSJohn Dyson 			vm_map_clear_recursive(map);
1521b44959ceSTor Egge 			if (vm_map_lock_upgrade(map)) {
1522b44959ceSTor Egge 				vm_map_lock(map);
1523b44959ceSTor Egge 				if (vm_map_lookup_entry(map, estart, &entry)
1524b44959ceSTor Egge 				    == FALSE) {
1525b44959ceSTor Egge 					vm_map_unlock(map);
1526e7673b84STor Egge 					/*
1527e7673b84STor Egge 					 * vm_fault_user_wire succeded, thus
1528e7673b84STor Egge 					 * the area between start and eend
1529e7673b84STor Egge 					 * is wired and has to be unwired
1530e7673b84STor Egge 					 * here as part of the cleanup.
1531e7673b84STor Egge 					 */
1532b44959ceSTor Egge 					(void) vm_map_user_pageable(map,
1533b44959ceSTor Egge 								    start,
1534e7673b84STor Egge 								    eend,
1535b44959ceSTor Egge 								    TRUE);
1536b44959ceSTor Egge 					return (KERN_INVALID_ADDRESS);
1537b44959ceSTor Egge 				}
1538b44959ceSTor Egge 			}
1539b44959ceSTor Egge 			vm_map_simplify_entry(map,entry);
15407aaaa4fdSJohn Dyson 		}
15417aaaa4fdSJohn Dyson 	}
154247221757SJohn Dyson 	map->timestamp++;
15437aaaa4fdSJohn Dyson 	vm_map_unlock(map);
15447aaaa4fdSJohn Dyson 	return KERN_SUCCESS;
15457aaaa4fdSJohn Dyson }
15467aaaa4fdSJohn Dyson 
15477aaaa4fdSJohn Dyson /*
1548df8bae1dSRodney W. Grimes  *	vm_map_pageable:
1549df8bae1dSRodney W. Grimes  *
1550df8bae1dSRodney W. Grimes  *	Sets the pageability of the specified address
1551df8bae1dSRodney W. Grimes  *	range in the target map.  Regions specified
1552df8bae1dSRodney W. Grimes  *	as not pageable require locked-down physical
1553df8bae1dSRodney W. Grimes  *	memory and physical page maps.
1554df8bae1dSRodney W. Grimes  *
1555df8bae1dSRodney W. Grimes  *	The map must not be locked, but a reference
1556df8bae1dSRodney W. Grimes  *	must remain to the map throughout the call.
1557df8bae1dSRodney W. Grimes  */
1558df8bae1dSRodney W. Grimes int
15591b40f8c0SMatthew Dillon vm_map_pageable(
15601b40f8c0SMatthew Dillon 	vm_map_t map,
15611b40f8c0SMatthew Dillon 	vm_offset_t start,
15621b40f8c0SMatthew Dillon 	vm_offset_t end,
15631b40f8c0SMatthew Dillon 	boolean_t new_pageable)
1564df8bae1dSRodney W. Grimes {
1565c0877f10SJohn Dyson 	vm_map_entry_t entry;
1566df8bae1dSRodney W. Grimes 	vm_map_entry_t start_entry;
1567c0877f10SJohn Dyson 	vm_offset_t failed = 0;
1568df8bae1dSRodney W. Grimes 	int rv;
1569df8bae1dSRodney W. Grimes 
15700cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
15710cddd8f0SMatthew Dillon 
1572df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1573df8bae1dSRodney W. Grimes 
1574df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1575df8bae1dSRodney W. Grimes 
1576df8bae1dSRodney W. Grimes 	/*
15770d94caffSDavid Greenman 	 * Only one pageability change may take place at one time, since
15780d94caffSDavid Greenman 	 * vm_fault assumes it will be called only once for each
15790d94caffSDavid Greenman 	 * wiring/unwiring.  Therefore, we have to make sure we're actually
15800d94caffSDavid Greenman 	 * changing the pageability for the entire region.  We do so before
15810d94caffSDavid Greenman 	 * making any changes.
1582df8bae1dSRodney W. Grimes 	 */
1583df8bae1dSRodney W. Grimes 
1584df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &start_entry) == FALSE) {
1585df8bae1dSRodney W. Grimes 		vm_map_unlock(map);
1586df8bae1dSRodney W. Grimes 		return (KERN_INVALID_ADDRESS);
1587df8bae1dSRodney W. Grimes 	}
1588df8bae1dSRodney W. Grimes 	entry = start_entry;
1589df8bae1dSRodney W. Grimes 
1590df8bae1dSRodney W. Grimes 	/*
15910d94caffSDavid Greenman 	 * Actions are rather different for wiring and unwiring, so we have
15920d94caffSDavid Greenman 	 * two separate cases.
1593df8bae1dSRodney W. Grimes 	 */
1594df8bae1dSRodney W. Grimes 
1595df8bae1dSRodney W. Grimes 	if (new_pageable) {
1596df8bae1dSRodney W. Grimes 
1597df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
1598df8bae1dSRodney W. Grimes 
1599df8bae1dSRodney W. Grimes 		/*
16000d94caffSDavid Greenman 		 * Unwiring.  First ensure that the range to be unwired is
16010d94caffSDavid Greenman 		 * really wired down and that there are no holes.
1602df8bae1dSRodney W. Grimes 		 */
1603df8bae1dSRodney W. Grimes 		while ((entry != &map->header) && (entry->start < end)) {
1604df8bae1dSRodney W. Grimes 
1605df8bae1dSRodney W. Grimes 			if (entry->wired_count == 0 ||
1606df8bae1dSRodney W. Grimes 			    (entry->end < end &&
1607df8bae1dSRodney W. Grimes 				(entry->next == &map->header ||
1608df8bae1dSRodney W. Grimes 				    entry->next->start > entry->end))) {
1609df8bae1dSRodney W. Grimes 				vm_map_unlock(map);
1610df8bae1dSRodney W. Grimes 				return (KERN_INVALID_ARGUMENT);
1611df8bae1dSRodney W. Grimes 			}
1612df8bae1dSRodney W. Grimes 			entry = entry->next;
1613df8bae1dSRodney W. Grimes 		}
1614df8bae1dSRodney W. Grimes 
1615df8bae1dSRodney W. Grimes 		/*
16160d94caffSDavid Greenman 		 * Now decrement the wiring count for each region. If a region
16170d94caffSDavid Greenman 		 * becomes completely unwired, unwire its physical pages and
16180d94caffSDavid Greenman 		 * mappings.
1619df8bae1dSRodney W. Grimes 		 */
1620df8bae1dSRodney W. Grimes 		entry = start_entry;
1621df8bae1dSRodney W. Grimes 		while ((entry != &map->header) && (entry->start < end)) {
1622df8bae1dSRodney W. Grimes 			vm_map_clip_end(map, entry, end);
1623df8bae1dSRodney W. Grimes 
1624df8bae1dSRodney W. Grimes 			entry->wired_count--;
1625df8bae1dSRodney W. Grimes 			if (entry->wired_count == 0)
1626df8bae1dSRodney W. Grimes 				vm_fault_unwire(map, entry->start, entry->end);
1627df8bae1dSRodney W. Grimes 
162844428f62SAlan Cox 			vm_map_simplify_entry(map, entry);
162944428f62SAlan Cox 
1630df8bae1dSRodney W. Grimes 			entry = entry->next;
1631df8bae1dSRodney W. Grimes 		}
16320d94caffSDavid Greenman 	} else {
1633df8bae1dSRodney W. Grimes 		/*
1634df8bae1dSRodney W. Grimes 		 * Wiring.  We must do this in two passes:
1635df8bae1dSRodney W. Grimes 		 *
16360d94caffSDavid Greenman 		 * 1.  Holding the write lock, we create any shadow or zero-fill
16370d94caffSDavid Greenman 		 * objects that need to be created. Then we clip each map
16380d94caffSDavid Greenman 		 * entry to the region to be wired and increment its wiring
16390d94caffSDavid Greenman 		 * count.  We create objects before clipping the map entries
1640df8bae1dSRodney W. Grimes 		 * to avoid object proliferation.
1641df8bae1dSRodney W. Grimes 		 *
16420d94caffSDavid Greenman 		 * 2.  We downgrade to a read lock, and call vm_fault_wire to
16430d94caffSDavid Greenman 		 * fault in the pages for any newly wired area (wired_count is
16440d94caffSDavid Greenman 		 * 1).
1645df8bae1dSRodney W. Grimes 		 *
16460d94caffSDavid Greenman 		 * Downgrading to a read lock for vm_fault_wire avoids a possible
164724a1cce3SDavid Greenman 		 * deadlock with another process that may have faulted on one
16480d94caffSDavid Greenman 		 * of the pages to be wired (it would mark the page busy,
16490d94caffSDavid Greenman 		 * blocking us, then in turn block on the map lock that we
16500d94caffSDavid Greenman 		 * hold).  Because of problems in the recursive lock package,
16510d94caffSDavid Greenman 		 * we cannot upgrade to a write lock in vm_map_lookup.  Thus,
16520d94caffSDavid Greenman 		 * any actions that require the write lock must be done
16530d94caffSDavid Greenman 		 * beforehand.  Because we keep the read lock on the map, the
16540d94caffSDavid Greenman 		 * copy-on-write status of the entries we modify here cannot
16550d94caffSDavid Greenman 		 * change.
1656df8bae1dSRodney W. Grimes 		 */
1657df8bae1dSRodney W. Grimes 
1658df8bae1dSRodney W. Grimes 		/*
1659df8bae1dSRodney W. Grimes 		 * Pass 1.
1660df8bae1dSRodney W. Grimes 		 */
1661df8bae1dSRodney W. Grimes 		while ((entry != &map->header) && (entry->start < end)) {
1662df8bae1dSRodney W. Grimes 			if (entry->wired_count == 0) {
1663df8bae1dSRodney W. Grimes 
1664df8bae1dSRodney W. Grimes 				/*
1665df8bae1dSRodney W. Grimes 				 * Perform actions of vm_map_lookup that need
1666df8bae1dSRodney W. Grimes 				 * the write lock on the map: create a shadow
1667df8bae1dSRodney W. Grimes 				 * object for a copy-on-write region, or an
1668df8bae1dSRodney W. Grimes 				 * object for a zero-fill region.
1669df8bae1dSRodney W. Grimes 				 *
1670df8bae1dSRodney W. Grimes 				 * We don't have to do this for entries that
1671ad5fca3bSAlan Cox 				 * point to sub maps, because we won't
1672ad5fca3bSAlan Cox 				 * hold the lock on the sub map.
1673df8bae1dSRodney W. Grimes 				 */
16749fdfe602SMatthew Dillon 				if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1675afa07f7eSJohn Dyson 					int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY;
1676b5b40fa6SJohn Dyson 					if (copyflag &&
1677df8bae1dSRodney W. Grimes 					    ((entry->protection & VM_PROT_WRITE) != 0)) {
1678df8bae1dSRodney W. Grimes 
1679df8bae1dSRodney W. Grimes 						vm_object_shadow(&entry->object.vm_object,
1680df8bae1dSRodney W. Grimes 						    &entry->offset,
1681c2e11a03SJohn Dyson 						    atop(entry->end - entry->start));
1682afa07f7eSJohn Dyson 						entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
16834e71e795SMatthew Dillon 					} else if (entry->object.vm_object == NULL &&
16844e71e795SMatthew Dillon 						   !map->system_map) {
1685df8bae1dSRodney W. Grimes 						entry->object.vm_object =
1686a316d390SJohn Dyson 						    vm_object_allocate(OBJT_DEFAULT,
1687c2e11a03SJohn Dyson 							atop(entry->end - entry->start));
1688df8bae1dSRodney W. Grimes 						entry->offset = (vm_offset_t) 0;
1689df8bae1dSRodney W. Grimes 					}
1690df8bae1dSRodney W. Grimes 				}
1691df8bae1dSRodney W. Grimes 			}
1692df8bae1dSRodney W. Grimes 			vm_map_clip_start(map, entry, start);
1693df8bae1dSRodney W. Grimes 			vm_map_clip_end(map, entry, end);
1694df8bae1dSRodney W. Grimes 			entry->wired_count++;
1695df8bae1dSRodney W. Grimes 
1696df8bae1dSRodney W. Grimes 			/*
1697df8bae1dSRodney W. Grimes 			 * Check for holes
1698df8bae1dSRodney W. Grimes 			 */
1699df8bae1dSRodney W. Grimes 			if (entry->end < end &&
1700df8bae1dSRodney W. Grimes 			    (entry->next == &map->header ||
1701df8bae1dSRodney W. Grimes 				entry->next->start > entry->end)) {
1702df8bae1dSRodney W. Grimes 				/*
17030d94caffSDavid Greenman 				 * Found one.  Object creation actions do not
17040d94caffSDavid Greenman 				 * need to be undone, but the wired counts
17050d94caffSDavid Greenman 				 * need to be restored.
1706df8bae1dSRodney W. Grimes 				 */
1707df8bae1dSRodney W. Grimes 				while (entry != &map->header && entry->end > start) {
1708df8bae1dSRodney W. Grimes 					entry->wired_count--;
1709df8bae1dSRodney W. Grimes 					entry = entry->prev;
1710df8bae1dSRodney W. Grimes 				}
1711df8bae1dSRodney W. Grimes 				vm_map_unlock(map);
1712df8bae1dSRodney W. Grimes 				return (KERN_INVALID_ARGUMENT);
1713df8bae1dSRodney W. Grimes 			}
1714df8bae1dSRodney W. Grimes 			entry = entry->next;
1715df8bae1dSRodney W. Grimes 		}
1716df8bae1dSRodney W. Grimes 
1717df8bae1dSRodney W. Grimes 		/*
1718df8bae1dSRodney W. Grimes 		 * Pass 2.
1719df8bae1dSRodney W. Grimes 		 */
1720df8bae1dSRodney W. Grimes 
1721df8bae1dSRodney W. Grimes 		/*
1722df8bae1dSRodney W. Grimes 		 * HACK HACK HACK HACK
1723df8bae1dSRodney W. Grimes 		 *
172424a1cce3SDavid Greenman 		 * If we are wiring in the kernel map or a submap of it,
172524a1cce3SDavid Greenman 		 * unlock the map to avoid deadlocks.  We trust that the
172624a1cce3SDavid Greenman 		 * kernel is well-behaved, and therefore will not do
172724a1cce3SDavid Greenman 		 * anything destructive to this region of the map while
172824a1cce3SDavid Greenman 		 * we have it unlocked.  We cannot trust user processes
172924a1cce3SDavid Greenman 		 * to do the same.
1730df8bae1dSRodney W. Grimes 		 *
1731df8bae1dSRodney W. Grimes 		 * HACK HACK HACK HACK
1732df8bae1dSRodney W. Grimes 		 */
1733df8bae1dSRodney W. Grimes 		if (vm_map_pmap(map) == kernel_pmap) {
1734df8bae1dSRodney W. Grimes 			vm_map_unlock(map);	/* trust me ... */
17350d94caffSDavid Greenman 		} else {
173603e9c6c1SJohn Dyson 			vm_map_lock_downgrade(map);
1737df8bae1dSRodney W. Grimes 		}
1738df8bae1dSRodney W. Grimes 
1739df8bae1dSRodney W. Grimes 		rv = 0;
1740df8bae1dSRodney W. Grimes 		entry = start_entry;
1741df8bae1dSRodney W. Grimes 		while (entry != &map->header && entry->start < end) {
1742df8bae1dSRodney W. Grimes 			/*
17430d94caffSDavid Greenman 			 * If vm_fault_wire fails for any page we need to undo
17440d94caffSDavid Greenman 			 * what has been done.  We decrement the wiring count
17450d94caffSDavid Greenman 			 * for those pages which have not yet been wired (now)
17460d94caffSDavid Greenman 			 * and unwire those that have (later).
1747df8bae1dSRodney W. Grimes 			 *
1748df8bae1dSRodney W. Grimes 			 * XXX this violates the locking protocol on the map,
1749df8bae1dSRodney W. Grimes 			 * needs to be fixed.
1750df8bae1dSRodney W. Grimes 			 */
1751df8bae1dSRodney W. Grimes 			if (rv)
1752df8bae1dSRodney W. Grimes 				entry->wired_count--;
1753df8bae1dSRodney W. Grimes 			else if (entry->wired_count == 1) {
1754df8bae1dSRodney W. Grimes 				rv = vm_fault_wire(map, entry->start, entry->end);
1755df8bae1dSRodney W. Grimes 				if (rv) {
1756df8bae1dSRodney W. Grimes 					failed = entry->start;
1757df8bae1dSRodney W. Grimes 					entry->wired_count--;
1758df8bae1dSRodney W. Grimes 				}
1759df8bae1dSRodney W. Grimes 			}
1760df8bae1dSRodney W. Grimes 			entry = entry->next;
1761df8bae1dSRodney W. Grimes 		}
1762df8bae1dSRodney W. Grimes 
1763df8bae1dSRodney W. Grimes 		if (vm_map_pmap(map) == kernel_pmap) {
1764df8bae1dSRodney W. Grimes 			vm_map_lock(map);
1765df8bae1dSRodney W. Grimes 		}
1766df8bae1dSRodney W. Grimes 		if (rv) {
1767df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
1768df8bae1dSRodney W. Grimes 			(void) vm_map_pageable(map, start, failed, TRUE);
1769df8bae1dSRodney W. Grimes 			return (rv);
1770df8bae1dSRodney W. Grimes 		}
1771e7673b84STor Egge 		/*
1772e7673b84STor Egge 		 * An exclusive lock on the map is needed in order to call
1773e7673b84STor Egge 		 * vm_map_simplify_entry().  If the current lock on the map
1774e7673b84STor Egge 		 * is only a shared lock, an upgrade is needed.
1775e7673b84STor Egge 		 */
1776e7673b84STor Egge 		if (vm_map_pmap(map) != kernel_pmap &&
1777e7673b84STor Egge 		    vm_map_lock_upgrade(map)) {
1778e7673b84STor Egge 			vm_map_lock(map);
1779e7673b84STor Egge 			if (vm_map_lookup_entry(map, start, &start_entry) ==
1780e7673b84STor Egge 			    FALSE) {
1781e7673b84STor Egge 				vm_map_unlock(map);
1782e7673b84STor Egge 				return KERN_SUCCESS;
1783e7673b84STor Egge 			}
1784e7673b84STor Egge 		}
1785b7b2aac2SJohn Dyson 		vm_map_simplify_entry(map, start_entry);
1786df8bae1dSRodney W. Grimes 	}
1787df8bae1dSRodney W. Grimes 
1788df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1789df8bae1dSRodney W. Grimes 
1790df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
1791df8bae1dSRodney W. Grimes }
1792df8bae1dSRodney W. Grimes 
1793df8bae1dSRodney W. Grimes /*
1794df8bae1dSRodney W. Grimes  * vm_map_clean
1795df8bae1dSRodney W. Grimes  *
1796df8bae1dSRodney W. Grimes  * Push any dirty cached pages in the address range to their pager.
1797df8bae1dSRodney W. Grimes  * If syncio is TRUE, dirty pages are written synchronously.
1798df8bae1dSRodney W. Grimes  * If invalidate is TRUE, any cached pages are freed as well.
1799df8bae1dSRodney W. Grimes  *
1800df8bae1dSRodney W. Grimes  * Returns an error if any part of the specified range is not mapped.
1801df8bae1dSRodney W. Grimes  */
1802df8bae1dSRodney W. Grimes int
18031b40f8c0SMatthew Dillon vm_map_clean(
18041b40f8c0SMatthew Dillon 	vm_map_t map,
18051b40f8c0SMatthew Dillon 	vm_offset_t start,
18061b40f8c0SMatthew Dillon 	vm_offset_t end,
18071b40f8c0SMatthew Dillon 	boolean_t syncio,
18081b40f8c0SMatthew Dillon 	boolean_t invalidate)
1809df8bae1dSRodney W. Grimes {
1810c0877f10SJohn Dyson 	vm_map_entry_t current;
1811df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
1812df8bae1dSRodney W. Grimes 	vm_size_t size;
1813df8bae1dSRodney W. Grimes 	vm_object_t object;
1814a316d390SJohn Dyson 	vm_ooffset_t offset;
1815df8bae1dSRodney W. Grimes 
18160cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
18170cddd8f0SMatthew Dillon 
1818df8bae1dSRodney W. Grimes 	vm_map_lock_read(map);
1819df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1820df8bae1dSRodney W. Grimes 	if (!vm_map_lookup_entry(map, start, &entry)) {
1821df8bae1dSRodney W. Grimes 		vm_map_unlock_read(map);
1822df8bae1dSRodney W. Grimes 		return (KERN_INVALID_ADDRESS);
1823df8bae1dSRodney W. Grimes 	}
1824df8bae1dSRodney W. Grimes 	/*
1825df8bae1dSRodney W. Grimes 	 * Make a first pass to check for holes.
1826df8bae1dSRodney W. Grimes 	 */
1827df8bae1dSRodney W. Grimes 	for (current = entry; current->start < end; current = current->next) {
1828afa07f7eSJohn Dyson 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1829df8bae1dSRodney W. Grimes 			vm_map_unlock_read(map);
1830df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ARGUMENT);
1831df8bae1dSRodney W. Grimes 		}
1832df8bae1dSRodney W. Grimes 		if (end > current->end &&
1833df8bae1dSRodney W. Grimes 		    (current->next == &map->header ||
1834df8bae1dSRodney W. Grimes 			current->end != current->next->start)) {
1835df8bae1dSRodney W. Grimes 			vm_map_unlock_read(map);
1836df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ADDRESS);
1837df8bae1dSRodney W. Grimes 		}
1838df8bae1dSRodney W. Grimes 	}
1839df8bae1dSRodney W. Grimes 
1840cf2819ccSJohn Dyson 	if (invalidate)
1841cf2819ccSJohn Dyson 		pmap_remove(vm_map_pmap(map), start, end);
1842df8bae1dSRodney W. Grimes 	/*
1843df8bae1dSRodney W. Grimes 	 * Make a second pass, cleaning/uncaching pages from the indicated
1844df8bae1dSRodney W. Grimes 	 * objects as we go.
1845df8bae1dSRodney W. Grimes 	 */
1846df8bae1dSRodney W. Grimes 	for (current = entry; current->start < end; current = current->next) {
1847df8bae1dSRodney W. Grimes 		offset = current->offset + (start - current->start);
1848df8bae1dSRodney W. Grimes 		size = (end <= current->end ? end : current->end) - start;
18499fdfe602SMatthew Dillon 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1850c0877f10SJohn Dyson 			vm_map_t smap;
1851df8bae1dSRodney W. Grimes 			vm_map_entry_t tentry;
1852df8bae1dSRodney W. Grimes 			vm_size_t tsize;
1853df8bae1dSRodney W. Grimes 
18549fdfe602SMatthew Dillon 			smap = current->object.sub_map;
1855df8bae1dSRodney W. Grimes 			vm_map_lock_read(smap);
1856df8bae1dSRodney W. Grimes 			(void) vm_map_lookup_entry(smap, offset, &tentry);
1857df8bae1dSRodney W. Grimes 			tsize = tentry->end - offset;
1858df8bae1dSRodney W. Grimes 			if (tsize < size)
1859df8bae1dSRodney W. Grimes 				size = tsize;
1860df8bae1dSRodney W. Grimes 			object = tentry->object.vm_object;
1861df8bae1dSRodney W. Grimes 			offset = tentry->offset + (offset - tentry->start);
1862df8bae1dSRodney W. Grimes 			vm_map_unlock_read(smap);
1863df8bae1dSRodney W. Grimes 		} else {
1864df8bae1dSRodney W. Grimes 			object = current->object.vm_object;
1865df8bae1dSRodney W. Grimes 		}
18668a02c104SJohn Dyson 		/*
18678a02c104SJohn Dyson 		 * Note that there is absolutely no sense in writing out
18688a02c104SJohn Dyson 		 * anonymous objects, so we track down the vnode object
18698a02c104SJohn Dyson 		 * to write out.
18708a02c104SJohn Dyson 		 * We invalidate (remove) all pages from the address space
18718a02c104SJohn Dyson 		 * anyway, for semantic correctness.
18728a02c104SJohn Dyson 		 */
18738a02c104SJohn Dyson 		while (object->backing_object) {
18748a02c104SJohn Dyson 			object = object->backing_object;
18758a02c104SJohn Dyson 			offset += object->backing_object_offset;
18768a02c104SJohn Dyson 			if (object->size < OFF_TO_IDX( offset + size))
18778a02c104SJohn Dyson 				size = IDX_TO_OFF(object->size) - offset;
18788a02c104SJohn Dyson 		}
1879ff359f84SMatthew Dillon 		if (object && (object->type == OBJT_VNODE) &&
1880ff359f84SMatthew Dillon 		    (current->protection & VM_PROT_WRITE)) {
1881df8bae1dSRodney W. Grimes 			/*
1882ff359f84SMatthew Dillon 			 * Flush pages if writing is allowed, invalidate them
1883ff359f84SMatthew Dillon 			 * if invalidation requested.  Pages undergoing I/O
1884ff359f84SMatthew Dillon 			 * will be ignored by vm_object_page_remove().
1885f5cf85d4SDavid Greenman 			 *
1886ff359f84SMatthew Dillon 			 * We cannot lock the vnode and then wait for paging
1887ff359f84SMatthew Dillon 			 * to complete without deadlocking against vm_fault.
1888ff359f84SMatthew Dillon 			 * Instead we simply call vm_object_page_remove() and
1889ff359f84SMatthew Dillon 			 * allow it to block internally on a page-by-page
1890ff359f84SMatthew Dillon 			 * basis when it encounters pages undergoing async
1891ff359f84SMatthew Dillon 			 * I/O.
1892df8bae1dSRodney W. Grimes 			 */
18938f9110f6SJohn Dyson 			int flags;
1894ff359f84SMatthew Dillon 
1895ff359f84SMatthew Dillon 			vm_object_reference(object);
1896b40ce416SJulian Elischer 			vn_lock(object->handle, LK_EXCLUSIVE | LK_RETRY, curthread);
18978f9110f6SJohn Dyson 			flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
18988f9110f6SJohn Dyson 			flags |= invalidate ? OBJPC_INVAL : 0;
1899a316d390SJohn Dyson 			vm_object_page_clean(object,
1900a316d390SJohn Dyson 			    OFF_TO_IDX(offset),
19012be70f79SJohn Dyson 			    OFF_TO_IDX(offset + size + PAGE_MASK),
19028f9110f6SJohn Dyson 			    flags);
1903cf2819ccSJohn Dyson 			if (invalidate) {
1904ff359f84SMatthew Dillon 				/*vm_object_pip_wait(object, "objmcl");*/
1905a316d390SJohn Dyson 				vm_object_page_remove(object,
1906a316d390SJohn Dyson 				    OFF_TO_IDX(offset),
19072be70f79SJohn Dyson 				    OFF_TO_IDX(offset + size + PAGE_MASK),
1908a316d390SJohn Dyson 				    FALSE);
1909cf2819ccSJohn Dyson 			}
1910b40ce416SJulian Elischer 			VOP_UNLOCK(object->handle, 0, curthread);
1911ff359f84SMatthew Dillon 			vm_object_deallocate(object);
1912a02051c3SJohn Dyson 		}
1913df8bae1dSRodney W. Grimes 		start += size;
1914df8bae1dSRodney W. Grimes 	}
1915df8bae1dSRodney W. Grimes 
1916df8bae1dSRodney W. Grimes 	vm_map_unlock_read(map);
1917df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
1918df8bae1dSRodney W. Grimes }
1919df8bae1dSRodney W. Grimes 
1920df8bae1dSRodney W. Grimes /*
1921df8bae1dSRodney W. Grimes  *	vm_map_entry_unwire:	[ internal use only ]
1922df8bae1dSRodney W. Grimes  *
1923df8bae1dSRodney W. Grimes  *	Make the region specified by this entry pageable.
1924df8bae1dSRodney W. Grimes  *
1925df8bae1dSRodney W. Grimes  *	The map in question should be locked.
1926df8bae1dSRodney W. Grimes  *	[This is the reason for this routine's existence.]
1927df8bae1dSRodney W. Grimes  */
19280362d7d7SJohn Dyson static void
19291b40f8c0SMatthew Dillon vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
1930df8bae1dSRodney W. Grimes {
1931df8bae1dSRodney W. Grimes 	vm_fault_unwire(map, entry->start, entry->end);
1932df8bae1dSRodney W. Grimes 	entry->wired_count = 0;
1933df8bae1dSRodney W. Grimes }
1934df8bae1dSRodney W. Grimes 
1935df8bae1dSRodney W. Grimes /*
1936df8bae1dSRodney W. Grimes  *	vm_map_entry_delete:	[ internal use only ]
1937df8bae1dSRodney W. Grimes  *
1938df8bae1dSRodney W. Grimes  *	Deallocate the given entry from the target map.
1939df8bae1dSRodney W. Grimes  */
19400362d7d7SJohn Dyson static void
19411b40f8c0SMatthew Dillon vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry)
1942df8bae1dSRodney W. Grimes {
1943df8bae1dSRodney W. Grimes 	vm_map_entry_unlink(map, entry);
1944df8bae1dSRodney W. Grimes 	map->size -= entry->end - entry->start;
1945df8bae1dSRodney W. Grimes 
19469fdfe602SMatthew Dillon 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1947df8bae1dSRodney W. Grimes 		vm_object_deallocate(entry->object.vm_object);
1948b5b40fa6SJohn Dyson 	}
1949df8bae1dSRodney W. Grimes 
1950df8bae1dSRodney W. Grimes 	vm_map_entry_dispose(map, entry);
1951df8bae1dSRodney W. Grimes }
1952df8bae1dSRodney W. Grimes 
1953df8bae1dSRodney W. Grimes /*
1954df8bae1dSRodney W. Grimes  *	vm_map_delete:	[ internal use only ]
1955df8bae1dSRodney W. Grimes  *
1956df8bae1dSRodney W. Grimes  *	Deallocates the given address range from the target
1957df8bae1dSRodney W. Grimes  *	map.
1958df8bae1dSRodney W. Grimes  */
1959df8bae1dSRodney W. Grimes int
19601b40f8c0SMatthew Dillon vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end)
1961df8bae1dSRodney W. Grimes {
1962cbd8ec09SJohn Dyson 	vm_object_t object;
1963c0877f10SJohn Dyson 	vm_map_entry_t entry;
1964df8bae1dSRodney W. Grimes 	vm_map_entry_t first_entry;
1965df8bae1dSRodney W. Grimes 
19660cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
19670cddd8f0SMatthew Dillon 
1968df8bae1dSRodney W. Grimes 	/*
1969df8bae1dSRodney W. Grimes 	 * Find the start of the region, and clip it
1970df8bae1dSRodney W. Grimes 	 */
1971df8bae1dSRodney W. Grimes 
1972876318ecSAlan Cox 	if (!vm_map_lookup_entry(map, start, &first_entry))
1973df8bae1dSRodney W. Grimes 		entry = first_entry->next;
1974876318ecSAlan Cox 	else {
1975df8bae1dSRodney W. Grimes 		entry = first_entry;
1976df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
1977df8bae1dSRodney W. Grimes 		/*
19780d94caffSDavid Greenman 		 * Fix the lookup hint now, rather than each time though the
19790d94caffSDavid Greenman 		 * loop.
1980df8bae1dSRodney W. Grimes 		 */
1981df8bae1dSRodney W. Grimes 		SAVE_HINT(map, entry->prev);
1982df8bae1dSRodney W. Grimes 	}
1983df8bae1dSRodney W. Grimes 
1984df8bae1dSRodney W. Grimes 	/*
1985df8bae1dSRodney W. Grimes 	 * Save the free space hint
1986df8bae1dSRodney W. Grimes 	 */
1987df8bae1dSRodney W. Grimes 
1988b18bfc3dSJohn Dyson 	if (entry == &map->header) {
1989b18bfc3dSJohn Dyson 		map->first_free = &map->header;
19902dbea5d2SJohn Dyson 	} else if (map->first_free->start >= start) {
1991df8bae1dSRodney W. Grimes 		map->first_free = entry->prev;
19922dbea5d2SJohn Dyson 	}
1993df8bae1dSRodney W. Grimes 
1994df8bae1dSRodney W. Grimes 	/*
1995df8bae1dSRodney W. Grimes 	 * Step through all entries in this region
1996df8bae1dSRodney W. Grimes 	 */
1997df8bae1dSRodney W. Grimes 
1998df8bae1dSRodney W. Grimes 	while ((entry != &map->header) && (entry->start < end)) {
1999df8bae1dSRodney W. Grimes 		vm_map_entry_t next;
2000b18bfc3dSJohn Dyson 		vm_offset_t s, e;
2001cbd8ec09SJohn Dyson 		vm_pindex_t offidxstart, offidxend, count;
2002df8bae1dSRodney W. Grimes 
2003df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, entry, end);
2004df8bae1dSRodney W. Grimes 
2005df8bae1dSRodney W. Grimes 		s = entry->start;
2006df8bae1dSRodney W. Grimes 		e = entry->end;
2007c0877f10SJohn Dyson 		next = entry->next;
2008df8bae1dSRodney W. Grimes 
2009cbd8ec09SJohn Dyson 		offidxstart = OFF_TO_IDX(entry->offset);
2010cbd8ec09SJohn Dyson 		count = OFF_TO_IDX(e - s);
2011cbd8ec09SJohn Dyson 		object = entry->object.vm_object;
20122dbea5d2SJohn Dyson 
2013df8bae1dSRodney W. Grimes 		/*
20140d94caffSDavid Greenman 		 * Unwire before removing addresses from the pmap; otherwise,
20150d94caffSDavid Greenman 		 * unwiring will put the entries back in the pmap.
2016df8bae1dSRodney W. Grimes 		 */
2017c0877f10SJohn Dyson 		if (entry->wired_count != 0) {
2018df8bae1dSRodney W. Grimes 			vm_map_entry_unwire(map, entry);
2019c0877f10SJohn Dyson 		}
2020df8bae1dSRodney W. Grimes 
2021cbd8ec09SJohn Dyson 		offidxend = offidxstart + count;
2022df8bae1dSRodney W. Grimes 
2023c0877f10SJohn Dyson 		if ((object == kernel_object) || (object == kmem_object)) {
20242dbea5d2SJohn Dyson 			vm_object_page_remove(object, offidxstart, offidxend, FALSE);
2025b18bfc3dSJohn Dyson 		} else {
2026df8bae1dSRodney W. Grimes 			pmap_remove(map->pmap, s, e);
2027876318ecSAlan Cox 			if (object != NULL &&
2028876318ecSAlan Cox 			    object->ref_count != 1 &&
2029876318ecSAlan Cox 			    (object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING &&
2030876318ecSAlan Cox 			    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
20312dbea5d2SJohn Dyson 				vm_object_collapse(object);
20322dbea5d2SJohn Dyson 				vm_object_page_remove(object, offidxstart, offidxend, FALSE);
20332dbea5d2SJohn Dyson 				if (object->type == OBJT_SWAP) {
2034cbd8ec09SJohn Dyson 					swap_pager_freespace(object, offidxstart, count);
20352dbea5d2SJohn Dyson 				}
2036876318ecSAlan Cox 				if (offidxend >= object->size &&
2037876318ecSAlan Cox 				    offidxstart < object->size) {
2038c0877f10SJohn Dyson 					object->size = offidxstart;
2039c0877f10SJohn Dyson 				}
20402dbea5d2SJohn Dyson 			}
2041b18bfc3dSJohn Dyson 		}
2042df8bae1dSRodney W. Grimes 
2043df8bae1dSRodney W. Grimes 		/*
20440d94caffSDavid Greenman 		 * Delete the entry (which may delete the object) only after
20450d94caffSDavid Greenman 		 * removing all pmap entries pointing to its pages.
20460d94caffSDavid Greenman 		 * (Otherwise, its page frames may be reallocated, and any
20470d94caffSDavid Greenman 		 * modify bits will be set in the wrong object!)
2048df8bae1dSRodney W. Grimes 		 */
2049df8bae1dSRodney W. Grimes 		vm_map_entry_delete(map, entry);
2050df8bae1dSRodney W. Grimes 		entry = next;
2051df8bae1dSRodney W. Grimes 	}
2052df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
2053df8bae1dSRodney W. Grimes }
2054df8bae1dSRodney W. Grimes 
2055df8bae1dSRodney W. Grimes /*
2056df8bae1dSRodney W. Grimes  *	vm_map_remove:
2057df8bae1dSRodney W. Grimes  *
2058df8bae1dSRodney W. Grimes  *	Remove the given address range from the target map.
2059df8bae1dSRodney W. Grimes  *	This is the exported form of vm_map_delete.
2060df8bae1dSRodney W. Grimes  */
2061df8bae1dSRodney W. Grimes int
20621b40f8c0SMatthew Dillon vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
2063df8bae1dSRodney W. Grimes {
2064c0877f10SJohn Dyson 	int result, s = 0;
20658d6e8edeSDavid Greenman 
20660cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
20670cddd8f0SMatthew Dillon 
206808442f8aSBosko Milekic 	if (map == kmem_map)
2069b18bfc3dSJohn Dyson 		s = splvm();
2070df8bae1dSRodney W. Grimes 
2071df8bae1dSRodney W. Grimes 	vm_map_lock(map);
2072df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
2073df8bae1dSRodney W. Grimes 	result = vm_map_delete(map, start, end);
2074df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
2075df8bae1dSRodney W. Grimes 
207608442f8aSBosko Milekic 	if (map == kmem_map)
20778d6e8edeSDavid Greenman 		splx(s);
20788d6e8edeSDavid Greenman 
2079df8bae1dSRodney W. Grimes 	return (result);
2080df8bae1dSRodney W. Grimes }
2081df8bae1dSRodney W. Grimes 
2082df8bae1dSRodney W. Grimes /*
2083df8bae1dSRodney W. Grimes  *	vm_map_check_protection:
2084df8bae1dSRodney W. Grimes  *
2085df8bae1dSRodney W. Grimes  *	Assert that the target map allows the specified
2086df8bae1dSRodney W. Grimes  *	privilege on the entire address region given.
2087df8bae1dSRodney W. Grimes  *	The entire region must be allocated.
2088df8bae1dSRodney W. Grimes  */
20890d94caffSDavid Greenman boolean_t
2090b9dcd593SBruce Evans vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
2091b9dcd593SBruce Evans 			vm_prot_t protection)
2092df8bae1dSRodney W. Grimes {
2093c0877f10SJohn Dyson 	vm_map_entry_t entry;
2094df8bae1dSRodney W. Grimes 	vm_map_entry_t tmp_entry;
2095df8bae1dSRodney W. Grimes 
20960cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
20970cddd8f0SMatthew Dillon 
2098df8bae1dSRodney W. Grimes 	if (!vm_map_lookup_entry(map, start, &tmp_entry)) {
2099df8bae1dSRodney W. Grimes 		return (FALSE);
2100df8bae1dSRodney W. Grimes 	}
2101df8bae1dSRodney W. Grimes 	entry = tmp_entry;
2102df8bae1dSRodney W. Grimes 
2103df8bae1dSRodney W. Grimes 	while (start < end) {
2104df8bae1dSRodney W. Grimes 		if (entry == &map->header) {
2105df8bae1dSRodney W. Grimes 			return (FALSE);
2106df8bae1dSRodney W. Grimes 		}
2107df8bae1dSRodney W. Grimes 		/*
2108df8bae1dSRodney W. Grimes 		 * No holes allowed!
2109df8bae1dSRodney W. Grimes 		 */
2110df8bae1dSRodney W. Grimes 
2111df8bae1dSRodney W. Grimes 		if (start < entry->start) {
2112df8bae1dSRodney W. Grimes 			return (FALSE);
2113df8bae1dSRodney W. Grimes 		}
2114df8bae1dSRodney W. Grimes 		/*
2115df8bae1dSRodney W. Grimes 		 * Check protection associated with entry.
2116df8bae1dSRodney W. Grimes 		 */
2117df8bae1dSRodney W. Grimes 
2118df8bae1dSRodney W. Grimes 		if ((entry->protection & protection) != protection) {
2119df8bae1dSRodney W. Grimes 			return (FALSE);
2120df8bae1dSRodney W. Grimes 		}
2121df8bae1dSRodney W. Grimes 		/* go to next entry */
2122df8bae1dSRodney W. Grimes 
2123df8bae1dSRodney W. Grimes 		start = entry->end;
2124df8bae1dSRodney W. Grimes 		entry = entry->next;
2125df8bae1dSRodney W. Grimes 	}
2126df8bae1dSRodney W. Grimes 	return (TRUE);
2127df8bae1dSRodney W. Grimes }
2128df8bae1dSRodney W. Grimes 
212986524867SJohn Dyson /*
213086524867SJohn Dyson  * Split the pages in a map entry into a new object.  This affords
213186524867SJohn Dyson  * easier removal of unused pages, and keeps object inheritance from
213286524867SJohn Dyson  * being a negative impact on memory usage.
213386524867SJohn Dyson  */
2134c0877f10SJohn Dyson static void
21351b40f8c0SMatthew Dillon vm_map_split(vm_map_entry_t entry)
2136c0877f10SJohn Dyson {
213786524867SJohn Dyson 	vm_page_t m;
2138bd6be915SJohn Dyson 	vm_object_t orig_object, new_object, source;
2139c0877f10SJohn Dyson 	vm_offset_t s, e;
2140c0877f10SJohn Dyson 	vm_pindex_t offidxstart, offidxend, idx;
2141c0877f10SJohn Dyson 	vm_size_t size;
2142c0877f10SJohn Dyson 	vm_ooffset_t offset;
2143c0877f10SJohn Dyson 
21440cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
21450cddd8f0SMatthew Dillon 
2146c0877f10SJohn Dyson 	orig_object = entry->object.vm_object;
2147c0877f10SJohn Dyson 	if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
2148c0877f10SJohn Dyson 		return;
2149c0877f10SJohn Dyson 	if (orig_object->ref_count <= 1)
2150c0877f10SJohn Dyson 		return;
2151c0877f10SJohn Dyson 
2152c0877f10SJohn Dyson 	offset = entry->offset;
2153c0877f10SJohn Dyson 	s = entry->start;
2154c0877f10SJohn Dyson 	e = entry->end;
2155c0877f10SJohn Dyson 
2156c0877f10SJohn Dyson 	offidxstart = OFF_TO_IDX(offset);
2157c0877f10SJohn Dyson 	offidxend = offidxstart + OFF_TO_IDX(e - s);
2158c0877f10SJohn Dyson 	size = offidxend - offidxstart;
2159c0877f10SJohn Dyson 
2160c0877f10SJohn Dyson 	new_object = vm_pager_allocate(orig_object->type,
21616cde7a16SDavid Greenman 		NULL, IDX_TO_OFF(size), VM_PROT_ALL, 0LL);
2162c0877f10SJohn Dyson 	if (new_object == NULL)
2163c0877f10SJohn Dyson 		return;
2164c0877f10SJohn Dyson 
2165bd6be915SJohn Dyson 	source = orig_object->backing_object;
2166bd6be915SJohn Dyson 	if (source != NULL) {
2167bd6be915SJohn Dyson 		vm_object_reference(source);	/* Referenced by new_object */
2168bd6be915SJohn Dyson 		TAILQ_INSERT_TAIL(&source->shadow_head,
2169bd6be915SJohn Dyson 				  new_object, shadow_list);
2170069e9bc1SDoug Rabson 		vm_object_clear_flag(source, OBJ_ONEMAPPING);
2171bd6be915SJohn Dyson 		new_object->backing_object_offset =
2172a0fce827SJohn Polstra 			orig_object->backing_object_offset + IDX_TO_OFF(offidxstart);
2173bd6be915SJohn Dyson 		new_object->backing_object = source;
2174bd6be915SJohn Dyson 		source->shadow_count++;
2175bd6be915SJohn Dyson 		source->generation++;
2176bd6be915SJohn Dyson 	}
2177bd6be915SJohn Dyson 
2178c0877f10SJohn Dyson 	for (idx = 0; idx < size; idx++) {
2179c0877f10SJohn Dyson 		vm_page_t m;
2180c0877f10SJohn Dyson 
2181c0877f10SJohn Dyson 	retry:
2182c0877f10SJohn Dyson 		m = vm_page_lookup(orig_object, offidxstart + idx);
2183c0877f10SJohn Dyson 		if (m == NULL)
2184c0877f10SJohn Dyson 			continue;
21851c7c3c6aSMatthew Dillon 
21861c7c3c6aSMatthew Dillon 		/*
21871c7c3c6aSMatthew Dillon 		 * We must wait for pending I/O to complete before we can
21881c7c3c6aSMatthew Dillon 		 * rename the page.
2189d1bf5d56SMatthew Dillon 		 *
2190d1bf5d56SMatthew Dillon 		 * We do not have to VM_PROT_NONE the page as mappings should
2191d1bf5d56SMatthew Dillon 		 * not be changed by this operation.
21921c7c3c6aSMatthew Dillon 		 */
21931c7c3c6aSMatthew Dillon 		if (vm_page_sleep_busy(m, TRUE, "spltwt"))
2194c0877f10SJohn Dyson 			goto retry;
2195c0877f10SJohn Dyson 
2196e69763a3SDoug Rabson 		vm_page_busy(m);
2197c0877f10SJohn Dyson 		vm_page_rename(m, new_object, idx);
21987dbf82dcSMatthew Dillon 		/* page automatically made dirty by rename and cache handled */
2199e69763a3SDoug Rabson 		vm_page_busy(m);
2200c0877f10SJohn Dyson 	}
2201c0877f10SJohn Dyson 
2202c0877f10SJohn Dyson 	if (orig_object->type == OBJT_SWAP) {
2203d474eaaaSDoug Rabson 		vm_object_pip_add(orig_object, 1);
2204c0877f10SJohn Dyson 		/*
2205c0877f10SJohn Dyson 		 * copy orig_object pages into new_object
2206c0877f10SJohn Dyson 		 * and destroy unneeded pages in
2207c0877f10SJohn Dyson 		 * shadow object.
2208c0877f10SJohn Dyson 		 */
22091c7c3c6aSMatthew Dillon 		swap_pager_copy(orig_object, new_object, offidxstart, 0);
2210c0877f10SJohn Dyson 		vm_object_pip_wakeup(orig_object);
2211c0877f10SJohn Dyson 	}
2212c0877f10SJohn Dyson 
221386524867SJohn Dyson 	for (idx = 0; idx < size; idx++) {
221486524867SJohn Dyson 		m = vm_page_lookup(new_object, idx);
221586524867SJohn Dyson 		if (m) {
2216e69763a3SDoug Rabson 			vm_page_wakeup(m);
221786524867SJohn Dyson 		}
221886524867SJohn Dyson 	}
221986524867SJohn Dyson 
2220c0877f10SJohn Dyson 	entry->object.vm_object = new_object;
2221c0877f10SJohn Dyson 	entry->offset = 0LL;
2222c0877f10SJohn Dyson 	vm_object_deallocate(orig_object);
2223c0877f10SJohn Dyson }
2224c0877f10SJohn Dyson 
2225df8bae1dSRodney W. Grimes /*
2226df8bae1dSRodney W. Grimes  *	vm_map_copy_entry:
2227df8bae1dSRodney W. Grimes  *
2228df8bae1dSRodney W. Grimes  *	Copies the contents of the source entry to the destination
2229df8bae1dSRodney W. Grimes  *	entry.  The entries *must* be aligned properly.
2230df8bae1dSRodney W. Grimes  */
2231f708ef1bSPoul-Henning Kamp static void
22321b40f8c0SMatthew Dillon vm_map_copy_entry(
22331b40f8c0SMatthew Dillon 	vm_map_t src_map,
22341b40f8c0SMatthew Dillon 	vm_map_t dst_map,
22351b40f8c0SMatthew Dillon 	vm_map_entry_t src_entry,
22361b40f8c0SMatthew Dillon 	vm_map_entry_t dst_entry)
2237df8bae1dSRodney W. Grimes {
2238c0877f10SJohn Dyson 	vm_object_t src_object;
2239c0877f10SJohn Dyson 
22409fdfe602SMatthew Dillon 	if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
2241df8bae1dSRodney W. Grimes 		return;
2242df8bae1dSRodney W. Grimes 
2243df8bae1dSRodney W. Grimes 	if (src_entry->wired_count == 0) {
2244df8bae1dSRodney W. Grimes 
2245df8bae1dSRodney W. Grimes 		/*
22460d94caffSDavid Greenman 		 * If the source entry is marked needs_copy, it is already
22470d94caffSDavid Greenman 		 * write-protected.
2248df8bae1dSRodney W. Grimes 		 */
2249afa07f7eSJohn Dyson 		if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
2250df8bae1dSRodney W. Grimes 			pmap_protect(src_map->pmap,
2251df8bae1dSRodney W. Grimes 			    src_entry->start,
2252df8bae1dSRodney W. Grimes 			    src_entry->end,
2253df8bae1dSRodney W. Grimes 			    src_entry->protection & ~VM_PROT_WRITE);
2254df8bae1dSRodney W. Grimes 		}
2255b18bfc3dSJohn Dyson 
2256df8bae1dSRodney W. Grimes 		/*
2257df8bae1dSRodney W. Grimes 		 * Make a copy of the object.
2258df8bae1dSRodney W. Grimes 		 */
22598aef1712SMatthew Dillon 		if ((src_object = src_entry->object.vm_object) != NULL) {
2260c0877f10SJohn Dyson 
2261c0877f10SJohn Dyson 			if ((src_object->handle == NULL) &&
2262c0877f10SJohn Dyson 				(src_object->type == OBJT_DEFAULT ||
2263c0877f10SJohn Dyson 				 src_object->type == OBJT_SWAP)) {
2264c0877f10SJohn Dyson 				vm_object_collapse(src_object);
226596fb8cf2SJohn Dyson 				if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
2266c0877f10SJohn Dyson 					vm_map_split(src_entry);
2267c0877f10SJohn Dyson 					src_object = src_entry->object.vm_object;
2268c0877f10SJohn Dyson 				}
2269c0877f10SJohn Dyson 			}
2270c0877f10SJohn Dyson 
2271c0877f10SJohn Dyson 			vm_object_reference(src_object);
2272069e9bc1SDoug Rabson 			vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
2273c0877f10SJohn Dyson 			dst_entry->object.vm_object = src_object;
2274afa07f7eSJohn Dyson 			src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2275afa07f7eSJohn Dyson 			dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2276b18bfc3dSJohn Dyson 			dst_entry->offset = src_entry->offset;
2277b18bfc3dSJohn Dyson 		} else {
2278b18bfc3dSJohn Dyson 			dst_entry->object.vm_object = NULL;
2279b18bfc3dSJohn Dyson 			dst_entry->offset = 0;
2280b18bfc3dSJohn Dyson 		}
2281df8bae1dSRodney W. Grimes 
2282df8bae1dSRodney W. Grimes 		pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
2283df8bae1dSRodney W. Grimes 		    dst_entry->end - dst_entry->start, src_entry->start);
22840d94caffSDavid Greenman 	} else {
2285df8bae1dSRodney W. Grimes 		/*
2286df8bae1dSRodney W. Grimes 		 * Of course, wired down pages can't be set copy-on-write.
22870d94caffSDavid Greenman 		 * Cause wired pages to be copied into the new map by
22880d94caffSDavid Greenman 		 * simulating faults (the new pages are pageable)
2289df8bae1dSRodney W. Grimes 		 */
2290df8bae1dSRodney W. Grimes 		vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry);
2291df8bae1dSRodney W. Grimes 	}
2292df8bae1dSRodney W. Grimes }
2293df8bae1dSRodney W. Grimes 
2294df8bae1dSRodney W. Grimes /*
2295df8bae1dSRodney W. Grimes  * vmspace_fork:
2296df8bae1dSRodney W. Grimes  * Create a new process vmspace structure and vm_map
2297df8bae1dSRodney W. Grimes  * based on those of an existing process.  The new map
2298df8bae1dSRodney W. Grimes  * is based on the old map, according to the inheritance
2299df8bae1dSRodney W. Grimes  * values on the regions in that map.
2300df8bae1dSRodney W. Grimes  *
2301df8bae1dSRodney W. Grimes  * The source map must not be locked.
2302df8bae1dSRodney W. Grimes  */
2303df8bae1dSRodney W. Grimes struct vmspace *
23041b40f8c0SMatthew Dillon vmspace_fork(struct vmspace *vm1)
2305df8bae1dSRodney W. Grimes {
2306c0877f10SJohn Dyson 	struct vmspace *vm2;
2307df8bae1dSRodney W. Grimes 	vm_map_t old_map = &vm1->vm_map;
2308df8bae1dSRodney W. Grimes 	vm_map_t new_map;
2309df8bae1dSRodney W. Grimes 	vm_map_entry_t old_entry;
2310df8bae1dSRodney W. Grimes 	vm_map_entry_t new_entry;
2311de5f6a77SJohn Dyson 	vm_object_t object;
2312df8bae1dSRodney W. Grimes 
23130cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
23140cddd8f0SMatthew Dillon 
2315df8bae1dSRodney W. Grimes 	vm_map_lock(old_map);
2316b823bbd6SMatthew Dillon 	old_map->infork = 1;
2317df8bae1dSRodney W. Grimes 
23182d8acc0fSJohn Dyson 	vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
2319df8bae1dSRodney W. Grimes 	bcopy(&vm1->vm_startcopy, &vm2->vm_startcopy,
2320df8bae1dSRodney W. Grimes 	    (caddr_t) (vm1 + 1) - (caddr_t) &vm1->vm_startcopy);
2321df8bae1dSRodney W. Grimes 	new_map = &vm2->vm_map;	/* XXX */
232247221757SJohn Dyson 	new_map->timestamp = 1;
2323df8bae1dSRodney W. Grimes 
2324df8bae1dSRodney W. Grimes 	old_entry = old_map->header.next;
2325df8bae1dSRodney W. Grimes 
2326df8bae1dSRodney W. Grimes 	while (old_entry != &old_map->header) {
2327afa07f7eSJohn Dyson 		if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2328df8bae1dSRodney W. Grimes 			panic("vm_map_fork: encountered a submap");
2329df8bae1dSRodney W. Grimes 
2330df8bae1dSRodney W. Grimes 		switch (old_entry->inheritance) {
2331df8bae1dSRodney W. Grimes 		case VM_INHERIT_NONE:
2332df8bae1dSRodney W. Grimes 			break;
2333df8bae1dSRodney W. Grimes 
2334df8bae1dSRodney W. Grimes 		case VM_INHERIT_SHARE:
2335df8bae1dSRodney W. Grimes 			/*
2336fed9a903SJohn Dyson 			 * Clone the entry, creating the shared object if necessary.
2337fed9a903SJohn Dyson 			 */
2338fed9a903SJohn Dyson 			object = old_entry->object.vm_object;
2339fed9a903SJohn Dyson 			if (object == NULL) {
2340fed9a903SJohn Dyson 				object = vm_object_allocate(OBJT_DEFAULT,
2341c2e11a03SJohn Dyson 					atop(old_entry->end - old_entry->start));
2342fed9a903SJohn Dyson 				old_entry->object.vm_object = object;
2343fed9a903SJohn Dyson 				old_entry->offset = (vm_offset_t) 0;
23449a2f6362SAlan Cox 			}
23459a2f6362SAlan Cox 
23469a2f6362SAlan Cox 			/*
23479a2f6362SAlan Cox 			 * Add the reference before calling vm_object_shadow
23489a2f6362SAlan Cox 			 * to insure that a shadow object is created.
23499a2f6362SAlan Cox 			 */
23509a2f6362SAlan Cox 			vm_object_reference(object);
23519a2f6362SAlan Cox 			if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
23525069bf57SJohn Dyson 				vm_object_shadow(&old_entry->object.vm_object,
23535069bf57SJohn Dyson 					&old_entry->offset,
2354c2e11a03SJohn Dyson 					atop(old_entry->end - old_entry->start));
23555069bf57SJohn Dyson 				old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
2356d30344bdSIan Dowse 				/* Transfer the second reference too. */
2357d30344bdSIan Dowse 				vm_object_reference(
2358d30344bdSIan Dowse 				    old_entry->object.vm_object);
2359d30344bdSIan Dowse 				vm_object_deallocate(object);
23605069bf57SJohn Dyson 				object = old_entry->object.vm_object;
2361fed9a903SJohn Dyson 			}
2362069e9bc1SDoug Rabson 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
2363fed9a903SJohn Dyson 
2364fed9a903SJohn Dyson 			/*
2365ad5fca3bSAlan Cox 			 * Clone the entry, referencing the shared object.
2366df8bae1dSRodney W. Grimes 			 */
2367df8bae1dSRodney W. Grimes 			new_entry = vm_map_entry_create(new_map);
2368df8bae1dSRodney W. Grimes 			*new_entry = *old_entry;
2369028fe6ecSTor Egge 			new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2370df8bae1dSRodney W. Grimes 			new_entry->wired_count = 0;
2371df8bae1dSRodney W. Grimes 
2372df8bae1dSRodney W. Grimes 			/*
23730d94caffSDavid Greenman 			 * Insert the entry into the new map -- we know we're
23740d94caffSDavid Greenman 			 * inserting at the end of the new map.
2375df8bae1dSRodney W. Grimes 			 */
2376df8bae1dSRodney W. Grimes 
2377df8bae1dSRodney W. Grimes 			vm_map_entry_link(new_map, new_map->header.prev,
2378df8bae1dSRodney W. Grimes 			    new_entry);
2379df8bae1dSRodney W. Grimes 
2380df8bae1dSRodney W. Grimes 			/*
2381df8bae1dSRodney W. Grimes 			 * Update the physical map
2382df8bae1dSRodney W. Grimes 			 */
2383df8bae1dSRodney W. Grimes 
2384df8bae1dSRodney W. Grimes 			pmap_copy(new_map->pmap, old_map->pmap,
2385df8bae1dSRodney W. Grimes 			    new_entry->start,
2386df8bae1dSRodney W. Grimes 			    (old_entry->end - old_entry->start),
2387df8bae1dSRodney W. Grimes 			    old_entry->start);
2388df8bae1dSRodney W. Grimes 			break;
2389df8bae1dSRodney W. Grimes 
2390df8bae1dSRodney W. Grimes 		case VM_INHERIT_COPY:
2391df8bae1dSRodney W. Grimes 			/*
2392df8bae1dSRodney W. Grimes 			 * Clone the entry and link into the map.
2393df8bae1dSRodney W. Grimes 			 */
2394df8bae1dSRodney W. Grimes 			new_entry = vm_map_entry_create(new_map);
2395df8bae1dSRodney W. Grimes 			*new_entry = *old_entry;
2396028fe6ecSTor Egge 			new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2397df8bae1dSRodney W. Grimes 			new_entry->wired_count = 0;
2398df8bae1dSRodney W. Grimes 			new_entry->object.vm_object = NULL;
2399df8bae1dSRodney W. Grimes 			vm_map_entry_link(new_map, new_map->header.prev,
2400df8bae1dSRodney W. Grimes 			    new_entry);
2401bd7e5f99SJohn Dyson 			vm_map_copy_entry(old_map, new_map, old_entry,
2402bd7e5f99SJohn Dyson 			    new_entry);
2403df8bae1dSRodney W. Grimes 			break;
2404df8bae1dSRodney W. Grimes 		}
2405df8bae1dSRodney W. Grimes 		old_entry = old_entry->next;
2406df8bae1dSRodney W. Grimes 	}
2407df8bae1dSRodney W. Grimes 
2408df8bae1dSRodney W. Grimes 	new_map->size = old_map->size;
2409b823bbd6SMatthew Dillon 	old_map->infork = 0;
2410df8bae1dSRodney W. Grimes 	vm_map_unlock(old_map);
2411df8bae1dSRodney W. Grimes 
2412df8bae1dSRodney W. Grimes 	return (vm2);
2413df8bae1dSRodney W. Grimes }
2414df8bae1dSRodney W. Grimes 
241594f7e29aSAlan Cox int
241694f7e29aSAlan Cox vm_map_stack (vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
241794f7e29aSAlan Cox 	      vm_prot_t prot, vm_prot_t max, int cow)
241894f7e29aSAlan Cox {
241994f7e29aSAlan Cox 	vm_map_entry_t prev_entry;
242094f7e29aSAlan Cox 	vm_map_entry_t new_stack_entry;
242194f7e29aSAlan Cox 	vm_size_t      init_ssize;
242294f7e29aSAlan Cox 	int            rv;
242394f7e29aSAlan Cox 
24240cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
24250cddd8f0SMatthew Dillon 
242694f7e29aSAlan Cox 	if (VM_MIN_ADDRESS > 0 && addrbos < VM_MIN_ADDRESS)
242794f7e29aSAlan Cox 		return (KERN_NO_SPACE);
242894f7e29aSAlan Cox 
2429cbc89bfbSPaul Saab 	if (max_ssize < sgrowsiz)
243094f7e29aSAlan Cox 		init_ssize = max_ssize;
243194f7e29aSAlan Cox 	else
2432cbc89bfbSPaul Saab 		init_ssize = sgrowsiz;
243394f7e29aSAlan Cox 
243494f7e29aSAlan Cox 	vm_map_lock(map);
243594f7e29aSAlan Cox 
243694f7e29aSAlan Cox 	/* If addr is already mapped, no go */
243794f7e29aSAlan Cox 	if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
243894f7e29aSAlan Cox 		vm_map_unlock(map);
243994f7e29aSAlan Cox 		return (KERN_NO_SPACE);
244094f7e29aSAlan Cox 	}
244194f7e29aSAlan Cox 
244294f7e29aSAlan Cox 	/* If we can't accomodate max_ssize in the current mapping,
244394f7e29aSAlan Cox 	 * no go.  However, we need to be aware that subsequent user
244494f7e29aSAlan Cox 	 * mappings might map into the space we have reserved for
244594f7e29aSAlan Cox 	 * stack, and currently this space is not protected.
244694f7e29aSAlan Cox 	 *
244794f7e29aSAlan Cox 	 * Hopefully we will at least detect this condition
244894f7e29aSAlan Cox 	 * when we try to grow the stack.
244994f7e29aSAlan Cox 	 */
245094f7e29aSAlan Cox 	if ((prev_entry->next != &map->header) &&
245194f7e29aSAlan Cox 	    (prev_entry->next->start < addrbos + max_ssize)) {
245294f7e29aSAlan Cox 		vm_map_unlock(map);
245394f7e29aSAlan Cox 		return (KERN_NO_SPACE);
245494f7e29aSAlan Cox 	}
245594f7e29aSAlan Cox 
245694f7e29aSAlan Cox 	/* We initially map a stack of only init_ssize.  We will
245794f7e29aSAlan Cox 	 * grow as needed later.  Since this is to be a grow
245894f7e29aSAlan Cox 	 * down stack, we map at the top of the range.
245994f7e29aSAlan Cox 	 *
246094f7e29aSAlan Cox 	 * Note: we would normally expect prot and max to be
246194f7e29aSAlan Cox 	 * VM_PROT_ALL, and cow to be 0.  Possibly we should
246294f7e29aSAlan Cox 	 * eliminate these as input parameters, and just
246394f7e29aSAlan Cox 	 * pass these values here in the insert call.
246494f7e29aSAlan Cox 	 */
246594f7e29aSAlan Cox 	rv = vm_map_insert(map, NULL, 0, addrbos + max_ssize - init_ssize,
246694f7e29aSAlan Cox 	                   addrbos + max_ssize, prot, max, cow);
246794f7e29aSAlan Cox 
246894f7e29aSAlan Cox 	/* Now set the avail_ssize amount */
246994f7e29aSAlan Cox 	if (rv == KERN_SUCCESS){
247029b45e9eSAlan Cox 		if (prev_entry != &map->header)
247129b45e9eSAlan Cox 			vm_map_clip_end(map, prev_entry, addrbos + max_ssize - init_ssize);
247294f7e29aSAlan Cox 		new_stack_entry = prev_entry->next;
247394f7e29aSAlan Cox 		if (new_stack_entry->end   != addrbos + max_ssize ||
247494f7e29aSAlan Cox 		    new_stack_entry->start != addrbos + max_ssize - init_ssize)
247594f7e29aSAlan Cox 			panic ("Bad entry start/end for new stack entry");
247694f7e29aSAlan Cox 		else
247794f7e29aSAlan Cox 			new_stack_entry->avail_ssize = max_ssize - init_ssize;
247894f7e29aSAlan Cox 	}
247994f7e29aSAlan Cox 
248094f7e29aSAlan Cox 	vm_map_unlock(map);
248194f7e29aSAlan Cox 	return (rv);
248294f7e29aSAlan Cox }
248394f7e29aSAlan Cox 
248494f7e29aSAlan Cox /* Attempts to grow a vm stack entry.  Returns KERN_SUCCESS if the
248594f7e29aSAlan Cox  * desired address is already mapped, or if we successfully grow
248694f7e29aSAlan Cox  * the stack.  Also returns KERN_SUCCESS if addr is outside the
248794f7e29aSAlan Cox  * stack range (this is strange, but preserves compatibility with
248894f7e29aSAlan Cox  * the grow function in vm_machdep.c).
248994f7e29aSAlan Cox  */
249094f7e29aSAlan Cox int
249194f7e29aSAlan Cox vm_map_growstack (struct proc *p, vm_offset_t addr)
249294f7e29aSAlan Cox {
249394f7e29aSAlan Cox 	vm_map_entry_t prev_entry;
249494f7e29aSAlan Cox 	vm_map_entry_t stack_entry;
249594f7e29aSAlan Cox 	vm_map_entry_t new_stack_entry;
249694f7e29aSAlan Cox 	struct vmspace *vm = p->p_vmspace;
249794f7e29aSAlan Cox 	vm_map_t map = &vm->vm_map;
249894f7e29aSAlan Cox 	vm_offset_t    end;
249994f7e29aSAlan Cox 	int      grow_amount;
250094f7e29aSAlan Cox 	int      rv;
250194f7e29aSAlan Cox 	int      is_procstack;
250223955314SAlfred Perlstein 
25030cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
250423955314SAlfred Perlstein 
250594f7e29aSAlan Cox Retry:
250694f7e29aSAlan Cox 	vm_map_lock_read(map);
250794f7e29aSAlan Cox 
250894f7e29aSAlan Cox 	/* If addr is already in the entry range, no need to grow.*/
250994f7e29aSAlan Cox 	if (vm_map_lookup_entry(map, addr, &prev_entry)) {
251094f7e29aSAlan Cox 		vm_map_unlock_read(map);
25110cddd8f0SMatthew Dillon 		return (KERN_SUCCESS);
251294f7e29aSAlan Cox 	}
251394f7e29aSAlan Cox 
251494f7e29aSAlan Cox 	if ((stack_entry = prev_entry->next) == &map->header) {
251594f7e29aSAlan Cox 		vm_map_unlock_read(map);
25160cddd8f0SMatthew Dillon 		return (KERN_SUCCESS);
251794f7e29aSAlan Cox 	}
251894f7e29aSAlan Cox 	if (prev_entry == &map->header)
251994f7e29aSAlan Cox 		end = stack_entry->start - stack_entry->avail_ssize;
252094f7e29aSAlan Cox 	else
252194f7e29aSAlan Cox 		end = prev_entry->end;
252294f7e29aSAlan Cox 
252394f7e29aSAlan Cox 	/* This next test mimics the old grow function in vm_machdep.c.
252494f7e29aSAlan Cox 	 * It really doesn't quite make sense, but we do it anyway
252594f7e29aSAlan Cox 	 * for compatibility.
252694f7e29aSAlan Cox 	 *
252794f7e29aSAlan Cox 	 * If not growable stack, return success.  This signals the
252894f7e29aSAlan Cox 	 * caller to proceed as he would normally with normal vm.
252994f7e29aSAlan Cox 	 */
253094f7e29aSAlan Cox 	if (stack_entry->avail_ssize < 1 ||
253194f7e29aSAlan Cox 	    addr >= stack_entry->start ||
253294f7e29aSAlan Cox 	    addr <  stack_entry->start - stack_entry->avail_ssize) {
253394f7e29aSAlan Cox 		vm_map_unlock_read(map);
25340cddd8f0SMatthew Dillon 		return (KERN_SUCCESS);
253594f7e29aSAlan Cox 	}
253694f7e29aSAlan Cox 
253794f7e29aSAlan Cox 	/* Find the minimum grow amount */
253894f7e29aSAlan Cox 	grow_amount = roundup (stack_entry->start - addr, PAGE_SIZE);
253994f7e29aSAlan Cox 	if (grow_amount > stack_entry->avail_ssize) {
254094f7e29aSAlan Cox 		vm_map_unlock_read(map);
25410cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
254294f7e29aSAlan Cox 	}
254394f7e29aSAlan Cox 
254494f7e29aSAlan Cox 	/* If there is no longer enough space between the entries
254594f7e29aSAlan Cox 	 * nogo, and adjust the available space.  Note: this
254694f7e29aSAlan Cox 	 * should only happen if the user has mapped into the
254794f7e29aSAlan Cox 	 * stack area after the stack was created, and is
254894f7e29aSAlan Cox 	 * probably an error.
254994f7e29aSAlan Cox 	 *
255094f7e29aSAlan Cox 	 * This also effectively destroys any guard page the user
255194f7e29aSAlan Cox 	 * might have intended by limiting the stack size.
255294f7e29aSAlan Cox 	 */
255394f7e29aSAlan Cox 	if (grow_amount > stack_entry->start - end) {
255494f7e29aSAlan Cox 		if (vm_map_lock_upgrade(map))
255594f7e29aSAlan Cox 			goto Retry;
255694f7e29aSAlan Cox 
255794f7e29aSAlan Cox 		stack_entry->avail_ssize = stack_entry->start - end;
255894f7e29aSAlan Cox 
255994f7e29aSAlan Cox 		vm_map_unlock(map);
25600cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
256194f7e29aSAlan Cox 	}
256294f7e29aSAlan Cox 
256394f7e29aSAlan Cox 	is_procstack = addr >= (vm_offset_t)vm->vm_maxsaddr;
256494f7e29aSAlan Cox 
256594f7e29aSAlan Cox 	/* If this is the main process stack, see if we're over the
256694f7e29aSAlan Cox 	 * stack limit.
256794f7e29aSAlan Cox 	 */
25686389da78SAlan Cox 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount >
256994f7e29aSAlan Cox 			     p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
257094f7e29aSAlan Cox 		vm_map_unlock_read(map);
25710cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
257294f7e29aSAlan Cox 	}
257394f7e29aSAlan Cox 
257494f7e29aSAlan Cox 	/* Round up the grow amount modulo SGROWSIZ */
2575cbc89bfbSPaul Saab 	grow_amount = roundup (grow_amount, sgrowsiz);
257694f7e29aSAlan Cox 	if (grow_amount > stack_entry->avail_ssize) {
257794f7e29aSAlan Cox 		grow_amount = stack_entry->avail_ssize;
257894f7e29aSAlan Cox 	}
25796389da78SAlan Cox 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount >
258094f7e29aSAlan Cox 	                     p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
258194f7e29aSAlan Cox 		grow_amount = p->p_rlimit[RLIMIT_STACK].rlim_cur -
25826389da78SAlan Cox 		              ctob(vm->vm_ssize);
258394f7e29aSAlan Cox 	}
258494f7e29aSAlan Cox 
258594f7e29aSAlan Cox 	if (vm_map_lock_upgrade(map))
258694f7e29aSAlan Cox 		goto Retry;
258794f7e29aSAlan Cox 
258894f7e29aSAlan Cox 	/* Get the preliminary new entry start value */
258994f7e29aSAlan Cox 	addr = stack_entry->start - grow_amount;
259094f7e29aSAlan Cox 
259194f7e29aSAlan Cox 	/* If this puts us into the previous entry, cut back our growth
259294f7e29aSAlan Cox 	 * to the available space.  Also, see the note above.
259394f7e29aSAlan Cox 	 */
259494f7e29aSAlan Cox 	if (addr < end) {
259594f7e29aSAlan Cox 		stack_entry->avail_ssize = stack_entry->start - end;
259694f7e29aSAlan Cox 		addr = end;
259794f7e29aSAlan Cox 	}
259894f7e29aSAlan Cox 
259994f7e29aSAlan Cox 	rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start,
260029b45e9eSAlan Cox 			   VM_PROT_ALL,
260129b45e9eSAlan Cox 			   VM_PROT_ALL,
260294f7e29aSAlan Cox 			   0);
260394f7e29aSAlan Cox 
260494f7e29aSAlan Cox 	/* Adjust the available stack space by the amount we grew. */
260594f7e29aSAlan Cox 	if (rv == KERN_SUCCESS) {
260629b45e9eSAlan Cox 		if (prev_entry != &map->header)
260729b45e9eSAlan Cox 			vm_map_clip_end(map, prev_entry, addr);
260894f7e29aSAlan Cox 		new_stack_entry = prev_entry->next;
260994f7e29aSAlan Cox 		if (new_stack_entry->end   != stack_entry->start  ||
261094f7e29aSAlan Cox 		    new_stack_entry->start != addr)
261194f7e29aSAlan Cox 			panic ("Bad stack grow start/end in new stack entry");
261294f7e29aSAlan Cox 		else {
261394f7e29aSAlan Cox 			new_stack_entry->avail_ssize = stack_entry->avail_ssize -
261494f7e29aSAlan Cox 							(new_stack_entry->end -
261594f7e29aSAlan Cox 							 new_stack_entry->start);
261694f7e29aSAlan Cox 			if (is_procstack)
26176389da78SAlan Cox 				vm->vm_ssize += btoc(new_stack_entry->end -
26186389da78SAlan Cox 						     new_stack_entry->start);
261994f7e29aSAlan Cox 		}
262094f7e29aSAlan Cox 	}
262194f7e29aSAlan Cox 
262294f7e29aSAlan Cox 	vm_map_unlock(map);
26230cddd8f0SMatthew Dillon 	return (rv);
262494f7e29aSAlan Cox }
262594f7e29aSAlan Cox 
2626df8bae1dSRodney W. Grimes /*
26275856e12eSJohn Dyson  * Unshare the specified VM space for exec.  If other processes are
26285856e12eSJohn Dyson  * mapped to it, then create a new one.  The new vmspace is null.
26295856e12eSJohn Dyson  */
26305856e12eSJohn Dyson 
26315856e12eSJohn Dyson void
26321b40f8c0SMatthew Dillon vmspace_exec(struct proc *p)
26331b40f8c0SMatthew Dillon {
26345856e12eSJohn Dyson 	struct vmspace *oldvmspace = p->p_vmspace;
26355856e12eSJohn Dyson 	struct vmspace *newvmspace;
26365856e12eSJohn Dyson 	vm_map_t map = &p->p_vmspace->vm_map;
26375856e12eSJohn Dyson 
26380cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
26392d8acc0fSJohn Dyson 	newvmspace = vmspace_alloc(map->min_offset, map->max_offset);
26405856e12eSJohn Dyson 	bcopy(&oldvmspace->vm_startcopy, &newvmspace->vm_startcopy,
26415856e12eSJohn Dyson 	    (caddr_t) (newvmspace + 1) - (caddr_t) &newvmspace->vm_startcopy);
26425856e12eSJohn Dyson 	/*
26435856e12eSJohn Dyson 	 * This code is written like this for prototype purposes.  The
26445856e12eSJohn Dyson 	 * goal is to avoid running down the vmspace here, but let the
26455856e12eSJohn Dyson 	 * other process's that are still using the vmspace to finally
26465856e12eSJohn Dyson 	 * run it down.  Even though there is little or no chance of blocking
26475856e12eSJohn Dyson 	 * here, it is a good idea to keep this form for future mods.
26485856e12eSJohn Dyson 	 */
26495856e12eSJohn Dyson 	p->p_vmspace = newvmspace;
2650d4da2dbaSAlan Cox 	pmap_pinit2(vmspace_pmap(newvmspace));
265121c641b2SJohn Baldwin 	vmspace_free(oldvmspace);
2652b40ce416SJulian Elischer 	if (p == curthread->td_proc)		/* XXXKSE ? */
2653b40ce416SJulian Elischer 		pmap_activate(curthread);
26545856e12eSJohn Dyson }
26555856e12eSJohn Dyson 
26565856e12eSJohn Dyson /*
26575856e12eSJohn Dyson  * Unshare the specified VM space for forcing COW.  This
26585856e12eSJohn Dyson  * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
26595856e12eSJohn Dyson  */
26605856e12eSJohn Dyson 
26615856e12eSJohn Dyson void
26621b40f8c0SMatthew Dillon vmspace_unshare(struct proc *p)
26631b40f8c0SMatthew Dillon {
26645856e12eSJohn Dyson 	struct vmspace *oldvmspace = p->p_vmspace;
26655856e12eSJohn Dyson 	struct vmspace *newvmspace;
26665856e12eSJohn Dyson 
26670cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
26685856e12eSJohn Dyson 	if (oldvmspace->vm_refcnt == 1)
26695856e12eSJohn Dyson 		return;
26705856e12eSJohn Dyson 	newvmspace = vmspace_fork(oldvmspace);
26715856e12eSJohn Dyson 	p->p_vmspace = newvmspace;
2672d4da2dbaSAlan Cox 	pmap_pinit2(vmspace_pmap(newvmspace));
267321c641b2SJohn Baldwin 	vmspace_free(oldvmspace);
2674b40ce416SJulian Elischer 	if (p == curthread->td_proc)		/* XXXKSE ? */
2675b40ce416SJulian Elischer 		pmap_activate(curthread);
26765856e12eSJohn Dyson }
26775856e12eSJohn Dyson 
26785856e12eSJohn Dyson 
26795856e12eSJohn Dyson /*
2680df8bae1dSRodney W. Grimes  *	vm_map_lookup:
2681df8bae1dSRodney W. Grimes  *
2682df8bae1dSRodney W. Grimes  *	Finds the VM object, offset, and
2683df8bae1dSRodney W. Grimes  *	protection for a given virtual address in the
2684df8bae1dSRodney W. Grimes  *	specified map, assuming a page fault of the
2685df8bae1dSRodney W. Grimes  *	type specified.
2686df8bae1dSRodney W. Grimes  *
2687df8bae1dSRodney W. Grimes  *	Leaves the map in question locked for read; return
2688df8bae1dSRodney W. Grimes  *	values are guaranteed until a vm_map_lookup_done
2689df8bae1dSRodney W. Grimes  *	call is performed.  Note that the map argument
2690df8bae1dSRodney W. Grimes  *	is in/out; the returned map must be used in
2691df8bae1dSRodney W. Grimes  *	the call to vm_map_lookup_done.
2692df8bae1dSRodney W. Grimes  *
2693df8bae1dSRodney W. Grimes  *	A handle (out_entry) is returned for use in
2694df8bae1dSRodney W. Grimes  *	vm_map_lookup_done, to make that fast.
2695df8bae1dSRodney W. Grimes  *
2696df8bae1dSRodney W. Grimes  *	If a lookup is requested with "write protection"
2697df8bae1dSRodney W. Grimes  *	specified, the map may be changed to perform virtual
2698df8bae1dSRodney W. Grimes  *	copying operations, although the data referenced will
2699df8bae1dSRodney W. Grimes  *	remain the same.
2700df8bae1dSRodney W. Grimes  */
2701df8bae1dSRodney W. Grimes int
2702b9dcd593SBruce Evans vm_map_lookup(vm_map_t *var_map,		/* IN/OUT */
2703b9dcd593SBruce Evans 	      vm_offset_t vaddr,
270447221757SJohn Dyson 	      vm_prot_t fault_typea,
2705b9dcd593SBruce Evans 	      vm_map_entry_t *out_entry,	/* OUT */
2706b9dcd593SBruce Evans 	      vm_object_t *object,		/* OUT */
2707b9dcd593SBruce Evans 	      vm_pindex_t *pindex,		/* OUT */
2708b9dcd593SBruce Evans 	      vm_prot_t *out_prot,		/* OUT */
27092d8acc0fSJohn Dyson 	      boolean_t *wired)			/* OUT */
2710df8bae1dSRodney W. Grimes {
2711c0877f10SJohn Dyson 	vm_map_entry_t entry;
2712c0877f10SJohn Dyson 	vm_map_t map = *var_map;
2713c0877f10SJohn Dyson 	vm_prot_t prot;
271447221757SJohn Dyson 	vm_prot_t fault_type = fault_typea;
2715df8bae1dSRodney W. Grimes 
27160cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
2717df8bae1dSRodney W. Grimes RetryLookup:;
2718df8bae1dSRodney W. Grimes 
2719df8bae1dSRodney W. Grimes 	/*
2720df8bae1dSRodney W. Grimes 	 * Lookup the faulting address.
2721df8bae1dSRodney W. Grimes 	 */
2722df8bae1dSRodney W. Grimes 
2723df8bae1dSRodney W. Grimes 	vm_map_lock_read(map);
2724df8bae1dSRodney W. Grimes 
2725df8bae1dSRodney W. Grimes #define	RETURN(why) \
2726df8bae1dSRodney W. Grimes 		{ \
2727df8bae1dSRodney W. Grimes 		vm_map_unlock_read(map); \
2728df8bae1dSRodney W. Grimes 		return(why); \
2729df8bae1dSRodney W. Grimes 		}
2730df8bae1dSRodney W. Grimes 
2731df8bae1dSRodney W. Grimes 	/*
27320d94caffSDavid Greenman 	 * If the map has an interesting hint, try it before calling full
27330d94caffSDavid Greenman 	 * blown lookup routine.
2734df8bae1dSRodney W. Grimes 	 */
2735df8bae1dSRodney W. Grimes 
2736df8bae1dSRodney W. Grimes 	entry = map->hint;
2737df8bae1dSRodney W. Grimes 
2738df8bae1dSRodney W. Grimes 	*out_entry = entry;
2739df8bae1dSRodney W. Grimes 
2740df8bae1dSRodney W. Grimes 	if ((entry == &map->header) ||
2741df8bae1dSRodney W. Grimes 	    (vaddr < entry->start) || (vaddr >= entry->end)) {
2742df8bae1dSRodney W. Grimes 		vm_map_entry_t tmp_entry;
2743df8bae1dSRodney W. Grimes 
2744df8bae1dSRodney W. Grimes 		/*
27450d94caffSDavid Greenman 		 * Entry was either not a valid hint, or the vaddr was not
27460d94caffSDavid Greenman 		 * contained in the entry, so do a full lookup.
2747df8bae1dSRodney W. Grimes 		 */
2748df8bae1dSRodney W. Grimes 		if (!vm_map_lookup_entry(map, vaddr, &tmp_entry))
2749df8bae1dSRodney W. Grimes 			RETURN(KERN_INVALID_ADDRESS);
2750df8bae1dSRodney W. Grimes 
2751df8bae1dSRodney W. Grimes 		entry = tmp_entry;
2752df8bae1dSRodney W. Grimes 		*out_entry = entry;
2753df8bae1dSRodney W. Grimes 	}
2754b7b2aac2SJohn Dyson 
2755df8bae1dSRodney W. Grimes 	/*
2756df8bae1dSRodney W. Grimes 	 * Handle submaps.
2757df8bae1dSRodney W. Grimes 	 */
2758df8bae1dSRodney W. Grimes 
2759afa07f7eSJohn Dyson 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
2760df8bae1dSRodney W. Grimes 		vm_map_t old_map = map;
2761df8bae1dSRodney W. Grimes 
2762df8bae1dSRodney W. Grimes 		*var_map = map = entry->object.sub_map;
2763df8bae1dSRodney W. Grimes 		vm_map_unlock_read(old_map);
2764df8bae1dSRodney W. Grimes 		goto RetryLookup;
2765df8bae1dSRodney W. Grimes 	}
2766a04c970aSJohn Dyson 
2767df8bae1dSRodney W. Grimes 	/*
27680d94caffSDavid Greenman 	 * Check whether this task is allowed to have this page.
2769a04c970aSJohn Dyson 	 * Note the special case for MAP_ENTRY_COW
2770a04c970aSJohn Dyson 	 * pages with an override.  This is to implement a forced
2771a04c970aSJohn Dyson 	 * COW for debuggers.
2772df8bae1dSRodney W. Grimes 	 */
2773df8bae1dSRodney W. Grimes 
2774480ba2f5SJohn Dyson 	if (fault_type & VM_PROT_OVERRIDE_WRITE)
2775480ba2f5SJohn Dyson 		prot = entry->max_protection;
2776480ba2f5SJohn Dyson 	else
2777df8bae1dSRodney W. Grimes 		prot = entry->protection;
277847221757SJohn Dyson 
277947221757SJohn Dyson 	fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
278047221757SJohn Dyson 	if ((fault_type & prot) != fault_type) {
278147221757SJohn Dyson 			RETURN(KERN_PROTECTION_FAILURE);
278247221757SJohn Dyson 	}
278347221757SJohn Dyson 
27842ed14a92SAlan Cox 	if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
278547221757SJohn Dyson 	    (entry->eflags & MAP_ENTRY_COW) &&
27862ed14a92SAlan Cox 	    (fault_type & VM_PROT_WRITE) &&
278747221757SJohn Dyson 	    (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
2788df8bae1dSRodney W. Grimes 		RETURN(KERN_PROTECTION_FAILURE);
2789a04c970aSJohn Dyson 	}
2790df8bae1dSRodney W. Grimes 
2791df8bae1dSRodney W. Grimes 	/*
27920d94caffSDavid Greenman 	 * If this page is not pageable, we have to get it for all possible
27930d94caffSDavid Greenman 	 * accesses.
2794df8bae1dSRodney W. Grimes 	 */
2795df8bae1dSRodney W. Grimes 
279605f0fdd2SPoul-Henning Kamp 	*wired = (entry->wired_count != 0);
279705f0fdd2SPoul-Henning Kamp 	if (*wired)
2798df8bae1dSRodney W. Grimes 		prot = fault_type = entry->protection;
2799df8bae1dSRodney W. Grimes 
2800df8bae1dSRodney W. Grimes 	/*
2801df8bae1dSRodney W. Grimes 	 * If the entry was copy-on-write, we either ...
2802df8bae1dSRodney W. Grimes 	 */
2803df8bae1dSRodney W. Grimes 
2804afa07f7eSJohn Dyson 	if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
2805df8bae1dSRodney W. Grimes 		/*
28060d94caffSDavid Greenman 		 * If we want to write the page, we may as well handle that
2807ad5fca3bSAlan Cox 		 * now since we've got the map locked.
2808df8bae1dSRodney W. Grimes 		 *
28090d94caffSDavid Greenman 		 * If we don't need to write the page, we just demote the
28100d94caffSDavid Greenman 		 * permissions allowed.
2811df8bae1dSRodney W. Grimes 		 */
2812df8bae1dSRodney W. Grimes 
2813df8bae1dSRodney W. Grimes 		if (fault_type & VM_PROT_WRITE) {
2814df8bae1dSRodney W. Grimes 			/*
28150d94caffSDavid Greenman 			 * Make a new object, and place it in the object
28160d94caffSDavid Greenman 			 * chain.  Note that no new references have appeared
2817ad5fca3bSAlan Cox 			 * -- one just moved from the map to the new
28180d94caffSDavid Greenman 			 * object.
2819df8bae1dSRodney W. Grimes 			 */
2820df8bae1dSRodney W. Grimes 
28219b09b6c7SMatthew Dillon 			if (vm_map_lock_upgrade(map))
2822df8bae1dSRodney W. Grimes 				goto RetryLookup;
28239b09b6c7SMatthew Dillon 
2824df8bae1dSRodney W. Grimes 			vm_object_shadow(
2825df8bae1dSRodney W. Grimes 			    &entry->object.vm_object,
2826df8bae1dSRodney W. Grimes 			    &entry->offset,
2827c2e11a03SJohn Dyson 			    atop(entry->end - entry->start));
2828df8bae1dSRodney W. Grimes 
2829afa07f7eSJohn Dyson 			entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
28309b09b6c7SMatthew Dillon 			vm_map_lock_downgrade(map);
28310d94caffSDavid Greenman 		} else {
2832df8bae1dSRodney W. Grimes 			/*
28330d94caffSDavid Greenman 			 * We're attempting to read a copy-on-write page --
28340d94caffSDavid Greenman 			 * don't allow writes.
2835df8bae1dSRodney W. Grimes 			 */
2836df8bae1dSRodney W. Grimes 
28372d8acc0fSJohn Dyson 			prot &= ~VM_PROT_WRITE;
2838df8bae1dSRodney W. Grimes 		}
2839df8bae1dSRodney W. Grimes 	}
28402d8acc0fSJohn Dyson 
2841df8bae1dSRodney W. Grimes 	/*
2842df8bae1dSRodney W. Grimes 	 * Create an object if necessary.
2843df8bae1dSRodney W. Grimes 	 */
28444e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL &&
28454e71e795SMatthew Dillon 	    !map->system_map) {
28469b09b6c7SMatthew Dillon 		if (vm_map_lock_upgrade(map))
2847df8bae1dSRodney W. Grimes 			goto RetryLookup;
28489b09b6c7SMatthew Dillon 
284924a1cce3SDavid Greenman 		entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
2850c2e11a03SJohn Dyson 		    atop(entry->end - entry->start));
2851df8bae1dSRodney W. Grimes 		entry->offset = 0;
28529b09b6c7SMatthew Dillon 		vm_map_lock_downgrade(map);
2853df8bae1dSRodney W. Grimes 	}
2854b5b40fa6SJohn Dyson 
2855df8bae1dSRodney W. Grimes 	/*
28560d94caffSDavid Greenman 	 * Return the object/offset from this entry.  If the entry was
28570d94caffSDavid Greenman 	 * copy-on-write or empty, it has been fixed up.
2858df8bae1dSRodney W. Grimes 	 */
2859df8bae1dSRodney W. Grimes 
28609b09b6c7SMatthew Dillon 	*pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
2861df8bae1dSRodney W. Grimes 	*object = entry->object.vm_object;
2862df8bae1dSRodney W. Grimes 
2863df8bae1dSRodney W. Grimes 	/*
2864df8bae1dSRodney W. Grimes 	 * Return whether this is the only map sharing this data.
2865df8bae1dSRodney W. Grimes 	 */
2866df8bae1dSRodney W. Grimes 
2867df8bae1dSRodney W. Grimes 	*out_prot = prot;
2868df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
2869df8bae1dSRodney W. Grimes 
2870df8bae1dSRodney W. Grimes #undef	RETURN
2871df8bae1dSRodney W. Grimes }
2872df8bae1dSRodney W. Grimes 
2873df8bae1dSRodney W. Grimes /*
2874df8bae1dSRodney W. Grimes  *	vm_map_lookup_done:
2875df8bae1dSRodney W. Grimes  *
2876df8bae1dSRodney W. Grimes  *	Releases locks acquired by a vm_map_lookup
2877df8bae1dSRodney W. Grimes  *	(according to the handle returned by that lookup).
2878df8bae1dSRodney W. Grimes  */
2879df8bae1dSRodney W. Grimes 
28800d94caffSDavid Greenman void
28811b40f8c0SMatthew Dillon vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry)
2882df8bae1dSRodney W. Grimes {
2883df8bae1dSRodney W. Grimes 	/*
2884df8bae1dSRodney W. Grimes 	 * Unlock the main-level map
2885df8bae1dSRodney W. Grimes 	 */
28860cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
2887df8bae1dSRodney W. Grimes 	vm_map_unlock_read(map);
2888df8bae1dSRodney W. Grimes }
2889df8bae1dSRodney W. Grimes 
28901efb74fbSJohn Dyson /*
28911efb74fbSJohn Dyson  * Implement uiomove with VM operations.  This handles (and collateral changes)
28921efb74fbSJohn Dyson  * support every combination of source object modification, and COW type
28931efb74fbSJohn Dyson  * operations.
28941efb74fbSJohn Dyson  */
28951efb74fbSJohn Dyson int
28961b40f8c0SMatthew Dillon vm_uiomove(
28971b40f8c0SMatthew Dillon 	vm_map_t mapa,
28981b40f8c0SMatthew Dillon 	vm_object_t srcobject,
28991b40f8c0SMatthew Dillon 	off_t cp,
29001b40f8c0SMatthew Dillon 	int cnta,
29011b40f8c0SMatthew Dillon 	vm_offset_t uaddra,
29021b40f8c0SMatthew Dillon 	int *npages)
29031efb74fbSJohn Dyson {
29041efb74fbSJohn Dyson 	vm_map_t map;
290547221757SJohn Dyson 	vm_object_t first_object, oldobject, object;
29062d8acc0fSJohn Dyson 	vm_map_entry_t entry;
29071efb74fbSJohn Dyson 	vm_prot_t prot;
29082d8acc0fSJohn Dyson 	boolean_t wired;
29091efb74fbSJohn Dyson 	int tcnt, rv;
29102d8acc0fSJohn Dyson 	vm_offset_t uaddr, start, end, tend;
29111efb74fbSJohn Dyson 	vm_pindex_t first_pindex, osize, oindex;
29121efb74fbSJohn Dyson 	off_t ooffset;
291347221757SJohn Dyson 	int cnt;
29141efb74fbSJohn Dyson 
29150cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
29160cddd8f0SMatthew Dillon 
291795e5e988SJohn Dyson 	if (npages)
291895e5e988SJohn Dyson 		*npages = 0;
291995e5e988SJohn Dyson 
292047221757SJohn Dyson 	cnt = cnta;
29212d8acc0fSJohn Dyson 	uaddr = uaddra;
29222d8acc0fSJohn Dyson 
29231efb74fbSJohn Dyson 	while (cnt > 0) {
29241efb74fbSJohn Dyson 		map = mapa;
29251efb74fbSJohn Dyson 
29261efb74fbSJohn Dyson 		if ((vm_map_lookup(&map, uaddr,
29272d8acc0fSJohn Dyson 			VM_PROT_READ, &entry, &first_object,
29282d8acc0fSJohn Dyson 			&first_pindex, &prot, &wired)) != KERN_SUCCESS) {
29291efb74fbSJohn Dyson 			return EFAULT;
29301efb74fbSJohn Dyson 		}
29311efb74fbSJohn Dyson 
29322d8acc0fSJohn Dyson 		vm_map_clip_start(map, entry, uaddr);
29331efb74fbSJohn Dyson 
29341efb74fbSJohn Dyson 		tcnt = cnt;
29352d8acc0fSJohn Dyson 		tend = uaddr + tcnt;
29362d8acc0fSJohn Dyson 		if (tend > entry->end) {
29372d8acc0fSJohn Dyson 			tcnt = entry->end - uaddr;
29382d8acc0fSJohn Dyson 			tend = entry->end;
29392d8acc0fSJohn Dyson 		}
29401efb74fbSJohn Dyson 
29412d8acc0fSJohn Dyson 		vm_map_clip_end(map, entry, tend);
29421efb74fbSJohn Dyson 
29432d8acc0fSJohn Dyson 		start = entry->start;
29442d8acc0fSJohn Dyson 		end = entry->end;
29451efb74fbSJohn Dyson 
2946c2e11a03SJohn Dyson 		osize = atop(tcnt);
294795e5e988SJohn Dyson 
2948925a3a41SJohn Dyson 		oindex = OFF_TO_IDX(cp);
294995e5e988SJohn Dyson 		if (npages) {
2950925a3a41SJohn Dyson 			vm_pindex_t idx;
295195e5e988SJohn Dyson 			for (idx = 0; idx < osize; idx++) {
295295e5e988SJohn Dyson 				vm_page_t m;
2953925a3a41SJohn Dyson 				if ((m = vm_page_lookup(srcobject, oindex + idx)) == NULL) {
29542d8acc0fSJohn Dyson 					vm_map_lookup_done(map, entry);
295595e5e988SJohn Dyson 					return 0;
295695e5e988SJohn Dyson 				}
29571c7c3c6aSMatthew Dillon 				/*
29581c7c3c6aSMatthew Dillon 				 * disallow busy or invalid pages, but allow
29591c7c3c6aSMatthew Dillon 				 * m->busy pages if they are entirely valid.
29601c7c3c6aSMatthew Dillon 				 */
2961925a3a41SJohn Dyson 				if ((m->flags & PG_BUSY) ||
296295e5e988SJohn Dyson 					((m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL)) {
29632d8acc0fSJohn Dyson 					vm_map_lookup_done(map, entry);
296495e5e988SJohn Dyson 					return 0;
296595e5e988SJohn Dyson 				}
296695e5e988SJohn Dyson 			}
296795e5e988SJohn Dyson 		}
296895e5e988SJohn Dyson 
29691efb74fbSJohn Dyson /*
29701efb74fbSJohn Dyson  * If we are changing an existing map entry, just redirect
29711efb74fbSJohn Dyson  * the object, and change mappings.
29721efb74fbSJohn Dyson  */
29732d8acc0fSJohn Dyson 		if ((first_object->type == OBJT_VNODE) &&
29742d8acc0fSJohn Dyson 			((oldobject = entry->object.vm_object) == first_object)) {
29752d8acc0fSJohn Dyson 
29762d8acc0fSJohn Dyson 			if ((entry->offset != cp) || (oldobject != srcobject)) {
29772d8acc0fSJohn Dyson 				/*
29782d8acc0fSJohn Dyson    				* Remove old window into the file
29792d8acc0fSJohn Dyson    				*/
29802d8acc0fSJohn Dyson 				pmap_remove (map->pmap, uaddr, tend);
29812d8acc0fSJohn Dyson 
29822d8acc0fSJohn Dyson 				/*
29832d8acc0fSJohn Dyson    				* Force copy on write for mmaped regions
29842d8acc0fSJohn Dyson    				*/
29852d8acc0fSJohn Dyson 				vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
29862d8acc0fSJohn Dyson 
29872d8acc0fSJohn Dyson 				/*
29882d8acc0fSJohn Dyson    				* Point the object appropriately
29892d8acc0fSJohn Dyson    				*/
29902d8acc0fSJohn Dyson 				if (oldobject != srcobject) {
29912d8acc0fSJohn Dyson 
29922d8acc0fSJohn Dyson 				/*
29932d8acc0fSJohn Dyson    				* Set the object optimization hint flag
29942d8acc0fSJohn Dyson    				*/
2995069e9bc1SDoug Rabson 					vm_object_set_flag(srcobject, OBJ_OPT);
29962d8acc0fSJohn Dyson 					vm_object_reference(srcobject);
29972d8acc0fSJohn Dyson 					entry->object.vm_object = srcobject;
29982d8acc0fSJohn Dyson 
29992d8acc0fSJohn Dyson 					if (oldobject) {
30002d8acc0fSJohn Dyson 						vm_object_deallocate(oldobject);
30012d8acc0fSJohn Dyson 					}
30022d8acc0fSJohn Dyson 				}
30032d8acc0fSJohn Dyson 
30042d8acc0fSJohn Dyson 				entry->offset = cp;
30052d8acc0fSJohn Dyson 				map->timestamp++;
30062d8acc0fSJohn Dyson 			} else {
30072d8acc0fSJohn Dyson 				pmap_remove (map->pmap, uaddr, tend);
30082d8acc0fSJohn Dyson 			}
30092d8acc0fSJohn Dyson 
30102d8acc0fSJohn Dyson 		} else if ((first_object->ref_count == 1) &&
3011925a3a41SJohn Dyson 			(first_object->size == osize) &&
301247221757SJohn Dyson 			((first_object->type == OBJT_DEFAULT) ||
301347221757SJohn Dyson 				(first_object->type == OBJT_SWAP)) ) {
3014925a3a41SJohn Dyson 
3015925a3a41SJohn Dyson 			oldobject = first_object->backing_object;
3016925a3a41SJohn Dyson 
3017925a3a41SJohn Dyson 			if ((first_object->backing_object_offset != cp) ||
3018925a3a41SJohn Dyson 				(oldobject != srcobject)) {
3019925a3a41SJohn Dyson 				/*
3020925a3a41SJohn Dyson    				* Remove old window into the file
3021925a3a41SJohn Dyson    				*/
30222d8acc0fSJohn Dyson 				pmap_remove (map->pmap, uaddr, tend);
3023925a3a41SJohn Dyson 
3024925a3a41SJohn Dyson 				/*
302547221757SJohn Dyson 				 * Remove unneeded old pages
302647221757SJohn Dyson 				 */
302747221757SJohn Dyson 				vm_object_page_remove(first_object, 0, 0, 0);
302847221757SJohn Dyson 
302947221757SJohn Dyson 				/*
303047221757SJohn Dyson 				 * Invalidate swap space
303147221757SJohn Dyson 				 */
303247221757SJohn Dyson 				if (first_object->type == OBJT_SWAP) {
303347221757SJohn Dyson 					swap_pager_freespace(first_object,
30341c7c3c6aSMatthew Dillon 						0,
303547221757SJohn Dyson 						first_object->size);
303647221757SJohn Dyson 				}
303747221757SJohn Dyson 
303847221757SJohn Dyson 				/*
3039925a3a41SJohn Dyson    				* Force copy on write for mmaped regions
3040925a3a41SJohn Dyson    				*/
304147221757SJohn Dyson 				vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
30421efb74fbSJohn Dyson 
30431efb74fbSJohn Dyson 				/*
30441efb74fbSJohn Dyson    				* Point the object appropriately
30451efb74fbSJohn Dyson    				*/
3046925a3a41SJohn Dyson 				if (oldobject != srcobject) {
304747221757SJohn Dyson 
3048925a3a41SJohn Dyson 				/*
3049925a3a41SJohn Dyson    				* Set the object optimization hint flag
3050925a3a41SJohn Dyson    				*/
3051069e9bc1SDoug Rabson 					vm_object_set_flag(srcobject, OBJ_OPT);
3052925a3a41SJohn Dyson 					vm_object_reference(srcobject);
3053925a3a41SJohn Dyson 
3054925a3a41SJohn Dyson 					if (oldobject) {
3055925a3a41SJohn Dyson 						TAILQ_REMOVE(&oldobject->shadow_head,
3056925a3a41SJohn Dyson 							first_object, shadow_list);
3057925a3a41SJohn Dyson 						oldobject->shadow_count--;
3058b4309055SMatthew Dillon 						/* XXX bump generation? */
3059925a3a41SJohn Dyson 						vm_object_deallocate(oldobject);
3060925a3a41SJohn Dyson 					}
3061925a3a41SJohn Dyson 
3062925a3a41SJohn Dyson 					TAILQ_INSERT_TAIL(&srcobject->shadow_head,
3063925a3a41SJohn Dyson 						first_object, shadow_list);
3064925a3a41SJohn Dyson 					srcobject->shadow_count++;
3065b4309055SMatthew Dillon 					/* XXX bump generation? */
3066925a3a41SJohn Dyson 
3067925a3a41SJohn Dyson 					first_object->backing_object = srcobject;
3068925a3a41SJohn Dyson 				}
30691efb74fbSJohn Dyson 				first_object->backing_object_offset = cp;
30702d8acc0fSJohn Dyson 				map->timestamp++;
3071925a3a41SJohn Dyson 			} else {
30722d8acc0fSJohn Dyson 				pmap_remove (map->pmap, uaddr, tend);
3073925a3a41SJohn Dyson 			}
30741efb74fbSJohn Dyson /*
30751efb74fbSJohn Dyson  * Otherwise, we have to do a logical mmap.
30761efb74fbSJohn Dyson  */
30771efb74fbSJohn Dyson 		} else {
30781efb74fbSJohn Dyson 
3079069e9bc1SDoug Rabson 			vm_object_set_flag(srcobject, OBJ_OPT);
3080925a3a41SJohn Dyson 			vm_object_reference(srcobject);
30811efb74fbSJohn Dyson 
30822d8acc0fSJohn Dyson 			pmap_remove (map->pmap, uaddr, tend);
30831efb74fbSJohn Dyson 
308447221757SJohn Dyson 			vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
308547221757SJohn Dyson 			vm_map_lock_upgrade(map);
30861efb74fbSJohn Dyson 
30872d8acc0fSJohn Dyson 			if (entry == &map->header) {
30881efb74fbSJohn Dyson 				map->first_free = &map->header;
30891efb74fbSJohn Dyson 			} else if (map->first_free->start >= start) {
30902d8acc0fSJohn Dyson 				map->first_free = entry->prev;
30911efb74fbSJohn Dyson 			}
30921efb74fbSJohn Dyson 
30932d8acc0fSJohn Dyson 			SAVE_HINT(map, entry->prev);
30942d8acc0fSJohn Dyson 			vm_map_entry_delete(map, entry);
30951efb74fbSJohn Dyson 
30962d8acc0fSJohn Dyson 			object = srcobject;
30972d8acc0fSJohn Dyson 			ooffset = cp;
30982d8acc0fSJohn Dyson 
30992d8acc0fSJohn Dyson 			rv = vm_map_insert(map, object, ooffset, start, tend,
3100e5f13bddSAlan Cox 				VM_PROT_ALL, VM_PROT_ALL, MAP_COPY_ON_WRITE);
31011efb74fbSJohn Dyson 
31021efb74fbSJohn Dyson 			if (rv != KERN_SUCCESS)
31031efb74fbSJohn Dyson 				panic("vm_uiomove: could not insert new entry: %d", rv);
31041efb74fbSJohn Dyson 		}
31051efb74fbSJohn Dyson 
31061efb74fbSJohn Dyson /*
31071efb74fbSJohn Dyson  * Map the window directly, if it is already in memory
31081efb74fbSJohn Dyson  */
31092d8acc0fSJohn Dyson 		pmap_object_init_pt(map->pmap, uaddr,
31102d8acc0fSJohn Dyson 			srcobject, oindex, tcnt, 0);
31111efb74fbSJohn Dyson 
311247221757SJohn Dyson 		map->timestamp++;
31131efb74fbSJohn Dyson 		vm_map_unlock(map);
31141efb74fbSJohn Dyson 
31151efb74fbSJohn Dyson 		cnt -= tcnt;
31162d8acc0fSJohn Dyson 		uaddr += tcnt;
31171efb74fbSJohn Dyson 		cp += tcnt;
311895e5e988SJohn Dyson 		if (npages)
311995e5e988SJohn Dyson 			*npages += osize;
31201efb74fbSJohn Dyson 	}
31211efb74fbSJohn Dyson 	return 0;
31221efb74fbSJohn Dyson }
31231efb74fbSJohn Dyson 
31241efb74fbSJohn Dyson /*
31251efb74fbSJohn Dyson  * Performs the copy_on_write operations necessary to allow the virtual copies
31261efb74fbSJohn Dyson  * into user space to work.  This has to be called for write(2) system calls
31271efb74fbSJohn Dyson  * from other processes, file unlinking, and file size shrinkage.
31281efb74fbSJohn Dyson  */
31291efb74fbSJohn Dyson void
31301b40f8c0SMatthew Dillon vm_freeze_copyopts(vm_object_t object, vm_pindex_t froma, vm_pindex_t toa)
31311efb74fbSJohn Dyson {
3132f5ef029eSPoul-Henning Kamp 	int rv;
3133f5ef029eSPoul-Henning Kamp 	vm_object_t robject;
3134f5ef029eSPoul-Henning Kamp 	vm_pindex_t idx;
31351efb74fbSJohn Dyson 
31360cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
31372d8acc0fSJohn Dyson 	if ((object == NULL) ||
313895e5e988SJohn Dyson 		((object->flags & OBJ_OPT) == 0))
313995e5e988SJohn Dyson 		return;
31401efb74fbSJohn Dyson 
31411efb74fbSJohn Dyson 	if (object->shadow_count > object->ref_count)
31421efb74fbSJohn Dyson 		panic("vm_freeze_copyopts: sc > rc");
31431efb74fbSJohn Dyson 
31448aef1712SMatthew Dillon 	while((robject = TAILQ_FIRST(&object->shadow_head)) != NULL) {
31451efb74fbSJohn Dyson 		vm_pindex_t bo_pindex;
31461efb74fbSJohn Dyson 		vm_page_t m_in, m_out;
31471efb74fbSJohn Dyson 
31481efb74fbSJohn Dyson 		bo_pindex = OFF_TO_IDX(robject->backing_object_offset);
31491efb74fbSJohn Dyson 
315095e5e988SJohn Dyson 		vm_object_reference(robject);
3151925a3a41SJohn Dyson 
315266095752SJohn Dyson 		vm_object_pip_wait(robject, "objfrz");
3153925a3a41SJohn Dyson 
31541efb74fbSJohn Dyson 		if (robject->ref_count == 1) {
31551efb74fbSJohn Dyson 			vm_object_deallocate(robject);
31561efb74fbSJohn Dyson 			continue;
31571efb74fbSJohn Dyson 		}
31581efb74fbSJohn Dyson 
3159d474eaaaSDoug Rabson 		vm_object_pip_add(robject, 1);
31601efb74fbSJohn Dyson 
316147221757SJohn Dyson 		for (idx = 0; idx < robject->size; idx++) {
31621efb74fbSJohn Dyson 
316395461b45SJohn Dyson 			m_out = vm_page_grab(robject, idx,
316495461b45SJohn Dyson 						VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
31651efb74fbSJohn Dyson 
31661efb74fbSJohn Dyson 			if (m_out->valid == 0) {
316795461b45SJohn Dyson 				m_in = vm_page_grab(object, bo_pindex + idx,
316895461b45SJohn Dyson 						VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
316995461b45SJohn Dyson 				if (m_in->valid == 0) {
317047221757SJohn Dyson 					rv = vm_pager_get_pages(object, &m_in, 1, 0);
317147221757SJohn Dyson 					if (rv != VM_PAGER_OK) {
31723efc015bSPeter Wemm 						printf("vm_freeze_copyopts: cannot read page from file: %lx\n", (long)m_in->pindex);
317347221757SJohn Dyson 						continue;
31741efb74fbSJohn Dyson 					}
317595461b45SJohn Dyson 					vm_page_deactivate(m_in);
317647221757SJohn Dyson 				}
317747221757SJohn Dyson 
317847221757SJohn Dyson 				vm_page_protect(m_in, VM_PROT_NONE);
317947221757SJohn Dyson 				pmap_copy_page(VM_PAGE_TO_PHYS(m_in), VM_PAGE_TO_PHYS(m_out));
318095461b45SJohn Dyson 				m_out->valid = m_in->valid;
31817dbf82dcSMatthew Dillon 				vm_page_dirty(m_out);
318295461b45SJohn Dyson 				vm_page_activate(m_out);
3183e69763a3SDoug Rabson 				vm_page_wakeup(m_in);
31841efb74fbSJohn Dyson 			}
3185e69763a3SDoug Rabson 			vm_page_wakeup(m_out);
318647221757SJohn Dyson 		}
3187925a3a41SJohn Dyson 
31881efb74fbSJohn Dyson 		object->shadow_count--;
318947221757SJohn Dyson 		object->ref_count--;
31901efb74fbSJohn Dyson 		TAILQ_REMOVE(&object->shadow_head, robject, shadow_list);
31911efb74fbSJohn Dyson 		robject->backing_object = NULL;
31921efb74fbSJohn Dyson 		robject->backing_object_offset = 0;
31931efb74fbSJohn Dyson 
319447221757SJohn Dyson 		vm_object_pip_wakeup(robject);
31951efb74fbSJohn Dyson 		vm_object_deallocate(robject);
31961efb74fbSJohn Dyson 	}
319747221757SJohn Dyson 
3198069e9bc1SDoug Rabson 	vm_object_clear_flag(object, OBJ_OPT);
31991efb74fbSJohn Dyson }
32001efb74fbSJohn Dyson 
3201c7c34a24SBruce Evans #include "opt_ddb.h"
3202c3cb3e12SDavid Greenman #ifdef DDB
3203c7c34a24SBruce Evans #include <sys/kernel.h>
3204c7c34a24SBruce Evans 
3205c7c34a24SBruce Evans #include <ddb/ddb.h>
3206c7c34a24SBruce Evans 
3207df8bae1dSRodney W. Grimes /*
3208df8bae1dSRodney W. Grimes  *	vm_map_print:	[ debug ]
3209df8bae1dSRodney W. Grimes  */
3210c7c34a24SBruce Evans DB_SHOW_COMMAND(map, vm_map_print)
3211df8bae1dSRodney W. Grimes {
321295e5e988SJohn Dyson 	static int nlines;
3213c7c34a24SBruce Evans 	/* XXX convert args. */
3214c0877f10SJohn Dyson 	vm_map_t map = (vm_map_t)addr;
3215c7c34a24SBruce Evans 	boolean_t full = have_addr;
3216df8bae1dSRodney W. Grimes 
3217c0877f10SJohn Dyson 	vm_map_entry_t entry;
3218c7c34a24SBruce Evans 
3219e5f251d2SAlan Cox 	db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
3220e5f251d2SAlan Cox 	    (void *)map,
3221101eeb7fSBruce Evans 	    (void *)map->pmap, map->nentries, map->timestamp);
322295e5e988SJohn Dyson 	nlines++;
3223df8bae1dSRodney W. Grimes 
3224c7c34a24SBruce Evans 	if (!full && db_indent)
3225df8bae1dSRodney W. Grimes 		return;
3226df8bae1dSRodney W. Grimes 
3227c7c34a24SBruce Evans 	db_indent += 2;
3228df8bae1dSRodney W. Grimes 	for (entry = map->header.next; entry != &map->header;
3229df8bae1dSRodney W. Grimes 	    entry = entry->next) {
3230fc62ef1fSBruce Evans 		db_iprintf("map entry %p: start=%p, end=%p\n",
3231fc62ef1fSBruce Evans 		    (void *)entry, (void *)entry->start, (void *)entry->end);
323295e5e988SJohn Dyson 		nlines++;
3233e5f251d2SAlan Cox 		{
3234df8bae1dSRodney W. Grimes 			static char *inheritance_name[4] =
3235df8bae1dSRodney W. Grimes 			{"share", "copy", "none", "donate_copy"};
32360d94caffSDavid Greenman 
323795e5e988SJohn Dyson 			db_iprintf(" prot=%x/%x/%s",
3238df8bae1dSRodney W. Grimes 			    entry->protection,
3239df8bae1dSRodney W. Grimes 			    entry->max_protection,
32408aef1712SMatthew Dillon 			    inheritance_name[(int)(unsigned char)entry->inheritance]);
3241df8bae1dSRodney W. Grimes 			if (entry->wired_count != 0)
324295e5e988SJohn Dyson 				db_printf(", wired");
3243df8bae1dSRodney W. Grimes 		}
32449fdfe602SMatthew Dillon 		if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3245101eeb7fSBruce Evans 			/* XXX no %qd in kernel.  Truncate entry->offset. */
3246101eeb7fSBruce Evans 			db_printf(", share=%p, offset=0x%lx\n",
32479fdfe602SMatthew Dillon 			    (void *)entry->object.sub_map,
3248ecbb00a2SDoug Rabson 			    (long)entry->offset);
324995e5e988SJohn Dyson 			nlines++;
3250df8bae1dSRodney W. Grimes 			if ((entry->prev == &map->header) ||
32519fdfe602SMatthew Dillon 			    (entry->prev->object.sub_map !=
32529fdfe602SMatthew Dillon 				entry->object.sub_map)) {
3253c7c34a24SBruce Evans 				db_indent += 2;
3254101eeb7fSBruce Evans 				vm_map_print((db_expr_t)(intptr_t)
32559fdfe602SMatthew Dillon 					     entry->object.sub_map,
3256914181e7SBruce Evans 					     full, 0, (char *)0);
3257c7c34a24SBruce Evans 				db_indent -= 2;
3258df8bae1dSRodney W. Grimes 			}
32590d94caffSDavid Greenman 		} else {
3260101eeb7fSBruce Evans 			/* XXX no %qd in kernel.  Truncate entry->offset. */
3261101eeb7fSBruce Evans 			db_printf(", object=%p, offset=0x%lx",
3262101eeb7fSBruce Evans 			    (void *)entry->object.vm_object,
3263ecbb00a2SDoug Rabson 			    (long)entry->offset);
3264afa07f7eSJohn Dyson 			if (entry->eflags & MAP_ENTRY_COW)
3265c7c34a24SBruce Evans 				db_printf(", copy (%s)",
3266afa07f7eSJohn Dyson 				    (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
3267c7c34a24SBruce Evans 			db_printf("\n");
326895e5e988SJohn Dyson 			nlines++;
3269df8bae1dSRodney W. Grimes 
3270df8bae1dSRodney W. Grimes 			if ((entry->prev == &map->header) ||
3271df8bae1dSRodney W. Grimes 			    (entry->prev->object.vm_object !=
3272df8bae1dSRodney W. Grimes 				entry->object.vm_object)) {
3273c7c34a24SBruce Evans 				db_indent += 2;
3274101eeb7fSBruce Evans 				vm_object_print((db_expr_t)(intptr_t)
3275101eeb7fSBruce Evans 						entry->object.vm_object,
3276914181e7SBruce Evans 						full, 0, (char *)0);
327795e5e988SJohn Dyson 				nlines += 4;
3278c7c34a24SBruce Evans 				db_indent -= 2;
3279df8bae1dSRodney W. Grimes 			}
3280df8bae1dSRodney W. Grimes 		}
3281df8bae1dSRodney W. Grimes 	}
3282c7c34a24SBruce Evans 	db_indent -= 2;
328395e5e988SJohn Dyson 	if (db_indent == 0)
328495e5e988SJohn Dyson 		nlines = 0;
3285df8bae1dSRodney W. Grimes }
328695e5e988SJohn Dyson 
328795e5e988SJohn Dyson 
328895e5e988SJohn Dyson DB_SHOW_COMMAND(procvm, procvm)
328995e5e988SJohn Dyson {
329095e5e988SJohn Dyson 	struct proc *p;
329195e5e988SJohn Dyson 
329295e5e988SJohn Dyson 	if (have_addr) {
329395e5e988SJohn Dyson 		p = (struct proc *) addr;
329495e5e988SJohn Dyson 	} else {
329595e5e988SJohn Dyson 		p = curproc;
329695e5e988SJohn Dyson 	}
329795e5e988SJohn Dyson 
3298ac1e407bSBruce Evans 	db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
3299ac1e407bSBruce Evans 	    (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
3300b1028ad1SLuoqi Chen 	    (void *)vmspace_pmap(p->p_vmspace));
330195e5e988SJohn Dyson 
3302101eeb7fSBruce Evans 	vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);
330395e5e988SJohn Dyson }
330495e5e988SJohn Dyson 
3305c7c34a24SBruce Evans #endif /* DDB */
3306