xref: /freebsd/sys/vm/vm_map.c (revision 148a8da8)
1 /*-
2  * SPDX-License-Identifier: (BSD-3-Clause AND MIT-CMU)
3  *
4  * Copyright (c) 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * The Mach Operating System project at Carnegie-Mellon University.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	from: @(#)vm_map.c	8.3 (Berkeley) 1/12/94
35  *
36  *
37  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
38  * All rights reserved.
39  *
40  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
41  *
42  * Permission to use, copy, modify and distribute this software and
43  * its documentation is hereby granted, provided that both the copyright
44  * notice and this permission notice appear in all copies of the
45  * software, derivative works or modified versions, and any portions
46  * thereof, and that both notices appear in supporting documentation.
47  *
48  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
49  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
50  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
51  *
52  * Carnegie Mellon requests users of this software to return to
53  *
54  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
55  *  School of Computer Science
56  *  Carnegie Mellon University
57  *  Pittsburgh PA 15213-3890
58  *
59  * any improvements or extensions that they make and grant Carnegie the
60  * rights to redistribute these changes.
61  */
62 
63 /*
64  *	Virtual memory mapping module.
65  */
66 
67 #include <sys/cdefs.h>
68 __FBSDID("$FreeBSD$");
69 
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/kernel.h>
73 #include <sys/ktr.h>
74 #include <sys/lock.h>
75 #include <sys/mutex.h>
76 #include <sys/proc.h>
77 #include <sys/vmmeter.h>
78 #include <sys/mman.h>
79 #include <sys/vnode.h>
80 #include <sys/racct.h>
81 #include <sys/resourcevar.h>
82 #include <sys/rwlock.h>
83 #include <sys/file.h>
84 #include <sys/sysctl.h>
85 #include <sys/sysent.h>
86 #include <sys/shm.h>
87 
88 #include <vm/vm.h>
89 #include <vm/vm_param.h>
90 #include <vm/pmap.h>
91 #include <vm/vm_map.h>
92 #include <vm/vm_page.h>
93 #include <vm/vm_object.h>
94 #include <vm/vm_pager.h>
95 #include <vm/vm_kern.h>
96 #include <vm/vm_extern.h>
97 #include <vm/vnode_pager.h>
98 #include <vm/swap_pager.h>
99 #include <vm/uma.h>
100 
101 /*
102  *	Virtual memory maps provide for the mapping, protection,
103  *	and sharing of virtual memory objects.  In addition,
104  *	this module provides for an efficient virtual copy of
105  *	memory from one map to another.
106  *
107  *	Synchronization is required prior to most operations.
108  *
109  *	Maps consist of an ordered doubly-linked list of simple
110  *	entries; a self-adjusting binary search tree of these
111  *	entries is used to speed up lookups.
112  *
113  *	Since portions of maps are specified by start/end addresses,
114  *	which may not align with existing map entries, all
115  *	routines merely "clip" entries to these start/end values.
116  *	[That is, an entry is split into two, bordering at a
117  *	start or end value.]  Note that these clippings may not
118  *	always be necessary (as the two resulting entries are then
119  *	not changed); however, the clipping is done for convenience.
120  *
121  *	As mentioned above, virtual copy operations are performed
122  *	by copying VM object references from one map to
123  *	another, and then marking both regions as copy-on-write.
124  */
125 
126 static struct mtx map_sleep_mtx;
127 static uma_zone_t mapentzone;
128 static uma_zone_t kmapentzone;
129 static uma_zone_t mapzone;
130 static uma_zone_t vmspace_zone;
131 static int vmspace_zinit(void *mem, int size, int flags);
132 static int vm_map_zinit(void *mem, int ize, int flags);
133 static void _vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min,
134     vm_offset_t max);
135 static void vm_map_entry_deallocate(vm_map_entry_t entry, boolean_t system_map);
136 static void vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry);
137 static void vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry);
138 static int vm_map_growstack(vm_map_t map, vm_offset_t addr,
139     vm_map_entry_t gap_entry);
140 static void vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
141     vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags);
142 #ifdef INVARIANTS
143 static void vm_map_zdtor(void *mem, int size, void *arg);
144 static void vmspace_zdtor(void *mem, int size, void *arg);
145 #endif
146 static int vm_map_stack_locked(vm_map_t map, vm_offset_t addrbos,
147     vm_size_t max_ssize, vm_size_t growsize, vm_prot_t prot, vm_prot_t max,
148     int cow);
149 static void vm_map_wire_entry_failure(vm_map_t map, vm_map_entry_t entry,
150     vm_offset_t failed_addr);
151 
152 #define	ENTRY_CHARGED(e) ((e)->cred != NULL || \
153     ((e)->object.vm_object != NULL && (e)->object.vm_object->cred != NULL && \
154      !((e)->eflags & MAP_ENTRY_NEEDS_COPY)))
155 
156 /*
157  * PROC_VMSPACE_{UN,}LOCK() can be a noop as long as vmspaces are type
158  * stable.
159  */
160 #define PROC_VMSPACE_LOCK(p) do { } while (0)
161 #define PROC_VMSPACE_UNLOCK(p) do { } while (0)
162 
163 /*
164  *	VM_MAP_RANGE_CHECK:	[ internal use only ]
165  *
166  *	Asserts that the starting and ending region
167  *	addresses fall within the valid range of the map.
168  */
169 #define	VM_MAP_RANGE_CHECK(map, start, end)		\
170 		{					\
171 		if (start < vm_map_min(map))		\
172 			start = vm_map_min(map);	\
173 		if (end > vm_map_max(map))		\
174 			end = vm_map_max(map);		\
175 		if (start > end)			\
176 			start = end;			\
177 		}
178 
179 /*
180  *	vm_map_startup:
181  *
182  *	Initialize the vm_map module.  Must be called before
183  *	any other vm_map routines.
184  *
185  *	Map and entry structures are allocated from the general
186  *	purpose memory pool with some exceptions:
187  *
188  *	- The kernel map and kmem submap are allocated statically.
189  *	- Kernel map entries are allocated out of a static pool.
190  *
191  *	These restrictions are necessary since malloc() uses the
192  *	maps and requires map entries.
193  */
194 
195 void
196 vm_map_startup(void)
197 {
198 	mtx_init(&map_sleep_mtx, "vm map sleep mutex", NULL, MTX_DEF);
199 	mapzone = uma_zcreate("MAP", sizeof(struct vm_map), NULL,
200 #ifdef INVARIANTS
201 	    vm_map_zdtor,
202 #else
203 	    NULL,
204 #endif
205 	    vm_map_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
206 	uma_prealloc(mapzone, MAX_KMAP);
207 	kmapentzone = uma_zcreate("KMAP ENTRY", sizeof(struct vm_map_entry),
208 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
209 	    UMA_ZONE_MTXCLASS | UMA_ZONE_VM);
210 	mapentzone = uma_zcreate("MAP ENTRY", sizeof(struct vm_map_entry),
211 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
212 	vmspace_zone = uma_zcreate("VMSPACE", sizeof(struct vmspace), NULL,
213 #ifdef INVARIANTS
214 	    vmspace_zdtor,
215 #else
216 	    NULL,
217 #endif
218 	    vmspace_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
219 }
220 
221 static int
222 vmspace_zinit(void *mem, int size, int flags)
223 {
224 	struct vmspace *vm;
225 
226 	vm = (struct vmspace *)mem;
227 
228 	vm->vm_map.pmap = NULL;
229 	(void)vm_map_zinit(&vm->vm_map, sizeof(vm->vm_map), flags);
230 	PMAP_LOCK_INIT(vmspace_pmap(vm));
231 	return (0);
232 }
233 
234 static int
235 vm_map_zinit(void *mem, int size, int flags)
236 {
237 	vm_map_t map;
238 
239 	map = (vm_map_t)mem;
240 	memset(map, 0, sizeof(*map));
241 	mtx_init(&map->system_mtx, "vm map (system)", NULL, MTX_DEF | MTX_DUPOK);
242 	sx_init(&map->lock, "vm map (user)");
243 	return (0);
244 }
245 
246 #ifdef INVARIANTS
247 static void
248 vmspace_zdtor(void *mem, int size, void *arg)
249 {
250 	struct vmspace *vm;
251 
252 	vm = (struct vmspace *)mem;
253 
254 	vm_map_zdtor(&vm->vm_map, sizeof(vm->vm_map), arg);
255 }
256 static void
257 vm_map_zdtor(void *mem, int size, void *arg)
258 {
259 	vm_map_t map;
260 
261 	map = (vm_map_t)mem;
262 	KASSERT(map->nentries == 0,
263 	    ("map %p nentries == %d on free.",
264 	    map, map->nentries));
265 	KASSERT(map->size == 0,
266 	    ("map %p size == %lu on free.",
267 	    map, (unsigned long)map->size));
268 }
269 #endif	/* INVARIANTS */
270 
271 /*
272  * Allocate a vmspace structure, including a vm_map and pmap,
273  * and initialize those structures.  The refcnt is set to 1.
274  *
275  * If 'pinit' is NULL then the embedded pmap is initialized via pmap_pinit().
276  */
277 struct vmspace *
278 vmspace_alloc(vm_offset_t min, vm_offset_t max, pmap_pinit_t pinit)
279 {
280 	struct vmspace *vm;
281 
282 	vm = uma_zalloc(vmspace_zone, M_WAITOK);
283 	KASSERT(vm->vm_map.pmap == NULL, ("vm_map.pmap must be NULL"));
284 	if (!pinit(vmspace_pmap(vm))) {
285 		uma_zfree(vmspace_zone, vm);
286 		return (NULL);
287 	}
288 	CTR1(KTR_VM, "vmspace_alloc: %p", vm);
289 	_vm_map_init(&vm->vm_map, vmspace_pmap(vm), min, max);
290 	vm->vm_refcnt = 1;
291 	vm->vm_shm = NULL;
292 	vm->vm_swrss = 0;
293 	vm->vm_tsize = 0;
294 	vm->vm_dsize = 0;
295 	vm->vm_ssize = 0;
296 	vm->vm_taddr = 0;
297 	vm->vm_daddr = 0;
298 	vm->vm_maxsaddr = 0;
299 	return (vm);
300 }
301 
302 #ifdef RACCT
303 static void
304 vmspace_container_reset(struct proc *p)
305 {
306 
307 	PROC_LOCK(p);
308 	racct_set(p, RACCT_DATA, 0);
309 	racct_set(p, RACCT_STACK, 0);
310 	racct_set(p, RACCT_RSS, 0);
311 	racct_set(p, RACCT_MEMLOCK, 0);
312 	racct_set(p, RACCT_VMEM, 0);
313 	PROC_UNLOCK(p);
314 }
315 #endif
316 
317 static inline void
318 vmspace_dofree(struct vmspace *vm)
319 {
320 
321 	CTR1(KTR_VM, "vmspace_free: %p", vm);
322 
323 	/*
324 	 * Make sure any SysV shm is freed, it might not have been in
325 	 * exit1().
326 	 */
327 	shmexit(vm);
328 
329 	/*
330 	 * Lock the map, to wait out all other references to it.
331 	 * Delete all of the mappings and pages they hold, then call
332 	 * the pmap module to reclaim anything left.
333 	 */
334 	(void)vm_map_remove(&vm->vm_map, vm_map_min(&vm->vm_map),
335 	    vm_map_max(&vm->vm_map));
336 
337 	pmap_release(vmspace_pmap(vm));
338 	vm->vm_map.pmap = NULL;
339 	uma_zfree(vmspace_zone, vm);
340 }
341 
342 void
343 vmspace_free(struct vmspace *vm)
344 {
345 
346 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
347 	    "vmspace_free() called");
348 
349 	if (vm->vm_refcnt == 0)
350 		panic("vmspace_free: attempt to free already freed vmspace");
351 
352 	if (atomic_fetchadd_int(&vm->vm_refcnt, -1) == 1)
353 		vmspace_dofree(vm);
354 }
355 
356 void
357 vmspace_exitfree(struct proc *p)
358 {
359 	struct vmspace *vm;
360 
361 	PROC_VMSPACE_LOCK(p);
362 	vm = p->p_vmspace;
363 	p->p_vmspace = NULL;
364 	PROC_VMSPACE_UNLOCK(p);
365 	KASSERT(vm == &vmspace0, ("vmspace_exitfree: wrong vmspace"));
366 	vmspace_free(vm);
367 }
368 
369 void
370 vmspace_exit(struct thread *td)
371 {
372 	int refcnt;
373 	struct vmspace *vm;
374 	struct proc *p;
375 
376 	/*
377 	 * Release user portion of address space.
378 	 * This releases references to vnodes,
379 	 * which could cause I/O if the file has been unlinked.
380 	 * Need to do this early enough that we can still sleep.
381 	 *
382 	 * The last exiting process to reach this point releases as
383 	 * much of the environment as it can. vmspace_dofree() is the
384 	 * slower fallback in case another process had a temporary
385 	 * reference to the vmspace.
386 	 */
387 
388 	p = td->td_proc;
389 	vm = p->p_vmspace;
390 	atomic_add_int(&vmspace0.vm_refcnt, 1);
391 	refcnt = vm->vm_refcnt;
392 	do {
393 		if (refcnt > 1 && p->p_vmspace != &vmspace0) {
394 			/* Switch now since other proc might free vmspace */
395 			PROC_VMSPACE_LOCK(p);
396 			p->p_vmspace = &vmspace0;
397 			PROC_VMSPACE_UNLOCK(p);
398 			pmap_activate(td);
399 		}
400 	} while (!atomic_fcmpset_int(&vm->vm_refcnt, &refcnt, refcnt - 1));
401 	if (refcnt == 1) {
402 		if (p->p_vmspace != vm) {
403 			/* vmspace not yet freed, switch back */
404 			PROC_VMSPACE_LOCK(p);
405 			p->p_vmspace = vm;
406 			PROC_VMSPACE_UNLOCK(p);
407 			pmap_activate(td);
408 		}
409 		pmap_remove_pages(vmspace_pmap(vm));
410 		/* Switch now since this proc will free vmspace */
411 		PROC_VMSPACE_LOCK(p);
412 		p->p_vmspace = &vmspace0;
413 		PROC_VMSPACE_UNLOCK(p);
414 		pmap_activate(td);
415 		vmspace_dofree(vm);
416 	}
417 #ifdef RACCT
418 	if (racct_enable)
419 		vmspace_container_reset(p);
420 #endif
421 }
422 
423 /* Acquire reference to vmspace owned by another process. */
424 
425 struct vmspace *
426 vmspace_acquire_ref(struct proc *p)
427 {
428 	struct vmspace *vm;
429 	int refcnt;
430 
431 	PROC_VMSPACE_LOCK(p);
432 	vm = p->p_vmspace;
433 	if (vm == NULL) {
434 		PROC_VMSPACE_UNLOCK(p);
435 		return (NULL);
436 	}
437 	refcnt = vm->vm_refcnt;
438 	do {
439 		if (refcnt <= 0) { 	/* Avoid 0->1 transition */
440 			PROC_VMSPACE_UNLOCK(p);
441 			return (NULL);
442 		}
443 	} while (!atomic_fcmpset_int(&vm->vm_refcnt, &refcnt, refcnt + 1));
444 	if (vm != p->p_vmspace) {
445 		PROC_VMSPACE_UNLOCK(p);
446 		vmspace_free(vm);
447 		return (NULL);
448 	}
449 	PROC_VMSPACE_UNLOCK(p);
450 	return (vm);
451 }
452 
453 /*
454  * Switch between vmspaces in an AIO kernel process.
455  *
456  * The AIO kernel processes switch to and from a user process's
457  * vmspace while performing an I/O operation on behalf of a user
458  * process.  The new vmspace is either the vmspace of a user process
459  * obtained from an active AIO request or the initial vmspace of the
460  * AIO kernel process (when it is idling).  Because user processes
461  * will block to drain any active AIO requests before proceeding in
462  * exit() or execve(), the vmspace reference count for these vmspaces
463  * can never be 0.  This allows for a much simpler implementation than
464  * the loop in vmspace_acquire_ref() above.  Similarly, AIO kernel
465  * processes hold an extra reference on their initial vmspace for the
466  * life of the process so that this guarantee is true for any vmspace
467  * passed as 'newvm'.
468  */
469 void
470 vmspace_switch_aio(struct vmspace *newvm)
471 {
472 	struct vmspace *oldvm;
473 
474 	/* XXX: Need some way to assert that this is an aio daemon. */
475 
476 	KASSERT(newvm->vm_refcnt > 0,
477 	    ("vmspace_switch_aio: newvm unreferenced"));
478 
479 	oldvm = curproc->p_vmspace;
480 	if (oldvm == newvm)
481 		return;
482 
483 	/*
484 	 * Point to the new address space and refer to it.
485 	 */
486 	curproc->p_vmspace = newvm;
487 	atomic_add_int(&newvm->vm_refcnt, 1);
488 
489 	/* Activate the new mapping. */
490 	pmap_activate(curthread);
491 
492 	/* Remove the daemon's reference to the old address space. */
493 	KASSERT(oldvm->vm_refcnt > 1,
494 	    ("vmspace_switch_aio: oldvm dropping last reference"));
495 	vmspace_free(oldvm);
496 }
497 
498 void
499 _vm_map_lock(vm_map_t map, const char *file, int line)
500 {
501 
502 	if (map->system_map)
503 		mtx_lock_flags_(&map->system_mtx, 0, file, line);
504 	else
505 		sx_xlock_(&map->lock, file, line);
506 	map->timestamp++;
507 }
508 
509 static void
510 vm_map_process_deferred(void)
511 {
512 	struct thread *td;
513 	vm_map_entry_t entry, next;
514 	vm_object_t object;
515 
516 	td = curthread;
517 	entry = td->td_map_def_user;
518 	td->td_map_def_user = NULL;
519 	while (entry != NULL) {
520 		next = entry->next;
521 		if ((entry->eflags & MAP_ENTRY_VN_WRITECNT) != 0) {
522 			/*
523 			 * Decrement the object's writemappings and
524 			 * possibly the vnode's v_writecount.
525 			 */
526 			KASSERT((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0,
527 			    ("Submap with writecount"));
528 			object = entry->object.vm_object;
529 			KASSERT(object != NULL, ("No object for writecount"));
530 			vnode_pager_release_writecount(object, entry->start,
531 			    entry->end);
532 		}
533 		vm_map_entry_deallocate(entry, FALSE);
534 		entry = next;
535 	}
536 }
537 
538 void
539 _vm_map_unlock(vm_map_t map, const char *file, int line)
540 {
541 
542 	if (map->system_map)
543 		mtx_unlock_flags_(&map->system_mtx, 0, file, line);
544 	else {
545 		sx_xunlock_(&map->lock, file, line);
546 		vm_map_process_deferred();
547 	}
548 }
549 
550 void
551 _vm_map_lock_read(vm_map_t map, const char *file, int line)
552 {
553 
554 	if (map->system_map)
555 		mtx_lock_flags_(&map->system_mtx, 0, file, line);
556 	else
557 		sx_slock_(&map->lock, file, line);
558 }
559 
560 void
561 _vm_map_unlock_read(vm_map_t map, const char *file, int line)
562 {
563 
564 	if (map->system_map)
565 		mtx_unlock_flags_(&map->system_mtx, 0, file, line);
566 	else {
567 		sx_sunlock_(&map->lock, file, line);
568 		vm_map_process_deferred();
569 	}
570 }
571 
572 int
573 _vm_map_trylock(vm_map_t map, const char *file, int line)
574 {
575 	int error;
576 
577 	error = map->system_map ?
578 	    !mtx_trylock_flags_(&map->system_mtx, 0, file, line) :
579 	    !sx_try_xlock_(&map->lock, file, line);
580 	if (error == 0)
581 		map->timestamp++;
582 	return (error == 0);
583 }
584 
585 int
586 _vm_map_trylock_read(vm_map_t map, const char *file, int line)
587 {
588 	int error;
589 
590 	error = map->system_map ?
591 	    !mtx_trylock_flags_(&map->system_mtx, 0, file, line) :
592 	    !sx_try_slock_(&map->lock, file, line);
593 	return (error == 0);
594 }
595 
596 /*
597  *	_vm_map_lock_upgrade:	[ internal use only ]
598  *
599  *	Tries to upgrade a read (shared) lock on the specified map to a write
600  *	(exclusive) lock.  Returns the value "0" if the upgrade succeeds and a
601  *	non-zero value if the upgrade fails.  If the upgrade fails, the map is
602  *	returned without a read or write lock held.
603  *
604  *	Requires that the map be read locked.
605  */
606 int
607 _vm_map_lock_upgrade(vm_map_t map, const char *file, int line)
608 {
609 	unsigned int last_timestamp;
610 
611 	if (map->system_map) {
612 		mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
613 	} else {
614 		if (!sx_try_upgrade_(&map->lock, file, line)) {
615 			last_timestamp = map->timestamp;
616 			sx_sunlock_(&map->lock, file, line);
617 			vm_map_process_deferred();
618 			/*
619 			 * If the map's timestamp does not change while the
620 			 * map is unlocked, then the upgrade succeeds.
621 			 */
622 			sx_xlock_(&map->lock, file, line);
623 			if (last_timestamp != map->timestamp) {
624 				sx_xunlock_(&map->lock, file, line);
625 				return (1);
626 			}
627 		}
628 	}
629 	map->timestamp++;
630 	return (0);
631 }
632 
633 void
634 _vm_map_lock_downgrade(vm_map_t map, const char *file, int line)
635 {
636 
637 	if (map->system_map) {
638 		mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
639 	} else
640 		sx_downgrade_(&map->lock, file, line);
641 }
642 
643 /*
644  *	vm_map_locked:
645  *
646  *	Returns a non-zero value if the caller holds a write (exclusive) lock
647  *	on the specified map and the value "0" otherwise.
648  */
649 int
650 vm_map_locked(vm_map_t map)
651 {
652 
653 	if (map->system_map)
654 		return (mtx_owned(&map->system_mtx));
655 	else
656 		return (sx_xlocked(&map->lock));
657 }
658 
659 #ifdef INVARIANTS
660 static void
661 _vm_map_assert_locked(vm_map_t map, const char *file, int line)
662 {
663 
664 	if (map->system_map)
665 		mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
666 	else
667 		sx_assert_(&map->lock, SA_XLOCKED, file, line);
668 }
669 
670 #define	VM_MAP_ASSERT_LOCKED(map) \
671     _vm_map_assert_locked(map, LOCK_FILE, LOCK_LINE)
672 
673 #ifdef DIAGNOSTIC
674 static int enable_vmmap_check = 1;
675 #else
676 static int enable_vmmap_check = 0;
677 #endif
678 SYSCTL_INT(_debug, OID_AUTO, vmmap_check, CTLFLAG_RWTUN,
679     &enable_vmmap_check, 0, "Enable vm map consistency checking");
680 
681 static void
682 _vm_map_assert_consistent(vm_map_t map)
683 {
684 	vm_map_entry_t entry;
685 	vm_map_entry_t child;
686 	vm_size_t max_left, max_right;
687 
688 	if (!enable_vmmap_check)
689 		return;
690 
691 	for (entry = map->header.next; entry != &map->header;
692 	    entry = entry->next) {
693 		KASSERT(entry->prev->end <= entry->start,
694 		    ("map %p prev->end = %jx, start = %jx", map,
695 		    (uintmax_t)entry->prev->end, (uintmax_t)entry->start));
696 		KASSERT(entry->start < entry->end,
697 		    ("map %p start = %jx, end = %jx", map,
698 		    (uintmax_t)entry->start, (uintmax_t)entry->end));
699 		KASSERT(entry->end <= entry->next->start,
700 		    ("map %p end = %jx, next->start = %jx", map,
701 		    (uintmax_t)entry->end, (uintmax_t)entry->next->start));
702 		KASSERT(entry->left == NULL ||
703 		    entry->left->start < entry->start,
704 		    ("map %p left->start = %jx, start = %jx", map,
705 		    (uintmax_t)entry->left->start, (uintmax_t)entry->start));
706 		KASSERT(entry->right == NULL ||
707 		    entry->start < entry->right->start,
708 		    ("map %p start = %jx, right->start = %jx", map,
709 		    (uintmax_t)entry->start, (uintmax_t)entry->right->start));
710 		child = entry->left;
711 		max_left = (child != NULL) ? child->max_free :
712 			entry->start - entry->prev->end;
713 		child = entry->right;
714 		max_right = (child != NULL) ? child->max_free :
715 			entry->next->start - entry->end;
716 		KASSERT(entry->max_free == MAX(max_left, max_right),
717 		    ("map %p max = %jx, max_left = %jx, max_right = %jx", map,
718 		     (uintmax_t)entry->max_free,
719 		     (uintmax_t)max_left, (uintmax_t)max_right));
720 	}
721 }
722 
723 #define VM_MAP_ASSERT_CONSISTENT(map) \
724     _vm_map_assert_consistent(map)
725 #else
726 #define	VM_MAP_ASSERT_LOCKED(map)
727 #define VM_MAP_ASSERT_CONSISTENT(map)
728 #endif /* INVARIANTS */
729 
730 /*
731  *	_vm_map_unlock_and_wait:
732  *
733  *	Atomically releases the lock on the specified map and puts the calling
734  *	thread to sleep.  The calling thread will remain asleep until either
735  *	vm_map_wakeup() is performed on the map or the specified timeout is
736  *	exceeded.
737  *
738  *	WARNING!  This function does not perform deferred deallocations of
739  *	objects and map	entries.  Therefore, the calling thread is expected to
740  *	reacquire the map lock after reawakening and later perform an ordinary
741  *	unlock operation, such as vm_map_unlock(), before completing its
742  *	operation on the map.
743  */
744 int
745 _vm_map_unlock_and_wait(vm_map_t map, int timo, const char *file, int line)
746 {
747 
748 	mtx_lock(&map_sleep_mtx);
749 	if (map->system_map)
750 		mtx_unlock_flags_(&map->system_mtx, 0, file, line);
751 	else
752 		sx_xunlock_(&map->lock, file, line);
753 	return (msleep(&map->root, &map_sleep_mtx, PDROP | PVM, "vmmaps",
754 	    timo));
755 }
756 
757 /*
758  *	vm_map_wakeup:
759  *
760  *	Awaken any threads that have slept on the map using
761  *	vm_map_unlock_and_wait().
762  */
763 void
764 vm_map_wakeup(vm_map_t map)
765 {
766 
767 	/*
768 	 * Acquire and release map_sleep_mtx to prevent a wakeup()
769 	 * from being performed (and lost) between the map unlock
770 	 * and the msleep() in _vm_map_unlock_and_wait().
771 	 */
772 	mtx_lock(&map_sleep_mtx);
773 	mtx_unlock(&map_sleep_mtx);
774 	wakeup(&map->root);
775 }
776 
777 void
778 vm_map_busy(vm_map_t map)
779 {
780 
781 	VM_MAP_ASSERT_LOCKED(map);
782 	map->busy++;
783 }
784 
785 void
786 vm_map_unbusy(vm_map_t map)
787 {
788 
789 	VM_MAP_ASSERT_LOCKED(map);
790 	KASSERT(map->busy, ("vm_map_unbusy: not busy"));
791 	if (--map->busy == 0 && (map->flags & MAP_BUSY_WAKEUP)) {
792 		vm_map_modflags(map, 0, MAP_BUSY_WAKEUP);
793 		wakeup(&map->busy);
794 	}
795 }
796 
797 void
798 vm_map_wait_busy(vm_map_t map)
799 {
800 
801 	VM_MAP_ASSERT_LOCKED(map);
802 	while (map->busy) {
803 		vm_map_modflags(map, MAP_BUSY_WAKEUP, 0);
804 		if (map->system_map)
805 			msleep(&map->busy, &map->system_mtx, 0, "mbusy", 0);
806 		else
807 			sx_sleep(&map->busy, &map->lock, 0, "mbusy", 0);
808 	}
809 	map->timestamp++;
810 }
811 
812 long
813 vmspace_resident_count(struct vmspace *vmspace)
814 {
815 	return pmap_resident_count(vmspace_pmap(vmspace));
816 }
817 
818 /*
819  *	vm_map_create:
820  *
821  *	Creates and returns a new empty VM map with
822  *	the given physical map structure, and having
823  *	the given lower and upper address bounds.
824  */
825 vm_map_t
826 vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max)
827 {
828 	vm_map_t result;
829 
830 	result = uma_zalloc(mapzone, M_WAITOK);
831 	CTR1(KTR_VM, "vm_map_create: %p", result);
832 	_vm_map_init(result, pmap, min, max);
833 	return (result);
834 }
835 
836 /*
837  * Initialize an existing vm_map structure
838  * such as that in the vmspace structure.
839  */
840 static void
841 _vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min, vm_offset_t max)
842 {
843 
844 	map->header.next = map->header.prev = &map->header;
845 	map->header.eflags = MAP_ENTRY_HEADER;
846 	map->needs_wakeup = FALSE;
847 	map->system_map = 0;
848 	map->pmap = pmap;
849 	map->header.end = min;
850 	map->header.start = max;
851 	map->flags = 0;
852 	map->root = NULL;
853 	map->timestamp = 0;
854 	map->busy = 0;
855 	map->anon_loc = 0;
856 }
857 
858 void
859 vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min, vm_offset_t max)
860 {
861 
862 	_vm_map_init(map, pmap, min, max);
863 	mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
864 	sx_init(&map->lock, "user map");
865 }
866 
867 /*
868  *	vm_map_entry_dispose:	[ internal use only ]
869  *
870  *	Inverse of vm_map_entry_create.
871  */
872 static void
873 vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry)
874 {
875 	uma_zfree(map->system_map ? kmapentzone : mapentzone, entry);
876 }
877 
878 /*
879  *	vm_map_entry_create:	[ internal use only ]
880  *
881  *	Allocates a VM map entry for insertion.
882  *	No entry fields are filled in.
883  */
884 static vm_map_entry_t
885 vm_map_entry_create(vm_map_t map)
886 {
887 	vm_map_entry_t new_entry;
888 
889 	if (map->system_map)
890 		new_entry = uma_zalloc(kmapentzone, M_NOWAIT);
891 	else
892 		new_entry = uma_zalloc(mapentzone, M_WAITOK);
893 	if (new_entry == NULL)
894 		panic("vm_map_entry_create: kernel resources exhausted");
895 	return (new_entry);
896 }
897 
898 /*
899  *	vm_map_entry_set_behavior:
900  *
901  *	Set the expected access behavior, either normal, random, or
902  *	sequential.
903  */
904 static inline void
905 vm_map_entry_set_behavior(vm_map_entry_t entry, u_char behavior)
906 {
907 	entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) |
908 	    (behavior & MAP_ENTRY_BEHAV_MASK);
909 }
910 
911 /*
912  *	vm_map_entry_set_max_free:
913  *
914  *	Set the max_free field in a vm_map_entry.
915  */
916 static inline void
917 vm_map_entry_set_max_free(vm_map_entry_t entry)
918 {
919 	vm_map_entry_t child;
920 	vm_size_t max_left, max_right;
921 
922 	child = entry->left;
923 	max_left = (child != NULL) ? child->max_free :
924 	    entry->start - entry->prev->end;
925 	child = entry->right;
926 	max_right = (child != NULL) ? child->max_free :
927 	    entry->next->start - entry->end;
928 	entry->max_free = MAX(max_left, max_right);
929 }
930 
931 #define SPLAY_LEFT_STEP(root, y, rlist, test) do {	\
932 	y = root->left;					\
933 	if (y != NULL && (test)) {			\
934 		/* Rotate right and make y root. */	\
935 		root->left = y->right;			\
936 		y->right = root;			\
937 		vm_map_entry_set_max_free(root);	\
938 		root = y;				\
939 		y = root->left;				\
940 	}						\
941 	/* Put root on rlist. */			\
942 	root->left = rlist;				\
943 	rlist = root;					\
944 	root = y;					\
945 } while (0)
946 
947 #define SPLAY_RIGHT_STEP(root, y, llist, test) do {	\
948 	y = root->right;				\
949 	if (y != NULL && (test)) {			\
950 		/* Rotate left and make y root. */	\
951 		root->right = y->left;			\
952 		y->left = root;				\
953 		vm_map_entry_set_max_free(root);	\
954 		root = y;				\
955 		y = root->right;			\
956 	}						\
957 	/* Put root on llist. */			\
958 	root->right = llist;				\
959 	llist = root;					\
960 	root = y;					\
961 } while (0)
962 
963 /*
964  * Walk down the tree until we find addr or a NULL pointer where addr would go,
965  * breaking off left and right subtrees of nodes less than, or greater than
966  * addr.  Treat pointers to nodes with max_free < length as NULL pointers.
967  * llist and rlist are the two sides in reverse order (bottom-up), with llist
968  * linked by the right pointer and rlist linked by the left pointer in the
969  * vm_map_entry.
970  */
971 static vm_map_entry_t
972 vm_map_splay_split(vm_offset_t addr, vm_size_t length,
973     vm_map_entry_t root, vm_map_entry_t *out_llist, vm_map_entry_t *out_rlist)
974 {
975 	vm_map_entry_t llist, rlist;
976 	vm_map_entry_t y;
977 
978 	llist = NULL;
979 	rlist = NULL;
980 	while (root != NULL && root->max_free >= length) {
981 		if (addr < root->start) {
982 			SPLAY_LEFT_STEP(root, y, rlist,
983 			    y->max_free >= length && addr < y->start);
984 		} else if (addr >= root->end) {
985 			SPLAY_RIGHT_STEP(root, y, llist,
986 			    y->max_free >= length && addr >= y->end);
987 		} else
988 			break;
989 	}
990 	*out_llist = llist;
991 	*out_rlist = rlist;
992 	return (root);
993 }
994 
995 static void
996 vm_map_splay_findnext(vm_map_entry_t root, vm_map_entry_t *iolist)
997 {
998 	vm_map_entry_t rlist, y;
999 
1000 	root = root->right;
1001 	rlist = *iolist;
1002 	while (root != NULL)
1003 		SPLAY_LEFT_STEP(root, y, rlist, true);
1004 	*iolist = rlist;
1005 }
1006 
1007 static void
1008 vm_map_splay_findprev(vm_map_entry_t root, vm_map_entry_t *iolist)
1009 {
1010 	vm_map_entry_t llist, y;
1011 
1012 	root = root->left;
1013 	llist = *iolist;
1014 	while (root != NULL)
1015 		SPLAY_RIGHT_STEP(root, y, llist, true);
1016 	*iolist = llist;
1017 }
1018 
1019 /*
1020  * Walk back up the two spines, flip the pointers and set max_free.  The
1021  * subtrees of the root go at the bottom of llist and rlist.
1022  */
1023 static vm_map_entry_t
1024 vm_map_splay_merge(vm_map_entry_t root,
1025     vm_map_entry_t llist, vm_map_entry_t rlist,
1026     vm_map_entry_t ltree, vm_map_entry_t rtree)
1027 {
1028 	vm_map_entry_t y;
1029 
1030 	while (llist != NULL) {
1031 		y = llist->right;
1032 		llist->right = ltree;
1033 		vm_map_entry_set_max_free(llist);
1034 		ltree = llist;
1035 		llist = y;
1036 	}
1037 	while (rlist != NULL) {
1038 		y = rlist->left;
1039 		rlist->left = rtree;
1040 		vm_map_entry_set_max_free(rlist);
1041 		rtree = rlist;
1042 		rlist = y;
1043 	}
1044 
1045 	/*
1046 	 * Final assembly: add ltree and rtree as subtrees of root.
1047 	 */
1048 	root->left = ltree;
1049 	root->right = rtree;
1050 	vm_map_entry_set_max_free(root);
1051 
1052 	return (root);
1053 }
1054 
1055 /*
1056  *	vm_map_entry_splay:
1057  *
1058  *	The Sleator and Tarjan top-down splay algorithm with the
1059  *	following variation.  Max_free must be computed bottom-up, so
1060  *	on the downward pass, maintain the left and right spines in
1061  *	reverse order.  Then, make a second pass up each side to fix
1062  *	the pointers and compute max_free.  The time bound is O(log n)
1063  *	amortized.
1064  *
1065  *	The new root is the vm_map_entry containing "addr", or else an
1066  *	adjacent entry (lower if possible) if addr is not in the tree.
1067  *
1068  *	The map must be locked, and leaves it so.
1069  *
1070  *	Returns: the new root.
1071  */
1072 static vm_map_entry_t
1073 vm_map_entry_splay(vm_offset_t addr, vm_map_entry_t root)
1074 {
1075 	vm_map_entry_t llist, rlist;
1076 
1077 	root = vm_map_splay_split(addr, 0, root, &llist, &rlist);
1078 	if (root != NULL) {
1079 		/* do nothing */
1080 	} else if (llist != NULL) {
1081 		/*
1082 		 * Recover the greatest node in the left
1083 		 * subtree and make it the root.
1084 		 */
1085 		root = llist;
1086 		llist = root->right;
1087 		root->right = NULL;
1088 	} else if (rlist != NULL) {
1089 		/*
1090 		 * Recover the least node in the right
1091 		 * subtree and make it the root.
1092 		 */
1093 		root = rlist;
1094 		rlist = root->left;
1095 		root->left = NULL;
1096 	} else {
1097 		/* There is no root. */
1098 		return (NULL);
1099 	}
1100 	return (vm_map_splay_merge(root, llist, rlist,
1101 	    root->left, root->right));
1102 }
1103 
1104 /*
1105  *	vm_map_entry_{un,}link:
1106  *
1107  *	Insert/remove entries from maps.
1108  */
1109 static void
1110 vm_map_entry_link(vm_map_t map,
1111 		  vm_map_entry_t entry)
1112 {
1113 	vm_map_entry_t llist, rlist, root;
1114 
1115 	CTR3(KTR_VM,
1116 	    "vm_map_entry_link: map %p, nentries %d, entry %p", map,
1117 	    map->nentries, entry);
1118 	VM_MAP_ASSERT_LOCKED(map);
1119 	map->nentries++;
1120 	root = map->root;
1121 	root = vm_map_splay_split(entry->start, 0, root, &llist, &rlist);
1122 	KASSERT(root == NULL,
1123 	    ("vm_map_entry_link: link object already mapped"));
1124 	entry->prev = (llist == NULL) ? &map->header : llist;
1125 	entry->next = (rlist == NULL) ? &map->header : rlist;
1126 	entry->prev->next = entry->next->prev = entry;
1127 	root = vm_map_splay_merge(entry, llist, rlist, NULL, NULL);
1128 	map->root = entry;
1129 	VM_MAP_ASSERT_CONSISTENT(map);
1130 }
1131 
1132 enum unlink_merge_type {
1133 	UNLINK_MERGE_PREV,
1134 	UNLINK_MERGE_NONE,
1135 	UNLINK_MERGE_NEXT
1136 };
1137 
1138 static void
1139 vm_map_entry_unlink(vm_map_t map,
1140 		    vm_map_entry_t entry,
1141 		    enum unlink_merge_type op)
1142 {
1143 	vm_map_entry_t llist, rlist, root, y;
1144 
1145 	VM_MAP_ASSERT_LOCKED(map);
1146 	llist = entry->prev;
1147 	rlist = entry->next;
1148 	llist->next = rlist;
1149 	rlist->prev = llist;
1150 	root = map->root;
1151 	root = vm_map_splay_split(entry->start, 0, root, &llist, &rlist);
1152 	KASSERT(root != NULL,
1153 	    ("vm_map_entry_unlink: unlink object not mapped"));
1154 
1155 	switch (op) {
1156 	case UNLINK_MERGE_PREV:
1157 		vm_map_splay_findprev(root, &llist);
1158 		llist->end = root->end;
1159 		y = root->right;
1160 		root = llist;
1161 		llist = root->right;
1162 		root->right = y;
1163 		break;
1164 	case UNLINK_MERGE_NEXT:
1165 		vm_map_splay_findnext(root, &rlist);
1166 		rlist->start = root->start;
1167 		rlist->offset = root->offset;
1168 		y = root->left;
1169 		root = rlist;
1170 		rlist = root->left;
1171 		root->left = y;
1172 		break;
1173 	case UNLINK_MERGE_NONE:
1174 		vm_map_splay_findprev(root, &llist);
1175 		vm_map_splay_findnext(root, &rlist);
1176 		if (llist != NULL) {
1177 			root = llist;
1178 			llist = root->right;
1179 			root->right = NULL;
1180 		} else if (rlist != NULL) {
1181 			root = rlist;
1182 			rlist = root->left;
1183 			root->left = NULL;
1184 		} else
1185 			root = NULL;
1186 		break;
1187 	}
1188 	if (root != NULL)
1189 		root = vm_map_splay_merge(root, llist, rlist,
1190 		    root->left, root->right);
1191 	map->root = root;
1192 	VM_MAP_ASSERT_CONSISTENT(map);
1193 	map->nentries--;
1194 	CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map,
1195 	    map->nentries, entry);
1196 }
1197 
1198 /*
1199  *	vm_map_entry_resize_free:
1200  *
1201  *	Recompute the amount of free space following a modified vm_map_entry
1202  *	and propagate those values up the tree.  Call this function after
1203  *	resizing a map entry in-place by changing the end value, without a
1204  *	call to vm_map_entry_link() or _unlink().
1205  *
1206  *	The map must be locked, and leaves it so.
1207  */
1208 static void
1209 vm_map_entry_resize_free(vm_map_t map, vm_map_entry_t entry)
1210 {
1211 	vm_map_entry_t llist, rlist, root;
1212 
1213 	VM_MAP_ASSERT_LOCKED(map);
1214 	root = map->root;
1215 	root = vm_map_splay_split(entry->start, 0, root, &llist, &rlist);
1216 	KASSERT(root != NULL,
1217 	    ("vm_map_entry_resize_free: resize_free object not mapped"));
1218 	vm_map_splay_findnext(root, &rlist);
1219 	root->right = NULL;
1220 	map->root = vm_map_splay_merge(root, llist, rlist,
1221 	    root->left, root->right);
1222 	VM_MAP_ASSERT_CONSISTENT(map);
1223 	CTR3(KTR_VM, "vm_map_entry_resize_free: map %p, nentries %d, entry %p", map,
1224 	    map->nentries, entry);
1225 }
1226 
1227 /*
1228  *	vm_map_lookup_entry:	[ internal use only ]
1229  *
1230  *	Finds the map entry containing (or
1231  *	immediately preceding) the specified address
1232  *	in the given map; the entry is returned
1233  *	in the "entry" parameter.  The boolean
1234  *	result indicates whether the address is
1235  *	actually contained in the map.
1236  */
1237 boolean_t
1238 vm_map_lookup_entry(
1239 	vm_map_t map,
1240 	vm_offset_t address,
1241 	vm_map_entry_t *entry)	/* OUT */
1242 {
1243 	vm_map_entry_t cur, lbound;
1244 	boolean_t locked;
1245 
1246 	/*
1247 	 * If the map is empty, then the map entry immediately preceding
1248 	 * "address" is the map's header.
1249 	 */
1250 	cur = map->root;
1251 	if (cur == NULL) {
1252 		*entry = &map->header;
1253 		return (FALSE);
1254 	}
1255 	if (address >= cur->start && cur->end > address) {
1256 		*entry = cur;
1257 		return (TRUE);
1258 	}
1259 	if ((locked = vm_map_locked(map)) ||
1260 	    sx_try_upgrade(&map->lock)) {
1261 		/*
1262 		 * Splay requires a write lock on the map.  However, it only
1263 		 * restructures the binary search tree; it does not otherwise
1264 		 * change the map.  Thus, the map's timestamp need not change
1265 		 * on a temporary upgrade.
1266 		 */
1267 		map->root = cur = vm_map_entry_splay(address, cur);
1268 		VM_MAP_ASSERT_CONSISTENT(map);
1269 		if (!locked)
1270 			sx_downgrade(&map->lock);
1271 
1272 		/*
1273 		 * If "address" is contained within a map entry, the new root
1274 		 * is that map entry.  Otherwise, the new root is a map entry
1275 		 * immediately before or after "address".
1276 		 */
1277 		if (address < cur->start) {
1278 			*entry = &map->header;
1279 			return (FALSE);
1280 		}
1281 		*entry = cur;
1282 		return (address < cur->end);
1283 	}
1284 	/*
1285 	 * Since the map is only locked for read access, perform a
1286 	 * standard binary search tree lookup for "address".
1287 	 */
1288 	lbound = &map->header;
1289 	do {
1290 		if (address < cur->start) {
1291 			cur = cur->left;
1292 		} else if (cur->end <= address) {
1293 			lbound = cur;
1294 			cur = cur->right;
1295 		} else {
1296 			*entry = cur;
1297 			return (TRUE);
1298 		}
1299 	} while (cur != NULL);
1300 	*entry = lbound;
1301 	return (FALSE);
1302 }
1303 
1304 /*
1305  *	vm_map_insert:
1306  *
1307  *	Inserts the given whole VM object into the target
1308  *	map at the specified address range.  The object's
1309  *	size should match that of the address range.
1310  *
1311  *	Requires that the map be locked, and leaves it so.
1312  *
1313  *	If object is non-NULL, ref count must be bumped by caller
1314  *	prior to making call to account for the new entry.
1315  */
1316 int
1317 vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1318     vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max, int cow)
1319 {
1320 	vm_map_entry_t new_entry, prev_entry, temp_entry;
1321 	struct ucred *cred;
1322 	vm_eflags_t protoeflags;
1323 	vm_inherit_t inheritance;
1324 
1325 	VM_MAP_ASSERT_LOCKED(map);
1326 	KASSERT(object != kernel_object ||
1327 	    (cow & MAP_COPY_ON_WRITE) == 0,
1328 	    ("vm_map_insert: kernel object and COW"));
1329 	KASSERT(object == NULL || (cow & MAP_NOFAULT) == 0,
1330 	    ("vm_map_insert: paradoxical MAP_NOFAULT request"));
1331 	KASSERT((prot & ~max) == 0,
1332 	    ("prot %#x is not subset of max_prot %#x", prot, max));
1333 
1334 	/*
1335 	 * Check that the start and end points are not bogus.
1336 	 */
1337 	if (start < vm_map_min(map) || end > vm_map_max(map) ||
1338 	    start >= end)
1339 		return (KERN_INVALID_ADDRESS);
1340 
1341 	/*
1342 	 * Find the entry prior to the proposed starting address; if it's part
1343 	 * of an existing entry, this range is bogus.
1344 	 */
1345 	if (vm_map_lookup_entry(map, start, &temp_entry))
1346 		return (KERN_NO_SPACE);
1347 
1348 	prev_entry = temp_entry;
1349 
1350 	/*
1351 	 * Assert that the next entry doesn't overlap the end point.
1352 	 */
1353 	if (prev_entry->next->start < end)
1354 		return (KERN_NO_SPACE);
1355 
1356 	if ((cow & MAP_CREATE_GUARD) != 0 && (object != NULL ||
1357 	    max != VM_PROT_NONE))
1358 		return (KERN_INVALID_ARGUMENT);
1359 
1360 	protoeflags = 0;
1361 	if (cow & MAP_COPY_ON_WRITE)
1362 		protoeflags |= MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY;
1363 	if (cow & MAP_NOFAULT)
1364 		protoeflags |= MAP_ENTRY_NOFAULT;
1365 	if (cow & MAP_DISABLE_SYNCER)
1366 		protoeflags |= MAP_ENTRY_NOSYNC;
1367 	if (cow & MAP_DISABLE_COREDUMP)
1368 		protoeflags |= MAP_ENTRY_NOCOREDUMP;
1369 	if (cow & MAP_STACK_GROWS_DOWN)
1370 		protoeflags |= MAP_ENTRY_GROWS_DOWN;
1371 	if (cow & MAP_STACK_GROWS_UP)
1372 		protoeflags |= MAP_ENTRY_GROWS_UP;
1373 	if (cow & MAP_VN_WRITECOUNT)
1374 		protoeflags |= MAP_ENTRY_VN_WRITECNT;
1375 	if ((cow & MAP_CREATE_GUARD) != 0)
1376 		protoeflags |= MAP_ENTRY_GUARD;
1377 	if ((cow & MAP_CREATE_STACK_GAP_DN) != 0)
1378 		protoeflags |= MAP_ENTRY_STACK_GAP_DN;
1379 	if ((cow & MAP_CREATE_STACK_GAP_UP) != 0)
1380 		protoeflags |= MAP_ENTRY_STACK_GAP_UP;
1381 	if (cow & MAP_INHERIT_SHARE)
1382 		inheritance = VM_INHERIT_SHARE;
1383 	else
1384 		inheritance = VM_INHERIT_DEFAULT;
1385 
1386 	cred = NULL;
1387 	if ((cow & (MAP_ACC_NO_CHARGE | MAP_NOFAULT | MAP_CREATE_GUARD)) != 0)
1388 		goto charged;
1389 	if ((cow & MAP_ACC_CHARGED) || ((prot & VM_PROT_WRITE) &&
1390 	    ((protoeflags & MAP_ENTRY_NEEDS_COPY) || object == NULL))) {
1391 		if (!(cow & MAP_ACC_CHARGED) && !swap_reserve(end - start))
1392 			return (KERN_RESOURCE_SHORTAGE);
1393 		KASSERT(object == NULL ||
1394 		    (protoeflags & MAP_ENTRY_NEEDS_COPY) != 0 ||
1395 		    object->cred == NULL,
1396 		    ("overcommit: vm_map_insert o %p", object));
1397 		cred = curthread->td_ucred;
1398 	}
1399 
1400 charged:
1401 	/* Expand the kernel pmap, if necessary. */
1402 	if (map == kernel_map && end > kernel_vm_end)
1403 		pmap_growkernel(end);
1404 	if (object != NULL) {
1405 		/*
1406 		 * OBJ_ONEMAPPING must be cleared unless this mapping
1407 		 * is trivially proven to be the only mapping for any
1408 		 * of the object's pages.  (Object granularity
1409 		 * reference counting is insufficient to recognize
1410 		 * aliases with precision.)
1411 		 */
1412 		VM_OBJECT_WLOCK(object);
1413 		if (object->ref_count > 1 || object->shadow_count != 0)
1414 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
1415 		VM_OBJECT_WUNLOCK(object);
1416 	} else if ((prev_entry->eflags & ~MAP_ENTRY_USER_WIRED) ==
1417 	    protoeflags &&
1418 	    (cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0 &&
1419 	    prev_entry->end == start && (prev_entry->cred == cred ||
1420 	    (prev_entry->object.vm_object != NULL &&
1421 	    prev_entry->object.vm_object->cred == cred)) &&
1422 	    vm_object_coalesce(prev_entry->object.vm_object,
1423 	    prev_entry->offset,
1424 	    (vm_size_t)(prev_entry->end - prev_entry->start),
1425 	    (vm_size_t)(end - prev_entry->end), cred != NULL &&
1426 	    (protoeflags & MAP_ENTRY_NEEDS_COPY) == 0)) {
1427 		/*
1428 		 * We were able to extend the object.  Determine if we
1429 		 * can extend the previous map entry to include the
1430 		 * new range as well.
1431 		 */
1432 		if (prev_entry->inheritance == inheritance &&
1433 		    prev_entry->protection == prot &&
1434 		    prev_entry->max_protection == max &&
1435 		    prev_entry->wired_count == 0) {
1436 			KASSERT((prev_entry->eflags & MAP_ENTRY_USER_WIRED) ==
1437 			    0, ("prev_entry %p has incoherent wiring",
1438 			    prev_entry));
1439 			if ((prev_entry->eflags & MAP_ENTRY_GUARD) == 0)
1440 				map->size += end - prev_entry->end;
1441 			prev_entry->end = end;
1442 			vm_map_entry_resize_free(map, prev_entry);
1443 			vm_map_simplify_entry(map, prev_entry);
1444 			return (KERN_SUCCESS);
1445 		}
1446 
1447 		/*
1448 		 * If we can extend the object but cannot extend the
1449 		 * map entry, we have to create a new map entry.  We
1450 		 * must bump the ref count on the extended object to
1451 		 * account for it.  object may be NULL.
1452 		 */
1453 		object = prev_entry->object.vm_object;
1454 		offset = prev_entry->offset +
1455 		    (prev_entry->end - prev_entry->start);
1456 		vm_object_reference(object);
1457 		if (cred != NULL && object != NULL && object->cred != NULL &&
1458 		    !(prev_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
1459 			/* Object already accounts for this uid. */
1460 			cred = NULL;
1461 		}
1462 	}
1463 	if (cred != NULL)
1464 		crhold(cred);
1465 
1466 	/*
1467 	 * Create a new entry
1468 	 */
1469 	new_entry = vm_map_entry_create(map);
1470 	new_entry->start = start;
1471 	new_entry->end = end;
1472 	new_entry->cred = NULL;
1473 
1474 	new_entry->eflags = protoeflags;
1475 	new_entry->object.vm_object = object;
1476 	new_entry->offset = offset;
1477 
1478 	new_entry->inheritance = inheritance;
1479 	new_entry->protection = prot;
1480 	new_entry->max_protection = max;
1481 	new_entry->wired_count = 0;
1482 	new_entry->wiring_thread = NULL;
1483 	new_entry->read_ahead = VM_FAULT_READ_AHEAD_INIT;
1484 	new_entry->next_read = start;
1485 
1486 	KASSERT(cred == NULL || !ENTRY_CHARGED(new_entry),
1487 	    ("overcommit: vm_map_insert leaks vm_map %p", new_entry));
1488 	new_entry->cred = cred;
1489 
1490 	/*
1491 	 * Insert the new entry into the list
1492 	 */
1493 	vm_map_entry_link(map, new_entry);
1494 	if ((new_entry->eflags & MAP_ENTRY_GUARD) == 0)
1495 		map->size += new_entry->end - new_entry->start;
1496 
1497 	/*
1498 	 * Try to coalesce the new entry with both the previous and next
1499 	 * entries in the list.  Previously, we only attempted to coalesce
1500 	 * with the previous entry when object is NULL.  Here, we handle the
1501 	 * other cases, which are less common.
1502 	 */
1503 	vm_map_simplify_entry(map, new_entry);
1504 
1505 	if ((cow & (MAP_PREFAULT | MAP_PREFAULT_PARTIAL)) != 0) {
1506 		vm_map_pmap_enter(map, start, prot, object, OFF_TO_IDX(offset),
1507 		    end - start, cow & MAP_PREFAULT_PARTIAL);
1508 	}
1509 
1510 	return (KERN_SUCCESS);
1511 }
1512 
1513 /*
1514  *	vm_map_findspace:
1515  *
1516  *	Find the first fit (lowest VM address) for "length" free bytes
1517  *	beginning at address >= start in the given map.
1518  *
1519  *	In a vm_map_entry, "max_free" is the maximum amount of
1520  *	contiguous free space between an entry in its subtree and a
1521  *	neighbor of that entry.  This allows finding a free region in
1522  *	one path down the tree, so O(log n) amortized with splay
1523  *	trees.
1524  *
1525  *	The map must be locked, and leaves it so.
1526  *
1527  *	Returns: starting address if sufficient space,
1528  *		 vm_map_max(map)-length+1 if insufficient space.
1529  */
1530 vm_offset_t
1531 vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length)
1532 {
1533 	vm_map_entry_t llist, rlist, root, y;
1534 	vm_size_t left_length;
1535 
1536 	/*
1537 	 * Request must fit within min/max VM address and must avoid
1538 	 * address wrap.
1539 	 */
1540 	start = MAX(start, vm_map_min(map));
1541 	if (start + length > vm_map_max(map) || start + length < start)
1542 		return (vm_map_max(map) - length + 1);
1543 
1544 	/* Empty tree means wide open address space. */
1545 	if (map->root == NULL)
1546 		return (start);
1547 
1548 	/*
1549 	 * After splay, if start comes before root node, then there
1550 	 * must be a gap from start to the root.
1551 	 */
1552 	root = vm_map_splay_split(start, length, map->root,
1553 	    &llist, &rlist);
1554 	if (root != NULL)
1555 		start = root->end;
1556 	else if (rlist != NULL) {
1557 		root = rlist;
1558 		rlist = root->left;
1559 		root->left = NULL;
1560 	} else {
1561 		root = llist;
1562 		llist = root->right;
1563 		root->right = NULL;
1564 	}
1565 	map->root = vm_map_splay_merge(root, llist, rlist,
1566 	    root->left, root->right);
1567 	VM_MAP_ASSERT_CONSISTENT(map);
1568 	if (start + length <= root->start)
1569 		return (start);
1570 
1571 	/*
1572 	 * Root is the last node that might begin its gap before
1573 	 * start, and this is the last comparison where address
1574 	 * wrap might be a problem.
1575 	 */
1576 	if (root->right == NULL &&
1577 	    start + length <= vm_map_max(map))
1578 		return (start);
1579 
1580 	/* With max_free, can immediately tell if no solution. */
1581 	if (root->right == NULL || length > root->right->max_free)
1582 		return (vm_map_max(map) - length + 1);
1583 
1584 	/*
1585 	 * Splay for the least large-enough gap in the right subtree.
1586 	 */
1587 	llist = NULL;
1588         rlist = NULL;
1589 	for (left_length = 0; ;
1590 	     left_length = root->left != NULL ?
1591 	     root->left->max_free : root->start - llist->end) {
1592 		if (length <= left_length)
1593 			SPLAY_LEFT_STEP(root, y, rlist,
1594 			    length <= (y->left != NULL ?
1595 			    y->left->max_free : y->start - llist->end));
1596 		else
1597 			SPLAY_RIGHT_STEP(root, y, llist,
1598 			    length > (y->left != NULL ?
1599 			    y->left->max_free : y->start - root->end));
1600 		if (root == NULL)
1601 			break;
1602 	}
1603 	root = llist;
1604 	llist = root->right;
1605 	if ((y = rlist) == NULL)
1606 		root->right = NULL;
1607 	else {
1608 		rlist = y->left;
1609 		y->left = NULL;
1610 		root->right = y->right;
1611 	}
1612 	root = vm_map_splay_merge(root, llist, rlist,
1613 	    root->left, root->right);
1614 	if (y != NULL) {
1615 		y->right = root->right;
1616 		vm_map_entry_set_max_free(y);
1617 		root->right = y;
1618 		vm_map_entry_set_max_free(root);
1619 	}
1620 	map->root = root;
1621 	VM_MAP_ASSERT_CONSISTENT(map);
1622 	return (root->end);
1623 }
1624 
1625 int
1626 vm_map_fixed(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1627     vm_offset_t start, vm_size_t length, vm_prot_t prot,
1628     vm_prot_t max, int cow)
1629 {
1630 	vm_offset_t end;
1631 	int result;
1632 
1633 	end = start + length;
1634 	KASSERT((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0 ||
1635 	    object == NULL,
1636 	    ("vm_map_fixed: non-NULL backing object for stack"));
1637 	vm_map_lock(map);
1638 	VM_MAP_RANGE_CHECK(map, start, end);
1639 	if ((cow & MAP_CHECK_EXCL) == 0)
1640 		vm_map_delete(map, start, end);
1641 	if ((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) != 0) {
1642 		result = vm_map_stack_locked(map, start, length, sgrowsiz,
1643 		    prot, max, cow);
1644 	} else {
1645 		result = vm_map_insert(map, object, offset, start, end,
1646 		    prot, max, cow);
1647 	}
1648 	vm_map_unlock(map);
1649 	return (result);
1650 }
1651 
1652 static const int aslr_pages_rnd_64[2] = {0x1000, 0x10};
1653 static const int aslr_pages_rnd_32[2] = {0x100, 0x4};
1654 
1655 static int cluster_anon = 1;
1656 SYSCTL_INT(_vm, OID_AUTO, cluster_anon, CTLFLAG_RW,
1657     &cluster_anon, 0,
1658     "Cluster anonymous mappings: 0 = no, 1 = yes if no hint, 2 = always");
1659 
1660 static bool
1661 clustering_anon_allowed(vm_offset_t addr)
1662 {
1663 
1664 	switch (cluster_anon) {
1665 	case 0:
1666 		return (false);
1667 	case 1:
1668 		return (addr == 0);
1669 	case 2:
1670 	default:
1671 		return (true);
1672 	}
1673 }
1674 
1675 static long aslr_restarts;
1676 SYSCTL_LONG(_vm, OID_AUTO, aslr_restarts, CTLFLAG_RD,
1677     &aslr_restarts, 0,
1678     "Number of aslr failures");
1679 
1680 #define	MAP_32BIT_MAX_ADDR	((vm_offset_t)1 << 31)
1681 
1682 /*
1683  * Searches for the specified amount of free space in the given map with the
1684  * specified alignment.  Performs an address-ordered, first-fit search from
1685  * the given address "*addr", with an optional upper bound "max_addr".  If the
1686  * parameter "alignment" is zero, then the alignment is computed from the
1687  * given (object, offset) pair so as to enable the greatest possible use of
1688  * superpage mappings.  Returns KERN_SUCCESS and the address of the free space
1689  * in "*addr" if successful.  Otherwise, returns KERN_NO_SPACE.
1690  *
1691  * The map must be locked.  Initially, there must be at least "length" bytes
1692  * of free space at the given address.
1693  */
1694 static int
1695 vm_map_alignspace(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1696     vm_offset_t *addr, vm_size_t length, vm_offset_t max_addr,
1697     vm_offset_t alignment)
1698 {
1699 	vm_offset_t aligned_addr, free_addr;
1700 
1701 	VM_MAP_ASSERT_LOCKED(map);
1702 	free_addr = *addr;
1703 	KASSERT(free_addr == vm_map_findspace(map, free_addr, length),
1704 	    ("caller failed to provide space %d at address %p",
1705 	     (int)length, (void*)free_addr));
1706 	for (;;) {
1707 		/*
1708 		 * At the start of every iteration, the free space at address
1709 		 * "*addr" is at least "length" bytes.
1710 		 */
1711 		if (alignment == 0)
1712 			pmap_align_superpage(object, offset, addr, length);
1713 		else if ((*addr & (alignment - 1)) != 0) {
1714 			*addr &= ~(alignment - 1);
1715 			*addr += alignment;
1716 		}
1717 		aligned_addr = *addr;
1718 		if (aligned_addr == free_addr) {
1719 			/*
1720 			 * Alignment did not change "*addr", so "*addr" must
1721 			 * still provide sufficient free space.
1722 			 */
1723 			return (KERN_SUCCESS);
1724 		}
1725 
1726 		/*
1727 		 * Test for address wrap on "*addr".  A wrapped "*addr" could
1728 		 * be a valid address, in which case vm_map_findspace() cannot
1729 		 * be relied upon to fail.
1730 		 */
1731 		if (aligned_addr < free_addr)
1732 			return (KERN_NO_SPACE);
1733 		*addr = vm_map_findspace(map, aligned_addr, length);
1734 		if (*addr + length > vm_map_max(map) ||
1735 		    (max_addr != 0 && *addr + length > max_addr))
1736 			return (KERN_NO_SPACE);
1737 		free_addr = *addr;
1738 		if (free_addr == aligned_addr) {
1739 			/*
1740 			 * If a successful call to vm_map_findspace() did not
1741 			 * change "*addr", then "*addr" must still be aligned
1742 			 * and provide sufficient free space.
1743 			 */
1744 			return (KERN_SUCCESS);
1745 		}
1746 	}
1747 }
1748 
1749 /*
1750  *	vm_map_find finds an unallocated region in the target address
1751  *	map with the given length.  The search is defined to be
1752  *	first-fit from the specified address; the region found is
1753  *	returned in the same parameter.
1754  *
1755  *	If object is non-NULL, ref count must be bumped by caller
1756  *	prior to making call to account for the new entry.
1757  */
1758 int
1759 vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1760 	    vm_offset_t *addr,	/* IN/OUT */
1761 	    vm_size_t length, vm_offset_t max_addr, int find_space,
1762 	    vm_prot_t prot, vm_prot_t max, int cow)
1763 {
1764 	vm_offset_t alignment, curr_min_addr, min_addr;
1765 	int gap, pidx, rv, try;
1766 	bool cluster, en_aslr, update_anon;
1767 
1768 	KASSERT((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0 ||
1769 	    object == NULL,
1770 	    ("vm_map_find: non-NULL backing object for stack"));
1771 	MPASS((cow & MAP_REMAP) == 0 || (find_space == VMFS_NO_SPACE &&
1772 	    (cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0));
1773 	if (find_space == VMFS_OPTIMAL_SPACE && (object == NULL ||
1774 	    (object->flags & OBJ_COLORED) == 0))
1775 		find_space = VMFS_ANY_SPACE;
1776 	if (find_space >> 8 != 0) {
1777 		KASSERT((find_space & 0xff) == 0, ("bad VMFS flags"));
1778 		alignment = (vm_offset_t)1 << (find_space >> 8);
1779 	} else
1780 		alignment = 0;
1781 	en_aslr = (map->flags & MAP_ASLR) != 0;
1782 	update_anon = cluster = clustering_anon_allowed(*addr) &&
1783 	    (map->flags & MAP_IS_SUB_MAP) == 0 && max_addr == 0 &&
1784 	    find_space != VMFS_NO_SPACE && object == NULL &&
1785 	    (cow & (MAP_INHERIT_SHARE | MAP_STACK_GROWS_UP |
1786 	    MAP_STACK_GROWS_DOWN)) == 0 && prot != PROT_NONE;
1787 	curr_min_addr = min_addr = *addr;
1788 	if (en_aslr && min_addr == 0 && !cluster &&
1789 	    find_space != VMFS_NO_SPACE &&
1790 	    (map->flags & MAP_ASLR_IGNSTART) != 0)
1791 		curr_min_addr = min_addr = vm_map_min(map);
1792 	try = 0;
1793 	vm_map_lock(map);
1794 	if (cluster) {
1795 		curr_min_addr = map->anon_loc;
1796 		if (curr_min_addr == 0)
1797 			cluster = false;
1798 	}
1799 	if (find_space != VMFS_NO_SPACE) {
1800 		KASSERT(find_space == VMFS_ANY_SPACE ||
1801 		    find_space == VMFS_OPTIMAL_SPACE ||
1802 		    find_space == VMFS_SUPER_SPACE ||
1803 		    alignment != 0, ("unexpected VMFS flag"));
1804 again:
1805 		/*
1806 		 * When creating an anonymous mapping, try clustering
1807 		 * with an existing anonymous mapping first.
1808 		 *
1809 		 * We make up to two attempts to find address space
1810 		 * for a given find_space value. The first attempt may
1811 		 * apply randomization or may cluster with an existing
1812 		 * anonymous mapping. If this first attempt fails,
1813 		 * perform a first-fit search of the available address
1814 		 * space.
1815 		 *
1816 		 * If all tries failed, and find_space is
1817 		 * VMFS_OPTIMAL_SPACE, fallback to VMFS_ANY_SPACE.
1818 		 * Again enable clustering and randomization.
1819 		 */
1820 		try++;
1821 		MPASS(try <= 2);
1822 
1823 		if (try == 2) {
1824 			/*
1825 			 * Second try: we failed either to find a
1826 			 * suitable region for randomizing the
1827 			 * allocation, or to cluster with an existing
1828 			 * mapping.  Retry with free run.
1829 			 */
1830 			curr_min_addr = (map->flags & MAP_ASLR_IGNSTART) != 0 ?
1831 			    vm_map_min(map) : min_addr;
1832 			atomic_add_long(&aslr_restarts, 1);
1833 		}
1834 
1835 		if (try == 1 && en_aslr && !cluster) {
1836 			/*
1837 			 * Find space for allocation, including
1838 			 * gap needed for later randomization.
1839 			 */
1840 			pidx = MAXPAGESIZES > 1 && pagesizes[1] != 0 &&
1841 			    (find_space == VMFS_SUPER_SPACE || find_space ==
1842 			    VMFS_OPTIMAL_SPACE) ? 1 : 0;
1843 			gap = vm_map_max(map) > MAP_32BIT_MAX_ADDR &&
1844 			    (max_addr == 0 || max_addr > MAP_32BIT_MAX_ADDR) ?
1845 			    aslr_pages_rnd_64[pidx] : aslr_pages_rnd_32[pidx];
1846 			*addr = vm_map_findspace(map, curr_min_addr,
1847 			    length + gap * pagesizes[pidx]);
1848 			if (*addr + length + gap * pagesizes[pidx] >
1849 			    vm_map_max(map))
1850 				goto again;
1851 			/* And randomize the start address. */
1852 			*addr += (arc4random() % gap) * pagesizes[pidx];
1853 			if (max_addr != 0 && *addr + length > max_addr)
1854 				goto again;
1855 		} else {
1856 			*addr = vm_map_findspace(map, curr_min_addr, length);
1857 			if (*addr + length > vm_map_max(map) ||
1858 			    (max_addr != 0 && *addr + length > max_addr)) {
1859 				if (cluster) {
1860 					cluster = false;
1861 					MPASS(try == 1);
1862 					goto again;
1863 				}
1864 				rv = KERN_NO_SPACE;
1865 				goto done;
1866 			}
1867 		}
1868 
1869 		if (find_space != VMFS_ANY_SPACE &&
1870 		    (rv = vm_map_alignspace(map, object, offset, addr, length,
1871 		    max_addr, alignment)) != KERN_SUCCESS) {
1872 			if (find_space == VMFS_OPTIMAL_SPACE) {
1873 				find_space = VMFS_ANY_SPACE;
1874 				curr_min_addr = min_addr;
1875 				cluster = update_anon;
1876 				try = 0;
1877 				goto again;
1878 			}
1879 			goto done;
1880 		}
1881 	} else if ((cow & MAP_REMAP) != 0) {
1882 		if (*addr < vm_map_min(map) ||
1883 		    *addr + length > vm_map_max(map) ||
1884 		    *addr + length <= length) {
1885 			rv = KERN_INVALID_ADDRESS;
1886 			goto done;
1887 		}
1888 		vm_map_delete(map, *addr, *addr + length);
1889 	}
1890 	if ((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) != 0) {
1891 		rv = vm_map_stack_locked(map, *addr, length, sgrowsiz, prot,
1892 		    max, cow);
1893 	} else {
1894 		rv = vm_map_insert(map, object, offset, *addr, *addr + length,
1895 		    prot, max, cow);
1896 	}
1897 	if (rv == KERN_SUCCESS && update_anon)
1898 		map->anon_loc = *addr + length;
1899 done:
1900 	vm_map_unlock(map);
1901 	return (rv);
1902 }
1903 
1904 /*
1905  *	vm_map_find_min() is a variant of vm_map_find() that takes an
1906  *	additional parameter (min_addr) and treats the given address
1907  *	(*addr) differently.  Specifically, it treats *addr as a hint
1908  *	and not as the minimum address where the mapping is created.
1909  *
1910  *	This function works in two phases.  First, it tries to
1911  *	allocate above the hint.  If that fails and the hint is
1912  *	greater than min_addr, it performs a second pass, replacing
1913  *	the hint with min_addr as the minimum address for the
1914  *	allocation.
1915  */
1916 int
1917 vm_map_find_min(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1918     vm_offset_t *addr, vm_size_t length, vm_offset_t min_addr,
1919     vm_offset_t max_addr, int find_space, vm_prot_t prot, vm_prot_t max,
1920     int cow)
1921 {
1922 	vm_offset_t hint;
1923 	int rv;
1924 
1925 	hint = *addr;
1926 	for (;;) {
1927 		rv = vm_map_find(map, object, offset, addr, length, max_addr,
1928 		    find_space, prot, max, cow);
1929 		if (rv == KERN_SUCCESS || min_addr >= hint)
1930 			return (rv);
1931 		*addr = hint = min_addr;
1932 	}
1933 }
1934 
1935 /*
1936  * A map entry with any of the following flags set must not be merged with
1937  * another entry.
1938  */
1939 #define	MAP_ENTRY_NOMERGE_MASK	(MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP | \
1940 	    MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP)
1941 
1942 static bool
1943 vm_map_mergeable_neighbors(vm_map_entry_t prev, vm_map_entry_t entry)
1944 {
1945 
1946 	KASSERT((prev->eflags & MAP_ENTRY_NOMERGE_MASK) == 0 ||
1947 	    (entry->eflags & MAP_ENTRY_NOMERGE_MASK) == 0,
1948 	    ("vm_map_mergeable_neighbors: neither %p nor %p are mergeable",
1949 	    prev, entry));
1950 	return (prev->end == entry->start &&
1951 	    prev->object.vm_object == entry->object.vm_object &&
1952 	    (prev->object.vm_object == NULL ||
1953 	    prev->offset + (prev->end - prev->start) == entry->offset) &&
1954 	    prev->eflags == entry->eflags &&
1955 	    prev->protection == entry->protection &&
1956 	    prev->max_protection == entry->max_protection &&
1957 	    prev->inheritance == entry->inheritance &&
1958 	    prev->wired_count == entry->wired_count &&
1959 	    prev->cred == entry->cred);
1960 }
1961 
1962 static void
1963 vm_map_merged_neighbor_dispose(vm_map_t map, vm_map_entry_t entry)
1964 {
1965 
1966 	/*
1967 	 * If the backing object is a vnode object, vm_object_deallocate()
1968 	 * calls vrele().  However, vrele() does not lock the vnode because
1969 	 * the vnode has additional references.  Thus, the map lock can be
1970 	 * kept without causing a lock-order reversal with the vnode lock.
1971 	 *
1972 	 * Since we count the number of virtual page mappings in
1973 	 * object->un_pager.vnp.writemappings, the writemappings value
1974 	 * should not be adjusted when the entry is disposed of.
1975 	 */
1976 	if (entry->object.vm_object != NULL)
1977 		vm_object_deallocate(entry->object.vm_object);
1978 	if (entry->cred != NULL)
1979 		crfree(entry->cred);
1980 	vm_map_entry_dispose(map, entry);
1981 }
1982 
1983 /*
1984  *	vm_map_simplify_entry:
1985  *
1986  *	Simplify the given map entry by merging with either neighbor.  This
1987  *	routine also has the ability to merge with both neighbors.
1988  *
1989  *	The map must be locked.
1990  *
1991  *	This routine guarantees that the passed entry remains valid (though
1992  *	possibly extended).  When merging, this routine may delete one or
1993  *	both neighbors.
1994  */
1995 void
1996 vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry)
1997 {
1998 	vm_map_entry_t next, prev;
1999 
2000 	if ((entry->eflags & MAP_ENTRY_NOMERGE_MASK) != 0)
2001 		return;
2002 	prev = entry->prev;
2003 	if (vm_map_mergeable_neighbors(prev, entry)) {
2004 		vm_map_entry_unlink(map, prev, UNLINK_MERGE_NEXT);
2005 		vm_map_merged_neighbor_dispose(map, prev);
2006 	}
2007 	next = entry->next;
2008 	if (vm_map_mergeable_neighbors(entry, next)) {
2009 		vm_map_entry_unlink(map, next, UNLINK_MERGE_PREV);
2010 		vm_map_merged_neighbor_dispose(map, next);
2011 	}
2012 }
2013 
2014 /*
2015  *	vm_map_clip_start:	[ internal use only ]
2016  *
2017  *	Asserts that the given entry begins at or after
2018  *	the specified address; if necessary,
2019  *	it splits the entry into two.
2020  */
2021 #define vm_map_clip_start(map, entry, startaddr) \
2022 { \
2023 	if (startaddr > entry->start) \
2024 		_vm_map_clip_start(map, entry, startaddr); \
2025 }
2026 
2027 /*
2028  *	This routine is called only when it is known that
2029  *	the entry must be split.
2030  */
2031 static void
2032 _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start)
2033 {
2034 	vm_map_entry_t new_entry;
2035 
2036 	VM_MAP_ASSERT_LOCKED(map);
2037 	KASSERT(entry->end > start && entry->start < start,
2038 	    ("_vm_map_clip_start: invalid clip of entry %p", entry));
2039 
2040 	/*
2041 	 * Split off the front portion -- note that we must insert the new
2042 	 * entry BEFORE this one, so that this entry has the specified
2043 	 * starting address.
2044 	 */
2045 	vm_map_simplify_entry(map, entry);
2046 
2047 	/*
2048 	 * If there is no object backing this entry, we might as well create
2049 	 * one now.  If we defer it, an object can get created after the map
2050 	 * is clipped, and individual objects will be created for the split-up
2051 	 * map.  This is a bit of a hack, but is also about the best place to
2052 	 * put this improvement.
2053 	 */
2054 	if (entry->object.vm_object == NULL && !map->system_map &&
2055 	    (entry->eflags & MAP_ENTRY_GUARD) == 0) {
2056 		vm_object_t object;
2057 		object = vm_object_allocate(OBJT_DEFAULT,
2058 				atop(entry->end - entry->start));
2059 		entry->object.vm_object = object;
2060 		entry->offset = 0;
2061 		if (entry->cred != NULL) {
2062 			object->cred = entry->cred;
2063 			object->charge = entry->end - entry->start;
2064 			entry->cred = NULL;
2065 		}
2066 	} else if (entry->object.vm_object != NULL &&
2067 		   ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) &&
2068 		   entry->cred != NULL) {
2069 		VM_OBJECT_WLOCK(entry->object.vm_object);
2070 		KASSERT(entry->object.vm_object->cred == NULL,
2071 		    ("OVERCOMMIT: vm_entry_clip_start: both cred e %p", entry));
2072 		entry->object.vm_object->cred = entry->cred;
2073 		entry->object.vm_object->charge = entry->end - entry->start;
2074 		VM_OBJECT_WUNLOCK(entry->object.vm_object);
2075 		entry->cred = NULL;
2076 	}
2077 
2078 	new_entry = vm_map_entry_create(map);
2079 	*new_entry = *entry;
2080 
2081 	new_entry->end = start;
2082 	entry->offset += (start - entry->start);
2083 	entry->start = start;
2084 	if (new_entry->cred != NULL)
2085 		crhold(entry->cred);
2086 
2087 	vm_map_entry_link(map, new_entry);
2088 
2089 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
2090 		vm_object_reference(new_entry->object.vm_object);
2091 		/*
2092 		 * The object->un_pager.vnp.writemappings for the
2093 		 * object of MAP_ENTRY_VN_WRITECNT type entry shall be
2094 		 * kept as is here.  The virtual pages are
2095 		 * re-distributed among the clipped entries, so the sum is
2096 		 * left the same.
2097 		 */
2098 	}
2099 }
2100 
2101 /*
2102  *	vm_map_clip_end:	[ internal use only ]
2103  *
2104  *	Asserts that the given entry ends at or before
2105  *	the specified address; if necessary,
2106  *	it splits the entry into two.
2107  */
2108 #define vm_map_clip_end(map, entry, endaddr) \
2109 { \
2110 	if ((endaddr) < (entry->end)) \
2111 		_vm_map_clip_end((map), (entry), (endaddr)); \
2112 }
2113 
2114 /*
2115  *	This routine is called only when it is known that
2116  *	the entry must be split.
2117  */
2118 static void
2119 _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end)
2120 {
2121 	vm_map_entry_t new_entry;
2122 
2123 	VM_MAP_ASSERT_LOCKED(map);
2124 	KASSERT(entry->start < end && entry->end > end,
2125 	    ("_vm_map_clip_end: invalid clip of entry %p", entry));
2126 
2127 	/*
2128 	 * If there is no object backing this entry, we might as well create
2129 	 * one now.  If we defer it, an object can get created after the map
2130 	 * is clipped, and individual objects will be created for the split-up
2131 	 * map.  This is a bit of a hack, but is also about the best place to
2132 	 * put this improvement.
2133 	 */
2134 	if (entry->object.vm_object == NULL && !map->system_map &&
2135 	    (entry->eflags & MAP_ENTRY_GUARD) == 0) {
2136 		vm_object_t object;
2137 		object = vm_object_allocate(OBJT_DEFAULT,
2138 				atop(entry->end - entry->start));
2139 		entry->object.vm_object = object;
2140 		entry->offset = 0;
2141 		if (entry->cred != NULL) {
2142 			object->cred = entry->cred;
2143 			object->charge = entry->end - entry->start;
2144 			entry->cred = NULL;
2145 		}
2146 	} else if (entry->object.vm_object != NULL &&
2147 		   ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) &&
2148 		   entry->cred != NULL) {
2149 		VM_OBJECT_WLOCK(entry->object.vm_object);
2150 		KASSERT(entry->object.vm_object->cred == NULL,
2151 		    ("OVERCOMMIT: vm_entry_clip_end: both cred e %p", entry));
2152 		entry->object.vm_object->cred = entry->cred;
2153 		entry->object.vm_object->charge = entry->end - entry->start;
2154 		VM_OBJECT_WUNLOCK(entry->object.vm_object);
2155 		entry->cred = NULL;
2156 	}
2157 
2158 	/*
2159 	 * Create a new entry and insert it AFTER the specified entry
2160 	 */
2161 	new_entry = vm_map_entry_create(map);
2162 	*new_entry = *entry;
2163 
2164 	new_entry->start = entry->end = end;
2165 	new_entry->offset += (end - entry->start);
2166 	if (new_entry->cred != NULL)
2167 		crhold(entry->cred);
2168 
2169 	vm_map_entry_link(map, new_entry);
2170 
2171 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
2172 		vm_object_reference(new_entry->object.vm_object);
2173 	}
2174 }
2175 
2176 /*
2177  *	vm_map_submap:		[ kernel use only ]
2178  *
2179  *	Mark the given range as handled by a subordinate map.
2180  *
2181  *	This range must have been created with vm_map_find,
2182  *	and no other operations may have been performed on this
2183  *	range prior to calling vm_map_submap.
2184  *
2185  *	Only a limited number of operations can be performed
2186  *	within this rage after calling vm_map_submap:
2187  *		vm_fault
2188  *	[Don't try vm_map_copy!]
2189  *
2190  *	To remove a submapping, one must first remove the
2191  *	range from the superior map, and then destroy the
2192  *	submap (if desired).  [Better yet, don't try it.]
2193  */
2194 int
2195 vm_map_submap(
2196 	vm_map_t map,
2197 	vm_offset_t start,
2198 	vm_offset_t end,
2199 	vm_map_t submap)
2200 {
2201 	vm_map_entry_t entry;
2202 	int result;
2203 
2204 	result = KERN_INVALID_ARGUMENT;
2205 
2206 	vm_map_lock(submap);
2207 	submap->flags |= MAP_IS_SUB_MAP;
2208 	vm_map_unlock(submap);
2209 
2210 	vm_map_lock(map);
2211 
2212 	VM_MAP_RANGE_CHECK(map, start, end);
2213 
2214 	if (vm_map_lookup_entry(map, start, &entry)) {
2215 		vm_map_clip_start(map, entry, start);
2216 	} else
2217 		entry = entry->next;
2218 
2219 	vm_map_clip_end(map, entry, end);
2220 
2221 	if ((entry->start == start) && (entry->end == end) &&
2222 	    ((entry->eflags & MAP_ENTRY_COW) == 0) &&
2223 	    (entry->object.vm_object == NULL)) {
2224 		entry->object.sub_map = submap;
2225 		entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
2226 		result = KERN_SUCCESS;
2227 	}
2228 	vm_map_unlock(map);
2229 
2230 	if (result != KERN_SUCCESS) {
2231 		vm_map_lock(submap);
2232 		submap->flags &= ~MAP_IS_SUB_MAP;
2233 		vm_map_unlock(submap);
2234 	}
2235 	return (result);
2236 }
2237 
2238 /*
2239  * The maximum number of pages to map if MAP_PREFAULT_PARTIAL is specified
2240  */
2241 #define	MAX_INIT_PT	96
2242 
2243 /*
2244  *	vm_map_pmap_enter:
2245  *
2246  *	Preload the specified map's pmap with mappings to the specified
2247  *	object's memory-resident pages.  No further physical pages are
2248  *	allocated, and no further virtual pages are retrieved from secondary
2249  *	storage.  If the specified flags include MAP_PREFAULT_PARTIAL, then a
2250  *	limited number of page mappings are created at the low-end of the
2251  *	specified address range.  (For this purpose, a superpage mapping
2252  *	counts as one page mapping.)  Otherwise, all resident pages within
2253  *	the specified address range are mapped.
2254  */
2255 static void
2256 vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
2257     vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags)
2258 {
2259 	vm_offset_t start;
2260 	vm_page_t p, p_start;
2261 	vm_pindex_t mask, psize, threshold, tmpidx;
2262 
2263 	if ((prot & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 || object == NULL)
2264 		return;
2265 	VM_OBJECT_RLOCK(object);
2266 	if (object->type == OBJT_DEVICE || object->type == OBJT_SG) {
2267 		VM_OBJECT_RUNLOCK(object);
2268 		VM_OBJECT_WLOCK(object);
2269 		if (object->type == OBJT_DEVICE || object->type == OBJT_SG) {
2270 			pmap_object_init_pt(map->pmap, addr, object, pindex,
2271 			    size);
2272 			VM_OBJECT_WUNLOCK(object);
2273 			return;
2274 		}
2275 		VM_OBJECT_LOCK_DOWNGRADE(object);
2276 	}
2277 
2278 	psize = atop(size);
2279 	if (psize + pindex > object->size) {
2280 		if (object->size < pindex) {
2281 			VM_OBJECT_RUNLOCK(object);
2282 			return;
2283 		}
2284 		psize = object->size - pindex;
2285 	}
2286 
2287 	start = 0;
2288 	p_start = NULL;
2289 	threshold = MAX_INIT_PT;
2290 
2291 	p = vm_page_find_least(object, pindex);
2292 	/*
2293 	 * Assert: the variable p is either (1) the page with the
2294 	 * least pindex greater than or equal to the parameter pindex
2295 	 * or (2) NULL.
2296 	 */
2297 	for (;
2298 	     p != NULL && (tmpidx = p->pindex - pindex) < psize;
2299 	     p = TAILQ_NEXT(p, listq)) {
2300 		/*
2301 		 * don't allow an madvise to blow away our really
2302 		 * free pages allocating pv entries.
2303 		 */
2304 		if (((flags & MAP_PREFAULT_MADVISE) != 0 &&
2305 		    vm_page_count_severe()) ||
2306 		    ((flags & MAP_PREFAULT_PARTIAL) != 0 &&
2307 		    tmpidx >= threshold)) {
2308 			psize = tmpidx;
2309 			break;
2310 		}
2311 		if (p->valid == VM_PAGE_BITS_ALL) {
2312 			if (p_start == NULL) {
2313 				start = addr + ptoa(tmpidx);
2314 				p_start = p;
2315 			}
2316 			/* Jump ahead if a superpage mapping is possible. */
2317 			if (p->psind > 0 && ((addr + ptoa(tmpidx)) &
2318 			    (pagesizes[p->psind] - 1)) == 0) {
2319 				mask = atop(pagesizes[p->psind]) - 1;
2320 				if (tmpidx + mask < psize &&
2321 				    vm_page_ps_test(p, PS_ALL_VALID, NULL)) {
2322 					p += mask;
2323 					threshold += mask;
2324 				}
2325 			}
2326 		} else if (p_start != NULL) {
2327 			pmap_enter_object(map->pmap, start, addr +
2328 			    ptoa(tmpidx), p_start, prot);
2329 			p_start = NULL;
2330 		}
2331 	}
2332 	if (p_start != NULL)
2333 		pmap_enter_object(map->pmap, start, addr + ptoa(psize),
2334 		    p_start, prot);
2335 	VM_OBJECT_RUNLOCK(object);
2336 }
2337 
2338 /*
2339  *	vm_map_protect:
2340  *
2341  *	Sets the protection of the specified address
2342  *	region in the target map.  If "set_max" is
2343  *	specified, the maximum protection is to be set;
2344  *	otherwise, only the current protection is affected.
2345  */
2346 int
2347 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
2348 	       vm_prot_t new_prot, boolean_t set_max)
2349 {
2350 	vm_map_entry_t current, entry;
2351 	vm_object_t obj;
2352 	struct ucred *cred;
2353 	vm_prot_t old_prot;
2354 
2355 	if (start == end)
2356 		return (KERN_SUCCESS);
2357 
2358 	vm_map_lock(map);
2359 
2360 	/*
2361 	 * Ensure that we are not concurrently wiring pages.  vm_map_wire() may
2362 	 * need to fault pages into the map and will drop the map lock while
2363 	 * doing so, and the VM object may end up in an inconsistent state if we
2364 	 * update the protection on the map entry in between faults.
2365 	 */
2366 	vm_map_wait_busy(map);
2367 
2368 	VM_MAP_RANGE_CHECK(map, start, end);
2369 
2370 	if (vm_map_lookup_entry(map, start, &entry)) {
2371 		vm_map_clip_start(map, entry, start);
2372 	} else {
2373 		entry = entry->next;
2374 	}
2375 
2376 	/*
2377 	 * Make a first pass to check for protection violations.
2378 	 */
2379 	for (current = entry; current->start < end; current = current->next) {
2380 		if ((current->eflags & MAP_ENTRY_GUARD) != 0)
2381 			continue;
2382 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
2383 			vm_map_unlock(map);
2384 			return (KERN_INVALID_ARGUMENT);
2385 		}
2386 		if ((new_prot & current->max_protection) != new_prot) {
2387 			vm_map_unlock(map);
2388 			return (KERN_PROTECTION_FAILURE);
2389 		}
2390 	}
2391 
2392 	/*
2393 	 * Do an accounting pass for private read-only mappings that
2394 	 * now will do cow due to allowed write (e.g. debugger sets
2395 	 * breakpoint on text segment)
2396 	 */
2397 	for (current = entry; current->start < end; current = current->next) {
2398 
2399 		vm_map_clip_end(map, current, end);
2400 
2401 		if (set_max ||
2402 		    ((new_prot & ~(current->protection)) & VM_PROT_WRITE) == 0 ||
2403 		    ENTRY_CHARGED(current) ||
2404 		    (current->eflags & MAP_ENTRY_GUARD) != 0) {
2405 			continue;
2406 		}
2407 
2408 		cred = curthread->td_ucred;
2409 		obj = current->object.vm_object;
2410 
2411 		if (obj == NULL || (current->eflags & MAP_ENTRY_NEEDS_COPY)) {
2412 			if (!swap_reserve(current->end - current->start)) {
2413 				vm_map_unlock(map);
2414 				return (KERN_RESOURCE_SHORTAGE);
2415 			}
2416 			crhold(cred);
2417 			current->cred = cred;
2418 			continue;
2419 		}
2420 
2421 		VM_OBJECT_WLOCK(obj);
2422 		if (obj->type != OBJT_DEFAULT && obj->type != OBJT_SWAP) {
2423 			VM_OBJECT_WUNLOCK(obj);
2424 			continue;
2425 		}
2426 
2427 		/*
2428 		 * Charge for the whole object allocation now, since
2429 		 * we cannot distinguish between non-charged and
2430 		 * charged clipped mapping of the same object later.
2431 		 */
2432 		KASSERT(obj->charge == 0,
2433 		    ("vm_map_protect: object %p overcharged (entry %p)",
2434 		    obj, current));
2435 		if (!swap_reserve(ptoa(obj->size))) {
2436 			VM_OBJECT_WUNLOCK(obj);
2437 			vm_map_unlock(map);
2438 			return (KERN_RESOURCE_SHORTAGE);
2439 		}
2440 
2441 		crhold(cred);
2442 		obj->cred = cred;
2443 		obj->charge = ptoa(obj->size);
2444 		VM_OBJECT_WUNLOCK(obj);
2445 	}
2446 
2447 	/*
2448 	 * Go back and fix up protections. [Note that clipping is not
2449 	 * necessary the second time.]
2450 	 */
2451 	for (current = entry; current->start < end; current = current->next) {
2452 		if ((current->eflags & MAP_ENTRY_GUARD) != 0)
2453 			continue;
2454 
2455 		old_prot = current->protection;
2456 
2457 		if (set_max)
2458 			current->protection =
2459 			    (current->max_protection = new_prot) &
2460 			    old_prot;
2461 		else
2462 			current->protection = new_prot;
2463 
2464 		/*
2465 		 * For user wired map entries, the normal lazy evaluation of
2466 		 * write access upgrades through soft page faults is
2467 		 * undesirable.  Instead, immediately copy any pages that are
2468 		 * copy-on-write and enable write access in the physical map.
2469 		 */
2470 		if ((current->eflags & MAP_ENTRY_USER_WIRED) != 0 &&
2471 		    (current->protection & VM_PROT_WRITE) != 0 &&
2472 		    (old_prot & VM_PROT_WRITE) == 0)
2473 			vm_fault_copy_entry(map, map, current, current, NULL);
2474 
2475 		/*
2476 		 * When restricting access, update the physical map.  Worry
2477 		 * about copy-on-write here.
2478 		 */
2479 		if ((old_prot & ~current->protection) != 0) {
2480 #define MASK(entry)	(((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
2481 							VM_PROT_ALL)
2482 			pmap_protect(map->pmap, current->start,
2483 			    current->end,
2484 			    current->protection & MASK(current));
2485 #undef	MASK
2486 		}
2487 		vm_map_simplify_entry(map, current);
2488 	}
2489 	vm_map_unlock(map);
2490 	return (KERN_SUCCESS);
2491 }
2492 
2493 /*
2494  *	vm_map_madvise:
2495  *
2496  *	This routine traverses a processes map handling the madvise
2497  *	system call.  Advisories are classified as either those effecting
2498  *	the vm_map_entry structure, or those effecting the underlying
2499  *	objects.
2500  */
2501 int
2502 vm_map_madvise(
2503 	vm_map_t map,
2504 	vm_offset_t start,
2505 	vm_offset_t end,
2506 	int behav)
2507 {
2508 	vm_map_entry_t current, entry;
2509 	bool modify_map;
2510 
2511 	/*
2512 	 * Some madvise calls directly modify the vm_map_entry, in which case
2513 	 * we need to use an exclusive lock on the map and we need to perform
2514 	 * various clipping operations.  Otherwise we only need a read-lock
2515 	 * on the map.
2516 	 */
2517 	switch(behav) {
2518 	case MADV_NORMAL:
2519 	case MADV_SEQUENTIAL:
2520 	case MADV_RANDOM:
2521 	case MADV_NOSYNC:
2522 	case MADV_AUTOSYNC:
2523 	case MADV_NOCORE:
2524 	case MADV_CORE:
2525 		if (start == end)
2526 			return (0);
2527 		modify_map = true;
2528 		vm_map_lock(map);
2529 		break;
2530 	case MADV_WILLNEED:
2531 	case MADV_DONTNEED:
2532 	case MADV_FREE:
2533 		if (start == end)
2534 			return (0);
2535 		modify_map = false;
2536 		vm_map_lock_read(map);
2537 		break;
2538 	default:
2539 		return (EINVAL);
2540 	}
2541 
2542 	/*
2543 	 * Locate starting entry and clip if necessary.
2544 	 */
2545 	VM_MAP_RANGE_CHECK(map, start, end);
2546 
2547 	if (vm_map_lookup_entry(map, start, &entry)) {
2548 		if (modify_map)
2549 			vm_map_clip_start(map, entry, start);
2550 	} else {
2551 		entry = entry->next;
2552 	}
2553 
2554 	if (modify_map) {
2555 		/*
2556 		 * madvise behaviors that are implemented in the vm_map_entry.
2557 		 *
2558 		 * We clip the vm_map_entry so that behavioral changes are
2559 		 * limited to the specified address range.
2560 		 */
2561 		for (current = entry; current->start < end;
2562 		    current = current->next) {
2563 			if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
2564 				continue;
2565 
2566 			vm_map_clip_end(map, current, end);
2567 
2568 			switch (behav) {
2569 			case MADV_NORMAL:
2570 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
2571 				break;
2572 			case MADV_SEQUENTIAL:
2573 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
2574 				break;
2575 			case MADV_RANDOM:
2576 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
2577 				break;
2578 			case MADV_NOSYNC:
2579 				current->eflags |= MAP_ENTRY_NOSYNC;
2580 				break;
2581 			case MADV_AUTOSYNC:
2582 				current->eflags &= ~MAP_ENTRY_NOSYNC;
2583 				break;
2584 			case MADV_NOCORE:
2585 				current->eflags |= MAP_ENTRY_NOCOREDUMP;
2586 				break;
2587 			case MADV_CORE:
2588 				current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
2589 				break;
2590 			default:
2591 				break;
2592 			}
2593 			vm_map_simplify_entry(map, current);
2594 		}
2595 		vm_map_unlock(map);
2596 	} else {
2597 		vm_pindex_t pstart, pend;
2598 
2599 		/*
2600 		 * madvise behaviors that are implemented in the underlying
2601 		 * vm_object.
2602 		 *
2603 		 * Since we don't clip the vm_map_entry, we have to clip
2604 		 * the vm_object pindex and count.
2605 		 */
2606 		for (current = entry; current->start < end;
2607 		    current = current->next) {
2608 			vm_offset_t useEnd, useStart;
2609 
2610 			if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
2611 				continue;
2612 
2613 			pstart = OFF_TO_IDX(current->offset);
2614 			pend = pstart + atop(current->end - current->start);
2615 			useStart = current->start;
2616 			useEnd = current->end;
2617 
2618 			if (current->start < start) {
2619 				pstart += atop(start - current->start);
2620 				useStart = start;
2621 			}
2622 			if (current->end > end) {
2623 				pend -= atop(current->end - end);
2624 				useEnd = end;
2625 			}
2626 
2627 			if (pstart >= pend)
2628 				continue;
2629 
2630 			/*
2631 			 * Perform the pmap_advise() before clearing
2632 			 * PGA_REFERENCED in vm_page_advise().  Otherwise, a
2633 			 * concurrent pmap operation, such as pmap_remove(),
2634 			 * could clear a reference in the pmap and set
2635 			 * PGA_REFERENCED on the page before the pmap_advise()
2636 			 * had completed.  Consequently, the page would appear
2637 			 * referenced based upon an old reference that
2638 			 * occurred before this pmap_advise() ran.
2639 			 */
2640 			if (behav == MADV_DONTNEED || behav == MADV_FREE)
2641 				pmap_advise(map->pmap, useStart, useEnd,
2642 				    behav);
2643 
2644 			vm_object_madvise(current->object.vm_object, pstart,
2645 			    pend, behav);
2646 
2647 			/*
2648 			 * Pre-populate paging structures in the
2649 			 * WILLNEED case.  For wired entries, the
2650 			 * paging structures are already populated.
2651 			 */
2652 			if (behav == MADV_WILLNEED &&
2653 			    current->wired_count == 0) {
2654 				vm_map_pmap_enter(map,
2655 				    useStart,
2656 				    current->protection,
2657 				    current->object.vm_object,
2658 				    pstart,
2659 				    ptoa(pend - pstart),
2660 				    MAP_PREFAULT_MADVISE
2661 				);
2662 			}
2663 		}
2664 		vm_map_unlock_read(map);
2665 	}
2666 	return (0);
2667 }
2668 
2669 
2670 /*
2671  *	vm_map_inherit:
2672  *
2673  *	Sets the inheritance of the specified address
2674  *	range in the target map.  Inheritance
2675  *	affects how the map will be shared with
2676  *	child maps at the time of vmspace_fork.
2677  */
2678 int
2679 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
2680 	       vm_inherit_t new_inheritance)
2681 {
2682 	vm_map_entry_t entry;
2683 	vm_map_entry_t temp_entry;
2684 
2685 	switch (new_inheritance) {
2686 	case VM_INHERIT_NONE:
2687 	case VM_INHERIT_COPY:
2688 	case VM_INHERIT_SHARE:
2689 	case VM_INHERIT_ZERO:
2690 		break;
2691 	default:
2692 		return (KERN_INVALID_ARGUMENT);
2693 	}
2694 	if (start == end)
2695 		return (KERN_SUCCESS);
2696 	vm_map_lock(map);
2697 	VM_MAP_RANGE_CHECK(map, start, end);
2698 	if (vm_map_lookup_entry(map, start, &temp_entry)) {
2699 		entry = temp_entry;
2700 		vm_map_clip_start(map, entry, start);
2701 	} else
2702 		entry = temp_entry->next;
2703 	while (entry->start < end) {
2704 		vm_map_clip_end(map, entry, end);
2705 		if ((entry->eflags & MAP_ENTRY_GUARD) == 0 ||
2706 		    new_inheritance != VM_INHERIT_ZERO)
2707 			entry->inheritance = new_inheritance;
2708 		vm_map_simplify_entry(map, entry);
2709 		entry = entry->next;
2710 	}
2711 	vm_map_unlock(map);
2712 	return (KERN_SUCCESS);
2713 }
2714 
2715 /*
2716  *	vm_map_unwire:
2717  *
2718  *	Implements both kernel and user unwiring.
2719  */
2720 int
2721 vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
2722     int flags)
2723 {
2724 	vm_map_entry_t entry, first_entry, tmp_entry;
2725 	vm_offset_t saved_start;
2726 	unsigned int last_timestamp;
2727 	int rv;
2728 	boolean_t need_wakeup, result, user_unwire;
2729 
2730 	if (start == end)
2731 		return (KERN_SUCCESS);
2732 	user_unwire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
2733 	vm_map_lock(map);
2734 	VM_MAP_RANGE_CHECK(map, start, end);
2735 	if (!vm_map_lookup_entry(map, start, &first_entry)) {
2736 		if (flags & VM_MAP_WIRE_HOLESOK)
2737 			first_entry = first_entry->next;
2738 		else {
2739 			vm_map_unlock(map);
2740 			return (KERN_INVALID_ADDRESS);
2741 		}
2742 	}
2743 	last_timestamp = map->timestamp;
2744 	entry = first_entry;
2745 	while (entry->start < end) {
2746 		if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
2747 			/*
2748 			 * We have not yet clipped the entry.
2749 			 */
2750 			saved_start = (start >= entry->start) ? start :
2751 			    entry->start;
2752 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2753 			if (vm_map_unlock_and_wait(map, 0)) {
2754 				/*
2755 				 * Allow interruption of user unwiring?
2756 				 */
2757 			}
2758 			vm_map_lock(map);
2759 			if (last_timestamp+1 != map->timestamp) {
2760 				/*
2761 				 * Look again for the entry because the map was
2762 				 * modified while it was unlocked.
2763 				 * Specifically, the entry may have been
2764 				 * clipped, merged, or deleted.
2765 				 */
2766 				if (!vm_map_lookup_entry(map, saved_start,
2767 				    &tmp_entry)) {
2768 					if (flags & VM_MAP_WIRE_HOLESOK)
2769 						tmp_entry = tmp_entry->next;
2770 					else {
2771 						if (saved_start == start) {
2772 							/*
2773 							 * First_entry has been deleted.
2774 							 */
2775 							vm_map_unlock(map);
2776 							return (KERN_INVALID_ADDRESS);
2777 						}
2778 						end = saved_start;
2779 						rv = KERN_INVALID_ADDRESS;
2780 						goto done;
2781 					}
2782 				}
2783 				if (entry == first_entry)
2784 					first_entry = tmp_entry;
2785 				else
2786 					first_entry = NULL;
2787 				entry = tmp_entry;
2788 			}
2789 			last_timestamp = map->timestamp;
2790 			continue;
2791 		}
2792 		vm_map_clip_start(map, entry, start);
2793 		vm_map_clip_end(map, entry, end);
2794 		/*
2795 		 * Mark the entry in case the map lock is released.  (See
2796 		 * above.)
2797 		 */
2798 		KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 &&
2799 		    entry->wiring_thread == NULL,
2800 		    ("owned map entry %p", entry));
2801 		entry->eflags |= MAP_ENTRY_IN_TRANSITION;
2802 		entry->wiring_thread = curthread;
2803 		/*
2804 		 * Check the map for holes in the specified region.
2805 		 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
2806 		 */
2807 		if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
2808 		    (entry->end < end && entry->next->start > entry->end)) {
2809 			end = entry->end;
2810 			rv = KERN_INVALID_ADDRESS;
2811 			goto done;
2812 		}
2813 		/*
2814 		 * If system unwiring, require that the entry is system wired.
2815 		 */
2816 		if (!user_unwire &&
2817 		    vm_map_entry_system_wired_count(entry) == 0) {
2818 			end = entry->end;
2819 			rv = KERN_INVALID_ARGUMENT;
2820 			goto done;
2821 		}
2822 		entry = entry->next;
2823 	}
2824 	rv = KERN_SUCCESS;
2825 done:
2826 	need_wakeup = FALSE;
2827 	if (first_entry == NULL) {
2828 		result = vm_map_lookup_entry(map, start, &first_entry);
2829 		if (!result && (flags & VM_MAP_WIRE_HOLESOK))
2830 			first_entry = first_entry->next;
2831 		else
2832 			KASSERT(result, ("vm_map_unwire: lookup failed"));
2833 	}
2834 	for (entry = first_entry; entry->start < end; entry = entry->next) {
2835 		/*
2836 		 * If VM_MAP_WIRE_HOLESOK was specified, an empty
2837 		 * space in the unwired region could have been mapped
2838 		 * while the map lock was dropped for draining
2839 		 * MAP_ENTRY_IN_TRANSITION.  Moreover, another thread
2840 		 * could be simultaneously wiring this new mapping
2841 		 * entry.  Detect these cases and skip any entries
2842 		 * marked as in transition by us.
2843 		 */
2844 		if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 ||
2845 		    entry->wiring_thread != curthread) {
2846 			KASSERT((flags & VM_MAP_WIRE_HOLESOK) != 0,
2847 			    ("vm_map_unwire: !HOLESOK and new/changed entry"));
2848 			continue;
2849 		}
2850 
2851 		if (rv == KERN_SUCCESS && (!user_unwire ||
2852 		    (entry->eflags & MAP_ENTRY_USER_WIRED))) {
2853 			if (user_unwire)
2854 				entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2855 			if (entry->wired_count == 1)
2856 				vm_map_entry_unwire(map, entry);
2857 			else
2858 				entry->wired_count--;
2859 		}
2860 		KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0,
2861 		    ("vm_map_unwire: in-transition flag missing %p", entry));
2862 		KASSERT(entry->wiring_thread == curthread,
2863 		    ("vm_map_unwire: alien wire %p", entry));
2864 		entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
2865 		entry->wiring_thread = NULL;
2866 		if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
2867 			entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
2868 			need_wakeup = TRUE;
2869 		}
2870 		vm_map_simplify_entry(map, entry);
2871 	}
2872 	vm_map_unlock(map);
2873 	if (need_wakeup)
2874 		vm_map_wakeup(map);
2875 	return (rv);
2876 }
2877 
2878 /*
2879  *	vm_map_wire_entry_failure:
2880  *
2881  *	Handle a wiring failure on the given entry.
2882  *
2883  *	The map should be locked.
2884  */
2885 static void
2886 vm_map_wire_entry_failure(vm_map_t map, vm_map_entry_t entry,
2887     vm_offset_t failed_addr)
2888 {
2889 
2890 	VM_MAP_ASSERT_LOCKED(map);
2891 	KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 &&
2892 	    entry->wired_count == 1,
2893 	    ("vm_map_wire_entry_failure: entry %p isn't being wired", entry));
2894 	KASSERT(failed_addr < entry->end,
2895 	    ("vm_map_wire_entry_failure: entry %p was fully wired", entry));
2896 
2897 	/*
2898 	 * If any pages at the start of this entry were successfully wired,
2899 	 * then unwire them.
2900 	 */
2901 	if (failed_addr > entry->start) {
2902 		pmap_unwire(map->pmap, entry->start, failed_addr);
2903 		vm_object_unwire(entry->object.vm_object, entry->offset,
2904 		    failed_addr - entry->start, PQ_ACTIVE);
2905 	}
2906 
2907 	/*
2908 	 * Assign an out-of-range value to represent the failure to wire this
2909 	 * entry.
2910 	 */
2911 	entry->wired_count = -1;
2912 }
2913 
2914 /*
2915  *	vm_map_wire:
2916  *
2917  *	Implements both kernel and user wiring.
2918  */
2919 int
2920 vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
2921     int flags)
2922 {
2923 	vm_map_entry_t entry, first_entry, tmp_entry;
2924 	vm_offset_t faddr, saved_end, saved_start;
2925 	unsigned int last_timestamp;
2926 	int rv;
2927 	boolean_t need_wakeup, result, user_wire;
2928 	vm_prot_t prot;
2929 
2930 	if (start == end)
2931 		return (KERN_SUCCESS);
2932 	prot = 0;
2933 	if (flags & VM_MAP_WIRE_WRITE)
2934 		prot |= VM_PROT_WRITE;
2935 	user_wire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
2936 	vm_map_lock(map);
2937 	VM_MAP_RANGE_CHECK(map, start, end);
2938 	if (!vm_map_lookup_entry(map, start, &first_entry)) {
2939 		if (flags & VM_MAP_WIRE_HOLESOK)
2940 			first_entry = first_entry->next;
2941 		else {
2942 			vm_map_unlock(map);
2943 			return (KERN_INVALID_ADDRESS);
2944 		}
2945 	}
2946 	last_timestamp = map->timestamp;
2947 	entry = first_entry;
2948 	while (entry->start < end) {
2949 		if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
2950 			/*
2951 			 * We have not yet clipped the entry.
2952 			 */
2953 			saved_start = (start >= entry->start) ? start :
2954 			    entry->start;
2955 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2956 			if (vm_map_unlock_and_wait(map, 0)) {
2957 				/*
2958 				 * Allow interruption of user wiring?
2959 				 */
2960 			}
2961 			vm_map_lock(map);
2962 			if (last_timestamp + 1 != map->timestamp) {
2963 				/*
2964 				 * Look again for the entry because the map was
2965 				 * modified while it was unlocked.
2966 				 * Specifically, the entry may have been
2967 				 * clipped, merged, or deleted.
2968 				 */
2969 				if (!vm_map_lookup_entry(map, saved_start,
2970 				    &tmp_entry)) {
2971 					if (flags & VM_MAP_WIRE_HOLESOK)
2972 						tmp_entry = tmp_entry->next;
2973 					else {
2974 						if (saved_start == start) {
2975 							/*
2976 							 * first_entry has been deleted.
2977 							 */
2978 							vm_map_unlock(map);
2979 							return (KERN_INVALID_ADDRESS);
2980 						}
2981 						end = saved_start;
2982 						rv = KERN_INVALID_ADDRESS;
2983 						goto done;
2984 					}
2985 				}
2986 				if (entry == first_entry)
2987 					first_entry = tmp_entry;
2988 				else
2989 					first_entry = NULL;
2990 				entry = tmp_entry;
2991 			}
2992 			last_timestamp = map->timestamp;
2993 			continue;
2994 		}
2995 		vm_map_clip_start(map, entry, start);
2996 		vm_map_clip_end(map, entry, end);
2997 		/*
2998 		 * Mark the entry in case the map lock is released.  (See
2999 		 * above.)
3000 		 */
3001 		KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 &&
3002 		    entry->wiring_thread == NULL,
3003 		    ("owned map entry %p", entry));
3004 		entry->eflags |= MAP_ENTRY_IN_TRANSITION;
3005 		entry->wiring_thread = curthread;
3006 		if ((entry->protection & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0
3007 		    || (entry->protection & prot) != prot) {
3008 			entry->eflags |= MAP_ENTRY_WIRE_SKIPPED;
3009 			if ((flags & VM_MAP_WIRE_HOLESOK) == 0) {
3010 				end = entry->end;
3011 				rv = KERN_INVALID_ADDRESS;
3012 				goto done;
3013 			}
3014 			goto next_entry;
3015 		}
3016 		if (entry->wired_count == 0) {
3017 			entry->wired_count++;
3018 			saved_start = entry->start;
3019 			saved_end = entry->end;
3020 
3021 			/*
3022 			 * Release the map lock, relying on the in-transition
3023 			 * mark.  Mark the map busy for fork.
3024 			 */
3025 			vm_map_busy(map);
3026 			vm_map_unlock(map);
3027 
3028 			faddr = saved_start;
3029 			do {
3030 				/*
3031 				 * Simulate a fault to get the page and enter
3032 				 * it into the physical map.
3033 				 */
3034 				if ((rv = vm_fault(map, faddr, VM_PROT_NONE,
3035 				    VM_FAULT_WIRE)) != KERN_SUCCESS)
3036 					break;
3037 			} while ((faddr += PAGE_SIZE) < saved_end);
3038 			vm_map_lock(map);
3039 			vm_map_unbusy(map);
3040 			if (last_timestamp + 1 != map->timestamp) {
3041 				/*
3042 				 * Look again for the entry because the map was
3043 				 * modified while it was unlocked.  The entry
3044 				 * may have been clipped, but NOT merged or
3045 				 * deleted.
3046 				 */
3047 				result = vm_map_lookup_entry(map, saved_start,
3048 				    &tmp_entry);
3049 				KASSERT(result, ("vm_map_wire: lookup failed"));
3050 				if (entry == first_entry)
3051 					first_entry = tmp_entry;
3052 				else
3053 					first_entry = NULL;
3054 				entry = tmp_entry;
3055 				while (entry->end < saved_end) {
3056 					/*
3057 					 * In case of failure, handle entries
3058 					 * that were not fully wired here;
3059 					 * fully wired entries are handled
3060 					 * later.
3061 					 */
3062 					if (rv != KERN_SUCCESS &&
3063 					    faddr < entry->end)
3064 						vm_map_wire_entry_failure(map,
3065 						    entry, faddr);
3066 					entry = entry->next;
3067 				}
3068 			}
3069 			last_timestamp = map->timestamp;
3070 			if (rv != KERN_SUCCESS) {
3071 				vm_map_wire_entry_failure(map, entry, faddr);
3072 				end = entry->end;
3073 				goto done;
3074 			}
3075 		} else if (!user_wire ||
3076 			   (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
3077 			entry->wired_count++;
3078 		}
3079 		/*
3080 		 * Check the map for holes in the specified region.
3081 		 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
3082 		 */
3083 	next_entry:
3084 		if ((flags & VM_MAP_WIRE_HOLESOK) == 0 &&
3085 		    entry->end < end && entry->next->start > entry->end) {
3086 			end = entry->end;
3087 			rv = KERN_INVALID_ADDRESS;
3088 			goto done;
3089 		}
3090 		entry = entry->next;
3091 	}
3092 	rv = KERN_SUCCESS;
3093 done:
3094 	need_wakeup = FALSE;
3095 	if (first_entry == NULL) {
3096 		result = vm_map_lookup_entry(map, start, &first_entry);
3097 		if (!result && (flags & VM_MAP_WIRE_HOLESOK))
3098 			first_entry = first_entry->next;
3099 		else
3100 			KASSERT(result, ("vm_map_wire: lookup failed"));
3101 	}
3102 	for (entry = first_entry; entry->start < end; entry = entry->next) {
3103 		/*
3104 		 * If VM_MAP_WIRE_HOLESOK was specified, an empty
3105 		 * space in the unwired region could have been mapped
3106 		 * while the map lock was dropped for faulting in the
3107 		 * pages or draining MAP_ENTRY_IN_TRANSITION.
3108 		 * Moreover, another thread could be simultaneously
3109 		 * wiring this new mapping entry.  Detect these cases
3110 		 * and skip any entries marked as in transition not by us.
3111 		 */
3112 		if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 ||
3113 		    entry->wiring_thread != curthread) {
3114 			KASSERT((flags & VM_MAP_WIRE_HOLESOK) != 0,
3115 			    ("vm_map_wire: !HOLESOK and new/changed entry"));
3116 			continue;
3117 		}
3118 
3119 		if ((entry->eflags & MAP_ENTRY_WIRE_SKIPPED) != 0)
3120 			goto next_entry_done;
3121 
3122 		if (rv == KERN_SUCCESS) {
3123 			if (user_wire)
3124 				entry->eflags |= MAP_ENTRY_USER_WIRED;
3125 		} else if (entry->wired_count == -1) {
3126 			/*
3127 			 * Wiring failed on this entry.  Thus, unwiring is
3128 			 * unnecessary.
3129 			 */
3130 			entry->wired_count = 0;
3131 		} else if (!user_wire ||
3132 		    (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
3133 			/*
3134 			 * Undo the wiring.  Wiring succeeded on this entry
3135 			 * but failed on a later entry.
3136 			 */
3137 			if (entry->wired_count == 1)
3138 				vm_map_entry_unwire(map, entry);
3139 			else
3140 				entry->wired_count--;
3141 		}
3142 	next_entry_done:
3143 		KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0,
3144 		    ("vm_map_wire: in-transition flag missing %p", entry));
3145 		KASSERT(entry->wiring_thread == curthread,
3146 		    ("vm_map_wire: alien wire %p", entry));
3147 		entry->eflags &= ~(MAP_ENTRY_IN_TRANSITION |
3148 		    MAP_ENTRY_WIRE_SKIPPED);
3149 		entry->wiring_thread = NULL;
3150 		if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
3151 			entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
3152 			need_wakeup = TRUE;
3153 		}
3154 		vm_map_simplify_entry(map, entry);
3155 	}
3156 	vm_map_unlock(map);
3157 	if (need_wakeup)
3158 		vm_map_wakeup(map);
3159 	return (rv);
3160 }
3161 
3162 /*
3163  * vm_map_sync
3164  *
3165  * Push any dirty cached pages in the address range to their pager.
3166  * If syncio is TRUE, dirty pages are written synchronously.
3167  * If invalidate is TRUE, any cached pages are freed as well.
3168  *
3169  * If the size of the region from start to end is zero, we are
3170  * supposed to flush all modified pages within the region containing
3171  * start.  Unfortunately, a region can be split or coalesced with
3172  * neighboring regions, making it difficult to determine what the
3173  * original region was.  Therefore, we approximate this requirement by
3174  * flushing the current region containing start.
3175  *
3176  * Returns an error if any part of the specified range is not mapped.
3177  */
3178 int
3179 vm_map_sync(
3180 	vm_map_t map,
3181 	vm_offset_t start,
3182 	vm_offset_t end,
3183 	boolean_t syncio,
3184 	boolean_t invalidate)
3185 {
3186 	vm_map_entry_t current;
3187 	vm_map_entry_t entry;
3188 	vm_size_t size;
3189 	vm_object_t object;
3190 	vm_ooffset_t offset;
3191 	unsigned int last_timestamp;
3192 	boolean_t failed;
3193 
3194 	vm_map_lock_read(map);
3195 	VM_MAP_RANGE_CHECK(map, start, end);
3196 	if (!vm_map_lookup_entry(map, start, &entry)) {
3197 		vm_map_unlock_read(map);
3198 		return (KERN_INVALID_ADDRESS);
3199 	} else if (start == end) {
3200 		start = entry->start;
3201 		end = entry->end;
3202 	}
3203 	/*
3204 	 * Make a first pass to check for user-wired memory and holes.
3205 	 */
3206 	for (current = entry; current->start < end; current = current->next) {
3207 		if (invalidate && (current->eflags & MAP_ENTRY_USER_WIRED)) {
3208 			vm_map_unlock_read(map);
3209 			return (KERN_INVALID_ARGUMENT);
3210 		}
3211 		if (end > current->end &&
3212 		    current->end != current->next->start) {
3213 			vm_map_unlock_read(map);
3214 			return (KERN_INVALID_ADDRESS);
3215 		}
3216 	}
3217 
3218 	if (invalidate)
3219 		pmap_remove(map->pmap, start, end);
3220 	failed = FALSE;
3221 
3222 	/*
3223 	 * Make a second pass, cleaning/uncaching pages from the indicated
3224 	 * objects as we go.
3225 	 */
3226 	for (current = entry; current->start < end;) {
3227 		offset = current->offset + (start - current->start);
3228 		size = (end <= current->end ? end : current->end) - start;
3229 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
3230 			vm_map_t smap;
3231 			vm_map_entry_t tentry;
3232 			vm_size_t tsize;
3233 
3234 			smap = current->object.sub_map;
3235 			vm_map_lock_read(smap);
3236 			(void) vm_map_lookup_entry(smap, offset, &tentry);
3237 			tsize = tentry->end - offset;
3238 			if (tsize < size)
3239 				size = tsize;
3240 			object = tentry->object.vm_object;
3241 			offset = tentry->offset + (offset - tentry->start);
3242 			vm_map_unlock_read(smap);
3243 		} else {
3244 			object = current->object.vm_object;
3245 		}
3246 		vm_object_reference(object);
3247 		last_timestamp = map->timestamp;
3248 		vm_map_unlock_read(map);
3249 		if (!vm_object_sync(object, offset, size, syncio, invalidate))
3250 			failed = TRUE;
3251 		start += size;
3252 		vm_object_deallocate(object);
3253 		vm_map_lock_read(map);
3254 		if (last_timestamp == map->timestamp ||
3255 		    !vm_map_lookup_entry(map, start, &current))
3256 			current = current->next;
3257 	}
3258 
3259 	vm_map_unlock_read(map);
3260 	return (failed ? KERN_FAILURE : KERN_SUCCESS);
3261 }
3262 
3263 /*
3264  *	vm_map_entry_unwire:	[ internal use only ]
3265  *
3266  *	Make the region specified by this entry pageable.
3267  *
3268  *	The map in question should be locked.
3269  *	[This is the reason for this routine's existence.]
3270  */
3271 static void
3272 vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
3273 {
3274 
3275 	VM_MAP_ASSERT_LOCKED(map);
3276 	KASSERT(entry->wired_count > 0,
3277 	    ("vm_map_entry_unwire: entry %p isn't wired", entry));
3278 	pmap_unwire(map->pmap, entry->start, entry->end);
3279 	vm_object_unwire(entry->object.vm_object, entry->offset, entry->end -
3280 	    entry->start, PQ_ACTIVE);
3281 	entry->wired_count = 0;
3282 }
3283 
3284 static void
3285 vm_map_entry_deallocate(vm_map_entry_t entry, boolean_t system_map)
3286 {
3287 
3288 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0)
3289 		vm_object_deallocate(entry->object.vm_object);
3290 	uma_zfree(system_map ? kmapentzone : mapentzone, entry);
3291 }
3292 
3293 /*
3294  *	vm_map_entry_delete:	[ internal use only ]
3295  *
3296  *	Deallocate the given entry from the target map.
3297  */
3298 static void
3299 vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry)
3300 {
3301 	vm_object_t object;
3302 	vm_pindex_t offidxstart, offidxend, count, size1;
3303 	vm_size_t size;
3304 
3305 	vm_map_entry_unlink(map, entry, UNLINK_MERGE_NONE);
3306 	object = entry->object.vm_object;
3307 
3308 	if ((entry->eflags & MAP_ENTRY_GUARD) != 0) {
3309 		MPASS(entry->cred == NULL);
3310 		MPASS((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0);
3311 		MPASS(object == NULL);
3312 		vm_map_entry_deallocate(entry, map->system_map);
3313 		return;
3314 	}
3315 
3316 	size = entry->end - entry->start;
3317 	map->size -= size;
3318 
3319 	if (entry->cred != NULL) {
3320 		swap_release_by_cred(size, entry->cred);
3321 		crfree(entry->cred);
3322 	}
3323 
3324 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
3325 	    (object != NULL)) {
3326 		KASSERT(entry->cred == NULL || object->cred == NULL ||
3327 		    (entry->eflags & MAP_ENTRY_NEEDS_COPY),
3328 		    ("OVERCOMMIT vm_map_entry_delete: both cred %p", entry));
3329 		count = atop(size);
3330 		offidxstart = OFF_TO_IDX(entry->offset);
3331 		offidxend = offidxstart + count;
3332 		VM_OBJECT_WLOCK(object);
3333 		if (object->ref_count != 1 && ((object->flags & (OBJ_NOSPLIT |
3334 		    OBJ_ONEMAPPING)) == OBJ_ONEMAPPING ||
3335 		    object == kernel_object)) {
3336 			vm_object_collapse(object);
3337 
3338 			/*
3339 			 * The option OBJPR_NOTMAPPED can be passed here
3340 			 * because vm_map_delete() already performed
3341 			 * pmap_remove() on the only mapping to this range
3342 			 * of pages.
3343 			 */
3344 			vm_object_page_remove(object, offidxstart, offidxend,
3345 			    OBJPR_NOTMAPPED);
3346 			if (object->type == OBJT_SWAP)
3347 				swap_pager_freespace(object, offidxstart,
3348 				    count);
3349 			if (offidxend >= object->size &&
3350 			    offidxstart < object->size) {
3351 				size1 = object->size;
3352 				object->size = offidxstart;
3353 				if (object->cred != NULL) {
3354 					size1 -= object->size;
3355 					KASSERT(object->charge >= ptoa(size1),
3356 					    ("object %p charge < 0", object));
3357 					swap_release_by_cred(ptoa(size1),
3358 					    object->cred);
3359 					object->charge -= ptoa(size1);
3360 				}
3361 			}
3362 		}
3363 		VM_OBJECT_WUNLOCK(object);
3364 	} else
3365 		entry->object.vm_object = NULL;
3366 	if (map->system_map)
3367 		vm_map_entry_deallocate(entry, TRUE);
3368 	else {
3369 		entry->next = curthread->td_map_def_user;
3370 		curthread->td_map_def_user = entry;
3371 	}
3372 }
3373 
3374 /*
3375  *	vm_map_delete:	[ internal use only ]
3376  *
3377  *	Deallocates the given address range from the target
3378  *	map.
3379  */
3380 int
3381 vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end)
3382 {
3383 	vm_map_entry_t entry;
3384 	vm_map_entry_t first_entry;
3385 
3386 	VM_MAP_ASSERT_LOCKED(map);
3387 	if (start == end)
3388 		return (KERN_SUCCESS);
3389 
3390 	/*
3391 	 * Find the start of the region, and clip it
3392 	 */
3393 	if (!vm_map_lookup_entry(map, start, &first_entry))
3394 		entry = first_entry->next;
3395 	else {
3396 		entry = first_entry;
3397 		vm_map_clip_start(map, entry, start);
3398 	}
3399 
3400 	/*
3401 	 * Step through all entries in this region
3402 	 */
3403 	while (entry->start < end) {
3404 		vm_map_entry_t next;
3405 
3406 		/*
3407 		 * Wait for wiring or unwiring of an entry to complete.
3408 		 * Also wait for any system wirings to disappear on
3409 		 * user maps.
3410 		 */
3411 		if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 ||
3412 		    (vm_map_pmap(map) != kernel_pmap &&
3413 		    vm_map_entry_system_wired_count(entry) != 0)) {
3414 			unsigned int last_timestamp;
3415 			vm_offset_t saved_start;
3416 			vm_map_entry_t tmp_entry;
3417 
3418 			saved_start = entry->start;
3419 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
3420 			last_timestamp = map->timestamp;
3421 			(void) vm_map_unlock_and_wait(map, 0);
3422 			vm_map_lock(map);
3423 			if (last_timestamp + 1 != map->timestamp) {
3424 				/*
3425 				 * Look again for the entry because the map was
3426 				 * modified while it was unlocked.
3427 				 * Specifically, the entry may have been
3428 				 * clipped, merged, or deleted.
3429 				 */
3430 				if (!vm_map_lookup_entry(map, saved_start,
3431 							 &tmp_entry))
3432 					entry = tmp_entry->next;
3433 				else {
3434 					entry = tmp_entry;
3435 					vm_map_clip_start(map, entry,
3436 							  saved_start);
3437 				}
3438 			}
3439 			continue;
3440 		}
3441 		vm_map_clip_end(map, entry, end);
3442 
3443 		next = entry->next;
3444 
3445 		/*
3446 		 * Unwire before removing addresses from the pmap; otherwise,
3447 		 * unwiring will put the entries back in the pmap.
3448 		 */
3449 		if (entry->wired_count != 0)
3450 			vm_map_entry_unwire(map, entry);
3451 
3452 		/*
3453 		 * Remove mappings for the pages, but only if the
3454 		 * mappings could exist.  For instance, it does not
3455 		 * make sense to call pmap_remove() for guard entries.
3456 		 */
3457 		if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0 ||
3458 		    entry->object.vm_object != NULL)
3459 			pmap_remove(map->pmap, entry->start, entry->end);
3460 
3461 		if (entry->end == map->anon_loc)
3462 			map->anon_loc = entry->start;
3463 
3464 		/*
3465 		 * Delete the entry only after removing all pmap
3466 		 * entries pointing to its pages.  (Otherwise, its
3467 		 * page frames may be reallocated, and any modify bits
3468 		 * will be set in the wrong object!)
3469 		 */
3470 		vm_map_entry_delete(map, entry);
3471 		entry = next;
3472 	}
3473 	return (KERN_SUCCESS);
3474 }
3475 
3476 /*
3477  *	vm_map_remove:
3478  *
3479  *	Remove the given address range from the target map.
3480  *	This is the exported form of vm_map_delete.
3481  */
3482 int
3483 vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
3484 {
3485 	int result;
3486 
3487 	vm_map_lock(map);
3488 	VM_MAP_RANGE_CHECK(map, start, end);
3489 	result = vm_map_delete(map, start, end);
3490 	vm_map_unlock(map);
3491 	return (result);
3492 }
3493 
3494 /*
3495  *	vm_map_check_protection:
3496  *
3497  *	Assert that the target map allows the specified privilege on the
3498  *	entire address region given.  The entire region must be allocated.
3499  *
3500  *	WARNING!  This code does not and should not check whether the
3501  *	contents of the region is accessible.  For example a smaller file
3502  *	might be mapped into a larger address space.
3503  *
3504  *	NOTE!  This code is also called by munmap().
3505  *
3506  *	The map must be locked.  A read lock is sufficient.
3507  */
3508 boolean_t
3509 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
3510 			vm_prot_t protection)
3511 {
3512 	vm_map_entry_t entry;
3513 	vm_map_entry_t tmp_entry;
3514 
3515 	if (!vm_map_lookup_entry(map, start, &tmp_entry))
3516 		return (FALSE);
3517 	entry = tmp_entry;
3518 
3519 	while (start < end) {
3520 		/*
3521 		 * No holes allowed!
3522 		 */
3523 		if (start < entry->start)
3524 			return (FALSE);
3525 		/*
3526 		 * Check protection associated with entry.
3527 		 */
3528 		if ((entry->protection & protection) != protection)
3529 			return (FALSE);
3530 		/* go to next entry */
3531 		start = entry->end;
3532 		entry = entry->next;
3533 	}
3534 	return (TRUE);
3535 }
3536 
3537 /*
3538  *	vm_map_copy_entry:
3539  *
3540  *	Copies the contents of the source entry to the destination
3541  *	entry.  The entries *must* be aligned properly.
3542  */
3543 static void
3544 vm_map_copy_entry(
3545 	vm_map_t src_map,
3546 	vm_map_t dst_map,
3547 	vm_map_entry_t src_entry,
3548 	vm_map_entry_t dst_entry,
3549 	vm_ooffset_t *fork_charge)
3550 {
3551 	vm_object_t src_object;
3552 	vm_map_entry_t fake_entry;
3553 	vm_offset_t size;
3554 	struct ucred *cred;
3555 	int charged;
3556 
3557 	VM_MAP_ASSERT_LOCKED(dst_map);
3558 
3559 	if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
3560 		return;
3561 
3562 	if (src_entry->wired_count == 0 ||
3563 	    (src_entry->protection & VM_PROT_WRITE) == 0) {
3564 		/*
3565 		 * If the source entry is marked needs_copy, it is already
3566 		 * write-protected.
3567 		 */
3568 		if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0 &&
3569 		    (src_entry->protection & VM_PROT_WRITE) != 0) {
3570 			pmap_protect(src_map->pmap,
3571 			    src_entry->start,
3572 			    src_entry->end,
3573 			    src_entry->protection & ~VM_PROT_WRITE);
3574 		}
3575 
3576 		/*
3577 		 * Make a copy of the object.
3578 		 */
3579 		size = src_entry->end - src_entry->start;
3580 		if ((src_object = src_entry->object.vm_object) != NULL) {
3581 			VM_OBJECT_WLOCK(src_object);
3582 			charged = ENTRY_CHARGED(src_entry);
3583 			if (src_object->handle == NULL &&
3584 			    (src_object->type == OBJT_DEFAULT ||
3585 			    src_object->type == OBJT_SWAP)) {
3586 				vm_object_collapse(src_object);
3587 				if ((src_object->flags & (OBJ_NOSPLIT |
3588 				    OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
3589 					vm_object_split(src_entry);
3590 					src_object =
3591 					    src_entry->object.vm_object;
3592 				}
3593 			}
3594 			vm_object_reference_locked(src_object);
3595 			vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
3596 			if (src_entry->cred != NULL &&
3597 			    !(src_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
3598 				KASSERT(src_object->cred == NULL,
3599 				    ("OVERCOMMIT: vm_map_copy_entry: cred %p",
3600 				     src_object));
3601 				src_object->cred = src_entry->cred;
3602 				src_object->charge = size;
3603 			}
3604 			VM_OBJECT_WUNLOCK(src_object);
3605 			dst_entry->object.vm_object = src_object;
3606 			if (charged) {
3607 				cred = curthread->td_ucred;
3608 				crhold(cred);
3609 				dst_entry->cred = cred;
3610 				*fork_charge += size;
3611 				if (!(src_entry->eflags &
3612 				      MAP_ENTRY_NEEDS_COPY)) {
3613 					crhold(cred);
3614 					src_entry->cred = cred;
3615 					*fork_charge += size;
3616 				}
3617 			}
3618 			src_entry->eflags |= MAP_ENTRY_COW |
3619 			    MAP_ENTRY_NEEDS_COPY;
3620 			dst_entry->eflags |= MAP_ENTRY_COW |
3621 			    MAP_ENTRY_NEEDS_COPY;
3622 			dst_entry->offset = src_entry->offset;
3623 			if (src_entry->eflags & MAP_ENTRY_VN_WRITECNT) {
3624 				/*
3625 				 * MAP_ENTRY_VN_WRITECNT cannot
3626 				 * indicate write reference from
3627 				 * src_entry, since the entry is
3628 				 * marked as needs copy.  Allocate a
3629 				 * fake entry that is used to
3630 				 * decrement object->un_pager.vnp.writecount
3631 				 * at the appropriate time.  Attach
3632 				 * fake_entry to the deferred list.
3633 				 */
3634 				fake_entry = vm_map_entry_create(dst_map);
3635 				fake_entry->eflags = MAP_ENTRY_VN_WRITECNT;
3636 				src_entry->eflags &= ~MAP_ENTRY_VN_WRITECNT;
3637 				vm_object_reference(src_object);
3638 				fake_entry->object.vm_object = src_object;
3639 				fake_entry->start = src_entry->start;
3640 				fake_entry->end = src_entry->end;
3641 				fake_entry->next = curthread->td_map_def_user;
3642 				curthread->td_map_def_user = fake_entry;
3643 			}
3644 
3645 			pmap_copy(dst_map->pmap, src_map->pmap,
3646 			    dst_entry->start, dst_entry->end - dst_entry->start,
3647 			    src_entry->start);
3648 		} else {
3649 			dst_entry->object.vm_object = NULL;
3650 			dst_entry->offset = 0;
3651 			if (src_entry->cred != NULL) {
3652 				dst_entry->cred = curthread->td_ucred;
3653 				crhold(dst_entry->cred);
3654 				*fork_charge += size;
3655 			}
3656 		}
3657 	} else {
3658 		/*
3659 		 * We don't want to make writeable wired pages copy-on-write.
3660 		 * Immediately copy these pages into the new map by simulating
3661 		 * page faults.  The new pages are pageable.
3662 		 */
3663 		vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry,
3664 		    fork_charge);
3665 	}
3666 }
3667 
3668 /*
3669  * vmspace_map_entry_forked:
3670  * Update the newly-forked vmspace each time a map entry is inherited
3671  * or copied.  The values for vm_dsize and vm_tsize are approximate
3672  * (and mostly-obsolete ideas in the face of mmap(2) et al.)
3673  */
3674 static void
3675 vmspace_map_entry_forked(const struct vmspace *vm1, struct vmspace *vm2,
3676     vm_map_entry_t entry)
3677 {
3678 	vm_size_t entrysize;
3679 	vm_offset_t newend;
3680 
3681 	if ((entry->eflags & MAP_ENTRY_GUARD) != 0)
3682 		return;
3683 	entrysize = entry->end - entry->start;
3684 	vm2->vm_map.size += entrysize;
3685 	if (entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP)) {
3686 		vm2->vm_ssize += btoc(entrysize);
3687 	} else if (entry->start >= (vm_offset_t)vm1->vm_daddr &&
3688 	    entry->start < (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)) {
3689 		newend = MIN(entry->end,
3690 		    (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize));
3691 		vm2->vm_dsize += btoc(newend - entry->start);
3692 	} else if (entry->start >= (vm_offset_t)vm1->vm_taddr &&
3693 	    entry->start < (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)) {
3694 		newend = MIN(entry->end,
3695 		    (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize));
3696 		vm2->vm_tsize += btoc(newend - entry->start);
3697 	}
3698 }
3699 
3700 /*
3701  * vmspace_fork:
3702  * Create a new process vmspace structure and vm_map
3703  * based on those of an existing process.  The new map
3704  * is based on the old map, according to the inheritance
3705  * values on the regions in that map.
3706  *
3707  * XXX It might be worth coalescing the entries added to the new vmspace.
3708  *
3709  * The source map must not be locked.
3710  */
3711 struct vmspace *
3712 vmspace_fork(struct vmspace *vm1, vm_ooffset_t *fork_charge)
3713 {
3714 	struct vmspace *vm2;
3715 	vm_map_t new_map, old_map;
3716 	vm_map_entry_t new_entry, old_entry;
3717 	vm_object_t object;
3718 	int error, locked;
3719 	vm_inherit_t inh;
3720 
3721 	old_map = &vm1->vm_map;
3722 	/* Copy immutable fields of vm1 to vm2. */
3723 	vm2 = vmspace_alloc(vm_map_min(old_map), vm_map_max(old_map),
3724 	    pmap_pinit);
3725 	if (vm2 == NULL)
3726 		return (NULL);
3727 
3728 	vm2->vm_taddr = vm1->vm_taddr;
3729 	vm2->vm_daddr = vm1->vm_daddr;
3730 	vm2->vm_maxsaddr = vm1->vm_maxsaddr;
3731 	vm_map_lock(old_map);
3732 	if (old_map->busy)
3733 		vm_map_wait_busy(old_map);
3734 	new_map = &vm2->vm_map;
3735 	locked = vm_map_trylock(new_map); /* trylock to silence WITNESS */
3736 	KASSERT(locked, ("vmspace_fork: lock failed"));
3737 
3738 	error = pmap_vmspace_copy(new_map->pmap, old_map->pmap);
3739 	if (error != 0) {
3740 		sx_xunlock(&old_map->lock);
3741 		sx_xunlock(&new_map->lock);
3742 		vm_map_process_deferred();
3743 		vmspace_free(vm2);
3744 		return (NULL);
3745 	}
3746 
3747 	new_map->anon_loc = old_map->anon_loc;
3748 
3749 	old_entry = old_map->header.next;
3750 
3751 	while (old_entry != &old_map->header) {
3752 		if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
3753 			panic("vm_map_fork: encountered a submap");
3754 
3755 		inh = old_entry->inheritance;
3756 		if ((old_entry->eflags & MAP_ENTRY_GUARD) != 0 &&
3757 		    inh != VM_INHERIT_NONE)
3758 			inh = VM_INHERIT_COPY;
3759 
3760 		switch (inh) {
3761 		case VM_INHERIT_NONE:
3762 			break;
3763 
3764 		case VM_INHERIT_SHARE:
3765 			/*
3766 			 * Clone the entry, creating the shared object if necessary.
3767 			 */
3768 			object = old_entry->object.vm_object;
3769 			if (object == NULL) {
3770 				object = vm_object_allocate(OBJT_DEFAULT,
3771 					atop(old_entry->end - old_entry->start));
3772 				old_entry->object.vm_object = object;
3773 				old_entry->offset = 0;
3774 				if (old_entry->cred != NULL) {
3775 					object->cred = old_entry->cred;
3776 					object->charge = old_entry->end -
3777 					    old_entry->start;
3778 					old_entry->cred = NULL;
3779 				}
3780 			}
3781 
3782 			/*
3783 			 * Add the reference before calling vm_object_shadow
3784 			 * to insure that a shadow object is created.
3785 			 */
3786 			vm_object_reference(object);
3787 			if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3788 				vm_object_shadow(&old_entry->object.vm_object,
3789 				    &old_entry->offset,
3790 				    old_entry->end - old_entry->start);
3791 				old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
3792 				/* Transfer the second reference too. */
3793 				vm_object_reference(
3794 				    old_entry->object.vm_object);
3795 
3796 				/*
3797 				 * As in vm_map_simplify_entry(), the
3798 				 * vnode lock will not be acquired in
3799 				 * this call to vm_object_deallocate().
3800 				 */
3801 				vm_object_deallocate(object);
3802 				object = old_entry->object.vm_object;
3803 			}
3804 			VM_OBJECT_WLOCK(object);
3805 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
3806 			if (old_entry->cred != NULL) {
3807 				KASSERT(object->cred == NULL, ("vmspace_fork both cred"));
3808 				object->cred = old_entry->cred;
3809 				object->charge = old_entry->end - old_entry->start;
3810 				old_entry->cred = NULL;
3811 			}
3812 
3813 			/*
3814 			 * Assert the correct state of the vnode
3815 			 * v_writecount while the object is locked, to
3816 			 * not relock it later for the assertion
3817 			 * correctness.
3818 			 */
3819 			if (old_entry->eflags & MAP_ENTRY_VN_WRITECNT &&
3820 			    object->type == OBJT_VNODE) {
3821 				KASSERT(((struct vnode *)object->handle)->
3822 				    v_writecount > 0,
3823 				    ("vmspace_fork: v_writecount %p", object));
3824 				KASSERT(object->un_pager.vnp.writemappings > 0,
3825 				    ("vmspace_fork: vnp.writecount %p",
3826 				    object));
3827 			}
3828 			VM_OBJECT_WUNLOCK(object);
3829 
3830 			/*
3831 			 * Clone the entry, referencing the shared object.
3832 			 */
3833 			new_entry = vm_map_entry_create(new_map);
3834 			*new_entry = *old_entry;
3835 			new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
3836 			    MAP_ENTRY_IN_TRANSITION);
3837 			new_entry->wiring_thread = NULL;
3838 			new_entry->wired_count = 0;
3839 			if (new_entry->eflags & MAP_ENTRY_VN_WRITECNT) {
3840 				vnode_pager_update_writecount(object,
3841 				    new_entry->start, new_entry->end);
3842 			}
3843 
3844 			/*
3845 			 * Insert the entry into the new map -- we know we're
3846 			 * inserting at the end of the new map.
3847 			 */
3848 			vm_map_entry_link(new_map, new_entry);
3849 			vmspace_map_entry_forked(vm1, vm2, new_entry);
3850 
3851 			/*
3852 			 * Update the physical map
3853 			 */
3854 			pmap_copy(new_map->pmap, old_map->pmap,
3855 			    new_entry->start,
3856 			    (old_entry->end - old_entry->start),
3857 			    old_entry->start);
3858 			break;
3859 
3860 		case VM_INHERIT_COPY:
3861 			/*
3862 			 * Clone the entry and link into the map.
3863 			 */
3864 			new_entry = vm_map_entry_create(new_map);
3865 			*new_entry = *old_entry;
3866 			/*
3867 			 * Copied entry is COW over the old object.
3868 			 */
3869 			new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
3870 			    MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_VN_WRITECNT);
3871 			new_entry->wiring_thread = NULL;
3872 			new_entry->wired_count = 0;
3873 			new_entry->object.vm_object = NULL;
3874 			new_entry->cred = NULL;
3875 			vm_map_entry_link(new_map, new_entry);
3876 			vmspace_map_entry_forked(vm1, vm2, new_entry);
3877 			vm_map_copy_entry(old_map, new_map, old_entry,
3878 			    new_entry, fork_charge);
3879 			break;
3880 
3881 		case VM_INHERIT_ZERO:
3882 			/*
3883 			 * Create a new anonymous mapping entry modelled from
3884 			 * the old one.
3885 			 */
3886 			new_entry = vm_map_entry_create(new_map);
3887 			memset(new_entry, 0, sizeof(*new_entry));
3888 
3889 			new_entry->start = old_entry->start;
3890 			new_entry->end = old_entry->end;
3891 			new_entry->eflags = old_entry->eflags &
3892 			    ~(MAP_ENTRY_USER_WIRED | MAP_ENTRY_IN_TRANSITION |
3893 			    MAP_ENTRY_VN_WRITECNT);
3894 			new_entry->protection = old_entry->protection;
3895 			new_entry->max_protection = old_entry->max_protection;
3896 			new_entry->inheritance = VM_INHERIT_ZERO;
3897 
3898 			vm_map_entry_link(new_map, new_entry);
3899 			vmspace_map_entry_forked(vm1, vm2, new_entry);
3900 
3901 			new_entry->cred = curthread->td_ucred;
3902 			crhold(new_entry->cred);
3903 			*fork_charge += (new_entry->end - new_entry->start);
3904 
3905 			break;
3906 		}
3907 		old_entry = old_entry->next;
3908 	}
3909 	/*
3910 	 * Use inlined vm_map_unlock() to postpone handling the deferred
3911 	 * map entries, which cannot be done until both old_map and
3912 	 * new_map locks are released.
3913 	 */
3914 	sx_xunlock(&old_map->lock);
3915 	sx_xunlock(&new_map->lock);
3916 	vm_map_process_deferred();
3917 
3918 	return (vm2);
3919 }
3920 
3921 /*
3922  * Create a process's stack for exec_new_vmspace().  This function is never
3923  * asked to wire the newly created stack.
3924  */
3925 int
3926 vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
3927     vm_prot_t prot, vm_prot_t max, int cow)
3928 {
3929 	vm_size_t growsize, init_ssize;
3930 	rlim_t vmemlim;
3931 	int rv;
3932 
3933 	MPASS((map->flags & MAP_WIREFUTURE) == 0);
3934 	growsize = sgrowsiz;
3935 	init_ssize = (max_ssize < growsize) ? max_ssize : growsize;
3936 	vm_map_lock(map);
3937 	vmemlim = lim_cur(curthread, RLIMIT_VMEM);
3938 	/* If we would blow our VMEM resource limit, no go */
3939 	if (map->size + init_ssize > vmemlim) {
3940 		rv = KERN_NO_SPACE;
3941 		goto out;
3942 	}
3943 	rv = vm_map_stack_locked(map, addrbos, max_ssize, growsize, prot,
3944 	    max, cow);
3945 out:
3946 	vm_map_unlock(map);
3947 	return (rv);
3948 }
3949 
3950 static int stack_guard_page = 1;
3951 SYSCTL_INT(_security_bsd, OID_AUTO, stack_guard_page, CTLFLAG_RWTUN,
3952     &stack_guard_page, 0,
3953     "Specifies the number of guard pages for a stack that grows");
3954 
3955 static int
3956 vm_map_stack_locked(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
3957     vm_size_t growsize, vm_prot_t prot, vm_prot_t max, int cow)
3958 {
3959 	vm_map_entry_t new_entry, prev_entry;
3960 	vm_offset_t bot, gap_bot, gap_top, top;
3961 	vm_size_t init_ssize, sgp;
3962 	int orient, rv;
3963 
3964 	/*
3965 	 * The stack orientation is piggybacked with the cow argument.
3966 	 * Extract it into orient and mask the cow argument so that we
3967 	 * don't pass it around further.
3968 	 */
3969 	orient = cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP);
3970 	KASSERT(orient != 0, ("No stack grow direction"));
3971 	KASSERT(orient != (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP),
3972 	    ("bi-dir stack"));
3973 
3974 	if (addrbos < vm_map_min(map) ||
3975 	    addrbos + max_ssize > vm_map_max(map) ||
3976 	    addrbos + max_ssize <= addrbos)
3977 		return (KERN_INVALID_ADDRESS);
3978 	sgp = (vm_size_t)stack_guard_page * PAGE_SIZE;
3979 	if (sgp >= max_ssize)
3980 		return (KERN_INVALID_ARGUMENT);
3981 
3982 	init_ssize = growsize;
3983 	if (max_ssize < init_ssize + sgp)
3984 		init_ssize = max_ssize - sgp;
3985 
3986 	/* If addr is already mapped, no go */
3987 	if (vm_map_lookup_entry(map, addrbos, &prev_entry))
3988 		return (KERN_NO_SPACE);
3989 
3990 	/*
3991 	 * If we can't accommodate max_ssize in the current mapping, no go.
3992 	 */
3993 	if (prev_entry->next->start < addrbos + max_ssize)
3994 		return (KERN_NO_SPACE);
3995 
3996 	/*
3997 	 * We initially map a stack of only init_ssize.  We will grow as
3998 	 * needed later.  Depending on the orientation of the stack (i.e.
3999 	 * the grow direction) we either map at the top of the range, the
4000 	 * bottom of the range or in the middle.
4001 	 *
4002 	 * Note: we would normally expect prot and max to be VM_PROT_ALL,
4003 	 * and cow to be 0.  Possibly we should eliminate these as input
4004 	 * parameters, and just pass these values here in the insert call.
4005 	 */
4006 	if (orient == MAP_STACK_GROWS_DOWN) {
4007 		bot = addrbos + max_ssize - init_ssize;
4008 		top = bot + init_ssize;
4009 		gap_bot = addrbos;
4010 		gap_top = bot;
4011 	} else /* if (orient == MAP_STACK_GROWS_UP) */ {
4012 		bot = addrbos;
4013 		top = bot + init_ssize;
4014 		gap_bot = top;
4015 		gap_top = addrbos + max_ssize;
4016 	}
4017 	rv = vm_map_insert(map, NULL, 0, bot, top, prot, max, cow);
4018 	if (rv != KERN_SUCCESS)
4019 		return (rv);
4020 	new_entry = prev_entry->next;
4021 	KASSERT(new_entry->end == top || new_entry->start == bot,
4022 	    ("Bad entry start/end for new stack entry"));
4023 	KASSERT((orient & MAP_STACK_GROWS_DOWN) == 0 ||
4024 	    (new_entry->eflags & MAP_ENTRY_GROWS_DOWN) != 0,
4025 	    ("new entry lacks MAP_ENTRY_GROWS_DOWN"));
4026 	KASSERT((orient & MAP_STACK_GROWS_UP) == 0 ||
4027 	    (new_entry->eflags & MAP_ENTRY_GROWS_UP) != 0,
4028 	    ("new entry lacks MAP_ENTRY_GROWS_UP"));
4029 	rv = vm_map_insert(map, NULL, 0, gap_bot, gap_top, VM_PROT_NONE,
4030 	    VM_PROT_NONE, MAP_CREATE_GUARD | (orient == MAP_STACK_GROWS_DOWN ?
4031 	    MAP_CREATE_STACK_GAP_DN : MAP_CREATE_STACK_GAP_UP));
4032 	if (rv != KERN_SUCCESS)
4033 		(void)vm_map_delete(map, bot, top);
4034 	return (rv);
4035 }
4036 
4037 /*
4038  * Attempts to grow a vm stack entry.  Returns KERN_SUCCESS if we
4039  * successfully grow the stack.
4040  */
4041 static int
4042 vm_map_growstack(vm_map_t map, vm_offset_t addr, vm_map_entry_t gap_entry)
4043 {
4044 	vm_map_entry_t stack_entry;
4045 	struct proc *p;
4046 	struct vmspace *vm;
4047 	struct ucred *cred;
4048 	vm_offset_t gap_end, gap_start, grow_start;
4049 	size_t grow_amount, guard, max_grow;
4050 	rlim_t lmemlim, stacklim, vmemlim;
4051 	int rv, rv1;
4052 	bool gap_deleted, grow_down, is_procstack;
4053 #ifdef notyet
4054 	uint64_t limit;
4055 #endif
4056 #ifdef RACCT
4057 	int error;
4058 #endif
4059 
4060 	p = curproc;
4061 	vm = p->p_vmspace;
4062 
4063 	/*
4064 	 * Disallow stack growth when the access is performed by a
4065 	 * debugger or AIO daemon.  The reason is that the wrong
4066 	 * resource limits are applied.
4067 	 */
4068 	if (map != &p->p_vmspace->vm_map || p->p_textvp == NULL)
4069 		return (KERN_FAILURE);
4070 
4071 	MPASS(!map->system_map);
4072 
4073 	guard = stack_guard_page * PAGE_SIZE;
4074 	lmemlim = lim_cur(curthread, RLIMIT_MEMLOCK);
4075 	stacklim = lim_cur(curthread, RLIMIT_STACK);
4076 	vmemlim = lim_cur(curthread, RLIMIT_VMEM);
4077 retry:
4078 	/* If addr is not in a hole for a stack grow area, no need to grow. */
4079 	if (gap_entry == NULL && !vm_map_lookup_entry(map, addr, &gap_entry))
4080 		return (KERN_FAILURE);
4081 	if ((gap_entry->eflags & MAP_ENTRY_GUARD) == 0)
4082 		return (KERN_SUCCESS);
4083 	if ((gap_entry->eflags & MAP_ENTRY_STACK_GAP_DN) != 0) {
4084 		stack_entry = gap_entry->next;
4085 		if ((stack_entry->eflags & MAP_ENTRY_GROWS_DOWN) == 0 ||
4086 		    stack_entry->start != gap_entry->end)
4087 			return (KERN_FAILURE);
4088 		grow_amount = round_page(stack_entry->start - addr);
4089 		grow_down = true;
4090 	} else if ((gap_entry->eflags & MAP_ENTRY_STACK_GAP_UP) != 0) {
4091 		stack_entry = gap_entry->prev;
4092 		if ((stack_entry->eflags & MAP_ENTRY_GROWS_UP) == 0 ||
4093 		    stack_entry->end != gap_entry->start)
4094 			return (KERN_FAILURE);
4095 		grow_amount = round_page(addr + 1 - stack_entry->end);
4096 		grow_down = false;
4097 	} else {
4098 		return (KERN_FAILURE);
4099 	}
4100 	max_grow = gap_entry->end - gap_entry->start;
4101 	if (guard > max_grow)
4102 		return (KERN_NO_SPACE);
4103 	max_grow -= guard;
4104 	if (grow_amount > max_grow)
4105 		return (KERN_NO_SPACE);
4106 
4107 	/*
4108 	 * If this is the main process stack, see if we're over the stack
4109 	 * limit.
4110 	 */
4111 	is_procstack = addr >= (vm_offset_t)vm->vm_maxsaddr &&
4112 	    addr < (vm_offset_t)p->p_sysent->sv_usrstack;
4113 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim))
4114 		return (KERN_NO_SPACE);
4115 
4116 #ifdef RACCT
4117 	if (racct_enable) {
4118 		PROC_LOCK(p);
4119 		if (is_procstack && racct_set(p, RACCT_STACK,
4120 		    ctob(vm->vm_ssize) + grow_amount)) {
4121 			PROC_UNLOCK(p);
4122 			return (KERN_NO_SPACE);
4123 		}
4124 		PROC_UNLOCK(p);
4125 	}
4126 #endif
4127 
4128 	grow_amount = roundup(grow_amount, sgrowsiz);
4129 	if (grow_amount > max_grow)
4130 		grow_amount = max_grow;
4131 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
4132 		grow_amount = trunc_page((vm_size_t)stacklim) -
4133 		    ctob(vm->vm_ssize);
4134 	}
4135 
4136 #ifdef notyet
4137 	PROC_LOCK(p);
4138 	limit = racct_get_available(p, RACCT_STACK);
4139 	PROC_UNLOCK(p);
4140 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > limit))
4141 		grow_amount = limit - ctob(vm->vm_ssize);
4142 #endif
4143 
4144 	if (!old_mlock && (map->flags & MAP_WIREFUTURE) != 0) {
4145 		if (ptoa(pmap_wired_count(map->pmap)) + grow_amount > lmemlim) {
4146 			rv = KERN_NO_SPACE;
4147 			goto out;
4148 		}
4149 #ifdef RACCT
4150 		if (racct_enable) {
4151 			PROC_LOCK(p);
4152 			if (racct_set(p, RACCT_MEMLOCK,
4153 			    ptoa(pmap_wired_count(map->pmap)) + grow_amount)) {
4154 				PROC_UNLOCK(p);
4155 				rv = KERN_NO_SPACE;
4156 				goto out;
4157 			}
4158 			PROC_UNLOCK(p);
4159 		}
4160 #endif
4161 	}
4162 
4163 	/* If we would blow our VMEM resource limit, no go */
4164 	if (map->size + grow_amount > vmemlim) {
4165 		rv = KERN_NO_SPACE;
4166 		goto out;
4167 	}
4168 #ifdef RACCT
4169 	if (racct_enable) {
4170 		PROC_LOCK(p);
4171 		if (racct_set(p, RACCT_VMEM, map->size + grow_amount)) {
4172 			PROC_UNLOCK(p);
4173 			rv = KERN_NO_SPACE;
4174 			goto out;
4175 		}
4176 		PROC_UNLOCK(p);
4177 	}
4178 #endif
4179 
4180 	if (vm_map_lock_upgrade(map)) {
4181 		gap_entry = NULL;
4182 		vm_map_lock_read(map);
4183 		goto retry;
4184 	}
4185 
4186 	if (grow_down) {
4187 		grow_start = gap_entry->end - grow_amount;
4188 		if (gap_entry->start + grow_amount == gap_entry->end) {
4189 			gap_start = gap_entry->start;
4190 			gap_end = gap_entry->end;
4191 			vm_map_entry_delete(map, gap_entry);
4192 			gap_deleted = true;
4193 		} else {
4194 			MPASS(gap_entry->start < gap_entry->end - grow_amount);
4195 			gap_entry->end -= grow_amount;
4196 			vm_map_entry_resize_free(map, gap_entry);
4197 			gap_deleted = false;
4198 		}
4199 		rv = vm_map_insert(map, NULL, 0, grow_start,
4200 		    grow_start + grow_amount,
4201 		    stack_entry->protection, stack_entry->max_protection,
4202 		    MAP_STACK_GROWS_DOWN);
4203 		if (rv != KERN_SUCCESS) {
4204 			if (gap_deleted) {
4205 				rv1 = vm_map_insert(map, NULL, 0, gap_start,
4206 				    gap_end, VM_PROT_NONE, VM_PROT_NONE,
4207 				    MAP_CREATE_GUARD | MAP_CREATE_STACK_GAP_DN);
4208 				MPASS(rv1 == KERN_SUCCESS);
4209 			} else {
4210 				gap_entry->end += grow_amount;
4211 				vm_map_entry_resize_free(map, gap_entry);
4212 			}
4213 		}
4214 	} else {
4215 		grow_start = stack_entry->end;
4216 		cred = stack_entry->cred;
4217 		if (cred == NULL && stack_entry->object.vm_object != NULL)
4218 			cred = stack_entry->object.vm_object->cred;
4219 		if (cred != NULL && !swap_reserve_by_cred(grow_amount, cred))
4220 			rv = KERN_NO_SPACE;
4221 		/* Grow the underlying object if applicable. */
4222 		else if (stack_entry->object.vm_object == NULL ||
4223 		    vm_object_coalesce(stack_entry->object.vm_object,
4224 		    stack_entry->offset,
4225 		    (vm_size_t)(stack_entry->end - stack_entry->start),
4226 		    (vm_size_t)grow_amount, cred != NULL)) {
4227 			if (gap_entry->start + grow_amount == gap_entry->end)
4228 				vm_map_entry_delete(map, gap_entry);
4229 			else
4230 				gap_entry->start += grow_amount;
4231 			stack_entry->end += grow_amount;
4232 			map->size += grow_amount;
4233 			vm_map_entry_resize_free(map, stack_entry);
4234 			rv = KERN_SUCCESS;
4235 		} else
4236 			rv = KERN_FAILURE;
4237 	}
4238 	if (rv == KERN_SUCCESS && is_procstack)
4239 		vm->vm_ssize += btoc(grow_amount);
4240 
4241 	/*
4242 	 * Heed the MAP_WIREFUTURE flag if it was set for this process.
4243 	 */
4244 	if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE) != 0) {
4245 		vm_map_unlock(map);
4246 		vm_map_wire(map, grow_start, grow_start + grow_amount,
4247 		    VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
4248 		vm_map_lock_read(map);
4249 	} else
4250 		vm_map_lock_downgrade(map);
4251 
4252 out:
4253 #ifdef RACCT
4254 	if (racct_enable && rv != KERN_SUCCESS) {
4255 		PROC_LOCK(p);
4256 		error = racct_set(p, RACCT_VMEM, map->size);
4257 		KASSERT(error == 0, ("decreasing RACCT_VMEM failed"));
4258 		if (!old_mlock) {
4259 			error = racct_set(p, RACCT_MEMLOCK,
4260 			    ptoa(pmap_wired_count(map->pmap)));
4261 			KASSERT(error == 0, ("decreasing RACCT_MEMLOCK failed"));
4262 		}
4263 	    	error = racct_set(p, RACCT_STACK, ctob(vm->vm_ssize));
4264 		KASSERT(error == 0, ("decreasing RACCT_STACK failed"));
4265 		PROC_UNLOCK(p);
4266 	}
4267 #endif
4268 
4269 	return (rv);
4270 }
4271 
4272 /*
4273  * Unshare the specified VM space for exec.  If other processes are
4274  * mapped to it, then create a new one.  The new vmspace is null.
4275  */
4276 int
4277 vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser)
4278 {
4279 	struct vmspace *oldvmspace = p->p_vmspace;
4280 	struct vmspace *newvmspace;
4281 
4282 	KASSERT((curthread->td_pflags & TDP_EXECVMSPC) == 0,
4283 	    ("vmspace_exec recursed"));
4284 	newvmspace = vmspace_alloc(minuser, maxuser, pmap_pinit);
4285 	if (newvmspace == NULL)
4286 		return (ENOMEM);
4287 	newvmspace->vm_swrss = oldvmspace->vm_swrss;
4288 	/*
4289 	 * This code is written like this for prototype purposes.  The
4290 	 * goal is to avoid running down the vmspace here, but let the
4291 	 * other process's that are still using the vmspace to finally
4292 	 * run it down.  Even though there is little or no chance of blocking
4293 	 * here, it is a good idea to keep this form for future mods.
4294 	 */
4295 	PROC_VMSPACE_LOCK(p);
4296 	p->p_vmspace = newvmspace;
4297 	PROC_VMSPACE_UNLOCK(p);
4298 	if (p == curthread->td_proc)
4299 		pmap_activate(curthread);
4300 	curthread->td_pflags |= TDP_EXECVMSPC;
4301 	return (0);
4302 }
4303 
4304 /*
4305  * Unshare the specified VM space for forcing COW.  This
4306  * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
4307  */
4308 int
4309 vmspace_unshare(struct proc *p)
4310 {
4311 	struct vmspace *oldvmspace = p->p_vmspace;
4312 	struct vmspace *newvmspace;
4313 	vm_ooffset_t fork_charge;
4314 
4315 	if (oldvmspace->vm_refcnt == 1)
4316 		return (0);
4317 	fork_charge = 0;
4318 	newvmspace = vmspace_fork(oldvmspace, &fork_charge);
4319 	if (newvmspace == NULL)
4320 		return (ENOMEM);
4321 	if (!swap_reserve_by_cred(fork_charge, p->p_ucred)) {
4322 		vmspace_free(newvmspace);
4323 		return (ENOMEM);
4324 	}
4325 	PROC_VMSPACE_LOCK(p);
4326 	p->p_vmspace = newvmspace;
4327 	PROC_VMSPACE_UNLOCK(p);
4328 	if (p == curthread->td_proc)
4329 		pmap_activate(curthread);
4330 	vmspace_free(oldvmspace);
4331 	return (0);
4332 }
4333 
4334 /*
4335  *	vm_map_lookup:
4336  *
4337  *	Finds the VM object, offset, and
4338  *	protection for a given virtual address in the
4339  *	specified map, assuming a page fault of the
4340  *	type specified.
4341  *
4342  *	Leaves the map in question locked for read; return
4343  *	values are guaranteed until a vm_map_lookup_done
4344  *	call is performed.  Note that the map argument
4345  *	is in/out; the returned map must be used in
4346  *	the call to vm_map_lookup_done.
4347  *
4348  *	A handle (out_entry) is returned for use in
4349  *	vm_map_lookup_done, to make that fast.
4350  *
4351  *	If a lookup is requested with "write protection"
4352  *	specified, the map may be changed to perform virtual
4353  *	copying operations, although the data referenced will
4354  *	remain the same.
4355  */
4356 int
4357 vm_map_lookup(vm_map_t *var_map,		/* IN/OUT */
4358 	      vm_offset_t vaddr,
4359 	      vm_prot_t fault_typea,
4360 	      vm_map_entry_t *out_entry,	/* OUT */
4361 	      vm_object_t *object,		/* OUT */
4362 	      vm_pindex_t *pindex,		/* OUT */
4363 	      vm_prot_t *out_prot,		/* OUT */
4364 	      boolean_t *wired)			/* OUT */
4365 {
4366 	vm_map_entry_t entry;
4367 	vm_map_t map = *var_map;
4368 	vm_prot_t prot;
4369 	vm_prot_t fault_type = fault_typea;
4370 	vm_object_t eobject;
4371 	vm_size_t size;
4372 	struct ucred *cred;
4373 
4374 RetryLookup:
4375 
4376 	vm_map_lock_read(map);
4377 
4378 RetryLookupLocked:
4379 	/*
4380 	 * Lookup the faulting address.
4381 	 */
4382 	if (!vm_map_lookup_entry(map, vaddr, out_entry)) {
4383 		vm_map_unlock_read(map);
4384 		return (KERN_INVALID_ADDRESS);
4385 	}
4386 
4387 	entry = *out_entry;
4388 
4389 	/*
4390 	 * Handle submaps.
4391 	 */
4392 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
4393 		vm_map_t old_map = map;
4394 
4395 		*var_map = map = entry->object.sub_map;
4396 		vm_map_unlock_read(old_map);
4397 		goto RetryLookup;
4398 	}
4399 
4400 	/*
4401 	 * Check whether this task is allowed to have this page.
4402 	 */
4403 	prot = entry->protection;
4404 	if ((fault_typea & VM_PROT_FAULT_LOOKUP) != 0) {
4405 		fault_typea &= ~VM_PROT_FAULT_LOOKUP;
4406 		if (prot == VM_PROT_NONE && map != kernel_map &&
4407 		    (entry->eflags & MAP_ENTRY_GUARD) != 0 &&
4408 		    (entry->eflags & (MAP_ENTRY_STACK_GAP_DN |
4409 		    MAP_ENTRY_STACK_GAP_UP)) != 0 &&
4410 		    vm_map_growstack(map, vaddr, entry) == KERN_SUCCESS)
4411 			goto RetryLookupLocked;
4412 	}
4413 	fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
4414 	if ((fault_type & prot) != fault_type || prot == VM_PROT_NONE) {
4415 		vm_map_unlock_read(map);
4416 		return (KERN_PROTECTION_FAILURE);
4417 	}
4418 	KASSERT((prot & VM_PROT_WRITE) == 0 || (entry->eflags &
4419 	    (MAP_ENTRY_USER_WIRED | MAP_ENTRY_NEEDS_COPY)) !=
4420 	    (MAP_ENTRY_USER_WIRED | MAP_ENTRY_NEEDS_COPY),
4421 	    ("entry %p flags %x", entry, entry->eflags));
4422 	if ((fault_typea & VM_PROT_COPY) != 0 &&
4423 	    (entry->max_protection & VM_PROT_WRITE) == 0 &&
4424 	    (entry->eflags & MAP_ENTRY_COW) == 0) {
4425 		vm_map_unlock_read(map);
4426 		return (KERN_PROTECTION_FAILURE);
4427 	}
4428 
4429 	/*
4430 	 * If this page is not pageable, we have to get it for all possible
4431 	 * accesses.
4432 	 */
4433 	*wired = (entry->wired_count != 0);
4434 	if (*wired)
4435 		fault_type = entry->protection;
4436 	size = entry->end - entry->start;
4437 	/*
4438 	 * If the entry was copy-on-write, we either ...
4439 	 */
4440 	if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
4441 		/*
4442 		 * If we want to write the page, we may as well handle that
4443 		 * now since we've got the map locked.
4444 		 *
4445 		 * If we don't need to write the page, we just demote the
4446 		 * permissions allowed.
4447 		 */
4448 		if ((fault_type & VM_PROT_WRITE) != 0 ||
4449 		    (fault_typea & VM_PROT_COPY) != 0) {
4450 			/*
4451 			 * Make a new object, and place it in the object
4452 			 * chain.  Note that no new references have appeared
4453 			 * -- one just moved from the map to the new
4454 			 * object.
4455 			 */
4456 			if (vm_map_lock_upgrade(map))
4457 				goto RetryLookup;
4458 
4459 			if (entry->cred == NULL) {
4460 				/*
4461 				 * The debugger owner is charged for
4462 				 * the memory.
4463 				 */
4464 				cred = curthread->td_ucred;
4465 				crhold(cred);
4466 				if (!swap_reserve_by_cred(size, cred)) {
4467 					crfree(cred);
4468 					vm_map_unlock(map);
4469 					return (KERN_RESOURCE_SHORTAGE);
4470 				}
4471 				entry->cred = cred;
4472 			}
4473 			vm_object_shadow(&entry->object.vm_object,
4474 			    &entry->offset, size);
4475 			entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
4476 			eobject = entry->object.vm_object;
4477 			if (eobject->cred != NULL) {
4478 				/*
4479 				 * The object was not shadowed.
4480 				 */
4481 				swap_release_by_cred(size, entry->cred);
4482 				crfree(entry->cred);
4483 				entry->cred = NULL;
4484 			} else if (entry->cred != NULL) {
4485 				VM_OBJECT_WLOCK(eobject);
4486 				eobject->cred = entry->cred;
4487 				eobject->charge = size;
4488 				VM_OBJECT_WUNLOCK(eobject);
4489 				entry->cred = NULL;
4490 			}
4491 
4492 			vm_map_lock_downgrade(map);
4493 		} else {
4494 			/*
4495 			 * We're attempting to read a copy-on-write page --
4496 			 * don't allow writes.
4497 			 */
4498 			prot &= ~VM_PROT_WRITE;
4499 		}
4500 	}
4501 
4502 	/*
4503 	 * Create an object if necessary.
4504 	 */
4505 	if (entry->object.vm_object == NULL &&
4506 	    !map->system_map) {
4507 		if (vm_map_lock_upgrade(map))
4508 			goto RetryLookup;
4509 		entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
4510 		    atop(size));
4511 		entry->offset = 0;
4512 		if (entry->cred != NULL) {
4513 			VM_OBJECT_WLOCK(entry->object.vm_object);
4514 			entry->object.vm_object->cred = entry->cred;
4515 			entry->object.vm_object->charge = size;
4516 			VM_OBJECT_WUNLOCK(entry->object.vm_object);
4517 			entry->cred = NULL;
4518 		}
4519 		vm_map_lock_downgrade(map);
4520 	}
4521 
4522 	/*
4523 	 * Return the object/offset from this entry.  If the entry was
4524 	 * copy-on-write or empty, it has been fixed up.
4525 	 */
4526 	*pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
4527 	*object = entry->object.vm_object;
4528 
4529 	*out_prot = prot;
4530 	return (KERN_SUCCESS);
4531 }
4532 
4533 /*
4534  *	vm_map_lookup_locked:
4535  *
4536  *	Lookup the faulting address.  A version of vm_map_lookup that returns
4537  *      KERN_FAILURE instead of blocking on map lock or memory allocation.
4538  */
4539 int
4540 vm_map_lookup_locked(vm_map_t *var_map,		/* IN/OUT */
4541 		     vm_offset_t vaddr,
4542 		     vm_prot_t fault_typea,
4543 		     vm_map_entry_t *out_entry,	/* OUT */
4544 		     vm_object_t *object,	/* OUT */
4545 		     vm_pindex_t *pindex,	/* OUT */
4546 		     vm_prot_t *out_prot,	/* OUT */
4547 		     boolean_t *wired)		/* OUT */
4548 {
4549 	vm_map_entry_t entry;
4550 	vm_map_t map = *var_map;
4551 	vm_prot_t prot;
4552 	vm_prot_t fault_type = fault_typea;
4553 
4554 	/*
4555 	 * Lookup the faulting address.
4556 	 */
4557 	if (!vm_map_lookup_entry(map, vaddr, out_entry))
4558 		return (KERN_INVALID_ADDRESS);
4559 
4560 	entry = *out_entry;
4561 
4562 	/*
4563 	 * Fail if the entry refers to a submap.
4564 	 */
4565 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
4566 		return (KERN_FAILURE);
4567 
4568 	/*
4569 	 * Check whether this task is allowed to have this page.
4570 	 */
4571 	prot = entry->protection;
4572 	fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
4573 	if ((fault_type & prot) != fault_type)
4574 		return (KERN_PROTECTION_FAILURE);
4575 
4576 	/*
4577 	 * If this page is not pageable, we have to get it for all possible
4578 	 * accesses.
4579 	 */
4580 	*wired = (entry->wired_count != 0);
4581 	if (*wired)
4582 		fault_type = entry->protection;
4583 
4584 	if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
4585 		/*
4586 		 * Fail if the entry was copy-on-write for a write fault.
4587 		 */
4588 		if (fault_type & VM_PROT_WRITE)
4589 			return (KERN_FAILURE);
4590 		/*
4591 		 * We're attempting to read a copy-on-write page --
4592 		 * don't allow writes.
4593 		 */
4594 		prot &= ~VM_PROT_WRITE;
4595 	}
4596 
4597 	/*
4598 	 * Fail if an object should be created.
4599 	 */
4600 	if (entry->object.vm_object == NULL && !map->system_map)
4601 		return (KERN_FAILURE);
4602 
4603 	/*
4604 	 * Return the object/offset from this entry.  If the entry was
4605 	 * copy-on-write or empty, it has been fixed up.
4606 	 */
4607 	*pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
4608 	*object = entry->object.vm_object;
4609 
4610 	*out_prot = prot;
4611 	return (KERN_SUCCESS);
4612 }
4613 
4614 /*
4615  *	vm_map_lookup_done:
4616  *
4617  *	Releases locks acquired by a vm_map_lookup
4618  *	(according to the handle returned by that lookup).
4619  */
4620 void
4621 vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry)
4622 {
4623 	/*
4624 	 * Unlock the main-level map
4625 	 */
4626 	vm_map_unlock_read(map);
4627 }
4628 
4629 vm_offset_t
4630 vm_map_max_KBI(const struct vm_map *map)
4631 {
4632 
4633 	return (vm_map_max(map));
4634 }
4635 
4636 vm_offset_t
4637 vm_map_min_KBI(const struct vm_map *map)
4638 {
4639 
4640 	return (vm_map_min(map));
4641 }
4642 
4643 pmap_t
4644 vm_map_pmap_KBI(vm_map_t map)
4645 {
4646 
4647 	return (map->pmap);
4648 }
4649 
4650 #include "opt_ddb.h"
4651 #ifdef DDB
4652 #include <sys/kernel.h>
4653 
4654 #include <ddb/ddb.h>
4655 
4656 static void
4657 vm_map_print(vm_map_t map)
4658 {
4659 	vm_map_entry_t entry;
4660 
4661 	db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
4662 	    (void *)map,
4663 	    (void *)map->pmap, map->nentries, map->timestamp);
4664 
4665 	db_indent += 2;
4666 	for (entry = map->header.next; entry != &map->header;
4667 	    entry = entry->next) {
4668 		db_iprintf("map entry %p: start=%p, end=%p, eflags=%#x, \n",
4669 		    (void *)entry, (void *)entry->start, (void *)entry->end,
4670 		    entry->eflags);
4671 		{
4672 			static char *inheritance_name[4] =
4673 			{"share", "copy", "none", "donate_copy"};
4674 
4675 			db_iprintf(" prot=%x/%x/%s",
4676 			    entry->protection,
4677 			    entry->max_protection,
4678 			    inheritance_name[(int)(unsigned char)entry->inheritance]);
4679 			if (entry->wired_count != 0)
4680 				db_printf(", wired");
4681 		}
4682 		if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
4683 			db_printf(", share=%p, offset=0x%jx\n",
4684 			    (void *)entry->object.sub_map,
4685 			    (uintmax_t)entry->offset);
4686 			if ((entry->prev == &map->header) ||
4687 			    (entry->prev->object.sub_map !=
4688 				entry->object.sub_map)) {
4689 				db_indent += 2;
4690 				vm_map_print((vm_map_t)entry->object.sub_map);
4691 				db_indent -= 2;
4692 			}
4693 		} else {
4694 			if (entry->cred != NULL)
4695 				db_printf(", ruid %d", entry->cred->cr_ruid);
4696 			db_printf(", object=%p, offset=0x%jx",
4697 			    (void *)entry->object.vm_object,
4698 			    (uintmax_t)entry->offset);
4699 			if (entry->object.vm_object && entry->object.vm_object->cred)
4700 				db_printf(", obj ruid %d charge %jx",
4701 				    entry->object.vm_object->cred->cr_ruid,
4702 				    (uintmax_t)entry->object.vm_object->charge);
4703 			if (entry->eflags & MAP_ENTRY_COW)
4704 				db_printf(", copy (%s)",
4705 				    (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
4706 			db_printf("\n");
4707 
4708 			if ((entry->prev == &map->header) ||
4709 			    (entry->prev->object.vm_object !=
4710 				entry->object.vm_object)) {
4711 				db_indent += 2;
4712 				vm_object_print((db_expr_t)(intptr_t)
4713 						entry->object.vm_object,
4714 						0, 0, (char *)0);
4715 				db_indent -= 2;
4716 			}
4717 		}
4718 	}
4719 	db_indent -= 2;
4720 }
4721 
4722 DB_SHOW_COMMAND(map, map)
4723 {
4724 
4725 	if (!have_addr) {
4726 		db_printf("usage: show map <addr>\n");
4727 		return;
4728 	}
4729 	vm_map_print((vm_map_t)addr);
4730 }
4731 
4732 DB_SHOW_COMMAND(procvm, procvm)
4733 {
4734 	struct proc *p;
4735 
4736 	if (have_addr) {
4737 		p = db_lookup_proc(addr);
4738 	} else {
4739 		p = curproc;
4740 	}
4741 
4742 	db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
4743 	    (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
4744 	    (void *)vmspace_pmap(p->p_vmspace));
4745 
4746 	vm_map_print((vm_map_t)&p->p_vmspace->vm_map);
4747 }
4748 
4749 #endif /* DDB */
4750