xref: /freebsd/sys/vm/vm_map.c (revision 87569f75a91f298c52a71823c04d41cf53c88889)
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  * 4. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	from: @(#)vm_map.c	8.3 (Berkeley) 1/12/94
33  *
34  *
35  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
36  * All rights reserved.
37  *
38  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
39  *
40  * Permission to use, copy, modify and distribute this software and
41  * its documentation is hereby granted, provided that both the copyright
42  * notice and this permission notice appear in all copies of the
43  * software, derivative works or modified versions, and any portions
44  * thereof, and that both notices appear in supporting documentation.
45  *
46  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
47  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
48  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
49  *
50  * Carnegie Mellon requests users of this software to return to
51  *
52  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
53  *  School of Computer Science
54  *  Carnegie Mellon University
55  *  Pittsburgh PA 15213-3890
56  *
57  * any improvements or extensions that they make and grant Carnegie the
58  * rights to redistribute these changes.
59  */
60 
61 /*
62  *	Virtual memory mapping module.
63  */
64 
65 #include <sys/cdefs.h>
66 __FBSDID("$FreeBSD$");
67 
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/ktr.h>
71 #include <sys/lock.h>
72 #include <sys/mutex.h>
73 #include <sys/proc.h>
74 #include <sys/vmmeter.h>
75 #include <sys/mman.h>
76 #include <sys/vnode.h>
77 #include <sys/resourcevar.h>
78 #include <sys/file.h>
79 #include <sys/sysent.h>
80 #include <sys/shm.h>
81 
82 #include <vm/vm.h>
83 #include <vm/vm_param.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_pager.h>
89 #include <vm/vm_kern.h>
90 #include <vm/vm_extern.h>
91 #include <vm/swap_pager.h>
92 #include <vm/uma.h>
93 
94 /*
95  *	Virtual memory maps provide for the mapping, protection,
96  *	and sharing of virtual memory objects.  In addition,
97  *	this module provides for an efficient virtual copy of
98  *	memory from one map to another.
99  *
100  *	Synchronization is required prior to most operations.
101  *
102  *	Maps consist of an ordered doubly-linked list of simple
103  *	entries; a single hint is used to speed up lookups.
104  *
105  *	Since portions of maps are specified by start/end addresses,
106  *	which may not align with existing map entries, all
107  *	routines merely "clip" entries to these start/end values.
108  *	[That is, an entry is split into two, bordering at a
109  *	start or end value.]  Note that these clippings may not
110  *	always be necessary (as the two resulting entries are then
111  *	not changed); however, the clipping is done for convenience.
112  *
113  *	As mentioned above, virtual copy operations are performed
114  *	by copying VM object references from one map to
115  *	another, and then marking both regions as copy-on-write.
116  */
117 
118 /*
119  *	vm_map_startup:
120  *
121  *	Initialize the vm_map module.  Must be called before
122  *	any other vm_map routines.
123  *
124  *	Map and entry structures are allocated from the general
125  *	purpose memory pool with some exceptions:
126  *
127  *	- The kernel map and kmem submap are allocated statically.
128  *	- Kernel map entries are allocated out of a static pool.
129  *
130  *	These restrictions are necessary since malloc() uses the
131  *	maps and requires map entries.
132  */
133 
134 static struct mtx map_sleep_mtx;
135 static uma_zone_t mapentzone;
136 static uma_zone_t kmapentzone;
137 static uma_zone_t mapzone;
138 static uma_zone_t vmspace_zone;
139 static struct vm_object kmapentobj;
140 static int vmspace_zinit(void *mem, int size, int flags);
141 static void vmspace_zfini(void *mem, int size);
142 static int vm_map_zinit(void *mem, int ize, int flags);
143 static void vm_map_zfini(void *mem, int size);
144 static void _vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max);
145 
146 #ifdef INVARIANTS
147 static void vm_map_zdtor(void *mem, int size, void *arg);
148 static void vmspace_zdtor(void *mem, int size, void *arg);
149 #endif
150 
151 void
152 vm_map_startup(void)
153 {
154 	mtx_init(&map_sleep_mtx, "vm map sleep mutex", NULL, MTX_DEF);
155 	mapzone = uma_zcreate("MAP", sizeof(struct vm_map), NULL,
156 #ifdef INVARIANTS
157 	    vm_map_zdtor,
158 #else
159 	    NULL,
160 #endif
161 	    vm_map_zinit, vm_map_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
162 	uma_prealloc(mapzone, MAX_KMAP);
163 	kmapentzone = uma_zcreate("KMAP ENTRY", sizeof(struct vm_map_entry),
164 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
165 	    UMA_ZONE_MTXCLASS | UMA_ZONE_VM);
166 	uma_prealloc(kmapentzone, MAX_KMAPENT);
167 	mapentzone = uma_zcreate("MAP ENTRY", sizeof(struct vm_map_entry),
168 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
169 }
170 
171 static void
172 vmspace_zfini(void *mem, int size)
173 {
174 	struct vmspace *vm;
175 
176 	vm = (struct vmspace *)mem;
177 	pmap_release(vmspace_pmap(vm));
178 	vm_map_zfini(&vm->vm_map, sizeof(vm->vm_map));
179 }
180 
181 static int
182 vmspace_zinit(void *mem, int size, int flags)
183 {
184 	struct vmspace *vm;
185 
186 	vm = (struct vmspace *)mem;
187 
188 	(void)vm_map_zinit(&vm->vm_map, sizeof(vm->vm_map), flags);
189 	pmap_pinit(vmspace_pmap(vm));
190 	return (0);
191 }
192 
193 static void
194 vm_map_zfini(void *mem, int size)
195 {
196 	vm_map_t map;
197 
198 	map = (vm_map_t)mem;
199 	mtx_destroy(&map->system_mtx);
200 	sx_destroy(&map->lock);
201 }
202 
203 static int
204 vm_map_zinit(void *mem, int size, int flags)
205 {
206 	vm_map_t map;
207 
208 	map = (vm_map_t)mem;
209 	map->nentries = 0;
210 	map->size = 0;
211 	mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
212 	sx_init(&map->lock, "user map");
213 	return (0);
214 }
215 
216 #ifdef INVARIANTS
217 static void
218 vmspace_zdtor(void *mem, int size, void *arg)
219 {
220 	struct vmspace *vm;
221 
222 	vm = (struct vmspace *)mem;
223 
224 	vm_map_zdtor(&vm->vm_map, sizeof(vm->vm_map), arg);
225 }
226 static void
227 vm_map_zdtor(void *mem, int size, void *arg)
228 {
229 	vm_map_t map;
230 
231 	map = (vm_map_t)mem;
232 	KASSERT(map->nentries == 0,
233 	    ("map %p nentries == %d on free.",
234 	    map, map->nentries));
235 	KASSERT(map->size == 0,
236 	    ("map %p size == %lu on free.",
237 	    map, (unsigned long)map->size));
238 }
239 #endif	/* INVARIANTS */
240 
241 /*
242  * Allocate a vmspace structure, including a vm_map and pmap,
243  * and initialize those structures.  The refcnt is set to 1.
244  */
245 struct vmspace *
246 vmspace_alloc(min, max)
247 	vm_offset_t min, max;
248 {
249 	struct vmspace *vm;
250 
251 	vm = uma_zalloc(vmspace_zone, M_WAITOK);
252 	CTR1(KTR_VM, "vmspace_alloc: %p", vm);
253 	_vm_map_init(&vm->vm_map, min, max);
254 	vm->vm_map.pmap = vmspace_pmap(vm);		/* XXX */
255 	vm->vm_refcnt = 1;
256 	vm->vm_shm = NULL;
257 	vm->vm_swrss = 0;
258 	vm->vm_tsize = 0;
259 	vm->vm_dsize = 0;
260 	vm->vm_ssize = 0;
261 	vm->vm_taddr = 0;
262 	vm->vm_daddr = 0;
263 	vm->vm_maxsaddr = 0;
264 	vm->vm_exitingcnt = 0;
265 	return (vm);
266 }
267 
268 void
269 vm_init2(void)
270 {
271 	uma_zone_set_obj(kmapentzone, &kmapentobj, lmin(cnt.v_page_count,
272 	    (VM_MAX_KERNEL_ADDRESS - KERNBASE) / PAGE_SIZE) / 8 +
273 	     maxproc * 2 + maxfiles);
274 	vmspace_zone = uma_zcreate("VMSPACE", sizeof(struct vmspace), NULL,
275 #ifdef INVARIANTS
276 	    vmspace_zdtor,
277 #else
278 	    NULL,
279 #endif
280 	    vmspace_zinit, vmspace_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
281 }
282 
283 static inline void
284 vmspace_dofree(struct vmspace *vm)
285 {
286 	CTR1(KTR_VM, "vmspace_free: %p", vm);
287 
288 	/*
289 	 * Make sure any SysV shm is freed, it might not have been in
290 	 * exit1().
291 	 */
292 	shmexit(vm);
293 
294 	/*
295 	 * Lock the map, to wait out all other references to it.
296 	 * Delete all of the mappings and pages they hold, then call
297 	 * the pmap module to reclaim anything left.
298 	 */
299 	(void)vm_map_remove(&vm->vm_map, vm->vm_map.min_offset,
300 	    vm->vm_map.max_offset);
301 
302 	uma_zfree(vmspace_zone, vm);
303 }
304 
305 void
306 vmspace_free(struct vmspace *vm)
307 {
308 	int refcnt;
309 
310 	if (vm->vm_refcnt == 0)
311 		panic("vmspace_free: attempt to free already freed vmspace");
312 
313 	do
314 		refcnt = vm->vm_refcnt;
315 	while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1));
316 	if (refcnt == 1 && vm->vm_exitingcnt == 0)
317 		vmspace_dofree(vm);
318 }
319 
320 void
321 vmspace_exitfree(struct proc *p)
322 {
323 	struct vmspace *vm;
324 	int exitingcnt;
325 
326 	vm = p->p_vmspace;
327 	p->p_vmspace = NULL;
328 
329 	/*
330 	 * cleanup by parent process wait()ing on exiting child.  vm_refcnt
331 	 * may not be 0 (e.g. fork() and child exits without exec()ing).
332 	 * exitingcnt may increment above 0 and drop back down to zero
333 	 * several times while vm_refcnt is held non-zero.  vm_refcnt
334 	 * may also increment above 0 and drop back down to zero several
335 	 * times while vm_exitingcnt is held non-zero.
336 	 *
337 	 * The last wait on the exiting child's vmspace will clean up
338 	 * the remainder of the vmspace.
339 	 */
340 	do
341 		exitingcnt = vm->vm_exitingcnt;
342 	while (!atomic_cmpset_int(&vm->vm_exitingcnt, exitingcnt,
343 	    exitingcnt - 1));
344 	if (vm->vm_refcnt == 0 && exitingcnt == 1)
345 		vmspace_dofree(vm);
346 }
347 
348 void
349 _vm_map_lock(vm_map_t map, const char *file, int line)
350 {
351 
352 	if (map->system_map)
353 		_mtx_lock_flags(&map->system_mtx, 0, file, line);
354 	else
355 		_sx_xlock(&map->lock, file, line);
356 	map->timestamp++;
357 }
358 
359 void
360 _vm_map_unlock(vm_map_t map, const char *file, int line)
361 {
362 
363 	if (map->system_map)
364 		_mtx_unlock_flags(&map->system_mtx, 0, file, line);
365 	else
366 		_sx_xunlock(&map->lock, file, line);
367 }
368 
369 void
370 _vm_map_lock_read(vm_map_t map, const char *file, int line)
371 {
372 
373 	if (map->system_map)
374 		_mtx_lock_flags(&map->system_mtx, 0, file, line);
375 	else
376 		_sx_xlock(&map->lock, file, line);
377 }
378 
379 void
380 _vm_map_unlock_read(vm_map_t map, const char *file, int line)
381 {
382 
383 	if (map->system_map)
384 		_mtx_unlock_flags(&map->system_mtx, 0, file, line);
385 	else
386 		_sx_xunlock(&map->lock, file, line);
387 }
388 
389 int
390 _vm_map_trylock(vm_map_t map, const char *file, int line)
391 {
392 	int error;
393 
394 	error = map->system_map ?
395 	    !_mtx_trylock(&map->system_mtx, 0, file, line) :
396 	    !_sx_try_xlock(&map->lock, file, line);
397 	if (error == 0)
398 		map->timestamp++;
399 	return (error == 0);
400 }
401 
402 int
403 _vm_map_trylock_read(vm_map_t map, const char *file, int line)
404 {
405 	int error;
406 
407 	error = map->system_map ?
408 	    !_mtx_trylock(&map->system_mtx, 0, file, line) :
409 	    !_sx_try_xlock(&map->lock, file, line);
410 	return (error == 0);
411 }
412 
413 int
414 _vm_map_lock_upgrade(vm_map_t map, const char *file, int line)
415 {
416 
417 #ifdef INVARIANTS
418 	if (map->system_map) {
419 		_mtx_assert(&map->system_mtx, MA_OWNED, file, line);
420 	} else
421 		_sx_assert(&map->lock, SX_XLOCKED, file, line);
422 #endif
423 	map->timestamp++;
424 	return (0);
425 }
426 
427 void
428 _vm_map_lock_downgrade(vm_map_t map, const char *file, int line)
429 {
430 
431 #ifdef INVARIANTS
432 	if (map->system_map) {
433 		_mtx_assert(&map->system_mtx, MA_OWNED, file, line);
434 	} else
435 		_sx_assert(&map->lock, SX_XLOCKED, file, line);
436 #endif
437 }
438 
439 /*
440  *	vm_map_unlock_and_wait:
441  */
442 int
443 vm_map_unlock_and_wait(vm_map_t map, boolean_t user_wait)
444 {
445 
446 	mtx_lock(&map_sleep_mtx);
447 	vm_map_unlock(map);
448 	return (msleep(&map->root, &map_sleep_mtx, PDROP | PVM, "vmmaps", 0));
449 }
450 
451 /*
452  *	vm_map_wakeup:
453  */
454 void
455 vm_map_wakeup(vm_map_t map)
456 {
457 
458 	/*
459 	 * Acquire and release map_sleep_mtx to prevent a wakeup()
460 	 * from being performed (and lost) between the vm_map_unlock()
461 	 * and the msleep() in vm_map_unlock_and_wait().
462 	 */
463 	mtx_lock(&map_sleep_mtx);
464 	mtx_unlock(&map_sleep_mtx);
465 	wakeup(&map->root);
466 }
467 
468 long
469 vmspace_resident_count(struct vmspace *vmspace)
470 {
471 	return pmap_resident_count(vmspace_pmap(vmspace));
472 }
473 
474 long
475 vmspace_wired_count(struct vmspace *vmspace)
476 {
477 	return pmap_wired_count(vmspace_pmap(vmspace));
478 }
479 
480 /*
481  *	vm_map_create:
482  *
483  *	Creates and returns a new empty VM map with
484  *	the given physical map structure, and having
485  *	the given lower and upper address bounds.
486  */
487 vm_map_t
488 vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max)
489 {
490 	vm_map_t result;
491 
492 	result = uma_zalloc(mapzone, M_WAITOK);
493 	CTR1(KTR_VM, "vm_map_create: %p", result);
494 	_vm_map_init(result, min, max);
495 	result->pmap = pmap;
496 	return (result);
497 }
498 
499 /*
500  * Initialize an existing vm_map structure
501  * such as that in the vmspace structure.
502  * The pmap is set elsewhere.
503  */
504 static void
505 _vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max)
506 {
507 
508 	map->header.next = map->header.prev = &map->header;
509 	map->needs_wakeup = FALSE;
510 	map->system_map = 0;
511 	map->min_offset = min;
512 	map->max_offset = max;
513 	map->flags = 0;
514 	map->root = NULL;
515 	map->timestamp = 0;
516 }
517 
518 void
519 vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max)
520 {
521 	_vm_map_init(map, min, max);
522 	mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
523 	sx_init(&map->lock, "user map");
524 }
525 
526 /*
527  *	vm_map_entry_dispose:	[ internal use only ]
528  *
529  *	Inverse of vm_map_entry_create.
530  */
531 static void
532 vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry)
533 {
534 	uma_zfree(map->system_map ? kmapentzone : mapentzone, entry);
535 }
536 
537 /*
538  *	vm_map_entry_create:	[ internal use only ]
539  *
540  *	Allocates a VM map entry for insertion.
541  *	No entry fields are filled in.
542  */
543 static vm_map_entry_t
544 vm_map_entry_create(vm_map_t map)
545 {
546 	vm_map_entry_t new_entry;
547 
548 	if (map->system_map)
549 		new_entry = uma_zalloc(kmapentzone, M_NOWAIT);
550 	else
551 		new_entry = uma_zalloc(mapentzone, M_WAITOK);
552 	if (new_entry == NULL)
553 		panic("vm_map_entry_create: kernel resources exhausted");
554 	return (new_entry);
555 }
556 
557 /*
558  *	vm_map_entry_set_behavior:
559  *
560  *	Set the expected access behavior, either normal, random, or
561  *	sequential.
562  */
563 static inline void
564 vm_map_entry_set_behavior(vm_map_entry_t entry, u_char behavior)
565 {
566 	entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) |
567 	    (behavior & MAP_ENTRY_BEHAV_MASK);
568 }
569 
570 /*
571  *	vm_map_entry_set_max_free:
572  *
573  *	Set the max_free field in a vm_map_entry.
574  */
575 static inline void
576 vm_map_entry_set_max_free(vm_map_entry_t entry)
577 {
578 
579 	entry->max_free = entry->adj_free;
580 	if (entry->left != NULL && entry->left->max_free > entry->max_free)
581 		entry->max_free = entry->left->max_free;
582 	if (entry->right != NULL && entry->right->max_free > entry->max_free)
583 		entry->max_free = entry->right->max_free;
584 }
585 
586 /*
587  *	vm_map_entry_splay:
588  *
589  *	The Sleator and Tarjan top-down splay algorithm with the
590  *	following variation.  Max_free must be computed bottom-up, so
591  *	on the downward pass, maintain the left and right spines in
592  *	reverse order.  Then, make a second pass up each side to fix
593  *	the pointers and compute max_free.  The time bound is O(log n)
594  *	amortized.
595  *
596  *	The new root is the vm_map_entry containing "addr", or else an
597  *	adjacent entry (lower or higher) if addr is not in the tree.
598  *
599  *	The map must be locked, and leaves it so.
600  *
601  *	Returns: the new root.
602  */
603 static vm_map_entry_t
604 vm_map_entry_splay(vm_offset_t addr, vm_map_entry_t root)
605 {
606 	vm_map_entry_t llist, rlist;
607 	vm_map_entry_t ltree, rtree;
608 	vm_map_entry_t y;
609 
610 	/* Special case of empty tree. */
611 	if (root == NULL)
612 		return (root);
613 
614 	/*
615 	 * Pass One: Splay down the tree until we find addr or a NULL
616 	 * pointer where addr would go.  llist and rlist are the two
617 	 * sides in reverse order (bottom-up), with llist linked by
618 	 * the right pointer and rlist linked by the left pointer in
619 	 * the vm_map_entry.  Wait until Pass Two to set max_free on
620 	 * the two spines.
621 	 */
622 	llist = NULL;
623 	rlist = NULL;
624 	for (;;) {
625 		/* root is never NULL in here. */
626 		if (addr < root->start) {
627 			y = root->left;
628 			if (y == NULL)
629 				break;
630 			if (addr < y->start && y->left != NULL) {
631 				/* Rotate right and put y on rlist. */
632 				root->left = y->right;
633 				y->right = root;
634 				vm_map_entry_set_max_free(root);
635 				root = y->left;
636 				y->left = rlist;
637 				rlist = y;
638 			} else {
639 				/* Put root on rlist. */
640 				root->left = rlist;
641 				rlist = root;
642 				root = y;
643 			}
644 		} else {
645 			y = root->right;
646 			if (addr < root->end || y == NULL)
647 				break;
648 			if (addr >= y->end && y->right != NULL) {
649 				/* Rotate left and put y on llist. */
650 				root->right = y->left;
651 				y->left = root;
652 				vm_map_entry_set_max_free(root);
653 				root = y->right;
654 				y->right = llist;
655 				llist = y;
656 			} else {
657 				/* Put root on llist. */
658 				root->right = llist;
659 				llist = root;
660 				root = y;
661 			}
662 		}
663 	}
664 
665 	/*
666 	 * Pass Two: Walk back up the two spines, flip the pointers
667 	 * and set max_free.  The subtrees of the root go at the
668 	 * bottom of llist and rlist.
669 	 */
670 	ltree = root->left;
671 	while (llist != NULL) {
672 		y = llist->right;
673 		llist->right = ltree;
674 		vm_map_entry_set_max_free(llist);
675 		ltree = llist;
676 		llist = y;
677 	}
678 	rtree = root->right;
679 	while (rlist != NULL) {
680 		y = rlist->left;
681 		rlist->left = rtree;
682 		vm_map_entry_set_max_free(rlist);
683 		rtree = rlist;
684 		rlist = y;
685 	}
686 
687 	/*
688 	 * Final assembly: add ltree and rtree as subtrees of root.
689 	 */
690 	root->left = ltree;
691 	root->right = rtree;
692 	vm_map_entry_set_max_free(root);
693 
694 	return (root);
695 }
696 
697 /*
698  *	vm_map_entry_{un,}link:
699  *
700  *	Insert/remove entries from maps.
701  */
702 static void
703 vm_map_entry_link(vm_map_t map,
704 		  vm_map_entry_t after_where,
705 		  vm_map_entry_t entry)
706 {
707 
708 	CTR4(KTR_VM,
709 	    "vm_map_entry_link: map %p, nentries %d, entry %p, after %p", map,
710 	    map->nentries, entry, after_where);
711 	map->nentries++;
712 	entry->prev = after_where;
713 	entry->next = after_where->next;
714 	entry->next->prev = entry;
715 	after_where->next = entry;
716 
717 	if (after_where != &map->header) {
718 		if (after_where != map->root)
719 			vm_map_entry_splay(after_where->start, map->root);
720 		entry->right = after_where->right;
721 		entry->left = after_where;
722 		after_where->right = NULL;
723 		after_where->adj_free = entry->start - after_where->end;
724 		vm_map_entry_set_max_free(after_where);
725 	} else {
726 		entry->right = map->root;
727 		entry->left = NULL;
728 	}
729 	entry->adj_free = (entry->next == &map->header ? map->max_offset :
730 	    entry->next->start) - entry->end;
731 	vm_map_entry_set_max_free(entry);
732 	map->root = entry;
733 }
734 
735 static void
736 vm_map_entry_unlink(vm_map_t map,
737 		    vm_map_entry_t entry)
738 {
739 	vm_map_entry_t next, prev, root;
740 
741 	if (entry != map->root)
742 		vm_map_entry_splay(entry->start, map->root);
743 	if (entry->left == NULL)
744 		root = entry->right;
745 	else {
746 		root = vm_map_entry_splay(entry->start, entry->left);
747 		root->right = entry->right;
748 		root->adj_free = (entry->next == &map->header ? map->max_offset :
749 		    entry->next->start) - root->end;
750 		vm_map_entry_set_max_free(root);
751 	}
752 	map->root = root;
753 
754 	prev = entry->prev;
755 	next = entry->next;
756 	next->prev = prev;
757 	prev->next = next;
758 	map->nentries--;
759 	CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map,
760 	    map->nentries, entry);
761 }
762 
763 /*
764  *	vm_map_entry_resize_free:
765  *
766  *	Recompute the amount of free space following a vm_map_entry
767  *	and propagate that value up the tree.  Call this function after
768  *	resizing a map entry in-place, that is, without a call to
769  *	vm_map_entry_link() or _unlink().
770  *
771  *	The map must be locked, and leaves it so.
772  */
773 static void
774 vm_map_entry_resize_free(vm_map_t map, vm_map_entry_t entry)
775 {
776 
777 	/*
778 	 * Using splay trees without parent pointers, propagating
779 	 * max_free up the tree is done by moving the entry to the
780 	 * root and making the change there.
781 	 */
782 	if (entry != map->root)
783 		map->root = vm_map_entry_splay(entry->start, map->root);
784 
785 	entry->adj_free = (entry->next == &map->header ? map->max_offset :
786 	    entry->next->start) - entry->end;
787 	vm_map_entry_set_max_free(entry);
788 }
789 
790 /*
791  *	vm_map_lookup_entry:	[ internal use only ]
792  *
793  *	Finds the map entry containing (or
794  *	immediately preceding) the specified address
795  *	in the given map; the entry is returned
796  *	in the "entry" parameter.  The boolean
797  *	result indicates whether the address is
798  *	actually contained in the map.
799  */
800 boolean_t
801 vm_map_lookup_entry(
802 	vm_map_t map,
803 	vm_offset_t address,
804 	vm_map_entry_t *entry)	/* OUT */
805 {
806 	vm_map_entry_t cur;
807 
808 	cur = vm_map_entry_splay(address, map->root);
809 	if (cur == NULL)
810 		*entry = &map->header;
811 	else {
812 		map->root = cur;
813 
814 		if (address >= cur->start) {
815 			*entry = cur;
816 			if (cur->end > address)
817 				return (TRUE);
818 		} else
819 			*entry = cur->prev;
820 	}
821 	return (FALSE);
822 }
823 
824 /*
825  *	vm_map_insert:
826  *
827  *	Inserts the given whole VM object into the target
828  *	map at the specified address range.  The object's
829  *	size should match that of the address range.
830  *
831  *	Requires that the map be locked, and leaves it so.
832  *
833  *	If object is non-NULL, ref count must be bumped by caller
834  *	prior to making call to account for the new entry.
835  */
836 int
837 vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
838 	      vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max,
839 	      int cow)
840 {
841 	vm_map_entry_t new_entry;
842 	vm_map_entry_t prev_entry;
843 	vm_map_entry_t temp_entry;
844 	vm_eflags_t protoeflags;
845 
846 	/*
847 	 * Check that the start and end points are not bogus.
848 	 */
849 	if ((start < map->min_offset) || (end > map->max_offset) ||
850 	    (start >= end))
851 		return (KERN_INVALID_ADDRESS);
852 
853 	/*
854 	 * Find the entry prior to the proposed starting address; if it's part
855 	 * of an existing entry, this range is bogus.
856 	 */
857 	if (vm_map_lookup_entry(map, start, &temp_entry))
858 		return (KERN_NO_SPACE);
859 
860 	prev_entry = temp_entry;
861 
862 	/*
863 	 * Assert that the next entry doesn't overlap the end point.
864 	 */
865 	if ((prev_entry->next != &map->header) &&
866 	    (prev_entry->next->start < end))
867 		return (KERN_NO_SPACE);
868 
869 	protoeflags = 0;
870 
871 	if (cow & MAP_COPY_ON_WRITE)
872 		protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
873 
874 	if (cow & MAP_NOFAULT) {
875 		protoeflags |= MAP_ENTRY_NOFAULT;
876 
877 		KASSERT(object == NULL,
878 			("vm_map_insert: paradoxical MAP_NOFAULT request"));
879 	}
880 	if (cow & MAP_DISABLE_SYNCER)
881 		protoeflags |= MAP_ENTRY_NOSYNC;
882 	if (cow & MAP_DISABLE_COREDUMP)
883 		protoeflags |= MAP_ENTRY_NOCOREDUMP;
884 
885 	if (object != NULL) {
886 		/*
887 		 * OBJ_ONEMAPPING must be cleared unless this mapping
888 		 * is trivially proven to be the only mapping for any
889 		 * of the object's pages.  (Object granularity
890 		 * reference counting is insufficient to recognize
891 		 * aliases with precision.)
892 		 */
893 		VM_OBJECT_LOCK(object);
894 		if (object->ref_count > 1 || object->shadow_count != 0)
895 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
896 		VM_OBJECT_UNLOCK(object);
897 	}
898 	else if ((prev_entry != &map->header) &&
899 		 (prev_entry->eflags == protoeflags) &&
900 		 (prev_entry->end == start) &&
901 		 (prev_entry->wired_count == 0) &&
902 		 ((prev_entry->object.vm_object == NULL) ||
903 		  vm_object_coalesce(prev_entry->object.vm_object,
904 				     prev_entry->offset,
905 				     (vm_size_t)(prev_entry->end - prev_entry->start),
906 				     (vm_size_t)(end - prev_entry->end)))) {
907 		/*
908 		 * We were able to extend the object.  Determine if we
909 		 * can extend the previous map entry to include the
910 		 * new range as well.
911 		 */
912 		if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
913 		    (prev_entry->protection == prot) &&
914 		    (prev_entry->max_protection == max)) {
915 			map->size += (end - prev_entry->end);
916 			prev_entry->end = end;
917 			vm_map_entry_resize_free(map, prev_entry);
918 			vm_map_simplify_entry(map, prev_entry);
919 			return (KERN_SUCCESS);
920 		}
921 
922 		/*
923 		 * If we can extend the object but cannot extend the
924 		 * map entry, we have to create a new map entry.  We
925 		 * must bump the ref count on the extended object to
926 		 * account for it.  object may be NULL.
927 		 */
928 		object = prev_entry->object.vm_object;
929 		offset = prev_entry->offset +
930 			(prev_entry->end - prev_entry->start);
931 		vm_object_reference(object);
932 	}
933 
934 	/*
935 	 * NOTE: if conditionals fail, object can be NULL here.  This occurs
936 	 * in things like the buffer map where we manage kva but do not manage
937 	 * backing objects.
938 	 */
939 
940 	/*
941 	 * Create a new entry
942 	 */
943 	new_entry = vm_map_entry_create(map);
944 	new_entry->start = start;
945 	new_entry->end = end;
946 
947 	new_entry->eflags = protoeflags;
948 	new_entry->object.vm_object = object;
949 	new_entry->offset = offset;
950 	new_entry->avail_ssize = 0;
951 
952 	new_entry->inheritance = VM_INHERIT_DEFAULT;
953 	new_entry->protection = prot;
954 	new_entry->max_protection = max;
955 	new_entry->wired_count = 0;
956 
957 	/*
958 	 * Insert the new entry into the list
959 	 */
960 	vm_map_entry_link(map, prev_entry, new_entry);
961 	map->size += new_entry->end - new_entry->start;
962 
963 #if 0
964 	/*
965 	 * Temporarily removed to avoid MAP_STACK panic, due to
966 	 * MAP_STACK being a huge hack.  Will be added back in
967 	 * when MAP_STACK (and the user stack mapping) is fixed.
968 	 */
969 	/*
970 	 * It may be possible to simplify the entry
971 	 */
972 	vm_map_simplify_entry(map, new_entry);
973 #endif
974 
975 	if (cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) {
976 		vm_map_pmap_enter(map, start, prot,
977 				    object, OFF_TO_IDX(offset), end - start,
978 				    cow & MAP_PREFAULT_PARTIAL);
979 	}
980 
981 	return (KERN_SUCCESS);
982 }
983 
984 /*
985  *	vm_map_findspace:
986  *
987  *	Find the first fit (lowest VM address) for "length" free bytes
988  *	beginning at address >= start in the given map.
989  *
990  *	In a vm_map_entry, "adj_free" is the amount of free space
991  *	adjacent (higher address) to this entry, and "max_free" is the
992  *	maximum amount of contiguous free space in its subtree.  This
993  *	allows finding a free region in one path down the tree, so
994  *	O(log n) amortized with splay trees.
995  *
996  *	The map must be locked, and leaves it so.
997  *
998  *	Returns: 0 on success, and starting address in *addr,
999  *		 1 if insufficient space.
1000  */
1001 int
1002 vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length,
1003     vm_offset_t *addr)	/* OUT */
1004 {
1005 	vm_map_entry_t entry;
1006 	vm_offset_t end, st;
1007 
1008 	/*
1009 	 * Request must fit within min/max VM address and must avoid
1010 	 * address wrap.
1011 	 */
1012 	if (start < map->min_offset)
1013 		start = map->min_offset;
1014 	if (start + length > map->max_offset || start + length < start)
1015 		return (1);
1016 
1017 	/* Empty tree means wide open address space. */
1018 	if (map->root == NULL) {
1019 		*addr = start;
1020 		goto found;
1021 	}
1022 
1023 	/*
1024 	 * After splay, if start comes before root node, then there
1025 	 * must be a gap from start to the root.
1026 	 */
1027 	map->root = vm_map_entry_splay(start, map->root);
1028 	if (start + length <= map->root->start) {
1029 		*addr = start;
1030 		goto found;
1031 	}
1032 
1033 	/*
1034 	 * Root is the last node that might begin its gap before
1035 	 * start, and this is the last comparison where address
1036 	 * wrap might be a problem.
1037 	 */
1038 	st = (start > map->root->end) ? start : map->root->end;
1039 	if (length <= map->root->end + map->root->adj_free - st) {
1040 		*addr = st;
1041 		goto found;
1042 	}
1043 
1044 	/* With max_free, can immediately tell if no solution. */
1045 	entry = map->root->right;
1046 	if (entry == NULL || length > entry->max_free)
1047 		return (1);
1048 
1049 	/*
1050 	 * Search the right subtree in the order: left subtree, root,
1051 	 * right subtree (first fit).  The previous splay implies that
1052 	 * all regions in the right subtree have addresses > start.
1053 	 */
1054 	while (entry != NULL) {
1055 		if (entry->left != NULL && entry->left->max_free >= length)
1056 			entry = entry->left;
1057 		else if (entry->adj_free >= length) {
1058 			*addr = entry->end;
1059 			goto found;
1060 		} else
1061 			entry = entry->right;
1062 	}
1063 
1064 	/* Can't get here, so panic if we do. */
1065 	panic("vm_map_findspace: max_free corrupt");
1066 
1067 found:
1068 	/* Expand the kernel pmap, if necessary. */
1069 	if (map == kernel_map) {
1070 		end = round_page(*addr + length);
1071 		if (end > kernel_vm_end)
1072 			pmap_growkernel(end);
1073 	}
1074 	return (0);
1075 }
1076 
1077 /*
1078  *	vm_map_find finds an unallocated region in the target address
1079  *	map with the given length.  The search is defined to be
1080  *	first-fit from the specified address; the region found is
1081  *	returned in the same parameter.
1082  *
1083  *	If object is non-NULL, ref count must be bumped by caller
1084  *	prior to making call to account for the new entry.
1085  */
1086 int
1087 vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1088 	    vm_offset_t *addr,	/* IN/OUT */
1089 	    vm_size_t length, boolean_t find_space, vm_prot_t prot,
1090 	    vm_prot_t max, int cow)
1091 {
1092 	vm_offset_t start;
1093 	int result;
1094 
1095 	start = *addr;
1096 	vm_map_lock(map);
1097 	if (find_space) {
1098 		if (vm_map_findspace(map, start, length, addr)) {
1099 			vm_map_unlock(map);
1100 			return (KERN_NO_SPACE);
1101 		}
1102 		start = *addr;
1103 	}
1104 	result = vm_map_insert(map, object, offset,
1105 		start, start + length, prot, max, cow);
1106 	vm_map_unlock(map);
1107 	return (result);
1108 }
1109 
1110 /*
1111  *	vm_map_simplify_entry:
1112  *
1113  *	Simplify the given map entry by merging with either neighbor.  This
1114  *	routine also has the ability to merge with both neighbors.
1115  *
1116  *	The map must be locked.
1117  *
1118  *	This routine guarentees that the passed entry remains valid (though
1119  *	possibly extended).  When merging, this routine may delete one or
1120  *	both neighbors.
1121  */
1122 void
1123 vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry)
1124 {
1125 	vm_map_entry_t next, prev;
1126 	vm_size_t prevsize, esize;
1127 
1128 	if (entry->eflags & (MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP))
1129 		return;
1130 
1131 	prev = entry->prev;
1132 	if (prev != &map->header) {
1133 		prevsize = prev->end - prev->start;
1134 		if ( (prev->end == entry->start) &&
1135 		     (prev->object.vm_object == entry->object.vm_object) &&
1136 		     (!prev->object.vm_object ||
1137 			(prev->offset + prevsize == entry->offset)) &&
1138 		     (prev->eflags == entry->eflags) &&
1139 		     (prev->protection == entry->protection) &&
1140 		     (prev->max_protection == entry->max_protection) &&
1141 		     (prev->inheritance == entry->inheritance) &&
1142 		     (prev->wired_count == entry->wired_count)) {
1143 			vm_map_entry_unlink(map, prev);
1144 			entry->start = prev->start;
1145 			entry->offset = prev->offset;
1146 			if (entry->prev != &map->header)
1147 				vm_map_entry_resize_free(map, entry->prev);
1148 			if (prev->object.vm_object)
1149 				vm_object_deallocate(prev->object.vm_object);
1150 			vm_map_entry_dispose(map, prev);
1151 		}
1152 	}
1153 
1154 	next = entry->next;
1155 	if (next != &map->header) {
1156 		esize = entry->end - entry->start;
1157 		if ((entry->end == next->start) &&
1158 		    (next->object.vm_object == entry->object.vm_object) &&
1159 		     (!entry->object.vm_object ||
1160 			(entry->offset + esize == next->offset)) &&
1161 		    (next->eflags == entry->eflags) &&
1162 		    (next->protection == entry->protection) &&
1163 		    (next->max_protection == entry->max_protection) &&
1164 		    (next->inheritance == entry->inheritance) &&
1165 		    (next->wired_count == entry->wired_count)) {
1166 			vm_map_entry_unlink(map, next);
1167 			entry->end = next->end;
1168 			vm_map_entry_resize_free(map, entry);
1169 			if (next->object.vm_object)
1170 				vm_object_deallocate(next->object.vm_object);
1171 			vm_map_entry_dispose(map, next);
1172 		}
1173 	}
1174 }
1175 /*
1176  *	vm_map_clip_start:	[ internal use only ]
1177  *
1178  *	Asserts that the given entry begins at or after
1179  *	the specified address; if necessary,
1180  *	it splits the entry into two.
1181  */
1182 #define vm_map_clip_start(map, entry, startaddr) \
1183 { \
1184 	if (startaddr > entry->start) \
1185 		_vm_map_clip_start(map, entry, startaddr); \
1186 }
1187 
1188 /*
1189  *	This routine is called only when it is known that
1190  *	the entry must be split.
1191  */
1192 static void
1193 _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start)
1194 {
1195 	vm_map_entry_t new_entry;
1196 
1197 	/*
1198 	 * Split off the front portion -- note that we must insert the new
1199 	 * entry BEFORE this one, so that this entry has the specified
1200 	 * starting address.
1201 	 */
1202 	vm_map_simplify_entry(map, entry);
1203 
1204 	/*
1205 	 * If there is no object backing this entry, we might as well create
1206 	 * one now.  If we defer it, an object can get created after the map
1207 	 * is clipped, and individual objects will be created for the split-up
1208 	 * map.  This is a bit of a hack, but is also about the best place to
1209 	 * put this improvement.
1210 	 */
1211 	if (entry->object.vm_object == NULL && !map->system_map) {
1212 		vm_object_t object;
1213 		object = vm_object_allocate(OBJT_DEFAULT,
1214 				atop(entry->end - entry->start));
1215 		entry->object.vm_object = object;
1216 		entry->offset = 0;
1217 	}
1218 
1219 	new_entry = vm_map_entry_create(map);
1220 	*new_entry = *entry;
1221 
1222 	new_entry->end = start;
1223 	entry->offset += (start - entry->start);
1224 	entry->start = start;
1225 
1226 	vm_map_entry_link(map, entry->prev, new_entry);
1227 
1228 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1229 		vm_object_reference(new_entry->object.vm_object);
1230 	}
1231 }
1232 
1233 /*
1234  *	vm_map_clip_end:	[ internal use only ]
1235  *
1236  *	Asserts that the given entry ends at or before
1237  *	the specified address; if necessary,
1238  *	it splits the entry into two.
1239  */
1240 #define vm_map_clip_end(map, entry, endaddr) \
1241 { \
1242 	if ((endaddr) < (entry->end)) \
1243 		_vm_map_clip_end((map), (entry), (endaddr)); \
1244 }
1245 
1246 /*
1247  *	This routine is called only when it is known that
1248  *	the entry must be split.
1249  */
1250 static void
1251 _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end)
1252 {
1253 	vm_map_entry_t new_entry;
1254 
1255 	/*
1256 	 * If there is no object backing this entry, we might as well create
1257 	 * one now.  If we defer it, an object can get created after the map
1258 	 * is clipped, and individual objects will be created for the split-up
1259 	 * map.  This is a bit of a hack, but is also about the best place to
1260 	 * put this improvement.
1261 	 */
1262 	if (entry->object.vm_object == NULL && !map->system_map) {
1263 		vm_object_t object;
1264 		object = vm_object_allocate(OBJT_DEFAULT,
1265 				atop(entry->end - entry->start));
1266 		entry->object.vm_object = object;
1267 		entry->offset = 0;
1268 	}
1269 
1270 	/*
1271 	 * Create a new entry and insert it AFTER the specified entry
1272 	 */
1273 	new_entry = vm_map_entry_create(map);
1274 	*new_entry = *entry;
1275 
1276 	new_entry->start = entry->end = end;
1277 	new_entry->offset += (end - entry->start);
1278 
1279 	vm_map_entry_link(map, entry, new_entry);
1280 
1281 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1282 		vm_object_reference(new_entry->object.vm_object);
1283 	}
1284 }
1285 
1286 /*
1287  *	VM_MAP_RANGE_CHECK:	[ internal use only ]
1288  *
1289  *	Asserts that the starting and ending region
1290  *	addresses fall within the valid range of the map.
1291  */
1292 #define	VM_MAP_RANGE_CHECK(map, start, end)		\
1293 		{					\
1294 		if (start < vm_map_min(map))		\
1295 			start = vm_map_min(map);	\
1296 		if (end > vm_map_max(map))		\
1297 			end = vm_map_max(map);		\
1298 		if (start > end)			\
1299 			start = end;			\
1300 		}
1301 
1302 /*
1303  *	vm_map_submap:		[ kernel use only ]
1304  *
1305  *	Mark the given range as handled by a subordinate map.
1306  *
1307  *	This range must have been created with vm_map_find,
1308  *	and no other operations may have been performed on this
1309  *	range prior to calling vm_map_submap.
1310  *
1311  *	Only a limited number of operations can be performed
1312  *	within this rage after calling vm_map_submap:
1313  *		vm_fault
1314  *	[Don't try vm_map_copy!]
1315  *
1316  *	To remove a submapping, one must first remove the
1317  *	range from the superior map, and then destroy the
1318  *	submap (if desired).  [Better yet, don't try it.]
1319  */
1320 int
1321 vm_map_submap(
1322 	vm_map_t map,
1323 	vm_offset_t start,
1324 	vm_offset_t end,
1325 	vm_map_t submap)
1326 {
1327 	vm_map_entry_t entry;
1328 	int result = KERN_INVALID_ARGUMENT;
1329 
1330 	vm_map_lock(map);
1331 
1332 	VM_MAP_RANGE_CHECK(map, start, end);
1333 
1334 	if (vm_map_lookup_entry(map, start, &entry)) {
1335 		vm_map_clip_start(map, entry, start);
1336 	} else
1337 		entry = entry->next;
1338 
1339 	vm_map_clip_end(map, entry, end);
1340 
1341 	if ((entry->start == start) && (entry->end == end) &&
1342 	    ((entry->eflags & MAP_ENTRY_COW) == 0) &&
1343 	    (entry->object.vm_object == NULL)) {
1344 		entry->object.sub_map = submap;
1345 		entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
1346 		result = KERN_SUCCESS;
1347 	}
1348 	vm_map_unlock(map);
1349 
1350 	return (result);
1351 }
1352 
1353 /*
1354  * The maximum number of pages to map
1355  */
1356 #define	MAX_INIT_PT	96
1357 
1358 /*
1359  *	vm_map_pmap_enter:
1360  *
1361  *	Preload read-only mappings for the given object into the specified
1362  *	map.  This eliminates the soft faults on process startup and
1363  *	immediately after an mmap(2).
1364  */
1365 void
1366 vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
1367     vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags)
1368 {
1369 	vm_offset_t tmpidx;
1370 	int psize;
1371 	vm_page_t p, mpte;
1372 	boolean_t are_queues_locked;
1373 
1374 	if ((prot & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 || object == NULL)
1375 		return;
1376 	VM_OBJECT_LOCK(object);
1377 	if (object->type == OBJT_DEVICE) {
1378 		pmap_object_init_pt(map->pmap, addr, object, pindex, size);
1379 		goto unlock_return;
1380 	}
1381 
1382 	psize = atop(size);
1383 
1384 	if (object->type != OBJT_VNODE ||
1385 	    ((flags & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
1386 	     (object->resident_page_count > MAX_INIT_PT))) {
1387 		goto unlock_return;
1388 	}
1389 
1390 	if (psize + pindex > object->size) {
1391 		if (object->size < pindex)
1392 			goto unlock_return;
1393 		psize = object->size - pindex;
1394 	}
1395 
1396 	are_queues_locked = FALSE;
1397 	mpte = NULL;
1398 
1399 	if ((p = TAILQ_FIRST(&object->memq)) != NULL) {
1400 		if (p->pindex < pindex) {
1401 			p = vm_page_splay(pindex, object->root);
1402 			if ((object->root = p)->pindex < pindex)
1403 				p = TAILQ_NEXT(p, listq);
1404 		}
1405 	}
1406 	/*
1407 	 * Assert: the variable p is either (1) the page with the
1408 	 * least pindex greater than or equal to the parameter pindex
1409 	 * or (2) NULL.
1410 	 */
1411 	for (;
1412 	     p != NULL && (tmpidx = p->pindex - pindex) < psize;
1413 	     p = TAILQ_NEXT(p, listq)) {
1414 		/*
1415 		 * don't allow an madvise to blow away our really
1416 		 * free pages allocating pv entries.
1417 		 */
1418 		if ((flags & MAP_PREFAULT_MADVISE) &&
1419 		    cnt.v_free_count < cnt.v_free_reserved) {
1420 			break;
1421 		}
1422 		if ((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL &&
1423 		    (p->busy == 0) &&
1424 		    (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
1425 			if (!are_queues_locked) {
1426 				are_queues_locked = TRUE;
1427 				vm_page_lock_queues();
1428 			}
1429 			if (VM_PAGE_INQUEUE1(p, PQ_CACHE))
1430 				vm_page_deactivate(p);
1431 			mpte = pmap_enter_quick(map->pmap,
1432 			    addr + ptoa(tmpidx), p, prot, mpte);
1433 		}
1434 	}
1435 	if (are_queues_locked)
1436 		vm_page_unlock_queues();
1437 unlock_return:
1438 	VM_OBJECT_UNLOCK(object);
1439 }
1440 
1441 /*
1442  *	vm_map_protect:
1443  *
1444  *	Sets the protection of the specified address
1445  *	region in the target map.  If "set_max" is
1446  *	specified, the maximum protection is to be set;
1447  *	otherwise, only the current protection is affected.
1448  */
1449 int
1450 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
1451 	       vm_prot_t new_prot, boolean_t set_max)
1452 {
1453 	vm_map_entry_t current;
1454 	vm_map_entry_t entry;
1455 
1456 	vm_map_lock(map);
1457 
1458 	VM_MAP_RANGE_CHECK(map, start, end);
1459 
1460 	if (vm_map_lookup_entry(map, start, &entry)) {
1461 		vm_map_clip_start(map, entry, start);
1462 	} else {
1463 		entry = entry->next;
1464 	}
1465 
1466 	/*
1467 	 * Make a first pass to check for protection violations.
1468 	 */
1469 	current = entry;
1470 	while ((current != &map->header) && (current->start < end)) {
1471 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1472 			vm_map_unlock(map);
1473 			return (KERN_INVALID_ARGUMENT);
1474 		}
1475 		if ((new_prot & current->max_protection) != new_prot) {
1476 			vm_map_unlock(map);
1477 			return (KERN_PROTECTION_FAILURE);
1478 		}
1479 		current = current->next;
1480 	}
1481 
1482 	/*
1483 	 * Go back and fix up protections. [Note that clipping is not
1484 	 * necessary the second time.]
1485 	 */
1486 	current = entry;
1487 	while ((current != &map->header) && (current->start < end)) {
1488 		vm_prot_t old_prot;
1489 
1490 		vm_map_clip_end(map, current, end);
1491 
1492 		old_prot = current->protection;
1493 		if (set_max)
1494 			current->protection =
1495 			    (current->max_protection = new_prot) &
1496 			    old_prot;
1497 		else
1498 			current->protection = new_prot;
1499 
1500 		/*
1501 		 * Update physical map if necessary. Worry about copy-on-write
1502 		 * here -- CHECK THIS XXX
1503 		 */
1504 		if (current->protection != old_prot) {
1505 #define MASK(entry)	(((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1506 							VM_PROT_ALL)
1507 			pmap_protect(map->pmap, current->start,
1508 			    current->end,
1509 			    current->protection & MASK(current));
1510 #undef	MASK
1511 		}
1512 		vm_map_simplify_entry(map, current);
1513 		current = current->next;
1514 	}
1515 	vm_map_unlock(map);
1516 	return (KERN_SUCCESS);
1517 }
1518 
1519 /*
1520  *	vm_map_madvise:
1521  *
1522  *	This routine traverses a processes map handling the madvise
1523  *	system call.  Advisories are classified as either those effecting
1524  *	the vm_map_entry structure, or those effecting the underlying
1525  *	objects.
1526  */
1527 int
1528 vm_map_madvise(
1529 	vm_map_t map,
1530 	vm_offset_t start,
1531 	vm_offset_t end,
1532 	int behav)
1533 {
1534 	vm_map_entry_t current, entry;
1535 	int modify_map = 0;
1536 
1537 	/*
1538 	 * Some madvise calls directly modify the vm_map_entry, in which case
1539 	 * we need to use an exclusive lock on the map and we need to perform
1540 	 * various clipping operations.  Otherwise we only need a read-lock
1541 	 * on the map.
1542 	 */
1543 	switch(behav) {
1544 	case MADV_NORMAL:
1545 	case MADV_SEQUENTIAL:
1546 	case MADV_RANDOM:
1547 	case MADV_NOSYNC:
1548 	case MADV_AUTOSYNC:
1549 	case MADV_NOCORE:
1550 	case MADV_CORE:
1551 		modify_map = 1;
1552 		vm_map_lock(map);
1553 		break;
1554 	case MADV_WILLNEED:
1555 	case MADV_DONTNEED:
1556 	case MADV_FREE:
1557 		vm_map_lock_read(map);
1558 		break;
1559 	default:
1560 		return (KERN_INVALID_ARGUMENT);
1561 	}
1562 
1563 	/*
1564 	 * Locate starting entry and clip if necessary.
1565 	 */
1566 	VM_MAP_RANGE_CHECK(map, start, end);
1567 
1568 	if (vm_map_lookup_entry(map, start, &entry)) {
1569 		if (modify_map)
1570 			vm_map_clip_start(map, entry, start);
1571 	} else {
1572 		entry = entry->next;
1573 	}
1574 
1575 	if (modify_map) {
1576 		/*
1577 		 * madvise behaviors that are implemented in the vm_map_entry.
1578 		 *
1579 		 * We clip the vm_map_entry so that behavioral changes are
1580 		 * limited to the specified address range.
1581 		 */
1582 		for (current = entry;
1583 		     (current != &map->header) && (current->start < end);
1584 		     current = current->next
1585 		) {
1586 			if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1587 				continue;
1588 
1589 			vm_map_clip_end(map, current, end);
1590 
1591 			switch (behav) {
1592 			case MADV_NORMAL:
1593 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
1594 				break;
1595 			case MADV_SEQUENTIAL:
1596 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
1597 				break;
1598 			case MADV_RANDOM:
1599 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
1600 				break;
1601 			case MADV_NOSYNC:
1602 				current->eflags |= MAP_ENTRY_NOSYNC;
1603 				break;
1604 			case MADV_AUTOSYNC:
1605 				current->eflags &= ~MAP_ENTRY_NOSYNC;
1606 				break;
1607 			case MADV_NOCORE:
1608 				current->eflags |= MAP_ENTRY_NOCOREDUMP;
1609 				break;
1610 			case MADV_CORE:
1611 				current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
1612 				break;
1613 			default:
1614 				break;
1615 			}
1616 			vm_map_simplify_entry(map, current);
1617 		}
1618 		vm_map_unlock(map);
1619 	} else {
1620 		vm_pindex_t pindex;
1621 		int count;
1622 
1623 		/*
1624 		 * madvise behaviors that are implemented in the underlying
1625 		 * vm_object.
1626 		 *
1627 		 * Since we don't clip the vm_map_entry, we have to clip
1628 		 * the vm_object pindex and count.
1629 		 */
1630 		for (current = entry;
1631 		     (current != &map->header) && (current->start < end);
1632 		     current = current->next
1633 		) {
1634 			vm_offset_t useStart;
1635 
1636 			if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1637 				continue;
1638 
1639 			pindex = OFF_TO_IDX(current->offset);
1640 			count = atop(current->end - current->start);
1641 			useStart = current->start;
1642 
1643 			if (current->start < start) {
1644 				pindex += atop(start - current->start);
1645 				count -= atop(start - current->start);
1646 				useStart = start;
1647 			}
1648 			if (current->end > end)
1649 				count -= atop(current->end - end);
1650 
1651 			if (count <= 0)
1652 				continue;
1653 
1654 			vm_object_madvise(current->object.vm_object,
1655 					  pindex, count, behav);
1656 			if (behav == MADV_WILLNEED) {
1657 				vm_map_pmap_enter(map,
1658 				    useStart,
1659 				    current->protection,
1660 				    current->object.vm_object,
1661 				    pindex,
1662 				    (count << PAGE_SHIFT),
1663 				    MAP_PREFAULT_MADVISE
1664 				);
1665 			}
1666 		}
1667 		vm_map_unlock_read(map);
1668 	}
1669 	return (0);
1670 }
1671 
1672 
1673 /*
1674  *	vm_map_inherit:
1675  *
1676  *	Sets the inheritance of the specified address
1677  *	range in the target map.  Inheritance
1678  *	affects how the map will be shared with
1679  *	child maps at the time of vm_map_fork.
1680  */
1681 int
1682 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
1683 	       vm_inherit_t new_inheritance)
1684 {
1685 	vm_map_entry_t entry;
1686 	vm_map_entry_t temp_entry;
1687 
1688 	switch (new_inheritance) {
1689 	case VM_INHERIT_NONE:
1690 	case VM_INHERIT_COPY:
1691 	case VM_INHERIT_SHARE:
1692 		break;
1693 	default:
1694 		return (KERN_INVALID_ARGUMENT);
1695 	}
1696 	vm_map_lock(map);
1697 	VM_MAP_RANGE_CHECK(map, start, end);
1698 	if (vm_map_lookup_entry(map, start, &temp_entry)) {
1699 		entry = temp_entry;
1700 		vm_map_clip_start(map, entry, start);
1701 	} else
1702 		entry = temp_entry->next;
1703 	while ((entry != &map->header) && (entry->start < end)) {
1704 		vm_map_clip_end(map, entry, end);
1705 		entry->inheritance = new_inheritance;
1706 		vm_map_simplify_entry(map, entry);
1707 		entry = entry->next;
1708 	}
1709 	vm_map_unlock(map);
1710 	return (KERN_SUCCESS);
1711 }
1712 
1713 /*
1714  *	vm_map_unwire:
1715  *
1716  *	Implements both kernel and user unwiring.
1717  */
1718 int
1719 vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
1720     int flags)
1721 {
1722 	vm_map_entry_t entry, first_entry, tmp_entry;
1723 	vm_offset_t saved_start;
1724 	unsigned int last_timestamp;
1725 	int rv;
1726 	boolean_t need_wakeup, result, user_unwire;
1727 
1728 	user_unwire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
1729 	vm_map_lock(map);
1730 	VM_MAP_RANGE_CHECK(map, start, end);
1731 	if (!vm_map_lookup_entry(map, start, &first_entry)) {
1732 		if (flags & VM_MAP_WIRE_HOLESOK)
1733 			first_entry = first_entry->next;
1734 		else {
1735 			vm_map_unlock(map);
1736 			return (KERN_INVALID_ADDRESS);
1737 		}
1738 	}
1739 	last_timestamp = map->timestamp;
1740 	entry = first_entry;
1741 	while (entry != &map->header && entry->start < end) {
1742 		if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1743 			/*
1744 			 * We have not yet clipped the entry.
1745 			 */
1746 			saved_start = (start >= entry->start) ? start :
1747 			    entry->start;
1748 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1749 			if (vm_map_unlock_and_wait(map, user_unwire)) {
1750 				/*
1751 				 * Allow interruption of user unwiring?
1752 				 */
1753 			}
1754 			vm_map_lock(map);
1755 			if (last_timestamp+1 != map->timestamp) {
1756 				/*
1757 				 * Look again for the entry because the map was
1758 				 * modified while it was unlocked.
1759 				 * Specifically, the entry may have been
1760 				 * clipped, merged, or deleted.
1761 				 */
1762 				if (!vm_map_lookup_entry(map, saved_start,
1763 				    &tmp_entry)) {
1764 					if (flags & VM_MAP_WIRE_HOLESOK)
1765 						tmp_entry = tmp_entry->next;
1766 					else {
1767 						if (saved_start == start) {
1768 							/*
1769 							 * First_entry has been deleted.
1770 							 */
1771 							vm_map_unlock(map);
1772 							return (KERN_INVALID_ADDRESS);
1773 						}
1774 						end = saved_start;
1775 						rv = KERN_INVALID_ADDRESS;
1776 						goto done;
1777 					}
1778 				}
1779 				if (entry == first_entry)
1780 					first_entry = tmp_entry;
1781 				else
1782 					first_entry = NULL;
1783 				entry = tmp_entry;
1784 			}
1785 			last_timestamp = map->timestamp;
1786 			continue;
1787 		}
1788 		vm_map_clip_start(map, entry, start);
1789 		vm_map_clip_end(map, entry, end);
1790 		/*
1791 		 * Mark the entry in case the map lock is released.  (See
1792 		 * above.)
1793 		 */
1794 		entry->eflags |= MAP_ENTRY_IN_TRANSITION;
1795 		/*
1796 		 * Check the map for holes in the specified region.
1797 		 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
1798 		 */
1799 		if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
1800 		    (entry->end < end && (entry->next == &map->header ||
1801 		    entry->next->start > entry->end))) {
1802 			end = entry->end;
1803 			rv = KERN_INVALID_ADDRESS;
1804 			goto done;
1805 		}
1806 		/*
1807 		 * If system unwiring, require that the entry is system wired.
1808 		 */
1809 		if (!user_unwire &&
1810 		    vm_map_entry_system_wired_count(entry) == 0) {
1811 			end = entry->end;
1812 			rv = KERN_INVALID_ARGUMENT;
1813 			goto done;
1814 		}
1815 		entry = entry->next;
1816 	}
1817 	rv = KERN_SUCCESS;
1818 done:
1819 	need_wakeup = FALSE;
1820 	if (first_entry == NULL) {
1821 		result = vm_map_lookup_entry(map, start, &first_entry);
1822 		if (!result && (flags & VM_MAP_WIRE_HOLESOK))
1823 			first_entry = first_entry->next;
1824 		else
1825 			KASSERT(result, ("vm_map_unwire: lookup failed"));
1826 	}
1827 	entry = first_entry;
1828 	while (entry != &map->header && entry->start < end) {
1829 		if (rv == KERN_SUCCESS && (!user_unwire ||
1830 		    (entry->eflags & MAP_ENTRY_USER_WIRED))) {
1831 			if (user_unwire)
1832 				entry->eflags &= ~MAP_ENTRY_USER_WIRED;
1833 			entry->wired_count--;
1834 			if (entry->wired_count == 0) {
1835 				/*
1836 				 * Retain the map lock.
1837 				 */
1838 				vm_fault_unwire(map, entry->start, entry->end,
1839 				    entry->object.vm_object != NULL &&
1840 				    entry->object.vm_object->type == OBJT_DEVICE);
1841 			}
1842 		}
1843 		KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
1844 			("vm_map_unwire: in-transition flag missing"));
1845 		entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
1846 		if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
1847 			entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
1848 			need_wakeup = TRUE;
1849 		}
1850 		vm_map_simplify_entry(map, entry);
1851 		entry = entry->next;
1852 	}
1853 	vm_map_unlock(map);
1854 	if (need_wakeup)
1855 		vm_map_wakeup(map);
1856 	return (rv);
1857 }
1858 
1859 /*
1860  *	vm_map_wire:
1861  *
1862  *	Implements both kernel and user wiring.
1863  */
1864 int
1865 vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
1866     int flags)
1867 {
1868 	vm_map_entry_t entry, first_entry, tmp_entry;
1869 	vm_offset_t saved_end, saved_start;
1870 	unsigned int last_timestamp;
1871 	int rv;
1872 	boolean_t fictitious, need_wakeup, result, user_wire;
1873 
1874 	user_wire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
1875 	vm_map_lock(map);
1876 	VM_MAP_RANGE_CHECK(map, start, end);
1877 	if (!vm_map_lookup_entry(map, start, &first_entry)) {
1878 		if (flags & VM_MAP_WIRE_HOLESOK)
1879 			first_entry = first_entry->next;
1880 		else {
1881 			vm_map_unlock(map);
1882 			return (KERN_INVALID_ADDRESS);
1883 		}
1884 	}
1885 	last_timestamp = map->timestamp;
1886 	entry = first_entry;
1887 	while (entry != &map->header && entry->start < end) {
1888 		if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1889 			/*
1890 			 * We have not yet clipped the entry.
1891 			 */
1892 			saved_start = (start >= entry->start) ? start :
1893 			    entry->start;
1894 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1895 			if (vm_map_unlock_and_wait(map, user_wire)) {
1896 				/*
1897 				 * Allow interruption of user wiring?
1898 				 */
1899 			}
1900 			vm_map_lock(map);
1901 			if (last_timestamp + 1 != map->timestamp) {
1902 				/*
1903 				 * Look again for the entry because the map was
1904 				 * modified while it was unlocked.
1905 				 * Specifically, the entry may have been
1906 				 * clipped, merged, or deleted.
1907 				 */
1908 				if (!vm_map_lookup_entry(map, saved_start,
1909 				    &tmp_entry)) {
1910 					if (flags & VM_MAP_WIRE_HOLESOK)
1911 						tmp_entry = tmp_entry->next;
1912 					else {
1913 						if (saved_start == start) {
1914 							/*
1915 							 * first_entry has been deleted.
1916 							 */
1917 							vm_map_unlock(map);
1918 							return (KERN_INVALID_ADDRESS);
1919 						}
1920 						end = saved_start;
1921 						rv = KERN_INVALID_ADDRESS;
1922 						goto done;
1923 					}
1924 				}
1925 				if (entry == first_entry)
1926 					first_entry = tmp_entry;
1927 				else
1928 					first_entry = NULL;
1929 				entry = tmp_entry;
1930 			}
1931 			last_timestamp = map->timestamp;
1932 			continue;
1933 		}
1934 		vm_map_clip_start(map, entry, start);
1935 		vm_map_clip_end(map, entry, end);
1936 		/*
1937 		 * Mark the entry in case the map lock is released.  (See
1938 		 * above.)
1939 		 */
1940 		entry->eflags |= MAP_ENTRY_IN_TRANSITION;
1941 		/*
1942 		 *
1943 		 */
1944 		if (entry->wired_count == 0) {
1945 			entry->wired_count++;
1946 			saved_start = entry->start;
1947 			saved_end = entry->end;
1948 			fictitious = entry->object.vm_object != NULL &&
1949 			    entry->object.vm_object->type == OBJT_DEVICE;
1950 			/*
1951 			 * Release the map lock, relying on the in-transition
1952 			 * mark.
1953 			 */
1954 			vm_map_unlock(map);
1955 			rv = vm_fault_wire(map, saved_start, saved_end,
1956 			    user_wire, fictitious);
1957 			vm_map_lock(map);
1958 			if (last_timestamp + 1 != map->timestamp) {
1959 				/*
1960 				 * Look again for the entry because the map was
1961 				 * modified while it was unlocked.  The entry
1962 				 * may have been clipped, but NOT merged or
1963 				 * deleted.
1964 				 */
1965 				result = vm_map_lookup_entry(map, saved_start,
1966 				    &tmp_entry);
1967 				KASSERT(result, ("vm_map_wire: lookup failed"));
1968 				if (entry == first_entry)
1969 					first_entry = tmp_entry;
1970 				else
1971 					first_entry = NULL;
1972 				entry = tmp_entry;
1973 				while (entry->end < saved_end) {
1974 					if (rv != KERN_SUCCESS) {
1975 						KASSERT(entry->wired_count == 1,
1976 						    ("vm_map_wire: bad count"));
1977 						entry->wired_count = -1;
1978 					}
1979 					entry = entry->next;
1980 				}
1981 			}
1982 			last_timestamp = map->timestamp;
1983 			if (rv != KERN_SUCCESS) {
1984 				KASSERT(entry->wired_count == 1,
1985 				    ("vm_map_wire: bad count"));
1986 				/*
1987 				 * Assign an out-of-range value to represent
1988 				 * the failure to wire this entry.
1989 				 */
1990 				entry->wired_count = -1;
1991 				end = entry->end;
1992 				goto done;
1993 			}
1994 		} else if (!user_wire ||
1995 			   (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
1996 			entry->wired_count++;
1997 		}
1998 		/*
1999 		 * Check the map for holes in the specified region.
2000 		 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
2001 		 */
2002 		if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
2003 		    (entry->end < end && (entry->next == &map->header ||
2004 		    entry->next->start > entry->end))) {
2005 			end = entry->end;
2006 			rv = KERN_INVALID_ADDRESS;
2007 			goto done;
2008 		}
2009 		entry = entry->next;
2010 	}
2011 	rv = KERN_SUCCESS;
2012 done:
2013 	need_wakeup = FALSE;
2014 	if (first_entry == NULL) {
2015 		result = vm_map_lookup_entry(map, start, &first_entry);
2016 		if (!result && (flags & VM_MAP_WIRE_HOLESOK))
2017 			first_entry = first_entry->next;
2018 		else
2019 			KASSERT(result, ("vm_map_wire: lookup failed"));
2020 	}
2021 	entry = first_entry;
2022 	while (entry != &map->header && entry->start < end) {
2023 		if (rv == KERN_SUCCESS) {
2024 			if (user_wire)
2025 				entry->eflags |= MAP_ENTRY_USER_WIRED;
2026 		} else if (entry->wired_count == -1) {
2027 			/*
2028 			 * Wiring failed on this entry.  Thus, unwiring is
2029 			 * unnecessary.
2030 			 */
2031 			entry->wired_count = 0;
2032 		} else {
2033 			if (!user_wire ||
2034 			    (entry->eflags & MAP_ENTRY_USER_WIRED) == 0)
2035 				entry->wired_count--;
2036 			if (entry->wired_count == 0) {
2037 				/*
2038 				 * Retain the map lock.
2039 				 */
2040 				vm_fault_unwire(map, entry->start, entry->end,
2041 				    entry->object.vm_object != NULL &&
2042 				    entry->object.vm_object->type == OBJT_DEVICE);
2043 			}
2044 		}
2045 		KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
2046 			("vm_map_wire: in-transition flag missing"));
2047 		entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
2048 		if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
2049 			entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
2050 			need_wakeup = TRUE;
2051 		}
2052 		vm_map_simplify_entry(map, entry);
2053 		entry = entry->next;
2054 	}
2055 	vm_map_unlock(map);
2056 	if (need_wakeup)
2057 		vm_map_wakeup(map);
2058 	return (rv);
2059 }
2060 
2061 /*
2062  * vm_map_sync
2063  *
2064  * Push any dirty cached pages in the address range to their pager.
2065  * If syncio is TRUE, dirty pages are written synchronously.
2066  * If invalidate is TRUE, any cached pages are freed as well.
2067  *
2068  * If the size of the region from start to end is zero, we are
2069  * supposed to flush all modified pages within the region containing
2070  * start.  Unfortunately, a region can be split or coalesced with
2071  * neighboring regions, making it difficult to determine what the
2072  * original region was.  Therefore, we approximate this requirement by
2073  * flushing the current region containing start.
2074  *
2075  * Returns an error if any part of the specified range is not mapped.
2076  */
2077 int
2078 vm_map_sync(
2079 	vm_map_t map,
2080 	vm_offset_t start,
2081 	vm_offset_t end,
2082 	boolean_t syncio,
2083 	boolean_t invalidate)
2084 {
2085 	vm_map_entry_t current;
2086 	vm_map_entry_t entry;
2087 	vm_size_t size;
2088 	vm_object_t object;
2089 	vm_ooffset_t offset;
2090 
2091 	vm_map_lock_read(map);
2092 	VM_MAP_RANGE_CHECK(map, start, end);
2093 	if (!vm_map_lookup_entry(map, start, &entry)) {
2094 		vm_map_unlock_read(map);
2095 		return (KERN_INVALID_ADDRESS);
2096 	} else if (start == end) {
2097 		start = entry->start;
2098 		end = entry->end;
2099 	}
2100 	/*
2101 	 * Make a first pass to check for user-wired memory and holes.
2102 	 */
2103 	for (current = entry; current->start < end; current = current->next) {
2104 		if (invalidate && (current->eflags & MAP_ENTRY_USER_WIRED)) {
2105 			vm_map_unlock_read(map);
2106 			return (KERN_INVALID_ARGUMENT);
2107 		}
2108 		if (end > current->end &&
2109 		    (current->next == &map->header ||
2110 			current->end != current->next->start)) {
2111 			vm_map_unlock_read(map);
2112 			return (KERN_INVALID_ADDRESS);
2113 		}
2114 	}
2115 
2116 	if (invalidate) {
2117 		VM_LOCK_GIANT();
2118 		pmap_remove(map->pmap, start, end);
2119 		VM_UNLOCK_GIANT();
2120 	}
2121 	/*
2122 	 * Make a second pass, cleaning/uncaching pages from the indicated
2123 	 * objects as we go.
2124 	 */
2125 	for (current = entry; current->start < end; current = current->next) {
2126 		offset = current->offset + (start - current->start);
2127 		size = (end <= current->end ? end : current->end) - start;
2128 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
2129 			vm_map_t smap;
2130 			vm_map_entry_t tentry;
2131 			vm_size_t tsize;
2132 
2133 			smap = current->object.sub_map;
2134 			vm_map_lock_read(smap);
2135 			(void) vm_map_lookup_entry(smap, offset, &tentry);
2136 			tsize = tentry->end - offset;
2137 			if (tsize < size)
2138 				size = tsize;
2139 			object = tentry->object.vm_object;
2140 			offset = tentry->offset + (offset - tentry->start);
2141 			vm_map_unlock_read(smap);
2142 		} else {
2143 			object = current->object.vm_object;
2144 		}
2145 		vm_object_sync(object, offset, size, syncio, invalidate);
2146 		start += size;
2147 	}
2148 
2149 	vm_map_unlock_read(map);
2150 	return (KERN_SUCCESS);
2151 }
2152 
2153 /*
2154  *	vm_map_entry_unwire:	[ internal use only ]
2155  *
2156  *	Make the region specified by this entry pageable.
2157  *
2158  *	The map in question should be locked.
2159  *	[This is the reason for this routine's existence.]
2160  */
2161 static void
2162 vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
2163 {
2164 	vm_fault_unwire(map, entry->start, entry->end,
2165 	    entry->object.vm_object != NULL &&
2166 	    entry->object.vm_object->type == OBJT_DEVICE);
2167 	entry->wired_count = 0;
2168 }
2169 
2170 /*
2171  *	vm_map_entry_delete:	[ internal use only ]
2172  *
2173  *	Deallocate the given entry from the target map.
2174  */
2175 static void
2176 vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry)
2177 {
2178 	vm_object_t object;
2179 	vm_pindex_t offidxstart, offidxend, count;
2180 
2181 	vm_map_entry_unlink(map, entry);
2182 	map->size -= entry->end - entry->start;
2183 
2184 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
2185 	    (object = entry->object.vm_object) != NULL) {
2186 		count = OFF_TO_IDX(entry->end - entry->start);
2187 		offidxstart = OFF_TO_IDX(entry->offset);
2188 		offidxend = offidxstart + count;
2189 		VM_OBJECT_LOCK(object);
2190 		if (object->ref_count != 1 &&
2191 		    ((object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING ||
2192 		     object == kernel_object || object == kmem_object) &&
2193 		    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2194 			vm_object_collapse(object);
2195 			vm_object_page_remove(object, offidxstart, offidxend, FALSE);
2196 			if (object->type == OBJT_SWAP)
2197 				swap_pager_freespace(object, offidxstart, count);
2198 			if (offidxend >= object->size &&
2199 			    offidxstart < object->size)
2200 				object->size = offidxstart;
2201 		}
2202 		VM_OBJECT_UNLOCK(object);
2203 		vm_object_deallocate(object);
2204 	}
2205 
2206 	vm_map_entry_dispose(map, entry);
2207 }
2208 
2209 /*
2210  *	vm_map_delete:	[ internal use only ]
2211  *
2212  *	Deallocates the given address range from the target
2213  *	map.
2214  */
2215 int
2216 vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end)
2217 {
2218 	vm_map_entry_t entry;
2219 	vm_map_entry_t first_entry;
2220 
2221 	/*
2222 	 * Find the start of the region, and clip it
2223 	 */
2224 	if (!vm_map_lookup_entry(map, start, &first_entry))
2225 		entry = first_entry->next;
2226 	else {
2227 		entry = first_entry;
2228 		vm_map_clip_start(map, entry, start);
2229 	}
2230 
2231 	/*
2232 	 * Step through all entries in this region
2233 	 */
2234 	while ((entry != &map->header) && (entry->start < end)) {
2235 		vm_map_entry_t next;
2236 
2237 		/*
2238 		 * Wait for wiring or unwiring of an entry to complete.
2239 		 * Also wait for any system wirings to disappear on
2240 		 * user maps.
2241 		 */
2242 		if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 ||
2243 		    (vm_map_pmap(map) != kernel_pmap &&
2244 		    vm_map_entry_system_wired_count(entry) != 0)) {
2245 			unsigned int last_timestamp;
2246 			vm_offset_t saved_start;
2247 			vm_map_entry_t tmp_entry;
2248 
2249 			saved_start = entry->start;
2250 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2251 			last_timestamp = map->timestamp;
2252 			(void) vm_map_unlock_and_wait(map, FALSE);
2253 			vm_map_lock(map);
2254 			if (last_timestamp + 1 != map->timestamp) {
2255 				/*
2256 				 * Look again for the entry because the map was
2257 				 * modified while it was unlocked.
2258 				 * Specifically, the entry may have been
2259 				 * clipped, merged, or deleted.
2260 				 */
2261 				if (!vm_map_lookup_entry(map, saved_start,
2262 							 &tmp_entry))
2263 					entry = tmp_entry->next;
2264 				else {
2265 					entry = tmp_entry;
2266 					vm_map_clip_start(map, entry,
2267 							  saved_start);
2268 				}
2269 			}
2270 			continue;
2271 		}
2272 		vm_map_clip_end(map, entry, end);
2273 
2274 		next = entry->next;
2275 
2276 		/*
2277 		 * Unwire before removing addresses from the pmap; otherwise,
2278 		 * unwiring will put the entries back in the pmap.
2279 		 */
2280 		if (entry->wired_count != 0) {
2281 			vm_map_entry_unwire(map, entry);
2282 		}
2283 
2284 		if (!map->system_map)
2285 			VM_LOCK_GIANT();
2286 		pmap_remove(map->pmap, entry->start, entry->end);
2287 		if (!map->system_map)
2288 			VM_UNLOCK_GIANT();
2289 
2290 		/*
2291 		 * Delete the entry (which may delete the object) only after
2292 		 * removing all pmap entries pointing to its pages.
2293 		 * (Otherwise, its page frames may be reallocated, and any
2294 		 * modify bits will be set in the wrong object!)
2295 		 */
2296 		vm_map_entry_delete(map, entry);
2297 		entry = next;
2298 	}
2299 	return (KERN_SUCCESS);
2300 }
2301 
2302 /*
2303  *	vm_map_remove:
2304  *
2305  *	Remove the given address range from the target map.
2306  *	This is the exported form of vm_map_delete.
2307  */
2308 int
2309 vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
2310 {
2311 	int result;
2312 
2313 	vm_map_lock(map);
2314 	VM_MAP_RANGE_CHECK(map, start, end);
2315 	result = vm_map_delete(map, start, end);
2316 	vm_map_unlock(map);
2317 	return (result);
2318 }
2319 
2320 /*
2321  *	vm_map_check_protection:
2322  *
2323  *	Assert that the target map allows the specified privilege on the
2324  *	entire address region given.  The entire region must be allocated.
2325  *
2326  *	WARNING!  This code does not and should not check whether the
2327  *	contents of the region is accessible.  For example a smaller file
2328  *	might be mapped into a larger address space.
2329  *
2330  *	NOTE!  This code is also called by munmap().
2331  *
2332  *	The map must be locked.  A read lock is sufficient.
2333  */
2334 boolean_t
2335 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
2336 			vm_prot_t protection)
2337 {
2338 	vm_map_entry_t entry;
2339 	vm_map_entry_t tmp_entry;
2340 
2341 	if (!vm_map_lookup_entry(map, start, &tmp_entry))
2342 		return (FALSE);
2343 	entry = tmp_entry;
2344 
2345 	while (start < end) {
2346 		if (entry == &map->header)
2347 			return (FALSE);
2348 		/*
2349 		 * No holes allowed!
2350 		 */
2351 		if (start < entry->start)
2352 			return (FALSE);
2353 		/*
2354 		 * Check protection associated with entry.
2355 		 */
2356 		if ((entry->protection & protection) != protection)
2357 			return (FALSE);
2358 		/* go to next entry */
2359 		start = entry->end;
2360 		entry = entry->next;
2361 	}
2362 	return (TRUE);
2363 }
2364 
2365 /*
2366  *	vm_map_copy_entry:
2367  *
2368  *	Copies the contents of the source entry to the destination
2369  *	entry.  The entries *must* be aligned properly.
2370  */
2371 static void
2372 vm_map_copy_entry(
2373 	vm_map_t src_map,
2374 	vm_map_t dst_map,
2375 	vm_map_entry_t src_entry,
2376 	vm_map_entry_t dst_entry)
2377 {
2378 	vm_object_t src_object;
2379 
2380 	if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
2381 		return;
2382 
2383 	if (src_entry->wired_count == 0) {
2384 
2385 		/*
2386 		 * If the source entry is marked needs_copy, it is already
2387 		 * write-protected.
2388 		 */
2389 		if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
2390 			pmap_protect(src_map->pmap,
2391 			    src_entry->start,
2392 			    src_entry->end,
2393 			    src_entry->protection & ~VM_PROT_WRITE);
2394 		}
2395 
2396 		/*
2397 		 * Make a copy of the object.
2398 		 */
2399 		if ((src_object = src_entry->object.vm_object) != NULL) {
2400 			VM_OBJECT_LOCK(src_object);
2401 			if ((src_object->handle == NULL) &&
2402 				(src_object->type == OBJT_DEFAULT ||
2403 				 src_object->type == OBJT_SWAP)) {
2404 				vm_object_collapse(src_object);
2405 				if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
2406 					vm_object_split(src_entry);
2407 					src_object = src_entry->object.vm_object;
2408 				}
2409 			}
2410 			vm_object_reference_locked(src_object);
2411 			vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
2412 			VM_OBJECT_UNLOCK(src_object);
2413 			dst_entry->object.vm_object = src_object;
2414 			src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2415 			dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2416 			dst_entry->offset = src_entry->offset;
2417 		} else {
2418 			dst_entry->object.vm_object = NULL;
2419 			dst_entry->offset = 0;
2420 		}
2421 
2422 		pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
2423 		    dst_entry->end - dst_entry->start, src_entry->start);
2424 	} else {
2425 		/*
2426 		 * Of course, wired down pages can't be set copy-on-write.
2427 		 * Cause wired pages to be copied into the new map by
2428 		 * simulating faults (the new pages are pageable)
2429 		 */
2430 		vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry);
2431 	}
2432 }
2433 
2434 /*
2435  * vmspace_map_entry_forked:
2436  * Update the newly-forked vmspace each time a map entry is inherited
2437  * or copied.  The values for vm_dsize and vm_tsize are approximate
2438  * (and mostly-obsolete ideas in the face of mmap(2) et al.)
2439  */
2440 static void
2441 vmspace_map_entry_forked(const struct vmspace *vm1, struct vmspace *vm2,
2442     vm_map_entry_t entry)
2443 {
2444 	vm_size_t entrysize;
2445 	vm_offset_t newend;
2446 
2447 	entrysize = entry->end - entry->start;
2448 	vm2->vm_map.size += entrysize;
2449 	if (entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP)) {
2450 		vm2->vm_ssize += btoc(entrysize);
2451 	} else if (entry->start >= (vm_offset_t)vm1->vm_daddr &&
2452 	    entry->start < (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)) {
2453 		newend = MIN(entry->end,
2454 		    (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize));
2455 		vm2->vm_dsize += btoc(newend - entry->start);
2456 	} else if (entry->start >= (vm_offset_t)vm1->vm_taddr &&
2457 	    entry->start < (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)) {
2458 		newend = MIN(entry->end,
2459 		    (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize));
2460 		vm2->vm_tsize += btoc(newend - entry->start);
2461 	}
2462 }
2463 
2464 /*
2465  * vmspace_fork:
2466  * Create a new process vmspace structure and vm_map
2467  * based on those of an existing process.  The new map
2468  * is based on the old map, according to the inheritance
2469  * values on the regions in that map.
2470  *
2471  * XXX It might be worth coalescing the entries added to the new vmspace.
2472  *
2473  * The source map must not be locked.
2474  */
2475 struct vmspace *
2476 vmspace_fork(struct vmspace *vm1)
2477 {
2478 	struct vmspace *vm2;
2479 	vm_map_t old_map = &vm1->vm_map;
2480 	vm_map_t new_map;
2481 	vm_map_entry_t old_entry;
2482 	vm_map_entry_t new_entry;
2483 	vm_object_t object;
2484 
2485 	vm_map_lock(old_map);
2486 
2487 	vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
2488 	vm2->vm_taddr = vm1->vm_taddr;
2489 	vm2->vm_daddr = vm1->vm_daddr;
2490 	vm2->vm_maxsaddr = vm1->vm_maxsaddr;
2491 	new_map = &vm2->vm_map;	/* XXX */
2492 	new_map->timestamp = 1;
2493 
2494 	/* Do not inherit the MAP_WIREFUTURE property. */
2495 	if ((new_map->flags & MAP_WIREFUTURE) == MAP_WIREFUTURE)
2496 		new_map->flags &= ~MAP_WIREFUTURE;
2497 
2498 	old_entry = old_map->header.next;
2499 
2500 	while (old_entry != &old_map->header) {
2501 		if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2502 			panic("vm_map_fork: encountered a submap");
2503 
2504 		switch (old_entry->inheritance) {
2505 		case VM_INHERIT_NONE:
2506 			break;
2507 
2508 		case VM_INHERIT_SHARE:
2509 			/*
2510 			 * Clone the entry, creating the shared object if necessary.
2511 			 */
2512 			object = old_entry->object.vm_object;
2513 			if (object == NULL) {
2514 				object = vm_object_allocate(OBJT_DEFAULT,
2515 					atop(old_entry->end - old_entry->start));
2516 				old_entry->object.vm_object = object;
2517 				old_entry->offset = 0;
2518 			}
2519 
2520 			/*
2521 			 * Add the reference before calling vm_object_shadow
2522 			 * to insure that a shadow object is created.
2523 			 */
2524 			vm_object_reference(object);
2525 			if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
2526 				vm_object_shadow(&old_entry->object.vm_object,
2527 					&old_entry->offset,
2528 					atop(old_entry->end - old_entry->start));
2529 				old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
2530 				/* Transfer the second reference too. */
2531 				vm_object_reference(
2532 				    old_entry->object.vm_object);
2533 				vm_object_deallocate(object);
2534 				object = old_entry->object.vm_object;
2535 			}
2536 			VM_OBJECT_LOCK(object);
2537 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
2538 			VM_OBJECT_UNLOCK(object);
2539 
2540 			/*
2541 			 * Clone the entry, referencing the shared object.
2542 			 */
2543 			new_entry = vm_map_entry_create(new_map);
2544 			*new_entry = *old_entry;
2545 			new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2546 			new_entry->wired_count = 0;
2547 
2548 			/*
2549 			 * Insert the entry into the new map -- we know we're
2550 			 * inserting at the end of the new map.
2551 			 */
2552 			vm_map_entry_link(new_map, new_map->header.prev,
2553 			    new_entry);
2554 			vmspace_map_entry_forked(vm1, vm2, new_entry);
2555 
2556 			/*
2557 			 * Update the physical map
2558 			 */
2559 			pmap_copy(new_map->pmap, old_map->pmap,
2560 			    new_entry->start,
2561 			    (old_entry->end - old_entry->start),
2562 			    old_entry->start);
2563 			break;
2564 
2565 		case VM_INHERIT_COPY:
2566 			/*
2567 			 * Clone the entry and link into the map.
2568 			 */
2569 			new_entry = vm_map_entry_create(new_map);
2570 			*new_entry = *old_entry;
2571 			new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2572 			new_entry->wired_count = 0;
2573 			new_entry->object.vm_object = NULL;
2574 			vm_map_entry_link(new_map, new_map->header.prev,
2575 			    new_entry);
2576 			vmspace_map_entry_forked(vm1, vm2, new_entry);
2577 			vm_map_copy_entry(old_map, new_map, old_entry,
2578 			    new_entry);
2579 			break;
2580 		}
2581 		old_entry = old_entry->next;
2582 	}
2583 
2584 	vm_map_unlock(old_map);
2585 
2586 	return (vm2);
2587 }
2588 
2589 int
2590 vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
2591     vm_prot_t prot, vm_prot_t max, int cow)
2592 {
2593 	vm_map_entry_t new_entry, prev_entry;
2594 	vm_offset_t bot, top;
2595 	vm_size_t init_ssize;
2596 	int orient, rv;
2597 	rlim_t vmemlim;
2598 
2599 	/*
2600 	 * The stack orientation is piggybacked with the cow argument.
2601 	 * Extract it into orient and mask the cow argument so that we
2602 	 * don't pass it around further.
2603 	 * NOTE: We explicitly allow bi-directional stacks.
2604 	 */
2605 	orient = cow & (MAP_STACK_GROWS_DOWN|MAP_STACK_GROWS_UP);
2606 	cow &= ~orient;
2607 	KASSERT(orient != 0, ("No stack grow direction"));
2608 
2609 	if (addrbos < vm_map_min(map) || addrbos > map->max_offset)
2610 		return (KERN_NO_SPACE);
2611 
2612 	init_ssize = (max_ssize < sgrowsiz) ? max_ssize : sgrowsiz;
2613 
2614 	PROC_LOCK(curthread->td_proc);
2615 	vmemlim = lim_cur(curthread->td_proc, RLIMIT_VMEM);
2616 	PROC_UNLOCK(curthread->td_proc);
2617 
2618 	vm_map_lock(map);
2619 
2620 	/* If addr is already mapped, no go */
2621 	if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
2622 		vm_map_unlock(map);
2623 		return (KERN_NO_SPACE);
2624 	}
2625 
2626 	/* If we would blow our VMEM resource limit, no go */
2627 	if (map->size + init_ssize > vmemlim) {
2628 		vm_map_unlock(map);
2629 		return (KERN_NO_SPACE);
2630 	}
2631 
2632 	/*
2633 	 * If we can't accomodate max_ssize in the current mapping, no go.
2634 	 * However, we need to be aware that subsequent user mappings might
2635 	 * map into the space we have reserved for stack, and currently this
2636 	 * space is not protected.
2637 	 *
2638 	 * Hopefully we will at least detect this condition when we try to
2639 	 * grow the stack.
2640 	 */
2641 	if ((prev_entry->next != &map->header) &&
2642 	    (prev_entry->next->start < addrbos + max_ssize)) {
2643 		vm_map_unlock(map);
2644 		return (KERN_NO_SPACE);
2645 	}
2646 
2647 	/*
2648 	 * We initially map a stack of only init_ssize.  We will grow as
2649 	 * needed later.  Depending on the orientation of the stack (i.e.
2650 	 * the grow direction) we either map at the top of the range, the
2651 	 * bottom of the range or in the middle.
2652 	 *
2653 	 * Note: we would normally expect prot and max to be VM_PROT_ALL,
2654 	 * and cow to be 0.  Possibly we should eliminate these as input
2655 	 * parameters, and just pass these values here in the insert call.
2656 	 */
2657 	if (orient == MAP_STACK_GROWS_DOWN)
2658 		bot = addrbos + max_ssize - init_ssize;
2659 	else if (orient == MAP_STACK_GROWS_UP)
2660 		bot = addrbos;
2661 	else
2662 		bot = round_page(addrbos + max_ssize/2 - init_ssize/2);
2663 	top = bot + init_ssize;
2664 	rv = vm_map_insert(map, NULL, 0, bot, top, prot, max, cow);
2665 
2666 	/* Now set the avail_ssize amount. */
2667 	if (rv == KERN_SUCCESS) {
2668 		if (prev_entry != &map->header)
2669 			vm_map_clip_end(map, prev_entry, bot);
2670 		new_entry = prev_entry->next;
2671 		if (new_entry->end != top || new_entry->start != bot)
2672 			panic("Bad entry start/end for new stack entry");
2673 
2674 		new_entry->avail_ssize = max_ssize - init_ssize;
2675 		if (orient & MAP_STACK_GROWS_DOWN)
2676 			new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
2677 		if (orient & MAP_STACK_GROWS_UP)
2678 			new_entry->eflags |= MAP_ENTRY_GROWS_UP;
2679 	}
2680 
2681 	vm_map_unlock(map);
2682 	return (rv);
2683 }
2684 
2685 /* Attempts to grow a vm stack entry.  Returns KERN_SUCCESS if the
2686  * desired address is already mapped, or if we successfully grow
2687  * the stack.  Also returns KERN_SUCCESS if addr is outside the
2688  * stack range (this is strange, but preserves compatibility with
2689  * the grow function in vm_machdep.c).
2690  */
2691 int
2692 vm_map_growstack(struct proc *p, vm_offset_t addr)
2693 {
2694 	vm_map_entry_t next_entry, prev_entry;
2695 	vm_map_entry_t new_entry, stack_entry;
2696 	struct vmspace *vm = p->p_vmspace;
2697 	vm_map_t map = &vm->vm_map;
2698 	vm_offset_t end;
2699 	size_t grow_amount, max_grow;
2700 	rlim_t stacklim, vmemlim;
2701 	int is_procstack, rv;
2702 
2703 Retry:
2704 	PROC_LOCK(p);
2705 	stacklim = lim_cur(p, RLIMIT_STACK);
2706 	vmemlim = lim_cur(p, RLIMIT_VMEM);
2707 	PROC_UNLOCK(p);
2708 
2709 	vm_map_lock_read(map);
2710 
2711 	/* If addr is already in the entry range, no need to grow.*/
2712 	if (vm_map_lookup_entry(map, addr, &prev_entry)) {
2713 		vm_map_unlock_read(map);
2714 		return (KERN_SUCCESS);
2715 	}
2716 
2717 	next_entry = prev_entry->next;
2718 	if (!(prev_entry->eflags & MAP_ENTRY_GROWS_UP)) {
2719 		/*
2720 		 * This entry does not grow upwards. Since the address lies
2721 		 * beyond this entry, the next entry (if one exists) has to
2722 		 * be a downward growable entry. The entry list header is
2723 		 * never a growable entry, so it suffices to check the flags.
2724 		 */
2725 		if (!(next_entry->eflags & MAP_ENTRY_GROWS_DOWN)) {
2726 			vm_map_unlock_read(map);
2727 			return (KERN_SUCCESS);
2728 		}
2729 		stack_entry = next_entry;
2730 	} else {
2731 		/*
2732 		 * This entry grows upward. If the next entry does not at
2733 		 * least grow downwards, this is the entry we need to grow.
2734 		 * otherwise we have two possible choices and we have to
2735 		 * select one.
2736 		 */
2737 		if (next_entry->eflags & MAP_ENTRY_GROWS_DOWN) {
2738 			/*
2739 			 * We have two choices; grow the entry closest to
2740 			 * the address to minimize the amount of growth.
2741 			 */
2742 			if (addr - prev_entry->end <= next_entry->start - addr)
2743 				stack_entry = prev_entry;
2744 			else
2745 				stack_entry = next_entry;
2746 		} else
2747 			stack_entry = prev_entry;
2748 	}
2749 
2750 	if (stack_entry == next_entry) {
2751 		KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_DOWN, ("foo"));
2752 		KASSERT(addr < stack_entry->start, ("foo"));
2753 		end = (prev_entry != &map->header) ? prev_entry->end :
2754 		    stack_entry->start - stack_entry->avail_ssize;
2755 		grow_amount = roundup(stack_entry->start - addr, PAGE_SIZE);
2756 		max_grow = stack_entry->start - end;
2757 	} else {
2758 		KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_UP, ("foo"));
2759 		KASSERT(addr >= stack_entry->end, ("foo"));
2760 		end = (next_entry != &map->header) ? next_entry->start :
2761 		    stack_entry->end + stack_entry->avail_ssize;
2762 		grow_amount = roundup(addr + 1 - stack_entry->end, PAGE_SIZE);
2763 		max_grow = end - stack_entry->end;
2764 	}
2765 
2766 	if (grow_amount > stack_entry->avail_ssize) {
2767 		vm_map_unlock_read(map);
2768 		return (KERN_NO_SPACE);
2769 	}
2770 
2771 	/*
2772 	 * If there is no longer enough space between the entries nogo, and
2773 	 * adjust the available space.  Note: this  should only happen if the
2774 	 * user has mapped into the stack area after the stack was created,
2775 	 * and is probably an error.
2776 	 *
2777 	 * This also effectively destroys any guard page the user might have
2778 	 * intended by limiting the stack size.
2779 	 */
2780 	if (grow_amount > max_grow) {
2781 		if (vm_map_lock_upgrade(map))
2782 			goto Retry;
2783 
2784 		stack_entry->avail_ssize = max_grow;
2785 
2786 		vm_map_unlock(map);
2787 		return (KERN_NO_SPACE);
2788 	}
2789 
2790 	is_procstack = (addr >= (vm_offset_t)vm->vm_maxsaddr) ? 1 : 0;
2791 
2792 	/*
2793 	 * If this is the main process stack, see if we're over the stack
2794 	 * limit.
2795 	 */
2796 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
2797 		vm_map_unlock_read(map);
2798 		return (KERN_NO_SPACE);
2799 	}
2800 
2801 	/* Round up the grow amount modulo SGROWSIZ */
2802 	grow_amount = roundup (grow_amount, sgrowsiz);
2803 	if (grow_amount > stack_entry->avail_ssize)
2804 		grow_amount = stack_entry->avail_ssize;
2805 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
2806 		grow_amount = stacklim - ctob(vm->vm_ssize);
2807 	}
2808 
2809 	/* If we would blow our VMEM resource limit, no go */
2810 	if (map->size + grow_amount > vmemlim) {
2811 		vm_map_unlock_read(map);
2812 		return (KERN_NO_SPACE);
2813 	}
2814 
2815 	if (vm_map_lock_upgrade(map))
2816 		goto Retry;
2817 
2818 	if (stack_entry == next_entry) {
2819 		/*
2820 		 * Growing downward.
2821 		 */
2822 		/* Get the preliminary new entry start value */
2823 		addr = stack_entry->start - grow_amount;
2824 
2825 		/*
2826 		 * If this puts us into the previous entry, cut back our
2827 		 * growth to the available space. Also, see the note above.
2828 		 */
2829 		if (addr < end) {
2830 			stack_entry->avail_ssize = max_grow;
2831 			addr = end;
2832 		}
2833 
2834 		rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start,
2835 		    p->p_sysent->sv_stackprot, VM_PROT_ALL, 0);
2836 
2837 		/* Adjust the available stack space by the amount we grew. */
2838 		if (rv == KERN_SUCCESS) {
2839 			if (prev_entry != &map->header)
2840 				vm_map_clip_end(map, prev_entry, addr);
2841 			new_entry = prev_entry->next;
2842 			KASSERT(new_entry == stack_entry->prev, ("foo"));
2843 			KASSERT(new_entry->end == stack_entry->start, ("foo"));
2844 			KASSERT(new_entry->start == addr, ("foo"));
2845 			grow_amount = new_entry->end - new_entry->start;
2846 			new_entry->avail_ssize = stack_entry->avail_ssize -
2847 			    grow_amount;
2848 			stack_entry->eflags &= ~MAP_ENTRY_GROWS_DOWN;
2849 			new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
2850 		}
2851 	} else {
2852 		/*
2853 		 * Growing upward.
2854 		 */
2855 		addr = stack_entry->end + grow_amount;
2856 
2857 		/*
2858 		 * If this puts us into the next entry, cut back our growth
2859 		 * to the available space. Also, see the note above.
2860 		 */
2861 		if (addr > end) {
2862 			stack_entry->avail_ssize = end - stack_entry->end;
2863 			addr = end;
2864 		}
2865 
2866 		grow_amount = addr - stack_entry->end;
2867 
2868 		/* Grow the underlying object if applicable. */
2869 		if (stack_entry->object.vm_object == NULL ||
2870 		    vm_object_coalesce(stack_entry->object.vm_object,
2871 		    stack_entry->offset,
2872 		    (vm_size_t)(stack_entry->end - stack_entry->start),
2873 		    (vm_size_t)grow_amount)) {
2874 			map->size += (addr - stack_entry->end);
2875 			/* Update the current entry. */
2876 			stack_entry->end = addr;
2877 			stack_entry->avail_ssize -= grow_amount;
2878 			vm_map_entry_resize_free(map, stack_entry);
2879 			rv = KERN_SUCCESS;
2880 
2881 			if (next_entry != &map->header)
2882 				vm_map_clip_start(map, next_entry, addr);
2883 		} else
2884 			rv = KERN_FAILURE;
2885 	}
2886 
2887 	if (rv == KERN_SUCCESS && is_procstack)
2888 		vm->vm_ssize += btoc(grow_amount);
2889 
2890 	vm_map_unlock(map);
2891 
2892 	/*
2893 	 * Heed the MAP_WIREFUTURE flag if it was set for this process.
2894 	 */
2895 	if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE)) {
2896 		vm_map_wire(map,
2897 		    (stack_entry == next_entry) ? addr : addr - grow_amount,
2898 		    (stack_entry == next_entry) ? stack_entry->start : addr,
2899 		    (p->p_flag & P_SYSTEM)
2900 		    ? VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES
2901 		    : VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
2902 	}
2903 
2904 	return (rv);
2905 }
2906 
2907 /*
2908  * Unshare the specified VM space for exec.  If other processes are
2909  * mapped to it, then create a new one.  The new vmspace is null.
2910  */
2911 void
2912 vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser)
2913 {
2914 	struct vmspace *oldvmspace = p->p_vmspace;
2915 	struct vmspace *newvmspace;
2916 
2917 	newvmspace = vmspace_alloc(minuser, maxuser);
2918 	newvmspace->vm_swrss = oldvmspace->vm_swrss;
2919 	/*
2920 	 * This code is written like this for prototype purposes.  The
2921 	 * goal is to avoid running down the vmspace here, but let the
2922 	 * other process's that are still using the vmspace to finally
2923 	 * run it down.  Even though there is little or no chance of blocking
2924 	 * here, it is a good idea to keep this form for future mods.
2925 	 */
2926 	p->p_vmspace = newvmspace;
2927 	if (p == curthread->td_proc)		/* XXXKSE ? */
2928 		pmap_activate(curthread);
2929 	vmspace_free(oldvmspace);
2930 }
2931 
2932 /*
2933  * Unshare the specified VM space for forcing COW.  This
2934  * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
2935  */
2936 void
2937 vmspace_unshare(struct proc *p)
2938 {
2939 	struct vmspace *oldvmspace = p->p_vmspace;
2940 	struct vmspace *newvmspace;
2941 
2942 	if (oldvmspace->vm_refcnt == 1)
2943 		return;
2944 	newvmspace = vmspace_fork(oldvmspace);
2945 	p->p_vmspace = newvmspace;
2946 	if (p == curthread->td_proc)		/* XXXKSE ? */
2947 		pmap_activate(curthread);
2948 	vmspace_free(oldvmspace);
2949 }
2950 
2951 /*
2952  *	vm_map_lookup:
2953  *
2954  *	Finds the VM object, offset, and
2955  *	protection for a given virtual address in the
2956  *	specified map, assuming a page fault of the
2957  *	type specified.
2958  *
2959  *	Leaves the map in question locked for read; return
2960  *	values are guaranteed until a vm_map_lookup_done
2961  *	call is performed.  Note that the map argument
2962  *	is in/out; the returned map must be used in
2963  *	the call to vm_map_lookup_done.
2964  *
2965  *	A handle (out_entry) is returned for use in
2966  *	vm_map_lookup_done, to make that fast.
2967  *
2968  *	If a lookup is requested with "write protection"
2969  *	specified, the map may be changed to perform virtual
2970  *	copying operations, although the data referenced will
2971  *	remain the same.
2972  */
2973 int
2974 vm_map_lookup(vm_map_t *var_map,		/* IN/OUT */
2975 	      vm_offset_t vaddr,
2976 	      vm_prot_t fault_typea,
2977 	      vm_map_entry_t *out_entry,	/* OUT */
2978 	      vm_object_t *object,		/* OUT */
2979 	      vm_pindex_t *pindex,		/* OUT */
2980 	      vm_prot_t *out_prot,		/* OUT */
2981 	      boolean_t *wired)			/* OUT */
2982 {
2983 	vm_map_entry_t entry;
2984 	vm_map_t map = *var_map;
2985 	vm_prot_t prot;
2986 	vm_prot_t fault_type = fault_typea;
2987 
2988 RetryLookup:;
2989 	/*
2990 	 * Lookup the faulting address.
2991 	 */
2992 
2993 	vm_map_lock_read(map);
2994 #define	RETURN(why) \
2995 		{ \
2996 		vm_map_unlock_read(map); \
2997 		return (why); \
2998 		}
2999 
3000 	/*
3001 	 * If the map has an interesting hint, try it before calling full
3002 	 * blown lookup routine.
3003 	 */
3004 	entry = map->root;
3005 	*out_entry = entry;
3006 	if (entry == NULL ||
3007 	    (vaddr < entry->start) || (vaddr >= entry->end)) {
3008 		/*
3009 		 * Entry was either not a valid hint, or the vaddr was not
3010 		 * contained in the entry, so do a full lookup.
3011 		 */
3012 		if (!vm_map_lookup_entry(map, vaddr, out_entry))
3013 			RETURN(KERN_INVALID_ADDRESS);
3014 
3015 		entry = *out_entry;
3016 	}
3017 
3018 	/*
3019 	 * Handle submaps.
3020 	 */
3021 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3022 		vm_map_t old_map = map;
3023 
3024 		*var_map = map = entry->object.sub_map;
3025 		vm_map_unlock_read(old_map);
3026 		goto RetryLookup;
3027 	}
3028 
3029 	/*
3030 	 * Check whether this task is allowed to have this page.
3031 	 * Note the special case for MAP_ENTRY_COW
3032 	 * pages with an override.  This is to implement a forced
3033 	 * COW for debuggers.
3034 	 */
3035 	if (fault_type & VM_PROT_OVERRIDE_WRITE)
3036 		prot = entry->max_protection;
3037 	else
3038 		prot = entry->protection;
3039 	fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
3040 	if ((fault_type & prot) != fault_type) {
3041 			RETURN(KERN_PROTECTION_FAILURE);
3042 	}
3043 	if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
3044 	    (entry->eflags & MAP_ENTRY_COW) &&
3045 	    (fault_type & VM_PROT_WRITE) &&
3046 	    (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
3047 		RETURN(KERN_PROTECTION_FAILURE);
3048 	}
3049 
3050 	/*
3051 	 * If this page is not pageable, we have to get it for all possible
3052 	 * accesses.
3053 	 */
3054 	*wired = (entry->wired_count != 0);
3055 	if (*wired)
3056 		prot = fault_type = entry->protection;
3057 
3058 	/*
3059 	 * If the entry was copy-on-write, we either ...
3060 	 */
3061 	if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3062 		/*
3063 		 * If we want to write the page, we may as well handle that
3064 		 * now since we've got the map locked.
3065 		 *
3066 		 * If we don't need to write the page, we just demote the
3067 		 * permissions allowed.
3068 		 */
3069 		if (fault_type & VM_PROT_WRITE) {
3070 			/*
3071 			 * Make a new object, and place it in the object
3072 			 * chain.  Note that no new references have appeared
3073 			 * -- one just moved from the map to the new
3074 			 * object.
3075 			 */
3076 			if (vm_map_lock_upgrade(map))
3077 				goto RetryLookup;
3078 
3079 			vm_object_shadow(
3080 			    &entry->object.vm_object,
3081 			    &entry->offset,
3082 			    atop(entry->end - entry->start));
3083 			entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
3084 
3085 			vm_map_lock_downgrade(map);
3086 		} else {
3087 			/*
3088 			 * We're attempting to read a copy-on-write page --
3089 			 * don't allow writes.
3090 			 */
3091 			prot &= ~VM_PROT_WRITE;
3092 		}
3093 	}
3094 
3095 	/*
3096 	 * Create an object if necessary.
3097 	 */
3098 	if (entry->object.vm_object == NULL &&
3099 	    !map->system_map) {
3100 		if (vm_map_lock_upgrade(map))
3101 			goto RetryLookup;
3102 		entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
3103 		    atop(entry->end - entry->start));
3104 		entry->offset = 0;
3105 		vm_map_lock_downgrade(map);
3106 	}
3107 
3108 	/*
3109 	 * Return the object/offset from this entry.  If the entry was
3110 	 * copy-on-write or empty, it has been fixed up.
3111 	 */
3112 	*pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
3113 	*object = entry->object.vm_object;
3114 
3115 	*out_prot = prot;
3116 	return (KERN_SUCCESS);
3117 
3118 #undef	RETURN
3119 }
3120 
3121 /*
3122  *	vm_map_lookup_locked:
3123  *
3124  *	Lookup the faulting address.  A version of vm_map_lookup that returns
3125  *      KERN_FAILURE instead of blocking on map lock or memory allocation.
3126  */
3127 int
3128 vm_map_lookup_locked(vm_map_t *var_map,		/* IN/OUT */
3129 		     vm_offset_t vaddr,
3130 		     vm_prot_t fault_typea,
3131 		     vm_map_entry_t *out_entry,	/* OUT */
3132 		     vm_object_t *object,	/* OUT */
3133 		     vm_pindex_t *pindex,	/* OUT */
3134 		     vm_prot_t *out_prot,	/* OUT */
3135 		     boolean_t *wired)		/* OUT */
3136 {
3137 	vm_map_entry_t entry;
3138 	vm_map_t map = *var_map;
3139 	vm_prot_t prot;
3140 	vm_prot_t fault_type = fault_typea;
3141 
3142 	/*
3143 	 * If the map has an interesting hint, try it before calling full
3144 	 * blown lookup routine.
3145 	 */
3146 	entry = map->root;
3147 	*out_entry = entry;
3148 	if (entry == NULL ||
3149 	    (vaddr < entry->start) || (vaddr >= entry->end)) {
3150 		/*
3151 		 * Entry was either not a valid hint, or the vaddr was not
3152 		 * contained in the entry, so do a full lookup.
3153 		 */
3154 		if (!vm_map_lookup_entry(map, vaddr, out_entry))
3155 			return (KERN_INVALID_ADDRESS);
3156 
3157 		entry = *out_entry;
3158 	}
3159 
3160 	/*
3161 	 * Fail if the entry refers to a submap.
3162 	 */
3163 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
3164 		return (KERN_FAILURE);
3165 
3166 	/*
3167 	 * Check whether this task is allowed to have this page.
3168 	 * Note the special case for MAP_ENTRY_COW
3169 	 * pages with an override.  This is to implement a forced
3170 	 * COW for debuggers.
3171 	 */
3172 	if (fault_type & VM_PROT_OVERRIDE_WRITE)
3173 		prot = entry->max_protection;
3174 	else
3175 		prot = entry->protection;
3176 	fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
3177 	if ((fault_type & prot) != fault_type)
3178 		return (KERN_PROTECTION_FAILURE);
3179 	if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
3180 	    (entry->eflags & MAP_ENTRY_COW) &&
3181 	    (fault_type & VM_PROT_WRITE) &&
3182 	    (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0)
3183 		return (KERN_PROTECTION_FAILURE);
3184 
3185 	/*
3186 	 * If this page is not pageable, we have to get it for all possible
3187 	 * accesses.
3188 	 */
3189 	*wired = (entry->wired_count != 0);
3190 	if (*wired)
3191 		prot = fault_type = entry->protection;
3192 
3193 	if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3194 		/*
3195 		 * Fail if the entry was copy-on-write for a write fault.
3196 		 */
3197 		if (fault_type & VM_PROT_WRITE)
3198 			return (KERN_FAILURE);
3199 		/*
3200 		 * We're attempting to read a copy-on-write page --
3201 		 * don't allow writes.
3202 		 */
3203 		prot &= ~VM_PROT_WRITE;
3204 	}
3205 
3206 	/*
3207 	 * Fail if an object should be created.
3208 	 */
3209 	if (entry->object.vm_object == NULL && !map->system_map)
3210 		return (KERN_FAILURE);
3211 
3212 	/*
3213 	 * Return the object/offset from this entry.  If the entry was
3214 	 * copy-on-write or empty, it has been fixed up.
3215 	 */
3216 	*pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
3217 	*object = entry->object.vm_object;
3218 
3219 	*out_prot = prot;
3220 	return (KERN_SUCCESS);
3221 }
3222 
3223 /*
3224  *	vm_map_lookup_done:
3225  *
3226  *	Releases locks acquired by a vm_map_lookup
3227  *	(according to the handle returned by that lookup).
3228  */
3229 void
3230 vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry)
3231 {
3232 	/*
3233 	 * Unlock the main-level map
3234 	 */
3235 	vm_map_unlock_read(map);
3236 }
3237 
3238 #include "opt_ddb.h"
3239 #ifdef DDB
3240 #include <sys/kernel.h>
3241 
3242 #include <ddb/ddb.h>
3243 
3244 /*
3245  *	vm_map_print:	[ debug ]
3246  */
3247 DB_SHOW_COMMAND(map, vm_map_print)
3248 {
3249 	static int nlines;
3250 	/* XXX convert args. */
3251 	vm_map_t map = (vm_map_t)addr;
3252 	boolean_t full = have_addr;
3253 
3254 	vm_map_entry_t entry;
3255 
3256 	db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
3257 	    (void *)map,
3258 	    (void *)map->pmap, map->nentries, map->timestamp);
3259 	nlines++;
3260 
3261 	if (!full && db_indent)
3262 		return;
3263 
3264 	db_indent += 2;
3265 	for (entry = map->header.next; entry != &map->header;
3266 	    entry = entry->next) {
3267 		db_iprintf("map entry %p: start=%p, end=%p\n",
3268 		    (void *)entry, (void *)entry->start, (void *)entry->end);
3269 		nlines++;
3270 		{
3271 			static char *inheritance_name[4] =
3272 			{"share", "copy", "none", "donate_copy"};
3273 
3274 			db_iprintf(" prot=%x/%x/%s",
3275 			    entry->protection,
3276 			    entry->max_protection,
3277 			    inheritance_name[(int)(unsigned char)entry->inheritance]);
3278 			if (entry->wired_count != 0)
3279 				db_printf(", wired");
3280 		}
3281 		if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3282 			db_printf(", share=%p, offset=0x%jx\n",
3283 			    (void *)entry->object.sub_map,
3284 			    (uintmax_t)entry->offset);
3285 			nlines++;
3286 			if ((entry->prev == &map->header) ||
3287 			    (entry->prev->object.sub_map !=
3288 				entry->object.sub_map)) {
3289 				db_indent += 2;
3290 				vm_map_print((db_expr_t)(intptr_t)
3291 					     entry->object.sub_map,
3292 					     full, 0, (char *)0);
3293 				db_indent -= 2;
3294 			}
3295 		} else {
3296 			db_printf(", object=%p, offset=0x%jx",
3297 			    (void *)entry->object.vm_object,
3298 			    (uintmax_t)entry->offset);
3299 			if (entry->eflags & MAP_ENTRY_COW)
3300 				db_printf(", copy (%s)",
3301 				    (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
3302 			db_printf("\n");
3303 			nlines++;
3304 
3305 			if ((entry->prev == &map->header) ||
3306 			    (entry->prev->object.vm_object !=
3307 				entry->object.vm_object)) {
3308 				db_indent += 2;
3309 				vm_object_print((db_expr_t)(intptr_t)
3310 						entry->object.vm_object,
3311 						full, 0, (char *)0);
3312 				nlines += 4;
3313 				db_indent -= 2;
3314 			}
3315 		}
3316 	}
3317 	db_indent -= 2;
3318 	if (db_indent == 0)
3319 		nlines = 0;
3320 }
3321 
3322 
3323 DB_SHOW_COMMAND(procvm, procvm)
3324 {
3325 	struct proc *p;
3326 
3327 	if (have_addr) {
3328 		p = (struct proc *) addr;
3329 	} else {
3330 		p = curproc;
3331 	}
3332 
3333 	db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
3334 	    (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
3335 	    (void *)vmspace_pmap(p->p_vmspace));
3336 
3337 	vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);
3338 }
3339 
3340 #endif /* DDB */
3341