2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * The Mach Operating System project at Carnegie-Mellon University.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * from: @(#)vm_map.c 8.3 (Berkeley) 1/12/94
35 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
36 * All rights reserved.
38 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
40 * Permission to use, copy, modify and distribute this software and
41 * its documentation is hereby granted, provided that both the copyright
42 * notice and this permission notice appear in all copies of the
43 * software, derivative works or modified versions, and any portions
44 * thereof, and that both notices appear in supporting documentation.
46 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
47 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
48 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
50 * Carnegie Mellon requests users of this software to return to
52 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
53 * School of Computer Science
54 * Carnegie Mellon University
55 * Pittsburgh PA 15213-3890
57 * any improvements or extensions that they make and grant Carnegie the
58 * rights to redistribute these changes.
62 * Virtual memory mapping module.
65 #include <sys/cdefs.h>
66 __FBSDID("$FreeBSD$");
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/kernel.h>
73 #include <sys/mutex.h>
75 #include <sys/vmmeter.h>
77 #include <sys/vnode.h>
78 #include <sys/resourcevar.h>
80 #include <sys/sysctl.h>
81 #include <sys/sysent.h>
85 #include <vm/vm_param.h>
87 #include <vm/vm_map.h>
88 #include <vm/vm_page.h>
89 #include <vm/vm_object.h>
90 #include <vm/vm_pager.h>
91 #include <vm/vm_kern.h>
92 #include <vm/vm_extern.h>
93 #include <vm/swap_pager.h>
97 * Virtual memory maps provide for the mapping, protection,
98 * and sharing of virtual memory objects. In addition,
99 * this module provides for an efficient virtual copy of
100 * memory from one map to another.
102 * Synchronization is required prior to most operations.
104 * Maps consist of an ordered doubly-linked list of simple
105 * entries; a self-adjusting binary search tree of these
106 * entries is used to speed up lookups.
108 * Since portions of maps are specified by start/end addresses,
109 * which may not align with existing map entries, all
110 * routines merely "clip" entries to these start/end values.
111 * [That is, an entry is split into two, bordering at a
112 * start or end value.] Note that these clippings may not
113 * always be necessary (as the two resulting entries are then
114 * not changed); however, the clipping is done for convenience.
116 * As mentioned above, virtual copy operations are performed
117 * by copying VM object references from one map to
118 * another, and then marking both regions as copy-on-write.
121 static struct mtx map_sleep_mtx;
122 static uma_zone_t mapentzone;
123 static uma_zone_t kmapentzone;
124 static uma_zone_t mapzone;
125 static uma_zone_t vmspace_zone;
126 static struct vm_object kmapentobj;
127 static int vmspace_zinit(void *mem, int size, int flags);
128 static void vmspace_zfini(void *mem, int size);
129 static int vm_map_zinit(void *mem, int ize, int flags);
130 static void vm_map_zfini(void *mem, int size);
131 static void _vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max);
132 static void vm_map_entry_deallocate(vm_map_entry_t entry, boolean_t system_map);
133 static void vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry);
135 static void vm_map_zdtor(void *mem, int size, void *arg);
136 static void vmspace_zdtor(void *mem, int size, void *arg);
139 #define ENTRY_CHARGED(e) ((e)->uip != NULL || \
140 ((e)->object.vm_object != NULL && (e)->object.vm_object->uip != NULL && \
141 !((e)->eflags & MAP_ENTRY_NEEDS_COPY)))
144 * PROC_VMSPACE_{UN,}LOCK() can be a noop as long as vmspaces are type
147 #define PROC_VMSPACE_LOCK(p) do { } while (0)
148 #define PROC_VMSPACE_UNLOCK(p) do { } while (0)
151 * VM_MAP_RANGE_CHECK: [ internal use only ]
153 * Asserts that the starting and ending region
154 * addresses fall within the valid range of the map.
156 #define VM_MAP_RANGE_CHECK(map, start, end) \
158 if (start < vm_map_min(map)) \
159 start = vm_map_min(map); \
160 if (end > vm_map_max(map)) \
161 end = vm_map_max(map); \
169 * Initialize the vm_map module. Must be called before
170 * any other vm_map routines.
172 * Map and entry structures are allocated from the general
173 * purpose memory pool with some exceptions:
175 * - The kernel map and kmem submap are allocated statically.
176 * - Kernel map entries are allocated out of a static pool.
178 * These restrictions are necessary since malloc() uses the
179 * maps and requires map entries.
185 mtx_init(&map_sleep_mtx, "vm map sleep mutex", NULL, MTX_DEF);
186 mapzone = uma_zcreate("MAP", sizeof(struct vm_map), NULL,
192 vm_map_zinit, vm_map_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
193 uma_prealloc(mapzone, MAX_KMAP);
194 kmapentzone = uma_zcreate("KMAP ENTRY", sizeof(struct vm_map_entry),
195 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
196 UMA_ZONE_MTXCLASS | UMA_ZONE_VM);
197 uma_prealloc(kmapentzone, MAX_KMAPENT);
198 mapentzone = uma_zcreate("MAP ENTRY", sizeof(struct vm_map_entry),
199 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
203 vmspace_zfini(void *mem, int size)
207 vm = (struct vmspace *)mem;
208 vm_map_zfini(&vm->vm_map, sizeof(vm->vm_map));
212 vmspace_zinit(void *mem, int size, int flags)
216 vm = (struct vmspace *)mem;
218 vm->vm_map.pmap = NULL;
219 (void)vm_map_zinit(&vm->vm_map, sizeof(vm->vm_map), flags);
224 vm_map_zfini(void *mem, int size)
229 mtx_destroy(&map->system_mtx);
230 sx_destroy(&map->lock);
234 vm_map_zinit(void *mem, int size, int flags)
241 mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
242 sx_init(&map->lock, "user map");
248 vmspace_zdtor(void *mem, int size, void *arg)
252 vm = (struct vmspace *)mem;
254 vm_map_zdtor(&vm->vm_map, sizeof(vm->vm_map), arg);
257 vm_map_zdtor(void *mem, int size, void *arg)
262 KASSERT(map->nentries == 0,
263 ("map %p nentries == %d on free.",
264 map, map->nentries));
265 KASSERT(map->size == 0,
266 ("map %p size == %lu on free.",
267 map, (unsigned long)map->size));
269 #endif /* INVARIANTS */
272 * Allocate a vmspace structure, including a vm_map and pmap,
273 * and initialize those structures. The refcnt is set to 1.
276 vmspace_alloc(min, max)
277 vm_offset_t min, max;
281 vm = uma_zalloc(vmspace_zone, M_WAITOK);
282 if (vm->vm_map.pmap == NULL && !pmap_pinit(vmspace_pmap(vm))) {
283 uma_zfree(vmspace_zone, vm);
286 CTR1(KTR_VM, "vmspace_alloc: %p", vm);
287 _vm_map_init(&vm->vm_map, min, max);
288 vm->vm_map.pmap = vmspace_pmap(vm); /* XXX */
304 uma_zone_set_obj(kmapentzone, &kmapentobj, lmin(cnt.v_page_count,
305 (VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS) / PAGE_SIZE) / 8 +
306 maxproc * 2 + maxfiles);
307 vmspace_zone = uma_zcreate("VMSPACE", sizeof(struct vmspace), NULL,
313 vmspace_zinit, vmspace_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
317 vmspace_dofree(struct vmspace *vm)
320 CTR1(KTR_VM, "vmspace_free: %p", vm);
323 * Make sure any SysV shm is freed, it might not have been in
329 * Lock the map, to wait out all other references to it.
330 * Delete all of the mappings and pages they hold, then call
331 * the pmap module to reclaim anything left.
333 (void)vm_map_remove(&vm->vm_map, vm->vm_map.min_offset,
334 vm->vm_map.max_offset);
336 pmap_release(vmspace_pmap(vm));
337 vm->vm_map.pmap = NULL;
338 uma_zfree(vmspace_zone, vm);
342 vmspace_free(struct vmspace *vm)
345 if (vm->vm_refcnt == 0)
346 panic("vmspace_free: attempt to free already freed vmspace");
348 if (atomic_fetchadd_int(&vm->vm_refcnt, -1) == 1)
353 vmspace_exitfree(struct proc *p)
357 PROC_VMSPACE_LOCK(p);
360 PROC_VMSPACE_UNLOCK(p);
361 KASSERT(vm == &vmspace0, ("vmspace_exitfree: wrong vmspace"));
366 vmspace_exit(struct thread *td)
373 * Release user portion of address space.
374 * This releases references to vnodes,
375 * which could cause I/O if the file has been unlinked.
376 * Need to do this early enough that we can still sleep.
378 * The last exiting process to reach this point releases as
379 * much of the environment as it can. vmspace_dofree() is the
380 * slower fallback in case another process had a temporary
381 * reference to the vmspace.
386 atomic_add_int(&vmspace0.vm_refcnt, 1);
388 refcnt = vm->vm_refcnt;
389 if (refcnt > 1 && p->p_vmspace != &vmspace0) {
390 /* Switch now since other proc might free vmspace */
391 PROC_VMSPACE_LOCK(p);
392 p->p_vmspace = &vmspace0;
393 PROC_VMSPACE_UNLOCK(p);
396 } while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1));
398 if (p->p_vmspace != vm) {
399 /* vmspace not yet freed, switch back */
400 PROC_VMSPACE_LOCK(p);
402 PROC_VMSPACE_UNLOCK(p);
405 pmap_remove_pages(vmspace_pmap(vm));
406 /* Switch now since this proc will free vmspace */
407 PROC_VMSPACE_LOCK(p);
408 p->p_vmspace = &vmspace0;
409 PROC_VMSPACE_UNLOCK(p);
415 /* Acquire reference to vmspace owned by another process. */
418 vmspace_acquire_ref(struct proc *p)
423 PROC_VMSPACE_LOCK(p);
426 PROC_VMSPACE_UNLOCK(p);
430 refcnt = vm->vm_refcnt;
431 if (refcnt <= 0) { /* Avoid 0->1 transition */
432 PROC_VMSPACE_UNLOCK(p);
435 } while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt + 1));
436 if (vm != p->p_vmspace) {
437 PROC_VMSPACE_UNLOCK(p);
441 PROC_VMSPACE_UNLOCK(p);
446 _vm_map_lock(vm_map_t map, const char *file, int line)
450 _mtx_lock_flags(&map->system_mtx, 0, file, line);
452 (void)_sx_xlock(&map->lock, 0, file, line);
457 vm_map_process_deferred(void)
460 vm_map_entry_t entry, next;
463 entry = td->td_map_def_user;
464 td->td_map_def_user = NULL;
465 while (entry != NULL) {
467 vm_map_entry_deallocate(entry, FALSE);
473 _vm_map_unlock(vm_map_t map, const char *file, int line)
477 _mtx_unlock_flags(&map->system_mtx, 0, file, line);
479 _sx_xunlock(&map->lock, file, line);
480 vm_map_process_deferred();
485 _vm_map_lock_read(vm_map_t map, const char *file, int line)
489 _mtx_lock_flags(&map->system_mtx, 0, file, line);
491 (void)_sx_slock(&map->lock, 0, file, line);
495 _vm_map_unlock_read(vm_map_t map, const char *file, int line)
499 _mtx_unlock_flags(&map->system_mtx, 0, file, line);
501 _sx_sunlock(&map->lock, file, line);
502 vm_map_process_deferred();
507 _vm_map_trylock(vm_map_t map, const char *file, int line)
511 error = map->system_map ?
512 !_mtx_trylock(&map->system_mtx, 0, file, line) :
513 !_sx_try_xlock(&map->lock, file, line);
520 _vm_map_trylock_read(vm_map_t map, const char *file, int line)
524 error = map->system_map ?
525 !_mtx_trylock(&map->system_mtx, 0, file, line) :
526 !_sx_try_slock(&map->lock, file, line);
531 * _vm_map_lock_upgrade: [ internal use only ]
533 * Tries to upgrade a read (shared) lock on the specified map to a write
534 * (exclusive) lock. Returns the value "0" if the upgrade succeeds and a
535 * non-zero value if the upgrade fails. If the upgrade fails, the map is
536 * returned without a read or write lock held.
538 * Requires that the map be read locked.
541 _vm_map_lock_upgrade(vm_map_t map, const char *file, int line)
543 unsigned int last_timestamp;
545 if (map->system_map) {
547 _mtx_assert(&map->system_mtx, MA_OWNED, file, line);
550 if (!_sx_try_upgrade(&map->lock, file, line)) {
551 last_timestamp = map->timestamp;
552 _sx_sunlock(&map->lock, file, line);
553 vm_map_process_deferred();
555 * If the map's timestamp does not change while the
556 * map is unlocked, then the upgrade succeeds.
558 (void)_sx_xlock(&map->lock, 0, file, line);
559 if (last_timestamp != map->timestamp) {
560 _sx_xunlock(&map->lock, file, line);
570 _vm_map_lock_downgrade(vm_map_t map, const char *file, int line)
573 if (map->system_map) {
575 _mtx_assert(&map->system_mtx, MA_OWNED, file, line);
578 _sx_downgrade(&map->lock, file, line);
584 * Returns a non-zero value if the caller holds a write (exclusive) lock
585 * on the specified map and the value "0" otherwise.
588 vm_map_locked(vm_map_t map)
592 return (mtx_owned(&map->system_mtx));
594 return (sx_xlocked(&map->lock));
599 _vm_map_assert_locked(vm_map_t map, const char *file, int line)
603 _mtx_assert(&map->system_mtx, MA_OWNED, file, line);
605 _sx_assert(&map->lock, SA_XLOCKED, file, line);
610 _vm_map_assert_locked_read(vm_map_t map, const char *file, int line)
614 _mtx_assert(&map->system_mtx, MA_OWNED, file, line);
616 _sx_assert(&map->lock, SA_SLOCKED, file, line);
620 #define VM_MAP_ASSERT_LOCKED(map) \
621 _vm_map_assert_locked(map, LOCK_FILE, LOCK_LINE)
622 #define VM_MAP_ASSERT_LOCKED_READ(map) \
623 _vm_map_assert_locked_read(map, LOCK_FILE, LOCK_LINE)
625 #define VM_MAP_ASSERT_LOCKED(map)
626 #define VM_MAP_ASSERT_LOCKED_READ(map)
630 * _vm_map_unlock_and_wait:
632 * Atomically releases the lock on the specified map and puts the calling
633 * thread to sleep. The calling thread will remain asleep until either
634 * vm_map_wakeup() is performed on the map or the specified timeout is
637 * WARNING! This function does not perform deferred deallocations of
638 * objects and map entries. Therefore, the calling thread is expected to
639 * reacquire the map lock after reawakening and later perform an ordinary
640 * unlock operation, such as vm_map_unlock(), before completing its
641 * operation on the map.
644 _vm_map_unlock_and_wait(vm_map_t map, int timo, const char *file, int line)
647 mtx_lock(&map_sleep_mtx);
649 _mtx_unlock_flags(&map->system_mtx, 0, file, line);
651 _sx_xunlock(&map->lock, file, line);
652 return (msleep(&map->root, &map_sleep_mtx, PDROP | PVM, "vmmaps",
659 * Awaken any threads that have slept on the map using
660 * vm_map_unlock_and_wait().
663 vm_map_wakeup(vm_map_t map)
667 * Acquire and release map_sleep_mtx to prevent a wakeup()
668 * from being performed (and lost) between the map unlock
669 * and the msleep() in _vm_map_unlock_and_wait().
671 mtx_lock(&map_sleep_mtx);
672 mtx_unlock(&map_sleep_mtx);
677 vm_map_busy(vm_map_t map)
680 VM_MAP_ASSERT_LOCKED(map);
685 vm_map_unbusy(vm_map_t map)
688 VM_MAP_ASSERT_LOCKED(map);
689 KASSERT(map->busy, ("vm_map_unbusy: not busy"));
690 if (--map->busy == 0 && (map->flags & MAP_BUSY_WAKEUP)) {
691 vm_map_modflags(map, 0, MAP_BUSY_WAKEUP);
697 vm_map_wait_busy(vm_map_t map)
700 VM_MAP_ASSERT_LOCKED(map);
702 vm_map_modflags(map, MAP_BUSY_WAKEUP, 0);
704 msleep(&map->busy, &map->system_mtx, 0, "mbusy", 0);
706 sx_sleep(&map->busy, &map->lock, 0, "mbusy", 0);
712 vmspace_resident_count(struct vmspace *vmspace)
714 return pmap_resident_count(vmspace_pmap(vmspace));
718 vmspace_wired_count(struct vmspace *vmspace)
720 return pmap_wired_count(vmspace_pmap(vmspace));
726 * Creates and returns a new empty VM map with
727 * the given physical map structure, and having
728 * the given lower and upper address bounds.
731 vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max)
735 result = uma_zalloc(mapzone, M_WAITOK);
736 CTR1(KTR_VM, "vm_map_create: %p", result);
737 _vm_map_init(result, min, max);
743 * Initialize an existing vm_map structure
744 * such as that in the vmspace structure.
745 * The pmap is set elsewhere.
748 _vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max)
751 map->header.next = map->header.prev = &map->header;
752 map->needs_wakeup = FALSE;
754 map->min_offset = min;
755 map->max_offset = max;
763 vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max)
765 _vm_map_init(map, min, max);
766 mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
767 sx_init(&map->lock, "user map");
771 * vm_map_entry_dispose: [ internal use only ]
773 * Inverse of vm_map_entry_create.
776 vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry)
778 uma_zfree(map->system_map ? kmapentzone : mapentzone, entry);
782 * vm_map_entry_create: [ internal use only ]
784 * Allocates a VM map entry for insertion.
785 * No entry fields are filled in.
787 static vm_map_entry_t
788 vm_map_entry_create(vm_map_t map)
790 vm_map_entry_t new_entry;
793 new_entry = uma_zalloc(kmapentzone, M_NOWAIT);
795 new_entry = uma_zalloc(mapentzone, M_WAITOK);
796 if (new_entry == NULL)
797 panic("vm_map_entry_create: kernel resources exhausted");
802 * vm_map_entry_set_behavior:
804 * Set the expected access behavior, either normal, random, or
808 vm_map_entry_set_behavior(vm_map_entry_t entry, u_char behavior)
810 entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) |
811 (behavior & MAP_ENTRY_BEHAV_MASK);
815 * vm_map_entry_set_max_free:
817 * Set the max_free field in a vm_map_entry.
820 vm_map_entry_set_max_free(vm_map_entry_t entry)
823 entry->max_free = entry->adj_free;
824 if (entry->left != NULL && entry->left->max_free > entry->max_free)
825 entry->max_free = entry->left->max_free;
826 if (entry->right != NULL && entry->right->max_free > entry->max_free)
827 entry->max_free = entry->right->max_free;
831 * vm_map_entry_splay:
833 * The Sleator and Tarjan top-down splay algorithm with the
834 * following variation. Max_free must be computed bottom-up, so
835 * on the downward pass, maintain the left and right spines in
836 * reverse order. Then, make a second pass up each side to fix
837 * the pointers and compute max_free. The time bound is O(log n)
840 * The new root is the vm_map_entry containing "addr", or else an
841 * adjacent entry (lower or higher) if addr is not in the tree.
843 * The map must be locked, and leaves it so.
845 * Returns: the new root.
847 static vm_map_entry_t
848 vm_map_entry_splay(vm_offset_t addr, vm_map_entry_t root)
850 vm_map_entry_t llist, rlist;
851 vm_map_entry_t ltree, rtree;
854 /* Special case of empty tree. */
859 * Pass One: Splay down the tree until we find addr or a NULL
860 * pointer where addr would go. llist and rlist are the two
861 * sides in reverse order (bottom-up), with llist linked by
862 * the right pointer and rlist linked by the left pointer in
863 * the vm_map_entry. Wait until Pass Two to set max_free on
869 /* root is never NULL in here. */
870 if (addr < root->start) {
874 if (addr < y->start && y->left != NULL) {
875 /* Rotate right and put y on rlist. */
876 root->left = y->right;
878 vm_map_entry_set_max_free(root);
883 /* Put root on rlist. */
888 } else if (addr >= root->end) {
892 if (addr >= y->end && y->right != NULL) {
893 /* Rotate left and put y on llist. */
894 root->right = y->left;
896 vm_map_entry_set_max_free(root);
901 /* Put root on llist. */
911 * Pass Two: Walk back up the two spines, flip the pointers
912 * and set max_free. The subtrees of the root go at the
913 * bottom of llist and rlist.
916 while (llist != NULL) {
918 llist->right = ltree;
919 vm_map_entry_set_max_free(llist);
924 while (rlist != NULL) {
927 vm_map_entry_set_max_free(rlist);
933 * Final assembly: add ltree and rtree as subtrees of root.
937 vm_map_entry_set_max_free(root);
943 * vm_map_entry_{un,}link:
945 * Insert/remove entries from maps.
948 vm_map_entry_link(vm_map_t map,
949 vm_map_entry_t after_where,
950 vm_map_entry_t entry)
954 "vm_map_entry_link: map %p, nentries %d, entry %p, after %p", map,
955 map->nentries, entry, after_where);
956 VM_MAP_ASSERT_LOCKED(map);
958 entry->prev = after_where;
959 entry->next = after_where->next;
960 entry->next->prev = entry;
961 after_where->next = entry;
963 if (after_where != &map->header) {
964 if (after_where != map->root)
965 vm_map_entry_splay(after_where->start, map->root);
966 entry->right = after_where->right;
967 entry->left = after_where;
968 after_where->right = NULL;
969 after_where->adj_free = entry->start - after_where->end;
970 vm_map_entry_set_max_free(after_where);
972 entry->right = map->root;
975 entry->adj_free = (entry->next == &map->header ? map->max_offset :
976 entry->next->start) - entry->end;
977 vm_map_entry_set_max_free(entry);
982 vm_map_entry_unlink(vm_map_t map,
983 vm_map_entry_t entry)
985 vm_map_entry_t next, prev, root;
987 VM_MAP_ASSERT_LOCKED(map);
988 if (entry != map->root)
989 vm_map_entry_splay(entry->start, map->root);
990 if (entry->left == NULL)
993 root = vm_map_entry_splay(entry->start, entry->left);
994 root->right = entry->right;
995 root->adj_free = (entry->next == &map->header ? map->max_offset :
996 entry->next->start) - root->end;
997 vm_map_entry_set_max_free(root);
1006 CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map,
1007 map->nentries, entry);
1011 * vm_map_entry_resize_free:
1013 * Recompute the amount of free space following a vm_map_entry
1014 * and propagate that value up the tree. Call this function after
1015 * resizing a map entry in-place, that is, without a call to
1016 * vm_map_entry_link() or _unlink().
1018 * The map must be locked, and leaves it so.
1021 vm_map_entry_resize_free(vm_map_t map, vm_map_entry_t entry)
1025 * Using splay trees without parent pointers, propagating
1026 * max_free up the tree is done by moving the entry to the
1027 * root and making the change there.
1029 if (entry != map->root)
1030 map->root = vm_map_entry_splay(entry->start, map->root);
1032 entry->adj_free = (entry->next == &map->header ? map->max_offset :
1033 entry->next->start) - entry->end;
1034 vm_map_entry_set_max_free(entry);
1038 * vm_map_lookup_entry: [ internal use only ]
1040 * Finds the map entry containing (or
1041 * immediately preceding) the specified address
1042 * in the given map; the entry is returned
1043 * in the "entry" parameter. The boolean
1044 * result indicates whether the address is
1045 * actually contained in the map.
1048 vm_map_lookup_entry(
1050 vm_offset_t address,
1051 vm_map_entry_t *entry) /* OUT */
1057 * If the map is empty, then the map entry immediately preceding
1058 * "address" is the map's header.
1062 *entry = &map->header;
1063 else if (address >= cur->start && cur->end > address) {
1066 } else if ((locked = vm_map_locked(map)) ||
1067 sx_try_upgrade(&map->lock)) {
1069 * Splay requires a write lock on the map. However, it only
1070 * restructures the binary search tree; it does not otherwise
1071 * change the map. Thus, the map's timestamp need not change
1072 * on a temporary upgrade.
1074 map->root = cur = vm_map_entry_splay(address, cur);
1076 sx_downgrade(&map->lock);
1079 * If "address" is contained within a map entry, the new root
1080 * is that map entry. Otherwise, the new root is a map entry
1081 * immediately before or after "address".
1083 if (address >= cur->start) {
1085 if (cur->end > address)
1091 * Since the map is only locked for read access, perform a
1092 * standard binary search tree lookup for "address".
1095 if (address < cur->start) {
1096 if (cur->left == NULL) {
1101 } else if (cur->end > address) {
1105 if (cur->right == NULL) {
1118 * Inserts the given whole VM object into the target
1119 * map at the specified address range. The object's
1120 * size should match that of the address range.
1122 * Requires that the map be locked, and leaves it so.
1124 * If object is non-NULL, ref count must be bumped by caller
1125 * prior to making call to account for the new entry.
1128 vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1129 vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max,
1132 vm_map_entry_t new_entry;
1133 vm_map_entry_t prev_entry;
1134 vm_map_entry_t temp_entry;
1135 vm_eflags_t protoeflags;
1136 struct uidinfo *uip;
1137 boolean_t charge_prev_obj;
1139 VM_MAP_ASSERT_LOCKED(map);
1142 * Check that the start and end points are not bogus.
1144 if ((start < map->min_offset) || (end > map->max_offset) ||
1146 return (KERN_INVALID_ADDRESS);
1149 * Find the entry prior to the proposed starting address; if it's part
1150 * of an existing entry, this range is bogus.
1152 if (vm_map_lookup_entry(map, start, &temp_entry))
1153 return (KERN_NO_SPACE);
1155 prev_entry = temp_entry;
1158 * Assert that the next entry doesn't overlap the end point.
1160 if ((prev_entry->next != &map->header) &&
1161 (prev_entry->next->start < end))
1162 return (KERN_NO_SPACE);
1165 charge_prev_obj = FALSE;
1167 if (cow & MAP_COPY_ON_WRITE)
1168 protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
1170 if (cow & MAP_NOFAULT) {
1171 protoeflags |= MAP_ENTRY_NOFAULT;
1173 KASSERT(object == NULL,
1174 ("vm_map_insert: paradoxical MAP_NOFAULT request"));
1176 if (cow & MAP_DISABLE_SYNCER)
1177 protoeflags |= MAP_ENTRY_NOSYNC;
1178 if (cow & MAP_DISABLE_COREDUMP)
1179 protoeflags |= MAP_ENTRY_NOCOREDUMP;
1182 KASSERT((object != kmem_object && object != kernel_object) ||
1183 ((object == kmem_object || object == kernel_object) &&
1184 !(protoeflags & MAP_ENTRY_NEEDS_COPY)),
1185 ("kmem or kernel object and cow"));
1186 if (cow & (MAP_ACC_NO_CHARGE | MAP_NOFAULT))
1188 if ((cow & MAP_ACC_CHARGED) || ((prot & VM_PROT_WRITE) &&
1189 ((protoeflags & MAP_ENTRY_NEEDS_COPY) || object == NULL))) {
1190 if (!(cow & MAP_ACC_CHARGED) && !swap_reserve(end - start))
1191 return (KERN_RESOURCE_SHORTAGE);
1192 KASSERT(object == NULL || (protoeflags & MAP_ENTRY_NEEDS_COPY) ||
1193 object->uip == NULL,
1194 ("OVERCOMMIT: vm_map_insert o %p", object));
1195 uip = curthread->td_ucred->cr_ruidinfo;
1197 if (object == NULL && !(protoeflags & MAP_ENTRY_NEEDS_COPY))
1198 charge_prev_obj = TRUE;
1202 /* Expand the kernel pmap, if necessary. */
1203 if (map == kernel_map && end > kernel_vm_end)
1204 pmap_growkernel(end);
1205 if (object != NULL) {
1207 * OBJ_ONEMAPPING must be cleared unless this mapping
1208 * is trivially proven to be the only mapping for any
1209 * of the object's pages. (Object granularity
1210 * reference counting is insufficient to recognize
1211 * aliases with precision.)
1213 VM_OBJECT_LOCK(object);
1214 if (object->ref_count > 1 || object->shadow_count != 0)
1215 vm_object_clear_flag(object, OBJ_ONEMAPPING);
1216 VM_OBJECT_UNLOCK(object);
1218 else if ((prev_entry != &map->header) &&
1219 (prev_entry->eflags == protoeflags) &&
1220 (prev_entry->end == start) &&
1221 (prev_entry->wired_count == 0) &&
1222 (prev_entry->uip == uip ||
1223 (prev_entry->object.vm_object != NULL &&
1224 (prev_entry->object.vm_object->uip == uip))) &&
1225 vm_object_coalesce(prev_entry->object.vm_object,
1227 (vm_size_t)(prev_entry->end - prev_entry->start),
1228 (vm_size_t)(end - prev_entry->end), charge_prev_obj)) {
1230 * We were able to extend the object. Determine if we
1231 * can extend the previous map entry to include the
1232 * new range as well.
1234 if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
1235 (prev_entry->protection == prot) &&
1236 (prev_entry->max_protection == max)) {
1237 map->size += (end - prev_entry->end);
1238 prev_entry->end = end;
1239 vm_map_entry_resize_free(map, prev_entry);
1240 vm_map_simplify_entry(map, prev_entry);
1243 return (KERN_SUCCESS);
1247 * If we can extend the object but cannot extend the
1248 * map entry, we have to create a new map entry. We
1249 * must bump the ref count on the extended object to
1250 * account for it. object may be NULL.
1252 object = prev_entry->object.vm_object;
1253 offset = prev_entry->offset +
1254 (prev_entry->end - prev_entry->start);
1255 vm_object_reference(object);
1256 if (uip != NULL && object != NULL && object->uip != NULL &&
1257 !(prev_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
1258 /* Object already accounts for this uid. */
1265 * NOTE: if conditionals fail, object can be NULL here. This occurs
1266 * in things like the buffer map where we manage kva but do not manage
1271 * Create a new entry
1273 new_entry = vm_map_entry_create(map);
1274 new_entry->start = start;
1275 new_entry->end = end;
1276 new_entry->uip = NULL;
1278 new_entry->eflags = protoeflags;
1279 new_entry->object.vm_object = object;
1280 new_entry->offset = offset;
1281 new_entry->avail_ssize = 0;
1283 new_entry->inheritance = VM_INHERIT_DEFAULT;
1284 new_entry->protection = prot;
1285 new_entry->max_protection = max;
1286 new_entry->wired_count = 0;
1288 KASSERT(uip == NULL || !ENTRY_CHARGED(new_entry),
1289 ("OVERCOMMIT: vm_map_insert leaks vm_map %p", new_entry));
1290 new_entry->uip = uip;
1293 * Insert the new entry into the list
1295 vm_map_entry_link(map, prev_entry, new_entry);
1296 map->size += new_entry->end - new_entry->start;
1300 * Temporarily removed to avoid MAP_STACK panic, due to
1301 * MAP_STACK being a huge hack. Will be added back in
1302 * when MAP_STACK (and the user stack mapping) is fixed.
1305 * It may be possible to simplify the entry
1307 vm_map_simplify_entry(map, new_entry);
1310 if (cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) {
1311 vm_map_pmap_enter(map, start, prot,
1312 object, OFF_TO_IDX(offset), end - start,
1313 cow & MAP_PREFAULT_PARTIAL);
1316 return (KERN_SUCCESS);
1322 * Find the first fit (lowest VM address) for "length" free bytes
1323 * beginning at address >= start in the given map.
1325 * In a vm_map_entry, "adj_free" is the amount of free space
1326 * adjacent (higher address) to this entry, and "max_free" is the
1327 * maximum amount of contiguous free space in its subtree. This
1328 * allows finding a free region in one path down the tree, so
1329 * O(log n) amortized with splay trees.
1331 * The map must be locked, and leaves it so.
1333 * Returns: 0 on success, and starting address in *addr,
1334 * 1 if insufficient space.
1337 vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length,
1338 vm_offset_t *addr) /* OUT */
1340 vm_map_entry_t entry;
1344 * Request must fit within min/max VM address and must avoid
1347 if (start < map->min_offset)
1348 start = map->min_offset;
1349 if (start + length > map->max_offset || start + length < start)
1352 /* Empty tree means wide open address space. */
1353 if (map->root == NULL) {
1359 * After splay, if start comes before root node, then there
1360 * must be a gap from start to the root.
1362 map->root = vm_map_entry_splay(start, map->root);
1363 if (start + length <= map->root->start) {
1369 * Root is the last node that might begin its gap before
1370 * start, and this is the last comparison where address
1371 * wrap might be a problem.
1373 st = (start > map->root->end) ? start : map->root->end;
1374 if (length <= map->root->end + map->root->adj_free - st) {
1379 /* With max_free, can immediately tell if no solution. */
1380 entry = map->root->right;
1381 if (entry == NULL || length > entry->max_free)
1385 * Search the right subtree in the order: left subtree, root,
1386 * right subtree (first fit). The previous splay implies that
1387 * all regions in the right subtree have addresses > start.
1389 while (entry != NULL) {
1390 if (entry->left != NULL && entry->left->max_free >= length)
1391 entry = entry->left;
1392 else if (entry->adj_free >= length) {
1396 entry = entry->right;
1399 /* Can't get here, so panic if we do. */
1400 panic("vm_map_findspace: max_free corrupt");
1404 vm_map_fixed(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1405 vm_offset_t start, vm_size_t length, vm_prot_t prot,
1406 vm_prot_t max, int cow)
1411 end = start + length;
1413 VM_MAP_RANGE_CHECK(map, start, end);
1414 (void) vm_map_delete(map, start, end);
1415 result = vm_map_insert(map, object, offset, start, end, prot,
1422 * vm_map_find finds an unallocated region in the target address
1423 * map with the given length. The search is defined to be
1424 * first-fit from the specified address; the region found is
1425 * returned in the same parameter.
1427 * If object is non-NULL, ref count must be bumped by caller
1428 * prior to making call to account for the new entry.
1431 vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1432 vm_offset_t *addr, /* IN/OUT */
1433 vm_size_t length, int find_space, vm_prot_t prot,
1434 vm_prot_t max, int cow)
1442 if (find_space != VMFS_NO_SPACE) {
1443 if (vm_map_findspace(map, start, length, addr)) {
1445 return (KERN_NO_SPACE);
1447 switch (find_space) {
1448 case VMFS_ALIGNED_SPACE:
1449 pmap_align_superpage(object, offset, addr,
1452 #ifdef VMFS_TLB_ALIGNED_SPACE
1453 case VMFS_TLB_ALIGNED_SPACE:
1454 pmap_align_tlb(addr);
1463 result = vm_map_insert(map, object, offset, start, start +
1464 length, prot, max, cow);
1465 } while (result == KERN_NO_SPACE && find_space == VMFS_ALIGNED_SPACE);
1471 * vm_map_simplify_entry:
1473 * Simplify the given map entry by merging with either neighbor. This
1474 * routine also has the ability to merge with both neighbors.
1476 * The map must be locked.
1478 * This routine guarentees that the passed entry remains valid (though
1479 * possibly extended). When merging, this routine may delete one or
1483 vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry)
1485 vm_map_entry_t next, prev;
1486 vm_size_t prevsize, esize;
1488 if (entry->eflags & (MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP))
1492 if (prev != &map->header) {
1493 prevsize = prev->end - prev->start;
1494 if ( (prev->end == entry->start) &&
1495 (prev->object.vm_object == entry->object.vm_object) &&
1496 (!prev->object.vm_object ||
1497 (prev->offset + prevsize == entry->offset)) &&
1498 (prev->eflags == entry->eflags) &&
1499 (prev->protection == entry->protection) &&
1500 (prev->max_protection == entry->max_protection) &&
1501 (prev->inheritance == entry->inheritance) &&
1502 (prev->wired_count == entry->wired_count) &&
1503 (prev->uip == entry->uip)) {
1504 vm_map_entry_unlink(map, prev);
1505 entry->start = prev->start;
1506 entry->offset = prev->offset;
1507 if (entry->prev != &map->header)
1508 vm_map_entry_resize_free(map, entry->prev);
1511 * If the backing object is a vnode object,
1512 * vm_object_deallocate() calls vrele().
1513 * However, vrele() does not lock the vnode
1514 * because the vnode has additional
1515 * references. Thus, the map lock can be kept
1516 * without causing a lock-order reversal with
1519 if (prev->object.vm_object)
1520 vm_object_deallocate(prev->object.vm_object);
1521 if (prev->uip != NULL)
1523 vm_map_entry_dispose(map, prev);
1528 if (next != &map->header) {
1529 esize = entry->end - entry->start;
1530 if ((entry->end == next->start) &&
1531 (next->object.vm_object == entry->object.vm_object) &&
1532 (!entry->object.vm_object ||
1533 (entry->offset + esize == next->offset)) &&
1534 (next->eflags == entry->eflags) &&
1535 (next->protection == entry->protection) &&
1536 (next->max_protection == entry->max_protection) &&
1537 (next->inheritance == entry->inheritance) &&
1538 (next->wired_count == entry->wired_count) &&
1539 (next->uip == entry->uip)) {
1540 vm_map_entry_unlink(map, next);
1541 entry->end = next->end;
1542 vm_map_entry_resize_free(map, entry);
1545 * See comment above.
1547 if (next->object.vm_object)
1548 vm_object_deallocate(next->object.vm_object);
1549 if (next->uip != NULL)
1551 vm_map_entry_dispose(map, next);
1556 * vm_map_clip_start: [ internal use only ]
1558 * Asserts that the given entry begins at or after
1559 * the specified address; if necessary,
1560 * it splits the entry into two.
1562 #define vm_map_clip_start(map, entry, startaddr) \
1564 if (startaddr > entry->start) \
1565 _vm_map_clip_start(map, entry, startaddr); \
1569 * This routine is called only when it is known that
1570 * the entry must be split.
1573 _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start)
1575 vm_map_entry_t new_entry;
1577 VM_MAP_ASSERT_LOCKED(map);
1580 * Split off the front portion -- note that we must insert the new
1581 * entry BEFORE this one, so that this entry has the specified
1584 vm_map_simplify_entry(map, entry);
1587 * If there is no object backing this entry, we might as well create
1588 * one now. If we defer it, an object can get created after the map
1589 * is clipped, and individual objects will be created for the split-up
1590 * map. This is a bit of a hack, but is also about the best place to
1591 * put this improvement.
1593 if (entry->object.vm_object == NULL && !map->system_map) {
1595 object = vm_object_allocate(OBJT_DEFAULT,
1596 atop(entry->end - entry->start));
1597 entry->object.vm_object = object;
1599 if (entry->uip != NULL) {
1600 object->uip = entry->uip;
1601 object->charge = entry->end - entry->start;
1604 } else if (entry->object.vm_object != NULL &&
1605 ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) &&
1606 entry->uip != NULL) {
1607 VM_OBJECT_LOCK(entry->object.vm_object);
1608 KASSERT(entry->object.vm_object->uip == NULL,
1609 ("OVERCOMMIT: vm_entry_clip_start: both uip e %p", entry));
1610 entry->object.vm_object->uip = entry->uip;
1611 entry->object.vm_object->charge = entry->end - entry->start;
1612 VM_OBJECT_UNLOCK(entry->object.vm_object);
1616 new_entry = vm_map_entry_create(map);
1617 *new_entry = *entry;
1619 new_entry->end = start;
1620 entry->offset += (start - entry->start);
1621 entry->start = start;
1622 if (new_entry->uip != NULL)
1625 vm_map_entry_link(map, entry->prev, new_entry);
1627 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1628 vm_object_reference(new_entry->object.vm_object);
1633 * vm_map_clip_end: [ internal use only ]
1635 * Asserts that the given entry ends at or before
1636 * the specified address; if necessary,
1637 * it splits the entry into two.
1639 #define vm_map_clip_end(map, entry, endaddr) \
1641 if ((endaddr) < (entry->end)) \
1642 _vm_map_clip_end((map), (entry), (endaddr)); \
1646 * This routine is called only when it is known that
1647 * the entry must be split.
1650 _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end)
1652 vm_map_entry_t new_entry;
1654 VM_MAP_ASSERT_LOCKED(map);
1657 * If there is no object backing this entry, we might as well create
1658 * one now. If we defer it, an object can get created after the map
1659 * is clipped, and individual objects will be created for the split-up
1660 * map. This is a bit of a hack, but is also about the best place to
1661 * put this improvement.
1663 if (entry->object.vm_object == NULL && !map->system_map) {
1665 object = vm_object_allocate(OBJT_DEFAULT,
1666 atop(entry->end - entry->start));
1667 entry->object.vm_object = object;
1669 if (entry->uip != NULL) {
1670 object->uip = entry->uip;
1671 object->charge = entry->end - entry->start;
1674 } else if (entry->object.vm_object != NULL &&
1675 ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) &&
1676 entry->uip != NULL) {
1677 VM_OBJECT_LOCK(entry->object.vm_object);
1678 KASSERT(entry->object.vm_object->uip == NULL,
1679 ("OVERCOMMIT: vm_entry_clip_end: both uip e %p", entry));
1680 entry->object.vm_object->uip = entry->uip;
1681 entry->object.vm_object->charge = entry->end - entry->start;
1682 VM_OBJECT_UNLOCK(entry->object.vm_object);
1687 * Create a new entry and insert it AFTER the specified entry
1689 new_entry = vm_map_entry_create(map);
1690 *new_entry = *entry;
1692 new_entry->start = entry->end = end;
1693 new_entry->offset += (end - entry->start);
1694 if (new_entry->uip != NULL)
1697 vm_map_entry_link(map, entry, new_entry);
1699 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1700 vm_object_reference(new_entry->object.vm_object);
1705 * vm_map_submap: [ kernel use only ]
1707 * Mark the given range as handled by a subordinate map.
1709 * This range must have been created with vm_map_find,
1710 * and no other operations may have been performed on this
1711 * range prior to calling vm_map_submap.
1713 * Only a limited number of operations can be performed
1714 * within this rage after calling vm_map_submap:
1716 * [Don't try vm_map_copy!]
1718 * To remove a submapping, one must first remove the
1719 * range from the superior map, and then destroy the
1720 * submap (if desired). [Better yet, don't try it.]
1729 vm_map_entry_t entry;
1730 int result = KERN_INVALID_ARGUMENT;
1734 VM_MAP_RANGE_CHECK(map, start, end);
1736 if (vm_map_lookup_entry(map, start, &entry)) {
1737 vm_map_clip_start(map, entry, start);
1739 entry = entry->next;
1741 vm_map_clip_end(map, entry, end);
1743 if ((entry->start == start) && (entry->end == end) &&
1744 ((entry->eflags & MAP_ENTRY_COW) == 0) &&
1745 (entry->object.vm_object == NULL)) {
1746 entry->object.sub_map = submap;
1747 entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
1748 result = KERN_SUCCESS;
1756 * The maximum number of pages to map
1758 #define MAX_INIT_PT 96
1761 * vm_map_pmap_enter:
1763 * Preload read-only mappings for the given object's resident pages into
1764 * the given map. This eliminates the soft faults on process startup and
1765 * immediately after an mmap(2). Because these are speculative mappings,
1766 * cached pages are not reactivated and mapped.
1769 vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
1770 vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags)
1773 vm_page_t p, p_start;
1774 vm_pindex_t psize, tmpidx;
1775 boolean_t are_queues_locked;
1777 if ((prot & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 || object == NULL)
1779 VM_OBJECT_LOCK(object);
1780 if (object->type == OBJT_DEVICE || object->type == OBJT_SG) {
1781 pmap_object_init_pt(map->pmap, addr, object, pindex, size);
1787 if ((flags & MAP_PREFAULT_PARTIAL) && psize > MAX_INIT_PT &&
1788 object->resident_page_count > MAX_INIT_PT)
1791 if (psize + pindex > object->size) {
1792 if (object->size < pindex)
1794 psize = object->size - pindex;
1797 are_queues_locked = FALSE;
1801 p = vm_page_find_least(object, pindex);
1803 * Assert: the variable p is either (1) the page with the
1804 * least pindex greater than or equal to the parameter pindex
1808 p != NULL && (tmpidx = p->pindex - pindex) < psize;
1809 p = TAILQ_NEXT(p, listq)) {
1811 * don't allow an madvise to blow away our really
1812 * free pages allocating pv entries.
1814 if ((flags & MAP_PREFAULT_MADVISE) &&
1815 cnt.v_free_count < cnt.v_free_reserved) {
1819 if (p->valid == VM_PAGE_BITS_ALL) {
1820 if (p_start == NULL) {
1821 start = addr + ptoa(tmpidx);
1824 } else if (p_start != NULL) {
1825 if (!are_queues_locked) {
1826 are_queues_locked = TRUE;
1827 vm_page_lock_queues();
1829 pmap_enter_object(map->pmap, start, addr +
1830 ptoa(tmpidx), p_start, prot);
1834 if (p_start != NULL) {
1835 if (!are_queues_locked) {
1836 are_queues_locked = TRUE;
1837 vm_page_lock_queues();
1839 pmap_enter_object(map->pmap, start, addr + ptoa(psize),
1842 if (are_queues_locked)
1843 vm_page_unlock_queues();
1845 VM_OBJECT_UNLOCK(object);
1851 * Sets the protection of the specified address
1852 * region in the target map. If "set_max" is
1853 * specified, the maximum protection is to be set;
1854 * otherwise, only the current protection is affected.
1857 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
1858 vm_prot_t new_prot, boolean_t set_max)
1860 vm_map_entry_t current, entry;
1862 struct uidinfo *uip;
1867 VM_MAP_RANGE_CHECK(map, start, end);
1869 if (vm_map_lookup_entry(map, start, &entry)) {
1870 vm_map_clip_start(map, entry, start);
1872 entry = entry->next;
1876 * Make a first pass to check for protection violations.
1879 while ((current != &map->header) && (current->start < end)) {
1880 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1882 return (KERN_INVALID_ARGUMENT);
1884 if ((new_prot & current->max_protection) != new_prot) {
1886 return (KERN_PROTECTION_FAILURE);
1888 current = current->next;
1893 * Do an accounting pass for private read-only mappings that
1894 * now will do cow due to allowed write (e.g. debugger sets
1895 * breakpoint on text segment)
1897 for (current = entry; (current != &map->header) &&
1898 (current->start < end); current = current->next) {
1900 vm_map_clip_end(map, current, end);
1903 ((new_prot & ~(current->protection)) & VM_PROT_WRITE) == 0 ||
1904 ENTRY_CHARGED(current)) {
1908 uip = curthread->td_ucred->cr_ruidinfo;
1909 obj = current->object.vm_object;
1911 if (obj == NULL || (current->eflags & MAP_ENTRY_NEEDS_COPY)) {
1912 if (!swap_reserve(current->end - current->start)) {
1914 return (KERN_RESOURCE_SHORTAGE);
1921 VM_OBJECT_LOCK(obj);
1922 if (obj->type != OBJT_DEFAULT && obj->type != OBJT_SWAP) {
1923 VM_OBJECT_UNLOCK(obj);
1928 * Charge for the whole object allocation now, since
1929 * we cannot distinguish between non-charged and
1930 * charged clipped mapping of the same object later.
1932 KASSERT(obj->charge == 0,
1933 ("vm_map_protect: object %p overcharged\n", obj));
1934 if (!swap_reserve(ptoa(obj->size))) {
1935 VM_OBJECT_UNLOCK(obj);
1937 return (KERN_RESOURCE_SHORTAGE);
1942 obj->charge = ptoa(obj->size);
1943 VM_OBJECT_UNLOCK(obj);
1947 * Go back and fix up protections. [Note that clipping is not
1948 * necessary the second time.]
1951 while ((current != &map->header) && (current->start < end)) {
1952 old_prot = current->protection;
1955 current->protection =
1956 (current->max_protection = new_prot) &
1959 current->protection = new_prot;
1961 if ((current->eflags & (MAP_ENTRY_COW | MAP_ENTRY_USER_WIRED))
1962 == (MAP_ENTRY_COW | MAP_ENTRY_USER_WIRED) &&
1963 (current->protection & VM_PROT_WRITE) != 0 &&
1964 (old_prot & VM_PROT_WRITE) == 0) {
1965 vm_fault_copy_entry(map, map, current, current, NULL);
1969 * Update physical map if necessary. Worry about copy-on-write
1972 if (current->protection != old_prot) {
1973 #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1975 pmap_protect(map->pmap, current->start,
1977 current->protection & MASK(current));
1980 vm_map_simplify_entry(map, current);
1981 current = current->next;
1984 return (KERN_SUCCESS);
1990 * This routine traverses a processes map handling the madvise
1991 * system call. Advisories are classified as either those effecting
1992 * the vm_map_entry structure, or those effecting the underlying
2002 vm_map_entry_t current, entry;
2006 * Some madvise calls directly modify the vm_map_entry, in which case
2007 * we need to use an exclusive lock on the map and we need to perform
2008 * various clipping operations. Otherwise we only need a read-lock
2013 case MADV_SEQUENTIAL:
2025 vm_map_lock_read(map);
2028 return (KERN_INVALID_ARGUMENT);
2032 * Locate starting entry and clip if necessary.
2034 VM_MAP_RANGE_CHECK(map, start, end);
2036 if (vm_map_lookup_entry(map, start, &entry)) {
2038 vm_map_clip_start(map, entry, start);
2040 entry = entry->next;
2045 * madvise behaviors that are implemented in the vm_map_entry.
2047 * We clip the vm_map_entry so that behavioral changes are
2048 * limited to the specified address range.
2050 for (current = entry;
2051 (current != &map->header) && (current->start < end);
2052 current = current->next
2054 if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
2057 vm_map_clip_end(map, current, end);
2061 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
2063 case MADV_SEQUENTIAL:
2064 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
2067 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
2070 current->eflags |= MAP_ENTRY_NOSYNC;
2073 current->eflags &= ~MAP_ENTRY_NOSYNC;
2076 current->eflags |= MAP_ENTRY_NOCOREDUMP;
2079 current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
2084 vm_map_simplify_entry(map, current);
2088 vm_pindex_t pstart, pend;
2091 * madvise behaviors that are implemented in the underlying
2094 * Since we don't clip the vm_map_entry, we have to clip
2095 * the vm_object pindex and count.
2097 for (current = entry;
2098 (current != &map->header) && (current->start < end);
2099 current = current->next
2101 vm_offset_t useStart;
2103 if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
2106 pstart = OFF_TO_IDX(current->offset);
2107 pend = pstart + atop(current->end - current->start);
2108 useStart = current->start;
2110 if (current->start < start) {
2111 pstart += atop(start - current->start);
2114 if (current->end > end)
2115 pend -= atop(current->end - end);
2120 vm_object_madvise(current->object.vm_object, pstart,
2122 if (behav == MADV_WILLNEED) {
2123 vm_map_pmap_enter(map,
2125 current->protection,
2126 current->object.vm_object,
2128 ptoa(pend - pstart),
2129 MAP_PREFAULT_MADVISE
2133 vm_map_unlock_read(map);
2142 * Sets the inheritance of the specified address
2143 * range in the target map. Inheritance
2144 * affects how the map will be shared with
2145 * child maps at the time of vmspace_fork.
2148 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
2149 vm_inherit_t new_inheritance)
2151 vm_map_entry_t entry;
2152 vm_map_entry_t temp_entry;
2154 switch (new_inheritance) {
2155 case VM_INHERIT_NONE:
2156 case VM_INHERIT_COPY:
2157 case VM_INHERIT_SHARE:
2160 return (KERN_INVALID_ARGUMENT);
2163 VM_MAP_RANGE_CHECK(map, start, end);
2164 if (vm_map_lookup_entry(map, start, &temp_entry)) {
2166 vm_map_clip_start(map, entry, start);
2168 entry = temp_entry->next;
2169 while ((entry != &map->header) && (entry->start < end)) {
2170 vm_map_clip_end(map, entry, end);
2171 entry->inheritance = new_inheritance;
2172 vm_map_simplify_entry(map, entry);
2173 entry = entry->next;
2176 return (KERN_SUCCESS);
2182 * Implements both kernel and user unwiring.
2185 vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
2188 vm_map_entry_t entry, first_entry, tmp_entry;
2189 vm_offset_t saved_start;
2190 unsigned int last_timestamp;
2192 boolean_t need_wakeup, result, user_unwire;
2194 user_unwire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
2196 VM_MAP_RANGE_CHECK(map, start, end);
2197 if (!vm_map_lookup_entry(map, start, &first_entry)) {
2198 if (flags & VM_MAP_WIRE_HOLESOK)
2199 first_entry = first_entry->next;
2202 return (KERN_INVALID_ADDRESS);
2205 last_timestamp = map->timestamp;
2206 entry = first_entry;
2207 while (entry != &map->header && entry->start < end) {
2208 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
2210 * We have not yet clipped the entry.
2212 saved_start = (start >= entry->start) ? start :
2214 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2215 if (vm_map_unlock_and_wait(map, 0)) {
2217 * Allow interruption of user unwiring?
2221 if (last_timestamp+1 != map->timestamp) {
2223 * Look again for the entry because the map was
2224 * modified while it was unlocked.
2225 * Specifically, the entry may have been
2226 * clipped, merged, or deleted.
2228 if (!vm_map_lookup_entry(map, saved_start,
2230 if (flags & VM_MAP_WIRE_HOLESOK)
2231 tmp_entry = tmp_entry->next;
2233 if (saved_start == start) {
2235 * First_entry has been deleted.
2238 return (KERN_INVALID_ADDRESS);
2241 rv = KERN_INVALID_ADDRESS;
2245 if (entry == first_entry)
2246 first_entry = tmp_entry;
2251 last_timestamp = map->timestamp;
2254 vm_map_clip_start(map, entry, start);
2255 vm_map_clip_end(map, entry, end);
2257 * Mark the entry in case the map lock is released. (See
2260 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
2262 * Check the map for holes in the specified region.
2263 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
2265 if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
2266 (entry->end < end && (entry->next == &map->header ||
2267 entry->next->start > entry->end))) {
2269 rv = KERN_INVALID_ADDRESS;
2273 * If system unwiring, require that the entry is system wired.
2276 vm_map_entry_system_wired_count(entry) == 0) {
2278 rv = KERN_INVALID_ARGUMENT;
2281 entry = entry->next;
2285 need_wakeup = FALSE;
2286 if (first_entry == NULL) {
2287 result = vm_map_lookup_entry(map, start, &first_entry);
2288 if (!result && (flags & VM_MAP_WIRE_HOLESOK))
2289 first_entry = first_entry->next;
2291 KASSERT(result, ("vm_map_unwire: lookup failed"));
2293 entry = first_entry;
2294 while (entry != &map->header && entry->start < end) {
2295 if (rv == KERN_SUCCESS && (!user_unwire ||
2296 (entry->eflags & MAP_ENTRY_USER_WIRED))) {
2298 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2299 entry->wired_count--;
2300 if (entry->wired_count == 0) {
2302 * Retain the map lock.
2304 vm_fault_unwire(map, entry->start, entry->end,
2305 entry->object.vm_object != NULL &&
2306 (entry->object.vm_object->type == OBJT_DEVICE ||
2307 entry->object.vm_object->type == OBJT_SG));
2310 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
2311 ("vm_map_unwire: in-transition flag missing"));
2312 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
2313 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
2314 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
2317 vm_map_simplify_entry(map, entry);
2318 entry = entry->next;
2329 * Implements both kernel and user wiring.
2332 vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
2335 vm_map_entry_t entry, first_entry, tmp_entry;
2336 vm_offset_t saved_end, saved_start;
2337 unsigned int last_timestamp;
2339 boolean_t fictitious, need_wakeup, result, user_wire;
2341 user_wire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
2343 VM_MAP_RANGE_CHECK(map, start, end);
2344 if (!vm_map_lookup_entry(map, start, &first_entry)) {
2345 if (flags & VM_MAP_WIRE_HOLESOK)
2346 first_entry = first_entry->next;
2349 return (KERN_INVALID_ADDRESS);
2352 last_timestamp = map->timestamp;
2353 entry = first_entry;
2354 while (entry != &map->header && entry->start < end) {
2355 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
2357 * We have not yet clipped the entry.
2359 saved_start = (start >= entry->start) ? start :
2361 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2362 if (vm_map_unlock_and_wait(map, 0)) {
2364 * Allow interruption of user wiring?
2368 if (last_timestamp + 1 != map->timestamp) {
2370 * Look again for the entry because the map was
2371 * modified while it was unlocked.
2372 * Specifically, the entry may have been
2373 * clipped, merged, or deleted.
2375 if (!vm_map_lookup_entry(map, saved_start,
2377 if (flags & VM_MAP_WIRE_HOLESOK)
2378 tmp_entry = tmp_entry->next;
2380 if (saved_start == start) {
2382 * first_entry has been deleted.
2385 return (KERN_INVALID_ADDRESS);
2388 rv = KERN_INVALID_ADDRESS;
2392 if (entry == first_entry)
2393 first_entry = tmp_entry;
2398 last_timestamp = map->timestamp;
2401 vm_map_clip_start(map, entry, start);
2402 vm_map_clip_end(map, entry, end);
2404 * Mark the entry in case the map lock is released. (See
2407 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
2411 if (entry->wired_count == 0) {
2412 if ((entry->protection & (VM_PROT_READ|VM_PROT_EXECUTE))
2414 entry->eflags |= MAP_ENTRY_WIRE_SKIPPED;
2415 if ((flags & VM_MAP_WIRE_HOLESOK) == 0) {
2417 rv = KERN_INVALID_ADDRESS;
2422 entry->wired_count++;
2423 saved_start = entry->start;
2424 saved_end = entry->end;
2425 fictitious = entry->object.vm_object != NULL &&
2426 (entry->object.vm_object->type == OBJT_DEVICE ||
2427 entry->object.vm_object->type == OBJT_SG);
2429 * Release the map lock, relying on the in-transition
2430 * mark. Mark the map busy for fork.
2434 rv = vm_fault_wire(map, saved_start, saved_end,
2435 user_wire, fictitious);
2438 if (last_timestamp + 1 != map->timestamp) {
2440 * Look again for the entry because the map was
2441 * modified while it was unlocked. The entry
2442 * may have been clipped, but NOT merged or
2445 result = vm_map_lookup_entry(map, saved_start,
2447 KASSERT(result, ("vm_map_wire: lookup failed"));
2448 if (entry == first_entry)
2449 first_entry = tmp_entry;
2453 while (entry->end < saved_end) {
2454 if (rv != KERN_SUCCESS) {
2455 KASSERT(entry->wired_count == 1,
2456 ("vm_map_wire: bad count"));
2457 entry->wired_count = -1;
2459 entry = entry->next;
2462 last_timestamp = map->timestamp;
2463 if (rv != KERN_SUCCESS) {
2464 KASSERT(entry->wired_count == 1,
2465 ("vm_map_wire: bad count"));
2467 * Assign an out-of-range value to represent
2468 * the failure to wire this entry.
2470 entry->wired_count = -1;
2474 } else if (!user_wire ||
2475 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
2476 entry->wired_count++;
2479 * Check the map for holes in the specified region.
2480 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
2483 if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
2484 (entry->end < end && (entry->next == &map->header ||
2485 entry->next->start > entry->end))) {
2487 rv = KERN_INVALID_ADDRESS;
2490 entry = entry->next;
2494 need_wakeup = FALSE;
2495 if (first_entry == NULL) {
2496 result = vm_map_lookup_entry(map, start, &first_entry);
2497 if (!result && (flags & VM_MAP_WIRE_HOLESOK))
2498 first_entry = first_entry->next;
2500 KASSERT(result, ("vm_map_wire: lookup failed"));
2502 entry = first_entry;
2503 while (entry != &map->header && entry->start < end) {
2504 if ((entry->eflags & MAP_ENTRY_WIRE_SKIPPED) != 0)
2505 goto next_entry_done;
2506 if (rv == KERN_SUCCESS) {
2508 entry->eflags |= MAP_ENTRY_USER_WIRED;
2509 } else if (entry->wired_count == -1) {
2511 * Wiring failed on this entry. Thus, unwiring is
2514 entry->wired_count = 0;
2517 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0)
2518 entry->wired_count--;
2519 if (entry->wired_count == 0) {
2521 * Retain the map lock.
2523 vm_fault_unwire(map, entry->start, entry->end,
2524 entry->object.vm_object != NULL &&
2525 (entry->object.vm_object->type == OBJT_DEVICE ||
2526 entry->object.vm_object->type == OBJT_SG));
2530 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
2531 ("vm_map_wire: in-transition flag missing"));
2532 entry->eflags &= ~(MAP_ENTRY_IN_TRANSITION|MAP_ENTRY_WIRE_SKIPPED);
2533 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
2534 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
2537 vm_map_simplify_entry(map, entry);
2538 entry = entry->next;
2549 * Push any dirty cached pages in the address range to their pager.
2550 * If syncio is TRUE, dirty pages are written synchronously.
2551 * If invalidate is TRUE, any cached pages are freed as well.
2553 * If the size of the region from start to end is zero, we are
2554 * supposed to flush all modified pages within the region containing
2555 * start. Unfortunately, a region can be split or coalesced with
2556 * neighboring regions, making it difficult to determine what the
2557 * original region was. Therefore, we approximate this requirement by
2558 * flushing the current region containing start.
2560 * Returns an error if any part of the specified range is not mapped.
2568 boolean_t invalidate)
2570 vm_map_entry_t current;
2571 vm_map_entry_t entry;
2574 vm_ooffset_t offset;
2575 unsigned int last_timestamp;
2578 vm_map_lock_read(map);
2579 VM_MAP_RANGE_CHECK(map, start, end);
2580 if (!vm_map_lookup_entry(map, start, &entry)) {
2581 vm_map_unlock_read(map);
2582 return (KERN_INVALID_ADDRESS);
2583 } else if (start == end) {
2584 start = entry->start;
2588 * Make a first pass to check for user-wired memory and holes.
2590 for (current = entry; current != &map->header && current->start < end;
2591 current = current->next) {
2592 if (invalidate && (current->eflags & MAP_ENTRY_USER_WIRED)) {
2593 vm_map_unlock_read(map);
2594 return (KERN_INVALID_ARGUMENT);
2596 if (end > current->end &&
2597 (current->next == &map->header ||
2598 current->end != current->next->start)) {
2599 vm_map_unlock_read(map);
2600 return (KERN_INVALID_ADDRESS);
2605 pmap_remove(map->pmap, start, end);
2609 * Make a second pass, cleaning/uncaching pages from the indicated
2612 for (current = entry; current != &map->header && current->start < end;) {
2613 offset = current->offset + (start - current->start);
2614 size = (end <= current->end ? end : current->end) - start;
2615 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
2617 vm_map_entry_t tentry;
2620 smap = current->object.sub_map;
2621 vm_map_lock_read(smap);
2622 (void) vm_map_lookup_entry(smap, offset, &tentry);
2623 tsize = tentry->end - offset;
2626 object = tentry->object.vm_object;
2627 offset = tentry->offset + (offset - tentry->start);
2628 vm_map_unlock_read(smap);
2630 object = current->object.vm_object;
2632 vm_object_reference(object);
2633 last_timestamp = map->timestamp;
2634 vm_map_unlock_read(map);
2635 if (!vm_object_sync(object, offset, size, syncio, invalidate))
2638 vm_object_deallocate(object);
2639 vm_map_lock_read(map);
2640 if (last_timestamp == map->timestamp ||
2641 !vm_map_lookup_entry(map, start, ¤t))
2642 current = current->next;
2645 vm_map_unlock_read(map);
2646 return (failed ? KERN_FAILURE : KERN_SUCCESS);
2650 * vm_map_entry_unwire: [ internal use only ]
2652 * Make the region specified by this entry pageable.
2654 * The map in question should be locked.
2655 * [This is the reason for this routine's existence.]
2658 vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
2660 vm_fault_unwire(map, entry->start, entry->end,
2661 entry->object.vm_object != NULL &&
2662 (entry->object.vm_object->type == OBJT_DEVICE ||
2663 entry->object.vm_object->type == OBJT_SG));
2664 entry->wired_count = 0;
2668 vm_map_entry_deallocate(vm_map_entry_t entry, boolean_t system_map)
2671 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0)
2672 vm_object_deallocate(entry->object.vm_object);
2673 uma_zfree(system_map ? kmapentzone : mapentzone, entry);
2677 * vm_map_entry_delete: [ internal use only ]
2679 * Deallocate the given entry from the target map.
2682 vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry)
2685 vm_pindex_t offidxstart, offidxend, count, size1;
2688 vm_map_entry_unlink(map, entry);
2689 object = entry->object.vm_object;
2690 size = entry->end - entry->start;
2693 if (entry->uip != NULL) {
2694 swap_release_by_uid(size, entry->uip);
2698 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
2700 KASSERT(entry->uip == NULL || object->uip == NULL ||
2701 (entry->eflags & MAP_ENTRY_NEEDS_COPY),
2702 ("OVERCOMMIT vm_map_entry_delete: both uip %p", entry));
2703 count = OFF_TO_IDX(size);
2704 offidxstart = OFF_TO_IDX(entry->offset);
2705 offidxend = offidxstart + count;
2706 VM_OBJECT_LOCK(object);
2707 if (object->ref_count != 1 &&
2708 ((object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING ||
2709 object == kernel_object || object == kmem_object)) {
2710 vm_object_collapse(object);
2711 vm_object_page_remove(object, offidxstart, offidxend, FALSE);
2712 if (object->type == OBJT_SWAP)
2713 swap_pager_freespace(object, offidxstart, count);
2714 if (offidxend >= object->size &&
2715 offidxstart < object->size) {
2716 size1 = object->size;
2717 object->size = offidxstart;
2718 if (object->uip != NULL) {
2719 size1 -= object->size;
2720 KASSERT(object->charge >= ptoa(size1),
2721 ("vm_map_entry_delete: object->charge < 0"));
2722 swap_release_by_uid(ptoa(size1), object->uip);
2723 object->charge -= ptoa(size1);
2727 VM_OBJECT_UNLOCK(object);
2729 entry->object.vm_object = NULL;
2730 if (map->system_map)
2731 vm_map_entry_deallocate(entry, TRUE);
2733 entry->next = curthread->td_map_def_user;
2734 curthread->td_map_def_user = entry;
2739 * vm_map_delete: [ internal use only ]
2741 * Deallocates the given address range from the target
2745 vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end)
2747 vm_map_entry_t entry;
2748 vm_map_entry_t first_entry;
2750 VM_MAP_ASSERT_LOCKED(map);
2753 * Find the start of the region, and clip it
2755 if (!vm_map_lookup_entry(map, start, &first_entry))
2756 entry = first_entry->next;
2758 entry = first_entry;
2759 vm_map_clip_start(map, entry, start);
2763 * Step through all entries in this region
2765 while ((entry != &map->header) && (entry->start < end)) {
2766 vm_map_entry_t next;
2769 * Wait for wiring or unwiring of an entry to complete.
2770 * Also wait for any system wirings to disappear on
2773 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 ||
2774 (vm_map_pmap(map) != kernel_pmap &&
2775 vm_map_entry_system_wired_count(entry) != 0)) {
2776 unsigned int last_timestamp;
2777 vm_offset_t saved_start;
2778 vm_map_entry_t tmp_entry;
2780 saved_start = entry->start;
2781 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2782 last_timestamp = map->timestamp;
2783 (void) vm_map_unlock_and_wait(map, 0);
2785 if (last_timestamp + 1 != map->timestamp) {
2787 * Look again for the entry because the map was
2788 * modified while it was unlocked.
2789 * Specifically, the entry may have been
2790 * clipped, merged, or deleted.
2792 if (!vm_map_lookup_entry(map, saved_start,
2794 entry = tmp_entry->next;
2797 vm_map_clip_start(map, entry,
2803 vm_map_clip_end(map, entry, end);
2808 * Unwire before removing addresses from the pmap; otherwise,
2809 * unwiring will put the entries back in the pmap.
2811 if (entry->wired_count != 0) {
2812 vm_map_entry_unwire(map, entry);
2815 pmap_remove(map->pmap, entry->start, entry->end);
2818 * Delete the entry only after removing all pmap
2819 * entries pointing to its pages. (Otherwise, its
2820 * page frames may be reallocated, and any modify bits
2821 * will be set in the wrong object!)
2823 vm_map_entry_delete(map, entry);
2826 return (KERN_SUCCESS);
2832 * Remove the given address range from the target map.
2833 * This is the exported form of vm_map_delete.
2836 vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
2841 VM_MAP_RANGE_CHECK(map, start, end);
2842 result = vm_map_delete(map, start, end);
2848 * vm_map_check_protection:
2850 * Assert that the target map allows the specified privilege on the
2851 * entire address region given. The entire region must be allocated.
2853 * WARNING! This code does not and should not check whether the
2854 * contents of the region is accessible. For example a smaller file
2855 * might be mapped into a larger address space.
2857 * NOTE! This code is also called by munmap().
2859 * The map must be locked. A read lock is sufficient.
2862 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
2863 vm_prot_t protection)
2865 vm_map_entry_t entry;
2866 vm_map_entry_t tmp_entry;
2868 if (!vm_map_lookup_entry(map, start, &tmp_entry))
2872 while (start < end) {
2873 if (entry == &map->header)
2878 if (start < entry->start)
2881 * Check protection associated with entry.
2883 if ((entry->protection & protection) != protection)
2885 /* go to next entry */
2887 entry = entry->next;
2893 * vm_map_copy_entry:
2895 * Copies the contents of the source entry to the destination
2896 * entry. The entries *must* be aligned properly.
2902 vm_map_entry_t src_entry,
2903 vm_map_entry_t dst_entry,
2904 vm_ooffset_t *fork_charge)
2906 vm_object_t src_object;
2908 struct uidinfo *uip;
2911 VM_MAP_ASSERT_LOCKED(dst_map);
2913 if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
2916 if (src_entry->wired_count == 0) {
2919 * If the source entry is marked needs_copy, it is already
2922 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
2923 pmap_protect(src_map->pmap,
2926 src_entry->protection & ~VM_PROT_WRITE);
2930 * Make a copy of the object.
2932 size = src_entry->end - src_entry->start;
2933 if ((src_object = src_entry->object.vm_object) != NULL) {
2934 VM_OBJECT_LOCK(src_object);
2935 charged = ENTRY_CHARGED(src_entry);
2936 if ((src_object->handle == NULL) &&
2937 (src_object->type == OBJT_DEFAULT ||
2938 src_object->type == OBJT_SWAP)) {
2939 vm_object_collapse(src_object);
2940 if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
2941 vm_object_split(src_entry);
2942 src_object = src_entry->object.vm_object;
2945 vm_object_reference_locked(src_object);
2946 vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
2947 if (src_entry->uip != NULL &&
2948 !(src_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
2949 KASSERT(src_object->uip == NULL,
2950 ("OVERCOMMIT: vm_map_copy_entry: uip %p",
2952 src_object->uip = src_entry->uip;
2953 src_object->charge = size;
2955 VM_OBJECT_UNLOCK(src_object);
2956 dst_entry->object.vm_object = src_object;
2958 uip = curthread->td_ucred->cr_ruidinfo;
2960 dst_entry->uip = uip;
2961 *fork_charge += size;
2962 if (!(src_entry->eflags &
2963 MAP_ENTRY_NEEDS_COPY)) {
2965 src_entry->uip = uip;
2966 *fork_charge += size;
2969 src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2970 dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2971 dst_entry->offset = src_entry->offset;
2973 dst_entry->object.vm_object = NULL;
2974 dst_entry->offset = 0;
2975 if (src_entry->uip != NULL) {
2976 dst_entry->uip = curthread->td_ucred->cr_ruidinfo;
2977 uihold(dst_entry->uip);
2978 *fork_charge += size;
2982 pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
2983 dst_entry->end - dst_entry->start, src_entry->start);
2986 * Of course, wired down pages can't be set copy-on-write.
2987 * Cause wired pages to be copied into the new map by
2988 * simulating faults (the new pages are pageable)
2990 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry,
2996 * vmspace_map_entry_forked:
2997 * Update the newly-forked vmspace each time a map entry is inherited
2998 * or copied. The values for vm_dsize and vm_tsize are approximate
2999 * (and mostly-obsolete ideas in the face of mmap(2) et al.)
3002 vmspace_map_entry_forked(const struct vmspace *vm1, struct vmspace *vm2,
3003 vm_map_entry_t entry)
3005 vm_size_t entrysize;
3008 entrysize = entry->end - entry->start;
3009 vm2->vm_map.size += entrysize;
3010 if (entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP)) {
3011 vm2->vm_ssize += btoc(entrysize);
3012 } else if (entry->start >= (vm_offset_t)vm1->vm_daddr &&
3013 entry->start < (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)) {
3014 newend = MIN(entry->end,
3015 (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize));
3016 vm2->vm_dsize += btoc(newend - entry->start);
3017 } else if (entry->start >= (vm_offset_t)vm1->vm_taddr &&
3018 entry->start < (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)) {
3019 newend = MIN(entry->end,
3020 (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize));
3021 vm2->vm_tsize += btoc(newend - entry->start);
3027 * Create a new process vmspace structure and vm_map
3028 * based on those of an existing process. The new map
3029 * is based on the old map, according to the inheritance
3030 * values on the regions in that map.
3032 * XXX It might be worth coalescing the entries added to the new vmspace.
3034 * The source map must not be locked.
3037 vmspace_fork(struct vmspace *vm1, vm_ooffset_t *fork_charge)
3039 struct vmspace *vm2;
3040 vm_map_t old_map = &vm1->vm_map;
3042 vm_map_entry_t old_entry;
3043 vm_map_entry_t new_entry;
3047 vm_map_lock(old_map);
3049 vm_map_wait_busy(old_map);
3050 vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
3052 goto unlock_and_return;
3053 vm2->vm_taddr = vm1->vm_taddr;
3054 vm2->vm_daddr = vm1->vm_daddr;
3055 vm2->vm_maxsaddr = vm1->vm_maxsaddr;
3056 new_map = &vm2->vm_map; /* XXX */
3057 locked = vm_map_trylock(new_map); /* trylock to silence WITNESS */
3058 KASSERT(locked, ("vmspace_fork: lock failed"));
3059 new_map->timestamp = 1;
3061 old_entry = old_map->header.next;
3063 while (old_entry != &old_map->header) {
3064 if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
3065 panic("vm_map_fork: encountered a submap");
3067 switch (old_entry->inheritance) {
3068 case VM_INHERIT_NONE:
3071 case VM_INHERIT_SHARE:
3073 * Clone the entry, creating the shared object if necessary.
3075 object = old_entry->object.vm_object;
3076 if (object == NULL) {
3077 object = vm_object_allocate(OBJT_DEFAULT,
3078 atop(old_entry->end - old_entry->start));
3079 old_entry->object.vm_object = object;
3080 old_entry->offset = 0;
3081 if (old_entry->uip != NULL) {
3082 object->uip = old_entry->uip;
3083 object->charge = old_entry->end -
3085 old_entry->uip = NULL;
3090 * Add the reference before calling vm_object_shadow
3091 * to insure that a shadow object is created.
3093 vm_object_reference(object);
3094 if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3095 vm_object_shadow(&old_entry->object.vm_object,
3097 atop(old_entry->end - old_entry->start));
3098 old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
3099 /* Transfer the second reference too. */
3100 vm_object_reference(
3101 old_entry->object.vm_object);
3104 * As in vm_map_simplify_entry(), the
3105 * vnode lock will not be acquired in
3106 * this call to vm_object_deallocate().
3108 vm_object_deallocate(object);
3109 object = old_entry->object.vm_object;
3111 VM_OBJECT_LOCK(object);
3112 vm_object_clear_flag(object, OBJ_ONEMAPPING);
3113 if (old_entry->uip != NULL) {
3114 KASSERT(object->uip == NULL, ("vmspace_fork both uip"));
3115 object->uip = old_entry->uip;
3116 object->charge = old_entry->end - old_entry->start;
3117 old_entry->uip = NULL;
3119 VM_OBJECT_UNLOCK(object);
3122 * Clone the entry, referencing the shared object.
3124 new_entry = vm_map_entry_create(new_map);
3125 *new_entry = *old_entry;
3126 new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
3127 MAP_ENTRY_IN_TRANSITION);
3128 new_entry->wired_count = 0;
3131 * Insert the entry into the new map -- we know we're
3132 * inserting at the end of the new map.
3134 vm_map_entry_link(new_map, new_map->header.prev,
3136 vmspace_map_entry_forked(vm1, vm2, new_entry);
3139 * Update the physical map
3141 pmap_copy(new_map->pmap, old_map->pmap,
3143 (old_entry->end - old_entry->start),
3147 case VM_INHERIT_COPY:
3149 * Clone the entry and link into the map.
3151 new_entry = vm_map_entry_create(new_map);
3152 *new_entry = *old_entry;
3153 new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
3154 MAP_ENTRY_IN_TRANSITION);
3155 new_entry->wired_count = 0;
3156 new_entry->object.vm_object = NULL;
3157 new_entry->uip = NULL;
3158 vm_map_entry_link(new_map, new_map->header.prev,
3160 vmspace_map_entry_forked(vm1, vm2, new_entry);
3161 vm_map_copy_entry(old_map, new_map, old_entry,
3162 new_entry, fork_charge);
3165 old_entry = old_entry->next;
3168 vm_map_unlock(old_map);
3170 vm_map_unlock(new_map);
3176 vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
3177 vm_prot_t prot, vm_prot_t max, int cow)
3179 vm_map_entry_t new_entry, prev_entry;
3180 vm_offset_t bot, top;
3181 vm_size_t init_ssize;
3186 * The stack orientation is piggybacked with the cow argument.
3187 * Extract it into orient and mask the cow argument so that we
3188 * don't pass it around further.
3189 * NOTE: We explicitly allow bi-directional stacks.
3191 orient = cow & (MAP_STACK_GROWS_DOWN|MAP_STACK_GROWS_UP);
3193 KASSERT(orient != 0, ("No stack grow direction"));
3195 if (addrbos < vm_map_min(map) ||
3196 addrbos > vm_map_max(map) ||
3197 addrbos + max_ssize < addrbos)
3198 return (KERN_NO_SPACE);
3200 init_ssize = (max_ssize < sgrowsiz) ? max_ssize : sgrowsiz;
3202 PROC_LOCK(curthread->td_proc);
3203 vmemlim = lim_cur(curthread->td_proc, RLIMIT_VMEM);
3204 PROC_UNLOCK(curthread->td_proc);
3208 /* If addr is already mapped, no go */
3209 if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
3211 return (KERN_NO_SPACE);
3214 /* If we would blow our VMEM resource limit, no go */
3215 if (map->size + init_ssize > vmemlim) {
3217 return (KERN_NO_SPACE);
3221 * If we can't accomodate max_ssize in the current mapping, no go.
3222 * However, we need to be aware that subsequent user mappings might
3223 * map into the space we have reserved for stack, and currently this
3224 * space is not protected.
3226 * Hopefully we will at least detect this condition when we try to
3229 if ((prev_entry->next != &map->header) &&
3230 (prev_entry->next->start < addrbos + max_ssize)) {
3232 return (KERN_NO_SPACE);
3236 * We initially map a stack of only init_ssize. We will grow as
3237 * needed later. Depending on the orientation of the stack (i.e.
3238 * the grow direction) we either map at the top of the range, the
3239 * bottom of the range or in the middle.
3241 * Note: we would normally expect prot and max to be VM_PROT_ALL,
3242 * and cow to be 0. Possibly we should eliminate these as input
3243 * parameters, and just pass these values here in the insert call.
3245 if (orient == MAP_STACK_GROWS_DOWN)
3246 bot = addrbos + max_ssize - init_ssize;
3247 else if (orient == MAP_STACK_GROWS_UP)
3250 bot = round_page(addrbos + max_ssize/2 - init_ssize/2);
3251 top = bot + init_ssize;
3252 rv = vm_map_insert(map, NULL, 0, bot, top, prot, max, cow);
3254 /* Now set the avail_ssize amount. */
3255 if (rv == KERN_SUCCESS) {
3256 if (prev_entry != &map->header)
3257 vm_map_clip_end(map, prev_entry, bot);
3258 new_entry = prev_entry->next;
3259 if (new_entry->end != top || new_entry->start != bot)
3260 panic("Bad entry start/end for new stack entry");
3262 new_entry->avail_ssize = max_ssize - init_ssize;
3263 if (orient & MAP_STACK_GROWS_DOWN)
3264 new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
3265 if (orient & MAP_STACK_GROWS_UP)
3266 new_entry->eflags |= MAP_ENTRY_GROWS_UP;
3273 static int stack_guard_page = 0;
3274 TUNABLE_INT("security.bsd.stack_guard_page", &stack_guard_page);
3275 SYSCTL_INT(_security_bsd, OID_AUTO, stack_guard_page, CTLFLAG_RW,
3276 &stack_guard_page, 0,
3277 "Insert stack guard page ahead of the growable segments.");
3279 /* Attempts to grow a vm stack entry. Returns KERN_SUCCESS if the
3280 * desired address is already mapped, or if we successfully grow
3281 * the stack. Also returns KERN_SUCCESS if addr is outside the
3282 * stack range (this is strange, but preserves compatibility with
3283 * the grow function in vm_machdep.c).
3286 vm_map_growstack(struct proc *p, vm_offset_t addr)
3288 vm_map_entry_t next_entry, prev_entry;
3289 vm_map_entry_t new_entry, stack_entry;
3290 struct vmspace *vm = p->p_vmspace;
3291 vm_map_t map = &vm->vm_map;
3293 size_t grow_amount, max_grow;
3294 rlim_t stacklim, vmemlim;
3295 int is_procstack, rv;
3296 struct uidinfo *uip;
3300 stacklim = lim_cur(p, RLIMIT_STACK);
3301 vmemlim = lim_cur(p, RLIMIT_VMEM);
3304 vm_map_lock_read(map);
3306 /* If addr is already in the entry range, no need to grow.*/
3307 if (vm_map_lookup_entry(map, addr, &prev_entry)) {
3308 vm_map_unlock_read(map);
3309 return (KERN_SUCCESS);
3312 next_entry = prev_entry->next;
3313 if (!(prev_entry->eflags & MAP_ENTRY_GROWS_UP)) {
3315 * This entry does not grow upwards. Since the address lies
3316 * beyond this entry, the next entry (if one exists) has to
3317 * be a downward growable entry. The entry list header is
3318 * never a growable entry, so it suffices to check the flags.
3320 if (!(next_entry->eflags & MAP_ENTRY_GROWS_DOWN)) {
3321 vm_map_unlock_read(map);
3322 return (KERN_SUCCESS);
3324 stack_entry = next_entry;
3327 * This entry grows upward. If the next entry does not at
3328 * least grow downwards, this is the entry we need to grow.
3329 * otherwise we have two possible choices and we have to
3332 if (next_entry->eflags & MAP_ENTRY_GROWS_DOWN) {
3334 * We have two choices; grow the entry closest to
3335 * the address to minimize the amount of growth.
3337 if (addr - prev_entry->end <= next_entry->start - addr)
3338 stack_entry = prev_entry;
3340 stack_entry = next_entry;
3342 stack_entry = prev_entry;
3345 if (stack_entry == next_entry) {
3346 KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_DOWN, ("foo"));
3347 KASSERT(addr < stack_entry->start, ("foo"));
3348 end = (prev_entry != &map->header) ? prev_entry->end :
3349 stack_entry->start - stack_entry->avail_ssize;
3350 grow_amount = roundup(stack_entry->start - addr, PAGE_SIZE);
3351 max_grow = stack_entry->start - end;
3353 KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_UP, ("foo"));
3354 KASSERT(addr >= stack_entry->end, ("foo"));
3355 end = (next_entry != &map->header) ? next_entry->start :
3356 stack_entry->end + stack_entry->avail_ssize;
3357 grow_amount = roundup(addr + 1 - stack_entry->end, PAGE_SIZE);
3358 max_grow = end - stack_entry->end;
3361 if (grow_amount > stack_entry->avail_ssize) {
3362 vm_map_unlock_read(map);
3363 return (KERN_NO_SPACE);
3367 * If there is no longer enough space between the entries nogo, and
3368 * adjust the available space. Note: this should only happen if the
3369 * user has mapped into the stack area after the stack was created,
3370 * and is probably an error.
3372 * This also effectively destroys any guard page the user might have
3373 * intended by limiting the stack size.
3375 if (grow_amount + (stack_guard_page ? PAGE_SIZE : 0) > max_grow) {
3376 if (vm_map_lock_upgrade(map))
3379 stack_entry->avail_ssize = max_grow;
3382 return (KERN_NO_SPACE);
3385 is_procstack = (addr >= (vm_offset_t)vm->vm_maxsaddr) ? 1 : 0;
3388 * If this is the main process stack, see if we're over the stack
3391 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
3392 vm_map_unlock_read(map);
3393 return (KERN_NO_SPACE);
3396 /* Round up the grow amount modulo SGROWSIZ */
3397 grow_amount = roundup (grow_amount, sgrowsiz);
3398 if (grow_amount > stack_entry->avail_ssize)
3399 grow_amount = stack_entry->avail_ssize;
3400 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
3401 grow_amount = trunc_page((vm_size_t)stacklim) -
3405 /* If we would blow our VMEM resource limit, no go */
3406 if (map->size + grow_amount > vmemlim) {
3407 vm_map_unlock_read(map);
3408 return (KERN_NO_SPACE);
3411 if (vm_map_lock_upgrade(map))
3414 if (stack_entry == next_entry) {
3418 /* Get the preliminary new entry start value */
3419 addr = stack_entry->start - grow_amount;
3422 * If this puts us into the previous entry, cut back our
3423 * growth to the available space. Also, see the note above.
3426 stack_entry->avail_ssize = max_grow;
3428 if (stack_guard_page)
3432 rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start,
3433 p->p_sysent->sv_stackprot, VM_PROT_ALL, 0);
3435 /* Adjust the available stack space by the amount we grew. */
3436 if (rv == KERN_SUCCESS) {
3437 if (prev_entry != &map->header)
3438 vm_map_clip_end(map, prev_entry, addr);
3439 new_entry = prev_entry->next;
3440 KASSERT(new_entry == stack_entry->prev, ("foo"));
3441 KASSERT(new_entry->end == stack_entry->start, ("foo"));
3442 KASSERT(new_entry->start == addr, ("foo"));
3443 grow_amount = new_entry->end - new_entry->start;
3444 new_entry->avail_ssize = stack_entry->avail_ssize -
3446 stack_entry->eflags &= ~MAP_ENTRY_GROWS_DOWN;
3447 new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
3453 addr = stack_entry->end + grow_amount;
3456 * If this puts us into the next entry, cut back our growth
3457 * to the available space. Also, see the note above.
3460 stack_entry->avail_ssize = end - stack_entry->end;
3462 if (stack_guard_page)
3466 grow_amount = addr - stack_entry->end;
3467 uip = stack_entry->uip;
3468 if (uip == NULL && stack_entry->object.vm_object != NULL)
3469 uip = stack_entry->object.vm_object->uip;
3470 if (uip != NULL && !swap_reserve_by_uid(grow_amount, uip))
3472 /* Grow the underlying object if applicable. */
3473 else if (stack_entry->object.vm_object == NULL ||
3474 vm_object_coalesce(stack_entry->object.vm_object,
3475 stack_entry->offset,
3476 (vm_size_t)(stack_entry->end - stack_entry->start),
3477 (vm_size_t)grow_amount, uip != NULL)) {
3478 map->size += (addr - stack_entry->end);
3479 /* Update the current entry. */
3480 stack_entry->end = addr;
3481 stack_entry->avail_ssize -= grow_amount;
3482 vm_map_entry_resize_free(map, stack_entry);
3485 if (next_entry != &map->header)
3486 vm_map_clip_start(map, next_entry, addr);
3491 if (rv == KERN_SUCCESS && is_procstack)
3492 vm->vm_ssize += btoc(grow_amount);
3497 * Heed the MAP_WIREFUTURE flag if it was set for this process.
3499 if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE)) {
3501 (stack_entry == next_entry) ? addr : addr - grow_amount,
3502 (stack_entry == next_entry) ? stack_entry->start : addr,
3503 (p->p_flag & P_SYSTEM)
3504 ? VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES
3505 : VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
3512 * Unshare the specified VM space for exec. If other processes are
3513 * mapped to it, then create a new one. The new vmspace is null.
3516 vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser)
3518 struct vmspace *oldvmspace = p->p_vmspace;
3519 struct vmspace *newvmspace;
3521 newvmspace = vmspace_alloc(minuser, maxuser);
3522 if (newvmspace == NULL)
3524 newvmspace->vm_swrss = oldvmspace->vm_swrss;
3526 * This code is written like this for prototype purposes. The
3527 * goal is to avoid running down the vmspace here, but let the
3528 * other process's that are still using the vmspace to finally
3529 * run it down. Even though there is little or no chance of blocking
3530 * here, it is a good idea to keep this form for future mods.
3532 PROC_VMSPACE_LOCK(p);
3533 p->p_vmspace = newvmspace;
3534 PROC_VMSPACE_UNLOCK(p);
3535 if (p == curthread->td_proc)
3536 pmap_activate(curthread);
3537 vmspace_free(oldvmspace);
3542 * Unshare the specified VM space for forcing COW. This
3543 * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
3546 vmspace_unshare(struct proc *p)
3548 struct vmspace *oldvmspace = p->p_vmspace;
3549 struct vmspace *newvmspace;
3550 vm_ooffset_t fork_charge;
3552 if (oldvmspace->vm_refcnt == 1)
3555 newvmspace = vmspace_fork(oldvmspace, &fork_charge);
3556 if (newvmspace == NULL)
3558 if (!swap_reserve_by_uid(fork_charge, p->p_ucred->cr_ruidinfo)) {
3559 vmspace_free(newvmspace);
3562 PROC_VMSPACE_LOCK(p);
3563 p->p_vmspace = newvmspace;
3564 PROC_VMSPACE_UNLOCK(p);
3565 if (p == curthread->td_proc)
3566 pmap_activate(curthread);
3567 vmspace_free(oldvmspace);
3574 * Finds the VM object, offset, and
3575 * protection for a given virtual address in the
3576 * specified map, assuming a page fault of the
3579 * Leaves the map in question locked for read; return
3580 * values are guaranteed until a vm_map_lookup_done
3581 * call is performed. Note that the map argument
3582 * is in/out; the returned map must be used in
3583 * the call to vm_map_lookup_done.
3585 * A handle (out_entry) is returned for use in
3586 * vm_map_lookup_done, to make that fast.
3588 * If a lookup is requested with "write protection"
3589 * specified, the map may be changed to perform virtual
3590 * copying operations, although the data referenced will
3594 vm_map_lookup(vm_map_t *var_map, /* IN/OUT */
3596 vm_prot_t fault_typea,
3597 vm_map_entry_t *out_entry, /* OUT */
3598 vm_object_t *object, /* OUT */
3599 vm_pindex_t *pindex, /* OUT */
3600 vm_prot_t *out_prot, /* OUT */
3601 boolean_t *wired) /* OUT */
3603 vm_map_entry_t entry;
3604 vm_map_t map = *var_map;
3606 vm_prot_t fault_type = fault_typea;
3607 vm_object_t eobject;
3608 struct uidinfo *uip;
3613 vm_map_lock_read(map);
3616 * Lookup the faulting address.
3618 if (!vm_map_lookup_entry(map, vaddr, out_entry)) {
3619 vm_map_unlock_read(map);
3620 return (KERN_INVALID_ADDRESS);
3628 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3629 vm_map_t old_map = map;
3631 *var_map = map = entry->object.sub_map;
3632 vm_map_unlock_read(old_map);
3637 * Check whether this task is allowed to have this page.
3638 * Note the special case for MAP_ENTRY_COW
3639 * pages with an override. This is to implement a forced
3640 * COW for debuggers.
3642 if (fault_type & VM_PROT_OVERRIDE_WRITE)
3643 prot = entry->max_protection;
3645 prot = entry->protection;
3646 fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
3647 if ((fault_type & prot) != fault_type) {
3648 vm_map_unlock_read(map);
3649 return (KERN_PROTECTION_FAILURE);
3651 if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
3652 (entry->eflags & MAP_ENTRY_COW) &&
3653 (fault_type & VM_PROT_WRITE) &&
3654 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
3655 vm_map_unlock_read(map);
3656 return (KERN_PROTECTION_FAILURE);
3660 * If this page is not pageable, we have to get it for all possible
3663 *wired = (entry->wired_count != 0);
3665 prot = fault_type = entry->protection;
3666 size = entry->end - entry->start;
3668 * If the entry was copy-on-write, we either ...
3670 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3672 * If we want to write the page, we may as well handle that
3673 * now since we've got the map locked.
3675 * If we don't need to write the page, we just demote the
3676 * permissions allowed.
3678 if (fault_type & VM_PROT_WRITE) {
3680 * Make a new object, and place it in the object
3681 * chain. Note that no new references have appeared
3682 * -- one just moved from the map to the new
3685 if (vm_map_lock_upgrade(map))
3688 if (entry->uip == NULL) {
3690 * The debugger owner is charged for
3693 uip = curthread->td_ucred->cr_ruidinfo;
3695 if (!swap_reserve_by_uid(size, uip)) {
3698 return (KERN_RESOURCE_SHORTAGE);
3703 &entry->object.vm_object,
3706 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
3707 eobject = entry->object.vm_object;
3708 if (eobject->uip != NULL) {
3710 * The object was not shadowed.
3712 swap_release_by_uid(size, entry->uip);
3715 } else if (entry->uip != NULL) {
3716 VM_OBJECT_LOCK(eobject);
3717 eobject->uip = entry->uip;
3718 eobject->charge = size;
3719 VM_OBJECT_UNLOCK(eobject);
3723 vm_map_lock_downgrade(map);
3726 * We're attempting to read a copy-on-write page --
3727 * don't allow writes.
3729 prot &= ~VM_PROT_WRITE;
3734 * Create an object if necessary.
3736 if (entry->object.vm_object == NULL &&
3738 if (vm_map_lock_upgrade(map))
3740 entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
3743 if (entry->uip != NULL) {
3744 VM_OBJECT_LOCK(entry->object.vm_object);
3745 entry->object.vm_object->uip = entry->uip;
3746 entry->object.vm_object->charge = size;
3747 VM_OBJECT_UNLOCK(entry->object.vm_object);
3750 vm_map_lock_downgrade(map);
3754 * Return the object/offset from this entry. If the entry was
3755 * copy-on-write or empty, it has been fixed up.
3757 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
3758 *object = entry->object.vm_object;
3761 return (KERN_SUCCESS);
3765 * vm_map_lookup_locked:
3767 * Lookup the faulting address. A version of vm_map_lookup that returns
3768 * KERN_FAILURE instead of blocking on map lock or memory allocation.
3771 vm_map_lookup_locked(vm_map_t *var_map, /* IN/OUT */
3773 vm_prot_t fault_typea,
3774 vm_map_entry_t *out_entry, /* OUT */
3775 vm_object_t *object, /* OUT */
3776 vm_pindex_t *pindex, /* OUT */
3777 vm_prot_t *out_prot, /* OUT */
3778 boolean_t *wired) /* OUT */
3780 vm_map_entry_t entry;
3781 vm_map_t map = *var_map;
3783 vm_prot_t fault_type = fault_typea;
3786 * Lookup the faulting address.
3788 if (!vm_map_lookup_entry(map, vaddr, out_entry))
3789 return (KERN_INVALID_ADDRESS);
3794 * Fail if the entry refers to a submap.
3796 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
3797 return (KERN_FAILURE);
3800 * Check whether this task is allowed to have this page.
3801 * Note the special case for MAP_ENTRY_COW
3802 * pages with an override. This is to implement a forced
3803 * COW for debuggers.
3805 if (fault_type & VM_PROT_OVERRIDE_WRITE)
3806 prot = entry->max_protection;
3808 prot = entry->protection;
3809 fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
3810 if ((fault_type & prot) != fault_type)
3811 return (KERN_PROTECTION_FAILURE);
3812 if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
3813 (entry->eflags & MAP_ENTRY_COW) &&
3814 (fault_type & VM_PROT_WRITE) &&
3815 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0)
3816 return (KERN_PROTECTION_FAILURE);
3819 * If this page is not pageable, we have to get it for all possible
3822 *wired = (entry->wired_count != 0);
3824 prot = fault_type = entry->protection;
3826 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3828 * Fail if the entry was copy-on-write for a write fault.
3830 if (fault_type & VM_PROT_WRITE)
3831 return (KERN_FAILURE);
3833 * We're attempting to read a copy-on-write page --
3834 * don't allow writes.
3836 prot &= ~VM_PROT_WRITE;
3840 * Fail if an object should be created.
3842 if (entry->object.vm_object == NULL && !map->system_map)
3843 return (KERN_FAILURE);
3846 * Return the object/offset from this entry. If the entry was
3847 * copy-on-write or empty, it has been fixed up.
3849 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
3850 *object = entry->object.vm_object;
3853 return (KERN_SUCCESS);
3857 * vm_map_lookup_done:
3859 * Releases locks acquired by a vm_map_lookup
3860 * (according to the handle returned by that lookup).
3863 vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry)
3866 * Unlock the main-level map
3868 vm_map_unlock_read(map);
3871 #include "opt_ddb.h"
3873 #include <sys/kernel.h>
3875 #include <ddb/ddb.h>
3878 * vm_map_print: [ debug ]
3880 DB_SHOW_COMMAND(map, vm_map_print)
3883 /* XXX convert args. */
3884 vm_map_t map = (vm_map_t)addr;
3885 boolean_t full = have_addr;
3887 vm_map_entry_t entry;
3889 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
3891 (void *)map->pmap, map->nentries, map->timestamp);
3894 if (!full && db_indent)
3898 for (entry = map->header.next; entry != &map->header;
3899 entry = entry->next) {
3900 db_iprintf("map entry %p: start=%p, end=%p\n",
3901 (void *)entry, (void *)entry->start, (void *)entry->end);
3904 static char *inheritance_name[4] =
3905 {"share", "copy", "none", "donate_copy"};
3907 db_iprintf(" prot=%x/%x/%s",
3909 entry->max_protection,
3910 inheritance_name[(int)(unsigned char)entry->inheritance]);
3911 if (entry->wired_count != 0)
3912 db_printf(", wired");
3914 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3915 db_printf(", share=%p, offset=0x%jx\n",
3916 (void *)entry->object.sub_map,
3917 (uintmax_t)entry->offset);
3919 if ((entry->prev == &map->header) ||
3920 (entry->prev->object.sub_map !=
3921 entry->object.sub_map)) {
3923 vm_map_print((db_expr_t)(intptr_t)
3924 entry->object.sub_map,
3925 full, 0, (char *)0);
3929 if (entry->uip != NULL)
3930 db_printf(", uip %d", entry->uip->ui_uid);
3931 db_printf(", object=%p, offset=0x%jx",
3932 (void *)entry->object.vm_object,
3933 (uintmax_t)entry->offset);
3934 if (entry->object.vm_object && entry->object.vm_object->uip)
3935 db_printf(", obj uip %d charge %jx",
3936 entry->object.vm_object->uip->ui_uid,
3937 (uintmax_t)entry->object.vm_object->charge);
3938 if (entry->eflags & MAP_ENTRY_COW)
3939 db_printf(", copy (%s)",
3940 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
3944 if ((entry->prev == &map->header) ||
3945 (entry->prev->object.vm_object !=
3946 entry->object.vm_object)) {
3948 vm_object_print((db_expr_t)(intptr_t)
3949 entry->object.vm_object,
3950 full, 0, (char *)0);
3962 DB_SHOW_COMMAND(procvm, procvm)
3967 p = (struct proc *) addr;
3972 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
3973 (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
3974 (void *)vmspace_pmap(p->p_vmspace));
3976 vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);