xref: /freebsd/sys/vm/vm_map.c (revision 069e9bc1b4e6189140c16ee2d7277dc0301b3f71)
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
17df8bae1dSRodney W. Grimes  *    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  *
64069e9bc1SDoug Rabson  * $Id: vm_map.c,v 1.133 1998/08/06 08:33:19 dfr Exp $
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>
73df8bae1dSRodney W. Grimes #include <sys/malloc.h>
74b5e8ce9fSBruce Evans #include <sys/proc.h>
75efeaf95aSDavid Greenman #include <sys/vmmeter.h>
76867a482dSJohn Dyson #include <sys/mman.h>
771efb74fbSJohn Dyson #include <sys/vnode.h>
78df8bae1dSRodney W. Grimes 
79df8bae1dSRodney W. Grimes #include <vm/vm.h>
80efeaf95aSDavid Greenman #include <vm/vm_param.h>
81efeaf95aSDavid Greenman #include <vm/vm_prot.h>
82efeaf95aSDavid Greenman #include <vm/vm_inherit.h>
83996c772fSJohn Dyson #include <sys/lock.h>
84efeaf95aSDavid Greenman #include <vm/pmap.h>
85efeaf95aSDavid Greenman #include <vm/vm_map.h>
86df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
87df8bae1dSRodney W. Grimes #include <vm/vm_object.h>
8847221757SJohn Dyson #include <vm/vm_pager.h>
8926f9a767SRodney W. Grimes #include <vm/vm_kern.h>
90efeaf95aSDavid Greenman #include <vm/vm_extern.h>
91f35329acSJohn Dyson #include <vm/default_pager.h>
9247221757SJohn Dyson #include <vm/swap_pager.h>
933075778bSJohn Dyson #include <vm/vm_zone.h>
94df8bae1dSRodney W. Grimes 
95a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_VMMAP, "VM map", "VM map structures");
9655166637SPoul-Henning Kamp 
97df8bae1dSRodney W. Grimes /*
98df8bae1dSRodney W. Grimes  *	Virtual memory maps provide for the mapping, protection,
99df8bae1dSRodney W. Grimes  *	and sharing of virtual memory objects.  In addition,
100df8bae1dSRodney W. Grimes  *	this module provides for an efficient virtual copy of
101df8bae1dSRodney W. Grimes  *	memory from one map to another.
102df8bae1dSRodney W. Grimes  *
103df8bae1dSRodney W. Grimes  *	Synchronization is required prior to most operations.
104df8bae1dSRodney W. Grimes  *
105df8bae1dSRodney W. Grimes  *	Maps consist of an ordered doubly-linked list of simple
106df8bae1dSRodney W. Grimes  *	entries; a single hint is used to speed up lookups.
107df8bae1dSRodney W. Grimes  *
108df8bae1dSRodney W. Grimes  *	In order to properly represent the sharing of virtual
109df8bae1dSRodney W. Grimes  *	memory regions among maps, the map structure is bi-level.
110df8bae1dSRodney W. Grimes  *	Top-level ("address") maps refer to regions of sharable
111df8bae1dSRodney W. Grimes  *	virtual memory.  These regions are implemented as
112df8bae1dSRodney W. Grimes  *	("sharing") maps, which then refer to the actual virtual
113df8bae1dSRodney W. Grimes  *	memory objects.  When two address maps "share" memory,
114df8bae1dSRodney W. Grimes  *	their top-level maps both have references to the same
115df8bae1dSRodney W. Grimes  *	sharing map.  When memory is virtual-copied from one
116df8bae1dSRodney W. Grimes  *	address map to another, the references in the sharing
117df8bae1dSRodney W. Grimes  *	maps are actually copied -- no copying occurs at the
118df8bae1dSRodney W. Grimes  *	virtual memory object level.
119df8bae1dSRodney W. Grimes  *
120df8bae1dSRodney W. Grimes  *	Since portions of maps are specified by start/end addreses,
121df8bae1dSRodney W. Grimes  *	which may not align with existing map entries, all
122df8bae1dSRodney W. Grimes  *	routines merely "clip" entries to these start/end values.
123df8bae1dSRodney W. Grimes  *	[That is, an entry is split into two, bordering at a
124df8bae1dSRodney W. Grimes  *	start or end value.]  Note that these clippings may not
125df8bae1dSRodney W. Grimes  *	always be necessary (as the two resulting entries are then
126df8bae1dSRodney W. Grimes  *	not changed); however, the clipping is done for convenience.
127df8bae1dSRodney W. Grimes  *	No attempt is currently made to "glue back together" two
128df8bae1dSRodney W. Grimes  *	abutting entries.
129df8bae1dSRodney W. Grimes  *
130df8bae1dSRodney W. Grimes  *	As mentioned above, virtual copy operations are performed
131df8bae1dSRodney W. Grimes  *	by copying VM object references from one sharing map to
132df8bae1dSRodney W. Grimes  *	another, and then marking both regions as copy-on-write.
133df8bae1dSRodney W. Grimes  *	It is important to note that only one writeable reference
134df8bae1dSRodney W. Grimes  *	to a VM object region exists in any map -- this means that
135df8bae1dSRodney W. Grimes  *	shadow object creation can be delayed until a write operation
136df8bae1dSRodney W. Grimes  *	occurs.
137df8bae1dSRodney W. Grimes  */
138df8bae1dSRodney W. Grimes 
139df8bae1dSRodney W. Grimes /*
140df8bae1dSRodney W. Grimes  *	vm_map_startup:
141df8bae1dSRodney W. Grimes  *
142df8bae1dSRodney W. Grimes  *	Initialize the vm_map module.  Must be called before
143df8bae1dSRodney W. Grimes  *	any other vm_map routines.
144df8bae1dSRodney W. Grimes  *
145df8bae1dSRodney W. Grimes  *	Map and entry structures are allocated from the general
146df8bae1dSRodney W. Grimes  *	purpose memory pool with some exceptions:
147df8bae1dSRodney W. Grimes  *
148df8bae1dSRodney W. Grimes  *	- The kernel map and kmem submap are allocated statically.
149df8bae1dSRodney W. Grimes  *	- Kernel map entries are allocated out of a static pool.
150df8bae1dSRodney W. Grimes  *
151df8bae1dSRodney W. Grimes  *	These restrictions are necessary since malloc() uses the
152df8bae1dSRodney W. Grimes  *	maps and requires map entries.
153df8bae1dSRodney W. Grimes  */
154df8bae1dSRodney W. Grimes 
155bd7e5f99SJohn Dyson extern char kstack[];
156b7b2aac2SJohn Dyson extern int inmprotect;
157df8bae1dSRodney W. Grimes 
1583075778bSJohn Dyson static struct vm_zone kmapentzone_store, mapentzone_store, mapzone_store;
1592d8acc0fSJohn Dyson static vm_zone_t mapentzone, kmapentzone, mapzone, vmspace_zone;
1603075778bSJohn Dyson static struct vm_object kmapentobj, mapentobj, mapobj;
1613075778bSJohn Dyson #define MAP_ENTRY_INIT	128
162303b270bSEivind Eklund static struct vm_map_entry map_entry_init[MAX_MAPENT];
163303b270bSEivind Eklund static struct vm_map_entry kmap_entry_init[MAX_KMAPENT];
164303b270bSEivind Eklund static struct vm_map map_init[MAX_KMAP];
165b18bfc3dSJohn Dyson 
166df8bae1dSRodney W. Grimes static void _vm_map_clip_end __P((vm_map_t, vm_map_entry_t, vm_offset_t));
167df8bae1dSRodney W. Grimes static void _vm_map_clip_start __P((vm_map_t, vm_map_entry_t, vm_offset_t));
168f708ef1bSPoul-Henning Kamp static vm_map_entry_t vm_map_entry_create __P((vm_map_t));
169f708ef1bSPoul-Henning Kamp static void vm_map_entry_delete __P((vm_map_t, vm_map_entry_t));
1700362d7d7SJohn Dyson static void vm_map_entry_dispose __P((vm_map_t, vm_map_entry_t));
171f708ef1bSPoul-Henning Kamp static void vm_map_entry_unwire __P((vm_map_t, vm_map_entry_t));
172f708ef1bSPoul-Henning Kamp static void vm_map_copy_entry __P((vm_map_t, vm_map_t, vm_map_entry_t,
173f708ef1bSPoul-Henning Kamp 		vm_map_entry_t));
174dda6b171SJohn Dyson static void vm_map_split __P((vm_map_entry_t));
175df8bae1dSRodney W. Grimes 
1760d94caffSDavid Greenman void
1770d94caffSDavid Greenman vm_map_startup()
178df8bae1dSRodney W. Grimes {
1793075778bSJohn Dyson 	mapzone = &mapzone_store;
1800d65e566SJohn Dyson 	zbootinit(mapzone, "MAP", sizeof (struct vm_map),
1813075778bSJohn Dyson 		map_init, MAX_KMAP);
1823075778bSJohn Dyson 	kmapentzone = &kmapentzone_store;
1830d65e566SJohn Dyson 	zbootinit(kmapentzone, "KMAP ENTRY", sizeof (struct vm_map_entry),
1843075778bSJohn Dyson 		kmap_entry_init, MAX_KMAPENT);
1853075778bSJohn Dyson 	mapentzone = &mapentzone_store;
1860d65e566SJohn Dyson 	zbootinit(mapentzone, "MAP ENTRY", sizeof (struct vm_map_entry),
1873075778bSJohn Dyson 		map_entry_init, MAX_MAPENT);
188df8bae1dSRodney W. Grimes }
189df8bae1dSRodney W. Grimes 
190df8bae1dSRodney W. Grimes /*
191df8bae1dSRodney W. Grimes  * Allocate a vmspace structure, including a vm_map and pmap,
192df8bae1dSRodney W. Grimes  * and initialize those structures.  The refcnt is set to 1.
193df8bae1dSRodney W. Grimes  * The remaining fields must be initialized by the caller.
194df8bae1dSRodney W. Grimes  */
195df8bae1dSRodney W. Grimes struct vmspace *
1962d8acc0fSJohn Dyson vmspace_alloc(min, max)
197df8bae1dSRodney W. Grimes 	vm_offset_t min, max;
198df8bae1dSRodney W. Grimes {
199c0877f10SJohn Dyson 	struct vmspace *vm;
2000d94caffSDavid Greenman 
2012d8acc0fSJohn Dyson 	vm = zalloc(vmspace_zone);
2022d8acc0fSJohn Dyson 	bzero(&vm->vm_map, sizeof vm->vm_map);
2032d8acc0fSJohn Dyson 	vm_map_init(&vm->vm_map, min, max);
204df8bae1dSRodney W. Grimes 	pmap_pinit(&vm->vm_pmap);
205df8bae1dSRodney W. Grimes 	vm->vm_map.pmap = &vm->vm_pmap;		/* XXX */
206df8bae1dSRodney W. Grimes 	vm->vm_refcnt = 1;
2072d8acc0fSJohn Dyson 	vm->vm_shm = NULL;
208df8bae1dSRodney W. Grimes 	return (vm);
209df8bae1dSRodney W. Grimes }
210df8bae1dSRodney W. Grimes 
211df8bae1dSRodney W. Grimes void
2123075778bSJohn Dyson vm_init2(void) {
2130d65e566SJohn Dyson 	zinitna(kmapentzone, &kmapentobj,
2140a80f406SJohn Dyson 		NULL, 0, cnt.v_page_count / 4, ZONE_INTERRUPT, 1);
2150d65e566SJohn Dyson 	zinitna(mapentzone, &mapentobj,
2160a80f406SJohn Dyson 		NULL, 0, 0, 0, 1);
2170d65e566SJohn Dyson 	zinitna(mapzone, &mapobj,
2180a80f406SJohn Dyson 		NULL, 0, 0, 0, 1);
2192d8acc0fSJohn Dyson 	vmspace_zone = zinit("VMSPACE", sizeof (struct vmspace), 0, 0, 3);
220ba9be04cSJohn Dyson 	pmap_init2();
22199448ed1SJohn Dyson 	vm_object_init2();
2223075778bSJohn Dyson }
2233075778bSJohn Dyson 
2243075778bSJohn Dyson void
225df8bae1dSRodney W. Grimes vmspace_free(vm)
226c0877f10SJohn Dyson 	struct vmspace *vm;
227df8bae1dSRodney W. Grimes {
228df8bae1dSRodney W. Grimes 
229a1f6d91cSDavid Greenman 	if (vm->vm_refcnt == 0)
230a1f6d91cSDavid Greenman 		panic("vmspace_free: attempt to free already freed vmspace");
231a1f6d91cSDavid Greenman 
232df8bae1dSRodney W. Grimes 	if (--vm->vm_refcnt == 0) {
233bd7e5f99SJohn Dyson 
23430dcfc09SJohn Dyson 		/*
235df8bae1dSRodney W. Grimes 		 * Lock the map, to wait out all other references to it.
2360d94caffSDavid Greenman 		 * Delete all of the mappings and pages they hold, then call
2370d94caffSDavid Greenman 		 * the pmap module to reclaim anything left.
238df8bae1dSRodney W. Grimes 		 */
239df8bae1dSRodney W. Grimes 		vm_map_lock(&vm->vm_map);
240df8bae1dSRodney W. Grimes 		(void) vm_map_delete(&vm->vm_map, vm->vm_map.min_offset,
241df8bae1dSRodney W. Grimes 		    vm->vm_map.max_offset);
242a1f6d91cSDavid Greenman 		vm_map_unlock(&vm->vm_map);
243b18bfc3dSJohn Dyson 
244df8bae1dSRodney W. Grimes 		pmap_release(&vm->vm_pmap);
2452d8acc0fSJohn Dyson 		zfree(vmspace_zone, vm);
246df8bae1dSRodney W. Grimes 	}
247df8bae1dSRodney W. Grimes }
248df8bae1dSRodney W. Grimes 
249df8bae1dSRodney W. Grimes /*
250df8bae1dSRodney W. Grimes  *	vm_map_create:
251df8bae1dSRodney W. Grimes  *
252df8bae1dSRodney W. Grimes  *	Creates and returns a new empty VM map with
253df8bae1dSRodney W. Grimes  *	the given physical map structure, and having
254df8bae1dSRodney W. Grimes  *	the given lower and upper address bounds.
255df8bae1dSRodney W. Grimes  */
2560d94caffSDavid Greenman vm_map_t
2572d8acc0fSJohn Dyson vm_map_create(pmap, min, max)
258df8bae1dSRodney W. Grimes 	pmap_t pmap;
259df8bae1dSRodney W. Grimes 	vm_offset_t min, max;
260df8bae1dSRodney W. Grimes {
261c0877f10SJohn Dyson 	vm_map_t result;
262df8bae1dSRodney W. Grimes 
2633075778bSJohn Dyson 	result = zalloc(mapzone);
2642d8acc0fSJohn Dyson 	vm_map_init(result, min, max);
265df8bae1dSRodney W. Grimes 	result->pmap = pmap;
266df8bae1dSRodney W. Grimes 	return (result);
267df8bae1dSRodney W. Grimes }
268df8bae1dSRodney W. Grimes 
269df8bae1dSRodney W. Grimes /*
270df8bae1dSRodney W. Grimes  * Initialize an existing vm_map structure
271df8bae1dSRodney W. Grimes  * such as that in the vmspace structure.
272df8bae1dSRodney W. Grimes  * The pmap is set elsewhere.
273df8bae1dSRodney W. Grimes  */
274df8bae1dSRodney W. Grimes void
2752d8acc0fSJohn Dyson vm_map_init(map, min, max)
276c0877f10SJohn Dyson 	struct vm_map *map;
277df8bae1dSRodney W. Grimes 	vm_offset_t min, max;
278df8bae1dSRodney W. Grimes {
279df8bae1dSRodney W. Grimes 	map->header.next = map->header.prev = &map->header;
280df8bae1dSRodney W. Grimes 	map->nentries = 0;
281df8bae1dSRodney W. Grimes 	map->size = 0;
282df8bae1dSRodney W. Grimes 	map->is_main_map = TRUE;
2833075778bSJohn Dyson 	map->system_map = 0;
284df8bae1dSRodney W. Grimes 	map->min_offset = min;
285df8bae1dSRodney W. Grimes 	map->max_offset = max;
286df8bae1dSRodney W. Grimes 	map->first_free = &map->header;
287df8bae1dSRodney W. Grimes 	map->hint = &map->header;
288df8bae1dSRodney W. Grimes 	map->timestamp = 0;
2898f9110f6SJohn Dyson 	lockinit(&map->lock, PVM, "thrd_sleep", 0, LK_NOPAUSE);
290df8bae1dSRodney W. Grimes }
291df8bae1dSRodney W. Grimes 
292df8bae1dSRodney W. Grimes /*
293b18bfc3dSJohn Dyson  *	vm_map_entry_dispose:	[ internal use only ]
294b18bfc3dSJohn Dyson  *
295b18bfc3dSJohn Dyson  *	Inverse of vm_map_entry_create.
296b18bfc3dSJohn Dyson  */
29762487bb4SJohn Dyson static void
298b18bfc3dSJohn Dyson vm_map_entry_dispose(map, entry)
299b18bfc3dSJohn Dyson 	vm_map_t map;
300b18bfc3dSJohn Dyson 	vm_map_entry_t entry;
301b18bfc3dSJohn Dyson {
302b79933ebSJohn Dyson 	zfree((map->system_map || !mapentzone) ? kmapentzone : mapentzone, entry);
303b18bfc3dSJohn Dyson }
304b18bfc3dSJohn Dyson 
305b18bfc3dSJohn Dyson /*
306df8bae1dSRodney W. Grimes  *	vm_map_entry_create:	[ internal use only ]
307df8bae1dSRodney W. Grimes  *
308df8bae1dSRodney W. Grimes  *	Allocates a VM map entry for insertion.
309df8bae1dSRodney W. Grimes  *	No entry fields are filled in.  This routine is
310df8bae1dSRodney W. Grimes  */
311f708ef1bSPoul-Henning Kamp static vm_map_entry_t
31226f9a767SRodney W. Grimes vm_map_entry_create(map)
313df8bae1dSRodney W. Grimes 	vm_map_t map;
314df8bae1dSRodney W. Grimes {
315b79933ebSJohn Dyson 	return zalloc((map->system_map || !mapentzone) ? kmapentzone : mapentzone);
316df8bae1dSRodney W. Grimes }
317df8bae1dSRodney W. Grimes 
318df8bae1dSRodney W. Grimes /*
319df8bae1dSRodney W. Grimes  *	vm_map_entry_{un,}link:
320df8bae1dSRodney W. Grimes  *
321df8bae1dSRodney W. Grimes  *	Insert/remove entries from maps.
322df8bae1dSRodney W. Grimes  */
323df8bae1dSRodney W. Grimes #define	vm_map_entry_link(map, after_where, entry) \
324df8bae1dSRodney W. Grimes 		{ \
325df8bae1dSRodney W. Grimes 		(map)->nentries++; \
32647221757SJohn Dyson 		(map)->timestamp++; \
327df8bae1dSRodney W. Grimes 		(entry)->prev = (after_where); \
328df8bae1dSRodney W. Grimes 		(entry)->next = (after_where)->next; \
329df8bae1dSRodney W. Grimes 		(entry)->prev->next = (entry); \
330df8bae1dSRodney W. Grimes 		(entry)->next->prev = (entry); \
331df8bae1dSRodney W. Grimes 		}
332df8bae1dSRodney W. Grimes #define	vm_map_entry_unlink(map, entry) \
333df8bae1dSRodney W. Grimes 		{ \
334df8bae1dSRodney W. Grimes 		(map)->nentries--; \
33547221757SJohn Dyson 		(map)->timestamp++; \
336df8bae1dSRodney W. Grimes 		(entry)->next->prev = (entry)->prev; \
337df8bae1dSRodney W. Grimes 		(entry)->prev->next = (entry)->next; \
338df8bae1dSRodney W. Grimes 		}
339df8bae1dSRodney W. Grimes 
340df8bae1dSRodney W. Grimes /*
341df8bae1dSRodney W. Grimes  *	SAVE_HINT:
342df8bae1dSRodney W. Grimes  *
343df8bae1dSRodney W. Grimes  *	Saves the specified entry as the hint for
34424a1cce3SDavid Greenman  *	future lookups.
345df8bae1dSRodney W. Grimes  */
346df8bae1dSRodney W. Grimes #define	SAVE_HINT(map,value) \
34724a1cce3SDavid Greenman 		(map)->hint = (value);
348df8bae1dSRodney W. Grimes 
349df8bae1dSRodney W. Grimes /*
350df8bae1dSRodney W. Grimes  *	vm_map_lookup_entry:	[ internal use only ]
351df8bae1dSRodney W. Grimes  *
352df8bae1dSRodney W. Grimes  *	Finds the map entry containing (or
353df8bae1dSRodney W. Grimes  *	immediately preceding) the specified address
354df8bae1dSRodney W. Grimes  *	in the given map; the entry is returned
355df8bae1dSRodney W. Grimes  *	in the "entry" parameter.  The boolean
356df8bae1dSRodney W. Grimes  *	result indicates whether the address is
357df8bae1dSRodney W. Grimes  *	actually contained in the map.
358df8bae1dSRodney W. Grimes  */
3590d94caffSDavid Greenman boolean_t
3600d94caffSDavid Greenman vm_map_lookup_entry(map, address, entry)
361c0877f10SJohn Dyson 	vm_map_t map;
362c0877f10SJohn Dyson 	vm_offset_t address;
363df8bae1dSRodney W. Grimes 	vm_map_entry_t *entry;	/* OUT */
364df8bae1dSRodney W. Grimes {
365c0877f10SJohn Dyson 	vm_map_entry_t cur;
366c0877f10SJohn Dyson 	vm_map_entry_t last;
367df8bae1dSRodney W. Grimes 
368df8bae1dSRodney W. Grimes 	/*
3690d94caffSDavid Greenman 	 * Start looking either from the head of the list, or from the hint.
370df8bae1dSRodney W. Grimes 	 */
371df8bae1dSRodney W. Grimes 
372df8bae1dSRodney W. Grimes 	cur = map->hint;
373df8bae1dSRodney W. Grimes 
374df8bae1dSRodney W. Grimes 	if (cur == &map->header)
375df8bae1dSRodney W. Grimes 		cur = cur->next;
376df8bae1dSRodney W. Grimes 
377df8bae1dSRodney W. Grimes 	if (address >= cur->start) {
378df8bae1dSRodney W. Grimes 		/*
379df8bae1dSRodney W. Grimes 		 * Go from hint to end of list.
380df8bae1dSRodney W. Grimes 		 *
3810d94caffSDavid Greenman 		 * But first, make a quick check to see if we are already looking
3820d94caffSDavid Greenman 		 * at the entry we want (which is usually the case). Note also
3830d94caffSDavid Greenman 		 * that we don't need to save the hint here... it is the same
3840d94caffSDavid Greenman 		 * hint (unless we are at the header, in which case the hint
3850d94caffSDavid Greenman 		 * didn't buy us anything anyway).
386df8bae1dSRodney W. Grimes 		 */
387df8bae1dSRodney W. Grimes 		last = &map->header;
388df8bae1dSRodney W. Grimes 		if ((cur != last) && (cur->end > address)) {
389df8bae1dSRodney W. Grimes 			*entry = cur;
390df8bae1dSRodney W. Grimes 			return (TRUE);
391df8bae1dSRodney W. Grimes 		}
3920d94caffSDavid Greenman 	} else {
393df8bae1dSRodney W. Grimes 		/*
394df8bae1dSRodney W. Grimes 		 * Go from start to hint, *inclusively*
395df8bae1dSRodney W. Grimes 		 */
396df8bae1dSRodney W. Grimes 		last = cur->next;
397df8bae1dSRodney W. Grimes 		cur = map->header.next;
398df8bae1dSRodney W. Grimes 	}
399df8bae1dSRodney W. Grimes 
400df8bae1dSRodney W. Grimes 	/*
401df8bae1dSRodney W. Grimes 	 * Search linearly
402df8bae1dSRodney W. Grimes 	 */
403df8bae1dSRodney W. Grimes 
404df8bae1dSRodney W. Grimes 	while (cur != last) {
405df8bae1dSRodney W. Grimes 		if (cur->end > address) {
406df8bae1dSRodney W. Grimes 			if (address >= cur->start) {
407df8bae1dSRodney W. Grimes 				/*
4080d94caffSDavid Greenman 				 * Save this lookup for future hints, and
4090d94caffSDavid Greenman 				 * return
410df8bae1dSRodney W. Grimes 				 */
411df8bae1dSRodney W. Grimes 
412df8bae1dSRodney W. Grimes 				*entry = cur;
413df8bae1dSRodney W. Grimes 				SAVE_HINT(map, cur);
414df8bae1dSRodney W. Grimes 				return (TRUE);
415df8bae1dSRodney W. Grimes 			}
416df8bae1dSRodney W. Grimes 			break;
417df8bae1dSRodney W. Grimes 		}
418df8bae1dSRodney W. Grimes 		cur = cur->next;
419df8bae1dSRodney W. Grimes 	}
420df8bae1dSRodney W. Grimes 	*entry = cur->prev;
421df8bae1dSRodney W. Grimes 	SAVE_HINT(map, *entry);
422df8bae1dSRodney W. Grimes 	return (FALSE);
423df8bae1dSRodney W. Grimes }
424df8bae1dSRodney W. Grimes 
425df8bae1dSRodney W. Grimes /*
42630dcfc09SJohn Dyson  *	vm_map_insert:
42730dcfc09SJohn Dyson  *
42830dcfc09SJohn Dyson  *	Inserts the given whole VM object into the target
42930dcfc09SJohn Dyson  *	map at the specified address range.  The object's
43030dcfc09SJohn Dyson  *	size should match that of the address range.
43130dcfc09SJohn Dyson  *
43230dcfc09SJohn Dyson  *	Requires that the map be locked, and leaves it so.
43330dcfc09SJohn Dyson  */
43430dcfc09SJohn Dyson int
435b9dcd593SBruce Evans vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
436b9dcd593SBruce Evans 	      vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max,
437b9dcd593SBruce Evans 	      int cow)
43830dcfc09SJohn Dyson {
439c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
440c0877f10SJohn Dyson 	vm_map_entry_t prev_entry;
44130dcfc09SJohn Dyson 	vm_map_entry_t temp_entry;
442a5b6fd29SJohn Dyson 	vm_object_t prev_object;
443afa07f7eSJohn Dyson 	u_char protoeflags;
44430dcfc09SJohn Dyson 
445cdc2c291SJohn Dyson 	if ((object != NULL) && (cow & MAP_NOFAULT)) {
446cdc2c291SJohn Dyson 		panic("vm_map_insert: paradoxical MAP_NOFAULT request");
447cdc2c291SJohn Dyson 	}
448cdc2c291SJohn Dyson 
44930dcfc09SJohn Dyson 	/*
45030dcfc09SJohn Dyson 	 * Check that the start and end points are not bogus.
45130dcfc09SJohn Dyson 	 */
45230dcfc09SJohn Dyson 
45330dcfc09SJohn Dyson 	if ((start < map->min_offset) || (end > map->max_offset) ||
45430dcfc09SJohn Dyson 	    (start >= end))
45530dcfc09SJohn Dyson 		return (KERN_INVALID_ADDRESS);
45630dcfc09SJohn Dyson 
45730dcfc09SJohn Dyson 	/*
45830dcfc09SJohn Dyson 	 * Find the entry prior to the proposed starting address; if it's part
45930dcfc09SJohn Dyson 	 * of an existing entry, this range is bogus.
46030dcfc09SJohn Dyson 	 */
46130dcfc09SJohn Dyson 
46230dcfc09SJohn Dyson 	if (vm_map_lookup_entry(map, start, &temp_entry))
46330dcfc09SJohn Dyson 		return (KERN_NO_SPACE);
46430dcfc09SJohn Dyson 
46530dcfc09SJohn Dyson 	prev_entry = temp_entry;
46630dcfc09SJohn Dyson 
46730dcfc09SJohn Dyson 	/*
46830dcfc09SJohn Dyson 	 * Assert that the next entry doesn't overlap the end point.
46930dcfc09SJohn Dyson 	 */
47030dcfc09SJohn Dyson 
47130dcfc09SJohn Dyson 	if ((prev_entry->next != &map->header) &&
47230dcfc09SJohn Dyson 	    (prev_entry->next->start < end))
47330dcfc09SJohn Dyson 		return (KERN_NO_SPACE);
47430dcfc09SJohn Dyson 
475afa07f7eSJohn Dyson 	protoeflags = 0;
476afa07f7eSJohn Dyson 	if (cow & MAP_COPY_NEEDED)
477afa07f7eSJohn Dyson 		protoeflags |= MAP_ENTRY_NEEDS_COPY;
478afa07f7eSJohn Dyson 
479afa07f7eSJohn Dyson 	if (cow & MAP_COPY_ON_WRITE)
480afa07f7eSJohn Dyson 		protoeflags |= MAP_ENTRY_COW;
481afa07f7eSJohn Dyson 
482afa07f7eSJohn Dyson 	if (cow & MAP_NOFAULT)
483afa07f7eSJohn Dyson 		protoeflags |= MAP_ENTRY_NOFAULT;
484afa07f7eSJohn Dyson 
48530dcfc09SJohn Dyson 	/*
48630dcfc09SJohn Dyson 	 * See if we can avoid creating a new entry by extending one of our
4878cc7e047SJohn Dyson 	 * neighbors.  Or at least extend the object.
48830dcfc09SJohn Dyson 	 */
4898cc7e047SJohn Dyson 
4908cc7e047SJohn Dyson 	if ((object == NULL) &&
4918cc7e047SJohn Dyson 	    (prev_entry != &map->header) &&
492afa07f7eSJohn Dyson 	    (( prev_entry->eflags & (MAP_ENTRY_IS_A_MAP | MAP_ENTRY_IS_SUB_MAP)) == 0) &&
49395e5e988SJohn Dyson 		((prev_entry->object.vm_object == NULL) ||
49495e5e988SJohn Dyson 			(prev_entry->object.vm_object->type == OBJT_DEFAULT)) &&
4958cc7e047SJohn Dyson 	    (prev_entry->end == start) &&
4968cc7e047SJohn Dyson 	    (prev_entry->wired_count == 0)) {
4978cc7e047SJohn Dyson 
498cdc2c291SJohn Dyson 
499afa07f7eSJohn Dyson 		if ((protoeflags == prev_entry->eflags) &&
500afa07f7eSJohn Dyson 		    ((cow & MAP_NOFAULT) ||
5018cc7e047SJohn Dyson 		     vm_object_coalesce(prev_entry->object.vm_object,
50230dcfc09SJohn Dyson 					OFF_TO_IDX(prev_entry->offset),
5038cc7e047SJohn Dyson 					(vm_size_t) (prev_entry->end - prev_entry->start),
504cdc2c291SJohn Dyson 					(vm_size_t) (end - prev_entry->end)))) {
505a5b6fd29SJohn Dyson 
50630dcfc09SJohn Dyson 			/*
5078cc7e047SJohn Dyson 			 * Coalesced the two objects.  Can we extend the
5088cc7e047SJohn Dyson 			 * previous map entry to include the new range?
50930dcfc09SJohn Dyson 			 */
5108cc7e047SJohn Dyson 			if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
5118cc7e047SJohn Dyson 			    (prev_entry->protection == prot) &&
5128cc7e047SJohn Dyson 			    (prev_entry->max_protection == max)) {
5138cc7e047SJohn Dyson 
51430dcfc09SJohn Dyson 				map->size += (end - prev_entry->end);
51530dcfc09SJohn Dyson 				prev_entry->end = end;
516afa07f7eSJohn Dyson 				if ((cow & MAP_NOFAULT) == 0) {
517a5b6fd29SJohn Dyson 					prev_object = prev_entry->object.vm_object;
518b5b40fa6SJohn Dyson 					default_pager_convert_to_swapq(prev_object);
519cdc2c291SJohn Dyson 				}
52030dcfc09SJohn Dyson 				return (KERN_SUCCESS);
52130dcfc09SJohn Dyson 			}
5228cc7e047SJohn Dyson 			else {
5238cc7e047SJohn Dyson 				object = prev_entry->object.vm_object;
5248cc7e047SJohn Dyson 				offset = prev_entry->offset + (prev_entry->end -
5258cc7e047SJohn Dyson 							       prev_entry->start);
5268cc7e047SJohn Dyson 
5278cc7e047SJohn Dyson 				vm_object_reference(object);
528b18bfc3dSJohn Dyson 			}
52967bf6868SJohn Dyson 		}
5308cc7e047SJohn Dyson 	}
5318cc7e047SJohn Dyson 
53230dcfc09SJohn Dyson 	/*
53330dcfc09SJohn Dyson 	 * Create a new entry
53430dcfc09SJohn Dyson 	 */
53530dcfc09SJohn Dyson 
53630dcfc09SJohn Dyson 	new_entry = vm_map_entry_create(map);
53730dcfc09SJohn Dyson 	new_entry->start = start;
53830dcfc09SJohn Dyson 	new_entry->end = end;
53930dcfc09SJohn Dyson 
540afa07f7eSJohn Dyson 	new_entry->eflags = protoeflags;
54130dcfc09SJohn Dyson 	new_entry->object.vm_object = object;
54230dcfc09SJohn Dyson 	new_entry->offset = offset;
543cbd8ec09SJohn Dyson 	if (object) {
544cbd8ec09SJohn Dyson 		if ((object->ref_count > 1) || (object->shadow_count != 0)) {
545069e9bc1SDoug Rabson 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
546cbd8ec09SJohn Dyson 		} else {
547069e9bc1SDoug Rabson 			vm_object_set_flag(object, OBJ_ONEMAPPING);
548cbd8ec09SJohn Dyson 		}
549cbd8ec09SJohn Dyson 	}
55030dcfc09SJohn Dyson 
55130dcfc09SJohn Dyson 	if (map->is_main_map) {
55230dcfc09SJohn Dyson 		new_entry->inheritance = VM_INHERIT_DEFAULT;
55330dcfc09SJohn Dyson 		new_entry->protection = prot;
55430dcfc09SJohn Dyson 		new_entry->max_protection = max;
55530dcfc09SJohn Dyson 		new_entry->wired_count = 0;
55630dcfc09SJohn Dyson 	}
55730dcfc09SJohn Dyson 	/*
55830dcfc09SJohn Dyson 	 * Insert the new entry into the list
55930dcfc09SJohn Dyson 	 */
56030dcfc09SJohn Dyson 
56130dcfc09SJohn Dyson 	vm_map_entry_link(map, prev_entry, new_entry);
56230dcfc09SJohn Dyson 	map->size += new_entry->end - new_entry->start;
56330dcfc09SJohn Dyson 
56430dcfc09SJohn Dyson 	/*
56530dcfc09SJohn Dyson 	 * Update the free space hint
56630dcfc09SJohn Dyson 	 */
56767bf6868SJohn Dyson 	if ((map->first_free == prev_entry) &&
56867bf6868SJohn Dyson 		(prev_entry->end >= new_entry->start))
56930dcfc09SJohn Dyson 		map->first_free = new_entry;
57030dcfc09SJohn Dyson 
571b5b40fa6SJohn Dyson 	default_pager_convert_to_swapq(object);
57230dcfc09SJohn Dyson 	return (KERN_SUCCESS);
57330dcfc09SJohn Dyson }
57430dcfc09SJohn Dyson 
57530dcfc09SJohn Dyson /*
576df8bae1dSRodney W. Grimes  * Find sufficient space for `length' bytes in the given map, starting at
577df8bae1dSRodney W. Grimes  * `start'.  The map must be locked.  Returns 0 on success, 1 on no space.
578df8bae1dSRodney W. Grimes  */
579df8bae1dSRodney W. Grimes int
580df8bae1dSRodney W. Grimes vm_map_findspace(map, start, length, addr)
581c0877f10SJohn Dyson 	vm_map_t map;
582c0877f10SJohn Dyson 	vm_offset_t start;
583df8bae1dSRodney W. Grimes 	vm_size_t length;
584df8bae1dSRodney W. Grimes 	vm_offset_t *addr;
585df8bae1dSRodney W. Grimes {
586c0877f10SJohn Dyson 	vm_map_entry_t entry, next;
587c0877f10SJohn Dyson 	vm_offset_t end;
588df8bae1dSRodney W. Grimes 
589df8bae1dSRodney W. Grimes 	if (start < map->min_offset)
590df8bae1dSRodney W. Grimes 		start = map->min_offset;
591df8bae1dSRodney W. Grimes 	if (start > map->max_offset)
592df8bae1dSRodney W. Grimes 		return (1);
593df8bae1dSRodney W. Grimes 
594df8bae1dSRodney W. Grimes 	/*
5950d94caffSDavid Greenman 	 * Look for the first possible address; if there's already something
5960d94caffSDavid Greenman 	 * at this address, we have to start after it.
597df8bae1dSRodney W. Grimes 	 */
598df8bae1dSRodney W. Grimes 	if (start == map->min_offset) {
59967bf6868SJohn Dyson 		if ((entry = map->first_free) != &map->header)
600df8bae1dSRodney W. Grimes 			start = entry->end;
601df8bae1dSRodney W. Grimes 	} else {
602df8bae1dSRodney W. Grimes 		vm_map_entry_t tmp;
6030d94caffSDavid Greenman 
604df8bae1dSRodney W. Grimes 		if (vm_map_lookup_entry(map, start, &tmp))
605df8bae1dSRodney W. Grimes 			start = tmp->end;
606df8bae1dSRodney W. Grimes 		entry = tmp;
607df8bae1dSRodney W. Grimes 	}
608df8bae1dSRodney W. Grimes 
609df8bae1dSRodney W. Grimes 	/*
6100d94caffSDavid Greenman 	 * Look through the rest of the map, trying to fit a new region in the
6110d94caffSDavid Greenman 	 * gap between existing regions, or after the very last region.
612df8bae1dSRodney W. Grimes 	 */
613df8bae1dSRodney W. Grimes 	for (;; start = (entry = next)->end) {
614df8bae1dSRodney W. Grimes 		/*
615df8bae1dSRodney W. Grimes 		 * Find the end of the proposed new region.  Be sure we didn't
616df8bae1dSRodney W. Grimes 		 * go beyond the end of the map, or wrap around the address;
617df8bae1dSRodney W. Grimes 		 * if so, we lose.  Otherwise, if this is the last entry, or
618df8bae1dSRodney W. Grimes 		 * if the proposed new region fits before the next entry, we
619df8bae1dSRodney W. Grimes 		 * win.
620df8bae1dSRodney W. Grimes 		 */
621df8bae1dSRodney W. Grimes 		end = start + length;
622df8bae1dSRodney W. Grimes 		if (end > map->max_offset || end < start)
623df8bae1dSRodney W. Grimes 			return (1);
624df8bae1dSRodney W. Grimes 		next = entry->next;
625df8bae1dSRodney W. Grimes 		if (next == &map->header || next->start >= end)
626df8bae1dSRodney W. Grimes 			break;
627df8bae1dSRodney W. Grimes 	}
628df8bae1dSRodney W. Grimes 	SAVE_HINT(map, entry);
629df8bae1dSRodney W. Grimes 	*addr = start;
63099448ed1SJohn Dyson 	if (map == kernel_map) {
63199448ed1SJohn Dyson 		vm_offset_t ksize;
63299448ed1SJohn Dyson 		if ((ksize = round_page(start + length)) > kernel_vm_end) {
63399448ed1SJohn Dyson 			pmap_growkernel(ksize);
63499448ed1SJohn Dyson 		}
63599448ed1SJohn Dyson 	}
636df8bae1dSRodney W. Grimes 	return (0);
637df8bae1dSRodney W. Grimes }
638df8bae1dSRodney W. Grimes 
639df8bae1dSRodney W. Grimes /*
640df8bae1dSRodney W. Grimes  *	vm_map_find finds an unallocated region in the target address
641df8bae1dSRodney W. Grimes  *	map with the given length.  The search is defined to be
642df8bae1dSRodney W. Grimes  *	first-fit from the specified address; the region found is
643df8bae1dSRodney W. Grimes  *	returned in the same parameter.
644df8bae1dSRodney W. Grimes  *
645df8bae1dSRodney W. Grimes  */
646df8bae1dSRodney W. Grimes int
647b9dcd593SBruce Evans vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
648b9dcd593SBruce Evans 	    vm_offset_t *addr,	/* IN/OUT */
649b9dcd593SBruce Evans 	    vm_size_t length, boolean_t find_space, vm_prot_t prot,
650b9dcd593SBruce Evans 	    vm_prot_t max, int cow)
651df8bae1dSRodney W. Grimes {
652c0877f10SJohn Dyson 	vm_offset_t start;
6538d6e8edeSDavid Greenman 	int result, s = 0;
654df8bae1dSRodney W. Grimes 
655df8bae1dSRodney W. Grimes 	start = *addr;
6568d6e8edeSDavid Greenman 
6579579ee64SDavid Greenman 	if (map == kmem_map || map == mb_map)
658b18bfc3dSJohn Dyson 		s = splvm();
6598d6e8edeSDavid Greenman 
660bea41bcfSDavid Greenman 	vm_map_lock(map);
661df8bae1dSRodney W. Grimes 	if (find_space) {
662df8bae1dSRodney W. Grimes 		if (vm_map_findspace(map, start, length, addr)) {
663df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
6649579ee64SDavid Greenman 			if (map == kmem_map || map == mb_map)
6658d6e8edeSDavid Greenman 				splx(s);
666df8bae1dSRodney W. Grimes 			return (KERN_NO_SPACE);
667df8bae1dSRodney W. Grimes 		}
668df8bae1dSRodney W. Grimes 		start = *addr;
669df8bae1dSRodney W. Grimes 	}
670bd7e5f99SJohn Dyson 	result = vm_map_insert(map, object, offset,
671bd7e5f99SJohn Dyson 		start, start + length, prot, max, cow);
672df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
6738d6e8edeSDavid Greenman 
6749579ee64SDavid Greenman 	if (map == kmem_map || map == mb_map)
6758d6e8edeSDavid Greenman 		splx(s);
6768d6e8edeSDavid Greenman 
677df8bae1dSRodney W. Grimes 	return (result);
678df8bae1dSRodney W. Grimes }
679df8bae1dSRodney W. Grimes 
680df8bae1dSRodney W. Grimes /*
681b7b2aac2SJohn Dyson  *	vm_map_simplify_entry:
68267bf6868SJohn Dyson  *
683b7b2aac2SJohn Dyson  *	Simplify the given map entry by merging with either neighbor.
684df8bae1dSRodney W. Grimes  */
685b7b2aac2SJohn Dyson void
6860d94caffSDavid Greenman vm_map_simplify_entry(map, entry)
687df8bae1dSRodney W. Grimes 	vm_map_t map;
688df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
689df8bae1dSRodney W. Grimes {
690308c24baSJohn Dyson 	vm_map_entry_t next, prev;
691b7b2aac2SJohn Dyson 	vm_size_t prevsize, esize;
692df8bae1dSRodney W. Grimes 
693afa07f7eSJohn Dyson 	if (entry->eflags & (MAP_ENTRY_IS_SUB_MAP|MAP_ENTRY_IS_A_MAP))
694df8bae1dSRodney W. Grimes 		return;
695308c24baSJohn Dyson 
696308c24baSJohn Dyson 	prev = entry->prev;
697308c24baSJohn Dyson 	if (prev != &map->header) {
69867bf6868SJohn Dyson 		prevsize = prev->end - prev->start;
69967bf6868SJohn Dyson 		if ( (prev->end == entry->start) &&
70067bf6868SJohn Dyson 		     (prev->object.vm_object == entry->object.vm_object) &&
70195e5e988SJohn Dyson 		     (!prev->object.vm_object ||
70295e5e988SJohn Dyson 				(prev->object.vm_object->behavior == entry->object.vm_object->behavior)) &&
70367bf6868SJohn Dyson 		     (!prev->object.vm_object ||
70467bf6868SJohn Dyson 			(prev->offset + prevsize == entry->offset)) &&
705afa07f7eSJohn Dyson 		     (prev->eflags == entry->eflags) &&
70667bf6868SJohn Dyson 		     (prev->protection == entry->protection) &&
70767bf6868SJohn Dyson 		     (prev->max_protection == entry->max_protection) &&
70867bf6868SJohn Dyson 		     (prev->inheritance == entry->inheritance) &&
709b7b2aac2SJohn Dyson 		     (prev->wired_count == entry->wired_count)) {
710308c24baSJohn Dyson 			if (map->first_free == prev)
711308c24baSJohn Dyson 				map->first_free = entry;
712b18bfc3dSJohn Dyson 			if (map->hint == prev)
713b18bfc3dSJohn Dyson 				map->hint = entry;
714308c24baSJohn Dyson 			vm_map_entry_unlink(map, prev);
715308c24baSJohn Dyson 			entry->start = prev->start;
716308c24baSJohn Dyson 			entry->offset = prev->offset;
717b18bfc3dSJohn Dyson 			if (prev->object.vm_object)
718308c24baSJohn Dyson 				vm_object_deallocate(prev->object.vm_object);
719308c24baSJohn Dyson 			vm_map_entry_dispose(map, prev);
720308c24baSJohn Dyson 		}
721308c24baSJohn Dyson 	}
722de5f6a77SJohn Dyson 
723de5f6a77SJohn Dyson 	next = entry->next;
724308c24baSJohn Dyson 	if (next != &map->header) {
72567bf6868SJohn Dyson 		esize = entry->end - entry->start;
72667bf6868SJohn Dyson 		if ((entry->end == next->start) &&
72767bf6868SJohn Dyson 		    (next->object.vm_object == entry->object.vm_object) &&
72895e5e988SJohn Dyson 		    (!next->object.vm_object ||
72995e5e988SJohn Dyson 				(next->object.vm_object->behavior == entry->object.vm_object->behavior)) &&
73067bf6868SJohn Dyson 		     (!entry->object.vm_object ||
73167bf6868SJohn Dyson 			(entry->offset + esize == next->offset)) &&
732afa07f7eSJohn Dyson 		    (next->eflags == entry->eflags) &&
73367bf6868SJohn Dyson 		    (next->protection == entry->protection) &&
73467bf6868SJohn Dyson 		    (next->max_protection == entry->max_protection) &&
73567bf6868SJohn Dyson 		    (next->inheritance == entry->inheritance) &&
736b7b2aac2SJohn Dyson 		    (next->wired_count == entry->wired_count)) {
737308c24baSJohn Dyson 			if (map->first_free == next)
738308c24baSJohn Dyson 				map->first_free = entry;
739b18bfc3dSJohn Dyson 			if (map->hint == next)
740b18bfc3dSJohn Dyson 				map->hint = entry;
741de5f6a77SJohn Dyson 			vm_map_entry_unlink(map, next);
742de5f6a77SJohn Dyson 			entry->end = next->end;
743b18bfc3dSJohn Dyson 			if (next->object.vm_object)
744de5f6a77SJohn Dyson 				vm_object_deallocate(next->object.vm_object);
745de5f6a77SJohn Dyson 			vm_map_entry_dispose(map, next);
746df8bae1dSRodney W. Grimes 	        }
747df8bae1dSRodney W. Grimes 	}
748de5f6a77SJohn Dyson }
749df8bae1dSRodney W. Grimes /*
750df8bae1dSRodney W. Grimes  *	vm_map_clip_start:	[ internal use only ]
751df8bae1dSRodney W. Grimes  *
752df8bae1dSRodney W. Grimes  *	Asserts that the given entry begins at or after
753df8bae1dSRodney W. Grimes  *	the specified address; if necessary,
754df8bae1dSRodney W. Grimes  *	it splits the entry into two.
755df8bae1dSRodney W. Grimes  */
756df8bae1dSRodney W. Grimes #define vm_map_clip_start(map, entry, startaddr) \
757df8bae1dSRodney W. Grimes { \
758df8bae1dSRodney W. Grimes 	if (startaddr > entry->start) \
759df8bae1dSRodney W. Grimes 		_vm_map_clip_start(map, entry, startaddr); \
760c0877f10SJohn Dyson 	else if (entry->object.vm_object && (entry->object.vm_object->ref_count == 1)) \
761069e9bc1SDoug Rabson 		vm_object_set_flag(entry->object.vm_object, OBJ_ONEMAPPING); \
762df8bae1dSRodney W. Grimes }
763df8bae1dSRodney W. Grimes 
764df8bae1dSRodney W. Grimes /*
765df8bae1dSRodney W. Grimes  *	This routine is called only when it is known that
766df8bae1dSRodney W. Grimes  *	the entry must be split.
767df8bae1dSRodney W. Grimes  */
7680d94caffSDavid Greenman static void
7690d94caffSDavid Greenman _vm_map_clip_start(map, entry, start)
770c0877f10SJohn Dyson 	vm_map_t map;
771c0877f10SJohn Dyson 	vm_map_entry_t entry;
772c0877f10SJohn Dyson 	vm_offset_t start;
773df8bae1dSRodney W. Grimes {
774c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
775df8bae1dSRodney W. Grimes 
776df8bae1dSRodney W. Grimes 	/*
7770d94caffSDavid Greenman 	 * Split off the front portion -- note that we must insert the new
7780d94caffSDavid Greenman 	 * entry BEFORE this one, so that this entry has the specified
7790d94caffSDavid Greenman 	 * starting address.
780df8bae1dSRodney W. Grimes 	 */
781df8bae1dSRodney W. Grimes 
782f32dbbeeSJohn Dyson 	vm_map_simplify_entry(map, entry);
783f32dbbeeSJohn Dyson 
78411cccda1SJohn Dyson 	/*
78511cccda1SJohn Dyson 	 * If there is no object backing this entry, we might as well create
78611cccda1SJohn Dyson 	 * one now.  If we defer it, an object can get created after the map
78711cccda1SJohn Dyson 	 * is clipped, and individual objects will be created for the split-up
78811cccda1SJohn Dyson 	 * map.  This is a bit of a hack, but is also about the best place to
78911cccda1SJohn Dyson 	 * put this improvement.
79011cccda1SJohn Dyson 	 */
79111cccda1SJohn Dyson 
79211cccda1SJohn Dyson 	if (entry->object.vm_object == NULL) {
79311cccda1SJohn Dyson 		vm_object_t object;
79411cccda1SJohn Dyson 		object = vm_object_allocate(OBJT_DEFAULT,
795c2e11a03SJohn Dyson 				atop(entry->end - entry->start));
79611cccda1SJohn Dyson 		entry->object.vm_object = object;
79711cccda1SJohn Dyson 		entry->offset = 0;
79811cccda1SJohn Dyson 	}
79911cccda1SJohn Dyson 
800df8bae1dSRodney W. Grimes 	new_entry = vm_map_entry_create(map);
801df8bae1dSRodney W. Grimes 	*new_entry = *entry;
802df8bae1dSRodney W. Grimes 
803df8bae1dSRodney W. Grimes 	new_entry->end = start;
804df8bae1dSRodney W. Grimes 	entry->offset += (start - entry->start);
805df8bae1dSRodney W. Grimes 	entry->start = start;
806df8bae1dSRodney W. Grimes 
807df8bae1dSRodney W. Grimes 	vm_map_entry_link(map, entry->prev, new_entry);
808df8bae1dSRodney W. Grimes 
809c0877f10SJohn Dyson 	if ((entry->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP)) == 0) {
810c0877f10SJohn Dyson 		if (new_entry->object.vm_object->ref_count == 1)
811069e9bc1SDoug Rabson 			vm_object_set_flag(new_entry->object.vm_object,
812069e9bc1SDoug Rabson 					   OBJ_ONEMAPPING);
813df8bae1dSRodney W. Grimes 		vm_object_reference(new_entry->object.vm_object);
814df8bae1dSRodney W. Grimes 	}
815c0877f10SJohn Dyson }
816df8bae1dSRodney W. Grimes 
817df8bae1dSRodney W. Grimes /*
818df8bae1dSRodney W. Grimes  *	vm_map_clip_end:	[ internal use only ]
819df8bae1dSRodney W. Grimes  *
820df8bae1dSRodney W. Grimes  *	Asserts that the given entry ends at or before
821df8bae1dSRodney W. Grimes  *	the specified address; if necessary,
822df8bae1dSRodney W. Grimes  *	it splits the entry into two.
823df8bae1dSRodney W. Grimes  */
824df8bae1dSRodney W. Grimes 
825df8bae1dSRodney W. Grimes #define vm_map_clip_end(map, entry, endaddr) \
826df8bae1dSRodney W. Grimes { \
827df8bae1dSRodney W. Grimes 	if (endaddr < entry->end) \
828df8bae1dSRodney W. Grimes 		_vm_map_clip_end(map, entry, endaddr); \
829c0877f10SJohn Dyson 	else if (entry->object.vm_object && (entry->object.vm_object->ref_count == 1)) \
830069e9bc1SDoug Rabson 		vm_object_set_flag(entry->object.vm_object, OBJ_ONEMAPPING); \
831df8bae1dSRodney W. Grimes }
832df8bae1dSRodney W. Grimes 
833df8bae1dSRodney W. Grimes /*
834df8bae1dSRodney W. Grimes  *	This routine is called only when it is known that
835df8bae1dSRodney W. Grimes  *	the entry must be split.
836df8bae1dSRodney W. Grimes  */
8370d94caffSDavid Greenman static void
8380d94caffSDavid Greenman _vm_map_clip_end(map, entry, end)
839c0877f10SJohn Dyson 	vm_map_t map;
840c0877f10SJohn Dyson 	vm_map_entry_t entry;
841c0877f10SJohn Dyson 	vm_offset_t end;
842df8bae1dSRodney W. Grimes {
843c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
844df8bae1dSRodney W. Grimes 
845df8bae1dSRodney W. Grimes 	/*
84611cccda1SJohn Dyson 	 * If there is no object backing this entry, we might as well create
84711cccda1SJohn Dyson 	 * one now.  If we defer it, an object can get created after the map
84811cccda1SJohn Dyson 	 * is clipped, and individual objects will be created for the split-up
84911cccda1SJohn Dyson 	 * map.  This is a bit of a hack, but is also about the best place to
85011cccda1SJohn Dyson 	 * put this improvement.
85111cccda1SJohn Dyson 	 */
85211cccda1SJohn Dyson 
85311cccda1SJohn Dyson 	if (entry->object.vm_object == NULL) {
85411cccda1SJohn Dyson 		vm_object_t object;
85511cccda1SJohn Dyson 		object = vm_object_allocate(OBJT_DEFAULT,
856c2e11a03SJohn Dyson 				atop(entry->end - entry->start));
85711cccda1SJohn Dyson 		entry->object.vm_object = object;
85811cccda1SJohn Dyson 		entry->offset = 0;
85911cccda1SJohn Dyson 	}
86011cccda1SJohn Dyson 
86111cccda1SJohn Dyson 	/*
8620d94caffSDavid Greenman 	 * Create a new entry and insert it AFTER the specified entry
863df8bae1dSRodney W. Grimes 	 */
864df8bae1dSRodney W. Grimes 
865df8bae1dSRodney W. Grimes 	new_entry = vm_map_entry_create(map);
866df8bae1dSRodney W. Grimes 	*new_entry = *entry;
867df8bae1dSRodney W. Grimes 
868df8bae1dSRodney W. Grimes 	new_entry->start = entry->end = end;
869df8bae1dSRodney W. Grimes 	new_entry->offset += (end - entry->start);
870df8bae1dSRodney W. Grimes 
871df8bae1dSRodney W. Grimes 	vm_map_entry_link(map, entry, new_entry);
872df8bae1dSRodney W. Grimes 
873c0877f10SJohn Dyson 	if ((entry->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP)) == 0) {
874c0877f10SJohn Dyson 		if (new_entry->object.vm_object->ref_count == 1)
875069e9bc1SDoug Rabson 			vm_object_set_flag(new_entry->object.vm_object,
876069e9bc1SDoug Rabson 					   OBJ_ONEMAPPING);
877df8bae1dSRodney W. Grimes 		vm_object_reference(new_entry->object.vm_object);
878df8bae1dSRodney W. Grimes 	}
879c0877f10SJohn Dyson }
880df8bae1dSRodney W. Grimes 
881df8bae1dSRodney W. Grimes /*
882df8bae1dSRodney W. Grimes  *	VM_MAP_RANGE_CHECK:	[ internal use only ]
883df8bae1dSRodney W. Grimes  *
884df8bae1dSRodney W. Grimes  *	Asserts that the starting and ending region
885df8bae1dSRodney W. Grimes  *	addresses fall within the valid range of the map.
886df8bae1dSRodney W. Grimes  */
887df8bae1dSRodney W. Grimes #define	VM_MAP_RANGE_CHECK(map, start, end)		\
888df8bae1dSRodney W. Grimes 		{					\
889df8bae1dSRodney W. Grimes 		if (start < vm_map_min(map))		\
890df8bae1dSRodney W. Grimes 			start = vm_map_min(map);	\
891df8bae1dSRodney W. Grimes 		if (end > vm_map_max(map))		\
892df8bae1dSRodney W. Grimes 			end = vm_map_max(map);		\
893df8bae1dSRodney W. Grimes 		if (start > end)			\
894df8bae1dSRodney W. Grimes 			start = end;			\
895df8bae1dSRodney W. Grimes 		}
896df8bae1dSRodney W. Grimes 
897df8bae1dSRodney W. Grimes /*
898df8bae1dSRodney W. Grimes  *	vm_map_submap:		[ kernel use only ]
899df8bae1dSRodney W. Grimes  *
900df8bae1dSRodney W. Grimes  *	Mark the given range as handled by a subordinate map.
901df8bae1dSRodney W. Grimes  *
902df8bae1dSRodney W. Grimes  *	This range must have been created with vm_map_find,
903df8bae1dSRodney W. Grimes  *	and no other operations may have been performed on this
904df8bae1dSRodney W. Grimes  *	range prior to calling vm_map_submap.
905df8bae1dSRodney W. Grimes  *
906df8bae1dSRodney W. Grimes  *	Only a limited number of operations can be performed
907df8bae1dSRodney W. Grimes  *	within this rage after calling vm_map_submap:
908df8bae1dSRodney W. Grimes  *		vm_fault
909df8bae1dSRodney W. Grimes  *	[Don't try vm_map_copy!]
910df8bae1dSRodney W. Grimes  *
911df8bae1dSRodney W. Grimes  *	To remove a submapping, one must first remove the
912df8bae1dSRodney W. Grimes  *	range from the superior map, and then destroy the
913df8bae1dSRodney W. Grimes  *	submap (if desired).  [Better yet, don't try it.]
914df8bae1dSRodney W. Grimes  */
915df8bae1dSRodney W. Grimes int
916df8bae1dSRodney W. Grimes vm_map_submap(map, start, end, submap)
917c0877f10SJohn Dyson 	vm_map_t map;
918c0877f10SJohn Dyson 	vm_offset_t start;
919c0877f10SJohn Dyson 	vm_offset_t end;
920df8bae1dSRodney W. Grimes 	vm_map_t submap;
921df8bae1dSRodney W. Grimes {
922df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
923c0877f10SJohn Dyson 	int result = KERN_INVALID_ARGUMENT;
924df8bae1dSRodney W. Grimes 
925df8bae1dSRodney W. Grimes 	vm_map_lock(map);
926df8bae1dSRodney W. Grimes 
927df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
928df8bae1dSRodney W. Grimes 
929df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &entry)) {
930df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
9310d94caffSDavid Greenman 	} else
932df8bae1dSRodney W. Grimes 		entry = entry->next;
933df8bae1dSRodney W. Grimes 
934df8bae1dSRodney W. Grimes 	vm_map_clip_end(map, entry, end);
935df8bae1dSRodney W. Grimes 
936df8bae1dSRodney W. Grimes 	if ((entry->start == start) && (entry->end == end) &&
937afa07f7eSJohn Dyson 	    ((entry->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_COW)) == 0) &&
938afa07f7eSJohn Dyson 	    (entry->object.vm_object == NULL)) {
9392d8acc0fSJohn Dyson 		entry->object.sub_map = submap;
940afa07f7eSJohn Dyson 		entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
941df8bae1dSRodney W. Grimes 		result = KERN_SUCCESS;
942df8bae1dSRodney W. Grimes 	}
943df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
944df8bae1dSRodney W. Grimes 
945df8bae1dSRodney W. Grimes 	return (result);
946df8bae1dSRodney W. Grimes }
947df8bae1dSRodney W. Grimes 
948df8bae1dSRodney W. Grimes /*
949df8bae1dSRodney W. Grimes  *	vm_map_protect:
950df8bae1dSRodney W. Grimes  *
951df8bae1dSRodney W. Grimes  *	Sets the protection of the specified address
952df8bae1dSRodney W. Grimes  *	region in the target map.  If "set_max" is
953df8bae1dSRodney W. Grimes  *	specified, the maximum protection is to be set;
954df8bae1dSRodney W. Grimes  *	otherwise, only the current protection is affected.
955df8bae1dSRodney W. Grimes  */
956df8bae1dSRodney W. Grimes int
957b9dcd593SBruce Evans vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
958b9dcd593SBruce Evans 	       vm_prot_t new_prot, boolean_t set_max)
959df8bae1dSRodney W. Grimes {
960c0877f10SJohn Dyson 	vm_map_entry_t current;
961df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
962df8bae1dSRodney W. Grimes 
963df8bae1dSRodney W. Grimes 	vm_map_lock(map);
964df8bae1dSRodney W. Grimes 
965df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
966df8bae1dSRodney W. Grimes 
967df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &entry)) {
968df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
969b7b2aac2SJohn Dyson 	} else {
970df8bae1dSRodney W. Grimes 		entry = entry->next;
971b7b2aac2SJohn Dyson 	}
972df8bae1dSRodney W. Grimes 
973df8bae1dSRodney W. Grimes 	/*
9740d94caffSDavid Greenman 	 * Make a first pass to check for protection violations.
975df8bae1dSRodney W. Grimes 	 */
976df8bae1dSRodney W. Grimes 
977df8bae1dSRodney W. Grimes 	current = entry;
978df8bae1dSRodney W. Grimes 	while ((current != &map->header) && (current->start < end)) {
979afa07f7eSJohn Dyson 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
980a1f6d91cSDavid Greenman 			vm_map_unlock(map);
981df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ARGUMENT);
982a1f6d91cSDavid Greenman 		}
983df8bae1dSRodney W. Grimes 		if ((new_prot & current->max_protection) != new_prot) {
984df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
985df8bae1dSRodney W. Grimes 			return (KERN_PROTECTION_FAILURE);
986df8bae1dSRodney W. Grimes 		}
987df8bae1dSRodney W. Grimes 		current = current->next;
988df8bae1dSRodney W. Grimes 	}
989df8bae1dSRodney W. Grimes 
990df8bae1dSRodney W. Grimes 	/*
9910d94caffSDavid Greenman 	 * Go back and fix up protections. [Note that clipping is not
9920d94caffSDavid Greenman 	 * necessary the second time.]
993df8bae1dSRodney W. Grimes 	 */
994df8bae1dSRodney W. Grimes 
995df8bae1dSRodney W. Grimes 	current = entry;
996df8bae1dSRodney W. Grimes 
997df8bae1dSRodney W. Grimes 	while ((current != &map->header) && (current->start < end)) {
998df8bae1dSRodney W. Grimes 		vm_prot_t old_prot;
999df8bae1dSRodney W. Grimes 
1000df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, current, end);
1001df8bae1dSRodney W. Grimes 
1002df8bae1dSRodney W. Grimes 		old_prot = current->protection;
1003df8bae1dSRodney W. Grimes 		if (set_max)
1004df8bae1dSRodney W. Grimes 			current->protection =
1005df8bae1dSRodney W. Grimes 			    (current->max_protection = new_prot) &
1006df8bae1dSRodney W. Grimes 			    old_prot;
1007df8bae1dSRodney W. Grimes 		else
1008df8bae1dSRodney W. Grimes 			current->protection = new_prot;
1009df8bae1dSRodney W. Grimes 
1010df8bae1dSRodney W. Grimes 		/*
10110d94caffSDavid Greenman 		 * Update physical map if necessary. Worry about copy-on-write
10120d94caffSDavid Greenman 		 * here -- CHECK THIS XXX
1013df8bae1dSRodney W. Grimes 		 */
1014df8bae1dSRodney W. Grimes 
1015df8bae1dSRodney W. Grimes 		if (current->protection != old_prot) {
1016afa07f7eSJohn Dyson #define MASK(entry)	(((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1017df8bae1dSRodney W. Grimes 							VM_PROT_ALL)
1018df8bae1dSRodney W. Grimes 
1019afa07f7eSJohn Dyson 			if (current->eflags & MAP_ENTRY_IS_A_MAP) {
1020df8bae1dSRodney W. Grimes 				vm_map_entry_t share_entry;
1021df8bae1dSRodney W. Grimes 				vm_offset_t share_end;
1022df8bae1dSRodney W. Grimes 
1023df8bae1dSRodney W. Grimes 				vm_map_lock(current->object.share_map);
1024df8bae1dSRodney W. Grimes 				(void) vm_map_lookup_entry(
1025df8bae1dSRodney W. Grimes 				    current->object.share_map,
1026df8bae1dSRodney W. Grimes 				    current->offset,
1027df8bae1dSRodney W. Grimes 				    &share_entry);
1028df8bae1dSRodney W. Grimes 				share_end = current->offset +
1029df8bae1dSRodney W. Grimes 				    (current->end - current->start);
1030df8bae1dSRodney W. Grimes 				while ((share_entry !=
1031df8bae1dSRodney W. Grimes 					&current->object.share_map->header) &&
1032df8bae1dSRodney W. Grimes 				    (share_entry->start < share_end)) {
1033df8bae1dSRodney W. Grimes 
1034df8bae1dSRodney W. Grimes 					pmap_protect(map->pmap,
10355270eceaSBruce Evans 					    (qmax(share_entry->start,
1036df8bae1dSRodney W. Grimes 						    current->offset) -
1037df8bae1dSRodney W. Grimes 						current->offset +
1038df8bae1dSRodney W. Grimes 						current->start),
1039df8bae1dSRodney W. Grimes 					    min(share_entry->end,
1040df8bae1dSRodney W. Grimes 						share_end) -
1041df8bae1dSRodney W. Grimes 					    current->offset +
1042df8bae1dSRodney W. Grimes 					    current->start,
1043df8bae1dSRodney W. Grimes 					    current->protection &
1044df8bae1dSRodney W. Grimes 					    MASK(share_entry));
1045df8bae1dSRodney W. Grimes 
1046df8bae1dSRodney W. Grimes 					share_entry = share_entry->next;
1047df8bae1dSRodney W. Grimes 				}
1048df8bae1dSRodney W. Grimes 				vm_map_unlock(current->object.share_map);
10490d94caffSDavid Greenman 			} else
1050df8bae1dSRodney W. Grimes 				pmap_protect(map->pmap, current->start,
1051df8bae1dSRodney W. Grimes 				    current->end,
1052df8bae1dSRodney W. Grimes 				    current->protection & MASK(entry));
1053df8bae1dSRodney W. Grimes #undef	MASK
1054df8bae1dSRodney W. Grimes 		}
10557d78abc9SJohn Dyson 
10567d78abc9SJohn Dyson 		vm_map_simplify_entry(map, current);
10577d78abc9SJohn Dyson 
1058df8bae1dSRodney W. Grimes 		current = current->next;
1059df8bae1dSRodney W. Grimes 	}
1060df8bae1dSRodney W. Grimes 
10612d8acc0fSJohn Dyson 	map->timestamp++;
1062df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1063df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
1064df8bae1dSRodney W. Grimes }
1065df8bae1dSRodney W. Grimes 
1066df8bae1dSRodney W. Grimes /*
1067867a482dSJohn Dyson  *	vm_map_madvise:
1068867a482dSJohn Dyson  *
1069867a482dSJohn Dyson  * 	This routine traverses a processes map handling the madvise
1070867a482dSJohn Dyson  *	system call.
1071867a482dSJohn Dyson  */
1072867a482dSJohn Dyson void
1073867a482dSJohn Dyson vm_map_madvise(map, pmap, start, end, advise)
1074867a482dSJohn Dyson 	vm_map_t map;
1075867a482dSJohn Dyson 	pmap_t pmap;
1076867a482dSJohn Dyson 	vm_offset_t start, end;
1077867a482dSJohn Dyson 	int advise;
1078867a482dSJohn Dyson {
1079c0877f10SJohn Dyson 	vm_map_entry_t current;
1080867a482dSJohn Dyson 	vm_map_entry_t entry;
1081867a482dSJohn Dyson 
1082867a482dSJohn Dyson 	vm_map_lock(map);
1083867a482dSJohn Dyson 
1084867a482dSJohn Dyson 	VM_MAP_RANGE_CHECK(map, start, end);
1085867a482dSJohn Dyson 
1086867a482dSJohn Dyson 	if (vm_map_lookup_entry(map, start, &entry)) {
1087867a482dSJohn Dyson 		vm_map_clip_start(map, entry, start);
1088867a482dSJohn Dyson 	} else
1089867a482dSJohn Dyson 		entry = entry->next;
1090867a482dSJohn Dyson 
1091867a482dSJohn Dyson 	for(current = entry;
1092867a482dSJohn Dyson 		(current != &map->header) && (current->start < end);
1093867a482dSJohn Dyson 		current = current->next) {
109447221757SJohn Dyson 		vm_size_t size;
1095fed9a903SJohn Dyson 
1096afa07f7eSJohn Dyson 		if (current->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP)) {
1097867a482dSJohn Dyson 			continue;
1098867a482dSJohn Dyson 		}
1099fed9a903SJohn Dyson 
110047221757SJohn Dyson 		vm_map_clip_end(map, current, end);
110147221757SJohn Dyson 		size = current->end - current->start;
110247221757SJohn Dyson 
1103fed9a903SJohn Dyson 		/*
1104fed9a903SJohn Dyson 		 * Create an object if needed
1105fed9a903SJohn Dyson 		 */
1106fed9a903SJohn Dyson 		if (current->object.vm_object == NULL) {
1107fed9a903SJohn Dyson 			vm_object_t object;
110847221757SJohn Dyson 			if ((advise == MADV_FREE) || (advise == MADV_DONTNEED))
110947221757SJohn Dyson 				continue;
1110fed9a903SJohn Dyson 			object = vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(size));
1111fed9a903SJohn Dyson 			current->object.vm_object = object;
1112fed9a903SJohn Dyson 			current->offset = 0;
1113fed9a903SJohn Dyson 		}
1114fed9a903SJohn Dyson 
1115867a482dSJohn Dyson 		switch (advise) {
1116867a482dSJohn Dyson 	case MADV_NORMAL:
1117867a482dSJohn Dyson 			current->object.vm_object->behavior = OBJ_NORMAL;
1118867a482dSJohn Dyson 			break;
1119867a482dSJohn Dyson 	case MADV_SEQUENTIAL:
1120867a482dSJohn Dyson 			current->object.vm_object->behavior = OBJ_SEQUENTIAL;
1121867a482dSJohn Dyson 			break;
1122867a482dSJohn Dyson 	case MADV_RANDOM:
1123867a482dSJohn Dyson 			current->object.vm_object->behavior = OBJ_RANDOM;
1124867a482dSJohn Dyson 			break;
1125867a482dSJohn Dyson 	/*
1126867a482dSJohn Dyson 	 * Right now, we could handle DONTNEED and WILLNEED with common code.
1127867a482dSJohn Dyson 	 * They are mostly the same, except for the potential async reads (NYI).
1128867a482dSJohn Dyson 	 */
11290a47b48bSJohn Dyson 	case MADV_FREE:
1130867a482dSJohn Dyson 	case MADV_DONTNEED:
1131867a482dSJohn Dyson 			{
1132867a482dSJohn Dyson 				vm_pindex_t pindex;
1133867a482dSJohn Dyson 				int count;
113447221757SJohn Dyson 				pindex = OFF_TO_IDX(current->offset);
1135867a482dSJohn Dyson 				count = OFF_TO_IDX(size);
1136867a482dSJohn Dyson 				/*
1137867a482dSJohn Dyson 				 * MADV_DONTNEED removes the page from all
1138867a482dSJohn Dyson 				 * pmaps, so pmap_remove is not necessary.
1139867a482dSJohn Dyson 				 */
1140867a482dSJohn Dyson 				vm_object_madvise(current->object.vm_object,
1141867a482dSJohn Dyson 					pindex, count, advise);
1142867a482dSJohn Dyson 			}
1143867a482dSJohn Dyson 			break;
1144867a482dSJohn Dyson 
1145867a482dSJohn Dyson 	case MADV_WILLNEED:
1146867a482dSJohn Dyson 			{
1147867a482dSJohn Dyson 				vm_pindex_t pindex;
1148867a482dSJohn Dyson 				int count;
11494334b0d8SJohn Dyson 				pindex = OFF_TO_IDX(current->offset);
1150867a482dSJohn Dyson 				count = OFF_TO_IDX(size);
1151867a482dSJohn Dyson 				vm_object_madvise(current->object.vm_object,
1152867a482dSJohn Dyson 					pindex, count, advise);
1153867a482dSJohn Dyson 				pmap_object_init_pt(pmap, current->start,
1154867a482dSJohn Dyson 					current->object.vm_object, pindex,
1155867a482dSJohn Dyson 					(count << PAGE_SHIFT), 0);
1156867a482dSJohn Dyson 			}
1157867a482dSJohn Dyson 			break;
1158867a482dSJohn Dyson 
1159867a482dSJohn Dyson 	default:
1160867a482dSJohn Dyson 			break;
1161867a482dSJohn Dyson 		}
1162867a482dSJohn Dyson 	}
1163867a482dSJohn Dyson 
116447221757SJohn Dyson 	map->timestamp++;
1165867a482dSJohn Dyson 	vm_map_simplify_entry(map, entry);
1166867a482dSJohn Dyson 	vm_map_unlock(map);
1167867a482dSJohn Dyson 	return;
1168867a482dSJohn Dyson }
1169867a482dSJohn Dyson 
1170867a482dSJohn Dyson 
1171867a482dSJohn Dyson /*
1172df8bae1dSRodney W. Grimes  *	vm_map_inherit:
1173df8bae1dSRodney W. Grimes  *
1174df8bae1dSRodney W. Grimes  *	Sets the inheritance of the specified address
1175df8bae1dSRodney W. Grimes  *	range in the target map.  Inheritance
1176df8bae1dSRodney W. Grimes  *	affects how the map will be shared with
1177df8bae1dSRodney W. Grimes  *	child maps at the time of vm_map_fork.
1178df8bae1dSRodney W. Grimes  */
1179df8bae1dSRodney W. Grimes int
1180b9dcd593SBruce Evans vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
1181b9dcd593SBruce Evans 	       vm_inherit_t new_inheritance)
1182df8bae1dSRodney W. Grimes {
1183c0877f10SJohn Dyson 	vm_map_entry_t entry;
1184df8bae1dSRodney W. Grimes 	vm_map_entry_t temp_entry;
1185df8bae1dSRodney W. Grimes 
1186df8bae1dSRodney W. Grimes 	switch (new_inheritance) {
1187df8bae1dSRodney W. Grimes 	case VM_INHERIT_NONE:
1188df8bae1dSRodney W. Grimes 	case VM_INHERIT_COPY:
1189df8bae1dSRodney W. Grimes 	case VM_INHERIT_SHARE:
1190df8bae1dSRodney W. Grimes 		break;
1191df8bae1dSRodney W. Grimes 	default:
1192df8bae1dSRodney W. Grimes 		return (KERN_INVALID_ARGUMENT);
1193df8bae1dSRodney W. Grimes 	}
1194df8bae1dSRodney W. Grimes 
1195df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1196df8bae1dSRodney W. Grimes 
1197df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1198df8bae1dSRodney W. Grimes 
1199df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &temp_entry)) {
1200df8bae1dSRodney W. Grimes 		entry = temp_entry;
1201df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
12020d94caffSDavid Greenman 	} else
1203df8bae1dSRodney W. Grimes 		entry = temp_entry->next;
1204df8bae1dSRodney W. Grimes 
1205df8bae1dSRodney W. Grimes 	while ((entry != &map->header) && (entry->start < end)) {
1206df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, entry, end);
1207df8bae1dSRodney W. Grimes 
1208df8bae1dSRodney W. Grimes 		entry->inheritance = new_inheritance;
1209df8bae1dSRodney W. Grimes 
1210df8bae1dSRodney W. Grimes 		entry = entry->next;
1211df8bae1dSRodney W. Grimes 	}
1212df8bae1dSRodney W. Grimes 
1213f32dbbeeSJohn Dyson 	vm_map_simplify_entry(map, temp_entry);
121447221757SJohn Dyson 	map->timestamp++;
1215df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1216df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
1217df8bae1dSRodney W. Grimes }
1218df8bae1dSRodney W. Grimes 
1219df8bae1dSRodney W. Grimes /*
12207aaaa4fdSJohn Dyson  * Implement the semantics of mlock
12217aaaa4fdSJohn Dyson  */
12227aaaa4fdSJohn Dyson int
12237aaaa4fdSJohn Dyson vm_map_user_pageable(map, start, end, new_pageable)
1224c0877f10SJohn Dyson 	vm_map_t map;
1225c0877f10SJohn Dyson 	vm_offset_t start;
1226c0877f10SJohn Dyson 	vm_offset_t end;
1227c0877f10SJohn Dyson 	boolean_t new_pageable;
12287aaaa4fdSJohn Dyson {
1229b44959ceSTor Egge 	vm_map_entry_t entry;
12307aaaa4fdSJohn Dyson 	vm_map_entry_t start_entry;
1231b44959ceSTor Egge 	vm_offset_t estart;
12327aaaa4fdSJohn Dyson 	int rv;
12337aaaa4fdSJohn Dyson 
12347aaaa4fdSJohn Dyson 	vm_map_lock(map);
12357aaaa4fdSJohn Dyson 	VM_MAP_RANGE_CHECK(map, start, end);
12367aaaa4fdSJohn Dyson 
12377aaaa4fdSJohn Dyson 	if (vm_map_lookup_entry(map, start, &start_entry) == FALSE) {
12387aaaa4fdSJohn Dyson 		vm_map_unlock(map);
12397aaaa4fdSJohn Dyson 		return (KERN_INVALID_ADDRESS);
12407aaaa4fdSJohn Dyson 	}
12417aaaa4fdSJohn Dyson 
12427aaaa4fdSJohn Dyson 	if (new_pageable) {
12437aaaa4fdSJohn Dyson 
12447aaaa4fdSJohn Dyson 		entry = start_entry;
12457aaaa4fdSJohn Dyson 		vm_map_clip_start(map, entry, start);
12467aaaa4fdSJohn Dyson 
12477aaaa4fdSJohn Dyson 		/*
12487aaaa4fdSJohn Dyson 		 * Now decrement the wiring count for each region. If a region
12497aaaa4fdSJohn Dyson 		 * becomes completely unwired, unwire its physical pages and
12507aaaa4fdSJohn Dyson 		 * mappings.
12517aaaa4fdSJohn Dyson 		 */
1252996c772fSJohn Dyson 		vm_map_set_recursive(map);
12537aaaa4fdSJohn Dyson 
12547aaaa4fdSJohn Dyson 		entry = start_entry;
12557aaaa4fdSJohn Dyson 		while ((entry != &map->header) && (entry->start < end)) {
1256afa07f7eSJohn Dyson 			if (entry->eflags & MAP_ENTRY_USER_WIRED) {
12577aaaa4fdSJohn Dyson 				vm_map_clip_end(map, entry, end);
1258afa07f7eSJohn Dyson 				entry->eflags &= ~MAP_ENTRY_USER_WIRED;
12597aaaa4fdSJohn Dyson 				entry->wired_count--;
12607aaaa4fdSJohn Dyson 				if (entry->wired_count == 0)
12617aaaa4fdSJohn Dyson 					vm_fault_unwire(map, entry->start, entry->end);
12627aaaa4fdSJohn Dyson 			}
1263b44959ceSTor Egge 			vm_map_simplify_entry(map,entry);
12647aaaa4fdSJohn Dyson 			entry = entry->next;
12657aaaa4fdSJohn Dyson 		}
1266996c772fSJohn Dyson 		vm_map_clear_recursive(map);
12677aaaa4fdSJohn Dyson 	} else {
12687aaaa4fdSJohn Dyson 
12697aaaa4fdSJohn Dyson 		entry = start_entry;
12707aaaa4fdSJohn Dyson 
12717aaaa4fdSJohn Dyson 		while ((entry != &map->header) && (entry->start < end)) {
12727aaaa4fdSJohn Dyson 
1273afa07f7eSJohn Dyson 			if (entry->eflags & MAP_ENTRY_USER_WIRED) {
12747aaaa4fdSJohn Dyson 				entry = entry->next;
12757aaaa4fdSJohn Dyson 				continue;
12767aaaa4fdSJohn Dyson 			}
12777aaaa4fdSJohn Dyson 
12787aaaa4fdSJohn Dyson 			if (entry->wired_count != 0) {
12797aaaa4fdSJohn Dyson 				entry->wired_count++;
1280afa07f7eSJohn Dyson 				entry->eflags |= MAP_ENTRY_USER_WIRED;
12817aaaa4fdSJohn Dyson 				entry = entry->next;
12827aaaa4fdSJohn Dyson 				continue;
12837aaaa4fdSJohn Dyson 			}
12847aaaa4fdSJohn Dyson 
12857aaaa4fdSJohn Dyson 			/* Here on entry being newly wired */
12867aaaa4fdSJohn Dyson 
1287afa07f7eSJohn Dyson 			if ((entry->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP)) == 0) {
1288afa07f7eSJohn Dyson 				int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY;
12897aaaa4fdSJohn Dyson 				if (copyflag && ((entry->protection & VM_PROT_WRITE) != 0)) {
12907aaaa4fdSJohn Dyson 
12917aaaa4fdSJohn Dyson 					vm_object_shadow(&entry->object.vm_object,
12927aaaa4fdSJohn Dyson 					    &entry->offset,
1293c2e11a03SJohn Dyson 					    atop(entry->end - entry->start));
1294afa07f7eSJohn Dyson 					entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
12957aaaa4fdSJohn Dyson 
12967aaaa4fdSJohn Dyson 				} else if (entry->object.vm_object == NULL) {
12977aaaa4fdSJohn Dyson 
12987aaaa4fdSJohn Dyson 					entry->object.vm_object =
12997aaaa4fdSJohn Dyson 					    vm_object_allocate(OBJT_DEFAULT,
1300c2e11a03SJohn Dyson 						atop(entry->end - entry->start));
13017aaaa4fdSJohn Dyson 					entry->offset = (vm_offset_t) 0;
13027aaaa4fdSJohn Dyson 
13037aaaa4fdSJohn Dyson 				}
13047aaaa4fdSJohn Dyson 				default_pager_convert_to_swapq(entry->object.vm_object);
13057aaaa4fdSJohn Dyson 			}
13067aaaa4fdSJohn Dyson 
13077aaaa4fdSJohn Dyson 			vm_map_clip_start(map, entry, start);
13087aaaa4fdSJohn Dyson 			vm_map_clip_end(map, entry, end);
13097aaaa4fdSJohn Dyson 
13107aaaa4fdSJohn Dyson 			entry->wired_count++;
1311afa07f7eSJohn Dyson 			entry->eflags |= MAP_ENTRY_USER_WIRED;
1312b44959ceSTor Egge 			estart = entry->start;
13137aaaa4fdSJohn Dyson 
13147aaaa4fdSJohn Dyson 			/* First we need to allow map modifications */
1315996c772fSJohn Dyson 			vm_map_set_recursive(map);
131603e9c6c1SJohn Dyson 			vm_map_lock_downgrade(map);
131747221757SJohn Dyson 			map->timestamp++;
13187aaaa4fdSJohn Dyson 
13197aaaa4fdSJohn Dyson 			rv = vm_fault_user_wire(map, entry->start, entry->end);
13207aaaa4fdSJohn Dyson 			if (rv) {
13217aaaa4fdSJohn Dyson 
13227aaaa4fdSJohn Dyson 				entry->wired_count--;
1323afa07f7eSJohn Dyson 				entry->eflags &= ~MAP_ENTRY_USER_WIRED;
13247aaaa4fdSJohn Dyson 
1325996c772fSJohn Dyson 				vm_map_clear_recursive(map);
13267aaaa4fdSJohn Dyson 				vm_map_unlock(map);
13277aaaa4fdSJohn Dyson 
13287aaaa4fdSJohn Dyson 				(void) vm_map_user_pageable(map, start, entry->start, TRUE);
13297aaaa4fdSJohn Dyson 				return rv;
13307aaaa4fdSJohn Dyson 			}
13317aaaa4fdSJohn Dyson 
1332996c772fSJohn Dyson 			vm_map_clear_recursive(map);
1333b44959ceSTor Egge 			if (vm_map_lock_upgrade(map)) {
1334b44959ceSTor Egge 				vm_map_lock(map);
1335b44959ceSTor Egge 				if (vm_map_lookup_entry(map, estart, &entry)
1336b44959ceSTor Egge 				    == FALSE) {
1337b44959ceSTor Egge 					vm_map_unlock(map);
1338b44959ceSTor Egge 					(void) vm_map_user_pageable(map,
1339b44959ceSTor Egge 								    start,
1340b44959ceSTor Egge 								    estart,
1341b44959ceSTor Egge 								    TRUE);
1342b44959ceSTor Egge 					return (KERN_INVALID_ADDRESS);
1343b44959ceSTor Egge 				}
1344b44959ceSTor Egge 			}
1345b44959ceSTor Egge 			vm_map_simplify_entry(map,entry);
13467aaaa4fdSJohn Dyson 		}
13477aaaa4fdSJohn Dyson 	}
134847221757SJohn Dyson 	map->timestamp++;
13497aaaa4fdSJohn Dyson 	vm_map_unlock(map);
13507aaaa4fdSJohn Dyson 	return KERN_SUCCESS;
13517aaaa4fdSJohn Dyson }
13527aaaa4fdSJohn Dyson 
13537aaaa4fdSJohn Dyson /*
1354df8bae1dSRodney W. Grimes  *	vm_map_pageable:
1355df8bae1dSRodney W. Grimes  *
1356df8bae1dSRodney W. Grimes  *	Sets the pageability of the specified address
1357df8bae1dSRodney W. Grimes  *	range in the target map.  Regions specified
1358df8bae1dSRodney W. Grimes  *	as not pageable require locked-down physical
1359df8bae1dSRodney W. Grimes  *	memory and physical page maps.
1360df8bae1dSRodney W. Grimes  *
1361df8bae1dSRodney W. Grimes  *	The map must not be locked, but a reference
1362df8bae1dSRodney W. Grimes  *	must remain to the map throughout the call.
1363df8bae1dSRodney W. Grimes  */
1364df8bae1dSRodney W. Grimes int
1365df8bae1dSRodney W. Grimes vm_map_pageable(map, start, end, new_pageable)
1366c0877f10SJohn Dyson 	vm_map_t map;
1367c0877f10SJohn Dyson 	vm_offset_t start;
1368c0877f10SJohn Dyson 	vm_offset_t end;
1369c0877f10SJohn Dyson 	boolean_t new_pageable;
1370df8bae1dSRodney W. Grimes {
1371c0877f10SJohn Dyson 	vm_map_entry_t entry;
1372df8bae1dSRodney W. Grimes 	vm_map_entry_t start_entry;
1373c0877f10SJohn Dyson 	vm_offset_t failed = 0;
1374df8bae1dSRodney W. Grimes 	int rv;
1375df8bae1dSRodney W. Grimes 
1376df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1377df8bae1dSRodney W. Grimes 
1378df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1379df8bae1dSRodney W. Grimes 
1380df8bae1dSRodney W. Grimes 	/*
13810d94caffSDavid Greenman 	 * Only one pageability change may take place at one time, since
13820d94caffSDavid Greenman 	 * vm_fault assumes it will be called only once for each
13830d94caffSDavid Greenman 	 * wiring/unwiring.  Therefore, we have to make sure we're actually
13840d94caffSDavid Greenman 	 * changing the pageability for the entire region.  We do so before
13850d94caffSDavid Greenman 	 * making any changes.
1386df8bae1dSRodney W. Grimes 	 */
1387df8bae1dSRodney W. Grimes 
1388df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &start_entry) == FALSE) {
1389df8bae1dSRodney W. Grimes 		vm_map_unlock(map);
1390df8bae1dSRodney W. Grimes 		return (KERN_INVALID_ADDRESS);
1391df8bae1dSRodney W. Grimes 	}
1392df8bae1dSRodney W. Grimes 	entry = start_entry;
1393df8bae1dSRodney W. Grimes 
1394df8bae1dSRodney W. Grimes 	/*
13950d94caffSDavid Greenman 	 * Actions are rather different for wiring and unwiring, so we have
13960d94caffSDavid Greenman 	 * two separate cases.
1397df8bae1dSRodney W. Grimes 	 */
1398df8bae1dSRodney W. Grimes 
1399df8bae1dSRodney W. Grimes 	if (new_pageable) {
1400df8bae1dSRodney W. Grimes 
1401df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
1402df8bae1dSRodney W. Grimes 
1403df8bae1dSRodney W. Grimes 		/*
14040d94caffSDavid Greenman 		 * Unwiring.  First ensure that the range to be unwired is
14050d94caffSDavid Greenman 		 * really wired down and that there are no holes.
1406df8bae1dSRodney W. Grimes 		 */
1407df8bae1dSRodney W. Grimes 		while ((entry != &map->header) && (entry->start < end)) {
1408df8bae1dSRodney W. Grimes 
1409df8bae1dSRodney W. Grimes 			if (entry->wired_count == 0 ||
1410df8bae1dSRodney W. Grimes 			    (entry->end < end &&
1411df8bae1dSRodney W. Grimes 				(entry->next == &map->header ||
1412df8bae1dSRodney W. Grimes 				    entry->next->start > entry->end))) {
1413df8bae1dSRodney W. Grimes 				vm_map_unlock(map);
1414df8bae1dSRodney W. Grimes 				return (KERN_INVALID_ARGUMENT);
1415df8bae1dSRodney W. Grimes 			}
1416df8bae1dSRodney W. Grimes 			entry = entry->next;
1417df8bae1dSRodney W. Grimes 		}
1418df8bae1dSRodney W. Grimes 
1419df8bae1dSRodney W. Grimes 		/*
14200d94caffSDavid Greenman 		 * Now decrement the wiring count for each region. If a region
14210d94caffSDavid Greenman 		 * becomes completely unwired, unwire its physical pages and
14220d94caffSDavid Greenman 		 * mappings.
1423df8bae1dSRodney W. Grimes 		 */
1424996c772fSJohn Dyson 		vm_map_set_recursive(map);
1425df8bae1dSRodney W. Grimes 
1426df8bae1dSRodney W. Grimes 		entry = start_entry;
1427df8bae1dSRodney W. Grimes 		while ((entry != &map->header) && (entry->start < end)) {
1428df8bae1dSRodney W. Grimes 			vm_map_clip_end(map, entry, end);
1429df8bae1dSRodney W. Grimes 
1430df8bae1dSRodney W. Grimes 			entry->wired_count--;
1431df8bae1dSRodney W. Grimes 			if (entry->wired_count == 0)
1432df8bae1dSRodney W. Grimes 				vm_fault_unwire(map, entry->start, entry->end);
1433df8bae1dSRodney W. Grimes 
1434df8bae1dSRodney W. Grimes 			entry = entry->next;
1435df8bae1dSRodney W. Grimes 		}
1436f32dbbeeSJohn Dyson 		vm_map_simplify_entry(map, start_entry);
1437996c772fSJohn Dyson 		vm_map_clear_recursive(map);
14380d94caffSDavid Greenman 	} else {
1439df8bae1dSRodney W. Grimes 		/*
1440df8bae1dSRodney W. Grimes 		 * Wiring.  We must do this in two passes:
1441df8bae1dSRodney W. Grimes 		 *
14420d94caffSDavid Greenman 		 * 1.  Holding the write lock, we create any shadow or zero-fill
14430d94caffSDavid Greenman 		 * objects that need to be created. Then we clip each map
14440d94caffSDavid Greenman 		 * entry to the region to be wired and increment its wiring
14450d94caffSDavid Greenman 		 * count.  We create objects before clipping the map entries
1446df8bae1dSRodney W. Grimes 		 * to avoid object proliferation.
1447df8bae1dSRodney W. Grimes 		 *
14480d94caffSDavid Greenman 		 * 2.  We downgrade to a read lock, and call vm_fault_wire to
14490d94caffSDavid Greenman 		 * fault in the pages for any newly wired area (wired_count is
14500d94caffSDavid Greenman 		 * 1).
1451df8bae1dSRodney W. Grimes 		 *
14520d94caffSDavid Greenman 		 * Downgrading to a read lock for vm_fault_wire avoids a possible
145324a1cce3SDavid Greenman 		 * deadlock with another process that may have faulted on one
14540d94caffSDavid Greenman 		 * of the pages to be wired (it would mark the page busy,
14550d94caffSDavid Greenman 		 * blocking us, then in turn block on the map lock that we
14560d94caffSDavid Greenman 		 * hold).  Because of problems in the recursive lock package,
14570d94caffSDavid Greenman 		 * we cannot upgrade to a write lock in vm_map_lookup.  Thus,
14580d94caffSDavid Greenman 		 * any actions that require the write lock must be done
14590d94caffSDavid Greenman 		 * beforehand.  Because we keep the read lock on the map, the
14600d94caffSDavid Greenman 		 * copy-on-write status of the entries we modify here cannot
14610d94caffSDavid Greenman 		 * change.
1462df8bae1dSRodney W. Grimes 		 */
1463df8bae1dSRodney W. Grimes 
1464df8bae1dSRodney W. Grimes 		/*
1465df8bae1dSRodney W. Grimes 		 * Pass 1.
1466df8bae1dSRodney W. Grimes 		 */
1467df8bae1dSRodney W. Grimes 		while ((entry != &map->header) && (entry->start < end)) {
1468df8bae1dSRodney W. Grimes 			if (entry->wired_count == 0) {
1469df8bae1dSRodney W. Grimes 
1470df8bae1dSRodney W. Grimes 				/*
1471df8bae1dSRodney W. Grimes 				 * Perform actions of vm_map_lookup that need
1472df8bae1dSRodney W. Grimes 				 * the write lock on the map: create a shadow
1473df8bae1dSRodney W. Grimes 				 * object for a copy-on-write region, or an
1474df8bae1dSRodney W. Grimes 				 * object for a zero-fill region.
1475df8bae1dSRodney W. Grimes 				 *
1476df8bae1dSRodney W. Grimes 				 * We don't have to do this for entries that
14770d94caffSDavid Greenman 				 * point to sharing maps, because we won't
14780d94caffSDavid Greenman 				 * hold the lock on the sharing map.
1479df8bae1dSRodney W. Grimes 				 */
1480afa07f7eSJohn Dyson 				if ((entry->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP)) == 0) {
1481afa07f7eSJohn Dyson 					int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY;
1482b5b40fa6SJohn Dyson 					if (copyflag &&
1483df8bae1dSRodney W. Grimes 					    ((entry->protection & VM_PROT_WRITE) != 0)) {
1484df8bae1dSRodney W. Grimes 
1485df8bae1dSRodney W. Grimes 						vm_object_shadow(&entry->object.vm_object,
1486df8bae1dSRodney W. Grimes 						    &entry->offset,
1487c2e11a03SJohn Dyson 						    atop(entry->end - entry->start));
1488afa07f7eSJohn Dyson 						entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
14890d94caffSDavid Greenman 					} else if (entry->object.vm_object == NULL) {
1490df8bae1dSRodney W. Grimes 						entry->object.vm_object =
1491a316d390SJohn Dyson 						    vm_object_allocate(OBJT_DEFAULT,
1492c2e11a03SJohn Dyson 							atop(entry->end - entry->start));
1493df8bae1dSRodney W. Grimes 						entry->offset = (vm_offset_t) 0;
1494df8bae1dSRodney W. Grimes 					}
1495b5b40fa6SJohn Dyson 					default_pager_convert_to_swapq(entry->object.vm_object);
1496df8bae1dSRodney W. Grimes 				}
1497df8bae1dSRodney W. Grimes 			}
1498df8bae1dSRodney W. Grimes 			vm_map_clip_start(map, entry, start);
1499df8bae1dSRodney W. Grimes 			vm_map_clip_end(map, entry, end);
1500df8bae1dSRodney W. Grimes 			entry->wired_count++;
1501df8bae1dSRodney W. Grimes 
1502df8bae1dSRodney W. Grimes 			/*
1503df8bae1dSRodney W. Grimes 			 * Check for holes
1504df8bae1dSRodney W. Grimes 			 */
1505df8bae1dSRodney W. Grimes 			if (entry->end < end &&
1506df8bae1dSRodney W. Grimes 			    (entry->next == &map->header ||
1507df8bae1dSRodney W. Grimes 				entry->next->start > entry->end)) {
1508df8bae1dSRodney W. Grimes 				/*
15090d94caffSDavid Greenman 				 * Found one.  Object creation actions do not
15100d94caffSDavid Greenman 				 * need to be undone, but the wired counts
15110d94caffSDavid Greenman 				 * need to be restored.
1512df8bae1dSRodney W. Grimes 				 */
1513df8bae1dSRodney W. Grimes 				while (entry != &map->header && entry->end > start) {
1514df8bae1dSRodney W. Grimes 					entry->wired_count--;
1515df8bae1dSRodney W. Grimes 					entry = entry->prev;
1516df8bae1dSRodney W. Grimes 				}
151747221757SJohn Dyson 				map->timestamp++;
1518df8bae1dSRodney W. Grimes 				vm_map_unlock(map);
1519df8bae1dSRodney W. Grimes 				return (KERN_INVALID_ARGUMENT);
1520df8bae1dSRodney W. Grimes 			}
1521df8bae1dSRodney W. Grimes 			entry = entry->next;
1522df8bae1dSRodney W. Grimes 		}
1523df8bae1dSRodney W. Grimes 
1524df8bae1dSRodney W. Grimes 		/*
1525df8bae1dSRodney W. Grimes 		 * Pass 2.
1526df8bae1dSRodney W. Grimes 		 */
1527df8bae1dSRodney W. Grimes 
1528df8bae1dSRodney W. Grimes 		/*
1529df8bae1dSRodney W. Grimes 		 * HACK HACK HACK HACK
1530df8bae1dSRodney W. Grimes 		 *
153124a1cce3SDavid Greenman 		 * If we are wiring in the kernel map or a submap of it,
153224a1cce3SDavid Greenman 		 * unlock the map to avoid deadlocks.  We trust that the
153324a1cce3SDavid Greenman 		 * kernel is well-behaved, and therefore will not do
153424a1cce3SDavid Greenman 		 * anything destructive to this region of the map while
153524a1cce3SDavid Greenman 		 * we have it unlocked.  We cannot trust user processes
153624a1cce3SDavid Greenman 		 * to do the same.
1537df8bae1dSRodney W. Grimes 		 *
1538df8bae1dSRodney W. Grimes 		 * HACK HACK HACK HACK
1539df8bae1dSRodney W. Grimes 		 */
1540df8bae1dSRodney W. Grimes 		if (vm_map_pmap(map) == kernel_pmap) {
1541df8bae1dSRodney W. Grimes 			vm_map_unlock(map);	/* trust me ... */
15420d94caffSDavid Greenman 		} else {
1543996c772fSJohn Dyson 			vm_map_set_recursive(map);
154403e9c6c1SJohn Dyson 			vm_map_lock_downgrade(map);
1545df8bae1dSRodney W. Grimes 		}
1546df8bae1dSRodney W. Grimes 
1547df8bae1dSRodney W. Grimes 		rv = 0;
1548df8bae1dSRodney W. Grimes 		entry = start_entry;
1549df8bae1dSRodney W. Grimes 		while (entry != &map->header && entry->start < end) {
1550df8bae1dSRodney W. Grimes 			/*
15510d94caffSDavid Greenman 			 * If vm_fault_wire fails for any page we need to undo
15520d94caffSDavid Greenman 			 * what has been done.  We decrement the wiring count
15530d94caffSDavid Greenman 			 * for those pages which have not yet been wired (now)
15540d94caffSDavid Greenman 			 * and unwire those that have (later).
1555df8bae1dSRodney W. Grimes 			 *
1556df8bae1dSRodney W. Grimes 			 * XXX this violates the locking protocol on the map,
1557df8bae1dSRodney W. Grimes 			 * needs to be fixed.
1558df8bae1dSRodney W. Grimes 			 */
1559df8bae1dSRodney W. Grimes 			if (rv)
1560df8bae1dSRodney W. Grimes 				entry->wired_count--;
1561df8bae1dSRodney W. Grimes 			else if (entry->wired_count == 1) {
1562df8bae1dSRodney W. Grimes 				rv = vm_fault_wire(map, entry->start, entry->end);
1563df8bae1dSRodney W. Grimes 				if (rv) {
1564df8bae1dSRodney W. Grimes 					failed = entry->start;
1565df8bae1dSRodney W. Grimes 					entry->wired_count--;
1566df8bae1dSRodney W. Grimes 				}
1567df8bae1dSRodney W. Grimes 			}
1568df8bae1dSRodney W. Grimes 			entry = entry->next;
1569df8bae1dSRodney W. Grimes 		}
1570df8bae1dSRodney W. Grimes 
1571df8bae1dSRodney W. Grimes 		if (vm_map_pmap(map) == kernel_pmap) {
1572df8bae1dSRodney W. Grimes 			vm_map_lock(map);
15730d94caffSDavid Greenman 		} else {
1574996c772fSJohn Dyson 			vm_map_clear_recursive(map);
1575df8bae1dSRodney W. Grimes 		}
1576df8bae1dSRodney W. Grimes 		if (rv) {
1577df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
1578df8bae1dSRodney W. Grimes 			(void) vm_map_pageable(map, start, failed, TRUE);
1579df8bae1dSRodney W. Grimes 			return (rv);
1580df8bae1dSRodney W. Grimes 		}
1581b7b2aac2SJohn Dyson 		vm_map_simplify_entry(map, start_entry);
1582df8bae1dSRodney W. Grimes 	}
1583df8bae1dSRodney W. Grimes 
1584df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1585df8bae1dSRodney W. Grimes 
158647221757SJohn Dyson 	map->timestamp++;
1587df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
1588df8bae1dSRodney W. Grimes }
1589df8bae1dSRodney W. Grimes 
1590df8bae1dSRodney W. Grimes /*
1591df8bae1dSRodney W. Grimes  * vm_map_clean
1592df8bae1dSRodney W. Grimes  *
1593df8bae1dSRodney W. Grimes  * Push any dirty cached pages in the address range to their pager.
1594df8bae1dSRodney W. Grimes  * If syncio is TRUE, dirty pages are written synchronously.
1595df8bae1dSRodney W. Grimes  * If invalidate is TRUE, any cached pages are freed as well.
1596df8bae1dSRodney W. Grimes  *
1597df8bae1dSRodney W. Grimes  * Returns an error if any part of the specified range is not mapped.
1598df8bae1dSRodney W. Grimes  */
1599df8bae1dSRodney W. Grimes int
1600df8bae1dSRodney W. Grimes vm_map_clean(map, start, end, syncio, invalidate)
1601df8bae1dSRodney W. Grimes 	vm_map_t map;
1602df8bae1dSRodney W. Grimes 	vm_offset_t start;
1603df8bae1dSRodney W. Grimes 	vm_offset_t end;
1604df8bae1dSRodney W. Grimes 	boolean_t syncio;
1605df8bae1dSRodney W. Grimes 	boolean_t invalidate;
1606df8bae1dSRodney W. Grimes {
1607c0877f10SJohn Dyson 	vm_map_entry_t current;
1608df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
1609df8bae1dSRodney W. Grimes 	vm_size_t size;
1610df8bae1dSRodney W. Grimes 	vm_object_t object;
1611a316d390SJohn Dyson 	vm_ooffset_t offset;
1612df8bae1dSRodney W. Grimes 
1613df8bae1dSRodney W. Grimes 	vm_map_lock_read(map);
1614df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1615df8bae1dSRodney W. Grimes 	if (!vm_map_lookup_entry(map, start, &entry)) {
1616df8bae1dSRodney W. Grimes 		vm_map_unlock_read(map);
1617df8bae1dSRodney W. Grimes 		return (KERN_INVALID_ADDRESS);
1618df8bae1dSRodney W. Grimes 	}
1619df8bae1dSRodney W. Grimes 	/*
1620df8bae1dSRodney W. Grimes 	 * Make a first pass to check for holes.
1621df8bae1dSRodney W. Grimes 	 */
1622df8bae1dSRodney W. Grimes 	for (current = entry; current->start < end; current = current->next) {
1623afa07f7eSJohn Dyson 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1624df8bae1dSRodney W. Grimes 			vm_map_unlock_read(map);
1625df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ARGUMENT);
1626df8bae1dSRodney W. Grimes 		}
1627df8bae1dSRodney W. Grimes 		if (end > current->end &&
1628df8bae1dSRodney W. Grimes 		    (current->next == &map->header ||
1629df8bae1dSRodney W. Grimes 			current->end != current->next->start)) {
1630df8bae1dSRodney W. Grimes 			vm_map_unlock_read(map);
1631df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ADDRESS);
1632df8bae1dSRodney W. Grimes 		}
1633df8bae1dSRodney W. Grimes 	}
1634df8bae1dSRodney W. Grimes 
1635cf2819ccSJohn Dyson 	if (invalidate)
1636cf2819ccSJohn Dyson 		pmap_remove(vm_map_pmap(map), start, end);
1637df8bae1dSRodney W. Grimes 	/*
1638df8bae1dSRodney W. Grimes 	 * Make a second pass, cleaning/uncaching pages from the indicated
1639df8bae1dSRodney W. Grimes 	 * objects as we go.
1640df8bae1dSRodney W. Grimes 	 */
1641df8bae1dSRodney W. Grimes 	for (current = entry; current->start < end; current = current->next) {
1642df8bae1dSRodney W. Grimes 		offset = current->offset + (start - current->start);
1643df8bae1dSRodney W. Grimes 		size = (end <= current->end ? end : current->end) - start;
1644afa07f7eSJohn Dyson 		if (current->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP)) {
1645c0877f10SJohn Dyson 			vm_map_t smap;
1646df8bae1dSRodney W. Grimes 			vm_map_entry_t tentry;
1647df8bae1dSRodney W. Grimes 			vm_size_t tsize;
1648df8bae1dSRodney W. Grimes 
1649df8bae1dSRodney W. Grimes 			smap = current->object.share_map;
1650df8bae1dSRodney W. Grimes 			vm_map_lock_read(smap);
1651df8bae1dSRodney W. Grimes 			(void) vm_map_lookup_entry(smap, offset, &tentry);
1652df8bae1dSRodney W. Grimes 			tsize = tentry->end - offset;
1653df8bae1dSRodney W. Grimes 			if (tsize < size)
1654df8bae1dSRodney W. Grimes 				size = tsize;
1655df8bae1dSRodney W. Grimes 			object = tentry->object.vm_object;
1656df8bae1dSRodney W. Grimes 			offset = tentry->offset + (offset - tentry->start);
1657df8bae1dSRodney W. Grimes 			vm_map_unlock_read(smap);
1658df8bae1dSRodney W. Grimes 		} else {
1659df8bae1dSRodney W. Grimes 			object = current->object.vm_object;
1660df8bae1dSRodney W. Grimes 		}
16618a02c104SJohn Dyson 		/*
16628a02c104SJohn Dyson 		 * Note that there is absolutely no sense in writing out
16638a02c104SJohn Dyson 		 * anonymous objects, so we track down the vnode object
16648a02c104SJohn Dyson 		 * to write out.
16658a02c104SJohn Dyson 		 * We invalidate (remove) all pages from the address space
16668a02c104SJohn Dyson 		 * anyway, for semantic correctness.
16678a02c104SJohn Dyson 		 */
16688a02c104SJohn Dyson 		while (object->backing_object) {
16698a02c104SJohn Dyson 			object = object->backing_object;
16708a02c104SJohn Dyson 			offset += object->backing_object_offset;
16718a02c104SJohn Dyson 			if (object->size < OFF_TO_IDX( offset + size))
16728a02c104SJohn Dyson 				size = IDX_TO_OFF(object->size) - offset;
16738a02c104SJohn Dyson 		}
167424a1cce3SDavid Greenman 		if (object && (object->type == OBJT_VNODE)) {
1675df8bae1dSRodney W. Grimes 			/*
16760d94caffSDavid Greenman 			 * Flush pages if writing is allowed. XXX should we continue
16770d94caffSDavid Greenman 			 * on an error?
1678f5cf85d4SDavid Greenman 			 *
1679f5cf85d4SDavid Greenman 			 * XXX Doing async I/O and then removing all the pages from
1680f5cf85d4SDavid Greenman 			 *     the object before it completes is probably a very bad
1681f5cf85d4SDavid Greenman 			 *     idea.
1682df8bae1dSRodney W. Grimes 			 */
1683a02051c3SJohn Dyson 			if (current->protection & VM_PROT_WRITE) {
16848f9110f6SJohn Dyson 				int flags;
16852be70f79SJohn Dyson 				if (object->type == OBJT_VNODE)
1686157ac55fSJohn Dyson 					vn_lock(object->handle, LK_EXCLUSIVE | LK_RETRY, curproc);
16878f9110f6SJohn Dyson 				flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
16888f9110f6SJohn Dyson 				flags |= invalidate ? OBJPC_INVAL : 0;
1689a316d390SJohn Dyson 		   	    vm_object_page_clean(object,
1690a316d390SJohn Dyson 					OFF_TO_IDX(offset),
16912be70f79SJohn Dyson 					OFF_TO_IDX(offset + size + PAGE_MASK),
16928f9110f6SJohn Dyson 					flags);
1693cf2819ccSJohn Dyson 				if (invalidate) {
1694cf2819ccSJohn Dyson 					vm_object_pip_wait(object, "objmcl");
1695a316d390SJohn Dyson 					vm_object_page_remove(object,
1696a316d390SJohn Dyson 						OFF_TO_IDX(offset),
16972be70f79SJohn Dyson 						OFF_TO_IDX(offset + size + PAGE_MASK),
1698a316d390SJohn Dyson 						FALSE);
1699cf2819ccSJohn Dyson 				}
17002be70f79SJohn Dyson 				if (object->type == OBJT_VNODE)
17012be70f79SJohn Dyson 					VOP_UNLOCK(object->handle, 0, curproc);
1702bf4bd9bdSDavid Greenman 			}
1703a02051c3SJohn Dyson 		}
1704df8bae1dSRodney W. Grimes 		start += size;
1705df8bae1dSRodney W. Grimes 	}
1706df8bae1dSRodney W. Grimes 
1707df8bae1dSRodney W. Grimes 	vm_map_unlock_read(map);
1708df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
1709df8bae1dSRodney W. Grimes }
1710df8bae1dSRodney W. Grimes 
1711df8bae1dSRodney W. Grimes /*
1712df8bae1dSRodney W. Grimes  *	vm_map_entry_unwire:	[ internal use only ]
1713df8bae1dSRodney W. Grimes  *
1714df8bae1dSRodney W. Grimes  *	Make the region specified by this entry pageable.
1715df8bae1dSRodney W. Grimes  *
1716df8bae1dSRodney W. Grimes  *	The map in question should be locked.
1717df8bae1dSRodney W. Grimes  *	[This is the reason for this routine's existence.]
1718df8bae1dSRodney W. Grimes  */
17190362d7d7SJohn Dyson static void
17200d94caffSDavid Greenman vm_map_entry_unwire(map, entry)
1721df8bae1dSRodney W. Grimes 	vm_map_t map;
1722c0877f10SJohn Dyson 	vm_map_entry_t entry;
1723df8bae1dSRodney W. Grimes {
1724df8bae1dSRodney W. Grimes 	vm_fault_unwire(map, entry->start, entry->end);
1725df8bae1dSRodney W. Grimes 	entry->wired_count = 0;
1726df8bae1dSRodney W. Grimes }
1727df8bae1dSRodney W. Grimes 
1728df8bae1dSRodney W. Grimes /*
1729df8bae1dSRodney W. Grimes  *	vm_map_entry_delete:	[ internal use only ]
1730df8bae1dSRodney W. Grimes  *
1731df8bae1dSRodney W. Grimes  *	Deallocate the given entry from the target map.
1732df8bae1dSRodney W. Grimes  */
17330362d7d7SJohn Dyson static void
17340d94caffSDavid Greenman vm_map_entry_delete(map, entry)
1735c0877f10SJohn Dyson 	vm_map_t map;
1736c0877f10SJohn Dyson 	vm_map_entry_t entry;
1737df8bae1dSRodney W. Grimes {
1738df8bae1dSRodney W. Grimes 	vm_map_entry_unlink(map, entry);
1739df8bae1dSRodney W. Grimes 	map->size -= entry->end - entry->start;
1740df8bae1dSRodney W. Grimes 
17412d8acc0fSJohn Dyson 	if ((entry->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP)) == 0) {
1742df8bae1dSRodney W. Grimes 		vm_object_deallocate(entry->object.vm_object);
1743b5b40fa6SJohn Dyson 	}
1744df8bae1dSRodney W. Grimes 
1745df8bae1dSRodney W. Grimes 	vm_map_entry_dispose(map, entry);
1746df8bae1dSRodney W. Grimes }
1747df8bae1dSRodney W. Grimes 
1748df8bae1dSRodney W. Grimes /*
1749df8bae1dSRodney W. Grimes  *	vm_map_delete:	[ internal use only ]
1750df8bae1dSRodney W. Grimes  *
1751df8bae1dSRodney W. Grimes  *	Deallocates the given address range from the target
1752df8bae1dSRodney W. Grimes  *	map.
1753df8bae1dSRodney W. Grimes  *
1754df8bae1dSRodney W. Grimes  *	When called with a sharing map, removes pages from
1755df8bae1dSRodney W. Grimes  *	that region from all physical maps.
1756df8bae1dSRodney W. Grimes  */
1757df8bae1dSRodney W. Grimes int
1758df8bae1dSRodney W. Grimes vm_map_delete(map, start, end)
1759c0877f10SJohn Dyson 	vm_map_t map;
1760df8bae1dSRodney W. Grimes 	vm_offset_t start;
1761c0877f10SJohn Dyson 	vm_offset_t end;
1762df8bae1dSRodney W. Grimes {
1763cbd8ec09SJohn Dyson 	vm_object_t object;
1764c0877f10SJohn Dyson 	vm_map_entry_t entry;
1765df8bae1dSRodney W. Grimes 	vm_map_entry_t first_entry;
1766df8bae1dSRodney W. Grimes 
1767df8bae1dSRodney W. Grimes 	/*
1768df8bae1dSRodney W. Grimes 	 * Find the start of the region, and clip it
1769df8bae1dSRodney W. Grimes 	 */
1770df8bae1dSRodney W. Grimes 
17712dbea5d2SJohn Dyson 	if (!vm_map_lookup_entry(map, start, &first_entry)) {
1772df8bae1dSRodney W. Grimes 		entry = first_entry->next;
1773cbd8ec09SJohn Dyson 		object = entry->object.vm_object;
1774cbd8ec09SJohn Dyson 		if (object && (object->ref_count == 1) && (object->shadow_count == 0))
1775069e9bc1SDoug Rabson 			vm_object_set_flag(object, OBJ_ONEMAPPING);
17762dbea5d2SJohn Dyson 	} else {
1777df8bae1dSRodney W. Grimes 		entry = first_entry;
1778df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
1779df8bae1dSRodney W. Grimes 		/*
17800d94caffSDavid Greenman 		 * Fix the lookup hint now, rather than each time though the
17810d94caffSDavid Greenman 		 * loop.
1782df8bae1dSRodney W. Grimes 		 */
1783df8bae1dSRodney W. Grimes 		SAVE_HINT(map, entry->prev);
1784df8bae1dSRodney W. Grimes 	}
1785df8bae1dSRodney W. Grimes 
1786df8bae1dSRodney W. Grimes 	/*
1787df8bae1dSRodney W. Grimes 	 * Save the free space hint
1788df8bae1dSRodney W. Grimes 	 */
1789df8bae1dSRodney W. Grimes 
1790b18bfc3dSJohn Dyson 	if (entry == &map->header) {
1791b18bfc3dSJohn Dyson 		map->first_free = &map->header;
17922dbea5d2SJohn Dyson 	} else if (map->first_free->start >= start) {
1793df8bae1dSRodney W. Grimes 		map->first_free = entry->prev;
17942dbea5d2SJohn Dyson 	}
1795df8bae1dSRodney W. Grimes 
1796df8bae1dSRodney W. Grimes 	/*
1797df8bae1dSRodney W. Grimes 	 * Step through all entries in this region
1798df8bae1dSRodney W. Grimes 	 */
1799df8bae1dSRodney W. Grimes 
1800df8bae1dSRodney W. Grimes 	while ((entry != &map->header) && (entry->start < end)) {
1801df8bae1dSRodney W. Grimes 		vm_map_entry_t next;
1802b18bfc3dSJohn Dyson 		vm_offset_t s, e;
1803cbd8ec09SJohn Dyson 		vm_pindex_t offidxstart, offidxend, count;
1804df8bae1dSRodney W. Grimes 
1805df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, entry, end);
1806df8bae1dSRodney W. Grimes 
1807df8bae1dSRodney W. Grimes 		s = entry->start;
1808df8bae1dSRodney W. Grimes 		e = entry->end;
1809c0877f10SJohn Dyson 		next = entry->next;
1810df8bae1dSRodney W. Grimes 
1811cbd8ec09SJohn Dyson 		offidxstart = OFF_TO_IDX(entry->offset);
1812cbd8ec09SJohn Dyson 		count = OFF_TO_IDX(e - s);
1813cbd8ec09SJohn Dyson 		object = entry->object.vm_object;
18142dbea5d2SJohn Dyson 
1815df8bae1dSRodney W. Grimes 		/*
18160d94caffSDavid Greenman 		 * Unwire before removing addresses from the pmap; otherwise,
18170d94caffSDavid Greenman 		 * unwiring will put the entries back in the pmap.
1818df8bae1dSRodney W. Grimes 		 */
1819c0877f10SJohn Dyson 		if (entry->wired_count != 0) {
1820df8bae1dSRodney W. Grimes 			vm_map_entry_unwire(map, entry);
1821c0877f10SJohn Dyson 		}
1822df8bae1dSRodney W. Grimes 
1823cbd8ec09SJohn Dyson 		offidxend = offidxstart + count;
1824df8bae1dSRodney W. Grimes 		/*
18250d94caffSDavid Greenman 		 * If this is a sharing map, we must remove *all* references
18260d94caffSDavid Greenman 		 * to this data, since we can't find all of the physical maps
18270d94caffSDavid Greenman 		 * which are sharing it.
1828df8bae1dSRodney W. Grimes 		 */
1829df8bae1dSRodney W. Grimes 
1830c0877f10SJohn Dyson 		if ((object == kernel_object) || (object == kmem_object)) {
18312dbea5d2SJohn Dyson 			vm_object_page_remove(object, offidxstart, offidxend, FALSE);
1832b18bfc3dSJohn Dyson 		} else if (!map->is_main_map) {
18332dbea5d2SJohn Dyson 			vm_object_pmap_remove(object, offidxstart, offidxend);
1834b18bfc3dSJohn Dyson 		} else {
1835df8bae1dSRodney W. Grimes 			pmap_remove(map->pmap, s, e);
1836c0877f10SJohn Dyson 			if (object &&
183796fb8cf2SJohn Dyson 				((object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) &&
1838c0877f10SJohn Dyson 				((object->type == OBJT_SWAP) || (object->type == OBJT_DEFAULT))) {
18392dbea5d2SJohn Dyson 				vm_object_collapse(object);
18402dbea5d2SJohn Dyson 				vm_object_page_remove(object, offidxstart, offidxend, FALSE);
18412dbea5d2SJohn Dyson 				if (object->type == OBJT_SWAP) {
1842cbd8ec09SJohn Dyson 					swap_pager_freespace(object, offidxstart, count);
18432dbea5d2SJohn Dyson 				}
1844cbd8ec09SJohn Dyson 
1845cbd8ec09SJohn Dyson 				if ((offidxend >= object->size) &&
1846cbd8ec09SJohn Dyson 					(offidxstart < object->size)) {
1847c0877f10SJohn Dyson 						object->size = offidxstart;
1848c0877f10SJohn Dyson 				}
18492dbea5d2SJohn Dyson 			}
1850b18bfc3dSJohn Dyson 		}
1851df8bae1dSRodney W. Grimes 
1852df8bae1dSRodney W. Grimes 		/*
18530d94caffSDavid Greenman 		 * Delete the entry (which may delete the object) only after
18540d94caffSDavid Greenman 		 * removing all pmap entries pointing to its pages.
18550d94caffSDavid Greenman 		 * (Otherwise, its page frames may be reallocated, and any
18560d94caffSDavid Greenman 		 * modify bits will be set in the wrong object!)
1857df8bae1dSRodney W. Grimes 		 */
1858df8bae1dSRodney W. Grimes 		vm_map_entry_delete(map, entry);
1859df8bae1dSRodney W. Grimes 		entry = next;
1860df8bae1dSRodney W. Grimes 	}
1861df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
1862df8bae1dSRodney W. Grimes }
1863df8bae1dSRodney W. Grimes 
1864df8bae1dSRodney W. Grimes /*
1865df8bae1dSRodney W. Grimes  *	vm_map_remove:
1866df8bae1dSRodney W. Grimes  *
1867df8bae1dSRodney W. Grimes  *	Remove the given address range from the target map.
1868df8bae1dSRodney W. Grimes  *	This is the exported form of vm_map_delete.
1869df8bae1dSRodney W. Grimes  */
1870df8bae1dSRodney W. Grimes int
1871df8bae1dSRodney W. Grimes vm_map_remove(map, start, end)
1872c0877f10SJohn Dyson 	vm_map_t map;
1873c0877f10SJohn Dyson 	vm_offset_t start;
1874c0877f10SJohn Dyson 	vm_offset_t end;
1875df8bae1dSRodney W. Grimes {
1876c0877f10SJohn Dyson 	int result, s = 0;
18778d6e8edeSDavid Greenman 
18789579ee64SDavid Greenman 	if (map == kmem_map || map == mb_map)
1879b18bfc3dSJohn Dyson 		s = splvm();
1880df8bae1dSRodney W. Grimes 
1881df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1882df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1883df8bae1dSRodney W. Grimes 	result = vm_map_delete(map, start, end);
1884df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1885df8bae1dSRodney W. Grimes 
18869579ee64SDavid Greenman 	if (map == kmem_map || map == mb_map)
18878d6e8edeSDavid Greenman 		splx(s);
18888d6e8edeSDavid Greenman 
1889df8bae1dSRodney W. Grimes 	return (result);
1890df8bae1dSRodney W. Grimes }
1891df8bae1dSRodney W. Grimes 
1892df8bae1dSRodney W. Grimes /*
1893df8bae1dSRodney W. Grimes  *	vm_map_check_protection:
1894df8bae1dSRodney W. Grimes  *
1895df8bae1dSRodney W. Grimes  *	Assert that the target map allows the specified
1896df8bae1dSRodney W. Grimes  *	privilege on the entire address region given.
1897df8bae1dSRodney W. Grimes  *	The entire region must be allocated.
1898df8bae1dSRodney W. Grimes  */
18990d94caffSDavid Greenman boolean_t
1900b9dcd593SBruce Evans vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
1901b9dcd593SBruce Evans 			vm_prot_t protection)
1902df8bae1dSRodney W. Grimes {
1903c0877f10SJohn Dyson 	vm_map_entry_t entry;
1904df8bae1dSRodney W. Grimes 	vm_map_entry_t tmp_entry;
1905df8bae1dSRodney W. Grimes 
1906df8bae1dSRodney W. Grimes 	if (!vm_map_lookup_entry(map, start, &tmp_entry)) {
1907df8bae1dSRodney W. Grimes 		return (FALSE);
1908df8bae1dSRodney W. Grimes 	}
1909df8bae1dSRodney W. Grimes 	entry = tmp_entry;
1910df8bae1dSRodney W. Grimes 
1911df8bae1dSRodney W. Grimes 	while (start < end) {
1912df8bae1dSRodney W. Grimes 		if (entry == &map->header) {
1913df8bae1dSRodney W. Grimes 			return (FALSE);
1914df8bae1dSRodney W. Grimes 		}
1915df8bae1dSRodney W. Grimes 		/*
1916df8bae1dSRodney W. Grimes 		 * No holes allowed!
1917df8bae1dSRodney W. Grimes 		 */
1918df8bae1dSRodney W. Grimes 
1919df8bae1dSRodney W. Grimes 		if (start < entry->start) {
1920df8bae1dSRodney W. Grimes 			return (FALSE);
1921df8bae1dSRodney W. Grimes 		}
1922df8bae1dSRodney W. Grimes 		/*
1923df8bae1dSRodney W. Grimes 		 * Check protection associated with entry.
1924df8bae1dSRodney W. Grimes 		 */
1925df8bae1dSRodney W. Grimes 
1926df8bae1dSRodney W. Grimes 		if ((entry->protection & protection) != protection) {
1927df8bae1dSRodney W. Grimes 			return (FALSE);
1928df8bae1dSRodney W. Grimes 		}
1929df8bae1dSRodney W. Grimes 		/* go to next entry */
1930df8bae1dSRodney W. Grimes 
1931df8bae1dSRodney W. Grimes 		start = entry->end;
1932df8bae1dSRodney W. Grimes 		entry = entry->next;
1933df8bae1dSRodney W. Grimes 	}
1934df8bae1dSRodney W. Grimes 	return (TRUE);
1935df8bae1dSRodney W. Grimes }
1936df8bae1dSRodney W. Grimes 
193786524867SJohn Dyson /*
193886524867SJohn Dyson  * Split the pages in a map entry into a new object.  This affords
193986524867SJohn Dyson  * easier removal of unused pages, and keeps object inheritance from
194086524867SJohn Dyson  * being a negative impact on memory usage.
194186524867SJohn Dyson  */
1942c0877f10SJohn Dyson static void
1943c0877f10SJohn Dyson vm_map_split(entry)
1944c0877f10SJohn Dyson 	vm_map_entry_t entry;
1945c0877f10SJohn Dyson {
194686524867SJohn Dyson 	vm_page_t m;
1947bd6be915SJohn Dyson 	vm_object_t orig_object, new_object, source;
1948c0877f10SJohn Dyson 	vm_offset_t s, e;
1949c0877f10SJohn Dyson 	vm_pindex_t offidxstart, offidxend, idx;
1950c0877f10SJohn Dyson 	vm_size_t size;
1951c0877f10SJohn Dyson 	vm_ooffset_t offset;
1952c0877f10SJohn Dyson 
1953c0877f10SJohn Dyson 	orig_object = entry->object.vm_object;
1954c0877f10SJohn Dyson 	if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
1955c0877f10SJohn Dyson 		return;
1956c0877f10SJohn Dyson 	if (orig_object->ref_count <= 1)
1957c0877f10SJohn Dyson 		return;
1958c0877f10SJohn Dyson 
1959c0877f10SJohn Dyson 	offset = entry->offset;
1960c0877f10SJohn Dyson 	s = entry->start;
1961c0877f10SJohn Dyson 	e = entry->end;
1962c0877f10SJohn Dyson 
1963c0877f10SJohn Dyson 	offidxstart = OFF_TO_IDX(offset);
1964c0877f10SJohn Dyson 	offidxend = offidxstart + OFF_TO_IDX(e - s);
1965c0877f10SJohn Dyson 	size = offidxend - offidxstart;
1966c0877f10SJohn Dyson 
1967c0877f10SJohn Dyson 	new_object = vm_pager_allocate(orig_object->type,
1968c0877f10SJohn Dyson 		NULL, size, VM_PROT_ALL, 0LL);
1969c0877f10SJohn Dyson 	if (new_object == NULL)
1970c0877f10SJohn Dyson 		return;
1971c0877f10SJohn Dyson 
1972bd6be915SJohn Dyson 	source = orig_object->backing_object;
1973bd6be915SJohn Dyson 	if (source != NULL) {
1974bd6be915SJohn Dyson 		vm_object_reference(source);	/* Referenced by new_object */
1975bd6be915SJohn Dyson 		TAILQ_INSERT_TAIL(&source->shadow_head,
1976bd6be915SJohn Dyson 				  new_object, shadow_list);
1977069e9bc1SDoug Rabson 		vm_object_clear_flag(source, OBJ_ONEMAPPING);
1978bd6be915SJohn Dyson 		new_object->backing_object_offset =
1979bd6be915SJohn Dyson 			orig_object->backing_object_offset + offidxstart;
1980bd6be915SJohn Dyson 		new_object->backing_object = source;
1981bd6be915SJohn Dyson 		source->shadow_count++;
1982bd6be915SJohn Dyson 		source->generation++;
1983bd6be915SJohn Dyson 	}
1984bd6be915SJohn Dyson 
1985c0877f10SJohn Dyson 	for (idx = 0; idx < size; idx++) {
1986c0877f10SJohn Dyson 		vm_page_t m;
1987c0877f10SJohn Dyson 
1988c0877f10SJohn Dyson 	retry:
1989c0877f10SJohn Dyson 		m = vm_page_lookup(orig_object, offidxstart + idx);
1990c0877f10SJohn Dyson 		if (m == NULL)
1991c0877f10SJohn Dyson 			continue;
1992c0877f10SJohn Dyson 		if (m->flags & PG_BUSY) {
1993069e9bc1SDoug Rabson 			PAGE_SET_FLAG(m, PG_WANTED);
1994c0877f10SJohn Dyson 			tsleep(m, PVM, "spltwt", 0);
1995c0877f10SJohn Dyson 			goto retry;
1996c0877f10SJohn Dyson 		}
1997c0877f10SJohn Dyson 
1998069e9bc1SDoug Rabson 		PAGE_SET_FLAG(m, PG_BUSY);
1999c0877f10SJohn Dyson 		vm_page_protect(m, VM_PROT_NONE);
2000c0877f10SJohn Dyson 		vm_page_rename(m, new_object, idx);
2001e493d28aSJohn Dyson 		m->dirty = VM_PAGE_BITS_ALL;
2002069e9bc1SDoug Rabson 		PAGE_SET_FLAG(m, PG_BUSY);
2003c0877f10SJohn Dyson 	}
2004c0877f10SJohn Dyson 
2005c0877f10SJohn Dyson 	if (orig_object->type == OBJT_SWAP) {
2006d474eaaaSDoug Rabson 		vm_object_pip_add(orig_object, 1);
2007c0877f10SJohn Dyson 		/*
2008c0877f10SJohn Dyson 		 * copy orig_object pages into new_object
2009c0877f10SJohn Dyson 		 * and destroy unneeded pages in
2010c0877f10SJohn Dyson 		 * shadow object.
2011c0877f10SJohn Dyson 		 */
2012c0877f10SJohn Dyson 		swap_pager_copy(orig_object, OFF_TO_IDX(orig_object->paging_offset),
2013c0877f10SJohn Dyson 		    new_object, OFF_TO_IDX(new_object->paging_offset),
2014c0877f10SJohn Dyson 			offidxstart, 0);
2015c0877f10SJohn Dyson 		vm_object_pip_wakeup(orig_object);
2016c0877f10SJohn Dyson 	}
2017c0877f10SJohn Dyson 
201886524867SJohn Dyson 	for (idx = 0; idx < size; idx++) {
201986524867SJohn Dyson 		m = vm_page_lookup(new_object, idx);
202086524867SJohn Dyson 		if (m) {
202186524867SJohn Dyson 			PAGE_WAKEUP(m);
202286524867SJohn Dyson 		}
202386524867SJohn Dyson 	}
202486524867SJohn Dyson 
2025c0877f10SJohn Dyson 	entry->object.vm_object = new_object;
2026c0877f10SJohn Dyson 	entry->offset = 0LL;
2027c0877f10SJohn Dyson 	vm_object_deallocate(orig_object);
2028c0877f10SJohn Dyson }
2029c0877f10SJohn Dyson 
2030df8bae1dSRodney W. Grimes /*
2031df8bae1dSRodney W. Grimes  *	vm_map_copy_entry:
2032df8bae1dSRodney W. Grimes  *
2033df8bae1dSRodney W. Grimes  *	Copies the contents of the source entry to the destination
2034df8bae1dSRodney W. Grimes  *	entry.  The entries *must* be aligned properly.
2035df8bae1dSRodney W. Grimes  */
2036f708ef1bSPoul-Henning Kamp static void
20370d94caffSDavid Greenman vm_map_copy_entry(src_map, dst_map, src_entry, dst_entry)
2038df8bae1dSRodney W. Grimes 	vm_map_t src_map, dst_map;
2039c0877f10SJohn Dyson 	vm_map_entry_t src_entry, dst_entry;
2040df8bae1dSRodney W. Grimes {
2041c0877f10SJohn Dyson 	vm_object_t src_object;
2042c0877f10SJohn Dyson 
2043afa07f7eSJohn Dyson 	if ((dst_entry->eflags|src_entry->eflags) &
2044afa07f7eSJohn Dyson 		(MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP))
2045df8bae1dSRodney W. Grimes 		return;
2046df8bae1dSRodney W. Grimes 
2047df8bae1dSRodney W. Grimes 	if (src_entry->wired_count == 0) {
2048df8bae1dSRodney W. Grimes 
2049df8bae1dSRodney W. Grimes 		/*
20500d94caffSDavid Greenman 		 * If the source entry is marked needs_copy, it is already
20510d94caffSDavid Greenman 		 * write-protected.
2052df8bae1dSRodney W. Grimes 		 */
2053afa07f7eSJohn Dyson 		if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
2054df8bae1dSRodney W. Grimes 			pmap_protect(src_map->pmap,
2055df8bae1dSRodney W. Grimes 			    src_entry->start,
2056df8bae1dSRodney W. Grimes 			    src_entry->end,
2057df8bae1dSRodney W. Grimes 			    src_entry->protection & ~VM_PROT_WRITE);
2058df8bae1dSRodney W. Grimes 		}
2059b18bfc3dSJohn Dyson 
2060df8bae1dSRodney W. Grimes 		/*
2061df8bae1dSRodney W. Grimes 		 * Make a copy of the object.
2062df8bae1dSRodney W. Grimes 		 */
2063c0877f10SJohn Dyson 		if (src_object = src_entry->object.vm_object) {
2064c0877f10SJohn Dyson 
2065c0877f10SJohn Dyson 			if ((src_object->handle == NULL) &&
2066c0877f10SJohn Dyson 				(src_object->type == OBJT_DEFAULT ||
2067c0877f10SJohn Dyson 				 src_object->type == OBJT_SWAP)) {
2068c0877f10SJohn Dyson 				vm_object_collapse(src_object);
206996fb8cf2SJohn Dyson 				if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
2070c0877f10SJohn Dyson 					vm_map_split(src_entry);
2071cbd8ec09SJohn Dyson 					src_map->timestamp++;
2072c0877f10SJohn Dyson 					src_object = src_entry->object.vm_object;
2073c0877f10SJohn Dyson 				}
2074c0877f10SJohn Dyson 			}
2075c0877f10SJohn Dyson 
2076c0877f10SJohn Dyson 			vm_object_reference(src_object);
2077069e9bc1SDoug Rabson 			vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
2078c0877f10SJohn Dyson 			dst_entry->object.vm_object = src_object;
2079afa07f7eSJohn Dyson 			src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2080afa07f7eSJohn Dyson 			dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2081b18bfc3dSJohn Dyson 			dst_entry->offset = src_entry->offset;
2082b18bfc3dSJohn Dyson 		} else {
2083b18bfc3dSJohn Dyson 			dst_entry->object.vm_object = NULL;
2084b18bfc3dSJohn Dyson 			dst_entry->offset = 0;
2085b18bfc3dSJohn Dyson 		}
2086df8bae1dSRodney W. Grimes 
2087df8bae1dSRodney W. Grimes 		pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
2088df8bae1dSRodney W. Grimes 		    dst_entry->end - dst_entry->start, src_entry->start);
20890d94caffSDavid Greenman 	} else {
2090df8bae1dSRodney W. Grimes 		/*
2091df8bae1dSRodney W. Grimes 		 * Of course, wired down pages can't be set copy-on-write.
20920d94caffSDavid Greenman 		 * Cause wired pages to be copied into the new map by
20930d94caffSDavid Greenman 		 * simulating faults (the new pages are pageable)
2094df8bae1dSRodney W. Grimes 		 */
2095df8bae1dSRodney W. Grimes 		vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry);
2096df8bae1dSRodney W. Grimes 	}
2097df8bae1dSRodney W. Grimes }
2098df8bae1dSRodney W. Grimes 
2099df8bae1dSRodney W. Grimes /*
2100df8bae1dSRodney W. Grimes  * vmspace_fork:
2101df8bae1dSRodney W. Grimes  * Create a new process vmspace structure and vm_map
2102df8bae1dSRodney W. Grimes  * based on those of an existing process.  The new map
2103df8bae1dSRodney W. Grimes  * is based on the old map, according to the inheritance
2104df8bae1dSRodney W. Grimes  * values on the regions in that map.
2105df8bae1dSRodney W. Grimes  *
2106df8bae1dSRodney W. Grimes  * The source map must not be locked.
2107df8bae1dSRodney W. Grimes  */
2108df8bae1dSRodney W. Grimes struct vmspace *
2109df8bae1dSRodney W. Grimes vmspace_fork(vm1)
2110c0877f10SJohn Dyson 	struct vmspace *vm1;
2111df8bae1dSRodney W. Grimes {
2112c0877f10SJohn Dyson 	struct vmspace *vm2;
2113df8bae1dSRodney W. Grimes 	vm_map_t old_map = &vm1->vm_map;
2114df8bae1dSRodney W. Grimes 	vm_map_t new_map;
2115df8bae1dSRodney W. Grimes 	vm_map_entry_t old_entry;
2116df8bae1dSRodney W. Grimes 	vm_map_entry_t new_entry;
2117df8bae1dSRodney W. Grimes 	pmap_t new_pmap;
2118de5f6a77SJohn Dyson 	vm_object_t object;
2119df8bae1dSRodney W. Grimes 
2120df8bae1dSRodney W. Grimes 	vm_map_lock(old_map);
2121df8bae1dSRodney W. Grimes 
21222d8acc0fSJohn Dyson 	vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
2123df8bae1dSRodney W. Grimes 	bcopy(&vm1->vm_startcopy, &vm2->vm_startcopy,
2124df8bae1dSRodney W. Grimes 	    (caddr_t) (vm1 + 1) - (caddr_t) &vm1->vm_startcopy);
2125df8bae1dSRodney W. Grimes 	new_pmap = &vm2->vm_pmap;	/* XXX */
2126df8bae1dSRodney W. Grimes 	new_map = &vm2->vm_map;	/* XXX */
212747221757SJohn Dyson 	new_map->timestamp = 1;
2128df8bae1dSRodney W. Grimes 
2129df8bae1dSRodney W. Grimes 	old_entry = old_map->header.next;
2130df8bae1dSRodney W. Grimes 
2131df8bae1dSRodney W. Grimes 	while (old_entry != &old_map->header) {
2132afa07f7eSJohn Dyson 		if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2133df8bae1dSRodney W. Grimes 			panic("vm_map_fork: encountered a submap");
2134df8bae1dSRodney W. Grimes 
2135df8bae1dSRodney W. Grimes 		switch (old_entry->inheritance) {
2136df8bae1dSRodney W. Grimes 		case VM_INHERIT_NONE:
2137df8bae1dSRodney W. Grimes 			break;
2138df8bae1dSRodney W. Grimes 
2139df8bae1dSRodney W. Grimes 		case VM_INHERIT_SHARE:
2140df8bae1dSRodney W. Grimes 			/*
2141fed9a903SJohn Dyson 			 * Clone the entry, creating the shared object if necessary.
2142fed9a903SJohn Dyson 			 */
2143fed9a903SJohn Dyson 			object = old_entry->object.vm_object;
2144fed9a903SJohn Dyson 			if (object == NULL) {
2145fed9a903SJohn Dyson 				object = vm_object_allocate(OBJT_DEFAULT,
2146c2e11a03SJohn Dyson 					atop(old_entry->end - old_entry->start));
2147fed9a903SJohn Dyson 				old_entry->object.vm_object = object;
2148fed9a903SJohn Dyson 				old_entry->offset = (vm_offset_t) 0;
21495069bf57SJohn Dyson 			} else if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
21505069bf57SJohn Dyson 				vm_object_shadow(&old_entry->object.vm_object,
21515069bf57SJohn Dyson 					&old_entry->offset,
2152c2e11a03SJohn Dyson 					atop(old_entry->end - old_entry->start));
21535069bf57SJohn Dyson 				old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
21545069bf57SJohn Dyson 				object = old_entry->object.vm_object;
2155fed9a903SJohn Dyson 			}
2156069e9bc1SDoug Rabson 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
2157fed9a903SJohn Dyson 
2158fed9a903SJohn Dyson 			/*
2159df8bae1dSRodney W. Grimes 			 * Clone the entry, referencing the sharing map.
2160df8bae1dSRodney W. Grimes 			 */
2161df8bae1dSRodney W. Grimes 			new_entry = vm_map_entry_create(new_map);
2162df8bae1dSRodney W. Grimes 			*new_entry = *old_entry;
2163df8bae1dSRodney W. Grimes 			new_entry->wired_count = 0;
216495e5e988SJohn Dyson 			vm_object_reference(object);
2165df8bae1dSRodney W. Grimes 
2166df8bae1dSRodney W. Grimes 			/*
21670d94caffSDavid Greenman 			 * Insert the entry into the new map -- we know we're
21680d94caffSDavid Greenman 			 * inserting at the end of the new map.
2169df8bae1dSRodney W. Grimes 			 */
2170df8bae1dSRodney W. Grimes 
2171df8bae1dSRodney W. Grimes 			vm_map_entry_link(new_map, new_map->header.prev,
2172df8bae1dSRodney W. Grimes 			    new_entry);
2173df8bae1dSRodney W. Grimes 
2174df8bae1dSRodney W. Grimes 			/*
2175df8bae1dSRodney W. Grimes 			 * Update the physical map
2176df8bae1dSRodney W. Grimes 			 */
2177df8bae1dSRodney W. Grimes 
2178df8bae1dSRodney W. Grimes 			pmap_copy(new_map->pmap, old_map->pmap,
2179df8bae1dSRodney W. Grimes 			    new_entry->start,
2180df8bae1dSRodney W. Grimes 			    (old_entry->end - old_entry->start),
2181df8bae1dSRodney W. Grimes 			    old_entry->start);
2182df8bae1dSRodney W. Grimes 			break;
2183df8bae1dSRodney W. Grimes 
2184df8bae1dSRodney W. Grimes 		case VM_INHERIT_COPY:
2185df8bae1dSRodney W. Grimes 			/*
2186df8bae1dSRodney W. Grimes 			 * Clone the entry and link into the map.
2187df8bae1dSRodney W. Grimes 			 */
2188df8bae1dSRodney W. Grimes 			new_entry = vm_map_entry_create(new_map);
2189df8bae1dSRodney W. Grimes 			*new_entry = *old_entry;
2190df8bae1dSRodney W. Grimes 			new_entry->wired_count = 0;
2191df8bae1dSRodney W. Grimes 			new_entry->object.vm_object = NULL;
2192afa07f7eSJohn Dyson 			new_entry->eflags &= ~MAP_ENTRY_IS_A_MAP;
2193df8bae1dSRodney W. Grimes 			vm_map_entry_link(new_map, new_map->header.prev,
2194df8bae1dSRodney W. Grimes 			    new_entry);
2195bd7e5f99SJohn Dyson 			vm_map_copy_entry(old_map, new_map, old_entry,
2196bd7e5f99SJohn Dyson 			    new_entry);
2197df8bae1dSRodney W. Grimes 			break;
2198df8bae1dSRodney W. Grimes 		}
2199df8bae1dSRodney W. Grimes 		old_entry = old_entry->next;
2200df8bae1dSRodney W. Grimes 	}
2201df8bae1dSRodney W. Grimes 
2202df8bae1dSRodney W. Grimes 	new_map->size = old_map->size;
2203df8bae1dSRodney W. Grimes 	vm_map_unlock(old_map);
2204cbd8ec09SJohn Dyson 	old_map->timestamp++;
2205df8bae1dSRodney W. Grimes 
2206df8bae1dSRodney W. Grimes 	return (vm2);
2207df8bae1dSRodney W. Grimes }
2208df8bae1dSRodney W. Grimes 
2209df8bae1dSRodney W. Grimes /*
22105856e12eSJohn Dyson  * Unshare the specified VM space for exec.  If other processes are
22115856e12eSJohn Dyson  * mapped to it, then create a new one.  The new vmspace is null.
22125856e12eSJohn Dyson  */
22135856e12eSJohn Dyson 
22145856e12eSJohn Dyson void
22155856e12eSJohn Dyson vmspace_exec(struct proc *p) {
22165856e12eSJohn Dyson 	struct vmspace *oldvmspace = p->p_vmspace;
22175856e12eSJohn Dyson 	struct vmspace *newvmspace;
22185856e12eSJohn Dyson 	vm_map_t map = &p->p_vmspace->vm_map;
22195856e12eSJohn Dyson 
22202d8acc0fSJohn Dyson 	newvmspace = vmspace_alloc(map->min_offset, map->max_offset);
22215856e12eSJohn Dyson 	bcopy(&oldvmspace->vm_startcopy, &newvmspace->vm_startcopy,
22225856e12eSJohn Dyson 	    (caddr_t) (newvmspace + 1) - (caddr_t) &newvmspace->vm_startcopy);
22235856e12eSJohn Dyson 	/*
22245856e12eSJohn Dyson 	 * This code is written like this for prototype purposes.  The
22255856e12eSJohn Dyson 	 * goal is to avoid running down the vmspace here, but let the
22265856e12eSJohn Dyson 	 * other process's that are still using the vmspace to finally
22275856e12eSJohn Dyson 	 * run it down.  Even though there is little or no chance of blocking
22285856e12eSJohn Dyson 	 * here, it is a good idea to keep this form for future mods.
22295856e12eSJohn Dyson 	 */
22305856e12eSJohn Dyson 	vmspace_free(oldvmspace);
22315856e12eSJohn Dyson 	p->p_vmspace = newvmspace;
22325856e12eSJohn Dyson 	if (p == curproc)
22335856e12eSJohn Dyson 		pmap_activate(p);
22345856e12eSJohn Dyson }
22355856e12eSJohn Dyson 
22365856e12eSJohn Dyson /*
22375856e12eSJohn Dyson  * Unshare the specified VM space for forcing COW.  This
22385856e12eSJohn Dyson  * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
22395856e12eSJohn Dyson  */
22405856e12eSJohn Dyson 
22415856e12eSJohn Dyson void
22425856e12eSJohn Dyson vmspace_unshare(struct proc *p) {
22435856e12eSJohn Dyson 	struct vmspace *oldvmspace = p->p_vmspace;
22445856e12eSJohn Dyson 	struct vmspace *newvmspace;
22455856e12eSJohn Dyson 
22465856e12eSJohn Dyson 	if (oldvmspace->vm_refcnt == 1)
22475856e12eSJohn Dyson 		return;
22485856e12eSJohn Dyson 	newvmspace = vmspace_fork(oldvmspace);
22495856e12eSJohn Dyson 	vmspace_free(oldvmspace);
22505856e12eSJohn Dyson 	p->p_vmspace = newvmspace;
22515856e12eSJohn Dyson 	if (p == curproc)
22525856e12eSJohn Dyson 		pmap_activate(p);
22535856e12eSJohn Dyson }
22545856e12eSJohn Dyson 
22555856e12eSJohn Dyson 
22565856e12eSJohn Dyson /*
2257df8bae1dSRodney W. Grimes  *	vm_map_lookup:
2258df8bae1dSRodney W. Grimes  *
2259df8bae1dSRodney W. Grimes  *	Finds the VM object, offset, and
2260df8bae1dSRodney W. Grimes  *	protection for a given virtual address in the
2261df8bae1dSRodney W. Grimes  *	specified map, assuming a page fault of the
2262df8bae1dSRodney W. Grimes  *	type specified.
2263df8bae1dSRodney W. Grimes  *
2264df8bae1dSRodney W. Grimes  *	Leaves the map in question locked for read; return
2265df8bae1dSRodney W. Grimes  *	values are guaranteed until a vm_map_lookup_done
2266df8bae1dSRodney W. Grimes  *	call is performed.  Note that the map argument
2267df8bae1dSRodney W. Grimes  *	is in/out; the returned map must be used in
2268df8bae1dSRodney W. Grimes  *	the call to vm_map_lookup_done.
2269df8bae1dSRodney W. Grimes  *
2270df8bae1dSRodney W. Grimes  *	A handle (out_entry) is returned for use in
2271df8bae1dSRodney W. Grimes  *	vm_map_lookup_done, to make that fast.
2272df8bae1dSRodney W. Grimes  *
2273df8bae1dSRodney W. Grimes  *	If a lookup is requested with "write protection"
2274df8bae1dSRodney W. Grimes  *	specified, the map may be changed to perform virtual
2275df8bae1dSRodney W. Grimes  *	copying operations, although the data referenced will
2276df8bae1dSRodney W. Grimes  *	remain the same.
2277df8bae1dSRodney W. Grimes  */
2278df8bae1dSRodney W. Grimes int
2279b9dcd593SBruce Evans vm_map_lookup(vm_map_t *var_map,		/* IN/OUT */
2280b9dcd593SBruce Evans 	      vm_offset_t vaddr,
228147221757SJohn Dyson 	      vm_prot_t fault_typea,
2282b9dcd593SBruce Evans 	      vm_map_entry_t *out_entry,	/* OUT */
2283b9dcd593SBruce Evans 	      vm_object_t *object,		/* OUT */
2284b9dcd593SBruce Evans 	      vm_pindex_t *pindex,		/* OUT */
2285b9dcd593SBruce Evans 	      vm_prot_t *out_prot,		/* OUT */
22862d8acc0fSJohn Dyson 	      boolean_t *wired)			/* OUT */
2287df8bae1dSRodney W. Grimes {
2288df8bae1dSRodney W. Grimes 	vm_map_t share_map;
2289df8bae1dSRodney W. Grimes 	vm_offset_t share_offset;
2290c0877f10SJohn Dyson 	vm_map_entry_t entry;
2291c0877f10SJohn Dyson 	vm_map_t map = *var_map;
2292c0877f10SJohn Dyson 	vm_prot_t prot;
2293c0877f10SJohn Dyson 	boolean_t su;
229447221757SJohn Dyson 	vm_prot_t fault_type = fault_typea;
2295df8bae1dSRodney W. Grimes 
2296df8bae1dSRodney W. Grimes RetryLookup:;
2297df8bae1dSRodney W. Grimes 
2298df8bae1dSRodney W. Grimes 	/*
2299df8bae1dSRodney W. Grimes 	 * Lookup the faulting address.
2300df8bae1dSRodney W. Grimes 	 */
2301df8bae1dSRodney W. Grimes 
2302df8bae1dSRodney W. Grimes 	vm_map_lock_read(map);
2303df8bae1dSRodney W. Grimes 
2304df8bae1dSRodney W. Grimes #define	RETURN(why) \
2305df8bae1dSRodney W. Grimes 		{ \
2306df8bae1dSRodney W. Grimes 		vm_map_unlock_read(map); \
2307df8bae1dSRodney W. Grimes 		return(why); \
2308df8bae1dSRodney W. Grimes 		}
2309df8bae1dSRodney W. Grimes 
2310df8bae1dSRodney W. Grimes 	/*
23110d94caffSDavid Greenman 	 * If the map has an interesting hint, try it before calling full
23120d94caffSDavid Greenman 	 * blown lookup routine.
2313df8bae1dSRodney W. Grimes 	 */
2314df8bae1dSRodney W. Grimes 
2315df8bae1dSRodney W. Grimes 	entry = map->hint;
2316df8bae1dSRodney W. Grimes 
2317df8bae1dSRodney W. Grimes 	*out_entry = entry;
2318df8bae1dSRodney W. Grimes 
2319df8bae1dSRodney W. Grimes 	if ((entry == &map->header) ||
2320df8bae1dSRodney W. Grimes 	    (vaddr < entry->start) || (vaddr >= entry->end)) {
2321df8bae1dSRodney W. Grimes 		vm_map_entry_t tmp_entry;
2322df8bae1dSRodney W. Grimes 
2323df8bae1dSRodney W. Grimes 		/*
23240d94caffSDavid Greenman 		 * Entry was either not a valid hint, or the vaddr was not
23250d94caffSDavid Greenman 		 * contained in the entry, so do a full lookup.
2326df8bae1dSRodney W. Grimes 		 */
2327df8bae1dSRodney W. Grimes 		if (!vm_map_lookup_entry(map, vaddr, &tmp_entry))
2328df8bae1dSRodney W. Grimes 			RETURN(KERN_INVALID_ADDRESS);
2329df8bae1dSRodney W. Grimes 
2330df8bae1dSRodney W. Grimes 		entry = tmp_entry;
2331df8bae1dSRodney W. Grimes 		*out_entry = entry;
2332df8bae1dSRodney W. Grimes 	}
2333b7b2aac2SJohn Dyson 
2334df8bae1dSRodney W. Grimes 	/*
2335df8bae1dSRodney W. Grimes 	 * Handle submaps.
2336df8bae1dSRodney W. Grimes 	 */
2337df8bae1dSRodney W. Grimes 
2338afa07f7eSJohn Dyson 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
2339df8bae1dSRodney W. Grimes 		vm_map_t old_map = map;
2340df8bae1dSRodney W. Grimes 
2341df8bae1dSRodney W. Grimes 		*var_map = map = entry->object.sub_map;
2342df8bae1dSRodney W. Grimes 		vm_map_unlock_read(old_map);
2343df8bae1dSRodney W. Grimes 		goto RetryLookup;
2344df8bae1dSRodney W. Grimes 	}
2345a04c970aSJohn Dyson 
2346df8bae1dSRodney W. Grimes 	/*
23470d94caffSDavid Greenman 	 * Check whether this task is allowed to have this page.
2348a04c970aSJohn Dyson 	 * Note the special case for MAP_ENTRY_COW
2349a04c970aSJohn Dyson 	 * pages with an override.  This is to implement a forced
2350a04c970aSJohn Dyson 	 * COW for debuggers.
2351df8bae1dSRodney W. Grimes 	 */
2352df8bae1dSRodney W. Grimes 
2353480ba2f5SJohn Dyson 	if (fault_type & VM_PROT_OVERRIDE_WRITE)
2354480ba2f5SJohn Dyson 		prot = entry->max_protection;
2355480ba2f5SJohn Dyson 	else
2356df8bae1dSRodney W. Grimes 		prot = entry->protection;
235747221757SJohn Dyson 
235847221757SJohn Dyson 	fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
235947221757SJohn Dyson 	if ((fault_type & prot) != fault_type) {
236047221757SJohn Dyson 			RETURN(KERN_PROTECTION_FAILURE);
236147221757SJohn Dyson 	}
236247221757SJohn Dyson 
2363480ba2f5SJohn Dyson 	if (entry->wired_count && (fault_type & VM_PROT_WRITE) &&
236447221757SJohn Dyson 			(entry->eflags & MAP_ENTRY_COW) &&
236547221757SJohn Dyson 			(fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
2366df8bae1dSRodney W. Grimes 			RETURN(KERN_PROTECTION_FAILURE);
2367a04c970aSJohn Dyson 	}
2368df8bae1dSRodney W. Grimes 
2369df8bae1dSRodney W. Grimes 	/*
23700d94caffSDavid Greenman 	 * If this page is not pageable, we have to get it for all possible
23710d94caffSDavid Greenman 	 * accesses.
2372df8bae1dSRodney W. Grimes 	 */
2373df8bae1dSRodney W. Grimes 
237405f0fdd2SPoul-Henning Kamp 	*wired = (entry->wired_count != 0);
237505f0fdd2SPoul-Henning Kamp 	if (*wired)
2376df8bae1dSRodney W. Grimes 		prot = fault_type = entry->protection;
2377df8bae1dSRodney W. Grimes 
2378df8bae1dSRodney W. Grimes 	/*
23790d94caffSDavid Greenman 	 * If we don't already have a VM object, track it down.
2380df8bae1dSRodney W. Grimes 	 */
2381df8bae1dSRodney W. Grimes 
2382afa07f7eSJohn Dyson 	su = (entry->eflags & MAP_ENTRY_IS_A_MAP) == 0;
238305f0fdd2SPoul-Henning Kamp 	if (su) {
2384df8bae1dSRodney W. Grimes 		share_map = map;
2385df8bae1dSRodney W. Grimes 		share_offset = vaddr;
23860d94caffSDavid Greenman 	} else {
2387df8bae1dSRodney W. Grimes 		vm_map_entry_t share_entry;
2388df8bae1dSRodney W. Grimes 
2389df8bae1dSRodney W. Grimes 		/*
2390df8bae1dSRodney W. Grimes 		 * Compute the sharing map, and offset into it.
2391df8bae1dSRodney W. Grimes 		 */
2392df8bae1dSRodney W. Grimes 
2393df8bae1dSRodney W. Grimes 		share_map = entry->object.share_map;
2394df8bae1dSRodney W. Grimes 		share_offset = (vaddr - entry->start) + entry->offset;
2395df8bae1dSRodney W. Grimes 
2396df8bae1dSRodney W. Grimes 		/*
2397df8bae1dSRodney W. Grimes 		 * Look for the backing store object and offset
2398df8bae1dSRodney W. Grimes 		 */
2399df8bae1dSRodney W. Grimes 
2400df8bae1dSRodney W. Grimes 		vm_map_lock_read(share_map);
2401df8bae1dSRodney W. Grimes 
2402df8bae1dSRodney W. Grimes 		if (!vm_map_lookup_entry(share_map, share_offset,
2403df8bae1dSRodney W. Grimes 			&share_entry)) {
2404df8bae1dSRodney W. Grimes 			vm_map_unlock_read(share_map);
2405df8bae1dSRodney W. Grimes 			RETURN(KERN_INVALID_ADDRESS);
2406df8bae1dSRodney W. Grimes 		}
2407df8bae1dSRodney W. Grimes 		entry = share_entry;
2408df8bae1dSRodney W. Grimes 	}
2409df8bae1dSRodney W. Grimes 
2410df8bae1dSRodney W. Grimes 	/*
2411df8bae1dSRodney W. Grimes 	 * If the entry was copy-on-write, we either ...
2412df8bae1dSRodney W. Grimes 	 */
2413df8bae1dSRodney W. Grimes 
2414afa07f7eSJohn Dyson 	if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
2415df8bae1dSRodney W. Grimes 		/*
24160d94caffSDavid Greenman 		 * If we want to write the page, we may as well handle that
24170d94caffSDavid Greenman 		 * now since we've got the sharing map locked.
2418df8bae1dSRodney W. Grimes 		 *
24190d94caffSDavid Greenman 		 * If we don't need to write the page, we just demote the
24200d94caffSDavid Greenman 		 * permissions allowed.
2421df8bae1dSRodney W. Grimes 		 */
2422df8bae1dSRodney W. Grimes 
2423df8bae1dSRodney W. Grimes 		if (fault_type & VM_PROT_WRITE) {
2424df8bae1dSRodney W. Grimes 			/*
24250d94caffSDavid Greenman 			 * Make a new object, and place it in the object
24260d94caffSDavid Greenman 			 * chain.  Note that no new references have appeared
24270d94caffSDavid Greenman 			 * -- one just moved from the share map to the new
24280d94caffSDavid Greenman 			 * object.
2429df8bae1dSRodney W. Grimes 			 */
2430df8bae1dSRodney W. Grimes 
243103e9c6c1SJohn Dyson 			if (vm_map_lock_upgrade(share_map)) {
2432df8bae1dSRodney W. Grimes 				if (share_map != map)
2433df8bae1dSRodney W. Grimes 					vm_map_unlock_read(map);
2434dbc806e7SJohn Dyson 
2435df8bae1dSRodney W. Grimes 				goto RetryLookup;
2436df8bae1dSRodney W. Grimes 			}
2437df8bae1dSRodney W. Grimes 			vm_object_shadow(
2438df8bae1dSRodney W. Grimes 			    &entry->object.vm_object,
2439df8bae1dSRodney W. Grimes 			    &entry->offset,
2440c2e11a03SJohn Dyson 			    atop(entry->end - entry->start));
2441df8bae1dSRodney W. Grimes 
2442afa07f7eSJohn Dyson 			entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
244303e9c6c1SJohn Dyson 			vm_map_lock_downgrade(share_map);
24440d94caffSDavid Greenman 		} else {
2445df8bae1dSRodney W. Grimes 			/*
24460d94caffSDavid Greenman 			 * We're attempting to read a copy-on-write page --
24470d94caffSDavid Greenman 			 * don't allow writes.
2448df8bae1dSRodney W. Grimes 			 */
2449df8bae1dSRodney W. Grimes 
24502d8acc0fSJohn Dyson 			prot &= ~VM_PROT_WRITE;
2451df8bae1dSRodney W. Grimes 		}
2452df8bae1dSRodney W. Grimes 	}
24532d8acc0fSJohn Dyson 
2454df8bae1dSRodney W. Grimes 	/*
2455df8bae1dSRodney W. Grimes 	 * Create an object if necessary.
2456df8bae1dSRodney W. Grimes 	 */
2457df8bae1dSRodney W. Grimes 	if (entry->object.vm_object == NULL) {
2458df8bae1dSRodney W. Grimes 
245903e9c6c1SJohn Dyson 		if (vm_map_lock_upgrade(share_map)) {
2460df8bae1dSRodney W. Grimes 			if (share_map != map)
2461df8bae1dSRodney W. Grimes 				vm_map_unlock_read(map);
2462df8bae1dSRodney W. Grimes 			goto RetryLookup;
2463df8bae1dSRodney W. Grimes 		}
246424a1cce3SDavid Greenman 		entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
2465c2e11a03SJohn Dyson 		    atop(entry->end - entry->start));
2466df8bae1dSRodney W. Grimes 		entry->offset = 0;
246703e9c6c1SJohn Dyson 		vm_map_lock_downgrade(share_map);
2468df8bae1dSRodney W. Grimes 	}
2469b5b40fa6SJohn Dyson 
2470925a3a41SJohn Dyson 	if (entry->object.vm_object->type == OBJT_DEFAULT)
2471b5b40fa6SJohn Dyson 		default_pager_convert_to_swapq(entry->object.vm_object);
2472df8bae1dSRodney W. Grimes 	/*
24730d94caffSDavid Greenman 	 * Return the object/offset from this entry.  If the entry was
24740d94caffSDavid Greenman 	 * copy-on-write or empty, it has been fixed up.
2475df8bae1dSRodney W. Grimes 	 */
2476df8bae1dSRodney W. Grimes 
2477a316d390SJohn Dyson 	*pindex = OFF_TO_IDX((share_offset - entry->start) + entry->offset);
2478df8bae1dSRodney W. Grimes 	*object = entry->object.vm_object;
2479df8bae1dSRodney W. Grimes 
2480df8bae1dSRodney W. Grimes 	/*
2481df8bae1dSRodney W. Grimes 	 * Return whether this is the only map sharing this data.
2482df8bae1dSRodney W. Grimes 	 */
2483df8bae1dSRodney W. Grimes 
2484df8bae1dSRodney W. Grimes 	*out_prot = prot;
2485df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
2486df8bae1dSRodney W. Grimes 
2487df8bae1dSRodney W. Grimes #undef	RETURN
2488df8bae1dSRodney W. Grimes }
2489df8bae1dSRodney W. Grimes 
2490df8bae1dSRodney W. Grimes /*
2491df8bae1dSRodney W. Grimes  *	vm_map_lookup_done:
2492df8bae1dSRodney W. Grimes  *
2493df8bae1dSRodney W. Grimes  *	Releases locks acquired by a vm_map_lookup
2494df8bae1dSRodney W. Grimes  *	(according to the handle returned by that lookup).
2495df8bae1dSRodney W. Grimes  */
2496df8bae1dSRodney W. Grimes 
24970d94caffSDavid Greenman void
24980d94caffSDavid Greenman vm_map_lookup_done(map, entry)
2499c0877f10SJohn Dyson 	vm_map_t map;
2500df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
2501df8bae1dSRodney W. Grimes {
2502df8bae1dSRodney W. Grimes 	/*
2503df8bae1dSRodney W. Grimes 	 * If this entry references a map, unlock it first.
2504df8bae1dSRodney W. Grimes 	 */
2505df8bae1dSRodney W. Grimes 
2506afa07f7eSJohn Dyson 	if (entry->eflags & MAP_ENTRY_IS_A_MAP)
2507df8bae1dSRodney W. Grimes 		vm_map_unlock_read(entry->object.share_map);
2508df8bae1dSRodney W. Grimes 
2509df8bae1dSRodney W. Grimes 	/*
2510df8bae1dSRodney W. Grimes 	 * Unlock the main-level map
2511df8bae1dSRodney W. Grimes 	 */
2512df8bae1dSRodney W. Grimes 
2513df8bae1dSRodney W. Grimes 	vm_map_unlock_read(map);
2514df8bae1dSRodney W. Grimes }
2515df8bae1dSRodney W. Grimes 
25161efb74fbSJohn Dyson /*
25171efb74fbSJohn Dyson  * Implement uiomove with VM operations.  This handles (and collateral changes)
25181efb74fbSJohn Dyson  * support every combination of source object modification, and COW type
25191efb74fbSJohn Dyson  * operations.
25201efb74fbSJohn Dyson  */
25211efb74fbSJohn Dyson int
252247221757SJohn Dyson vm_uiomove(mapa, srcobject, cp, cnta, uaddra, npages)
25231efb74fbSJohn Dyson 	vm_map_t mapa;
25241efb74fbSJohn Dyson 	vm_object_t srcobject;
25251efb74fbSJohn Dyson 	off_t cp;
252647221757SJohn Dyson 	int cnta;
25271efb74fbSJohn Dyson 	vm_offset_t uaddra;
252895e5e988SJohn Dyson 	int *npages;
25291efb74fbSJohn Dyson {
25301efb74fbSJohn Dyson 	vm_map_t map;
253147221757SJohn Dyson 	vm_object_t first_object, oldobject, object;
25322d8acc0fSJohn Dyson 	vm_map_entry_t entry;
25331efb74fbSJohn Dyson 	vm_prot_t prot;
25342d8acc0fSJohn Dyson 	boolean_t wired;
25351efb74fbSJohn Dyson 	int tcnt, rv;
25362d8acc0fSJohn Dyson 	vm_offset_t uaddr, start, end, tend;
25371efb74fbSJohn Dyson 	vm_pindex_t first_pindex, osize, oindex;
25381efb74fbSJohn Dyson 	off_t ooffset;
253947221757SJohn Dyson 	int cnt;
25401efb74fbSJohn Dyson 
254195e5e988SJohn Dyson 	if (npages)
254295e5e988SJohn Dyson 		*npages = 0;
254395e5e988SJohn Dyson 
254447221757SJohn Dyson 	cnt = cnta;
25452d8acc0fSJohn Dyson 	uaddr = uaddra;
25462d8acc0fSJohn Dyson 
25471efb74fbSJohn Dyson 	while (cnt > 0) {
25481efb74fbSJohn Dyson 		map = mapa;
25491efb74fbSJohn Dyson 
25501efb74fbSJohn Dyson 		if ((vm_map_lookup(&map, uaddr,
25512d8acc0fSJohn Dyson 			VM_PROT_READ, &entry, &first_object,
25522d8acc0fSJohn Dyson 			&first_pindex, &prot, &wired)) != KERN_SUCCESS) {
25531efb74fbSJohn Dyson 			return EFAULT;
25541efb74fbSJohn Dyson 		}
25551efb74fbSJohn Dyson 
25562d8acc0fSJohn Dyson 		vm_map_clip_start(map, entry, uaddr);
25571efb74fbSJohn Dyson 
25581efb74fbSJohn Dyson 		tcnt = cnt;
25592d8acc0fSJohn Dyson 		tend = uaddr + tcnt;
25602d8acc0fSJohn Dyson 		if (tend > entry->end) {
25612d8acc0fSJohn Dyson 			tcnt = entry->end - uaddr;
25622d8acc0fSJohn Dyson 			tend = entry->end;
25632d8acc0fSJohn Dyson 		}
25641efb74fbSJohn Dyson 
25652d8acc0fSJohn Dyson 		vm_map_clip_end(map, entry, tend);
25661efb74fbSJohn Dyson 
25672d8acc0fSJohn Dyson 		start = entry->start;
25682d8acc0fSJohn Dyson 		end = entry->end;
25691efb74fbSJohn Dyson 
2570c2e11a03SJohn Dyson 		osize = atop(tcnt);
257195e5e988SJohn Dyson 
2572925a3a41SJohn Dyson 		oindex = OFF_TO_IDX(cp);
257395e5e988SJohn Dyson 		if (npages) {
2574925a3a41SJohn Dyson 			vm_pindex_t idx;
257595e5e988SJohn Dyson 			for (idx = 0; idx < osize; idx++) {
257695e5e988SJohn Dyson 				vm_page_t m;
2577925a3a41SJohn Dyson 				if ((m = vm_page_lookup(srcobject, oindex + idx)) == NULL) {
25782d8acc0fSJohn Dyson 					vm_map_lookup_done(map, entry);
257995e5e988SJohn Dyson 					return 0;
258095e5e988SJohn Dyson 				}
2581925a3a41SJohn Dyson 				if ((m->flags & PG_BUSY) ||
258295e5e988SJohn Dyson 					((m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL)) {
25832d8acc0fSJohn Dyson 					vm_map_lookup_done(map, entry);
258495e5e988SJohn Dyson 					return 0;
258595e5e988SJohn Dyson 				}
258695e5e988SJohn Dyson 			}
258795e5e988SJohn Dyson 		}
258895e5e988SJohn Dyson 
25891efb74fbSJohn Dyson /*
25901efb74fbSJohn Dyson  * If we are changing an existing map entry, just redirect
25911efb74fbSJohn Dyson  * the object, and change mappings.
25921efb74fbSJohn Dyson  */
25932d8acc0fSJohn Dyson 		if ((first_object->type == OBJT_VNODE) &&
25942d8acc0fSJohn Dyson 			((oldobject = entry->object.vm_object) == first_object)) {
25952d8acc0fSJohn Dyson 
25962d8acc0fSJohn Dyson 			if ((entry->offset != cp) || (oldobject != srcobject)) {
25972d8acc0fSJohn Dyson 				/*
25982d8acc0fSJohn Dyson    				* Remove old window into the file
25992d8acc0fSJohn Dyson    				*/
26002d8acc0fSJohn Dyson 				pmap_remove (map->pmap, uaddr, tend);
26012d8acc0fSJohn Dyson 
26022d8acc0fSJohn Dyson 				/*
26032d8acc0fSJohn Dyson    				* Force copy on write for mmaped regions
26042d8acc0fSJohn Dyson    				*/
26052d8acc0fSJohn Dyson 				vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
26062d8acc0fSJohn Dyson 
26072d8acc0fSJohn Dyson 				/*
26082d8acc0fSJohn Dyson    				* Point the object appropriately
26092d8acc0fSJohn Dyson    				*/
26102d8acc0fSJohn Dyson 				if (oldobject != srcobject) {
26112d8acc0fSJohn Dyson 
26122d8acc0fSJohn Dyson 				/*
26132d8acc0fSJohn Dyson    				* Set the object optimization hint flag
26142d8acc0fSJohn Dyson    				*/
2615069e9bc1SDoug Rabson 					vm_object_set_flag(srcobject, OBJ_OPT);
26162d8acc0fSJohn Dyson 					vm_object_reference(srcobject);
26172d8acc0fSJohn Dyson 					entry->object.vm_object = srcobject;
26182d8acc0fSJohn Dyson 
26192d8acc0fSJohn Dyson 					if (oldobject) {
26202d8acc0fSJohn Dyson 						vm_object_deallocate(oldobject);
26212d8acc0fSJohn Dyson 					}
26222d8acc0fSJohn Dyson 				}
26232d8acc0fSJohn Dyson 
26242d8acc0fSJohn Dyson 				entry->offset = cp;
26252d8acc0fSJohn Dyson 				map->timestamp++;
26262d8acc0fSJohn Dyson 			} else {
26272d8acc0fSJohn Dyson 				pmap_remove (map->pmap, uaddr, tend);
26282d8acc0fSJohn Dyson 			}
26292d8acc0fSJohn Dyson 
26302d8acc0fSJohn Dyson 		} else if ((first_object->ref_count == 1) &&
2631925a3a41SJohn Dyson 			(first_object->size == osize) &&
263247221757SJohn Dyson 			((first_object->type == OBJT_DEFAULT) ||
263347221757SJohn Dyson 				(first_object->type == OBJT_SWAP)) ) {
2634925a3a41SJohn Dyson 
2635925a3a41SJohn Dyson 			oldobject = first_object->backing_object;
2636925a3a41SJohn Dyson 
2637925a3a41SJohn Dyson 			if ((first_object->backing_object_offset != cp) ||
2638925a3a41SJohn Dyson 				(oldobject != srcobject)) {
2639925a3a41SJohn Dyson 				/*
2640925a3a41SJohn Dyson    				* Remove old window into the file
2641925a3a41SJohn Dyson    				*/
26422d8acc0fSJohn Dyson 				pmap_remove (map->pmap, uaddr, tend);
2643925a3a41SJohn Dyson 
2644925a3a41SJohn Dyson 				/*
264547221757SJohn Dyson 				 * Remove unneeded old pages
264647221757SJohn Dyson 				 */
264747221757SJohn Dyson 				if (first_object->resident_page_count) {
264847221757SJohn Dyson 					vm_object_page_remove (first_object, 0, 0, 0);
264947221757SJohn Dyson 				}
265047221757SJohn Dyson 
265147221757SJohn Dyson 				/*
265247221757SJohn Dyson 				 * Invalidate swap space
265347221757SJohn Dyson 				 */
265447221757SJohn Dyson 				if (first_object->type == OBJT_SWAP) {
265547221757SJohn Dyson 					swap_pager_freespace(first_object,
265647221757SJohn Dyson 						OFF_TO_IDX(first_object->paging_offset),
265747221757SJohn Dyson 						first_object->size);
265847221757SJohn Dyson 				}
265947221757SJohn Dyson 
266047221757SJohn Dyson 				/*
2661925a3a41SJohn Dyson    				* Force copy on write for mmaped regions
2662925a3a41SJohn Dyson    				*/
266347221757SJohn Dyson 				vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
26641efb74fbSJohn Dyson 
26651efb74fbSJohn Dyson 				/*
26661efb74fbSJohn Dyson    				* Point the object appropriately
26671efb74fbSJohn Dyson    				*/
2668925a3a41SJohn Dyson 				if (oldobject != srcobject) {
266947221757SJohn Dyson 
2670925a3a41SJohn Dyson 				/*
2671925a3a41SJohn Dyson    				* Set the object optimization hint flag
2672925a3a41SJohn Dyson    				*/
2673069e9bc1SDoug Rabson 					vm_object_set_flag(srcobject, OBJ_OPT);
2674925a3a41SJohn Dyson 					vm_object_reference(srcobject);
2675925a3a41SJohn Dyson 
2676925a3a41SJohn Dyson 					if (oldobject) {
2677925a3a41SJohn Dyson 						TAILQ_REMOVE(&oldobject->shadow_head,
2678925a3a41SJohn Dyson 							first_object, shadow_list);
2679925a3a41SJohn Dyson 						oldobject->shadow_count--;
2680925a3a41SJohn Dyson 						vm_object_deallocate(oldobject);
2681925a3a41SJohn Dyson 					}
2682925a3a41SJohn Dyson 
2683925a3a41SJohn Dyson 					TAILQ_INSERT_TAIL(&srcobject->shadow_head,
2684925a3a41SJohn Dyson 						first_object, shadow_list);
2685925a3a41SJohn Dyson 					srcobject->shadow_count++;
2686925a3a41SJohn Dyson 
2687925a3a41SJohn Dyson 					first_object->backing_object = srcobject;
2688925a3a41SJohn Dyson 				}
26891efb74fbSJohn Dyson 				first_object->backing_object_offset = cp;
26902d8acc0fSJohn Dyson 				map->timestamp++;
2691925a3a41SJohn Dyson 			} else {
26922d8acc0fSJohn Dyson 				pmap_remove (map->pmap, uaddr, tend);
2693925a3a41SJohn Dyson 			}
26941efb74fbSJohn Dyson /*
26951efb74fbSJohn Dyson  * Otherwise, we have to do a logical mmap.
26961efb74fbSJohn Dyson  */
26971efb74fbSJohn Dyson 		} else {
26981efb74fbSJohn Dyson 
2699069e9bc1SDoug Rabson 			vm_object_set_flag(srcobject, OBJ_OPT);
2700925a3a41SJohn Dyson 			vm_object_reference(srcobject);
27011efb74fbSJohn Dyson 
27022d8acc0fSJohn Dyson 			pmap_remove (map->pmap, uaddr, tend);
27031efb74fbSJohn Dyson 
270447221757SJohn Dyson 			vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
270547221757SJohn Dyson 			vm_map_lock_upgrade(map);
27061efb74fbSJohn Dyson 
27072d8acc0fSJohn Dyson 			if (entry == &map->header) {
27081efb74fbSJohn Dyson 				map->first_free = &map->header;
27091efb74fbSJohn Dyson 			} else if (map->first_free->start >= start) {
27102d8acc0fSJohn Dyson 				map->first_free = entry->prev;
27111efb74fbSJohn Dyson 			}
27121efb74fbSJohn Dyson 
27132d8acc0fSJohn Dyson 			SAVE_HINT(map, entry->prev);
27142d8acc0fSJohn Dyson 			vm_map_entry_delete(map, entry);
27151efb74fbSJohn Dyson 
27162d8acc0fSJohn Dyson 			object = srcobject;
27172d8acc0fSJohn Dyson 			ooffset = cp;
27182d8acc0fSJohn Dyson #if 0
27192d8acc0fSJohn Dyson 			vm_object_shadow(&object, &ooffset, osize);
27202d8acc0fSJohn Dyson #endif
27212d8acc0fSJohn Dyson 
27222d8acc0fSJohn Dyson 			rv = vm_map_insert(map, object, ooffset, start, tend,
27232d8acc0fSJohn Dyson 				VM_PROT_ALL, VM_PROT_ALL, MAP_COPY_ON_WRITE|MAP_COPY_NEEDED);
27241efb74fbSJohn Dyson 
27251efb74fbSJohn Dyson 			if (rv != KERN_SUCCESS)
27261efb74fbSJohn Dyson 				panic("vm_uiomove: could not insert new entry: %d", rv);
27271efb74fbSJohn Dyson 		}
27281efb74fbSJohn Dyson 
27291efb74fbSJohn Dyson /*
27301efb74fbSJohn Dyson  * Map the window directly, if it is already in memory
27311efb74fbSJohn Dyson  */
27322d8acc0fSJohn Dyson 		pmap_object_init_pt(map->pmap, uaddr,
27332d8acc0fSJohn Dyson 			srcobject, oindex, tcnt, 0);
27341efb74fbSJohn Dyson 
273547221757SJohn Dyson 		map->timestamp++;
27361efb74fbSJohn Dyson 		vm_map_unlock(map);
27371efb74fbSJohn Dyson 
27381efb74fbSJohn Dyson 		cnt -= tcnt;
27392d8acc0fSJohn Dyson 		uaddr += tcnt;
27401efb74fbSJohn Dyson 		cp += tcnt;
274195e5e988SJohn Dyson 		if (npages)
274295e5e988SJohn Dyson 			*npages += osize;
27431efb74fbSJohn Dyson 	}
27441efb74fbSJohn Dyson 	return 0;
27451efb74fbSJohn Dyson }
27461efb74fbSJohn Dyson 
27471efb74fbSJohn Dyson /*
27481efb74fbSJohn Dyson  * Performs the copy_on_write operations necessary to allow the virtual copies
27491efb74fbSJohn Dyson  * into user space to work.  This has to be called for write(2) system calls
27501efb74fbSJohn Dyson  * from other processes, file unlinking, and file size shrinkage.
27511efb74fbSJohn Dyson  */
27521efb74fbSJohn Dyson void
27531efb74fbSJohn Dyson vm_freeze_copyopts(object, froma, toa)
27541efb74fbSJohn Dyson 	vm_object_t object;
27551efb74fbSJohn Dyson 	vm_pindex_t froma, toa;
27561efb74fbSJohn Dyson {
275795461b45SJohn Dyson 	int s, rv;
27581efb74fbSJohn Dyson 	vm_object_t robject, robjectn;
27591efb74fbSJohn Dyson 	vm_pindex_t idx, from, to;
27601efb74fbSJohn Dyson 
27612d8acc0fSJohn Dyson 	if ((object == NULL) ||
276295e5e988SJohn Dyson 		((object->flags & OBJ_OPT) == 0))
276395e5e988SJohn Dyson 		return;
27641efb74fbSJohn Dyson 
27651efb74fbSJohn Dyson 	if (object->shadow_count > object->ref_count)
27661efb74fbSJohn Dyson 		panic("vm_freeze_copyopts: sc > rc");
27671efb74fbSJohn Dyson 
276847221757SJohn Dyson 	while( robject = TAILQ_FIRST(&object->shadow_head)) {
27691efb74fbSJohn Dyson 		vm_pindex_t bo_pindex;
27701efb74fbSJohn Dyson 		vm_page_t m_in, m_out;
27711efb74fbSJohn Dyson 
27721efb74fbSJohn Dyson 		bo_pindex = OFF_TO_IDX(robject->backing_object_offset);
27731efb74fbSJohn Dyson 
277495e5e988SJohn Dyson 		vm_object_reference(robject);
2775925a3a41SJohn Dyson 
277666095752SJohn Dyson 		vm_object_pip_wait(robject, "objfrz");
2777925a3a41SJohn Dyson 
27781efb74fbSJohn Dyson 		if (robject->ref_count == 1) {
27791efb74fbSJohn Dyson 			vm_object_deallocate(robject);
27801efb74fbSJohn Dyson 			continue;
27811efb74fbSJohn Dyson 		}
27821efb74fbSJohn Dyson 
2783d474eaaaSDoug Rabson 		vm_object_pip_add(robject, 1);
27841efb74fbSJohn Dyson 
278547221757SJohn Dyson 		for (idx = 0; idx < robject->size; idx++) {
27861efb74fbSJohn Dyson 
278747221757SJohn Dyson m_outretry:
278895461b45SJohn Dyson 			m_out = vm_page_grab(robject, idx,
278995461b45SJohn Dyson 						VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
27901efb74fbSJohn Dyson 
27911efb74fbSJohn Dyson 			if (m_out->valid == 0) {
279247221757SJohn Dyson m_inretry:
279395461b45SJohn Dyson 				m_in = vm_page_grab(object, bo_pindex + idx,
279495461b45SJohn Dyson 						VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
279595461b45SJohn Dyson 				if (m_in->valid == 0) {
279647221757SJohn Dyson 					rv = vm_pager_get_pages(object, &m_in, 1, 0);
279747221757SJohn Dyson 					if (rv != VM_PAGER_OK) {
279847221757SJohn Dyson 						printf("vm_freeze_copyopts: cannot read page from file: %x\n", m_in->pindex);
279947221757SJohn Dyson 						continue;
28001efb74fbSJohn Dyson 					}
280195461b45SJohn Dyson 					vm_page_deactivate(m_in);
280247221757SJohn Dyson 				}
280347221757SJohn Dyson 
280447221757SJohn Dyson 				vm_page_protect(m_in, VM_PROT_NONE);
280547221757SJohn Dyson 				pmap_copy_page(VM_PAGE_TO_PHYS(m_in), VM_PAGE_TO_PHYS(m_out));
280695461b45SJohn Dyson 				m_out->valid = m_in->valid;
280747221757SJohn Dyson 				m_out->dirty = VM_PAGE_BITS_ALL;
280847221757SJohn Dyson 
280995461b45SJohn Dyson 				vm_page_activate(m_out);
281047221757SJohn Dyson 
28111efb74fbSJohn Dyson 				PAGE_WAKEUP(m_in);
28121efb74fbSJohn Dyson 			}
281395461b45SJohn Dyson 			PAGE_WAKEUP(m_out);
281447221757SJohn Dyson 		}
2815925a3a41SJohn Dyson 
28161efb74fbSJohn Dyson 		object->shadow_count--;
281747221757SJohn Dyson 		object->ref_count--;
28181efb74fbSJohn Dyson 		TAILQ_REMOVE(&object->shadow_head, robject, shadow_list);
28191efb74fbSJohn Dyson 		robject->backing_object = NULL;
28201efb74fbSJohn Dyson 		robject->backing_object_offset = 0;
28211efb74fbSJohn Dyson 
282247221757SJohn Dyson 		vm_object_pip_wakeup(robject);
28231efb74fbSJohn Dyson 		vm_object_deallocate(robject);
28241efb74fbSJohn Dyson 	}
282547221757SJohn Dyson 
2826069e9bc1SDoug Rabson 	vm_object_clear_flag(object, OBJ_OPT);
28271efb74fbSJohn Dyson }
28281efb74fbSJohn Dyson 
2829c7c34a24SBruce Evans #include "opt_ddb.h"
2830c3cb3e12SDavid Greenman #ifdef DDB
2831c7c34a24SBruce Evans #include <sys/kernel.h>
2832c7c34a24SBruce Evans 
2833c7c34a24SBruce Evans #include <ddb/ddb.h>
2834c7c34a24SBruce Evans 
2835df8bae1dSRodney W. Grimes /*
2836df8bae1dSRodney W. Grimes  *	vm_map_print:	[ debug ]
2837df8bae1dSRodney W. Grimes  */
2838c7c34a24SBruce Evans DB_SHOW_COMMAND(map, vm_map_print)
2839df8bae1dSRodney W. Grimes {
284095e5e988SJohn Dyson 	static int nlines;
2841c7c34a24SBruce Evans 	/* XXX convert args. */
2842c0877f10SJohn Dyson 	vm_map_t map = (vm_map_t)addr;
2843c7c34a24SBruce Evans 	boolean_t full = have_addr;
2844df8bae1dSRodney W. Grimes 
2845c0877f10SJohn Dyson 	vm_map_entry_t entry;
2846c7c34a24SBruce Evans 
2847101eeb7fSBruce Evans 	db_iprintf("%s map %p: pmap=%p, nentries=%d, version=%u\n",
2848101eeb7fSBruce Evans 	    (map->is_main_map ? "Task" : "Share"), (void *)map,
2849101eeb7fSBruce Evans 	    (void *)map->pmap, map->nentries, map->timestamp);
285095e5e988SJohn Dyson 	nlines++;
2851df8bae1dSRodney W. Grimes 
2852c7c34a24SBruce Evans 	if (!full && db_indent)
2853df8bae1dSRodney W. Grimes 		return;
2854df8bae1dSRodney W. Grimes 
2855c7c34a24SBruce Evans 	db_indent += 2;
2856df8bae1dSRodney W. Grimes 	for (entry = map->header.next; entry != &map->header;
2857df8bae1dSRodney W. Grimes 	    entry = entry->next) {
285895e5e988SJohn Dyson #if 0
285995e5e988SJohn Dyson 		if (nlines > 18) {
286095e5e988SJohn Dyson 			db_printf("--More--");
286195e5e988SJohn Dyson 			cngetc();
286295e5e988SJohn Dyson 			db_printf("\r");
286395e5e988SJohn Dyson 			nlines = 0;
286495e5e988SJohn Dyson 		}
286595e5e988SJohn Dyson #endif
286695e5e988SJohn Dyson 
2867fc62ef1fSBruce Evans 		db_iprintf("map entry %p: start=%p, end=%p\n",
2868fc62ef1fSBruce Evans 		    (void *)entry, (void *)entry->start, (void *)entry->end);
286995e5e988SJohn Dyson 		nlines++;
2870df8bae1dSRodney W. Grimes 		if (map->is_main_map) {
2871df8bae1dSRodney W. Grimes 			static char *inheritance_name[4] =
2872df8bae1dSRodney W. Grimes 			{"share", "copy", "none", "donate_copy"};
28730d94caffSDavid Greenman 
287495e5e988SJohn Dyson 			db_iprintf(" prot=%x/%x/%s",
2875df8bae1dSRodney W. Grimes 			    entry->protection,
2876df8bae1dSRodney W. Grimes 			    entry->max_protection,
2877df8bae1dSRodney W. Grimes 			    inheritance_name[entry->inheritance]);
2878df8bae1dSRodney W. Grimes 			if (entry->wired_count != 0)
287995e5e988SJohn Dyson 				db_printf(", wired");
2880df8bae1dSRodney W. Grimes 		}
2881afa07f7eSJohn Dyson 		if (entry->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP)) {
2882101eeb7fSBruce Evans 			/* XXX no %qd in kernel.  Truncate entry->offset. */
2883101eeb7fSBruce Evans 			db_printf(", share=%p, offset=0x%lx\n",
2884101eeb7fSBruce Evans 			    (void *)entry->object.share_map,
2885ecbb00a2SDoug Rabson 			    (long)entry->offset);
288695e5e988SJohn Dyson 			nlines++;
2887df8bae1dSRodney W. Grimes 			if ((entry->prev == &map->header) ||
2888afa07f7eSJohn Dyson 			    ((entry->prev->eflags & MAP_ENTRY_IS_A_MAP) == 0) ||
2889df8bae1dSRodney W. Grimes 			    (entry->prev->object.share_map !=
2890df8bae1dSRodney W. Grimes 				entry->object.share_map)) {
2891c7c34a24SBruce Evans 				db_indent += 2;
2892101eeb7fSBruce Evans 				vm_map_print((db_expr_t)(intptr_t)
2893101eeb7fSBruce Evans 					     entry->object.share_map,
2894914181e7SBruce Evans 					     full, 0, (char *)0);
2895c7c34a24SBruce Evans 				db_indent -= 2;
2896df8bae1dSRodney W. Grimes 			}
28970d94caffSDavid Greenman 		} else {
2898101eeb7fSBruce Evans 			/* XXX no %qd in kernel.  Truncate entry->offset. */
2899101eeb7fSBruce Evans 			db_printf(", object=%p, offset=0x%lx",
2900101eeb7fSBruce Evans 			    (void *)entry->object.vm_object,
2901ecbb00a2SDoug Rabson 			    (long)entry->offset);
2902afa07f7eSJohn Dyson 			if (entry->eflags & MAP_ENTRY_COW)
2903c7c34a24SBruce Evans 				db_printf(", copy (%s)",
2904afa07f7eSJohn Dyson 				    (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
2905c7c34a24SBruce Evans 			db_printf("\n");
290695e5e988SJohn Dyson 			nlines++;
2907df8bae1dSRodney W. Grimes 
2908df8bae1dSRodney W. Grimes 			if ((entry->prev == &map->header) ||
2909afa07f7eSJohn Dyson 			    (entry->prev->eflags & MAP_ENTRY_IS_A_MAP) ||
2910df8bae1dSRodney W. Grimes 			    (entry->prev->object.vm_object !=
2911df8bae1dSRodney W. Grimes 				entry->object.vm_object)) {
2912c7c34a24SBruce Evans 				db_indent += 2;
2913101eeb7fSBruce Evans 				vm_object_print((db_expr_t)(intptr_t)
2914101eeb7fSBruce Evans 						entry->object.vm_object,
2915914181e7SBruce Evans 						full, 0, (char *)0);
291695e5e988SJohn Dyson 				nlines += 4;
2917c7c34a24SBruce Evans 				db_indent -= 2;
2918df8bae1dSRodney W. Grimes 			}
2919df8bae1dSRodney W. Grimes 		}
2920df8bae1dSRodney W. Grimes 	}
2921c7c34a24SBruce Evans 	db_indent -= 2;
292295e5e988SJohn Dyson 	if (db_indent == 0)
292395e5e988SJohn Dyson 		nlines = 0;
2924df8bae1dSRodney W. Grimes }
292595e5e988SJohn Dyson 
292695e5e988SJohn Dyson 
292795e5e988SJohn Dyson DB_SHOW_COMMAND(procvm, procvm)
292895e5e988SJohn Dyson {
292995e5e988SJohn Dyson 	struct proc *p;
293095e5e988SJohn Dyson 
293195e5e988SJohn Dyson 	if (have_addr) {
293295e5e988SJohn Dyson 		p = (struct proc *) addr;
293395e5e988SJohn Dyson 	} else {
293495e5e988SJohn Dyson 		p = curproc;
293595e5e988SJohn Dyson 	}
293695e5e988SJohn Dyson 
2937ac1e407bSBruce Evans 	db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
2938ac1e407bSBruce Evans 	    (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
2939ac1e407bSBruce Evans 	    (void *)&p->p_vmspace->vm_pmap);
294095e5e988SJohn Dyson 
2941101eeb7fSBruce Evans 	vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);
294295e5e988SJohn Dyson }
294395e5e988SJohn Dyson 
2944c7c34a24SBruce Evans #endif /* DDB */
2945