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