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_dispose(vm_map_t map, vm_map_entry_t entry);
132 static void vm_map_zdtor(void *mem, int size, void *arg);
133 static void vmspace_zdtor(void *mem, int size, void *arg);
136 #define ENTRY_CHARGED(e) ((e)->uip != NULL || \
137 ((e)->object.vm_object != NULL && (e)->object.vm_object->uip != NULL && \
138 !((e)->eflags & MAP_ENTRY_NEEDS_COPY)))
141 * PROC_VMSPACE_{UN,}LOCK() can be a noop as long as vmspaces are type
144 #define PROC_VMSPACE_LOCK(p) do { } while (0)
145 #define PROC_VMSPACE_UNLOCK(p) do { } while (0)
148 * VM_MAP_RANGE_CHECK: [ internal use only ]
150 * Asserts that the starting and ending region
151 * addresses fall within the valid range of the map.
153 #define VM_MAP_RANGE_CHECK(map, start, end) \
155 if (start < vm_map_min(map)) \
156 start = vm_map_min(map); \
157 if (end > vm_map_max(map)) \
158 end = vm_map_max(map); \
166 * Initialize the vm_map module. Must be called before
167 * any other vm_map routines.
169 * Map and entry structures are allocated from the general
170 * purpose memory pool with some exceptions:
172 * - The kernel map and kmem submap are allocated statically.
173 * - Kernel map entries are allocated out of a static pool.
175 * These restrictions are necessary since malloc() uses the
176 * maps and requires map entries.
182 mtx_init(&map_sleep_mtx, "vm map sleep mutex", NULL, MTX_DEF);
183 mapzone = uma_zcreate("MAP", sizeof(struct vm_map), NULL,
189 vm_map_zinit, vm_map_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
190 uma_prealloc(mapzone, MAX_KMAP);
191 kmapentzone = uma_zcreate("KMAP ENTRY", sizeof(struct vm_map_entry),
192 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
193 UMA_ZONE_MTXCLASS | UMA_ZONE_VM);
194 uma_prealloc(kmapentzone, MAX_KMAPENT);
195 mapentzone = uma_zcreate("MAP ENTRY", sizeof(struct vm_map_entry),
196 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
200 vmspace_zfini(void *mem, int size)
204 vm = (struct vmspace *)mem;
205 vm_map_zfini(&vm->vm_map, sizeof(vm->vm_map));
209 vmspace_zinit(void *mem, int size, int flags)
213 vm = (struct vmspace *)mem;
215 vm->vm_map.pmap = NULL;
216 (void)vm_map_zinit(&vm->vm_map, sizeof(vm->vm_map), flags);
221 vm_map_zfini(void *mem, int size)
226 mtx_destroy(&map->system_mtx);
227 sx_destroy(&map->lock);
231 vm_map_zinit(void *mem, int size, int flags)
238 mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
239 sx_init(&map->lock, "user map");
245 vmspace_zdtor(void *mem, int size, void *arg)
249 vm = (struct vmspace *)mem;
251 vm_map_zdtor(&vm->vm_map, sizeof(vm->vm_map), arg);
254 vm_map_zdtor(void *mem, int size, void *arg)
259 KASSERT(map->nentries == 0,
260 ("map %p nentries == %d on free.",
261 map, map->nentries));
262 KASSERT(map->size == 0,
263 ("map %p size == %lu on free.",
264 map, (unsigned long)map->size));
266 #endif /* INVARIANTS */
269 * Allocate a vmspace structure, including a vm_map and pmap,
270 * and initialize those structures. The refcnt is set to 1.
273 vmspace_alloc(min, max)
274 vm_offset_t min, max;
278 vm = uma_zalloc(vmspace_zone, M_WAITOK);
279 if (vm->vm_map.pmap == NULL && !pmap_pinit(vmspace_pmap(vm))) {
280 uma_zfree(vmspace_zone, vm);
283 CTR1(KTR_VM, "vmspace_alloc: %p", vm);
284 _vm_map_init(&vm->vm_map, min, max);
285 vm->vm_map.pmap = vmspace_pmap(vm); /* XXX */
301 uma_zone_set_obj(kmapentzone, &kmapentobj, lmin(cnt.v_page_count,
302 (VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS) / PAGE_SIZE) / 8 +
303 maxproc * 2 + maxfiles);
304 vmspace_zone = uma_zcreate("VMSPACE", sizeof(struct vmspace), NULL,
310 vmspace_zinit, vmspace_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
314 vmspace_dofree(struct vmspace *vm)
316 CTR1(KTR_VM, "vmspace_free: %p", vm);
319 * Make sure any SysV shm is freed, it might not have been in
325 * Lock the map, to wait out all other references to it.
326 * Delete all of the mappings and pages they hold, then call
327 * the pmap module to reclaim anything left.
329 (void)vm_map_remove(&vm->vm_map, vm->vm_map.min_offset,
330 vm->vm_map.max_offset);
333 * XXX Comment out the pmap_release call for now. The
334 * vmspace_zone is marked as UMA_ZONE_NOFREE, and bugs cause
335 * pmap.resident_count to be != 0 on exit sometimes.
337 /* pmap_release(vmspace_pmap(vm)); */
338 uma_zfree(vmspace_zone, vm);
342 vmspace_free(struct vmspace *vm)
346 if (vm->vm_refcnt == 0)
347 panic("vmspace_free: attempt to free already freed vmspace");
350 refcnt = vm->vm_refcnt;
351 while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1));
357 vmspace_exitfree(struct proc *p)
361 PROC_VMSPACE_LOCK(p);
364 PROC_VMSPACE_UNLOCK(p);
365 KASSERT(vm == &vmspace0, ("vmspace_exitfree: wrong vmspace"));
370 vmspace_exit(struct thread *td)
377 * Release user portion of address space.
378 * This releases references to vnodes,
379 * which could cause I/O if the file has been unlinked.
380 * Need to do this early enough that we can still sleep.
382 * The last exiting process to reach this point releases as
383 * much of the environment as it can. vmspace_dofree() is the
384 * slower fallback in case another process had a temporary
385 * reference to the vmspace.
390 atomic_add_int(&vmspace0.vm_refcnt, 1);
392 refcnt = vm->vm_refcnt;
393 if (refcnt > 1 && p->p_vmspace != &vmspace0) {
394 /* Switch now since other proc might free vmspace */
395 PROC_VMSPACE_LOCK(p);
396 p->p_vmspace = &vmspace0;
397 PROC_VMSPACE_UNLOCK(p);
400 } while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1));
402 if (p->p_vmspace != vm) {
403 /* vmspace not yet freed, switch back */
404 PROC_VMSPACE_LOCK(p);
406 PROC_VMSPACE_UNLOCK(p);
409 pmap_remove_pages(vmspace_pmap(vm));
410 /* Switch now since this proc will free vmspace */
411 PROC_VMSPACE_LOCK(p);
412 p->p_vmspace = &vmspace0;
413 PROC_VMSPACE_UNLOCK(p);
419 /* Acquire reference to vmspace owned by another process. */
422 vmspace_acquire_ref(struct proc *p)
427 PROC_VMSPACE_LOCK(p);
430 PROC_VMSPACE_UNLOCK(p);
434 refcnt = vm->vm_refcnt;
435 if (refcnt <= 0) { /* Avoid 0->1 transition */
436 PROC_VMSPACE_UNLOCK(p);
439 } while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt + 1));
440 if (vm != p->p_vmspace) {
441 PROC_VMSPACE_UNLOCK(p);
445 PROC_VMSPACE_UNLOCK(p);
450 _vm_map_lock(vm_map_t map, const char *file, int line)
454 _mtx_lock_flags(&map->system_mtx, 0, file, line);
456 (void)_sx_xlock(&map->lock, 0, file, line);
461 _vm_map_unlock(vm_map_t map, const char *file, int line)
463 vm_map_entry_t free_entry, entry;
466 free_entry = map->deferred_freelist;
467 map->deferred_freelist = NULL;
470 _mtx_unlock_flags(&map->system_mtx, 0, file, line);
472 _sx_xunlock(&map->lock, file, line);
474 while (free_entry != NULL) {
476 free_entry = free_entry->next;
478 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
479 object = entry->object.vm_object;
480 vm_object_deallocate(object);
483 vm_map_entry_dispose(map, entry);
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);
508 _vm_map_trylock(vm_map_t map, const char *file, int line)
512 error = map->system_map ?
513 !_mtx_trylock(&map->system_mtx, 0, file, line) :
514 !_sx_try_xlock(&map->lock, file, line);
521 _vm_map_trylock_read(vm_map_t map, const char *file, int line)
525 error = map->system_map ?
526 !_mtx_trylock(&map->system_mtx, 0, file, line) :
527 !_sx_try_slock(&map->lock, file, line);
532 * _vm_map_lock_upgrade: [ internal use only ]
534 * Tries to upgrade a read (shared) lock on the specified map to a write
535 * (exclusive) lock. Returns the value "0" if the upgrade succeeds and a
536 * non-zero value if the upgrade fails. If the upgrade fails, the map is
537 * returned without a read or write lock held.
539 * Requires that the map be read locked.
542 _vm_map_lock_upgrade(vm_map_t map, const char *file, int line)
544 unsigned int last_timestamp;
546 if (map->system_map) {
548 _mtx_assert(&map->system_mtx, MA_OWNED, file, line);
551 if (!_sx_try_upgrade(&map->lock, file, line)) {
552 last_timestamp = map->timestamp;
553 _sx_sunlock(&map->lock, file, line);
555 * If the map's timestamp does not change while the
556 * map is unlocked, then the upgrade succeeds.
558 (void)_sx_xlock(&map->lock, 0, file, line);
559 if (last_timestamp != map->timestamp) {
560 _sx_xunlock(&map->lock, file, line);
570 _vm_map_lock_downgrade(vm_map_t map, const char *file, int line)
573 if (map->system_map) {
575 _mtx_assert(&map->system_mtx, MA_OWNED, file, line);
578 _sx_downgrade(&map->lock, file, line);
584 * Returns a non-zero value if the caller holds a write (exclusive) lock
585 * on the specified map and the value "0" otherwise.
588 vm_map_locked(vm_map_t map)
592 return (mtx_owned(&map->system_mtx));
594 return (sx_xlocked(&map->lock));
599 _vm_map_assert_locked(vm_map_t map, const char *file, int line)
603 _mtx_assert(&map->system_mtx, MA_OWNED, file, line);
605 _sx_assert(&map->lock, SA_XLOCKED, file, line);
610 _vm_map_assert_locked_read(vm_map_t map, const char *file, int line)
614 _mtx_assert(&map->system_mtx, MA_OWNED, file, line);
616 _sx_assert(&map->lock, SA_SLOCKED, file, line);
620 #define VM_MAP_ASSERT_LOCKED(map) \
621 _vm_map_assert_locked(map, LOCK_FILE, LOCK_LINE)
622 #define VM_MAP_ASSERT_LOCKED_READ(map) \
623 _vm_map_assert_locked_read(map, LOCK_FILE, LOCK_LINE)
625 #define VM_MAP_ASSERT_LOCKED(map)
626 #define VM_MAP_ASSERT_LOCKED_READ(map)
630 * vm_map_unlock_and_wait:
633 vm_map_unlock_and_wait(vm_map_t map, int timo)
636 mtx_lock(&map_sleep_mtx);
638 return (msleep(&map->root, &map_sleep_mtx, PDROP | PVM, "vmmaps", timo));
645 vm_map_wakeup(vm_map_t map)
649 * Acquire and release map_sleep_mtx to prevent a wakeup()
650 * from being performed (and lost) between the vm_map_unlock()
651 * and the msleep() in vm_map_unlock_and_wait().
653 mtx_lock(&map_sleep_mtx);
654 mtx_unlock(&map_sleep_mtx);
659 vmspace_resident_count(struct vmspace *vmspace)
661 return pmap_resident_count(vmspace_pmap(vmspace));
665 vmspace_wired_count(struct vmspace *vmspace)
667 return pmap_wired_count(vmspace_pmap(vmspace));
673 * Creates and returns a new empty VM map with
674 * the given physical map structure, and having
675 * the given lower and upper address bounds.
678 vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max)
682 result = uma_zalloc(mapzone, M_WAITOK);
683 CTR1(KTR_VM, "vm_map_create: %p", result);
684 _vm_map_init(result, min, max);
690 * Initialize an existing vm_map structure
691 * such as that in the vmspace structure.
692 * The pmap is set elsewhere.
695 _vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max)
698 map->header.next = map->header.prev = &map->header;
699 map->needs_wakeup = FALSE;
701 map->min_offset = min;
702 map->max_offset = max;
706 map->deferred_freelist = NULL;
710 vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max)
712 _vm_map_init(map, min, max);
713 mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
714 sx_init(&map->lock, "user map");
718 * vm_map_entry_dispose: [ internal use only ]
720 * Inverse of vm_map_entry_create.
723 vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry)
725 uma_zfree(map->system_map ? kmapentzone : mapentzone, entry);
729 * vm_map_entry_create: [ internal use only ]
731 * Allocates a VM map entry for insertion.
732 * No entry fields are filled in.
734 static vm_map_entry_t
735 vm_map_entry_create(vm_map_t map)
737 vm_map_entry_t new_entry;
740 new_entry = uma_zalloc(kmapentzone, M_NOWAIT);
742 new_entry = uma_zalloc(mapentzone, M_WAITOK);
743 if (new_entry == NULL)
744 panic("vm_map_entry_create: kernel resources exhausted");
749 * vm_map_entry_set_behavior:
751 * Set the expected access behavior, either normal, random, or
755 vm_map_entry_set_behavior(vm_map_entry_t entry, u_char behavior)
757 entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) |
758 (behavior & MAP_ENTRY_BEHAV_MASK);
762 * vm_map_entry_set_max_free:
764 * Set the max_free field in a vm_map_entry.
767 vm_map_entry_set_max_free(vm_map_entry_t entry)
770 entry->max_free = entry->adj_free;
771 if (entry->left != NULL && entry->left->max_free > entry->max_free)
772 entry->max_free = entry->left->max_free;
773 if (entry->right != NULL && entry->right->max_free > entry->max_free)
774 entry->max_free = entry->right->max_free;
778 * vm_map_entry_splay:
780 * The Sleator and Tarjan top-down splay algorithm with the
781 * following variation. Max_free must be computed bottom-up, so
782 * on the downward pass, maintain the left and right spines in
783 * reverse order. Then, make a second pass up each side to fix
784 * the pointers and compute max_free. The time bound is O(log n)
787 * The new root is the vm_map_entry containing "addr", or else an
788 * adjacent entry (lower or higher) if addr is not in the tree.
790 * The map must be locked, and leaves it so.
792 * Returns: the new root.
794 static vm_map_entry_t
795 vm_map_entry_splay(vm_offset_t addr, vm_map_entry_t root)
797 vm_map_entry_t llist, rlist;
798 vm_map_entry_t ltree, rtree;
801 /* Special case of empty tree. */
806 * Pass One: Splay down the tree until we find addr or a NULL
807 * pointer where addr would go. llist and rlist are the two
808 * sides in reverse order (bottom-up), with llist linked by
809 * the right pointer and rlist linked by the left pointer in
810 * the vm_map_entry. Wait until Pass Two to set max_free on
816 /* root is never NULL in here. */
817 if (addr < root->start) {
821 if (addr < y->start && y->left != NULL) {
822 /* Rotate right and put y on rlist. */
823 root->left = y->right;
825 vm_map_entry_set_max_free(root);
830 /* Put root on rlist. */
835 } else if (addr >= root->end) {
839 if (addr >= y->end && y->right != NULL) {
840 /* Rotate left and put y on llist. */
841 root->right = y->left;
843 vm_map_entry_set_max_free(root);
848 /* Put root on llist. */
858 * Pass Two: Walk back up the two spines, flip the pointers
859 * and set max_free. The subtrees of the root go at the
860 * bottom of llist and rlist.
863 while (llist != NULL) {
865 llist->right = ltree;
866 vm_map_entry_set_max_free(llist);
871 while (rlist != NULL) {
874 vm_map_entry_set_max_free(rlist);
880 * Final assembly: add ltree and rtree as subtrees of root.
884 vm_map_entry_set_max_free(root);
890 * vm_map_entry_{un,}link:
892 * Insert/remove entries from maps.
895 vm_map_entry_link(vm_map_t map,
896 vm_map_entry_t after_where,
897 vm_map_entry_t entry)
901 "vm_map_entry_link: map %p, nentries %d, entry %p, after %p", map,
902 map->nentries, entry, after_where);
903 VM_MAP_ASSERT_LOCKED(map);
905 entry->prev = after_where;
906 entry->next = after_where->next;
907 entry->next->prev = entry;
908 after_where->next = entry;
910 if (after_where != &map->header) {
911 if (after_where != map->root)
912 vm_map_entry_splay(after_where->start, map->root);
913 entry->right = after_where->right;
914 entry->left = after_where;
915 after_where->right = NULL;
916 after_where->adj_free = entry->start - after_where->end;
917 vm_map_entry_set_max_free(after_where);
919 entry->right = map->root;
922 entry->adj_free = (entry->next == &map->header ? map->max_offset :
923 entry->next->start) - entry->end;
924 vm_map_entry_set_max_free(entry);
929 vm_map_entry_unlink(vm_map_t map,
930 vm_map_entry_t entry)
932 vm_map_entry_t next, prev, root;
934 VM_MAP_ASSERT_LOCKED(map);
935 if (entry != map->root)
936 vm_map_entry_splay(entry->start, map->root);
937 if (entry->left == NULL)
940 root = vm_map_entry_splay(entry->start, entry->left);
941 root->right = entry->right;
942 root->adj_free = (entry->next == &map->header ? map->max_offset :
943 entry->next->start) - root->end;
944 vm_map_entry_set_max_free(root);
953 CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map,
954 map->nentries, entry);
958 * vm_map_entry_resize_free:
960 * Recompute the amount of free space following a vm_map_entry
961 * and propagate that value up the tree. Call this function after
962 * resizing a map entry in-place, that is, without a call to
963 * vm_map_entry_link() or _unlink().
965 * The map must be locked, and leaves it so.
968 vm_map_entry_resize_free(vm_map_t map, vm_map_entry_t entry)
972 * Using splay trees without parent pointers, propagating
973 * max_free up the tree is done by moving the entry to the
974 * root and making the change there.
976 if (entry != map->root)
977 map->root = vm_map_entry_splay(entry->start, map->root);
979 entry->adj_free = (entry->next == &map->header ? map->max_offset :
980 entry->next->start) - entry->end;
981 vm_map_entry_set_max_free(entry);
985 * vm_map_lookup_entry: [ internal use only ]
987 * Finds the map entry containing (or
988 * immediately preceding) the specified address
989 * in the given map; the entry is returned
990 * in the "entry" parameter. The boolean
991 * result indicates whether the address is
992 * actually contained in the map.
998 vm_map_entry_t *entry) /* OUT */
1004 * If the map is empty, then the map entry immediately preceding
1005 * "address" is the map's header.
1009 *entry = &map->header;
1010 else if (address >= cur->start && cur->end > address) {
1013 } else if ((locked = vm_map_locked(map)) ||
1014 sx_try_upgrade(&map->lock)) {
1016 * Splay requires a write lock on the map. However, it only
1017 * restructures the binary search tree; it does not otherwise
1018 * change the map. Thus, the map's timestamp need not change
1019 * on a temporary upgrade.
1021 map->root = cur = vm_map_entry_splay(address, cur);
1023 sx_downgrade(&map->lock);
1026 * If "address" is contained within a map entry, the new root
1027 * is that map entry. Otherwise, the new root is a map entry
1028 * immediately before or after "address".
1030 if (address >= cur->start) {
1032 if (cur->end > address)
1038 * Since the map is only locked for read access, perform a
1039 * standard binary search tree lookup for "address".
1042 if (address < cur->start) {
1043 if (cur->left == NULL) {
1048 } else if (cur->end > address) {
1052 if (cur->right == NULL) {
1065 * Inserts the given whole VM object into the target
1066 * map at the specified address range. The object's
1067 * size should match that of the address range.
1069 * Requires that the map be locked, and leaves it so.
1071 * If object is non-NULL, ref count must be bumped by caller
1072 * prior to making call to account for the new entry.
1075 vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1076 vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max,
1079 vm_map_entry_t new_entry;
1080 vm_map_entry_t prev_entry;
1081 vm_map_entry_t temp_entry;
1082 vm_eflags_t protoeflags;
1083 struct uidinfo *uip;
1084 boolean_t charge_prev_obj;
1086 VM_MAP_ASSERT_LOCKED(map);
1089 * Check that the start and end points are not bogus.
1091 if ((start < map->min_offset) || (end > map->max_offset) ||
1093 return (KERN_INVALID_ADDRESS);
1096 * Find the entry prior to the proposed starting address; if it's part
1097 * of an existing entry, this range is bogus.
1099 if (vm_map_lookup_entry(map, start, &temp_entry))
1100 return (KERN_NO_SPACE);
1102 prev_entry = temp_entry;
1105 * Assert that the next entry doesn't overlap the end point.
1107 if ((prev_entry->next != &map->header) &&
1108 (prev_entry->next->start < end))
1109 return (KERN_NO_SPACE);
1112 charge_prev_obj = FALSE;
1114 if (cow & MAP_COPY_ON_WRITE)
1115 protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
1117 if (cow & MAP_NOFAULT) {
1118 protoeflags |= MAP_ENTRY_NOFAULT;
1120 KASSERT(object == NULL,
1121 ("vm_map_insert: paradoxical MAP_NOFAULT request"));
1123 if (cow & MAP_DISABLE_SYNCER)
1124 protoeflags |= MAP_ENTRY_NOSYNC;
1125 if (cow & MAP_DISABLE_COREDUMP)
1126 protoeflags |= MAP_ENTRY_NOCOREDUMP;
1129 KASSERT((object != kmem_object && object != kernel_object) ||
1130 ((object == kmem_object || object == kernel_object) &&
1131 !(protoeflags & MAP_ENTRY_NEEDS_COPY)),
1132 ("kmem or kernel object and cow"));
1133 if (cow & (MAP_ACC_NO_CHARGE | MAP_NOFAULT))
1135 if ((cow & MAP_ACC_CHARGED) || ((prot & VM_PROT_WRITE) &&
1136 ((protoeflags & MAP_ENTRY_NEEDS_COPY) || object == NULL))) {
1137 if (!(cow & MAP_ACC_CHARGED) && !swap_reserve(end - start))
1138 return (KERN_RESOURCE_SHORTAGE);
1139 KASSERT(object == NULL || (protoeflags & MAP_ENTRY_NEEDS_COPY) ||
1140 object->uip == NULL,
1141 ("OVERCOMMIT: vm_map_insert o %p", object));
1142 uip = curthread->td_ucred->cr_ruidinfo;
1144 if (object == NULL && !(protoeflags & MAP_ENTRY_NEEDS_COPY))
1145 charge_prev_obj = TRUE;
1149 if (object != NULL) {
1151 * OBJ_ONEMAPPING must be cleared unless this mapping
1152 * is trivially proven to be the only mapping for any
1153 * of the object's pages. (Object granularity
1154 * reference counting is insufficient to recognize
1155 * aliases with precision.)
1157 VM_OBJECT_LOCK(object);
1158 if (object->ref_count > 1 || object->shadow_count != 0)
1159 vm_object_clear_flag(object, OBJ_ONEMAPPING);
1160 VM_OBJECT_UNLOCK(object);
1162 else if ((prev_entry != &map->header) &&
1163 (prev_entry->eflags == protoeflags) &&
1164 (prev_entry->end == start) &&
1165 (prev_entry->wired_count == 0) &&
1166 (prev_entry->uip == uip ||
1167 (prev_entry->object.vm_object != NULL &&
1168 (prev_entry->object.vm_object->uip == uip))) &&
1169 vm_object_coalesce(prev_entry->object.vm_object,
1171 (vm_size_t)(prev_entry->end - prev_entry->start),
1172 (vm_size_t)(end - prev_entry->end), charge_prev_obj)) {
1174 * We were able to extend the object. Determine if we
1175 * can extend the previous map entry to include the
1176 * new range as well.
1178 if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
1179 (prev_entry->protection == prot) &&
1180 (prev_entry->max_protection == max)) {
1181 map->size += (end - prev_entry->end);
1182 prev_entry->end = end;
1183 vm_map_entry_resize_free(map, prev_entry);
1184 vm_map_simplify_entry(map, prev_entry);
1187 return (KERN_SUCCESS);
1191 * If we can extend the object but cannot extend the
1192 * map entry, we have to create a new map entry. We
1193 * must bump the ref count on the extended object to
1194 * account for it. object may be NULL.
1196 object = prev_entry->object.vm_object;
1197 offset = prev_entry->offset +
1198 (prev_entry->end - prev_entry->start);
1199 vm_object_reference(object);
1200 if (uip != NULL && object != NULL && object->uip != NULL &&
1201 !(prev_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
1202 /* Object already accounts for this uid. */
1209 * NOTE: if conditionals fail, object can be NULL here. This occurs
1210 * in things like the buffer map where we manage kva but do not manage
1215 * Create a new entry
1217 new_entry = vm_map_entry_create(map);
1218 new_entry->start = start;
1219 new_entry->end = end;
1220 new_entry->uip = NULL;
1222 new_entry->eflags = protoeflags;
1223 new_entry->object.vm_object = object;
1224 new_entry->offset = offset;
1225 new_entry->avail_ssize = 0;
1227 new_entry->inheritance = VM_INHERIT_DEFAULT;
1228 new_entry->protection = prot;
1229 new_entry->max_protection = max;
1230 new_entry->wired_count = 0;
1232 KASSERT(uip == NULL || !ENTRY_CHARGED(new_entry),
1233 ("OVERCOMMIT: vm_map_insert leaks vm_map %p", new_entry));
1234 new_entry->uip = uip;
1237 * Insert the new entry into the list
1239 vm_map_entry_link(map, prev_entry, new_entry);
1240 map->size += new_entry->end - new_entry->start;
1244 * Temporarily removed to avoid MAP_STACK panic, due to
1245 * MAP_STACK being a huge hack. Will be added back in
1246 * when MAP_STACK (and the user stack mapping) is fixed.
1249 * It may be possible to simplify the entry
1251 vm_map_simplify_entry(map, new_entry);
1254 if (cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) {
1255 vm_map_pmap_enter(map, start, prot,
1256 object, OFF_TO_IDX(offset), end - start,
1257 cow & MAP_PREFAULT_PARTIAL);
1260 return (KERN_SUCCESS);
1266 * Find the first fit (lowest VM address) for "length" free bytes
1267 * beginning at address >= start in the given map.
1269 * In a vm_map_entry, "adj_free" is the amount of free space
1270 * adjacent (higher address) to this entry, and "max_free" is the
1271 * maximum amount of contiguous free space in its subtree. This
1272 * allows finding a free region in one path down the tree, so
1273 * O(log n) amortized with splay trees.
1275 * The map must be locked, and leaves it so.
1277 * Returns: 0 on success, and starting address in *addr,
1278 * 1 if insufficient space.
1281 vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length,
1282 vm_offset_t *addr) /* OUT */
1284 vm_map_entry_t entry;
1285 vm_offset_t end, st;
1288 * Request must fit within min/max VM address and must avoid
1291 if (start < map->min_offset)
1292 start = map->min_offset;
1293 if (start + length > map->max_offset || start + length < start)
1296 /* Empty tree means wide open address space. */
1297 if (map->root == NULL) {
1303 * After splay, if start comes before root node, then there
1304 * must be a gap from start to the root.
1306 map->root = vm_map_entry_splay(start, map->root);
1307 if (start + length <= map->root->start) {
1313 * Root is the last node that might begin its gap before
1314 * start, and this is the last comparison where address
1315 * wrap might be a problem.
1317 st = (start > map->root->end) ? start : map->root->end;
1318 if (length <= map->root->end + map->root->adj_free - st) {
1323 /* With max_free, can immediately tell if no solution. */
1324 entry = map->root->right;
1325 if (entry == NULL || length > entry->max_free)
1329 * Search the right subtree in the order: left subtree, root,
1330 * right subtree (first fit). The previous splay implies that
1331 * all regions in the right subtree have addresses > start.
1333 while (entry != NULL) {
1334 if (entry->left != NULL && entry->left->max_free >= length)
1335 entry = entry->left;
1336 else if (entry->adj_free >= length) {
1340 entry = entry->right;
1343 /* Can't get here, so panic if we do. */
1344 panic("vm_map_findspace: max_free corrupt");
1347 /* Expand the kernel pmap, if necessary. */
1348 if (map == kernel_map) {
1349 end = round_page(*addr + length);
1350 if (end > kernel_vm_end)
1351 pmap_growkernel(end);
1357 vm_map_fixed(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1358 vm_offset_t start, vm_size_t length, vm_prot_t prot,
1359 vm_prot_t max, int cow)
1364 end = start + length;
1366 VM_MAP_RANGE_CHECK(map, start, end);
1367 (void) vm_map_delete(map, start, end);
1368 result = vm_map_insert(map, object, offset, start, end, prot,
1375 * vm_map_find finds an unallocated region in the target address
1376 * map with the given length. The search is defined to be
1377 * first-fit from the specified address; the region found is
1378 * returned in the same parameter.
1380 * If object is non-NULL, ref count must be bumped by caller
1381 * prior to making call to account for the new entry.
1384 vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1385 vm_offset_t *addr, /* IN/OUT */
1386 vm_size_t length, int find_space, vm_prot_t prot,
1387 vm_prot_t max, int cow)
1395 if (find_space != VMFS_NO_SPACE) {
1396 if (vm_map_findspace(map, start, length, addr)) {
1398 return (KERN_NO_SPACE);
1400 if (find_space == VMFS_ALIGNED_SPACE)
1401 pmap_align_superpage(object, offset, addr,
1405 result = vm_map_insert(map, object, offset, start, start +
1406 length, prot, max, cow);
1407 } while (result == KERN_NO_SPACE && find_space == VMFS_ALIGNED_SPACE);
1413 * vm_map_simplify_entry:
1415 * Simplify the given map entry by merging with either neighbor. This
1416 * routine also has the ability to merge with both neighbors.
1418 * The map must be locked.
1420 * This routine guarentees that the passed entry remains valid (though
1421 * possibly extended). When merging, this routine may delete one or
1425 vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry)
1427 vm_map_entry_t next, prev;
1428 vm_size_t prevsize, esize;
1430 if (entry->eflags & (MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP))
1434 if (prev != &map->header) {
1435 prevsize = prev->end - prev->start;
1436 if ( (prev->end == entry->start) &&
1437 (prev->object.vm_object == entry->object.vm_object) &&
1438 (!prev->object.vm_object ||
1439 (prev->offset + prevsize == entry->offset)) &&
1440 (prev->eflags == entry->eflags) &&
1441 (prev->protection == entry->protection) &&
1442 (prev->max_protection == entry->max_protection) &&
1443 (prev->inheritance == entry->inheritance) &&
1444 (prev->wired_count == entry->wired_count) &&
1445 (prev->uip == entry->uip)) {
1446 vm_map_entry_unlink(map, prev);
1447 entry->start = prev->start;
1448 entry->offset = prev->offset;
1449 if (entry->prev != &map->header)
1450 vm_map_entry_resize_free(map, entry->prev);
1453 * If the backing object is a vnode object,
1454 * vm_object_deallocate() calls vrele().
1455 * However, vrele() does not lock the vnode
1456 * because the vnode has additional
1457 * references. Thus, the map lock can be kept
1458 * without causing a lock-order reversal with
1461 if (prev->object.vm_object)
1462 vm_object_deallocate(prev->object.vm_object);
1463 if (prev->uip != NULL)
1465 vm_map_entry_dispose(map, prev);
1470 if (next != &map->header) {
1471 esize = entry->end - entry->start;
1472 if ((entry->end == next->start) &&
1473 (next->object.vm_object == entry->object.vm_object) &&
1474 (!entry->object.vm_object ||
1475 (entry->offset + esize == next->offset)) &&
1476 (next->eflags == entry->eflags) &&
1477 (next->protection == entry->protection) &&
1478 (next->max_protection == entry->max_protection) &&
1479 (next->inheritance == entry->inheritance) &&
1480 (next->wired_count == entry->wired_count) &&
1481 (next->uip == entry->uip)) {
1482 vm_map_entry_unlink(map, next);
1483 entry->end = next->end;
1484 vm_map_entry_resize_free(map, entry);
1487 * See comment above.
1489 if (next->object.vm_object)
1490 vm_object_deallocate(next->object.vm_object);
1491 if (next->uip != NULL)
1493 vm_map_entry_dispose(map, next);
1498 * vm_map_clip_start: [ internal use only ]
1500 * Asserts that the given entry begins at or after
1501 * the specified address; if necessary,
1502 * it splits the entry into two.
1504 #define vm_map_clip_start(map, entry, startaddr) \
1506 if (startaddr > entry->start) \
1507 _vm_map_clip_start(map, entry, startaddr); \
1511 * This routine is called only when it is known that
1512 * the entry must be split.
1515 _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start)
1517 vm_map_entry_t new_entry;
1519 VM_MAP_ASSERT_LOCKED(map);
1522 * Split off the front portion -- note that we must insert the new
1523 * entry BEFORE this one, so that this entry has the specified
1526 vm_map_simplify_entry(map, entry);
1529 * If there is no object backing this entry, we might as well create
1530 * one now. If we defer it, an object can get created after the map
1531 * is clipped, and individual objects will be created for the split-up
1532 * map. This is a bit of a hack, but is also about the best place to
1533 * put this improvement.
1535 if (entry->object.vm_object == NULL && !map->system_map) {
1537 object = vm_object_allocate(OBJT_DEFAULT,
1538 atop(entry->end - entry->start));
1539 entry->object.vm_object = object;
1541 if (entry->uip != NULL) {
1542 object->uip = entry->uip;
1543 object->charge = entry->end - entry->start;
1546 } else if (entry->object.vm_object != NULL &&
1547 ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) &&
1548 entry->uip != NULL) {
1549 VM_OBJECT_LOCK(entry->object.vm_object);
1550 KASSERT(entry->object.vm_object->uip == NULL,
1551 ("OVERCOMMIT: vm_entry_clip_start: both uip e %p", entry));
1552 entry->object.vm_object->uip = entry->uip;
1553 entry->object.vm_object->charge = entry->end - entry->start;
1554 VM_OBJECT_UNLOCK(entry->object.vm_object);
1558 new_entry = vm_map_entry_create(map);
1559 *new_entry = *entry;
1561 new_entry->end = start;
1562 entry->offset += (start - entry->start);
1563 entry->start = start;
1564 if (new_entry->uip != NULL)
1567 vm_map_entry_link(map, entry->prev, new_entry);
1569 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1570 vm_object_reference(new_entry->object.vm_object);
1575 * vm_map_clip_end: [ internal use only ]
1577 * Asserts that the given entry ends at or before
1578 * the specified address; if necessary,
1579 * it splits the entry into two.
1581 #define vm_map_clip_end(map, entry, endaddr) \
1583 if ((endaddr) < (entry->end)) \
1584 _vm_map_clip_end((map), (entry), (endaddr)); \
1588 * This routine is called only when it is known that
1589 * the entry must be split.
1592 _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end)
1594 vm_map_entry_t new_entry;
1596 VM_MAP_ASSERT_LOCKED(map);
1599 * If there is no object backing this entry, we might as well create
1600 * one now. If we defer it, an object can get created after the map
1601 * is clipped, and individual objects will be created for the split-up
1602 * map. This is a bit of a hack, but is also about the best place to
1603 * put this improvement.
1605 if (entry->object.vm_object == NULL && !map->system_map) {
1607 object = vm_object_allocate(OBJT_DEFAULT,
1608 atop(entry->end - entry->start));
1609 entry->object.vm_object = object;
1611 if (entry->uip != NULL) {
1612 object->uip = entry->uip;
1613 object->charge = entry->end - entry->start;
1616 } else if (entry->object.vm_object != NULL &&
1617 ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) &&
1618 entry->uip != NULL) {
1619 VM_OBJECT_LOCK(entry->object.vm_object);
1620 KASSERT(entry->object.vm_object->uip == NULL,
1621 ("OVERCOMMIT: vm_entry_clip_end: both uip e %p", entry));
1622 entry->object.vm_object->uip = entry->uip;
1623 entry->object.vm_object->charge = entry->end - entry->start;
1624 VM_OBJECT_UNLOCK(entry->object.vm_object);
1629 * Create a new entry and insert it AFTER the specified entry
1631 new_entry = vm_map_entry_create(map);
1632 *new_entry = *entry;
1634 new_entry->start = entry->end = end;
1635 new_entry->offset += (end - entry->start);
1636 if (new_entry->uip != NULL)
1639 vm_map_entry_link(map, entry, new_entry);
1641 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1642 vm_object_reference(new_entry->object.vm_object);
1647 * vm_map_submap: [ kernel use only ]
1649 * Mark the given range as handled by a subordinate map.
1651 * This range must have been created with vm_map_find,
1652 * and no other operations may have been performed on this
1653 * range prior to calling vm_map_submap.
1655 * Only a limited number of operations can be performed
1656 * within this rage after calling vm_map_submap:
1658 * [Don't try vm_map_copy!]
1660 * To remove a submapping, one must first remove the
1661 * range from the superior map, and then destroy the
1662 * submap (if desired). [Better yet, don't try it.]
1671 vm_map_entry_t entry;
1672 int result = KERN_INVALID_ARGUMENT;
1676 VM_MAP_RANGE_CHECK(map, start, end);
1678 if (vm_map_lookup_entry(map, start, &entry)) {
1679 vm_map_clip_start(map, entry, start);
1681 entry = entry->next;
1683 vm_map_clip_end(map, entry, end);
1685 if ((entry->start == start) && (entry->end == end) &&
1686 ((entry->eflags & MAP_ENTRY_COW) == 0) &&
1687 (entry->object.vm_object == NULL)) {
1688 entry->object.sub_map = submap;
1689 entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
1690 result = KERN_SUCCESS;
1698 * The maximum number of pages to map
1700 #define MAX_INIT_PT 96
1703 * vm_map_pmap_enter:
1705 * Preload read-only mappings for the given object's resident pages into
1706 * the given map. This eliminates the soft faults on process startup and
1707 * immediately after an mmap(2). Because these are speculative mappings,
1708 * cached pages are not reactivated and mapped.
1711 vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
1712 vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags)
1715 vm_page_t p, p_start;
1716 vm_pindex_t psize, tmpidx;
1717 boolean_t are_queues_locked;
1719 if ((prot & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 || object == NULL)
1721 VM_OBJECT_LOCK(object);
1722 if (object->type == OBJT_DEVICE || object->type == OBJT_SG) {
1723 pmap_object_init_pt(map->pmap, addr, object, pindex, size);
1729 if ((flags & MAP_PREFAULT_PARTIAL) && psize > MAX_INIT_PT &&
1730 object->resident_page_count > MAX_INIT_PT)
1733 if (psize + pindex > object->size) {
1734 if (object->size < pindex)
1736 psize = object->size - pindex;
1739 are_queues_locked = FALSE;
1743 if ((p = TAILQ_FIRST(&object->memq)) != NULL) {
1744 if (p->pindex < pindex) {
1745 p = vm_page_splay(pindex, object->root);
1746 if ((object->root = p)->pindex < pindex)
1747 p = TAILQ_NEXT(p, listq);
1751 * Assert: the variable p is either (1) the page with the
1752 * least pindex greater than or equal to the parameter pindex
1756 p != NULL && (tmpidx = p->pindex - pindex) < psize;
1757 p = TAILQ_NEXT(p, listq)) {
1759 * don't allow an madvise to blow away our really
1760 * free pages allocating pv entries.
1762 if ((flags & MAP_PREFAULT_MADVISE) &&
1763 cnt.v_free_count < cnt.v_free_reserved) {
1767 if (p->valid == VM_PAGE_BITS_ALL) {
1768 if (p_start == NULL) {
1769 start = addr + ptoa(tmpidx);
1772 } else if (p_start != NULL) {
1773 if (!are_queues_locked) {
1774 are_queues_locked = TRUE;
1775 vm_page_lock_queues();
1777 pmap_enter_object(map->pmap, start, addr +
1778 ptoa(tmpidx), p_start, prot);
1782 if (p_start != NULL) {
1783 if (!are_queues_locked) {
1784 are_queues_locked = TRUE;
1785 vm_page_lock_queues();
1787 pmap_enter_object(map->pmap, start, addr + ptoa(psize),
1790 if (are_queues_locked)
1791 vm_page_unlock_queues();
1793 VM_OBJECT_UNLOCK(object);
1799 * Sets the protection of the specified address
1800 * region in the target map. If "set_max" is
1801 * specified, the maximum protection is to be set;
1802 * otherwise, only the current protection is affected.
1805 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
1806 vm_prot_t new_prot, boolean_t set_max)
1808 vm_map_entry_t current, entry;
1810 struct uidinfo *uip;
1815 VM_MAP_RANGE_CHECK(map, start, end);
1817 if (vm_map_lookup_entry(map, start, &entry)) {
1818 vm_map_clip_start(map, entry, start);
1820 entry = entry->next;
1824 * Make a first pass to check for protection violations.
1827 while ((current != &map->header) && (current->start < end)) {
1828 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1830 return (KERN_INVALID_ARGUMENT);
1832 if ((new_prot & current->max_protection) != new_prot) {
1834 return (KERN_PROTECTION_FAILURE);
1836 current = current->next;
1841 * Do an accounting pass for private read-only mappings that
1842 * now will do cow due to allowed write (e.g. debugger sets
1843 * breakpoint on text segment)
1845 for (current = entry; (current != &map->header) &&
1846 (current->start < end); current = current->next) {
1848 vm_map_clip_end(map, current, end);
1851 ((new_prot & ~(current->protection)) & VM_PROT_WRITE) == 0 ||
1852 ENTRY_CHARGED(current)) {
1856 uip = curthread->td_ucred->cr_ruidinfo;
1857 obj = current->object.vm_object;
1859 if (obj == NULL || (current->eflags & MAP_ENTRY_NEEDS_COPY)) {
1860 if (!swap_reserve(current->end - current->start)) {
1862 return (KERN_RESOURCE_SHORTAGE);
1869 VM_OBJECT_LOCK(obj);
1870 if (obj->type != OBJT_DEFAULT && obj->type != OBJT_SWAP) {
1871 VM_OBJECT_UNLOCK(obj);
1876 * Charge for the whole object allocation now, since
1877 * we cannot distinguish between non-charged and
1878 * charged clipped mapping of the same object later.
1880 KASSERT(obj->charge == 0,
1881 ("vm_map_protect: object %p overcharged\n", obj));
1882 if (!swap_reserve(ptoa(obj->size))) {
1883 VM_OBJECT_UNLOCK(obj);
1885 return (KERN_RESOURCE_SHORTAGE);
1890 obj->charge = ptoa(obj->size);
1891 VM_OBJECT_UNLOCK(obj);
1895 * Go back and fix up protections. [Note that clipping is not
1896 * necessary the second time.]
1899 while ((current != &map->header) && (current->start < end)) {
1900 old_prot = current->protection;
1903 current->protection =
1904 (current->max_protection = new_prot) &
1907 current->protection = new_prot;
1909 if ((current->eflags & (MAP_ENTRY_COW | MAP_ENTRY_USER_WIRED))
1910 == (MAP_ENTRY_COW | MAP_ENTRY_USER_WIRED) &&
1911 (current->protection & VM_PROT_WRITE) != 0 &&
1912 (old_prot & VM_PROT_WRITE) == 0) {
1913 vm_fault_copy_entry(map, map, current, current, NULL);
1917 * Update physical map if necessary. Worry about copy-on-write
1920 if (current->protection != old_prot) {
1921 #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1923 pmap_protect(map->pmap, current->start,
1925 current->protection & MASK(current));
1928 vm_map_simplify_entry(map, current);
1929 current = current->next;
1932 return (KERN_SUCCESS);
1938 * This routine traverses a processes map handling the madvise
1939 * system call. Advisories are classified as either those effecting
1940 * the vm_map_entry structure, or those effecting the underlying
1950 vm_map_entry_t current, entry;
1954 * Some madvise calls directly modify the vm_map_entry, in which case
1955 * we need to use an exclusive lock on the map and we need to perform
1956 * various clipping operations. Otherwise we only need a read-lock
1961 case MADV_SEQUENTIAL:
1973 vm_map_lock_read(map);
1976 return (KERN_INVALID_ARGUMENT);
1980 * Locate starting entry and clip if necessary.
1982 VM_MAP_RANGE_CHECK(map, start, end);
1984 if (vm_map_lookup_entry(map, start, &entry)) {
1986 vm_map_clip_start(map, entry, start);
1988 entry = entry->next;
1993 * madvise behaviors that are implemented in the vm_map_entry.
1995 * We clip the vm_map_entry so that behavioral changes are
1996 * limited to the specified address range.
1998 for (current = entry;
1999 (current != &map->header) && (current->start < end);
2000 current = current->next
2002 if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
2005 vm_map_clip_end(map, current, end);
2009 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
2011 case MADV_SEQUENTIAL:
2012 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
2015 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
2018 current->eflags |= MAP_ENTRY_NOSYNC;
2021 current->eflags &= ~MAP_ENTRY_NOSYNC;
2024 current->eflags |= MAP_ENTRY_NOCOREDUMP;
2027 current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
2032 vm_map_simplify_entry(map, current);
2040 * madvise behaviors that are implemented in the underlying
2043 * Since we don't clip the vm_map_entry, we have to clip
2044 * the vm_object pindex and count.
2046 for (current = entry;
2047 (current != &map->header) && (current->start < end);
2048 current = current->next
2050 vm_offset_t useStart;
2052 if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
2055 pindex = OFF_TO_IDX(current->offset);
2056 count = atop(current->end - current->start);
2057 useStart = current->start;
2059 if (current->start < start) {
2060 pindex += atop(start - current->start);
2061 count -= atop(start - current->start);
2064 if (current->end > end)
2065 count -= atop(current->end - end);
2070 vm_object_madvise(current->object.vm_object,
2071 pindex, count, behav);
2072 if (behav == MADV_WILLNEED) {
2073 vm_map_pmap_enter(map,
2075 current->protection,
2076 current->object.vm_object,
2078 (count << PAGE_SHIFT),
2079 MAP_PREFAULT_MADVISE
2083 vm_map_unlock_read(map);
2092 * Sets the inheritance of the specified address
2093 * range in the target map. Inheritance
2094 * affects how the map will be shared with
2095 * child maps at the time of vmspace_fork.
2098 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
2099 vm_inherit_t new_inheritance)
2101 vm_map_entry_t entry;
2102 vm_map_entry_t temp_entry;
2104 switch (new_inheritance) {
2105 case VM_INHERIT_NONE:
2106 case VM_INHERIT_COPY:
2107 case VM_INHERIT_SHARE:
2110 return (KERN_INVALID_ARGUMENT);
2113 VM_MAP_RANGE_CHECK(map, start, end);
2114 if (vm_map_lookup_entry(map, start, &temp_entry)) {
2116 vm_map_clip_start(map, entry, start);
2118 entry = temp_entry->next;
2119 while ((entry != &map->header) && (entry->start < end)) {
2120 vm_map_clip_end(map, entry, end);
2121 entry->inheritance = new_inheritance;
2122 vm_map_simplify_entry(map, entry);
2123 entry = entry->next;
2126 return (KERN_SUCCESS);
2132 * Implements both kernel and user unwiring.
2135 vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
2138 vm_map_entry_t entry, first_entry, tmp_entry;
2139 vm_offset_t saved_start;
2140 unsigned int last_timestamp;
2142 boolean_t need_wakeup, result, user_unwire;
2144 user_unwire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
2146 VM_MAP_RANGE_CHECK(map, start, end);
2147 if (!vm_map_lookup_entry(map, start, &first_entry)) {
2148 if (flags & VM_MAP_WIRE_HOLESOK)
2149 first_entry = first_entry->next;
2152 return (KERN_INVALID_ADDRESS);
2155 last_timestamp = map->timestamp;
2156 entry = first_entry;
2157 while (entry != &map->header && entry->start < end) {
2158 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
2160 * We have not yet clipped the entry.
2162 saved_start = (start >= entry->start) ? start :
2164 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2165 if (vm_map_unlock_and_wait(map, 0)) {
2167 * Allow interruption of user unwiring?
2171 if (last_timestamp+1 != map->timestamp) {
2173 * Look again for the entry because the map was
2174 * modified while it was unlocked.
2175 * Specifically, the entry may have been
2176 * clipped, merged, or deleted.
2178 if (!vm_map_lookup_entry(map, saved_start,
2180 if (flags & VM_MAP_WIRE_HOLESOK)
2181 tmp_entry = tmp_entry->next;
2183 if (saved_start == start) {
2185 * First_entry has been deleted.
2188 return (KERN_INVALID_ADDRESS);
2191 rv = KERN_INVALID_ADDRESS;
2195 if (entry == first_entry)
2196 first_entry = tmp_entry;
2201 last_timestamp = map->timestamp;
2204 vm_map_clip_start(map, entry, start);
2205 vm_map_clip_end(map, entry, end);
2207 * Mark the entry in case the map lock is released. (See
2210 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
2212 * Check the map for holes in the specified region.
2213 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
2215 if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
2216 (entry->end < end && (entry->next == &map->header ||
2217 entry->next->start > entry->end))) {
2219 rv = KERN_INVALID_ADDRESS;
2223 * If system unwiring, require that the entry is system wired.
2226 vm_map_entry_system_wired_count(entry) == 0) {
2228 rv = KERN_INVALID_ARGUMENT;
2231 entry = entry->next;
2235 need_wakeup = FALSE;
2236 if (first_entry == NULL) {
2237 result = vm_map_lookup_entry(map, start, &first_entry);
2238 if (!result && (flags & VM_MAP_WIRE_HOLESOK))
2239 first_entry = first_entry->next;
2241 KASSERT(result, ("vm_map_unwire: lookup failed"));
2243 entry = first_entry;
2244 while (entry != &map->header && entry->start < end) {
2245 if (rv == KERN_SUCCESS && (!user_unwire ||
2246 (entry->eflags & MAP_ENTRY_USER_WIRED))) {
2248 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2249 entry->wired_count--;
2250 if (entry->wired_count == 0) {
2252 * Retain the map lock.
2254 vm_fault_unwire(map, entry->start, entry->end,
2255 entry->object.vm_object != NULL &&
2256 (entry->object.vm_object->type == OBJT_DEVICE ||
2257 entry->object.vm_object->type == OBJT_SG));
2260 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
2261 ("vm_map_unwire: in-transition flag missing"));
2262 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
2263 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
2264 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
2267 vm_map_simplify_entry(map, entry);
2268 entry = entry->next;
2279 * Implements both kernel and user wiring.
2282 vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
2285 vm_map_entry_t entry, first_entry, tmp_entry;
2286 vm_offset_t saved_end, saved_start;
2287 unsigned int last_timestamp;
2289 boolean_t fictitious, need_wakeup, result, user_wire;
2291 user_wire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
2293 VM_MAP_RANGE_CHECK(map, start, end);
2294 if (!vm_map_lookup_entry(map, start, &first_entry)) {
2295 if (flags & VM_MAP_WIRE_HOLESOK)
2296 first_entry = first_entry->next;
2299 return (KERN_INVALID_ADDRESS);
2302 last_timestamp = map->timestamp;
2303 entry = first_entry;
2304 while (entry != &map->header && entry->start < end) {
2305 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
2307 * We have not yet clipped the entry.
2309 saved_start = (start >= entry->start) ? start :
2311 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2312 if (vm_map_unlock_and_wait(map, 0)) {
2314 * Allow interruption of user wiring?
2318 if (last_timestamp + 1 != map->timestamp) {
2320 * Look again for the entry because the map was
2321 * modified while it was unlocked.
2322 * Specifically, the entry may have been
2323 * clipped, merged, or deleted.
2325 if (!vm_map_lookup_entry(map, saved_start,
2327 if (flags & VM_MAP_WIRE_HOLESOK)
2328 tmp_entry = tmp_entry->next;
2330 if (saved_start == start) {
2332 * first_entry has been deleted.
2335 return (KERN_INVALID_ADDRESS);
2338 rv = KERN_INVALID_ADDRESS;
2342 if (entry == first_entry)
2343 first_entry = tmp_entry;
2348 last_timestamp = map->timestamp;
2351 vm_map_clip_start(map, entry, start);
2352 vm_map_clip_end(map, entry, end);
2354 * Mark the entry in case the map lock is released. (See
2357 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
2361 if (entry->wired_count == 0) {
2362 if ((entry->protection & (VM_PROT_READ|VM_PROT_EXECUTE))
2364 entry->eflags |= MAP_ENTRY_WIRE_SKIPPED;
2365 if ((flags & VM_MAP_WIRE_HOLESOK) == 0) {
2367 rv = KERN_INVALID_ADDRESS;
2372 entry->wired_count++;
2373 saved_start = entry->start;
2374 saved_end = entry->end;
2375 fictitious = entry->object.vm_object != NULL &&
2376 (entry->object.vm_object->type == OBJT_DEVICE ||
2377 entry->object.vm_object->type == OBJT_SG);
2379 * Release the map lock, relying on the in-transition
2383 rv = vm_fault_wire(map, saved_start, saved_end,
2384 user_wire, fictitious);
2386 if (last_timestamp + 1 != map->timestamp) {
2388 * Look again for the entry because the map was
2389 * modified while it was unlocked. The entry
2390 * may have been clipped, but NOT merged or
2393 result = vm_map_lookup_entry(map, saved_start,
2395 KASSERT(result, ("vm_map_wire: lookup failed"));
2396 if (entry == first_entry)
2397 first_entry = tmp_entry;
2401 while (entry->end < saved_end) {
2402 if (rv != KERN_SUCCESS) {
2403 KASSERT(entry->wired_count == 1,
2404 ("vm_map_wire: bad count"));
2405 entry->wired_count = -1;
2407 entry = entry->next;
2410 last_timestamp = map->timestamp;
2411 if (rv != KERN_SUCCESS) {
2412 KASSERT(entry->wired_count == 1,
2413 ("vm_map_wire: bad count"));
2415 * Assign an out-of-range value to represent
2416 * the failure to wire this entry.
2418 entry->wired_count = -1;
2422 } else if (!user_wire ||
2423 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
2424 entry->wired_count++;
2427 * Check the map for holes in the specified region.
2428 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
2431 if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
2432 (entry->end < end && (entry->next == &map->header ||
2433 entry->next->start > entry->end))) {
2435 rv = KERN_INVALID_ADDRESS;
2438 entry = entry->next;
2442 need_wakeup = FALSE;
2443 if (first_entry == NULL) {
2444 result = vm_map_lookup_entry(map, start, &first_entry);
2445 if (!result && (flags & VM_MAP_WIRE_HOLESOK))
2446 first_entry = first_entry->next;
2448 KASSERT(result, ("vm_map_wire: lookup failed"));
2450 entry = first_entry;
2451 while (entry != &map->header && entry->start < end) {
2452 if ((entry->eflags & MAP_ENTRY_WIRE_SKIPPED) != 0)
2453 goto next_entry_done;
2454 if (rv == KERN_SUCCESS) {
2456 entry->eflags |= MAP_ENTRY_USER_WIRED;
2457 } else if (entry->wired_count == -1) {
2459 * Wiring failed on this entry. Thus, unwiring is
2462 entry->wired_count = 0;
2465 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0)
2466 entry->wired_count--;
2467 if (entry->wired_count == 0) {
2469 * Retain the map lock.
2471 vm_fault_unwire(map, entry->start, entry->end,
2472 entry->object.vm_object != NULL &&
2473 (entry->object.vm_object->type == OBJT_DEVICE ||
2474 entry->object.vm_object->type == OBJT_SG));
2478 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
2479 ("vm_map_wire: in-transition flag missing"));
2480 entry->eflags &= ~(MAP_ENTRY_IN_TRANSITION|MAP_ENTRY_WIRE_SKIPPED);
2481 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
2482 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
2485 vm_map_simplify_entry(map, entry);
2486 entry = entry->next;
2497 * Push any dirty cached pages in the address range to their pager.
2498 * If syncio is TRUE, dirty pages are written synchronously.
2499 * If invalidate is TRUE, any cached pages are freed as well.
2501 * If the size of the region from start to end is zero, we are
2502 * supposed to flush all modified pages within the region containing
2503 * start. Unfortunately, a region can be split or coalesced with
2504 * neighboring regions, making it difficult to determine what the
2505 * original region was. Therefore, we approximate this requirement by
2506 * flushing the current region containing start.
2508 * Returns an error if any part of the specified range is not mapped.
2516 boolean_t invalidate)
2518 vm_map_entry_t current;
2519 vm_map_entry_t entry;
2522 vm_ooffset_t offset;
2523 unsigned int last_timestamp;
2525 vm_map_lock_read(map);
2526 VM_MAP_RANGE_CHECK(map, start, end);
2527 if (!vm_map_lookup_entry(map, start, &entry)) {
2528 vm_map_unlock_read(map);
2529 return (KERN_INVALID_ADDRESS);
2530 } else if (start == end) {
2531 start = entry->start;
2535 * Make a first pass to check for user-wired memory and holes.
2537 for (current = entry; current != &map->header && current->start < end;
2538 current = current->next) {
2539 if (invalidate && (current->eflags & MAP_ENTRY_USER_WIRED)) {
2540 vm_map_unlock_read(map);
2541 return (KERN_INVALID_ARGUMENT);
2543 if (end > current->end &&
2544 (current->next == &map->header ||
2545 current->end != current->next->start)) {
2546 vm_map_unlock_read(map);
2547 return (KERN_INVALID_ADDRESS);
2552 pmap_remove(map->pmap, start, end);
2555 * Make a second pass, cleaning/uncaching pages from the indicated
2558 for (current = entry; current != &map->header && current->start < end;) {
2559 offset = current->offset + (start - current->start);
2560 size = (end <= current->end ? end : current->end) - start;
2561 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
2563 vm_map_entry_t tentry;
2566 smap = current->object.sub_map;
2567 vm_map_lock_read(smap);
2568 (void) vm_map_lookup_entry(smap, offset, &tentry);
2569 tsize = tentry->end - offset;
2572 object = tentry->object.vm_object;
2573 offset = tentry->offset + (offset - tentry->start);
2574 vm_map_unlock_read(smap);
2576 object = current->object.vm_object;
2578 vm_object_reference(object);
2579 last_timestamp = map->timestamp;
2580 vm_map_unlock_read(map);
2581 vm_object_sync(object, offset, size, syncio, invalidate);
2583 vm_object_deallocate(object);
2584 vm_map_lock_read(map);
2585 if (last_timestamp == map->timestamp ||
2586 !vm_map_lookup_entry(map, start, ¤t))
2587 current = current->next;
2590 vm_map_unlock_read(map);
2591 return (KERN_SUCCESS);
2595 * vm_map_entry_unwire: [ internal use only ]
2597 * Make the region specified by this entry pageable.
2599 * The map in question should be locked.
2600 * [This is the reason for this routine's existence.]
2603 vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
2605 vm_fault_unwire(map, entry->start, entry->end,
2606 entry->object.vm_object != NULL &&
2607 (entry->object.vm_object->type == OBJT_DEVICE ||
2608 entry->object.vm_object->type == OBJT_SG));
2609 entry->wired_count = 0;
2613 * vm_map_entry_delete: [ internal use only ]
2615 * Deallocate the given entry from the target map.
2618 vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry)
2621 vm_pindex_t offidxstart, offidxend, count, size1;
2624 vm_map_entry_unlink(map, entry);
2625 object = entry->object.vm_object;
2626 size = entry->end - entry->start;
2629 if (entry->uip != NULL) {
2630 swap_release_by_uid(size, entry->uip);
2634 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
2636 KASSERT(entry->uip == NULL || object->uip == NULL ||
2637 (entry->eflags & MAP_ENTRY_NEEDS_COPY),
2638 ("OVERCOMMIT vm_map_entry_delete: both uip %p", entry));
2639 count = OFF_TO_IDX(size);
2640 offidxstart = OFF_TO_IDX(entry->offset);
2641 offidxend = offidxstart + count;
2642 VM_OBJECT_LOCK(object);
2643 if (object->ref_count != 1 &&
2644 ((object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING ||
2645 object == kernel_object || object == kmem_object)) {
2646 vm_object_collapse(object);
2647 vm_object_page_remove(object, offidxstart, offidxend, FALSE);
2648 if (object->type == OBJT_SWAP)
2649 swap_pager_freespace(object, offidxstart, count);
2650 if (offidxend >= object->size &&
2651 offidxstart < object->size) {
2652 size1 = object->size;
2653 object->size = offidxstart;
2654 if (object->uip != NULL) {
2655 size1 -= object->size;
2656 KASSERT(object->charge >= ptoa(size1),
2657 ("vm_map_entry_delete: object->charge < 0"));
2658 swap_release_by_uid(ptoa(size1), object->uip);
2659 object->charge -= ptoa(size1);
2663 VM_OBJECT_UNLOCK(object);
2665 entry->object.vm_object = NULL;
2669 * vm_map_delete: [ internal use only ]
2671 * Deallocates the given address range from the target
2675 vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end)
2677 vm_map_entry_t entry;
2678 vm_map_entry_t first_entry;
2680 VM_MAP_ASSERT_LOCKED(map);
2683 * Find the start of the region, and clip it
2685 if (!vm_map_lookup_entry(map, start, &first_entry))
2686 entry = first_entry->next;
2688 entry = first_entry;
2689 vm_map_clip_start(map, entry, start);
2693 * Step through all entries in this region
2695 while ((entry != &map->header) && (entry->start < end)) {
2696 vm_map_entry_t next;
2699 * Wait for wiring or unwiring of an entry to complete.
2700 * Also wait for any system wirings to disappear on
2703 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 ||
2704 (vm_map_pmap(map) != kernel_pmap &&
2705 vm_map_entry_system_wired_count(entry) != 0)) {
2706 unsigned int last_timestamp;
2707 vm_offset_t saved_start;
2708 vm_map_entry_t tmp_entry;
2710 saved_start = entry->start;
2711 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2712 last_timestamp = map->timestamp;
2713 (void) vm_map_unlock_and_wait(map, 0);
2715 if (last_timestamp + 1 != map->timestamp) {
2717 * Look again for the entry because the map was
2718 * modified while it was unlocked.
2719 * Specifically, the entry may have been
2720 * clipped, merged, or deleted.
2722 if (!vm_map_lookup_entry(map, saved_start,
2724 entry = tmp_entry->next;
2727 vm_map_clip_start(map, entry,
2733 vm_map_clip_end(map, entry, end);
2738 * Unwire before removing addresses from the pmap; otherwise,
2739 * unwiring will put the entries back in the pmap.
2741 if (entry->wired_count != 0) {
2742 vm_map_entry_unwire(map, entry);
2745 pmap_remove(map->pmap, entry->start, entry->end);
2748 * Delete the entry only after removing all pmap
2749 * entries pointing to its pages. (Otherwise, its
2750 * page frames may be reallocated, and any modify bits
2751 * will be set in the wrong object!)
2753 vm_map_entry_delete(map, entry);
2754 entry->next = map->deferred_freelist;
2755 map->deferred_freelist = entry;
2758 return (KERN_SUCCESS);
2764 * Remove the given address range from the target map.
2765 * This is the exported form of vm_map_delete.
2768 vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
2773 VM_MAP_RANGE_CHECK(map, start, end);
2774 result = vm_map_delete(map, start, end);
2780 * vm_map_check_protection:
2782 * Assert that the target map allows the specified privilege on the
2783 * entire address region given. The entire region must be allocated.
2785 * WARNING! This code does not and should not check whether the
2786 * contents of the region is accessible. For example a smaller file
2787 * might be mapped into a larger address space.
2789 * NOTE! This code is also called by munmap().
2791 * The map must be locked. A read lock is sufficient.
2794 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
2795 vm_prot_t protection)
2797 vm_map_entry_t entry;
2798 vm_map_entry_t tmp_entry;
2800 if (!vm_map_lookup_entry(map, start, &tmp_entry))
2804 while (start < end) {
2805 if (entry == &map->header)
2810 if (start < entry->start)
2813 * Check protection associated with entry.
2815 if ((entry->protection & protection) != protection)
2817 /* go to next entry */
2819 entry = entry->next;
2825 * vm_map_copy_entry:
2827 * Copies the contents of the source entry to the destination
2828 * entry. The entries *must* be aligned properly.
2834 vm_map_entry_t src_entry,
2835 vm_map_entry_t dst_entry,
2836 vm_ooffset_t *fork_charge)
2838 vm_object_t src_object;
2840 struct uidinfo *uip;
2843 VM_MAP_ASSERT_LOCKED(dst_map);
2845 if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
2848 if (src_entry->wired_count == 0) {
2851 * If the source entry is marked needs_copy, it is already
2854 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
2855 pmap_protect(src_map->pmap,
2858 src_entry->protection & ~VM_PROT_WRITE);
2862 * Make a copy of the object.
2864 size = src_entry->end - src_entry->start;
2865 if ((src_object = src_entry->object.vm_object) != NULL) {
2866 VM_OBJECT_LOCK(src_object);
2867 charged = ENTRY_CHARGED(src_entry);
2868 if ((src_object->handle == NULL) &&
2869 (src_object->type == OBJT_DEFAULT ||
2870 src_object->type == OBJT_SWAP)) {
2871 vm_object_collapse(src_object);
2872 if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
2873 vm_object_split(src_entry);
2874 src_object = src_entry->object.vm_object;
2877 vm_object_reference_locked(src_object);
2878 vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
2879 if (src_entry->uip != NULL &&
2880 !(src_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
2881 KASSERT(src_object->uip == NULL,
2882 ("OVERCOMMIT: vm_map_copy_entry: uip %p",
2884 src_object->uip = src_entry->uip;
2885 src_object->charge = size;
2887 VM_OBJECT_UNLOCK(src_object);
2888 dst_entry->object.vm_object = src_object;
2890 uip = curthread->td_ucred->cr_ruidinfo;
2892 dst_entry->uip = uip;
2893 *fork_charge += size;
2894 if (!(src_entry->eflags &
2895 MAP_ENTRY_NEEDS_COPY)) {
2897 src_entry->uip = uip;
2898 *fork_charge += size;
2901 src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2902 dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2903 dst_entry->offset = src_entry->offset;
2905 dst_entry->object.vm_object = NULL;
2906 dst_entry->offset = 0;
2907 if (src_entry->uip != NULL) {
2908 dst_entry->uip = curthread->td_ucred->cr_ruidinfo;
2909 uihold(dst_entry->uip);
2910 *fork_charge += size;
2914 pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
2915 dst_entry->end - dst_entry->start, src_entry->start);
2918 * Of course, wired down pages can't be set copy-on-write.
2919 * Cause wired pages to be copied into the new map by
2920 * simulating faults (the new pages are pageable)
2922 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry,
2928 * vmspace_map_entry_forked:
2929 * Update the newly-forked vmspace each time a map entry is inherited
2930 * or copied. The values for vm_dsize and vm_tsize are approximate
2931 * (and mostly-obsolete ideas in the face of mmap(2) et al.)
2934 vmspace_map_entry_forked(const struct vmspace *vm1, struct vmspace *vm2,
2935 vm_map_entry_t entry)
2937 vm_size_t entrysize;
2940 entrysize = entry->end - entry->start;
2941 vm2->vm_map.size += entrysize;
2942 if (entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP)) {
2943 vm2->vm_ssize += btoc(entrysize);
2944 } else if (entry->start >= (vm_offset_t)vm1->vm_daddr &&
2945 entry->start < (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)) {
2946 newend = MIN(entry->end,
2947 (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize));
2948 vm2->vm_dsize += btoc(newend - entry->start);
2949 } else if (entry->start >= (vm_offset_t)vm1->vm_taddr &&
2950 entry->start < (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)) {
2951 newend = MIN(entry->end,
2952 (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize));
2953 vm2->vm_tsize += btoc(newend - entry->start);
2959 * Create a new process vmspace structure and vm_map
2960 * based on those of an existing process. The new map
2961 * is based on the old map, according to the inheritance
2962 * values on the regions in that map.
2964 * XXX It might be worth coalescing the entries added to the new vmspace.
2966 * The source map must not be locked.
2969 vmspace_fork(struct vmspace *vm1, vm_ooffset_t *fork_charge)
2971 struct vmspace *vm2;
2972 vm_map_t old_map = &vm1->vm_map;
2974 vm_map_entry_t old_entry;
2975 vm_map_entry_t new_entry;
2979 vm_map_lock(old_map);
2980 vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
2982 goto unlock_and_return;
2983 vm2->vm_taddr = vm1->vm_taddr;
2984 vm2->vm_daddr = vm1->vm_daddr;
2985 vm2->vm_maxsaddr = vm1->vm_maxsaddr;
2986 new_map = &vm2->vm_map; /* XXX */
2987 locked = vm_map_trylock(new_map); /* trylock to silence WITNESS */
2988 KASSERT(locked, ("vmspace_fork: lock failed"));
2989 new_map->timestamp = 1;
2991 old_entry = old_map->header.next;
2993 while (old_entry != &old_map->header) {
2994 if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2995 panic("vm_map_fork: encountered a submap");
2997 switch (old_entry->inheritance) {
2998 case VM_INHERIT_NONE:
3001 case VM_INHERIT_SHARE:
3003 * Clone the entry, creating the shared object if necessary.
3005 object = old_entry->object.vm_object;
3006 if (object == NULL) {
3007 object = vm_object_allocate(OBJT_DEFAULT,
3008 atop(old_entry->end - old_entry->start));
3009 old_entry->object.vm_object = object;
3010 old_entry->offset = 0;
3011 if (old_entry->uip != NULL) {
3012 object->uip = old_entry->uip;
3013 object->charge = old_entry->end -
3015 old_entry->uip = NULL;
3020 * Add the reference before calling vm_object_shadow
3021 * to insure that a shadow object is created.
3023 vm_object_reference(object);
3024 if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3025 vm_object_shadow(&old_entry->object.vm_object,
3027 atop(old_entry->end - old_entry->start));
3028 old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
3029 /* Transfer the second reference too. */
3030 vm_object_reference(
3031 old_entry->object.vm_object);
3034 * As in vm_map_simplify_entry(), the
3035 * vnode lock will not be acquired in
3036 * this call to vm_object_deallocate().
3038 vm_object_deallocate(object);
3039 object = old_entry->object.vm_object;
3041 VM_OBJECT_LOCK(object);
3042 vm_object_clear_flag(object, OBJ_ONEMAPPING);
3043 if (old_entry->uip != NULL) {
3044 KASSERT(object->uip == NULL, ("vmspace_fork both uip"));
3045 object->uip = old_entry->uip;
3046 object->charge = old_entry->end - old_entry->start;
3047 old_entry->uip = NULL;
3049 VM_OBJECT_UNLOCK(object);
3052 * Clone the entry, referencing the shared object.
3054 new_entry = vm_map_entry_create(new_map);
3055 *new_entry = *old_entry;
3056 new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
3057 MAP_ENTRY_IN_TRANSITION);
3058 new_entry->wired_count = 0;
3061 * Insert the entry into the new map -- we know we're
3062 * inserting at the end of the new map.
3064 vm_map_entry_link(new_map, new_map->header.prev,
3066 vmspace_map_entry_forked(vm1, vm2, new_entry);
3069 * Update the physical map
3071 pmap_copy(new_map->pmap, old_map->pmap,
3073 (old_entry->end - old_entry->start),
3077 case VM_INHERIT_COPY:
3079 * Clone the entry and link into the map.
3081 new_entry = vm_map_entry_create(new_map);
3082 *new_entry = *old_entry;
3083 new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
3084 MAP_ENTRY_IN_TRANSITION);
3085 new_entry->wired_count = 0;
3086 new_entry->object.vm_object = NULL;
3087 new_entry->uip = NULL;
3088 vm_map_entry_link(new_map, new_map->header.prev,
3090 vmspace_map_entry_forked(vm1, vm2, new_entry);
3091 vm_map_copy_entry(old_map, new_map, old_entry,
3092 new_entry, fork_charge);
3095 old_entry = old_entry->next;
3098 vm_map_unlock(old_map);
3100 vm_map_unlock(new_map);
3106 vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
3107 vm_prot_t prot, vm_prot_t max, int cow)
3109 vm_map_entry_t new_entry, prev_entry;
3110 vm_offset_t bot, top;
3111 vm_size_t init_ssize;
3116 * The stack orientation is piggybacked with the cow argument.
3117 * Extract it into orient and mask the cow argument so that we
3118 * don't pass it around further.
3119 * NOTE: We explicitly allow bi-directional stacks.
3121 orient = cow & (MAP_STACK_GROWS_DOWN|MAP_STACK_GROWS_UP);
3123 KASSERT(orient != 0, ("No stack grow direction"));
3125 if (addrbos < vm_map_min(map) ||
3126 addrbos > vm_map_max(map) ||
3127 addrbos + max_ssize < addrbos)
3128 return (KERN_NO_SPACE);
3130 init_ssize = (max_ssize < sgrowsiz) ? max_ssize : sgrowsiz;
3132 PROC_LOCK(curthread->td_proc);
3133 vmemlim = lim_cur(curthread->td_proc, RLIMIT_VMEM);
3134 PROC_UNLOCK(curthread->td_proc);
3138 /* If addr is already mapped, no go */
3139 if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
3141 return (KERN_NO_SPACE);
3144 /* If we would blow our VMEM resource limit, no go */
3145 if (map->size + init_ssize > vmemlim) {
3147 return (KERN_NO_SPACE);
3151 * If we can't accomodate max_ssize in the current mapping, no go.
3152 * However, we need to be aware that subsequent user mappings might
3153 * map into the space we have reserved for stack, and currently this
3154 * space is not protected.
3156 * Hopefully we will at least detect this condition when we try to
3159 if ((prev_entry->next != &map->header) &&
3160 (prev_entry->next->start < addrbos + max_ssize)) {
3162 return (KERN_NO_SPACE);
3166 * We initially map a stack of only init_ssize. We will grow as
3167 * needed later. Depending on the orientation of the stack (i.e.
3168 * the grow direction) we either map at the top of the range, the
3169 * bottom of the range or in the middle.
3171 * Note: we would normally expect prot and max to be VM_PROT_ALL,
3172 * and cow to be 0. Possibly we should eliminate these as input
3173 * parameters, and just pass these values here in the insert call.
3175 if (orient == MAP_STACK_GROWS_DOWN)
3176 bot = addrbos + max_ssize - init_ssize;
3177 else if (orient == MAP_STACK_GROWS_UP)
3180 bot = round_page(addrbos + max_ssize/2 - init_ssize/2);
3181 top = bot + init_ssize;
3182 rv = vm_map_insert(map, NULL, 0, bot, top, prot, max, cow);
3184 /* Now set the avail_ssize amount. */
3185 if (rv == KERN_SUCCESS) {
3186 if (prev_entry != &map->header)
3187 vm_map_clip_end(map, prev_entry, bot);
3188 new_entry = prev_entry->next;
3189 if (new_entry->end != top || new_entry->start != bot)
3190 panic("Bad entry start/end for new stack entry");
3192 new_entry->avail_ssize = max_ssize - init_ssize;
3193 if (orient & MAP_STACK_GROWS_DOWN)
3194 new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
3195 if (orient & MAP_STACK_GROWS_UP)
3196 new_entry->eflags |= MAP_ENTRY_GROWS_UP;
3203 /* Attempts to grow a vm stack entry. Returns KERN_SUCCESS if the
3204 * desired address is already mapped, or if we successfully grow
3205 * the stack. Also returns KERN_SUCCESS if addr is outside the
3206 * stack range (this is strange, but preserves compatibility with
3207 * the grow function in vm_machdep.c).
3210 vm_map_growstack(struct proc *p, vm_offset_t addr)
3212 vm_map_entry_t next_entry, prev_entry;
3213 vm_map_entry_t new_entry, stack_entry;
3214 struct vmspace *vm = p->p_vmspace;
3215 vm_map_t map = &vm->vm_map;
3217 size_t grow_amount, max_grow;
3218 rlim_t stacklim, vmemlim;
3219 int is_procstack, rv;
3220 struct uidinfo *uip;
3224 stacklim = lim_cur(p, RLIMIT_STACK);
3225 vmemlim = lim_cur(p, RLIMIT_VMEM);
3228 vm_map_lock_read(map);
3230 /* If addr is already in the entry range, no need to grow.*/
3231 if (vm_map_lookup_entry(map, addr, &prev_entry)) {
3232 vm_map_unlock_read(map);
3233 return (KERN_SUCCESS);
3236 next_entry = prev_entry->next;
3237 if (!(prev_entry->eflags & MAP_ENTRY_GROWS_UP)) {
3239 * This entry does not grow upwards. Since the address lies
3240 * beyond this entry, the next entry (if one exists) has to
3241 * be a downward growable entry. The entry list header is
3242 * never a growable entry, so it suffices to check the flags.
3244 if (!(next_entry->eflags & MAP_ENTRY_GROWS_DOWN)) {
3245 vm_map_unlock_read(map);
3246 return (KERN_SUCCESS);
3248 stack_entry = next_entry;
3251 * This entry grows upward. If the next entry does not at
3252 * least grow downwards, this is the entry we need to grow.
3253 * otherwise we have two possible choices and we have to
3256 if (next_entry->eflags & MAP_ENTRY_GROWS_DOWN) {
3258 * We have two choices; grow the entry closest to
3259 * the address to minimize the amount of growth.
3261 if (addr - prev_entry->end <= next_entry->start - addr)
3262 stack_entry = prev_entry;
3264 stack_entry = next_entry;
3266 stack_entry = prev_entry;
3269 if (stack_entry == next_entry) {
3270 KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_DOWN, ("foo"));
3271 KASSERT(addr < stack_entry->start, ("foo"));
3272 end = (prev_entry != &map->header) ? prev_entry->end :
3273 stack_entry->start - stack_entry->avail_ssize;
3274 grow_amount = roundup(stack_entry->start - addr, PAGE_SIZE);
3275 max_grow = stack_entry->start - end;
3277 KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_UP, ("foo"));
3278 KASSERT(addr >= stack_entry->end, ("foo"));
3279 end = (next_entry != &map->header) ? next_entry->start :
3280 stack_entry->end + stack_entry->avail_ssize;
3281 grow_amount = roundup(addr + 1 - stack_entry->end, PAGE_SIZE);
3282 max_grow = end - stack_entry->end;
3285 if (grow_amount > stack_entry->avail_ssize) {
3286 vm_map_unlock_read(map);
3287 return (KERN_NO_SPACE);
3291 * If there is no longer enough space between the entries nogo, and
3292 * adjust the available space. Note: this should only happen if the
3293 * user has mapped into the stack area after the stack was created,
3294 * and is probably an error.
3296 * This also effectively destroys any guard page the user might have
3297 * intended by limiting the stack size.
3299 if (grow_amount > max_grow) {
3300 if (vm_map_lock_upgrade(map))
3303 stack_entry->avail_ssize = max_grow;
3306 return (KERN_NO_SPACE);
3309 is_procstack = (addr >= (vm_offset_t)vm->vm_maxsaddr) ? 1 : 0;
3312 * If this is the main process stack, see if we're over the stack
3315 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
3316 vm_map_unlock_read(map);
3317 return (KERN_NO_SPACE);
3320 /* Round up the grow amount modulo SGROWSIZ */
3321 grow_amount = roundup (grow_amount, sgrowsiz);
3322 if (grow_amount > stack_entry->avail_ssize)
3323 grow_amount = stack_entry->avail_ssize;
3324 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
3325 grow_amount = stacklim - ctob(vm->vm_ssize);
3328 /* If we would blow our VMEM resource limit, no go */
3329 if (map->size + grow_amount > vmemlim) {
3330 vm_map_unlock_read(map);
3331 return (KERN_NO_SPACE);
3334 if (vm_map_lock_upgrade(map))
3337 if (stack_entry == next_entry) {
3341 /* Get the preliminary new entry start value */
3342 addr = stack_entry->start - grow_amount;
3345 * If this puts us into the previous entry, cut back our
3346 * growth to the available space. Also, see the note above.
3349 stack_entry->avail_ssize = max_grow;
3353 rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start,
3354 p->p_sysent->sv_stackprot, VM_PROT_ALL, 0);
3356 /* Adjust the available stack space by the amount we grew. */
3357 if (rv == KERN_SUCCESS) {
3358 if (prev_entry != &map->header)
3359 vm_map_clip_end(map, prev_entry, addr);
3360 new_entry = prev_entry->next;
3361 KASSERT(new_entry == stack_entry->prev, ("foo"));
3362 KASSERT(new_entry->end == stack_entry->start, ("foo"));
3363 KASSERT(new_entry->start == addr, ("foo"));
3364 grow_amount = new_entry->end - new_entry->start;
3365 new_entry->avail_ssize = stack_entry->avail_ssize -
3367 stack_entry->eflags &= ~MAP_ENTRY_GROWS_DOWN;
3368 new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
3374 addr = stack_entry->end + grow_amount;
3377 * If this puts us into the next entry, cut back our growth
3378 * to the available space. Also, see the note above.
3381 stack_entry->avail_ssize = end - stack_entry->end;
3385 grow_amount = addr - stack_entry->end;
3386 uip = stack_entry->uip;
3387 if (uip == NULL && stack_entry->object.vm_object != NULL)
3388 uip = stack_entry->object.vm_object->uip;
3389 if (uip != NULL && !swap_reserve_by_uid(grow_amount, uip))
3391 /* Grow the underlying object if applicable. */
3392 else if (stack_entry->object.vm_object == NULL ||
3393 vm_object_coalesce(stack_entry->object.vm_object,
3394 stack_entry->offset,
3395 (vm_size_t)(stack_entry->end - stack_entry->start),
3396 (vm_size_t)grow_amount, uip != NULL)) {
3397 map->size += (addr - stack_entry->end);
3398 /* Update the current entry. */
3399 stack_entry->end = addr;
3400 stack_entry->avail_ssize -= grow_amount;
3401 vm_map_entry_resize_free(map, stack_entry);
3404 if (next_entry != &map->header)
3405 vm_map_clip_start(map, next_entry, addr);
3410 if (rv == KERN_SUCCESS && is_procstack)
3411 vm->vm_ssize += btoc(grow_amount);
3416 * Heed the MAP_WIREFUTURE flag if it was set for this process.
3418 if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE)) {
3420 (stack_entry == next_entry) ? addr : addr - grow_amount,
3421 (stack_entry == next_entry) ? stack_entry->start : addr,
3422 (p->p_flag & P_SYSTEM)
3423 ? VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES
3424 : VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
3431 * Unshare the specified VM space for exec. If other processes are
3432 * mapped to it, then create a new one. The new vmspace is null.
3435 vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser)
3437 struct vmspace *oldvmspace = p->p_vmspace;
3438 struct vmspace *newvmspace;
3440 newvmspace = vmspace_alloc(minuser, maxuser);
3441 if (newvmspace == NULL)
3443 newvmspace->vm_swrss = oldvmspace->vm_swrss;
3445 * This code is written like this for prototype purposes. The
3446 * goal is to avoid running down the vmspace here, but let the
3447 * other process's that are still using the vmspace to finally
3448 * run it down. Even though there is little or no chance of blocking
3449 * here, it is a good idea to keep this form for future mods.
3451 PROC_VMSPACE_LOCK(p);
3452 p->p_vmspace = newvmspace;
3453 PROC_VMSPACE_UNLOCK(p);
3454 if (p == curthread->td_proc)
3455 pmap_activate(curthread);
3456 vmspace_free(oldvmspace);
3461 * Unshare the specified VM space for forcing COW. This
3462 * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
3465 vmspace_unshare(struct proc *p)
3467 struct vmspace *oldvmspace = p->p_vmspace;
3468 struct vmspace *newvmspace;
3469 vm_ooffset_t fork_charge;
3471 if (oldvmspace->vm_refcnt == 1)
3474 newvmspace = vmspace_fork(oldvmspace, &fork_charge);
3475 if (newvmspace == NULL)
3477 if (!swap_reserve_by_uid(fork_charge, p->p_ucred->cr_ruidinfo)) {
3478 vmspace_free(newvmspace);
3481 PROC_VMSPACE_LOCK(p);
3482 p->p_vmspace = newvmspace;
3483 PROC_VMSPACE_UNLOCK(p);
3484 if (p == curthread->td_proc)
3485 pmap_activate(curthread);
3486 vmspace_free(oldvmspace);
3493 * Finds the VM object, offset, and
3494 * protection for a given virtual address in the
3495 * specified map, assuming a page fault of the
3498 * Leaves the map in question locked for read; return
3499 * values are guaranteed until a vm_map_lookup_done
3500 * call is performed. Note that the map argument
3501 * is in/out; the returned map must be used in
3502 * the call to vm_map_lookup_done.
3504 * A handle (out_entry) is returned for use in
3505 * vm_map_lookup_done, to make that fast.
3507 * If a lookup is requested with "write protection"
3508 * specified, the map may be changed to perform virtual
3509 * copying operations, although the data referenced will
3513 vm_map_lookup(vm_map_t *var_map, /* IN/OUT */
3515 vm_prot_t fault_typea,
3516 vm_map_entry_t *out_entry, /* OUT */
3517 vm_object_t *object, /* OUT */
3518 vm_pindex_t *pindex, /* OUT */
3519 vm_prot_t *out_prot, /* OUT */
3520 boolean_t *wired) /* OUT */
3522 vm_map_entry_t entry;
3523 vm_map_t map = *var_map;
3525 vm_prot_t fault_type = fault_typea;
3526 vm_object_t eobject;
3527 struct uidinfo *uip;
3532 vm_map_lock_read(map);
3535 * Lookup the faulting address.
3537 if (!vm_map_lookup_entry(map, vaddr, out_entry)) {
3538 vm_map_unlock_read(map);
3539 return (KERN_INVALID_ADDRESS);
3547 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3548 vm_map_t old_map = map;
3550 *var_map = map = entry->object.sub_map;
3551 vm_map_unlock_read(old_map);
3556 * Check whether this task is allowed to have this page.
3557 * Note the special case for MAP_ENTRY_COW
3558 * pages with an override. This is to implement a forced
3559 * COW for debuggers.
3561 if (fault_type & VM_PROT_OVERRIDE_WRITE)
3562 prot = entry->max_protection;
3564 prot = entry->protection;
3565 fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
3566 if ((fault_type & prot) != fault_type) {
3567 vm_map_unlock_read(map);
3568 return (KERN_PROTECTION_FAILURE);
3570 if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
3571 (entry->eflags & MAP_ENTRY_COW) &&
3572 (fault_type & VM_PROT_WRITE) &&
3573 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
3574 vm_map_unlock_read(map);
3575 return (KERN_PROTECTION_FAILURE);
3579 * If this page is not pageable, we have to get it for all possible
3582 *wired = (entry->wired_count != 0);
3584 prot = fault_type = entry->protection;
3585 size = entry->end - entry->start;
3587 * If the entry was copy-on-write, we either ...
3589 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3591 * If we want to write the page, we may as well handle that
3592 * now since we've got the map locked.
3594 * If we don't need to write the page, we just demote the
3595 * permissions allowed.
3597 if (fault_type & VM_PROT_WRITE) {
3599 * Make a new object, and place it in the object
3600 * chain. Note that no new references have appeared
3601 * -- one just moved from the map to the new
3604 if (vm_map_lock_upgrade(map))
3607 if (entry->uip == NULL) {
3609 * The debugger owner is charged for
3612 uip = curthread->td_ucred->cr_ruidinfo;
3614 if (!swap_reserve_by_uid(size, uip)) {
3617 return (KERN_RESOURCE_SHORTAGE);
3622 &entry->object.vm_object,
3625 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
3626 eobject = entry->object.vm_object;
3627 if (eobject->uip != NULL) {
3629 * The object was not shadowed.
3631 swap_release_by_uid(size, entry->uip);
3634 } else if (entry->uip != NULL) {
3635 VM_OBJECT_LOCK(eobject);
3636 eobject->uip = entry->uip;
3637 eobject->charge = size;
3638 VM_OBJECT_UNLOCK(eobject);
3642 vm_map_lock_downgrade(map);
3645 * We're attempting to read a copy-on-write page --
3646 * don't allow writes.
3648 prot &= ~VM_PROT_WRITE;
3653 * Create an object if necessary.
3655 if (entry->object.vm_object == NULL &&
3657 if (vm_map_lock_upgrade(map))
3659 entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
3662 if (entry->uip != NULL) {
3663 VM_OBJECT_LOCK(entry->object.vm_object);
3664 entry->object.vm_object->uip = entry->uip;
3665 entry->object.vm_object->charge = size;
3666 VM_OBJECT_UNLOCK(entry->object.vm_object);
3669 vm_map_lock_downgrade(map);
3673 * Return the object/offset from this entry. If the entry was
3674 * copy-on-write or empty, it has been fixed up.
3676 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
3677 *object = entry->object.vm_object;
3680 return (KERN_SUCCESS);
3684 * vm_map_lookup_locked:
3686 * Lookup the faulting address. A version of vm_map_lookup that returns
3687 * KERN_FAILURE instead of blocking on map lock or memory allocation.
3690 vm_map_lookup_locked(vm_map_t *var_map, /* IN/OUT */
3692 vm_prot_t fault_typea,
3693 vm_map_entry_t *out_entry, /* OUT */
3694 vm_object_t *object, /* OUT */
3695 vm_pindex_t *pindex, /* OUT */
3696 vm_prot_t *out_prot, /* OUT */
3697 boolean_t *wired) /* OUT */
3699 vm_map_entry_t entry;
3700 vm_map_t map = *var_map;
3702 vm_prot_t fault_type = fault_typea;
3705 * Lookup the faulting address.
3707 if (!vm_map_lookup_entry(map, vaddr, out_entry))
3708 return (KERN_INVALID_ADDRESS);
3713 * Fail if the entry refers to a submap.
3715 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
3716 return (KERN_FAILURE);
3719 * Check whether this task is allowed to have this page.
3720 * Note the special case for MAP_ENTRY_COW
3721 * pages with an override. This is to implement a forced
3722 * COW for debuggers.
3724 if (fault_type & VM_PROT_OVERRIDE_WRITE)
3725 prot = entry->max_protection;
3727 prot = entry->protection;
3728 fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
3729 if ((fault_type & prot) != fault_type)
3730 return (KERN_PROTECTION_FAILURE);
3731 if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
3732 (entry->eflags & MAP_ENTRY_COW) &&
3733 (fault_type & VM_PROT_WRITE) &&
3734 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0)
3735 return (KERN_PROTECTION_FAILURE);
3738 * If this page is not pageable, we have to get it for all possible
3741 *wired = (entry->wired_count != 0);
3743 prot = fault_type = entry->protection;
3745 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3747 * Fail if the entry was copy-on-write for a write fault.
3749 if (fault_type & VM_PROT_WRITE)
3750 return (KERN_FAILURE);
3752 * We're attempting to read a copy-on-write page --
3753 * don't allow writes.
3755 prot &= ~VM_PROT_WRITE;
3759 * Fail if an object should be created.
3761 if (entry->object.vm_object == NULL && !map->system_map)
3762 return (KERN_FAILURE);
3765 * Return the object/offset from this entry. If the entry was
3766 * copy-on-write or empty, it has been fixed up.
3768 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
3769 *object = entry->object.vm_object;
3772 return (KERN_SUCCESS);
3776 * vm_map_lookup_done:
3778 * Releases locks acquired by a vm_map_lookup
3779 * (according to the handle returned by that lookup).
3782 vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry)
3785 * Unlock the main-level map
3787 vm_map_unlock_read(map);
3790 #include "opt_ddb.h"
3792 #include <sys/kernel.h>
3794 #include <ddb/ddb.h>
3797 * vm_map_print: [ debug ]
3799 DB_SHOW_COMMAND(map, vm_map_print)
3802 /* XXX convert args. */
3803 vm_map_t map = (vm_map_t)addr;
3804 boolean_t full = have_addr;
3806 vm_map_entry_t entry;
3808 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
3810 (void *)map->pmap, map->nentries, map->timestamp);
3813 if (!full && db_indent)
3817 for (entry = map->header.next; entry != &map->header;
3818 entry = entry->next) {
3819 db_iprintf("map entry %p: start=%p, end=%p\n",
3820 (void *)entry, (void *)entry->start, (void *)entry->end);
3823 static char *inheritance_name[4] =
3824 {"share", "copy", "none", "donate_copy"};
3826 db_iprintf(" prot=%x/%x/%s",
3828 entry->max_protection,
3829 inheritance_name[(int)(unsigned char)entry->inheritance]);
3830 if (entry->wired_count != 0)
3831 db_printf(", wired");
3833 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3834 db_printf(", share=%p, offset=0x%jx\n",
3835 (void *)entry->object.sub_map,
3836 (uintmax_t)entry->offset);
3838 if ((entry->prev == &map->header) ||
3839 (entry->prev->object.sub_map !=
3840 entry->object.sub_map)) {
3842 vm_map_print((db_expr_t)(intptr_t)
3843 entry->object.sub_map,
3844 full, 0, (char *)0);
3848 if (entry->uip != NULL)
3849 db_printf(", uip %d", entry->uip->ui_uid);
3850 db_printf(", object=%p, offset=0x%jx",
3851 (void *)entry->object.vm_object,
3852 (uintmax_t)entry->offset);
3853 if (entry->object.vm_object && entry->object.vm_object->uip)
3854 db_printf(", obj uip %d charge %jx",
3855 entry->object.vm_object->uip->ui_uid,
3856 (uintmax_t)entry->object.vm_object->charge);
3857 if (entry->eflags & MAP_ENTRY_COW)
3858 db_printf(", copy (%s)",
3859 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
3863 if ((entry->prev == &map->header) ||
3864 (entry->prev->object.vm_object !=
3865 entry->object.vm_object)) {
3867 vm_object_print((db_expr_t)(intptr_t)
3868 entry->object.vm_object,
3869 full, 0, (char *)0);
3881 DB_SHOW_COMMAND(procvm, procvm)
3886 p = (struct proc *) addr;
3891 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
3892 (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
3893 (void *)vmspace_pmap(p->p_vmspace));
3895 vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);