2 * SPDX-License-Identifier: (BSD-3-Clause AND MIT-CMU)
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * The Mach Operating System project at Carnegie-Mellon University.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * from: @(#)vm_map.c 8.3 (Berkeley) 1/12/94
37 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
38 * All rights reserved.
40 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
42 * Permission to use, copy, modify and distribute this software and
43 * its documentation is hereby granted, provided that both the copyright
44 * notice and this permission notice appear in all copies of the
45 * software, derivative works or modified versions, and any portions
46 * thereof, and that both notices appear in supporting documentation.
48 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
49 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
50 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
52 * Carnegie Mellon requests users of this software to return to
54 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
55 * School of Computer Science
56 * Carnegie Mellon University
57 * Pittsburgh PA 15213-3890
59 * any improvements or extensions that they make and grant Carnegie the
60 * rights to redistribute these changes.
64 * Virtual memory mapping module.
67 #include <sys/cdefs.h>
68 __FBSDID("$FreeBSD$");
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/kernel.h>
75 #include <sys/mutex.h>
77 #include <sys/vmmeter.h>
79 #include <sys/vnode.h>
80 #include <sys/racct.h>
81 #include <sys/resourcevar.h>
82 #include <sys/rwlock.h>
84 #include <sys/sysctl.h>
85 #include <sys/sysent.h>
89 #include <vm/vm_param.h>
91 #include <vm/vm_map.h>
92 #include <vm/vm_page.h>
93 #include <vm/vm_object.h>
94 #include <vm/vm_pager.h>
95 #include <vm/vm_kern.h>
96 #include <vm/vm_extern.h>
97 #include <vm/vnode_pager.h>
98 #include <vm/swap_pager.h>
102 * Virtual memory maps provide for the mapping, protection,
103 * and sharing of virtual memory objects. In addition,
104 * this module provides for an efficient virtual copy of
105 * memory from one map to another.
107 * Synchronization is required prior to most operations.
109 * Maps consist of an ordered doubly-linked list of simple
110 * entries; a self-adjusting binary search tree of these
111 * entries is used to speed up lookups.
113 * Since portions of maps are specified by start/end addresses,
114 * which may not align with existing map entries, all
115 * routines merely "clip" entries to these start/end values.
116 * [That is, an entry is split into two, bordering at a
117 * start or end value.] Note that these clippings may not
118 * always be necessary (as the two resulting entries are then
119 * not changed); however, the clipping is done for convenience.
121 * As mentioned above, virtual copy operations are performed
122 * by copying VM object references from one map to
123 * another, and then marking both regions as copy-on-write.
126 static struct mtx map_sleep_mtx;
127 static uma_zone_t mapentzone;
128 static uma_zone_t kmapentzone;
129 static uma_zone_t mapzone;
130 static uma_zone_t vmspace_zone;
131 static int vmspace_zinit(void *mem, int size, int flags);
132 static int vm_map_zinit(void *mem, int ize, int flags);
133 static void _vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min,
135 static void vm_map_entry_deallocate(vm_map_entry_t entry, boolean_t system_map);
136 static void vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry);
137 static void vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry);
138 static int vm_map_growstack(vm_map_t map, vm_offset_t addr,
139 vm_map_entry_t gap_entry);
140 static void vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
141 vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags);
143 static void vm_map_zdtor(void *mem, int size, void *arg);
144 static void vmspace_zdtor(void *mem, int size, void *arg);
146 static int vm_map_stack_locked(vm_map_t map, vm_offset_t addrbos,
147 vm_size_t max_ssize, vm_size_t growsize, vm_prot_t prot, vm_prot_t max,
149 static void vm_map_wire_entry_failure(vm_map_t map, vm_map_entry_t entry,
150 vm_offset_t failed_addr);
152 #define ENTRY_CHARGED(e) ((e)->cred != NULL || \
153 ((e)->object.vm_object != NULL && (e)->object.vm_object->cred != NULL && \
154 !((e)->eflags & MAP_ENTRY_NEEDS_COPY)))
157 * PROC_VMSPACE_{UN,}LOCK() can be a noop as long as vmspaces are type
160 #define PROC_VMSPACE_LOCK(p) do { } while (0)
161 #define PROC_VMSPACE_UNLOCK(p) do { } while (0)
164 * VM_MAP_RANGE_CHECK: [ internal use only ]
166 * Asserts that the starting and ending region
167 * addresses fall within the valid range of the map.
169 #define VM_MAP_RANGE_CHECK(map, start, end) \
171 if (start < vm_map_min(map)) \
172 start = vm_map_min(map); \
173 if (end > vm_map_max(map)) \
174 end = vm_map_max(map); \
182 * Initialize the vm_map module. Must be called before
183 * any other vm_map routines.
185 * Map and entry structures are allocated from the general
186 * purpose memory pool with some exceptions:
188 * - The kernel map and kmem submap are allocated statically.
189 * - Kernel map entries are allocated out of a static pool.
191 * These restrictions are necessary since malloc() uses the
192 * maps and requires map entries.
198 mtx_init(&map_sleep_mtx, "vm map sleep mutex", NULL, MTX_DEF);
199 mapzone = uma_zcreate("MAP", sizeof(struct vm_map), NULL,
205 vm_map_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
206 uma_prealloc(mapzone, MAX_KMAP);
207 kmapentzone = uma_zcreate("KMAP ENTRY", sizeof(struct vm_map_entry),
208 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
209 UMA_ZONE_MTXCLASS | UMA_ZONE_VM);
210 mapentzone = uma_zcreate("MAP ENTRY", sizeof(struct vm_map_entry),
211 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
212 vmspace_zone = uma_zcreate("VMSPACE", sizeof(struct vmspace), NULL,
218 vmspace_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
222 vmspace_zinit(void *mem, int size, int flags)
226 vm = (struct vmspace *)mem;
228 vm->vm_map.pmap = NULL;
229 (void)vm_map_zinit(&vm->vm_map, sizeof(vm->vm_map), flags);
230 PMAP_LOCK_INIT(vmspace_pmap(vm));
235 vm_map_zinit(void *mem, int size, int flags)
240 memset(map, 0, sizeof(*map));
241 mtx_init(&map->system_mtx, "vm map (system)", NULL, MTX_DEF | MTX_DUPOK);
242 sx_init(&map->lock, "vm map (user)");
248 vmspace_zdtor(void *mem, int size, void *arg)
252 vm = (struct vmspace *)mem;
254 vm_map_zdtor(&vm->vm_map, sizeof(vm->vm_map), arg);
257 vm_map_zdtor(void *mem, int size, void *arg)
262 KASSERT(map->nentries == 0,
263 ("map %p nentries == %d on free.",
264 map, map->nentries));
265 KASSERT(map->size == 0,
266 ("map %p size == %lu on free.",
267 map, (unsigned long)map->size));
269 #endif /* INVARIANTS */
272 * Allocate a vmspace structure, including a vm_map and pmap,
273 * and initialize those structures. The refcnt is set to 1.
275 * If 'pinit' is NULL then the embedded pmap is initialized via pmap_pinit().
278 vmspace_alloc(vm_offset_t min, vm_offset_t max, pmap_pinit_t pinit)
282 vm = uma_zalloc(vmspace_zone, M_WAITOK);
283 KASSERT(vm->vm_map.pmap == NULL, ("vm_map.pmap must be NULL"));
284 if (!pinit(vmspace_pmap(vm))) {
285 uma_zfree(vmspace_zone, vm);
288 CTR1(KTR_VM, "vmspace_alloc: %p", vm);
289 _vm_map_init(&vm->vm_map, vmspace_pmap(vm), min, max);
304 vmspace_container_reset(struct proc *p)
308 racct_set(p, RACCT_DATA, 0);
309 racct_set(p, RACCT_STACK, 0);
310 racct_set(p, RACCT_RSS, 0);
311 racct_set(p, RACCT_MEMLOCK, 0);
312 racct_set(p, RACCT_VMEM, 0);
318 vmspace_dofree(struct vmspace *vm)
321 CTR1(KTR_VM, "vmspace_free: %p", vm);
324 * Make sure any SysV shm is freed, it might not have been in
330 * Lock the map, to wait out all other references to it.
331 * Delete all of the mappings and pages they hold, then call
332 * the pmap module to reclaim anything left.
334 (void)vm_map_remove(&vm->vm_map, vm_map_min(&vm->vm_map),
335 vm_map_max(&vm->vm_map));
337 pmap_release(vmspace_pmap(vm));
338 vm->vm_map.pmap = NULL;
339 uma_zfree(vmspace_zone, vm);
343 vmspace_free(struct vmspace *vm)
346 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
347 "vmspace_free() called");
349 if (vm->vm_refcnt == 0)
350 panic("vmspace_free: attempt to free already freed vmspace");
352 if (atomic_fetchadd_int(&vm->vm_refcnt, -1) == 1)
357 vmspace_exitfree(struct proc *p)
361 PROC_VMSPACE_LOCK(p);
364 PROC_VMSPACE_UNLOCK(p);
365 KASSERT(vm == &vmspace0, ("vmspace_exitfree: wrong vmspace"));
370 vmspace_exit(struct thread *td)
377 * Release user portion of address space.
378 * This releases references to vnodes,
379 * which could cause I/O if the file has been unlinked.
380 * Need to do this early enough that we can still sleep.
382 * The last exiting process to reach this point releases as
383 * much of the environment as it can. vmspace_dofree() is the
384 * slower fallback in case another process had a temporary
385 * reference to the vmspace.
390 atomic_add_int(&vmspace0.vm_refcnt, 1);
391 refcnt = vm->vm_refcnt;
393 if (refcnt > 1 && p->p_vmspace != &vmspace0) {
394 /* Switch now since other proc might free vmspace */
395 PROC_VMSPACE_LOCK(p);
396 p->p_vmspace = &vmspace0;
397 PROC_VMSPACE_UNLOCK(p);
400 } while (!atomic_fcmpset_int(&vm->vm_refcnt, &refcnt, refcnt - 1));
402 if (p->p_vmspace != vm) {
403 /* vmspace not yet freed, switch back */
404 PROC_VMSPACE_LOCK(p);
406 PROC_VMSPACE_UNLOCK(p);
409 pmap_remove_pages(vmspace_pmap(vm));
410 /* Switch now since this proc will free vmspace */
411 PROC_VMSPACE_LOCK(p);
412 p->p_vmspace = &vmspace0;
413 PROC_VMSPACE_UNLOCK(p);
419 vmspace_container_reset(p);
423 /* Acquire reference to vmspace owned by another process. */
426 vmspace_acquire_ref(struct proc *p)
431 PROC_VMSPACE_LOCK(p);
434 PROC_VMSPACE_UNLOCK(p);
437 refcnt = vm->vm_refcnt;
439 if (refcnt <= 0) { /* Avoid 0->1 transition */
440 PROC_VMSPACE_UNLOCK(p);
443 } while (!atomic_fcmpset_int(&vm->vm_refcnt, &refcnt, refcnt + 1));
444 if (vm != p->p_vmspace) {
445 PROC_VMSPACE_UNLOCK(p);
449 PROC_VMSPACE_UNLOCK(p);
454 * Switch between vmspaces in an AIO kernel process.
456 * The AIO kernel processes switch to and from a user process's
457 * vmspace while performing an I/O operation on behalf of a user
458 * process. The new vmspace is either the vmspace of a user process
459 * obtained from an active AIO request or the initial vmspace of the
460 * AIO kernel process (when it is idling). Because user processes
461 * will block to drain any active AIO requests before proceeding in
462 * exit() or execve(), the vmspace reference count for these vmspaces
463 * can never be 0. This allows for a much simpler implementation than
464 * the loop in vmspace_acquire_ref() above. Similarly, AIO kernel
465 * processes hold an extra reference on their initial vmspace for the
466 * life of the process so that this guarantee is true for any vmspace
470 vmspace_switch_aio(struct vmspace *newvm)
472 struct vmspace *oldvm;
474 /* XXX: Need some way to assert that this is an aio daemon. */
476 KASSERT(newvm->vm_refcnt > 0,
477 ("vmspace_switch_aio: newvm unreferenced"));
479 oldvm = curproc->p_vmspace;
484 * Point to the new address space and refer to it.
486 curproc->p_vmspace = newvm;
487 atomic_add_int(&newvm->vm_refcnt, 1);
489 /* Activate the new mapping. */
490 pmap_activate(curthread);
492 /* Remove the daemon's reference to the old address space. */
493 KASSERT(oldvm->vm_refcnt > 1,
494 ("vmspace_switch_aio: oldvm dropping last reference"));
499 _vm_map_lock(vm_map_t map, const char *file, int line)
503 mtx_lock_flags_(&map->system_mtx, 0, file, line);
505 sx_xlock_(&map->lock, file, line);
510 vm_map_entry_set_vnode_text(vm_map_entry_t entry, bool add)
512 vm_object_t object, object1;
515 if ((entry->eflags & MAP_ENTRY_VN_EXEC) == 0)
517 KASSERT((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0,
518 ("Submap with execs"));
519 object = entry->object.vm_object;
520 KASSERT(object != NULL, ("No object for text, entry %p", entry));
521 VM_OBJECT_RLOCK(object);
522 while ((object1 = object->backing_object) != NULL) {
523 VM_OBJECT_RLOCK(object1);
524 VM_OBJECT_RUNLOCK(object);
529 * For OBJT_DEAD objects, v_writecount was handled in
530 * vnode_pager_dealloc().
532 if (object->type != OBJT_DEAD) {
533 KASSERT(((object->flags & OBJ_TMPFS) == 0 &&
534 object->type == OBJT_VNODE) ||
535 ((object->flags & OBJ_TMPFS) != 0 &&
536 object->type == OBJT_SWAP),
537 ("vm_map_entry_set_vnode_text: wrong object type, "
538 "entry %p, object %p, add %d", entry, object, add));
539 vp = (object->flags & OBJ_TMPFS) == 0 ? object->handle :
540 object->un_pager.swp.swp_tmpfs;
542 VOP_SET_TEXT_CHECKED(vp);
544 VOP_UNSET_TEXT_CHECKED(vp);
546 VM_OBJECT_RUNLOCK(object);
550 vm_map_process_deferred(void)
553 vm_map_entry_t entry, next;
557 entry = td->td_map_def_user;
558 td->td_map_def_user = NULL;
559 while (entry != NULL) {
561 MPASS((entry->eflags & (MAP_ENTRY_VN_WRITECNT |
562 MAP_ENTRY_VN_EXEC)) != (MAP_ENTRY_VN_WRITECNT |
564 if ((entry->eflags & MAP_ENTRY_VN_WRITECNT) != 0) {
566 * Decrement the object's writemappings and
567 * possibly the vnode's v_writecount.
569 KASSERT((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0,
570 ("Submap with writecount"));
571 object = entry->object.vm_object;
572 KASSERT(object != NULL, ("No object for writecount"));
573 vnode_pager_release_writecount(object, entry->start,
576 vm_map_entry_set_vnode_text(entry, false);
577 vm_map_entry_deallocate(entry, FALSE);
583 _vm_map_unlock(vm_map_t map, const char *file, int line)
587 mtx_unlock_flags_(&map->system_mtx, 0, file, line);
589 sx_xunlock_(&map->lock, file, line);
590 vm_map_process_deferred();
595 _vm_map_lock_read(vm_map_t map, const char *file, int line)
599 mtx_lock_flags_(&map->system_mtx, 0, file, line);
601 sx_slock_(&map->lock, file, line);
605 _vm_map_unlock_read(vm_map_t map, const char *file, int line)
609 mtx_unlock_flags_(&map->system_mtx, 0, file, line);
611 sx_sunlock_(&map->lock, file, line);
612 vm_map_process_deferred();
617 _vm_map_trylock(vm_map_t map, const char *file, int line)
621 error = map->system_map ?
622 !mtx_trylock_flags_(&map->system_mtx, 0, file, line) :
623 !sx_try_xlock_(&map->lock, file, line);
630 _vm_map_trylock_read(vm_map_t map, const char *file, int line)
634 error = map->system_map ?
635 !mtx_trylock_flags_(&map->system_mtx, 0, file, line) :
636 !sx_try_slock_(&map->lock, file, line);
641 * _vm_map_lock_upgrade: [ internal use only ]
643 * Tries to upgrade a read (shared) lock on the specified map to a write
644 * (exclusive) lock. Returns the value "0" if the upgrade succeeds and a
645 * non-zero value if the upgrade fails. If the upgrade fails, the map is
646 * returned without a read or write lock held.
648 * Requires that the map be read locked.
651 _vm_map_lock_upgrade(vm_map_t map, const char *file, int line)
653 unsigned int last_timestamp;
655 if (map->system_map) {
656 mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
658 if (!sx_try_upgrade_(&map->lock, file, line)) {
659 last_timestamp = map->timestamp;
660 sx_sunlock_(&map->lock, file, line);
661 vm_map_process_deferred();
663 * If the map's timestamp does not change while the
664 * map is unlocked, then the upgrade succeeds.
666 sx_xlock_(&map->lock, file, line);
667 if (last_timestamp != map->timestamp) {
668 sx_xunlock_(&map->lock, file, line);
678 _vm_map_lock_downgrade(vm_map_t map, const char *file, int line)
681 if (map->system_map) {
682 mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
684 sx_downgrade_(&map->lock, file, line);
690 * Returns a non-zero value if the caller holds a write (exclusive) lock
691 * on the specified map and the value "0" otherwise.
694 vm_map_locked(vm_map_t map)
698 return (mtx_owned(&map->system_mtx));
700 return (sx_xlocked(&map->lock));
705 _vm_map_assert_locked(vm_map_t map, const char *file, int line)
709 mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
711 sx_assert_(&map->lock, SA_XLOCKED, file, line);
714 #define VM_MAP_ASSERT_LOCKED(map) \
715 _vm_map_assert_locked(map, LOCK_FILE, LOCK_LINE)
718 static int enable_vmmap_check = 1;
720 static int enable_vmmap_check = 0;
722 SYSCTL_INT(_debug, OID_AUTO, vmmap_check, CTLFLAG_RWTUN,
723 &enable_vmmap_check, 0, "Enable vm map consistency checking");
726 _vm_map_assert_consistent(vm_map_t map)
728 vm_map_entry_t entry;
729 vm_map_entry_t child;
730 vm_size_t max_left, max_right;
732 if (!enable_vmmap_check)
735 for (entry = map->header.next; entry != &map->header;
736 entry = entry->next) {
737 KASSERT(entry->prev->end <= entry->start,
738 ("map %p prev->end = %jx, start = %jx", map,
739 (uintmax_t)entry->prev->end, (uintmax_t)entry->start));
740 KASSERT(entry->start < entry->end,
741 ("map %p start = %jx, end = %jx", map,
742 (uintmax_t)entry->start, (uintmax_t)entry->end));
743 KASSERT(entry->end <= entry->next->start,
744 ("map %p end = %jx, next->start = %jx", map,
745 (uintmax_t)entry->end, (uintmax_t)entry->next->start));
746 KASSERT(entry->left == NULL ||
747 entry->left->start < entry->start,
748 ("map %p left->start = %jx, start = %jx", map,
749 (uintmax_t)entry->left->start, (uintmax_t)entry->start));
750 KASSERT(entry->right == NULL ||
751 entry->start < entry->right->start,
752 ("map %p start = %jx, right->start = %jx", map,
753 (uintmax_t)entry->start, (uintmax_t)entry->right->start));
755 max_left = (child != NULL) ? child->max_free :
756 entry->start - entry->prev->end;
757 child = entry->right;
758 max_right = (child != NULL) ? child->max_free :
759 entry->next->start - entry->end;
760 KASSERT(entry->max_free == MAX(max_left, max_right),
761 ("map %p max = %jx, max_left = %jx, max_right = %jx", map,
762 (uintmax_t)entry->max_free,
763 (uintmax_t)max_left, (uintmax_t)max_right));
767 #define VM_MAP_ASSERT_CONSISTENT(map) \
768 _vm_map_assert_consistent(map)
770 #define VM_MAP_ASSERT_LOCKED(map)
771 #define VM_MAP_ASSERT_CONSISTENT(map)
772 #endif /* INVARIANTS */
775 * _vm_map_unlock_and_wait:
777 * Atomically releases the lock on the specified map and puts the calling
778 * thread to sleep. The calling thread will remain asleep until either
779 * vm_map_wakeup() is performed on the map or the specified timeout is
782 * WARNING! This function does not perform deferred deallocations of
783 * objects and map entries. Therefore, the calling thread is expected to
784 * reacquire the map lock after reawakening and later perform an ordinary
785 * unlock operation, such as vm_map_unlock(), before completing its
786 * operation on the map.
789 _vm_map_unlock_and_wait(vm_map_t map, int timo, const char *file, int line)
792 mtx_lock(&map_sleep_mtx);
794 mtx_unlock_flags_(&map->system_mtx, 0, file, line);
796 sx_xunlock_(&map->lock, file, line);
797 return (msleep(&map->root, &map_sleep_mtx, PDROP | PVM, "vmmaps",
804 * Awaken any threads that have slept on the map using
805 * vm_map_unlock_and_wait().
808 vm_map_wakeup(vm_map_t map)
812 * Acquire and release map_sleep_mtx to prevent a wakeup()
813 * from being performed (and lost) between the map unlock
814 * and the msleep() in _vm_map_unlock_and_wait().
816 mtx_lock(&map_sleep_mtx);
817 mtx_unlock(&map_sleep_mtx);
822 vm_map_busy(vm_map_t map)
825 VM_MAP_ASSERT_LOCKED(map);
830 vm_map_unbusy(vm_map_t map)
833 VM_MAP_ASSERT_LOCKED(map);
834 KASSERT(map->busy, ("vm_map_unbusy: not busy"));
835 if (--map->busy == 0 && (map->flags & MAP_BUSY_WAKEUP)) {
836 vm_map_modflags(map, 0, MAP_BUSY_WAKEUP);
842 vm_map_wait_busy(vm_map_t map)
845 VM_MAP_ASSERT_LOCKED(map);
847 vm_map_modflags(map, MAP_BUSY_WAKEUP, 0);
849 msleep(&map->busy, &map->system_mtx, 0, "mbusy", 0);
851 sx_sleep(&map->busy, &map->lock, 0, "mbusy", 0);
857 vmspace_resident_count(struct vmspace *vmspace)
859 return pmap_resident_count(vmspace_pmap(vmspace));
865 * Creates and returns a new empty VM map with
866 * the given physical map structure, and having
867 * the given lower and upper address bounds.
870 vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max)
874 result = uma_zalloc(mapzone, M_WAITOK);
875 CTR1(KTR_VM, "vm_map_create: %p", result);
876 _vm_map_init(result, pmap, min, max);
881 * Initialize an existing vm_map structure
882 * such as that in the vmspace structure.
885 _vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min, vm_offset_t max)
888 map->header.next = map->header.prev = &map->header;
889 map->header.eflags = MAP_ENTRY_HEADER;
890 map->needs_wakeup = FALSE;
893 map->header.end = min;
894 map->header.start = max;
903 vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min, vm_offset_t max)
906 _vm_map_init(map, pmap, min, max);
907 mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
908 sx_init(&map->lock, "user map");
912 * vm_map_entry_dispose: [ internal use only ]
914 * Inverse of vm_map_entry_create.
917 vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry)
919 uma_zfree(map->system_map ? kmapentzone : mapentzone, entry);
923 * vm_map_entry_create: [ internal use only ]
925 * Allocates a VM map entry for insertion.
926 * No entry fields are filled in.
928 static vm_map_entry_t
929 vm_map_entry_create(vm_map_t map)
931 vm_map_entry_t new_entry;
934 new_entry = uma_zalloc(kmapentzone, M_NOWAIT);
936 new_entry = uma_zalloc(mapentzone, M_WAITOK);
937 if (new_entry == NULL)
938 panic("vm_map_entry_create: kernel resources exhausted");
943 * vm_map_entry_set_behavior:
945 * Set the expected access behavior, either normal, random, or
949 vm_map_entry_set_behavior(vm_map_entry_t entry, u_char behavior)
951 entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) |
952 (behavior & MAP_ENTRY_BEHAV_MASK);
956 * vm_map_entry_set_max_free:
958 * Set the max_free field in a vm_map_entry.
961 vm_map_entry_set_max_free(vm_map_entry_t entry)
963 vm_map_entry_t child;
964 vm_size_t max_left, max_right;
967 max_left = (child != NULL) ? child->max_free :
968 entry->start - entry->prev->end;
969 child = entry->right;
970 max_right = (child != NULL) ? child->max_free :
971 entry->next->start - entry->end;
972 entry->max_free = MAX(max_left, max_right);
975 #define SPLAY_LEFT_STEP(root, y, rlist, test) do { \
977 if (y != NULL && (test)) { \
978 /* Rotate right and make y root. */ \
979 root->left = y->right; \
981 vm_map_entry_set_max_free(root); \
985 /* Put root on rlist. */ \
986 root->left = rlist; \
991 #define SPLAY_RIGHT_STEP(root, y, llist, test) do { \
993 if (y != NULL && (test)) { \
994 /* Rotate left and make y root. */ \
995 root->right = y->left; \
997 vm_map_entry_set_max_free(root); \
1001 /* Put root on llist. */ \
1002 root->right = llist; \
1008 * Walk down the tree until we find addr or a NULL pointer where addr would go,
1009 * breaking off left and right subtrees of nodes less than, or greater than
1010 * addr. Treat pointers to nodes with max_free < length as NULL pointers.
1011 * llist and rlist are the two sides in reverse order (bottom-up), with llist
1012 * linked by the right pointer and rlist linked by the left pointer in the
1015 static vm_map_entry_t
1016 vm_map_splay_split(vm_offset_t addr, vm_size_t length,
1017 vm_map_entry_t root, vm_map_entry_t *out_llist, vm_map_entry_t *out_rlist)
1019 vm_map_entry_t llist, rlist;
1024 while (root != NULL && root->max_free >= length) {
1025 if (addr < root->start) {
1026 SPLAY_LEFT_STEP(root, y, rlist,
1027 y->max_free >= length && addr < y->start);
1028 } else if (addr >= root->end) {
1029 SPLAY_RIGHT_STEP(root, y, llist,
1030 y->max_free >= length && addr >= y->end);
1040 vm_map_splay_findnext(vm_map_entry_t root, vm_map_entry_t *iolist)
1042 vm_map_entry_t rlist, y;
1046 while (root != NULL)
1047 SPLAY_LEFT_STEP(root, y, rlist, true);
1052 vm_map_splay_findprev(vm_map_entry_t root, vm_map_entry_t *iolist)
1054 vm_map_entry_t llist, y;
1058 while (root != NULL)
1059 SPLAY_RIGHT_STEP(root, y, llist, true);
1064 * Walk back up the two spines, flip the pointers and set max_free. The
1065 * subtrees of the root go at the bottom of llist and rlist.
1067 static vm_map_entry_t
1068 vm_map_splay_merge(vm_map_entry_t root,
1069 vm_map_entry_t llist, vm_map_entry_t rlist,
1070 vm_map_entry_t ltree, vm_map_entry_t rtree)
1074 while (llist != NULL) {
1076 llist->right = ltree;
1077 vm_map_entry_set_max_free(llist);
1081 while (rlist != NULL) {
1083 rlist->left = rtree;
1084 vm_map_entry_set_max_free(rlist);
1090 * Final assembly: add ltree and rtree as subtrees of root.
1093 root->right = rtree;
1094 vm_map_entry_set_max_free(root);
1100 * vm_map_entry_splay:
1102 * The Sleator and Tarjan top-down splay algorithm with the
1103 * following variation. Max_free must be computed bottom-up, so
1104 * on the downward pass, maintain the left and right spines in
1105 * reverse order. Then, make a second pass up each side to fix
1106 * the pointers and compute max_free. The time bound is O(log n)
1109 * The new root is the vm_map_entry containing "addr", or else an
1110 * adjacent entry (lower if possible) if addr is not in the tree.
1112 * The map must be locked, and leaves it so.
1114 * Returns: the new root.
1116 static vm_map_entry_t
1117 vm_map_entry_splay(vm_offset_t addr, vm_map_entry_t root)
1119 vm_map_entry_t llist, rlist;
1121 root = vm_map_splay_split(addr, 0, root, &llist, &rlist);
1124 } else if (llist != NULL) {
1126 * Recover the greatest node in the left
1127 * subtree and make it the root.
1130 llist = root->right;
1132 } else if (rlist != NULL) {
1134 * Recover the least node in the right
1135 * subtree and make it the root.
1141 /* There is no root. */
1144 return (vm_map_splay_merge(root, llist, rlist,
1145 root->left, root->right));
1149 * vm_map_entry_{un,}link:
1151 * Insert/remove entries from maps.
1154 vm_map_entry_link(vm_map_t map,
1155 vm_map_entry_t entry)
1157 vm_map_entry_t llist, rlist, root;
1160 "vm_map_entry_link: map %p, nentries %d, entry %p", map,
1161 map->nentries, entry);
1162 VM_MAP_ASSERT_LOCKED(map);
1165 root = vm_map_splay_split(entry->start, 0, root, &llist, &rlist);
1166 KASSERT(root == NULL,
1167 ("vm_map_entry_link: link object already mapped"));
1168 entry->prev = (llist == NULL) ? &map->header : llist;
1169 entry->next = (rlist == NULL) ? &map->header : rlist;
1170 entry->prev->next = entry->next->prev = entry;
1171 root = vm_map_splay_merge(entry, llist, rlist, NULL, NULL);
1173 VM_MAP_ASSERT_CONSISTENT(map);
1176 enum unlink_merge_type {
1183 vm_map_entry_unlink(vm_map_t map,
1184 vm_map_entry_t entry,
1185 enum unlink_merge_type op)
1187 vm_map_entry_t llist, rlist, root, y;
1189 VM_MAP_ASSERT_LOCKED(map);
1190 llist = entry->prev;
1191 rlist = entry->next;
1192 llist->next = rlist;
1193 rlist->prev = llist;
1195 root = vm_map_splay_split(entry->start, 0, root, &llist, &rlist);
1196 KASSERT(root != NULL,
1197 ("vm_map_entry_unlink: unlink object not mapped"));
1200 case UNLINK_MERGE_PREV:
1201 vm_map_splay_findprev(root, &llist);
1202 llist->end = root->end;
1205 llist = root->right;
1208 case UNLINK_MERGE_NEXT:
1209 vm_map_splay_findnext(root, &rlist);
1210 rlist->start = root->start;
1211 rlist->offset = root->offset;
1217 case UNLINK_MERGE_NONE:
1218 vm_map_splay_findprev(root, &llist);
1219 vm_map_splay_findnext(root, &rlist);
1220 if (llist != NULL) {
1222 llist = root->right;
1224 } else if (rlist != NULL) {
1233 root = vm_map_splay_merge(root, llist, rlist,
1234 root->left, root->right);
1236 VM_MAP_ASSERT_CONSISTENT(map);
1238 CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map,
1239 map->nentries, entry);
1243 * vm_map_entry_resize_free:
1245 * Recompute the amount of free space following a modified vm_map_entry
1246 * and propagate those values up the tree. Call this function after
1247 * resizing a map entry in-place by changing the end value, without a
1248 * call to vm_map_entry_link() or _unlink().
1250 * The map must be locked, and leaves it so.
1253 vm_map_entry_resize_free(vm_map_t map, vm_map_entry_t entry)
1255 vm_map_entry_t llist, rlist, root;
1257 VM_MAP_ASSERT_LOCKED(map);
1259 root = vm_map_splay_split(entry->start, 0, root, &llist, &rlist);
1260 KASSERT(root != NULL,
1261 ("vm_map_entry_resize_free: resize_free object not mapped"));
1262 vm_map_splay_findnext(root, &rlist);
1264 map->root = vm_map_splay_merge(root, llist, rlist,
1265 root->left, root->right);
1266 VM_MAP_ASSERT_CONSISTENT(map);
1267 CTR3(KTR_VM, "vm_map_entry_resize_free: map %p, nentries %d, entry %p", map,
1268 map->nentries, entry);
1272 * vm_map_lookup_entry: [ internal use only ]
1274 * Finds the map entry containing (or
1275 * immediately preceding) the specified address
1276 * in the given map; the entry is returned
1277 * in the "entry" parameter. The boolean
1278 * result indicates whether the address is
1279 * actually contained in the map.
1282 vm_map_lookup_entry(
1284 vm_offset_t address,
1285 vm_map_entry_t *entry) /* OUT */
1287 vm_map_entry_t cur, lbound;
1291 * If the map is empty, then the map entry immediately preceding
1292 * "address" is the map's header.
1296 *entry = &map->header;
1299 if (address >= cur->start && cur->end > address) {
1303 if ((locked = vm_map_locked(map)) ||
1304 sx_try_upgrade(&map->lock)) {
1306 * Splay requires a write lock on the map. However, it only
1307 * restructures the binary search tree; it does not otherwise
1308 * change the map. Thus, the map's timestamp need not change
1309 * on a temporary upgrade.
1311 map->root = cur = vm_map_entry_splay(address, cur);
1312 VM_MAP_ASSERT_CONSISTENT(map);
1314 sx_downgrade(&map->lock);
1317 * If "address" is contained within a map entry, the new root
1318 * is that map entry. Otherwise, the new root is a map entry
1319 * immediately before or after "address".
1321 if (address < cur->start) {
1322 *entry = &map->header;
1326 return (address < cur->end);
1329 * Since the map is only locked for read access, perform a
1330 * standard binary search tree lookup for "address".
1332 lbound = &map->header;
1334 if (address < cur->start) {
1336 } else if (cur->end <= address) {
1343 } while (cur != NULL);
1351 * Inserts the given whole VM object into the target
1352 * map at the specified address range. The object's
1353 * size should match that of the address range.
1355 * Requires that the map be locked, and leaves it so.
1357 * If object is non-NULL, ref count must be bumped by caller
1358 * prior to making call to account for the new entry.
1361 vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1362 vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max, int cow)
1364 vm_map_entry_t new_entry, prev_entry, temp_entry;
1366 vm_eflags_t protoeflags;
1367 vm_inherit_t inheritance;
1369 VM_MAP_ASSERT_LOCKED(map);
1370 KASSERT(object != kernel_object ||
1371 (cow & MAP_COPY_ON_WRITE) == 0,
1372 ("vm_map_insert: kernel object and COW"));
1373 KASSERT(object == NULL || (cow & MAP_NOFAULT) == 0,
1374 ("vm_map_insert: paradoxical MAP_NOFAULT request"));
1375 KASSERT((prot & ~max) == 0,
1376 ("prot %#x is not subset of max_prot %#x", prot, max));
1379 * Check that the start and end points are not bogus.
1381 if (start < vm_map_min(map) || end > vm_map_max(map) ||
1383 return (KERN_INVALID_ADDRESS);
1386 * Find the entry prior to the proposed starting address; if it's part
1387 * of an existing entry, this range is bogus.
1389 if (vm_map_lookup_entry(map, start, &temp_entry))
1390 return (KERN_NO_SPACE);
1392 prev_entry = temp_entry;
1395 * Assert that the next entry doesn't overlap the end point.
1397 if (prev_entry->next->start < end)
1398 return (KERN_NO_SPACE);
1400 if ((cow & MAP_CREATE_GUARD) != 0 && (object != NULL ||
1401 max != VM_PROT_NONE))
1402 return (KERN_INVALID_ARGUMENT);
1405 if (cow & MAP_COPY_ON_WRITE)
1406 protoeflags |= MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY;
1407 if (cow & MAP_NOFAULT)
1408 protoeflags |= MAP_ENTRY_NOFAULT;
1409 if (cow & MAP_DISABLE_SYNCER)
1410 protoeflags |= MAP_ENTRY_NOSYNC;
1411 if (cow & MAP_DISABLE_COREDUMP)
1412 protoeflags |= MAP_ENTRY_NOCOREDUMP;
1413 if (cow & MAP_STACK_GROWS_DOWN)
1414 protoeflags |= MAP_ENTRY_GROWS_DOWN;
1415 if (cow & MAP_STACK_GROWS_UP)
1416 protoeflags |= MAP_ENTRY_GROWS_UP;
1417 if (cow & MAP_VN_WRITECOUNT)
1418 protoeflags |= MAP_ENTRY_VN_WRITECNT;
1419 if (cow & MAP_VN_EXEC)
1420 protoeflags |= MAP_ENTRY_VN_EXEC;
1421 if ((cow & MAP_CREATE_GUARD) != 0)
1422 protoeflags |= MAP_ENTRY_GUARD;
1423 if ((cow & MAP_CREATE_STACK_GAP_DN) != 0)
1424 protoeflags |= MAP_ENTRY_STACK_GAP_DN;
1425 if ((cow & MAP_CREATE_STACK_GAP_UP) != 0)
1426 protoeflags |= MAP_ENTRY_STACK_GAP_UP;
1427 if (cow & MAP_INHERIT_SHARE)
1428 inheritance = VM_INHERIT_SHARE;
1430 inheritance = VM_INHERIT_DEFAULT;
1433 if ((cow & (MAP_ACC_NO_CHARGE | MAP_NOFAULT | MAP_CREATE_GUARD)) != 0)
1435 if ((cow & MAP_ACC_CHARGED) || ((prot & VM_PROT_WRITE) &&
1436 ((protoeflags & MAP_ENTRY_NEEDS_COPY) || object == NULL))) {
1437 if (!(cow & MAP_ACC_CHARGED) && !swap_reserve(end - start))
1438 return (KERN_RESOURCE_SHORTAGE);
1439 KASSERT(object == NULL ||
1440 (protoeflags & MAP_ENTRY_NEEDS_COPY) != 0 ||
1441 object->cred == NULL,
1442 ("overcommit: vm_map_insert o %p", object));
1443 cred = curthread->td_ucred;
1447 /* Expand the kernel pmap, if necessary. */
1448 if (map == kernel_map && end > kernel_vm_end)
1449 pmap_growkernel(end);
1450 if (object != NULL) {
1452 * OBJ_ONEMAPPING must be cleared unless this mapping
1453 * is trivially proven to be the only mapping for any
1454 * of the object's pages. (Object granularity
1455 * reference counting is insufficient to recognize
1456 * aliases with precision.)
1458 VM_OBJECT_WLOCK(object);
1459 if (object->ref_count > 1 || object->shadow_count != 0)
1460 vm_object_clear_flag(object, OBJ_ONEMAPPING);
1461 VM_OBJECT_WUNLOCK(object);
1462 } else if ((prev_entry->eflags & ~MAP_ENTRY_USER_WIRED) ==
1464 (cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP |
1465 MAP_VN_EXEC)) == 0 &&
1466 prev_entry->end == start && (prev_entry->cred == cred ||
1467 (prev_entry->object.vm_object != NULL &&
1468 prev_entry->object.vm_object->cred == cred)) &&
1469 vm_object_coalesce(prev_entry->object.vm_object,
1471 (vm_size_t)(prev_entry->end - prev_entry->start),
1472 (vm_size_t)(end - prev_entry->end), cred != NULL &&
1473 (protoeflags & MAP_ENTRY_NEEDS_COPY) == 0)) {
1475 * We were able to extend the object. Determine if we
1476 * can extend the previous map entry to include the
1477 * new range as well.
1479 if (prev_entry->inheritance == inheritance &&
1480 prev_entry->protection == prot &&
1481 prev_entry->max_protection == max &&
1482 prev_entry->wired_count == 0) {
1483 KASSERT((prev_entry->eflags & MAP_ENTRY_USER_WIRED) ==
1484 0, ("prev_entry %p has incoherent wiring",
1486 if ((prev_entry->eflags & MAP_ENTRY_GUARD) == 0)
1487 map->size += end - prev_entry->end;
1488 prev_entry->end = end;
1489 vm_map_entry_resize_free(map, prev_entry);
1490 vm_map_simplify_entry(map, prev_entry);
1491 return (KERN_SUCCESS);
1495 * If we can extend the object but cannot extend the
1496 * map entry, we have to create a new map entry. We
1497 * must bump the ref count on the extended object to
1498 * account for it. object may be NULL.
1500 object = prev_entry->object.vm_object;
1501 offset = prev_entry->offset +
1502 (prev_entry->end - prev_entry->start);
1503 vm_object_reference(object);
1504 if (cred != NULL && object != NULL && object->cred != NULL &&
1505 !(prev_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
1506 /* Object already accounts for this uid. */
1514 * Create a new entry
1516 new_entry = vm_map_entry_create(map);
1517 new_entry->start = start;
1518 new_entry->end = end;
1519 new_entry->cred = NULL;
1521 new_entry->eflags = protoeflags;
1522 new_entry->object.vm_object = object;
1523 new_entry->offset = offset;
1525 new_entry->inheritance = inheritance;
1526 new_entry->protection = prot;
1527 new_entry->max_protection = max;
1528 new_entry->wired_count = 0;
1529 new_entry->wiring_thread = NULL;
1530 new_entry->read_ahead = VM_FAULT_READ_AHEAD_INIT;
1531 new_entry->next_read = start;
1533 KASSERT(cred == NULL || !ENTRY_CHARGED(new_entry),
1534 ("overcommit: vm_map_insert leaks vm_map %p", new_entry));
1535 new_entry->cred = cred;
1538 * Insert the new entry into the list
1540 vm_map_entry_link(map, new_entry);
1541 if ((new_entry->eflags & MAP_ENTRY_GUARD) == 0)
1542 map->size += new_entry->end - new_entry->start;
1545 * Try to coalesce the new entry with both the previous and next
1546 * entries in the list. Previously, we only attempted to coalesce
1547 * with the previous entry when object is NULL. Here, we handle the
1548 * other cases, which are less common.
1550 vm_map_simplify_entry(map, new_entry);
1552 if ((cow & (MAP_PREFAULT | MAP_PREFAULT_PARTIAL)) != 0) {
1553 vm_map_pmap_enter(map, start, prot, object, OFF_TO_IDX(offset),
1554 end - start, cow & MAP_PREFAULT_PARTIAL);
1557 return (KERN_SUCCESS);
1563 * Find the first fit (lowest VM address) for "length" free bytes
1564 * beginning at address >= start in the given map.
1566 * In a vm_map_entry, "max_free" is the maximum amount of
1567 * contiguous free space between an entry in its subtree and a
1568 * neighbor of that entry. This allows finding a free region in
1569 * one path down the tree, so O(log n) amortized with splay
1572 * The map must be locked, and leaves it so.
1574 * Returns: starting address if sufficient space,
1575 * vm_map_max(map)-length+1 if insufficient space.
1578 vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length)
1580 vm_map_entry_t llist, rlist, root, y;
1581 vm_size_t left_length;
1584 * Request must fit within min/max VM address and must avoid
1587 start = MAX(start, vm_map_min(map));
1588 if (start + length > vm_map_max(map) || start + length < start)
1589 return (vm_map_max(map) - length + 1);
1591 /* Empty tree means wide open address space. */
1592 if (map->root == NULL)
1596 * After splay, if start comes before root node, then there
1597 * must be a gap from start to the root.
1599 root = vm_map_splay_split(start, length, map->root,
1603 else if (rlist != NULL) {
1609 llist = root->right;
1612 map->root = vm_map_splay_merge(root, llist, rlist,
1613 root->left, root->right);
1614 VM_MAP_ASSERT_CONSISTENT(map);
1615 if (start + length <= root->start)
1619 * Root is the last node that might begin its gap before
1620 * start, and this is the last comparison where address
1621 * wrap might be a problem.
1623 if (root->right == NULL &&
1624 start + length <= vm_map_max(map))
1627 /* With max_free, can immediately tell if no solution. */
1628 if (root->right == NULL || length > root->right->max_free)
1629 return (vm_map_max(map) - length + 1);
1632 * Splay for the least large-enough gap in the right subtree.
1636 for (left_length = 0; ;
1637 left_length = root->left != NULL ?
1638 root->left->max_free : root->start - llist->end) {
1639 if (length <= left_length)
1640 SPLAY_LEFT_STEP(root, y, rlist,
1641 length <= (y->left != NULL ?
1642 y->left->max_free : y->start - llist->end));
1644 SPLAY_RIGHT_STEP(root, y, llist,
1645 length > (y->left != NULL ?
1646 y->left->max_free : y->start - root->end));
1651 llist = root->right;
1652 if ((y = rlist) == NULL)
1657 root->right = y->right;
1659 root = vm_map_splay_merge(root, llist, rlist,
1660 root->left, root->right);
1662 y->right = root->right;
1663 vm_map_entry_set_max_free(y);
1665 vm_map_entry_set_max_free(root);
1668 VM_MAP_ASSERT_CONSISTENT(map);
1673 vm_map_fixed(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1674 vm_offset_t start, vm_size_t length, vm_prot_t prot,
1675 vm_prot_t max, int cow)
1680 end = start + length;
1681 KASSERT((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0 ||
1683 ("vm_map_fixed: non-NULL backing object for stack"));
1685 VM_MAP_RANGE_CHECK(map, start, end);
1686 if ((cow & MAP_CHECK_EXCL) == 0)
1687 vm_map_delete(map, start, end);
1688 if ((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) != 0) {
1689 result = vm_map_stack_locked(map, start, length, sgrowsiz,
1692 result = vm_map_insert(map, object, offset, start, end,
1699 static const int aslr_pages_rnd_64[2] = {0x1000, 0x10};
1700 static const int aslr_pages_rnd_32[2] = {0x100, 0x4};
1702 static int cluster_anon = 1;
1703 SYSCTL_INT(_vm, OID_AUTO, cluster_anon, CTLFLAG_RW,
1705 "Cluster anonymous mappings: 0 = no, 1 = yes if no hint, 2 = always");
1708 clustering_anon_allowed(vm_offset_t addr)
1711 switch (cluster_anon) {
1722 static long aslr_restarts;
1723 SYSCTL_LONG(_vm, OID_AUTO, aslr_restarts, CTLFLAG_RD,
1725 "Number of aslr failures");
1727 #define MAP_32BIT_MAX_ADDR ((vm_offset_t)1 << 31)
1730 * Searches for the specified amount of free space in the given map with the
1731 * specified alignment. Performs an address-ordered, first-fit search from
1732 * the given address "*addr", with an optional upper bound "max_addr". If the
1733 * parameter "alignment" is zero, then the alignment is computed from the
1734 * given (object, offset) pair so as to enable the greatest possible use of
1735 * superpage mappings. Returns KERN_SUCCESS and the address of the free space
1736 * in "*addr" if successful. Otherwise, returns KERN_NO_SPACE.
1738 * The map must be locked. Initially, there must be at least "length" bytes
1739 * of free space at the given address.
1742 vm_map_alignspace(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1743 vm_offset_t *addr, vm_size_t length, vm_offset_t max_addr,
1744 vm_offset_t alignment)
1746 vm_offset_t aligned_addr, free_addr;
1748 VM_MAP_ASSERT_LOCKED(map);
1750 KASSERT(free_addr == vm_map_findspace(map, free_addr, length),
1751 ("caller failed to provide space %d at address %p",
1752 (int)length, (void*)free_addr));
1755 * At the start of every iteration, the free space at address
1756 * "*addr" is at least "length" bytes.
1759 pmap_align_superpage(object, offset, addr, length);
1760 else if ((*addr & (alignment - 1)) != 0) {
1761 *addr &= ~(alignment - 1);
1764 aligned_addr = *addr;
1765 if (aligned_addr == free_addr) {
1767 * Alignment did not change "*addr", so "*addr" must
1768 * still provide sufficient free space.
1770 return (KERN_SUCCESS);
1774 * Test for address wrap on "*addr". A wrapped "*addr" could
1775 * be a valid address, in which case vm_map_findspace() cannot
1776 * be relied upon to fail.
1778 if (aligned_addr < free_addr)
1779 return (KERN_NO_SPACE);
1780 *addr = vm_map_findspace(map, aligned_addr, length);
1781 if (*addr + length > vm_map_max(map) ||
1782 (max_addr != 0 && *addr + length > max_addr))
1783 return (KERN_NO_SPACE);
1785 if (free_addr == aligned_addr) {
1787 * If a successful call to vm_map_findspace() did not
1788 * change "*addr", then "*addr" must still be aligned
1789 * and provide sufficient free space.
1791 return (KERN_SUCCESS);
1797 * vm_map_find finds an unallocated region in the target address
1798 * map with the given length. The search is defined to be
1799 * first-fit from the specified address; the region found is
1800 * returned in the same parameter.
1802 * If object is non-NULL, ref count must be bumped by caller
1803 * prior to making call to account for the new entry.
1806 vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1807 vm_offset_t *addr, /* IN/OUT */
1808 vm_size_t length, vm_offset_t max_addr, int find_space,
1809 vm_prot_t prot, vm_prot_t max, int cow)
1811 vm_offset_t alignment, curr_min_addr, min_addr;
1812 int gap, pidx, rv, try;
1813 bool cluster, en_aslr, update_anon;
1815 KASSERT((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0 ||
1817 ("vm_map_find: non-NULL backing object for stack"));
1818 MPASS((cow & MAP_REMAP) == 0 || (find_space == VMFS_NO_SPACE &&
1819 (cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0));
1820 if (find_space == VMFS_OPTIMAL_SPACE && (object == NULL ||
1821 (object->flags & OBJ_COLORED) == 0))
1822 find_space = VMFS_ANY_SPACE;
1823 if (find_space >> 8 != 0) {
1824 KASSERT((find_space & 0xff) == 0, ("bad VMFS flags"));
1825 alignment = (vm_offset_t)1 << (find_space >> 8);
1828 en_aslr = (map->flags & MAP_ASLR) != 0;
1829 update_anon = cluster = clustering_anon_allowed(*addr) &&
1830 (map->flags & MAP_IS_SUB_MAP) == 0 && max_addr == 0 &&
1831 find_space != VMFS_NO_SPACE && object == NULL &&
1832 (cow & (MAP_INHERIT_SHARE | MAP_STACK_GROWS_UP |
1833 MAP_STACK_GROWS_DOWN)) == 0 && prot != PROT_NONE;
1834 curr_min_addr = min_addr = *addr;
1835 if (en_aslr && min_addr == 0 && !cluster &&
1836 find_space != VMFS_NO_SPACE &&
1837 (map->flags & MAP_ASLR_IGNSTART) != 0)
1838 curr_min_addr = min_addr = vm_map_min(map);
1842 curr_min_addr = map->anon_loc;
1843 if (curr_min_addr == 0)
1846 if (find_space != VMFS_NO_SPACE) {
1847 KASSERT(find_space == VMFS_ANY_SPACE ||
1848 find_space == VMFS_OPTIMAL_SPACE ||
1849 find_space == VMFS_SUPER_SPACE ||
1850 alignment != 0, ("unexpected VMFS flag"));
1853 * When creating an anonymous mapping, try clustering
1854 * with an existing anonymous mapping first.
1856 * We make up to two attempts to find address space
1857 * for a given find_space value. The first attempt may
1858 * apply randomization or may cluster with an existing
1859 * anonymous mapping. If this first attempt fails,
1860 * perform a first-fit search of the available address
1863 * If all tries failed, and find_space is
1864 * VMFS_OPTIMAL_SPACE, fallback to VMFS_ANY_SPACE.
1865 * Again enable clustering and randomization.
1872 * Second try: we failed either to find a
1873 * suitable region for randomizing the
1874 * allocation, or to cluster with an existing
1875 * mapping. Retry with free run.
1877 curr_min_addr = (map->flags & MAP_ASLR_IGNSTART) != 0 ?
1878 vm_map_min(map) : min_addr;
1879 atomic_add_long(&aslr_restarts, 1);
1882 if (try == 1 && en_aslr && !cluster) {
1884 * Find space for allocation, including
1885 * gap needed for later randomization.
1887 pidx = MAXPAGESIZES > 1 && pagesizes[1] != 0 &&
1888 (find_space == VMFS_SUPER_SPACE || find_space ==
1889 VMFS_OPTIMAL_SPACE) ? 1 : 0;
1890 gap = vm_map_max(map) > MAP_32BIT_MAX_ADDR &&
1891 (max_addr == 0 || max_addr > MAP_32BIT_MAX_ADDR) ?
1892 aslr_pages_rnd_64[pidx] : aslr_pages_rnd_32[pidx];
1893 *addr = vm_map_findspace(map, curr_min_addr,
1894 length + gap * pagesizes[pidx]);
1895 if (*addr + length + gap * pagesizes[pidx] >
1898 /* And randomize the start address. */
1899 *addr += (arc4random() % gap) * pagesizes[pidx];
1900 if (max_addr != 0 && *addr + length > max_addr)
1903 *addr = vm_map_findspace(map, curr_min_addr, length);
1904 if (*addr + length > vm_map_max(map) ||
1905 (max_addr != 0 && *addr + length > max_addr)) {
1916 if (find_space != VMFS_ANY_SPACE &&
1917 (rv = vm_map_alignspace(map, object, offset, addr, length,
1918 max_addr, alignment)) != KERN_SUCCESS) {
1919 if (find_space == VMFS_OPTIMAL_SPACE) {
1920 find_space = VMFS_ANY_SPACE;
1921 curr_min_addr = min_addr;
1922 cluster = update_anon;
1928 } else if ((cow & MAP_REMAP) != 0) {
1929 if (*addr < vm_map_min(map) ||
1930 *addr + length > vm_map_max(map) ||
1931 *addr + length <= length) {
1932 rv = KERN_INVALID_ADDRESS;
1935 vm_map_delete(map, *addr, *addr + length);
1937 if ((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) != 0) {
1938 rv = vm_map_stack_locked(map, *addr, length, sgrowsiz, prot,
1941 rv = vm_map_insert(map, object, offset, *addr, *addr + length,
1944 if (rv == KERN_SUCCESS && update_anon)
1945 map->anon_loc = *addr + length;
1952 * vm_map_find_min() is a variant of vm_map_find() that takes an
1953 * additional parameter (min_addr) and treats the given address
1954 * (*addr) differently. Specifically, it treats *addr as a hint
1955 * and not as the minimum address where the mapping is created.
1957 * This function works in two phases. First, it tries to
1958 * allocate above the hint. If that fails and the hint is
1959 * greater than min_addr, it performs a second pass, replacing
1960 * the hint with min_addr as the minimum address for the
1964 vm_map_find_min(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1965 vm_offset_t *addr, vm_size_t length, vm_offset_t min_addr,
1966 vm_offset_t max_addr, int find_space, vm_prot_t prot, vm_prot_t max,
1974 rv = vm_map_find(map, object, offset, addr, length, max_addr,
1975 find_space, prot, max, cow);
1976 if (rv == KERN_SUCCESS || min_addr >= hint)
1978 *addr = hint = min_addr;
1983 * A map entry with any of the following flags set must not be merged with
1986 #define MAP_ENTRY_NOMERGE_MASK (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP | \
1987 MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP | MAP_ENTRY_VN_EXEC)
1990 vm_map_mergeable_neighbors(vm_map_entry_t prev, vm_map_entry_t entry)
1993 KASSERT((prev->eflags & MAP_ENTRY_NOMERGE_MASK) == 0 ||
1994 (entry->eflags & MAP_ENTRY_NOMERGE_MASK) == 0,
1995 ("vm_map_mergeable_neighbors: neither %p nor %p are mergeable",
1997 return (prev->end == entry->start &&
1998 prev->object.vm_object == entry->object.vm_object &&
1999 (prev->object.vm_object == NULL ||
2000 prev->offset + (prev->end - prev->start) == entry->offset) &&
2001 prev->eflags == entry->eflags &&
2002 prev->protection == entry->protection &&
2003 prev->max_protection == entry->max_protection &&
2004 prev->inheritance == entry->inheritance &&
2005 prev->wired_count == entry->wired_count &&
2006 prev->cred == entry->cred);
2010 vm_map_merged_neighbor_dispose(vm_map_t map, vm_map_entry_t entry)
2014 * If the backing object is a vnode object, vm_object_deallocate()
2015 * calls vrele(). However, vrele() does not lock the vnode because
2016 * the vnode has additional references. Thus, the map lock can be
2017 * kept without causing a lock-order reversal with the vnode lock.
2019 * Since we count the number of virtual page mappings in
2020 * object->un_pager.vnp.writemappings, the writemappings value
2021 * should not be adjusted when the entry is disposed of.
2023 if (entry->object.vm_object != NULL)
2024 vm_object_deallocate(entry->object.vm_object);
2025 if (entry->cred != NULL)
2026 crfree(entry->cred);
2027 vm_map_entry_dispose(map, entry);
2031 * vm_map_simplify_entry:
2033 * Simplify the given map entry by merging with either neighbor. This
2034 * routine also has the ability to merge with both neighbors.
2036 * The map must be locked.
2038 * This routine guarantees that the passed entry remains valid (though
2039 * possibly extended). When merging, this routine may delete one or
2043 vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry)
2045 vm_map_entry_t next, prev;
2047 if ((entry->eflags & MAP_ENTRY_NOMERGE_MASK) != 0)
2050 if (vm_map_mergeable_neighbors(prev, entry)) {
2051 vm_map_entry_unlink(map, prev, UNLINK_MERGE_NEXT);
2052 vm_map_merged_neighbor_dispose(map, prev);
2055 if (vm_map_mergeable_neighbors(entry, next)) {
2056 vm_map_entry_unlink(map, next, UNLINK_MERGE_PREV);
2057 vm_map_merged_neighbor_dispose(map, next);
2062 * vm_map_clip_start: [ internal use only ]
2064 * Asserts that the given entry begins at or after
2065 * the specified address; if necessary,
2066 * it splits the entry into two.
2068 #define vm_map_clip_start(map, entry, startaddr) \
2070 if (startaddr > entry->start) \
2071 _vm_map_clip_start(map, entry, startaddr); \
2075 * This routine is called only when it is known that
2076 * the entry must be split.
2079 _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start)
2081 vm_map_entry_t new_entry;
2083 VM_MAP_ASSERT_LOCKED(map);
2084 KASSERT(entry->end > start && entry->start < start,
2085 ("_vm_map_clip_start: invalid clip of entry %p", entry));
2088 * Split off the front portion -- note that we must insert the new
2089 * entry BEFORE this one, so that this entry has the specified
2092 vm_map_simplify_entry(map, entry);
2095 * If there is no object backing this entry, we might as well create
2096 * one now. If we defer it, an object can get created after the map
2097 * is clipped, and individual objects will be created for the split-up
2098 * map. This is a bit of a hack, but is also about the best place to
2099 * put this improvement.
2101 if (entry->object.vm_object == NULL && !map->system_map &&
2102 (entry->eflags & MAP_ENTRY_GUARD) == 0) {
2104 object = vm_object_allocate(OBJT_DEFAULT,
2105 atop(entry->end - entry->start));
2106 entry->object.vm_object = object;
2108 if (entry->cred != NULL) {
2109 object->cred = entry->cred;
2110 object->charge = entry->end - entry->start;
2113 } else if (entry->object.vm_object != NULL &&
2114 ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) &&
2115 entry->cred != NULL) {
2116 VM_OBJECT_WLOCK(entry->object.vm_object);
2117 KASSERT(entry->object.vm_object->cred == NULL,
2118 ("OVERCOMMIT: vm_entry_clip_start: both cred e %p", entry));
2119 entry->object.vm_object->cred = entry->cred;
2120 entry->object.vm_object->charge = entry->end - entry->start;
2121 VM_OBJECT_WUNLOCK(entry->object.vm_object);
2125 new_entry = vm_map_entry_create(map);
2126 *new_entry = *entry;
2128 new_entry->end = start;
2129 entry->offset += (start - entry->start);
2130 entry->start = start;
2131 if (new_entry->cred != NULL)
2132 crhold(entry->cred);
2134 vm_map_entry_link(map, new_entry);
2136 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
2137 vm_object_reference(new_entry->object.vm_object);
2138 vm_map_entry_set_vnode_text(new_entry, true);
2140 * The object->un_pager.vnp.writemappings for the
2141 * object of MAP_ENTRY_VN_WRITECNT type entry shall be
2142 * kept as is here. The virtual pages are
2143 * re-distributed among the clipped entries, so the sum is
2150 * vm_map_clip_end: [ internal use only ]
2152 * Asserts that the given entry ends at or before
2153 * the specified address; if necessary,
2154 * it splits the entry into two.
2156 #define vm_map_clip_end(map, entry, endaddr) \
2158 if ((endaddr) < (entry->end)) \
2159 _vm_map_clip_end((map), (entry), (endaddr)); \
2163 * This routine is called only when it is known that
2164 * the entry must be split.
2167 _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end)
2169 vm_map_entry_t new_entry;
2171 VM_MAP_ASSERT_LOCKED(map);
2172 KASSERT(entry->start < end && entry->end > end,
2173 ("_vm_map_clip_end: invalid clip of entry %p", entry));
2176 * If there is no object backing this entry, we might as well create
2177 * one now. If we defer it, an object can get created after the map
2178 * is clipped, and individual objects will be created for the split-up
2179 * map. This is a bit of a hack, but is also about the best place to
2180 * put this improvement.
2182 if (entry->object.vm_object == NULL && !map->system_map &&
2183 (entry->eflags & MAP_ENTRY_GUARD) == 0) {
2185 object = vm_object_allocate(OBJT_DEFAULT,
2186 atop(entry->end - entry->start));
2187 entry->object.vm_object = object;
2189 if (entry->cred != NULL) {
2190 object->cred = entry->cred;
2191 object->charge = entry->end - entry->start;
2194 } else if (entry->object.vm_object != NULL &&
2195 ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) &&
2196 entry->cred != NULL) {
2197 VM_OBJECT_WLOCK(entry->object.vm_object);
2198 KASSERT(entry->object.vm_object->cred == NULL,
2199 ("OVERCOMMIT: vm_entry_clip_end: both cred e %p", entry));
2200 entry->object.vm_object->cred = entry->cred;
2201 entry->object.vm_object->charge = entry->end - entry->start;
2202 VM_OBJECT_WUNLOCK(entry->object.vm_object);
2207 * Create a new entry and insert it AFTER the specified entry
2209 new_entry = vm_map_entry_create(map);
2210 *new_entry = *entry;
2212 new_entry->start = entry->end = end;
2213 new_entry->offset += (end - entry->start);
2214 if (new_entry->cred != NULL)
2215 crhold(entry->cred);
2217 vm_map_entry_link(map, new_entry);
2219 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
2220 vm_object_reference(new_entry->object.vm_object);
2221 vm_map_entry_set_vnode_text(new_entry, true);
2226 * vm_map_submap: [ kernel use only ]
2228 * Mark the given range as handled by a subordinate map.
2230 * This range must have been created with vm_map_find,
2231 * and no other operations may have been performed on this
2232 * range prior to calling vm_map_submap.
2234 * Only a limited number of operations can be performed
2235 * within this rage after calling vm_map_submap:
2237 * [Don't try vm_map_copy!]
2239 * To remove a submapping, one must first remove the
2240 * range from the superior map, and then destroy the
2241 * submap (if desired). [Better yet, don't try it.]
2250 vm_map_entry_t entry;
2253 result = KERN_INVALID_ARGUMENT;
2255 vm_map_lock(submap);
2256 submap->flags |= MAP_IS_SUB_MAP;
2257 vm_map_unlock(submap);
2261 VM_MAP_RANGE_CHECK(map, start, end);
2263 if (vm_map_lookup_entry(map, start, &entry)) {
2264 vm_map_clip_start(map, entry, start);
2266 entry = entry->next;
2268 vm_map_clip_end(map, entry, end);
2270 if ((entry->start == start) && (entry->end == end) &&
2271 ((entry->eflags & MAP_ENTRY_COW) == 0) &&
2272 (entry->object.vm_object == NULL)) {
2273 entry->object.sub_map = submap;
2274 entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
2275 result = KERN_SUCCESS;
2279 if (result != KERN_SUCCESS) {
2280 vm_map_lock(submap);
2281 submap->flags &= ~MAP_IS_SUB_MAP;
2282 vm_map_unlock(submap);
2288 * The maximum number of pages to map if MAP_PREFAULT_PARTIAL is specified
2290 #define MAX_INIT_PT 96
2293 * vm_map_pmap_enter:
2295 * Preload the specified map's pmap with mappings to the specified
2296 * object's memory-resident pages. No further physical pages are
2297 * allocated, and no further virtual pages are retrieved from secondary
2298 * storage. If the specified flags include MAP_PREFAULT_PARTIAL, then a
2299 * limited number of page mappings are created at the low-end of the
2300 * specified address range. (For this purpose, a superpage mapping
2301 * counts as one page mapping.) Otherwise, all resident pages within
2302 * the specified address range are mapped.
2305 vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
2306 vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags)
2309 vm_page_t p, p_start;
2310 vm_pindex_t mask, psize, threshold, tmpidx;
2312 if ((prot & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 || object == NULL)
2314 VM_OBJECT_RLOCK(object);
2315 if (object->type == OBJT_DEVICE || object->type == OBJT_SG) {
2316 VM_OBJECT_RUNLOCK(object);
2317 VM_OBJECT_WLOCK(object);
2318 if (object->type == OBJT_DEVICE || object->type == OBJT_SG) {
2319 pmap_object_init_pt(map->pmap, addr, object, pindex,
2321 VM_OBJECT_WUNLOCK(object);
2324 VM_OBJECT_LOCK_DOWNGRADE(object);
2328 if (psize + pindex > object->size) {
2329 if (object->size < pindex) {
2330 VM_OBJECT_RUNLOCK(object);
2333 psize = object->size - pindex;
2338 threshold = MAX_INIT_PT;
2340 p = vm_page_find_least(object, pindex);
2342 * Assert: the variable p is either (1) the page with the
2343 * least pindex greater than or equal to the parameter pindex
2347 p != NULL && (tmpidx = p->pindex - pindex) < psize;
2348 p = TAILQ_NEXT(p, listq)) {
2350 * don't allow an madvise to blow away our really
2351 * free pages allocating pv entries.
2353 if (((flags & MAP_PREFAULT_MADVISE) != 0 &&
2354 vm_page_count_severe()) ||
2355 ((flags & MAP_PREFAULT_PARTIAL) != 0 &&
2356 tmpidx >= threshold)) {
2360 if (p->valid == VM_PAGE_BITS_ALL) {
2361 if (p_start == NULL) {
2362 start = addr + ptoa(tmpidx);
2365 /* Jump ahead if a superpage mapping is possible. */
2366 if (p->psind > 0 && ((addr + ptoa(tmpidx)) &
2367 (pagesizes[p->psind] - 1)) == 0) {
2368 mask = atop(pagesizes[p->psind]) - 1;
2369 if (tmpidx + mask < psize &&
2370 vm_page_ps_test(p, PS_ALL_VALID, NULL)) {
2375 } else if (p_start != NULL) {
2376 pmap_enter_object(map->pmap, start, addr +
2377 ptoa(tmpidx), p_start, prot);
2381 if (p_start != NULL)
2382 pmap_enter_object(map->pmap, start, addr + ptoa(psize),
2384 VM_OBJECT_RUNLOCK(object);
2390 * Sets the protection of the specified address
2391 * region in the target map. If "set_max" is
2392 * specified, the maximum protection is to be set;
2393 * otherwise, only the current protection is affected.
2396 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
2397 vm_prot_t new_prot, boolean_t set_max)
2399 vm_map_entry_t current, entry, in_tran;
2405 return (KERN_SUCCESS);
2412 * Ensure that we are not concurrently wiring pages. vm_map_wire() may
2413 * need to fault pages into the map and will drop the map lock while
2414 * doing so, and the VM object may end up in an inconsistent state if we
2415 * update the protection on the map entry in between faults.
2417 vm_map_wait_busy(map);
2419 VM_MAP_RANGE_CHECK(map, start, end);
2421 if (vm_map_lookup_entry(map, start, &entry)) {
2422 vm_map_clip_start(map, entry, start);
2424 entry = entry->next;
2428 * Make a first pass to check for protection violations.
2430 for (current = entry; current->start < end; current = current->next) {
2431 if ((current->eflags & MAP_ENTRY_GUARD) != 0)
2433 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
2435 return (KERN_INVALID_ARGUMENT);
2437 if ((new_prot & current->max_protection) != new_prot) {
2439 return (KERN_PROTECTION_FAILURE);
2441 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0)
2446 * Postpone the operation until all in transition map entries
2447 * are stabilized. In-transition entry might already have its
2448 * pages wired and wired_count incremented, but
2449 * MAP_ENTRY_USER_WIRED flag not yet set, and visible to other
2450 * threads because the map lock is dropped. In this case we
2451 * would miss our call to vm_fault_copy_entry().
2453 if (in_tran != NULL) {
2454 in_tran->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2455 vm_map_unlock_and_wait(map, 0);
2460 * Do an accounting pass for private read-only mappings that
2461 * now will do cow due to allowed write (e.g. debugger sets
2462 * breakpoint on text segment)
2464 for (current = entry; current->start < end; current = current->next) {
2466 vm_map_clip_end(map, current, end);
2469 ((new_prot & ~(current->protection)) & VM_PROT_WRITE) == 0 ||
2470 ENTRY_CHARGED(current) ||
2471 (current->eflags & MAP_ENTRY_GUARD) != 0) {
2475 cred = curthread->td_ucred;
2476 obj = current->object.vm_object;
2478 if (obj == NULL || (current->eflags & MAP_ENTRY_NEEDS_COPY)) {
2479 if (!swap_reserve(current->end - current->start)) {
2481 return (KERN_RESOURCE_SHORTAGE);
2484 current->cred = cred;
2488 VM_OBJECT_WLOCK(obj);
2489 if (obj->type != OBJT_DEFAULT && obj->type != OBJT_SWAP) {
2490 VM_OBJECT_WUNLOCK(obj);
2495 * Charge for the whole object allocation now, since
2496 * we cannot distinguish between non-charged and
2497 * charged clipped mapping of the same object later.
2499 KASSERT(obj->charge == 0,
2500 ("vm_map_protect: object %p overcharged (entry %p)",
2502 if (!swap_reserve(ptoa(obj->size))) {
2503 VM_OBJECT_WUNLOCK(obj);
2505 return (KERN_RESOURCE_SHORTAGE);
2510 obj->charge = ptoa(obj->size);
2511 VM_OBJECT_WUNLOCK(obj);
2515 * Go back and fix up protections. [Note that clipping is not
2516 * necessary the second time.]
2518 for (current = entry; current->start < end; current = current->next) {
2519 if ((current->eflags & MAP_ENTRY_GUARD) != 0)
2522 old_prot = current->protection;
2525 current->protection =
2526 (current->max_protection = new_prot) &
2529 current->protection = new_prot;
2532 * For user wired map entries, the normal lazy evaluation of
2533 * write access upgrades through soft page faults is
2534 * undesirable. Instead, immediately copy any pages that are
2535 * copy-on-write and enable write access in the physical map.
2537 if ((current->eflags & MAP_ENTRY_USER_WIRED) != 0 &&
2538 (current->protection & VM_PROT_WRITE) != 0 &&
2539 (old_prot & VM_PROT_WRITE) == 0)
2540 vm_fault_copy_entry(map, map, current, current, NULL);
2543 * When restricting access, update the physical map. Worry
2544 * about copy-on-write here.
2546 if ((old_prot & ~current->protection) != 0) {
2547 #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
2549 pmap_protect(map->pmap, current->start,
2551 current->protection & MASK(current));
2554 vm_map_simplify_entry(map, current);
2557 return (KERN_SUCCESS);
2563 * This routine traverses a processes map handling the madvise
2564 * system call. Advisories are classified as either those effecting
2565 * the vm_map_entry structure, or those effecting the underlying
2575 vm_map_entry_t current, entry;
2579 * Some madvise calls directly modify the vm_map_entry, in which case
2580 * we need to use an exclusive lock on the map and we need to perform
2581 * various clipping operations. Otherwise we only need a read-lock
2586 case MADV_SEQUENTIAL:
2603 vm_map_lock_read(map);
2610 * Locate starting entry and clip if necessary.
2612 VM_MAP_RANGE_CHECK(map, start, end);
2614 if (vm_map_lookup_entry(map, start, &entry)) {
2616 vm_map_clip_start(map, entry, start);
2618 entry = entry->next;
2623 * madvise behaviors that are implemented in the vm_map_entry.
2625 * We clip the vm_map_entry so that behavioral changes are
2626 * limited to the specified address range.
2628 for (current = entry; current->start < end;
2629 current = current->next) {
2630 if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
2633 vm_map_clip_end(map, current, end);
2637 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
2639 case MADV_SEQUENTIAL:
2640 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
2643 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
2646 current->eflags |= MAP_ENTRY_NOSYNC;
2649 current->eflags &= ~MAP_ENTRY_NOSYNC;
2652 current->eflags |= MAP_ENTRY_NOCOREDUMP;
2655 current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
2660 vm_map_simplify_entry(map, current);
2664 vm_pindex_t pstart, pend;
2667 * madvise behaviors that are implemented in the underlying
2670 * Since we don't clip the vm_map_entry, we have to clip
2671 * the vm_object pindex and count.
2673 for (current = entry; current->start < end;
2674 current = current->next) {
2675 vm_offset_t useEnd, useStart;
2677 if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
2680 pstart = OFF_TO_IDX(current->offset);
2681 pend = pstart + atop(current->end - current->start);
2682 useStart = current->start;
2683 useEnd = current->end;
2685 if (current->start < start) {
2686 pstart += atop(start - current->start);
2689 if (current->end > end) {
2690 pend -= atop(current->end - end);
2698 * Perform the pmap_advise() before clearing
2699 * PGA_REFERENCED in vm_page_advise(). Otherwise, a
2700 * concurrent pmap operation, such as pmap_remove(),
2701 * could clear a reference in the pmap and set
2702 * PGA_REFERENCED on the page before the pmap_advise()
2703 * had completed. Consequently, the page would appear
2704 * referenced based upon an old reference that
2705 * occurred before this pmap_advise() ran.
2707 if (behav == MADV_DONTNEED || behav == MADV_FREE)
2708 pmap_advise(map->pmap, useStart, useEnd,
2711 vm_object_madvise(current->object.vm_object, pstart,
2715 * Pre-populate paging structures in the
2716 * WILLNEED case. For wired entries, the
2717 * paging structures are already populated.
2719 if (behav == MADV_WILLNEED &&
2720 current->wired_count == 0) {
2721 vm_map_pmap_enter(map,
2723 current->protection,
2724 current->object.vm_object,
2726 ptoa(pend - pstart),
2727 MAP_PREFAULT_MADVISE
2731 vm_map_unlock_read(map);
2740 * Sets the inheritance of the specified address
2741 * range in the target map. Inheritance
2742 * affects how the map will be shared with
2743 * child maps at the time of vmspace_fork.
2746 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
2747 vm_inherit_t new_inheritance)
2749 vm_map_entry_t entry;
2750 vm_map_entry_t temp_entry;
2752 switch (new_inheritance) {
2753 case VM_INHERIT_NONE:
2754 case VM_INHERIT_COPY:
2755 case VM_INHERIT_SHARE:
2756 case VM_INHERIT_ZERO:
2759 return (KERN_INVALID_ARGUMENT);
2762 return (KERN_SUCCESS);
2764 VM_MAP_RANGE_CHECK(map, start, end);
2765 if (vm_map_lookup_entry(map, start, &temp_entry)) {
2767 vm_map_clip_start(map, entry, start);
2769 entry = temp_entry->next;
2770 while (entry->start < end) {
2771 vm_map_clip_end(map, entry, end);
2772 if ((entry->eflags & MAP_ENTRY_GUARD) == 0 ||
2773 new_inheritance != VM_INHERIT_ZERO)
2774 entry->inheritance = new_inheritance;
2775 vm_map_simplify_entry(map, entry);
2776 entry = entry->next;
2779 return (KERN_SUCCESS);
2785 * Implements both kernel and user unwiring.
2788 vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
2791 vm_map_entry_t entry, first_entry, tmp_entry;
2792 vm_offset_t saved_start;
2793 unsigned int last_timestamp;
2795 boolean_t need_wakeup, result, user_unwire;
2798 return (KERN_SUCCESS);
2799 user_unwire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
2801 VM_MAP_RANGE_CHECK(map, start, end);
2802 if (!vm_map_lookup_entry(map, start, &first_entry)) {
2803 if (flags & VM_MAP_WIRE_HOLESOK)
2804 first_entry = first_entry->next;
2807 return (KERN_INVALID_ADDRESS);
2810 last_timestamp = map->timestamp;
2811 entry = first_entry;
2812 while (entry->start < end) {
2813 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
2815 * We have not yet clipped the entry.
2817 saved_start = (start >= entry->start) ? start :
2819 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2820 if (vm_map_unlock_and_wait(map, 0)) {
2822 * Allow interruption of user unwiring?
2826 if (last_timestamp+1 != map->timestamp) {
2828 * Look again for the entry because the map was
2829 * modified while it was unlocked.
2830 * Specifically, the entry may have been
2831 * clipped, merged, or deleted.
2833 if (!vm_map_lookup_entry(map, saved_start,
2835 if (flags & VM_MAP_WIRE_HOLESOK)
2836 tmp_entry = tmp_entry->next;
2838 if (saved_start == start) {
2840 * First_entry has been deleted.
2843 return (KERN_INVALID_ADDRESS);
2846 rv = KERN_INVALID_ADDRESS;
2850 if (entry == first_entry)
2851 first_entry = tmp_entry;
2856 last_timestamp = map->timestamp;
2859 vm_map_clip_start(map, entry, start);
2860 vm_map_clip_end(map, entry, end);
2862 * Mark the entry in case the map lock is released. (See
2865 KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 &&
2866 entry->wiring_thread == NULL,
2867 ("owned map entry %p", entry));
2868 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
2869 entry->wiring_thread = curthread;
2871 * Check the map for holes in the specified region.
2872 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
2874 if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
2875 (entry->end < end && entry->next->start > entry->end)) {
2877 rv = KERN_INVALID_ADDRESS;
2881 * If system unwiring, require that the entry is system wired.
2884 vm_map_entry_system_wired_count(entry) == 0) {
2886 rv = KERN_INVALID_ARGUMENT;
2889 entry = entry->next;
2893 need_wakeup = FALSE;
2894 if (first_entry == NULL) {
2895 result = vm_map_lookup_entry(map, start, &first_entry);
2896 if (!result && (flags & VM_MAP_WIRE_HOLESOK))
2897 first_entry = first_entry->next;
2899 KASSERT(result, ("vm_map_unwire: lookup failed"));
2901 for (entry = first_entry; entry->start < end; entry = entry->next) {
2903 * If VM_MAP_WIRE_HOLESOK was specified, an empty
2904 * space in the unwired region could have been mapped
2905 * while the map lock was dropped for draining
2906 * MAP_ENTRY_IN_TRANSITION. Moreover, another thread
2907 * could be simultaneously wiring this new mapping
2908 * entry. Detect these cases and skip any entries
2909 * marked as in transition by us.
2911 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 ||
2912 entry->wiring_thread != curthread) {
2913 KASSERT((flags & VM_MAP_WIRE_HOLESOK) != 0,
2914 ("vm_map_unwire: !HOLESOK and new/changed entry"));
2918 if (rv == KERN_SUCCESS && (!user_unwire ||
2919 (entry->eflags & MAP_ENTRY_USER_WIRED))) {
2921 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2922 if (entry->wired_count == 1)
2923 vm_map_entry_unwire(map, entry);
2925 entry->wired_count--;
2927 KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0,
2928 ("vm_map_unwire: in-transition flag missing %p", entry));
2929 KASSERT(entry->wiring_thread == curthread,
2930 ("vm_map_unwire: alien wire %p", entry));
2931 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
2932 entry->wiring_thread = NULL;
2933 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
2934 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
2937 vm_map_simplify_entry(map, entry);
2946 * vm_map_wire_entry_failure:
2948 * Handle a wiring failure on the given entry.
2950 * The map should be locked.
2953 vm_map_wire_entry_failure(vm_map_t map, vm_map_entry_t entry,
2954 vm_offset_t failed_addr)
2957 VM_MAP_ASSERT_LOCKED(map);
2958 KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 &&
2959 entry->wired_count == 1,
2960 ("vm_map_wire_entry_failure: entry %p isn't being wired", entry));
2961 KASSERT(failed_addr < entry->end,
2962 ("vm_map_wire_entry_failure: entry %p was fully wired", entry));
2965 * If any pages at the start of this entry were successfully wired,
2968 if (failed_addr > entry->start) {
2969 pmap_unwire(map->pmap, entry->start, failed_addr);
2970 vm_object_unwire(entry->object.vm_object, entry->offset,
2971 failed_addr - entry->start, PQ_ACTIVE);
2975 * Assign an out-of-range value to represent the failure to wire this
2978 entry->wired_count = -1;
2984 * Implements both kernel and user wiring.
2987 vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
2990 vm_map_entry_t entry, first_entry, tmp_entry;
2991 vm_offset_t faddr, saved_end, saved_start;
2992 unsigned int last_timestamp;
2994 boolean_t need_wakeup, result, user_wire;
2998 return (KERN_SUCCESS);
3000 if (flags & VM_MAP_WIRE_WRITE)
3001 prot |= VM_PROT_WRITE;
3002 user_wire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
3004 VM_MAP_RANGE_CHECK(map, start, end);
3005 if (!vm_map_lookup_entry(map, start, &first_entry)) {
3006 if (flags & VM_MAP_WIRE_HOLESOK)
3007 first_entry = first_entry->next;
3010 return (KERN_INVALID_ADDRESS);
3013 last_timestamp = map->timestamp;
3014 entry = first_entry;
3015 while (entry->start < end) {
3016 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
3018 * We have not yet clipped the entry.
3020 saved_start = (start >= entry->start) ? start :
3022 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
3023 if (vm_map_unlock_and_wait(map, 0)) {
3025 * Allow interruption of user wiring?
3029 if (last_timestamp + 1 != map->timestamp) {
3031 * Look again for the entry because the map was
3032 * modified while it was unlocked.
3033 * Specifically, the entry may have been
3034 * clipped, merged, or deleted.
3036 if (!vm_map_lookup_entry(map, saved_start,
3038 if (flags & VM_MAP_WIRE_HOLESOK)
3039 tmp_entry = tmp_entry->next;
3041 if (saved_start == start) {
3043 * first_entry has been deleted.
3046 return (KERN_INVALID_ADDRESS);
3049 rv = KERN_INVALID_ADDRESS;
3053 if (entry == first_entry)
3054 first_entry = tmp_entry;
3059 last_timestamp = map->timestamp;
3062 vm_map_clip_start(map, entry, start);
3063 vm_map_clip_end(map, entry, end);
3065 * Mark the entry in case the map lock is released. (See
3068 KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 &&
3069 entry->wiring_thread == NULL,
3070 ("owned map entry %p", entry));
3071 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
3072 entry->wiring_thread = curthread;
3073 if ((entry->protection & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0
3074 || (entry->protection & prot) != prot) {
3075 entry->eflags |= MAP_ENTRY_WIRE_SKIPPED;
3076 if ((flags & VM_MAP_WIRE_HOLESOK) == 0) {
3078 rv = KERN_INVALID_ADDRESS;
3083 if (entry->wired_count == 0) {
3084 entry->wired_count++;
3085 saved_start = entry->start;
3086 saved_end = entry->end;
3089 * Release the map lock, relying on the in-transition
3090 * mark. Mark the map busy for fork.
3095 faddr = saved_start;
3098 * Simulate a fault to get the page and enter
3099 * it into the physical map.
3101 if ((rv = vm_fault(map, faddr, VM_PROT_NONE,
3102 VM_FAULT_WIRE)) != KERN_SUCCESS)
3104 } while ((faddr += PAGE_SIZE) < saved_end);
3107 if (last_timestamp + 1 != map->timestamp) {
3109 * Look again for the entry because the map was
3110 * modified while it was unlocked. The entry
3111 * may have been clipped, but NOT merged or
3114 result = vm_map_lookup_entry(map, saved_start,
3116 KASSERT(result, ("vm_map_wire: lookup failed"));
3117 if (entry == first_entry)
3118 first_entry = tmp_entry;
3122 while (entry->end < saved_end) {
3124 * In case of failure, handle entries
3125 * that were not fully wired here;
3126 * fully wired entries are handled
3129 if (rv != KERN_SUCCESS &&
3131 vm_map_wire_entry_failure(map,
3133 entry = entry->next;
3136 last_timestamp = map->timestamp;
3137 if (rv != KERN_SUCCESS) {
3138 vm_map_wire_entry_failure(map, entry, faddr);
3142 } else if (!user_wire ||
3143 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
3144 entry->wired_count++;
3147 * Check the map for holes in the specified region.
3148 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
3151 if ((flags & VM_MAP_WIRE_HOLESOK) == 0 &&
3152 entry->end < end && entry->next->start > entry->end) {
3154 rv = KERN_INVALID_ADDRESS;
3157 entry = entry->next;
3161 need_wakeup = FALSE;
3162 if (first_entry == NULL) {
3163 result = vm_map_lookup_entry(map, start, &first_entry);
3164 if (!result && (flags & VM_MAP_WIRE_HOLESOK))
3165 first_entry = first_entry->next;
3167 KASSERT(result, ("vm_map_wire: lookup failed"));
3169 for (entry = first_entry; entry->start < end; entry = entry->next) {
3171 * If VM_MAP_WIRE_HOLESOK was specified, an empty
3172 * space in the unwired region could have been mapped
3173 * while the map lock was dropped for faulting in the
3174 * pages or draining MAP_ENTRY_IN_TRANSITION.
3175 * Moreover, another thread could be simultaneously
3176 * wiring this new mapping entry. Detect these cases
3177 * and skip any entries marked as in transition not by us.
3179 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 ||
3180 entry->wiring_thread != curthread) {
3181 KASSERT((flags & VM_MAP_WIRE_HOLESOK) != 0,
3182 ("vm_map_wire: !HOLESOK and new/changed entry"));
3186 if ((entry->eflags & MAP_ENTRY_WIRE_SKIPPED) != 0)
3187 goto next_entry_done;
3189 if (rv == KERN_SUCCESS) {
3191 entry->eflags |= MAP_ENTRY_USER_WIRED;
3192 } else if (entry->wired_count == -1) {
3194 * Wiring failed on this entry. Thus, unwiring is
3197 entry->wired_count = 0;
3198 } else if (!user_wire ||
3199 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
3201 * Undo the wiring. Wiring succeeded on this entry
3202 * but failed on a later entry.
3204 if (entry->wired_count == 1)
3205 vm_map_entry_unwire(map, entry);
3207 entry->wired_count--;
3210 KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0,
3211 ("vm_map_wire: in-transition flag missing %p", entry));
3212 KASSERT(entry->wiring_thread == curthread,
3213 ("vm_map_wire: alien wire %p", entry));
3214 entry->eflags &= ~(MAP_ENTRY_IN_TRANSITION |
3215 MAP_ENTRY_WIRE_SKIPPED);
3216 entry->wiring_thread = NULL;
3217 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
3218 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
3221 vm_map_simplify_entry(map, entry);
3232 * Push any dirty cached pages in the address range to their pager.
3233 * If syncio is TRUE, dirty pages are written synchronously.
3234 * If invalidate is TRUE, any cached pages are freed as well.
3236 * If the size of the region from start to end is zero, we are
3237 * supposed to flush all modified pages within the region containing
3238 * start. Unfortunately, a region can be split or coalesced with
3239 * neighboring regions, making it difficult to determine what the
3240 * original region was. Therefore, we approximate this requirement by
3241 * flushing the current region containing start.
3243 * Returns an error if any part of the specified range is not mapped.
3251 boolean_t invalidate)
3253 vm_map_entry_t current;
3254 vm_map_entry_t entry;
3257 vm_ooffset_t offset;
3258 unsigned int last_timestamp;
3261 vm_map_lock_read(map);
3262 VM_MAP_RANGE_CHECK(map, start, end);
3263 if (!vm_map_lookup_entry(map, start, &entry)) {
3264 vm_map_unlock_read(map);
3265 return (KERN_INVALID_ADDRESS);
3266 } else if (start == end) {
3267 start = entry->start;
3271 * Make a first pass to check for user-wired memory and holes.
3273 for (current = entry; current->start < end; current = current->next) {
3274 if (invalidate && (current->eflags & MAP_ENTRY_USER_WIRED)) {
3275 vm_map_unlock_read(map);
3276 return (KERN_INVALID_ARGUMENT);
3278 if (end > current->end &&
3279 current->end != current->next->start) {
3280 vm_map_unlock_read(map);
3281 return (KERN_INVALID_ADDRESS);
3286 pmap_remove(map->pmap, start, end);
3290 * Make a second pass, cleaning/uncaching pages from the indicated
3293 for (current = entry; current->start < end;) {
3294 offset = current->offset + (start - current->start);
3295 size = (end <= current->end ? end : current->end) - start;
3296 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
3298 vm_map_entry_t tentry;
3301 smap = current->object.sub_map;
3302 vm_map_lock_read(smap);
3303 (void) vm_map_lookup_entry(smap, offset, &tentry);
3304 tsize = tentry->end - offset;
3307 object = tentry->object.vm_object;
3308 offset = tentry->offset + (offset - tentry->start);
3309 vm_map_unlock_read(smap);
3311 object = current->object.vm_object;
3313 vm_object_reference(object);
3314 last_timestamp = map->timestamp;
3315 vm_map_unlock_read(map);
3316 if (!vm_object_sync(object, offset, size, syncio, invalidate))
3319 vm_object_deallocate(object);
3320 vm_map_lock_read(map);
3321 if (last_timestamp == map->timestamp ||
3322 !vm_map_lookup_entry(map, start, ¤t))
3323 current = current->next;
3326 vm_map_unlock_read(map);
3327 return (failed ? KERN_FAILURE : KERN_SUCCESS);
3331 * vm_map_entry_unwire: [ internal use only ]
3333 * Make the region specified by this entry pageable.
3335 * The map in question should be locked.
3336 * [This is the reason for this routine's existence.]
3339 vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
3342 VM_MAP_ASSERT_LOCKED(map);
3343 KASSERT(entry->wired_count > 0,
3344 ("vm_map_entry_unwire: entry %p isn't wired", entry));
3345 pmap_unwire(map->pmap, entry->start, entry->end);
3346 vm_object_unwire(entry->object.vm_object, entry->offset, entry->end -
3347 entry->start, PQ_ACTIVE);
3348 entry->wired_count = 0;
3352 vm_map_entry_deallocate(vm_map_entry_t entry, boolean_t system_map)
3355 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0)
3356 vm_object_deallocate(entry->object.vm_object);
3357 uma_zfree(system_map ? kmapentzone : mapentzone, entry);
3361 * vm_map_entry_delete: [ internal use only ]
3363 * Deallocate the given entry from the target map.
3366 vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry)
3369 vm_pindex_t offidxstart, offidxend, count, size1;
3372 vm_map_entry_unlink(map, entry, UNLINK_MERGE_NONE);
3373 object = entry->object.vm_object;
3375 if ((entry->eflags & MAP_ENTRY_GUARD) != 0) {
3376 MPASS(entry->cred == NULL);
3377 MPASS((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0);
3378 MPASS(object == NULL);
3379 vm_map_entry_deallocate(entry, map->system_map);
3383 size = entry->end - entry->start;
3386 if (entry->cred != NULL) {
3387 swap_release_by_cred(size, entry->cred);
3388 crfree(entry->cred);
3391 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
3393 KASSERT(entry->cred == NULL || object->cred == NULL ||
3394 (entry->eflags & MAP_ENTRY_NEEDS_COPY),
3395 ("OVERCOMMIT vm_map_entry_delete: both cred %p", entry));
3397 offidxstart = OFF_TO_IDX(entry->offset);
3398 offidxend = offidxstart + count;
3399 VM_OBJECT_WLOCK(object);
3400 if (object->ref_count != 1 && ((object->flags & (OBJ_NOSPLIT |
3401 OBJ_ONEMAPPING)) == OBJ_ONEMAPPING ||
3402 object == kernel_object)) {
3403 vm_object_collapse(object);
3406 * The option OBJPR_NOTMAPPED can be passed here
3407 * because vm_map_delete() already performed
3408 * pmap_remove() on the only mapping to this range
3411 vm_object_page_remove(object, offidxstart, offidxend,
3413 if (object->type == OBJT_SWAP)
3414 swap_pager_freespace(object, offidxstart,
3416 if (offidxend >= object->size &&
3417 offidxstart < object->size) {
3418 size1 = object->size;
3419 object->size = offidxstart;
3420 if (object->cred != NULL) {
3421 size1 -= object->size;
3422 KASSERT(object->charge >= ptoa(size1),
3423 ("object %p charge < 0", object));
3424 swap_release_by_cred(ptoa(size1),
3426 object->charge -= ptoa(size1);
3430 VM_OBJECT_WUNLOCK(object);
3432 entry->object.vm_object = NULL;
3433 if (map->system_map)
3434 vm_map_entry_deallocate(entry, TRUE);
3436 entry->next = curthread->td_map_def_user;
3437 curthread->td_map_def_user = entry;
3442 * vm_map_delete: [ internal use only ]
3444 * Deallocates the given address range from the target
3448 vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end)
3450 vm_map_entry_t entry;
3451 vm_map_entry_t first_entry;
3453 VM_MAP_ASSERT_LOCKED(map);
3455 return (KERN_SUCCESS);
3458 * Find the start of the region, and clip it
3460 if (!vm_map_lookup_entry(map, start, &first_entry))
3461 entry = first_entry->next;
3463 entry = first_entry;
3464 vm_map_clip_start(map, entry, start);
3468 * Step through all entries in this region
3470 while (entry->start < end) {
3471 vm_map_entry_t next;
3474 * Wait for wiring or unwiring of an entry to complete.
3475 * Also wait for any system wirings to disappear on
3478 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 ||
3479 (vm_map_pmap(map) != kernel_pmap &&
3480 vm_map_entry_system_wired_count(entry) != 0)) {
3481 unsigned int last_timestamp;
3482 vm_offset_t saved_start;
3483 vm_map_entry_t tmp_entry;
3485 saved_start = entry->start;
3486 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
3487 last_timestamp = map->timestamp;
3488 (void) vm_map_unlock_and_wait(map, 0);
3490 if (last_timestamp + 1 != map->timestamp) {
3492 * Look again for the entry because the map was
3493 * modified while it was unlocked.
3494 * Specifically, the entry may have been
3495 * clipped, merged, or deleted.
3497 if (!vm_map_lookup_entry(map, saved_start,
3499 entry = tmp_entry->next;
3502 vm_map_clip_start(map, entry,
3508 vm_map_clip_end(map, entry, end);
3513 * Unwire before removing addresses from the pmap; otherwise,
3514 * unwiring will put the entries back in the pmap.
3516 if (entry->wired_count != 0)
3517 vm_map_entry_unwire(map, entry);
3520 * Remove mappings for the pages, but only if the
3521 * mappings could exist. For instance, it does not
3522 * make sense to call pmap_remove() for guard entries.
3524 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0 ||
3525 entry->object.vm_object != NULL)
3526 pmap_remove(map->pmap, entry->start, entry->end);
3528 if (entry->end == map->anon_loc)
3529 map->anon_loc = entry->start;
3532 * Delete the entry only after removing all pmap
3533 * entries pointing to its pages. (Otherwise, its
3534 * page frames may be reallocated, and any modify bits
3535 * will be set in the wrong object!)
3537 vm_map_entry_delete(map, entry);
3540 return (KERN_SUCCESS);
3546 * Remove the given address range from the target map.
3547 * This is the exported form of vm_map_delete.
3550 vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
3555 VM_MAP_RANGE_CHECK(map, start, end);
3556 result = vm_map_delete(map, start, end);
3562 * vm_map_check_protection:
3564 * Assert that the target map allows the specified privilege on the
3565 * entire address region given. The entire region must be allocated.
3567 * WARNING! This code does not and should not check whether the
3568 * contents of the region is accessible. For example a smaller file
3569 * might be mapped into a larger address space.
3571 * NOTE! This code is also called by munmap().
3573 * The map must be locked. A read lock is sufficient.
3576 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
3577 vm_prot_t protection)
3579 vm_map_entry_t entry;
3580 vm_map_entry_t tmp_entry;
3582 if (!vm_map_lookup_entry(map, start, &tmp_entry))
3586 while (start < end) {
3590 if (start < entry->start)
3593 * Check protection associated with entry.
3595 if ((entry->protection & protection) != protection)
3597 /* go to next entry */
3599 entry = entry->next;
3605 * vm_map_copy_entry:
3607 * Copies the contents of the source entry to the destination
3608 * entry. The entries *must* be aligned properly.
3614 vm_map_entry_t src_entry,
3615 vm_map_entry_t dst_entry,
3616 vm_ooffset_t *fork_charge)
3618 vm_object_t src_object;
3619 vm_map_entry_t fake_entry;
3624 VM_MAP_ASSERT_LOCKED(dst_map);
3626 if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
3629 if (src_entry->wired_count == 0 ||
3630 (src_entry->protection & VM_PROT_WRITE) == 0) {
3632 * If the source entry is marked needs_copy, it is already
3635 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0 &&
3636 (src_entry->protection & VM_PROT_WRITE) != 0) {
3637 pmap_protect(src_map->pmap,
3640 src_entry->protection & ~VM_PROT_WRITE);
3644 * Make a copy of the object.
3646 size = src_entry->end - src_entry->start;
3647 if ((src_object = src_entry->object.vm_object) != NULL) {
3648 VM_OBJECT_WLOCK(src_object);
3649 charged = ENTRY_CHARGED(src_entry);
3650 if (src_object->handle == NULL &&
3651 (src_object->type == OBJT_DEFAULT ||
3652 src_object->type == OBJT_SWAP)) {
3653 vm_object_collapse(src_object);
3654 if ((src_object->flags & (OBJ_NOSPLIT |
3655 OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
3656 vm_object_split(src_entry);
3658 src_entry->object.vm_object;
3661 vm_object_reference_locked(src_object);
3662 vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
3663 if (src_entry->cred != NULL &&
3664 !(src_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
3665 KASSERT(src_object->cred == NULL,
3666 ("OVERCOMMIT: vm_map_copy_entry: cred %p",
3668 src_object->cred = src_entry->cred;
3669 src_object->charge = size;
3671 VM_OBJECT_WUNLOCK(src_object);
3672 dst_entry->object.vm_object = src_object;
3674 cred = curthread->td_ucred;
3676 dst_entry->cred = cred;
3677 *fork_charge += size;
3678 if (!(src_entry->eflags &
3679 MAP_ENTRY_NEEDS_COPY)) {
3681 src_entry->cred = cred;
3682 *fork_charge += size;
3685 src_entry->eflags |= MAP_ENTRY_COW |
3686 MAP_ENTRY_NEEDS_COPY;
3687 dst_entry->eflags |= MAP_ENTRY_COW |
3688 MAP_ENTRY_NEEDS_COPY;
3689 dst_entry->offset = src_entry->offset;
3690 if (src_entry->eflags & MAP_ENTRY_VN_WRITECNT) {
3692 * MAP_ENTRY_VN_WRITECNT cannot
3693 * indicate write reference from
3694 * src_entry, since the entry is
3695 * marked as needs copy. Allocate a
3696 * fake entry that is used to
3697 * decrement object->un_pager.vnp.writecount
3698 * at the appropriate time. Attach
3699 * fake_entry to the deferred list.
3701 fake_entry = vm_map_entry_create(dst_map);
3702 fake_entry->eflags = MAP_ENTRY_VN_WRITECNT;
3703 src_entry->eflags &= ~MAP_ENTRY_VN_WRITECNT;
3704 vm_object_reference(src_object);
3705 fake_entry->object.vm_object = src_object;
3706 fake_entry->start = src_entry->start;
3707 fake_entry->end = src_entry->end;
3708 fake_entry->next = curthread->td_map_def_user;
3709 curthread->td_map_def_user = fake_entry;
3712 pmap_copy(dst_map->pmap, src_map->pmap,
3713 dst_entry->start, dst_entry->end - dst_entry->start,
3716 dst_entry->object.vm_object = NULL;
3717 dst_entry->offset = 0;
3718 if (src_entry->cred != NULL) {
3719 dst_entry->cred = curthread->td_ucred;
3720 crhold(dst_entry->cred);
3721 *fork_charge += size;
3726 * We don't want to make writeable wired pages copy-on-write.
3727 * Immediately copy these pages into the new map by simulating
3728 * page faults. The new pages are pageable.
3730 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry,
3736 * vmspace_map_entry_forked:
3737 * Update the newly-forked vmspace each time a map entry is inherited
3738 * or copied. The values for vm_dsize and vm_tsize are approximate
3739 * (and mostly-obsolete ideas in the face of mmap(2) et al.)
3742 vmspace_map_entry_forked(const struct vmspace *vm1, struct vmspace *vm2,
3743 vm_map_entry_t entry)
3745 vm_size_t entrysize;
3748 if ((entry->eflags & MAP_ENTRY_GUARD) != 0)
3750 entrysize = entry->end - entry->start;
3751 vm2->vm_map.size += entrysize;
3752 if (entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP)) {
3753 vm2->vm_ssize += btoc(entrysize);
3754 } else if (entry->start >= (vm_offset_t)vm1->vm_daddr &&
3755 entry->start < (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)) {
3756 newend = MIN(entry->end,
3757 (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize));
3758 vm2->vm_dsize += btoc(newend - entry->start);
3759 } else if (entry->start >= (vm_offset_t)vm1->vm_taddr &&
3760 entry->start < (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)) {
3761 newend = MIN(entry->end,
3762 (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize));
3763 vm2->vm_tsize += btoc(newend - entry->start);
3769 * Create a new process vmspace structure and vm_map
3770 * based on those of an existing process. The new map
3771 * is based on the old map, according to the inheritance
3772 * values on the regions in that map.
3774 * XXX It might be worth coalescing the entries added to the new vmspace.
3776 * The source map must not be locked.
3779 vmspace_fork(struct vmspace *vm1, vm_ooffset_t *fork_charge)
3781 struct vmspace *vm2;
3782 vm_map_t new_map, old_map;
3783 vm_map_entry_t new_entry, old_entry;
3788 old_map = &vm1->vm_map;
3789 /* Copy immutable fields of vm1 to vm2. */
3790 vm2 = vmspace_alloc(vm_map_min(old_map), vm_map_max(old_map),
3795 vm2->vm_taddr = vm1->vm_taddr;
3796 vm2->vm_daddr = vm1->vm_daddr;
3797 vm2->vm_maxsaddr = vm1->vm_maxsaddr;
3798 vm_map_lock(old_map);
3800 vm_map_wait_busy(old_map);
3801 new_map = &vm2->vm_map;
3802 locked = vm_map_trylock(new_map); /* trylock to silence WITNESS */
3803 KASSERT(locked, ("vmspace_fork: lock failed"));
3805 error = pmap_vmspace_copy(new_map->pmap, old_map->pmap);
3807 sx_xunlock(&old_map->lock);
3808 sx_xunlock(&new_map->lock);
3809 vm_map_process_deferred();
3814 new_map->anon_loc = old_map->anon_loc;
3816 old_entry = old_map->header.next;
3818 while (old_entry != &old_map->header) {
3819 if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
3820 panic("vm_map_fork: encountered a submap");
3822 inh = old_entry->inheritance;
3823 if ((old_entry->eflags & MAP_ENTRY_GUARD) != 0 &&
3824 inh != VM_INHERIT_NONE)
3825 inh = VM_INHERIT_COPY;
3828 case VM_INHERIT_NONE:
3831 case VM_INHERIT_SHARE:
3833 * Clone the entry, creating the shared object if necessary.
3835 object = old_entry->object.vm_object;
3836 if (object == NULL) {
3837 object = vm_object_allocate(OBJT_DEFAULT,
3838 atop(old_entry->end - old_entry->start));
3839 old_entry->object.vm_object = object;
3840 old_entry->offset = 0;
3841 if (old_entry->cred != NULL) {
3842 object->cred = old_entry->cred;
3843 object->charge = old_entry->end -
3845 old_entry->cred = NULL;
3850 * Add the reference before calling vm_object_shadow
3851 * to insure that a shadow object is created.
3853 vm_object_reference(object);
3854 if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3855 vm_object_shadow(&old_entry->object.vm_object,
3857 old_entry->end - old_entry->start);
3858 old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
3859 /* Transfer the second reference too. */
3860 vm_object_reference(
3861 old_entry->object.vm_object);
3864 * As in vm_map_simplify_entry(), the
3865 * vnode lock will not be acquired in
3866 * this call to vm_object_deallocate().
3868 vm_object_deallocate(object);
3869 object = old_entry->object.vm_object;
3871 VM_OBJECT_WLOCK(object);
3872 vm_object_clear_flag(object, OBJ_ONEMAPPING);
3873 if (old_entry->cred != NULL) {
3874 KASSERT(object->cred == NULL, ("vmspace_fork both cred"));
3875 object->cred = old_entry->cred;
3876 object->charge = old_entry->end - old_entry->start;
3877 old_entry->cred = NULL;
3881 * Assert the correct state of the vnode
3882 * v_writecount while the object is locked, to
3883 * not relock it later for the assertion
3886 if (old_entry->eflags & MAP_ENTRY_VN_WRITECNT &&
3887 object->type == OBJT_VNODE) {
3888 KASSERT(((struct vnode *)object->handle)->
3890 ("vmspace_fork: v_writecount %p", object));
3891 KASSERT(object->un_pager.vnp.writemappings > 0,
3892 ("vmspace_fork: vnp.writecount %p",
3895 VM_OBJECT_WUNLOCK(object);
3898 * Clone the entry, referencing the shared object.
3900 new_entry = vm_map_entry_create(new_map);
3901 *new_entry = *old_entry;
3902 new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
3903 MAP_ENTRY_IN_TRANSITION);
3904 new_entry->wiring_thread = NULL;
3905 new_entry->wired_count = 0;
3906 if (new_entry->eflags & MAP_ENTRY_VN_WRITECNT) {
3907 vnode_pager_update_writecount(object,
3908 new_entry->start, new_entry->end);
3910 vm_map_entry_set_vnode_text(new_entry, true);
3913 * Insert the entry into the new map -- we know we're
3914 * inserting at the end of the new map.
3916 vm_map_entry_link(new_map, new_entry);
3917 vmspace_map_entry_forked(vm1, vm2, new_entry);
3920 * Update the physical map
3922 pmap_copy(new_map->pmap, old_map->pmap,
3924 (old_entry->end - old_entry->start),
3928 case VM_INHERIT_COPY:
3930 * Clone the entry and link into the map.
3932 new_entry = vm_map_entry_create(new_map);
3933 *new_entry = *old_entry;
3935 * Copied entry is COW over the old object.
3937 new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
3938 MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_VN_WRITECNT);
3939 new_entry->wiring_thread = NULL;
3940 new_entry->wired_count = 0;
3941 new_entry->object.vm_object = NULL;
3942 new_entry->cred = NULL;
3943 vm_map_entry_link(new_map, new_entry);
3944 vmspace_map_entry_forked(vm1, vm2, new_entry);
3945 vm_map_copy_entry(old_map, new_map, old_entry,
3946 new_entry, fork_charge);
3947 vm_map_entry_set_vnode_text(new_entry, true);
3950 case VM_INHERIT_ZERO:
3952 * Create a new anonymous mapping entry modelled from
3955 new_entry = vm_map_entry_create(new_map);
3956 memset(new_entry, 0, sizeof(*new_entry));
3958 new_entry->start = old_entry->start;
3959 new_entry->end = old_entry->end;
3960 new_entry->eflags = old_entry->eflags &
3961 ~(MAP_ENTRY_USER_WIRED | MAP_ENTRY_IN_TRANSITION |
3962 MAP_ENTRY_VN_WRITECNT | MAP_ENTRY_VN_EXEC);
3963 new_entry->protection = old_entry->protection;
3964 new_entry->max_protection = old_entry->max_protection;
3965 new_entry->inheritance = VM_INHERIT_ZERO;
3967 vm_map_entry_link(new_map, new_entry);
3968 vmspace_map_entry_forked(vm1, vm2, new_entry);
3970 new_entry->cred = curthread->td_ucred;
3971 crhold(new_entry->cred);
3972 *fork_charge += (new_entry->end - new_entry->start);
3976 old_entry = old_entry->next;
3979 * Use inlined vm_map_unlock() to postpone handling the deferred
3980 * map entries, which cannot be done until both old_map and
3981 * new_map locks are released.
3983 sx_xunlock(&old_map->lock);
3984 sx_xunlock(&new_map->lock);
3985 vm_map_process_deferred();
3991 * Create a process's stack for exec_new_vmspace(). This function is never
3992 * asked to wire the newly created stack.
3995 vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
3996 vm_prot_t prot, vm_prot_t max, int cow)
3998 vm_size_t growsize, init_ssize;
4002 MPASS((map->flags & MAP_WIREFUTURE) == 0);
4003 growsize = sgrowsiz;
4004 init_ssize = (max_ssize < growsize) ? max_ssize : growsize;
4006 vmemlim = lim_cur(curthread, RLIMIT_VMEM);
4007 /* If we would blow our VMEM resource limit, no go */
4008 if (map->size + init_ssize > vmemlim) {
4012 rv = vm_map_stack_locked(map, addrbos, max_ssize, growsize, prot,
4019 static int stack_guard_page = 1;
4020 SYSCTL_INT(_security_bsd, OID_AUTO, stack_guard_page, CTLFLAG_RWTUN,
4021 &stack_guard_page, 0,
4022 "Specifies the number of guard pages for a stack that grows");
4025 vm_map_stack_locked(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
4026 vm_size_t growsize, vm_prot_t prot, vm_prot_t max, int cow)
4028 vm_map_entry_t new_entry, prev_entry;
4029 vm_offset_t bot, gap_bot, gap_top, top;
4030 vm_size_t init_ssize, sgp;
4034 * The stack orientation is piggybacked with the cow argument.
4035 * Extract it into orient and mask the cow argument so that we
4036 * don't pass it around further.
4038 orient = cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP);
4039 KASSERT(orient != 0, ("No stack grow direction"));
4040 KASSERT(orient != (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP),
4043 if (addrbos < vm_map_min(map) ||
4044 addrbos + max_ssize > vm_map_max(map) ||
4045 addrbos + max_ssize <= addrbos)
4046 return (KERN_INVALID_ADDRESS);
4047 sgp = (vm_size_t)stack_guard_page * PAGE_SIZE;
4048 if (sgp >= max_ssize)
4049 return (KERN_INVALID_ARGUMENT);
4051 init_ssize = growsize;
4052 if (max_ssize < init_ssize + sgp)
4053 init_ssize = max_ssize - sgp;
4055 /* If addr is already mapped, no go */
4056 if (vm_map_lookup_entry(map, addrbos, &prev_entry))
4057 return (KERN_NO_SPACE);
4060 * If we can't accommodate max_ssize in the current mapping, no go.
4062 if (prev_entry->next->start < addrbos + max_ssize)
4063 return (KERN_NO_SPACE);
4066 * We initially map a stack of only init_ssize. We will grow as
4067 * needed later. Depending on the orientation of the stack (i.e.
4068 * the grow direction) we either map at the top of the range, the
4069 * bottom of the range or in the middle.
4071 * Note: we would normally expect prot and max to be VM_PROT_ALL,
4072 * and cow to be 0. Possibly we should eliminate these as input
4073 * parameters, and just pass these values here in the insert call.
4075 if (orient == MAP_STACK_GROWS_DOWN) {
4076 bot = addrbos + max_ssize - init_ssize;
4077 top = bot + init_ssize;
4080 } else /* if (orient == MAP_STACK_GROWS_UP) */ {
4082 top = bot + init_ssize;
4084 gap_top = addrbos + max_ssize;
4086 rv = vm_map_insert(map, NULL, 0, bot, top, prot, max, cow);
4087 if (rv != KERN_SUCCESS)
4089 new_entry = prev_entry->next;
4090 KASSERT(new_entry->end == top || new_entry->start == bot,
4091 ("Bad entry start/end for new stack entry"));
4092 KASSERT((orient & MAP_STACK_GROWS_DOWN) == 0 ||
4093 (new_entry->eflags & MAP_ENTRY_GROWS_DOWN) != 0,
4094 ("new entry lacks MAP_ENTRY_GROWS_DOWN"));
4095 KASSERT((orient & MAP_STACK_GROWS_UP) == 0 ||
4096 (new_entry->eflags & MAP_ENTRY_GROWS_UP) != 0,
4097 ("new entry lacks MAP_ENTRY_GROWS_UP"));
4098 rv = vm_map_insert(map, NULL, 0, gap_bot, gap_top, VM_PROT_NONE,
4099 VM_PROT_NONE, MAP_CREATE_GUARD | (orient == MAP_STACK_GROWS_DOWN ?
4100 MAP_CREATE_STACK_GAP_DN : MAP_CREATE_STACK_GAP_UP));
4101 if (rv != KERN_SUCCESS)
4102 (void)vm_map_delete(map, bot, top);
4107 * Attempts to grow a vm stack entry. Returns KERN_SUCCESS if we
4108 * successfully grow the stack.
4111 vm_map_growstack(vm_map_t map, vm_offset_t addr, vm_map_entry_t gap_entry)
4113 vm_map_entry_t stack_entry;
4117 vm_offset_t gap_end, gap_start, grow_start;
4118 size_t grow_amount, guard, max_grow;
4119 rlim_t lmemlim, stacklim, vmemlim;
4121 bool gap_deleted, grow_down, is_procstack;
4133 * Disallow stack growth when the access is performed by a
4134 * debugger or AIO daemon. The reason is that the wrong
4135 * resource limits are applied.
4137 if (map != &p->p_vmspace->vm_map || p->p_textvp == NULL)
4138 return (KERN_FAILURE);
4140 MPASS(!map->system_map);
4142 guard = stack_guard_page * PAGE_SIZE;
4143 lmemlim = lim_cur(curthread, RLIMIT_MEMLOCK);
4144 stacklim = lim_cur(curthread, RLIMIT_STACK);
4145 vmemlim = lim_cur(curthread, RLIMIT_VMEM);
4147 /* If addr is not in a hole for a stack grow area, no need to grow. */
4148 if (gap_entry == NULL && !vm_map_lookup_entry(map, addr, &gap_entry))
4149 return (KERN_FAILURE);
4150 if ((gap_entry->eflags & MAP_ENTRY_GUARD) == 0)
4151 return (KERN_SUCCESS);
4152 if ((gap_entry->eflags & MAP_ENTRY_STACK_GAP_DN) != 0) {
4153 stack_entry = gap_entry->next;
4154 if ((stack_entry->eflags & MAP_ENTRY_GROWS_DOWN) == 0 ||
4155 stack_entry->start != gap_entry->end)
4156 return (KERN_FAILURE);
4157 grow_amount = round_page(stack_entry->start - addr);
4159 } else if ((gap_entry->eflags & MAP_ENTRY_STACK_GAP_UP) != 0) {
4160 stack_entry = gap_entry->prev;
4161 if ((stack_entry->eflags & MAP_ENTRY_GROWS_UP) == 0 ||
4162 stack_entry->end != gap_entry->start)
4163 return (KERN_FAILURE);
4164 grow_amount = round_page(addr + 1 - stack_entry->end);
4167 return (KERN_FAILURE);
4169 max_grow = gap_entry->end - gap_entry->start;
4170 if (guard > max_grow)
4171 return (KERN_NO_SPACE);
4173 if (grow_amount > max_grow)
4174 return (KERN_NO_SPACE);
4177 * If this is the main process stack, see if we're over the stack
4180 is_procstack = addr >= (vm_offset_t)vm->vm_maxsaddr &&
4181 addr < (vm_offset_t)p->p_sysent->sv_usrstack;
4182 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim))
4183 return (KERN_NO_SPACE);
4188 if (is_procstack && racct_set(p, RACCT_STACK,
4189 ctob(vm->vm_ssize) + grow_amount)) {
4191 return (KERN_NO_SPACE);
4197 grow_amount = roundup(grow_amount, sgrowsiz);
4198 if (grow_amount > max_grow)
4199 grow_amount = max_grow;
4200 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
4201 grow_amount = trunc_page((vm_size_t)stacklim) -
4207 limit = racct_get_available(p, RACCT_STACK);
4209 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > limit))
4210 grow_amount = limit - ctob(vm->vm_ssize);
4213 if (!old_mlock && (map->flags & MAP_WIREFUTURE) != 0) {
4214 if (ptoa(pmap_wired_count(map->pmap)) + grow_amount > lmemlim) {
4221 if (racct_set(p, RACCT_MEMLOCK,
4222 ptoa(pmap_wired_count(map->pmap)) + grow_amount)) {
4232 /* If we would blow our VMEM resource limit, no go */
4233 if (map->size + grow_amount > vmemlim) {
4240 if (racct_set(p, RACCT_VMEM, map->size + grow_amount)) {
4249 if (vm_map_lock_upgrade(map)) {
4251 vm_map_lock_read(map);
4256 grow_start = gap_entry->end - grow_amount;
4257 if (gap_entry->start + grow_amount == gap_entry->end) {
4258 gap_start = gap_entry->start;
4259 gap_end = gap_entry->end;
4260 vm_map_entry_delete(map, gap_entry);
4263 MPASS(gap_entry->start < gap_entry->end - grow_amount);
4264 gap_entry->end -= grow_amount;
4265 vm_map_entry_resize_free(map, gap_entry);
4266 gap_deleted = false;
4268 rv = vm_map_insert(map, NULL, 0, grow_start,
4269 grow_start + grow_amount,
4270 stack_entry->protection, stack_entry->max_protection,
4271 MAP_STACK_GROWS_DOWN);
4272 if (rv != KERN_SUCCESS) {
4274 rv1 = vm_map_insert(map, NULL, 0, gap_start,
4275 gap_end, VM_PROT_NONE, VM_PROT_NONE,
4276 MAP_CREATE_GUARD | MAP_CREATE_STACK_GAP_DN);
4277 MPASS(rv1 == KERN_SUCCESS);
4279 gap_entry->end += grow_amount;
4280 vm_map_entry_resize_free(map, gap_entry);
4284 grow_start = stack_entry->end;
4285 cred = stack_entry->cred;
4286 if (cred == NULL && stack_entry->object.vm_object != NULL)
4287 cred = stack_entry->object.vm_object->cred;
4288 if (cred != NULL && !swap_reserve_by_cred(grow_amount, cred))
4290 /* Grow the underlying object if applicable. */
4291 else if (stack_entry->object.vm_object == NULL ||
4292 vm_object_coalesce(stack_entry->object.vm_object,
4293 stack_entry->offset,
4294 (vm_size_t)(stack_entry->end - stack_entry->start),
4295 (vm_size_t)grow_amount, cred != NULL)) {
4296 if (gap_entry->start + grow_amount == gap_entry->end)
4297 vm_map_entry_delete(map, gap_entry);
4299 gap_entry->start += grow_amount;
4300 stack_entry->end += grow_amount;
4301 map->size += grow_amount;
4302 vm_map_entry_resize_free(map, stack_entry);
4307 if (rv == KERN_SUCCESS && is_procstack)
4308 vm->vm_ssize += btoc(grow_amount);
4311 * Heed the MAP_WIREFUTURE flag if it was set for this process.
4313 if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE) != 0) {
4315 vm_map_wire(map, grow_start, grow_start + grow_amount,
4316 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
4317 vm_map_lock_read(map);
4319 vm_map_lock_downgrade(map);
4323 if (racct_enable && rv != KERN_SUCCESS) {
4325 error = racct_set(p, RACCT_VMEM, map->size);
4326 KASSERT(error == 0, ("decreasing RACCT_VMEM failed"));
4328 error = racct_set(p, RACCT_MEMLOCK,
4329 ptoa(pmap_wired_count(map->pmap)));
4330 KASSERT(error == 0, ("decreasing RACCT_MEMLOCK failed"));
4332 error = racct_set(p, RACCT_STACK, ctob(vm->vm_ssize));
4333 KASSERT(error == 0, ("decreasing RACCT_STACK failed"));
4342 * Unshare the specified VM space for exec. If other processes are
4343 * mapped to it, then create a new one. The new vmspace is null.
4346 vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser)
4348 struct vmspace *oldvmspace = p->p_vmspace;
4349 struct vmspace *newvmspace;
4351 KASSERT((curthread->td_pflags & TDP_EXECVMSPC) == 0,
4352 ("vmspace_exec recursed"));
4353 newvmspace = vmspace_alloc(minuser, maxuser, pmap_pinit);
4354 if (newvmspace == NULL)
4356 newvmspace->vm_swrss = oldvmspace->vm_swrss;
4358 * This code is written like this for prototype purposes. The
4359 * goal is to avoid running down the vmspace here, but let the
4360 * other process's that are still using the vmspace to finally
4361 * run it down. Even though there is little or no chance of blocking
4362 * here, it is a good idea to keep this form for future mods.
4364 PROC_VMSPACE_LOCK(p);
4365 p->p_vmspace = newvmspace;
4366 PROC_VMSPACE_UNLOCK(p);
4367 if (p == curthread->td_proc)
4368 pmap_activate(curthread);
4369 curthread->td_pflags |= TDP_EXECVMSPC;
4374 * Unshare the specified VM space for forcing COW. This
4375 * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
4378 vmspace_unshare(struct proc *p)
4380 struct vmspace *oldvmspace = p->p_vmspace;
4381 struct vmspace *newvmspace;
4382 vm_ooffset_t fork_charge;
4384 if (oldvmspace->vm_refcnt == 1)
4387 newvmspace = vmspace_fork(oldvmspace, &fork_charge);
4388 if (newvmspace == NULL)
4390 if (!swap_reserve_by_cred(fork_charge, p->p_ucred)) {
4391 vmspace_free(newvmspace);
4394 PROC_VMSPACE_LOCK(p);
4395 p->p_vmspace = newvmspace;
4396 PROC_VMSPACE_UNLOCK(p);
4397 if (p == curthread->td_proc)
4398 pmap_activate(curthread);
4399 vmspace_free(oldvmspace);
4406 * Finds the VM object, offset, and
4407 * protection for a given virtual address in the
4408 * specified map, assuming a page fault of the
4411 * Leaves the map in question locked for read; return
4412 * values are guaranteed until a vm_map_lookup_done
4413 * call is performed. Note that the map argument
4414 * is in/out; the returned map must be used in
4415 * the call to vm_map_lookup_done.
4417 * A handle (out_entry) is returned for use in
4418 * vm_map_lookup_done, to make that fast.
4420 * If a lookup is requested with "write protection"
4421 * specified, the map may be changed to perform virtual
4422 * copying operations, although the data referenced will
4426 vm_map_lookup(vm_map_t *var_map, /* IN/OUT */
4428 vm_prot_t fault_typea,
4429 vm_map_entry_t *out_entry, /* OUT */
4430 vm_object_t *object, /* OUT */
4431 vm_pindex_t *pindex, /* OUT */
4432 vm_prot_t *out_prot, /* OUT */
4433 boolean_t *wired) /* OUT */
4435 vm_map_entry_t entry;
4436 vm_map_t map = *var_map;
4438 vm_prot_t fault_type = fault_typea;
4439 vm_object_t eobject;
4445 vm_map_lock_read(map);
4449 * Lookup the faulting address.
4451 if (!vm_map_lookup_entry(map, vaddr, out_entry)) {
4452 vm_map_unlock_read(map);
4453 return (KERN_INVALID_ADDRESS);
4461 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
4462 vm_map_t old_map = map;
4464 *var_map = map = entry->object.sub_map;
4465 vm_map_unlock_read(old_map);
4470 * Check whether this task is allowed to have this page.
4472 prot = entry->protection;
4473 if ((fault_typea & VM_PROT_FAULT_LOOKUP) != 0) {
4474 fault_typea &= ~VM_PROT_FAULT_LOOKUP;
4475 if (prot == VM_PROT_NONE && map != kernel_map &&
4476 (entry->eflags & MAP_ENTRY_GUARD) != 0 &&
4477 (entry->eflags & (MAP_ENTRY_STACK_GAP_DN |
4478 MAP_ENTRY_STACK_GAP_UP)) != 0 &&
4479 vm_map_growstack(map, vaddr, entry) == KERN_SUCCESS)
4480 goto RetryLookupLocked;
4482 fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
4483 if ((fault_type & prot) != fault_type || prot == VM_PROT_NONE) {
4484 vm_map_unlock_read(map);
4485 return (KERN_PROTECTION_FAILURE);
4487 KASSERT((prot & VM_PROT_WRITE) == 0 || (entry->eflags &
4488 (MAP_ENTRY_USER_WIRED | MAP_ENTRY_NEEDS_COPY)) !=
4489 (MAP_ENTRY_USER_WIRED | MAP_ENTRY_NEEDS_COPY),
4490 ("entry %p flags %x", entry, entry->eflags));
4491 if ((fault_typea & VM_PROT_COPY) != 0 &&
4492 (entry->max_protection & VM_PROT_WRITE) == 0 &&
4493 (entry->eflags & MAP_ENTRY_COW) == 0) {
4494 vm_map_unlock_read(map);
4495 return (KERN_PROTECTION_FAILURE);
4499 * If this page is not pageable, we have to get it for all possible
4502 *wired = (entry->wired_count != 0);
4504 fault_type = entry->protection;
4505 size = entry->end - entry->start;
4507 * If the entry was copy-on-write, we either ...
4509 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
4511 * If we want to write the page, we may as well handle that
4512 * now since we've got the map locked.
4514 * If we don't need to write the page, we just demote the
4515 * permissions allowed.
4517 if ((fault_type & VM_PROT_WRITE) != 0 ||
4518 (fault_typea & VM_PROT_COPY) != 0) {
4520 * Make a new object, and place it in the object
4521 * chain. Note that no new references have appeared
4522 * -- one just moved from the map to the new
4525 if (vm_map_lock_upgrade(map))
4528 if (entry->cred == NULL) {
4530 * The debugger owner is charged for
4533 cred = curthread->td_ucred;
4535 if (!swap_reserve_by_cred(size, cred)) {
4538 return (KERN_RESOURCE_SHORTAGE);
4542 vm_object_shadow(&entry->object.vm_object,
4543 &entry->offset, size);
4544 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
4545 eobject = entry->object.vm_object;
4546 if (eobject->cred != NULL) {
4548 * The object was not shadowed.
4550 swap_release_by_cred(size, entry->cred);
4551 crfree(entry->cred);
4553 } else if (entry->cred != NULL) {
4554 VM_OBJECT_WLOCK(eobject);
4555 eobject->cred = entry->cred;
4556 eobject->charge = size;
4557 VM_OBJECT_WUNLOCK(eobject);
4561 vm_map_lock_downgrade(map);
4564 * We're attempting to read a copy-on-write page --
4565 * don't allow writes.
4567 prot &= ~VM_PROT_WRITE;
4572 * Create an object if necessary.
4574 if (entry->object.vm_object == NULL &&
4576 if (vm_map_lock_upgrade(map))
4578 entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
4581 if (entry->cred != NULL) {
4582 VM_OBJECT_WLOCK(entry->object.vm_object);
4583 entry->object.vm_object->cred = entry->cred;
4584 entry->object.vm_object->charge = size;
4585 VM_OBJECT_WUNLOCK(entry->object.vm_object);
4588 vm_map_lock_downgrade(map);
4592 * Return the object/offset from this entry. If the entry was
4593 * copy-on-write or empty, it has been fixed up.
4595 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
4596 *object = entry->object.vm_object;
4599 return (KERN_SUCCESS);
4603 * vm_map_lookup_locked:
4605 * Lookup the faulting address. A version of vm_map_lookup that returns
4606 * KERN_FAILURE instead of blocking on map lock or memory allocation.
4609 vm_map_lookup_locked(vm_map_t *var_map, /* IN/OUT */
4611 vm_prot_t fault_typea,
4612 vm_map_entry_t *out_entry, /* OUT */
4613 vm_object_t *object, /* OUT */
4614 vm_pindex_t *pindex, /* OUT */
4615 vm_prot_t *out_prot, /* OUT */
4616 boolean_t *wired) /* OUT */
4618 vm_map_entry_t entry;
4619 vm_map_t map = *var_map;
4621 vm_prot_t fault_type = fault_typea;
4624 * Lookup the faulting address.
4626 if (!vm_map_lookup_entry(map, vaddr, out_entry))
4627 return (KERN_INVALID_ADDRESS);
4632 * Fail if the entry refers to a submap.
4634 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
4635 return (KERN_FAILURE);
4638 * Check whether this task is allowed to have this page.
4640 prot = entry->protection;
4641 fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
4642 if ((fault_type & prot) != fault_type)
4643 return (KERN_PROTECTION_FAILURE);
4646 * If this page is not pageable, we have to get it for all possible
4649 *wired = (entry->wired_count != 0);
4651 fault_type = entry->protection;
4653 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
4655 * Fail if the entry was copy-on-write for a write fault.
4657 if (fault_type & VM_PROT_WRITE)
4658 return (KERN_FAILURE);
4660 * We're attempting to read a copy-on-write page --
4661 * don't allow writes.
4663 prot &= ~VM_PROT_WRITE;
4667 * Fail if an object should be created.
4669 if (entry->object.vm_object == NULL && !map->system_map)
4670 return (KERN_FAILURE);
4673 * Return the object/offset from this entry. If the entry was
4674 * copy-on-write or empty, it has been fixed up.
4676 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
4677 *object = entry->object.vm_object;
4680 return (KERN_SUCCESS);
4684 * vm_map_lookup_done:
4686 * Releases locks acquired by a vm_map_lookup
4687 * (according to the handle returned by that lookup).
4690 vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry)
4693 * Unlock the main-level map
4695 vm_map_unlock_read(map);
4699 vm_map_max_KBI(const struct vm_map *map)
4702 return (vm_map_max(map));
4706 vm_map_min_KBI(const struct vm_map *map)
4709 return (vm_map_min(map));
4713 vm_map_pmap_KBI(vm_map_t map)
4719 #include "opt_ddb.h"
4721 #include <sys/kernel.h>
4723 #include <ddb/ddb.h>
4726 vm_map_print(vm_map_t map)
4728 vm_map_entry_t entry;
4730 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
4732 (void *)map->pmap, map->nentries, map->timestamp);
4735 for (entry = map->header.next; entry != &map->header;
4736 entry = entry->next) {
4737 db_iprintf("map entry %p: start=%p, end=%p, eflags=%#x, \n",
4738 (void *)entry, (void *)entry->start, (void *)entry->end,
4741 static char *inheritance_name[4] =
4742 {"share", "copy", "none", "donate_copy"};
4744 db_iprintf(" prot=%x/%x/%s",
4746 entry->max_protection,
4747 inheritance_name[(int)(unsigned char)entry->inheritance]);
4748 if (entry->wired_count != 0)
4749 db_printf(", wired");
4751 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
4752 db_printf(", share=%p, offset=0x%jx\n",
4753 (void *)entry->object.sub_map,
4754 (uintmax_t)entry->offset);
4755 if ((entry->prev == &map->header) ||
4756 (entry->prev->object.sub_map !=
4757 entry->object.sub_map)) {
4759 vm_map_print((vm_map_t)entry->object.sub_map);
4763 if (entry->cred != NULL)
4764 db_printf(", ruid %d", entry->cred->cr_ruid);
4765 db_printf(", object=%p, offset=0x%jx",
4766 (void *)entry->object.vm_object,
4767 (uintmax_t)entry->offset);
4768 if (entry->object.vm_object && entry->object.vm_object->cred)
4769 db_printf(", obj ruid %d charge %jx",
4770 entry->object.vm_object->cred->cr_ruid,
4771 (uintmax_t)entry->object.vm_object->charge);
4772 if (entry->eflags & MAP_ENTRY_COW)
4773 db_printf(", copy (%s)",
4774 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
4777 if ((entry->prev == &map->header) ||
4778 (entry->prev->object.vm_object !=
4779 entry->object.vm_object)) {
4781 vm_object_print((db_expr_t)(intptr_t)
4782 entry->object.vm_object,
4791 DB_SHOW_COMMAND(map, map)
4795 db_printf("usage: show map <addr>\n");
4798 vm_map_print((vm_map_t)addr);
4801 DB_SHOW_COMMAND(procvm, procvm)
4806 p = db_lookup_proc(addr);
4811 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
4812 (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
4813 (void *)vmspace_pmap(p->p_vmspace));
4815 vm_map_print((vm_map_t)&p->p_vmspace->vm_map);