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 int vm_map_alignspace(vm_map_t map, vm_object_t object,
136 vm_ooffset_t offset, vm_offset_t *addr, vm_size_t length,
137 vm_offset_t max_addr, vm_offset_t alignment);
138 static void vm_map_entry_deallocate(vm_map_entry_t entry, boolean_t system_map);
139 static void vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry);
140 static void vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry);
141 static int vm_map_growstack(vm_map_t map, vm_offset_t addr,
142 vm_map_entry_t gap_entry);
143 static void vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
144 vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags);
146 static void vm_map_zdtor(void *mem, int size, void *arg);
147 static void vmspace_zdtor(void *mem, int size, void *arg);
149 static int vm_map_stack_locked(vm_map_t map, vm_offset_t addrbos,
150 vm_size_t max_ssize, vm_size_t growsize, vm_prot_t prot, vm_prot_t max,
152 static void vm_map_wire_entry_failure(vm_map_t map, vm_map_entry_t entry,
153 vm_offset_t failed_addr);
155 #define ENTRY_CHARGED(e) ((e)->cred != NULL || \
156 ((e)->object.vm_object != NULL && (e)->object.vm_object->cred != NULL && \
157 !((e)->eflags & MAP_ENTRY_NEEDS_COPY)))
160 * PROC_VMSPACE_{UN,}LOCK() can be a noop as long as vmspaces are type
163 #define PROC_VMSPACE_LOCK(p) do { } while (0)
164 #define PROC_VMSPACE_UNLOCK(p) do { } while (0)
167 * VM_MAP_RANGE_CHECK: [ internal use only ]
169 * Asserts that the starting and ending region
170 * addresses fall within the valid range of the map.
172 #define VM_MAP_RANGE_CHECK(map, start, end) \
174 if (start < vm_map_min(map)) \
175 start = vm_map_min(map); \
176 if (end > vm_map_max(map)) \
177 end = vm_map_max(map); \
185 * Initialize the vm_map module. Must be called before
186 * any other vm_map routines.
188 * Map and entry structures are allocated from the general
189 * purpose memory pool with some exceptions:
191 * - The kernel map and kmem submap are allocated statically.
192 * - Kernel map entries are allocated out of a static pool.
194 * These restrictions are necessary since malloc() uses the
195 * maps and requires map entries.
201 mtx_init(&map_sleep_mtx, "vm map sleep mutex", NULL, MTX_DEF);
202 mapzone = uma_zcreate("MAP", sizeof(struct vm_map), NULL,
208 vm_map_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
209 uma_prealloc(mapzone, MAX_KMAP);
210 kmapentzone = uma_zcreate("KMAP ENTRY", sizeof(struct vm_map_entry),
211 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
212 UMA_ZONE_MTXCLASS | UMA_ZONE_VM);
213 mapentzone = uma_zcreate("MAP ENTRY", sizeof(struct vm_map_entry),
214 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
215 vmspace_zone = uma_zcreate("VMSPACE", sizeof(struct vmspace), NULL,
221 vmspace_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
225 vmspace_zinit(void *mem, int size, int flags)
229 vm = (struct vmspace *)mem;
231 vm->vm_map.pmap = NULL;
232 (void)vm_map_zinit(&vm->vm_map, sizeof(vm->vm_map), flags);
233 PMAP_LOCK_INIT(vmspace_pmap(vm));
238 vm_map_zinit(void *mem, int size, int flags)
243 memset(map, 0, sizeof(*map));
244 mtx_init(&map->system_mtx, "vm map (system)", NULL, MTX_DEF | MTX_DUPOK);
245 sx_init(&map->lock, "vm map (user)");
251 vmspace_zdtor(void *mem, int size, void *arg)
255 vm = (struct vmspace *)mem;
257 vm_map_zdtor(&vm->vm_map, sizeof(vm->vm_map), arg);
260 vm_map_zdtor(void *mem, int size, void *arg)
265 KASSERT(map->nentries == 0,
266 ("map %p nentries == %d on free.",
267 map, map->nentries));
268 KASSERT(map->size == 0,
269 ("map %p size == %lu on free.",
270 map, (unsigned long)map->size));
272 #endif /* INVARIANTS */
275 * Allocate a vmspace structure, including a vm_map and pmap,
276 * and initialize those structures. The refcnt is set to 1.
278 * If 'pinit' is NULL then the embedded pmap is initialized via pmap_pinit().
281 vmspace_alloc(vm_offset_t min, vm_offset_t max, pmap_pinit_t pinit)
285 vm = uma_zalloc(vmspace_zone, M_WAITOK);
287 KASSERT(vm->vm_map.pmap == NULL, ("vm_map.pmap must be NULL"));
292 if (!pinit(vmspace_pmap(vm))) {
293 uma_zfree(vmspace_zone, vm);
296 CTR1(KTR_VM, "vmspace_alloc: %p", vm);
297 _vm_map_init(&vm->vm_map, vmspace_pmap(vm), min, max);
312 vmspace_container_reset(struct proc *p)
316 racct_set(p, RACCT_DATA, 0);
317 racct_set(p, RACCT_STACK, 0);
318 racct_set(p, RACCT_RSS, 0);
319 racct_set(p, RACCT_MEMLOCK, 0);
320 racct_set(p, RACCT_VMEM, 0);
326 vmspace_dofree(struct vmspace *vm)
329 CTR1(KTR_VM, "vmspace_free: %p", vm);
332 * Make sure any SysV shm is freed, it might not have been in
338 * Lock the map, to wait out all other references to it.
339 * Delete all of the mappings and pages they hold, then call
340 * the pmap module to reclaim anything left.
342 (void)vm_map_remove(&vm->vm_map, vm_map_min(&vm->vm_map),
343 vm_map_max(&vm->vm_map));
345 pmap_release(vmspace_pmap(vm));
346 vm->vm_map.pmap = NULL;
347 uma_zfree(vmspace_zone, vm);
351 vmspace_free(struct vmspace *vm)
354 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
355 "vmspace_free() called");
357 if (vm->vm_refcnt == 0)
358 panic("vmspace_free: attempt to free already freed vmspace");
360 if (atomic_fetchadd_int(&vm->vm_refcnt, -1) == 1)
365 vmspace_exitfree(struct proc *p)
369 PROC_VMSPACE_LOCK(p);
372 PROC_VMSPACE_UNLOCK(p);
373 KASSERT(vm == &vmspace0, ("vmspace_exitfree: wrong vmspace"));
378 vmspace_exit(struct thread *td)
385 * Release user portion of address space.
386 * This releases references to vnodes,
387 * which could cause I/O if the file has been unlinked.
388 * Need to do this early enough that we can still sleep.
390 * The last exiting process to reach this point releases as
391 * much of the environment as it can. vmspace_dofree() is the
392 * slower fallback in case another process had a temporary
393 * reference to the vmspace.
398 atomic_add_int(&vmspace0.vm_refcnt, 1);
400 refcnt = vm->vm_refcnt;
401 if (refcnt > 1 && p->p_vmspace != &vmspace0) {
402 /* Switch now since other proc might free vmspace */
403 PROC_VMSPACE_LOCK(p);
404 p->p_vmspace = &vmspace0;
405 PROC_VMSPACE_UNLOCK(p);
408 } while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1));
410 if (p->p_vmspace != vm) {
411 /* vmspace not yet freed, switch back */
412 PROC_VMSPACE_LOCK(p);
414 PROC_VMSPACE_UNLOCK(p);
417 pmap_remove_pages(vmspace_pmap(vm));
418 /* Switch now since this proc will free vmspace */
419 PROC_VMSPACE_LOCK(p);
420 p->p_vmspace = &vmspace0;
421 PROC_VMSPACE_UNLOCK(p);
427 vmspace_container_reset(p);
431 /* Acquire reference to vmspace owned by another process. */
434 vmspace_acquire_ref(struct proc *p)
439 PROC_VMSPACE_LOCK(p);
442 PROC_VMSPACE_UNLOCK(p);
446 refcnt = vm->vm_refcnt;
447 if (refcnt <= 0) { /* Avoid 0->1 transition */
448 PROC_VMSPACE_UNLOCK(p);
451 } while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt + 1));
452 if (vm != p->p_vmspace) {
453 PROC_VMSPACE_UNLOCK(p);
457 PROC_VMSPACE_UNLOCK(p);
462 * Switch between vmspaces in an AIO kernel process.
464 * The AIO kernel processes switch to and from a user process's
465 * vmspace while performing an I/O operation on behalf of a user
466 * process. The new vmspace is either the vmspace of a user process
467 * obtained from an active AIO request or the initial vmspace of the
468 * AIO kernel process (when it is idling). Because user processes
469 * will block to drain any active AIO requests before proceeding in
470 * exit() or execve(), the vmspace reference count for these vmspaces
471 * can never be 0. This allows for a much simpler implementation than
472 * the loop in vmspace_acquire_ref() above. Similarly, AIO kernel
473 * processes hold an extra reference on their initial vmspace for the
474 * life of the process so that this guarantee is true for any vmspace
478 vmspace_switch_aio(struct vmspace *newvm)
480 struct vmspace *oldvm;
482 /* XXX: Need some way to assert that this is an aio daemon. */
484 KASSERT(newvm->vm_refcnt > 0,
485 ("vmspace_switch_aio: newvm unreferenced"));
487 oldvm = curproc->p_vmspace;
492 * Point to the new address space and refer to it.
494 curproc->p_vmspace = newvm;
495 atomic_add_int(&newvm->vm_refcnt, 1);
497 /* Activate the new mapping. */
498 pmap_activate(curthread);
500 /* Remove the daemon's reference to the old address space. */
501 KASSERT(oldvm->vm_refcnt > 1,
502 ("vmspace_switch_aio: oldvm dropping last reference"));
507 _vm_map_lock(vm_map_t map, const char *file, int line)
511 mtx_lock_flags_(&map->system_mtx, 0, file, line);
513 sx_xlock_(&map->lock, file, line);
518 vm_map_process_deferred(void)
521 vm_map_entry_t entry, next;
525 entry = td->td_map_def_user;
526 td->td_map_def_user = NULL;
527 while (entry != NULL) {
529 if ((entry->eflags & MAP_ENTRY_VN_WRITECNT) != 0) {
531 * Decrement the object's writemappings and
532 * possibly the vnode's v_writecount.
534 KASSERT((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0,
535 ("Submap with writecount"));
536 object = entry->object.vm_object;
537 KASSERT(object != NULL, ("No object for writecount"));
538 vnode_pager_release_writecount(object, entry->start,
541 vm_map_entry_deallocate(entry, FALSE);
547 _vm_map_unlock(vm_map_t map, const char *file, int line)
551 mtx_unlock_flags_(&map->system_mtx, 0, file, line);
553 sx_xunlock_(&map->lock, file, line);
554 vm_map_process_deferred();
559 _vm_map_lock_read(vm_map_t map, const char *file, int line)
563 mtx_lock_flags_(&map->system_mtx, 0, file, line);
565 sx_slock_(&map->lock, file, line);
569 _vm_map_unlock_read(vm_map_t map, const char *file, int line)
573 mtx_unlock_flags_(&map->system_mtx, 0, file, line);
575 sx_sunlock_(&map->lock, file, line);
576 vm_map_process_deferred();
581 _vm_map_trylock(vm_map_t map, const char *file, int line)
585 error = map->system_map ?
586 !mtx_trylock_flags_(&map->system_mtx, 0, file, line) :
587 !sx_try_xlock_(&map->lock, file, line);
594 _vm_map_trylock_read(vm_map_t map, const char *file, int line)
598 error = map->system_map ?
599 !mtx_trylock_flags_(&map->system_mtx, 0, file, line) :
600 !sx_try_slock_(&map->lock, file, line);
605 * _vm_map_lock_upgrade: [ internal use only ]
607 * Tries to upgrade a read (shared) lock on the specified map to a write
608 * (exclusive) lock. Returns the value "0" if the upgrade succeeds and a
609 * non-zero value if the upgrade fails. If the upgrade fails, the map is
610 * returned without a read or write lock held.
612 * Requires that the map be read locked.
615 _vm_map_lock_upgrade(vm_map_t map, const char *file, int line)
617 unsigned int last_timestamp;
619 if (map->system_map) {
620 mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
622 if (!sx_try_upgrade_(&map->lock, file, line)) {
623 last_timestamp = map->timestamp;
624 sx_sunlock_(&map->lock, file, line);
625 vm_map_process_deferred();
627 * If the map's timestamp does not change while the
628 * map is unlocked, then the upgrade succeeds.
630 sx_xlock_(&map->lock, file, line);
631 if (last_timestamp != map->timestamp) {
632 sx_xunlock_(&map->lock, file, line);
642 _vm_map_lock_downgrade(vm_map_t map, const char *file, int line)
645 if (map->system_map) {
646 mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
648 sx_downgrade_(&map->lock, file, line);
654 * Returns a non-zero value if the caller holds a write (exclusive) lock
655 * on the specified map and the value "0" otherwise.
658 vm_map_locked(vm_map_t map)
662 return (mtx_owned(&map->system_mtx));
664 return (sx_xlocked(&map->lock));
669 _vm_map_assert_locked(vm_map_t map, const char *file, int line)
673 mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
675 sx_assert_(&map->lock, SA_XLOCKED, file, line);
678 #define VM_MAP_ASSERT_LOCKED(map) \
679 _vm_map_assert_locked(map, LOCK_FILE, LOCK_LINE)
681 #define VM_MAP_ASSERT_LOCKED(map)
685 * _vm_map_unlock_and_wait:
687 * Atomically releases the lock on the specified map and puts the calling
688 * thread to sleep. The calling thread will remain asleep until either
689 * vm_map_wakeup() is performed on the map or the specified timeout is
692 * WARNING! This function does not perform deferred deallocations of
693 * objects and map entries. Therefore, the calling thread is expected to
694 * reacquire the map lock after reawakening and later perform an ordinary
695 * unlock operation, such as vm_map_unlock(), before completing its
696 * operation on the map.
699 _vm_map_unlock_and_wait(vm_map_t map, int timo, const char *file, int line)
702 mtx_lock(&map_sleep_mtx);
704 mtx_unlock_flags_(&map->system_mtx, 0, file, line);
706 sx_xunlock_(&map->lock, file, line);
707 return (msleep(&map->root, &map_sleep_mtx, PDROP | PVM, "vmmaps",
714 * Awaken any threads that have slept on the map using
715 * vm_map_unlock_and_wait().
718 vm_map_wakeup(vm_map_t map)
722 * Acquire and release map_sleep_mtx to prevent a wakeup()
723 * from being performed (and lost) between the map unlock
724 * and the msleep() in _vm_map_unlock_and_wait().
726 mtx_lock(&map_sleep_mtx);
727 mtx_unlock(&map_sleep_mtx);
732 vm_map_busy(vm_map_t map)
735 VM_MAP_ASSERT_LOCKED(map);
740 vm_map_unbusy(vm_map_t map)
743 VM_MAP_ASSERT_LOCKED(map);
744 KASSERT(map->busy, ("vm_map_unbusy: not busy"));
745 if (--map->busy == 0 && (map->flags & MAP_BUSY_WAKEUP)) {
746 vm_map_modflags(map, 0, MAP_BUSY_WAKEUP);
752 vm_map_wait_busy(vm_map_t map)
755 VM_MAP_ASSERT_LOCKED(map);
757 vm_map_modflags(map, MAP_BUSY_WAKEUP, 0);
759 msleep(&map->busy, &map->system_mtx, 0, "mbusy", 0);
761 sx_sleep(&map->busy, &map->lock, 0, "mbusy", 0);
767 vmspace_resident_count(struct vmspace *vmspace)
769 return pmap_resident_count(vmspace_pmap(vmspace));
775 * Creates and returns a new empty VM map with
776 * the given physical map structure, and having
777 * the given lower and upper address bounds.
780 vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max)
784 result = uma_zalloc(mapzone, M_WAITOK);
785 CTR1(KTR_VM, "vm_map_create: %p", result);
786 _vm_map_init(result, pmap, min, max);
791 * Initialize an existing vm_map structure
792 * such as that in the vmspace structure.
795 _vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min, vm_offset_t max)
798 map->header.next = map->header.prev = &map->header;
799 map->header.eflags = MAP_ENTRY_HEADER;
800 map->needs_wakeup = FALSE;
803 map->header.end = min;
804 map->header.start = max;
812 vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min, vm_offset_t max)
815 _vm_map_init(map, pmap, min, max);
816 mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
817 sx_init(&map->lock, "user map");
821 * vm_map_entry_dispose: [ internal use only ]
823 * Inverse of vm_map_entry_create.
826 vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry)
828 uma_zfree(map->system_map ? kmapentzone : mapentzone, entry);
832 * vm_map_entry_create: [ internal use only ]
834 * Allocates a VM map entry for insertion.
835 * No entry fields are filled in.
837 static vm_map_entry_t
838 vm_map_entry_create(vm_map_t map)
840 vm_map_entry_t new_entry;
843 new_entry = uma_zalloc(kmapentzone, M_NOWAIT);
845 new_entry = uma_zalloc(mapentzone, M_WAITOK);
846 if (new_entry == NULL)
847 panic("vm_map_entry_create: kernel resources exhausted");
852 * vm_map_entry_set_behavior:
854 * Set the expected access behavior, either normal, random, or
858 vm_map_entry_set_behavior(vm_map_entry_t entry, u_char behavior)
860 entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) |
861 (behavior & MAP_ENTRY_BEHAV_MASK);
865 * vm_map_entry_set_max_free:
867 * Set the max_free field in a vm_map_entry.
870 vm_map_entry_set_max_free(vm_map_entry_t entry)
873 entry->max_free = entry->adj_free;
874 if (entry->left != NULL && entry->left->max_free > entry->max_free)
875 entry->max_free = entry->left->max_free;
876 if (entry->right != NULL && entry->right->max_free > entry->max_free)
877 entry->max_free = entry->right->max_free;
881 * vm_map_entry_splay:
883 * The Sleator and Tarjan top-down splay algorithm with the
884 * following variation. Max_free must be computed bottom-up, so
885 * on the downward pass, maintain the left and right spines in
886 * reverse order. Then, make a second pass up each side to fix
887 * the pointers and compute max_free. The time bound is O(log n)
890 * The new root is the vm_map_entry containing "addr", or else an
891 * adjacent entry (lower or higher) if addr is not in the tree.
893 * The map must be locked, and leaves it so.
895 * Returns: the new root.
897 static vm_map_entry_t
898 vm_map_entry_splay(vm_offset_t addr, vm_map_entry_t root)
900 vm_map_entry_t llist, rlist;
901 vm_map_entry_t ltree, rtree;
904 /* Special case of empty tree. */
909 * Pass One: Splay down the tree until we find addr or a NULL
910 * pointer where addr would go. llist and rlist are the two
911 * sides in reverse order (bottom-up), with llist linked by
912 * the right pointer and rlist linked by the left pointer in
913 * the vm_map_entry. Wait until Pass Two to set max_free on
919 /* root is never NULL in here. */
920 if (addr < root->start) {
924 if (addr < y->start && y->left != NULL) {
925 /* Rotate right and put y on rlist. */
926 root->left = y->right;
928 vm_map_entry_set_max_free(root);
933 /* Put root on rlist. */
938 } else if (addr >= root->end) {
942 if (addr >= y->end && y->right != NULL) {
943 /* Rotate left and put y on llist. */
944 root->right = y->left;
946 vm_map_entry_set_max_free(root);
951 /* Put root on llist. */
961 * Pass Two: Walk back up the two spines, flip the pointers
962 * and set max_free. The subtrees of the root go at the
963 * bottom of llist and rlist.
966 while (llist != NULL) {
968 llist->right = ltree;
969 vm_map_entry_set_max_free(llist);
974 while (rlist != NULL) {
977 vm_map_entry_set_max_free(rlist);
983 * Final assembly: add ltree and rtree as subtrees of root.
987 vm_map_entry_set_max_free(root);
993 * vm_map_entry_{un,}link:
995 * Insert/remove entries from maps.
998 vm_map_entry_link(vm_map_t map,
999 vm_map_entry_t after_where,
1000 vm_map_entry_t entry)
1004 "vm_map_entry_link: map %p, nentries %d, entry %p, after %p", map,
1005 map->nentries, entry, after_where);
1006 VM_MAP_ASSERT_LOCKED(map);
1007 KASSERT(after_where->end <= entry->start,
1008 ("vm_map_entry_link: prev end %jx new start %jx overlap",
1009 (uintmax_t)after_where->end, (uintmax_t)entry->start));
1010 KASSERT(entry->end <= after_where->next->start,
1011 ("vm_map_entry_link: new end %jx next start %jx overlap",
1012 (uintmax_t)entry->end, (uintmax_t)after_where->next->start));
1015 entry->prev = after_where;
1016 entry->next = after_where->next;
1017 entry->next->prev = entry;
1018 after_where->next = entry;
1020 if (after_where != &map->header) {
1021 if (after_where != map->root)
1022 vm_map_entry_splay(after_where->start, map->root);
1023 entry->right = after_where->right;
1024 entry->left = after_where;
1025 after_where->right = NULL;
1026 after_where->adj_free = entry->start - after_where->end;
1027 vm_map_entry_set_max_free(after_where);
1029 entry->right = map->root;
1032 entry->adj_free = entry->next->start - entry->end;
1033 vm_map_entry_set_max_free(entry);
1038 vm_map_entry_unlink(vm_map_t map,
1039 vm_map_entry_t entry)
1041 vm_map_entry_t next, prev, root;
1043 VM_MAP_ASSERT_LOCKED(map);
1044 if (entry != map->root)
1045 vm_map_entry_splay(entry->start, map->root);
1046 if (entry->left == NULL)
1047 root = entry->right;
1049 root = vm_map_entry_splay(entry->start, entry->left);
1050 root->right = entry->right;
1051 root->adj_free = entry->next->start - root->end;
1052 vm_map_entry_set_max_free(root);
1061 CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map,
1062 map->nentries, entry);
1066 * vm_map_entry_resize_free:
1068 * Recompute the amount of free space following a vm_map_entry
1069 * and propagate that value up the tree. Call this function after
1070 * resizing a map entry in-place, that is, without a call to
1071 * vm_map_entry_link() or _unlink().
1073 * The map must be locked, and leaves it so.
1076 vm_map_entry_resize_free(vm_map_t map, vm_map_entry_t entry)
1080 * Using splay trees without parent pointers, propagating
1081 * max_free up the tree is done by moving the entry to the
1082 * root and making the change there.
1084 if (entry != map->root)
1085 map->root = vm_map_entry_splay(entry->start, map->root);
1087 entry->adj_free = entry->next->start - entry->end;
1088 vm_map_entry_set_max_free(entry);
1092 * vm_map_lookup_entry: [ internal use only ]
1094 * Finds the map entry containing (or
1095 * immediately preceding) the specified address
1096 * in the given map; the entry is returned
1097 * in the "entry" parameter. The boolean
1098 * result indicates whether the address is
1099 * actually contained in the map.
1102 vm_map_lookup_entry(
1104 vm_offset_t address,
1105 vm_map_entry_t *entry) /* OUT */
1111 * If the map is empty, then the map entry immediately preceding
1112 * "address" is the map's header.
1116 *entry = &map->header;
1117 else if (address >= cur->start && cur->end > address) {
1120 } else if ((locked = vm_map_locked(map)) ||
1121 sx_try_upgrade(&map->lock)) {
1123 * Splay requires a write lock on the map. However, it only
1124 * restructures the binary search tree; it does not otherwise
1125 * change the map. Thus, the map's timestamp need not change
1126 * on a temporary upgrade.
1128 map->root = cur = vm_map_entry_splay(address, cur);
1130 sx_downgrade(&map->lock);
1133 * If "address" is contained within a map entry, the new root
1134 * is that map entry. Otherwise, the new root is a map entry
1135 * immediately before or after "address".
1137 if (address >= cur->start) {
1139 if (cur->end > address)
1145 * Since the map is only locked for read access, perform a
1146 * standard binary search tree lookup for "address".
1149 if (address < cur->start) {
1150 if (cur->left == NULL) {
1155 } else if (cur->end > address) {
1159 if (cur->right == NULL) {
1172 * Inserts the given whole VM object into the target
1173 * map at the specified address range. The object's
1174 * size should match that of the address range.
1176 * Requires that the map be locked, and leaves it so.
1178 * If object is non-NULL, ref count must be bumped by caller
1179 * prior to making call to account for the new entry.
1182 vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1183 vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max, int cow)
1185 vm_map_entry_t new_entry, prev_entry, temp_entry;
1187 vm_eflags_t protoeflags;
1188 vm_inherit_t inheritance;
1190 VM_MAP_ASSERT_LOCKED(map);
1191 KASSERT(object != kernel_object ||
1192 (cow & MAP_COPY_ON_WRITE) == 0,
1193 ("vm_map_insert: kernel object and COW"));
1194 KASSERT(object == NULL || (cow & MAP_NOFAULT) == 0,
1195 ("vm_map_insert: paradoxical MAP_NOFAULT request"));
1196 KASSERT((prot & ~max) == 0,
1197 ("prot %#x is not subset of max_prot %#x", prot, max));
1200 * Check that the start and end points are not bogus.
1202 if (start < vm_map_min(map) || end > vm_map_max(map) ||
1204 return (KERN_INVALID_ADDRESS);
1207 * Find the entry prior to the proposed starting address; if it's part
1208 * of an existing entry, this range is bogus.
1210 if (vm_map_lookup_entry(map, start, &temp_entry))
1211 return (KERN_NO_SPACE);
1213 prev_entry = temp_entry;
1216 * Assert that the next entry doesn't overlap the end point.
1218 if (prev_entry->next->start < end)
1219 return (KERN_NO_SPACE);
1221 if ((cow & MAP_CREATE_GUARD) != 0 && (object != NULL ||
1222 max != VM_PROT_NONE))
1223 return (KERN_INVALID_ARGUMENT);
1226 if (cow & MAP_COPY_ON_WRITE)
1227 protoeflags |= MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY;
1228 if (cow & MAP_NOFAULT)
1229 protoeflags |= MAP_ENTRY_NOFAULT;
1230 if (cow & MAP_DISABLE_SYNCER)
1231 protoeflags |= MAP_ENTRY_NOSYNC;
1232 if (cow & MAP_DISABLE_COREDUMP)
1233 protoeflags |= MAP_ENTRY_NOCOREDUMP;
1234 if (cow & MAP_STACK_GROWS_DOWN)
1235 protoeflags |= MAP_ENTRY_GROWS_DOWN;
1236 if (cow & MAP_STACK_GROWS_UP)
1237 protoeflags |= MAP_ENTRY_GROWS_UP;
1238 if (cow & MAP_VN_WRITECOUNT)
1239 protoeflags |= MAP_ENTRY_VN_WRITECNT;
1240 if ((cow & MAP_CREATE_GUARD) != 0)
1241 protoeflags |= MAP_ENTRY_GUARD;
1242 if ((cow & MAP_CREATE_STACK_GAP_DN) != 0)
1243 protoeflags |= MAP_ENTRY_STACK_GAP_DN;
1244 if ((cow & MAP_CREATE_STACK_GAP_UP) != 0)
1245 protoeflags |= MAP_ENTRY_STACK_GAP_UP;
1246 if (cow & MAP_INHERIT_SHARE)
1247 inheritance = VM_INHERIT_SHARE;
1249 inheritance = VM_INHERIT_DEFAULT;
1252 if ((cow & (MAP_ACC_NO_CHARGE | MAP_NOFAULT | MAP_CREATE_GUARD)) != 0)
1254 if ((cow & MAP_ACC_CHARGED) || ((prot & VM_PROT_WRITE) &&
1255 ((protoeflags & MAP_ENTRY_NEEDS_COPY) || object == NULL))) {
1256 if (!(cow & MAP_ACC_CHARGED) && !swap_reserve(end - start))
1257 return (KERN_RESOURCE_SHORTAGE);
1258 KASSERT(object == NULL ||
1259 (protoeflags & MAP_ENTRY_NEEDS_COPY) != 0 ||
1260 object->cred == NULL,
1261 ("overcommit: vm_map_insert o %p", object));
1262 cred = curthread->td_ucred;
1266 /* Expand the kernel pmap, if necessary. */
1267 if (map == kernel_map && end > kernel_vm_end)
1268 pmap_growkernel(end);
1269 if (object != NULL) {
1271 * OBJ_ONEMAPPING must be cleared unless this mapping
1272 * is trivially proven to be the only mapping for any
1273 * of the object's pages. (Object granularity
1274 * reference counting is insufficient to recognize
1275 * aliases with precision.)
1277 VM_OBJECT_WLOCK(object);
1278 if (object->ref_count > 1 || object->shadow_count != 0)
1279 vm_object_clear_flag(object, OBJ_ONEMAPPING);
1280 VM_OBJECT_WUNLOCK(object);
1281 } else if ((prev_entry->eflags & ~MAP_ENTRY_USER_WIRED) ==
1283 (cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0 &&
1284 prev_entry->end == start && (prev_entry->cred == cred ||
1285 (prev_entry->object.vm_object != NULL &&
1286 prev_entry->object.vm_object->cred == cred)) &&
1287 vm_object_coalesce(prev_entry->object.vm_object,
1289 (vm_size_t)(prev_entry->end - prev_entry->start),
1290 (vm_size_t)(end - prev_entry->end), cred != NULL &&
1291 (protoeflags & MAP_ENTRY_NEEDS_COPY) == 0)) {
1293 * We were able to extend the object. Determine if we
1294 * can extend the previous map entry to include the
1295 * new range as well.
1297 if (prev_entry->inheritance == inheritance &&
1298 prev_entry->protection == prot &&
1299 prev_entry->max_protection == max &&
1300 prev_entry->wired_count == 0) {
1301 KASSERT((prev_entry->eflags & MAP_ENTRY_USER_WIRED) ==
1302 0, ("prev_entry %p has incoherent wiring",
1304 if ((prev_entry->eflags & MAP_ENTRY_GUARD) == 0)
1305 map->size += end - prev_entry->end;
1306 prev_entry->end = end;
1307 vm_map_entry_resize_free(map, prev_entry);
1308 vm_map_simplify_entry(map, prev_entry);
1309 return (KERN_SUCCESS);
1313 * If we can extend the object but cannot extend the
1314 * map entry, we have to create a new map entry. We
1315 * must bump the ref count on the extended object to
1316 * account for it. object may be NULL.
1318 object = prev_entry->object.vm_object;
1319 offset = prev_entry->offset +
1320 (prev_entry->end - prev_entry->start);
1321 vm_object_reference(object);
1322 if (cred != NULL && object != NULL && object->cred != NULL &&
1323 !(prev_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
1324 /* Object already accounts for this uid. */
1332 * Create a new entry
1334 new_entry = vm_map_entry_create(map);
1335 new_entry->start = start;
1336 new_entry->end = end;
1337 new_entry->cred = NULL;
1339 new_entry->eflags = protoeflags;
1340 new_entry->object.vm_object = object;
1341 new_entry->offset = offset;
1343 new_entry->inheritance = inheritance;
1344 new_entry->protection = prot;
1345 new_entry->max_protection = max;
1346 new_entry->wired_count = 0;
1347 new_entry->wiring_thread = NULL;
1348 new_entry->read_ahead = VM_FAULT_READ_AHEAD_INIT;
1349 new_entry->next_read = start;
1351 KASSERT(cred == NULL || !ENTRY_CHARGED(new_entry),
1352 ("overcommit: vm_map_insert leaks vm_map %p", new_entry));
1353 new_entry->cred = cred;
1356 * Insert the new entry into the list
1358 vm_map_entry_link(map, prev_entry, new_entry);
1359 if ((new_entry->eflags & MAP_ENTRY_GUARD) == 0)
1360 map->size += new_entry->end - new_entry->start;
1363 * Try to coalesce the new entry with both the previous and next
1364 * entries in the list. Previously, we only attempted to coalesce
1365 * with the previous entry when object is NULL. Here, we handle the
1366 * other cases, which are less common.
1368 vm_map_simplify_entry(map, new_entry);
1370 if ((cow & (MAP_PREFAULT | MAP_PREFAULT_PARTIAL)) != 0) {
1371 vm_map_pmap_enter(map, start, prot, object, OFF_TO_IDX(offset),
1372 end - start, cow & MAP_PREFAULT_PARTIAL);
1375 return (KERN_SUCCESS);
1381 * Find the first fit (lowest VM address) for "length" free bytes
1382 * beginning at address >= start in the given map.
1384 * In a vm_map_entry, "adj_free" is the amount of free space
1385 * adjacent (higher address) to this entry, and "max_free" is the
1386 * maximum amount of contiguous free space in its subtree. This
1387 * allows finding a free region in one path down the tree, so
1388 * O(log n) amortized with splay trees.
1390 * The map must be locked, and leaves it so.
1392 * Returns: 0 on success, and starting address in *addr,
1393 * 1 if insufficient space.
1396 vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length,
1397 vm_offset_t *addr) /* OUT */
1399 vm_map_entry_t entry;
1403 * Request must fit within min/max VM address and must avoid
1406 start = MAX(start, vm_map_min(map));
1407 if (start + length > vm_map_max(map) || start + length < start)
1410 /* Empty tree means wide open address space. */
1411 if (map->root == NULL) {
1417 * After splay, if start comes before root node, then there
1418 * must be a gap from start to the root.
1420 map->root = vm_map_entry_splay(start, map->root);
1421 if (start + length <= map->root->start) {
1427 * Root is the last node that might begin its gap before
1428 * start, and this is the last comparison where address
1429 * wrap might be a problem.
1431 st = (start > map->root->end) ? start : map->root->end;
1432 if (length <= map->root->end + map->root->adj_free - st) {
1437 /* With max_free, can immediately tell if no solution. */
1438 entry = map->root->right;
1439 if (entry == NULL || length > entry->max_free)
1443 * Search the right subtree in the order: left subtree, root,
1444 * right subtree (first fit). The previous splay implies that
1445 * all regions in the right subtree have addresses > start.
1447 while (entry != NULL) {
1448 if (entry->left != NULL && entry->left->max_free >= length)
1449 entry = entry->left;
1450 else if (entry->adj_free >= length) {
1454 entry = entry->right;
1457 /* Can't get here, so panic if we do. */
1458 panic("vm_map_findspace: max_free corrupt");
1462 vm_map_fixed(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1463 vm_offset_t start, vm_size_t length, vm_prot_t prot,
1464 vm_prot_t max, int cow)
1469 end = start + length;
1470 KASSERT((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0 ||
1472 ("vm_map_fixed: non-NULL backing object for stack"));
1474 VM_MAP_RANGE_CHECK(map, start, end);
1475 if ((cow & MAP_CHECK_EXCL) == 0)
1476 vm_map_delete(map, start, end);
1477 if ((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) != 0) {
1478 result = vm_map_stack_locked(map, start, length, sgrowsiz,
1481 result = vm_map_insert(map, object, offset, start, end,
1489 * Searches for the specified amount of free space in the given map with the
1490 * specified alignment. Performs an address-ordered, first-fit search from
1491 * the given address "*addr", with an optional upper bound "max_addr". If the
1492 * parameter "alignment" is zero, then the alignment is computed from the
1493 * given (object, offset) pair so as to enable the greatest possible use of
1494 * superpage mappings. Returns KERN_SUCCESS and the address of the free space
1495 * in "*addr" if successful. Otherwise, returns KERN_NO_SPACE.
1497 * The map must be locked. Initially, there must be at least "length" bytes
1498 * of free space at the given address.
1501 vm_map_alignspace(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1502 vm_offset_t *addr, vm_size_t length, vm_offset_t max_addr,
1503 vm_offset_t alignment)
1505 vm_offset_t aligned_addr, free_addr;
1507 VM_MAP_ASSERT_LOCKED(map);
1509 KASSERT(!vm_map_findspace(map, free_addr, length, addr) &&
1510 free_addr == *addr, ("caller provided insufficient free space"));
1513 * At the start of every iteration, the free space at address
1514 * "*addr" is at least "length" bytes.
1517 pmap_align_superpage(object, offset, addr, length);
1518 else if ((*addr & (alignment - 1)) != 0) {
1519 *addr &= ~(alignment - 1);
1522 aligned_addr = *addr;
1523 if (aligned_addr == free_addr) {
1525 * Alignment did not change "*addr", so "*addr" must
1526 * still provide sufficient free space.
1528 return (KERN_SUCCESS);
1532 * Test for address wrap on "*addr". A wrapped "*addr" could
1533 * be a valid address, in which case vm_map_findspace() cannot
1534 * be relied upon to fail.
1536 if (aligned_addr < free_addr ||
1537 vm_map_findspace(map, aligned_addr, length, addr) ||
1538 (max_addr != 0 && *addr + length > max_addr))
1539 return (KERN_NO_SPACE);
1541 if (free_addr == aligned_addr) {
1543 * If a successful call to vm_map_findspace() did not
1544 * change "*addr", then "*addr" must still be aligned
1545 * and provide sufficient free space.
1547 return (KERN_SUCCESS);
1553 * vm_map_find finds an unallocated region in the target address
1554 * map with the given length. The search is defined to be
1555 * first-fit from the specified address; the region found is
1556 * returned in the same parameter.
1558 * If object is non-NULL, ref count must be bumped by caller
1559 * prior to making call to account for the new entry.
1562 vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1563 vm_offset_t *addr, /* IN/OUT */
1564 vm_size_t length, vm_offset_t max_addr, int find_space,
1565 vm_prot_t prot, vm_prot_t max, int cow)
1567 vm_offset_t alignment, min_addr;
1570 KASSERT((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0 ||
1572 ("vm_map_find: non-NULL backing object for stack"));
1573 if (find_space == VMFS_OPTIMAL_SPACE && (object == NULL ||
1574 (object->flags & OBJ_COLORED) == 0))
1575 find_space = VMFS_ANY_SPACE;
1576 if (find_space >> 8 != 0) {
1577 KASSERT((find_space & 0xff) == 0, ("bad VMFS flags"));
1578 alignment = (vm_offset_t)1 << (find_space >> 8);
1582 if (find_space != VMFS_NO_SPACE) {
1583 KASSERT(find_space == VMFS_ANY_SPACE ||
1584 find_space == VMFS_OPTIMAL_SPACE ||
1585 find_space == VMFS_SUPER_SPACE ||
1586 alignment != 0, ("unexpected VMFS flag"));
1589 if (vm_map_findspace(map, min_addr, length, addr) ||
1590 (max_addr != 0 && *addr + length > max_addr)) {
1594 if (find_space != VMFS_ANY_SPACE &&
1595 (rv = vm_map_alignspace(map, object, offset, addr, length,
1596 max_addr, alignment)) != KERN_SUCCESS) {
1597 if (find_space == VMFS_OPTIMAL_SPACE) {
1598 find_space = VMFS_ANY_SPACE;
1604 if ((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) != 0) {
1605 rv = vm_map_stack_locked(map, *addr, length, sgrowsiz, prot,
1608 rv = vm_map_insert(map, object, offset, *addr, *addr + length,
1617 * vm_map_find_min() is a variant of vm_map_find() that takes an
1618 * additional parameter (min_addr) and treats the given address
1619 * (*addr) differently. Specifically, it treats *addr as a hint
1620 * and not as the minimum address where the mapping is created.
1622 * This function works in two phases. First, it tries to
1623 * allocate above the hint. If that fails and the hint is
1624 * greater than min_addr, it performs a second pass, replacing
1625 * the hint with min_addr as the minimum address for the
1629 vm_map_find_min(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1630 vm_offset_t *addr, vm_size_t length, vm_offset_t min_addr,
1631 vm_offset_t max_addr, int find_space, vm_prot_t prot, vm_prot_t max,
1639 rv = vm_map_find(map, object, offset, addr, length, max_addr,
1640 find_space, prot, max, cow);
1641 if (rv == KERN_SUCCESS || min_addr >= hint)
1643 *addr = hint = min_addr;
1648 * A map entry with any of the following flags set must not be merged with
1651 #define MAP_ENTRY_NOMERGE_MASK (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP | \
1652 MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP)
1655 vm_map_mergeable_neighbors(vm_map_entry_t prev, vm_map_entry_t entry)
1658 KASSERT((prev->eflags & MAP_ENTRY_NOMERGE_MASK) == 0 ||
1659 (entry->eflags & MAP_ENTRY_NOMERGE_MASK) == 0,
1660 ("vm_map_mergeable_neighbors: neither %p nor %p are mergeable",
1662 return (prev->end == entry->start &&
1663 prev->object.vm_object == entry->object.vm_object &&
1664 (prev->object.vm_object == NULL ||
1665 prev->offset + (prev->end - prev->start) == entry->offset) &&
1666 prev->eflags == entry->eflags &&
1667 prev->protection == entry->protection &&
1668 prev->max_protection == entry->max_protection &&
1669 prev->inheritance == entry->inheritance &&
1670 prev->wired_count == entry->wired_count &&
1671 prev->cred == entry->cred);
1675 vm_map_merged_neighbor_dispose(vm_map_t map, vm_map_entry_t entry)
1679 * If the backing object is a vnode object, vm_object_deallocate()
1680 * calls vrele(). However, vrele() does not lock the vnode because
1681 * the vnode has additional references. Thus, the map lock can be
1682 * kept without causing a lock-order reversal with the vnode lock.
1684 * Since we count the number of virtual page mappings in
1685 * object->un_pager.vnp.writemappings, the writemappings value
1686 * should not be adjusted when the entry is disposed of.
1688 if (entry->object.vm_object != NULL)
1689 vm_object_deallocate(entry->object.vm_object);
1690 if (entry->cred != NULL)
1691 crfree(entry->cred);
1692 vm_map_entry_dispose(map, entry);
1696 * vm_map_simplify_entry:
1698 * Simplify the given map entry by merging with either neighbor. This
1699 * routine also has the ability to merge with both neighbors.
1701 * The map must be locked.
1703 * This routine guarantees that the passed entry remains valid (though
1704 * possibly extended). When merging, this routine may delete one or
1708 vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry)
1710 vm_map_entry_t next, prev;
1712 if ((entry->eflags & MAP_ENTRY_NOMERGE_MASK) != 0)
1715 if (vm_map_mergeable_neighbors(prev, entry)) {
1716 vm_map_entry_unlink(map, prev);
1717 entry->start = prev->start;
1718 entry->offset = prev->offset;
1719 if (entry->prev != &map->header)
1720 vm_map_entry_resize_free(map, entry->prev);
1721 vm_map_merged_neighbor_dispose(map, prev);
1724 if (vm_map_mergeable_neighbors(entry, next)) {
1725 vm_map_entry_unlink(map, next);
1726 entry->end = next->end;
1727 vm_map_entry_resize_free(map, entry);
1728 vm_map_merged_neighbor_dispose(map, next);
1733 * vm_map_clip_start: [ internal use only ]
1735 * Asserts that the given entry begins at or after
1736 * the specified address; if necessary,
1737 * it splits the entry into two.
1739 #define vm_map_clip_start(map, entry, startaddr) \
1741 if (startaddr > entry->start) \
1742 _vm_map_clip_start(map, entry, startaddr); \
1746 * This routine is called only when it is known that
1747 * the entry must be split.
1750 _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start)
1752 vm_map_entry_t new_entry;
1754 VM_MAP_ASSERT_LOCKED(map);
1755 KASSERT(entry->end > start && entry->start < start,
1756 ("_vm_map_clip_start: invalid clip of entry %p", entry));
1759 * Split off the front portion -- note that we must insert the new
1760 * entry BEFORE this one, so that this entry has the specified
1763 vm_map_simplify_entry(map, entry);
1766 * If there is no object backing this entry, we might as well create
1767 * one now. If we defer it, an object can get created after the map
1768 * is clipped, and individual objects will be created for the split-up
1769 * map. This is a bit of a hack, but is also about the best place to
1770 * put this improvement.
1772 if (entry->object.vm_object == NULL && !map->system_map &&
1773 (entry->eflags & MAP_ENTRY_GUARD) == 0) {
1775 object = vm_object_allocate(OBJT_DEFAULT,
1776 atop(entry->end - entry->start));
1777 entry->object.vm_object = object;
1779 if (entry->cred != NULL) {
1780 object->cred = entry->cred;
1781 object->charge = entry->end - entry->start;
1784 } else if (entry->object.vm_object != NULL &&
1785 ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) &&
1786 entry->cred != NULL) {
1787 VM_OBJECT_WLOCK(entry->object.vm_object);
1788 KASSERT(entry->object.vm_object->cred == NULL,
1789 ("OVERCOMMIT: vm_entry_clip_start: both cred e %p", entry));
1790 entry->object.vm_object->cred = entry->cred;
1791 entry->object.vm_object->charge = entry->end - entry->start;
1792 VM_OBJECT_WUNLOCK(entry->object.vm_object);
1796 new_entry = vm_map_entry_create(map);
1797 *new_entry = *entry;
1799 new_entry->end = start;
1800 entry->offset += (start - entry->start);
1801 entry->start = start;
1802 if (new_entry->cred != NULL)
1803 crhold(entry->cred);
1805 vm_map_entry_link(map, entry->prev, new_entry);
1807 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1808 vm_object_reference(new_entry->object.vm_object);
1810 * The object->un_pager.vnp.writemappings for the
1811 * object of MAP_ENTRY_VN_WRITECNT type entry shall be
1812 * kept as is here. The virtual pages are
1813 * re-distributed among the clipped entries, so the sum is
1820 * vm_map_clip_end: [ internal use only ]
1822 * Asserts that the given entry ends at or before
1823 * the specified address; if necessary,
1824 * it splits the entry into two.
1826 #define vm_map_clip_end(map, entry, endaddr) \
1828 if ((endaddr) < (entry->end)) \
1829 _vm_map_clip_end((map), (entry), (endaddr)); \
1833 * This routine is called only when it is known that
1834 * the entry must be split.
1837 _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end)
1839 vm_map_entry_t new_entry;
1841 VM_MAP_ASSERT_LOCKED(map);
1842 KASSERT(entry->start < end && entry->end > end,
1843 ("_vm_map_clip_end: invalid clip of entry %p", entry));
1846 * If there is no object backing this entry, we might as well create
1847 * one now. If we defer it, an object can get created after the map
1848 * is clipped, and individual objects will be created for the split-up
1849 * map. This is a bit of a hack, but is also about the best place to
1850 * put this improvement.
1852 if (entry->object.vm_object == NULL && !map->system_map &&
1853 (entry->eflags & MAP_ENTRY_GUARD) == 0) {
1855 object = vm_object_allocate(OBJT_DEFAULT,
1856 atop(entry->end - entry->start));
1857 entry->object.vm_object = object;
1859 if (entry->cred != NULL) {
1860 object->cred = entry->cred;
1861 object->charge = entry->end - entry->start;
1864 } else if (entry->object.vm_object != NULL &&
1865 ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) &&
1866 entry->cred != NULL) {
1867 VM_OBJECT_WLOCK(entry->object.vm_object);
1868 KASSERT(entry->object.vm_object->cred == NULL,
1869 ("OVERCOMMIT: vm_entry_clip_end: both cred e %p", entry));
1870 entry->object.vm_object->cred = entry->cred;
1871 entry->object.vm_object->charge = entry->end - entry->start;
1872 VM_OBJECT_WUNLOCK(entry->object.vm_object);
1877 * Create a new entry and insert it AFTER the specified entry
1879 new_entry = vm_map_entry_create(map);
1880 *new_entry = *entry;
1882 new_entry->start = entry->end = end;
1883 new_entry->offset += (end - entry->start);
1884 if (new_entry->cred != NULL)
1885 crhold(entry->cred);
1887 vm_map_entry_link(map, entry, new_entry);
1889 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1890 vm_object_reference(new_entry->object.vm_object);
1895 * vm_map_submap: [ kernel use only ]
1897 * Mark the given range as handled by a subordinate map.
1899 * This range must have been created with vm_map_find,
1900 * and no other operations may have been performed on this
1901 * range prior to calling vm_map_submap.
1903 * Only a limited number of operations can be performed
1904 * within this rage after calling vm_map_submap:
1906 * [Don't try vm_map_copy!]
1908 * To remove a submapping, one must first remove the
1909 * range from the superior map, and then destroy the
1910 * submap (if desired). [Better yet, don't try it.]
1919 vm_map_entry_t entry;
1920 int result = KERN_INVALID_ARGUMENT;
1924 VM_MAP_RANGE_CHECK(map, start, end);
1926 if (vm_map_lookup_entry(map, start, &entry)) {
1927 vm_map_clip_start(map, entry, start);
1929 entry = entry->next;
1931 vm_map_clip_end(map, entry, end);
1933 if ((entry->start == start) && (entry->end == end) &&
1934 ((entry->eflags & MAP_ENTRY_COW) == 0) &&
1935 (entry->object.vm_object == NULL)) {
1936 entry->object.sub_map = submap;
1937 entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
1938 result = KERN_SUCCESS;
1946 * The maximum number of pages to map if MAP_PREFAULT_PARTIAL is specified
1948 #define MAX_INIT_PT 96
1951 * vm_map_pmap_enter:
1953 * Preload the specified map's pmap with mappings to the specified
1954 * object's memory-resident pages. No further physical pages are
1955 * allocated, and no further virtual pages are retrieved from secondary
1956 * storage. If the specified flags include MAP_PREFAULT_PARTIAL, then a
1957 * limited number of page mappings are created at the low-end of the
1958 * specified address range. (For this purpose, a superpage mapping
1959 * counts as one page mapping.) Otherwise, all resident pages within
1960 * the specified address range are mapped.
1963 vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
1964 vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags)
1967 vm_page_t p, p_start;
1968 vm_pindex_t mask, psize, threshold, tmpidx;
1970 if ((prot & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 || object == NULL)
1972 VM_OBJECT_RLOCK(object);
1973 if (object->type == OBJT_DEVICE || object->type == OBJT_SG) {
1974 VM_OBJECT_RUNLOCK(object);
1975 VM_OBJECT_WLOCK(object);
1976 if (object->type == OBJT_DEVICE || object->type == OBJT_SG) {
1977 pmap_object_init_pt(map->pmap, addr, object, pindex,
1979 VM_OBJECT_WUNLOCK(object);
1982 VM_OBJECT_LOCK_DOWNGRADE(object);
1986 if (psize + pindex > object->size) {
1987 if (object->size < pindex) {
1988 VM_OBJECT_RUNLOCK(object);
1991 psize = object->size - pindex;
1996 threshold = MAX_INIT_PT;
1998 p = vm_page_find_least(object, pindex);
2000 * Assert: the variable p is either (1) the page with the
2001 * least pindex greater than or equal to the parameter pindex
2005 p != NULL && (tmpidx = p->pindex - pindex) < psize;
2006 p = TAILQ_NEXT(p, listq)) {
2008 * don't allow an madvise to blow away our really
2009 * free pages allocating pv entries.
2011 if (((flags & MAP_PREFAULT_MADVISE) != 0 &&
2012 vm_page_count_severe()) ||
2013 ((flags & MAP_PREFAULT_PARTIAL) != 0 &&
2014 tmpidx >= threshold)) {
2018 if (p->valid == VM_PAGE_BITS_ALL) {
2019 if (p_start == NULL) {
2020 start = addr + ptoa(tmpidx);
2023 /* Jump ahead if a superpage mapping is possible. */
2024 if (p->psind > 0 && ((addr + ptoa(tmpidx)) &
2025 (pagesizes[p->psind] - 1)) == 0) {
2026 mask = atop(pagesizes[p->psind]) - 1;
2027 if (tmpidx + mask < psize &&
2028 vm_page_ps_test(p, PS_ALL_VALID, NULL)) {
2033 } else if (p_start != NULL) {
2034 pmap_enter_object(map->pmap, start, addr +
2035 ptoa(tmpidx), p_start, prot);
2039 if (p_start != NULL)
2040 pmap_enter_object(map->pmap, start, addr + ptoa(psize),
2042 VM_OBJECT_RUNLOCK(object);
2048 * Sets the protection of the specified address
2049 * region in the target map. If "set_max" is
2050 * specified, the maximum protection is to be set;
2051 * otherwise, only the current protection is affected.
2054 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
2055 vm_prot_t new_prot, boolean_t set_max)
2057 vm_map_entry_t current, entry;
2063 return (KERN_SUCCESS);
2068 * Ensure that we are not concurrently wiring pages. vm_map_wire() may
2069 * need to fault pages into the map and will drop the map lock while
2070 * doing so, and the VM object may end up in an inconsistent state if we
2071 * update the protection on the map entry in between faults.
2073 vm_map_wait_busy(map);
2075 VM_MAP_RANGE_CHECK(map, start, end);
2077 if (vm_map_lookup_entry(map, start, &entry)) {
2078 vm_map_clip_start(map, entry, start);
2080 entry = entry->next;
2084 * Make a first pass to check for protection violations.
2086 for (current = entry; current->start < end; current = current->next) {
2087 if ((current->eflags & MAP_ENTRY_GUARD) != 0)
2089 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
2091 return (KERN_INVALID_ARGUMENT);
2093 if ((new_prot & current->max_protection) != new_prot) {
2095 return (KERN_PROTECTION_FAILURE);
2100 * Do an accounting pass for private read-only mappings that
2101 * now will do cow due to allowed write (e.g. debugger sets
2102 * breakpoint on text segment)
2104 for (current = entry; current->start < end; current = current->next) {
2106 vm_map_clip_end(map, current, end);
2109 ((new_prot & ~(current->protection)) & VM_PROT_WRITE) == 0 ||
2110 ENTRY_CHARGED(current) ||
2111 (current->eflags & MAP_ENTRY_GUARD) != 0) {
2115 cred = curthread->td_ucred;
2116 obj = current->object.vm_object;
2118 if (obj == NULL || (current->eflags & MAP_ENTRY_NEEDS_COPY)) {
2119 if (!swap_reserve(current->end - current->start)) {
2121 return (KERN_RESOURCE_SHORTAGE);
2124 current->cred = cred;
2128 VM_OBJECT_WLOCK(obj);
2129 if (obj->type != OBJT_DEFAULT && obj->type != OBJT_SWAP) {
2130 VM_OBJECT_WUNLOCK(obj);
2135 * Charge for the whole object allocation now, since
2136 * we cannot distinguish between non-charged and
2137 * charged clipped mapping of the same object later.
2139 KASSERT(obj->charge == 0,
2140 ("vm_map_protect: object %p overcharged (entry %p)",
2142 if (!swap_reserve(ptoa(obj->size))) {
2143 VM_OBJECT_WUNLOCK(obj);
2145 return (KERN_RESOURCE_SHORTAGE);
2150 obj->charge = ptoa(obj->size);
2151 VM_OBJECT_WUNLOCK(obj);
2155 * Go back and fix up protections. [Note that clipping is not
2156 * necessary the second time.]
2158 for (current = entry; current->start < end; current = current->next) {
2159 if ((current->eflags & MAP_ENTRY_GUARD) != 0)
2162 old_prot = current->protection;
2165 current->protection =
2166 (current->max_protection = new_prot) &
2169 current->protection = new_prot;
2172 * For user wired map entries, the normal lazy evaluation of
2173 * write access upgrades through soft page faults is
2174 * undesirable. Instead, immediately copy any pages that are
2175 * copy-on-write and enable write access in the physical map.
2177 if ((current->eflags & MAP_ENTRY_USER_WIRED) != 0 &&
2178 (current->protection & VM_PROT_WRITE) != 0 &&
2179 (old_prot & VM_PROT_WRITE) == 0)
2180 vm_fault_copy_entry(map, map, current, current, NULL);
2183 * When restricting access, update the physical map. Worry
2184 * about copy-on-write here.
2186 if ((old_prot & ~current->protection) != 0) {
2187 #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
2189 pmap_protect(map->pmap, current->start,
2191 current->protection & MASK(current));
2194 vm_map_simplify_entry(map, current);
2197 return (KERN_SUCCESS);
2203 * This routine traverses a processes map handling the madvise
2204 * system call. Advisories are classified as either those effecting
2205 * the vm_map_entry structure, or those effecting the underlying
2215 vm_map_entry_t current, entry;
2219 * Some madvise calls directly modify the vm_map_entry, in which case
2220 * we need to use an exclusive lock on the map and we need to perform
2221 * various clipping operations. Otherwise we only need a read-lock
2226 case MADV_SEQUENTIAL:
2243 vm_map_lock_read(map);
2250 * Locate starting entry and clip if necessary.
2252 VM_MAP_RANGE_CHECK(map, start, end);
2254 if (vm_map_lookup_entry(map, start, &entry)) {
2256 vm_map_clip_start(map, entry, start);
2258 entry = entry->next;
2263 * madvise behaviors that are implemented in the vm_map_entry.
2265 * We clip the vm_map_entry so that behavioral changes are
2266 * limited to the specified address range.
2268 for (current = entry; current->start < end;
2269 current = current->next) {
2270 if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
2273 vm_map_clip_end(map, current, end);
2277 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
2279 case MADV_SEQUENTIAL:
2280 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
2283 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
2286 current->eflags |= MAP_ENTRY_NOSYNC;
2289 current->eflags &= ~MAP_ENTRY_NOSYNC;
2292 current->eflags |= MAP_ENTRY_NOCOREDUMP;
2295 current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
2300 vm_map_simplify_entry(map, current);
2304 vm_pindex_t pstart, pend;
2307 * madvise behaviors that are implemented in the underlying
2310 * Since we don't clip the vm_map_entry, we have to clip
2311 * the vm_object pindex and count.
2313 for (current = entry; current->start < end;
2314 current = current->next) {
2315 vm_offset_t useEnd, useStart;
2317 if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
2320 pstart = OFF_TO_IDX(current->offset);
2321 pend = pstart + atop(current->end - current->start);
2322 useStart = current->start;
2323 useEnd = current->end;
2325 if (current->start < start) {
2326 pstart += atop(start - current->start);
2329 if (current->end > end) {
2330 pend -= atop(current->end - end);
2338 * Perform the pmap_advise() before clearing
2339 * PGA_REFERENCED in vm_page_advise(). Otherwise, a
2340 * concurrent pmap operation, such as pmap_remove(),
2341 * could clear a reference in the pmap and set
2342 * PGA_REFERENCED on the page before the pmap_advise()
2343 * had completed. Consequently, the page would appear
2344 * referenced based upon an old reference that
2345 * occurred before this pmap_advise() ran.
2347 if (behav == MADV_DONTNEED || behav == MADV_FREE)
2348 pmap_advise(map->pmap, useStart, useEnd,
2351 vm_object_madvise(current->object.vm_object, pstart,
2355 * Pre-populate paging structures in the
2356 * WILLNEED case. For wired entries, the
2357 * paging structures are already populated.
2359 if (behav == MADV_WILLNEED &&
2360 current->wired_count == 0) {
2361 vm_map_pmap_enter(map,
2363 current->protection,
2364 current->object.vm_object,
2366 ptoa(pend - pstart),
2367 MAP_PREFAULT_MADVISE
2371 vm_map_unlock_read(map);
2380 * Sets the inheritance of the specified address
2381 * range in the target map. Inheritance
2382 * affects how the map will be shared with
2383 * child maps at the time of vmspace_fork.
2386 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
2387 vm_inherit_t new_inheritance)
2389 vm_map_entry_t entry;
2390 vm_map_entry_t temp_entry;
2392 switch (new_inheritance) {
2393 case VM_INHERIT_NONE:
2394 case VM_INHERIT_COPY:
2395 case VM_INHERIT_SHARE:
2396 case VM_INHERIT_ZERO:
2399 return (KERN_INVALID_ARGUMENT);
2402 return (KERN_SUCCESS);
2404 VM_MAP_RANGE_CHECK(map, start, end);
2405 if (vm_map_lookup_entry(map, start, &temp_entry)) {
2407 vm_map_clip_start(map, entry, start);
2409 entry = temp_entry->next;
2410 while (entry->start < end) {
2411 vm_map_clip_end(map, entry, end);
2412 if ((entry->eflags & MAP_ENTRY_GUARD) == 0 ||
2413 new_inheritance != VM_INHERIT_ZERO)
2414 entry->inheritance = new_inheritance;
2415 vm_map_simplify_entry(map, entry);
2416 entry = entry->next;
2419 return (KERN_SUCCESS);
2425 * Implements both kernel and user unwiring.
2428 vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
2431 vm_map_entry_t entry, first_entry, tmp_entry;
2432 vm_offset_t saved_start;
2433 unsigned int last_timestamp;
2435 boolean_t need_wakeup, result, user_unwire;
2438 return (KERN_SUCCESS);
2439 user_unwire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
2441 VM_MAP_RANGE_CHECK(map, start, end);
2442 if (!vm_map_lookup_entry(map, start, &first_entry)) {
2443 if (flags & VM_MAP_WIRE_HOLESOK)
2444 first_entry = first_entry->next;
2447 return (KERN_INVALID_ADDRESS);
2450 last_timestamp = map->timestamp;
2451 entry = first_entry;
2452 while (entry->start < end) {
2453 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
2455 * We have not yet clipped the entry.
2457 saved_start = (start >= entry->start) ? start :
2459 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2460 if (vm_map_unlock_and_wait(map, 0)) {
2462 * Allow interruption of user unwiring?
2466 if (last_timestamp+1 != map->timestamp) {
2468 * Look again for the entry because the map was
2469 * modified while it was unlocked.
2470 * Specifically, the entry may have been
2471 * clipped, merged, or deleted.
2473 if (!vm_map_lookup_entry(map, saved_start,
2475 if (flags & VM_MAP_WIRE_HOLESOK)
2476 tmp_entry = tmp_entry->next;
2478 if (saved_start == start) {
2480 * First_entry has been deleted.
2483 return (KERN_INVALID_ADDRESS);
2486 rv = KERN_INVALID_ADDRESS;
2490 if (entry == first_entry)
2491 first_entry = tmp_entry;
2496 last_timestamp = map->timestamp;
2499 vm_map_clip_start(map, entry, start);
2500 vm_map_clip_end(map, entry, end);
2502 * Mark the entry in case the map lock is released. (See
2505 KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 &&
2506 entry->wiring_thread == NULL,
2507 ("owned map entry %p", entry));
2508 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
2509 entry->wiring_thread = curthread;
2511 * Check the map for holes in the specified region.
2512 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
2514 if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
2515 (entry->end < end && entry->next->start > entry->end)) {
2517 rv = KERN_INVALID_ADDRESS;
2521 * If system unwiring, require that the entry is system wired.
2524 vm_map_entry_system_wired_count(entry) == 0) {
2526 rv = KERN_INVALID_ARGUMENT;
2529 entry = entry->next;
2533 need_wakeup = FALSE;
2534 if (first_entry == NULL) {
2535 result = vm_map_lookup_entry(map, start, &first_entry);
2536 if (!result && (flags & VM_MAP_WIRE_HOLESOK))
2537 first_entry = first_entry->next;
2539 KASSERT(result, ("vm_map_unwire: lookup failed"));
2541 for (entry = first_entry; entry->start < end; entry = entry->next) {
2543 * If VM_MAP_WIRE_HOLESOK was specified, an empty
2544 * space in the unwired region could have been mapped
2545 * while the map lock was dropped for draining
2546 * MAP_ENTRY_IN_TRANSITION. Moreover, another thread
2547 * could be simultaneously wiring this new mapping
2548 * entry. Detect these cases and skip any entries
2549 * marked as in transition by us.
2551 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 ||
2552 entry->wiring_thread != curthread) {
2553 KASSERT((flags & VM_MAP_WIRE_HOLESOK) != 0,
2554 ("vm_map_unwire: !HOLESOK and new/changed entry"));
2558 if (rv == KERN_SUCCESS && (!user_unwire ||
2559 (entry->eflags & MAP_ENTRY_USER_WIRED))) {
2561 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2562 if (entry->wired_count == 1)
2563 vm_map_entry_unwire(map, entry);
2565 entry->wired_count--;
2567 KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0,
2568 ("vm_map_unwire: in-transition flag missing %p", entry));
2569 KASSERT(entry->wiring_thread == curthread,
2570 ("vm_map_unwire: alien wire %p", entry));
2571 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
2572 entry->wiring_thread = NULL;
2573 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
2574 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
2577 vm_map_simplify_entry(map, entry);
2586 * vm_map_wire_entry_failure:
2588 * Handle a wiring failure on the given entry.
2590 * The map should be locked.
2593 vm_map_wire_entry_failure(vm_map_t map, vm_map_entry_t entry,
2594 vm_offset_t failed_addr)
2597 VM_MAP_ASSERT_LOCKED(map);
2598 KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 &&
2599 entry->wired_count == 1,
2600 ("vm_map_wire_entry_failure: entry %p isn't being wired", entry));
2601 KASSERT(failed_addr < entry->end,
2602 ("vm_map_wire_entry_failure: entry %p was fully wired", entry));
2605 * If any pages at the start of this entry were successfully wired,
2608 if (failed_addr > entry->start) {
2609 pmap_unwire(map->pmap, entry->start, failed_addr);
2610 vm_object_unwire(entry->object.vm_object, entry->offset,
2611 failed_addr - entry->start, PQ_ACTIVE);
2615 * Assign an out-of-range value to represent the failure to wire this
2618 entry->wired_count = -1;
2624 * Implements both kernel and user wiring.
2627 vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
2630 vm_map_entry_t entry, first_entry, tmp_entry;
2631 vm_offset_t faddr, saved_end, saved_start;
2632 unsigned int last_timestamp;
2634 boolean_t need_wakeup, result, user_wire;
2638 return (KERN_SUCCESS);
2640 if (flags & VM_MAP_WIRE_WRITE)
2641 prot |= VM_PROT_WRITE;
2642 user_wire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
2644 VM_MAP_RANGE_CHECK(map, start, end);
2645 if (!vm_map_lookup_entry(map, start, &first_entry)) {
2646 if (flags & VM_MAP_WIRE_HOLESOK)
2647 first_entry = first_entry->next;
2650 return (KERN_INVALID_ADDRESS);
2653 last_timestamp = map->timestamp;
2654 entry = first_entry;
2655 while (entry->start < end) {
2656 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
2658 * We have not yet clipped the entry.
2660 saved_start = (start >= entry->start) ? start :
2662 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2663 if (vm_map_unlock_and_wait(map, 0)) {
2665 * Allow interruption of user wiring?
2669 if (last_timestamp + 1 != map->timestamp) {
2671 * Look again for the entry because the map was
2672 * modified while it was unlocked.
2673 * Specifically, the entry may have been
2674 * clipped, merged, or deleted.
2676 if (!vm_map_lookup_entry(map, saved_start,
2678 if (flags & VM_MAP_WIRE_HOLESOK)
2679 tmp_entry = tmp_entry->next;
2681 if (saved_start == start) {
2683 * first_entry has been deleted.
2686 return (KERN_INVALID_ADDRESS);
2689 rv = KERN_INVALID_ADDRESS;
2693 if (entry == first_entry)
2694 first_entry = tmp_entry;
2699 last_timestamp = map->timestamp;
2702 vm_map_clip_start(map, entry, start);
2703 vm_map_clip_end(map, entry, end);
2705 * Mark the entry in case the map lock is released. (See
2708 KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 &&
2709 entry->wiring_thread == NULL,
2710 ("owned map entry %p", entry));
2711 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
2712 entry->wiring_thread = curthread;
2713 if ((entry->protection & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0
2714 || (entry->protection & prot) != prot) {
2715 entry->eflags |= MAP_ENTRY_WIRE_SKIPPED;
2716 if ((flags & VM_MAP_WIRE_HOLESOK) == 0) {
2718 rv = KERN_INVALID_ADDRESS;
2723 if (entry->wired_count == 0) {
2724 entry->wired_count++;
2725 saved_start = entry->start;
2726 saved_end = entry->end;
2729 * Release the map lock, relying on the in-transition
2730 * mark. Mark the map busy for fork.
2735 faddr = saved_start;
2738 * Simulate a fault to get the page and enter
2739 * it into the physical map.
2741 if ((rv = vm_fault(map, faddr, VM_PROT_NONE,
2742 VM_FAULT_WIRE)) != KERN_SUCCESS)
2744 } while ((faddr += PAGE_SIZE) < saved_end);
2747 if (last_timestamp + 1 != map->timestamp) {
2749 * Look again for the entry because the map was
2750 * modified while it was unlocked. The entry
2751 * may have been clipped, but NOT merged or
2754 result = vm_map_lookup_entry(map, saved_start,
2756 KASSERT(result, ("vm_map_wire: lookup failed"));
2757 if (entry == first_entry)
2758 first_entry = tmp_entry;
2762 while (entry->end < saved_end) {
2764 * In case of failure, handle entries
2765 * that were not fully wired here;
2766 * fully wired entries are handled
2769 if (rv != KERN_SUCCESS &&
2771 vm_map_wire_entry_failure(map,
2773 entry = entry->next;
2776 last_timestamp = map->timestamp;
2777 if (rv != KERN_SUCCESS) {
2778 vm_map_wire_entry_failure(map, entry, faddr);
2782 } else if (!user_wire ||
2783 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
2784 entry->wired_count++;
2787 * Check the map for holes in the specified region.
2788 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
2791 if ((flags & VM_MAP_WIRE_HOLESOK) == 0 &&
2792 entry->end < end && entry->next->start > entry->end) {
2794 rv = KERN_INVALID_ADDRESS;
2797 entry = entry->next;
2801 need_wakeup = FALSE;
2802 if (first_entry == NULL) {
2803 result = vm_map_lookup_entry(map, start, &first_entry);
2804 if (!result && (flags & VM_MAP_WIRE_HOLESOK))
2805 first_entry = first_entry->next;
2807 KASSERT(result, ("vm_map_wire: lookup failed"));
2809 for (entry = first_entry; entry->start < end; entry = entry->next) {
2811 * If VM_MAP_WIRE_HOLESOK was specified, an empty
2812 * space in the unwired region could have been mapped
2813 * while the map lock was dropped for faulting in the
2814 * pages or draining MAP_ENTRY_IN_TRANSITION.
2815 * Moreover, another thread could be simultaneously
2816 * wiring this new mapping entry. Detect these cases
2817 * and skip any entries marked as in transition not by us.
2819 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 ||
2820 entry->wiring_thread != curthread) {
2821 KASSERT((flags & VM_MAP_WIRE_HOLESOK) != 0,
2822 ("vm_map_wire: !HOLESOK and new/changed entry"));
2826 if ((entry->eflags & MAP_ENTRY_WIRE_SKIPPED) != 0)
2827 goto next_entry_done;
2829 if (rv == KERN_SUCCESS) {
2831 entry->eflags |= MAP_ENTRY_USER_WIRED;
2832 } else if (entry->wired_count == -1) {
2834 * Wiring failed on this entry. Thus, unwiring is
2837 entry->wired_count = 0;
2838 } else if (!user_wire ||
2839 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
2841 * Undo the wiring. Wiring succeeded on this entry
2842 * but failed on a later entry.
2844 if (entry->wired_count == 1)
2845 vm_map_entry_unwire(map, entry);
2847 entry->wired_count--;
2850 KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0,
2851 ("vm_map_wire: in-transition flag missing %p", entry));
2852 KASSERT(entry->wiring_thread == curthread,
2853 ("vm_map_wire: alien wire %p", entry));
2854 entry->eflags &= ~(MAP_ENTRY_IN_TRANSITION |
2855 MAP_ENTRY_WIRE_SKIPPED);
2856 entry->wiring_thread = NULL;
2857 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
2858 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
2861 vm_map_simplify_entry(map, entry);
2872 * Push any dirty cached pages in the address range to their pager.
2873 * If syncio is TRUE, dirty pages are written synchronously.
2874 * If invalidate is TRUE, any cached pages are freed as well.
2876 * If the size of the region from start to end is zero, we are
2877 * supposed to flush all modified pages within the region containing
2878 * start. Unfortunately, a region can be split or coalesced with
2879 * neighboring regions, making it difficult to determine what the
2880 * original region was. Therefore, we approximate this requirement by
2881 * flushing the current region containing start.
2883 * Returns an error if any part of the specified range is not mapped.
2891 boolean_t invalidate)
2893 vm_map_entry_t current;
2894 vm_map_entry_t entry;
2897 vm_ooffset_t offset;
2898 unsigned int last_timestamp;
2901 vm_map_lock_read(map);
2902 VM_MAP_RANGE_CHECK(map, start, end);
2903 if (!vm_map_lookup_entry(map, start, &entry)) {
2904 vm_map_unlock_read(map);
2905 return (KERN_INVALID_ADDRESS);
2906 } else if (start == end) {
2907 start = entry->start;
2911 * Make a first pass to check for user-wired memory and holes.
2913 for (current = entry; current->start < end; current = current->next) {
2914 if (invalidate && (current->eflags & MAP_ENTRY_USER_WIRED)) {
2915 vm_map_unlock_read(map);
2916 return (KERN_INVALID_ARGUMENT);
2918 if (end > current->end &&
2919 current->end != current->next->start) {
2920 vm_map_unlock_read(map);
2921 return (KERN_INVALID_ADDRESS);
2926 pmap_remove(map->pmap, start, end);
2930 * Make a second pass, cleaning/uncaching pages from the indicated
2933 for (current = entry; current->start < end;) {
2934 offset = current->offset + (start - current->start);
2935 size = (end <= current->end ? end : current->end) - start;
2936 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
2938 vm_map_entry_t tentry;
2941 smap = current->object.sub_map;
2942 vm_map_lock_read(smap);
2943 (void) vm_map_lookup_entry(smap, offset, &tentry);
2944 tsize = tentry->end - offset;
2947 object = tentry->object.vm_object;
2948 offset = tentry->offset + (offset - tentry->start);
2949 vm_map_unlock_read(smap);
2951 object = current->object.vm_object;
2953 vm_object_reference(object);
2954 last_timestamp = map->timestamp;
2955 vm_map_unlock_read(map);
2956 if (!vm_object_sync(object, offset, size, syncio, invalidate))
2959 vm_object_deallocate(object);
2960 vm_map_lock_read(map);
2961 if (last_timestamp == map->timestamp ||
2962 !vm_map_lookup_entry(map, start, ¤t))
2963 current = current->next;
2966 vm_map_unlock_read(map);
2967 return (failed ? KERN_FAILURE : KERN_SUCCESS);
2971 * vm_map_entry_unwire: [ internal use only ]
2973 * Make the region specified by this entry pageable.
2975 * The map in question should be locked.
2976 * [This is the reason for this routine's existence.]
2979 vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
2982 VM_MAP_ASSERT_LOCKED(map);
2983 KASSERT(entry->wired_count > 0,
2984 ("vm_map_entry_unwire: entry %p isn't wired", entry));
2985 pmap_unwire(map->pmap, entry->start, entry->end);
2986 vm_object_unwire(entry->object.vm_object, entry->offset, entry->end -
2987 entry->start, PQ_ACTIVE);
2988 entry->wired_count = 0;
2992 vm_map_entry_deallocate(vm_map_entry_t entry, boolean_t system_map)
2995 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0)
2996 vm_object_deallocate(entry->object.vm_object);
2997 uma_zfree(system_map ? kmapentzone : mapentzone, entry);
3001 * vm_map_entry_delete: [ internal use only ]
3003 * Deallocate the given entry from the target map.
3006 vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry)
3009 vm_pindex_t offidxstart, offidxend, count, size1;
3012 vm_map_entry_unlink(map, entry);
3013 object = entry->object.vm_object;
3015 if ((entry->eflags & MAP_ENTRY_GUARD) != 0) {
3016 MPASS(entry->cred == NULL);
3017 MPASS((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0);
3018 MPASS(object == NULL);
3019 vm_map_entry_deallocate(entry, map->system_map);
3023 size = entry->end - entry->start;
3026 if (entry->cred != NULL) {
3027 swap_release_by_cred(size, entry->cred);
3028 crfree(entry->cred);
3031 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
3033 KASSERT(entry->cred == NULL || object->cred == NULL ||
3034 (entry->eflags & MAP_ENTRY_NEEDS_COPY),
3035 ("OVERCOMMIT vm_map_entry_delete: both cred %p", entry));
3037 offidxstart = OFF_TO_IDX(entry->offset);
3038 offidxend = offidxstart + count;
3039 VM_OBJECT_WLOCK(object);
3040 if (object->ref_count != 1 && ((object->flags & (OBJ_NOSPLIT |
3041 OBJ_ONEMAPPING)) == OBJ_ONEMAPPING ||
3042 object == kernel_object)) {
3043 vm_object_collapse(object);
3046 * The option OBJPR_NOTMAPPED can be passed here
3047 * because vm_map_delete() already performed
3048 * pmap_remove() on the only mapping to this range
3051 vm_object_page_remove(object, offidxstart, offidxend,
3053 if (object->type == OBJT_SWAP)
3054 swap_pager_freespace(object, offidxstart,
3056 if (offidxend >= object->size &&
3057 offidxstart < object->size) {
3058 size1 = object->size;
3059 object->size = offidxstart;
3060 if (object->cred != NULL) {
3061 size1 -= object->size;
3062 KASSERT(object->charge >= ptoa(size1),
3063 ("object %p charge < 0", object));
3064 swap_release_by_cred(ptoa(size1),
3066 object->charge -= ptoa(size1);
3070 VM_OBJECT_WUNLOCK(object);
3072 entry->object.vm_object = NULL;
3073 if (map->system_map)
3074 vm_map_entry_deallocate(entry, TRUE);
3076 entry->next = curthread->td_map_def_user;
3077 curthread->td_map_def_user = entry;
3082 * vm_map_delete: [ internal use only ]
3084 * Deallocates the given address range from the target
3088 vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end)
3090 vm_map_entry_t entry;
3091 vm_map_entry_t first_entry;
3093 VM_MAP_ASSERT_LOCKED(map);
3095 return (KERN_SUCCESS);
3098 * Find the start of the region, and clip it
3100 if (!vm_map_lookup_entry(map, start, &first_entry))
3101 entry = first_entry->next;
3103 entry = first_entry;
3104 vm_map_clip_start(map, entry, start);
3108 * Step through all entries in this region
3110 while (entry->start < end) {
3111 vm_map_entry_t next;
3114 * Wait for wiring or unwiring of an entry to complete.
3115 * Also wait for any system wirings to disappear on
3118 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 ||
3119 (vm_map_pmap(map) != kernel_pmap &&
3120 vm_map_entry_system_wired_count(entry) != 0)) {
3121 unsigned int last_timestamp;
3122 vm_offset_t saved_start;
3123 vm_map_entry_t tmp_entry;
3125 saved_start = entry->start;
3126 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
3127 last_timestamp = map->timestamp;
3128 (void) vm_map_unlock_and_wait(map, 0);
3130 if (last_timestamp + 1 != map->timestamp) {
3132 * Look again for the entry because the map was
3133 * modified while it was unlocked.
3134 * Specifically, the entry may have been
3135 * clipped, merged, or deleted.
3137 if (!vm_map_lookup_entry(map, saved_start,
3139 entry = tmp_entry->next;
3142 vm_map_clip_start(map, entry,
3148 vm_map_clip_end(map, entry, end);
3153 * Unwire before removing addresses from the pmap; otherwise,
3154 * unwiring will put the entries back in the pmap.
3156 if (entry->wired_count != 0)
3157 vm_map_entry_unwire(map, entry);
3160 * Remove mappings for the pages, but only if the
3161 * mappings could exist. For instance, it does not
3162 * make sense to call pmap_remove() for guard entries.
3164 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0 ||
3165 entry->object.vm_object != NULL)
3166 pmap_remove(map->pmap, entry->start, entry->end);
3169 * Delete the entry only after removing all pmap
3170 * entries pointing to its pages. (Otherwise, its
3171 * page frames may be reallocated, and any modify bits
3172 * will be set in the wrong object!)
3174 vm_map_entry_delete(map, entry);
3177 return (KERN_SUCCESS);
3183 * Remove the given address range from the target map.
3184 * This is the exported form of vm_map_delete.
3187 vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
3192 VM_MAP_RANGE_CHECK(map, start, end);
3193 result = vm_map_delete(map, start, end);
3199 * vm_map_check_protection:
3201 * Assert that the target map allows the specified privilege on the
3202 * entire address region given. The entire region must be allocated.
3204 * WARNING! This code does not and should not check whether the
3205 * contents of the region is accessible. For example a smaller file
3206 * might be mapped into a larger address space.
3208 * NOTE! This code is also called by munmap().
3210 * The map must be locked. A read lock is sufficient.
3213 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
3214 vm_prot_t protection)
3216 vm_map_entry_t entry;
3217 vm_map_entry_t tmp_entry;
3219 if (!vm_map_lookup_entry(map, start, &tmp_entry))
3223 while (start < end) {
3227 if (start < entry->start)
3230 * Check protection associated with entry.
3232 if ((entry->protection & protection) != protection)
3234 /* go to next entry */
3236 entry = entry->next;
3242 * vm_map_copy_entry:
3244 * Copies the contents of the source entry to the destination
3245 * entry. The entries *must* be aligned properly.
3251 vm_map_entry_t src_entry,
3252 vm_map_entry_t dst_entry,
3253 vm_ooffset_t *fork_charge)
3255 vm_object_t src_object;
3256 vm_map_entry_t fake_entry;
3261 VM_MAP_ASSERT_LOCKED(dst_map);
3263 if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
3266 if (src_entry->wired_count == 0 ||
3267 (src_entry->protection & VM_PROT_WRITE) == 0) {
3269 * If the source entry is marked needs_copy, it is already
3272 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0 &&
3273 (src_entry->protection & VM_PROT_WRITE) != 0) {
3274 pmap_protect(src_map->pmap,
3277 src_entry->protection & ~VM_PROT_WRITE);
3281 * Make a copy of the object.
3283 size = src_entry->end - src_entry->start;
3284 if ((src_object = src_entry->object.vm_object) != NULL) {
3285 VM_OBJECT_WLOCK(src_object);
3286 charged = ENTRY_CHARGED(src_entry);
3287 if (src_object->handle == NULL &&
3288 (src_object->type == OBJT_DEFAULT ||
3289 src_object->type == OBJT_SWAP)) {
3290 vm_object_collapse(src_object);
3291 if ((src_object->flags & (OBJ_NOSPLIT |
3292 OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
3293 vm_object_split(src_entry);
3295 src_entry->object.vm_object;
3298 vm_object_reference_locked(src_object);
3299 vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
3300 if (src_entry->cred != NULL &&
3301 !(src_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
3302 KASSERT(src_object->cred == NULL,
3303 ("OVERCOMMIT: vm_map_copy_entry: cred %p",
3305 src_object->cred = src_entry->cred;
3306 src_object->charge = size;
3308 VM_OBJECT_WUNLOCK(src_object);
3309 dst_entry->object.vm_object = src_object;
3311 cred = curthread->td_ucred;
3313 dst_entry->cred = cred;
3314 *fork_charge += size;
3315 if (!(src_entry->eflags &
3316 MAP_ENTRY_NEEDS_COPY)) {
3318 src_entry->cred = cred;
3319 *fork_charge += size;
3322 src_entry->eflags |= MAP_ENTRY_COW |
3323 MAP_ENTRY_NEEDS_COPY;
3324 dst_entry->eflags |= MAP_ENTRY_COW |
3325 MAP_ENTRY_NEEDS_COPY;
3326 dst_entry->offset = src_entry->offset;
3327 if (src_entry->eflags & MAP_ENTRY_VN_WRITECNT) {
3329 * MAP_ENTRY_VN_WRITECNT cannot
3330 * indicate write reference from
3331 * src_entry, since the entry is
3332 * marked as needs copy. Allocate a
3333 * fake entry that is used to
3334 * decrement object->un_pager.vnp.writecount
3335 * at the appropriate time. Attach
3336 * fake_entry to the deferred list.
3338 fake_entry = vm_map_entry_create(dst_map);
3339 fake_entry->eflags = MAP_ENTRY_VN_WRITECNT;
3340 src_entry->eflags &= ~MAP_ENTRY_VN_WRITECNT;
3341 vm_object_reference(src_object);
3342 fake_entry->object.vm_object = src_object;
3343 fake_entry->start = src_entry->start;
3344 fake_entry->end = src_entry->end;
3345 fake_entry->next = curthread->td_map_def_user;
3346 curthread->td_map_def_user = fake_entry;
3349 pmap_copy(dst_map->pmap, src_map->pmap,
3350 dst_entry->start, dst_entry->end - dst_entry->start,
3353 dst_entry->object.vm_object = NULL;
3354 dst_entry->offset = 0;
3355 if (src_entry->cred != NULL) {
3356 dst_entry->cred = curthread->td_ucred;
3357 crhold(dst_entry->cred);
3358 *fork_charge += size;
3363 * We don't want to make writeable wired pages copy-on-write.
3364 * Immediately copy these pages into the new map by simulating
3365 * page faults. The new pages are pageable.
3367 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry,
3373 * vmspace_map_entry_forked:
3374 * Update the newly-forked vmspace each time a map entry is inherited
3375 * or copied. The values for vm_dsize and vm_tsize are approximate
3376 * (and mostly-obsolete ideas in the face of mmap(2) et al.)
3379 vmspace_map_entry_forked(const struct vmspace *vm1, struct vmspace *vm2,
3380 vm_map_entry_t entry)
3382 vm_size_t entrysize;
3385 if ((entry->eflags & MAP_ENTRY_GUARD) != 0)
3387 entrysize = entry->end - entry->start;
3388 vm2->vm_map.size += entrysize;
3389 if (entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP)) {
3390 vm2->vm_ssize += btoc(entrysize);
3391 } else if (entry->start >= (vm_offset_t)vm1->vm_daddr &&
3392 entry->start < (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)) {
3393 newend = MIN(entry->end,
3394 (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize));
3395 vm2->vm_dsize += btoc(newend - entry->start);
3396 } else if (entry->start >= (vm_offset_t)vm1->vm_taddr &&
3397 entry->start < (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)) {
3398 newend = MIN(entry->end,
3399 (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize));
3400 vm2->vm_tsize += btoc(newend - entry->start);
3406 * Create a new process vmspace structure and vm_map
3407 * based on those of an existing process. The new map
3408 * is based on the old map, according to the inheritance
3409 * values on the regions in that map.
3411 * XXX It might be worth coalescing the entries added to the new vmspace.
3413 * The source map must not be locked.
3416 vmspace_fork(struct vmspace *vm1, vm_ooffset_t *fork_charge)
3418 struct vmspace *vm2;
3419 vm_map_t new_map, old_map;
3420 vm_map_entry_t new_entry, old_entry;
3425 old_map = &vm1->vm_map;
3426 /* Copy immutable fields of vm1 to vm2. */
3427 vm2 = vmspace_alloc(vm_map_min(old_map), vm_map_max(old_map), NULL);
3430 vm2->vm_taddr = vm1->vm_taddr;
3431 vm2->vm_daddr = vm1->vm_daddr;
3432 vm2->vm_maxsaddr = vm1->vm_maxsaddr;
3433 vm_map_lock(old_map);
3435 vm_map_wait_busy(old_map);
3436 new_map = &vm2->vm_map;
3437 locked = vm_map_trylock(new_map); /* trylock to silence WITNESS */
3438 KASSERT(locked, ("vmspace_fork: lock failed"));
3440 old_entry = old_map->header.next;
3442 while (old_entry != &old_map->header) {
3443 if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
3444 panic("vm_map_fork: encountered a submap");
3446 inh = old_entry->inheritance;
3447 if ((old_entry->eflags & MAP_ENTRY_GUARD) != 0 &&
3448 inh != VM_INHERIT_NONE)
3449 inh = VM_INHERIT_COPY;
3452 case VM_INHERIT_NONE:
3455 case VM_INHERIT_SHARE:
3457 * Clone the entry, creating the shared object if necessary.
3459 object = old_entry->object.vm_object;
3460 if (object == NULL) {
3461 object = vm_object_allocate(OBJT_DEFAULT,
3462 atop(old_entry->end - old_entry->start));
3463 old_entry->object.vm_object = object;
3464 old_entry->offset = 0;
3465 if (old_entry->cred != NULL) {
3466 object->cred = old_entry->cred;
3467 object->charge = old_entry->end -
3469 old_entry->cred = NULL;
3474 * Add the reference before calling vm_object_shadow
3475 * to insure that a shadow object is created.
3477 vm_object_reference(object);
3478 if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3479 vm_object_shadow(&old_entry->object.vm_object,
3481 old_entry->end - old_entry->start);
3482 old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
3483 /* Transfer the second reference too. */
3484 vm_object_reference(
3485 old_entry->object.vm_object);
3488 * As in vm_map_simplify_entry(), the
3489 * vnode lock will not be acquired in
3490 * this call to vm_object_deallocate().
3492 vm_object_deallocate(object);
3493 object = old_entry->object.vm_object;
3495 VM_OBJECT_WLOCK(object);
3496 vm_object_clear_flag(object, OBJ_ONEMAPPING);
3497 if (old_entry->cred != NULL) {
3498 KASSERT(object->cred == NULL, ("vmspace_fork both cred"));
3499 object->cred = old_entry->cred;
3500 object->charge = old_entry->end - old_entry->start;
3501 old_entry->cred = NULL;
3505 * Assert the correct state of the vnode
3506 * v_writecount while the object is locked, to
3507 * not relock it later for the assertion
3510 if (old_entry->eflags & MAP_ENTRY_VN_WRITECNT &&
3511 object->type == OBJT_VNODE) {
3512 KASSERT(((struct vnode *)object->handle)->
3514 ("vmspace_fork: v_writecount %p", object));
3515 KASSERT(object->un_pager.vnp.writemappings > 0,
3516 ("vmspace_fork: vnp.writecount %p",
3519 VM_OBJECT_WUNLOCK(object);
3522 * Clone the entry, referencing the shared object.
3524 new_entry = vm_map_entry_create(new_map);
3525 *new_entry = *old_entry;
3526 new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
3527 MAP_ENTRY_IN_TRANSITION);
3528 new_entry->wiring_thread = NULL;
3529 new_entry->wired_count = 0;
3530 if (new_entry->eflags & MAP_ENTRY_VN_WRITECNT) {
3531 vnode_pager_update_writecount(object,
3532 new_entry->start, new_entry->end);
3536 * Insert the entry into the new map -- we know we're
3537 * inserting at the end of the new map.
3539 vm_map_entry_link(new_map, new_map->header.prev,
3541 vmspace_map_entry_forked(vm1, vm2, new_entry);
3544 * Update the physical map
3546 pmap_copy(new_map->pmap, old_map->pmap,
3548 (old_entry->end - old_entry->start),
3552 case VM_INHERIT_COPY:
3554 * Clone the entry and link into the map.
3556 new_entry = vm_map_entry_create(new_map);
3557 *new_entry = *old_entry;
3559 * Copied entry is COW over the old object.
3561 new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
3562 MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_VN_WRITECNT);
3563 new_entry->wiring_thread = NULL;
3564 new_entry->wired_count = 0;
3565 new_entry->object.vm_object = NULL;
3566 new_entry->cred = NULL;
3567 vm_map_entry_link(new_map, new_map->header.prev,
3569 vmspace_map_entry_forked(vm1, vm2, new_entry);
3570 vm_map_copy_entry(old_map, new_map, old_entry,
3571 new_entry, fork_charge);
3574 case VM_INHERIT_ZERO:
3576 * Create a new anonymous mapping entry modelled from
3579 new_entry = vm_map_entry_create(new_map);
3580 memset(new_entry, 0, sizeof(*new_entry));
3582 new_entry->start = old_entry->start;
3583 new_entry->end = old_entry->end;
3584 new_entry->eflags = old_entry->eflags &
3585 ~(MAP_ENTRY_USER_WIRED | MAP_ENTRY_IN_TRANSITION |
3586 MAP_ENTRY_VN_WRITECNT);
3587 new_entry->protection = old_entry->protection;
3588 new_entry->max_protection = old_entry->max_protection;
3589 new_entry->inheritance = VM_INHERIT_ZERO;
3591 vm_map_entry_link(new_map, new_map->header.prev,
3593 vmspace_map_entry_forked(vm1, vm2, new_entry);
3595 new_entry->cred = curthread->td_ucred;
3596 crhold(new_entry->cred);
3597 *fork_charge += (new_entry->end - new_entry->start);
3601 old_entry = old_entry->next;
3604 * Use inlined vm_map_unlock() to postpone handling the deferred
3605 * map entries, which cannot be done until both old_map and
3606 * new_map locks are released.
3608 sx_xunlock(&old_map->lock);
3609 sx_xunlock(&new_map->lock);
3610 vm_map_process_deferred();
3616 * Create a process's stack for exec_new_vmspace(). This function is never
3617 * asked to wire the newly created stack.
3620 vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
3621 vm_prot_t prot, vm_prot_t max, int cow)
3623 vm_size_t growsize, init_ssize;
3627 MPASS((map->flags & MAP_WIREFUTURE) == 0);
3628 growsize = sgrowsiz;
3629 init_ssize = (max_ssize < growsize) ? max_ssize : growsize;
3631 vmemlim = lim_cur(curthread, RLIMIT_VMEM);
3632 /* If we would blow our VMEM resource limit, no go */
3633 if (map->size + init_ssize > vmemlim) {
3637 rv = vm_map_stack_locked(map, addrbos, max_ssize, growsize, prot,
3644 static int stack_guard_page = 1;
3645 SYSCTL_INT(_security_bsd, OID_AUTO, stack_guard_page, CTLFLAG_RWTUN,
3646 &stack_guard_page, 0,
3647 "Specifies the number of guard pages for a stack that grows");
3650 vm_map_stack_locked(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
3651 vm_size_t growsize, vm_prot_t prot, vm_prot_t max, int cow)
3653 vm_map_entry_t new_entry, prev_entry;
3654 vm_offset_t bot, gap_bot, gap_top, top;
3655 vm_size_t init_ssize, sgp;
3659 * The stack orientation is piggybacked with the cow argument.
3660 * Extract it into orient and mask the cow argument so that we
3661 * don't pass it around further.
3663 orient = cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP);
3664 KASSERT(orient != 0, ("No stack grow direction"));
3665 KASSERT(orient != (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP),
3668 if (addrbos < vm_map_min(map) ||
3669 addrbos + max_ssize > vm_map_max(map) ||
3670 addrbos + max_ssize <= addrbos)
3671 return (KERN_INVALID_ADDRESS);
3672 sgp = (vm_size_t)stack_guard_page * PAGE_SIZE;
3673 if (sgp >= max_ssize)
3674 return (KERN_INVALID_ARGUMENT);
3676 init_ssize = growsize;
3677 if (max_ssize < init_ssize + sgp)
3678 init_ssize = max_ssize - sgp;
3680 /* If addr is already mapped, no go */
3681 if (vm_map_lookup_entry(map, addrbos, &prev_entry))
3682 return (KERN_NO_SPACE);
3685 * If we can't accommodate max_ssize in the current mapping, no go.
3687 if (prev_entry->next->start < addrbos + max_ssize)
3688 return (KERN_NO_SPACE);
3691 * We initially map a stack of only init_ssize. We will grow as
3692 * needed later. Depending on the orientation of the stack (i.e.
3693 * the grow direction) we either map at the top of the range, the
3694 * bottom of the range or in the middle.
3696 * Note: we would normally expect prot and max to be VM_PROT_ALL,
3697 * and cow to be 0. Possibly we should eliminate these as input
3698 * parameters, and just pass these values here in the insert call.
3700 if (orient == MAP_STACK_GROWS_DOWN) {
3701 bot = addrbos + max_ssize - init_ssize;
3702 top = bot + init_ssize;
3705 } else /* if (orient == MAP_STACK_GROWS_UP) */ {
3707 top = bot + init_ssize;
3709 gap_top = addrbos + max_ssize;
3711 rv = vm_map_insert(map, NULL, 0, bot, top, prot, max, cow);
3712 if (rv != KERN_SUCCESS)
3714 new_entry = prev_entry->next;
3715 KASSERT(new_entry->end == top || new_entry->start == bot,
3716 ("Bad entry start/end for new stack entry"));
3717 KASSERT((orient & MAP_STACK_GROWS_DOWN) == 0 ||
3718 (new_entry->eflags & MAP_ENTRY_GROWS_DOWN) != 0,
3719 ("new entry lacks MAP_ENTRY_GROWS_DOWN"));
3720 KASSERT((orient & MAP_STACK_GROWS_UP) == 0 ||
3721 (new_entry->eflags & MAP_ENTRY_GROWS_UP) != 0,
3722 ("new entry lacks MAP_ENTRY_GROWS_UP"));
3723 rv = vm_map_insert(map, NULL, 0, gap_bot, gap_top, VM_PROT_NONE,
3724 VM_PROT_NONE, MAP_CREATE_GUARD | (orient == MAP_STACK_GROWS_DOWN ?
3725 MAP_CREATE_STACK_GAP_DN : MAP_CREATE_STACK_GAP_UP));
3726 if (rv != KERN_SUCCESS)
3727 (void)vm_map_delete(map, bot, top);
3732 * Attempts to grow a vm stack entry. Returns KERN_SUCCESS if we
3733 * successfully grow the stack.
3736 vm_map_growstack(vm_map_t map, vm_offset_t addr, vm_map_entry_t gap_entry)
3738 vm_map_entry_t stack_entry;
3742 vm_offset_t gap_end, gap_start, grow_start;
3743 size_t grow_amount, guard, max_grow;
3744 rlim_t lmemlim, stacklim, vmemlim;
3746 bool gap_deleted, grow_down, is_procstack;
3758 * Disallow stack growth when the access is performed by a
3759 * debugger or AIO daemon. The reason is that the wrong
3760 * resource limits are applied.
3762 if (map != &p->p_vmspace->vm_map || p->p_textvp == NULL)
3763 return (KERN_FAILURE);
3765 MPASS(!map->system_map);
3767 guard = stack_guard_page * PAGE_SIZE;
3768 lmemlim = lim_cur(curthread, RLIMIT_MEMLOCK);
3769 stacklim = lim_cur(curthread, RLIMIT_STACK);
3770 vmemlim = lim_cur(curthread, RLIMIT_VMEM);
3772 /* If addr is not in a hole for a stack grow area, no need to grow. */
3773 if (gap_entry == NULL && !vm_map_lookup_entry(map, addr, &gap_entry))
3774 return (KERN_FAILURE);
3775 if ((gap_entry->eflags & MAP_ENTRY_GUARD) == 0)
3776 return (KERN_SUCCESS);
3777 if ((gap_entry->eflags & MAP_ENTRY_STACK_GAP_DN) != 0) {
3778 stack_entry = gap_entry->next;
3779 if ((stack_entry->eflags & MAP_ENTRY_GROWS_DOWN) == 0 ||
3780 stack_entry->start != gap_entry->end)
3781 return (KERN_FAILURE);
3782 grow_amount = round_page(stack_entry->start - addr);
3784 } else if ((gap_entry->eflags & MAP_ENTRY_STACK_GAP_UP) != 0) {
3785 stack_entry = gap_entry->prev;
3786 if ((stack_entry->eflags & MAP_ENTRY_GROWS_UP) == 0 ||
3787 stack_entry->end != gap_entry->start)
3788 return (KERN_FAILURE);
3789 grow_amount = round_page(addr + 1 - stack_entry->end);
3792 return (KERN_FAILURE);
3794 max_grow = gap_entry->end - gap_entry->start;
3795 if (guard > max_grow)
3796 return (KERN_NO_SPACE);
3798 if (grow_amount > max_grow)
3799 return (KERN_NO_SPACE);
3802 * If this is the main process stack, see if we're over the stack
3805 is_procstack = addr >= (vm_offset_t)vm->vm_maxsaddr &&
3806 addr < (vm_offset_t)p->p_sysent->sv_usrstack;
3807 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim))
3808 return (KERN_NO_SPACE);
3813 if (is_procstack && racct_set(p, RACCT_STACK,
3814 ctob(vm->vm_ssize) + grow_amount)) {
3816 return (KERN_NO_SPACE);
3822 grow_amount = roundup(grow_amount, sgrowsiz);
3823 if (grow_amount > max_grow)
3824 grow_amount = max_grow;
3825 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
3826 grow_amount = trunc_page((vm_size_t)stacklim) -
3832 limit = racct_get_available(p, RACCT_STACK);
3834 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > limit))
3835 grow_amount = limit - ctob(vm->vm_ssize);
3838 if (!old_mlock && (map->flags & MAP_WIREFUTURE) != 0) {
3839 if (ptoa(pmap_wired_count(map->pmap)) + grow_amount > lmemlim) {
3846 if (racct_set(p, RACCT_MEMLOCK,
3847 ptoa(pmap_wired_count(map->pmap)) + grow_amount)) {
3857 /* If we would blow our VMEM resource limit, no go */
3858 if (map->size + grow_amount > vmemlim) {
3865 if (racct_set(p, RACCT_VMEM, map->size + grow_amount)) {
3874 if (vm_map_lock_upgrade(map)) {
3876 vm_map_lock_read(map);
3881 grow_start = gap_entry->end - grow_amount;
3882 if (gap_entry->start + grow_amount == gap_entry->end) {
3883 gap_start = gap_entry->start;
3884 gap_end = gap_entry->end;
3885 vm_map_entry_delete(map, gap_entry);
3888 MPASS(gap_entry->start < gap_entry->end - grow_amount);
3889 gap_entry->end -= grow_amount;
3890 vm_map_entry_resize_free(map, gap_entry);
3891 gap_deleted = false;
3893 rv = vm_map_insert(map, NULL, 0, grow_start,
3894 grow_start + grow_amount,
3895 stack_entry->protection, stack_entry->max_protection,
3896 MAP_STACK_GROWS_DOWN);
3897 if (rv != KERN_SUCCESS) {
3899 rv1 = vm_map_insert(map, NULL, 0, gap_start,
3900 gap_end, VM_PROT_NONE, VM_PROT_NONE,
3901 MAP_CREATE_GUARD | MAP_CREATE_STACK_GAP_DN);
3902 MPASS(rv1 == KERN_SUCCESS);
3904 gap_entry->end += grow_amount;
3905 vm_map_entry_resize_free(map, gap_entry);
3909 grow_start = stack_entry->end;
3910 cred = stack_entry->cred;
3911 if (cred == NULL && stack_entry->object.vm_object != NULL)
3912 cred = stack_entry->object.vm_object->cred;
3913 if (cred != NULL && !swap_reserve_by_cred(grow_amount, cred))
3915 /* Grow the underlying object if applicable. */
3916 else if (stack_entry->object.vm_object == NULL ||
3917 vm_object_coalesce(stack_entry->object.vm_object,
3918 stack_entry->offset,
3919 (vm_size_t)(stack_entry->end - stack_entry->start),
3920 (vm_size_t)grow_amount, cred != NULL)) {
3921 if (gap_entry->start + grow_amount == gap_entry->end)
3922 vm_map_entry_delete(map, gap_entry);
3924 gap_entry->start += grow_amount;
3925 stack_entry->end += grow_amount;
3926 map->size += grow_amount;
3927 vm_map_entry_resize_free(map, stack_entry);
3932 if (rv == KERN_SUCCESS && is_procstack)
3933 vm->vm_ssize += btoc(grow_amount);
3936 * Heed the MAP_WIREFUTURE flag if it was set for this process.
3938 if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE) != 0) {
3940 vm_map_wire(map, grow_start, grow_start + grow_amount,
3941 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
3942 vm_map_lock_read(map);
3944 vm_map_lock_downgrade(map);
3948 if (racct_enable && rv != KERN_SUCCESS) {
3950 error = racct_set(p, RACCT_VMEM, map->size);
3951 KASSERT(error == 0, ("decreasing RACCT_VMEM failed"));
3953 error = racct_set(p, RACCT_MEMLOCK,
3954 ptoa(pmap_wired_count(map->pmap)));
3955 KASSERT(error == 0, ("decreasing RACCT_MEMLOCK failed"));
3957 error = racct_set(p, RACCT_STACK, ctob(vm->vm_ssize));
3958 KASSERT(error == 0, ("decreasing RACCT_STACK failed"));
3967 * Unshare the specified VM space for exec. If other processes are
3968 * mapped to it, then create a new one. The new vmspace is null.
3971 vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser)
3973 struct vmspace *oldvmspace = p->p_vmspace;
3974 struct vmspace *newvmspace;
3976 KASSERT((curthread->td_pflags & TDP_EXECVMSPC) == 0,
3977 ("vmspace_exec recursed"));
3978 newvmspace = vmspace_alloc(minuser, maxuser, NULL);
3979 if (newvmspace == NULL)
3981 newvmspace->vm_swrss = oldvmspace->vm_swrss;
3983 * This code is written like this for prototype purposes. The
3984 * goal is to avoid running down the vmspace here, but let the
3985 * other process's that are still using the vmspace to finally
3986 * run it down. Even though there is little or no chance of blocking
3987 * here, it is a good idea to keep this form for future mods.
3989 PROC_VMSPACE_LOCK(p);
3990 p->p_vmspace = newvmspace;
3991 PROC_VMSPACE_UNLOCK(p);
3992 if (p == curthread->td_proc)
3993 pmap_activate(curthread);
3994 curthread->td_pflags |= TDP_EXECVMSPC;
3999 * Unshare the specified VM space for forcing COW. This
4000 * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
4003 vmspace_unshare(struct proc *p)
4005 struct vmspace *oldvmspace = p->p_vmspace;
4006 struct vmspace *newvmspace;
4007 vm_ooffset_t fork_charge;
4009 if (oldvmspace->vm_refcnt == 1)
4012 newvmspace = vmspace_fork(oldvmspace, &fork_charge);
4013 if (newvmspace == NULL)
4015 if (!swap_reserve_by_cred(fork_charge, p->p_ucred)) {
4016 vmspace_free(newvmspace);
4019 PROC_VMSPACE_LOCK(p);
4020 p->p_vmspace = newvmspace;
4021 PROC_VMSPACE_UNLOCK(p);
4022 if (p == curthread->td_proc)
4023 pmap_activate(curthread);
4024 vmspace_free(oldvmspace);
4031 * Finds the VM object, offset, and
4032 * protection for a given virtual address in the
4033 * specified map, assuming a page fault of the
4036 * Leaves the map in question locked for read; return
4037 * values are guaranteed until a vm_map_lookup_done
4038 * call is performed. Note that the map argument
4039 * is in/out; the returned map must be used in
4040 * the call to vm_map_lookup_done.
4042 * A handle (out_entry) is returned for use in
4043 * vm_map_lookup_done, to make that fast.
4045 * If a lookup is requested with "write protection"
4046 * specified, the map may be changed to perform virtual
4047 * copying operations, although the data referenced will
4051 vm_map_lookup(vm_map_t *var_map, /* IN/OUT */
4053 vm_prot_t fault_typea,
4054 vm_map_entry_t *out_entry, /* OUT */
4055 vm_object_t *object, /* OUT */
4056 vm_pindex_t *pindex, /* OUT */
4057 vm_prot_t *out_prot, /* OUT */
4058 boolean_t *wired) /* OUT */
4060 vm_map_entry_t entry;
4061 vm_map_t map = *var_map;
4063 vm_prot_t fault_type = fault_typea;
4064 vm_object_t eobject;
4070 vm_map_lock_read(map);
4074 * Lookup the faulting address.
4076 if (!vm_map_lookup_entry(map, vaddr, out_entry)) {
4077 vm_map_unlock_read(map);
4078 return (KERN_INVALID_ADDRESS);
4086 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
4087 vm_map_t old_map = map;
4089 *var_map = map = entry->object.sub_map;
4090 vm_map_unlock_read(old_map);
4095 * Check whether this task is allowed to have this page.
4097 prot = entry->protection;
4098 if ((fault_typea & VM_PROT_FAULT_LOOKUP) != 0) {
4099 fault_typea &= ~VM_PROT_FAULT_LOOKUP;
4100 if (prot == VM_PROT_NONE && map != kernel_map &&
4101 (entry->eflags & MAP_ENTRY_GUARD) != 0 &&
4102 (entry->eflags & (MAP_ENTRY_STACK_GAP_DN |
4103 MAP_ENTRY_STACK_GAP_UP)) != 0 &&
4104 vm_map_growstack(map, vaddr, entry) == KERN_SUCCESS)
4105 goto RetryLookupLocked;
4107 fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
4108 if ((fault_type & prot) != fault_type || prot == VM_PROT_NONE) {
4109 vm_map_unlock_read(map);
4110 return (KERN_PROTECTION_FAILURE);
4112 KASSERT((prot & VM_PROT_WRITE) == 0 || (entry->eflags &
4113 (MAP_ENTRY_USER_WIRED | MAP_ENTRY_NEEDS_COPY)) !=
4114 (MAP_ENTRY_USER_WIRED | MAP_ENTRY_NEEDS_COPY),
4115 ("entry %p flags %x", entry, entry->eflags));
4116 if ((fault_typea & VM_PROT_COPY) != 0 &&
4117 (entry->max_protection & VM_PROT_WRITE) == 0 &&
4118 (entry->eflags & MAP_ENTRY_COW) == 0) {
4119 vm_map_unlock_read(map);
4120 return (KERN_PROTECTION_FAILURE);
4124 * If this page is not pageable, we have to get it for all possible
4127 *wired = (entry->wired_count != 0);
4129 fault_type = entry->protection;
4130 size = entry->end - entry->start;
4132 * If the entry was copy-on-write, we either ...
4134 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
4136 * If we want to write the page, we may as well handle that
4137 * now since we've got the map locked.
4139 * If we don't need to write the page, we just demote the
4140 * permissions allowed.
4142 if ((fault_type & VM_PROT_WRITE) != 0 ||
4143 (fault_typea & VM_PROT_COPY) != 0) {
4145 * Make a new object, and place it in the object
4146 * chain. Note that no new references have appeared
4147 * -- one just moved from the map to the new
4150 if (vm_map_lock_upgrade(map))
4153 if (entry->cred == NULL) {
4155 * The debugger owner is charged for
4158 cred = curthread->td_ucred;
4160 if (!swap_reserve_by_cred(size, cred)) {
4163 return (KERN_RESOURCE_SHORTAGE);
4167 vm_object_shadow(&entry->object.vm_object,
4168 &entry->offset, size);
4169 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
4170 eobject = entry->object.vm_object;
4171 if (eobject->cred != NULL) {
4173 * The object was not shadowed.
4175 swap_release_by_cred(size, entry->cred);
4176 crfree(entry->cred);
4178 } else if (entry->cred != NULL) {
4179 VM_OBJECT_WLOCK(eobject);
4180 eobject->cred = entry->cred;
4181 eobject->charge = size;
4182 VM_OBJECT_WUNLOCK(eobject);
4186 vm_map_lock_downgrade(map);
4189 * We're attempting to read a copy-on-write page --
4190 * don't allow writes.
4192 prot &= ~VM_PROT_WRITE;
4197 * Create an object if necessary.
4199 if (entry->object.vm_object == NULL &&
4201 if (vm_map_lock_upgrade(map))
4203 entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
4206 if (entry->cred != NULL) {
4207 VM_OBJECT_WLOCK(entry->object.vm_object);
4208 entry->object.vm_object->cred = entry->cred;
4209 entry->object.vm_object->charge = size;
4210 VM_OBJECT_WUNLOCK(entry->object.vm_object);
4213 vm_map_lock_downgrade(map);
4217 * Return the object/offset from this entry. If the entry was
4218 * copy-on-write or empty, it has been fixed up.
4220 *pindex = UOFF_TO_IDX((vaddr - entry->start) + entry->offset);
4221 *object = entry->object.vm_object;
4224 return (KERN_SUCCESS);
4228 * vm_map_lookup_locked:
4230 * Lookup the faulting address. A version of vm_map_lookup that returns
4231 * KERN_FAILURE instead of blocking on map lock or memory allocation.
4234 vm_map_lookup_locked(vm_map_t *var_map, /* IN/OUT */
4236 vm_prot_t fault_typea,
4237 vm_map_entry_t *out_entry, /* OUT */
4238 vm_object_t *object, /* OUT */
4239 vm_pindex_t *pindex, /* OUT */
4240 vm_prot_t *out_prot, /* OUT */
4241 boolean_t *wired) /* OUT */
4243 vm_map_entry_t entry;
4244 vm_map_t map = *var_map;
4246 vm_prot_t fault_type = fault_typea;
4249 * Lookup the faulting address.
4251 if (!vm_map_lookup_entry(map, vaddr, out_entry))
4252 return (KERN_INVALID_ADDRESS);
4257 * Fail if the entry refers to a submap.
4259 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
4260 return (KERN_FAILURE);
4263 * Check whether this task is allowed to have this page.
4265 prot = entry->protection;
4266 fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
4267 if ((fault_type & prot) != fault_type)
4268 return (KERN_PROTECTION_FAILURE);
4271 * If this page is not pageable, we have to get it for all possible
4274 *wired = (entry->wired_count != 0);
4276 fault_type = entry->protection;
4278 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
4280 * Fail if the entry was copy-on-write for a write fault.
4282 if (fault_type & VM_PROT_WRITE)
4283 return (KERN_FAILURE);
4285 * We're attempting to read a copy-on-write page --
4286 * don't allow writes.
4288 prot &= ~VM_PROT_WRITE;
4292 * Fail if an object should be created.
4294 if (entry->object.vm_object == NULL && !map->system_map)
4295 return (KERN_FAILURE);
4298 * Return the object/offset from this entry. If the entry was
4299 * copy-on-write or empty, it has been fixed up.
4301 *pindex = UOFF_TO_IDX((vaddr - entry->start) + entry->offset);
4302 *object = entry->object.vm_object;
4305 return (KERN_SUCCESS);
4309 * vm_map_lookup_done:
4311 * Releases locks acquired by a vm_map_lookup
4312 * (according to the handle returned by that lookup).
4315 vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry)
4318 * Unlock the main-level map
4320 vm_map_unlock_read(map);
4324 vm_map_max_KBI(const struct vm_map *map)
4327 return (vm_map_max(map));
4331 vm_map_min_KBI(const struct vm_map *map)
4334 return (vm_map_min(map));
4338 vm_map_pmap_KBI(vm_map_t map)
4344 #include "opt_ddb.h"
4346 #include <sys/kernel.h>
4348 #include <ddb/ddb.h>
4351 vm_map_print(vm_map_t map)
4353 vm_map_entry_t entry;
4355 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
4357 (void *)map->pmap, map->nentries, map->timestamp);
4360 for (entry = map->header.next; entry != &map->header;
4361 entry = entry->next) {
4362 db_iprintf("map entry %p: start=%p, end=%p, eflags=%#x, \n",
4363 (void *)entry, (void *)entry->start, (void *)entry->end,
4366 static char *inheritance_name[4] =
4367 {"share", "copy", "none", "donate_copy"};
4369 db_iprintf(" prot=%x/%x/%s",
4371 entry->max_protection,
4372 inheritance_name[(int)(unsigned char)entry->inheritance]);
4373 if (entry->wired_count != 0)
4374 db_printf(", wired");
4376 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
4377 db_printf(", share=%p, offset=0x%jx\n",
4378 (void *)entry->object.sub_map,
4379 (uintmax_t)entry->offset);
4380 if ((entry->prev == &map->header) ||
4381 (entry->prev->object.sub_map !=
4382 entry->object.sub_map)) {
4384 vm_map_print((vm_map_t)entry->object.sub_map);
4388 if (entry->cred != NULL)
4389 db_printf(", ruid %d", entry->cred->cr_ruid);
4390 db_printf(", object=%p, offset=0x%jx",
4391 (void *)entry->object.vm_object,
4392 (uintmax_t)entry->offset);
4393 if (entry->object.vm_object && entry->object.vm_object->cred)
4394 db_printf(", obj ruid %d charge %jx",
4395 entry->object.vm_object->cred->cr_ruid,
4396 (uintmax_t)entry->object.vm_object->charge);
4397 if (entry->eflags & MAP_ENTRY_COW)
4398 db_printf(", copy (%s)",
4399 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
4402 if ((entry->prev == &map->header) ||
4403 (entry->prev->object.vm_object !=
4404 entry->object.vm_object)) {
4406 vm_object_print((db_expr_t)(intptr_t)
4407 entry->object.vm_object,
4416 DB_SHOW_COMMAND(map, map)
4420 db_printf("usage: show map <addr>\n");
4423 vm_map_print((vm_map_t)addr);
4426 DB_SHOW_COMMAND(procvm, procvm)
4431 p = db_lookup_proc(addr);
4436 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
4437 (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
4438 (void *)vmspace_pmap(p->p_vmspace));
4440 vm_map_print((vm_map_t)&p->p_vmspace->vm_map);