xref: /freebsd/sys/vm/vm_map.c (revision 11afcc8f9f96d657b8e6f7547c02c1957331fc96)
1 /*
2  * Copyright (c) 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * The Mach Operating System project at Carnegie-Mellon University.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	from: @(#)vm_map.c	8.3 (Berkeley) 1/12/94
37  *
38  *
39  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
40  * All rights reserved.
41  *
42  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
43  *
44  * Permission to use, copy, modify and distribute this software and
45  * its documentation is hereby granted, provided that both the copyright
46  * notice and this permission notice appear in all copies of the
47  * software, derivative works or modified versions, and any portions
48  * thereof, and that both notices appear in supporting documentation.
49  *
50  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
51  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
52  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
53  *
54  * Carnegie Mellon requests users of this software to return to
55  *
56  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
57  *  School of Computer Science
58  *  Carnegie Mellon University
59  *  Pittsburgh PA 15213-3890
60  *
61  * any improvements or extensions that they make and grant Carnegie the
62  * rights to redistribute these changes.
63  *
64  * $Id: vm_map.c,v 1.132 1998/07/14 12:14:58 bde Exp $
65  */
66 
67 /*
68  *	Virtual memory mapping module.
69  */
70 
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/malloc.h>
74 #include <sys/proc.h>
75 #include <sys/vmmeter.h>
76 #include <sys/mman.h>
77 #include <sys/vnode.h>
78 
79 #include <vm/vm.h>
80 #include <vm/vm_param.h>
81 #include <vm/vm_prot.h>
82 #include <vm/vm_inherit.h>
83 #include <sys/lock.h>
84 #include <vm/pmap.h>
85 #include <vm/vm_map.h>
86 #include <vm/vm_page.h>
87 #include <vm/vm_object.h>
88 #include <vm/vm_pager.h>
89 #include <vm/vm_kern.h>
90 #include <vm/vm_extern.h>
91 #include <vm/default_pager.h>
92 #include <vm/swap_pager.h>
93 #include <vm/vm_zone.h>
94 
95 static MALLOC_DEFINE(M_VMMAP, "VM map", "VM map structures");
96 
97 /*
98  *	Virtual memory maps provide for the mapping, protection,
99  *	and sharing of virtual memory objects.  In addition,
100  *	this module provides for an efficient virtual copy of
101  *	memory from one map to another.
102  *
103  *	Synchronization is required prior to most operations.
104  *
105  *	Maps consist of an ordered doubly-linked list of simple
106  *	entries; a single hint is used to speed up lookups.
107  *
108  *	In order to properly represent the sharing of virtual
109  *	memory regions among maps, the map structure is bi-level.
110  *	Top-level ("address") maps refer to regions of sharable
111  *	virtual memory.  These regions are implemented as
112  *	("sharing") maps, which then refer to the actual virtual
113  *	memory objects.  When two address maps "share" memory,
114  *	their top-level maps both have references to the same
115  *	sharing map.  When memory is virtual-copied from one
116  *	address map to another, the references in the sharing
117  *	maps are actually copied -- no copying occurs at the
118  *	virtual memory object level.
119  *
120  *	Since portions of maps are specified by start/end addreses,
121  *	which may not align with existing map entries, all
122  *	routines merely "clip" entries to these start/end values.
123  *	[That is, an entry is split into two, bordering at a
124  *	start or end value.]  Note that these clippings may not
125  *	always be necessary (as the two resulting entries are then
126  *	not changed); however, the clipping is done for convenience.
127  *	No attempt is currently made to "glue back together" two
128  *	abutting entries.
129  *
130  *	As mentioned above, virtual copy operations are performed
131  *	by copying VM object references from one sharing map to
132  *	another, and then marking both regions as copy-on-write.
133  *	It is important to note that only one writeable reference
134  *	to a VM object region exists in any map -- this means that
135  *	shadow object creation can be delayed until a write operation
136  *	occurs.
137  */
138 
139 /*
140  *	vm_map_startup:
141  *
142  *	Initialize the vm_map module.  Must be called before
143  *	any other vm_map routines.
144  *
145  *	Map and entry structures are allocated from the general
146  *	purpose memory pool with some exceptions:
147  *
148  *	- The kernel map and kmem submap are allocated statically.
149  *	- Kernel map entries are allocated out of a static pool.
150  *
151  *	These restrictions are necessary since malloc() uses the
152  *	maps and requires map entries.
153  */
154 
155 extern char kstack[];
156 extern int inmprotect;
157 
158 static struct vm_zone kmapentzone_store, mapentzone_store, mapzone_store;
159 static vm_zone_t mapentzone, kmapentzone, mapzone, vmspace_zone;
160 static struct vm_object kmapentobj, mapentobj, mapobj;
161 #define MAP_ENTRY_INIT	128
162 static struct vm_map_entry map_entry_init[MAX_MAPENT];
163 static struct vm_map_entry kmap_entry_init[MAX_KMAPENT];
164 static struct vm_map map_init[MAX_KMAP];
165 
166 static void _vm_map_clip_end __P((vm_map_t, vm_map_entry_t, vm_offset_t));
167 static void _vm_map_clip_start __P((vm_map_t, vm_map_entry_t, vm_offset_t));
168 static vm_map_entry_t vm_map_entry_create __P((vm_map_t));
169 static void vm_map_entry_delete __P((vm_map_t, vm_map_entry_t));
170 static void vm_map_entry_dispose __P((vm_map_t, vm_map_entry_t));
171 static void vm_map_entry_unwire __P((vm_map_t, vm_map_entry_t));
172 static void vm_map_copy_entry __P((vm_map_t, vm_map_t, vm_map_entry_t,
173 		vm_map_entry_t));
174 static void vm_map_split __P((vm_map_entry_t));
175 
176 void
177 vm_map_startup()
178 {
179 	mapzone = &mapzone_store;
180 	zbootinit(mapzone, "MAP", sizeof (struct vm_map),
181 		map_init, MAX_KMAP);
182 	kmapentzone = &kmapentzone_store;
183 	zbootinit(kmapentzone, "KMAP ENTRY", sizeof (struct vm_map_entry),
184 		kmap_entry_init, MAX_KMAPENT);
185 	mapentzone = &mapentzone_store;
186 	zbootinit(mapentzone, "MAP ENTRY", sizeof (struct vm_map_entry),
187 		map_entry_init, MAX_MAPENT);
188 }
189 
190 /*
191  * Allocate a vmspace structure, including a vm_map and pmap,
192  * and initialize those structures.  The refcnt is set to 1.
193  * The remaining fields must be initialized by the caller.
194  */
195 struct vmspace *
196 vmspace_alloc(min, max)
197 	vm_offset_t min, max;
198 {
199 	struct vmspace *vm;
200 
201 	vm = zalloc(vmspace_zone);
202 	bzero(&vm->vm_map, sizeof vm->vm_map);
203 	vm_map_init(&vm->vm_map, min, max);
204 	pmap_pinit(&vm->vm_pmap);
205 	vm->vm_map.pmap = &vm->vm_pmap;		/* XXX */
206 	vm->vm_refcnt = 1;
207 	vm->vm_shm = NULL;
208 	return (vm);
209 }
210 
211 void
212 vm_init2(void) {
213 	zinitna(kmapentzone, &kmapentobj,
214 		NULL, 0, cnt.v_page_count / 4, ZONE_INTERRUPT, 1);
215 	zinitna(mapentzone, &mapentobj,
216 		NULL, 0, 0, 0, 1);
217 	zinitna(mapzone, &mapobj,
218 		NULL, 0, 0, 0, 1);
219 	vmspace_zone = zinit("VMSPACE", sizeof (struct vmspace), 0, 0, 3);
220 	pmap_init2();
221 	vm_object_init2();
222 }
223 
224 void
225 vmspace_free(vm)
226 	struct vmspace *vm;
227 {
228 
229 	if (vm->vm_refcnt == 0)
230 		panic("vmspace_free: attempt to free already freed vmspace");
231 
232 	if (--vm->vm_refcnt == 0) {
233 
234 		/*
235 		 * Lock the map, to wait out all other references to it.
236 		 * Delete all of the mappings and pages they hold, then call
237 		 * the pmap module to reclaim anything left.
238 		 */
239 		vm_map_lock(&vm->vm_map);
240 		(void) vm_map_delete(&vm->vm_map, vm->vm_map.min_offset,
241 		    vm->vm_map.max_offset);
242 		vm_map_unlock(&vm->vm_map);
243 
244 		pmap_release(&vm->vm_pmap);
245 		zfree(vmspace_zone, vm);
246 	}
247 }
248 
249 /*
250  *	vm_map_create:
251  *
252  *	Creates and returns a new empty VM map with
253  *	the given physical map structure, and having
254  *	the given lower and upper address bounds.
255  */
256 vm_map_t
257 vm_map_create(pmap, min, max)
258 	pmap_t pmap;
259 	vm_offset_t min, max;
260 {
261 	vm_map_t result;
262 
263 	result = zalloc(mapzone);
264 	vm_map_init(result, min, max);
265 	result->pmap = pmap;
266 	return (result);
267 }
268 
269 /*
270  * Initialize an existing vm_map structure
271  * such as that in the vmspace structure.
272  * The pmap is set elsewhere.
273  */
274 void
275 vm_map_init(map, min, max)
276 	struct vm_map *map;
277 	vm_offset_t min, max;
278 {
279 	map->header.next = map->header.prev = &map->header;
280 	map->nentries = 0;
281 	map->size = 0;
282 	map->is_main_map = TRUE;
283 	map->system_map = 0;
284 	map->min_offset = min;
285 	map->max_offset = max;
286 	map->first_free = &map->header;
287 	map->hint = &map->header;
288 	map->timestamp = 0;
289 	lockinit(&map->lock, PVM, "thrd_sleep", 0, LK_NOPAUSE);
290 }
291 
292 /*
293  *	vm_map_entry_dispose:	[ internal use only ]
294  *
295  *	Inverse of vm_map_entry_create.
296  */
297 static void
298 vm_map_entry_dispose(map, entry)
299 	vm_map_t map;
300 	vm_map_entry_t entry;
301 {
302 	zfree((map->system_map || !mapentzone) ? kmapentzone : mapentzone, entry);
303 }
304 
305 /*
306  *	vm_map_entry_create:	[ internal use only ]
307  *
308  *	Allocates a VM map entry for insertion.
309  *	No entry fields are filled in.  This routine is
310  */
311 static vm_map_entry_t
312 vm_map_entry_create(map)
313 	vm_map_t map;
314 {
315 	return zalloc((map->system_map || !mapentzone) ? kmapentzone : mapentzone);
316 }
317 
318 /*
319  *	vm_map_entry_{un,}link:
320  *
321  *	Insert/remove entries from maps.
322  */
323 #define	vm_map_entry_link(map, after_where, entry) \
324 		{ \
325 		(map)->nentries++; \
326 		(map)->timestamp++; \
327 		(entry)->prev = (after_where); \
328 		(entry)->next = (after_where)->next; \
329 		(entry)->prev->next = (entry); \
330 		(entry)->next->prev = (entry); \
331 		}
332 #define	vm_map_entry_unlink(map, entry) \
333 		{ \
334 		(map)->nentries--; \
335 		(map)->timestamp++; \
336 		(entry)->next->prev = (entry)->prev; \
337 		(entry)->prev->next = (entry)->next; \
338 		}
339 
340 /*
341  *	SAVE_HINT:
342  *
343  *	Saves the specified entry as the hint for
344  *	future lookups.
345  */
346 #define	SAVE_HINT(map,value) \
347 		(map)->hint = (value);
348 
349 /*
350  *	vm_map_lookup_entry:	[ internal use only ]
351  *
352  *	Finds the map entry containing (or
353  *	immediately preceding) the specified address
354  *	in the given map; the entry is returned
355  *	in the "entry" parameter.  The boolean
356  *	result indicates whether the address is
357  *	actually contained in the map.
358  */
359 boolean_t
360 vm_map_lookup_entry(map, address, entry)
361 	vm_map_t map;
362 	vm_offset_t address;
363 	vm_map_entry_t *entry;	/* OUT */
364 {
365 	vm_map_entry_t cur;
366 	vm_map_entry_t last;
367 
368 	/*
369 	 * Start looking either from the head of the list, or from the hint.
370 	 */
371 
372 	cur = map->hint;
373 
374 	if (cur == &map->header)
375 		cur = cur->next;
376 
377 	if (address >= cur->start) {
378 		/*
379 		 * Go from hint to end of list.
380 		 *
381 		 * But first, make a quick check to see if we are already looking
382 		 * at the entry we want (which is usually the case). Note also
383 		 * that we don't need to save the hint here... it is the same
384 		 * hint (unless we are at the header, in which case the hint
385 		 * didn't buy us anything anyway).
386 		 */
387 		last = &map->header;
388 		if ((cur != last) && (cur->end > address)) {
389 			*entry = cur;
390 			return (TRUE);
391 		}
392 	} else {
393 		/*
394 		 * Go from start to hint, *inclusively*
395 		 */
396 		last = cur->next;
397 		cur = map->header.next;
398 	}
399 
400 	/*
401 	 * Search linearly
402 	 */
403 
404 	while (cur != last) {
405 		if (cur->end > address) {
406 			if (address >= cur->start) {
407 				/*
408 				 * Save this lookup for future hints, and
409 				 * return
410 				 */
411 
412 				*entry = cur;
413 				SAVE_HINT(map, cur);
414 				return (TRUE);
415 			}
416 			break;
417 		}
418 		cur = cur->next;
419 	}
420 	*entry = cur->prev;
421 	SAVE_HINT(map, *entry);
422 	return (FALSE);
423 }
424 
425 /*
426  *	vm_map_insert:
427  *
428  *	Inserts the given whole VM object into the target
429  *	map at the specified address range.  The object's
430  *	size should match that of the address range.
431  *
432  *	Requires that the map be locked, and leaves it so.
433  */
434 int
435 vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
436 	      vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max,
437 	      int cow)
438 {
439 	vm_map_entry_t new_entry;
440 	vm_map_entry_t prev_entry;
441 	vm_map_entry_t temp_entry;
442 	vm_object_t prev_object;
443 	u_char protoeflags;
444 
445 	if ((object != NULL) && (cow & MAP_NOFAULT)) {
446 		panic("vm_map_insert: paradoxical MAP_NOFAULT request");
447 	}
448 
449 	/*
450 	 * Check that the start and end points are not bogus.
451 	 */
452 
453 	if ((start < map->min_offset) || (end > map->max_offset) ||
454 	    (start >= end))
455 		return (KERN_INVALID_ADDRESS);
456 
457 	/*
458 	 * Find the entry prior to the proposed starting address; if it's part
459 	 * of an existing entry, this range is bogus.
460 	 */
461 
462 	if (vm_map_lookup_entry(map, start, &temp_entry))
463 		return (KERN_NO_SPACE);
464 
465 	prev_entry = temp_entry;
466 
467 	/*
468 	 * Assert that the next entry doesn't overlap the end point.
469 	 */
470 
471 	if ((prev_entry->next != &map->header) &&
472 	    (prev_entry->next->start < end))
473 		return (KERN_NO_SPACE);
474 
475 	protoeflags = 0;
476 	if (cow & MAP_COPY_NEEDED)
477 		protoeflags |= MAP_ENTRY_NEEDS_COPY;
478 
479 	if (cow & MAP_COPY_ON_WRITE)
480 		protoeflags |= MAP_ENTRY_COW;
481 
482 	if (cow & MAP_NOFAULT)
483 		protoeflags |= MAP_ENTRY_NOFAULT;
484 
485 	/*
486 	 * See if we can avoid creating a new entry by extending one of our
487 	 * neighbors.  Or at least extend the object.
488 	 */
489 
490 	if ((object == NULL) &&
491 	    (prev_entry != &map->header) &&
492 	    (( prev_entry->eflags & (MAP_ENTRY_IS_A_MAP | MAP_ENTRY_IS_SUB_MAP)) == 0) &&
493 		((prev_entry->object.vm_object == NULL) ||
494 			(prev_entry->object.vm_object->type == OBJT_DEFAULT)) &&
495 	    (prev_entry->end == start) &&
496 	    (prev_entry->wired_count == 0)) {
497 
498 
499 		if ((protoeflags == prev_entry->eflags) &&
500 		    ((cow & MAP_NOFAULT) ||
501 		     vm_object_coalesce(prev_entry->object.vm_object,
502 					OFF_TO_IDX(prev_entry->offset),
503 					(vm_size_t) (prev_entry->end - prev_entry->start),
504 					(vm_size_t) (end - prev_entry->end)))) {
505 
506 			/*
507 			 * Coalesced the two objects.  Can we extend the
508 			 * previous map entry to include the new range?
509 			 */
510 			if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
511 			    (prev_entry->protection == prot) &&
512 			    (prev_entry->max_protection == max)) {
513 
514 				map->size += (end - prev_entry->end);
515 				prev_entry->end = end;
516 				if ((cow & MAP_NOFAULT) == 0) {
517 					prev_object = prev_entry->object.vm_object;
518 					default_pager_convert_to_swapq(prev_object);
519 				}
520 				return (KERN_SUCCESS);
521 			}
522 			else {
523 				object = prev_entry->object.vm_object;
524 				offset = prev_entry->offset + (prev_entry->end -
525 							       prev_entry->start);
526 
527 				vm_object_reference(object);
528 			}
529 		}
530 	}
531 
532 	/*
533 	 * Create a new entry
534 	 */
535 
536 	new_entry = vm_map_entry_create(map);
537 	new_entry->start = start;
538 	new_entry->end = end;
539 
540 	new_entry->eflags = protoeflags;
541 	new_entry->object.vm_object = object;
542 	new_entry->offset = offset;
543 	if (object) {
544 		if ((object->ref_count > 1) || (object->shadow_count != 0)) {
545 			object->flags &= ~OBJ_ONEMAPPING;
546 		} else {
547 			object->flags |= OBJ_ONEMAPPING;
548 		}
549 	}
550 
551 	if (map->is_main_map) {
552 		new_entry->inheritance = VM_INHERIT_DEFAULT;
553 		new_entry->protection = prot;
554 		new_entry->max_protection = max;
555 		new_entry->wired_count = 0;
556 	}
557 	/*
558 	 * Insert the new entry into the list
559 	 */
560 
561 	vm_map_entry_link(map, prev_entry, new_entry);
562 	map->size += new_entry->end - new_entry->start;
563 
564 	/*
565 	 * Update the free space hint
566 	 */
567 	if ((map->first_free == prev_entry) &&
568 		(prev_entry->end >= new_entry->start))
569 		map->first_free = new_entry;
570 
571 	default_pager_convert_to_swapq(object);
572 	return (KERN_SUCCESS);
573 }
574 
575 /*
576  * Find sufficient space for `length' bytes in the given map, starting at
577  * `start'.  The map must be locked.  Returns 0 on success, 1 on no space.
578  */
579 int
580 vm_map_findspace(map, start, length, addr)
581 	vm_map_t map;
582 	vm_offset_t start;
583 	vm_size_t length;
584 	vm_offset_t *addr;
585 {
586 	vm_map_entry_t entry, next;
587 	vm_offset_t end;
588 
589 	if (start < map->min_offset)
590 		start = map->min_offset;
591 	if (start > map->max_offset)
592 		return (1);
593 
594 	/*
595 	 * Look for the first possible address; if there's already something
596 	 * at this address, we have to start after it.
597 	 */
598 	if (start == map->min_offset) {
599 		if ((entry = map->first_free) != &map->header)
600 			start = entry->end;
601 	} else {
602 		vm_map_entry_t tmp;
603 
604 		if (vm_map_lookup_entry(map, start, &tmp))
605 			start = tmp->end;
606 		entry = tmp;
607 	}
608 
609 	/*
610 	 * Look through the rest of the map, trying to fit a new region in the
611 	 * gap between existing regions, or after the very last region.
612 	 */
613 	for (;; start = (entry = next)->end) {
614 		/*
615 		 * Find the end of the proposed new region.  Be sure we didn't
616 		 * go beyond the end of the map, or wrap around the address;
617 		 * if so, we lose.  Otherwise, if this is the last entry, or
618 		 * if the proposed new region fits before the next entry, we
619 		 * win.
620 		 */
621 		end = start + length;
622 		if (end > map->max_offset || end < start)
623 			return (1);
624 		next = entry->next;
625 		if (next == &map->header || next->start >= end)
626 			break;
627 	}
628 	SAVE_HINT(map, entry);
629 	*addr = start;
630 	if (map == kernel_map) {
631 		vm_offset_t ksize;
632 		if ((ksize = round_page(start + length)) > kernel_vm_end) {
633 			pmap_growkernel(ksize);
634 		}
635 	}
636 	return (0);
637 }
638 
639 /*
640  *	vm_map_find finds an unallocated region in the target address
641  *	map with the given length.  The search is defined to be
642  *	first-fit from the specified address; the region found is
643  *	returned in the same parameter.
644  *
645  */
646 int
647 vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
648 	    vm_offset_t *addr,	/* IN/OUT */
649 	    vm_size_t length, boolean_t find_space, vm_prot_t prot,
650 	    vm_prot_t max, int cow)
651 {
652 	vm_offset_t start;
653 	int result, s = 0;
654 
655 	start = *addr;
656 
657 	if (map == kmem_map || map == mb_map)
658 		s = splvm();
659 
660 	vm_map_lock(map);
661 	if (find_space) {
662 		if (vm_map_findspace(map, start, length, addr)) {
663 			vm_map_unlock(map);
664 			if (map == kmem_map || map == mb_map)
665 				splx(s);
666 			return (KERN_NO_SPACE);
667 		}
668 		start = *addr;
669 	}
670 	result = vm_map_insert(map, object, offset,
671 		start, start + length, prot, max, cow);
672 	vm_map_unlock(map);
673 
674 	if (map == kmem_map || map == mb_map)
675 		splx(s);
676 
677 	return (result);
678 }
679 
680 /*
681  *	vm_map_simplify_entry:
682  *
683  *	Simplify the given map entry by merging with either neighbor.
684  */
685 void
686 vm_map_simplify_entry(map, entry)
687 	vm_map_t map;
688 	vm_map_entry_t entry;
689 {
690 	vm_map_entry_t next, prev;
691 	vm_size_t prevsize, esize;
692 
693 	if (entry->eflags & (MAP_ENTRY_IS_SUB_MAP|MAP_ENTRY_IS_A_MAP))
694 		return;
695 
696 	prev = entry->prev;
697 	if (prev != &map->header) {
698 		prevsize = prev->end - prev->start;
699 		if ( (prev->end == entry->start) &&
700 		     (prev->object.vm_object == entry->object.vm_object) &&
701 		     (!prev->object.vm_object ||
702 				(prev->object.vm_object->behavior == entry->object.vm_object->behavior)) &&
703 		     (!prev->object.vm_object ||
704 			(prev->offset + prevsize == entry->offset)) &&
705 		     (prev->eflags == entry->eflags) &&
706 		     (prev->protection == entry->protection) &&
707 		     (prev->max_protection == entry->max_protection) &&
708 		     (prev->inheritance == entry->inheritance) &&
709 		     (prev->wired_count == entry->wired_count)) {
710 			if (map->first_free == prev)
711 				map->first_free = entry;
712 			if (map->hint == prev)
713 				map->hint = entry;
714 			vm_map_entry_unlink(map, prev);
715 			entry->start = prev->start;
716 			entry->offset = prev->offset;
717 			if (prev->object.vm_object)
718 				vm_object_deallocate(prev->object.vm_object);
719 			vm_map_entry_dispose(map, prev);
720 		}
721 	}
722 
723 	next = entry->next;
724 	if (next != &map->header) {
725 		esize = entry->end - entry->start;
726 		if ((entry->end == next->start) &&
727 		    (next->object.vm_object == entry->object.vm_object) &&
728 		    (!next->object.vm_object ||
729 				(next->object.vm_object->behavior == entry->object.vm_object->behavior)) &&
730 		     (!entry->object.vm_object ||
731 			(entry->offset + esize == next->offset)) &&
732 		    (next->eflags == entry->eflags) &&
733 		    (next->protection == entry->protection) &&
734 		    (next->max_protection == entry->max_protection) &&
735 		    (next->inheritance == entry->inheritance) &&
736 		    (next->wired_count == entry->wired_count)) {
737 			if (map->first_free == next)
738 				map->first_free = entry;
739 			if (map->hint == next)
740 				map->hint = entry;
741 			vm_map_entry_unlink(map, next);
742 			entry->end = next->end;
743 			if (next->object.vm_object)
744 				vm_object_deallocate(next->object.vm_object);
745 			vm_map_entry_dispose(map, next);
746 	        }
747 	}
748 }
749 /*
750  *	vm_map_clip_start:	[ internal use only ]
751  *
752  *	Asserts that the given entry begins at or after
753  *	the specified address; if necessary,
754  *	it splits the entry into two.
755  */
756 #define vm_map_clip_start(map, entry, startaddr) \
757 { \
758 	if (startaddr > entry->start) \
759 		_vm_map_clip_start(map, entry, startaddr); \
760 	else if (entry->object.vm_object && (entry->object.vm_object->ref_count == 1)) \
761 		entry->object.vm_object->flags |= OBJ_ONEMAPPING; \
762 }
763 
764 /*
765  *	This routine is called only when it is known that
766  *	the entry must be split.
767  */
768 static void
769 _vm_map_clip_start(map, entry, start)
770 	vm_map_t map;
771 	vm_map_entry_t entry;
772 	vm_offset_t start;
773 {
774 	vm_map_entry_t new_entry;
775 
776 	/*
777 	 * Split off the front portion -- note that we must insert the new
778 	 * entry BEFORE this one, so that this entry has the specified
779 	 * starting address.
780 	 */
781 
782 	vm_map_simplify_entry(map, entry);
783 
784 	/*
785 	 * If there is no object backing this entry, we might as well create
786 	 * one now.  If we defer it, an object can get created after the map
787 	 * is clipped, and individual objects will be created for the split-up
788 	 * map.  This is a bit of a hack, but is also about the best place to
789 	 * put this improvement.
790 	 */
791 
792 	if (entry->object.vm_object == NULL) {
793 		vm_object_t object;
794 		object = vm_object_allocate(OBJT_DEFAULT,
795 				atop(entry->end - entry->start));
796 		entry->object.vm_object = object;
797 		entry->offset = 0;
798 	}
799 
800 	new_entry = vm_map_entry_create(map);
801 	*new_entry = *entry;
802 
803 	new_entry->end = start;
804 	entry->offset += (start - entry->start);
805 	entry->start = start;
806 
807 	vm_map_entry_link(map, entry->prev, new_entry);
808 
809 	if ((entry->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP)) == 0) {
810 		if (new_entry->object.vm_object->ref_count == 1)
811 			new_entry->object.vm_object->flags |= OBJ_ONEMAPPING;
812 		vm_object_reference(new_entry->object.vm_object);
813 	}
814 }
815 
816 /*
817  *	vm_map_clip_end:	[ internal use only ]
818  *
819  *	Asserts that the given entry ends at or before
820  *	the specified address; if necessary,
821  *	it splits the entry into two.
822  */
823 
824 #define vm_map_clip_end(map, entry, endaddr) \
825 { \
826 	if (endaddr < entry->end) \
827 		_vm_map_clip_end(map, entry, endaddr); \
828 	else if (entry->object.vm_object && (entry->object.vm_object->ref_count == 1)) \
829 		entry->object.vm_object->flags |= OBJ_ONEMAPPING; \
830 }
831 
832 /*
833  *	This routine is called only when it is known that
834  *	the entry must be split.
835  */
836 static void
837 _vm_map_clip_end(map, entry, end)
838 	vm_map_t map;
839 	vm_map_entry_t entry;
840 	vm_offset_t end;
841 {
842 	vm_map_entry_t new_entry;
843 
844 	/*
845 	 * If there is no object backing this entry, we might as well create
846 	 * one now.  If we defer it, an object can get created after the map
847 	 * is clipped, and individual objects will be created for the split-up
848 	 * map.  This is a bit of a hack, but is also about the best place to
849 	 * put this improvement.
850 	 */
851 
852 	if (entry->object.vm_object == NULL) {
853 		vm_object_t object;
854 		object = vm_object_allocate(OBJT_DEFAULT,
855 				atop(entry->end - entry->start));
856 		entry->object.vm_object = object;
857 		entry->offset = 0;
858 	}
859 
860 	/*
861 	 * Create a new entry and insert it AFTER the specified entry
862 	 */
863 
864 	new_entry = vm_map_entry_create(map);
865 	*new_entry = *entry;
866 
867 	new_entry->start = entry->end = end;
868 	new_entry->offset += (end - entry->start);
869 
870 	vm_map_entry_link(map, entry, new_entry);
871 
872 	if ((entry->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP)) == 0) {
873 		if (new_entry->object.vm_object->ref_count == 1)
874 			new_entry->object.vm_object->flags |= OBJ_ONEMAPPING;
875 		vm_object_reference(new_entry->object.vm_object);
876 	}
877 }
878 
879 /*
880  *	VM_MAP_RANGE_CHECK:	[ internal use only ]
881  *
882  *	Asserts that the starting and ending region
883  *	addresses fall within the valid range of the map.
884  */
885 #define	VM_MAP_RANGE_CHECK(map, start, end)		\
886 		{					\
887 		if (start < vm_map_min(map))		\
888 			start = vm_map_min(map);	\
889 		if (end > vm_map_max(map))		\
890 			end = vm_map_max(map);		\
891 		if (start > end)			\
892 			start = end;			\
893 		}
894 
895 /*
896  *	vm_map_submap:		[ kernel use only ]
897  *
898  *	Mark the given range as handled by a subordinate map.
899  *
900  *	This range must have been created with vm_map_find,
901  *	and no other operations may have been performed on this
902  *	range prior to calling vm_map_submap.
903  *
904  *	Only a limited number of operations can be performed
905  *	within this rage after calling vm_map_submap:
906  *		vm_fault
907  *	[Don't try vm_map_copy!]
908  *
909  *	To remove a submapping, one must first remove the
910  *	range from the superior map, and then destroy the
911  *	submap (if desired).  [Better yet, don't try it.]
912  */
913 int
914 vm_map_submap(map, start, end, submap)
915 	vm_map_t map;
916 	vm_offset_t start;
917 	vm_offset_t end;
918 	vm_map_t submap;
919 {
920 	vm_map_entry_t entry;
921 	int result = KERN_INVALID_ARGUMENT;
922 
923 	vm_map_lock(map);
924 
925 	VM_MAP_RANGE_CHECK(map, start, end);
926 
927 	if (vm_map_lookup_entry(map, start, &entry)) {
928 		vm_map_clip_start(map, entry, start);
929 	} else
930 		entry = entry->next;
931 
932 	vm_map_clip_end(map, entry, end);
933 
934 	if ((entry->start == start) && (entry->end == end) &&
935 	    ((entry->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_COW)) == 0) &&
936 	    (entry->object.vm_object == NULL)) {
937 		entry->object.sub_map = submap;
938 		entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
939 		result = KERN_SUCCESS;
940 	}
941 	vm_map_unlock(map);
942 
943 	return (result);
944 }
945 
946 /*
947  *	vm_map_protect:
948  *
949  *	Sets the protection of the specified address
950  *	region in the target map.  If "set_max" is
951  *	specified, the maximum protection is to be set;
952  *	otherwise, only the current protection is affected.
953  */
954 int
955 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
956 	       vm_prot_t new_prot, boolean_t set_max)
957 {
958 	vm_map_entry_t current;
959 	vm_map_entry_t entry;
960 
961 	vm_map_lock(map);
962 
963 	VM_MAP_RANGE_CHECK(map, start, end);
964 
965 	if (vm_map_lookup_entry(map, start, &entry)) {
966 		vm_map_clip_start(map, entry, start);
967 	} else {
968 		entry = entry->next;
969 	}
970 
971 	/*
972 	 * Make a first pass to check for protection violations.
973 	 */
974 
975 	current = entry;
976 	while ((current != &map->header) && (current->start < end)) {
977 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
978 			vm_map_unlock(map);
979 			return (KERN_INVALID_ARGUMENT);
980 		}
981 		if ((new_prot & current->max_protection) != new_prot) {
982 			vm_map_unlock(map);
983 			return (KERN_PROTECTION_FAILURE);
984 		}
985 		current = current->next;
986 	}
987 
988 	/*
989 	 * Go back and fix up protections. [Note that clipping is not
990 	 * necessary the second time.]
991 	 */
992 
993 	current = entry;
994 
995 	while ((current != &map->header) && (current->start < end)) {
996 		vm_prot_t old_prot;
997 
998 		vm_map_clip_end(map, current, end);
999 
1000 		old_prot = current->protection;
1001 		if (set_max)
1002 			current->protection =
1003 			    (current->max_protection = new_prot) &
1004 			    old_prot;
1005 		else
1006 			current->protection = new_prot;
1007 
1008 		/*
1009 		 * Update physical map if necessary. Worry about copy-on-write
1010 		 * here -- CHECK THIS XXX
1011 		 */
1012 
1013 		if (current->protection != old_prot) {
1014 #define MASK(entry)	(((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1015 							VM_PROT_ALL)
1016 
1017 			if (current->eflags & MAP_ENTRY_IS_A_MAP) {
1018 				vm_map_entry_t share_entry;
1019 				vm_offset_t share_end;
1020 
1021 				vm_map_lock(current->object.share_map);
1022 				(void) vm_map_lookup_entry(
1023 				    current->object.share_map,
1024 				    current->offset,
1025 				    &share_entry);
1026 				share_end = current->offset +
1027 				    (current->end - current->start);
1028 				while ((share_entry !=
1029 					&current->object.share_map->header) &&
1030 				    (share_entry->start < share_end)) {
1031 
1032 					pmap_protect(map->pmap,
1033 					    (qmax(share_entry->start,
1034 						    current->offset) -
1035 						current->offset +
1036 						current->start),
1037 					    min(share_entry->end,
1038 						share_end) -
1039 					    current->offset +
1040 					    current->start,
1041 					    current->protection &
1042 					    MASK(share_entry));
1043 
1044 					share_entry = share_entry->next;
1045 				}
1046 				vm_map_unlock(current->object.share_map);
1047 			} else
1048 				pmap_protect(map->pmap, current->start,
1049 				    current->end,
1050 				    current->protection & MASK(entry));
1051 #undef	MASK
1052 		}
1053 
1054 		vm_map_simplify_entry(map, current);
1055 
1056 		current = current->next;
1057 	}
1058 
1059 	map->timestamp++;
1060 	vm_map_unlock(map);
1061 	return (KERN_SUCCESS);
1062 }
1063 
1064 /*
1065  *	vm_map_madvise:
1066  *
1067  * 	This routine traverses a processes map handling the madvise
1068  *	system call.
1069  */
1070 void
1071 vm_map_madvise(map, pmap, start, end, advise)
1072 	vm_map_t map;
1073 	pmap_t pmap;
1074 	vm_offset_t start, end;
1075 	int advise;
1076 {
1077 	vm_map_entry_t current;
1078 	vm_map_entry_t entry;
1079 
1080 	vm_map_lock(map);
1081 
1082 	VM_MAP_RANGE_CHECK(map, start, end);
1083 
1084 	if (vm_map_lookup_entry(map, start, &entry)) {
1085 		vm_map_clip_start(map, entry, start);
1086 	} else
1087 		entry = entry->next;
1088 
1089 	for(current = entry;
1090 		(current != &map->header) && (current->start < end);
1091 		current = current->next) {
1092 		vm_size_t size;
1093 
1094 		if (current->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP)) {
1095 			continue;
1096 		}
1097 
1098 		vm_map_clip_end(map, current, end);
1099 		size = current->end - current->start;
1100 
1101 		/*
1102 		 * Create an object if needed
1103 		 */
1104 		if (current->object.vm_object == NULL) {
1105 			vm_object_t object;
1106 			if ((advise == MADV_FREE) || (advise == MADV_DONTNEED))
1107 				continue;
1108 			object = vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(size));
1109 			current->object.vm_object = object;
1110 			current->offset = 0;
1111 		}
1112 
1113 		switch (advise) {
1114 	case MADV_NORMAL:
1115 			current->object.vm_object->behavior = OBJ_NORMAL;
1116 			break;
1117 	case MADV_SEQUENTIAL:
1118 			current->object.vm_object->behavior = OBJ_SEQUENTIAL;
1119 			break;
1120 	case MADV_RANDOM:
1121 			current->object.vm_object->behavior = OBJ_RANDOM;
1122 			break;
1123 	/*
1124 	 * Right now, we could handle DONTNEED and WILLNEED with common code.
1125 	 * They are mostly the same, except for the potential async reads (NYI).
1126 	 */
1127 	case MADV_FREE:
1128 	case MADV_DONTNEED:
1129 			{
1130 				vm_pindex_t pindex;
1131 				int count;
1132 				pindex = OFF_TO_IDX(current->offset);
1133 				count = OFF_TO_IDX(size);
1134 				/*
1135 				 * MADV_DONTNEED removes the page from all
1136 				 * pmaps, so pmap_remove is not necessary.
1137 				 */
1138 				vm_object_madvise(current->object.vm_object,
1139 					pindex, count, advise);
1140 			}
1141 			break;
1142 
1143 	case MADV_WILLNEED:
1144 			{
1145 				vm_pindex_t pindex;
1146 				int count;
1147 				pindex = OFF_TO_IDX(current->offset);
1148 				count = OFF_TO_IDX(size);
1149 				vm_object_madvise(current->object.vm_object,
1150 					pindex, count, advise);
1151 				pmap_object_init_pt(pmap, current->start,
1152 					current->object.vm_object, pindex,
1153 					(count << PAGE_SHIFT), 0);
1154 			}
1155 			break;
1156 
1157 	default:
1158 			break;
1159 		}
1160 	}
1161 
1162 	map->timestamp++;
1163 	vm_map_simplify_entry(map, entry);
1164 	vm_map_unlock(map);
1165 	return;
1166 }
1167 
1168 
1169 /*
1170  *	vm_map_inherit:
1171  *
1172  *	Sets the inheritance of the specified address
1173  *	range in the target map.  Inheritance
1174  *	affects how the map will be shared with
1175  *	child maps at the time of vm_map_fork.
1176  */
1177 int
1178 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
1179 	       vm_inherit_t new_inheritance)
1180 {
1181 	vm_map_entry_t entry;
1182 	vm_map_entry_t temp_entry;
1183 
1184 	switch (new_inheritance) {
1185 	case VM_INHERIT_NONE:
1186 	case VM_INHERIT_COPY:
1187 	case VM_INHERIT_SHARE:
1188 		break;
1189 	default:
1190 		return (KERN_INVALID_ARGUMENT);
1191 	}
1192 
1193 	vm_map_lock(map);
1194 
1195 	VM_MAP_RANGE_CHECK(map, start, end);
1196 
1197 	if (vm_map_lookup_entry(map, start, &temp_entry)) {
1198 		entry = temp_entry;
1199 		vm_map_clip_start(map, entry, start);
1200 	} else
1201 		entry = temp_entry->next;
1202 
1203 	while ((entry != &map->header) && (entry->start < end)) {
1204 		vm_map_clip_end(map, entry, end);
1205 
1206 		entry->inheritance = new_inheritance;
1207 
1208 		entry = entry->next;
1209 	}
1210 
1211 	vm_map_simplify_entry(map, temp_entry);
1212 	map->timestamp++;
1213 	vm_map_unlock(map);
1214 	return (KERN_SUCCESS);
1215 }
1216 
1217 /*
1218  * Implement the semantics of mlock
1219  */
1220 int
1221 vm_map_user_pageable(map, start, end, new_pageable)
1222 	vm_map_t map;
1223 	vm_offset_t start;
1224 	vm_offset_t end;
1225 	boolean_t new_pageable;
1226 {
1227 	vm_map_entry_t entry;
1228 	vm_map_entry_t start_entry;
1229 	vm_offset_t estart;
1230 	int rv;
1231 
1232 	vm_map_lock(map);
1233 	VM_MAP_RANGE_CHECK(map, start, end);
1234 
1235 	if (vm_map_lookup_entry(map, start, &start_entry) == FALSE) {
1236 		vm_map_unlock(map);
1237 		return (KERN_INVALID_ADDRESS);
1238 	}
1239 
1240 	if (new_pageable) {
1241 
1242 		entry = start_entry;
1243 		vm_map_clip_start(map, entry, start);
1244 
1245 		/*
1246 		 * Now decrement the wiring count for each region. If a region
1247 		 * becomes completely unwired, unwire its physical pages and
1248 		 * mappings.
1249 		 */
1250 		vm_map_set_recursive(map);
1251 
1252 		entry = start_entry;
1253 		while ((entry != &map->header) && (entry->start < end)) {
1254 			if (entry->eflags & MAP_ENTRY_USER_WIRED) {
1255 				vm_map_clip_end(map, entry, end);
1256 				entry->eflags &= ~MAP_ENTRY_USER_WIRED;
1257 				entry->wired_count--;
1258 				if (entry->wired_count == 0)
1259 					vm_fault_unwire(map, entry->start, entry->end);
1260 			}
1261 			vm_map_simplify_entry(map,entry);
1262 			entry = entry->next;
1263 		}
1264 		vm_map_clear_recursive(map);
1265 	} else {
1266 
1267 		entry = start_entry;
1268 
1269 		while ((entry != &map->header) && (entry->start < end)) {
1270 
1271 			if (entry->eflags & MAP_ENTRY_USER_WIRED) {
1272 				entry = entry->next;
1273 				continue;
1274 			}
1275 
1276 			if (entry->wired_count != 0) {
1277 				entry->wired_count++;
1278 				entry->eflags |= MAP_ENTRY_USER_WIRED;
1279 				entry = entry->next;
1280 				continue;
1281 			}
1282 
1283 			/* Here on entry being newly wired */
1284 
1285 			if ((entry->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP)) == 0) {
1286 				int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY;
1287 				if (copyflag && ((entry->protection & VM_PROT_WRITE) != 0)) {
1288 
1289 					vm_object_shadow(&entry->object.vm_object,
1290 					    &entry->offset,
1291 					    atop(entry->end - entry->start));
1292 					entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
1293 
1294 				} else if (entry->object.vm_object == NULL) {
1295 
1296 					entry->object.vm_object =
1297 					    vm_object_allocate(OBJT_DEFAULT,
1298 						atop(entry->end - entry->start));
1299 					entry->offset = (vm_offset_t) 0;
1300 
1301 				}
1302 				default_pager_convert_to_swapq(entry->object.vm_object);
1303 			}
1304 
1305 			vm_map_clip_start(map, entry, start);
1306 			vm_map_clip_end(map, entry, end);
1307 
1308 			entry->wired_count++;
1309 			entry->eflags |= MAP_ENTRY_USER_WIRED;
1310 			estart = entry->start;
1311 
1312 			/* First we need to allow map modifications */
1313 			vm_map_set_recursive(map);
1314 			vm_map_lock_downgrade(map);
1315 			map->timestamp++;
1316 
1317 			rv = vm_fault_user_wire(map, entry->start, entry->end);
1318 			if (rv) {
1319 
1320 				entry->wired_count--;
1321 				entry->eflags &= ~MAP_ENTRY_USER_WIRED;
1322 
1323 				vm_map_clear_recursive(map);
1324 				vm_map_unlock(map);
1325 
1326 				(void) vm_map_user_pageable(map, start, entry->start, TRUE);
1327 				return rv;
1328 			}
1329 
1330 			vm_map_clear_recursive(map);
1331 			if (vm_map_lock_upgrade(map)) {
1332 				vm_map_lock(map);
1333 				if (vm_map_lookup_entry(map, estart, &entry)
1334 				    == FALSE) {
1335 					vm_map_unlock(map);
1336 					(void) vm_map_user_pageable(map,
1337 								    start,
1338 								    estart,
1339 								    TRUE);
1340 					return (KERN_INVALID_ADDRESS);
1341 				}
1342 			}
1343 			vm_map_simplify_entry(map,entry);
1344 		}
1345 	}
1346 	map->timestamp++;
1347 	vm_map_unlock(map);
1348 	return KERN_SUCCESS;
1349 }
1350 
1351 /*
1352  *	vm_map_pageable:
1353  *
1354  *	Sets the pageability of the specified address
1355  *	range in the target map.  Regions specified
1356  *	as not pageable require locked-down physical
1357  *	memory and physical page maps.
1358  *
1359  *	The map must not be locked, but a reference
1360  *	must remain to the map throughout the call.
1361  */
1362 int
1363 vm_map_pageable(map, start, end, new_pageable)
1364 	vm_map_t map;
1365 	vm_offset_t start;
1366 	vm_offset_t end;
1367 	boolean_t new_pageable;
1368 {
1369 	vm_map_entry_t entry;
1370 	vm_map_entry_t start_entry;
1371 	vm_offset_t failed = 0;
1372 	int rv;
1373 
1374 	vm_map_lock(map);
1375 
1376 	VM_MAP_RANGE_CHECK(map, start, end);
1377 
1378 	/*
1379 	 * Only one pageability change may take place at one time, since
1380 	 * vm_fault assumes it will be called only once for each
1381 	 * wiring/unwiring.  Therefore, we have to make sure we're actually
1382 	 * changing the pageability for the entire region.  We do so before
1383 	 * making any changes.
1384 	 */
1385 
1386 	if (vm_map_lookup_entry(map, start, &start_entry) == FALSE) {
1387 		vm_map_unlock(map);
1388 		return (KERN_INVALID_ADDRESS);
1389 	}
1390 	entry = start_entry;
1391 
1392 	/*
1393 	 * Actions are rather different for wiring and unwiring, so we have
1394 	 * two separate cases.
1395 	 */
1396 
1397 	if (new_pageable) {
1398 
1399 		vm_map_clip_start(map, entry, start);
1400 
1401 		/*
1402 		 * Unwiring.  First ensure that the range to be unwired is
1403 		 * really wired down and that there are no holes.
1404 		 */
1405 		while ((entry != &map->header) && (entry->start < end)) {
1406 
1407 			if (entry->wired_count == 0 ||
1408 			    (entry->end < end &&
1409 				(entry->next == &map->header ||
1410 				    entry->next->start > entry->end))) {
1411 				vm_map_unlock(map);
1412 				return (KERN_INVALID_ARGUMENT);
1413 			}
1414 			entry = entry->next;
1415 		}
1416 
1417 		/*
1418 		 * Now decrement the wiring count for each region. If a region
1419 		 * becomes completely unwired, unwire its physical pages and
1420 		 * mappings.
1421 		 */
1422 		vm_map_set_recursive(map);
1423 
1424 		entry = start_entry;
1425 		while ((entry != &map->header) && (entry->start < end)) {
1426 			vm_map_clip_end(map, entry, end);
1427 
1428 			entry->wired_count--;
1429 			if (entry->wired_count == 0)
1430 				vm_fault_unwire(map, entry->start, entry->end);
1431 
1432 			entry = entry->next;
1433 		}
1434 		vm_map_simplify_entry(map, start_entry);
1435 		vm_map_clear_recursive(map);
1436 	} else {
1437 		/*
1438 		 * Wiring.  We must do this in two passes:
1439 		 *
1440 		 * 1.  Holding the write lock, we create any shadow or zero-fill
1441 		 * objects that need to be created. Then we clip each map
1442 		 * entry to the region to be wired and increment its wiring
1443 		 * count.  We create objects before clipping the map entries
1444 		 * to avoid object proliferation.
1445 		 *
1446 		 * 2.  We downgrade to a read lock, and call vm_fault_wire to
1447 		 * fault in the pages for any newly wired area (wired_count is
1448 		 * 1).
1449 		 *
1450 		 * Downgrading to a read lock for vm_fault_wire avoids a possible
1451 		 * deadlock with another process that may have faulted on one
1452 		 * of the pages to be wired (it would mark the page busy,
1453 		 * blocking us, then in turn block on the map lock that we
1454 		 * hold).  Because of problems in the recursive lock package,
1455 		 * we cannot upgrade to a write lock in vm_map_lookup.  Thus,
1456 		 * any actions that require the write lock must be done
1457 		 * beforehand.  Because we keep the read lock on the map, the
1458 		 * copy-on-write status of the entries we modify here cannot
1459 		 * change.
1460 		 */
1461 
1462 		/*
1463 		 * Pass 1.
1464 		 */
1465 		while ((entry != &map->header) && (entry->start < end)) {
1466 			if (entry->wired_count == 0) {
1467 
1468 				/*
1469 				 * Perform actions of vm_map_lookup that need
1470 				 * the write lock on the map: create a shadow
1471 				 * object for a copy-on-write region, or an
1472 				 * object for a zero-fill region.
1473 				 *
1474 				 * We don't have to do this for entries that
1475 				 * point to sharing maps, because we won't
1476 				 * hold the lock on the sharing map.
1477 				 */
1478 				if ((entry->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP)) == 0) {
1479 					int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY;
1480 					if (copyflag &&
1481 					    ((entry->protection & VM_PROT_WRITE) != 0)) {
1482 
1483 						vm_object_shadow(&entry->object.vm_object,
1484 						    &entry->offset,
1485 						    atop(entry->end - entry->start));
1486 						entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
1487 					} else if (entry->object.vm_object == NULL) {
1488 						entry->object.vm_object =
1489 						    vm_object_allocate(OBJT_DEFAULT,
1490 							atop(entry->end - entry->start));
1491 						entry->offset = (vm_offset_t) 0;
1492 					}
1493 					default_pager_convert_to_swapq(entry->object.vm_object);
1494 				}
1495 			}
1496 			vm_map_clip_start(map, entry, start);
1497 			vm_map_clip_end(map, entry, end);
1498 			entry->wired_count++;
1499 
1500 			/*
1501 			 * Check for holes
1502 			 */
1503 			if (entry->end < end &&
1504 			    (entry->next == &map->header ||
1505 				entry->next->start > entry->end)) {
1506 				/*
1507 				 * Found one.  Object creation actions do not
1508 				 * need to be undone, but the wired counts
1509 				 * need to be restored.
1510 				 */
1511 				while (entry != &map->header && entry->end > start) {
1512 					entry->wired_count--;
1513 					entry = entry->prev;
1514 				}
1515 				map->timestamp++;
1516 				vm_map_unlock(map);
1517 				return (KERN_INVALID_ARGUMENT);
1518 			}
1519 			entry = entry->next;
1520 		}
1521 
1522 		/*
1523 		 * Pass 2.
1524 		 */
1525 
1526 		/*
1527 		 * HACK HACK HACK HACK
1528 		 *
1529 		 * If we are wiring in the kernel map or a submap of it,
1530 		 * unlock the map to avoid deadlocks.  We trust that the
1531 		 * kernel is well-behaved, and therefore will not do
1532 		 * anything destructive to this region of the map while
1533 		 * we have it unlocked.  We cannot trust user processes
1534 		 * to do the same.
1535 		 *
1536 		 * HACK HACK HACK HACK
1537 		 */
1538 		if (vm_map_pmap(map) == kernel_pmap) {
1539 			vm_map_unlock(map);	/* trust me ... */
1540 		} else {
1541 			vm_map_set_recursive(map);
1542 			vm_map_lock_downgrade(map);
1543 		}
1544 
1545 		rv = 0;
1546 		entry = start_entry;
1547 		while (entry != &map->header && entry->start < end) {
1548 			/*
1549 			 * If vm_fault_wire fails for any page we need to undo
1550 			 * what has been done.  We decrement the wiring count
1551 			 * for those pages which have not yet been wired (now)
1552 			 * and unwire those that have (later).
1553 			 *
1554 			 * XXX this violates the locking protocol on the map,
1555 			 * needs to be fixed.
1556 			 */
1557 			if (rv)
1558 				entry->wired_count--;
1559 			else if (entry->wired_count == 1) {
1560 				rv = vm_fault_wire(map, entry->start, entry->end);
1561 				if (rv) {
1562 					failed = entry->start;
1563 					entry->wired_count--;
1564 				}
1565 			}
1566 			entry = entry->next;
1567 		}
1568 
1569 		if (vm_map_pmap(map) == kernel_pmap) {
1570 			vm_map_lock(map);
1571 		} else {
1572 			vm_map_clear_recursive(map);
1573 		}
1574 		if (rv) {
1575 			vm_map_unlock(map);
1576 			(void) vm_map_pageable(map, start, failed, TRUE);
1577 			return (rv);
1578 		}
1579 		vm_map_simplify_entry(map, start_entry);
1580 	}
1581 
1582 	vm_map_unlock(map);
1583 
1584 	map->timestamp++;
1585 	return (KERN_SUCCESS);
1586 }
1587 
1588 /*
1589  * vm_map_clean
1590  *
1591  * Push any dirty cached pages in the address range to their pager.
1592  * If syncio is TRUE, dirty pages are written synchronously.
1593  * If invalidate is TRUE, any cached pages are freed as well.
1594  *
1595  * Returns an error if any part of the specified range is not mapped.
1596  */
1597 int
1598 vm_map_clean(map, start, end, syncio, invalidate)
1599 	vm_map_t map;
1600 	vm_offset_t start;
1601 	vm_offset_t end;
1602 	boolean_t syncio;
1603 	boolean_t invalidate;
1604 {
1605 	vm_map_entry_t current;
1606 	vm_map_entry_t entry;
1607 	vm_size_t size;
1608 	vm_object_t object;
1609 	vm_ooffset_t offset;
1610 
1611 	vm_map_lock_read(map);
1612 	VM_MAP_RANGE_CHECK(map, start, end);
1613 	if (!vm_map_lookup_entry(map, start, &entry)) {
1614 		vm_map_unlock_read(map);
1615 		return (KERN_INVALID_ADDRESS);
1616 	}
1617 	/*
1618 	 * Make a first pass to check for holes.
1619 	 */
1620 	for (current = entry; current->start < end; current = current->next) {
1621 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1622 			vm_map_unlock_read(map);
1623 			return (KERN_INVALID_ARGUMENT);
1624 		}
1625 		if (end > current->end &&
1626 		    (current->next == &map->header ||
1627 			current->end != current->next->start)) {
1628 			vm_map_unlock_read(map);
1629 			return (KERN_INVALID_ADDRESS);
1630 		}
1631 	}
1632 
1633 	if (invalidate)
1634 		pmap_remove(vm_map_pmap(map), start, end);
1635 	/*
1636 	 * Make a second pass, cleaning/uncaching pages from the indicated
1637 	 * objects as we go.
1638 	 */
1639 	for (current = entry; current->start < end; current = current->next) {
1640 		offset = current->offset + (start - current->start);
1641 		size = (end <= current->end ? end : current->end) - start;
1642 		if (current->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP)) {
1643 			vm_map_t smap;
1644 			vm_map_entry_t tentry;
1645 			vm_size_t tsize;
1646 
1647 			smap = current->object.share_map;
1648 			vm_map_lock_read(smap);
1649 			(void) vm_map_lookup_entry(smap, offset, &tentry);
1650 			tsize = tentry->end - offset;
1651 			if (tsize < size)
1652 				size = tsize;
1653 			object = tentry->object.vm_object;
1654 			offset = tentry->offset + (offset - tentry->start);
1655 			vm_map_unlock_read(smap);
1656 		} else {
1657 			object = current->object.vm_object;
1658 		}
1659 		/*
1660 		 * Note that there is absolutely no sense in writing out
1661 		 * anonymous objects, so we track down the vnode object
1662 		 * to write out.
1663 		 * We invalidate (remove) all pages from the address space
1664 		 * anyway, for semantic correctness.
1665 		 */
1666 		while (object->backing_object) {
1667 			object = object->backing_object;
1668 			offset += object->backing_object_offset;
1669 			if (object->size < OFF_TO_IDX( offset + size))
1670 				size = IDX_TO_OFF(object->size) - offset;
1671 		}
1672 		if (object && (object->type == OBJT_VNODE)) {
1673 			/*
1674 			 * Flush pages if writing is allowed. XXX should we continue
1675 			 * on an error?
1676 			 *
1677 			 * XXX Doing async I/O and then removing all the pages from
1678 			 *     the object before it completes is probably a very bad
1679 			 *     idea.
1680 			 */
1681 			if (current->protection & VM_PROT_WRITE) {
1682 				int flags;
1683 				if (object->type == OBJT_VNODE)
1684 					vn_lock(object->handle, LK_EXCLUSIVE | LK_RETRY, curproc);
1685 				flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
1686 				flags |= invalidate ? OBJPC_INVAL : 0;
1687 		   	    vm_object_page_clean(object,
1688 					OFF_TO_IDX(offset),
1689 					OFF_TO_IDX(offset + size + PAGE_MASK),
1690 					flags);
1691 				if (invalidate) {
1692 					vm_object_pip_wait(object, "objmcl");
1693 					vm_object_page_remove(object,
1694 						OFF_TO_IDX(offset),
1695 						OFF_TO_IDX(offset + size + PAGE_MASK),
1696 						FALSE);
1697 				}
1698 				if (object->type == OBJT_VNODE)
1699 					VOP_UNLOCK(object->handle, 0, curproc);
1700 			}
1701 		}
1702 		start += size;
1703 	}
1704 
1705 	vm_map_unlock_read(map);
1706 	return (KERN_SUCCESS);
1707 }
1708 
1709 /*
1710  *	vm_map_entry_unwire:	[ internal use only ]
1711  *
1712  *	Make the region specified by this entry pageable.
1713  *
1714  *	The map in question should be locked.
1715  *	[This is the reason for this routine's existence.]
1716  */
1717 static void
1718 vm_map_entry_unwire(map, entry)
1719 	vm_map_t map;
1720 	vm_map_entry_t entry;
1721 {
1722 	vm_fault_unwire(map, entry->start, entry->end);
1723 	entry->wired_count = 0;
1724 }
1725 
1726 /*
1727  *	vm_map_entry_delete:	[ internal use only ]
1728  *
1729  *	Deallocate the given entry from the target map.
1730  */
1731 static void
1732 vm_map_entry_delete(map, entry)
1733 	vm_map_t map;
1734 	vm_map_entry_t entry;
1735 {
1736 	vm_map_entry_unlink(map, entry);
1737 	map->size -= entry->end - entry->start;
1738 
1739 	if ((entry->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP)) == 0) {
1740 		vm_object_deallocate(entry->object.vm_object);
1741 	}
1742 
1743 	vm_map_entry_dispose(map, entry);
1744 }
1745 
1746 /*
1747  *	vm_map_delete:	[ internal use only ]
1748  *
1749  *	Deallocates the given address range from the target
1750  *	map.
1751  *
1752  *	When called with a sharing map, removes pages from
1753  *	that region from all physical maps.
1754  */
1755 int
1756 vm_map_delete(map, start, end)
1757 	vm_map_t map;
1758 	vm_offset_t start;
1759 	vm_offset_t end;
1760 {
1761 	vm_object_t object;
1762 	vm_map_entry_t entry;
1763 	vm_map_entry_t first_entry;
1764 
1765 	/*
1766 	 * Find the start of the region, and clip it
1767 	 */
1768 
1769 	if (!vm_map_lookup_entry(map, start, &first_entry)) {
1770 		entry = first_entry->next;
1771 		object = entry->object.vm_object;
1772 		if (object && (object->ref_count == 1) && (object->shadow_count == 0))
1773 				object->flags |= OBJ_ONEMAPPING;
1774 	} else {
1775 		entry = first_entry;
1776 		vm_map_clip_start(map, entry, start);
1777 		/*
1778 		 * Fix the lookup hint now, rather than each time though the
1779 		 * loop.
1780 		 */
1781 		SAVE_HINT(map, entry->prev);
1782 	}
1783 
1784 	/*
1785 	 * Save the free space hint
1786 	 */
1787 
1788 	if (entry == &map->header) {
1789 		map->first_free = &map->header;
1790 	} else if (map->first_free->start >= start) {
1791 		map->first_free = entry->prev;
1792 	}
1793 
1794 	/*
1795 	 * Step through all entries in this region
1796 	 */
1797 
1798 	while ((entry != &map->header) && (entry->start < end)) {
1799 		vm_map_entry_t next;
1800 		vm_offset_t s, e;
1801 		vm_pindex_t offidxstart, offidxend, count;
1802 
1803 		vm_map_clip_end(map, entry, end);
1804 
1805 		s = entry->start;
1806 		e = entry->end;
1807 		next = entry->next;
1808 
1809 		offidxstart = OFF_TO_IDX(entry->offset);
1810 		count = OFF_TO_IDX(e - s);
1811 		object = entry->object.vm_object;
1812 
1813 		/*
1814 		 * Unwire before removing addresses from the pmap; otherwise,
1815 		 * unwiring will put the entries back in the pmap.
1816 		 */
1817 		if (entry->wired_count != 0) {
1818 			vm_map_entry_unwire(map, entry);
1819 		}
1820 
1821 		offidxend = offidxstart + count;
1822 		/*
1823 		 * If this is a sharing map, we must remove *all* references
1824 		 * to this data, since we can't find all of the physical maps
1825 		 * which are sharing it.
1826 		 */
1827 
1828 		if ((object == kernel_object) || (object == kmem_object)) {
1829 			vm_object_page_remove(object, offidxstart, offidxend, FALSE);
1830 		} else if (!map->is_main_map) {
1831 			vm_object_pmap_remove(object, offidxstart, offidxend);
1832 		} else {
1833 			pmap_remove(map->pmap, s, e);
1834 			if (object &&
1835 				((object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) &&
1836 				((object->type == OBJT_SWAP) || (object->type == OBJT_DEFAULT))) {
1837 				vm_object_collapse(object);
1838 				vm_object_page_remove(object, offidxstart, offidxend, FALSE);
1839 				if (object->type == OBJT_SWAP) {
1840 					swap_pager_freespace(object, offidxstart, count);
1841 				}
1842 
1843 				if ((offidxend >= object->size) &&
1844 					(offidxstart < object->size)) {
1845 						object->size = offidxstart;
1846 				}
1847 			}
1848 		}
1849 
1850 		/*
1851 		 * Delete the entry (which may delete the object) only after
1852 		 * removing all pmap entries pointing to its pages.
1853 		 * (Otherwise, its page frames may be reallocated, and any
1854 		 * modify bits will be set in the wrong object!)
1855 		 */
1856 		vm_map_entry_delete(map, entry);
1857 		entry = next;
1858 	}
1859 	return (KERN_SUCCESS);
1860 }
1861 
1862 /*
1863  *	vm_map_remove:
1864  *
1865  *	Remove the given address range from the target map.
1866  *	This is the exported form of vm_map_delete.
1867  */
1868 int
1869 vm_map_remove(map, start, end)
1870 	vm_map_t map;
1871 	vm_offset_t start;
1872 	vm_offset_t end;
1873 {
1874 	int result, s = 0;
1875 
1876 	if (map == kmem_map || map == mb_map)
1877 		s = splvm();
1878 
1879 	vm_map_lock(map);
1880 	VM_MAP_RANGE_CHECK(map, start, end);
1881 	result = vm_map_delete(map, start, end);
1882 	vm_map_unlock(map);
1883 
1884 	if (map == kmem_map || map == mb_map)
1885 		splx(s);
1886 
1887 	return (result);
1888 }
1889 
1890 /*
1891  *	vm_map_check_protection:
1892  *
1893  *	Assert that the target map allows the specified
1894  *	privilege on the entire address region given.
1895  *	The entire region must be allocated.
1896  */
1897 boolean_t
1898 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
1899 			vm_prot_t protection)
1900 {
1901 	vm_map_entry_t entry;
1902 	vm_map_entry_t tmp_entry;
1903 
1904 	if (!vm_map_lookup_entry(map, start, &tmp_entry)) {
1905 		return (FALSE);
1906 	}
1907 	entry = tmp_entry;
1908 
1909 	while (start < end) {
1910 		if (entry == &map->header) {
1911 			return (FALSE);
1912 		}
1913 		/*
1914 		 * No holes allowed!
1915 		 */
1916 
1917 		if (start < entry->start) {
1918 			return (FALSE);
1919 		}
1920 		/*
1921 		 * Check protection associated with entry.
1922 		 */
1923 
1924 		if ((entry->protection & protection) != protection) {
1925 			return (FALSE);
1926 		}
1927 		/* go to next entry */
1928 
1929 		start = entry->end;
1930 		entry = entry->next;
1931 	}
1932 	return (TRUE);
1933 }
1934 
1935 /*
1936  * Split the pages in a map entry into a new object.  This affords
1937  * easier removal of unused pages, and keeps object inheritance from
1938  * being a negative impact on memory usage.
1939  */
1940 static void
1941 vm_map_split(entry)
1942 	vm_map_entry_t entry;
1943 {
1944 	vm_page_t m;
1945 	vm_object_t orig_object, new_object, source;
1946 	vm_offset_t s, e;
1947 	vm_pindex_t offidxstart, offidxend, idx;
1948 	vm_size_t size;
1949 	vm_ooffset_t offset;
1950 
1951 	orig_object = entry->object.vm_object;
1952 	if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
1953 		return;
1954 	if (orig_object->ref_count <= 1)
1955 		return;
1956 
1957 	offset = entry->offset;
1958 	s = entry->start;
1959 	e = entry->end;
1960 
1961 	offidxstart = OFF_TO_IDX(offset);
1962 	offidxend = offidxstart + OFF_TO_IDX(e - s);
1963 	size = offidxend - offidxstart;
1964 
1965 	new_object = vm_pager_allocate(orig_object->type,
1966 		NULL, size, VM_PROT_ALL, 0LL);
1967 	if (new_object == NULL)
1968 		return;
1969 
1970 	source = orig_object->backing_object;
1971 	if (source != NULL) {
1972 		vm_object_reference(source);	/* Referenced by new_object */
1973 		TAILQ_INSERT_TAIL(&source->shadow_head,
1974 				  new_object, shadow_list);
1975 		source->flags &= ~OBJ_ONEMAPPING;
1976 		new_object->backing_object_offset =
1977 			orig_object->backing_object_offset + offidxstart;
1978 		new_object->backing_object = source;
1979 		source->shadow_count++;
1980 		source->generation++;
1981 	}
1982 
1983 	for (idx = 0; idx < size; idx++) {
1984 		vm_page_t m;
1985 
1986 	retry:
1987 		m = vm_page_lookup(orig_object, offidxstart + idx);
1988 		if (m == NULL)
1989 			continue;
1990 		if (m->flags & PG_BUSY) {
1991 			m->flags |= PG_WANTED;
1992 			tsleep(m, PVM, "spltwt", 0);
1993 			goto retry;
1994 		}
1995 
1996 		m->flags |= PG_BUSY;
1997 		vm_page_protect(m, VM_PROT_NONE);
1998 		vm_page_rename(m, new_object, idx);
1999 		m->dirty = VM_PAGE_BITS_ALL;
2000 		m->flags |= PG_BUSY;
2001 	}
2002 
2003 	if (orig_object->type == OBJT_SWAP) {
2004 		vm_object_pip_add(orig_object, 1);
2005 		/*
2006 		 * copy orig_object pages into new_object
2007 		 * and destroy unneeded pages in
2008 		 * shadow object.
2009 		 */
2010 		swap_pager_copy(orig_object, OFF_TO_IDX(orig_object->paging_offset),
2011 		    new_object, OFF_TO_IDX(new_object->paging_offset),
2012 			offidxstart, 0);
2013 		vm_object_pip_wakeup(orig_object);
2014 	}
2015 
2016 	for (idx = 0; idx < size; idx++) {
2017 		m = vm_page_lookup(new_object, idx);
2018 		if (m) {
2019 			PAGE_WAKEUP(m);
2020 		}
2021 	}
2022 
2023 	entry->object.vm_object = new_object;
2024 	entry->offset = 0LL;
2025 	vm_object_deallocate(orig_object);
2026 }
2027 
2028 /*
2029  *	vm_map_copy_entry:
2030  *
2031  *	Copies the contents of the source entry to the destination
2032  *	entry.  The entries *must* be aligned properly.
2033  */
2034 static void
2035 vm_map_copy_entry(src_map, dst_map, src_entry, dst_entry)
2036 	vm_map_t src_map, dst_map;
2037 	vm_map_entry_t src_entry, dst_entry;
2038 {
2039 	vm_object_t src_object;
2040 
2041 	if ((dst_entry->eflags|src_entry->eflags) &
2042 		(MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP))
2043 		return;
2044 
2045 	if (src_entry->wired_count == 0) {
2046 
2047 		/*
2048 		 * If the source entry is marked needs_copy, it is already
2049 		 * write-protected.
2050 		 */
2051 		if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
2052 			pmap_protect(src_map->pmap,
2053 			    src_entry->start,
2054 			    src_entry->end,
2055 			    src_entry->protection & ~VM_PROT_WRITE);
2056 		}
2057 
2058 		/*
2059 		 * Make a copy of the object.
2060 		 */
2061 		if (src_object = src_entry->object.vm_object) {
2062 
2063 			if ((src_object->handle == NULL) &&
2064 				(src_object->type == OBJT_DEFAULT ||
2065 				 src_object->type == OBJT_SWAP)) {
2066 				vm_object_collapse(src_object);
2067 				if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
2068 					vm_map_split(src_entry);
2069 					src_map->timestamp++;
2070 					src_object = src_entry->object.vm_object;
2071 				}
2072 			}
2073 
2074 			vm_object_reference(src_object);
2075 			src_object->flags &= ~OBJ_ONEMAPPING;
2076 			dst_entry->object.vm_object = src_object;
2077 			src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2078 			dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2079 			dst_entry->offset = src_entry->offset;
2080 		} else {
2081 			dst_entry->object.vm_object = NULL;
2082 			dst_entry->offset = 0;
2083 		}
2084 
2085 		pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
2086 		    dst_entry->end - dst_entry->start, src_entry->start);
2087 	} else {
2088 		/*
2089 		 * Of course, wired down pages can't be set copy-on-write.
2090 		 * Cause wired pages to be copied into the new map by
2091 		 * simulating faults (the new pages are pageable)
2092 		 */
2093 		vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry);
2094 	}
2095 }
2096 
2097 /*
2098  * vmspace_fork:
2099  * Create a new process vmspace structure and vm_map
2100  * based on those of an existing process.  The new map
2101  * is based on the old map, according to the inheritance
2102  * values on the regions in that map.
2103  *
2104  * The source map must not be locked.
2105  */
2106 struct vmspace *
2107 vmspace_fork(vm1)
2108 	struct vmspace *vm1;
2109 {
2110 	struct vmspace *vm2;
2111 	vm_map_t old_map = &vm1->vm_map;
2112 	vm_map_t new_map;
2113 	vm_map_entry_t old_entry;
2114 	vm_map_entry_t new_entry;
2115 	pmap_t new_pmap;
2116 	vm_object_t object;
2117 
2118 	vm_map_lock(old_map);
2119 
2120 	vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
2121 	bcopy(&vm1->vm_startcopy, &vm2->vm_startcopy,
2122 	    (caddr_t) (vm1 + 1) - (caddr_t) &vm1->vm_startcopy);
2123 	new_pmap = &vm2->vm_pmap;	/* XXX */
2124 	new_map = &vm2->vm_map;	/* XXX */
2125 	new_map->timestamp = 1;
2126 
2127 	old_entry = old_map->header.next;
2128 
2129 	while (old_entry != &old_map->header) {
2130 		if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2131 			panic("vm_map_fork: encountered a submap");
2132 
2133 		switch (old_entry->inheritance) {
2134 		case VM_INHERIT_NONE:
2135 			break;
2136 
2137 		case VM_INHERIT_SHARE:
2138 			/*
2139 			 * Clone the entry, creating the shared object if necessary.
2140 			 */
2141 			object = old_entry->object.vm_object;
2142 			if (object == NULL) {
2143 				object = vm_object_allocate(OBJT_DEFAULT,
2144 					atop(old_entry->end - old_entry->start));
2145 				old_entry->object.vm_object = object;
2146 				old_entry->offset = (vm_offset_t) 0;
2147 			} else if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
2148 				vm_object_shadow(&old_entry->object.vm_object,
2149 					&old_entry->offset,
2150 					atop(old_entry->end - old_entry->start));
2151 				old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
2152 				object = old_entry->object.vm_object;
2153 			}
2154 			object->flags &= ~OBJ_ONEMAPPING;
2155 
2156 			/*
2157 			 * Clone the entry, referencing the sharing map.
2158 			 */
2159 			new_entry = vm_map_entry_create(new_map);
2160 			*new_entry = *old_entry;
2161 			new_entry->wired_count = 0;
2162 			vm_object_reference(object);
2163 
2164 			/*
2165 			 * Insert the entry into the new map -- we know we're
2166 			 * inserting at the end of the new map.
2167 			 */
2168 
2169 			vm_map_entry_link(new_map, new_map->header.prev,
2170 			    new_entry);
2171 
2172 			/*
2173 			 * Update the physical map
2174 			 */
2175 
2176 			pmap_copy(new_map->pmap, old_map->pmap,
2177 			    new_entry->start,
2178 			    (old_entry->end - old_entry->start),
2179 			    old_entry->start);
2180 			break;
2181 
2182 		case VM_INHERIT_COPY:
2183 			/*
2184 			 * Clone the entry and link into the map.
2185 			 */
2186 			new_entry = vm_map_entry_create(new_map);
2187 			*new_entry = *old_entry;
2188 			new_entry->wired_count = 0;
2189 			new_entry->object.vm_object = NULL;
2190 			new_entry->eflags &= ~MAP_ENTRY_IS_A_MAP;
2191 			vm_map_entry_link(new_map, new_map->header.prev,
2192 			    new_entry);
2193 			vm_map_copy_entry(old_map, new_map, old_entry,
2194 			    new_entry);
2195 			break;
2196 		}
2197 		old_entry = old_entry->next;
2198 	}
2199 
2200 	new_map->size = old_map->size;
2201 	vm_map_unlock(old_map);
2202 	old_map->timestamp++;
2203 
2204 	return (vm2);
2205 }
2206 
2207 /*
2208  * Unshare the specified VM space for exec.  If other processes are
2209  * mapped to it, then create a new one.  The new vmspace is null.
2210  */
2211 
2212 void
2213 vmspace_exec(struct proc *p) {
2214 	struct vmspace *oldvmspace = p->p_vmspace;
2215 	struct vmspace *newvmspace;
2216 	vm_map_t map = &p->p_vmspace->vm_map;
2217 
2218 	newvmspace = vmspace_alloc(map->min_offset, map->max_offset);
2219 	bcopy(&oldvmspace->vm_startcopy, &newvmspace->vm_startcopy,
2220 	    (caddr_t) (newvmspace + 1) - (caddr_t) &newvmspace->vm_startcopy);
2221 	/*
2222 	 * This code is written like this for prototype purposes.  The
2223 	 * goal is to avoid running down the vmspace here, but let the
2224 	 * other process's that are still using the vmspace to finally
2225 	 * run it down.  Even though there is little or no chance of blocking
2226 	 * here, it is a good idea to keep this form for future mods.
2227 	 */
2228 	vmspace_free(oldvmspace);
2229 	p->p_vmspace = newvmspace;
2230 	if (p == curproc)
2231 		pmap_activate(p);
2232 }
2233 
2234 /*
2235  * Unshare the specified VM space for forcing COW.  This
2236  * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
2237  */
2238 
2239 void
2240 vmspace_unshare(struct proc *p) {
2241 	struct vmspace *oldvmspace = p->p_vmspace;
2242 	struct vmspace *newvmspace;
2243 
2244 	if (oldvmspace->vm_refcnt == 1)
2245 		return;
2246 	newvmspace = vmspace_fork(oldvmspace);
2247 	vmspace_free(oldvmspace);
2248 	p->p_vmspace = newvmspace;
2249 	if (p == curproc)
2250 		pmap_activate(p);
2251 }
2252 
2253 
2254 /*
2255  *	vm_map_lookup:
2256  *
2257  *	Finds the VM object, offset, and
2258  *	protection for a given virtual address in the
2259  *	specified map, assuming a page fault of the
2260  *	type specified.
2261  *
2262  *	Leaves the map in question locked for read; return
2263  *	values are guaranteed until a vm_map_lookup_done
2264  *	call is performed.  Note that the map argument
2265  *	is in/out; the returned map must be used in
2266  *	the call to vm_map_lookup_done.
2267  *
2268  *	A handle (out_entry) is returned for use in
2269  *	vm_map_lookup_done, to make that fast.
2270  *
2271  *	If a lookup is requested with "write protection"
2272  *	specified, the map may be changed to perform virtual
2273  *	copying operations, although the data referenced will
2274  *	remain the same.
2275  */
2276 int
2277 vm_map_lookup(vm_map_t *var_map,		/* IN/OUT */
2278 	      vm_offset_t vaddr,
2279 	      vm_prot_t fault_typea,
2280 	      vm_map_entry_t *out_entry,	/* OUT */
2281 	      vm_object_t *object,		/* OUT */
2282 	      vm_pindex_t *pindex,		/* OUT */
2283 	      vm_prot_t *out_prot,		/* OUT */
2284 	      boolean_t *wired)			/* OUT */
2285 {
2286 	vm_map_t share_map;
2287 	vm_offset_t share_offset;
2288 	vm_map_entry_t entry;
2289 	vm_map_t map = *var_map;
2290 	vm_prot_t prot;
2291 	boolean_t su;
2292 	vm_prot_t fault_type = fault_typea;
2293 
2294 RetryLookup:;
2295 
2296 	/*
2297 	 * Lookup the faulting address.
2298 	 */
2299 
2300 	vm_map_lock_read(map);
2301 
2302 #define	RETURN(why) \
2303 		{ \
2304 		vm_map_unlock_read(map); \
2305 		return(why); \
2306 		}
2307 
2308 	/*
2309 	 * If the map has an interesting hint, try it before calling full
2310 	 * blown lookup routine.
2311 	 */
2312 
2313 	entry = map->hint;
2314 
2315 	*out_entry = entry;
2316 
2317 	if ((entry == &map->header) ||
2318 	    (vaddr < entry->start) || (vaddr >= entry->end)) {
2319 		vm_map_entry_t tmp_entry;
2320 
2321 		/*
2322 		 * Entry was either not a valid hint, or the vaddr was not
2323 		 * contained in the entry, so do a full lookup.
2324 		 */
2325 		if (!vm_map_lookup_entry(map, vaddr, &tmp_entry))
2326 			RETURN(KERN_INVALID_ADDRESS);
2327 
2328 		entry = tmp_entry;
2329 		*out_entry = entry;
2330 	}
2331 
2332 	/*
2333 	 * Handle submaps.
2334 	 */
2335 
2336 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
2337 		vm_map_t old_map = map;
2338 
2339 		*var_map = map = entry->object.sub_map;
2340 		vm_map_unlock_read(old_map);
2341 		goto RetryLookup;
2342 	}
2343 
2344 	/*
2345 	 * Check whether this task is allowed to have this page.
2346 	 * Note the special case for MAP_ENTRY_COW
2347 	 * pages with an override.  This is to implement a forced
2348 	 * COW for debuggers.
2349 	 */
2350 
2351 	if (fault_type & VM_PROT_OVERRIDE_WRITE)
2352 		prot = entry->max_protection;
2353 	else
2354 		prot = entry->protection;
2355 
2356 	fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
2357 	if ((fault_type & prot) != fault_type) {
2358 			RETURN(KERN_PROTECTION_FAILURE);
2359 	}
2360 
2361 	if (entry->wired_count && (fault_type & VM_PROT_WRITE) &&
2362 			(entry->eflags & MAP_ENTRY_COW) &&
2363 			(fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
2364 			RETURN(KERN_PROTECTION_FAILURE);
2365 	}
2366 
2367 	/*
2368 	 * If this page is not pageable, we have to get it for all possible
2369 	 * accesses.
2370 	 */
2371 
2372 	*wired = (entry->wired_count != 0);
2373 	if (*wired)
2374 		prot = fault_type = entry->protection;
2375 
2376 	/*
2377 	 * If we don't already have a VM object, track it down.
2378 	 */
2379 
2380 	su = (entry->eflags & MAP_ENTRY_IS_A_MAP) == 0;
2381 	if (su) {
2382 		share_map = map;
2383 		share_offset = vaddr;
2384 	} else {
2385 		vm_map_entry_t share_entry;
2386 
2387 		/*
2388 		 * Compute the sharing map, and offset into it.
2389 		 */
2390 
2391 		share_map = entry->object.share_map;
2392 		share_offset = (vaddr - entry->start) + entry->offset;
2393 
2394 		/*
2395 		 * Look for the backing store object and offset
2396 		 */
2397 
2398 		vm_map_lock_read(share_map);
2399 
2400 		if (!vm_map_lookup_entry(share_map, share_offset,
2401 			&share_entry)) {
2402 			vm_map_unlock_read(share_map);
2403 			RETURN(KERN_INVALID_ADDRESS);
2404 		}
2405 		entry = share_entry;
2406 	}
2407 
2408 	/*
2409 	 * If the entry was copy-on-write, we either ...
2410 	 */
2411 
2412 	if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
2413 		/*
2414 		 * If we want to write the page, we may as well handle that
2415 		 * now since we've got the sharing map locked.
2416 		 *
2417 		 * If we don't need to write the page, we just demote the
2418 		 * permissions allowed.
2419 		 */
2420 
2421 		if (fault_type & VM_PROT_WRITE) {
2422 			/*
2423 			 * Make a new object, and place it in the object
2424 			 * chain.  Note that no new references have appeared
2425 			 * -- one just moved from the share map to the new
2426 			 * object.
2427 			 */
2428 
2429 			if (vm_map_lock_upgrade(share_map)) {
2430 				if (share_map != map)
2431 					vm_map_unlock_read(map);
2432 
2433 				goto RetryLookup;
2434 			}
2435 			vm_object_shadow(
2436 			    &entry->object.vm_object,
2437 			    &entry->offset,
2438 			    atop(entry->end - entry->start));
2439 
2440 			entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
2441 			vm_map_lock_downgrade(share_map);
2442 		} else {
2443 			/*
2444 			 * We're attempting to read a copy-on-write page --
2445 			 * don't allow writes.
2446 			 */
2447 
2448 			prot &= ~VM_PROT_WRITE;
2449 		}
2450 	}
2451 
2452 	/*
2453 	 * Create an object if necessary.
2454 	 */
2455 	if (entry->object.vm_object == NULL) {
2456 
2457 		if (vm_map_lock_upgrade(share_map)) {
2458 			if (share_map != map)
2459 				vm_map_unlock_read(map);
2460 			goto RetryLookup;
2461 		}
2462 		entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
2463 		    atop(entry->end - entry->start));
2464 		entry->offset = 0;
2465 		vm_map_lock_downgrade(share_map);
2466 	}
2467 
2468 	if (entry->object.vm_object->type == OBJT_DEFAULT)
2469 		default_pager_convert_to_swapq(entry->object.vm_object);
2470 	/*
2471 	 * Return the object/offset from this entry.  If the entry was
2472 	 * copy-on-write or empty, it has been fixed up.
2473 	 */
2474 
2475 	*pindex = OFF_TO_IDX((share_offset - entry->start) + entry->offset);
2476 	*object = entry->object.vm_object;
2477 
2478 	/*
2479 	 * Return whether this is the only map sharing this data.
2480 	 */
2481 
2482 	*out_prot = prot;
2483 	return (KERN_SUCCESS);
2484 
2485 #undef	RETURN
2486 }
2487 
2488 /*
2489  *	vm_map_lookup_done:
2490  *
2491  *	Releases locks acquired by a vm_map_lookup
2492  *	(according to the handle returned by that lookup).
2493  */
2494 
2495 void
2496 vm_map_lookup_done(map, entry)
2497 	vm_map_t map;
2498 	vm_map_entry_t entry;
2499 {
2500 	/*
2501 	 * If this entry references a map, unlock it first.
2502 	 */
2503 
2504 	if (entry->eflags & MAP_ENTRY_IS_A_MAP)
2505 		vm_map_unlock_read(entry->object.share_map);
2506 
2507 	/*
2508 	 * Unlock the main-level map
2509 	 */
2510 
2511 	vm_map_unlock_read(map);
2512 }
2513 
2514 /*
2515  * Implement uiomove with VM operations.  This handles (and collateral changes)
2516  * support every combination of source object modification, and COW type
2517  * operations.
2518  */
2519 int
2520 vm_uiomove(mapa, srcobject, cp, cnta, uaddra, npages)
2521 	vm_map_t mapa;
2522 	vm_object_t srcobject;
2523 	off_t cp;
2524 	int cnta;
2525 	vm_offset_t uaddra;
2526 	int *npages;
2527 {
2528 	vm_map_t map;
2529 	vm_object_t first_object, oldobject, object;
2530 	vm_map_entry_t entry;
2531 	vm_prot_t prot;
2532 	boolean_t wired;
2533 	int tcnt, rv;
2534 	vm_offset_t uaddr, start, end, tend;
2535 	vm_pindex_t first_pindex, osize, oindex;
2536 	off_t ooffset;
2537 	int cnt;
2538 
2539 	if (npages)
2540 		*npages = 0;
2541 
2542 	cnt = cnta;
2543 	uaddr = uaddra;
2544 
2545 	while (cnt > 0) {
2546 		map = mapa;
2547 
2548 		if ((vm_map_lookup(&map, uaddr,
2549 			VM_PROT_READ, &entry, &first_object,
2550 			&first_pindex, &prot, &wired)) != KERN_SUCCESS) {
2551 			return EFAULT;
2552 		}
2553 
2554 		vm_map_clip_start(map, entry, uaddr);
2555 
2556 		tcnt = cnt;
2557 		tend = uaddr + tcnt;
2558 		if (tend > entry->end) {
2559 			tcnt = entry->end - uaddr;
2560 			tend = entry->end;
2561 		}
2562 
2563 		vm_map_clip_end(map, entry, tend);
2564 
2565 		start = entry->start;
2566 		end = entry->end;
2567 
2568 		osize = atop(tcnt);
2569 
2570 		oindex = OFF_TO_IDX(cp);
2571 		if (npages) {
2572 			vm_pindex_t idx;
2573 			for (idx = 0; idx < osize; idx++) {
2574 				vm_page_t m;
2575 				if ((m = vm_page_lookup(srcobject, oindex + idx)) == NULL) {
2576 					vm_map_lookup_done(map, entry);
2577 					return 0;
2578 				}
2579 				if ((m->flags & PG_BUSY) ||
2580 					((m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL)) {
2581 					vm_map_lookup_done(map, entry);
2582 					return 0;
2583 				}
2584 			}
2585 		}
2586 
2587 /*
2588  * If we are changing an existing map entry, just redirect
2589  * the object, and change mappings.
2590  */
2591 		if ((first_object->type == OBJT_VNODE) &&
2592 			((oldobject = entry->object.vm_object) == first_object)) {
2593 
2594 			if ((entry->offset != cp) || (oldobject != srcobject)) {
2595 				/*
2596    				* Remove old window into the file
2597    				*/
2598 				pmap_remove (map->pmap, uaddr, tend);
2599 
2600 				/*
2601    				* Force copy on write for mmaped regions
2602    				*/
2603 				vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
2604 
2605 				/*
2606    				* Point the object appropriately
2607    				*/
2608 				if (oldobject != srcobject) {
2609 
2610 				/*
2611    				* Set the object optimization hint flag
2612    				*/
2613 					srcobject->flags |= OBJ_OPT;
2614 					vm_object_reference(srcobject);
2615 					entry->object.vm_object = srcobject;
2616 
2617 					if (oldobject) {
2618 						vm_object_deallocate(oldobject);
2619 					}
2620 				}
2621 
2622 				entry->offset = cp;
2623 				map->timestamp++;
2624 			} else {
2625 				pmap_remove (map->pmap, uaddr, tend);
2626 			}
2627 
2628 		} else if ((first_object->ref_count == 1) &&
2629 			(first_object->size == osize) &&
2630 			((first_object->type == OBJT_DEFAULT) ||
2631 				(first_object->type == OBJT_SWAP)) ) {
2632 
2633 			oldobject = first_object->backing_object;
2634 
2635 			if ((first_object->backing_object_offset != cp) ||
2636 				(oldobject != srcobject)) {
2637 				/*
2638    				* Remove old window into the file
2639    				*/
2640 				pmap_remove (map->pmap, uaddr, tend);
2641 
2642 				/*
2643 				 * Remove unneeded old pages
2644 				 */
2645 				if (first_object->resident_page_count) {
2646 					vm_object_page_remove (first_object, 0, 0, 0);
2647 				}
2648 
2649 				/*
2650 				 * Invalidate swap space
2651 				 */
2652 				if (first_object->type == OBJT_SWAP) {
2653 					swap_pager_freespace(first_object,
2654 						OFF_TO_IDX(first_object->paging_offset),
2655 						first_object->size);
2656 				}
2657 
2658 				/*
2659    				* Force copy on write for mmaped regions
2660    				*/
2661 				vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
2662 
2663 				/*
2664    				* Point the object appropriately
2665    				*/
2666 				if (oldobject != srcobject) {
2667 
2668 				/*
2669    				* Set the object optimization hint flag
2670    				*/
2671 					srcobject->flags |= OBJ_OPT;
2672 					vm_object_reference(srcobject);
2673 
2674 					if (oldobject) {
2675 						TAILQ_REMOVE(&oldobject->shadow_head,
2676 							first_object, shadow_list);
2677 						oldobject->shadow_count--;
2678 						vm_object_deallocate(oldobject);
2679 					}
2680 
2681 					TAILQ_INSERT_TAIL(&srcobject->shadow_head,
2682 						first_object, shadow_list);
2683 					srcobject->shadow_count++;
2684 
2685 					first_object->backing_object = srcobject;
2686 				}
2687 				first_object->backing_object_offset = cp;
2688 				map->timestamp++;
2689 			} else {
2690 				pmap_remove (map->pmap, uaddr, tend);
2691 			}
2692 /*
2693  * Otherwise, we have to do a logical mmap.
2694  */
2695 		} else {
2696 
2697 			srcobject->flags |= OBJ_OPT;
2698 			vm_object_reference(srcobject);
2699 
2700 			pmap_remove (map->pmap, uaddr, tend);
2701 
2702 			vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
2703 			vm_map_lock_upgrade(map);
2704 
2705 			if (entry == &map->header) {
2706 				map->first_free = &map->header;
2707 			} else if (map->first_free->start >= start) {
2708 				map->first_free = entry->prev;
2709 			}
2710 
2711 			SAVE_HINT(map, entry->prev);
2712 			vm_map_entry_delete(map, entry);
2713 
2714 			object = srcobject;
2715 			ooffset = cp;
2716 #if 0
2717 			vm_object_shadow(&object, &ooffset, osize);
2718 #endif
2719 
2720 			rv = vm_map_insert(map, object, ooffset, start, tend,
2721 				VM_PROT_ALL, VM_PROT_ALL, MAP_COPY_ON_WRITE|MAP_COPY_NEEDED);
2722 
2723 			if (rv != KERN_SUCCESS)
2724 				panic("vm_uiomove: could not insert new entry: %d", rv);
2725 		}
2726 
2727 /*
2728  * Map the window directly, if it is already in memory
2729  */
2730 		pmap_object_init_pt(map->pmap, uaddr,
2731 			srcobject, oindex, tcnt, 0);
2732 
2733 		map->timestamp++;
2734 		vm_map_unlock(map);
2735 
2736 		cnt -= tcnt;
2737 		uaddr += tcnt;
2738 		cp += tcnt;
2739 		if (npages)
2740 			*npages += osize;
2741 	}
2742 	return 0;
2743 }
2744 
2745 /*
2746  * Performs the copy_on_write operations necessary to allow the virtual copies
2747  * into user space to work.  This has to be called for write(2) system calls
2748  * from other processes, file unlinking, and file size shrinkage.
2749  */
2750 void
2751 vm_freeze_copyopts(object, froma, toa)
2752 	vm_object_t object;
2753 	vm_pindex_t froma, toa;
2754 {
2755 	int s, rv;
2756 	vm_object_t robject, robjectn;
2757 	vm_pindex_t idx, from, to;
2758 
2759 	if ((object == NULL) ||
2760 		((object->flags & OBJ_OPT) == 0))
2761 		return;
2762 
2763 	if (object->shadow_count > object->ref_count)
2764 		panic("vm_freeze_copyopts: sc > rc");
2765 
2766 	while( robject = TAILQ_FIRST(&object->shadow_head)) {
2767 		vm_pindex_t bo_pindex;
2768 		vm_page_t m_in, m_out;
2769 
2770 		bo_pindex = OFF_TO_IDX(robject->backing_object_offset);
2771 
2772 		vm_object_reference(robject);
2773 
2774 		vm_object_pip_wait(robject, "objfrz");
2775 
2776 		if (robject->ref_count == 1) {
2777 			vm_object_deallocate(robject);
2778 			continue;
2779 		}
2780 
2781 		vm_object_pip_add(robject, 1);
2782 
2783 		for (idx = 0; idx < robject->size; idx++) {
2784 
2785 m_outretry:
2786 			m_out = vm_page_grab(robject, idx,
2787 						VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
2788 
2789 			if (m_out->valid == 0) {
2790 m_inretry:
2791 				m_in = vm_page_grab(object, bo_pindex + idx,
2792 						VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
2793 				if (m_in->valid == 0) {
2794 					rv = vm_pager_get_pages(object, &m_in, 1, 0);
2795 					if (rv != VM_PAGER_OK) {
2796 						printf("vm_freeze_copyopts: cannot read page from file: %x\n", m_in->pindex);
2797 						continue;
2798 					}
2799 					vm_page_deactivate(m_in);
2800 				}
2801 
2802 				vm_page_protect(m_in, VM_PROT_NONE);
2803 				pmap_copy_page(VM_PAGE_TO_PHYS(m_in), VM_PAGE_TO_PHYS(m_out));
2804 				m_out->valid = m_in->valid;
2805 				m_out->dirty = VM_PAGE_BITS_ALL;
2806 
2807 				vm_page_activate(m_out);
2808 
2809 				PAGE_WAKEUP(m_in);
2810 			}
2811 			PAGE_WAKEUP(m_out);
2812 		}
2813 
2814 		object->shadow_count--;
2815 		object->ref_count--;
2816 		TAILQ_REMOVE(&object->shadow_head, robject, shadow_list);
2817 		robject->backing_object = NULL;
2818 		robject->backing_object_offset = 0;
2819 
2820 		vm_object_pip_wakeup(robject);
2821 		vm_object_deallocate(robject);
2822 	}
2823 
2824 	object->flags &= ~OBJ_OPT;
2825 }
2826 
2827 #include "opt_ddb.h"
2828 #ifdef DDB
2829 #include <sys/kernel.h>
2830 
2831 #include <ddb/ddb.h>
2832 
2833 /*
2834  *	vm_map_print:	[ debug ]
2835  */
2836 DB_SHOW_COMMAND(map, vm_map_print)
2837 {
2838 	static int nlines;
2839 	/* XXX convert args. */
2840 	vm_map_t map = (vm_map_t)addr;
2841 	boolean_t full = have_addr;
2842 
2843 	vm_map_entry_t entry;
2844 
2845 	db_iprintf("%s map %p: pmap=%p, nentries=%d, version=%u\n",
2846 	    (map->is_main_map ? "Task" : "Share"), (void *)map,
2847 	    (void *)map->pmap, map->nentries, map->timestamp);
2848 	nlines++;
2849 
2850 	if (!full && db_indent)
2851 		return;
2852 
2853 	db_indent += 2;
2854 	for (entry = map->header.next; entry != &map->header;
2855 	    entry = entry->next) {
2856 #if 0
2857 		if (nlines > 18) {
2858 			db_printf("--More--");
2859 			cngetc();
2860 			db_printf("\r");
2861 			nlines = 0;
2862 		}
2863 #endif
2864 
2865 		db_iprintf("map entry %p: start=%p, end=%p\n",
2866 		    (void *)entry, (void *)entry->start, (void *)entry->end);
2867 		nlines++;
2868 		if (map->is_main_map) {
2869 			static char *inheritance_name[4] =
2870 			{"share", "copy", "none", "donate_copy"};
2871 
2872 			db_iprintf(" prot=%x/%x/%s",
2873 			    entry->protection,
2874 			    entry->max_protection,
2875 			    inheritance_name[entry->inheritance]);
2876 			if (entry->wired_count != 0)
2877 				db_printf(", wired");
2878 		}
2879 		if (entry->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP)) {
2880 			/* XXX no %qd in kernel.  Truncate entry->offset. */
2881 			db_printf(", share=%p, offset=0x%lx\n",
2882 			    (void *)entry->object.share_map,
2883 			    (long)entry->offset);
2884 			nlines++;
2885 			if ((entry->prev == &map->header) ||
2886 			    ((entry->prev->eflags & MAP_ENTRY_IS_A_MAP) == 0) ||
2887 			    (entry->prev->object.share_map !=
2888 				entry->object.share_map)) {
2889 				db_indent += 2;
2890 				vm_map_print((db_expr_t)(intptr_t)
2891 					     entry->object.share_map,
2892 					     full, 0, (char *)0);
2893 				db_indent -= 2;
2894 			}
2895 		} else {
2896 			/* XXX no %qd in kernel.  Truncate entry->offset. */
2897 			db_printf(", object=%p, offset=0x%lx",
2898 			    (void *)entry->object.vm_object,
2899 			    (long)entry->offset);
2900 			if (entry->eflags & MAP_ENTRY_COW)
2901 				db_printf(", copy (%s)",
2902 				    (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
2903 			db_printf("\n");
2904 			nlines++;
2905 
2906 			if ((entry->prev == &map->header) ||
2907 			    (entry->prev->eflags & MAP_ENTRY_IS_A_MAP) ||
2908 			    (entry->prev->object.vm_object !=
2909 				entry->object.vm_object)) {
2910 				db_indent += 2;
2911 				vm_object_print((db_expr_t)(intptr_t)
2912 						entry->object.vm_object,
2913 						full, 0, (char *)0);
2914 				nlines += 4;
2915 				db_indent -= 2;
2916 			}
2917 		}
2918 	}
2919 	db_indent -= 2;
2920 	if (db_indent == 0)
2921 		nlines = 0;
2922 }
2923 
2924 
2925 DB_SHOW_COMMAND(procvm, procvm)
2926 {
2927 	struct proc *p;
2928 
2929 	if (have_addr) {
2930 		p = (struct proc *) addr;
2931 	} else {
2932 		p = curproc;
2933 	}
2934 
2935 	db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
2936 	    (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
2937 	    (void *)&p->p_vmspace->vm_pmap);
2938 
2939 	vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);
2940 }
2941 
2942 #endif /* DDB */
2943