2 * SPDX-License-Identifier: (BSD-3-Clause AND MIT-CMU)
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * The Mach Operating System project at Carnegie-Mellon University.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * from: @(#)vm_map.c 8.3 (Berkeley) 1/12/94
37 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
38 * All rights reserved.
40 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
42 * Permission to use, copy, modify and distribute this software and
43 * its documentation is hereby granted, provided that both the copyright
44 * notice and this permission notice appear in all copies of the
45 * software, derivative works or modified versions, and any portions
46 * thereof, and that both notices appear in supporting documentation.
48 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
49 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
50 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
52 * Carnegie Mellon requests users of this software to return to
54 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
55 * School of Computer Science
56 * Carnegie Mellon University
57 * Pittsburgh PA 15213-3890
59 * any improvements or extensions that they make and grant Carnegie the
60 * rights to redistribute these changes.
64 * Virtual memory mapping module.
67 #include <sys/cdefs.h>
68 __FBSDID("$FreeBSD$");
70 #include <sys/param.h>
71 #include <sys/systm.h>
73 #include <sys/kernel.h>
76 #include <sys/mutex.h>
78 #include <sys/vmmeter.h>
80 #include <sys/vnode.h>
81 #include <sys/racct.h>
82 #include <sys/resourcevar.h>
83 #include <sys/rwlock.h>
85 #include <sys/sysctl.h>
86 #include <sys/sysent.h>
90 #include <vm/vm_param.h>
92 #include <vm/vm_map.h>
93 #include <vm/vm_page.h>
94 #include <vm/vm_pageout.h>
95 #include <vm/vm_object.h>
96 #include <vm/vm_pager.h>
97 #include <vm/vm_kern.h>
98 #include <vm/vm_extern.h>
99 #include <vm/vnode_pager.h>
100 #include <vm/swap_pager.h>
104 * Virtual memory maps provide for the mapping, protection,
105 * and sharing of virtual memory objects. In addition,
106 * this module provides for an efficient virtual copy of
107 * memory from one map to another.
109 * Synchronization is required prior to most operations.
111 * Maps consist of an ordered doubly-linked list of simple
112 * entries; a self-adjusting binary search tree of these
113 * entries is used to speed up lookups.
115 * Since portions of maps are specified by start/end addresses,
116 * which may not align with existing map entries, all
117 * routines merely "clip" entries to these start/end values.
118 * [That is, an entry is split into two, bordering at a
119 * start or end value.] Note that these clippings may not
120 * always be necessary (as the two resulting entries are then
121 * not changed); however, the clipping is done for convenience.
123 * As mentioned above, virtual copy operations are performed
124 * by copying VM object references from one map to
125 * another, and then marking both regions as copy-on-write.
128 static struct mtx map_sleep_mtx;
129 static uma_zone_t mapentzone;
130 static uma_zone_t kmapentzone;
131 static uma_zone_t mapzone;
132 static uma_zone_t vmspace_zone;
133 static int vmspace_zinit(void *mem, int size, int flags);
134 static int vm_map_zinit(void *mem, int ize, int flags);
135 static void _vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min,
137 static void vm_map_entry_deallocate(vm_map_entry_t entry, boolean_t system_map);
138 static void vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry);
139 static void vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry);
140 static int vm_map_growstack(vm_map_t map, vm_offset_t addr,
141 vm_map_entry_t gap_entry);
142 static void vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
143 vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags);
145 static void vm_map_zdtor(void *mem, int size, void *arg);
146 static void vmspace_zdtor(void *mem, int size, void *arg);
148 static int vm_map_stack_locked(vm_map_t map, vm_offset_t addrbos,
149 vm_size_t max_ssize, vm_size_t growsize, vm_prot_t prot, vm_prot_t max,
151 static void vm_map_wire_entry_failure(vm_map_t map, vm_map_entry_t entry,
152 vm_offset_t failed_addr);
154 #define ENTRY_CHARGED(e) ((e)->cred != NULL || \
155 ((e)->object.vm_object != NULL && (e)->object.vm_object->cred != NULL && \
156 !((e)->eflags & MAP_ENTRY_NEEDS_COPY)))
159 * PROC_VMSPACE_{UN,}LOCK() can be a noop as long as vmspaces are type
162 #define PROC_VMSPACE_LOCK(p) do { } while (0)
163 #define PROC_VMSPACE_UNLOCK(p) do { } while (0)
166 * VM_MAP_RANGE_CHECK: [ internal use only ]
168 * Asserts that the starting and ending region
169 * addresses fall within the valid range of the map.
171 #define VM_MAP_RANGE_CHECK(map, start, end) \
173 if (start < vm_map_min(map)) \
174 start = vm_map_min(map); \
175 if (end > vm_map_max(map)) \
176 end = vm_map_max(map); \
184 * Initialize the vm_map module. Must be called before
185 * any other vm_map routines.
187 * Map and entry structures are allocated from the general
188 * purpose memory pool with some exceptions:
190 * - The kernel map and kmem submap are allocated statically.
191 * - Kernel map entries are allocated out of a static pool.
193 * These restrictions are necessary since malloc() uses the
194 * maps and requires map entries.
200 mtx_init(&map_sleep_mtx, "vm map sleep mutex", NULL, MTX_DEF);
201 mapzone = uma_zcreate("MAP", sizeof(struct vm_map), NULL,
207 vm_map_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
208 uma_prealloc(mapzone, MAX_KMAP);
209 kmapentzone = uma_zcreate("KMAP ENTRY", sizeof(struct vm_map_entry),
210 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
211 UMA_ZONE_MTXCLASS | UMA_ZONE_VM);
212 mapentzone = uma_zcreate("MAP ENTRY", sizeof(struct vm_map_entry),
213 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
214 vmspace_zone = uma_zcreate("VMSPACE", sizeof(struct vmspace), NULL,
220 vmspace_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
224 vmspace_zinit(void *mem, int size, int flags)
228 vm = (struct vmspace *)mem;
230 vm->vm_map.pmap = NULL;
231 (void)vm_map_zinit(&vm->vm_map, sizeof(vm->vm_map), flags);
232 PMAP_LOCK_INIT(vmspace_pmap(vm));
237 vm_map_zinit(void *mem, int size, int flags)
242 memset(map, 0, sizeof(*map));
243 mtx_init(&map->system_mtx, "vm map (system)", NULL, MTX_DEF | MTX_DUPOK);
244 sx_init(&map->lock, "vm map (user)");
250 vmspace_zdtor(void *mem, int size, void *arg)
254 vm = (struct vmspace *)mem;
256 vm_map_zdtor(&vm->vm_map, sizeof(vm->vm_map), arg);
259 vm_map_zdtor(void *mem, int size, void *arg)
264 KASSERT(map->nentries == 0,
265 ("map %p nentries == %d on free.",
266 map, map->nentries));
267 KASSERT(map->size == 0,
268 ("map %p size == %lu on free.",
269 map, (unsigned long)map->size));
271 #endif /* INVARIANTS */
274 * Allocate a vmspace structure, including a vm_map and pmap,
275 * and initialize those structures. The refcnt is set to 1.
277 * If 'pinit' is NULL then the embedded pmap is initialized via pmap_pinit().
280 vmspace_alloc(vm_offset_t min, vm_offset_t max, pmap_pinit_t pinit)
284 vm = uma_zalloc(vmspace_zone, M_WAITOK);
285 KASSERT(vm->vm_map.pmap == NULL, ("vm_map.pmap must be NULL"));
286 if (!pinit(vmspace_pmap(vm))) {
287 uma_zfree(vmspace_zone, vm);
290 CTR1(KTR_VM, "vmspace_alloc: %p", vm);
291 _vm_map_init(&vm->vm_map, vmspace_pmap(vm), min, max);
306 vmspace_container_reset(struct proc *p)
310 racct_set(p, RACCT_DATA, 0);
311 racct_set(p, RACCT_STACK, 0);
312 racct_set(p, RACCT_RSS, 0);
313 racct_set(p, RACCT_MEMLOCK, 0);
314 racct_set(p, RACCT_VMEM, 0);
320 vmspace_dofree(struct vmspace *vm)
323 CTR1(KTR_VM, "vmspace_free: %p", vm);
326 * Make sure any SysV shm is freed, it might not have been in
332 * Lock the map, to wait out all other references to it.
333 * Delete all of the mappings and pages they hold, then call
334 * the pmap module to reclaim anything left.
336 (void)vm_map_remove(&vm->vm_map, vm_map_min(&vm->vm_map),
337 vm_map_max(&vm->vm_map));
339 pmap_release(vmspace_pmap(vm));
340 vm->vm_map.pmap = NULL;
341 uma_zfree(vmspace_zone, vm);
345 vmspace_free(struct vmspace *vm)
348 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
349 "vmspace_free() called");
351 if (vm->vm_refcnt == 0)
352 panic("vmspace_free: attempt to free already freed vmspace");
354 if (atomic_fetchadd_int(&vm->vm_refcnt, -1) == 1)
359 vmspace_exitfree(struct proc *p)
363 PROC_VMSPACE_LOCK(p);
366 PROC_VMSPACE_UNLOCK(p);
367 KASSERT(vm == &vmspace0, ("vmspace_exitfree: wrong vmspace"));
372 vmspace_exit(struct thread *td)
379 * Release user portion of address space.
380 * This releases references to vnodes,
381 * which could cause I/O if the file has been unlinked.
382 * Need to do this early enough that we can still sleep.
384 * The last exiting process to reach this point releases as
385 * much of the environment as it can. vmspace_dofree() is the
386 * slower fallback in case another process had a temporary
387 * reference to the vmspace.
392 atomic_add_int(&vmspace0.vm_refcnt, 1);
393 refcnt = vm->vm_refcnt;
395 if (refcnt > 1 && p->p_vmspace != &vmspace0) {
396 /* Switch now since other proc might free vmspace */
397 PROC_VMSPACE_LOCK(p);
398 p->p_vmspace = &vmspace0;
399 PROC_VMSPACE_UNLOCK(p);
402 } while (!atomic_fcmpset_int(&vm->vm_refcnt, &refcnt, refcnt - 1));
404 if (p->p_vmspace != vm) {
405 /* vmspace not yet freed, switch back */
406 PROC_VMSPACE_LOCK(p);
408 PROC_VMSPACE_UNLOCK(p);
411 pmap_remove_pages(vmspace_pmap(vm));
412 /* Switch now since this proc will free vmspace */
413 PROC_VMSPACE_LOCK(p);
414 p->p_vmspace = &vmspace0;
415 PROC_VMSPACE_UNLOCK(p);
421 vmspace_container_reset(p);
425 /* Acquire reference to vmspace owned by another process. */
428 vmspace_acquire_ref(struct proc *p)
433 PROC_VMSPACE_LOCK(p);
436 PROC_VMSPACE_UNLOCK(p);
439 refcnt = vm->vm_refcnt;
441 if (refcnt <= 0) { /* Avoid 0->1 transition */
442 PROC_VMSPACE_UNLOCK(p);
445 } while (!atomic_fcmpset_int(&vm->vm_refcnt, &refcnt, refcnt + 1));
446 if (vm != p->p_vmspace) {
447 PROC_VMSPACE_UNLOCK(p);
451 PROC_VMSPACE_UNLOCK(p);
456 * Switch between vmspaces in an AIO kernel process.
458 * The new vmspace is either the vmspace of a user process obtained
459 * from an active AIO request or the initial vmspace of the AIO kernel
460 * process (when it is idling). Because user processes will block to
461 * drain any active AIO requests before proceeding in exit() or
462 * execve(), the reference count for vmspaces from AIO requests can
463 * never be 0. Similarly, AIO kernel processes hold an extra
464 * reference on their initial vmspace for the life of the process. As
465 * a result, the 'newvm' vmspace always has a non-zero reference
466 * count. This permits an additional reference on 'newvm' to be
467 * acquired via a simple atomic increment rather than the loop in
468 * vmspace_acquire_ref() above.
471 vmspace_switch_aio(struct vmspace *newvm)
473 struct vmspace *oldvm;
475 /* XXX: Need some way to assert that this is an aio daemon. */
477 KASSERT(newvm->vm_refcnt > 0,
478 ("vmspace_switch_aio: newvm unreferenced"));
480 oldvm = curproc->p_vmspace;
485 * Point to the new address space and refer to it.
487 curproc->p_vmspace = newvm;
488 atomic_add_int(&newvm->vm_refcnt, 1);
490 /* Activate the new mapping. */
491 pmap_activate(curthread);
497 _vm_map_lock(vm_map_t map, const char *file, int line)
501 mtx_lock_flags_(&map->system_mtx, 0, file, line);
503 sx_xlock_(&map->lock, file, line);
508 vm_map_entry_set_vnode_text(vm_map_entry_t entry, bool add)
514 if ((entry->eflags & MAP_ENTRY_VN_EXEC) == 0)
516 KASSERT((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0,
517 ("Submap with execs"));
518 object = entry->object.vm_object;
519 KASSERT(object != NULL, ("No object for text, entry %p", entry));
520 if ((object->flags & OBJ_ANON) != 0)
521 object = object->handle;
523 KASSERT(object->backing_object == NULL,
524 ("non-anon object %p shadows", object));
525 KASSERT(object != NULL, ("No content object for text, entry %p obj %p",
526 entry, entry->object.vm_object));
529 * Mostly, we do not lock the backing object. It is
530 * referenced by the entry we are processing, so it cannot go
535 if (object->type == OBJT_DEAD) {
537 * For OBJT_DEAD objects, v_writecount was handled in
538 * vnode_pager_dealloc().
540 } else if (object->type == OBJT_VNODE) {
542 } else if (object->type == OBJT_SWAP) {
543 KASSERT((object->flags & OBJ_TMPFS_NODE) != 0,
544 ("vm_map_entry_set_vnode_text: swap and !TMPFS "
545 "entry %p, object %p, add %d", entry, object, add));
547 * Tmpfs VREG node, which was reclaimed, has
548 * OBJ_TMPFS_NODE flag set, but not OBJ_TMPFS. In
549 * this case there is no v_writecount to adjust.
551 VM_OBJECT_RLOCK(object);
552 if ((object->flags & OBJ_TMPFS) != 0) {
553 vp = object->un_pager.swp.swp_tmpfs;
559 VM_OBJECT_RUNLOCK(object);
562 ("vm_map_entry_set_vnode_text: wrong object type, "
563 "entry %p, object %p, add %d", entry, object, add));
567 VOP_SET_TEXT_CHECKED(vp);
569 vn_lock(vp, LK_SHARED | LK_RETRY);
570 VOP_UNSET_TEXT_CHECKED(vp);
579 * Use a different name for this vm_map_entry field when it's use
580 * is not consistent with its use as part of an ordered search tree.
582 #define defer_next right
585 vm_map_process_deferred(void)
588 vm_map_entry_t entry, next;
592 entry = td->td_map_def_user;
593 td->td_map_def_user = NULL;
594 while (entry != NULL) {
595 next = entry->defer_next;
596 MPASS((entry->eflags & (MAP_ENTRY_WRITECNT |
597 MAP_ENTRY_VN_EXEC)) != (MAP_ENTRY_WRITECNT |
599 if ((entry->eflags & MAP_ENTRY_WRITECNT) != 0) {
601 * Decrement the object's writemappings and
602 * possibly the vnode's v_writecount.
604 KASSERT((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0,
605 ("Submap with writecount"));
606 object = entry->object.vm_object;
607 KASSERT(object != NULL, ("No object for writecount"));
608 vm_pager_release_writecount(object, entry->start,
611 vm_map_entry_set_vnode_text(entry, false);
612 vm_map_entry_deallocate(entry, FALSE);
619 _vm_map_assert_locked(vm_map_t map, const char *file, int line)
623 mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
625 sx_assert_(&map->lock, SA_XLOCKED, file, line);
628 #define VM_MAP_ASSERT_LOCKED(map) \
629 _vm_map_assert_locked(map, LOCK_FILE, LOCK_LINE)
631 enum { VMMAP_CHECK_NONE, VMMAP_CHECK_UNLOCK, VMMAP_CHECK_ALL };
633 static int enable_vmmap_check = VMMAP_CHECK_UNLOCK;
635 static int enable_vmmap_check = VMMAP_CHECK_NONE;
637 SYSCTL_INT(_debug, OID_AUTO, vmmap_check, CTLFLAG_RWTUN,
638 &enable_vmmap_check, 0, "Enable vm map consistency checking");
640 static void _vm_map_assert_consistent(vm_map_t map, int check);
642 #define VM_MAP_ASSERT_CONSISTENT(map) \
643 _vm_map_assert_consistent(map, VMMAP_CHECK_ALL)
645 #define VM_MAP_UNLOCK_CONSISTENT(map) do { \
646 if (map->nupdates > map->nentries) { \
647 _vm_map_assert_consistent(map, VMMAP_CHECK_UNLOCK); \
652 #define VM_MAP_UNLOCK_CONSISTENT(map)
655 #define VM_MAP_ASSERT_LOCKED(map)
656 #define VM_MAP_ASSERT_CONSISTENT(map)
657 #define VM_MAP_UNLOCK_CONSISTENT(map)
658 #endif /* INVARIANTS */
661 _vm_map_unlock(vm_map_t map, const char *file, int line)
664 VM_MAP_UNLOCK_CONSISTENT(map);
666 mtx_unlock_flags_(&map->system_mtx, 0, file, line);
668 sx_xunlock_(&map->lock, file, line);
669 vm_map_process_deferred();
674 _vm_map_lock_read(vm_map_t map, const char *file, int line)
678 mtx_lock_flags_(&map->system_mtx, 0, file, line);
680 sx_slock_(&map->lock, file, line);
684 _vm_map_unlock_read(vm_map_t map, const char *file, int line)
688 mtx_unlock_flags_(&map->system_mtx, 0, file, line);
690 sx_sunlock_(&map->lock, file, line);
691 vm_map_process_deferred();
696 _vm_map_trylock(vm_map_t map, const char *file, int line)
700 error = map->system_map ?
701 !mtx_trylock_flags_(&map->system_mtx, 0, file, line) :
702 !sx_try_xlock_(&map->lock, file, line);
709 _vm_map_trylock_read(vm_map_t map, const char *file, int line)
713 error = map->system_map ?
714 !mtx_trylock_flags_(&map->system_mtx, 0, file, line) :
715 !sx_try_slock_(&map->lock, file, line);
720 * _vm_map_lock_upgrade: [ internal use only ]
722 * Tries to upgrade a read (shared) lock on the specified map to a write
723 * (exclusive) lock. Returns the value "0" if the upgrade succeeds and a
724 * non-zero value if the upgrade fails. If the upgrade fails, the map is
725 * returned without a read or write lock held.
727 * Requires that the map be read locked.
730 _vm_map_lock_upgrade(vm_map_t map, const char *file, int line)
732 unsigned int last_timestamp;
734 if (map->system_map) {
735 mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
737 if (!sx_try_upgrade_(&map->lock, file, line)) {
738 last_timestamp = map->timestamp;
739 sx_sunlock_(&map->lock, file, line);
740 vm_map_process_deferred();
742 * If the map's timestamp does not change while the
743 * map is unlocked, then the upgrade succeeds.
745 sx_xlock_(&map->lock, file, line);
746 if (last_timestamp != map->timestamp) {
747 sx_xunlock_(&map->lock, file, line);
757 _vm_map_lock_downgrade(vm_map_t map, const char *file, int line)
760 if (map->system_map) {
761 mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
763 VM_MAP_UNLOCK_CONSISTENT(map);
764 sx_downgrade_(&map->lock, file, line);
771 * Returns a non-zero value if the caller holds a write (exclusive) lock
772 * on the specified map and the value "0" otherwise.
775 vm_map_locked(vm_map_t map)
779 return (mtx_owned(&map->system_mtx));
781 return (sx_xlocked(&map->lock));
785 * _vm_map_unlock_and_wait:
787 * Atomically releases the lock on the specified map and puts the calling
788 * thread to sleep. The calling thread will remain asleep until either
789 * vm_map_wakeup() is performed on the map or the specified timeout is
792 * WARNING! This function does not perform deferred deallocations of
793 * objects and map entries. Therefore, the calling thread is expected to
794 * reacquire the map lock after reawakening and later perform an ordinary
795 * unlock operation, such as vm_map_unlock(), before completing its
796 * operation on the map.
799 _vm_map_unlock_and_wait(vm_map_t map, int timo, const char *file, int line)
802 VM_MAP_UNLOCK_CONSISTENT(map);
803 mtx_lock(&map_sleep_mtx);
805 mtx_unlock_flags_(&map->system_mtx, 0, file, line);
807 sx_xunlock_(&map->lock, file, line);
808 return (msleep(&map->root, &map_sleep_mtx, PDROP | PVM, "vmmaps",
815 * Awaken any threads that have slept on the map using
816 * vm_map_unlock_and_wait().
819 vm_map_wakeup(vm_map_t map)
823 * Acquire and release map_sleep_mtx to prevent a wakeup()
824 * from being performed (and lost) between the map unlock
825 * and the msleep() in _vm_map_unlock_and_wait().
827 mtx_lock(&map_sleep_mtx);
828 mtx_unlock(&map_sleep_mtx);
833 vm_map_busy(vm_map_t map)
836 VM_MAP_ASSERT_LOCKED(map);
841 vm_map_unbusy(vm_map_t map)
844 VM_MAP_ASSERT_LOCKED(map);
845 KASSERT(map->busy, ("vm_map_unbusy: not busy"));
846 if (--map->busy == 0 && (map->flags & MAP_BUSY_WAKEUP)) {
847 vm_map_modflags(map, 0, MAP_BUSY_WAKEUP);
853 vm_map_wait_busy(vm_map_t map)
856 VM_MAP_ASSERT_LOCKED(map);
858 vm_map_modflags(map, MAP_BUSY_WAKEUP, 0);
860 msleep(&map->busy, &map->system_mtx, 0, "mbusy", 0);
862 sx_sleep(&map->busy, &map->lock, 0, "mbusy", 0);
868 vmspace_resident_count(struct vmspace *vmspace)
870 return pmap_resident_count(vmspace_pmap(vmspace));
876 * Creates and returns a new empty VM map with
877 * the given physical map structure, and having
878 * the given lower and upper address bounds.
881 vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max)
885 result = uma_zalloc(mapzone, M_WAITOK);
886 CTR1(KTR_VM, "vm_map_create: %p", result);
887 _vm_map_init(result, pmap, min, max);
892 * Initialize an existing vm_map structure
893 * such as that in the vmspace structure.
896 _vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min, vm_offset_t max)
899 map->header.eflags = MAP_ENTRY_HEADER;
900 map->needs_wakeup = FALSE;
903 map->header.end = min;
904 map->header.start = max;
906 map->header.left = map->header.right = &map->header;
917 vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min, vm_offset_t max)
920 _vm_map_init(map, pmap, min, max);
921 mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
922 sx_init(&map->lock, "user map");
926 * vm_map_entry_dispose: [ internal use only ]
928 * Inverse of vm_map_entry_create.
931 vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry)
933 uma_zfree(map->system_map ? kmapentzone : mapentzone, entry);
937 * vm_map_entry_create: [ internal use only ]
939 * Allocates a VM map entry for insertion.
940 * No entry fields are filled in.
942 static vm_map_entry_t
943 vm_map_entry_create(vm_map_t map)
945 vm_map_entry_t new_entry;
948 new_entry = uma_zalloc(kmapentzone, M_NOWAIT);
950 new_entry = uma_zalloc(mapentzone, M_WAITOK);
951 if (new_entry == NULL)
952 panic("vm_map_entry_create: kernel resources exhausted");
957 * vm_map_entry_set_behavior:
959 * Set the expected access behavior, either normal, random, or
963 vm_map_entry_set_behavior(vm_map_entry_t entry, u_char behavior)
965 entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) |
966 (behavior & MAP_ENTRY_BEHAV_MASK);
970 * vm_map_entry_max_free_{left,right}:
972 * Compute the size of the largest free gap between two entries,
973 * one the root of a tree and the other the ancestor of that root
974 * that is the least or greatest ancestor found on the search path.
976 static inline vm_size_t
977 vm_map_entry_max_free_left(vm_map_entry_t root, vm_map_entry_t left_ancestor)
980 return (root->left != left_ancestor ?
981 root->left->max_free : root->start - left_ancestor->end);
984 static inline vm_size_t
985 vm_map_entry_max_free_right(vm_map_entry_t root, vm_map_entry_t right_ancestor)
988 return (root->right != right_ancestor ?
989 root->right->max_free : right_ancestor->start - root->end);
993 * vm_map_entry_{pred,succ}:
995 * Find the {predecessor, successor} of the entry by taking one step
996 * in the appropriate direction and backtracking as much as necessary.
997 * vm_map_entry_succ is defined in vm_map.h.
999 static inline vm_map_entry_t
1000 vm_map_entry_pred(vm_map_entry_t entry)
1002 vm_map_entry_t prior;
1004 prior = entry->left;
1005 if (prior->right->start < entry->start) {
1007 prior = prior->right;
1008 while (prior->right != entry);
1013 static inline vm_size_t
1014 vm_size_max(vm_size_t a, vm_size_t b)
1017 return (a > b ? a : b);
1020 #define SPLAY_LEFT_STEP(root, y, llist, rlist, test) do { \
1022 vm_size_t max_free; \
1025 * Infer root->right->max_free == root->max_free when \
1026 * y->max_free < root->max_free || root->max_free == 0. \
1027 * Otherwise, look right to find it. \
1030 max_free = root->max_free; \
1031 KASSERT(max_free == vm_size_max( \
1032 vm_map_entry_max_free_left(root, llist), \
1033 vm_map_entry_max_free_right(root, rlist)), \
1034 ("%s: max_free invariant fails", __func__)); \
1035 if (max_free - 1 < vm_map_entry_max_free_left(root, llist)) \
1036 max_free = vm_map_entry_max_free_right(root, rlist); \
1037 if (y != llist && (test)) { \
1038 /* Rotate right and make y root. */ \
1043 if (max_free < y->max_free) \
1044 root->max_free = max_free = \
1045 vm_size_max(max_free, z->max_free); \
1046 } else if (max_free < y->max_free) \
1047 root->max_free = max_free = \
1048 vm_size_max(max_free, root->start - y->end);\
1052 /* Copy right->max_free. Put root on rlist. */ \
1053 root->max_free = max_free; \
1054 KASSERT(max_free == vm_map_entry_max_free_right(root, rlist), \
1055 ("%s: max_free not copied from right", __func__)); \
1056 root->left = rlist; \
1058 root = y != llist ? y : NULL; \
1061 #define SPLAY_RIGHT_STEP(root, y, llist, rlist, test) do { \
1063 vm_size_t max_free; \
1066 * Infer root->left->max_free == root->max_free when \
1067 * y->max_free < root->max_free || root->max_free == 0. \
1068 * Otherwise, look left to find it. \
1071 max_free = root->max_free; \
1072 KASSERT(max_free == vm_size_max( \
1073 vm_map_entry_max_free_left(root, llist), \
1074 vm_map_entry_max_free_right(root, rlist)), \
1075 ("%s: max_free invariant fails", __func__)); \
1076 if (max_free - 1 < vm_map_entry_max_free_right(root, rlist)) \
1077 max_free = vm_map_entry_max_free_left(root, llist); \
1078 if (y != rlist && (test)) { \
1079 /* Rotate left and make y root. */ \
1084 if (max_free < y->max_free) \
1085 root->max_free = max_free = \
1086 vm_size_max(max_free, z->max_free); \
1087 } else if (max_free < y->max_free) \
1088 root->max_free = max_free = \
1089 vm_size_max(max_free, y->start - root->end);\
1093 /* Copy left->max_free. Put root on llist. */ \
1094 root->max_free = max_free; \
1095 KASSERT(max_free == vm_map_entry_max_free_left(root, llist), \
1096 ("%s: max_free not copied from left", __func__)); \
1097 root->right = llist; \
1099 root = y != rlist ? y : NULL; \
1103 * Walk down the tree until we find addr or a gap where addr would go, breaking
1104 * off left and right subtrees of nodes less than, or greater than addr. Treat
1105 * subtrees with root->max_free < length as empty trees. llist and rlist are
1106 * the two sides in reverse order (bottom-up), with llist linked by the right
1107 * pointer and rlist linked by the left pointer in the vm_map_entry, and both
1108 * lists terminated by &map->header. This function, and the subsequent call to
1109 * vm_map_splay_merge_{left,right,pred,succ}, rely on the start and end address
1110 * values in &map->header.
1112 static __always_inline vm_map_entry_t
1113 vm_map_splay_split(vm_map_t map, vm_offset_t addr, vm_size_t length,
1114 vm_map_entry_t *llist, vm_map_entry_t *rlist)
1116 vm_map_entry_t left, right, root, y;
1118 left = right = &map->header;
1120 while (root != NULL && root->max_free >= length) {
1121 KASSERT(left->end <= root->start &&
1122 root->end <= right->start,
1123 ("%s: root not within tree bounds", __func__));
1124 if (addr < root->start) {
1125 SPLAY_LEFT_STEP(root, y, left, right,
1126 y->max_free >= length && addr < y->start);
1127 } else if (addr >= root->end) {
1128 SPLAY_RIGHT_STEP(root, y, left, right,
1129 y->max_free >= length && addr >= y->end);
1138 static __always_inline void
1139 vm_map_splay_findnext(vm_map_entry_t root, vm_map_entry_t *rlist)
1141 vm_map_entry_t hi, right, y;
1144 hi = root->right == right ? NULL : root->right;
1148 SPLAY_LEFT_STEP(hi, y, root, right, true);
1153 static __always_inline void
1154 vm_map_splay_findprev(vm_map_entry_t root, vm_map_entry_t *llist)
1156 vm_map_entry_t left, lo, y;
1159 lo = root->left == left ? NULL : root->left;
1163 SPLAY_RIGHT_STEP(lo, y, left, root, true);
1169 vm_map_entry_swap(vm_map_entry_t *a, vm_map_entry_t *b)
1179 * Walk back up the two spines, flip the pointers and set max_free. The
1180 * subtrees of the root go at the bottom of llist and rlist.
1183 vm_map_splay_merge_left_walk(vm_map_entry_t header, vm_map_entry_t root,
1184 vm_map_entry_t tail, vm_size_t max_free, vm_map_entry_t llist)
1188 * The max_free values of the children of llist are in
1189 * llist->max_free and max_free. Update with the
1192 llist->max_free = max_free =
1193 vm_size_max(llist->max_free, max_free);
1194 vm_map_entry_swap(&llist->right, &tail);
1195 vm_map_entry_swap(&tail, &llist);
1196 } while (llist != header);
1202 * When llist is known to be the predecessor of root.
1204 static inline vm_size_t
1205 vm_map_splay_merge_pred(vm_map_entry_t header, vm_map_entry_t root,
1206 vm_map_entry_t llist)
1210 max_free = root->start - llist->end;
1211 if (llist != header) {
1212 max_free = vm_map_splay_merge_left_walk(header, root,
1213 root, max_free, llist);
1215 root->left = header;
1216 header->right = root;
1222 * When llist may or may not be the predecessor of root.
1224 static inline vm_size_t
1225 vm_map_splay_merge_left(vm_map_entry_t header, vm_map_entry_t root,
1226 vm_map_entry_t llist)
1230 max_free = vm_map_entry_max_free_left(root, llist);
1231 if (llist != header) {
1232 max_free = vm_map_splay_merge_left_walk(header, root,
1233 root->left == llist ? root : root->left,
1240 vm_map_splay_merge_right_walk(vm_map_entry_t header, vm_map_entry_t root,
1241 vm_map_entry_t tail, vm_size_t max_free, vm_map_entry_t rlist)
1245 * The max_free values of the children of rlist are in
1246 * rlist->max_free and max_free. Update with the
1249 rlist->max_free = max_free =
1250 vm_size_max(rlist->max_free, max_free);
1251 vm_map_entry_swap(&rlist->left, &tail);
1252 vm_map_entry_swap(&tail, &rlist);
1253 } while (rlist != header);
1259 * When rlist is known to be the succecessor of root.
1261 static inline vm_size_t
1262 vm_map_splay_merge_succ(vm_map_entry_t header, vm_map_entry_t root,
1263 vm_map_entry_t rlist)
1267 max_free = rlist->start - root->end;
1268 if (rlist != header) {
1269 max_free = vm_map_splay_merge_right_walk(header, root,
1270 root, max_free, rlist);
1272 root->right = header;
1273 header->left = root;
1279 * When rlist may or may not be the succecessor of root.
1281 static inline vm_size_t
1282 vm_map_splay_merge_right(vm_map_entry_t header, vm_map_entry_t root,
1283 vm_map_entry_t rlist)
1287 max_free = vm_map_entry_max_free_right(root, rlist);
1288 if (rlist != header) {
1289 max_free = vm_map_splay_merge_right_walk(header, root,
1290 root->right == rlist ? root : root->right,
1299 * The Sleator and Tarjan top-down splay algorithm with the
1300 * following variation. Max_free must be computed bottom-up, so
1301 * on the downward pass, maintain the left and right spines in
1302 * reverse order. Then, make a second pass up each side to fix
1303 * the pointers and compute max_free. The time bound is O(log n)
1306 * The tree is threaded, which means that there are no null pointers.
1307 * When a node has no left child, its left pointer points to its
1308 * predecessor, which the last ancestor on the search path from the root
1309 * where the search branched right. Likewise, when a node has no right
1310 * child, its right pointer points to its successor. The map header node
1311 * is the predecessor of the first map entry, and the successor of the
1314 * The new root is the vm_map_entry containing "addr", or else an
1315 * adjacent entry (lower if possible) if addr is not in the tree.
1317 * The map must be locked, and leaves it so.
1319 * Returns: the new root.
1321 static vm_map_entry_t
1322 vm_map_splay(vm_map_t map, vm_offset_t addr)
1324 vm_map_entry_t header, llist, rlist, root;
1325 vm_size_t max_free_left, max_free_right;
1327 header = &map->header;
1328 root = vm_map_splay_split(map, addr, 0, &llist, &rlist);
1330 max_free_left = vm_map_splay_merge_left(header, root, llist);
1331 max_free_right = vm_map_splay_merge_right(header, root, rlist);
1332 } else if (llist != header) {
1334 * Recover the greatest node in the left
1335 * subtree and make it the root.
1338 llist = root->right;
1339 max_free_left = vm_map_splay_merge_left(header, root, llist);
1340 max_free_right = vm_map_splay_merge_succ(header, root, rlist);
1341 } else if (rlist != header) {
1343 * Recover the least node in the right
1344 * subtree and make it the root.
1348 max_free_left = vm_map_splay_merge_pred(header, root, llist);
1349 max_free_right = vm_map_splay_merge_right(header, root, rlist);
1351 /* There is no root. */
1354 root->max_free = vm_size_max(max_free_left, max_free_right);
1356 VM_MAP_ASSERT_CONSISTENT(map);
1361 * vm_map_entry_{un,}link:
1363 * Insert/remove entries from maps. On linking, if new entry clips
1364 * existing entry, trim existing entry to avoid overlap, and manage
1365 * offsets. On unlinking, merge disappearing entry with neighbor, if
1366 * called for, and manage offsets. Callers should not modify fields in
1367 * entries already mapped.
1370 vm_map_entry_link(vm_map_t map, vm_map_entry_t entry)
1372 vm_map_entry_t header, llist, rlist, root;
1373 vm_size_t max_free_left, max_free_right;
1376 "vm_map_entry_link: map %p, nentries %d, entry %p", map,
1377 map->nentries, entry);
1378 VM_MAP_ASSERT_LOCKED(map);
1380 header = &map->header;
1381 root = vm_map_splay_split(map, entry->start, 0, &llist, &rlist);
1384 * The new entry does not overlap any existing entry in the
1385 * map, so it becomes the new root of the map tree.
1387 max_free_left = vm_map_splay_merge_pred(header, entry, llist);
1388 max_free_right = vm_map_splay_merge_succ(header, entry, rlist);
1389 } else if (entry->start == root->start) {
1391 * The new entry is a clone of root, with only the end field
1392 * changed. The root entry will be shrunk to abut the new
1393 * entry, and will be the right child of the new root entry in
1396 KASSERT(entry->end < root->end,
1397 ("%s: clip_start not within entry", __func__));
1398 vm_map_splay_findprev(root, &llist);
1399 root->offset += entry->end - root->start;
1400 root->start = entry->end;
1401 max_free_left = vm_map_splay_merge_pred(header, entry, llist);
1402 max_free_right = root->max_free = vm_size_max(
1403 vm_map_splay_merge_pred(entry, root, entry),
1404 vm_map_splay_merge_right(header, root, rlist));
1407 * The new entry is a clone of root, with only the start field
1408 * changed. The root entry will be shrunk to abut the new
1409 * entry, and will be the left child of the new root entry in
1412 KASSERT(entry->end == root->end,
1413 ("%s: clip_start not within entry", __func__));
1414 vm_map_splay_findnext(root, &rlist);
1415 entry->offset += entry->start - root->start;
1416 root->end = entry->start;
1417 max_free_left = root->max_free = vm_size_max(
1418 vm_map_splay_merge_left(header, root, llist),
1419 vm_map_splay_merge_succ(entry, root, entry));
1420 max_free_right = vm_map_splay_merge_succ(header, entry, rlist);
1422 entry->max_free = vm_size_max(max_free_left, max_free_right);
1424 VM_MAP_ASSERT_CONSISTENT(map);
1427 enum unlink_merge_type {
1433 vm_map_entry_unlink(vm_map_t map, vm_map_entry_t entry,
1434 enum unlink_merge_type op)
1436 vm_map_entry_t header, llist, rlist, root;
1437 vm_size_t max_free_left, max_free_right;
1439 VM_MAP_ASSERT_LOCKED(map);
1440 header = &map->header;
1441 root = vm_map_splay_split(map, entry->start, 0, &llist, &rlist);
1442 KASSERT(root != NULL,
1443 ("vm_map_entry_unlink: unlink object not mapped"));
1445 vm_map_splay_findprev(root, &llist);
1446 vm_map_splay_findnext(root, &rlist);
1447 if (op == UNLINK_MERGE_NEXT) {
1448 rlist->start = root->start;
1449 rlist->offset = root->offset;
1451 if (llist != header) {
1453 llist = root->right;
1454 max_free_left = vm_map_splay_merge_left(header, root, llist);
1455 max_free_right = vm_map_splay_merge_succ(header, root, rlist);
1456 } else if (rlist != header) {
1459 max_free_left = vm_map_splay_merge_pred(header, root, llist);
1460 max_free_right = vm_map_splay_merge_right(header, root, rlist);
1462 header->left = header->right = header;
1466 root->max_free = vm_size_max(max_free_left, max_free_right);
1468 VM_MAP_ASSERT_CONSISTENT(map);
1470 CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map,
1471 map->nentries, entry);
1475 * vm_map_entry_resize:
1477 * Resize a vm_map_entry, recompute the amount of free space that
1478 * follows it and propagate that value up the tree.
1480 * The map must be locked, and leaves it so.
1483 vm_map_entry_resize(vm_map_t map, vm_map_entry_t entry, vm_size_t grow_amount)
1485 vm_map_entry_t header, llist, rlist, root;
1487 VM_MAP_ASSERT_LOCKED(map);
1488 header = &map->header;
1489 root = vm_map_splay_split(map, entry->start, 0, &llist, &rlist);
1490 KASSERT(root != NULL, ("%s: resize object not mapped", __func__));
1491 vm_map_splay_findnext(root, &rlist);
1492 entry->end += grow_amount;
1493 root->max_free = vm_size_max(
1494 vm_map_splay_merge_left(header, root, llist),
1495 vm_map_splay_merge_succ(header, root, rlist));
1497 VM_MAP_ASSERT_CONSISTENT(map);
1498 CTR4(KTR_VM, "%s: map %p, nentries %d, entry %p",
1499 __func__, map, map->nentries, entry);
1503 * vm_map_lookup_entry: [ internal use only ]
1505 * Finds the map entry containing (or
1506 * immediately preceding) the specified address
1507 * in the given map; the entry is returned
1508 * in the "entry" parameter. The boolean
1509 * result indicates whether the address is
1510 * actually contained in the map.
1513 vm_map_lookup_entry(
1515 vm_offset_t address,
1516 vm_map_entry_t *entry) /* OUT */
1518 vm_map_entry_t cur, header, lbound, ubound;
1522 * If the map is empty, then the map entry immediately preceding
1523 * "address" is the map's header.
1525 header = &map->header;
1531 if (address >= cur->start && cur->end > address) {
1535 if ((locked = vm_map_locked(map)) ||
1536 sx_try_upgrade(&map->lock)) {
1538 * Splay requires a write lock on the map. However, it only
1539 * restructures the binary search tree; it does not otherwise
1540 * change the map. Thus, the map's timestamp need not change
1541 * on a temporary upgrade.
1543 cur = vm_map_splay(map, address);
1545 VM_MAP_UNLOCK_CONSISTENT(map);
1546 sx_downgrade(&map->lock);
1550 * If "address" is contained within a map entry, the new root
1551 * is that map entry. Otherwise, the new root is a map entry
1552 * immediately before or after "address".
1554 if (address < cur->start) {
1559 return (address < cur->end);
1562 * Since the map is only locked for read access, perform a
1563 * standard binary search tree lookup for "address".
1565 lbound = ubound = header;
1567 if (address < cur->start) {
1572 } else if (cur->end <= address) {
1589 * Inserts the given whole VM object into the target
1590 * map at the specified address range. The object's
1591 * size should match that of the address range.
1593 * Requires that the map be locked, and leaves it so.
1595 * If object is non-NULL, ref count must be bumped by caller
1596 * prior to making call to account for the new entry.
1599 vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1600 vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max, int cow)
1602 vm_map_entry_t new_entry, next_entry, prev_entry;
1604 vm_eflags_t protoeflags;
1605 vm_inherit_t inheritance;
1607 VM_MAP_ASSERT_LOCKED(map);
1608 KASSERT(object != kernel_object ||
1609 (cow & MAP_COPY_ON_WRITE) == 0,
1610 ("vm_map_insert: kernel object and COW"));
1611 KASSERT(object == NULL || (cow & MAP_NOFAULT) == 0,
1612 ("vm_map_insert: paradoxical MAP_NOFAULT request"));
1613 KASSERT((prot & ~max) == 0,
1614 ("prot %#x is not subset of max_prot %#x", prot, max));
1617 * Check that the start and end points are not bogus.
1619 if (start < vm_map_min(map) || end > vm_map_max(map) ||
1621 return (KERN_INVALID_ADDRESS);
1624 * Find the entry prior to the proposed starting address; if it's part
1625 * of an existing entry, this range is bogus.
1627 if (vm_map_lookup_entry(map, start, &prev_entry))
1628 return (KERN_NO_SPACE);
1631 * Assert that the next entry doesn't overlap the end point.
1633 next_entry = vm_map_entry_succ(prev_entry);
1634 if (next_entry->start < end)
1635 return (KERN_NO_SPACE);
1637 if ((cow & MAP_CREATE_GUARD) != 0 && (object != NULL ||
1638 max != VM_PROT_NONE))
1639 return (KERN_INVALID_ARGUMENT);
1642 if (cow & MAP_COPY_ON_WRITE)
1643 protoeflags |= MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY;
1644 if (cow & MAP_NOFAULT)
1645 protoeflags |= MAP_ENTRY_NOFAULT;
1646 if (cow & MAP_DISABLE_SYNCER)
1647 protoeflags |= MAP_ENTRY_NOSYNC;
1648 if (cow & MAP_DISABLE_COREDUMP)
1649 protoeflags |= MAP_ENTRY_NOCOREDUMP;
1650 if (cow & MAP_STACK_GROWS_DOWN)
1651 protoeflags |= MAP_ENTRY_GROWS_DOWN;
1652 if (cow & MAP_STACK_GROWS_UP)
1653 protoeflags |= MAP_ENTRY_GROWS_UP;
1654 if (cow & MAP_WRITECOUNT)
1655 protoeflags |= MAP_ENTRY_WRITECNT;
1656 if (cow & MAP_VN_EXEC)
1657 protoeflags |= MAP_ENTRY_VN_EXEC;
1658 if ((cow & MAP_CREATE_GUARD) != 0)
1659 protoeflags |= MAP_ENTRY_GUARD;
1660 if ((cow & MAP_CREATE_STACK_GAP_DN) != 0)
1661 protoeflags |= MAP_ENTRY_STACK_GAP_DN;
1662 if ((cow & MAP_CREATE_STACK_GAP_UP) != 0)
1663 protoeflags |= MAP_ENTRY_STACK_GAP_UP;
1664 if (cow & MAP_INHERIT_SHARE)
1665 inheritance = VM_INHERIT_SHARE;
1667 inheritance = VM_INHERIT_DEFAULT;
1670 if ((cow & (MAP_ACC_NO_CHARGE | MAP_NOFAULT | MAP_CREATE_GUARD)) != 0)
1672 if ((cow & MAP_ACC_CHARGED) || ((prot & VM_PROT_WRITE) &&
1673 ((protoeflags & MAP_ENTRY_NEEDS_COPY) || object == NULL))) {
1674 if (!(cow & MAP_ACC_CHARGED) && !swap_reserve(end - start))
1675 return (KERN_RESOURCE_SHORTAGE);
1676 KASSERT(object == NULL ||
1677 (protoeflags & MAP_ENTRY_NEEDS_COPY) != 0 ||
1678 object->cred == NULL,
1679 ("overcommit: vm_map_insert o %p", object));
1680 cred = curthread->td_ucred;
1684 /* Expand the kernel pmap, if necessary. */
1685 if (map == kernel_map && end > kernel_vm_end)
1686 pmap_growkernel(end);
1687 if (object != NULL) {
1689 * OBJ_ONEMAPPING must be cleared unless this mapping
1690 * is trivially proven to be the only mapping for any
1691 * of the object's pages. (Object granularity
1692 * reference counting is insufficient to recognize
1693 * aliases with precision.)
1695 if ((object->flags & OBJ_ANON) != 0) {
1696 VM_OBJECT_WLOCK(object);
1697 if (object->ref_count > 1 || object->shadow_count != 0)
1698 vm_object_clear_flag(object, OBJ_ONEMAPPING);
1699 VM_OBJECT_WUNLOCK(object);
1701 } else if ((prev_entry->eflags & ~MAP_ENTRY_USER_WIRED) ==
1703 (cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP |
1704 MAP_VN_EXEC)) == 0 &&
1705 prev_entry->end == start && (prev_entry->cred == cred ||
1706 (prev_entry->object.vm_object != NULL &&
1707 prev_entry->object.vm_object->cred == cred)) &&
1708 vm_object_coalesce(prev_entry->object.vm_object,
1710 (vm_size_t)(prev_entry->end - prev_entry->start),
1711 (vm_size_t)(end - prev_entry->end), cred != NULL &&
1712 (protoeflags & MAP_ENTRY_NEEDS_COPY) == 0)) {
1714 * We were able to extend the object. Determine if we
1715 * can extend the previous map entry to include the
1716 * new range as well.
1718 if (prev_entry->inheritance == inheritance &&
1719 prev_entry->protection == prot &&
1720 prev_entry->max_protection == max &&
1721 prev_entry->wired_count == 0) {
1722 KASSERT((prev_entry->eflags & MAP_ENTRY_USER_WIRED) ==
1723 0, ("prev_entry %p has incoherent wiring",
1725 if ((prev_entry->eflags & MAP_ENTRY_GUARD) == 0)
1726 map->size += end - prev_entry->end;
1727 vm_map_entry_resize(map, prev_entry,
1728 end - prev_entry->end);
1729 vm_map_try_merge_entries(map, prev_entry, next_entry);
1730 return (KERN_SUCCESS);
1734 * If we can extend the object but cannot extend the
1735 * map entry, we have to create a new map entry. We
1736 * must bump the ref count on the extended object to
1737 * account for it. object may be NULL.
1739 object = prev_entry->object.vm_object;
1740 offset = prev_entry->offset +
1741 (prev_entry->end - prev_entry->start);
1742 vm_object_reference(object);
1743 if (cred != NULL && object != NULL && object->cred != NULL &&
1744 !(prev_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
1745 /* Object already accounts for this uid. */
1753 * Create a new entry
1755 new_entry = vm_map_entry_create(map);
1756 new_entry->start = start;
1757 new_entry->end = end;
1758 new_entry->cred = NULL;
1760 new_entry->eflags = protoeflags;
1761 new_entry->object.vm_object = object;
1762 new_entry->offset = offset;
1764 new_entry->inheritance = inheritance;
1765 new_entry->protection = prot;
1766 new_entry->max_protection = max;
1767 new_entry->wired_count = 0;
1768 new_entry->wiring_thread = NULL;
1769 new_entry->read_ahead = VM_FAULT_READ_AHEAD_INIT;
1770 new_entry->next_read = start;
1772 KASSERT(cred == NULL || !ENTRY_CHARGED(new_entry),
1773 ("overcommit: vm_map_insert leaks vm_map %p", new_entry));
1774 new_entry->cred = cred;
1777 * Insert the new entry into the list
1779 vm_map_entry_link(map, new_entry);
1780 if ((new_entry->eflags & MAP_ENTRY_GUARD) == 0)
1781 map->size += new_entry->end - new_entry->start;
1784 * Try to coalesce the new entry with both the previous and next
1785 * entries in the list. Previously, we only attempted to coalesce
1786 * with the previous entry when object is NULL. Here, we handle the
1787 * other cases, which are less common.
1789 vm_map_try_merge_entries(map, prev_entry, new_entry);
1790 vm_map_try_merge_entries(map, new_entry, next_entry);
1792 if ((cow & (MAP_PREFAULT | MAP_PREFAULT_PARTIAL)) != 0) {
1793 vm_map_pmap_enter(map, start, prot, object, OFF_TO_IDX(offset),
1794 end - start, cow & MAP_PREFAULT_PARTIAL);
1797 return (KERN_SUCCESS);
1803 * Find the first fit (lowest VM address) for "length" free bytes
1804 * beginning at address >= start in the given map.
1806 * In a vm_map_entry, "max_free" is the maximum amount of
1807 * contiguous free space between an entry in its subtree and a
1808 * neighbor of that entry. This allows finding a free region in
1809 * one path down the tree, so O(log n) amortized with splay
1812 * The map must be locked, and leaves it so.
1814 * Returns: starting address if sufficient space,
1815 * vm_map_max(map)-length+1 if insufficient space.
1818 vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length)
1820 vm_map_entry_t header, llist, rlist, root, y;
1821 vm_size_t left_length, max_free_left, max_free_right;
1822 vm_offset_t gap_end;
1825 * Request must fit within min/max VM address and must avoid
1828 start = MAX(start, vm_map_min(map));
1829 if (start >= vm_map_max(map) || length > vm_map_max(map) - start)
1830 return (vm_map_max(map) - length + 1);
1832 /* Empty tree means wide open address space. */
1833 if (map->root == NULL)
1837 * After splay_split, if start is within an entry, push it to the start
1838 * of the following gap. If rlist is at the end of the gap containing
1839 * start, save the end of that gap in gap_end to see if the gap is big
1840 * enough; otherwise set gap_end to start skip gap-checking and move
1841 * directly to a search of the right subtree.
1843 header = &map->header;
1844 root = vm_map_splay_split(map, start, length, &llist, &rlist);
1845 gap_end = rlist->start;
1848 if (root->right != rlist)
1850 max_free_left = vm_map_splay_merge_left(header, root, llist);
1851 max_free_right = vm_map_splay_merge_right(header, root, rlist);
1852 } else if (rlist != header) {
1855 max_free_left = vm_map_splay_merge_pred(header, root, llist);
1856 max_free_right = vm_map_splay_merge_right(header, root, rlist);
1859 llist = root->right;
1860 max_free_left = vm_map_splay_merge_left(header, root, llist);
1861 max_free_right = vm_map_splay_merge_succ(header, root, rlist);
1863 root->max_free = vm_size_max(max_free_left, max_free_right);
1865 VM_MAP_ASSERT_CONSISTENT(map);
1866 if (length <= gap_end - start)
1869 /* With max_free, can immediately tell if no solution. */
1870 if (root->right == header || length > root->right->max_free)
1871 return (vm_map_max(map) - length + 1);
1874 * Splay for the least large-enough gap in the right subtree.
1876 llist = rlist = header;
1877 for (left_length = 0;;
1878 left_length = vm_map_entry_max_free_left(root, llist)) {
1879 if (length <= left_length)
1880 SPLAY_LEFT_STEP(root, y, llist, rlist,
1881 length <= vm_map_entry_max_free_left(y, llist));
1883 SPLAY_RIGHT_STEP(root, y, llist, rlist,
1884 length > vm_map_entry_max_free_left(y, root));
1889 llist = root->right;
1890 max_free_left = vm_map_splay_merge_left(header, root, llist);
1891 if (rlist == header) {
1892 root->max_free = vm_size_max(max_free_left,
1893 vm_map_splay_merge_succ(header, root, rlist));
1897 y->max_free = vm_size_max(
1898 vm_map_splay_merge_pred(root, y, root),
1899 vm_map_splay_merge_right(header, y, rlist));
1900 root->max_free = vm_size_max(max_free_left, y->max_free);
1903 VM_MAP_ASSERT_CONSISTENT(map);
1908 vm_map_fixed(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1909 vm_offset_t start, vm_size_t length, vm_prot_t prot,
1910 vm_prot_t max, int cow)
1915 end = start + length;
1916 KASSERT((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0 ||
1918 ("vm_map_fixed: non-NULL backing object for stack"));
1920 VM_MAP_RANGE_CHECK(map, start, end);
1921 if ((cow & MAP_CHECK_EXCL) == 0)
1922 vm_map_delete(map, start, end);
1923 if ((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) != 0) {
1924 result = vm_map_stack_locked(map, start, length, sgrowsiz,
1927 result = vm_map_insert(map, object, offset, start, end,
1934 static const int aslr_pages_rnd_64[2] = {0x1000, 0x10};
1935 static const int aslr_pages_rnd_32[2] = {0x100, 0x4};
1937 static int cluster_anon = 1;
1938 SYSCTL_INT(_vm, OID_AUTO, cluster_anon, CTLFLAG_RW,
1940 "Cluster anonymous mappings: 0 = no, 1 = yes if no hint, 2 = always");
1943 clustering_anon_allowed(vm_offset_t addr)
1946 switch (cluster_anon) {
1957 static long aslr_restarts;
1958 SYSCTL_LONG(_vm, OID_AUTO, aslr_restarts, CTLFLAG_RD,
1960 "Number of aslr failures");
1962 #define MAP_32BIT_MAX_ADDR ((vm_offset_t)1 << 31)
1965 * Searches for the specified amount of free space in the given map with the
1966 * specified alignment. Performs an address-ordered, first-fit search from
1967 * the given address "*addr", with an optional upper bound "max_addr". If the
1968 * parameter "alignment" is zero, then the alignment is computed from the
1969 * given (object, offset) pair so as to enable the greatest possible use of
1970 * superpage mappings. Returns KERN_SUCCESS and the address of the free space
1971 * in "*addr" if successful. Otherwise, returns KERN_NO_SPACE.
1973 * The map must be locked. Initially, there must be at least "length" bytes
1974 * of free space at the given address.
1977 vm_map_alignspace(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1978 vm_offset_t *addr, vm_size_t length, vm_offset_t max_addr,
1979 vm_offset_t alignment)
1981 vm_offset_t aligned_addr, free_addr;
1983 VM_MAP_ASSERT_LOCKED(map);
1985 KASSERT(free_addr == vm_map_findspace(map, free_addr, length),
1986 ("caller failed to provide space %#jx at address %p",
1987 (uintmax_t)length, (void *)free_addr));
1990 * At the start of every iteration, the free space at address
1991 * "*addr" is at least "length" bytes.
1994 pmap_align_superpage(object, offset, addr, length);
1995 else if ((*addr & (alignment - 1)) != 0) {
1996 *addr &= ~(alignment - 1);
1999 aligned_addr = *addr;
2000 if (aligned_addr == free_addr) {
2002 * Alignment did not change "*addr", so "*addr" must
2003 * still provide sufficient free space.
2005 return (KERN_SUCCESS);
2009 * Test for address wrap on "*addr". A wrapped "*addr" could
2010 * be a valid address, in which case vm_map_findspace() cannot
2011 * be relied upon to fail.
2013 if (aligned_addr < free_addr)
2014 return (KERN_NO_SPACE);
2015 *addr = vm_map_findspace(map, aligned_addr, length);
2016 if (*addr + length > vm_map_max(map) ||
2017 (max_addr != 0 && *addr + length > max_addr))
2018 return (KERN_NO_SPACE);
2020 if (free_addr == aligned_addr) {
2022 * If a successful call to vm_map_findspace() did not
2023 * change "*addr", then "*addr" must still be aligned
2024 * and provide sufficient free space.
2026 return (KERN_SUCCESS);
2032 * vm_map_find finds an unallocated region in the target address
2033 * map with the given length. The search is defined to be
2034 * first-fit from the specified address; the region found is
2035 * returned in the same parameter.
2037 * If object is non-NULL, ref count must be bumped by caller
2038 * prior to making call to account for the new entry.
2041 vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
2042 vm_offset_t *addr, /* IN/OUT */
2043 vm_size_t length, vm_offset_t max_addr, int find_space,
2044 vm_prot_t prot, vm_prot_t max, int cow)
2046 vm_offset_t alignment, curr_min_addr, min_addr;
2047 int gap, pidx, rv, try;
2048 bool cluster, en_aslr, update_anon;
2050 KASSERT((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0 ||
2052 ("vm_map_find: non-NULL backing object for stack"));
2053 MPASS((cow & MAP_REMAP) == 0 || (find_space == VMFS_NO_SPACE &&
2054 (cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0));
2055 if (find_space == VMFS_OPTIMAL_SPACE && (object == NULL ||
2056 (object->flags & OBJ_COLORED) == 0))
2057 find_space = VMFS_ANY_SPACE;
2058 if (find_space >> 8 != 0) {
2059 KASSERT((find_space & 0xff) == 0, ("bad VMFS flags"));
2060 alignment = (vm_offset_t)1 << (find_space >> 8);
2063 en_aslr = (map->flags & MAP_ASLR) != 0;
2064 update_anon = cluster = clustering_anon_allowed(*addr) &&
2065 (map->flags & MAP_IS_SUB_MAP) == 0 && max_addr == 0 &&
2066 find_space != VMFS_NO_SPACE && object == NULL &&
2067 (cow & (MAP_INHERIT_SHARE | MAP_STACK_GROWS_UP |
2068 MAP_STACK_GROWS_DOWN)) == 0 && prot != PROT_NONE;
2069 curr_min_addr = min_addr = *addr;
2070 if (en_aslr && min_addr == 0 && !cluster &&
2071 find_space != VMFS_NO_SPACE &&
2072 (map->flags & MAP_ASLR_IGNSTART) != 0)
2073 curr_min_addr = min_addr = vm_map_min(map);
2077 curr_min_addr = map->anon_loc;
2078 if (curr_min_addr == 0)
2081 if (find_space != VMFS_NO_SPACE) {
2082 KASSERT(find_space == VMFS_ANY_SPACE ||
2083 find_space == VMFS_OPTIMAL_SPACE ||
2084 find_space == VMFS_SUPER_SPACE ||
2085 alignment != 0, ("unexpected VMFS flag"));
2088 * When creating an anonymous mapping, try clustering
2089 * with an existing anonymous mapping first.
2091 * We make up to two attempts to find address space
2092 * for a given find_space value. The first attempt may
2093 * apply randomization or may cluster with an existing
2094 * anonymous mapping. If this first attempt fails,
2095 * perform a first-fit search of the available address
2098 * If all tries failed, and find_space is
2099 * VMFS_OPTIMAL_SPACE, fallback to VMFS_ANY_SPACE.
2100 * Again enable clustering and randomization.
2107 * Second try: we failed either to find a
2108 * suitable region for randomizing the
2109 * allocation, or to cluster with an existing
2110 * mapping. Retry with free run.
2112 curr_min_addr = (map->flags & MAP_ASLR_IGNSTART) != 0 ?
2113 vm_map_min(map) : min_addr;
2114 atomic_add_long(&aslr_restarts, 1);
2117 if (try == 1 && en_aslr && !cluster) {
2119 * Find space for allocation, including
2120 * gap needed for later randomization.
2122 pidx = MAXPAGESIZES > 1 && pagesizes[1] != 0 &&
2123 (find_space == VMFS_SUPER_SPACE || find_space ==
2124 VMFS_OPTIMAL_SPACE) ? 1 : 0;
2125 gap = vm_map_max(map) > MAP_32BIT_MAX_ADDR &&
2126 (max_addr == 0 || max_addr > MAP_32BIT_MAX_ADDR) ?
2127 aslr_pages_rnd_64[pidx] : aslr_pages_rnd_32[pidx];
2128 *addr = vm_map_findspace(map, curr_min_addr,
2129 length + gap * pagesizes[pidx]);
2130 if (*addr + length + gap * pagesizes[pidx] >
2133 /* And randomize the start address. */
2134 *addr += (arc4random() % gap) * pagesizes[pidx];
2135 if (max_addr != 0 && *addr + length > max_addr)
2138 *addr = vm_map_findspace(map, curr_min_addr, length);
2139 if (*addr + length > vm_map_max(map) ||
2140 (max_addr != 0 && *addr + length > max_addr)) {
2151 if (find_space != VMFS_ANY_SPACE &&
2152 (rv = vm_map_alignspace(map, object, offset, addr, length,
2153 max_addr, alignment)) != KERN_SUCCESS) {
2154 if (find_space == VMFS_OPTIMAL_SPACE) {
2155 find_space = VMFS_ANY_SPACE;
2156 curr_min_addr = min_addr;
2157 cluster = update_anon;
2163 } else if ((cow & MAP_REMAP) != 0) {
2164 if (*addr < vm_map_min(map) ||
2165 *addr + length > vm_map_max(map) ||
2166 *addr + length <= length) {
2167 rv = KERN_INVALID_ADDRESS;
2170 vm_map_delete(map, *addr, *addr + length);
2172 if ((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) != 0) {
2173 rv = vm_map_stack_locked(map, *addr, length, sgrowsiz, prot,
2176 rv = vm_map_insert(map, object, offset, *addr, *addr + length,
2179 if (rv == KERN_SUCCESS && update_anon)
2180 map->anon_loc = *addr + length;
2187 * vm_map_find_min() is a variant of vm_map_find() that takes an
2188 * additional parameter (min_addr) and treats the given address
2189 * (*addr) differently. Specifically, it treats *addr as a hint
2190 * and not as the minimum address where the mapping is created.
2192 * This function works in two phases. First, it tries to
2193 * allocate above the hint. If that fails and the hint is
2194 * greater than min_addr, it performs a second pass, replacing
2195 * the hint with min_addr as the minimum address for the
2199 vm_map_find_min(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
2200 vm_offset_t *addr, vm_size_t length, vm_offset_t min_addr,
2201 vm_offset_t max_addr, int find_space, vm_prot_t prot, vm_prot_t max,
2209 rv = vm_map_find(map, object, offset, addr, length, max_addr,
2210 find_space, prot, max, cow);
2211 if (rv == KERN_SUCCESS || min_addr >= hint)
2213 *addr = hint = min_addr;
2218 * A map entry with any of the following flags set must not be merged with
2221 #define MAP_ENTRY_NOMERGE_MASK (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP | \
2222 MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP | MAP_ENTRY_VN_EXEC)
2225 vm_map_mergeable_neighbors(vm_map_entry_t prev, vm_map_entry_t entry)
2228 KASSERT((prev->eflags & MAP_ENTRY_NOMERGE_MASK) == 0 ||
2229 (entry->eflags & MAP_ENTRY_NOMERGE_MASK) == 0,
2230 ("vm_map_mergeable_neighbors: neither %p nor %p are mergeable",
2232 return (prev->end == entry->start &&
2233 prev->object.vm_object == entry->object.vm_object &&
2234 (prev->object.vm_object == NULL ||
2235 prev->offset + (prev->end - prev->start) == entry->offset) &&
2236 prev->eflags == entry->eflags &&
2237 prev->protection == entry->protection &&
2238 prev->max_protection == entry->max_protection &&
2239 prev->inheritance == entry->inheritance &&
2240 prev->wired_count == entry->wired_count &&
2241 prev->cred == entry->cred);
2245 vm_map_merged_neighbor_dispose(vm_map_t map, vm_map_entry_t entry)
2249 * If the backing object is a vnode object, vm_object_deallocate()
2250 * calls vrele(). However, vrele() does not lock the vnode because
2251 * the vnode has additional references. Thus, the map lock can be
2252 * kept without causing a lock-order reversal with the vnode lock.
2254 * Since we count the number of virtual page mappings in
2255 * object->un_pager.vnp.writemappings, the writemappings value
2256 * should not be adjusted when the entry is disposed of.
2258 if (entry->object.vm_object != NULL)
2259 vm_object_deallocate(entry->object.vm_object);
2260 if (entry->cred != NULL)
2261 crfree(entry->cred);
2262 vm_map_entry_dispose(map, entry);
2266 * vm_map_try_merge_entries:
2268 * Compare the given map entry to its predecessor, and merge its precessor
2269 * into it if possible. The entry remains valid, and may be extended.
2270 * The predecessor may be deleted.
2272 * The map must be locked.
2275 vm_map_try_merge_entries(vm_map_t map, vm_map_entry_t prev_entry,
2276 vm_map_entry_t entry)
2279 VM_MAP_ASSERT_LOCKED(map);
2280 if ((entry->eflags & MAP_ENTRY_NOMERGE_MASK) == 0 &&
2281 vm_map_mergeable_neighbors(prev_entry, entry)) {
2282 vm_map_entry_unlink(map, prev_entry, UNLINK_MERGE_NEXT);
2283 vm_map_merged_neighbor_dispose(map, prev_entry);
2288 * vm_map_entry_back:
2290 * Allocate an object to back a map entry.
2293 vm_map_entry_back(vm_map_entry_t entry)
2297 KASSERT(entry->object.vm_object == NULL,
2298 ("map entry %p has backing object", entry));
2299 KASSERT((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0,
2300 ("map entry %p is a submap", entry));
2301 object = vm_object_allocate_anon(atop(entry->end - entry->start), NULL,
2302 entry->cred, entry->end - entry->start);
2303 entry->object.vm_object = object;
2309 * vm_map_entry_charge_object
2311 * If there is no object backing this entry, create one. Otherwise, if
2312 * the entry has cred, give it to the backing object.
2315 vm_map_entry_charge_object(vm_map_t map, vm_map_entry_t entry)
2318 VM_MAP_ASSERT_LOCKED(map);
2319 KASSERT((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0,
2320 ("map entry %p is a submap", entry));
2321 if (entry->object.vm_object == NULL && !map->system_map &&
2322 (entry->eflags & MAP_ENTRY_GUARD) == 0)
2323 vm_map_entry_back(entry);
2324 else if (entry->object.vm_object != NULL &&
2325 ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) &&
2326 entry->cred != NULL) {
2327 VM_OBJECT_WLOCK(entry->object.vm_object);
2328 KASSERT(entry->object.vm_object->cred == NULL,
2329 ("OVERCOMMIT: %s: both cred e %p", __func__, entry));
2330 entry->object.vm_object->cred = entry->cred;
2331 entry->object.vm_object->charge = entry->end - entry->start;
2332 VM_OBJECT_WUNLOCK(entry->object.vm_object);
2338 * vm_map_entry_clone
2340 * Create a duplicate map entry for clipping.
2342 static vm_map_entry_t
2343 vm_map_entry_clone(vm_map_t map, vm_map_entry_t entry)
2345 vm_map_entry_t new_entry;
2347 VM_MAP_ASSERT_LOCKED(map);
2350 * Create a backing object now, if none exists, so that more individual
2351 * objects won't be created after the map entry is split.
2353 vm_map_entry_charge_object(map, entry);
2355 /* Clone the entry. */
2356 new_entry = vm_map_entry_create(map);
2357 *new_entry = *entry;
2358 if (new_entry->cred != NULL)
2359 crhold(entry->cred);
2360 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
2361 vm_object_reference(new_entry->object.vm_object);
2362 vm_map_entry_set_vnode_text(new_entry, true);
2364 * The object->un_pager.vnp.writemappings for the object of
2365 * MAP_ENTRY_WRITECNT type entry shall be kept as is here. The
2366 * virtual pages are re-distributed among the clipped entries,
2367 * so the sum is left the same.
2374 * vm_map_clip_start: [ internal use only ]
2376 * Asserts that the given entry begins at or after
2377 * the specified address; if necessary,
2378 * it splits the entry into two.
2380 #define vm_map_clip_start(map, entry, startaddr) \
2382 if (startaddr > entry->start) \
2383 _vm_map_clip_start(map, entry, startaddr); \
2387 * This routine is called only when it is known that
2388 * the entry must be split.
2391 _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start)
2393 vm_map_entry_t new_entry;
2395 VM_MAP_ASSERT_LOCKED(map);
2396 KASSERT(entry->end > start && entry->start < start,
2397 ("_vm_map_clip_start: invalid clip of entry %p", entry));
2399 new_entry = vm_map_entry_clone(map, entry);
2402 * Split off the front portion. Insert the new entry BEFORE this one,
2403 * so that this entry has the specified starting address.
2405 new_entry->end = start;
2406 vm_map_entry_link(map, new_entry);
2410 * vm_map_lookup_clip_start:
2412 * Find the entry at or just after 'start', and clip it if 'start' is in
2413 * the interior of the entry. Return entry after 'start', and in
2414 * prev_entry set the entry before 'start'.
2416 static inline vm_map_entry_t
2417 vm_map_lookup_clip_start(vm_map_t map, vm_offset_t start,
2418 vm_map_entry_t *prev_entry)
2420 vm_map_entry_t entry;
2422 if (vm_map_lookup_entry(map, start, prev_entry)) {
2423 entry = *prev_entry;
2424 vm_map_clip_start(map, entry, start);
2425 *prev_entry = vm_map_entry_pred(entry);
2427 entry = vm_map_entry_succ(*prev_entry);
2432 * vm_map_clip_end: [ internal use only ]
2434 * Asserts that the given entry ends at or before
2435 * the specified address; if necessary,
2436 * it splits the entry into two.
2438 #define vm_map_clip_end(map, entry, endaddr) \
2440 if ((endaddr) < (entry->end)) \
2441 _vm_map_clip_end((map), (entry), (endaddr)); \
2445 * This routine is called only when it is known that
2446 * the entry must be split.
2449 _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end)
2451 vm_map_entry_t new_entry;
2453 VM_MAP_ASSERT_LOCKED(map);
2454 KASSERT(entry->start < end && entry->end > end,
2455 ("_vm_map_clip_end: invalid clip of entry %p", entry));
2457 new_entry = vm_map_entry_clone(map, entry);
2460 * Split off the back portion. Insert the new entry AFTER this one,
2461 * so that this entry has the specified ending address.
2463 new_entry->start = end;
2464 vm_map_entry_link(map, new_entry);
2468 * vm_map_submap: [ kernel use only ]
2470 * Mark the given range as handled by a subordinate map.
2472 * This range must have been created with vm_map_find,
2473 * and no other operations may have been performed on this
2474 * range prior to calling vm_map_submap.
2476 * Only a limited number of operations can be performed
2477 * within this rage after calling vm_map_submap:
2479 * [Don't try vm_map_copy!]
2481 * To remove a submapping, one must first remove the
2482 * range from the superior map, and then destroy the
2483 * submap (if desired). [Better yet, don't try it.]
2492 vm_map_entry_t entry;
2495 result = KERN_INVALID_ARGUMENT;
2497 vm_map_lock(submap);
2498 submap->flags |= MAP_IS_SUB_MAP;
2499 vm_map_unlock(submap);
2502 VM_MAP_RANGE_CHECK(map, start, end);
2503 if (vm_map_lookup_entry(map, start, &entry) && entry->end >= end &&
2504 (entry->eflags & MAP_ENTRY_COW) == 0 &&
2505 entry->object.vm_object == NULL) {
2506 vm_map_clip_start(map, entry, start);
2507 vm_map_clip_end(map, entry, end);
2508 entry->object.sub_map = submap;
2509 entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
2510 result = KERN_SUCCESS;
2514 if (result != KERN_SUCCESS) {
2515 vm_map_lock(submap);
2516 submap->flags &= ~MAP_IS_SUB_MAP;
2517 vm_map_unlock(submap);
2523 * The maximum number of pages to map if MAP_PREFAULT_PARTIAL is specified
2525 #define MAX_INIT_PT 96
2528 * vm_map_pmap_enter:
2530 * Preload the specified map's pmap with mappings to the specified
2531 * object's memory-resident pages. No further physical pages are
2532 * allocated, and no further virtual pages are retrieved from secondary
2533 * storage. If the specified flags include MAP_PREFAULT_PARTIAL, then a
2534 * limited number of page mappings are created at the low-end of the
2535 * specified address range. (For this purpose, a superpage mapping
2536 * counts as one page mapping.) Otherwise, all resident pages within
2537 * the specified address range are mapped.
2540 vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
2541 vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags)
2544 vm_page_t p, p_start;
2545 vm_pindex_t mask, psize, threshold, tmpidx;
2547 if ((prot & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 || object == NULL)
2549 if (object->type == OBJT_DEVICE || object->type == OBJT_SG) {
2550 VM_OBJECT_WLOCK(object);
2551 if (object->type == OBJT_DEVICE || object->type == OBJT_SG) {
2552 pmap_object_init_pt(map->pmap, addr, object, pindex,
2554 VM_OBJECT_WUNLOCK(object);
2557 VM_OBJECT_LOCK_DOWNGRADE(object);
2559 VM_OBJECT_RLOCK(object);
2562 if (psize + pindex > object->size) {
2563 if (pindex >= object->size) {
2564 VM_OBJECT_RUNLOCK(object);
2567 psize = object->size - pindex;
2572 threshold = MAX_INIT_PT;
2574 p = vm_page_find_least(object, pindex);
2576 * Assert: the variable p is either (1) the page with the
2577 * least pindex greater than or equal to the parameter pindex
2581 p != NULL && (tmpidx = p->pindex - pindex) < psize;
2582 p = TAILQ_NEXT(p, listq)) {
2584 * don't allow an madvise to blow away our really
2585 * free pages allocating pv entries.
2587 if (((flags & MAP_PREFAULT_MADVISE) != 0 &&
2588 vm_page_count_severe()) ||
2589 ((flags & MAP_PREFAULT_PARTIAL) != 0 &&
2590 tmpidx >= threshold)) {
2594 if (vm_page_all_valid(p)) {
2595 if (p_start == NULL) {
2596 start = addr + ptoa(tmpidx);
2599 /* Jump ahead if a superpage mapping is possible. */
2600 if (p->psind > 0 && ((addr + ptoa(tmpidx)) &
2601 (pagesizes[p->psind] - 1)) == 0) {
2602 mask = atop(pagesizes[p->psind]) - 1;
2603 if (tmpidx + mask < psize &&
2604 vm_page_ps_test(p, PS_ALL_VALID, NULL)) {
2609 } else if (p_start != NULL) {
2610 pmap_enter_object(map->pmap, start, addr +
2611 ptoa(tmpidx), p_start, prot);
2615 if (p_start != NULL)
2616 pmap_enter_object(map->pmap, start, addr + ptoa(psize),
2618 VM_OBJECT_RUNLOCK(object);
2624 * Sets the protection of the specified address
2625 * region in the target map. If "set_max" is
2626 * specified, the maximum protection is to be set;
2627 * otherwise, only the current protection is affected.
2630 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
2631 vm_prot_t new_prot, boolean_t set_max)
2633 vm_map_entry_t entry, first_entry, in_tran, prev_entry;
2640 return (KERN_SUCCESS);
2647 * Ensure that we are not concurrently wiring pages. vm_map_wire() may
2648 * need to fault pages into the map and will drop the map lock while
2649 * doing so, and the VM object may end up in an inconsistent state if we
2650 * update the protection on the map entry in between faults.
2652 vm_map_wait_busy(map);
2654 VM_MAP_RANGE_CHECK(map, start, end);
2656 if (!vm_map_lookup_entry(map, start, &first_entry))
2657 first_entry = vm_map_entry_succ(first_entry);
2660 * Make a first pass to check for protection violations.
2662 for (entry = first_entry; entry->start < end;
2663 entry = vm_map_entry_succ(entry)) {
2664 if ((entry->eflags & MAP_ENTRY_GUARD) != 0)
2666 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0) {
2668 return (KERN_INVALID_ARGUMENT);
2670 if ((new_prot & entry->max_protection) != new_prot) {
2672 return (KERN_PROTECTION_FAILURE);
2674 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0)
2679 * Postpone the operation until all in-transition map entries have
2680 * stabilized. An in-transition entry might already have its pages
2681 * wired and wired_count incremented, but not yet have its
2682 * MAP_ENTRY_USER_WIRED flag set. In which case, we would fail to call
2683 * vm_fault_copy_entry() in the final loop below.
2685 if (in_tran != NULL) {
2686 in_tran->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2687 vm_map_unlock_and_wait(map, 0);
2692 * Before changing the protections, try to reserve swap space for any
2693 * private (i.e., copy-on-write) mappings that are transitioning from
2694 * read-only to read/write access. If a reservation fails, break out
2695 * of this loop early and let the next loop simplify the entries, since
2696 * some may now be mergeable.
2699 vm_map_clip_start(map, first_entry, start);
2700 for (entry = first_entry; entry->start < end;
2701 entry = vm_map_entry_succ(entry)) {
2702 vm_map_clip_end(map, entry, end);
2705 ((new_prot & ~entry->protection) & VM_PROT_WRITE) == 0 ||
2706 ENTRY_CHARGED(entry) ||
2707 (entry->eflags & MAP_ENTRY_GUARD) != 0) {
2711 cred = curthread->td_ucred;
2712 obj = entry->object.vm_object;
2715 (entry->eflags & MAP_ENTRY_NEEDS_COPY) != 0) {
2716 if (!swap_reserve(entry->end - entry->start)) {
2717 rv = KERN_RESOURCE_SHORTAGE;
2726 if (obj->type != OBJT_DEFAULT && obj->type != OBJT_SWAP)
2728 VM_OBJECT_WLOCK(obj);
2729 if (obj->type != OBJT_DEFAULT && obj->type != OBJT_SWAP) {
2730 VM_OBJECT_WUNLOCK(obj);
2735 * Charge for the whole object allocation now, since
2736 * we cannot distinguish between non-charged and
2737 * charged clipped mapping of the same object later.
2739 KASSERT(obj->charge == 0,
2740 ("vm_map_protect: object %p overcharged (entry %p)",
2742 if (!swap_reserve(ptoa(obj->size))) {
2743 VM_OBJECT_WUNLOCK(obj);
2744 rv = KERN_RESOURCE_SHORTAGE;
2751 obj->charge = ptoa(obj->size);
2752 VM_OBJECT_WUNLOCK(obj);
2756 * If enough swap space was available, go back and fix up protections.
2757 * Otherwise, just simplify entries, since some may have been modified.
2758 * [Note that clipping is not necessary the second time.]
2760 for (prev_entry = vm_map_entry_pred(first_entry), entry = first_entry;
2762 vm_map_try_merge_entries(map, prev_entry, entry),
2763 prev_entry = entry, entry = vm_map_entry_succ(entry)) {
2764 if (rv != KERN_SUCCESS ||
2765 (entry->eflags & MAP_ENTRY_GUARD) != 0)
2768 old_prot = entry->protection;
2772 (entry->max_protection = new_prot) &
2775 entry->protection = new_prot;
2778 * For user wired map entries, the normal lazy evaluation of
2779 * write access upgrades through soft page faults is
2780 * undesirable. Instead, immediately copy any pages that are
2781 * copy-on-write and enable write access in the physical map.
2783 if ((entry->eflags & MAP_ENTRY_USER_WIRED) != 0 &&
2784 (entry->protection & VM_PROT_WRITE) != 0 &&
2785 (old_prot & VM_PROT_WRITE) == 0)
2786 vm_fault_copy_entry(map, map, entry, entry, NULL);
2789 * When restricting access, update the physical map. Worry
2790 * about copy-on-write here.
2792 if ((old_prot & ~entry->protection) != 0) {
2793 #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
2795 pmap_protect(map->pmap, entry->start,
2797 entry->protection & MASK(entry));
2801 vm_map_try_merge_entries(map, prev_entry, entry);
2809 * This routine traverses a processes map handling the madvise
2810 * system call. Advisories are classified as either those effecting
2811 * the vm_map_entry structure, or those effecting the underlying
2821 vm_map_entry_t entry, prev_entry;
2825 * Some madvise calls directly modify the vm_map_entry, in which case
2826 * we need to use an exclusive lock on the map and we need to perform
2827 * various clipping operations. Otherwise we only need a read-lock
2832 case MADV_SEQUENTIAL:
2849 vm_map_lock_read(map);
2856 * Locate starting entry and clip if necessary.
2858 VM_MAP_RANGE_CHECK(map, start, end);
2862 * madvise behaviors that are implemented in the vm_map_entry.
2864 * We clip the vm_map_entry so that behavioral changes are
2865 * limited to the specified address range.
2867 for (entry = vm_map_lookup_clip_start(map, start, &prev_entry);
2869 prev_entry = entry, entry = vm_map_entry_succ(entry)) {
2870 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0)
2873 vm_map_clip_end(map, entry, end);
2877 vm_map_entry_set_behavior(entry,
2878 MAP_ENTRY_BEHAV_NORMAL);
2880 case MADV_SEQUENTIAL:
2881 vm_map_entry_set_behavior(entry,
2882 MAP_ENTRY_BEHAV_SEQUENTIAL);
2885 vm_map_entry_set_behavior(entry,
2886 MAP_ENTRY_BEHAV_RANDOM);
2889 entry->eflags |= MAP_ENTRY_NOSYNC;
2892 entry->eflags &= ~MAP_ENTRY_NOSYNC;
2895 entry->eflags |= MAP_ENTRY_NOCOREDUMP;
2898 entry->eflags &= ~MAP_ENTRY_NOCOREDUMP;
2903 vm_map_try_merge_entries(map, prev_entry, entry);
2905 vm_map_try_merge_entries(map, prev_entry, entry);
2908 vm_pindex_t pstart, pend;
2911 * madvise behaviors that are implemented in the underlying
2914 * Since we don't clip the vm_map_entry, we have to clip
2915 * the vm_object pindex and count.
2917 if (!vm_map_lookup_entry(map, start, &entry))
2918 entry = vm_map_entry_succ(entry);
2919 for (; entry->start < end;
2920 entry = vm_map_entry_succ(entry)) {
2921 vm_offset_t useEnd, useStart;
2923 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0)
2927 * MADV_FREE would otherwise rewind time to
2928 * the creation of the shadow object. Because
2929 * we hold the VM map read-locked, neither the
2930 * entry's object nor the presence of a
2931 * backing object can change.
2933 if (behav == MADV_FREE &&
2934 entry->object.vm_object != NULL &&
2935 entry->object.vm_object->backing_object != NULL)
2938 pstart = OFF_TO_IDX(entry->offset);
2939 pend = pstart + atop(entry->end - entry->start);
2940 useStart = entry->start;
2941 useEnd = entry->end;
2943 if (entry->start < start) {
2944 pstart += atop(start - entry->start);
2947 if (entry->end > end) {
2948 pend -= atop(entry->end - end);
2956 * Perform the pmap_advise() before clearing
2957 * PGA_REFERENCED in vm_page_advise(). Otherwise, a
2958 * concurrent pmap operation, such as pmap_remove(),
2959 * could clear a reference in the pmap and set
2960 * PGA_REFERENCED on the page before the pmap_advise()
2961 * had completed. Consequently, the page would appear
2962 * referenced based upon an old reference that
2963 * occurred before this pmap_advise() ran.
2965 if (behav == MADV_DONTNEED || behav == MADV_FREE)
2966 pmap_advise(map->pmap, useStart, useEnd,
2969 vm_object_madvise(entry->object.vm_object, pstart,
2973 * Pre-populate paging structures in the
2974 * WILLNEED case. For wired entries, the
2975 * paging structures are already populated.
2977 if (behav == MADV_WILLNEED &&
2978 entry->wired_count == 0) {
2979 vm_map_pmap_enter(map,
2982 entry->object.vm_object,
2984 ptoa(pend - pstart),
2985 MAP_PREFAULT_MADVISE
2989 vm_map_unlock_read(map);
2998 * Sets the inheritance of the specified address
2999 * range in the target map. Inheritance
3000 * affects how the map will be shared with
3001 * child maps at the time of vmspace_fork.
3004 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
3005 vm_inherit_t new_inheritance)
3007 vm_map_entry_t entry, prev_entry;
3009 switch (new_inheritance) {
3010 case VM_INHERIT_NONE:
3011 case VM_INHERIT_COPY:
3012 case VM_INHERIT_SHARE:
3013 case VM_INHERIT_ZERO:
3016 return (KERN_INVALID_ARGUMENT);
3019 return (KERN_SUCCESS);
3021 VM_MAP_RANGE_CHECK(map, start, end);
3022 for (entry = vm_map_lookup_clip_start(map, start, &prev_entry);
3024 prev_entry = entry, entry = vm_map_entry_succ(entry)) {
3025 vm_map_clip_end(map, entry, end);
3026 if ((entry->eflags & MAP_ENTRY_GUARD) == 0 ||
3027 new_inheritance != VM_INHERIT_ZERO)
3028 entry->inheritance = new_inheritance;
3029 vm_map_try_merge_entries(map, prev_entry, entry);
3031 vm_map_try_merge_entries(map, prev_entry, entry);
3033 return (KERN_SUCCESS);
3037 * vm_map_entry_in_transition:
3039 * Release the map lock, and sleep until the entry is no longer in
3040 * transition. Awake and acquire the map lock. If the map changed while
3041 * another held the lock, lookup a possibly-changed entry at or after the
3042 * 'start' position of the old entry.
3044 static vm_map_entry_t
3045 vm_map_entry_in_transition(vm_map_t map, vm_offset_t in_start,
3046 vm_offset_t *io_end, bool holes_ok, vm_map_entry_t in_entry)
3048 vm_map_entry_t entry;
3050 u_int last_timestamp;
3052 VM_MAP_ASSERT_LOCKED(map);
3053 KASSERT((in_entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0,
3054 ("not in-tranition map entry %p", in_entry));
3056 * We have not yet clipped the entry.
3058 start = MAX(in_start, in_entry->start);
3059 in_entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
3060 last_timestamp = map->timestamp;
3061 if (vm_map_unlock_and_wait(map, 0)) {
3063 * Allow interruption of user wiring/unwiring?
3067 if (last_timestamp + 1 == map->timestamp)
3071 * Look again for the entry because the map was modified while it was
3072 * unlocked. Specifically, the entry may have been clipped, merged, or
3075 if (!vm_map_lookup_entry(map, start, &entry)) {
3080 entry = vm_map_entry_succ(entry);
3088 * Implements both kernel and user unwiring.
3091 vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
3094 vm_map_entry_t entry, first_entry, next_entry, prev_entry;
3096 bool holes_ok, need_wakeup, user_unwire;
3099 return (KERN_SUCCESS);
3100 holes_ok = (flags & VM_MAP_WIRE_HOLESOK) != 0;
3101 user_unwire = (flags & VM_MAP_WIRE_USER) != 0;
3103 VM_MAP_RANGE_CHECK(map, start, end);
3104 if (!vm_map_lookup_entry(map, start, &first_entry)) {
3106 first_entry = vm_map_entry_succ(first_entry);
3109 return (KERN_INVALID_ADDRESS);
3113 for (entry = first_entry; entry->start < end; entry = next_entry) {
3114 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
3116 * We have not yet clipped the entry.
3118 next_entry = vm_map_entry_in_transition(map, start,
3119 &end, holes_ok, entry);
3120 if (next_entry == NULL) {
3121 if (entry == first_entry) {
3123 return (KERN_INVALID_ADDRESS);
3125 rv = KERN_INVALID_ADDRESS;
3128 first_entry = (entry == first_entry) ?
3132 vm_map_clip_start(map, entry, start);
3133 vm_map_clip_end(map, entry, end);
3135 * Mark the entry in case the map lock is released. (See
3138 KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 &&
3139 entry->wiring_thread == NULL,
3140 ("owned map entry %p", entry));
3141 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
3142 entry->wiring_thread = curthread;
3143 next_entry = vm_map_entry_succ(entry);
3145 * Check the map for holes in the specified region.
3146 * If holes_ok, skip this check.
3149 entry->end < end && next_entry->start > entry->end) {
3151 rv = KERN_INVALID_ADDRESS;
3155 * If system unwiring, require that the entry is system wired.
3158 vm_map_entry_system_wired_count(entry) == 0) {
3160 rv = KERN_INVALID_ARGUMENT;
3164 need_wakeup = false;
3165 if (first_entry == NULL &&
3166 !vm_map_lookup_entry(map, start, &first_entry)) {
3167 KASSERT(holes_ok, ("vm_map_unwire: lookup failed"));
3168 prev_entry = first_entry;
3169 entry = vm_map_entry_succ(first_entry);
3171 prev_entry = vm_map_entry_pred(first_entry);
3172 entry = first_entry;
3174 for (; entry->start < end;
3175 prev_entry = entry, entry = vm_map_entry_succ(entry)) {
3177 * If holes_ok was specified, an empty
3178 * space in the unwired region could have been mapped
3179 * while the map lock was dropped for draining
3180 * MAP_ENTRY_IN_TRANSITION. Moreover, another thread
3181 * could be simultaneously wiring this new mapping
3182 * entry. Detect these cases and skip any entries
3183 * marked as in transition by us.
3185 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 ||
3186 entry->wiring_thread != curthread) {
3188 ("vm_map_unwire: !HOLESOK and new/changed entry"));
3192 if (rv == KERN_SUCCESS && (!user_unwire ||
3193 (entry->eflags & MAP_ENTRY_USER_WIRED))) {
3194 if (entry->wired_count == 1)
3195 vm_map_entry_unwire(map, entry);
3197 entry->wired_count--;
3199 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
3201 KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0,
3202 ("vm_map_unwire: in-transition flag missing %p", entry));
3203 KASSERT(entry->wiring_thread == curthread,
3204 ("vm_map_unwire: alien wire %p", entry));
3205 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
3206 entry->wiring_thread = NULL;
3207 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
3208 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
3211 vm_map_try_merge_entries(map, prev_entry, entry);
3213 vm_map_try_merge_entries(map, prev_entry, entry);
3221 vm_map_wire_user_count_sub(u_long npages)
3224 atomic_subtract_long(&vm_user_wire_count, npages);
3228 vm_map_wire_user_count_add(u_long npages)
3232 wired = vm_user_wire_count;
3234 if (npages + wired > vm_page_max_user_wired)
3236 } while (!atomic_fcmpset_long(&vm_user_wire_count, &wired,
3243 * vm_map_wire_entry_failure:
3245 * Handle a wiring failure on the given entry.
3247 * The map should be locked.
3250 vm_map_wire_entry_failure(vm_map_t map, vm_map_entry_t entry,
3251 vm_offset_t failed_addr)
3254 VM_MAP_ASSERT_LOCKED(map);
3255 KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 &&
3256 entry->wired_count == 1,
3257 ("vm_map_wire_entry_failure: entry %p isn't being wired", entry));
3258 KASSERT(failed_addr < entry->end,
3259 ("vm_map_wire_entry_failure: entry %p was fully wired", entry));
3262 * If any pages at the start of this entry were successfully wired,
3265 if (failed_addr > entry->start) {
3266 pmap_unwire(map->pmap, entry->start, failed_addr);
3267 vm_object_unwire(entry->object.vm_object, entry->offset,
3268 failed_addr - entry->start, PQ_ACTIVE);
3272 * Assign an out-of-range value to represent the failure to wire this
3275 entry->wired_count = -1;
3279 vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end, int flags)
3284 rv = vm_map_wire_locked(map, start, end, flags);
3291 * vm_map_wire_locked:
3293 * Implements both kernel and user wiring. Returns with the map locked,
3294 * the map lock may be dropped.
3297 vm_map_wire_locked(vm_map_t map, vm_offset_t start, vm_offset_t end, int flags)
3299 vm_map_entry_t entry, first_entry, next_entry, prev_entry;
3300 vm_offset_t faddr, saved_end, saved_start;
3302 u_int last_timestamp;
3304 bool holes_ok, need_wakeup, user_wire;
3307 VM_MAP_ASSERT_LOCKED(map);
3310 return (KERN_SUCCESS);
3312 if (flags & VM_MAP_WIRE_WRITE)
3313 prot |= VM_PROT_WRITE;
3314 holes_ok = (flags & VM_MAP_WIRE_HOLESOK) != 0;
3315 user_wire = (flags & VM_MAP_WIRE_USER) != 0;
3316 VM_MAP_RANGE_CHECK(map, start, end);
3317 if (!vm_map_lookup_entry(map, start, &first_entry)) {
3319 first_entry = vm_map_entry_succ(first_entry);
3321 return (KERN_INVALID_ADDRESS);
3323 for (entry = first_entry; entry->start < end; entry = next_entry) {
3324 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
3326 * We have not yet clipped the entry.
3328 next_entry = vm_map_entry_in_transition(map, start,
3329 &end, holes_ok, entry);
3330 if (next_entry == NULL) {
3331 if (entry == first_entry)
3332 return (KERN_INVALID_ADDRESS);
3333 rv = KERN_INVALID_ADDRESS;
3336 first_entry = (entry == first_entry) ?
3340 vm_map_clip_start(map, entry, start);
3341 vm_map_clip_end(map, entry, end);
3343 * Mark the entry in case the map lock is released. (See
3346 KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 &&
3347 entry->wiring_thread == NULL,
3348 ("owned map entry %p", entry));
3349 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
3350 entry->wiring_thread = curthread;
3351 if ((entry->protection & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0
3352 || (entry->protection & prot) != prot) {
3353 entry->eflags |= MAP_ENTRY_WIRE_SKIPPED;
3356 rv = KERN_INVALID_ADDRESS;
3359 } else if (entry->wired_count == 0) {
3360 entry->wired_count++;
3362 npages = atop(entry->end - entry->start);
3363 if (user_wire && !vm_map_wire_user_count_add(npages)) {
3364 vm_map_wire_entry_failure(map, entry,
3367 rv = KERN_RESOURCE_SHORTAGE;
3372 * Release the map lock, relying on the in-transition
3373 * mark. Mark the map busy for fork.
3375 saved_start = entry->start;
3376 saved_end = entry->end;
3377 last_timestamp = map->timestamp;
3381 faddr = saved_start;
3384 * Simulate a fault to get the page and enter
3385 * it into the physical map.
3387 if ((rv = vm_fault(map, faddr,
3388 VM_PROT_NONE, VM_FAULT_WIRE, NULL)) !=
3391 } while ((faddr += PAGE_SIZE) < saved_end);
3394 if (last_timestamp + 1 != map->timestamp) {
3396 * Look again for the entry because the map was
3397 * modified while it was unlocked. The entry
3398 * may have been clipped, but NOT merged or
3401 if (!vm_map_lookup_entry(map, saved_start,
3404 ("vm_map_wire: lookup failed"));
3405 first_entry = (entry == first_entry) ?
3407 for (entry = next_entry; entry->end < saved_end;
3408 entry = vm_map_entry_succ(entry)) {
3410 * In case of failure, handle entries
3411 * that were not fully wired here;
3412 * fully wired entries are handled
3415 if (rv != KERN_SUCCESS &&
3417 vm_map_wire_entry_failure(map,
3421 if (rv != KERN_SUCCESS) {
3422 vm_map_wire_entry_failure(map, entry, faddr);
3424 vm_map_wire_user_count_sub(npages);
3428 } else if (!user_wire ||
3429 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
3430 entry->wired_count++;
3433 * Check the map for holes in the specified region.
3434 * If holes_ok was specified, skip this check.
3436 next_entry = vm_map_entry_succ(entry);
3438 entry->end < end && next_entry->start > entry->end) {
3440 rv = KERN_INVALID_ADDRESS;
3446 need_wakeup = false;
3447 if (first_entry == NULL &&
3448 !vm_map_lookup_entry(map, start, &first_entry)) {
3449 KASSERT(holes_ok, ("vm_map_wire: lookup failed"));
3450 prev_entry = first_entry;
3451 entry = vm_map_entry_succ(first_entry);
3453 prev_entry = vm_map_entry_pred(first_entry);
3454 entry = first_entry;
3456 for (; entry->start < end;
3457 prev_entry = entry, entry = vm_map_entry_succ(entry)) {
3459 * If holes_ok was specified, an empty
3460 * space in the unwired region could have been mapped
3461 * while the map lock was dropped for faulting in the
3462 * pages or draining MAP_ENTRY_IN_TRANSITION.
3463 * Moreover, another thread could be simultaneously
3464 * wiring this new mapping entry. Detect these cases
3465 * and skip any entries marked as in transition not by us.
3467 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 ||
3468 entry->wiring_thread != curthread) {
3470 ("vm_map_wire: !HOLESOK and new/changed entry"));
3474 if ((entry->eflags & MAP_ENTRY_WIRE_SKIPPED) != 0) {
3476 } else if (rv == KERN_SUCCESS) {
3478 entry->eflags |= MAP_ENTRY_USER_WIRED;
3479 } else if (entry->wired_count == -1) {
3481 * Wiring failed on this entry. Thus, unwiring is
3484 entry->wired_count = 0;
3485 } else if (!user_wire ||
3486 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
3488 * Undo the wiring. Wiring succeeded on this entry
3489 * but failed on a later entry.
3491 if (entry->wired_count == 1) {
3492 vm_map_entry_unwire(map, entry);
3494 vm_map_wire_user_count_sub(
3495 atop(entry->end - entry->start));
3497 entry->wired_count--;
3499 KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0,
3500 ("vm_map_wire: in-transition flag missing %p", entry));
3501 KASSERT(entry->wiring_thread == curthread,
3502 ("vm_map_wire: alien wire %p", entry));
3503 entry->eflags &= ~(MAP_ENTRY_IN_TRANSITION |
3504 MAP_ENTRY_WIRE_SKIPPED);
3505 entry->wiring_thread = NULL;
3506 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
3507 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
3510 vm_map_try_merge_entries(map, prev_entry, entry);
3512 vm_map_try_merge_entries(map, prev_entry, entry);
3521 * Push any dirty cached pages in the address range to their pager.
3522 * If syncio is TRUE, dirty pages are written synchronously.
3523 * If invalidate is TRUE, any cached pages are freed as well.
3525 * If the size of the region from start to end is zero, we are
3526 * supposed to flush all modified pages within the region containing
3527 * start. Unfortunately, a region can be split or coalesced with
3528 * neighboring regions, making it difficult to determine what the
3529 * original region was. Therefore, we approximate this requirement by
3530 * flushing the current region containing start.
3532 * Returns an error if any part of the specified range is not mapped.
3540 boolean_t invalidate)
3542 vm_map_entry_t entry, first_entry, next_entry;
3545 vm_ooffset_t offset;
3546 unsigned int last_timestamp;
3549 vm_map_lock_read(map);
3550 VM_MAP_RANGE_CHECK(map, start, end);
3551 if (!vm_map_lookup_entry(map, start, &first_entry)) {
3552 vm_map_unlock_read(map);
3553 return (KERN_INVALID_ADDRESS);
3554 } else if (start == end) {
3555 start = first_entry->start;
3556 end = first_entry->end;
3559 * Make a first pass to check for user-wired memory and holes.
3561 for (entry = first_entry; entry->start < end; entry = next_entry) {
3563 (entry->eflags & MAP_ENTRY_USER_WIRED) != 0) {
3564 vm_map_unlock_read(map);
3565 return (KERN_INVALID_ARGUMENT);
3567 next_entry = vm_map_entry_succ(entry);
3568 if (end > entry->end &&
3569 entry->end != next_entry->start) {
3570 vm_map_unlock_read(map);
3571 return (KERN_INVALID_ADDRESS);
3576 pmap_remove(map->pmap, start, end);
3580 * Make a second pass, cleaning/uncaching pages from the indicated
3583 for (entry = first_entry; entry->start < end;) {
3584 offset = entry->offset + (start - entry->start);
3585 size = (end <= entry->end ? end : entry->end) - start;
3586 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0) {
3588 vm_map_entry_t tentry;
3591 smap = entry->object.sub_map;
3592 vm_map_lock_read(smap);
3593 (void) vm_map_lookup_entry(smap, offset, &tentry);
3594 tsize = tentry->end - offset;
3597 object = tentry->object.vm_object;
3598 offset = tentry->offset + (offset - tentry->start);
3599 vm_map_unlock_read(smap);
3601 object = entry->object.vm_object;
3603 vm_object_reference(object);
3604 last_timestamp = map->timestamp;
3605 vm_map_unlock_read(map);
3606 if (!vm_object_sync(object, offset, size, syncio, invalidate))
3609 vm_object_deallocate(object);
3610 vm_map_lock_read(map);
3611 if (last_timestamp == map->timestamp ||
3612 !vm_map_lookup_entry(map, start, &entry))
3613 entry = vm_map_entry_succ(entry);
3616 vm_map_unlock_read(map);
3617 return (failed ? KERN_FAILURE : KERN_SUCCESS);
3621 * vm_map_entry_unwire: [ internal use only ]
3623 * Make the region specified by this entry pageable.
3625 * The map in question should be locked.
3626 * [This is the reason for this routine's existence.]
3629 vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
3633 VM_MAP_ASSERT_LOCKED(map);
3634 KASSERT(entry->wired_count > 0,
3635 ("vm_map_entry_unwire: entry %p isn't wired", entry));
3637 size = entry->end - entry->start;
3638 if ((entry->eflags & MAP_ENTRY_USER_WIRED) != 0)
3639 vm_map_wire_user_count_sub(atop(size));
3640 pmap_unwire(map->pmap, entry->start, entry->end);
3641 vm_object_unwire(entry->object.vm_object, entry->offset, size,
3643 entry->wired_count = 0;
3647 vm_map_entry_deallocate(vm_map_entry_t entry, boolean_t system_map)
3650 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0)
3651 vm_object_deallocate(entry->object.vm_object);
3652 uma_zfree(system_map ? kmapentzone : mapentzone, entry);
3656 * vm_map_entry_delete: [ internal use only ]
3658 * Deallocate the given entry from the target map.
3661 vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry)
3664 vm_pindex_t offidxstart, offidxend, count, size1;
3667 vm_map_entry_unlink(map, entry, UNLINK_MERGE_NONE);
3668 object = entry->object.vm_object;
3670 if ((entry->eflags & MAP_ENTRY_GUARD) != 0) {
3671 MPASS(entry->cred == NULL);
3672 MPASS((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0);
3673 MPASS(object == NULL);
3674 vm_map_entry_deallocate(entry, map->system_map);
3678 size = entry->end - entry->start;
3681 if (entry->cred != NULL) {
3682 swap_release_by_cred(size, entry->cred);
3683 crfree(entry->cred);
3686 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0 || object == NULL) {
3687 entry->object.vm_object = NULL;
3688 } else if ((object->flags & OBJ_ANON) != 0 ||
3689 object == kernel_object) {
3690 KASSERT(entry->cred == NULL || object->cred == NULL ||
3691 (entry->eflags & MAP_ENTRY_NEEDS_COPY),
3692 ("OVERCOMMIT vm_map_entry_delete: both cred %p", entry));
3694 offidxstart = OFF_TO_IDX(entry->offset);
3695 offidxend = offidxstart + count;
3696 VM_OBJECT_WLOCK(object);
3697 if (object->ref_count != 1 &&
3698 ((object->flags & OBJ_ONEMAPPING) != 0 ||
3699 object == kernel_object)) {
3700 vm_object_collapse(object);
3703 * The option OBJPR_NOTMAPPED can be passed here
3704 * because vm_map_delete() already performed
3705 * pmap_remove() on the only mapping to this range
3708 vm_object_page_remove(object, offidxstart, offidxend,
3710 if (object->type == OBJT_SWAP)
3711 swap_pager_freespace(object, offidxstart,
3713 if (offidxend >= object->size &&
3714 offidxstart < object->size) {
3715 size1 = object->size;
3716 object->size = offidxstart;
3717 if (object->cred != NULL) {
3718 size1 -= object->size;
3719 KASSERT(object->charge >= ptoa(size1),
3720 ("object %p charge < 0", object));
3721 swap_release_by_cred(ptoa(size1),
3723 object->charge -= ptoa(size1);
3727 VM_OBJECT_WUNLOCK(object);
3729 if (map->system_map)
3730 vm_map_entry_deallocate(entry, TRUE);
3732 entry->defer_next = curthread->td_map_def_user;
3733 curthread->td_map_def_user = entry;
3738 * vm_map_delete: [ internal use only ]
3740 * Deallocates the given address range from the target
3744 vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end)
3746 vm_map_entry_t entry, next_entry;
3748 VM_MAP_ASSERT_LOCKED(map);
3750 return (KERN_SUCCESS);
3753 * Find the start of the region, and clip it.
3754 * Step through all entries in this region.
3756 for (entry = vm_map_lookup_clip_start(map, start, &entry);
3757 entry->start < end; entry = next_entry) {
3759 * Wait for wiring or unwiring of an entry to complete.
3760 * Also wait for any system wirings to disappear on
3763 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 ||
3764 (vm_map_pmap(map) != kernel_pmap &&
3765 vm_map_entry_system_wired_count(entry) != 0)) {
3766 unsigned int last_timestamp;
3767 vm_offset_t saved_start;
3769 saved_start = entry->start;
3770 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
3771 last_timestamp = map->timestamp;
3772 (void) vm_map_unlock_and_wait(map, 0);
3774 if (last_timestamp + 1 != map->timestamp) {
3776 * Look again for the entry because the map was
3777 * modified while it was unlocked.
3778 * Specifically, the entry may have been
3779 * clipped, merged, or deleted.
3781 next_entry = vm_map_lookup_clip_start(map,
3782 saved_start, &next_entry);
3787 vm_map_clip_end(map, entry, end);
3788 next_entry = vm_map_entry_succ(entry);
3791 * Unwire before removing addresses from the pmap; otherwise,
3792 * unwiring will put the entries back in the pmap.
3794 if (entry->wired_count != 0)
3795 vm_map_entry_unwire(map, entry);
3798 * Remove mappings for the pages, but only if the
3799 * mappings could exist. For instance, it does not
3800 * make sense to call pmap_remove() for guard entries.
3802 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0 ||
3803 entry->object.vm_object != NULL)
3804 pmap_remove(map->pmap, entry->start, entry->end);
3806 if (entry->end == map->anon_loc)
3807 map->anon_loc = entry->start;
3810 * Delete the entry only after removing all pmap
3811 * entries pointing to its pages. (Otherwise, its
3812 * page frames may be reallocated, and any modify bits
3813 * will be set in the wrong object!)
3815 vm_map_entry_delete(map, entry);
3817 return (KERN_SUCCESS);
3823 * Remove the given address range from the target map.
3824 * This is the exported form of vm_map_delete.
3827 vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
3832 VM_MAP_RANGE_CHECK(map, start, end);
3833 result = vm_map_delete(map, start, end);
3839 * vm_map_check_protection:
3841 * Assert that the target map allows the specified privilege on the
3842 * entire address region given. The entire region must be allocated.
3844 * WARNING! This code does not and should not check whether the
3845 * contents of the region is accessible. For example a smaller file
3846 * might be mapped into a larger address space.
3848 * NOTE! This code is also called by munmap().
3850 * The map must be locked. A read lock is sufficient.
3853 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
3854 vm_prot_t protection)
3856 vm_map_entry_t entry;
3857 vm_map_entry_t tmp_entry;
3859 if (!vm_map_lookup_entry(map, start, &tmp_entry))
3863 while (start < end) {
3867 if (start < entry->start)
3870 * Check protection associated with entry.
3872 if ((entry->protection & protection) != protection)
3874 /* go to next entry */
3876 entry = vm_map_entry_succ(entry);
3884 * vm_map_copy_swap_object:
3886 * Copies a swap-backed object from an existing map entry to a
3887 * new one. Carries forward the swap charge. May change the
3888 * src object on return.
3891 vm_map_copy_swap_object(vm_map_entry_t src_entry, vm_map_entry_t dst_entry,
3892 vm_offset_t size, vm_ooffset_t *fork_charge)
3894 vm_object_t src_object;
3898 src_object = src_entry->object.vm_object;
3899 charged = ENTRY_CHARGED(src_entry);
3900 if ((src_object->flags & OBJ_ANON) != 0) {
3901 VM_OBJECT_WLOCK(src_object);
3902 vm_object_collapse(src_object);
3903 if ((src_object->flags & OBJ_ONEMAPPING) != 0) {
3904 vm_object_split(src_entry);
3905 src_object = src_entry->object.vm_object;
3907 vm_object_reference_locked(src_object);
3908 vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
3909 VM_OBJECT_WUNLOCK(src_object);
3911 vm_object_reference(src_object);
3912 if (src_entry->cred != NULL &&
3913 !(src_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
3914 KASSERT(src_object->cred == NULL,
3915 ("OVERCOMMIT: vm_map_copy_anon_entry: cred %p",
3917 src_object->cred = src_entry->cred;
3918 src_object->charge = size;
3920 dst_entry->object.vm_object = src_object;
3922 cred = curthread->td_ucred;
3924 dst_entry->cred = cred;
3925 *fork_charge += size;
3926 if (!(src_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
3928 src_entry->cred = cred;
3929 *fork_charge += size;
3935 * vm_map_copy_entry:
3937 * Copies the contents of the source entry to the destination
3938 * entry. The entries *must* be aligned properly.
3944 vm_map_entry_t src_entry,
3945 vm_map_entry_t dst_entry,
3946 vm_ooffset_t *fork_charge)
3948 vm_object_t src_object;
3949 vm_map_entry_t fake_entry;
3952 VM_MAP_ASSERT_LOCKED(dst_map);
3954 if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
3957 if (src_entry->wired_count == 0 ||
3958 (src_entry->protection & VM_PROT_WRITE) == 0) {
3960 * If the source entry is marked needs_copy, it is already
3963 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0 &&
3964 (src_entry->protection & VM_PROT_WRITE) != 0) {
3965 pmap_protect(src_map->pmap,
3968 src_entry->protection & ~VM_PROT_WRITE);
3972 * Make a copy of the object.
3974 size = src_entry->end - src_entry->start;
3975 if ((src_object = src_entry->object.vm_object) != NULL) {
3976 if (src_object->type == OBJT_DEFAULT ||
3977 src_object->type == OBJT_SWAP) {
3978 vm_map_copy_swap_object(src_entry, dst_entry,
3980 /* May have split/collapsed, reload obj. */
3981 src_object = src_entry->object.vm_object;
3983 vm_object_reference(src_object);
3984 dst_entry->object.vm_object = src_object;
3986 src_entry->eflags |= MAP_ENTRY_COW |
3987 MAP_ENTRY_NEEDS_COPY;
3988 dst_entry->eflags |= MAP_ENTRY_COW |
3989 MAP_ENTRY_NEEDS_COPY;
3990 dst_entry->offset = src_entry->offset;
3991 if (src_entry->eflags & MAP_ENTRY_WRITECNT) {
3993 * MAP_ENTRY_WRITECNT cannot
3994 * indicate write reference from
3995 * src_entry, since the entry is
3996 * marked as needs copy. Allocate a
3997 * fake entry that is used to
3998 * decrement object->un_pager writecount
3999 * at the appropriate time. Attach
4000 * fake_entry to the deferred list.
4002 fake_entry = vm_map_entry_create(dst_map);
4003 fake_entry->eflags = MAP_ENTRY_WRITECNT;
4004 src_entry->eflags &= ~MAP_ENTRY_WRITECNT;
4005 vm_object_reference(src_object);
4006 fake_entry->object.vm_object = src_object;
4007 fake_entry->start = src_entry->start;
4008 fake_entry->end = src_entry->end;
4009 fake_entry->defer_next =
4010 curthread->td_map_def_user;
4011 curthread->td_map_def_user = fake_entry;
4014 pmap_copy(dst_map->pmap, src_map->pmap,
4015 dst_entry->start, dst_entry->end - dst_entry->start,
4018 dst_entry->object.vm_object = NULL;
4019 dst_entry->offset = 0;
4020 if (src_entry->cred != NULL) {
4021 dst_entry->cred = curthread->td_ucred;
4022 crhold(dst_entry->cred);
4023 *fork_charge += size;
4028 * We don't want to make writeable wired pages copy-on-write.
4029 * Immediately copy these pages into the new map by simulating
4030 * page faults. The new pages are pageable.
4032 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry,
4038 * vmspace_map_entry_forked:
4039 * Update the newly-forked vmspace each time a map entry is inherited
4040 * or copied. The values for vm_dsize and vm_tsize are approximate
4041 * (and mostly-obsolete ideas in the face of mmap(2) et al.)
4044 vmspace_map_entry_forked(const struct vmspace *vm1, struct vmspace *vm2,
4045 vm_map_entry_t entry)
4047 vm_size_t entrysize;
4050 if ((entry->eflags & MAP_ENTRY_GUARD) != 0)
4052 entrysize = entry->end - entry->start;
4053 vm2->vm_map.size += entrysize;
4054 if (entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP)) {
4055 vm2->vm_ssize += btoc(entrysize);
4056 } else if (entry->start >= (vm_offset_t)vm1->vm_daddr &&
4057 entry->start < (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)) {
4058 newend = MIN(entry->end,
4059 (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize));
4060 vm2->vm_dsize += btoc(newend - entry->start);
4061 } else if (entry->start >= (vm_offset_t)vm1->vm_taddr &&
4062 entry->start < (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)) {
4063 newend = MIN(entry->end,
4064 (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize));
4065 vm2->vm_tsize += btoc(newend - entry->start);
4071 * Create a new process vmspace structure and vm_map
4072 * based on those of an existing process. The new map
4073 * is based on the old map, according to the inheritance
4074 * values on the regions in that map.
4076 * XXX It might be worth coalescing the entries added to the new vmspace.
4078 * The source map must not be locked.
4081 vmspace_fork(struct vmspace *vm1, vm_ooffset_t *fork_charge)
4083 struct vmspace *vm2;
4084 vm_map_t new_map, old_map;
4085 vm_map_entry_t new_entry, old_entry;
4090 old_map = &vm1->vm_map;
4091 /* Copy immutable fields of vm1 to vm2. */
4092 vm2 = vmspace_alloc(vm_map_min(old_map), vm_map_max(old_map),
4097 vm2->vm_taddr = vm1->vm_taddr;
4098 vm2->vm_daddr = vm1->vm_daddr;
4099 vm2->vm_maxsaddr = vm1->vm_maxsaddr;
4100 vm_map_lock(old_map);
4102 vm_map_wait_busy(old_map);
4103 new_map = &vm2->vm_map;
4104 locked = vm_map_trylock(new_map); /* trylock to silence WITNESS */
4105 KASSERT(locked, ("vmspace_fork: lock failed"));
4107 error = pmap_vmspace_copy(new_map->pmap, old_map->pmap);
4109 sx_xunlock(&old_map->lock);
4110 sx_xunlock(&new_map->lock);
4111 vm_map_process_deferred();
4116 new_map->anon_loc = old_map->anon_loc;
4118 VM_MAP_ENTRY_FOREACH(old_entry, old_map) {
4119 if ((old_entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0)
4120 panic("vm_map_fork: encountered a submap");
4122 inh = old_entry->inheritance;
4123 if ((old_entry->eflags & MAP_ENTRY_GUARD) != 0 &&
4124 inh != VM_INHERIT_NONE)
4125 inh = VM_INHERIT_COPY;
4128 case VM_INHERIT_NONE:
4131 case VM_INHERIT_SHARE:
4133 * Clone the entry, creating the shared object if
4136 object = old_entry->object.vm_object;
4137 if (object == NULL) {
4138 vm_map_entry_back(old_entry);
4139 object = old_entry->object.vm_object;
4143 * Add the reference before calling vm_object_shadow
4144 * to insure that a shadow object is created.
4146 vm_object_reference(object);
4147 if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
4148 vm_object_shadow(&old_entry->object.vm_object,
4150 old_entry->end - old_entry->start,
4152 /* Transfer the second reference too. */
4154 old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
4155 old_entry->cred = NULL;
4158 * As in vm_map_merged_neighbor_dispose(),
4159 * the vnode lock will not be acquired in
4160 * this call to vm_object_deallocate().
4162 vm_object_deallocate(object);
4163 object = old_entry->object.vm_object;
4165 VM_OBJECT_WLOCK(object);
4166 vm_object_clear_flag(object, OBJ_ONEMAPPING);
4167 if (old_entry->cred != NULL) {
4168 KASSERT(object->cred == NULL,
4169 ("vmspace_fork both cred"));
4170 object->cred = old_entry->cred;
4171 object->charge = old_entry->end -
4173 old_entry->cred = NULL;
4177 * Assert the correct state of the vnode
4178 * v_writecount while the object is locked, to
4179 * not relock it later for the assertion
4182 if (old_entry->eflags & MAP_ENTRY_WRITECNT &&
4183 object->type == OBJT_VNODE) {
4184 KASSERT(((struct vnode *)object->
4185 handle)->v_writecount > 0,
4186 ("vmspace_fork: v_writecount %p",
4188 KASSERT(object->un_pager.vnp.
4190 ("vmspace_fork: vnp.writecount %p",
4193 VM_OBJECT_WUNLOCK(object);
4197 * Clone the entry, referencing the shared object.
4199 new_entry = vm_map_entry_create(new_map);
4200 *new_entry = *old_entry;
4201 new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
4202 MAP_ENTRY_IN_TRANSITION);
4203 new_entry->wiring_thread = NULL;
4204 new_entry->wired_count = 0;
4205 if (new_entry->eflags & MAP_ENTRY_WRITECNT) {
4206 vm_pager_update_writecount(object,
4207 new_entry->start, new_entry->end);
4209 vm_map_entry_set_vnode_text(new_entry, true);
4212 * Insert the entry into the new map -- we know we're
4213 * inserting at the end of the new map.
4215 vm_map_entry_link(new_map, new_entry);
4216 vmspace_map_entry_forked(vm1, vm2, new_entry);
4219 * Update the physical map
4221 pmap_copy(new_map->pmap, old_map->pmap,
4223 (old_entry->end - old_entry->start),
4227 case VM_INHERIT_COPY:
4229 * Clone the entry and link into the map.
4231 new_entry = vm_map_entry_create(new_map);
4232 *new_entry = *old_entry;
4234 * Copied entry is COW over the old object.
4236 new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
4237 MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_WRITECNT);
4238 new_entry->wiring_thread = NULL;
4239 new_entry->wired_count = 0;
4240 new_entry->object.vm_object = NULL;
4241 new_entry->cred = NULL;
4242 vm_map_entry_link(new_map, new_entry);
4243 vmspace_map_entry_forked(vm1, vm2, new_entry);
4244 vm_map_copy_entry(old_map, new_map, old_entry,
4245 new_entry, fork_charge);
4246 vm_map_entry_set_vnode_text(new_entry, true);
4249 case VM_INHERIT_ZERO:
4251 * Create a new anonymous mapping entry modelled from
4254 new_entry = vm_map_entry_create(new_map);
4255 memset(new_entry, 0, sizeof(*new_entry));
4257 new_entry->start = old_entry->start;
4258 new_entry->end = old_entry->end;
4259 new_entry->eflags = old_entry->eflags &
4260 ~(MAP_ENTRY_USER_WIRED | MAP_ENTRY_IN_TRANSITION |
4261 MAP_ENTRY_WRITECNT | MAP_ENTRY_VN_EXEC);
4262 new_entry->protection = old_entry->protection;
4263 new_entry->max_protection = old_entry->max_protection;
4264 new_entry->inheritance = VM_INHERIT_ZERO;
4266 vm_map_entry_link(new_map, new_entry);
4267 vmspace_map_entry_forked(vm1, vm2, new_entry);
4269 new_entry->cred = curthread->td_ucred;
4270 crhold(new_entry->cred);
4271 *fork_charge += (new_entry->end - new_entry->start);
4277 * Use inlined vm_map_unlock() to postpone handling the deferred
4278 * map entries, which cannot be done until both old_map and
4279 * new_map locks are released.
4281 sx_xunlock(&old_map->lock);
4282 sx_xunlock(&new_map->lock);
4283 vm_map_process_deferred();
4289 * Create a process's stack for exec_new_vmspace(). This function is never
4290 * asked to wire the newly created stack.
4293 vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
4294 vm_prot_t prot, vm_prot_t max, int cow)
4296 vm_size_t growsize, init_ssize;
4300 MPASS((map->flags & MAP_WIREFUTURE) == 0);
4301 growsize = sgrowsiz;
4302 init_ssize = (max_ssize < growsize) ? max_ssize : growsize;
4304 vmemlim = lim_cur(curthread, RLIMIT_VMEM);
4305 /* If we would blow our VMEM resource limit, no go */
4306 if (map->size + init_ssize > vmemlim) {
4310 rv = vm_map_stack_locked(map, addrbos, max_ssize, growsize, prot,
4317 static int stack_guard_page = 1;
4318 SYSCTL_INT(_security_bsd, OID_AUTO, stack_guard_page, CTLFLAG_RWTUN,
4319 &stack_guard_page, 0,
4320 "Specifies the number of guard pages for a stack that grows");
4323 vm_map_stack_locked(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
4324 vm_size_t growsize, vm_prot_t prot, vm_prot_t max, int cow)
4326 vm_map_entry_t new_entry, prev_entry;
4327 vm_offset_t bot, gap_bot, gap_top, top;
4328 vm_size_t init_ssize, sgp;
4332 * The stack orientation is piggybacked with the cow argument.
4333 * Extract it into orient and mask the cow argument so that we
4334 * don't pass it around further.
4336 orient = cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP);
4337 KASSERT(orient != 0, ("No stack grow direction"));
4338 KASSERT(orient != (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP),
4341 if (addrbos < vm_map_min(map) ||
4342 addrbos + max_ssize > vm_map_max(map) ||
4343 addrbos + max_ssize <= addrbos)
4344 return (KERN_INVALID_ADDRESS);
4345 sgp = ((curproc->p_flag2 & P2_STKGAP_DISABLE) != 0 ||
4346 (curproc->p_fctl0 & NT_FREEBSD_FCTL_STKGAP_DISABLE) != 0) ? 0 :
4347 (vm_size_t)stack_guard_page * PAGE_SIZE;
4348 if (sgp >= max_ssize)
4349 return (KERN_INVALID_ARGUMENT);
4351 init_ssize = growsize;
4352 if (max_ssize < init_ssize + sgp)
4353 init_ssize = max_ssize - sgp;
4355 /* If addr is already mapped, no go */
4356 if (vm_map_lookup_entry(map, addrbos, &prev_entry))
4357 return (KERN_NO_SPACE);
4360 * If we can't accommodate max_ssize in the current mapping, no go.
4362 if (vm_map_entry_succ(prev_entry)->start < addrbos + max_ssize)
4363 return (KERN_NO_SPACE);
4366 * We initially map a stack of only init_ssize. We will grow as
4367 * needed later. Depending on the orientation of the stack (i.e.
4368 * the grow direction) we either map at the top of the range, the
4369 * bottom of the range or in the middle.
4371 * Note: we would normally expect prot and max to be VM_PROT_ALL,
4372 * and cow to be 0. Possibly we should eliminate these as input
4373 * parameters, and just pass these values here in the insert call.
4375 if (orient == MAP_STACK_GROWS_DOWN) {
4376 bot = addrbos + max_ssize - init_ssize;
4377 top = bot + init_ssize;
4380 } else /* if (orient == MAP_STACK_GROWS_UP) */ {
4382 top = bot + init_ssize;
4384 gap_top = addrbos + max_ssize;
4386 rv = vm_map_insert(map, NULL, 0, bot, top, prot, max, cow);
4387 if (rv != KERN_SUCCESS)
4389 new_entry = vm_map_entry_succ(prev_entry);
4390 KASSERT(new_entry->end == top || new_entry->start == bot,
4391 ("Bad entry start/end for new stack entry"));
4392 KASSERT((orient & MAP_STACK_GROWS_DOWN) == 0 ||
4393 (new_entry->eflags & MAP_ENTRY_GROWS_DOWN) != 0,
4394 ("new entry lacks MAP_ENTRY_GROWS_DOWN"));
4395 KASSERT((orient & MAP_STACK_GROWS_UP) == 0 ||
4396 (new_entry->eflags & MAP_ENTRY_GROWS_UP) != 0,
4397 ("new entry lacks MAP_ENTRY_GROWS_UP"));
4398 if (gap_bot == gap_top)
4399 return (KERN_SUCCESS);
4400 rv = vm_map_insert(map, NULL, 0, gap_bot, gap_top, VM_PROT_NONE,
4401 VM_PROT_NONE, MAP_CREATE_GUARD | (orient == MAP_STACK_GROWS_DOWN ?
4402 MAP_CREATE_STACK_GAP_DN : MAP_CREATE_STACK_GAP_UP));
4403 if (rv == KERN_SUCCESS) {
4405 * Gap can never successfully handle a fault, so
4406 * read-ahead logic is never used for it. Re-use
4407 * next_read of the gap entry to store
4408 * stack_guard_page for vm_map_growstack().
4410 if (orient == MAP_STACK_GROWS_DOWN)
4411 vm_map_entry_pred(new_entry)->next_read = sgp;
4413 vm_map_entry_succ(new_entry)->next_read = sgp;
4415 (void)vm_map_delete(map, bot, top);
4421 * Attempts to grow a vm stack entry. Returns KERN_SUCCESS if we
4422 * successfully grow the stack.
4425 vm_map_growstack(vm_map_t map, vm_offset_t addr, vm_map_entry_t gap_entry)
4427 vm_map_entry_t stack_entry;
4431 vm_offset_t gap_end, gap_start, grow_start;
4432 vm_size_t grow_amount, guard, max_grow;
4433 rlim_t lmemlim, stacklim, vmemlim;
4435 bool gap_deleted, grow_down, is_procstack;
4447 * Disallow stack growth when the access is performed by a
4448 * debugger or AIO daemon. The reason is that the wrong
4449 * resource limits are applied.
4451 if (p != initproc && (map != &p->p_vmspace->vm_map ||
4452 p->p_textvp == NULL))
4453 return (KERN_FAILURE);
4455 MPASS(!map->system_map);
4457 lmemlim = lim_cur(curthread, RLIMIT_MEMLOCK);
4458 stacklim = lim_cur(curthread, RLIMIT_STACK);
4459 vmemlim = lim_cur(curthread, RLIMIT_VMEM);
4461 /* If addr is not in a hole for a stack grow area, no need to grow. */
4462 if (gap_entry == NULL && !vm_map_lookup_entry(map, addr, &gap_entry))
4463 return (KERN_FAILURE);
4464 if ((gap_entry->eflags & MAP_ENTRY_GUARD) == 0)
4465 return (KERN_SUCCESS);
4466 if ((gap_entry->eflags & MAP_ENTRY_STACK_GAP_DN) != 0) {
4467 stack_entry = vm_map_entry_succ(gap_entry);
4468 if ((stack_entry->eflags & MAP_ENTRY_GROWS_DOWN) == 0 ||
4469 stack_entry->start != gap_entry->end)
4470 return (KERN_FAILURE);
4471 grow_amount = round_page(stack_entry->start - addr);
4473 } else if ((gap_entry->eflags & MAP_ENTRY_STACK_GAP_UP) != 0) {
4474 stack_entry = vm_map_entry_pred(gap_entry);
4475 if ((stack_entry->eflags & MAP_ENTRY_GROWS_UP) == 0 ||
4476 stack_entry->end != gap_entry->start)
4477 return (KERN_FAILURE);
4478 grow_amount = round_page(addr + 1 - stack_entry->end);
4481 return (KERN_FAILURE);
4483 guard = ((curproc->p_flag2 & P2_STKGAP_DISABLE) != 0 ||
4484 (curproc->p_fctl0 & NT_FREEBSD_FCTL_STKGAP_DISABLE) != 0) ? 0 :
4485 gap_entry->next_read;
4486 max_grow = gap_entry->end - gap_entry->start;
4487 if (guard > max_grow)
4488 return (KERN_NO_SPACE);
4490 if (grow_amount > max_grow)
4491 return (KERN_NO_SPACE);
4494 * If this is the main process stack, see if we're over the stack
4497 is_procstack = addr >= (vm_offset_t)vm->vm_maxsaddr &&
4498 addr < (vm_offset_t)p->p_sysent->sv_usrstack;
4499 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim))
4500 return (KERN_NO_SPACE);
4505 if (is_procstack && racct_set(p, RACCT_STACK,
4506 ctob(vm->vm_ssize) + grow_amount)) {
4508 return (KERN_NO_SPACE);
4514 grow_amount = roundup(grow_amount, sgrowsiz);
4515 if (grow_amount > max_grow)
4516 grow_amount = max_grow;
4517 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
4518 grow_amount = trunc_page((vm_size_t)stacklim) -
4524 limit = racct_get_available(p, RACCT_STACK);
4526 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > limit))
4527 grow_amount = limit - ctob(vm->vm_ssize);
4530 if (!old_mlock && (map->flags & MAP_WIREFUTURE) != 0) {
4531 if (ptoa(pmap_wired_count(map->pmap)) + grow_amount > lmemlim) {
4538 if (racct_set(p, RACCT_MEMLOCK,
4539 ptoa(pmap_wired_count(map->pmap)) + grow_amount)) {
4549 /* If we would blow our VMEM resource limit, no go */
4550 if (map->size + grow_amount > vmemlim) {
4557 if (racct_set(p, RACCT_VMEM, map->size + grow_amount)) {
4566 if (vm_map_lock_upgrade(map)) {
4568 vm_map_lock_read(map);
4573 grow_start = gap_entry->end - grow_amount;
4574 if (gap_entry->start + grow_amount == gap_entry->end) {
4575 gap_start = gap_entry->start;
4576 gap_end = gap_entry->end;
4577 vm_map_entry_delete(map, gap_entry);
4580 MPASS(gap_entry->start < gap_entry->end - grow_amount);
4581 vm_map_entry_resize(map, gap_entry, -grow_amount);
4582 gap_deleted = false;
4584 rv = vm_map_insert(map, NULL, 0, grow_start,
4585 grow_start + grow_amount,
4586 stack_entry->protection, stack_entry->max_protection,
4587 MAP_STACK_GROWS_DOWN);
4588 if (rv != KERN_SUCCESS) {
4590 rv1 = vm_map_insert(map, NULL, 0, gap_start,
4591 gap_end, VM_PROT_NONE, VM_PROT_NONE,
4592 MAP_CREATE_GUARD | MAP_CREATE_STACK_GAP_DN);
4593 MPASS(rv1 == KERN_SUCCESS);
4595 vm_map_entry_resize(map, gap_entry,
4599 grow_start = stack_entry->end;
4600 cred = stack_entry->cred;
4601 if (cred == NULL && stack_entry->object.vm_object != NULL)
4602 cred = stack_entry->object.vm_object->cred;
4603 if (cred != NULL && !swap_reserve_by_cred(grow_amount, cred))
4605 /* Grow the underlying object if applicable. */
4606 else if (stack_entry->object.vm_object == NULL ||
4607 vm_object_coalesce(stack_entry->object.vm_object,
4608 stack_entry->offset,
4609 (vm_size_t)(stack_entry->end - stack_entry->start),
4610 grow_amount, cred != NULL)) {
4611 if (gap_entry->start + grow_amount == gap_entry->end) {
4612 vm_map_entry_delete(map, gap_entry);
4613 vm_map_entry_resize(map, stack_entry,
4616 gap_entry->start += grow_amount;
4617 stack_entry->end += grow_amount;
4619 map->size += grow_amount;
4624 if (rv == KERN_SUCCESS && is_procstack)
4625 vm->vm_ssize += btoc(grow_amount);
4628 * Heed the MAP_WIREFUTURE flag if it was set for this process.
4630 if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE) != 0) {
4631 rv = vm_map_wire_locked(map, grow_start,
4632 grow_start + grow_amount,
4633 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
4635 vm_map_lock_downgrade(map);
4639 if (racct_enable && rv != KERN_SUCCESS) {
4641 error = racct_set(p, RACCT_VMEM, map->size);
4642 KASSERT(error == 0, ("decreasing RACCT_VMEM failed"));
4644 error = racct_set(p, RACCT_MEMLOCK,
4645 ptoa(pmap_wired_count(map->pmap)));
4646 KASSERT(error == 0, ("decreasing RACCT_MEMLOCK failed"));
4648 error = racct_set(p, RACCT_STACK, ctob(vm->vm_ssize));
4649 KASSERT(error == 0, ("decreasing RACCT_STACK failed"));
4658 * Unshare the specified VM space for exec. If other processes are
4659 * mapped to it, then create a new one. The new vmspace is null.
4662 vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser)
4664 struct vmspace *oldvmspace = p->p_vmspace;
4665 struct vmspace *newvmspace;
4667 KASSERT((curthread->td_pflags & TDP_EXECVMSPC) == 0,
4668 ("vmspace_exec recursed"));
4669 newvmspace = vmspace_alloc(minuser, maxuser, pmap_pinit);
4670 if (newvmspace == NULL)
4672 newvmspace->vm_swrss = oldvmspace->vm_swrss;
4674 * This code is written like this for prototype purposes. The
4675 * goal is to avoid running down the vmspace here, but let the
4676 * other process's that are still using the vmspace to finally
4677 * run it down. Even though there is little or no chance of blocking
4678 * here, it is a good idea to keep this form for future mods.
4680 PROC_VMSPACE_LOCK(p);
4681 p->p_vmspace = newvmspace;
4682 PROC_VMSPACE_UNLOCK(p);
4683 if (p == curthread->td_proc)
4684 pmap_activate(curthread);
4685 curthread->td_pflags |= TDP_EXECVMSPC;
4690 * Unshare the specified VM space for forcing COW. This
4691 * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
4694 vmspace_unshare(struct proc *p)
4696 struct vmspace *oldvmspace = p->p_vmspace;
4697 struct vmspace *newvmspace;
4698 vm_ooffset_t fork_charge;
4700 if (oldvmspace->vm_refcnt == 1)
4703 newvmspace = vmspace_fork(oldvmspace, &fork_charge);
4704 if (newvmspace == NULL)
4706 if (!swap_reserve_by_cred(fork_charge, p->p_ucred)) {
4707 vmspace_free(newvmspace);
4710 PROC_VMSPACE_LOCK(p);
4711 p->p_vmspace = newvmspace;
4712 PROC_VMSPACE_UNLOCK(p);
4713 if (p == curthread->td_proc)
4714 pmap_activate(curthread);
4715 vmspace_free(oldvmspace);
4722 * Finds the VM object, offset, and
4723 * protection for a given virtual address in the
4724 * specified map, assuming a page fault of the
4727 * Leaves the map in question locked for read; return
4728 * values are guaranteed until a vm_map_lookup_done
4729 * call is performed. Note that the map argument
4730 * is in/out; the returned map must be used in
4731 * the call to vm_map_lookup_done.
4733 * A handle (out_entry) is returned for use in
4734 * vm_map_lookup_done, to make that fast.
4736 * If a lookup is requested with "write protection"
4737 * specified, the map may be changed to perform virtual
4738 * copying operations, although the data referenced will
4742 vm_map_lookup(vm_map_t *var_map, /* IN/OUT */
4744 vm_prot_t fault_typea,
4745 vm_map_entry_t *out_entry, /* OUT */
4746 vm_object_t *object, /* OUT */
4747 vm_pindex_t *pindex, /* OUT */
4748 vm_prot_t *out_prot, /* OUT */
4749 boolean_t *wired) /* OUT */
4751 vm_map_entry_t entry;
4752 vm_map_t map = *var_map;
4754 vm_prot_t fault_type;
4755 vm_object_t eobject;
4761 vm_map_lock_read(map);
4765 * Lookup the faulting address.
4767 if (!vm_map_lookup_entry(map, vaddr, out_entry)) {
4768 vm_map_unlock_read(map);
4769 return (KERN_INVALID_ADDRESS);
4777 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
4778 vm_map_t old_map = map;
4780 *var_map = map = entry->object.sub_map;
4781 vm_map_unlock_read(old_map);
4786 * Check whether this task is allowed to have this page.
4788 prot = entry->protection;
4789 if ((fault_typea & VM_PROT_FAULT_LOOKUP) != 0) {
4790 fault_typea &= ~VM_PROT_FAULT_LOOKUP;
4791 if (prot == VM_PROT_NONE && map != kernel_map &&
4792 (entry->eflags & MAP_ENTRY_GUARD) != 0 &&
4793 (entry->eflags & (MAP_ENTRY_STACK_GAP_DN |
4794 MAP_ENTRY_STACK_GAP_UP)) != 0 &&
4795 vm_map_growstack(map, vaddr, entry) == KERN_SUCCESS)
4796 goto RetryLookupLocked;
4798 fault_type = fault_typea & VM_PROT_ALL;
4799 if ((fault_type & prot) != fault_type || prot == VM_PROT_NONE) {
4800 vm_map_unlock_read(map);
4801 return (KERN_PROTECTION_FAILURE);
4803 KASSERT((prot & VM_PROT_WRITE) == 0 || (entry->eflags &
4804 (MAP_ENTRY_USER_WIRED | MAP_ENTRY_NEEDS_COPY)) !=
4805 (MAP_ENTRY_USER_WIRED | MAP_ENTRY_NEEDS_COPY),
4806 ("entry %p flags %x", entry, entry->eflags));
4807 if ((fault_typea & VM_PROT_COPY) != 0 &&
4808 (entry->max_protection & VM_PROT_WRITE) == 0 &&
4809 (entry->eflags & MAP_ENTRY_COW) == 0) {
4810 vm_map_unlock_read(map);
4811 return (KERN_PROTECTION_FAILURE);
4815 * If this page is not pageable, we have to get it for all possible
4818 *wired = (entry->wired_count != 0);
4820 fault_type = entry->protection;
4821 size = entry->end - entry->start;
4824 * If the entry was copy-on-write, we either ...
4826 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
4828 * If we want to write the page, we may as well handle that
4829 * now since we've got the map locked.
4831 * If we don't need to write the page, we just demote the
4832 * permissions allowed.
4834 if ((fault_type & VM_PROT_WRITE) != 0 ||
4835 (fault_typea & VM_PROT_COPY) != 0) {
4837 * Make a new object, and place it in the object
4838 * chain. Note that no new references have appeared
4839 * -- one just moved from the map to the new
4842 if (vm_map_lock_upgrade(map))
4845 if (entry->cred == NULL) {
4847 * The debugger owner is charged for
4850 cred = curthread->td_ucred;
4852 if (!swap_reserve_by_cred(size, cred)) {
4855 return (KERN_RESOURCE_SHORTAGE);
4859 eobject = entry->object.vm_object;
4860 vm_object_shadow(&entry->object.vm_object,
4861 &entry->offset, size, entry->cred, false);
4862 if (eobject == entry->object.vm_object) {
4864 * The object was not shadowed.
4866 swap_release_by_cred(size, entry->cred);
4867 crfree(entry->cred);
4870 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
4872 vm_map_lock_downgrade(map);
4875 * We're attempting to read a copy-on-write page --
4876 * don't allow writes.
4878 prot &= ~VM_PROT_WRITE;
4883 * Create an object if necessary.
4885 if (entry->object.vm_object == NULL && !map->system_map) {
4886 if (vm_map_lock_upgrade(map))
4888 entry->object.vm_object = vm_object_allocate_anon(atop(size),
4889 NULL, entry->cred, entry->cred != NULL ? size : 0);
4892 vm_map_lock_downgrade(map);
4896 * Return the object/offset from this entry. If the entry was
4897 * copy-on-write or empty, it has been fixed up.
4899 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
4900 *object = entry->object.vm_object;
4903 return (KERN_SUCCESS);
4907 * vm_map_lookup_locked:
4909 * Lookup the faulting address. A version of vm_map_lookup that returns
4910 * KERN_FAILURE instead of blocking on map lock or memory allocation.
4913 vm_map_lookup_locked(vm_map_t *var_map, /* IN/OUT */
4915 vm_prot_t fault_typea,
4916 vm_map_entry_t *out_entry, /* OUT */
4917 vm_object_t *object, /* OUT */
4918 vm_pindex_t *pindex, /* OUT */
4919 vm_prot_t *out_prot, /* OUT */
4920 boolean_t *wired) /* OUT */
4922 vm_map_entry_t entry;
4923 vm_map_t map = *var_map;
4925 vm_prot_t fault_type = fault_typea;
4928 * Lookup the faulting address.
4930 if (!vm_map_lookup_entry(map, vaddr, out_entry))
4931 return (KERN_INVALID_ADDRESS);
4936 * Fail if the entry refers to a submap.
4938 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
4939 return (KERN_FAILURE);
4942 * Check whether this task is allowed to have this page.
4944 prot = entry->protection;
4945 fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
4946 if ((fault_type & prot) != fault_type)
4947 return (KERN_PROTECTION_FAILURE);
4950 * If this page is not pageable, we have to get it for all possible
4953 *wired = (entry->wired_count != 0);
4955 fault_type = entry->protection;
4957 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
4959 * Fail if the entry was copy-on-write for a write fault.
4961 if (fault_type & VM_PROT_WRITE)
4962 return (KERN_FAILURE);
4964 * We're attempting to read a copy-on-write page --
4965 * don't allow writes.
4967 prot &= ~VM_PROT_WRITE;
4971 * Fail if an object should be created.
4973 if (entry->object.vm_object == NULL && !map->system_map)
4974 return (KERN_FAILURE);
4977 * Return the object/offset from this entry. If the entry was
4978 * copy-on-write or empty, it has been fixed up.
4980 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
4981 *object = entry->object.vm_object;
4984 return (KERN_SUCCESS);
4988 * vm_map_lookup_done:
4990 * Releases locks acquired by a vm_map_lookup
4991 * (according to the handle returned by that lookup).
4994 vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry)
4997 * Unlock the main-level map
4999 vm_map_unlock_read(map);
5003 vm_map_max_KBI(const struct vm_map *map)
5006 return (vm_map_max(map));
5010 vm_map_min_KBI(const struct vm_map *map)
5013 return (vm_map_min(map));
5017 vm_map_pmap_KBI(vm_map_t map)
5025 _vm_map_assert_consistent(vm_map_t map, int check)
5027 vm_map_entry_t entry, prev;
5028 vm_map_entry_t cur, header, lbound, ubound;
5029 vm_size_t max_left, max_right;
5034 if (enable_vmmap_check != check)
5037 header = prev = &map->header;
5038 VM_MAP_ENTRY_FOREACH(entry, map) {
5039 KASSERT(prev->end <= entry->start,
5040 ("map %p prev->end = %jx, start = %jx", map,
5041 (uintmax_t)prev->end, (uintmax_t)entry->start));
5042 KASSERT(entry->start < entry->end,
5043 ("map %p start = %jx, end = %jx", map,
5044 (uintmax_t)entry->start, (uintmax_t)entry->end));
5045 KASSERT(entry->left == header ||
5046 entry->left->start < entry->start,
5047 ("map %p left->start = %jx, start = %jx", map,
5048 (uintmax_t)entry->left->start, (uintmax_t)entry->start));
5049 KASSERT(entry->right == header ||
5050 entry->start < entry->right->start,
5051 ("map %p start = %jx, right->start = %jx", map,
5052 (uintmax_t)entry->start, (uintmax_t)entry->right->start));
5054 lbound = ubound = header;
5056 if (entry->start < cur->start) {
5059 KASSERT(cur != lbound,
5060 ("map %p cannot find %jx",
5061 map, (uintmax_t)entry->start));
5062 } else if (cur->end <= entry->start) {
5065 KASSERT(cur != ubound,
5066 ("map %p cannot find %jx",
5067 map, (uintmax_t)entry->start));
5069 KASSERT(cur == entry,
5070 ("map %p cannot find %jx",
5071 map, (uintmax_t)entry->start));
5075 max_left = vm_map_entry_max_free_left(entry, lbound);
5076 max_right = vm_map_entry_max_free_right(entry, ubound);
5077 KASSERT(entry->max_free == vm_size_max(max_left, max_right),
5078 ("map %p max = %jx, max_left = %jx, max_right = %jx", map,
5079 (uintmax_t)entry->max_free,
5080 (uintmax_t)max_left, (uintmax_t)max_right));
5083 KASSERT(prev->end <= entry->start,
5084 ("map %p prev->end = %jx, start = %jx", map,
5085 (uintmax_t)prev->end, (uintmax_t)entry->start));
5089 #include "opt_ddb.h"
5091 #include <sys/kernel.h>
5093 #include <ddb/ddb.h>
5096 vm_map_print(vm_map_t map)
5098 vm_map_entry_t entry, prev;
5100 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
5102 (void *)map->pmap, map->nentries, map->timestamp);
5105 prev = &map->header;
5106 VM_MAP_ENTRY_FOREACH(entry, map) {
5107 db_iprintf("map entry %p: start=%p, end=%p, eflags=%#x, \n",
5108 (void *)entry, (void *)entry->start, (void *)entry->end,
5111 static const char * const inheritance_name[4] =
5112 {"share", "copy", "none", "donate_copy"};
5114 db_iprintf(" prot=%x/%x/%s",
5116 entry->max_protection,
5117 inheritance_name[(int)(unsigned char)
5118 entry->inheritance]);
5119 if (entry->wired_count != 0)
5120 db_printf(", wired");
5122 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
5123 db_printf(", share=%p, offset=0x%jx\n",
5124 (void *)entry->object.sub_map,
5125 (uintmax_t)entry->offset);
5126 if (prev == &map->header ||
5127 prev->object.sub_map !=
5128 entry->object.sub_map) {
5130 vm_map_print((vm_map_t)entry->object.sub_map);
5134 if (entry->cred != NULL)
5135 db_printf(", ruid %d", entry->cred->cr_ruid);
5136 db_printf(", object=%p, offset=0x%jx",
5137 (void *)entry->object.vm_object,
5138 (uintmax_t)entry->offset);
5139 if (entry->object.vm_object && entry->object.vm_object->cred)
5140 db_printf(", obj ruid %d charge %jx",
5141 entry->object.vm_object->cred->cr_ruid,
5142 (uintmax_t)entry->object.vm_object->charge);
5143 if (entry->eflags & MAP_ENTRY_COW)
5144 db_printf(", copy (%s)",
5145 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
5148 if (prev == &map->header ||
5149 prev->object.vm_object !=
5150 entry->object.vm_object) {
5152 vm_object_print((db_expr_t)(intptr_t)
5153 entry->object.vm_object,
5163 DB_SHOW_COMMAND(map, map)
5167 db_printf("usage: show map <addr>\n");
5170 vm_map_print((vm_map_t)addr);
5173 DB_SHOW_COMMAND(procvm, procvm)
5178 p = db_lookup_proc(addr);
5183 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
5184 (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
5185 (void *)vmspace_pmap(p->p_vmspace));
5187 vm_map_print((vm_map_t)&p->p_vmspace->vm_map);