xref: /freebsd/sys/vm/vm_map.c (revision 1a484d28dd97477d653509b6fdcf3c836254787f)
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>
73b5e8ce9fSBruce Evans #include <sys/proc.h>
74efeaf95aSDavid Greenman #include <sys/vmmeter.h>
75867a482dSJohn Dyson #include <sys/mman.h>
761efb74fbSJohn Dyson #include <sys/vnode.h>
772267af78SJulian Elischer #include <sys/resourcevar.h>
78df8bae1dSRodney W. Grimes 
79df8bae1dSRodney W. Grimes #include <vm/vm.h>
80efeaf95aSDavid Greenman #include <vm/vm_param.h>
81996c772fSJohn Dyson #include <sys/lock.h>
82efeaf95aSDavid Greenman #include <vm/pmap.h>
83efeaf95aSDavid Greenman #include <vm/vm_map.h>
84df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
85df8bae1dSRodney W. Grimes #include <vm/vm_object.h>
8647221757SJohn Dyson #include <vm/vm_pager.h>
8726f9a767SRodney W. Grimes #include <vm/vm_kern.h>
88efeaf95aSDavid Greenman #include <vm/vm_extern.h>
893075778bSJohn Dyson #include <vm/vm_zone.h>
9021cd6e62SSeigo Tanimura #include <vm/swap_pager.h>
91df8bae1dSRodney W. Grimes 
92df8bae1dSRodney W. Grimes /*
93df8bae1dSRodney W. Grimes  *	Virtual memory maps provide for the mapping, protection,
94df8bae1dSRodney W. Grimes  *	and sharing of virtual memory objects.  In addition,
95df8bae1dSRodney W. Grimes  *	this module provides for an efficient virtual copy of
96df8bae1dSRodney W. Grimes  *	memory from one map to another.
97df8bae1dSRodney W. Grimes  *
98df8bae1dSRodney W. Grimes  *	Synchronization is required prior to most operations.
99df8bae1dSRodney W. Grimes  *
100df8bae1dSRodney W. Grimes  *	Maps consist of an ordered doubly-linked list of simple
101df8bae1dSRodney W. Grimes  *	entries; a single hint is used to speed up lookups.
102df8bae1dSRodney W. Grimes  *
103956f3135SPhilippe Charnier  *	Since portions of maps are specified by start/end addresses,
104df8bae1dSRodney W. Grimes  *	which may not align with existing map entries, all
105df8bae1dSRodney W. Grimes  *	routines merely "clip" entries to these start/end values.
106df8bae1dSRodney W. Grimes  *	[That is, an entry is split into two, bordering at a
107df8bae1dSRodney W. Grimes  *	start or end value.]  Note that these clippings may not
108df8bae1dSRodney W. Grimes  *	always be necessary (as the two resulting entries are then
109df8bae1dSRodney W. Grimes  *	not changed); however, the clipping is done for convenience.
110df8bae1dSRodney W. Grimes  *
111df8bae1dSRodney W. Grimes  *	As mentioned above, virtual copy operations are performed
112ad5fca3bSAlan Cox  *	by copying VM object references from one map to
113df8bae1dSRodney W. Grimes  *	another, and then marking both regions as copy-on-write.
114df8bae1dSRodney W. Grimes  */
115df8bae1dSRodney W. Grimes 
116df8bae1dSRodney W. Grimes /*
117df8bae1dSRodney W. Grimes  *	vm_map_startup:
118df8bae1dSRodney W. Grimes  *
119df8bae1dSRodney W. Grimes  *	Initialize the vm_map module.  Must be called before
120df8bae1dSRodney W. Grimes  *	any other vm_map routines.
121df8bae1dSRodney W. Grimes  *
122df8bae1dSRodney W. Grimes  *	Map and entry structures are allocated from the general
123df8bae1dSRodney W. Grimes  *	purpose memory pool with some exceptions:
124df8bae1dSRodney W. Grimes  *
125df8bae1dSRodney W. Grimes  *	- The kernel map and kmem submap are allocated statically.
126df8bae1dSRodney W. Grimes  *	- Kernel map entries are allocated out of a static pool.
127df8bae1dSRodney W. Grimes  *
128df8bae1dSRodney W. Grimes  *	These restrictions are necessary since malloc() uses the
129df8bae1dSRodney W. Grimes  *	maps and requires map entries.
130df8bae1dSRodney W. Grimes  */
131df8bae1dSRodney W. Grimes 
1323075778bSJohn Dyson static struct vm_zone kmapentzone_store, mapentzone_store, mapzone_store;
1332d8acc0fSJohn Dyson static vm_zone_t mapentzone, kmapentzone, mapzone, vmspace_zone;
1343075778bSJohn Dyson static struct vm_object kmapentobj, mapentobj, mapobj;
1351fc43fd1SAlan Cox 
136303b270bSEivind Eklund static struct vm_map_entry map_entry_init[MAX_MAPENT];
137303b270bSEivind Eklund static struct vm_map_entry kmap_entry_init[MAX_KMAPENT];
138303b270bSEivind Eklund static struct vm_map map_init[MAX_KMAP];
139b18bfc3dSJohn Dyson 
140df8bae1dSRodney W. Grimes static void _vm_map_clip_end __P((vm_map_t, vm_map_entry_t, vm_offset_t));
141df8bae1dSRodney W. Grimes static void _vm_map_clip_start __P((vm_map_t, vm_map_entry_t, vm_offset_t));
142f708ef1bSPoul-Henning Kamp static vm_map_entry_t vm_map_entry_create __P((vm_map_t));
143f708ef1bSPoul-Henning Kamp static void vm_map_entry_delete __P((vm_map_t, vm_map_entry_t));
1440362d7d7SJohn Dyson static void vm_map_entry_dispose __P((vm_map_t, vm_map_entry_t));
145f708ef1bSPoul-Henning Kamp static void vm_map_entry_unwire __P((vm_map_t, vm_map_entry_t));
146f708ef1bSPoul-Henning Kamp static void vm_map_copy_entry __P((vm_map_t, vm_map_t, vm_map_entry_t,
147f708ef1bSPoul-Henning Kamp 		vm_map_entry_t));
148dda6b171SJohn Dyson static void vm_map_split __P((vm_map_entry_t));
149df8bae1dSRodney W. Grimes 
1500d94caffSDavid Greenman void
1510d94caffSDavid Greenman vm_map_startup()
152df8bae1dSRodney W. Grimes {
1533075778bSJohn Dyson 	mapzone = &mapzone_store;
1540d65e566SJohn Dyson 	zbootinit(mapzone, "MAP", sizeof (struct vm_map),
1553075778bSJohn Dyson 		map_init, MAX_KMAP);
1563075778bSJohn Dyson 	kmapentzone = &kmapentzone_store;
1570d65e566SJohn Dyson 	zbootinit(kmapentzone, "KMAP ENTRY", sizeof (struct vm_map_entry),
1583075778bSJohn Dyson 		kmap_entry_init, MAX_KMAPENT);
1593075778bSJohn Dyson 	mapentzone = &mapentzone_store;
1600d65e566SJohn Dyson 	zbootinit(mapentzone, "MAP ENTRY", sizeof (struct vm_map_entry),
1613075778bSJohn Dyson 		map_entry_init, MAX_MAPENT);
162df8bae1dSRodney W. Grimes }
163df8bae1dSRodney W. Grimes 
164df8bae1dSRodney W. Grimes /*
165df8bae1dSRodney W. Grimes  * Allocate a vmspace structure, including a vm_map and pmap,
166df8bae1dSRodney W. Grimes  * and initialize those structures.  The refcnt is set to 1.
167df8bae1dSRodney W. Grimes  * The remaining fields must be initialized by the caller.
168df8bae1dSRodney W. Grimes  */
169df8bae1dSRodney W. Grimes struct vmspace *
1702d8acc0fSJohn Dyson vmspace_alloc(min, max)
171df8bae1dSRodney W. Grimes 	vm_offset_t min, max;
172df8bae1dSRodney W. Grimes {
173c0877f10SJohn Dyson 	struct vmspace *vm;
1740d94caffSDavid Greenman 
1752d8acc0fSJohn Dyson 	vm = zalloc(vmspace_zone);
1762d8acc0fSJohn Dyson 	vm_map_init(&vm->vm_map, min, max);
177b1028ad1SLuoqi Chen 	pmap_pinit(vmspace_pmap(vm));
178b1028ad1SLuoqi Chen 	vm->vm_map.pmap = vmspace_pmap(vm);		/* XXX */
179df8bae1dSRodney W. Grimes 	vm->vm_refcnt = 1;
1802d8acc0fSJohn Dyson 	vm->vm_shm = NULL;
181df8bae1dSRodney W. Grimes 	return (vm);
182df8bae1dSRodney W. Grimes }
183df8bae1dSRodney W. Grimes 
184df8bae1dSRodney W. Grimes void
1853075778bSJohn Dyson vm_init2(void) {
1860d65e566SJohn Dyson 	zinitna(kmapentzone, &kmapentobj,
1870a80f406SJohn Dyson 		NULL, 0, cnt.v_page_count / 4, ZONE_INTERRUPT, 1);
1880d65e566SJohn Dyson 	zinitna(mapentzone, &mapentobj,
1890a80f406SJohn Dyson 		NULL, 0, 0, 0, 1);
1900d65e566SJohn Dyson 	zinitna(mapzone, &mapobj,
1910a80f406SJohn Dyson 		NULL, 0, 0, 0, 1);
1922d8acc0fSJohn Dyson 	vmspace_zone = zinit("VMSPACE", sizeof (struct vmspace), 0, 0, 3);
193ba9be04cSJohn Dyson 	pmap_init2();
19499448ed1SJohn Dyson 	vm_object_init2();
1953075778bSJohn Dyson }
1963075778bSJohn Dyson 
1973075778bSJohn Dyson void
198df8bae1dSRodney W. Grimes vmspace_free(vm)
199c0877f10SJohn Dyson 	struct vmspace *vm;
200df8bae1dSRodney W. Grimes {
201df8bae1dSRodney W. Grimes 
202a1f6d91cSDavid Greenman 	if (vm->vm_refcnt == 0)
203a1f6d91cSDavid Greenman 		panic("vmspace_free: attempt to free already freed vmspace");
204a1f6d91cSDavid Greenman 
205df8bae1dSRodney W. Grimes 	if (--vm->vm_refcnt == 0) {
206bd7e5f99SJohn Dyson 
20730dcfc09SJohn Dyson 		/*
208df8bae1dSRodney W. Grimes 		 * Lock the map, to wait out all other references to it.
2090d94caffSDavid Greenman 		 * Delete all of the mappings and pages they hold, then call
2100d94caffSDavid Greenman 		 * the pmap module to reclaim anything left.
211df8bae1dSRodney W. Grimes 		 */
212df8bae1dSRodney W. Grimes 		vm_map_lock(&vm->vm_map);
213df8bae1dSRodney W. Grimes 		(void) vm_map_delete(&vm->vm_map, vm->vm_map.min_offset,
214df8bae1dSRodney W. Grimes 		    vm->vm_map.max_offset);
215a1f6d91cSDavid Greenman 		vm_map_unlock(&vm->vm_map);
216b18bfc3dSJohn Dyson 
217b1028ad1SLuoqi Chen 		pmap_release(vmspace_pmap(vm));
218a18b1f1dSJason Evans 		vm_map_destroy(&vm->vm_map);
2192d8acc0fSJohn Dyson 		zfree(vmspace_zone, vm);
220df8bae1dSRodney W. Grimes 	}
221df8bae1dSRodney W. Grimes }
222df8bae1dSRodney W. Grimes 
223df8bae1dSRodney W. Grimes /*
224df8bae1dSRodney W. Grimes  *	vm_map_create:
225df8bae1dSRodney W. Grimes  *
226df8bae1dSRodney W. Grimes  *	Creates and returns a new empty VM map with
227df8bae1dSRodney W. Grimes  *	the given physical map structure, and having
228df8bae1dSRodney W. Grimes  *	the given lower and upper address bounds.
229df8bae1dSRodney W. Grimes  */
2300d94caffSDavid Greenman vm_map_t
2312d8acc0fSJohn Dyson vm_map_create(pmap, min, max)
232df8bae1dSRodney W. Grimes 	pmap_t pmap;
233df8bae1dSRodney W. Grimes 	vm_offset_t min, max;
234df8bae1dSRodney W. Grimes {
235c0877f10SJohn Dyson 	vm_map_t result;
236df8bae1dSRodney W. Grimes 
2373075778bSJohn Dyson 	result = zalloc(mapzone);
2382d8acc0fSJohn Dyson 	vm_map_init(result, min, max);
239df8bae1dSRodney W. Grimes 	result->pmap = pmap;
240df8bae1dSRodney W. Grimes 	return (result);
241df8bae1dSRodney W. Grimes }
242df8bae1dSRodney W. Grimes 
243df8bae1dSRodney W. Grimes /*
244df8bae1dSRodney W. Grimes  * Initialize an existing vm_map structure
245df8bae1dSRodney W. Grimes  * such as that in the vmspace structure.
246df8bae1dSRodney W. Grimes  * The pmap is set elsewhere.
247df8bae1dSRodney W. Grimes  */
248df8bae1dSRodney W. Grimes void
2492d8acc0fSJohn Dyson vm_map_init(map, min, max)
250c0877f10SJohn Dyson 	struct vm_map *map;
251df8bae1dSRodney W. Grimes 	vm_offset_t min, max;
252df8bae1dSRodney W. Grimes {
253df8bae1dSRodney W. Grimes 	map->header.next = map->header.prev = &map->header;
254df8bae1dSRodney W. Grimes 	map->nentries = 0;
255df8bae1dSRodney W. Grimes 	map->size = 0;
2563075778bSJohn Dyson 	map->system_map = 0;
257df8bae1dSRodney W. Grimes 	map->min_offset = min;
258df8bae1dSRodney W. Grimes 	map->max_offset = max;
259df8bae1dSRodney W. Grimes 	map->first_free = &map->header;
260df8bae1dSRodney W. Grimes 	map->hint = &map->header;
261df8bae1dSRodney W. Grimes 	map->timestamp = 0;
2628f9110f6SJohn Dyson 	lockinit(&map->lock, PVM, "thrd_sleep", 0, LK_NOPAUSE);
263df8bae1dSRodney W. Grimes }
264df8bae1dSRodney W. Grimes 
265a18b1f1dSJason Evans void
266a18b1f1dSJason Evans vm_map_destroy(map)
267a18b1f1dSJason Evans 	struct vm_map *map;
268a18b1f1dSJason Evans {
269a18b1f1dSJason Evans 	lockdestroy(&map->lock);
270a18b1f1dSJason Evans }
271a18b1f1dSJason Evans 
272df8bae1dSRodney W. Grimes /*
273b18bfc3dSJohn Dyson  *	vm_map_entry_dispose:	[ internal use only ]
274b18bfc3dSJohn Dyson  *
275b18bfc3dSJohn Dyson  *	Inverse of vm_map_entry_create.
276b18bfc3dSJohn Dyson  */
27762487bb4SJohn Dyson static void
278b18bfc3dSJohn Dyson vm_map_entry_dispose(map, entry)
279b18bfc3dSJohn Dyson 	vm_map_t map;
280b18bfc3dSJohn Dyson 	vm_map_entry_t entry;
281b18bfc3dSJohn Dyson {
282b79933ebSJohn Dyson 	zfree((map->system_map || !mapentzone) ? kmapentzone : mapentzone, entry);
283b18bfc3dSJohn Dyson }
284b18bfc3dSJohn Dyson 
285b18bfc3dSJohn Dyson /*
286df8bae1dSRodney W. Grimes  *	vm_map_entry_create:	[ internal use only ]
287df8bae1dSRodney W. Grimes  *
288df8bae1dSRodney W. Grimes  *	Allocates a VM map entry for insertion.
289df8bae1dSRodney W. Grimes  *	No entry fields are filled in.  This routine is
290df8bae1dSRodney W. Grimes  */
291f708ef1bSPoul-Henning Kamp static vm_map_entry_t
29226f9a767SRodney W. Grimes vm_map_entry_create(map)
293df8bae1dSRodney W. Grimes 	vm_map_t map;
294df8bae1dSRodney W. Grimes {
2951f6889a1SMatthew Dillon 	vm_map_entry_t new_entry;
2961f6889a1SMatthew Dillon 
2971f6889a1SMatthew Dillon 	new_entry = zalloc((map->system_map || !mapentzone) ?
2981f6889a1SMatthew Dillon 		kmapentzone : mapentzone);
2991f6889a1SMatthew Dillon 	if (new_entry == NULL)
3001f6889a1SMatthew Dillon 	    panic("vm_map_entry_create: kernel resources exhausted");
3011f6889a1SMatthew Dillon 	return(new_entry);
302df8bae1dSRodney W. Grimes }
303df8bae1dSRodney W. Grimes 
304df8bae1dSRodney W. Grimes /*
305df8bae1dSRodney W. Grimes  *	vm_map_entry_{un,}link:
306df8bae1dSRodney W. Grimes  *
307df8bae1dSRodney W. Grimes  *	Insert/remove entries from maps.
308df8bae1dSRodney W. Grimes  */
30999c81ca9SAlan Cox static __inline void
31099c81ca9SAlan Cox vm_map_entry_link(vm_map_t map,
31199c81ca9SAlan Cox 		  vm_map_entry_t after_where,
31299c81ca9SAlan Cox 		  vm_map_entry_t entry)
31399c81ca9SAlan Cox {
31499c81ca9SAlan Cox 	map->nentries++;
31599c81ca9SAlan Cox 	entry->prev = after_where;
31699c81ca9SAlan Cox 	entry->next = after_where->next;
31799c81ca9SAlan Cox 	entry->next->prev = entry;
31899c81ca9SAlan Cox 	after_where->next = entry;
319df8bae1dSRodney W. Grimes }
32099c81ca9SAlan Cox 
32199c81ca9SAlan Cox static __inline void
32299c81ca9SAlan Cox vm_map_entry_unlink(vm_map_t map,
32399c81ca9SAlan Cox 		    vm_map_entry_t entry)
32499c81ca9SAlan Cox {
32599c81ca9SAlan Cox 	vm_map_entry_t prev = entry->prev;
32699c81ca9SAlan Cox 	vm_map_entry_t next = entry->next;
32799c81ca9SAlan Cox 
32899c81ca9SAlan Cox 	next->prev = prev;
32999c81ca9SAlan Cox 	prev->next = next;
33099c81ca9SAlan Cox 	map->nentries--;
331df8bae1dSRodney W. Grimes }
332df8bae1dSRodney W. Grimes 
333df8bae1dSRodney W. Grimes /*
334df8bae1dSRodney W. Grimes  *	SAVE_HINT:
335df8bae1dSRodney W. Grimes  *
336df8bae1dSRodney W. Grimes  *	Saves the specified entry as the hint for
33724a1cce3SDavid Greenman  *	future lookups.
338df8bae1dSRodney W. Grimes  */
339df8bae1dSRodney W. Grimes #define	SAVE_HINT(map,value) \
34024a1cce3SDavid Greenman 		(map)->hint = (value);
341df8bae1dSRodney W. Grimes 
342df8bae1dSRodney W. Grimes /*
343df8bae1dSRodney W. Grimes  *	vm_map_lookup_entry:	[ internal use only ]
344df8bae1dSRodney W. Grimes  *
345df8bae1dSRodney W. Grimes  *	Finds the map entry containing (or
346df8bae1dSRodney W. Grimes  *	immediately preceding) the specified address
347df8bae1dSRodney W. Grimes  *	in the given map; the entry is returned
348df8bae1dSRodney W. Grimes  *	in the "entry" parameter.  The boolean
349df8bae1dSRodney W. Grimes  *	result indicates whether the address is
350df8bae1dSRodney W. Grimes  *	actually contained in the map.
351df8bae1dSRodney W. Grimes  */
3520d94caffSDavid Greenman boolean_t
3530d94caffSDavid Greenman vm_map_lookup_entry(map, address, entry)
354c0877f10SJohn Dyson 	vm_map_t map;
355c0877f10SJohn Dyson 	vm_offset_t address;
356df8bae1dSRodney W. Grimes 	vm_map_entry_t *entry;	/* OUT */
357df8bae1dSRodney W. Grimes {
358c0877f10SJohn Dyson 	vm_map_entry_t cur;
359c0877f10SJohn Dyson 	vm_map_entry_t last;
360df8bae1dSRodney W. Grimes 
361df8bae1dSRodney W. Grimes 	/*
3620d94caffSDavid Greenman 	 * Start looking either from the head of the list, or from the hint.
363df8bae1dSRodney W. Grimes 	 */
364df8bae1dSRodney W. Grimes 
365df8bae1dSRodney W. Grimes 	cur = map->hint;
366df8bae1dSRodney W. Grimes 
367df8bae1dSRodney W. Grimes 	if (cur == &map->header)
368df8bae1dSRodney W. Grimes 		cur = cur->next;
369df8bae1dSRodney W. Grimes 
370df8bae1dSRodney W. Grimes 	if (address >= cur->start) {
371df8bae1dSRodney W. Grimes 		/*
372df8bae1dSRodney W. Grimes 		 * Go from hint to end of list.
373df8bae1dSRodney W. Grimes 		 *
3740d94caffSDavid Greenman 		 * But first, make a quick check to see if we are already looking
3750d94caffSDavid Greenman 		 * at the entry we want (which is usually the case). Note also
3760d94caffSDavid Greenman 		 * that we don't need to save the hint here... it is the same
3770d94caffSDavid Greenman 		 * hint (unless we are at the header, in which case the hint
3780d94caffSDavid Greenman 		 * didn't buy us anything anyway).
379df8bae1dSRodney W. Grimes 		 */
380df8bae1dSRodney W. Grimes 		last = &map->header;
381df8bae1dSRodney W. Grimes 		if ((cur != last) && (cur->end > address)) {
382df8bae1dSRodney W. Grimes 			*entry = cur;
383df8bae1dSRodney W. Grimes 			return (TRUE);
384df8bae1dSRodney W. Grimes 		}
3850d94caffSDavid Greenman 	} else {
386df8bae1dSRodney W. Grimes 		/*
387df8bae1dSRodney W. Grimes 		 * Go from start to hint, *inclusively*
388df8bae1dSRodney W. Grimes 		 */
389df8bae1dSRodney W. Grimes 		last = cur->next;
390df8bae1dSRodney W. Grimes 		cur = map->header.next;
391df8bae1dSRodney W. Grimes 	}
392df8bae1dSRodney W. Grimes 
393df8bae1dSRodney W. Grimes 	/*
394df8bae1dSRodney W. Grimes 	 * Search linearly
395df8bae1dSRodney W. Grimes 	 */
396df8bae1dSRodney W. Grimes 
397df8bae1dSRodney W. Grimes 	while (cur != last) {
398df8bae1dSRodney W. Grimes 		if (cur->end > address) {
399df8bae1dSRodney W. Grimes 			if (address >= cur->start) {
400df8bae1dSRodney W. Grimes 				/*
4010d94caffSDavid Greenman 				 * Save this lookup for future hints, and
4020d94caffSDavid Greenman 				 * return
403df8bae1dSRodney W. Grimes 				 */
404df8bae1dSRodney W. Grimes 
405df8bae1dSRodney W. Grimes 				*entry = cur;
406df8bae1dSRodney W. Grimes 				SAVE_HINT(map, cur);
407df8bae1dSRodney W. Grimes 				return (TRUE);
408df8bae1dSRodney W. Grimes 			}
409df8bae1dSRodney W. Grimes 			break;
410df8bae1dSRodney W. Grimes 		}
411df8bae1dSRodney W. Grimes 		cur = cur->next;
412df8bae1dSRodney W. Grimes 	}
413df8bae1dSRodney W. Grimes 	*entry = cur->prev;
414df8bae1dSRodney W. Grimes 	SAVE_HINT(map, *entry);
415df8bae1dSRodney W. Grimes 	return (FALSE);
416df8bae1dSRodney W. Grimes }
417df8bae1dSRodney W. Grimes 
418df8bae1dSRodney W. Grimes /*
41930dcfc09SJohn Dyson  *	vm_map_insert:
42030dcfc09SJohn Dyson  *
42130dcfc09SJohn Dyson  *	Inserts the given whole VM object into the target
42230dcfc09SJohn Dyson  *	map at the specified address range.  The object's
42330dcfc09SJohn Dyson  *	size should match that of the address range.
42430dcfc09SJohn Dyson  *
42530dcfc09SJohn Dyson  *	Requires that the map be locked, and leaves it so.
4262aaeadf8SMatthew Dillon  *
4272aaeadf8SMatthew Dillon  *	If object is non-NULL, ref count must be bumped by caller
4282aaeadf8SMatthew Dillon  *	prior to making call to account for the new entry.
42930dcfc09SJohn Dyson  */
43030dcfc09SJohn Dyson int
431b9dcd593SBruce Evans vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
432b9dcd593SBruce Evans 	      vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max,
433b9dcd593SBruce Evans 	      int cow)
43430dcfc09SJohn Dyson {
435c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
436c0877f10SJohn Dyson 	vm_map_entry_t prev_entry;
43730dcfc09SJohn Dyson 	vm_map_entry_t temp_entry;
4389730a5daSPaul Saab 	vm_eflags_t protoeflags;
43930dcfc09SJohn Dyson 
44030dcfc09SJohn Dyson 	/*
44130dcfc09SJohn Dyson 	 * Check that the start and end points are not bogus.
44230dcfc09SJohn Dyson 	 */
44330dcfc09SJohn Dyson 
44430dcfc09SJohn Dyson 	if ((start < map->min_offset) || (end > map->max_offset) ||
44530dcfc09SJohn Dyson 	    (start >= end))
44630dcfc09SJohn Dyson 		return (KERN_INVALID_ADDRESS);
44730dcfc09SJohn Dyson 
44830dcfc09SJohn Dyson 	/*
44930dcfc09SJohn Dyson 	 * Find the entry prior to the proposed starting address; if it's part
45030dcfc09SJohn Dyson 	 * of an existing entry, this range is bogus.
45130dcfc09SJohn Dyson 	 */
45230dcfc09SJohn Dyson 
45330dcfc09SJohn Dyson 	if (vm_map_lookup_entry(map, start, &temp_entry))
45430dcfc09SJohn Dyson 		return (KERN_NO_SPACE);
45530dcfc09SJohn Dyson 
45630dcfc09SJohn Dyson 	prev_entry = temp_entry;
45730dcfc09SJohn Dyson 
45830dcfc09SJohn Dyson 	/*
45930dcfc09SJohn Dyson 	 * Assert that the next entry doesn't overlap the end point.
46030dcfc09SJohn Dyson 	 */
46130dcfc09SJohn Dyson 
46230dcfc09SJohn Dyson 	if ((prev_entry->next != &map->header) &&
46330dcfc09SJohn Dyson 	    (prev_entry->next->start < end))
46430dcfc09SJohn Dyson 		return (KERN_NO_SPACE);
46530dcfc09SJohn Dyson 
466afa07f7eSJohn Dyson 	protoeflags = 0;
467afa07f7eSJohn Dyson 
468afa07f7eSJohn Dyson 	if (cow & MAP_COPY_ON_WRITE)
469e5f13bddSAlan Cox 		protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
470afa07f7eSJohn Dyson 
4714e045f93SAlan Cox 	if (cow & MAP_NOFAULT) {
472afa07f7eSJohn Dyson 		protoeflags |= MAP_ENTRY_NOFAULT;
473afa07f7eSJohn Dyson 
4744e045f93SAlan Cox 		KASSERT(object == NULL,
4754e045f93SAlan Cox 			("vm_map_insert: paradoxical MAP_NOFAULT request"));
4764e045f93SAlan Cox 	}
4774f79d873SMatthew Dillon 	if (cow & MAP_DISABLE_SYNCER)
4784f79d873SMatthew Dillon 		protoeflags |= MAP_ENTRY_NOSYNC;
4799730a5daSPaul Saab 	if (cow & MAP_DISABLE_COREDUMP)
4809730a5daSPaul Saab 		protoeflags |= MAP_ENTRY_NOCOREDUMP;
4814f79d873SMatthew Dillon 
4822aaeadf8SMatthew Dillon 	if (object) {
48330dcfc09SJohn Dyson 		/*
4842aaeadf8SMatthew Dillon 		 * When object is non-NULL, it could be shared with another
4852aaeadf8SMatthew Dillon 		 * process.  We have to set or clear OBJ_ONEMAPPING
4862aaeadf8SMatthew Dillon 		 * appropriately.
48730dcfc09SJohn Dyson 		 */
4882aaeadf8SMatthew Dillon 		if ((object->ref_count > 1) || (object->shadow_count != 0)) {
4892aaeadf8SMatthew Dillon 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
4902aaeadf8SMatthew Dillon 		}
4914e045f93SAlan Cox 	}
4924e045f93SAlan Cox 	else if ((prev_entry != &map->header) &&
4934e045f93SAlan Cox 		 (prev_entry->eflags == protoeflags) &&
4948cc7e047SJohn Dyson 		 (prev_entry->end == start) &&
4954e045f93SAlan Cox 		 (prev_entry->wired_count == 0) &&
4964e045f93SAlan Cox 		 ((prev_entry->object.vm_object == NULL) ||
4978cc7e047SJohn Dyson 		  vm_object_coalesce(prev_entry->object.vm_object,
49830dcfc09SJohn Dyson 				     OFF_TO_IDX(prev_entry->offset),
4998cc7e047SJohn Dyson 				     (vm_size_t)(prev_entry->end - prev_entry->start),
500cdc2c291SJohn Dyson 				     (vm_size_t)(end - prev_entry->end)))) {
50130dcfc09SJohn Dyson 		/*
5022aaeadf8SMatthew Dillon 		 * We were able to extend the object.  Determine if we
5032aaeadf8SMatthew Dillon 		 * can extend the previous map entry to include the
5042aaeadf8SMatthew Dillon 		 * new range as well.
50530dcfc09SJohn Dyson 		 */
5068cc7e047SJohn Dyson 		if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
5078cc7e047SJohn Dyson 		    (prev_entry->protection == prot) &&
5088cc7e047SJohn Dyson 		    (prev_entry->max_protection == max)) {
50930dcfc09SJohn Dyson 			map->size += (end - prev_entry->end);
51030dcfc09SJohn Dyson 			prev_entry->end = end;
5114e71e795SMatthew Dillon 			vm_map_simplify_entry(map, prev_entry);
51230dcfc09SJohn Dyson 			return (KERN_SUCCESS);
51330dcfc09SJohn Dyson 		}
5148cc7e047SJohn Dyson 
5152aaeadf8SMatthew Dillon 		/*
5162aaeadf8SMatthew Dillon 		 * If we can extend the object but cannot extend the
5172aaeadf8SMatthew Dillon 		 * map entry, we have to create a new map entry.  We
5182aaeadf8SMatthew Dillon 		 * must bump the ref count on the extended object to
5194e71e795SMatthew Dillon 		 * account for it.  object may be NULL.
5202aaeadf8SMatthew Dillon 		 */
5212aaeadf8SMatthew Dillon 		object = prev_entry->object.vm_object;
5222aaeadf8SMatthew Dillon 		offset = prev_entry->offset +
5232aaeadf8SMatthew Dillon 			(prev_entry->end - prev_entry->start);
5248cc7e047SJohn Dyson 		vm_object_reference(object);
525b18bfc3dSJohn Dyson 	}
5262aaeadf8SMatthew Dillon 
5272aaeadf8SMatthew Dillon 	/*
5282aaeadf8SMatthew Dillon 	 * NOTE: if conditionals fail, object can be NULL here.  This occurs
5292aaeadf8SMatthew Dillon 	 * in things like the buffer map where we manage kva but do not manage
5302aaeadf8SMatthew Dillon 	 * backing objects.
5312aaeadf8SMatthew Dillon 	 */
5328cc7e047SJohn Dyson 
53330dcfc09SJohn Dyson 	/*
53430dcfc09SJohn Dyson 	 * Create a new entry
53530dcfc09SJohn Dyson 	 */
53630dcfc09SJohn Dyson 
53730dcfc09SJohn Dyson 	new_entry = vm_map_entry_create(map);
53830dcfc09SJohn Dyson 	new_entry->start = start;
53930dcfc09SJohn Dyson 	new_entry->end = end;
54030dcfc09SJohn Dyson 
541afa07f7eSJohn Dyson 	new_entry->eflags = protoeflags;
54230dcfc09SJohn Dyson 	new_entry->object.vm_object = object;
54330dcfc09SJohn Dyson 	new_entry->offset = offset;
5442267af78SJulian Elischer 	new_entry->avail_ssize = 0;
5452267af78SJulian Elischer 
54630dcfc09SJohn Dyson 	new_entry->inheritance = VM_INHERIT_DEFAULT;
54730dcfc09SJohn Dyson 	new_entry->protection = prot;
54830dcfc09SJohn Dyson 	new_entry->max_protection = max;
54930dcfc09SJohn Dyson 	new_entry->wired_count = 0;
550e5f251d2SAlan Cox 
55130dcfc09SJohn Dyson 	/*
55230dcfc09SJohn Dyson 	 * Insert the new entry into the list
55330dcfc09SJohn Dyson 	 */
55430dcfc09SJohn Dyson 
55530dcfc09SJohn Dyson 	vm_map_entry_link(map, prev_entry, new_entry);
55630dcfc09SJohn Dyson 	map->size += new_entry->end - new_entry->start;
55730dcfc09SJohn Dyson 
55830dcfc09SJohn Dyson 	/*
55930dcfc09SJohn Dyson 	 * Update the free space hint
56030dcfc09SJohn Dyson 	 */
56167bf6868SJohn Dyson 	if ((map->first_free == prev_entry) &&
5624f79d873SMatthew Dillon 	    (prev_entry->end >= new_entry->start)) {
56330dcfc09SJohn Dyson 		map->first_free = new_entry;
5644f79d873SMatthew Dillon 	}
56530dcfc09SJohn Dyson 
5661a484d28SMatthew Dillon #if 0
5671a484d28SMatthew Dillon 	/*
5681a484d28SMatthew Dillon 	 * Temporarily removed to avoid MAP_STACK panic, due to
5691a484d28SMatthew Dillon 	 * MAP_STACK being a huge hack.  Will be added back in
5701a484d28SMatthew Dillon 	 * when MAP_STACK (and the user stack mapping) is fixed.
5711a484d28SMatthew Dillon 	 */
5724e71e795SMatthew Dillon 	/*
5734e71e795SMatthew Dillon 	 * It may be possible to simplify the entry
5744e71e795SMatthew Dillon 	 */
5754e71e795SMatthew Dillon 	vm_map_simplify_entry(map, new_entry);
5761a484d28SMatthew Dillon #endif
5774e71e795SMatthew Dillon 
5784f79d873SMatthew Dillon 	if (cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) {
579e972780aSAlan Cox 		pmap_object_init_pt(map->pmap, start,
580e972780aSAlan Cox 				    object, OFF_TO_IDX(offset), end - start,
581e972780aSAlan Cox 				    cow & MAP_PREFAULT_PARTIAL);
5824f79d873SMatthew Dillon 	}
583e972780aSAlan Cox 
58430dcfc09SJohn Dyson 	return (KERN_SUCCESS);
58530dcfc09SJohn Dyson }
58630dcfc09SJohn Dyson 
58730dcfc09SJohn Dyson /*
588df8bae1dSRodney W. Grimes  * Find sufficient space for `length' bytes in the given map, starting at
589df8bae1dSRodney W. Grimes  * `start'.  The map must be locked.  Returns 0 on success, 1 on no space.
590df8bae1dSRodney W. Grimes  */
591df8bae1dSRodney W. Grimes int
592df8bae1dSRodney W. Grimes vm_map_findspace(map, start, length, addr)
593c0877f10SJohn Dyson 	vm_map_t map;
594c0877f10SJohn Dyson 	vm_offset_t start;
595df8bae1dSRodney W. Grimes 	vm_size_t length;
596df8bae1dSRodney W. Grimes 	vm_offset_t *addr;
597df8bae1dSRodney W. Grimes {
598c0877f10SJohn Dyson 	vm_map_entry_t entry, next;
599c0877f10SJohn Dyson 	vm_offset_t end;
600df8bae1dSRodney W. Grimes 
601df8bae1dSRodney W. Grimes 	if (start < map->min_offset)
602df8bae1dSRodney W. Grimes 		start = map->min_offset;
603df8bae1dSRodney W. Grimes 	if (start > map->max_offset)
604df8bae1dSRodney W. Grimes 		return (1);
605df8bae1dSRodney W. Grimes 
606df8bae1dSRodney W. Grimes 	/*
6070d94caffSDavid Greenman 	 * Look for the first possible address; if there's already something
6080d94caffSDavid Greenman 	 * at this address, we have to start after it.
609df8bae1dSRodney W. Grimes 	 */
610df8bae1dSRodney W. Grimes 	if (start == map->min_offset) {
61167bf6868SJohn Dyson 		if ((entry = map->first_free) != &map->header)
612df8bae1dSRodney W. Grimes 			start = entry->end;
613df8bae1dSRodney W. Grimes 	} else {
614df8bae1dSRodney W. Grimes 		vm_map_entry_t tmp;
6150d94caffSDavid Greenman 
616df8bae1dSRodney W. Grimes 		if (vm_map_lookup_entry(map, start, &tmp))
617df8bae1dSRodney W. Grimes 			start = tmp->end;
618df8bae1dSRodney W. Grimes 		entry = tmp;
619df8bae1dSRodney W. Grimes 	}
620df8bae1dSRodney W. Grimes 
621df8bae1dSRodney W. Grimes 	/*
6220d94caffSDavid Greenman 	 * Look through the rest of the map, trying to fit a new region in the
6230d94caffSDavid Greenman 	 * gap between existing regions, or after the very last region.
624df8bae1dSRodney W. Grimes 	 */
625df8bae1dSRodney W. Grimes 	for (;; start = (entry = next)->end) {
626df8bae1dSRodney W. Grimes 		/*
627df8bae1dSRodney W. Grimes 		 * Find the end of the proposed new region.  Be sure we didn't
628df8bae1dSRodney W. Grimes 		 * go beyond the end of the map, or wrap around the address;
629df8bae1dSRodney W. Grimes 		 * if so, we lose.  Otherwise, if this is the last entry, or
630df8bae1dSRodney W. Grimes 		 * if the proposed new region fits before the next entry, we
631df8bae1dSRodney W. Grimes 		 * win.
632df8bae1dSRodney W. Grimes 		 */
633df8bae1dSRodney W. Grimes 		end = start + length;
634df8bae1dSRodney W. Grimes 		if (end > map->max_offset || end < start)
635df8bae1dSRodney W. Grimes 			return (1);
636df8bae1dSRodney W. Grimes 		next = entry->next;
637df8bae1dSRodney W. Grimes 		if (next == &map->header || next->start >= end)
638df8bae1dSRodney W. Grimes 			break;
639df8bae1dSRodney W. Grimes 	}
640df8bae1dSRodney W. Grimes 	SAVE_HINT(map, entry);
641df8bae1dSRodney W. Grimes 	*addr = start;
64299448ed1SJohn Dyson 	if (map == kernel_map) {
64399448ed1SJohn Dyson 		vm_offset_t ksize;
64499448ed1SJohn Dyson 		if ((ksize = round_page(start + length)) > kernel_vm_end) {
64599448ed1SJohn Dyson 			pmap_growkernel(ksize);
64699448ed1SJohn Dyson 		}
64799448ed1SJohn Dyson 	}
648df8bae1dSRodney W. Grimes 	return (0);
649df8bae1dSRodney W. Grimes }
650df8bae1dSRodney W. Grimes 
651df8bae1dSRodney W. Grimes /*
652df8bae1dSRodney W. Grimes  *	vm_map_find finds an unallocated region in the target address
653df8bae1dSRodney W. Grimes  *	map with the given length.  The search is defined to be
654df8bae1dSRodney W. Grimes  *	first-fit from the specified address; the region found is
655df8bae1dSRodney W. Grimes  *	returned in the same parameter.
656df8bae1dSRodney W. Grimes  *
6572aaeadf8SMatthew Dillon  *	If object is non-NULL, ref count must be bumped by caller
6582aaeadf8SMatthew Dillon  *	prior to making call to account for the new entry.
659df8bae1dSRodney W. Grimes  */
660df8bae1dSRodney W. Grimes int
661b9dcd593SBruce Evans vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
662b9dcd593SBruce Evans 	    vm_offset_t *addr,	/* IN/OUT */
663b9dcd593SBruce Evans 	    vm_size_t length, boolean_t find_space, vm_prot_t prot,
664b9dcd593SBruce Evans 	    vm_prot_t max, int cow)
665df8bae1dSRodney W. Grimes {
666c0877f10SJohn Dyson 	vm_offset_t start;
6678d6e8edeSDavid Greenman 	int result, s = 0;
668df8bae1dSRodney W. Grimes 
669df8bae1dSRodney W. Grimes 	start = *addr;
6708d6e8edeSDavid Greenman 
6719579ee64SDavid Greenman 	if (map == kmem_map || map == mb_map)
672b18bfc3dSJohn Dyson 		s = splvm();
6738d6e8edeSDavid Greenman 
674bea41bcfSDavid Greenman 	vm_map_lock(map);
675df8bae1dSRodney W. Grimes 	if (find_space) {
676df8bae1dSRodney W. Grimes 		if (vm_map_findspace(map, start, length, addr)) {
677df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
6789579ee64SDavid Greenman 			if (map == kmem_map || map == mb_map)
6798d6e8edeSDavid Greenman 				splx(s);
680df8bae1dSRodney W. Grimes 			return (KERN_NO_SPACE);
681df8bae1dSRodney W. Grimes 		}
682df8bae1dSRodney W. Grimes 		start = *addr;
683df8bae1dSRodney W. Grimes 	}
684bd7e5f99SJohn Dyson 	result = vm_map_insert(map, object, offset,
685bd7e5f99SJohn Dyson 		start, start + length, prot, max, cow);
686df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
6878d6e8edeSDavid Greenman 
6889579ee64SDavid Greenman 	if (map == kmem_map || map == mb_map)
6898d6e8edeSDavid Greenman 		splx(s);
6908d6e8edeSDavid Greenman 
691df8bae1dSRodney W. Grimes 	return (result);
692df8bae1dSRodney W. Grimes }
693df8bae1dSRodney W. Grimes 
694df8bae1dSRodney W. Grimes /*
695b7b2aac2SJohn Dyson  *	vm_map_simplify_entry:
69667bf6868SJohn Dyson  *
6974e71e795SMatthew Dillon  *	Simplify the given map entry by merging with either neighbor.  This
6984e71e795SMatthew Dillon  *	routine also has the ability to merge with both neighbors.
6994e71e795SMatthew Dillon  *
7004e71e795SMatthew Dillon  *	The map must be locked.
7014e71e795SMatthew Dillon  *
7024e71e795SMatthew Dillon  *	This routine guarentees that the passed entry remains valid (though
7034e71e795SMatthew Dillon  *	possibly extended).  When merging, this routine may delete one or
7044e71e795SMatthew Dillon  *	both neighbors.
705df8bae1dSRodney W. Grimes  */
706b7b2aac2SJohn Dyson void
7070d94caffSDavid Greenman vm_map_simplify_entry(map, entry)
708df8bae1dSRodney W. Grimes 	vm_map_t map;
709df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
710df8bae1dSRodney W. Grimes {
711308c24baSJohn Dyson 	vm_map_entry_t next, prev;
712b7b2aac2SJohn Dyson 	vm_size_t prevsize, esize;
713df8bae1dSRodney W. Grimes 
7149fdfe602SMatthew Dillon 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
715df8bae1dSRodney W. Grimes 		return;
716308c24baSJohn Dyson 
717308c24baSJohn Dyson 	prev = entry->prev;
718308c24baSJohn Dyson 	if (prev != &map->header) {
71967bf6868SJohn Dyson 		prevsize = prev->end - prev->start;
72067bf6868SJohn Dyson 		if ( (prev->end == entry->start) &&
72167bf6868SJohn Dyson 		     (prev->object.vm_object == entry->object.vm_object) &&
72295e5e988SJohn Dyson 		     (!prev->object.vm_object ||
72367bf6868SJohn Dyson 			(prev->offset + prevsize == entry->offset)) &&
724afa07f7eSJohn Dyson 		     (prev->eflags == entry->eflags) &&
72567bf6868SJohn Dyson 		     (prev->protection == entry->protection) &&
72667bf6868SJohn Dyson 		     (prev->max_protection == entry->max_protection) &&
72767bf6868SJohn Dyson 		     (prev->inheritance == entry->inheritance) &&
728b7b2aac2SJohn Dyson 		     (prev->wired_count == entry->wired_count)) {
729308c24baSJohn Dyson 			if (map->first_free == prev)
730308c24baSJohn Dyson 				map->first_free = entry;
731b18bfc3dSJohn Dyson 			if (map->hint == prev)
732b18bfc3dSJohn Dyson 				map->hint = entry;
733308c24baSJohn Dyson 			vm_map_entry_unlink(map, prev);
734308c24baSJohn Dyson 			entry->start = prev->start;
735308c24baSJohn Dyson 			entry->offset = prev->offset;
736b18bfc3dSJohn Dyson 			if (prev->object.vm_object)
737308c24baSJohn Dyson 				vm_object_deallocate(prev->object.vm_object);
738308c24baSJohn Dyson 			vm_map_entry_dispose(map, prev);
739308c24baSJohn Dyson 		}
740308c24baSJohn Dyson 	}
741de5f6a77SJohn Dyson 
742de5f6a77SJohn Dyson 	next = entry->next;
743308c24baSJohn Dyson 	if (next != &map->header) {
74467bf6868SJohn Dyson 		esize = entry->end - entry->start;
74567bf6868SJohn Dyson 		if ((entry->end == next->start) &&
74667bf6868SJohn Dyson 		    (next->object.vm_object == entry->object.vm_object) &&
74767bf6868SJohn Dyson 		     (!entry->object.vm_object ||
74867bf6868SJohn Dyson 			(entry->offset + esize == next->offset)) &&
749afa07f7eSJohn Dyson 		    (next->eflags == entry->eflags) &&
75067bf6868SJohn Dyson 		    (next->protection == entry->protection) &&
75167bf6868SJohn Dyson 		    (next->max_protection == entry->max_protection) &&
75267bf6868SJohn Dyson 		    (next->inheritance == entry->inheritance) &&
753b7b2aac2SJohn Dyson 		    (next->wired_count == entry->wired_count)) {
754308c24baSJohn Dyson 			if (map->first_free == next)
755308c24baSJohn Dyson 				map->first_free = entry;
756b18bfc3dSJohn Dyson 			if (map->hint == next)
757b18bfc3dSJohn Dyson 				map->hint = entry;
758de5f6a77SJohn Dyson 			vm_map_entry_unlink(map, next);
759de5f6a77SJohn Dyson 			entry->end = next->end;
760b18bfc3dSJohn Dyson 			if (next->object.vm_object)
761de5f6a77SJohn Dyson 				vm_object_deallocate(next->object.vm_object);
762de5f6a77SJohn Dyson 			vm_map_entry_dispose(map, next);
763df8bae1dSRodney W. Grimes 	        }
764df8bae1dSRodney W. Grimes 	}
765de5f6a77SJohn Dyson }
766df8bae1dSRodney W. Grimes /*
767df8bae1dSRodney W. Grimes  *	vm_map_clip_start:	[ internal use only ]
768df8bae1dSRodney W. Grimes  *
769df8bae1dSRodney W. Grimes  *	Asserts that the given entry begins at or after
770df8bae1dSRodney W. Grimes  *	the specified address; if necessary,
771df8bae1dSRodney W. Grimes  *	it splits the entry into two.
772df8bae1dSRodney W. Grimes  */
773df8bae1dSRodney W. Grimes #define vm_map_clip_start(map, entry, startaddr) \
774df8bae1dSRodney W. Grimes { \
775df8bae1dSRodney W. Grimes 	if (startaddr > entry->start) \
776df8bae1dSRodney W. Grimes 		_vm_map_clip_start(map, entry, startaddr); \
777df8bae1dSRodney W. Grimes }
778df8bae1dSRodney W. Grimes 
779df8bae1dSRodney W. Grimes /*
780df8bae1dSRodney W. Grimes  *	This routine is called only when it is known that
781df8bae1dSRodney W. Grimes  *	the entry must be split.
782df8bae1dSRodney W. Grimes  */
7830d94caffSDavid Greenman static void
7840d94caffSDavid Greenman _vm_map_clip_start(map, entry, start)
785c0877f10SJohn Dyson 	vm_map_t map;
786c0877f10SJohn Dyson 	vm_map_entry_t entry;
787c0877f10SJohn Dyson 	vm_offset_t start;
788df8bae1dSRodney W. Grimes {
789c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
790df8bae1dSRodney W. Grimes 
791df8bae1dSRodney W. Grimes 	/*
7920d94caffSDavid Greenman 	 * Split off the front portion -- note that we must insert the new
7930d94caffSDavid Greenman 	 * entry BEFORE this one, so that this entry has the specified
7940d94caffSDavid Greenman 	 * starting address.
795df8bae1dSRodney W. Grimes 	 */
796df8bae1dSRodney W. Grimes 
797f32dbbeeSJohn Dyson 	vm_map_simplify_entry(map, entry);
798f32dbbeeSJohn Dyson 
79911cccda1SJohn Dyson 	/*
80011cccda1SJohn Dyson 	 * If there is no object backing this entry, we might as well create
80111cccda1SJohn Dyson 	 * one now.  If we defer it, an object can get created after the map
80211cccda1SJohn Dyson 	 * is clipped, and individual objects will be created for the split-up
80311cccda1SJohn Dyson 	 * map.  This is a bit of a hack, but is also about the best place to
80411cccda1SJohn Dyson 	 * put this improvement.
80511cccda1SJohn Dyson 	 */
80611cccda1SJohn Dyson 
8074e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL && !map->system_map) {
80811cccda1SJohn Dyson 		vm_object_t object;
80911cccda1SJohn Dyson 		object = vm_object_allocate(OBJT_DEFAULT,
810c2e11a03SJohn Dyson 				atop(entry->end - entry->start));
81111cccda1SJohn Dyson 		entry->object.vm_object = object;
81211cccda1SJohn Dyson 		entry->offset = 0;
81311cccda1SJohn Dyson 	}
81411cccda1SJohn Dyson 
815df8bae1dSRodney W. Grimes 	new_entry = vm_map_entry_create(map);
816df8bae1dSRodney W. Grimes 	*new_entry = *entry;
817df8bae1dSRodney W. Grimes 
818df8bae1dSRodney W. Grimes 	new_entry->end = start;
819df8bae1dSRodney W. Grimes 	entry->offset += (start - entry->start);
820df8bae1dSRodney W. Grimes 	entry->start = start;
821df8bae1dSRodney W. Grimes 
822df8bae1dSRodney W. Grimes 	vm_map_entry_link(map, entry->prev, new_entry);
823df8bae1dSRodney W. Grimes 
8249fdfe602SMatthew Dillon 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
825df8bae1dSRodney W. Grimes 		vm_object_reference(new_entry->object.vm_object);
826df8bae1dSRodney W. Grimes 	}
827c0877f10SJohn Dyson }
828df8bae1dSRodney W. Grimes 
829df8bae1dSRodney W. Grimes /*
830df8bae1dSRodney W. Grimes  *	vm_map_clip_end:	[ internal use only ]
831df8bae1dSRodney W. Grimes  *
832df8bae1dSRodney W. Grimes  *	Asserts that the given entry ends at or before
833df8bae1dSRodney W. Grimes  *	the specified address; if necessary,
834df8bae1dSRodney W. Grimes  *	it splits the entry into two.
835df8bae1dSRodney W. Grimes  */
836df8bae1dSRodney W. Grimes 
837df8bae1dSRodney W. Grimes #define vm_map_clip_end(map, entry, endaddr) \
838df8bae1dSRodney W. Grimes { \
839df8bae1dSRodney W. Grimes 	if (endaddr < entry->end) \
840df8bae1dSRodney W. Grimes 		_vm_map_clip_end(map, entry, endaddr); \
841df8bae1dSRodney W. Grimes }
842df8bae1dSRodney W. Grimes 
843df8bae1dSRodney W. Grimes /*
844df8bae1dSRodney W. Grimes  *	This routine is called only when it is known that
845df8bae1dSRodney W. Grimes  *	the entry must be split.
846df8bae1dSRodney W. Grimes  */
8470d94caffSDavid Greenman static void
8480d94caffSDavid Greenman _vm_map_clip_end(map, entry, end)
849c0877f10SJohn Dyson 	vm_map_t map;
850c0877f10SJohn Dyson 	vm_map_entry_t entry;
851c0877f10SJohn Dyson 	vm_offset_t end;
852df8bae1dSRodney W. Grimes {
853c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
854df8bae1dSRodney W. Grimes 
855df8bae1dSRodney W. Grimes 	/*
85611cccda1SJohn Dyson 	 * If there is no object backing this entry, we might as well create
85711cccda1SJohn Dyson 	 * one now.  If we defer it, an object can get created after the map
85811cccda1SJohn Dyson 	 * is clipped, and individual objects will be created for the split-up
85911cccda1SJohn Dyson 	 * map.  This is a bit of a hack, but is also about the best place to
86011cccda1SJohn Dyson 	 * put this improvement.
86111cccda1SJohn Dyson 	 */
86211cccda1SJohn Dyson 
8634e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL && !map->system_map) {
86411cccda1SJohn Dyson 		vm_object_t object;
86511cccda1SJohn Dyson 		object = vm_object_allocate(OBJT_DEFAULT,
866c2e11a03SJohn Dyson 				atop(entry->end - entry->start));
86711cccda1SJohn Dyson 		entry->object.vm_object = object;
86811cccda1SJohn Dyson 		entry->offset = 0;
86911cccda1SJohn Dyson 	}
87011cccda1SJohn Dyson 
87111cccda1SJohn Dyson 	/*
8720d94caffSDavid Greenman 	 * Create a new entry and insert it AFTER the specified entry
873df8bae1dSRodney W. Grimes 	 */
874df8bae1dSRodney W. Grimes 
875df8bae1dSRodney W. Grimes 	new_entry = vm_map_entry_create(map);
876df8bae1dSRodney W. Grimes 	*new_entry = *entry;
877df8bae1dSRodney W. Grimes 
878df8bae1dSRodney W. Grimes 	new_entry->start = entry->end = end;
879df8bae1dSRodney W. Grimes 	new_entry->offset += (end - entry->start);
880df8bae1dSRodney W. Grimes 
881df8bae1dSRodney W. Grimes 	vm_map_entry_link(map, entry, new_entry);
882df8bae1dSRodney W. Grimes 
8839fdfe602SMatthew Dillon 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
884df8bae1dSRodney W. Grimes 		vm_object_reference(new_entry->object.vm_object);
885df8bae1dSRodney W. Grimes 	}
886c0877f10SJohn Dyson }
887df8bae1dSRodney W. Grimes 
888df8bae1dSRodney W. Grimes /*
889df8bae1dSRodney W. Grimes  *	VM_MAP_RANGE_CHECK:	[ internal use only ]
890df8bae1dSRodney W. Grimes  *
891df8bae1dSRodney W. Grimes  *	Asserts that the starting and ending region
892df8bae1dSRodney W. Grimes  *	addresses fall within the valid range of the map.
893df8bae1dSRodney W. Grimes  */
894df8bae1dSRodney W. Grimes #define	VM_MAP_RANGE_CHECK(map, start, end)		\
895df8bae1dSRodney W. Grimes 		{					\
896df8bae1dSRodney W. Grimes 		if (start < vm_map_min(map))		\
897df8bae1dSRodney W. Grimes 			start = vm_map_min(map);	\
898df8bae1dSRodney W. Grimes 		if (end > vm_map_max(map))		\
899df8bae1dSRodney W. Grimes 			end = vm_map_max(map);		\
900df8bae1dSRodney W. Grimes 		if (start > end)			\
901df8bae1dSRodney W. Grimes 			start = end;			\
902df8bae1dSRodney W. Grimes 		}
903df8bae1dSRodney W. Grimes 
904df8bae1dSRodney W. Grimes /*
905df8bae1dSRodney W. Grimes  *	vm_map_submap:		[ kernel use only ]
906df8bae1dSRodney W. Grimes  *
907df8bae1dSRodney W. Grimes  *	Mark the given range as handled by a subordinate map.
908df8bae1dSRodney W. Grimes  *
909df8bae1dSRodney W. Grimes  *	This range must have been created with vm_map_find,
910df8bae1dSRodney W. Grimes  *	and no other operations may have been performed on this
911df8bae1dSRodney W. Grimes  *	range prior to calling vm_map_submap.
912df8bae1dSRodney W. Grimes  *
913df8bae1dSRodney W. Grimes  *	Only a limited number of operations can be performed
914df8bae1dSRodney W. Grimes  *	within this rage after calling vm_map_submap:
915df8bae1dSRodney W. Grimes  *		vm_fault
916df8bae1dSRodney W. Grimes  *	[Don't try vm_map_copy!]
917df8bae1dSRodney W. Grimes  *
918df8bae1dSRodney W. Grimes  *	To remove a submapping, one must first remove the
919df8bae1dSRodney W. Grimes  *	range from the superior map, and then destroy the
920df8bae1dSRodney W. Grimes  *	submap (if desired).  [Better yet, don't try it.]
921df8bae1dSRodney W. Grimes  */
922df8bae1dSRodney W. Grimes int
923df8bae1dSRodney W. Grimes vm_map_submap(map, start, end, submap)
924c0877f10SJohn Dyson 	vm_map_t map;
925c0877f10SJohn Dyson 	vm_offset_t start;
926c0877f10SJohn Dyson 	vm_offset_t end;
927df8bae1dSRodney W. Grimes 	vm_map_t submap;
928df8bae1dSRodney W. Grimes {
929df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
930c0877f10SJohn Dyson 	int result = KERN_INVALID_ARGUMENT;
931df8bae1dSRodney W. Grimes 
932df8bae1dSRodney W. Grimes 	vm_map_lock(map);
933df8bae1dSRodney W. Grimes 
934df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
935df8bae1dSRodney W. Grimes 
936df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &entry)) {
937df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
9380d94caffSDavid Greenman 	} else
939df8bae1dSRodney W. Grimes 		entry = entry->next;
940df8bae1dSRodney W. Grimes 
941df8bae1dSRodney W. Grimes 	vm_map_clip_end(map, entry, end);
942df8bae1dSRodney W. Grimes 
943df8bae1dSRodney W. Grimes 	if ((entry->start == start) && (entry->end == end) &&
9449fdfe602SMatthew Dillon 	    ((entry->eflags & MAP_ENTRY_COW) == 0) &&
945afa07f7eSJohn Dyson 	    (entry->object.vm_object == NULL)) {
9462d8acc0fSJohn Dyson 		entry->object.sub_map = submap;
947afa07f7eSJohn Dyson 		entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
948df8bae1dSRodney W. Grimes 		result = KERN_SUCCESS;
949df8bae1dSRodney W. Grimes 	}
950df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
951df8bae1dSRodney W. Grimes 
952df8bae1dSRodney W. Grimes 	return (result);
953df8bae1dSRodney W. Grimes }
954df8bae1dSRodney W. Grimes 
955df8bae1dSRodney W. Grimes /*
956df8bae1dSRodney W. Grimes  *	vm_map_protect:
957df8bae1dSRodney W. Grimes  *
958df8bae1dSRodney W. Grimes  *	Sets the protection of the specified address
959df8bae1dSRodney W. Grimes  *	region in the target map.  If "set_max" is
960df8bae1dSRodney W. Grimes  *	specified, the maximum protection is to be set;
961df8bae1dSRodney W. Grimes  *	otherwise, only the current protection is affected.
962df8bae1dSRodney W. Grimes  */
963df8bae1dSRodney W. Grimes int
964b9dcd593SBruce Evans vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
965b9dcd593SBruce Evans 	       vm_prot_t new_prot, boolean_t set_max)
966df8bae1dSRodney W. Grimes {
967c0877f10SJohn Dyson 	vm_map_entry_t current;
968df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
969df8bae1dSRodney W. Grimes 
970df8bae1dSRodney W. Grimes 	vm_map_lock(map);
971df8bae1dSRodney W. Grimes 
972df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
973df8bae1dSRodney W. Grimes 
974df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &entry)) {
975df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
976b7b2aac2SJohn Dyson 	} else {
977df8bae1dSRodney W. Grimes 		entry = entry->next;
978b7b2aac2SJohn Dyson 	}
979df8bae1dSRodney W. Grimes 
980df8bae1dSRodney W. Grimes 	/*
9810d94caffSDavid Greenman 	 * Make a first pass to check for protection violations.
982df8bae1dSRodney W. Grimes 	 */
983df8bae1dSRodney W. Grimes 
984df8bae1dSRodney W. Grimes 	current = entry;
985df8bae1dSRodney W. Grimes 	while ((current != &map->header) && (current->start < end)) {
986afa07f7eSJohn Dyson 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
987a1f6d91cSDavid Greenman 			vm_map_unlock(map);
988df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ARGUMENT);
989a1f6d91cSDavid Greenman 		}
990df8bae1dSRodney W. Grimes 		if ((new_prot & current->max_protection) != new_prot) {
991df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
992df8bae1dSRodney W. Grimes 			return (KERN_PROTECTION_FAILURE);
993df8bae1dSRodney W. Grimes 		}
994df8bae1dSRodney W. Grimes 		current = current->next;
995df8bae1dSRodney W. Grimes 	}
996df8bae1dSRodney W. Grimes 
997df8bae1dSRodney W. Grimes 	/*
9980d94caffSDavid Greenman 	 * Go back and fix up protections. [Note that clipping is not
9990d94caffSDavid Greenman 	 * necessary the second time.]
1000df8bae1dSRodney W. Grimes 	 */
1001df8bae1dSRodney W. Grimes 
1002df8bae1dSRodney W. Grimes 	current = entry;
1003df8bae1dSRodney W. Grimes 
1004df8bae1dSRodney W. Grimes 	while ((current != &map->header) && (current->start < end)) {
1005df8bae1dSRodney W. Grimes 		vm_prot_t old_prot;
1006df8bae1dSRodney W. Grimes 
1007df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, current, end);
1008df8bae1dSRodney W. Grimes 
1009df8bae1dSRodney W. Grimes 		old_prot = current->protection;
1010df8bae1dSRodney W. Grimes 		if (set_max)
1011df8bae1dSRodney W. Grimes 			current->protection =
1012df8bae1dSRodney W. Grimes 			    (current->max_protection = new_prot) &
1013df8bae1dSRodney W. Grimes 			    old_prot;
1014df8bae1dSRodney W. Grimes 		else
1015df8bae1dSRodney W. Grimes 			current->protection = new_prot;
1016df8bae1dSRodney W. Grimes 
1017df8bae1dSRodney W. Grimes 		/*
10180d94caffSDavid Greenman 		 * Update physical map if necessary. Worry about copy-on-write
10190d94caffSDavid Greenman 		 * here -- CHECK THIS XXX
1020df8bae1dSRodney W. Grimes 		 */
1021df8bae1dSRodney W. Grimes 
1022df8bae1dSRodney W. Grimes 		if (current->protection != old_prot) {
1023afa07f7eSJohn Dyson #define MASK(entry)	(((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1024df8bae1dSRodney W. Grimes 							VM_PROT_ALL)
1025df8bae1dSRodney W. Grimes 
1026df8bae1dSRodney W. Grimes 			pmap_protect(map->pmap, current->start,
1027df8bae1dSRodney W. Grimes 			    current->end,
10281c85e3dfSAlan Cox 			    current->protection & MASK(current));
1029df8bae1dSRodney W. Grimes #undef	MASK
1030df8bae1dSRodney W. Grimes 		}
10317d78abc9SJohn Dyson 
10327d78abc9SJohn Dyson 		vm_map_simplify_entry(map, current);
10337d78abc9SJohn Dyson 
1034df8bae1dSRodney W. Grimes 		current = current->next;
1035df8bae1dSRodney W. Grimes 	}
1036df8bae1dSRodney W. Grimes 
1037df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1038df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
1039df8bae1dSRodney W. Grimes }
1040df8bae1dSRodney W. Grimes 
1041df8bae1dSRodney W. Grimes /*
1042867a482dSJohn Dyson  *	vm_map_madvise:
1043867a482dSJohn Dyson  *
1044867a482dSJohn Dyson  * 	This routine traverses a processes map handling the madvise
1045f7fc307aSAlan Cox  *	system call.  Advisories are classified as either those effecting
1046f7fc307aSAlan Cox  *	the vm_map_entry structure, or those effecting the underlying
1047f7fc307aSAlan Cox  *	objects.
1048867a482dSJohn Dyson  */
1049b4309055SMatthew Dillon 
1050b4309055SMatthew Dillon int
1051f7fc307aSAlan Cox vm_map_madvise(map, start, end, behav)
1052867a482dSJohn Dyson 	vm_map_t map;
1053867a482dSJohn Dyson 	vm_offset_t start, end;
1054f7fc307aSAlan Cox 	int behav;
1055867a482dSJohn Dyson {
1056f7fc307aSAlan Cox 	vm_map_entry_t current, entry;
1057b4309055SMatthew Dillon 	int modify_map = 0;
1058867a482dSJohn Dyson 
1059b4309055SMatthew Dillon 	/*
1060b4309055SMatthew Dillon 	 * Some madvise calls directly modify the vm_map_entry, in which case
1061b4309055SMatthew Dillon 	 * we need to use an exclusive lock on the map and we need to perform
1062b4309055SMatthew Dillon 	 * various clipping operations.  Otherwise we only need a read-lock
1063b4309055SMatthew Dillon 	 * on the map.
1064b4309055SMatthew Dillon 	 */
1065f7fc307aSAlan Cox 
1066b4309055SMatthew Dillon 	switch(behav) {
1067b4309055SMatthew Dillon 	case MADV_NORMAL:
1068b4309055SMatthew Dillon 	case MADV_SEQUENTIAL:
1069b4309055SMatthew Dillon 	case MADV_RANDOM:
10704f79d873SMatthew Dillon 	case MADV_NOSYNC:
10714f79d873SMatthew Dillon 	case MADV_AUTOSYNC:
10729730a5daSPaul Saab 	case MADV_NOCORE:
10739730a5daSPaul Saab 	case MADV_CORE:
1074b4309055SMatthew Dillon 		modify_map = 1;
1075867a482dSJohn Dyson 		vm_map_lock(map);
1076b4309055SMatthew Dillon 		break;
1077b4309055SMatthew Dillon 	case MADV_WILLNEED:
1078b4309055SMatthew Dillon 	case MADV_DONTNEED:
1079b4309055SMatthew Dillon 	case MADV_FREE:
1080f7fc307aSAlan Cox 		vm_map_lock_read(map);
1081b4309055SMatthew Dillon 		break;
1082b4309055SMatthew Dillon 	default:
1083b4309055SMatthew Dillon 		return (KERN_INVALID_ARGUMENT);
1084b4309055SMatthew Dillon 	}
1085b4309055SMatthew Dillon 
1086b4309055SMatthew Dillon 	/*
1087b4309055SMatthew Dillon 	 * Locate starting entry and clip if necessary.
1088b4309055SMatthew Dillon 	 */
1089867a482dSJohn Dyson 
1090867a482dSJohn Dyson 	VM_MAP_RANGE_CHECK(map, start, end);
1091867a482dSJohn Dyson 
1092867a482dSJohn Dyson 	if (vm_map_lookup_entry(map, start, &entry)) {
1093f7fc307aSAlan Cox 		if (modify_map)
1094867a482dSJohn Dyson 			vm_map_clip_start(map, entry, start);
1095b4309055SMatthew Dillon 	} else {
1096867a482dSJohn Dyson 		entry = entry->next;
1097b4309055SMatthew Dillon 	}
1098867a482dSJohn Dyson 
1099f7fc307aSAlan Cox 	if (modify_map) {
1100f7fc307aSAlan Cox 		/*
1101f7fc307aSAlan Cox 		 * madvise behaviors that are implemented in the vm_map_entry.
1102f7fc307aSAlan Cox 		 *
1103f7fc307aSAlan Cox 		 * We clip the vm_map_entry so that behavioral changes are
1104f7fc307aSAlan Cox 		 * limited to the specified address range.
1105f7fc307aSAlan Cox 		 */
1106867a482dSJohn Dyson 		for (current = entry;
1107867a482dSJohn Dyson 		     (current != &map->header) && (current->start < end);
1108b4309055SMatthew Dillon 		     current = current->next
1109b4309055SMatthew Dillon 		) {
1110f7fc307aSAlan Cox 			if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1111867a482dSJohn Dyson 				continue;
1112fed9a903SJohn Dyson 
111347221757SJohn Dyson 			vm_map_clip_end(map, current, end);
1114fed9a903SJohn Dyson 
1115f7fc307aSAlan Cox 			switch (behav) {
1116867a482dSJohn Dyson 			case MADV_NORMAL:
11177f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
1118867a482dSJohn Dyson 				break;
1119867a482dSJohn Dyson 			case MADV_SEQUENTIAL:
11207f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
1121867a482dSJohn Dyson 				break;
1122867a482dSJohn Dyson 			case MADV_RANDOM:
11237f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
1124867a482dSJohn Dyson 				break;
11254f79d873SMatthew Dillon 			case MADV_NOSYNC:
11264f79d873SMatthew Dillon 				current->eflags |= MAP_ENTRY_NOSYNC;
11274f79d873SMatthew Dillon 				break;
11284f79d873SMatthew Dillon 			case MADV_AUTOSYNC:
11294f79d873SMatthew Dillon 				current->eflags &= ~MAP_ENTRY_NOSYNC;
11304f79d873SMatthew Dillon 				break;
11319730a5daSPaul Saab 			case MADV_NOCORE:
11329730a5daSPaul Saab 				current->eflags |= MAP_ENTRY_NOCOREDUMP;
11339730a5daSPaul Saab 				break;
11349730a5daSPaul Saab 			case MADV_CORE:
11359730a5daSPaul Saab 				current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
11369730a5daSPaul Saab 				break;
1137867a482dSJohn Dyson 			default:
1138867a482dSJohn Dyson 				break;
1139867a482dSJohn Dyson 			}
1140f7fc307aSAlan Cox 			vm_map_simplify_entry(map, current);
1141867a482dSJohn Dyson 		}
1142867a482dSJohn Dyson 		vm_map_unlock(map);
1143b4309055SMatthew Dillon 	} else {
1144f7fc307aSAlan Cox 		vm_pindex_t pindex;
1145f7fc307aSAlan Cox 		int count;
1146f7fc307aSAlan Cox 
1147f7fc307aSAlan Cox 		/*
1148f7fc307aSAlan Cox 		 * madvise behaviors that are implemented in the underlying
1149f7fc307aSAlan Cox 		 * vm_object.
1150f7fc307aSAlan Cox 		 *
1151f7fc307aSAlan Cox 		 * Since we don't clip the vm_map_entry, we have to clip
1152f7fc307aSAlan Cox 		 * the vm_object pindex and count.
1153f7fc307aSAlan Cox 		 */
1154f7fc307aSAlan Cox 		for (current = entry;
1155f7fc307aSAlan Cox 		     (current != &map->header) && (current->start < end);
1156b4309055SMatthew Dillon 		     current = current->next
1157b4309055SMatthew Dillon 		) {
11585f99b57cSMatthew Dillon 			vm_offset_t useStart;
11595f99b57cSMatthew Dillon 
1160f7fc307aSAlan Cox 			if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1161f7fc307aSAlan Cox 				continue;
1162f7fc307aSAlan Cox 
1163f7fc307aSAlan Cox 			pindex = OFF_TO_IDX(current->offset);
1164f7fc307aSAlan Cox 			count = atop(current->end - current->start);
11655f99b57cSMatthew Dillon 			useStart = current->start;
1166f7fc307aSAlan Cox 
1167f7fc307aSAlan Cox 			if (current->start < start) {
1168f7fc307aSAlan Cox 				pindex += atop(start - current->start);
1169f7fc307aSAlan Cox 				count -= atop(start - current->start);
11705f99b57cSMatthew Dillon 				useStart = start;
1171f7fc307aSAlan Cox 			}
1172f7fc307aSAlan Cox 			if (current->end > end)
1173f7fc307aSAlan Cox 				count -= atop(current->end - end);
1174f7fc307aSAlan Cox 
1175f7fc307aSAlan Cox 			if (count <= 0)
1176f7fc307aSAlan Cox 				continue;
1177f7fc307aSAlan Cox 
1178f7fc307aSAlan Cox 			vm_object_madvise(current->object.vm_object,
1179f7fc307aSAlan Cox 					  pindex, count, behav);
1180b4309055SMatthew Dillon 			if (behav == MADV_WILLNEED) {
1181b4309055SMatthew Dillon 				pmap_object_init_pt(
1182b4309055SMatthew Dillon 				    map->pmap,
11835f99b57cSMatthew Dillon 				    useStart,
1184f7fc307aSAlan Cox 				    current->object.vm_object,
1185b4309055SMatthew Dillon 				    pindex,
1186b4309055SMatthew Dillon 				    (count << PAGE_SHIFT),
1187b4309055SMatthew Dillon 				    0
1188b4309055SMatthew Dillon 				);
1189f7fc307aSAlan Cox 			}
1190f7fc307aSAlan Cox 		}
1191f7fc307aSAlan Cox 		vm_map_unlock_read(map);
1192f7fc307aSAlan Cox 	}
1193b4309055SMatthew Dillon 	return(0);
1194867a482dSJohn Dyson }
1195867a482dSJohn Dyson 
1196867a482dSJohn Dyson 
1197867a482dSJohn Dyson /*
1198df8bae1dSRodney W. Grimes  *	vm_map_inherit:
1199df8bae1dSRodney W. Grimes  *
1200df8bae1dSRodney W. Grimes  *	Sets the inheritance of the specified address
1201df8bae1dSRodney W. Grimes  *	range in the target map.  Inheritance
1202df8bae1dSRodney W. Grimes  *	affects how the map will be shared with
1203df8bae1dSRodney W. Grimes  *	child maps at the time of vm_map_fork.
1204df8bae1dSRodney W. Grimes  */
1205df8bae1dSRodney W. Grimes int
1206b9dcd593SBruce Evans vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
1207b9dcd593SBruce Evans 	       vm_inherit_t new_inheritance)
1208df8bae1dSRodney W. Grimes {
1209c0877f10SJohn Dyson 	vm_map_entry_t entry;
1210df8bae1dSRodney W. Grimes 	vm_map_entry_t temp_entry;
1211df8bae1dSRodney W. Grimes 
1212df8bae1dSRodney W. Grimes 	switch (new_inheritance) {
1213df8bae1dSRodney W. Grimes 	case VM_INHERIT_NONE:
1214df8bae1dSRodney W. Grimes 	case VM_INHERIT_COPY:
1215df8bae1dSRodney W. Grimes 	case VM_INHERIT_SHARE:
1216df8bae1dSRodney W. Grimes 		break;
1217df8bae1dSRodney W. Grimes 	default:
1218df8bae1dSRodney W. Grimes 		return (KERN_INVALID_ARGUMENT);
1219df8bae1dSRodney W. Grimes 	}
1220df8bae1dSRodney W. Grimes 
1221df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1222df8bae1dSRodney W. Grimes 
1223df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1224df8bae1dSRodney W. Grimes 
1225df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &temp_entry)) {
1226df8bae1dSRodney W. Grimes 		entry = temp_entry;
1227df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
12280d94caffSDavid Greenman 	} else
1229df8bae1dSRodney W. Grimes 		entry = temp_entry->next;
1230df8bae1dSRodney W. Grimes 
1231df8bae1dSRodney W. Grimes 	while ((entry != &map->header) && (entry->start < end)) {
1232df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, entry, end);
1233df8bae1dSRodney W. Grimes 
1234df8bae1dSRodney W. Grimes 		entry->inheritance = new_inheritance;
1235df8bae1dSRodney W. Grimes 
123644428f62SAlan Cox 		vm_map_simplify_entry(map, entry);
123744428f62SAlan Cox 
1238df8bae1dSRodney W. Grimes 		entry = entry->next;
1239df8bae1dSRodney W. Grimes 	}
1240df8bae1dSRodney W. Grimes 
1241df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1242df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
1243df8bae1dSRodney W. Grimes }
1244df8bae1dSRodney W. Grimes 
1245df8bae1dSRodney W. Grimes /*
12467aaaa4fdSJohn Dyson  * Implement the semantics of mlock
12477aaaa4fdSJohn Dyson  */
12487aaaa4fdSJohn Dyson int
12497aaaa4fdSJohn Dyson vm_map_user_pageable(map, start, end, new_pageable)
1250c0877f10SJohn Dyson 	vm_map_t map;
1251c0877f10SJohn Dyson 	vm_offset_t start;
1252c0877f10SJohn Dyson 	vm_offset_t end;
1253c0877f10SJohn Dyson 	boolean_t new_pageable;
12547aaaa4fdSJohn Dyson {
1255b44959ceSTor Egge 	vm_map_entry_t entry;
12567aaaa4fdSJohn Dyson 	vm_map_entry_t start_entry;
1257b44959ceSTor Egge 	vm_offset_t estart;
12587aaaa4fdSJohn Dyson 	int rv;
12597aaaa4fdSJohn Dyson 
12607aaaa4fdSJohn Dyson 	vm_map_lock(map);
12617aaaa4fdSJohn Dyson 	VM_MAP_RANGE_CHECK(map, start, end);
12627aaaa4fdSJohn Dyson 
12637aaaa4fdSJohn Dyson 	if (vm_map_lookup_entry(map, start, &start_entry) == FALSE) {
12647aaaa4fdSJohn Dyson 		vm_map_unlock(map);
12657aaaa4fdSJohn Dyson 		return (KERN_INVALID_ADDRESS);
12667aaaa4fdSJohn Dyson 	}
12677aaaa4fdSJohn Dyson 
12687aaaa4fdSJohn Dyson 	if (new_pageable) {
12697aaaa4fdSJohn Dyson 
12707aaaa4fdSJohn Dyson 		entry = start_entry;
12717aaaa4fdSJohn Dyson 		vm_map_clip_start(map, entry, start);
12727aaaa4fdSJohn Dyson 
12737aaaa4fdSJohn Dyson 		/*
12747aaaa4fdSJohn Dyson 		 * Now decrement the wiring count for each region. If a region
12757aaaa4fdSJohn Dyson 		 * becomes completely unwired, unwire its physical pages and
12767aaaa4fdSJohn Dyson 		 * mappings.
12777aaaa4fdSJohn Dyson 		 */
12787aaaa4fdSJohn Dyson 		while ((entry != &map->header) && (entry->start < end)) {
1279afa07f7eSJohn Dyson 			if (entry->eflags & MAP_ENTRY_USER_WIRED) {
12807aaaa4fdSJohn Dyson 				vm_map_clip_end(map, entry, end);
1281afa07f7eSJohn Dyson 				entry->eflags &= ~MAP_ENTRY_USER_WIRED;
12827aaaa4fdSJohn Dyson 				entry->wired_count--;
12837aaaa4fdSJohn Dyson 				if (entry->wired_count == 0)
12847aaaa4fdSJohn Dyson 					vm_fault_unwire(map, entry->start, entry->end);
12857aaaa4fdSJohn Dyson 			}
1286b44959ceSTor Egge 			vm_map_simplify_entry(map,entry);
12877aaaa4fdSJohn Dyson 			entry = entry->next;
12887aaaa4fdSJohn Dyson 		}
12897aaaa4fdSJohn Dyson 	} else {
12907aaaa4fdSJohn Dyson 
12917aaaa4fdSJohn Dyson 		entry = start_entry;
12927aaaa4fdSJohn Dyson 
12937aaaa4fdSJohn Dyson 		while ((entry != &map->header) && (entry->start < end)) {
12947aaaa4fdSJohn Dyson 
1295afa07f7eSJohn Dyson 			if (entry->eflags & MAP_ENTRY_USER_WIRED) {
12967aaaa4fdSJohn Dyson 				entry = entry->next;
12977aaaa4fdSJohn Dyson 				continue;
12987aaaa4fdSJohn Dyson 			}
12997aaaa4fdSJohn Dyson 
13007aaaa4fdSJohn Dyson 			if (entry->wired_count != 0) {
13017aaaa4fdSJohn Dyson 				entry->wired_count++;
1302afa07f7eSJohn Dyson 				entry->eflags |= MAP_ENTRY_USER_WIRED;
13037aaaa4fdSJohn Dyson 				entry = entry->next;
13047aaaa4fdSJohn Dyson 				continue;
13057aaaa4fdSJohn Dyson 			}
13067aaaa4fdSJohn Dyson 
13077aaaa4fdSJohn Dyson 			/* Here on entry being newly wired */
13087aaaa4fdSJohn Dyson 
13099fdfe602SMatthew Dillon 			if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1310afa07f7eSJohn Dyson 				int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY;
13117aaaa4fdSJohn Dyson 				if (copyflag && ((entry->protection & VM_PROT_WRITE) != 0)) {
13127aaaa4fdSJohn Dyson 
13137aaaa4fdSJohn Dyson 					vm_object_shadow(&entry->object.vm_object,
13147aaaa4fdSJohn Dyson 					    &entry->offset,
1315c2e11a03SJohn Dyson 					    atop(entry->end - entry->start));
1316afa07f7eSJohn Dyson 					entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
13177aaaa4fdSJohn Dyson 
13184e71e795SMatthew Dillon 				} else if (entry->object.vm_object == NULL &&
13194e71e795SMatthew Dillon 					   !map->system_map) {
13207aaaa4fdSJohn Dyson 
13217aaaa4fdSJohn Dyson 					entry->object.vm_object =
13227aaaa4fdSJohn Dyson 					    vm_object_allocate(OBJT_DEFAULT,
1323c2e11a03SJohn Dyson 						atop(entry->end - entry->start));
13247aaaa4fdSJohn Dyson 					entry->offset = (vm_offset_t) 0;
13257aaaa4fdSJohn Dyson 
13267aaaa4fdSJohn Dyson 				}
13277aaaa4fdSJohn Dyson 			}
13287aaaa4fdSJohn Dyson 
13297aaaa4fdSJohn Dyson 			vm_map_clip_start(map, entry, start);
13307aaaa4fdSJohn Dyson 			vm_map_clip_end(map, entry, end);
13317aaaa4fdSJohn Dyson 
13327aaaa4fdSJohn Dyson 			entry->wired_count++;
1333afa07f7eSJohn Dyson 			entry->eflags |= MAP_ENTRY_USER_WIRED;
1334b44959ceSTor Egge 			estart = entry->start;
13357aaaa4fdSJohn Dyson 
13367aaaa4fdSJohn Dyson 			/* First we need to allow map modifications */
1337996c772fSJohn Dyson 			vm_map_set_recursive(map);
133803e9c6c1SJohn Dyson 			vm_map_lock_downgrade(map);
133947221757SJohn Dyson 			map->timestamp++;
13407aaaa4fdSJohn Dyson 
13417aaaa4fdSJohn Dyson 			rv = vm_fault_user_wire(map, entry->start, entry->end);
13427aaaa4fdSJohn Dyson 			if (rv) {
13437aaaa4fdSJohn Dyson 
13447aaaa4fdSJohn Dyson 				entry->wired_count--;
1345afa07f7eSJohn Dyson 				entry->eflags &= ~MAP_ENTRY_USER_WIRED;
13467aaaa4fdSJohn Dyson 
1347996c772fSJohn Dyson 				vm_map_clear_recursive(map);
13487aaaa4fdSJohn Dyson 				vm_map_unlock(map);
13497aaaa4fdSJohn Dyson 
13507aaaa4fdSJohn Dyson 				(void) vm_map_user_pageable(map, start, entry->start, TRUE);
13517aaaa4fdSJohn Dyson 				return rv;
13527aaaa4fdSJohn Dyson 			}
13537aaaa4fdSJohn Dyson 
1354996c772fSJohn Dyson 			vm_map_clear_recursive(map);
1355b44959ceSTor Egge 			if (vm_map_lock_upgrade(map)) {
1356b44959ceSTor Egge 				vm_map_lock(map);
1357b44959ceSTor Egge 				if (vm_map_lookup_entry(map, estart, &entry)
1358b44959ceSTor Egge 				    == FALSE) {
1359b44959ceSTor Egge 					vm_map_unlock(map);
1360b44959ceSTor Egge 					(void) vm_map_user_pageable(map,
1361b44959ceSTor Egge 								    start,
1362b44959ceSTor Egge 								    estart,
1363b44959ceSTor Egge 								    TRUE);
1364b44959ceSTor Egge 					return (KERN_INVALID_ADDRESS);
1365b44959ceSTor Egge 				}
1366b44959ceSTor Egge 			}
1367b44959ceSTor Egge 			vm_map_simplify_entry(map,entry);
13687aaaa4fdSJohn Dyson 		}
13697aaaa4fdSJohn Dyson 	}
137047221757SJohn Dyson 	map->timestamp++;
13717aaaa4fdSJohn Dyson 	vm_map_unlock(map);
13727aaaa4fdSJohn Dyson 	return KERN_SUCCESS;
13737aaaa4fdSJohn Dyson }
13747aaaa4fdSJohn Dyson 
13757aaaa4fdSJohn Dyson /*
1376df8bae1dSRodney W. Grimes  *	vm_map_pageable:
1377df8bae1dSRodney W. Grimes  *
1378df8bae1dSRodney W. Grimes  *	Sets the pageability of the specified address
1379df8bae1dSRodney W. Grimes  *	range in the target map.  Regions specified
1380df8bae1dSRodney W. Grimes  *	as not pageable require locked-down physical
1381df8bae1dSRodney W. Grimes  *	memory and physical page maps.
1382df8bae1dSRodney W. Grimes  *
1383df8bae1dSRodney W. Grimes  *	The map must not be locked, but a reference
1384df8bae1dSRodney W. Grimes  *	must remain to the map throughout the call.
1385df8bae1dSRodney W. Grimes  */
1386df8bae1dSRodney W. Grimes int
1387df8bae1dSRodney W. Grimes vm_map_pageable(map, start, end, new_pageable)
1388c0877f10SJohn Dyson 	vm_map_t map;
1389c0877f10SJohn Dyson 	vm_offset_t start;
1390c0877f10SJohn Dyson 	vm_offset_t end;
1391c0877f10SJohn Dyson 	boolean_t new_pageable;
1392df8bae1dSRodney W. Grimes {
1393c0877f10SJohn Dyson 	vm_map_entry_t entry;
1394df8bae1dSRodney W. Grimes 	vm_map_entry_t start_entry;
1395c0877f10SJohn Dyson 	vm_offset_t failed = 0;
1396df8bae1dSRodney W. Grimes 	int rv;
1397df8bae1dSRodney W. Grimes 
1398df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1399df8bae1dSRodney W. Grimes 
1400df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1401df8bae1dSRodney W. Grimes 
1402df8bae1dSRodney W. Grimes 	/*
14030d94caffSDavid Greenman 	 * Only one pageability change may take place at one time, since
14040d94caffSDavid Greenman 	 * vm_fault assumes it will be called only once for each
14050d94caffSDavid Greenman 	 * wiring/unwiring.  Therefore, we have to make sure we're actually
14060d94caffSDavid Greenman 	 * changing the pageability for the entire region.  We do so before
14070d94caffSDavid Greenman 	 * making any changes.
1408df8bae1dSRodney W. Grimes 	 */
1409df8bae1dSRodney W. Grimes 
1410df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &start_entry) == FALSE) {
1411df8bae1dSRodney W. Grimes 		vm_map_unlock(map);
1412df8bae1dSRodney W. Grimes 		return (KERN_INVALID_ADDRESS);
1413df8bae1dSRodney W. Grimes 	}
1414df8bae1dSRodney W. Grimes 	entry = start_entry;
1415df8bae1dSRodney W. Grimes 
1416df8bae1dSRodney W. Grimes 	/*
14170d94caffSDavid Greenman 	 * Actions are rather different for wiring and unwiring, so we have
14180d94caffSDavid Greenman 	 * two separate cases.
1419df8bae1dSRodney W. Grimes 	 */
1420df8bae1dSRodney W. Grimes 
1421df8bae1dSRodney W. Grimes 	if (new_pageable) {
1422df8bae1dSRodney W. Grimes 
1423df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
1424df8bae1dSRodney W. Grimes 
1425df8bae1dSRodney W. Grimes 		/*
14260d94caffSDavid Greenman 		 * Unwiring.  First ensure that the range to be unwired is
14270d94caffSDavid Greenman 		 * really wired down and that there are no holes.
1428df8bae1dSRodney W. Grimes 		 */
1429df8bae1dSRodney W. Grimes 		while ((entry != &map->header) && (entry->start < end)) {
1430df8bae1dSRodney W. Grimes 
1431df8bae1dSRodney W. Grimes 			if (entry->wired_count == 0 ||
1432df8bae1dSRodney W. Grimes 			    (entry->end < end &&
1433df8bae1dSRodney W. Grimes 				(entry->next == &map->header ||
1434df8bae1dSRodney W. Grimes 				    entry->next->start > entry->end))) {
1435df8bae1dSRodney W. Grimes 				vm_map_unlock(map);
1436df8bae1dSRodney W. Grimes 				return (KERN_INVALID_ARGUMENT);
1437df8bae1dSRodney W. Grimes 			}
1438df8bae1dSRodney W. Grimes 			entry = entry->next;
1439df8bae1dSRodney W. Grimes 		}
1440df8bae1dSRodney W. Grimes 
1441df8bae1dSRodney W. Grimes 		/*
14420d94caffSDavid Greenman 		 * Now decrement the wiring count for each region. If a region
14430d94caffSDavid Greenman 		 * becomes completely unwired, unwire its physical pages and
14440d94caffSDavid Greenman 		 * mappings.
1445df8bae1dSRodney W. Grimes 		 */
1446df8bae1dSRodney W. Grimes 		entry = start_entry;
1447df8bae1dSRodney W. Grimes 		while ((entry != &map->header) && (entry->start < end)) {
1448df8bae1dSRodney W. Grimes 			vm_map_clip_end(map, entry, end);
1449df8bae1dSRodney W. Grimes 
1450df8bae1dSRodney W. Grimes 			entry->wired_count--;
1451df8bae1dSRodney W. Grimes 			if (entry->wired_count == 0)
1452df8bae1dSRodney W. Grimes 				vm_fault_unwire(map, entry->start, entry->end);
1453df8bae1dSRodney W. Grimes 
145444428f62SAlan Cox 			vm_map_simplify_entry(map, entry);
145544428f62SAlan Cox 
1456df8bae1dSRodney W. Grimes 			entry = entry->next;
1457df8bae1dSRodney W. Grimes 		}
14580d94caffSDavid Greenman 	} else {
1459df8bae1dSRodney W. Grimes 		/*
1460df8bae1dSRodney W. Grimes 		 * Wiring.  We must do this in two passes:
1461df8bae1dSRodney W. Grimes 		 *
14620d94caffSDavid Greenman 		 * 1.  Holding the write lock, we create any shadow or zero-fill
14630d94caffSDavid Greenman 		 * objects that need to be created. Then we clip each map
14640d94caffSDavid Greenman 		 * entry to the region to be wired and increment its wiring
14650d94caffSDavid Greenman 		 * count.  We create objects before clipping the map entries
1466df8bae1dSRodney W. Grimes 		 * to avoid object proliferation.
1467df8bae1dSRodney W. Grimes 		 *
14680d94caffSDavid Greenman 		 * 2.  We downgrade to a read lock, and call vm_fault_wire to
14690d94caffSDavid Greenman 		 * fault in the pages for any newly wired area (wired_count is
14700d94caffSDavid Greenman 		 * 1).
1471df8bae1dSRodney W. Grimes 		 *
14720d94caffSDavid Greenman 		 * Downgrading to a read lock for vm_fault_wire avoids a possible
147324a1cce3SDavid Greenman 		 * deadlock with another process that may have faulted on one
14740d94caffSDavid Greenman 		 * of the pages to be wired (it would mark the page busy,
14750d94caffSDavid Greenman 		 * blocking us, then in turn block on the map lock that we
14760d94caffSDavid Greenman 		 * hold).  Because of problems in the recursive lock package,
14770d94caffSDavid Greenman 		 * we cannot upgrade to a write lock in vm_map_lookup.  Thus,
14780d94caffSDavid Greenman 		 * any actions that require the write lock must be done
14790d94caffSDavid Greenman 		 * beforehand.  Because we keep the read lock on the map, the
14800d94caffSDavid Greenman 		 * copy-on-write status of the entries we modify here cannot
14810d94caffSDavid Greenman 		 * change.
1482df8bae1dSRodney W. Grimes 		 */
1483df8bae1dSRodney W. Grimes 
1484df8bae1dSRodney W. Grimes 		/*
1485df8bae1dSRodney W. Grimes 		 * Pass 1.
1486df8bae1dSRodney W. Grimes 		 */
1487df8bae1dSRodney W. Grimes 		while ((entry != &map->header) && (entry->start < end)) {
1488df8bae1dSRodney W. Grimes 			if (entry->wired_count == 0) {
1489df8bae1dSRodney W. Grimes 
1490df8bae1dSRodney W. Grimes 				/*
1491df8bae1dSRodney W. Grimes 				 * Perform actions of vm_map_lookup that need
1492df8bae1dSRodney W. Grimes 				 * the write lock on the map: create a shadow
1493df8bae1dSRodney W. Grimes 				 * object for a copy-on-write region, or an
1494df8bae1dSRodney W. Grimes 				 * object for a zero-fill region.
1495df8bae1dSRodney W. Grimes 				 *
1496df8bae1dSRodney W. Grimes 				 * We don't have to do this for entries that
1497ad5fca3bSAlan Cox 				 * point to sub maps, because we won't
1498ad5fca3bSAlan Cox 				 * hold the lock on the sub map.
1499df8bae1dSRodney W. Grimes 				 */
15009fdfe602SMatthew Dillon 				if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1501afa07f7eSJohn Dyson 					int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY;
1502b5b40fa6SJohn Dyson 					if (copyflag &&
1503df8bae1dSRodney W. Grimes 					    ((entry->protection & VM_PROT_WRITE) != 0)) {
1504df8bae1dSRodney W. Grimes 
1505df8bae1dSRodney W. Grimes 						vm_object_shadow(&entry->object.vm_object,
1506df8bae1dSRodney W. Grimes 						    &entry->offset,
1507c2e11a03SJohn Dyson 						    atop(entry->end - entry->start));
1508afa07f7eSJohn Dyson 						entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
15094e71e795SMatthew Dillon 					} else if (entry->object.vm_object == NULL &&
15104e71e795SMatthew Dillon 						   !map->system_map) {
1511df8bae1dSRodney W. Grimes 						entry->object.vm_object =
1512a316d390SJohn Dyson 						    vm_object_allocate(OBJT_DEFAULT,
1513c2e11a03SJohn Dyson 							atop(entry->end - entry->start));
1514df8bae1dSRodney W. Grimes 						entry->offset = (vm_offset_t) 0;
1515df8bae1dSRodney W. Grimes 					}
1516df8bae1dSRodney W. Grimes 				}
1517df8bae1dSRodney W. Grimes 			}
1518df8bae1dSRodney W. Grimes 			vm_map_clip_start(map, entry, start);
1519df8bae1dSRodney W. Grimes 			vm_map_clip_end(map, entry, end);
1520df8bae1dSRodney W. Grimes 			entry->wired_count++;
1521df8bae1dSRodney W. Grimes 
1522df8bae1dSRodney W. Grimes 			/*
1523df8bae1dSRodney W. Grimes 			 * Check for holes
1524df8bae1dSRodney W. Grimes 			 */
1525df8bae1dSRodney W. Grimes 			if (entry->end < end &&
1526df8bae1dSRodney W. Grimes 			    (entry->next == &map->header ||
1527df8bae1dSRodney W. Grimes 				entry->next->start > entry->end)) {
1528df8bae1dSRodney W. Grimes 				/*
15290d94caffSDavid Greenman 				 * Found one.  Object creation actions do not
15300d94caffSDavid Greenman 				 * need to be undone, but the wired counts
15310d94caffSDavid Greenman 				 * need to be restored.
1532df8bae1dSRodney W. Grimes 				 */
1533df8bae1dSRodney W. Grimes 				while (entry != &map->header && entry->end > start) {
1534df8bae1dSRodney W. Grimes 					entry->wired_count--;
1535df8bae1dSRodney W. Grimes 					entry = entry->prev;
1536df8bae1dSRodney W. Grimes 				}
1537df8bae1dSRodney W. Grimes 				vm_map_unlock(map);
1538df8bae1dSRodney W. Grimes 				return (KERN_INVALID_ARGUMENT);
1539df8bae1dSRodney W. Grimes 			}
1540df8bae1dSRodney W. Grimes 			entry = entry->next;
1541df8bae1dSRodney W. Grimes 		}
1542df8bae1dSRodney W. Grimes 
1543df8bae1dSRodney W. Grimes 		/*
1544df8bae1dSRodney W. Grimes 		 * Pass 2.
1545df8bae1dSRodney W. Grimes 		 */
1546df8bae1dSRodney W. Grimes 
1547df8bae1dSRodney W. Grimes 		/*
1548df8bae1dSRodney W. Grimes 		 * HACK HACK HACK HACK
1549df8bae1dSRodney W. Grimes 		 *
155024a1cce3SDavid Greenman 		 * If we are wiring in the kernel map or a submap of it,
155124a1cce3SDavid Greenman 		 * unlock the map to avoid deadlocks.  We trust that the
155224a1cce3SDavid Greenman 		 * kernel is well-behaved, and therefore will not do
155324a1cce3SDavid Greenman 		 * anything destructive to this region of the map while
155424a1cce3SDavid Greenman 		 * we have it unlocked.  We cannot trust user processes
155524a1cce3SDavid Greenman 		 * to do the same.
1556df8bae1dSRodney W. Grimes 		 *
1557df8bae1dSRodney W. Grimes 		 * HACK HACK HACK HACK
1558df8bae1dSRodney W. Grimes 		 */
1559df8bae1dSRodney W. Grimes 		if (vm_map_pmap(map) == kernel_pmap) {
1560df8bae1dSRodney W. Grimes 			vm_map_unlock(map);	/* trust me ... */
15610d94caffSDavid Greenman 		} else {
156203e9c6c1SJohn Dyson 			vm_map_lock_downgrade(map);
1563df8bae1dSRodney W. Grimes 		}
1564df8bae1dSRodney W. Grimes 
1565df8bae1dSRodney W. Grimes 		rv = 0;
1566df8bae1dSRodney W. Grimes 		entry = start_entry;
1567df8bae1dSRodney W. Grimes 		while (entry != &map->header && entry->start < end) {
1568df8bae1dSRodney W. Grimes 			/*
15690d94caffSDavid Greenman 			 * If vm_fault_wire fails for any page we need to undo
15700d94caffSDavid Greenman 			 * what has been done.  We decrement the wiring count
15710d94caffSDavid Greenman 			 * for those pages which have not yet been wired (now)
15720d94caffSDavid Greenman 			 * and unwire those that have (later).
1573df8bae1dSRodney W. Grimes 			 *
1574df8bae1dSRodney W. Grimes 			 * XXX this violates the locking protocol on the map,
1575df8bae1dSRodney W. Grimes 			 * needs to be fixed.
1576df8bae1dSRodney W. Grimes 			 */
1577df8bae1dSRodney W. Grimes 			if (rv)
1578df8bae1dSRodney W. Grimes 				entry->wired_count--;
1579df8bae1dSRodney W. Grimes 			else if (entry->wired_count == 1) {
1580df8bae1dSRodney W. Grimes 				rv = vm_fault_wire(map, entry->start, entry->end);
1581df8bae1dSRodney W. Grimes 				if (rv) {
1582df8bae1dSRodney W. Grimes 					failed = entry->start;
1583df8bae1dSRodney W. Grimes 					entry->wired_count--;
1584df8bae1dSRodney W. Grimes 				}
1585df8bae1dSRodney W. Grimes 			}
1586df8bae1dSRodney W. Grimes 			entry = entry->next;
1587df8bae1dSRodney W. Grimes 		}
1588df8bae1dSRodney W. Grimes 
1589df8bae1dSRodney W. Grimes 		if (vm_map_pmap(map) == kernel_pmap) {
1590df8bae1dSRodney W. Grimes 			vm_map_lock(map);
1591df8bae1dSRodney W. Grimes 		}
1592df8bae1dSRodney W. Grimes 		if (rv) {
1593df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
1594df8bae1dSRodney W. Grimes 			(void) vm_map_pageable(map, start, failed, TRUE);
1595df8bae1dSRodney W. Grimes 			return (rv);
1596df8bae1dSRodney W. Grimes 		}
1597b7b2aac2SJohn Dyson 		vm_map_simplify_entry(map, start_entry);
1598df8bae1dSRodney W. Grimes 	}
1599df8bae1dSRodney W. Grimes 
1600df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1601df8bae1dSRodney W. Grimes 
1602df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
1603df8bae1dSRodney W. Grimes }
1604df8bae1dSRodney W. Grimes 
1605df8bae1dSRodney W. Grimes /*
1606df8bae1dSRodney W. Grimes  * vm_map_clean
1607df8bae1dSRodney W. Grimes  *
1608df8bae1dSRodney W. Grimes  * Push any dirty cached pages in the address range to their pager.
1609df8bae1dSRodney W. Grimes  * If syncio is TRUE, dirty pages are written synchronously.
1610df8bae1dSRodney W. Grimes  * If invalidate is TRUE, any cached pages are freed as well.
1611df8bae1dSRodney W. Grimes  *
1612df8bae1dSRodney W. Grimes  * Returns an error if any part of the specified range is not mapped.
1613df8bae1dSRodney W. Grimes  */
1614df8bae1dSRodney W. Grimes int
1615df8bae1dSRodney W. Grimes vm_map_clean(map, start, end, syncio, invalidate)
1616df8bae1dSRodney W. Grimes 	vm_map_t map;
1617df8bae1dSRodney W. Grimes 	vm_offset_t start;
1618df8bae1dSRodney W. Grimes 	vm_offset_t end;
1619df8bae1dSRodney W. Grimes 	boolean_t syncio;
1620df8bae1dSRodney W. Grimes 	boolean_t invalidate;
1621df8bae1dSRodney W. Grimes {
1622c0877f10SJohn Dyson 	vm_map_entry_t current;
1623df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
1624df8bae1dSRodney W. Grimes 	vm_size_t size;
1625df8bae1dSRodney W. Grimes 	vm_object_t object;
1626a316d390SJohn Dyson 	vm_ooffset_t offset;
1627df8bae1dSRodney W. Grimes 
1628df8bae1dSRodney W. Grimes 	vm_map_lock_read(map);
1629df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1630df8bae1dSRodney W. Grimes 	if (!vm_map_lookup_entry(map, start, &entry)) {
1631df8bae1dSRodney W. Grimes 		vm_map_unlock_read(map);
1632df8bae1dSRodney W. Grimes 		return (KERN_INVALID_ADDRESS);
1633df8bae1dSRodney W. Grimes 	}
1634df8bae1dSRodney W. Grimes 	/*
1635df8bae1dSRodney W. Grimes 	 * Make a first pass to check for holes.
1636df8bae1dSRodney W. Grimes 	 */
1637df8bae1dSRodney W. Grimes 	for (current = entry; current->start < end; current = current->next) {
1638afa07f7eSJohn Dyson 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1639df8bae1dSRodney W. Grimes 			vm_map_unlock_read(map);
1640df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ARGUMENT);
1641df8bae1dSRodney W. Grimes 		}
1642df8bae1dSRodney W. Grimes 		if (end > current->end &&
1643df8bae1dSRodney W. Grimes 		    (current->next == &map->header ||
1644df8bae1dSRodney W. Grimes 			current->end != current->next->start)) {
1645df8bae1dSRodney W. Grimes 			vm_map_unlock_read(map);
1646df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ADDRESS);
1647df8bae1dSRodney W. Grimes 		}
1648df8bae1dSRodney W. Grimes 	}
1649df8bae1dSRodney W. Grimes 
1650cf2819ccSJohn Dyson 	if (invalidate)
1651cf2819ccSJohn Dyson 		pmap_remove(vm_map_pmap(map), start, end);
1652df8bae1dSRodney W. Grimes 	/*
1653df8bae1dSRodney W. Grimes 	 * Make a second pass, cleaning/uncaching pages from the indicated
1654df8bae1dSRodney W. Grimes 	 * objects as we go.
1655df8bae1dSRodney W. Grimes 	 */
1656df8bae1dSRodney W. Grimes 	for (current = entry; current->start < end; current = current->next) {
1657df8bae1dSRodney W. Grimes 		offset = current->offset + (start - current->start);
1658df8bae1dSRodney W. Grimes 		size = (end <= current->end ? end : current->end) - start;
16599fdfe602SMatthew Dillon 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1660c0877f10SJohn Dyson 			vm_map_t smap;
1661df8bae1dSRodney W. Grimes 			vm_map_entry_t tentry;
1662df8bae1dSRodney W. Grimes 			vm_size_t tsize;
1663df8bae1dSRodney W. Grimes 
16649fdfe602SMatthew Dillon 			smap = current->object.sub_map;
1665df8bae1dSRodney W. Grimes 			vm_map_lock_read(smap);
1666df8bae1dSRodney W. Grimes 			(void) vm_map_lookup_entry(smap, offset, &tentry);
1667df8bae1dSRodney W. Grimes 			tsize = tentry->end - offset;
1668df8bae1dSRodney W. Grimes 			if (tsize < size)
1669df8bae1dSRodney W. Grimes 				size = tsize;
1670df8bae1dSRodney W. Grimes 			object = tentry->object.vm_object;
1671df8bae1dSRodney W. Grimes 			offset = tentry->offset + (offset - tentry->start);
1672df8bae1dSRodney W. Grimes 			vm_map_unlock_read(smap);
1673df8bae1dSRodney W. Grimes 		} else {
1674df8bae1dSRodney W. Grimes 			object = current->object.vm_object;
1675df8bae1dSRodney W. Grimes 		}
16768a02c104SJohn Dyson 		/*
16778a02c104SJohn Dyson 		 * Note that there is absolutely no sense in writing out
16788a02c104SJohn Dyson 		 * anonymous objects, so we track down the vnode object
16798a02c104SJohn Dyson 		 * to write out.
16808a02c104SJohn Dyson 		 * We invalidate (remove) all pages from the address space
16818a02c104SJohn Dyson 		 * anyway, for semantic correctness.
16828a02c104SJohn Dyson 		 */
16838a02c104SJohn Dyson 		while (object->backing_object) {
16848a02c104SJohn Dyson 			object = object->backing_object;
16858a02c104SJohn Dyson 			offset += object->backing_object_offset;
16868a02c104SJohn Dyson 			if (object->size < OFF_TO_IDX( offset + size))
16878a02c104SJohn Dyson 				size = IDX_TO_OFF(object->size) - offset;
16888a02c104SJohn Dyson 		}
1689ff359f84SMatthew Dillon 		if (object && (object->type == OBJT_VNODE) &&
1690ff359f84SMatthew Dillon 		    (current->protection & VM_PROT_WRITE)) {
1691df8bae1dSRodney W. Grimes 			/*
1692ff359f84SMatthew Dillon 			 * Flush pages if writing is allowed, invalidate them
1693ff359f84SMatthew Dillon 			 * if invalidation requested.  Pages undergoing I/O
1694ff359f84SMatthew Dillon 			 * will be ignored by vm_object_page_remove().
1695f5cf85d4SDavid Greenman 			 *
1696ff359f84SMatthew Dillon 			 * We cannot lock the vnode and then wait for paging
1697ff359f84SMatthew Dillon 			 * to complete without deadlocking against vm_fault.
1698ff359f84SMatthew Dillon 			 * Instead we simply call vm_object_page_remove() and
1699ff359f84SMatthew Dillon 			 * allow it to block internally on a page-by-page
1700ff359f84SMatthew Dillon 			 * basis when it encounters pages undergoing async
1701ff359f84SMatthew Dillon 			 * I/O.
1702df8bae1dSRodney W. Grimes 			 */
17038f9110f6SJohn Dyson 			int flags;
1704ff359f84SMatthew Dillon 
1705ff359f84SMatthew Dillon 			vm_object_reference(object);
1706157ac55fSJohn Dyson 			vn_lock(object->handle, LK_EXCLUSIVE | LK_RETRY, curproc);
17078f9110f6SJohn Dyson 			flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
17088f9110f6SJohn Dyson 			flags |= invalidate ? OBJPC_INVAL : 0;
1709a316d390SJohn Dyson 			vm_object_page_clean(object,
1710a316d390SJohn Dyson 			    OFF_TO_IDX(offset),
17112be70f79SJohn Dyson 			    OFF_TO_IDX(offset + size + PAGE_MASK),
17128f9110f6SJohn Dyson 			    flags);
1713cf2819ccSJohn Dyson 			if (invalidate) {
1714ff359f84SMatthew Dillon 				/*vm_object_pip_wait(object, "objmcl");*/
1715a316d390SJohn Dyson 				vm_object_page_remove(object,
1716a316d390SJohn Dyson 				    OFF_TO_IDX(offset),
17172be70f79SJohn Dyson 				    OFF_TO_IDX(offset + size + PAGE_MASK),
1718a316d390SJohn Dyson 				    FALSE);
1719cf2819ccSJohn Dyson 			}
17202be70f79SJohn Dyson 			VOP_UNLOCK(object->handle, 0, curproc);
1721ff359f84SMatthew Dillon 			vm_object_deallocate(object);
1722a02051c3SJohn Dyson 		}
1723df8bae1dSRodney W. Grimes 		start += size;
1724df8bae1dSRodney W. Grimes 	}
1725df8bae1dSRodney W. Grimes 
1726df8bae1dSRodney W. Grimes 	vm_map_unlock_read(map);
1727df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
1728df8bae1dSRodney W. Grimes }
1729df8bae1dSRodney W. Grimes 
1730df8bae1dSRodney W. Grimes /*
1731df8bae1dSRodney W. Grimes  *	vm_map_entry_unwire:	[ internal use only ]
1732df8bae1dSRodney W. Grimes  *
1733df8bae1dSRodney W. Grimes  *	Make the region specified by this entry pageable.
1734df8bae1dSRodney W. Grimes  *
1735df8bae1dSRodney W. Grimes  *	The map in question should be locked.
1736df8bae1dSRodney W. Grimes  *	[This is the reason for this routine's existence.]
1737df8bae1dSRodney W. Grimes  */
17380362d7d7SJohn Dyson static void
17390d94caffSDavid Greenman vm_map_entry_unwire(map, entry)
1740df8bae1dSRodney W. Grimes 	vm_map_t map;
1741c0877f10SJohn Dyson 	vm_map_entry_t entry;
1742df8bae1dSRodney W. Grimes {
1743df8bae1dSRodney W. Grimes 	vm_fault_unwire(map, entry->start, entry->end);
1744df8bae1dSRodney W. Grimes 	entry->wired_count = 0;
1745df8bae1dSRodney W. Grimes }
1746df8bae1dSRodney W. Grimes 
1747df8bae1dSRodney W. Grimes /*
1748df8bae1dSRodney W. Grimes  *	vm_map_entry_delete:	[ internal use only ]
1749df8bae1dSRodney W. Grimes  *
1750df8bae1dSRodney W. Grimes  *	Deallocate the given entry from the target map.
1751df8bae1dSRodney W. Grimes  */
17520362d7d7SJohn Dyson static void
17530d94caffSDavid Greenman vm_map_entry_delete(map, entry)
1754c0877f10SJohn Dyson 	vm_map_t map;
1755c0877f10SJohn Dyson 	vm_map_entry_t entry;
1756df8bae1dSRodney W. Grimes {
1757df8bae1dSRodney W. Grimes 	vm_map_entry_unlink(map, entry);
1758df8bae1dSRodney W. Grimes 	map->size -= entry->end - entry->start;
1759df8bae1dSRodney W. Grimes 
17609fdfe602SMatthew Dillon 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1761df8bae1dSRodney W. Grimes 		vm_object_deallocate(entry->object.vm_object);
1762b5b40fa6SJohn Dyson 	}
1763df8bae1dSRodney W. Grimes 
1764df8bae1dSRodney W. Grimes 	vm_map_entry_dispose(map, entry);
1765df8bae1dSRodney W. Grimes }
1766df8bae1dSRodney W. Grimes 
1767df8bae1dSRodney W. Grimes /*
1768df8bae1dSRodney W. Grimes  *	vm_map_delete:	[ internal use only ]
1769df8bae1dSRodney W. Grimes  *
1770df8bae1dSRodney W. Grimes  *	Deallocates the given address range from the target
1771df8bae1dSRodney W. Grimes  *	map.
1772df8bae1dSRodney W. Grimes  */
1773df8bae1dSRodney W. Grimes int
1774df8bae1dSRodney W. Grimes vm_map_delete(map, start, end)
1775c0877f10SJohn Dyson 	vm_map_t map;
1776df8bae1dSRodney W. Grimes 	vm_offset_t start;
1777c0877f10SJohn Dyson 	vm_offset_t end;
1778df8bae1dSRodney W. Grimes {
1779cbd8ec09SJohn Dyson 	vm_object_t object;
1780c0877f10SJohn Dyson 	vm_map_entry_t entry;
1781df8bae1dSRodney W. Grimes 	vm_map_entry_t first_entry;
1782df8bae1dSRodney W. Grimes 
1783df8bae1dSRodney W. Grimes 	/*
1784df8bae1dSRodney W. Grimes 	 * Find the start of the region, and clip it
1785df8bae1dSRodney W. Grimes 	 */
1786df8bae1dSRodney W. Grimes 
1787876318ecSAlan Cox 	if (!vm_map_lookup_entry(map, start, &first_entry))
1788df8bae1dSRodney W. Grimes 		entry = first_entry->next;
1789876318ecSAlan Cox 	else {
1790df8bae1dSRodney W. Grimes 		entry = first_entry;
1791df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
1792df8bae1dSRodney W. Grimes 		/*
17930d94caffSDavid Greenman 		 * Fix the lookup hint now, rather than each time though the
17940d94caffSDavid Greenman 		 * loop.
1795df8bae1dSRodney W. Grimes 		 */
1796df8bae1dSRodney W. Grimes 		SAVE_HINT(map, entry->prev);
1797df8bae1dSRodney W. Grimes 	}
1798df8bae1dSRodney W. Grimes 
1799df8bae1dSRodney W. Grimes 	/*
1800df8bae1dSRodney W. Grimes 	 * Save the free space hint
1801df8bae1dSRodney W. Grimes 	 */
1802df8bae1dSRodney W. Grimes 
1803b18bfc3dSJohn Dyson 	if (entry == &map->header) {
1804b18bfc3dSJohn Dyson 		map->first_free = &map->header;
18052dbea5d2SJohn Dyson 	} else if (map->first_free->start >= start) {
1806df8bae1dSRodney W. Grimes 		map->first_free = entry->prev;
18072dbea5d2SJohn Dyson 	}
1808df8bae1dSRodney W. Grimes 
1809df8bae1dSRodney W. Grimes 	/*
1810df8bae1dSRodney W. Grimes 	 * Step through all entries in this region
1811df8bae1dSRodney W. Grimes 	 */
1812df8bae1dSRodney W. Grimes 
1813df8bae1dSRodney W. Grimes 	while ((entry != &map->header) && (entry->start < end)) {
1814df8bae1dSRodney W. Grimes 		vm_map_entry_t next;
1815b18bfc3dSJohn Dyson 		vm_offset_t s, e;
1816cbd8ec09SJohn Dyson 		vm_pindex_t offidxstart, offidxend, count;
1817df8bae1dSRodney W. Grimes 
1818df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, entry, end);
1819df8bae1dSRodney W. Grimes 
1820df8bae1dSRodney W. Grimes 		s = entry->start;
1821df8bae1dSRodney W. Grimes 		e = entry->end;
1822c0877f10SJohn Dyson 		next = entry->next;
1823df8bae1dSRodney W. Grimes 
1824cbd8ec09SJohn Dyson 		offidxstart = OFF_TO_IDX(entry->offset);
1825cbd8ec09SJohn Dyson 		count = OFF_TO_IDX(e - s);
1826cbd8ec09SJohn Dyson 		object = entry->object.vm_object;
18272dbea5d2SJohn Dyson 
1828df8bae1dSRodney W. Grimes 		/*
18290d94caffSDavid Greenman 		 * Unwire before removing addresses from the pmap; otherwise,
18300d94caffSDavid Greenman 		 * unwiring will put the entries back in the pmap.
1831df8bae1dSRodney W. Grimes 		 */
1832c0877f10SJohn Dyson 		if (entry->wired_count != 0) {
1833df8bae1dSRodney W. Grimes 			vm_map_entry_unwire(map, entry);
1834c0877f10SJohn Dyson 		}
1835df8bae1dSRodney W. Grimes 
1836cbd8ec09SJohn Dyson 		offidxend = offidxstart + count;
1837df8bae1dSRodney W. Grimes 
1838c0877f10SJohn Dyson 		if ((object == kernel_object) || (object == kmem_object)) {
18392dbea5d2SJohn Dyson 			vm_object_page_remove(object, offidxstart, offidxend, FALSE);
1840b18bfc3dSJohn Dyson 		} else {
1841df8bae1dSRodney W. Grimes 			pmap_remove(map->pmap, s, e);
1842876318ecSAlan Cox 			if (object != NULL &&
1843876318ecSAlan Cox 			    object->ref_count != 1 &&
1844876318ecSAlan Cox 			    (object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING &&
1845876318ecSAlan Cox 			    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
18462dbea5d2SJohn Dyson 				vm_object_collapse(object);
18472dbea5d2SJohn Dyson 				vm_object_page_remove(object, offidxstart, offidxend, FALSE);
18482dbea5d2SJohn Dyson 				if (object->type == OBJT_SWAP) {
1849cbd8ec09SJohn Dyson 					swap_pager_freespace(object, offidxstart, count);
18502dbea5d2SJohn Dyson 				}
1851876318ecSAlan Cox 				if (offidxend >= object->size &&
1852876318ecSAlan Cox 				    offidxstart < object->size) {
1853c0877f10SJohn Dyson 					object->size = offidxstart;
1854c0877f10SJohn Dyson 				}
18552dbea5d2SJohn Dyson 			}
1856b18bfc3dSJohn Dyson 		}
1857df8bae1dSRodney W. Grimes 
1858df8bae1dSRodney W. Grimes 		/*
18590d94caffSDavid Greenman 		 * Delete the entry (which may delete the object) only after
18600d94caffSDavid Greenman 		 * removing all pmap entries pointing to its pages.
18610d94caffSDavid Greenman 		 * (Otherwise, its page frames may be reallocated, and any
18620d94caffSDavid Greenman 		 * modify bits will be set in the wrong object!)
1863df8bae1dSRodney W. Grimes 		 */
1864df8bae1dSRodney W. Grimes 		vm_map_entry_delete(map, entry);
1865df8bae1dSRodney W. Grimes 		entry = next;
1866df8bae1dSRodney W. Grimes 	}
1867df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
1868df8bae1dSRodney W. Grimes }
1869df8bae1dSRodney W. Grimes 
1870df8bae1dSRodney W. Grimes /*
1871df8bae1dSRodney W. Grimes  *	vm_map_remove:
1872df8bae1dSRodney W. Grimes  *
1873df8bae1dSRodney W. Grimes  *	Remove the given address range from the target map.
1874df8bae1dSRodney W. Grimes  *	This is the exported form of vm_map_delete.
1875df8bae1dSRodney W. Grimes  */
1876df8bae1dSRodney W. Grimes int
1877df8bae1dSRodney W. Grimes vm_map_remove(map, start, end)
1878c0877f10SJohn Dyson 	vm_map_t map;
1879c0877f10SJohn Dyson 	vm_offset_t start;
1880c0877f10SJohn Dyson 	vm_offset_t end;
1881df8bae1dSRodney W. Grimes {
1882c0877f10SJohn Dyson 	int result, s = 0;
18838d6e8edeSDavid Greenman 
18849579ee64SDavid Greenman 	if (map == kmem_map || map == mb_map)
1885b18bfc3dSJohn Dyson 		s = splvm();
1886df8bae1dSRodney W. Grimes 
1887df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1888df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1889df8bae1dSRodney W. Grimes 	result = vm_map_delete(map, start, end);
1890df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1891df8bae1dSRodney W. Grimes 
18929579ee64SDavid Greenman 	if (map == kmem_map || map == mb_map)
18938d6e8edeSDavid Greenman 		splx(s);
18948d6e8edeSDavid Greenman 
1895df8bae1dSRodney W. Grimes 	return (result);
1896df8bae1dSRodney W. Grimes }
1897df8bae1dSRodney W. Grimes 
1898df8bae1dSRodney W. Grimes /*
1899df8bae1dSRodney W. Grimes  *	vm_map_check_protection:
1900df8bae1dSRodney W. Grimes  *
1901df8bae1dSRodney W. Grimes  *	Assert that the target map allows the specified
1902df8bae1dSRodney W. Grimes  *	privilege on the entire address region given.
1903df8bae1dSRodney W. Grimes  *	The entire region must be allocated.
1904df8bae1dSRodney W. Grimes  */
19050d94caffSDavid Greenman boolean_t
1906b9dcd593SBruce Evans vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
1907b9dcd593SBruce Evans 			vm_prot_t protection)
1908df8bae1dSRodney W. Grimes {
1909c0877f10SJohn Dyson 	vm_map_entry_t entry;
1910df8bae1dSRodney W. Grimes 	vm_map_entry_t tmp_entry;
1911df8bae1dSRodney W. Grimes 
1912df8bae1dSRodney W. Grimes 	if (!vm_map_lookup_entry(map, start, &tmp_entry)) {
1913df8bae1dSRodney W. Grimes 		return (FALSE);
1914df8bae1dSRodney W. Grimes 	}
1915df8bae1dSRodney W. Grimes 	entry = tmp_entry;
1916df8bae1dSRodney W. Grimes 
1917df8bae1dSRodney W. Grimes 	while (start < end) {
1918df8bae1dSRodney W. Grimes 		if (entry == &map->header) {
1919df8bae1dSRodney W. Grimes 			return (FALSE);
1920df8bae1dSRodney W. Grimes 		}
1921df8bae1dSRodney W. Grimes 		/*
1922df8bae1dSRodney W. Grimes 		 * No holes allowed!
1923df8bae1dSRodney W. Grimes 		 */
1924df8bae1dSRodney W. Grimes 
1925df8bae1dSRodney W. Grimes 		if (start < entry->start) {
1926df8bae1dSRodney W. Grimes 			return (FALSE);
1927df8bae1dSRodney W. Grimes 		}
1928df8bae1dSRodney W. Grimes 		/*
1929df8bae1dSRodney W. Grimes 		 * Check protection associated with entry.
1930df8bae1dSRodney W. Grimes 		 */
1931df8bae1dSRodney W. Grimes 
1932df8bae1dSRodney W. Grimes 		if ((entry->protection & protection) != protection) {
1933df8bae1dSRodney W. Grimes 			return (FALSE);
1934df8bae1dSRodney W. Grimes 		}
1935df8bae1dSRodney W. Grimes 		/* go to next entry */
1936df8bae1dSRodney W. Grimes 
1937df8bae1dSRodney W. Grimes 		start = entry->end;
1938df8bae1dSRodney W. Grimes 		entry = entry->next;
1939df8bae1dSRodney W. Grimes 	}
1940df8bae1dSRodney W. Grimes 	return (TRUE);
1941df8bae1dSRodney W. Grimes }
1942df8bae1dSRodney W. Grimes 
194386524867SJohn Dyson /*
194486524867SJohn Dyson  * Split the pages in a map entry into a new object.  This affords
194586524867SJohn Dyson  * easier removal of unused pages, and keeps object inheritance from
194686524867SJohn Dyson  * being a negative impact on memory usage.
194786524867SJohn Dyson  */
1948c0877f10SJohn Dyson static void
1949c0877f10SJohn Dyson vm_map_split(entry)
1950c0877f10SJohn Dyson 	vm_map_entry_t entry;
1951c0877f10SJohn Dyson {
195286524867SJohn Dyson 	vm_page_t m;
1953bd6be915SJohn Dyson 	vm_object_t orig_object, new_object, source;
1954c0877f10SJohn Dyson 	vm_offset_t s, e;
1955c0877f10SJohn Dyson 	vm_pindex_t offidxstart, offidxend, idx;
1956c0877f10SJohn Dyson 	vm_size_t size;
1957c0877f10SJohn Dyson 	vm_ooffset_t offset;
1958c0877f10SJohn Dyson 
1959c0877f10SJohn Dyson 	orig_object = entry->object.vm_object;
1960c0877f10SJohn Dyson 	if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
1961c0877f10SJohn Dyson 		return;
1962c0877f10SJohn Dyson 	if (orig_object->ref_count <= 1)
1963c0877f10SJohn Dyson 		return;
1964c0877f10SJohn Dyson 
1965c0877f10SJohn Dyson 	offset = entry->offset;
1966c0877f10SJohn Dyson 	s = entry->start;
1967c0877f10SJohn Dyson 	e = entry->end;
1968c0877f10SJohn Dyson 
1969c0877f10SJohn Dyson 	offidxstart = OFF_TO_IDX(offset);
1970c0877f10SJohn Dyson 	offidxend = offidxstart + OFF_TO_IDX(e - s);
1971c0877f10SJohn Dyson 	size = offidxend - offidxstart;
1972c0877f10SJohn Dyson 
1973c0877f10SJohn Dyson 	new_object = vm_pager_allocate(orig_object->type,
19746cde7a16SDavid Greenman 		NULL, IDX_TO_OFF(size), VM_PROT_ALL, 0LL);
1975c0877f10SJohn Dyson 	if (new_object == NULL)
1976c0877f10SJohn Dyson 		return;
1977c0877f10SJohn Dyson 
1978bd6be915SJohn Dyson 	source = orig_object->backing_object;
1979bd6be915SJohn Dyson 	if (source != NULL) {
1980bd6be915SJohn Dyson 		vm_object_reference(source);	/* Referenced by new_object */
1981bd6be915SJohn Dyson 		TAILQ_INSERT_TAIL(&source->shadow_head,
1982bd6be915SJohn Dyson 				  new_object, shadow_list);
1983069e9bc1SDoug Rabson 		vm_object_clear_flag(source, OBJ_ONEMAPPING);
1984bd6be915SJohn Dyson 		new_object->backing_object_offset =
1985a0fce827SJohn Polstra 			orig_object->backing_object_offset + IDX_TO_OFF(offidxstart);
1986bd6be915SJohn Dyson 		new_object->backing_object = source;
1987bd6be915SJohn Dyson 		source->shadow_count++;
1988bd6be915SJohn Dyson 		source->generation++;
1989bd6be915SJohn Dyson 	}
1990bd6be915SJohn Dyson 
1991c0877f10SJohn Dyson 	for (idx = 0; idx < size; idx++) {
1992c0877f10SJohn Dyson 		vm_page_t m;
1993c0877f10SJohn Dyson 
1994c0877f10SJohn Dyson 	retry:
1995c0877f10SJohn Dyson 		m = vm_page_lookup(orig_object, offidxstart + idx);
1996c0877f10SJohn Dyson 		if (m == NULL)
1997c0877f10SJohn Dyson 			continue;
19981c7c3c6aSMatthew Dillon 
19991c7c3c6aSMatthew Dillon 		/*
20001c7c3c6aSMatthew Dillon 		 * We must wait for pending I/O to complete before we can
20011c7c3c6aSMatthew Dillon 		 * rename the page.
2002d1bf5d56SMatthew Dillon 		 *
2003d1bf5d56SMatthew Dillon 		 * We do not have to VM_PROT_NONE the page as mappings should
2004d1bf5d56SMatthew Dillon 		 * not be changed by this operation.
20051c7c3c6aSMatthew Dillon 		 */
20061c7c3c6aSMatthew Dillon 		if (vm_page_sleep_busy(m, TRUE, "spltwt"))
2007c0877f10SJohn Dyson 			goto retry;
2008c0877f10SJohn Dyson 
2009e69763a3SDoug Rabson 		vm_page_busy(m);
2010c0877f10SJohn Dyson 		vm_page_rename(m, new_object, idx);
20117dbf82dcSMatthew Dillon 		/* page automatically made dirty by rename and cache handled */
2012e69763a3SDoug Rabson 		vm_page_busy(m);
2013c0877f10SJohn Dyson 	}
2014c0877f10SJohn Dyson 
2015c0877f10SJohn Dyson 	if (orig_object->type == OBJT_SWAP) {
2016d474eaaaSDoug Rabson 		vm_object_pip_add(orig_object, 1);
2017c0877f10SJohn Dyson 		/*
2018c0877f10SJohn Dyson 		 * copy orig_object pages into new_object
2019c0877f10SJohn Dyson 		 * and destroy unneeded pages in
2020c0877f10SJohn Dyson 		 * shadow object.
2021c0877f10SJohn Dyson 		 */
20221c7c3c6aSMatthew Dillon 		swap_pager_copy(orig_object, new_object, offidxstart, 0);
2023c0877f10SJohn Dyson 		vm_object_pip_wakeup(orig_object);
2024c0877f10SJohn Dyson 	}
2025c0877f10SJohn Dyson 
202686524867SJohn Dyson 	for (idx = 0; idx < size; idx++) {
202786524867SJohn Dyson 		m = vm_page_lookup(new_object, idx);
202886524867SJohn Dyson 		if (m) {
2029e69763a3SDoug Rabson 			vm_page_wakeup(m);
203086524867SJohn Dyson 		}
203186524867SJohn Dyson 	}
203286524867SJohn Dyson 
2033c0877f10SJohn Dyson 	entry->object.vm_object = new_object;
2034c0877f10SJohn Dyson 	entry->offset = 0LL;
2035c0877f10SJohn Dyson 	vm_object_deallocate(orig_object);
2036c0877f10SJohn Dyson }
2037c0877f10SJohn Dyson 
2038df8bae1dSRodney W. Grimes /*
2039df8bae1dSRodney W. Grimes  *	vm_map_copy_entry:
2040df8bae1dSRodney W. Grimes  *
2041df8bae1dSRodney W. Grimes  *	Copies the contents of the source entry to the destination
2042df8bae1dSRodney W. Grimes  *	entry.  The entries *must* be aligned properly.
2043df8bae1dSRodney W. Grimes  */
2044f708ef1bSPoul-Henning Kamp static void
20450d94caffSDavid Greenman vm_map_copy_entry(src_map, dst_map, src_entry, dst_entry)
2046df8bae1dSRodney W. Grimes 	vm_map_t src_map, dst_map;
2047c0877f10SJohn Dyson 	vm_map_entry_t src_entry, dst_entry;
2048df8bae1dSRodney W. Grimes {
2049c0877f10SJohn Dyson 	vm_object_t src_object;
2050c0877f10SJohn Dyson 
20519fdfe602SMatthew Dillon 	if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
2052df8bae1dSRodney W. Grimes 		return;
2053df8bae1dSRodney W. Grimes 
2054df8bae1dSRodney W. Grimes 	if (src_entry->wired_count == 0) {
2055df8bae1dSRodney W. Grimes 
2056df8bae1dSRodney W. Grimes 		/*
20570d94caffSDavid Greenman 		 * If the source entry is marked needs_copy, it is already
20580d94caffSDavid Greenman 		 * write-protected.
2059df8bae1dSRodney W. Grimes 		 */
2060afa07f7eSJohn Dyson 		if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
2061df8bae1dSRodney W. Grimes 			pmap_protect(src_map->pmap,
2062df8bae1dSRodney W. Grimes 			    src_entry->start,
2063df8bae1dSRodney W. Grimes 			    src_entry->end,
2064df8bae1dSRodney W. Grimes 			    src_entry->protection & ~VM_PROT_WRITE);
2065df8bae1dSRodney W. Grimes 		}
2066b18bfc3dSJohn Dyson 
2067df8bae1dSRodney W. Grimes 		/*
2068df8bae1dSRodney W. Grimes 		 * Make a copy of the object.
2069df8bae1dSRodney W. Grimes 		 */
20708aef1712SMatthew Dillon 		if ((src_object = src_entry->object.vm_object) != NULL) {
2071c0877f10SJohn Dyson 
2072c0877f10SJohn Dyson 			if ((src_object->handle == NULL) &&
2073c0877f10SJohn Dyson 				(src_object->type == OBJT_DEFAULT ||
2074c0877f10SJohn Dyson 				 src_object->type == OBJT_SWAP)) {
2075c0877f10SJohn Dyson 				vm_object_collapse(src_object);
207696fb8cf2SJohn Dyson 				if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
2077c0877f10SJohn Dyson 					vm_map_split(src_entry);
2078c0877f10SJohn Dyson 					src_object = src_entry->object.vm_object;
2079c0877f10SJohn Dyson 				}
2080c0877f10SJohn Dyson 			}
2081c0877f10SJohn Dyson 
2082c0877f10SJohn Dyson 			vm_object_reference(src_object);
2083069e9bc1SDoug Rabson 			vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
2084c0877f10SJohn Dyson 			dst_entry->object.vm_object = src_object;
2085afa07f7eSJohn Dyson 			src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2086afa07f7eSJohn Dyson 			dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2087b18bfc3dSJohn Dyson 			dst_entry->offset = src_entry->offset;
2088b18bfc3dSJohn Dyson 		} else {
2089b18bfc3dSJohn Dyson 			dst_entry->object.vm_object = NULL;
2090b18bfc3dSJohn Dyson 			dst_entry->offset = 0;
2091b18bfc3dSJohn Dyson 		}
2092df8bae1dSRodney W. Grimes 
2093df8bae1dSRodney W. Grimes 		pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
2094df8bae1dSRodney W. Grimes 		    dst_entry->end - dst_entry->start, src_entry->start);
20950d94caffSDavid Greenman 	} else {
2096df8bae1dSRodney W. Grimes 		/*
2097df8bae1dSRodney W. Grimes 		 * Of course, wired down pages can't be set copy-on-write.
20980d94caffSDavid Greenman 		 * Cause wired pages to be copied into the new map by
20990d94caffSDavid Greenman 		 * simulating faults (the new pages are pageable)
2100df8bae1dSRodney W. Grimes 		 */
2101df8bae1dSRodney W. Grimes 		vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry);
2102df8bae1dSRodney W. Grimes 	}
2103df8bae1dSRodney W. Grimes }
2104df8bae1dSRodney W. Grimes 
2105df8bae1dSRodney W. Grimes /*
2106df8bae1dSRodney W. Grimes  * vmspace_fork:
2107df8bae1dSRodney W. Grimes  * Create a new process vmspace structure and vm_map
2108df8bae1dSRodney W. Grimes  * based on those of an existing process.  The new map
2109df8bae1dSRodney W. Grimes  * is based on the old map, according to the inheritance
2110df8bae1dSRodney W. Grimes  * values on the regions in that map.
2111df8bae1dSRodney W. Grimes  *
2112df8bae1dSRodney W. Grimes  * The source map must not be locked.
2113df8bae1dSRodney W. Grimes  */
2114df8bae1dSRodney W. Grimes struct vmspace *
2115df8bae1dSRodney W. Grimes vmspace_fork(vm1)
2116c0877f10SJohn Dyson 	struct vmspace *vm1;
2117df8bae1dSRodney W. Grimes {
2118c0877f10SJohn Dyson 	struct vmspace *vm2;
2119df8bae1dSRodney W. Grimes 	vm_map_t old_map = &vm1->vm_map;
2120df8bae1dSRodney W. Grimes 	vm_map_t new_map;
2121df8bae1dSRodney W. Grimes 	vm_map_entry_t old_entry;
2122df8bae1dSRodney W. Grimes 	vm_map_entry_t new_entry;
2123de5f6a77SJohn Dyson 	vm_object_t object;
2124df8bae1dSRodney W. Grimes 
2125df8bae1dSRodney W. Grimes 	vm_map_lock(old_map);
2126df8bae1dSRodney W. Grimes 
21272d8acc0fSJohn Dyson 	vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
2128df8bae1dSRodney W. Grimes 	bcopy(&vm1->vm_startcopy, &vm2->vm_startcopy,
2129df8bae1dSRodney W. Grimes 	    (caddr_t) (vm1 + 1) - (caddr_t) &vm1->vm_startcopy);
2130df8bae1dSRodney W. Grimes 	new_map = &vm2->vm_map;	/* XXX */
213147221757SJohn Dyson 	new_map->timestamp = 1;
2132df8bae1dSRodney W. Grimes 
2133df8bae1dSRodney W. Grimes 	old_entry = old_map->header.next;
2134df8bae1dSRodney W. Grimes 
2135df8bae1dSRodney W. Grimes 	while (old_entry != &old_map->header) {
2136afa07f7eSJohn Dyson 		if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2137df8bae1dSRodney W. Grimes 			panic("vm_map_fork: encountered a submap");
2138df8bae1dSRodney W. Grimes 
2139df8bae1dSRodney W. Grimes 		switch (old_entry->inheritance) {
2140df8bae1dSRodney W. Grimes 		case VM_INHERIT_NONE:
2141df8bae1dSRodney W. Grimes 			break;
2142df8bae1dSRodney W. Grimes 
2143df8bae1dSRodney W. Grimes 		case VM_INHERIT_SHARE:
2144df8bae1dSRodney W. Grimes 			/*
2145fed9a903SJohn Dyson 			 * Clone the entry, creating the shared object if necessary.
2146fed9a903SJohn Dyson 			 */
2147fed9a903SJohn Dyson 			object = old_entry->object.vm_object;
2148fed9a903SJohn Dyson 			if (object == NULL) {
2149fed9a903SJohn Dyson 				object = vm_object_allocate(OBJT_DEFAULT,
2150c2e11a03SJohn Dyson 					atop(old_entry->end - old_entry->start));
2151fed9a903SJohn Dyson 				old_entry->object.vm_object = object;
2152fed9a903SJohn Dyson 				old_entry->offset = (vm_offset_t) 0;
21539a2f6362SAlan Cox 			}
21549a2f6362SAlan Cox 
21559a2f6362SAlan Cox 			/*
21569a2f6362SAlan Cox 			 * Add the reference before calling vm_object_shadow
21579a2f6362SAlan Cox 			 * to insure that a shadow object is created.
21589a2f6362SAlan Cox 			 */
21599a2f6362SAlan Cox 			vm_object_reference(object);
21609a2f6362SAlan Cox 			if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
21615069bf57SJohn Dyson 				vm_object_shadow(&old_entry->object.vm_object,
21625069bf57SJohn Dyson 					&old_entry->offset,
2163c2e11a03SJohn Dyson 					atop(old_entry->end - old_entry->start));
21645069bf57SJohn Dyson 				old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
2165d30344bdSIan Dowse 				/* Transfer the second reference too. */
2166d30344bdSIan Dowse 				vm_object_reference(
2167d30344bdSIan Dowse 				    old_entry->object.vm_object);
2168d30344bdSIan Dowse 				vm_object_deallocate(object);
21695069bf57SJohn Dyson 				object = old_entry->object.vm_object;
2170fed9a903SJohn Dyson 			}
2171069e9bc1SDoug Rabson 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
2172fed9a903SJohn Dyson 
2173fed9a903SJohn Dyson 			/*
2174ad5fca3bSAlan Cox 			 * Clone the entry, referencing the shared object.
2175df8bae1dSRodney W. Grimes 			 */
2176df8bae1dSRodney W. Grimes 			new_entry = vm_map_entry_create(new_map);
2177df8bae1dSRodney W. Grimes 			*new_entry = *old_entry;
2178028fe6ecSTor Egge 			new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2179df8bae1dSRodney W. Grimes 			new_entry->wired_count = 0;
2180df8bae1dSRodney W. Grimes 
2181df8bae1dSRodney W. Grimes 			/*
21820d94caffSDavid Greenman 			 * Insert the entry into the new map -- we know we're
21830d94caffSDavid Greenman 			 * inserting at the end of the new map.
2184df8bae1dSRodney W. Grimes 			 */
2185df8bae1dSRodney W. Grimes 
2186df8bae1dSRodney W. Grimes 			vm_map_entry_link(new_map, new_map->header.prev,
2187df8bae1dSRodney W. Grimes 			    new_entry);
2188df8bae1dSRodney W. Grimes 
2189df8bae1dSRodney W. Grimes 			/*
2190df8bae1dSRodney W. Grimes 			 * Update the physical map
2191df8bae1dSRodney W. Grimes 			 */
2192df8bae1dSRodney W. Grimes 
2193df8bae1dSRodney W. Grimes 			pmap_copy(new_map->pmap, old_map->pmap,
2194df8bae1dSRodney W. Grimes 			    new_entry->start,
2195df8bae1dSRodney W. Grimes 			    (old_entry->end - old_entry->start),
2196df8bae1dSRodney W. Grimes 			    old_entry->start);
2197df8bae1dSRodney W. Grimes 			break;
2198df8bae1dSRodney W. Grimes 
2199df8bae1dSRodney W. Grimes 		case VM_INHERIT_COPY:
2200df8bae1dSRodney W. Grimes 			/*
2201df8bae1dSRodney W. Grimes 			 * Clone the entry and link into the map.
2202df8bae1dSRodney W. Grimes 			 */
2203df8bae1dSRodney W. Grimes 			new_entry = vm_map_entry_create(new_map);
2204df8bae1dSRodney W. Grimes 			*new_entry = *old_entry;
2205028fe6ecSTor Egge 			new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2206df8bae1dSRodney W. Grimes 			new_entry->wired_count = 0;
2207df8bae1dSRodney W. Grimes 			new_entry->object.vm_object = NULL;
2208df8bae1dSRodney W. Grimes 			vm_map_entry_link(new_map, new_map->header.prev,
2209df8bae1dSRodney W. Grimes 			    new_entry);
2210bd7e5f99SJohn Dyson 			vm_map_copy_entry(old_map, new_map, old_entry,
2211bd7e5f99SJohn Dyson 			    new_entry);
2212df8bae1dSRodney W. Grimes 			break;
2213df8bae1dSRodney W. Grimes 		}
2214df8bae1dSRodney W. Grimes 		old_entry = old_entry->next;
2215df8bae1dSRodney W. Grimes 	}
2216df8bae1dSRodney W. Grimes 
2217df8bae1dSRodney W. Grimes 	new_map->size = old_map->size;
2218df8bae1dSRodney W. Grimes 	vm_map_unlock(old_map);
2219df8bae1dSRodney W. Grimes 
2220df8bae1dSRodney W. Grimes 	return (vm2);
2221df8bae1dSRodney W. Grimes }
2222df8bae1dSRodney W. Grimes 
222394f7e29aSAlan Cox int
222494f7e29aSAlan Cox vm_map_stack (vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
222594f7e29aSAlan Cox 	      vm_prot_t prot, vm_prot_t max, int cow)
222694f7e29aSAlan Cox {
222794f7e29aSAlan Cox 	vm_map_entry_t prev_entry;
222894f7e29aSAlan Cox 	vm_map_entry_t new_stack_entry;
222994f7e29aSAlan Cox 	vm_size_t      init_ssize;
223094f7e29aSAlan Cox 	int            rv;
223194f7e29aSAlan Cox 
223294f7e29aSAlan Cox 	if (VM_MIN_ADDRESS > 0 && addrbos < VM_MIN_ADDRESS)
223394f7e29aSAlan Cox 		return (KERN_NO_SPACE);
223494f7e29aSAlan Cox 
223594f7e29aSAlan Cox 	if (max_ssize < SGROWSIZ)
223694f7e29aSAlan Cox 		init_ssize = max_ssize;
223794f7e29aSAlan Cox 	else
223894f7e29aSAlan Cox 		init_ssize = SGROWSIZ;
223994f7e29aSAlan Cox 
224094f7e29aSAlan Cox 	vm_map_lock(map);
224194f7e29aSAlan Cox 
224294f7e29aSAlan Cox 	/* If addr is already mapped, no go */
224394f7e29aSAlan Cox 	if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
224494f7e29aSAlan Cox 		vm_map_unlock(map);
224594f7e29aSAlan Cox 		return (KERN_NO_SPACE);
224694f7e29aSAlan Cox 	}
224794f7e29aSAlan Cox 
224894f7e29aSAlan Cox 	/* If we can't accomodate max_ssize in the current mapping,
224994f7e29aSAlan Cox 	 * no go.  However, we need to be aware that subsequent user
225094f7e29aSAlan Cox 	 * mappings might map into the space we have reserved for
225194f7e29aSAlan Cox 	 * stack, and currently this space is not protected.
225294f7e29aSAlan Cox 	 *
225394f7e29aSAlan Cox 	 * Hopefully we will at least detect this condition
225494f7e29aSAlan Cox 	 * when we try to grow the stack.
225594f7e29aSAlan Cox 	 */
225694f7e29aSAlan Cox 	if ((prev_entry->next != &map->header) &&
225794f7e29aSAlan Cox 	    (prev_entry->next->start < addrbos + max_ssize)) {
225894f7e29aSAlan Cox 		vm_map_unlock(map);
225994f7e29aSAlan Cox 		return (KERN_NO_SPACE);
226094f7e29aSAlan Cox 	}
226194f7e29aSAlan Cox 
226294f7e29aSAlan Cox 	/* We initially map a stack of only init_ssize.  We will
226394f7e29aSAlan Cox 	 * grow as needed later.  Since this is to be a grow
226494f7e29aSAlan Cox 	 * down stack, we map at the top of the range.
226594f7e29aSAlan Cox 	 *
226694f7e29aSAlan Cox 	 * Note: we would normally expect prot and max to be
226794f7e29aSAlan Cox 	 * VM_PROT_ALL, and cow to be 0.  Possibly we should
226894f7e29aSAlan Cox 	 * eliminate these as input parameters, and just
226994f7e29aSAlan Cox 	 * pass these values here in the insert call.
227094f7e29aSAlan Cox 	 */
227194f7e29aSAlan Cox 	rv = vm_map_insert(map, NULL, 0, addrbos + max_ssize - init_ssize,
227294f7e29aSAlan Cox 	                   addrbos + max_ssize, prot, max, cow);
227394f7e29aSAlan Cox 
227494f7e29aSAlan Cox 	/* Now set the avail_ssize amount */
227594f7e29aSAlan Cox 	if (rv == KERN_SUCCESS){
227629b45e9eSAlan Cox 		if (prev_entry != &map->header)
227729b45e9eSAlan Cox 			vm_map_clip_end(map, prev_entry, addrbos + max_ssize - init_ssize);
227894f7e29aSAlan Cox 		new_stack_entry = prev_entry->next;
227994f7e29aSAlan Cox 		if (new_stack_entry->end   != addrbos + max_ssize ||
228094f7e29aSAlan Cox 		    new_stack_entry->start != addrbos + max_ssize - init_ssize)
228194f7e29aSAlan Cox 			panic ("Bad entry start/end for new stack entry");
228294f7e29aSAlan Cox 		else
228394f7e29aSAlan Cox 			new_stack_entry->avail_ssize = max_ssize - init_ssize;
228494f7e29aSAlan Cox 	}
228594f7e29aSAlan Cox 
228694f7e29aSAlan Cox 	vm_map_unlock(map);
228794f7e29aSAlan Cox 	return (rv);
228894f7e29aSAlan Cox }
228994f7e29aSAlan Cox 
229094f7e29aSAlan Cox /* Attempts to grow a vm stack entry.  Returns KERN_SUCCESS if the
229194f7e29aSAlan Cox  * desired address is already mapped, or if we successfully grow
229294f7e29aSAlan Cox  * the stack.  Also returns KERN_SUCCESS if addr is outside the
229394f7e29aSAlan Cox  * stack range (this is strange, but preserves compatibility with
229494f7e29aSAlan Cox  * the grow function in vm_machdep.c).
229594f7e29aSAlan Cox  */
229694f7e29aSAlan Cox int
229794f7e29aSAlan Cox vm_map_growstack (struct proc *p, vm_offset_t addr)
229894f7e29aSAlan Cox {
229994f7e29aSAlan Cox 	vm_map_entry_t prev_entry;
230094f7e29aSAlan Cox 	vm_map_entry_t stack_entry;
230194f7e29aSAlan Cox 	vm_map_entry_t new_stack_entry;
230294f7e29aSAlan Cox 	struct vmspace *vm = p->p_vmspace;
230394f7e29aSAlan Cox 	vm_map_t map = &vm->vm_map;
230494f7e29aSAlan Cox 	vm_offset_t    end;
230594f7e29aSAlan Cox 	int      grow_amount;
230694f7e29aSAlan Cox 	int      rv;
230794f7e29aSAlan Cox 	int      is_procstack;
230894f7e29aSAlan Cox Retry:
230994f7e29aSAlan Cox 	vm_map_lock_read(map);
231094f7e29aSAlan Cox 
231194f7e29aSAlan Cox 	/* If addr is already in the entry range, no need to grow.*/
231294f7e29aSAlan Cox 	if (vm_map_lookup_entry(map, addr, &prev_entry)) {
231394f7e29aSAlan Cox 		vm_map_unlock_read(map);
231494f7e29aSAlan Cox 		return (KERN_SUCCESS);
231594f7e29aSAlan Cox 	}
231694f7e29aSAlan Cox 
231794f7e29aSAlan Cox 	if ((stack_entry = prev_entry->next) == &map->header) {
231894f7e29aSAlan Cox 		vm_map_unlock_read(map);
231994f7e29aSAlan Cox 		return (KERN_SUCCESS);
232094f7e29aSAlan Cox 	}
232194f7e29aSAlan Cox 	if (prev_entry == &map->header)
232294f7e29aSAlan Cox 		end = stack_entry->start - stack_entry->avail_ssize;
232394f7e29aSAlan Cox 	else
232494f7e29aSAlan Cox 		end = prev_entry->end;
232594f7e29aSAlan Cox 
232694f7e29aSAlan Cox 	/* This next test mimics the old grow function in vm_machdep.c.
232794f7e29aSAlan Cox 	 * It really doesn't quite make sense, but we do it anyway
232894f7e29aSAlan Cox 	 * for compatibility.
232994f7e29aSAlan Cox 	 *
233094f7e29aSAlan Cox 	 * If not growable stack, return success.  This signals the
233194f7e29aSAlan Cox 	 * caller to proceed as he would normally with normal vm.
233294f7e29aSAlan Cox 	 */
233394f7e29aSAlan Cox 	if (stack_entry->avail_ssize < 1 ||
233494f7e29aSAlan Cox 	    addr >= stack_entry->start ||
233594f7e29aSAlan Cox 	    addr <  stack_entry->start - stack_entry->avail_ssize) {
233694f7e29aSAlan Cox 		vm_map_unlock_read(map);
233794f7e29aSAlan Cox 		return (KERN_SUCCESS);
233894f7e29aSAlan Cox 	}
233994f7e29aSAlan Cox 
234094f7e29aSAlan Cox 	/* Find the minimum grow amount */
234194f7e29aSAlan Cox 	grow_amount = roundup (stack_entry->start - addr, PAGE_SIZE);
234294f7e29aSAlan Cox 	if (grow_amount > stack_entry->avail_ssize) {
234394f7e29aSAlan Cox 		vm_map_unlock_read(map);
234494f7e29aSAlan Cox 		return (KERN_NO_SPACE);
234594f7e29aSAlan Cox 	}
234694f7e29aSAlan Cox 
234794f7e29aSAlan Cox 	/* If there is no longer enough space between the entries
234894f7e29aSAlan Cox 	 * nogo, and adjust the available space.  Note: this
234994f7e29aSAlan Cox 	 * should only happen if the user has mapped into the
235094f7e29aSAlan Cox 	 * stack area after the stack was created, and is
235194f7e29aSAlan Cox 	 * probably an error.
235294f7e29aSAlan Cox 	 *
235394f7e29aSAlan Cox 	 * This also effectively destroys any guard page the user
235494f7e29aSAlan Cox 	 * might have intended by limiting the stack size.
235594f7e29aSAlan Cox 	 */
235694f7e29aSAlan Cox 	if (grow_amount > stack_entry->start - end) {
235794f7e29aSAlan Cox 		if (vm_map_lock_upgrade(map))
235894f7e29aSAlan Cox 			goto Retry;
235994f7e29aSAlan Cox 
236094f7e29aSAlan Cox 		stack_entry->avail_ssize = stack_entry->start - end;
236194f7e29aSAlan Cox 
236294f7e29aSAlan Cox 		vm_map_unlock(map);
236394f7e29aSAlan Cox 		return (KERN_NO_SPACE);
236494f7e29aSAlan Cox 	}
236594f7e29aSAlan Cox 
236694f7e29aSAlan Cox 	is_procstack = addr >= (vm_offset_t)vm->vm_maxsaddr;
236794f7e29aSAlan Cox 
236894f7e29aSAlan Cox 	/* If this is the main process stack, see if we're over the
236994f7e29aSAlan Cox 	 * stack limit.
237094f7e29aSAlan Cox 	 */
23716389da78SAlan Cox 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount >
237294f7e29aSAlan Cox 			     p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
237394f7e29aSAlan Cox 		vm_map_unlock_read(map);
237494f7e29aSAlan Cox 		return (KERN_NO_SPACE);
237594f7e29aSAlan Cox 	}
237694f7e29aSAlan Cox 
237794f7e29aSAlan Cox 	/* Round up the grow amount modulo SGROWSIZ */
237894f7e29aSAlan Cox 	grow_amount = roundup (grow_amount, SGROWSIZ);
237994f7e29aSAlan Cox 	if (grow_amount > stack_entry->avail_ssize) {
238094f7e29aSAlan Cox 		grow_amount = stack_entry->avail_ssize;
238194f7e29aSAlan Cox 	}
23826389da78SAlan Cox 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount >
238394f7e29aSAlan Cox 	                     p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
238494f7e29aSAlan Cox 		grow_amount = p->p_rlimit[RLIMIT_STACK].rlim_cur -
23856389da78SAlan Cox 		              ctob(vm->vm_ssize);
238694f7e29aSAlan Cox 	}
238794f7e29aSAlan Cox 
238894f7e29aSAlan Cox 	if (vm_map_lock_upgrade(map))
238994f7e29aSAlan Cox 		goto Retry;
239094f7e29aSAlan Cox 
239194f7e29aSAlan Cox 	/* Get the preliminary new entry start value */
239294f7e29aSAlan Cox 	addr = stack_entry->start - grow_amount;
239394f7e29aSAlan Cox 
239494f7e29aSAlan Cox 	/* If this puts us into the previous entry, cut back our growth
239594f7e29aSAlan Cox 	 * to the available space.  Also, see the note above.
239694f7e29aSAlan Cox 	 */
239794f7e29aSAlan Cox 	if (addr < end) {
239894f7e29aSAlan Cox 		stack_entry->avail_ssize = stack_entry->start - end;
239994f7e29aSAlan Cox 		addr = end;
240094f7e29aSAlan Cox 	}
240194f7e29aSAlan Cox 
240294f7e29aSAlan Cox 	rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start,
240329b45e9eSAlan Cox 			   VM_PROT_ALL,
240429b45e9eSAlan Cox 			   VM_PROT_ALL,
240594f7e29aSAlan Cox 			   0);
240694f7e29aSAlan Cox 
240794f7e29aSAlan Cox 	/* Adjust the available stack space by the amount we grew. */
240894f7e29aSAlan Cox 	if (rv == KERN_SUCCESS) {
240929b45e9eSAlan Cox 		if (prev_entry != &map->header)
241029b45e9eSAlan Cox 			vm_map_clip_end(map, prev_entry, addr);
241194f7e29aSAlan Cox 		new_stack_entry = prev_entry->next;
241294f7e29aSAlan Cox 		if (new_stack_entry->end   != stack_entry->start  ||
241394f7e29aSAlan Cox 		    new_stack_entry->start != addr)
241494f7e29aSAlan Cox 			panic ("Bad stack grow start/end in new stack entry");
241594f7e29aSAlan Cox 		else {
241694f7e29aSAlan Cox 			new_stack_entry->avail_ssize = stack_entry->avail_ssize -
241794f7e29aSAlan Cox 							(new_stack_entry->end -
241894f7e29aSAlan Cox 							 new_stack_entry->start);
241994f7e29aSAlan Cox 			if (is_procstack)
24206389da78SAlan Cox 				vm->vm_ssize += btoc(new_stack_entry->end -
24216389da78SAlan Cox 						     new_stack_entry->start);
242294f7e29aSAlan Cox 		}
242394f7e29aSAlan Cox 	}
242494f7e29aSAlan Cox 
242594f7e29aSAlan Cox 	vm_map_unlock(map);
242694f7e29aSAlan Cox 	return (rv);
242794f7e29aSAlan Cox 
242894f7e29aSAlan Cox }
242994f7e29aSAlan Cox 
2430df8bae1dSRodney W. Grimes /*
24315856e12eSJohn Dyson  * Unshare the specified VM space for exec.  If other processes are
24325856e12eSJohn Dyson  * mapped to it, then create a new one.  The new vmspace is null.
24335856e12eSJohn Dyson  */
24345856e12eSJohn Dyson 
24355856e12eSJohn Dyson void
24365856e12eSJohn Dyson vmspace_exec(struct proc *p) {
24375856e12eSJohn Dyson 	struct vmspace *oldvmspace = p->p_vmspace;
24385856e12eSJohn Dyson 	struct vmspace *newvmspace;
24395856e12eSJohn Dyson 	vm_map_t map = &p->p_vmspace->vm_map;
24405856e12eSJohn Dyson 
24412d8acc0fSJohn Dyson 	newvmspace = vmspace_alloc(map->min_offset, map->max_offset);
24425856e12eSJohn Dyson 	bcopy(&oldvmspace->vm_startcopy, &newvmspace->vm_startcopy,
24435856e12eSJohn Dyson 	    (caddr_t) (newvmspace + 1) - (caddr_t) &newvmspace->vm_startcopy);
24445856e12eSJohn Dyson 	/*
24455856e12eSJohn Dyson 	 * This code is written like this for prototype purposes.  The
24465856e12eSJohn Dyson 	 * goal is to avoid running down the vmspace here, but let the
24475856e12eSJohn Dyson 	 * other process's that are still using the vmspace to finally
24485856e12eSJohn Dyson 	 * run it down.  Even though there is little or no chance of blocking
24495856e12eSJohn Dyson 	 * here, it is a good idea to keep this form for future mods.
24505856e12eSJohn Dyson 	 */
24515856e12eSJohn Dyson 	vmspace_free(oldvmspace);
24525856e12eSJohn Dyson 	p->p_vmspace = newvmspace;
2453d4da2dbaSAlan Cox 	pmap_pinit2(vmspace_pmap(newvmspace));
24545856e12eSJohn Dyson 	if (p == curproc)
24555856e12eSJohn Dyson 		pmap_activate(p);
24565856e12eSJohn Dyson }
24575856e12eSJohn Dyson 
24585856e12eSJohn Dyson /*
24595856e12eSJohn Dyson  * Unshare the specified VM space for forcing COW.  This
24605856e12eSJohn Dyson  * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
24615856e12eSJohn Dyson  */
24625856e12eSJohn Dyson 
24635856e12eSJohn Dyson void
24645856e12eSJohn Dyson vmspace_unshare(struct proc *p) {
24655856e12eSJohn Dyson 	struct vmspace *oldvmspace = p->p_vmspace;
24665856e12eSJohn Dyson 	struct vmspace *newvmspace;
24675856e12eSJohn Dyson 
24685856e12eSJohn Dyson 	if (oldvmspace->vm_refcnt == 1)
24695856e12eSJohn Dyson 		return;
24705856e12eSJohn Dyson 	newvmspace = vmspace_fork(oldvmspace);
24715856e12eSJohn Dyson 	vmspace_free(oldvmspace);
24725856e12eSJohn Dyson 	p->p_vmspace = newvmspace;
2473d4da2dbaSAlan Cox 	pmap_pinit2(vmspace_pmap(newvmspace));
24745856e12eSJohn Dyson 	if (p == curproc)
24755856e12eSJohn Dyson 		pmap_activate(p);
24765856e12eSJohn Dyson }
24775856e12eSJohn Dyson 
24785856e12eSJohn Dyson 
24795856e12eSJohn Dyson /*
2480df8bae1dSRodney W. Grimes  *	vm_map_lookup:
2481df8bae1dSRodney W. Grimes  *
2482df8bae1dSRodney W. Grimes  *	Finds the VM object, offset, and
2483df8bae1dSRodney W. Grimes  *	protection for a given virtual address in the
2484df8bae1dSRodney W. Grimes  *	specified map, assuming a page fault of the
2485df8bae1dSRodney W. Grimes  *	type specified.
2486df8bae1dSRodney W. Grimes  *
2487df8bae1dSRodney W. Grimes  *	Leaves the map in question locked for read; return
2488df8bae1dSRodney W. Grimes  *	values are guaranteed until a vm_map_lookup_done
2489df8bae1dSRodney W. Grimes  *	call is performed.  Note that the map argument
2490df8bae1dSRodney W. Grimes  *	is in/out; the returned map must be used in
2491df8bae1dSRodney W. Grimes  *	the call to vm_map_lookup_done.
2492df8bae1dSRodney W. Grimes  *
2493df8bae1dSRodney W. Grimes  *	A handle (out_entry) is returned for use in
2494df8bae1dSRodney W. Grimes  *	vm_map_lookup_done, to make that fast.
2495df8bae1dSRodney W. Grimes  *
2496df8bae1dSRodney W. Grimes  *	If a lookup is requested with "write protection"
2497df8bae1dSRodney W. Grimes  *	specified, the map may be changed to perform virtual
2498df8bae1dSRodney W. Grimes  *	copying operations, although the data referenced will
2499df8bae1dSRodney W. Grimes  *	remain the same.
2500df8bae1dSRodney W. Grimes  */
2501df8bae1dSRodney W. Grimes int
2502b9dcd593SBruce Evans vm_map_lookup(vm_map_t *var_map,		/* IN/OUT */
2503b9dcd593SBruce Evans 	      vm_offset_t vaddr,
250447221757SJohn Dyson 	      vm_prot_t fault_typea,
2505b9dcd593SBruce Evans 	      vm_map_entry_t *out_entry,	/* OUT */
2506b9dcd593SBruce Evans 	      vm_object_t *object,		/* OUT */
2507b9dcd593SBruce Evans 	      vm_pindex_t *pindex,		/* OUT */
2508b9dcd593SBruce Evans 	      vm_prot_t *out_prot,		/* OUT */
25092d8acc0fSJohn Dyson 	      boolean_t *wired)			/* OUT */
2510df8bae1dSRodney W. Grimes {
2511c0877f10SJohn Dyson 	vm_map_entry_t entry;
2512c0877f10SJohn Dyson 	vm_map_t map = *var_map;
2513c0877f10SJohn Dyson 	vm_prot_t prot;
251447221757SJohn Dyson 	vm_prot_t fault_type = fault_typea;
2515df8bae1dSRodney W. Grimes 
2516df8bae1dSRodney W. Grimes RetryLookup:;
2517df8bae1dSRodney W. Grimes 
2518df8bae1dSRodney W. Grimes 	/*
2519df8bae1dSRodney W. Grimes 	 * Lookup the faulting address.
2520df8bae1dSRodney W. Grimes 	 */
2521df8bae1dSRodney W. Grimes 
2522df8bae1dSRodney W. Grimes 	vm_map_lock_read(map);
2523df8bae1dSRodney W. Grimes 
2524df8bae1dSRodney W. Grimes #define	RETURN(why) \
2525df8bae1dSRodney W. Grimes 		{ \
2526df8bae1dSRodney W. Grimes 		vm_map_unlock_read(map); \
2527df8bae1dSRodney W. Grimes 		return(why); \
2528df8bae1dSRodney W. Grimes 		}
2529df8bae1dSRodney W. Grimes 
2530df8bae1dSRodney W. Grimes 	/*
25310d94caffSDavid Greenman 	 * If the map has an interesting hint, try it before calling full
25320d94caffSDavid Greenman 	 * blown lookup routine.
2533df8bae1dSRodney W. Grimes 	 */
2534df8bae1dSRodney W. Grimes 
2535df8bae1dSRodney W. Grimes 	entry = map->hint;
2536df8bae1dSRodney W. Grimes 
2537df8bae1dSRodney W. Grimes 	*out_entry = entry;
2538df8bae1dSRodney W. Grimes 
2539df8bae1dSRodney W. Grimes 	if ((entry == &map->header) ||
2540df8bae1dSRodney W. Grimes 	    (vaddr < entry->start) || (vaddr >= entry->end)) {
2541df8bae1dSRodney W. Grimes 		vm_map_entry_t tmp_entry;
2542df8bae1dSRodney W. Grimes 
2543df8bae1dSRodney W. Grimes 		/*
25440d94caffSDavid Greenman 		 * Entry was either not a valid hint, or the vaddr was not
25450d94caffSDavid Greenman 		 * contained in the entry, so do a full lookup.
2546df8bae1dSRodney W. Grimes 		 */
2547df8bae1dSRodney W. Grimes 		if (!vm_map_lookup_entry(map, vaddr, &tmp_entry))
2548df8bae1dSRodney W. Grimes 			RETURN(KERN_INVALID_ADDRESS);
2549df8bae1dSRodney W. Grimes 
2550df8bae1dSRodney W. Grimes 		entry = tmp_entry;
2551df8bae1dSRodney W. Grimes 		*out_entry = entry;
2552df8bae1dSRodney W. Grimes 	}
2553b7b2aac2SJohn Dyson 
2554df8bae1dSRodney W. Grimes 	/*
2555df8bae1dSRodney W. Grimes 	 * Handle submaps.
2556df8bae1dSRodney W. Grimes 	 */
2557df8bae1dSRodney W. Grimes 
2558afa07f7eSJohn Dyson 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
2559df8bae1dSRodney W. Grimes 		vm_map_t old_map = map;
2560df8bae1dSRodney W. Grimes 
2561df8bae1dSRodney W. Grimes 		*var_map = map = entry->object.sub_map;
2562df8bae1dSRodney W. Grimes 		vm_map_unlock_read(old_map);
2563df8bae1dSRodney W. Grimes 		goto RetryLookup;
2564df8bae1dSRodney W. Grimes 	}
2565a04c970aSJohn Dyson 
2566df8bae1dSRodney W. Grimes 	/*
25670d94caffSDavid Greenman 	 * Check whether this task is allowed to have this page.
2568a04c970aSJohn Dyson 	 * Note the special case for MAP_ENTRY_COW
2569a04c970aSJohn Dyson 	 * pages with an override.  This is to implement a forced
2570a04c970aSJohn Dyson 	 * COW for debuggers.
2571df8bae1dSRodney W. Grimes 	 */
2572df8bae1dSRodney W. Grimes 
2573480ba2f5SJohn Dyson 	if (fault_type & VM_PROT_OVERRIDE_WRITE)
2574480ba2f5SJohn Dyson 		prot = entry->max_protection;
2575480ba2f5SJohn Dyson 	else
2576df8bae1dSRodney W. Grimes 		prot = entry->protection;
257747221757SJohn Dyson 
257847221757SJohn Dyson 	fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
257947221757SJohn Dyson 	if ((fault_type & prot) != fault_type) {
258047221757SJohn Dyson 			RETURN(KERN_PROTECTION_FAILURE);
258147221757SJohn Dyson 	}
258247221757SJohn Dyson 
25832ed14a92SAlan Cox 	if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
258447221757SJohn Dyson 	    (entry->eflags & MAP_ENTRY_COW) &&
25852ed14a92SAlan Cox 	    (fault_type & VM_PROT_WRITE) &&
258647221757SJohn Dyson 	    (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
2587df8bae1dSRodney W. Grimes 		RETURN(KERN_PROTECTION_FAILURE);
2588a04c970aSJohn Dyson 	}
2589df8bae1dSRodney W. Grimes 
2590df8bae1dSRodney W. Grimes 	/*
25910d94caffSDavid Greenman 	 * If this page is not pageable, we have to get it for all possible
25920d94caffSDavid Greenman 	 * accesses.
2593df8bae1dSRodney W. Grimes 	 */
2594df8bae1dSRodney W. Grimes 
259505f0fdd2SPoul-Henning Kamp 	*wired = (entry->wired_count != 0);
259605f0fdd2SPoul-Henning Kamp 	if (*wired)
2597df8bae1dSRodney W. Grimes 		prot = fault_type = entry->protection;
2598df8bae1dSRodney W. Grimes 
2599df8bae1dSRodney W. Grimes 	/*
2600df8bae1dSRodney W. Grimes 	 * If the entry was copy-on-write, we either ...
2601df8bae1dSRodney W. Grimes 	 */
2602df8bae1dSRodney W. Grimes 
2603afa07f7eSJohn Dyson 	if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
2604df8bae1dSRodney W. Grimes 		/*
26050d94caffSDavid Greenman 		 * If we want to write the page, we may as well handle that
2606ad5fca3bSAlan Cox 		 * now since we've got the map locked.
2607df8bae1dSRodney W. Grimes 		 *
26080d94caffSDavid Greenman 		 * If we don't need to write the page, we just demote the
26090d94caffSDavid Greenman 		 * permissions allowed.
2610df8bae1dSRodney W. Grimes 		 */
2611df8bae1dSRodney W. Grimes 
2612df8bae1dSRodney W. Grimes 		if (fault_type & VM_PROT_WRITE) {
2613df8bae1dSRodney W. Grimes 			/*
26140d94caffSDavid Greenman 			 * Make a new object, and place it in the object
26150d94caffSDavid Greenman 			 * chain.  Note that no new references have appeared
2616ad5fca3bSAlan Cox 			 * -- one just moved from the map to the new
26170d94caffSDavid Greenman 			 * object.
2618df8bae1dSRodney W. Grimes 			 */
2619df8bae1dSRodney W. Grimes 
26209b09b6c7SMatthew Dillon 			if (vm_map_lock_upgrade(map))
2621df8bae1dSRodney W. Grimes 				goto RetryLookup;
26229b09b6c7SMatthew Dillon 
2623df8bae1dSRodney W. Grimes 			vm_object_shadow(
2624df8bae1dSRodney W. Grimes 			    &entry->object.vm_object,
2625df8bae1dSRodney W. Grimes 			    &entry->offset,
2626c2e11a03SJohn Dyson 			    atop(entry->end - entry->start));
2627df8bae1dSRodney W. Grimes 
2628afa07f7eSJohn Dyson 			entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
26299b09b6c7SMatthew Dillon 			vm_map_lock_downgrade(map);
26300d94caffSDavid Greenman 		} else {
2631df8bae1dSRodney W. Grimes 			/*
26320d94caffSDavid Greenman 			 * We're attempting to read a copy-on-write page --
26330d94caffSDavid Greenman 			 * don't allow writes.
2634df8bae1dSRodney W. Grimes 			 */
2635df8bae1dSRodney W. Grimes 
26362d8acc0fSJohn Dyson 			prot &= ~VM_PROT_WRITE;
2637df8bae1dSRodney W. Grimes 		}
2638df8bae1dSRodney W. Grimes 	}
26392d8acc0fSJohn Dyson 
2640df8bae1dSRodney W. Grimes 	/*
2641df8bae1dSRodney W. Grimes 	 * Create an object if necessary.
2642df8bae1dSRodney W. Grimes 	 */
26434e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL &&
26444e71e795SMatthew Dillon 	    !map->system_map) {
26459b09b6c7SMatthew Dillon 		if (vm_map_lock_upgrade(map))
2646df8bae1dSRodney W. Grimes 			goto RetryLookup;
26479b09b6c7SMatthew Dillon 
264824a1cce3SDavid Greenman 		entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
2649c2e11a03SJohn Dyson 		    atop(entry->end - entry->start));
2650df8bae1dSRodney W. Grimes 		entry->offset = 0;
26519b09b6c7SMatthew Dillon 		vm_map_lock_downgrade(map);
2652df8bae1dSRodney W. Grimes 	}
2653b5b40fa6SJohn Dyson 
2654df8bae1dSRodney W. Grimes 	/*
26550d94caffSDavid Greenman 	 * Return the object/offset from this entry.  If the entry was
26560d94caffSDavid Greenman 	 * copy-on-write or empty, it has been fixed up.
2657df8bae1dSRodney W. Grimes 	 */
2658df8bae1dSRodney W. Grimes 
26599b09b6c7SMatthew Dillon 	*pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
2660df8bae1dSRodney W. Grimes 	*object = entry->object.vm_object;
2661df8bae1dSRodney W. Grimes 
2662df8bae1dSRodney W. Grimes 	/*
2663df8bae1dSRodney W. Grimes 	 * Return whether this is the only map sharing this data.
2664df8bae1dSRodney W. Grimes 	 */
2665df8bae1dSRodney W. Grimes 
2666df8bae1dSRodney W. Grimes 	*out_prot = prot;
2667df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
2668df8bae1dSRodney W. Grimes 
2669df8bae1dSRodney W. Grimes #undef	RETURN
2670df8bae1dSRodney W. Grimes }
2671df8bae1dSRodney W. Grimes 
2672df8bae1dSRodney W. Grimes /*
2673df8bae1dSRodney W. Grimes  *	vm_map_lookup_done:
2674df8bae1dSRodney W. Grimes  *
2675df8bae1dSRodney W. Grimes  *	Releases locks acquired by a vm_map_lookup
2676df8bae1dSRodney W. Grimes  *	(according to the handle returned by that lookup).
2677df8bae1dSRodney W. Grimes  */
2678df8bae1dSRodney W. Grimes 
26790d94caffSDavid Greenman void
26800d94caffSDavid Greenman vm_map_lookup_done(map, entry)
2681c0877f10SJohn Dyson 	vm_map_t map;
2682df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
2683df8bae1dSRodney W. Grimes {
2684df8bae1dSRodney W. Grimes 	/*
2685df8bae1dSRodney W. Grimes 	 * Unlock the main-level map
2686df8bae1dSRodney W. Grimes 	 */
2687df8bae1dSRodney W. Grimes 
2688df8bae1dSRodney W. Grimes 	vm_map_unlock_read(map);
2689df8bae1dSRodney W. Grimes }
2690df8bae1dSRodney W. Grimes 
26911efb74fbSJohn Dyson /*
26921efb74fbSJohn Dyson  * Implement uiomove with VM operations.  This handles (and collateral changes)
26931efb74fbSJohn Dyson  * support every combination of source object modification, and COW type
26941efb74fbSJohn Dyson  * operations.
26951efb74fbSJohn Dyson  */
26961efb74fbSJohn Dyson int
269747221757SJohn Dyson vm_uiomove(mapa, srcobject, cp, cnta, uaddra, npages)
26981efb74fbSJohn Dyson 	vm_map_t mapa;
26991efb74fbSJohn Dyson 	vm_object_t srcobject;
27001efb74fbSJohn Dyson 	off_t cp;
270147221757SJohn Dyson 	int cnta;
27021efb74fbSJohn Dyson 	vm_offset_t uaddra;
270395e5e988SJohn Dyson 	int *npages;
27041efb74fbSJohn Dyson {
27051efb74fbSJohn Dyson 	vm_map_t map;
270647221757SJohn Dyson 	vm_object_t first_object, oldobject, object;
27072d8acc0fSJohn Dyson 	vm_map_entry_t entry;
27081efb74fbSJohn Dyson 	vm_prot_t prot;
27092d8acc0fSJohn Dyson 	boolean_t wired;
27101efb74fbSJohn Dyson 	int tcnt, rv;
27112d8acc0fSJohn Dyson 	vm_offset_t uaddr, start, end, tend;
27121efb74fbSJohn Dyson 	vm_pindex_t first_pindex, osize, oindex;
27131efb74fbSJohn Dyson 	off_t ooffset;
271447221757SJohn Dyson 	int cnt;
27151efb74fbSJohn Dyson 
271695e5e988SJohn Dyson 	if (npages)
271795e5e988SJohn Dyson 		*npages = 0;
271895e5e988SJohn Dyson 
271947221757SJohn Dyson 	cnt = cnta;
27202d8acc0fSJohn Dyson 	uaddr = uaddra;
27212d8acc0fSJohn Dyson 
27221efb74fbSJohn Dyson 	while (cnt > 0) {
27231efb74fbSJohn Dyson 		map = mapa;
27241efb74fbSJohn Dyson 
27251efb74fbSJohn Dyson 		if ((vm_map_lookup(&map, uaddr,
27262d8acc0fSJohn Dyson 			VM_PROT_READ, &entry, &first_object,
27272d8acc0fSJohn Dyson 			&first_pindex, &prot, &wired)) != KERN_SUCCESS) {
27281efb74fbSJohn Dyson 			return EFAULT;
27291efb74fbSJohn Dyson 		}
27301efb74fbSJohn Dyson 
27312d8acc0fSJohn Dyson 		vm_map_clip_start(map, entry, uaddr);
27321efb74fbSJohn Dyson 
27331efb74fbSJohn Dyson 		tcnt = cnt;
27342d8acc0fSJohn Dyson 		tend = uaddr + tcnt;
27352d8acc0fSJohn Dyson 		if (tend > entry->end) {
27362d8acc0fSJohn Dyson 			tcnt = entry->end - uaddr;
27372d8acc0fSJohn Dyson 			tend = entry->end;
27382d8acc0fSJohn Dyson 		}
27391efb74fbSJohn Dyson 
27402d8acc0fSJohn Dyson 		vm_map_clip_end(map, entry, tend);
27411efb74fbSJohn Dyson 
27422d8acc0fSJohn Dyson 		start = entry->start;
27432d8acc0fSJohn Dyson 		end = entry->end;
27441efb74fbSJohn Dyson 
2745c2e11a03SJohn Dyson 		osize = atop(tcnt);
274695e5e988SJohn Dyson 
2747925a3a41SJohn Dyson 		oindex = OFF_TO_IDX(cp);
274895e5e988SJohn Dyson 		if (npages) {
2749925a3a41SJohn Dyson 			vm_pindex_t idx;
275095e5e988SJohn Dyson 			for (idx = 0; idx < osize; idx++) {
275195e5e988SJohn Dyson 				vm_page_t m;
2752925a3a41SJohn Dyson 				if ((m = vm_page_lookup(srcobject, oindex + idx)) == NULL) {
27532d8acc0fSJohn Dyson 					vm_map_lookup_done(map, entry);
275495e5e988SJohn Dyson 					return 0;
275595e5e988SJohn Dyson 				}
27561c7c3c6aSMatthew Dillon 				/*
27571c7c3c6aSMatthew Dillon 				 * disallow busy or invalid pages, but allow
27581c7c3c6aSMatthew Dillon 				 * m->busy pages if they are entirely valid.
27591c7c3c6aSMatthew Dillon 				 */
2760925a3a41SJohn Dyson 				if ((m->flags & PG_BUSY) ||
276195e5e988SJohn Dyson 					((m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL)) {
27622d8acc0fSJohn Dyson 					vm_map_lookup_done(map, entry);
276395e5e988SJohn Dyson 					return 0;
276495e5e988SJohn Dyson 				}
276595e5e988SJohn Dyson 			}
276695e5e988SJohn Dyson 		}
276795e5e988SJohn Dyson 
27681efb74fbSJohn Dyson /*
27691efb74fbSJohn Dyson  * If we are changing an existing map entry, just redirect
27701efb74fbSJohn Dyson  * the object, and change mappings.
27711efb74fbSJohn Dyson  */
27722d8acc0fSJohn Dyson 		if ((first_object->type == OBJT_VNODE) &&
27732d8acc0fSJohn Dyson 			((oldobject = entry->object.vm_object) == first_object)) {
27742d8acc0fSJohn Dyson 
27752d8acc0fSJohn Dyson 			if ((entry->offset != cp) || (oldobject != srcobject)) {
27762d8acc0fSJohn Dyson 				/*
27772d8acc0fSJohn Dyson    				* Remove old window into the file
27782d8acc0fSJohn Dyson    				*/
27792d8acc0fSJohn Dyson 				pmap_remove (map->pmap, uaddr, tend);
27802d8acc0fSJohn Dyson 
27812d8acc0fSJohn Dyson 				/*
27822d8acc0fSJohn Dyson    				* Force copy on write for mmaped regions
27832d8acc0fSJohn Dyson    				*/
27842d8acc0fSJohn Dyson 				vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
27852d8acc0fSJohn Dyson 
27862d8acc0fSJohn Dyson 				/*
27872d8acc0fSJohn Dyson    				* Point the object appropriately
27882d8acc0fSJohn Dyson    				*/
27892d8acc0fSJohn Dyson 				if (oldobject != srcobject) {
27902d8acc0fSJohn Dyson 
27912d8acc0fSJohn Dyson 				/*
27922d8acc0fSJohn Dyson    				* Set the object optimization hint flag
27932d8acc0fSJohn Dyson    				*/
2794069e9bc1SDoug Rabson 					vm_object_set_flag(srcobject, OBJ_OPT);
27952d8acc0fSJohn Dyson 					vm_object_reference(srcobject);
27962d8acc0fSJohn Dyson 					entry->object.vm_object = srcobject;
27972d8acc0fSJohn Dyson 
27982d8acc0fSJohn Dyson 					if (oldobject) {
27992d8acc0fSJohn Dyson 						vm_object_deallocate(oldobject);
28002d8acc0fSJohn Dyson 					}
28012d8acc0fSJohn Dyson 				}
28022d8acc0fSJohn Dyson 
28032d8acc0fSJohn Dyson 				entry->offset = cp;
28042d8acc0fSJohn Dyson 				map->timestamp++;
28052d8acc0fSJohn Dyson 			} else {
28062d8acc0fSJohn Dyson 				pmap_remove (map->pmap, uaddr, tend);
28072d8acc0fSJohn Dyson 			}
28082d8acc0fSJohn Dyson 
28092d8acc0fSJohn Dyson 		} else if ((first_object->ref_count == 1) &&
2810925a3a41SJohn Dyson 			(first_object->size == osize) &&
281147221757SJohn Dyson 			((first_object->type == OBJT_DEFAULT) ||
281247221757SJohn Dyson 				(first_object->type == OBJT_SWAP)) ) {
2813925a3a41SJohn Dyson 
2814925a3a41SJohn Dyson 			oldobject = first_object->backing_object;
2815925a3a41SJohn Dyson 
2816925a3a41SJohn Dyson 			if ((first_object->backing_object_offset != cp) ||
2817925a3a41SJohn Dyson 				(oldobject != srcobject)) {
2818925a3a41SJohn Dyson 				/*
2819925a3a41SJohn Dyson    				* Remove old window into the file
2820925a3a41SJohn Dyson    				*/
28212d8acc0fSJohn Dyson 				pmap_remove (map->pmap, uaddr, tend);
2822925a3a41SJohn Dyson 
2823925a3a41SJohn Dyson 				/*
282447221757SJohn Dyson 				 * Remove unneeded old pages
282547221757SJohn Dyson 				 */
282647221757SJohn Dyson 				vm_object_page_remove(first_object, 0, 0, 0);
282747221757SJohn Dyson 
282847221757SJohn Dyson 				/*
282947221757SJohn Dyson 				 * Invalidate swap space
283047221757SJohn Dyson 				 */
283147221757SJohn Dyson 				if (first_object->type == OBJT_SWAP) {
283247221757SJohn Dyson 					swap_pager_freespace(first_object,
28331c7c3c6aSMatthew Dillon 						0,
283447221757SJohn Dyson 						first_object->size);
283547221757SJohn Dyson 				}
283647221757SJohn Dyson 
283747221757SJohn Dyson 				/*
2838925a3a41SJohn Dyson    				* Force copy on write for mmaped regions
2839925a3a41SJohn Dyson    				*/
284047221757SJohn Dyson 				vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
28411efb74fbSJohn Dyson 
28421efb74fbSJohn Dyson 				/*
28431efb74fbSJohn Dyson    				* Point the object appropriately
28441efb74fbSJohn Dyson    				*/
2845925a3a41SJohn Dyson 				if (oldobject != srcobject) {
284647221757SJohn Dyson 
2847925a3a41SJohn Dyson 				/*
2848925a3a41SJohn Dyson    				* Set the object optimization hint flag
2849925a3a41SJohn Dyson    				*/
2850069e9bc1SDoug Rabson 					vm_object_set_flag(srcobject, OBJ_OPT);
2851925a3a41SJohn Dyson 					vm_object_reference(srcobject);
2852925a3a41SJohn Dyson 
2853925a3a41SJohn Dyson 					if (oldobject) {
2854925a3a41SJohn Dyson 						TAILQ_REMOVE(&oldobject->shadow_head,
2855925a3a41SJohn Dyson 							first_object, shadow_list);
2856925a3a41SJohn Dyson 						oldobject->shadow_count--;
2857b4309055SMatthew Dillon 						/* XXX bump generation? */
2858925a3a41SJohn Dyson 						vm_object_deallocate(oldobject);
2859925a3a41SJohn Dyson 					}
2860925a3a41SJohn Dyson 
2861925a3a41SJohn Dyson 					TAILQ_INSERT_TAIL(&srcobject->shadow_head,
2862925a3a41SJohn Dyson 						first_object, shadow_list);
2863925a3a41SJohn Dyson 					srcobject->shadow_count++;
2864b4309055SMatthew Dillon 					/* XXX bump generation? */
2865925a3a41SJohn Dyson 
2866925a3a41SJohn Dyson 					first_object->backing_object = srcobject;
2867925a3a41SJohn Dyson 				}
28681efb74fbSJohn Dyson 				first_object->backing_object_offset = cp;
28692d8acc0fSJohn Dyson 				map->timestamp++;
2870925a3a41SJohn Dyson 			} else {
28712d8acc0fSJohn Dyson 				pmap_remove (map->pmap, uaddr, tend);
2872925a3a41SJohn Dyson 			}
28731efb74fbSJohn Dyson /*
28741efb74fbSJohn Dyson  * Otherwise, we have to do a logical mmap.
28751efb74fbSJohn Dyson  */
28761efb74fbSJohn Dyson 		} else {
28771efb74fbSJohn Dyson 
2878069e9bc1SDoug Rabson 			vm_object_set_flag(srcobject, OBJ_OPT);
2879925a3a41SJohn Dyson 			vm_object_reference(srcobject);
28801efb74fbSJohn Dyson 
28812d8acc0fSJohn Dyson 			pmap_remove (map->pmap, uaddr, tend);
28821efb74fbSJohn Dyson 
288347221757SJohn Dyson 			vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
288447221757SJohn Dyson 			vm_map_lock_upgrade(map);
28851efb74fbSJohn Dyson 
28862d8acc0fSJohn Dyson 			if (entry == &map->header) {
28871efb74fbSJohn Dyson 				map->first_free = &map->header;
28881efb74fbSJohn Dyson 			} else if (map->first_free->start >= start) {
28892d8acc0fSJohn Dyson 				map->first_free = entry->prev;
28901efb74fbSJohn Dyson 			}
28911efb74fbSJohn Dyson 
28922d8acc0fSJohn Dyson 			SAVE_HINT(map, entry->prev);
28932d8acc0fSJohn Dyson 			vm_map_entry_delete(map, entry);
28941efb74fbSJohn Dyson 
28952d8acc0fSJohn Dyson 			object = srcobject;
28962d8acc0fSJohn Dyson 			ooffset = cp;
28972d8acc0fSJohn Dyson 
28982d8acc0fSJohn Dyson 			rv = vm_map_insert(map, object, ooffset, start, tend,
2899e5f13bddSAlan Cox 				VM_PROT_ALL, VM_PROT_ALL, MAP_COPY_ON_WRITE);
29001efb74fbSJohn Dyson 
29011efb74fbSJohn Dyson 			if (rv != KERN_SUCCESS)
29021efb74fbSJohn Dyson 				panic("vm_uiomove: could not insert new entry: %d", rv);
29031efb74fbSJohn Dyson 		}
29041efb74fbSJohn Dyson 
29051efb74fbSJohn Dyson /*
29061efb74fbSJohn Dyson  * Map the window directly, if it is already in memory
29071efb74fbSJohn Dyson  */
29082d8acc0fSJohn Dyson 		pmap_object_init_pt(map->pmap, uaddr,
29092d8acc0fSJohn Dyson 			srcobject, oindex, tcnt, 0);
29101efb74fbSJohn Dyson 
291147221757SJohn Dyson 		map->timestamp++;
29121efb74fbSJohn Dyson 		vm_map_unlock(map);
29131efb74fbSJohn Dyson 
29141efb74fbSJohn Dyson 		cnt -= tcnt;
29152d8acc0fSJohn Dyson 		uaddr += tcnt;
29161efb74fbSJohn Dyson 		cp += tcnt;
291795e5e988SJohn Dyson 		if (npages)
291895e5e988SJohn Dyson 			*npages += osize;
29191efb74fbSJohn Dyson 	}
29201efb74fbSJohn Dyson 	return 0;
29211efb74fbSJohn Dyson }
29221efb74fbSJohn Dyson 
29231efb74fbSJohn Dyson /*
29241efb74fbSJohn Dyson  * Performs the copy_on_write operations necessary to allow the virtual copies
29251efb74fbSJohn Dyson  * into user space to work.  This has to be called for write(2) system calls
29261efb74fbSJohn Dyson  * from other processes, file unlinking, and file size shrinkage.
29271efb74fbSJohn Dyson  */
29281efb74fbSJohn Dyson void
29291efb74fbSJohn Dyson vm_freeze_copyopts(object, froma, toa)
29301efb74fbSJohn Dyson 	vm_object_t object;
29311efb74fbSJohn Dyson 	vm_pindex_t froma, toa;
29321efb74fbSJohn Dyson {
2933f5ef029eSPoul-Henning Kamp 	int rv;
2934f5ef029eSPoul-Henning Kamp 	vm_object_t robject;
2935f5ef029eSPoul-Henning Kamp 	vm_pindex_t idx;
29361efb74fbSJohn Dyson 
29372d8acc0fSJohn Dyson 	if ((object == NULL) ||
293895e5e988SJohn Dyson 		((object->flags & OBJ_OPT) == 0))
293995e5e988SJohn Dyson 		return;
29401efb74fbSJohn Dyson 
29411efb74fbSJohn Dyson 	if (object->shadow_count > object->ref_count)
29421efb74fbSJohn Dyson 		panic("vm_freeze_copyopts: sc > rc");
29431efb74fbSJohn Dyson 
29448aef1712SMatthew Dillon 	while((robject = TAILQ_FIRST(&object->shadow_head)) != NULL) {
29451efb74fbSJohn Dyson 		vm_pindex_t bo_pindex;
29461efb74fbSJohn Dyson 		vm_page_t m_in, m_out;
29471efb74fbSJohn Dyson 
29481efb74fbSJohn Dyson 		bo_pindex = OFF_TO_IDX(robject->backing_object_offset);
29491efb74fbSJohn Dyson 
295095e5e988SJohn Dyson 		vm_object_reference(robject);
2951925a3a41SJohn Dyson 
295266095752SJohn Dyson 		vm_object_pip_wait(robject, "objfrz");
2953925a3a41SJohn Dyson 
29541efb74fbSJohn Dyson 		if (robject->ref_count == 1) {
29551efb74fbSJohn Dyson 			vm_object_deallocate(robject);
29561efb74fbSJohn Dyson 			continue;
29571efb74fbSJohn Dyson 		}
29581efb74fbSJohn Dyson 
2959d474eaaaSDoug Rabson 		vm_object_pip_add(robject, 1);
29601efb74fbSJohn Dyson 
296147221757SJohn Dyson 		for (idx = 0; idx < robject->size; idx++) {
29621efb74fbSJohn Dyson 
296395461b45SJohn Dyson 			m_out = vm_page_grab(robject, idx,
296495461b45SJohn Dyson 						VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
29651efb74fbSJohn Dyson 
29661efb74fbSJohn Dyson 			if (m_out->valid == 0) {
296795461b45SJohn Dyson 				m_in = vm_page_grab(object, bo_pindex + idx,
296895461b45SJohn Dyson 						VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
296995461b45SJohn Dyson 				if (m_in->valid == 0) {
297047221757SJohn Dyson 					rv = vm_pager_get_pages(object, &m_in, 1, 0);
297147221757SJohn Dyson 					if (rv != VM_PAGER_OK) {
29723efc015bSPeter Wemm 						printf("vm_freeze_copyopts: cannot read page from file: %lx\n", (long)m_in->pindex);
297347221757SJohn Dyson 						continue;
29741efb74fbSJohn Dyson 					}
297595461b45SJohn Dyson 					vm_page_deactivate(m_in);
297647221757SJohn Dyson 				}
297747221757SJohn Dyson 
297847221757SJohn Dyson 				vm_page_protect(m_in, VM_PROT_NONE);
297947221757SJohn Dyson 				pmap_copy_page(VM_PAGE_TO_PHYS(m_in), VM_PAGE_TO_PHYS(m_out));
298095461b45SJohn Dyson 				m_out->valid = m_in->valid;
29817dbf82dcSMatthew Dillon 				vm_page_dirty(m_out);
298295461b45SJohn Dyson 				vm_page_activate(m_out);
2983e69763a3SDoug Rabson 				vm_page_wakeup(m_in);
29841efb74fbSJohn Dyson 			}
2985e69763a3SDoug Rabson 			vm_page_wakeup(m_out);
298647221757SJohn Dyson 		}
2987925a3a41SJohn Dyson 
29881efb74fbSJohn Dyson 		object->shadow_count--;
298947221757SJohn Dyson 		object->ref_count--;
29901efb74fbSJohn Dyson 		TAILQ_REMOVE(&object->shadow_head, robject, shadow_list);
29911efb74fbSJohn Dyson 		robject->backing_object = NULL;
29921efb74fbSJohn Dyson 		robject->backing_object_offset = 0;
29931efb74fbSJohn Dyson 
299447221757SJohn Dyson 		vm_object_pip_wakeup(robject);
29951efb74fbSJohn Dyson 		vm_object_deallocate(robject);
29961efb74fbSJohn Dyson 	}
299747221757SJohn Dyson 
2998069e9bc1SDoug Rabson 	vm_object_clear_flag(object, OBJ_OPT);
29991efb74fbSJohn Dyson }
30001efb74fbSJohn Dyson 
3001c7c34a24SBruce Evans #include "opt_ddb.h"
3002c3cb3e12SDavid Greenman #ifdef DDB
3003c7c34a24SBruce Evans #include <sys/kernel.h>
3004c7c34a24SBruce Evans 
3005c7c34a24SBruce Evans #include <ddb/ddb.h>
3006c7c34a24SBruce Evans 
3007df8bae1dSRodney W. Grimes /*
3008df8bae1dSRodney W. Grimes  *	vm_map_print:	[ debug ]
3009df8bae1dSRodney W. Grimes  */
3010c7c34a24SBruce Evans DB_SHOW_COMMAND(map, vm_map_print)
3011df8bae1dSRodney W. Grimes {
301295e5e988SJohn Dyson 	static int nlines;
3013c7c34a24SBruce Evans 	/* XXX convert args. */
3014c0877f10SJohn Dyson 	vm_map_t map = (vm_map_t)addr;
3015c7c34a24SBruce Evans 	boolean_t full = have_addr;
3016df8bae1dSRodney W. Grimes 
3017c0877f10SJohn Dyson 	vm_map_entry_t entry;
3018c7c34a24SBruce Evans 
3019e5f251d2SAlan Cox 	db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
3020e5f251d2SAlan Cox 	    (void *)map,
3021101eeb7fSBruce Evans 	    (void *)map->pmap, map->nentries, map->timestamp);
302295e5e988SJohn Dyson 	nlines++;
3023df8bae1dSRodney W. Grimes 
3024c7c34a24SBruce Evans 	if (!full && db_indent)
3025df8bae1dSRodney W. Grimes 		return;
3026df8bae1dSRodney W. Grimes 
3027c7c34a24SBruce Evans 	db_indent += 2;
3028df8bae1dSRodney W. Grimes 	for (entry = map->header.next; entry != &map->header;
3029df8bae1dSRodney W. Grimes 	    entry = entry->next) {
3030fc62ef1fSBruce Evans 		db_iprintf("map entry %p: start=%p, end=%p\n",
3031fc62ef1fSBruce Evans 		    (void *)entry, (void *)entry->start, (void *)entry->end);
303295e5e988SJohn Dyson 		nlines++;
3033e5f251d2SAlan Cox 		{
3034df8bae1dSRodney W. Grimes 			static char *inheritance_name[4] =
3035df8bae1dSRodney W. Grimes 			{"share", "copy", "none", "donate_copy"};
30360d94caffSDavid Greenman 
303795e5e988SJohn Dyson 			db_iprintf(" prot=%x/%x/%s",
3038df8bae1dSRodney W. Grimes 			    entry->protection,
3039df8bae1dSRodney W. Grimes 			    entry->max_protection,
30408aef1712SMatthew Dillon 			    inheritance_name[(int)(unsigned char)entry->inheritance]);
3041df8bae1dSRodney W. Grimes 			if (entry->wired_count != 0)
304295e5e988SJohn Dyson 				db_printf(", wired");
3043df8bae1dSRodney W. Grimes 		}
30449fdfe602SMatthew Dillon 		if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3045101eeb7fSBruce Evans 			/* XXX no %qd in kernel.  Truncate entry->offset. */
3046101eeb7fSBruce Evans 			db_printf(", share=%p, offset=0x%lx\n",
30479fdfe602SMatthew Dillon 			    (void *)entry->object.sub_map,
3048ecbb00a2SDoug Rabson 			    (long)entry->offset);
304995e5e988SJohn Dyson 			nlines++;
3050df8bae1dSRodney W. Grimes 			if ((entry->prev == &map->header) ||
30519fdfe602SMatthew Dillon 			    (entry->prev->object.sub_map !=
30529fdfe602SMatthew Dillon 				entry->object.sub_map)) {
3053c7c34a24SBruce Evans 				db_indent += 2;
3054101eeb7fSBruce Evans 				vm_map_print((db_expr_t)(intptr_t)
30559fdfe602SMatthew Dillon 					     entry->object.sub_map,
3056914181e7SBruce Evans 					     full, 0, (char *)0);
3057c7c34a24SBruce Evans 				db_indent -= 2;
3058df8bae1dSRodney W. Grimes 			}
30590d94caffSDavid Greenman 		} else {
3060101eeb7fSBruce Evans 			/* XXX no %qd in kernel.  Truncate entry->offset. */
3061101eeb7fSBruce Evans 			db_printf(", object=%p, offset=0x%lx",
3062101eeb7fSBruce Evans 			    (void *)entry->object.vm_object,
3063ecbb00a2SDoug Rabson 			    (long)entry->offset);
3064afa07f7eSJohn Dyson 			if (entry->eflags & MAP_ENTRY_COW)
3065c7c34a24SBruce Evans 				db_printf(", copy (%s)",
3066afa07f7eSJohn Dyson 				    (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
3067c7c34a24SBruce Evans 			db_printf("\n");
306895e5e988SJohn Dyson 			nlines++;
3069df8bae1dSRodney W. Grimes 
3070df8bae1dSRodney W. Grimes 			if ((entry->prev == &map->header) ||
3071df8bae1dSRodney W. Grimes 			    (entry->prev->object.vm_object !=
3072df8bae1dSRodney W. Grimes 				entry->object.vm_object)) {
3073c7c34a24SBruce Evans 				db_indent += 2;
3074101eeb7fSBruce Evans 				vm_object_print((db_expr_t)(intptr_t)
3075101eeb7fSBruce Evans 						entry->object.vm_object,
3076914181e7SBruce Evans 						full, 0, (char *)0);
307795e5e988SJohn Dyson 				nlines += 4;
3078c7c34a24SBruce Evans 				db_indent -= 2;
3079df8bae1dSRodney W. Grimes 			}
3080df8bae1dSRodney W. Grimes 		}
3081df8bae1dSRodney W. Grimes 	}
3082c7c34a24SBruce Evans 	db_indent -= 2;
308395e5e988SJohn Dyson 	if (db_indent == 0)
308495e5e988SJohn Dyson 		nlines = 0;
3085df8bae1dSRodney W. Grimes }
308695e5e988SJohn Dyson 
308795e5e988SJohn Dyson 
308895e5e988SJohn Dyson DB_SHOW_COMMAND(procvm, procvm)
308995e5e988SJohn Dyson {
309095e5e988SJohn Dyson 	struct proc *p;
309195e5e988SJohn Dyson 
309295e5e988SJohn Dyson 	if (have_addr) {
309395e5e988SJohn Dyson 		p = (struct proc *) addr;
309495e5e988SJohn Dyson 	} else {
309595e5e988SJohn Dyson 		p = curproc;
309695e5e988SJohn Dyson 	}
309795e5e988SJohn Dyson 
3098ac1e407bSBruce Evans 	db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
3099ac1e407bSBruce Evans 	    (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
3100b1028ad1SLuoqi Chen 	    (void *)vmspace_pmap(p->p_vmspace));
310195e5e988SJohn Dyson 
3102101eeb7fSBruce Evans 	vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);
310395e5e988SJohn Dyson }
310495e5e988SJohn Dyson 
3105c7c34a24SBruce Evans #endif /* DDB */
3106