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