xref: /freebsd/sys/vm/vm_map.c (revision b28cb1ca078f0937c720a5a9bb6d25826580ea0f)
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;
257b823bbd6SMatthew Dillon 	map->infork = 0;
258df8bae1dSRodney W. Grimes 	map->min_offset = min;
259df8bae1dSRodney W. Grimes 	map->max_offset = max;
260df8bae1dSRodney W. Grimes 	map->first_free = &map->header;
261df8bae1dSRodney W. Grimes 	map->hint = &map->header;
262df8bae1dSRodney W. Grimes 	map->timestamp = 0;
2638f9110f6SJohn Dyson 	lockinit(&map->lock, PVM, "thrd_sleep", 0, LK_NOPAUSE);
264df8bae1dSRodney W. Grimes }
265df8bae1dSRodney W. Grimes 
266a18b1f1dSJason Evans void
267a18b1f1dSJason Evans vm_map_destroy(map)
268a18b1f1dSJason Evans 	struct vm_map *map;
269a18b1f1dSJason Evans {
270a18b1f1dSJason Evans 	lockdestroy(&map->lock);
271a18b1f1dSJason Evans }
272a18b1f1dSJason Evans 
273df8bae1dSRodney W. Grimes /*
274b18bfc3dSJohn Dyson  *	vm_map_entry_dispose:	[ internal use only ]
275b18bfc3dSJohn Dyson  *
276b18bfc3dSJohn Dyson  *	Inverse of vm_map_entry_create.
277b18bfc3dSJohn Dyson  */
27862487bb4SJohn Dyson static void
279b18bfc3dSJohn Dyson vm_map_entry_dispose(map, entry)
280b18bfc3dSJohn Dyson 	vm_map_t map;
281b18bfc3dSJohn Dyson 	vm_map_entry_t entry;
282b18bfc3dSJohn Dyson {
283b79933ebSJohn Dyson 	zfree((map->system_map || !mapentzone) ? kmapentzone : mapentzone, entry);
284b18bfc3dSJohn Dyson }
285b18bfc3dSJohn Dyson 
286b18bfc3dSJohn Dyson /*
287df8bae1dSRodney W. Grimes  *	vm_map_entry_create:	[ internal use only ]
288df8bae1dSRodney W. Grimes  *
289df8bae1dSRodney W. Grimes  *	Allocates a VM map entry for insertion.
290b28cb1caSAlfred Perlstein  *	No entry fields are filled in.
291df8bae1dSRodney W. Grimes  */
292f708ef1bSPoul-Henning Kamp static vm_map_entry_t
29326f9a767SRodney W. Grimes vm_map_entry_create(map)
294df8bae1dSRodney W. Grimes 	vm_map_t map;
295df8bae1dSRodney W. Grimes {
2961f6889a1SMatthew Dillon 	vm_map_entry_t new_entry;
2971f6889a1SMatthew Dillon 
2981f6889a1SMatthew Dillon 	new_entry = zalloc((map->system_map || !mapentzone) ?
2991f6889a1SMatthew Dillon 		kmapentzone : mapentzone);
3001f6889a1SMatthew Dillon 	if (new_entry == NULL)
3011f6889a1SMatthew Dillon 	    panic("vm_map_entry_create: kernel resources exhausted");
3021f6889a1SMatthew Dillon 	return(new_entry);
303df8bae1dSRodney W. Grimes }
304df8bae1dSRodney W. Grimes 
305df8bae1dSRodney W. Grimes /*
306df8bae1dSRodney W. Grimes  *	vm_map_entry_{un,}link:
307df8bae1dSRodney W. Grimes  *
308df8bae1dSRodney W. Grimes  *	Insert/remove entries from maps.
309df8bae1dSRodney W. Grimes  */
31099c81ca9SAlan Cox static __inline void
31199c81ca9SAlan Cox vm_map_entry_link(vm_map_t map,
31299c81ca9SAlan Cox 		  vm_map_entry_t after_where,
31399c81ca9SAlan Cox 		  vm_map_entry_t entry)
31499c81ca9SAlan Cox {
31599c81ca9SAlan Cox 	map->nentries++;
31699c81ca9SAlan Cox 	entry->prev = after_where;
31799c81ca9SAlan Cox 	entry->next = after_where->next;
31899c81ca9SAlan Cox 	entry->next->prev = entry;
31999c81ca9SAlan Cox 	after_where->next = entry;
320df8bae1dSRodney W. Grimes }
32199c81ca9SAlan Cox 
32299c81ca9SAlan Cox static __inline void
32399c81ca9SAlan Cox vm_map_entry_unlink(vm_map_t map,
32499c81ca9SAlan Cox 		    vm_map_entry_t entry)
32599c81ca9SAlan Cox {
32699c81ca9SAlan Cox 	vm_map_entry_t prev = entry->prev;
32799c81ca9SAlan Cox 	vm_map_entry_t next = entry->next;
32899c81ca9SAlan Cox 
32999c81ca9SAlan Cox 	next->prev = prev;
33099c81ca9SAlan Cox 	prev->next = next;
33199c81ca9SAlan Cox 	map->nentries--;
332df8bae1dSRodney W. Grimes }
333df8bae1dSRodney W. Grimes 
334df8bae1dSRodney W. Grimes /*
335df8bae1dSRodney W. Grimes  *	SAVE_HINT:
336df8bae1dSRodney W. Grimes  *
337df8bae1dSRodney W. Grimes  *	Saves the specified entry as the hint for
33824a1cce3SDavid Greenman  *	future lookups.
339df8bae1dSRodney W. Grimes  */
340df8bae1dSRodney W. Grimes #define	SAVE_HINT(map,value) \
34124a1cce3SDavid Greenman 		(map)->hint = (value);
342df8bae1dSRodney W. Grimes 
343df8bae1dSRodney W. Grimes /*
344df8bae1dSRodney W. Grimes  *	vm_map_lookup_entry:	[ internal use only ]
345df8bae1dSRodney W. Grimes  *
346df8bae1dSRodney W. Grimes  *	Finds the map entry containing (or
347df8bae1dSRodney W. Grimes  *	immediately preceding) the specified address
348df8bae1dSRodney W. Grimes  *	in the given map; the entry is returned
349df8bae1dSRodney W. Grimes  *	in the "entry" parameter.  The boolean
350df8bae1dSRodney W. Grimes  *	result indicates whether the address is
351df8bae1dSRodney W. Grimes  *	actually contained in the map.
352df8bae1dSRodney W. Grimes  */
3530d94caffSDavid Greenman boolean_t
3540d94caffSDavid Greenman vm_map_lookup_entry(map, address, entry)
355c0877f10SJohn Dyson 	vm_map_t map;
356c0877f10SJohn Dyson 	vm_offset_t address;
357df8bae1dSRodney W. Grimes 	vm_map_entry_t *entry;	/* OUT */
358df8bae1dSRodney W. Grimes {
359c0877f10SJohn Dyson 	vm_map_entry_t cur;
360c0877f10SJohn Dyson 	vm_map_entry_t last;
361df8bae1dSRodney W. Grimes 
362df8bae1dSRodney W. Grimes 	/*
3630d94caffSDavid Greenman 	 * Start looking either from the head of the list, or from the hint.
364df8bae1dSRodney W. Grimes 	 */
365df8bae1dSRodney W. Grimes 
366df8bae1dSRodney W. Grimes 	cur = map->hint;
367df8bae1dSRodney W. Grimes 
368df8bae1dSRodney W. Grimes 	if (cur == &map->header)
369df8bae1dSRodney W. Grimes 		cur = cur->next;
370df8bae1dSRodney W. Grimes 
371df8bae1dSRodney W. Grimes 	if (address >= cur->start) {
372df8bae1dSRodney W. Grimes 		/*
373df8bae1dSRodney W. Grimes 		 * Go from hint to end of list.
374df8bae1dSRodney W. Grimes 		 *
3750d94caffSDavid Greenman 		 * But first, make a quick check to see if we are already looking
3760d94caffSDavid Greenman 		 * at the entry we want (which is usually the case). Note also
3770d94caffSDavid Greenman 		 * that we don't need to save the hint here... it is the same
3780d94caffSDavid Greenman 		 * hint (unless we are at the header, in which case the hint
3790d94caffSDavid Greenman 		 * didn't buy us anything anyway).
380df8bae1dSRodney W. Grimes 		 */
381df8bae1dSRodney W. Grimes 		last = &map->header;
382df8bae1dSRodney W. Grimes 		if ((cur != last) && (cur->end > address)) {
383df8bae1dSRodney W. Grimes 			*entry = cur;
384df8bae1dSRodney W. Grimes 			return (TRUE);
385df8bae1dSRodney W. Grimes 		}
3860d94caffSDavid Greenman 	} else {
387df8bae1dSRodney W. Grimes 		/*
388df8bae1dSRodney W. Grimes 		 * Go from start to hint, *inclusively*
389df8bae1dSRodney W. Grimes 		 */
390df8bae1dSRodney W. Grimes 		last = cur->next;
391df8bae1dSRodney W. Grimes 		cur = map->header.next;
392df8bae1dSRodney W. Grimes 	}
393df8bae1dSRodney W. Grimes 
394df8bae1dSRodney W. Grimes 	/*
395df8bae1dSRodney W. Grimes 	 * Search linearly
396df8bae1dSRodney W. Grimes 	 */
397df8bae1dSRodney W. Grimes 
398df8bae1dSRodney W. Grimes 	while (cur != last) {
399df8bae1dSRodney W. Grimes 		if (cur->end > address) {
400df8bae1dSRodney W. Grimes 			if (address >= cur->start) {
401df8bae1dSRodney W. Grimes 				/*
4020d94caffSDavid Greenman 				 * Save this lookup for future hints, and
4030d94caffSDavid Greenman 				 * return
404df8bae1dSRodney W. Grimes 				 */
405df8bae1dSRodney W. Grimes 
406df8bae1dSRodney W. Grimes 				*entry = cur;
407df8bae1dSRodney W. Grimes 				SAVE_HINT(map, cur);
408df8bae1dSRodney W. Grimes 				return (TRUE);
409df8bae1dSRodney W. Grimes 			}
410df8bae1dSRodney W. Grimes 			break;
411df8bae1dSRodney W. Grimes 		}
412df8bae1dSRodney W. Grimes 		cur = cur->next;
413df8bae1dSRodney W. Grimes 	}
414df8bae1dSRodney W. Grimes 	*entry = cur->prev;
415df8bae1dSRodney W. Grimes 	SAVE_HINT(map, *entry);
416df8bae1dSRodney W. Grimes 	return (FALSE);
417df8bae1dSRodney W. Grimes }
418df8bae1dSRodney W. Grimes 
419df8bae1dSRodney W. Grimes /*
42030dcfc09SJohn Dyson  *	vm_map_insert:
42130dcfc09SJohn Dyson  *
42230dcfc09SJohn Dyson  *	Inserts the given whole VM object into the target
42330dcfc09SJohn Dyson  *	map at the specified address range.  The object's
42430dcfc09SJohn Dyson  *	size should match that of the address range.
42530dcfc09SJohn Dyson  *
42630dcfc09SJohn Dyson  *	Requires that the map be locked, and leaves it so.
4272aaeadf8SMatthew Dillon  *
4282aaeadf8SMatthew Dillon  *	If object is non-NULL, ref count must be bumped by caller
4292aaeadf8SMatthew Dillon  *	prior to making call to account for the new entry.
43030dcfc09SJohn Dyson  */
43130dcfc09SJohn Dyson int
432b9dcd593SBruce Evans vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
433b9dcd593SBruce Evans 	      vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max,
434b9dcd593SBruce Evans 	      int cow)
43530dcfc09SJohn Dyson {
436c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
437c0877f10SJohn Dyson 	vm_map_entry_t prev_entry;
43830dcfc09SJohn Dyson 	vm_map_entry_t temp_entry;
4399730a5daSPaul Saab 	vm_eflags_t protoeflags;
44030dcfc09SJohn Dyson 
44130dcfc09SJohn Dyson 	/*
44230dcfc09SJohn Dyson 	 * Check that the start and end points are not bogus.
44330dcfc09SJohn Dyson 	 */
44430dcfc09SJohn Dyson 
44530dcfc09SJohn Dyson 	if ((start < map->min_offset) || (end > map->max_offset) ||
44630dcfc09SJohn Dyson 	    (start >= end))
44730dcfc09SJohn Dyson 		return (KERN_INVALID_ADDRESS);
44830dcfc09SJohn Dyson 
44930dcfc09SJohn Dyson 	/*
45030dcfc09SJohn Dyson 	 * Find the entry prior to the proposed starting address; if it's part
45130dcfc09SJohn Dyson 	 * of an existing entry, this range is bogus.
45230dcfc09SJohn Dyson 	 */
45330dcfc09SJohn Dyson 
45430dcfc09SJohn Dyson 	if (vm_map_lookup_entry(map, start, &temp_entry))
45530dcfc09SJohn Dyson 		return (KERN_NO_SPACE);
45630dcfc09SJohn Dyson 
45730dcfc09SJohn Dyson 	prev_entry = temp_entry;
45830dcfc09SJohn Dyson 
45930dcfc09SJohn Dyson 	/*
46030dcfc09SJohn Dyson 	 * Assert that the next entry doesn't overlap the end point.
46130dcfc09SJohn Dyson 	 */
46230dcfc09SJohn Dyson 
46330dcfc09SJohn Dyson 	if ((prev_entry->next != &map->header) &&
46430dcfc09SJohn Dyson 	    (prev_entry->next->start < end))
46530dcfc09SJohn Dyson 		return (KERN_NO_SPACE);
46630dcfc09SJohn Dyson 
467afa07f7eSJohn Dyson 	protoeflags = 0;
468afa07f7eSJohn Dyson 
469afa07f7eSJohn Dyson 	if (cow & MAP_COPY_ON_WRITE)
470e5f13bddSAlan Cox 		protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
471afa07f7eSJohn Dyson 
4724e045f93SAlan Cox 	if (cow & MAP_NOFAULT) {
473afa07f7eSJohn Dyson 		protoeflags |= MAP_ENTRY_NOFAULT;
474afa07f7eSJohn Dyson 
4754e045f93SAlan Cox 		KASSERT(object == NULL,
4764e045f93SAlan Cox 			("vm_map_insert: paradoxical MAP_NOFAULT request"));
4774e045f93SAlan Cox 	}
4784f79d873SMatthew Dillon 	if (cow & MAP_DISABLE_SYNCER)
4794f79d873SMatthew Dillon 		protoeflags |= MAP_ENTRY_NOSYNC;
4809730a5daSPaul Saab 	if (cow & MAP_DISABLE_COREDUMP)
4819730a5daSPaul Saab 		protoeflags |= MAP_ENTRY_NOCOREDUMP;
4824f79d873SMatthew Dillon 
4832aaeadf8SMatthew Dillon 	if (object) {
48430dcfc09SJohn Dyson 		/*
4852aaeadf8SMatthew Dillon 		 * When object is non-NULL, it could be shared with another
4862aaeadf8SMatthew Dillon 		 * process.  We have to set or clear OBJ_ONEMAPPING
4872aaeadf8SMatthew Dillon 		 * appropriately.
48830dcfc09SJohn Dyson 		 */
4892aaeadf8SMatthew Dillon 		if ((object->ref_count > 1) || (object->shadow_count != 0)) {
4902aaeadf8SMatthew Dillon 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
4912aaeadf8SMatthew Dillon 		}
4924e045f93SAlan Cox 	}
4934e045f93SAlan Cox 	else if ((prev_entry != &map->header) &&
4944e045f93SAlan Cox 		 (prev_entry->eflags == protoeflags) &&
4958cc7e047SJohn Dyson 		 (prev_entry->end == start) &&
4964e045f93SAlan Cox 		 (prev_entry->wired_count == 0) &&
4974e045f93SAlan Cox 		 ((prev_entry->object.vm_object == NULL) ||
4988cc7e047SJohn Dyson 		  vm_object_coalesce(prev_entry->object.vm_object,
49930dcfc09SJohn Dyson 				     OFF_TO_IDX(prev_entry->offset),
5008cc7e047SJohn Dyson 				     (vm_size_t)(prev_entry->end - prev_entry->start),
501cdc2c291SJohn Dyson 				     (vm_size_t)(end - prev_entry->end)))) {
50230dcfc09SJohn Dyson 		/*
5032aaeadf8SMatthew Dillon 		 * We were able to extend the object.  Determine if we
5042aaeadf8SMatthew Dillon 		 * can extend the previous map entry to include the
5052aaeadf8SMatthew Dillon 		 * new range as well.
50630dcfc09SJohn Dyson 		 */
5078cc7e047SJohn Dyson 		if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
5088cc7e047SJohn Dyson 		    (prev_entry->protection == prot) &&
5098cc7e047SJohn Dyson 		    (prev_entry->max_protection == max)) {
51030dcfc09SJohn Dyson 			map->size += (end - prev_entry->end);
51130dcfc09SJohn Dyson 			prev_entry->end = end;
5124e71e795SMatthew Dillon 			vm_map_simplify_entry(map, prev_entry);
51330dcfc09SJohn Dyson 			return (KERN_SUCCESS);
51430dcfc09SJohn Dyson 		}
5158cc7e047SJohn Dyson 
5162aaeadf8SMatthew Dillon 		/*
5172aaeadf8SMatthew Dillon 		 * If we can extend the object but cannot extend the
5182aaeadf8SMatthew Dillon 		 * map entry, we have to create a new map entry.  We
5192aaeadf8SMatthew Dillon 		 * must bump the ref count on the extended object to
5204e71e795SMatthew Dillon 		 * account for it.  object may be NULL.
5212aaeadf8SMatthew Dillon 		 */
5222aaeadf8SMatthew Dillon 		object = prev_entry->object.vm_object;
5232aaeadf8SMatthew Dillon 		offset = prev_entry->offset +
5242aaeadf8SMatthew Dillon 			(prev_entry->end - prev_entry->start);
5258cc7e047SJohn Dyson 		vm_object_reference(object);
526b18bfc3dSJohn Dyson 	}
5272aaeadf8SMatthew Dillon 
5282aaeadf8SMatthew Dillon 	/*
5292aaeadf8SMatthew Dillon 	 * NOTE: if conditionals fail, object can be NULL here.  This occurs
5302aaeadf8SMatthew Dillon 	 * in things like the buffer map where we manage kva but do not manage
5312aaeadf8SMatthew Dillon 	 * backing objects.
5322aaeadf8SMatthew Dillon 	 */
5338cc7e047SJohn Dyson 
53430dcfc09SJohn Dyson 	/*
53530dcfc09SJohn Dyson 	 * Create a new entry
53630dcfc09SJohn Dyson 	 */
53730dcfc09SJohn Dyson 
53830dcfc09SJohn Dyson 	new_entry = vm_map_entry_create(map);
53930dcfc09SJohn Dyson 	new_entry->start = start;
54030dcfc09SJohn Dyson 	new_entry->end = end;
54130dcfc09SJohn Dyson 
542afa07f7eSJohn Dyson 	new_entry->eflags = protoeflags;
54330dcfc09SJohn Dyson 	new_entry->object.vm_object = object;
54430dcfc09SJohn Dyson 	new_entry->offset = offset;
5452267af78SJulian Elischer 	new_entry->avail_ssize = 0;
5462267af78SJulian Elischer 
54730dcfc09SJohn Dyson 	new_entry->inheritance = VM_INHERIT_DEFAULT;
54830dcfc09SJohn Dyson 	new_entry->protection = prot;
54930dcfc09SJohn Dyson 	new_entry->max_protection = max;
55030dcfc09SJohn Dyson 	new_entry->wired_count = 0;
551e5f251d2SAlan Cox 
55230dcfc09SJohn Dyson 	/*
55330dcfc09SJohn Dyson 	 * Insert the new entry into the list
55430dcfc09SJohn Dyson 	 */
55530dcfc09SJohn Dyson 
55630dcfc09SJohn Dyson 	vm_map_entry_link(map, prev_entry, new_entry);
55730dcfc09SJohn Dyson 	map->size += new_entry->end - new_entry->start;
55830dcfc09SJohn Dyson 
55930dcfc09SJohn Dyson 	/*
56030dcfc09SJohn Dyson 	 * Update the free space hint
56130dcfc09SJohn Dyson 	 */
56267bf6868SJohn Dyson 	if ((map->first_free == prev_entry) &&
5634f79d873SMatthew Dillon 	    (prev_entry->end >= new_entry->start)) {
56430dcfc09SJohn Dyson 		map->first_free = new_entry;
5654f79d873SMatthew Dillon 	}
56630dcfc09SJohn Dyson 
5671a484d28SMatthew Dillon #if 0
5681a484d28SMatthew Dillon 	/*
5691a484d28SMatthew Dillon 	 * Temporarily removed to avoid MAP_STACK panic, due to
5701a484d28SMatthew Dillon 	 * MAP_STACK being a huge hack.  Will be added back in
5711a484d28SMatthew Dillon 	 * when MAP_STACK (and the user stack mapping) is fixed.
5721a484d28SMatthew Dillon 	 */
5734e71e795SMatthew Dillon 	/*
5744e71e795SMatthew Dillon 	 * It may be possible to simplify the entry
5754e71e795SMatthew Dillon 	 */
5764e71e795SMatthew Dillon 	vm_map_simplify_entry(map, new_entry);
5771a484d28SMatthew Dillon #endif
5784e71e795SMatthew Dillon 
5794f79d873SMatthew Dillon 	if (cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) {
580e972780aSAlan Cox 		pmap_object_init_pt(map->pmap, start,
581e972780aSAlan Cox 				    object, OFF_TO_IDX(offset), end - start,
582e972780aSAlan Cox 				    cow & MAP_PREFAULT_PARTIAL);
5834f79d873SMatthew Dillon 	}
584e972780aSAlan Cox 
58530dcfc09SJohn Dyson 	return (KERN_SUCCESS);
58630dcfc09SJohn Dyson }
58730dcfc09SJohn Dyson 
58830dcfc09SJohn Dyson /*
589df8bae1dSRodney W. Grimes  * Find sufficient space for `length' bytes in the given map, starting at
590df8bae1dSRodney W. Grimes  * `start'.  The map must be locked.  Returns 0 on success, 1 on no space.
591df8bae1dSRodney W. Grimes  */
592df8bae1dSRodney W. Grimes int
593df8bae1dSRodney W. Grimes vm_map_findspace(map, start, length, addr)
594c0877f10SJohn Dyson 	vm_map_t map;
595c0877f10SJohn Dyson 	vm_offset_t start;
596df8bae1dSRodney W. Grimes 	vm_size_t length;
597df8bae1dSRodney W. Grimes 	vm_offset_t *addr;
598df8bae1dSRodney W. Grimes {
599c0877f10SJohn Dyson 	vm_map_entry_t entry, next;
600c0877f10SJohn Dyson 	vm_offset_t end;
601df8bae1dSRodney W. Grimes 
602df8bae1dSRodney W. Grimes 	if (start < map->min_offset)
603df8bae1dSRodney W. Grimes 		start = map->min_offset;
604df8bae1dSRodney W. Grimes 	if (start > map->max_offset)
605df8bae1dSRodney W. Grimes 		return (1);
606df8bae1dSRodney W. Grimes 
607df8bae1dSRodney W. Grimes 	/*
6080d94caffSDavid Greenman 	 * Look for the first possible address; if there's already something
6090d94caffSDavid Greenman 	 * at this address, we have to start after it.
610df8bae1dSRodney W. Grimes 	 */
611df8bae1dSRodney W. Grimes 	if (start == map->min_offset) {
61267bf6868SJohn Dyson 		if ((entry = map->first_free) != &map->header)
613df8bae1dSRodney W. Grimes 			start = entry->end;
614df8bae1dSRodney W. Grimes 	} else {
615df8bae1dSRodney W. Grimes 		vm_map_entry_t tmp;
6160d94caffSDavid Greenman 
617df8bae1dSRodney W. Grimes 		if (vm_map_lookup_entry(map, start, &tmp))
618df8bae1dSRodney W. Grimes 			start = tmp->end;
619df8bae1dSRodney W. Grimes 		entry = tmp;
620df8bae1dSRodney W. Grimes 	}
621df8bae1dSRodney W. Grimes 
622df8bae1dSRodney W. Grimes 	/*
6230d94caffSDavid Greenman 	 * Look through the rest of the map, trying to fit a new region in the
6240d94caffSDavid Greenman 	 * gap between existing regions, or after the very last region.
625df8bae1dSRodney W. Grimes 	 */
626df8bae1dSRodney W. Grimes 	for (;; start = (entry = next)->end) {
627df8bae1dSRodney W. Grimes 		/*
628df8bae1dSRodney W. Grimes 		 * Find the end of the proposed new region.  Be sure we didn't
629df8bae1dSRodney W. Grimes 		 * go beyond the end of the map, or wrap around the address;
630df8bae1dSRodney W. Grimes 		 * if so, we lose.  Otherwise, if this is the last entry, or
631df8bae1dSRodney W. Grimes 		 * if the proposed new region fits before the next entry, we
632df8bae1dSRodney W. Grimes 		 * win.
633df8bae1dSRodney W. Grimes 		 */
634df8bae1dSRodney W. Grimes 		end = start + length;
635df8bae1dSRodney W. Grimes 		if (end > map->max_offset || end < start)
636df8bae1dSRodney W. Grimes 			return (1);
637df8bae1dSRodney W. Grimes 		next = entry->next;
638df8bae1dSRodney W. Grimes 		if (next == &map->header || next->start >= end)
639df8bae1dSRodney W. Grimes 			break;
640df8bae1dSRodney W. Grimes 	}
641df8bae1dSRodney W. Grimes 	SAVE_HINT(map, entry);
642df8bae1dSRodney W. Grimes 	*addr = start;
64399448ed1SJohn Dyson 	if (map == kernel_map) {
64499448ed1SJohn Dyson 		vm_offset_t ksize;
64599448ed1SJohn Dyson 		if ((ksize = round_page(start + length)) > kernel_vm_end) {
64699448ed1SJohn Dyson 			pmap_growkernel(ksize);
64799448ed1SJohn Dyson 		}
64899448ed1SJohn Dyson 	}
649df8bae1dSRodney W. Grimes 	return (0);
650df8bae1dSRodney W. Grimes }
651df8bae1dSRodney W. Grimes 
652df8bae1dSRodney W. Grimes /*
653df8bae1dSRodney W. Grimes  *	vm_map_find finds an unallocated region in the target address
654df8bae1dSRodney W. Grimes  *	map with the given length.  The search is defined to be
655df8bae1dSRodney W. Grimes  *	first-fit from the specified address; the region found is
656df8bae1dSRodney W. Grimes  *	returned in the same parameter.
657df8bae1dSRodney W. Grimes  *
6582aaeadf8SMatthew Dillon  *	If object is non-NULL, ref count must be bumped by caller
6592aaeadf8SMatthew Dillon  *	prior to making call to account for the new entry.
660df8bae1dSRodney W. Grimes  */
661df8bae1dSRodney W. Grimes int
662b9dcd593SBruce Evans vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
663b9dcd593SBruce Evans 	    vm_offset_t *addr,	/* IN/OUT */
664b9dcd593SBruce Evans 	    vm_size_t length, boolean_t find_space, vm_prot_t prot,
665b9dcd593SBruce Evans 	    vm_prot_t max, int cow)
666df8bae1dSRodney W. Grimes {
667c0877f10SJohn Dyson 	vm_offset_t start;
6688d6e8edeSDavid Greenman 	int result, s = 0;
669df8bae1dSRodney W. Grimes 
670df8bae1dSRodney W. Grimes 	start = *addr;
6718d6e8edeSDavid Greenman 
6729579ee64SDavid Greenman 	if (map == kmem_map || map == mb_map)
673b18bfc3dSJohn Dyson 		s = splvm();
6748d6e8edeSDavid Greenman 
675bea41bcfSDavid Greenman 	vm_map_lock(map);
676df8bae1dSRodney W. Grimes 	if (find_space) {
677df8bae1dSRodney W. Grimes 		if (vm_map_findspace(map, start, length, addr)) {
678df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
6799579ee64SDavid Greenman 			if (map == kmem_map || map == mb_map)
6808d6e8edeSDavid Greenman 				splx(s);
681df8bae1dSRodney W. Grimes 			return (KERN_NO_SPACE);
682df8bae1dSRodney W. Grimes 		}
683df8bae1dSRodney W. Grimes 		start = *addr;
684df8bae1dSRodney W. Grimes 	}
685bd7e5f99SJohn Dyson 	result = vm_map_insert(map, object, offset,
686bd7e5f99SJohn Dyson 		start, start + length, prot, max, cow);
687df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
6888d6e8edeSDavid Greenman 
6899579ee64SDavid Greenman 	if (map == kmem_map || map == mb_map)
6908d6e8edeSDavid Greenman 		splx(s);
6918d6e8edeSDavid Greenman 
692df8bae1dSRodney W. Grimes 	return (result);
693df8bae1dSRodney W. Grimes }
694df8bae1dSRodney W. Grimes 
695df8bae1dSRodney W. Grimes /*
696b7b2aac2SJohn Dyson  *	vm_map_simplify_entry:
69767bf6868SJohn Dyson  *
6984e71e795SMatthew Dillon  *	Simplify the given map entry by merging with either neighbor.  This
6994e71e795SMatthew Dillon  *	routine also has the ability to merge with both neighbors.
7004e71e795SMatthew Dillon  *
7014e71e795SMatthew Dillon  *	The map must be locked.
7024e71e795SMatthew Dillon  *
7034e71e795SMatthew Dillon  *	This routine guarentees that the passed entry remains valid (though
7044e71e795SMatthew Dillon  *	possibly extended).  When merging, this routine may delete one or
7054e71e795SMatthew Dillon  *	both neighbors.
706df8bae1dSRodney W. Grimes  */
707b7b2aac2SJohn Dyson void
7080d94caffSDavid Greenman vm_map_simplify_entry(map, entry)
709df8bae1dSRodney W. Grimes 	vm_map_t map;
710df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
711df8bae1dSRodney W. Grimes {
712308c24baSJohn Dyson 	vm_map_entry_t next, prev;
713b7b2aac2SJohn Dyson 	vm_size_t prevsize, esize;
714df8bae1dSRodney W. Grimes 
7159fdfe602SMatthew Dillon 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
716df8bae1dSRodney W. Grimes 		return;
717308c24baSJohn Dyson 
718308c24baSJohn Dyson 	prev = entry->prev;
719308c24baSJohn Dyson 	if (prev != &map->header) {
72067bf6868SJohn Dyson 		prevsize = prev->end - prev->start;
72167bf6868SJohn Dyson 		if ( (prev->end == entry->start) &&
72267bf6868SJohn Dyson 		     (prev->object.vm_object == entry->object.vm_object) &&
72395e5e988SJohn Dyson 		     (!prev->object.vm_object ||
72467bf6868SJohn Dyson 			(prev->offset + prevsize == entry->offset)) &&
725afa07f7eSJohn Dyson 		     (prev->eflags == entry->eflags) &&
72667bf6868SJohn Dyson 		     (prev->protection == entry->protection) &&
72767bf6868SJohn Dyson 		     (prev->max_protection == entry->max_protection) &&
72867bf6868SJohn Dyson 		     (prev->inheritance == entry->inheritance) &&
729b7b2aac2SJohn Dyson 		     (prev->wired_count == entry->wired_count)) {
730308c24baSJohn Dyson 			if (map->first_free == prev)
731308c24baSJohn Dyson 				map->first_free = entry;
732b18bfc3dSJohn Dyson 			if (map->hint == prev)
733b18bfc3dSJohn Dyson 				map->hint = entry;
734308c24baSJohn Dyson 			vm_map_entry_unlink(map, prev);
735308c24baSJohn Dyson 			entry->start = prev->start;
736308c24baSJohn Dyson 			entry->offset = prev->offset;
737b18bfc3dSJohn Dyson 			if (prev->object.vm_object)
738308c24baSJohn Dyson 				vm_object_deallocate(prev->object.vm_object);
739308c24baSJohn Dyson 			vm_map_entry_dispose(map, prev);
740308c24baSJohn Dyson 		}
741308c24baSJohn Dyson 	}
742de5f6a77SJohn Dyson 
743de5f6a77SJohn Dyson 	next = entry->next;
744308c24baSJohn Dyson 	if (next != &map->header) {
74567bf6868SJohn Dyson 		esize = entry->end - entry->start;
74667bf6868SJohn Dyson 		if ((entry->end == next->start) &&
74767bf6868SJohn Dyson 		    (next->object.vm_object == entry->object.vm_object) &&
74867bf6868SJohn Dyson 		     (!entry->object.vm_object ||
74967bf6868SJohn Dyson 			(entry->offset + esize == next->offset)) &&
750afa07f7eSJohn Dyson 		    (next->eflags == entry->eflags) &&
75167bf6868SJohn Dyson 		    (next->protection == entry->protection) &&
75267bf6868SJohn Dyson 		    (next->max_protection == entry->max_protection) &&
75367bf6868SJohn Dyson 		    (next->inheritance == entry->inheritance) &&
754b7b2aac2SJohn Dyson 		    (next->wired_count == entry->wired_count)) {
755308c24baSJohn Dyson 			if (map->first_free == next)
756308c24baSJohn Dyson 				map->first_free = entry;
757b18bfc3dSJohn Dyson 			if (map->hint == next)
758b18bfc3dSJohn Dyson 				map->hint = entry;
759de5f6a77SJohn Dyson 			vm_map_entry_unlink(map, next);
760de5f6a77SJohn Dyson 			entry->end = next->end;
761b18bfc3dSJohn Dyson 			if (next->object.vm_object)
762de5f6a77SJohn Dyson 				vm_object_deallocate(next->object.vm_object);
763de5f6a77SJohn Dyson 			vm_map_entry_dispose(map, next);
764df8bae1dSRodney W. Grimes 	        }
765df8bae1dSRodney W. Grimes 	}
766de5f6a77SJohn Dyson }
767df8bae1dSRodney W. Grimes /*
768df8bae1dSRodney W. Grimes  *	vm_map_clip_start:	[ internal use only ]
769df8bae1dSRodney W. Grimes  *
770df8bae1dSRodney W. Grimes  *	Asserts that the given entry begins at or after
771df8bae1dSRodney W. Grimes  *	the specified address; if necessary,
772df8bae1dSRodney W. Grimes  *	it splits the entry into two.
773df8bae1dSRodney W. Grimes  */
774df8bae1dSRodney W. Grimes #define vm_map_clip_start(map, entry, startaddr) \
775df8bae1dSRodney W. Grimes { \
776df8bae1dSRodney W. Grimes 	if (startaddr > entry->start) \
777df8bae1dSRodney W. Grimes 		_vm_map_clip_start(map, entry, startaddr); \
778df8bae1dSRodney W. Grimes }
779df8bae1dSRodney W. Grimes 
780df8bae1dSRodney W. Grimes /*
781df8bae1dSRodney W. Grimes  *	This routine is called only when it is known that
782df8bae1dSRodney W. Grimes  *	the entry must be split.
783df8bae1dSRodney W. Grimes  */
7840d94caffSDavid Greenman static void
7850d94caffSDavid Greenman _vm_map_clip_start(map, entry, start)
786c0877f10SJohn Dyson 	vm_map_t map;
787c0877f10SJohn Dyson 	vm_map_entry_t entry;
788c0877f10SJohn Dyson 	vm_offset_t start;
789df8bae1dSRodney W. Grimes {
790c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
791df8bae1dSRodney W. Grimes 
792df8bae1dSRodney W. Grimes 	/*
7930d94caffSDavid Greenman 	 * Split off the front portion -- note that we must insert the new
7940d94caffSDavid Greenman 	 * entry BEFORE this one, so that this entry has the specified
7950d94caffSDavid Greenman 	 * starting address.
796df8bae1dSRodney W. Grimes 	 */
797df8bae1dSRodney W. Grimes 
798f32dbbeeSJohn Dyson 	vm_map_simplify_entry(map, entry);
799f32dbbeeSJohn Dyson 
80011cccda1SJohn Dyson 	/*
80111cccda1SJohn Dyson 	 * If there is no object backing this entry, we might as well create
80211cccda1SJohn Dyson 	 * one now.  If we defer it, an object can get created after the map
80311cccda1SJohn Dyson 	 * is clipped, and individual objects will be created for the split-up
80411cccda1SJohn Dyson 	 * map.  This is a bit of a hack, but is also about the best place to
80511cccda1SJohn Dyson 	 * put this improvement.
80611cccda1SJohn Dyson 	 */
80711cccda1SJohn Dyson 
8084e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL && !map->system_map) {
80911cccda1SJohn Dyson 		vm_object_t object;
81011cccda1SJohn Dyson 		object = vm_object_allocate(OBJT_DEFAULT,
811c2e11a03SJohn Dyson 				atop(entry->end - entry->start));
81211cccda1SJohn Dyson 		entry->object.vm_object = object;
81311cccda1SJohn Dyson 		entry->offset = 0;
81411cccda1SJohn Dyson 	}
81511cccda1SJohn Dyson 
816df8bae1dSRodney W. Grimes 	new_entry = vm_map_entry_create(map);
817df8bae1dSRodney W. Grimes 	*new_entry = *entry;
818df8bae1dSRodney W. Grimes 
819df8bae1dSRodney W. Grimes 	new_entry->end = start;
820df8bae1dSRodney W. Grimes 	entry->offset += (start - entry->start);
821df8bae1dSRodney W. Grimes 	entry->start = start;
822df8bae1dSRodney W. Grimes 
823df8bae1dSRodney W. Grimes 	vm_map_entry_link(map, entry->prev, new_entry);
824df8bae1dSRodney W. Grimes 
8259fdfe602SMatthew Dillon 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
826df8bae1dSRodney W. Grimes 		vm_object_reference(new_entry->object.vm_object);
827df8bae1dSRodney W. Grimes 	}
828c0877f10SJohn Dyson }
829df8bae1dSRodney W. Grimes 
830df8bae1dSRodney W. Grimes /*
831df8bae1dSRodney W. Grimes  *	vm_map_clip_end:	[ internal use only ]
832df8bae1dSRodney W. Grimes  *
833df8bae1dSRodney W. Grimes  *	Asserts that the given entry ends at or before
834df8bae1dSRodney W. Grimes  *	the specified address; if necessary,
835df8bae1dSRodney W. Grimes  *	it splits the entry into two.
836df8bae1dSRodney W. Grimes  */
837df8bae1dSRodney W. Grimes 
838df8bae1dSRodney W. Grimes #define vm_map_clip_end(map, entry, endaddr) \
839df8bae1dSRodney W. Grimes { \
840df8bae1dSRodney W. Grimes 	if (endaddr < entry->end) \
841df8bae1dSRodney W. Grimes 		_vm_map_clip_end(map, entry, endaddr); \
842df8bae1dSRodney W. Grimes }
843df8bae1dSRodney W. Grimes 
844df8bae1dSRodney W. Grimes /*
845df8bae1dSRodney W. Grimes  *	This routine is called only when it is known that
846df8bae1dSRodney W. Grimes  *	the entry must be split.
847df8bae1dSRodney W. Grimes  */
8480d94caffSDavid Greenman static void
8490d94caffSDavid Greenman _vm_map_clip_end(map, entry, end)
850c0877f10SJohn Dyson 	vm_map_t map;
851c0877f10SJohn Dyson 	vm_map_entry_t entry;
852c0877f10SJohn Dyson 	vm_offset_t end;
853df8bae1dSRodney W. Grimes {
854c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
855df8bae1dSRodney W. Grimes 
856df8bae1dSRodney W. Grimes 	/*
85711cccda1SJohn Dyson 	 * If there is no object backing this entry, we might as well create
85811cccda1SJohn Dyson 	 * one now.  If we defer it, an object can get created after the map
85911cccda1SJohn Dyson 	 * is clipped, and individual objects will be created for the split-up
86011cccda1SJohn Dyson 	 * map.  This is a bit of a hack, but is also about the best place to
86111cccda1SJohn Dyson 	 * put this improvement.
86211cccda1SJohn Dyson 	 */
86311cccda1SJohn Dyson 
8644e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL && !map->system_map) {
86511cccda1SJohn Dyson 		vm_object_t object;
86611cccda1SJohn Dyson 		object = vm_object_allocate(OBJT_DEFAULT,
867c2e11a03SJohn Dyson 				atop(entry->end - entry->start));
86811cccda1SJohn Dyson 		entry->object.vm_object = object;
86911cccda1SJohn Dyson 		entry->offset = 0;
87011cccda1SJohn Dyson 	}
87111cccda1SJohn Dyson 
87211cccda1SJohn Dyson 	/*
8730d94caffSDavid Greenman 	 * Create a new entry and insert it AFTER the specified entry
874df8bae1dSRodney W. Grimes 	 */
875df8bae1dSRodney W. Grimes 
876df8bae1dSRodney W. Grimes 	new_entry = vm_map_entry_create(map);
877df8bae1dSRodney W. Grimes 	*new_entry = *entry;
878df8bae1dSRodney W. Grimes 
879df8bae1dSRodney W. Grimes 	new_entry->start = entry->end = end;
880df8bae1dSRodney W. Grimes 	new_entry->offset += (end - entry->start);
881df8bae1dSRodney W. Grimes 
882df8bae1dSRodney W. Grimes 	vm_map_entry_link(map, entry, new_entry);
883df8bae1dSRodney W. Grimes 
8849fdfe602SMatthew Dillon 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
885df8bae1dSRodney W. Grimes 		vm_object_reference(new_entry->object.vm_object);
886df8bae1dSRodney W. Grimes 	}
887c0877f10SJohn Dyson }
888df8bae1dSRodney W. Grimes 
889df8bae1dSRodney W. Grimes /*
890df8bae1dSRodney W. Grimes  *	VM_MAP_RANGE_CHECK:	[ internal use only ]
891df8bae1dSRodney W. Grimes  *
892df8bae1dSRodney W. Grimes  *	Asserts that the starting and ending region
893df8bae1dSRodney W. Grimes  *	addresses fall within the valid range of the map.
894df8bae1dSRodney W. Grimes  */
895df8bae1dSRodney W. Grimes #define	VM_MAP_RANGE_CHECK(map, start, end)		\
896df8bae1dSRodney W. Grimes 		{					\
897df8bae1dSRodney W. Grimes 		if (start < vm_map_min(map))		\
898df8bae1dSRodney W. Grimes 			start = vm_map_min(map);	\
899df8bae1dSRodney W. Grimes 		if (end > vm_map_max(map))		\
900df8bae1dSRodney W. Grimes 			end = vm_map_max(map);		\
901df8bae1dSRodney W. Grimes 		if (start > end)			\
902df8bae1dSRodney W. Grimes 			start = end;			\
903df8bae1dSRodney W. Grimes 		}
904df8bae1dSRodney W. Grimes 
905df8bae1dSRodney W. Grimes /*
906df8bae1dSRodney W. Grimes  *	vm_map_submap:		[ kernel use only ]
907df8bae1dSRodney W. Grimes  *
908df8bae1dSRodney W. Grimes  *	Mark the given range as handled by a subordinate map.
909df8bae1dSRodney W. Grimes  *
910df8bae1dSRodney W. Grimes  *	This range must have been created with vm_map_find,
911df8bae1dSRodney W. Grimes  *	and no other operations may have been performed on this
912df8bae1dSRodney W. Grimes  *	range prior to calling vm_map_submap.
913df8bae1dSRodney W. Grimes  *
914df8bae1dSRodney W. Grimes  *	Only a limited number of operations can be performed
915df8bae1dSRodney W. Grimes  *	within this rage after calling vm_map_submap:
916df8bae1dSRodney W. Grimes  *		vm_fault
917df8bae1dSRodney W. Grimes  *	[Don't try vm_map_copy!]
918df8bae1dSRodney W. Grimes  *
919df8bae1dSRodney W. Grimes  *	To remove a submapping, one must first remove the
920df8bae1dSRodney W. Grimes  *	range from the superior map, and then destroy the
921df8bae1dSRodney W. Grimes  *	submap (if desired).  [Better yet, don't try it.]
922df8bae1dSRodney W. Grimes  */
923df8bae1dSRodney W. Grimes int
924df8bae1dSRodney W. Grimes vm_map_submap(map, start, end, submap)
925c0877f10SJohn Dyson 	vm_map_t map;
926c0877f10SJohn Dyson 	vm_offset_t start;
927c0877f10SJohn Dyson 	vm_offset_t end;
928df8bae1dSRodney W. Grimes 	vm_map_t submap;
929df8bae1dSRodney W. Grimes {
930df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
931c0877f10SJohn Dyson 	int result = KERN_INVALID_ARGUMENT;
932df8bae1dSRodney W. Grimes 
933df8bae1dSRodney W. Grimes 	vm_map_lock(map);
934df8bae1dSRodney W. Grimes 
935df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
936df8bae1dSRodney W. Grimes 
937df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &entry)) {
938df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
9390d94caffSDavid Greenman 	} else
940df8bae1dSRodney W. Grimes 		entry = entry->next;
941df8bae1dSRodney W. Grimes 
942df8bae1dSRodney W. Grimes 	vm_map_clip_end(map, entry, end);
943df8bae1dSRodney W. Grimes 
944df8bae1dSRodney W. Grimes 	if ((entry->start == start) && (entry->end == end) &&
9459fdfe602SMatthew Dillon 	    ((entry->eflags & MAP_ENTRY_COW) == 0) &&
946afa07f7eSJohn Dyson 	    (entry->object.vm_object == NULL)) {
9472d8acc0fSJohn Dyson 		entry->object.sub_map = submap;
948afa07f7eSJohn Dyson 		entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
949df8bae1dSRodney W. Grimes 		result = KERN_SUCCESS;
950df8bae1dSRodney W. Grimes 	}
951df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
952df8bae1dSRodney W. Grimes 
953df8bae1dSRodney W. Grimes 	return (result);
954df8bae1dSRodney W. Grimes }
955df8bae1dSRodney W. Grimes 
956df8bae1dSRodney W. Grimes /*
957df8bae1dSRodney W. Grimes  *	vm_map_protect:
958df8bae1dSRodney W. Grimes  *
959df8bae1dSRodney W. Grimes  *	Sets the protection of the specified address
960df8bae1dSRodney W. Grimes  *	region in the target map.  If "set_max" is
961df8bae1dSRodney W. Grimes  *	specified, the maximum protection is to be set;
962df8bae1dSRodney W. Grimes  *	otherwise, only the current protection is affected.
963df8bae1dSRodney W. Grimes  */
964df8bae1dSRodney W. Grimes int
965b9dcd593SBruce Evans vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
966b9dcd593SBruce Evans 	       vm_prot_t new_prot, boolean_t set_max)
967df8bae1dSRodney W. Grimes {
968c0877f10SJohn Dyson 	vm_map_entry_t current;
969df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
970df8bae1dSRodney W. Grimes 
971df8bae1dSRodney W. Grimes 	vm_map_lock(map);
972df8bae1dSRodney W. Grimes 
973df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
974df8bae1dSRodney W. Grimes 
975df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &entry)) {
976df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
977b7b2aac2SJohn Dyson 	} else {
978df8bae1dSRodney W. Grimes 		entry = entry->next;
979b7b2aac2SJohn Dyson 	}
980df8bae1dSRodney W. Grimes 
981df8bae1dSRodney W. Grimes 	/*
9820d94caffSDavid Greenman 	 * Make a first pass to check for protection violations.
983df8bae1dSRodney W. Grimes 	 */
984df8bae1dSRodney W. Grimes 
985df8bae1dSRodney W. Grimes 	current = entry;
986df8bae1dSRodney W. Grimes 	while ((current != &map->header) && (current->start < end)) {
987afa07f7eSJohn Dyson 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
988a1f6d91cSDavid Greenman 			vm_map_unlock(map);
989df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ARGUMENT);
990a1f6d91cSDavid Greenman 		}
991df8bae1dSRodney W. Grimes 		if ((new_prot & current->max_protection) != new_prot) {
992df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
993df8bae1dSRodney W. Grimes 			return (KERN_PROTECTION_FAILURE);
994df8bae1dSRodney W. Grimes 		}
995df8bae1dSRodney W. Grimes 		current = current->next;
996df8bae1dSRodney W. Grimes 	}
997df8bae1dSRodney W. Grimes 
998df8bae1dSRodney W. Grimes 	/*
9990d94caffSDavid Greenman 	 * Go back and fix up protections. [Note that clipping is not
10000d94caffSDavid Greenman 	 * necessary the second time.]
1001df8bae1dSRodney W. Grimes 	 */
1002df8bae1dSRodney W. Grimes 
1003df8bae1dSRodney W. Grimes 	current = entry;
1004df8bae1dSRodney W. Grimes 
1005df8bae1dSRodney W. Grimes 	while ((current != &map->header) && (current->start < end)) {
1006df8bae1dSRodney W. Grimes 		vm_prot_t old_prot;
1007df8bae1dSRodney W. Grimes 
1008df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, current, end);
1009df8bae1dSRodney W. Grimes 
1010df8bae1dSRodney W. Grimes 		old_prot = current->protection;
1011df8bae1dSRodney W. Grimes 		if (set_max)
1012df8bae1dSRodney W. Grimes 			current->protection =
1013df8bae1dSRodney W. Grimes 			    (current->max_protection = new_prot) &
1014df8bae1dSRodney W. Grimes 			    old_prot;
1015df8bae1dSRodney W. Grimes 		else
1016df8bae1dSRodney W. Grimes 			current->protection = new_prot;
1017df8bae1dSRodney W. Grimes 
1018df8bae1dSRodney W. Grimes 		/*
10190d94caffSDavid Greenman 		 * Update physical map if necessary. Worry about copy-on-write
10200d94caffSDavid Greenman 		 * here -- CHECK THIS XXX
1021df8bae1dSRodney W. Grimes 		 */
1022df8bae1dSRodney W. Grimes 
1023df8bae1dSRodney W. Grimes 		if (current->protection != old_prot) {
1024afa07f7eSJohn Dyson #define MASK(entry)	(((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1025df8bae1dSRodney W. Grimes 							VM_PROT_ALL)
1026df8bae1dSRodney W. Grimes 
1027df8bae1dSRodney W. Grimes 			pmap_protect(map->pmap, current->start,
1028df8bae1dSRodney W. Grimes 			    current->end,
10291c85e3dfSAlan Cox 			    current->protection & MASK(current));
1030df8bae1dSRodney W. Grimes #undef	MASK
1031df8bae1dSRodney W. Grimes 		}
10327d78abc9SJohn Dyson 
10337d78abc9SJohn Dyson 		vm_map_simplify_entry(map, current);
10347d78abc9SJohn Dyson 
1035df8bae1dSRodney W. Grimes 		current = current->next;
1036df8bae1dSRodney W. Grimes 	}
1037df8bae1dSRodney W. Grimes 
1038df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1039df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
1040df8bae1dSRodney W. Grimes }
1041df8bae1dSRodney W. Grimes 
1042df8bae1dSRodney W. Grimes /*
1043867a482dSJohn Dyson  *	vm_map_madvise:
1044867a482dSJohn Dyson  *
1045867a482dSJohn Dyson  * 	This routine traverses a processes map handling the madvise
1046f7fc307aSAlan Cox  *	system call.  Advisories are classified as either those effecting
1047f7fc307aSAlan Cox  *	the vm_map_entry structure, or those effecting the underlying
1048f7fc307aSAlan Cox  *	objects.
1049867a482dSJohn Dyson  */
1050b4309055SMatthew Dillon 
1051b4309055SMatthew Dillon int
1052f7fc307aSAlan Cox vm_map_madvise(map, start, end, behav)
1053867a482dSJohn Dyson 	vm_map_t map;
1054867a482dSJohn Dyson 	vm_offset_t start, end;
1055f7fc307aSAlan Cox 	int behav;
1056867a482dSJohn Dyson {
1057f7fc307aSAlan Cox 	vm_map_entry_t current, entry;
1058b4309055SMatthew Dillon 	int modify_map = 0;
1059867a482dSJohn Dyson 
1060b4309055SMatthew Dillon 	/*
1061b4309055SMatthew Dillon 	 * Some madvise calls directly modify the vm_map_entry, in which case
1062b4309055SMatthew Dillon 	 * we need to use an exclusive lock on the map and we need to perform
1063b4309055SMatthew Dillon 	 * various clipping operations.  Otherwise we only need a read-lock
1064b4309055SMatthew Dillon 	 * on the map.
1065b4309055SMatthew Dillon 	 */
1066f7fc307aSAlan Cox 
1067b4309055SMatthew Dillon 	switch(behav) {
1068b4309055SMatthew Dillon 	case MADV_NORMAL:
1069b4309055SMatthew Dillon 	case MADV_SEQUENTIAL:
1070b4309055SMatthew Dillon 	case MADV_RANDOM:
10714f79d873SMatthew Dillon 	case MADV_NOSYNC:
10724f79d873SMatthew Dillon 	case MADV_AUTOSYNC:
10739730a5daSPaul Saab 	case MADV_NOCORE:
10749730a5daSPaul Saab 	case MADV_CORE:
1075b4309055SMatthew Dillon 		modify_map = 1;
1076867a482dSJohn Dyson 		vm_map_lock(map);
1077b4309055SMatthew Dillon 		break;
1078b4309055SMatthew Dillon 	case MADV_WILLNEED:
1079b4309055SMatthew Dillon 	case MADV_DONTNEED:
1080b4309055SMatthew Dillon 	case MADV_FREE:
1081f7fc307aSAlan Cox 		vm_map_lock_read(map);
1082b4309055SMatthew Dillon 		break;
1083b4309055SMatthew Dillon 	default:
1084b4309055SMatthew Dillon 		return (KERN_INVALID_ARGUMENT);
1085b4309055SMatthew Dillon 	}
1086b4309055SMatthew Dillon 
1087b4309055SMatthew Dillon 	/*
1088b4309055SMatthew Dillon 	 * Locate starting entry and clip if necessary.
1089b4309055SMatthew Dillon 	 */
1090867a482dSJohn Dyson 
1091867a482dSJohn Dyson 	VM_MAP_RANGE_CHECK(map, start, end);
1092867a482dSJohn Dyson 
1093867a482dSJohn Dyson 	if (vm_map_lookup_entry(map, start, &entry)) {
1094f7fc307aSAlan Cox 		if (modify_map)
1095867a482dSJohn Dyson 			vm_map_clip_start(map, entry, start);
1096b4309055SMatthew Dillon 	} else {
1097867a482dSJohn Dyson 		entry = entry->next;
1098b4309055SMatthew Dillon 	}
1099867a482dSJohn Dyson 
1100f7fc307aSAlan Cox 	if (modify_map) {
1101f7fc307aSAlan Cox 		/*
1102f7fc307aSAlan Cox 		 * madvise behaviors that are implemented in the vm_map_entry.
1103f7fc307aSAlan Cox 		 *
1104f7fc307aSAlan Cox 		 * We clip the vm_map_entry so that behavioral changes are
1105f7fc307aSAlan Cox 		 * limited to the specified address range.
1106f7fc307aSAlan Cox 		 */
1107867a482dSJohn Dyson 		for (current = entry;
1108867a482dSJohn Dyson 		     (current != &map->header) && (current->start < end);
1109b4309055SMatthew Dillon 		     current = current->next
1110b4309055SMatthew Dillon 		) {
1111f7fc307aSAlan Cox 			if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1112867a482dSJohn Dyson 				continue;
1113fed9a903SJohn Dyson 
111447221757SJohn Dyson 			vm_map_clip_end(map, current, end);
1115fed9a903SJohn Dyson 
1116f7fc307aSAlan Cox 			switch (behav) {
1117867a482dSJohn Dyson 			case MADV_NORMAL:
11187f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
1119867a482dSJohn Dyson 				break;
1120867a482dSJohn Dyson 			case MADV_SEQUENTIAL:
11217f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
1122867a482dSJohn Dyson 				break;
1123867a482dSJohn Dyson 			case MADV_RANDOM:
11247f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
1125867a482dSJohn Dyson 				break;
11264f79d873SMatthew Dillon 			case MADV_NOSYNC:
11274f79d873SMatthew Dillon 				current->eflags |= MAP_ENTRY_NOSYNC;
11284f79d873SMatthew Dillon 				break;
11294f79d873SMatthew Dillon 			case MADV_AUTOSYNC:
11304f79d873SMatthew Dillon 				current->eflags &= ~MAP_ENTRY_NOSYNC;
11314f79d873SMatthew Dillon 				break;
11329730a5daSPaul Saab 			case MADV_NOCORE:
11339730a5daSPaul Saab 				current->eflags |= MAP_ENTRY_NOCOREDUMP;
11349730a5daSPaul Saab 				break;
11359730a5daSPaul Saab 			case MADV_CORE:
11369730a5daSPaul Saab 				current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
11379730a5daSPaul Saab 				break;
1138867a482dSJohn Dyson 			default:
1139867a482dSJohn Dyson 				break;
1140867a482dSJohn Dyson 			}
1141f7fc307aSAlan Cox 			vm_map_simplify_entry(map, current);
1142867a482dSJohn Dyson 		}
1143867a482dSJohn Dyson 		vm_map_unlock(map);
1144b4309055SMatthew Dillon 	} else {
1145f7fc307aSAlan Cox 		vm_pindex_t pindex;
1146f7fc307aSAlan Cox 		int count;
1147f7fc307aSAlan Cox 
1148f7fc307aSAlan Cox 		/*
1149f7fc307aSAlan Cox 		 * madvise behaviors that are implemented in the underlying
1150f7fc307aSAlan Cox 		 * vm_object.
1151f7fc307aSAlan Cox 		 *
1152f7fc307aSAlan Cox 		 * Since we don't clip the vm_map_entry, we have to clip
1153f7fc307aSAlan Cox 		 * the vm_object pindex and count.
1154f7fc307aSAlan Cox 		 */
1155f7fc307aSAlan Cox 		for (current = entry;
1156f7fc307aSAlan Cox 		     (current != &map->header) && (current->start < end);
1157b4309055SMatthew Dillon 		     current = current->next
1158b4309055SMatthew Dillon 		) {
11595f99b57cSMatthew Dillon 			vm_offset_t useStart;
11605f99b57cSMatthew Dillon 
1161f7fc307aSAlan Cox 			if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1162f7fc307aSAlan Cox 				continue;
1163f7fc307aSAlan Cox 
1164f7fc307aSAlan Cox 			pindex = OFF_TO_IDX(current->offset);
1165f7fc307aSAlan Cox 			count = atop(current->end - current->start);
11665f99b57cSMatthew Dillon 			useStart = current->start;
1167f7fc307aSAlan Cox 
1168f7fc307aSAlan Cox 			if (current->start < start) {
1169f7fc307aSAlan Cox 				pindex += atop(start - current->start);
1170f7fc307aSAlan Cox 				count -= atop(start - current->start);
11715f99b57cSMatthew Dillon 				useStart = start;
1172f7fc307aSAlan Cox 			}
1173f7fc307aSAlan Cox 			if (current->end > end)
1174f7fc307aSAlan Cox 				count -= atop(current->end - end);
1175f7fc307aSAlan Cox 
1176f7fc307aSAlan Cox 			if (count <= 0)
1177f7fc307aSAlan Cox 				continue;
1178f7fc307aSAlan Cox 
1179f7fc307aSAlan Cox 			vm_object_madvise(current->object.vm_object,
1180f7fc307aSAlan Cox 					  pindex, count, behav);
1181b4309055SMatthew Dillon 			if (behav == MADV_WILLNEED) {
1182b4309055SMatthew Dillon 				pmap_object_init_pt(
1183b4309055SMatthew Dillon 				    map->pmap,
11845f99b57cSMatthew Dillon 				    useStart,
1185f7fc307aSAlan Cox 				    current->object.vm_object,
1186b4309055SMatthew Dillon 				    pindex,
1187b4309055SMatthew Dillon 				    (count << PAGE_SHIFT),
1188b4309055SMatthew Dillon 				    0
1189b4309055SMatthew Dillon 				);
1190f7fc307aSAlan Cox 			}
1191f7fc307aSAlan Cox 		}
1192f7fc307aSAlan Cox 		vm_map_unlock_read(map);
1193f7fc307aSAlan Cox 	}
1194b4309055SMatthew Dillon 	return(0);
1195867a482dSJohn Dyson }
1196867a482dSJohn Dyson 
1197867a482dSJohn Dyson 
1198867a482dSJohn Dyson /*
1199df8bae1dSRodney W. Grimes  *	vm_map_inherit:
1200df8bae1dSRodney W. Grimes  *
1201df8bae1dSRodney W. Grimes  *	Sets the inheritance of the specified address
1202df8bae1dSRodney W. Grimes  *	range in the target map.  Inheritance
1203df8bae1dSRodney W. Grimes  *	affects how the map will be shared with
1204df8bae1dSRodney W. Grimes  *	child maps at the time of vm_map_fork.
1205df8bae1dSRodney W. Grimes  */
1206df8bae1dSRodney W. Grimes int
1207b9dcd593SBruce Evans vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
1208b9dcd593SBruce Evans 	       vm_inherit_t new_inheritance)
1209df8bae1dSRodney W. Grimes {
1210c0877f10SJohn Dyson 	vm_map_entry_t entry;
1211df8bae1dSRodney W. Grimes 	vm_map_entry_t temp_entry;
1212df8bae1dSRodney W. Grimes 
1213df8bae1dSRodney W. Grimes 	switch (new_inheritance) {
1214df8bae1dSRodney W. Grimes 	case VM_INHERIT_NONE:
1215df8bae1dSRodney W. Grimes 	case VM_INHERIT_COPY:
1216df8bae1dSRodney W. Grimes 	case VM_INHERIT_SHARE:
1217df8bae1dSRodney W. Grimes 		break;
1218df8bae1dSRodney W. Grimes 	default:
1219df8bae1dSRodney W. Grimes 		return (KERN_INVALID_ARGUMENT);
1220df8bae1dSRodney W. Grimes 	}
1221df8bae1dSRodney W. Grimes 
1222df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1223df8bae1dSRodney W. Grimes 
1224df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1225df8bae1dSRodney W. Grimes 
1226df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &temp_entry)) {
1227df8bae1dSRodney W. Grimes 		entry = temp_entry;
1228df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
12290d94caffSDavid Greenman 	} else
1230df8bae1dSRodney W. Grimes 		entry = temp_entry->next;
1231df8bae1dSRodney W. Grimes 
1232df8bae1dSRodney W. Grimes 	while ((entry != &map->header) && (entry->start < end)) {
1233df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, entry, end);
1234df8bae1dSRodney W. Grimes 
1235df8bae1dSRodney W. Grimes 		entry->inheritance = new_inheritance;
1236df8bae1dSRodney W. Grimes 
123744428f62SAlan Cox 		vm_map_simplify_entry(map, entry);
123844428f62SAlan Cox 
1239df8bae1dSRodney W. Grimes 		entry = entry->next;
1240df8bae1dSRodney W. Grimes 	}
1241df8bae1dSRodney W. Grimes 
1242df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1243df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
1244df8bae1dSRodney W. Grimes }
1245df8bae1dSRodney W. Grimes 
1246df8bae1dSRodney W. Grimes /*
12477aaaa4fdSJohn Dyson  * Implement the semantics of mlock
12487aaaa4fdSJohn Dyson  */
12497aaaa4fdSJohn Dyson int
12507aaaa4fdSJohn Dyson vm_map_user_pageable(map, start, end, new_pageable)
1251c0877f10SJohn Dyson 	vm_map_t map;
1252c0877f10SJohn Dyson 	vm_offset_t start;
1253c0877f10SJohn Dyson 	vm_offset_t end;
1254c0877f10SJohn Dyson 	boolean_t new_pageable;
12557aaaa4fdSJohn Dyson {
1256b44959ceSTor Egge 	vm_map_entry_t entry;
12577aaaa4fdSJohn Dyson 	vm_map_entry_t start_entry;
1258b44959ceSTor Egge 	vm_offset_t estart;
12597aaaa4fdSJohn Dyson 	int rv;
12607aaaa4fdSJohn Dyson 
12617aaaa4fdSJohn Dyson 	vm_map_lock(map);
12627aaaa4fdSJohn Dyson 	VM_MAP_RANGE_CHECK(map, start, end);
12637aaaa4fdSJohn Dyson 
12647aaaa4fdSJohn Dyson 	if (vm_map_lookup_entry(map, start, &start_entry) == FALSE) {
12657aaaa4fdSJohn Dyson 		vm_map_unlock(map);
12667aaaa4fdSJohn Dyson 		return (KERN_INVALID_ADDRESS);
12677aaaa4fdSJohn Dyson 	}
12687aaaa4fdSJohn Dyson 
12697aaaa4fdSJohn Dyson 	if (new_pageable) {
12707aaaa4fdSJohn Dyson 
12717aaaa4fdSJohn Dyson 		entry = start_entry;
12727aaaa4fdSJohn Dyson 		vm_map_clip_start(map, entry, start);
12737aaaa4fdSJohn Dyson 
12747aaaa4fdSJohn Dyson 		/*
12757aaaa4fdSJohn Dyson 		 * Now decrement the wiring count for each region. If a region
12767aaaa4fdSJohn Dyson 		 * becomes completely unwired, unwire its physical pages and
12777aaaa4fdSJohn Dyson 		 * mappings.
12787aaaa4fdSJohn Dyson 		 */
12797aaaa4fdSJohn Dyson 		while ((entry != &map->header) && (entry->start < end)) {
1280afa07f7eSJohn Dyson 			if (entry->eflags & MAP_ENTRY_USER_WIRED) {
12817aaaa4fdSJohn Dyson 				vm_map_clip_end(map, entry, end);
1282afa07f7eSJohn Dyson 				entry->eflags &= ~MAP_ENTRY_USER_WIRED;
12837aaaa4fdSJohn Dyson 				entry->wired_count--;
12847aaaa4fdSJohn Dyson 				if (entry->wired_count == 0)
12857aaaa4fdSJohn Dyson 					vm_fault_unwire(map, entry->start, entry->end);
12867aaaa4fdSJohn Dyson 			}
1287b44959ceSTor Egge 			vm_map_simplify_entry(map,entry);
12887aaaa4fdSJohn Dyson 			entry = entry->next;
12897aaaa4fdSJohn Dyson 		}
12907aaaa4fdSJohn Dyson 	} else {
12917aaaa4fdSJohn Dyson 
12927aaaa4fdSJohn Dyson 		entry = start_entry;
12937aaaa4fdSJohn Dyson 
12947aaaa4fdSJohn Dyson 		while ((entry != &map->header) && (entry->start < end)) {
12957aaaa4fdSJohn Dyson 
1296afa07f7eSJohn Dyson 			if (entry->eflags & MAP_ENTRY_USER_WIRED) {
12977aaaa4fdSJohn Dyson 				entry = entry->next;
12987aaaa4fdSJohn Dyson 				continue;
12997aaaa4fdSJohn Dyson 			}
13007aaaa4fdSJohn Dyson 
13017aaaa4fdSJohn Dyson 			if (entry->wired_count != 0) {
13027aaaa4fdSJohn Dyson 				entry->wired_count++;
1303afa07f7eSJohn Dyson 				entry->eflags |= MAP_ENTRY_USER_WIRED;
13047aaaa4fdSJohn Dyson 				entry = entry->next;
13057aaaa4fdSJohn Dyson 				continue;
13067aaaa4fdSJohn Dyson 			}
13077aaaa4fdSJohn Dyson 
13087aaaa4fdSJohn Dyson 			/* Here on entry being newly wired */
13097aaaa4fdSJohn Dyson 
13109fdfe602SMatthew Dillon 			if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1311afa07f7eSJohn Dyson 				int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY;
13127aaaa4fdSJohn Dyson 				if (copyflag && ((entry->protection & VM_PROT_WRITE) != 0)) {
13137aaaa4fdSJohn Dyson 
13147aaaa4fdSJohn Dyson 					vm_object_shadow(&entry->object.vm_object,
13157aaaa4fdSJohn Dyson 					    &entry->offset,
1316c2e11a03SJohn Dyson 					    atop(entry->end - entry->start));
1317afa07f7eSJohn Dyson 					entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
13187aaaa4fdSJohn Dyson 
13194e71e795SMatthew Dillon 				} else if (entry->object.vm_object == NULL &&
13204e71e795SMatthew Dillon 					   !map->system_map) {
13217aaaa4fdSJohn Dyson 
13227aaaa4fdSJohn Dyson 					entry->object.vm_object =
13237aaaa4fdSJohn Dyson 					    vm_object_allocate(OBJT_DEFAULT,
1324c2e11a03SJohn Dyson 						atop(entry->end - entry->start));
13257aaaa4fdSJohn Dyson 					entry->offset = (vm_offset_t) 0;
13267aaaa4fdSJohn Dyson 
13277aaaa4fdSJohn Dyson 				}
13287aaaa4fdSJohn Dyson 			}
13297aaaa4fdSJohn Dyson 
13307aaaa4fdSJohn Dyson 			vm_map_clip_start(map, entry, start);
13317aaaa4fdSJohn Dyson 			vm_map_clip_end(map, entry, end);
13327aaaa4fdSJohn Dyson 
13337aaaa4fdSJohn Dyson 			entry->wired_count++;
1334afa07f7eSJohn Dyson 			entry->eflags |= MAP_ENTRY_USER_WIRED;
1335b44959ceSTor Egge 			estart = entry->start;
13367aaaa4fdSJohn Dyson 
13377aaaa4fdSJohn Dyson 			/* First we need to allow map modifications */
1338996c772fSJohn Dyson 			vm_map_set_recursive(map);
133903e9c6c1SJohn Dyson 			vm_map_lock_downgrade(map);
134047221757SJohn Dyson 			map->timestamp++;
13417aaaa4fdSJohn Dyson 
13427aaaa4fdSJohn Dyson 			rv = vm_fault_user_wire(map, entry->start, entry->end);
13437aaaa4fdSJohn Dyson 			if (rv) {
13447aaaa4fdSJohn Dyson 
13457aaaa4fdSJohn Dyson 				entry->wired_count--;
1346afa07f7eSJohn Dyson 				entry->eflags &= ~MAP_ENTRY_USER_WIRED;
13477aaaa4fdSJohn Dyson 
1348996c772fSJohn Dyson 				vm_map_clear_recursive(map);
13497aaaa4fdSJohn Dyson 				vm_map_unlock(map);
13507aaaa4fdSJohn Dyson 
13517aaaa4fdSJohn Dyson 				(void) vm_map_user_pageable(map, start, entry->start, TRUE);
13527aaaa4fdSJohn Dyson 				return rv;
13537aaaa4fdSJohn Dyson 			}
13547aaaa4fdSJohn Dyson 
1355996c772fSJohn Dyson 			vm_map_clear_recursive(map);
1356b44959ceSTor Egge 			if (vm_map_lock_upgrade(map)) {
1357b44959ceSTor Egge 				vm_map_lock(map);
1358b44959ceSTor Egge 				if (vm_map_lookup_entry(map, estart, &entry)
1359b44959ceSTor Egge 				    == FALSE) {
1360b44959ceSTor Egge 					vm_map_unlock(map);
1361b44959ceSTor Egge 					(void) vm_map_user_pageable(map,
1362b44959ceSTor Egge 								    start,
1363b44959ceSTor Egge 								    estart,
1364b44959ceSTor Egge 								    TRUE);
1365b44959ceSTor Egge 					return (KERN_INVALID_ADDRESS);
1366b44959ceSTor Egge 				}
1367b44959ceSTor Egge 			}
1368b44959ceSTor Egge 			vm_map_simplify_entry(map,entry);
13697aaaa4fdSJohn Dyson 		}
13707aaaa4fdSJohn Dyson 	}
137147221757SJohn Dyson 	map->timestamp++;
13727aaaa4fdSJohn Dyson 	vm_map_unlock(map);
13737aaaa4fdSJohn Dyson 	return KERN_SUCCESS;
13747aaaa4fdSJohn Dyson }
13757aaaa4fdSJohn Dyson 
13767aaaa4fdSJohn Dyson /*
1377df8bae1dSRodney W. Grimes  *	vm_map_pageable:
1378df8bae1dSRodney W. Grimes  *
1379df8bae1dSRodney W. Grimes  *	Sets the pageability of the specified address
1380df8bae1dSRodney W. Grimes  *	range in the target map.  Regions specified
1381df8bae1dSRodney W. Grimes  *	as not pageable require locked-down physical
1382df8bae1dSRodney W. Grimes  *	memory and physical page maps.
1383df8bae1dSRodney W. Grimes  *
1384df8bae1dSRodney W. Grimes  *	The map must not be locked, but a reference
1385df8bae1dSRodney W. Grimes  *	must remain to the map throughout the call.
1386df8bae1dSRodney W. Grimes  */
1387df8bae1dSRodney W. Grimes int
1388df8bae1dSRodney W. Grimes vm_map_pageable(map, start, end, new_pageable)
1389c0877f10SJohn Dyson 	vm_map_t map;
1390c0877f10SJohn Dyson 	vm_offset_t start;
1391c0877f10SJohn Dyson 	vm_offset_t end;
1392c0877f10SJohn Dyson 	boolean_t new_pageable;
1393df8bae1dSRodney W. Grimes {
1394c0877f10SJohn Dyson 	vm_map_entry_t entry;
1395df8bae1dSRodney W. Grimes 	vm_map_entry_t start_entry;
1396c0877f10SJohn Dyson 	vm_offset_t failed = 0;
1397df8bae1dSRodney W. Grimes 	int rv;
1398df8bae1dSRodney W. Grimes 
1399df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1400df8bae1dSRodney W. Grimes 
1401df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1402df8bae1dSRodney W. Grimes 
1403df8bae1dSRodney W. Grimes 	/*
14040d94caffSDavid Greenman 	 * Only one pageability change may take place at one time, since
14050d94caffSDavid Greenman 	 * vm_fault assumes it will be called only once for each
14060d94caffSDavid Greenman 	 * wiring/unwiring.  Therefore, we have to make sure we're actually
14070d94caffSDavid Greenman 	 * changing the pageability for the entire region.  We do so before
14080d94caffSDavid Greenman 	 * making any changes.
1409df8bae1dSRodney W. Grimes 	 */
1410df8bae1dSRodney W. Grimes 
1411df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &start_entry) == FALSE) {
1412df8bae1dSRodney W. Grimes 		vm_map_unlock(map);
1413df8bae1dSRodney W. Grimes 		return (KERN_INVALID_ADDRESS);
1414df8bae1dSRodney W. Grimes 	}
1415df8bae1dSRodney W. Grimes 	entry = start_entry;
1416df8bae1dSRodney W. Grimes 
1417df8bae1dSRodney W. Grimes 	/*
14180d94caffSDavid Greenman 	 * Actions are rather different for wiring and unwiring, so we have
14190d94caffSDavid Greenman 	 * two separate cases.
1420df8bae1dSRodney W. Grimes 	 */
1421df8bae1dSRodney W. Grimes 
1422df8bae1dSRodney W. Grimes 	if (new_pageable) {
1423df8bae1dSRodney W. Grimes 
1424df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
1425df8bae1dSRodney W. Grimes 
1426df8bae1dSRodney W. Grimes 		/*
14270d94caffSDavid Greenman 		 * Unwiring.  First ensure that the range to be unwired is
14280d94caffSDavid Greenman 		 * really wired down and that there are no holes.
1429df8bae1dSRodney W. Grimes 		 */
1430df8bae1dSRodney W. Grimes 		while ((entry != &map->header) && (entry->start < end)) {
1431df8bae1dSRodney W. Grimes 
1432df8bae1dSRodney W. Grimes 			if (entry->wired_count == 0 ||
1433df8bae1dSRodney W. Grimes 			    (entry->end < end &&
1434df8bae1dSRodney W. Grimes 				(entry->next == &map->header ||
1435df8bae1dSRodney W. Grimes 				    entry->next->start > entry->end))) {
1436df8bae1dSRodney W. Grimes 				vm_map_unlock(map);
1437df8bae1dSRodney W. Grimes 				return (KERN_INVALID_ARGUMENT);
1438df8bae1dSRodney W. Grimes 			}
1439df8bae1dSRodney W. Grimes 			entry = entry->next;
1440df8bae1dSRodney W. Grimes 		}
1441df8bae1dSRodney W. Grimes 
1442df8bae1dSRodney W. Grimes 		/*
14430d94caffSDavid Greenman 		 * Now decrement the wiring count for each region. If a region
14440d94caffSDavid Greenman 		 * becomes completely unwired, unwire its physical pages and
14450d94caffSDavid Greenman 		 * mappings.
1446df8bae1dSRodney W. Grimes 		 */
1447df8bae1dSRodney W. Grimes 		entry = start_entry;
1448df8bae1dSRodney W. Grimes 		while ((entry != &map->header) && (entry->start < end)) {
1449df8bae1dSRodney W. Grimes 			vm_map_clip_end(map, entry, end);
1450df8bae1dSRodney W. Grimes 
1451df8bae1dSRodney W. Grimes 			entry->wired_count--;
1452df8bae1dSRodney W. Grimes 			if (entry->wired_count == 0)
1453df8bae1dSRodney W. Grimes 				vm_fault_unwire(map, entry->start, entry->end);
1454df8bae1dSRodney W. Grimes 
145544428f62SAlan Cox 			vm_map_simplify_entry(map, entry);
145644428f62SAlan Cox 
1457df8bae1dSRodney W. Grimes 			entry = entry->next;
1458df8bae1dSRodney W. Grimes 		}
14590d94caffSDavid Greenman 	} else {
1460df8bae1dSRodney W. Grimes 		/*
1461df8bae1dSRodney W. Grimes 		 * Wiring.  We must do this in two passes:
1462df8bae1dSRodney W. Grimes 		 *
14630d94caffSDavid Greenman 		 * 1.  Holding the write lock, we create any shadow or zero-fill
14640d94caffSDavid Greenman 		 * objects that need to be created. Then we clip each map
14650d94caffSDavid Greenman 		 * entry to the region to be wired and increment its wiring
14660d94caffSDavid Greenman 		 * count.  We create objects before clipping the map entries
1467df8bae1dSRodney W. Grimes 		 * to avoid object proliferation.
1468df8bae1dSRodney W. Grimes 		 *
14690d94caffSDavid Greenman 		 * 2.  We downgrade to a read lock, and call vm_fault_wire to
14700d94caffSDavid Greenman 		 * fault in the pages for any newly wired area (wired_count is
14710d94caffSDavid Greenman 		 * 1).
1472df8bae1dSRodney W. Grimes 		 *
14730d94caffSDavid Greenman 		 * Downgrading to a read lock for vm_fault_wire avoids a possible
147424a1cce3SDavid Greenman 		 * deadlock with another process that may have faulted on one
14750d94caffSDavid Greenman 		 * of the pages to be wired (it would mark the page busy,
14760d94caffSDavid Greenman 		 * blocking us, then in turn block on the map lock that we
14770d94caffSDavid Greenman 		 * hold).  Because of problems in the recursive lock package,
14780d94caffSDavid Greenman 		 * we cannot upgrade to a write lock in vm_map_lookup.  Thus,
14790d94caffSDavid Greenman 		 * any actions that require the write lock must be done
14800d94caffSDavid Greenman 		 * beforehand.  Because we keep the read lock on the map, the
14810d94caffSDavid Greenman 		 * copy-on-write status of the entries we modify here cannot
14820d94caffSDavid Greenman 		 * change.
1483df8bae1dSRodney W. Grimes 		 */
1484df8bae1dSRodney W. Grimes 
1485df8bae1dSRodney W. Grimes 		/*
1486df8bae1dSRodney W. Grimes 		 * Pass 1.
1487df8bae1dSRodney W. Grimes 		 */
1488df8bae1dSRodney W. Grimes 		while ((entry != &map->header) && (entry->start < end)) {
1489df8bae1dSRodney W. Grimes 			if (entry->wired_count == 0) {
1490df8bae1dSRodney W. Grimes 
1491df8bae1dSRodney W. Grimes 				/*
1492df8bae1dSRodney W. Grimes 				 * Perform actions of vm_map_lookup that need
1493df8bae1dSRodney W. Grimes 				 * the write lock on the map: create a shadow
1494df8bae1dSRodney W. Grimes 				 * object for a copy-on-write region, or an
1495df8bae1dSRodney W. Grimes 				 * object for a zero-fill region.
1496df8bae1dSRodney W. Grimes 				 *
1497df8bae1dSRodney W. Grimes 				 * We don't have to do this for entries that
1498ad5fca3bSAlan Cox 				 * point to sub maps, because we won't
1499ad5fca3bSAlan Cox 				 * hold the lock on the sub map.
1500df8bae1dSRodney W. Grimes 				 */
15019fdfe602SMatthew Dillon 				if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1502afa07f7eSJohn Dyson 					int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY;
1503b5b40fa6SJohn Dyson 					if (copyflag &&
1504df8bae1dSRodney W. Grimes 					    ((entry->protection & VM_PROT_WRITE) != 0)) {
1505df8bae1dSRodney W. Grimes 
1506df8bae1dSRodney W. Grimes 						vm_object_shadow(&entry->object.vm_object,
1507df8bae1dSRodney W. Grimes 						    &entry->offset,
1508c2e11a03SJohn Dyson 						    atop(entry->end - entry->start));
1509afa07f7eSJohn Dyson 						entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
15104e71e795SMatthew Dillon 					} else if (entry->object.vm_object == NULL &&
15114e71e795SMatthew Dillon 						   !map->system_map) {
1512df8bae1dSRodney W. Grimes 						entry->object.vm_object =
1513a316d390SJohn Dyson 						    vm_object_allocate(OBJT_DEFAULT,
1514c2e11a03SJohn Dyson 							atop(entry->end - entry->start));
1515df8bae1dSRodney W. Grimes 						entry->offset = (vm_offset_t) 0;
1516df8bae1dSRodney W. Grimes 					}
1517df8bae1dSRodney W. Grimes 				}
1518df8bae1dSRodney W. Grimes 			}
1519df8bae1dSRodney W. Grimes 			vm_map_clip_start(map, entry, start);
1520df8bae1dSRodney W. Grimes 			vm_map_clip_end(map, entry, end);
1521df8bae1dSRodney W. Grimes 			entry->wired_count++;
1522df8bae1dSRodney W. Grimes 
1523df8bae1dSRodney W. Grimes 			/*
1524df8bae1dSRodney W. Grimes 			 * Check for holes
1525df8bae1dSRodney W. Grimes 			 */
1526df8bae1dSRodney W. Grimes 			if (entry->end < end &&
1527df8bae1dSRodney W. Grimes 			    (entry->next == &map->header ||
1528df8bae1dSRodney W. Grimes 				entry->next->start > entry->end)) {
1529df8bae1dSRodney W. Grimes 				/*
15300d94caffSDavid Greenman 				 * Found one.  Object creation actions do not
15310d94caffSDavid Greenman 				 * need to be undone, but the wired counts
15320d94caffSDavid Greenman 				 * need to be restored.
1533df8bae1dSRodney W. Grimes 				 */
1534df8bae1dSRodney W. Grimes 				while (entry != &map->header && entry->end > start) {
1535df8bae1dSRodney W. Grimes 					entry->wired_count--;
1536df8bae1dSRodney W. Grimes 					entry = entry->prev;
1537df8bae1dSRodney W. Grimes 				}
1538df8bae1dSRodney W. Grimes 				vm_map_unlock(map);
1539df8bae1dSRodney W. Grimes 				return (KERN_INVALID_ARGUMENT);
1540df8bae1dSRodney W. Grimes 			}
1541df8bae1dSRodney W. Grimes 			entry = entry->next;
1542df8bae1dSRodney W. Grimes 		}
1543df8bae1dSRodney W. Grimes 
1544df8bae1dSRodney W. Grimes 		/*
1545df8bae1dSRodney W. Grimes 		 * Pass 2.
1546df8bae1dSRodney W. Grimes 		 */
1547df8bae1dSRodney W. Grimes 
1548df8bae1dSRodney W. Grimes 		/*
1549df8bae1dSRodney W. Grimes 		 * HACK HACK HACK HACK
1550df8bae1dSRodney W. Grimes 		 *
155124a1cce3SDavid Greenman 		 * If we are wiring in the kernel map or a submap of it,
155224a1cce3SDavid Greenman 		 * unlock the map to avoid deadlocks.  We trust that the
155324a1cce3SDavid Greenman 		 * kernel is well-behaved, and therefore will not do
155424a1cce3SDavid Greenman 		 * anything destructive to this region of the map while
155524a1cce3SDavid Greenman 		 * we have it unlocked.  We cannot trust user processes
155624a1cce3SDavid Greenman 		 * to do the same.
1557df8bae1dSRodney W. Grimes 		 *
1558df8bae1dSRodney W. Grimes 		 * HACK HACK HACK HACK
1559df8bae1dSRodney W. Grimes 		 */
1560df8bae1dSRodney W. Grimes 		if (vm_map_pmap(map) == kernel_pmap) {
1561df8bae1dSRodney W. Grimes 			vm_map_unlock(map);	/* trust me ... */
15620d94caffSDavid Greenman 		} else {
156303e9c6c1SJohn Dyson 			vm_map_lock_downgrade(map);
1564df8bae1dSRodney W. Grimes 		}
1565df8bae1dSRodney W. Grimes 
1566df8bae1dSRodney W. Grimes 		rv = 0;
1567df8bae1dSRodney W. Grimes 		entry = start_entry;
1568df8bae1dSRodney W. Grimes 		while (entry != &map->header && entry->start < end) {
1569df8bae1dSRodney W. Grimes 			/*
15700d94caffSDavid Greenman 			 * If vm_fault_wire fails for any page we need to undo
15710d94caffSDavid Greenman 			 * what has been done.  We decrement the wiring count
15720d94caffSDavid Greenman 			 * for those pages which have not yet been wired (now)
15730d94caffSDavid Greenman 			 * and unwire those that have (later).
1574df8bae1dSRodney W. Grimes 			 *
1575df8bae1dSRodney W. Grimes 			 * XXX this violates the locking protocol on the map,
1576df8bae1dSRodney W. Grimes 			 * needs to be fixed.
1577df8bae1dSRodney W. Grimes 			 */
1578df8bae1dSRodney W. Grimes 			if (rv)
1579df8bae1dSRodney W. Grimes 				entry->wired_count--;
1580df8bae1dSRodney W. Grimes 			else if (entry->wired_count == 1) {
1581df8bae1dSRodney W. Grimes 				rv = vm_fault_wire(map, entry->start, entry->end);
1582df8bae1dSRodney W. Grimes 				if (rv) {
1583df8bae1dSRodney W. Grimes 					failed = entry->start;
1584df8bae1dSRodney W. Grimes 					entry->wired_count--;
1585df8bae1dSRodney W. Grimes 				}
1586df8bae1dSRodney W. Grimes 			}
1587df8bae1dSRodney W. Grimes 			entry = entry->next;
1588df8bae1dSRodney W. Grimes 		}
1589df8bae1dSRodney W. Grimes 
1590df8bae1dSRodney W. Grimes 		if (vm_map_pmap(map) == kernel_pmap) {
1591df8bae1dSRodney W. Grimes 			vm_map_lock(map);
1592df8bae1dSRodney W. Grimes 		}
1593df8bae1dSRodney W. Grimes 		if (rv) {
1594df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
1595df8bae1dSRodney W. Grimes 			(void) vm_map_pageable(map, start, failed, TRUE);
1596df8bae1dSRodney W. Grimes 			return (rv);
1597df8bae1dSRodney W. Grimes 		}
1598b7b2aac2SJohn Dyson 		vm_map_simplify_entry(map, start_entry);
1599df8bae1dSRodney W. Grimes 	}
1600df8bae1dSRodney W. Grimes 
1601df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1602df8bae1dSRodney W. Grimes 
1603df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
1604df8bae1dSRodney W. Grimes }
1605df8bae1dSRodney W. Grimes 
1606df8bae1dSRodney W. Grimes /*
1607df8bae1dSRodney W. Grimes  * vm_map_clean
1608df8bae1dSRodney W. Grimes  *
1609df8bae1dSRodney W. Grimes  * Push any dirty cached pages in the address range to their pager.
1610df8bae1dSRodney W. Grimes  * If syncio is TRUE, dirty pages are written synchronously.
1611df8bae1dSRodney W. Grimes  * If invalidate is TRUE, any cached pages are freed as well.
1612df8bae1dSRodney W. Grimes  *
1613df8bae1dSRodney W. Grimes  * Returns an error if any part of the specified range is not mapped.
1614df8bae1dSRodney W. Grimes  */
1615df8bae1dSRodney W. Grimes int
1616df8bae1dSRodney W. Grimes vm_map_clean(map, start, end, syncio, invalidate)
1617df8bae1dSRodney W. Grimes 	vm_map_t map;
1618df8bae1dSRodney W. Grimes 	vm_offset_t start;
1619df8bae1dSRodney W. Grimes 	vm_offset_t end;
1620df8bae1dSRodney W. Grimes 	boolean_t syncio;
1621df8bae1dSRodney W. Grimes 	boolean_t invalidate;
1622df8bae1dSRodney W. Grimes {
1623c0877f10SJohn Dyson 	vm_map_entry_t current;
1624df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
1625df8bae1dSRodney W. Grimes 	vm_size_t size;
1626df8bae1dSRodney W. Grimes 	vm_object_t object;
1627a316d390SJohn Dyson 	vm_ooffset_t offset;
1628df8bae1dSRodney W. Grimes 
1629df8bae1dSRodney W. Grimes 	vm_map_lock_read(map);
1630df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1631df8bae1dSRodney W. Grimes 	if (!vm_map_lookup_entry(map, start, &entry)) {
1632df8bae1dSRodney W. Grimes 		vm_map_unlock_read(map);
1633df8bae1dSRodney W. Grimes 		return (KERN_INVALID_ADDRESS);
1634df8bae1dSRodney W. Grimes 	}
1635df8bae1dSRodney W. Grimes 	/*
1636df8bae1dSRodney W. Grimes 	 * Make a first pass to check for holes.
1637df8bae1dSRodney W. Grimes 	 */
1638df8bae1dSRodney W. Grimes 	for (current = entry; current->start < end; current = current->next) {
1639afa07f7eSJohn Dyson 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1640df8bae1dSRodney W. Grimes 			vm_map_unlock_read(map);
1641df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ARGUMENT);
1642df8bae1dSRodney W. Grimes 		}
1643df8bae1dSRodney W. Grimes 		if (end > current->end &&
1644df8bae1dSRodney W. Grimes 		    (current->next == &map->header ||
1645df8bae1dSRodney W. Grimes 			current->end != current->next->start)) {
1646df8bae1dSRodney W. Grimes 			vm_map_unlock_read(map);
1647df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ADDRESS);
1648df8bae1dSRodney W. Grimes 		}
1649df8bae1dSRodney W. Grimes 	}
1650df8bae1dSRodney W. Grimes 
1651cf2819ccSJohn Dyson 	if (invalidate)
1652cf2819ccSJohn Dyson 		pmap_remove(vm_map_pmap(map), start, end);
1653df8bae1dSRodney W. Grimes 	/*
1654df8bae1dSRodney W. Grimes 	 * Make a second pass, cleaning/uncaching pages from the indicated
1655df8bae1dSRodney W. Grimes 	 * objects as we go.
1656df8bae1dSRodney W. Grimes 	 */
1657df8bae1dSRodney W. Grimes 	for (current = entry; current->start < end; current = current->next) {
1658df8bae1dSRodney W. Grimes 		offset = current->offset + (start - current->start);
1659df8bae1dSRodney W. Grimes 		size = (end <= current->end ? end : current->end) - start;
16609fdfe602SMatthew Dillon 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1661c0877f10SJohn Dyson 			vm_map_t smap;
1662df8bae1dSRodney W. Grimes 			vm_map_entry_t tentry;
1663df8bae1dSRodney W. Grimes 			vm_size_t tsize;
1664df8bae1dSRodney W. Grimes 
16659fdfe602SMatthew Dillon 			smap = current->object.sub_map;
1666df8bae1dSRodney W. Grimes 			vm_map_lock_read(smap);
1667df8bae1dSRodney W. Grimes 			(void) vm_map_lookup_entry(smap, offset, &tentry);
1668df8bae1dSRodney W. Grimes 			tsize = tentry->end - offset;
1669df8bae1dSRodney W. Grimes 			if (tsize < size)
1670df8bae1dSRodney W. Grimes 				size = tsize;
1671df8bae1dSRodney W. Grimes 			object = tentry->object.vm_object;
1672df8bae1dSRodney W. Grimes 			offset = tentry->offset + (offset - tentry->start);
1673df8bae1dSRodney W. Grimes 			vm_map_unlock_read(smap);
1674df8bae1dSRodney W. Grimes 		} else {
1675df8bae1dSRodney W. Grimes 			object = current->object.vm_object;
1676df8bae1dSRodney W. Grimes 		}
16778a02c104SJohn Dyson 		/*
16788a02c104SJohn Dyson 		 * Note that there is absolutely no sense in writing out
16798a02c104SJohn Dyson 		 * anonymous objects, so we track down the vnode object
16808a02c104SJohn Dyson 		 * to write out.
16818a02c104SJohn Dyson 		 * We invalidate (remove) all pages from the address space
16828a02c104SJohn Dyson 		 * anyway, for semantic correctness.
16838a02c104SJohn Dyson 		 */
16848a02c104SJohn Dyson 		while (object->backing_object) {
16858a02c104SJohn Dyson 			object = object->backing_object;
16868a02c104SJohn Dyson 			offset += object->backing_object_offset;
16878a02c104SJohn Dyson 			if (object->size < OFF_TO_IDX( offset + size))
16888a02c104SJohn Dyson 				size = IDX_TO_OFF(object->size) - offset;
16898a02c104SJohn Dyson 		}
1690ff359f84SMatthew Dillon 		if (object && (object->type == OBJT_VNODE) &&
1691ff359f84SMatthew Dillon 		    (current->protection & VM_PROT_WRITE)) {
1692df8bae1dSRodney W. Grimes 			/*
1693ff359f84SMatthew Dillon 			 * Flush pages if writing is allowed, invalidate them
1694ff359f84SMatthew Dillon 			 * if invalidation requested.  Pages undergoing I/O
1695ff359f84SMatthew Dillon 			 * will be ignored by vm_object_page_remove().
1696f5cf85d4SDavid Greenman 			 *
1697ff359f84SMatthew Dillon 			 * We cannot lock the vnode and then wait for paging
1698ff359f84SMatthew Dillon 			 * to complete without deadlocking against vm_fault.
1699ff359f84SMatthew Dillon 			 * Instead we simply call vm_object_page_remove() and
1700ff359f84SMatthew Dillon 			 * allow it to block internally on a page-by-page
1701ff359f84SMatthew Dillon 			 * basis when it encounters pages undergoing async
1702ff359f84SMatthew Dillon 			 * I/O.
1703df8bae1dSRodney W. Grimes 			 */
17048f9110f6SJohn Dyson 			int flags;
1705ff359f84SMatthew Dillon 
1706ff359f84SMatthew Dillon 			vm_object_reference(object);
1707157ac55fSJohn Dyson 			vn_lock(object->handle, LK_EXCLUSIVE | LK_RETRY, curproc);
17088f9110f6SJohn Dyson 			flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
17098f9110f6SJohn Dyson 			flags |= invalidate ? OBJPC_INVAL : 0;
1710a316d390SJohn Dyson 			vm_object_page_clean(object,
1711a316d390SJohn Dyson 			    OFF_TO_IDX(offset),
17122be70f79SJohn Dyson 			    OFF_TO_IDX(offset + size + PAGE_MASK),
17138f9110f6SJohn Dyson 			    flags);
1714cf2819ccSJohn Dyson 			if (invalidate) {
1715ff359f84SMatthew Dillon 				/*vm_object_pip_wait(object, "objmcl");*/
1716a316d390SJohn Dyson 				vm_object_page_remove(object,
1717a316d390SJohn Dyson 				    OFF_TO_IDX(offset),
17182be70f79SJohn Dyson 				    OFF_TO_IDX(offset + size + PAGE_MASK),
1719a316d390SJohn Dyson 				    FALSE);
1720cf2819ccSJohn Dyson 			}
17212be70f79SJohn Dyson 			VOP_UNLOCK(object->handle, 0, curproc);
1722ff359f84SMatthew Dillon 			vm_object_deallocate(object);
1723a02051c3SJohn Dyson 		}
1724df8bae1dSRodney W. Grimes 		start += size;
1725df8bae1dSRodney W. Grimes 	}
1726df8bae1dSRodney W. Grimes 
1727df8bae1dSRodney W. Grimes 	vm_map_unlock_read(map);
1728df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
1729df8bae1dSRodney W. Grimes }
1730df8bae1dSRodney W. Grimes 
1731df8bae1dSRodney W. Grimes /*
1732df8bae1dSRodney W. Grimes  *	vm_map_entry_unwire:	[ internal use only ]
1733df8bae1dSRodney W. Grimes  *
1734df8bae1dSRodney W. Grimes  *	Make the region specified by this entry pageable.
1735df8bae1dSRodney W. Grimes  *
1736df8bae1dSRodney W. Grimes  *	The map in question should be locked.
1737df8bae1dSRodney W. Grimes  *	[This is the reason for this routine's existence.]
1738df8bae1dSRodney W. Grimes  */
17390362d7d7SJohn Dyson static void
17400d94caffSDavid Greenman vm_map_entry_unwire(map, entry)
1741df8bae1dSRodney W. Grimes 	vm_map_t map;
1742c0877f10SJohn Dyson 	vm_map_entry_t entry;
1743df8bae1dSRodney W. Grimes {
1744df8bae1dSRodney W. Grimes 	vm_fault_unwire(map, entry->start, entry->end);
1745df8bae1dSRodney W. Grimes 	entry->wired_count = 0;
1746df8bae1dSRodney W. Grimes }
1747df8bae1dSRodney W. Grimes 
1748df8bae1dSRodney W. Grimes /*
1749df8bae1dSRodney W. Grimes  *	vm_map_entry_delete:	[ internal use only ]
1750df8bae1dSRodney W. Grimes  *
1751df8bae1dSRodney W. Grimes  *	Deallocate the given entry from the target map.
1752df8bae1dSRodney W. Grimes  */
17530362d7d7SJohn Dyson static void
17540d94caffSDavid Greenman vm_map_entry_delete(map, entry)
1755c0877f10SJohn Dyson 	vm_map_t map;
1756c0877f10SJohn Dyson 	vm_map_entry_t entry;
1757df8bae1dSRodney W. Grimes {
1758df8bae1dSRodney W. Grimes 	vm_map_entry_unlink(map, entry);
1759df8bae1dSRodney W. Grimes 	map->size -= entry->end - entry->start;
1760df8bae1dSRodney W. Grimes 
17619fdfe602SMatthew Dillon 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1762df8bae1dSRodney W. Grimes 		vm_object_deallocate(entry->object.vm_object);
1763b5b40fa6SJohn Dyson 	}
1764df8bae1dSRodney W. Grimes 
1765df8bae1dSRodney W. Grimes 	vm_map_entry_dispose(map, entry);
1766df8bae1dSRodney W. Grimes }
1767df8bae1dSRodney W. Grimes 
1768df8bae1dSRodney W. Grimes /*
1769df8bae1dSRodney W. Grimes  *	vm_map_delete:	[ internal use only ]
1770df8bae1dSRodney W. Grimes  *
1771df8bae1dSRodney W. Grimes  *	Deallocates the given address range from the target
1772df8bae1dSRodney W. Grimes  *	map.
1773df8bae1dSRodney W. Grimes  */
1774df8bae1dSRodney W. Grimes int
1775df8bae1dSRodney W. Grimes vm_map_delete(map, start, end)
1776c0877f10SJohn Dyson 	vm_map_t map;
1777df8bae1dSRodney W. Grimes 	vm_offset_t start;
1778c0877f10SJohn Dyson 	vm_offset_t end;
1779df8bae1dSRodney W. Grimes {
1780cbd8ec09SJohn Dyson 	vm_object_t object;
1781c0877f10SJohn Dyson 	vm_map_entry_t entry;
1782df8bae1dSRodney W. Grimes 	vm_map_entry_t first_entry;
1783df8bae1dSRodney W. Grimes 
1784df8bae1dSRodney W. Grimes 	/*
1785df8bae1dSRodney W. Grimes 	 * Find the start of the region, and clip it
1786df8bae1dSRodney W. Grimes 	 */
1787df8bae1dSRodney W. Grimes 
1788876318ecSAlan Cox 	if (!vm_map_lookup_entry(map, start, &first_entry))
1789df8bae1dSRodney W. Grimes 		entry = first_entry->next;
1790876318ecSAlan Cox 	else {
1791df8bae1dSRodney W. Grimes 		entry = first_entry;
1792df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
1793df8bae1dSRodney W. Grimes 		/*
17940d94caffSDavid Greenman 		 * Fix the lookup hint now, rather than each time though the
17950d94caffSDavid Greenman 		 * loop.
1796df8bae1dSRodney W. Grimes 		 */
1797df8bae1dSRodney W. Grimes 		SAVE_HINT(map, entry->prev);
1798df8bae1dSRodney W. Grimes 	}
1799df8bae1dSRodney W. Grimes 
1800df8bae1dSRodney W. Grimes 	/*
1801df8bae1dSRodney W. Grimes 	 * Save the free space hint
1802df8bae1dSRodney W. Grimes 	 */
1803df8bae1dSRodney W. Grimes 
1804b18bfc3dSJohn Dyson 	if (entry == &map->header) {
1805b18bfc3dSJohn Dyson 		map->first_free = &map->header;
18062dbea5d2SJohn Dyson 	} else if (map->first_free->start >= start) {
1807df8bae1dSRodney W. Grimes 		map->first_free = entry->prev;
18082dbea5d2SJohn Dyson 	}
1809df8bae1dSRodney W. Grimes 
1810df8bae1dSRodney W. Grimes 	/*
1811df8bae1dSRodney W. Grimes 	 * Step through all entries in this region
1812df8bae1dSRodney W. Grimes 	 */
1813df8bae1dSRodney W. Grimes 
1814df8bae1dSRodney W. Grimes 	while ((entry != &map->header) && (entry->start < end)) {
1815df8bae1dSRodney W. Grimes 		vm_map_entry_t next;
1816b18bfc3dSJohn Dyson 		vm_offset_t s, e;
1817cbd8ec09SJohn Dyson 		vm_pindex_t offidxstart, offidxend, count;
1818df8bae1dSRodney W. Grimes 
1819df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, entry, end);
1820df8bae1dSRodney W. Grimes 
1821df8bae1dSRodney W. Grimes 		s = entry->start;
1822df8bae1dSRodney W. Grimes 		e = entry->end;
1823c0877f10SJohn Dyson 		next = entry->next;
1824df8bae1dSRodney W. Grimes 
1825cbd8ec09SJohn Dyson 		offidxstart = OFF_TO_IDX(entry->offset);
1826cbd8ec09SJohn Dyson 		count = OFF_TO_IDX(e - s);
1827cbd8ec09SJohn Dyson 		object = entry->object.vm_object;
18282dbea5d2SJohn Dyson 
1829df8bae1dSRodney W. Grimes 		/*
18300d94caffSDavid Greenman 		 * Unwire before removing addresses from the pmap; otherwise,
18310d94caffSDavid Greenman 		 * unwiring will put the entries back in the pmap.
1832df8bae1dSRodney W. Grimes 		 */
1833c0877f10SJohn Dyson 		if (entry->wired_count != 0) {
1834df8bae1dSRodney W. Grimes 			vm_map_entry_unwire(map, entry);
1835c0877f10SJohn Dyson 		}
1836df8bae1dSRodney W. Grimes 
1837cbd8ec09SJohn Dyson 		offidxend = offidxstart + count;
1838df8bae1dSRodney W. Grimes 
1839c0877f10SJohn Dyson 		if ((object == kernel_object) || (object == kmem_object)) {
18402dbea5d2SJohn Dyson 			vm_object_page_remove(object, offidxstart, offidxend, FALSE);
1841b18bfc3dSJohn Dyson 		} else {
1842df8bae1dSRodney W. Grimes 			pmap_remove(map->pmap, s, e);
1843876318ecSAlan Cox 			if (object != NULL &&
1844876318ecSAlan Cox 			    object->ref_count != 1 &&
1845876318ecSAlan Cox 			    (object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING &&
1846876318ecSAlan Cox 			    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
18472dbea5d2SJohn Dyson 				vm_object_collapse(object);
18482dbea5d2SJohn Dyson 				vm_object_page_remove(object, offidxstart, offidxend, FALSE);
18492dbea5d2SJohn Dyson 				if (object->type == OBJT_SWAP) {
1850cbd8ec09SJohn Dyson 					swap_pager_freespace(object, offidxstart, count);
18512dbea5d2SJohn Dyson 				}
1852876318ecSAlan Cox 				if (offidxend >= object->size &&
1853876318ecSAlan Cox 				    offidxstart < object->size) {
1854c0877f10SJohn Dyson 					object->size = offidxstart;
1855c0877f10SJohn Dyson 				}
18562dbea5d2SJohn Dyson 			}
1857b18bfc3dSJohn Dyson 		}
1858df8bae1dSRodney W. Grimes 
1859df8bae1dSRodney W. Grimes 		/*
18600d94caffSDavid Greenman 		 * Delete the entry (which may delete the object) only after
18610d94caffSDavid Greenman 		 * removing all pmap entries pointing to its pages.
18620d94caffSDavid Greenman 		 * (Otherwise, its page frames may be reallocated, and any
18630d94caffSDavid Greenman 		 * modify bits will be set in the wrong object!)
1864df8bae1dSRodney W. Grimes 		 */
1865df8bae1dSRodney W. Grimes 		vm_map_entry_delete(map, entry);
1866df8bae1dSRodney W. Grimes 		entry = next;
1867df8bae1dSRodney W. Grimes 	}
1868df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
1869df8bae1dSRodney W. Grimes }
1870df8bae1dSRodney W. Grimes 
1871df8bae1dSRodney W. Grimes /*
1872df8bae1dSRodney W. Grimes  *	vm_map_remove:
1873df8bae1dSRodney W. Grimes  *
1874df8bae1dSRodney W. Grimes  *	Remove the given address range from the target map.
1875df8bae1dSRodney W. Grimes  *	This is the exported form of vm_map_delete.
1876df8bae1dSRodney W. Grimes  */
1877df8bae1dSRodney W. Grimes int
1878df8bae1dSRodney W. Grimes vm_map_remove(map, start, end)
1879c0877f10SJohn Dyson 	vm_map_t map;
1880c0877f10SJohn Dyson 	vm_offset_t start;
1881c0877f10SJohn Dyson 	vm_offset_t end;
1882df8bae1dSRodney W. Grimes {
1883c0877f10SJohn Dyson 	int result, s = 0;
18848d6e8edeSDavid Greenman 
18859579ee64SDavid Greenman 	if (map == kmem_map || map == mb_map)
1886b18bfc3dSJohn Dyson 		s = splvm();
1887df8bae1dSRodney W. Grimes 
1888df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1889df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1890df8bae1dSRodney W. Grimes 	result = vm_map_delete(map, start, end);
1891df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1892df8bae1dSRodney W. Grimes 
18939579ee64SDavid Greenman 	if (map == kmem_map || map == mb_map)
18948d6e8edeSDavid Greenman 		splx(s);
18958d6e8edeSDavid Greenman 
1896df8bae1dSRodney W. Grimes 	return (result);
1897df8bae1dSRodney W. Grimes }
1898df8bae1dSRodney W. Grimes 
1899df8bae1dSRodney W. Grimes /*
1900df8bae1dSRodney W. Grimes  *	vm_map_check_protection:
1901df8bae1dSRodney W. Grimes  *
1902df8bae1dSRodney W. Grimes  *	Assert that the target map allows the specified
1903df8bae1dSRodney W. Grimes  *	privilege on the entire address region given.
1904df8bae1dSRodney W. Grimes  *	The entire region must be allocated.
1905df8bae1dSRodney W. Grimes  */
19060d94caffSDavid Greenman boolean_t
1907b9dcd593SBruce Evans vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
1908b9dcd593SBruce Evans 			vm_prot_t protection)
1909df8bae1dSRodney W. Grimes {
1910c0877f10SJohn Dyson 	vm_map_entry_t entry;
1911df8bae1dSRodney W. Grimes 	vm_map_entry_t tmp_entry;
1912df8bae1dSRodney W. Grimes 
1913df8bae1dSRodney W. Grimes 	if (!vm_map_lookup_entry(map, start, &tmp_entry)) {
1914df8bae1dSRodney W. Grimes 		return (FALSE);
1915df8bae1dSRodney W. Grimes 	}
1916df8bae1dSRodney W. Grimes 	entry = tmp_entry;
1917df8bae1dSRodney W. Grimes 
1918df8bae1dSRodney W. Grimes 	while (start < end) {
1919df8bae1dSRodney W. Grimes 		if (entry == &map->header) {
1920df8bae1dSRodney W. Grimes 			return (FALSE);
1921df8bae1dSRodney W. Grimes 		}
1922df8bae1dSRodney W. Grimes 		/*
1923df8bae1dSRodney W. Grimes 		 * No holes allowed!
1924df8bae1dSRodney W. Grimes 		 */
1925df8bae1dSRodney W. Grimes 
1926df8bae1dSRodney W. Grimes 		if (start < entry->start) {
1927df8bae1dSRodney W. Grimes 			return (FALSE);
1928df8bae1dSRodney W. Grimes 		}
1929df8bae1dSRodney W. Grimes 		/*
1930df8bae1dSRodney W. Grimes 		 * Check protection associated with entry.
1931df8bae1dSRodney W. Grimes 		 */
1932df8bae1dSRodney W. Grimes 
1933df8bae1dSRodney W. Grimes 		if ((entry->protection & protection) != protection) {
1934df8bae1dSRodney W. Grimes 			return (FALSE);
1935df8bae1dSRodney W. Grimes 		}
1936df8bae1dSRodney W. Grimes 		/* go to next entry */
1937df8bae1dSRodney W. Grimes 
1938df8bae1dSRodney W. Grimes 		start = entry->end;
1939df8bae1dSRodney W. Grimes 		entry = entry->next;
1940df8bae1dSRodney W. Grimes 	}
1941df8bae1dSRodney W. Grimes 	return (TRUE);
1942df8bae1dSRodney W. Grimes }
1943df8bae1dSRodney W. Grimes 
194486524867SJohn Dyson /*
194586524867SJohn Dyson  * Split the pages in a map entry into a new object.  This affords
194686524867SJohn Dyson  * easier removal of unused pages, and keeps object inheritance from
194786524867SJohn Dyson  * being a negative impact on memory usage.
194886524867SJohn Dyson  */
1949c0877f10SJohn Dyson static void
1950c0877f10SJohn Dyson vm_map_split(entry)
1951c0877f10SJohn Dyson 	vm_map_entry_t entry;
1952c0877f10SJohn Dyson {
195386524867SJohn Dyson 	vm_page_t m;
1954bd6be915SJohn Dyson 	vm_object_t orig_object, new_object, source;
1955c0877f10SJohn Dyson 	vm_offset_t s, e;
1956c0877f10SJohn Dyson 	vm_pindex_t offidxstart, offidxend, idx;
1957c0877f10SJohn Dyson 	vm_size_t size;
1958c0877f10SJohn Dyson 	vm_ooffset_t offset;
1959c0877f10SJohn Dyson 
1960c0877f10SJohn Dyson 	orig_object = entry->object.vm_object;
1961c0877f10SJohn Dyson 	if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
1962c0877f10SJohn Dyson 		return;
1963c0877f10SJohn Dyson 	if (orig_object->ref_count <= 1)
1964c0877f10SJohn Dyson 		return;
1965c0877f10SJohn Dyson 
1966c0877f10SJohn Dyson 	offset = entry->offset;
1967c0877f10SJohn Dyson 	s = entry->start;
1968c0877f10SJohn Dyson 	e = entry->end;
1969c0877f10SJohn Dyson 
1970c0877f10SJohn Dyson 	offidxstart = OFF_TO_IDX(offset);
1971c0877f10SJohn Dyson 	offidxend = offidxstart + OFF_TO_IDX(e - s);
1972c0877f10SJohn Dyson 	size = offidxend - offidxstart;
1973c0877f10SJohn Dyson 
1974c0877f10SJohn Dyson 	new_object = vm_pager_allocate(orig_object->type,
19756cde7a16SDavid Greenman 		NULL, IDX_TO_OFF(size), VM_PROT_ALL, 0LL);
1976c0877f10SJohn Dyson 	if (new_object == NULL)
1977c0877f10SJohn Dyson 		return;
1978c0877f10SJohn Dyson 
1979bd6be915SJohn Dyson 	source = orig_object->backing_object;
1980bd6be915SJohn Dyson 	if (source != NULL) {
1981bd6be915SJohn Dyson 		vm_object_reference(source);	/* Referenced by new_object */
1982bd6be915SJohn Dyson 		TAILQ_INSERT_TAIL(&source->shadow_head,
1983bd6be915SJohn Dyson 				  new_object, shadow_list);
1984069e9bc1SDoug Rabson 		vm_object_clear_flag(source, OBJ_ONEMAPPING);
1985bd6be915SJohn Dyson 		new_object->backing_object_offset =
1986a0fce827SJohn Polstra 			orig_object->backing_object_offset + IDX_TO_OFF(offidxstart);
1987bd6be915SJohn Dyson 		new_object->backing_object = source;
1988bd6be915SJohn Dyson 		source->shadow_count++;
1989bd6be915SJohn Dyson 		source->generation++;
1990bd6be915SJohn Dyson 	}
1991bd6be915SJohn Dyson 
1992c0877f10SJohn Dyson 	for (idx = 0; idx < size; idx++) {
1993c0877f10SJohn Dyson 		vm_page_t m;
1994c0877f10SJohn Dyson 
1995c0877f10SJohn Dyson 	retry:
1996c0877f10SJohn Dyson 		m = vm_page_lookup(orig_object, offidxstart + idx);
1997c0877f10SJohn Dyson 		if (m == NULL)
1998c0877f10SJohn Dyson 			continue;
19991c7c3c6aSMatthew Dillon 
20001c7c3c6aSMatthew Dillon 		/*
20011c7c3c6aSMatthew Dillon 		 * We must wait for pending I/O to complete before we can
20021c7c3c6aSMatthew Dillon 		 * rename the page.
2003d1bf5d56SMatthew Dillon 		 *
2004d1bf5d56SMatthew Dillon 		 * We do not have to VM_PROT_NONE the page as mappings should
2005d1bf5d56SMatthew Dillon 		 * not be changed by this operation.
20061c7c3c6aSMatthew Dillon 		 */
20071c7c3c6aSMatthew Dillon 		if (vm_page_sleep_busy(m, TRUE, "spltwt"))
2008c0877f10SJohn Dyson 			goto retry;
2009c0877f10SJohn Dyson 
2010e69763a3SDoug Rabson 		vm_page_busy(m);
2011c0877f10SJohn Dyson 		vm_page_rename(m, new_object, idx);
20127dbf82dcSMatthew Dillon 		/* page automatically made dirty by rename and cache handled */
2013e69763a3SDoug Rabson 		vm_page_busy(m);
2014c0877f10SJohn Dyson 	}
2015c0877f10SJohn Dyson 
2016c0877f10SJohn Dyson 	if (orig_object->type == OBJT_SWAP) {
2017d474eaaaSDoug Rabson 		vm_object_pip_add(orig_object, 1);
2018c0877f10SJohn Dyson 		/*
2019c0877f10SJohn Dyson 		 * copy orig_object pages into new_object
2020c0877f10SJohn Dyson 		 * and destroy unneeded pages in
2021c0877f10SJohn Dyson 		 * shadow object.
2022c0877f10SJohn Dyson 		 */
20231c7c3c6aSMatthew Dillon 		swap_pager_copy(orig_object, new_object, offidxstart, 0);
2024c0877f10SJohn Dyson 		vm_object_pip_wakeup(orig_object);
2025c0877f10SJohn Dyson 	}
2026c0877f10SJohn Dyson 
202786524867SJohn Dyson 	for (idx = 0; idx < size; idx++) {
202886524867SJohn Dyson 		m = vm_page_lookup(new_object, idx);
202986524867SJohn Dyson 		if (m) {
2030e69763a3SDoug Rabson 			vm_page_wakeup(m);
203186524867SJohn Dyson 		}
203286524867SJohn Dyson 	}
203386524867SJohn Dyson 
2034c0877f10SJohn Dyson 	entry->object.vm_object = new_object;
2035c0877f10SJohn Dyson 	entry->offset = 0LL;
2036c0877f10SJohn Dyson 	vm_object_deallocate(orig_object);
2037c0877f10SJohn Dyson }
2038c0877f10SJohn Dyson 
2039df8bae1dSRodney W. Grimes /*
2040df8bae1dSRodney W. Grimes  *	vm_map_copy_entry:
2041df8bae1dSRodney W. Grimes  *
2042df8bae1dSRodney W. Grimes  *	Copies the contents of the source entry to the destination
2043df8bae1dSRodney W. Grimes  *	entry.  The entries *must* be aligned properly.
2044df8bae1dSRodney W. Grimes  */
2045f708ef1bSPoul-Henning Kamp static void
20460d94caffSDavid Greenman vm_map_copy_entry(src_map, dst_map, src_entry, dst_entry)
2047df8bae1dSRodney W. Grimes 	vm_map_t src_map, dst_map;
2048c0877f10SJohn Dyson 	vm_map_entry_t src_entry, dst_entry;
2049df8bae1dSRodney W. Grimes {
2050c0877f10SJohn Dyson 	vm_object_t src_object;
2051c0877f10SJohn Dyson 
20529fdfe602SMatthew Dillon 	if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
2053df8bae1dSRodney W. Grimes 		return;
2054df8bae1dSRodney W. Grimes 
2055df8bae1dSRodney W. Grimes 	if (src_entry->wired_count == 0) {
2056df8bae1dSRodney W. Grimes 
2057df8bae1dSRodney W. Grimes 		/*
20580d94caffSDavid Greenman 		 * If the source entry is marked needs_copy, it is already
20590d94caffSDavid Greenman 		 * write-protected.
2060df8bae1dSRodney W. Grimes 		 */
2061afa07f7eSJohn Dyson 		if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
2062df8bae1dSRodney W. Grimes 			pmap_protect(src_map->pmap,
2063df8bae1dSRodney W. Grimes 			    src_entry->start,
2064df8bae1dSRodney W. Grimes 			    src_entry->end,
2065df8bae1dSRodney W. Grimes 			    src_entry->protection & ~VM_PROT_WRITE);
2066df8bae1dSRodney W. Grimes 		}
2067b18bfc3dSJohn Dyson 
2068df8bae1dSRodney W. Grimes 		/*
2069df8bae1dSRodney W. Grimes 		 * Make a copy of the object.
2070df8bae1dSRodney W. Grimes 		 */
20718aef1712SMatthew Dillon 		if ((src_object = src_entry->object.vm_object) != NULL) {
2072c0877f10SJohn Dyson 
2073c0877f10SJohn Dyson 			if ((src_object->handle == NULL) &&
2074c0877f10SJohn Dyson 				(src_object->type == OBJT_DEFAULT ||
2075c0877f10SJohn Dyson 				 src_object->type == OBJT_SWAP)) {
2076c0877f10SJohn Dyson 				vm_object_collapse(src_object);
207796fb8cf2SJohn Dyson 				if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
2078c0877f10SJohn Dyson 					vm_map_split(src_entry);
2079c0877f10SJohn Dyson 					src_object = src_entry->object.vm_object;
2080c0877f10SJohn Dyson 				}
2081c0877f10SJohn Dyson 			}
2082c0877f10SJohn Dyson 
2083c0877f10SJohn Dyson 			vm_object_reference(src_object);
2084069e9bc1SDoug Rabson 			vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
2085c0877f10SJohn Dyson 			dst_entry->object.vm_object = src_object;
2086afa07f7eSJohn Dyson 			src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2087afa07f7eSJohn Dyson 			dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2088b18bfc3dSJohn Dyson 			dst_entry->offset = src_entry->offset;
2089b18bfc3dSJohn Dyson 		} else {
2090b18bfc3dSJohn Dyson 			dst_entry->object.vm_object = NULL;
2091b18bfc3dSJohn Dyson 			dst_entry->offset = 0;
2092b18bfc3dSJohn Dyson 		}
2093df8bae1dSRodney W. Grimes 
2094df8bae1dSRodney W. Grimes 		pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
2095df8bae1dSRodney W. Grimes 		    dst_entry->end - dst_entry->start, src_entry->start);
20960d94caffSDavid Greenman 	} else {
2097df8bae1dSRodney W. Grimes 		/*
2098df8bae1dSRodney W. Grimes 		 * Of course, wired down pages can't be set copy-on-write.
20990d94caffSDavid Greenman 		 * Cause wired pages to be copied into the new map by
21000d94caffSDavid Greenman 		 * simulating faults (the new pages are pageable)
2101df8bae1dSRodney W. Grimes 		 */
2102df8bae1dSRodney W. Grimes 		vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry);
2103df8bae1dSRodney W. Grimes 	}
2104df8bae1dSRodney W. Grimes }
2105df8bae1dSRodney W. Grimes 
2106df8bae1dSRodney W. Grimes /*
2107df8bae1dSRodney W. Grimes  * vmspace_fork:
2108df8bae1dSRodney W. Grimes  * Create a new process vmspace structure and vm_map
2109df8bae1dSRodney W. Grimes  * based on those of an existing process.  The new map
2110df8bae1dSRodney W. Grimes  * is based on the old map, according to the inheritance
2111df8bae1dSRodney W. Grimes  * values on the regions in that map.
2112df8bae1dSRodney W. Grimes  *
2113df8bae1dSRodney W. Grimes  * The source map must not be locked.
2114df8bae1dSRodney W. Grimes  */
2115df8bae1dSRodney W. Grimes struct vmspace *
2116df8bae1dSRodney W. Grimes vmspace_fork(vm1)
2117c0877f10SJohn Dyson 	struct vmspace *vm1;
2118df8bae1dSRodney W. Grimes {
2119c0877f10SJohn Dyson 	struct vmspace *vm2;
2120df8bae1dSRodney W. Grimes 	vm_map_t old_map = &vm1->vm_map;
2121df8bae1dSRodney W. Grimes 	vm_map_t new_map;
2122df8bae1dSRodney W. Grimes 	vm_map_entry_t old_entry;
2123df8bae1dSRodney W. Grimes 	vm_map_entry_t new_entry;
2124de5f6a77SJohn Dyson 	vm_object_t object;
2125df8bae1dSRodney W. Grimes 
2126df8bae1dSRodney W. Grimes 	vm_map_lock(old_map);
2127b823bbd6SMatthew Dillon 	old_map->infork = 1;
2128df8bae1dSRodney W. Grimes 
21292d8acc0fSJohn Dyson 	vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
2130df8bae1dSRodney W. Grimes 	bcopy(&vm1->vm_startcopy, &vm2->vm_startcopy,
2131df8bae1dSRodney W. Grimes 	    (caddr_t) (vm1 + 1) - (caddr_t) &vm1->vm_startcopy);
2132df8bae1dSRodney W. Grimes 	new_map = &vm2->vm_map;	/* XXX */
213347221757SJohn Dyson 	new_map->timestamp = 1;
2134df8bae1dSRodney W. Grimes 
2135df8bae1dSRodney W. Grimes 	old_entry = old_map->header.next;
2136df8bae1dSRodney W. Grimes 
2137df8bae1dSRodney W. Grimes 	while (old_entry != &old_map->header) {
2138afa07f7eSJohn Dyson 		if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2139df8bae1dSRodney W. Grimes 			panic("vm_map_fork: encountered a submap");
2140df8bae1dSRodney W. Grimes 
2141df8bae1dSRodney W. Grimes 		switch (old_entry->inheritance) {
2142df8bae1dSRodney W. Grimes 		case VM_INHERIT_NONE:
2143df8bae1dSRodney W. Grimes 			break;
2144df8bae1dSRodney W. Grimes 
2145df8bae1dSRodney W. Grimes 		case VM_INHERIT_SHARE:
2146df8bae1dSRodney W. Grimes 			/*
2147fed9a903SJohn Dyson 			 * Clone the entry, creating the shared object if necessary.
2148fed9a903SJohn Dyson 			 */
2149fed9a903SJohn Dyson 			object = old_entry->object.vm_object;
2150fed9a903SJohn Dyson 			if (object == NULL) {
2151fed9a903SJohn Dyson 				object = vm_object_allocate(OBJT_DEFAULT,
2152c2e11a03SJohn Dyson 					atop(old_entry->end - old_entry->start));
2153fed9a903SJohn Dyson 				old_entry->object.vm_object = object;
2154fed9a903SJohn Dyson 				old_entry->offset = (vm_offset_t) 0;
21559a2f6362SAlan Cox 			}
21569a2f6362SAlan Cox 
21579a2f6362SAlan Cox 			/*
21589a2f6362SAlan Cox 			 * Add the reference before calling vm_object_shadow
21599a2f6362SAlan Cox 			 * to insure that a shadow object is created.
21609a2f6362SAlan Cox 			 */
21619a2f6362SAlan Cox 			vm_object_reference(object);
21629a2f6362SAlan Cox 			if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
21635069bf57SJohn Dyson 				vm_object_shadow(&old_entry->object.vm_object,
21645069bf57SJohn Dyson 					&old_entry->offset,
2165c2e11a03SJohn Dyson 					atop(old_entry->end - old_entry->start));
21665069bf57SJohn Dyson 				old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
2167d30344bdSIan Dowse 				/* Transfer the second reference too. */
2168d30344bdSIan Dowse 				vm_object_reference(
2169d30344bdSIan Dowse 				    old_entry->object.vm_object);
2170d30344bdSIan Dowse 				vm_object_deallocate(object);
21715069bf57SJohn Dyson 				object = old_entry->object.vm_object;
2172fed9a903SJohn Dyson 			}
2173069e9bc1SDoug Rabson 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
2174fed9a903SJohn Dyson 
2175fed9a903SJohn Dyson 			/*
2176ad5fca3bSAlan Cox 			 * Clone the entry, referencing the shared object.
2177df8bae1dSRodney W. Grimes 			 */
2178df8bae1dSRodney W. Grimes 			new_entry = vm_map_entry_create(new_map);
2179df8bae1dSRodney W. Grimes 			*new_entry = *old_entry;
2180028fe6ecSTor Egge 			new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2181df8bae1dSRodney W. Grimes 			new_entry->wired_count = 0;
2182df8bae1dSRodney W. Grimes 
2183df8bae1dSRodney W. Grimes 			/*
21840d94caffSDavid Greenman 			 * Insert the entry into the new map -- we know we're
21850d94caffSDavid Greenman 			 * inserting at the end of the new map.
2186df8bae1dSRodney W. Grimes 			 */
2187df8bae1dSRodney W. Grimes 
2188df8bae1dSRodney W. Grimes 			vm_map_entry_link(new_map, new_map->header.prev,
2189df8bae1dSRodney W. Grimes 			    new_entry);
2190df8bae1dSRodney W. Grimes 
2191df8bae1dSRodney W. Grimes 			/*
2192df8bae1dSRodney W. Grimes 			 * Update the physical map
2193df8bae1dSRodney W. Grimes 			 */
2194df8bae1dSRodney W. Grimes 
2195df8bae1dSRodney W. Grimes 			pmap_copy(new_map->pmap, old_map->pmap,
2196df8bae1dSRodney W. Grimes 			    new_entry->start,
2197df8bae1dSRodney W. Grimes 			    (old_entry->end - old_entry->start),
2198df8bae1dSRodney W. Grimes 			    old_entry->start);
2199df8bae1dSRodney W. Grimes 			break;
2200df8bae1dSRodney W. Grimes 
2201df8bae1dSRodney W. Grimes 		case VM_INHERIT_COPY:
2202df8bae1dSRodney W. Grimes 			/*
2203df8bae1dSRodney W. Grimes 			 * Clone the entry and link into the map.
2204df8bae1dSRodney W. Grimes 			 */
2205df8bae1dSRodney W. Grimes 			new_entry = vm_map_entry_create(new_map);
2206df8bae1dSRodney W. Grimes 			*new_entry = *old_entry;
2207028fe6ecSTor Egge 			new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2208df8bae1dSRodney W. Grimes 			new_entry->wired_count = 0;
2209df8bae1dSRodney W. Grimes 			new_entry->object.vm_object = NULL;
2210df8bae1dSRodney W. Grimes 			vm_map_entry_link(new_map, new_map->header.prev,
2211df8bae1dSRodney W. Grimes 			    new_entry);
2212bd7e5f99SJohn Dyson 			vm_map_copy_entry(old_map, new_map, old_entry,
2213bd7e5f99SJohn Dyson 			    new_entry);
2214df8bae1dSRodney W. Grimes 			break;
2215df8bae1dSRodney W. Grimes 		}
2216df8bae1dSRodney W. Grimes 		old_entry = old_entry->next;
2217df8bae1dSRodney W. Grimes 	}
2218df8bae1dSRodney W. Grimes 
2219df8bae1dSRodney W. Grimes 	new_map->size = old_map->size;
2220b823bbd6SMatthew Dillon 	old_map->infork = 0;
2221df8bae1dSRodney W. Grimes 	vm_map_unlock(old_map);
2222df8bae1dSRodney W. Grimes 
2223df8bae1dSRodney W. Grimes 	return (vm2);
2224df8bae1dSRodney W. Grimes }
2225df8bae1dSRodney W. Grimes 
222694f7e29aSAlan Cox int
222794f7e29aSAlan Cox vm_map_stack (vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
222894f7e29aSAlan Cox 	      vm_prot_t prot, vm_prot_t max, int cow)
222994f7e29aSAlan Cox {
223094f7e29aSAlan Cox 	vm_map_entry_t prev_entry;
223194f7e29aSAlan Cox 	vm_map_entry_t new_stack_entry;
223294f7e29aSAlan Cox 	vm_size_t      init_ssize;
223394f7e29aSAlan Cox 	int            rv;
223494f7e29aSAlan Cox 
223594f7e29aSAlan Cox 	if (VM_MIN_ADDRESS > 0 && addrbos < VM_MIN_ADDRESS)
223694f7e29aSAlan Cox 		return (KERN_NO_SPACE);
223794f7e29aSAlan Cox 
223894f7e29aSAlan Cox 	if (max_ssize < SGROWSIZ)
223994f7e29aSAlan Cox 		init_ssize = max_ssize;
224094f7e29aSAlan Cox 	else
224194f7e29aSAlan Cox 		init_ssize = SGROWSIZ;
224294f7e29aSAlan Cox 
224394f7e29aSAlan Cox 	vm_map_lock(map);
224494f7e29aSAlan Cox 
224594f7e29aSAlan Cox 	/* If addr is already mapped, no go */
224694f7e29aSAlan Cox 	if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
224794f7e29aSAlan Cox 		vm_map_unlock(map);
224894f7e29aSAlan Cox 		return (KERN_NO_SPACE);
224994f7e29aSAlan Cox 	}
225094f7e29aSAlan Cox 
225194f7e29aSAlan Cox 	/* If we can't accomodate max_ssize in the current mapping,
225294f7e29aSAlan Cox 	 * no go.  However, we need to be aware that subsequent user
225394f7e29aSAlan Cox 	 * mappings might map into the space we have reserved for
225494f7e29aSAlan Cox 	 * stack, and currently this space is not protected.
225594f7e29aSAlan Cox 	 *
225694f7e29aSAlan Cox 	 * Hopefully we will at least detect this condition
225794f7e29aSAlan Cox 	 * when we try to grow the stack.
225894f7e29aSAlan Cox 	 */
225994f7e29aSAlan Cox 	if ((prev_entry->next != &map->header) &&
226094f7e29aSAlan Cox 	    (prev_entry->next->start < addrbos + max_ssize)) {
226194f7e29aSAlan Cox 		vm_map_unlock(map);
226294f7e29aSAlan Cox 		return (KERN_NO_SPACE);
226394f7e29aSAlan Cox 	}
226494f7e29aSAlan Cox 
226594f7e29aSAlan Cox 	/* We initially map a stack of only init_ssize.  We will
226694f7e29aSAlan Cox 	 * grow as needed later.  Since this is to be a grow
226794f7e29aSAlan Cox 	 * down stack, we map at the top of the range.
226894f7e29aSAlan Cox 	 *
226994f7e29aSAlan Cox 	 * Note: we would normally expect prot and max to be
227094f7e29aSAlan Cox 	 * VM_PROT_ALL, and cow to be 0.  Possibly we should
227194f7e29aSAlan Cox 	 * eliminate these as input parameters, and just
227294f7e29aSAlan Cox 	 * pass these values here in the insert call.
227394f7e29aSAlan Cox 	 */
227494f7e29aSAlan Cox 	rv = vm_map_insert(map, NULL, 0, addrbos + max_ssize - init_ssize,
227594f7e29aSAlan Cox 	                   addrbos + max_ssize, prot, max, cow);
227694f7e29aSAlan Cox 
227794f7e29aSAlan Cox 	/* Now set the avail_ssize amount */
227894f7e29aSAlan Cox 	if (rv == KERN_SUCCESS){
227929b45e9eSAlan Cox 		if (prev_entry != &map->header)
228029b45e9eSAlan Cox 			vm_map_clip_end(map, prev_entry, addrbos + max_ssize - init_ssize);
228194f7e29aSAlan Cox 		new_stack_entry = prev_entry->next;
228294f7e29aSAlan Cox 		if (new_stack_entry->end   != addrbos + max_ssize ||
228394f7e29aSAlan Cox 		    new_stack_entry->start != addrbos + max_ssize - init_ssize)
228494f7e29aSAlan Cox 			panic ("Bad entry start/end for new stack entry");
228594f7e29aSAlan Cox 		else
228694f7e29aSAlan Cox 			new_stack_entry->avail_ssize = max_ssize - init_ssize;
228794f7e29aSAlan Cox 	}
228894f7e29aSAlan Cox 
228994f7e29aSAlan Cox 	vm_map_unlock(map);
229094f7e29aSAlan Cox 	return (rv);
229194f7e29aSAlan Cox }
229294f7e29aSAlan Cox 
229394f7e29aSAlan Cox /* Attempts to grow a vm stack entry.  Returns KERN_SUCCESS if the
229494f7e29aSAlan Cox  * desired address is already mapped, or if we successfully grow
229594f7e29aSAlan Cox  * the stack.  Also returns KERN_SUCCESS if addr is outside the
229694f7e29aSAlan Cox  * stack range (this is strange, but preserves compatibility with
229794f7e29aSAlan Cox  * the grow function in vm_machdep.c).
229894f7e29aSAlan Cox  */
229994f7e29aSAlan Cox int
230094f7e29aSAlan Cox vm_map_growstack (struct proc *p, vm_offset_t addr)
230194f7e29aSAlan Cox {
230294f7e29aSAlan Cox 	vm_map_entry_t prev_entry;
230394f7e29aSAlan Cox 	vm_map_entry_t stack_entry;
230494f7e29aSAlan Cox 	vm_map_entry_t new_stack_entry;
230594f7e29aSAlan Cox 	struct vmspace *vm = p->p_vmspace;
230694f7e29aSAlan Cox 	vm_map_t map = &vm->vm_map;
230794f7e29aSAlan Cox 	vm_offset_t    end;
230894f7e29aSAlan Cox 	int      grow_amount;
230994f7e29aSAlan Cox 	int      rv;
231094f7e29aSAlan Cox 	int      is_procstack;
231194f7e29aSAlan Cox Retry:
231294f7e29aSAlan Cox 	vm_map_lock_read(map);
231394f7e29aSAlan Cox 
231494f7e29aSAlan Cox 	/* If addr is already in the entry range, no need to grow.*/
231594f7e29aSAlan Cox 	if (vm_map_lookup_entry(map, addr, &prev_entry)) {
231694f7e29aSAlan Cox 		vm_map_unlock_read(map);
231794f7e29aSAlan Cox 		return (KERN_SUCCESS);
231894f7e29aSAlan Cox 	}
231994f7e29aSAlan Cox 
232094f7e29aSAlan Cox 	if ((stack_entry = prev_entry->next) == &map->header) {
232194f7e29aSAlan Cox 		vm_map_unlock_read(map);
232294f7e29aSAlan Cox 		return (KERN_SUCCESS);
232394f7e29aSAlan Cox 	}
232494f7e29aSAlan Cox 	if (prev_entry == &map->header)
232594f7e29aSAlan Cox 		end = stack_entry->start - stack_entry->avail_ssize;
232694f7e29aSAlan Cox 	else
232794f7e29aSAlan Cox 		end = prev_entry->end;
232894f7e29aSAlan Cox 
232994f7e29aSAlan Cox 	/* This next test mimics the old grow function in vm_machdep.c.
233094f7e29aSAlan Cox 	 * It really doesn't quite make sense, but we do it anyway
233194f7e29aSAlan Cox 	 * for compatibility.
233294f7e29aSAlan Cox 	 *
233394f7e29aSAlan Cox 	 * If not growable stack, return success.  This signals the
233494f7e29aSAlan Cox 	 * caller to proceed as he would normally with normal vm.
233594f7e29aSAlan Cox 	 */
233694f7e29aSAlan Cox 	if (stack_entry->avail_ssize < 1 ||
233794f7e29aSAlan Cox 	    addr >= stack_entry->start ||
233894f7e29aSAlan Cox 	    addr <  stack_entry->start - stack_entry->avail_ssize) {
233994f7e29aSAlan Cox 		vm_map_unlock_read(map);
234094f7e29aSAlan Cox 		return (KERN_SUCCESS);
234194f7e29aSAlan Cox 	}
234294f7e29aSAlan Cox 
234394f7e29aSAlan Cox 	/* Find the minimum grow amount */
234494f7e29aSAlan Cox 	grow_amount = roundup (stack_entry->start - addr, PAGE_SIZE);
234594f7e29aSAlan Cox 	if (grow_amount > stack_entry->avail_ssize) {
234694f7e29aSAlan Cox 		vm_map_unlock_read(map);
234794f7e29aSAlan Cox 		return (KERN_NO_SPACE);
234894f7e29aSAlan Cox 	}
234994f7e29aSAlan Cox 
235094f7e29aSAlan Cox 	/* If there is no longer enough space between the entries
235194f7e29aSAlan Cox 	 * nogo, and adjust the available space.  Note: this
235294f7e29aSAlan Cox 	 * should only happen if the user has mapped into the
235394f7e29aSAlan Cox 	 * stack area after the stack was created, and is
235494f7e29aSAlan Cox 	 * probably an error.
235594f7e29aSAlan Cox 	 *
235694f7e29aSAlan Cox 	 * This also effectively destroys any guard page the user
235794f7e29aSAlan Cox 	 * might have intended by limiting the stack size.
235894f7e29aSAlan Cox 	 */
235994f7e29aSAlan Cox 	if (grow_amount > stack_entry->start - end) {
236094f7e29aSAlan Cox 		if (vm_map_lock_upgrade(map))
236194f7e29aSAlan Cox 			goto Retry;
236294f7e29aSAlan Cox 
236394f7e29aSAlan Cox 		stack_entry->avail_ssize = stack_entry->start - end;
236494f7e29aSAlan Cox 
236594f7e29aSAlan Cox 		vm_map_unlock(map);
236694f7e29aSAlan Cox 		return (KERN_NO_SPACE);
236794f7e29aSAlan Cox 	}
236894f7e29aSAlan Cox 
236994f7e29aSAlan Cox 	is_procstack = addr >= (vm_offset_t)vm->vm_maxsaddr;
237094f7e29aSAlan Cox 
237194f7e29aSAlan Cox 	/* If this is the main process stack, see if we're over the
237294f7e29aSAlan Cox 	 * stack limit.
237394f7e29aSAlan Cox 	 */
23746389da78SAlan Cox 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount >
237594f7e29aSAlan Cox 			     p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
237694f7e29aSAlan Cox 		vm_map_unlock_read(map);
237794f7e29aSAlan Cox 		return (KERN_NO_SPACE);
237894f7e29aSAlan Cox 	}
237994f7e29aSAlan Cox 
238094f7e29aSAlan Cox 	/* Round up the grow amount modulo SGROWSIZ */
238194f7e29aSAlan Cox 	grow_amount = roundup (grow_amount, SGROWSIZ);
238294f7e29aSAlan Cox 	if (grow_amount > stack_entry->avail_ssize) {
238394f7e29aSAlan Cox 		grow_amount = stack_entry->avail_ssize;
238494f7e29aSAlan Cox 	}
23856389da78SAlan Cox 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount >
238694f7e29aSAlan Cox 	                     p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
238794f7e29aSAlan Cox 		grow_amount = p->p_rlimit[RLIMIT_STACK].rlim_cur -
23886389da78SAlan Cox 		              ctob(vm->vm_ssize);
238994f7e29aSAlan Cox 	}
239094f7e29aSAlan Cox 
239194f7e29aSAlan Cox 	if (vm_map_lock_upgrade(map))
239294f7e29aSAlan Cox 		goto Retry;
239394f7e29aSAlan Cox 
239494f7e29aSAlan Cox 	/* Get the preliminary new entry start value */
239594f7e29aSAlan Cox 	addr = stack_entry->start - grow_amount;
239694f7e29aSAlan Cox 
239794f7e29aSAlan Cox 	/* If this puts us into the previous entry, cut back our growth
239894f7e29aSAlan Cox 	 * to the available space.  Also, see the note above.
239994f7e29aSAlan Cox 	 */
240094f7e29aSAlan Cox 	if (addr < end) {
240194f7e29aSAlan Cox 		stack_entry->avail_ssize = stack_entry->start - end;
240294f7e29aSAlan Cox 		addr = end;
240394f7e29aSAlan Cox 	}
240494f7e29aSAlan Cox 
240594f7e29aSAlan Cox 	rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start,
240629b45e9eSAlan Cox 			   VM_PROT_ALL,
240729b45e9eSAlan Cox 			   VM_PROT_ALL,
240894f7e29aSAlan Cox 			   0);
240994f7e29aSAlan Cox 
241094f7e29aSAlan Cox 	/* Adjust the available stack space by the amount we grew. */
241194f7e29aSAlan Cox 	if (rv == KERN_SUCCESS) {
241229b45e9eSAlan Cox 		if (prev_entry != &map->header)
241329b45e9eSAlan Cox 			vm_map_clip_end(map, prev_entry, addr);
241494f7e29aSAlan Cox 		new_stack_entry = prev_entry->next;
241594f7e29aSAlan Cox 		if (new_stack_entry->end   != stack_entry->start  ||
241694f7e29aSAlan Cox 		    new_stack_entry->start != addr)
241794f7e29aSAlan Cox 			panic ("Bad stack grow start/end in new stack entry");
241894f7e29aSAlan Cox 		else {
241994f7e29aSAlan Cox 			new_stack_entry->avail_ssize = stack_entry->avail_ssize -
242094f7e29aSAlan Cox 							(new_stack_entry->end -
242194f7e29aSAlan Cox 							 new_stack_entry->start);
242294f7e29aSAlan Cox 			if (is_procstack)
24236389da78SAlan Cox 				vm->vm_ssize += btoc(new_stack_entry->end -
24246389da78SAlan Cox 						     new_stack_entry->start);
242594f7e29aSAlan Cox 		}
242694f7e29aSAlan Cox 	}
242794f7e29aSAlan Cox 
242894f7e29aSAlan Cox 	vm_map_unlock(map);
242994f7e29aSAlan Cox 	return (rv);
243094f7e29aSAlan Cox 
243194f7e29aSAlan Cox }
243294f7e29aSAlan Cox 
2433df8bae1dSRodney W. Grimes /*
24345856e12eSJohn Dyson  * Unshare the specified VM space for exec.  If other processes are
24355856e12eSJohn Dyson  * mapped to it, then create a new one.  The new vmspace is null.
24365856e12eSJohn Dyson  */
24375856e12eSJohn Dyson 
24385856e12eSJohn Dyson void
24395856e12eSJohn Dyson vmspace_exec(struct proc *p) {
24405856e12eSJohn Dyson 	struct vmspace *oldvmspace = p->p_vmspace;
24415856e12eSJohn Dyson 	struct vmspace *newvmspace;
24425856e12eSJohn Dyson 	vm_map_t map = &p->p_vmspace->vm_map;
24435856e12eSJohn Dyson 
24442d8acc0fSJohn Dyson 	newvmspace = vmspace_alloc(map->min_offset, map->max_offset);
24455856e12eSJohn Dyson 	bcopy(&oldvmspace->vm_startcopy, &newvmspace->vm_startcopy,
24465856e12eSJohn Dyson 	    (caddr_t) (newvmspace + 1) - (caddr_t) &newvmspace->vm_startcopy);
24475856e12eSJohn Dyson 	/*
24485856e12eSJohn Dyson 	 * This code is written like this for prototype purposes.  The
24495856e12eSJohn Dyson 	 * goal is to avoid running down the vmspace here, but let the
24505856e12eSJohn Dyson 	 * other process's that are still using the vmspace to finally
24515856e12eSJohn Dyson 	 * run it down.  Even though there is little or no chance of blocking
24525856e12eSJohn Dyson 	 * here, it is a good idea to keep this form for future mods.
24535856e12eSJohn Dyson 	 */
24545856e12eSJohn Dyson 	vmspace_free(oldvmspace);
24555856e12eSJohn Dyson 	p->p_vmspace = newvmspace;
2456d4da2dbaSAlan Cox 	pmap_pinit2(vmspace_pmap(newvmspace));
24575856e12eSJohn Dyson 	if (p == curproc)
24585856e12eSJohn Dyson 		pmap_activate(p);
24595856e12eSJohn Dyson }
24605856e12eSJohn Dyson 
24615856e12eSJohn Dyson /*
24625856e12eSJohn Dyson  * Unshare the specified VM space for forcing COW.  This
24635856e12eSJohn Dyson  * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
24645856e12eSJohn Dyson  */
24655856e12eSJohn Dyson 
24665856e12eSJohn Dyson void
24675856e12eSJohn Dyson vmspace_unshare(struct proc *p) {
24685856e12eSJohn Dyson 	struct vmspace *oldvmspace = p->p_vmspace;
24695856e12eSJohn Dyson 	struct vmspace *newvmspace;
24705856e12eSJohn Dyson 
24715856e12eSJohn Dyson 	if (oldvmspace->vm_refcnt == 1)
24725856e12eSJohn Dyson 		return;
24735856e12eSJohn Dyson 	newvmspace = vmspace_fork(oldvmspace);
24745856e12eSJohn Dyson 	vmspace_free(oldvmspace);
24755856e12eSJohn Dyson 	p->p_vmspace = newvmspace;
2476d4da2dbaSAlan Cox 	pmap_pinit2(vmspace_pmap(newvmspace));
24775856e12eSJohn Dyson 	if (p == curproc)
24785856e12eSJohn Dyson 		pmap_activate(p);
24795856e12eSJohn Dyson }
24805856e12eSJohn Dyson 
24815856e12eSJohn Dyson 
24825856e12eSJohn Dyson /*
2483df8bae1dSRodney W. Grimes  *	vm_map_lookup:
2484df8bae1dSRodney W. Grimes  *
2485df8bae1dSRodney W. Grimes  *	Finds the VM object, offset, and
2486df8bae1dSRodney W. Grimes  *	protection for a given virtual address in the
2487df8bae1dSRodney W. Grimes  *	specified map, assuming a page fault of the
2488df8bae1dSRodney W. Grimes  *	type specified.
2489df8bae1dSRodney W. Grimes  *
2490df8bae1dSRodney W. Grimes  *	Leaves the map in question locked for read; return
2491df8bae1dSRodney W. Grimes  *	values are guaranteed until a vm_map_lookup_done
2492df8bae1dSRodney W. Grimes  *	call is performed.  Note that the map argument
2493df8bae1dSRodney W. Grimes  *	is in/out; the returned map must be used in
2494df8bae1dSRodney W. Grimes  *	the call to vm_map_lookup_done.
2495df8bae1dSRodney W. Grimes  *
2496df8bae1dSRodney W. Grimes  *	A handle (out_entry) is returned for use in
2497df8bae1dSRodney W. Grimes  *	vm_map_lookup_done, to make that fast.
2498df8bae1dSRodney W. Grimes  *
2499df8bae1dSRodney W. Grimes  *	If a lookup is requested with "write protection"
2500df8bae1dSRodney W. Grimes  *	specified, the map may be changed to perform virtual
2501df8bae1dSRodney W. Grimes  *	copying operations, although the data referenced will
2502df8bae1dSRodney W. Grimes  *	remain the same.
2503df8bae1dSRodney W. Grimes  */
2504df8bae1dSRodney W. Grimes int
2505b9dcd593SBruce Evans vm_map_lookup(vm_map_t *var_map,		/* IN/OUT */
2506b9dcd593SBruce Evans 	      vm_offset_t vaddr,
250747221757SJohn Dyson 	      vm_prot_t fault_typea,
2508b9dcd593SBruce Evans 	      vm_map_entry_t *out_entry,	/* OUT */
2509b9dcd593SBruce Evans 	      vm_object_t *object,		/* OUT */
2510b9dcd593SBruce Evans 	      vm_pindex_t *pindex,		/* OUT */
2511b9dcd593SBruce Evans 	      vm_prot_t *out_prot,		/* OUT */
25122d8acc0fSJohn Dyson 	      boolean_t *wired)			/* OUT */
2513df8bae1dSRodney W. Grimes {
2514c0877f10SJohn Dyson 	vm_map_entry_t entry;
2515c0877f10SJohn Dyson 	vm_map_t map = *var_map;
2516c0877f10SJohn Dyson 	vm_prot_t prot;
251747221757SJohn Dyson 	vm_prot_t fault_type = fault_typea;
2518df8bae1dSRodney W. Grimes 
2519df8bae1dSRodney W. Grimes RetryLookup:;
2520df8bae1dSRodney W. Grimes 
2521df8bae1dSRodney W. Grimes 	/*
2522df8bae1dSRodney W. Grimes 	 * Lookup the faulting address.
2523df8bae1dSRodney W. Grimes 	 */
2524df8bae1dSRodney W. Grimes 
2525df8bae1dSRodney W. Grimes 	vm_map_lock_read(map);
2526df8bae1dSRodney W. Grimes 
2527df8bae1dSRodney W. Grimes #define	RETURN(why) \
2528df8bae1dSRodney W. Grimes 		{ \
2529df8bae1dSRodney W. Grimes 		vm_map_unlock_read(map); \
2530df8bae1dSRodney W. Grimes 		return(why); \
2531df8bae1dSRodney W. Grimes 		}
2532df8bae1dSRodney W. Grimes 
2533df8bae1dSRodney W. Grimes 	/*
25340d94caffSDavid Greenman 	 * If the map has an interesting hint, try it before calling full
25350d94caffSDavid Greenman 	 * blown lookup routine.
2536df8bae1dSRodney W. Grimes 	 */
2537df8bae1dSRodney W. Grimes 
2538df8bae1dSRodney W. Grimes 	entry = map->hint;
2539df8bae1dSRodney W. Grimes 
2540df8bae1dSRodney W. Grimes 	*out_entry = entry;
2541df8bae1dSRodney W. Grimes 
2542df8bae1dSRodney W. Grimes 	if ((entry == &map->header) ||
2543df8bae1dSRodney W. Grimes 	    (vaddr < entry->start) || (vaddr >= entry->end)) {
2544df8bae1dSRodney W. Grimes 		vm_map_entry_t tmp_entry;
2545df8bae1dSRodney W. Grimes 
2546df8bae1dSRodney W. Grimes 		/*
25470d94caffSDavid Greenman 		 * Entry was either not a valid hint, or the vaddr was not
25480d94caffSDavid Greenman 		 * contained in the entry, so do a full lookup.
2549df8bae1dSRodney W. Grimes 		 */
2550df8bae1dSRodney W. Grimes 		if (!vm_map_lookup_entry(map, vaddr, &tmp_entry))
2551df8bae1dSRodney W. Grimes 			RETURN(KERN_INVALID_ADDRESS);
2552df8bae1dSRodney W. Grimes 
2553df8bae1dSRodney W. Grimes 		entry = tmp_entry;
2554df8bae1dSRodney W. Grimes 		*out_entry = entry;
2555df8bae1dSRodney W. Grimes 	}
2556b7b2aac2SJohn Dyson 
2557df8bae1dSRodney W. Grimes 	/*
2558df8bae1dSRodney W. Grimes 	 * Handle submaps.
2559df8bae1dSRodney W. Grimes 	 */
2560df8bae1dSRodney W. Grimes 
2561afa07f7eSJohn Dyson 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
2562df8bae1dSRodney W. Grimes 		vm_map_t old_map = map;
2563df8bae1dSRodney W. Grimes 
2564df8bae1dSRodney W. Grimes 		*var_map = map = entry->object.sub_map;
2565df8bae1dSRodney W. Grimes 		vm_map_unlock_read(old_map);
2566df8bae1dSRodney W. Grimes 		goto RetryLookup;
2567df8bae1dSRodney W. Grimes 	}
2568a04c970aSJohn Dyson 
2569df8bae1dSRodney W. Grimes 	/*
25700d94caffSDavid Greenman 	 * Check whether this task is allowed to have this page.
2571a04c970aSJohn Dyson 	 * Note the special case for MAP_ENTRY_COW
2572a04c970aSJohn Dyson 	 * pages with an override.  This is to implement a forced
2573a04c970aSJohn Dyson 	 * COW for debuggers.
2574df8bae1dSRodney W. Grimes 	 */
2575df8bae1dSRodney W. Grimes 
2576480ba2f5SJohn Dyson 	if (fault_type & VM_PROT_OVERRIDE_WRITE)
2577480ba2f5SJohn Dyson 		prot = entry->max_protection;
2578480ba2f5SJohn Dyson 	else
2579df8bae1dSRodney W. Grimes 		prot = entry->protection;
258047221757SJohn Dyson 
258147221757SJohn Dyson 	fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
258247221757SJohn Dyson 	if ((fault_type & prot) != fault_type) {
258347221757SJohn Dyson 			RETURN(KERN_PROTECTION_FAILURE);
258447221757SJohn Dyson 	}
258547221757SJohn Dyson 
25862ed14a92SAlan Cox 	if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
258747221757SJohn Dyson 	    (entry->eflags & MAP_ENTRY_COW) &&
25882ed14a92SAlan Cox 	    (fault_type & VM_PROT_WRITE) &&
258947221757SJohn Dyson 	    (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
2590df8bae1dSRodney W. Grimes 		RETURN(KERN_PROTECTION_FAILURE);
2591a04c970aSJohn Dyson 	}
2592df8bae1dSRodney W. Grimes 
2593df8bae1dSRodney W. Grimes 	/*
25940d94caffSDavid Greenman 	 * If this page is not pageable, we have to get it for all possible
25950d94caffSDavid Greenman 	 * accesses.
2596df8bae1dSRodney W. Grimes 	 */
2597df8bae1dSRodney W. Grimes 
259805f0fdd2SPoul-Henning Kamp 	*wired = (entry->wired_count != 0);
259905f0fdd2SPoul-Henning Kamp 	if (*wired)
2600df8bae1dSRodney W. Grimes 		prot = fault_type = entry->protection;
2601df8bae1dSRodney W. Grimes 
2602df8bae1dSRodney W. Grimes 	/*
2603df8bae1dSRodney W. Grimes 	 * If the entry was copy-on-write, we either ...
2604df8bae1dSRodney W. Grimes 	 */
2605df8bae1dSRodney W. Grimes 
2606afa07f7eSJohn Dyson 	if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
2607df8bae1dSRodney W. Grimes 		/*
26080d94caffSDavid Greenman 		 * If we want to write the page, we may as well handle that
2609ad5fca3bSAlan Cox 		 * now since we've got the map locked.
2610df8bae1dSRodney W. Grimes 		 *
26110d94caffSDavid Greenman 		 * If we don't need to write the page, we just demote the
26120d94caffSDavid Greenman 		 * permissions allowed.
2613df8bae1dSRodney W. Grimes 		 */
2614df8bae1dSRodney W. Grimes 
2615df8bae1dSRodney W. Grimes 		if (fault_type & VM_PROT_WRITE) {
2616df8bae1dSRodney W. Grimes 			/*
26170d94caffSDavid Greenman 			 * Make a new object, and place it in the object
26180d94caffSDavid Greenman 			 * chain.  Note that no new references have appeared
2619ad5fca3bSAlan Cox 			 * -- one just moved from the map to the new
26200d94caffSDavid Greenman 			 * object.
2621df8bae1dSRodney W. Grimes 			 */
2622df8bae1dSRodney W. Grimes 
26239b09b6c7SMatthew Dillon 			if (vm_map_lock_upgrade(map))
2624df8bae1dSRodney W. Grimes 				goto RetryLookup;
26259b09b6c7SMatthew Dillon 
2626df8bae1dSRodney W. Grimes 			vm_object_shadow(
2627df8bae1dSRodney W. Grimes 			    &entry->object.vm_object,
2628df8bae1dSRodney W. Grimes 			    &entry->offset,
2629c2e11a03SJohn Dyson 			    atop(entry->end - entry->start));
2630df8bae1dSRodney W. Grimes 
2631afa07f7eSJohn Dyson 			entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
26329b09b6c7SMatthew Dillon 			vm_map_lock_downgrade(map);
26330d94caffSDavid Greenman 		} else {
2634df8bae1dSRodney W. Grimes 			/*
26350d94caffSDavid Greenman 			 * We're attempting to read a copy-on-write page --
26360d94caffSDavid Greenman 			 * don't allow writes.
2637df8bae1dSRodney W. Grimes 			 */
2638df8bae1dSRodney W. Grimes 
26392d8acc0fSJohn Dyson 			prot &= ~VM_PROT_WRITE;
2640df8bae1dSRodney W. Grimes 		}
2641df8bae1dSRodney W. Grimes 	}
26422d8acc0fSJohn Dyson 
2643df8bae1dSRodney W. Grimes 	/*
2644df8bae1dSRodney W. Grimes 	 * Create an object if necessary.
2645df8bae1dSRodney W. Grimes 	 */
26464e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL &&
26474e71e795SMatthew Dillon 	    !map->system_map) {
26489b09b6c7SMatthew Dillon 		if (vm_map_lock_upgrade(map))
2649df8bae1dSRodney W. Grimes 			goto RetryLookup;
26509b09b6c7SMatthew Dillon 
265124a1cce3SDavid Greenman 		entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
2652c2e11a03SJohn Dyson 		    atop(entry->end - entry->start));
2653df8bae1dSRodney W. Grimes 		entry->offset = 0;
26549b09b6c7SMatthew Dillon 		vm_map_lock_downgrade(map);
2655df8bae1dSRodney W. Grimes 	}
2656b5b40fa6SJohn Dyson 
2657df8bae1dSRodney W. Grimes 	/*
26580d94caffSDavid Greenman 	 * Return the object/offset from this entry.  If the entry was
26590d94caffSDavid Greenman 	 * copy-on-write or empty, it has been fixed up.
2660df8bae1dSRodney W. Grimes 	 */
2661df8bae1dSRodney W. Grimes 
26629b09b6c7SMatthew Dillon 	*pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
2663df8bae1dSRodney W. Grimes 	*object = entry->object.vm_object;
2664df8bae1dSRodney W. Grimes 
2665df8bae1dSRodney W. Grimes 	/*
2666df8bae1dSRodney W. Grimes 	 * Return whether this is the only map sharing this data.
2667df8bae1dSRodney W. Grimes 	 */
2668df8bae1dSRodney W. Grimes 
2669df8bae1dSRodney W. Grimes 	*out_prot = prot;
2670df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
2671df8bae1dSRodney W. Grimes 
2672df8bae1dSRodney W. Grimes #undef	RETURN
2673df8bae1dSRodney W. Grimes }
2674df8bae1dSRodney W. Grimes 
2675df8bae1dSRodney W. Grimes /*
2676df8bae1dSRodney W. Grimes  *	vm_map_lookup_done:
2677df8bae1dSRodney W. Grimes  *
2678df8bae1dSRodney W. Grimes  *	Releases locks acquired by a vm_map_lookup
2679df8bae1dSRodney W. Grimes  *	(according to the handle returned by that lookup).
2680df8bae1dSRodney W. Grimes  */
2681df8bae1dSRodney W. Grimes 
26820d94caffSDavid Greenman void
26830d94caffSDavid Greenman vm_map_lookup_done(map, entry)
2684c0877f10SJohn Dyson 	vm_map_t map;
2685df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
2686df8bae1dSRodney W. Grimes {
2687df8bae1dSRodney W. Grimes 	/*
2688df8bae1dSRodney W. Grimes 	 * Unlock the main-level map
2689df8bae1dSRodney W. Grimes 	 */
2690df8bae1dSRodney W. Grimes 
2691df8bae1dSRodney W. Grimes 	vm_map_unlock_read(map);
2692df8bae1dSRodney W. Grimes }
2693df8bae1dSRodney W. Grimes 
26941efb74fbSJohn Dyson /*
26951efb74fbSJohn Dyson  * Implement uiomove with VM operations.  This handles (and collateral changes)
26961efb74fbSJohn Dyson  * support every combination of source object modification, and COW type
26971efb74fbSJohn Dyson  * operations.
26981efb74fbSJohn Dyson  */
26991efb74fbSJohn Dyson int
270047221757SJohn Dyson vm_uiomove(mapa, srcobject, cp, cnta, uaddra, npages)
27011efb74fbSJohn Dyson 	vm_map_t mapa;
27021efb74fbSJohn Dyson 	vm_object_t srcobject;
27031efb74fbSJohn Dyson 	off_t cp;
270447221757SJohn Dyson 	int cnta;
27051efb74fbSJohn Dyson 	vm_offset_t uaddra;
270695e5e988SJohn Dyson 	int *npages;
27071efb74fbSJohn Dyson {
27081efb74fbSJohn Dyson 	vm_map_t map;
270947221757SJohn Dyson 	vm_object_t first_object, oldobject, object;
27102d8acc0fSJohn Dyson 	vm_map_entry_t entry;
27111efb74fbSJohn Dyson 	vm_prot_t prot;
27122d8acc0fSJohn Dyson 	boolean_t wired;
27131efb74fbSJohn Dyson 	int tcnt, rv;
27142d8acc0fSJohn Dyson 	vm_offset_t uaddr, start, end, tend;
27151efb74fbSJohn Dyson 	vm_pindex_t first_pindex, osize, oindex;
27161efb74fbSJohn Dyson 	off_t ooffset;
271747221757SJohn Dyson 	int cnt;
27181efb74fbSJohn Dyson 
271995e5e988SJohn Dyson 	if (npages)
272095e5e988SJohn Dyson 		*npages = 0;
272195e5e988SJohn Dyson 
272247221757SJohn Dyson 	cnt = cnta;
27232d8acc0fSJohn Dyson 	uaddr = uaddra;
27242d8acc0fSJohn Dyson 
27251efb74fbSJohn Dyson 	while (cnt > 0) {
27261efb74fbSJohn Dyson 		map = mapa;
27271efb74fbSJohn Dyson 
27281efb74fbSJohn Dyson 		if ((vm_map_lookup(&map, uaddr,
27292d8acc0fSJohn Dyson 			VM_PROT_READ, &entry, &first_object,
27302d8acc0fSJohn Dyson 			&first_pindex, &prot, &wired)) != KERN_SUCCESS) {
27311efb74fbSJohn Dyson 			return EFAULT;
27321efb74fbSJohn Dyson 		}
27331efb74fbSJohn Dyson 
27342d8acc0fSJohn Dyson 		vm_map_clip_start(map, entry, uaddr);
27351efb74fbSJohn Dyson 
27361efb74fbSJohn Dyson 		tcnt = cnt;
27372d8acc0fSJohn Dyson 		tend = uaddr + tcnt;
27382d8acc0fSJohn Dyson 		if (tend > entry->end) {
27392d8acc0fSJohn Dyson 			tcnt = entry->end - uaddr;
27402d8acc0fSJohn Dyson 			tend = entry->end;
27412d8acc0fSJohn Dyson 		}
27421efb74fbSJohn Dyson 
27432d8acc0fSJohn Dyson 		vm_map_clip_end(map, entry, tend);
27441efb74fbSJohn Dyson 
27452d8acc0fSJohn Dyson 		start = entry->start;
27462d8acc0fSJohn Dyson 		end = entry->end;
27471efb74fbSJohn Dyson 
2748c2e11a03SJohn Dyson 		osize = atop(tcnt);
274995e5e988SJohn Dyson 
2750925a3a41SJohn Dyson 		oindex = OFF_TO_IDX(cp);
275195e5e988SJohn Dyson 		if (npages) {
2752925a3a41SJohn Dyson 			vm_pindex_t idx;
275395e5e988SJohn Dyson 			for (idx = 0; idx < osize; idx++) {
275495e5e988SJohn Dyson 				vm_page_t m;
2755925a3a41SJohn Dyson 				if ((m = vm_page_lookup(srcobject, oindex + idx)) == NULL) {
27562d8acc0fSJohn Dyson 					vm_map_lookup_done(map, entry);
275795e5e988SJohn Dyson 					return 0;
275895e5e988SJohn Dyson 				}
27591c7c3c6aSMatthew Dillon 				/*
27601c7c3c6aSMatthew Dillon 				 * disallow busy or invalid pages, but allow
27611c7c3c6aSMatthew Dillon 				 * m->busy pages if they are entirely valid.
27621c7c3c6aSMatthew Dillon 				 */
2763925a3a41SJohn Dyson 				if ((m->flags & PG_BUSY) ||
276495e5e988SJohn Dyson 					((m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL)) {
27652d8acc0fSJohn Dyson 					vm_map_lookup_done(map, entry);
276695e5e988SJohn Dyson 					return 0;
276795e5e988SJohn Dyson 				}
276895e5e988SJohn Dyson 			}
276995e5e988SJohn Dyson 		}
277095e5e988SJohn Dyson 
27711efb74fbSJohn Dyson /*
27721efb74fbSJohn Dyson  * If we are changing an existing map entry, just redirect
27731efb74fbSJohn Dyson  * the object, and change mappings.
27741efb74fbSJohn Dyson  */
27752d8acc0fSJohn Dyson 		if ((first_object->type == OBJT_VNODE) &&
27762d8acc0fSJohn Dyson 			((oldobject = entry->object.vm_object) == first_object)) {
27772d8acc0fSJohn Dyson 
27782d8acc0fSJohn Dyson 			if ((entry->offset != cp) || (oldobject != srcobject)) {
27792d8acc0fSJohn Dyson 				/*
27802d8acc0fSJohn Dyson    				* Remove old window into the file
27812d8acc0fSJohn Dyson    				*/
27822d8acc0fSJohn Dyson 				pmap_remove (map->pmap, uaddr, tend);
27832d8acc0fSJohn Dyson 
27842d8acc0fSJohn Dyson 				/*
27852d8acc0fSJohn Dyson    				* Force copy on write for mmaped regions
27862d8acc0fSJohn Dyson    				*/
27872d8acc0fSJohn Dyson 				vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
27882d8acc0fSJohn Dyson 
27892d8acc0fSJohn Dyson 				/*
27902d8acc0fSJohn Dyson    				* Point the object appropriately
27912d8acc0fSJohn Dyson    				*/
27922d8acc0fSJohn Dyson 				if (oldobject != srcobject) {
27932d8acc0fSJohn Dyson 
27942d8acc0fSJohn Dyson 				/*
27952d8acc0fSJohn Dyson    				* Set the object optimization hint flag
27962d8acc0fSJohn Dyson    				*/
2797069e9bc1SDoug Rabson 					vm_object_set_flag(srcobject, OBJ_OPT);
27982d8acc0fSJohn Dyson 					vm_object_reference(srcobject);
27992d8acc0fSJohn Dyson 					entry->object.vm_object = srcobject;
28002d8acc0fSJohn Dyson 
28012d8acc0fSJohn Dyson 					if (oldobject) {
28022d8acc0fSJohn Dyson 						vm_object_deallocate(oldobject);
28032d8acc0fSJohn Dyson 					}
28042d8acc0fSJohn Dyson 				}
28052d8acc0fSJohn Dyson 
28062d8acc0fSJohn Dyson 				entry->offset = cp;
28072d8acc0fSJohn Dyson 				map->timestamp++;
28082d8acc0fSJohn Dyson 			} else {
28092d8acc0fSJohn Dyson 				pmap_remove (map->pmap, uaddr, tend);
28102d8acc0fSJohn Dyson 			}
28112d8acc0fSJohn Dyson 
28122d8acc0fSJohn Dyson 		} else if ((first_object->ref_count == 1) &&
2813925a3a41SJohn Dyson 			(first_object->size == osize) &&
281447221757SJohn Dyson 			((first_object->type == OBJT_DEFAULT) ||
281547221757SJohn Dyson 				(first_object->type == OBJT_SWAP)) ) {
2816925a3a41SJohn Dyson 
2817925a3a41SJohn Dyson 			oldobject = first_object->backing_object;
2818925a3a41SJohn Dyson 
2819925a3a41SJohn Dyson 			if ((first_object->backing_object_offset != cp) ||
2820925a3a41SJohn Dyson 				(oldobject != srcobject)) {
2821925a3a41SJohn Dyson 				/*
2822925a3a41SJohn Dyson    				* Remove old window into the file
2823925a3a41SJohn Dyson    				*/
28242d8acc0fSJohn Dyson 				pmap_remove (map->pmap, uaddr, tend);
2825925a3a41SJohn Dyson 
2826925a3a41SJohn Dyson 				/*
282747221757SJohn Dyson 				 * Remove unneeded old pages
282847221757SJohn Dyson 				 */
282947221757SJohn Dyson 				vm_object_page_remove(first_object, 0, 0, 0);
283047221757SJohn Dyson 
283147221757SJohn Dyson 				/*
283247221757SJohn Dyson 				 * Invalidate swap space
283347221757SJohn Dyson 				 */
283447221757SJohn Dyson 				if (first_object->type == OBJT_SWAP) {
283547221757SJohn Dyson 					swap_pager_freespace(first_object,
28361c7c3c6aSMatthew Dillon 						0,
283747221757SJohn Dyson 						first_object->size);
283847221757SJohn Dyson 				}
283947221757SJohn Dyson 
284047221757SJohn Dyson 				/*
2841925a3a41SJohn Dyson    				* Force copy on write for mmaped regions
2842925a3a41SJohn Dyson    				*/
284347221757SJohn Dyson 				vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
28441efb74fbSJohn Dyson 
28451efb74fbSJohn Dyson 				/*
28461efb74fbSJohn Dyson    				* Point the object appropriately
28471efb74fbSJohn Dyson    				*/
2848925a3a41SJohn Dyson 				if (oldobject != srcobject) {
284947221757SJohn Dyson 
2850925a3a41SJohn Dyson 				/*
2851925a3a41SJohn Dyson    				* Set the object optimization hint flag
2852925a3a41SJohn Dyson    				*/
2853069e9bc1SDoug Rabson 					vm_object_set_flag(srcobject, OBJ_OPT);
2854925a3a41SJohn Dyson 					vm_object_reference(srcobject);
2855925a3a41SJohn Dyson 
2856925a3a41SJohn Dyson 					if (oldobject) {
2857925a3a41SJohn Dyson 						TAILQ_REMOVE(&oldobject->shadow_head,
2858925a3a41SJohn Dyson 							first_object, shadow_list);
2859925a3a41SJohn Dyson 						oldobject->shadow_count--;
2860b4309055SMatthew Dillon 						/* XXX bump generation? */
2861925a3a41SJohn Dyson 						vm_object_deallocate(oldobject);
2862925a3a41SJohn Dyson 					}
2863925a3a41SJohn Dyson 
2864925a3a41SJohn Dyson 					TAILQ_INSERT_TAIL(&srcobject->shadow_head,
2865925a3a41SJohn Dyson 						first_object, shadow_list);
2866925a3a41SJohn Dyson 					srcobject->shadow_count++;
2867b4309055SMatthew Dillon 					/* XXX bump generation? */
2868925a3a41SJohn Dyson 
2869925a3a41SJohn Dyson 					first_object->backing_object = srcobject;
2870925a3a41SJohn Dyson 				}
28711efb74fbSJohn Dyson 				first_object->backing_object_offset = cp;
28722d8acc0fSJohn Dyson 				map->timestamp++;
2873925a3a41SJohn Dyson 			} else {
28742d8acc0fSJohn Dyson 				pmap_remove (map->pmap, uaddr, tend);
2875925a3a41SJohn Dyson 			}
28761efb74fbSJohn Dyson /*
28771efb74fbSJohn Dyson  * Otherwise, we have to do a logical mmap.
28781efb74fbSJohn Dyson  */
28791efb74fbSJohn Dyson 		} else {
28801efb74fbSJohn Dyson 
2881069e9bc1SDoug Rabson 			vm_object_set_flag(srcobject, OBJ_OPT);
2882925a3a41SJohn Dyson 			vm_object_reference(srcobject);
28831efb74fbSJohn Dyson 
28842d8acc0fSJohn Dyson 			pmap_remove (map->pmap, uaddr, tend);
28851efb74fbSJohn Dyson 
288647221757SJohn Dyson 			vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
288747221757SJohn Dyson 			vm_map_lock_upgrade(map);
28881efb74fbSJohn Dyson 
28892d8acc0fSJohn Dyson 			if (entry == &map->header) {
28901efb74fbSJohn Dyson 				map->first_free = &map->header;
28911efb74fbSJohn Dyson 			} else if (map->first_free->start >= start) {
28922d8acc0fSJohn Dyson 				map->first_free = entry->prev;
28931efb74fbSJohn Dyson 			}
28941efb74fbSJohn Dyson 
28952d8acc0fSJohn Dyson 			SAVE_HINT(map, entry->prev);
28962d8acc0fSJohn Dyson 			vm_map_entry_delete(map, entry);
28971efb74fbSJohn Dyson 
28982d8acc0fSJohn Dyson 			object = srcobject;
28992d8acc0fSJohn Dyson 			ooffset = cp;
29002d8acc0fSJohn Dyson 
29012d8acc0fSJohn Dyson 			rv = vm_map_insert(map, object, ooffset, start, tend,
2902e5f13bddSAlan Cox 				VM_PROT_ALL, VM_PROT_ALL, MAP_COPY_ON_WRITE);
29031efb74fbSJohn Dyson 
29041efb74fbSJohn Dyson 			if (rv != KERN_SUCCESS)
29051efb74fbSJohn Dyson 				panic("vm_uiomove: could not insert new entry: %d", rv);
29061efb74fbSJohn Dyson 		}
29071efb74fbSJohn Dyson 
29081efb74fbSJohn Dyson /*
29091efb74fbSJohn Dyson  * Map the window directly, if it is already in memory
29101efb74fbSJohn Dyson  */
29112d8acc0fSJohn Dyson 		pmap_object_init_pt(map->pmap, uaddr,
29122d8acc0fSJohn Dyson 			srcobject, oindex, tcnt, 0);
29131efb74fbSJohn Dyson 
291447221757SJohn Dyson 		map->timestamp++;
29151efb74fbSJohn Dyson 		vm_map_unlock(map);
29161efb74fbSJohn Dyson 
29171efb74fbSJohn Dyson 		cnt -= tcnt;
29182d8acc0fSJohn Dyson 		uaddr += tcnt;
29191efb74fbSJohn Dyson 		cp += tcnt;
292095e5e988SJohn Dyson 		if (npages)
292195e5e988SJohn Dyson 			*npages += osize;
29221efb74fbSJohn Dyson 	}
29231efb74fbSJohn Dyson 	return 0;
29241efb74fbSJohn Dyson }
29251efb74fbSJohn Dyson 
29261efb74fbSJohn Dyson /*
29271efb74fbSJohn Dyson  * Performs the copy_on_write operations necessary to allow the virtual copies
29281efb74fbSJohn Dyson  * into user space to work.  This has to be called for write(2) system calls
29291efb74fbSJohn Dyson  * from other processes, file unlinking, and file size shrinkage.
29301efb74fbSJohn Dyson  */
29311efb74fbSJohn Dyson void
29321efb74fbSJohn Dyson vm_freeze_copyopts(object, froma, toa)
29331efb74fbSJohn Dyson 	vm_object_t object;
29341efb74fbSJohn Dyson 	vm_pindex_t froma, toa;
29351efb74fbSJohn Dyson {
2936f5ef029eSPoul-Henning Kamp 	int rv;
2937f5ef029eSPoul-Henning Kamp 	vm_object_t robject;
2938f5ef029eSPoul-Henning Kamp 	vm_pindex_t idx;
29391efb74fbSJohn Dyson 
29402d8acc0fSJohn Dyson 	if ((object == NULL) ||
294195e5e988SJohn Dyson 		((object->flags & OBJ_OPT) == 0))
294295e5e988SJohn Dyson 		return;
29431efb74fbSJohn Dyson 
29441efb74fbSJohn Dyson 	if (object->shadow_count > object->ref_count)
29451efb74fbSJohn Dyson 		panic("vm_freeze_copyopts: sc > rc");
29461efb74fbSJohn Dyson 
29478aef1712SMatthew Dillon 	while((robject = TAILQ_FIRST(&object->shadow_head)) != NULL) {
29481efb74fbSJohn Dyson 		vm_pindex_t bo_pindex;
29491efb74fbSJohn Dyson 		vm_page_t m_in, m_out;
29501efb74fbSJohn Dyson 
29511efb74fbSJohn Dyson 		bo_pindex = OFF_TO_IDX(robject->backing_object_offset);
29521efb74fbSJohn Dyson 
295395e5e988SJohn Dyson 		vm_object_reference(robject);
2954925a3a41SJohn Dyson 
295566095752SJohn Dyson 		vm_object_pip_wait(robject, "objfrz");
2956925a3a41SJohn Dyson 
29571efb74fbSJohn Dyson 		if (robject->ref_count == 1) {
29581efb74fbSJohn Dyson 			vm_object_deallocate(robject);
29591efb74fbSJohn Dyson 			continue;
29601efb74fbSJohn Dyson 		}
29611efb74fbSJohn Dyson 
2962d474eaaaSDoug Rabson 		vm_object_pip_add(robject, 1);
29631efb74fbSJohn Dyson 
296447221757SJohn Dyson 		for (idx = 0; idx < robject->size; idx++) {
29651efb74fbSJohn Dyson 
296695461b45SJohn Dyson 			m_out = vm_page_grab(robject, idx,
296795461b45SJohn Dyson 						VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
29681efb74fbSJohn Dyson 
29691efb74fbSJohn Dyson 			if (m_out->valid == 0) {
297095461b45SJohn Dyson 				m_in = vm_page_grab(object, bo_pindex + idx,
297195461b45SJohn Dyson 						VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
297295461b45SJohn Dyson 				if (m_in->valid == 0) {
297347221757SJohn Dyson 					rv = vm_pager_get_pages(object, &m_in, 1, 0);
297447221757SJohn Dyson 					if (rv != VM_PAGER_OK) {
29753efc015bSPeter Wemm 						printf("vm_freeze_copyopts: cannot read page from file: %lx\n", (long)m_in->pindex);
297647221757SJohn Dyson 						continue;
29771efb74fbSJohn Dyson 					}
297895461b45SJohn Dyson 					vm_page_deactivate(m_in);
297947221757SJohn Dyson 				}
298047221757SJohn Dyson 
298147221757SJohn Dyson 				vm_page_protect(m_in, VM_PROT_NONE);
298247221757SJohn Dyson 				pmap_copy_page(VM_PAGE_TO_PHYS(m_in), VM_PAGE_TO_PHYS(m_out));
298395461b45SJohn Dyson 				m_out->valid = m_in->valid;
29847dbf82dcSMatthew Dillon 				vm_page_dirty(m_out);
298595461b45SJohn Dyson 				vm_page_activate(m_out);
2986e69763a3SDoug Rabson 				vm_page_wakeup(m_in);
29871efb74fbSJohn Dyson 			}
2988e69763a3SDoug Rabson 			vm_page_wakeup(m_out);
298947221757SJohn Dyson 		}
2990925a3a41SJohn Dyson 
29911efb74fbSJohn Dyson 		object->shadow_count--;
299247221757SJohn Dyson 		object->ref_count--;
29931efb74fbSJohn Dyson 		TAILQ_REMOVE(&object->shadow_head, robject, shadow_list);
29941efb74fbSJohn Dyson 		robject->backing_object = NULL;
29951efb74fbSJohn Dyson 		robject->backing_object_offset = 0;
29961efb74fbSJohn Dyson 
299747221757SJohn Dyson 		vm_object_pip_wakeup(robject);
29981efb74fbSJohn Dyson 		vm_object_deallocate(robject);
29991efb74fbSJohn Dyson 	}
300047221757SJohn Dyson 
3001069e9bc1SDoug Rabson 	vm_object_clear_flag(object, OBJ_OPT);
30021efb74fbSJohn Dyson }
30031efb74fbSJohn Dyson 
3004c7c34a24SBruce Evans #include "opt_ddb.h"
3005c3cb3e12SDavid Greenman #ifdef DDB
3006c7c34a24SBruce Evans #include <sys/kernel.h>
3007c7c34a24SBruce Evans 
3008c7c34a24SBruce Evans #include <ddb/ddb.h>
3009c7c34a24SBruce Evans 
3010df8bae1dSRodney W. Grimes /*
3011df8bae1dSRodney W. Grimes  *	vm_map_print:	[ debug ]
3012df8bae1dSRodney W. Grimes  */
3013c7c34a24SBruce Evans DB_SHOW_COMMAND(map, vm_map_print)
3014df8bae1dSRodney W. Grimes {
301595e5e988SJohn Dyson 	static int nlines;
3016c7c34a24SBruce Evans 	/* XXX convert args. */
3017c0877f10SJohn Dyson 	vm_map_t map = (vm_map_t)addr;
3018c7c34a24SBruce Evans 	boolean_t full = have_addr;
3019df8bae1dSRodney W. Grimes 
3020c0877f10SJohn Dyson 	vm_map_entry_t entry;
3021c7c34a24SBruce Evans 
3022e5f251d2SAlan Cox 	db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
3023e5f251d2SAlan Cox 	    (void *)map,
3024101eeb7fSBruce Evans 	    (void *)map->pmap, map->nentries, map->timestamp);
302595e5e988SJohn Dyson 	nlines++;
3026df8bae1dSRodney W. Grimes 
3027c7c34a24SBruce Evans 	if (!full && db_indent)
3028df8bae1dSRodney W. Grimes 		return;
3029df8bae1dSRodney W. Grimes 
3030c7c34a24SBruce Evans 	db_indent += 2;
3031df8bae1dSRodney W. Grimes 	for (entry = map->header.next; entry != &map->header;
3032df8bae1dSRodney W. Grimes 	    entry = entry->next) {
3033fc62ef1fSBruce Evans 		db_iprintf("map entry %p: start=%p, end=%p\n",
3034fc62ef1fSBruce Evans 		    (void *)entry, (void *)entry->start, (void *)entry->end);
303595e5e988SJohn Dyson 		nlines++;
3036e5f251d2SAlan Cox 		{
3037df8bae1dSRodney W. Grimes 			static char *inheritance_name[4] =
3038df8bae1dSRodney W. Grimes 			{"share", "copy", "none", "donate_copy"};
30390d94caffSDavid Greenman 
304095e5e988SJohn Dyson 			db_iprintf(" prot=%x/%x/%s",
3041df8bae1dSRodney W. Grimes 			    entry->protection,
3042df8bae1dSRodney W. Grimes 			    entry->max_protection,
30438aef1712SMatthew Dillon 			    inheritance_name[(int)(unsigned char)entry->inheritance]);
3044df8bae1dSRodney W. Grimes 			if (entry->wired_count != 0)
304595e5e988SJohn Dyson 				db_printf(", wired");
3046df8bae1dSRodney W. Grimes 		}
30479fdfe602SMatthew Dillon 		if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3048101eeb7fSBruce Evans 			/* XXX no %qd in kernel.  Truncate entry->offset. */
3049101eeb7fSBruce Evans 			db_printf(", share=%p, offset=0x%lx\n",
30509fdfe602SMatthew Dillon 			    (void *)entry->object.sub_map,
3051ecbb00a2SDoug Rabson 			    (long)entry->offset);
305295e5e988SJohn Dyson 			nlines++;
3053df8bae1dSRodney W. Grimes 			if ((entry->prev == &map->header) ||
30549fdfe602SMatthew Dillon 			    (entry->prev->object.sub_map !=
30559fdfe602SMatthew Dillon 				entry->object.sub_map)) {
3056c7c34a24SBruce Evans 				db_indent += 2;
3057101eeb7fSBruce Evans 				vm_map_print((db_expr_t)(intptr_t)
30589fdfe602SMatthew Dillon 					     entry->object.sub_map,
3059914181e7SBruce Evans 					     full, 0, (char *)0);
3060c7c34a24SBruce Evans 				db_indent -= 2;
3061df8bae1dSRodney W. Grimes 			}
30620d94caffSDavid Greenman 		} else {
3063101eeb7fSBruce Evans 			/* XXX no %qd in kernel.  Truncate entry->offset. */
3064101eeb7fSBruce Evans 			db_printf(", object=%p, offset=0x%lx",
3065101eeb7fSBruce Evans 			    (void *)entry->object.vm_object,
3066ecbb00a2SDoug Rabson 			    (long)entry->offset);
3067afa07f7eSJohn Dyson 			if (entry->eflags & MAP_ENTRY_COW)
3068c7c34a24SBruce Evans 				db_printf(", copy (%s)",
3069afa07f7eSJohn Dyson 				    (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
3070c7c34a24SBruce Evans 			db_printf("\n");
307195e5e988SJohn Dyson 			nlines++;
3072df8bae1dSRodney W. Grimes 
3073df8bae1dSRodney W. Grimes 			if ((entry->prev == &map->header) ||
3074df8bae1dSRodney W. Grimes 			    (entry->prev->object.vm_object !=
3075df8bae1dSRodney W. Grimes 				entry->object.vm_object)) {
3076c7c34a24SBruce Evans 				db_indent += 2;
3077101eeb7fSBruce Evans 				vm_object_print((db_expr_t)(intptr_t)
3078101eeb7fSBruce Evans 						entry->object.vm_object,
3079914181e7SBruce Evans 						full, 0, (char *)0);
308095e5e988SJohn Dyson 				nlines += 4;
3081c7c34a24SBruce Evans 				db_indent -= 2;
3082df8bae1dSRodney W. Grimes 			}
3083df8bae1dSRodney W. Grimes 		}
3084df8bae1dSRodney W. Grimes 	}
3085c7c34a24SBruce Evans 	db_indent -= 2;
308695e5e988SJohn Dyson 	if (db_indent == 0)
308795e5e988SJohn Dyson 		nlines = 0;
3088df8bae1dSRodney W. Grimes }
308995e5e988SJohn Dyson 
309095e5e988SJohn Dyson 
309195e5e988SJohn Dyson DB_SHOW_COMMAND(procvm, procvm)
309295e5e988SJohn Dyson {
309395e5e988SJohn Dyson 	struct proc *p;
309495e5e988SJohn Dyson 
309595e5e988SJohn Dyson 	if (have_addr) {
309695e5e988SJohn Dyson 		p = (struct proc *) addr;
309795e5e988SJohn Dyson 	} else {
309895e5e988SJohn Dyson 		p = curproc;
309995e5e988SJohn Dyson 	}
310095e5e988SJohn Dyson 
3101ac1e407bSBruce Evans 	db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
3102ac1e407bSBruce Evans 	    (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
3103b1028ad1SLuoqi Chen 	    (void *)vmspace_pmap(p->p_vmspace));
310495e5e988SJohn Dyson 
3105101eeb7fSBruce Evans 	vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);
310695e5e988SJohn Dyson }
310795e5e988SJohn Dyson 
3108c7c34a24SBruce Evans #endif /* DDB */
3109