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 vm_map_zfini(&vm->vm_map, sizeof(vm->vm_map));
204 vmspace_zinit(void *mem, int size, int flags)
208 vm = (struct vmspace *)mem;
210 vm->vm_map.pmap = NULL;
211 (void)vm_map_zinit(&vm->vm_map, sizeof(vm->vm_map), flags);
216 vm_map_zfini(void *mem, int size)
221 mtx_destroy(&map->system_mtx);
222 sx_destroy(&map->lock);
226 vm_map_zinit(void *mem, int size, int flags)
233 mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
234 sx_init(&map->lock, "user map");
240 vmspace_zdtor(void *mem, int size, void *arg)
244 vm = (struct vmspace *)mem;
246 vm_map_zdtor(&vm->vm_map, sizeof(vm->vm_map), arg);
249 vm_map_zdtor(void *mem, int size, void *arg)
254 KASSERT(map->nentries == 0,
255 ("map %p nentries == %d on free.",
256 map, map->nentries));
257 KASSERT(map->size == 0,
258 ("map %p size == %lu on free.",
259 map, (unsigned long)map->size));
261 #endif /* INVARIANTS */
264 * Allocate a vmspace structure, including a vm_map and pmap,
265 * and initialize those structures. The refcnt is set to 1.
268 vmspace_alloc(min, max)
269 vm_offset_t min, max;
273 vm = uma_zalloc(vmspace_zone, M_WAITOK);
274 if (vm->vm_map.pmap == NULL && !pmap_pinit(vmspace_pmap(vm))) {
275 uma_zfree(vmspace_zone, vm);
278 CTR1(KTR_VM, "vmspace_alloc: %p", vm);
279 _vm_map_init(&vm->vm_map, min, max);
280 vm->vm_map.pmap = vmspace_pmap(vm); /* XXX */
296 uma_zone_set_obj(kmapentzone, &kmapentobj, lmin(cnt.v_page_count,
297 (VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS) / PAGE_SIZE) / 8 +
298 maxproc * 2 + maxfiles);
299 vmspace_zone = uma_zcreate("VMSPACE", sizeof(struct vmspace), NULL,
305 vmspace_zinit, vmspace_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
309 vmspace_dofree(struct vmspace *vm)
311 CTR1(KTR_VM, "vmspace_free: %p", vm);
314 * Make sure any SysV shm is freed, it might not have been in
320 * Lock the map, to wait out all other references to it.
321 * Delete all of the mappings and pages they hold, then call
322 * the pmap module to reclaim anything left.
324 (void)vm_map_remove(&vm->vm_map, vm->vm_map.min_offset,
325 vm->vm_map.max_offset);
328 * XXX Comment out the pmap_release call for now. The
329 * vmspace_zone is marked as UMA_ZONE_NOFREE, and bugs cause
330 * pmap.resident_count to be != 0 on exit sometimes.
332 /* pmap_release(vmspace_pmap(vm)); */
333 uma_zfree(vmspace_zone, vm);
337 vmspace_free(struct vmspace *vm)
341 if (vm->vm_refcnt == 0)
342 panic("vmspace_free: attempt to free already freed vmspace");
345 refcnt = vm->vm_refcnt;
346 while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1));
352 vmspace_exitfree(struct proc *p)
356 PROC_VMSPACE_LOCK(p);
359 PROC_VMSPACE_UNLOCK(p);
360 KASSERT(vm == &vmspace0, ("vmspace_exitfree: wrong vmspace"));
365 vmspace_exit(struct thread *td)
372 * Release user portion of address space.
373 * This releases references to vnodes,
374 * which could cause I/O if the file has been unlinked.
375 * Need to do this early enough that we can still sleep.
377 * The last exiting process to reach this point releases as
378 * much of the environment as it can. vmspace_dofree() is the
379 * slower fallback in case another process had a temporary
380 * reference to the vmspace.
385 atomic_add_int(&vmspace0.vm_refcnt, 1);
387 refcnt = vm->vm_refcnt;
388 if (refcnt > 1 && p->p_vmspace != &vmspace0) {
389 /* Switch now since other proc might free vmspace */
390 PROC_VMSPACE_LOCK(p);
391 p->p_vmspace = &vmspace0;
392 PROC_VMSPACE_UNLOCK(p);
395 } while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1));
397 if (p->p_vmspace != vm) {
398 /* vmspace not yet freed, switch back */
399 PROC_VMSPACE_LOCK(p);
401 PROC_VMSPACE_UNLOCK(p);
404 pmap_remove_pages(vmspace_pmap(vm));
405 /* Switch now since this proc will free vmspace */
406 PROC_VMSPACE_LOCK(p);
407 p->p_vmspace = &vmspace0;
408 PROC_VMSPACE_UNLOCK(p);
414 /* Acquire reference to vmspace owned by another process. */
417 vmspace_acquire_ref(struct proc *p)
422 PROC_VMSPACE_LOCK(p);
425 PROC_VMSPACE_UNLOCK(p);
429 refcnt = vm->vm_refcnt;
430 if (refcnt <= 0) { /* Avoid 0->1 transition */
431 PROC_VMSPACE_UNLOCK(p);
434 } while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt + 1));
435 if (vm != p->p_vmspace) {
436 PROC_VMSPACE_UNLOCK(p);
440 PROC_VMSPACE_UNLOCK(p);
445 _vm_map_lock(vm_map_t map, const char *file, int line)
449 _mtx_lock_flags(&map->system_mtx, 0, file, line);
451 (void)_sx_xlock(&map->lock, 0, file, line);
456 _vm_map_unlock(vm_map_t map, const char *file, int line)
460 _mtx_unlock_flags(&map->system_mtx, 0, file, line);
462 _sx_xunlock(&map->lock, file, line);
466 _vm_map_lock_read(vm_map_t map, const char *file, int line)
470 _mtx_lock_flags(&map->system_mtx, 0, file, line);
472 (void)_sx_xlock(&map->lock, 0, file, line);
476 _vm_map_unlock_read(vm_map_t map, const char *file, int line)
480 _mtx_unlock_flags(&map->system_mtx, 0, file, line);
482 _sx_xunlock(&map->lock, file, line);
486 _vm_map_trylock(vm_map_t map, const char *file, int line)
490 error = map->system_map ?
491 !_mtx_trylock(&map->system_mtx, 0, file, line) :
492 !_sx_try_xlock(&map->lock, file, line);
499 _vm_map_trylock_read(vm_map_t map, const char *file, int line)
503 error = map->system_map ?
504 !_mtx_trylock(&map->system_mtx, 0, file, line) :
505 !_sx_try_xlock(&map->lock, file, line);
510 _vm_map_lock_upgrade(vm_map_t map, const char *file, int line)
514 if (map->system_map) {
515 _mtx_assert(&map->system_mtx, MA_OWNED, file, line);
517 _sx_assert(&map->lock, SX_XLOCKED, file, line);
524 _vm_map_lock_downgrade(vm_map_t map, const char *file, int line)
528 if (map->system_map) {
529 _mtx_assert(&map->system_mtx, MA_OWNED, file, line);
531 _sx_assert(&map->lock, SX_XLOCKED, file, line);
536 * vm_map_unlock_and_wait:
539 vm_map_unlock_and_wait(vm_map_t map, boolean_t user_wait)
542 mtx_lock(&map_sleep_mtx);
544 return (msleep(&map->root, &map_sleep_mtx, PDROP | PVM, "vmmaps", 0));
551 vm_map_wakeup(vm_map_t map)
555 * Acquire and release map_sleep_mtx to prevent a wakeup()
556 * from being performed (and lost) between the vm_map_unlock()
557 * and the msleep() in vm_map_unlock_and_wait().
559 mtx_lock(&map_sleep_mtx);
560 mtx_unlock(&map_sleep_mtx);
565 vmspace_resident_count(struct vmspace *vmspace)
567 return pmap_resident_count(vmspace_pmap(vmspace));
571 vmspace_wired_count(struct vmspace *vmspace)
573 return pmap_wired_count(vmspace_pmap(vmspace));
579 * Creates and returns a new empty VM map with
580 * the given physical map structure, and having
581 * the given lower and upper address bounds.
584 vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max)
588 result = uma_zalloc(mapzone, M_WAITOK);
589 CTR1(KTR_VM, "vm_map_create: %p", result);
590 _vm_map_init(result, min, max);
596 * Initialize an existing vm_map structure
597 * such as that in the vmspace structure.
598 * The pmap is set elsewhere.
601 _vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max)
604 map->header.next = map->header.prev = &map->header;
605 map->needs_wakeup = FALSE;
607 map->min_offset = min;
608 map->max_offset = max;
615 vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max)
617 _vm_map_init(map, min, max);
618 mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
619 sx_init(&map->lock, "user map");
623 * vm_map_entry_dispose: [ internal use only ]
625 * Inverse of vm_map_entry_create.
628 vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry)
630 uma_zfree(map->system_map ? kmapentzone : mapentzone, entry);
634 * vm_map_entry_create: [ internal use only ]
636 * Allocates a VM map entry for insertion.
637 * No entry fields are filled in.
639 static vm_map_entry_t
640 vm_map_entry_create(vm_map_t map)
642 vm_map_entry_t new_entry;
645 new_entry = uma_zalloc(kmapentzone, M_NOWAIT);
647 new_entry = uma_zalloc(mapentzone, M_WAITOK);
648 if (new_entry == NULL)
649 panic("vm_map_entry_create: kernel resources exhausted");
654 * vm_map_entry_set_behavior:
656 * Set the expected access behavior, either normal, random, or
660 vm_map_entry_set_behavior(vm_map_entry_t entry, u_char behavior)
662 entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) |
663 (behavior & MAP_ENTRY_BEHAV_MASK);
667 * vm_map_entry_set_max_free:
669 * Set the max_free field in a vm_map_entry.
672 vm_map_entry_set_max_free(vm_map_entry_t entry)
675 entry->max_free = entry->adj_free;
676 if (entry->left != NULL && entry->left->max_free > entry->max_free)
677 entry->max_free = entry->left->max_free;
678 if (entry->right != NULL && entry->right->max_free > entry->max_free)
679 entry->max_free = entry->right->max_free;
683 * vm_map_entry_splay:
685 * The Sleator and Tarjan top-down splay algorithm with the
686 * following variation. Max_free must be computed bottom-up, so
687 * on the downward pass, maintain the left and right spines in
688 * reverse order. Then, make a second pass up each side to fix
689 * the pointers and compute max_free. The time bound is O(log n)
692 * The new root is the vm_map_entry containing "addr", or else an
693 * adjacent entry (lower or higher) if addr is not in the tree.
695 * The map must be locked, and leaves it so.
697 * Returns: the new root.
699 static vm_map_entry_t
700 vm_map_entry_splay(vm_offset_t addr, vm_map_entry_t root)
702 vm_map_entry_t llist, rlist;
703 vm_map_entry_t ltree, rtree;
706 /* Special case of empty tree. */
711 * Pass One: Splay down the tree until we find addr or a NULL
712 * pointer where addr would go. llist and rlist are the two
713 * sides in reverse order (bottom-up), with llist linked by
714 * the right pointer and rlist linked by the left pointer in
715 * the vm_map_entry. Wait until Pass Two to set max_free on
721 /* root is never NULL in here. */
722 if (addr < root->start) {
726 if (addr < y->start && y->left != NULL) {
727 /* Rotate right and put y on rlist. */
728 root->left = y->right;
730 vm_map_entry_set_max_free(root);
735 /* Put root on rlist. */
742 if (addr < root->end || y == NULL)
744 if (addr >= y->end && y->right != NULL) {
745 /* Rotate left and put y on llist. */
746 root->right = y->left;
748 vm_map_entry_set_max_free(root);
753 /* Put root on llist. */
762 * Pass Two: Walk back up the two spines, flip the pointers
763 * and set max_free. The subtrees of the root go at the
764 * bottom of llist and rlist.
767 while (llist != NULL) {
769 llist->right = ltree;
770 vm_map_entry_set_max_free(llist);
775 while (rlist != NULL) {
778 vm_map_entry_set_max_free(rlist);
784 * Final assembly: add ltree and rtree as subtrees of root.
788 vm_map_entry_set_max_free(root);
794 * vm_map_entry_{un,}link:
796 * Insert/remove entries from maps.
799 vm_map_entry_link(vm_map_t map,
800 vm_map_entry_t after_where,
801 vm_map_entry_t entry)
805 "vm_map_entry_link: map %p, nentries %d, entry %p, after %p", map,
806 map->nentries, entry, after_where);
808 entry->prev = after_where;
809 entry->next = after_where->next;
810 entry->next->prev = entry;
811 after_where->next = entry;
813 if (after_where != &map->header) {
814 if (after_where != map->root)
815 vm_map_entry_splay(after_where->start, map->root);
816 entry->right = after_where->right;
817 entry->left = after_where;
818 after_where->right = NULL;
819 after_where->adj_free = entry->start - after_where->end;
820 vm_map_entry_set_max_free(after_where);
822 entry->right = map->root;
825 entry->adj_free = (entry->next == &map->header ? map->max_offset :
826 entry->next->start) - entry->end;
827 vm_map_entry_set_max_free(entry);
832 vm_map_entry_unlink(vm_map_t map,
833 vm_map_entry_t entry)
835 vm_map_entry_t next, prev, root;
837 if (entry != map->root)
838 vm_map_entry_splay(entry->start, map->root);
839 if (entry->left == NULL)
842 root = vm_map_entry_splay(entry->start, entry->left);
843 root->right = entry->right;
844 root->adj_free = (entry->next == &map->header ? map->max_offset :
845 entry->next->start) - root->end;
846 vm_map_entry_set_max_free(root);
855 CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map,
856 map->nentries, entry);
860 * vm_map_entry_resize_free:
862 * Recompute the amount of free space following a vm_map_entry
863 * and propagate that value up the tree. Call this function after
864 * resizing a map entry in-place, that is, without a call to
865 * vm_map_entry_link() or _unlink().
867 * The map must be locked, and leaves it so.
870 vm_map_entry_resize_free(vm_map_t map, vm_map_entry_t entry)
874 * Using splay trees without parent pointers, propagating
875 * max_free up the tree is done by moving the entry to the
876 * root and making the change there.
878 if (entry != map->root)
879 map->root = vm_map_entry_splay(entry->start, map->root);
881 entry->adj_free = (entry->next == &map->header ? map->max_offset :
882 entry->next->start) - entry->end;
883 vm_map_entry_set_max_free(entry);
887 * vm_map_lookup_entry: [ internal use only ]
889 * Finds the map entry containing (or
890 * immediately preceding) the specified address
891 * in the given map; the entry is returned
892 * in the "entry" parameter. The boolean
893 * result indicates whether the address is
894 * actually contained in the map.
900 vm_map_entry_t *entry) /* OUT */
904 cur = vm_map_entry_splay(address, map->root);
906 *entry = &map->header;
910 if (address >= cur->start) {
912 if (cur->end > address)
923 * Inserts the given whole VM object into the target
924 * map at the specified address range. The object's
925 * size should match that of the address range.
927 * Requires that the map be locked, and leaves it so.
929 * If object is non-NULL, ref count must be bumped by caller
930 * prior to making call to account for the new entry.
933 vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
934 vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max,
937 vm_map_entry_t new_entry;
938 vm_map_entry_t prev_entry;
939 vm_map_entry_t temp_entry;
940 vm_eflags_t protoeflags;
943 * Check that the start and end points are not bogus.
945 if ((start < map->min_offset) || (end > map->max_offset) ||
947 return (KERN_INVALID_ADDRESS);
950 * Find the entry prior to the proposed starting address; if it's part
951 * of an existing entry, this range is bogus.
953 if (vm_map_lookup_entry(map, start, &temp_entry))
954 return (KERN_NO_SPACE);
956 prev_entry = temp_entry;
959 * Assert that the next entry doesn't overlap the end point.
961 if ((prev_entry->next != &map->header) &&
962 (prev_entry->next->start < end))
963 return (KERN_NO_SPACE);
967 if (cow & MAP_COPY_ON_WRITE)
968 protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
970 if (cow & MAP_NOFAULT) {
971 protoeflags |= MAP_ENTRY_NOFAULT;
973 KASSERT(object == NULL,
974 ("vm_map_insert: paradoxical MAP_NOFAULT request"));
976 if (cow & MAP_DISABLE_SYNCER)
977 protoeflags |= MAP_ENTRY_NOSYNC;
978 if (cow & MAP_DISABLE_COREDUMP)
979 protoeflags |= MAP_ENTRY_NOCOREDUMP;
981 if (object != NULL) {
983 * OBJ_ONEMAPPING must be cleared unless this mapping
984 * is trivially proven to be the only mapping for any
985 * of the object's pages. (Object granularity
986 * reference counting is insufficient to recognize
987 * aliases with precision.)
989 VM_OBJECT_LOCK(object);
990 if (object->ref_count > 1 || object->shadow_count != 0)
991 vm_object_clear_flag(object, OBJ_ONEMAPPING);
992 VM_OBJECT_UNLOCK(object);
994 else if ((prev_entry != &map->header) &&
995 (prev_entry->eflags == protoeflags) &&
996 (prev_entry->end == start) &&
997 (prev_entry->wired_count == 0) &&
998 ((prev_entry->object.vm_object == NULL) ||
999 vm_object_coalesce(prev_entry->object.vm_object,
1001 (vm_size_t)(prev_entry->end - prev_entry->start),
1002 (vm_size_t)(end - prev_entry->end)))) {
1004 * We were able to extend the object. Determine if we
1005 * can extend the previous map entry to include the
1006 * new range as well.
1008 if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
1009 (prev_entry->protection == prot) &&
1010 (prev_entry->max_protection == max)) {
1011 map->size += (end - prev_entry->end);
1012 prev_entry->end = end;
1013 vm_map_entry_resize_free(map, prev_entry);
1014 vm_map_simplify_entry(map, prev_entry);
1015 return (KERN_SUCCESS);
1019 * If we can extend the object but cannot extend the
1020 * map entry, we have to create a new map entry. We
1021 * must bump the ref count on the extended object to
1022 * account for it. object may be NULL.
1024 object = prev_entry->object.vm_object;
1025 offset = prev_entry->offset +
1026 (prev_entry->end - prev_entry->start);
1027 vm_object_reference(object);
1031 * NOTE: if conditionals fail, object can be NULL here. This occurs
1032 * in things like the buffer map where we manage kva but do not manage
1037 * Create a new entry
1039 new_entry = vm_map_entry_create(map);
1040 new_entry->start = start;
1041 new_entry->end = end;
1043 new_entry->eflags = protoeflags;
1044 new_entry->object.vm_object = object;
1045 new_entry->offset = offset;
1046 new_entry->avail_ssize = 0;
1048 new_entry->inheritance = VM_INHERIT_DEFAULT;
1049 new_entry->protection = prot;
1050 new_entry->max_protection = max;
1051 new_entry->wired_count = 0;
1054 * Insert the new entry into the list
1056 vm_map_entry_link(map, prev_entry, new_entry);
1057 map->size += new_entry->end - new_entry->start;
1061 * Temporarily removed to avoid MAP_STACK panic, due to
1062 * MAP_STACK being a huge hack. Will be added back in
1063 * when MAP_STACK (and the user stack mapping) is fixed.
1066 * It may be possible to simplify the entry
1068 vm_map_simplify_entry(map, new_entry);
1071 if (cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) {
1072 vm_map_pmap_enter(map, start, prot,
1073 object, OFF_TO_IDX(offset), end - start,
1074 cow & MAP_PREFAULT_PARTIAL);
1077 return (KERN_SUCCESS);
1083 * Find the first fit (lowest VM address) for "length" free bytes
1084 * beginning at address >= start in the given map.
1086 * In a vm_map_entry, "adj_free" is the amount of free space
1087 * adjacent (higher address) to this entry, and "max_free" is the
1088 * maximum amount of contiguous free space in its subtree. This
1089 * allows finding a free region in one path down the tree, so
1090 * O(log n) amortized with splay trees.
1092 * The map must be locked, and leaves it so.
1094 * Returns: 0 on success, and starting address in *addr,
1095 * 1 if insufficient space.
1098 vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length,
1099 vm_offset_t *addr) /* OUT */
1101 vm_map_entry_t entry;
1102 vm_offset_t end, st;
1105 * Request must fit within min/max VM address and must avoid
1108 if (start < map->min_offset)
1109 start = map->min_offset;
1110 if (start + length > map->max_offset || start + length < start)
1113 /* Empty tree means wide open address space. */
1114 if (map->root == NULL) {
1120 * After splay, if start comes before root node, then there
1121 * must be a gap from start to the root.
1123 map->root = vm_map_entry_splay(start, map->root);
1124 if (start + length <= map->root->start) {
1130 * Root is the last node that might begin its gap before
1131 * start, and this is the last comparison where address
1132 * wrap might be a problem.
1134 st = (start > map->root->end) ? start : map->root->end;
1135 if (length <= map->root->end + map->root->adj_free - st) {
1140 /* With max_free, can immediately tell if no solution. */
1141 entry = map->root->right;
1142 if (entry == NULL || length > entry->max_free)
1146 * Search the right subtree in the order: left subtree, root,
1147 * right subtree (first fit). The previous splay implies that
1148 * all regions in the right subtree have addresses > start.
1150 while (entry != NULL) {
1151 if (entry->left != NULL && entry->left->max_free >= length)
1152 entry = entry->left;
1153 else if (entry->adj_free >= length) {
1157 entry = entry->right;
1160 /* Can't get here, so panic if we do. */
1161 panic("vm_map_findspace: max_free corrupt");
1164 /* Expand the kernel pmap, if necessary. */
1165 if (map == kernel_map) {
1166 end = round_page(*addr + length);
1167 if (end > kernel_vm_end)
1168 pmap_growkernel(end);
1174 vm_map_fixed(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1175 vm_offset_t start, vm_size_t length, vm_prot_t prot,
1176 vm_prot_t max, int cow)
1182 end = start + length;
1183 VM_MAP_RANGE_CHECK(map, start, end);
1184 (void) vm_map_delete(map, start, end);
1185 result = vm_map_insert(map, object, offset, start, end, prot,
1192 * vm_map_find finds an unallocated region in the target address
1193 * map with the given length. The search is defined to be
1194 * first-fit from the specified address; the region found is
1195 * returned in the same parameter.
1197 * If object is non-NULL, ref count must be bumped by caller
1198 * prior to making call to account for the new entry.
1201 vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1202 vm_offset_t *addr, /* IN/OUT */
1203 vm_size_t length, int find_space, vm_prot_t prot,
1204 vm_prot_t max, int cow)
1212 if (find_space != VMFS_NO_SPACE) {
1213 if (vm_map_findspace(map, start, length, addr)) {
1215 return (KERN_NO_SPACE);
1217 if (find_space == VMFS_ALIGNED_SPACE)
1218 pmap_align_superpage(object, offset, addr,
1222 result = vm_map_insert(map, object, offset, start, start +
1223 length, prot, max, cow);
1224 } while (result == KERN_NO_SPACE && find_space == VMFS_ALIGNED_SPACE);
1230 * vm_map_simplify_entry:
1232 * Simplify the given map entry by merging with either neighbor. This
1233 * routine also has the ability to merge with both neighbors.
1235 * The map must be locked.
1237 * This routine guarentees that the passed entry remains valid (though
1238 * possibly extended). When merging, this routine may delete one or
1242 vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry)
1244 vm_map_entry_t next, prev;
1245 vm_size_t prevsize, esize;
1247 if (entry->eflags & (MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP))
1251 if (prev != &map->header) {
1252 prevsize = prev->end - prev->start;
1253 if ( (prev->end == entry->start) &&
1254 (prev->object.vm_object == entry->object.vm_object) &&
1255 (!prev->object.vm_object ||
1256 (prev->offset + prevsize == entry->offset)) &&
1257 (prev->eflags == entry->eflags) &&
1258 (prev->protection == entry->protection) &&
1259 (prev->max_protection == entry->max_protection) &&
1260 (prev->inheritance == entry->inheritance) &&
1261 (prev->wired_count == entry->wired_count)) {
1262 vm_map_entry_unlink(map, prev);
1263 entry->start = prev->start;
1264 entry->offset = prev->offset;
1265 if (entry->prev != &map->header)
1266 vm_map_entry_resize_free(map, entry->prev);
1267 if (prev->object.vm_object)
1268 vm_object_deallocate(prev->object.vm_object);
1269 vm_map_entry_dispose(map, prev);
1274 if (next != &map->header) {
1275 esize = entry->end - entry->start;
1276 if ((entry->end == next->start) &&
1277 (next->object.vm_object == entry->object.vm_object) &&
1278 (!entry->object.vm_object ||
1279 (entry->offset + esize == next->offset)) &&
1280 (next->eflags == entry->eflags) &&
1281 (next->protection == entry->protection) &&
1282 (next->max_protection == entry->max_protection) &&
1283 (next->inheritance == entry->inheritance) &&
1284 (next->wired_count == entry->wired_count)) {
1285 vm_map_entry_unlink(map, next);
1286 entry->end = next->end;
1287 vm_map_entry_resize_free(map, entry);
1288 if (next->object.vm_object)
1289 vm_object_deallocate(next->object.vm_object);
1290 vm_map_entry_dispose(map, next);
1295 * vm_map_clip_start: [ internal use only ]
1297 * Asserts that the given entry begins at or after
1298 * the specified address; if necessary,
1299 * it splits the entry into two.
1301 #define vm_map_clip_start(map, entry, startaddr) \
1303 if (startaddr > entry->start) \
1304 _vm_map_clip_start(map, entry, startaddr); \
1308 * This routine is called only when it is known that
1309 * the entry must be split.
1312 _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start)
1314 vm_map_entry_t new_entry;
1317 * Split off the front portion -- note that we must insert the new
1318 * entry BEFORE this one, so that this entry has the specified
1321 vm_map_simplify_entry(map, entry);
1324 * If there is no object backing this entry, we might as well create
1325 * one now. If we defer it, an object can get created after the map
1326 * is clipped, and individual objects will be created for the split-up
1327 * map. This is a bit of a hack, but is also about the best place to
1328 * put this improvement.
1330 if (entry->object.vm_object == NULL && !map->system_map) {
1332 object = vm_object_allocate(OBJT_DEFAULT,
1333 atop(entry->end - entry->start));
1334 entry->object.vm_object = object;
1338 new_entry = vm_map_entry_create(map);
1339 *new_entry = *entry;
1341 new_entry->end = start;
1342 entry->offset += (start - entry->start);
1343 entry->start = start;
1345 vm_map_entry_link(map, entry->prev, new_entry);
1347 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1348 vm_object_reference(new_entry->object.vm_object);
1353 * vm_map_clip_end: [ internal use only ]
1355 * Asserts that the given entry ends at or before
1356 * the specified address; if necessary,
1357 * it splits the entry into two.
1359 #define vm_map_clip_end(map, entry, endaddr) \
1361 if ((endaddr) < (entry->end)) \
1362 _vm_map_clip_end((map), (entry), (endaddr)); \
1366 * This routine is called only when it is known that
1367 * the entry must be split.
1370 _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end)
1372 vm_map_entry_t new_entry;
1375 * If there is no object backing this entry, we might as well create
1376 * one now. If we defer it, an object can get created after the map
1377 * is clipped, and individual objects will be created for the split-up
1378 * map. This is a bit of a hack, but is also about the best place to
1379 * put this improvement.
1381 if (entry->object.vm_object == NULL && !map->system_map) {
1383 object = vm_object_allocate(OBJT_DEFAULT,
1384 atop(entry->end - entry->start));
1385 entry->object.vm_object = object;
1390 * Create a new entry and insert it AFTER the specified entry
1392 new_entry = vm_map_entry_create(map);
1393 *new_entry = *entry;
1395 new_entry->start = entry->end = end;
1396 new_entry->offset += (end - entry->start);
1398 vm_map_entry_link(map, entry, new_entry);
1400 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1401 vm_object_reference(new_entry->object.vm_object);
1406 * vm_map_submap: [ kernel use only ]
1408 * Mark the given range as handled by a subordinate map.
1410 * This range must have been created with vm_map_find,
1411 * and no other operations may have been performed on this
1412 * range prior to calling vm_map_submap.
1414 * Only a limited number of operations can be performed
1415 * within this rage after calling vm_map_submap:
1417 * [Don't try vm_map_copy!]
1419 * To remove a submapping, one must first remove the
1420 * range from the superior map, and then destroy the
1421 * submap (if desired). [Better yet, don't try it.]
1430 vm_map_entry_t entry;
1431 int result = KERN_INVALID_ARGUMENT;
1435 VM_MAP_RANGE_CHECK(map, start, end);
1437 if (vm_map_lookup_entry(map, start, &entry)) {
1438 vm_map_clip_start(map, entry, start);
1440 entry = entry->next;
1442 vm_map_clip_end(map, entry, end);
1444 if ((entry->start == start) && (entry->end == end) &&
1445 ((entry->eflags & MAP_ENTRY_COW) == 0) &&
1446 (entry->object.vm_object == NULL)) {
1447 entry->object.sub_map = submap;
1448 entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
1449 result = KERN_SUCCESS;
1457 * The maximum number of pages to map
1459 #define MAX_INIT_PT 96
1462 * vm_map_pmap_enter:
1464 * Preload read-only mappings for the given object's resident pages into
1465 * the given map. This eliminates the soft faults on process startup and
1466 * immediately after an mmap(2). Unless the given flags include
1467 * MAP_PREFAULT_MADVISE, cached pages are not reactivated and mapped.
1470 vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
1471 vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags)
1474 vm_page_t p, p_start;
1475 vm_pindex_t psize, tmpidx;
1476 boolean_t are_queues_locked;
1478 if ((prot & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 || object == NULL)
1480 VM_OBJECT_LOCK(object);
1481 if (object->type == OBJT_DEVICE) {
1482 pmap_object_init_pt(map->pmap, addr, object, pindex, size);
1488 if (object->type != OBJT_VNODE ||
1489 ((flags & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
1490 (object->resident_page_count > MAX_INIT_PT))) {
1494 if (psize + pindex > object->size) {
1495 if (object->size < pindex)
1497 psize = object->size - pindex;
1500 are_queues_locked = FALSE;
1504 if ((p = TAILQ_FIRST(&object->memq)) != NULL) {
1505 if (p->pindex < pindex) {
1506 p = vm_page_splay(pindex, object->root);
1507 if ((object->root = p)->pindex < pindex)
1508 p = TAILQ_NEXT(p, listq);
1512 * Assert: the variable p is either (1) the page with the
1513 * least pindex greater than or equal to the parameter pindex
1517 p != NULL && (tmpidx = p->pindex - pindex) < psize;
1518 p = TAILQ_NEXT(p, listq)) {
1520 * don't allow an madvise to blow away our really
1521 * free pages allocating pv entries.
1523 if ((flags & MAP_PREFAULT_MADVISE) &&
1524 cnt.v_free_count < cnt.v_free_reserved) {
1528 if ((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL &&
1530 if (p_start == NULL) {
1531 start = addr + ptoa(tmpidx);
1534 } else if (p_start != NULL) {
1535 if (!are_queues_locked) {
1536 are_queues_locked = TRUE;
1537 vm_page_lock_queues();
1539 pmap_enter_object(map->pmap, start, addr +
1540 ptoa(tmpidx), p_start, prot);
1544 if (p_start != NULL) {
1545 if (!are_queues_locked) {
1546 are_queues_locked = TRUE;
1547 vm_page_lock_queues();
1549 pmap_enter_object(map->pmap, start, addr + ptoa(psize),
1552 if (are_queues_locked)
1553 vm_page_unlock_queues();
1555 VM_OBJECT_UNLOCK(object);
1561 * Sets the protection of the specified address
1562 * region in the target map. If "set_max" is
1563 * specified, the maximum protection is to be set;
1564 * otherwise, only the current protection is affected.
1567 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
1568 vm_prot_t new_prot, boolean_t set_max)
1570 vm_map_entry_t current;
1571 vm_map_entry_t entry;
1575 VM_MAP_RANGE_CHECK(map, start, end);
1577 if (vm_map_lookup_entry(map, start, &entry)) {
1578 vm_map_clip_start(map, entry, start);
1580 entry = entry->next;
1584 * Make a first pass to check for protection violations.
1587 while ((current != &map->header) && (current->start < end)) {
1588 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1590 return (KERN_INVALID_ARGUMENT);
1592 if ((new_prot & current->max_protection) != new_prot) {
1594 return (KERN_PROTECTION_FAILURE);
1596 current = current->next;
1600 * Go back and fix up protections. [Note that clipping is not
1601 * necessary the second time.]
1604 while ((current != &map->header) && (current->start < end)) {
1607 vm_map_clip_end(map, current, end);
1609 old_prot = current->protection;
1611 current->protection =
1612 (current->max_protection = new_prot) &
1615 current->protection = new_prot;
1618 * Update physical map if necessary. Worry about copy-on-write
1619 * here -- CHECK THIS XXX
1621 if (current->protection != old_prot) {
1622 #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1624 pmap_protect(map->pmap, current->start,
1626 current->protection & MASK(current));
1629 vm_map_simplify_entry(map, current);
1630 current = current->next;
1633 return (KERN_SUCCESS);
1639 * This routine traverses a processes map handling the madvise
1640 * system call. Advisories are classified as either those effecting
1641 * the vm_map_entry structure, or those effecting the underlying
1651 vm_map_entry_t current, entry;
1655 * Some madvise calls directly modify the vm_map_entry, in which case
1656 * we need to use an exclusive lock on the map and we need to perform
1657 * various clipping operations. Otherwise we only need a read-lock
1662 case MADV_SEQUENTIAL:
1674 vm_map_lock_read(map);
1677 return (KERN_INVALID_ARGUMENT);
1681 * Locate starting entry and clip if necessary.
1683 VM_MAP_RANGE_CHECK(map, start, end);
1685 if (vm_map_lookup_entry(map, start, &entry)) {
1687 vm_map_clip_start(map, entry, start);
1689 entry = entry->next;
1694 * madvise behaviors that are implemented in the vm_map_entry.
1696 * We clip the vm_map_entry so that behavioral changes are
1697 * limited to the specified address range.
1699 for (current = entry;
1700 (current != &map->header) && (current->start < end);
1701 current = current->next
1703 if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1706 vm_map_clip_end(map, current, end);
1710 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
1712 case MADV_SEQUENTIAL:
1713 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
1716 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
1719 current->eflags |= MAP_ENTRY_NOSYNC;
1722 current->eflags &= ~MAP_ENTRY_NOSYNC;
1725 current->eflags |= MAP_ENTRY_NOCOREDUMP;
1728 current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
1733 vm_map_simplify_entry(map, current);
1741 * madvise behaviors that are implemented in the underlying
1744 * Since we don't clip the vm_map_entry, we have to clip
1745 * the vm_object pindex and count.
1747 for (current = entry;
1748 (current != &map->header) && (current->start < end);
1749 current = current->next
1751 vm_offset_t useStart;
1753 if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1756 pindex = OFF_TO_IDX(current->offset);
1757 count = atop(current->end - current->start);
1758 useStart = current->start;
1760 if (current->start < start) {
1761 pindex += atop(start - current->start);
1762 count -= atop(start - current->start);
1765 if (current->end > end)
1766 count -= atop(current->end - end);
1771 vm_object_madvise(current->object.vm_object,
1772 pindex, count, behav);
1773 if (behav == MADV_WILLNEED) {
1774 vm_map_pmap_enter(map,
1776 current->protection,
1777 current->object.vm_object,
1779 (count << PAGE_SHIFT),
1780 MAP_PREFAULT_MADVISE
1784 vm_map_unlock_read(map);
1793 * Sets the inheritance of the specified address
1794 * range in the target map. Inheritance
1795 * affects how the map will be shared with
1796 * child maps at the time of vm_map_fork.
1799 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
1800 vm_inherit_t new_inheritance)
1802 vm_map_entry_t entry;
1803 vm_map_entry_t temp_entry;
1805 switch (new_inheritance) {
1806 case VM_INHERIT_NONE:
1807 case VM_INHERIT_COPY:
1808 case VM_INHERIT_SHARE:
1811 return (KERN_INVALID_ARGUMENT);
1814 VM_MAP_RANGE_CHECK(map, start, end);
1815 if (vm_map_lookup_entry(map, start, &temp_entry)) {
1817 vm_map_clip_start(map, entry, start);
1819 entry = temp_entry->next;
1820 while ((entry != &map->header) && (entry->start < end)) {
1821 vm_map_clip_end(map, entry, end);
1822 entry->inheritance = new_inheritance;
1823 vm_map_simplify_entry(map, entry);
1824 entry = entry->next;
1827 return (KERN_SUCCESS);
1833 * Implements both kernel and user unwiring.
1836 vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
1839 vm_map_entry_t entry, first_entry, tmp_entry;
1840 vm_offset_t saved_start;
1841 unsigned int last_timestamp;
1843 boolean_t need_wakeup, result, user_unwire;
1845 user_unwire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
1847 VM_MAP_RANGE_CHECK(map, start, end);
1848 if (!vm_map_lookup_entry(map, start, &first_entry)) {
1849 if (flags & VM_MAP_WIRE_HOLESOK)
1850 first_entry = first_entry->next;
1853 return (KERN_INVALID_ADDRESS);
1856 last_timestamp = map->timestamp;
1857 entry = first_entry;
1858 while (entry != &map->header && entry->start < end) {
1859 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1861 * We have not yet clipped the entry.
1863 saved_start = (start >= entry->start) ? start :
1865 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1866 if (vm_map_unlock_and_wait(map, user_unwire)) {
1868 * Allow interruption of user unwiring?
1872 if (last_timestamp+1 != map->timestamp) {
1874 * Look again for the entry because the map was
1875 * modified while it was unlocked.
1876 * Specifically, the entry may have been
1877 * clipped, merged, or deleted.
1879 if (!vm_map_lookup_entry(map, saved_start,
1881 if (flags & VM_MAP_WIRE_HOLESOK)
1882 tmp_entry = tmp_entry->next;
1884 if (saved_start == start) {
1886 * First_entry has been deleted.
1889 return (KERN_INVALID_ADDRESS);
1892 rv = KERN_INVALID_ADDRESS;
1896 if (entry == first_entry)
1897 first_entry = tmp_entry;
1902 last_timestamp = map->timestamp;
1905 vm_map_clip_start(map, entry, start);
1906 vm_map_clip_end(map, entry, end);
1908 * Mark the entry in case the map lock is released. (See
1911 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
1913 * Check the map for holes in the specified region.
1914 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
1916 if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
1917 (entry->end < end && (entry->next == &map->header ||
1918 entry->next->start > entry->end))) {
1920 rv = KERN_INVALID_ADDRESS;
1924 * If system unwiring, require that the entry is system wired.
1927 vm_map_entry_system_wired_count(entry) == 0) {
1929 rv = KERN_INVALID_ARGUMENT;
1932 entry = entry->next;
1936 need_wakeup = FALSE;
1937 if (first_entry == NULL) {
1938 result = vm_map_lookup_entry(map, start, &first_entry);
1939 if (!result && (flags & VM_MAP_WIRE_HOLESOK))
1940 first_entry = first_entry->next;
1942 KASSERT(result, ("vm_map_unwire: lookup failed"));
1944 entry = first_entry;
1945 while (entry != &map->header && entry->start < end) {
1946 if (rv == KERN_SUCCESS && (!user_unwire ||
1947 (entry->eflags & MAP_ENTRY_USER_WIRED))) {
1949 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
1950 entry->wired_count--;
1951 if (entry->wired_count == 0) {
1953 * Retain the map lock.
1955 vm_fault_unwire(map, entry->start, entry->end,
1956 entry->object.vm_object != NULL &&
1957 entry->object.vm_object->type == OBJT_DEVICE);
1960 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
1961 ("vm_map_unwire: in-transition flag missing"));
1962 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
1963 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
1964 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
1967 vm_map_simplify_entry(map, entry);
1968 entry = entry->next;
1979 * Implements both kernel and user wiring.
1982 vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
1985 vm_map_entry_t entry, first_entry, tmp_entry;
1986 vm_offset_t saved_end, saved_start;
1987 unsigned int last_timestamp;
1989 boolean_t fictitious, need_wakeup, result, user_wire;
1991 user_wire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
1993 VM_MAP_RANGE_CHECK(map, start, end);
1994 if (!vm_map_lookup_entry(map, start, &first_entry)) {
1995 if (flags & VM_MAP_WIRE_HOLESOK)
1996 first_entry = first_entry->next;
1999 return (KERN_INVALID_ADDRESS);
2002 last_timestamp = map->timestamp;
2003 entry = first_entry;
2004 while (entry != &map->header && entry->start < end) {
2005 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
2007 * We have not yet clipped the entry.
2009 saved_start = (start >= entry->start) ? start :
2011 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2012 if (vm_map_unlock_and_wait(map, user_wire)) {
2014 * Allow interruption of user wiring?
2018 if (last_timestamp + 1 != map->timestamp) {
2020 * Look again for the entry because the map was
2021 * modified while it was unlocked.
2022 * Specifically, the entry may have been
2023 * clipped, merged, or deleted.
2025 if (!vm_map_lookup_entry(map, saved_start,
2027 if (flags & VM_MAP_WIRE_HOLESOK)
2028 tmp_entry = tmp_entry->next;
2030 if (saved_start == start) {
2032 * first_entry has been deleted.
2035 return (KERN_INVALID_ADDRESS);
2038 rv = KERN_INVALID_ADDRESS;
2042 if (entry == first_entry)
2043 first_entry = tmp_entry;
2048 last_timestamp = map->timestamp;
2051 vm_map_clip_start(map, entry, start);
2052 vm_map_clip_end(map, entry, end);
2054 * Mark the entry in case the map lock is released. (See
2057 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
2061 if (entry->wired_count == 0) {
2062 entry->wired_count++;
2063 saved_start = entry->start;
2064 saved_end = entry->end;
2065 fictitious = entry->object.vm_object != NULL &&
2066 entry->object.vm_object->type == OBJT_DEVICE;
2068 * Release the map lock, relying on the in-transition
2072 rv = vm_fault_wire(map, saved_start, saved_end,
2073 user_wire, fictitious);
2075 if (last_timestamp + 1 != map->timestamp) {
2077 * Look again for the entry because the map was
2078 * modified while it was unlocked. The entry
2079 * may have been clipped, but NOT merged or
2082 result = vm_map_lookup_entry(map, saved_start,
2084 KASSERT(result, ("vm_map_wire: lookup failed"));
2085 if (entry == first_entry)
2086 first_entry = tmp_entry;
2090 while (entry->end < saved_end) {
2091 if (rv != KERN_SUCCESS) {
2092 KASSERT(entry->wired_count == 1,
2093 ("vm_map_wire: bad count"));
2094 entry->wired_count = -1;
2096 entry = entry->next;
2099 last_timestamp = map->timestamp;
2100 if (rv != KERN_SUCCESS) {
2101 KASSERT(entry->wired_count == 1,
2102 ("vm_map_wire: bad count"));
2104 * Assign an out-of-range value to represent
2105 * the failure to wire this entry.
2107 entry->wired_count = -1;
2111 } else if (!user_wire ||
2112 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
2113 entry->wired_count++;
2116 * Check the map for holes in the specified region.
2117 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
2119 if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
2120 (entry->end < end && (entry->next == &map->header ||
2121 entry->next->start > entry->end))) {
2123 rv = KERN_INVALID_ADDRESS;
2126 entry = entry->next;
2130 need_wakeup = FALSE;
2131 if (first_entry == NULL) {
2132 result = vm_map_lookup_entry(map, start, &first_entry);
2133 if (!result && (flags & VM_MAP_WIRE_HOLESOK))
2134 first_entry = first_entry->next;
2136 KASSERT(result, ("vm_map_wire: lookup failed"));
2138 entry = first_entry;
2139 while (entry != &map->header && entry->start < end) {
2140 if (rv == KERN_SUCCESS) {
2142 entry->eflags |= MAP_ENTRY_USER_WIRED;
2143 } else if (entry->wired_count == -1) {
2145 * Wiring failed on this entry. Thus, unwiring is
2148 entry->wired_count = 0;
2151 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0)
2152 entry->wired_count--;
2153 if (entry->wired_count == 0) {
2155 * Retain the map lock.
2157 vm_fault_unwire(map, entry->start, entry->end,
2158 entry->object.vm_object != NULL &&
2159 entry->object.vm_object->type == OBJT_DEVICE);
2162 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
2163 ("vm_map_wire: in-transition flag missing"));
2164 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
2165 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
2166 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
2169 vm_map_simplify_entry(map, entry);
2170 entry = entry->next;
2181 * Push any dirty cached pages in the address range to their pager.
2182 * If syncio is TRUE, dirty pages are written synchronously.
2183 * If invalidate is TRUE, any cached pages are freed as well.
2185 * If the size of the region from start to end is zero, we are
2186 * supposed to flush all modified pages within the region containing
2187 * start. Unfortunately, a region can be split or coalesced with
2188 * neighboring regions, making it difficult to determine what the
2189 * original region was. Therefore, we approximate this requirement by
2190 * flushing the current region containing start.
2192 * Returns an error if any part of the specified range is not mapped.
2200 boolean_t invalidate)
2202 vm_map_entry_t current;
2203 vm_map_entry_t entry;
2206 vm_ooffset_t offset;
2208 vm_map_lock_read(map);
2209 VM_MAP_RANGE_CHECK(map, start, end);
2210 if (!vm_map_lookup_entry(map, start, &entry)) {
2211 vm_map_unlock_read(map);
2212 return (KERN_INVALID_ADDRESS);
2213 } else if (start == end) {
2214 start = entry->start;
2218 * Make a first pass to check for user-wired memory and holes.
2220 for (current = entry; current != &map->header && current->start < end;
2221 current = current->next) {
2222 if (invalidate && (current->eflags & MAP_ENTRY_USER_WIRED)) {
2223 vm_map_unlock_read(map);
2224 return (KERN_INVALID_ARGUMENT);
2226 if (end > current->end &&
2227 (current->next == &map->header ||
2228 current->end != current->next->start)) {
2229 vm_map_unlock_read(map);
2230 return (KERN_INVALID_ADDRESS);
2235 pmap_remove(map->pmap, start, end);
2238 * Make a second pass, cleaning/uncaching pages from the indicated
2241 for (current = entry; current != &map->header && current->start < end;
2242 current = current->next) {
2243 offset = current->offset + (start - current->start);
2244 size = (end <= current->end ? end : current->end) - start;
2245 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
2247 vm_map_entry_t tentry;
2250 smap = current->object.sub_map;
2251 vm_map_lock_read(smap);
2252 (void) vm_map_lookup_entry(smap, offset, &tentry);
2253 tsize = tentry->end - offset;
2256 object = tentry->object.vm_object;
2257 offset = tentry->offset + (offset - tentry->start);
2258 vm_map_unlock_read(smap);
2260 object = current->object.vm_object;
2262 vm_object_sync(object, offset, size, syncio, invalidate);
2266 vm_map_unlock_read(map);
2267 return (KERN_SUCCESS);
2271 * vm_map_entry_unwire: [ internal use only ]
2273 * Make the region specified by this entry pageable.
2275 * The map in question should be locked.
2276 * [This is the reason for this routine's existence.]
2279 vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
2281 vm_fault_unwire(map, entry->start, entry->end,
2282 entry->object.vm_object != NULL &&
2283 entry->object.vm_object->type == OBJT_DEVICE);
2284 entry->wired_count = 0;
2288 * vm_map_entry_delete: [ internal use only ]
2290 * Deallocate the given entry from the target map.
2293 vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry)
2296 vm_pindex_t offidxstart, offidxend, count;
2298 vm_map_entry_unlink(map, entry);
2299 map->size -= entry->end - entry->start;
2301 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
2302 (object = entry->object.vm_object) != NULL) {
2303 count = OFF_TO_IDX(entry->end - entry->start);
2304 offidxstart = OFF_TO_IDX(entry->offset);
2305 offidxend = offidxstart + count;
2306 VM_OBJECT_LOCK(object);
2307 if (object->ref_count != 1 &&
2308 ((object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING ||
2309 object == kernel_object || object == kmem_object)) {
2310 vm_object_collapse(object);
2311 vm_object_page_remove(object, offidxstart, offidxend, FALSE);
2312 if (object->type == OBJT_SWAP)
2313 swap_pager_freespace(object, offidxstart, count);
2314 if (offidxend >= object->size &&
2315 offidxstart < object->size)
2316 object->size = offidxstart;
2318 VM_OBJECT_UNLOCK(object);
2319 vm_object_deallocate(object);
2322 vm_map_entry_dispose(map, entry);
2326 * vm_map_delete: [ internal use only ]
2328 * Deallocates the given address range from the target
2332 vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end)
2334 vm_map_entry_t entry;
2335 vm_map_entry_t first_entry;
2338 * Find the start of the region, and clip it
2340 if (!vm_map_lookup_entry(map, start, &first_entry))
2341 entry = first_entry->next;
2343 entry = first_entry;
2344 vm_map_clip_start(map, entry, start);
2348 * Step through all entries in this region
2350 while ((entry != &map->header) && (entry->start < end)) {
2351 vm_map_entry_t next;
2354 * Wait for wiring or unwiring of an entry to complete.
2355 * Also wait for any system wirings to disappear on
2358 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 ||
2359 (vm_map_pmap(map) != kernel_pmap &&
2360 vm_map_entry_system_wired_count(entry) != 0)) {
2361 unsigned int last_timestamp;
2362 vm_offset_t saved_start;
2363 vm_map_entry_t tmp_entry;
2365 saved_start = entry->start;
2366 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2367 last_timestamp = map->timestamp;
2368 (void) vm_map_unlock_and_wait(map, FALSE);
2370 if (last_timestamp + 1 != map->timestamp) {
2372 * Look again for the entry because the map was
2373 * modified while it was unlocked.
2374 * Specifically, the entry may have been
2375 * clipped, merged, or deleted.
2377 if (!vm_map_lookup_entry(map, saved_start,
2379 entry = tmp_entry->next;
2382 vm_map_clip_start(map, entry,
2388 vm_map_clip_end(map, entry, end);
2393 * Unwire before removing addresses from the pmap; otherwise,
2394 * unwiring will put the entries back in the pmap.
2396 if (entry->wired_count != 0) {
2397 vm_map_entry_unwire(map, entry);
2400 pmap_remove(map->pmap, entry->start, entry->end);
2403 * Delete the entry (which may delete the object) only after
2404 * removing all pmap entries pointing to its pages.
2405 * (Otherwise, its page frames may be reallocated, and any
2406 * modify bits will be set in the wrong object!)
2408 vm_map_entry_delete(map, entry);
2411 return (KERN_SUCCESS);
2417 * Remove the given address range from the target map.
2418 * This is the exported form of vm_map_delete.
2421 vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
2426 VM_MAP_RANGE_CHECK(map, start, end);
2427 result = vm_map_delete(map, start, end);
2433 * vm_map_check_protection:
2435 * Assert that the target map allows the specified privilege on the
2436 * entire address region given. The entire region must be allocated.
2438 * WARNING! This code does not and should not check whether the
2439 * contents of the region is accessible. For example a smaller file
2440 * might be mapped into a larger address space.
2442 * NOTE! This code is also called by munmap().
2444 * The map must be locked. A read lock is sufficient.
2447 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
2448 vm_prot_t protection)
2450 vm_map_entry_t entry;
2451 vm_map_entry_t tmp_entry;
2453 if (!vm_map_lookup_entry(map, start, &tmp_entry))
2457 while (start < end) {
2458 if (entry == &map->header)
2463 if (start < entry->start)
2466 * Check protection associated with entry.
2468 if ((entry->protection & protection) != protection)
2470 /* go to next entry */
2472 entry = entry->next;
2478 * vm_map_copy_entry:
2480 * Copies the contents of the source entry to the destination
2481 * entry. The entries *must* be aligned properly.
2487 vm_map_entry_t src_entry,
2488 vm_map_entry_t dst_entry)
2490 vm_object_t src_object;
2492 if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
2495 if (src_entry->wired_count == 0) {
2498 * If the source entry is marked needs_copy, it is already
2501 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
2502 pmap_protect(src_map->pmap,
2505 src_entry->protection & ~VM_PROT_WRITE);
2509 * Make a copy of the object.
2511 if ((src_object = src_entry->object.vm_object) != NULL) {
2512 VM_OBJECT_LOCK(src_object);
2513 if ((src_object->handle == NULL) &&
2514 (src_object->type == OBJT_DEFAULT ||
2515 src_object->type == OBJT_SWAP)) {
2516 vm_object_collapse(src_object);
2517 if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
2518 vm_object_split(src_entry);
2519 src_object = src_entry->object.vm_object;
2522 vm_object_reference_locked(src_object);
2523 vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
2524 VM_OBJECT_UNLOCK(src_object);
2525 dst_entry->object.vm_object = src_object;
2526 src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2527 dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2528 dst_entry->offset = src_entry->offset;
2530 dst_entry->object.vm_object = NULL;
2531 dst_entry->offset = 0;
2534 pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
2535 dst_entry->end - dst_entry->start, src_entry->start);
2538 * Of course, wired down pages can't be set copy-on-write.
2539 * Cause wired pages to be copied into the new map by
2540 * simulating faults (the new pages are pageable)
2542 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry);
2547 * vmspace_map_entry_forked:
2548 * Update the newly-forked vmspace each time a map entry is inherited
2549 * or copied. The values for vm_dsize and vm_tsize are approximate
2550 * (and mostly-obsolete ideas in the face of mmap(2) et al.)
2553 vmspace_map_entry_forked(const struct vmspace *vm1, struct vmspace *vm2,
2554 vm_map_entry_t entry)
2556 vm_size_t entrysize;
2559 entrysize = entry->end - entry->start;
2560 vm2->vm_map.size += entrysize;
2561 if (entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP)) {
2562 vm2->vm_ssize += btoc(entrysize);
2563 } else if (entry->start >= (vm_offset_t)vm1->vm_daddr &&
2564 entry->start < (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)) {
2565 newend = MIN(entry->end,
2566 (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize));
2567 vm2->vm_dsize += btoc(newend - entry->start);
2568 } else if (entry->start >= (vm_offset_t)vm1->vm_taddr &&
2569 entry->start < (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)) {
2570 newend = MIN(entry->end,
2571 (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize));
2572 vm2->vm_tsize += btoc(newend - entry->start);
2578 * Create a new process vmspace structure and vm_map
2579 * based on those of an existing process. The new map
2580 * is based on the old map, according to the inheritance
2581 * values on the regions in that map.
2583 * XXX It might be worth coalescing the entries added to the new vmspace.
2585 * The source map must not be locked.
2588 vmspace_fork(struct vmspace *vm1)
2590 struct vmspace *vm2;
2591 vm_map_t old_map = &vm1->vm_map;
2593 vm_map_entry_t old_entry;
2594 vm_map_entry_t new_entry;
2597 vm_map_lock(old_map);
2599 vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
2601 goto unlock_and_return;
2602 vm2->vm_taddr = vm1->vm_taddr;
2603 vm2->vm_daddr = vm1->vm_daddr;
2604 vm2->vm_maxsaddr = vm1->vm_maxsaddr;
2605 new_map = &vm2->vm_map; /* XXX */
2606 new_map->timestamp = 1;
2608 old_entry = old_map->header.next;
2610 while (old_entry != &old_map->header) {
2611 if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2612 panic("vm_map_fork: encountered a submap");
2614 switch (old_entry->inheritance) {
2615 case VM_INHERIT_NONE:
2618 case VM_INHERIT_SHARE:
2620 * Clone the entry, creating the shared object if necessary.
2622 object = old_entry->object.vm_object;
2623 if (object == NULL) {
2624 object = vm_object_allocate(OBJT_DEFAULT,
2625 atop(old_entry->end - old_entry->start));
2626 old_entry->object.vm_object = object;
2627 old_entry->offset = 0;
2631 * Add the reference before calling vm_object_shadow
2632 * to insure that a shadow object is created.
2634 vm_object_reference(object);
2635 if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
2636 vm_object_shadow(&old_entry->object.vm_object,
2638 atop(old_entry->end - old_entry->start));
2639 old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
2640 /* Transfer the second reference too. */
2641 vm_object_reference(
2642 old_entry->object.vm_object);
2643 vm_object_deallocate(object);
2644 object = old_entry->object.vm_object;
2646 VM_OBJECT_LOCK(object);
2647 vm_object_clear_flag(object, OBJ_ONEMAPPING);
2648 VM_OBJECT_UNLOCK(object);
2651 * Clone the entry, referencing the shared object.
2653 new_entry = vm_map_entry_create(new_map);
2654 *new_entry = *old_entry;
2655 new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
2656 MAP_ENTRY_IN_TRANSITION);
2657 new_entry->wired_count = 0;
2660 * Insert the entry into the new map -- we know we're
2661 * inserting at the end of the new map.
2663 vm_map_entry_link(new_map, new_map->header.prev,
2665 vmspace_map_entry_forked(vm1, vm2, new_entry);
2668 * Update the physical map
2670 pmap_copy(new_map->pmap, old_map->pmap,
2672 (old_entry->end - old_entry->start),
2676 case VM_INHERIT_COPY:
2678 * Clone the entry and link into the map.
2680 new_entry = vm_map_entry_create(new_map);
2681 *new_entry = *old_entry;
2682 new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
2683 MAP_ENTRY_IN_TRANSITION);
2684 new_entry->wired_count = 0;
2685 new_entry->object.vm_object = NULL;
2686 vm_map_entry_link(new_map, new_map->header.prev,
2688 vmspace_map_entry_forked(vm1, vm2, new_entry);
2689 vm_map_copy_entry(old_map, new_map, old_entry,
2693 old_entry = old_entry->next;
2696 vm_map_unlock(old_map);
2702 vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
2703 vm_prot_t prot, vm_prot_t max, int cow)
2705 vm_map_entry_t new_entry, prev_entry;
2706 vm_offset_t bot, top;
2707 vm_size_t init_ssize;
2712 * The stack orientation is piggybacked with the cow argument.
2713 * Extract it into orient and mask the cow argument so that we
2714 * don't pass it around further.
2715 * NOTE: We explicitly allow bi-directional stacks.
2717 orient = cow & (MAP_STACK_GROWS_DOWN|MAP_STACK_GROWS_UP);
2719 KASSERT(orient != 0, ("No stack grow direction"));
2721 if (addrbos < vm_map_min(map) ||
2722 addrbos > vm_map_max(map) ||
2723 addrbos + max_ssize < addrbos)
2724 return (KERN_NO_SPACE);
2726 init_ssize = (max_ssize < sgrowsiz) ? max_ssize : sgrowsiz;
2728 PROC_LOCK(curthread->td_proc);
2729 vmemlim = lim_cur(curthread->td_proc, RLIMIT_VMEM);
2730 PROC_UNLOCK(curthread->td_proc);
2734 /* If addr is already mapped, no go */
2735 if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
2737 return (KERN_NO_SPACE);
2740 /* If we would blow our VMEM resource limit, no go */
2741 if (map->size + init_ssize > vmemlim) {
2743 return (KERN_NO_SPACE);
2747 * If we can't accomodate max_ssize in the current mapping, no go.
2748 * However, we need to be aware that subsequent user mappings might
2749 * map into the space we have reserved for stack, and currently this
2750 * space is not protected.
2752 * Hopefully we will at least detect this condition when we try to
2755 if ((prev_entry->next != &map->header) &&
2756 (prev_entry->next->start < addrbos + max_ssize)) {
2758 return (KERN_NO_SPACE);
2762 * We initially map a stack of only init_ssize. We will grow as
2763 * needed later. Depending on the orientation of the stack (i.e.
2764 * the grow direction) we either map at the top of the range, the
2765 * bottom of the range or in the middle.
2767 * Note: we would normally expect prot and max to be VM_PROT_ALL,
2768 * and cow to be 0. Possibly we should eliminate these as input
2769 * parameters, and just pass these values here in the insert call.
2771 if (orient == MAP_STACK_GROWS_DOWN)
2772 bot = addrbos + max_ssize - init_ssize;
2773 else if (orient == MAP_STACK_GROWS_UP)
2776 bot = round_page(addrbos + max_ssize/2 - init_ssize/2);
2777 top = bot + init_ssize;
2778 rv = vm_map_insert(map, NULL, 0, bot, top, prot, max, cow);
2780 /* Now set the avail_ssize amount. */
2781 if (rv == KERN_SUCCESS) {
2782 if (prev_entry != &map->header)
2783 vm_map_clip_end(map, prev_entry, bot);
2784 new_entry = prev_entry->next;
2785 if (new_entry->end != top || new_entry->start != bot)
2786 panic("Bad entry start/end for new stack entry");
2788 new_entry->avail_ssize = max_ssize - init_ssize;
2789 if (orient & MAP_STACK_GROWS_DOWN)
2790 new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
2791 if (orient & MAP_STACK_GROWS_UP)
2792 new_entry->eflags |= MAP_ENTRY_GROWS_UP;
2799 /* Attempts to grow a vm stack entry. Returns KERN_SUCCESS if the
2800 * desired address is already mapped, or if we successfully grow
2801 * the stack. Also returns KERN_SUCCESS if addr is outside the
2802 * stack range (this is strange, but preserves compatibility with
2803 * the grow function in vm_machdep.c).
2806 vm_map_growstack(struct proc *p, vm_offset_t addr)
2808 vm_map_entry_t next_entry, prev_entry;
2809 vm_map_entry_t new_entry, stack_entry;
2810 struct vmspace *vm = p->p_vmspace;
2811 vm_map_t map = &vm->vm_map;
2813 size_t grow_amount, max_grow;
2814 rlim_t stacklim, vmemlim;
2815 int is_procstack, rv;
2819 stacklim = lim_cur(p, RLIMIT_STACK);
2820 vmemlim = lim_cur(p, RLIMIT_VMEM);
2823 vm_map_lock_read(map);
2825 /* If addr is already in the entry range, no need to grow.*/
2826 if (vm_map_lookup_entry(map, addr, &prev_entry)) {
2827 vm_map_unlock_read(map);
2828 return (KERN_SUCCESS);
2831 next_entry = prev_entry->next;
2832 if (!(prev_entry->eflags & MAP_ENTRY_GROWS_UP)) {
2834 * This entry does not grow upwards. Since the address lies
2835 * beyond this entry, the next entry (if one exists) has to
2836 * be a downward growable entry. The entry list header is
2837 * never a growable entry, so it suffices to check the flags.
2839 if (!(next_entry->eflags & MAP_ENTRY_GROWS_DOWN)) {
2840 vm_map_unlock_read(map);
2841 return (KERN_SUCCESS);
2843 stack_entry = next_entry;
2846 * This entry grows upward. If the next entry does not at
2847 * least grow downwards, this is the entry we need to grow.
2848 * otherwise we have two possible choices and we have to
2851 if (next_entry->eflags & MAP_ENTRY_GROWS_DOWN) {
2853 * We have two choices; grow the entry closest to
2854 * the address to minimize the amount of growth.
2856 if (addr - prev_entry->end <= next_entry->start - addr)
2857 stack_entry = prev_entry;
2859 stack_entry = next_entry;
2861 stack_entry = prev_entry;
2864 if (stack_entry == next_entry) {
2865 KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_DOWN, ("foo"));
2866 KASSERT(addr < stack_entry->start, ("foo"));
2867 end = (prev_entry != &map->header) ? prev_entry->end :
2868 stack_entry->start - stack_entry->avail_ssize;
2869 grow_amount = roundup(stack_entry->start - addr, PAGE_SIZE);
2870 max_grow = stack_entry->start - end;
2872 KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_UP, ("foo"));
2873 KASSERT(addr >= stack_entry->end, ("foo"));
2874 end = (next_entry != &map->header) ? next_entry->start :
2875 stack_entry->end + stack_entry->avail_ssize;
2876 grow_amount = roundup(addr + 1 - stack_entry->end, PAGE_SIZE);
2877 max_grow = end - stack_entry->end;
2880 if (grow_amount > stack_entry->avail_ssize) {
2881 vm_map_unlock_read(map);
2882 return (KERN_NO_SPACE);
2886 * If there is no longer enough space between the entries nogo, and
2887 * adjust the available space. Note: this should only happen if the
2888 * user has mapped into the stack area after the stack was created,
2889 * and is probably an error.
2891 * This also effectively destroys any guard page the user might have
2892 * intended by limiting the stack size.
2894 if (grow_amount > max_grow) {
2895 if (vm_map_lock_upgrade(map))
2898 stack_entry->avail_ssize = max_grow;
2901 return (KERN_NO_SPACE);
2904 is_procstack = (addr >= (vm_offset_t)vm->vm_maxsaddr) ? 1 : 0;
2907 * If this is the main process stack, see if we're over the stack
2910 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
2911 vm_map_unlock_read(map);
2912 return (KERN_NO_SPACE);
2915 /* Round up the grow amount modulo SGROWSIZ */
2916 grow_amount = roundup (grow_amount, sgrowsiz);
2917 if (grow_amount > stack_entry->avail_ssize)
2918 grow_amount = stack_entry->avail_ssize;
2919 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
2920 grow_amount = stacklim - ctob(vm->vm_ssize);
2923 /* If we would blow our VMEM resource limit, no go */
2924 if (map->size + grow_amount > vmemlim) {
2925 vm_map_unlock_read(map);
2926 return (KERN_NO_SPACE);
2929 if (vm_map_lock_upgrade(map))
2932 if (stack_entry == next_entry) {
2936 /* Get the preliminary new entry start value */
2937 addr = stack_entry->start - grow_amount;
2940 * If this puts us into the previous entry, cut back our
2941 * growth to the available space. Also, see the note above.
2944 stack_entry->avail_ssize = max_grow;
2948 rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start,
2949 p->p_sysent->sv_stackprot, VM_PROT_ALL, 0);
2951 /* Adjust the available stack space by the amount we grew. */
2952 if (rv == KERN_SUCCESS) {
2953 if (prev_entry != &map->header)
2954 vm_map_clip_end(map, prev_entry, addr);
2955 new_entry = prev_entry->next;
2956 KASSERT(new_entry == stack_entry->prev, ("foo"));
2957 KASSERT(new_entry->end == stack_entry->start, ("foo"));
2958 KASSERT(new_entry->start == addr, ("foo"));
2959 grow_amount = new_entry->end - new_entry->start;
2960 new_entry->avail_ssize = stack_entry->avail_ssize -
2962 stack_entry->eflags &= ~MAP_ENTRY_GROWS_DOWN;
2963 new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
2969 addr = stack_entry->end + grow_amount;
2972 * If this puts us into the next entry, cut back our growth
2973 * to the available space. Also, see the note above.
2976 stack_entry->avail_ssize = end - stack_entry->end;
2980 grow_amount = addr - stack_entry->end;
2982 /* Grow the underlying object if applicable. */
2983 if (stack_entry->object.vm_object == NULL ||
2984 vm_object_coalesce(stack_entry->object.vm_object,
2985 stack_entry->offset,
2986 (vm_size_t)(stack_entry->end - stack_entry->start),
2987 (vm_size_t)grow_amount)) {
2988 map->size += (addr - stack_entry->end);
2989 /* Update the current entry. */
2990 stack_entry->end = addr;
2991 stack_entry->avail_ssize -= grow_amount;
2992 vm_map_entry_resize_free(map, stack_entry);
2995 if (next_entry != &map->header)
2996 vm_map_clip_start(map, next_entry, addr);
3001 if (rv == KERN_SUCCESS && is_procstack)
3002 vm->vm_ssize += btoc(grow_amount);
3007 * Heed the MAP_WIREFUTURE flag if it was set for this process.
3009 if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE)) {
3011 (stack_entry == next_entry) ? addr : addr - grow_amount,
3012 (stack_entry == next_entry) ? stack_entry->start : addr,
3013 (p->p_flag & P_SYSTEM)
3014 ? VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES
3015 : VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
3022 * Unshare the specified VM space for exec. If other processes are
3023 * mapped to it, then create a new one. The new vmspace is null.
3026 vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser)
3028 struct vmspace *oldvmspace = p->p_vmspace;
3029 struct vmspace *newvmspace;
3031 newvmspace = vmspace_alloc(minuser, maxuser);
3032 if (newvmspace == NULL)
3034 newvmspace->vm_swrss = oldvmspace->vm_swrss;
3036 * This code is written like this for prototype purposes. The
3037 * goal is to avoid running down the vmspace here, but let the
3038 * other process's that are still using the vmspace to finally
3039 * run it down. Even though there is little or no chance of blocking
3040 * here, it is a good idea to keep this form for future mods.
3042 PROC_VMSPACE_LOCK(p);
3043 p->p_vmspace = newvmspace;
3044 PROC_VMSPACE_UNLOCK(p);
3045 if (p == curthread->td_proc) /* XXXKSE ? */
3046 pmap_activate(curthread);
3047 vmspace_free(oldvmspace);
3052 * Unshare the specified VM space for forcing COW. This
3053 * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
3056 vmspace_unshare(struct proc *p)
3058 struct vmspace *oldvmspace = p->p_vmspace;
3059 struct vmspace *newvmspace;
3061 if (oldvmspace->vm_refcnt == 1)
3063 newvmspace = vmspace_fork(oldvmspace);
3064 if (newvmspace == NULL)
3066 PROC_VMSPACE_LOCK(p);
3067 p->p_vmspace = newvmspace;
3068 PROC_VMSPACE_UNLOCK(p);
3069 if (p == curthread->td_proc) /* XXXKSE ? */
3070 pmap_activate(curthread);
3071 vmspace_free(oldvmspace);
3078 * Finds the VM object, offset, and
3079 * protection for a given virtual address in the
3080 * specified map, assuming a page fault of the
3083 * Leaves the map in question locked for read; return
3084 * values are guaranteed until a vm_map_lookup_done
3085 * call is performed. Note that the map argument
3086 * is in/out; the returned map must be used in
3087 * the call to vm_map_lookup_done.
3089 * A handle (out_entry) is returned for use in
3090 * vm_map_lookup_done, to make that fast.
3092 * If a lookup is requested with "write protection"
3093 * specified, the map may be changed to perform virtual
3094 * copying operations, although the data referenced will
3098 vm_map_lookup(vm_map_t *var_map, /* IN/OUT */
3100 vm_prot_t fault_typea,
3101 vm_map_entry_t *out_entry, /* OUT */
3102 vm_object_t *object, /* OUT */
3103 vm_pindex_t *pindex, /* OUT */
3104 vm_prot_t *out_prot, /* OUT */
3105 boolean_t *wired) /* OUT */
3107 vm_map_entry_t entry;
3108 vm_map_t map = *var_map;
3110 vm_prot_t fault_type = fault_typea;
3114 * Lookup the faulting address.
3117 vm_map_lock_read(map);
3118 #define RETURN(why) \
3120 vm_map_unlock_read(map); \
3125 * If the map has an interesting hint, try it before calling full
3126 * blown lookup routine.
3130 if (entry == NULL ||
3131 (vaddr < entry->start) || (vaddr >= entry->end)) {
3133 * Entry was either not a valid hint, or the vaddr was not
3134 * contained in the entry, so do a full lookup.
3136 if (!vm_map_lookup_entry(map, vaddr, out_entry))
3137 RETURN(KERN_INVALID_ADDRESS);
3145 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3146 vm_map_t old_map = map;
3148 *var_map = map = entry->object.sub_map;
3149 vm_map_unlock_read(old_map);
3154 * Check whether this task is allowed to have this page.
3155 * Note the special case for MAP_ENTRY_COW
3156 * pages with an override. This is to implement a forced
3157 * COW for debuggers.
3159 if (fault_type & VM_PROT_OVERRIDE_WRITE)
3160 prot = entry->max_protection;
3162 prot = entry->protection;
3163 fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
3164 if ((fault_type & prot) != fault_type) {
3165 RETURN(KERN_PROTECTION_FAILURE);
3167 if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
3168 (entry->eflags & MAP_ENTRY_COW) &&
3169 (fault_type & VM_PROT_WRITE) &&
3170 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
3171 RETURN(KERN_PROTECTION_FAILURE);
3175 * If this page is not pageable, we have to get it for all possible
3178 *wired = (entry->wired_count != 0);
3180 prot = fault_type = entry->protection;
3183 * If the entry was copy-on-write, we either ...
3185 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3187 * If we want to write the page, we may as well handle that
3188 * now since we've got the map locked.
3190 * If we don't need to write the page, we just demote the
3191 * permissions allowed.
3193 if (fault_type & VM_PROT_WRITE) {
3195 * Make a new object, and place it in the object
3196 * chain. Note that no new references have appeared
3197 * -- one just moved from the map to the new
3200 if (vm_map_lock_upgrade(map))
3204 &entry->object.vm_object,
3206 atop(entry->end - entry->start));
3207 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
3209 vm_map_lock_downgrade(map);
3212 * We're attempting to read a copy-on-write page --
3213 * don't allow writes.
3215 prot &= ~VM_PROT_WRITE;
3220 * Create an object if necessary.
3222 if (entry->object.vm_object == NULL &&
3224 if (vm_map_lock_upgrade(map))
3226 entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
3227 atop(entry->end - entry->start));
3229 vm_map_lock_downgrade(map);
3233 * Return the object/offset from this entry. If the entry was
3234 * copy-on-write or empty, it has been fixed up.
3236 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
3237 *object = entry->object.vm_object;
3240 return (KERN_SUCCESS);
3246 * vm_map_lookup_locked:
3248 * Lookup the faulting address. A version of vm_map_lookup that returns
3249 * KERN_FAILURE instead of blocking on map lock or memory allocation.
3252 vm_map_lookup_locked(vm_map_t *var_map, /* IN/OUT */
3254 vm_prot_t fault_typea,
3255 vm_map_entry_t *out_entry, /* OUT */
3256 vm_object_t *object, /* OUT */
3257 vm_pindex_t *pindex, /* OUT */
3258 vm_prot_t *out_prot, /* OUT */
3259 boolean_t *wired) /* OUT */
3261 vm_map_entry_t entry;
3262 vm_map_t map = *var_map;
3264 vm_prot_t fault_type = fault_typea;
3267 * If the map has an interesting hint, try it before calling full
3268 * blown lookup routine.
3272 if (entry == NULL ||
3273 (vaddr < entry->start) || (vaddr >= entry->end)) {
3275 * Entry was either not a valid hint, or the vaddr was not
3276 * contained in the entry, so do a full lookup.
3278 if (!vm_map_lookup_entry(map, vaddr, out_entry))
3279 return (KERN_INVALID_ADDRESS);
3285 * Fail if the entry refers to a submap.
3287 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
3288 return (KERN_FAILURE);
3291 * Check whether this task is allowed to have this page.
3292 * Note the special case for MAP_ENTRY_COW
3293 * pages with an override. This is to implement a forced
3294 * COW for debuggers.
3296 if (fault_type & VM_PROT_OVERRIDE_WRITE)
3297 prot = entry->max_protection;
3299 prot = entry->protection;
3300 fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
3301 if ((fault_type & prot) != fault_type)
3302 return (KERN_PROTECTION_FAILURE);
3303 if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
3304 (entry->eflags & MAP_ENTRY_COW) &&
3305 (fault_type & VM_PROT_WRITE) &&
3306 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0)
3307 return (KERN_PROTECTION_FAILURE);
3310 * If this page is not pageable, we have to get it for all possible
3313 *wired = (entry->wired_count != 0);
3315 prot = fault_type = entry->protection;
3317 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3319 * Fail if the entry was copy-on-write for a write fault.
3321 if (fault_type & VM_PROT_WRITE)
3322 return (KERN_FAILURE);
3324 * We're attempting to read a copy-on-write page --
3325 * don't allow writes.
3327 prot &= ~VM_PROT_WRITE;
3331 * Fail if an object should be created.
3333 if (entry->object.vm_object == NULL && !map->system_map)
3334 return (KERN_FAILURE);
3337 * Return the object/offset from this entry. If the entry was
3338 * copy-on-write or empty, it has been fixed up.
3340 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
3341 *object = entry->object.vm_object;
3344 return (KERN_SUCCESS);
3348 * vm_map_lookup_done:
3350 * Releases locks acquired by a vm_map_lookup
3351 * (according to the handle returned by that lookup).
3354 vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry)
3357 * Unlock the main-level map
3359 vm_map_unlock_read(map);
3362 #include "opt_ddb.h"
3364 #include <sys/kernel.h>
3366 #include <ddb/ddb.h>
3369 * vm_map_print: [ debug ]
3371 DB_SHOW_COMMAND(map, vm_map_print)
3374 /* XXX convert args. */
3375 vm_map_t map = (vm_map_t)addr;
3376 boolean_t full = have_addr;
3378 vm_map_entry_t entry;
3380 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
3382 (void *)map->pmap, map->nentries, map->timestamp);
3385 if (!full && db_indent)
3389 for (entry = map->header.next; entry != &map->header;
3390 entry = entry->next) {
3391 db_iprintf("map entry %p: start=%p, end=%p\n",
3392 (void *)entry, (void *)entry->start, (void *)entry->end);
3395 static char *inheritance_name[4] =
3396 {"share", "copy", "none", "donate_copy"};
3398 db_iprintf(" prot=%x/%x/%s",
3400 entry->max_protection,
3401 inheritance_name[(int)(unsigned char)entry->inheritance]);
3402 if (entry->wired_count != 0)
3403 db_printf(", wired");
3405 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3406 db_printf(", share=%p, offset=0x%jx\n",
3407 (void *)entry->object.sub_map,
3408 (uintmax_t)entry->offset);
3410 if ((entry->prev == &map->header) ||
3411 (entry->prev->object.sub_map !=
3412 entry->object.sub_map)) {
3414 vm_map_print((db_expr_t)(intptr_t)
3415 entry->object.sub_map,
3416 full, 0, (char *)0);
3420 db_printf(", object=%p, offset=0x%jx",
3421 (void *)entry->object.vm_object,
3422 (uintmax_t)entry->offset);
3423 if (entry->eflags & MAP_ENTRY_COW)
3424 db_printf(", copy (%s)",
3425 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
3429 if ((entry->prev == &map->header) ||
3430 (entry->prev->object.vm_object !=
3431 entry->object.vm_object)) {
3433 vm_object_print((db_expr_t)(intptr_t)
3434 entry->object.vm_object,
3435 full, 0, (char *)0);
3447 DB_SHOW_COMMAND(procvm, procvm)
3452 p = (struct proc *) addr;
3457 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
3458 (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
3459 (void *)vmspace_pmap(p->p_vmspace));
3461 vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);