xref: /freebsd/sys/vm/vm_map.c (revision d8b878873e7aa8df1972cc6a642804b17eb61087)
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 			switch (find_space) {
1398 			case VMFS_ALIGNED_SPACE:
1399 				pmap_align_superpage(object, offset, addr,
1400 				    length);
1401 				break;
1402 #ifdef VMFS_TLB_ALIGNED_SPACE
1403 			case VMFS_TLB_ALIGNED_SPACE:
1404 				pmap_align_tlb(addr);
1405 				break;
1406 #endif
1407 			default:
1408 				break;
1409 			}
1410 
1411 			start = *addr;
1412 		}
1413 		result = vm_map_insert(map, object, offset, start, start +
1414 		    length, prot, max, cow);
1415 	} while (result == KERN_NO_SPACE && find_space == VMFS_ALIGNED_SPACE);
1416 	vm_map_unlock(map);
1417 	return (result);
1418 }
1419 
1420 /*
1421  *	vm_map_simplify_entry:
1422  *
1423  *	Simplify the given map entry by merging with either neighbor.  This
1424  *	routine also has the ability to merge with both neighbors.
1425  *
1426  *	The map must be locked.
1427  *
1428  *	This routine guarentees that the passed entry remains valid (though
1429  *	possibly extended).  When merging, this routine may delete one or
1430  *	both neighbors.
1431  */
1432 void
1433 vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry)
1434 {
1435 	vm_map_entry_t next, prev;
1436 	vm_size_t prevsize, esize;
1437 
1438 	if (entry->eflags & (MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP))
1439 		return;
1440 
1441 	prev = entry->prev;
1442 	if (prev != &map->header) {
1443 		prevsize = prev->end - prev->start;
1444 		if ( (prev->end == entry->start) &&
1445 		     (prev->object.vm_object == entry->object.vm_object) &&
1446 		     (!prev->object.vm_object ||
1447 			(prev->offset + prevsize == entry->offset)) &&
1448 		     (prev->eflags == entry->eflags) &&
1449 		     (prev->protection == entry->protection) &&
1450 		     (prev->max_protection == entry->max_protection) &&
1451 		     (prev->inheritance == entry->inheritance) &&
1452 		     (prev->wired_count == entry->wired_count) &&
1453 		     (prev->uip == entry->uip)) {
1454 			vm_map_entry_unlink(map, prev);
1455 			entry->start = prev->start;
1456 			entry->offset = prev->offset;
1457 			if (entry->prev != &map->header)
1458 				vm_map_entry_resize_free(map, entry->prev);
1459 
1460 			/*
1461 			 * If the backing object is a vnode object,
1462 			 * vm_object_deallocate() calls vrele().
1463 			 * However, vrele() does not lock the vnode
1464 			 * because the vnode has additional
1465 			 * references.  Thus, the map lock can be kept
1466 			 * without causing a lock-order reversal with
1467 			 * the vnode lock.
1468 			 */
1469 			if (prev->object.vm_object)
1470 				vm_object_deallocate(prev->object.vm_object);
1471 			if (prev->uip != NULL)
1472 				uifree(prev->uip);
1473 			vm_map_entry_dispose(map, prev);
1474 		}
1475 	}
1476 
1477 	next = entry->next;
1478 	if (next != &map->header) {
1479 		esize = entry->end - entry->start;
1480 		if ((entry->end == next->start) &&
1481 		    (next->object.vm_object == entry->object.vm_object) &&
1482 		     (!entry->object.vm_object ||
1483 			(entry->offset + esize == next->offset)) &&
1484 		    (next->eflags == entry->eflags) &&
1485 		    (next->protection == entry->protection) &&
1486 		    (next->max_protection == entry->max_protection) &&
1487 		    (next->inheritance == entry->inheritance) &&
1488 		    (next->wired_count == entry->wired_count) &&
1489 		    (next->uip == entry->uip)) {
1490 			vm_map_entry_unlink(map, next);
1491 			entry->end = next->end;
1492 			vm_map_entry_resize_free(map, entry);
1493 
1494 			/*
1495 			 * See comment above.
1496 			 */
1497 			if (next->object.vm_object)
1498 				vm_object_deallocate(next->object.vm_object);
1499 			if (next->uip != NULL)
1500 				uifree(next->uip);
1501 			vm_map_entry_dispose(map, next);
1502 		}
1503 	}
1504 }
1505 /*
1506  *	vm_map_clip_start:	[ internal use only ]
1507  *
1508  *	Asserts that the given entry begins at or after
1509  *	the specified address; if necessary,
1510  *	it splits the entry into two.
1511  */
1512 #define vm_map_clip_start(map, entry, startaddr) \
1513 { \
1514 	if (startaddr > entry->start) \
1515 		_vm_map_clip_start(map, entry, startaddr); \
1516 }
1517 
1518 /*
1519  *	This routine is called only when it is known that
1520  *	the entry must be split.
1521  */
1522 static void
1523 _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start)
1524 {
1525 	vm_map_entry_t new_entry;
1526 
1527 	VM_MAP_ASSERT_LOCKED(map);
1528 
1529 	/*
1530 	 * Split off the front portion -- note that we must insert the new
1531 	 * entry BEFORE this one, so that this entry has the specified
1532 	 * starting address.
1533 	 */
1534 	vm_map_simplify_entry(map, entry);
1535 
1536 	/*
1537 	 * If there is no object backing this entry, we might as well create
1538 	 * one now.  If we defer it, an object can get created after the map
1539 	 * is clipped, and individual objects will be created for the split-up
1540 	 * map.  This is a bit of a hack, but is also about the best place to
1541 	 * put this improvement.
1542 	 */
1543 	if (entry->object.vm_object == NULL && !map->system_map) {
1544 		vm_object_t object;
1545 		object = vm_object_allocate(OBJT_DEFAULT,
1546 				atop(entry->end - entry->start));
1547 		entry->object.vm_object = object;
1548 		entry->offset = 0;
1549 		if (entry->uip != NULL) {
1550 			object->uip = entry->uip;
1551 			object->charge = entry->end - entry->start;
1552 			entry->uip = NULL;
1553 		}
1554 	} else if (entry->object.vm_object != NULL &&
1555 		   ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) &&
1556 		   entry->uip != NULL) {
1557 		VM_OBJECT_LOCK(entry->object.vm_object);
1558 		KASSERT(entry->object.vm_object->uip == NULL,
1559 		    ("OVERCOMMIT: vm_entry_clip_start: both uip e %p", entry));
1560 		entry->object.vm_object->uip = entry->uip;
1561 		entry->object.vm_object->charge = entry->end - entry->start;
1562 		VM_OBJECT_UNLOCK(entry->object.vm_object);
1563 		entry->uip = NULL;
1564 	}
1565 
1566 	new_entry = vm_map_entry_create(map);
1567 	*new_entry = *entry;
1568 
1569 	new_entry->end = start;
1570 	entry->offset += (start - entry->start);
1571 	entry->start = start;
1572 	if (new_entry->uip != NULL)
1573 		uihold(entry->uip);
1574 
1575 	vm_map_entry_link(map, entry->prev, new_entry);
1576 
1577 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1578 		vm_object_reference(new_entry->object.vm_object);
1579 	}
1580 }
1581 
1582 /*
1583  *	vm_map_clip_end:	[ internal use only ]
1584  *
1585  *	Asserts that the given entry ends at or before
1586  *	the specified address; if necessary,
1587  *	it splits the entry into two.
1588  */
1589 #define vm_map_clip_end(map, entry, endaddr) \
1590 { \
1591 	if ((endaddr) < (entry->end)) \
1592 		_vm_map_clip_end((map), (entry), (endaddr)); \
1593 }
1594 
1595 /*
1596  *	This routine is called only when it is known that
1597  *	the entry must be split.
1598  */
1599 static void
1600 _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end)
1601 {
1602 	vm_map_entry_t new_entry;
1603 
1604 	VM_MAP_ASSERT_LOCKED(map);
1605 
1606 	/*
1607 	 * If there is no object backing this entry, we might as well create
1608 	 * one now.  If we defer it, an object can get created after the map
1609 	 * is clipped, and individual objects will be created for the split-up
1610 	 * map.  This is a bit of a hack, but is also about the best place to
1611 	 * put this improvement.
1612 	 */
1613 	if (entry->object.vm_object == NULL && !map->system_map) {
1614 		vm_object_t object;
1615 		object = vm_object_allocate(OBJT_DEFAULT,
1616 				atop(entry->end - entry->start));
1617 		entry->object.vm_object = object;
1618 		entry->offset = 0;
1619 		if (entry->uip != NULL) {
1620 			object->uip = entry->uip;
1621 			object->charge = entry->end - entry->start;
1622 			entry->uip = NULL;
1623 		}
1624 	} else if (entry->object.vm_object != NULL &&
1625 		   ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) &&
1626 		   entry->uip != NULL) {
1627 		VM_OBJECT_LOCK(entry->object.vm_object);
1628 		KASSERT(entry->object.vm_object->uip == NULL,
1629 		    ("OVERCOMMIT: vm_entry_clip_end: both uip e %p", entry));
1630 		entry->object.vm_object->uip = entry->uip;
1631 		entry->object.vm_object->charge = entry->end - entry->start;
1632 		VM_OBJECT_UNLOCK(entry->object.vm_object);
1633 		entry->uip = NULL;
1634 	}
1635 
1636 	/*
1637 	 * Create a new entry and insert it AFTER the specified entry
1638 	 */
1639 	new_entry = vm_map_entry_create(map);
1640 	*new_entry = *entry;
1641 
1642 	new_entry->start = entry->end = end;
1643 	new_entry->offset += (end - entry->start);
1644 	if (new_entry->uip != NULL)
1645 		uihold(entry->uip);
1646 
1647 	vm_map_entry_link(map, entry, new_entry);
1648 
1649 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1650 		vm_object_reference(new_entry->object.vm_object);
1651 	}
1652 }
1653 
1654 /*
1655  *	vm_map_submap:		[ kernel use only ]
1656  *
1657  *	Mark the given range as handled by a subordinate map.
1658  *
1659  *	This range must have been created with vm_map_find,
1660  *	and no other operations may have been performed on this
1661  *	range prior to calling vm_map_submap.
1662  *
1663  *	Only a limited number of operations can be performed
1664  *	within this rage after calling vm_map_submap:
1665  *		vm_fault
1666  *	[Don't try vm_map_copy!]
1667  *
1668  *	To remove a submapping, one must first remove the
1669  *	range from the superior map, and then destroy the
1670  *	submap (if desired).  [Better yet, don't try it.]
1671  */
1672 int
1673 vm_map_submap(
1674 	vm_map_t map,
1675 	vm_offset_t start,
1676 	vm_offset_t end,
1677 	vm_map_t submap)
1678 {
1679 	vm_map_entry_t entry;
1680 	int result = KERN_INVALID_ARGUMENT;
1681 
1682 	vm_map_lock(map);
1683 
1684 	VM_MAP_RANGE_CHECK(map, start, end);
1685 
1686 	if (vm_map_lookup_entry(map, start, &entry)) {
1687 		vm_map_clip_start(map, entry, start);
1688 	} else
1689 		entry = entry->next;
1690 
1691 	vm_map_clip_end(map, entry, end);
1692 
1693 	if ((entry->start == start) && (entry->end == end) &&
1694 	    ((entry->eflags & MAP_ENTRY_COW) == 0) &&
1695 	    (entry->object.vm_object == NULL)) {
1696 		entry->object.sub_map = submap;
1697 		entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
1698 		result = KERN_SUCCESS;
1699 	}
1700 	vm_map_unlock(map);
1701 
1702 	return (result);
1703 }
1704 
1705 /*
1706  * The maximum number of pages to map
1707  */
1708 #define	MAX_INIT_PT	96
1709 
1710 /*
1711  *	vm_map_pmap_enter:
1712  *
1713  *	Preload read-only mappings for the given object's resident pages into
1714  *	the given map.  This eliminates the soft faults on process startup and
1715  *	immediately after an mmap(2).  Because these are speculative mappings,
1716  *	cached pages are not reactivated and mapped.
1717  */
1718 void
1719 vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
1720     vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags)
1721 {
1722 	vm_offset_t start;
1723 	vm_page_t p, p_start;
1724 	vm_pindex_t psize, tmpidx;
1725 	boolean_t are_queues_locked;
1726 
1727 	if ((prot & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 || object == NULL)
1728 		return;
1729 	VM_OBJECT_LOCK(object);
1730 	if (object->type == OBJT_DEVICE || object->type == OBJT_SG) {
1731 		pmap_object_init_pt(map->pmap, addr, object, pindex, size);
1732 		goto unlock_return;
1733 	}
1734 
1735 	psize = atop(size);
1736 
1737 	if ((flags & MAP_PREFAULT_PARTIAL) && psize > MAX_INIT_PT &&
1738 	    object->resident_page_count > MAX_INIT_PT)
1739 		goto unlock_return;
1740 
1741 	if (psize + pindex > object->size) {
1742 		if (object->size < pindex)
1743 			goto unlock_return;
1744 		psize = object->size - pindex;
1745 	}
1746 
1747 	are_queues_locked = FALSE;
1748 	start = 0;
1749 	p_start = NULL;
1750 
1751 	if ((p = TAILQ_FIRST(&object->memq)) != NULL) {
1752 		if (p->pindex < pindex) {
1753 			p = vm_page_splay(pindex, object->root);
1754 			if ((object->root = p)->pindex < pindex)
1755 				p = TAILQ_NEXT(p, listq);
1756 		}
1757 	}
1758 	/*
1759 	 * Assert: the variable p is either (1) the page with the
1760 	 * least pindex greater than or equal to the parameter pindex
1761 	 * or (2) NULL.
1762 	 */
1763 	for (;
1764 	     p != NULL && (tmpidx = p->pindex - pindex) < psize;
1765 	     p = TAILQ_NEXT(p, listq)) {
1766 		/*
1767 		 * don't allow an madvise to blow away our really
1768 		 * free pages allocating pv entries.
1769 		 */
1770 		if ((flags & MAP_PREFAULT_MADVISE) &&
1771 		    cnt.v_free_count < cnt.v_free_reserved) {
1772 			psize = tmpidx;
1773 			break;
1774 		}
1775 		if (p->valid == VM_PAGE_BITS_ALL) {
1776 			if (p_start == NULL) {
1777 				start = addr + ptoa(tmpidx);
1778 				p_start = p;
1779 			}
1780 		} else if (p_start != NULL) {
1781 			if (!are_queues_locked) {
1782 				are_queues_locked = TRUE;
1783 				vm_page_lock_queues();
1784 			}
1785 			pmap_enter_object(map->pmap, start, addr +
1786 			    ptoa(tmpidx), p_start, prot);
1787 			p_start = NULL;
1788 		}
1789 	}
1790 	if (p_start != NULL) {
1791 		if (!are_queues_locked) {
1792 			are_queues_locked = TRUE;
1793 			vm_page_lock_queues();
1794 		}
1795 		pmap_enter_object(map->pmap, start, addr + ptoa(psize),
1796 		    p_start, prot);
1797 	}
1798 	if (are_queues_locked)
1799 		vm_page_unlock_queues();
1800 unlock_return:
1801 	VM_OBJECT_UNLOCK(object);
1802 }
1803 
1804 /*
1805  *	vm_map_protect:
1806  *
1807  *	Sets the protection of the specified address
1808  *	region in the target map.  If "set_max" is
1809  *	specified, the maximum protection is to be set;
1810  *	otherwise, only the current protection is affected.
1811  */
1812 int
1813 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
1814 	       vm_prot_t new_prot, boolean_t set_max)
1815 {
1816 	vm_map_entry_t current, entry;
1817 	vm_object_t obj;
1818 	struct uidinfo *uip;
1819 	vm_prot_t old_prot;
1820 
1821 	vm_map_lock(map);
1822 
1823 	VM_MAP_RANGE_CHECK(map, start, end);
1824 
1825 	if (vm_map_lookup_entry(map, start, &entry)) {
1826 		vm_map_clip_start(map, entry, start);
1827 	} else {
1828 		entry = entry->next;
1829 	}
1830 
1831 	/*
1832 	 * Make a first pass to check for protection violations.
1833 	 */
1834 	current = entry;
1835 	while ((current != &map->header) && (current->start < end)) {
1836 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1837 			vm_map_unlock(map);
1838 			return (KERN_INVALID_ARGUMENT);
1839 		}
1840 		if ((new_prot & current->max_protection) != new_prot) {
1841 			vm_map_unlock(map);
1842 			return (KERN_PROTECTION_FAILURE);
1843 		}
1844 		current = current->next;
1845 	}
1846 
1847 
1848 	/*
1849 	 * Do an accounting pass for private read-only mappings that
1850 	 * now will do cow due to allowed write (e.g. debugger sets
1851 	 * breakpoint on text segment)
1852 	 */
1853 	for (current = entry; (current != &map->header) &&
1854 	     (current->start < end); current = current->next) {
1855 
1856 		vm_map_clip_end(map, current, end);
1857 
1858 		if (set_max ||
1859 		    ((new_prot & ~(current->protection)) & VM_PROT_WRITE) == 0 ||
1860 		    ENTRY_CHARGED(current)) {
1861 			continue;
1862 		}
1863 
1864 		uip = curthread->td_ucred->cr_ruidinfo;
1865 		obj = current->object.vm_object;
1866 
1867 		if (obj == NULL || (current->eflags & MAP_ENTRY_NEEDS_COPY)) {
1868 			if (!swap_reserve(current->end - current->start)) {
1869 				vm_map_unlock(map);
1870 				return (KERN_RESOURCE_SHORTAGE);
1871 			}
1872 			uihold(uip);
1873 			current->uip = uip;
1874 			continue;
1875 		}
1876 
1877 		VM_OBJECT_LOCK(obj);
1878 		if (obj->type != OBJT_DEFAULT && obj->type != OBJT_SWAP) {
1879 			VM_OBJECT_UNLOCK(obj);
1880 			continue;
1881 		}
1882 
1883 		/*
1884 		 * Charge for the whole object allocation now, since
1885 		 * we cannot distinguish between non-charged and
1886 		 * charged clipped mapping of the same object later.
1887 		 */
1888 		KASSERT(obj->charge == 0,
1889 		    ("vm_map_protect: object %p overcharged\n", obj));
1890 		if (!swap_reserve(ptoa(obj->size))) {
1891 			VM_OBJECT_UNLOCK(obj);
1892 			vm_map_unlock(map);
1893 			return (KERN_RESOURCE_SHORTAGE);
1894 		}
1895 
1896 		uihold(uip);
1897 		obj->uip = uip;
1898 		obj->charge = ptoa(obj->size);
1899 		VM_OBJECT_UNLOCK(obj);
1900 	}
1901 
1902 	/*
1903 	 * Go back and fix up protections. [Note that clipping is not
1904 	 * necessary the second time.]
1905 	 */
1906 	current = entry;
1907 	while ((current != &map->header) && (current->start < end)) {
1908 		old_prot = current->protection;
1909 
1910 		if (set_max)
1911 			current->protection =
1912 			    (current->max_protection = new_prot) &
1913 			    old_prot;
1914 		else
1915 			current->protection = new_prot;
1916 
1917 		if ((current->eflags & (MAP_ENTRY_COW | MAP_ENTRY_USER_WIRED))
1918 		     == (MAP_ENTRY_COW | MAP_ENTRY_USER_WIRED) &&
1919 		    (current->protection & VM_PROT_WRITE) != 0 &&
1920 		    (old_prot & VM_PROT_WRITE) == 0) {
1921 			vm_fault_copy_entry(map, map, current, current, NULL);
1922 		}
1923 
1924 		/*
1925 		 * When restricting access, update the physical map.  Worry
1926 		 * about copy-on-write here.
1927 		 */
1928 		if ((old_prot & ~current->protection) != 0) {
1929 #define MASK(entry)	(((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1930 							VM_PROT_ALL)
1931 			pmap_protect(map->pmap, current->start,
1932 			    current->end,
1933 			    current->protection & MASK(current));
1934 #undef	MASK
1935 		}
1936 		vm_map_simplify_entry(map, current);
1937 		current = current->next;
1938 	}
1939 	vm_map_unlock(map);
1940 	return (KERN_SUCCESS);
1941 }
1942 
1943 /*
1944  *	vm_map_madvise:
1945  *
1946  *	This routine traverses a processes map handling the madvise
1947  *	system call.  Advisories are classified as either those effecting
1948  *	the vm_map_entry structure, or those effecting the underlying
1949  *	objects.
1950  */
1951 int
1952 vm_map_madvise(
1953 	vm_map_t map,
1954 	vm_offset_t start,
1955 	vm_offset_t end,
1956 	int behav)
1957 {
1958 	vm_map_entry_t current, entry;
1959 	int modify_map = 0;
1960 
1961 	/*
1962 	 * Some madvise calls directly modify the vm_map_entry, in which case
1963 	 * we need to use an exclusive lock on the map and we need to perform
1964 	 * various clipping operations.  Otherwise we only need a read-lock
1965 	 * on the map.
1966 	 */
1967 	switch(behav) {
1968 	case MADV_NORMAL:
1969 	case MADV_SEQUENTIAL:
1970 	case MADV_RANDOM:
1971 	case MADV_NOSYNC:
1972 	case MADV_AUTOSYNC:
1973 	case MADV_NOCORE:
1974 	case MADV_CORE:
1975 		modify_map = 1;
1976 		vm_map_lock(map);
1977 		break;
1978 	case MADV_WILLNEED:
1979 	case MADV_DONTNEED:
1980 	case MADV_FREE:
1981 		vm_map_lock_read(map);
1982 		break;
1983 	default:
1984 		return (KERN_INVALID_ARGUMENT);
1985 	}
1986 
1987 	/*
1988 	 * Locate starting entry and clip if necessary.
1989 	 */
1990 	VM_MAP_RANGE_CHECK(map, start, end);
1991 
1992 	if (vm_map_lookup_entry(map, start, &entry)) {
1993 		if (modify_map)
1994 			vm_map_clip_start(map, entry, start);
1995 	} else {
1996 		entry = entry->next;
1997 	}
1998 
1999 	if (modify_map) {
2000 		/*
2001 		 * madvise behaviors that are implemented in the vm_map_entry.
2002 		 *
2003 		 * We clip the vm_map_entry so that behavioral changes are
2004 		 * limited to the specified address range.
2005 		 */
2006 		for (current = entry;
2007 		     (current != &map->header) && (current->start < end);
2008 		     current = current->next
2009 		) {
2010 			if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
2011 				continue;
2012 
2013 			vm_map_clip_end(map, current, end);
2014 
2015 			switch (behav) {
2016 			case MADV_NORMAL:
2017 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
2018 				break;
2019 			case MADV_SEQUENTIAL:
2020 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
2021 				break;
2022 			case MADV_RANDOM:
2023 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
2024 				break;
2025 			case MADV_NOSYNC:
2026 				current->eflags |= MAP_ENTRY_NOSYNC;
2027 				break;
2028 			case MADV_AUTOSYNC:
2029 				current->eflags &= ~MAP_ENTRY_NOSYNC;
2030 				break;
2031 			case MADV_NOCORE:
2032 				current->eflags |= MAP_ENTRY_NOCOREDUMP;
2033 				break;
2034 			case MADV_CORE:
2035 				current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
2036 				break;
2037 			default:
2038 				break;
2039 			}
2040 			vm_map_simplify_entry(map, current);
2041 		}
2042 		vm_map_unlock(map);
2043 	} else {
2044 		vm_pindex_t pindex;
2045 		int count;
2046 
2047 		/*
2048 		 * madvise behaviors that are implemented in the underlying
2049 		 * vm_object.
2050 		 *
2051 		 * Since we don't clip the vm_map_entry, we have to clip
2052 		 * the vm_object pindex and count.
2053 		 */
2054 		for (current = entry;
2055 		     (current != &map->header) && (current->start < end);
2056 		     current = current->next
2057 		) {
2058 			vm_offset_t useStart;
2059 
2060 			if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
2061 				continue;
2062 
2063 			pindex = OFF_TO_IDX(current->offset);
2064 			count = atop(current->end - current->start);
2065 			useStart = current->start;
2066 
2067 			if (current->start < start) {
2068 				pindex += atop(start - current->start);
2069 				count -= atop(start - current->start);
2070 				useStart = start;
2071 			}
2072 			if (current->end > end)
2073 				count -= atop(current->end - end);
2074 
2075 			if (count <= 0)
2076 				continue;
2077 
2078 			vm_object_madvise(current->object.vm_object,
2079 					  pindex, count, behav);
2080 			if (behav == MADV_WILLNEED) {
2081 				vm_map_pmap_enter(map,
2082 				    useStart,
2083 				    current->protection,
2084 				    current->object.vm_object,
2085 				    pindex,
2086 				    (count << PAGE_SHIFT),
2087 				    MAP_PREFAULT_MADVISE
2088 				);
2089 			}
2090 		}
2091 		vm_map_unlock_read(map);
2092 	}
2093 	return (0);
2094 }
2095 
2096 
2097 /*
2098  *	vm_map_inherit:
2099  *
2100  *	Sets the inheritance of the specified address
2101  *	range in the target map.  Inheritance
2102  *	affects how the map will be shared with
2103  *	child maps at the time of vmspace_fork.
2104  */
2105 int
2106 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
2107 	       vm_inherit_t new_inheritance)
2108 {
2109 	vm_map_entry_t entry;
2110 	vm_map_entry_t temp_entry;
2111 
2112 	switch (new_inheritance) {
2113 	case VM_INHERIT_NONE:
2114 	case VM_INHERIT_COPY:
2115 	case VM_INHERIT_SHARE:
2116 		break;
2117 	default:
2118 		return (KERN_INVALID_ARGUMENT);
2119 	}
2120 	vm_map_lock(map);
2121 	VM_MAP_RANGE_CHECK(map, start, end);
2122 	if (vm_map_lookup_entry(map, start, &temp_entry)) {
2123 		entry = temp_entry;
2124 		vm_map_clip_start(map, entry, start);
2125 	} else
2126 		entry = temp_entry->next;
2127 	while ((entry != &map->header) && (entry->start < end)) {
2128 		vm_map_clip_end(map, entry, end);
2129 		entry->inheritance = new_inheritance;
2130 		vm_map_simplify_entry(map, entry);
2131 		entry = entry->next;
2132 	}
2133 	vm_map_unlock(map);
2134 	return (KERN_SUCCESS);
2135 }
2136 
2137 /*
2138  *	vm_map_unwire:
2139  *
2140  *	Implements both kernel and user unwiring.
2141  */
2142 int
2143 vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
2144     int flags)
2145 {
2146 	vm_map_entry_t entry, first_entry, tmp_entry;
2147 	vm_offset_t saved_start;
2148 	unsigned int last_timestamp;
2149 	int rv;
2150 	boolean_t need_wakeup, result, user_unwire;
2151 
2152 	user_unwire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
2153 	vm_map_lock(map);
2154 	VM_MAP_RANGE_CHECK(map, start, end);
2155 	if (!vm_map_lookup_entry(map, start, &first_entry)) {
2156 		if (flags & VM_MAP_WIRE_HOLESOK)
2157 			first_entry = first_entry->next;
2158 		else {
2159 			vm_map_unlock(map);
2160 			return (KERN_INVALID_ADDRESS);
2161 		}
2162 	}
2163 	last_timestamp = map->timestamp;
2164 	entry = first_entry;
2165 	while (entry != &map->header && entry->start < end) {
2166 		if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
2167 			/*
2168 			 * We have not yet clipped the entry.
2169 			 */
2170 			saved_start = (start >= entry->start) ? start :
2171 			    entry->start;
2172 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2173 			if (vm_map_unlock_and_wait(map, 0)) {
2174 				/*
2175 				 * Allow interruption of user unwiring?
2176 				 */
2177 			}
2178 			vm_map_lock(map);
2179 			if (last_timestamp+1 != map->timestamp) {
2180 				/*
2181 				 * Look again for the entry because the map was
2182 				 * modified while it was unlocked.
2183 				 * Specifically, the entry may have been
2184 				 * clipped, merged, or deleted.
2185 				 */
2186 				if (!vm_map_lookup_entry(map, saved_start,
2187 				    &tmp_entry)) {
2188 					if (flags & VM_MAP_WIRE_HOLESOK)
2189 						tmp_entry = tmp_entry->next;
2190 					else {
2191 						if (saved_start == start) {
2192 							/*
2193 							 * First_entry has been deleted.
2194 							 */
2195 							vm_map_unlock(map);
2196 							return (KERN_INVALID_ADDRESS);
2197 						}
2198 						end = saved_start;
2199 						rv = KERN_INVALID_ADDRESS;
2200 						goto done;
2201 					}
2202 				}
2203 				if (entry == first_entry)
2204 					first_entry = tmp_entry;
2205 				else
2206 					first_entry = NULL;
2207 				entry = tmp_entry;
2208 			}
2209 			last_timestamp = map->timestamp;
2210 			continue;
2211 		}
2212 		vm_map_clip_start(map, entry, start);
2213 		vm_map_clip_end(map, entry, end);
2214 		/*
2215 		 * Mark the entry in case the map lock is released.  (See
2216 		 * above.)
2217 		 */
2218 		entry->eflags |= MAP_ENTRY_IN_TRANSITION;
2219 		/*
2220 		 * Check the map for holes in the specified region.
2221 		 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
2222 		 */
2223 		if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
2224 		    (entry->end < end && (entry->next == &map->header ||
2225 		    entry->next->start > entry->end))) {
2226 			end = entry->end;
2227 			rv = KERN_INVALID_ADDRESS;
2228 			goto done;
2229 		}
2230 		/*
2231 		 * If system unwiring, require that the entry is system wired.
2232 		 */
2233 		if (!user_unwire &&
2234 		    vm_map_entry_system_wired_count(entry) == 0) {
2235 			end = entry->end;
2236 			rv = KERN_INVALID_ARGUMENT;
2237 			goto done;
2238 		}
2239 		entry = entry->next;
2240 	}
2241 	rv = KERN_SUCCESS;
2242 done:
2243 	need_wakeup = FALSE;
2244 	if (first_entry == NULL) {
2245 		result = vm_map_lookup_entry(map, start, &first_entry);
2246 		if (!result && (flags & VM_MAP_WIRE_HOLESOK))
2247 			first_entry = first_entry->next;
2248 		else
2249 			KASSERT(result, ("vm_map_unwire: lookup failed"));
2250 	}
2251 	entry = first_entry;
2252 	while (entry != &map->header && entry->start < end) {
2253 		if (rv == KERN_SUCCESS && (!user_unwire ||
2254 		    (entry->eflags & MAP_ENTRY_USER_WIRED))) {
2255 			if (user_unwire)
2256 				entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2257 			entry->wired_count--;
2258 			if (entry->wired_count == 0) {
2259 				/*
2260 				 * Retain the map lock.
2261 				 */
2262 				vm_fault_unwire(map, entry->start, entry->end,
2263 				    entry->object.vm_object != NULL &&
2264 				    (entry->object.vm_object->type == OBJT_DEVICE ||
2265 				    entry->object.vm_object->type == OBJT_SG));
2266 			}
2267 		}
2268 		KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
2269 			("vm_map_unwire: in-transition flag missing"));
2270 		entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
2271 		if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
2272 			entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
2273 			need_wakeup = TRUE;
2274 		}
2275 		vm_map_simplify_entry(map, entry);
2276 		entry = entry->next;
2277 	}
2278 	vm_map_unlock(map);
2279 	if (need_wakeup)
2280 		vm_map_wakeup(map);
2281 	return (rv);
2282 }
2283 
2284 /*
2285  *	vm_map_wire:
2286  *
2287  *	Implements both kernel and user wiring.
2288  */
2289 int
2290 vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
2291     int flags)
2292 {
2293 	vm_map_entry_t entry, first_entry, tmp_entry;
2294 	vm_offset_t saved_end, saved_start;
2295 	unsigned int last_timestamp;
2296 	int rv;
2297 	boolean_t fictitious, need_wakeup, result, user_wire;
2298 
2299 	user_wire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
2300 	vm_map_lock(map);
2301 	VM_MAP_RANGE_CHECK(map, start, end);
2302 	if (!vm_map_lookup_entry(map, start, &first_entry)) {
2303 		if (flags & VM_MAP_WIRE_HOLESOK)
2304 			first_entry = first_entry->next;
2305 		else {
2306 			vm_map_unlock(map);
2307 			return (KERN_INVALID_ADDRESS);
2308 		}
2309 	}
2310 	last_timestamp = map->timestamp;
2311 	entry = first_entry;
2312 	while (entry != &map->header && entry->start < end) {
2313 		if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
2314 			/*
2315 			 * We have not yet clipped the entry.
2316 			 */
2317 			saved_start = (start >= entry->start) ? start :
2318 			    entry->start;
2319 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2320 			if (vm_map_unlock_and_wait(map, 0)) {
2321 				/*
2322 				 * Allow interruption of user wiring?
2323 				 */
2324 			}
2325 			vm_map_lock(map);
2326 			if (last_timestamp + 1 != map->timestamp) {
2327 				/*
2328 				 * Look again for the entry because the map was
2329 				 * modified while it was unlocked.
2330 				 * Specifically, the entry may have been
2331 				 * clipped, merged, or deleted.
2332 				 */
2333 				if (!vm_map_lookup_entry(map, saved_start,
2334 				    &tmp_entry)) {
2335 					if (flags & VM_MAP_WIRE_HOLESOK)
2336 						tmp_entry = tmp_entry->next;
2337 					else {
2338 						if (saved_start == start) {
2339 							/*
2340 							 * first_entry has been deleted.
2341 							 */
2342 							vm_map_unlock(map);
2343 							return (KERN_INVALID_ADDRESS);
2344 						}
2345 						end = saved_start;
2346 						rv = KERN_INVALID_ADDRESS;
2347 						goto done;
2348 					}
2349 				}
2350 				if (entry == first_entry)
2351 					first_entry = tmp_entry;
2352 				else
2353 					first_entry = NULL;
2354 				entry = tmp_entry;
2355 			}
2356 			last_timestamp = map->timestamp;
2357 			continue;
2358 		}
2359 		vm_map_clip_start(map, entry, start);
2360 		vm_map_clip_end(map, entry, end);
2361 		/*
2362 		 * Mark the entry in case the map lock is released.  (See
2363 		 * above.)
2364 		 */
2365 		entry->eflags |= MAP_ENTRY_IN_TRANSITION;
2366 		/*
2367 		 *
2368 		 */
2369 		if (entry->wired_count == 0) {
2370 			if ((entry->protection & (VM_PROT_READ|VM_PROT_EXECUTE))
2371 			    == 0) {
2372 				entry->eflags |= MAP_ENTRY_WIRE_SKIPPED;
2373 				if ((flags & VM_MAP_WIRE_HOLESOK) == 0) {
2374 					end = entry->end;
2375 					rv = KERN_INVALID_ADDRESS;
2376 					goto done;
2377 				}
2378 				goto next_entry;
2379 			}
2380 			entry->wired_count++;
2381 			saved_start = entry->start;
2382 			saved_end = entry->end;
2383 			fictitious = entry->object.vm_object != NULL &&
2384 			    (entry->object.vm_object->type == OBJT_DEVICE ||
2385 			    entry->object.vm_object->type == OBJT_SG);
2386 			/*
2387 			 * Release the map lock, relying on the in-transition
2388 			 * mark.
2389 			 */
2390 			vm_map_unlock(map);
2391 			rv = vm_fault_wire(map, saved_start, saved_end,
2392 			    fictitious);
2393 			vm_map_lock(map);
2394 			if (last_timestamp + 1 != map->timestamp) {
2395 				/*
2396 				 * Look again for the entry because the map was
2397 				 * modified while it was unlocked.  The entry
2398 				 * may have been clipped, but NOT merged or
2399 				 * deleted.
2400 				 */
2401 				result = vm_map_lookup_entry(map, saved_start,
2402 				    &tmp_entry);
2403 				KASSERT(result, ("vm_map_wire: lookup failed"));
2404 				if (entry == first_entry)
2405 					first_entry = tmp_entry;
2406 				else
2407 					first_entry = NULL;
2408 				entry = tmp_entry;
2409 				while (entry->end < saved_end) {
2410 					if (rv != KERN_SUCCESS) {
2411 						KASSERT(entry->wired_count == 1,
2412 						    ("vm_map_wire: bad count"));
2413 						entry->wired_count = -1;
2414 					}
2415 					entry = entry->next;
2416 				}
2417 			}
2418 			last_timestamp = map->timestamp;
2419 			if (rv != KERN_SUCCESS) {
2420 				KASSERT(entry->wired_count == 1,
2421 				    ("vm_map_wire: bad count"));
2422 				/*
2423 				 * Assign an out-of-range value to represent
2424 				 * the failure to wire this entry.
2425 				 */
2426 				entry->wired_count = -1;
2427 				end = entry->end;
2428 				goto done;
2429 			}
2430 		} else if (!user_wire ||
2431 			   (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
2432 			entry->wired_count++;
2433 		}
2434 		/*
2435 		 * Check the map for holes in the specified region.
2436 		 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
2437 		 */
2438 	next_entry:
2439 		if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
2440 		    (entry->end < end && (entry->next == &map->header ||
2441 		    entry->next->start > entry->end))) {
2442 			end = entry->end;
2443 			rv = KERN_INVALID_ADDRESS;
2444 			goto done;
2445 		}
2446 		entry = entry->next;
2447 	}
2448 	rv = KERN_SUCCESS;
2449 done:
2450 	need_wakeup = FALSE;
2451 	if (first_entry == NULL) {
2452 		result = vm_map_lookup_entry(map, start, &first_entry);
2453 		if (!result && (flags & VM_MAP_WIRE_HOLESOK))
2454 			first_entry = first_entry->next;
2455 		else
2456 			KASSERT(result, ("vm_map_wire: lookup failed"));
2457 	}
2458 	entry = first_entry;
2459 	while (entry != &map->header && entry->start < end) {
2460 		if ((entry->eflags & MAP_ENTRY_WIRE_SKIPPED) != 0)
2461 			goto next_entry_done;
2462 		if (rv == KERN_SUCCESS) {
2463 			if (user_wire)
2464 				entry->eflags |= MAP_ENTRY_USER_WIRED;
2465 		} else if (entry->wired_count == -1) {
2466 			/*
2467 			 * Wiring failed on this entry.  Thus, unwiring is
2468 			 * unnecessary.
2469 			 */
2470 			entry->wired_count = 0;
2471 		} else {
2472 			if (!user_wire ||
2473 			    (entry->eflags & MAP_ENTRY_USER_WIRED) == 0)
2474 				entry->wired_count--;
2475 			if (entry->wired_count == 0) {
2476 				/*
2477 				 * Retain the map lock.
2478 				 */
2479 				vm_fault_unwire(map, entry->start, entry->end,
2480 				    entry->object.vm_object != NULL &&
2481 				    (entry->object.vm_object->type == OBJT_DEVICE ||
2482 				    entry->object.vm_object->type == OBJT_SG));
2483 			}
2484 		}
2485 	next_entry_done:
2486 		KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
2487 			("vm_map_wire: in-transition flag missing"));
2488 		entry->eflags &= ~(MAP_ENTRY_IN_TRANSITION|MAP_ENTRY_WIRE_SKIPPED);
2489 		if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
2490 			entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
2491 			need_wakeup = TRUE;
2492 		}
2493 		vm_map_simplify_entry(map, entry);
2494 		entry = entry->next;
2495 	}
2496 	vm_map_unlock(map);
2497 	if (need_wakeup)
2498 		vm_map_wakeup(map);
2499 	return (rv);
2500 }
2501 
2502 /*
2503  * vm_map_sync
2504  *
2505  * Push any dirty cached pages in the address range to their pager.
2506  * If syncio is TRUE, dirty pages are written synchronously.
2507  * If invalidate is TRUE, any cached pages are freed as well.
2508  *
2509  * If the size of the region from start to end is zero, we are
2510  * supposed to flush all modified pages within the region containing
2511  * start.  Unfortunately, a region can be split or coalesced with
2512  * neighboring regions, making it difficult to determine what the
2513  * original region was.  Therefore, we approximate this requirement by
2514  * flushing the current region containing start.
2515  *
2516  * Returns an error if any part of the specified range is not mapped.
2517  */
2518 int
2519 vm_map_sync(
2520 	vm_map_t map,
2521 	vm_offset_t start,
2522 	vm_offset_t end,
2523 	boolean_t syncio,
2524 	boolean_t invalidate)
2525 {
2526 	vm_map_entry_t current;
2527 	vm_map_entry_t entry;
2528 	vm_size_t size;
2529 	vm_object_t object;
2530 	vm_ooffset_t offset;
2531 	unsigned int last_timestamp;
2532 
2533 	vm_map_lock_read(map);
2534 	VM_MAP_RANGE_CHECK(map, start, end);
2535 	if (!vm_map_lookup_entry(map, start, &entry)) {
2536 		vm_map_unlock_read(map);
2537 		return (KERN_INVALID_ADDRESS);
2538 	} else if (start == end) {
2539 		start = entry->start;
2540 		end = entry->end;
2541 	}
2542 	/*
2543 	 * Make a first pass to check for user-wired memory and holes.
2544 	 */
2545 	for (current = entry; current != &map->header && current->start < end;
2546 	    current = current->next) {
2547 		if (invalidate && (current->eflags & MAP_ENTRY_USER_WIRED)) {
2548 			vm_map_unlock_read(map);
2549 			return (KERN_INVALID_ARGUMENT);
2550 		}
2551 		if (end > current->end &&
2552 		    (current->next == &map->header ||
2553 			current->end != current->next->start)) {
2554 			vm_map_unlock_read(map);
2555 			return (KERN_INVALID_ADDRESS);
2556 		}
2557 	}
2558 
2559 	if (invalidate)
2560 		pmap_remove(map->pmap, start, end);
2561 
2562 	/*
2563 	 * Make a second pass, cleaning/uncaching pages from the indicated
2564 	 * objects as we go.
2565 	 */
2566 	for (current = entry; current != &map->header && current->start < end;) {
2567 		offset = current->offset + (start - current->start);
2568 		size = (end <= current->end ? end : current->end) - start;
2569 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
2570 			vm_map_t smap;
2571 			vm_map_entry_t tentry;
2572 			vm_size_t tsize;
2573 
2574 			smap = current->object.sub_map;
2575 			vm_map_lock_read(smap);
2576 			(void) vm_map_lookup_entry(smap, offset, &tentry);
2577 			tsize = tentry->end - offset;
2578 			if (tsize < size)
2579 				size = tsize;
2580 			object = tentry->object.vm_object;
2581 			offset = tentry->offset + (offset - tentry->start);
2582 			vm_map_unlock_read(smap);
2583 		} else {
2584 			object = current->object.vm_object;
2585 		}
2586 		vm_object_reference(object);
2587 		last_timestamp = map->timestamp;
2588 		vm_map_unlock_read(map);
2589 		vm_object_sync(object, offset, size, syncio, invalidate);
2590 		start += size;
2591 		vm_object_deallocate(object);
2592 		vm_map_lock_read(map);
2593 		if (last_timestamp == map->timestamp ||
2594 		    !vm_map_lookup_entry(map, start, &current))
2595 			current = current->next;
2596 	}
2597 
2598 	vm_map_unlock_read(map);
2599 	return (KERN_SUCCESS);
2600 }
2601 
2602 /*
2603  *	vm_map_entry_unwire:	[ internal use only ]
2604  *
2605  *	Make the region specified by this entry pageable.
2606  *
2607  *	The map in question should be locked.
2608  *	[This is the reason for this routine's existence.]
2609  */
2610 static void
2611 vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
2612 {
2613 	vm_fault_unwire(map, entry->start, entry->end,
2614 	    entry->object.vm_object != NULL &&
2615 	    (entry->object.vm_object->type == OBJT_DEVICE ||
2616 	    entry->object.vm_object->type == OBJT_SG));
2617 	entry->wired_count = 0;
2618 }
2619 
2620 /*
2621  *	vm_map_entry_delete:	[ internal use only ]
2622  *
2623  *	Deallocate the given entry from the target map.
2624  */
2625 static void
2626 vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry)
2627 {
2628 	vm_object_t object;
2629 	vm_pindex_t offidxstart, offidxend, count, size1;
2630 	vm_ooffset_t size;
2631 
2632 	vm_map_entry_unlink(map, entry);
2633 	object = entry->object.vm_object;
2634 	size = entry->end - entry->start;
2635 	map->size -= size;
2636 
2637 	if (entry->uip != NULL) {
2638 		swap_release_by_uid(size, entry->uip);
2639 		uifree(entry->uip);
2640 	}
2641 
2642 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
2643 	    (object != NULL)) {
2644 		KASSERT(entry->uip == NULL || object->uip == NULL ||
2645 		    (entry->eflags & MAP_ENTRY_NEEDS_COPY),
2646 		    ("OVERCOMMIT vm_map_entry_delete: both uip %p", entry));
2647 		count = OFF_TO_IDX(size);
2648 		offidxstart = OFF_TO_IDX(entry->offset);
2649 		offidxend = offidxstart + count;
2650 		VM_OBJECT_LOCK(object);
2651 		if (object->ref_count != 1 &&
2652 		    ((object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING ||
2653 		    object == kernel_object || object == kmem_object)) {
2654 			vm_object_collapse(object);
2655 			vm_object_page_remove(object, offidxstart, offidxend, FALSE);
2656 			if (object->type == OBJT_SWAP)
2657 				swap_pager_freespace(object, offidxstart, count);
2658 			if (offidxend >= object->size &&
2659 			    offidxstart < object->size) {
2660 				size1 = object->size;
2661 				object->size = offidxstart;
2662 				if (object->uip != NULL) {
2663 					size1 -= object->size;
2664 					KASSERT(object->charge >= ptoa(size1),
2665 					    ("vm_map_entry_delete: object->charge < 0"));
2666 					swap_release_by_uid(ptoa(size1), object->uip);
2667 					object->charge -= ptoa(size1);
2668 				}
2669 			}
2670 		}
2671 		VM_OBJECT_UNLOCK(object);
2672 	} else
2673 		entry->object.vm_object = NULL;
2674 }
2675 
2676 /*
2677  *	vm_map_delete:	[ internal use only ]
2678  *
2679  *	Deallocates the given address range from the target
2680  *	map.
2681  */
2682 int
2683 vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end)
2684 {
2685 	vm_map_entry_t entry;
2686 	vm_map_entry_t first_entry;
2687 
2688 	VM_MAP_ASSERT_LOCKED(map);
2689 
2690 	/*
2691 	 * Find the start of the region, and clip it
2692 	 */
2693 	if (!vm_map_lookup_entry(map, start, &first_entry))
2694 		entry = first_entry->next;
2695 	else {
2696 		entry = first_entry;
2697 		vm_map_clip_start(map, entry, start);
2698 	}
2699 
2700 	/*
2701 	 * Step through all entries in this region
2702 	 */
2703 	while ((entry != &map->header) && (entry->start < end)) {
2704 		vm_map_entry_t next;
2705 
2706 		/*
2707 		 * Wait for wiring or unwiring of an entry to complete.
2708 		 * Also wait for any system wirings to disappear on
2709 		 * user maps.
2710 		 */
2711 		if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 ||
2712 		    (vm_map_pmap(map) != kernel_pmap &&
2713 		    vm_map_entry_system_wired_count(entry) != 0)) {
2714 			unsigned int last_timestamp;
2715 			vm_offset_t saved_start;
2716 			vm_map_entry_t tmp_entry;
2717 
2718 			saved_start = entry->start;
2719 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2720 			last_timestamp = map->timestamp;
2721 			(void) vm_map_unlock_and_wait(map, 0);
2722 			vm_map_lock(map);
2723 			if (last_timestamp + 1 != map->timestamp) {
2724 				/*
2725 				 * Look again for the entry because the map was
2726 				 * modified while it was unlocked.
2727 				 * Specifically, the entry may have been
2728 				 * clipped, merged, or deleted.
2729 				 */
2730 				if (!vm_map_lookup_entry(map, saved_start,
2731 							 &tmp_entry))
2732 					entry = tmp_entry->next;
2733 				else {
2734 					entry = tmp_entry;
2735 					vm_map_clip_start(map, entry,
2736 							  saved_start);
2737 				}
2738 			}
2739 			continue;
2740 		}
2741 		vm_map_clip_end(map, entry, end);
2742 
2743 		next = entry->next;
2744 
2745 		/*
2746 		 * Unwire before removing addresses from the pmap; otherwise,
2747 		 * unwiring will put the entries back in the pmap.
2748 		 */
2749 		if (entry->wired_count != 0) {
2750 			vm_map_entry_unwire(map, entry);
2751 		}
2752 
2753 		pmap_remove(map->pmap, entry->start, entry->end);
2754 
2755 		/*
2756 		 * Delete the entry only after removing all pmap
2757 		 * entries pointing to its pages.  (Otherwise, its
2758 		 * page frames may be reallocated, and any modify bits
2759 		 * will be set in the wrong object!)
2760 		 */
2761 		vm_map_entry_delete(map, entry);
2762 		entry->next = map->deferred_freelist;
2763 		map->deferred_freelist = entry;
2764 		entry = next;
2765 	}
2766 	return (KERN_SUCCESS);
2767 }
2768 
2769 /*
2770  *	vm_map_remove:
2771  *
2772  *	Remove the given address range from the target map.
2773  *	This is the exported form of vm_map_delete.
2774  */
2775 int
2776 vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
2777 {
2778 	int result;
2779 
2780 	vm_map_lock(map);
2781 	VM_MAP_RANGE_CHECK(map, start, end);
2782 	result = vm_map_delete(map, start, end);
2783 	vm_map_unlock(map);
2784 	return (result);
2785 }
2786 
2787 /*
2788  *	vm_map_check_protection:
2789  *
2790  *	Assert that the target map allows the specified privilege on the
2791  *	entire address region given.  The entire region must be allocated.
2792  *
2793  *	WARNING!  This code does not and should not check whether the
2794  *	contents of the region is accessible.  For example a smaller file
2795  *	might be mapped into a larger address space.
2796  *
2797  *	NOTE!  This code is also called by munmap().
2798  *
2799  *	The map must be locked.  A read lock is sufficient.
2800  */
2801 boolean_t
2802 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
2803 			vm_prot_t protection)
2804 {
2805 	vm_map_entry_t entry;
2806 	vm_map_entry_t tmp_entry;
2807 
2808 	if (!vm_map_lookup_entry(map, start, &tmp_entry))
2809 		return (FALSE);
2810 	entry = tmp_entry;
2811 
2812 	while (start < end) {
2813 		if (entry == &map->header)
2814 			return (FALSE);
2815 		/*
2816 		 * No holes allowed!
2817 		 */
2818 		if (start < entry->start)
2819 			return (FALSE);
2820 		/*
2821 		 * Check protection associated with entry.
2822 		 */
2823 		if ((entry->protection & protection) != protection)
2824 			return (FALSE);
2825 		/* go to next entry */
2826 		start = entry->end;
2827 		entry = entry->next;
2828 	}
2829 	return (TRUE);
2830 }
2831 
2832 /*
2833  *	vm_map_copy_entry:
2834  *
2835  *	Copies the contents of the source entry to the destination
2836  *	entry.  The entries *must* be aligned properly.
2837  */
2838 static void
2839 vm_map_copy_entry(
2840 	vm_map_t src_map,
2841 	vm_map_t dst_map,
2842 	vm_map_entry_t src_entry,
2843 	vm_map_entry_t dst_entry,
2844 	vm_ooffset_t *fork_charge)
2845 {
2846 	vm_object_t src_object;
2847 	vm_offset_t size;
2848 	struct uidinfo *uip;
2849 	int charged;
2850 
2851 	VM_MAP_ASSERT_LOCKED(dst_map);
2852 
2853 	if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
2854 		return;
2855 
2856 	if (src_entry->wired_count == 0) {
2857 
2858 		/*
2859 		 * If the source entry is marked needs_copy, it is already
2860 		 * write-protected.
2861 		 */
2862 		if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
2863 			pmap_protect(src_map->pmap,
2864 			    src_entry->start,
2865 			    src_entry->end,
2866 			    src_entry->protection & ~VM_PROT_WRITE);
2867 		}
2868 
2869 		/*
2870 		 * Make a copy of the object.
2871 		 */
2872 		size = src_entry->end - src_entry->start;
2873 		if ((src_object = src_entry->object.vm_object) != NULL) {
2874 			VM_OBJECT_LOCK(src_object);
2875 			charged = ENTRY_CHARGED(src_entry);
2876 			if ((src_object->handle == NULL) &&
2877 				(src_object->type == OBJT_DEFAULT ||
2878 				 src_object->type == OBJT_SWAP)) {
2879 				vm_object_collapse(src_object);
2880 				if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
2881 					vm_object_split(src_entry);
2882 					src_object = src_entry->object.vm_object;
2883 				}
2884 			}
2885 			vm_object_reference_locked(src_object);
2886 			vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
2887 			if (src_entry->uip != NULL &&
2888 			    !(src_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
2889 				KASSERT(src_object->uip == NULL,
2890 				    ("OVERCOMMIT: vm_map_copy_entry: uip %p",
2891 				     src_object));
2892 				src_object->uip = src_entry->uip;
2893 				src_object->charge = size;
2894 			}
2895 			VM_OBJECT_UNLOCK(src_object);
2896 			dst_entry->object.vm_object = src_object;
2897 			if (charged) {
2898 				uip = curthread->td_ucred->cr_ruidinfo;
2899 				uihold(uip);
2900 				dst_entry->uip = uip;
2901 				*fork_charge += size;
2902 				if (!(src_entry->eflags &
2903 				      MAP_ENTRY_NEEDS_COPY)) {
2904 					uihold(uip);
2905 					src_entry->uip = uip;
2906 					*fork_charge += size;
2907 				}
2908 			}
2909 			src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2910 			dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2911 			dst_entry->offset = src_entry->offset;
2912 		} else {
2913 			dst_entry->object.vm_object = NULL;
2914 			dst_entry->offset = 0;
2915 			if (src_entry->uip != NULL) {
2916 				dst_entry->uip = curthread->td_ucred->cr_ruidinfo;
2917 				uihold(dst_entry->uip);
2918 				*fork_charge += size;
2919 			}
2920 		}
2921 
2922 		pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
2923 		    dst_entry->end - dst_entry->start, src_entry->start);
2924 	} else {
2925 		/*
2926 		 * Of course, wired down pages can't be set copy-on-write.
2927 		 * Cause wired pages to be copied into the new map by
2928 		 * simulating faults (the new pages are pageable)
2929 		 */
2930 		vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry,
2931 		    fork_charge);
2932 	}
2933 }
2934 
2935 /*
2936  * vmspace_map_entry_forked:
2937  * Update the newly-forked vmspace each time a map entry is inherited
2938  * or copied.  The values for vm_dsize and vm_tsize are approximate
2939  * (and mostly-obsolete ideas in the face of mmap(2) et al.)
2940  */
2941 static void
2942 vmspace_map_entry_forked(const struct vmspace *vm1, struct vmspace *vm2,
2943     vm_map_entry_t entry)
2944 {
2945 	vm_size_t entrysize;
2946 	vm_offset_t newend;
2947 
2948 	entrysize = entry->end - entry->start;
2949 	vm2->vm_map.size += entrysize;
2950 	if (entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP)) {
2951 		vm2->vm_ssize += btoc(entrysize);
2952 	} else if (entry->start >= (vm_offset_t)vm1->vm_daddr &&
2953 	    entry->start < (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)) {
2954 		newend = MIN(entry->end,
2955 		    (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize));
2956 		vm2->vm_dsize += btoc(newend - entry->start);
2957 	} else if (entry->start >= (vm_offset_t)vm1->vm_taddr &&
2958 	    entry->start < (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)) {
2959 		newend = MIN(entry->end,
2960 		    (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize));
2961 		vm2->vm_tsize += btoc(newend - entry->start);
2962 	}
2963 }
2964 
2965 /*
2966  * vmspace_fork:
2967  * Create a new process vmspace structure and vm_map
2968  * based on those of an existing process.  The new map
2969  * is based on the old map, according to the inheritance
2970  * values on the regions in that map.
2971  *
2972  * XXX It might be worth coalescing the entries added to the new vmspace.
2973  *
2974  * The source map must not be locked.
2975  */
2976 struct vmspace *
2977 vmspace_fork(struct vmspace *vm1, vm_ooffset_t *fork_charge)
2978 {
2979 	struct vmspace *vm2;
2980 	vm_map_t old_map = &vm1->vm_map;
2981 	vm_map_t new_map;
2982 	vm_map_entry_t old_entry;
2983 	vm_map_entry_t new_entry;
2984 	vm_object_t object;
2985 	int locked;
2986 
2987 	vm_map_lock(old_map);
2988 	vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
2989 	if (vm2 == NULL)
2990 		goto unlock_and_return;
2991 	vm2->vm_taddr = vm1->vm_taddr;
2992 	vm2->vm_daddr = vm1->vm_daddr;
2993 	vm2->vm_maxsaddr = vm1->vm_maxsaddr;
2994 	new_map = &vm2->vm_map;	/* XXX */
2995 	locked = vm_map_trylock(new_map); /* trylock to silence WITNESS */
2996 	KASSERT(locked, ("vmspace_fork: lock failed"));
2997 	new_map->timestamp = 1;
2998 
2999 	old_entry = old_map->header.next;
3000 
3001 	while (old_entry != &old_map->header) {
3002 		if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
3003 			panic("vm_map_fork: encountered a submap");
3004 
3005 		switch (old_entry->inheritance) {
3006 		case VM_INHERIT_NONE:
3007 			break;
3008 
3009 		case VM_INHERIT_SHARE:
3010 			/*
3011 			 * Clone the entry, creating the shared object if necessary.
3012 			 */
3013 			object = old_entry->object.vm_object;
3014 			if (object == NULL) {
3015 				object = vm_object_allocate(OBJT_DEFAULT,
3016 					atop(old_entry->end - old_entry->start));
3017 				old_entry->object.vm_object = object;
3018 				old_entry->offset = 0;
3019 				if (old_entry->uip != NULL) {
3020 					object->uip = old_entry->uip;
3021 					object->charge = old_entry->end -
3022 					    old_entry->start;
3023 					old_entry->uip = NULL;
3024 				}
3025 			}
3026 
3027 			/*
3028 			 * Add the reference before calling vm_object_shadow
3029 			 * to insure that a shadow object is created.
3030 			 */
3031 			vm_object_reference(object);
3032 			if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3033 				vm_object_shadow(&old_entry->object.vm_object,
3034 					&old_entry->offset,
3035 					atop(old_entry->end - old_entry->start));
3036 				old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
3037 				/* Transfer the second reference too. */
3038 				vm_object_reference(
3039 				    old_entry->object.vm_object);
3040 
3041 				/*
3042 				 * As in vm_map_simplify_entry(), the
3043 				 * vnode lock will not be acquired in
3044 				 * this call to vm_object_deallocate().
3045 				 */
3046 				vm_object_deallocate(object);
3047 				object = old_entry->object.vm_object;
3048 			}
3049 			VM_OBJECT_LOCK(object);
3050 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
3051 			if (old_entry->uip != NULL) {
3052 				KASSERT(object->uip == NULL, ("vmspace_fork both uip"));
3053 				object->uip = old_entry->uip;
3054 				object->charge = old_entry->end - old_entry->start;
3055 				old_entry->uip = NULL;
3056 			}
3057 			VM_OBJECT_UNLOCK(object);
3058 
3059 			/*
3060 			 * Clone the entry, referencing the shared object.
3061 			 */
3062 			new_entry = vm_map_entry_create(new_map);
3063 			*new_entry = *old_entry;
3064 			new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
3065 			    MAP_ENTRY_IN_TRANSITION);
3066 			new_entry->wired_count = 0;
3067 
3068 			/*
3069 			 * Insert the entry into the new map -- we know we're
3070 			 * inserting at the end of the new map.
3071 			 */
3072 			vm_map_entry_link(new_map, new_map->header.prev,
3073 			    new_entry);
3074 			vmspace_map_entry_forked(vm1, vm2, new_entry);
3075 
3076 			/*
3077 			 * Update the physical map
3078 			 */
3079 			pmap_copy(new_map->pmap, old_map->pmap,
3080 			    new_entry->start,
3081 			    (old_entry->end - old_entry->start),
3082 			    old_entry->start);
3083 			break;
3084 
3085 		case VM_INHERIT_COPY:
3086 			/*
3087 			 * Clone the entry and link into the map.
3088 			 */
3089 			new_entry = vm_map_entry_create(new_map);
3090 			*new_entry = *old_entry;
3091 			new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
3092 			    MAP_ENTRY_IN_TRANSITION);
3093 			new_entry->wired_count = 0;
3094 			new_entry->object.vm_object = NULL;
3095 			new_entry->uip = NULL;
3096 			vm_map_entry_link(new_map, new_map->header.prev,
3097 			    new_entry);
3098 			vmspace_map_entry_forked(vm1, vm2, new_entry);
3099 			vm_map_copy_entry(old_map, new_map, old_entry,
3100 			    new_entry, fork_charge);
3101 			break;
3102 		}
3103 		old_entry = old_entry->next;
3104 	}
3105 unlock_and_return:
3106 	vm_map_unlock(old_map);
3107 	if (vm2 != NULL)
3108 		vm_map_unlock(new_map);
3109 
3110 	return (vm2);
3111 }
3112 
3113 int
3114 vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
3115     vm_prot_t prot, vm_prot_t max, int cow)
3116 {
3117 	vm_map_entry_t new_entry, prev_entry;
3118 	vm_offset_t bot, top;
3119 	vm_size_t init_ssize;
3120 	int orient, rv;
3121 	rlim_t vmemlim;
3122 
3123 	/*
3124 	 * The stack orientation is piggybacked with the cow argument.
3125 	 * Extract it into orient and mask the cow argument so that we
3126 	 * don't pass it around further.
3127 	 * NOTE: We explicitly allow bi-directional stacks.
3128 	 */
3129 	orient = cow & (MAP_STACK_GROWS_DOWN|MAP_STACK_GROWS_UP);
3130 	cow &= ~orient;
3131 	KASSERT(orient != 0, ("No stack grow direction"));
3132 
3133 	if (addrbos < vm_map_min(map) ||
3134 	    addrbos > vm_map_max(map) ||
3135 	    addrbos + max_ssize < addrbos)
3136 		return (KERN_NO_SPACE);
3137 
3138 	init_ssize = (max_ssize < sgrowsiz) ? max_ssize : sgrowsiz;
3139 
3140 	PROC_LOCK(curthread->td_proc);
3141 	vmemlim = lim_cur(curthread->td_proc, RLIMIT_VMEM);
3142 	PROC_UNLOCK(curthread->td_proc);
3143 
3144 	vm_map_lock(map);
3145 
3146 	/* If addr is already mapped, no go */
3147 	if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
3148 		vm_map_unlock(map);
3149 		return (KERN_NO_SPACE);
3150 	}
3151 
3152 	/* If we would blow our VMEM resource limit, no go */
3153 	if (map->size + init_ssize > vmemlim) {
3154 		vm_map_unlock(map);
3155 		return (KERN_NO_SPACE);
3156 	}
3157 
3158 	/*
3159 	 * If we can't accomodate max_ssize in the current mapping, no go.
3160 	 * However, we need to be aware that subsequent user mappings might
3161 	 * map into the space we have reserved for stack, and currently this
3162 	 * space is not protected.
3163 	 *
3164 	 * Hopefully we will at least detect this condition when we try to
3165 	 * grow the stack.
3166 	 */
3167 	if ((prev_entry->next != &map->header) &&
3168 	    (prev_entry->next->start < addrbos + max_ssize)) {
3169 		vm_map_unlock(map);
3170 		return (KERN_NO_SPACE);
3171 	}
3172 
3173 	/*
3174 	 * We initially map a stack of only init_ssize.  We will grow as
3175 	 * needed later.  Depending on the orientation of the stack (i.e.
3176 	 * the grow direction) we either map at the top of the range, the
3177 	 * bottom of the range or in the middle.
3178 	 *
3179 	 * Note: we would normally expect prot and max to be VM_PROT_ALL,
3180 	 * and cow to be 0.  Possibly we should eliminate these as input
3181 	 * parameters, and just pass these values here in the insert call.
3182 	 */
3183 	if (orient == MAP_STACK_GROWS_DOWN)
3184 		bot = addrbos + max_ssize - init_ssize;
3185 	else if (orient == MAP_STACK_GROWS_UP)
3186 		bot = addrbos;
3187 	else
3188 		bot = round_page(addrbos + max_ssize/2 - init_ssize/2);
3189 	top = bot + init_ssize;
3190 	rv = vm_map_insert(map, NULL, 0, bot, top, prot, max, cow);
3191 
3192 	/* Now set the avail_ssize amount. */
3193 	if (rv == KERN_SUCCESS) {
3194 		if (prev_entry != &map->header)
3195 			vm_map_clip_end(map, prev_entry, bot);
3196 		new_entry = prev_entry->next;
3197 		if (new_entry->end != top || new_entry->start != bot)
3198 			panic("Bad entry start/end for new stack entry");
3199 
3200 		new_entry->avail_ssize = max_ssize - init_ssize;
3201 		if (orient & MAP_STACK_GROWS_DOWN)
3202 			new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
3203 		if (orient & MAP_STACK_GROWS_UP)
3204 			new_entry->eflags |= MAP_ENTRY_GROWS_UP;
3205 	}
3206 
3207 	vm_map_unlock(map);
3208 	return (rv);
3209 }
3210 
3211 /* Attempts to grow a vm stack entry.  Returns KERN_SUCCESS if the
3212  * desired address is already mapped, or if we successfully grow
3213  * the stack.  Also returns KERN_SUCCESS if addr is outside the
3214  * stack range (this is strange, but preserves compatibility with
3215  * the grow function in vm_machdep.c).
3216  */
3217 int
3218 vm_map_growstack(struct proc *p, vm_offset_t addr)
3219 {
3220 	vm_map_entry_t next_entry, prev_entry;
3221 	vm_map_entry_t new_entry, stack_entry;
3222 	struct vmspace *vm = p->p_vmspace;
3223 	vm_map_t map = &vm->vm_map;
3224 	vm_offset_t end;
3225 	size_t grow_amount, max_grow;
3226 	rlim_t stacklim, vmemlim;
3227 	int is_procstack, rv;
3228 	struct uidinfo *uip;
3229 
3230 Retry:
3231 	PROC_LOCK(p);
3232 	stacklim = lim_cur(p, RLIMIT_STACK);
3233 	vmemlim = lim_cur(p, RLIMIT_VMEM);
3234 	PROC_UNLOCK(p);
3235 
3236 	vm_map_lock_read(map);
3237 
3238 	/* If addr is already in the entry range, no need to grow.*/
3239 	if (vm_map_lookup_entry(map, addr, &prev_entry)) {
3240 		vm_map_unlock_read(map);
3241 		return (KERN_SUCCESS);
3242 	}
3243 
3244 	next_entry = prev_entry->next;
3245 	if (!(prev_entry->eflags & MAP_ENTRY_GROWS_UP)) {
3246 		/*
3247 		 * This entry does not grow upwards. Since the address lies
3248 		 * beyond this entry, the next entry (if one exists) has to
3249 		 * be a downward growable entry. The entry list header is
3250 		 * never a growable entry, so it suffices to check the flags.
3251 		 */
3252 		if (!(next_entry->eflags & MAP_ENTRY_GROWS_DOWN)) {
3253 			vm_map_unlock_read(map);
3254 			return (KERN_SUCCESS);
3255 		}
3256 		stack_entry = next_entry;
3257 	} else {
3258 		/*
3259 		 * This entry grows upward. If the next entry does not at
3260 		 * least grow downwards, this is the entry we need to grow.
3261 		 * otherwise we have two possible choices and we have to
3262 		 * select one.
3263 		 */
3264 		if (next_entry->eflags & MAP_ENTRY_GROWS_DOWN) {
3265 			/*
3266 			 * We have two choices; grow the entry closest to
3267 			 * the address to minimize the amount of growth.
3268 			 */
3269 			if (addr - prev_entry->end <= next_entry->start - addr)
3270 				stack_entry = prev_entry;
3271 			else
3272 				stack_entry = next_entry;
3273 		} else
3274 			stack_entry = prev_entry;
3275 	}
3276 
3277 	if (stack_entry == next_entry) {
3278 		KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_DOWN, ("foo"));
3279 		KASSERT(addr < stack_entry->start, ("foo"));
3280 		end = (prev_entry != &map->header) ? prev_entry->end :
3281 		    stack_entry->start - stack_entry->avail_ssize;
3282 		grow_amount = roundup(stack_entry->start - addr, PAGE_SIZE);
3283 		max_grow = stack_entry->start - end;
3284 	} else {
3285 		KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_UP, ("foo"));
3286 		KASSERT(addr >= stack_entry->end, ("foo"));
3287 		end = (next_entry != &map->header) ? next_entry->start :
3288 		    stack_entry->end + stack_entry->avail_ssize;
3289 		grow_amount = roundup(addr + 1 - stack_entry->end, PAGE_SIZE);
3290 		max_grow = end - stack_entry->end;
3291 	}
3292 
3293 	if (grow_amount > stack_entry->avail_ssize) {
3294 		vm_map_unlock_read(map);
3295 		return (KERN_NO_SPACE);
3296 	}
3297 
3298 	/*
3299 	 * If there is no longer enough space between the entries nogo, and
3300 	 * adjust the available space.  Note: this  should only happen if the
3301 	 * user has mapped into the stack area after the stack was created,
3302 	 * and is probably an error.
3303 	 *
3304 	 * This also effectively destroys any guard page the user might have
3305 	 * intended by limiting the stack size.
3306 	 */
3307 	if (grow_amount > max_grow) {
3308 		if (vm_map_lock_upgrade(map))
3309 			goto Retry;
3310 
3311 		stack_entry->avail_ssize = max_grow;
3312 
3313 		vm_map_unlock(map);
3314 		return (KERN_NO_SPACE);
3315 	}
3316 
3317 	is_procstack = (addr >= (vm_offset_t)vm->vm_maxsaddr) ? 1 : 0;
3318 
3319 	/*
3320 	 * If this is the main process stack, see if we're over the stack
3321 	 * limit.
3322 	 */
3323 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
3324 		vm_map_unlock_read(map);
3325 		return (KERN_NO_SPACE);
3326 	}
3327 
3328 	/* Round up the grow amount modulo SGROWSIZ */
3329 	grow_amount = roundup (grow_amount, sgrowsiz);
3330 	if (grow_amount > stack_entry->avail_ssize)
3331 		grow_amount = stack_entry->avail_ssize;
3332 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
3333 		grow_amount = stacklim - ctob(vm->vm_ssize);
3334 	}
3335 
3336 	/* If we would blow our VMEM resource limit, no go */
3337 	if (map->size + grow_amount > vmemlim) {
3338 		vm_map_unlock_read(map);
3339 		return (KERN_NO_SPACE);
3340 	}
3341 
3342 	if (vm_map_lock_upgrade(map))
3343 		goto Retry;
3344 
3345 	if (stack_entry == next_entry) {
3346 		/*
3347 		 * Growing downward.
3348 		 */
3349 		/* Get the preliminary new entry start value */
3350 		addr = stack_entry->start - grow_amount;
3351 
3352 		/*
3353 		 * If this puts us into the previous entry, cut back our
3354 		 * growth to the available space. Also, see the note above.
3355 		 */
3356 		if (addr < end) {
3357 			stack_entry->avail_ssize = max_grow;
3358 			addr = end;
3359 		}
3360 
3361 		rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start,
3362 		    p->p_sysent->sv_stackprot, VM_PROT_ALL, 0);
3363 
3364 		/* Adjust the available stack space by the amount we grew. */
3365 		if (rv == KERN_SUCCESS) {
3366 			if (prev_entry != &map->header)
3367 				vm_map_clip_end(map, prev_entry, addr);
3368 			new_entry = prev_entry->next;
3369 			KASSERT(new_entry == stack_entry->prev, ("foo"));
3370 			KASSERT(new_entry->end == stack_entry->start, ("foo"));
3371 			KASSERT(new_entry->start == addr, ("foo"));
3372 			grow_amount = new_entry->end - new_entry->start;
3373 			new_entry->avail_ssize = stack_entry->avail_ssize -
3374 			    grow_amount;
3375 			stack_entry->eflags &= ~MAP_ENTRY_GROWS_DOWN;
3376 			new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
3377 		}
3378 	} else {
3379 		/*
3380 		 * Growing upward.
3381 		 */
3382 		addr = stack_entry->end + grow_amount;
3383 
3384 		/*
3385 		 * If this puts us into the next entry, cut back our growth
3386 		 * to the available space. Also, see the note above.
3387 		 */
3388 		if (addr > end) {
3389 			stack_entry->avail_ssize = end - stack_entry->end;
3390 			addr = end;
3391 		}
3392 
3393 		grow_amount = addr - stack_entry->end;
3394 		uip = stack_entry->uip;
3395 		if (uip == NULL && stack_entry->object.vm_object != NULL)
3396 			uip = stack_entry->object.vm_object->uip;
3397 		if (uip != NULL && !swap_reserve_by_uid(grow_amount, uip))
3398 			rv = KERN_NO_SPACE;
3399 		/* Grow the underlying object if applicable. */
3400 		else if (stack_entry->object.vm_object == NULL ||
3401 			 vm_object_coalesce(stack_entry->object.vm_object,
3402 			 stack_entry->offset,
3403 			 (vm_size_t)(stack_entry->end - stack_entry->start),
3404 			 (vm_size_t)grow_amount, uip != NULL)) {
3405 			map->size += (addr - stack_entry->end);
3406 			/* Update the current entry. */
3407 			stack_entry->end = addr;
3408 			stack_entry->avail_ssize -= grow_amount;
3409 			vm_map_entry_resize_free(map, stack_entry);
3410 			rv = KERN_SUCCESS;
3411 
3412 			if (next_entry != &map->header)
3413 				vm_map_clip_start(map, next_entry, addr);
3414 		} else
3415 			rv = KERN_FAILURE;
3416 	}
3417 
3418 	if (rv == KERN_SUCCESS && is_procstack)
3419 		vm->vm_ssize += btoc(grow_amount);
3420 
3421 	vm_map_unlock(map);
3422 
3423 	/*
3424 	 * Heed the MAP_WIREFUTURE flag if it was set for this process.
3425 	 */
3426 	if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE)) {
3427 		vm_map_wire(map,
3428 		    (stack_entry == next_entry) ? addr : addr - grow_amount,
3429 		    (stack_entry == next_entry) ? stack_entry->start : addr,
3430 		    (p->p_flag & P_SYSTEM)
3431 		    ? VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES
3432 		    : VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
3433 	}
3434 
3435 	return (rv);
3436 }
3437 
3438 /*
3439  * Unshare the specified VM space for exec.  If other processes are
3440  * mapped to it, then create a new one.  The new vmspace is null.
3441  */
3442 int
3443 vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser)
3444 {
3445 	struct vmspace *oldvmspace = p->p_vmspace;
3446 	struct vmspace *newvmspace;
3447 
3448 	newvmspace = vmspace_alloc(minuser, maxuser);
3449 	if (newvmspace == NULL)
3450 		return (ENOMEM);
3451 	newvmspace->vm_swrss = oldvmspace->vm_swrss;
3452 	/*
3453 	 * This code is written like this for prototype purposes.  The
3454 	 * goal is to avoid running down the vmspace here, but let the
3455 	 * other process's that are still using the vmspace to finally
3456 	 * run it down.  Even though there is little or no chance of blocking
3457 	 * here, it is a good idea to keep this form for future mods.
3458 	 */
3459 	PROC_VMSPACE_LOCK(p);
3460 	p->p_vmspace = newvmspace;
3461 	PROC_VMSPACE_UNLOCK(p);
3462 	if (p == curthread->td_proc)
3463 		pmap_activate(curthread);
3464 	vmspace_free(oldvmspace);
3465 	return (0);
3466 }
3467 
3468 /*
3469  * Unshare the specified VM space for forcing COW.  This
3470  * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
3471  */
3472 int
3473 vmspace_unshare(struct proc *p)
3474 {
3475 	struct vmspace *oldvmspace = p->p_vmspace;
3476 	struct vmspace *newvmspace;
3477 	vm_ooffset_t fork_charge;
3478 
3479 	if (oldvmspace->vm_refcnt == 1)
3480 		return (0);
3481 	fork_charge = 0;
3482 	newvmspace = vmspace_fork(oldvmspace, &fork_charge);
3483 	if (newvmspace == NULL)
3484 		return (ENOMEM);
3485 	if (!swap_reserve_by_uid(fork_charge, p->p_ucred->cr_ruidinfo)) {
3486 		vmspace_free(newvmspace);
3487 		return (ENOMEM);
3488 	}
3489 	PROC_VMSPACE_LOCK(p);
3490 	p->p_vmspace = newvmspace;
3491 	PROC_VMSPACE_UNLOCK(p);
3492 	if (p == curthread->td_proc)
3493 		pmap_activate(curthread);
3494 	vmspace_free(oldvmspace);
3495 	return (0);
3496 }
3497 
3498 /*
3499  *	vm_map_lookup:
3500  *
3501  *	Finds the VM object, offset, and
3502  *	protection for a given virtual address in the
3503  *	specified map, assuming a page fault of the
3504  *	type specified.
3505  *
3506  *	Leaves the map in question locked for read; return
3507  *	values are guaranteed until a vm_map_lookup_done
3508  *	call is performed.  Note that the map argument
3509  *	is in/out; the returned map must be used in
3510  *	the call to vm_map_lookup_done.
3511  *
3512  *	A handle (out_entry) is returned for use in
3513  *	vm_map_lookup_done, to make that fast.
3514  *
3515  *	If a lookup is requested with "write protection"
3516  *	specified, the map may be changed to perform virtual
3517  *	copying operations, although the data referenced will
3518  *	remain the same.
3519  */
3520 int
3521 vm_map_lookup(vm_map_t *var_map,		/* IN/OUT */
3522 	      vm_offset_t vaddr,
3523 	      vm_prot_t fault_typea,
3524 	      vm_map_entry_t *out_entry,	/* OUT */
3525 	      vm_object_t *object,		/* OUT */
3526 	      vm_pindex_t *pindex,		/* OUT */
3527 	      vm_prot_t *out_prot,		/* OUT */
3528 	      boolean_t *wired)			/* OUT */
3529 {
3530 	vm_map_entry_t entry;
3531 	vm_map_t map = *var_map;
3532 	vm_prot_t prot;
3533 	vm_prot_t fault_type = fault_typea;
3534 	vm_object_t eobject;
3535 	struct uidinfo *uip;
3536 	vm_ooffset_t size;
3537 
3538 RetryLookup:;
3539 
3540 	vm_map_lock_read(map);
3541 
3542 	/*
3543 	 * Lookup the faulting address.
3544 	 */
3545 	if (!vm_map_lookup_entry(map, vaddr, out_entry)) {
3546 		vm_map_unlock_read(map);
3547 		return (KERN_INVALID_ADDRESS);
3548 	}
3549 
3550 	entry = *out_entry;
3551 
3552 	/*
3553 	 * Handle submaps.
3554 	 */
3555 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3556 		vm_map_t old_map = map;
3557 
3558 		*var_map = map = entry->object.sub_map;
3559 		vm_map_unlock_read(old_map);
3560 		goto RetryLookup;
3561 	}
3562 
3563 	/*
3564 	 * Check whether this task is allowed to have this page.
3565 	 */
3566 	prot = entry->protection;
3567 	fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
3568 	if ((fault_type & prot) != fault_type || prot == VM_PROT_NONE) {
3569 		vm_map_unlock_read(map);
3570 		return (KERN_PROTECTION_FAILURE);
3571 	}
3572 	if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
3573 	    (entry->eflags & MAP_ENTRY_COW) &&
3574 	    (fault_type & VM_PROT_WRITE)) {
3575 		vm_map_unlock_read(map);
3576 		return (KERN_PROTECTION_FAILURE);
3577 	}
3578 
3579 	/*
3580 	 * If this page is not pageable, we have to get it for all possible
3581 	 * accesses.
3582 	 */
3583 	*wired = (entry->wired_count != 0);
3584 	if (*wired)
3585 		fault_type = entry->protection;
3586 	size = entry->end - entry->start;
3587 	/*
3588 	 * If the entry was copy-on-write, we either ...
3589 	 */
3590 	if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3591 		/*
3592 		 * If we want to write the page, we may as well handle that
3593 		 * now since we've got the map locked.
3594 		 *
3595 		 * If we don't need to write the page, we just demote the
3596 		 * permissions allowed.
3597 		 */
3598 		if ((fault_type & VM_PROT_WRITE) != 0 ||
3599 		    (fault_typea & VM_PROT_COPY) != 0) {
3600 			/*
3601 			 * Make a new object, and place it in the object
3602 			 * chain.  Note that no new references have appeared
3603 			 * -- one just moved from the map to the new
3604 			 * object.
3605 			 */
3606 			if (vm_map_lock_upgrade(map))
3607 				goto RetryLookup;
3608 
3609 			if (entry->uip == NULL) {
3610 				/*
3611 				 * The debugger owner is charged for
3612 				 * the memory.
3613 				 */
3614 				uip = curthread->td_ucred->cr_ruidinfo;
3615 				uihold(uip);
3616 				if (!swap_reserve_by_uid(size, uip)) {
3617 					uifree(uip);
3618 					vm_map_unlock(map);
3619 					return (KERN_RESOURCE_SHORTAGE);
3620 				}
3621 				entry->uip = uip;
3622 			}
3623 			vm_object_shadow(
3624 			    &entry->object.vm_object,
3625 			    &entry->offset,
3626 			    atop(size));
3627 			entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
3628 			eobject = entry->object.vm_object;
3629 			if (eobject->uip != NULL) {
3630 				/*
3631 				 * The object was not shadowed.
3632 				 */
3633 				swap_release_by_uid(size, entry->uip);
3634 				uifree(entry->uip);
3635 				entry->uip = NULL;
3636 			} else if (entry->uip != NULL) {
3637 				VM_OBJECT_LOCK(eobject);
3638 				eobject->uip = entry->uip;
3639 				eobject->charge = size;
3640 				VM_OBJECT_UNLOCK(eobject);
3641 				entry->uip = NULL;
3642 			}
3643 
3644 			vm_map_lock_downgrade(map);
3645 		} else {
3646 			/*
3647 			 * We're attempting to read a copy-on-write page --
3648 			 * don't allow writes.
3649 			 */
3650 			prot &= ~VM_PROT_WRITE;
3651 		}
3652 	}
3653 
3654 	/*
3655 	 * Create an object if necessary.
3656 	 */
3657 	if (entry->object.vm_object == NULL &&
3658 	    !map->system_map) {
3659 		if (vm_map_lock_upgrade(map))
3660 			goto RetryLookup;
3661 		entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
3662 		    atop(size));
3663 		entry->offset = 0;
3664 		if (entry->uip != NULL) {
3665 			VM_OBJECT_LOCK(entry->object.vm_object);
3666 			entry->object.vm_object->uip = entry->uip;
3667 			entry->object.vm_object->charge = size;
3668 			VM_OBJECT_UNLOCK(entry->object.vm_object);
3669 			entry->uip = NULL;
3670 		}
3671 		vm_map_lock_downgrade(map);
3672 	}
3673 
3674 	/*
3675 	 * Return the object/offset from this entry.  If the entry was
3676 	 * copy-on-write or empty, it has been fixed up.
3677 	 */
3678 	*pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
3679 	*object = entry->object.vm_object;
3680 
3681 	*out_prot = prot;
3682 	return (KERN_SUCCESS);
3683 }
3684 
3685 /*
3686  *	vm_map_lookup_locked:
3687  *
3688  *	Lookup the faulting address.  A version of vm_map_lookup that returns
3689  *      KERN_FAILURE instead of blocking on map lock or memory allocation.
3690  */
3691 int
3692 vm_map_lookup_locked(vm_map_t *var_map,		/* IN/OUT */
3693 		     vm_offset_t vaddr,
3694 		     vm_prot_t fault_typea,
3695 		     vm_map_entry_t *out_entry,	/* OUT */
3696 		     vm_object_t *object,	/* OUT */
3697 		     vm_pindex_t *pindex,	/* OUT */
3698 		     vm_prot_t *out_prot,	/* OUT */
3699 		     boolean_t *wired)		/* OUT */
3700 {
3701 	vm_map_entry_t entry;
3702 	vm_map_t map = *var_map;
3703 	vm_prot_t prot;
3704 	vm_prot_t fault_type = fault_typea;
3705 
3706 	/*
3707 	 * Lookup the faulting address.
3708 	 */
3709 	if (!vm_map_lookup_entry(map, vaddr, out_entry))
3710 		return (KERN_INVALID_ADDRESS);
3711 
3712 	entry = *out_entry;
3713 
3714 	/*
3715 	 * Fail if the entry refers to a submap.
3716 	 */
3717 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
3718 		return (KERN_FAILURE);
3719 
3720 	/*
3721 	 * Check whether this task is allowed to have this page.
3722 	 */
3723 	prot = entry->protection;
3724 	fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
3725 	if ((fault_type & prot) != fault_type)
3726 		return (KERN_PROTECTION_FAILURE);
3727 	if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
3728 	    (entry->eflags & MAP_ENTRY_COW) &&
3729 	    (fault_type & VM_PROT_WRITE))
3730 		return (KERN_PROTECTION_FAILURE);
3731 
3732 	/*
3733 	 * If this page is not pageable, we have to get it for all possible
3734 	 * accesses.
3735 	 */
3736 	*wired = (entry->wired_count != 0);
3737 	if (*wired)
3738 		fault_type = entry->protection;
3739 
3740 	if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3741 		/*
3742 		 * Fail if the entry was copy-on-write for a write fault.
3743 		 */
3744 		if (fault_type & VM_PROT_WRITE)
3745 			return (KERN_FAILURE);
3746 		/*
3747 		 * We're attempting to read a copy-on-write page --
3748 		 * don't allow writes.
3749 		 */
3750 		prot &= ~VM_PROT_WRITE;
3751 	}
3752 
3753 	/*
3754 	 * Fail if an object should be created.
3755 	 */
3756 	if (entry->object.vm_object == NULL && !map->system_map)
3757 		return (KERN_FAILURE);
3758 
3759 	/*
3760 	 * Return the object/offset from this entry.  If the entry was
3761 	 * copy-on-write or empty, it has been fixed up.
3762 	 */
3763 	*pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
3764 	*object = entry->object.vm_object;
3765 
3766 	*out_prot = prot;
3767 	return (KERN_SUCCESS);
3768 }
3769 
3770 /*
3771  *	vm_map_lookup_done:
3772  *
3773  *	Releases locks acquired by a vm_map_lookup
3774  *	(according to the handle returned by that lookup).
3775  */
3776 void
3777 vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry)
3778 {
3779 	/*
3780 	 * Unlock the main-level map
3781 	 */
3782 	vm_map_unlock_read(map);
3783 }
3784 
3785 #include "opt_ddb.h"
3786 #ifdef DDB
3787 #include <sys/kernel.h>
3788 
3789 #include <ddb/ddb.h>
3790 
3791 /*
3792  *	vm_map_print:	[ debug ]
3793  */
3794 DB_SHOW_COMMAND(map, vm_map_print)
3795 {
3796 	static int nlines;
3797 	/* XXX convert args. */
3798 	vm_map_t map = (vm_map_t)addr;
3799 	boolean_t full = have_addr;
3800 
3801 	vm_map_entry_t entry;
3802 
3803 	db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
3804 	    (void *)map,
3805 	    (void *)map->pmap, map->nentries, map->timestamp);
3806 	nlines++;
3807 
3808 	if (!full && db_indent)
3809 		return;
3810 
3811 	db_indent += 2;
3812 	for (entry = map->header.next; entry != &map->header;
3813 	    entry = entry->next) {
3814 		db_iprintf("map entry %p: start=%p, end=%p\n",
3815 		    (void *)entry, (void *)entry->start, (void *)entry->end);
3816 		nlines++;
3817 		{
3818 			static char *inheritance_name[4] =
3819 			{"share", "copy", "none", "donate_copy"};
3820 
3821 			db_iprintf(" prot=%x/%x/%s",
3822 			    entry->protection,
3823 			    entry->max_protection,
3824 			    inheritance_name[(int)(unsigned char)entry->inheritance]);
3825 			if (entry->wired_count != 0)
3826 				db_printf(", wired");
3827 		}
3828 		if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3829 			db_printf(", share=%p, offset=0x%jx\n",
3830 			    (void *)entry->object.sub_map,
3831 			    (uintmax_t)entry->offset);
3832 			nlines++;
3833 			if ((entry->prev == &map->header) ||
3834 			    (entry->prev->object.sub_map !=
3835 				entry->object.sub_map)) {
3836 				db_indent += 2;
3837 				vm_map_print((db_expr_t)(intptr_t)
3838 					     entry->object.sub_map,
3839 					     full, 0, (char *)0);
3840 				db_indent -= 2;
3841 			}
3842 		} else {
3843 			if (entry->uip != NULL)
3844 				db_printf(", uip %d", entry->uip->ui_uid);
3845 			db_printf(", object=%p, offset=0x%jx",
3846 			    (void *)entry->object.vm_object,
3847 			    (uintmax_t)entry->offset);
3848 			if (entry->object.vm_object && entry->object.vm_object->uip)
3849 				db_printf(", obj uip %d charge %jx",
3850 				    entry->object.vm_object->uip->ui_uid,
3851 				    (uintmax_t)entry->object.vm_object->charge);
3852 			if (entry->eflags & MAP_ENTRY_COW)
3853 				db_printf(", copy (%s)",
3854 				    (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
3855 			db_printf("\n");
3856 			nlines++;
3857 
3858 			if ((entry->prev == &map->header) ||
3859 			    (entry->prev->object.vm_object !=
3860 				entry->object.vm_object)) {
3861 				db_indent += 2;
3862 				vm_object_print((db_expr_t)(intptr_t)
3863 						entry->object.vm_object,
3864 						full, 0, (char *)0);
3865 				nlines += 4;
3866 				db_indent -= 2;
3867 			}
3868 		}
3869 	}
3870 	db_indent -= 2;
3871 	if (db_indent == 0)
3872 		nlines = 0;
3873 }
3874 
3875 
3876 DB_SHOW_COMMAND(procvm, procvm)
3877 {
3878 	struct proc *p;
3879 
3880 	if (have_addr) {
3881 		p = (struct proc *) addr;
3882 	} else {
3883 		p = curproc;
3884 	}
3885 
3886 	db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
3887 	    (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
3888 	    (void *)vmspace_pmap(p->p_vmspace));
3889 
3890 	vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);
3891 }
3892 
3893 #endif /* DDB */
3894