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 p = vm_page_find_least(object, pindex);
1745 * Assert: the variable p is either (1) the page with the
1746 * least pindex greater than or equal to the parameter pindex
1750 p != NULL && (tmpidx = p->pindex - pindex) < psize;
1751 p = TAILQ_NEXT(p, listq)) {
1753 * don't allow an madvise to blow away our really
1754 * free pages allocating pv entries.
1756 if ((flags & MAP_PREFAULT_MADVISE) &&
1757 cnt.v_free_count < cnt.v_free_reserved) {
1761 if (p->valid == VM_PAGE_BITS_ALL) {
1762 if (p_start == NULL) {
1763 start = addr + ptoa(tmpidx);
1766 } else if (p_start != NULL) {
1767 if (!are_queues_locked) {
1768 are_queues_locked = TRUE;
1769 vm_page_lock_queues();
1771 pmap_enter_object(map->pmap, start, addr +
1772 ptoa(tmpidx), p_start, prot);
1776 if (p_start != NULL) {
1777 if (!are_queues_locked) {
1778 are_queues_locked = TRUE;
1779 vm_page_lock_queues();
1781 pmap_enter_object(map->pmap, start, addr + ptoa(psize),
1784 if (are_queues_locked)
1785 vm_page_unlock_queues();
1787 VM_OBJECT_UNLOCK(object);
1793 * Sets the protection of the specified address
1794 * region in the target map. If "set_max" is
1795 * specified, the maximum protection is to be set;
1796 * otherwise, only the current protection is affected.
1799 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
1800 vm_prot_t new_prot, boolean_t set_max)
1802 vm_map_entry_t current, entry;
1804 struct uidinfo *uip;
1809 VM_MAP_RANGE_CHECK(map, start, end);
1811 if (vm_map_lookup_entry(map, start, &entry)) {
1812 vm_map_clip_start(map, entry, start);
1814 entry = entry->next;
1818 * Make a first pass to check for protection violations.
1821 while ((current != &map->header) && (current->start < end)) {
1822 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1824 return (KERN_INVALID_ARGUMENT);
1826 if ((new_prot & current->max_protection) != new_prot) {
1828 return (KERN_PROTECTION_FAILURE);
1830 current = current->next;
1835 * Do an accounting pass for private read-only mappings that
1836 * now will do cow due to allowed write (e.g. debugger sets
1837 * breakpoint on text segment)
1839 for (current = entry; (current != &map->header) &&
1840 (current->start < end); current = current->next) {
1842 vm_map_clip_end(map, current, end);
1845 ((new_prot & ~(current->protection)) & VM_PROT_WRITE) == 0 ||
1846 ENTRY_CHARGED(current)) {
1850 uip = curthread->td_ucred->cr_ruidinfo;
1851 obj = current->object.vm_object;
1853 if (obj == NULL || (current->eflags & MAP_ENTRY_NEEDS_COPY)) {
1854 if (!swap_reserve(current->end - current->start)) {
1856 return (KERN_RESOURCE_SHORTAGE);
1863 VM_OBJECT_LOCK(obj);
1864 if (obj->type != OBJT_DEFAULT && obj->type != OBJT_SWAP) {
1865 VM_OBJECT_UNLOCK(obj);
1870 * Charge for the whole object allocation now, since
1871 * we cannot distinguish between non-charged and
1872 * charged clipped mapping of the same object later.
1874 KASSERT(obj->charge == 0,
1875 ("vm_map_protect: object %p overcharged\n", obj));
1876 if (!swap_reserve(ptoa(obj->size))) {
1877 VM_OBJECT_UNLOCK(obj);
1879 return (KERN_RESOURCE_SHORTAGE);
1884 obj->charge = ptoa(obj->size);
1885 VM_OBJECT_UNLOCK(obj);
1889 * Go back and fix up protections. [Note that clipping is not
1890 * necessary the second time.]
1893 while ((current != &map->header) && (current->start < end)) {
1894 old_prot = current->protection;
1897 current->protection =
1898 (current->max_protection = new_prot) &
1901 current->protection = new_prot;
1903 if ((current->eflags & (MAP_ENTRY_COW | MAP_ENTRY_USER_WIRED))
1904 == (MAP_ENTRY_COW | MAP_ENTRY_USER_WIRED) &&
1905 (current->protection & VM_PROT_WRITE) != 0 &&
1906 (old_prot & VM_PROT_WRITE) == 0) {
1907 vm_fault_copy_entry(map, map, current, current, NULL);
1911 * Update physical map if necessary. Worry about copy-on-write
1914 if (current->protection != old_prot) {
1915 #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1917 pmap_protect(map->pmap, current->start,
1919 current->protection & MASK(current));
1922 vm_map_simplify_entry(map, current);
1923 current = current->next;
1926 return (KERN_SUCCESS);
1932 * This routine traverses a processes map handling the madvise
1933 * system call. Advisories are classified as either those effecting
1934 * the vm_map_entry structure, or those effecting the underlying
1944 vm_map_entry_t current, entry;
1948 * Some madvise calls directly modify the vm_map_entry, in which case
1949 * we need to use an exclusive lock on the map and we need to perform
1950 * various clipping operations. Otherwise we only need a read-lock
1955 case MADV_SEQUENTIAL:
1967 vm_map_lock_read(map);
1970 return (KERN_INVALID_ARGUMENT);
1974 * Locate starting entry and clip if necessary.
1976 VM_MAP_RANGE_CHECK(map, start, end);
1978 if (vm_map_lookup_entry(map, start, &entry)) {
1980 vm_map_clip_start(map, entry, start);
1982 entry = entry->next;
1987 * madvise behaviors that are implemented in the vm_map_entry.
1989 * We clip the vm_map_entry so that behavioral changes are
1990 * limited to the specified address range.
1992 for (current = entry;
1993 (current != &map->header) && (current->start < end);
1994 current = current->next
1996 if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1999 vm_map_clip_end(map, current, end);
2003 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
2005 case MADV_SEQUENTIAL:
2006 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
2009 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
2012 current->eflags |= MAP_ENTRY_NOSYNC;
2015 current->eflags &= ~MAP_ENTRY_NOSYNC;
2018 current->eflags |= MAP_ENTRY_NOCOREDUMP;
2021 current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
2026 vm_map_simplify_entry(map, current);
2034 * madvise behaviors that are implemented in the underlying
2037 * Since we don't clip the vm_map_entry, we have to clip
2038 * the vm_object pindex and count.
2040 for (current = entry;
2041 (current != &map->header) && (current->start < end);
2042 current = current->next
2044 vm_offset_t useStart;
2046 if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
2049 pindex = OFF_TO_IDX(current->offset);
2050 count = atop(current->end - current->start);
2051 useStart = current->start;
2053 if (current->start < start) {
2054 pindex += atop(start - current->start);
2055 count -= atop(start - current->start);
2058 if (current->end > end)
2059 count -= atop(current->end - end);
2064 vm_object_madvise(current->object.vm_object,
2065 pindex, count, behav);
2066 if (behav == MADV_WILLNEED) {
2067 vm_map_pmap_enter(map,
2069 current->protection,
2070 current->object.vm_object,
2072 (count << PAGE_SHIFT),
2073 MAP_PREFAULT_MADVISE
2077 vm_map_unlock_read(map);
2086 * Sets the inheritance of the specified address
2087 * range in the target map. Inheritance
2088 * affects how the map will be shared with
2089 * child maps at the time of vmspace_fork.
2092 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
2093 vm_inherit_t new_inheritance)
2095 vm_map_entry_t entry;
2096 vm_map_entry_t temp_entry;
2098 switch (new_inheritance) {
2099 case VM_INHERIT_NONE:
2100 case VM_INHERIT_COPY:
2101 case VM_INHERIT_SHARE:
2104 return (KERN_INVALID_ARGUMENT);
2107 VM_MAP_RANGE_CHECK(map, start, end);
2108 if (vm_map_lookup_entry(map, start, &temp_entry)) {
2110 vm_map_clip_start(map, entry, start);
2112 entry = temp_entry->next;
2113 while ((entry != &map->header) && (entry->start < end)) {
2114 vm_map_clip_end(map, entry, end);
2115 entry->inheritance = new_inheritance;
2116 vm_map_simplify_entry(map, entry);
2117 entry = entry->next;
2120 return (KERN_SUCCESS);
2126 * Implements both kernel and user unwiring.
2129 vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
2132 vm_map_entry_t entry, first_entry, tmp_entry;
2133 vm_offset_t saved_start;
2134 unsigned int last_timestamp;
2136 boolean_t need_wakeup, result, user_unwire;
2138 user_unwire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
2140 VM_MAP_RANGE_CHECK(map, start, end);
2141 if (!vm_map_lookup_entry(map, start, &first_entry)) {
2142 if (flags & VM_MAP_WIRE_HOLESOK)
2143 first_entry = first_entry->next;
2146 return (KERN_INVALID_ADDRESS);
2149 last_timestamp = map->timestamp;
2150 entry = first_entry;
2151 while (entry != &map->header && entry->start < end) {
2152 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
2154 * We have not yet clipped the entry.
2156 saved_start = (start >= entry->start) ? start :
2158 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2159 if (vm_map_unlock_and_wait(map, 0)) {
2161 * Allow interruption of user unwiring?
2165 if (last_timestamp+1 != map->timestamp) {
2167 * Look again for the entry because the map was
2168 * modified while it was unlocked.
2169 * Specifically, the entry may have been
2170 * clipped, merged, or deleted.
2172 if (!vm_map_lookup_entry(map, saved_start,
2174 if (flags & VM_MAP_WIRE_HOLESOK)
2175 tmp_entry = tmp_entry->next;
2177 if (saved_start == start) {
2179 * First_entry has been deleted.
2182 return (KERN_INVALID_ADDRESS);
2185 rv = KERN_INVALID_ADDRESS;
2189 if (entry == first_entry)
2190 first_entry = tmp_entry;
2195 last_timestamp = map->timestamp;
2198 vm_map_clip_start(map, entry, start);
2199 vm_map_clip_end(map, entry, end);
2201 * Mark the entry in case the map lock is released. (See
2204 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
2206 * Check the map for holes in the specified region.
2207 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
2209 if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
2210 (entry->end < end && (entry->next == &map->header ||
2211 entry->next->start > entry->end))) {
2213 rv = KERN_INVALID_ADDRESS;
2217 * If system unwiring, require that the entry is system wired.
2220 vm_map_entry_system_wired_count(entry) == 0) {
2222 rv = KERN_INVALID_ARGUMENT;
2225 entry = entry->next;
2229 need_wakeup = FALSE;
2230 if (first_entry == NULL) {
2231 result = vm_map_lookup_entry(map, start, &first_entry);
2232 if (!result && (flags & VM_MAP_WIRE_HOLESOK))
2233 first_entry = first_entry->next;
2235 KASSERT(result, ("vm_map_unwire: lookup failed"));
2237 entry = first_entry;
2238 while (entry != &map->header && entry->start < end) {
2239 if (rv == KERN_SUCCESS && (!user_unwire ||
2240 (entry->eflags & MAP_ENTRY_USER_WIRED))) {
2242 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2243 entry->wired_count--;
2244 if (entry->wired_count == 0) {
2246 * Retain the map lock.
2248 vm_fault_unwire(map, entry->start, entry->end,
2249 entry->object.vm_object != NULL &&
2250 (entry->object.vm_object->type == OBJT_DEVICE ||
2251 entry->object.vm_object->type == OBJT_SG));
2254 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
2255 ("vm_map_unwire: in-transition flag missing"));
2256 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
2257 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
2258 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
2261 vm_map_simplify_entry(map, entry);
2262 entry = entry->next;
2273 * Implements both kernel and user wiring.
2276 vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
2279 vm_map_entry_t entry, first_entry, tmp_entry;
2280 vm_offset_t saved_end, saved_start;
2281 unsigned int last_timestamp;
2283 boolean_t fictitious, need_wakeup, result, user_wire;
2285 user_wire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
2287 VM_MAP_RANGE_CHECK(map, start, end);
2288 if (!vm_map_lookup_entry(map, start, &first_entry)) {
2289 if (flags & VM_MAP_WIRE_HOLESOK)
2290 first_entry = first_entry->next;
2293 return (KERN_INVALID_ADDRESS);
2296 last_timestamp = map->timestamp;
2297 entry = first_entry;
2298 while (entry != &map->header && entry->start < end) {
2299 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
2301 * We have not yet clipped the entry.
2303 saved_start = (start >= entry->start) ? start :
2305 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2306 if (vm_map_unlock_and_wait(map, 0)) {
2308 * Allow interruption of user wiring?
2312 if (last_timestamp + 1 != map->timestamp) {
2314 * Look again for the entry because the map was
2315 * modified while it was unlocked.
2316 * Specifically, the entry may have been
2317 * clipped, merged, or deleted.
2319 if (!vm_map_lookup_entry(map, saved_start,
2321 if (flags & VM_MAP_WIRE_HOLESOK)
2322 tmp_entry = tmp_entry->next;
2324 if (saved_start == start) {
2326 * first_entry has been deleted.
2329 return (KERN_INVALID_ADDRESS);
2332 rv = KERN_INVALID_ADDRESS;
2336 if (entry == first_entry)
2337 first_entry = tmp_entry;
2342 last_timestamp = map->timestamp;
2345 vm_map_clip_start(map, entry, start);
2346 vm_map_clip_end(map, entry, end);
2348 * Mark the entry in case the map lock is released. (See
2351 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
2355 if (entry->wired_count == 0) {
2356 if ((entry->protection & (VM_PROT_READ|VM_PROT_EXECUTE))
2358 entry->eflags |= MAP_ENTRY_WIRE_SKIPPED;
2359 if ((flags & VM_MAP_WIRE_HOLESOK) == 0) {
2361 rv = KERN_INVALID_ADDRESS;
2366 entry->wired_count++;
2367 saved_start = entry->start;
2368 saved_end = entry->end;
2369 fictitious = entry->object.vm_object != NULL &&
2370 (entry->object.vm_object->type == OBJT_DEVICE ||
2371 entry->object.vm_object->type == OBJT_SG);
2373 * Release the map lock, relying on the in-transition
2377 rv = vm_fault_wire(map, saved_start, saved_end,
2378 user_wire, fictitious);
2380 if (last_timestamp + 1 != map->timestamp) {
2382 * Look again for the entry because the map was
2383 * modified while it was unlocked. The entry
2384 * may have been clipped, but NOT merged or
2387 result = vm_map_lookup_entry(map, saved_start,
2389 KASSERT(result, ("vm_map_wire: lookup failed"));
2390 if (entry == first_entry)
2391 first_entry = tmp_entry;
2395 while (entry->end < saved_end) {
2396 if (rv != KERN_SUCCESS) {
2397 KASSERT(entry->wired_count == 1,
2398 ("vm_map_wire: bad count"));
2399 entry->wired_count = -1;
2401 entry = entry->next;
2404 last_timestamp = map->timestamp;
2405 if (rv != KERN_SUCCESS) {
2406 KASSERT(entry->wired_count == 1,
2407 ("vm_map_wire: bad count"));
2409 * Assign an out-of-range value to represent
2410 * the failure to wire this entry.
2412 entry->wired_count = -1;
2416 } else if (!user_wire ||
2417 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
2418 entry->wired_count++;
2421 * Check the map for holes in the specified region.
2422 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
2425 if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
2426 (entry->end < end && (entry->next == &map->header ||
2427 entry->next->start > entry->end))) {
2429 rv = KERN_INVALID_ADDRESS;
2432 entry = entry->next;
2436 need_wakeup = FALSE;
2437 if (first_entry == NULL) {
2438 result = vm_map_lookup_entry(map, start, &first_entry);
2439 if (!result && (flags & VM_MAP_WIRE_HOLESOK))
2440 first_entry = first_entry->next;
2442 KASSERT(result, ("vm_map_wire: lookup failed"));
2444 entry = first_entry;
2445 while (entry != &map->header && entry->start < end) {
2446 if ((entry->eflags & MAP_ENTRY_WIRE_SKIPPED) != 0)
2447 goto next_entry_done;
2448 if (rv == KERN_SUCCESS) {
2450 entry->eflags |= MAP_ENTRY_USER_WIRED;
2451 } else if (entry->wired_count == -1) {
2453 * Wiring failed on this entry. Thus, unwiring is
2456 entry->wired_count = 0;
2459 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0)
2460 entry->wired_count--;
2461 if (entry->wired_count == 0) {
2463 * Retain the map lock.
2465 vm_fault_unwire(map, entry->start, entry->end,
2466 entry->object.vm_object != NULL &&
2467 (entry->object.vm_object->type == OBJT_DEVICE ||
2468 entry->object.vm_object->type == OBJT_SG));
2472 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
2473 ("vm_map_wire: in-transition flag missing"));
2474 entry->eflags &= ~(MAP_ENTRY_IN_TRANSITION|MAP_ENTRY_WIRE_SKIPPED);
2475 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
2476 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
2479 vm_map_simplify_entry(map, entry);
2480 entry = entry->next;
2491 * Push any dirty cached pages in the address range to their pager.
2492 * If syncio is TRUE, dirty pages are written synchronously.
2493 * If invalidate is TRUE, any cached pages are freed as well.
2495 * If the size of the region from start to end is zero, we are
2496 * supposed to flush all modified pages within the region containing
2497 * start. Unfortunately, a region can be split or coalesced with
2498 * neighboring regions, making it difficult to determine what the
2499 * original region was. Therefore, we approximate this requirement by
2500 * flushing the current region containing start.
2502 * Returns an error if any part of the specified range is not mapped.
2510 boolean_t invalidate)
2512 vm_map_entry_t current;
2513 vm_map_entry_t entry;
2516 vm_ooffset_t offset;
2517 unsigned int last_timestamp;
2519 vm_map_lock_read(map);
2520 VM_MAP_RANGE_CHECK(map, start, end);
2521 if (!vm_map_lookup_entry(map, start, &entry)) {
2522 vm_map_unlock_read(map);
2523 return (KERN_INVALID_ADDRESS);
2524 } else if (start == end) {
2525 start = entry->start;
2529 * Make a first pass to check for user-wired memory and holes.
2531 for (current = entry; current != &map->header && current->start < end;
2532 current = current->next) {
2533 if (invalidate && (current->eflags & MAP_ENTRY_USER_WIRED)) {
2534 vm_map_unlock_read(map);
2535 return (KERN_INVALID_ARGUMENT);
2537 if (end > current->end &&
2538 (current->next == &map->header ||
2539 current->end != current->next->start)) {
2540 vm_map_unlock_read(map);
2541 return (KERN_INVALID_ADDRESS);
2546 pmap_remove(map->pmap, start, end);
2549 * Make a second pass, cleaning/uncaching pages from the indicated
2552 for (current = entry; current != &map->header && current->start < end;) {
2553 offset = current->offset + (start - current->start);
2554 size = (end <= current->end ? end : current->end) - start;
2555 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
2557 vm_map_entry_t tentry;
2560 smap = current->object.sub_map;
2561 vm_map_lock_read(smap);
2562 (void) vm_map_lookup_entry(smap, offset, &tentry);
2563 tsize = tentry->end - offset;
2566 object = tentry->object.vm_object;
2567 offset = tentry->offset + (offset - tentry->start);
2568 vm_map_unlock_read(smap);
2570 object = current->object.vm_object;
2572 vm_object_reference(object);
2573 last_timestamp = map->timestamp;
2574 vm_map_unlock_read(map);
2575 vm_object_sync(object, offset, size, syncio, invalidate);
2577 vm_object_deallocate(object);
2578 vm_map_lock_read(map);
2579 if (last_timestamp == map->timestamp ||
2580 !vm_map_lookup_entry(map, start, ¤t))
2581 current = current->next;
2584 vm_map_unlock_read(map);
2585 return (KERN_SUCCESS);
2589 * vm_map_entry_unwire: [ internal use only ]
2591 * Make the region specified by this entry pageable.
2593 * The map in question should be locked.
2594 * [This is the reason for this routine's existence.]
2597 vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
2599 vm_fault_unwire(map, entry->start, entry->end,
2600 entry->object.vm_object != NULL &&
2601 (entry->object.vm_object->type == OBJT_DEVICE ||
2602 entry->object.vm_object->type == OBJT_SG));
2603 entry->wired_count = 0;
2607 * vm_map_entry_delete: [ internal use only ]
2609 * Deallocate the given entry from the target map.
2612 vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry)
2615 vm_pindex_t offidxstart, offidxend, count, size1;
2618 vm_map_entry_unlink(map, entry);
2619 object = entry->object.vm_object;
2620 size = entry->end - entry->start;
2623 if (entry->uip != NULL) {
2624 swap_release_by_uid(size, entry->uip);
2628 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
2630 KASSERT(entry->uip == NULL || object->uip == NULL ||
2631 (entry->eflags & MAP_ENTRY_NEEDS_COPY),
2632 ("OVERCOMMIT vm_map_entry_delete: both uip %p", entry));
2633 count = OFF_TO_IDX(size);
2634 offidxstart = OFF_TO_IDX(entry->offset);
2635 offidxend = offidxstart + count;
2636 VM_OBJECT_LOCK(object);
2637 if (object->ref_count != 1 &&
2638 ((object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING ||
2639 object == kernel_object || object == kmem_object)) {
2640 vm_object_collapse(object);
2641 vm_object_page_remove(object, offidxstart, offidxend, FALSE);
2642 if (object->type == OBJT_SWAP)
2643 swap_pager_freespace(object, offidxstart, count);
2644 if (offidxend >= object->size &&
2645 offidxstart < object->size) {
2646 size1 = object->size;
2647 object->size = offidxstart;
2648 if (object->uip != NULL) {
2649 size1 -= object->size;
2650 KASSERT(object->charge >= ptoa(size1),
2651 ("vm_map_entry_delete: object->charge < 0"));
2652 swap_release_by_uid(ptoa(size1), object->uip);
2653 object->charge -= ptoa(size1);
2657 VM_OBJECT_UNLOCK(object);
2659 entry->object.vm_object = NULL;
2663 * vm_map_delete: [ internal use only ]
2665 * Deallocates the given address range from the target
2669 vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end)
2671 vm_map_entry_t entry;
2672 vm_map_entry_t first_entry;
2674 VM_MAP_ASSERT_LOCKED(map);
2677 * Find the start of the region, and clip it
2679 if (!vm_map_lookup_entry(map, start, &first_entry))
2680 entry = first_entry->next;
2682 entry = first_entry;
2683 vm_map_clip_start(map, entry, start);
2687 * Step through all entries in this region
2689 while ((entry != &map->header) && (entry->start < end)) {
2690 vm_map_entry_t next;
2693 * Wait for wiring or unwiring of an entry to complete.
2694 * Also wait for any system wirings to disappear on
2697 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 ||
2698 (vm_map_pmap(map) != kernel_pmap &&
2699 vm_map_entry_system_wired_count(entry) != 0)) {
2700 unsigned int last_timestamp;
2701 vm_offset_t saved_start;
2702 vm_map_entry_t tmp_entry;
2704 saved_start = entry->start;
2705 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2706 last_timestamp = map->timestamp;
2707 (void) vm_map_unlock_and_wait(map, 0);
2709 if (last_timestamp + 1 != map->timestamp) {
2711 * Look again for the entry because the map was
2712 * modified while it was unlocked.
2713 * Specifically, the entry may have been
2714 * clipped, merged, or deleted.
2716 if (!vm_map_lookup_entry(map, saved_start,
2718 entry = tmp_entry->next;
2721 vm_map_clip_start(map, entry,
2727 vm_map_clip_end(map, entry, end);
2732 * Unwire before removing addresses from the pmap; otherwise,
2733 * unwiring will put the entries back in the pmap.
2735 if (entry->wired_count != 0) {
2736 vm_map_entry_unwire(map, entry);
2739 pmap_remove(map->pmap, entry->start, entry->end);
2742 * Delete the entry only after removing all pmap
2743 * entries pointing to its pages. (Otherwise, its
2744 * page frames may be reallocated, and any modify bits
2745 * will be set in the wrong object!)
2747 vm_map_entry_delete(map, entry);
2748 entry->next = map->deferred_freelist;
2749 map->deferred_freelist = entry;
2752 return (KERN_SUCCESS);
2758 * Remove the given address range from the target map.
2759 * This is the exported form of vm_map_delete.
2762 vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
2767 VM_MAP_RANGE_CHECK(map, start, end);
2768 result = vm_map_delete(map, start, end);
2774 * vm_map_check_protection:
2776 * Assert that the target map allows the specified privilege on the
2777 * entire address region given. The entire region must be allocated.
2779 * WARNING! This code does not and should not check whether the
2780 * contents of the region is accessible. For example a smaller file
2781 * might be mapped into a larger address space.
2783 * NOTE! This code is also called by munmap().
2785 * The map must be locked. A read lock is sufficient.
2788 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
2789 vm_prot_t protection)
2791 vm_map_entry_t entry;
2792 vm_map_entry_t tmp_entry;
2794 if (!vm_map_lookup_entry(map, start, &tmp_entry))
2798 while (start < end) {
2799 if (entry == &map->header)
2804 if (start < entry->start)
2807 * Check protection associated with entry.
2809 if ((entry->protection & protection) != protection)
2811 /* go to next entry */
2813 entry = entry->next;
2819 * vm_map_copy_entry:
2821 * Copies the contents of the source entry to the destination
2822 * entry. The entries *must* be aligned properly.
2828 vm_map_entry_t src_entry,
2829 vm_map_entry_t dst_entry,
2830 vm_ooffset_t *fork_charge)
2832 vm_object_t src_object;
2834 struct uidinfo *uip;
2837 VM_MAP_ASSERT_LOCKED(dst_map);
2839 if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
2842 if (src_entry->wired_count == 0) {
2845 * If the source entry is marked needs_copy, it is already
2848 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
2849 pmap_protect(src_map->pmap,
2852 src_entry->protection & ~VM_PROT_WRITE);
2856 * Make a copy of the object.
2858 size = src_entry->end - src_entry->start;
2859 if ((src_object = src_entry->object.vm_object) != NULL) {
2860 VM_OBJECT_LOCK(src_object);
2861 charged = ENTRY_CHARGED(src_entry);
2862 if ((src_object->handle == NULL) &&
2863 (src_object->type == OBJT_DEFAULT ||
2864 src_object->type == OBJT_SWAP)) {
2865 vm_object_collapse(src_object);
2866 if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
2867 vm_object_split(src_entry);
2868 src_object = src_entry->object.vm_object;
2871 vm_object_reference_locked(src_object);
2872 vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
2873 if (src_entry->uip != NULL &&
2874 !(src_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
2875 KASSERT(src_object->uip == NULL,
2876 ("OVERCOMMIT: vm_map_copy_entry: uip %p",
2878 src_object->uip = src_entry->uip;
2879 src_object->charge = size;
2881 VM_OBJECT_UNLOCK(src_object);
2882 dst_entry->object.vm_object = src_object;
2884 uip = curthread->td_ucred->cr_ruidinfo;
2886 dst_entry->uip = uip;
2887 *fork_charge += size;
2888 if (!(src_entry->eflags &
2889 MAP_ENTRY_NEEDS_COPY)) {
2891 src_entry->uip = uip;
2892 *fork_charge += size;
2895 src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2896 dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2897 dst_entry->offset = src_entry->offset;
2899 dst_entry->object.vm_object = NULL;
2900 dst_entry->offset = 0;
2901 if (src_entry->uip != NULL) {
2902 dst_entry->uip = curthread->td_ucred->cr_ruidinfo;
2903 uihold(dst_entry->uip);
2904 *fork_charge += size;
2908 pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
2909 dst_entry->end - dst_entry->start, src_entry->start);
2912 * Of course, wired down pages can't be set copy-on-write.
2913 * Cause wired pages to be copied into the new map by
2914 * simulating faults (the new pages are pageable)
2916 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry,
2922 * vmspace_map_entry_forked:
2923 * Update the newly-forked vmspace each time a map entry is inherited
2924 * or copied. The values for vm_dsize and vm_tsize are approximate
2925 * (and mostly-obsolete ideas in the face of mmap(2) et al.)
2928 vmspace_map_entry_forked(const struct vmspace *vm1, struct vmspace *vm2,
2929 vm_map_entry_t entry)
2931 vm_size_t entrysize;
2934 entrysize = entry->end - entry->start;
2935 vm2->vm_map.size += entrysize;
2936 if (entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP)) {
2937 vm2->vm_ssize += btoc(entrysize);
2938 } else if (entry->start >= (vm_offset_t)vm1->vm_daddr &&
2939 entry->start < (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)) {
2940 newend = MIN(entry->end,
2941 (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize));
2942 vm2->vm_dsize += btoc(newend - entry->start);
2943 } else if (entry->start >= (vm_offset_t)vm1->vm_taddr &&
2944 entry->start < (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)) {
2945 newend = MIN(entry->end,
2946 (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize));
2947 vm2->vm_tsize += btoc(newend - entry->start);
2953 * Create a new process vmspace structure and vm_map
2954 * based on those of an existing process. The new map
2955 * is based on the old map, according to the inheritance
2956 * values on the regions in that map.
2958 * XXX It might be worth coalescing the entries added to the new vmspace.
2960 * The source map must not be locked.
2963 vmspace_fork(struct vmspace *vm1, vm_ooffset_t *fork_charge)
2965 struct vmspace *vm2;
2966 vm_map_t old_map = &vm1->vm_map;
2968 vm_map_entry_t old_entry;
2969 vm_map_entry_t new_entry;
2973 vm_map_lock(old_map);
2974 vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
2976 goto unlock_and_return;
2977 vm2->vm_taddr = vm1->vm_taddr;
2978 vm2->vm_daddr = vm1->vm_daddr;
2979 vm2->vm_maxsaddr = vm1->vm_maxsaddr;
2980 new_map = &vm2->vm_map; /* XXX */
2981 locked = vm_map_trylock(new_map); /* trylock to silence WITNESS */
2982 KASSERT(locked, ("vmspace_fork: lock failed"));
2983 new_map->timestamp = 1;
2985 old_entry = old_map->header.next;
2987 while (old_entry != &old_map->header) {
2988 if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2989 panic("vm_map_fork: encountered a submap");
2991 switch (old_entry->inheritance) {
2992 case VM_INHERIT_NONE:
2995 case VM_INHERIT_SHARE:
2997 * Clone the entry, creating the shared object if necessary.
2999 object = old_entry->object.vm_object;
3000 if (object == NULL) {
3001 object = vm_object_allocate(OBJT_DEFAULT,
3002 atop(old_entry->end - old_entry->start));
3003 old_entry->object.vm_object = object;
3004 old_entry->offset = 0;
3005 if (old_entry->uip != NULL) {
3006 object->uip = old_entry->uip;
3007 object->charge = old_entry->end -
3009 old_entry->uip = NULL;
3014 * Add the reference before calling vm_object_shadow
3015 * to insure that a shadow object is created.
3017 vm_object_reference(object);
3018 if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3019 vm_object_shadow(&old_entry->object.vm_object,
3021 atop(old_entry->end - old_entry->start));
3022 old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
3023 /* Transfer the second reference too. */
3024 vm_object_reference(
3025 old_entry->object.vm_object);
3028 * As in vm_map_simplify_entry(), the
3029 * vnode lock will not be acquired in
3030 * this call to vm_object_deallocate().
3032 vm_object_deallocate(object);
3033 object = old_entry->object.vm_object;
3035 VM_OBJECT_LOCK(object);
3036 vm_object_clear_flag(object, OBJ_ONEMAPPING);
3037 if (old_entry->uip != NULL) {
3038 KASSERT(object->uip == NULL, ("vmspace_fork both uip"));
3039 object->uip = old_entry->uip;
3040 object->charge = old_entry->end - old_entry->start;
3041 old_entry->uip = NULL;
3043 VM_OBJECT_UNLOCK(object);
3046 * Clone the entry, referencing the shared object.
3048 new_entry = vm_map_entry_create(new_map);
3049 *new_entry = *old_entry;
3050 new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
3051 MAP_ENTRY_IN_TRANSITION);
3052 new_entry->wired_count = 0;
3055 * Insert the entry into the new map -- we know we're
3056 * inserting at the end of the new map.
3058 vm_map_entry_link(new_map, new_map->header.prev,
3060 vmspace_map_entry_forked(vm1, vm2, new_entry);
3063 * Update the physical map
3065 pmap_copy(new_map->pmap, old_map->pmap,
3067 (old_entry->end - old_entry->start),
3071 case VM_INHERIT_COPY:
3073 * Clone the entry and link into the map.
3075 new_entry = vm_map_entry_create(new_map);
3076 *new_entry = *old_entry;
3077 new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
3078 MAP_ENTRY_IN_TRANSITION);
3079 new_entry->wired_count = 0;
3080 new_entry->object.vm_object = NULL;
3081 new_entry->uip = NULL;
3082 vm_map_entry_link(new_map, new_map->header.prev,
3084 vmspace_map_entry_forked(vm1, vm2, new_entry);
3085 vm_map_copy_entry(old_map, new_map, old_entry,
3086 new_entry, fork_charge);
3089 old_entry = old_entry->next;
3092 vm_map_unlock(old_map);
3094 vm_map_unlock(new_map);
3100 vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
3101 vm_prot_t prot, vm_prot_t max, int cow)
3103 vm_map_entry_t new_entry, prev_entry;
3104 vm_offset_t bot, top;
3105 vm_size_t init_ssize;
3110 * The stack orientation is piggybacked with the cow argument.
3111 * Extract it into orient and mask the cow argument so that we
3112 * don't pass it around further.
3113 * NOTE: We explicitly allow bi-directional stacks.
3115 orient = cow & (MAP_STACK_GROWS_DOWN|MAP_STACK_GROWS_UP);
3117 KASSERT(orient != 0, ("No stack grow direction"));
3119 if (addrbos < vm_map_min(map) ||
3120 addrbos > vm_map_max(map) ||
3121 addrbos + max_ssize < addrbos)
3122 return (KERN_NO_SPACE);
3124 init_ssize = (max_ssize < sgrowsiz) ? max_ssize : sgrowsiz;
3126 PROC_LOCK(curthread->td_proc);
3127 vmemlim = lim_cur(curthread->td_proc, RLIMIT_VMEM);
3128 PROC_UNLOCK(curthread->td_proc);
3132 /* If addr is already mapped, no go */
3133 if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
3135 return (KERN_NO_SPACE);
3138 /* If we would blow our VMEM resource limit, no go */
3139 if (map->size + init_ssize > vmemlim) {
3141 return (KERN_NO_SPACE);
3145 * If we can't accomodate max_ssize in the current mapping, no go.
3146 * However, we need to be aware that subsequent user mappings might
3147 * map into the space we have reserved for stack, and currently this
3148 * space is not protected.
3150 * Hopefully we will at least detect this condition when we try to
3153 if ((prev_entry->next != &map->header) &&
3154 (prev_entry->next->start < addrbos + max_ssize)) {
3156 return (KERN_NO_SPACE);
3160 * We initially map a stack of only init_ssize. We will grow as
3161 * needed later. Depending on the orientation of the stack (i.e.
3162 * the grow direction) we either map at the top of the range, the
3163 * bottom of the range or in the middle.
3165 * Note: we would normally expect prot and max to be VM_PROT_ALL,
3166 * and cow to be 0. Possibly we should eliminate these as input
3167 * parameters, and just pass these values here in the insert call.
3169 if (orient == MAP_STACK_GROWS_DOWN)
3170 bot = addrbos + max_ssize - init_ssize;
3171 else if (orient == MAP_STACK_GROWS_UP)
3174 bot = round_page(addrbos + max_ssize/2 - init_ssize/2);
3175 top = bot + init_ssize;
3176 rv = vm_map_insert(map, NULL, 0, bot, top, prot, max, cow);
3178 /* Now set the avail_ssize amount. */
3179 if (rv == KERN_SUCCESS) {
3180 if (prev_entry != &map->header)
3181 vm_map_clip_end(map, prev_entry, bot);
3182 new_entry = prev_entry->next;
3183 if (new_entry->end != top || new_entry->start != bot)
3184 panic("Bad entry start/end for new stack entry");
3186 new_entry->avail_ssize = max_ssize - init_ssize;
3187 if (orient & MAP_STACK_GROWS_DOWN)
3188 new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
3189 if (orient & MAP_STACK_GROWS_UP)
3190 new_entry->eflags |= MAP_ENTRY_GROWS_UP;
3197 /* Attempts to grow a vm stack entry. Returns KERN_SUCCESS if the
3198 * desired address is already mapped, or if we successfully grow
3199 * the stack. Also returns KERN_SUCCESS if addr is outside the
3200 * stack range (this is strange, but preserves compatibility with
3201 * the grow function in vm_machdep.c).
3204 vm_map_growstack(struct proc *p, vm_offset_t addr)
3206 vm_map_entry_t next_entry, prev_entry;
3207 vm_map_entry_t new_entry, stack_entry;
3208 struct vmspace *vm = p->p_vmspace;
3209 vm_map_t map = &vm->vm_map;
3211 size_t grow_amount, max_grow;
3212 rlim_t stacklim, vmemlim;
3213 int is_procstack, rv;
3214 struct uidinfo *uip;
3218 stacklim = lim_cur(p, RLIMIT_STACK);
3219 vmemlim = lim_cur(p, RLIMIT_VMEM);
3222 vm_map_lock_read(map);
3224 /* If addr is already in the entry range, no need to grow.*/
3225 if (vm_map_lookup_entry(map, addr, &prev_entry)) {
3226 vm_map_unlock_read(map);
3227 return (KERN_SUCCESS);
3230 next_entry = prev_entry->next;
3231 if (!(prev_entry->eflags & MAP_ENTRY_GROWS_UP)) {
3233 * This entry does not grow upwards. Since the address lies
3234 * beyond this entry, the next entry (if one exists) has to
3235 * be a downward growable entry. The entry list header is
3236 * never a growable entry, so it suffices to check the flags.
3238 if (!(next_entry->eflags & MAP_ENTRY_GROWS_DOWN)) {
3239 vm_map_unlock_read(map);
3240 return (KERN_SUCCESS);
3242 stack_entry = next_entry;
3245 * This entry grows upward. If the next entry does not at
3246 * least grow downwards, this is the entry we need to grow.
3247 * otherwise we have two possible choices and we have to
3250 if (next_entry->eflags & MAP_ENTRY_GROWS_DOWN) {
3252 * We have two choices; grow the entry closest to
3253 * the address to minimize the amount of growth.
3255 if (addr - prev_entry->end <= next_entry->start - addr)
3256 stack_entry = prev_entry;
3258 stack_entry = next_entry;
3260 stack_entry = prev_entry;
3263 if (stack_entry == next_entry) {
3264 KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_DOWN, ("foo"));
3265 KASSERT(addr < stack_entry->start, ("foo"));
3266 end = (prev_entry != &map->header) ? prev_entry->end :
3267 stack_entry->start - stack_entry->avail_ssize;
3268 grow_amount = roundup(stack_entry->start - addr, PAGE_SIZE);
3269 max_grow = stack_entry->start - end;
3271 KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_UP, ("foo"));
3272 KASSERT(addr >= stack_entry->end, ("foo"));
3273 end = (next_entry != &map->header) ? next_entry->start :
3274 stack_entry->end + stack_entry->avail_ssize;
3275 grow_amount = roundup(addr + 1 - stack_entry->end, PAGE_SIZE);
3276 max_grow = end - stack_entry->end;
3279 if (grow_amount > stack_entry->avail_ssize) {
3280 vm_map_unlock_read(map);
3281 return (KERN_NO_SPACE);
3285 * If there is no longer enough space between the entries nogo, and
3286 * adjust the available space. Note: this should only happen if the
3287 * user has mapped into the stack area after the stack was created,
3288 * and is probably an error.
3290 * This also effectively destroys any guard page the user might have
3291 * intended by limiting the stack size.
3293 if (grow_amount > max_grow) {
3294 if (vm_map_lock_upgrade(map))
3297 stack_entry->avail_ssize = max_grow;
3300 return (KERN_NO_SPACE);
3303 is_procstack = (addr >= (vm_offset_t)vm->vm_maxsaddr) ? 1 : 0;
3306 * If this is the main process stack, see if we're over the stack
3309 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
3310 vm_map_unlock_read(map);
3311 return (KERN_NO_SPACE);
3314 /* Round up the grow amount modulo SGROWSIZ */
3315 grow_amount = roundup (grow_amount, sgrowsiz);
3316 if (grow_amount > stack_entry->avail_ssize)
3317 grow_amount = stack_entry->avail_ssize;
3318 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
3319 grow_amount = stacklim - ctob(vm->vm_ssize);
3322 /* If we would blow our VMEM resource limit, no go */
3323 if (map->size + grow_amount > vmemlim) {
3324 vm_map_unlock_read(map);
3325 return (KERN_NO_SPACE);
3328 if (vm_map_lock_upgrade(map))
3331 if (stack_entry == next_entry) {
3335 /* Get the preliminary new entry start value */
3336 addr = stack_entry->start - grow_amount;
3339 * If this puts us into the previous entry, cut back our
3340 * growth to the available space. Also, see the note above.
3343 stack_entry->avail_ssize = max_grow;
3347 rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start,
3348 p->p_sysent->sv_stackprot, VM_PROT_ALL, 0);
3350 /* Adjust the available stack space by the amount we grew. */
3351 if (rv == KERN_SUCCESS) {
3352 if (prev_entry != &map->header)
3353 vm_map_clip_end(map, prev_entry, addr);
3354 new_entry = prev_entry->next;
3355 KASSERT(new_entry == stack_entry->prev, ("foo"));
3356 KASSERT(new_entry->end == stack_entry->start, ("foo"));
3357 KASSERT(new_entry->start == addr, ("foo"));
3358 grow_amount = new_entry->end - new_entry->start;
3359 new_entry->avail_ssize = stack_entry->avail_ssize -
3361 stack_entry->eflags &= ~MAP_ENTRY_GROWS_DOWN;
3362 new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
3368 addr = stack_entry->end + grow_amount;
3371 * If this puts us into the next entry, cut back our growth
3372 * to the available space. Also, see the note above.
3375 stack_entry->avail_ssize = end - stack_entry->end;
3379 grow_amount = addr - stack_entry->end;
3380 uip = stack_entry->uip;
3381 if (uip == NULL && stack_entry->object.vm_object != NULL)
3382 uip = stack_entry->object.vm_object->uip;
3383 if (uip != NULL && !swap_reserve_by_uid(grow_amount, uip))
3385 /* Grow the underlying object if applicable. */
3386 else if (stack_entry->object.vm_object == NULL ||
3387 vm_object_coalesce(stack_entry->object.vm_object,
3388 stack_entry->offset,
3389 (vm_size_t)(stack_entry->end - stack_entry->start),
3390 (vm_size_t)grow_amount, uip != NULL)) {
3391 map->size += (addr - stack_entry->end);
3392 /* Update the current entry. */
3393 stack_entry->end = addr;
3394 stack_entry->avail_ssize -= grow_amount;
3395 vm_map_entry_resize_free(map, stack_entry);
3398 if (next_entry != &map->header)
3399 vm_map_clip_start(map, next_entry, addr);
3404 if (rv == KERN_SUCCESS && is_procstack)
3405 vm->vm_ssize += btoc(grow_amount);
3410 * Heed the MAP_WIREFUTURE flag if it was set for this process.
3412 if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE)) {
3414 (stack_entry == next_entry) ? addr : addr - grow_amount,
3415 (stack_entry == next_entry) ? stack_entry->start : addr,
3416 (p->p_flag & P_SYSTEM)
3417 ? VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES
3418 : VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
3425 * Unshare the specified VM space for exec. If other processes are
3426 * mapped to it, then create a new one. The new vmspace is null.
3429 vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser)
3431 struct vmspace *oldvmspace = p->p_vmspace;
3432 struct vmspace *newvmspace;
3434 newvmspace = vmspace_alloc(minuser, maxuser);
3435 if (newvmspace == NULL)
3437 newvmspace->vm_swrss = oldvmspace->vm_swrss;
3439 * This code is written like this for prototype purposes. The
3440 * goal is to avoid running down the vmspace here, but let the
3441 * other process's that are still using the vmspace to finally
3442 * run it down. Even though there is little or no chance of blocking
3443 * here, it is a good idea to keep this form for future mods.
3445 PROC_VMSPACE_LOCK(p);
3446 p->p_vmspace = newvmspace;
3447 PROC_VMSPACE_UNLOCK(p);
3448 if (p == curthread->td_proc)
3449 pmap_activate(curthread);
3450 vmspace_free(oldvmspace);
3455 * Unshare the specified VM space for forcing COW. This
3456 * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
3459 vmspace_unshare(struct proc *p)
3461 struct vmspace *oldvmspace = p->p_vmspace;
3462 struct vmspace *newvmspace;
3463 vm_ooffset_t fork_charge;
3465 if (oldvmspace->vm_refcnt == 1)
3468 newvmspace = vmspace_fork(oldvmspace, &fork_charge);
3469 if (newvmspace == NULL)
3471 if (!swap_reserve_by_uid(fork_charge, p->p_ucred->cr_ruidinfo)) {
3472 vmspace_free(newvmspace);
3475 PROC_VMSPACE_LOCK(p);
3476 p->p_vmspace = newvmspace;
3477 PROC_VMSPACE_UNLOCK(p);
3478 if (p == curthread->td_proc)
3479 pmap_activate(curthread);
3480 vmspace_free(oldvmspace);
3487 * Finds the VM object, offset, and
3488 * protection for a given virtual address in the
3489 * specified map, assuming a page fault of the
3492 * Leaves the map in question locked for read; return
3493 * values are guaranteed until a vm_map_lookup_done
3494 * call is performed. Note that the map argument
3495 * is in/out; the returned map must be used in
3496 * the call to vm_map_lookup_done.
3498 * A handle (out_entry) is returned for use in
3499 * vm_map_lookup_done, to make that fast.
3501 * If a lookup is requested with "write protection"
3502 * specified, the map may be changed to perform virtual
3503 * copying operations, although the data referenced will
3507 vm_map_lookup(vm_map_t *var_map, /* IN/OUT */
3509 vm_prot_t fault_typea,
3510 vm_map_entry_t *out_entry, /* OUT */
3511 vm_object_t *object, /* OUT */
3512 vm_pindex_t *pindex, /* OUT */
3513 vm_prot_t *out_prot, /* OUT */
3514 boolean_t *wired) /* OUT */
3516 vm_map_entry_t entry;
3517 vm_map_t map = *var_map;
3519 vm_prot_t fault_type = fault_typea;
3520 vm_object_t eobject;
3521 struct uidinfo *uip;
3526 vm_map_lock_read(map);
3529 * Lookup the faulting address.
3531 if (!vm_map_lookup_entry(map, vaddr, out_entry)) {
3532 vm_map_unlock_read(map);
3533 return (KERN_INVALID_ADDRESS);
3541 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3542 vm_map_t old_map = map;
3544 *var_map = map = entry->object.sub_map;
3545 vm_map_unlock_read(old_map);
3550 * Check whether this task is allowed to have this page.
3551 * Note the special case for MAP_ENTRY_COW
3552 * pages with an override. This is to implement a forced
3553 * COW for debuggers.
3555 if (fault_type & VM_PROT_OVERRIDE_WRITE)
3556 prot = entry->max_protection;
3558 prot = entry->protection;
3559 fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
3560 if ((fault_type & prot) != fault_type) {
3561 vm_map_unlock_read(map);
3562 return (KERN_PROTECTION_FAILURE);
3564 if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
3565 (entry->eflags & MAP_ENTRY_COW) &&
3566 (fault_type & VM_PROT_WRITE) &&
3567 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
3568 vm_map_unlock_read(map);
3569 return (KERN_PROTECTION_FAILURE);
3573 * If this page is not pageable, we have to get it for all possible
3576 *wired = (entry->wired_count != 0);
3578 prot = fault_type = entry->protection;
3579 size = entry->end - entry->start;
3581 * If the entry was copy-on-write, we either ...
3583 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3585 * If we want to write the page, we may as well handle that
3586 * now since we've got the map locked.
3588 * If we don't need to write the page, we just demote the
3589 * permissions allowed.
3591 if (fault_type & VM_PROT_WRITE) {
3593 * Make a new object, and place it in the object
3594 * chain. Note that no new references have appeared
3595 * -- one just moved from the map to the new
3598 if (vm_map_lock_upgrade(map))
3601 if (entry->uip == NULL) {
3603 * The debugger owner is charged for
3606 uip = curthread->td_ucred->cr_ruidinfo;
3608 if (!swap_reserve_by_uid(size, uip)) {
3611 return (KERN_RESOURCE_SHORTAGE);
3616 &entry->object.vm_object,
3619 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
3620 eobject = entry->object.vm_object;
3621 if (eobject->uip != NULL) {
3623 * The object was not shadowed.
3625 swap_release_by_uid(size, entry->uip);
3628 } else if (entry->uip != NULL) {
3629 VM_OBJECT_LOCK(eobject);
3630 eobject->uip = entry->uip;
3631 eobject->charge = size;
3632 VM_OBJECT_UNLOCK(eobject);
3636 vm_map_lock_downgrade(map);
3639 * We're attempting to read a copy-on-write page --
3640 * don't allow writes.
3642 prot &= ~VM_PROT_WRITE;
3647 * Create an object if necessary.
3649 if (entry->object.vm_object == NULL &&
3651 if (vm_map_lock_upgrade(map))
3653 entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
3656 if (entry->uip != NULL) {
3657 VM_OBJECT_LOCK(entry->object.vm_object);
3658 entry->object.vm_object->uip = entry->uip;
3659 entry->object.vm_object->charge = size;
3660 VM_OBJECT_UNLOCK(entry->object.vm_object);
3663 vm_map_lock_downgrade(map);
3667 * Return the object/offset from this entry. If the entry was
3668 * copy-on-write or empty, it has been fixed up.
3670 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
3671 *object = entry->object.vm_object;
3674 return (KERN_SUCCESS);
3678 * vm_map_lookup_locked:
3680 * Lookup the faulting address. A version of vm_map_lookup that returns
3681 * KERN_FAILURE instead of blocking on map lock or memory allocation.
3684 vm_map_lookup_locked(vm_map_t *var_map, /* IN/OUT */
3686 vm_prot_t fault_typea,
3687 vm_map_entry_t *out_entry, /* OUT */
3688 vm_object_t *object, /* OUT */
3689 vm_pindex_t *pindex, /* OUT */
3690 vm_prot_t *out_prot, /* OUT */
3691 boolean_t *wired) /* OUT */
3693 vm_map_entry_t entry;
3694 vm_map_t map = *var_map;
3696 vm_prot_t fault_type = fault_typea;
3699 * Lookup the faulting address.
3701 if (!vm_map_lookup_entry(map, vaddr, out_entry))
3702 return (KERN_INVALID_ADDRESS);
3707 * Fail if the entry refers to a submap.
3709 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
3710 return (KERN_FAILURE);
3713 * Check whether this task is allowed to have this page.
3714 * Note the special case for MAP_ENTRY_COW
3715 * pages with an override. This is to implement a forced
3716 * COW for debuggers.
3718 if (fault_type & VM_PROT_OVERRIDE_WRITE)
3719 prot = entry->max_protection;
3721 prot = entry->protection;
3722 fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
3723 if ((fault_type & prot) != fault_type)
3724 return (KERN_PROTECTION_FAILURE);
3725 if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
3726 (entry->eflags & MAP_ENTRY_COW) &&
3727 (fault_type & VM_PROT_WRITE) &&
3728 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0)
3729 return (KERN_PROTECTION_FAILURE);
3732 * If this page is not pageable, we have to get it for all possible
3735 *wired = (entry->wired_count != 0);
3737 prot = fault_type = entry->protection;
3739 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3741 * Fail if the entry was copy-on-write for a write fault.
3743 if (fault_type & VM_PROT_WRITE)
3744 return (KERN_FAILURE);
3746 * We're attempting to read a copy-on-write page --
3747 * don't allow writes.
3749 prot &= ~VM_PROT_WRITE;
3753 * Fail if an object should be created.
3755 if (entry->object.vm_object == NULL && !map->system_map)
3756 return (KERN_FAILURE);
3759 * Return the object/offset from this entry. If the entry was
3760 * copy-on-write or empty, it has been fixed up.
3762 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
3763 *object = entry->object.vm_object;
3766 return (KERN_SUCCESS);
3770 * vm_map_lookup_done:
3772 * Releases locks acquired by a vm_map_lookup
3773 * (according to the handle returned by that lookup).
3776 vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry)
3779 * Unlock the main-level map
3781 vm_map_unlock_read(map);
3784 #include "opt_ddb.h"
3786 #include <sys/kernel.h>
3788 #include <ddb/ddb.h>
3791 * vm_map_print: [ debug ]
3793 DB_SHOW_COMMAND(map, vm_map_print)
3796 /* XXX convert args. */
3797 vm_map_t map = (vm_map_t)addr;
3798 boolean_t full = have_addr;
3800 vm_map_entry_t entry;
3802 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
3804 (void *)map->pmap, map->nentries, map->timestamp);
3807 if (!full && db_indent)
3811 for (entry = map->header.next; entry != &map->header;
3812 entry = entry->next) {
3813 db_iprintf("map entry %p: start=%p, end=%p\n",
3814 (void *)entry, (void *)entry->start, (void *)entry->end);
3817 static char *inheritance_name[4] =
3818 {"share", "copy", "none", "donate_copy"};
3820 db_iprintf(" prot=%x/%x/%s",
3822 entry->max_protection,
3823 inheritance_name[(int)(unsigned char)entry->inheritance]);
3824 if (entry->wired_count != 0)
3825 db_printf(", wired");
3827 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3828 db_printf(", share=%p, offset=0x%jx\n",
3829 (void *)entry->object.sub_map,
3830 (uintmax_t)entry->offset);
3832 if ((entry->prev == &map->header) ||
3833 (entry->prev->object.sub_map !=
3834 entry->object.sub_map)) {
3836 vm_map_print((db_expr_t)(intptr_t)
3837 entry->object.sub_map,
3838 full, 0, (char *)0);
3842 if (entry->uip != NULL)
3843 db_printf(", uip %d", entry->uip->ui_uid);
3844 db_printf(", object=%p, offset=0x%jx",
3845 (void *)entry->object.vm_object,
3846 (uintmax_t)entry->offset);
3847 if (entry->object.vm_object && entry->object.vm_object->uip)
3848 db_printf(", obj uip %d charge %jx",
3849 entry->object.vm_object->uip->ui_uid,
3850 (uintmax_t)entry->object.vm_object->charge);
3851 if (entry->eflags & MAP_ENTRY_COW)
3852 db_printf(", copy (%s)",
3853 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
3857 if ((entry->prev == &map->header) ||
3858 (entry->prev->object.vm_object !=
3859 entry->object.vm_object)) {
3861 vm_object_print((db_expr_t)(intptr_t)
3862 entry->object.vm_object,
3863 full, 0, (char *)0);
3875 DB_SHOW_COMMAND(procvm, procvm)
3880 p = (struct proc *) addr;
3885 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
3886 (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
3887 (void *)vmspace_pmap(p->p_vmspace));
3889 vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);