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>
72 #include <sys/mutex.h>
74 #include <sys/vmmeter.h>
76 #include <sys/vnode.h>
77 #include <sys/resourcevar.h>
79 #include <sys/sysent.h>
83 #include <vm/vm_param.h>
85 #include <vm/vm_map.h>
86 #include <vm/vm_page.h>
87 #include <vm/vm_object.h>
88 #include <vm/vm_pager.h>
89 #include <vm/vm_kern.h>
90 #include <vm/vm_extern.h>
91 #include <vm/swap_pager.h>
95 * Virtual memory maps provide for the mapping, protection,
96 * and sharing of virtual memory objects. In addition,
97 * this module provides for an efficient virtual copy of
98 * memory from one map to another.
100 * Synchronization is required prior to most operations.
102 * Maps consist of an ordered doubly-linked list of simple
103 * entries; a self-adjusting binary search tree of these
104 * entries is used to speed up lookups.
106 * Since portions of maps are specified by start/end addresses,
107 * which may not align with existing map entries, all
108 * routines merely "clip" entries to these start/end values.
109 * [That is, an entry is split into two, bordering at a
110 * start or end value.] Note that these clippings may not
111 * always be necessary (as the two resulting entries are then
112 * not changed); however, the clipping is done for convenience.
114 * As mentioned above, virtual copy operations are performed
115 * by copying VM object references from one map to
116 * another, and then marking both regions as copy-on-write.
119 static struct mtx map_sleep_mtx;
120 static uma_zone_t mapentzone;
121 static uma_zone_t kmapentzone;
122 static uma_zone_t mapzone;
123 static uma_zone_t vmspace_zone;
124 static struct vm_object kmapentobj;
125 static int vmspace_zinit(void *mem, int size, int flags);
126 static void vmspace_zfini(void *mem, int size);
127 static int vm_map_zinit(void *mem, int ize, int flags);
128 static void vm_map_zfini(void *mem, int size);
129 static void _vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max);
130 static void vm_map_entry_deallocate(vm_map_entry_t entry, boolean_t system_map);
131 static void vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry);
133 static void vm_map_zdtor(void *mem, int size, void *arg);
134 static void vmspace_zdtor(void *mem, int size, void *arg);
137 #define ENTRY_CHARGED(e) ((e)->uip != NULL || \
138 ((e)->object.vm_object != NULL && (e)->object.vm_object->uip != NULL && \
139 !((e)->eflags & MAP_ENTRY_NEEDS_COPY)))
142 * PROC_VMSPACE_{UN,}LOCK() can be a noop as long as vmspaces are type
145 #define PROC_VMSPACE_LOCK(p) do { } while (0)
146 #define PROC_VMSPACE_UNLOCK(p) do { } while (0)
149 * VM_MAP_RANGE_CHECK: [ internal use only ]
151 * Asserts that the starting and ending region
152 * addresses fall within the valid range of the map.
154 #define VM_MAP_RANGE_CHECK(map, start, end) \
156 if (start < vm_map_min(map)) \
157 start = vm_map_min(map); \
158 if (end > vm_map_max(map)) \
159 end = vm_map_max(map); \
167 * Initialize the vm_map module. Must be called before
168 * any other vm_map routines.
170 * Map and entry structures are allocated from the general
171 * purpose memory pool with some exceptions:
173 * - The kernel map and kmem submap are allocated statically.
174 * - Kernel map entries are allocated out of a static pool.
176 * These restrictions are necessary since malloc() uses the
177 * maps and requires map entries.
183 mtx_init(&map_sleep_mtx, "vm map sleep mutex", NULL, MTX_DEF);
184 mapzone = uma_zcreate("MAP", sizeof(struct vm_map), NULL,
190 vm_map_zinit, vm_map_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
191 uma_prealloc(mapzone, MAX_KMAP);
192 kmapentzone = uma_zcreate("KMAP ENTRY", sizeof(struct vm_map_entry),
193 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
194 UMA_ZONE_MTXCLASS | UMA_ZONE_VM);
195 uma_prealloc(kmapentzone, MAX_KMAPENT);
196 mapentzone = uma_zcreate("MAP ENTRY", sizeof(struct vm_map_entry),
197 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
201 vmspace_zfini(void *mem, int size)
205 vm = (struct vmspace *)mem;
206 vm_map_zfini(&vm->vm_map, sizeof(vm->vm_map));
210 vmspace_zinit(void *mem, int size, int flags)
214 vm = (struct vmspace *)mem;
216 vm->vm_map.pmap = NULL;
217 (void)vm_map_zinit(&vm->vm_map, sizeof(vm->vm_map), flags);
222 vm_map_zfini(void *mem, int size)
227 mtx_destroy(&map->system_mtx);
228 sx_destroy(&map->lock);
232 vm_map_zinit(void *mem, int size, int flags)
239 mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
240 sx_init(&map->lock, "user map");
246 vmspace_zdtor(void *mem, int size, void *arg)
250 vm = (struct vmspace *)mem;
252 vm_map_zdtor(&vm->vm_map, sizeof(vm->vm_map), arg);
255 vm_map_zdtor(void *mem, int size, void *arg)
260 KASSERT(map->nentries == 0,
261 ("map %p nentries == %d on free.",
262 map, map->nentries));
263 KASSERT(map->size == 0,
264 ("map %p size == %lu on free.",
265 map, (unsigned long)map->size));
267 #endif /* INVARIANTS */
270 * Allocate a vmspace structure, including a vm_map and pmap,
271 * and initialize those structures. The refcnt is set to 1.
274 vmspace_alloc(min, max)
275 vm_offset_t min, max;
279 vm = uma_zalloc(vmspace_zone, M_WAITOK);
280 if (vm->vm_map.pmap == NULL && !pmap_pinit(vmspace_pmap(vm))) {
281 uma_zfree(vmspace_zone, vm);
284 CTR1(KTR_VM, "vmspace_alloc: %p", vm);
285 _vm_map_init(&vm->vm_map, min, max);
286 vm->vm_map.pmap = vmspace_pmap(vm); /* XXX */
302 uma_zone_set_obj(kmapentzone, &kmapentobj, lmin(cnt.v_page_count,
303 (VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS) / PAGE_SIZE) / 8 +
304 maxproc * 2 + maxfiles);
305 vmspace_zone = uma_zcreate("VMSPACE", sizeof(struct vmspace), NULL,
311 vmspace_zinit, vmspace_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
315 vmspace_dofree(struct vmspace *vm)
317 CTR1(KTR_VM, "vmspace_free: %p", vm);
320 * Make sure any SysV shm is freed, it might not have been in
326 * Lock the map, to wait out all other references to it.
327 * Delete all of the mappings and pages they hold, then call
328 * the pmap module to reclaim anything left.
330 (void)vm_map_remove(&vm->vm_map, vm->vm_map.min_offset,
331 vm->vm_map.max_offset);
334 * XXX Comment out the pmap_release call for now. The
335 * vmspace_zone is marked as UMA_ZONE_NOFREE, and bugs cause
336 * pmap.resident_count to be != 0 on exit sometimes.
338 /* pmap_release(vmspace_pmap(vm)); */
339 uma_zfree(vmspace_zone, vm);
343 vmspace_free(struct vmspace *vm)
347 if (vm->vm_refcnt == 0)
348 panic("vmspace_free: attempt to free already freed vmspace");
351 refcnt = vm->vm_refcnt;
352 while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1));
358 vmspace_exitfree(struct proc *p)
362 PROC_VMSPACE_LOCK(p);
365 PROC_VMSPACE_UNLOCK(p);
366 KASSERT(vm == &vmspace0, ("vmspace_exitfree: wrong vmspace"));
371 vmspace_exit(struct thread *td)
378 * Release user portion of address space.
379 * This releases references to vnodes,
380 * which could cause I/O if the file has been unlinked.
381 * Need to do this early enough that we can still sleep.
383 * The last exiting process to reach this point releases as
384 * much of the environment as it can. vmspace_dofree() is the
385 * slower fallback in case another process had a temporary
386 * reference to the vmspace.
391 atomic_add_int(&vmspace0.vm_refcnt, 1);
393 refcnt = vm->vm_refcnt;
394 if (refcnt > 1 && p->p_vmspace != &vmspace0) {
395 /* Switch now since other proc might free vmspace */
396 PROC_VMSPACE_LOCK(p);
397 p->p_vmspace = &vmspace0;
398 PROC_VMSPACE_UNLOCK(p);
401 } while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1));
403 if (p->p_vmspace != vm) {
404 /* vmspace not yet freed, switch back */
405 PROC_VMSPACE_LOCK(p);
407 PROC_VMSPACE_UNLOCK(p);
410 pmap_remove_pages(vmspace_pmap(vm));
411 /* Switch now since this proc will free vmspace */
412 PROC_VMSPACE_LOCK(p);
413 p->p_vmspace = &vmspace0;
414 PROC_VMSPACE_UNLOCK(p);
420 /* Acquire reference to vmspace owned by another process. */
423 vmspace_acquire_ref(struct proc *p)
428 PROC_VMSPACE_LOCK(p);
431 PROC_VMSPACE_UNLOCK(p);
435 refcnt = vm->vm_refcnt;
436 if (refcnt <= 0) { /* Avoid 0->1 transition */
437 PROC_VMSPACE_UNLOCK(p);
440 } while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt + 1));
441 if (vm != p->p_vmspace) {
442 PROC_VMSPACE_UNLOCK(p);
446 PROC_VMSPACE_UNLOCK(p);
451 _vm_map_lock(vm_map_t map, const char *file, int line)
455 _mtx_lock_flags(&map->system_mtx, 0, file, line);
457 (void)_sx_xlock(&map->lock, 0, file, line);
462 vm_map_process_deferred(void)
465 vm_map_entry_t entry;
469 while ((entry = td->td_map_def_user) != NULL) {
470 td->td_map_def_user = entry->next;
471 vm_map_entry_deallocate(entry, FALSE);
476 _vm_map_unlock(vm_map_t map, const char *file, int line)
480 _mtx_unlock_flags(&map->system_mtx, 0, file, line);
482 _sx_xunlock(&map->lock, file, line);
483 vm_map_process_deferred();
488 _vm_map_lock_read(vm_map_t map, const char *file, int line)
492 _mtx_lock_flags(&map->system_mtx, 0, file, line);
494 (void)_sx_slock(&map->lock, 0, file, line);
498 _vm_map_unlock_read(vm_map_t map, const char *file, int line)
502 _mtx_unlock_flags(&map->system_mtx, 0, file, line);
504 _sx_sunlock(&map->lock, file, line);
505 vm_map_process_deferred();
510 _vm_map_trylock(vm_map_t map, const char *file, int line)
514 error = map->system_map ?
515 !_mtx_trylock(&map->system_mtx, 0, file, line) :
516 !_sx_try_xlock(&map->lock, file, line);
523 _vm_map_trylock_read(vm_map_t map, const char *file, int line)
527 error = map->system_map ?
528 !_mtx_trylock(&map->system_mtx, 0, file, line) :
529 !_sx_try_slock(&map->lock, file, line);
534 * _vm_map_lock_upgrade: [ internal use only ]
536 * Tries to upgrade a read (shared) lock on the specified map to a write
537 * (exclusive) lock. Returns the value "0" if the upgrade succeeds and a
538 * non-zero value if the upgrade fails. If the upgrade fails, the map is
539 * returned without a read or write lock held.
541 * Requires that the map be read locked.
544 _vm_map_lock_upgrade(vm_map_t map, const char *file, int line)
546 unsigned int last_timestamp;
548 if (map->system_map) {
550 _mtx_assert(&map->system_mtx, MA_OWNED, file, line);
553 if (!_sx_try_upgrade(&map->lock, file, line)) {
554 last_timestamp = map->timestamp;
555 _sx_sunlock(&map->lock, file, line);
556 vm_map_process_deferred();
558 * If the map's timestamp does not change while the
559 * map is unlocked, then the upgrade succeeds.
561 (void)_sx_xlock(&map->lock, 0, file, line);
562 if (last_timestamp != map->timestamp) {
563 _sx_xunlock(&map->lock, file, line);
573 _vm_map_lock_downgrade(vm_map_t map, const char *file, int line)
576 if (map->system_map) {
578 _mtx_assert(&map->system_mtx, MA_OWNED, file, line);
581 _sx_downgrade(&map->lock, file, line);
587 * Returns a non-zero value if the caller holds a write (exclusive) lock
588 * on the specified map and the value "0" otherwise.
591 vm_map_locked(vm_map_t map)
595 return (mtx_owned(&map->system_mtx));
597 return (sx_xlocked(&map->lock));
602 _vm_map_assert_locked(vm_map_t map, const char *file, int line)
606 _mtx_assert(&map->system_mtx, MA_OWNED, file, line);
608 _sx_assert(&map->lock, SA_XLOCKED, file, line);
613 _vm_map_assert_locked_read(vm_map_t map, const char *file, int line)
617 _mtx_assert(&map->system_mtx, MA_OWNED, file, line);
619 _sx_assert(&map->lock, SA_SLOCKED, file, line);
623 #define VM_MAP_ASSERT_LOCKED(map) \
624 _vm_map_assert_locked(map, LOCK_FILE, LOCK_LINE)
625 #define VM_MAP_ASSERT_LOCKED_READ(map) \
626 _vm_map_assert_locked_read(map, LOCK_FILE, LOCK_LINE)
628 #define VM_MAP_ASSERT_LOCKED(map)
629 #define VM_MAP_ASSERT_LOCKED_READ(map)
633 * _vm_map_unlock_and_wait:
635 * Atomically releases the lock on the specified map and puts the calling
636 * thread to sleep. The calling thread will remain asleep until either
637 * vm_map_wakeup() is performed on the map or the specified timeout is
640 * WARNING! This function does not perform deferred deallocations of
641 * objects and map entries. Therefore, the calling thread is expected to
642 * reacquire the map lock after reawakening and later perform an ordinary
643 * unlock operation, such as vm_map_unlock(), before completing its
644 * operation on the map.
647 _vm_map_unlock_and_wait(vm_map_t map, int timo, const char *file, int line)
650 mtx_lock(&map_sleep_mtx);
652 _mtx_unlock_flags(&map->system_mtx, 0, file, line);
654 _sx_xunlock(&map->lock, file, line);
655 return (msleep(&map->root, &map_sleep_mtx, PDROP | PVM, "vmmaps",
662 * Awaken any threads that have slept on the map using
663 * vm_map_unlock_and_wait().
666 vm_map_wakeup(vm_map_t map)
670 * Acquire and release map_sleep_mtx to prevent a wakeup()
671 * from being performed (and lost) between the map unlock
672 * and the msleep() in _vm_map_unlock_and_wait().
674 mtx_lock(&map_sleep_mtx);
675 mtx_unlock(&map_sleep_mtx);
680 vmspace_resident_count(struct vmspace *vmspace)
682 return pmap_resident_count(vmspace_pmap(vmspace));
686 vmspace_wired_count(struct vmspace *vmspace)
688 return pmap_wired_count(vmspace_pmap(vmspace));
694 * Creates and returns a new empty VM map with
695 * the given physical map structure, and having
696 * the given lower and upper address bounds.
699 vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max)
703 result = uma_zalloc(mapzone, M_WAITOK);
704 CTR1(KTR_VM, "vm_map_create: %p", result);
705 _vm_map_init(result, min, max);
711 * Initialize an existing vm_map structure
712 * such as that in the vmspace structure.
713 * The pmap is set elsewhere.
716 _vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max)
719 map->header.next = map->header.prev = &map->header;
720 map->needs_wakeup = FALSE;
722 map->min_offset = min;
723 map->max_offset = max;
730 vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max)
732 _vm_map_init(map, min, max);
733 mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
734 sx_init(&map->lock, "user map");
738 * vm_map_entry_dispose: [ internal use only ]
740 * Inverse of vm_map_entry_create.
743 vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry)
745 uma_zfree(map->system_map ? kmapentzone : mapentzone, entry);
749 * vm_map_entry_create: [ internal use only ]
751 * Allocates a VM map entry for insertion.
752 * No entry fields are filled in.
754 static vm_map_entry_t
755 vm_map_entry_create(vm_map_t map)
757 vm_map_entry_t new_entry;
760 new_entry = uma_zalloc(kmapentzone, M_NOWAIT);
762 new_entry = uma_zalloc(mapentzone, M_WAITOK);
763 if (new_entry == NULL)
764 panic("vm_map_entry_create: kernel resources exhausted");
769 * vm_map_entry_set_behavior:
771 * Set the expected access behavior, either normal, random, or
775 vm_map_entry_set_behavior(vm_map_entry_t entry, u_char behavior)
777 entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) |
778 (behavior & MAP_ENTRY_BEHAV_MASK);
782 * vm_map_entry_set_max_free:
784 * Set the max_free field in a vm_map_entry.
787 vm_map_entry_set_max_free(vm_map_entry_t entry)
790 entry->max_free = entry->adj_free;
791 if (entry->left != NULL && entry->left->max_free > entry->max_free)
792 entry->max_free = entry->left->max_free;
793 if (entry->right != NULL && entry->right->max_free > entry->max_free)
794 entry->max_free = entry->right->max_free;
798 * vm_map_entry_splay:
800 * The Sleator and Tarjan top-down splay algorithm with the
801 * following variation. Max_free must be computed bottom-up, so
802 * on the downward pass, maintain the left and right spines in
803 * reverse order. Then, make a second pass up each side to fix
804 * the pointers and compute max_free. The time bound is O(log n)
807 * The new root is the vm_map_entry containing "addr", or else an
808 * adjacent entry (lower or higher) if addr is not in the tree.
810 * The map must be locked, and leaves it so.
812 * Returns: the new root.
814 static vm_map_entry_t
815 vm_map_entry_splay(vm_offset_t addr, vm_map_entry_t root)
817 vm_map_entry_t llist, rlist;
818 vm_map_entry_t ltree, rtree;
821 /* Special case of empty tree. */
826 * Pass One: Splay down the tree until we find addr or a NULL
827 * pointer where addr would go. llist and rlist are the two
828 * sides in reverse order (bottom-up), with llist linked by
829 * the right pointer and rlist linked by the left pointer in
830 * the vm_map_entry. Wait until Pass Two to set max_free on
836 /* root is never NULL in here. */
837 if (addr < root->start) {
841 if (addr < y->start && y->left != NULL) {
842 /* Rotate right and put y on rlist. */
843 root->left = y->right;
845 vm_map_entry_set_max_free(root);
850 /* Put root on rlist. */
855 } else if (addr >= root->end) {
859 if (addr >= y->end && y->right != NULL) {
860 /* Rotate left and put y on llist. */
861 root->right = y->left;
863 vm_map_entry_set_max_free(root);
868 /* Put root on llist. */
878 * Pass Two: Walk back up the two spines, flip the pointers
879 * and set max_free. The subtrees of the root go at the
880 * bottom of llist and rlist.
883 while (llist != NULL) {
885 llist->right = ltree;
886 vm_map_entry_set_max_free(llist);
891 while (rlist != NULL) {
894 vm_map_entry_set_max_free(rlist);
900 * Final assembly: add ltree and rtree as subtrees of root.
904 vm_map_entry_set_max_free(root);
910 * vm_map_entry_{un,}link:
912 * Insert/remove entries from maps.
915 vm_map_entry_link(vm_map_t map,
916 vm_map_entry_t after_where,
917 vm_map_entry_t entry)
921 "vm_map_entry_link: map %p, nentries %d, entry %p, after %p", map,
922 map->nentries, entry, after_where);
923 VM_MAP_ASSERT_LOCKED(map);
925 entry->prev = after_where;
926 entry->next = after_where->next;
927 entry->next->prev = entry;
928 after_where->next = entry;
930 if (after_where != &map->header) {
931 if (after_where != map->root)
932 vm_map_entry_splay(after_where->start, map->root);
933 entry->right = after_where->right;
934 entry->left = after_where;
935 after_where->right = NULL;
936 after_where->adj_free = entry->start - after_where->end;
937 vm_map_entry_set_max_free(after_where);
939 entry->right = map->root;
942 entry->adj_free = (entry->next == &map->header ? map->max_offset :
943 entry->next->start) - entry->end;
944 vm_map_entry_set_max_free(entry);
949 vm_map_entry_unlink(vm_map_t map,
950 vm_map_entry_t entry)
952 vm_map_entry_t next, prev, root;
954 VM_MAP_ASSERT_LOCKED(map);
955 if (entry != map->root)
956 vm_map_entry_splay(entry->start, map->root);
957 if (entry->left == NULL)
960 root = vm_map_entry_splay(entry->start, entry->left);
961 root->right = entry->right;
962 root->adj_free = (entry->next == &map->header ? map->max_offset :
963 entry->next->start) - root->end;
964 vm_map_entry_set_max_free(root);
973 CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map,
974 map->nentries, entry);
978 * vm_map_entry_resize_free:
980 * Recompute the amount of free space following a vm_map_entry
981 * and propagate that value up the tree. Call this function after
982 * resizing a map entry in-place, that is, without a call to
983 * vm_map_entry_link() or _unlink().
985 * The map must be locked, and leaves it so.
988 vm_map_entry_resize_free(vm_map_t map, vm_map_entry_t entry)
992 * Using splay trees without parent pointers, propagating
993 * max_free up the tree is done by moving the entry to the
994 * root and making the change there.
996 if (entry != map->root)
997 map->root = vm_map_entry_splay(entry->start, map->root);
999 entry->adj_free = (entry->next == &map->header ? map->max_offset :
1000 entry->next->start) - entry->end;
1001 vm_map_entry_set_max_free(entry);
1005 * vm_map_lookup_entry: [ internal use only ]
1007 * Finds the map entry containing (or
1008 * immediately preceding) the specified address
1009 * in the given map; the entry is returned
1010 * in the "entry" parameter. The boolean
1011 * result indicates whether the address is
1012 * actually contained in the map.
1015 vm_map_lookup_entry(
1017 vm_offset_t address,
1018 vm_map_entry_t *entry) /* OUT */
1024 * If the map is empty, then the map entry immediately preceding
1025 * "address" is the map's header.
1029 *entry = &map->header;
1030 else if (address >= cur->start && cur->end > address) {
1033 } else if ((locked = vm_map_locked(map)) ||
1034 sx_try_upgrade(&map->lock)) {
1036 * Splay requires a write lock on the map. However, it only
1037 * restructures the binary search tree; it does not otherwise
1038 * change the map. Thus, the map's timestamp need not change
1039 * on a temporary upgrade.
1041 map->root = cur = vm_map_entry_splay(address, cur);
1043 sx_downgrade(&map->lock);
1046 * If "address" is contained within a map entry, the new root
1047 * is that map entry. Otherwise, the new root is a map entry
1048 * immediately before or after "address".
1050 if (address >= cur->start) {
1052 if (cur->end > address)
1058 * Since the map is only locked for read access, perform a
1059 * standard binary search tree lookup for "address".
1062 if (address < cur->start) {
1063 if (cur->left == NULL) {
1068 } else if (cur->end > address) {
1072 if (cur->right == NULL) {
1085 * Inserts the given whole VM object into the target
1086 * map at the specified address range. The object's
1087 * size should match that of the address range.
1089 * Requires that the map be locked, and leaves it so.
1091 * If object is non-NULL, ref count must be bumped by caller
1092 * prior to making call to account for the new entry.
1095 vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1096 vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max,
1099 vm_map_entry_t new_entry;
1100 vm_map_entry_t prev_entry;
1101 vm_map_entry_t temp_entry;
1102 vm_eflags_t protoeflags;
1103 struct uidinfo *uip;
1104 boolean_t charge_prev_obj;
1106 VM_MAP_ASSERT_LOCKED(map);
1109 * Check that the start and end points are not bogus.
1111 if ((start < map->min_offset) || (end > map->max_offset) ||
1113 return (KERN_INVALID_ADDRESS);
1116 * Find the entry prior to the proposed starting address; if it's part
1117 * of an existing entry, this range is bogus.
1119 if (vm_map_lookup_entry(map, start, &temp_entry))
1120 return (KERN_NO_SPACE);
1122 prev_entry = temp_entry;
1125 * Assert that the next entry doesn't overlap the end point.
1127 if ((prev_entry->next != &map->header) &&
1128 (prev_entry->next->start < end))
1129 return (KERN_NO_SPACE);
1132 charge_prev_obj = FALSE;
1134 if (cow & MAP_COPY_ON_WRITE)
1135 protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
1137 if (cow & MAP_NOFAULT) {
1138 protoeflags |= MAP_ENTRY_NOFAULT;
1140 KASSERT(object == NULL,
1141 ("vm_map_insert: paradoxical MAP_NOFAULT request"));
1143 if (cow & MAP_DISABLE_SYNCER)
1144 protoeflags |= MAP_ENTRY_NOSYNC;
1145 if (cow & MAP_DISABLE_COREDUMP)
1146 protoeflags |= MAP_ENTRY_NOCOREDUMP;
1149 KASSERT((object != kmem_object && object != kernel_object) ||
1150 ((object == kmem_object || object == kernel_object) &&
1151 !(protoeflags & MAP_ENTRY_NEEDS_COPY)),
1152 ("kmem or kernel object and cow"));
1153 if (cow & (MAP_ACC_NO_CHARGE | MAP_NOFAULT))
1155 if ((cow & MAP_ACC_CHARGED) || ((prot & VM_PROT_WRITE) &&
1156 ((protoeflags & MAP_ENTRY_NEEDS_COPY) || object == NULL))) {
1157 if (!(cow & MAP_ACC_CHARGED) && !swap_reserve(end - start))
1158 return (KERN_RESOURCE_SHORTAGE);
1159 KASSERT(object == NULL || (protoeflags & MAP_ENTRY_NEEDS_COPY) ||
1160 object->uip == NULL,
1161 ("OVERCOMMIT: vm_map_insert o %p", object));
1162 uip = curthread->td_ucred->cr_ruidinfo;
1164 if (object == NULL && !(protoeflags & MAP_ENTRY_NEEDS_COPY))
1165 charge_prev_obj = TRUE;
1169 /* Expand the kernel pmap, if necessary. */
1170 if (map == kernel_map && end > kernel_vm_end)
1171 pmap_growkernel(end);
1172 if (object != NULL) {
1174 * OBJ_ONEMAPPING must be cleared unless this mapping
1175 * is trivially proven to be the only mapping for any
1176 * of the object's pages. (Object granularity
1177 * reference counting is insufficient to recognize
1178 * aliases with precision.)
1180 VM_OBJECT_LOCK(object);
1181 if (object->ref_count > 1 || object->shadow_count != 0)
1182 vm_object_clear_flag(object, OBJ_ONEMAPPING);
1183 VM_OBJECT_UNLOCK(object);
1185 else if ((prev_entry != &map->header) &&
1186 (prev_entry->eflags == protoeflags) &&
1187 (prev_entry->end == start) &&
1188 (prev_entry->wired_count == 0) &&
1189 (prev_entry->uip == uip ||
1190 (prev_entry->object.vm_object != NULL &&
1191 (prev_entry->object.vm_object->uip == uip))) &&
1192 vm_object_coalesce(prev_entry->object.vm_object,
1194 (vm_size_t)(prev_entry->end - prev_entry->start),
1195 (vm_size_t)(end - prev_entry->end), charge_prev_obj)) {
1197 * We were able to extend the object. Determine if we
1198 * can extend the previous map entry to include the
1199 * new range as well.
1201 if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
1202 (prev_entry->protection == prot) &&
1203 (prev_entry->max_protection == max)) {
1204 map->size += (end - prev_entry->end);
1205 prev_entry->end = end;
1206 vm_map_entry_resize_free(map, prev_entry);
1207 vm_map_simplify_entry(map, prev_entry);
1210 return (KERN_SUCCESS);
1214 * If we can extend the object but cannot extend the
1215 * map entry, we have to create a new map entry. We
1216 * must bump the ref count on the extended object to
1217 * account for it. object may be NULL.
1219 object = prev_entry->object.vm_object;
1220 offset = prev_entry->offset +
1221 (prev_entry->end - prev_entry->start);
1222 vm_object_reference(object);
1223 if (uip != NULL && object != NULL && object->uip != NULL &&
1224 !(prev_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
1225 /* Object already accounts for this uid. */
1232 * NOTE: if conditionals fail, object can be NULL here. This occurs
1233 * in things like the buffer map where we manage kva but do not manage
1238 * Create a new entry
1240 new_entry = vm_map_entry_create(map);
1241 new_entry->start = start;
1242 new_entry->end = end;
1243 new_entry->uip = NULL;
1245 new_entry->eflags = protoeflags;
1246 new_entry->object.vm_object = object;
1247 new_entry->offset = offset;
1248 new_entry->avail_ssize = 0;
1250 new_entry->inheritance = VM_INHERIT_DEFAULT;
1251 new_entry->protection = prot;
1252 new_entry->max_protection = max;
1253 new_entry->wired_count = 0;
1255 KASSERT(uip == NULL || !ENTRY_CHARGED(new_entry),
1256 ("OVERCOMMIT: vm_map_insert leaks vm_map %p", new_entry));
1257 new_entry->uip = uip;
1260 * Insert the new entry into the list
1262 vm_map_entry_link(map, prev_entry, new_entry);
1263 map->size += new_entry->end - new_entry->start;
1267 * Temporarily removed to avoid MAP_STACK panic, due to
1268 * MAP_STACK being a huge hack. Will be added back in
1269 * when MAP_STACK (and the user stack mapping) is fixed.
1272 * It may be possible to simplify the entry
1274 vm_map_simplify_entry(map, new_entry);
1277 if (cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) {
1278 vm_map_pmap_enter(map, start, prot,
1279 object, OFF_TO_IDX(offset), end - start,
1280 cow & MAP_PREFAULT_PARTIAL);
1283 return (KERN_SUCCESS);
1289 * Find the first fit (lowest VM address) for "length" free bytes
1290 * beginning at address >= start in the given map.
1292 * In a vm_map_entry, "adj_free" is the amount of free space
1293 * adjacent (higher address) to this entry, and "max_free" is the
1294 * maximum amount of contiguous free space in its subtree. This
1295 * allows finding a free region in one path down the tree, so
1296 * O(log n) amortized with splay trees.
1298 * The map must be locked, and leaves it so.
1300 * Returns: 0 on success, and starting address in *addr,
1301 * 1 if insufficient space.
1304 vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length,
1305 vm_offset_t *addr) /* OUT */
1307 vm_map_entry_t entry;
1311 * Request must fit within min/max VM address and must avoid
1314 if (start < map->min_offset)
1315 start = map->min_offset;
1316 if (start + length > map->max_offset || start + length < start)
1319 /* Empty tree means wide open address space. */
1320 if (map->root == NULL) {
1326 * After splay, if start comes before root node, then there
1327 * must be a gap from start to the root.
1329 map->root = vm_map_entry_splay(start, map->root);
1330 if (start + length <= map->root->start) {
1336 * Root is the last node that might begin its gap before
1337 * start, and this is the last comparison where address
1338 * wrap might be a problem.
1340 st = (start > map->root->end) ? start : map->root->end;
1341 if (length <= map->root->end + map->root->adj_free - st) {
1346 /* With max_free, can immediately tell if no solution. */
1347 entry = map->root->right;
1348 if (entry == NULL || length > entry->max_free)
1352 * Search the right subtree in the order: left subtree, root,
1353 * right subtree (first fit). The previous splay implies that
1354 * all regions in the right subtree have addresses > start.
1356 while (entry != NULL) {
1357 if (entry->left != NULL && entry->left->max_free >= length)
1358 entry = entry->left;
1359 else if (entry->adj_free >= length) {
1363 entry = entry->right;
1366 /* Can't get here, so panic if we do. */
1367 panic("vm_map_findspace: max_free corrupt");
1371 vm_map_fixed(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1372 vm_offset_t start, vm_size_t length, vm_prot_t prot,
1373 vm_prot_t max, int cow)
1378 end = start + length;
1380 VM_MAP_RANGE_CHECK(map, start, end);
1381 (void) vm_map_delete(map, start, end);
1382 result = vm_map_insert(map, object, offset, start, end, prot,
1389 * vm_map_find finds an unallocated region in the target address
1390 * map with the given length. The search is defined to be
1391 * first-fit from the specified address; the region found is
1392 * returned in the same parameter.
1394 * If object is non-NULL, ref count must be bumped by caller
1395 * prior to making call to account for the new entry.
1398 vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1399 vm_offset_t *addr, /* IN/OUT */
1400 vm_size_t length, int find_space, vm_prot_t prot,
1401 vm_prot_t max, int cow)
1409 if (find_space != VMFS_NO_SPACE) {
1410 if (vm_map_findspace(map, start, length, addr)) {
1412 return (KERN_NO_SPACE);
1414 if (find_space == VMFS_ALIGNED_SPACE)
1415 pmap_align_superpage(object, offset, addr,
1419 result = vm_map_insert(map, object, offset, start, start +
1420 length, prot, max, cow);
1421 } while (result == KERN_NO_SPACE && find_space == VMFS_ALIGNED_SPACE);
1427 * vm_map_simplify_entry:
1429 * Simplify the given map entry by merging with either neighbor. This
1430 * routine also has the ability to merge with both neighbors.
1432 * The map must be locked.
1434 * This routine guarentees that the passed entry remains valid (though
1435 * possibly extended). When merging, this routine may delete one or
1439 vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry)
1441 vm_map_entry_t next, prev;
1442 vm_size_t prevsize, esize;
1444 if (entry->eflags & (MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP))
1448 if (prev != &map->header) {
1449 prevsize = prev->end - prev->start;
1450 if ( (prev->end == entry->start) &&
1451 (prev->object.vm_object == entry->object.vm_object) &&
1452 (!prev->object.vm_object ||
1453 (prev->offset + prevsize == entry->offset)) &&
1454 (prev->eflags == entry->eflags) &&
1455 (prev->protection == entry->protection) &&
1456 (prev->max_protection == entry->max_protection) &&
1457 (prev->inheritance == entry->inheritance) &&
1458 (prev->wired_count == entry->wired_count) &&
1459 (prev->uip == entry->uip)) {
1460 vm_map_entry_unlink(map, prev);
1461 entry->start = prev->start;
1462 entry->offset = prev->offset;
1463 if (entry->prev != &map->header)
1464 vm_map_entry_resize_free(map, entry->prev);
1467 * If the backing object is a vnode object,
1468 * vm_object_deallocate() calls vrele().
1469 * However, vrele() does not lock the vnode
1470 * because the vnode has additional
1471 * references. Thus, the map lock can be kept
1472 * without causing a lock-order reversal with
1475 if (prev->object.vm_object)
1476 vm_object_deallocate(prev->object.vm_object);
1477 if (prev->uip != NULL)
1479 vm_map_entry_dispose(map, prev);
1484 if (next != &map->header) {
1485 esize = entry->end - entry->start;
1486 if ((entry->end == next->start) &&
1487 (next->object.vm_object == entry->object.vm_object) &&
1488 (!entry->object.vm_object ||
1489 (entry->offset + esize == next->offset)) &&
1490 (next->eflags == entry->eflags) &&
1491 (next->protection == entry->protection) &&
1492 (next->max_protection == entry->max_protection) &&
1493 (next->inheritance == entry->inheritance) &&
1494 (next->wired_count == entry->wired_count) &&
1495 (next->uip == entry->uip)) {
1496 vm_map_entry_unlink(map, next);
1497 entry->end = next->end;
1498 vm_map_entry_resize_free(map, entry);
1501 * See comment above.
1503 if (next->object.vm_object)
1504 vm_object_deallocate(next->object.vm_object);
1505 if (next->uip != NULL)
1507 vm_map_entry_dispose(map, next);
1512 * vm_map_clip_start: [ internal use only ]
1514 * Asserts that the given entry begins at or after
1515 * the specified address; if necessary,
1516 * it splits the entry into two.
1518 #define vm_map_clip_start(map, entry, startaddr) \
1520 if (startaddr > entry->start) \
1521 _vm_map_clip_start(map, entry, startaddr); \
1525 * This routine is called only when it is known that
1526 * the entry must be split.
1529 _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start)
1531 vm_map_entry_t new_entry;
1533 VM_MAP_ASSERT_LOCKED(map);
1536 * Split off the front portion -- note that we must insert the new
1537 * entry BEFORE this one, so that this entry has the specified
1540 vm_map_simplify_entry(map, entry);
1543 * If there is no object backing this entry, we might as well create
1544 * one now. If we defer it, an object can get created after the map
1545 * is clipped, and individual objects will be created for the split-up
1546 * map. This is a bit of a hack, but is also about the best place to
1547 * put this improvement.
1549 if (entry->object.vm_object == NULL && !map->system_map) {
1551 object = vm_object_allocate(OBJT_DEFAULT,
1552 atop(entry->end - entry->start));
1553 entry->object.vm_object = object;
1555 if (entry->uip != NULL) {
1556 object->uip = entry->uip;
1557 object->charge = entry->end - entry->start;
1560 } else if (entry->object.vm_object != NULL &&
1561 ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) &&
1562 entry->uip != NULL) {
1563 VM_OBJECT_LOCK(entry->object.vm_object);
1564 KASSERT(entry->object.vm_object->uip == NULL,
1565 ("OVERCOMMIT: vm_entry_clip_start: both uip e %p", entry));
1566 entry->object.vm_object->uip = entry->uip;
1567 entry->object.vm_object->charge = entry->end - entry->start;
1568 VM_OBJECT_UNLOCK(entry->object.vm_object);
1572 new_entry = vm_map_entry_create(map);
1573 *new_entry = *entry;
1575 new_entry->end = start;
1576 entry->offset += (start - entry->start);
1577 entry->start = start;
1578 if (new_entry->uip != NULL)
1581 vm_map_entry_link(map, entry->prev, new_entry);
1583 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1584 vm_object_reference(new_entry->object.vm_object);
1589 * vm_map_clip_end: [ internal use only ]
1591 * Asserts that the given entry ends at or before
1592 * the specified address; if necessary,
1593 * it splits the entry into two.
1595 #define vm_map_clip_end(map, entry, endaddr) \
1597 if ((endaddr) < (entry->end)) \
1598 _vm_map_clip_end((map), (entry), (endaddr)); \
1602 * This routine is called only when it is known that
1603 * the entry must be split.
1606 _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end)
1608 vm_map_entry_t new_entry;
1610 VM_MAP_ASSERT_LOCKED(map);
1613 * If there is no object backing this entry, we might as well create
1614 * one now. If we defer it, an object can get created after the map
1615 * is clipped, and individual objects will be created for the split-up
1616 * map. This is a bit of a hack, but is also about the best place to
1617 * put this improvement.
1619 if (entry->object.vm_object == NULL && !map->system_map) {
1621 object = vm_object_allocate(OBJT_DEFAULT,
1622 atop(entry->end - entry->start));
1623 entry->object.vm_object = object;
1625 if (entry->uip != NULL) {
1626 object->uip = entry->uip;
1627 object->charge = entry->end - entry->start;
1630 } else if (entry->object.vm_object != NULL &&
1631 ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) &&
1632 entry->uip != NULL) {
1633 VM_OBJECT_LOCK(entry->object.vm_object);
1634 KASSERT(entry->object.vm_object->uip == NULL,
1635 ("OVERCOMMIT: vm_entry_clip_end: both uip e %p", entry));
1636 entry->object.vm_object->uip = entry->uip;
1637 entry->object.vm_object->charge = entry->end - entry->start;
1638 VM_OBJECT_UNLOCK(entry->object.vm_object);
1643 * Create a new entry and insert it AFTER the specified entry
1645 new_entry = vm_map_entry_create(map);
1646 *new_entry = *entry;
1648 new_entry->start = entry->end = end;
1649 new_entry->offset += (end - entry->start);
1650 if (new_entry->uip != NULL)
1653 vm_map_entry_link(map, entry, new_entry);
1655 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1656 vm_object_reference(new_entry->object.vm_object);
1661 * vm_map_submap: [ kernel use only ]
1663 * Mark the given range as handled by a subordinate map.
1665 * This range must have been created with vm_map_find,
1666 * and no other operations may have been performed on this
1667 * range prior to calling vm_map_submap.
1669 * Only a limited number of operations can be performed
1670 * within this rage after calling vm_map_submap:
1672 * [Don't try vm_map_copy!]
1674 * To remove a submapping, one must first remove the
1675 * range from the superior map, and then destroy the
1676 * submap (if desired). [Better yet, don't try it.]
1685 vm_map_entry_t entry;
1686 int result = KERN_INVALID_ARGUMENT;
1690 VM_MAP_RANGE_CHECK(map, start, end);
1692 if (vm_map_lookup_entry(map, start, &entry)) {
1693 vm_map_clip_start(map, entry, start);
1695 entry = entry->next;
1697 vm_map_clip_end(map, entry, end);
1699 if ((entry->start == start) && (entry->end == end) &&
1700 ((entry->eflags & MAP_ENTRY_COW) == 0) &&
1701 (entry->object.vm_object == NULL)) {
1702 entry->object.sub_map = submap;
1703 entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
1704 result = KERN_SUCCESS;
1712 * The maximum number of pages to map
1714 #define MAX_INIT_PT 96
1717 * vm_map_pmap_enter:
1719 * Preload read-only mappings for the given object's resident pages into
1720 * the given map. This eliminates the soft faults on process startup and
1721 * immediately after an mmap(2). Because these are speculative mappings,
1722 * cached pages are not reactivated and mapped.
1725 vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
1726 vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags)
1729 vm_page_t p, p_start;
1730 vm_pindex_t psize, tmpidx;
1731 boolean_t are_queues_locked;
1733 if ((prot & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 || object == NULL)
1735 VM_OBJECT_LOCK(object);
1736 if (object->type == OBJT_DEVICE || object->type == OBJT_SG) {
1737 pmap_object_init_pt(map->pmap, addr, object, pindex, size);
1743 if ((flags & MAP_PREFAULT_PARTIAL) && psize > MAX_INIT_PT &&
1744 object->resident_page_count > MAX_INIT_PT)
1747 if (psize + pindex > object->size) {
1748 if (object->size < pindex)
1750 psize = object->size - pindex;
1753 are_queues_locked = FALSE;
1757 p = vm_page_find_least(object, pindex);
1759 * Assert: the variable p is either (1) the page with the
1760 * least pindex greater than or equal to the parameter pindex
1764 p != NULL && (tmpidx = p->pindex - pindex) < psize;
1765 p = TAILQ_NEXT(p, listq)) {
1767 * don't allow an madvise to blow away our really
1768 * free pages allocating pv entries.
1770 if ((flags & MAP_PREFAULT_MADVISE) &&
1771 cnt.v_free_count < cnt.v_free_reserved) {
1775 if (p->valid == VM_PAGE_BITS_ALL) {
1776 if (p_start == NULL) {
1777 start = addr + ptoa(tmpidx);
1780 } else if (p_start != NULL) {
1781 if (!are_queues_locked) {
1782 are_queues_locked = TRUE;
1783 vm_page_lock_queues();
1785 pmap_enter_object(map->pmap, start, addr +
1786 ptoa(tmpidx), p_start, prot);
1790 if (p_start != NULL) {
1791 if (!are_queues_locked) {
1792 are_queues_locked = TRUE;
1793 vm_page_lock_queues();
1795 pmap_enter_object(map->pmap, start, addr + ptoa(psize),
1798 if (are_queues_locked)
1799 vm_page_unlock_queues();
1801 VM_OBJECT_UNLOCK(object);
1807 * Sets the protection of the specified address
1808 * region in the target map. If "set_max" is
1809 * specified, the maximum protection is to be set;
1810 * otherwise, only the current protection is affected.
1813 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
1814 vm_prot_t new_prot, boolean_t set_max)
1816 vm_map_entry_t current, entry;
1818 struct uidinfo *uip;
1823 VM_MAP_RANGE_CHECK(map, start, end);
1825 if (vm_map_lookup_entry(map, start, &entry)) {
1826 vm_map_clip_start(map, entry, start);
1828 entry = entry->next;
1832 * Make a first pass to check for protection violations.
1835 while ((current != &map->header) && (current->start < end)) {
1836 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1838 return (KERN_INVALID_ARGUMENT);
1840 if ((new_prot & current->max_protection) != new_prot) {
1842 return (KERN_PROTECTION_FAILURE);
1844 current = current->next;
1849 * Do an accounting pass for private read-only mappings that
1850 * now will do cow due to allowed write (e.g. debugger sets
1851 * breakpoint on text segment)
1853 for (current = entry; (current != &map->header) &&
1854 (current->start < end); current = current->next) {
1856 vm_map_clip_end(map, current, end);
1859 ((new_prot & ~(current->protection)) & VM_PROT_WRITE) == 0 ||
1860 ENTRY_CHARGED(current)) {
1864 uip = curthread->td_ucred->cr_ruidinfo;
1865 obj = current->object.vm_object;
1867 if (obj == NULL || (current->eflags & MAP_ENTRY_NEEDS_COPY)) {
1868 if (!swap_reserve(current->end - current->start)) {
1870 return (KERN_RESOURCE_SHORTAGE);
1877 VM_OBJECT_LOCK(obj);
1878 if (obj->type != OBJT_DEFAULT && obj->type != OBJT_SWAP) {
1879 VM_OBJECT_UNLOCK(obj);
1884 * Charge for the whole object allocation now, since
1885 * we cannot distinguish between non-charged and
1886 * charged clipped mapping of the same object later.
1888 KASSERT(obj->charge == 0,
1889 ("vm_map_protect: object %p overcharged\n", obj));
1890 if (!swap_reserve(ptoa(obj->size))) {
1891 VM_OBJECT_UNLOCK(obj);
1893 return (KERN_RESOURCE_SHORTAGE);
1898 obj->charge = ptoa(obj->size);
1899 VM_OBJECT_UNLOCK(obj);
1903 * Go back and fix up protections. [Note that clipping is not
1904 * necessary the second time.]
1907 while ((current != &map->header) && (current->start < end)) {
1908 old_prot = current->protection;
1911 current->protection =
1912 (current->max_protection = new_prot) &
1915 current->protection = new_prot;
1917 if ((current->eflags & (MAP_ENTRY_COW | MAP_ENTRY_USER_WIRED))
1918 == (MAP_ENTRY_COW | MAP_ENTRY_USER_WIRED) &&
1919 (current->protection & VM_PROT_WRITE) != 0 &&
1920 (old_prot & VM_PROT_WRITE) == 0) {
1921 vm_fault_copy_entry(map, map, current, current, NULL);
1925 * Update physical map if necessary. Worry about copy-on-write
1928 if (current->protection != old_prot) {
1929 #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1931 pmap_protect(map->pmap, current->start,
1933 current->protection & MASK(current));
1936 vm_map_simplify_entry(map, current);
1937 current = current->next;
1940 return (KERN_SUCCESS);
1946 * This routine traverses a processes map handling the madvise
1947 * system call. Advisories are classified as either those effecting
1948 * the vm_map_entry structure, or those effecting the underlying
1958 vm_map_entry_t current, entry;
1962 * Some madvise calls directly modify the vm_map_entry, in which case
1963 * we need to use an exclusive lock on the map and we need to perform
1964 * various clipping operations. Otherwise we only need a read-lock
1969 case MADV_SEQUENTIAL:
1981 vm_map_lock_read(map);
1984 return (KERN_INVALID_ARGUMENT);
1988 * Locate starting entry and clip if necessary.
1990 VM_MAP_RANGE_CHECK(map, start, end);
1992 if (vm_map_lookup_entry(map, start, &entry)) {
1994 vm_map_clip_start(map, entry, start);
1996 entry = entry->next;
2001 * madvise behaviors that are implemented in the vm_map_entry.
2003 * We clip the vm_map_entry so that behavioral changes are
2004 * limited to the specified address range.
2006 for (current = entry;
2007 (current != &map->header) && (current->start < end);
2008 current = current->next
2010 if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
2013 vm_map_clip_end(map, current, end);
2017 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
2019 case MADV_SEQUENTIAL:
2020 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
2023 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
2026 current->eflags |= MAP_ENTRY_NOSYNC;
2029 current->eflags &= ~MAP_ENTRY_NOSYNC;
2032 current->eflags |= MAP_ENTRY_NOCOREDUMP;
2035 current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
2040 vm_map_simplify_entry(map, current);
2048 * madvise behaviors that are implemented in the underlying
2051 * Since we don't clip the vm_map_entry, we have to clip
2052 * the vm_object pindex and count.
2054 for (current = entry;
2055 (current != &map->header) && (current->start < end);
2056 current = current->next
2058 vm_offset_t useStart;
2060 if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
2063 pindex = OFF_TO_IDX(current->offset);
2064 count = atop(current->end - current->start);
2065 useStart = current->start;
2067 if (current->start < start) {
2068 pindex += atop(start - current->start);
2069 count -= atop(start - current->start);
2072 if (current->end > end)
2073 count -= atop(current->end - end);
2078 vm_object_madvise(current->object.vm_object,
2079 pindex, count, behav);
2080 if (behav == MADV_WILLNEED) {
2081 vm_map_pmap_enter(map,
2083 current->protection,
2084 current->object.vm_object,
2086 (count << PAGE_SHIFT),
2087 MAP_PREFAULT_MADVISE
2091 vm_map_unlock_read(map);
2100 * Sets the inheritance of the specified address
2101 * range in the target map. Inheritance
2102 * affects how the map will be shared with
2103 * child maps at the time of vmspace_fork.
2106 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
2107 vm_inherit_t new_inheritance)
2109 vm_map_entry_t entry;
2110 vm_map_entry_t temp_entry;
2112 switch (new_inheritance) {
2113 case VM_INHERIT_NONE:
2114 case VM_INHERIT_COPY:
2115 case VM_INHERIT_SHARE:
2118 return (KERN_INVALID_ARGUMENT);
2121 VM_MAP_RANGE_CHECK(map, start, end);
2122 if (vm_map_lookup_entry(map, start, &temp_entry)) {
2124 vm_map_clip_start(map, entry, start);
2126 entry = temp_entry->next;
2127 while ((entry != &map->header) && (entry->start < end)) {
2128 vm_map_clip_end(map, entry, end);
2129 entry->inheritance = new_inheritance;
2130 vm_map_simplify_entry(map, entry);
2131 entry = entry->next;
2134 return (KERN_SUCCESS);
2140 * Implements both kernel and user unwiring.
2143 vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
2146 vm_map_entry_t entry, first_entry, tmp_entry;
2147 vm_offset_t saved_start;
2148 unsigned int last_timestamp;
2150 boolean_t need_wakeup, result, user_unwire;
2152 user_unwire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
2154 VM_MAP_RANGE_CHECK(map, start, end);
2155 if (!vm_map_lookup_entry(map, start, &first_entry)) {
2156 if (flags & VM_MAP_WIRE_HOLESOK)
2157 first_entry = first_entry->next;
2160 return (KERN_INVALID_ADDRESS);
2163 last_timestamp = map->timestamp;
2164 entry = first_entry;
2165 while (entry != &map->header && entry->start < end) {
2166 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
2168 * We have not yet clipped the entry.
2170 saved_start = (start >= entry->start) ? start :
2172 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2173 if (vm_map_unlock_and_wait(map, 0)) {
2175 * Allow interruption of user unwiring?
2179 if (last_timestamp+1 != map->timestamp) {
2181 * Look again for the entry because the map was
2182 * modified while it was unlocked.
2183 * Specifically, the entry may have been
2184 * clipped, merged, or deleted.
2186 if (!vm_map_lookup_entry(map, saved_start,
2188 if (flags & VM_MAP_WIRE_HOLESOK)
2189 tmp_entry = tmp_entry->next;
2191 if (saved_start == start) {
2193 * First_entry has been deleted.
2196 return (KERN_INVALID_ADDRESS);
2199 rv = KERN_INVALID_ADDRESS;
2203 if (entry == first_entry)
2204 first_entry = tmp_entry;
2209 last_timestamp = map->timestamp;
2212 vm_map_clip_start(map, entry, start);
2213 vm_map_clip_end(map, entry, end);
2215 * Mark the entry in case the map lock is released. (See
2218 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
2220 * Check the map for holes in the specified region.
2221 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
2223 if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
2224 (entry->end < end && (entry->next == &map->header ||
2225 entry->next->start > entry->end))) {
2227 rv = KERN_INVALID_ADDRESS;
2231 * If system unwiring, require that the entry is system wired.
2234 vm_map_entry_system_wired_count(entry) == 0) {
2236 rv = KERN_INVALID_ARGUMENT;
2239 entry = entry->next;
2243 need_wakeup = FALSE;
2244 if (first_entry == NULL) {
2245 result = vm_map_lookup_entry(map, start, &first_entry);
2246 if (!result && (flags & VM_MAP_WIRE_HOLESOK))
2247 first_entry = first_entry->next;
2249 KASSERT(result, ("vm_map_unwire: lookup failed"));
2251 entry = first_entry;
2252 while (entry != &map->header && entry->start < end) {
2253 if (rv == KERN_SUCCESS && (!user_unwire ||
2254 (entry->eflags & MAP_ENTRY_USER_WIRED))) {
2256 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2257 entry->wired_count--;
2258 if (entry->wired_count == 0) {
2260 * Retain the map lock.
2262 vm_fault_unwire(map, entry->start, entry->end,
2263 entry->object.vm_object != NULL &&
2264 (entry->object.vm_object->type == OBJT_DEVICE ||
2265 entry->object.vm_object->type == OBJT_SG));
2268 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
2269 ("vm_map_unwire: in-transition flag missing"));
2270 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
2271 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
2272 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
2275 vm_map_simplify_entry(map, entry);
2276 entry = entry->next;
2287 * Implements both kernel and user wiring.
2290 vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
2293 vm_map_entry_t entry, first_entry, tmp_entry;
2294 vm_offset_t saved_end, saved_start;
2295 unsigned int last_timestamp;
2297 boolean_t fictitious, need_wakeup, result, user_wire;
2299 user_wire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
2301 VM_MAP_RANGE_CHECK(map, start, end);
2302 if (!vm_map_lookup_entry(map, start, &first_entry)) {
2303 if (flags & VM_MAP_WIRE_HOLESOK)
2304 first_entry = first_entry->next;
2307 return (KERN_INVALID_ADDRESS);
2310 last_timestamp = map->timestamp;
2311 entry = first_entry;
2312 while (entry != &map->header && entry->start < end) {
2313 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
2315 * We have not yet clipped the entry.
2317 saved_start = (start >= entry->start) ? start :
2319 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2320 if (vm_map_unlock_and_wait(map, 0)) {
2322 * Allow interruption of user wiring?
2326 if (last_timestamp + 1 != map->timestamp) {
2328 * Look again for the entry because the map was
2329 * modified while it was unlocked.
2330 * Specifically, the entry may have been
2331 * clipped, merged, or deleted.
2333 if (!vm_map_lookup_entry(map, saved_start,
2335 if (flags & VM_MAP_WIRE_HOLESOK)
2336 tmp_entry = tmp_entry->next;
2338 if (saved_start == start) {
2340 * first_entry has been deleted.
2343 return (KERN_INVALID_ADDRESS);
2346 rv = KERN_INVALID_ADDRESS;
2350 if (entry == first_entry)
2351 first_entry = tmp_entry;
2356 last_timestamp = map->timestamp;
2359 vm_map_clip_start(map, entry, start);
2360 vm_map_clip_end(map, entry, end);
2362 * Mark the entry in case the map lock is released. (See
2365 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
2369 if (entry->wired_count == 0) {
2370 if ((entry->protection & (VM_PROT_READ|VM_PROT_EXECUTE))
2372 entry->eflags |= MAP_ENTRY_WIRE_SKIPPED;
2373 if ((flags & VM_MAP_WIRE_HOLESOK) == 0) {
2375 rv = KERN_INVALID_ADDRESS;
2380 entry->wired_count++;
2381 saved_start = entry->start;
2382 saved_end = entry->end;
2383 fictitious = entry->object.vm_object != NULL &&
2384 (entry->object.vm_object->type == OBJT_DEVICE ||
2385 entry->object.vm_object->type == OBJT_SG);
2387 * Release the map lock, relying on the in-transition
2391 rv = vm_fault_wire(map, saved_start, saved_end,
2392 user_wire, fictitious);
2394 if (last_timestamp + 1 != map->timestamp) {
2396 * Look again for the entry because the map was
2397 * modified while it was unlocked. The entry
2398 * may have been clipped, but NOT merged or
2401 result = vm_map_lookup_entry(map, saved_start,
2403 KASSERT(result, ("vm_map_wire: lookup failed"));
2404 if (entry == first_entry)
2405 first_entry = tmp_entry;
2409 while (entry->end < saved_end) {
2410 if (rv != KERN_SUCCESS) {
2411 KASSERT(entry->wired_count == 1,
2412 ("vm_map_wire: bad count"));
2413 entry->wired_count = -1;
2415 entry = entry->next;
2418 last_timestamp = map->timestamp;
2419 if (rv != KERN_SUCCESS) {
2420 KASSERT(entry->wired_count == 1,
2421 ("vm_map_wire: bad count"));
2423 * Assign an out-of-range value to represent
2424 * the failure to wire this entry.
2426 entry->wired_count = -1;
2430 } else if (!user_wire ||
2431 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
2432 entry->wired_count++;
2435 * Check the map for holes in the specified region.
2436 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
2439 if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
2440 (entry->end < end && (entry->next == &map->header ||
2441 entry->next->start > entry->end))) {
2443 rv = KERN_INVALID_ADDRESS;
2446 entry = entry->next;
2450 need_wakeup = FALSE;
2451 if (first_entry == NULL) {
2452 result = vm_map_lookup_entry(map, start, &first_entry);
2453 if (!result && (flags & VM_MAP_WIRE_HOLESOK))
2454 first_entry = first_entry->next;
2456 KASSERT(result, ("vm_map_wire: lookup failed"));
2458 entry = first_entry;
2459 while (entry != &map->header && entry->start < end) {
2460 if ((entry->eflags & MAP_ENTRY_WIRE_SKIPPED) != 0)
2461 goto next_entry_done;
2462 if (rv == KERN_SUCCESS) {
2464 entry->eflags |= MAP_ENTRY_USER_WIRED;
2465 } else if (entry->wired_count == -1) {
2467 * Wiring failed on this entry. Thus, unwiring is
2470 entry->wired_count = 0;
2473 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0)
2474 entry->wired_count--;
2475 if (entry->wired_count == 0) {
2477 * Retain the map lock.
2479 vm_fault_unwire(map, entry->start, entry->end,
2480 entry->object.vm_object != NULL &&
2481 (entry->object.vm_object->type == OBJT_DEVICE ||
2482 entry->object.vm_object->type == OBJT_SG));
2486 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
2487 ("vm_map_wire: in-transition flag missing"));
2488 entry->eflags &= ~(MAP_ENTRY_IN_TRANSITION|MAP_ENTRY_WIRE_SKIPPED);
2489 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
2490 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
2493 vm_map_simplify_entry(map, entry);
2494 entry = entry->next;
2505 * Push any dirty cached pages in the address range to their pager.
2506 * If syncio is TRUE, dirty pages are written synchronously.
2507 * If invalidate is TRUE, any cached pages are freed as well.
2509 * If the size of the region from start to end is zero, we are
2510 * supposed to flush all modified pages within the region containing
2511 * start. Unfortunately, a region can be split or coalesced with
2512 * neighboring regions, making it difficult to determine what the
2513 * original region was. Therefore, we approximate this requirement by
2514 * flushing the current region containing start.
2516 * Returns an error if any part of the specified range is not mapped.
2524 boolean_t invalidate)
2526 vm_map_entry_t current;
2527 vm_map_entry_t entry;
2530 vm_ooffset_t offset;
2531 unsigned int last_timestamp;
2533 vm_map_lock_read(map);
2534 VM_MAP_RANGE_CHECK(map, start, end);
2535 if (!vm_map_lookup_entry(map, start, &entry)) {
2536 vm_map_unlock_read(map);
2537 return (KERN_INVALID_ADDRESS);
2538 } else if (start == end) {
2539 start = entry->start;
2543 * Make a first pass to check for user-wired memory and holes.
2545 for (current = entry; current != &map->header && current->start < end;
2546 current = current->next) {
2547 if (invalidate && (current->eflags & MAP_ENTRY_USER_WIRED)) {
2548 vm_map_unlock_read(map);
2549 return (KERN_INVALID_ARGUMENT);
2551 if (end > current->end &&
2552 (current->next == &map->header ||
2553 current->end != current->next->start)) {
2554 vm_map_unlock_read(map);
2555 return (KERN_INVALID_ADDRESS);
2560 pmap_remove(map->pmap, start, end);
2563 * Make a second pass, cleaning/uncaching pages from the indicated
2566 for (current = entry; current != &map->header && current->start < end;) {
2567 offset = current->offset + (start - current->start);
2568 size = (end <= current->end ? end : current->end) - start;
2569 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
2571 vm_map_entry_t tentry;
2574 smap = current->object.sub_map;
2575 vm_map_lock_read(smap);
2576 (void) vm_map_lookup_entry(smap, offset, &tentry);
2577 tsize = tentry->end - offset;
2580 object = tentry->object.vm_object;
2581 offset = tentry->offset + (offset - tentry->start);
2582 vm_map_unlock_read(smap);
2584 object = current->object.vm_object;
2586 vm_object_reference(object);
2587 last_timestamp = map->timestamp;
2588 vm_map_unlock_read(map);
2589 vm_object_sync(object, offset, size, syncio, invalidate);
2591 vm_object_deallocate(object);
2592 vm_map_lock_read(map);
2593 if (last_timestamp == map->timestamp ||
2594 !vm_map_lookup_entry(map, start, ¤t))
2595 current = current->next;
2598 vm_map_unlock_read(map);
2599 return (KERN_SUCCESS);
2603 * vm_map_entry_unwire: [ internal use only ]
2605 * Make the region specified by this entry pageable.
2607 * The map in question should be locked.
2608 * [This is the reason for this routine's existence.]
2611 vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
2613 vm_fault_unwire(map, entry->start, entry->end,
2614 entry->object.vm_object != NULL &&
2615 (entry->object.vm_object->type == OBJT_DEVICE ||
2616 entry->object.vm_object->type == OBJT_SG));
2617 entry->wired_count = 0;
2621 vm_map_entry_deallocate(vm_map_entry_t entry, boolean_t system_map)
2624 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0)
2625 vm_object_deallocate(entry->object.vm_object);
2626 uma_zfree(system_map ? kmapentzone : mapentzone, entry);
2630 * vm_map_entry_delete: [ internal use only ]
2632 * Deallocate the given entry from the target map.
2635 vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry)
2638 vm_pindex_t offidxstart, offidxend, count, size1;
2641 vm_map_entry_unlink(map, entry);
2642 object = entry->object.vm_object;
2643 size = entry->end - entry->start;
2646 if (entry->uip != NULL) {
2647 swap_release_by_uid(size, entry->uip);
2651 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
2653 KASSERT(entry->uip == NULL || object->uip == NULL ||
2654 (entry->eflags & MAP_ENTRY_NEEDS_COPY),
2655 ("OVERCOMMIT vm_map_entry_delete: both uip %p", entry));
2656 count = OFF_TO_IDX(size);
2657 offidxstart = OFF_TO_IDX(entry->offset);
2658 offidxend = offidxstart + count;
2659 VM_OBJECT_LOCK(object);
2660 if (object->ref_count != 1 &&
2661 ((object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING ||
2662 object == kernel_object || object == kmem_object)) {
2663 vm_object_collapse(object);
2664 vm_object_page_remove(object, offidxstart, offidxend, FALSE);
2665 if (object->type == OBJT_SWAP)
2666 swap_pager_freespace(object, offidxstart, count);
2667 if (offidxend >= object->size &&
2668 offidxstart < object->size) {
2669 size1 = object->size;
2670 object->size = offidxstart;
2671 if (object->uip != NULL) {
2672 size1 -= object->size;
2673 KASSERT(object->charge >= ptoa(size1),
2674 ("vm_map_entry_delete: object->charge < 0"));
2675 swap_release_by_uid(ptoa(size1), object->uip);
2676 object->charge -= ptoa(size1);
2680 VM_OBJECT_UNLOCK(object);
2682 entry->object.vm_object = NULL;
2683 if (map->system_map)
2684 vm_map_entry_deallocate(entry, TRUE);
2686 entry->next = curthread->td_map_def_user;
2687 curthread->td_map_def_user = entry;
2692 * vm_map_delete: [ internal use only ]
2694 * Deallocates the given address range from the target
2698 vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end)
2700 vm_map_entry_t entry;
2701 vm_map_entry_t first_entry;
2703 VM_MAP_ASSERT_LOCKED(map);
2706 * Find the start of the region, and clip it
2708 if (!vm_map_lookup_entry(map, start, &first_entry))
2709 entry = first_entry->next;
2711 entry = first_entry;
2712 vm_map_clip_start(map, entry, start);
2716 * Step through all entries in this region
2718 while ((entry != &map->header) && (entry->start < end)) {
2719 vm_map_entry_t next;
2722 * Wait for wiring or unwiring of an entry to complete.
2723 * Also wait for any system wirings to disappear on
2726 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 ||
2727 (vm_map_pmap(map) != kernel_pmap &&
2728 vm_map_entry_system_wired_count(entry) != 0)) {
2729 unsigned int last_timestamp;
2730 vm_offset_t saved_start;
2731 vm_map_entry_t tmp_entry;
2733 saved_start = entry->start;
2734 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2735 last_timestamp = map->timestamp;
2736 (void) vm_map_unlock_and_wait(map, 0);
2738 if (last_timestamp + 1 != map->timestamp) {
2740 * Look again for the entry because the map was
2741 * modified while it was unlocked.
2742 * Specifically, the entry may have been
2743 * clipped, merged, or deleted.
2745 if (!vm_map_lookup_entry(map, saved_start,
2747 entry = tmp_entry->next;
2750 vm_map_clip_start(map, entry,
2756 vm_map_clip_end(map, entry, end);
2761 * Unwire before removing addresses from the pmap; otherwise,
2762 * unwiring will put the entries back in the pmap.
2764 if (entry->wired_count != 0) {
2765 vm_map_entry_unwire(map, entry);
2768 pmap_remove(map->pmap, entry->start, entry->end);
2771 * Delete the entry only after removing all pmap
2772 * entries pointing to its pages. (Otherwise, its
2773 * page frames may be reallocated, and any modify bits
2774 * will be set in the wrong object!)
2776 vm_map_entry_delete(map, entry);
2779 return (KERN_SUCCESS);
2785 * Remove the given address range from the target map.
2786 * This is the exported form of vm_map_delete.
2789 vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
2794 VM_MAP_RANGE_CHECK(map, start, end);
2795 result = vm_map_delete(map, start, end);
2801 * vm_map_check_protection:
2803 * Assert that the target map allows the specified privilege on the
2804 * entire address region given. The entire region must be allocated.
2806 * WARNING! This code does not and should not check whether the
2807 * contents of the region is accessible. For example a smaller file
2808 * might be mapped into a larger address space.
2810 * NOTE! This code is also called by munmap().
2812 * The map must be locked. A read lock is sufficient.
2815 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
2816 vm_prot_t protection)
2818 vm_map_entry_t entry;
2819 vm_map_entry_t tmp_entry;
2821 if (!vm_map_lookup_entry(map, start, &tmp_entry))
2825 while (start < end) {
2826 if (entry == &map->header)
2831 if (start < entry->start)
2834 * Check protection associated with entry.
2836 if ((entry->protection & protection) != protection)
2838 /* go to next entry */
2840 entry = entry->next;
2846 * vm_map_copy_entry:
2848 * Copies the contents of the source entry to the destination
2849 * entry. The entries *must* be aligned properly.
2855 vm_map_entry_t src_entry,
2856 vm_map_entry_t dst_entry,
2857 vm_ooffset_t *fork_charge)
2859 vm_object_t src_object;
2861 struct uidinfo *uip;
2864 VM_MAP_ASSERT_LOCKED(dst_map);
2866 if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
2869 if (src_entry->wired_count == 0) {
2872 * If the source entry is marked needs_copy, it is already
2875 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
2876 pmap_protect(src_map->pmap,
2879 src_entry->protection & ~VM_PROT_WRITE);
2883 * Make a copy of the object.
2885 size = src_entry->end - src_entry->start;
2886 if ((src_object = src_entry->object.vm_object) != NULL) {
2887 VM_OBJECT_LOCK(src_object);
2888 charged = ENTRY_CHARGED(src_entry);
2889 if ((src_object->handle == NULL) &&
2890 (src_object->type == OBJT_DEFAULT ||
2891 src_object->type == OBJT_SWAP)) {
2892 vm_object_collapse(src_object);
2893 if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
2894 vm_object_split(src_entry);
2895 src_object = src_entry->object.vm_object;
2898 vm_object_reference_locked(src_object);
2899 vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
2900 if (src_entry->uip != NULL &&
2901 !(src_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
2902 KASSERT(src_object->uip == NULL,
2903 ("OVERCOMMIT: vm_map_copy_entry: uip %p",
2905 src_object->uip = src_entry->uip;
2906 src_object->charge = size;
2908 VM_OBJECT_UNLOCK(src_object);
2909 dst_entry->object.vm_object = src_object;
2911 uip = curthread->td_ucred->cr_ruidinfo;
2913 dst_entry->uip = uip;
2914 *fork_charge += size;
2915 if (!(src_entry->eflags &
2916 MAP_ENTRY_NEEDS_COPY)) {
2918 src_entry->uip = uip;
2919 *fork_charge += size;
2922 src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2923 dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2924 dst_entry->offset = src_entry->offset;
2926 dst_entry->object.vm_object = NULL;
2927 dst_entry->offset = 0;
2928 if (src_entry->uip != NULL) {
2929 dst_entry->uip = curthread->td_ucred->cr_ruidinfo;
2930 uihold(dst_entry->uip);
2931 *fork_charge += size;
2935 pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
2936 dst_entry->end - dst_entry->start, src_entry->start);
2939 * Of course, wired down pages can't be set copy-on-write.
2940 * Cause wired pages to be copied into the new map by
2941 * simulating faults (the new pages are pageable)
2943 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry,
2949 * vmspace_map_entry_forked:
2950 * Update the newly-forked vmspace each time a map entry is inherited
2951 * or copied. The values for vm_dsize and vm_tsize are approximate
2952 * (and mostly-obsolete ideas in the face of mmap(2) et al.)
2955 vmspace_map_entry_forked(const struct vmspace *vm1, struct vmspace *vm2,
2956 vm_map_entry_t entry)
2958 vm_size_t entrysize;
2961 entrysize = entry->end - entry->start;
2962 vm2->vm_map.size += entrysize;
2963 if (entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP)) {
2964 vm2->vm_ssize += btoc(entrysize);
2965 } else if (entry->start >= (vm_offset_t)vm1->vm_daddr &&
2966 entry->start < (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)) {
2967 newend = MIN(entry->end,
2968 (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize));
2969 vm2->vm_dsize += btoc(newend - entry->start);
2970 } else if (entry->start >= (vm_offset_t)vm1->vm_taddr &&
2971 entry->start < (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)) {
2972 newend = MIN(entry->end,
2973 (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize));
2974 vm2->vm_tsize += btoc(newend - entry->start);
2980 * Create a new process vmspace structure and vm_map
2981 * based on those of an existing process. The new map
2982 * is based on the old map, according to the inheritance
2983 * values on the regions in that map.
2985 * XXX It might be worth coalescing the entries added to the new vmspace.
2987 * The source map must not be locked.
2990 vmspace_fork(struct vmspace *vm1, vm_ooffset_t *fork_charge)
2992 struct vmspace *vm2;
2993 vm_map_t old_map = &vm1->vm_map;
2995 vm_map_entry_t old_entry;
2996 vm_map_entry_t new_entry;
3000 vm_map_lock(old_map);
3001 vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
3003 goto unlock_and_return;
3004 vm2->vm_taddr = vm1->vm_taddr;
3005 vm2->vm_daddr = vm1->vm_daddr;
3006 vm2->vm_maxsaddr = vm1->vm_maxsaddr;
3007 new_map = &vm2->vm_map; /* XXX */
3008 locked = vm_map_trylock(new_map); /* trylock to silence WITNESS */
3009 KASSERT(locked, ("vmspace_fork: lock failed"));
3010 new_map->timestamp = 1;
3012 old_entry = old_map->header.next;
3014 while (old_entry != &old_map->header) {
3015 if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
3016 panic("vm_map_fork: encountered a submap");
3018 switch (old_entry->inheritance) {
3019 case VM_INHERIT_NONE:
3022 case VM_INHERIT_SHARE:
3024 * Clone the entry, creating the shared object if necessary.
3026 object = old_entry->object.vm_object;
3027 if (object == NULL) {
3028 object = vm_object_allocate(OBJT_DEFAULT,
3029 atop(old_entry->end - old_entry->start));
3030 old_entry->object.vm_object = object;
3031 old_entry->offset = 0;
3032 if (old_entry->uip != NULL) {
3033 object->uip = old_entry->uip;
3034 object->charge = old_entry->end -
3036 old_entry->uip = NULL;
3041 * Add the reference before calling vm_object_shadow
3042 * to insure that a shadow object is created.
3044 vm_object_reference(object);
3045 if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3046 vm_object_shadow(&old_entry->object.vm_object,
3048 atop(old_entry->end - old_entry->start));
3049 old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
3050 /* Transfer the second reference too. */
3051 vm_object_reference(
3052 old_entry->object.vm_object);
3055 * As in vm_map_simplify_entry(), the
3056 * vnode lock will not be acquired in
3057 * this call to vm_object_deallocate().
3059 vm_object_deallocate(object);
3060 object = old_entry->object.vm_object;
3062 VM_OBJECT_LOCK(object);
3063 vm_object_clear_flag(object, OBJ_ONEMAPPING);
3064 if (old_entry->uip != NULL) {
3065 KASSERT(object->uip == NULL, ("vmspace_fork both uip"));
3066 object->uip = old_entry->uip;
3067 object->charge = old_entry->end - old_entry->start;
3068 old_entry->uip = NULL;
3070 VM_OBJECT_UNLOCK(object);
3073 * Clone the entry, referencing the shared object.
3075 new_entry = vm_map_entry_create(new_map);
3076 *new_entry = *old_entry;
3077 new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
3078 MAP_ENTRY_IN_TRANSITION);
3079 new_entry->wired_count = 0;
3082 * Insert the entry into the new map -- we know we're
3083 * inserting at the end of the new map.
3085 vm_map_entry_link(new_map, new_map->header.prev,
3087 vmspace_map_entry_forked(vm1, vm2, new_entry);
3090 * Update the physical map
3092 pmap_copy(new_map->pmap, old_map->pmap,
3094 (old_entry->end - old_entry->start),
3098 case VM_INHERIT_COPY:
3100 * Clone the entry and link into the map.
3102 new_entry = vm_map_entry_create(new_map);
3103 *new_entry = *old_entry;
3104 new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
3105 MAP_ENTRY_IN_TRANSITION);
3106 new_entry->wired_count = 0;
3107 new_entry->object.vm_object = NULL;
3108 new_entry->uip = NULL;
3109 vm_map_entry_link(new_map, new_map->header.prev,
3111 vmspace_map_entry_forked(vm1, vm2, new_entry);
3112 vm_map_copy_entry(old_map, new_map, old_entry,
3113 new_entry, fork_charge);
3116 old_entry = old_entry->next;
3119 vm_map_unlock(old_map);
3121 vm_map_unlock(new_map);
3127 vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
3128 vm_prot_t prot, vm_prot_t max, int cow)
3130 vm_map_entry_t new_entry, prev_entry;
3131 vm_offset_t bot, top;
3132 vm_size_t init_ssize;
3137 * The stack orientation is piggybacked with the cow argument.
3138 * Extract it into orient and mask the cow argument so that we
3139 * don't pass it around further.
3140 * NOTE: We explicitly allow bi-directional stacks.
3142 orient = cow & (MAP_STACK_GROWS_DOWN|MAP_STACK_GROWS_UP);
3144 KASSERT(orient != 0, ("No stack grow direction"));
3146 if (addrbos < vm_map_min(map) ||
3147 addrbos > vm_map_max(map) ||
3148 addrbos + max_ssize < addrbos)
3149 return (KERN_NO_SPACE);
3151 init_ssize = (max_ssize < sgrowsiz) ? max_ssize : sgrowsiz;
3153 PROC_LOCK(curthread->td_proc);
3154 vmemlim = lim_cur(curthread->td_proc, RLIMIT_VMEM);
3155 PROC_UNLOCK(curthread->td_proc);
3159 /* If addr is already mapped, no go */
3160 if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
3162 return (KERN_NO_SPACE);
3165 /* If we would blow our VMEM resource limit, no go */
3166 if (map->size + init_ssize > vmemlim) {
3168 return (KERN_NO_SPACE);
3172 * If we can't accomodate max_ssize in the current mapping, no go.
3173 * However, we need to be aware that subsequent user mappings might
3174 * map into the space we have reserved for stack, and currently this
3175 * space is not protected.
3177 * Hopefully we will at least detect this condition when we try to
3180 if ((prev_entry->next != &map->header) &&
3181 (prev_entry->next->start < addrbos + max_ssize)) {
3183 return (KERN_NO_SPACE);
3187 * We initially map a stack of only init_ssize. We will grow as
3188 * needed later. Depending on the orientation of the stack (i.e.
3189 * the grow direction) we either map at the top of the range, the
3190 * bottom of the range or in the middle.
3192 * Note: we would normally expect prot and max to be VM_PROT_ALL,
3193 * and cow to be 0. Possibly we should eliminate these as input
3194 * parameters, and just pass these values here in the insert call.
3196 if (orient == MAP_STACK_GROWS_DOWN)
3197 bot = addrbos + max_ssize - init_ssize;
3198 else if (orient == MAP_STACK_GROWS_UP)
3201 bot = round_page(addrbos + max_ssize/2 - init_ssize/2);
3202 top = bot + init_ssize;
3203 rv = vm_map_insert(map, NULL, 0, bot, top, prot, max, cow);
3205 /* Now set the avail_ssize amount. */
3206 if (rv == KERN_SUCCESS) {
3207 if (prev_entry != &map->header)
3208 vm_map_clip_end(map, prev_entry, bot);
3209 new_entry = prev_entry->next;
3210 if (new_entry->end != top || new_entry->start != bot)
3211 panic("Bad entry start/end for new stack entry");
3213 new_entry->avail_ssize = max_ssize - init_ssize;
3214 if (orient & MAP_STACK_GROWS_DOWN)
3215 new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
3216 if (orient & MAP_STACK_GROWS_UP)
3217 new_entry->eflags |= MAP_ENTRY_GROWS_UP;
3224 /* Attempts to grow a vm stack entry. Returns KERN_SUCCESS if the
3225 * desired address is already mapped, or if we successfully grow
3226 * the stack. Also returns KERN_SUCCESS if addr is outside the
3227 * stack range (this is strange, but preserves compatibility with
3228 * the grow function in vm_machdep.c).
3231 vm_map_growstack(struct proc *p, vm_offset_t addr)
3233 vm_map_entry_t next_entry, prev_entry;
3234 vm_map_entry_t new_entry, stack_entry;
3235 struct vmspace *vm = p->p_vmspace;
3236 vm_map_t map = &vm->vm_map;
3238 size_t grow_amount, max_grow;
3239 rlim_t stacklim, vmemlim;
3240 int is_procstack, rv;
3241 struct uidinfo *uip;
3245 stacklim = lim_cur(p, RLIMIT_STACK);
3246 vmemlim = lim_cur(p, RLIMIT_VMEM);
3249 vm_map_lock_read(map);
3251 /* If addr is already in the entry range, no need to grow.*/
3252 if (vm_map_lookup_entry(map, addr, &prev_entry)) {
3253 vm_map_unlock_read(map);
3254 return (KERN_SUCCESS);
3257 next_entry = prev_entry->next;
3258 if (!(prev_entry->eflags & MAP_ENTRY_GROWS_UP)) {
3260 * This entry does not grow upwards. Since the address lies
3261 * beyond this entry, the next entry (if one exists) has to
3262 * be a downward growable entry. The entry list header is
3263 * never a growable entry, so it suffices to check the flags.
3265 if (!(next_entry->eflags & MAP_ENTRY_GROWS_DOWN)) {
3266 vm_map_unlock_read(map);
3267 return (KERN_SUCCESS);
3269 stack_entry = next_entry;
3272 * This entry grows upward. If the next entry does not at
3273 * least grow downwards, this is the entry we need to grow.
3274 * otherwise we have two possible choices and we have to
3277 if (next_entry->eflags & MAP_ENTRY_GROWS_DOWN) {
3279 * We have two choices; grow the entry closest to
3280 * the address to minimize the amount of growth.
3282 if (addr - prev_entry->end <= next_entry->start - addr)
3283 stack_entry = prev_entry;
3285 stack_entry = next_entry;
3287 stack_entry = prev_entry;
3290 if (stack_entry == next_entry) {
3291 KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_DOWN, ("foo"));
3292 KASSERT(addr < stack_entry->start, ("foo"));
3293 end = (prev_entry != &map->header) ? prev_entry->end :
3294 stack_entry->start - stack_entry->avail_ssize;
3295 grow_amount = roundup(stack_entry->start - addr, PAGE_SIZE);
3296 max_grow = stack_entry->start - end;
3298 KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_UP, ("foo"));
3299 KASSERT(addr >= stack_entry->end, ("foo"));
3300 end = (next_entry != &map->header) ? next_entry->start :
3301 stack_entry->end + stack_entry->avail_ssize;
3302 grow_amount = roundup(addr + 1 - stack_entry->end, PAGE_SIZE);
3303 max_grow = end - stack_entry->end;
3306 if (grow_amount > stack_entry->avail_ssize) {
3307 vm_map_unlock_read(map);
3308 return (KERN_NO_SPACE);
3312 * If there is no longer enough space between the entries nogo, and
3313 * adjust the available space. Note: this should only happen if the
3314 * user has mapped into the stack area after the stack was created,
3315 * and is probably an error.
3317 * This also effectively destroys any guard page the user might have
3318 * intended by limiting the stack size.
3320 if (grow_amount > max_grow) {
3321 if (vm_map_lock_upgrade(map))
3324 stack_entry->avail_ssize = max_grow;
3327 return (KERN_NO_SPACE);
3330 is_procstack = (addr >= (vm_offset_t)vm->vm_maxsaddr) ? 1 : 0;
3333 * If this is the main process stack, see if we're over the stack
3336 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
3337 vm_map_unlock_read(map);
3338 return (KERN_NO_SPACE);
3341 /* Round up the grow amount modulo SGROWSIZ */
3342 grow_amount = roundup (grow_amount, sgrowsiz);
3343 if (grow_amount > stack_entry->avail_ssize)
3344 grow_amount = stack_entry->avail_ssize;
3345 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
3346 grow_amount = trunc_page((vm_size_t)stacklim) -
3350 /* If we would blow our VMEM resource limit, no go */
3351 if (map->size + grow_amount > vmemlim) {
3352 vm_map_unlock_read(map);
3353 return (KERN_NO_SPACE);
3356 if (vm_map_lock_upgrade(map))
3359 if (stack_entry == next_entry) {
3363 /* Get the preliminary new entry start value */
3364 addr = stack_entry->start - grow_amount;
3367 * If this puts us into the previous entry, cut back our
3368 * growth to the available space. Also, see the note above.
3371 stack_entry->avail_ssize = max_grow;
3375 rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start,
3376 p->p_sysent->sv_stackprot, VM_PROT_ALL, 0);
3378 /* Adjust the available stack space by the amount we grew. */
3379 if (rv == KERN_SUCCESS) {
3380 if (prev_entry != &map->header)
3381 vm_map_clip_end(map, prev_entry, addr);
3382 new_entry = prev_entry->next;
3383 KASSERT(new_entry == stack_entry->prev, ("foo"));
3384 KASSERT(new_entry->end == stack_entry->start, ("foo"));
3385 KASSERT(new_entry->start == addr, ("foo"));
3386 grow_amount = new_entry->end - new_entry->start;
3387 new_entry->avail_ssize = stack_entry->avail_ssize -
3389 stack_entry->eflags &= ~MAP_ENTRY_GROWS_DOWN;
3390 new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
3396 addr = stack_entry->end + grow_amount;
3399 * If this puts us into the next entry, cut back our growth
3400 * to the available space. Also, see the note above.
3403 stack_entry->avail_ssize = end - stack_entry->end;
3407 grow_amount = addr - stack_entry->end;
3408 uip = stack_entry->uip;
3409 if (uip == NULL && stack_entry->object.vm_object != NULL)
3410 uip = stack_entry->object.vm_object->uip;
3411 if (uip != NULL && !swap_reserve_by_uid(grow_amount, uip))
3413 /* Grow the underlying object if applicable. */
3414 else if (stack_entry->object.vm_object == NULL ||
3415 vm_object_coalesce(stack_entry->object.vm_object,
3416 stack_entry->offset,
3417 (vm_size_t)(stack_entry->end - stack_entry->start),
3418 (vm_size_t)grow_amount, uip != NULL)) {
3419 map->size += (addr - stack_entry->end);
3420 /* Update the current entry. */
3421 stack_entry->end = addr;
3422 stack_entry->avail_ssize -= grow_amount;
3423 vm_map_entry_resize_free(map, stack_entry);
3426 if (next_entry != &map->header)
3427 vm_map_clip_start(map, next_entry, addr);
3432 if (rv == KERN_SUCCESS && is_procstack)
3433 vm->vm_ssize += btoc(grow_amount);
3438 * Heed the MAP_WIREFUTURE flag if it was set for this process.
3440 if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE)) {
3442 (stack_entry == next_entry) ? addr : addr - grow_amount,
3443 (stack_entry == next_entry) ? stack_entry->start : addr,
3444 (p->p_flag & P_SYSTEM)
3445 ? VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES
3446 : VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
3453 * Unshare the specified VM space for exec. If other processes are
3454 * mapped to it, then create a new one. The new vmspace is null.
3457 vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser)
3459 struct vmspace *oldvmspace = p->p_vmspace;
3460 struct vmspace *newvmspace;
3462 newvmspace = vmspace_alloc(minuser, maxuser);
3463 if (newvmspace == NULL)
3465 newvmspace->vm_swrss = oldvmspace->vm_swrss;
3467 * This code is written like this for prototype purposes. The
3468 * goal is to avoid running down the vmspace here, but let the
3469 * other process's that are still using the vmspace to finally
3470 * run it down. Even though there is little or no chance of blocking
3471 * here, it is a good idea to keep this form for future mods.
3473 PROC_VMSPACE_LOCK(p);
3474 p->p_vmspace = newvmspace;
3475 PROC_VMSPACE_UNLOCK(p);
3476 if (p == curthread->td_proc)
3477 pmap_activate(curthread);
3478 vmspace_free(oldvmspace);
3483 * Unshare the specified VM space for forcing COW. This
3484 * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
3487 vmspace_unshare(struct proc *p)
3489 struct vmspace *oldvmspace = p->p_vmspace;
3490 struct vmspace *newvmspace;
3491 vm_ooffset_t fork_charge;
3493 if (oldvmspace->vm_refcnt == 1)
3496 newvmspace = vmspace_fork(oldvmspace, &fork_charge);
3497 if (newvmspace == NULL)
3499 if (!swap_reserve_by_uid(fork_charge, p->p_ucred->cr_ruidinfo)) {
3500 vmspace_free(newvmspace);
3503 PROC_VMSPACE_LOCK(p);
3504 p->p_vmspace = newvmspace;
3505 PROC_VMSPACE_UNLOCK(p);
3506 if (p == curthread->td_proc)
3507 pmap_activate(curthread);
3508 vmspace_free(oldvmspace);
3515 * Finds the VM object, offset, and
3516 * protection for a given virtual address in the
3517 * specified map, assuming a page fault of the
3520 * Leaves the map in question locked for read; return
3521 * values are guaranteed until a vm_map_lookup_done
3522 * call is performed. Note that the map argument
3523 * is in/out; the returned map must be used in
3524 * the call to vm_map_lookup_done.
3526 * A handle (out_entry) is returned for use in
3527 * vm_map_lookup_done, to make that fast.
3529 * If a lookup is requested with "write protection"
3530 * specified, the map may be changed to perform virtual
3531 * copying operations, although the data referenced will
3535 vm_map_lookup(vm_map_t *var_map, /* IN/OUT */
3537 vm_prot_t fault_typea,
3538 vm_map_entry_t *out_entry, /* OUT */
3539 vm_object_t *object, /* OUT */
3540 vm_pindex_t *pindex, /* OUT */
3541 vm_prot_t *out_prot, /* OUT */
3542 boolean_t *wired) /* OUT */
3544 vm_map_entry_t entry;
3545 vm_map_t map = *var_map;
3547 vm_prot_t fault_type = fault_typea;
3548 vm_object_t eobject;
3549 struct uidinfo *uip;
3554 vm_map_lock_read(map);
3557 * Lookup the faulting address.
3559 if (!vm_map_lookup_entry(map, vaddr, out_entry)) {
3560 vm_map_unlock_read(map);
3561 return (KERN_INVALID_ADDRESS);
3569 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3570 vm_map_t old_map = map;
3572 *var_map = map = entry->object.sub_map;
3573 vm_map_unlock_read(old_map);
3578 * Check whether this task is allowed to have this page.
3579 * Note the special case for MAP_ENTRY_COW
3580 * pages with an override. This is to implement a forced
3581 * COW for debuggers.
3583 if (fault_type & VM_PROT_OVERRIDE_WRITE)
3584 prot = entry->max_protection;
3586 prot = entry->protection;
3587 fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
3588 if ((fault_type & prot) != fault_type) {
3589 vm_map_unlock_read(map);
3590 return (KERN_PROTECTION_FAILURE);
3592 if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
3593 (entry->eflags & MAP_ENTRY_COW) &&
3594 (fault_type & VM_PROT_WRITE) &&
3595 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
3596 vm_map_unlock_read(map);
3597 return (KERN_PROTECTION_FAILURE);
3601 * If this page is not pageable, we have to get it for all possible
3604 *wired = (entry->wired_count != 0);
3606 prot = fault_type = entry->protection;
3607 size = entry->end - entry->start;
3609 * If the entry was copy-on-write, we either ...
3611 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3613 * If we want to write the page, we may as well handle that
3614 * now since we've got the map locked.
3616 * If we don't need to write the page, we just demote the
3617 * permissions allowed.
3619 if (fault_type & VM_PROT_WRITE) {
3621 * Make a new object, and place it in the object
3622 * chain. Note that no new references have appeared
3623 * -- one just moved from the map to the new
3626 if (vm_map_lock_upgrade(map))
3629 if (entry->uip == NULL) {
3631 * The debugger owner is charged for
3634 uip = curthread->td_ucred->cr_ruidinfo;
3636 if (!swap_reserve_by_uid(size, uip)) {
3639 return (KERN_RESOURCE_SHORTAGE);
3644 &entry->object.vm_object,
3647 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
3648 eobject = entry->object.vm_object;
3649 if (eobject->uip != NULL) {
3651 * The object was not shadowed.
3653 swap_release_by_uid(size, entry->uip);
3656 } else if (entry->uip != NULL) {
3657 VM_OBJECT_LOCK(eobject);
3658 eobject->uip = entry->uip;
3659 eobject->charge = size;
3660 VM_OBJECT_UNLOCK(eobject);
3664 vm_map_lock_downgrade(map);
3667 * We're attempting to read a copy-on-write page --
3668 * don't allow writes.
3670 prot &= ~VM_PROT_WRITE;
3675 * Create an object if necessary.
3677 if (entry->object.vm_object == NULL &&
3679 if (vm_map_lock_upgrade(map))
3681 entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
3684 if (entry->uip != NULL) {
3685 VM_OBJECT_LOCK(entry->object.vm_object);
3686 entry->object.vm_object->uip = entry->uip;
3687 entry->object.vm_object->charge = size;
3688 VM_OBJECT_UNLOCK(entry->object.vm_object);
3691 vm_map_lock_downgrade(map);
3695 * Return the object/offset from this entry. If the entry was
3696 * copy-on-write or empty, it has been fixed up.
3698 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
3699 *object = entry->object.vm_object;
3702 return (KERN_SUCCESS);
3706 * vm_map_lookup_locked:
3708 * Lookup the faulting address. A version of vm_map_lookup that returns
3709 * KERN_FAILURE instead of blocking on map lock or memory allocation.
3712 vm_map_lookup_locked(vm_map_t *var_map, /* IN/OUT */
3714 vm_prot_t fault_typea,
3715 vm_map_entry_t *out_entry, /* OUT */
3716 vm_object_t *object, /* OUT */
3717 vm_pindex_t *pindex, /* OUT */
3718 vm_prot_t *out_prot, /* OUT */
3719 boolean_t *wired) /* OUT */
3721 vm_map_entry_t entry;
3722 vm_map_t map = *var_map;
3724 vm_prot_t fault_type = fault_typea;
3727 * Lookup the faulting address.
3729 if (!vm_map_lookup_entry(map, vaddr, out_entry))
3730 return (KERN_INVALID_ADDRESS);
3735 * Fail if the entry refers to a submap.
3737 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
3738 return (KERN_FAILURE);
3741 * Check whether this task is allowed to have this page.
3742 * Note the special case for MAP_ENTRY_COW
3743 * pages with an override. This is to implement a forced
3744 * COW for debuggers.
3746 if (fault_type & VM_PROT_OVERRIDE_WRITE)
3747 prot = entry->max_protection;
3749 prot = entry->protection;
3750 fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
3751 if ((fault_type & prot) != fault_type)
3752 return (KERN_PROTECTION_FAILURE);
3753 if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
3754 (entry->eflags & MAP_ENTRY_COW) &&
3755 (fault_type & VM_PROT_WRITE) &&
3756 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0)
3757 return (KERN_PROTECTION_FAILURE);
3760 * If this page is not pageable, we have to get it for all possible
3763 *wired = (entry->wired_count != 0);
3765 prot = fault_type = entry->protection;
3767 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3769 * Fail if the entry was copy-on-write for a write fault.
3771 if (fault_type & VM_PROT_WRITE)
3772 return (KERN_FAILURE);
3774 * We're attempting to read a copy-on-write page --
3775 * don't allow writes.
3777 prot &= ~VM_PROT_WRITE;
3781 * Fail if an object should be created.
3783 if (entry->object.vm_object == NULL && !map->system_map)
3784 return (KERN_FAILURE);
3787 * Return the object/offset from this entry. If the entry was
3788 * copy-on-write or empty, it has been fixed up.
3790 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
3791 *object = entry->object.vm_object;
3794 return (KERN_SUCCESS);
3798 * vm_map_lookup_done:
3800 * Releases locks acquired by a vm_map_lookup
3801 * (according to the handle returned by that lookup).
3804 vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry)
3807 * Unlock the main-level map
3809 vm_map_unlock_read(map);
3812 #include "opt_ddb.h"
3814 #include <sys/kernel.h>
3816 #include <ddb/ddb.h>
3819 * vm_map_print: [ debug ]
3821 DB_SHOW_COMMAND(map, vm_map_print)
3824 /* XXX convert args. */
3825 vm_map_t map = (vm_map_t)addr;
3826 boolean_t full = have_addr;
3828 vm_map_entry_t entry;
3830 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
3832 (void *)map->pmap, map->nentries, map->timestamp);
3835 if (!full && db_indent)
3839 for (entry = map->header.next; entry != &map->header;
3840 entry = entry->next) {
3841 db_iprintf("map entry %p: start=%p, end=%p\n",
3842 (void *)entry, (void *)entry->start, (void *)entry->end);
3845 static char *inheritance_name[4] =
3846 {"share", "copy", "none", "donate_copy"};
3848 db_iprintf(" prot=%x/%x/%s",
3850 entry->max_protection,
3851 inheritance_name[(int)(unsigned char)entry->inheritance]);
3852 if (entry->wired_count != 0)
3853 db_printf(", wired");
3855 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3856 db_printf(", share=%p, offset=0x%jx\n",
3857 (void *)entry->object.sub_map,
3858 (uintmax_t)entry->offset);
3860 if ((entry->prev == &map->header) ||
3861 (entry->prev->object.sub_map !=
3862 entry->object.sub_map)) {
3864 vm_map_print((db_expr_t)(intptr_t)
3865 entry->object.sub_map,
3866 full, 0, (char *)0);
3870 if (entry->uip != NULL)
3871 db_printf(", uip %d", entry->uip->ui_uid);
3872 db_printf(", object=%p, offset=0x%jx",
3873 (void *)entry->object.vm_object,
3874 (uintmax_t)entry->offset);
3875 if (entry->object.vm_object && entry->object.vm_object->uip)
3876 db_printf(", obj uip %d charge %jx",
3877 entry->object.vm_object->uip->ui_uid,
3878 (uintmax_t)entry->object.vm_object->charge);
3879 if (entry->eflags & MAP_ENTRY_COW)
3880 db_printf(", copy (%s)",
3881 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
3885 if ((entry->prev == &map->header) ||
3886 (entry->prev->object.vm_object !=
3887 entry->object.vm_object)) {
3889 vm_object_print((db_expr_t)(intptr_t)
3890 entry->object.vm_object,
3891 full, 0, (char *)0);
3903 DB_SHOW_COMMAND(procvm, procvm)
3908 p = (struct proc *) addr;
3913 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
3914 (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
3915 (void *)vmspace_pmap(p->p_vmspace));
3917 vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);