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