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)
161 mtx_init(&map_sleep_mtx, "vm map sleep mutex", NULL, MTX_DEF);
162 mapzone = uma_zcreate("MAP", sizeof(struct vm_map), NULL,
168 vm_map_zinit, vm_map_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
169 uma_prealloc(mapzone, MAX_KMAP);
170 kmapentzone = uma_zcreate("KMAP ENTRY", sizeof(struct vm_map_entry),
171 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
172 UMA_ZONE_MTXCLASS | UMA_ZONE_VM);
173 uma_prealloc(kmapentzone, MAX_KMAPENT);
174 mapentzone = uma_zcreate("MAP ENTRY", sizeof(struct vm_map_entry),
175 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
179 vmspace_zfini(void *mem, int size)
183 vm = (struct vmspace *)mem;
184 pmap_release(vmspace_pmap(vm));
185 vm_map_zfini(&vm->vm_map, sizeof(vm->vm_map));
189 vmspace_zinit(void *mem, int size, int flags)
193 vm = (struct vmspace *)mem;
195 (void)vm_map_zinit(&vm->vm_map, sizeof(vm->vm_map), flags);
196 pmap_pinit(vmspace_pmap(vm));
201 vm_map_zfini(void *mem, int size)
206 mtx_destroy(&map->system_mtx);
207 sx_destroy(&map->lock);
211 vm_map_zinit(void *mem, int size, int flags)
218 mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
219 sx_init(&map->lock, "user map");
225 vmspace_zdtor(void *mem, int size, void *arg)
229 vm = (struct vmspace *)mem;
231 vm_map_zdtor(&vm->vm_map, sizeof(vm->vm_map), arg);
234 vm_map_zdtor(void *mem, int size, void *arg)
239 KASSERT(map->nentries == 0,
240 ("map %p nentries == %d on free.",
241 map, map->nentries));
242 KASSERT(map->size == 0,
243 ("map %p size == %lu on free.",
244 map, (unsigned long)map->size));
246 #endif /* INVARIANTS */
249 * Allocate a vmspace structure, including a vm_map and pmap,
250 * and initialize those structures. The refcnt is set to 1.
253 vmspace_alloc(min, max)
254 vm_offset_t min, max;
258 vm = uma_zalloc(vmspace_zone, M_WAITOK);
259 CTR1(KTR_VM, "vmspace_alloc: %p", vm);
260 _vm_map_init(&vm->vm_map, min, max);
261 vm->vm_map.pmap = vmspace_pmap(vm); /* XXX */
277 uma_zone_set_obj(kmapentzone, &kmapentobj, lmin(cnt.v_page_count,
278 (VM_MAX_KERNEL_ADDRESS - KERNBASE) / PAGE_SIZE) / 8 +
279 maxproc * 2 + maxfiles);
280 vmspace_zone = uma_zcreate("VMSPACE", sizeof(struct vmspace), NULL,
286 vmspace_zinit, vmspace_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
290 vmspace_dofree(struct vmspace *vm)
292 CTR1(KTR_VM, "vmspace_free: %p", vm);
295 * Make sure any SysV shm is freed, it might not have been in
301 * Lock the map, to wait out all other references to it.
302 * Delete all of the mappings and pages they hold, then call
303 * the pmap module to reclaim anything left.
305 (void)vm_map_remove(&vm->vm_map, vm->vm_map.min_offset,
306 vm->vm_map.max_offset);
308 uma_zfree(vmspace_zone, vm);
312 vmspace_free(struct vmspace *vm)
316 if (vm->vm_refcnt == 0)
317 panic("vmspace_free: attempt to free already freed vmspace");
320 refcnt = vm->vm_refcnt;
321 while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1));
327 vmspace_exitfree(struct proc *p)
331 PROC_VMSPACE_LOCK(p);
334 PROC_VMSPACE_UNLOCK(p);
335 KASSERT(vm == &vmspace0, ("vmspace_exitfree: wrong vmspace"));
340 vmspace_exit(struct thread *td)
347 * Release user portion of address space.
348 * This releases references to vnodes,
349 * which could cause I/O if the file has been unlinked.
350 * Need to do this early enough that we can still sleep.
352 * The last exiting process to reach this point releases as
353 * much of the environment as it can. vmspace_dofree() is the
354 * slower fallback in case another process had a temporary
355 * reference to the vmspace.
360 atomic_add_int(&vmspace0.vm_refcnt, 1);
362 refcnt = vm->vm_refcnt;
363 if (refcnt > 1 && p->p_vmspace != &vmspace0) {
364 /* Switch now since other proc might free vmspace */
365 PROC_VMSPACE_LOCK(p);
366 p->p_vmspace = &vmspace0;
367 PROC_VMSPACE_UNLOCK(p);
370 } while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1));
372 if (p->p_vmspace != vm) {
373 /* vmspace not yet freed, switch back */
374 PROC_VMSPACE_LOCK(p);
376 PROC_VMSPACE_UNLOCK(p);
379 pmap_remove_pages(vmspace_pmap(vm));
380 /* Switch now since this proc will free vmspace */
381 PROC_VMSPACE_LOCK(p);
382 p->p_vmspace = &vmspace0;
383 PROC_VMSPACE_UNLOCK(p);
389 /* Acquire reference to vmspace owned by another process. */
392 vmspace_acquire_ref(struct proc *p)
397 PROC_VMSPACE_LOCK(p);
400 PROC_VMSPACE_UNLOCK(p);
404 refcnt = vm->vm_refcnt;
405 if (refcnt <= 0) { /* Avoid 0->1 transition */
406 PROC_VMSPACE_UNLOCK(p);
409 } while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt + 1));
410 if (vm != p->p_vmspace) {
411 PROC_VMSPACE_UNLOCK(p);
415 PROC_VMSPACE_UNLOCK(p);
420 _vm_map_lock(vm_map_t map, const char *file, int line)
424 _mtx_lock_flags(&map->system_mtx, 0, file, line);
426 _sx_xlock(&map->lock, file, line);
431 _vm_map_unlock(vm_map_t map, const char *file, int line)
435 _mtx_unlock_flags(&map->system_mtx, 0, file, line);
437 _sx_xunlock(&map->lock, file, line);
441 _vm_map_lock_read(vm_map_t map, const char *file, int line)
445 _mtx_lock_flags(&map->system_mtx, 0, file, line);
447 _sx_xlock(&map->lock, file, line);
451 _vm_map_unlock_read(vm_map_t map, const char *file, int line)
455 _mtx_unlock_flags(&map->system_mtx, 0, file, line);
457 _sx_xunlock(&map->lock, file, line);
461 _vm_map_trylock(vm_map_t map, const char *file, int line)
465 error = map->system_map ?
466 !_mtx_trylock(&map->system_mtx, 0, file, line) :
467 !_sx_try_xlock(&map->lock, file, line);
474 _vm_map_trylock_read(vm_map_t map, const char *file, int line)
478 error = map->system_map ?
479 !_mtx_trylock(&map->system_mtx, 0, file, line) :
480 !_sx_try_xlock(&map->lock, file, line);
485 _vm_map_lock_upgrade(vm_map_t map, const char *file, int line)
489 if (map->system_map) {
490 _mtx_assert(&map->system_mtx, MA_OWNED, file, line);
492 _sx_assert(&map->lock, SX_XLOCKED, file, line);
499 _vm_map_lock_downgrade(vm_map_t map, const char *file, int line)
503 if (map->system_map) {
504 _mtx_assert(&map->system_mtx, MA_OWNED, file, line);
506 _sx_assert(&map->lock, SX_XLOCKED, file, line);
511 * vm_map_unlock_and_wait:
514 vm_map_unlock_and_wait(vm_map_t map, boolean_t user_wait)
517 mtx_lock(&map_sleep_mtx);
519 return (msleep(&map->root, &map_sleep_mtx, PDROP | PVM, "vmmaps", 0));
526 vm_map_wakeup(vm_map_t map)
530 * Acquire and release map_sleep_mtx to prevent a wakeup()
531 * from being performed (and lost) between the vm_map_unlock()
532 * and the msleep() in vm_map_unlock_and_wait().
534 mtx_lock(&map_sleep_mtx);
535 mtx_unlock(&map_sleep_mtx);
540 vmspace_resident_count(struct vmspace *vmspace)
542 return pmap_resident_count(vmspace_pmap(vmspace));
546 vmspace_wired_count(struct vmspace *vmspace)
548 return pmap_wired_count(vmspace_pmap(vmspace));
554 * Creates and returns a new empty VM map with
555 * the given physical map structure, and having
556 * the given lower and upper address bounds.
559 vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max)
563 result = uma_zalloc(mapzone, M_WAITOK);
564 CTR1(KTR_VM, "vm_map_create: %p", result);
565 _vm_map_init(result, min, max);
571 * Initialize an existing vm_map structure
572 * such as that in the vmspace structure.
573 * The pmap is set elsewhere.
576 _vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max)
579 map->header.next = map->header.prev = &map->header;
580 map->needs_wakeup = FALSE;
582 map->min_offset = min;
583 map->max_offset = max;
590 vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max)
592 _vm_map_init(map, min, max);
593 mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
594 sx_init(&map->lock, "user map");
598 * vm_map_entry_dispose: [ internal use only ]
600 * Inverse of vm_map_entry_create.
603 vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry)
605 uma_zfree(map->system_map ? kmapentzone : mapentzone, entry);
609 * vm_map_entry_create: [ internal use only ]
611 * Allocates a VM map entry for insertion.
612 * No entry fields are filled in.
614 static vm_map_entry_t
615 vm_map_entry_create(vm_map_t map)
617 vm_map_entry_t new_entry;
620 new_entry = uma_zalloc(kmapentzone, M_NOWAIT);
622 new_entry = uma_zalloc(mapentzone, M_WAITOK);
623 if (new_entry == NULL)
624 panic("vm_map_entry_create: kernel resources exhausted");
629 * vm_map_entry_set_behavior:
631 * Set the expected access behavior, either normal, random, or
635 vm_map_entry_set_behavior(vm_map_entry_t entry, u_char behavior)
637 entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) |
638 (behavior & MAP_ENTRY_BEHAV_MASK);
642 * vm_map_entry_set_max_free:
644 * Set the max_free field in a vm_map_entry.
647 vm_map_entry_set_max_free(vm_map_entry_t entry)
650 entry->max_free = entry->adj_free;
651 if (entry->left != NULL && entry->left->max_free > entry->max_free)
652 entry->max_free = entry->left->max_free;
653 if (entry->right != NULL && entry->right->max_free > entry->max_free)
654 entry->max_free = entry->right->max_free;
658 * vm_map_entry_splay:
660 * The Sleator and Tarjan top-down splay algorithm with the
661 * following variation. Max_free must be computed bottom-up, so
662 * on the downward pass, maintain the left and right spines in
663 * reverse order. Then, make a second pass up each side to fix
664 * the pointers and compute max_free. The time bound is O(log n)
667 * The new root is the vm_map_entry containing "addr", or else an
668 * adjacent entry (lower or higher) if addr is not in the tree.
670 * The map must be locked, and leaves it so.
672 * Returns: the new root.
674 static vm_map_entry_t
675 vm_map_entry_splay(vm_offset_t addr, vm_map_entry_t root)
677 vm_map_entry_t llist, rlist;
678 vm_map_entry_t ltree, rtree;
681 /* Special case of empty tree. */
686 * Pass One: Splay down the tree until we find addr or a NULL
687 * pointer where addr would go. llist and rlist are the two
688 * sides in reverse order (bottom-up), with llist linked by
689 * the right pointer and rlist linked by the left pointer in
690 * the vm_map_entry. Wait until Pass Two to set max_free on
696 /* root is never NULL in here. */
697 if (addr < root->start) {
701 if (addr < y->start && y->left != NULL) {
702 /* Rotate right and put y on rlist. */
703 root->left = y->right;
705 vm_map_entry_set_max_free(root);
710 /* Put root on rlist. */
717 if (addr < root->end || y == NULL)
719 if (addr >= y->end && y->right != NULL) {
720 /* Rotate left and put y on llist. */
721 root->right = y->left;
723 vm_map_entry_set_max_free(root);
728 /* Put root on llist. */
737 * Pass Two: Walk back up the two spines, flip the pointers
738 * and set max_free. The subtrees of the root go at the
739 * bottom of llist and rlist.
742 while (llist != NULL) {
744 llist->right = ltree;
745 vm_map_entry_set_max_free(llist);
750 while (rlist != NULL) {
753 vm_map_entry_set_max_free(rlist);
759 * Final assembly: add ltree and rtree as subtrees of root.
763 vm_map_entry_set_max_free(root);
769 * vm_map_entry_{un,}link:
771 * Insert/remove entries from maps.
774 vm_map_entry_link(vm_map_t map,
775 vm_map_entry_t after_where,
776 vm_map_entry_t entry)
780 "vm_map_entry_link: map %p, nentries %d, entry %p, after %p", map,
781 map->nentries, entry, after_where);
783 entry->prev = after_where;
784 entry->next = after_where->next;
785 entry->next->prev = entry;
786 after_where->next = entry;
788 if (after_where != &map->header) {
789 if (after_where != map->root)
790 vm_map_entry_splay(after_where->start, map->root);
791 entry->right = after_where->right;
792 entry->left = after_where;
793 after_where->right = NULL;
794 after_where->adj_free = entry->start - after_where->end;
795 vm_map_entry_set_max_free(after_where);
797 entry->right = map->root;
800 entry->adj_free = (entry->next == &map->header ? map->max_offset :
801 entry->next->start) - entry->end;
802 vm_map_entry_set_max_free(entry);
807 vm_map_entry_unlink(vm_map_t map,
808 vm_map_entry_t entry)
810 vm_map_entry_t next, prev, root;
812 if (entry != map->root)
813 vm_map_entry_splay(entry->start, map->root);
814 if (entry->left == NULL)
817 root = vm_map_entry_splay(entry->start, entry->left);
818 root->right = entry->right;
819 root->adj_free = (entry->next == &map->header ? map->max_offset :
820 entry->next->start) - root->end;
821 vm_map_entry_set_max_free(root);
830 CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map,
831 map->nentries, entry);
835 * vm_map_entry_resize_free:
837 * Recompute the amount of free space following a vm_map_entry
838 * and propagate that value up the tree. Call this function after
839 * resizing a map entry in-place, that is, without a call to
840 * vm_map_entry_link() or _unlink().
842 * The map must be locked, and leaves it so.
845 vm_map_entry_resize_free(vm_map_t map, vm_map_entry_t entry)
849 * Using splay trees without parent pointers, propagating
850 * max_free up the tree is done by moving the entry to the
851 * root and making the change there.
853 if (entry != map->root)
854 map->root = vm_map_entry_splay(entry->start, map->root);
856 entry->adj_free = (entry->next == &map->header ? map->max_offset :
857 entry->next->start) - entry->end;
858 vm_map_entry_set_max_free(entry);
862 * vm_map_lookup_entry: [ internal use only ]
864 * Finds the map entry containing (or
865 * immediately preceding) the specified address
866 * in the given map; the entry is returned
867 * in the "entry" parameter. The boolean
868 * result indicates whether the address is
869 * actually contained in the map.
875 vm_map_entry_t *entry) /* OUT */
879 cur = vm_map_entry_splay(address, map->root);
881 *entry = &map->header;
885 if (address >= cur->start) {
887 if (cur->end > address)
898 * Inserts the given whole VM object into the target
899 * map at the specified address range. The object's
900 * size should match that of the address range.
902 * Requires that the map be locked, and leaves it so.
904 * If object is non-NULL, ref count must be bumped by caller
905 * prior to making call to account for the new entry.
908 vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
909 vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max,
912 vm_map_entry_t new_entry;
913 vm_map_entry_t prev_entry;
914 vm_map_entry_t temp_entry;
915 vm_eflags_t protoeflags;
918 * Check that the start and end points are not bogus.
920 if ((start < map->min_offset) || (end > map->max_offset) ||
922 return (KERN_INVALID_ADDRESS);
925 * Find the entry prior to the proposed starting address; if it's part
926 * of an existing entry, this range is bogus.
928 if (vm_map_lookup_entry(map, start, &temp_entry))
929 return (KERN_NO_SPACE);
931 prev_entry = temp_entry;
934 * Assert that the next entry doesn't overlap the end point.
936 if ((prev_entry->next != &map->header) &&
937 (prev_entry->next->start < end))
938 return (KERN_NO_SPACE);
942 if (cow & MAP_COPY_ON_WRITE)
943 protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
945 if (cow & MAP_NOFAULT) {
946 protoeflags |= MAP_ENTRY_NOFAULT;
948 KASSERT(object == NULL,
949 ("vm_map_insert: paradoxical MAP_NOFAULT request"));
951 if (cow & MAP_DISABLE_SYNCER)
952 protoeflags |= MAP_ENTRY_NOSYNC;
953 if (cow & MAP_DISABLE_COREDUMP)
954 protoeflags |= MAP_ENTRY_NOCOREDUMP;
956 if (object != NULL) {
958 * OBJ_ONEMAPPING must be cleared unless this mapping
959 * is trivially proven to be the only mapping for any
960 * of the object's pages. (Object granularity
961 * reference counting is insufficient to recognize
962 * aliases with precision.)
964 VM_OBJECT_LOCK(object);
965 if (object->ref_count > 1 || object->shadow_count != 0)
966 vm_object_clear_flag(object, OBJ_ONEMAPPING);
967 VM_OBJECT_UNLOCK(object);
969 else if ((prev_entry != &map->header) &&
970 (prev_entry->eflags == protoeflags) &&
971 (prev_entry->end == start) &&
972 (prev_entry->wired_count == 0) &&
973 ((prev_entry->object.vm_object == NULL) ||
974 vm_object_coalesce(prev_entry->object.vm_object,
976 (vm_size_t)(prev_entry->end - prev_entry->start),
977 (vm_size_t)(end - prev_entry->end)))) {
979 * We were able to extend the object. Determine if we
980 * can extend the previous map entry to include the
983 if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
984 (prev_entry->protection == prot) &&
985 (prev_entry->max_protection == max)) {
986 map->size += (end - prev_entry->end);
987 prev_entry->end = end;
988 vm_map_entry_resize_free(map, prev_entry);
989 vm_map_simplify_entry(map, prev_entry);
990 return (KERN_SUCCESS);
994 * If we can extend the object but cannot extend the
995 * map entry, we have to create a new map entry. We
996 * must bump the ref count on the extended object to
997 * account for it. object may be NULL.
999 object = prev_entry->object.vm_object;
1000 offset = prev_entry->offset +
1001 (prev_entry->end - prev_entry->start);
1002 vm_object_reference(object);
1006 * NOTE: if conditionals fail, object can be NULL here. This occurs
1007 * in things like the buffer map where we manage kva but do not manage
1012 * Create a new entry
1014 new_entry = vm_map_entry_create(map);
1015 new_entry->start = start;
1016 new_entry->end = end;
1018 new_entry->eflags = protoeflags;
1019 new_entry->object.vm_object = object;
1020 new_entry->offset = offset;
1021 new_entry->avail_ssize = 0;
1023 new_entry->inheritance = VM_INHERIT_DEFAULT;
1024 new_entry->protection = prot;
1025 new_entry->max_protection = max;
1026 new_entry->wired_count = 0;
1029 * Insert the new entry into the list
1031 vm_map_entry_link(map, prev_entry, new_entry);
1032 map->size += new_entry->end - new_entry->start;
1036 * Temporarily removed to avoid MAP_STACK panic, due to
1037 * MAP_STACK being a huge hack. Will be added back in
1038 * when MAP_STACK (and the user stack mapping) is fixed.
1041 * It may be possible to simplify the entry
1043 vm_map_simplify_entry(map, new_entry);
1046 if (cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) {
1047 vm_map_pmap_enter(map, start, prot,
1048 object, OFF_TO_IDX(offset), end - start,
1049 cow & MAP_PREFAULT_PARTIAL);
1052 return (KERN_SUCCESS);
1058 * Find the first fit (lowest VM address) for "length" free bytes
1059 * beginning at address >= start in the given map.
1061 * In a vm_map_entry, "adj_free" is the amount of free space
1062 * adjacent (higher address) to this entry, and "max_free" is the
1063 * maximum amount of contiguous free space in its subtree. This
1064 * allows finding a free region in one path down the tree, so
1065 * O(log n) amortized with splay trees.
1067 * The map must be locked, and leaves it so.
1069 * Returns: 0 on success, and starting address in *addr,
1070 * 1 if insufficient space.
1073 vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length,
1074 vm_offset_t *addr) /* OUT */
1076 vm_map_entry_t entry;
1077 vm_offset_t end, st;
1080 * Request must fit within min/max VM address and must avoid
1083 if (start < map->min_offset)
1084 start = map->min_offset;
1085 if (start + length > map->max_offset || start + length < start)
1088 /* Empty tree means wide open address space. */
1089 if (map->root == NULL) {
1095 * After splay, if start comes before root node, then there
1096 * must be a gap from start to the root.
1098 map->root = vm_map_entry_splay(start, map->root);
1099 if (start + length <= map->root->start) {
1105 * Root is the last node that might begin its gap before
1106 * start, and this is the last comparison where address
1107 * wrap might be a problem.
1109 st = (start > map->root->end) ? start : map->root->end;
1110 if (length <= map->root->end + map->root->adj_free - st) {
1115 /* With max_free, can immediately tell if no solution. */
1116 entry = map->root->right;
1117 if (entry == NULL || length > entry->max_free)
1121 * Search the right subtree in the order: left subtree, root,
1122 * right subtree (first fit). The previous splay implies that
1123 * all regions in the right subtree have addresses > start.
1125 while (entry != NULL) {
1126 if (entry->left != NULL && entry->left->max_free >= length)
1127 entry = entry->left;
1128 else if (entry->adj_free >= length) {
1132 entry = entry->right;
1135 /* Can't get here, so panic if we do. */
1136 panic("vm_map_findspace: max_free corrupt");
1139 /* Expand the kernel pmap, if necessary. */
1140 if (map == kernel_map) {
1141 end = round_page(*addr + length);
1142 if (end > kernel_vm_end)
1143 pmap_growkernel(end);
1149 * vm_map_find finds an unallocated region in the target address
1150 * map with the given length. The search is defined to be
1151 * first-fit from the specified address; the region found is
1152 * returned in the same parameter.
1154 * If object is non-NULL, ref count must be bumped by caller
1155 * prior to making call to account for the new entry.
1158 vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1159 vm_offset_t *addr, /* IN/OUT */
1160 vm_size_t length, boolean_t find_space, vm_prot_t prot,
1161 vm_prot_t max, int cow)
1169 if (vm_map_findspace(map, start, length, addr)) {
1171 return (KERN_NO_SPACE);
1175 result = vm_map_insert(map, object, offset,
1176 start, start + length, prot, max, cow);
1182 * vm_map_simplify_entry:
1184 * Simplify the given map entry by merging with either neighbor. This
1185 * routine also has the ability to merge with both neighbors.
1187 * The map must be locked.
1189 * This routine guarentees that the passed entry remains valid (though
1190 * possibly extended). When merging, this routine may delete one or
1194 vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry)
1196 vm_map_entry_t next, prev;
1197 vm_size_t prevsize, esize;
1199 if (entry->eflags & (MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP))
1203 if (prev != &map->header) {
1204 prevsize = prev->end - prev->start;
1205 if ( (prev->end == entry->start) &&
1206 (prev->object.vm_object == entry->object.vm_object) &&
1207 (!prev->object.vm_object ||
1208 (prev->offset + prevsize == entry->offset)) &&
1209 (prev->eflags == entry->eflags) &&
1210 (prev->protection == entry->protection) &&
1211 (prev->max_protection == entry->max_protection) &&
1212 (prev->inheritance == entry->inheritance) &&
1213 (prev->wired_count == entry->wired_count)) {
1214 vm_map_entry_unlink(map, prev);
1215 entry->start = prev->start;
1216 entry->offset = prev->offset;
1217 if (entry->prev != &map->header)
1218 vm_map_entry_resize_free(map, entry->prev);
1219 if (prev->object.vm_object)
1220 vm_object_deallocate(prev->object.vm_object);
1221 vm_map_entry_dispose(map, prev);
1226 if (next != &map->header) {
1227 esize = entry->end - entry->start;
1228 if ((entry->end == next->start) &&
1229 (next->object.vm_object == entry->object.vm_object) &&
1230 (!entry->object.vm_object ||
1231 (entry->offset + esize == next->offset)) &&
1232 (next->eflags == entry->eflags) &&
1233 (next->protection == entry->protection) &&
1234 (next->max_protection == entry->max_protection) &&
1235 (next->inheritance == entry->inheritance) &&
1236 (next->wired_count == entry->wired_count)) {
1237 vm_map_entry_unlink(map, next);
1238 entry->end = next->end;
1239 vm_map_entry_resize_free(map, entry);
1240 if (next->object.vm_object)
1241 vm_object_deallocate(next->object.vm_object);
1242 vm_map_entry_dispose(map, next);
1247 * vm_map_clip_start: [ internal use only ]
1249 * Asserts that the given entry begins at or after
1250 * the specified address; if necessary,
1251 * it splits the entry into two.
1253 #define vm_map_clip_start(map, entry, startaddr) \
1255 if (startaddr > entry->start) \
1256 _vm_map_clip_start(map, entry, startaddr); \
1260 * This routine is called only when it is known that
1261 * the entry must be split.
1264 _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start)
1266 vm_map_entry_t new_entry;
1269 * Split off the front portion -- note that we must insert the new
1270 * entry BEFORE this one, so that this entry has the specified
1273 vm_map_simplify_entry(map, entry);
1276 * If there is no object backing this entry, we might as well create
1277 * one now. If we defer it, an object can get created after the map
1278 * is clipped, and individual objects will be created for the split-up
1279 * map. This is a bit of a hack, but is also about the best place to
1280 * put this improvement.
1282 if (entry->object.vm_object == NULL && !map->system_map) {
1284 object = vm_object_allocate(OBJT_DEFAULT,
1285 atop(entry->end - entry->start));
1286 entry->object.vm_object = object;
1290 new_entry = vm_map_entry_create(map);
1291 *new_entry = *entry;
1293 new_entry->end = start;
1294 entry->offset += (start - entry->start);
1295 entry->start = start;
1297 vm_map_entry_link(map, entry->prev, new_entry);
1299 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1300 vm_object_reference(new_entry->object.vm_object);
1305 * vm_map_clip_end: [ internal use only ]
1307 * Asserts that the given entry ends at or before
1308 * the specified address; if necessary,
1309 * it splits the entry into two.
1311 #define vm_map_clip_end(map, entry, endaddr) \
1313 if ((endaddr) < (entry->end)) \
1314 _vm_map_clip_end((map), (entry), (endaddr)); \
1318 * This routine is called only when it is known that
1319 * the entry must be split.
1322 _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end)
1324 vm_map_entry_t new_entry;
1327 * If there is no object backing this entry, we might as well create
1328 * one now. If we defer it, an object can get created after the map
1329 * is clipped, and individual objects will be created for the split-up
1330 * map. This is a bit of a hack, but is also about the best place to
1331 * put this improvement.
1333 if (entry->object.vm_object == NULL && !map->system_map) {
1335 object = vm_object_allocate(OBJT_DEFAULT,
1336 atop(entry->end - entry->start));
1337 entry->object.vm_object = object;
1342 * Create a new entry and insert it AFTER the specified entry
1344 new_entry = vm_map_entry_create(map);
1345 *new_entry = *entry;
1347 new_entry->start = entry->end = end;
1348 new_entry->offset += (end - entry->start);
1350 vm_map_entry_link(map, entry, new_entry);
1352 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1353 vm_object_reference(new_entry->object.vm_object);
1358 * VM_MAP_RANGE_CHECK: [ internal use only ]
1360 * Asserts that the starting and ending region
1361 * addresses fall within the valid range of the map.
1363 #define VM_MAP_RANGE_CHECK(map, start, end) \
1365 if (start < vm_map_min(map)) \
1366 start = vm_map_min(map); \
1367 if (end > vm_map_max(map)) \
1368 end = vm_map_max(map); \
1374 * vm_map_submap: [ kernel use only ]
1376 * Mark the given range as handled by a subordinate map.
1378 * This range must have been created with vm_map_find,
1379 * and no other operations may have been performed on this
1380 * range prior to calling vm_map_submap.
1382 * Only a limited number of operations can be performed
1383 * within this rage after calling vm_map_submap:
1385 * [Don't try vm_map_copy!]
1387 * To remove a submapping, one must first remove the
1388 * range from the superior map, and then destroy the
1389 * submap (if desired). [Better yet, don't try it.]
1398 vm_map_entry_t entry;
1399 int result = KERN_INVALID_ARGUMENT;
1403 VM_MAP_RANGE_CHECK(map, start, end);
1405 if (vm_map_lookup_entry(map, start, &entry)) {
1406 vm_map_clip_start(map, entry, start);
1408 entry = entry->next;
1410 vm_map_clip_end(map, entry, end);
1412 if ((entry->start == start) && (entry->end == end) &&
1413 ((entry->eflags & MAP_ENTRY_COW) == 0) &&
1414 (entry->object.vm_object == NULL)) {
1415 entry->object.sub_map = submap;
1416 entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
1417 result = KERN_SUCCESS;
1425 * The maximum number of pages to map
1427 #define MAX_INIT_PT 96
1430 * vm_map_pmap_enter:
1432 * Preload read-only mappings for the given object into the specified
1433 * map. This eliminates the soft faults on process startup and
1434 * immediately after an mmap(2).
1437 vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
1438 vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags)
1440 vm_offset_t start, tmpidx;
1442 vm_page_t p, p_start;
1443 boolean_t are_queues_locked;
1445 if ((prot & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 || object == NULL)
1447 VM_OBJECT_LOCK(object);
1448 if (object->type == OBJT_DEVICE) {
1449 pmap_object_init_pt(map->pmap, addr, object, pindex, size);
1455 if (object->type != OBJT_VNODE ||
1456 ((flags & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
1457 (object->resident_page_count > MAX_INIT_PT))) {
1461 if (psize + pindex > object->size) {
1462 if (object->size < pindex)
1464 psize = object->size - pindex;
1467 are_queues_locked = FALSE;
1471 if ((p = TAILQ_FIRST(&object->memq)) != NULL) {
1472 if (p->pindex < pindex) {
1473 p = vm_page_splay(pindex, object->root);
1474 if ((object->root = p)->pindex < pindex)
1475 p = TAILQ_NEXT(p, listq);
1479 * Assert: the variable p is either (1) the page with the
1480 * least pindex greater than or equal to the parameter pindex
1484 p != NULL && (tmpidx = p->pindex - pindex) < psize;
1485 p = TAILQ_NEXT(p, listq)) {
1487 * don't allow an madvise to blow away our really
1488 * free pages allocating pv entries.
1490 if ((flags & MAP_PREFAULT_MADVISE) &&
1491 cnt.v_free_count < cnt.v_free_reserved) {
1495 if ((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL &&
1497 (p->flags & PG_FICTITIOUS) == 0) {
1498 if (p_start == NULL) {
1499 start = addr + ptoa(tmpidx);
1502 if (!are_queues_locked) {
1503 are_queues_locked = TRUE;
1504 vm_page_lock_queues();
1506 if (VM_PAGE_INQUEUE1(p, PQ_CACHE))
1507 vm_page_deactivate(p);
1508 } else if (p_start != NULL) {
1509 pmap_enter_object(map->pmap, start, addr +
1510 ptoa(tmpidx), p_start, prot);
1514 if (p_start != NULL)
1515 pmap_enter_object(map->pmap, start, addr + ptoa(psize),
1517 if (are_queues_locked)
1518 vm_page_unlock_queues();
1520 VM_OBJECT_UNLOCK(object);
1526 * Sets the protection of the specified address
1527 * region in the target map. If "set_max" is
1528 * specified, the maximum protection is to be set;
1529 * otherwise, only the current protection is affected.
1532 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
1533 vm_prot_t new_prot, boolean_t set_max)
1535 vm_map_entry_t current;
1536 vm_map_entry_t entry;
1540 VM_MAP_RANGE_CHECK(map, start, end);
1542 if (vm_map_lookup_entry(map, start, &entry)) {
1543 vm_map_clip_start(map, entry, start);
1545 entry = entry->next;
1549 * Make a first pass to check for protection violations.
1552 while ((current != &map->header) && (current->start < end)) {
1553 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1555 return (KERN_INVALID_ARGUMENT);
1557 if ((new_prot & current->max_protection) != new_prot) {
1559 return (KERN_PROTECTION_FAILURE);
1561 current = current->next;
1565 * Go back and fix up protections. [Note that clipping is not
1566 * necessary the second time.]
1569 while ((current != &map->header) && (current->start < end)) {
1572 vm_map_clip_end(map, current, end);
1574 old_prot = current->protection;
1576 current->protection =
1577 (current->max_protection = new_prot) &
1580 current->protection = new_prot;
1583 * Update physical map if necessary. Worry about copy-on-write
1584 * here -- CHECK THIS XXX
1586 if (current->protection != old_prot) {
1587 #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1589 pmap_protect(map->pmap, current->start,
1591 current->protection & MASK(current));
1594 vm_map_simplify_entry(map, current);
1595 current = current->next;
1598 return (KERN_SUCCESS);
1604 * This routine traverses a processes map handling the madvise
1605 * system call. Advisories are classified as either those effecting
1606 * the vm_map_entry structure, or those effecting the underlying
1616 vm_map_entry_t current, entry;
1620 * Some madvise calls directly modify the vm_map_entry, in which case
1621 * we need to use an exclusive lock on the map and we need to perform
1622 * various clipping operations. Otherwise we only need a read-lock
1627 case MADV_SEQUENTIAL:
1639 vm_map_lock_read(map);
1642 return (KERN_INVALID_ARGUMENT);
1646 * Locate starting entry and clip if necessary.
1648 VM_MAP_RANGE_CHECK(map, start, end);
1650 if (vm_map_lookup_entry(map, start, &entry)) {
1652 vm_map_clip_start(map, entry, start);
1654 entry = entry->next;
1659 * madvise behaviors that are implemented in the vm_map_entry.
1661 * We clip the vm_map_entry so that behavioral changes are
1662 * limited to the specified address range.
1664 for (current = entry;
1665 (current != &map->header) && (current->start < end);
1666 current = current->next
1668 if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1671 vm_map_clip_end(map, current, end);
1675 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
1677 case MADV_SEQUENTIAL:
1678 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
1681 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
1684 current->eflags |= MAP_ENTRY_NOSYNC;
1687 current->eflags &= ~MAP_ENTRY_NOSYNC;
1690 current->eflags |= MAP_ENTRY_NOCOREDUMP;
1693 current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
1698 vm_map_simplify_entry(map, current);
1706 * madvise behaviors that are implemented in the underlying
1709 * Since we don't clip the vm_map_entry, we have to clip
1710 * the vm_object pindex and count.
1712 for (current = entry;
1713 (current != &map->header) && (current->start < end);
1714 current = current->next
1716 vm_offset_t useStart;
1718 if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1721 pindex = OFF_TO_IDX(current->offset);
1722 count = atop(current->end - current->start);
1723 useStart = current->start;
1725 if (current->start < start) {
1726 pindex += atop(start - current->start);
1727 count -= atop(start - current->start);
1730 if (current->end > end)
1731 count -= atop(current->end - end);
1736 vm_object_madvise(current->object.vm_object,
1737 pindex, count, behav);
1738 if (behav == MADV_WILLNEED) {
1739 vm_map_pmap_enter(map,
1741 current->protection,
1742 current->object.vm_object,
1744 (count << PAGE_SHIFT),
1745 MAP_PREFAULT_MADVISE
1749 vm_map_unlock_read(map);
1758 * Sets the inheritance of the specified address
1759 * range in the target map. Inheritance
1760 * affects how the map will be shared with
1761 * child maps at the time of vm_map_fork.
1764 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
1765 vm_inherit_t new_inheritance)
1767 vm_map_entry_t entry;
1768 vm_map_entry_t temp_entry;
1770 switch (new_inheritance) {
1771 case VM_INHERIT_NONE:
1772 case VM_INHERIT_COPY:
1773 case VM_INHERIT_SHARE:
1776 return (KERN_INVALID_ARGUMENT);
1779 VM_MAP_RANGE_CHECK(map, start, end);
1780 if (vm_map_lookup_entry(map, start, &temp_entry)) {
1782 vm_map_clip_start(map, entry, start);
1784 entry = temp_entry->next;
1785 while ((entry != &map->header) && (entry->start < end)) {
1786 vm_map_clip_end(map, entry, end);
1787 entry->inheritance = new_inheritance;
1788 vm_map_simplify_entry(map, entry);
1789 entry = entry->next;
1792 return (KERN_SUCCESS);
1798 * Implements both kernel and user unwiring.
1801 vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
1804 vm_map_entry_t entry, first_entry, tmp_entry;
1805 vm_offset_t saved_start;
1806 unsigned int last_timestamp;
1808 boolean_t need_wakeup, result, user_unwire;
1810 user_unwire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
1812 VM_MAP_RANGE_CHECK(map, start, end);
1813 if (!vm_map_lookup_entry(map, start, &first_entry)) {
1814 if (flags & VM_MAP_WIRE_HOLESOK)
1815 first_entry = first_entry->next;
1818 return (KERN_INVALID_ADDRESS);
1821 last_timestamp = map->timestamp;
1822 entry = first_entry;
1823 while (entry != &map->header && entry->start < end) {
1824 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1826 * We have not yet clipped the entry.
1828 saved_start = (start >= entry->start) ? start :
1830 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1831 if (vm_map_unlock_and_wait(map, user_unwire)) {
1833 * Allow interruption of user unwiring?
1837 if (last_timestamp+1 != map->timestamp) {
1839 * Look again for the entry because the map was
1840 * modified while it was unlocked.
1841 * Specifically, the entry may have been
1842 * clipped, merged, or deleted.
1844 if (!vm_map_lookup_entry(map, saved_start,
1846 if (flags & VM_MAP_WIRE_HOLESOK)
1847 tmp_entry = tmp_entry->next;
1849 if (saved_start == start) {
1851 * First_entry has been deleted.
1854 return (KERN_INVALID_ADDRESS);
1857 rv = KERN_INVALID_ADDRESS;
1861 if (entry == first_entry)
1862 first_entry = tmp_entry;
1867 last_timestamp = map->timestamp;
1870 vm_map_clip_start(map, entry, start);
1871 vm_map_clip_end(map, entry, end);
1873 * Mark the entry in case the map lock is released. (See
1876 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
1878 * Check the map for holes in the specified region.
1879 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
1881 if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
1882 (entry->end < end && (entry->next == &map->header ||
1883 entry->next->start > entry->end))) {
1885 rv = KERN_INVALID_ADDRESS;
1889 * If system unwiring, require that the entry is system wired.
1892 vm_map_entry_system_wired_count(entry) == 0) {
1894 rv = KERN_INVALID_ARGUMENT;
1897 entry = entry->next;
1901 need_wakeup = FALSE;
1902 if (first_entry == NULL) {
1903 result = vm_map_lookup_entry(map, start, &first_entry);
1904 if (!result && (flags & VM_MAP_WIRE_HOLESOK))
1905 first_entry = first_entry->next;
1907 KASSERT(result, ("vm_map_unwire: lookup failed"));
1909 entry = first_entry;
1910 while (entry != &map->header && entry->start < end) {
1911 if (rv == KERN_SUCCESS && (!user_unwire ||
1912 (entry->eflags & MAP_ENTRY_USER_WIRED))) {
1914 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
1915 entry->wired_count--;
1916 if (entry->wired_count == 0) {
1918 * Retain the map lock.
1920 vm_fault_unwire(map, entry->start, entry->end,
1921 entry->object.vm_object != NULL &&
1922 entry->object.vm_object->type == OBJT_DEVICE);
1925 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
1926 ("vm_map_unwire: in-transition flag missing"));
1927 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
1928 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
1929 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
1932 vm_map_simplify_entry(map, entry);
1933 entry = entry->next;
1944 * Implements both kernel and user wiring.
1947 vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
1950 vm_map_entry_t entry, first_entry, tmp_entry;
1951 vm_offset_t saved_end, saved_start;
1952 unsigned int last_timestamp;
1954 boolean_t fictitious, need_wakeup, result, user_wire;
1956 user_wire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
1958 VM_MAP_RANGE_CHECK(map, start, end);
1959 if (!vm_map_lookup_entry(map, start, &first_entry)) {
1960 if (flags & VM_MAP_WIRE_HOLESOK)
1961 first_entry = first_entry->next;
1964 return (KERN_INVALID_ADDRESS);
1967 last_timestamp = map->timestamp;
1968 entry = first_entry;
1969 while (entry != &map->header && entry->start < end) {
1970 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1972 * We have not yet clipped the entry.
1974 saved_start = (start >= entry->start) ? start :
1976 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1977 if (vm_map_unlock_and_wait(map, user_wire)) {
1979 * Allow interruption of user wiring?
1983 if (last_timestamp + 1 != map->timestamp) {
1985 * Look again for the entry because the map was
1986 * modified while it was unlocked.
1987 * Specifically, the entry may have been
1988 * clipped, merged, or deleted.
1990 if (!vm_map_lookup_entry(map, saved_start,
1992 if (flags & VM_MAP_WIRE_HOLESOK)
1993 tmp_entry = tmp_entry->next;
1995 if (saved_start == start) {
1997 * first_entry has been deleted.
2000 return (KERN_INVALID_ADDRESS);
2003 rv = KERN_INVALID_ADDRESS;
2007 if (entry == first_entry)
2008 first_entry = tmp_entry;
2013 last_timestamp = map->timestamp;
2016 vm_map_clip_start(map, entry, start);
2017 vm_map_clip_end(map, entry, end);
2019 * Mark the entry in case the map lock is released. (See
2022 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
2026 if (entry->wired_count == 0) {
2027 entry->wired_count++;
2028 saved_start = entry->start;
2029 saved_end = entry->end;
2030 fictitious = entry->object.vm_object != NULL &&
2031 entry->object.vm_object->type == OBJT_DEVICE;
2033 * Release the map lock, relying on the in-transition
2037 rv = vm_fault_wire(map, saved_start, saved_end,
2038 user_wire, fictitious);
2040 if (last_timestamp + 1 != map->timestamp) {
2042 * Look again for the entry because the map was
2043 * modified while it was unlocked. The entry
2044 * may have been clipped, but NOT merged or
2047 result = vm_map_lookup_entry(map, saved_start,
2049 KASSERT(result, ("vm_map_wire: lookup failed"));
2050 if (entry == first_entry)
2051 first_entry = tmp_entry;
2055 while (entry->end < saved_end) {
2056 if (rv != KERN_SUCCESS) {
2057 KASSERT(entry->wired_count == 1,
2058 ("vm_map_wire: bad count"));
2059 entry->wired_count = -1;
2061 entry = entry->next;
2064 last_timestamp = map->timestamp;
2065 if (rv != KERN_SUCCESS) {
2066 KASSERT(entry->wired_count == 1,
2067 ("vm_map_wire: bad count"));
2069 * Assign an out-of-range value to represent
2070 * the failure to wire this entry.
2072 entry->wired_count = -1;
2076 } else if (!user_wire ||
2077 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
2078 entry->wired_count++;
2081 * Check the map for holes in the specified region.
2082 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
2084 if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
2085 (entry->end < end && (entry->next == &map->header ||
2086 entry->next->start > entry->end))) {
2088 rv = KERN_INVALID_ADDRESS;
2091 entry = entry->next;
2095 need_wakeup = FALSE;
2096 if (first_entry == NULL) {
2097 result = vm_map_lookup_entry(map, start, &first_entry);
2098 if (!result && (flags & VM_MAP_WIRE_HOLESOK))
2099 first_entry = first_entry->next;
2101 KASSERT(result, ("vm_map_wire: lookup failed"));
2103 entry = first_entry;
2104 while (entry != &map->header && entry->start < end) {
2105 if (rv == KERN_SUCCESS) {
2107 entry->eflags |= MAP_ENTRY_USER_WIRED;
2108 } else if (entry->wired_count == -1) {
2110 * Wiring failed on this entry. Thus, unwiring is
2113 entry->wired_count = 0;
2116 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0)
2117 entry->wired_count--;
2118 if (entry->wired_count == 0) {
2120 * Retain the map lock.
2122 vm_fault_unwire(map, entry->start, entry->end,
2123 entry->object.vm_object != NULL &&
2124 entry->object.vm_object->type == OBJT_DEVICE);
2127 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
2128 ("vm_map_wire: in-transition flag missing"));
2129 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
2130 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
2131 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
2134 vm_map_simplify_entry(map, entry);
2135 entry = entry->next;
2146 * Push any dirty cached pages in the address range to their pager.
2147 * If syncio is TRUE, dirty pages are written synchronously.
2148 * If invalidate is TRUE, any cached pages are freed as well.
2150 * If the size of the region from start to end is zero, we are
2151 * supposed to flush all modified pages within the region containing
2152 * start. Unfortunately, a region can be split or coalesced with
2153 * neighboring regions, making it difficult to determine what the
2154 * original region was. Therefore, we approximate this requirement by
2155 * flushing the current region containing start.
2157 * Returns an error if any part of the specified range is not mapped.
2165 boolean_t invalidate)
2167 vm_map_entry_t current;
2168 vm_map_entry_t entry;
2171 vm_ooffset_t offset;
2173 vm_map_lock_read(map);
2174 VM_MAP_RANGE_CHECK(map, start, end);
2175 if (!vm_map_lookup_entry(map, start, &entry)) {
2176 vm_map_unlock_read(map);
2177 return (KERN_INVALID_ADDRESS);
2178 } else if (start == end) {
2179 start = entry->start;
2183 * Make a first pass to check for user-wired memory and holes.
2185 for (current = entry; current->start < end; current = current->next) {
2186 if (invalidate && (current->eflags & MAP_ENTRY_USER_WIRED)) {
2187 vm_map_unlock_read(map);
2188 return (KERN_INVALID_ARGUMENT);
2190 if (end > current->end &&
2191 (current->next == &map->header ||
2192 current->end != current->next->start)) {
2193 vm_map_unlock_read(map);
2194 return (KERN_INVALID_ADDRESS);
2199 pmap_remove(map->pmap, start, end);
2202 * Make a second pass, cleaning/uncaching pages from the indicated
2205 for (current = entry; current->start < end; current = current->next) {
2206 offset = current->offset + (start - current->start);
2207 size = (end <= current->end ? end : current->end) - start;
2208 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
2210 vm_map_entry_t tentry;
2213 smap = current->object.sub_map;
2214 vm_map_lock_read(smap);
2215 (void) vm_map_lookup_entry(smap, offset, &tentry);
2216 tsize = tentry->end - offset;
2219 object = tentry->object.vm_object;
2220 offset = tentry->offset + (offset - tentry->start);
2221 vm_map_unlock_read(smap);
2223 object = current->object.vm_object;
2225 vm_object_sync(object, offset, size, syncio, invalidate);
2229 vm_map_unlock_read(map);
2230 return (KERN_SUCCESS);
2234 * vm_map_entry_unwire: [ internal use only ]
2236 * Make the region specified by this entry pageable.
2238 * The map in question should be locked.
2239 * [This is the reason for this routine's existence.]
2242 vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
2244 vm_fault_unwire(map, entry->start, entry->end,
2245 entry->object.vm_object != NULL &&
2246 entry->object.vm_object->type == OBJT_DEVICE);
2247 entry->wired_count = 0;
2251 * vm_map_entry_delete: [ internal use only ]
2253 * Deallocate the given entry from the target map.
2256 vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry)
2259 vm_pindex_t offidxstart, offidxend, count;
2261 vm_map_entry_unlink(map, entry);
2262 map->size -= entry->end - entry->start;
2264 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
2265 (object = entry->object.vm_object) != NULL) {
2266 count = OFF_TO_IDX(entry->end - entry->start);
2267 offidxstart = OFF_TO_IDX(entry->offset);
2268 offidxend = offidxstart + count;
2269 VM_OBJECT_LOCK(object);
2270 if (object->ref_count != 1 &&
2271 ((object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING ||
2272 object == kernel_object || object == kmem_object) &&
2273 (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2274 vm_object_collapse(object);
2275 vm_object_page_remove(object, offidxstart, offidxend, FALSE);
2276 if (object->type == OBJT_SWAP)
2277 swap_pager_freespace(object, offidxstart, count);
2278 if (offidxend >= object->size &&
2279 offidxstart < object->size)
2280 object->size = offidxstart;
2282 VM_OBJECT_UNLOCK(object);
2283 vm_object_deallocate(object);
2286 vm_map_entry_dispose(map, entry);
2290 * vm_map_delete: [ internal use only ]
2292 * Deallocates the given address range from the target
2296 vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end)
2298 vm_map_entry_t entry;
2299 vm_map_entry_t first_entry;
2302 * Find the start of the region, and clip it
2304 if (!vm_map_lookup_entry(map, start, &first_entry))
2305 entry = first_entry->next;
2307 entry = first_entry;
2308 vm_map_clip_start(map, entry, start);
2312 * Step through all entries in this region
2314 while ((entry != &map->header) && (entry->start < end)) {
2315 vm_map_entry_t next;
2318 * Wait for wiring or unwiring of an entry to complete.
2319 * Also wait for any system wirings to disappear on
2322 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 ||
2323 (vm_map_pmap(map) != kernel_pmap &&
2324 vm_map_entry_system_wired_count(entry) != 0)) {
2325 unsigned int last_timestamp;
2326 vm_offset_t saved_start;
2327 vm_map_entry_t tmp_entry;
2329 saved_start = entry->start;
2330 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2331 last_timestamp = map->timestamp;
2332 (void) vm_map_unlock_and_wait(map, FALSE);
2334 if (last_timestamp + 1 != map->timestamp) {
2336 * Look again for the entry because the map was
2337 * modified while it was unlocked.
2338 * Specifically, the entry may have been
2339 * clipped, merged, or deleted.
2341 if (!vm_map_lookup_entry(map, saved_start,
2343 entry = tmp_entry->next;
2346 vm_map_clip_start(map, entry,
2352 vm_map_clip_end(map, entry, end);
2357 * Unwire before removing addresses from the pmap; otherwise,
2358 * unwiring will put the entries back in the pmap.
2360 if (entry->wired_count != 0) {
2361 vm_map_entry_unwire(map, entry);
2364 pmap_remove(map->pmap, entry->start, entry->end);
2367 * Delete the entry (which may delete the object) only after
2368 * removing all pmap entries pointing to its pages.
2369 * (Otherwise, its page frames may be reallocated, and any
2370 * modify bits will be set in the wrong object!)
2372 vm_map_entry_delete(map, entry);
2375 return (KERN_SUCCESS);
2381 * Remove the given address range from the target map.
2382 * This is the exported form of vm_map_delete.
2385 vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
2390 VM_MAP_RANGE_CHECK(map, start, end);
2391 result = vm_map_delete(map, start, end);
2397 * vm_map_check_protection:
2399 * Assert that the target map allows the specified privilege on the
2400 * entire address region given. The entire region must be allocated.
2402 * WARNING! This code does not and should not check whether the
2403 * contents of the region is accessible. For example a smaller file
2404 * might be mapped into a larger address space.
2406 * NOTE! This code is also called by munmap().
2408 * The map must be locked. A read lock is sufficient.
2411 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
2412 vm_prot_t protection)
2414 vm_map_entry_t entry;
2415 vm_map_entry_t tmp_entry;
2417 if (!vm_map_lookup_entry(map, start, &tmp_entry))
2421 while (start < end) {
2422 if (entry == &map->header)
2427 if (start < entry->start)
2430 * Check protection associated with entry.
2432 if ((entry->protection & protection) != protection)
2434 /* go to next entry */
2436 entry = entry->next;
2442 * vm_map_copy_entry:
2444 * Copies the contents of the source entry to the destination
2445 * entry. The entries *must* be aligned properly.
2451 vm_map_entry_t src_entry,
2452 vm_map_entry_t dst_entry)
2454 vm_object_t src_object;
2456 if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
2459 if (src_entry->wired_count == 0) {
2462 * If the source entry is marked needs_copy, it is already
2465 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
2466 pmap_protect(src_map->pmap,
2469 src_entry->protection & ~VM_PROT_WRITE);
2473 * Make a copy of the object.
2475 if ((src_object = src_entry->object.vm_object) != NULL) {
2476 VM_OBJECT_LOCK(src_object);
2477 if ((src_object->handle == NULL) &&
2478 (src_object->type == OBJT_DEFAULT ||
2479 src_object->type == OBJT_SWAP)) {
2480 vm_object_collapse(src_object);
2481 if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
2482 vm_object_split(src_entry);
2483 src_object = src_entry->object.vm_object;
2486 vm_object_reference_locked(src_object);
2487 vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
2488 VM_OBJECT_UNLOCK(src_object);
2489 dst_entry->object.vm_object = src_object;
2490 src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2491 dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2492 dst_entry->offset = src_entry->offset;
2494 dst_entry->object.vm_object = NULL;
2495 dst_entry->offset = 0;
2498 pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
2499 dst_entry->end - dst_entry->start, src_entry->start);
2502 * Of course, wired down pages can't be set copy-on-write.
2503 * Cause wired pages to be copied into the new map by
2504 * simulating faults (the new pages are pageable)
2506 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry);
2511 * vmspace_map_entry_forked:
2512 * Update the newly-forked vmspace each time a map entry is inherited
2513 * or copied. The values for vm_dsize and vm_tsize are approximate
2514 * (and mostly-obsolete ideas in the face of mmap(2) et al.)
2517 vmspace_map_entry_forked(const struct vmspace *vm1, struct vmspace *vm2,
2518 vm_map_entry_t entry)
2520 vm_size_t entrysize;
2523 entrysize = entry->end - entry->start;
2524 vm2->vm_map.size += entrysize;
2525 if (entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP)) {
2526 vm2->vm_ssize += btoc(entrysize);
2527 } else if (entry->start >= (vm_offset_t)vm1->vm_daddr &&
2528 entry->start < (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)) {
2529 newend = MIN(entry->end,
2530 (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize));
2531 vm2->vm_dsize += btoc(newend - entry->start);
2532 } else if (entry->start >= (vm_offset_t)vm1->vm_taddr &&
2533 entry->start < (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)) {
2534 newend = MIN(entry->end,
2535 (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize));
2536 vm2->vm_tsize += btoc(newend - entry->start);
2542 * Create a new process vmspace structure and vm_map
2543 * based on those of an existing process. The new map
2544 * is based on the old map, according to the inheritance
2545 * values on the regions in that map.
2547 * XXX It might be worth coalescing the entries added to the new vmspace.
2549 * The source map must not be locked.
2552 vmspace_fork(struct vmspace *vm1)
2554 struct vmspace *vm2;
2555 vm_map_t old_map = &vm1->vm_map;
2557 vm_map_entry_t old_entry;
2558 vm_map_entry_t new_entry;
2561 vm_map_lock(old_map);
2563 vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
2564 vm2->vm_taddr = vm1->vm_taddr;
2565 vm2->vm_daddr = vm1->vm_daddr;
2566 vm2->vm_maxsaddr = vm1->vm_maxsaddr;
2567 new_map = &vm2->vm_map; /* XXX */
2568 new_map->timestamp = 1;
2570 /* Do not inherit the MAP_WIREFUTURE property. */
2571 if ((new_map->flags & MAP_WIREFUTURE) == MAP_WIREFUTURE)
2572 new_map->flags &= ~MAP_WIREFUTURE;
2574 old_entry = old_map->header.next;
2576 while (old_entry != &old_map->header) {
2577 if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2578 panic("vm_map_fork: encountered a submap");
2580 switch (old_entry->inheritance) {
2581 case VM_INHERIT_NONE:
2584 case VM_INHERIT_SHARE:
2586 * Clone the entry, creating the shared object if necessary.
2588 object = old_entry->object.vm_object;
2589 if (object == NULL) {
2590 object = vm_object_allocate(OBJT_DEFAULT,
2591 atop(old_entry->end - old_entry->start));
2592 old_entry->object.vm_object = object;
2593 old_entry->offset = 0;
2597 * Add the reference before calling vm_object_shadow
2598 * to insure that a shadow object is created.
2600 vm_object_reference(object);
2601 if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
2602 vm_object_shadow(&old_entry->object.vm_object,
2604 atop(old_entry->end - old_entry->start));
2605 old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
2606 /* Transfer the second reference too. */
2607 vm_object_reference(
2608 old_entry->object.vm_object);
2609 vm_object_deallocate(object);
2610 object = old_entry->object.vm_object;
2612 VM_OBJECT_LOCK(object);
2613 vm_object_clear_flag(object, OBJ_ONEMAPPING);
2614 VM_OBJECT_UNLOCK(object);
2617 * Clone the entry, referencing the shared object.
2619 new_entry = vm_map_entry_create(new_map);
2620 *new_entry = *old_entry;
2621 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2622 new_entry->wired_count = 0;
2625 * Insert the entry into the new map -- we know we're
2626 * inserting at the end of the new map.
2628 vm_map_entry_link(new_map, new_map->header.prev,
2630 vmspace_map_entry_forked(vm1, vm2, new_entry);
2633 * Update the physical map
2635 pmap_copy(new_map->pmap, old_map->pmap,
2637 (old_entry->end - old_entry->start),
2641 case VM_INHERIT_COPY:
2643 * Clone the entry and link into the map.
2645 new_entry = vm_map_entry_create(new_map);
2646 *new_entry = *old_entry;
2647 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2648 new_entry->wired_count = 0;
2649 new_entry->object.vm_object = NULL;
2650 vm_map_entry_link(new_map, new_map->header.prev,
2652 vmspace_map_entry_forked(vm1, vm2, new_entry);
2653 vm_map_copy_entry(old_map, new_map, old_entry,
2657 old_entry = old_entry->next;
2660 vm_map_unlock(old_map);
2666 vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
2667 vm_prot_t prot, vm_prot_t max, int cow)
2669 vm_map_entry_t new_entry, prev_entry;
2670 vm_offset_t bot, top;
2671 vm_size_t init_ssize;
2676 * The stack orientation is piggybacked with the cow argument.
2677 * Extract it into orient and mask the cow argument so that we
2678 * don't pass it around further.
2679 * NOTE: We explicitly allow bi-directional stacks.
2681 orient = cow & (MAP_STACK_GROWS_DOWN|MAP_STACK_GROWS_UP);
2683 KASSERT(orient != 0, ("No stack grow direction"));
2685 if (addrbos < vm_map_min(map) || addrbos > map->max_offset)
2686 return (KERN_NO_SPACE);
2688 init_ssize = (max_ssize < sgrowsiz) ? max_ssize : sgrowsiz;
2690 PROC_LOCK(curthread->td_proc);
2691 vmemlim = lim_cur(curthread->td_proc, RLIMIT_VMEM);
2692 PROC_UNLOCK(curthread->td_proc);
2696 /* If addr is already mapped, no go */
2697 if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
2699 return (KERN_NO_SPACE);
2702 /* If we would blow our VMEM resource limit, no go */
2703 if (map->size + init_ssize > vmemlim) {
2705 return (KERN_NO_SPACE);
2709 * If we can't accomodate max_ssize in the current mapping, no go.
2710 * However, we need to be aware that subsequent user mappings might
2711 * map into the space we have reserved for stack, and currently this
2712 * space is not protected.
2714 * Hopefully we will at least detect this condition when we try to
2717 if ((prev_entry->next != &map->header) &&
2718 (prev_entry->next->start < addrbos + max_ssize)) {
2720 return (KERN_NO_SPACE);
2724 * We initially map a stack of only init_ssize. We will grow as
2725 * needed later. Depending on the orientation of the stack (i.e.
2726 * the grow direction) we either map at the top of the range, the
2727 * bottom of the range or in the middle.
2729 * Note: we would normally expect prot and max to be VM_PROT_ALL,
2730 * and cow to be 0. Possibly we should eliminate these as input
2731 * parameters, and just pass these values here in the insert call.
2733 if (orient == MAP_STACK_GROWS_DOWN)
2734 bot = addrbos + max_ssize - init_ssize;
2735 else if (orient == MAP_STACK_GROWS_UP)
2738 bot = round_page(addrbos + max_ssize/2 - init_ssize/2);
2739 top = bot + init_ssize;
2740 rv = vm_map_insert(map, NULL, 0, bot, top, prot, max, cow);
2742 /* Now set the avail_ssize amount. */
2743 if (rv == KERN_SUCCESS) {
2744 if (prev_entry != &map->header)
2745 vm_map_clip_end(map, prev_entry, bot);
2746 new_entry = prev_entry->next;
2747 if (new_entry->end != top || new_entry->start != bot)
2748 panic("Bad entry start/end for new stack entry");
2750 new_entry->avail_ssize = max_ssize - init_ssize;
2751 if (orient & MAP_STACK_GROWS_DOWN)
2752 new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
2753 if (orient & MAP_STACK_GROWS_UP)
2754 new_entry->eflags |= MAP_ENTRY_GROWS_UP;
2761 /* Attempts to grow a vm stack entry. Returns KERN_SUCCESS if the
2762 * desired address is already mapped, or if we successfully grow
2763 * the stack. Also returns KERN_SUCCESS if addr is outside the
2764 * stack range (this is strange, but preserves compatibility with
2765 * the grow function in vm_machdep.c).
2768 vm_map_growstack(struct proc *p, vm_offset_t addr)
2770 vm_map_entry_t next_entry, prev_entry;
2771 vm_map_entry_t new_entry, stack_entry;
2772 struct vmspace *vm = p->p_vmspace;
2773 vm_map_t map = &vm->vm_map;
2775 size_t grow_amount, max_grow;
2776 rlim_t stacklim, vmemlim;
2777 int is_procstack, rv;
2781 stacklim = lim_cur(p, RLIMIT_STACK);
2782 vmemlim = lim_cur(p, RLIMIT_VMEM);
2785 vm_map_lock_read(map);
2787 /* If addr is already in the entry range, no need to grow.*/
2788 if (vm_map_lookup_entry(map, addr, &prev_entry)) {
2789 vm_map_unlock_read(map);
2790 return (KERN_SUCCESS);
2793 next_entry = prev_entry->next;
2794 if (!(prev_entry->eflags & MAP_ENTRY_GROWS_UP)) {
2796 * This entry does not grow upwards. Since the address lies
2797 * beyond this entry, the next entry (if one exists) has to
2798 * be a downward growable entry. The entry list header is
2799 * never a growable entry, so it suffices to check the flags.
2801 if (!(next_entry->eflags & MAP_ENTRY_GROWS_DOWN)) {
2802 vm_map_unlock_read(map);
2803 return (KERN_SUCCESS);
2805 stack_entry = next_entry;
2808 * This entry grows upward. If the next entry does not at
2809 * least grow downwards, this is the entry we need to grow.
2810 * otherwise we have two possible choices and we have to
2813 if (next_entry->eflags & MAP_ENTRY_GROWS_DOWN) {
2815 * We have two choices; grow the entry closest to
2816 * the address to minimize the amount of growth.
2818 if (addr - prev_entry->end <= next_entry->start - addr)
2819 stack_entry = prev_entry;
2821 stack_entry = next_entry;
2823 stack_entry = prev_entry;
2826 if (stack_entry == next_entry) {
2827 KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_DOWN, ("foo"));
2828 KASSERT(addr < stack_entry->start, ("foo"));
2829 end = (prev_entry != &map->header) ? prev_entry->end :
2830 stack_entry->start - stack_entry->avail_ssize;
2831 grow_amount = roundup(stack_entry->start - addr, PAGE_SIZE);
2832 max_grow = stack_entry->start - end;
2834 KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_UP, ("foo"));
2835 KASSERT(addr >= stack_entry->end, ("foo"));
2836 end = (next_entry != &map->header) ? next_entry->start :
2837 stack_entry->end + stack_entry->avail_ssize;
2838 grow_amount = roundup(addr + 1 - stack_entry->end, PAGE_SIZE);
2839 max_grow = end - stack_entry->end;
2842 if (grow_amount > stack_entry->avail_ssize) {
2843 vm_map_unlock_read(map);
2844 return (KERN_NO_SPACE);
2848 * If there is no longer enough space between the entries nogo, and
2849 * adjust the available space. Note: this should only happen if the
2850 * user has mapped into the stack area after the stack was created,
2851 * and is probably an error.
2853 * This also effectively destroys any guard page the user might have
2854 * intended by limiting the stack size.
2856 if (grow_amount > max_grow) {
2857 if (vm_map_lock_upgrade(map))
2860 stack_entry->avail_ssize = max_grow;
2863 return (KERN_NO_SPACE);
2866 is_procstack = (addr >= (vm_offset_t)vm->vm_maxsaddr) ? 1 : 0;
2869 * If this is the main process stack, see if we're over the stack
2872 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
2873 vm_map_unlock_read(map);
2874 return (KERN_NO_SPACE);
2877 /* Round up the grow amount modulo SGROWSIZ */
2878 grow_amount = roundup (grow_amount, sgrowsiz);
2879 if (grow_amount > stack_entry->avail_ssize)
2880 grow_amount = stack_entry->avail_ssize;
2881 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
2882 grow_amount = stacklim - ctob(vm->vm_ssize);
2885 /* If we would blow our VMEM resource limit, no go */
2886 if (map->size + grow_amount > vmemlim) {
2887 vm_map_unlock_read(map);
2888 return (KERN_NO_SPACE);
2891 if (vm_map_lock_upgrade(map))
2894 if (stack_entry == next_entry) {
2898 /* Get the preliminary new entry start value */
2899 addr = stack_entry->start - grow_amount;
2902 * If this puts us into the previous entry, cut back our
2903 * growth to the available space. Also, see the note above.
2906 stack_entry->avail_ssize = max_grow;
2910 rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start,
2911 p->p_sysent->sv_stackprot, VM_PROT_ALL, 0);
2913 /* Adjust the available stack space by the amount we grew. */
2914 if (rv == KERN_SUCCESS) {
2915 if (prev_entry != &map->header)
2916 vm_map_clip_end(map, prev_entry, addr);
2917 new_entry = prev_entry->next;
2918 KASSERT(new_entry == stack_entry->prev, ("foo"));
2919 KASSERT(new_entry->end == stack_entry->start, ("foo"));
2920 KASSERT(new_entry->start == addr, ("foo"));
2921 grow_amount = new_entry->end - new_entry->start;
2922 new_entry->avail_ssize = stack_entry->avail_ssize -
2924 stack_entry->eflags &= ~MAP_ENTRY_GROWS_DOWN;
2925 new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
2931 addr = stack_entry->end + grow_amount;
2934 * If this puts us into the next entry, cut back our growth
2935 * to the available space. Also, see the note above.
2938 stack_entry->avail_ssize = end - stack_entry->end;
2942 grow_amount = addr - stack_entry->end;
2944 /* Grow the underlying object if applicable. */
2945 if (stack_entry->object.vm_object == NULL ||
2946 vm_object_coalesce(stack_entry->object.vm_object,
2947 stack_entry->offset,
2948 (vm_size_t)(stack_entry->end - stack_entry->start),
2949 (vm_size_t)grow_amount)) {
2950 map->size += (addr - stack_entry->end);
2951 /* Update the current entry. */
2952 stack_entry->end = addr;
2953 stack_entry->avail_ssize -= grow_amount;
2954 vm_map_entry_resize_free(map, stack_entry);
2957 if (next_entry != &map->header)
2958 vm_map_clip_start(map, next_entry, addr);
2963 if (rv == KERN_SUCCESS && is_procstack)
2964 vm->vm_ssize += btoc(grow_amount);
2969 * Heed the MAP_WIREFUTURE flag if it was set for this process.
2971 if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE)) {
2973 (stack_entry == next_entry) ? addr : addr - grow_amount,
2974 (stack_entry == next_entry) ? stack_entry->start : addr,
2975 (p->p_flag & P_SYSTEM)
2976 ? VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES
2977 : VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
2984 * Unshare the specified VM space for exec. If other processes are
2985 * mapped to it, then create a new one. The new vmspace is null.
2988 vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser)
2990 struct vmspace *oldvmspace = p->p_vmspace;
2991 struct vmspace *newvmspace;
2993 newvmspace = vmspace_alloc(minuser, maxuser);
2994 newvmspace->vm_swrss = oldvmspace->vm_swrss;
2996 * This code is written like this for prototype purposes. The
2997 * goal is to avoid running down the vmspace here, but let the
2998 * other process's that are still using the vmspace to finally
2999 * run it down. Even though there is little or no chance of blocking
3000 * here, it is a good idea to keep this form for future mods.
3002 PROC_VMSPACE_LOCK(p);
3003 p->p_vmspace = newvmspace;
3004 PROC_VMSPACE_UNLOCK(p);
3005 if (p == curthread->td_proc) /* XXXKSE ? */
3006 pmap_activate(curthread);
3007 vmspace_free(oldvmspace);
3011 * Unshare the specified VM space for forcing COW. This
3012 * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
3015 vmspace_unshare(struct proc *p)
3017 struct vmspace *oldvmspace = p->p_vmspace;
3018 struct vmspace *newvmspace;
3020 if (oldvmspace->vm_refcnt == 1)
3022 newvmspace = vmspace_fork(oldvmspace);
3023 PROC_VMSPACE_LOCK(p);
3024 p->p_vmspace = newvmspace;
3025 PROC_VMSPACE_UNLOCK(p);
3026 if (p == curthread->td_proc) /* XXXKSE ? */
3027 pmap_activate(curthread);
3028 vmspace_free(oldvmspace);
3034 * Finds the VM object, offset, and
3035 * protection for a given virtual address in the
3036 * specified map, assuming a page fault of the
3039 * Leaves the map in question locked for read; return
3040 * values are guaranteed until a vm_map_lookup_done
3041 * call is performed. Note that the map argument
3042 * is in/out; the returned map must be used in
3043 * the call to vm_map_lookup_done.
3045 * A handle (out_entry) is returned for use in
3046 * vm_map_lookup_done, to make that fast.
3048 * If a lookup is requested with "write protection"
3049 * specified, the map may be changed to perform virtual
3050 * copying operations, although the data referenced will
3054 vm_map_lookup(vm_map_t *var_map, /* IN/OUT */
3056 vm_prot_t fault_typea,
3057 vm_map_entry_t *out_entry, /* OUT */
3058 vm_object_t *object, /* OUT */
3059 vm_pindex_t *pindex, /* OUT */
3060 vm_prot_t *out_prot, /* OUT */
3061 boolean_t *wired) /* OUT */
3063 vm_map_entry_t entry;
3064 vm_map_t map = *var_map;
3066 vm_prot_t fault_type = fault_typea;
3070 * Lookup the faulting address.
3073 vm_map_lock_read(map);
3074 #define RETURN(why) \
3076 vm_map_unlock_read(map); \
3081 * If the map has an interesting hint, try it before calling full
3082 * blown lookup routine.
3086 if (entry == NULL ||
3087 (vaddr < entry->start) || (vaddr >= entry->end)) {
3089 * Entry was either not a valid hint, or the vaddr was not
3090 * contained in the entry, so do a full lookup.
3092 if (!vm_map_lookup_entry(map, vaddr, out_entry))
3093 RETURN(KERN_INVALID_ADDRESS);
3101 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3102 vm_map_t old_map = map;
3104 *var_map = map = entry->object.sub_map;
3105 vm_map_unlock_read(old_map);
3110 * Check whether this task is allowed to have this page.
3111 * Note the special case for MAP_ENTRY_COW
3112 * pages with an override. This is to implement a forced
3113 * COW for debuggers.
3115 if (fault_type & VM_PROT_OVERRIDE_WRITE)
3116 prot = entry->max_protection;
3118 prot = entry->protection;
3119 fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
3120 if ((fault_type & prot) != fault_type) {
3121 RETURN(KERN_PROTECTION_FAILURE);
3123 if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
3124 (entry->eflags & MAP_ENTRY_COW) &&
3125 (fault_type & VM_PROT_WRITE) &&
3126 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
3127 RETURN(KERN_PROTECTION_FAILURE);
3131 * If this page is not pageable, we have to get it for all possible
3134 *wired = (entry->wired_count != 0);
3136 prot = fault_type = entry->protection;
3139 * If the entry was copy-on-write, we either ...
3141 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3143 * If we want to write the page, we may as well handle that
3144 * now since we've got the map locked.
3146 * If we don't need to write the page, we just demote the
3147 * permissions allowed.
3149 if (fault_type & VM_PROT_WRITE) {
3151 * Make a new object, and place it in the object
3152 * chain. Note that no new references have appeared
3153 * -- one just moved from the map to the new
3156 if (vm_map_lock_upgrade(map))
3160 &entry->object.vm_object,
3162 atop(entry->end - entry->start));
3163 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
3165 vm_map_lock_downgrade(map);
3168 * We're attempting to read a copy-on-write page --
3169 * don't allow writes.
3171 prot &= ~VM_PROT_WRITE;
3176 * Create an object if necessary.
3178 if (entry->object.vm_object == NULL &&
3180 if (vm_map_lock_upgrade(map))
3182 entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
3183 atop(entry->end - entry->start));
3185 vm_map_lock_downgrade(map);
3189 * Return the object/offset from this entry. If the entry was
3190 * copy-on-write or empty, it has been fixed up.
3192 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
3193 *object = entry->object.vm_object;
3196 return (KERN_SUCCESS);
3202 * vm_map_lookup_locked:
3204 * Lookup the faulting address. A version of vm_map_lookup that returns
3205 * KERN_FAILURE instead of blocking on map lock or memory allocation.
3208 vm_map_lookup_locked(vm_map_t *var_map, /* IN/OUT */
3210 vm_prot_t fault_typea,
3211 vm_map_entry_t *out_entry, /* OUT */
3212 vm_object_t *object, /* OUT */
3213 vm_pindex_t *pindex, /* OUT */
3214 vm_prot_t *out_prot, /* OUT */
3215 boolean_t *wired) /* OUT */
3217 vm_map_entry_t entry;
3218 vm_map_t map = *var_map;
3220 vm_prot_t fault_type = fault_typea;
3223 * If the map has an interesting hint, try it before calling full
3224 * blown lookup routine.
3228 if (entry == NULL ||
3229 (vaddr < entry->start) || (vaddr >= entry->end)) {
3231 * Entry was either not a valid hint, or the vaddr was not
3232 * contained in the entry, so do a full lookup.
3234 if (!vm_map_lookup_entry(map, vaddr, out_entry))
3235 return (KERN_INVALID_ADDRESS);
3241 * Fail if the entry refers to a submap.
3243 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
3244 return (KERN_FAILURE);
3247 * Check whether this task is allowed to have this page.
3248 * Note the special case for MAP_ENTRY_COW
3249 * pages with an override. This is to implement a forced
3250 * COW for debuggers.
3252 if (fault_type & VM_PROT_OVERRIDE_WRITE)
3253 prot = entry->max_protection;
3255 prot = entry->protection;
3256 fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
3257 if ((fault_type & prot) != fault_type)
3258 return (KERN_PROTECTION_FAILURE);
3259 if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
3260 (entry->eflags & MAP_ENTRY_COW) &&
3261 (fault_type & VM_PROT_WRITE) &&
3262 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0)
3263 return (KERN_PROTECTION_FAILURE);
3266 * If this page is not pageable, we have to get it for all possible
3269 *wired = (entry->wired_count != 0);
3271 prot = fault_type = entry->protection;
3273 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3275 * Fail if the entry was copy-on-write for a write fault.
3277 if (fault_type & VM_PROT_WRITE)
3278 return (KERN_FAILURE);
3280 * We're attempting to read a copy-on-write page --
3281 * don't allow writes.
3283 prot &= ~VM_PROT_WRITE;
3287 * Fail if an object should be created.
3289 if (entry->object.vm_object == NULL && !map->system_map)
3290 return (KERN_FAILURE);
3293 * Return the object/offset from this entry. If the entry was
3294 * copy-on-write or empty, it has been fixed up.
3296 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
3297 *object = entry->object.vm_object;
3300 return (KERN_SUCCESS);
3304 * vm_map_lookup_done:
3306 * Releases locks acquired by a vm_map_lookup
3307 * (according to the handle returned by that lookup).
3310 vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry)
3313 * Unlock the main-level map
3315 vm_map_unlock_read(map);
3318 #include "opt_ddb.h"
3320 #include <sys/kernel.h>
3322 #include <ddb/ddb.h>
3325 * vm_map_print: [ debug ]
3327 DB_SHOW_COMMAND(map, vm_map_print)
3330 /* XXX convert args. */
3331 vm_map_t map = (vm_map_t)addr;
3332 boolean_t full = have_addr;
3334 vm_map_entry_t entry;
3336 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
3338 (void *)map->pmap, map->nentries, map->timestamp);
3341 if (!full && db_indent)
3345 for (entry = map->header.next; entry != &map->header;
3346 entry = entry->next) {
3347 db_iprintf("map entry %p: start=%p, end=%p\n",
3348 (void *)entry, (void *)entry->start, (void *)entry->end);
3351 static char *inheritance_name[4] =
3352 {"share", "copy", "none", "donate_copy"};
3354 db_iprintf(" prot=%x/%x/%s",
3356 entry->max_protection,
3357 inheritance_name[(int)(unsigned char)entry->inheritance]);
3358 if (entry->wired_count != 0)
3359 db_printf(", wired");
3361 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3362 db_printf(", share=%p, offset=0x%jx\n",
3363 (void *)entry->object.sub_map,
3364 (uintmax_t)entry->offset);
3366 if ((entry->prev == &map->header) ||
3367 (entry->prev->object.sub_map !=
3368 entry->object.sub_map)) {
3370 vm_map_print((db_expr_t)(intptr_t)
3371 entry->object.sub_map,
3372 full, 0, (char *)0);
3376 db_printf(", object=%p, offset=0x%jx",
3377 (void *)entry->object.vm_object,
3378 (uintmax_t)entry->offset);
3379 if (entry->eflags & MAP_ENTRY_COW)
3380 db_printf(", copy (%s)",
3381 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
3385 if ((entry->prev == &map->header) ||
3386 (entry->prev->object.vm_object !=
3387 entry->object.vm_object)) {
3389 vm_object_print((db_expr_t)(intptr_t)
3390 entry->object.vm_object,
3391 full, 0, (char *)0);
3403 DB_SHOW_COMMAND(procvm, procvm)
3408 p = (struct proc *) addr;
3413 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
3414 (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
3415 (void *)vmspace_pmap(p->p_vmspace));
3417 vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);