2 * SPDX-License-Identifier: (BSD-3-Clause AND MIT-CMU)
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * The Mach Operating System project at Carnegie-Mellon University.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * from: @(#)vm_map.c 8.3 (Berkeley) 1/12/94
37 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
38 * All rights reserved.
40 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
42 * Permission to use, copy, modify and distribute this software and
43 * its documentation is hereby granted, provided that both the copyright
44 * notice and this permission notice appear in all copies of the
45 * software, derivative works or modified versions, and any portions
46 * thereof, and that both notices appear in supporting documentation.
48 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
49 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
50 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
52 * Carnegie Mellon requests users of this software to return to
54 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
55 * School of Computer Science
56 * Carnegie Mellon University
57 * Pittsburgh PA 15213-3890
59 * any improvements or extensions that they make and grant Carnegie the
60 * rights to redistribute these changes.
64 * Virtual memory mapping module.
67 #include <sys/cdefs.h>
68 __FBSDID("$FreeBSD$");
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/kernel.h>
75 #include <sys/mutex.h>
77 #include <sys/vmmeter.h>
79 #include <sys/vnode.h>
80 #include <sys/racct.h>
81 #include <sys/resourcevar.h>
82 #include <sys/rwlock.h>
84 #include <sys/sysctl.h>
85 #include <sys/sysent.h>
89 #include <vm/vm_param.h>
91 #include <vm/vm_map.h>
92 #include <vm/vm_page.h>
93 #include <vm/vm_pageout.h>
94 #include <vm/vm_object.h>
95 #include <vm/vm_pager.h>
96 #include <vm/vm_kern.h>
97 #include <vm/vm_extern.h>
98 #include <vm/vnode_pager.h>
99 #include <vm/swap_pager.h>
103 * Virtual memory maps provide for the mapping, protection,
104 * and sharing of virtual memory objects. In addition,
105 * this module provides for an efficient virtual copy of
106 * memory from one map to another.
108 * Synchronization is required prior to most operations.
110 * Maps consist of an ordered doubly-linked list of simple
111 * entries; a self-adjusting binary search tree of these
112 * entries is used to speed up lookups.
114 * Since portions of maps are specified by start/end addresses,
115 * which may not align with existing map entries, all
116 * routines merely "clip" entries to these start/end values.
117 * [That is, an entry is split into two, bordering at a
118 * start or end value.] Note that these clippings may not
119 * always be necessary (as the two resulting entries are then
120 * not changed); however, the clipping is done for convenience.
122 * As mentioned above, virtual copy operations are performed
123 * by copying VM object references from one map to
124 * another, and then marking both regions as copy-on-write.
127 static struct mtx map_sleep_mtx;
128 static uma_zone_t mapentzone;
129 static uma_zone_t kmapentzone;
130 static uma_zone_t mapzone;
131 static uma_zone_t vmspace_zone;
132 static int vmspace_zinit(void *mem, int size, int flags);
133 static int vm_map_zinit(void *mem, int ize, int flags);
134 static void _vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min,
136 static void vm_map_entry_deallocate(vm_map_entry_t entry, boolean_t system_map);
137 static void vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry);
138 static void vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry);
139 static int vm_map_growstack(vm_map_t map, vm_offset_t addr,
140 vm_map_entry_t gap_entry);
141 static void vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
142 vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags);
144 static void vm_map_zdtor(void *mem, int size, void *arg);
145 static void vmspace_zdtor(void *mem, int size, void *arg);
147 static int vm_map_stack_locked(vm_map_t map, vm_offset_t addrbos,
148 vm_size_t max_ssize, vm_size_t growsize, vm_prot_t prot, vm_prot_t max,
150 static void vm_map_wire_entry_failure(vm_map_t map, vm_map_entry_t entry,
151 vm_offset_t failed_addr);
153 #define ENTRY_CHARGED(e) ((e)->cred != NULL || \
154 ((e)->object.vm_object != NULL && (e)->object.vm_object->cred != NULL && \
155 !((e)->eflags & MAP_ENTRY_NEEDS_COPY)))
158 * PROC_VMSPACE_{UN,}LOCK() can be a noop as long as vmspaces are type
161 #define PROC_VMSPACE_LOCK(p) do { } while (0)
162 #define PROC_VMSPACE_UNLOCK(p) do { } while (0)
165 * VM_MAP_RANGE_CHECK: [ internal use only ]
167 * Asserts that the starting and ending region
168 * addresses fall within the valid range of the map.
170 #define VM_MAP_RANGE_CHECK(map, start, end) \
172 if (start < vm_map_min(map)) \
173 start = vm_map_min(map); \
174 if (end > vm_map_max(map)) \
175 end = vm_map_max(map); \
183 * Initialize the vm_map module. Must be called before
184 * any other vm_map routines.
186 * Map and entry structures are allocated from the general
187 * purpose memory pool with some exceptions:
189 * - The kernel map and kmem submap are allocated statically.
190 * - Kernel map entries are allocated out of a static pool.
192 * These restrictions are necessary since malloc() uses the
193 * maps and requires map entries.
199 mtx_init(&map_sleep_mtx, "vm map sleep mutex", NULL, MTX_DEF);
200 mapzone = uma_zcreate("MAP", sizeof(struct vm_map), NULL,
206 vm_map_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
207 uma_prealloc(mapzone, MAX_KMAP);
208 kmapentzone = uma_zcreate("KMAP ENTRY", sizeof(struct vm_map_entry),
209 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
210 UMA_ZONE_MTXCLASS | UMA_ZONE_VM);
211 mapentzone = uma_zcreate("MAP ENTRY", sizeof(struct vm_map_entry),
212 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
213 vmspace_zone = uma_zcreate("VMSPACE", sizeof(struct vmspace), NULL,
219 vmspace_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
223 vmspace_zinit(void *mem, int size, int flags)
227 vm = (struct vmspace *)mem;
229 vm->vm_map.pmap = NULL;
230 (void)vm_map_zinit(&vm->vm_map, sizeof(vm->vm_map), flags);
231 PMAP_LOCK_INIT(vmspace_pmap(vm));
236 vm_map_zinit(void *mem, int size, int flags)
241 memset(map, 0, sizeof(*map));
242 mtx_init(&map->system_mtx, "vm map (system)", NULL, MTX_DEF | MTX_DUPOK);
243 sx_init(&map->lock, "vm map (user)");
249 vmspace_zdtor(void *mem, int size, void *arg)
253 vm = (struct vmspace *)mem;
255 vm_map_zdtor(&vm->vm_map, sizeof(vm->vm_map), arg);
258 vm_map_zdtor(void *mem, int size, void *arg)
263 KASSERT(map->nentries == 0,
264 ("map %p nentries == %d on free.",
265 map, map->nentries));
266 KASSERT(map->size == 0,
267 ("map %p size == %lu on free.",
268 map, (unsigned long)map->size));
270 #endif /* INVARIANTS */
273 * Allocate a vmspace structure, including a vm_map and pmap,
274 * and initialize those structures. The refcnt is set to 1.
276 * If 'pinit' is NULL then the embedded pmap is initialized via pmap_pinit().
279 vmspace_alloc(vm_offset_t min, vm_offset_t max, pmap_pinit_t pinit)
283 vm = uma_zalloc(vmspace_zone, M_WAITOK);
284 KASSERT(vm->vm_map.pmap == NULL, ("vm_map.pmap must be NULL"));
285 if (!pinit(vmspace_pmap(vm))) {
286 uma_zfree(vmspace_zone, vm);
289 CTR1(KTR_VM, "vmspace_alloc: %p", vm);
290 _vm_map_init(&vm->vm_map, vmspace_pmap(vm), min, max);
305 vmspace_container_reset(struct proc *p)
309 racct_set(p, RACCT_DATA, 0);
310 racct_set(p, RACCT_STACK, 0);
311 racct_set(p, RACCT_RSS, 0);
312 racct_set(p, RACCT_MEMLOCK, 0);
313 racct_set(p, RACCT_VMEM, 0);
319 vmspace_dofree(struct vmspace *vm)
322 CTR1(KTR_VM, "vmspace_free: %p", vm);
325 * Make sure any SysV shm is freed, it might not have been in
331 * Lock the map, to wait out all other references to it.
332 * Delete all of the mappings and pages they hold, then call
333 * the pmap module to reclaim anything left.
335 (void)vm_map_remove(&vm->vm_map, vm_map_min(&vm->vm_map),
336 vm_map_max(&vm->vm_map));
338 pmap_release(vmspace_pmap(vm));
339 vm->vm_map.pmap = NULL;
340 uma_zfree(vmspace_zone, vm);
344 vmspace_free(struct vmspace *vm)
347 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
348 "vmspace_free() called");
350 if (vm->vm_refcnt == 0)
351 panic("vmspace_free: attempt to free already freed vmspace");
353 if (atomic_fetchadd_int(&vm->vm_refcnt, -1) == 1)
358 vmspace_exitfree(struct proc *p)
362 PROC_VMSPACE_LOCK(p);
365 PROC_VMSPACE_UNLOCK(p);
366 KASSERT(vm == &vmspace0, ("vmspace_exitfree: wrong vmspace"));
371 vmspace_exit(struct thread *td)
378 * Release user portion of address space.
379 * This releases references to vnodes,
380 * which could cause I/O if the file has been unlinked.
381 * Need to do this early enough that we can still sleep.
383 * The last exiting process to reach this point releases as
384 * much of the environment as it can. vmspace_dofree() is the
385 * slower fallback in case another process had a temporary
386 * reference to the vmspace.
391 atomic_add_int(&vmspace0.vm_refcnt, 1);
392 refcnt = vm->vm_refcnt;
394 if (refcnt > 1 && p->p_vmspace != &vmspace0) {
395 /* Switch now since other proc might free vmspace */
396 PROC_VMSPACE_LOCK(p);
397 p->p_vmspace = &vmspace0;
398 PROC_VMSPACE_UNLOCK(p);
401 } while (!atomic_fcmpset_int(&vm->vm_refcnt, &refcnt, refcnt - 1));
403 if (p->p_vmspace != vm) {
404 /* vmspace not yet freed, switch back */
405 PROC_VMSPACE_LOCK(p);
407 PROC_VMSPACE_UNLOCK(p);
410 pmap_remove_pages(vmspace_pmap(vm));
411 /* Switch now since this proc will free vmspace */
412 PROC_VMSPACE_LOCK(p);
413 p->p_vmspace = &vmspace0;
414 PROC_VMSPACE_UNLOCK(p);
420 vmspace_container_reset(p);
424 /* Acquire reference to vmspace owned by another process. */
427 vmspace_acquire_ref(struct proc *p)
432 PROC_VMSPACE_LOCK(p);
435 PROC_VMSPACE_UNLOCK(p);
438 refcnt = vm->vm_refcnt;
440 if (refcnt <= 0) { /* Avoid 0->1 transition */
441 PROC_VMSPACE_UNLOCK(p);
444 } while (!atomic_fcmpset_int(&vm->vm_refcnt, &refcnt, refcnt + 1));
445 if (vm != p->p_vmspace) {
446 PROC_VMSPACE_UNLOCK(p);
450 PROC_VMSPACE_UNLOCK(p);
455 * Switch between vmspaces in an AIO kernel process.
457 * The new vmspace is either the vmspace of a user process obtained
458 * from an active AIO request or the initial vmspace of the AIO kernel
459 * process (when it is idling). Because user processes will block to
460 * drain any active AIO requests before proceeding in exit() or
461 * execve(), the reference count for vmspaces from AIO requests can
462 * never be 0. Similarly, AIO kernel processes hold an extra
463 * reference on their initial vmspace for the life of the process. As
464 * a result, the 'newvm' vmspace always has a non-zero reference
465 * count. This permits an additional reference on 'newvm' to be
466 * acquired via a simple atomic increment rather than the loop in
467 * vmspace_acquire_ref() above.
470 vmspace_switch_aio(struct vmspace *newvm)
472 struct vmspace *oldvm;
474 /* XXX: Need some way to assert that this is an aio daemon. */
476 KASSERT(newvm->vm_refcnt > 0,
477 ("vmspace_switch_aio: newvm unreferenced"));
479 oldvm = curproc->p_vmspace;
484 * Point to the new address space and refer to it.
486 curproc->p_vmspace = newvm;
487 atomic_add_int(&newvm->vm_refcnt, 1);
489 /* Activate the new mapping. */
490 pmap_activate(curthread);
496 _vm_map_lock(vm_map_t map, const char *file, int line)
500 mtx_lock_flags_(&map->system_mtx, 0, file, line);
502 sx_xlock_(&map->lock, file, line);
507 vm_map_entry_set_vnode_text(vm_map_entry_t entry, bool add)
509 vm_object_t object, object1;
512 if ((entry->eflags & MAP_ENTRY_VN_EXEC) == 0)
514 KASSERT((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0,
515 ("Submap with execs"));
516 object = entry->object.vm_object;
517 KASSERT(object != NULL, ("No object for text, entry %p", entry));
518 VM_OBJECT_RLOCK(object);
519 while ((object1 = object->backing_object) != NULL) {
520 VM_OBJECT_RLOCK(object1);
521 VM_OBJECT_RUNLOCK(object);
526 if (object->type == OBJT_DEAD) {
528 * For OBJT_DEAD objects, v_writecount was handled in
529 * vnode_pager_dealloc().
531 } else if (object->type == OBJT_VNODE) {
533 } else if (object->type == OBJT_SWAP) {
534 KASSERT((object->flags & OBJ_TMPFS_NODE) != 0,
535 ("vm_map_entry_set_vnode_text: swap and !TMPFS "
536 "entry %p, object %p, add %d", entry, object, add));
538 * Tmpfs VREG node, which was reclaimed, has
539 * OBJ_TMPFS_NODE flag set, but not OBJ_TMPFS. In
540 * this case there is no v_writecount to adjust.
542 if ((object->flags & OBJ_TMPFS) != 0)
543 vp = object->un_pager.swp.swp_tmpfs;
546 ("vm_map_entry_set_vnode_text: wrong object type, "
547 "entry %p, object %p, add %d", entry, object, add));
551 VOP_SET_TEXT_CHECKED(vp);
553 VOP_UNSET_TEXT_CHECKED(vp);
555 VM_OBJECT_RUNLOCK(object);
559 vm_map_process_deferred(void)
562 vm_map_entry_t entry, next;
566 entry = td->td_map_def_user;
567 td->td_map_def_user = NULL;
568 while (entry != NULL) {
570 MPASS((entry->eflags & (MAP_ENTRY_VN_WRITECNT |
571 MAP_ENTRY_VN_EXEC)) != (MAP_ENTRY_VN_WRITECNT |
573 if ((entry->eflags & MAP_ENTRY_VN_WRITECNT) != 0) {
575 * Decrement the object's writemappings and
576 * possibly the vnode's v_writecount.
578 KASSERT((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0,
579 ("Submap with writecount"));
580 object = entry->object.vm_object;
581 KASSERT(object != NULL, ("No object for writecount"));
582 vnode_pager_release_writecount(object, entry->start,
585 vm_map_entry_set_vnode_text(entry, false);
586 vm_map_entry_deallocate(entry, FALSE);
592 _vm_map_unlock(vm_map_t map, const char *file, int line)
596 mtx_unlock_flags_(&map->system_mtx, 0, file, line);
598 sx_xunlock_(&map->lock, file, line);
599 vm_map_process_deferred();
604 _vm_map_lock_read(vm_map_t map, const char *file, int line)
608 mtx_lock_flags_(&map->system_mtx, 0, file, line);
610 sx_slock_(&map->lock, file, line);
614 _vm_map_unlock_read(vm_map_t map, const char *file, int line)
618 mtx_unlock_flags_(&map->system_mtx, 0, file, line);
620 sx_sunlock_(&map->lock, file, line);
621 vm_map_process_deferred();
626 _vm_map_trylock(vm_map_t map, const char *file, int line)
630 error = map->system_map ?
631 !mtx_trylock_flags_(&map->system_mtx, 0, file, line) :
632 !sx_try_xlock_(&map->lock, file, line);
639 _vm_map_trylock_read(vm_map_t map, const char *file, int line)
643 error = map->system_map ?
644 !mtx_trylock_flags_(&map->system_mtx, 0, file, line) :
645 !sx_try_slock_(&map->lock, file, line);
650 * _vm_map_lock_upgrade: [ internal use only ]
652 * Tries to upgrade a read (shared) lock on the specified map to a write
653 * (exclusive) lock. Returns the value "0" if the upgrade succeeds and a
654 * non-zero value if the upgrade fails. If the upgrade fails, the map is
655 * returned without a read or write lock held.
657 * Requires that the map be read locked.
660 _vm_map_lock_upgrade(vm_map_t map, const char *file, int line)
662 unsigned int last_timestamp;
664 if (map->system_map) {
665 mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
667 if (!sx_try_upgrade_(&map->lock, file, line)) {
668 last_timestamp = map->timestamp;
669 sx_sunlock_(&map->lock, file, line);
670 vm_map_process_deferred();
672 * If the map's timestamp does not change while the
673 * map is unlocked, then the upgrade succeeds.
675 sx_xlock_(&map->lock, file, line);
676 if (last_timestamp != map->timestamp) {
677 sx_xunlock_(&map->lock, file, line);
687 _vm_map_lock_downgrade(vm_map_t map, const char *file, int line)
690 if (map->system_map) {
691 mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
693 sx_downgrade_(&map->lock, file, line);
699 * Returns a non-zero value if the caller holds a write (exclusive) lock
700 * on the specified map and the value "0" otherwise.
703 vm_map_locked(vm_map_t map)
707 return (mtx_owned(&map->system_mtx));
709 return (sx_xlocked(&map->lock));
714 _vm_map_assert_locked(vm_map_t map, const char *file, int line)
718 mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
720 sx_assert_(&map->lock, SA_XLOCKED, file, line);
723 #define VM_MAP_ASSERT_LOCKED(map) \
724 _vm_map_assert_locked(map, LOCK_FILE, LOCK_LINE)
727 static int enable_vmmap_check = 1;
729 static int enable_vmmap_check = 0;
731 SYSCTL_INT(_debug, OID_AUTO, vmmap_check, CTLFLAG_RWTUN,
732 &enable_vmmap_check, 0, "Enable vm map consistency checking");
735 _vm_map_assert_consistent(vm_map_t map)
737 vm_map_entry_t entry;
738 vm_map_entry_t child;
739 vm_size_t max_left, max_right;
741 if (!enable_vmmap_check)
744 for (entry = map->header.next; entry != &map->header;
745 entry = entry->next) {
746 KASSERT(entry->prev->end <= entry->start,
747 ("map %p prev->end = %jx, start = %jx", map,
748 (uintmax_t)entry->prev->end, (uintmax_t)entry->start));
749 KASSERT(entry->start < entry->end,
750 ("map %p start = %jx, end = %jx", map,
751 (uintmax_t)entry->start, (uintmax_t)entry->end));
752 KASSERT(entry->end <= entry->next->start,
753 ("map %p end = %jx, next->start = %jx", map,
754 (uintmax_t)entry->end, (uintmax_t)entry->next->start));
755 KASSERT(entry->left == NULL ||
756 entry->left->start < entry->start,
757 ("map %p left->start = %jx, start = %jx", map,
758 (uintmax_t)entry->left->start, (uintmax_t)entry->start));
759 KASSERT(entry->right == NULL ||
760 entry->start < entry->right->start,
761 ("map %p start = %jx, right->start = %jx", map,
762 (uintmax_t)entry->start, (uintmax_t)entry->right->start));
764 max_left = (child != NULL) ? child->max_free :
765 entry->start - entry->prev->end;
766 child = entry->right;
767 max_right = (child != NULL) ? child->max_free :
768 entry->next->start - entry->end;
769 KASSERT(entry->max_free == MAX(max_left, max_right),
770 ("map %p max = %jx, max_left = %jx, max_right = %jx", map,
771 (uintmax_t)entry->max_free,
772 (uintmax_t)max_left, (uintmax_t)max_right));
776 #define VM_MAP_ASSERT_CONSISTENT(map) \
777 _vm_map_assert_consistent(map)
779 #define VM_MAP_ASSERT_LOCKED(map)
780 #define VM_MAP_ASSERT_CONSISTENT(map)
781 #endif /* INVARIANTS */
784 * _vm_map_unlock_and_wait:
786 * Atomically releases the lock on the specified map and puts the calling
787 * thread to sleep. The calling thread will remain asleep until either
788 * vm_map_wakeup() is performed on the map or the specified timeout is
791 * WARNING! This function does not perform deferred deallocations of
792 * objects and map entries. Therefore, the calling thread is expected to
793 * reacquire the map lock after reawakening and later perform an ordinary
794 * unlock operation, such as vm_map_unlock(), before completing its
795 * operation on the map.
798 _vm_map_unlock_and_wait(vm_map_t map, int timo, const char *file, int line)
801 mtx_lock(&map_sleep_mtx);
803 mtx_unlock_flags_(&map->system_mtx, 0, file, line);
805 sx_xunlock_(&map->lock, file, line);
806 return (msleep(&map->root, &map_sleep_mtx, PDROP | PVM, "vmmaps",
813 * Awaken any threads that have slept on the map using
814 * vm_map_unlock_and_wait().
817 vm_map_wakeup(vm_map_t map)
821 * Acquire and release map_sleep_mtx to prevent a wakeup()
822 * from being performed (and lost) between the map unlock
823 * and the msleep() in _vm_map_unlock_and_wait().
825 mtx_lock(&map_sleep_mtx);
826 mtx_unlock(&map_sleep_mtx);
831 vm_map_busy(vm_map_t map)
834 VM_MAP_ASSERT_LOCKED(map);
839 vm_map_unbusy(vm_map_t map)
842 VM_MAP_ASSERT_LOCKED(map);
843 KASSERT(map->busy, ("vm_map_unbusy: not busy"));
844 if (--map->busy == 0 && (map->flags & MAP_BUSY_WAKEUP)) {
845 vm_map_modflags(map, 0, MAP_BUSY_WAKEUP);
851 vm_map_wait_busy(vm_map_t map)
854 VM_MAP_ASSERT_LOCKED(map);
856 vm_map_modflags(map, MAP_BUSY_WAKEUP, 0);
858 msleep(&map->busy, &map->system_mtx, 0, "mbusy", 0);
860 sx_sleep(&map->busy, &map->lock, 0, "mbusy", 0);
866 vmspace_resident_count(struct vmspace *vmspace)
868 return pmap_resident_count(vmspace_pmap(vmspace));
874 * Creates and returns a new empty VM map with
875 * the given physical map structure, and having
876 * the given lower and upper address bounds.
879 vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max)
883 result = uma_zalloc(mapzone, M_WAITOK);
884 CTR1(KTR_VM, "vm_map_create: %p", result);
885 _vm_map_init(result, pmap, min, max);
890 * Initialize an existing vm_map structure
891 * such as that in the vmspace structure.
894 _vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min, vm_offset_t max)
897 map->header.next = map->header.prev = &map->header;
898 map->header.eflags = MAP_ENTRY_HEADER;
899 map->needs_wakeup = FALSE;
902 map->header.end = min;
903 map->header.start = max;
912 vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min, vm_offset_t max)
915 _vm_map_init(map, pmap, min, max);
916 mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
917 sx_init(&map->lock, "user map");
921 * vm_map_entry_dispose: [ internal use only ]
923 * Inverse of vm_map_entry_create.
926 vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry)
928 uma_zfree(map->system_map ? kmapentzone : mapentzone, entry);
932 * vm_map_entry_create: [ internal use only ]
934 * Allocates a VM map entry for insertion.
935 * No entry fields are filled in.
937 static vm_map_entry_t
938 vm_map_entry_create(vm_map_t map)
940 vm_map_entry_t new_entry;
943 new_entry = uma_zalloc(kmapentzone, M_NOWAIT);
945 new_entry = uma_zalloc(mapentzone, M_WAITOK);
946 if (new_entry == NULL)
947 panic("vm_map_entry_create: kernel resources exhausted");
952 * vm_map_entry_set_behavior:
954 * Set the expected access behavior, either normal, random, or
958 vm_map_entry_set_behavior(vm_map_entry_t entry, u_char behavior)
960 entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) |
961 (behavior & MAP_ENTRY_BEHAV_MASK);
965 * vm_map_entry_max_free_{left,right}:
967 * Compute the size of the largest free gap between two entries,
968 * one the root of a tree and the other the ancestor of that root
969 * that is the least or greatest ancestor found on the search path.
971 static inline vm_size_t
972 vm_map_entry_max_free_left(vm_map_entry_t root, vm_map_entry_t left_ancestor)
975 return (root->left != NULL ?
976 root->left->max_free : root->start - left_ancestor->end);
979 static inline vm_size_t
980 vm_map_entry_max_free_right(vm_map_entry_t root, vm_map_entry_t right_ancestor)
983 return (root->right != NULL ?
984 root->right->max_free : right_ancestor->start - root->end);
987 #define SPLAY_LEFT_STEP(root, y, rlist, test) do { \
988 vm_size_t max_free; \
991 * Infer root->right->max_free == root->max_free when \
992 * y->max_free < root->max_free || root->max_free == 0. \
993 * Otherwise, look right to find it. \
996 max_free = root->max_free; \
997 KASSERT(max_free >= vm_map_entry_max_free_right(root, rlist), \
998 ("%s: max_free invariant fails", __func__)); \
999 if (y == NULL ? max_free > 0 : max_free - 1 < y->max_free) \
1000 max_free = vm_map_entry_max_free_right(root, rlist); \
1001 if (y != NULL && (test)) { \
1002 /* Rotate right and make y root. */ \
1003 root->left = y->right; \
1005 if (max_free < y->max_free) \
1006 root->max_free = max_free = MAX(max_free, \
1007 vm_map_entry_max_free_left(root, y)); \
1011 /* Copy right->max_free. Put root on rlist. */ \
1012 root->max_free = max_free; \
1013 KASSERT(max_free == vm_map_entry_max_free_right(root, rlist), \
1014 ("%s: max_free not copied from right", __func__)); \
1015 root->left = rlist; \
1020 #define SPLAY_RIGHT_STEP(root, y, llist, test) do { \
1021 vm_size_t max_free; \
1024 * Infer root->left->max_free == root->max_free when \
1025 * y->max_free < root->max_free || root->max_free == 0. \
1026 * Otherwise, look left to find it. \
1029 max_free = root->max_free; \
1030 KASSERT(max_free >= vm_map_entry_max_free_left(root, llist), \
1031 ("%s: max_free invariant fails", __func__)); \
1032 if (y == NULL ? max_free > 0 : max_free - 1 < y->max_free) \
1033 max_free = vm_map_entry_max_free_left(root, llist); \
1034 if (y != NULL && (test)) { \
1035 /* Rotate left and make y root. */ \
1036 root->right = y->left; \
1038 if (max_free < y->max_free) \
1039 root->max_free = max_free = MAX(max_free, \
1040 vm_map_entry_max_free_right(root, y)); \
1044 /* Copy left->max_free. Put root on llist. */ \
1045 root->max_free = max_free; \
1046 KASSERT(max_free == vm_map_entry_max_free_left(root, llist), \
1047 ("%s: max_free not copied from left", __func__)); \
1048 root->right = llist; \
1054 * Walk down the tree until we find addr or a NULL pointer where addr would go,
1055 * breaking off left and right subtrees of nodes less than, or greater than
1056 * addr. Treat pointers to nodes with max_free < length as NULL pointers.
1057 * llist and rlist are the two sides in reverse order (bottom-up), with llist
1058 * linked by the right pointer and rlist linked by the left pointer in the
1059 * vm_map_entry, and both lists terminated by &map->header. This function, and
1060 * the subsequent call to vm_map_splay_merge, rely on the start and end address
1061 * values in &map->header.
1063 static vm_map_entry_t
1064 vm_map_splay_split(vm_map_t map, vm_offset_t addr, vm_size_t length,
1065 vm_map_entry_t *out_llist, vm_map_entry_t *out_rlist)
1067 vm_map_entry_t llist, rlist, root, y;
1069 llist = rlist = &map->header;
1071 while (root != NULL && root->max_free >= length) {
1072 KASSERT(llist->end <= root->start && root->end <= rlist->start,
1073 ("%s: root not within tree bounds", __func__));
1074 if (addr < root->start) {
1075 SPLAY_LEFT_STEP(root, y, rlist,
1076 y->max_free >= length && addr < y->start);
1077 } else if (addr >= root->end) {
1078 SPLAY_RIGHT_STEP(root, y, llist,
1079 y->max_free >= length && addr >= y->end);
1089 vm_map_splay_findnext(vm_map_entry_t root, vm_map_entry_t *iolist)
1091 vm_map_entry_t rlist, y;
1095 while (root != NULL)
1096 SPLAY_LEFT_STEP(root, y, rlist, true);
1101 vm_map_splay_findprev(vm_map_entry_t root, vm_map_entry_t *iolist)
1103 vm_map_entry_t llist, y;
1107 while (root != NULL)
1108 SPLAY_RIGHT_STEP(root, y, llist, true);
1113 vm_map_entry_swap(vm_map_entry_t *a, vm_map_entry_t *b)
1123 * Walk back up the two spines, flip the pointers and set max_free. The
1124 * subtrees of the root go at the bottom of llist and rlist.
1127 vm_map_splay_merge(vm_map_t map, vm_map_entry_t root,
1128 vm_map_entry_t llist, vm_map_entry_t rlist)
1130 vm_map_entry_t prev;
1131 vm_size_t max_free_left, max_free_right;
1133 max_free_left = vm_map_entry_max_free_left(root, llist);
1134 if (llist != &map->header) {
1138 * The max_free values of the children of llist are in
1139 * llist->max_free and max_free_left. Update with the
1142 llist->max_free = max_free_left =
1143 MAX(llist->max_free, max_free_left);
1144 vm_map_entry_swap(&llist->right, &prev);
1145 vm_map_entry_swap(&prev, &llist);
1146 } while (llist != &map->header);
1149 max_free_right = vm_map_entry_max_free_right(root, rlist);
1150 if (rlist != &map->header) {
1154 * The max_free values of the children of rlist are in
1155 * rlist->max_free and max_free_right. Update with the
1158 rlist->max_free = max_free_right =
1159 MAX(rlist->max_free, max_free_right);
1160 vm_map_entry_swap(&rlist->left, &prev);
1161 vm_map_entry_swap(&prev, &rlist);
1162 } while (rlist != &map->header);
1165 root->max_free = MAX(max_free_left, max_free_right);
1172 * The Sleator and Tarjan top-down splay algorithm with the
1173 * following variation. Max_free must be computed bottom-up, so
1174 * on the downward pass, maintain the left and right spines in
1175 * reverse order. Then, make a second pass up each side to fix
1176 * the pointers and compute max_free. The time bound is O(log n)
1179 * The new root is the vm_map_entry containing "addr", or else an
1180 * adjacent entry (lower if possible) if addr is not in the tree.
1182 * The map must be locked, and leaves it so.
1184 * Returns: the new root.
1186 static vm_map_entry_t
1187 vm_map_splay(vm_map_t map, vm_offset_t addr)
1189 vm_map_entry_t llist, rlist, root;
1191 root = vm_map_splay_split(map, addr, 0, &llist, &rlist);
1194 } else if (llist != &map->header) {
1196 * Recover the greatest node in the left
1197 * subtree and make it the root.
1200 llist = root->right;
1202 } else if (rlist != &map->header) {
1204 * Recover the least node in the right
1205 * subtree and make it the root.
1211 /* There is no root. */
1214 vm_map_splay_merge(map, root, llist, rlist);
1215 VM_MAP_ASSERT_CONSISTENT(map);
1220 * vm_map_entry_{un,}link:
1222 * Insert/remove entries from maps.
1225 vm_map_entry_link(vm_map_t map, vm_map_entry_t entry)
1227 vm_map_entry_t llist, rlist, root;
1230 "vm_map_entry_link: map %p, nentries %d, entry %p", map,
1231 map->nentries, entry);
1232 VM_MAP_ASSERT_LOCKED(map);
1234 root = vm_map_splay_split(map, entry->start, 0, &llist, &rlist);
1235 KASSERT(root == NULL,
1236 ("vm_map_entry_link: link object already mapped"));
1237 entry->prev = llist;
1238 entry->next = rlist;
1239 llist->next = rlist->prev = entry;
1240 entry->left = entry->right = NULL;
1241 vm_map_splay_merge(map, entry, llist, rlist);
1242 VM_MAP_ASSERT_CONSISTENT(map);
1245 enum unlink_merge_type {
1252 vm_map_entry_unlink(vm_map_t map, vm_map_entry_t entry,
1253 enum unlink_merge_type op)
1255 vm_map_entry_t llist, rlist, root, y;
1257 VM_MAP_ASSERT_LOCKED(map);
1258 root = vm_map_splay_split(map, entry->start, 0, &llist, &rlist);
1259 KASSERT(root != NULL,
1260 ("vm_map_entry_unlink: unlink object not mapped"));
1263 case UNLINK_MERGE_PREV:
1264 vm_map_splay_findprev(root, &llist);
1265 llist->end = root->end;
1268 llist = root->right;
1271 case UNLINK_MERGE_NEXT:
1272 vm_map_splay_findnext(root, &rlist);
1273 rlist->start = root->start;
1274 rlist->offset = root->offset;
1280 case UNLINK_MERGE_NONE:
1281 vm_map_splay_findprev(root, &llist);
1282 vm_map_splay_findnext(root, &rlist);
1283 if (llist != &map->header) {
1285 llist = root->right;
1287 } else if (rlist != &map->header) {
1296 y->prev = entry->prev;
1299 vm_map_splay_merge(map, root, llist, rlist);
1302 VM_MAP_ASSERT_CONSISTENT(map);
1304 CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map,
1305 map->nentries, entry);
1309 * vm_map_entry_resize:
1311 * Resize a vm_map_entry, recompute the amount of free space that
1312 * follows it and propagate that value up the tree.
1314 * The map must be locked, and leaves it so.
1317 vm_map_entry_resize(vm_map_t map, vm_map_entry_t entry, vm_size_t grow_amount)
1319 vm_map_entry_t llist, rlist, root;
1321 VM_MAP_ASSERT_LOCKED(map);
1322 root = vm_map_splay_split(map, entry->start, 0, &llist, &rlist);
1323 KASSERT(root != NULL,
1324 ("%s: resize object not mapped", __func__));
1325 vm_map_splay_findnext(root, &rlist);
1327 entry->end += grow_amount;
1328 vm_map_splay_merge(map, root, llist, rlist);
1329 VM_MAP_ASSERT_CONSISTENT(map);
1330 CTR4(KTR_VM, "%s: map %p, nentries %d, entry %p",
1331 __func__, map, map->nentries, entry);
1335 * vm_map_lookup_entry: [ internal use only ]
1337 * Finds the map entry containing (or
1338 * immediately preceding) the specified address
1339 * in the given map; the entry is returned
1340 * in the "entry" parameter. The boolean
1341 * result indicates whether the address is
1342 * actually contained in the map.
1345 vm_map_lookup_entry(
1347 vm_offset_t address,
1348 vm_map_entry_t *entry) /* OUT */
1350 vm_map_entry_t cur, lbound;
1354 * If the map is empty, then the map entry immediately preceding
1355 * "address" is the map's header.
1359 *entry = &map->header;
1362 if (address >= cur->start && cur->end > address) {
1366 if ((locked = vm_map_locked(map)) ||
1367 sx_try_upgrade(&map->lock)) {
1369 * Splay requires a write lock on the map. However, it only
1370 * restructures the binary search tree; it does not otherwise
1371 * change the map. Thus, the map's timestamp need not change
1372 * on a temporary upgrade.
1374 cur = vm_map_splay(map, address);
1376 sx_downgrade(&map->lock);
1379 * If "address" is contained within a map entry, the new root
1380 * is that map entry. Otherwise, the new root is a map entry
1381 * immediately before or after "address".
1383 if (address < cur->start) {
1384 *entry = &map->header;
1388 return (address < cur->end);
1391 * Since the map is only locked for read access, perform a
1392 * standard binary search tree lookup for "address".
1394 lbound = &map->header;
1396 if (address < cur->start) {
1398 } else if (cur->end <= address) {
1405 } while (cur != NULL);
1413 * Inserts the given whole VM object into the target
1414 * map at the specified address range. The object's
1415 * size should match that of the address range.
1417 * Requires that the map be locked, and leaves it so.
1419 * If object is non-NULL, ref count must be bumped by caller
1420 * prior to making call to account for the new entry.
1423 vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1424 vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max, int cow)
1426 vm_map_entry_t new_entry, prev_entry, temp_entry;
1428 vm_eflags_t protoeflags;
1429 vm_inherit_t inheritance;
1431 VM_MAP_ASSERT_LOCKED(map);
1432 KASSERT(object != kernel_object ||
1433 (cow & MAP_COPY_ON_WRITE) == 0,
1434 ("vm_map_insert: kernel object and COW"));
1435 KASSERT(object == NULL || (cow & MAP_NOFAULT) == 0,
1436 ("vm_map_insert: paradoxical MAP_NOFAULT request"));
1437 KASSERT((prot & ~max) == 0,
1438 ("prot %#x is not subset of max_prot %#x", prot, max));
1441 * Check that the start and end points are not bogus.
1443 if (start < vm_map_min(map) || end > vm_map_max(map) ||
1445 return (KERN_INVALID_ADDRESS);
1448 * Find the entry prior to the proposed starting address; if it's part
1449 * of an existing entry, this range is bogus.
1451 if (vm_map_lookup_entry(map, start, &temp_entry))
1452 return (KERN_NO_SPACE);
1454 prev_entry = temp_entry;
1457 * Assert that the next entry doesn't overlap the end point.
1459 if (prev_entry->next->start < end)
1460 return (KERN_NO_SPACE);
1462 if ((cow & MAP_CREATE_GUARD) != 0 && (object != NULL ||
1463 max != VM_PROT_NONE))
1464 return (KERN_INVALID_ARGUMENT);
1467 if (cow & MAP_COPY_ON_WRITE)
1468 protoeflags |= MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY;
1469 if (cow & MAP_NOFAULT)
1470 protoeflags |= MAP_ENTRY_NOFAULT;
1471 if (cow & MAP_DISABLE_SYNCER)
1472 protoeflags |= MAP_ENTRY_NOSYNC;
1473 if (cow & MAP_DISABLE_COREDUMP)
1474 protoeflags |= MAP_ENTRY_NOCOREDUMP;
1475 if (cow & MAP_STACK_GROWS_DOWN)
1476 protoeflags |= MAP_ENTRY_GROWS_DOWN;
1477 if (cow & MAP_STACK_GROWS_UP)
1478 protoeflags |= MAP_ENTRY_GROWS_UP;
1479 if (cow & MAP_VN_WRITECOUNT)
1480 protoeflags |= MAP_ENTRY_VN_WRITECNT;
1481 if (cow & MAP_VN_EXEC)
1482 protoeflags |= MAP_ENTRY_VN_EXEC;
1483 if ((cow & MAP_CREATE_GUARD) != 0)
1484 protoeflags |= MAP_ENTRY_GUARD;
1485 if ((cow & MAP_CREATE_STACK_GAP_DN) != 0)
1486 protoeflags |= MAP_ENTRY_STACK_GAP_DN;
1487 if ((cow & MAP_CREATE_STACK_GAP_UP) != 0)
1488 protoeflags |= MAP_ENTRY_STACK_GAP_UP;
1489 if (cow & MAP_INHERIT_SHARE)
1490 inheritance = VM_INHERIT_SHARE;
1492 inheritance = VM_INHERIT_DEFAULT;
1495 if ((cow & (MAP_ACC_NO_CHARGE | MAP_NOFAULT | MAP_CREATE_GUARD)) != 0)
1497 if ((cow & MAP_ACC_CHARGED) || ((prot & VM_PROT_WRITE) &&
1498 ((protoeflags & MAP_ENTRY_NEEDS_COPY) || object == NULL))) {
1499 if (!(cow & MAP_ACC_CHARGED) && !swap_reserve(end - start))
1500 return (KERN_RESOURCE_SHORTAGE);
1501 KASSERT(object == NULL ||
1502 (protoeflags & MAP_ENTRY_NEEDS_COPY) != 0 ||
1503 object->cred == NULL,
1504 ("overcommit: vm_map_insert o %p", object));
1505 cred = curthread->td_ucred;
1509 /* Expand the kernel pmap, if necessary. */
1510 if (map == kernel_map && end > kernel_vm_end)
1511 pmap_growkernel(end);
1512 if (object != NULL) {
1514 * OBJ_ONEMAPPING must be cleared unless this mapping
1515 * is trivially proven to be the only mapping for any
1516 * of the object's pages. (Object granularity
1517 * reference counting is insufficient to recognize
1518 * aliases with precision.)
1520 VM_OBJECT_WLOCK(object);
1521 if (object->ref_count > 1 || object->shadow_count != 0)
1522 vm_object_clear_flag(object, OBJ_ONEMAPPING);
1523 VM_OBJECT_WUNLOCK(object);
1524 } else if ((prev_entry->eflags & ~MAP_ENTRY_USER_WIRED) ==
1526 (cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP |
1527 MAP_VN_EXEC)) == 0 &&
1528 prev_entry->end == start && (prev_entry->cred == cred ||
1529 (prev_entry->object.vm_object != NULL &&
1530 prev_entry->object.vm_object->cred == cred)) &&
1531 vm_object_coalesce(prev_entry->object.vm_object,
1533 (vm_size_t)(prev_entry->end - prev_entry->start),
1534 (vm_size_t)(end - prev_entry->end), cred != NULL &&
1535 (protoeflags & MAP_ENTRY_NEEDS_COPY) == 0)) {
1537 * We were able to extend the object. Determine if we
1538 * can extend the previous map entry to include the
1539 * new range as well.
1541 if (prev_entry->inheritance == inheritance &&
1542 prev_entry->protection == prot &&
1543 prev_entry->max_protection == max &&
1544 prev_entry->wired_count == 0) {
1545 KASSERT((prev_entry->eflags & MAP_ENTRY_USER_WIRED) ==
1546 0, ("prev_entry %p has incoherent wiring",
1548 if ((prev_entry->eflags & MAP_ENTRY_GUARD) == 0)
1549 map->size += end - prev_entry->end;
1550 vm_map_entry_resize(map, prev_entry,
1551 end - prev_entry->end);
1552 vm_map_simplify_entry(map, prev_entry);
1553 return (KERN_SUCCESS);
1557 * If we can extend the object but cannot extend the
1558 * map entry, we have to create a new map entry. We
1559 * must bump the ref count on the extended object to
1560 * account for it. object may be NULL.
1562 object = prev_entry->object.vm_object;
1563 offset = prev_entry->offset +
1564 (prev_entry->end - prev_entry->start);
1565 vm_object_reference(object);
1566 if (cred != NULL && object != NULL && object->cred != NULL &&
1567 !(prev_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
1568 /* Object already accounts for this uid. */
1576 * Create a new entry
1578 new_entry = vm_map_entry_create(map);
1579 new_entry->start = start;
1580 new_entry->end = end;
1581 new_entry->cred = NULL;
1583 new_entry->eflags = protoeflags;
1584 new_entry->object.vm_object = object;
1585 new_entry->offset = offset;
1587 new_entry->inheritance = inheritance;
1588 new_entry->protection = prot;
1589 new_entry->max_protection = max;
1590 new_entry->wired_count = 0;
1591 new_entry->wiring_thread = NULL;
1592 new_entry->read_ahead = VM_FAULT_READ_AHEAD_INIT;
1593 new_entry->next_read = start;
1595 KASSERT(cred == NULL || !ENTRY_CHARGED(new_entry),
1596 ("overcommit: vm_map_insert leaks vm_map %p", new_entry));
1597 new_entry->cred = cred;
1600 * Insert the new entry into the list
1602 vm_map_entry_link(map, new_entry);
1603 if ((new_entry->eflags & MAP_ENTRY_GUARD) == 0)
1604 map->size += new_entry->end - new_entry->start;
1607 * Try to coalesce the new entry with both the previous and next
1608 * entries in the list. Previously, we only attempted to coalesce
1609 * with the previous entry when object is NULL. Here, we handle the
1610 * other cases, which are less common.
1612 vm_map_simplify_entry(map, new_entry);
1614 if ((cow & (MAP_PREFAULT | MAP_PREFAULT_PARTIAL)) != 0) {
1615 vm_map_pmap_enter(map, start, prot, object, OFF_TO_IDX(offset),
1616 end - start, cow & MAP_PREFAULT_PARTIAL);
1619 return (KERN_SUCCESS);
1625 * Find the first fit (lowest VM address) for "length" free bytes
1626 * beginning at address >= start in the given map.
1628 * In a vm_map_entry, "max_free" is the maximum amount of
1629 * contiguous free space between an entry in its subtree and a
1630 * neighbor of that entry. This allows finding a free region in
1631 * one path down the tree, so O(log n) amortized with splay
1634 * The map must be locked, and leaves it so.
1636 * Returns: starting address if sufficient space,
1637 * vm_map_max(map)-length+1 if insufficient space.
1640 vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length)
1642 vm_map_entry_t llist, rlist, root, y;
1643 vm_size_t left_length;
1644 vm_offset_t gap_end;
1647 * Request must fit within min/max VM address and must avoid
1650 start = MAX(start, vm_map_min(map));
1651 if (start >= vm_map_max(map) || length > vm_map_max(map) - start)
1652 return (vm_map_max(map) - length + 1);
1654 /* Empty tree means wide open address space. */
1655 if (map->root == NULL)
1659 * After splay_split, if start is within an entry, push it to the start
1660 * of the following gap. If rlist is at the end of the gap containing
1661 * start, save the end of that gap in gap_end to see if the gap is big
1662 * enough; otherwise set gap_end to start skip gap-checking and move
1663 * directly to a search of the right subtree.
1665 root = vm_map_splay_split(map, start, length, &llist, &rlist);
1666 gap_end = rlist->start;
1669 if (root->right != NULL)
1671 } else if (rlist != &map->header) {
1677 llist = root->right;
1680 vm_map_splay_merge(map, root, llist, rlist);
1681 VM_MAP_ASSERT_CONSISTENT(map);
1682 if (length <= gap_end - start)
1685 /* With max_free, can immediately tell if no solution. */
1686 if (root->right == NULL || length > root->right->max_free)
1687 return (vm_map_max(map) - length + 1);
1690 * Splay for the least large-enough gap in the right subtree.
1692 llist = rlist = &map->header;
1693 for (left_length = 0;;
1694 left_length = vm_map_entry_max_free_left(root, llist)) {
1695 if (length <= left_length)
1696 SPLAY_LEFT_STEP(root, y, rlist,
1697 length <= vm_map_entry_max_free_left(y, llist));
1699 SPLAY_RIGHT_STEP(root, y, llist,
1700 length > vm_map_entry_max_free_left(y, root));
1705 llist = root->right;
1707 if (rlist != &map->header) {
1711 vm_map_splay_merge(map, y, &map->header, rlist);
1713 vm_map_entry_max_free_left(y, root),
1714 vm_map_entry_max_free_right(y, &map->header));
1717 vm_map_splay_merge(map, root, llist, &map->header);
1718 VM_MAP_ASSERT_CONSISTENT(map);
1723 vm_map_fixed(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1724 vm_offset_t start, vm_size_t length, vm_prot_t prot,
1725 vm_prot_t max, int cow)
1730 end = start + length;
1731 KASSERT((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0 ||
1733 ("vm_map_fixed: non-NULL backing object for stack"));
1735 VM_MAP_RANGE_CHECK(map, start, end);
1736 if ((cow & MAP_CHECK_EXCL) == 0)
1737 vm_map_delete(map, start, end);
1738 if ((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) != 0) {
1739 result = vm_map_stack_locked(map, start, length, sgrowsiz,
1742 result = vm_map_insert(map, object, offset, start, end,
1749 static const int aslr_pages_rnd_64[2] = {0x1000, 0x10};
1750 static const int aslr_pages_rnd_32[2] = {0x100, 0x4};
1752 static int cluster_anon = 1;
1753 SYSCTL_INT(_vm, OID_AUTO, cluster_anon, CTLFLAG_RW,
1755 "Cluster anonymous mappings: 0 = no, 1 = yes if no hint, 2 = always");
1758 clustering_anon_allowed(vm_offset_t addr)
1761 switch (cluster_anon) {
1772 static long aslr_restarts;
1773 SYSCTL_LONG(_vm, OID_AUTO, aslr_restarts, CTLFLAG_RD,
1775 "Number of aslr failures");
1777 #define MAP_32BIT_MAX_ADDR ((vm_offset_t)1 << 31)
1780 * Searches for the specified amount of free space in the given map with the
1781 * specified alignment. Performs an address-ordered, first-fit search from
1782 * the given address "*addr", with an optional upper bound "max_addr". If the
1783 * parameter "alignment" is zero, then the alignment is computed from the
1784 * given (object, offset) pair so as to enable the greatest possible use of
1785 * superpage mappings. Returns KERN_SUCCESS and the address of the free space
1786 * in "*addr" if successful. Otherwise, returns KERN_NO_SPACE.
1788 * The map must be locked. Initially, there must be at least "length" bytes
1789 * of free space at the given address.
1792 vm_map_alignspace(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1793 vm_offset_t *addr, vm_size_t length, vm_offset_t max_addr,
1794 vm_offset_t alignment)
1796 vm_offset_t aligned_addr, free_addr;
1798 VM_MAP_ASSERT_LOCKED(map);
1800 KASSERT(free_addr == vm_map_findspace(map, free_addr, length),
1801 ("caller failed to provide space %#jx at address %p",
1802 (uintmax_t)length, (void *)free_addr));
1805 * At the start of every iteration, the free space at address
1806 * "*addr" is at least "length" bytes.
1809 pmap_align_superpage(object, offset, addr, length);
1810 else if ((*addr & (alignment - 1)) != 0) {
1811 *addr &= ~(alignment - 1);
1814 aligned_addr = *addr;
1815 if (aligned_addr == free_addr) {
1817 * Alignment did not change "*addr", so "*addr" must
1818 * still provide sufficient free space.
1820 return (KERN_SUCCESS);
1824 * Test for address wrap on "*addr". A wrapped "*addr" could
1825 * be a valid address, in which case vm_map_findspace() cannot
1826 * be relied upon to fail.
1828 if (aligned_addr < free_addr)
1829 return (KERN_NO_SPACE);
1830 *addr = vm_map_findspace(map, aligned_addr, length);
1831 if (*addr + length > vm_map_max(map) ||
1832 (max_addr != 0 && *addr + length > max_addr))
1833 return (KERN_NO_SPACE);
1835 if (free_addr == aligned_addr) {
1837 * If a successful call to vm_map_findspace() did not
1838 * change "*addr", then "*addr" must still be aligned
1839 * and provide sufficient free space.
1841 return (KERN_SUCCESS);
1847 * vm_map_find finds an unallocated region in the target address
1848 * map with the given length. The search is defined to be
1849 * first-fit from the specified address; the region found is
1850 * returned in the same parameter.
1852 * If object is non-NULL, ref count must be bumped by caller
1853 * prior to making call to account for the new entry.
1856 vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1857 vm_offset_t *addr, /* IN/OUT */
1858 vm_size_t length, vm_offset_t max_addr, int find_space,
1859 vm_prot_t prot, vm_prot_t max, int cow)
1861 vm_offset_t alignment, curr_min_addr, min_addr;
1862 int gap, pidx, rv, try;
1863 bool cluster, en_aslr, update_anon;
1865 KASSERT((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0 ||
1867 ("vm_map_find: non-NULL backing object for stack"));
1868 MPASS((cow & MAP_REMAP) == 0 || (find_space == VMFS_NO_SPACE &&
1869 (cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0));
1870 if (find_space == VMFS_OPTIMAL_SPACE && (object == NULL ||
1871 (object->flags & OBJ_COLORED) == 0))
1872 find_space = VMFS_ANY_SPACE;
1873 if (find_space >> 8 != 0) {
1874 KASSERT((find_space & 0xff) == 0, ("bad VMFS flags"));
1875 alignment = (vm_offset_t)1 << (find_space >> 8);
1878 en_aslr = (map->flags & MAP_ASLR) != 0;
1879 update_anon = cluster = clustering_anon_allowed(*addr) &&
1880 (map->flags & MAP_IS_SUB_MAP) == 0 && max_addr == 0 &&
1881 find_space != VMFS_NO_SPACE && object == NULL &&
1882 (cow & (MAP_INHERIT_SHARE | MAP_STACK_GROWS_UP |
1883 MAP_STACK_GROWS_DOWN)) == 0 && prot != PROT_NONE;
1884 curr_min_addr = min_addr = *addr;
1885 if (en_aslr && min_addr == 0 && !cluster &&
1886 find_space != VMFS_NO_SPACE &&
1887 (map->flags & MAP_ASLR_IGNSTART) != 0)
1888 curr_min_addr = min_addr = vm_map_min(map);
1892 curr_min_addr = map->anon_loc;
1893 if (curr_min_addr == 0)
1896 if (find_space != VMFS_NO_SPACE) {
1897 KASSERT(find_space == VMFS_ANY_SPACE ||
1898 find_space == VMFS_OPTIMAL_SPACE ||
1899 find_space == VMFS_SUPER_SPACE ||
1900 alignment != 0, ("unexpected VMFS flag"));
1903 * When creating an anonymous mapping, try clustering
1904 * with an existing anonymous mapping first.
1906 * We make up to two attempts to find address space
1907 * for a given find_space value. The first attempt may
1908 * apply randomization or may cluster with an existing
1909 * anonymous mapping. If this first attempt fails,
1910 * perform a first-fit search of the available address
1913 * If all tries failed, and find_space is
1914 * VMFS_OPTIMAL_SPACE, fallback to VMFS_ANY_SPACE.
1915 * Again enable clustering and randomization.
1922 * Second try: we failed either to find a
1923 * suitable region for randomizing the
1924 * allocation, or to cluster with an existing
1925 * mapping. Retry with free run.
1927 curr_min_addr = (map->flags & MAP_ASLR_IGNSTART) != 0 ?
1928 vm_map_min(map) : min_addr;
1929 atomic_add_long(&aslr_restarts, 1);
1932 if (try == 1 && en_aslr && !cluster) {
1934 * Find space for allocation, including
1935 * gap needed for later randomization.
1937 pidx = MAXPAGESIZES > 1 && pagesizes[1] != 0 &&
1938 (find_space == VMFS_SUPER_SPACE || find_space ==
1939 VMFS_OPTIMAL_SPACE) ? 1 : 0;
1940 gap = vm_map_max(map) > MAP_32BIT_MAX_ADDR &&
1941 (max_addr == 0 || max_addr > MAP_32BIT_MAX_ADDR) ?
1942 aslr_pages_rnd_64[pidx] : aslr_pages_rnd_32[pidx];
1943 *addr = vm_map_findspace(map, curr_min_addr,
1944 length + gap * pagesizes[pidx]);
1945 if (*addr + length + gap * pagesizes[pidx] >
1948 /* And randomize the start address. */
1949 *addr += (arc4random() % gap) * pagesizes[pidx];
1950 if (max_addr != 0 && *addr + length > max_addr)
1953 *addr = vm_map_findspace(map, curr_min_addr, length);
1954 if (*addr + length > vm_map_max(map) ||
1955 (max_addr != 0 && *addr + length > max_addr)) {
1966 if (find_space != VMFS_ANY_SPACE &&
1967 (rv = vm_map_alignspace(map, object, offset, addr, length,
1968 max_addr, alignment)) != KERN_SUCCESS) {
1969 if (find_space == VMFS_OPTIMAL_SPACE) {
1970 find_space = VMFS_ANY_SPACE;
1971 curr_min_addr = min_addr;
1972 cluster = update_anon;
1978 } else if ((cow & MAP_REMAP) != 0) {
1979 if (*addr < vm_map_min(map) ||
1980 *addr + length > vm_map_max(map) ||
1981 *addr + length <= length) {
1982 rv = KERN_INVALID_ADDRESS;
1985 vm_map_delete(map, *addr, *addr + length);
1987 if ((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) != 0) {
1988 rv = vm_map_stack_locked(map, *addr, length, sgrowsiz, prot,
1991 rv = vm_map_insert(map, object, offset, *addr, *addr + length,
1994 if (rv == KERN_SUCCESS && update_anon)
1995 map->anon_loc = *addr + length;
2002 * vm_map_find_min() is a variant of vm_map_find() that takes an
2003 * additional parameter (min_addr) and treats the given address
2004 * (*addr) differently. Specifically, it treats *addr as a hint
2005 * and not as the minimum address where the mapping is created.
2007 * This function works in two phases. First, it tries to
2008 * allocate above the hint. If that fails and the hint is
2009 * greater than min_addr, it performs a second pass, replacing
2010 * the hint with min_addr as the minimum address for the
2014 vm_map_find_min(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
2015 vm_offset_t *addr, vm_size_t length, vm_offset_t min_addr,
2016 vm_offset_t max_addr, int find_space, vm_prot_t prot, vm_prot_t max,
2024 rv = vm_map_find(map, object, offset, addr, length, max_addr,
2025 find_space, prot, max, cow);
2026 if (rv == KERN_SUCCESS || min_addr >= hint)
2028 *addr = hint = min_addr;
2033 * A map entry with any of the following flags set must not be merged with
2036 #define MAP_ENTRY_NOMERGE_MASK (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP | \
2037 MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP | MAP_ENTRY_VN_EXEC)
2040 vm_map_mergeable_neighbors(vm_map_entry_t prev, vm_map_entry_t entry)
2043 KASSERT((prev->eflags & MAP_ENTRY_NOMERGE_MASK) == 0 ||
2044 (entry->eflags & MAP_ENTRY_NOMERGE_MASK) == 0,
2045 ("vm_map_mergeable_neighbors: neither %p nor %p are mergeable",
2047 return (prev->end == entry->start &&
2048 prev->object.vm_object == entry->object.vm_object &&
2049 (prev->object.vm_object == NULL ||
2050 prev->offset + (prev->end - prev->start) == entry->offset) &&
2051 prev->eflags == entry->eflags &&
2052 prev->protection == entry->protection &&
2053 prev->max_protection == entry->max_protection &&
2054 prev->inheritance == entry->inheritance &&
2055 prev->wired_count == entry->wired_count &&
2056 prev->cred == entry->cred);
2060 vm_map_merged_neighbor_dispose(vm_map_t map, vm_map_entry_t entry)
2064 * If the backing object is a vnode object, vm_object_deallocate()
2065 * calls vrele(). However, vrele() does not lock the vnode because
2066 * the vnode has additional references. Thus, the map lock can be
2067 * kept without causing a lock-order reversal with the vnode lock.
2069 * Since we count the number of virtual page mappings in
2070 * object->un_pager.vnp.writemappings, the writemappings value
2071 * should not be adjusted when the entry is disposed of.
2073 if (entry->object.vm_object != NULL)
2074 vm_object_deallocate(entry->object.vm_object);
2075 if (entry->cred != NULL)
2076 crfree(entry->cred);
2077 vm_map_entry_dispose(map, entry);
2081 * vm_map_simplify_entry:
2083 * Simplify the given map entry by merging with either neighbor. This
2084 * routine also has the ability to merge with both neighbors.
2086 * The map must be locked.
2088 * This routine guarantees that the passed entry remains valid (though
2089 * possibly extended). When merging, this routine may delete one or
2093 vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry)
2095 vm_map_entry_t next, prev;
2097 if ((entry->eflags & MAP_ENTRY_NOMERGE_MASK) != 0)
2100 if (vm_map_mergeable_neighbors(prev, entry)) {
2101 vm_map_entry_unlink(map, prev, UNLINK_MERGE_NEXT);
2102 vm_map_merged_neighbor_dispose(map, prev);
2105 if (vm_map_mergeable_neighbors(entry, next)) {
2106 vm_map_entry_unlink(map, next, UNLINK_MERGE_PREV);
2107 vm_map_merged_neighbor_dispose(map, next);
2112 * vm_map_clip_start: [ internal use only ]
2114 * Asserts that the given entry begins at or after
2115 * the specified address; if necessary,
2116 * it splits the entry into two.
2118 #define vm_map_clip_start(map, entry, startaddr) \
2120 if (startaddr > entry->start) \
2121 _vm_map_clip_start(map, entry, startaddr); \
2125 * This routine is called only when it is known that
2126 * the entry must be split.
2129 _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start)
2131 vm_map_entry_t new_entry;
2133 VM_MAP_ASSERT_LOCKED(map);
2134 KASSERT(entry->end > start && entry->start < start,
2135 ("_vm_map_clip_start: invalid clip of entry %p", entry));
2138 * Split off the front portion -- note that we must insert the new
2139 * entry BEFORE this one, so that this entry has the specified
2142 vm_map_simplify_entry(map, entry);
2145 * If there is no object backing this entry, we might as well create
2146 * one now. If we defer it, an object can get created after the map
2147 * is clipped, and individual objects will be created for the split-up
2148 * map. This is a bit of a hack, but is also about the best place to
2149 * put this improvement.
2151 if (entry->object.vm_object == NULL && !map->system_map &&
2152 (entry->eflags & MAP_ENTRY_GUARD) == 0) {
2154 object = vm_object_allocate(OBJT_DEFAULT,
2155 atop(entry->end - entry->start));
2156 entry->object.vm_object = object;
2158 if (entry->cred != NULL) {
2159 object->cred = entry->cred;
2160 object->charge = entry->end - entry->start;
2163 } else if (entry->object.vm_object != NULL &&
2164 ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) &&
2165 entry->cred != NULL) {
2166 VM_OBJECT_WLOCK(entry->object.vm_object);
2167 KASSERT(entry->object.vm_object->cred == NULL,
2168 ("OVERCOMMIT: vm_entry_clip_start: both cred e %p", entry));
2169 entry->object.vm_object->cred = entry->cred;
2170 entry->object.vm_object->charge = entry->end - entry->start;
2171 VM_OBJECT_WUNLOCK(entry->object.vm_object);
2175 new_entry = vm_map_entry_create(map);
2176 *new_entry = *entry;
2178 new_entry->end = start;
2179 entry->offset += (start - entry->start);
2180 entry->start = start;
2181 if (new_entry->cred != NULL)
2182 crhold(entry->cred);
2184 vm_map_entry_link(map, new_entry);
2186 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
2187 vm_object_reference(new_entry->object.vm_object);
2188 vm_map_entry_set_vnode_text(new_entry, true);
2190 * The object->un_pager.vnp.writemappings for the
2191 * object of MAP_ENTRY_VN_WRITECNT type entry shall be
2192 * kept as is here. The virtual pages are
2193 * re-distributed among the clipped entries, so the sum is
2200 * vm_map_clip_end: [ internal use only ]
2202 * Asserts that the given entry ends at or before
2203 * the specified address; if necessary,
2204 * it splits the entry into two.
2206 #define vm_map_clip_end(map, entry, endaddr) \
2208 if ((endaddr) < (entry->end)) \
2209 _vm_map_clip_end((map), (entry), (endaddr)); \
2213 * This routine is called only when it is known that
2214 * the entry must be split.
2217 _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end)
2219 vm_map_entry_t new_entry;
2221 VM_MAP_ASSERT_LOCKED(map);
2222 KASSERT(entry->start < end && entry->end > end,
2223 ("_vm_map_clip_end: invalid clip of entry %p", entry));
2226 * If there is no object backing this entry, we might as well create
2227 * one now. If we defer it, an object can get created after the map
2228 * is clipped, and individual objects will be created for the split-up
2229 * map. This is a bit of a hack, but is also about the best place to
2230 * put this improvement.
2232 if (entry->object.vm_object == NULL && !map->system_map &&
2233 (entry->eflags & MAP_ENTRY_GUARD) == 0) {
2235 object = vm_object_allocate(OBJT_DEFAULT,
2236 atop(entry->end - entry->start));
2237 entry->object.vm_object = object;
2239 if (entry->cred != NULL) {
2240 object->cred = entry->cred;
2241 object->charge = entry->end - entry->start;
2244 } else if (entry->object.vm_object != NULL &&
2245 ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) &&
2246 entry->cred != NULL) {
2247 VM_OBJECT_WLOCK(entry->object.vm_object);
2248 KASSERT(entry->object.vm_object->cred == NULL,
2249 ("OVERCOMMIT: vm_entry_clip_end: both cred e %p", entry));
2250 entry->object.vm_object->cred = entry->cred;
2251 entry->object.vm_object->charge = entry->end - entry->start;
2252 VM_OBJECT_WUNLOCK(entry->object.vm_object);
2257 * Create a new entry and insert it AFTER the specified entry
2259 new_entry = vm_map_entry_create(map);
2260 *new_entry = *entry;
2262 new_entry->start = entry->end = end;
2263 new_entry->offset += (end - entry->start);
2264 if (new_entry->cred != NULL)
2265 crhold(entry->cred);
2267 vm_map_entry_link(map, new_entry);
2269 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
2270 vm_object_reference(new_entry->object.vm_object);
2271 vm_map_entry_set_vnode_text(new_entry, true);
2276 * vm_map_submap: [ kernel use only ]
2278 * Mark the given range as handled by a subordinate map.
2280 * This range must have been created with vm_map_find,
2281 * and no other operations may have been performed on this
2282 * range prior to calling vm_map_submap.
2284 * Only a limited number of operations can be performed
2285 * within this rage after calling vm_map_submap:
2287 * [Don't try vm_map_copy!]
2289 * To remove a submapping, one must first remove the
2290 * range from the superior map, and then destroy the
2291 * submap (if desired). [Better yet, don't try it.]
2300 vm_map_entry_t entry;
2303 result = KERN_INVALID_ARGUMENT;
2305 vm_map_lock(submap);
2306 submap->flags |= MAP_IS_SUB_MAP;
2307 vm_map_unlock(submap);
2311 VM_MAP_RANGE_CHECK(map, start, end);
2313 if (vm_map_lookup_entry(map, start, &entry)) {
2314 vm_map_clip_start(map, entry, start);
2316 entry = entry->next;
2318 vm_map_clip_end(map, entry, end);
2320 if ((entry->start == start) && (entry->end == end) &&
2321 ((entry->eflags & MAP_ENTRY_COW) == 0) &&
2322 (entry->object.vm_object == NULL)) {
2323 entry->object.sub_map = submap;
2324 entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
2325 result = KERN_SUCCESS;
2329 if (result != KERN_SUCCESS) {
2330 vm_map_lock(submap);
2331 submap->flags &= ~MAP_IS_SUB_MAP;
2332 vm_map_unlock(submap);
2338 * The maximum number of pages to map if MAP_PREFAULT_PARTIAL is specified
2340 #define MAX_INIT_PT 96
2343 * vm_map_pmap_enter:
2345 * Preload the specified map's pmap with mappings to the specified
2346 * object's memory-resident pages. No further physical pages are
2347 * allocated, and no further virtual pages are retrieved from secondary
2348 * storage. If the specified flags include MAP_PREFAULT_PARTIAL, then a
2349 * limited number of page mappings are created at the low-end of the
2350 * specified address range. (For this purpose, a superpage mapping
2351 * counts as one page mapping.) Otherwise, all resident pages within
2352 * the specified address range are mapped.
2355 vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
2356 vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags)
2359 vm_page_t p, p_start;
2360 vm_pindex_t mask, psize, threshold, tmpidx;
2362 if ((prot & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 || object == NULL)
2364 VM_OBJECT_RLOCK(object);
2365 if (object->type == OBJT_DEVICE || object->type == OBJT_SG) {
2366 VM_OBJECT_RUNLOCK(object);
2367 VM_OBJECT_WLOCK(object);
2368 if (object->type == OBJT_DEVICE || object->type == OBJT_SG) {
2369 pmap_object_init_pt(map->pmap, addr, object, pindex,
2371 VM_OBJECT_WUNLOCK(object);
2374 VM_OBJECT_LOCK_DOWNGRADE(object);
2378 if (psize + pindex > object->size) {
2379 if (object->size < pindex) {
2380 VM_OBJECT_RUNLOCK(object);
2383 psize = object->size - pindex;
2388 threshold = MAX_INIT_PT;
2390 p = vm_page_find_least(object, pindex);
2392 * Assert: the variable p is either (1) the page with the
2393 * least pindex greater than or equal to the parameter pindex
2397 p != NULL && (tmpidx = p->pindex - pindex) < psize;
2398 p = TAILQ_NEXT(p, listq)) {
2400 * don't allow an madvise to blow away our really
2401 * free pages allocating pv entries.
2403 if (((flags & MAP_PREFAULT_MADVISE) != 0 &&
2404 vm_page_count_severe()) ||
2405 ((flags & MAP_PREFAULT_PARTIAL) != 0 &&
2406 tmpidx >= threshold)) {
2410 if (p->valid == VM_PAGE_BITS_ALL) {
2411 if (p_start == NULL) {
2412 start = addr + ptoa(tmpidx);
2415 /* Jump ahead if a superpage mapping is possible. */
2416 if (p->psind > 0 && ((addr + ptoa(tmpidx)) &
2417 (pagesizes[p->psind] - 1)) == 0) {
2418 mask = atop(pagesizes[p->psind]) - 1;
2419 if (tmpidx + mask < psize &&
2420 vm_page_ps_test(p, PS_ALL_VALID, NULL)) {
2425 } else if (p_start != NULL) {
2426 pmap_enter_object(map->pmap, start, addr +
2427 ptoa(tmpidx), p_start, prot);
2431 if (p_start != NULL)
2432 pmap_enter_object(map->pmap, start, addr + ptoa(psize),
2434 VM_OBJECT_RUNLOCK(object);
2440 * Sets the protection of the specified address
2441 * region in the target map. If "set_max" is
2442 * specified, the maximum protection is to be set;
2443 * otherwise, only the current protection is affected.
2446 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
2447 vm_prot_t new_prot, boolean_t set_max)
2449 vm_map_entry_t current, entry, in_tran;
2455 return (KERN_SUCCESS);
2462 * Ensure that we are not concurrently wiring pages. vm_map_wire() may
2463 * need to fault pages into the map and will drop the map lock while
2464 * doing so, and the VM object may end up in an inconsistent state if we
2465 * update the protection on the map entry in between faults.
2467 vm_map_wait_busy(map);
2469 VM_MAP_RANGE_CHECK(map, start, end);
2471 if (vm_map_lookup_entry(map, start, &entry)) {
2472 vm_map_clip_start(map, entry, start);
2474 entry = entry->next;
2478 * Make a first pass to check for protection violations.
2480 for (current = entry; current->start < end; current = current->next) {
2481 if ((current->eflags & MAP_ENTRY_GUARD) != 0)
2483 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
2485 return (KERN_INVALID_ARGUMENT);
2487 if ((new_prot & current->max_protection) != new_prot) {
2489 return (KERN_PROTECTION_FAILURE);
2491 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0)
2496 * Postpone the operation until all in transition map entries
2497 * are stabilized. In-transition entry might already have its
2498 * pages wired and wired_count incremented, but
2499 * MAP_ENTRY_USER_WIRED flag not yet set, and visible to other
2500 * threads because the map lock is dropped. In this case we
2501 * would miss our call to vm_fault_copy_entry().
2503 if (in_tran != NULL) {
2504 in_tran->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2505 vm_map_unlock_and_wait(map, 0);
2510 * Do an accounting pass for private read-only mappings that
2511 * now will do cow due to allowed write (e.g. debugger sets
2512 * breakpoint on text segment)
2514 for (current = entry; current->start < end; current = current->next) {
2516 vm_map_clip_end(map, current, end);
2519 ((new_prot & ~(current->protection)) & VM_PROT_WRITE) == 0 ||
2520 ENTRY_CHARGED(current) ||
2521 (current->eflags & MAP_ENTRY_GUARD) != 0) {
2525 cred = curthread->td_ucred;
2526 obj = current->object.vm_object;
2528 if (obj == NULL || (current->eflags & MAP_ENTRY_NEEDS_COPY)) {
2529 if (!swap_reserve(current->end - current->start)) {
2531 return (KERN_RESOURCE_SHORTAGE);
2534 current->cred = cred;
2538 VM_OBJECT_WLOCK(obj);
2539 if (obj->type != OBJT_DEFAULT && obj->type != OBJT_SWAP) {
2540 VM_OBJECT_WUNLOCK(obj);
2545 * Charge for the whole object allocation now, since
2546 * we cannot distinguish between non-charged and
2547 * charged clipped mapping of the same object later.
2549 KASSERT(obj->charge == 0,
2550 ("vm_map_protect: object %p overcharged (entry %p)",
2552 if (!swap_reserve(ptoa(obj->size))) {
2553 VM_OBJECT_WUNLOCK(obj);
2555 return (KERN_RESOURCE_SHORTAGE);
2560 obj->charge = ptoa(obj->size);
2561 VM_OBJECT_WUNLOCK(obj);
2565 * Go back and fix up protections. [Note that clipping is not
2566 * necessary the second time.]
2568 for (current = entry; current->start < end; current = current->next) {
2569 if ((current->eflags & MAP_ENTRY_GUARD) != 0)
2572 old_prot = current->protection;
2575 current->protection =
2576 (current->max_protection = new_prot) &
2579 current->protection = new_prot;
2582 * For user wired map entries, the normal lazy evaluation of
2583 * write access upgrades through soft page faults is
2584 * undesirable. Instead, immediately copy any pages that are
2585 * copy-on-write and enable write access in the physical map.
2587 if ((current->eflags & MAP_ENTRY_USER_WIRED) != 0 &&
2588 (current->protection & VM_PROT_WRITE) != 0 &&
2589 (old_prot & VM_PROT_WRITE) == 0)
2590 vm_fault_copy_entry(map, map, current, current, NULL);
2593 * When restricting access, update the physical map. Worry
2594 * about copy-on-write here.
2596 if ((old_prot & ~current->protection) != 0) {
2597 #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
2599 pmap_protect(map->pmap, current->start,
2601 current->protection & MASK(current));
2604 vm_map_simplify_entry(map, current);
2607 return (KERN_SUCCESS);
2613 * This routine traverses a processes map handling the madvise
2614 * system call. Advisories are classified as either those effecting
2615 * the vm_map_entry structure, or those effecting the underlying
2625 vm_map_entry_t current, entry;
2629 * Some madvise calls directly modify the vm_map_entry, in which case
2630 * we need to use an exclusive lock on the map and we need to perform
2631 * various clipping operations. Otherwise we only need a read-lock
2636 case MADV_SEQUENTIAL:
2653 vm_map_lock_read(map);
2660 * Locate starting entry and clip if necessary.
2662 VM_MAP_RANGE_CHECK(map, start, end);
2664 if (vm_map_lookup_entry(map, start, &entry)) {
2666 vm_map_clip_start(map, entry, start);
2668 entry = entry->next;
2673 * madvise behaviors that are implemented in the vm_map_entry.
2675 * We clip the vm_map_entry so that behavioral changes are
2676 * limited to the specified address range.
2678 for (current = entry; current->start < end;
2679 current = current->next) {
2680 if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
2683 vm_map_clip_end(map, current, end);
2687 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
2689 case MADV_SEQUENTIAL:
2690 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
2693 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
2696 current->eflags |= MAP_ENTRY_NOSYNC;
2699 current->eflags &= ~MAP_ENTRY_NOSYNC;
2702 current->eflags |= MAP_ENTRY_NOCOREDUMP;
2705 current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
2710 vm_map_simplify_entry(map, current);
2714 vm_pindex_t pstart, pend;
2717 * madvise behaviors that are implemented in the underlying
2720 * Since we don't clip the vm_map_entry, we have to clip
2721 * the vm_object pindex and count.
2723 for (current = entry; current->start < end;
2724 current = current->next) {
2725 vm_offset_t useEnd, useStart;
2727 if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
2730 pstart = OFF_TO_IDX(current->offset);
2731 pend = pstart + atop(current->end - current->start);
2732 useStart = current->start;
2733 useEnd = current->end;
2735 if (current->start < start) {
2736 pstart += atop(start - current->start);
2739 if (current->end > end) {
2740 pend -= atop(current->end - end);
2748 * Perform the pmap_advise() before clearing
2749 * PGA_REFERENCED in vm_page_advise(). Otherwise, a
2750 * concurrent pmap operation, such as pmap_remove(),
2751 * could clear a reference in the pmap and set
2752 * PGA_REFERENCED on the page before the pmap_advise()
2753 * had completed. Consequently, the page would appear
2754 * referenced based upon an old reference that
2755 * occurred before this pmap_advise() ran.
2757 if (behav == MADV_DONTNEED || behav == MADV_FREE)
2758 pmap_advise(map->pmap, useStart, useEnd,
2761 vm_object_madvise(current->object.vm_object, pstart,
2765 * Pre-populate paging structures in the
2766 * WILLNEED case. For wired entries, the
2767 * paging structures are already populated.
2769 if (behav == MADV_WILLNEED &&
2770 current->wired_count == 0) {
2771 vm_map_pmap_enter(map,
2773 current->protection,
2774 current->object.vm_object,
2776 ptoa(pend - pstart),
2777 MAP_PREFAULT_MADVISE
2781 vm_map_unlock_read(map);
2790 * Sets the inheritance of the specified address
2791 * range in the target map. Inheritance
2792 * affects how the map will be shared with
2793 * child maps at the time of vmspace_fork.
2796 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
2797 vm_inherit_t new_inheritance)
2799 vm_map_entry_t entry;
2800 vm_map_entry_t temp_entry;
2802 switch (new_inheritance) {
2803 case VM_INHERIT_NONE:
2804 case VM_INHERIT_COPY:
2805 case VM_INHERIT_SHARE:
2806 case VM_INHERIT_ZERO:
2809 return (KERN_INVALID_ARGUMENT);
2812 return (KERN_SUCCESS);
2814 VM_MAP_RANGE_CHECK(map, start, end);
2815 if (vm_map_lookup_entry(map, start, &temp_entry)) {
2817 vm_map_clip_start(map, entry, start);
2819 entry = temp_entry->next;
2820 while (entry->start < end) {
2821 vm_map_clip_end(map, entry, end);
2822 if ((entry->eflags & MAP_ENTRY_GUARD) == 0 ||
2823 new_inheritance != VM_INHERIT_ZERO)
2824 entry->inheritance = new_inheritance;
2825 vm_map_simplify_entry(map, entry);
2826 entry = entry->next;
2829 return (KERN_SUCCESS);
2835 * Implements both kernel and user unwiring.
2838 vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
2841 vm_map_entry_t entry, first_entry, tmp_entry;
2842 vm_offset_t saved_start;
2843 unsigned int last_timestamp;
2845 boolean_t need_wakeup, result, user_unwire;
2848 return (KERN_SUCCESS);
2849 user_unwire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
2851 VM_MAP_RANGE_CHECK(map, start, end);
2852 if (!vm_map_lookup_entry(map, start, &first_entry)) {
2853 if (flags & VM_MAP_WIRE_HOLESOK)
2854 first_entry = first_entry->next;
2857 return (KERN_INVALID_ADDRESS);
2860 last_timestamp = map->timestamp;
2861 entry = first_entry;
2862 while (entry->start < end) {
2863 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
2865 * We have not yet clipped the entry.
2867 saved_start = (start >= entry->start) ? start :
2869 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2870 if (vm_map_unlock_and_wait(map, 0)) {
2872 * Allow interruption of user unwiring?
2876 if (last_timestamp+1 != map->timestamp) {
2878 * Look again for the entry because the map was
2879 * modified while it was unlocked.
2880 * Specifically, the entry may have been
2881 * clipped, merged, or deleted.
2883 if (!vm_map_lookup_entry(map, saved_start,
2885 if (flags & VM_MAP_WIRE_HOLESOK)
2886 tmp_entry = tmp_entry->next;
2888 if (saved_start == start) {
2890 * First_entry has been deleted.
2893 return (KERN_INVALID_ADDRESS);
2896 rv = KERN_INVALID_ADDRESS;
2900 if (entry == first_entry)
2901 first_entry = tmp_entry;
2906 last_timestamp = map->timestamp;
2909 vm_map_clip_start(map, entry, start);
2910 vm_map_clip_end(map, entry, end);
2912 * Mark the entry in case the map lock is released. (See
2915 KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 &&
2916 entry->wiring_thread == NULL,
2917 ("owned map entry %p", entry));
2918 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
2919 entry->wiring_thread = curthread;
2921 * Check the map for holes in the specified region.
2922 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
2924 if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
2925 (entry->end < end && entry->next->start > entry->end)) {
2927 rv = KERN_INVALID_ADDRESS;
2931 * If system unwiring, require that the entry is system wired.
2934 vm_map_entry_system_wired_count(entry) == 0) {
2936 rv = KERN_INVALID_ARGUMENT;
2939 entry = entry->next;
2943 need_wakeup = FALSE;
2944 if (first_entry == NULL) {
2945 result = vm_map_lookup_entry(map, start, &first_entry);
2946 if (!result && (flags & VM_MAP_WIRE_HOLESOK))
2947 first_entry = first_entry->next;
2949 KASSERT(result, ("vm_map_unwire: lookup failed"));
2951 for (entry = first_entry; entry->start < end; entry = entry->next) {
2953 * If VM_MAP_WIRE_HOLESOK was specified, an empty
2954 * space in the unwired region could have been mapped
2955 * while the map lock was dropped for draining
2956 * MAP_ENTRY_IN_TRANSITION. Moreover, another thread
2957 * could be simultaneously wiring this new mapping
2958 * entry. Detect these cases and skip any entries
2959 * marked as in transition by us.
2961 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 ||
2962 entry->wiring_thread != curthread) {
2963 KASSERT((flags & VM_MAP_WIRE_HOLESOK) != 0,
2964 ("vm_map_unwire: !HOLESOK and new/changed entry"));
2968 if (rv == KERN_SUCCESS && (!user_unwire ||
2969 (entry->eflags & MAP_ENTRY_USER_WIRED))) {
2970 if (entry->wired_count == 1)
2971 vm_map_entry_unwire(map, entry);
2973 entry->wired_count--;
2975 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2977 KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0,
2978 ("vm_map_unwire: in-transition flag missing %p", entry));
2979 KASSERT(entry->wiring_thread == curthread,
2980 ("vm_map_unwire: alien wire %p", entry));
2981 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
2982 entry->wiring_thread = NULL;
2983 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
2984 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
2987 vm_map_simplify_entry(map, entry);
2996 vm_map_wire_user_count_sub(u_long npages)
2999 atomic_subtract_long(&vm_user_wire_count, npages);
3003 vm_map_wire_user_count_add(u_long npages)
3007 wired = vm_user_wire_count;
3009 if (npages + wired > vm_page_max_user_wired)
3011 } while (!atomic_fcmpset_long(&vm_user_wire_count, &wired,
3018 * vm_map_wire_entry_failure:
3020 * Handle a wiring failure on the given entry.
3022 * The map should be locked.
3025 vm_map_wire_entry_failure(vm_map_t map, vm_map_entry_t entry,
3026 vm_offset_t failed_addr)
3029 VM_MAP_ASSERT_LOCKED(map);
3030 KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 &&
3031 entry->wired_count == 1,
3032 ("vm_map_wire_entry_failure: entry %p isn't being wired", entry));
3033 KASSERT(failed_addr < entry->end,
3034 ("vm_map_wire_entry_failure: entry %p was fully wired", entry));
3037 * If any pages at the start of this entry were successfully wired,
3040 if (failed_addr > entry->start) {
3041 pmap_unwire(map->pmap, entry->start, failed_addr);
3042 vm_object_unwire(entry->object.vm_object, entry->offset,
3043 failed_addr - entry->start, PQ_ACTIVE);
3047 * Assign an out-of-range value to represent the failure to wire this
3050 entry->wired_count = -1;
3054 vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end, int flags)
3059 rv = vm_map_wire_locked(map, start, end, flags);
3066 * vm_map_wire_locked:
3068 * Implements both kernel and user wiring. Returns with the map locked,
3069 * the map lock may be dropped.
3072 vm_map_wire_locked(vm_map_t map, vm_offset_t start, vm_offset_t end, int flags)
3074 vm_map_entry_t entry, first_entry, tmp_entry;
3075 vm_offset_t faddr, saved_end, saved_start;
3077 u_int last_timestamp;
3079 boolean_t need_wakeup, result, user_wire;
3082 VM_MAP_ASSERT_LOCKED(map);
3085 return (KERN_SUCCESS);
3087 if (flags & VM_MAP_WIRE_WRITE)
3088 prot |= VM_PROT_WRITE;
3089 user_wire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
3090 VM_MAP_RANGE_CHECK(map, start, end);
3091 if (!vm_map_lookup_entry(map, start, &first_entry)) {
3092 if (flags & VM_MAP_WIRE_HOLESOK)
3093 first_entry = first_entry->next;
3095 return (KERN_INVALID_ADDRESS);
3097 last_timestamp = map->timestamp;
3098 entry = first_entry;
3099 while (entry->start < end) {
3100 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
3102 * We have not yet clipped the entry.
3104 saved_start = (start >= entry->start) ? start :
3106 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
3107 if (vm_map_unlock_and_wait(map, 0)) {
3109 * Allow interruption of user wiring?
3113 if (last_timestamp + 1 != map->timestamp) {
3115 * Look again for the entry because the map was
3116 * modified while it was unlocked.
3117 * Specifically, the entry may have been
3118 * clipped, merged, or deleted.
3120 if (!vm_map_lookup_entry(map, saved_start,
3122 if (flags & VM_MAP_WIRE_HOLESOK)
3123 tmp_entry = tmp_entry->next;
3125 if (saved_start == start) {
3127 * first_entry has been deleted.
3129 return (KERN_INVALID_ADDRESS);
3132 rv = KERN_INVALID_ADDRESS;
3136 if (entry == first_entry)
3137 first_entry = tmp_entry;
3142 last_timestamp = map->timestamp;
3145 vm_map_clip_start(map, entry, start);
3146 vm_map_clip_end(map, entry, end);
3148 * Mark the entry in case the map lock is released. (See
3151 KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 &&
3152 entry->wiring_thread == NULL,
3153 ("owned map entry %p", entry));
3154 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
3155 entry->wiring_thread = curthread;
3156 if ((entry->protection & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0
3157 || (entry->protection & prot) != prot) {
3158 entry->eflags |= MAP_ENTRY_WIRE_SKIPPED;
3159 if ((flags & VM_MAP_WIRE_HOLESOK) == 0) {
3161 rv = KERN_INVALID_ADDRESS;
3166 if (entry->wired_count == 0) {
3167 entry->wired_count++;
3169 npages = atop(entry->end - entry->start);
3170 if (user_wire && !vm_map_wire_user_count_add(npages)) {
3171 vm_map_wire_entry_failure(map, entry,
3174 rv = KERN_RESOURCE_SHORTAGE;
3179 * Release the map lock, relying on the in-transition
3180 * mark. Mark the map busy for fork.
3182 saved_start = entry->start;
3183 saved_end = entry->end;
3187 faddr = saved_start;
3190 * Simulate a fault to get the page and enter
3191 * it into the physical map.
3193 if ((rv = vm_fault(map, faddr, VM_PROT_NONE,
3194 VM_FAULT_WIRE)) != KERN_SUCCESS)
3196 } while ((faddr += PAGE_SIZE) < saved_end);
3199 if (last_timestamp + 1 != map->timestamp) {
3201 * Look again for the entry because the map was
3202 * modified while it was unlocked. The entry
3203 * may have been clipped, but NOT merged or
3206 result = vm_map_lookup_entry(map, saved_start,
3208 KASSERT(result, ("vm_map_wire: lookup failed"));
3209 if (entry == first_entry)
3210 first_entry = tmp_entry;
3214 while (entry->end < saved_end) {
3216 * In case of failure, handle entries
3217 * that were not fully wired here;
3218 * fully wired entries are handled
3221 if (rv != KERN_SUCCESS &&
3223 vm_map_wire_entry_failure(map,
3225 entry = entry->next;
3228 last_timestamp = map->timestamp;
3229 if (rv != KERN_SUCCESS) {
3230 vm_map_wire_entry_failure(map, entry, faddr);
3232 vm_map_wire_user_count_sub(npages);
3236 } else if (!user_wire ||
3237 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
3238 entry->wired_count++;
3241 * Check the map for holes in the specified region.
3242 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
3245 if ((flags & VM_MAP_WIRE_HOLESOK) == 0 &&
3246 entry->end < end && entry->next->start > entry->end) {
3248 rv = KERN_INVALID_ADDRESS;
3251 entry = entry->next;
3255 need_wakeup = FALSE;
3256 if (first_entry == NULL) {
3257 result = vm_map_lookup_entry(map, start, &first_entry);
3258 if (!result && (flags & VM_MAP_WIRE_HOLESOK))
3259 first_entry = first_entry->next;
3261 KASSERT(result, ("vm_map_wire: lookup failed"));
3263 for (entry = first_entry; entry->start < end; entry = entry->next) {
3265 * If VM_MAP_WIRE_HOLESOK was specified, an empty
3266 * space in the unwired region could have been mapped
3267 * while the map lock was dropped for faulting in the
3268 * pages or draining MAP_ENTRY_IN_TRANSITION.
3269 * Moreover, another thread could be simultaneously
3270 * wiring this new mapping entry. Detect these cases
3271 * and skip any entries marked as in transition not by us.
3273 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 ||
3274 entry->wiring_thread != curthread) {
3275 KASSERT((flags & VM_MAP_WIRE_HOLESOK) != 0,
3276 ("vm_map_wire: !HOLESOK and new/changed entry"));
3280 if ((entry->eflags & MAP_ENTRY_WIRE_SKIPPED) != 0)
3281 goto next_entry_done;
3283 if (rv == KERN_SUCCESS) {
3285 entry->eflags |= MAP_ENTRY_USER_WIRED;
3286 } else if (entry->wired_count == -1) {
3288 * Wiring failed on this entry. Thus, unwiring is
3291 entry->wired_count = 0;
3292 } else if (!user_wire ||
3293 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
3295 * Undo the wiring. Wiring succeeded on this entry
3296 * but failed on a later entry.
3298 if (entry->wired_count == 1) {
3299 vm_map_entry_unwire(map, entry);
3301 vm_map_wire_user_count_sub(
3302 atop(entry->end - entry->start));
3304 entry->wired_count--;
3307 KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0,
3308 ("vm_map_wire: in-transition flag missing %p", entry));
3309 KASSERT(entry->wiring_thread == curthread,
3310 ("vm_map_wire: alien wire %p", entry));
3311 entry->eflags &= ~(MAP_ENTRY_IN_TRANSITION |
3312 MAP_ENTRY_WIRE_SKIPPED);
3313 entry->wiring_thread = NULL;
3314 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
3315 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
3318 vm_map_simplify_entry(map, entry);
3328 * Push any dirty cached pages in the address range to their pager.
3329 * If syncio is TRUE, dirty pages are written synchronously.
3330 * If invalidate is TRUE, any cached pages are freed as well.
3332 * If the size of the region from start to end is zero, we are
3333 * supposed to flush all modified pages within the region containing
3334 * start. Unfortunately, a region can be split or coalesced with
3335 * neighboring regions, making it difficult to determine what the
3336 * original region was. Therefore, we approximate this requirement by
3337 * flushing the current region containing start.
3339 * Returns an error if any part of the specified range is not mapped.
3347 boolean_t invalidate)
3349 vm_map_entry_t current;
3350 vm_map_entry_t entry;
3353 vm_ooffset_t offset;
3354 unsigned int last_timestamp;
3357 vm_map_lock_read(map);
3358 VM_MAP_RANGE_CHECK(map, start, end);
3359 if (!vm_map_lookup_entry(map, start, &entry)) {
3360 vm_map_unlock_read(map);
3361 return (KERN_INVALID_ADDRESS);
3362 } else if (start == end) {
3363 start = entry->start;
3367 * Make a first pass to check for user-wired memory and holes.
3369 for (current = entry; current->start < end; current = current->next) {
3370 if (invalidate && (current->eflags & MAP_ENTRY_USER_WIRED)) {
3371 vm_map_unlock_read(map);
3372 return (KERN_INVALID_ARGUMENT);
3374 if (end > current->end &&
3375 current->end != current->next->start) {
3376 vm_map_unlock_read(map);
3377 return (KERN_INVALID_ADDRESS);
3382 pmap_remove(map->pmap, start, end);
3386 * Make a second pass, cleaning/uncaching pages from the indicated
3389 for (current = entry; current->start < end;) {
3390 offset = current->offset + (start - current->start);
3391 size = (end <= current->end ? end : current->end) - start;
3392 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
3394 vm_map_entry_t tentry;
3397 smap = current->object.sub_map;
3398 vm_map_lock_read(smap);
3399 (void) vm_map_lookup_entry(smap, offset, &tentry);
3400 tsize = tentry->end - offset;
3403 object = tentry->object.vm_object;
3404 offset = tentry->offset + (offset - tentry->start);
3405 vm_map_unlock_read(smap);
3407 object = current->object.vm_object;
3409 vm_object_reference(object);
3410 last_timestamp = map->timestamp;
3411 vm_map_unlock_read(map);
3412 if (!vm_object_sync(object, offset, size, syncio, invalidate))
3415 vm_object_deallocate(object);
3416 vm_map_lock_read(map);
3417 if (last_timestamp == map->timestamp ||
3418 !vm_map_lookup_entry(map, start, ¤t))
3419 current = current->next;
3422 vm_map_unlock_read(map);
3423 return (failed ? KERN_FAILURE : KERN_SUCCESS);
3427 * vm_map_entry_unwire: [ internal use only ]
3429 * Make the region specified by this entry pageable.
3431 * The map in question should be locked.
3432 * [This is the reason for this routine's existence.]
3435 vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
3439 VM_MAP_ASSERT_LOCKED(map);
3440 KASSERT(entry->wired_count > 0,
3441 ("vm_map_entry_unwire: entry %p isn't wired", entry));
3443 size = entry->end - entry->start;
3444 if ((entry->eflags & MAP_ENTRY_USER_WIRED) != 0)
3445 vm_map_wire_user_count_sub(atop(size));
3446 pmap_unwire(map->pmap, entry->start, entry->end);
3447 vm_object_unwire(entry->object.vm_object, entry->offset, size,
3449 entry->wired_count = 0;
3453 vm_map_entry_deallocate(vm_map_entry_t entry, boolean_t system_map)
3456 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0)
3457 vm_object_deallocate(entry->object.vm_object);
3458 uma_zfree(system_map ? kmapentzone : mapentzone, entry);
3462 * vm_map_entry_delete: [ internal use only ]
3464 * Deallocate the given entry from the target map.
3467 vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry)
3470 vm_pindex_t offidxstart, offidxend, count, size1;
3473 vm_map_entry_unlink(map, entry, UNLINK_MERGE_NONE);
3474 object = entry->object.vm_object;
3476 if ((entry->eflags & MAP_ENTRY_GUARD) != 0) {
3477 MPASS(entry->cred == NULL);
3478 MPASS((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0);
3479 MPASS(object == NULL);
3480 vm_map_entry_deallocate(entry, map->system_map);
3484 size = entry->end - entry->start;
3487 if (entry->cred != NULL) {
3488 swap_release_by_cred(size, entry->cred);
3489 crfree(entry->cred);
3492 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
3494 KASSERT(entry->cred == NULL || object->cred == NULL ||
3495 (entry->eflags & MAP_ENTRY_NEEDS_COPY),
3496 ("OVERCOMMIT vm_map_entry_delete: both cred %p", entry));
3498 offidxstart = OFF_TO_IDX(entry->offset);
3499 offidxend = offidxstart + count;
3500 VM_OBJECT_WLOCK(object);
3501 if (object->ref_count != 1 && ((object->flags & (OBJ_NOSPLIT |
3502 OBJ_ONEMAPPING)) == OBJ_ONEMAPPING ||
3503 object == kernel_object)) {
3504 vm_object_collapse(object);
3507 * The option OBJPR_NOTMAPPED can be passed here
3508 * because vm_map_delete() already performed
3509 * pmap_remove() on the only mapping to this range
3512 vm_object_page_remove(object, offidxstart, offidxend,
3514 if (object->type == OBJT_SWAP)
3515 swap_pager_freespace(object, offidxstart,
3517 if (offidxend >= object->size &&
3518 offidxstart < object->size) {
3519 size1 = object->size;
3520 object->size = offidxstart;
3521 if (object->cred != NULL) {
3522 size1 -= object->size;
3523 KASSERT(object->charge >= ptoa(size1),
3524 ("object %p charge < 0", object));
3525 swap_release_by_cred(ptoa(size1),
3527 object->charge -= ptoa(size1);
3531 VM_OBJECT_WUNLOCK(object);
3533 entry->object.vm_object = NULL;
3534 if (map->system_map)
3535 vm_map_entry_deallocate(entry, TRUE);
3537 entry->next = curthread->td_map_def_user;
3538 curthread->td_map_def_user = entry;
3543 * vm_map_delete: [ internal use only ]
3545 * Deallocates the given address range from the target
3549 vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end)
3551 vm_map_entry_t entry;
3552 vm_map_entry_t first_entry;
3554 VM_MAP_ASSERT_LOCKED(map);
3556 return (KERN_SUCCESS);
3559 * Find the start of the region, and clip it
3561 if (!vm_map_lookup_entry(map, start, &first_entry))
3562 entry = first_entry->next;
3564 entry = first_entry;
3565 vm_map_clip_start(map, entry, start);
3569 * Step through all entries in this region
3571 while (entry->start < end) {
3572 vm_map_entry_t next;
3575 * Wait for wiring or unwiring of an entry to complete.
3576 * Also wait for any system wirings to disappear on
3579 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 ||
3580 (vm_map_pmap(map) != kernel_pmap &&
3581 vm_map_entry_system_wired_count(entry) != 0)) {
3582 unsigned int last_timestamp;
3583 vm_offset_t saved_start;
3584 vm_map_entry_t tmp_entry;
3586 saved_start = entry->start;
3587 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
3588 last_timestamp = map->timestamp;
3589 (void) vm_map_unlock_and_wait(map, 0);
3591 if (last_timestamp + 1 != map->timestamp) {
3593 * Look again for the entry because the map was
3594 * modified while it was unlocked.
3595 * Specifically, the entry may have been
3596 * clipped, merged, or deleted.
3598 if (!vm_map_lookup_entry(map, saved_start,
3600 entry = tmp_entry->next;
3603 vm_map_clip_start(map, entry,
3609 vm_map_clip_end(map, entry, end);
3614 * Unwire before removing addresses from the pmap; otherwise,
3615 * unwiring will put the entries back in the pmap.
3617 if (entry->wired_count != 0)
3618 vm_map_entry_unwire(map, entry);
3621 * Remove mappings for the pages, but only if the
3622 * mappings could exist. For instance, it does not
3623 * make sense to call pmap_remove() for guard entries.
3625 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0 ||
3626 entry->object.vm_object != NULL)
3627 pmap_remove(map->pmap, entry->start, entry->end);
3629 if (entry->end == map->anon_loc)
3630 map->anon_loc = entry->start;
3633 * Delete the entry only after removing all pmap
3634 * entries pointing to its pages. (Otherwise, its
3635 * page frames may be reallocated, and any modify bits
3636 * will be set in the wrong object!)
3638 vm_map_entry_delete(map, entry);
3641 return (KERN_SUCCESS);
3647 * Remove the given address range from the target map.
3648 * This is the exported form of vm_map_delete.
3651 vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
3656 VM_MAP_RANGE_CHECK(map, start, end);
3657 result = vm_map_delete(map, start, end);
3663 * vm_map_check_protection:
3665 * Assert that the target map allows the specified privilege on the
3666 * entire address region given. The entire region must be allocated.
3668 * WARNING! This code does not and should not check whether the
3669 * contents of the region is accessible. For example a smaller file
3670 * might be mapped into a larger address space.
3672 * NOTE! This code is also called by munmap().
3674 * The map must be locked. A read lock is sufficient.
3677 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
3678 vm_prot_t protection)
3680 vm_map_entry_t entry;
3681 vm_map_entry_t tmp_entry;
3683 if (!vm_map_lookup_entry(map, start, &tmp_entry))
3687 while (start < end) {
3691 if (start < entry->start)
3694 * Check protection associated with entry.
3696 if ((entry->protection & protection) != protection)
3698 /* go to next entry */
3700 entry = entry->next;
3706 * vm_map_copy_entry:
3708 * Copies the contents of the source entry to the destination
3709 * entry. The entries *must* be aligned properly.
3715 vm_map_entry_t src_entry,
3716 vm_map_entry_t dst_entry,
3717 vm_ooffset_t *fork_charge)
3719 vm_object_t src_object;
3720 vm_map_entry_t fake_entry;
3725 VM_MAP_ASSERT_LOCKED(dst_map);
3727 if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
3730 if (src_entry->wired_count == 0 ||
3731 (src_entry->protection & VM_PROT_WRITE) == 0) {
3733 * If the source entry is marked needs_copy, it is already
3736 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0 &&
3737 (src_entry->protection & VM_PROT_WRITE) != 0) {
3738 pmap_protect(src_map->pmap,
3741 src_entry->protection & ~VM_PROT_WRITE);
3745 * Make a copy of the object.
3747 size = src_entry->end - src_entry->start;
3748 if ((src_object = src_entry->object.vm_object) != NULL) {
3749 VM_OBJECT_WLOCK(src_object);
3750 charged = ENTRY_CHARGED(src_entry);
3751 if (src_object->handle == NULL &&
3752 (src_object->type == OBJT_DEFAULT ||
3753 src_object->type == OBJT_SWAP)) {
3754 vm_object_collapse(src_object);
3755 if ((src_object->flags & (OBJ_NOSPLIT |
3756 OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
3757 vm_object_split(src_entry);
3759 src_entry->object.vm_object;
3762 vm_object_reference_locked(src_object);
3763 vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
3764 if (src_entry->cred != NULL &&
3765 !(src_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
3766 KASSERT(src_object->cred == NULL,
3767 ("OVERCOMMIT: vm_map_copy_entry: cred %p",
3769 src_object->cred = src_entry->cred;
3770 src_object->charge = size;
3772 VM_OBJECT_WUNLOCK(src_object);
3773 dst_entry->object.vm_object = src_object;
3775 cred = curthread->td_ucred;
3777 dst_entry->cred = cred;
3778 *fork_charge += size;
3779 if (!(src_entry->eflags &
3780 MAP_ENTRY_NEEDS_COPY)) {
3782 src_entry->cred = cred;
3783 *fork_charge += size;
3786 src_entry->eflags |= MAP_ENTRY_COW |
3787 MAP_ENTRY_NEEDS_COPY;
3788 dst_entry->eflags |= MAP_ENTRY_COW |
3789 MAP_ENTRY_NEEDS_COPY;
3790 dst_entry->offset = src_entry->offset;
3791 if (src_entry->eflags & MAP_ENTRY_VN_WRITECNT) {
3793 * MAP_ENTRY_VN_WRITECNT cannot
3794 * indicate write reference from
3795 * src_entry, since the entry is
3796 * marked as needs copy. Allocate a
3797 * fake entry that is used to
3798 * decrement object->un_pager.vnp.writecount
3799 * at the appropriate time. Attach
3800 * fake_entry to the deferred list.
3802 fake_entry = vm_map_entry_create(dst_map);
3803 fake_entry->eflags = MAP_ENTRY_VN_WRITECNT;
3804 src_entry->eflags &= ~MAP_ENTRY_VN_WRITECNT;
3805 vm_object_reference(src_object);
3806 fake_entry->object.vm_object = src_object;
3807 fake_entry->start = src_entry->start;
3808 fake_entry->end = src_entry->end;
3809 fake_entry->next = curthread->td_map_def_user;
3810 curthread->td_map_def_user = fake_entry;
3813 pmap_copy(dst_map->pmap, src_map->pmap,
3814 dst_entry->start, dst_entry->end - dst_entry->start,
3817 dst_entry->object.vm_object = NULL;
3818 dst_entry->offset = 0;
3819 if (src_entry->cred != NULL) {
3820 dst_entry->cred = curthread->td_ucred;
3821 crhold(dst_entry->cred);
3822 *fork_charge += size;
3827 * We don't want to make writeable wired pages copy-on-write.
3828 * Immediately copy these pages into the new map by simulating
3829 * page faults. The new pages are pageable.
3831 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry,
3837 * vmspace_map_entry_forked:
3838 * Update the newly-forked vmspace each time a map entry is inherited
3839 * or copied. The values for vm_dsize and vm_tsize are approximate
3840 * (and mostly-obsolete ideas in the face of mmap(2) et al.)
3843 vmspace_map_entry_forked(const struct vmspace *vm1, struct vmspace *vm2,
3844 vm_map_entry_t entry)
3846 vm_size_t entrysize;
3849 if ((entry->eflags & MAP_ENTRY_GUARD) != 0)
3851 entrysize = entry->end - entry->start;
3852 vm2->vm_map.size += entrysize;
3853 if (entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP)) {
3854 vm2->vm_ssize += btoc(entrysize);
3855 } else if (entry->start >= (vm_offset_t)vm1->vm_daddr &&
3856 entry->start < (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)) {
3857 newend = MIN(entry->end,
3858 (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize));
3859 vm2->vm_dsize += btoc(newend - entry->start);
3860 } else if (entry->start >= (vm_offset_t)vm1->vm_taddr &&
3861 entry->start < (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)) {
3862 newend = MIN(entry->end,
3863 (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize));
3864 vm2->vm_tsize += btoc(newend - entry->start);
3870 * Create a new process vmspace structure and vm_map
3871 * based on those of an existing process. The new map
3872 * is based on the old map, according to the inheritance
3873 * values on the regions in that map.
3875 * XXX It might be worth coalescing the entries added to the new vmspace.
3877 * The source map must not be locked.
3880 vmspace_fork(struct vmspace *vm1, vm_ooffset_t *fork_charge)
3882 struct vmspace *vm2;
3883 vm_map_t new_map, old_map;
3884 vm_map_entry_t new_entry, old_entry;
3889 old_map = &vm1->vm_map;
3890 /* Copy immutable fields of vm1 to vm2. */
3891 vm2 = vmspace_alloc(vm_map_min(old_map), vm_map_max(old_map),
3896 vm2->vm_taddr = vm1->vm_taddr;
3897 vm2->vm_daddr = vm1->vm_daddr;
3898 vm2->vm_maxsaddr = vm1->vm_maxsaddr;
3899 vm_map_lock(old_map);
3901 vm_map_wait_busy(old_map);
3902 new_map = &vm2->vm_map;
3903 locked = vm_map_trylock(new_map); /* trylock to silence WITNESS */
3904 KASSERT(locked, ("vmspace_fork: lock failed"));
3906 error = pmap_vmspace_copy(new_map->pmap, old_map->pmap);
3908 sx_xunlock(&old_map->lock);
3909 sx_xunlock(&new_map->lock);
3910 vm_map_process_deferred();
3915 new_map->anon_loc = old_map->anon_loc;
3917 old_entry = old_map->header.next;
3919 while (old_entry != &old_map->header) {
3920 if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
3921 panic("vm_map_fork: encountered a submap");
3923 inh = old_entry->inheritance;
3924 if ((old_entry->eflags & MAP_ENTRY_GUARD) != 0 &&
3925 inh != VM_INHERIT_NONE)
3926 inh = VM_INHERIT_COPY;
3929 case VM_INHERIT_NONE:
3932 case VM_INHERIT_SHARE:
3934 * Clone the entry, creating the shared object if necessary.
3936 object = old_entry->object.vm_object;
3937 if (object == NULL) {
3938 object = vm_object_allocate(OBJT_DEFAULT,
3939 atop(old_entry->end - old_entry->start));
3940 old_entry->object.vm_object = object;
3941 old_entry->offset = 0;
3942 if (old_entry->cred != NULL) {
3943 object->cred = old_entry->cred;
3944 object->charge = old_entry->end -
3946 old_entry->cred = NULL;
3951 * Add the reference before calling vm_object_shadow
3952 * to insure that a shadow object is created.
3954 vm_object_reference(object);
3955 if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3956 vm_object_shadow(&old_entry->object.vm_object,
3958 old_entry->end - old_entry->start);
3959 old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
3960 /* Transfer the second reference too. */
3961 vm_object_reference(
3962 old_entry->object.vm_object);
3965 * As in vm_map_simplify_entry(), the
3966 * vnode lock will not be acquired in
3967 * this call to vm_object_deallocate().
3969 vm_object_deallocate(object);
3970 object = old_entry->object.vm_object;
3972 VM_OBJECT_WLOCK(object);
3973 vm_object_clear_flag(object, OBJ_ONEMAPPING);
3974 if (old_entry->cred != NULL) {
3975 KASSERT(object->cred == NULL, ("vmspace_fork both cred"));
3976 object->cred = old_entry->cred;
3977 object->charge = old_entry->end - old_entry->start;
3978 old_entry->cred = NULL;
3982 * Assert the correct state of the vnode
3983 * v_writecount while the object is locked, to
3984 * not relock it later for the assertion
3987 if (old_entry->eflags & MAP_ENTRY_VN_WRITECNT &&
3988 object->type == OBJT_VNODE) {
3989 KASSERT(((struct vnode *)object->handle)->
3991 ("vmspace_fork: v_writecount %p", object));
3992 KASSERT(object->un_pager.vnp.writemappings > 0,
3993 ("vmspace_fork: vnp.writecount %p",
3996 VM_OBJECT_WUNLOCK(object);
3999 * Clone the entry, referencing the shared object.
4001 new_entry = vm_map_entry_create(new_map);
4002 *new_entry = *old_entry;
4003 new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
4004 MAP_ENTRY_IN_TRANSITION);
4005 new_entry->wiring_thread = NULL;
4006 new_entry->wired_count = 0;
4007 if (new_entry->eflags & MAP_ENTRY_VN_WRITECNT) {
4008 vnode_pager_update_writecount(object,
4009 new_entry->start, new_entry->end);
4011 vm_map_entry_set_vnode_text(new_entry, true);
4014 * Insert the entry into the new map -- we know we're
4015 * inserting at the end of the new map.
4017 vm_map_entry_link(new_map, new_entry);
4018 vmspace_map_entry_forked(vm1, vm2, new_entry);
4021 * Update the physical map
4023 pmap_copy(new_map->pmap, old_map->pmap,
4025 (old_entry->end - old_entry->start),
4029 case VM_INHERIT_COPY:
4031 * Clone the entry and link into the map.
4033 new_entry = vm_map_entry_create(new_map);
4034 *new_entry = *old_entry;
4036 * Copied entry is COW over the old object.
4038 new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
4039 MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_VN_WRITECNT);
4040 new_entry->wiring_thread = NULL;
4041 new_entry->wired_count = 0;
4042 new_entry->object.vm_object = NULL;
4043 new_entry->cred = NULL;
4044 vm_map_entry_link(new_map, new_entry);
4045 vmspace_map_entry_forked(vm1, vm2, new_entry);
4046 vm_map_copy_entry(old_map, new_map, old_entry,
4047 new_entry, fork_charge);
4048 vm_map_entry_set_vnode_text(new_entry, true);
4051 case VM_INHERIT_ZERO:
4053 * Create a new anonymous mapping entry modelled from
4056 new_entry = vm_map_entry_create(new_map);
4057 memset(new_entry, 0, sizeof(*new_entry));
4059 new_entry->start = old_entry->start;
4060 new_entry->end = old_entry->end;
4061 new_entry->eflags = old_entry->eflags &
4062 ~(MAP_ENTRY_USER_WIRED | MAP_ENTRY_IN_TRANSITION |
4063 MAP_ENTRY_VN_WRITECNT | MAP_ENTRY_VN_EXEC);
4064 new_entry->protection = old_entry->protection;
4065 new_entry->max_protection = old_entry->max_protection;
4066 new_entry->inheritance = VM_INHERIT_ZERO;
4068 vm_map_entry_link(new_map, new_entry);
4069 vmspace_map_entry_forked(vm1, vm2, new_entry);
4071 new_entry->cred = curthread->td_ucred;
4072 crhold(new_entry->cred);
4073 *fork_charge += (new_entry->end - new_entry->start);
4077 old_entry = old_entry->next;
4080 * Use inlined vm_map_unlock() to postpone handling the deferred
4081 * map entries, which cannot be done until both old_map and
4082 * new_map locks are released.
4084 sx_xunlock(&old_map->lock);
4085 sx_xunlock(&new_map->lock);
4086 vm_map_process_deferred();
4092 * Create a process's stack for exec_new_vmspace(). This function is never
4093 * asked to wire the newly created stack.
4096 vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
4097 vm_prot_t prot, vm_prot_t max, int cow)
4099 vm_size_t growsize, init_ssize;
4103 MPASS((map->flags & MAP_WIREFUTURE) == 0);
4104 growsize = sgrowsiz;
4105 init_ssize = (max_ssize < growsize) ? max_ssize : growsize;
4107 vmemlim = lim_cur(curthread, RLIMIT_VMEM);
4108 /* If we would blow our VMEM resource limit, no go */
4109 if (map->size + init_ssize > vmemlim) {
4113 rv = vm_map_stack_locked(map, addrbos, max_ssize, growsize, prot,
4120 static int stack_guard_page = 1;
4121 SYSCTL_INT(_security_bsd, OID_AUTO, stack_guard_page, CTLFLAG_RWTUN,
4122 &stack_guard_page, 0,
4123 "Specifies the number of guard pages for a stack that grows");
4126 vm_map_stack_locked(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
4127 vm_size_t growsize, vm_prot_t prot, vm_prot_t max, int cow)
4129 vm_map_entry_t new_entry, prev_entry;
4130 vm_offset_t bot, gap_bot, gap_top, top;
4131 vm_size_t init_ssize, sgp;
4135 * The stack orientation is piggybacked with the cow argument.
4136 * Extract it into orient and mask the cow argument so that we
4137 * don't pass it around further.
4139 orient = cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP);
4140 KASSERT(orient != 0, ("No stack grow direction"));
4141 KASSERT(orient != (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP),
4144 if (addrbos < vm_map_min(map) ||
4145 addrbos + max_ssize > vm_map_max(map) ||
4146 addrbos + max_ssize <= addrbos)
4147 return (KERN_INVALID_ADDRESS);
4148 sgp = (vm_size_t)stack_guard_page * PAGE_SIZE;
4149 if (sgp >= max_ssize)
4150 return (KERN_INVALID_ARGUMENT);
4152 init_ssize = growsize;
4153 if (max_ssize < init_ssize + sgp)
4154 init_ssize = max_ssize - sgp;
4156 /* If addr is already mapped, no go */
4157 if (vm_map_lookup_entry(map, addrbos, &prev_entry))
4158 return (KERN_NO_SPACE);
4161 * If we can't accommodate max_ssize in the current mapping, no go.
4163 if (prev_entry->next->start < addrbos + max_ssize)
4164 return (KERN_NO_SPACE);
4167 * We initially map a stack of only init_ssize. We will grow as
4168 * needed later. Depending on the orientation of the stack (i.e.
4169 * the grow direction) we either map at the top of the range, the
4170 * bottom of the range or in the middle.
4172 * Note: we would normally expect prot and max to be VM_PROT_ALL,
4173 * and cow to be 0. Possibly we should eliminate these as input
4174 * parameters, and just pass these values here in the insert call.
4176 if (orient == MAP_STACK_GROWS_DOWN) {
4177 bot = addrbos + max_ssize - init_ssize;
4178 top = bot + init_ssize;
4181 } else /* if (orient == MAP_STACK_GROWS_UP) */ {
4183 top = bot + init_ssize;
4185 gap_top = addrbos + max_ssize;
4187 rv = vm_map_insert(map, NULL, 0, bot, top, prot, max, cow);
4188 if (rv != KERN_SUCCESS)
4190 new_entry = prev_entry->next;
4191 KASSERT(new_entry->end == top || new_entry->start == bot,
4192 ("Bad entry start/end for new stack entry"));
4193 KASSERT((orient & MAP_STACK_GROWS_DOWN) == 0 ||
4194 (new_entry->eflags & MAP_ENTRY_GROWS_DOWN) != 0,
4195 ("new entry lacks MAP_ENTRY_GROWS_DOWN"));
4196 KASSERT((orient & MAP_STACK_GROWS_UP) == 0 ||
4197 (new_entry->eflags & MAP_ENTRY_GROWS_UP) != 0,
4198 ("new entry lacks MAP_ENTRY_GROWS_UP"));
4199 rv = vm_map_insert(map, NULL, 0, gap_bot, gap_top, VM_PROT_NONE,
4200 VM_PROT_NONE, MAP_CREATE_GUARD | (orient == MAP_STACK_GROWS_DOWN ?
4201 MAP_CREATE_STACK_GAP_DN : MAP_CREATE_STACK_GAP_UP));
4202 if (rv != KERN_SUCCESS)
4203 (void)vm_map_delete(map, bot, top);
4208 * Attempts to grow a vm stack entry. Returns KERN_SUCCESS if we
4209 * successfully grow the stack.
4212 vm_map_growstack(vm_map_t map, vm_offset_t addr, vm_map_entry_t gap_entry)
4214 vm_map_entry_t stack_entry;
4218 vm_offset_t gap_end, gap_start, grow_start;
4219 vm_size_t grow_amount, guard, max_grow;
4220 rlim_t lmemlim, stacklim, vmemlim;
4222 bool gap_deleted, grow_down, is_procstack;
4234 * Disallow stack growth when the access is performed by a
4235 * debugger or AIO daemon. The reason is that the wrong
4236 * resource limits are applied.
4238 if (map != &p->p_vmspace->vm_map || p->p_textvp == NULL)
4239 return (KERN_FAILURE);
4241 MPASS(!map->system_map);
4243 guard = stack_guard_page * PAGE_SIZE;
4244 lmemlim = lim_cur(curthread, RLIMIT_MEMLOCK);
4245 stacklim = lim_cur(curthread, RLIMIT_STACK);
4246 vmemlim = lim_cur(curthread, RLIMIT_VMEM);
4248 /* If addr is not in a hole for a stack grow area, no need to grow. */
4249 if (gap_entry == NULL && !vm_map_lookup_entry(map, addr, &gap_entry))
4250 return (KERN_FAILURE);
4251 if ((gap_entry->eflags & MAP_ENTRY_GUARD) == 0)
4252 return (KERN_SUCCESS);
4253 if ((gap_entry->eflags & MAP_ENTRY_STACK_GAP_DN) != 0) {
4254 stack_entry = gap_entry->next;
4255 if ((stack_entry->eflags & MAP_ENTRY_GROWS_DOWN) == 0 ||
4256 stack_entry->start != gap_entry->end)
4257 return (KERN_FAILURE);
4258 grow_amount = round_page(stack_entry->start - addr);
4260 } else if ((gap_entry->eflags & MAP_ENTRY_STACK_GAP_UP) != 0) {
4261 stack_entry = gap_entry->prev;
4262 if ((stack_entry->eflags & MAP_ENTRY_GROWS_UP) == 0 ||
4263 stack_entry->end != gap_entry->start)
4264 return (KERN_FAILURE);
4265 grow_amount = round_page(addr + 1 - stack_entry->end);
4268 return (KERN_FAILURE);
4270 max_grow = gap_entry->end - gap_entry->start;
4271 if (guard > max_grow)
4272 return (KERN_NO_SPACE);
4274 if (grow_amount > max_grow)
4275 return (KERN_NO_SPACE);
4278 * If this is the main process stack, see if we're over the stack
4281 is_procstack = addr >= (vm_offset_t)vm->vm_maxsaddr &&
4282 addr < (vm_offset_t)p->p_sysent->sv_usrstack;
4283 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim))
4284 return (KERN_NO_SPACE);
4289 if (is_procstack && racct_set(p, RACCT_STACK,
4290 ctob(vm->vm_ssize) + grow_amount)) {
4292 return (KERN_NO_SPACE);
4298 grow_amount = roundup(grow_amount, sgrowsiz);
4299 if (grow_amount > max_grow)
4300 grow_amount = max_grow;
4301 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
4302 grow_amount = trunc_page((vm_size_t)stacklim) -
4308 limit = racct_get_available(p, RACCT_STACK);
4310 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > limit))
4311 grow_amount = limit - ctob(vm->vm_ssize);
4314 if (!old_mlock && (map->flags & MAP_WIREFUTURE) != 0) {
4315 if (ptoa(pmap_wired_count(map->pmap)) + grow_amount > lmemlim) {
4322 if (racct_set(p, RACCT_MEMLOCK,
4323 ptoa(pmap_wired_count(map->pmap)) + grow_amount)) {
4333 /* If we would blow our VMEM resource limit, no go */
4334 if (map->size + grow_amount > vmemlim) {
4341 if (racct_set(p, RACCT_VMEM, map->size + grow_amount)) {
4350 if (vm_map_lock_upgrade(map)) {
4352 vm_map_lock_read(map);
4357 grow_start = gap_entry->end - grow_amount;
4358 if (gap_entry->start + grow_amount == gap_entry->end) {
4359 gap_start = gap_entry->start;
4360 gap_end = gap_entry->end;
4361 vm_map_entry_delete(map, gap_entry);
4364 MPASS(gap_entry->start < gap_entry->end - grow_amount);
4365 vm_map_entry_resize(map, gap_entry, -grow_amount);
4366 gap_deleted = false;
4368 rv = vm_map_insert(map, NULL, 0, grow_start,
4369 grow_start + grow_amount,
4370 stack_entry->protection, stack_entry->max_protection,
4371 MAP_STACK_GROWS_DOWN);
4372 if (rv != KERN_SUCCESS) {
4374 rv1 = vm_map_insert(map, NULL, 0, gap_start,
4375 gap_end, VM_PROT_NONE, VM_PROT_NONE,
4376 MAP_CREATE_GUARD | MAP_CREATE_STACK_GAP_DN);
4377 MPASS(rv1 == KERN_SUCCESS);
4379 vm_map_entry_resize(map, gap_entry,
4383 grow_start = stack_entry->end;
4384 cred = stack_entry->cred;
4385 if (cred == NULL && stack_entry->object.vm_object != NULL)
4386 cred = stack_entry->object.vm_object->cred;
4387 if (cred != NULL && !swap_reserve_by_cred(grow_amount, cred))
4389 /* Grow the underlying object if applicable. */
4390 else if (stack_entry->object.vm_object == NULL ||
4391 vm_object_coalesce(stack_entry->object.vm_object,
4392 stack_entry->offset,
4393 (vm_size_t)(stack_entry->end - stack_entry->start),
4394 grow_amount, cred != NULL)) {
4395 if (gap_entry->start + grow_amount == gap_entry->end) {
4396 vm_map_entry_delete(map, gap_entry);
4397 vm_map_entry_resize(map, stack_entry,
4400 gap_entry->start += grow_amount;
4401 stack_entry->end += grow_amount;
4403 map->size += grow_amount;
4408 if (rv == KERN_SUCCESS && is_procstack)
4409 vm->vm_ssize += btoc(grow_amount);
4412 * Heed the MAP_WIREFUTURE flag if it was set for this process.
4414 if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE) != 0) {
4415 rv = vm_map_wire_locked(map, grow_start,
4416 grow_start + grow_amount,
4417 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
4419 vm_map_lock_downgrade(map);
4423 if (racct_enable && rv != KERN_SUCCESS) {
4425 error = racct_set(p, RACCT_VMEM, map->size);
4426 KASSERT(error == 0, ("decreasing RACCT_VMEM failed"));
4428 error = racct_set(p, RACCT_MEMLOCK,
4429 ptoa(pmap_wired_count(map->pmap)));
4430 KASSERT(error == 0, ("decreasing RACCT_MEMLOCK failed"));
4432 error = racct_set(p, RACCT_STACK, ctob(vm->vm_ssize));
4433 KASSERT(error == 0, ("decreasing RACCT_STACK failed"));
4442 * Unshare the specified VM space for exec. If other processes are
4443 * mapped to it, then create a new one. The new vmspace is null.
4446 vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser)
4448 struct vmspace *oldvmspace = p->p_vmspace;
4449 struct vmspace *newvmspace;
4451 KASSERT((curthread->td_pflags & TDP_EXECVMSPC) == 0,
4452 ("vmspace_exec recursed"));
4453 newvmspace = vmspace_alloc(minuser, maxuser, pmap_pinit);
4454 if (newvmspace == NULL)
4456 newvmspace->vm_swrss = oldvmspace->vm_swrss;
4458 * This code is written like this for prototype purposes. The
4459 * goal is to avoid running down the vmspace here, but let the
4460 * other process's that are still using the vmspace to finally
4461 * run it down. Even though there is little or no chance of blocking
4462 * here, it is a good idea to keep this form for future mods.
4464 PROC_VMSPACE_LOCK(p);
4465 p->p_vmspace = newvmspace;
4466 PROC_VMSPACE_UNLOCK(p);
4467 if (p == curthread->td_proc)
4468 pmap_activate(curthread);
4469 curthread->td_pflags |= TDP_EXECVMSPC;
4474 * Unshare the specified VM space for forcing COW. This
4475 * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
4478 vmspace_unshare(struct proc *p)
4480 struct vmspace *oldvmspace = p->p_vmspace;
4481 struct vmspace *newvmspace;
4482 vm_ooffset_t fork_charge;
4484 if (oldvmspace->vm_refcnt == 1)
4487 newvmspace = vmspace_fork(oldvmspace, &fork_charge);
4488 if (newvmspace == NULL)
4490 if (!swap_reserve_by_cred(fork_charge, p->p_ucred)) {
4491 vmspace_free(newvmspace);
4494 PROC_VMSPACE_LOCK(p);
4495 p->p_vmspace = newvmspace;
4496 PROC_VMSPACE_UNLOCK(p);
4497 if (p == curthread->td_proc)
4498 pmap_activate(curthread);
4499 vmspace_free(oldvmspace);
4506 * Finds the VM object, offset, and
4507 * protection for a given virtual address in the
4508 * specified map, assuming a page fault of the
4511 * Leaves the map in question locked for read; return
4512 * values are guaranteed until a vm_map_lookup_done
4513 * call is performed. Note that the map argument
4514 * is in/out; the returned map must be used in
4515 * the call to vm_map_lookup_done.
4517 * A handle (out_entry) is returned for use in
4518 * vm_map_lookup_done, to make that fast.
4520 * If a lookup is requested with "write protection"
4521 * specified, the map may be changed to perform virtual
4522 * copying operations, although the data referenced will
4526 vm_map_lookup(vm_map_t *var_map, /* IN/OUT */
4528 vm_prot_t fault_typea,
4529 vm_map_entry_t *out_entry, /* OUT */
4530 vm_object_t *object, /* OUT */
4531 vm_pindex_t *pindex, /* OUT */
4532 vm_prot_t *out_prot, /* OUT */
4533 boolean_t *wired) /* OUT */
4535 vm_map_entry_t entry;
4536 vm_map_t map = *var_map;
4538 vm_prot_t fault_type = fault_typea;
4539 vm_object_t eobject;
4545 vm_map_lock_read(map);
4549 * Lookup the faulting address.
4551 if (!vm_map_lookup_entry(map, vaddr, out_entry)) {
4552 vm_map_unlock_read(map);
4553 return (KERN_INVALID_ADDRESS);
4561 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
4562 vm_map_t old_map = map;
4564 *var_map = map = entry->object.sub_map;
4565 vm_map_unlock_read(old_map);
4570 * Check whether this task is allowed to have this page.
4572 prot = entry->protection;
4573 if ((fault_typea & VM_PROT_FAULT_LOOKUP) != 0) {
4574 fault_typea &= ~VM_PROT_FAULT_LOOKUP;
4575 if (prot == VM_PROT_NONE && map != kernel_map &&
4576 (entry->eflags & MAP_ENTRY_GUARD) != 0 &&
4577 (entry->eflags & (MAP_ENTRY_STACK_GAP_DN |
4578 MAP_ENTRY_STACK_GAP_UP)) != 0 &&
4579 vm_map_growstack(map, vaddr, entry) == KERN_SUCCESS)
4580 goto RetryLookupLocked;
4582 fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
4583 if ((fault_type & prot) != fault_type || prot == VM_PROT_NONE) {
4584 vm_map_unlock_read(map);
4585 return (KERN_PROTECTION_FAILURE);
4587 KASSERT((prot & VM_PROT_WRITE) == 0 || (entry->eflags &
4588 (MAP_ENTRY_USER_WIRED | MAP_ENTRY_NEEDS_COPY)) !=
4589 (MAP_ENTRY_USER_WIRED | MAP_ENTRY_NEEDS_COPY),
4590 ("entry %p flags %x", entry, entry->eflags));
4591 if ((fault_typea & VM_PROT_COPY) != 0 &&
4592 (entry->max_protection & VM_PROT_WRITE) == 0 &&
4593 (entry->eflags & MAP_ENTRY_COW) == 0) {
4594 vm_map_unlock_read(map);
4595 return (KERN_PROTECTION_FAILURE);
4599 * If this page is not pageable, we have to get it for all possible
4602 *wired = (entry->wired_count != 0);
4604 fault_type = entry->protection;
4605 size = entry->end - entry->start;
4607 * If the entry was copy-on-write, we either ...
4609 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
4611 * If we want to write the page, we may as well handle that
4612 * now since we've got the map locked.
4614 * If we don't need to write the page, we just demote the
4615 * permissions allowed.
4617 if ((fault_type & VM_PROT_WRITE) != 0 ||
4618 (fault_typea & VM_PROT_COPY) != 0) {
4620 * Make a new object, and place it in the object
4621 * chain. Note that no new references have appeared
4622 * -- one just moved from the map to the new
4625 if (vm_map_lock_upgrade(map))
4628 if (entry->cred == NULL) {
4630 * The debugger owner is charged for
4633 cred = curthread->td_ucred;
4635 if (!swap_reserve_by_cred(size, cred)) {
4638 return (KERN_RESOURCE_SHORTAGE);
4642 vm_object_shadow(&entry->object.vm_object,
4643 &entry->offset, size);
4644 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
4645 eobject = entry->object.vm_object;
4646 if (eobject->cred != NULL) {
4648 * The object was not shadowed.
4650 swap_release_by_cred(size, entry->cred);
4651 crfree(entry->cred);
4653 } else if (entry->cred != NULL) {
4654 VM_OBJECT_WLOCK(eobject);
4655 eobject->cred = entry->cred;
4656 eobject->charge = size;
4657 VM_OBJECT_WUNLOCK(eobject);
4661 vm_map_lock_downgrade(map);
4664 * We're attempting to read a copy-on-write page --
4665 * don't allow writes.
4667 prot &= ~VM_PROT_WRITE;
4672 * Create an object if necessary.
4674 if (entry->object.vm_object == NULL &&
4676 if (vm_map_lock_upgrade(map))
4678 entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
4681 if (entry->cred != NULL) {
4682 VM_OBJECT_WLOCK(entry->object.vm_object);
4683 entry->object.vm_object->cred = entry->cred;
4684 entry->object.vm_object->charge = size;
4685 VM_OBJECT_WUNLOCK(entry->object.vm_object);
4688 vm_map_lock_downgrade(map);
4692 * Return the object/offset from this entry. If the entry was
4693 * copy-on-write or empty, it has been fixed up.
4695 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
4696 *object = entry->object.vm_object;
4699 return (KERN_SUCCESS);
4703 * vm_map_lookup_locked:
4705 * Lookup the faulting address. A version of vm_map_lookup that returns
4706 * KERN_FAILURE instead of blocking on map lock or memory allocation.
4709 vm_map_lookup_locked(vm_map_t *var_map, /* IN/OUT */
4711 vm_prot_t fault_typea,
4712 vm_map_entry_t *out_entry, /* OUT */
4713 vm_object_t *object, /* OUT */
4714 vm_pindex_t *pindex, /* OUT */
4715 vm_prot_t *out_prot, /* OUT */
4716 boolean_t *wired) /* OUT */
4718 vm_map_entry_t entry;
4719 vm_map_t map = *var_map;
4721 vm_prot_t fault_type = fault_typea;
4724 * Lookup the faulting address.
4726 if (!vm_map_lookup_entry(map, vaddr, out_entry))
4727 return (KERN_INVALID_ADDRESS);
4732 * Fail if the entry refers to a submap.
4734 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
4735 return (KERN_FAILURE);
4738 * Check whether this task is allowed to have this page.
4740 prot = entry->protection;
4741 fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
4742 if ((fault_type & prot) != fault_type)
4743 return (KERN_PROTECTION_FAILURE);
4746 * If this page is not pageable, we have to get it for all possible
4749 *wired = (entry->wired_count != 0);
4751 fault_type = entry->protection;
4753 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
4755 * Fail if the entry was copy-on-write for a write fault.
4757 if (fault_type & VM_PROT_WRITE)
4758 return (KERN_FAILURE);
4760 * We're attempting to read a copy-on-write page --
4761 * don't allow writes.
4763 prot &= ~VM_PROT_WRITE;
4767 * Fail if an object should be created.
4769 if (entry->object.vm_object == NULL && !map->system_map)
4770 return (KERN_FAILURE);
4773 * Return the object/offset from this entry. If the entry was
4774 * copy-on-write or empty, it has been fixed up.
4776 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
4777 *object = entry->object.vm_object;
4780 return (KERN_SUCCESS);
4784 * vm_map_lookup_done:
4786 * Releases locks acquired by a vm_map_lookup
4787 * (according to the handle returned by that lookup).
4790 vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry)
4793 * Unlock the main-level map
4795 vm_map_unlock_read(map);
4799 vm_map_max_KBI(const struct vm_map *map)
4802 return (vm_map_max(map));
4806 vm_map_min_KBI(const struct vm_map *map)
4809 return (vm_map_min(map));
4813 vm_map_pmap_KBI(vm_map_t map)
4819 #include "opt_ddb.h"
4821 #include <sys/kernel.h>
4823 #include <ddb/ddb.h>
4826 vm_map_print(vm_map_t map)
4828 vm_map_entry_t entry;
4830 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
4832 (void *)map->pmap, map->nentries, map->timestamp);
4835 for (entry = map->header.next; entry != &map->header;
4836 entry = entry->next) {
4837 db_iprintf("map entry %p: start=%p, end=%p, eflags=%#x, \n",
4838 (void *)entry, (void *)entry->start, (void *)entry->end,
4841 static char *inheritance_name[4] =
4842 {"share", "copy", "none", "donate_copy"};
4844 db_iprintf(" prot=%x/%x/%s",
4846 entry->max_protection,
4847 inheritance_name[(int)(unsigned char)entry->inheritance]);
4848 if (entry->wired_count != 0)
4849 db_printf(", wired");
4851 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
4852 db_printf(", share=%p, offset=0x%jx\n",
4853 (void *)entry->object.sub_map,
4854 (uintmax_t)entry->offset);
4855 if ((entry->prev == &map->header) ||
4856 (entry->prev->object.sub_map !=
4857 entry->object.sub_map)) {
4859 vm_map_print((vm_map_t)entry->object.sub_map);
4863 if (entry->cred != NULL)
4864 db_printf(", ruid %d", entry->cred->cr_ruid);
4865 db_printf(", object=%p, offset=0x%jx",
4866 (void *)entry->object.vm_object,
4867 (uintmax_t)entry->offset);
4868 if (entry->object.vm_object && entry->object.vm_object->cred)
4869 db_printf(", obj ruid %d charge %jx",
4870 entry->object.vm_object->cred->cr_ruid,
4871 (uintmax_t)entry->object.vm_object->charge);
4872 if (entry->eflags & MAP_ENTRY_COW)
4873 db_printf(", copy (%s)",
4874 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
4877 if ((entry->prev == &map->header) ||
4878 (entry->prev->object.vm_object !=
4879 entry->object.vm_object)) {
4881 vm_object_print((db_expr_t)(intptr_t)
4882 entry->object.vm_object,
4891 DB_SHOW_COMMAND(map, map)
4895 db_printf("usage: show map <addr>\n");
4898 vm_map_print((vm_map_t)addr);
4901 DB_SHOW_COMMAND(procvm, procvm)
4906 p = db_lookup_proc(addr);
4911 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
4912 (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
4913 (void *)vmspace_pmap(p->p_vmspace));
4915 vm_map_print((vm_map_t)&p->p_vmspace->vm_map);