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>
73 #include <sys/kernel.h>
76 #include <sys/mutex.h>
78 #include <sys/vmmeter.h>
80 #include <sys/vnode.h>
81 #include <sys/racct.h>
82 #include <sys/resourcevar.h>
83 #include <sys/rwlock.h>
85 #include <sys/sysctl.h>
86 #include <sys/sysent.h>
90 #include <vm/vm_param.h>
92 #include <vm/vm_map.h>
93 #include <vm/vm_page.h>
94 #include <vm/vm_pageout.h>
95 #include <vm/vm_object.h>
96 #include <vm/vm_pager.h>
97 #include <vm/vm_kern.h>
98 #include <vm/vm_extern.h>
99 #include <vm/vnode_pager.h>
100 #include <vm/swap_pager.h>
104 * Virtual memory maps provide for the mapping, protection,
105 * and sharing of virtual memory objects. In addition,
106 * this module provides for an efficient virtual copy of
107 * memory from one map to another.
109 * Synchronization is required prior to most operations.
111 * Maps consist of an ordered doubly-linked list of simple
112 * entries; a self-adjusting binary search tree of these
113 * entries is used to speed up lookups.
115 * Since portions of maps are specified by start/end addresses,
116 * which may not align with existing map entries, all
117 * routines merely "clip" entries to these start/end values.
118 * [That is, an entry is split into two, bordering at a
119 * start or end value.] Note that these clippings may not
120 * always be necessary (as the two resulting entries are then
121 * not changed); however, the clipping is done for convenience.
123 * As mentioned above, virtual copy operations are performed
124 * by copying VM object references from one map to
125 * another, and then marking both regions as copy-on-write.
128 static struct mtx map_sleep_mtx;
129 static uma_zone_t mapentzone;
130 static uma_zone_t kmapentzone;
131 static uma_zone_t mapzone;
132 static uma_zone_t vmspace_zone;
133 static int vmspace_zinit(void *mem, int size, int flags);
134 static int vm_map_zinit(void *mem, int ize, int flags);
135 static void _vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min,
137 static void vm_map_entry_deallocate(vm_map_entry_t entry, boolean_t system_map);
138 static void vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry);
139 static void vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry);
140 static int vm_map_growstack(vm_map_t map, vm_offset_t addr,
141 vm_map_entry_t gap_entry);
142 static void vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
143 vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags);
145 static void vm_map_zdtor(void *mem, int size, void *arg);
146 static void vmspace_zdtor(void *mem, int size, void *arg);
148 static int vm_map_stack_locked(vm_map_t map, vm_offset_t addrbos,
149 vm_size_t max_ssize, vm_size_t growsize, vm_prot_t prot, vm_prot_t max,
151 static void vm_map_wire_entry_failure(vm_map_t map, vm_map_entry_t entry,
152 vm_offset_t failed_addr);
154 #define ENTRY_CHARGED(e) ((e)->cred != NULL || \
155 ((e)->object.vm_object != NULL && (e)->object.vm_object->cred != NULL && \
156 !((e)->eflags & MAP_ENTRY_NEEDS_COPY)))
159 * PROC_VMSPACE_{UN,}LOCK() can be a noop as long as vmspaces are type
162 #define PROC_VMSPACE_LOCK(p) do { } while (0)
163 #define PROC_VMSPACE_UNLOCK(p) do { } while (0)
166 * VM_MAP_RANGE_CHECK: [ internal use only ]
168 * Asserts that the starting and ending region
169 * addresses fall within the valid range of the map.
171 #define VM_MAP_RANGE_CHECK(map, start, end) \
173 if (start < vm_map_min(map)) \
174 start = vm_map_min(map); \
175 if (end > vm_map_max(map)) \
176 end = vm_map_max(map); \
184 * Initialize the vm_map module. Must be called before
185 * any other vm_map routines.
187 * Map and entry structures are allocated from the general
188 * purpose memory pool with some exceptions:
190 * - The kernel map and kmem submap are allocated statically.
191 * - Kernel map entries are allocated out of a static pool.
193 * These restrictions are necessary since malloc() uses the
194 * maps and requires map entries.
200 mtx_init(&map_sleep_mtx, "vm map sleep mutex", NULL, MTX_DEF);
201 mapzone = uma_zcreate("MAP", sizeof(struct vm_map), NULL,
207 vm_map_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
208 uma_prealloc(mapzone, MAX_KMAP);
209 kmapentzone = uma_zcreate("KMAP ENTRY", sizeof(struct vm_map_entry),
210 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
211 UMA_ZONE_MTXCLASS | UMA_ZONE_VM);
212 mapentzone = uma_zcreate("MAP ENTRY", sizeof(struct vm_map_entry),
213 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
214 vmspace_zone = uma_zcreate("VMSPACE", sizeof(struct vmspace), NULL,
220 vmspace_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
224 vmspace_zinit(void *mem, int size, int flags)
228 vm = (struct vmspace *)mem;
230 vm->vm_map.pmap = NULL;
231 (void)vm_map_zinit(&vm->vm_map, sizeof(vm->vm_map), flags);
232 PMAP_LOCK_INIT(vmspace_pmap(vm));
237 vm_map_zinit(void *mem, int size, int flags)
242 memset(map, 0, sizeof(*map));
243 mtx_init(&map->system_mtx, "vm map (system)", NULL, MTX_DEF | MTX_DUPOK);
244 sx_init(&map->lock, "vm map (user)");
250 vmspace_zdtor(void *mem, int size, void *arg)
254 vm = (struct vmspace *)mem;
256 vm_map_zdtor(&vm->vm_map, sizeof(vm->vm_map), arg);
259 vm_map_zdtor(void *mem, int size, void *arg)
264 KASSERT(map->nentries == 0,
265 ("map %p nentries == %d on free.",
266 map, map->nentries));
267 KASSERT(map->size == 0,
268 ("map %p size == %lu on free.",
269 map, (unsigned long)map->size));
271 #endif /* INVARIANTS */
274 * Allocate a vmspace structure, including a vm_map and pmap,
275 * and initialize those structures. The refcnt is set to 1.
277 * If 'pinit' is NULL then the embedded pmap is initialized via pmap_pinit().
280 vmspace_alloc(vm_offset_t min, vm_offset_t max, pmap_pinit_t pinit)
284 vm = uma_zalloc(vmspace_zone, M_WAITOK);
285 KASSERT(vm->vm_map.pmap == NULL, ("vm_map.pmap must be NULL"));
286 if (!pinit(vmspace_pmap(vm))) {
287 uma_zfree(vmspace_zone, vm);
290 CTR1(KTR_VM, "vmspace_alloc: %p", vm);
291 _vm_map_init(&vm->vm_map, vmspace_pmap(vm), min, max);
306 vmspace_container_reset(struct proc *p)
310 racct_set(p, RACCT_DATA, 0);
311 racct_set(p, RACCT_STACK, 0);
312 racct_set(p, RACCT_RSS, 0);
313 racct_set(p, RACCT_MEMLOCK, 0);
314 racct_set(p, RACCT_VMEM, 0);
320 vmspace_dofree(struct vmspace *vm)
323 CTR1(KTR_VM, "vmspace_free: %p", vm);
326 * Make sure any SysV shm is freed, it might not have been in
332 * Lock the map, to wait out all other references to it.
333 * Delete all of the mappings and pages they hold, then call
334 * the pmap module to reclaim anything left.
336 (void)vm_map_remove(&vm->vm_map, vm_map_min(&vm->vm_map),
337 vm_map_max(&vm->vm_map));
339 pmap_release(vmspace_pmap(vm));
340 vm->vm_map.pmap = NULL;
341 uma_zfree(vmspace_zone, vm);
345 vmspace_free(struct vmspace *vm)
348 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
349 "vmspace_free() called");
351 if (vm->vm_refcnt == 0)
352 panic("vmspace_free: attempt to free already freed vmspace");
354 if (atomic_fetchadd_int(&vm->vm_refcnt, -1) == 1)
359 vmspace_exitfree(struct proc *p)
363 PROC_VMSPACE_LOCK(p);
366 PROC_VMSPACE_UNLOCK(p);
367 KASSERT(vm == &vmspace0, ("vmspace_exitfree: wrong vmspace"));
372 vmspace_exit(struct thread *td)
379 * Release user portion of address space.
380 * This releases references to vnodes,
381 * which could cause I/O if the file has been unlinked.
382 * Need to do this early enough that we can still sleep.
384 * The last exiting process to reach this point releases as
385 * much of the environment as it can. vmspace_dofree() is the
386 * slower fallback in case another process had a temporary
387 * reference to the vmspace.
392 atomic_add_int(&vmspace0.vm_refcnt, 1);
394 refcnt = vm->vm_refcnt;
395 if (refcnt > 1 && p->p_vmspace != &vmspace0) {
396 /* Switch now since other proc might free vmspace */
397 PROC_VMSPACE_LOCK(p);
398 p->p_vmspace = &vmspace0;
399 PROC_VMSPACE_UNLOCK(p);
402 } while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1));
404 if (p->p_vmspace != vm) {
405 /* vmspace not yet freed, switch back */
406 PROC_VMSPACE_LOCK(p);
408 PROC_VMSPACE_UNLOCK(p);
411 pmap_remove_pages(vmspace_pmap(vm));
412 /* Switch now since this proc will free vmspace */
413 PROC_VMSPACE_LOCK(p);
414 p->p_vmspace = &vmspace0;
415 PROC_VMSPACE_UNLOCK(p);
421 vmspace_container_reset(p);
425 /* Acquire reference to vmspace owned by another process. */
428 vmspace_acquire_ref(struct proc *p)
433 PROC_VMSPACE_LOCK(p);
436 PROC_VMSPACE_UNLOCK(p);
440 refcnt = vm->vm_refcnt;
441 if (refcnt <= 0) { /* Avoid 0->1 transition */
442 PROC_VMSPACE_UNLOCK(p);
445 } while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt + 1));
446 if (vm != p->p_vmspace) {
447 PROC_VMSPACE_UNLOCK(p);
451 PROC_VMSPACE_UNLOCK(p);
456 * Switch between vmspaces in an AIO kernel process.
458 * The new vmspace is either the vmspace of a user process obtained
459 * from an active AIO request or the initial vmspace of the AIO kernel
460 * process (when it is idling). Because user processes will block to
461 * drain any active AIO requests before proceeding in exit() or
462 * execve(), the reference count for vmspaces from AIO requests can
463 * never be 0. Similarly, AIO kernel processes hold an extra
464 * reference on their initial vmspace for the life of the process. As
465 * a result, the 'newvm' vmspace always has a non-zero reference
466 * count. This permits an additional reference on 'newvm' to be
467 * acquired via a simple atomic increment rather than the loop in
468 * vmspace_acquire_ref() above.
471 vmspace_switch_aio(struct vmspace *newvm)
473 struct vmspace *oldvm;
475 /* XXX: Need some way to assert that this is an aio daemon. */
477 KASSERT(newvm->vm_refcnt > 0,
478 ("vmspace_switch_aio: newvm unreferenced"));
480 oldvm = curproc->p_vmspace;
485 * Point to the new address space and refer to it.
487 curproc->p_vmspace = newvm;
488 atomic_add_int(&newvm->vm_refcnt, 1);
490 /* Activate the new mapping. */
491 pmap_activate(curthread);
497 _vm_map_lock(vm_map_t map, const char *file, int line)
501 mtx_lock_flags_(&map->system_mtx, 0, file, line);
503 sx_xlock_(&map->lock, file, line);
508 vm_map_entry_set_vnode_text(vm_map_entry_t entry, bool add)
510 vm_object_t object, object1;
513 if ((entry->eflags & MAP_ENTRY_VN_EXEC) == 0)
515 KASSERT((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0,
516 ("Submap with execs"));
517 object = entry->object.vm_object;
518 KASSERT(object != NULL, ("No object for text, entry %p", entry));
519 VM_OBJECT_RLOCK(object);
520 while ((object1 = object->backing_object) != NULL) {
521 VM_OBJECT_RLOCK(object1);
522 VM_OBJECT_RUNLOCK(object);
527 if (object->type == OBJT_DEAD) {
529 * For OBJT_DEAD objects, v_writecount was handled in
530 * vnode_pager_dealloc().
532 } else if (object->type == OBJT_VNODE) {
534 } else if (object->type == OBJT_SWAP) {
535 KASSERT((object->flags & OBJ_TMPFS_NODE) != 0,
536 ("vm_map_entry_set_vnode_text: swap and !TMPFS "
537 "entry %p, object %p, add %d", entry, object, add));
539 * Tmpfs VREG node, which was reclaimed, has
540 * OBJ_TMPFS_NODE flag set, but not OBJ_TMPFS. In
541 * this case there is no v_writecount to adjust.
543 if ((object->flags & OBJ_TMPFS) != 0)
544 vp = object->un_pager.swp.swp_tmpfs;
547 ("vm_map_entry_set_vnode_text: wrong object type, "
548 "entry %p, object %p, add %d", entry, object, add));
552 VOP_SET_TEXT_CHECKED(vp);
553 VM_OBJECT_RUNLOCK(object);
556 VM_OBJECT_RUNLOCK(object);
557 vn_lock(vp, LK_SHARED | LK_RETRY);
558 VOP_UNSET_TEXT_CHECKED(vp);
563 VM_OBJECT_RUNLOCK(object);
568 vm_map_process_deferred(void)
571 vm_map_entry_t entry, next;
575 entry = td->td_map_def_user;
576 td->td_map_def_user = NULL;
577 while (entry != NULL) {
579 MPASS((entry->eflags & (MAP_ENTRY_WRITECNT |
580 MAP_ENTRY_VN_EXEC)) != (MAP_ENTRY_WRITECNT |
582 if ((entry->eflags & MAP_ENTRY_WRITECNT) != 0) {
584 * Decrement the object's writemappings and
585 * possibly the vnode's v_writecount.
587 KASSERT((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0,
588 ("Submap with writecount"));
589 object = entry->object.vm_object;
590 KASSERT(object != NULL, ("No object for writecount"));
591 vm_pager_release_writecount(object, entry->start,
594 vm_map_entry_set_vnode_text(entry, false);
595 vm_map_entry_deallocate(entry, FALSE);
601 _vm_map_unlock(vm_map_t map, const char *file, int line)
605 mtx_unlock_flags_(&map->system_mtx, 0, file, line);
607 sx_xunlock_(&map->lock, file, line);
608 vm_map_process_deferred();
613 _vm_map_lock_read(vm_map_t map, const char *file, int line)
617 mtx_lock_flags_(&map->system_mtx, 0, file, line);
619 sx_slock_(&map->lock, file, line);
623 _vm_map_unlock_read(vm_map_t map, const char *file, int line)
627 mtx_unlock_flags_(&map->system_mtx, 0, file, line);
629 sx_sunlock_(&map->lock, file, line);
630 vm_map_process_deferred();
635 _vm_map_trylock(vm_map_t map, const char *file, int line)
639 error = map->system_map ?
640 !mtx_trylock_flags_(&map->system_mtx, 0, file, line) :
641 !sx_try_xlock_(&map->lock, file, line);
648 _vm_map_trylock_read(vm_map_t map, const char *file, int line)
652 error = map->system_map ?
653 !mtx_trylock_flags_(&map->system_mtx, 0, file, line) :
654 !sx_try_slock_(&map->lock, file, line);
659 * _vm_map_lock_upgrade: [ internal use only ]
661 * Tries to upgrade a read (shared) lock on the specified map to a write
662 * (exclusive) lock. Returns the value "0" if the upgrade succeeds and a
663 * non-zero value if the upgrade fails. If the upgrade fails, the map is
664 * returned without a read or write lock held.
666 * Requires that the map be read locked.
669 _vm_map_lock_upgrade(vm_map_t map, const char *file, int line)
671 unsigned int last_timestamp;
673 if (map->system_map) {
674 mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
676 if (!sx_try_upgrade_(&map->lock, file, line)) {
677 last_timestamp = map->timestamp;
678 sx_sunlock_(&map->lock, file, line);
679 vm_map_process_deferred();
681 * If the map's timestamp does not change while the
682 * map is unlocked, then the upgrade succeeds.
684 sx_xlock_(&map->lock, file, line);
685 if (last_timestamp != map->timestamp) {
686 sx_xunlock_(&map->lock, file, line);
696 _vm_map_lock_downgrade(vm_map_t map, const char *file, int line)
699 if (map->system_map) {
700 mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
702 sx_downgrade_(&map->lock, file, line);
708 * Returns a non-zero value if the caller holds a write (exclusive) lock
709 * on the specified map and the value "0" otherwise.
712 vm_map_locked(vm_map_t map)
716 return (mtx_owned(&map->system_mtx));
718 return (sx_xlocked(&map->lock));
723 _vm_map_assert_locked(vm_map_t map, const char *file, int line)
727 mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
729 sx_assert_(&map->lock, SA_XLOCKED, file, line);
732 #define VM_MAP_ASSERT_LOCKED(map) \
733 _vm_map_assert_locked(map, LOCK_FILE, LOCK_LINE)
736 static int enable_vmmap_check = 1;
738 static int enable_vmmap_check = 0;
740 SYSCTL_INT(_debug, OID_AUTO, vmmap_check, CTLFLAG_RWTUN,
741 &enable_vmmap_check, 0, "Enable vm map consistency checking");
744 _vm_map_assert_consistent(vm_map_t map)
746 vm_map_entry_t entry;
747 vm_map_entry_t child;
748 vm_size_t max_left, max_right;
750 if (!enable_vmmap_check)
753 for (entry = map->header.next; entry != &map->header;
754 entry = entry->next) {
755 KASSERT(entry->prev->end <= entry->start,
756 ("map %p prev->end = %jx, start = %jx", map,
757 (uintmax_t)entry->prev->end, (uintmax_t)entry->start));
758 KASSERT(entry->start < entry->end,
759 ("map %p start = %jx, end = %jx", map,
760 (uintmax_t)entry->start, (uintmax_t)entry->end));
761 KASSERT(entry->end <= entry->next->start,
762 ("map %p end = %jx, next->start = %jx", map,
763 (uintmax_t)entry->end, (uintmax_t)entry->next->start));
764 KASSERT(entry->left == NULL ||
765 entry->left->start < entry->start,
766 ("map %p left->start = %jx, start = %jx", map,
767 (uintmax_t)entry->left->start, (uintmax_t)entry->start));
768 KASSERT(entry->right == NULL ||
769 entry->start < entry->right->start,
770 ("map %p start = %jx, right->start = %jx", map,
771 (uintmax_t)entry->start, (uintmax_t)entry->right->start));
773 max_left = (child != NULL) ? child->max_free :
774 entry->start - entry->prev->end;
775 child = entry->right;
776 max_right = (child != NULL) ? child->max_free :
777 entry->next->start - entry->end;
778 KASSERT(entry->max_free == MAX(max_left, max_right),
779 ("map %p max = %jx, max_left = %jx, max_right = %jx", map,
780 (uintmax_t)entry->max_free,
781 (uintmax_t)max_left, (uintmax_t)max_right));
785 #define VM_MAP_ASSERT_CONSISTENT(map) \
786 _vm_map_assert_consistent(map)
788 #define VM_MAP_ASSERT_LOCKED(map)
789 #define VM_MAP_ASSERT_CONSISTENT(map)
790 #endif /* INVARIANTS */
793 * _vm_map_unlock_and_wait:
795 * Atomically releases the lock on the specified map and puts the calling
796 * thread to sleep. The calling thread will remain asleep until either
797 * vm_map_wakeup() is performed on the map or the specified timeout is
800 * WARNING! This function does not perform deferred deallocations of
801 * objects and map entries. Therefore, the calling thread is expected to
802 * reacquire the map lock after reawakening and later perform an ordinary
803 * unlock operation, such as vm_map_unlock(), before completing its
804 * operation on the map.
807 _vm_map_unlock_and_wait(vm_map_t map, int timo, const char *file, int line)
810 mtx_lock(&map_sleep_mtx);
812 mtx_unlock_flags_(&map->system_mtx, 0, file, line);
814 sx_xunlock_(&map->lock, file, line);
815 return (msleep(&map->root, &map_sleep_mtx, PDROP | PVM, "vmmaps",
822 * Awaken any threads that have slept on the map using
823 * vm_map_unlock_and_wait().
826 vm_map_wakeup(vm_map_t map)
830 * Acquire and release map_sleep_mtx to prevent a wakeup()
831 * from being performed (and lost) between the map unlock
832 * and the msleep() in _vm_map_unlock_and_wait().
834 mtx_lock(&map_sleep_mtx);
835 mtx_unlock(&map_sleep_mtx);
840 vm_map_busy(vm_map_t map)
843 VM_MAP_ASSERT_LOCKED(map);
848 vm_map_unbusy(vm_map_t map)
851 VM_MAP_ASSERT_LOCKED(map);
852 KASSERT(map->busy, ("vm_map_unbusy: not busy"));
853 if (--map->busy == 0 && (map->flags & MAP_BUSY_WAKEUP)) {
854 vm_map_modflags(map, 0, MAP_BUSY_WAKEUP);
860 vm_map_wait_busy(vm_map_t map)
863 VM_MAP_ASSERT_LOCKED(map);
865 vm_map_modflags(map, MAP_BUSY_WAKEUP, 0);
867 msleep(&map->busy, &map->system_mtx, 0, "mbusy", 0);
869 sx_sleep(&map->busy, &map->lock, 0, "mbusy", 0);
875 vmspace_resident_count(struct vmspace *vmspace)
877 return pmap_resident_count(vmspace_pmap(vmspace));
883 * Creates and returns a new empty VM map with
884 * the given physical map structure, and having
885 * the given lower and upper address bounds.
888 vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max)
892 result = uma_zalloc(mapzone, M_WAITOK);
893 CTR1(KTR_VM, "vm_map_create: %p", result);
894 _vm_map_init(result, pmap, min, max);
899 * Initialize an existing vm_map structure
900 * such as that in the vmspace structure.
903 _vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min, vm_offset_t max)
906 map->header.next = map->header.prev = &map->header;
907 map->header.eflags = MAP_ENTRY_HEADER;
908 map->needs_wakeup = FALSE;
911 map->header.end = min;
912 map->header.start = max;
921 vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min, vm_offset_t max)
924 _vm_map_init(map, pmap, min, max);
925 mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
926 sx_init(&map->lock, "user map");
930 * vm_map_entry_dispose: [ internal use only ]
932 * Inverse of vm_map_entry_create.
935 vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry)
937 uma_zfree(map->system_map ? kmapentzone : mapentzone, entry);
941 * vm_map_entry_create: [ internal use only ]
943 * Allocates a VM map entry for insertion.
944 * No entry fields are filled in.
946 static vm_map_entry_t
947 vm_map_entry_create(vm_map_t map)
949 vm_map_entry_t new_entry;
952 new_entry = uma_zalloc(kmapentzone, M_NOWAIT);
954 new_entry = uma_zalloc(mapentzone, M_WAITOK);
955 if (new_entry == NULL)
956 panic("vm_map_entry_create: kernel resources exhausted");
961 * vm_map_entry_set_behavior:
963 * Set the expected access behavior, either normal, random, or
967 vm_map_entry_set_behavior(vm_map_entry_t entry, u_char behavior)
969 entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) |
970 (behavior & MAP_ENTRY_BEHAV_MASK);
974 * vm_map_entry_max_free_{left,right}:
976 * Compute the size of the largest free gap between two entries,
977 * one the root of a tree and the other the ancestor of that root
978 * that is the least or greatest ancestor found on the search path.
980 static inline vm_size_t
981 vm_map_entry_max_free_left(vm_map_entry_t root, vm_map_entry_t left_ancestor)
984 return (root->left != NULL ?
985 root->left->max_free : root->start - left_ancestor->end);
988 static inline vm_size_t
989 vm_map_entry_max_free_right(vm_map_entry_t root, vm_map_entry_t right_ancestor)
992 return (root->right != NULL ?
993 root->right->max_free : right_ancestor->start - root->end);
996 #define SPLAY_LEFT_STEP(root, y, rlist, test) do { \
997 vm_size_t max_free; \
1000 * Infer root->right->max_free == root->max_free when \
1001 * y->max_free < root->max_free || root->max_free == 0. \
1002 * Otherwise, look right to find it. \
1005 max_free = root->max_free; \
1006 KASSERT(max_free >= vm_map_entry_max_free_right(root, rlist), \
1007 ("%s: max_free invariant fails", __func__)); \
1008 if (y == NULL ? max_free > 0 : max_free - 1 < y->max_free) \
1009 max_free = vm_map_entry_max_free_right(root, rlist); \
1010 if (y != NULL && (test)) { \
1011 /* Rotate right and make y root. */ \
1012 root->left = y->right; \
1014 if (max_free < y->max_free) \
1015 root->max_free = max_free = MAX(max_free, \
1016 vm_map_entry_max_free_left(root, y)); \
1020 /* Copy right->max_free. Put root on rlist. */ \
1021 root->max_free = max_free; \
1022 KASSERT(max_free == vm_map_entry_max_free_right(root, rlist), \
1023 ("%s: max_free not copied from right", __func__)); \
1024 root->left = rlist; \
1029 #define SPLAY_RIGHT_STEP(root, y, llist, test) do { \
1030 vm_size_t max_free; \
1033 * Infer root->left->max_free == root->max_free when \
1034 * y->max_free < root->max_free || root->max_free == 0. \
1035 * Otherwise, look left to find it. \
1038 max_free = root->max_free; \
1039 KASSERT(max_free >= vm_map_entry_max_free_left(root, llist), \
1040 ("%s: max_free invariant fails", __func__)); \
1041 if (y == NULL ? max_free > 0 : max_free - 1 < y->max_free) \
1042 max_free = vm_map_entry_max_free_left(root, llist); \
1043 if (y != NULL && (test)) { \
1044 /* Rotate left and make y root. */ \
1045 root->right = y->left; \
1047 if (max_free < y->max_free) \
1048 root->max_free = max_free = MAX(max_free, \
1049 vm_map_entry_max_free_right(root, y)); \
1053 /* Copy left->max_free. Put root on llist. */ \
1054 root->max_free = max_free; \
1055 KASSERT(max_free == vm_map_entry_max_free_left(root, llist), \
1056 ("%s: max_free not copied from left", __func__)); \
1057 root->right = llist; \
1063 * Walk down the tree until we find addr or a NULL pointer where addr would go,
1064 * breaking off left and right subtrees of nodes less than, or greater than
1065 * addr. Treat pointers to nodes with max_free < length as NULL pointers.
1066 * llist and rlist are the two sides in reverse order (bottom-up), with llist
1067 * linked by the right pointer and rlist linked by the left pointer in the
1068 * vm_map_entry, and both lists terminated by &map->header. This function, and
1069 * the subsequent call to vm_map_splay_merge, rely on the start and end address
1070 * values in &map->header.
1072 static vm_map_entry_t
1073 vm_map_splay_split(vm_map_t map, vm_offset_t addr, vm_size_t length,
1074 vm_map_entry_t *out_llist, vm_map_entry_t *out_rlist)
1076 vm_map_entry_t llist, rlist, root, y;
1078 llist = rlist = &map->header;
1080 while (root != NULL && root->max_free >= length) {
1081 KASSERT(llist->end <= root->start && root->end <= rlist->start,
1082 ("%s: root not within tree bounds", __func__));
1083 if (addr < root->start) {
1084 SPLAY_LEFT_STEP(root, y, rlist,
1085 y->max_free >= length && addr < y->start);
1086 } else if (addr >= root->end) {
1087 SPLAY_RIGHT_STEP(root, y, llist,
1088 y->max_free >= length && addr >= y->end);
1098 vm_map_splay_findnext(vm_map_entry_t root, vm_map_entry_t *iolist)
1100 vm_map_entry_t rlist, y;
1104 while (root != NULL)
1105 SPLAY_LEFT_STEP(root, y, rlist, true);
1110 vm_map_splay_findprev(vm_map_entry_t root, vm_map_entry_t *iolist)
1112 vm_map_entry_t llist, y;
1116 while (root != NULL)
1117 SPLAY_RIGHT_STEP(root, y, llist, true);
1122 vm_map_entry_swap(vm_map_entry_t *a, vm_map_entry_t *b)
1132 * Walk back up the two spines, flip the pointers and set max_free. The
1133 * subtrees of the root go at the bottom of llist and rlist.
1136 vm_map_splay_merge(vm_map_t map, vm_map_entry_t root,
1137 vm_map_entry_t llist, vm_map_entry_t rlist)
1139 vm_map_entry_t prev;
1140 vm_size_t max_free_left, max_free_right;
1142 max_free_left = vm_map_entry_max_free_left(root, llist);
1143 if (llist != &map->header) {
1147 * The max_free values of the children of llist are in
1148 * llist->max_free and max_free_left. Update with the
1151 llist->max_free = max_free_left =
1152 MAX(llist->max_free, max_free_left);
1153 vm_map_entry_swap(&llist->right, &prev);
1154 vm_map_entry_swap(&prev, &llist);
1155 } while (llist != &map->header);
1158 max_free_right = vm_map_entry_max_free_right(root, rlist);
1159 if (rlist != &map->header) {
1163 * The max_free values of the children of rlist are in
1164 * rlist->max_free and max_free_right. Update with the
1167 rlist->max_free = max_free_right =
1168 MAX(rlist->max_free, max_free_right);
1169 vm_map_entry_swap(&rlist->left, &prev);
1170 vm_map_entry_swap(&prev, &rlist);
1171 } while (rlist != &map->header);
1174 root->max_free = MAX(max_free_left, max_free_right);
1181 * The Sleator and Tarjan top-down splay algorithm with the
1182 * following variation. Max_free must be computed bottom-up, so
1183 * on the downward pass, maintain the left and right spines in
1184 * reverse order. Then, make a second pass up each side to fix
1185 * the pointers and compute max_free. The time bound is O(log n)
1188 * The new root is the vm_map_entry containing "addr", or else an
1189 * adjacent entry (lower if possible) if addr is not in the tree.
1191 * The map must be locked, and leaves it so.
1193 * Returns: the new root.
1195 static vm_map_entry_t
1196 vm_map_splay(vm_map_t map, vm_offset_t addr)
1198 vm_map_entry_t llist, rlist, root;
1200 root = vm_map_splay_split(map, addr, 0, &llist, &rlist);
1203 } else if (llist != &map->header) {
1205 * Recover the greatest node in the left
1206 * subtree and make it the root.
1209 llist = root->right;
1211 } else if (rlist != &map->header) {
1213 * Recover the least node in the right
1214 * subtree and make it the root.
1220 /* There is no root. */
1223 vm_map_splay_merge(map, root, llist, rlist);
1224 VM_MAP_ASSERT_CONSISTENT(map);
1229 * vm_map_entry_{un,}link:
1231 * Insert/remove entries from maps.
1234 vm_map_entry_link(vm_map_t map, vm_map_entry_t entry)
1236 vm_map_entry_t llist, rlist, root;
1239 "vm_map_entry_link: map %p, nentries %d, entry %p", map,
1240 map->nentries, entry);
1241 VM_MAP_ASSERT_LOCKED(map);
1243 root = vm_map_splay_split(map, entry->start, 0, &llist, &rlist);
1244 KASSERT(root == NULL,
1245 ("vm_map_entry_link: link object already mapped"));
1246 entry->prev = llist;
1247 entry->next = rlist;
1248 llist->next = rlist->prev = entry;
1249 entry->left = entry->right = NULL;
1250 vm_map_splay_merge(map, entry, llist, rlist);
1251 VM_MAP_ASSERT_CONSISTENT(map);
1254 enum unlink_merge_type {
1261 vm_map_entry_unlink(vm_map_t map, vm_map_entry_t entry,
1262 enum unlink_merge_type op)
1264 vm_map_entry_t llist, rlist, root, y;
1266 VM_MAP_ASSERT_LOCKED(map);
1267 root = vm_map_splay_split(map, entry->start, 0, &llist, &rlist);
1268 KASSERT(root != NULL,
1269 ("vm_map_entry_unlink: unlink object not mapped"));
1272 case UNLINK_MERGE_PREV:
1273 vm_map_splay_findprev(root, &llist);
1274 llist->end = root->end;
1277 llist = root->right;
1280 case UNLINK_MERGE_NEXT:
1281 vm_map_splay_findnext(root, &rlist);
1282 rlist->start = root->start;
1283 rlist->offset = root->offset;
1289 case UNLINK_MERGE_NONE:
1290 vm_map_splay_findprev(root, &llist);
1291 vm_map_splay_findnext(root, &rlist);
1292 if (llist != &map->header) {
1294 llist = root->right;
1296 } else if (rlist != &map->header) {
1305 y->prev = entry->prev;
1308 vm_map_splay_merge(map, root, llist, rlist);
1311 VM_MAP_ASSERT_CONSISTENT(map);
1313 CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map,
1314 map->nentries, entry);
1318 * vm_map_entry_resize_free:
1320 * Recompute the amount of free space following a modified vm_map_entry
1321 * and propagate those values up the tree. Call this function after
1322 * resizing a map entry in-place by changing the end value, without a
1323 * call to vm_map_entry_link() or _unlink().
1325 * The map must be locked, and leaves it so.
1328 vm_map_entry_resize_free(vm_map_t map, vm_map_entry_t entry)
1330 vm_map_entry_t llist, rlist, root;
1332 VM_MAP_ASSERT_LOCKED(map);
1333 root = vm_map_splay_split(map, entry->start, 0, &llist, &rlist);
1334 KASSERT(root != NULL,
1335 ("vm_map_entry_resize_free: resize_free object not mapped"));
1336 vm_map_splay_findnext(root, &rlist);
1338 vm_map_splay_merge(map, root, llist, rlist);
1339 VM_MAP_ASSERT_CONSISTENT(map);
1340 CTR3(KTR_VM, "vm_map_entry_resize_free: map %p, nentries %d, entry %p", map,
1341 map->nentries, entry);
1345 * vm_map_lookup_entry: [ internal use only ]
1347 * Finds the map entry containing (or
1348 * immediately preceding) the specified address
1349 * in the given map; the entry is returned
1350 * in the "entry" parameter. The boolean
1351 * result indicates whether the address is
1352 * actually contained in the map.
1355 vm_map_lookup_entry(
1357 vm_offset_t address,
1358 vm_map_entry_t *entry) /* OUT */
1360 vm_map_entry_t cur, lbound;
1364 * If the map is empty, then the map entry immediately preceding
1365 * "address" is the map's header.
1369 *entry = &map->header;
1372 if (address >= cur->start && cur->end > address) {
1376 if ((locked = vm_map_locked(map)) ||
1377 sx_try_upgrade(&map->lock)) {
1379 * Splay requires a write lock on the map. However, it only
1380 * restructures the binary search tree; it does not otherwise
1381 * change the map. Thus, the map's timestamp need not change
1382 * on a temporary upgrade.
1384 cur = vm_map_splay(map, address);
1386 sx_downgrade(&map->lock);
1389 * If "address" is contained within a map entry, the new root
1390 * is that map entry. Otherwise, the new root is a map entry
1391 * immediately before or after "address".
1393 if (address < cur->start) {
1394 *entry = &map->header;
1398 return (address < cur->end);
1401 * Since the map is only locked for read access, perform a
1402 * standard binary search tree lookup for "address".
1404 lbound = &map->header;
1406 if (address < cur->start) {
1408 } else if (cur->end <= address) {
1415 } while (cur != NULL);
1423 * Inserts the given whole VM object into the target
1424 * map at the specified address range. The object's
1425 * size should match that of the address range.
1427 * Requires that the map be locked, and leaves it so.
1429 * If object is non-NULL, ref count must be bumped by caller
1430 * prior to making call to account for the new entry.
1433 vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1434 vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max, int cow)
1436 vm_map_entry_t new_entry, prev_entry, temp_entry;
1438 vm_eflags_t protoeflags;
1439 vm_inherit_t inheritance;
1441 VM_MAP_ASSERT_LOCKED(map);
1442 KASSERT(object != kernel_object ||
1443 (cow & MAP_COPY_ON_WRITE) == 0,
1444 ("vm_map_insert: kernel object and COW"));
1445 KASSERT(object == NULL || (cow & MAP_NOFAULT) == 0,
1446 ("vm_map_insert: paradoxical MAP_NOFAULT request"));
1447 KASSERT((prot & ~max) == 0,
1448 ("prot %#x is not subset of max_prot %#x", prot, max));
1451 * Check that the start and end points are not bogus.
1453 if (start < vm_map_min(map) || end > vm_map_max(map) ||
1455 return (KERN_INVALID_ADDRESS);
1458 * Find the entry prior to the proposed starting address; if it's part
1459 * of an existing entry, this range is bogus.
1461 if (vm_map_lookup_entry(map, start, &temp_entry))
1462 return (KERN_NO_SPACE);
1464 prev_entry = temp_entry;
1467 * Assert that the next entry doesn't overlap the end point.
1469 if (prev_entry->next->start < end)
1470 return (KERN_NO_SPACE);
1472 if ((cow & MAP_CREATE_GUARD) != 0 && (object != NULL ||
1473 max != VM_PROT_NONE))
1474 return (KERN_INVALID_ARGUMENT);
1477 if (cow & MAP_COPY_ON_WRITE)
1478 protoeflags |= MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY;
1479 if (cow & MAP_NOFAULT)
1480 protoeflags |= MAP_ENTRY_NOFAULT;
1481 if (cow & MAP_DISABLE_SYNCER)
1482 protoeflags |= MAP_ENTRY_NOSYNC;
1483 if (cow & MAP_DISABLE_COREDUMP)
1484 protoeflags |= MAP_ENTRY_NOCOREDUMP;
1485 if (cow & MAP_STACK_GROWS_DOWN)
1486 protoeflags |= MAP_ENTRY_GROWS_DOWN;
1487 if (cow & MAP_STACK_GROWS_UP)
1488 protoeflags |= MAP_ENTRY_GROWS_UP;
1489 if (cow & MAP_WRITECOUNT)
1490 protoeflags |= MAP_ENTRY_WRITECNT;
1491 if (cow & MAP_VN_EXEC)
1492 protoeflags |= MAP_ENTRY_VN_EXEC;
1493 if ((cow & MAP_CREATE_GUARD) != 0)
1494 protoeflags |= MAP_ENTRY_GUARD;
1495 if ((cow & MAP_CREATE_STACK_GAP_DN) != 0)
1496 protoeflags |= MAP_ENTRY_STACK_GAP_DN;
1497 if ((cow & MAP_CREATE_STACK_GAP_UP) != 0)
1498 protoeflags |= MAP_ENTRY_STACK_GAP_UP;
1499 if (cow & MAP_INHERIT_SHARE)
1500 inheritance = VM_INHERIT_SHARE;
1502 inheritance = VM_INHERIT_DEFAULT;
1505 if ((cow & (MAP_ACC_NO_CHARGE | MAP_NOFAULT | MAP_CREATE_GUARD)) != 0)
1507 if ((cow & MAP_ACC_CHARGED) || ((prot & VM_PROT_WRITE) &&
1508 ((protoeflags & MAP_ENTRY_NEEDS_COPY) || object == NULL))) {
1509 if (!(cow & MAP_ACC_CHARGED) && !swap_reserve(end - start))
1510 return (KERN_RESOURCE_SHORTAGE);
1511 KASSERT(object == NULL ||
1512 (protoeflags & MAP_ENTRY_NEEDS_COPY) != 0 ||
1513 object->cred == NULL,
1514 ("overcommit: vm_map_insert o %p", object));
1515 cred = curthread->td_ucred;
1519 /* Expand the kernel pmap, if necessary. */
1520 if (map == kernel_map && end > kernel_vm_end)
1521 pmap_growkernel(end);
1522 if (object != NULL) {
1524 * OBJ_ONEMAPPING must be cleared unless this mapping
1525 * is trivially proven to be the only mapping for any
1526 * of the object's pages. (Object granularity
1527 * reference counting is insufficient to recognize
1528 * aliases with precision.)
1530 VM_OBJECT_WLOCK(object);
1531 if (object->ref_count > 1 || object->shadow_count != 0)
1532 vm_object_clear_flag(object, OBJ_ONEMAPPING);
1533 VM_OBJECT_WUNLOCK(object);
1534 } else if ((prev_entry->eflags & ~MAP_ENTRY_USER_WIRED) ==
1536 (cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP |
1537 MAP_VN_EXEC)) == 0 &&
1538 prev_entry->end == start && (prev_entry->cred == cred ||
1539 (prev_entry->object.vm_object != NULL &&
1540 prev_entry->object.vm_object->cred == cred)) &&
1541 vm_object_coalesce(prev_entry->object.vm_object,
1543 (vm_size_t)(prev_entry->end - prev_entry->start),
1544 (vm_size_t)(end - prev_entry->end), cred != NULL &&
1545 (protoeflags & MAP_ENTRY_NEEDS_COPY) == 0)) {
1547 * We were able to extend the object. Determine if we
1548 * can extend the previous map entry to include the
1549 * new range as well.
1551 if (prev_entry->inheritance == inheritance &&
1552 prev_entry->protection == prot &&
1553 prev_entry->max_protection == max &&
1554 prev_entry->wired_count == 0) {
1555 KASSERT((prev_entry->eflags & MAP_ENTRY_USER_WIRED) ==
1556 0, ("prev_entry %p has incoherent wiring",
1558 if ((prev_entry->eflags & MAP_ENTRY_GUARD) == 0)
1559 map->size += end - prev_entry->end;
1560 prev_entry->end = end;
1561 vm_map_entry_resize_free(map, prev_entry);
1562 vm_map_simplify_entry(map, prev_entry);
1563 return (KERN_SUCCESS);
1567 * If we can extend the object but cannot extend the
1568 * map entry, we have to create a new map entry. We
1569 * must bump the ref count on the extended object to
1570 * account for it. object may be NULL.
1572 object = prev_entry->object.vm_object;
1573 offset = prev_entry->offset +
1574 (prev_entry->end - prev_entry->start);
1575 vm_object_reference(object);
1576 if (cred != NULL && object != NULL && object->cred != NULL &&
1577 !(prev_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
1578 /* Object already accounts for this uid. */
1586 * Create a new entry
1588 new_entry = vm_map_entry_create(map);
1589 new_entry->start = start;
1590 new_entry->end = end;
1591 new_entry->cred = NULL;
1593 new_entry->eflags = protoeflags;
1594 new_entry->object.vm_object = object;
1595 new_entry->offset = offset;
1597 new_entry->inheritance = inheritance;
1598 new_entry->protection = prot;
1599 new_entry->max_protection = max;
1600 new_entry->wired_count = 0;
1601 new_entry->wiring_thread = NULL;
1602 new_entry->read_ahead = VM_FAULT_READ_AHEAD_INIT;
1603 new_entry->next_read = start;
1605 KASSERT(cred == NULL || !ENTRY_CHARGED(new_entry),
1606 ("overcommit: vm_map_insert leaks vm_map %p", new_entry));
1607 new_entry->cred = cred;
1610 * Insert the new entry into the list
1612 vm_map_entry_link(map, new_entry);
1613 if ((new_entry->eflags & MAP_ENTRY_GUARD) == 0)
1614 map->size += new_entry->end - new_entry->start;
1617 * Try to coalesce the new entry with both the previous and next
1618 * entries in the list. Previously, we only attempted to coalesce
1619 * with the previous entry when object is NULL. Here, we handle the
1620 * other cases, which are less common.
1622 vm_map_simplify_entry(map, new_entry);
1624 if ((cow & (MAP_PREFAULT | MAP_PREFAULT_PARTIAL)) != 0) {
1625 vm_map_pmap_enter(map, start, prot, object, OFF_TO_IDX(offset),
1626 end - start, cow & MAP_PREFAULT_PARTIAL);
1629 return (KERN_SUCCESS);
1635 * Find the first fit (lowest VM address) for "length" free bytes
1636 * beginning at address >= start in the given map.
1638 * In a vm_map_entry, "max_free" is the maximum amount of
1639 * contiguous free space between an entry in its subtree and a
1640 * neighbor of that entry. This allows finding a free region in
1641 * one path down the tree, so O(log n) amortized with splay
1644 * The map must be locked, and leaves it so.
1646 * Returns: starting address if sufficient space,
1647 * vm_map_max(map)-length+1 if insufficient space.
1650 vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length)
1652 vm_map_entry_t llist, rlist, root, y;
1653 vm_size_t left_length;
1654 vm_offset_t gap_end;
1657 * Request must fit within min/max VM address and must avoid
1660 start = MAX(start, vm_map_min(map));
1661 if (start >= vm_map_max(map) || length > vm_map_max(map) - start)
1662 return (vm_map_max(map) - length + 1);
1664 /* Empty tree means wide open address space. */
1665 if (map->root == NULL)
1669 * After splay_split, if start is within an entry, push it to the start
1670 * of the following gap. If rlist is at the end of the gap containing
1671 * start, save the end of that gap in gap_end to see if the gap is big
1672 * enough; otherwise set gap_end to start skip gap-checking and move
1673 * directly to a search of the right subtree.
1675 root = vm_map_splay_split(map, start, length, &llist, &rlist);
1676 gap_end = rlist->start;
1679 if (root->right != NULL)
1681 } else if (rlist != &map->header) {
1687 llist = root->right;
1690 vm_map_splay_merge(map, root, llist, rlist);
1691 VM_MAP_ASSERT_CONSISTENT(map);
1692 if (length <= gap_end - start)
1695 /* With max_free, can immediately tell if no solution. */
1696 if (root->right == NULL || length > root->right->max_free)
1697 return (vm_map_max(map) - length + 1);
1700 * Splay for the least large-enough gap in the right subtree.
1702 llist = rlist = &map->header;
1703 for (left_length = 0;;
1704 left_length = vm_map_entry_max_free_left(root, llist)) {
1705 if (length <= left_length)
1706 SPLAY_LEFT_STEP(root, y, rlist,
1707 length <= vm_map_entry_max_free_left(y, llist));
1709 SPLAY_RIGHT_STEP(root, y, llist,
1710 length > vm_map_entry_max_free_left(y, root));
1715 llist = root->right;
1717 if (rlist != &map->header) {
1721 vm_map_splay_merge(map, y, &map->header, rlist);
1723 vm_map_entry_max_free_left(y, root),
1724 vm_map_entry_max_free_right(y, &map->header));
1727 vm_map_splay_merge(map, root, llist, &map->header);
1728 VM_MAP_ASSERT_CONSISTENT(map);
1733 vm_map_fixed(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1734 vm_offset_t start, vm_size_t length, vm_prot_t prot,
1735 vm_prot_t max, int cow)
1740 end = start + length;
1741 KASSERT((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0 ||
1743 ("vm_map_fixed: non-NULL backing object for stack"));
1745 VM_MAP_RANGE_CHECK(map, start, end);
1746 if ((cow & MAP_CHECK_EXCL) == 0)
1747 vm_map_delete(map, start, end);
1748 if ((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) != 0) {
1749 result = vm_map_stack_locked(map, start, length, sgrowsiz,
1752 result = vm_map_insert(map, object, offset, start, end,
1759 static const int aslr_pages_rnd_64[2] = {0x1000, 0x10};
1760 static const int aslr_pages_rnd_32[2] = {0x100, 0x4};
1762 static int cluster_anon = 1;
1763 SYSCTL_INT(_vm, OID_AUTO, cluster_anon, CTLFLAG_RW,
1765 "Cluster anonymous mappings: 0 = no, 1 = yes if no hint, 2 = always");
1768 clustering_anon_allowed(vm_offset_t addr)
1771 switch (cluster_anon) {
1782 static long aslr_restarts;
1783 SYSCTL_LONG(_vm, OID_AUTO, aslr_restarts, CTLFLAG_RD,
1785 "Number of aslr failures");
1787 #define MAP_32BIT_MAX_ADDR ((vm_offset_t)1 << 31)
1790 * Searches for the specified amount of free space in the given map with the
1791 * specified alignment. Performs an address-ordered, first-fit search from
1792 * the given address "*addr", with an optional upper bound "max_addr". If the
1793 * parameter "alignment" is zero, then the alignment is computed from the
1794 * given (object, offset) pair so as to enable the greatest possible use of
1795 * superpage mappings. Returns KERN_SUCCESS and the address of the free space
1796 * in "*addr" if successful. Otherwise, returns KERN_NO_SPACE.
1798 * The map must be locked. Initially, there must be at least "length" bytes
1799 * of free space at the given address.
1802 vm_map_alignspace(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1803 vm_offset_t *addr, vm_size_t length, vm_offset_t max_addr,
1804 vm_offset_t alignment)
1806 vm_offset_t aligned_addr, free_addr;
1808 VM_MAP_ASSERT_LOCKED(map);
1810 KASSERT(free_addr == vm_map_findspace(map, free_addr, length),
1811 ("caller failed to provide space %#jx at address %p",
1812 (uintmax_t)length, (void *)free_addr));
1815 * At the start of every iteration, the free space at address
1816 * "*addr" is at least "length" bytes.
1819 pmap_align_superpage(object, offset, addr, length);
1820 else if ((*addr & (alignment - 1)) != 0) {
1821 *addr &= ~(alignment - 1);
1824 aligned_addr = *addr;
1825 if (aligned_addr == free_addr) {
1827 * Alignment did not change "*addr", so "*addr" must
1828 * still provide sufficient free space.
1830 return (KERN_SUCCESS);
1834 * Test for address wrap on "*addr". A wrapped "*addr" could
1835 * be a valid address, in which case vm_map_findspace() cannot
1836 * be relied upon to fail.
1838 if (aligned_addr < free_addr)
1839 return (KERN_NO_SPACE);
1840 *addr = vm_map_findspace(map, aligned_addr, length);
1841 if (*addr + length > vm_map_max(map) ||
1842 (max_addr != 0 && *addr + length > max_addr))
1843 return (KERN_NO_SPACE);
1845 if (free_addr == aligned_addr) {
1847 * If a successful call to vm_map_findspace() did not
1848 * change "*addr", then "*addr" must still be aligned
1849 * and provide sufficient free space.
1851 return (KERN_SUCCESS);
1857 * vm_map_find finds an unallocated region in the target address
1858 * map with the given length. The search is defined to be
1859 * first-fit from the specified address; the region found is
1860 * returned in the same parameter.
1862 * If object is non-NULL, ref count must be bumped by caller
1863 * prior to making call to account for the new entry.
1866 vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1867 vm_offset_t *addr, /* IN/OUT */
1868 vm_size_t length, vm_offset_t max_addr, int find_space,
1869 vm_prot_t prot, vm_prot_t max, int cow)
1871 vm_offset_t alignment, curr_min_addr, min_addr;
1872 int gap, pidx, rv, try;
1873 bool cluster, en_aslr, update_anon;
1875 KASSERT((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0 ||
1877 ("vm_map_find: non-NULL backing object for stack"));
1878 MPASS((cow & MAP_REMAP) == 0 || (find_space == VMFS_NO_SPACE &&
1879 (cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0));
1880 if (find_space == VMFS_OPTIMAL_SPACE && (object == NULL ||
1881 (object->flags & OBJ_COLORED) == 0))
1882 find_space = VMFS_ANY_SPACE;
1883 if (find_space >> 8 != 0) {
1884 KASSERT((find_space & 0xff) == 0, ("bad VMFS flags"));
1885 alignment = (vm_offset_t)1 << (find_space >> 8);
1888 en_aslr = (map->flags & MAP_ASLR) != 0;
1889 update_anon = cluster = clustering_anon_allowed(*addr) &&
1890 (map->flags & MAP_IS_SUB_MAP) == 0 && max_addr == 0 &&
1891 find_space != VMFS_NO_SPACE && object == NULL &&
1892 (cow & (MAP_INHERIT_SHARE | MAP_STACK_GROWS_UP |
1893 MAP_STACK_GROWS_DOWN)) == 0 && prot != PROT_NONE;
1894 curr_min_addr = min_addr = *addr;
1895 if (en_aslr && min_addr == 0 && !cluster &&
1896 find_space != VMFS_NO_SPACE &&
1897 (map->flags & MAP_ASLR_IGNSTART) != 0)
1898 curr_min_addr = min_addr = vm_map_min(map);
1902 curr_min_addr = map->anon_loc;
1903 if (curr_min_addr == 0)
1906 if (find_space != VMFS_NO_SPACE) {
1907 KASSERT(find_space == VMFS_ANY_SPACE ||
1908 find_space == VMFS_OPTIMAL_SPACE ||
1909 find_space == VMFS_SUPER_SPACE ||
1910 alignment != 0, ("unexpected VMFS flag"));
1913 * When creating an anonymous mapping, try clustering
1914 * with an existing anonymous mapping first.
1916 * We make up to two attempts to find address space
1917 * for a given find_space value. The first attempt may
1918 * apply randomization or may cluster with an existing
1919 * anonymous mapping. If this first attempt fails,
1920 * perform a first-fit search of the available address
1923 * If all tries failed, and find_space is
1924 * VMFS_OPTIMAL_SPACE, fallback to VMFS_ANY_SPACE.
1925 * Again enable clustering and randomization.
1932 * Second try: we failed either to find a
1933 * suitable region for randomizing the
1934 * allocation, or to cluster with an existing
1935 * mapping. Retry with free run.
1937 curr_min_addr = (map->flags & MAP_ASLR_IGNSTART) != 0 ?
1938 vm_map_min(map) : min_addr;
1939 atomic_add_long(&aslr_restarts, 1);
1942 if (try == 1 && en_aslr && !cluster) {
1944 * Find space for allocation, including
1945 * gap needed for later randomization.
1947 pidx = MAXPAGESIZES > 1 && pagesizes[1] != 0 &&
1948 (find_space == VMFS_SUPER_SPACE || find_space ==
1949 VMFS_OPTIMAL_SPACE) ? 1 : 0;
1950 gap = vm_map_max(map) > MAP_32BIT_MAX_ADDR &&
1951 (max_addr == 0 || max_addr > MAP_32BIT_MAX_ADDR) ?
1952 aslr_pages_rnd_64[pidx] : aslr_pages_rnd_32[pidx];
1953 *addr = vm_map_findspace(map, curr_min_addr,
1954 length + gap * pagesizes[pidx]);
1955 if (*addr + length + gap * pagesizes[pidx] >
1958 /* And randomize the start address. */
1959 *addr += (arc4random() % gap) * pagesizes[pidx];
1960 if (max_addr != 0 && *addr + length > max_addr)
1963 *addr = vm_map_findspace(map, curr_min_addr, length);
1964 if (*addr + length > vm_map_max(map) ||
1965 (max_addr != 0 && *addr + length > max_addr)) {
1976 if (find_space != VMFS_ANY_SPACE &&
1977 (rv = vm_map_alignspace(map, object, offset, addr, length,
1978 max_addr, alignment)) != KERN_SUCCESS) {
1979 if (find_space == VMFS_OPTIMAL_SPACE) {
1980 find_space = VMFS_ANY_SPACE;
1981 curr_min_addr = min_addr;
1982 cluster = update_anon;
1988 } else if ((cow & MAP_REMAP) != 0) {
1989 if (*addr < vm_map_min(map) ||
1990 *addr + length > vm_map_max(map) ||
1991 *addr + length <= length) {
1992 rv = KERN_INVALID_ADDRESS;
1995 vm_map_delete(map, *addr, *addr + length);
1997 if ((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) != 0) {
1998 rv = vm_map_stack_locked(map, *addr, length, sgrowsiz, prot,
2001 rv = vm_map_insert(map, object, offset, *addr, *addr + length,
2004 if (rv == KERN_SUCCESS && update_anon)
2005 map->anon_loc = *addr + length;
2012 * vm_map_find_min() is a variant of vm_map_find() that takes an
2013 * additional parameter (min_addr) and treats the given address
2014 * (*addr) differently. Specifically, it treats *addr as a hint
2015 * and not as the minimum address where the mapping is created.
2017 * This function works in two phases. First, it tries to
2018 * allocate above the hint. If that fails and the hint is
2019 * greater than min_addr, it performs a second pass, replacing
2020 * the hint with min_addr as the minimum address for the
2024 vm_map_find_min(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
2025 vm_offset_t *addr, vm_size_t length, vm_offset_t min_addr,
2026 vm_offset_t max_addr, int find_space, vm_prot_t prot, vm_prot_t max,
2034 rv = vm_map_find(map, object, offset, addr, length, max_addr,
2035 find_space, prot, max, cow);
2036 if (rv == KERN_SUCCESS || min_addr >= hint)
2038 *addr = hint = min_addr;
2043 * A map entry with any of the following flags set must not be merged with
2046 #define MAP_ENTRY_NOMERGE_MASK (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP | \
2047 MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP | MAP_ENTRY_VN_EXEC)
2050 vm_map_mergeable_neighbors(vm_map_entry_t prev, vm_map_entry_t entry)
2053 KASSERT((prev->eflags & MAP_ENTRY_NOMERGE_MASK) == 0 ||
2054 (entry->eflags & MAP_ENTRY_NOMERGE_MASK) == 0,
2055 ("vm_map_mergeable_neighbors: neither %p nor %p are mergeable",
2057 return (prev->end == entry->start &&
2058 prev->object.vm_object == entry->object.vm_object &&
2059 (prev->object.vm_object == NULL ||
2060 prev->offset + (prev->end - prev->start) == entry->offset) &&
2061 prev->eflags == entry->eflags &&
2062 prev->protection == entry->protection &&
2063 prev->max_protection == entry->max_protection &&
2064 prev->inheritance == entry->inheritance &&
2065 prev->wired_count == entry->wired_count &&
2066 prev->cred == entry->cred);
2070 vm_map_merged_neighbor_dispose(vm_map_t map, vm_map_entry_t entry)
2074 * If the backing object is a vnode object, vm_object_deallocate()
2075 * calls vrele(). However, vrele() does not lock the vnode because
2076 * the vnode has additional references. Thus, the map lock can be
2077 * kept without causing a lock-order reversal with the vnode lock.
2079 * Since we count the number of virtual page mappings in
2080 * object->un_pager.vnp.writemappings, the writemappings value
2081 * should not be adjusted when the entry is disposed of.
2083 if (entry->object.vm_object != NULL)
2084 vm_object_deallocate(entry->object.vm_object);
2085 if (entry->cred != NULL)
2086 crfree(entry->cred);
2087 vm_map_entry_dispose(map, entry);
2091 * vm_map_simplify_entry:
2093 * Simplify the given map entry by merging with either neighbor. This
2094 * routine also has the ability to merge with both neighbors.
2096 * The map must be locked.
2098 * This routine guarantees that the passed entry remains valid (though
2099 * possibly extended). When merging, this routine may delete one or
2103 vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry)
2105 vm_map_entry_t next, prev;
2107 if ((entry->eflags & MAP_ENTRY_NOMERGE_MASK) != 0)
2110 if (vm_map_mergeable_neighbors(prev, entry)) {
2111 vm_map_entry_unlink(map, prev, UNLINK_MERGE_NEXT);
2112 vm_map_merged_neighbor_dispose(map, prev);
2115 if (vm_map_mergeable_neighbors(entry, next)) {
2116 vm_map_entry_unlink(map, next, UNLINK_MERGE_PREV);
2117 vm_map_merged_neighbor_dispose(map, next);
2122 * vm_map_clip_start: [ internal use only ]
2124 * Asserts that the given entry begins at or after
2125 * the specified address; if necessary,
2126 * it splits the entry into two.
2128 #define vm_map_clip_start(map, entry, startaddr) \
2130 if (startaddr > entry->start) \
2131 _vm_map_clip_start(map, entry, startaddr); \
2135 * This routine is called only when it is known that
2136 * the entry must be split.
2139 _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start)
2141 vm_map_entry_t new_entry;
2143 VM_MAP_ASSERT_LOCKED(map);
2144 KASSERT(entry->end > start && entry->start < start,
2145 ("_vm_map_clip_start: invalid clip of entry %p", entry));
2148 * Split off the front portion -- note that we must insert the new
2149 * entry BEFORE this one, so that this entry has the specified
2152 vm_map_simplify_entry(map, entry);
2155 * If there is no object backing this entry, we might as well create
2156 * one now. If we defer it, an object can get created after the map
2157 * is clipped, and individual objects will be created for the split-up
2158 * map. This is a bit of a hack, but is also about the best place to
2159 * put this improvement.
2161 if (entry->object.vm_object == NULL && !map->system_map &&
2162 (entry->eflags & MAP_ENTRY_GUARD) == 0) {
2164 object = vm_object_allocate(OBJT_DEFAULT,
2165 atop(entry->end - entry->start));
2166 entry->object.vm_object = object;
2168 if (entry->cred != NULL) {
2169 object->cred = entry->cred;
2170 object->charge = entry->end - entry->start;
2173 } else if (entry->object.vm_object != NULL &&
2174 ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) &&
2175 entry->cred != NULL) {
2176 VM_OBJECT_WLOCK(entry->object.vm_object);
2177 KASSERT(entry->object.vm_object->cred == NULL,
2178 ("OVERCOMMIT: vm_entry_clip_start: both cred e %p", entry));
2179 entry->object.vm_object->cred = entry->cred;
2180 entry->object.vm_object->charge = entry->end - entry->start;
2181 VM_OBJECT_WUNLOCK(entry->object.vm_object);
2185 new_entry = vm_map_entry_create(map);
2186 *new_entry = *entry;
2188 new_entry->end = start;
2189 entry->offset += (start - entry->start);
2190 entry->start = start;
2191 if (new_entry->cred != NULL)
2192 crhold(entry->cred);
2194 vm_map_entry_link(map, new_entry);
2196 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
2197 vm_object_reference(new_entry->object.vm_object);
2198 vm_map_entry_set_vnode_text(new_entry, true);
2200 * The object->un_pager.vnp.writemappings for the
2201 * object of MAP_ENTRY_WRITECNT type entry shall be
2202 * kept as is here. The virtual pages are
2203 * re-distributed among the clipped entries, so the sum is
2210 * vm_map_clip_end: [ internal use only ]
2212 * Asserts that the given entry ends at or before
2213 * the specified address; if necessary,
2214 * it splits the entry into two.
2216 #define vm_map_clip_end(map, entry, endaddr) \
2218 if ((endaddr) < (entry->end)) \
2219 _vm_map_clip_end((map), (entry), (endaddr)); \
2223 * This routine is called only when it is known that
2224 * the entry must be split.
2227 _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end)
2229 vm_map_entry_t new_entry;
2231 VM_MAP_ASSERT_LOCKED(map);
2232 KASSERT(entry->start < end && entry->end > end,
2233 ("_vm_map_clip_end: invalid clip of entry %p", entry));
2236 * If there is no object backing this entry, we might as well create
2237 * one now. If we defer it, an object can get created after the map
2238 * is clipped, and individual objects will be created for the split-up
2239 * map. This is a bit of a hack, but is also about the best place to
2240 * put this improvement.
2242 if (entry->object.vm_object == NULL && !map->system_map &&
2243 (entry->eflags & MAP_ENTRY_GUARD) == 0) {
2245 object = vm_object_allocate(OBJT_DEFAULT,
2246 atop(entry->end - entry->start));
2247 entry->object.vm_object = object;
2249 if (entry->cred != NULL) {
2250 object->cred = entry->cred;
2251 object->charge = entry->end - entry->start;
2254 } else if (entry->object.vm_object != NULL &&
2255 ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) &&
2256 entry->cred != NULL) {
2257 VM_OBJECT_WLOCK(entry->object.vm_object);
2258 KASSERT(entry->object.vm_object->cred == NULL,
2259 ("OVERCOMMIT: vm_entry_clip_end: both cred e %p", entry));
2260 entry->object.vm_object->cred = entry->cred;
2261 entry->object.vm_object->charge = entry->end - entry->start;
2262 VM_OBJECT_WUNLOCK(entry->object.vm_object);
2267 * Create a new entry and insert it AFTER the specified entry
2269 new_entry = vm_map_entry_create(map);
2270 *new_entry = *entry;
2272 new_entry->start = entry->end = end;
2273 new_entry->offset += (end - entry->start);
2274 if (new_entry->cred != NULL)
2275 crhold(entry->cred);
2277 vm_map_entry_link(map, new_entry);
2279 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
2280 vm_object_reference(new_entry->object.vm_object);
2281 vm_map_entry_set_vnode_text(new_entry, true);
2286 * vm_map_submap: [ kernel use only ]
2288 * Mark the given range as handled by a subordinate map.
2290 * This range must have been created with vm_map_find,
2291 * and no other operations may have been performed on this
2292 * range prior to calling vm_map_submap.
2294 * Only a limited number of operations can be performed
2295 * within this rage after calling vm_map_submap:
2297 * [Don't try vm_map_copy!]
2299 * To remove a submapping, one must first remove the
2300 * range from the superior map, and then destroy the
2301 * submap (if desired). [Better yet, don't try it.]
2310 vm_map_entry_t entry;
2313 result = KERN_INVALID_ARGUMENT;
2315 vm_map_lock(submap);
2316 submap->flags |= MAP_IS_SUB_MAP;
2317 vm_map_unlock(submap);
2321 VM_MAP_RANGE_CHECK(map, start, end);
2323 if (vm_map_lookup_entry(map, start, &entry)) {
2324 vm_map_clip_start(map, entry, start);
2326 entry = entry->next;
2328 vm_map_clip_end(map, entry, end);
2330 if ((entry->start == start) && (entry->end == end) &&
2331 ((entry->eflags & MAP_ENTRY_COW) == 0) &&
2332 (entry->object.vm_object == NULL)) {
2333 entry->object.sub_map = submap;
2334 entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
2335 result = KERN_SUCCESS;
2339 if (result != KERN_SUCCESS) {
2340 vm_map_lock(submap);
2341 submap->flags &= ~MAP_IS_SUB_MAP;
2342 vm_map_unlock(submap);
2348 * The maximum number of pages to map if MAP_PREFAULT_PARTIAL is specified
2350 #define MAX_INIT_PT 96
2353 * vm_map_pmap_enter:
2355 * Preload the specified map's pmap with mappings to the specified
2356 * object's memory-resident pages. No further physical pages are
2357 * allocated, and no further virtual pages are retrieved from secondary
2358 * storage. If the specified flags include MAP_PREFAULT_PARTIAL, then a
2359 * limited number of page mappings are created at the low-end of the
2360 * specified address range. (For this purpose, a superpage mapping
2361 * counts as one page mapping.) Otherwise, all resident pages within
2362 * the specified address range are mapped.
2365 vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
2366 vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags)
2369 vm_page_t p, p_start;
2370 vm_pindex_t mask, psize, threshold, tmpidx;
2372 if ((prot & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 || object == NULL)
2374 VM_OBJECT_RLOCK(object);
2375 if (object->type == OBJT_DEVICE || object->type == OBJT_SG) {
2376 VM_OBJECT_RUNLOCK(object);
2377 VM_OBJECT_WLOCK(object);
2378 if (object->type == OBJT_DEVICE || object->type == OBJT_SG) {
2379 pmap_object_init_pt(map->pmap, addr, object, pindex,
2381 VM_OBJECT_WUNLOCK(object);
2384 VM_OBJECT_LOCK_DOWNGRADE(object);
2388 if (psize + pindex > object->size) {
2389 if (pindex >= object->size) {
2390 VM_OBJECT_RUNLOCK(object);
2393 psize = object->size - pindex;
2398 threshold = MAX_INIT_PT;
2400 p = vm_page_find_least(object, pindex);
2402 * Assert: the variable p is either (1) the page with the
2403 * least pindex greater than or equal to the parameter pindex
2407 p != NULL && (tmpidx = p->pindex - pindex) < psize;
2408 p = TAILQ_NEXT(p, listq)) {
2410 * don't allow an madvise to blow away our really
2411 * free pages allocating pv entries.
2413 if (((flags & MAP_PREFAULT_MADVISE) != 0 &&
2414 vm_page_count_severe()) ||
2415 ((flags & MAP_PREFAULT_PARTIAL) != 0 &&
2416 tmpidx >= threshold)) {
2420 if (p->valid == VM_PAGE_BITS_ALL) {
2421 if (p_start == NULL) {
2422 start = addr + ptoa(tmpidx);
2425 /* Jump ahead if a superpage mapping is possible. */
2426 if (p->psind > 0 && ((addr + ptoa(tmpidx)) &
2427 (pagesizes[p->psind] - 1)) == 0) {
2428 mask = atop(pagesizes[p->psind]) - 1;
2429 if (tmpidx + mask < psize &&
2430 vm_page_ps_test(p, PS_ALL_VALID, NULL)) {
2435 } else if (p_start != NULL) {
2436 pmap_enter_object(map->pmap, start, addr +
2437 ptoa(tmpidx), p_start, prot);
2441 if (p_start != NULL)
2442 pmap_enter_object(map->pmap, start, addr + ptoa(psize),
2444 VM_OBJECT_RUNLOCK(object);
2450 * Sets the protection of the specified address
2451 * region in the target map. If "set_max" is
2452 * specified, the maximum protection is to be set;
2453 * otherwise, only the current protection is affected.
2456 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
2457 vm_prot_t new_prot, boolean_t set_max)
2459 vm_map_entry_t current, entry, in_tran;
2465 return (KERN_SUCCESS);
2472 * Ensure that we are not concurrently wiring pages. vm_map_wire() may
2473 * need to fault pages into the map and will drop the map lock while
2474 * doing so, and the VM object may end up in an inconsistent state if we
2475 * update the protection on the map entry in between faults.
2477 vm_map_wait_busy(map);
2479 VM_MAP_RANGE_CHECK(map, start, end);
2481 if (vm_map_lookup_entry(map, start, &entry)) {
2482 vm_map_clip_start(map, entry, start);
2484 entry = entry->next;
2488 * Make a first pass to check for protection violations.
2490 for (current = entry; current->start < end; current = current->next) {
2491 if ((current->eflags & MAP_ENTRY_GUARD) != 0)
2493 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
2495 return (KERN_INVALID_ARGUMENT);
2497 if ((new_prot & current->max_protection) != new_prot) {
2499 return (KERN_PROTECTION_FAILURE);
2501 if ((current->eflags & MAP_ENTRY_IN_TRANSITION) != 0)
2506 * Postpone the operation until all in-transition map entries have
2507 * stabilized. An in-transition entry might already have its pages
2508 * wired and wired_count incremented, but not yet have its
2509 * MAP_ENTRY_USER_WIRED flag set. In which case, we would fail to call
2510 * vm_fault_copy_entry() in the final loop below.
2512 if (in_tran != NULL) {
2513 in_tran->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2514 vm_map_unlock_and_wait(map, 0);
2519 * Do an accounting pass for private read-only mappings that
2520 * now will do cow due to allowed write (e.g. debugger sets
2521 * breakpoint on text segment)
2523 for (current = entry; current->start < end; current = current->next) {
2525 vm_map_clip_end(map, current, end);
2528 ((new_prot & ~(current->protection)) & VM_PROT_WRITE) == 0 ||
2529 ENTRY_CHARGED(current) ||
2530 (current->eflags & MAP_ENTRY_GUARD) != 0) {
2534 cred = curthread->td_ucred;
2535 obj = current->object.vm_object;
2537 if (obj == NULL || (current->eflags & MAP_ENTRY_NEEDS_COPY)) {
2538 if (!swap_reserve(current->end - current->start)) {
2540 return (KERN_RESOURCE_SHORTAGE);
2543 current->cred = cred;
2547 VM_OBJECT_WLOCK(obj);
2548 if (obj->type != OBJT_DEFAULT && obj->type != OBJT_SWAP) {
2549 VM_OBJECT_WUNLOCK(obj);
2554 * Charge for the whole object allocation now, since
2555 * we cannot distinguish between non-charged and
2556 * charged clipped mapping of the same object later.
2558 KASSERT(obj->charge == 0,
2559 ("vm_map_protect: object %p overcharged (entry %p)",
2561 if (!swap_reserve(ptoa(obj->size))) {
2562 VM_OBJECT_WUNLOCK(obj);
2564 return (KERN_RESOURCE_SHORTAGE);
2569 obj->charge = ptoa(obj->size);
2570 VM_OBJECT_WUNLOCK(obj);
2574 * Go back and fix up protections. [Note that clipping is not
2575 * necessary the second time.]
2577 for (current = entry; current->start < end; current = current->next) {
2578 if ((current->eflags & MAP_ENTRY_GUARD) != 0)
2581 old_prot = current->protection;
2584 current->protection =
2585 (current->max_protection = new_prot) &
2588 current->protection = new_prot;
2591 * For user wired map entries, the normal lazy evaluation of
2592 * write access upgrades through soft page faults is
2593 * undesirable. Instead, immediately copy any pages that are
2594 * copy-on-write and enable write access in the physical map.
2596 if ((current->eflags & MAP_ENTRY_USER_WIRED) != 0 &&
2597 (current->protection & VM_PROT_WRITE) != 0 &&
2598 (old_prot & VM_PROT_WRITE) == 0)
2599 vm_fault_copy_entry(map, map, current, current, NULL);
2602 * When restricting access, update the physical map. Worry
2603 * about copy-on-write here.
2605 if ((old_prot & ~current->protection) != 0) {
2606 #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
2608 pmap_protect(map->pmap, current->start,
2610 current->protection & MASK(current));
2613 vm_map_simplify_entry(map, current);
2616 return (KERN_SUCCESS);
2622 * This routine traverses a processes map handling the madvise
2623 * system call. Advisories are classified as either those effecting
2624 * the vm_map_entry structure, or those effecting the underlying
2634 vm_map_entry_t current, entry;
2638 * Some madvise calls directly modify the vm_map_entry, in which case
2639 * we need to use an exclusive lock on the map and we need to perform
2640 * various clipping operations. Otherwise we only need a read-lock
2645 case MADV_SEQUENTIAL:
2662 vm_map_lock_read(map);
2669 * Locate starting entry and clip if necessary.
2671 VM_MAP_RANGE_CHECK(map, start, end);
2673 if (vm_map_lookup_entry(map, start, &entry)) {
2675 vm_map_clip_start(map, entry, start);
2677 entry = entry->next;
2682 * madvise behaviors that are implemented in the vm_map_entry.
2684 * We clip the vm_map_entry so that behavioral changes are
2685 * limited to the specified address range.
2687 for (current = entry; current->start < end;
2688 current = current->next) {
2689 if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
2692 vm_map_clip_end(map, current, end);
2696 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
2698 case MADV_SEQUENTIAL:
2699 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
2702 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
2705 current->eflags |= MAP_ENTRY_NOSYNC;
2708 current->eflags &= ~MAP_ENTRY_NOSYNC;
2711 current->eflags |= MAP_ENTRY_NOCOREDUMP;
2714 current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
2719 vm_map_simplify_entry(map, current);
2723 vm_pindex_t pstart, pend;
2726 * madvise behaviors that are implemented in the underlying
2729 * Since we don't clip the vm_map_entry, we have to clip
2730 * the vm_object pindex and count.
2732 for (current = entry; current->start < end;
2733 current = current->next) {
2734 vm_offset_t useEnd, useStart;
2736 if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
2740 * MADV_FREE would otherwise rewind time to
2741 * the creation of the shadow object. Because
2742 * we hold the VM map read-locked, neither the
2743 * entry's object nor the presence of a
2744 * backing object can change.
2746 if (behav == MADV_FREE &&
2747 current->object.vm_object != NULL &&
2748 current->object.vm_object->backing_object != NULL)
2751 pstart = OFF_TO_IDX(current->offset);
2752 pend = pstart + atop(current->end - current->start);
2753 useStart = current->start;
2754 useEnd = current->end;
2756 if (current->start < start) {
2757 pstart += atop(start - current->start);
2760 if (current->end > end) {
2761 pend -= atop(current->end - end);
2769 * Perform the pmap_advise() before clearing
2770 * PGA_REFERENCED in vm_page_advise(). Otherwise, a
2771 * concurrent pmap operation, such as pmap_remove(),
2772 * could clear a reference in the pmap and set
2773 * PGA_REFERENCED on the page before the pmap_advise()
2774 * had completed. Consequently, the page would appear
2775 * referenced based upon an old reference that
2776 * occurred before this pmap_advise() ran.
2778 if (behav == MADV_DONTNEED || behav == MADV_FREE)
2779 pmap_advise(map->pmap, useStart, useEnd,
2782 vm_object_madvise(current->object.vm_object, pstart,
2786 * Pre-populate paging structures in the
2787 * WILLNEED case. For wired entries, the
2788 * paging structures are already populated.
2790 if (behav == MADV_WILLNEED &&
2791 current->wired_count == 0) {
2792 vm_map_pmap_enter(map,
2794 current->protection,
2795 current->object.vm_object,
2797 ptoa(pend - pstart),
2798 MAP_PREFAULT_MADVISE
2802 vm_map_unlock_read(map);
2811 * Sets the inheritance of the specified address
2812 * range in the target map. Inheritance
2813 * affects how the map will be shared with
2814 * child maps at the time of vmspace_fork.
2817 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
2818 vm_inherit_t new_inheritance)
2820 vm_map_entry_t entry;
2821 vm_map_entry_t temp_entry;
2823 switch (new_inheritance) {
2824 case VM_INHERIT_NONE:
2825 case VM_INHERIT_COPY:
2826 case VM_INHERIT_SHARE:
2827 case VM_INHERIT_ZERO:
2830 return (KERN_INVALID_ARGUMENT);
2833 return (KERN_SUCCESS);
2835 VM_MAP_RANGE_CHECK(map, start, end);
2836 if (vm_map_lookup_entry(map, start, &temp_entry)) {
2838 vm_map_clip_start(map, entry, start);
2840 entry = temp_entry->next;
2841 while (entry->start < end) {
2842 vm_map_clip_end(map, entry, end);
2843 if ((entry->eflags & MAP_ENTRY_GUARD) == 0 ||
2844 new_inheritance != VM_INHERIT_ZERO)
2845 entry->inheritance = new_inheritance;
2846 vm_map_simplify_entry(map, entry);
2847 entry = entry->next;
2850 return (KERN_SUCCESS);
2856 * Implements both kernel and user unwiring.
2859 vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
2862 vm_map_entry_t entry, first_entry, tmp_entry;
2863 vm_offset_t saved_start;
2864 unsigned int last_timestamp;
2866 boolean_t need_wakeup, result, user_unwire;
2869 return (KERN_SUCCESS);
2870 user_unwire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
2872 VM_MAP_RANGE_CHECK(map, start, end);
2873 if (!vm_map_lookup_entry(map, start, &first_entry)) {
2874 if (flags & VM_MAP_WIRE_HOLESOK)
2875 first_entry = first_entry->next;
2878 return (KERN_INVALID_ADDRESS);
2881 last_timestamp = map->timestamp;
2882 entry = first_entry;
2883 while (entry->start < end) {
2884 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
2886 * We have not yet clipped the entry.
2888 saved_start = (start >= entry->start) ? start :
2890 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2891 if (vm_map_unlock_and_wait(map, 0)) {
2893 * Allow interruption of user unwiring?
2897 if (last_timestamp+1 != map->timestamp) {
2899 * Look again for the entry because the map was
2900 * modified while it was unlocked.
2901 * Specifically, the entry may have been
2902 * clipped, merged, or deleted.
2904 if (!vm_map_lookup_entry(map, saved_start,
2906 if (flags & VM_MAP_WIRE_HOLESOK)
2907 tmp_entry = tmp_entry->next;
2909 if (saved_start == start) {
2911 * First_entry has been deleted.
2914 return (KERN_INVALID_ADDRESS);
2917 rv = KERN_INVALID_ADDRESS;
2921 if (entry == first_entry)
2922 first_entry = tmp_entry;
2927 last_timestamp = map->timestamp;
2930 vm_map_clip_start(map, entry, start);
2931 vm_map_clip_end(map, entry, end);
2933 * Mark the entry in case the map lock is released. (See
2936 KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 &&
2937 entry->wiring_thread == NULL,
2938 ("owned map entry %p", entry));
2939 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
2940 entry->wiring_thread = curthread;
2942 * Check the map for holes in the specified region.
2943 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
2945 if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
2946 (entry->end < end && entry->next->start > entry->end)) {
2948 rv = KERN_INVALID_ADDRESS;
2952 * If system unwiring, require that the entry is system wired.
2955 vm_map_entry_system_wired_count(entry) == 0) {
2957 rv = KERN_INVALID_ARGUMENT;
2960 entry = entry->next;
2964 need_wakeup = FALSE;
2965 if (first_entry == NULL) {
2966 result = vm_map_lookup_entry(map, start, &first_entry);
2967 if (!result && (flags & VM_MAP_WIRE_HOLESOK))
2968 first_entry = first_entry->next;
2970 KASSERT(result, ("vm_map_unwire: lookup failed"));
2972 for (entry = first_entry; entry->start < end; entry = entry->next) {
2974 * If VM_MAP_WIRE_HOLESOK was specified, an empty
2975 * space in the unwired region could have been mapped
2976 * while the map lock was dropped for draining
2977 * MAP_ENTRY_IN_TRANSITION. Moreover, another thread
2978 * could be simultaneously wiring this new mapping
2979 * entry. Detect these cases and skip any entries
2980 * marked as in transition by us.
2982 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 ||
2983 entry->wiring_thread != curthread) {
2984 KASSERT((flags & VM_MAP_WIRE_HOLESOK) != 0,
2985 ("vm_map_unwire: !HOLESOK and new/changed entry"));
2989 if (rv == KERN_SUCCESS && (!user_unwire ||
2990 (entry->eflags & MAP_ENTRY_USER_WIRED))) {
2991 if (entry->wired_count == 1)
2992 vm_map_entry_unwire(map, entry);
2994 entry->wired_count--;
2996 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2998 KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0,
2999 ("vm_map_unwire: in-transition flag missing %p", entry));
3000 KASSERT(entry->wiring_thread == curthread,
3001 ("vm_map_unwire: alien wire %p", entry));
3002 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
3003 entry->wiring_thread = NULL;
3004 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
3005 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
3008 vm_map_simplify_entry(map, entry);
3017 vm_map_wire_user_count_sub(u_long npages)
3020 atomic_subtract_long(&vm_user_wire_count, npages);
3024 vm_map_wire_user_count_add(u_long npages)
3028 wired = vm_user_wire_count;
3030 if (npages + wired > (u_long)vm_page_max_user_wired)
3032 } while (!atomic_fcmpset_long(&vm_user_wire_count, &wired,
3039 * vm_map_wire_entry_failure:
3041 * Handle a wiring failure on the given entry.
3043 * The map should be locked.
3046 vm_map_wire_entry_failure(vm_map_t map, vm_map_entry_t entry,
3047 vm_offset_t failed_addr)
3050 VM_MAP_ASSERT_LOCKED(map);
3051 KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 &&
3052 entry->wired_count == 1,
3053 ("vm_map_wire_entry_failure: entry %p isn't being wired", entry));
3054 KASSERT(failed_addr < entry->end,
3055 ("vm_map_wire_entry_failure: entry %p was fully wired", entry));
3058 * If any pages at the start of this entry were successfully wired,
3061 if (failed_addr > entry->start) {
3062 pmap_unwire(map->pmap, entry->start, failed_addr);
3063 vm_object_unwire(entry->object.vm_object, entry->offset,
3064 failed_addr - entry->start, PQ_ACTIVE);
3068 * Assign an out-of-range value to represent the failure to wire this
3071 entry->wired_count = -1;
3075 vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end, int flags)
3080 rv = vm_map_wire_locked(map, start, end, flags);
3087 * vm_map_wire_locked:
3089 * Implements both kernel and user wiring. Returns with the map locked,
3090 * the map lock may be dropped.
3093 vm_map_wire_locked(vm_map_t map, vm_offset_t start, vm_offset_t end, int flags)
3095 vm_map_entry_t entry, first_entry, tmp_entry;
3096 vm_offset_t faddr, saved_end, saved_start;
3098 u_int last_timestamp;
3100 boolean_t need_wakeup, result, user_wire;
3103 VM_MAP_ASSERT_LOCKED(map);
3106 return (KERN_SUCCESS);
3108 if (flags & VM_MAP_WIRE_WRITE)
3109 prot |= VM_PROT_WRITE;
3110 user_wire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
3111 VM_MAP_RANGE_CHECK(map, start, end);
3112 if (!vm_map_lookup_entry(map, start, &first_entry)) {
3113 if (flags & VM_MAP_WIRE_HOLESOK)
3114 first_entry = first_entry->next;
3116 return (KERN_INVALID_ADDRESS);
3118 last_timestamp = map->timestamp;
3119 entry = first_entry;
3120 while (entry->start < end) {
3121 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
3123 * We have not yet clipped the entry.
3125 saved_start = (start >= entry->start) ? start :
3127 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
3128 if (vm_map_unlock_and_wait(map, 0)) {
3130 * Allow interruption of user wiring?
3134 if (last_timestamp + 1 != map->timestamp) {
3136 * Look again for the entry because the map was
3137 * modified while it was unlocked.
3138 * Specifically, the entry may have been
3139 * clipped, merged, or deleted.
3141 if (!vm_map_lookup_entry(map, saved_start,
3143 if (flags & VM_MAP_WIRE_HOLESOK)
3144 tmp_entry = tmp_entry->next;
3146 if (saved_start == start) {
3148 * first_entry has been deleted.
3150 return (KERN_INVALID_ADDRESS);
3153 rv = KERN_INVALID_ADDRESS;
3157 if (entry == first_entry)
3158 first_entry = tmp_entry;
3163 last_timestamp = map->timestamp;
3166 vm_map_clip_start(map, entry, start);
3167 vm_map_clip_end(map, entry, end);
3169 * Mark the entry in case the map lock is released. (See
3172 KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 &&
3173 entry->wiring_thread == NULL,
3174 ("owned map entry %p", entry));
3175 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
3176 entry->wiring_thread = curthread;
3177 if ((entry->protection & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0
3178 || (entry->protection & prot) != prot) {
3179 entry->eflags |= MAP_ENTRY_WIRE_SKIPPED;
3180 if ((flags & VM_MAP_WIRE_HOLESOK) == 0) {
3182 rv = KERN_INVALID_ADDRESS;
3187 if (entry->wired_count == 0) {
3188 entry->wired_count++;
3190 npages = atop(entry->end - entry->start);
3191 if (user_wire && !vm_map_wire_user_count_add(npages)) {
3192 vm_map_wire_entry_failure(map, entry,
3195 rv = KERN_RESOURCE_SHORTAGE;
3200 * Release the map lock, relying on the in-transition
3201 * mark. Mark the map busy for fork.
3203 saved_start = entry->start;
3204 saved_end = entry->end;
3208 faddr = saved_start;
3211 * Simulate a fault to get the page and enter
3212 * it into the physical map.
3214 if ((rv = vm_fault(map, faddr,
3215 VM_PROT_NONE, VM_FAULT_WIRE, NULL)) !=
3218 } while ((faddr += PAGE_SIZE) < saved_end);
3221 if (last_timestamp + 1 != map->timestamp) {
3223 * Look again for the entry because the map was
3224 * modified while it was unlocked. The entry
3225 * may have been clipped, but NOT merged or
3228 result = vm_map_lookup_entry(map, saved_start,
3230 KASSERT(result, ("vm_map_wire: lookup failed"));
3231 if (entry == first_entry)
3232 first_entry = tmp_entry;
3236 while (entry->end < saved_end) {
3238 * In case of failure, handle entries
3239 * that were not fully wired here;
3240 * fully wired entries are handled
3243 if (rv != KERN_SUCCESS &&
3245 vm_map_wire_entry_failure(map,
3247 entry = entry->next;
3250 last_timestamp = map->timestamp;
3251 if (rv != KERN_SUCCESS) {
3252 vm_map_wire_entry_failure(map, entry, faddr);
3254 vm_map_wire_user_count_sub(npages);
3258 } else if (!user_wire ||
3259 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
3260 entry->wired_count++;
3263 * Check the map for holes in the specified region.
3264 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
3267 if ((flags & VM_MAP_WIRE_HOLESOK) == 0 &&
3268 entry->end < end && entry->next->start > entry->end) {
3270 rv = KERN_INVALID_ADDRESS;
3273 entry = entry->next;
3277 need_wakeup = FALSE;
3278 if (first_entry == NULL) {
3279 result = vm_map_lookup_entry(map, start, &first_entry);
3280 if (!result && (flags & VM_MAP_WIRE_HOLESOK))
3281 first_entry = first_entry->next;
3283 KASSERT(result, ("vm_map_wire: lookup failed"));
3285 for (entry = first_entry; entry->start < end; entry = entry->next) {
3287 * If VM_MAP_WIRE_HOLESOK was specified, an empty
3288 * space in the unwired region could have been mapped
3289 * while the map lock was dropped for faulting in the
3290 * pages or draining MAP_ENTRY_IN_TRANSITION.
3291 * Moreover, another thread could be simultaneously
3292 * wiring this new mapping entry. Detect these cases
3293 * and skip any entries marked as in transition not by us.
3295 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 ||
3296 entry->wiring_thread != curthread) {
3297 KASSERT((flags & VM_MAP_WIRE_HOLESOK) != 0,
3298 ("vm_map_wire: !HOLESOK and new/changed entry"));
3302 if ((entry->eflags & MAP_ENTRY_WIRE_SKIPPED) != 0)
3303 goto next_entry_done;
3305 if (rv == KERN_SUCCESS) {
3307 entry->eflags |= MAP_ENTRY_USER_WIRED;
3308 } else if (entry->wired_count == -1) {
3310 * Wiring failed on this entry. Thus, unwiring is
3313 entry->wired_count = 0;
3314 } else if (!user_wire ||
3315 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
3317 * Undo the wiring. Wiring succeeded on this entry
3318 * but failed on a later entry.
3320 if (entry->wired_count == 1) {
3321 vm_map_entry_unwire(map, entry);
3323 vm_map_wire_user_count_sub(
3324 atop(entry->end - entry->start));
3326 entry->wired_count--;
3329 KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0,
3330 ("vm_map_wire: in-transition flag missing %p", entry));
3331 KASSERT(entry->wiring_thread == curthread,
3332 ("vm_map_wire: alien wire %p", entry));
3333 entry->eflags &= ~(MAP_ENTRY_IN_TRANSITION |
3334 MAP_ENTRY_WIRE_SKIPPED);
3335 entry->wiring_thread = NULL;
3336 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
3337 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
3340 vm_map_simplify_entry(map, entry);
3350 * Push any dirty cached pages in the address range to their pager.
3351 * If syncio is TRUE, dirty pages are written synchronously.
3352 * If invalidate is TRUE, any cached pages are freed as well.
3354 * If the size of the region from start to end is zero, we are
3355 * supposed to flush all modified pages within the region containing
3356 * start. Unfortunately, a region can be split or coalesced with
3357 * neighboring regions, making it difficult to determine what the
3358 * original region was. Therefore, we approximate this requirement by
3359 * flushing the current region containing start.
3361 * Returns an error if any part of the specified range is not mapped.
3369 boolean_t invalidate)
3371 vm_map_entry_t current;
3372 vm_map_entry_t entry;
3375 vm_ooffset_t offset;
3376 unsigned int last_timestamp;
3379 vm_map_lock_read(map);
3380 VM_MAP_RANGE_CHECK(map, start, end);
3381 if (!vm_map_lookup_entry(map, start, &entry)) {
3382 vm_map_unlock_read(map);
3383 return (KERN_INVALID_ADDRESS);
3384 } else if (start == end) {
3385 start = entry->start;
3389 * Make a first pass to check for user-wired memory and holes.
3391 for (current = entry; current->start < end; current = current->next) {
3392 if (invalidate && (current->eflags & MAP_ENTRY_USER_WIRED)) {
3393 vm_map_unlock_read(map);
3394 return (KERN_INVALID_ARGUMENT);
3396 if (end > current->end &&
3397 current->end != current->next->start) {
3398 vm_map_unlock_read(map);
3399 return (KERN_INVALID_ADDRESS);
3404 pmap_remove(map->pmap, start, end);
3408 * Make a second pass, cleaning/uncaching pages from the indicated
3411 for (current = entry; current->start < end;) {
3412 offset = current->offset + (start - current->start);
3413 size = (end <= current->end ? end : current->end) - start;
3414 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
3416 vm_map_entry_t tentry;
3419 smap = current->object.sub_map;
3420 vm_map_lock_read(smap);
3421 (void) vm_map_lookup_entry(smap, offset, &tentry);
3422 tsize = tentry->end - offset;
3425 object = tentry->object.vm_object;
3426 offset = tentry->offset + (offset - tentry->start);
3427 vm_map_unlock_read(smap);
3429 object = current->object.vm_object;
3431 vm_object_reference(object);
3432 last_timestamp = map->timestamp;
3433 vm_map_unlock_read(map);
3434 if (!vm_object_sync(object, offset, size, syncio, invalidate))
3437 vm_object_deallocate(object);
3438 vm_map_lock_read(map);
3439 if (last_timestamp == map->timestamp ||
3440 !vm_map_lookup_entry(map, start, ¤t))
3441 current = current->next;
3444 vm_map_unlock_read(map);
3445 return (failed ? KERN_FAILURE : KERN_SUCCESS);
3449 * vm_map_entry_unwire: [ internal use only ]
3451 * Make the region specified by this entry pageable.
3453 * The map in question should be locked.
3454 * [This is the reason for this routine's existence.]
3457 vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
3461 VM_MAP_ASSERT_LOCKED(map);
3462 KASSERT(entry->wired_count > 0,
3463 ("vm_map_entry_unwire: entry %p isn't wired", entry));
3465 size = entry->end - entry->start;
3466 if ((entry->eflags & MAP_ENTRY_USER_WIRED) != 0)
3467 vm_map_wire_user_count_sub(atop(size));
3468 pmap_unwire(map->pmap, entry->start, entry->end);
3469 vm_object_unwire(entry->object.vm_object, entry->offset, size,
3471 entry->wired_count = 0;
3475 vm_map_entry_deallocate(vm_map_entry_t entry, boolean_t system_map)
3478 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0)
3479 vm_object_deallocate(entry->object.vm_object);
3480 uma_zfree(system_map ? kmapentzone : mapentzone, entry);
3484 * vm_map_entry_delete: [ internal use only ]
3486 * Deallocate the given entry from the target map.
3489 vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry)
3492 vm_pindex_t offidxstart, offidxend, count, size1;
3495 vm_map_entry_unlink(map, entry, UNLINK_MERGE_NONE);
3496 object = entry->object.vm_object;
3498 if ((entry->eflags & MAP_ENTRY_GUARD) != 0) {
3499 MPASS(entry->cred == NULL);
3500 MPASS((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0);
3501 MPASS(object == NULL);
3502 vm_map_entry_deallocate(entry, map->system_map);
3506 size = entry->end - entry->start;
3509 if (entry->cred != NULL) {
3510 swap_release_by_cred(size, entry->cred);
3511 crfree(entry->cred);
3514 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
3516 KASSERT(entry->cred == NULL || object->cred == NULL ||
3517 (entry->eflags & MAP_ENTRY_NEEDS_COPY),
3518 ("OVERCOMMIT vm_map_entry_delete: both cred %p", entry));
3520 offidxstart = OFF_TO_IDX(entry->offset);
3521 offidxend = offidxstart + count;
3522 VM_OBJECT_WLOCK(object);
3523 if (object->ref_count != 1 && ((object->flags & (OBJ_NOSPLIT |
3524 OBJ_ONEMAPPING)) == OBJ_ONEMAPPING ||
3525 object == kernel_object)) {
3526 vm_object_collapse(object);
3529 * The option OBJPR_NOTMAPPED can be passed here
3530 * because vm_map_delete() already performed
3531 * pmap_remove() on the only mapping to this range
3534 vm_object_page_remove(object, offidxstart, offidxend,
3536 if (object->type == OBJT_SWAP)
3537 swap_pager_freespace(object, offidxstart,
3539 if (offidxend >= object->size &&
3540 offidxstart < object->size) {
3541 size1 = object->size;
3542 object->size = offidxstart;
3543 if (object->cred != NULL) {
3544 size1 -= object->size;
3545 KASSERT(object->charge >= ptoa(size1),
3546 ("object %p charge < 0", object));
3547 swap_release_by_cred(ptoa(size1),
3549 object->charge -= ptoa(size1);
3553 VM_OBJECT_WUNLOCK(object);
3555 entry->object.vm_object = NULL;
3556 if (map->system_map)
3557 vm_map_entry_deallocate(entry, TRUE);
3559 entry->next = curthread->td_map_def_user;
3560 curthread->td_map_def_user = entry;
3565 * vm_map_delete: [ internal use only ]
3567 * Deallocates the given address range from the target
3571 vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end)
3573 vm_map_entry_t entry;
3574 vm_map_entry_t first_entry;
3576 VM_MAP_ASSERT_LOCKED(map);
3578 return (KERN_SUCCESS);
3581 * Find the start of the region, and clip it
3583 if (!vm_map_lookup_entry(map, start, &first_entry))
3584 entry = first_entry->next;
3586 entry = first_entry;
3587 vm_map_clip_start(map, entry, start);
3591 * Step through all entries in this region
3593 while (entry->start < end) {
3594 vm_map_entry_t next;
3597 * Wait for wiring or unwiring of an entry to complete.
3598 * Also wait for any system wirings to disappear on
3601 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 ||
3602 (vm_map_pmap(map) != kernel_pmap &&
3603 vm_map_entry_system_wired_count(entry) != 0)) {
3604 unsigned int last_timestamp;
3605 vm_offset_t saved_start;
3606 vm_map_entry_t tmp_entry;
3608 saved_start = entry->start;
3609 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
3610 last_timestamp = map->timestamp;
3611 (void) vm_map_unlock_and_wait(map, 0);
3613 if (last_timestamp + 1 != map->timestamp) {
3615 * Look again for the entry because the map was
3616 * modified while it was unlocked.
3617 * Specifically, the entry may have been
3618 * clipped, merged, or deleted.
3620 if (!vm_map_lookup_entry(map, saved_start,
3622 entry = tmp_entry->next;
3625 vm_map_clip_start(map, entry,
3631 vm_map_clip_end(map, entry, end);
3636 * Unwire before removing addresses from the pmap; otherwise,
3637 * unwiring will put the entries back in the pmap.
3639 if (entry->wired_count != 0)
3640 vm_map_entry_unwire(map, entry);
3643 * Remove mappings for the pages, but only if the
3644 * mappings could exist. For instance, it does not
3645 * make sense to call pmap_remove() for guard entries.
3647 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0 ||
3648 entry->object.vm_object != NULL)
3649 pmap_remove(map->pmap, entry->start, entry->end);
3651 if (entry->end == map->anon_loc)
3652 map->anon_loc = entry->start;
3655 * Delete the entry only after removing all pmap
3656 * entries pointing to its pages. (Otherwise, its
3657 * page frames may be reallocated, and any modify bits
3658 * will be set in the wrong object!)
3660 vm_map_entry_delete(map, entry);
3663 return (KERN_SUCCESS);
3669 * Remove the given address range from the target map.
3670 * This is the exported form of vm_map_delete.
3673 vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
3678 VM_MAP_RANGE_CHECK(map, start, end);
3679 result = vm_map_delete(map, start, end);
3685 * vm_map_check_protection:
3687 * Assert that the target map allows the specified privilege on the
3688 * entire address region given. The entire region must be allocated.
3690 * WARNING! This code does not and should not check whether the
3691 * contents of the region is accessible. For example a smaller file
3692 * might be mapped into a larger address space.
3694 * NOTE! This code is also called by munmap().
3696 * The map must be locked. A read lock is sufficient.
3699 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
3700 vm_prot_t protection)
3702 vm_map_entry_t entry;
3703 vm_map_entry_t tmp_entry;
3705 if (!vm_map_lookup_entry(map, start, &tmp_entry))
3709 while (start < end) {
3713 if (start < entry->start)
3716 * Check protection associated with entry.
3718 if ((entry->protection & protection) != protection)
3720 /* go to next entry */
3722 entry = entry->next;
3728 * vm_map_copy_entry:
3730 * Copies the contents of the source entry to the destination
3731 * entry. The entries *must* be aligned properly.
3737 vm_map_entry_t src_entry,
3738 vm_map_entry_t dst_entry,
3739 vm_ooffset_t *fork_charge)
3741 vm_object_t src_object;
3742 vm_map_entry_t fake_entry;
3747 VM_MAP_ASSERT_LOCKED(dst_map);
3749 if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
3752 if (src_entry->wired_count == 0 ||
3753 (src_entry->protection & VM_PROT_WRITE) == 0) {
3755 * If the source entry is marked needs_copy, it is already
3758 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0 &&
3759 (src_entry->protection & VM_PROT_WRITE) != 0) {
3760 pmap_protect(src_map->pmap,
3763 src_entry->protection & ~VM_PROT_WRITE);
3767 * Make a copy of the object.
3769 size = src_entry->end - src_entry->start;
3770 if ((src_object = src_entry->object.vm_object) != NULL) {
3771 VM_OBJECT_WLOCK(src_object);
3772 charged = ENTRY_CHARGED(src_entry);
3773 if (src_object->handle == NULL &&
3774 (src_object->type == OBJT_DEFAULT ||
3775 src_object->type == OBJT_SWAP)) {
3776 vm_object_collapse(src_object);
3777 if ((src_object->flags & (OBJ_NOSPLIT |
3778 OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
3779 vm_object_split(src_entry);
3781 src_entry->object.vm_object;
3784 vm_object_reference_locked(src_object);
3785 vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
3786 if (src_entry->cred != NULL &&
3787 !(src_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
3788 KASSERT(src_object->cred == NULL,
3789 ("OVERCOMMIT: vm_map_copy_entry: cred %p",
3791 src_object->cred = src_entry->cred;
3792 src_object->charge = size;
3794 VM_OBJECT_WUNLOCK(src_object);
3795 dst_entry->object.vm_object = src_object;
3797 cred = curthread->td_ucred;
3799 dst_entry->cred = cred;
3800 *fork_charge += size;
3801 if (!(src_entry->eflags &
3802 MAP_ENTRY_NEEDS_COPY)) {
3804 src_entry->cred = cred;
3805 *fork_charge += size;
3808 src_entry->eflags |= MAP_ENTRY_COW |
3809 MAP_ENTRY_NEEDS_COPY;
3810 dst_entry->eflags |= MAP_ENTRY_COW |
3811 MAP_ENTRY_NEEDS_COPY;
3812 dst_entry->offset = src_entry->offset;
3813 if (src_entry->eflags & MAP_ENTRY_WRITECNT) {
3815 * MAP_ENTRY_WRITECNT cannot
3816 * indicate write reference from
3817 * src_entry, since the entry is
3818 * marked as needs copy. Allocate a
3819 * fake entry that is used to
3820 * decrement object->un_pager writecount
3821 * at the appropriate time. Attach
3822 * fake_entry to the deferred list.
3824 fake_entry = vm_map_entry_create(dst_map);
3825 fake_entry->eflags = MAP_ENTRY_WRITECNT;
3826 src_entry->eflags &= ~MAP_ENTRY_WRITECNT;
3827 vm_object_reference(src_object);
3828 fake_entry->object.vm_object = src_object;
3829 fake_entry->start = src_entry->start;
3830 fake_entry->end = src_entry->end;
3831 fake_entry->next = curthread->td_map_def_user;
3832 curthread->td_map_def_user = fake_entry;
3835 pmap_copy(dst_map->pmap, src_map->pmap,
3836 dst_entry->start, dst_entry->end - dst_entry->start,
3839 dst_entry->object.vm_object = NULL;
3840 dst_entry->offset = 0;
3841 if (src_entry->cred != NULL) {
3842 dst_entry->cred = curthread->td_ucred;
3843 crhold(dst_entry->cred);
3844 *fork_charge += size;
3849 * We don't want to make writeable wired pages copy-on-write.
3850 * Immediately copy these pages into the new map by simulating
3851 * page faults. The new pages are pageable.
3853 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry,
3859 * vmspace_map_entry_forked:
3860 * Update the newly-forked vmspace each time a map entry is inherited
3861 * or copied. The values for vm_dsize and vm_tsize are approximate
3862 * (and mostly-obsolete ideas in the face of mmap(2) et al.)
3865 vmspace_map_entry_forked(const struct vmspace *vm1, struct vmspace *vm2,
3866 vm_map_entry_t entry)
3868 vm_size_t entrysize;
3871 if ((entry->eflags & MAP_ENTRY_GUARD) != 0)
3873 entrysize = entry->end - entry->start;
3874 vm2->vm_map.size += entrysize;
3875 if (entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP)) {
3876 vm2->vm_ssize += btoc(entrysize);
3877 } else if (entry->start >= (vm_offset_t)vm1->vm_daddr &&
3878 entry->start < (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)) {
3879 newend = MIN(entry->end,
3880 (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize));
3881 vm2->vm_dsize += btoc(newend - entry->start);
3882 } else if (entry->start >= (vm_offset_t)vm1->vm_taddr &&
3883 entry->start < (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)) {
3884 newend = MIN(entry->end,
3885 (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize));
3886 vm2->vm_tsize += btoc(newend - entry->start);
3892 * Create a new process vmspace structure and vm_map
3893 * based on those of an existing process. The new map
3894 * is based on the old map, according to the inheritance
3895 * values on the regions in that map.
3897 * XXX It might be worth coalescing the entries added to the new vmspace.
3899 * The source map must not be locked.
3902 vmspace_fork(struct vmspace *vm1, vm_ooffset_t *fork_charge)
3904 struct vmspace *vm2;
3905 vm_map_t new_map, old_map;
3906 vm_map_entry_t new_entry, old_entry;
3911 old_map = &vm1->vm_map;
3912 /* Copy immutable fields of vm1 to vm2. */
3913 vm2 = vmspace_alloc(vm_map_min(old_map), vm_map_max(old_map),
3918 vm2->vm_taddr = vm1->vm_taddr;
3919 vm2->vm_daddr = vm1->vm_daddr;
3920 vm2->vm_maxsaddr = vm1->vm_maxsaddr;
3921 vm_map_lock(old_map);
3923 vm_map_wait_busy(old_map);
3924 new_map = &vm2->vm_map;
3925 locked = vm_map_trylock(new_map); /* trylock to silence WITNESS */
3926 KASSERT(locked, ("vmspace_fork: lock failed"));
3928 error = pmap_vmspace_copy(new_map->pmap, old_map->pmap);
3930 sx_xunlock(&old_map->lock);
3931 sx_xunlock(&new_map->lock);
3932 vm_map_process_deferred();
3937 new_map->anon_loc = old_map->anon_loc;
3939 old_entry = old_map->header.next;
3941 while (old_entry != &old_map->header) {
3942 if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
3943 panic("vm_map_fork: encountered a submap");
3945 inh = old_entry->inheritance;
3946 if ((old_entry->eflags & MAP_ENTRY_GUARD) != 0 &&
3947 inh != VM_INHERIT_NONE)
3948 inh = VM_INHERIT_COPY;
3951 case VM_INHERIT_NONE:
3954 case VM_INHERIT_SHARE:
3956 * Clone the entry, creating the shared object if necessary.
3958 object = old_entry->object.vm_object;
3959 if (object == NULL) {
3960 object = vm_object_allocate(OBJT_DEFAULT,
3961 atop(old_entry->end - old_entry->start));
3962 old_entry->object.vm_object = object;
3963 old_entry->offset = 0;
3964 if (old_entry->cred != NULL) {
3965 object->cred = old_entry->cred;
3966 object->charge = old_entry->end -
3968 old_entry->cred = NULL;
3973 * Add the reference before calling vm_object_shadow
3974 * to insure that a shadow object is created.
3976 vm_object_reference(object);
3977 if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3978 vm_object_shadow(&old_entry->object.vm_object,
3980 old_entry->end - old_entry->start);
3981 old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
3982 /* Transfer the second reference too. */
3983 vm_object_reference(
3984 old_entry->object.vm_object);
3987 * As in vm_map_simplify_entry(), the
3988 * vnode lock will not be acquired in
3989 * this call to vm_object_deallocate().
3991 vm_object_deallocate(object);
3992 object = old_entry->object.vm_object;
3994 VM_OBJECT_WLOCK(object);
3995 vm_object_clear_flag(object, OBJ_ONEMAPPING);
3996 if (old_entry->cred != NULL) {
3997 KASSERT(object->cred == NULL, ("vmspace_fork both cred"));
3998 object->cred = old_entry->cred;
3999 object->charge = old_entry->end - old_entry->start;
4000 old_entry->cred = NULL;
4004 * Assert the correct state of the vnode
4005 * v_writecount while the object is locked, to
4006 * not relock it later for the assertion
4009 if (old_entry->eflags & MAP_ENTRY_WRITECNT &&
4010 object->type == OBJT_VNODE) {
4011 KASSERT(((struct vnode *)object->handle)->
4013 ("vmspace_fork: v_writecount %p", object));
4014 KASSERT(object->un_pager.vnp.writemappings > 0,
4015 ("vmspace_fork: vnp.writecount %p",
4018 VM_OBJECT_WUNLOCK(object);
4021 * Clone the entry, referencing the shared object.
4023 new_entry = vm_map_entry_create(new_map);
4024 *new_entry = *old_entry;
4025 new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
4026 MAP_ENTRY_IN_TRANSITION);
4027 new_entry->wiring_thread = NULL;
4028 new_entry->wired_count = 0;
4029 if (new_entry->eflags & MAP_ENTRY_WRITECNT) {
4030 vm_pager_update_writecount(object,
4031 new_entry->start, new_entry->end);
4033 vm_map_entry_set_vnode_text(new_entry, true);
4036 * Insert the entry into the new map -- we know we're
4037 * inserting at the end of the new map.
4039 vm_map_entry_link(new_map, new_entry);
4040 vmspace_map_entry_forked(vm1, vm2, new_entry);
4043 * Update the physical map
4045 pmap_copy(new_map->pmap, old_map->pmap,
4047 (old_entry->end - old_entry->start),
4051 case VM_INHERIT_COPY:
4053 * Clone the entry and link into the map.
4055 new_entry = vm_map_entry_create(new_map);
4056 *new_entry = *old_entry;
4058 * Copied entry is COW over the old object.
4060 new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
4061 MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_WRITECNT);
4062 new_entry->wiring_thread = NULL;
4063 new_entry->wired_count = 0;
4064 new_entry->object.vm_object = NULL;
4065 new_entry->cred = NULL;
4066 vm_map_entry_link(new_map, new_entry);
4067 vmspace_map_entry_forked(vm1, vm2, new_entry);
4068 vm_map_copy_entry(old_map, new_map, old_entry,
4069 new_entry, fork_charge);
4070 vm_map_entry_set_vnode_text(new_entry, true);
4073 case VM_INHERIT_ZERO:
4075 * Create a new anonymous mapping entry modelled from
4078 new_entry = vm_map_entry_create(new_map);
4079 memset(new_entry, 0, sizeof(*new_entry));
4081 new_entry->start = old_entry->start;
4082 new_entry->end = old_entry->end;
4083 new_entry->eflags = old_entry->eflags &
4084 ~(MAP_ENTRY_USER_WIRED | MAP_ENTRY_IN_TRANSITION |
4085 MAP_ENTRY_WRITECNT | MAP_ENTRY_VN_EXEC);
4086 new_entry->protection = old_entry->protection;
4087 new_entry->max_protection = old_entry->max_protection;
4088 new_entry->inheritance = VM_INHERIT_ZERO;
4090 vm_map_entry_link(new_map, new_entry);
4091 vmspace_map_entry_forked(vm1, vm2, new_entry);
4093 new_entry->cred = curthread->td_ucred;
4094 crhold(new_entry->cred);
4095 *fork_charge += (new_entry->end - new_entry->start);
4099 old_entry = old_entry->next;
4102 * Use inlined vm_map_unlock() to postpone handling the deferred
4103 * map entries, which cannot be done until both old_map and
4104 * new_map locks are released.
4106 sx_xunlock(&old_map->lock);
4107 sx_xunlock(&new_map->lock);
4108 vm_map_process_deferred();
4114 * Create a process's stack for exec_new_vmspace(). This function is never
4115 * asked to wire the newly created stack.
4118 vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
4119 vm_prot_t prot, vm_prot_t max, int cow)
4121 vm_size_t growsize, init_ssize;
4125 MPASS((map->flags & MAP_WIREFUTURE) == 0);
4126 growsize = sgrowsiz;
4127 init_ssize = (max_ssize < growsize) ? max_ssize : growsize;
4129 vmemlim = lim_cur(curthread, RLIMIT_VMEM);
4130 /* If we would blow our VMEM resource limit, no go */
4131 if (map->size + init_ssize > vmemlim) {
4135 rv = vm_map_stack_locked(map, addrbos, max_ssize, growsize, prot,
4142 static int stack_guard_page = 1;
4143 SYSCTL_INT(_security_bsd, OID_AUTO, stack_guard_page, CTLFLAG_RWTUN,
4144 &stack_guard_page, 0,
4145 "Specifies the number of guard pages for a stack that grows");
4148 vm_map_stack_locked(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
4149 vm_size_t growsize, vm_prot_t prot, vm_prot_t max, int cow)
4151 vm_map_entry_t new_entry, prev_entry;
4152 vm_offset_t bot, gap_bot, gap_top, top;
4153 vm_size_t init_ssize, sgp;
4157 * The stack orientation is piggybacked with the cow argument.
4158 * Extract it into orient and mask the cow argument so that we
4159 * don't pass it around further.
4161 orient = cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP);
4162 KASSERT(orient != 0, ("No stack grow direction"));
4163 KASSERT(orient != (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP),
4166 if (addrbos < vm_map_min(map) ||
4167 addrbos + max_ssize > vm_map_max(map) ||
4168 addrbos + max_ssize <= addrbos)
4169 return (KERN_INVALID_ADDRESS);
4170 sgp = ((curproc->p_flag2 & P2_STKGAP_DISABLE) != 0 ||
4171 (curproc->p_fctl0 & NT_FREEBSD_FCTL_STKGAP_DISABLE) != 0) ? 0 :
4172 (vm_size_t)stack_guard_page * PAGE_SIZE;
4173 if (sgp >= max_ssize)
4174 return (KERN_INVALID_ARGUMENT);
4176 init_ssize = growsize;
4177 if (max_ssize < init_ssize + sgp)
4178 init_ssize = max_ssize - sgp;
4180 /* If addr is already mapped, no go */
4181 if (vm_map_lookup_entry(map, addrbos, &prev_entry))
4182 return (KERN_NO_SPACE);
4185 * If we can't accommodate max_ssize in the current mapping, no go.
4187 if (prev_entry->next->start < addrbos + max_ssize)
4188 return (KERN_NO_SPACE);
4191 * We initially map a stack of only init_ssize. We will grow as
4192 * needed later. Depending on the orientation of the stack (i.e.
4193 * the grow direction) we either map at the top of the range, the
4194 * bottom of the range or in the middle.
4196 * Note: we would normally expect prot and max to be VM_PROT_ALL,
4197 * and cow to be 0. Possibly we should eliminate these as input
4198 * parameters, and just pass these values here in the insert call.
4200 if (orient == MAP_STACK_GROWS_DOWN) {
4201 bot = addrbos + max_ssize - init_ssize;
4202 top = bot + init_ssize;
4205 } else /* if (orient == MAP_STACK_GROWS_UP) */ {
4207 top = bot + init_ssize;
4209 gap_top = addrbos + max_ssize;
4211 rv = vm_map_insert(map, NULL, 0, bot, top, prot, max, cow);
4212 if (rv != KERN_SUCCESS)
4214 new_entry = prev_entry->next;
4215 KASSERT(new_entry->end == top || new_entry->start == bot,
4216 ("Bad entry start/end for new stack entry"));
4217 KASSERT((orient & MAP_STACK_GROWS_DOWN) == 0 ||
4218 (new_entry->eflags & MAP_ENTRY_GROWS_DOWN) != 0,
4219 ("new entry lacks MAP_ENTRY_GROWS_DOWN"));
4220 KASSERT((orient & MAP_STACK_GROWS_UP) == 0 ||
4221 (new_entry->eflags & MAP_ENTRY_GROWS_UP) != 0,
4222 ("new entry lacks MAP_ENTRY_GROWS_UP"));
4223 if (gap_bot == gap_top)
4224 return (KERN_SUCCESS);
4225 rv = vm_map_insert(map, NULL, 0, gap_bot, gap_top, VM_PROT_NONE,
4226 VM_PROT_NONE, MAP_CREATE_GUARD | (orient == MAP_STACK_GROWS_DOWN ?
4227 MAP_CREATE_STACK_GAP_DN : MAP_CREATE_STACK_GAP_UP));
4228 if (rv == KERN_SUCCESS) {
4230 * Gap can never successfully handle a fault, so
4231 * read-ahead logic is never used for it. Re-use
4232 * next_read of the gap entry to store
4233 * stack_guard_page for vm_map_growstack().
4235 if (orient == MAP_STACK_GROWS_DOWN)
4236 new_entry->prev->next_read = sgp;
4238 new_entry->next->next_read = sgp;
4240 (void)vm_map_delete(map, bot, top);
4246 * Attempts to grow a vm stack entry. Returns KERN_SUCCESS if we
4247 * successfully grow the stack.
4250 vm_map_growstack(vm_map_t map, vm_offset_t addr, vm_map_entry_t gap_entry)
4252 vm_map_entry_t stack_entry;
4256 vm_offset_t gap_end, gap_start, grow_start;
4257 size_t grow_amount, guard, max_grow;
4258 rlim_t lmemlim, stacklim, vmemlim;
4260 bool gap_deleted, grow_down, is_procstack;
4272 * Disallow stack growth when the access is performed by a
4273 * debugger or AIO daemon. The reason is that the wrong
4274 * resource limits are applied.
4276 if (p != initproc && (map != &p->p_vmspace->vm_map ||
4277 p->p_textvp == NULL))
4278 return (KERN_FAILURE);
4280 MPASS(!map->system_map);
4282 lmemlim = lim_cur(curthread, RLIMIT_MEMLOCK);
4283 stacklim = lim_cur(curthread, RLIMIT_STACK);
4284 vmemlim = lim_cur(curthread, RLIMIT_VMEM);
4286 /* If addr is not in a hole for a stack grow area, no need to grow. */
4287 if (gap_entry == NULL && !vm_map_lookup_entry(map, addr, &gap_entry))
4288 return (KERN_FAILURE);
4289 if ((gap_entry->eflags & MAP_ENTRY_GUARD) == 0)
4290 return (KERN_SUCCESS);
4291 if ((gap_entry->eflags & MAP_ENTRY_STACK_GAP_DN) != 0) {
4292 stack_entry = gap_entry->next;
4293 if ((stack_entry->eflags & MAP_ENTRY_GROWS_DOWN) == 0 ||
4294 stack_entry->start != gap_entry->end)
4295 return (KERN_FAILURE);
4296 grow_amount = round_page(stack_entry->start - addr);
4298 } else if ((gap_entry->eflags & MAP_ENTRY_STACK_GAP_UP) != 0) {
4299 stack_entry = gap_entry->prev;
4300 if ((stack_entry->eflags & MAP_ENTRY_GROWS_UP) == 0 ||
4301 stack_entry->end != gap_entry->start)
4302 return (KERN_FAILURE);
4303 grow_amount = round_page(addr + 1 - stack_entry->end);
4306 return (KERN_FAILURE);
4308 guard = ((curproc->p_flag2 & P2_STKGAP_DISABLE) != 0 ||
4309 (curproc->p_fctl0 & NT_FREEBSD_FCTL_STKGAP_DISABLE) != 0) ? 0 :
4310 gap_entry->next_read;
4311 max_grow = gap_entry->end - gap_entry->start;
4312 if (guard > max_grow)
4313 return (KERN_NO_SPACE);
4315 if (grow_amount > max_grow)
4316 return (KERN_NO_SPACE);
4319 * If this is the main process stack, see if we're over the stack
4322 is_procstack = addr >= (vm_offset_t)vm->vm_maxsaddr &&
4323 addr < (vm_offset_t)p->p_sysent->sv_usrstack;
4324 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim))
4325 return (KERN_NO_SPACE);
4330 if (is_procstack && racct_set(p, RACCT_STACK,
4331 ctob(vm->vm_ssize) + grow_amount)) {
4333 return (KERN_NO_SPACE);
4339 grow_amount = roundup(grow_amount, sgrowsiz);
4340 if (grow_amount > max_grow)
4341 grow_amount = max_grow;
4342 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
4343 grow_amount = trunc_page((vm_size_t)stacklim) -
4349 limit = racct_get_available(p, RACCT_STACK);
4351 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > limit))
4352 grow_amount = limit - ctob(vm->vm_ssize);
4355 if (!old_mlock && (map->flags & MAP_WIREFUTURE) != 0) {
4356 if (ptoa(pmap_wired_count(map->pmap)) + grow_amount > lmemlim) {
4363 if (racct_set(p, RACCT_MEMLOCK,
4364 ptoa(pmap_wired_count(map->pmap)) + grow_amount)) {
4374 /* If we would blow our VMEM resource limit, no go */
4375 if (map->size + grow_amount > vmemlim) {
4382 if (racct_set(p, RACCT_VMEM, map->size + grow_amount)) {
4391 if (vm_map_lock_upgrade(map)) {
4393 vm_map_lock_read(map);
4398 grow_start = gap_entry->end - grow_amount;
4399 if (gap_entry->start + grow_amount == gap_entry->end) {
4400 gap_start = gap_entry->start;
4401 gap_end = gap_entry->end;
4402 vm_map_entry_delete(map, gap_entry);
4405 MPASS(gap_entry->start < gap_entry->end - grow_amount);
4406 gap_entry->end -= grow_amount;
4407 vm_map_entry_resize_free(map, gap_entry);
4408 gap_deleted = false;
4410 rv = vm_map_insert(map, NULL, 0, grow_start,
4411 grow_start + grow_amount,
4412 stack_entry->protection, stack_entry->max_protection,
4413 MAP_STACK_GROWS_DOWN);
4414 if (rv != KERN_SUCCESS) {
4416 rv1 = vm_map_insert(map, NULL, 0, gap_start,
4417 gap_end, VM_PROT_NONE, VM_PROT_NONE,
4418 MAP_CREATE_GUARD | MAP_CREATE_STACK_GAP_DN);
4419 MPASS(rv1 == KERN_SUCCESS);
4421 gap_entry->end += grow_amount;
4422 vm_map_entry_resize_free(map, gap_entry);
4426 grow_start = stack_entry->end;
4427 cred = stack_entry->cred;
4428 if (cred == NULL && stack_entry->object.vm_object != NULL)
4429 cred = stack_entry->object.vm_object->cred;
4430 if (cred != NULL && !swap_reserve_by_cred(grow_amount, cred))
4432 /* Grow the underlying object if applicable. */
4433 else if (stack_entry->object.vm_object == NULL ||
4434 vm_object_coalesce(stack_entry->object.vm_object,
4435 stack_entry->offset,
4436 (vm_size_t)(stack_entry->end - stack_entry->start),
4437 (vm_size_t)grow_amount, cred != NULL)) {
4438 if (gap_entry->start + grow_amount == gap_entry->end)
4439 vm_map_entry_delete(map, gap_entry);
4441 gap_entry->start += grow_amount;
4442 stack_entry->end += grow_amount;
4443 map->size += grow_amount;
4444 vm_map_entry_resize_free(map, stack_entry);
4449 if (rv == KERN_SUCCESS && is_procstack)
4450 vm->vm_ssize += btoc(grow_amount);
4453 * Heed the MAP_WIREFUTURE flag if it was set for this process.
4455 if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE) != 0) {
4456 rv = vm_map_wire_locked(map, grow_start,
4457 grow_start + grow_amount,
4458 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
4460 vm_map_lock_downgrade(map);
4464 if (racct_enable && rv != KERN_SUCCESS) {
4466 error = racct_set(p, RACCT_VMEM, map->size);
4467 KASSERT(error == 0, ("decreasing RACCT_VMEM failed"));
4469 error = racct_set(p, RACCT_MEMLOCK,
4470 ptoa(pmap_wired_count(map->pmap)));
4471 KASSERT(error == 0, ("decreasing RACCT_MEMLOCK failed"));
4473 error = racct_set(p, RACCT_STACK, ctob(vm->vm_ssize));
4474 KASSERT(error == 0, ("decreasing RACCT_STACK failed"));
4483 * Unshare the specified VM space for exec. If other processes are
4484 * mapped to it, then create a new one. The new vmspace is null.
4487 vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser)
4489 struct vmspace *oldvmspace = p->p_vmspace;
4490 struct vmspace *newvmspace;
4492 KASSERT((curthread->td_pflags & TDP_EXECVMSPC) == 0,
4493 ("vmspace_exec recursed"));
4494 newvmspace = vmspace_alloc(minuser, maxuser, pmap_pinit);
4495 if (newvmspace == NULL)
4497 newvmspace->vm_swrss = oldvmspace->vm_swrss;
4499 * This code is written like this for prototype purposes. The
4500 * goal is to avoid running down the vmspace here, but let the
4501 * other process's that are still using the vmspace to finally
4502 * run it down. Even though there is little or no chance of blocking
4503 * here, it is a good idea to keep this form for future mods.
4505 PROC_VMSPACE_LOCK(p);
4506 p->p_vmspace = newvmspace;
4507 PROC_VMSPACE_UNLOCK(p);
4508 if (p == curthread->td_proc)
4509 pmap_activate(curthread);
4510 curthread->td_pflags |= TDP_EXECVMSPC;
4515 * Unshare the specified VM space for forcing COW. This
4516 * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
4519 vmspace_unshare(struct proc *p)
4521 struct vmspace *oldvmspace = p->p_vmspace;
4522 struct vmspace *newvmspace;
4523 vm_ooffset_t fork_charge;
4525 if (oldvmspace->vm_refcnt == 1)
4528 newvmspace = vmspace_fork(oldvmspace, &fork_charge);
4529 if (newvmspace == NULL)
4531 if (!swap_reserve_by_cred(fork_charge, p->p_ucred)) {
4532 vmspace_free(newvmspace);
4535 PROC_VMSPACE_LOCK(p);
4536 p->p_vmspace = newvmspace;
4537 PROC_VMSPACE_UNLOCK(p);
4538 if (p == curthread->td_proc)
4539 pmap_activate(curthread);
4540 vmspace_free(oldvmspace);
4547 * Finds the VM object, offset, and
4548 * protection for a given virtual address in the
4549 * specified map, assuming a page fault of the
4552 * Leaves the map in question locked for read; return
4553 * values are guaranteed until a vm_map_lookup_done
4554 * call is performed. Note that the map argument
4555 * is in/out; the returned map must be used in
4556 * the call to vm_map_lookup_done.
4558 * A handle (out_entry) is returned for use in
4559 * vm_map_lookup_done, to make that fast.
4561 * If a lookup is requested with "write protection"
4562 * specified, the map may be changed to perform virtual
4563 * copying operations, although the data referenced will
4567 vm_map_lookup(vm_map_t *var_map, /* IN/OUT */
4569 vm_prot_t fault_typea,
4570 vm_map_entry_t *out_entry, /* OUT */
4571 vm_object_t *object, /* OUT */
4572 vm_pindex_t *pindex, /* OUT */
4573 vm_prot_t *out_prot, /* OUT */
4574 boolean_t *wired) /* OUT */
4576 vm_map_entry_t entry;
4577 vm_map_t map = *var_map;
4579 vm_prot_t fault_type;
4580 vm_object_t eobject;
4586 vm_map_lock_read(map);
4590 * Lookup the faulting address.
4592 if (!vm_map_lookup_entry(map, vaddr, out_entry)) {
4593 vm_map_unlock_read(map);
4594 return (KERN_INVALID_ADDRESS);
4602 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
4603 vm_map_t old_map = map;
4605 *var_map = map = entry->object.sub_map;
4606 vm_map_unlock_read(old_map);
4611 * Check whether this task is allowed to have this page.
4613 prot = entry->protection;
4614 if ((fault_typea & VM_PROT_FAULT_LOOKUP) != 0) {
4615 fault_typea &= ~VM_PROT_FAULT_LOOKUP;
4616 if (prot == VM_PROT_NONE && map != kernel_map &&
4617 (entry->eflags & MAP_ENTRY_GUARD) != 0 &&
4618 (entry->eflags & (MAP_ENTRY_STACK_GAP_DN |
4619 MAP_ENTRY_STACK_GAP_UP)) != 0 &&
4620 vm_map_growstack(map, vaddr, entry) == KERN_SUCCESS)
4621 goto RetryLookupLocked;
4623 fault_type = fault_typea & VM_PROT_ALL;
4624 if ((fault_type & prot) != fault_type || prot == VM_PROT_NONE) {
4625 vm_map_unlock_read(map);
4626 return (KERN_PROTECTION_FAILURE);
4628 KASSERT((prot & VM_PROT_WRITE) == 0 || (entry->eflags &
4629 (MAP_ENTRY_USER_WIRED | MAP_ENTRY_NEEDS_COPY)) !=
4630 (MAP_ENTRY_USER_WIRED | MAP_ENTRY_NEEDS_COPY),
4631 ("entry %p flags %x", entry, entry->eflags));
4632 if ((fault_typea & VM_PROT_COPY) != 0 &&
4633 (entry->max_protection & VM_PROT_WRITE) == 0 &&
4634 (entry->eflags & MAP_ENTRY_COW) == 0) {
4635 vm_map_unlock_read(map);
4636 return (KERN_PROTECTION_FAILURE);
4640 * If this page is not pageable, we have to get it for all possible
4643 *wired = (entry->wired_count != 0);
4645 fault_type = entry->protection;
4646 size = entry->end - entry->start;
4648 * If the entry was copy-on-write, we either ...
4650 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
4652 * If we want to write the page, we may as well handle that
4653 * now since we've got the map locked.
4655 * If we don't need to write the page, we just demote the
4656 * permissions allowed.
4658 if ((fault_type & VM_PROT_WRITE) != 0 ||
4659 (fault_typea & VM_PROT_COPY) != 0) {
4661 * Make a new object, and place it in the object
4662 * chain. Note that no new references have appeared
4663 * -- one just moved from the map to the new
4666 if (vm_map_lock_upgrade(map))
4669 if (entry->cred == NULL) {
4671 * The debugger owner is charged for
4674 cred = curthread->td_ucred;
4676 if (!swap_reserve_by_cred(size, cred)) {
4679 return (KERN_RESOURCE_SHORTAGE);
4683 vm_object_shadow(&entry->object.vm_object,
4684 &entry->offset, size);
4685 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
4686 eobject = entry->object.vm_object;
4687 if (eobject->cred != NULL) {
4689 * The object was not shadowed.
4691 swap_release_by_cred(size, entry->cred);
4692 crfree(entry->cred);
4694 } else if (entry->cred != NULL) {
4695 VM_OBJECT_WLOCK(eobject);
4696 eobject->cred = entry->cred;
4697 eobject->charge = size;
4698 VM_OBJECT_WUNLOCK(eobject);
4702 vm_map_lock_downgrade(map);
4705 * We're attempting to read a copy-on-write page --
4706 * don't allow writes.
4708 prot &= ~VM_PROT_WRITE;
4713 * Create an object if necessary.
4715 if (entry->object.vm_object == NULL &&
4717 if (vm_map_lock_upgrade(map))
4719 entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
4722 if (entry->cred != NULL) {
4723 VM_OBJECT_WLOCK(entry->object.vm_object);
4724 entry->object.vm_object->cred = entry->cred;
4725 entry->object.vm_object->charge = size;
4726 VM_OBJECT_WUNLOCK(entry->object.vm_object);
4729 vm_map_lock_downgrade(map);
4733 * Return the object/offset from this entry. If the entry was
4734 * copy-on-write or empty, it has been fixed up.
4736 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
4737 *object = entry->object.vm_object;
4740 return (KERN_SUCCESS);
4744 * vm_map_lookup_locked:
4746 * Lookup the faulting address. A version of vm_map_lookup that returns
4747 * KERN_FAILURE instead of blocking on map lock or memory allocation.
4750 vm_map_lookup_locked(vm_map_t *var_map, /* IN/OUT */
4752 vm_prot_t fault_typea,
4753 vm_map_entry_t *out_entry, /* OUT */
4754 vm_object_t *object, /* OUT */
4755 vm_pindex_t *pindex, /* OUT */
4756 vm_prot_t *out_prot, /* OUT */
4757 boolean_t *wired) /* OUT */
4759 vm_map_entry_t entry;
4760 vm_map_t map = *var_map;
4762 vm_prot_t fault_type = fault_typea;
4765 * Lookup the faulting address.
4767 if (!vm_map_lookup_entry(map, vaddr, out_entry))
4768 return (KERN_INVALID_ADDRESS);
4773 * Fail if the entry refers to a submap.
4775 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
4776 return (KERN_FAILURE);
4779 * Check whether this task is allowed to have this page.
4781 prot = entry->protection;
4782 fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
4783 if ((fault_type & prot) != fault_type)
4784 return (KERN_PROTECTION_FAILURE);
4787 * If this page is not pageable, we have to get it for all possible
4790 *wired = (entry->wired_count != 0);
4792 fault_type = entry->protection;
4794 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
4796 * Fail if the entry was copy-on-write for a write fault.
4798 if (fault_type & VM_PROT_WRITE)
4799 return (KERN_FAILURE);
4801 * We're attempting to read a copy-on-write page --
4802 * don't allow writes.
4804 prot &= ~VM_PROT_WRITE;
4808 * Fail if an object should be created.
4810 if (entry->object.vm_object == NULL && !map->system_map)
4811 return (KERN_FAILURE);
4814 * Return the object/offset from this entry. If the entry was
4815 * copy-on-write or empty, it has been fixed up.
4817 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
4818 *object = entry->object.vm_object;
4821 return (KERN_SUCCESS);
4825 * vm_map_lookup_done:
4827 * Releases locks acquired by a vm_map_lookup
4828 * (according to the handle returned by that lookup).
4831 vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry)
4834 * Unlock the main-level map
4836 vm_map_unlock_read(map);
4840 vm_map_max_KBI(const struct vm_map *map)
4843 return (vm_map_max(map));
4847 vm_map_min_KBI(const struct vm_map *map)
4850 return (vm_map_min(map));
4854 vm_map_pmap_KBI(vm_map_t map)
4860 #include "opt_ddb.h"
4862 #include <sys/kernel.h>
4864 #include <ddb/ddb.h>
4867 vm_map_print(vm_map_t map)
4869 vm_map_entry_t entry;
4871 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
4873 (void *)map->pmap, map->nentries, map->timestamp);
4876 for (entry = map->header.next; entry != &map->header;
4877 entry = entry->next) {
4878 db_iprintf("map entry %p: start=%p, end=%p, eflags=%#x, \n",
4879 (void *)entry, (void *)entry->start, (void *)entry->end,
4882 static char *inheritance_name[4] =
4883 {"share", "copy", "none", "donate_copy"};
4885 db_iprintf(" prot=%x/%x/%s",
4887 entry->max_protection,
4888 inheritance_name[(int)(unsigned char)entry->inheritance]);
4889 if (entry->wired_count != 0)
4890 db_printf(", wired");
4892 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
4893 db_printf(", share=%p, offset=0x%jx\n",
4894 (void *)entry->object.sub_map,
4895 (uintmax_t)entry->offset);
4896 if ((entry->prev == &map->header) ||
4897 (entry->prev->object.sub_map !=
4898 entry->object.sub_map)) {
4900 vm_map_print((vm_map_t)entry->object.sub_map);
4904 if (entry->cred != NULL)
4905 db_printf(", ruid %d", entry->cred->cr_ruid);
4906 db_printf(", object=%p, offset=0x%jx",
4907 (void *)entry->object.vm_object,
4908 (uintmax_t)entry->offset);
4909 if (entry->object.vm_object && entry->object.vm_object->cred)
4910 db_printf(", obj ruid %d charge %jx",
4911 entry->object.vm_object->cred->cr_ruid,
4912 (uintmax_t)entry->object.vm_object->charge);
4913 if (entry->eflags & MAP_ENTRY_COW)
4914 db_printf(", copy (%s)",
4915 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
4918 if ((entry->prev == &map->header) ||
4919 (entry->prev->object.vm_object !=
4920 entry->object.vm_object)) {
4922 vm_object_print((db_expr_t)(intptr_t)
4923 entry->object.vm_object,
4932 DB_SHOW_COMMAND(map, map)
4936 db_printf("usage: show map <addr>\n");
4939 vm_map_print((vm_map_t)addr);
4942 DB_SHOW_COMMAND(procvm, procvm)
4947 p = db_lookup_proc(addr);
4952 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
4953 (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
4954 (void *)vmspace_pmap(p->p_vmspace));
4956 vm_map_print((vm_map_t)&p->p_vmspace->vm_map);