xref: /freebsd/sys/vm/vm_map.c (revision 05c7a37afb48ddd5ee1bd921a5d46fe59cc70b15)
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.41 1996/03/28 04:53: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_pmap.pm_map = &vm->vm_map;
231 	vm->vm_refcnt = 1;
232 	return (vm);
233 }
234 
235 void
236 vmspace_free(vm)
237 	register struct vmspace *vm;
238 {
239 
240 	if (vm->vm_refcnt == 0)
241 		panic("vmspace_free: attempt to free already freed vmspace");
242 
243 	if (--vm->vm_refcnt == 0) {
244 		int s, i;
245 
246 		/*
247 		 * Lock the map, to wait out all other references to it.
248 		 * Delete all of the mappings and pages they hold, then call
249 		 * the pmap module to reclaim anything left.
250 		 */
251 		vm_map_lock(&vm->vm_map);
252 		(void) vm_map_delete(&vm->vm_map, vm->vm_map.min_offset,
253 		    vm->vm_map.max_offset);
254 		vm_map_unlock(&vm->vm_map);
255 		while( vm->vm_map.ref_count != 1)
256 			tsleep(&vm->vm_map.ref_count, PVM, "vmsfre", 0);
257 		--vm->vm_map.ref_count;
258 		pmap_release(&vm->vm_pmap);
259 		FREE(vm, M_VMMAP);
260 	}
261 }
262 
263 /*
264  *	vm_map_create:
265  *
266  *	Creates and returns a new empty VM map with
267  *	the given physical map structure, and having
268  *	the given lower and upper address bounds.
269  */
270 vm_map_t
271 vm_map_create(pmap, min, max, pageable)
272 	pmap_t pmap;
273 	vm_offset_t min, max;
274 	boolean_t pageable;
275 {
276 	register vm_map_t result;
277 
278 	if (kmem_map == NULL) {
279 		result = kmap_free;
280 		kmap_free = (vm_map_t) result->header.next;
281 		if (result == NULL)
282 			panic("vm_map_create: out of maps");
283 	} else
284 		MALLOC(result, vm_map_t, sizeof(struct vm_map),
285 		    M_VMMAP, M_WAITOK);
286 
287 	vm_map_init(result, min, max, pageable);
288 	result->pmap = pmap;
289 	return (result);
290 }
291 
292 /*
293  * Initialize an existing vm_map structure
294  * such as that in the vmspace structure.
295  * The pmap is set elsewhere.
296  */
297 void
298 vm_map_init(map, min, max, pageable)
299 	register struct vm_map *map;
300 	vm_offset_t min, max;
301 	boolean_t pageable;
302 {
303 	map->header.next = map->header.prev = &map->header;
304 	map->nentries = 0;
305 	map->size = 0;
306 	map->ref_count = 1;
307 	map->is_main_map = TRUE;
308 	map->min_offset = min;
309 	map->max_offset = max;
310 	map->entries_pageable = pageable;
311 	map->first_free = &map->header;
312 	map->hint = &map->header;
313 	map->timestamp = 0;
314 	lock_init(&map->lock, TRUE);
315 }
316 
317 /*
318  *	vm_map_entry_create:	[ internal use only ]
319  *
320  *	Allocates a VM map entry for insertion.
321  *	No entry fields are filled in.  This routine is
322  */
323 static struct vm_map_entry *mappool;
324 static int mappoolcnt;
325 
326 static vm_map_entry_t
327 vm_map_entry_create(map)
328 	vm_map_t map;
329 {
330 	vm_map_entry_t entry;
331 	int i;
332 
333 #define KENTRY_LOW_WATER 64
334 #define MAPENTRY_LOW_WATER 128
335 
336 	/*
337 	 * This is a *very* nasty (and sort of incomplete) hack!!!!
338 	 */
339 	if (kentry_count < KENTRY_LOW_WATER) {
340 		if (mapvmpgcnt && mapvm) {
341 			vm_page_t m;
342 
343 			m = vm_page_alloc(kmem_object,
344 			        OFF_TO_IDX(mapvm - vm_map_min(kmem_map)),
345 				    (map == kmem_map) ? VM_ALLOC_INTERRUPT : VM_ALLOC_NORMAL);
346 			if (m) {
347 				int newentries;
348 
349 				newentries = (PAGE_SIZE / sizeof(struct vm_map_entry));
350 				vm_page_wire(m);
351 				m->flags &= ~PG_BUSY;
352 				m->valid = VM_PAGE_BITS_ALL;
353 				pmap_enter(vm_map_pmap(kmem_map), mapvm,
354 				    VM_PAGE_TO_PHYS(m), VM_PROT_DEFAULT, 1);
355 				m->flags |= PG_WRITEABLE|PG_MAPPED;
356 
357 				entry = (vm_map_entry_t) mapvm;
358 				mapvm += PAGE_SIZE;
359 				--mapvmpgcnt;
360 
361 				for (i = 0; i < newentries; i++) {
362 					vm_map_entry_dispose(kernel_map, entry);
363 					entry++;
364 				}
365 			}
366 		}
367 	}
368 	if (map == kernel_map || map == kmem_map || map == pager_map) {
369 
370 		entry = kentry_free;
371 		if (entry) {
372 			kentry_free = entry->next;
373 			--kentry_count;
374 			return entry;
375 		}
376 		entry = mappool;
377 		if (entry) {
378 			mappool = entry->next;
379 			--mappoolcnt;
380 			return entry;
381 		}
382 	} else {
383 		entry = mappool;
384 		if (entry) {
385 			mappool = entry->next;
386 			--mappoolcnt;
387 			return entry;
388 		}
389 		MALLOC(entry, vm_map_entry_t, sizeof(struct vm_map_entry),
390 		    M_VMMAPENT, M_WAITOK);
391 	}
392 	if (entry == NULL)
393 		panic("vm_map_entry_create: out of map entries");
394 
395 	return (entry);
396 }
397 
398 /*
399  *	vm_map_entry_dispose:	[ internal use only ]
400  *
401  *	Inverse of vm_map_entry_create.
402  */
403 static void
404 vm_map_entry_dispose(map, entry)
405 	vm_map_t map;
406 	vm_map_entry_t entry;
407 {
408 	if ((kentry_count < KENTRY_LOW_WATER) ||
409 	    ((vm_offset_t) entry >= kentry_data && (vm_offset_t) entry < (kentry_data + kentry_data_size)) ||
410 	    ((vm_offset_t) entry >= mapvm_start && (vm_offset_t) entry < mapvmmax)) {
411 		entry->next = kentry_free;
412 		kentry_free = entry;
413 		++kentry_count;
414 		return;
415 	} else {
416 		if (mappoolcnt < MAPENTRY_LOW_WATER) {
417 			entry->next = mappool;
418 			mappool = entry;
419 			++mappoolcnt;
420 			return;
421 		}
422 		FREE(entry, M_VMMAPENT);
423 	}
424 }
425 
426 /*
427  *	vm_map_entry_{un,}link:
428  *
429  *	Insert/remove entries from maps.
430  */
431 #define	vm_map_entry_link(map, after_where, entry) \
432 		{ \
433 		(map)->nentries++; \
434 		(entry)->prev = (after_where); \
435 		(entry)->next = (after_where)->next; \
436 		(entry)->prev->next = (entry); \
437 		(entry)->next->prev = (entry); \
438 		}
439 #define	vm_map_entry_unlink(map, entry) \
440 		{ \
441 		(map)->nentries--; \
442 		(entry)->next->prev = (entry)->prev; \
443 		(entry)->prev->next = (entry)->next; \
444 		}
445 
446 /*
447  *	vm_map_reference:
448  *
449  *	Creates another valid reference to the given map.
450  *
451  */
452 void
453 vm_map_reference(map)
454 	register vm_map_t map;
455 {
456 	if (map == NULL)
457 		return;
458 
459 	map->ref_count++;
460 }
461 
462 /*
463  *	vm_map_deallocate:
464  *
465  *	Removes a reference from the specified map,
466  *	destroying it if no references remain.
467  *	The map should not be locked.
468  */
469 void
470 vm_map_deallocate(map)
471 	register vm_map_t map;
472 {
473 	register int c;
474 
475 	if (map == NULL)
476 		return;
477 
478 	c = map->ref_count;
479 
480 	if (c == 0)
481 		panic("vm_map_deallocate: deallocating already freed map");
482 
483 	if (c != 1) {
484 		--map->ref_count;
485 		wakeup(&map->ref_count);
486 		return;
487 	}
488 	/*
489 	 * Lock the map, to wait out all other references to it.
490 	 */
491 
492 	vm_map_lock(map);
493 	(void) vm_map_delete(map, map->min_offset, map->max_offset);
494 	--map->ref_count;
495 	if( map->ref_count != 0) {
496 		vm_map_unlock(map);
497 		return;
498 	}
499 
500 	pmap_destroy(map->pmap);
501 	FREE(map, M_VMMAP);
502 }
503 
504 /*
505  *	SAVE_HINT:
506  *
507  *	Saves the specified entry as the hint for
508  *	future lookups.
509  */
510 #define	SAVE_HINT(map,value) \
511 		(map)->hint = (value);
512 
513 /*
514  *	vm_map_lookup_entry:	[ internal use only ]
515  *
516  *	Finds the map entry containing (or
517  *	immediately preceding) the specified address
518  *	in the given map; the entry is returned
519  *	in the "entry" parameter.  The boolean
520  *	result indicates whether the address is
521  *	actually contained in the map.
522  */
523 boolean_t
524 vm_map_lookup_entry(map, address, entry)
525 	register vm_map_t map;
526 	register vm_offset_t address;
527 	vm_map_entry_t *entry;	/* OUT */
528 {
529 	register vm_map_entry_t cur;
530 	register vm_map_entry_t last;
531 
532 	/*
533 	 * Start looking either from the head of the list, or from the hint.
534 	 */
535 
536 	cur = map->hint;
537 
538 	if (cur == &map->header)
539 		cur = cur->next;
540 
541 	if (address >= cur->start) {
542 		/*
543 		 * Go from hint to end of list.
544 		 *
545 		 * But first, make a quick check to see if we are already looking
546 		 * at the entry we want (which is usually the case). Note also
547 		 * that we don't need to save the hint here... it is the same
548 		 * hint (unless we are at the header, in which case the hint
549 		 * didn't buy us anything anyway).
550 		 */
551 		last = &map->header;
552 		if ((cur != last) && (cur->end > address)) {
553 			*entry = cur;
554 			return (TRUE);
555 		}
556 	} else {
557 		/*
558 		 * Go from start to hint, *inclusively*
559 		 */
560 		last = cur->next;
561 		cur = map->header.next;
562 	}
563 
564 	/*
565 	 * Search linearly
566 	 */
567 
568 	while (cur != last) {
569 		if (cur->end > address) {
570 			if (address >= cur->start) {
571 				/*
572 				 * Save this lookup for future hints, and
573 				 * return
574 				 */
575 
576 				*entry = cur;
577 				SAVE_HINT(map, cur);
578 				return (TRUE);
579 			}
580 			break;
581 		}
582 		cur = cur->next;
583 	}
584 	*entry = cur->prev;
585 	SAVE_HINT(map, *entry);
586 	return (FALSE);
587 }
588 
589 /*
590  *	vm_map_insert:
591  *
592  *	Inserts the given whole VM object into the target
593  *	map at the specified address range.  The object's
594  *	size should match that of the address range.
595  *
596  *	Requires that the map be locked, and leaves it so.
597  */
598 int
599 vm_map_insert(map, object, offset, start, end, prot, max, cow)
600 	vm_map_t map;
601 	vm_object_t object;
602 	vm_ooffset_t offset;
603 	vm_offset_t start;
604 	vm_offset_t end;
605 	vm_prot_t prot, max;
606 	int cow;
607 {
608 	register vm_map_entry_t new_entry;
609 	register vm_map_entry_t prev_entry;
610 	vm_map_entry_t temp_entry;
611 
612 	/*
613 	 * Check that the start and end points are not bogus.
614 	 */
615 
616 	if ((start < map->min_offset) || (end > map->max_offset) ||
617 	    (start >= end))
618 		return (KERN_INVALID_ADDRESS);
619 
620 	/*
621 	 * Find the entry prior to the proposed starting address; if it's part
622 	 * of an existing entry, this range is bogus.
623 	 */
624 
625 	if (vm_map_lookup_entry(map, start, &temp_entry))
626 		return (KERN_NO_SPACE);
627 
628 	prev_entry = temp_entry;
629 
630 	/*
631 	 * Assert that the next entry doesn't overlap the end point.
632 	 */
633 
634 	if ((prev_entry->next != &map->header) &&
635 	    (prev_entry->next->start < end))
636 		return (KERN_NO_SPACE);
637 
638 	if ((prev_entry != &map->header) &&
639 		(prev_entry->end == start) &&
640 		(prev_entry->is_a_map == FALSE) &&
641 		(prev_entry->is_sub_map == FALSE) &&
642 		((object == NULL) || (prev_entry->object.vm_object == object)) &&
643 		(prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
644 		(prev_entry->protection == prot) &&
645 		(prev_entry->max_protection == max) &&
646 		(prev_entry->wired_count == 0)) {
647 	/*
648 	 * See if we can avoid creating a new entry by extending one of our
649 	 * neighbors.
650 	 */
651 
652 		if (object == NULL) {
653 			if (vm_object_coalesce(prev_entry->object.vm_object,
654 				OFF_TO_IDX(prev_entry->offset),
655 				(vm_size_t) (prev_entry->end
656 				    - prev_entry->start),
657 				(vm_size_t) (end - prev_entry->end))) {
658 				/*
659 				 * Coalesced the two objects - can extend the
660 				 * previous map entry to include the new
661 				 * range.
662 				 */
663 				map->size += (end - prev_entry->end);
664 				prev_entry->end = end;
665 				return (KERN_SUCCESS);
666 			}
667 		} /* else if ((object == prev_entry->object.vm_object) &&
668 			(prev_entry->offset + (prev_entry->end - prev_entry->start) == offset)) {
669 			map->size += (end - prev_entry->end);
670 			prev_entry->end = end;
671 			printf("map optim 1\n");
672 			return (KERN_SUCCESS);
673 		} */
674 	}
675 	/*
676 	 * Create a new entry
677 	 */
678 
679 	new_entry = vm_map_entry_create(map);
680 	new_entry->start = start;
681 	new_entry->end = end;
682 
683 	new_entry->is_a_map = FALSE;
684 	new_entry->is_sub_map = FALSE;
685 	new_entry->object.vm_object = object;
686 	new_entry->offset = offset;
687 
688 	if (cow & MAP_COPY_NEEDED)
689 		new_entry->needs_copy = TRUE;
690 	else
691 		new_entry->needs_copy = FALSE;
692 
693 	if (cow & MAP_COPY_ON_WRITE)
694 		new_entry->copy_on_write = TRUE;
695 	else
696 		new_entry->copy_on_write = FALSE;
697 
698 	if (map->is_main_map) {
699 		new_entry->inheritance = VM_INHERIT_DEFAULT;
700 		new_entry->protection = prot;
701 		new_entry->max_protection = max;
702 		new_entry->wired_count = 0;
703 	}
704 	/*
705 	 * Insert the new entry into the list
706 	 */
707 
708 	vm_map_entry_link(map, prev_entry, new_entry);
709 	map->size += new_entry->end - new_entry->start;
710 
711 	/*
712 	 * Update the free space hint
713 	 */
714 
715 	if ((map->first_free == prev_entry) &&
716 		(prev_entry->end >= new_entry->start))
717 		map->first_free = new_entry;
718 
719 	return (KERN_SUCCESS);
720 }
721 
722 /*
723  * Find sufficient space for `length' bytes in the given map, starting at
724  * `start'.  The map must be locked.  Returns 0 on success, 1 on no space.
725  */
726 int
727 vm_map_findspace(map, start, length, addr)
728 	register vm_map_t map;
729 	register vm_offset_t start;
730 	vm_size_t length;
731 	vm_offset_t *addr;
732 {
733 	register vm_map_entry_t entry, next;
734 	register vm_offset_t end;
735 
736 	if (start < map->min_offset)
737 		start = map->min_offset;
738 	if (start > map->max_offset)
739 		return (1);
740 
741 	/*
742 	 * Look for the first possible address; if there's already something
743 	 * at this address, we have to start after it.
744 	 */
745 	if (start == map->min_offset) {
746 		if ((entry = map->first_free) != &map->header)
747 			start = entry->end;
748 	} else {
749 		vm_map_entry_t tmp;
750 
751 		if (vm_map_lookup_entry(map, start, &tmp))
752 			start = tmp->end;
753 		entry = tmp;
754 	}
755 
756 	/*
757 	 * Look through the rest of the map, trying to fit a new region in the
758 	 * gap between existing regions, or after the very last region.
759 	 */
760 	for (;; start = (entry = next)->end) {
761 		/*
762 		 * Find the end of the proposed new region.  Be sure we didn't
763 		 * go beyond the end of the map, or wrap around the address;
764 		 * if so, we lose.  Otherwise, if this is the last entry, or
765 		 * if the proposed new region fits before the next entry, we
766 		 * win.
767 		 */
768 		end = start + length;
769 		if (end > map->max_offset || end < start)
770 			return (1);
771 		next = entry->next;
772 		if (next == &map->header || next->start >= end)
773 			break;
774 	}
775 	SAVE_HINT(map, entry);
776 	*addr = start;
777 	if (map == kernel_map && round_page(start + length) > kernel_vm_end)
778 		pmap_growkernel(round_page(start + length));
779 	return (0);
780 }
781 
782 /*
783  *	vm_map_find finds an unallocated region in the target address
784  *	map with the given length.  The search is defined to be
785  *	first-fit from the specified address; the region found is
786  *	returned in the same parameter.
787  *
788  */
789 int
790 vm_map_find(map, object, offset, addr, length, find_space, prot, max, cow)
791 	vm_map_t map;
792 	vm_object_t object;
793 	vm_ooffset_t offset;
794 	vm_offset_t *addr;	/* IN/OUT */
795 	vm_size_t length;
796 	boolean_t find_space;
797 	vm_prot_t prot, max;
798 	int cow;
799 {
800 	register vm_offset_t start;
801 	int result, s = 0;
802 
803 	start = *addr;
804 
805 	if (map == kmem_map)
806 		s = splhigh();
807 
808 	vm_map_lock(map);
809 	if (find_space) {
810 		if (vm_map_findspace(map, start, length, addr)) {
811 			vm_map_unlock(map);
812 			if (map == kmem_map)
813 				splx(s);
814 			return (KERN_NO_SPACE);
815 		}
816 		start = *addr;
817 	}
818 	result = vm_map_insert(map, object, offset,
819 		start, start + length, prot, max, cow);
820 	vm_map_unlock(map);
821 
822 	if (map == kmem_map)
823 		splx(s);
824 
825 	return (result);
826 }
827 
828 /*
829  *	vm_map_simplify_entry:	[ internal use only ]
830  *
831  *	Simplify the given map entry by:
832  *		removing extra sharing maps
833  *		[XXX maybe later] merging with a neighbor
834  */
835 static void
836 vm_map_simplify_entry(map, entry)
837 	vm_map_t map;
838 	vm_map_entry_t entry;
839 {
840 	vm_map_entry_t next, prev;
841 	vm_size_t nextsize, prevsize, esize;
842 
843 	/*
844 	 * If this entry corresponds to a sharing map, then see if we can
845 	 * remove the level of indirection. If it's not a sharing map, then it
846 	 * points to a VM object, so see if we can merge with either of our
847 	 * neighbors.
848 	 */
849 
850 	if (entry->is_sub_map || entry->is_a_map || entry->wired_count)
851 		return;
852 
853 	prev = entry->prev;
854 	if (prev != &map->header) {
855 		prevsize = prev->end - prev->start;
856 		if ( (prev->end == entry->start) &&
857 		     (prev->object.vm_object == entry->object.vm_object) &&
858 		     (prev->offset + prevsize == entry->offset) &&
859 		     (prev->needs_copy == entry->needs_copy) &&
860 		     (prev->copy_on_write == entry->copy_on_write) &&
861 		     (prev->protection == entry->protection) &&
862 		     (prev->max_protection == entry->max_protection) &&
863 		     (prev->inheritance == entry->inheritance) &&
864 		     (prev->is_a_map == FALSE) &&
865 		     (prev->is_sub_map == FALSE) &&
866 		     (prev->wired_count == 0)) {
867 			if (map->first_free == prev)
868 				map->first_free = entry;
869 			vm_map_entry_unlink(map, prev);
870 			entry->start = prev->start;
871 			entry->offset = prev->offset;
872 			vm_object_deallocate(prev->object.vm_object);
873 			vm_map_entry_dispose(map, prev);
874 		}
875 	}
876 
877 	next = entry->next;
878 	if (next != &map->header) {
879 		nextsize = next->end - next->start;
880 		esize = entry->end - entry->start;
881 		if ((entry->end == next->start) &&
882 		    (next->object.vm_object == entry->object.vm_object) &&
883 		    (entry->offset + esize == next->offset) &&
884 		    (next->needs_copy == entry->needs_copy) &&
885 		    (next->copy_on_write == entry->copy_on_write) &&
886 		    (next->protection == entry->protection) &&
887 		    (next->max_protection == entry->max_protection) &&
888 		    (next->inheritance == entry->inheritance) &&
889 		    (next->is_a_map == FALSE) &&
890 		    (next->is_sub_map == FALSE) &&
891 		    (next->wired_count == 0)) {
892 			if (map->first_free == next)
893 				map->first_free = entry;
894 			vm_map_entry_unlink(map, next);
895 			entry->end = next->end;
896 			vm_object_deallocate(next->object.vm_object);
897 			vm_map_entry_dispose(map, next);
898 	        }
899 	}
900 }
901 
902 /*
903  *	vm_map_clip_start:	[ internal use only ]
904  *
905  *	Asserts that the given entry begins at or after
906  *	the specified address; if necessary,
907  *	it splits the entry into two.
908  */
909 #define vm_map_clip_start(map, entry, startaddr) \
910 { \
911 	if (startaddr > entry->start) \
912 		_vm_map_clip_start(map, entry, startaddr); \
913 }
914 
915 /*
916  *	This routine is called only when it is known that
917  *	the entry must be split.
918  */
919 static void
920 _vm_map_clip_start(map, entry, start)
921 	register vm_map_t map;
922 	register vm_map_entry_t entry;
923 	register vm_offset_t start;
924 {
925 	register vm_map_entry_t new_entry;
926 
927 	/*
928 	 * Split off the front portion -- note that we must insert the new
929 	 * entry BEFORE this one, so that this entry has the specified
930 	 * starting address.
931 	 */
932 
933 	vm_map_simplify_entry(map, entry);
934 
935 	new_entry = vm_map_entry_create(map);
936 	*new_entry = *entry;
937 
938 	new_entry->end = start;
939 	entry->offset += (start - entry->start);
940 	entry->start = start;
941 
942 	vm_map_entry_link(map, entry->prev, new_entry);
943 
944 	if (entry->is_a_map || entry->is_sub_map)
945 		vm_map_reference(new_entry->object.share_map);
946 	else
947 		vm_object_reference(new_entry->object.vm_object);
948 }
949 
950 /*
951  *	vm_map_clip_end:	[ internal use only ]
952  *
953  *	Asserts that the given entry ends at or before
954  *	the specified address; if necessary,
955  *	it splits the entry into two.
956  */
957 
958 #define vm_map_clip_end(map, entry, endaddr) \
959 { \
960 	if (endaddr < entry->end) \
961 		_vm_map_clip_end(map, entry, endaddr); \
962 }
963 
964 /*
965  *	This routine is called only when it is known that
966  *	the entry must be split.
967  */
968 static void
969 _vm_map_clip_end(map, entry, end)
970 	register vm_map_t map;
971 	register vm_map_entry_t entry;
972 	register vm_offset_t end;
973 {
974 	register vm_map_entry_t new_entry;
975 
976 	/*
977 	 * Create a new entry and insert it AFTER the specified entry
978 	 */
979 
980 	new_entry = vm_map_entry_create(map);
981 	*new_entry = *entry;
982 
983 	new_entry->start = entry->end = end;
984 	new_entry->offset += (end - entry->start);
985 
986 	vm_map_entry_link(map, entry, new_entry);
987 
988 	if (entry->is_a_map || entry->is_sub_map)
989 		vm_map_reference(new_entry->object.share_map);
990 	else
991 		vm_object_reference(new_entry->object.vm_object);
992 }
993 
994 /*
995  *	VM_MAP_RANGE_CHECK:	[ internal use only ]
996  *
997  *	Asserts that the starting and ending region
998  *	addresses fall within the valid range of the map.
999  */
1000 #define	VM_MAP_RANGE_CHECK(map, start, end)		\
1001 		{					\
1002 		if (start < vm_map_min(map))		\
1003 			start = vm_map_min(map);	\
1004 		if (end > vm_map_max(map))		\
1005 			end = vm_map_max(map);		\
1006 		if (start > end)			\
1007 			start = end;			\
1008 		}
1009 
1010 /*
1011  *	vm_map_submap:		[ kernel use only ]
1012  *
1013  *	Mark the given range as handled by a subordinate map.
1014  *
1015  *	This range must have been created with vm_map_find,
1016  *	and no other operations may have been performed on this
1017  *	range prior to calling vm_map_submap.
1018  *
1019  *	Only a limited number of operations can be performed
1020  *	within this rage after calling vm_map_submap:
1021  *		vm_fault
1022  *	[Don't try vm_map_copy!]
1023  *
1024  *	To remove a submapping, one must first remove the
1025  *	range from the superior map, and then destroy the
1026  *	submap (if desired).  [Better yet, don't try it.]
1027  */
1028 int
1029 vm_map_submap(map, start, end, submap)
1030 	register vm_map_t map;
1031 	register vm_offset_t start;
1032 	register vm_offset_t end;
1033 	vm_map_t submap;
1034 {
1035 	vm_map_entry_t entry;
1036 	register int result = KERN_INVALID_ARGUMENT;
1037 
1038 	vm_map_lock(map);
1039 
1040 	VM_MAP_RANGE_CHECK(map, start, end);
1041 
1042 	if (vm_map_lookup_entry(map, start, &entry)) {
1043 		vm_map_clip_start(map, entry, start);
1044 	} else
1045 		entry = entry->next;
1046 
1047 	vm_map_clip_end(map, entry, end);
1048 
1049 	if ((entry->start == start) && (entry->end == end) &&
1050 	    (!entry->is_a_map) &&
1051 	    (entry->object.vm_object == NULL) &&
1052 	    (!entry->copy_on_write)) {
1053 		entry->is_a_map = FALSE;
1054 		entry->is_sub_map = TRUE;
1055 		vm_map_reference(entry->object.sub_map = submap);
1056 		result = KERN_SUCCESS;
1057 	}
1058 	vm_map_unlock(map);
1059 
1060 	return (result);
1061 }
1062 
1063 /*
1064  *	vm_map_protect:
1065  *
1066  *	Sets the protection of the specified address
1067  *	region in the target map.  If "set_max" is
1068  *	specified, the maximum protection is to be set;
1069  *	otherwise, only the current protection is affected.
1070  */
1071 int
1072 vm_map_protect(map, start, end, new_prot, set_max)
1073 	register vm_map_t map;
1074 	register vm_offset_t start;
1075 	register vm_offset_t end;
1076 	register vm_prot_t new_prot;
1077 	register boolean_t set_max;
1078 {
1079 	register vm_map_entry_t current;
1080 	vm_map_entry_t entry;
1081 
1082 	vm_map_lock(map);
1083 
1084 	VM_MAP_RANGE_CHECK(map, start, end);
1085 
1086 	if (vm_map_lookup_entry(map, start, &entry)) {
1087 		vm_map_clip_start(map, entry, start);
1088 	} else
1089 		entry = entry->next;
1090 
1091 	/*
1092 	 * Make a first pass to check for protection violations.
1093 	 */
1094 
1095 	current = entry;
1096 	while ((current != &map->header) && (current->start < end)) {
1097 		if (current->is_sub_map) {
1098 			vm_map_unlock(map);
1099 			return (KERN_INVALID_ARGUMENT);
1100 		}
1101 		if ((new_prot & current->max_protection) != new_prot) {
1102 			vm_map_unlock(map);
1103 			return (KERN_PROTECTION_FAILURE);
1104 		}
1105 		current = current->next;
1106 	}
1107 
1108 	/*
1109 	 * Go back and fix up protections. [Note that clipping is not
1110 	 * necessary the second time.]
1111 	 */
1112 
1113 	current = entry;
1114 
1115 	while ((current != &map->header) && (current->start < end)) {
1116 		vm_prot_t old_prot;
1117 
1118 		vm_map_clip_end(map, current, end);
1119 
1120 		old_prot = current->protection;
1121 		if (set_max)
1122 			current->protection =
1123 			    (current->max_protection = new_prot) &
1124 			    old_prot;
1125 		else
1126 			current->protection = new_prot;
1127 
1128 		/*
1129 		 * Update physical map if necessary. Worry about copy-on-write
1130 		 * here -- CHECK THIS XXX
1131 		 */
1132 
1133 		if (current->protection != old_prot) {
1134 
1135 #define MASK(entry)	((entry)->copy_on_write ? ~VM_PROT_WRITE : \
1136 							VM_PROT_ALL)
1137 #define	max(a,b)	((a) > (b) ? (a) : (b))
1138 
1139 			if (current->is_a_map) {
1140 				vm_map_entry_t share_entry;
1141 				vm_offset_t share_end;
1142 
1143 				vm_map_lock(current->object.share_map);
1144 				(void) vm_map_lookup_entry(
1145 				    current->object.share_map,
1146 				    current->offset,
1147 				    &share_entry);
1148 				share_end = current->offset +
1149 				    (current->end - current->start);
1150 				while ((share_entry !=
1151 					&current->object.share_map->header) &&
1152 				    (share_entry->start < share_end)) {
1153 
1154 					pmap_protect(map->pmap,
1155 					    (max(share_entry->start,
1156 						    current->offset) -
1157 						current->offset +
1158 						current->start),
1159 					    min(share_entry->end,
1160 						share_end) -
1161 					    current->offset +
1162 					    current->start,
1163 					    current->protection &
1164 					    MASK(share_entry));
1165 
1166 					share_entry = share_entry->next;
1167 				}
1168 				vm_map_unlock(current->object.share_map);
1169 			} else
1170 				pmap_protect(map->pmap, current->start,
1171 				    current->end,
1172 				    current->protection & MASK(entry));
1173 #undef	max
1174 #undef	MASK
1175 		}
1176 		current = current->next;
1177 	}
1178 
1179 	vm_map_simplify_entry(map, entry);
1180 	vm_map_unlock(map);
1181 	return (KERN_SUCCESS);
1182 }
1183 
1184 /*
1185  *	vm_map_inherit:
1186  *
1187  *	Sets the inheritance of the specified address
1188  *	range in the target map.  Inheritance
1189  *	affects how the map will be shared with
1190  *	child maps at the time of vm_map_fork.
1191  */
1192 int
1193 vm_map_inherit(map, start, end, new_inheritance)
1194 	register vm_map_t map;
1195 	register vm_offset_t start;
1196 	register vm_offset_t end;
1197 	register vm_inherit_t new_inheritance;
1198 {
1199 	register vm_map_entry_t entry;
1200 	vm_map_entry_t temp_entry;
1201 
1202 	switch (new_inheritance) {
1203 	case VM_INHERIT_NONE:
1204 	case VM_INHERIT_COPY:
1205 	case VM_INHERIT_SHARE:
1206 		break;
1207 	default:
1208 		return (KERN_INVALID_ARGUMENT);
1209 	}
1210 
1211 	vm_map_lock(map);
1212 
1213 	VM_MAP_RANGE_CHECK(map, start, end);
1214 
1215 	if (vm_map_lookup_entry(map, start, &temp_entry)) {
1216 		entry = temp_entry;
1217 		vm_map_clip_start(map, entry, start);
1218 	} else
1219 		entry = temp_entry->next;
1220 
1221 	while ((entry != &map->header) && (entry->start < end)) {
1222 		vm_map_clip_end(map, entry, end);
1223 
1224 		entry->inheritance = new_inheritance;
1225 
1226 		entry = entry->next;
1227 	}
1228 
1229 	vm_map_simplify_entry(map, temp_entry);
1230 	vm_map_unlock(map);
1231 	return (KERN_SUCCESS);
1232 }
1233 
1234 /*
1235  *	vm_map_pageable:
1236  *
1237  *	Sets the pageability of the specified address
1238  *	range in the target map.  Regions specified
1239  *	as not pageable require locked-down physical
1240  *	memory and physical page maps.
1241  *
1242  *	The map must not be locked, but a reference
1243  *	must remain to the map throughout the call.
1244  */
1245 int
1246 vm_map_pageable(map, start, end, new_pageable)
1247 	register vm_map_t map;
1248 	register vm_offset_t start;
1249 	register vm_offset_t end;
1250 	register boolean_t new_pageable;
1251 {
1252 	register vm_map_entry_t entry;
1253 	vm_map_entry_t start_entry;
1254 	register vm_offset_t failed = 0;
1255 	int rv;
1256 
1257 	vm_map_lock(map);
1258 
1259 	VM_MAP_RANGE_CHECK(map, start, end);
1260 
1261 	/*
1262 	 * Only one pageability change may take place at one time, since
1263 	 * vm_fault assumes it will be called only once for each
1264 	 * wiring/unwiring.  Therefore, we have to make sure we're actually
1265 	 * changing the pageability for the entire region.  We do so before
1266 	 * making any changes.
1267 	 */
1268 
1269 	if (vm_map_lookup_entry(map, start, &start_entry) == FALSE) {
1270 		vm_map_unlock(map);
1271 		return (KERN_INVALID_ADDRESS);
1272 	}
1273 	entry = start_entry;
1274 
1275 	/*
1276 	 * Actions are rather different for wiring and unwiring, so we have
1277 	 * two separate cases.
1278 	 */
1279 
1280 	if (new_pageable) {
1281 
1282 		vm_map_clip_start(map, entry, start);
1283 
1284 		/*
1285 		 * Unwiring.  First ensure that the range to be unwired is
1286 		 * really wired down and that there are no holes.
1287 		 */
1288 		while ((entry != &map->header) && (entry->start < end)) {
1289 
1290 			if (entry->wired_count == 0 ||
1291 			    (entry->end < end &&
1292 				(entry->next == &map->header ||
1293 				    entry->next->start > entry->end))) {
1294 				vm_map_unlock(map);
1295 				return (KERN_INVALID_ARGUMENT);
1296 			}
1297 			entry = entry->next;
1298 		}
1299 
1300 		/*
1301 		 * Now decrement the wiring count for each region. If a region
1302 		 * becomes completely unwired, unwire its physical pages and
1303 		 * mappings.
1304 		 */
1305 		lock_set_recursive(&map->lock);
1306 
1307 		entry = start_entry;
1308 		while ((entry != &map->header) && (entry->start < end)) {
1309 			vm_map_clip_end(map, entry, end);
1310 
1311 			entry->wired_count--;
1312 			if (entry->wired_count == 0)
1313 				vm_fault_unwire(map, entry->start, entry->end);
1314 
1315 			entry = entry->next;
1316 		}
1317 		vm_map_simplify_entry(map, start_entry);
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 		SAVE_HINT(map, prev_entry);
2282 		vm_map_entry_unlink(map, this_entry);
2283 		prev_entry->end = this_entry->end;
2284 		vm_object_deallocate(this_entry->object.vm_object);
2285 		vm_map_entry_dispose(map, this_entry);
2286 	}
2287 	vm_map_unlock(map);
2288 }
2289 
2290 #ifdef DDB
2291 /*
2292  *	vm_map_print:	[ debug ]
2293  */
2294 void
2295 vm_map_print(imap, full, dummy3, dummy4)
2296 	/* db_expr_t */ int imap;
2297 	boolean_t full;
2298 	/* db_expr_t */ int dummy3;
2299 	char *dummy4;
2300 {
2301 	register vm_map_entry_t entry;
2302 	register vm_map_t map = (vm_map_t)imap;	/* XXX */
2303 
2304 	iprintf("%s map 0x%x: pmap=0x%x,ref=%d,nentries=%d,version=%d\n",
2305 	    (map->is_main_map ? "Task" : "Share"),
2306 	    (int) map, (int) (map->pmap), map->ref_count, map->nentries,
2307 	    map->timestamp);
2308 
2309 	if (!full && indent)
2310 		return;
2311 
2312 	indent += 2;
2313 	for (entry = map->header.next; entry != &map->header;
2314 	    entry = entry->next) {
2315 		iprintf("map entry 0x%x: start=0x%x, end=0x%x, ",
2316 		    (int) entry, (int) entry->start, (int) entry->end);
2317 		if (map->is_main_map) {
2318 			static char *inheritance_name[4] =
2319 			{"share", "copy", "none", "donate_copy"};
2320 
2321 			printf("prot=%x/%x/%s, ",
2322 			    entry->protection,
2323 			    entry->max_protection,
2324 			    inheritance_name[entry->inheritance]);
2325 			if (entry->wired_count != 0)
2326 				printf("wired, ");
2327 		}
2328 		if (entry->is_a_map || entry->is_sub_map) {
2329 			printf("share=0x%x, offset=0x%x\n",
2330 			    (int) entry->object.share_map,
2331 			    (int) entry->offset);
2332 			if ((entry->prev == &map->header) ||
2333 			    (!entry->prev->is_a_map) ||
2334 			    (entry->prev->object.share_map !=
2335 				entry->object.share_map)) {
2336 				indent += 2;
2337 				vm_map_print((int)entry->object.share_map,
2338 					     full, 0, (char *)0);
2339 				indent -= 2;
2340 			}
2341 		} else {
2342 			printf("object=0x%x, offset=0x%x",
2343 			    (int) entry->object.vm_object,
2344 			    (int) entry->offset);
2345 			if (entry->copy_on_write)
2346 				printf(", copy (%s)",
2347 				    entry->needs_copy ? "needed" : "done");
2348 			printf("\n");
2349 
2350 			if ((entry->prev == &map->header) ||
2351 			    (entry->prev->is_a_map) ||
2352 			    (entry->prev->object.vm_object !=
2353 				entry->object.vm_object)) {
2354 				indent += 2;
2355 				vm_object_print((int)entry->object.vm_object,
2356 						full, 0, (char *)0);
2357 				indent -= 2;
2358 			}
2359 		}
2360 	}
2361 	indent -= 2;
2362 }
2363 #endif
2364