2 * SPDX-License-Identifier: (BSD-3-Clause AND MIT-CMU)
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * The Mach Operating System project at Carnegie-Mellon University.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
34 * from: @(#)vm_map.c 8.3 (Berkeley) 1/12/94
37 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
38 * All rights reserved.
40 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
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.
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.
52 * Carnegie Mellon requests users of this software to return to
54 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
55 * School of Computer Science
56 * Carnegie Mellon University
57 * Pittsburgh PA 15213-3890
59 * any improvements or extensions that they make and grant Carnegie the
60 * rights to redistribute these changes.
64 * Virtual memory mapping module.
67 #include <sys/cdefs.h>
68 __FBSDID("$FreeBSD$");
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/kernel.h>
75 #include <sys/mutex.h>
77 #include <sys/vmmeter.h>
79 #include <sys/vnode.h>
80 #include <sys/racct.h>
81 #include <sys/resourcevar.h>
82 #include <sys/rwlock.h>
84 #include <sys/sysctl.h>
85 #include <sys/sysent.h>
89 #include <vm/vm_param.h>
91 #include <vm/vm_map.h>
92 #include <vm/vm_page.h>
93 #include <vm/vm_pageout.h>
94 #include <vm/vm_object.h>
95 #include <vm/vm_pager.h>
96 #include <vm/vm_kern.h>
97 #include <vm/vm_extern.h>
98 #include <vm/vnode_pager.h>
99 #include <vm/swap_pager.h>
103 * Virtual memory maps provide for the mapping, protection,
104 * and sharing of virtual memory objects. In addition,
105 * this module provides for an efficient virtual copy of
106 * memory from one map to another.
108 * Synchronization is required prior to most operations.
110 * Maps consist of an ordered doubly-linked list of simple
111 * entries; a self-adjusting binary search tree of these
112 * entries is used to speed up lookups.
114 * Since portions of maps are specified by start/end addresses,
115 * which may not align with existing map entries, all
116 * routines merely "clip" entries to these start/end values.
117 * [That is, an entry is split into two, bordering at a
118 * start or end value.] Note that these clippings may not
119 * always be necessary (as the two resulting entries are then
120 * not changed); however, the clipping is done for convenience.
122 * As mentioned above, virtual copy operations are performed
123 * by copying VM object references from one map to
124 * another, and then marking both regions as copy-on-write.
127 static struct mtx map_sleep_mtx;
128 static uma_zone_t mapentzone;
129 static uma_zone_t kmapentzone;
130 static uma_zone_t mapzone;
131 static uma_zone_t vmspace_zone;
132 static int vmspace_zinit(void *mem, int size, int flags);
133 static int vm_map_zinit(void *mem, int ize, int flags);
134 static void _vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min,
136 static void vm_map_entry_deallocate(vm_map_entry_t entry, boolean_t system_map);
137 static void vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry);
138 static void vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry);
139 static int vm_map_growstack(vm_map_t map, vm_offset_t addr,
140 vm_map_entry_t gap_entry);
141 static void vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
142 vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags);
144 static void vm_map_zdtor(void *mem, int size, void *arg);
145 static void vmspace_zdtor(void *mem, int size, void *arg);
147 static int vm_map_stack_locked(vm_map_t map, vm_offset_t addrbos,
148 vm_size_t max_ssize, vm_size_t growsize, vm_prot_t prot, vm_prot_t max,
150 static void vm_map_wire_entry_failure(vm_map_t map, vm_map_entry_t entry,
151 vm_offset_t failed_addr);
153 #define ENTRY_CHARGED(e) ((e)->cred != NULL || \
154 ((e)->object.vm_object != NULL && (e)->object.vm_object->cred != NULL && \
155 !((e)->eflags & MAP_ENTRY_NEEDS_COPY)))
158 * PROC_VMSPACE_{UN,}LOCK() can be a noop as long as vmspaces are type
161 #define PROC_VMSPACE_LOCK(p) do { } while (0)
162 #define PROC_VMSPACE_UNLOCK(p) do { } while (0)
165 * VM_MAP_RANGE_CHECK: [ internal use only ]
167 * Asserts that the starting and ending region
168 * addresses fall within the valid range of the map.
170 #define VM_MAP_RANGE_CHECK(map, start, end) \
172 if (start < vm_map_min(map)) \
173 start = vm_map_min(map); \
174 if (end > vm_map_max(map)) \
175 end = vm_map_max(map); \
183 * Initialize the vm_map module. Must be called before
184 * any other vm_map routines.
186 * Map and entry structures are allocated from the general
187 * purpose memory pool with some exceptions:
189 * - The kernel map and kmem submap are allocated statically.
190 * - Kernel map entries are allocated out of a static pool.
192 * These restrictions are necessary since malloc() uses the
193 * maps and requires map entries.
199 mtx_init(&map_sleep_mtx, "vm map sleep mutex", NULL, MTX_DEF);
200 mapzone = uma_zcreate("MAP", sizeof(struct vm_map), NULL,
206 vm_map_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
207 uma_prealloc(mapzone, MAX_KMAP);
208 kmapentzone = uma_zcreate("KMAP ENTRY", sizeof(struct vm_map_entry),
209 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
210 UMA_ZONE_MTXCLASS | UMA_ZONE_VM);
211 mapentzone = uma_zcreate("MAP ENTRY", sizeof(struct vm_map_entry),
212 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
213 vmspace_zone = uma_zcreate("VMSPACE", sizeof(struct vmspace), NULL,
219 vmspace_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
223 vmspace_zinit(void *mem, int size, int flags)
227 vm = (struct vmspace *)mem;
229 vm->vm_map.pmap = NULL;
230 (void)vm_map_zinit(&vm->vm_map, sizeof(vm->vm_map), flags);
231 PMAP_LOCK_INIT(vmspace_pmap(vm));
236 vm_map_zinit(void *mem, int size, int flags)
241 memset(map, 0, sizeof(*map));
242 mtx_init(&map->system_mtx, "vm map (system)", NULL, MTX_DEF | MTX_DUPOK);
243 sx_init(&map->lock, "vm map (user)");
249 vmspace_zdtor(void *mem, int size, void *arg)
253 vm = (struct vmspace *)mem;
255 vm_map_zdtor(&vm->vm_map, sizeof(vm->vm_map), arg);
258 vm_map_zdtor(void *mem, int size, void *arg)
263 KASSERT(map->nentries == 0,
264 ("map %p nentries == %d on free.",
265 map, map->nentries));
266 KASSERT(map->size == 0,
267 ("map %p size == %lu on free.",
268 map, (unsigned long)map->size));
270 #endif /* INVARIANTS */
273 * Allocate a vmspace structure, including a vm_map and pmap,
274 * and initialize those structures. The refcnt is set to 1.
276 * If 'pinit' is NULL then the embedded pmap is initialized via pmap_pinit().
279 vmspace_alloc(vm_offset_t min, vm_offset_t max, pmap_pinit_t pinit)
283 vm = uma_zalloc(vmspace_zone, M_WAITOK);
284 KASSERT(vm->vm_map.pmap == NULL, ("vm_map.pmap must be NULL"));
285 if (!pinit(vmspace_pmap(vm))) {
286 uma_zfree(vmspace_zone, vm);
289 CTR1(KTR_VM, "vmspace_alloc: %p", vm);
290 _vm_map_init(&vm->vm_map, vmspace_pmap(vm), min, max);
305 vmspace_container_reset(struct proc *p)
309 racct_set(p, RACCT_DATA, 0);
310 racct_set(p, RACCT_STACK, 0);
311 racct_set(p, RACCT_RSS, 0);
312 racct_set(p, RACCT_MEMLOCK, 0);
313 racct_set(p, RACCT_VMEM, 0);
319 vmspace_dofree(struct vmspace *vm)
322 CTR1(KTR_VM, "vmspace_free: %p", vm);
325 * Make sure any SysV shm is freed, it might not have been in
331 * Lock the map, to wait out all other references to it.
332 * Delete all of the mappings and pages they hold, then call
333 * the pmap module to reclaim anything left.
335 (void)vm_map_remove(&vm->vm_map, vm_map_min(&vm->vm_map),
336 vm_map_max(&vm->vm_map));
338 pmap_release(vmspace_pmap(vm));
339 vm->vm_map.pmap = NULL;
340 uma_zfree(vmspace_zone, vm);
344 vmspace_free(struct vmspace *vm)
347 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
348 "vmspace_free() called");
350 if (vm->vm_refcnt == 0)
351 panic("vmspace_free: attempt to free already freed vmspace");
353 if (atomic_fetchadd_int(&vm->vm_refcnt, -1) == 1)
358 vmspace_exitfree(struct proc *p)
362 PROC_VMSPACE_LOCK(p);
365 PROC_VMSPACE_UNLOCK(p);
366 KASSERT(vm == &vmspace0, ("vmspace_exitfree: wrong vmspace"));
371 vmspace_exit(struct thread *td)
378 * Release user portion of address space.
379 * This releases references to vnodes,
380 * which could cause I/O if the file has been unlinked.
381 * Need to do this early enough that we can still sleep.
383 * The last exiting process to reach this point releases as
384 * much of the environment as it can. vmspace_dofree() is the
385 * slower fallback in case another process had a temporary
386 * reference to the vmspace.
391 atomic_add_int(&vmspace0.vm_refcnt, 1);
392 refcnt = vm->vm_refcnt;
394 if (refcnt > 1 && p->p_vmspace != &vmspace0) {
395 /* Switch now since other proc might free vmspace */
396 PROC_VMSPACE_LOCK(p);
397 p->p_vmspace = &vmspace0;
398 PROC_VMSPACE_UNLOCK(p);
401 } while (!atomic_fcmpset_int(&vm->vm_refcnt, &refcnt, refcnt - 1));
403 if (p->p_vmspace != vm) {
404 /* vmspace not yet freed, switch back */
405 PROC_VMSPACE_LOCK(p);
407 PROC_VMSPACE_UNLOCK(p);
410 pmap_remove_pages(vmspace_pmap(vm));
411 /* Switch now since this proc will free vmspace */
412 PROC_VMSPACE_LOCK(p);
413 p->p_vmspace = &vmspace0;
414 PROC_VMSPACE_UNLOCK(p);
420 vmspace_container_reset(p);
424 /* Acquire reference to vmspace owned by another process. */
427 vmspace_acquire_ref(struct proc *p)
432 PROC_VMSPACE_LOCK(p);
435 PROC_VMSPACE_UNLOCK(p);
438 refcnt = vm->vm_refcnt;
440 if (refcnt <= 0) { /* Avoid 0->1 transition */
441 PROC_VMSPACE_UNLOCK(p);
444 } while (!atomic_fcmpset_int(&vm->vm_refcnt, &refcnt, refcnt + 1));
445 if (vm != p->p_vmspace) {
446 PROC_VMSPACE_UNLOCK(p);
450 PROC_VMSPACE_UNLOCK(p);
455 * Switch between vmspaces in an AIO kernel process.
457 * The new vmspace is either the vmspace of a user process obtained
458 * from an active AIO request or the initial vmspace of the AIO kernel
459 * process (when it is idling). Because user processes will block to
460 * drain any active AIO requests before proceeding in exit() or
461 * execve(), the reference count for vmspaces from AIO requests can
462 * never be 0. Similarly, AIO kernel processes hold an extra
463 * reference on their initial vmspace for the life of the process. As
464 * a result, the 'newvm' vmspace always has a non-zero reference
465 * count. This permits an additional reference on 'newvm' to be
466 * acquired via a simple atomic increment rather than the loop in
467 * vmspace_acquire_ref() above.
470 vmspace_switch_aio(struct vmspace *newvm)
472 struct vmspace *oldvm;
474 /* XXX: Need some way to assert that this is an aio daemon. */
476 KASSERT(newvm->vm_refcnt > 0,
477 ("vmspace_switch_aio: newvm unreferenced"));
479 oldvm = curproc->p_vmspace;
484 * Point to the new address space and refer to it.
486 curproc->p_vmspace = newvm;
487 atomic_add_int(&newvm->vm_refcnt, 1);
489 /* Activate the new mapping. */
490 pmap_activate(curthread);
496 _vm_map_lock(vm_map_t map, const char *file, int line)
500 mtx_lock_flags_(&map->system_mtx, 0, file, line);
502 sx_xlock_(&map->lock, file, line);
507 vm_map_entry_set_vnode_text(vm_map_entry_t entry, bool add)
509 vm_object_t object, object1;
512 if ((entry->eflags & MAP_ENTRY_VN_EXEC) == 0)
514 KASSERT((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0,
515 ("Submap with execs"));
516 object = entry->object.vm_object;
517 KASSERT(object != NULL, ("No object for text, entry %p", entry));
518 VM_OBJECT_RLOCK(object);
519 while ((object1 = object->backing_object) != NULL) {
520 VM_OBJECT_RLOCK(object1);
521 VM_OBJECT_RUNLOCK(object);
526 if (object->type == OBJT_DEAD) {
528 * For OBJT_DEAD objects, v_writecount was handled in
529 * vnode_pager_dealloc().
531 } else if (object->type == OBJT_VNODE) {
533 } else if (object->type == OBJT_SWAP) {
534 KASSERT((object->flags & OBJ_TMPFS_NODE) != 0,
535 ("vm_map_entry_set_vnode_text: swap and !TMPFS "
536 "entry %p, object %p, add %d", entry, object, add));
538 * Tmpfs VREG node, which was reclaimed, has
539 * OBJ_TMPFS_NODE flag set, but not OBJ_TMPFS. In
540 * this case there is no v_writecount to adjust.
542 if ((object->flags & OBJ_TMPFS) != 0)
543 vp = object->un_pager.swp.swp_tmpfs;
546 ("vm_map_entry_set_vnode_text: wrong object type, "
547 "entry %p, object %p, add %d", entry, object, add));
551 VOP_SET_TEXT_CHECKED(vp);
553 VOP_UNSET_TEXT_CHECKED(vp);
555 VM_OBJECT_RUNLOCK(object);
559 vm_map_process_deferred(void)
562 vm_map_entry_t entry, next;
566 entry = td->td_map_def_user;
567 td->td_map_def_user = NULL;
568 while (entry != NULL) {
570 MPASS((entry->eflags & (MAP_ENTRY_VN_WRITECNT |
571 MAP_ENTRY_VN_EXEC)) != (MAP_ENTRY_VN_WRITECNT |
573 if ((entry->eflags & MAP_ENTRY_VN_WRITECNT) != 0) {
575 * Decrement the object's writemappings and
576 * possibly the vnode's v_writecount.
578 KASSERT((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0,
579 ("Submap with writecount"));
580 object = entry->object.vm_object;
581 KASSERT(object != NULL, ("No object for writecount"));
582 vnode_pager_release_writecount(object, entry->start,
585 vm_map_entry_set_vnode_text(entry, false);
586 vm_map_entry_deallocate(entry, FALSE);
592 _vm_map_unlock(vm_map_t map, const char *file, int line)
596 mtx_unlock_flags_(&map->system_mtx, 0, file, line);
598 sx_xunlock_(&map->lock, file, line);
599 vm_map_process_deferred();
604 _vm_map_lock_read(vm_map_t map, const char *file, int line)
608 mtx_lock_flags_(&map->system_mtx, 0, file, line);
610 sx_slock_(&map->lock, file, line);
614 _vm_map_unlock_read(vm_map_t map, const char *file, int line)
618 mtx_unlock_flags_(&map->system_mtx, 0, file, line);
620 sx_sunlock_(&map->lock, file, line);
621 vm_map_process_deferred();
626 _vm_map_trylock(vm_map_t map, const char *file, int line)
630 error = map->system_map ?
631 !mtx_trylock_flags_(&map->system_mtx, 0, file, line) :
632 !sx_try_xlock_(&map->lock, file, line);
639 _vm_map_trylock_read(vm_map_t map, const char *file, int line)
643 error = map->system_map ?
644 !mtx_trylock_flags_(&map->system_mtx, 0, file, line) :
645 !sx_try_slock_(&map->lock, file, line);
650 * _vm_map_lock_upgrade: [ internal use only ]
652 * Tries to upgrade a read (shared) lock on the specified map to a write
653 * (exclusive) lock. Returns the value "0" if the upgrade succeeds and a
654 * non-zero value if the upgrade fails. If the upgrade fails, the map is
655 * returned without a read or write lock held.
657 * Requires that the map be read locked.
660 _vm_map_lock_upgrade(vm_map_t map, const char *file, int line)
662 unsigned int last_timestamp;
664 if (map->system_map) {
665 mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
667 if (!sx_try_upgrade_(&map->lock, file, line)) {
668 last_timestamp = map->timestamp;
669 sx_sunlock_(&map->lock, file, line);
670 vm_map_process_deferred();
672 * If the map's timestamp does not change while the
673 * map is unlocked, then the upgrade succeeds.
675 sx_xlock_(&map->lock, file, line);
676 if (last_timestamp != map->timestamp) {
677 sx_xunlock_(&map->lock, file, line);
687 _vm_map_lock_downgrade(vm_map_t map, const char *file, int line)
690 if (map->system_map) {
691 mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
693 sx_downgrade_(&map->lock, file, line);
699 * Returns a non-zero value if the caller holds a write (exclusive) lock
700 * on the specified map and the value "0" otherwise.
703 vm_map_locked(vm_map_t map)
707 return (mtx_owned(&map->system_mtx));
709 return (sx_xlocked(&map->lock));
714 _vm_map_assert_locked(vm_map_t map, const char *file, int line)
718 mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
720 sx_assert_(&map->lock, SA_XLOCKED, file, line);
723 #define VM_MAP_ASSERT_LOCKED(map) \
724 _vm_map_assert_locked(map, LOCK_FILE, LOCK_LINE)
727 static int enable_vmmap_check = 1;
729 static int enable_vmmap_check = 0;
731 SYSCTL_INT(_debug, OID_AUTO, vmmap_check, CTLFLAG_RWTUN,
732 &enable_vmmap_check, 0, "Enable vm map consistency checking");
735 _vm_map_assert_consistent(vm_map_t map)
737 vm_map_entry_t child, entry, prev;
738 vm_size_t max_left, max_right;
740 if (!enable_vmmap_check)
743 for (prev = &map->header; (entry = prev->next) != &map->header;
745 KASSERT(prev->end <= entry->start,
746 ("map %p prev->end = %jx, start = %jx", map,
747 (uintmax_t)prev->end, (uintmax_t)entry->start));
748 KASSERT(entry->start < entry->end,
749 ("map %p start = %jx, end = %jx", map,
750 (uintmax_t)entry->start, (uintmax_t)entry->end));
751 KASSERT(entry->end <= entry->next->start,
752 ("map %p end = %jx, next->start = %jx", map,
753 (uintmax_t)entry->end, (uintmax_t)entry->next->start));
754 KASSERT(entry->left == NULL ||
755 entry->left->start < entry->start,
756 ("map %p left->start = %jx, start = %jx", map,
757 (uintmax_t)entry->left->start, (uintmax_t)entry->start));
758 KASSERT(entry->right == NULL ||
759 entry->start < entry->right->start,
760 ("map %p start = %jx, right->start = %jx", map,
761 (uintmax_t)entry->start, (uintmax_t)entry->right->start));
763 max_left = (child != NULL) ? child->max_free :
764 entry->start - prev->end;
765 child = entry->right;
766 max_right = (child != NULL) ? child->max_free :
767 entry->next->start - entry->end;
768 KASSERT(entry->max_free == MAX(max_left, max_right),
769 ("map %p max = %jx, max_left = %jx, max_right = %jx", map,
770 (uintmax_t)entry->max_free,
771 (uintmax_t)max_left, (uintmax_t)max_right));
775 #define VM_MAP_ASSERT_CONSISTENT(map) \
776 _vm_map_assert_consistent(map)
778 #define VM_MAP_ASSERT_LOCKED(map)
779 #define VM_MAP_ASSERT_CONSISTENT(map)
780 #endif /* INVARIANTS */
783 * _vm_map_unlock_and_wait:
785 * Atomically releases the lock on the specified map and puts the calling
786 * thread to sleep. The calling thread will remain asleep until either
787 * vm_map_wakeup() is performed on the map or the specified timeout is
790 * WARNING! This function does not perform deferred deallocations of
791 * objects and map entries. Therefore, the calling thread is expected to
792 * reacquire the map lock after reawakening and later perform an ordinary
793 * unlock operation, such as vm_map_unlock(), before completing its
794 * operation on the map.
797 _vm_map_unlock_and_wait(vm_map_t map, int timo, const char *file, int line)
800 mtx_lock(&map_sleep_mtx);
802 mtx_unlock_flags_(&map->system_mtx, 0, file, line);
804 sx_xunlock_(&map->lock, file, line);
805 return (msleep(&map->root, &map_sleep_mtx, PDROP | PVM, "vmmaps",
812 * Awaken any threads that have slept on the map using
813 * vm_map_unlock_and_wait().
816 vm_map_wakeup(vm_map_t map)
820 * Acquire and release map_sleep_mtx to prevent a wakeup()
821 * from being performed (and lost) between the map unlock
822 * and the msleep() in _vm_map_unlock_and_wait().
824 mtx_lock(&map_sleep_mtx);
825 mtx_unlock(&map_sleep_mtx);
830 vm_map_busy(vm_map_t map)
833 VM_MAP_ASSERT_LOCKED(map);
838 vm_map_unbusy(vm_map_t map)
841 VM_MAP_ASSERT_LOCKED(map);
842 KASSERT(map->busy, ("vm_map_unbusy: not busy"));
843 if (--map->busy == 0 && (map->flags & MAP_BUSY_WAKEUP)) {
844 vm_map_modflags(map, 0, MAP_BUSY_WAKEUP);
850 vm_map_wait_busy(vm_map_t map)
853 VM_MAP_ASSERT_LOCKED(map);
855 vm_map_modflags(map, MAP_BUSY_WAKEUP, 0);
857 msleep(&map->busy, &map->system_mtx, 0, "mbusy", 0);
859 sx_sleep(&map->busy, &map->lock, 0, "mbusy", 0);
865 vmspace_resident_count(struct vmspace *vmspace)
867 return pmap_resident_count(vmspace_pmap(vmspace));
873 * Creates and returns a new empty VM map with
874 * the given physical map structure, and having
875 * the given lower and upper address bounds.
878 vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max)
882 result = uma_zalloc(mapzone, M_WAITOK);
883 CTR1(KTR_VM, "vm_map_create: %p", result);
884 _vm_map_init(result, pmap, min, max);
889 * Initialize an existing vm_map structure
890 * such as that in the vmspace structure.
893 _vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min, vm_offset_t max)
896 map->header.next = map->header.prev = &map->header;
897 map->header.eflags = MAP_ENTRY_HEADER;
898 map->needs_wakeup = FALSE;
901 map->header.end = min;
902 map->header.start = max;
911 vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min, vm_offset_t max)
914 _vm_map_init(map, pmap, min, max);
915 mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
916 sx_init(&map->lock, "user map");
920 * vm_map_entry_dispose: [ internal use only ]
922 * Inverse of vm_map_entry_create.
925 vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry)
927 uma_zfree(map->system_map ? kmapentzone : mapentzone, entry);
931 * vm_map_entry_create: [ internal use only ]
933 * Allocates a VM map entry for insertion.
934 * No entry fields are filled in.
936 static vm_map_entry_t
937 vm_map_entry_create(vm_map_t map)
939 vm_map_entry_t new_entry;
942 new_entry = uma_zalloc(kmapentzone, M_NOWAIT);
944 new_entry = uma_zalloc(mapentzone, M_WAITOK);
945 if (new_entry == NULL)
946 panic("vm_map_entry_create: kernel resources exhausted");
951 * vm_map_entry_set_behavior:
953 * Set the expected access behavior, either normal, random, or
957 vm_map_entry_set_behavior(vm_map_entry_t entry, u_char behavior)
959 entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) |
960 (behavior & MAP_ENTRY_BEHAV_MASK);
964 * vm_map_entry_max_free_{left,right}:
966 * Compute the size of the largest free gap between two entries,
967 * one the root of a tree and the other the ancestor of that root
968 * that is the least or greatest ancestor found on the search path.
970 static inline vm_size_t
971 vm_map_entry_max_free_left(vm_map_entry_t root, vm_map_entry_t left_ancestor)
974 return (root->left != NULL ?
975 root->left->max_free : root->start - left_ancestor->end);
978 static inline vm_size_t
979 vm_map_entry_max_free_right(vm_map_entry_t root, vm_map_entry_t right_ancestor)
982 return (root->right != NULL ?
983 root->right->max_free : right_ancestor->start - root->end);
986 #define SPLAY_LEFT_STEP(root, y, rlist, test) do { \
987 vm_size_t max_free; \
990 * Infer root->right->max_free == root->max_free when \
991 * y->max_free < root->max_free || root->max_free == 0. \
992 * Otherwise, look right to find it. \
995 max_free = root->max_free; \
996 KASSERT(max_free >= vm_map_entry_max_free_right(root, rlist), \
997 ("%s: max_free invariant fails", __func__)); \
998 if (y == NULL ? max_free > 0 : max_free - 1 < y->max_free) \
999 max_free = vm_map_entry_max_free_right(root, rlist); \
1000 if (y != NULL && (test)) { \
1001 /* Rotate right and make y root. */ \
1002 root->left = y->right; \
1004 if (max_free < y->max_free) \
1005 root->max_free = max_free = MAX(max_free, \
1006 vm_map_entry_max_free_left(root, y)); \
1010 /* Copy right->max_free. Put root on rlist. */ \
1011 root->max_free = max_free; \
1012 KASSERT(max_free == vm_map_entry_max_free_right(root, rlist), \
1013 ("%s: max_free not copied from right", __func__)); \
1014 root->left = rlist; \
1019 #define SPLAY_RIGHT_STEP(root, y, llist, test) do { \
1020 vm_size_t max_free; \
1023 * Infer root->left->max_free == root->max_free when \
1024 * y->max_free < root->max_free || root->max_free == 0. \
1025 * Otherwise, look left to find it. \
1028 max_free = root->max_free; \
1029 KASSERT(max_free >= vm_map_entry_max_free_left(root, llist), \
1030 ("%s: max_free invariant fails", __func__)); \
1031 if (y == NULL ? max_free > 0 : max_free - 1 < y->max_free) \
1032 max_free = vm_map_entry_max_free_left(root, llist); \
1033 if (y != NULL && (test)) { \
1034 /* Rotate left and make y root. */ \
1035 root->right = y->left; \
1037 if (max_free < y->max_free) \
1038 root->max_free = max_free = MAX(max_free, \
1039 vm_map_entry_max_free_right(root, y)); \
1043 /* Copy left->max_free. Put root on llist. */ \
1044 root->max_free = max_free; \
1045 KASSERT(max_free == vm_map_entry_max_free_left(root, llist), \
1046 ("%s: max_free not copied from left", __func__)); \
1047 root->right = llist; \
1053 * Walk down the tree until we find addr or a NULL pointer where addr would go,
1054 * breaking off left and right subtrees of nodes less than, or greater than
1055 * addr. Treat pointers to nodes with max_free < length as NULL pointers.
1056 * llist and rlist are the two sides in reverse order (bottom-up), with llist
1057 * linked by the right pointer and rlist linked by the left pointer in the
1058 * vm_map_entry, and both lists terminated by &map->header. This function, and
1059 * the subsequent call to vm_map_splay_merge, rely on the start and end address
1060 * values in &map->header.
1062 static vm_map_entry_t
1063 vm_map_splay_split(vm_map_t map, vm_offset_t addr, vm_size_t length,
1064 vm_map_entry_t *out_llist, vm_map_entry_t *out_rlist)
1066 vm_map_entry_t llist, rlist, root, y;
1068 llist = rlist = &map->header;
1070 while (root != NULL && root->max_free >= length) {
1071 KASSERT(llist->end <= root->start && root->end <= rlist->start,
1072 ("%s: root not within tree bounds", __func__));
1073 if (addr < root->start) {
1074 SPLAY_LEFT_STEP(root, y, rlist,
1075 y->max_free >= length && addr < y->start);
1076 } else if (addr >= root->end) {
1077 SPLAY_RIGHT_STEP(root, y, llist,
1078 y->max_free >= length && addr >= y->end);
1088 vm_map_splay_findnext(vm_map_entry_t root, vm_map_entry_t *iolist)
1090 vm_map_entry_t rlist, y;
1094 while (root != NULL)
1095 SPLAY_LEFT_STEP(root, y, rlist, true);
1100 vm_map_splay_findprev(vm_map_entry_t root, vm_map_entry_t *iolist)
1102 vm_map_entry_t llist, y;
1106 while (root != NULL)
1107 SPLAY_RIGHT_STEP(root, y, llist, true);
1112 vm_map_entry_swap(vm_map_entry_t *a, vm_map_entry_t *b)
1122 * Walk back up the two spines, flip the pointers and set max_free. The
1123 * subtrees of the root go at the bottom of llist and rlist.
1126 vm_map_splay_merge(vm_map_t map, vm_map_entry_t root,
1127 vm_map_entry_t llist, vm_map_entry_t rlist)
1129 vm_map_entry_t prev;
1130 vm_size_t max_free_left, max_free_right;
1132 max_free_left = vm_map_entry_max_free_left(root, llist);
1133 if (llist != &map->header) {
1137 * The max_free values of the children of llist are in
1138 * llist->max_free and max_free_left. Update with the
1141 llist->max_free = max_free_left =
1142 MAX(llist->max_free, max_free_left);
1143 vm_map_entry_swap(&llist->right, &prev);
1144 vm_map_entry_swap(&prev, &llist);
1145 } while (llist != &map->header);
1148 max_free_right = vm_map_entry_max_free_right(root, rlist);
1149 if (rlist != &map->header) {
1153 * The max_free values of the children of rlist are in
1154 * rlist->max_free and max_free_right. Update with the
1157 rlist->max_free = max_free_right =
1158 MAX(rlist->max_free, max_free_right);
1159 vm_map_entry_swap(&rlist->left, &prev);
1160 vm_map_entry_swap(&prev, &rlist);
1161 } while (rlist != &map->header);
1164 root->max_free = MAX(max_free_left, max_free_right);
1171 * The Sleator and Tarjan top-down splay algorithm with the
1172 * following variation. Max_free must be computed bottom-up, so
1173 * on the downward pass, maintain the left and right spines in
1174 * reverse order. Then, make a second pass up each side to fix
1175 * the pointers and compute max_free. The time bound is O(log n)
1178 * The new root is the vm_map_entry containing "addr", or else an
1179 * adjacent entry (lower if possible) if addr is not in the tree.
1181 * The map must be locked, and leaves it so.
1183 * Returns: the new root.
1185 static vm_map_entry_t
1186 vm_map_splay(vm_map_t map, vm_offset_t addr)
1188 vm_map_entry_t llist, rlist, root;
1190 root = vm_map_splay_split(map, addr, 0, &llist, &rlist);
1193 } else if (llist != &map->header) {
1195 * Recover the greatest node in the left
1196 * subtree and make it the root.
1199 llist = root->right;
1201 } else if (rlist != &map->header) {
1203 * Recover the least node in the right
1204 * subtree and make it the root.
1210 /* There is no root. */
1213 vm_map_splay_merge(map, root, llist, rlist);
1214 VM_MAP_ASSERT_CONSISTENT(map);
1219 * vm_map_entry_{un,}link:
1221 * Insert/remove entries from maps.
1224 vm_map_entry_link(vm_map_t map, vm_map_entry_t entry)
1226 vm_map_entry_t llist, rlist, root;
1229 "vm_map_entry_link: map %p, nentries %d, entry %p", map,
1230 map->nentries, entry);
1231 VM_MAP_ASSERT_LOCKED(map);
1233 root = vm_map_splay_split(map, entry->start, 0, &llist, &rlist);
1234 KASSERT(root == NULL,
1235 ("vm_map_entry_link: link object already mapped"));
1236 entry->prev = llist;
1237 entry->next = rlist;
1238 llist->next = rlist->prev = entry;
1239 entry->left = entry->right = NULL;
1240 vm_map_splay_merge(map, entry, llist, rlist);
1241 VM_MAP_ASSERT_CONSISTENT(map);
1244 enum unlink_merge_type {
1251 vm_map_entry_unlink(vm_map_t map, vm_map_entry_t entry,
1252 enum unlink_merge_type op)
1254 vm_map_entry_t llist, rlist, root, y;
1256 VM_MAP_ASSERT_LOCKED(map);
1257 root = vm_map_splay_split(map, entry->start, 0, &llist, &rlist);
1258 KASSERT(root != NULL,
1259 ("vm_map_entry_unlink: unlink object not mapped"));
1262 case UNLINK_MERGE_PREV:
1263 vm_map_splay_findprev(root, &llist);
1264 llist->end = root->end;
1267 llist = root->right;
1270 case UNLINK_MERGE_NEXT:
1271 vm_map_splay_findnext(root, &rlist);
1272 rlist->start = root->start;
1273 rlist->offset = root->offset;
1279 case UNLINK_MERGE_NONE:
1280 vm_map_splay_findprev(root, &llist);
1281 vm_map_splay_findnext(root, &rlist);
1282 if (llist != &map->header) {
1284 llist = root->right;
1286 } else if (rlist != &map->header) {
1295 y->prev = entry->prev;
1298 vm_map_splay_merge(map, root, llist, rlist);
1301 VM_MAP_ASSERT_CONSISTENT(map);
1303 CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map,
1304 map->nentries, entry);
1308 * vm_map_entry_resize:
1310 * Resize a vm_map_entry, recompute the amount of free space that
1311 * follows it and propagate that value up the tree.
1313 * The map must be locked, and leaves it so.
1316 vm_map_entry_resize(vm_map_t map, vm_map_entry_t entry, vm_size_t grow_amount)
1318 vm_map_entry_t llist, rlist, root;
1320 VM_MAP_ASSERT_LOCKED(map);
1321 root = vm_map_splay_split(map, entry->start, 0, &llist, &rlist);
1322 KASSERT(root != NULL,
1323 ("%s: resize object not mapped", __func__));
1324 vm_map_splay_findnext(root, &rlist);
1326 entry->end += grow_amount;
1327 vm_map_splay_merge(map, root, llist, rlist);
1328 VM_MAP_ASSERT_CONSISTENT(map);
1329 CTR4(KTR_VM, "%s: map %p, nentries %d, entry %p",
1330 __func__, map, map->nentries, entry);
1334 * vm_map_lookup_entry: [ internal use only ]
1336 * Finds the map entry containing (or
1337 * immediately preceding) the specified address
1338 * in the given map; the entry is returned
1339 * in the "entry" parameter. The boolean
1340 * result indicates whether the address is
1341 * actually contained in the map.
1344 vm_map_lookup_entry(
1346 vm_offset_t address,
1347 vm_map_entry_t *entry) /* OUT */
1349 vm_map_entry_t cur, lbound;
1353 * If the map is empty, then the map entry immediately preceding
1354 * "address" is the map's header.
1358 *entry = &map->header;
1361 if (address >= cur->start && cur->end > address) {
1365 if ((locked = vm_map_locked(map)) ||
1366 sx_try_upgrade(&map->lock)) {
1368 * Splay requires a write lock on the map. However, it only
1369 * restructures the binary search tree; it does not otherwise
1370 * change the map. Thus, the map's timestamp need not change
1371 * on a temporary upgrade.
1373 cur = vm_map_splay(map, address);
1375 sx_downgrade(&map->lock);
1378 * If "address" is contained within a map entry, the new root
1379 * is that map entry. Otherwise, the new root is a map entry
1380 * immediately before or after "address".
1382 if (address < cur->start) {
1383 *entry = &map->header;
1387 return (address < cur->end);
1390 * Since the map is only locked for read access, perform a
1391 * standard binary search tree lookup for "address".
1393 lbound = &map->header;
1395 if (address < cur->start) {
1397 } else if (cur->end <= address) {
1404 } while (cur != NULL);
1412 * Inserts the given whole VM object into the target
1413 * map at the specified address range. The object's
1414 * size should match that of the address range.
1416 * Requires that the map be locked, and leaves it so.
1418 * If object is non-NULL, ref count must be bumped by caller
1419 * prior to making call to account for the new entry.
1422 vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1423 vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max, int cow)
1425 vm_map_entry_t new_entry, prev_entry;
1427 vm_eflags_t protoeflags;
1428 vm_inherit_t inheritance;
1430 VM_MAP_ASSERT_LOCKED(map);
1431 KASSERT(object != kernel_object ||
1432 (cow & MAP_COPY_ON_WRITE) == 0,
1433 ("vm_map_insert: kernel object and COW"));
1434 KASSERT(object == NULL || (cow & MAP_NOFAULT) == 0,
1435 ("vm_map_insert: paradoxical MAP_NOFAULT request"));
1436 KASSERT((prot & ~max) == 0,
1437 ("prot %#x is not subset of max_prot %#x", prot, max));
1440 * Check that the start and end points are not bogus.
1442 if (start < vm_map_min(map) || end > vm_map_max(map) ||
1444 return (KERN_INVALID_ADDRESS);
1447 * Find the entry prior to the proposed starting address; if it's part
1448 * of an existing entry, this range is bogus.
1450 if (vm_map_lookup_entry(map, start, &prev_entry))
1451 return (KERN_NO_SPACE);
1454 * Assert that the next entry doesn't overlap the end point.
1456 if (prev_entry->next->start < end)
1457 return (KERN_NO_SPACE);
1459 if ((cow & MAP_CREATE_GUARD) != 0 && (object != NULL ||
1460 max != VM_PROT_NONE))
1461 return (KERN_INVALID_ARGUMENT);
1464 if (cow & MAP_COPY_ON_WRITE)
1465 protoeflags |= MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY;
1466 if (cow & MAP_NOFAULT)
1467 protoeflags |= MAP_ENTRY_NOFAULT;
1468 if (cow & MAP_DISABLE_SYNCER)
1469 protoeflags |= MAP_ENTRY_NOSYNC;
1470 if (cow & MAP_DISABLE_COREDUMP)
1471 protoeflags |= MAP_ENTRY_NOCOREDUMP;
1472 if (cow & MAP_STACK_GROWS_DOWN)
1473 protoeflags |= MAP_ENTRY_GROWS_DOWN;
1474 if (cow & MAP_STACK_GROWS_UP)
1475 protoeflags |= MAP_ENTRY_GROWS_UP;
1476 if (cow & MAP_VN_WRITECOUNT)
1477 protoeflags |= MAP_ENTRY_VN_WRITECNT;
1478 if (cow & MAP_VN_EXEC)
1479 protoeflags |= MAP_ENTRY_VN_EXEC;
1480 if ((cow & MAP_CREATE_GUARD) != 0)
1481 protoeflags |= MAP_ENTRY_GUARD;
1482 if ((cow & MAP_CREATE_STACK_GAP_DN) != 0)
1483 protoeflags |= MAP_ENTRY_STACK_GAP_DN;
1484 if ((cow & MAP_CREATE_STACK_GAP_UP) != 0)
1485 protoeflags |= MAP_ENTRY_STACK_GAP_UP;
1486 if (cow & MAP_INHERIT_SHARE)
1487 inheritance = VM_INHERIT_SHARE;
1489 inheritance = VM_INHERIT_DEFAULT;
1492 if ((cow & (MAP_ACC_NO_CHARGE | MAP_NOFAULT | MAP_CREATE_GUARD)) != 0)
1494 if ((cow & MAP_ACC_CHARGED) || ((prot & VM_PROT_WRITE) &&
1495 ((protoeflags & MAP_ENTRY_NEEDS_COPY) || object == NULL))) {
1496 if (!(cow & MAP_ACC_CHARGED) && !swap_reserve(end - start))
1497 return (KERN_RESOURCE_SHORTAGE);
1498 KASSERT(object == NULL ||
1499 (protoeflags & MAP_ENTRY_NEEDS_COPY) != 0 ||
1500 object->cred == NULL,
1501 ("overcommit: vm_map_insert o %p", object));
1502 cred = curthread->td_ucred;
1506 /* Expand the kernel pmap, if necessary. */
1507 if (map == kernel_map && end > kernel_vm_end)
1508 pmap_growkernel(end);
1509 if (object != NULL) {
1511 * OBJ_ONEMAPPING must be cleared unless this mapping
1512 * is trivially proven to be the only mapping for any
1513 * of the object's pages. (Object granularity
1514 * reference counting is insufficient to recognize
1515 * aliases with precision.)
1517 VM_OBJECT_WLOCK(object);
1518 if (object->ref_count > 1 || object->shadow_count != 0)
1519 vm_object_clear_flag(object, OBJ_ONEMAPPING);
1520 VM_OBJECT_WUNLOCK(object);
1521 } else if ((prev_entry->eflags & ~MAP_ENTRY_USER_WIRED) ==
1523 (cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP |
1524 MAP_VN_EXEC)) == 0 &&
1525 prev_entry->end == start && (prev_entry->cred == cred ||
1526 (prev_entry->object.vm_object != NULL &&
1527 prev_entry->object.vm_object->cred == cred)) &&
1528 vm_object_coalesce(prev_entry->object.vm_object,
1530 (vm_size_t)(prev_entry->end - prev_entry->start),
1531 (vm_size_t)(end - prev_entry->end), cred != NULL &&
1532 (protoeflags & MAP_ENTRY_NEEDS_COPY) == 0)) {
1534 * We were able to extend the object. Determine if we
1535 * can extend the previous map entry to include the
1536 * new range as well.
1538 if (prev_entry->inheritance == inheritance &&
1539 prev_entry->protection == prot &&
1540 prev_entry->max_protection == max &&
1541 prev_entry->wired_count == 0) {
1542 KASSERT((prev_entry->eflags & MAP_ENTRY_USER_WIRED) ==
1543 0, ("prev_entry %p has incoherent wiring",
1545 if ((prev_entry->eflags & MAP_ENTRY_GUARD) == 0)
1546 map->size += end - prev_entry->end;
1547 vm_map_entry_resize(map, prev_entry,
1548 end - prev_entry->end);
1549 vm_map_simplify_entry(map, prev_entry);
1550 return (KERN_SUCCESS);
1554 * If we can extend the object but cannot extend the
1555 * map entry, we have to create a new map entry. We
1556 * must bump the ref count on the extended object to
1557 * account for it. object may be NULL.
1559 object = prev_entry->object.vm_object;
1560 offset = prev_entry->offset +
1561 (prev_entry->end - prev_entry->start);
1562 vm_object_reference(object);
1563 if (cred != NULL && object != NULL && object->cred != NULL &&
1564 !(prev_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
1565 /* Object already accounts for this uid. */
1573 * Create a new entry
1575 new_entry = vm_map_entry_create(map);
1576 new_entry->start = start;
1577 new_entry->end = end;
1578 new_entry->cred = NULL;
1580 new_entry->eflags = protoeflags;
1581 new_entry->object.vm_object = object;
1582 new_entry->offset = offset;
1584 new_entry->inheritance = inheritance;
1585 new_entry->protection = prot;
1586 new_entry->max_protection = max;
1587 new_entry->wired_count = 0;
1588 new_entry->wiring_thread = NULL;
1589 new_entry->read_ahead = VM_FAULT_READ_AHEAD_INIT;
1590 new_entry->next_read = start;
1592 KASSERT(cred == NULL || !ENTRY_CHARGED(new_entry),
1593 ("overcommit: vm_map_insert leaks vm_map %p", new_entry));
1594 new_entry->cred = cred;
1597 * Insert the new entry into the list
1599 vm_map_entry_link(map, new_entry);
1600 if ((new_entry->eflags & MAP_ENTRY_GUARD) == 0)
1601 map->size += new_entry->end - new_entry->start;
1604 * Try to coalesce the new entry with both the previous and next
1605 * entries in the list. Previously, we only attempted to coalesce
1606 * with the previous entry when object is NULL. Here, we handle the
1607 * other cases, which are less common.
1609 vm_map_simplify_entry(map, new_entry);
1611 if ((cow & (MAP_PREFAULT | MAP_PREFAULT_PARTIAL)) != 0) {
1612 vm_map_pmap_enter(map, start, prot, object, OFF_TO_IDX(offset),
1613 end - start, cow & MAP_PREFAULT_PARTIAL);
1616 return (KERN_SUCCESS);
1622 * Find the first fit (lowest VM address) for "length" free bytes
1623 * beginning at address >= start in the given map.
1625 * In a vm_map_entry, "max_free" is the maximum amount of
1626 * contiguous free space between an entry in its subtree and a
1627 * neighbor of that entry. This allows finding a free region in
1628 * one path down the tree, so O(log n) amortized with splay
1631 * The map must be locked, and leaves it so.
1633 * Returns: starting address if sufficient space,
1634 * vm_map_max(map)-length+1 if insufficient space.
1637 vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length)
1639 vm_map_entry_t llist, rlist, root, y;
1640 vm_size_t left_length;
1641 vm_offset_t gap_end;
1644 * Request must fit within min/max VM address and must avoid
1647 start = MAX(start, vm_map_min(map));
1648 if (start >= vm_map_max(map) || length > vm_map_max(map) - start)
1649 return (vm_map_max(map) - length + 1);
1651 /* Empty tree means wide open address space. */
1652 if (map->root == NULL)
1656 * After splay_split, if start is within an entry, push it to the start
1657 * of the following gap. If rlist is at the end of the gap containing
1658 * start, save the end of that gap in gap_end to see if the gap is big
1659 * enough; otherwise set gap_end to start skip gap-checking and move
1660 * directly to a search of the right subtree.
1662 root = vm_map_splay_split(map, start, length, &llist, &rlist);
1663 gap_end = rlist->start;
1666 if (root->right != NULL)
1668 } else if (rlist != &map->header) {
1674 llist = root->right;
1677 vm_map_splay_merge(map, root, llist, rlist);
1678 VM_MAP_ASSERT_CONSISTENT(map);
1679 if (length <= gap_end - start)
1682 /* With max_free, can immediately tell if no solution. */
1683 if (root->right == NULL || length > root->right->max_free)
1684 return (vm_map_max(map) - length + 1);
1687 * Splay for the least large-enough gap in the right subtree.
1689 llist = rlist = &map->header;
1690 for (left_length = 0;;
1691 left_length = vm_map_entry_max_free_left(root, llist)) {
1692 if (length <= left_length)
1693 SPLAY_LEFT_STEP(root, y, rlist,
1694 length <= vm_map_entry_max_free_left(y, llist));
1696 SPLAY_RIGHT_STEP(root, y, llist,
1697 length > vm_map_entry_max_free_left(y, root));
1702 llist = root->right;
1704 if (rlist != &map->header) {
1708 vm_map_splay_merge(map, y, &map->header, rlist);
1710 vm_map_entry_max_free_left(y, root),
1711 vm_map_entry_max_free_right(y, &map->header));
1714 vm_map_splay_merge(map, root, llist, &map->header);
1715 VM_MAP_ASSERT_CONSISTENT(map);
1720 vm_map_fixed(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1721 vm_offset_t start, vm_size_t length, vm_prot_t prot,
1722 vm_prot_t max, int cow)
1727 end = start + length;
1728 KASSERT((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0 ||
1730 ("vm_map_fixed: non-NULL backing object for stack"));
1732 VM_MAP_RANGE_CHECK(map, start, end);
1733 if ((cow & MAP_CHECK_EXCL) == 0)
1734 vm_map_delete(map, start, end);
1735 if ((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) != 0) {
1736 result = vm_map_stack_locked(map, start, length, sgrowsiz,
1739 result = vm_map_insert(map, object, offset, start, end,
1746 static const int aslr_pages_rnd_64[2] = {0x1000, 0x10};
1747 static const int aslr_pages_rnd_32[2] = {0x100, 0x4};
1749 static int cluster_anon = 1;
1750 SYSCTL_INT(_vm, OID_AUTO, cluster_anon, CTLFLAG_RW,
1752 "Cluster anonymous mappings: 0 = no, 1 = yes if no hint, 2 = always");
1755 clustering_anon_allowed(vm_offset_t addr)
1758 switch (cluster_anon) {
1769 static long aslr_restarts;
1770 SYSCTL_LONG(_vm, OID_AUTO, aslr_restarts, CTLFLAG_RD,
1772 "Number of aslr failures");
1774 #define MAP_32BIT_MAX_ADDR ((vm_offset_t)1 << 31)
1777 * Searches for the specified amount of free space in the given map with the
1778 * specified alignment. Performs an address-ordered, first-fit search from
1779 * the given address "*addr", with an optional upper bound "max_addr". If the
1780 * parameter "alignment" is zero, then the alignment is computed from the
1781 * given (object, offset) pair so as to enable the greatest possible use of
1782 * superpage mappings. Returns KERN_SUCCESS and the address of the free space
1783 * in "*addr" if successful. Otherwise, returns KERN_NO_SPACE.
1785 * The map must be locked. Initially, there must be at least "length" bytes
1786 * of free space at the given address.
1789 vm_map_alignspace(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1790 vm_offset_t *addr, vm_size_t length, vm_offset_t max_addr,
1791 vm_offset_t alignment)
1793 vm_offset_t aligned_addr, free_addr;
1795 VM_MAP_ASSERT_LOCKED(map);
1797 KASSERT(free_addr == vm_map_findspace(map, free_addr, length),
1798 ("caller failed to provide space %#jx at address %p",
1799 (uintmax_t)length, (void *)free_addr));
1802 * At the start of every iteration, the free space at address
1803 * "*addr" is at least "length" bytes.
1806 pmap_align_superpage(object, offset, addr, length);
1807 else if ((*addr & (alignment - 1)) != 0) {
1808 *addr &= ~(alignment - 1);
1811 aligned_addr = *addr;
1812 if (aligned_addr == free_addr) {
1814 * Alignment did not change "*addr", so "*addr" must
1815 * still provide sufficient free space.
1817 return (KERN_SUCCESS);
1821 * Test for address wrap on "*addr". A wrapped "*addr" could
1822 * be a valid address, in which case vm_map_findspace() cannot
1823 * be relied upon to fail.
1825 if (aligned_addr < free_addr)
1826 return (KERN_NO_SPACE);
1827 *addr = vm_map_findspace(map, aligned_addr, length);
1828 if (*addr + length > vm_map_max(map) ||
1829 (max_addr != 0 && *addr + length > max_addr))
1830 return (KERN_NO_SPACE);
1832 if (free_addr == aligned_addr) {
1834 * If a successful call to vm_map_findspace() did not
1835 * change "*addr", then "*addr" must still be aligned
1836 * and provide sufficient free space.
1838 return (KERN_SUCCESS);
1844 * vm_map_find finds an unallocated region in the target address
1845 * map with the given length. The search is defined to be
1846 * first-fit from the specified address; the region found is
1847 * returned in the same parameter.
1849 * If object is non-NULL, ref count must be bumped by caller
1850 * prior to making call to account for the new entry.
1853 vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1854 vm_offset_t *addr, /* IN/OUT */
1855 vm_size_t length, vm_offset_t max_addr, int find_space,
1856 vm_prot_t prot, vm_prot_t max, int cow)
1858 vm_offset_t alignment, curr_min_addr, min_addr;
1859 int gap, pidx, rv, try;
1860 bool cluster, en_aslr, update_anon;
1862 KASSERT((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0 ||
1864 ("vm_map_find: non-NULL backing object for stack"));
1865 MPASS((cow & MAP_REMAP) == 0 || (find_space == VMFS_NO_SPACE &&
1866 (cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0));
1867 if (find_space == VMFS_OPTIMAL_SPACE && (object == NULL ||
1868 (object->flags & OBJ_COLORED) == 0))
1869 find_space = VMFS_ANY_SPACE;
1870 if (find_space >> 8 != 0) {
1871 KASSERT((find_space & 0xff) == 0, ("bad VMFS flags"));
1872 alignment = (vm_offset_t)1 << (find_space >> 8);
1875 en_aslr = (map->flags & MAP_ASLR) != 0;
1876 update_anon = cluster = clustering_anon_allowed(*addr) &&
1877 (map->flags & MAP_IS_SUB_MAP) == 0 && max_addr == 0 &&
1878 find_space != VMFS_NO_SPACE && object == NULL &&
1879 (cow & (MAP_INHERIT_SHARE | MAP_STACK_GROWS_UP |
1880 MAP_STACK_GROWS_DOWN)) == 0 && prot != PROT_NONE;
1881 curr_min_addr = min_addr = *addr;
1882 if (en_aslr && min_addr == 0 && !cluster &&
1883 find_space != VMFS_NO_SPACE &&
1884 (map->flags & MAP_ASLR_IGNSTART) != 0)
1885 curr_min_addr = min_addr = vm_map_min(map);
1889 curr_min_addr = map->anon_loc;
1890 if (curr_min_addr == 0)
1893 if (find_space != VMFS_NO_SPACE) {
1894 KASSERT(find_space == VMFS_ANY_SPACE ||
1895 find_space == VMFS_OPTIMAL_SPACE ||
1896 find_space == VMFS_SUPER_SPACE ||
1897 alignment != 0, ("unexpected VMFS flag"));
1900 * When creating an anonymous mapping, try clustering
1901 * with an existing anonymous mapping first.
1903 * We make up to two attempts to find address space
1904 * for a given find_space value. The first attempt may
1905 * apply randomization or may cluster with an existing
1906 * anonymous mapping. If this first attempt fails,
1907 * perform a first-fit search of the available address
1910 * If all tries failed, and find_space is
1911 * VMFS_OPTIMAL_SPACE, fallback to VMFS_ANY_SPACE.
1912 * Again enable clustering and randomization.
1919 * Second try: we failed either to find a
1920 * suitable region for randomizing the
1921 * allocation, or to cluster with an existing
1922 * mapping. Retry with free run.
1924 curr_min_addr = (map->flags & MAP_ASLR_IGNSTART) != 0 ?
1925 vm_map_min(map) : min_addr;
1926 atomic_add_long(&aslr_restarts, 1);
1929 if (try == 1 && en_aslr && !cluster) {
1931 * Find space for allocation, including
1932 * gap needed for later randomization.
1934 pidx = MAXPAGESIZES > 1 && pagesizes[1] != 0 &&
1935 (find_space == VMFS_SUPER_SPACE || find_space ==
1936 VMFS_OPTIMAL_SPACE) ? 1 : 0;
1937 gap = vm_map_max(map) > MAP_32BIT_MAX_ADDR &&
1938 (max_addr == 0 || max_addr > MAP_32BIT_MAX_ADDR) ?
1939 aslr_pages_rnd_64[pidx] : aslr_pages_rnd_32[pidx];
1940 *addr = vm_map_findspace(map, curr_min_addr,
1941 length + gap * pagesizes[pidx]);
1942 if (*addr + length + gap * pagesizes[pidx] >
1945 /* And randomize the start address. */
1946 *addr += (arc4random() % gap) * pagesizes[pidx];
1947 if (max_addr != 0 && *addr + length > max_addr)
1950 *addr = vm_map_findspace(map, curr_min_addr, length);
1951 if (*addr + length > vm_map_max(map) ||
1952 (max_addr != 0 && *addr + length > max_addr)) {
1963 if (find_space != VMFS_ANY_SPACE &&
1964 (rv = vm_map_alignspace(map, object, offset, addr, length,
1965 max_addr, alignment)) != KERN_SUCCESS) {
1966 if (find_space == VMFS_OPTIMAL_SPACE) {
1967 find_space = VMFS_ANY_SPACE;
1968 curr_min_addr = min_addr;
1969 cluster = update_anon;
1975 } else if ((cow & MAP_REMAP) != 0) {
1976 if (*addr < vm_map_min(map) ||
1977 *addr + length > vm_map_max(map) ||
1978 *addr + length <= length) {
1979 rv = KERN_INVALID_ADDRESS;
1982 vm_map_delete(map, *addr, *addr + length);
1984 if ((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) != 0) {
1985 rv = vm_map_stack_locked(map, *addr, length, sgrowsiz, prot,
1988 rv = vm_map_insert(map, object, offset, *addr, *addr + length,
1991 if (rv == KERN_SUCCESS && update_anon)
1992 map->anon_loc = *addr + length;
1999 * vm_map_find_min() is a variant of vm_map_find() that takes an
2000 * additional parameter (min_addr) and treats the given address
2001 * (*addr) differently. Specifically, it treats *addr as a hint
2002 * and not as the minimum address where the mapping is created.
2004 * This function works in two phases. First, it tries to
2005 * allocate above the hint. If that fails and the hint is
2006 * greater than min_addr, it performs a second pass, replacing
2007 * the hint with min_addr as the minimum address for the
2011 vm_map_find_min(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
2012 vm_offset_t *addr, vm_size_t length, vm_offset_t min_addr,
2013 vm_offset_t max_addr, int find_space, vm_prot_t prot, vm_prot_t max,
2021 rv = vm_map_find(map, object, offset, addr, length, max_addr,
2022 find_space, prot, max, cow);
2023 if (rv == KERN_SUCCESS || min_addr >= hint)
2025 *addr = hint = min_addr;
2030 * A map entry with any of the following flags set must not be merged with
2033 #define MAP_ENTRY_NOMERGE_MASK (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP | \
2034 MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP | MAP_ENTRY_VN_EXEC)
2037 vm_map_mergeable_neighbors(vm_map_entry_t prev, vm_map_entry_t entry)
2040 KASSERT((prev->eflags & MAP_ENTRY_NOMERGE_MASK) == 0 ||
2041 (entry->eflags & MAP_ENTRY_NOMERGE_MASK) == 0,
2042 ("vm_map_mergeable_neighbors: neither %p nor %p are mergeable",
2044 return (prev->end == entry->start &&
2045 prev->object.vm_object == entry->object.vm_object &&
2046 (prev->object.vm_object == NULL ||
2047 prev->offset + (prev->end - prev->start) == entry->offset) &&
2048 prev->eflags == entry->eflags &&
2049 prev->protection == entry->protection &&
2050 prev->max_protection == entry->max_protection &&
2051 prev->inheritance == entry->inheritance &&
2052 prev->wired_count == entry->wired_count &&
2053 prev->cred == entry->cred);
2057 vm_map_merged_neighbor_dispose(vm_map_t map, vm_map_entry_t entry)
2061 * If the backing object is a vnode object, vm_object_deallocate()
2062 * calls vrele(). However, vrele() does not lock the vnode because
2063 * the vnode has additional references. Thus, the map lock can be
2064 * kept without causing a lock-order reversal with the vnode lock.
2066 * Since we count the number of virtual page mappings in
2067 * object->un_pager.vnp.writemappings, the writemappings value
2068 * should not be adjusted when the entry is disposed of.
2070 if (entry->object.vm_object != NULL)
2071 vm_object_deallocate(entry->object.vm_object);
2072 if (entry->cred != NULL)
2073 crfree(entry->cred);
2074 vm_map_entry_dispose(map, entry);
2078 * vm_map_simplify_entry:
2080 * Simplify the given map entry by merging with either neighbor. This
2081 * routine also has the ability to merge with both neighbors.
2083 * The map must be locked.
2085 * This routine guarantees that the passed entry remains valid (though
2086 * possibly extended). When merging, this routine may delete one or
2090 vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry)
2092 vm_map_entry_t next, prev;
2094 if ((entry->eflags & MAP_ENTRY_NOMERGE_MASK) != 0)
2097 if (vm_map_mergeable_neighbors(prev, entry)) {
2098 vm_map_entry_unlink(map, prev, UNLINK_MERGE_NEXT);
2099 vm_map_merged_neighbor_dispose(map, prev);
2102 if (vm_map_mergeable_neighbors(entry, next)) {
2103 vm_map_entry_unlink(map, next, UNLINK_MERGE_PREV);
2104 vm_map_merged_neighbor_dispose(map, next);
2109 * vm_map_entry_back:
2111 * Allocate an object to back a map entry.
2114 vm_map_entry_back(vm_map_entry_t entry)
2118 KASSERT(entry->object.vm_object == NULL,
2119 ("map entry %p has backing object", entry));
2120 KASSERT((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0,
2121 ("map entry %p is a submap", entry));
2122 object = vm_object_allocate(OBJT_DEFAULT,
2123 atop(entry->end - entry->start));
2124 entry->object.vm_object = object;
2126 if (entry->cred != NULL) {
2127 object->cred = entry->cred;
2128 object->charge = entry->end - entry->start;
2134 * vm_map_entry_charge_object
2136 * If there is no object backing this entry, create one. Otherwise, if
2137 * the entry has cred, give it to the backing object.
2140 vm_map_entry_charge_object(vm_map_t map, vm_map_entry_t entry)
2143 VM_MAP_ASSERT_LOCKED(map);
2144 KASSERT((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0,
2145 ("map entry %p is a submap", entry));
2146 if (entry->object.vm_object == NULL && !map->system_map &&
2147 (entry->eflags & MAP_ENTRY_GUARD) == 0)
2148 vm_map_entry_back(entry);
2149 else if (entry->object.vm_object != NULL &&
2150 ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) &&
2151 entry->cred != NULL) {
2152 VM_OBJECT_WLOCK(entry->object.vm_object);
2153 KASSERT(entry->object.vm_object->cred == NULL,
2154 ("OVERCOMMIT: %s: both cred e %p", __func__, entry));
2155 entry->object.vm_object->cred = entry->cred;
2156 entry->object.vm_object->charge = entry->end - entry->start;
2157 VM_OBJECT_WUNLOCK(entry->object.vm_object);
2163 * vm_map_clip_start: [ internal use only ]
2165 * Asserts that the given entry begins at or after
2166 * the specified address; if necessary,
2167 * it splits the entry into two.
2169 #define vm_map_clip_start(map, entry, startaddr) \
2171 if (startaddr > entry->start) \
2172 _vm_map_clip_start(map, entry, startaddr); \
2176 * This routine is called only when it is known that
2177 * the entry must be split.
2180 _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start)
2182 vm_map_entry_t new_entry;
2184 VM_MAP_ASSERT_LOCKED(map);
2185 KASSERT(entry->end > start && entry->start < start,
2186 ("_vm_map_clip_start: invalid clip of entry %p", entry));
2189 * Create a backing object now, if none exists, so that more individual
2190 * objects won't be created after the map entry is split.
2192 vm_map_entry_charge_object(map, entry);
2194 /* Clone the entry. */
2195 new_entry = vm_map_entry_create(map);
2196 *new_entry = *entry;
2199 * Split off the front portion. Insert the new entry BEFORE this one,
2200 * so that this entry has the specified starting address.
2202 new_entry->end = start;
2203 entry->offset += (start - entry->start);
2204 entry->start = start;
2205 if (new_entry->cred != NULL)
2206 crhold(entry->cred);
2208 vm_map_entry_link(map, new_entry);
2210 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
2211 vm_object_reference(new_entry->object.vm_object);
2212 vm_map_entry_set_vnode_text(new_entry, true);
2214 * The object->un_pager.vnp.writemappings for the
2215 * object of MAP_ENTRY_VN_WRITECNT type entry shall be
2216 * kept as is here. The virtual pages are
2217 * re-distributed among the clipped entries, so the sum is
2224 * vm_map_clip_end: [ internal use only ]
2226 * Asserts that the given entry ends at or before
2227 * the specified address; if necessary,
2228 * it splits the entry into two.
2230 #define vm_map_clip_end(map, entry, endaddr) \
2232 if ((endaddr) < (entry->end)) \
2233 _vm_map_clip_end((map), (entry), (endaddr)); \
2237 * This routine is called only when it is known that
2238 * the entry must be split.
2241 _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end)
2243 vm_map_entry_t new_entry;
2245 VM_MAP_ASSERT_LOCKED(map);
2246 KASSERT(entry->start < end && entry->end > end,
2247 ("_vm_map_clip_end: invalid clip of entry %p", entry));
2250 * Create a backing object now, if none exists, so that more individual
2251 * objects won't be created after the map entry is split.
2253 vm_map_entry_charge_object(map, entry);
2255 /* Clone the entry. */
2256 new_entry = vm_map_entry_create(map);
2257 *new_entry = *entry;
2260 * Split off the back portion. Insert the new entry AFTER this one,
2261 * so that this entry has the specified ending address.
2263 new_entry->start = entry->end = end;
2264 new_entry->offset += (end - entry->start);
2265 if (new_entry->cred != NULL)
2266 crhold(entry->cred);
2268 vm_map_entry_link(map, new_entry);
2270 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
2271 vm_object_reference(new_entry->object.vm_object);
2272 vm_map_entry_set_vnode_text(new_entry, true);
2277 * vm_map_submap: [ kernel use only ]
2279 * Mark the given range as handled by a subordinate map.
2281 * This range must have been created with vm_map_find,
2282 * and no other operations may have been performed on this
2283 * range prior to calling vm_map_submap.
2285 * Only a limited number of operations can be performed
2286 * within this rage after calling vm_map_submap:
2288 * [Don't try vm_map_copy!]
2290 * To remove a submapping, one must first remove the
2291 * range from the superior map, and then destroy the
2292 * submap (if desired). [Better yet, don't try it.]
2301 vm_map_entry_t entry;
2304 result = KERN_INVALID_ARGUMENT;
2306 vm_map_lock(submap);
2307 submap->flags |= MAP_IS_SUB_MAP;
2308 vm_map_unlock(submap);
2312 VM_MAP_RANGE_CHECK(map, start, end);
2314 if (vm_map_lookup_entry(map, start, &entry)) {
2315 vm_map_clip_start(map, entry, start);
2317 entry = entry->next;
2319 vm_map_clip_end(map, entry, end);
2321 if ((entry->start == start) && (entry->end == end) &&
2322 ((entry->eflags & MAP_ENTRY_COW) == 0) &&
2323 (entry->object.vm_object == NULL)) {
2324 entry->object.sub_map = submap;
2325 entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
2326 result = KERN_SUCCESS;
2330 if (result != KERN_SUCCESS) {
2331 vm_map_lock(submap);
2332 submap->flags &= ~MAP_IS_SUB_MAP;
2333 vm_map_unlock(submap);
2339 * The maximum number of pages to map if MAP_PREFAULT_PARTIAL is specified
2341 #define MAX_INIT_PT 96
2344 * vm_map_pmap_enter:
2346 * Preload the specified map's pmap with mappings to the specified
2347 * object's memory-resident pages. No further physical pages are
2348 * allocated, and no further virtual pages are retrieved from secondary
2349 * storage. If the specified flags include MAP_PREFAULT_PARTIAL, then a
2350 * limited number of page mappings are created at the low-end of the
2351 * specified address range. (For this purpose, a superpage mapping
2352 * counts as one page mapping.) Otherwise, all resident pages within
2353 * the specified address range are mapped.
2356 vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
2357 vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags)
2360 vm_page_t p, p_start;
2361 vm_pindex_t mask, psize, threshold, tmpidx;
2363 if ((prot & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 || object == NULL)
2365 VM_OBJECT_RLOCK(object);
2366 if (object->type == OBJT_DEVICE || object->type == OBJT_SG) {
2367 VM_OBJECT_RUNLOCK(object);
2368 VM_OBJECT_WLOCK(object);
2369 if (object->type == OBJT_DEVICE || object->type == OBJT_SG) {
2370 pmap_object_init_pt(map->pmap, addr, object, pindex,
2372 VM_OBJECT_WUNLOCK(object);
2375 VM_OBJECT_LOCK_DOWNGRADE(object);
2379 if (psize + pindex > object->size) {
2380 if (object->size < pindex) {
2381 VM_OBJECT_RUNLOCK(object);
2384 psize = object->size - pindex;
2389 threshold = MAX_INIT_PT;
2391 p = vm_page_find_least(object, pindex);
2393 * Assert: the variable p is either (1) the page with the
2394 * least pindex greater than or equal to the parameter pindex
2398 p != NULL && (tmpidx = p->pindex - pindex) < psize;
2399 p = TAILQ_NEXT(p, listq)) {
2401 * don't allow an madvise to blow away our really
2402 * free pages allocating pv entries.
2404 if (((flags & MAP_PREFAULT_MADVISE) != 0 &&
2405 vm_page_count_severe()) ||
2406 ((flags & MAP_PREFAULT_PARTIAL) != 0 &&
2407 tmpidx >= threshold)) {
2411 if (p->valid == VM_PAGE_BITS_ALL) {
2412 if (p_start == NULL) {
2413 start = addr + ptoa(tmpidx);
2416 /* Jump ahead if a superpage mapping is possible. */
2417 if (p->psind > 0 && ((addr + ptoa(tmpidx)) &
2418 (pagesizes[p->psind] - 1)) == 0) {
2419 mask = atop(pagesizes[p->psind]) - 1;
2420 if (tmpidx + mask < psize &&
2421 vm_page_ps_test(p, PS_ALL_VALID, NULL)) {
2426 } else if (p_start != NULL) {
2427 pmap_enter_object(map->pmap, start, addr +
2428 ptoa(tmpidx), p_start, prot);
2432 if (p_start != NULL)
2433 pmap_enter_object(map->pmap, start, addr + ptoa(psize),
2435 VM_OBJECT_RUNLOCK(object);
2441 * Sets the protection of the specified address
2442 * region in the target map. If "set_max" is
2443 * specified, the maximum protection is to be set;
2444 * otherwise, only the current protection is affected.
2447 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
2448 vm_prot_t new_prot, boolean_t set_max)
2450 vm_map_entry_t current, entry, in_tran;
2457 return (KERN_SUCCESS);
2464 * Ensure that we are not concurrently wiring pages. vm_map_wire() may
2465 * need to fault pages into the map and will drop the map lock while
2466 * doing so, and the VM object may end up in an inconsistent state if we
2467 * update the protection on the map entry in between faults.
2469 vm_map_wait_busy(map);
2471 VM_MAP_RANGE_CHECK(map, start, end);
2473 if (!vm_map_lookup_entry(map, start, &entry))
2474 entry = entry->next;
2477 * Make a first pass to check for protection violations.
2479 for (current = entry; current->start < end; current = current->next) {
2480 if ((current->eflags & MAP_ENTRY_GUARD) != 0)
2482 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
2484 return (KERN_INVALID_ARGUMENT);
2486 if ((new_prot & current->max_protection) != new_prot) {
2488 return (KERN_PROTECTION_FAILURE);
2490 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0)
2495 * Postpone the operation until all in transition map entries
2496 * are stabilized. In-transition entry might already have its
2497 * pages wired and wired_count incremented, but
2498 * MAP_ENTRY_USER_WIRED flag not yet set, and visible to other
2499 * threads because the map lock is dropped. In this case we
2500 * would miss our call to vm_fault_copy_entry().
2502 if (in_tran != NULL) {
2503 in_tran->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2504 vm_map_unlock_and_wait(map, 0);
2509 * Before changing the protections, try to reserve swap space for any
2510 * private (i.e., copy-on-write) mappings that are transitioning from
2511 * read-only to read/write access. If a reservation fails, break out
2512 * of this loop early and let the next loop simplify the entries, since
2513 * some may now be mergeable.
2516 vm_map_clip_start(map, entry, start);
2517 for (current = entry; current->start < end; current = current->next) {
2519 vm_map_clip_end(map, current, end);
2522 ((new_prot & ~(current->protection)) & VM_PROT_WRITE) == 0 ||
2523 ENTRY_CHARGED(current) ||
2524 (current->eflags & MAP_ENTRY_GUARD) != 0) {
2528 cred = curthread->td_ucred;
2529 obj = current->object.vm_object;
2531 if (obj == NULL || (current->eflags & MAP_ENTRY_NEEDS_COPY)) {
2532 if (!swap_reserve(current->end - current->start)) {
2533 rv = KERN_RESOURCE_SHORTAGE;
2538 current->cred = cred;
2542 VM_OBJECT_WLOCK(obj);
2543 if (obj->type != OBJT_DEFAULT && obj->type != OBJT_SWAP) {
2544 VM_OBJECT_WUNLOCK(obj);
2549 * Charge for the whole object allocation now, since
2550 * we cannot distinguish between non-charged and
2551 * charged clipped mapping of the same object later.
2553 KASSERT(obj->charge == 0,
2554 ("vm_map_protect: object %p overcharged (entry %p)",
2556 if (!swap_reserve(ptoa(obj->size))) {
2557 VM_OBJECT_WUNLOCK(obj);
2558 rv = KERN_RESOURCE_SHORTAGE;
2565 obj->charge = ptoa(obj->size);
2566 VM_OBJECT_WUNLOCK(obj);
2570 * If enough swap space was available, go back and fix up protections.
2571 * Otherwise, just simplify entries, since some may have been modified.
2572 * [Note that clipping is not necessary the second time.]
2574 for (current = entry; current->start < end;
2575 vm_map_simplify_entry(map, current), current = current->next) {
2576 if (rv != KERN_SUCCESS ||
2577 (current->eflags & MAP_ENTRY_GUARD) != 0)
2580 old_prot = current->protection;
2583 current->protection =
2584 (current->max_protection = new_prot) &
2587 current->protection = new_prot;
2590 * For user wired map entries, the normal lazy evaluation of
2591 * write access upgrades through soft page faults is
2592 * undesirable. Instead, immediately copy any pages that are
2593 * copy-on-write and enable write access in the physical map.
2595 if ((current->eflags & MAP_ENTRY_USER_WIRED) != 0 &&
2596 (current->protection & VM_PROT_WRITE) != 0 &&
2597 (old_prot & VM_PROT_WRITE) == 0)
2598 vm_fault_copy_entry(map, map, current, current, NULL);
2601 * When restricting access, update the physical map. Worry
2602 * about copy-on-write here.
2604 if ((old_prot & ~current->protection) != 0) {
2605 #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
2607 pmap_protect(map->pmap, current->start,
2609 current->protection & MASK(current));
2620 * This routine traverses a processes map handling the madvise
2621 * system call. Advisories are classified as either those effecting
2622 * the vm_map_entry structure, or those effecting the underlying
2632 vm_map_entry_t current, entry;
2636 * Some madvise calls directly modify the vm_map_entry, in which case
2637 * we need to use an exclusive lock on the map and we need to perform
2638 * various clipping operations. Otherwise we only need a read-lock
2643 case MADV_SEQUENTIAL:
2660 vm_map_lock_read(map);
2667 * Locate starting entry and clip if necessary.
2669 VM_MAP_RANGE_CHECK(map, start, end);
2671 if (vm_map_lookup_entry(map, start, &entry)) {
2673 vm_map_clip_start(map, entry, start);
2675 entry = entry->next;
2680 * madvise behaviors that are implemented in the vm_map_entry.
2682 * We clip the vm_map_entry so that behavioral changes are
2683 * limited to the specified address range.
2685 for (current = entry; current->start < end;
2686 current = current->next) {
2687 if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
2690 vm_map_clip_end(map, current, end);
2694 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
2696 case MADV_SEQUENTIAL:
2697 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
2700 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
2703 current->eflags |= MAP_ENTRY_NOSYNC;
2706 current->eflags &= ~MAP_ENTRY_NOSYNC;
2709 current->eflags |= MAP_ENTRY_NOCOREDUMP;
2712 current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
2717 vm_map_simplify_entry(map, current);
2721 vm_pindex_t pstart, pend;
2724 * madvise behaviors that are implemented in the underlying
2727 * Since we don't clip the vm_map_entry, we have to clip
2728 * the vm_object pindex and count.
2730 for (current = entry; current->start < end;
2731 current = current->next) {
2732 vm_offset_t useEnd, useStart;
2734 if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
2737 pstart = OFF_TO_IDX(current->offset);
2738 pend = pstart + atop(current->end - current->start);
2739 useStart = current->start;
2740 useEnd = current->end;
2742 if (current->start < start) {
2743 pstart += atop(start - current->start);
2746 if (current->end > end) {
2747 pend -= atop(current->end - end);
2755 * Perform the pmap_advise() before clearing
2756 * PGA_REFERENCED in vm_page_advise(). Otherwise, a
2757 * concurrent pmap operation, such as pmap_remove(),
2758 * could clear a reference in the pmap and set
2759 * PGA_REFERENCED on the page before the pmap_advise()
2760 * had completed. Consequently, the page would appear
2761 * referenced based upon an old reference that
2762 * occurred before this pmap_advise() ran.
2764 if (behav == MADV_DONTNEED || behav == MADV_FREE)
2765 pmap_advise(map->pmap, useStart, useEnd,
2768 vm_object_madvise(current->object.vm_object, pstart,
2772 * Pre-populate paging structures in the
2773 * WILLNEED case. For wired entries, the
2774 * paging structures are already populated.
2776 if (behav == MADV_WILLNEED &&
2777 current->wired_count == 0) {
2778 vm_map_pmap_enter(map,
2780 current->protection,
2781 current->object.vm_object,
2783 ptoa(pend - pstart),
2784 MAP_PREFAULT_MADVISE
2788 vm_map_unlock_read(map);
2797 * Sets the inheritance of the specified address
2798 * range in the target map. Inheritance
2799 * affects how the map will be shared with
2800 * child maps at the time of vmspace_fork.
2803 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
2804 vm_inherit_t new_inheritance)
2806 vm_map_entry_t entry;
2807 vm_map_entry_t temp_entry;
2809 switch (new_inheritance) {
2810 case VM_INHERIT_NONE:
2811 case VM_INHERIT_COPY:
2812 case VM_INHERIT_SHARE:
2813 case VM_INHERIT_ZERO:
2816 return (KERN_INVALID_ARGUMENT);
2819 return (KERN_SUCCESS);
2821 VM_MAP_RANGE_CHECK(map, start, end);
2822 if (vm_map_lookup_entry(map, start, &temp_entry)) {
2824 vm_map_clip_start(map, entry, start);
2826 entry = temp_entry->next;
2827 while (entry->start < end) {
2828 vm_map_clip_end(map, entry, end);
2829 if ((entry->eflags & MAP_ENTRY_GUARD) == 0 ||
2830 new_inheritance != VM_INHERIT_ZERO)
2831 entry->inheritance = new_inheritance;
2832 vm_map_simplify_entry(map, entry);
2833 entry = entry->next;
2836 return (KERN_SUCCESS);
2840 * vm_map_entry_in_transition:
2842 * Release the map lock, and sleep until the entry is no longer in
2843 * transition. Awake and acquire the map lock. If the map changed while
2844 * another held the lock, lookup a possibly-changed entry at or after the
2845 * 'start' position of the old entry.
2847 static vm_map_entry_t
2848 vm_map_entry_in_transition(vm_map_t map, vm_offset_t in_start,
2849 vm_offset_t *io_end, bool holes_ok, vm_map_entry_t in_entry)
2851 vm_map_entry_t entry;
2853 u_int last_timestamp;
2855 VM_MAP_ASSERT_LOCKED(map);
2856 KASSERT((in_entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0,
2857 ("not in-tranition map entry %p", in_entry));
2859 * We have not yet clipped the entry.
2861 start = MAX(in_start, in_entry->start);
2862 in_entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2863 last_timestamp = map->timestamp;
2864 if (vm_map_unlock_and_wait(map, 0)) {
2866 * Allow interruption of user wiring/unwiring?
2870 if (last_timestamp + 1 == map->timestamp)
2874 * Look again for the entry because the map was modified while it was
2875 * unlocked. Specifically, the entry may have been clipped, merged, or
2878 if (!vm_map_lookup_entry(map, start, &entry)) {
2883 entry = entry->next;
2891 * Implements both kernel and user unwiring.
2894 vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
2897 vm_map_entry_t entry, first_entry;
2899 bool first_iteration, holes_ok, need_wakeup, user_unwire;
2902 return (KERN_SUCCESS);
2903 holes_ok = (flags & VM_MAP_WIRE_HOLESOK) != 0;
2904 user_unwire = (flags & VM_MAP_WIRE_USER) != 0;
2906 VM_MAP_RANGE_CHECK(map, start, end);
2907 if (!vm_map_lookup_entry(map, start, &first_entry)) {
2909 first_entry = first_entry->next;
2912 return (KERN_INVALID_ADDRESS);
2915 first_iteration = true;
2916 entry = first_entry;
2918 while (entry->start < end) {
2919 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
2921 * We have not yet clipped the entry.
2923 entry = vm_map_entry_in_transition(map, start, &end,
2925 if (entry == NULL) {
2926 if (first_iteration) {
2928 return (KERN_INVALID_ADDRESS);
2930 rv = KERN_INVALID_ADDRESS;
2933 first_entry = first_iteration ? entry : NULL;
2936 first_iteration = false;
2937 vm_map_clip_start(map, entry, start);
2938 vm_map_clip_end(map, entry, end);
2940 * Mark the entry in case the map lock is released. (See
2943 KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 &&
2944 entry->wiring_thread == NULL,
2945 ("owned map entry %p", entry));
2946 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
2947 entry->wiring_thread = curthread;
2949 * Check the map for holes in the specified region.
2950 * If holes_ok, skip this check.
2953 (entry->end < end && entry->next->start > entry->end)) {
2955 rv = KERN_INVALID_ADDRESS;
2959 * If system unwiring, require that the entry is system wired.
2962 vm_map_entry_system_wired_count(entry) == 0) {
2964 rv = KERN_INVALID_ARGUMENT;
2967 entry = entry->next;
2969 need_wakeup = false;
2970 if (first_entry == NULL &&
2971 !vm_map_lookup_entry(map, start, &first_entry)) {
2972 KASSERT(holes_ok, ("vm_map_unwire: lookup failed"));
2973 first_entry = first_entry->next;
2975 for (entry = first_entry; entry->start < end; entry = entry->next) {
2977 * If holes_ok was specified, an empty
2978 * space in the unwired region could have been mapped
2979 * while the map lock was dropped for draining
2980 * MAP_ENTRY_IN_TRANSITION. Moreover, another thread
2981 * could be simultaneously wiring this new mapping
2982 * entry. Detect these cases and skip any entries
2983 * marked as in transition by us.
2985 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 ||
2986 entry->wiring_thread != curthread) {
2988 ("vm_map_unwire: !HOLESOK and new/changed entry"));
2992 if (rv == KERN_SUCCESS && (!user_unwire ||
2993 (entry->eflags & MAP_ENTRY_USER_WIRED))) {
2994 if (entry->wired_count == 1)
2995 vm_map_entry_unwire(map, entry);
2997 entry->wired_count--;
2999 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
3001 KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0,
3002 ("vm_map_unwire: in-transition flag missing %p", entry));
3003 KASSERT(entry->wiring_thread == curthread,
3004 ("vm_map_unwire: alien wire %p", entry));
3005 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
3006 entry->wiring_thread = NULL;
3007 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
3008 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
3011 vm_map_simplify_entry(map, entry);
3020 vm_map_wire_user_count_sub(u_long npages)
3023 atomic_subtract_long(&vm_user_wire_count, npages);
3027 vm_map_wire_user_count_add(u_long npages)
3031 wired = vm_user_wire_count;
3033 if (npages + wired > vm_page_max_user_wired)
3035 } while (!atomic_fcmpset_long(&vm_user_wire_count, &wired,
3042 * vm_map_wire_entry_failure:
3044 * Handle a wiring failure on the given entry.
3046 * The map should be locked.
3049 vm_map_wire_entry_failure(vm_map_t map, vm_map_entry_t entry,
3050 vm_offset_t failed_addr)
3053 VM_MAP_ASSERT_LOCKED(map);
3054 KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 &&
3055 entry->wired_count == 1,
3056 ("vm_map_wire_entry_failure: entry %p isn't being wired", entry));
3057 KASSERT(failed_addr < entry->end,
3058 ("vm_map_wire_entry_failure: entry %p was fully wired", entry));
3061 * If any pages at the start of this entry were successfully wired,
3064 if (failed_addr > entry->start) {
3065 pmap_unwire(map->pmap, entry->start, failed_addr);
3066 vm_object_unwire(entry->object.vm_object, entry->offset,
3067 failed_addr - entry->start, PQ_ACTIVE);
3071 * Assign an out-of-range value to represent the failure to wire this
3074 entry->wired_count = -1;
3078 vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end, int flags)
3083 rv = vm_map_wire_locked(map, start, end, flags);
3090 * vm_map_wire_locked:
3092 * Implements both kernel and user wiring. Returns with the map locked,
3093 * the map lock may be dropped.
3096 vm_map_wire_locked(vm_map_t map, vm_offset_t start, vm_offset_t end, int flags)
3098 vm_map_entry_t entry, first_entry, tmp_entry;
3099 vm_offset_t faddr, saved_end, saved_start;
3101 u_int last_timestamp;
3103 bool first_iteration, holes_ok, need_wakeup, user_wire;
3106 VM_MAP_ASSERT_LOCKED(map);
3109 return (KERN_SUCCESS);
3111 if (flags & VM_MAP_WIRE_WRITE)
3112 prot |= VM_PROT_WRITE;
3113 holes_ok = (flags & VM_MAP_WIRE_HOLESOK) != 0;
3114 user_wire = (flags & VM_MAP_WIRE_USER) != 0;
3115 VM_MAP_RANGE_CHECK(map, start, end);
3116 if (!vm_map_lookup_entry(map, start, &first_entry)) {
3118 first_entry = first_entry->next;
3120 return (KERN_INVALID_ADDRESS);
3122 first_iteration = true;
3123 entry = first_entry;
3124 while (entry->start < end) {
3125 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
3127 * We have not yet clipped the entry.
3129 entry = vm_map_entry_in_transition(map, start, &end,
3131 if (entry == NULL) {
3132 if (first_iteration)
3133 return (KERN_INVALID_ADDRESS);
3134 rv = KERN_INVALID_ADDRESS;
3137 first_entry = first_iteration ? entry : NULL;
3140 first_iteration = false;
3141 vm_map_clip_start(map, entry, start);
3142 vm_map_clip_end(map, entry, end);
3144 * Mark the entry in case the map lock is released. (See
3147 KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 &&
3148 entry->wiring_thread == NULL,
3149 ("owned map entry %p", entry));
3150 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
3151 entry->wiring_thread = curthread;
3152 if ((entry->protection & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0
3153 || (entry->protection & prot) != prot) {
3154 entry->eflags |= MAP_ENTRY_WIRE_SKIPPED;
3157 rv = KERN_INVALID_ADDRESS;
3160 } else if (entry->wired_count == 0) {
3161 entry->wired_count++;
3163 npages = atop(entry->end - entry->start);
3164 if (user_wire && !vm_map_wire_user_count_add(npages)) {
3165 vm_map_wire_entry_failure(map, entry,
3168 rv = KERN_RESOURCE_SHORTAGE;
3173 * Release the map lock, relying on the in-transition
3174 * mark. Mark the map busy for fork.
3176 saved_start = entry->start;
3177 saved_end = entry->end;
3178 last_timestamp = map->timestamp;
3182 faddr = saved_start;
3185 * Simulate a fault to get the page and enter
3186 * it into the physical map.
3188 if ((rv = vm_fault(map, faddr, VM_PROT_NONE,
3189 VM_FAULT_WIRE)) != KERN_SUCCESS)
3191 } while ((faddr += PAGE_SIZE) < saved_end);
3194 if (last_timestamp + 1 != map->timestamp) {
3196 * Look again for the entry because the map was
3197 * modified while it was unlocked. The entry
3198 * may have been clipped, but NOT merged or
3201 if (!vm_map_lookup_entry(map, saved_start,
3204 ("vm_map_wire: lookup failed"));
3205 if (entry == first_entry)
3206 first_entry = tmp_entry;
3210 while (entry->end < saved_end) {
3212 * In case of failure, handle entries
3213 * that were not fully wired here;
3214 * fully wired entries are handled
3217 if (rv != KERN_SUCCESS &&
3219 vm_map_wire_entry_failure(map,
3221 entry = entry->next;
3224 if (rv != KERN_SUCCESS) {
3225 vm_map_wire_entry_failure(map, entry, faddr);
3227 vm_map_wire_user_count_sub(npages);
3231 } else if (!user_wire ||
3232 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
3233 entry->wired_count++;
3236 * Check the map for holes in the specified region.
3237 * If holes_ok was specified, skip this check.
3240 entry->end < end && entry->next->start > entry->end) {
3242 rv = KERN_INVALID_ADDRESS;
3245 entry = entry->next;
3249 need_wakeup = false;
3250 if (first_entry == NULL &&
3251 !vm_map_lookup_entry(map, start, &first_entry)) {
3252 KASSERT(holes_ok, ("vm_map_wire: lookup failed"));
3253 first_entry = first_entry->next;
3255 for (entry = first_entry; entry->start < end; entry = entry->next) {
3257 * If holes_ok was specified, an empty
3258 * space in the unwired region could have been mapped
3259 * while the map lock was dropped for faulting in the
3260 * pages or draining MAP_ENTRY_IN_TRANSITION.
3261 * Moreover, another thread could be simultaneously
3262 * wiring this new mapping entry. Detect these cases
3263 * and skip any entries marked as in transition not by us.
3265 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 ||
3266 entry->wiring_thread != curthread) {
3268 ("vm_map_wire: !HOLESOK and new/changed entry"));
3272 if ((entry->eflags & MAP_ENTRY_WIRE_SKIPPED) != 0) {
3274 } else if (rv == KERN_SUCCESS) {
3276 entry->eflags |= MAP_ENTRY_USER_WIRED;
3277 } else if (entry->wired_count == -1) {
3279 * Wiring failed on this entry. Thus, unwiring is
3282 entry->wired_count = 0;
3283 } else if (!user_wire ||
3284 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
3286 * Undo the wiring. Wiring succeeded on this entry
3287 * but failed on a later entry.
3289 if (entry->wired_count == 1) {
3290 vm_map_entry_unwire(map, entry);
3292 vm_map_wire_user_count_sub(
3293 atop(entry->end - entry->start));
3295 entry->wired_count--;
3297 KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0,
3298 ("vm_map_wire: in-transition flag missing %p", entry));
3299 KASSERT(entry->wiring_thread == curthread,
3300 ("vm_map_wire: alien wire %p", entry));
3301 entry->eflags &= ~(MAP_ENTRY_IN_TRANSITION |
3302 MAP_ENTRY_WIRE_SKIPPED);
3303 entry->wiring_thread = NULL;
3304 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
3305 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
3308 vm_map_simplify_entry(map, entry);
3318 * Push any dirty cached pages in the address range to their pager.
3319 * If syncio is TRUE, dirty pages are written synchronously.
3320 * If invalidate is TRUE, any cached pages are freed as well.
3322 * If the size of the region from start to end is zero, we are
3323 * supposed to flush all modified pages within the region containing
3324 * start. Unfortunately, a region can be split or coalesced with
3325 * neighboring regions, making it difficult to determine what the
3326 * original region was. Therefore, we approximate this requirement by
3327 * flushing the current region containing start.
3329 * Returns an error if any part of the specified range is not mapped.
3337 boolean_t invalidate)
3339 vm_map_entry_t current;
3340 vm_map_entry_t entry;
3343 vm_ooffset_t offset;
3344 unsigned int last_timestamp;
3347 vm_map_lock_read(map);
3348 VM_MAP_RANGE_CHECK(map, start, end);
3349 if (!vm_map_lookup_entry(map, start, &entry)) {
3350 vm_map_unlock_read(map);
3351 return (KERN_INVALID_ADDRESS);
3352 } else if (start == end) {
3353 start = entry->start;
3357 * Make a first pass to check for user-wired memory and holes.
3359 for (current = entry; current->start < end; current = current->next) {
3360 if (invalidate && (current->eflags & MAP_ENTRY_USER_WIRED)) {
3361 vm_map_unlock_read(map);
3362 return (KERN_INVALID_ARGUMENT);
3364 if (end > current->end &&
3365 current->end != current->next->start) {
3366 vm_map_unlock_read(map);
3367 return (KERN_INVALID_ADDRESS);
3372 pmap_remove(map->pmap, start, end);
3376 * Make a second pass, cleaning/uncaching pages from the indicated
3379 for (current = entry; current->start < end;) {
3380 offset = current->offset + (start - current->start);
3381 size = (end <= current->end ? end : current->end) - start;
3382 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
3384 vm_map_entry_t tentry;
3387 smap = current->object.sub_map;
3388 vm_map_lock_read(smap);
3389 (void) vm_map_lookup_entry(smap, offset, &tentry);
3390 tsize = tentry->end - offset;
3393 object = tentry->object.vm_object;
3394 offset = tentry->offset + (offset - tentry->start);
3395 vm_map_unlock_read(smap);
3397 object = current->object.vm_object;
3399 vm_object_reference(object);
3400 last_timestamp = map->timestamp;
3401 vm_map_unlock_read(map);
3402 if (!vm_object_sync(object, offset, size, syncio, invalidate))
3405 vm_object_deallocate(object);
3406 vm_map_lock_read(map);
3407 if (last_timestamp == map->timestamp ||
3408 !vm_map_lookup_entry(map, start, ¤t))
3409 current = current->next;
3412 vm_map_unlock_read(map);
3413 return (failed ? KERN_FAILURE : KERN_SUCCESS);
3417 * vm_map_entry_unwire: [ internal use only ]
3419 * Make the region specified by this entry pageable.
3421 * The map in question should be locked.
3422 * [This is the reason for this routine's existence.]
3425 vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
3429 VM_MAP_ASSERT_LOCKED(map);
3430 KASSERT(entry->wired_count > 0,
3431 ("vm_map_entry_unwire: entry %p isn't wired", entry));
3433 size = entry->end - entry->start;
3434 if ((entry->eflags & MAP_ENTRY_USER_WIRED) != 0)
3435 vm_map_wire_user_count_sub(atop(size));
3436 pmap_unwire(map->pmap, entry->start, entry->end);
3437 vm_object_unwire(entry->object.vm_object, entry->offset, size,
3439 entry->wired_count = 0;
3443 vm_map_entry_deallocate(vm_map_entry_t entry, boolean_t system_map)
3446 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0)
3447 vm_object_deallocate(entry->object.vm_object);
3448 uma_zfree(system_map ? kmapentzone : mapentzone, entry);
3452 * vm_map_entry_delete: [ internal use only ]
3454 * Deallocate the given entry from the target map.
3457 vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry)
3460 vm_pindex_t offidxstart, offidxend, count, size1;
3463 vm_map_entry_unlink(map, entry, UNLINK_MERGE_NONE);
3464 object = entry->object.vm_object;
3466 if ((entry->eflags & MAP_ENTRY_GUARD) != 0) {
3467 MPASS(entry->cred == NULL);
3468 MPASS((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0);
3469 MPASS(object == NULL);
3470 vm_map_entry_deallocate(entry, map->system_map);
3474 size = entry->end - entry->start;
3477 if (entry->cred != NULL) {
3478 swap_release_by_cred(size, entry->cred);
3479 crfree(entry->cred);
3482 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
3484 KASSERT(entry->cred == NULL || object->cred == NULL ||
3485 (entry->eflags & MAP_ENTRY_NEEDS_COPY),
3486 ("OVERCOMMIT vm_map_entry_delete: both cred %p", entry));
3488 offidxstart = OFF_TO_IDX(entry->offset);
3489 offidxend = offidxstart + count;
3490 VM_OBJECT_WLOCK(object);
3491 if (object->ref_count != 1 && ((object->flags & (OBJ_NOSPLIT |
3492 OBJ_ONEMAPPING)) == OBJ_ONEMAPPING ||
3493 object == kernel_object)) {
3494 vm_object_collapse(object);
3497 * The option OBJPR_NOTMAPPED can be passed here
3498 * because vm_map_delete() already performed
3499 * pmap_remove() on the only mapping to this range
3502 vm_object_page_remove(object, offidxstart, offidxend,
3504 if (object->type == OBJT_SWAP)
3505 swap_pager_freespace(object, offidxstart,
3507 if (offidxend >= object->size &&
3508 offidxstart < object->size) {
3509 size1 = object->size;
3510 object->size = offidxstart;
3511 if (object->cred != NULL) {
3512 size1 -= object->size;
3513 KASSERT(object->charge >= ptoa(size1),
3514 ("object %p charge < 0", object));
3515 swap_release_by_cred(ptoa(size1),
3517 object->charge -= ptoa(size1);
3521 VM_OBJECT_WUNLOCK(object);
3523 entry->object.vm_object = NULL;
3524 if (map->system_map)
3525 vm_map_entry_deallocate(entry, TRUE);
3527 entry->next = curthread->td_map_def_user;
3528 curthread->td_map_def_user = entry;
3533 * vm_map_delete: [ internal use only ]
3535 * Deallocates the given address range from the target
3539 vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end)
3541 vm_map_entry_t entry;
3542 vm_map_entry_t first_entry;
3544 VM_MAP_ASSERT_LOCKED(map);
3546 return (KERN_SUCCESS);
3549 * Find the start of the region, and clip it
3551 if (!vm_map_lookup_entry(map, start, &first_entry))
3552 entry = first_entry->next;
3554 entry = first_entry;
3555 vm_map_clip_start(map, entry, start);
3559 * Step through all entries in this region
3561 while (entry->start < end) {
3562 vm_map_entry_t next;
3565 * Wait for wiring or unwiring of an entry to complete.
3566 * Also wait for any system wirings to disappear on
3569 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 ||
3570 (vm_map_pmap(map) != kernel_pmap &&
3571 vm_map_entry_system_wired_count(entry) != 0)) {
3572 unsigned int last_timestamp;
3573 vm_offset_t saved_start;
3574 vm_map_entry_t tmp_entry;
3576 saved_start = entry->start;
3577 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
3578 last_timestamp = map->timestamp;
3579 (void) vm_map_unlock_and_wait(map, 0);
3581 if (last_timestamp + 1 != map->timestamp) {
3583 * Look again for the entry because the map was
3584 * modified while it was unlocked.
3585 * Specifically, the entry may have been
3586 * clipped, merged, or deleted.
3588 if (!vm_map_lookup_entry(map, saved_start,
3590 entry = tmp_entry->next;
3593 vm_map_clip_start(map, entry,
3599 vm_map_clip_end(map, entry, end);
3604 * Unwire before removing addresses from the pmap; otherwise,
3605 * unwiring will put the entries back in the pmap.
3607 if (entry->wired_count != 0)
3608 vm_map_entry_unwire(map, entry);
3611 * Remove mappings for the pages, but only if the
3612 * mappings could exist. For instance, it does not
3613 * make sense to call pmap_remove() for guard entries.
3615 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0 ||
3616 entry->object.vm_object != NULL)
3617 pmap_remove(map->pmap, entry->start, entry->end);
3619 if (entry->end == map->anon_loc)
3620 map->anon_loc = entry->start;
3623 * Delete the entry only after removing all pmap
3624 * entries pointing to its pages. (Otherwise, its
3625 * page frames may be reallocated, and any modify bits
3626 * will be set in the wrong object!)
3628 vm_map_entry_delete(map, entry);
3631 return (KERN_SUCCESS);
3637 * Remove the given address range from the target map.
3638 * This is the exported form of vm_map_delete.
3641 vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
3646 VM_MAP_RANGE_CHECK(map, start, end);
3647 result = vm_map_delete(map, start, end);
3653 * vm_map_check_protection:
3655 * Assert that the target map allows the specified privilege on the
3656 * entire address region given. The entire region must be allocated.
3658 * WARNING! This code does not and should not check whether the
3659 * contents of the region is accessible. For example a smaller file
3660 * might be mapped into a larger address space.
3662 * NOTE! This code is also called by munmap().
3664 * The map must be locked. A read lock is sufficient.
3667 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
3668 vm_prot_t protection)
3670 vm_map_entry_t entry;
3671 vm_map_entry_t tmp_entry;
3673 if (!vm_map_lookup_entry(map, start, &tmp_entry))
3677 while (start < end) {
3681 if (start < entry->start)
3684 * Check protection associated with entry.
3686 if ((entry->protection & protection) != protection)
3688 /* go to next entry */
3690 entry = entry->next;
3696 * vm_map_copy_entry:
3698 * Copies the contents of the source entry to the destination
3699 * entry. The entries *must* be aligned properly.
3705 vm_map_entry_t src_entry,
3706 vm_map_entry_t dst_entry,
3707 vm_ooffset_t *fork_charge)
3709 vm_object_t src_object;
3710 vm_map_entry_t fake_entry;
3715 VM_MAP_ASSERT_LOCKED(dst_map);
3717 if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
3720 if (src_entry->wired_count == 0 ||
3721 (src_entry->protection & VM_PROT_WRITE) == 0) {
3723 * If the source entry is marked needs_copy, it is already
3726 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0 &&
3727 (src_entry->protection & VM_PROT_WRITE) != 0) {
3728 pmap_protect(src_map->pmap,
3731 src_entry->protection & ~VM_PROT_WRITE);
3735 * Make a copy of the object.
3737 size = src_entry->end - src_entry->start;
3738 if ((src_object = src_entry->object.vm_object) != NULL) {
3739 VM_OBJECT_WLOCK(src_object);
3740 charged = ENTRY_CHARGED(src_entry);
3741 if (src_object->handle == NULL &&
3742 (src_object->type == OBJT_DEFAULT ||
3743 src_object->type == OBJT_SWAP)) {
3744 vm_object_collapse(src_object);
3745 if ((src_object->flags & (OBJ_NOSPLIT |
3746 OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
3747 vm_object_split(src_entry);
3749 src_entry->object.vm_object;
3752 vm_object_reference_locked(src_object);
3753 vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
3754 if (src_entry->cred != NULL &&
3755 !(src_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
3756 KASSERT(src_object->cred == NULL,
3757 ("OVERCOMMIT: vm_map_copy_entry: cred %p",
3759 src_object->cred = src_entry->cred;
3760 src_object->charge = size;
3762 VM_OBJECT_WUNLOCK(src_object);
3763 dst_entry->object.vm_object = src_object;
3765 cred = curthread->td_ucred;
3767 dst_entry->cred = cred;
3768 *fork_charge += size;
3769 if (!(src_entry->eflags &
3770 MAP_ENTRY_NEEDS_COPY)) {
3772 src_entry->cred = cred;
3773 *fork_charge += size;
3776 src_entry->eflags |= MAP_ENTRY_COW |
3777 MAP_ENTRY_NEEDS_COPY;
3778 dst_entry->eflags |= MAP_ENTRY_COW |
3779 MAP_ENTRY_NEEDS_COPY;
3780 dst_entry->offset = src_entry->offset;
3781 if (src_entry->eflags & MAP_ENTRY_VN_WRITECNT) {
3783 * MAP_ENTRY_VN_WRITECNT cannot
3784 * indicate write reference from
3785 * src_entry, since the entry is
3786 * marked as needs copy. Allocate a
3787 * fake entry that is used to
3788 * decrement object->un_pager.vnp.writecount
3789 * at the appropriate time. Attach
3790 * fake_entry to the deferred list.
3792 fake_entry = vm_map_entry_create(dst_map);
3793 fake_entry->eflags = MAP_ENTRY_VN_WRITECNT;
3794 src_entry->eflags &= ~MAP_ENTRY_VN_WRITECNT;
3795 vm_object_reference(src_object);
3796 fake_entry->object.vm_object = src_object;
3797 fake_entry->start = src_entry->start;
3798 fake_entry->end = src_entry->end;
3799 fake_entry->next = curthread->td_map_def_user;
3800 curthread->td_map_def_user = fake_entry;
3803 pmap_copy(dst_map->pmap, src_map->pmap,
3804 dst_entry->start, dst_entry->end - dst_entry->start,
3807 dst_entry->object.vm_object = NULL;
3808 dst_entry->offset = 0;
3809 if (src_entry->cred != NULL) {
3810 dst_entry->cred = curthread->td_ucred;
3811 crhold(dst_entry->cred);
3812 *fork_charge += size;
3817 * We don't want to make writeable wired pages copy-on-write.
3818 * Immediately copy these pages into the new map by simulating
3819 * page faults. The new pages are pageable.
3821 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry,
3827 * vmspace_map_entry_forked:
3828 * Update the newly-forked vmspace each time a map entry is inherited
3829 * or copied. The values for vm_dsize and vm_tsize are approximate
3830 * (and mostly-obsolete ideas in the face of mmap(2) et al.)
3833 vmspace_map_entry_forked(const struct vmspace *vm1, struct vmspace *vm2,
3834 vm_map_entry_t entry)
3836 vm_size_t entrysize;
3839 if ((entry->eflags & MAP_ENTRY_GUARD) != 0)
3841 entrysize = entry->end - entry->start;
3842 vm2->vm_map.size += entrysize;
3843 if (entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP)) {
3844 vm2->vm_ssize += btoc(entrysize);
3845 } else if (entry->start >= (vm_offset_t)vm1->vm_daddr &&
3846 entry->start < (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)) {
3847 newend = MIN(entry->end,
3848 (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize));
3849 vm2->vm_dsize += btoc(newend - entry->start);
3850 } else if (entry->start >= (vm_offset_t)vm1->vm_taddr &&
3851 entry->start < (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)) {
3852 newend = MIN(entry->end,
3853 (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize));
3854 vm2->vm_tsize += btoc(newend - entry->start);
3860 * Create a new process vmspace structure and vm_map
3861 * based on those of an existing process. The new map
3862 * is based on the old map, according to the inheritance
3863 * values on the regions in that map.
3865 * XXX It might be worth coalescing the entries added to the new vmspace.
3867 * The source map must not be locked.
3870 vmspace_fork(struct vmspace *vm1, vm_ooffset_t *fork_charge)
3872 struct vmspace *vm2;
3873 vm_map_t new_map, old_map;
3874 vm_map_entry_t new_entry, old_entry;
3879 old_map = &vm1->vm_map;
3880 /* Copy immutable fields of vm1 to vm2. */
3881 vm2 = vmspace_alloc(vm_map_min(old_map), vm_map_max(old_map),
3886 vm2->vm_taddr = vm1->vm_taddr;
3887 vm2->vm_daddr = vm1->vm_daddr;
3888 vm2->vm_maxsaddr = vm1->vm_maxsaddr;
3889 vm_map_lock(old_map);
3891 vm_map_wait_busy(old_map);
3892 new_map = &vm2->vm_map;
3893 locked = vm_map_trylock(new_map); /* trylock to silence WITNESS */
3894 KASSERT(locked, ("vmspace_fork: lock failed"));
3896 error = pmap_vmspace_copy(new_map->pmap, old_map->pmap);
3898 sx_xunlock(&old_map->lock);
3899 sx_xunlock(&new_map->lock);
3900 vm_map_process_deferred();
3905 new_map->anon_loc = old_map->anon_loc;
3907 old_entry = old_map->header.next;
3909 while (old_entry != &old_map->header) {
3910 if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
3911 panic("vm_map_fork: encountered a submap");
3913 inh = old_entry->inheritance;
3914 if ((old_entry->eflags & MAP_ENTRY_GUARD) != 0 &&
3915 inh != VM_INHERIT_NONE)
3916 inh = VM_INHERIT_COPY;
3919 case VM_INHERIT_NONE:
3922 case VM_INHERIT_SHARE:
3924 * Clone the entry, creating the shared object if necessary.
3926 object = old_entry->object.vm_object;
3927 if (object == NULL) {
3928 vm_map_entry_back(old_entry);
3929 object = old_entry->object.vm_object;
3933 * Add the reference before calling vm_object_shadow
3934 * to insure that a shadow object is created.
3936 vm_object_reference(object);
3937 if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3938 vm_object_shadow(&old_entry->object.vm_object,
3940 old_entry->end - old_entry->start);
3941 old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
3942 /* Transfer the second reference too. */
3943 vm_object_reference(
3944 old_entry->object.vm_object);
3947 * As in vm_map_simplify_entry(), the
3948 * vnode lock will not be acquired in
3949 * this call to vm_object_deallocate().
3951 vm_object_deallocate(object);
3952 object = old_entry->object.vm_object;
3954 VM_OBJECT_WLOCK(object);
3955 vm_object_clear_flag(object, OBJ_ONEMAPPING);
3956 if (old_entry->cred != NULL) {
3957 KASSERT(object->cred == NULL, ("vmspace_fork both cred"));
3958 object->cred = old_entry->cred;
3959 object->charge = old_entry->end - old_entry->start;
3960 old_entry->cred = NULL;
3964 * Assert the correct state of the vnode
3965 * v_writecount while the object is locked, to
3966 * not relock it later for the assertion
3969 if (old_entry->eflags & MAP_ENTRY_VN_WRITECNT &&
3970 object->type == OBJT_VNODE) {
3971 KASSERT(((struct vnode *)object->handle)->
3973 ("vmspace_fork: v_writecount %p", object));
3974 KASSERT(object->un_pager.vnp.writemappings > 0,
3975 ("vmspace_fork: vnp.writecount %p",
3978 VM_OBJECT_WUNLOCK(object);
3981 * Clone the entry, referencing the shared object.
3983 new_entry = vm_map_entry_create(new_map);
3984 *new_entry = *old_entry;
3985 new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
3986 MAP_ENTRY_IN_TRANSITION);
3987 new_entry->wiring_thread = NULL;
3988 new_entry->wired_count = 0;
3989 if (new_entry->eflags & MAP_ENTRY_VN_WRITECNT) {
3990 vnode_pager_update_writecount(object,
3991 new_entry->start, new_entry->end);
3993 vm_map_entry_set_vnode_text(new_entry, true);
3996 * Insert the entry into the new map -- we know we're
3997 * inserting at the end of the new map.
3999 vm_map_entry_link(new_map, new_entry);
4000 vmspace_map_entry_forked(vm1, vm2, new_entry);
4003 * Update the physical map
4005 pmap_copy(new_map->pmap, old_map->pmap,
4007 (old_entry->end - old_entry->start),
4011 case VM_INHERIT_COPY:
4013 * Clone the entry and link into the map.
4015 new_entry = vm_map_entry_create(new_map);
4016 *new_entry = *old_entry;
4018 * Copied entry is COW over the old object.
4020 new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
4021 MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_VN_WRITECNT);
4022 new_entry->wiring_thread = NULL;
4023 new_entry->wired_count = 0;
4024 new_entry->object.vm_object = NULL;
4025 new_entry->cred = NULL;
4026 vm_map_entry_link(new_map, new_entry);
4027 vmspace_map_entry_forked(vm1, vm2, new_entry);
4028 vm_map_copy_entry(old_map, new_map, old_entry,
4029 new_entry, fork_charge);
4030 vm_map_entry_set_vnode_text(new_entry, true);
4033 case VM_INHERIT_ZERO:
4035 * Create a new anonymous mapping entry modelled from
4038 new_entry = vm_map_entry_create(new_map);
4039 memset(new_entry, 0, sizeof(*new_entry));
4041 new_entry->start = old_entry->start;
4042 new_entry->end = old_entry->end;
4043 new_entry->eflags = old_entry->eflags &
4044 ~(MAP_ENTRY_USER_WIRED | MAP_ENTRY_IN_TRANSITION |
4045 MAP_ENTRY_VN_WRITECNT | MAP_ENTRY_VN_EXEC);
4046 new_entry->protection = old_entry->protection;
4047 new_entry->max_protection = old_entry->max_protection;
4048 new_entry->inheritance = VM_INHERIT_ZERO;
4050 vm_map_entry_link(new_map, new_entry);
4051 vmspace_map_entry_forked(vm1, vm2, new_entry);
4053 new_entry->cred = curthread->td_ucred;
4054 crhold(new_entry->cred);
4055 *fork_charge += (new_entry->end - new_entry->start);
4059 old_entry = old_entry->next;
4062 * Use inlined vm_map_unlock() to postpone handling the deferred
4063 * map entries, which cannot be done until both old_map and
4064 * new_map locks are released.
4066 sx_xunlock(&old_map->lock);
4067 sx_xunlock(&new_map->lock);
4068 vm_map_process_deferred();
4074 * Create a process's stack for exec_new_vmspace(). This function is never
4075 * asked to wire the newly created stack.
4078 vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
4079 vm_prot_t prot, vm_prot_t max, int cow)
4081 vm_size_t growsize, init_ssize;
4085 MPASS((map->flags & MAP_WIREFUTURE) == 0);
4086 growsize = sgrowsiz;
4087 init_ssize = (max_ssize < growsize) ? max_ssize : growsize;
4089 vmemlim = lim_cur(curthread, RLIMIT_VMEM);
4090 /* If we would blow our VMEM resource limit, no go */
4091 if (map->size + init_ssize > vmemlim) {
4095 rv = vm_map_stack_locked(map, addrbos, max_ssize, growsize, prot,
4102 static int stack_guard_page = 1;
4103 SYSCTL_INT(_security_bsd, OID_AUTO, stack_guard_page, CTLFLAG_RWTUN,
4104 &stack_guard_page, 0,
4105 "Specifies the number of guard pages for a stack that grows");
4108 vm_map_stack_locked(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
4109 vm_size_t growsize, vm_prot_t prot, vm_prot_t max, int cow)
4111 vm_map_entry_t new_entry, prev_entry;
4112 vm_offset_t bot, gap_bot, gap_top, top;
4113 vm_size_t init_ssize, sgp;
4117 * The stack orientation is piggybacked with the cow argument.
4118 * Extract it into orient and mask the cow argument so that we
4119 * don't pass it around further.
4121 orient = cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP);
4122 KASSERT(orient != 0, ("No stack grow direction"));
4123 KASSERT(orient != (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP),
4126 if (addrbos < vm_map_min(map) ||
4127 addrbos + max_ssize > vm_map_max(map) ||
4128 addrbos + max_ssize <= addrbos)
4129 return (KERN_INVALID_ADDRESS);
4130 sgp = (vm_size_t)stack_guard_page * PAGE_SIZE;
4131 if (sgp >= max_ssize)
4132 return (KERN_INVALID_ARGUMENT);
4134 init_ssize = growsize;
4135 if (max_ssize < init_ssize + sgp)
4136 init_ssize = max_ssize - sgp;
4138 /* If addr is already mapped, no go */
4139 if (vm_map_lookup_entry(map, addrbos, &prev_entry))
4140 return (KERN_NO_SPACE);
4143 * If we can't accommodate max_ssize in the current mapping, no go.
4145 if (prev_entry->next->start < addrbos + max_ssize)
4146 return (KERN_NO_SPACE);
4149 * We initially map a stack of only init_ssize. We will grow as
4150 * needed later. Depending on the orientation of the stack (i.e.
4151 * the grow direction) we either map at the top of the range, the
4152 * bottom of the range or in the middle.
4154 * Note: we would normally expect prot and max to be VM_PROT_ALL,
4155 * and cow to be 0. Possibly we should eliminate these as input
4156 * parameters, and just pass these values here in the insert call.
4158 if (orient == MAP_STACK_GROWS_DOWN) {
4159 bot = addrbos + max_ssize - init_ssize;
4160 top = bot + init_ssize;
4163 } else /* if (orient == MAP_STACK_GROWS_UP) */ {
4165 top = bot + init_ssize;
4167 gap_top = addrbos + max_ssize;
4169 rv = vm_map_insert(map, NULL, 0, bot, top, prot, max, cow);
4170 if (rv != KERN_SUCCESS)
4172 new_entry = prev_entry->next;
4173 KASSERT(new_entry->end == top || new_entry->start == bot,
4174 ("Bad entry start/end for new stack entry"));
4175 KASSERT((orient & MAP_STACK_GROWS_DOWN) == 0 ||
4176 (new_entry->eflags & MAP_ENTRY_GROWS_DOWN) != 0,
4177 ("new entry lacks MAP_ENTRY_GROWS_DOWN"));
4178 KASSERT((orient & MAP_STACK_GROWS_UP) == 0 ||
4179 (new_entry->eflags & MAP_ENTRY_GROWS_UP) != 0,
4180 ("new entry lacks MAP_ENTRY_GROWS_UP"));
4181 rv = vm_map_insert(map, NULL, 0, gap_bot, gap_top, VM_PROT_NONE,
4182 VM_PROT_NONE, MAP_CREATE_GUARD | (orient == MAP_STACK_GROWS_DOWN ?
4183 MAP_CREATE_STACK_GAP_DN : MAP_CREATE_STACK_GAP_UP));
4184 if (rv != KERN_SUCCESS)
4185 (void)vm_map_delete(map, bot, top);
4190 * Attempts to grow a vm stack entry. Returns KERN_SUCCESS if we
4191 * successfully grow the stack.
4194 vm_map_growstack(vm_map_t map, vm_offset_t addr, vm_map_entry_t gap_entry)
4196 vm_map_entry_t stack_entry;
4200 vm_offset_t gap_end, gap_start, grow_start;
4201 vm_size_t grow_amount, guard, max_grow;
4202 rlim_t lmemlim, stacklim, vmemlim;
4204 bool gap_deleted, grow_down, is_procstack;
4216 * Disallow stack growth when the access is performed by a
4217 * debugger or AIO daemon. The reason is that the wrong
4218 * resource limits are applied.
4220 if (p != initproc && (map != &p->p_vmspace->vm_map ||
4221 p->p_textvp == NULL))
4222 return (KERN_FAILURE);
4224 MPASS(!map->system_map);
4226 guard = stack_guard_page * PAGE_SIZE;
4227 lmemlim = lim_cur(curthread, RLIMIT_MEMLOCK);
4228 stacklim = lim_cur(curthread, RLIMIT_STACK);
4229 vmemlim = lim_cur(curthread, RLIMIT_VMEM);
4231 /* If addr is not in a hole for a stack grow area, no need to grow. */
4232 if (gap_entry == NULL && !vm_map_lookup_entry(map, addr, &gap_entry))
4233 return (KERN_FAILURE);
4234 if ((gap_entry->eflags & MAP_ENTRY_GUARD) == 0)
4235 return (KERN_SUCCESS);
4236 if ((gap_entry->eflags & MAP_ENTRY_STACK_GAP_DN) != 0) {
4237 stack_entry = gap_entry->next;
4238 if ((stack_entry->eflags & MAP_ENTRY_GROWS_DOWN) == 0 ||
4239 stack_entry->start != gap_entry->end)
4240 return (KERN_FAILURE);
4241 grow_amount = round_page(stack_entry->start - addr);
4243 } else if ((gap_entry->eflags & MAP_ENTRY_STACK_GAP_UP) != 0) {
4244 stack_entry = gap_entry->prev;
4245 if ((stack_entry->eflags & MAP_ENTRY_GROWS_UP) == 0 ||
4246 stack_entry->end != gap_entry->start)
4247 return (KERN_FAILURE);
4248 grow_amount = round_page(addr + 1 - stack_entry->end);
4251 return (KERN_FAILURE);
4253 max_grow = gap_entry->end - gap_entry->start;
4254 if (guard > max_grow)
4255 return (KERN_NO_SPACE);
4257 if (grow_amount > max_grow)
4258 return (KERN_NO_SPACE);
4261 * If this is the main process stack, see if we're over the stack
4264 is_procstack = addr >= (vm_offset_t)vm->vm_maxsaddr &&
4265 addr < (vm_offset_t)p->p_sysent->sv_usrstack;
4266 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim))
4267 return (KERN_NO_SPACE);
4272 if (is_procstack && racct_set(p, RACCT_STACK,
4273 ctob(vm->vm_ssize) + grow_amount)) {
4275 return (KERN_NO_SPACE);
4281 grow_amount = roundup(grow_amount, sgrowsiz);
4282 if (grow_amount > max_grow)
4283 grow_amount = max_grow;
4284 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
4285 grow_amount = trunc_page((vm_size_t)stacklim) -
4291 limit = racct_get_available(p, RACCT_STACK);
4293 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > limit))
4294 grow_amount = limit - ctob(vm->vm_ssize);
4297 if (!old_mlock && (map->flags & MAP_WIREFUTURE) != 0) {
4298 if (ptoa(pmap_wired_count(map->pmap)) + grow_amount > lmemlim) {
4305 if (racct_set(p, RACCT_MEMLOCK,
4306 ptoa(pmap_wired_count(map->pmap)) + grow_amount)) {
4316 /* If we would blow our VMEM resource limit, no go */
4317 if (map->size + grow_amount > vmemlim) {
4324 if (racct_set(p, RACCT_VMEM, map->size + grow_amount)) {
4333 if (vm_map_lock_upgrade(map)) {
4335 vm_map_lock_read(map);
4340 grow_start = gap_entry->end - grow_amount;
4341 if (gap_entry->start + grow_amount == gap_entry->end) {
4342 gap_start = gap_entry->start;
4343 gap_end = gap_entry->end;
4344 vm_map_entry_delete(map, gap_entry);
4347 MPASS(gap_entry->start < gap_entry->end - grow_amount);
4348 vm_map_entry_resize(map, gap_entry, -grow_amount);
4349 gap_deleted = false;
4351 rv = vm_map_insert(map, NULL, 0, grow_start,
4352 grow_start + grow_amount,
4353 stack_entry->protection, stack_entry->max_protection,
4354 MAP_STACK_GROWS_DOWN);
4355 if (rv != KERN_SUCCESS) {
4357 rv1 = vm_map_insert(map, NULL, 0, gap_start,
4358 gap_end, VM_PROT_NONE, VM_PROT_NONE,
4359 MAP_CREATE_GUARD | MAP_CREATE_STACK_GAP_DN);
4360 MPASS(rv1 == KERN_SUCCESS);
4362 vm_map_entry_resize(map, gap_entry,
4366 grow_start = stack_entry->end;
4367 cred = stack_entry->cred;
4368 if (cred == NULL && stack_entry->object.vm_object != NULL)
4369 cred = stack_entry->object.vm_object->cred;
4370 if (cred != NULL && !swap_reserve_by_cred(grow_amount, cred))
4372 /* Grow the underlying object if applicable. */
4373 else if (stack_entry->object.vm_object == NULL ||
4374 vm_object_coalesce(stack_entry->object.vm_object,
4375 stack_entry->offset,
4376 (vm_size_t)(stack_entry->end - stack_entry->start),
4377 grow_amount, cred != NULL)) {
4378 if (gap_entry->start + grow_amount == gap_entry->end) {
4379 vm_map_entry_delete(map, gap_entry);
4380 vm_map_entry_resize(map, stack_entry,
4383 gap_entry->start += grow_amount;
4384 stack_entry->end += grow_amount;
4386 map->size += grow_amount;
4391 if (rv == KERN_SUCCESS && is_procstack)
4392 vm->vm_ssize += btoc(grow_amount);
4395 * Heed the MAP_WIREFUTURE flag if it was set for this process.
4397 if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE) != 0) {
4398 rv = vm_map_wire_locked(map, grow_start,
4399 grow_start + grow_amount,
4400 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
4402 vm_map_lock_downgrade(map);
4406 if (racct_enable && rv != KERN_SUCCESS) {
4408 error = racct_set(p, RACCT_VMEM, map->size);
4409 KASSERT(error == 0, ("decreasing RACCT_VMEM failed"));
4411 error = racct_set(p, RACCT_MEMLOCK,
4412 ptoa(pmap_wired_count(map->pmap)));
4413 KASSERT(error == 0, ("decreasing RACCT_MEMLOCK failed"));
4415 error = racct_set(p, RACCT_STACK, ctob(vm->vm_ssize));
4416 KASSERT(error == 0, ("decreasing RACCT_STACK failed"));
4425 * Unshare the specified VM space for exec. If other processes are
4426 * mapped to it, then create a new one. The new vmspace is null.
4429 vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser)
4431 struct vmspace *oldvmspace = p->p_vmspace;
4432 struct vmspace *newvmspace;
4434 KASSERT((curthread->td_pflags & TDP_EXECVMSPC) == 0,
4435 ("vmspace_exec recursed"));
4436 newvmspace = vmspace_alloc(minuser, maxuser, pmap_pinit);
4437 if (newvmspace == NULL)
4439 newvmspace->vm_swrss = oldvmspace->vm_swrss;
4441 * This code is written like this for prototype purposes. The
4442 * goal is to avoid running down the vmspace here, but let the
4443 * other process's that are still using the vmspace to finally
4444 * run it down. Even though there is little or no chance of blocking
4445 * here, it is a good idea to keep this form for future mods.
4447 PROC_VMSPACE_LOCK(p);
4448 p->p_vmspace = newvmspace;
4449 PROC_VMSPACE_UNLOCK(p);
4450 if (p == curthread->td_proc)
4451 pmap_activate(curthread);
4452 curthread->td_pflags |= TDP_EXECVMSPC;
4457 * Unshare the specified VM space for forcing COW. This
4458 * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
4461 vmspace_unshare(struct proc *p)
4463 struct vmspace *oldvmspace = p->p_vmspace;
4464 struct vmspace *newvmspace;
4465 vm_ooffset_t fork_charge;
4467 if (oldvmspace->vm_refcnt == 1)
4470 newvmspace = vmspace_fork(oldvmspace, &fork_charge);
4471 if (newvmspace == NULL)
4473 if (!swap_reserve_by_cred(fork_charge, p->p_ucred)) {
4474 vmspace_free(newvmspace);
4477 PROC_VMSPACE_LOCK(p);
4478 p->p_vmspace = newvmspace;
4479 PROC_VMSPACE_UNLOCK(p);
4480 if (p == curthread->td_proc)
4481 pmap_activate(curthread);
4482 vmspace_free(oldvmspace);
4489 * Finds the VM object, offset, and
4490 * protection for a given virtual address in the
4491 * specified map, assuming a page fault of the
4494 * Leaves the map in question locked for read; return
4495 * values are guaranteed until a vm_map_lookup_done
4496 * call is performed. Note that the map argument
4497 * is in/out; the returned map must be used in
4498 * the call to vm_map_lookup_done.
4500 * A handle (out_entry) is returned for use in
4501 * vm_map_lookup_done, to make that fast.
4503 * If a lookup is requested with "write protection"
4504 * specified, the map may be changed to perform virtual
4505 * copying operations, although the data referenced will
4509 vm_map_lookup(vm_map_t *var_map, /* IN/OUT */
4511 vm_prot_t fault_typea,
4512 vm_map_entry_t *out_entry, /* OUT */
4513 vm_object_t *object, /* OUT */
4514 vm_pindex_t *pindex, /* OUT */
4515 vm_prot_t *out_prot, /* OUT */
4516 boolean_t *wired) /* OUT */
4518 vm_map_entry_t entry;
4519 vm_map_t map = *var_map;
4521 vm_prot_t fault_type = fault_typea;
4522 vm_object_t eobject;
4528 vm_map_lock_read(map);
4532 * Lookup the faulting address.
4534 if (!vm_map_lookup_entry(map, vaddr, out_entry)) {
4535 vm_map_unlock_read(map);
4536 return (KERN_INVALID_ADDRESS);
4544 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
4545 vm_map_t old_map = map;
4547 *var_map = map = entry->object.sub_map;
4548 vm_map_unlock_read(old_map);
4553 * Check whether this task is allowed to have this page.
4555 prot = entry->protection;
4556 if ((fault_typea & VM_PROT_FAULT_LOOKUP) != 0) {
4557 fault_typea &= ~VM_PROT_FAULT_LOOKUP;
4558 if (prot == VM_PROT_NONE && map != kernel_map &&
4559 (entry->eflags & MAP_ENTRY_GUARD) != 0 &&
4560 (entry->eflags & (MAP_ENTRY_STACK_GAP_DN |
4561 MAP_ENTRY_STACK_GAP_UP)) != 0 &&
4562 vm_map_growstack(map, vaddr, entry) == KERN_SUCCESS)
4563 goto RetryLookupLocked;
4565 fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
4566 if ((fault_type & prot) != fault_type || prot == VM_PROT_NONE) {
4567 vm_map_unlock_read(map);
4568 return (KERN_PROTECTION_FAILURE);
4570 KASSERT((prot & VM_PROT_WRITE) == 0 || (entry->eflags &
4571 (MAP_ENTRY_USER_WIRED | MAP_ENTRY_NEEDS_COPY)) !=
4572 (MAP_ENTRY_USER_WIRED | MAP_ENTRY_NEEDS_COPY),
4573 ("entry %p flags %x", entry, entry->eflags));
4574 if ((fault_typea & VM_PROT_COPY) != 0 &&
4575 (entry->max_protection & VM_PROT_WRITE) == 0 &&
4576 (entry->eflags & MAP_ENTRY_COW) == 0) {
4577 vm_map_unlock_read(map);
4578 return (KERN_PROTECTION_FAILURE);
4582 * If this page is not pageable, we have to get it for all possible
4585 *wired = (entry->wired_count != 0);
4587 fault_type = entry->protection;
4588 size = entry->end - entry->start;
4590 * If the entry was copy-on-write, we either ...
4592 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
4594 * If we want to write the page, we may as well handle that
4595 * now since we've got the map locked.
4597 * If we don't need to write the page, we just demote the
4598 * permissions allowed.
4600 if ((fault_type & VM_PROT_WRITE) != 0 ||
4601 (fault_typea & VM_PROT_COPY) != 0) {
4603 * Make a new object, and place it in the object
4604 * chain. Note that no new references have appeared
4605 * -- one just moved from the map to the new
4608 if (vm_map_lock_upgrade(map))
4611 if (entry->cred == NULL) {
4613 * The debugger owner is charged for
4616 cred = curthread->td_ucred;
4618 if (!swap_reserve_by_cred(size, cred)) {
4621 return (KERN_RESOURCE_SHORTAGE);
4625 vm_object_shadow(&entry->object.vm_object,
4626 &entry->offset, size);
4627 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
4628 eobject = entry->object.vm_object;
4629 if (eobject->cred != NULL) {
4631 * The object was not shadowed.
4633 swap_release_by_cred(size, entry->cred);
4634 crfree(entry->cred);
4636 } else if (entry->cred != NULL) {
4637 VM_OBJECT_WLOCK(eobject);
4638 eobject->cred = entry->cred;
4639 eobject->charge = size;
4640 VM_OBJECT_WUNLOCK(eobject);
4644 vm_map_lock_downgrade(map);
4647 * We're attempting to read a copy-on-write page --
4648 * don't allow writes.
4650 prot &= ~VM_PROT_WRITE;
4655 * Create an object if necessary.
4657 if (entry->object.vm_object == NULL &&
4659 if (vm_map_lock_upgrade(map))
4661 entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
4664 if (entry->cred != NULL) {
4665 VM_OBJECT_WLOCK(entry->object.vm_object);
4666 entry->object.vm_object->cred = entry->cred;
4667 entry->object.vm_object->charge = size;
4668 VM_OBJECT_WUNLOCK(entry->object.vm_object);
4671 vm_map_lock_downgrade(map);
4675 * Return the object/offset from this entry. If the entry was
4676 * copy-on-write or empty, it has been fixed up.
4678 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
4679 *object = entry->object.vm_object;
4682 return (KERN_SUCCESS);
4686 * vm_map_lookup_locked:
4688 * Lookup the faulting address. A version of vm_map_lookup that returns
4689 * KERN_FAILURE instead of blocking on map lock or memory allocation.
4692 vm_map_lookup_locked(vm_map_t *var_map, /* IN/OUT */
4694 vm_prot_t fault_typea,
4695 vm_map_entry_t *out_entry, /* OUT */
4696 vm_object_t *object, /* OUT */
4697 vm_pindex_t *pindex, /* OUT */
4698 vm_prot_t *out_prot, /* OUT */
4699 boolean_t *wired) /* OUT */
4701 vm_map_entry_t entry;
4702 vm_map_t map = *var_map;
4704 vm_prot_t fault_type = fault_typea;
4707 * Lookup the faulting address.
4709 if (!vm_map_lookup_entry(map, vaddr, out_entry))
4710 return (KERN_INVALID_ADDRESS);
4715 * Fail if the entry refers to a submap.
4717 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
4718 return (KERN_FAILURE);
4721 * Check whether this task is allowed to have this page.
4723 prot = entry->protection;
4724 fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
4725 if ((fault_type & prot) != fault_type)
4726 return (KERN_PROTECTION_FAILURE);
4729 * If this page is not pageable, we have to get it for all possible
4732 *wired = (entry->wired_count != 0);
4734 fault_type = entry->protection;
4736 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
4738 * Fail if the entry was copy-on-write for a write fault.
4740 if (fault_type & VM_PROT_WRITE)
4741 return (KERN_FAILURE);
4743 * We're attempting to read a copy-on-write page --
4744 * don't allow writes.
4746 prot &= ~VM_PROT_WRITE;
4750 * Fail if an object should be created.
4752 if (entry->object.vm_object == NULL && !map->system_map)
4753 return (KERN_FAILURE);
4756 * Return the object/offset from this entry. If the entry was
4757 * copy-on-write or empty, it has been fixed up.
4759 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
4760 *object = entry->object.vm_object;
4763 return (KERN_SUCCESS);
4767 * vm_map_lookup_done:
4769 * Releases locks acquired by a vm_map_lookup
4770 * (according to the handle returned by that lookup).
4773 vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry)
4776 * Unlock the main-level map
4778 vm_map_unlock_read(map);
4782 vm_map_max_KBI(const struct vm_map *map)
4785 return (vm_map_max(map));
4789 vm_map_min_KBI(const struct vm_map *map)
4792 return (vm_map_min(map));
4796 vm_map_pmap_KBI(vm_map_t map)
4802 #include "opt_ddb.h"
4804 #include <sys/kernel.h>
4806 #include <ddb/ddb.h>
4809 vm_map_print(vm_map_t map)
4811 vm_map_entry_t entry, prev;
4813 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
4815 (void *)map->pmap, map->nentries, map->timestamp);
4818 for (prev = &map->header; (entry = prev->next) != &map->header;
4820 db_iprintf("map entry %p: start=%p, end=%p, eflags=%#x, \n",
4821 (void *)entry, (void *)entry->start, (void *)entry->end,
4824 static char *inheritance_name[4] =
4825 {"share", "copy", "none", "donate_copy"};
4827 db_iprintf(" prot=%x/%x/%s",
4829 entry->max_protection,
4830 inheritance_name[(int)(unsigned char)
4831 entry->inheritance]);
4832 if (entry->wired_count != 0)
4833 db_printf(", wired");
4835 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
4836 db_printf(", share=%p, offset=0x%jx\n",
4837 (void *)entry->object.sub_map,
4838 (uintmax_t)entry->offset);
4839 if (prev == &map->header ||
4840 prev->object.sub_map !=
4841 entry->object.sub_map) {
4843 vm_map_print((vm_map_t)entry->object.sub_map);
4847 if (entry->cred != NULL)
4848 db_printf(", ruid %d", entry->cred->cr_ruid);
4849 db_printf(", object=%p, offset=0x%jx",
4850 (void *)entry->object.vm_object,
4851 (uintmax_t)entry->offset);
4852 if (entry->object.vm_object && entry->object.vm_object->cred)
4853 db_printf(", obj ruid %d charge %jx",
4854 entry->object.vm_object->cred->cr_ruid,
4855 (uintmax_t)entry->object.vm_object->charge);
4856 if (entry->eflags & MAP_ENTRY_COW)
4857 db_printf(", copy (%s)",
4858 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
4861 if (prev == &map->header ||
4862 prev->object.vm_object !=
4863 entry->object.vm_object) {
4865 vm_object_print((db_expr_t)(intptr_t)
4866 entry->object.vm_object,
4875 DB_SHOW_COMMAND(map, map)
4879 db_printf("usage: show map <addr>\n");
4882 vm_map_print((vm_map_t)addr);
4885 DB_SHOW_COMMAND(procvm, procvm)
4890 p = db_lookup_proc(addr);
4895 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
4896 (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
4897 (void *)vmspace_pmap(p->p_vmspace));
4899 vm_map_print((vm_map_t)&p->p_vmspace->vm_map);