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