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);
192 uma_prealloc(mapentzone, MAX_MAPENT);
196 vmspace_zfini(void *mem, int size)
200 vm = (struct vmspace *)mem;
201 pmap_release(vmspace_pmap(vm));
202 vm_map_zfini(&vm->vm_map, sizeof(vm->vm_map));
206 vmspace_zinit(void *mem, int size, int flags)
210 vm = (struct vmspace *)mem;
212 (void)vm_map_zinit(&vm->vm_map, sizeof(vm->vm_map), flags);
213 pmap_pinit(vmspace_pmap(vm));
218 vm_map_zfini(void *mem, int size)
223 mtx_destroy(&map->system_mtx);
224 sx_destroy(&map->lock);
228 vm_map_zinit(void *mem, int size, int flags)
235 mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
236 sx_init(&map->lock, "user map");
242 vmspace_zdtor(void *mem, int size, void *arg)
246 vm = (struct vmspace *)mem;
248 vm_map_zdtor(&vm->vm_map, sizeof(vm->vm_map), arg);
251 vm_map_zdtor(void *mem, int size, void *arg)
256 KASSERT(map->nentries == 0,
257 ("map %p nentries == %d on free.",
258 map, map->nentries));
259 KASSERT(map->size == 0,
260 ("map %p size == %lu on free.",
261 map, (unsigned long)map->size));
263 #endif /* INVARIANTS */
266 * Allocate a vmspace structure, including a vm_map and pmap,
267 * and initialize those structures. The refcnt is set to 1.
270 vmspace_alloc(min, max)
271 vm_offset_t min, max;
275 vm = uma_zalloc(vmspace_zone, M_WAITOK);
276 CTR1(KTR_VM, "vmspace_alloc: %p", vm);
277 _vm_map_init(&vm->vm_map, min, max);
278 vm->vm_map.pmap = vmspace_pmap(vm); /* XXX */
294 uma_zone_set_obj(kmapentzone, &kmapentobj, lmin(cnt.v_page_count,
295 (VM_MAX_KERNEL_ADDRESS - KERNBASE) / PAGE_SIZE) / 8 +
296 maxproc * 2 + maxfiles);
297 vmspace_zone = uma_zcreate("VMSPACE", sizeof(struct vmspace), NULL,
303 vmspace_zinit, vmspace_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
308 vmspace_dofree(struct vmspace *vm)
310 CTR1(KTR_VM, "vmspace_free: %p", vm);
313 * Make sure any SysV shm is freed, it might not have been in
319 * Lock the map, to wait out all other references to it.
320 * Delete all of the mappings and pages they hold, then call
321 * the pmap module to reclaim anything left.
323 vm_map_lock(&vm->vm_map);
324 (void) vm_map_delete(&vm->vm_map, vm->vm_map.min_offset,
325 vm->vm_map.max_offset);
326 vm_map_unlock(&vm->vm_map);
328 uma_zfree(vmspace_zone, vm);
332 vmspace_free(struct vmspace *vm)
336 if (vm->vm_refcnt == 0)
337 panic("vmspace_free: attempt to free already freed vmspace");
340 refcnt = vm->vm_refcnt;
341 while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1));
347 vmspace_exitfree(struct proc *p)
351 PROC_VMSPACE_LOCK(p);
354 PROC_VMSPACE_UNLOCK(p);
355 KASSERT(vm == &vmspace0, ("vmspace_exitfree: wrong vmspace"));
360 vmspace_exit(struct thread *td)
367 * Release user portion of address space.
368 * This releases references to vnodes,
369 * which could cause I/O if the file has been unlinked.
370 * Need to do this early enough that we can still sleep.
372 * The last exiting process to reach this point releases as
373 * much of the environment as it can. vmspace_dofree() is the
374 * slower fallback in case another process had a temporary
375 * reference to the vmspace.
380 atomic_add_int(&vmspace0.vm_refcnt, 1);
382 refcnt = vm->vm_refcnt;
383 if (refcnt > 1 && p->p_vmspace != &vmspace0) {
384 /* Switch now since other proc might free vmspace */
385 PROC_VMSPACE_LOCK(p);
386 p->p_vmspace = &vmspace0;
387 PROC_VMSPACE_UNLOCK(p);
390 } while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1));
392 if (p->p_vmspace != vm) {
393 /* vmspace not yet freed, switch back */
394 PROC_VMSPACE_LOCK(p);
396 PROC_VMSPACE_UNLOCK(p);
399 pmap_remove_pages(vmspace_pmap(vm),
400 vm_map_min(&vm->vm_map),
401 vm_map_max(&vm->vm_map));
402 /* Switch now since this proc will free vmspace */
403 PROC_VMSPACE_LOCK(p);
404 p->p_vmspace = &vmspace0;
405 PROC_VMSPACE_UNLOCK(p);
411 /* Acquire reference to vmspace owned by another process. */
414 vmspace_acquire_ref(struct proc *p)
419 PROC_VMSPACE_LOCK(p);
422 PROC_VMSPACE_UNLOCK(p);
426 refcnt = vm->vm_refcnt;
427 if (refcnt <= 0) { /* Avoid 0->1 transition */
428 PROC_VMSPACE_UNLOCK(p);
431 } while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt + 1));
432 if (vm != p->p_vmspace) {
433 PROC_VMSPACE_UNLOCK(p);
437 PROC_VMSPACE_UNLOCK(p);
442 _vm_map_lock(vm_map_t map, const char *file, int line)
446 _mtx_lock_flags(&map->system_mtx, 0, file, line);
448 (void) _sx_xlock(&map->lock, 0, file, line);
453 _vm_map_unlock(vm_map_t map, const char *file, int line)
457 _mtx_unlock_flags(&map->system_mtx, 0, file, line);
459 _sx_xunlock(&map->lock, file, line);
463 _vm_map_lock_read(vm_map_t map, const char *file, int line)
467 _mtx_lock_flags(&map->system_mtx, 0, file, line);
469 (void) _sx_xlock(&map->lock, 0, file, line);
473 _vm_map_unlock_read(vm_map_t map, const char *file, int line)
477 _mtx_unlock_flags(&map->system_mtx, 0, file, line);
479 _sx_xunlock(&map->lock, file, line);
483 _vm_map_trylock(vm_map_t map, const char *file, int line)
487 error = map->system_map ?
488 !_mtx_trylock(&map->system_mtx, 0, file, line) :
489 !_sx_try_xlock(&map->lock, file, line);
496 _vm_map_trylock_read(vm_map_t map, const char *file, int line)
500 error = map->system_map ?
501 !_mtx_trylock(&map->system_mtx, 0, file, line) :
502 !_sx_try_xlock(&map->lock, file, line);
507 _vm_map_lock_upgrade(vm_map_t map, const char *file, int line)
511 if (map->system_map) {
512 _mtx_assert(&map->system_mtx, MA_OWNED, file, line);
514 _sx_assert(&map->lock, SX_XLOCKED, file, line);
521 _vm_map_lock_downgrade(vm_map_t map, const char *file, int line)
525 if (map->system_map) {
526 _mtx_assert(&map->system_mtx, MA_OWNED, file, line);
528 _sx_assert(&map->lock, SX_XLOCKED, file, line);
533 * vm_map_unlock_and_wait:
536 vm_map_unlock_and_wait(vm_map_t map, boolean_t user_wait)
539 mtx_lock(&map_sleep_mtx);
541 return (msleep(&map->root, &map_sleep_mtx, PDROP | PVM, "vmmaps", 0));
548 vm_map_wakeup(vm_map_t map)
552 * Acquire and release map_sleep_mtx to prevent a wakeup()
553 * from being performed (and lost) between the vm_map_unlock()
554 * and the msleep() in vm_map_unlock_and_wait().
556 mtx_lock(&map_sleep_mtx);
557 mtx_unlock(&map_sleep_mtx);
562 vmspace_resident_count(struct vmspace *vmspace)
564 return pmap_resident_count(vmspace_pmap(vmspace));
568 vmspace_wired_count(struct vmspace *vmspace)
570 return pmap_wired_count(vmspace_pmap(vmspace));
576 * Creates and returns a new empty VM map with
577 * the given physical map structure, and having
578 * the given lower and upper address bounds.
581 vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max)
585 result = uma_zalloc(mapzone, M_WAITOK);
586 CTR1(KTR_VM, "vm_map_create: %p", result);
587 _vm_map_init(result, min, max);
593 * Initialize an existing vm_map structure
594 * such as that in the vmspace structure.
595 * The pmap is set elsewhere.
598 _vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max)
601 map->header.next = map->header.prev = &map->header;
602 map->needs_wakeup = FALSE;
604 map->min_offset = min;
605 map->max_offset = max;
612 vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max)
614 _vm_map_init(map, min, max);
615 mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
616 sx_init(&map->lock, "user map");
620 * vm_map_entry_dispose: [ internal use only ]
622 * Inverse of vm_map_entry_create.
625 vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry)
627 uma_zfree(map->system_map ? kmapentzone : mapentzone, entry);
631 * vm_map_entry_create: [ internal use only ]
633 * Allocates a VM map entry for insertion.
634 * No entry fields are filled in.
636 static vm_map_entry_t
637 vm_map_entry_create(vm_map_t map)
639 vm_map_entry_t new_entry;
642 new_entry = uma_zalloc(kmapentzone, M_NOWAIT);
644 new_entry = uma_zalloc(mapentzone, M_WAITOK);
645 if (new_entry == NULL)
646 panic("vm_map_entry_create: kernel resources exhausted");
651 * vm_map_entry_set_behavior:
653 * Set the expected access behavior, either normal, random, or
657 vm_map_entry_set_behavior(vm_map_entry_t entry, u_char behavior)
659 entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) |
660 (behavior & MAP_ENTRY_BEHAV_MASK);
664 * vm_map_entry_set_max_free:
666 * Set the max_free field in a vm_map_entry.
669 vm_map_entry_set_max_free(vm_map_entry_t entry)
672 entry->max_free = entry->adj_free;
673 if (entry->left != NULL && entry->left->max_free > entry->max_free)
674 entry->max_free = entry->left->max_free;
675 if (entry->right != NULL && entry->right->max_free > entry->max_free)
676 entry->max_free = entry->right->max_free;
680 * vm_map_entry_splay:
682 * The Sleator and Tarjan top-down splay algorithm with the
683 * following variation. Max_free must be computed bottom-up, so
684 * on the downward pass, maintain the left and right spines in
685 * reverse order. Then, make a second pass up each side to fix
686 * the pointers and compute max_free. The time bound is O(log n)
689 * The new root is the vm_map_entry containing "addr", or else an
690 * adjacent entry (lower or higher) if addr is not in the tree.
692 * The map must be locked, and leaves it so.
694 * Returns: the new root.
696 static vm_map_entry_t
697 vm_map_entry_splay(vm_offset_t addr, vm_map_entry_t root)
699 vm_map_entry_t llist, rlist;
700 vm_map_entry_t ltree, rtree;
703 /* Special case of empty tree. */
708 * Pass One: Splay down the tree until we find addr or a NULL
709 * pointer where addr would go. llist and rlist are the two
710 * sides in reverse order (bottom-up), with llist linked by
711 * the right pointer and rlist linked by the left pointer in
712 * the vm_map_entry. Wait until Pass Two to set max_free on
718 /* root is never NULL in here. */
719 if (addr < root->start) {
723 if (addr < y->start && y->left != NULL) {
724 /* Rotate right and put y on rlist. */
725 root->left = y->right;
727 vm_map_entry_set_max_free(root);
732 /* Put root on rlist. */
739 if (addr < root->end || y == NULL)
741 if (addr >= y->end && y->right != NULL) {
742 /* Rotate left and put y on llist. */
743 root->right = y->left;
745 vm_map_entry_set_max_free(root);
750 /* Put root on llist. */
759 * Pass Two: Walk back up the two spines, flip the pointers
760 * and set max_free. The subtrees of the root go at the
761 * bottom of llist and rlist.
764 while (llist != NULL) {
766 llist->right = ltree;
767 vm_map_entry_set_max_free(llist);
772 while (rlist != NULL) {
775 vm_map_entry_set_max_free(rlist);
781 * Final assembly: add ltree and rtree as subtrees of root.
785 vm_map_entry_set_max_free(root);
791 * vm_map_entry_{un,}link:
793 * Insert/remove entries from maps.
796 vm_map_entry_link(vm_map_t map,
797 vm_map_entry_t after_where,
798 vm_map_entry_t entry)
802 "vm_map_entry_link: map %p, nentries %d, entry %p, after %p", map,
803 map->nentries, entry, after_where);
805 entry->prev = after_where;
806 entry->next = after_where->next;
807 entry->next->prev = entry;
808 after_where->next = entry;
810 if (after_where != &map->header) {
811 if (after_where != map->root)
812 vm_map_entry_splay(after_where->start, map->root);
813 entry->right = after_where->right;
814 entry->left = after_where;
815 after_where->right = NULL;
816 after_where->adj_free = entry->start - after_where->end;
817 vm_map_entry_set_max_free(after_where);
819 entry->right = map->root;
822 entry->adj_free = (entry->next == &map->header ? map->max_offset :
823 entry->next->start) - entry->end;
824 vm_map_entry_set_max_free(entry);
829 vm_map_entry_unlink(vm_map_t map,
830 vm_map_entry_t entry)
832 vm_map_entry_t next, prev, root;
834 if (entry != map->root)
835 vm_map_entry_splay(entry->start, map->root);
836 if (entry->left == NULL)
839 root = vm_map_entry_splay(entry->start, entry->left);
840 root->right = entry->right;
841 root->adj_free = (entry->next == &map->header ? map->max_offset :
842 entry->next->start) - root->end;
843 vm_map_entry_set_max_free(root);
852 CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map,
853 map->nentries, entry);
857 * vm_map_entry_resize_free:
859 * Recompute the amount of free space following a vm_map_entry
860 * and propagate that value up the tree. Call this function after
861 * resizing a map entry in-place, that is, without a call to
862 * vm_map_entry_link() or _unlink().
864 * The map must be locked, and leaves it so.
867 vm_map_entry_resize_free(vm_map_t map, vm_map_entry_t entry)
871 * Using splay trees without parent pointers, propagating
872 * max_free up the tree is done by moving the entry to the
873 * root and making the change there.
875 if (entry != map->root)
876 map->root = vm_map_entry_splay(entry->start, map->root);
878 entry->adj_free = (entry->next == &map->header ? map->max_offset :
879 entry->next->start) - entry->end;
880 vm_map_entry_set_max_free(entry);
884 * vm_map_lookup_entry: [ internal use only ]
886 * Finds the map entry containing (or
887 * immediately preceding) the specified address
888 * in the given map; the entry is returned
889 * in the "entry" parameter. The boolean
890 * result indicates whether the address is
891 * actually contained in the map.
897 vm_map_entry_t *entry) /* OUT */
901 cur = vm_map_entry_splay(address, map->root);
903 *entry = &map->header;
907 if (address >= cur->start) {
909 if (cur->end > address)
920 * Inserts the given whole VM object into the target
921 * map at the specified address range. The object's
922 * size should match that of the address range.
924 * Requires that the map be locked, and leaves it so.
926 * If object is non-NULL, ref count must be bumped by caller
927 * prior to making call to account for the new entry.
930 vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
931 vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max,
934 vm_map_entry_t new_entry;
935 vm_map_entry_t prev_entry;
936 vm_map_entry_t temp_entry;
937 vm_eflags_t protoeflags;
940 * Check that the start and end points are not bogus.
942 if ((start < map->min_offset) || (end > map->max_offset) ||
944 return (KERN_INVALID_ADDRESS);
947 * Find the entry prior to the proposed starting address; if it's part
948 * of an existing entry, this range is bogus.
950 if (vm_map_lookup_entry(map, start, &temp_entry))
951 return (KERN_NO_SPACE);
953 prev_entry = temp_entry;
956 * Assert that the next entry doesn't overlap the end point.
958 if ((prev_entry->next != &map->header) &&
959 (prev_entry->next->start < end))
960 return (KERN_NO_SPACE);
964 if (cow & MAP_COPY_ON_WRITE)
965 protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
967 if (cow & MAP_NOFAULT) {
968 protoeflags |= MAP_ENTRY_NOFAULT;
970 KASSERT(object == NULL,
971 ("vm_map_insert: paradoxical MAP_NOFAULT request"));
973 if (cow & MAP_DISABLE_SYNCER)
974 protoeflags |= MAP_ENTRY_NOSYNC;
975 if (cow & MAP_DISABLE_COREDUMP)
976 protoeflags |= MAP_ENTRY_NOCOREDUMP;
978 if (object != NULL) {
980 * OBJ_ONEMAPPING must be cleared unless this mapping
981 * is trivially proven to be the only mapping for any
982 * of the object's pages. (Object granularity
983 * reference counting is insufficient to recognize
984 * aliases with precision.)
986 VM_OBJECT_LOCK(object);
987 if (object->ref_count > 1 || object->shadow_count != 0)
988 vm_object_clear_flag(object, OBJ_ONEMAPPING);
989 VM_OBJECT_UNLOCK(object);
991 else if ((prev_entry != &map->header) &&
992 (prev_entry->eflags == protoeflags) &&
993 (prev_entry->end == start) &&
994 (prev_entry->wired_count == 0) &&
995 ((prev_entry->object.vm_object == NULL) ||
996 vm_object_coalesce(prev_entry->object.vm_object,
998 (vm_size_t)(prev_entry->end - prev_entry->start),
999 (vm_size_t)(end - prev_entry->end)))) {
1001 * We were able to extend the object. Determine if we
1002 * can extend the previous map entry to include the
1003 * new range as well.
1005 if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
1006 (prev_entry->protection == prot) &&
1007 (prev_entry->max_protection == max)) {
1008 map->size += (end - prev_entry->end);
1009 prev_entry->end = end;
1010 vm_map_entry_resize_free(map, prev_entry);
1011 vm_map_simplify_entry(map, prev_entry);
1012 return (KERN_SUCCESS);
1016 * If we can extend the object but cannot extend the
1017 * map entry, we have to create a new map entry. We
1018 * must bump the ref count on the extended object to
1019 * account for it. object may be NULL.
1021 object = prev_entry->object.vm_object;
1022 offset = prev_entry->offset +
1023 (prev_entry->end - prev_entry->start);
1024 vm_object_reference(object);
1028 * NOTE: if conditionals fail, object can be NULL here. This occurs
1029 * in things like the buffer map where we manage kva but do not manage
1034 * Create a new entry
1036 new_entry = vm_map_entry_create(map);
1037 new_entry->start = start;
1038 new_entry->end = end;
1040 new_entry->eflags = protoeflags;
1041 new_entry->object.vm_object = object;
1042 new_entry->offset = offset;
1043 new_entry->avail_ssize = 0;
1045 new_entry->inheritance = VM_INHERIT_DEFAULT;
1046 new_entry->protection = prot;
1047 new_entry->max_protection = max;
1048 new_entry->wired_count = 0;
1051 * Insert the new entry into the list
1053 vm_map_entry_link(map, prev_entry, new_entry);
1054 map->size += new_entry->end - new_entry->start;
1058 * Temporarily removed to avoid MAP_STACK panic, due to
1059 * MAP_STACK being a huge hack. Will be added back in
1060 * when MAP_STACK (and the user stack mapping) is fixed.
1063 * It may be possible to simplify the entry
1065 vm_map_simplify_entry(map, new_entry);
1068 if (cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) {
1069 vm_map_pmap_enter(map, start, prot,
1070 object, OFF_TO_IDX(offset), end - start,
1071 cow & MAP_PREFAULT_PARTIAL);
1074 return (KERN_SUCCESS);
1080 * Find the first fit (lowest VM address) for "length" free bytes
1081 * beginning at address >= start in the given map.
1083 * In a vm_map_entry, "adj_free" is the amount of free space
1084 * adjacent (higher address) to this entry, and "max_free" is the
1085 * maximum amount of contiguous free space in its subtree. This
1086 * allows finding a free region in one path down the tree, so
1087 * O(log n) amortized with splay trees.
1089 * The map must be locked, and leaves it so.
1091 * Returns: 0 on success, and starting address in *addr,
1092 * 1 if insufficient space.
1095 vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length,
1096 vm_offset_t *addr) /* OUT */
1098 vm_map_entry_t entry;
1099 vm_offset_t end, st;
1102 * Request must fit within min/max VM address and must avoid
1105 if (start < map->min_offset)
1106 start = map->min_offset;
1107 if (start + length > map->max_offset || start + length < start)
1110 /* Empty tree means wide open address space. */
1111 if (map->root == NULL) {
1117 * After splay, if start comes before root node, then there
1118 * must be a gap from start to the root.
1120 map->root = vm_map_entry_splay(start, map->root);
1121 if (start + length <= map->root->start) {
1127 * Root is the last node that might begin its gap before
1128 * start, and this is the last comparison where address
1129 * wrap might be a problem.
1131 st = (start > map->root->end) ? start : map->root->end;
1132 if (length <= map->root->end + map->root->adj_free - st) {
1137 /* With max_free, can immediately tell if no solution. */
1138 entry = map->root->right;
1139 if (entry == NULL || length > entry->max_free)
1143 * Search the right subtree in the order: left subtree, root,
1144 * right subtree (first fit). The previous splay implies that
1145 * all regions in the right subtree have addresses > start.
1147 while (entry != NULL) {
1148 if (entry->left != NULL && entry->left->max_free >= length)
1149 entry = entry->left;
1150 else if (entry->adj_free >= length) {
1154 entry = entry->right;
1157 /* Can't get here, so panic if we do. */
1158 panic("vm_map_findspace: max_free corrupt");
1161 /* Expand the kernel pmap, if necessary. */
1162 if (map == kernel_map) {
1163 end = round_page(*addr + length);
1164 if (end > kernel_vm_end)
1165 pmap_growkernel(end);
1171 vm_map_fixed(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1172 vm_offset_t *addr /* IN/OUT */, vm_size_t length, vm_prot_t prot,
1173 vm_prot_t max, int cow)
1175 vm_offset_t start, end;
1180 end = start + length;
1181 VM_MAP_RANGE_CHECK(map, start, end);
1182 (void) vm_map_delete(map, start, end);
1183 result = vm_map_insert(map, object, offset, start, end, prot,
1190 * vm_map_find finds an unallocated region in the target address
1191 * map with the given length. The search is defined to be
1192 * first-fit from the specified address; the region found is
1193 * returned in the same parameter.
1195 * If object is non-NULL, ref count must be bumped by caller
1196 * prior to making call to account for the new entry.
1199 vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1200 vm_offset_t *addr, /* IN/OUT */
1201 vm_size_t length, boolean_t find_space, vm_prot_t prot,
1202 vm_prot_t max, int cow)
1210 if (vm_map_findspace(map, start, length, addr)) {
1212 return (KERN_NO_SPACE);
1216 result = vm_map_insert(map, object, offset,
1217 start, start + length, prot, max, cow);
1223 * vm_map_simplify_entry:
1225 * Simplify the given map entry by merging with either neighbor. This
1226 * routine also has the ability to merge with both neighbors.
1228 * The map must be locked.
1230 * This routine guarentees that the passed entry remains valid (though
1231 * possibly extended). When merging, this routine may delete one or
1235 vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry)
1237 vm_map_entry_t next, prev;
1238 vm_size_t prevsize, esize;
1240 if (entry->eflags & (MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP))
1244 if (prev != &map->header) {
1245 prevsize = prev->end - prev->start;
1246 if ( (prev->end == entry->start) &&
1247 (prev->object.vm_object == entry->object.vm_object) &&
1248 (!prev->object.vm_object ||
1249 (prev->offset + prevsize == entry->offset)) &&
1250 (prev->eflags == entry->eflags) &&
1251 (prev->protection == entry->protection) &&
1252 (prev->max_protection == entry->max_protection) &&
1253 (prev->inheritance == entry->inheritance) &&
1254 (prev->wired_count == entry->wired_count)) {
1255 vm_map_entry_unlink(map, prev);
1256 entry->start = prev->start;
1257 entry->offset = prev->offset;
1258 if (entry->prev != &map->header)
1259 vm_map_entry_resize_free(map, entry->prev);
1260 if (prev->object.vm_object)
1261 vm_object_deallocate(prev->object.vm_object);
1262 vm_map_entry_dispose(map, prev);
1267 if (next != &map->header) {
1268 esize = entry->end - entry->start;
1269 if ((entry->end == next->start) &&
1270 (next->object.vm_object == entry->object.vm_object) &&
1271 (!entry->object.vm_object ||
1272 (entry->offset + esize == next->offset)) &&
1273 (next->eflags == entry->eflags) &&
1274 (next->protection == entry->protection) &&
1275 (next->max_protection == entry->max_protection) &&
1276 (next->inheritance == entry->inheritance) &&
1277 (next->wired_count == entry->wired_count)) {
1278 vm_map_entry_unlink(map, next);
1279 entry->end = next->end;
1280 vm_map_entry_resize_free(map, entry);
1281 if (next->object.vm_object)
1282 vm_object_deallocate(next->object.vm_object);
1283 vm_map_entry_dispose(map, next);
1288 * vm_map_clip_start: [ internal use only ]
1290 * Asserts that the given entry begins at or after
1291 * the specified address; if necessary,
1292 * it splits the entry into two.
1294 #define vm_map_clip_start(map, entry, startaddr) \
1296 if (startaddr > entry->start) \
1297 _vm_map_clip_start(map, entry, startaddr); \
1301 * This routine is called only when it is known that
1302 * the entry must be split.
1305 _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start)
1307 vm_map_entry_t new_entry;
1310 * Split off the front portion -- note that we must insert the new
1311 * entry BEFORE this one, so that this entry has the specified
1314 vm_map_simplify_entry(map, entry);
1317 * If there is no object backing this entry, we might as well create
1318 * one now. If we defer it, an object can get created after the map
1319 * is clipped, and individual objects will be created for the split-up
1320 * map. This is a bit of a hack, but is also about the best place to
1321 * put this improvement.
1323 if (entry->object.vm_object == NULL && !map->system_map) {
1325 object = vm_object_allocate(OBJT_DEFAULT,
1326 atop(entry->end - entry->start));
1327 entry->object.vm_object = object;
1331 new_entry = vm_map_entry_create(map);
1332 *new_entry = *entry;
1334 new_entry->end = start;
1335 entry->offset += (start - entry->start);
1336 entry->start = start;
1338 vm_map_entry_link(map, entry->prev, new_entry);
1340 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1341 vm_object_reference(new_entry->object.vm_object);
1346 * vm_map_clip_end: [ internal use only ]
1348 * Asserts that the given entry ends at or before
1349 * the specified address; if necessary,
1350 * it splits the entry into two.
1352 #define vm_map_clip_end(map, entry, endaddr) \
1354 if ((endaddr) < (entry->end)) \
1355 _vm_map_clip_end((map), (entry), (endaddr)); \
1359 * This routine is called only when it is known that
1360 * the entry must be split.
1363 _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end)
1365 vm_map_entry_t new_entry;
1368 * If there is no object backing this entry, we might as well create
1369 * one now. If we defer it, an object can get created after the map
1370 * is clipped, and individual objects will be created for the split-up
1371 * map. This is a bit of a hack, but is also about the best place to
1372 * put this improvement.
1374 if (entry->object.vm_object == NULL && !map->system_map) {
1376 object = vm_object_allocate(OBJT_DEFAULT,
1377 atop(entry->end - entry->start));
1378 entry->object.vm_object = object;
1383 * Create a new entry and insert it AFTER the specified entry
1385 new_entry = vm_map_entry_create(map);
1386 *new_entry = *entry;
1388 new_entry->start = entry->end = end;
1389 new_entry->offset += (end - entry->start);
1391 vm_map_entry_link(map, entry, new_entry);
1393 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1394 vm_object_reference(new_entry->object.vm_object);
1399 * vm_map_submap: [ kernel use only ]
1401 * Mark the given range as handled by a subordinate map.
1403 * This range must have been created with vm_map_find,
1404 * and no other operations may have been performed on this
1405 * range prior to calling vm_map_submap.
1407 * Only a limited number of operations can be performed
1408 * within this rage after calling vm_map_submap:
1410 * [Don't try vm_map_copy!]
1412 * To remove a submapping, one must first remove the
1413 * range from the superior map, and then destroy the
1414 * submap (if desired). [Better yet, don't try it.]
1423 vm_map_entry_t entry;
1424 int result = KERN_INVALID_ARGUMENT;
1428 VM_MAP_RANGE_CHECK(map, start, end);
1430 if (vm_map_lookup_entry(map, start, &entry)) {
1431 vm_map_clip_start(map, entry, start);
1433 entry = entry->next;
1435 vm_map_clip_end(map, entry, end);
1437 if ((entry->start == start) && (entry->end == end) &&
1438 ((entry->eflags & MAP_ENTRY_COW) == 0) &&
1439 (entry->object.vm_object == NULL)) {
1440 entry->object.sub_map = submap;
1441 entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
1442 result = KERN_SUCCESS;
1450 * The maximum number of pages to map
1452 #define MAX_INIT_PT 96
1455 * vm_map_pmap_enter:
1457 * Preload read-only mappings for the given object into the specified
1458 * map. This eliminates the soft faults on process startup and
1459 * immediately after an mmap(2).
1462 vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
1463 vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags)
1466 vm_page_t p, p_start;
1467 vm_pindex_t psize, tmpidx;
1468 boolean_t are_queues_locked;
1470 if ((prot & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 || object == NULL)
1472 VM_OBJECT_LOCK(object);
1473 if (object->type == OBJT_DEVICE) {
1474 pmap_object_init_pt(map->pmap, addr, object, pindex, size);
1480 if (object->type != OBJT_VNODE ||
1481 ((flags & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
1482 (object->resident_page_count > MAX_INIT_PT))) {
1486 if (psize + pindex > object->size) {
1487 if (object->size < pindex)
1489 psize = object->size - pindex;
1492 are_queues_locked = FALSE;
1496 if ((p = TAILQ_FIRST(&object->memq)) != NULL) {
1497 if (p->pindex < pindex) {
1498 p = vm_page_splay(pindex, object->root);
1499 if ((object->root = p)->pindex < pindex)
1500 p = TAILQ_NEXT(p, listq);
1504 * Assert: the variable p is either (1) the page with the
1505 * least pindex greater than or equal to the parameter pindex
1509 p != NULL && (tmpidx = p->pindex - pindex) < psize;
1510 p = TAILQ_NEXT(p, listq)) {
1512 * don't allow an madvise to blow away our really
1513 * free pages allocating pv entries.
1515 if ((flags & MAP_PREFAULT_MADVISE) &&
1516 cnt.v_free_count < cnt.v_free_reserved) {
1520 if ((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL &&
1522 if (p_start == NULL) {
1523 start = addr + ptoa(tmpidx);
1526 if (!are_queues_locked) {
1527 are_queues_locked = TRUE;
1528 vm_page_lock_queues();
1530 if ((p->queue - p->pc) == PQ_CACHE)
1531 vm_page_deactivate(p);
1532 } else if (p_start != NULL) {
1533 pmap_enter_object(map->pmap, start, addr +
1534 ptoa(tmpidx), p_start, prot);
1538 if (p_start != NULL)
1539 pmap_enter_object(map->pmap, start, addr + ptoa(psize),
1541 if (are_queues_locked)
1542 vm_page_unlock_queues();
1544 VM_OBJECT_UNLOCK(object);
1550 * Sets the protection of the specified address
1551 * region in the target map. If "set_max" is
1552 * specified, the maximum protection is to be set;
1553 * otherwise, only the current protection is affected.
1556 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
1557 vm_prot_t new_prot, boolean_t set_max)
1559 vm_map_entry_t current;
1560 vm_map_entry_t entry;
1564 VM_MAP_RANGE_CHECK(map, start, end);
1566 if (vm_map_lookup_entry(map, start, &entry)) {
1567 vm_map_clip_start(map, entry, start);
1569 entry = entry->next;
1573 * Make a first pass to check for protection violations.
1576 while ((current != &map->header) && (current->start < end)) {
1577 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1579 return (KERN_INVALID_ARGUMENT);
1581 if ((new_prot & current->max_protection) != new_prot) {
1583 return (KERN_PROTECTION_FAILURE);
1585 current = current->next;
1589 * Go back and fix up protections. [Note that clipping is not
1590 * necessary the second time.]
1593 while ((current != &map->header) && (current->start < end)) {
1596 vm_map_clip_end(map, current, end);
1598 old_prot = current->protection;
1600 current->protection =
1601 (current->max_protection = new_prot) &
1604 current->protection = new_prot;
1607 * Update physical map if necessary. Worry about copy-on-write
1608 * here -- CHECK THIS XXX
1610 if (current->protection != old_prot) {
1611 #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1613 pmap_protect(map->pmap, current->start,
1615 current->protection & MASK(current));
1618 vm_map_simplify_entry(map, current);
1619 current = current->next;
1622 return (KERN_SUCCESS);
1628 * This routine traverses a processes map handling the madvise
1629 * system call. Advisories are classified as either those effecting
1630 * the vm_map_entry structure, or those effecting the underlying
1640 vm_map_entry_t current, entry;
1644 * Some madvise calls directly modify the vm_map_entry, in which case
1645 * we need to use an exclusive lock on the map and we need to perform
1646 * various clipping operations. Otherwise we only need a read-lock
1651 case MADV_SEQUENTIAL:
1663 vm_map_lock_read(map);
1666 return (KERN_INVALID_ARGUMENT);
1670 * Locate starting entry and clip if necessary.
1672 VM_MAP_RANGE_CHECK(map, start, end);
1674 if (vm_map_lookup_entry(map, start, &entry)) {
1676 vm_map_clip_start(map, entry, start);
1678 entry = entry->next;
1683 * madvise behaviors that are implemented in the vm_map_entry.
1685 * We clip the vm_map_entry so that behavioral changes are
1686 * limited to the specified address range.
1688 for (current = entry;
1689 (current != &map->header) && (current->start < end);
1690 current = current->next
1692 if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1695 vm_map_clip_end(map, current, end);
1699 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
1701 case MADV_SEQUENTIAL:
1702 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
1705 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
1708 current->eflags |= MAP_ENTRY_NOSYNC;
1711 current->eflags &= ~MAP_ENTRY_NOSYNC;
1714 current->eflags |= MAP_ENTRY_NOCOREDUMP;
1717 current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
1722 vm_map_simplify_entry(map, current);
1730 * madvise behaviors that are implemented in the underlying
1733 * Since we don't clip the vm_map_entry, we have to clip
1734 * the vm_object pindex and count.
1736 for (current = entry;
1737 (current != &map->header) && (current->start < end);
1738 current = current->next
1740 vm_offset_t useStart;
1742 if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1745 pindex = OFF_TO_IDX(current->offset);
1746 count = atop(current->end - current->start);
1747 useStart = current->start;
1749 if (current->start < start) {
1750 pindex += atop(start - current->start);
1751 count -= atop(start - current->start);
1754 if (current->end > end)
1755 count -= atop(current->end - end);
1760 vm_object_madvise(current->object.vm_object,
1761 pindex, count, behav);
1762 if (behav == MADV_WILLNEED) {
1763 vm_map_pmap_enter(map,
1765 current->protection,
1766 current->object.vm_object,
1768 (count << PAGE_SHIFT),
1769 MAP_PREFAULT_MADVISE
1773 vm_map_unlock_read(map);
1782 * Sets the inheritance of the specified address
1783 * range in the target map. Inheritance
1784 * affects how the map will be shared with
1785 * child maps at the time of vm_map_fork.
1788 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
1789 vm_inherit_t new_inheritance)
1791 vm_map_entry_t entry;
1792 vm_map_entry_t temp_entry;
1794 switch (new_inheritance) {
1795 case VM_INHERIT_NONE:
1796 case VM_INHERIT_COPY:
1797 case VM_INHERIT_SHARE:
1800 return (KERN_INVALID_ARGUMENT);
1803 VM_MAP_RANGE_CHECK(map, start, end);
1804 if (vm_map_lookup_entry(map, start, &temp_entry)) {
1806 vm_map_clip_start(map, entry, start);
1808 entry = temp_entry->next;
1809 while ((entry != &map->header) && (entry->start < end)) {
1810 vm_map_clip_end(map, entry, end);
1811 entry->inheritance = new_inheritance;
1812 vm_map_simplify_entry(map, entry);
1813 entry = entry->next;
1816 return (KERN_SUCCESS);
1822 * Implements both kernel and user unwiring.
1825 vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
1828 vm_map_entry_t entry, first_entry, tmp_entry;
1829 vm_offset_t saved_start;
1830 unsigned int last_timestamp;
1832 boolean_t need_wakeup, result, user_unwire;
1834 user_unwire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
1836 VM_MAP_RANGE_CHECK(map, start, end);
1837 if (!vm_map_lookup_entry(map, start, &first_entry)) {
1838 if (flags & VM_MAP_WIRE_HOLESOK)
1839 first_entry = first_entry->next;
1842 return (KERN_INVALID_ADDRESS);
1845 last_timestamp = map->timestamp;
1846 entry = first_entry;
1847 while (entry != &map->header && entry->start < end) {
1848 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1850 * We have not yet clipped the entry.
1852 saved_start = (start >= entry->start) ? start :
1854 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1855 if (vm_map_unlock_and_wait(map, user_unwire)) {
1857 * Allow interruption of user unwiring?
1861 if (last_timestamp+1 != map->timestamp) {
1863 * Look again for the entry because the map was
1864 * modified while it was unlocked.
1865 * Specifically, the entry may have been
1866 * clipped, merged, or deleted.
1868 if (!vm_map_lookup_entry(map, saved_start,
1870 if (flags & VM_MAP_WIRE_HOLESOK)
1871 tmp_entry = tmp_entry->next;
1873 if (saved_start == start) {
1875 * First_entry has been deleted.
1878 return (KERN_INVALID_ADDRESS);
1881 rv = KERN_INVALID_ADDRESS;
1885 if (entry == first_entry)
1886 first_entry = tmp_entry;
1891 last_timestamp = map->timestamp;
1894 vm_map_clip_start(map, entry, start);
1895 vm_map_clip_end(map, entry, end);
1897 * Mark the entry in case the map lock is released. (See
1900 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
1902 * Check the map for holes in the specified region.
1903 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
1905 if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
1906 (entry->end < end && (entry->next == &map->header ||
1907 entry->next->start > entry->end))) {
1909 rv = KERN_INVALID_ADDRESS;
1913 * If system unwiring, require that the entry is system wired.
1916 vm_map_entry_system_wired_count(entry) == 0) {
1918 rv = KERN_INVALID_ARGUMENT;
1921 entry = entry->next;
1925 need_wakeup = FALSE;
1926 if (first_entry == NULL) {
1927 result = vm_map_lookup_entry(map, start, &first_entry);
1928 if (!result && (flags & VM_MAP_WIRE_HOLESOK))
1929 first_entry = first_entry->next;
1931 KASSERT(result, ("vm_map_unwire: lookup failed"));
1933 entry = first_entry;
1934 while (entry != &map->header && entry->start < end) {
1935 if (rv == KERN_SUCCESS && (!user_unwire ||
1936 (entry->eflags & MAP_ENTRY_USER_WIRED))) {
1938 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
1939 entry->wired_count--;
1940 if (entry->wired_count == 0) {
1942 * Retain the map lock.
1944 vm_fault_unwire(map, entry->start, entry->end,
1945 entry->object.vm_object != NULL &&
1946 entry->object.vm_object->type == OBJT_DEVICE);
1949 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
1950 ("vm_map_unwire: in-transition flag missing"));
1951 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
1952 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
1953 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
1956 vm_map_simplify_entry(map, entry);
1957 entry = entry->next;
1968 * Implements both kernel and user wiring.
1971 vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
1974 vm_map_entry_t entry, first_entry, tmp_entry;
1975 vm_offset_t saved_end, saved_start;
1976 unsigned int last_timestamp;
1978 boolean_t fictitious, need_wakeup, result, user_wire;
1980 user_wire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
1982 VM_MAP_RANGE_CHECK(map, start, end);
1983 if (!vm_map_lookup_entry(map, start, &first_entry)) {
1984 if (flags & VM_MAP_WIRE_HOLESOK)
1985 first_entry = first_entry->next;
1988 return (KERN_INVALID_ADDRESS);
1991 last_timestamp = map->timestamp;
1992 entry = first_entry;
1993 while (entry != &map->header && entry->start < end) {
1994 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1996 * We have not yet clipped the entry.
1998 saved_start = (start >= entry->start) ? start :
2000 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2001 if (vm_map_unlock_and_wait(map, user_wire)) {
2003 * Allow interruption of user wiring?
2007 if (last_timestamp + 1 != map->timestamp) {
2009 * Look again for the entry because the map was
2010 * modified while it was unlocked.
2011 * Specifically, the entry may have been
2012 * clipped, merged, or deleted.
2014 if (!vm_map_lookup_entry(map, saved_start,
2016 if (flags & VM_MAP_WIRE_HOLESOK)
2017 tmp_entry = tmp_entry->next;
2019 if (saved_start == start) {
2021 * first_entry has been deleted.
2024 return (KERN_INVALID_ADDRESS);
2027 rv = KERN_INVALID_ADDRESS;
2031 if (entry == first_entry)
2032 first_entry = tmp_entry;
2037 last_timestamp = map->timestamp;
2040 vm_map_clip_start(map, entry, start);
2041 vm_map_clip_end(map, entry, end);
2043 * Mark the entry in case the map lock is released. (See
2046 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
2050 if (entry->wired_count == 0) {
2051 entry->wired_count++;
2052 saved_start = entry->start;
2053 saved_end = entry->end;
2054 fictitious = entry->object.vm_object != NULL &&
2055 entry->object.vm_object->type == OBJT_DEVICE;
2057 * Release the map lock, relying on the in-transition
2061 rv = vm_fault_wire(map, saved_start, saved_end,
2062 user_wire, fictitious);
2064 if (last_timestamp + 1 != map->timestamp) {
2066 * Look again for the entry because the map was
2067 * modified while it was unlocked. The entry
2068 * may have been clipped, but NOT merged or
2071 result = vm_map_lookup_entry(map, saved_start,
2073 KASSERT(result, ("vm_map_wire: lookup failed"));
2074 if (entry == first_entry)
2075 first_entry = tmp_entry;
2079 while (entry->end < saved_end) {
2080 if (rv != KERN_SUCCESS) {
2081 KASSERT(entry->wired_count == 1,
2082 ("vm_map_wire: bad count"));
2083 entry->wired_count = -1;
2085 entry = entry->next;
2088 last_timestamp = map->timestamp;
2089 if (rv != KERN_SUCCESS) {
2090 KASSERT(entry->wired_count == 1,
2091 ("vm_map_wire: bad count"));
2093 * Assign an out-of-range value to represent
2094 * the failure to wire this entry.
2096 entry->wired_count = -1;
2100 } else if (!user_wire ||
2101 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
2102 entry->wired_count++;
2105 * Check the map for holes in the specified region.
2106 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
2108 if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
2109 (entry->end < end && (entry->next == &map->header ||
2110 entry->next->start > entry->end))) {
2112 rv = KERN_INVALID_ADDRESS;
2115 entry = entry->next;
2119 need_wakeup = FALSE;
2120 if (first_entry == NULL) {
2121 result = vm_map_lookup_entry(map, start, &first_entry);
2122 if (!result && (flags & VM_MAP_WIRE_HOLESOK))
2123 first_entry = first_entry->next;
2125 KASSERT(result, ("vm_map_wire: lookup failed"));
2127 entry = first_entry;
2128 while (entry != &map->header && entry->start < end) {
2129 if (rv == KERN_SUCCESS) {
2131 entry->eflags |= MAP_ENTRY_USER_WIRED;
2132 } else if (entry->wired_count == -1) {
2134 * Wiring failed on this entry. Thus, unwiring is
2137 entry->wired_count = 0;
2140 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0)
2141 entry->wired_count--;
2142 if (entry->wired_count == 0) {
2144 * Retain the map lock.
2146 vm_fault_unwire(map, entry->start, entry->end,
2147 entry->object.vm_object != NULL &&
2148 entry->object.vm_object->type == OBJT_DEVICE);
2151 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
2152 ("vm_map_wire: in-transition flag missing"));
2153 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
2154 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
2155 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
2158 vm_map_simplify_entry(map, entry);
2159 entry = entry->next;
2170 * Push any dirty cached pages in the address range to their pager.
2171 * If syncio is TRUE, dirty pages are written synchronously.
2172 * If invalidate is TRUE, any cached pages are freed as well.
2174 * If the size of the region from start to end is zero, we are
2175 * supposed to flush all modified pages within the region containing
2176 * start. Unfortunately, a region can be split or coalesced with
2177 * neighboring regions, making it difficult to determine what the
2178 * original region was. Therefore, we approximate this requirement by
2179 * flushing the current region containing start.
2181 * Returns an error if any part of the specified range is not mapped.
2189 boolean_t invalidate)
2191 vm_map_entry_t current;
2192 vm_map_entry_t entry;
2195 vm_ooffset_t offset;
2197 vm_map_lock_read(map);
2198 VM_MAP_RANGE_CHECK(map, start, end);
2199 if (!vm_map_lookup_entry(map, start, &entry)) {
2200 vm_map_unlock_read(map);
2201 return (KERN_INVALID_ADDRESS);
2202 } else if (start == end) {
2203 start = entry->start;
2207 * Make a first pass to check for user-wired memory and holes.
2209 for (current = entry; current != &map->header && current->start < end;
2210 current = current->next) {
2211 if (invalidate && (current->eflags & MAP_ENTRY_USER_WIRED)) {
2212 vm_map_unlock_read(map);
2213 return (KERN_INVALID_ARGUMENT);
2215 if (end > current->end &&
2216 (current->next == &map->header ||
2217 current->end != current->next->start)) {
2218 vm_map_unlock_read(map);
2219 return (KERN_INVALID_ADDRESS);
2225 pmap_remove(map->pmap, start, end);
2229 * Make a second pass, cleaning/uncaching pages from the indicated
2232 for (current = entry; current != &map->header && current->start < end;
2233 current = current->next) {
2234 offset = current->offset + (start - current->start);
2235 size = (end <= current->end ? end : current->end) - start;
2236 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
2238 vm_map_entry_t tentry;
2241 smap = current->object.sub_map;
2242 vm_map_lock_read(smap);
2243 (void) vm_map_lookup_entry(smap, offset, &tentry);
2244 tsize = tentry->end - offset;
2247 object = tentry->object.vm_object;
2248 offset = tentry->offset + (offset - tentry->start);
2249 vm_map_unlock_read(smap);
2251 object = current->object.vm_object;
2253 vm_object_sync(object, offset, size, syncio, invalidate);
2257 vm_map_unlock_read(map);
2258 return (KERN_SUCCESS);
2262 * vm_map_entry_unwire: [ internal use only ]
2264 * Make the region specified by this entry pageable.
2266 * The map in question should be locked.
2267 * [This is the reason for this routine's existence.]
2270 vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
2272 vm_fault_unwire(map, entry->start, entry->end,
2273 entry->object.vm_object != NULL &&
2274 entry->object.vm_object->type == OBJT_DEVICE);
2275 entry->wired_count = 0;
2279 * vm_map_entry_delete: [ internal use only ]
2281 * Deallocate the given entry from the target map.
2284 vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry)
2287 vm_pindex_t offidxstart, offidxend, count;
2289 vm_map_entry_unlink(map, entry);
2290 map->size -= entry->end - entry->start;
2292 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
2293 (object = entry->object.vm_object) != NULL) {
2294 count = OFF_TO_IDX(entry->end - entry->start);
2295 offidxstart = OFF_TO_IDX(entry->offset);
2296 offidxend = offidxstart + count;
2297 VM_OBJECT_LOCK(object);
2298 if (object->ref_count != 1 &&
2299 ((object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING ||
2300 object == kernel_object || object == kmem_object) &&
2301 (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2302 vm_object_collapse(object);
2303 vm_object_page_remove(object, offidxstart, offidxend, FALSE);
2304 if (object->type == OBJT_SWAP)
2305 swap_pager_freespace(object, offidxstart, count);
2306 if (offidxend >= object->size &&
2307 offidxstart < object->size)
2308 object->size = offidxstart;
2310 VM_OBJECT_UNLOCK(object);
2311 vm_object_deallocate(object);
2314 vm_map_entry_dispose(map, entry);
2318 * vm_map_delete: [ internal use only ]
2320 * Deallocates the given address range from the target
2324 vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end)
2326 vm_map_entry_t entry;
2327 vm_map_entry_t first_entry;
2330 * Find the start of the region, and clip it
2332 if (!vm_map_lookup_entry(map, start, &first_entry))
2333 entry = first_entry->next;
2335 entry = first_entry;
2336 vm_map_clip_start(map, entry, start);
2340 * Step through all entries in this region
2342 while ((entry != &map->header) && (entry->start < end)) {
2343 vm_map_entry_t next;
2346 * Wait for wiring or unwiring of an entry to complete.
2347 * Also wait for any system wirings to disappear on
2350 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 ||
2351 (vm_map_pmap(map) != kernel_pmap &&
2352 vm_map_entry_system_wired_count(entry) != 0)) {
2353 unsigned int last_timestamp;
2354 vm_offset_t saved_start;
2355 vm_map_entry_t tmp_entry;
2357 saved_start = entry->start;
2358 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2359 last_timestamp = map->timestamp;
2360 (void) vm_map_unlock_and_wait(map, FALSE);
2362 if (last_timestamp + 1 != map->timestamp) {
2364 * Look again for the entry because the map was
2365 * modified while it was unlocked.
2366 * Specifically, the entry may have been
2367 * clipped, merged, or deleted.
2369 if (!vm_map_lookup_entry(map, saved_start,
2371 entry = tmp_entry->next;
2374 vm_map_clip_start(map, entry,
2380 vm_map_clip_end(map, entry, end);
2385 * Unwire before removing addresses from the pmap; otherwise,
2386 * unwiring will put the entries back in the pmap.
2388 if (entry->wired_count != 0) {
2389 vm_map_entry_unwire(map, entry);
2392 if (!map->system_map)
2394 pmap_remove(map->pmap, entry->start, entry->end);
2395 if (!map->system_map)
2399 * Delete the entry (which may delete the object) only after
2400 * removing all pmap entries pointing to its pages.
2401 * (Otherwise, its page frames may be reallocated, and any
2402 * modify bits will be set in the wrong object!)
2404 vm_map_entry_delete(map, entry);
2407 return (KERN_SUCCESS);
2413 * Remove the given address range from the target map.
2414 * This is the exported form of vm_map_delete.
2417 vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
2422 VM_MAP_RANGE_CHECK(map, start, end);
2423 result = vm_map_delete(map, start, end);
2429 * vm_map_check_protection:
2431 * Assert that the target map allows the specified privilege on the
2432 * entire address region given. The entire region must be allocated.
2434 * WARNING! This code does not and should not check whether the
2435 * contents of the region is accessible. For example a smaller file
2436 * might be mapped into a larger address space.
2438 * NOTE! This code is also called by munmap().
2440 * The map must be locked. A read lock is sufficient.
2443 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
2444 vm_prot_t protection)
2446 vm_map_entry_t entry;
2447 vm_map_entry_t tmp_entry;
2449 if (!vm_map_lookup_entry(map, start, &tmp_entry))
2453 while (start < end) {
2454 if (entry == &map->header)
2459 if (start < entry->start)
2462 * Check protection associated with entry.
2464 if ((entry->protection & protection) != protection)
2466 /* go to next entry */
2468 entry = entry->next;
2474 * vm_map_copy_entry:
2476 * Copies the contents of the source entry to the destination
2477 * entry. The entries *must* be aligned properly.
2483 vm_map_entry_t src_entry,
2484 vm_map_entry_t dst_entry)
2486 vm_object_t src_object;
2488 if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
2491 if (src_entry->wired_count == 0) {
2494 * If the source entry is marked needs_copy, it is already
2497 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
2498 pmap_protect(src_map->pmap,
2501 src_entry->protection & ~VM_PROT_WRITE);
2505 * Make a copy of the object.
2507 if ((src_object = src_entry->object.vm_object) != NULL) {
2508 VM_OBJECT_LOCK(src_object);
2509 if ((src_object->handle == NULL) &&
2510 (src_object->type == OBJT_DEFAULT ||
2511 src_object->type == OBJT_SWAP)) {
2512 vm_object_collapse(src_object);
2513 if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
2514 vm_object_split(src_entry);
2515 src_object = src_entry->object.vm_object;
2518 vm_object_reference_locked(src_object);
2519 vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
2520 VM_OBJECT_UNLOCK(src_object);
2521 dst_entry->object.vm_object = src_object;
2522 src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2523 dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2524 dst_entry->offset = src_entry->offset;
2526 dst_entry->object.vm_object = NULL;
2527 dst_entry->offset = 0;
2530 pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
2531 dst_entry->end - dst_entry->start, src_entry->start);
2534 * Of course, wired down pages can't be set copy-on-write.
2535 * Cause wired pages to be copied into the new map by
2536 * simulating faults (the new pages are pageable)
2538 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry);
2543 * vmspace_map_entry_forked:
2544 * Update the newly-forked vmspace each time a map entry is inherited
2545 * or copied. The values for vm_dsize and vm_tsize are approximate
2546 * (and mostly-obsolete ideas in the face of mmap(2) et al.)
2549 vmspace_map_entry_forked(const struct vmspace *vm1, struct vmspace *vm2,
2550 vm_map_entry_t entry)
2552 vm_size_t entrysize;
2555 entrysize = entry->end - entry->start;
2556 vm2->vm_map.size += entrysize;
2557 if (entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP)) {
2558 vm2->vm_ssize += btoc(entrysize);
2559 } else if (entry->start >= (vm_offset_t)vm1->vm_daddr &&
2560 entry->start < (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)) {
2561 newend = MIN(entry->end,
2562 (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize));
2563 vm2->vm_dsize += btoc(newend - entry->start);
2564 } else if (entry->start >= (vm_offset_t)vm1->vm_taddr &&
2565 entry->start < (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)) {
2566 newend = MIN(entry->end,
2567 (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize));
2568 vm2->vm_tsize += btoc(newend - entry->start);
2574 * Create a new process vmspace structure and vm_map
2575 * based on those of an existing process. The new map
2576 * is based on the old map, according to the inheritance
2577 * values on the regions in that map.
2579 * XXX It might be worth coalescing the entries added to the new vmspace.
2581 * The source map must not be locked.
2584 vmspace_fork(struct vmspace *vm1)
2586 struct vmspace *vm2;
2587 vm_map_t old_map = &vm1->vm_map;
2589 vm_map_entry_t old_entry;
2590 vm_map_entry_t new_entry;
2593 vm_map_lock(old_map);
2595 vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
2596 vm2->vm_taddr = vm1->vm_taddr;
2597 vm2->vm_daddr = vm1->vm_daddr;
2598 vm2->vm_maxsaddr = vm1->vm_maxsaddr;
2599 new_map = &vm2->vm_map; /* XXX */
2600 new_map->timestamp = 1;
2602 /* Do not inherit the MAP_WIREFUTURE property. */
2603 if ((new_map->flags & MAP_WIREFUTURE) == MAP_WIREFUTURE)
2604 new_map->flags &= ~MAP_WIREFUTURE;
2606 old_entry = old_map->header.next;
2608 while (old_entry != &old_map->header) {
2609 if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2610 panic("vm_map_fork: encountered a submap");
2612 switch (old_entry->inheritance) {
2613 case VM_INHERIT_NONE:
2616 case VM_INHERIT_SHARE:
2618 * Clone the entry, creating the shared object if necessary.
2620 object = old_entry->object.vm_object;
2621 if (object == NULL) {
2622 object = vm_object_allocate(OBJT_DEFAULT,
2623 atop(old_entry->end - old_entry->start));
2624 old_entry->object.vm_object = object;
2625 old_entry->offset = 0;
2629 * Add the reference before calling vm_object_shadow
2630 * to insure that a shadow object is created.
2632 vm_object_reference(object);
2633 if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
2634 vm_object_shadow(&old_entry->object.vm_object,
2636 atop(old_entry->end - old_entry->start));
2637 old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
2638 /* Transfer the second reference too. */
2639 vm_object_reference(
2640 old_entry->object.vm_object);
2641 vm_object_deallocate(object);
2642 object = old_entry->object.vm_object;
2644 VM_OBJECT_LOCK(object);
2645 vm_object_clear_flag(object, OBJ_ONEMAPPING);
2646 VM_OBJECT_UNLOCK(object);
2649 * Clone the entry, referencing the shared object.
2651 new_entry = vm_map_entry_create(new_map);
2652 *new_entry = *old_entry;
2653 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2654 new_entry->wired_count = 0;
2657 * Insert the entry into the new map -- we know we're
2658 * inserting at the end of the new map.
2660 vm_map_entry_link(new_map, new_map->header.prev,
2662 vmspace_map_entry_forked(vm1, vm2, new_entry);
2665 * Update the physical map
2667 pmap_copy(new_map->pmap, old_map->pmap,
2669 (old_entry->end - old_entry->start),
2673 case VM_INHERIT_COPY:
2675 * Clone the entry and link into the map.
2677 new_entry = vm_map_entry_create(new_map);
2678 *new_entry = *old_entry;
2679 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2680 new_entry->wired_count = 0;
2681 new_entry->object.vm_object = NULL;
2682 vm_map_entry_link(new_map, new_map->header.prev,
2684 vmspace_map_entry_forked(vm1, vm2, new_entry);
2685 vm_map_copy_entry(old_map, new_map, old_entry,
2689 old_entry = old_entry->next;
2692 vm_map_unlock(old_map);
2698 vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
2699 vm_prot_t prot, vm_prot_t max, int cow)
2701 vm_map_entry_t new_entry, prev_entry;
2702 vm_offset_t bot, top;
2703 vm_size_t init_ssize;
2708 * The stack orientation is piggybacked with the cow argument.
2709 * Extract it into orient and mask the cow argument so that we
2710 * don't pass it around further.
2711 * NOTE: We explicitly allow bi-directional stacks.
2713 orient = cow & (MAP_STACK_GROWS_DOWN|MAP_STACK_GROWS_UP);
2715 KASSERT(orient != 0, ("No stack grow direction"));
2717 if (addrbos < vm_map_min(map) ||
2718 addrbos > vm_map_max(map) ||
2719 addrbos + max_ssize < addrbos)
2720 return (KERN_NO_SPACE);
2722 init_ssize = (max_ssize < sgrowsiz) ? max_ssize : sgrowsiz;
2724 PROC_LOCK(curthread->td_proc);
2725 vmemlim = lim_cur(curthread->td_proc, RLIMIT_VMEM);
2726 PROC_UNLOCK(curthread->td_proc);
2730 /* If addr is already mapped, no go */
2731 if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
2733 return (KERN_NO_SPACE);
2736 /* If we would blow our VMEM resource limit, no go */
2737 if (map->size + init_ssize > vmemlim) {
2739 return (KERN_NO_SPACE);
2743 * If we can't accomodate max_ssize in the current mapping, no go.
2744 * However, we need to be aware that subsequent user mappings might
2745 * map into the space we have reserved for stack, and currently this
2746 * space is not protected.
2748 * Hopefully we will at least detect this condition when we try to
2751 if ((prev_entry->next != &map->header) &&
2752 (prev_entry->next->start < addrbos + max_ssize)) {
2754 return (KERN_NO_SPACE);
2758 * We initially map a stack of only init_ssize. We will grow as
2759 * needed later. Depending on the orientation of the stack (i.e.
2760 * the grow direction) we either map at the top of the range, the
2761 * bottom of the range or in the middle.
2763 * Note: we would normally expect prot and max to be VM_PROT_ALL,
2764 * and cow to be 0. Possibly we should eliminate these as input
2765 * parameters, and just pass these values here in the insert call.
2767 if (orient == MAP_STACK_GROWS_DOWN)
2768 bot = addrbos + max_ssize - init_ssize;
2769 else if (orient == MAP_STACK_GROWS_UP)
2772 bot = round_page(addrbos + max_ssize/2 - init_ssize/2);
2773 top = bot + init_ssize;
2774 rv = vm_map_insert(map, NULL, 0, bot, top, prot, max, cow);
2776 /* Now set the avail_ssize amount. */
2777 if (rv == KERN_SUCCESS) {
2778 if (prev_entry != &map->header)
2779 vm_map_clip_end(map, prev_entry, bot);
2780 new_entry = prev_entry->next;
2781 if (new_entry->end != top || new_entry->start != bot)
2782 panic("Bad entry start/end for new stack entry");
2784 new_entry->avail_ssize = max_ssize - init_ssize;
2785 if (orient & MAP_STACK_GROWS_DOWN)
2786 new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
2787 if (orient & MAP_STACK_GROWS_UP)
2788 new_entry->eflags |= MAP_ENTRY_GROWS_UP;
2795 /* Attempts to grow a vm stack entry. Returns KERN_SUCCESS if the
2796 * desired address is already mapped, or if we successfully grow
2797 * the stack. Also returns KERN_SUCCESS if addr is outside the
2798 * stack range (this is strange, but preserves compatibility with
2799 * the grow function in vm_machdep.c).
2802 vm_map_growstack(struct proc *p, vm_offset_t addr)
2804 vm_map_entry_t next_entry, prev_entry;
2805 vm_map_entry_t new_entry, stack_entry;
2806 struct vmspace *vm = p->p_vmspace;
2807 vm_map_t map = &vm->vm_map;
2809 size_t grow_amount, max_grow;
2810 rlim_t stacklim, vmemlim;
2811 int is_procstack, rv;
2815 stacklim = lim_cur(p, RLIMIT_STACK);
2816 vmemlim = lim_cur(p, RLIMIT_VMEM);
2819 vm_map_lock_read(map);
2821 /* If addr is already in the entry range, no need to grow.*/
2822 if (vm_map_lookup_entry(map, addr, &prev_entry)) {
2823 vm_map_unlock_read(map);
2824 return (KERN_SUCCESS);
2827 next_entry = prev_entry->next;
2828 if (!(prev_entry->eflags & MAP_ENTRY_GROWS_UP)) {
2830 * This entry does not grow upwards. Since the address lies
2831 * beyond this entry, the next entry (if one exists) has to
2832 * be a downward growable entry. The entry list header is
2833 * never a growable entry, so it suffices to check the flags.
2835 if (!(next_entry->eflags & MAP_ENTRY_GROWS_DOWN)) {
2836 vm_map_unlock_read(map);
2837 return (KERN_SUCCESS);
2839 stack_entry = next_entry;
2842 * This entry grows upward. If the next entry does not at
2843 * least grow downwards, this is the entry we need to grow.
2844 * otherwise we have two possible choices and we have to
2847 if (next_entry->eflags & MAP_ENTRY_GROWS_DOWN) {
2849 * We have two choices; grow the entry closest to
2850 * the address to minimize the amount of growth.
2852 if (addr - prev_entry->end <= next_entry->start - addr)
2853 stack_entry = prev_entry;
2855 stack_entry = next_entry;
2857 stack_entry = prev_entry;
2860 if (stack_entry == next_entry) {
2861 KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_DOWN, ("foo"));
2862 KASSERT(addr < stack_entry->start, ("foo"));
2863 end = (prev_entry != &map->header) ? prev_entry->end :
2864 stack_entry->start - stack_entry->avail_ssize;
2865 grow_amount = roundup(stack_entry->start - addr, PAGE_SIZE);
2866 max_grow = stack_entry->start - end;
2868 KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_UP, ("foo"));
2869 KASSERT(addr >= stack_entry->end, ("foo"));
2870 end = (next_entry != &map->header) ? next_entry->start :
2871 stack_entry->end + stack_entry->avail_ssize;
2872 grow_amount = roundup(addr + 1 - stack_entry->end, PAGE_SIZE);
2873 max_grow = end - stack_entry->end;
2876 if (grow_amount > stack_entry->avail_ssize) {
2877 vm_map_unlock_read(map);
2878 return (KERN_NO_SPACE);
2882 * If there is no longer enough space between the entries nogo, and
2883 * adjust the available space. Note: this should only happen if the
2884 * user has mapped into the stack area after the stack was created,
2885 * and is probably an error.
2887 * This also effectively destroys any guard page the user might have
2888 * intended by limiting the stack size.
2890 if (grow_amount > max_grow) {
2891 if (vm_map_lock_upgrade(map))
2894 stack_entry->avail_ssize = max_grow;
2897 return (KERN_NO_SPACE);
2900 is_procstack = (addr >= (vm_offset_t)vm->vm_maxsaddr) ? 1 : 0;
2903 * If this is the main process stack, see if we're over the stack
2906 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
2907 vm_map_unlock_read(map);
2908 return (KERN_NO_SPACE);
2911 /* Round up the grow amount modulo SGROWSIZ */
2912 grow_amount = roundup (grow_amount, sgrowsiz);
2913 if (grow_amount > stack_entry->avail_ssize)
2914 grow_amount = stack_entry->avail_ssize;
2915 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
2916 grow_amount = stacklim - ctob(vm->vm_ssize);
2919 /* If we would blow our VMEM resource limit, no go */
2920 if (map->size + grow_amount > vmemlim) {
2921 vm_map_unlock_read(map);
2922 return (KERN_NO_SPACE);
2925 if (vm_map_lock_upgrade(map))
2928 if (stack_entry == next_entry) {
2932 /* Get the preliminary new entry start value */
2933 addr = stack_entry->start - grow_amount;
2936 * If this puts us into the previous entry, cut back our
2937 * growth to the available space. Also, see the note above.
2940 stack_entry->avail_ssize = max_grow;
2944 rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start,
2945 p->p_sysent->sv_stackprot, VM_PROT_ALL, 0);
2947 /* Adjust the available stack space by the amount we grew. */
2948 if (rv == KERN_SUCCESS) {
2949 if (prev_entry != &map->header)
2950 vm_map_clip_end(map, prev_entry, addr);
2951 new_entry = prev_entry->next;
2952 KASSERT(new_entry == stack_entry->prev, ("foo"));
2953 KASSERT(new_entry->end == stack_entry->start, ("foo"));
2954 KASSERT(new_entry->start == addr, ("foo"));
2955 grow_amount = new_entry->end - new_entry->start;
2956 new_entry->avail_ssize = stack_entry->avail_ssize -
2958 stack_entry->eflags &= ~MAP_ENTRY_GROWS_DOWN;
2959 new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
2965 addr = stack_entry->end + grow_amount;
2968 * If this puts us into the next entry, cut back our growth
2969 * to the available space. Also, see the note above.
2972 stack_entry->avail_ssize = end - stack_entry->end;
2976 grow_amount = addr - stack_entry->end;
2978 /* Grow the underlying object if applicable. */
2979 if (stack_entry->object.vm_object == NULL ||
2980 vm_object_coalesce(stack_entry->object.vm_object,
2981 stack_entry->offset,
2982 (vm_size_t)(stack_entry->end - stack_entry->start),
2983 (vm_size_t)grow_amount)) {
2984 map->size += (addr - stack_entry->end);
2985 /* Update the current entry. */
2986 stack_entry->end = addr;
2987 stack_entry->avail_ssize -= grow_amount;
2988 vm_map_entry_resize_free(map, stack_entry);
2991 if (next_entry != &map->header)
2992 vm_map_clip_start(map, next_entry, addr);
2997 if (rv == KERN_SUCCESS && is_procstack)
2998 vm->vm_ssize += btoc(grow_amount);
3003 * Heed the MAP_WIREFUTURE flag if it was set for this process.
3005 if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE)) {
3007 (stack_entry == next_entry) ? addr : addr - grow_amount,
3008 (stack_entry == next_entry) ? stack_entry->start : addr,
3009 (p->p_flag & P_SYSTEM)
3010 ? VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES
3011 : VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
3018 * Unshare the specified VM space for exec. If other processes are
3019 * mapped to it, then create a new one. The new vmspace is null.
3022 vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser)
3024 struct vmspace *oldvmspace = p->p_vmspace;
3025 struct vmspace *newvmspace;
3027 newvmspace = vmspace_alloc(minuser, maxuser);
3028 newvmspace->vm_swrss = oldvmspace->vm_swrss;
3030 * This code is written like this for prototype purposes. The
3031 * goal is to avoid running down the vmspace here, but let the
3032 * other process's that are still using the vmspace to finally
3033 * run it down. Even though there is little or no chance of blocking
3034 * here, it is a good idea to keep this form for future mods.
3036 PROC_VMSPACE_LOCK(p);
3037 p->p_vmspace = newvmspace;
3038 PROC_VMSPACE_UNLOCK(p);
3039 if (p == curthread->td_proc) /* XXXKSE ? */
3040 pmap_activate(curthread);
3041 vmspace_free(oldvmspace);
3045 * Unshare the specified VM space for forcing COW. This
3046 * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
3049 vmspace_unshare(struct proc *p)
3051 struct vmspace *oldvmspace = p->p_vmspace;
3052 struct vmspace *newvmspace;
3054 if (oldvmspace->vm_refcnt == 1)
3056 newvmspace = vmspace_fork(oldvmspace);
3057 PROC_VMSPACE_LOCK(p);
3058 p->p_vmspace = newvmspace;
3059 PROC_VMSPACE_UNLOCK(p);
3060 if (p == curthread->td_proc) /* XXXKSE ? */
3061 pmap_activate(curthread);
3062 vmspace_free(oldvmspace);
3068 * Finds the VM object, offset, and
3069 * protection for a given virtual address in the
3070 * specified map, assuming a page fault of the
3073 * Leaves the map in question locked for read; return
3074 * values are guaranteed until a vm_map_lookup_done
3075 * call is performed. Note that the map argument
3076 * is in/out; the returned map must be used in
3077 * the call to vm_map_lookup_done.
3079 * A handle (out_entry) is returned for use in
3080 * vm_map_lookup_done, to make that fast.
3082 * If a lookup is requested with "write protection"
3083 * specified, the map may be changed to perform virtual
3084 * copying operations, although the data referenced will
3088 vm_map_lookup(vm_map_t *var_map, /* IN/OUT */
3090 vm_prot_t fault_typea,
3091 vm_map_entry_t *out_entry, /* OUT */
3092 vm_object_t *object, /* OUT */
3093 vm_pindex_t *pindex, /* OUT */
3094 vm_prot_t *out_prot, /* OUT */
3095 boolean_t *wired) /* OUT */
3097 vm_map_entry_t entry;
3098 vm_map_t map = *var_map;
3100 vm_prot_t fault_type = fault_typea;
3104 * Lookup the faulting address.
3107 vm_map_lock_read(map);
3108 #define RETURN(why) \
3110 vm_map_unlock_read(map); \
3115 * If the map has an interesting hint, try it before calling full
3116 * blown lookup routine.
3120 if (entry == NULL ||
3121 (vaddr < entry->start) || (vaddr >= entry->end)) {
3123 * Entry was either not a valid hint, or the vaddr was not
3124 * contained in the entry, so do a full lookup.
3126 if (!vm_map_lookup_entry(map, vaddr, out_entry))
3127 RETURN(KERN_INVALID_ADDRESS);
3135 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3136 vm_map_t old_map = map;
3138 *var_map = map = entry->object.sub_map;
3139 vm_map_unlock_read(old_map);
3144 * Check whether this task is allowed to have this page.
3145 * Note the special case for MAP_ENTRY_COW
3146 * pages with an override. This is to implement a forced
3147 * COW for debuggers.
3149 if (fault_type & VM_PROT_OVERRIDE_WRITE)
3150 prot = entry->max_protection;
3152 prot = entry->protection;
3153 fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
3154 if ((fault_type & prot) != fault_type) {
3155 RETURN(KERN_PROTECTION_FAILURE);
3157 if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
3158 (entry->eflags & MAP_ENTRY_COW) &&
3159 (fault_type & VM_PROT_WRITE) &&
3160 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
3161 RETURN(KERN_PROTECTION_FAILURE);
3165 * If this page is not pageable, we have to get it for all possible
3168 *wired = (entry->wired_count != 0);
3170 prot = fault_type = entry->protection;
3173 * If the entry was copy-on-write, we either ...
3175 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3177 * If we want to write the page, we may as well handle that
3178 * now since we've got the map locked.
3180 * If we don't need to write the page, we just demote the
3181 * permissions allowed.
3183 if (fault_type & VM_PROT_WRITE) {
3185 * Make a new object, and place it in the object
3186 * chain. Note that no new references have appeared
3187 * -- one just moved from the map to the new
3190 if (vm_map_lock_upgrade(map))
3194 &entry->object.vm_object,
3196 atop(entry->end - entry->start));
3197 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
3199 vm_map_lock_downgrade(map);
3202 * We're attempting to read a copy-on-write page --
3203 * don't allow writes.
3205 prot &= ~VM_PROT_WRITE;
3210 * Create an object if necessary.
3212 if (entry->object.vm_object == NULL &&
3214 if (vm_map_lock_upgrade(map))
3216 entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
3217 atop(entry->end - entry->start));
3219 vm_map_lock_downgrade(map);
3223 * Return the object/offset from this entry. If the entry was
3224 * copy-on-write or empty, it has been fixed up.
3226 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
3227 *object = entry->object.vm_object;
3230 return (KERN_SUCCESS);
3236 * vm_map_lookup_locked:
3238 * Lookup the faulting address. A version of vm_map_lookup that returns
3239 * KERN_FAILURE instead of blocking on map lock or memory allocation.
3242 vm_map_lookup_locked(vm_map_t *var_map, /* IN/OUT */
3244 vm_prot_t fault_typea,
3245 vm_map_entry_t *out_entry, /* OUT */
3246 vm_object_t *object, /* OUT */
3247 vm_pindex_t *pindex, /* OUT */
3248 vm_prot_t *out_prot, /* OUT */
3249 boolean_t *wired) /* OUT */
3251 vm_map_entry_t entry;
3252 vm_map_t map = *var_map;
3254 vm_prot_t fault_type = fault_typea;
3257 * If the map has an interesting hint, try it before calling full
3258 * blown lookup routine.
3262 if (entry == NULL ||
3263 (vaddr < entry->start) || (vaddr >= entry->end)) {
3265 * Entry was either not a valid hint, or the vaddr was not
3266 * contained in the entry, so do a full lookup.
3268 if (!vm_map_lookup_entry(map, vaddr, out_entry))
3269 return (KERN_INVALID_ADDRESS);
3275 * Fail if the entry refers to a submap.
3277 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
3278 return (KERN_FAILURE);
3281 * Check whether this task is allowed to have this page.
3282 * Note the special case for MAP_ENTRY_COW
3283 * pages with an override. This is to implement a forced
3284 * COW for debuggers.
3286 if (fault_type & VM_PROT_OVERRIDE_WRITE)
3287 prot = entry->max_protection;
3289 prot = entry->protection;
3290 fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
3291 if ((fault_type & prot) != fault_type)
3292 return (KERN_PROTECTION_FAILURE);
3293 if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
3294 (entry->eflags & MAP_ENTRY_COW) &&
3295 (fault_type & VM_PROT_WRITE) &&
3296 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0)
3297 return (KERN_PROTECTION_FAILURE);
3300 * If this page is not pageable, we have to get it for all possible
3303 *wired = (entry->wired_count != 0);
3305 prot = fault_type = entry->protection;
3307 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3309 * Fail if the entry was copy-on-write for a write fault.
3311 if (fault_type & VM_PROT_WRITE)
3312 return (KERN_FAILURE);
3314 * We're attempting to read a copy-on-write page --
3315 * don't allow writes.
3317 prot &= ~VM_PROT_WRITE;
3321 * Fail if an object should be created.
3323 if (entry->object.vm_object == NULL && !map->system_map)
3324 return (KERN_FAILURE);
3327 * Return the object/offset from this entry. If the entry was
3328 * copy-on-write or empty, it has been fixed up.
3330 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
3331 *object = entry->object.vm_object;
3334 return (KERN_SUCCESS);
3338 * vm_map_lookup_done:
3340 * Releases locks acquired by a vm_map_lookup
3341 * (according to the handle returned by that lookup).
3344 vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry)
3347 * Unlock the main-level map
3349 vm_map_unlock_read(map);
3352 #include "opt_ddb.h"
3354 #include <sys/kernel.h>
3356 #include <ddb/ddb.h>
3359 * vm_map_print: [ debug ]
3361 DB_SHOW_COMMAND(map, vm_map_print)
3364 /* XXX convert args. */
3365 vm_map_t map = (vm_map_t)addr;
3366 boolean_t full = have_addr;
3368 vm_map_entry_t entry;
3370 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
3372 (void *)map->pmap, map->nentries, map->timestamp);
3375 if (!full && db_indent)
3379 for (entry = map->header.next; entry != &map->header;
3380 entry = entry->next) {
3381 db_iprintf("map entry %p: start=%p, end=%p\n",
3382 (void *)entry, (void *)entry->start, (void *)entry->end);
3385 static char *inheritance_name[4] =
3386 {"share", "copy", "none", "donate_copy"};
3388 db_iprintf(" prot=%x/%x/%s",
3390 entry->max_protection,
3391 inheritance_name[(int)(unsigned char)entry->inheritance]);
3392 if (entry->wired_count != 0)
3393 db_printf(", wired");
3395 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3396 db_printf(", share=%p, offset=0x%jx\n",
3397 (void *)entry->object.sub_map,
3398 (uintmax_t)entry->offset);
3400 if ((entry->prev == &map->header) ||
3401 (entry->prev->object.sub_map !=
3402 entry->object.sub_map)) {
3404 vm_map_print((db_expr_t)(intptr_t)
3405 entry->object.sub_map,
3406 full, 0, (char *)0);
3410 db_printf(", object=%p, offset=0x%jx",
3411 (void *)entry->object.vm_object,
3412 (uintmax_t)entry->offset);
3413 if (entry->eflags & MAP_ENTRY_COW)
3414 db_printf(", copy (%s)",
3415 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
3419 if ((entry->prev == &map->header) ||
3420 (entry->prev->object.vm_object !=
3421 entry->object.vm_object)) {
3423 vm_object_print((db_expr_t)(intptr_t)
3424 entry->object.vm_object,
3425 full, 0, (char *)0);
3437 DB_SHOW_COMMAND(procvm, procvm)
3442 p = (struct proc *) addr;
3447 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
3448 (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
3449 (void *)vmspace_pmap(p->p_vmspace));
3451 vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);