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