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 single hint is used to speed up lookups.
105 * Since portions of maps are specified by start/end addresses,
106 * which may not align with existing map entries, all
107 * routines merely "clip" entries to these start/end values.
108 * [That is, an entry is split into two, bordering at a
109 * start or end value.] Note that these clippings may not
110 * always be necessary (as the two resulting entries are then
111 * not changed); however, the clipping is done for convenience.
113 * As mentioned above, virtual copy operations are performed
114 * by copying VM object references from one map to
115 * another, and then marking both regions as copy-on-write.
121 * Initialize the vm_map module. Must be called before
122 * any other vm_map routines.
124 * Map and entry structures are allocated from the general
125 * purpose memory pool with some exceptions:
127 * - The kernel map and kmem submap are allocated statically.
128 * - Kernel map entries are allocated out of a static pool.
130 * These restrictions are necessary since malloc() uses the
131 * maps and requires map entries.
134 static struct mtx map_sleep_mtx;
135 static uma_zone_t mapentzone;
136 static uma_zone_t kmapentzone;
137 static uma_zone_t mapzone;
138 static uma_zone_t vmspace_zone;
139 static struct vm_object kmapentobj;
140 static int vmspace_zinit(void *mem, int size, int flags);
141 static void vmspace_zfini(void *mem, int size);
142 static int vm_map_zinit(void *mem, int ize, int flags);
143 static void vm_map_zfini(void *mem, int size);
144 static void _vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max);
147 static void vm_map_zdtor(void *mem, int size, void *arg);
148 static void vmspace_zdtor(void *mem, int size, void *arg);
152 * PROC_VMSPACE_{UN,}LOCK() can be a noop as long as vmspaces are type
155 #define PROC_VMSPACE_LOCK(p) do { } while (0)
156 #define PROC_VMSPACE_UNLOCK(p) do { } while (0)
159 * VM_MAP_RANGE_CHECK: [ internal use only ]
161 * Asserts that the starting and ending region
162 * addresses fall within the valid range of the map.
164 #define VM_MAP_RANGE_CHECK(map, start, end) \
166 if (start < vm_map_min(map)) \
167 start = vm_map_min(map); \
168 if (end > vm_map_max(map)) \
169 end = vm_map_max(map); \
177 mtx_init(&map_sleep_mtx, "vm map sleep mutex", NULL, MTX_DEF);
178 mapzone = uma_zcreate("MAP", sizeof(struct vm_map), NULL,
184 vm_map_zinit, vm_map_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
185 uma_prealloc(mapzone, MAX_KMAP);
186 kmapentzone = uma_zcreate("KMAP ENTRY", sizeof(struct vm_map_entry),
187 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
188 UMA_ZONE_MTXCLASS | UMA_ZONE_VM);
189 uma_prealloc(kmapentzone, MAX_KMAPENT);
190 mapentzone = uma_zcreate("MAP ENTRY", sizeof(struct vm_map_entry),
191 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
195 vmspace_zfini(void *mem, int size)
199 vm = (struct vmspace *)mem;
200 pmap_release(vmspace_pmap(vm));
201 vm_map_zfini(&vm->vm_map, sizeof(vm->vm_map));
205 vmspace_zinit(void *mem, int size, int flags)
209 vm = (struct vmspace *)mem;
211 (void)vm_map_zinit(&vm->vm_map, sizeof(vm->vm_map), flags);
212 pmap_pinit(vmspace_pmap(vm));
217 vm_map_zfini(void *mem, int size)
222 mtx_destroy(&map->system_mtx);
223 sx_destroy(&map->lock);
227 vm_map_zinit(void *mem, int size, int flags)
234 mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
235 sx_init(&map->lock, "user map");
241 vmspace_zdtor(void *mem, int size, void *arg)
245 vm = (struct vmspace *)mem;
247 vm_map_zdtor(&vm->vm_map, sizeof(vm->vm_map), arg);
250 vm_map_zdtor(void *mem, int size, void *arg)
255 KASSERT(map->nentries == 0,
256 ("map %p nentries == %d on free.",
257 map, map->nentries));
258 KASSERT(map->size == 0,
259 ("map %p size == %lu on free.",
260 map, (unsigned long)map->size));
262 #endif /* INVARIANTS */
265 * Allocate a vmspace structure, including a vm_map and pmap,
266 * and initialize those structures. The refcnt is set to 1.
269 vmspace_alloc(min, max)
270 vm_offset_t min, max;
274 vm = uma_zalloc(vmspace_zone, M_WAITOK);
275 CTR1(KTR_VM, "vmspace_alloc: %p", vm);
276 _vm_map_init(&vm->vm_map, min, max);
277 vm->vm_map.pmap = vmspace_pmap(vm); /* XXX */
293 uma_zone_set_obj(kmapentzone, &kmapentobj, lmin(cnt.v_page_count,
294 (VM_MAX_KERNEL_ADDRESS - KERNBASE) / PAGE_SIZE) / 8 +
295 maxproc * 2 + maxfiles);
296 vmspace_zone = uma_zcreate("VMSPACE", sizeof(struct vmspace), NULL,
302 vmspace_zinit, vmspace_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
306 vmspace_dofree(struct vmspace *vm)
308 CTR1(KTR_VM, "vmspace_free: %p", vm);
311 * Make sure any SysV shm is freed, it might not have been in
317 * Lock the map, to wait out all other references to it.
318 * Delete all of the mappings and pages they hold, then call
319 * the pmap module to reclaim anything left.
321 (void)vm_map_remove(&vm->vm_map, vm->vm_map.min_offset,
322 vm->vm_map.max_offset);
324 uma_zfree(vmspace_zone, vm);
328 vmspace_free(struct vmspace *vm)
332 if (vm->vm_refcnt == 0)
333 panic("vmspace_free: attempt to free already freed vmspace");
336 refcnt = vm->vm_refcnt;
337 while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1));
343 vmspace_exitfree(struct proc *p)
347 PROC_VMSPACE_LOCK(p);
350 PROC_VMSPACE_UNLOCK(p);
351 KASSERT(vm == &vmspace0, ("vmspace_exitfree: wrong vmspace"));
356 vmspace_exit(struct thread *td)
363 * Release user portion of address space.
364 * This releases references to vnodes,
365 * which could cause I/O if the file has been unlinked.
366 * Need to do this early enough that we can still sleep.
368 * The last exiting process to reach this point releases as
369 * much of the environment as it can. vmspace_dofree() is the
370 * slower fallback in case another process had a temporary
371 * reference to the vmspace.
376 atomic_add_int(&vmspace0.vm_refcnt, 1);
378 refcnt = vm->vm_refcnt;
379 if (refcnt > 1 && p->p_vmspace != &vmspace0) {
380 /* Switch now since other proc might free vmspace */
381 PROC_VMSPACE_LOCK(p);
382 p->p_vmspace = &vmspace0;
383 PROC_VMSPACE_UNLOCK(p);
386 } while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1));
388 if (p->p_vmspace != vm) {
389 /* vmspace not yet freed, switch back */
390 PROC_VMSPACE_LOCK(p);
392 PROC_VMSPACE_UNLOCK(p);
395 pmap_remove_pages(vmspace_pmap(vm));
396 /* Switch now since this proc will free vmspace */
397 PROC_VMSPACE_LOCK(p);
398 p->p_vmspace = &vmspace0;
399 PROC_VMSPACE_UNLOCK(p);
405 /* Acquire reference to vmspace owned by another process. */
408 vmspace_acquire_ref(struct proc *p)
413 PROC_VMSPACE_LOCK(p);
416 PROC_VMSPACE_UNLOCK(p);
420 refcnt = vm->vm_refcnt;
421 if (refcnt <= 0) { /* Avoid 0->1 transition */
422 PROC_VMSPACE_UNLOCK(p);
425 } while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt + 1));
426 if (vm != p->p_vmspace) {
427 PROC_VMSPACE_UNLOCK(p);
431 PROC_VMSPACE_UNLOCK(p);
436 _vm_map_lock(vm_map_t map, const char *file, int line)
440 _mtx_lock_flags(&map->system_mtx, 0, file, line);
442 (void)_sx_xlock(&map->lock, 0, file, line);
447 _vm_map_unlock(vm_map_t map, const char *file, int line)
451 _mtx_unlock_flags(&map->system_mtx, 0, file, line);
453 _sx_xunlock(&map->lock, file, line);
457 _vm_map_lock_read(vm_map_t map, const char *file, int line)
461 _mtx_lock_flags(&map->system_mtx, 0, file, line);
463 (void)_sx_xlock(&map->lock, 0, file, line);
467 _vm_map_unlock_read(vm_map_t map, const char *file, int line)
471 _mtx_unlock_flags(&map->system_mtx, 0, file, line);
473 _sx_xunlock(&map->lock, file, line);
477 _vm_map_trylock(vm_map_t map, const char *file, int line)
481 error = map->system_map ?
482 !_mtx_trylock(&map->system_mtx, 0, file, line) :
483 !_sx_try_xlock(&map->lock, file, line);
490 _vm_map_trylock_read(vm_map_t map, const char *file, int line)
494 error = map->system_map ?
495 !_mtx_trylock(&map->system_mtx, 0, file, line) :
496 !_sx_try_xlock(&map->lock, file, line);
501 _vm_map_lock_upgrade(vm_map_t map, const char *file, int line)
505 if (map->system_map) {
506 _mtx_assert(&map->system_mtx, MA_OWNED, file, line);
508 _sx_assert(&map->lock, SX_XLOCKED, file, line);
515 _vm_map_lock_downgrade(vm_map_t map, const char *file, int line)
519 if (map->system_map) {
520 _mtx_assert(&map->system_mtx, MA_OWNED, file, line);
522 _sx_assert(&map->lock, SX_XLOCKED, file, line);
527 * vm_map_unlock_and_wait:
530 vm_map_unlock_and_wait(vm_map_t map, boolean_t user_wait)
533 mtx_lock(&map_sleep_mtx);
535 return (msleep(&map->root, &map_sleep_mtx, PDROP | PVM, "vmmaps", 0));
542 vm_map_wakeup(vm_map_t map)
546 * Acquire and release map_sleep_mtx to prevent a wakeup()
547 * from being performed (and lost) between the vm_map_unlock()
548 * and the msleep() in vm_map_unlock_and_wait().
550 mtx_lock(&map_sleep_mtx);
551 mtx_unlock(&map_sleep_mtx);
556 vmspace_resident_count(struct vmspace *vmspace)
558 return pmap_resident_count(vmspace_pmap(vmspace));
562 vmspace_wired_count(struct vmspace *vmspace)
564 return pmap_wired_count(vmspace_pmap(vmspace));
570 * Creates and returns a new empty VM map with
571 * the given physical map structure, and having
572 * the given lower and upper address bounds.
575 vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max)
579 result = uma_zalloc(mapzone, M_WAITOK);
580 CTR1(KTR_VM, "vm_map_create: %p", result);
581 _vm_map_init(result, min, max);
587 * Initialize an existing vm_map structure
588 * such as that in the vmspace structure.
589 * The pmap is set elsewhere.
592 _vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max)
595 map->header.next = map->header.prev = &map->header;
596 map->needs_wakeup = FALSE;
598 map->min_offset = min;
599 map->max_offset = max;
606 vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max)
608 _vm_map_init(map, min, max);
609 mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
610 sx_init(&map->lock, "user map");
614 * vm_map_entry_dispose: [ internal use only ]
616 * Inverse of vm_map_entry_create.
619 vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry)
621 uma_zfree(map->system_map ? kmapentzone : mapentzone, entry);
625 * vm_map_entry_create: [ internal use only ]
627 * Allocates a VM map entry for insertion.
628 * No entry fields are filled in.
630 static vm_map_entry_t
631 vm_map_entry_create(vm_map_t map)
633 vm_map_entry_t new_entry;
636 new_entry = uma_zalloc(kmapentzone, M_NOWAIT);
638 new_entry = uma_zalloc(mapentzone, M_WAITOK);
639 if (new_entry == NULL)
640 panic("vm_map_entry_create: kernel resources exhausted");
645 * vm_map_entry_set_behavior:
647 * Set the expected access behavior, either normal, random, or
651 vm_map_entry_set_behavior(vm_map_entry_t entry, u_char behavior)
653 entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) |
654 (behavior & MAP_ENTRY_BEHAV_MASK);
658 * vm_map_entry_set_max_free:
660 * Set the max_free field in a vm_map_entry.
663 vm_map_entry_set_max_free(vm_map_entry_t entry)
666 entry->max_free = entry->adj_free;
667 if (entry->left != NULL && entry->left->max_free > entry->max_free)
668 entry->max_free = entry->left->max_free;
669 if (entry->right != NULL && entry->right->max_free > entry->max_free)
670 entry->max_free = entry->right->max_free;
674 * vm_map_entry_splay:
676 * The Sleator and Tarjan top-down splay algorithm with the
677 * following variation. Max_free must be computed bottom-up, so
678 * on the downward pass, maintain the left and right spines in
679 * reverse order. Then, make a second pass up each side to fix
680 * the pointers and compute max_free. The time bound is O(log n)
683 * The new root is the vm_map_entry containing "addr", or else an
684 * adjacent entry (lower or higher) if addr is not in the tree.
686 * The map must be locked, and leaves it so.
688 * Returns: the new root.
690 static vm_map_entry_t
691 vm_map_entry_splay(vm_offset_t addr, vm_map_entry_t root)
693 vm_map_entry_t llist, rlist;
694 vm_map_entry_t ltree, rtree;
697 /* Special case of empty tree. */
702 * Pass One: Splay down the tree until we find addr or a NULL
703 * pointer where addr would go. llist and rlist are the two
704 * sides in reverse order (bottom-up), with llist linked by
705 * the right pointer and rlist linked by the left pointer in
706 * the vm_map_entry. Wait until Pass Two to set max_free on
712 /* root is never NULL in here. */
713 if (addr < root->start) {
717 if (addr < y->start && y->left != NULL) {
718 /* Rotate right and put y on rlist. */
719 root->left = y->right;
721 vm_map_entry_set_max_free(root);
726 /* Put root on rlist. */
733 if (addr < root->end || y == NULL)
735 if (addr >= y->end && y->right != NULL) {
736 /* Rotate left and put y on llist. */
737 root->right = y->left;
739 vm_map_entry_set_max_free(root);
744 /* Put root on llist. */
753 * Pass Two: Walk back up the two spines, flip the pointers
754 * and set max_free. The subtrees of the root go at the
755 * bottom of llist and rlist.
758 while (llist != NULL) {
760 llist->right = ltree;
761 vm_map_entry_set_max_free(llist);
766 while (rlist != NULL) {
769 vm_map_entry_set_max_free(rlist);
775 * Final assembly: add ltree and rtree as subtrees of root.
779 vm_map_entry_set_max_free(root);
785 * vm_map_entry_{un,}link:
787 * Insert/remove entries from maps.
790 vm_map_entry_link(vm_map_t map,
791 vm_map_entry_t after_where,
792 vm_map_entry_t entry)
796 "vm_map_entry_link: map %p, nentries %d, entry %p, after %p", map,
797 map->nentries, entry, after_where);
799 entry->prev = after_where;
800 entry->next = after_where->next;
801 entry->next->prev = entry;
802 after_where->next = entry;
804 if (after_where != &map->header) {
805 if (after_where != map->root)
806 vm_map_entry_splay(after_where->start, map->root);
807 entry->right = after_where->right;
808 entry->left = after_where;
809 after_where->right = NULL;
810 after_where->adj_free = entry->start - after_where->end;
811 vm_map_entry_set_max_free(after_where);
813 entry->right = map->root;
816 entry->adj_free = (entry->next == &map->header ? map->max_offset :
817 entry->next->start) - entry->end;
818 vm_map_entry_set_max_free(entry);
823 vm_map_entry_unlink(vm_map_t map,
824 vm_map_entry_t entry)
826 vm_map_entry_t next, prev, root;
828 if (entry != map->root)
829 vm_map_entry_splay(entry->start, map->root);
830 if (entry->left == NULL)
833 root = vm_map_entry_splay(entry->start, entry->left);
834 root->right = entry->right;
835 root->adj_free = (entry->next == &map->header ? map->max_offset :
836 entry->next->start) - root->end;
837 vm_map_entry_set_max_free(root);
846 CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map,
847 map->nentries, entry);
851 * vm_map_entry_resize_free:
853 * Recompute the amount of free space following a vm_map_entry
854 * and propagate that value up the tree. Call this function after
855 * resizing a map entry in-place, that is, without a call to
856 * vm_map_entry_link() or _unlink().
858 * The map must be locked, and leaves it so.
861 vm_map_entry_resize_free(vm_map_t map, vm_map_entry_t entry)
865 * Using splay trees without parent pointers, propagating
866 * max_free up the tree is done by moving the entry to the
867 * root and making the change there.
869 if (entry != map->root)
870 map->root = vm_map_entry_splay(entry->start, map->root);
872 entry->adj_free = (entry->next == &map->header ? map->max_offset :
873 entry->next->start) - entry->end;
874 vm_map_entry_set_max_free(entry);
878 * vm_map_lookup_entry: [ internal use only ]
880 * Finds the map entry containing (or
881 * immediately preceding) the specified address
882 * in the given map; the entry is returned
883 * in the "entry" parameter. The boolean
884 * result indicates whether the address is
885 * actually contained in the map.
891 vm_map_entry_t *entry) /* OUT */
895 cur = vm_map_entry_splay(address, map->root);
897 *entry = &map->header;
901 if (address >= cur->start) {
903 if (cur->end > address)
914 * Inserts the given whole VM object into the target
915 * map at the specified address range. The object's
916 * size should match that of the address range.
918 * Requires that the map be locked, and leaves it so.
920 * If object is non-NULL, ref count must be bumped by caller
921 * prior to making call to account for the new entry.
924 vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
925 vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max,
928 vm_map_entry_t new_entry;
929 vm_map_entry_t prev_entry;
930 vm_map_entry_t temp_entry;
931 vm_eflags_t protoeflags;
934 * Check that the start and end points are not bogus.
936 if ((start < map->min_offset) || (end > map->max_offset) ||
938 return (KERN_INVALID_ADDRESS);
941 * Find the entry prior to the proposed starting address; if it's part
942 * of an existing entry, this range is bogus.
944 if (vm_map_lookup_entry(map, start, &temp_entry))
945 return (KERN_NO_SPACE);
947 prev_entry = temp_entry;
950 * Assert that the next entry doesn't overlap the end point.
952 if ((prev_entry->next != &map->header) &&
953 (prev_entry->next->start < end))
954 return (KERN_NO_SPACE);
958 if (cow & MAP_COPY_ON_WRITE)
959 protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
961 if (cow & MAP_NOFAULT) {
962 protoeflags |= MAP_ENTRY_NOFAULT;
964 KASSERT(object == NULL,
965 ("vm_map_insert: paradoxical MAP_NOFAULT request"));
967 if (cow & MAP_DISABLE_SYNCER)
968 protoeflags |= MAP_ENTRY_NOSYNC;
969 if (cow & MAP_DISABLE_COREDUMP)
970 protoeflags |= MAP_ENTRY_NOCOREDUMP;
972 if (object != NULL) {
974 * OBJ_ONEMAPPING must be cleared unless this mapping
975 * is trivially proven to be the only mapping for any
976 * of the object's pages. (Object granularity
977 * reference counting is insufficient to recognize
978 * aliases with precision.)
980 VM_OBJECT_LOCK(object);
981 if (object->ref_count > 1 || object->shadow_count != 0)
982 vm_object_clear_flag(object, OBJ_ONEMAPPING);
983 VM_OBJECT_UNLOCK(object);
985 else if ((prev_entry != &map->header) &&
986 (prev_entry->eflags == protoeflags) &&
987 (prev_entry->end == start) &&
988 (prev_entry->wired_count == 0) &&
989 ((prev_entry->object.vm_object == NULL) ||
990 vm_object_coalesce(prev_entry->object.vm_object,
992 (vm_size_t)(prev_entry->end - prev_entry->start),
993 (vm_size_t)(end - prev_entry->end)))) {
995 * We were able to extend the object. Determine if we
996 * can extend the previous map entry to include the
999 if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
1000 (prev_entry->protection == prot) &&
1001 (prev_entry->max_protection == max)) {
1002 map->size += (end - prev_entry->end);
1003 prev_entry->end = end;
1004 vm_map_entry_resize_free(map, prev_entry);
1005 vm_map_simplify_entry(map, prev_entry);
1006 return (KERN_SUCCESS);
1010 * If we can extend the object but cannot extend the
1011 * map entry, we have to create a new map entry. We
1012 * must bump the ref count on the extended object to
1013 * account for it. object may be NULL.
1015 object = prev_entry->object.vm_object;
1016 offset = prev_entry->offset +
1017 (prev_entry->end - prev_entry->start);
1018 vm_object_reference(object);
1022 * NOTE: if conditionals fail, object can be NULL here. This occurs
1023 * in things like the buffer map where we manage kva but do not manage
1028 * Create a new entry
1030 new_entry = vm_map_entry_create(map);
1031 new_entry->start = start;
1032 new_entry->end = end;
1034 new_entry->eflags = protoeflags;
1035 new_entry->object.vm_object = object;
1036 new_entry->offset = offset;
1037 new_entry->avail_ssize = 0;
1039 new_entry->inheritance = VM_INHERIT_DEFAULT;
1040 new_entry->protection = prot;
1041 new_entry->max_protection = max;
1042 new_entry->wired_count = 0;
1045 * Insert the new entry into the list
1047 vm_map_entry_link(map, prev_entry, new_entry);
1048 map->size += new_entry->end - new_entry->start;
1052 * Temporarily removed to avoid MAP_STACK panic, due to
1053 * MAP_STACK being a huge hack. Will be added back in
1054 * when MAP_STACK (and the user stack mapping) is fixed.
1057 * It may be possible to simplify the entry
1059 vm_map_simplify_entry(map, new_entry);
1062 if (cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) {
1063 vm_map_pmap_enter(map, start, prot,
1064 object, OFF_TO_IDX(offset), end - start,
1065 cow & MAP_PREFAULT_PARTIAL);
1068 return (KERN_SUCCESS);
1074 * Find the first fit (lowest VM address) for "length" free bytes
1075 * beginning at address >= start in the given map.
1077 * In a vm_map_entry, "adj_free" is the amount of free space
1078 * adjacent (higher address) to this entry, and "max_free" is the
1079 * maximum amount of contiguous free space in its subtree. This
1080 * allows finding a free region in one path down the tree, so
1081 * O(log n) amortized with splay trees.
1083 * The map must be locked, and leaves it so.
1085 * Returns: 0 on success, and starting address in *addr,
1086 * 1 if insufficient space.
1089 vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length,
1090 vm_offset_t *addr) /* OUT */
1092 vm_map_entry_t entry;
1093 vm_offset_t end, st;
1096 * Request must fit within min/max VM address and must avoid
1099 if (start < map->min_offset)
1100 start = map->min_offset;
1101 if (start + length > map->max_offset || start + length < start)
1104 /* Empty tree means wide open address space. */
1105 if (map->root == NULL) {
1111 * After splay, if start comes before root node, then there
1112 * must be a gap from start to the root.
1114 map->root = vm_map_entry_splay(start, map->root);
1115 if (start + length <= map->root->start) {
1121 * Root is the last node that might begin its gap before
1122 * start, and this is the last comparison where address
1123 * wrap might be a problem.
1125 st = (start > map->root->end) ? start : map->root->end;
1126 if (length <= map->root->end + map->root->adj_free - st) {
1131 /* With max_free, can immediately tell if no solution. */
1132 entry = map->root->right;
1133 if (entry == NULL || length > entry->max_free)
1137 * Search the right subtree in the order: left subtree, root,
1138 * right subtree (first fit). The previous splay implies that
1139 * all regions in the right subtree have addresses > start.
1141 while (entry != NULL) {
1142 if (entry->left != NULL && entry->left->max_free >= length)
1143 entry = entry->left;
1144 else if (entry->adj_free >= length) {
1148 entry = entry->right;
1151 /* Can't get here, so panic if we do. */
1152 panic("vm_map_findspace: max_free corrupt");
1155 /* Expand the kernel pmap, if necessary. */
1156 if (map == kernel_map) {
1157 end = round_page(*addr + length);
1158 if (end > kernel_vm_end)
1159 pmap_growkernel(end);
1165 vm_map_fixed(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1166 vm_offset_t *addr /* IN/OUT */, vm_size_t length, vm_prot_t prot,
1167 vm_prot_t max, int cow)
1169 vm_offset_t start, end;
1174 end = start + length;
1175 VM_MAP_RANGE_CHECK(map, start, end);
1176 (void) vm_map_delete(map, start, end);
1177 result = vm_map_insert(map, object, offset, start, end, prot,
1184 * vm_map_find finds an unallocated region in the target address
1185 * map with the given length. The search is defined to be
1186 * first-fit from the specified address; the region found is
1187 * returned in the same parameter.
1189 * If object is non-NULL, ref count must be bumped by caller
1190 * prior to making call to account for the new entry.
1193 vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1194 vm_offset_t *addr, /* IN/OUT */
1195 vm_size_t length, boolean_t find_space, vm_prot_t prot,
1196 vm_prot_t max, int cow)
1204 if (vm_map_findspace(map, start, length, addr)) {
1206 return (KERN_NO_SPACE);
1210 result = vm_map_insert(map, object, offset,
1211 start, start + length, prot, max, cow);
1217 * vm_map_simplify_entry:
1219 * Simplify the given map entry by merging with either neighbor. This
1220 * routine also has the ability to merge with both neighbors.
1222 * The map must be locked.
1224 * This routine guarentees that the passed entry remains valid (though
1225 * possibly extended). When merging, this routine may delete one or
1229 vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry)
1231 vm_map_entry_t next, prev;
1232 vm_size_t prevsize, esize;
1234 if (entry->eflags & (MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP))
1238 if (prev != &map->header) {
1239 prevsize = prev->end - prev->start;
1240 if ( (prev->end == entry->start) &&
1241 (prev->object.vm_object == entry->object.vm_object) &&
1242 (!prev->object.vm_object ||
1243 (prev->offset + prevsize == entry->offset)) &&
1244 (prev->eflags == entry->eflags) &&
1245 (prev->protection == entry->protection) &&
1246 (prev->max_protection == entry->max_protection) &&
1247 (prev->inheritance == entry->inheritance) &&
1248 (prev->wired_count == entry->wired_count)) {
1249 vm_map_entry_unlink(map, prev);
1250 entry->start = prev->start;
1251 entry->offset = prev->offset;
1252 if (entry->prev != &map->header)
1253 vm_map_entry_resize_free(map, entry->prev);
1254 if (prev->object.vm_object)
1255 vm_object_deallocate(prev->object.vm_object);
1256 vm_map_entry_dispose(map, prev);
1261 if (next != &map->header) {
1262 esize = entry->end - entry->start;
1263 if ((entry->end == next->start) &&
1264 (next->object.vm_object == entry->object.vm_object) &&
1265 (!entry->object.vm_object ||
1266 (entry->offset + esize == next->offset)) &&
1267 (next->eflags == entry->eflags) &&
1268 (next->protection == entry->protection) &&
1269 (next->max_protection == entry->max_protection) &&
1270 (next->inheritance == entry->inheritance) &&
1271 (next->wired_count == entry->wired_count)) {
1272 vm_map_entry_unlink(map, next);
1273 entry->end = next->end;
1274 vm_map_entry_resize_free(map, entry);
1275 if (next->object.vm_object)
1276 vm_object_deallocate(next->object.vm_object);
1277 vm_map_entry_dispose(map, next);
1282 * vm_map_clip_start: [ internal use only ]
1284 * Asserts that the given entry begins at or after
1285 * the specified address; if necessary,
1286 * it splits the entry into two.
1288 #define vm_map_clip_start(map, entry, startaddr) \
1290 if (startaddr > entry->start) \
1291 _vm_map_clip_start(map, entry, startaddr); \
1295 * This routine is called only when it is known that
1296 * the entry must be split.
1299 _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start)
1301 vm_map_entry_t new_entry;
1304 * Split off the front portion -- note that we must insert the new
1305 * entry BEFORE this one, so that this entry has the specified
1308 vm_map_simplify_entry(map, entry);
1311 * If there is no object backing this entry, we might as well create
1312 * one now. If we defer it, an object can get created after the map
1313 * is clipped, and individual objects will be created for the split-up
1314 * map. This is a bit of a hack, but is also about the best place to
1315 * put this improvement.
1317 if (entry->object.vm_object == NULL && !map->system_map) {
1319 object = vm_object_allocate(OBJT_DEFAULT,
1320 atop(entry->end - entry->start));
1321 entry->object.vm_object = object;
1325 new_entry = vm_map_entry_create(map);
1326 *new_entry = *entry;
1328 new_entry->end = start;
1329 entry->offset += (start - entry->start);
1330 entry->start = start;
1332 vm_map_entry_link(map, entry->prev, new_entry);
1334 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1335 vm_object_reference(new_entry->object.vm_object);
1340 * vm_map_clip_end: [ internal use only ]
1342 * Asserts that the given entry ends at or before
1343 * the specified address; if necessary,
1344 * it splits the entry into two.
1346 #define vm_map_clip_end(map, entry, endaddr) \
1348 if ((endaddr) < (entry->end)) \
1349 _vm_map_clip_end((map), (entry), (endaddr)); \
1353 * This routine is called only when it is known that
1354 * the entry must be split.
1357 _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end)
1359 vm_map_entry_t new_entry;
1362 * If there is no object backing this entry, we might as well create
1363 * one now. If we defer it, an object can get created after the map
1364 * is clipped, and individual objects will be created for the split-up
1365 * map. This is a bit of a hack, but is also about the best place to
1366 * put this improvement.
1368 if (entry->object.vm_object == NULL && !map->system_map) {
1370 object = vm_object_allocate(OBJT_DEFAULT,
1371 atop(entry->end - entry->start));
1372 entry->object.vm_object = object;
1377 * Create a new entry and insert it AFTER the specified entry
1379 new_entry = vm_map_entry_create(map);
1380 *new_entry = *entry;
1382 new_entry->start = entry->end = end;
1383 new_entry->offset += (end - entry->start);
1385 vm_map_entry_link(map, entry, new_entry);
1387 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1388 vm_object_reference(new_entry->object.vm_object);
1393 * vm_map_submap: [ kernel use only ]
1395 * Mark the given range as handled by a subordinate map.
1397 * This range must have been created with vm_map_find,
1398 * and no other operations may have been performed on this
1399 * range prior to calling vm_map_submap.
1401 * Only a limited number of operations can be performed
1402 * within this rage after calling vm_map_submap:
1404 * [Don't try vm_map_copy!]
1406 * To remove a submapping, one must first remove the
1407 * range from the superior map, and then destroy the
1408 * submap (if desired). [Better yet, don't try it.]
1417 vm_map_entry_t entry;
1418 int result = KERN_INVALID_ARGUMENT;
1422 VM_MAP_RANGE_CHECK(map, start, end);
1424 if (vm_map_lookup_entry(map, start, &entry)) {
1425 vm_map_clip_start(map, entry, start);
1427 entry = entry->next;
1429 vm_map_clip_end(map, entry, end);
1431 if ((entry->start == start) && (entry->end == end) &&
1432 ((entry->eflags & MAP_ENTRY_COW) == 0) &&
1433 (entry->object.vm_object == NULL)) {
1434 entry->object.sub_map = submap;
1435 entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
1436 result = KERN_SUCCESS;
1444 * The maximum number of pages to map
1446 #define MAX_INIT_PT 96
1449 * vm_map_pmap_enter:
1451 * Preload read-only mappings for the given object's resident pages into
1452 * the given map. This eliminates the soft faults on process startup and
1453 * immediately after an mmap(2). Unless the given flags include
1454 * MAP_PREFAULT_MADVISE, cached pages are not reactivated and mapped.
1457 vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
1458 vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags)
1461 vm_page_t p, p_start;
1462 vm_pindex_t psize, tmpidx;
1463 boolean_t are_queues_locked;
1465 if ((prot & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 || object == NULL)
1467 VM_OBJECT_LOCK(object);
1468 if (object->type == OBJT_DEVICE) {
1469 pmap_object_init_pt(map->pmap, addr, object, pindex, size);
1475 if (object->type != OBJT_VNODE ||
1476 ((flags & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
1477 (object->resident_page_count > MAX_INIT_PT))) {
1481 if (psize + pindex > object->size) {
1482 if (object->size < pindex)
1484 psize = object->size - pindex;
1487 are_queues_locked = FALSE;
1491 if ((p = TAILQ_FIRST(&object->memq)) != NULL) {
1492 if (p->pindex < pindex) {
1493 p = vm_page_splay(pindex, object->root);
1494 if ((object->root = p)->pindex < pindex)
1495 p = TAILQ_NEXT(p, listq);
1499 * Assert: the variable p is either (1) the page with the
1500 * least pindex greater than or equal to the parameter pindex
1504 p != NULL && (tmpidx = p->pindex - pindex) < psize;
1505 p = TAILQ_NEXT(p, listq)) {
1507 * don't allow an madvise to blow away our really
1508 * free pages allocating pv entries.
1510 if ((flags & MAP_PREFAULT_MADVISE) &&
1511 cnt.v_free_count < cnt.v_free_reserved) {
1515 if ((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL &&
1517 if (p_start == NULL) {
1518 start = addr + ptoa(tmpidx);
1521 } else if (p_start != NULL) {
1522 if (!are_queues_locked) {
1523 are_queues_locked = TRUE;
1524 vm_page_lock_queues();
1526 pmap_enter_object(map->pmap, start, addr +
1527 ptoa(tmpidx), p_start, prot);
1531 if (p_start != NULL) {
1532 if (!are_queues_locked) {
1533 are_queues_locked = TRUE;
1534 vm_page_lock_queues();
1536 pmap_enter_object(map->pmap, start, addr + ptoa(psize),
1539 if (are_queues_locked)
1540 vm_page_unlock_queues();
1542 VM_OBJECT_UNLOCK(object);
1548 * Sets the protection of the specified address
1549 * region in the target map. If "set_max" is
1550 * specified, the maximum protection is to be set;
1551 * otherwise, only the current protection is affected.
1554 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
1555 vm_prot_t new_prot, boolean_t set_max)
1557 vm_map_entry_t current;
1558 vm_map_entry_t entry;
1562 VM_MAP_RANGE_CHECK(map, start, end);
1564 if (vm_map_lookup_entry(map, start, &entry)) {
1565 vm_map_clip_start(map, entry, start);
1567 entry = entry->next;
1571 * Make a first pass to check for protection violations.
1574 while ((current != &map->header) && (current->start < end)) {
1575 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1577 return (KERN_INVALID_ARGUMENT);
1579 if ((new_prot & current->max_protection) != new_prot) {
1581 return (KERN_PROTECTION_FAILURE);
1583 current = current->next;
1587 * Go back and fix up protections. [Note that clipping is not
1588 * necessary the second time.]
1591 while ((current != &map->header) && (current->start < end)) {
1594 vm_map_clip_end(map, current, end);
1596 old_prot = current->protection;
1598 current->protection =
1599 (current->max_protection = new_prot) &
1602 current->protection = new_prot;
1605 * Update physical map if necessary. Worry about copy-on-write
1606 * here -- CHECK THIS XXX
1608 if (current->protection != old_prot) {
1609 #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1611 pmap_protect(map->pmap, current->start,
1613 current->protection & MASK(current));
1616 vm_map_simplify_entry(map, current);
1617 current = current->next;
1620 return (KERN_SUCCESS);
1626 * This routine traverses a processes map handling the madvise
1627 * system call. Advisories are classified as either those effecting
1628 * the vm_map_entry structure, or those effecting the underlying
1638 vm_map_entry_t current, entry;
1642 * Some madvise calls directly modify the vm_map_entry, in which case
1643 * we need to use an exclusive lock on the map and we need to perform
1644 * various clipping operations. Otherwise we only need a read-lock
1649 case MADV_SEQUENTIAL:
1661 vm_map_lock_read(map);
1664 return (KERN_INVALID_ARGUMENT);
1668 * Locate starting entry and clip if necessary.
1670 VM_MAP_RANGE_CHECK(map, start, end);
1672 if (vm_map_lookup_entry(map, start, &entry)) {
1674 vm_map_clip_start(map, entry, start);
1676 entry = entry->next;
1681 * madvise behaviors that are implemented in the vm_map_entry.
1683 * We clip the vm_map_entry so that behavioral changes are
1684 * limited to the specified address range.
1686 for (current = entry;
1687 (current != &map->header) && (current->start < end);
1688 current = current->next
1690 if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1693 vm_map_clip_end(map, current, end);
1697 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
1699 case MADV_SEQUENTIAL:
1700 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
1703 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
1706 current->eflags |= MAP_ENTRY_NOSYNC;
1709 current->eflags &= ~MAP_ENTRY_NOSYNC;
1712 current->eflags |= MAP_ENTRY_NOCOREDUMP;
1715 current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
1720 vm_map_simplify_entry(map, current);
1728 * madvise behaviors that are implemented in the underlying
1731 * Since we don't clip the vm_map_entry, we have to clip
1732 * the vm_object pindex and count.
1734 for (current = entry;
1735 (current != &map->header) && (current->start < end);
1736 current = current->next
1738 vm_offset_t useStart;
1740 if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1743 pindex = OFF_TO_IDX(current->offset);
1744 count = atop(current->end - current->start);
1745 useStart = current->start;
1747 if (current->start < start) {
1748 pindex += atop(start - current->start);
1749 count -= atop(start - current->start);
1752 if (current->end > end)
1753 count -= atop(current->end - end);
1758 vm_object_madvise(current->object.vm_object,
1759 pindex, count, behav);
1760 if (behav == MADV_WILLNEED) {
1761 vm_map_pmap_enter(map,
1763 current->protection,
1764 current->object.vm_object,
1766 (count << PAGE_SHIFT),
1767 MAP_PREFAULT_MADVISE
1771 vm_map_unlock_read(map);
1780 * Sets the inheritance of the specified address
1781 * range in the target map. Inheritance
1782 * affects how the map will be shared with
1783 * child maps at the time of vm_map_fork.
1786 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
1787 vm_inherit_t new_inheritance)
1789 vm_map_entry_t entry;
1790 vm_map_entry_t temp_entry;
1792 switch (new_inheritance) {
1793 case VM_INHERIT_NONE:
1794 case VM_INHERIT_COPY:
1795 case VM_INHERIT_SHARE:
1798 return (KERN_INVALID_ARGUMENT);
1801 VM_MAP_RANGE_CHECK(map, start, end);
1802 if (vm_map_lookup_entry(map, start, &temp_entry)) {
1804 vm_map_clip_start(map, entry, start);
1806 entry = temp_entry->next;
1807 while ((entry != &map->header) && (entry->start < end)) {
1808 vm_map_clip_end(map, entry, end);
1809 entry->inheritance = new_inheritance;
1810 vm_map_simplify_entry(map, entry);
1811 entry = entry->next;
1814 return (KERN_SUCCESS);
1820 * Implements both kernel and user unwiring.
1823 vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
1826 vm_map_entry_t entry, first_entry, tmp_entry;
1827 vm_offset_t saved_start;
1828 unsigned int last_timestamp;
1830 boolean_t need_wakeup, result, user_unwire;
1832 user_unwire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
1834 VM_MAP_RANGE_CHECK(map, start, end);
1835 if (!vm_map_lookup_entry(map, start, &first_entry)) {
1836 if (flags & VM_MAP_WIRE_HOLESOK)
1837 first_entry = first_entry->next;
1840 return (KERN_INVALID_ADDRESS);
1843 last_timestamp = map->timestamp;
1844 entry = first_entry;
1845 while (entry != &map->header && entry->start < end) {
1846 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1848 * We have not yet clipped the entry.
1850 saved_start = (start >= entry->start) ? start :
1852 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1853 if (vm_map_unlock_and_wait(map, user_unwire)) {
1855 * Allow interruption of user unwiring?
1859 if (last_timestamp+1 != map->timestamp) {
1861 * Look again for the entry because the map was
1862 * modified while it was unlocked.
1863 * Specifically, the entry may have been
1864 * clipped, merged, or deleted.
1866 if (!vm_map_lookup_entry(map, saved_start,
1868 if (flags & VM_MAP_WIRE_HOLESOK)
1869 tmp_entry = tmp_entry->next;
1871 if (saved_start == start) {
1873 * First_entry has been deleted.
1876 return (KERN_INVALID_ADDRESS);
1879 rv = KERN_INVALID_ADDRESS;
1883 if (entry == first_entry)
1884 first_entry = tmp_entry;
1889 last_timestamp = map->timestamp;
1892 vm_map_clip_start(map, entry, start);
1893 vm_map_clip_end(map, entry, end);
1895 * Mark the entry in case the map lock is released. (See
1898 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
1900 * Check the map for holes in the specified region.
1901 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
1903 if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
1904 (entry->end < end && (entry->next == &map->header ||
1905 entry->next->start > entry->end))) {
1907 rv = KERN_INVALID_ADDRESS;
1911 * If system unwiring, require that the entry is system wired.
1914 vm_map_entry_system_wired_count(entry) == 0) {
1916 rv = KERN_INVALID_ARGUMENT;
1919 entry = entry->next;
1923 need_wakeup = FALSE;
1924 if (first_entry == NULL) {
1925 result = vm_map_lookup_entry(map, start, &first_entry);
1926 if (!result && (flags & VM_MAP_WIRE_HOLESOK))
1927 first_entry = first_entry->next;
1929 KASSERT(result, ("vm_map_unwire: lookup failed"));
1931 entry = first_entry;
1932 while (entry != &map->header && entry->start < end) {
1933 if (rv == KERN_SUCCESS && (!user_unwire ||
1934 (entry->eflags & MAP_ENTRY_USER_WIRED))) {
1936 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
1937 entry->wired_count--;
1938 if (entry->wired_count == 0) {
1940 * Retain the map lock.
1942 vm_fault_unwire(map, entry->start, entry->end,
1943 entry->object.vm_object != NULL &&
1944 entry->object.vm_object->type == OBJT_DEVICE);
1947 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
1948 ("vm_map_unwire: in-transition flag missing"));
1949 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
1950 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
1951 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
1954 vm_map_simplify_entry(map, entry);
1955 entry = entry->next;
1966 * Implements both kernel and user wiring.
1969 vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
1972 vm_map_entry_t entry, first_entry, tmp_entry;
1973 vm_offset_t saved_end, saved_start;
1974 unsigned int last_timestamp;
1976 boolean_t fictitious, need_wakeup, result, user_wire;
1978 user_wire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
1980 VM_MAP_RANGE_CHECK(map, start, end);
1981 if (!vm_map_lookup_entry(map, start, &first_entry)) {
1982 if (flags & VM_MAP_WIRE_HOLESOK)
1983 first_entry = first_entry->next;
1986 return (KERN_INVALID_ADDRESS);
1989 last_timestamp = map->timestamp;
1990 entry = first_entry;
1991 while (entry != &map->header && entry->start < end) {
1992 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1994 * We have not yet clipped the entry.
1996 saved_start = (start >= entry->start) ? start :
1998 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1999 if (vm_map_unlock_and_wait(map, user_wire)) {
2001 * Allow interruption of user wiring?
2005 if (last_timestamp + 1 != map->timestamp) {
2007 * Look again for the entry because the map was
2008 * modified while it was unlocked.
2009 * Specifically, the entry may have been
2010 * clipped, merged, or deleted.
2012 if (!vm_map_lookup_entry(map, saved_start,
2014 if (flags & VM_MAP_WIRE_HOLESOK)
2015 tmp_entry = tmp_entry->next;
2017 if (saved_start == start) {
2019 * first_entry has been deleted.
2022 return (KERN_INVALID_ADDRESS);
2025 rv = KERN_INVALID_ADDRESS;
2029 if (entry == first_entry)
2030 first_entry = tmp_entry;
2035 last_timestamp = map->timestamp;
2038 vm_map_clip_start(map, entry, start);
2039 vm_map_clip_end(map, entry, end);
2041 * Mark the entry in case the map lock is released. (See
2044 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
2048 if (entry->wired_count == 0) {
2049 entry->wired_count++;
2050 saved_start = entry->start;
2051 saved_end = entry->end;
2052 fictitious = entry->object.vm_object != NULL &&
2053 entry->object.vm_object->type == OBJT_DEVICE;
2055 * Release the map lock, relying on the in-transition
2059 rv = vm_fault_wire(map, saved_start, saved_end,
2060 user_wire, fictitious);
2062 if (last_timestamp + 1 != map->timestamp) {
2064 * Look again for the entry because the map was
2065 * modified while it was unlocked. The entry
2066 * may have been clipped, but NOT merged or
2069 result = vm_map_lookup_entry(map, saved_start,
2071 KASSERT(result, ("vm_map_wire: lookup failed"));
2072 if (entry == first_entry)
2073 first_entry = tmp_entry;
2077 while (entry->end < saved_end) {
2078 if (rv != KERN_SUCCESS) {
2079 KASSERT(entry->wired_count == 1,
2080 ("vm_map_wire: bad count"));
2081 entry->wired_count = -1;
2083 entry = entry->next;
2086 last_timestamp = map->timestamp;
2087 if (rv != KERN_SUCCESS) {
2088 KASSERT(entry->wired_count == 1,
2089 ("vm_map_wire: bad count"));
2091 * Assign an out-of-range value to represent
2092 * the failure to wire this entry.
2094 entry->wired_count = -1;
2098 } else if (!user_wire ||
2099 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
2100 entry->wired_count++;
2103 * Check the map for holes in the specified region.
2104 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
2106 if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
2107 (entry->end < end && (entry->next == &map->header ||
2108 entry->next->start > entry->end))) {
2110 rv = KERN_INVALID_ADDRESS;
2113 entry = entry->next;
2117 need_wakeup = FALSE;
2118 if (first_entry == NULL) {
2119 result = vm_map_lookup_entry(map, start, &first_entry);
2120 if (!result && (flags & VM_MAP_WIRE_HOLESOK))
2121 first_entry = first_entry->next;
2123 KASSERT(result, ("vm_map_wire: lookup failed"));
2125 entry = first_entry;
2126 while (entry != &map->header && entry->start < end) {
2127 if (rv == KERN_SUCCESS) {
2129 entry->eflags |= MAP_ENTRY_USER_WIRED;
2130 } else if (entry->wired_count == -1) {
2132 * Wiring failed on this entry. Thus, unwiring is
2135 entry->wired_count = 0;
2138 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0)
2139 entry->wired_count--;
2140 if (entry->wired_count == 0) {
2142 * Retain the map lock.
2144 vm_fault_unwire(map, entry->start, entry->end,
2145 entry->object.vm_object != NULL &&
2146 entry->object.vm_object->type == OBJT_DEVICE);
2149 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
2150 ("vm_map_wire: in-transition flag missing"));
2151 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
2152 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
2153 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
2156 vm_map_simplify_entry(map, entry);
2157 entry = entry->next;
2168 * Push any dirty cached pages in the address range to their pager.
2169 * If syncio is TRUE, dirty pages are written synchronously.
2170 * If invalidate is TRUE, any cached pages are freed as well.
2172 * If the size of the region from start to end is zero, we are
2173 * supposed to flush all modified pages within the region containing
2174 * start. Unfortunately, a region can be split or coalesced with
2175 * neighboring regions, making it difficult to determine what the
2176 * original region was. Therefore, we approximate this requirement by
2177 * flushing the current region containing start.
2179 * Returns an error if any part of the specified range is not mapped.
2187 boolean_t invalidate)
2189 vm_map_entry_t current;
2190 vm_map_entry_t entry;
2193 vm_ooffset_t offset;
2195 vm_map_lock_read(map);
2196 VM_MAP_RANGE_CHECK(map, start, end);
2197 if (!vm_map_lookup_entry(map, start, &entry)) {
2198 vm_map_unlock_read(map);
2199 return (KERN_INVALID_ADDRESS);
2200 } else if (start == end) {
2201 start = entry->start;
2205 * Make a first pass to check for user-wired memory and holes.
2207 for (current = entry; current->start < end; current = current->next) {
2208 if (invalidate && (current->eflags & MAP_ENTRY_USER_WIRED)) {
2209 vm_map_unlock_read(map);
2210 return (KERN_INVALID_ARGUMENT);
2212 if (end > current->end &&
2213 (current->next == &map->header ||
2214 current->end != current->next->start)) {
2215 vm_map_unlock_read(map);
2216 return (KERN_INVALID_ADDRESS);
2221 pmap_remove(map->pmap, start, end);
2224 * Make a second pass, cleaning/uncaching pages from the indicated
2227 for (current = entry; current->start < end; current = current->next) {
2228 offset = current->offset + (start - current->start);
2229 size = (end <= current->end ? end : current->end) - start;
2230 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
2232 vm_map_entry_t tentry;
2235 smap = current->object.sub_map;
2236 vm_map_lock_read(smap);
2237 (void) vm_map_lookup_entry(smap, offset, &tentry);
2238 tsize = tentry->end - offset;
2241 object = tentry->object.vm_object;
2242 offset = tentry->offset + (offset - tentry->start);
2243 vm_map_unlock_read(smap);
2245 object = current->object.vm_object;
2247 vm_object_sync(object, offset, size, syncio, invalidate);
2251 vm_map_unlock_read(map);
2252 return (KERN_SUCCESS);
2256 * vm_map_entry_unwire: [ internal use only ]
2258 * Make the region specified by this entry pageable.
2260 * The map in question should be locked.
2261 * [This is the reason for this routine's existence.]
2264 vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
2266 vm_fault_unwire(map, entry->start, entry->end,
2267 entry->object.vm_object != NULL &&
2268 entry->object.vm_object->type == OBJT_DEVICE);
2269 entry->wired_count = 0;
2273 * vm_map_entry_delete: [ internal use only ]
2275 * Deallocate the given entry from the target map.
2278 vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry)
2281 vm_pindex_t offidxstart, offidxend, count;
2283 vm_map_entry_unlink(map, entry);
2284 map->size -= entry->end - entry->start;
2286 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
2287 (object = entry->object.vm_object) != NULL) {
2288 count = OFF_TO_IDX(entry->end - entry->start);
2289 offidxstart = OFF_TO_IDX(entry->offset);
2290 offidxend = offidxstart + count;
2291 VM_OBJECT_LOCK(object);
2292 if (object->ref_count != 1 &&
2293 ((object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING ||
2294 object == kernel_object || object == kmem_object)) {
2295 vm_object_collapse(object);
2296 vm_object_page_remove(object, offidxstart, offidxend, FALSE);
2297 if (object->type == OBJT_SWAP)
2298 swap_pager_freespace(object, offidxstart, count);
2299 if (offidxend >= object->size &&
2300 offidxstart < object->size)
2301 object->size = offidxstart;
2303 VM_OBJECT_UNLOCK(object);
2304 vm_object_deallocate(object);
2307 vm_map_entry_dispose(map, entry);
2311 * vm_map_delete: [ internal use only ]
2313 * Deallocates the given address range from the target
2317 vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end)
2319 vm_map_entry_t entry;
2320 vm_map_entry_t first_entry;
2323 * Find the start of the region, and clip it
2325 if (!vm_map_lookup_entry(map, start, &first_entry))
2326 entry = first_entry->next;
2328 entry = first_entry;
2329 vm_map_clip_start(map, entry, start);
2333 * Step through all entries in this region
2335 while ((entry != &map->header) && (entry->start < end)) {
2336 vm_map_entry_t next;
2339 * Wait for wiring or unwiring of an entry to complete.
2340 * Also wait for any system wirings to disappear on
2343 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 ||
2344 (vm_map_pmap(map) != kernel_pmap &&
2345 vm_map_entry_system_wired_count(entry) != 0)) {
2346 unsigned int last_timestamp;
2347 vm_offset_t saved_start;
2348 vm_map_entry_t tmp_entry;
2350 saved_start = entry->start;
2351 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2352 last_timestamp = map->timestamp;
2353 (void) vm_map_unlock_and_wait(map, FALSE);
2355 if (last_timestamp + 1 != map->timestamp) {
2357 * Look again for the entry because the map was
2358 * modified while it was unlocked.
2359 * Specifically, the entry may have been
2360 * clipped, merged, or deleted.
2362 if (!vm_map_lookup_entry(map, saved_start,
2364 entry = tmp_entry->next;
2367 vm_map_clip_start(map, entry,
2373 vm_map_clip_end(map, entry, end);
2378 * Unwire before removing addresses from the pmap; otherwise,
2379 * unwiring will put the entries back in the pmap.
2381 if (entry->wired_count != 0) {
2382 vm_map_entry_unwire(map, entry);
2385 pmap_remove(map->pmap, entry->start, entry->end);
2388 * Delete the entry (which may delete the object) only after
2389 * removing all pmap entries pointing to its pages.
2390 * (Otherwise, its page frames may be reallocated, and any
2391 * modify bits will be set in the wrong object!)
2393 vm_map_entry_delete(map, entry);
2396 return (KERN_SUCCESS);
2402 * Remove the given address range from the target map.
2403 * This is the exported form of vm_map_delete.
2406 vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
2411 VM_MAP_RANGE_CHECK(map, start, end);
2412 result = vm_map_delete(map, start, end);
2418 * vm_map_check_protection:
2420 * Assert that the target map allows the specified privilege on the
2421 * entire address region given. The entire region must be allocated.
2423 * WARNING! This code does not and should not check whether the
2424 * contents of the region is accessible. For example a smaller file
2425 * might be mapped into a larger address space.
2427 * NOTE! This code is also called by munmap().
2429 * The map must be locked. A read lock is sufficient.
2432 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
2433 vm_prot_t protection)
2435 vm_map_entry_t entry;
2436 vm_map_entry_t tmp_entry;
2438 if (!vm_map_lookup_entry(map, start, &tmp_entry))
2442 while (start < end) {
2443 if (entry == &map->header)
2448 if (start < entry->start)
2451 * Check protection associated with entry.
2453 if ((entry->protection & protection) != protection)
2455 /* go to next entry */
2457 entry = entry->next;
2463 * vm_map_copy_entry:
2465 * Copies the contents of the source entry to the destination
2466 * entry. The entries *must* be aligned properly.
2472 vm_map_entry_t src_entry,
2473 vm_map_entry_t dst_entry)
2475 vm_object_t src_object;
2477 if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
2480 if (src_entry->wired_count == 0) {
2483 * If the source entry is marked needs_copy, it is already
2486 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
2487 pmap_protect(src_map->pmap,
2490 src_entry->protection & ~VM_PROT_WRITE);
2494 * Make a copy of the object.
2496 if ((src_object = src_entry->object.vm_object) != NULL) {
2497 VM_OBJECT_LOCK(src_object);
2498 if ((src_object->handle == NULL) &&
2499 (src_object->type == OBJT_DEFAULT ||
2500 src_object->type == OBJT_SWAP)) {
2501 vm_object_collapse(src_object);
2502 if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
2503 vm_object_split(src_entry);
2504 src_object = src_entry->object.vm_object;
2507 vm_object_reference_locked(src_object);
2508 vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
2509 VM_OBJECT_UNLOCK(src_object);
2510 dst_entry->object.vm_object = src_object;
2511 src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2512 dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2513 dst_entry->offset = src_entry->offset;
2515 dst_entry->object.vm_object = NULL;
2516 dst_entry->offset = 0;
2519 pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
2520 dst_entry->end - dst_entry->start, src_entry->start);
2523 * Of course, wired down pages can't be set copy-on-write.
2524 * Cause wired pages to be copied into the new map by
2525 * simulating faults (the new pages are pageable)
2527 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry);
2532 * vmspace_map_entry_forked:
2533 * Update the newly-forked vmspace each time a map entry is inherited
2534 * or copied. The values for vm_dsize and vm_tsize are approximate
2535 * (and mostly-obsolete ideas in the face of mmap(2) et al.)
2538 vmspace_map_entry_forked(const struct vmspace *vm1, struct vmspace *vm2,
2539 vm_map_entry_t entry)
2541 vm_size_t entrysize;
2544 entrysize = entry->end - entry->start;
2545 vm2->vm_map.size += entrysize;
2546 if (entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP)) {
2547 vm2->vm_ssize += btoc(entrysize);
2548 } else if (entry->start >= (vm_offset_t)vm1->vm_daddr &&
2549 entry->start < (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)) {
2550 newend = MIN(entry->end,
2551 (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize));
2552 vm2->vm_dsize += btoc(newend - entry->start);
2553 } else if (entry->start >= (vm_offset_t)vm1->vm_taddr &&
2554 entry->start < (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)) {
2555 newend = MIN(entry->end,
2556 (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize));
2557 vm2->vm_tsize += btoc(newend - entry->start);
2563 * Create a new process vmspace structure and vm_map
2564 * based on those of an existing process. The new map
2565 * is based on the old map, according to the inheritance
2566 * values on the regions in that map.
2568 * XXX It might be worth coalescing the entries added to the new vmspace.
2570 * The source map must not be locked.
2573 vmspace_fork(struct vmspace *vm1)
2575 struct vmspace *vm2;
2576 vm_map_t old_map = &vm1->vm_map;
2578 vm_map_entry_t old_entry;
2579 vm_map_entry_t new_entry;
2582 vm_map_lock(old_map);
2584 vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
2585 vm2->vm_taddr = vm1->vm_taddr;
2586 vm2->vm_daddr = vm1->vm_daddr;
2587 vm2->vm_maxsaddr = vm1->vm_maxsaddr;
2588 new_map = &vm2->vm_map; /* XXX */
2589 new_map->timestamp = 1;
2591 old_entry = old_map->header.next;
2593 while (old_entry != &old_map->header) {
2594 if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2595 panic("vm_map_fork: encountered a submap");
2597 switch (old_entry->inheritance) {
2598 case VM_INHERIT_NONE:
2601 case VM_INHERIT_SHARE:
2603 * Clone the entry, creating the shared object if necessary.
2605 object = old_entry->object.vm_object;
2606 if (object == NULL) {
2607 object = vm_object_allocate(OBJT_DEFAULT,
2608 atop(old_entry->end - old_entry->start));
2609 old_entry->object.vm_object = object;
2610 old_entry->offset = 0;
2614 * Add the reference before calling vm_object_shadow
2615 * to insure that a shadow object is created.
2617 vm_object_reference(object);
2618 if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
2619 vm_object_shadow(&old_entry->object.vm_object,
2621 atop(old_entry->end - old_entry->start));
2622 old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
2623 /* Transfer the second reference too. */
2624 vm_object_reference(
2625 old_entry->object.vm_object);
2626 vm_object_deallocate(object);
2627 object = old_entry->object.vm_object;
2629 VM_OBJECT_LOCK(object);
2630 vm_object_clear_flag(object, OBJ_ONEMAPPING);
2631 VM_OBJECT_UNLOCK(object);
2634 * Clone the entry, referencing the shared object.
2636 new_entry = vm_map_entry_create(new_map);
2637 *new_entry = *old_entry;
2638 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2639 new_entry->wired_count = 0;
2642 * Insert the entry into the new map -- we know we're
2643 * inserting at the end of the new map.
2645 vm_map_entry_link(new_map, new_map->header.prev,
2647 vmspace_map_entry_forked(vm1, vm2, new_entry);
2650 * Update the physical map
2652 pmap_copy(new_map->pmap, old_map->pmap,
2654 (old_entry->end - old_entry->start),
2658 case VM_INHERIT_COPY:
2660 * Clone the entry and link into the map.
2662 new_entry = vm_map_entry_create(new_map);
2663 *new_entry = *old_entry;
2664 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2665 new_entry->wired_count = 0;
2666 new_entry->object.vm_object = NULL;
2667 vm_map_entry_link(new_map, new_map->header.prev,
2669 vmspace_map_entry_forked(vm1, vm2, new_entry);
2670 vm_map_copy_entry(old_map, new_map, old_entry,
2674 old_entry = old_entry->next;
2677 vm_map_unlock(old_map);
2683 vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
2684 vm_prot_t prot, vm_prot_t max, int cow)
2686 vm_map_entry_t new_entry, prev_entry;
2687 vm_offset_t bot, top;
2688 vm_size_t init_ssize;
2693 * The stack orientation is piggybacked with the cow argument.
2694 * Extract it into orient and mask the cow argument so that we
2695 * don't pass it around further.
2696 * NOTE: We explicitly allow bi-directional stacks.
2698 orient = cow & (MAP_STACK_GROWS_DOWN|MAP_STACK_GROWS_UP);
2700 KASSERT(orient != 0, ("No stack grow direction"));
2702 if (addrbos < vm_map_min(map) || addrbos > map->max_offset)
2703 return (KERN_NO_SPACE);
2705 init_ssize = (max_ssize < sgrowsiz) ? max_ssize : sgrowsiz;
2707 PROC_LOCK(curthread->td_proc);
2708 vmemlim = lim_cur(curthread->td_proc, RLIMIT_VMEM);
2709 PROC_UNLOCK(curthread->td_proc);
2713 /* If addr is already mapped, no go */
2714 if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
2716 return (KERN_NO_SPACE);
2719 /* If we would blow our VMEM resource limit, no go */
2720 if (map->size + init_ssize > vmemlim) {
2722 return (KERN_NO_SPACE);
2726 * If we can't accomodate max_ssize in the current mapping, no go.
2727 * However, we need to be aware that subsequent user mappings might
2728 * map into the space we have reserved for stack, and currently this
2729 * space is not protected.
2731 * Hopefully we will at least detect this condition when we try to
2734 if ((prev_entry->next != &map->header) &&
2735 (prev_entry->next->start < addrbos + max_ssize)) {
2737 return (KERN_NO_SPACE);
2741 * We initially map a stack of only init_ssize. We will grow as
2742 * needed later. Depending on the orientation of the stack (i.e.
2743 * the grow direction) we either map at the top of the range, the
2744 * bottom of the range or in the middle.
2746 * Note: we would normally expect prot and max to be VM_PROT_ALL,
2747 * and cow to be 0. Possibly we should eliminate these as input
2748 * parameters, and just pass these values here in the insert call.
2750 if (orient == MAP_STACK_GROWS_DOWN)
2751 bot = addrbos + max_ssize - init_ssize;
2752 else if (orient == MAP_STACK_GROWS_UP)
2755 bot = round_page(addrbos + max_ssize/2 - init_ssize/2);
2756 top = bot + init_ssize;
2757 rv = vm_map_insert(map, NULL, 0, bot, top, prot, max, cow);
2759 /* Now set the avail_ssize amount. */
2760 if (rv == KERN_SUCCESS) {
2761 if (prev_entry != &map->header)
2762 vm_map_clip_end(map, prev_entry, bot);
2763 new_entry = prev_entry->next;
2764 if (new_entry->end != top || new_entry->start != bot)
2765 panic("Bad entry start/end for new stack entry");
2767 new_entry->avail_ssize = max_ssize - init_ssize;
2768 if (orient & MAP_STACK_GROWS_DOWN)
2769 new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
2770 if (orient & MAP_STACK_GROWS_UP)
2771 new_entry->eflags |= MAP_ENTRY_GROWS_UP;
2778 /* Attempts to grow a vm stack entry. Returns KERN_SUCCESS if the
2779 * desired address is already mapped, or if we successfully grow
2780 * the stack. Also returns KERN_SUCCESS if addr is outside the
2781 * stack range (this is strange, but preserves compatibility with
2782 * the grow function in vm_machdep.c).
2785 vm_map_growstack(struct proc *p, vm_offset_t addr)
2787 vm_map_entry_t next_entry, prev_entry;
2788 vm_map_entry_t new_entry, stack_entry;
2789 struct vmspace *vm = p->p_vmspace;
2790 vm_map_t map = &vm->vm_map;
2792 size_t grow_amount, max_grow;
2793 rlim_t stacklim, vmemlim;
2794 int is_procstack, rv;
2798 stacklim = lim_cur(p, RLIMIT_STACK);
2799 vmemlim = lim_cur(p, RLIMIT_VMEM);
2802 vm_map_lock_read(map);
2804 /* If addr is already in the entry range, no need to grow.*/
2805 if (vm_map_lookup_entry(map, addr, &prev_entry)) {
2806 vm_map_unlock_read(map);
2807 return (KERN_SUCCESS);
2810 next_entry = prev_entry->next;
2811 if (!(prev_entry->eflags & MAP_ENTRY_GROWS_UP)) {
2813 * This entry does not grow upwards. Since the address lies
2814 * beyond this entry, the next entry (if one exists) has to
2815 * be a downward growable entry. The entry list header is
2816 * never a growable entry, so it suffices to check the flags.
2818 if (!(next_entry->eflags & MAP_ENTRY_GROWS_DOWN)) {
2819 vm_map_unlock_read(map);
2820 return (KERN_SUCCESS);
2822 stack_entry = next_entry;
2825 * This entry grows upward. If the next entry does not at
2826 * least grow downwards, this is the entry we need to grow.
2827 * otherwise we have two possible choices and we have to
2830 if (next_entry->eflags & MAP_ENTRY_GROWS_DOWN) {
2832 * We have two choices; grow the entry closest to
2833 * the address to minimize the amount of growth.
2835 if (addr - prev_entry->end <= next_entry->start - addr)
2836 stack_entry = prev_entry;
2838 stack_entry = next_entry;
2840 stack_entry = prev_entry;
2843 if (stack_entry == next_entry) {
2844 KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_DOWN, ("foo"));
2845 KASSERT(addr < stack_entry->start, ("foo"));
2846 end = (prev_entry != &map->header) ? prev_entry->end :
2847 stack_entry->start - stack_entry->avail_ssize;
2848 grow_amount = roundup(stack_entry->start - addr, PAGE_SIZE);
2849 max_grow = stack_entry->start - end;
2851 KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_UP, ("foo"));
2852 KASSERT(addr >= stack_entry->end, ("foo"));
2853 end = (next_entry != &map->header) ? next_entry->start :
2854 stack_entry->end + stack_entry->avail_ssize;
2855 grow_amount = roundup(addr + 1 - stack_entry->end, PAGE_SIZE);
2856 max_grow = end - stack_entry->end;
2859 if (grow_amount > stack_entry->avail_ssize) {
2860 vm_map_unlock_read(map);
2861 return (KERN_NO_SPACE);
2865 * If there is no longer enough space between the entries nogo, and
2866 * adjust the available space. Note: this should only happen if the
2867 * user has mapped into the stack area after the stack was created,
2868 * and is probably an error.
2870 * This also effectively destroys any guard page the user might have
2871 * intended by limiting the stack size.
2873 if (grow_amount > max_grow) {
2874 if (vm_map_lock_upgrade(map))
2877 stack_entry->avail_ssize = max_grow;
2880 return (KERN_NO_SPACE);
2883 is_procstack = (addr >= (vm_offset_t)vm->vm_maxsaddr) ? 1 : 0;
2886 * If this is the main process stack, see if we're over the stack
2889 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
2890 vm_map_unlock_read(map);
2891 return (KERN_NO_SPACE);
2894 /* Round up the grow amount modulo SGROWSIZ */
2895 grow_amount = roundup (grow_amount, sgrowsiz);
2896 if (grow_amount > stack_entry->avail_ssize)
2897 grow_amount = stack_entry->avail_ssize;
2898 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
2899 grow_amount = stacklim - ctob(vm->vm_ssize);
2902 /* If we would blow our VMEM resource limit, no go */
2903 if (map->size + grow_amount > vmemlim) {
2904 vm_map_unlock_read(map);
2905 return (KERN_NO_SPACE);
2908 if (vm_map_lock_upgrade(map))
2911 if (stack_entry == next_entry) {
2915 /* Get the preliminary new entry start value */
2916 addr = stack_entry->start - grow_amount;
2919 * If this puts us into the previous entry, cut back our
2920 * growth to the available space. Also, see the note above.
2923 stack_entry->avail_ssize = max_grow;
2927 rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start,
2928 p->p_sysent->sv_stackprot, VM_PROT_ALL, 0);
2930 /* Adjust the available stack space by the amount we grew. */
2931 if (rv == KERN_SUCCESS) {
2932 if (prev_entry != &map->header)
2933 vm_map_clip_end(map, prev_entry, addr);
2934 new_entry = prev_entry->next;
2935 KASSERT(new_entry == stack_entry->prev, ("foo"));
2936 KASSERT(new_entry->end == stack_entry->start, ("foo"));
2937 KASSERT(new_entry->start == addr, ("foo"));
2938 grow_amount = new_entry->end - new_entry->start;
2939 new_entry->avail_ssize = stack_entry->avail_ssize -
2941 stack_entry->eflags &= ~MAP_ENTRY_GROWS_DOWN;
2942 new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
2948 addr = stack_entry->end + grow_amount;
2951 * If this puts us into the next entry, cut back our growth
2952 * to the available space. Also, see the note above.
2955 stack_entry->avail_ssize = end - stack_entry->end;
2959 grow_amount = addr - stack_entry->end;
2961 /* Grow the underlying object if applicable. */
2962 if (stack_entry->object.vm_object == NULL ||
2963 vm_object_coalesce(stack_entry->object.vm_object,
2964 stack_entry->offset,
2965 (vm_size_t)(stack_entry->end - stack_entry->start),
2966 (vm_size_t)grow_amount)) {
2967 map->size += (addr - stack_entry->end);
2968 /* Update the current entry. */
2969 stack_entry->end = addr;
2970 stack_entry->avail_ssize -= grow_amount;
2971 vm_map_entry_resize_free(map, stack_entry);
2974 if (next_entry != &map->header)
2975 vm_map_clip_start(map, next_entry, addr);
2980 if (rv == KERN_SUCCESS && is_procstack)
2981 vm->vm_ssize += btoc(grow_amount);
2986 * Heed the MAP_WIREFUTURE flag if it was set for this process.
2988 if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE)) {
2990 (stack_entry == next_entry) ? addr : addr - grow_amount,
2991 (stack_entry == next_entry) ? stack_entry->start : addr,
2992 (p->p_flag & P_SYSTEM)
2993 ? VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES
2994 : VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
3001 * Unshare the specified VM space for exec. If other processes are
3002 * mapped to it, then create a new one. The new vmspace is null.
3005 vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser)
3007 struct vmspace *oldvmspace = p->p_vmspace;
3008 struct vmspace *newvmspace;
3010 newvmspace = vmspace_alloc(minuser, maxuser);
3011 newvmspace->vm_swrss = oldvmspace->vm_swrss;
3013 * This code is written like this for prototype purposes. The
3014 * goal is to avoid running down the vmspace here, but let the
3015 * other process's that are still using the vmspace to finally
3016 * run it down. Even though there is little or no chance of blocking
3017 * here, it is a good idea to keep this form for future mods.
3019 PROC_VMSPACE_LOCK(p);
3020 p->p_vmspace = newvmspace;
3021 PROC_VMSPACE_UNLOCK(p);
3022 if (p == curthread->td_proc) /* XXXKSE ? */
3023 pmap_activate(curthread);
3024 vmspace_free(oldvmspace);
3028 * Unshare the specified VM space for forcing COW. This
3029 * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
3032 vmspace_unshare(struct proc *p)
3034 struct vmspace *oldvmspace = p->p_vmspace;
3035 struct vmspace *newvmspace;
3037 if (oldvmspace->vm_refcnt == 1)
3039 newvmspace = vmspace_fork(oldvmspace);
3040 PROC_VMSPACE_LOCK(p);
3041 p->p_vmspace = newvmspace;
3042 PROC_VMSPACE_UNLOCK(p);
3043 if (p == curthread->td_proc) /* XXXKSE ? */
3044 pmap_activate(curthread);
3045 vmspace_free(oldvmspace);
3051 * Finds the VM object, offset, and
3052 * protection for a given virtual address in the
3053 * specified map, assuming a page fault of the
3056 * Leaves the map in question locked for read; return
3057 * values are guaranteed until a vm_map_lookup_done
3058 * call is performed. Note that the map argument
3059 * is in/out; the returned map must be used in
3060 * the call to vm_map_lookup_done.
3062 * A handle (out_entry) is returned for use in
3063 * vm_map_lookup_done, to make that fast.
3065 * If a lookup is requested with "write protection"
3066 * specified, the map may be changed to perform virtual
3067 * copying operations, although the data referenced will
3071 vm_map_lookup(vm_map_t *var_map, /* IN/OUT */
3073 vm_prot_t fault_typea,
3074 vm_map_entry_t *out_entry, /* OUT */
3075 vm_object_t *object, /* OUT */
3076 vm_pindex_t *pindex, /* OUT */
3077 vm_prot_t *out_prot, /* OUT */
3078 boolean_t *wired) /* OUT */
3080 vm_map_entry_t entry;
3081 vm_map_t map = *var_map;
3083 vm_prot_t fault_type = fault_typea;
3087 * Lookup the faulting address.
3090 vm_map_lock_read(map);
3091 #define RETURN(why) \
3093 vm_map_unlock_read(map); \
3098 * If the map has an interesting hint, try it before calling full
3099 * blown lookup routine.
3103 if (entry == NULL ||
3104 (vaddr < entry->start) || (vaddr >= entry->end)) {
3106 * Entry was either not a valid hint, or the vaddr was not
3107 * contained in the entry, so do a full lookup.
3109 if (!vm_map_lookup_entry(map, vaddr, out_entry))
3110 RETURN(KERN_INVALID_ADDRESS);
3118 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3119 vm_map_t old_map = map;
3121 *var_map = map = entry->object.sub_map;
3122 vm_map_unlock_read(old_map);
3127 * Check whether this task is allowed to have this page.
3128 * Note the special case for MAP_ENTRY_COW
3129 * pages with an override. This is to implement a forced
3130 * COW for debuggers.
3132 if (fault_type & VM_PROT_OVERRIDE_WRITE)
3133 prot = entry->max_protection;
3135 prot = entry->protection;
3136 fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
3137 if ((fault_type & prot) != fault_type) {
3138 RETURN(KERN_PROTECTION_FAILURE);
3140 if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
3141 (entry->eflags & MAP_ENTRY_COW) &&
3142 (fault_type & VM_PROT_WRITE) &&
3143 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
3144 RETURN(KERN_PROTECTION_FAILURE);
3148 * If this page is not pageable, we have to get it for all possible
3151 *wired = (entry->wired_count != 0);
3153 prot = fault_type = entry->protection;
3156 * If the entry was copy-on-write, we either ...
3158 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3160 * If we want to write the page, we may as well handle that
3161 * now since we've got the map locked.
3163 * If we don't need to write the page, we just demote the
3164 * permissions allowed.
3166 if (fault_type & VM_PROT_WRITE) {
3168 * Make a new object, and place it in the object
3169 * chain. Note that no new references have appeared
3170 * -- one just moved from the map to the new
3173 if (vm_map_lock_upgrade(map))
3177 &entry->object.vm_object,
3179 atop(entry->end - entry->start));
3180 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
3182 vm_map_lock_downgrade(map);
3185 * We're attempting to read a copy-on-write page --
3186 * don't allow writes.
3188 prot &= ~VM_PROT_WRITE;
3193 * Create an object if necessary.
3195 if (entry->object.vm_object == NULL &&
3197 if (vm_map_lock_upgrade(map))
3199 entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
3200 atop(entry->end - entry->start));
3202 vm_map_lock_downgrade(map);
3206 * Return the object/offset from this entry. If the entry was
3207 * copy-on-write or empty, it has been fixed up.
3209 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
3210 *object = entry->object.vm_object;
3213 return (KERN_SUCCESS);
3219 * vm_map_lookup_locked:
3221 * Lookup the faulting address. A version of vm_map_lookup that returns
3222 * KERN_FAILURE instead of blocking on map lock or memory allocation.
3225 vm_map_lookup_locked(vm_map_t *var_map, /* IN/OUT */
3227 vm_prot_t fault_typea,
3228 vm_map_entry_t *out_entry, /* OUT */
3229 vm_object_t *object, /* OUT */
3230 vm_pindex_t *pindex, /* OUT */
3231 vm_prot_t *out_prot, /* OUT */
3232 boolean_t *wired) /* OUT */
3234 vm_map_entry_t entry;
3235 vm_map_t map = *var_map;
3237 vm_prot_t fault_type = fault_typea;
3240 * If the map has an interesting hint, try it before calling full
3241 * blown lookup routine.
3245 if (entry == NULL ||
3246 (vaddr < entry->start) || (vaddr >= entry->end)) {
3248 * Entry was either not a valid hint, or the vaddr was not
3249 * contained in the entry, so do a full lookup.
3251 if (!vm_map_lookup_entry(map, vaddr, out_entry))
3252 return (KERN_INVALID_ADDRESS);
3258 * Fail if the entry refers to a submap.
3260 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
3261 return (KERN_FAILURE);
3264 * Check whether this task is allowed to have this page.
3265 * Note the special case for MAP_ENTRY_COW
3266 * pages with an override. This is to implement a forced
3267 * COW for debuggers.
3269 if (fault_type & VM_PROT_OVERRIDE_WRITE)
3270 prot = entry->max_protection;
3272 prot = entry->protection;
3273 fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
3274 if ((fault_type & prot) != fault_type)
3275 return (KERN_PROTECTION_FAILURE);
3276 if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
3277 (entry->eflags & MAP_ENTRY_COW) &&
3278 (fault_type & VM_PROT_WRITE) &&
3279 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0)
3280 return (KERN_PROTECTION_FAILURE);
3283 * If this page is not pageable, we have to get it for all possible
3286 *wired = (entry->wired_count != 0);
3288 prot = fault_type = entry->protection;
3290 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3292 * Fail if the entry was copy-on-write for a write fault.
3294 if (fault_type & VM_PROT_WRITE)
3295 return (KERN_FAILURE);
3297 * We're attempting to read a copy-on-write page --
3298 * don't allow writes.
3300 prot &= ~VM_PROT_WRITE;
3304 * Fail if an object should be created.
3306 if (entry->object.vm_object == NULL && !map->system_map)
3307 return (KERN_FAILURE);
3310 * Return the object/offset from this entry. If the entry was
3311 * copy-on-write or empty, it has been fixed up.
3313 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
3314 *object = entry->object.vm_object;
3317 return (KERN_SUCCESS);
3321 * vm_map_lookup_done:
3323 * Releases locks acquired by a vm_map_lookup
3324 * (according to the handle returned by that lookup).
3327 vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry)
3330 * Unlock the main-level map
3332 vm_map_unlock_read(map);
3335 #include "opt_ddb.h"
3337 #include <sys/kernel.h>
3339 #include <ddb/ddb.h>
3342 * vm_map_print: [ debug ]
3344 DB_SHOW_COMMAND(map, vm_map_print)
3347 /* XXX convert args. */
3348 vm_map_t map = (vm_map_t)addr;
3349 boolean_t full = have_addr;
3351 vm_map_entry_t entry;
3353 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
3355 (void *)map->pmap, map->nentries, map->timestamp);
3358 if (!full && db_indent)
3362 for (entry = map->header.next; entry != &map->header;
3363 entry = entry->next) {
3364 db_iprintf("map entry %p: start=%p, end=%p\n",
3365 (void *)entry, (void *)entry->start, (void *)entry->end);
3368 static char *inheritance_name[4] =
3369 {"share", "copy", "none", "donate_copy"};
3371 db_iprintf(" prot=%x/%x/%s",
3373 entry->max_protection,
3374 inheritance_name[(int)(unsigned char)entry->inheritance]);
3375 if (entry->wired_count != 0)
3376 db_printf(", wired");
3378 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3379 db_printf(", share=%p, offset=0x%jx\n",
3380 (void *)entry->object.sub_map,
3381 (uintmax_t)entry->offset);
3383 if ((entry->prev == &map->header) ||
3384 (entry->prev->object.sub_map !=
3385 entry->object.sub_map)) {
3387 vm_map_print((db_expr_t)(intptr_t)
3388 entry->object.sub_map,
3389 full, 0, (char *)0);
3393 db_printf(", object=%p, offset=0x%jx",
3394 (void *)entry->object.vm_object,
3395 (uintmax_t)entry->offset);
3396 if (entry->eflags & MAP_ENTRY_COW)
3397 db_printf(", copy (%s)",
3398 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
3402 if ((entry->prev == &map->header) ||
3403 (entry->prev->object.vm_object !=
3404 entry->object.vm_object)) {
3406 vm_object_print((db_expr_t)(intptr_t)
3407 entry->object.vm_object,
3408 full, 0, (char *)0);
3420 DB_SHOW_COMMAND(procvm, procvm)
3425 p = (struct proc *) addr;
3430 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
3431 (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
3432 (void *)vmspace_pmap(p->p_vmspace));
3434 vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);