xref: /original-bsd/sys/vm/vm_map.c (revision 03bd62d7)
1 /*
2  * Copyright (c) 1991 Regents of the University of California.
3  * 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  * %sccs.include.redist.c%
9  *
10  *	@(#)vm_map.c	7.7 (Berkeley) 03/02/93
11  *
12  *
13  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
14  * All rights reserved.
15  *
16  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
17  *
18  * Permission to use, copy, modify and distribute this software and
19  * its documentation is hereby granted, provided that both the copyright
20  * notice and this permission notice appear in all copies of the
21  * software, derivative works or modified versions, and any portions
22  * thereof, and that both notices appear in supporting documentation.
23  *
24  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
25  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
26  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
27  *
28  * Carnegie Mellon requests users of this software to return to
29  *
30  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
31  *  School of Computer Science
32  *  Carnegie Mellon University
33  *  Pittsburgh PA 15213-3890
34  *
35  * any improvements or extensions that they make and grant Carnegie the
36  * rights to redistribute these changes.
37  */
38 
39 /*
40  *	Virtual memory mapping module.
41  */
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/malloc.h>
46 
47 #include <vm/vm.h>
48 #include <vm/vm_page.h>
49 #include <vm/vm_object.h>
50 
51 /*
52  *	Virtual memory maps provide for the mapping, protection,
53  *	and sharing of virtual memory objects.  In addition,
54  *	this module provides for an efficient virtual copy of
55  *	memory from one map to another.
56  *
57  *	Synchronization is required prior to most operations.
58  *
59  *	Maps consist of an ordered doubly-linked list of simple
60  *	entries; a single hint is used to speed up lookups.
61  *
62  *	In order to properly represent the sharing of virtual
63  *	memory regions among maps, the map structure is bi-level.
64  *	Top-level ("address") maps refer to regions of sharable
65  *	virtual memory.  These regions are implemented as
66  *	("sharing") maps, which then refer to the actual virtual
67  *	memory objects.  When two address maps "share" memory,
68  *	their top-level maps both have references to the same
69  *	sharing map.  When memory is virtual-copied from one
70  *	address map to another, the references in the sharing
71  *	maps are actually copied -- no copying occurs at the
72  *	virtual memory object level.
73  *
74  *	Since portions of maps are specified by start/end addreses,
75  *	which may not align with existing map entries, all
76  *	routines merely "clip" entries to these start/end values.
77  *	[That is, an entry is split into two, bordering at a
78  *	start or end value.]  Note that these clippings may not
79  *	always be necessary (as the two resulting entries are then
80  *	not changed); however, the clipping is done for convenience.
81  *	No attempt is currently made to "glue back together" two
82  *	abutting entries.
83  *
84  *	As mentioned above, virtual copy operations are performed
85  *	by copying VM object references from one sharing map to
86  *	another, and then marking both regions as copy-on-write.
87  *	It is important to note that only one writeable reference
88  *	to a VM object region exists in any map -- this means that
89  *	shadow object creation can be delayed until a write operation
90  *	occurs.
91  */
92 
93 /*
94  *	vm_map_startup:
95  *
96  *	Initialize the vm_map module.  Must be called before
97  *	any other vm_map routines.
98  *
99  *	Map and entry structures are allocated from the general
100  *	purpose memory pool with some exceptions:
101  *
102  *	- The kernel map and kmem submap are allocated statically.
103  *	- Kernel map entries are allocated out of a static pool.
104  *
105  *	These restrictions are necessary since malloc() uses the
106  *	maps and requires map entries.
107  */
108 
109 vm_offset_t	kentry_data;
110 vm_size_t	kentry_data_size;
111 vm_map_entry_t	kentry_free;
112 vm_map_t	kmap_free;
113 
114 static void	_vm_map_clip_end __P((vm_map_t, vm_map_entry_t, vm_offset_t));
115 static void	_vm_map_clip_start __P((vm_map_t, vm_map_entry_t, vm_offset_t));
116 
117 void vm_map_startup()
118 {
119 	register int i;
120 	register vm_map_entry_t mep;
121 	vm_map_t mp;
122 
123 	/*
124 	 * Static map structures for allocation before initialization of
125 	 * kernel map or kmem map.  vm_map_create knows how to deal with them.
126 	 */
127 	kmap_free = mp = (vm_map_t) kentry_data;
128 	i = MAX_KMAP;
129 	while (--i > 0) {
130 		mp->header.next = (vm_map_entry_t) (mp + 1);
131 		mp++;
132 	}
133 	mp++->header.next = NULL;
134 
135 	/*
136 	 * Form a free list of statically allocated kernel map entries
137 	 * with the rest.
138 	 */
139 	kentry_free = mep = (vm_map_entry_t) mp;
140 	i = (kentry_data_size - MAX_KMAP * sizeof *mp) / sizeof *mep;
141 	while (--i > 0) {
142 		mep->next = mep + 1;
143 		mep++;
144 	}
145 	mep->next = NULL;
146 }
147 
148 /*
149  * Allocate a vmspace structure, including a vm_map and pmap,
150  * and initialize those structures.  The refcnt is set to 1.
151  * The remaining fields must be initialized by the caller.
152  */
153 struct vmspace *
154 vmspace_alloc(min, max, pageable)
155 	vm_offset_t min, max;
156 	int pageable;
157 {
158 	register struct vmspace *vm;
159 
160 	MALLOC(vm, struct vmspace *, sizeof(struct vmspace), M_VMMAP, M_WAITOK);
161 	bzero(vm, (caddr_t) &vm->vm_startcopy - (caddr_t) vm);
162 	vm_map_init(&vm->vm_map, min, max, pageable);
163 	pmap_pinit(&vm->vm_pmap);
164 	vm->vm_map.pmap = &vm->vm_pmap;		/* XXX */
165 	vm->vm_refcnt = 1;
166 	return (vm);
167 }
168 
169 void
170 vmspace_free(vm)
171 	register struct vmspace *vm;
172 {
173 
174 	if (--vm->vm_refcnt == 0) {
175 		/*
176 		 * Lock the map, to wait out all other references to it.
177 		 * Delete all of the mappings and pages they hold,
178 		 * then call the pmap module to reclaim anything left.
179 		 */
180 		vm_map_lock(&vm->vm_map);
181 		(void) vm_map_delete(&vm->vm_map, vm->vm_map.min_offset,
182 		    vm->vm_map.max_offset);
183 		pmap_release(&vm->vm_pmap);
184 		FREE(vm, M_VMMAP);
185 	}
186 }
187 
188 /*
189  *	vm_map_create:
190  *
191  *	Creates and returns a new empty VM map with
192  *	the given physical map structure, and having
193  *	the given lower and upper address bounds.
194  */
195 vm_map_t vm_map_create(pmap, min, max, pageable)
196 	pmap_t		pmap;
197 	vm_offset_t	min, max;
198 	boolean_t	pageable;
199 {
200 	register vm_map_t	result;
201 	extern vm_map_t		kernel_map, kmem_map;
202 
203 	if (kmem_map == NULL) {
204 		result = kmap_free;
205 		kmap_free = (vm_map_t) result->header.next;
206 		if (result == NULL)
207 			panic("vm_map_create: out of maps");
208 	} else
209 		MALLOC(result, vm_map_t, sizeof(struct vm_map),
210 		       M_VMMAP, M_WAITOK);
211 
212 	vm_map_init(result, min, max, pageable);
213 	result->pmap = pmap;
214 	return(result);
215 }
216 
217 /*
218  * Initialize an existing vm_map structure
219  * such as that in the vmspace structure.
220  * The pmap is set elsewhere.
221  */
222 void
223 vm_map_init(map, min, max, pageable)
224 	register struct vm_map *map;
225 	vm_offset_t	min, max;
226 	boolean_t	pageable;
227 {
228 	map->header.next = map->header.prev = &map->header;
229 	map->nentries = 0;
230 	map->size = 0;
231 	map->ref_count = 1;
232 	map->is_main_map = TRUE;
233 	map->min_offset = min;
234 	map->max_offset = max;
235 	map->entries_pageable = pageable;
236 	map->first_free = &map->header;
237 	map->hint = &map->header;
238 	map->timestamp = 0;
239 	lock_init(&map->lock, TRUE);
240 	simple_lock_init(&map->ref_lock);
241 	simple_lock_init(&map->hint_lock);
242 }
243 
244 /*
245  *	vm_map_entry_create:	[ internal use only ]
246  *
247  *	Allocates a VM map entry for insertion.
248  *	No entry fields are filled in.  This routine is
249  */
250 vm_map_entry_t vm_map_entry_create(map)
251 	vm_map_t	map;
252 {
253 	vm_map_entry_t	entry;
254 	extern vm_map_t		kernel_map, kmem_map, mb_map, pager_map;
255 
256 	if (map == kernel_map || map == kmem_map || map == mb_map ||
257 	    map == pager_map) {
258 		if (entry = kentry_free)
259 			kentry_free = kentry_free->next;
260 	} else
261 		MALLOC(entry, vm_map_entry_t, sizeof(struct vm_map_entry),
262 		       M_VMMAPENT, M_WAITOK);
263 	if (entry == NULL)
264 		panic("vm_map_entry_create: out of map entries");
265 
266 	return(entry);
267 }
268 
269 /*
270  *	vm_map_entry_dispose:	[ internal use only ]
271  *
272  *	Inverse of vm_map_entry_create.
273  */
274 void vm_map_entry_dispose(map, entry)
275 	vm_map_t	map;
276 	vm_map_entry_t	entry;
277 {
278 	extern vm_map_t		kernel_map, kmem_map, mb_map, pager_map;
279 
280 	if (map == kernel_map || map == kmem_map || map == mb_map ||
281 	    map == pager_map) {
282 		entry->next = kentry_free;
283 		kentry_free = entry;
284 	} else
285 		FREE(entry, M_VMMAPENT);
286 }
287 
288 /*
289  *	vm_map_entry_{un,}link:
290  *
291  *	Insert/remove entries from maps.
292  */
293 #define	vm_map_entry_link(map, after_where, entry) \
294 		{ \
295 		(map)->nentries++; \
296 		(entry)->prev = (after_where); \
297 		(entry)->next = (after_where)->next; \
298 		(entry)->prev->next = (entry); \
299 		(entry)->next->prev = (entry); \
300 		}
301 #define	vm_map_entry_unlink(map, entry) \
302 		{ \
303 		(map)->nentries--; \
304 		(entry)->next->prev = (entry)->prev; \
305 		(entry)->prev->next = (entry)->next; \
306 		}
307 
308 /*
309  *	vm_map_reference:
310  *
311  *	Creates another valid reference to the given map.
312  *
313  */
314 void vm_map_reference(map)
315 	register vm_map_t	map;
316 {
317 	if (map == NULL)
318 		return;
319 
320 	simple_lock(&map->ref_lock);
321 	map->ref_count++;
322 	simple_unlock(&map->ref_lock);
323 }
324 
325 /*
326  *	vm_map_deallocate:
327  *
328  *	Removes a reference from the specified map,
329  *	destroying it if no references remain.
330  *	The map should not be locked.
331  */
332 void vm_map_deallocate(map)
333 	register vm_map_t	map;
334 {
335 	register int		c;
336 
337 	if (map == NULL)
338 		return;
339 
340 	simple_lock(&map->ref_lock);
341 	c = --map->ref_count;
342 	simple_unlock(&map->ref_lock);
343 
344 	if (c > 0) {
345 		return;
346 	}
347 
348 	/*
349 	 *	Lock the map, to wait out all other references
350 	 *	to it.
351 	 */
352 
353 	vm_map_lock(map);
354 
355 	(void) vm_map_delete(map, map->min_offset, map->max_offset);
356 
357 	pmap_destroy(map->pmap);
358 
359 	FREE(map, M_VMMAP);
360 }
361 
362 /*
363  *	vm_map_insert:	[ internal use only ]
364  *
365  *	Inserts the given whole VM object into the target
366  *	map at the specified address range.  The object's
367  *	size should match that of the address range.
368  *
369  *	Requires that the map be locked, and leaves it so.
370  */
371 int
372 vm_map_insert(map, object, offset, start, end)
373 	vm_map_t	map;
374 	vm_object_t	object;
375 	vm_offset_t	offset;
376 	vm_offset_t	start;
377 	vm_offset_t	end;
378 {
379 	register vm_map_entry_t		new_entry;
380 	register vm_map_entry_t		prev_entry;
381 	vm_map_entry_t			temp_entry;
382 
383 	/*
384 	 *	Check that the start and end points are not bogus.
385 	 */
386 
387 	if ((start < map->min_offset) || (end > map->max_offset) ||
388 			(start >= end))
389 		return(KERN_INVALID_ADDRESS);
390 
391 	/*
392 	 *	Find the entry prior to the proposed
393 	 *	starting address; if it's part of an
394 	 *	existing entry, this range is bogus.
395 	 */
396 
397 	if (vm_map_lookup_entry(map, start, &temp_entry))
398 		return(KERN_NO_SPACE);
399 
400 	prev_entry = temp_entry;
401 
402 	/*
403 	 *	Assert that the next entry doesn't overlap the
404 	 *	end point.
405 	 */
406 
407 	if ((prev_entry->next != &map->header) &&
408 			(prev_entry->next->start < end))
409 		return(KERN_NO_SPACE);
410 
411 	/*
412 	 *	See if we can avoid creating a new entry by
413 	 *	extending one of our neighbors.
414 	 */
415 
416 	if (object == NULL) {
417 		if ((prev_entry != &map->header) &&
418 		    (prev_entry->end == start) &&
419 		    (map->is_main_map) &&
420 		    (prev_entry->is_a_map == FALSE) &&
421 		    (prev_entry->is_sub_map == FALSE) &&
422 		    (prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
423 		    (prev_entry->protection == VM_PROT_DEFAULT) &&
424 		    (prev_entry->max_protection == VM_PROT_DEFAULT) &&
425 		    (prev_entry->wired_count == 0)) {
426 
427 			if (vm_object_coalesce(prev_entry->object.vm_object,
428 					NULL,
429 					prev_entry->offset,
430 					(vm_offset_t) 0,
431 					(vm_size_t)(prev_entry->end
432 						     - prev_entry->start),
433 					(vm_size_t)(end - prev_entry->end))) {
434 				/*
435 				 *	Coalesced the two objects - can extend
436 				 *	the previous map entry to include the
437 				 *	new range.
438 				 */
439 				map->size += (end - prev_entry->end);
440 				prev_entry->end = end;
441 				return(KERN_SUCCESS);
442 			}
443 		}
444 	}
445 
446 	/*
447 	 *	Create a new entry
448 	 */
449 
450 	new_entry = vm_map_entry_create(map);
451 	new_entry->start = start;
452 	new_entry->end = end;
453 
454 	new_entry->is_a_map = FALSE;
455 	new_entry->is_sub_map = FALSE;
456 	new_entry->object.vm_object = object;
457 	new_entry->offset = offset;
458 
459 	new_entry->copy_on_write = FALSE;
460 	new_entry->needs_copy = FALSE;
461 
462 	if (map->is_main_map) {
463 		new_entry->inheritance = VM_INHERIT_DEFAULT;
464 		new_entry->protection = VM_PROT_DEFAULT;
465 		new_entry->max_protection = VM_PROT_DEFAULT;
466 		new_entry->wired_count = 0;
467 	}
468 
469 	/*
470 	 *	Insert the new entry into the list
471 	 */
472 
473 	vm_map_entry_link(map, prev_entry, new_entry);
474 	map->size += new_entry->end - new_entry->start;
475 
476 	/*
477 	 *	Update the free space hint
478 	 */
479 
480 	if ((map->first_free == prev_entry) && (prev_entry->end >= new_entry->start))
481 		map->first_free = new_entry;
482 
483 	return(KERN_SUCCESS);
484 }
485 
486 /*
487  *	SAVE_HINT:
488  *
489  *	Saves the specified entry as the hint for
490  *	future lookups.  Performs necessary interlocks.
491  */
492 #define	SAVE_HINT(map,value) \
493 		simple_lock(&(map)->hint_lock); \
494 		(map)->hint = (value); \
495 		simple_unlock(&(map)->hint_lock);
496 
497 /*
498  *	vm_map_lookup_entry:	[ internal use only ]
499  *
500  *	Finds the map entry containing (or
501  *	immediately preceding) the specified address
502  *	in the given map; the entry is returned
503  *	in the "entry" parameter.  The boolean
504  *	result indicates whether the address is
505  *	actually contained in the map.
506  */
507 boolean_t vm_map_lookup_entry(map, address, entry)
508 	register vm_map_t	map;
509 	register vm_offset_t	address;
510 	vm_map_entry_t		*entry;		/* OUT */
511 {
512 	register vm_map_entry_t		cur;
513 	register vm_map_entry_t		last;
514 
515 	/*
516 	 *	Start looking either from the head of the
517 	 *	list, or from the hint.
518 	 */
519 
520 	simple_lock(&map->hint_lock);
521 	cur = map->hint;
522 	simple_unlock(&map->hint_lock);
523 
524 	if (cur == &map->header)
525 		cur = cur->next;
526 
527 	if (address >= cur->start) {
528 	    	/*
529 		 *	Go from hint to end of list.
530 		 *
531 		 *	But first, make a quick check to see if
532 		 *	we are already looking at the entry we
533 		 *	want (which is usually the case).
534 		 *	Note also that we don't need to save the hint
535 		 *	here... it is the same hint (unless we are
536 		 *	at the header, in which case the hint didn't
537 		 *	buy us anything anyway).
538 		 */
539 		last = &map->header;
540 		if ((cur != last) && (cur->end > address)) {
541 			*entry = cur;
542 			return(TRUE);
543 		}
544 	}
545 	else {
546 	    	/*
547 		 *	Go from start to hint, *inclusively*
548 		 */
549 		last = cur->next;
550 		cur = map->header.next;
551 	}
552 
553 	/*
554 	 *	Search linearly
555 	 */
556 
557 	while (cur != last) {
558 		if (cur->end > address) {
559 			if (address >= cur->start) {
560 			    	/*
561 				 *	Save this lookup for future
562 				 *	hints, and return
563 				 */
564 
565 				*entry = cur;
566 				SAVE_HINT(map, cur);
567 				return(TRUE);
568 			}
569 			break;
570 		}
571 		cur = cur->next;
572 	}
573 	*entry = cur->prev;
574 	SAVE_HINT(map, *entry);
575 	return(FALSE);
576 }
577 
578 /*
579  * Find sufficient space for `length' bytes in the given map, starting at
580  * `start'.  The map must be locked.  Returns 0 on success, 1 on no space.
581  */
582 int
583 vm_map_findspace(map, start, length, addr)
584 	register vm_map_t map;
585 	register vm_offset_t start;
586 	vm_size_t length;
587 	vm_offset_t *addr;
588 {
589 	register vm_map_entry_t entry, next;
590 	register vm_offset_t end;
591 
592 	if (start < map->min_offset)
593 		start = map->min_offset;
594 	if (start > map->max_offset)
595 		return (1);
596 
597 	/*
598 	 * Look for the first possible address; if there's already
599 	 * something at this address, we have to start after it.
600 	 */
601 	if (start == map->min_offset) {
602 		if ((entry = map->first_free) != &map->header)
603 			start = entry->end;
604 	} else {
605 		vm_map_entry_t tmp;
606 		if (vm_map_lookup_entry(map, start, &tmp))
607 			start = tmp->end;
608 		entry = tmp;
609 	}
610 
611 	/*
612 	 * Look through the rest of the map, trying to fit a new region in
613 	 * the gap between existing regions, or after the very last region.
614 	 */
615 	for (;; start = (entry = next)->end) {
616 		/*
617 		 * Find the end of the proposed new region.  Be sure we didn't
618 		 * go beyond the end of the map, or wrap around the address;
619 		 * if so, we lose.  Otherwise, if this is the last entry, or
620 		 * if the proposed new region fits before the next entry, we
621 		 * win.
622 		 */
623 		end = start + length;
624 		if (end > map->max_offset || end < start)
625 			return (1);
626 		next = entry->next;
627 		if (next == &map->header || next->start >= end)
628 			break;
629 	}
630 	SAVE_HINT(map, entry);
631 	*addr = start;
632 	return (0);
633 }
634 
635 /*
636  *	vm_map_find finds an unallocated region in the target address
637  *	map with the given length.  The search is defined to be
638  *	first-fit from the specified address; the region found is
639  *	returned in the same parameter.
640  *
641  */
642 int
643 vm_map_find(map, object, offset, addr, length, find_space)
644 	vm_map_t	map;
645 	vm_object_t	object;
646 	vm_offset_t	offset;
647 	vm_offset_t	*addr;		/* IN/OUT */
648 	vm_size_t	length;
649 	boolean_t	find_space;
650 {
651 	register vm_offset_t	start;
652 	int			result;
653 
654 	start = *addr;
655 	vm_map_lock(map);
656 	if (find_space) {
657 		if (vm_map_findspace(map, start, length, addr)) {
658 			vm_map_unlock(map);
659 			return (KERN_NO_SPACE);
660 		}
661 		start = *addr;
662 	}
663 	result = vm_map_insert(map, object, offset, start, start + length);
664 	vm_map_unlock(map);
665 	return (result);
666 }
667 
668 /*
669  *	vm_map_simplify_entry:	[ internal use only ]
670  *
671  *	Simplify the given map entry by:
672  *		removing extra sharing maps
673  *		[XXX maybe later] merging with a neighbor
674  */
675 void vm_map_simplify_entry(map, entry)
676 	vm_map_t	map;
677 	vm_map_entry_t	entry;
678 {
679 #ifdef	lint
680 	map++;
681 #endif	lint
682 
683 	/*
684 	 *	If this entry corresponds to a sharing map, then
685 	 *	see if we can remove the level of indirection.
686 	 *	If it's not a sharing map, then it points to
687 	 *	a VM object, so see if we can merge with either
688 	 *	of our neighbors.
689 	 */
690 
691 	if (entry->is_sub_map)
692 		return;
693 	if (entry->is_a_map) {
694 #if	0
695 		vm_map_t	my_share_map;
696 		int		count;
697 
698 		my_share_map = entry->object.share_map;
699 		simple_lock(&my_share_map->ref_lock);
700 		count = my_share_map->ref_count;
701 		simple_unlock(&my_share_map->ref_lock);
702 
703 		if (count == 1) {
704 			/* Can move the region from
705 			 * entry->start to entry->end (+ entry->offset)
706 			 * in my_share_map into place of entry.
707 			 * Later.
708 			 */
709 		}
710 #endif	0
711 	}
712 	else {
713 		/*
714 		 *	Try to merge with our neighbors.
715 		 *
716 		 *	Conditions for merge are:
717 		 *
718 		 *	1.  entries are adjacent.
719 		 *	2.  both entries point to objects
720 		 *	    with null pagers.
721 		 *
722 		 * 	If a merge is possible, we replace the two
723 		 *	entries with a single entry, then merge
724 		 *	the two objects into a single object.
725 		 *
726 		 *	Now, all that is left to do is write the
727 		 *	code!
728 		 */
729 	}
730 }
731 
732 /*
733  *	vm_map_clip_start:	[ internal use only ]
734  *
735  *	Asserts that the given entry begins at or after
736  *	the specified address; if necessary,
737  *	it splits the entry into two.
738  */
739 #define vm_map_clip_start(map, entry, startaddr) \
740 { \
741 	if (startaddr > entry->start) \
742 		_vm_map_clip_start(map, entry, startaddr); \
743 }
744 
745 /*
746  *	This routine is called only when it is known that
747  *	the entry must be split.
748  */
749 static void _vm_map_clip_start(map, entry, start)
750 	register vm_map_t	map;
751 	register vm_map_entry_t	entry;
752 	register vm_offset_t	start;
753 {
754 	register vm_map_entry_t	new_entry;
755 
756 	/*
757 	 *	See if we can simplify this entry first
758 	 */
759 
760 	vm_map_simplify_entry(map, entry);
761 
762 	/*
763 	 *	Split off the front portion --
764 	 *	note that we must insert the new
765 	 *	entry BEFORE this one, so that
766 	 *	this entry has the specified starting
767 	 *	address.
768 	 */
769 
770 	new_entry = vm_map_entry_create(map);
771 	*new_entry = *entry;
772 
773 	new_entry->end = start;
774 	entry->offset += (start - entry->start);
775 	entry->start = start;
776 
777 	vm_map_entry_link(map, entry->prev, new_entry);
778 
779 	if (entry->is_a_map || entry->is_sub_map)
780 	 	vm_map_reference(new_entry->object.share_map);
781 	else
782 		vm_object_reference(new_entry->object.vm_object);
783 }
784 
785 /*
786  *	vm_map_clip_end:	[ internal use only ]
787  *
788  *	Asserts that the given entry ends at or before
789  *	the specified address; if necessary,
790  *	it splits the entry into two.
791  */
792 
793 #define vm_map_clip_end(map, entry, endaddr) \
794 { \
795 	if (endaddr < entry->end) \
796 		_vm_map_clip_end(map, entry, endaddr); \
797 }
798 
799 /*
800  *	This routine is called only when it is known that
801  *	the entry must be split.
802  */
803 static void _vm_map_clip_end(map, entry, end)
804 	register vm_map_t	map;
805 	register vm_map_entry_t	entry;
806 	register vm_offset_t	end;
807 {
808 	register vm_map_entry_t	new_entry;
809 
810 	/*
811 	 *	Create a new entry and insert it
812 	 *	AFTER the specified entry
813 	 */
814 
815 	new_entry = vm_map_entry_create(map);
816 	*new_entry = *entry;
817 
818 	new_entry->start = entry->end = end;
819 	new_entry->offset += (end - entry->start);
820 
821 	vm_map_entry_link(map, entry, new_entry);
822 
823 	if (entry->is_a_map || entry->is_sub_map)
824 	 	vm_map_reference(new_entry->object.share_map);
825 	else
826 		vm_object_reference(new_entry->object.vm_object);
827 }
828 
829 /*
830  *	VM_MAP_RANGE_CHECK:	[ internal use only ]
831  *
832  *	Asserts that the starting and ending region
833  *	addresses fall within the valid range of the map.
834  */
835 #define	VM_MAP_RANGE_CHECK(map, start, end)		\
836 		{					\
837 		if (start < vm_map_min(map))		\
838 			start = vm_map_min(map);	\
839 		if (end > vm_map_max(map))		\
840 			end = vm_map_max(map);		\
841 		if (start > end)			\
842 			start = end;			\
843 		}
844 
845 /*
846  *	vm_map_submap:		[ kernel use only ]
847  *
848  *	Mark the given range as handled by a subordinate map.
849  *
850  *	This range must have been created with vm_map_find,
851  *	and no other operations may have been performed on this
852  *	range prior to calling vm_map_submap.
853  *
854  *	Only a limited number of operations can be performed
855  *	within this rage after calling vm_map_submap:
856  *		vm_fault
857  *	[Don't try vm_map_copy!]
858  *
859  *	To remove a submapping, one must first remove the
860  *	range from the superior map, and then destroy the
861  *	submap (if desired).  [Better yet, don't try it.]
862  */
863 int
864 vm_map_submap(map, start, end, submap)
865 	register vm_map_t	map;
866 	register vm_offset_t	start;
867 	register vm_offset_t	end;
868 	vm_map_t		submap;
869 {
870 	vm_map_entry_t		entry;
871 	register int		result = KERN_INVALID_ARGUMENT;
872 
873 	vm_map_lock(map);
874 
875 	VM_MAP_RANGE_CHECK(map, start, end);
876 
877 	if (vm_map_lookup_entry(map, start, &entry)) {
878 		vm_map_clip_start(map, entry, start);
879 	}
880 	 else
881 		entry = entry->next;
882 
883 	vm_map_clip_end(map, entry, end);
884 
885 	if ((entry->start == start) && (entry->end == end) &&
886 	    (!entry->is_a_map) &&
887 	    (entry->object.vm_object == NULL) &&
888 	    (!entry->copy_on_write)) {
889 		entry->is_a_map = FALSE;
890 		entry->is_sub_map = TRUE;
891 		vm_map_reference(entry->object.sub_map = submap);
892 		result = KERN_SUCCESS;
893 	}
894 	vm_map_unlock(map);
895 
896 	return(result);
897 }
898 
899 /*
900  *	vm_map_protect:
901  *
902  *	Sets the protection of the specified address
903  *	region in the target map.  If "set_max" is
904  *	specified, the maximum protection is to be set;
905  *	otherwise, only the current protection is affected.
906  */
907 int
908 vm_map_protect(map, start, end, new_prot, set_max)
909 	register vm_map_t	map;
910 	register vm_offset_t	start;
911 	register vm_offset_t	end;
912 	register vm_prot_t	new_prot;
913 	register boolean_t	set_max;
914 {
915 	register vm_map_entry_t		current;
916 	vm_map_entry_t			entry;
917 
918 	vm_map_lock(map);
919 
920 	VM_MAP_RANGE_CHECK(map, start, end);
921 
922 	if (vm_map_lookup_entry(map, start, &entry)) {
923 		vm_map_clip_start(map, entry, start);
924 	}
925 	 else
926 		entry = entry->next;
927 
928 	/*
929 	 *	Make a first pass to check for protection
930 	 *	violations.
931 	 */
932 
933 	current = entry;
934 	while ((current != &map->header) && (current->start < end)) {
935 		if (current->is_sub_map)
936 			return(KERN_INVALID_ARGUMENT);
937 		if ((new_prot & current->max_protection) != new_prot) {
938 			vm_map_unlock(map);
939 			return(KERN_PROTECTION_FAILURE);
940 		}
941 
942 		current = current->next;
943 	}
944 
945 	/*
946 	 *	Go back and fix up protections.
947 	 *	[Note that clipping is not necessary the second time.]
948 	 */
949 
950 	current = entry;
951 
952 	while ((current != &map->header) && (current->start < end)) {
953 		vm_prot_t	old_prot;
954 
955 		vm_map_clip_end(map, current, end);
956 
957 		old_prot = current->protection;
958 		if (set_max)
959 			current->protection =
960 				(current->max_protection = new_prot) &
961 					old_prot;
962 		else
963 			current->protection = new_prot;
964 
965 		/*
966 		 *	Update physical map if necessary.
967 		 *	Worry about copy-on-write here -- CHECK THIS XXX
968 		 */
969 
970 		if (current->protection != old_prot) {
971 
972 #define MASK(entry)	((entry)->copy_on_write ? ~VM_PROT_WRITE : \
973 							VM_PROT_ALL)
974 #define	max(a,b)	((a) > (b) ? (a) : (b))
975 
976 			if (current->is_a_map) {
977 				vm_map_entry_t	share_entry;
978 				vm_offset_t	share_end;
979 
980 				vm_map_lock(current->object.share_map);
981 				(void) vm_map_lookup_entry(
982 						current->object.share_map,
983 						current->offset,
984 						&share_entry);
985 				share_end = current->offset +
986 					(current->end - current->start);
987 				while ((share_entry !=
988 					&current->object.share_map->header) &&
989 					(share_entry->start < share_end)) {
990 
991 					pmap_protect(map->pmap,
992 						(max(share_entry->start,
993 							current->offset) -
994 							current->offset +
995 							current->start),
996 						min(share_entry->end,
997 							share_end) -
998 						current->offset +
999 						current->start,
1000 						current->protection &
1001 							MASK(share_entry));
1002 
1003 					share_entry = share_entry->next;
1004 				}
1005 				vm_map_unlock(current->object.share_map);
1006 			}
1007 			else
1008 			 	pmap_protect(map->pmap, current->start,
1009 					current->end,
1010 					current->protection & MASK(entry));
1011 #undef	max
1012 #undef	MASK
1013 		}
1014 		current = current->next;
1015 	}
1016 
1017 	vm_map_unlock(map);
1018 	return(KERN_SUCCESS);
1019 }
1020 
1021 /*
1022  *	vm_map_inherit:
1023  *
1024  *	Sets the inheritance of the specified address
1025  *	range in the target map.  Inheritance
1026  *	affects how the map will be shared with
1027  *	child maps at the time of vm_map_fork.
1028  */
1029 int
1030 vm_map_inherit(map, start, end, new_inheritance)
1031 	register vm_map_t	map;
1032 	register vm_offset_t	start;
1033 	register vm_offset_t	end;
1034 	register vm_inherit_t	new_inheritance;
1035 {
1036 	register vm_map_entry_t	entry;
1037 	vm_map_entry_t	temp_entry;
1038 
1039 	switch (new_inheritance) {
1040 	case VM_INHERIT_NONE:
1041 	case VM_INHERIT_COPY:
1042 	case VM_INHERIT_SHARE:
1043 		break;
1044 	default:
1045 		return(KERN_INVALID_ARGUMENT);
1046 	}
1047 
1048 	vm_map_lock(map);
1049 
1050 	VM_MAP_RANGE_CHECK(map, start, end);
1051 
1052 	if (vm_map_lookup_entry(map, start, &temp_entry)) {
1053 		entry = temp_entry;
1054 		vm_map_clip_start(map, entry, start);
1055 	}
1056 	else
1057 		entry = temp_entry->next;
1058 
1059 	while ((entry != &map->header) && (entry->start < end)) {
1060 		vm_map_clip_end(map, entry, end);
1061 
1062 		entry->inheritance = new_inheritance;
1063 
1064 		entry = entry->next;
1065 	}
1066 
1067 	vm_map_unlock(map);
1068 	return(KERN_SUCCESS);
1069 }
1070 
1071 /*
1072  *	vm_map_pageable:
1073  *
1074  *	Sets the pageability of the specified address
1075  *	range in the target map.  Regions specified
1076  *	as not pageable require locked-down physical
1077  *	memory and physical page maps.
1078  *
1079  *	The map must not be locked, but a reference
1080  *	must remain to the map throughout the call.
1081  */
1082 int
1083 vm_map_pageable(map, start, end, new_pageable)
1084 	register vm_map_t	map;
1085 	register vm_offset_t	start;
1086 	register vm_offset_t	end;
1087 	register boolean_t	new_pageable;
1088 {
1089 	register vm_map_entry_t	entry;
1090 	vm_map_entry_t		temp_entry;
1091 
1092 	vm_map_lock(map);
1093 
1094 	VM_MAP_RANGE_CHECK(map, start, end);
1095 
1096 	/*
1097 	 *	Only one pageability change may take place at one
1098 	 *	time, since vm_fault assumes it will be called
1099 	 *	only once for each wiring/unwiring.  Therefore, we
1100 	 *	have to make sure we're actually changing the pageability
1101 	 *	for the entire region.  We do so before making any changes.
1102 	 */
1103 
1104 	if (vm_map_lookup_entry(map, start, &temp_entry)) {
1105 		entry = temp_entry;
1106 		vm_map_clip_start(map, entry, start);
1107 	}
1108 	else
1109 		entry = temp_entry->next;
1110 	temp_entry = entry;
1111 
1112 	/*
1113 	 *	Actions are rather different for wiring and unwiring,
1114 	 *	so we have two separate cases.
1115 	 */
1116 
1117 	if (new_pageable) {
1118 
1119 		/*
1120 		 *	Unwiring.  First ensure that the range to be
1121 		 *	unwired is really wired down.
1122 		 */
1123 		while ((entry != &map->header) && (entry->start < end)) {
1124 
1125 		    if (entry->wired_count == 0) {
1126 			vm_map_unlock(map);
1127 			return(KERN_INVALID_ARGUMENT);
1128 		    }
1129 		    entry = entry->next;
1130 		}
1131 
1132 		/*
1133 		 *	Now decrement the wiring count for each region.
1134 		 *	If a region becomes completely unwired,
1135 		 *	unwire its physical pages and mappings.
1136 		 */
1137 		lock_set_recursive(&map->lock);
1138 
1139 		entry = temp_entry;
1140 		while ((entry != &map->header) && (entry->start < end)) {
1141 		    vm_map_clip_end(map, entry, end);
1142 
1143 		    entry->wired_count--;
1144 		    if (entry->wired_count == 0)
1145 			vm_fault_unwire(map, entry->start, entry->end);
1146 
1147 		    entry = entry->next;
1148 		}
1149 		lock_clear_recursive(&map->lock);
1150 	}
1151 
1152 	else {
1153 		/*
1154 		 *	Wiring.  We must do this in two passes:
1155 		 *
1156 		 *	1.  Holding the write lock, we increment the
1157 		 *	    wiring count.  For any area that is not already
1158 		 *	    wired, we create any shadow objects that need
1159 		 *	    to be created.
1160 		 *
1161 		 *	2.  We downgrade to a read lock, and call
1162 		 *	    vm_fault_wire to fault in the pages for any
1163 		 *	    newly wired area (wired_count is 1).
1164 		 *
1165 		 *	Downgrading to a read lock for vm_fault_wire avoids
1166 		 *	a possible deadlock with another thread that may have
1167 		 *	faulted on one of the pages to be wired (it would mark
1168 		 *	the page busy, blocking us, then in turn block on the
1169 		 *	map lock that we hold).  Because of problems in the
1170 		 *	recursive lock package, we cannot upgrade to a write
1171 		 *	lock in vm_map_lookup.  Thus, any actions that require
1172 		 *	the write lock must be done beforehand.  Because we
1173 		 *	keep the read lock on the map, the copy-on-write status
1174 		 *	of the entries we modify here cannot change.
1175 		 */
1176 
1177 		/*
1178 		 *	Pass 1.
1179 		 */
1180 		entry = temp_entry;
1181 		while ((entry != &map->header) && (entry->start < end)) {
1182 		    vm_map_clip_end(map, entry, end);
1183 
1184 		    entry->wired_count++;
1185 		    if (entry->wired_count == 1) {
1186 
1187 			/*
1188 			 *	Perform actions of vm_map_lookup that need
1189 			 *	the write lock on the map: create a shadow
1190 			 *	object for a copy-on-write region, or an
1191 			 *	object for a zero-fill region.
1192 			 *
1193 			 *	We don't have to do this for entries that
1194 			 *	point to sharing maps, because we won't hold
1195 			 *	the lock on the sharing map.
1196 			 */
1197 			if (!entry->is_a_map) {
1198 			    if (entry->needs_copy &&
1199 				((entry->protection & VM_PROT_WRITE) != 0)) {
1200 
1201 				vm_object_shadow(&entry->object.vm_object,
1202 						&entry->offset,
1203 						(vm_size_t)(entry->end
1204 							- entry->start));
1205 				entry->needs_copy = FALSE;
1206 			    }
1207 			    else if (entry->object.vm_object == NULL) {
1208 				entry->object.vm_object =
1209 				    vm_object_allocate((vm_size_t)(entry->end
1210 				    			- entry->start));
1211 				entry->offset = (vm_offset_t)0;
1212 			    }
1213 			}
1214 		    }
1215 
1216 		    entry = entry->next;
1217 		}
1218 
1219 		/*
1220 		 *	Pass 2.
1221 		 */
1222 
1223 		/*
1224 		 * HACK HACK HACK HACK
1225 		 *
1226 		 * If we are wiring in the kernel map or a submap of it,
1227 		 * unlock the map to avoid deadlocks.  We trust that the
1228 		 * kernel threads are well-behaved, and therefore will
1229 		 * not do anything destructive to this region of the map
1230 		 * while we have it unlocked.  We cannot trust user threads
1231 		 * to do the same.
1232 		 *
1233 		 * HACK HACK HACK HACK
1234 		 */
1235 		if (vm_map_pmap(map) == kernel_pmap) {
1236 		    vm_map_unlock(map);		/* trust me ... */
1237 		}
1238 		else {
1239 		    lock_set_recursive(&map->lock);
1240 		    lock_write_to_read(&map->lock);
1241 		}
1242 
1243 		entry = temp_entry;
1244 		while (entry != &map->header && entry->start < end) {
1245 		    if (entry->wired_count == 1) {
1246 			vm_fault_wire(map, entry->start, entry->end);
1247 		    }
1248 		    entry = entry->next;
1249 		}
1250 
1251 		if (vm_map_pmap(map) == kernel_pmap) {
1252 		    vm_map_lock(map);
1253 		}
1254 		else {
1255 		    lock_clear_recursive(&map->lock);
1256 		}
1257 	}
1258 
1259 	vm_map_unlock(map);
1260 
1261 	return(KERN_SUCCESS);
1262 }
1263 
1264 /*
1265  *	vm_map_entry_unwire:	[ internal use only ]
1266  *
1267  *	Make the region specified by this entry pageable.
1268  *
1269  *	The map in question should be locked.
1270  *	[This is the reason for this routine's existence.]
1271  */
1272 void vm_map_entry_unwire(map, entry)
1273 	vm_map_t		map;
1274 	register vm_map_entry_t	entry;
1275 {
1276 	vm_fault_unwire(map, entry->start, entry->end);
1277 	entry->wired_count = 0;
1278 }
1279 
1280 /*
1281  *	vm_map_entry_delete:	[ internal use only ]
1282  *
1283  *	Deallocate the given entry from the target map.
1284  */
1285 void vm_map_entry_delete(map, entry)
1286 	register vm_map_t	map;
1287 	register vm_map_entry_t	entry;
1288 {
1289 	if (entry->wired_count != 0)
1290 		vm_map_entry_unwire(map, entry);
1291 
1292 	vm_map_entry_unlink(map, entry);
1293 	map->size -= entry->end - entry->start;
1294 
1295 	if (entry->is_a_map || entry->is_sub_map)
1296 		vm_map_deallocate(entry->object.share_map);
1297 	else
1298 	 	vm_object_deallocate(entry->object.vm_object);
1299 
1300 	vm_map_entry_dispose(map, entry);
1301 }
1302 
1303 /*
1304  *	vm_map_delete:	[ internal use only ]
1305  *
1306  *	Deallocates the given address range from the target
1307  *	map.
1308  *
1309  *	When called with a sharing map, removes pages from
1310  *	that region from all physical maps.
1311  */
1312 int
1313 vm_map_delete(map, start, end)
1314 	register vm_map_t	map;
1315 	vm_offset_t		start;
1316 	register vm_offset_t	end;
1317 {
1318 	register vm_map_entry_t	entry;
1319 	vm_map_entry_t		first_entry;
1320 
1321 	/*
1322 	 *	Find the start of the region, and clip it
1323 	 */
1324 
1325 	if (!vm_map_lookup_entry(map, start, &first_entry))
1326 		entry = first_entry->next;
1327 	else {
1328 		entry = first_entry;
1329 		vm_map_clip_start(map, entry, start);
1330 
1331 		/*
1332 		 *	Fix the lookup hint now, rather than each
1333 		 *	time though the loop.
1334 		 */
1335 
1336 		SAVE_HINT(map, entry->prev);
1337 	}
1338 
1339 	/*
1340 	 *	Save the free space hint
1341 	 */
1342 
1343 	if (map->first_free->start >= start)
1344 		map->first_free = entry->prev;
1345 
1346 	/*
1347 	 *	Step through all entries in this region
1348 	 */
1349 
1350 	while ((entry != &map->header) && (entry->start < end)) {
1351 		vm_map_entry_t		next;
1352 		register vm_offset_t	s, e;
1353 		register vm_object_t	object;
1354 
1355 		vm_map_clip_end(map, entry, end);
1356 
1357 		next = entry->next;
1358 		s = entry->start;
1359 		e = entry->end;
1360 
1361 		/*
1362 		 *	Unwire before removing addresses from the pmap;
1363 		 *	otherwise, unwiring will put the entries back in
1364 		 *	the pmap.
1365 		 */
1366 
1367 		object = entry->object.vm_object;
1368 		if (entry->wired_count != 0)
1369 			vm_map_entry_unwire(map, entry);
1370 
1371 		/*
1372 		 *	If this is a sharing map, we must remove
1373 		 *	*all* references to this data, since we can't
1374 		 *	find all of the physical maps which are sharing
1375 		 *	it.
1376 		 */
1377 
1378 		if (object == kernel_object || object == kmem_object)
1379 			vm_object_page_remove(object, entry->offset,
1380 					entry->offset + (e - s));
1381 		else if (!map->is_main_map)
1382 			vm_object_pmap_remove(object,
1383 					 entry->offset,
1384 					 entry->offset + (e - s));
1385 		else
1386 			pmap_remove(map->pmap, s, e);
1387 
1388 		/*
1389 		 *	Delete the entry (which may delete the object)
1390 		 *	only after removing all pmap entries pointing
1391 		 *	to its pages.  (Otherwise, its page frames may
1392 		 *	be reallocated, and any modify bits will be
1393 		 *	set in the wrong object!)
1394 		 */
1395 
1396 		vm_map_entry_delete(map, entry);
1397 		entry = next;
1398 	}
1399 	return(KERN_SUCCESS);
1400 }
1401 
1402 /*
1403  *	vm_map_remove:
1404  *
1405  *	Remove the given address range from the target map.
1406  *	This is the exported form of vm_map_delete.
1407  */
1408 int
1409 vm_map_remove(map, start, end)
1410 	register vm_map_t	map;
1411 	register vm_offset_t	start;
1412 	register vm_offset_t	end;
1413 {
1414 	register int		result;
1415 
1416 	vm_map_lock(map);
1417 	VM_MAP_RANGE_CHECK(map, start, end);
1418 	result = vm_map_delete(map, start, end);
1419 	vm_map_unlock(map);
1420 
1421 	return(result);
1422 }
1423 
1424 /*
1425  *	vm_map_check_protection:
1426  *
1427  *	Assert that the target map allows the specified
1428  *	privilege on the entire address region given.
1429  *	The entire region must be allocated.
1430  */
1431 boolean_t vm_map_check_protection(map, start, end, protection)
1432 	register vm_map_t	map;
1433 	register vm_offset_t	start;
1434 	register vm_offset_t	end;
1435 	register vm_prot_t	protection;
1436 {
1437 	register vm_map_entry_t	entry;
1438 	vm_map_entry_t		tmp_entry;
1439 
1440 	if (!vm_map_lookup_entry(map, start, &tmp_entry)) {
1441 		return(FALSE);
1442 	}
1443 
1444 	entry = tmp_entry;
1445 
1446 	while (start < end) {
1447 		if (entry == &map->header) {
1448 			return(FALSE);
1449 		}
1450 
1451 		/*
1452 		 *	No holes allowed!
1453 		 */
1454 
1455 		if (start < entry->start) {
1456 			return(FALSE);
1457 		}
1458 
1459 		/*
1460 		 * Check protection associated with entry.
1461 		 */
1462 
1463 		if ((entry->protection & protection) != protection) {
1464 			return(FALSE);
1465 		}
1466 
1467 		/* go to next entry */
1468 
1469 		start = entry->end;
1470 		entry = entry->next;
1471 	}
1472 	return(TRUE);
1473 }
1474 
1475 /*
1476  *	vm_map_copy_entry:
1477  *
1478  *	Copies the contents of the source entry to the destination
1479  *	entry.  The entries *must* be aligned properly.
1480  */
1481 void vm_map_copy_entry(src_map, dst_map, src_entry, dst_entry)
1482 	vm_map_t		src_map, dst_map;
1483 	register vm_map_entry_t	src_entry, dst_entry;
1484 {
1485 	vm_object_t	temp_object;
1486 
1487 	if (src_entry->is_sub_map || dst_entry->is_sub_map)
1488 		return;
1489 
1490 	if (dst_entry->object.vm_object != NULL &&
1491 	    (dst_entry->object.vm_object->flags & OBJ_INTERNAL) == 0)
1492 		printf("vm_map_copy_entry: copying over permanent data!\n");
1493 
1494 	/*
1495 	 *	If our destination map was wired down,
1496 	 *	unwire it now.
1497 	 */
1498 
1499 	if (dst_entry->wired_count != 0)
1500 		vm_map_entry_unwire(dst_map, dst_entry);
1501 
1502 	/*
1503 	 *	If we're dealing with a sharing map, we
1504 	 *	must remove the destination pages from
1505 	 *	all maps (since we cannot know which maps
1506 	 *	this sharing map belongs in).
1507 	 */
1508 
1509 	if (dst_map->is_main_map)
1510 		pmap_remove(dst_map->pmap, dst_entry->start, dst_entry->end);
1511 	else
1512 		vm_object_pmap_remove(dst_entry->object.vm_object,
1513 			dst_entry->offset,
1514 			dst_entry->offset +
1515 				(dst_entry->end - dst_entry->start));
1516 
1517 	if (src_entry->wired_count == 0) {
1518 
1519 		boolean_t	src_needs_copy;
1520 
1521 		/*
1522 		 *	If the source entry is marked needs_copy,
1523 		 *	it is already write-protected.
1524 		 */
1525 		if (!src_entry->needs_copy) {
1526 
1527 			boolean_t	su;
1528 
1529 			/*
1530 			 *	If the source entry has only one mapping,
1531 			 *	we can just protect the virtual address
1532 			 *	range.
1533 			 */
1534 			if (!(su = src_map->is_main_map)) {
1535 				simple_lock(&src_map->ref_lock);
1536 				su = (src_map->ref_count == 1);
1537 				simple_unlock(&src_map->ref_lock);
1538 			}
1539 
1540 			if (su) {
1541 				pmap_protect(src_map->pmap,
1542 					src_entry->start,
1543 					src_entry->end,
1544 					src_entry->protection & ~VM_PROT_WRITE);
1545 			}
1546 			else {
1547 				vm_object_pmap_copy(src_entry->object.vm_object,
1548 					src_entry->offset,
1549 					src_entry->offset + (src_entry->end
1550 							    -src_entry->start));
1551 			}
1552 		}
1553 
1554 		/*
1555 		 *	Make a copy of the object.
1556 		 */
1557 		temp_object = dst_entry->object.vm_object;
1558 		vm_object_copy(src_entry->object.vm_object,
1559 				src_entry->offset,
1560 				(vm_size_t)(src_entry->end -
1561 					    src_entry->start),
1562 				&dst_entry->object.vm_object,
1563 				&dst_entry->offset,
1564 				&src_needs_copy);
1565 		/*
1566 		 *	If we didn't get a copy-object now, mark the
1567 		 *	source map entry so that a shadow will be created
1568 		 *	to hold its changed pages.
1569 		 */
1570 		if (src_needs_copy)
1571 			src_entry->needs_copy = TRUE;
1572 
1573 		/*
1574 		 *	The destination always needs to have a shadow
1575 		 *	created.
1576 		 */
1577 		dst_entry->needs_copy = TRUE;
1578 
1579 		/*
1580 		 *	Mark the entries copy-on-write, so that write-enabling
1581 		 *	the entry won't make copy-on-write pages writable.
1582 		 */
1583 		src_entry->copy_on_write = TRUE;
1584 		dst_entry->copy_on_write = TRUE;
1585 		/*
1586 		 *	Get rid of the old object.
1587 		 */
1588 		vm_object_deallocate(temp_object);
1589 
1590 		pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
1591 			dst_entry->end - dst_entry->start, src_entry->start);
1592 	}
1593 	else {
1594 		/*
1595 		 *	Of course, wired down pages can't be set copy-on-write.
1596 		 *	Cause wired pages to be copied into the new
1597 		 *	map by simulating faults (the new pages are
1598 		 *	pageable)
1599 		 */
1600 		vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry);
1601 	}
1602 }
1603 
1604 /*
1605  *	vm_map_copy:
1606  *
1607  *	Perform a virtual memory copy from the source
1608  *	address map/range to the destination map/range.
1609  *
1610  *	If src_destroy or dst_alloc is requested,
1611  *	the source and destination regions should be
1612  *	disjoint, not only in the top-level map, but
1613  *	in the sharing maps as well.  [The best way
1614  *	to guarantee this is to use a new intermediate
1615  *	map to make copies.  This also reduces map
1616  *	fragmentation.]
1617  */
1618 int
1619 vm_map_copy(dst_map, src_map,
1620 			  dst_addr, len, src_addr,
1621 			  dst_alloc, src_destroy)
1622 	vm_map_t	dst_map;
1623 	vm_map_t	src_map;
1624 	vm_offset_t	dst_addr;
1625 	vm_size_t	len;
1626 	vm_offset_t	src_addr;
1627 	boolean_t	dst_alloc;
1628 	boolean_t	src_destroy;
1629 {
1630 	register
1631 	vm_map_entry_t	src_entry;
1632 	register
1633 	vm_map_entry_t	dst_entry;
1634 	vm_map_entry_t	tmp_entry;
1635 	vm_offset_t	src_start;
1636 	vm_offset_t	src_end;
1637 	vm_offset_t	dst_start;
1638 	vm_offset_t	dst_end;
1639 	vm_offset_t	src_clip;
1640 	vm_offset_t	dst_clip;
1641 	int		result;
1642 	boolean_t	old_src_destroy;
1643 
1644 	/*
1645 	 *	XXX While we figure out why src_destroy screws up,
1646 	 *	we'll do it by explicitly vm_map_delete'ing at the end.
1647 	 */
1648 
1649 	old_src_destroy = src_destroy;
1650 	src_destroy = FALSE;
1651 
1652 	/*
1653 	 *	Compute start and end of region in both maps
1654 	 */
1655 
1656 	src_start = src_addr;
1657 	src_end = src_start + len;
1658 	dst_start = dst_addr;
1659 	dst_end = dst_start + len;
1660 
1661 	/*
1662 	 *	Check that the region can exist in both source
1663 	 *	and destination.
1664 	 */
1665 
1666 	if ((dst_end < dst_start) || (src_end < src_start))
1667 		return(KERN_NO_SPACE);
1668 
1669 	/*
1670 	 *	Lock the maps in question -- we avoid deadlock
1671 	 *	by ordering lock acquisition by map value
1672 	 */
1673 
1674 	if (src_map == dst_map) {
1675 		vm_map_lock(src_map);
1676 	}
1677 	else if ((int) src_map < (int) dst_map) {
1678 	 	vm_map_lock(src_map);
1679 		vm_map_lock(dst_map);
1680 	} else {
1681 		vm_map_lock(dst_map);
1682 	 	vm_map_lock(src_map);
1683 	}
1684 
1685 	result = KERN_SUCCESS;
1686 
1687 	/*
1688 	 *	Check protections... source must be completely readable and
1689 	 *	destination must be completely writable.  [Note that if we're
1690 	 *	allocating the destination region, we don't have to worry
1691 	 *	about protection, but instead about whether the region
1692 	 *	exists.]
1693 	 */
1694 
1695 	if (src_map->is_main_map && dst_map->is_main_map) {
1696 		if (!vm_map_check_protection(src_map, src_start, src_end,
1697 					VM_PROT_READ)) {
1698 			result = KERN_PROTECTION_FAILURE;
1699 			goto Return;
1700 		}
1701 
1702 		if (dst_alloc) {
1703 			/* XXX Consider making this a vm_map_find instead */
1704 			if ((result = vm_map_insert(dst_map, NULL,
1705 					(vm_offset_t) 0, dst_start, dst_end)) != KERN_SUCCESS)
1706 				goto Return;
1707 		}
1708 		else if (!vm_map_check_protection(dst_map, dst_start, dst_end,
1709 					VM_PROT_WRITE)) {
1710 			result = KERN_PROTECTION_FAILURE;
1711 			goto Return;
1712 		}
1713 	}
1714 
1715 	/*
1716 	 *	Find the start entries and clip.
1717 	 *
1718 	 *	Note that checking protection asserts that the
1719 	 *	lookup cannot fail.
1720 	 *
1721 	 *	Also note that we wait to do the second lookup
1722 	 *	until we have done the first clip, as the clip
1723 	 *	may affect which entry we get!
1724 	 */
1725 
1726 	(void) vm_map_lookup_entry(src_map, src_addr, &tmp_entry);
1727 	src_entry = tmp_entry;
1728 	vm_map_clip_start(src_map, src_entry, src_start);
1729 
1730 	(void) vm_map_lookup_entry(dst_map, dst_addr, &tmp_entry);
1731 	dst_entry = tmp_entry;
1732 	vm_map_clip_start(dst_map, dst_entry, dst_start);
1733 
1734 	/*
1735 	 *	If both source and destination entries are the same,
1736 	 *	retry the first lookup, as it may have changed.
1737 	 */
1738 
1739 	if (src_entry == dst_entry) {
1740 		(void) vm_map_lookup_entry(src_map, src_addr, &tmp_entry);
1741 		src_entry = tmp_entry;
1742 	}
1743 
1744 	/*
1745 	 *	If source and destination entries are still the same,
1746 	 *	a null copy is being performed.
1747 	 */
1748 
1749 	if (src_entry == dst_entry)
1750 		goto Return;
1751 
1752 	/*
1753 	 *	Go through entries until we get to the end of the
1754 	 *	region.
1755 	 */
1756 
1757 	while (src_start < src_end) {
1758 		/*
1759 		 *	Clip the entries to the endpoint of the entire region.
1760 		 */
1761 
1762 		vm_map_clip_end(src_map, src_entry, src_end);
1763 		vm_map_clip_end(dst_map, dst_entry, dst_end);
1764 
1765 		/*
1766 		 *	Clip each entry to the endpoint of the other entry.
1767 		 */
1768 
1769 		src_clip = src_entry->start + (dst_entry->end - dst_entry->start);
1770 		vm_map_clip_end(src_map, src_entry, src_clip);
1771 
1772 		dst_clip = dst_entry->start + (src_entry->end - src_entry->start);
1773 		vm_map_clip_end(dst_map, dst_entry, dst_clip);
1774 
1775 		/*
1776 		 *	Both entries now match in size and relative endpoints.
1777 		 *
1778 		 *	If both entries refer to a VM object, we can
1779 		 *	deal with them now.
1780 		 */
1781 
1782 		if (!src_entry->is_a_map && !dst_entry->is_a_map) {
1783 			vm_map_copy_entry(src_map, dst_map, src_entry,
1784 						dst_entry);
1785 		}
1786 		else {
1787 			register vm_map_t	new_dst_map;
1788 			vm_offset_t		new_dst_start;
1789 			vm_size_t		new_size;
1790 			vm_map_t		new_src_map;
1791 			vm_offset_t		new_src_start;
1792 
1793 			/*
1794 			 *	We have to follow at least one sharing map.
1795 			 */
1796 
1797 			new_size = (dst_entry->end - dst_entry->start);
1798 
1799 			if (src_entry->is_a_map) {
1800 				new_src_map = src_entry->object.share_map;
1801 				new_src_start = src_entry->offset;
1802 			}
1803 			else {
1804 			 	new_src_map = src_map;
1805 				new_src_start = src_entry->start;
1806 				lock_set_recursive(&src_map->lock);
1807 			}
1808 
1809 			if (dst_entry->is_a_map) {
1810 			    	vm_offset_t	new_dst_end;
1811 
1812 				new_dst_map = dst_entry->object.share_map;
1813 				new_dst_start = dst_entry->offset;
1814 
1815 				/*
1816 				 *	Since the destination sharing entries
1817 				 *	will be merely deallocated, we can
1818 				 *	do that now, and replace the region
1819 				 *	with a null object.  [This prevents
1820 				 *	splitting the source map to match
1821 				 *	the form of the destination map.]
1822 				 *	Note that we can only do so if the
1823 				 *	source and destination do not overlap.
1824 				 */
1825 
1826 				new_dst_end = new_dst_start + new_size;
1827 
1828 				if (new_dst_map != new_src_map) {
1829 					vm_map_lock(new_dst_map);
1830 					(void) vm_map_delete(new_dst_map,
1831 							new_dst_start,
1832 							new_dst_end);
1833 					(void) vm_map_insert(new_dst_map,
1834 							NULL,
1835 							(vm_offset_t) 0,
1836 							new_dst_start,
1837 							new_dst_end);
1838 					vm_map_unlock(new_dst_map);
1839 				}
1840 			}
1841 			else {
1842 			 	new_dst_map = dst_map;
1843 				new_dst_start = dst_entry->start;
1844 				lock_set_recursive(&dst_map->lock);
1845 			}
1846 
1847 			/*
1848 			 *	Recursively copy the sharing map.
1849 			 */
1850 
1851 			(void) vm_map_copy(new_dst_map, new_src_map,
1852 				new_dst_start, new_size, new_src_start,
1853 				FALSE, FALSE);
1854 
1855 			if (dst_map == new_dst_map)
1856 				lock_clear_recursive(&dst_map->lock);
1857 			if (src_map == new_src_map)
1858 				lock_clear_recursive(&src_map->lock);
1859 		}
1860 
1861 		/*
1862 		 *	Update variables for next pass through the loop.
1863 		 */
1864 
1865 		src_start = src_entry->end;
1866 		src_entry = src_entry->next;
1867 		dst_start = dst_entry->end;
1868 		dst_entry = dst_entry->next;
1869 
1870 		/*
1871 		 *	If the source is to be destroyed, here is the
1872 		 *	place to do it.
1873 		 */
1874 
1875 		if (src_destroy && src_map->is_main_map &&
1876 						dst_map->is_main_map)
1877 			vm_map_entry_delete(src_map, src_entry->prev);
1878 	}
1879 
1880 	/*
1881 	 *	Update the physical maps as appropriate
1882 	 */
1883 
1884 	if (src_map->is_main_map && dst_map->is_main_map) {
1885 		if (src_destroy)
1886 			pmap_remove(src_map->pmap, src_addr, src_addr + len);
1887 	}
1888 
1889 	/*
1890 	 *	Unlock the maps
1891 	 */
1892 
1893 	Return: ;
1894 
1895 	if (old_src_destroy)
1896 		vm_map_delete(src_map, src_addr, src_addr + len);
1897 
1898 	vm_map_unlock(src_map);
1899 	if (src_map != dst_map)
1900 		vm_map_unlock(dst_map);
1901 
1902 	return(result);
1903 }
1904 
1905 /*
1906  * vmspace_fork:
1907  * Create a new process vmspace structure and vm_map
1908  * based on those of an existing process.  The new map
1909  * is based on the old map, according to the inheritance
1910  * values on the regions in that map.
1911  *
1912  * The source map must not be locked.
1913  */
1914 struct vmspace *
1915 vmspace_fork(vm1)
1916 	register struct vmspace *vm1;
1917 {
1918 	register struct vmspace *vm2;
1919 	vm_map_t	old_map = &vm1->vm_map;
1920 	vm_map_t	new_map;
1921 	vm_map_entry_t	old_entry;
1922 	vm_map_entry_t	new_entry;
1923 	pmap_t		new_pmap;
1924 
1925 	vm_map_lock(old_map);
1926 
1927 	vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset,
1928 	    old_map->entries_pageable);
1929 	bcopy(&vm1->vm_startcopy, &vm2->vm_startcopy,
1930 	    (caddr_t) (vm1 + 1) - (caddr_t) &vm1->vm_startcopy);
1931 	new_pmap = &vm2->vm_pmap;		/* XXX */
1932 	new_map = &vm2->vm_map;			/* XXX */
1933 
1934 	old_entry = old_map->header.next;
1935 
1936 	while (old_entry != &old_map->header) {
1937 		if (old_entry->is_sub_map)
1938 			panic("vm_map_fork: encountered a submap");
1939 
1940 		switch (old_entry->inheritance) {
1941 		case VM_INHERIT_NONE:
1942 			break;
1943 
1944 		case VM_INHERIT_SHARE:
1945 			/*
1946 			 *	If we don't already have a sharing map:
1947 			 */
1948 
1949 			if (!old_entry->is_a_map) {
1950 			 	vm_map_t	new_share_map;
1951 				vm_map_entry_t	new_share_entry;
1952 
1953 				/*
1954 				 *	Create a new sharing map
1955 				 */
1956 
1957 				new_share_map = vm_map_create(NULL,
1958 							old_entry->start,
1959 							old_entry->end,
1960 							TRUE);
1961 				new_share_map->is_main_map = FALSE;
1962 
1963 				/*
1964 				 *	Create the only sharing entry from the
1965 				 *	old task map entry.
1966 				 */
1967 
1968 				new_share_entry =
1969 					vm_map_entry_create(new_share_map);
1970 				*new_share_entry = *old_entry;
1971 
1972 				/*
1973 				 *	Insert the entry into the new sharing
1974 				 *	map
1975 				 */
1976 
1977 				vm_map_entry_link(new_share_map,
1978 						new_share_map->header.prev,
1979 						new_share_entry);
1980 
1981 				/*
1982 				 *	Fix up the task map entry to refer
1983 				 *	to the sharing map now.
1984 				 */
1985 
1986 				old_entry->is_a_map = TRUE;
1987 				old_entry->object.share_map = new_share_map;
1988 				old_entry->offset = old_entry->start;
1989 			}
1990 
1991 			/*
1992 			 *	Clone the entry, referencing the sharing map.
1993 			 */
1994 
1995 			new_entry = vm_map_entry_create(new_map);
1996 			*new_entry = *old_entry;
1997 			vm_map_reference(new_entry->object.share_map);
1998 
1999 			/*
2000 			 *	Insert the entry into the new map -- we
2001 			 *	know we're inserting at the end of the new
2002 			 *	map.
2003 			 */
2004 
2005 			vm_map_entry_link(new_map, new_map->header.prev,
2006 						new_entry);
2007 
2008 			/*
2009 			 *	Update the physical map
2010 			 */
2011 
2012 			pmap_copy(new_map->pmap, old_map->pmap,
2013 				new_entry->start,
2014 				(old_entry->end - old_entry->start),
2015 				old_entry->start);
2016 			break;
2017 
2018 		case VM_INHERIT_COPY:
2019 			/*
2020 			 *	Clone the entry and link into the map.
2021 			 */
2022 
2023 			new_entry = vm_map_entry_create(new_map);
2024 			*new_entry = *old_entry;
2025 			new_entry->wired_count = 0;
2026 			new_entry->object.vm_object = NULL;
2027 			new_entry->is_a_map = FALSE;
2028 			vm_map_entry_link(new_map, new_map->header.prev,
2029 							new_entry);
2030 			if (old_entry->is_a_map) {
2031 				int	check;
2032 
2033 				check = vm_map_copy(new_map,
2034 						old_entry->object.share_map,
2035 						new_entry->start,
2036 						(vm_size_t)(new_entry->end -
2037 							new_entry->start),
2038 						old_entry->offset,
2039 						FALSE, FALSE);
2040 				if (check != KERN_SUCCESS)
2041 					printf("vm_map_fork: copy in share_map region failed\n");
2042 			}
2043 			else {
2044 				vm_map_copy_entry(old_map, new_map, old_entry,
2045 						new_entry);
2046 			}
2047 			break;
2048 		}
2049 		old_entry = old_entry->next;
2050 	}
2051 
2052 	new_map->size = old_map->size;
2053 	vm_map_unlock(old_map);
2054 
2055 	return(vm2);
2056 }
2057 
2058 /*
2059  *	vm_map_lookup:
2060  *
2061  *	Finds the VM object, offset, and
2062  *	protection for a given virtual address in the
2063  *	specified map, assuming a page fault of the
2064  *	type specified.
2065  *
2066  *	Leaves the map in question locked for read; return
2067  *	values are guaranteed until a vm_map_lookup_done
2068  *	call is performed.  Note that the map argument
2069  *	is in/out; the returned map must be used in
2070  *	the call to vm_map_lookup_done.
2071  *
2072  *	A handle (out_entry) is returned for use in
2073  *	vm_map_lookup_done, to make that fast.
2074  *
2075  *	If a lookup is requested with "write protection"
2076  *	specified, the map may be changed to perform virtual
2077  *	copying operations, although the data referenced will
2078  *	remain the same.
2079  */
2080 int
2081 vm_map_lookup(var_map, vaddr, fault_type, out_entry,
2082 				object, offset, out_prot, wired, single_use)
2083 	vm_map_t		*var_map;	/* IN/OUT */
2084 	register vm_offset_t	vaddr;
2085 	register vm_prot_t	fault_type;
2086 
2087 	vm_map_entry_t		*out_entry;	/* OUT */
2088 	vm_object_t		*object;	/* OUT */
2089 	vm_offset_t		*offset;	/* OUT */
2090 	vm_prot_t		*out_prot;	/* OUT */
2091 	boolean_t		*wired;		/* OUT */
2092 	boolean_t		*single_use;	/* OUT */
2093 {
2094 	vm_map_t			share_map;
2095 	vm_offset_t			share_offset;
2096 	register vm_map_entry_t		entry;
2097 	register vm_map_t		map = *var_map;
2098 	register vm_prot_t		prot;
2099 	register boolean_t		su;
2100 
2101 	RetryLookup: ;
2102 
2103 	/*
2104 	 *	Lookup the faulting address.
2105 	 */
2106 
2107 	vm_map_lock_read(map);
2108 
2109 #define	RETURN(why) \
2110 		{ \
2111 		vm_map_unlock_read(map); \
2112 		return(why); \
2113 		}
2114 
2115 	/*
2116 	 *	If the map has an interesting hint, try it before calling
2117 	 *	full blown lookup routine.
2118 	 */
2119 
2120 	simple_lock(&map->hint_lock);
2121 	entry = map->hint;
2122 	simple_unlock(&map->hint_lock);
2123 
2124 	*out_entry = entry;
2125 
2126 	if ((entry == &map->header) ||
2127 	    (vaddr < entry->start) || (vaddr >= entry->end)) {
2128 		vm_map_entry_t	tmp_entry;
2129 
2130 		/*
2131 		 *	Entry was either not a valid hint, or the vaddr
2132 		 *	was not contained in the entry, so do a full lookup.
2133 		 */
2134 		if (!vm_map_lookup_entry(map, vaddr, &tmp_entry))
2135 			RETURN(KERN_INVALID_ADDRESS);
2136 
2137 		entry = tmp_entry;
2138 		*out_entry = entry;
2139 	}
2140 
2141 	/*
2142 	 *	Handle submaps.
2143 	 */
2144 
2145 	if (entry->is_sub_map) {
2146 		vm_map_t	old_map = map;
2147 
2148 		*var_map = map = entry->object.sub_map;
2149 		vm_map_unlock_read(old_map);
2150 		goto RetryLookup;
2151 	}
2152 
2153 	/*
2154 	 *	Check whether this task is allowed to have
2155 	 *	this page.
2156 	 */
2157 
2158 	prot = entry->protection;
2159 	if ((fault_type & (prot)) != fault_type)
2160 		RETURN(KERN_PROTECTION_FAILURE);
2161 
2162 	/*
2163 	 *	If this page is not pageable, we have to get
2164 	 *	it for all possible accesses.
2165 	 */
2166 
2167 	if (*wired = (entry->wired_count != 0))
2168 		prot = fault_type = entry->protection;
2169 
2170 	/*
2171 	 *	If we don't already have a VM object, track
2172 	 *	it down.
2173 	 */
2174 
2175 	if (su = !entry->is_a_map) {
2176 	 	share_map = map;
2177 		share_offset = vaddr;
2178 	}
2179 	else {
2180 		vm_map_entry_t	share_entry;
2181 
2182 		/*
2183 		 *	Compute the sharing map, and offset into it.
2184 		 */
2185 
2186 		share_map = entry->object.share_map;
2187 		share_offset = (vaddr - entry->start) + entry->offset;
2188 
2189 		/*
2190 		 *	Look for the backing store object and offset
2191 		 */
2192 
2193 		vm_map_lock_read(share_map);
2194 
2195 		if (!vm_map_lookup_entry(share_map, share_offset,
2196 					&share_entry)) {
2197 			vm_map_unlock_read(share_map);
2198 			RETURN(KERN_INVALID_ADDRESS);
2199 		}
2200 		entry = share_entry;
2201 	}
2202 
2203 	/*
2204 	 *	If the entry was copy-on-write, we either ...
2205 	 */
2206 
2207 	if (entry->needs_copy) {
2208 	    	/*
2209 		 *	If we want to write the page, we may as well
2210 		 *	handle that now since we've got the sharing
2211 		 *	map locked.
2212 		 *
2213 		 *	If we don't need to write the page, we just
2214 		 *	demote the permissions allowed.
2215 		 */
2216 
2217 		if (fault_type & VM_PROT_WRITE) {
2218 			/*
2219 			 *	Make a new object, and place it in the
2220 			 *	object chain.  Note that no new references
2221 			 *	have appeared -- one just moved from the
2222 			 *	share map to the new object.
2223 			 */
2224 
2225 			if (lock_read_to_write(&share_map->lock)) {
2226 				if (share_map != map)
2227 					vm_map_unlock_read(map);
2228 				goto RetryLookup;
2229 			}
2230 
2231 			vm_object_shadow(
2232 				&entry->object.vm_object,
2233 				&entry->offset,
2234 				(vm_size_t) (entry->end - entry->start));
2235 
2236 			entry->needs_copy = FALSE;
2237 
2238 			lock_write_to_read(&share_map->lock);
2239 		}
2240 		else {
2241 			/*
2242 			 *	We're attempting to read a copy-on-write
2243 			 *	page -- don't allow writes.
2244 			 */
2245 
2246 			prot &= (~VM_PROT_WRITE);
2247 		}
2248 	}
2249 
2250 	/*
2251 	 *	Create an object if necessary.
2252 	 */
2253 	if (entry->object.vm_object == NULL) {
2254 
2255 		if (lock_read_to_write(&share_map->lock)) {
2256 			if (share_map != map)
2257 				vm_map_unlock_read(map);
2258 			goto RetryLookup;
2259 		}
2260 
2261 		entry->object.vm_object = vm_object_allocate(
2262 					(vm_size_t)(entry->end - entry->start));
2263 		entry->offset = 0;
2264 		lock_write_to_read(&share_map->lock);
2265 	}
2266 
2267 	/*
2268 	 *	Return the object/offset from this entry.  If the entry
2269 	 *	was copy-on-write or empty, it has been fixed up.
2270 	 */
2271 
2272 	*offset = (share_offset - entry->start) + entry->offset;
2273 	*object = entry->object.vm_object;
2274 
2275 	/*
2276 	 *	Return whether this is the only map sharing this data.
2277 	 */
2278 
2279 	if (!su) {
2280 		simple_lock(&share_map->ref_lock);
2281 		su = (share_map->ref_count == 1);
2282 		simple_unlock(&share_map->ref_lock);
2283 	}
2284 
2285 	*out_prot = prot;
2286 	*single_use = su;
2287 
2288 	return(KERN_SUCCESS);
2289 
2290 #undef	RETURN
2291 }
2292 
2293 /*
2294  *	vm_map_lookup_done:
2295  *
2296  *	Releases locks acquired by a vm_map_lookup
2297  *	(according to the handle returned by that lookup).
2298  */
2299 
2300 void vm_map_lookup_done(map, entry)
2301 	register vm_map_t	map;
2302 	vm_map_entry_t		entry;
2303 {
2304 	/*
2305 	 *	If this entry references a map, unlock it first.
2306 	 */
2307 
2308 	if (entry->is_a_map)
2309 		vm_map_unlock_read(entry->object.share_map);
2310 
2311 	/*
2312 	 *	Unlock the main-level map
2313 	 */
2314 
2315 	vm_map_unlock_read(map);
2316 }
2317 
2318 /*
2319  *	Routine:	vm_map_simplify
2320  *	Purpose:
2321  *		Attempt to simplify the map representation in
2322  *		the vicinity of the given starting address.
2323  *	Note:
2324  *		This routine is intended primarily to keep the
2325  *		kernel maps more compact -- they generally don't
2326  *		benefit from the "expand a map entry" technology
2327  *		at allocation time because the adjacent entry
2328  *		is often wired down.
2329  */
2330 void vm_map_simplify(map, start)
2331 	vm_map_t	map;
2332 	vm_offset_t	start;
2333 {
2334 	vm_map_entry_t	this_entry;
2335 	vm_map_entry_t	prev_entry;
2336 
2337 	vm_map_lock(map);
2338 	if (
2339 		(vm_map_lookup_entry(map, start, &this_entry)) &&
2340 		((prev_entry = this_entry->prev) != &map->header) &&
2341 
2342 		(prev_entry->end == start) &&
2343 		(map->is_main_map) &&
2344 
2345 		(prev_entry->is_a_map == FALSE) &&
2346 		(prev_entry->is_sub_map == FALSE) &&
2347 
2348 		(this_entry->is_a_map == FALSE) &&
2349 		(this_entry->is_sub_map == FALSE) &&
2350 
2351 		(prev_entry->inheritance == this_entry->inheritance) &&
2352 		(prev_entry->protection == this_entry->protection) &&
2353 		(prev_entry->max_protection == this_entry->max_protection) &&
2354 		(prev_entry->wired_count == this_entry->wired_count) &&
2355 
2356 		(prev_entry->copy_on_write == this_entry->copy_on_write) &&
2357 		(prev_entry->needs_copy == this_entry->needs_copy) &&
2358 
2359 		(prev_entry->object.vm_object == this_entry->object.vm_object) &&
2360 		((prev_entry->offset + (prev_entry->end - prev_entry->start))
2361 		     == this_entry->offset)
2362 	) {
2363 		if (map->first_free == this_entry)
2364 			map->first_free = prev_entry;
2365 
2366 		SAVE_HINT(map, prev_entry);
2367 		vm_map_entry_unlink(map, this_entry);
2368 		prev_entry->end = this_entry->end;
2369 	 	vm_object_deallocate(this_entry->object.vm_object);
2370 		vm_map_entry_dispose(map, this_entry);
2371 	}
2372 	vm_map_unlock(map);
2373 }
2374 
2375 /*
2376  *	vm_map_print:	[ debug ]
2377  */
2378 void vm_map_print(map, full)
2379 	register vm_map_t	map;
2380 	boolean_t		full;
2381 {
2382 	register vm_map_entry_t	entry;
2383 	extern int indent;
2384 
2385 	iprintf("%s map 0x%x: pmap=0x%x,ref=%d,nentries=%d,version=%d\n",
2386 		(map->is_main_map ? "Task" : "Share"),
2387  		(int) map, (int) (map->pmap), map->ref_count, map->nentries,
2388 		map->timestamp);
2389 
2390 	if (!full && indent)
2391 		return;
2392 
2393 	indent += 2;
2394 	for (entry = map->header.next; entry != &map->header;
2395 				entry = entry->next) {
2396 		iprintf("map entry 0x%x: start=0x%x, end=0x%x, ",
2397 			(int) entry, (int) entry->start, (int) entry->end);
2398 		if (map->is_main_map) {
2399 		     	static char *inheritance_name[4] =
2400 				{ "share", "copy", "none", "donate_copy"};
2401 			printf("prot=%x/%x/%s, ",
2402 				entry->protection,
2403 				entry->max_protection,
2404 				inheritance_name[entry->inheritance]);
2405 			if (entry->wired_count != 0)
2406 				printf("wired, ");
2407 		}
2408 
2409 		if (entry->is_a_map || entry->is_sub_map) {
2410 		 	printf("share=0x%x, offset=0x%x\n",
2411 				(int) entry->object.share_map,
2412 				(int) entry->offset);
2413 			if ((entry->prev == &map->header) ||
2414 			    (!entry->prev->is_a_map) ||
2415 			    (entry->prev->object.share_map !=
2416 			     entry->object.share_map)) {
2417 				indent += 2;
2418 				vm_map_print(entry->object.share_map, full);
2419 				indent -= 2;
2420 			}
2421 
2422 		}
2423 		else {
2424 			printf("object=0x%x, offset=0x%x",
2425 				(int) entry->object.vm_object,
2426 				(int) entry->offset);
2427 			if (entry->copy_on_write)
2428 				printf(", copy (%s)",
2429 				       entry->needs_copy ? "needed" : "done");
2430 			printf("\n");
2431 
2432 			if ((entry->prev == &map->header) ||
2433 			    (entry->prev->is_a_map) ||
2434 			    (entry->prev->object.vm_object !=
2435 			     entry->object.vm_object)) {
2436 				indent += 2;
2437 				vm_object_print(entry->object.vm_object, full);
2438 				indent -= 2;
2439 			}
2440 		}
2441 	}
2442 	indent -= 2;
2443 }
2444