2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 2003-2017 The DragonFly Project. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * The Mach Operating System project at Carnegie-Mellon University.
9 * This code is derived from software contributed to The DragonFly Project
10 * by Matthew Dillon <dillon@backplane.com>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * from: @(#)vm_map.c 8.3 (Berkeley) 1/12/94
39 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
40 * All rights reserved.
42 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
44 * Permission to use, copy, modify and distribute this software and
45 * its documentation is hereby granted, provided that both the copyright
46 * notice and this permission notice appear in all copies of the
47 * software, derivative works or modified versions, and any portions
48 * thereof, and that both notices appear in supporting documentation.
50 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
51 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
52 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
54 * Carnegie Mellon requests users of this software to return to
56 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
57 * School of Computer Science
58 * Carnegie Mellon University
59 * Pittsburgh PA 15213-3890
61 * any improvements or extensions that they make and grant Carnegie the
62 * rights to redistribute these changes.
64 * $FreeBSD: src/sys/vm/vm_map.c,v 1.187.2.19 2003/05/27 00:47:02 alc Exp $
68 * Virtual memory mapping module.
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/kernel.h>
75 #include <sys/serialize.h>
77 #include <sys/vmmeter.h>
79 #include <sys/vnode.h>
80 #include <sys/resourcevar.h>
83 #include <sys/malloc.h>
84 #include <sys/objcache.h>
85 #include <sys/kern_syscall.h>
88 #include <vm/vm_param.h>
90 #include <vm/vm_map.h>
91 #include <vm/vm_page.h>
92 #include <vm/vm_object.h>
93 #include <vm/vm_pager.h>
94 #include <vm/vm_kern.h>
95 #include <vm/vm_extern.h>
96 #include <vm/swap_pager.h>
97 #include <vm/vm_zone.h>
99 #include <sys/random.h>
100 #include <sys/sysctl.h>
101 #include <sys/spinlock.h>
103 #include <sys/thread2.h>
104 #include <sys/spinlock2.h>
107 * Virtual memory maps provide for the mapping, protection, and sharing
108 * of virtual memory objects. In addition, this module provides for an
109 * efficient virtual copy of memory from one map to another.
111 * Synchronization is required prior to most operations.
113 * Maps consist of an ordered doubly-linked list of simple entries.
114 * A hint and a RB tree is used to speed-up lookups.
116 * Callers looking to modify maps specify start/end addresses which cause
117 * the related map entry to be clipped if necessary, and then later
118 * recombined if the pieces remained compatible.
120 * Virtual copy operations are performed by copying VM object references
121 * from one map to another, and then marking both regions as copy-on-write.
123 static boolean_t vmspace_ctor(void *obj, void *privdata, int ocflags);
124 static void vmspace_dtor(void *obj, void *privdata);
125 static void vmspace_terminate(struct vmspace *vm, int final);
127 MALLOC_DEFINE(M_VMSPACE, "vmspace", "vmspace objcache backingstore");
128 MALLOC_DEFINE(M_MAP_BACKING, "map_backing", "vm_map_backing to entry");
129 static struct objcache *vmspace_cache;
132 * per-cpu page table cross mappings are initialized in early boot
133 * and might require a considerable number of vm_map_entry structures.
135 #define MAPENTRYBSP_CACHE (MAXCPU+1)
136 #define MAPENTRYAP_CACHE 8
139 * Partioning threaded programs with large anonymous memory areas can
140 * improve concurrent fault performance.
142 #define MAP_ENTRY_PARTITION_SIZE ((vm_offset_t)(32 * 1024 * 1024))
143 #define MAP_ENTRY_PARTITION_MASK (MAP_ENTRY_PARTITION_SIZE - 1)
145 #define VM_MAP_ENTRY_WITHIN_PARTITION(entry) \
146 ((((entry)->start ^ (entry)->end) & ~MAP_ENTRY_PARTITION_MASK) == 0)
148 static struct vm_zone mapentzone_store;
149 static vm_zone_t mapentzone;
151 static struct vm_map_entry map_entry_init[MAX_MAPENT];
152 static struct vm_map_entry cpu_map_entry_init_bsp[MAPENTRYBSP_CACHE];
153 static struct vm_map_entry cpu_map_entry_init_ap[MAXCPU][MAPENTRYAP_CACHE];
155 static int randomize_mmap;
156 SYSCTL_INT(_vm, OID_AUTO, randomize_mmap, CTLFLAG_RW, &randomize_mmap, 0,
157 "Randomize mmap offsets");
158 static int vm_map_relock_enable = 1;
159 SYSCTL_INT(_vm, OID_AUTO, map_relock_enable, CTLFLAG_RW,
160 &vm_map_relock_enable, 0, "insert pop pgtable optimization");
161 static int vm_map_partition_enable = 1;
162 SYSCTL_INT(_vm, OID_AUTO, map_partition_enable, CTLFLAG_RW,
163 &vm_map_partition_enable, 0, "Break up larger vm_map_entry's");
164 static int vm_map_backing_limit = 5;
165 SYSCTL_INT(_vm, OID_AUTO, map_backing_limit, CTLFLAG_RW,
166 &vm_map_backing_limit, 0, "ba.backing_ba link depth");
167 static int vm_map_backing_shadow_test = 1;
168 SYSCTL_INT(_vm, OID_AUTO, map_backing_shadow_test, CTLFLAG_RW,
169 &vm_map_backing_shadow_test, 0, "ba.object shadow test");
171 static void vmspace_drop_notoken(struct vmspace *vm);
172 static void vm_map_entry_shadow(vm_map_entry_t entry, int addref);
173 static vm_map_entry_t vm_map_entry_create(vm_map_t map, int *);
174 static void vm_map_entry_dispose (vm_map_t map, vm_map_entry_t entry, int *);
175 static void vm_map_entry_dispose_ba (vm_map_backing_t ba);
176 static void _vm_map_clip_end (vm_map_t, vm_map_entry_t, vm_offset_t, int *);
177 static void _vm_map_clip_start (vm_map_t, vm_map_entry_t, vm_offset_t, int *);
178 static void vm_map_entry_delete (vm_map_t, vm_map_entry_t, int *);
179 static void vm_map_entry_unwire (vm_map_t, vm_map_entry_t);
180 static void vm_map_copy_entry (vm_map_t, vm_map_t, vm_map_entry_t,
182 static void vm_map_unclip_range (vm_map_t map, vm_map_entry_t start_entry,
183 vm_offset_t start, vm_offset_t end, int *countp, int flags);
184 static void vm_map_entry_partition(vm_map_t map, vm_map_entry_t entry,
185 vm_offset_t vaddr, int *countp);
188 * Initialize the vm_map module. Must be called before any other vm_map
191 * Map and entry structures are allocated from the general purpose
192 * memory pool with some exceptions:
194 * - The kernel map is allocated statically.
195 * - Initial kernel map entries are allocated out of a static pool.
196 * - We must set ZONE_SPECIAL here or the early boot code can get
197 * stuck if there are >63 cores.
199 * These restrictions are necessary since malloc() uses the
200 * maps and requires map entries.
202 * Called from the low level boot code only.
207 mapentzone = &mapentzone_store;
208 zbootinit(mapentzone, "MAP ENTRY", sizeof (struct vm_map_entry),
209 map_entry_init, MAX_MAPENT);
210 mapentzone_store.zflags |= ZONE_SPECIAL;
214 * Called prior to any vmspace allocations.
216 * Called from the low level boot code only.
221 vmspace_cache = objcache_create_mbacked(M_VMSPACE,
222 sizeof(struct vmspace),
224 vmspace_ctor, vmspace_dtor,
226 zinitna(mapentzone, NULL, 0, 0, ZONE_USE_RESERVE | ZONE_SPECIAL);
232 * objcache support. We leave the pmap root cached as long as possible
233 * for performance reasons.
237 vmspace_ctor(void *obj, void *privdata, int ocflags)
239 struct vmspace *vm = obj;
241 bzero(vm, sizeof(*vm));
242 vm->vm_refcnt = VM_REF_DELETED;
249 vmspace_dtor(void *obj, void *privdata)
251 struct vmspace *vm = obj;
253 KKASSERT(vm->vm_refcnt == VM_REF_DELETED);
254 pmap_puninit(vmspace_pmap(vm));
258 * Red black tree functions
260 * The caller must hold the related map lock.
262 static int rb_vm_map_compare(vm_map_entry_t a, vm_map_entry_t b);
263 RB_GENERATE(vm_map_rb_tree, vm_map_entry, rb_entry, rb_vm_map_compare);
265 /* a->start is address, and the only field which must be initialized */
267 rb_vm_map_compare(vm_map_entry_t a, vm_map_entry_t b)
269 if (a->start < b->start)
271 else if (a->start > b->start)
277 * Initialize vmspace ref/hold counts vmspace0. There is a holdcnt for
281 vmspace_initrefs(struct vmspace *vm)
288 * Allocate a vmspace structure, including a vm_map and pmap.
289 * Initialize numerous fields. While the initial allocation is zerod,
290 * subsequence reuse from the objcache leaves elements of the structure
291 * intact (particularly the pmap), so portions must be zerod.
293 * Returns a referenced vmspace.
298 vmspace_alloc(vm_offset_t min, vm_offset_t max)
302 vm = objcache_get(vmspace_cache, M_WAITOK);
304 bzero(&vm->vm_startcopy,
305 (char *)&vm->vm_endcopy - (char *)&vm->vm_startcopy);
306 vm_map_init(&vm->vm_map, min, max, NULL); /* initializes token */
309 * NOTE: hold to acquires token for safety.
311 * On return vmspace is referenced (refs=1, hold=1). That is,
312 * each refcnt also has a holdcnt. There can be additional holds
313 * (holdcnt) above and beyond the refcnt. Finalization is handled in
314 * two stages, one on refs 1->0, and the the second on hold 1->0.
316 KKASSERT(vm->vm_holdcnt == 0);
317 KKASSERT(vm->vm_refcnt == VM_REF_DELETED);
318 vmspace_initrefs(vm);
320 pmap_pinit(vmspace_pmap(vm)); /* (some fields reused) */
321 vm->vm_map.pmap = vmspace_pmap(vm); /* XXX */
324 cpu_vmspace_alloc(vm);
331 * NOTE: Can return 0 if the vmspace is exiting.
334 vmspace_getrefs(struct vmspace *vm)
340 if (n & VM_REF_DELETED)
346 vmspace_hold(struct vmspace *vm)
348 atomic_add_int(&vm->vm_holdcnt, 1);
349 lwkt_gettoken(&vm->vm_map.token);
353 * Drop with final termination interlock.
356 vmspace_drop(struct vmspace *vm)
358 lwkt_reltoken(&vm->vm_map.token);
359 vmspace_drop_notoken(vm);
363 vmspace_drop_notoken(struct vmspace *vm)
365 if (atomic_fetchadd_int(&vm->vm_holdcnt, -1) == 1) {
366 if (vm->vm_refcnt & VM_REF_DELETED)
367 vmspace_terminate(vm, 1);
372 * A vmspace object must not be in a terminated state to be able to obtain
373 * additional refs on it.
375 * These are official references to the vmspace, the count is used to check
376 * for vmspace sharing. Foreign accessors should use 'hold' and not 'ref'.
378 * XXX we need to combine hold & ref together into one 64-bit field to allow
379 * holds to prevent stage-1 termination.
382 vmspace_ref(struct vmspace *vm)
386 atomic_add_int(&vm->vm_holdcnt, 1);
387 n = atomic_fetchadd_int(&vm->vm_refcnt, 1);
388 KKASSERT((n & VM_REF_DELETED) == 0);
392 * Release a ref on the vmspace. On the 1->0 transition we do stage-1
393 * termination of the vmspace. Then, on the final drop of the hold we
394 * will do stage-2 final termination.
397 vmspace_rel(struct vmspace *vm)
402 * Drop refs. Each ref also has a hold which is also dropped.
404 * When refs hits 0 compete to get the VM_REF_DELETED flag (hold
405 * prevent finalization) to start termination processing.
406 * Finalization occurs when the last hold count drops to 0.
408 n = atomic_fetchadd_int(&vm->vm_refcnt, -1) - 1;
410 if (atomic_cmpset_int(&vm->vm_refcnt, 0, VM_REF_DELETED)) {
411 vmspace_terminate(vm, 0);
417 vmspace_drop_notoken(vm);
421 * This is called during exit indicating that the vmspace is no
422 * longer in used by an exiting process, but the process has not yet
425 * We drop refs, allowing for stage-1 termination, but maintain a holdcnt
426 * to prevent stage-2 until the process is reaped. Note hte order of
427 * operation, we must hold first.
432 vmspace_relexit(struct vmspace *vm)
434 atomic_add_int(&vm->vm_holdcnt, 1);
439 * Called during reap to disconnect the remainder of the vmspace from
440 * the process. On the hold drop the vmspace termination is finalized.
445 vmspace_exitfree(struct proc *p)
451 vmspace_drop_notoken(vm);
455 * Called in two cases:
457 * (1) When the last refcnt is dropped and the vmspace becomes inactive,
458 * called with final == 0. refcnt will be (u_int)-1 at this point,
459 * and holdcnt will still be non-zero.
461 * (2) When holdcnt becomes 0, called with final == 1. There should no
462 * longer be anyone with access to the vmspace.
464 * VMSPACE_EXIT1 flags the primary deactivation
465 * VMSPACE_EXIT2 flags the last reap
468 vmspace_terminate(struct vmspace *vm, int final)
472 lwkt_gettoken(&vm->vm_map.token);
474 KKASSERT((vm->vm_flags & VMSPACE_EXIT1) == 0);
475 vm->vm_flags |= VMSPACE_EXIT1;
478 * Get rid of most of the resources. Leave the kernel pmap
481 * If the pmap does not contain wired pages we can bulk-delete
482 * the pmap as a performance optimization before removing the
485 * If the pmap contains wired pages we cannot do this
486 * pre-optimization because currently vm_fault_unwire()
487 * expects the pmap pages to exist and will not decrement
488 * p->wire_count if they do not.
491 if (vmspace_pmap(vm)->pm_stats.wired_count) {
492 vm_map_remove(&vm->vm_map, VM_MIN_USER_ADDRESS,
493 VM_MAX_USER_ADDRESS);
494 pmap_remove_pages(vmspace_pmap(vm), VM_MIN_USER_ADDRESS,
495 VM_MAX_USER_ADDRESS);
497 pmap_remove_pages(vmspace_pmap(vm), VM_MIN_USER_ADDRESS,
498 VM_MAX_USER_ADDRESS);
499 vm_map_remove(&vm->vm_map, VM_MIN_USER_ADDRESS,
500 VM_MAX_USER_ADDRESS);
502 lwkt_reltoken(&vm->vm_map.token);
504 KKASSERT((vm->vm_flags & VMSPACE_EXIT1) != 0);
505 KKASSERT((vm->vm_flags & VMSPACE_EXIT2) == 0);
508 * Get rid of remaining basic resources.
510 vm->vm_flags |= VMSPACE_EXIT2;
513 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
514 vm_map_lock(&vm->vm_map);
515 cpu_vmspace_free(vm);
518 * Lock the map, to wait out all other references to it.
519 * Delete all of the mappings and pages they hold, then call
520 * the pmap module to reclaim anything left.
522 vm_map_delete(&vm->vm_map,
523 vm_map_min(&vm->vm_map),
524 vm_map_max(&vm->vm_map),
526 vm_map_unlock(&vm->vm_map);
527 vm_map_entry_release(count);
529 pmap_release(vmspace_pmap(vm));
530 lwkt_reltoken(&vm->vm_map.token);
531 objcache_put(vmspace_cache, vm);
536 * Swap useage is determined by taking the proportional swap used by
537 * VM objects backing the VM map. To make up for fractional losses,
538 * if the VM object has any swap use at all the associated map entries
539 * count for at least 1 swap page.
544 vmspace_swap_count(struct vmspace *vm)
546 vm_map_t map = &vm->vm_map;
549 vm_offset_t count = 0;
554 RB_FOREACH(cur, vm_map_rb_tree, &map->rb_root) {
555 switch(cur->maptype) {
556 case VM_MAPTYPE_NORMAL:
557 case VM_MAPTYPE_VPAGETABLE:
558 if ((object = cur->ba.object) == NULL)
560 if (object->swblock_count) {
561 n = (cur->end - cur->start) / PAGE_SIZE;
562 count += object->swblock_count *
563 SWAP_META_PAGES * n / object->size + 1;
576 * Calculate the approximate number of anonymous pages in use by
577 * this vmspace. To make up for fractional losses, we count each
578 * VM object as having at least 1 anonymous page.
583 vmspace_anonymous_count(struct vmspace *vm)
585 vm_map_t map = &vm->vm_map;
588 vm_offset_t count = 0;
591 RB_FOREACH(cur, vm_map_rb_tree, &map->rb_root) {
592 switch(cur->maptype) {
593 case VM_MAPTYPE_NORMAL:
594 case VM_MAPTYPE_VPAGETABLE:
595 if ((object = cur->ba.object) == NULL)
597 if (object->type != OBJT_DEFAULT &&
598 object->type != OBJT_SWAP) {
601 count += object->resident_page_count;
613 * Initialize an existing vm_map structure such as that in the vmspace
614 * structure. The pmap is initialized elsewhere.
619 vm_map_init(struct vm_map *map, vm_offset_t min_addr, vm_offset_t max_addr,
622 RB_INIT(&map->rb_root);
623 spin_init(&map->ilock_spin, "ilock");
624 map->ilock_base = NULL;
628 vm_map_min(map) = min_addr;
629 vm_map_max(map) = max_addr;
633 bzero(&map->freehint, sizeof(map->freehint));
634 lwkt_token_init(&map->token, "vm_map");
635 lockinit(&map->lock, "vm_maplk", (hz + 9) / 10, 0);
639 * Find the first possible free address for the specified request length.
640 * Returns 0 if we don't have one cached.
644 vm_map_freehint_find(vm_map_t map, vm_size_t length, vm_size_t align)
646 vm_map_freehint_t *scan;
648 scan = &map->freehint[0];
649 while (scan < &map->freehint[VM_MAP_FFCOUNT]) {
650 if (scan->length == length && scan->align == align)
658 * Unconditionally set the freehint. Called by vm_map_findspace() after
659 * it finds an address. This will help us iterate optimally on the next
664 vm_map_freehint_update(vm_map_t map, vm_offset_t start,
665 vm_size_t length, vm_size_t align)
667 vm_map_freehint_t *scan;
669 scan = &map->freehint[0];
670 while (scan < &map->freehint[VM_MAP_FFCOUNT]) {
671 if (scan->length == length && scan->align == align) {
677 scan = &map->freehint[map->freehint_newindex & VM_MAP_FFMASK];
680 scan->length = length;
681 ++map->freehint_newindex;
685 * Update any existing freehints (for any alignment), for the hole we just
690 vm_map_freehint_hole(vm_map_t map, vm_offset_t start, vm_size_t length)
692 vm_map_freehint_t *scan;
694 scan = &map->freehint[0];
695 while (scan < &map->freehint[VM_MAP_FFCOUNT]) {
696 if (scan->length <= length && scan->start > start)
703 * This function handles MAP_ENTRY_NEEDS_COPY by inserting a fronting
704 * object in the entry for COW faults.
706 * The entire chain including entry->ba (prior to inserting the fronting
707 * object) essentially becomes set in stone... elements of it can be paged
708 * in or out, but cannot be further modified.
710 * NOTE: If we do not optimize the backing chain then a unique copy is not
711 * needed. Note, however, that because portions of the chain are
712 * shared across pmaps we cannot make any changes to the vm_map_backing
713 * elements themselves.
715 * If the map segment is governed by a virtual page table then it is
716 * possible to address offsets beyond the mapped area. Just allocate
717 * a maximally sized object for this case.
719 * If addref is non-zero an additional reference is added to the returned
720 * entry. This mechanic exists because the additional reference might have
721 * to be added atomically and not after return to prevent a premature
722 * collapse. XXX currently there is no collapse code.
724 * The vm_map must be exclusively locked.
725 * No other requirements.
729 vm_map_entry_shadow(vm_map_entry_t entry, int addref)
737 if (entry->maptype == VM_MAPTYPE_VPAGETABLE)
740 length = atop(entry->end - entry->start);
741 ba = kmalloc(sizeof(*ba), M_MAP_BACKING, M_INTWAIT); /* copied later */
744 * Don't create the new object if the old object isn't shared.
746 * The ref on source is inherited when we move it into the ba.
747 * If addref is non-zero additional ref(s) are being added (probably
748 * for map entry fork purposes), so clear OBJ_ONEMAPPING.
750 * Caller ensures source exists (all backing_ba's must have objects),
751 * typically indirectly by virtue of the NEEDS_COPY flag being set.
753 * WARNING! Checking ref_count == 1 only works because we are testing
754 * the object embedded in the entry (entry->ba.object).
755 * This test DOES NOT WORK if checking an object hanging off
756 * the backing chain (entry->ba.backing_ba list) because the
757 * vm_map_backing might be shared, or part of a chain that
758 * is shared. Checking ba->refs is worthless.
760 source = entry->ba.object;
763 if (source->type != OBJT_VNODE) {
764 vm_object_hold(source);
765 if (source->ref_count == 1 &&
766 source->handle == NULL &&
767 (source->type == OBJT_DEFAULT ||
768 source->type == OBJT_SWAP)) {
770 vm_object_reference_locked(source);
771 vm_object_clear_flag(source,
774 vm_object_drop(source);
775 kfree(ba, M_MAP_BACKING);
778 drop_source = 1; /* drop source at end */
784 * Once it becomes part of a backing_ba chain it can wind up anywhere,
785 * drop the ONEMAPPING flag now.
787 vm_object_clear_flag(source, OBJ_ONEMAPPING);
790 * Allocate a new object with the given length. The new object
791 * is returned referenced but we may have to add another one.
792 * If we are adding a second reference we must clear OBJ_ONEMAPPING.
793 * (typically because the caller is about to clone a vm_map_entry).
795 * The source object currently has an extra reference to prevent
796 * collapses into it while we mess with its shadow list, which
797 * we will remove later in this routine.
799 * The target object may require a second reference if asked for one
802 result = vm_object_allocate(OBJT_DEFAULT, length);
804 panic("vm_object_shadow: no object for shadowing");
805 vm_object_hold(result);
807 vm_object_reference_locked(result);
808 vm_object_clear_flag(result, OBJ_ONEMAPPING);
812 * The new object shadows the source object.
814 * Try to optimize the result object's page color when shadowing
815 * in order to maintain page coloring consistency in the combined
818 * The source object is moved to ba, retaining its existing ref-count.
819 * No additional ref is needed.
821 * SHADOWING IS NOT APPLICABLE TO OBJT_VNODE OBJECTS
823 *ba = entry->ba; /* previous ba */
824 ba->refs = 1; /* initialize ref count */
825 entry->ba.object = result; /* new ba (at head of entry) */
826 entry->ba.backing_ba = ba;
827 entry->ba.backing_count = ba->backing_count + 1;
828 entry->ba.offset = 0;
831 /* cpu localization twist */
832 result->pg_color = vm_quickcolor();
835 * Adjust the return storage. Drop the ref on source before
838 vm_object_drop(result);
840 vm_object_drop(source);
842 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
846 * Allocate an object for a vm_map_entry.
848 * Object allocation for anonymous mappings is defered as long as possible.
849 * This function is called when we can defer no longer, generally when a map
850 * entry might be split or forked or takes a page fault.
852 * If the map segment is governed by a virtual page table then it is
853 * possible to address offsets beyond the mapped area. Just allocate
854 * a maximally sized object for this case.
856 * The vm_map must be exclusively locked.
857 * No other requirements.
860 vm_map_entry_allocate_object(vm_map_entry_t entry)
865 * ba.offset is added cumulatively in the backing_ba scan, so we
866 * can noly reset it to zero if ba.backing_ba is NULL. We reset
867 * it to 0 only for debugging convenience.
869 * ba.offset cannot otherwise be modified because it effects
870 * the offsets for the entire backing_ba chain.
872 if (entry->ba.backing_ba == NULL)
873 entry->ba.offset = 0;
875 if (entry->maptype == VM_MAPTYPE_VPAGETABLE) {
876 obj = vm_object_allocate(OBJT_DEFAULT, 0x7FFFFFFF); /* XXX */
878 obj = vm_object_allocate(OBJT_DEFAULT,
879 atop(entry->end - entry->start) +
882 entry->ba.object = obj;
886 * Set an initial negative count so the first attempt to reserve
887 * space preloads a bunch of vm_map_entry's for this cpu. Also
888 * pre-allocate 2 vm_map_entries which will be needed by zalloc() to
889 * map a new page for vm_map_entry structures. SMP systems are
890 * particularly sensitive.
892 * This routine is called in early boot so we cannot just call
893 * vm_map_entry_reserve().
895 * Called from the low level boot code only (for each cpu)
897 * WARNING! Take care not to have too-big a static/BSS structure here
898 * as MAXCPU can be 256+, otherwise the loader's 64MB heap
899 * can get blown out by the kernel plus the initrd image.
902 vm_map_entry_reserve_cpu_init(globaldata_t gd)
904 vm_map_entry_t entry;
908 atomic_add_int(&gd->gd_vme_avail, -MAP_RESERVE_COUNT * 2);
909 if (gd->gd_cpuid == 0) {
910 entry = &cpu_map_entry_init_bsp[0];
911 count = MAPENTRYBSP_CACHE;
913 entry = &cpu_map_entry_init_ap[gd->gd_cpuid][0];
914 count = MAPENTRYAP_CACHE;
916 for (i = 0; i < count; ++i, ++entry) {
917 MAPENT_FREELIST(entry) = gd->gd_vme_base;
918 gd->gd_vme_base = entry;
923 * Reserves vm_map_entry structures so code later-on can manipulate
924 * map_entry structures within a locked map without blocking trying
925 * to allocate a new vm_map_entry.
929 * WARNING! We must not decrement gd_vme_avail until after we have
930 * ensured that sufficient entries exist, otherwise we can
931 * get into an endless call recursion in the zalloc code
935 vm_map_entry_reserve(int count)
937 struct globaldata *gd = mycpu;
938 vm_map_entry_t entry;
941 * Make sure we have enough structures in gd_vme_base to handle
942 * the reservation request.
944 * Use a critical section to protect against VM faults. It might
945 * not be needed, but we have to be careful here.
947 if (gd->gd_vme_avail < count) {
949 while (gd->gd_vme_avail < count) {
950 entry = zalloc(mapentzone);
951 MAPENT_FREELIST(entry) = gd->gd_vme_base;
952 gd->gd_vme_base = entry;
953 atomic_add_int(&gd->gd_vme_avail, 1);
957 atomic_add_int(&gd->gd_vme_avail, -count);
963 * Releases previously reserved vm_map_entry structures that were not
964 * used. If we have too much junk in our per-cpu cache clean some of
970 vm_map_entry_release(int count)
972 struct globaldata *gd = mycpu;
973 vm_map_entry_t entry;
974 vm_map_entry_t efree;
976 count = atomic_fetchadd_int(&gd->gd_vme_avail, count) + count;
977 if (gd->gd_vme_avail > MAP_RESERVE_SLOP) {
980 while (gd->gd_vme_avail > MAP_RESERVE_HYST) {
981 entry = gd->gd_vme_base;
982 KKASSERT(entry != NULL);
983 gd->gd_vme_base = MAPENT_FREELIST(entry);
984 atomic_add_int(&gd->gd_vme_avail, -1);
985 MAPENT_FREELIST(entry) = efree;
989 while ((entry = efree) != NULL) {
990 efree = MAPENT_FREELIST(efree);
991 zfree(mapentzone, entry);
997 * Reserve map entry structures for use in kernel_map itself. These
998 * entries have *ALREADY* been reserved on a per-cpu basis when the map
999 * was inited. This function is used by zalloc() to avoid a recursion
1000 * when zalloc() itself needs to allocate additional kernel memory.
1002 * This function works like the normal reserve but does not load the
1003 * vm_map_entry cache (because that would result in an infinite
1004 * recursion). Note that gd_vme_avail may go negative. This is expected.
1006 * Any caller of this function must be sure to renormalize after
1007 * potentially eating entries to ensure that the reserve supply
1013 vm_map_entry_kreserve(int count)
1015 struct globaldata *gd = mycpu;
1017 atomic_add_int(&gd->gd_vme_avail, -count);
1018 KASSERT(gd->gd_vme_base != NULL,
1019 ("no reserved entries left, gd_vme_avail = %d",
1025 * Release previously reserved map entries for kernel_map. We do not
1026 * attempt to clean up like the normal release function as this would
1027 * cause an unnecessary (but probably not fatal) deep procedure call.
1032 vm_map_entry_krelease(int count)
1034 struct globaldata *gd = mycpu;
1036 atomic_add_int(&gd->gd_vme_avail, count);
1040 * Allocates a VM map entry for insertion. No entry fields are filled in.
1042 * The entries should have previously been reserved. The reservation count
1043 * is tracked in (*countp).
1047 static vm_map_entry_t
1048 vm_map_entry_create(vm_map_t map, int *countp)
1050 struct globaldata *gd = mycpu;
1051 vm_map_entry_t entry;
1053 KKASSERT(*countp > 0);
1056 entry = gd->gd_vme_base;
1057 KASSERT(entry != NULL, ("gd_vme_base NULL! count %d", *countp));
1058 gd->gd_vme_base = MAPENT_FREELIST(entry);
1065 * Dispose of the dynamically allocated backing_ba chain associated
1066 * with a vm_map_entry.
1068 * We decrement the (possibly shared) element and kfree() on the
1069 * 1->0 transition. We only iterate to the next backing_ba when
1070 * the previous one went through a 1->0 transition.
1073 vm_map_entry_dispose_ba(vm_map_backing_t ba)
1075 vm_map_backing_t next;
1079 refs = atomic_fetchadd_long(&ba->refs, -1);
1082 KKASSERT(refs == 1); /* transitioned 1->0 */
1084 vm_object_deallocate(ba->object);
1085 next = ba->backing_ba;
1086 kfree(ba, M_MAP_BACKING);
1092 * Dispose of a vm_map_entry that is no longer being referenced.
1097 vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry, int *countp)
1099 struct globaldata *gd = mycpu;
1102 * Dispose of the base object and the backing link.
1104 switch(entry->maptype) {
1105 case VM_MAPTYPE_NORMAL:
1106 case VM_MAPTYPE_VPAGETABLE:
1107 case VM_MAPTYPE_SUBMAP:
1108 if (entry->ba.object)
1109 vm_object_deallocate(entry->ba.object);
1111 case VM_MAPTYPE_UKSMAP:
1117 vm_map_entry_dispose_ba(entry->ba.backing_ba);
1120 * Cleanup for safety.
1122 entry->ba.backing_ba = NULL;
1123 entry->ba.object = NULL;
1124 entry->ba.offset = 0;
1128 MAPENT_FREELIST(entry) = gd->gd_vme_base;
1129 gd->gd_vme_base = entry;
1135 * Insert/remove entries from maps.
1137 * The related map must be exclusively locked.
1138 * The caller must hold map->token
1139 * No other requirements.
1141 static __inline void
1142 vm_map_entry_link(vm_map_t map, vm_map_entry_t entry)
1144 ASSERT_VM_MAP_LOCKED(map);
1147 if (vm_map_rb_tree_RB_INSERT(&map->rb_root, entry))
1148 panic("vm_map_entry_link: dup addr map %p ent %p", map, entry);
1151 static __inline void
1152 vm_map_entry_unlink(vm_map_t map,
1153 vm_map_entry_t entry)
1155 ASSERT_VM_MAP_LOCKED(map);
1157 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1158 panic("vm_map_entry_unlink: attempt to mess with "
1159 "locked entry! %p", entry);
1161 vm_map_rb_tree_RB_REMOVE(&map->rb_root, entry);
1166 * Finds the map entry containing (or immediately preceding) the specified
1167 * address in the given map. The entry is returned in (*entry).
1169 * The boolean result indicates whether the address is actually contained
1172 * The related map must be locked.
1173 * No other requirements.
1176 vm_map_lookup_entry(vm_map_t map, vm_offset_t address, vm_map_entry_t *entry)
1179 vm_map_entry_t last;
1181 ASSERT_VM_MAP_LOCKED(map);
1184 * Locate the record from the top of the tree. 'last' tracks the
1185 * closest prior record and is returned if no match is found, which
1186 * in binary tree terms means tracking the most recent right-branch
1187 * taken. If there is no prior record, *entry is set to NULL.
1190 tmp = RB_ROOT(&map->rb_root);
1193 if (address >= tmp->start) {
1194 if (address < tmp->end) {
1199 tmp = RB_RIGHT(tmp, rb_entry);
1201 tmp = RB_LEFT(tmp, rb_entry);
1209 * Inserts the given whole VM object into the target map at the specified
1210 * address range. The object's size should match that of the address range.
1212 * The map must be exclusively locked.
1213 * The object must be held.
1214 * The caller must have reserved sufficient vm_map_entry structures.
1216 * If object is non-NULL, ref count must be bumped by caller prior to
1217 * making call to account for the new entry.
1220 vm_map_insert(vm_map_t map, int *countp, void *map_object, void *map_aux,
1221 vm_ooffset_t offset, vm_offset_t start, vm_offset_t end,
1222 vm_maptype_t maptype, vm_subsys_t id,
1223 vm_prot_t prot, vm_prot_t max, int cow)
1225 vm_map_entry_t new_entry;
1226 vm_map_entry_t prev_entry;
1227 vm_map_entry_t next;
1228 vm_map_entry_t temp_entry;
1229 vm_eflags_t protoeflags;
1233 if (maptype == VM_MAPTYPE_UKSMAP)
1236 object = map_object;
1238 ASSERT_VM_MAP_LOCKED(map);
1240 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
1243 * Check that the start and end points are not bogus.
1245 if ((start < vm_map_min(map)) || (end > vm_map_max(map)) ||
1247 return (KERN_INVALID_ADDRESS);
1251 * Find the entry prior to the proposed starting address; if it's part
1252 * of an existing entry, this range is bogus.
1254 if (vm_map_lookup_entry(map, start, &temp_entry))
1255 return (KERN_NO_SPACE);
1256 prev_entry = temp_entry;
1259 * Assert that the next entry doesn't overlap the end point.
1262 next = vm_map_rb_tree_RB_NEXT(prev_entry);
1264 next = RB_MIN(vm_map_rb_tree, &map->rb_root);
1265 if (next && next->start < end)
1266 return (KERN_NO_SPACE);
1270 if (cow & MAP_COPY_ON_WRITE)
1271 protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
1273 if (cow & MAP_NOFAULT) {
1274 protoeflags |= MAP_ENTRY_NOFAULT;
1276 KASSERT(object == NULL,
1277 ("vm_map_insert: paradoxical MAP_NOFAULT request"));
1279 if (cow & MAP_DISABLE_SYNCER)
1280 protoeflags |= MAP_ENTRY_NOSYNC;
1281 if (cow & MAP_DISABLE_COREDUMP)
1282 protoeflags |= MAP_ENTRY_NOCOREDUMP;
1283 if (cow & MAP_IS_STACK)
1284 protoeflags |= MAP_ENTRY_STACK;
1285 if (cow & MAP_IS_KSTACK)
1286 protoeflags |= MAP_ENTRY_KSTACK;
1288 lwkt_gettoken(&map->token);
1292 * When object is non-NULL, it could be shared with another
1293 * process. We have to set or clear OBJ_ONEMAPPING
1296 * NOTE: This flag is only applicable to DEFAULT and SWAP
1297 * objects and will already be clear in other types
1298 * of objects, so a shared object lock is ok for
1301 if (object->ref_count > 1)
1302 vm_object_clear_flag(object, OBJ_ONEMAPPING);
1304 else if (prev_entry &&
1305 (prev_entry->eflags == protoeflags) &&
1306 (prev_entry->end == start) &&
1307 (prev_entry->wired_count == 0) &&
1308 (prev_entry->id == id) &&
1309 prev_entry->maptype == maptype &&
1310 maptype == VM_MAPTYPE_NORMAL &&
1311 prev_entry->ba.backing_ba == NULL && /* not backed */
1312 ((prev_entry->ba.object == NULL) ||
1313 vm_object_coalesce(prev_entry->ba.object,
1314 OFF_TO_IDX(prev_entry->ba.offset),
1315 (vm_size_t)(prev_entry->end - prev_entry->start),
1316 (vm_size_t)(end - prev_entry->end)))) {
1318 * We were able to extend the object. Determine if we
1319 * can extend the previous map entry to include the
1320 * new range as well.
1322 if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
1323 (prev_entry->protection == prot) &&
1324 (prev_entry->max_protection == max)) {
1325 map->size += (end - prev_entry->end);
1326 prev_entry->end = end;
1327 vm_map_simplify_entry(map, prev_entry, countp);
1328 lwkt_reltoken(&map->token);
1329 return (KERN_SUCCESS);
1333 * If we can extend the object but cannot extend the
1334 * map entry, we have to create a new map entry. We
1335 * must bump the ref count on the extended object to
1336 * account for it. object may be NULL.
1338 object = prev_entry->ba.object;
1339 offset = prev_entry->ba.offset +
1340 (prev_entry->end - prev_entry->start);
1342 vm_object_hold(object);
1343 vm_object_lock_swap(); /* map->token order */
1344 vm_object_reference_locked(object);
1345 map_object = object;
1351 * NOTE: if conditionals fail, object can be NULL here. This occurs
1352 * in things like the buffer map where we manage kva but do not manage
1357 * Create a new entry
1359 new_entry = vm_map_entry_create(map, countp);
1360 new_entry->start = start;
1361 new_entry->end = end;
1364 new_entry->maptype = maptype;
1365 new_entry->eflags = protoeflags;
1366 new_entry->aux.master_pde = 0; /* in case size is different */
1367 new_entry->aux.map_aux = map_aux;
1368 new_entry->ba.map_object = map_object;
1369 new_entry->ba.backing_ba = NULL;
1370 new_entry->ba.backing_count = 0;
1371 new_entry->ba.offset = offset;
1372 new_entry->ba.refs = 0;
1373 new_entry->ba.flags = 0;
1375 new_entry->inheritance = VM_INHERIT_DEFAULT;
1376 new_entry->protection = prot;
1377 new_entry->max_protection = max;
1378 new_entry->wired_count = 0;
1381 * Insert the new entry into the list
1384 vm_map_entry_link(map, new_entry);
1385 map->size += new_entry->end - new_entry->start;
1388 * Don't worry about updating freehint[] when inserting, allow
1389 * addresses to be lower than the actual first free spot.
1393 * Temporarily removed to avoid MAP_STACK panic, due to
1394 * MAP_STACK being a huge hack. Will be added back in
1395 * when MAP_STACK (and the user stack mapping) is fixed.
1398 * It may be possible to simplify the entry
1400 vm_map_simplify_entry(map, new_entry, countp);
1404 * Try to pre-populate the page table. Mappings governed by virtual
1405 * page tables cannot be prepopulated without a lot of work, so
1408 if ((cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) &&
1409 maptype != VM_MAPTYPE_VPAGETABLE &&
1410 maptype != VM_MAPTYPE_UKSMAP) {
1412 if (vm_map_relock_enable && (cow & MAP_PREFAULT_RELOCK)) {
1414 vm_object_lock_swap();
1415 vm_object_drop(object);
1417 pmap_object_init_pt(map->pmap, start, prot,
1418 object, OFF_TO_IDX(offset), end - start,
1419 cow & MAP_PREFAULT_PARTIAL);
1421 vm_object_hold(object);
1422 vm_object_lock_swap();
1425 lwkt_reltoken(&map->token);
1427 vm_object_drop(object);
1429 return (KERN_SUCCESS);
1433 * Find sufficient space for `length' bytes in the given map, starting at
1434 * `start'. Returns 0 on success, 1 on no space.
1436 * This function will returned an arbitrarily aligned pointer. If no
1437 * particular alignment is required you should pass align as 1. Note that
1438 * the map may return PAGE_SIZE aligned pointers if all the lengths used in
1439 * the map are a multiple of PAGE_SIZE, even if you pass a smaller align
1442 * 'align' should be a power of 2 but is not required to be.
1444 * The map must be exclusively locked.
1445 * No other requirements.
1448 vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length,
1449 vm_size_t align, int flags, vm_offset_t *addr)
1451 vm_map_entry_t entry;
1453 vm_offset_t hole_start;
1455 vm_offset_t align_mask;
1457 if (start < vm_map_min(map))
1458 start = vm_map_min(map);
1459 if (start > vm_map_max(map))
1463 * If the alignment is not a power of 2 we will have to use
1464 * a mod/division, set align_mask to a special value.
1466 if ((align | (align - 1)) + 1 != (align << 1))
1467 align_mask = (vm_offset_t)-1;
1469 align_mask = align - 1;
1472 * Use freehint to adjust the start point, hopefully reducing
1473 * the iteration to O(1).
1475 hole_start = vm_map_freehint_find(map, length, align);
1476 if (start < hole_start)
1478 if (vm_map_lookup_entry(map, start, &tmp))
1480 entry = tmp; /* may be NULL */
1483 * Look through the rest of the map, trying to fit a new region in the
1484 * gap between existing regions, or after the very last region.
1488 * Adjust the proposed start by the requested alignment,
1489 * be sure that we didn't wrap the address.
1491 if (align_mask == (vm_offset_t)-1)
1492 end = roundup(start, align);
1494 end = (start + align_mask) & ~align_mask;
1500 * Find the end of the proposed new region. Be sure we didn't
1501 * go beyond the end of the map, or wrap around the address.
1502 * Then check to see if this is the last entry or if the
1503 * proposed end fits in the gap between this and the next
1506 end = start + length;
1507 if (end > vm_map_max(map) || end < start)
1511 * Locate the next entry, we can stop if this is the
1512 * last entry (we know we are in-bounds so that would
1516 entry = vm_map_rb_tree_RB_NEXT(entry);
1518 entry = RB_MIN(vm_map_rb_tree, &map->rb_root);
1523 * Determine if the proposed area would overlap the
1526 * When matching against a STACK entry, only allow the
1527 * memory map to intrude on the ungrown portion of the
1528 * STACK entry when MAP_TRYFIXED is set.
1530 if (entry->start >= end) {
1531 if ((entry->eflags & MAP_ENTRY_STACK) == 0)
1533 if (flags & MAP_TRYFIXED)
1535 if (entry->start - entry->aux.avail_ssize >= end)
1542 * Update the freehint
1544 vm_map_freehint_update(map, start, length, align);
1547 * Grow the kernel_map if necessary. pmap_growkernel() will panic
1548 * if it fails. The kernel_map is locked and nothing can steal
1549 * our address space if pmap_growkernel() blocks.
1551 * NOTE: This may be unconditionally called for kldload areas on
1552 * x86_64 because these do not bump kernel_vm_end (which would
1553 * fill 128G worth of page tables!). Therefore we must not
1556 if (map == &kernel_map) {
1559 kstop = round_page(start + length);
1560 if (kstop > kernel_vm_end)
1561 pmap_growkernel(start, kstop);
1568 * vm_map_find finds an unallocated region in the target address map with
1569 * the given length and allocates it. The search is defined to be first-fit
1570 * from the specified address; the region found is returned in the same
1573 * If object is non-NULL, ref count must be bumped by caller
1574 * prior to making call to account for the new entry.
1576 * No requirements. This function will lock the map temporarily.
1579 vm_map_find(vm_map_t map, void *map_object, void *map_aux,
1580 vm_ooffset_t offset, vm_offset_t *addr,
1581 vm_size_t length, vm_size_t align, boolean_t fitit,
1582 vm_maptype_t maptype, vm_subsys_t id,
1583 vm_prot_t prot, vm_prot_t max, int cow)
1590 if (maptype == VM_MAPTYPE_UKSMAP)
1593 object = map_object;
1597 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1600 vm_object_hold_shared(object);
1602 if (vm_map_findspace(map, start, length, align, 0, addr)) {
1604 vm_object_drop(object);
1606 vm_map_entry_release(count);
1607 return (KERN_NO_SPACE);
1611 result = vm_map_insert(map, &count, map_object, map_aux,
1612 offset, start, start + length,
1613 maptype, id, prot, max, cow);
1615 vm_object_drop(object);
1617 vm_map_entry_release(count);
1623 * Simplify the given map entry by merging with either neighbor. This
1624 * routine also has the ability to merge with both neighbors.
1626 * This routine guarentees that the passed entry remains valid (though
1627 * possibly extended). When merging, this routine may delete one or
1628 * both neighbors. No action is taken on entries which have their
1629 * in-transition flag set.
1631 * The map must be exclusively locked.
1634 vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry, int *countp)
1636 vm_map_entry_t next, prev;
1637 vm_size_t prevsize, esize;
1639 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1640 ++mycpu->gd_cnt.v_intrans_coll;
1644 if (entry->maptype == VM_MAPTYPE_SUBMAP)
1646 if (entry->maptype == VM_MAPTYPE_UKSMAP)
1649 prev = vm_map_rb_tree_RB_PREV(entry);
1651 prevsize = prev->end - prev->start;
1652 if ( (prev->end == entry->start) &&
1653 (prev->maptype == entry->maptype) &&
1654 (prev->ba.object == entry->ba.object) &&
1655 (prev->ba.backing_ba == entry->ba.backing_ba) &&
1656 (!prev->ba.object ||
1657 (prev->ba.offset + prevsize == entry->ba.offset)) &&
1658 (prev->eflags == entry->eflags) &&
1659 (prev->protection == entry->protection) &&
1660 (prev->max_protection == entry->max_protection) &&
1661 (prev->inheritance == entry->inheritance) &&
1662 (prev->id == entry->id) &&
1663 (prev->wired_count == entry->wired_count)) {
1664 vm_map_entry_unlink(map, prev);
1665 entry->start = prev->start;
1666 entry->ba.offset = prev->ba.offset;
1667 vm_map_entry_dispose(map, prev, countp);
1671 next = vm_map_rb_tree_RB_NEXT(entry);
1673 esize = entry->end - entry->start;
1674 if ((entry->end == next->start) &&
1675 (next->maptype == entry->maptype) &&
1676 (next->ba.object == entry->ba.object) &&
1677 (prev->ba.backing_ba == entry->ba.backing_ba) &&
1678 (!entry->ba.object ||
1679 (entry->ba.offset + esize == next->ba.offset)) &&
1680 (next->eflags == entry->eflags) &&
1681 (next->protection == entry->protection) &&
1682 (next->max_protection == entry->max_protection) &&
1683 (next->inheritance == entry->inheritance) &&
1684 (next->id == entry->id) &&
1685 (next->wired_count == entry->wired_count)) {
1686 vm_map_entry_unlink(map, next);
1687 entry->end = next->end;
1688 vm_map_entry_dispose(map, next, countp);
1694 * Asserts that the given entry begins at or after the specified address.
1695 * If necessary, it splits the entry into two.
1697 #define vm_map_clip_start(map, entry, startaddr, countp) \
1699 if (startaddr > entry->start) \
1700 _vm_map_clip_start(map, entry, startaddr, countp); \
1704 * This routine is called only when it is known that the entry must be split.
1706 * The map must be exclusively locked.
1709 _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start,
1712 vm_map_entry_t new_entry;
1715 * Split off the front portion -- note that we must insert the new
1716 * entry BEFORE this one, so that this entry has the specified
1720 vm_map_simplify_entry(map, entry, countp);
1723 * If there is no object backing this entry, we might as well create
1724 * one now. If we defer it, an object can get created after the map
1725 * is clipped, and individual objects will be created for the split-up
1726 * map. This is a bit of a hack, but is also about the best place to
1727 * put this improvement.
1729 if (entry->ba.object == NULL && !map->system_map &&
1730 VM_MAP_ENTRY_WITHIN_PARTITION(entry)) {
1731 vm_map_entry_allocate_object(entry);
1734 new_entry = vm_map_entry_create(map, countp);
1735 *new_entry = *entry;
1737 new_entry->end = start;
1738 entry->ba.offset += (start - entry->start);
1739 entry->start = start;
1740 if (new_entry->ba.backing_ba)
1741 atomic_add_long(&new_entry->ba.backing_ba->refs, 1);
1743 vm_map_entry_link(map, new_entry);
1745 switch(entry->maptype) {
1746 case VM_MAPTYPE_NORMAL:
1747 case VM_MAPTYPE_VPAGETABLE:
1748 if (new_entry->ba.object) {
1749 vm_object_hold(new_entry->ba.object);
1750 vm_object_reference_locked(new_entry->ba.object);
1751 vm_object_drop(new_entry->ba.object);
1760 * Asserts that the given entry ends at or before the specified address.
1761 * If necessary, it splits the entry into two.
1763 * The map must be exclusively locked.
1765 #define vm_map_clip_end(map, entry, endaddr, countp) \
1767 if (endaddr < entry->end) \
1768 _vm_map_clip_end(map, entry, endaddr, countp); \
1772 * This routine is called only when it is known that the entry must be split.
1774 * The map must be exclusively locked.
1777 _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end,
1780 vm_map_entry_t new_entry;
1783 * If there is no object backing this entry, we might as well create
1784 * one now. If we defer it, an object can get created after the map
1785 * is clipped, and individual objects will be created for the split-up
1786 * map. This is a bit of a hack, but is also about the best place to
1787 * put this improvement.
1790 if (entry->ba.object == NULL && !map->system_map &&
1791 VM_MAP_ENTRY_WITHIN_PARTITION(entry)) {
1792 vm_map_entry_allocate_object(entry);
1796 * Create a new entry and insert it AFTER the specified entry
1798 new_entry = vm_map_entry_create(map, countp);
1799 *new_entry = *entry;
1801 new_entry->start = entry->end = end;
1802 new_entry->ba.offset += (end - entry->start);
1803 if (new_entry->ba.backing_ba)
1804 atomic_add_long(&new_entry->ba.backing_ba->refs, 1);
1806 vm_map_entry_link(map, new_entry);
1808 switch(entry->maptype) {
1809 case VM_MAPTYPE_NORMAL:
1810 case VM_MAPTYPE_VPAGETABLE:
1811 if (new_entry->ba.object) {
1812 vm_object_hold(new_entry->ba.object);
1813 vm_object_reference_locked(new_entry->ba.object);
1814 vm_object_drop(new_entry->ba.object);
1823 * Asserts that the starting and ending region addresses fall within the
1824 * valid range for the map.
1826 #define VM_MAP_RANGE_CHECK(map, start, end) \
1828 if (start < vm_map_min(map)) \
1829 start = vm_map_min(map); \
1830 if (end > vm_map_max(map)) \
1831 end = vm_map_max(map); \
1837 * Used to block when an in-transition collison occurs. The map
1838 * is unlocked for the sleep and relocked before the return.
1841 vm_map_transition_wait(vm_map_t map, int relock)
1843 tsleep_interlock(map, 0);
1845 tsleep(map, PINTERLOCKED, "vment", 0);
1851 * When we do blocking operations with the map lock held it is
1852 * possible that a clip might have occured on our in-transit entry,
1853 * requiring an adjustment to the entry in our loop. These macros
1854 * help the pageable and clip_range code deal with the case. The
1855 * conditional costs virtually nothing if no clipping has occured.
1858 #define CLIP_CHECK_BACK(entry, save_start) \
1860 while (entry->start != save_start) { \
1861 entry = vm_map_rb_tree_RB_PREV(entry); \
1862 KASSERT(entry, ("bad entry clip")); \
1866 #define CLIP_CHECK_FWD(entry, save_end) \
1868 while (entry->end != save_end) { \
1869 entry = vm_map_rb_tree_RB_NEXT(entry); \
1870 KASSERT(entry, ("bad entry clip")); \
1876 * Clip the specified range and return the base entry. The
1877 * range may cover several entries starting at the returned base
1878 * and the first and last entry in the covering sequence will be
1879 * properly clipped to the requested start and end address.
1881 * If no holes are allowed you should pass the MAP_CLIP_NO_HOLES
1884 * The MAP_ENTRY_IN_TRANSITION flag will be set for the entries
1885 * covered by the requested range.
1887 * The map must be exclusively locked on entry and will remain locked
1888 * on return. If no range exists or the range contains holes and you
1889 * specified that no holes were allowed, NULL will be returned. This
1890 * routine may temporarily unlock the map in order avoid a deadlock when
1895 vm_map_clip_range(vm_map_t map, vm_offset_t start, vm_offset_t end,
1896 int *countp, int flags)
1898 vm_map_entry_t start_entry;
1899 vm_map_entry_t entry;
1900 vm_map_entry_t next;
1903 * Locate the entry and effect initial clipping. The in-transition
1904 * case does not occur very often so do not try to optimize it.
1907 if (vm_map_lookup_entry(map, start, &start_entry) == FALSE)
1909 entry = start_entry;
1910 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1911 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1912 ++mycpu->gd_cnt.v_intrans_coll;
1913 ++mycpu->gd_cnt.v_intrans_wait;
1914 vm_map_transition_wait(map, 1);
1916 * entry and/or start_entry may have been clipped while
1917 * we slept, or may have gone away entirely. We have
1918 * to restart from the lookup.
1924 * Since we hold an exclusive map lock we do not have to restart
1925 * after clipping, even though clipping may block in zalloc.
1927 vm_map_clip_start(map, entry, start, countp);
1928 vm_map_clip_end(map, entry, end, countp);
1929 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
1932 * Scan entries covered by the range. When working on the next
1933 * entry a restart need only re-loop on the current entry which
1934 * we have already locked, since 'next' may have changed. Also,
1935 * even though entry is safe, it may have been clipped so we
1936 * have to iterate forwards through the clip after sleeping.
1939 next = vm_map_rb_tree_RB_NEXT(entry);
1940 if (next == NULL || next->start >= end)
1942 if (flags & MAP_CLIP_NO_HOLES) {
1943 if (next->start > entry->end) {
1944 vm_map_unclip_range(map, start_entry,
1945 start, entry->end, countp, flags);
1950 if (next->eflags & MAP_ENTRY_IN_TRANSITION) {
1951 vm_offset_t save_end = entry->end;
1952 next->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1953 ++mycpu->gd_cnt.v_intrans_coll;
1954 ++mycpu->gd_cnt.v_intrans_wait;
1955 vm_map_transition_wait(map, 1);
1958 * clips might have occured while we blocked.
1960 CLIP_CHECK_FWD(entry, save_end);
1961 CLIP_CHECK_BACK(start_entry, start);
1966 * No restart necessary even though clip_end may block, we
1967 * are holding the map lock.
1969 vm_map_clip_end(map, next, end, countp);
1970 next->eflags |= MAP_ENTRY_IN_TRANSITION;
1973 if (flags & MAP_CLIP_NO_HOLES) {
1974 if (entry->end != end) {
1975 vm_map_unclip_range(map, start_entry,
1976 start, entry->end, countp, flags);
1980 return(start_entry);
1984 * Undo the effect of vm_map_clip_range(). You should pass the same
1985 * flags and the same range that you passed to vm_map_clip_range().
1986 * This code will clear the in-transition flag on the entries and
1987 * wake up anyone waiting. This code will also simplify the sequence
1988 * and attempt to merge it with entries before and after the sequence.
1990 * The map must be locked on entry and will remain locked on return.
1992 * Note that you should also pass the start_entry returned by
1993 * vm_map_clip_range(). However, if you block between the two calls
1994 * with the map unlocked please be aware that the start_entry may
1995 * have been clipped and you may need to scan it backwards to find
1996 * the entry corresponding with the original start address. You are
1997 * responsible for this, vm_map_unclip_range() expects the correct
1998 * start_entry to be passed to it and will KASSERT otherwise.
2002 vm_map_unclip_range(vm_map_t map, vm_map_entry_t start_entry,
2003 vm_offset_t start, vm_offset_t end,
2004 int *countp, int flags)
2006 vm_map_entry_t entry;
2008 entry = start_entry;
2010 KASSERT(entry->start == start, ("unclip_range: illegal base entry"));
2011 while (entry && entry->start < end) {
2012 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
2013 ("in-transition flag not set during unclip on: %p",
2015 KASSERT(entry->end <= end,
2016 ("unclip_range: tail wasn't clipped"));
2017 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
2018 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
2019 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
2022 entry = vm_map_rb_tree_RB_NEXT(entry);
2026 * Simplification does not block so there is no restart case.
2028 entry = start_entry;
2029 while (entry && entry->start < end) {
2030 vm_map_simplify_entry(map, entry, countp);
2031 entry = vm_map_rb_tree_RB_NEXT(entry);
2036 * Mark the given range as handled by a subordinate map.
2038 * This range must have been created with vm_map_find(), and no other
2039 * operations may have been performed on this range prior to calling
2042 * Submappings cannot be removed.
2047 vm_map_submap(vm_map_t map, vm_offset_t start, vm_offset_t end, vm_map_t submap)
2049 vm_map_entry_t entry;
2050 int result = KERN_INVALID_ARGUMENT;
2053 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
2056 VM_MAP_RANGE_CHECK(map, start, end);
2058 if (vm_map_lookup_entry(map, start, &entry)) {
2059 vm_map_clip_start(map, entry, start, &count);
2061 entry = vm_map_rb_tree_RB_NEXT(entry);
2063 entry = RB_MIN(vm_map_rb_tree, &map->rb_root);
2066 vm_map_clip_end(map, entry, end, &count);
2068 if ((entry->start == start) && (entry->end == end) &&
2069 ((entry->eflags & MAP_ENTRY_COW) == 0) &&
2070 (entry->ba.object == NULL)) {
2071 entry->ba.sub_map = submap;
2072 entry->maptype = VM_MAPTYPE_SUBMAP;
2073 result = KERN_SUCCESS;
2076 vm_map_entry_release(count);
2082 * Sets the protection of the specified address region in the target map.
2083 * If "set_max" is specified, the maximum protection is to be set;
2084 * otherwise, only the current protection is affected.
2086 * The protection is not applicable to submaps, but is applicable to normal
2087 * maps and maps governed by virtual page tables. For example, when operating
2088 * on a virtual page table our protection basically controls how COW occurs
2089 * on the backing object, whereas the virtual page table abstraction itself
2090 * is an abstraction for userland.
2095 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
2096 vm_prot_t new_prot, boolean_t set_max)
2098 vm_map_entry_t current;
2099 vm_map_entry_t entry;
2102 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
2105 VM_MAP_RANGE_CHECK(map, start, end);
2107 if (vm_map_lookup_entry(map, start, &entry)) {
2108 vm_map_clip_start(map, entry, start, &count);
2110 entry = vm_map_rb_tree_RB_NEXT(entry);
2112 entry = RB_MIN(vm_map_rb_tree, &map->rb_root);
2116 * Make a first pass to check for protection violations.
2119 while (current && current->start < end) {
2120 if (current->maptype == VM_MAPTYPE_SUBMAP) {
2122 vm_map_entry_release(count);
2123 return (KERN_INVALID_ARGUMENT);
2125 if ((new_prot & current->max_protection) != new_prot) {
2127 vm_map_entry_release(count);
2128 return (KERN_PROTECTION_FAILURE);
2132 * When making a SHARED+RW file mmap writable, update
2135 if (new_prot & PROT_WRITE &&
2136 (current->eflags & MAP_ENTRY_NEEDS_COPY) == 0 &&
2137 (current->maptype == VM_MAPTYPE_NORMAL ||
2138 current->maptype == VM_MAPTYPE_VPAGETABLE) &&
2139 current->ba.object &&
2140 current->ba.object->type == OBJT_VNODE) {
2143 vp = current->ba.object->handle;
2144 if (vp && vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_NOWAIT) == 0) {
2145 vfs_timestamp(&vp->v_lastwrite_ts);
2146 vsetflags(vp, VLASTWRITETS);
2150 current = vm_map_rb_tree_RB_NEXT(current);
2154 * Go back and fix up protections. [Note that clipping is not
2155 * necessary the second time.]
2159 while (current && current->start < end) {
2162 vm_map_clip_end(map, current, end, &count);
2164 old_prot = current->protection;
2166 current->max_protection = new_prot;
2167 current->protection = new_prot & old_prot;
2169 current->protection = new_prot;
2173 * Update physical map if necessary. Worry about copy-on-write
2174 * here -- CHECK THIS XXX
2176 if (current->protection != old_prot) {
2177 #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
2180 pmap_protect(map->pmap, current->start,
2182 current->protection & MASK(current));
2186 vm_map_simplify_entry(map, current, &count);
2188 current = vm_map_rb_tree_RB_NEXT(current);
2191 vm_map_entry_release(count);
2192 return (KERN_SUCCESS);
2196 * This routine traverses a processes map handling the madvise
2197 * system call. Advisories are classified as either those effecting
2198 * the vm_map_entry structure, or those effecting the underlying
2201 * The <value> argument is used for extended madvise calls.
2206 vm_map_madvise(vm_map_t map, vm_offset_t start, vm_offset_t end,
2207 int behav, off_t value)
2209 vm_map_entry_t current, entry;
2215 * Some madvise calls directly modify the vm_map_entry, in which case
2216 * we need to use an exclusive lock on the map and we need to perform
2217 * various clipping operations. Otherwise we only need a read-lock
2220 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
2224 case MADV_SEQUENTIAL:
2238 vm_map_lock_read(map);
2241 vm_map_entry_release(count);
2246 * Locate starting entry and clip if necessary.
2249 VM_MAP_RANGE_CHECK(map, start, end);
2251 if (vm_map_lookup_entry(map, start, &entry)) {
2253 vm_map_clip_start(map, entry, start, &count);
2255 entry = vm_map_rb_tree_RB_NEXT(entry);
2257 entry = RB_MIN(vm_map_rb_tree, &map->rb_root);
2262 * madvise behaviors that are implemented in the vm_map_entry.
2264 * We clip the vm_map_entry so that behavioral changes are
2265 * limited to the specified address range.
2267 for (current = entry;
2268 current && current->start < end;
2269 current = vm_map_rb_tree_RB_NEXT(current)) {
2273 if (current->maptype == VM_MAPTYPE_SUBMAP)
2276 vm_map_clip_end(map, current, end, &count);
2280 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
2282 case MADV_SEQUENTIAL:
2283 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
2286 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
2289 current->eflags |= MAP_ENTRY_NOSYNC;
2292 current->eflags &= ~MAP_ENTRY_NOSYNC;
2295 current->eflags |= MAP_ENTRY_NOCOREDUMP;
2298 current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
2302 * Set the page directory page for a map
2303 * governed by a virtual page table. Mark
2304 * the entry as being governed by a virtual
2305 * page table if it is not.
2307 * XXX the page directory page is stored
2308 * in the avail_ssize field if the map_entry.
2310 * XXX the map simplification code does not
2311 * compare this field so weird things may
2312 * happen if you do not apply this function
2313 * to the entire mapping governed by the
2314 * virtual page table.
2316 if (current->maptype != VM_MAPTYPE_VPAGETABLE) {
2320 current->aux.master_pde = value;
2321 pmap_remove(map->pmap,
2322 current->start, current->end);
2326 * Invalidate the related pmap entries, used
2327 * to flush portions of the real kernel's
2328 * pmap when the caller has removed or
2329 * modified existing mappings in a virtual
2332 * (exclusive locked map version does not
2333 * need the range interlock).
2335 pmap_remove(map->pmap,
2336 current->start, current->end);
2342 vm_map_simplify_entry(map, current, &count);
2350 * madvise behaviors that are implemented in the underlying
2353 * Since we don't clip the vm_map_entry, we have to clip
2354 * the vm_object pindex and count.
2356 * NOTE! These functions are only supported on normal maps,
2357 * except MADV_INVAL which is also supported on
2358 * virtual page tables.
2360 * NOTE! These functions only apply to the top-most object.
2361 * It is not applicable to backing objects.
2363 for (current = entry;
2364 current && current->start < end;
2365 current = vm_map_rb_tree_RB_NEXT(current)) {
2366 vm_offset_t useStart;
2368 if (current->maptype != VM_MAPTYPE_NORMAL &&
2369 (current->maptype != VM_MAPTYPE_VPAGETABLE ||
2370 behav != MADV_INVAL)) {
2374 pindex = OFF_TO_IDX(current->ba.offset);
2375 delta = atop(current->end - current->start);
2376 useStart = current->start;
2378 if (current->start < start) {
2379 pindex += atop(start - current->start);
2380 delta -= atop(start - current->start);
2383 if (current->end > end)
2384 delta -= atop(current->end - end);
2386 if ((vm_spindex_t)delta <= 0)
2389 if (behav == MADV_INVAL) {
2391 * Invalidate the related pmap entries, used
2392 * to flush portions of the real kernel's
2393 * pmap when the caller has removed or
2394 * modified existing mappings in a virtual
2397 * (shared locked map version needs the
2398 * interlock, see vm_fault()).
2400 struct vm_map_ilock ilock;
2402 KASSERT(useStart >= VM_MIN_USER_ADDRESS &&
2403 useStart + ptoa(delta) <=
2404 VM_MAX_USER_ADDRESS,
2405 ("Bad range %016jx-%016jx (%016jx)",
2406 useStart, useStart + ptoa(delta),
2408 vm_map_interlock(map, &ilock,
2410 useStart + ptoa(delta));
2411 pmap_remove(map->pmap,
2413 useStart + ptoa(delta));
2414 vm_map_deinterlock(map, &ilock);
2416 vm_object_madvise(current->ba.object,
2417 pindex, delta, behav);
2421 * Try to populate the page table. Mappings governed
2422 * by virtual page tables cannot be pre-populated
2423 * without a lot of work so don't try.
2425 if (behav == MADV_WILLNEED &&
2426 current->maptype != VM_MAPTYPE_VPAGETABLE) {
2427 pmap_object_init_pt(
2430 current->protection,
2433 (count << PAGE_SHIFT),
2434 MAP_PREFAULT_MADVISE
2438 vm_map_unlock_read(map);
2440 vm_map_entry_release(count);
2446 * Sets the inheritance of the specified address range in the target map.
2447 * Inheritance affects how the map will be shared with child maps at the
2448 * time of vm_map_fork.
2451 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
2452 vm_inherit_t new_inheritance)
2454 vm_map_entry_t entry;
2455 vm_map_entry_t temp_entry;
2458 switch (new_inheritance) {
2459 case VM_INHERIT_NONE:
2460 case VM_INHERIT_COPY:
2461 case VM_INHERIT_SHARE:
2464 return (KERN_INVALID_ARGUMENT);
2467 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
2470 VM_MAP_RANGE_CHECK(map, start, end);
2472 if (vm_map_lookup_entry(map, start, &temp_entry)) {
2474 vm_map_clip_start(map, entry, start, &count);
2475 } else if (temp_entry) {
2476 entry = vm_map_rb_tree_RB_NEXT(temp_entry);
2478 entry = RB_MIN(vm_map_rb_tree, &map->rb_root);
2481 while (entry && entry->start < end) {
2482 vm_map_clip_end(map, entry, end, &count);
2484 entry->inheritance = new_inheritance;
2486 vm_map_simplify_entry(map, entry, &count);
2488 entry = vm_map_rb_tree_RB_NEXT(entry);
2491 vm_map_entry_release(count);
2492 return (KERN_SUCCESS);
2496 * Implement the semantics of mlock
2499 vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t real_end,
2500 boolean_t new_pageable)
2502 vm_map_entry_t entry;
2503 vm_map_entry_t start_entry;
2505 int rv = KERN_SUCCESS;
2508 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
2510 VM_MAP_RANGE_CHECK(map, start, real_end);
2513 start_entry = vm_map_clip_range(map, start, end, &count,
2515 if (start_entry == NULL) {
2517 vm_map_entry_release(count);
2518 return (KERN_INVALID_ADDRESS);
2521 if (new_pageable == 0) {
2522 entry = start_entry;
2523 while (entry && entry->start < end) {
2524 vm_offset_t save_start;
2525 vm_offset_t save_end;
2528 * Already user wired or hard wired (trivial cases)
2530 if (entry->eflags & MAP_ENTRY_USER_WIRED) {
2531 entry = vm_map_rb_tree_RB_NEXT(entry);
2534 if (entry->wired_count != 0) {
2535 entry->wired_count++;
2536 entry->eflags |= MAP_ENTRY_USER_WIRED;
2537 entry = vm_map_rb_tree_RB_NEXT(entry);
2542 * A new wiring requires instantiation of appropriate
2543 * management structures and the faulting in of the
2546 if (entry->maptype == VM_MAPTYPE_NORMAL ||
2547 entry->maptype == VM_MAPTYPE_VPAGETABLE) {
2548 int copyflag = entry->eflags &
2549 MAP_ENTRY_NEEDS_COPY;
2550 if (copyflag && ((entry->protection &
2551 VM_PROT_WRITE) != 0)) {
2552 vm_map_entry_shadow(entry, 0);
2553 } else if (entry->ba.object == NULL &&
2555 vm_map_entry_allocate_object(entry);
2558 entry->wired_count++;
2559 entry->eflags |= MAP_ENTRY_USER_WIRED;
2562 * Now fault in the area. Note that vm_fault_wire()
2563 * may release the map lock temporarily, it will be
2564 * relocked on return. The in-transition
2565 * flag protects the entries.
2567 save_start = entry->start;
2568 save_end = entry->end;
2569 rv = vm_fault_wire(map, entry, TRUE, 0);
2571 CLIP_CHECK_BACK(entry, save_start);
2573 KASSERT(entry->wired_count == 1, ("bad wired_count on entry"));
2574 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2575 entry->wired_count = 0;
2576 if (entry->end == save_end)
2578 entry = vm_map_rb_tree_RB_NEXT(entry);
2580 ("bad entry clip during backout"));
2582 end = save_start; /* unwire the rest */
2586 * note that even though the entry might have been
2587 * clipped, the USER_WIRED flag we set prevents
2588 * duplication so we do not have to do a
2591 entry = vm_map_rb_tree_RB_NEXT(entry);
2595 * If we failed fall through to the unwiring section to
2596 * unwire what we had wired so far. 'end' has already
2603 * start_entry might have been clipped if we unlocked the
2604 * map and blocked. No matter how clipped it has gotten
2605 * there should be a fragment that is on our start boundary.
2607 CLIP_CHECK_BACK(start_entry, start);
2611 * Deal with the unwiring case.
2615 * This is the unwiring case. We must first ensure that the
2616 * range to be unwired is really wired down. We know there
2619 entry = start_entry;
2620 while (entry && entry->start < end) {
2621 if ((entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
2622 rv = KERN_INVALID_ARGUMENT;
2625 KASSERT(entry->wired_count != 0,
2626 ("wired count was 0 with USER_WIRED set! %p",
2628 entry = vm_map_rb_tree_RB_NEXT(entry);
2632 * Now decrement the wiring count for each region. If a region
2633 * becomes completely unwired, unwire its physical pages and
2637 * The map entries are processed in a loop, checking to
2638 * make sure the entry is wired and asserting it has a wired
2639 * count. However, another loop was inserted more-or-less in
2640 * the middle of the unwiring path. This loop picks up the
2641 * "entry" loop variable from the first loop without first
2642 * setting it to start_entry. Naturally, the secound loop
2643 * is never entered and the pages backing the entries are
2644 * never unwired. This can lead to a leak of wired pages.
2646 entry = start_entry;
2647 while (entry && entry->start < end) {
2648 KASSERT(entry->eflags & MAP_ENTRY_USER_WIRED,
2649 ("expected USER_WIRED on entry %p", entry));
2650 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2651 entry->wired_count--;
2652 if (entry->wired_count == 0)
2653 vm_fault_unwire(map, entry);
2654 entry = vm_map_rb_tree_RB_NEXT(entry);
2658 vm_map_unclip_range(map, start_entry, start, real_end, &count,
2661 vm_map_entry_release(count);
2667 * Sets the pageability of the specified address range in the target map.
2668 * Regions specified as not pageable require locked-down physical
2669 * memory and physical page maps.
2671 * The map must not be locked, but a reference must remain to the map
2672 * throughout the call.
2674 * This function may be called via the zalloc path and must properly
2675 * reserve map entries for kernel_map.
2680 vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t real_end, int kmflags)
2682 vm_map_entry_t entry;
2683 vm_map_entry_t start_entry;
2685 int rv = KERN_SUCCESS;
2688 if (kmflags & KM_KRESERVE)
2689 count = vm_map_entry_kreserve(MAP_RESERVE_COUNT);
2691 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
2693 VM_MAP_RANGE_CHECK(map, start, real_end);
2696 start_entry = vm_map_clip_range(map, start, end, &count,
2698 if (start_entry == NULL) {
2700 rv = KERN_INVALID_ADDRESS;
2703 if ((kmflags & KM_PAGEABLE) == 0) {
2707 * 1. Holding the write lock, we create any shadow or zero-fill
2708 * objects that need to be created. Then we clip each map
2709 * entry to the region to be wired and increment its wiring
2710 * count. We create objects before clipping the map entries
2711 * to avoid object proliferation.
2713 * 2. We downgrade to a read lock, and call vm_fault_wire to
2714 * fault in the pages for any newly wired area (wired_count is
2717 * Downgrading to a read lock for vm_fault_wire avoids a
2718 * possible deadlock with another process that may have faulted
2719 * on one of the pages to be wired (it would mark the page busy,
2720 * blocking us, then in turn block on the map lock that we
2721 * hold). Because of problems in the recursive lock package,
2722 * we cannot upgrade to a write lock in vm_map_lookup. Thus,
2723 * any actions that require the write lock must be done
2724 * beforehand. Because we keep the read lock on the map, the
2725 * copy-on-write status of the entries we modify here cannot
2728 entry = start_entry;
2729 while (entry && entry->start < end) {
2731 * Trivial case if the entry is already wired
2733 if (entry->wired_count) {
2734 entry->wired_count++;
2735 entry = vm_map_rb_tree_RB_NEXT(entry);
2740 * The entry is being newly wired, we have to setup
2741 * appropriate management structures. A shadow
2742 * object is required for a copy-on-write region,
2743 * or a normal object for a zero-fill region. We
2744 * do not have to do this for entries that point to sub
2745 * maps because we won't hold the lock on the sub map.
2747 if (entry->maptype == VM_MAPTYPE_NORMAL ||
2748 entry->maptype == VM_MAPTYPE_VPAGETABLE) {
2749 int copyflag = entry->eflags &
2750 MAP_ENTRY_NEEDS_COPY;
2751 if (copyflag && ((entry->protection &
2752 VM_PROT_WRITE) != 0)) {
2753 vm_map_entry_shadow(entry, 0);
2754 } else if (entry->ba.object == NULL &&
2756 vm_map_entry_allocate_object(entry);
2759 entry->wired_count++;
2760 entry = vm_map_rb_tree_RB_NEXT(entry);
2768 * HACK HACK HACK HACK
2770 * vm_fault_wire() temporarily unlocks the map to avoid
2771 * deadlocks. The in-transition flag from vm_map_clip_range
2772 * call should protect us from changes while the map is
2775 * NOTE: Previously this comment stated that clipping might
2776 * still occur while the entry is unlocked, but from
2777 * what I can tell it actually cannot.
2779 * It is unclear whether the CLIP_CHECK_*() calls
2780 * are still needed but we keep them in anyway.
2782 * HACK HACK HACK HACK
2785 entry = start_entry;
2786 while (entry && entry->start < end) {
2788 * If vm_fault_wire fails for any page we need to undo
2789 * what has been done. We decrement the wiring count
2790 * for those pages which have not yet been wired (now)
2791 * and unwire those that have (later).
2793 vm_offset_t save_start = entry->start;
2794 vm_offset_t save_end = entry->end;
2796 if (entry->wired_count == 1)
2797 rv = vm_fault_wire(map, entry, FALSE, kmflags);
2799 CLIP_CHECK_BACK(entry, save_start);
2801 KASSERT(entry->wired_count == 1,
2802 ("wired_count changed unexpectedly"));
2803 entry->wired_count = 0;
2804 if (entry->end == save_end)
2806 entry = vm_map_rb_tree_RB_NEXT(entry);
2808 ("bad entry clip during backout"));
2813 CLIP_CHECK_FWD(entry, save_end);
2814 entry = vm_map_rb_tree_RB_NEXT(entry);
2818 * If a failure occured undo everything by falling through
2819 * to the unwiring code. 'end' has already been adjusted
2823 kmflags |= KM_PAGEABLE;
2826 * start_entry is still IN_TRANSITION but may have been
2827 * clipped since vm_fault_wire() unlocks and relocks the
2828 * map. No matter how clipped it has gotten there should
2829 * be a fragment that is on our start boundary.
2831 CLIP_CHECK_BACK(start_entry, start);
2834 if (kmflags & KM_PAGEABLE) {
2836 * This is the unwiring case. We must first ensure that the
2837 * range to be unwired is really wired down. We know there
2840 entry = start_entry;
2841 while (entry && entry->start < end) {
2842 if (entry->wired_count == 0) {
2843 rv = KERN_INVALID_ARGUMENT;
2846 entry = vm_map_rb_tree_RB_NEXT(entry);
2850 * Now decrement the wiring count for each region. If a region
2851 * becomes completely unwired, unwire its physical pages and
2854 entry = start_entry;
2855 while (entry && entry->start < end) {
2856 entry->wired_count--;
2857 if (entry->wired_count == 0)
2858 vm_fault_unwire(map, entry);
2859 entry = vm_map_rb_tree_RB_NEXT(entry);
2863 vm_map_unclip_range(map, start_entry, start, real_end,
2864 &count, MAP_CLIP_NO_HOLES);
2867 if (kmflags & KM_KRESERVE)
2868 vm_map_entry_krelease(count);
2870 vm_map_entry_release(count);
2875 * Mark a newly allocated address range as wired but do not fault in
2876 * the pages. The caller is expected to load the pages into the object.
2878 * The map must be locked on entry and will remain locked on return.
2879 * No other requirements.
2882 vm_map_set_wired_quick(vm_map_t map, vm_offset_t addr, vm_size_t size,
2885 vm_map_entry_t scan;
2886 vm_map_entry_t entry;
2888 entry = vm_map_clip_range(map, addr, addr + size,
2889 countp, MAP_CLIP_NO_HOLES);
2891 while (scan && scan->start < addr + size) {
2892 KKASSERT(scan->wired_count == 0);
2893 scan->wired_count = 1;
2894 scan = vm_map_rb_tree_RB_NEXT(scan);
2896 vm_map_unclip_range(map, entry, addr, addr + size,
2897 countp, MAP_CLIP_NO_HOLES);
2901 * Push any dirty cached pages in the address range to their pager.
2902 * If syncio is TRUE, dirty pages are written synchronously.
2903 * If invalidate is TRUE, any cached pages are freed as well.
2905 * This routine is called by sys_msync()
2907 * Returns an error if any part of the specified range is not mapped.
2912 vm_map_clean(vm_map_t map, vm_offset_t start, vm_offset_t end,
2913 boolean_t syncio, boolean_t invalidate)
2915 vm_map_entry_t current;
2916 vm_map_entry_t next;
2917 vm_map_entry_t entry;
2918 vm_map_backing_t ba;
2921 vm_ooffset_t offset;
2923 vm_map_lock_read(map);
2924 VM_MAP_RANGE_CHECK(map, start, end);
2925 if (!vm_map_lookup_entry(map, start, &entry)) {
2926 vm_map_unlock_read(map);
2927 return (KERN_INVALID_ADDRESS);
2929 lwkt_gettoken(&map->token);
2932 * Make a first pass to check for holes.
2935 while (current && current->start < end) {
2936 if (current->maptype == VM_MAPTYPE_SUBMAP) {
2937 lwkt_reltoken(&map->token);
2938 vm_map_unlock_read(map);
2939 return (KERN_INVALID_ARGUMENT);
2941 next = vm_map_rb_tree_RB_NEXT(current);
2942 if (end > current->end &&
2944 current->end != next->start)) {
2945 lwkt_reltoken(&map->token);
2946 vm_map_unlock_read(map);
2947 return (KERN_INVALID_ADDRESS);
2953 pmap_remove(vm_map_pmap(map), start, end);
2956 * Make a second pass, cleaning/uncaching pages from the indicated
2960 while (current && current->start < end) {
2961 offset = current->ba.offset + (start - current->start);
2962 size = (end <= current->end ? end : current->end) - start;
2964 switch(current->maptype) {
2965 case VM_MAPTYPE_SUBMAP:
2968 vm_map_entry_t tentry;
2971 smap = current->ba.sub_map;
2972 vm_map_lock_read(smap);
2973 vm_map_lookup_entry(smap, offset, &tentry);
2974 if (tentry == NULL) {
2975 tsize = vm_map_max(smap) - offset;
2977 offset = 0 + (offset - vm_map_min(smap));
2979 tsize = tentry->end - offset;
2981 offset = tentry->ba.offset +
2982 (offset - tentry->start);
2984 vm_map_unlock_read(smap);
2989 case VM_MAPTYPE_NORMAL:
2990 case VM_MAPTYPE_VPAGETABLE:
2998 object = ba->object;
3000 vm_object_hold(object);
3006 * Note that there is absolutely no sense in writing out
3007 * anonymous objects, so we track down the vnode object
3009 * We invalidate (remove) all pages from the address space
3010 * anyway, for semantic correctness.
3012 * note: certain anonymous maps, such as MAP_NOSYNC maps,
3013 * may start out with a NULL object.
3015 * XXX do we really want to stop at the first backing store
3016 * here if there are more? XXX
3022 while (ba->backing_ba != NULL) {
3023 ba = ba->backing_ba;
3024 offset += ba->offset;
3026 if (tobj->size < OFF_TO_IDX(offset + size))
3027 size = IDX_TO_OFF(tobj->size) - offset;
3028 break; /* XXX this break is not correct */
3030 if (object != tobj) {
3032 vm_object_drop(object);
3034 vm_object_hold(object);
3038 if (object && (object->type == OBJT_VNODE) &&
3039 (current->protection & VM_PROT_WRITE) &&
3040 (object->flags & OBJ_NOMSYNC) == 0) {
3042 * Flush pages if writing is allowed, invalidate them
3043 * if invalidation requested. Pages undergoing I/O
3044 * will be ignored by vm_object_page_remove().
3046 * We cannot lock the vnode and then wait for paging
3047 * to complete without deadlocking against vm_fault.
3048 * Instead we simply call vm_object_page_remove() and
3049 * allow it to block internally on a page-by-page
3050 * basis when it encounters pages undergoing async
3055 /* no chain wait needed for vnode objects */
3056 vm_object_reference_locked(object);
3057 vn_lock(object->handle, LK_EXCLUSIVE | LK_RETRY);
3058 flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
3059 flags |= invalidate ? OBJPC_INVAL : 0;
3062 * When operating on a virtual page table just
3063 * flush the whole object. XXX we probably ought
3066 switch(current->maptype) {
3067 case VM_MAPTYPE_NORMAL:
3068 vm_object_page_clean(object,
3070 OFF_TO_IDX(offset + size + PAGE_MASK),
3073 case VM_MAPTYPE_VPAGETABLE:
3074 vm_object_page_clean(object, 0, 0, flags);
3077 vn_unlock(((struct vnode *)object->handle));
3078 vm_object_deallocate_locked(object);
3080 if (object && invalidate &&
3081 ((object->type == OBJT_VNODE) ||
3082 (object->type == OBJT_DEVICE) ||
3083 (object->type == OBJT_MGTDEVICE))) {
3085 ((object->type == OBJT_DEVICE) ||
3086 (object->type == OBJT_MGTDEVICE)) ? FALSE : TRUE;
3087 /* no chain wait needed for vnode/device objects */
3088 vm_object_reference_locked(object);
3089 switch(current->maptype) {
3090 case VM_MAPTYPE_NORMAL:
3091 vm_object_page_remove(object,
3093 OFF_TO_IDX(offset + size + PAGE_MASK),
3096 case VM_MAPTYPE_VPAGETABLE:
3097 vm_object_page_remove(object, 0, 0, clean_only);
3100 vm_object_deallocate_locked(object);
3104 vm_object_drop(object);
3105 current = vm_map_rb_tree_RB_NEXT(current);
3108 lwkt_reltoken(&map->token);
3109 vm_map_unlock_read(map);
3111 return (KERN_SUCCESS);
3115 * Make the region specified by this entry pageable.
3117 * The vm_map must be exclusively locked.
3120 vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
3122 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
3123 entry->wired_count = 0;
3124 vm_fault_unwire(map, entry);
3128 * Deallocate the given entry from the target map.
3130 * The vm_map must be exclusively locked.
3133 vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry, int *countp)
3135 vm_map_entry_unlink(map, entry);
3136 map->size -= entry->end - entry->start;
3137 vm_map_entry_dispose(map, entry, countp);
3141 * Deallocates the given address range from the target map.
3143 * The vm_map must be exclusively locked.
3146 vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end, int *countp)
3149 vm_map_entry_t entry;
3150 vm_map_entry_t first_entry;
3151 vm_offset_t hole_start;
3153 ASSERT_VM_MAP_LOCKED(map);
3154 lwkt_gettoken(&map->token);
3157 * Find the start of the region, and clip it. Set entry to point
3158 * at the first record containing the requested address or, if no
3159 * such record exists, the next record with a greater address. The
3160 * loop will run from this point until a record beyond the termination
3161 * address is encountered.
3163 * Adjust freehint[] for either the clip case or the extension case.
3165 * GGG see other GGG comment.
3167 if (vm_map_lookup_entry(map, start, &first_entry)) {
3168 entry = first_entry;
3169 vm_map_clip_start(map, entry, start, countp);
3173 entry = vm_map_rb_tree_RB_NEXT(first_entry);
3175 hole_start = first_entry->start;
3177 hole_start = first_entry->end;
3179 entry = RB_MIN(vm_map_rb_tree, &map->rb_root);
3181 hole_start = vm_map_min(map);
3183 hole_start = vm_map_max(map);
3188 * Step through all entries in this region
3190 while (entry && entry->start < end) {
3191 vm_map_entry_t next;
3193 vm_pindex_t offidxstart, offidxend, count;
3196 * If we hit an in-transition entry we have to sleep and
3197 * retry. It's easier (and not really slower) to just retry
3198 * since this case occurs so rarely and the hint is already
3199 * pointing at the right place. We have to reset the
3200 * start offset so as not to accidently delete an entry
3201 * another process just created in vacated space.
3203 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
3204 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
3205 start = entry->start;
3206 ++mycpu->gd_cnt.v_intrans_coll;
3207 ++mycpu->gd_cnt.v_intrans_wait;
3208 vm_map_transition_wait(map, 1);
3211 vm_map_clip_end(map, entry, end, countp);
3215 next = vm_map_rb_tree_RB_NEXT(entry);
3217 offidxstart = OFF_TO_IDX(entry->ba.offset);
3218 count = OFF_TO_IDX(e - s);
3220 switch(entry->maptype) {
3221 case VM_MAPTYPE_NORMAL:
3222 case VM_MAPTYPE_VPAGETABLE:
3223 case VM_MAPTYPE_SUBMAP:
3224 object = entry->ba.object;
3232 * Unwire before removing addresses from the pmap; otherwise,
3233 * unwiring will put the entries back in the pmap.
3235 * Generally speaking, doing a bulk pmap_remove() before
3236 * removing the pages from the VM object is better at
3237 * reducing unnecessary IPIs. The pmap code is now optimized
3238 * to not blindly iterate the range when pt and pd pages
3241 if (entry->wired_count != 0)
3242 vm_map_entry_unwire(map, entry);
3244 offidxend = offidxstart + count;
3246 if (object == &kernel_object) {
3247 pmap_remove(map->pmap, s, e);
3248 vm_object_hold(object);
3249 vm_object_page_remove(object, offidxstart,
3251 vm_object_drop(object);
3252 } else if (object && object->type != OBJT_DEFAULT &&
3253 object->type != OBJT_SWAP) {
3255 * vnode object routines cannot be chain-locked,
3256 * but since we aren't removing pages from the
3257 * object here we can use a shared hold.
3259 vm_object_hold_shared(object);
3260 pmap_remove(map->pmap, s, e);
3261 vm_object_drop(object);
3262 } else if (object) {
3263 vm_object_hold(object);
3264 pmap_remove(map->pmap, s, e);
3266 if (object != NULL &&
3267 object->ref_count != 1 &&
3268 (object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) ==
3270 (object->type == OBJT_DEFAULT ||
3271 object->type == OBJT_SWAP)) {
3273 * When ONEMAPPING is set we can destroy the
3274 * pages underlying the entry's range.
3276 vm_object_page_remove(object, offidxstart,
3278 if (object->type == OBJT_SWAP) {
3279 swap_pager_freespace(object,
3283 if (offidxend >= object->size &&
3284 offidxstart < object->size) {
3285 object->size = offidxstart;
3288 vm_object_drop(object);
3289 } else if (entry->maptype == VM_MAPTYPE_UKSMAP) {
3290 pmap_remove(map->pmap, s, e);
3294 * Delete the entry (which may delete the object) only after
3295 * removing all pmap entries pointing to its pages.
3296 * (Otherwise, its page frames may be reallocated, and any
3297 * modify bits will be set in the wrong object!)
3299 vm_map_entry_delete(map, entry, countp);
3304 * We either reached the end and use vm_map_max as the end
3305 * address, or we didn't and we use the next entry as the
3308 if (entry == NULL) {
3309 vm_map_freehint_hole(map, hole_start,
3310 vm_map_max(map) - hole_start);
3312 vm_map_freehint_hole(map, hole_start,
3313 entry->start - hole_start);
3316 lwkt_reltoken(&map->token);
3318 return (KERN_SUCCESS);
3322 * Remove the given address range from the target map.
3323 * This is the exported form of vm_map_delete.
3328 vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
3333 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
3335 VM_MAP_RANGE_CHECK(map, start, end);
3336 result = vm_map_delete(map, start, end, &count);
3338 vm_map_entry_release(count);
3344 * Assert that the target map allows the specified privilege on the
3345 * entire address region given. The entire region must be allocated.
3347 * The caller must specify whether the vm_map is already locked or not.
3350 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
3351 vm_prot_t protection, boolean_t have_lock)
3353 vm_map_entry_t entry;
3354 vm_map_entry_t tmp_entry;
3357 if (have_lock == FALSE)
3358 vm_map_lock_read(map);
3360 if (!vm_map_lookup_entry(map, start, &tmp_entry)) {
3361 if (have_lock == FALSE)
3362 vm_map_unlock_read(map);
3368 while (start < end) {
3369 if (entry == NULL) {
3378 if (start < entry->start) {
3383 * Check protection associated with entry.
3386 if ((entry->protection & protection) != protection) {
3390 /* go to next entry */
3392 entry = vm_map_rb_tree_RB_NEXT(entry);
3394 if (have_lock == FALSE)
3395 vm_map_unlock_read(map);
3400 * Handles the dirty work of making src_entry and dst_entry copy-on-write
3401 * after src_entry has been cloned to dst_entry.
3403 * The vm_maps must be exclusively locked.
3404 * The vm_map's token must be held.
3406 * Because the maps are locked no faults can be in progress during the
3410 vm_map_copy_entry(vm_map_t src_map, vm_map_t dst_map,
3411 vm_map_entry_t src_entry, vm_map_entry_t dst_entry)
3413 vm_object_t src_object;
3416 * Nothing to do for special map types
3418 if (dst_entry->maptype == VM_MAPTYPE_SUBMAP ||
3419 dst_entry->maptype == VM_MAPTYPE_UKSMAP) {
3422 if (src_entry->maptype == VM_MAPTYPE_SUBMAP ||
3423 src_entry->maptype == VM_MAPTYPE_UKSMAP) {
3427 if (src_entry->wired_count) {
3429 * Of course, wired down pages can't be set copy-on-write.
3430 * Cause wired pages to be copied into the new map by
3431 * simulating faults (the new pages are pageable)
3433 * Scrap ba.object (its ref-count has not yet been adjusted
3434 * so we can just NULL out the field). Remove the backing
3437 * Then call vm_fault_copy_entry() to create a new object
3438 * in dst_entry and copy the wired pages from src to dst.
3440 dst_entry->ba.object = NULL;
3441 vm_map_entry_dispose_ba(dst_entry->ba.backing_ba);
3442 dst_entry->ba.backing_ba = NULL;
3443 dst_entry->ba.backing_count = 0;
3444 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry);
3446 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
3448 * If the source entry is not already marked NEEDS_COPY
3449 * we need to write-protect the PTEs.
3451 pmap_protect(src_map->pmap,
3454 src_entry->protection & ~VM_PROT_WRITE);
3458 * dst_entry.ba_object might be stale. Update it (its
3459 * ref-count has not yet been updated so just overwrite
3462 * If there is no object then we are golden. Also, in
3463 * this situation if there are no backing_ba linkages then
3464 * we can set ba.offset to 0 for debugging convenience.
3466 * ba.offset cannot otherwise be modified because it effects
3467 * the offsets for the entire backing_ba chain.
3469 src_object = src_entry->ba.object;
3472 vm_object_hold(src_object); /* for ref & flag clr */
3473 vm_object_reference_locked(src_object);
3474 vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
3476 src_entry->eflags |= (MAP_ENTRY_COW |
3477 MAP_ENTRY_NEEDS_COPY);
3478 dst_entry->eflags |= (MAP_ENTRY_COW |
3479 MAP_ENTRY_NEEDS_COPY);
3480 KKASSERT(dst_entry->ba.offset == src_entry->ba.offset);
3481 vm_object_drop(src_object);
3483 if (dst_entry->ba.backing_ba == NULL)
3484 dst_entry->ba.offset = 0;
3488 * Normal, allow the backing_ba link depth to
3491 pmap_copy(dst_map->pmap, src_map->pmap,
3493 dst_entry->end - dst_entry->start,
3499 * Create a vmspace for a new process and its related vm_map based on an
3500 * existing vmspace. The new map inherits information from the old map
3501 * according to inheritance settings.
3503 * The source map must not be locked.
3506 static void vmspace_fork_normal_entry(vm_map_t old_map, vm_map_t new_map,
3507 vm_map_entry_t old_entry, int *countp);
3508 static void vmspace_fork_uksmap_entry(vm_map_t old_map, vm_map_t new_map,
3509 vm_map_entry_t old_entry, int *countp);
3512 vmspace_fork(struct vmspace *vm1)
3514 struct vmspace *vm2;
3515 vm_map_t old_map = &vm1->vm_map;
3517 vm_map_entry_t old_entry;
3520 lwkt_gettoken(&vm1->vm_map.token);
3521 vm_map_lock(old_map);
3523 vm2 = vmspace_alloc(vm_map_min(old_map), vm_map_max(old_map));
3524 lwkt_gettoken(&vm2->vm_map.token);
3527 * We must bump the timestamp to force any concurrent fault
3530 bcopy(&vm1->vm_startcopy, &vm2->vm_startcopy,
3531 (caddr_t)&vm1->vm_endcopy - (caddr_t)&vm1->vm_startcopy);
3532 new_map = &vm2->vm_map; /* XXX */
3533 new_map->timestamp = 1;
3535 vm_map_lock(new_map);
3537 count = old_map->nentries;
3538 count = vm_map_entry_reserve(count + MAP_RESERVE_COUNT);
3540 RB_FOREACH(old_entry, vm_map_rb_tree, &old_map->rb_root) {
3541 switch(old_entry->maptype) {
3542 case VM_MAPTYPE_SUBMAP:
3543 panic("vm_map_fork: encountered a submap");
3545 case VM_MAPTYPE_UKSMAP:
3546 vmspace_fork_uksmap_entry(old_map, new_map,
3549 case VM_MAPTYPE_NORMAL:
3550 case VM_MAPTYPE_VPAGETABLE:
3551 vmspace_fork_normal_entry(old_map, new_map,
3557 new_map->size = old_map->size;
3558 vm_map_unlock(new_map);
3559 vm_map_unlock(old_map);
3560 vm_map_entry_release(count);
3562 lwkt_reltoken(&vm2->vm_map.token);
3563 lwkt_reltoken(&vm1->vm_map.token);
3570 vmspace_fork_normal_entry(vm_map_t old_map, vm_map_t new_map,
3571 vm_map_entry_t old_entry, int *countp)
3573 vm_map_entry_t new_entry;
3574 vm_map_backing_t ba;
3579 * Any uninterrupted sequence of ba->refs == 1 in the backing_ba
3580 * list can be collapsed. It's a good time to do this check with
3581 * regards to prior forked children likely having exited or execd.
3583 * Only the specific page ranges within the object(s) specified by
3584 * the entry can be collapsed.
3586 * Once we hit ba->refs > 1, or a non-anonymous-memory object,
3587 * we're done. Even if later ba's beyond this parent ba have
3588 * a ref count of 1 the whole sub-list could be shared at the this
3589 * parent ba and so we have to stop.
3591 * We do not have to test OBJ_ONEMAPPING here (it probably won't be
3592 * set anyway due to previous sharing of the object). Also the objects
3593 * themselves might have a ref_count > 1 due to clips and forks
3594 * related to OTHER page ranges. That is, the vm_object itself might
3595 * still be associated with multiple pmaps... just not this particular
3596 * page range within the object.
3598 while ((ba = old_entry->ba.backing_ba) && ba->refs == 1) {
3599 if (ba.object->type != OBJT_DEFAULT &&
3600 ba.object->type != OBJT_SWAP) {
3603 object = vm_object_collapse(old_entry->ba.object, ba->object);
3604 if (object == old_entry->ba.object) {
3606 * Merged into base, remove intermediate ba.
3609 --old_entry->ba.backing_count;
3610 old_entry->ba.backing_ba = ba->backing_ba;
3612 ba->backing_ba->offset += ba->offset;
3613 ba->backing_ba = NULL;
3614 vm_map_entry_dispose_ba(ba);
3615 } else if (object == ba->object) {
3617 * Merged into intermediate ba, shift it into
3621 vm_object_deallocate(old_entry->ba.object);
3622 --old_entry->ba.backing_count;
3623 old_entry->ba.backing_ba = ba->backing_ba;
3624 old_entry->ba.object = ba->object;
3625 old_entry->ba.offset += ba->offset;
3627 ba->backing_ba = NULL;
3628 vm_map_entry_dispose_ba(ba);
3636 * If the backing_ba link list gets too long then fault it
3637 * all into the head object and dispose of the list. We do
3638 * this in old_entry prior to cloning in order to benefit both
3641 * We can test our fronting object's size against its
3642 * resident_page_count for a really cheap (but probably not perfect)
3643 * all-shadowed test, allowing us to disconnect the backing_ba
3646 object = old_entry->ba.object;
3647 if (old_entry->ba.backing_ba &&
3648 (old_entry->ba.backing_count >= vm_map_backing_limit ||
3649 (vm_map_backing_shadow_test && object &&
3650 object->size == object->resident_page_count))) {
3652 * If there are too many backing_ba linkages we
3653 * collapse everything into the head
3655 * This will also remove all the pte's.
3657 if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY)
3658 vm_map_entry_shadow(old_entry, 0);
3660 vm_map_entry_allocate_object(old_entry);
3661 if (vm_fault_collapse(old_map, old_entry) == KERN_SUCCESS) {
3662 ba = old_entry->ba.backing_ba;
3663 old_entry->ba.backing_ba = NULL;
3664 old_entry->ba.backing_count = 0;
3665 vm_map_entry_dispose_ba(ba);
3668 object = NULL; /* object variable is now invalid */
3673 switch (old_entry->inheritance) {
3674 case VM_INHERIT_NONE:
3676 case VM_INHERIT_SHARE:
3678 * Clone the entry as a shared entry. This will look like
3679 * shared memory across the old and the new process. We must
3680 * ensure that the object is allocated.
3682 if (old_entry->ba.object == NULL)
3683 vm_map_entry_allocate_object(old_entry);
3685 if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3687 * Create the fronting vm_map_backing for
3688 * an entry which needs a copy, plus an extra
3689 * ref because we are going to duplicate it
3692 * The call to vm_map_entry_shadow() will also clear
3695 * XXX no more collapse. Still need extra ref
3698 vm_map_entry_shadow(old_entry, 1);
3699 } else if (old_entry->ba.object) {
3701 * We will make a shared copy of the object,
3702 * and must clear OBJ_ONEMAPPING.
3704 * Optimize vnode objects. OBJ_ONEMAPPING
3705 * is non-applicable but clear it anyway,
3706 * and its terminal so we don't have to deal
3707 * with chains. Reduces SMP conflicts.
3709 * XXX assert that object.vm_object != NULL
3710 * since we allocate it above.
3712 object = old_entry->ba.object;
3713 if (object->type == OBJT_VNODE) {
3714 vm_object_reference_quick(object);
3715 vm_object_clear_flag(object,
3718 vm_object_hold(object);
3719 vm_object_reference_locked(object);
3720 vm_object_clear_flag(object, OBJ_ONEMAPPING);
3721 vm_object_drop(object);
3726 * Clone the entry. We've already bumped the ref on
3727 * the vm_object for our new entry.
3729 new_entry = vm_map_entry_create(new_map, countp);
3730 *new_entry = *old_entry;
3732 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
3733 new_entry->wired_count = 0;
3734 if (new_entry->ba.backing_ba)
3735 atomic_add_long(&new_entry->ba.backing_ba->refs, 1);
3738 * Insert the entry into the new map -- we know we're
3739 * inserting at the end of the new map.
3741 vm_map_entry_link(new_map, new_entry);
3744 * Update the physical map
3746 pmap_copy(new_map->pmap, old_map->pmap,
3748 (old_entry->end - old_entry->start),
3751 case VM_INHERIT_COPY:
3753 * Clone the entry and link the copy into the new map.
3755 * Note that ref-counting adjustment for old_entry->ba.object
3756 * (if it isn't a special map that is) is handled by
3757 * vm_map_copy_entry().
3759 new_entry = vm_map_entry_create(new_map, countp);
3760 *new_entry = *old_entry;
3762 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
3763 new_entry->wired_count = 0;
3764 if (new_entry->ba.backing_ba)
3765 atomic_add_long(&new_entry->ba.backing_ba->refs, 1);
3767 vm_map_entry_link(new_map, new_entry);
3770 * This does the actual dirty work of making both entries
3771 * copy-on-write, and will also handle the fronting object.
3773 vm_map_copy_entry(old_map, new_map, old_entry, new_entry);
3779 * When forking user-kernel shared maps, the map might change in the
3780 * child so do not try to copy the underlying pmap entries.
3784 vmspace_fork_uksmap_entry(vm_map_t old_map, vm_map_t new_map,
3785 vm_map_entry_t old_entry, int *countp)
3787 vm_map_entry_t new_entry;
3789 new_entry = vm_map_entry_create(new_map, countp);
3790 *new_entry = *old_entry;
3792 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
3793 new_entry->wired_count = 0;
3794 if (new_entry->ba.backing_ba)
3795 atomic_add_long(&new_entry->ba.backing_ba->refs, 1);
3797 vm_map_entry_link(new_map, new_entry);
3801 * Create an auto-grow stack entry
3806 vm_map_stack (vm_map_t map, vm_offset_t *addrbos, vm_size_t max_ssize,
3807 int flags, vm_prot_t prot, vm_prot_t max, int cow)
3809 vm_map_entry_t prev_entry;
3810 vm_map_entry_t next;
3811 vm_size_t init_ssize;
3814 vm_offset_t tmpaddr;
3816 cow |= MAP_IS_STACK;
3818 if (max_ssize < sgrowsiz)
3819 init_ssize = max_ssize;
3821 init_ssize = sgrowsiz;
3823 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
3827 * Find space for the mapping
3829 if ((flags & (MAP_FIXED | MAP_TRYFIXED)) == 0) {
3830 if (vm_map_findspace(map, *addrbos, max_ssize, 1,
3833 vm_map_entry_release(count);
3834 return (KERN_NO_SPACE);
3839 /* If addr is already mapped, no go */
3840 if (vm_map_lookup_entry(map, *addrbos, &prev_entry)) {
3842 vm_map_entry_release(count);
3843 return (KERN_NO_SPACE);
3847 /* XXX already handled by kern_mmap() */
3848 /* If we would blow our VMEM resource limit, no go */
3849 if (map->size + init_ssize >
3850 curproc->p_rlimit[RLIMIT_VMEM].rlim_cur) {
3852 vm_map_entry_release(count);
3853 return (KERN_NO_SPACE);
3858 * If we can't accomodate max_ssize in the current mapping,
3859 * no go. However, we need to be aware that subsequent user
3860 * mappings might map into the space we have reserved for
3861 * stack, and currently this space is not protected.
3863 * Hopefully we will at least detect this condition
3864 * when we try to grow the stack.
3867 next = vm_map_rb_tree_RB_NEXT(prev_entry);
3869 next = RB_MIN(vm_map_rb_tree, &map->rb_root);
3871 if (next && next->start < *addrbos + max_ssize) {
3873 vm_map_entry_release(count);
3874 return (KERN_NO_SPACE);
3878 * We initially map a stack of only init_ssize. We will
3879 * grow as needed later. Since this is to be a grow
3880 * down stack, we map at the top of the range.
3882 * Note: we would normally expect prot and max to be
3883 * VM_PROT_ALL, and cow to be 0. Possibly we should
3884 * eliminate these as input parameters, and just
3885 * pass these values here in the insert call.
3887 rv = vm_map_insert(map, &count, NULL, NULL,
3888 0, *addrbos + max_ssize - init_ssize,
3889 *addrbos + max_ssize,
3891 VM_SUBSYS_STACK, prot, max, cow);
3893 /* Now set the avail_ssize amount */
3894 if (rv == KERN_SUCCESS) {
3896 next = vm_map_rb_tree_RB_NEXT(prev_entry);
3898 next = RB_MIN(vm_map_rb_tree, &map->rb_root);
3899 if (prev_entry != NULL) {
3900 vm_map_clip_end(map,
3902 *addrbos + max_ssize - init_ssize,
3905 if (next->end != *addrbos + max_ssize ||
3906 next->start != *addrbos + max_ssize - init_ssize){
3907 panic ("Bad entry start/end for new stack entry");
3909 next->aux.avail_ssize = max_ssize - init_ssize;
3914 vm_map_entry_release(count);
3919 * Attempts to grow a vm stack entry. Returns KERN_SUCCESS if the
3920 * desired address is already mapped, or if we successfully grow
3921 * the stack. Also returns KERN_SUCCESS if addr is outside the
3922 * stack range (this is strange, but preserves compatibility with
3923 * the grow function in vm_machdep.c).
3928 vm_map_growstack (vm_map_t map, vm_offset_t addr)
3930 vm_map_entry_t prev_entry;
3931 vm_map_entry_t stack_entry;
3932 vm_map_entry_t next;
3938 int rv = KERN_SUCCESS;
3940 int use_read_lock = 1;
3946 lp = curthread->td_lwp;
3947 p = curthread->td_proc;
3948 KKASSERT(lp != NULL);
3949 vm = lp->lwp_vmspace;
3952 * Growstack is only allowed on the current process. We disallow
3953 * other use cases, e.g. trying to access memory via procfs that
3954 * the stack hasn't grown into.
3956 if (map != &vm->vm_map) {
3957 return KERN_FAILURE;
3960 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
3963 vm_map_lock_read(map);
3968 * If addr is already in the entry range, no need to grow.
3969 * prev_entry returns NULL if addr is at the head.
3971 if (vm_map_lookup_entry(map, addr, &prev_entry))
3974 stack_entry = vm_map_rb_tree_RB_NEXT(prev_entry);
3976 stack_entry = RB_MIN(vm_map_rb_tree, &map->rb_root);
3978 if (stack_entry == NULL)
3980 if (prev_entry == NULL)
3981 end = stack_entry->start - stack_entry->aux.avail_ssize;
3983 end = prev_entry->end;
3986 * This next test mimics the old grow function in vm_machdep.c.
3987 * It really doesn't quite make sense, but we do it anyway
3988 * for compatibility.
3990 * If not growable stack, return success. This signals the
3991 * caller to proceed as he would normally with normal vm.
3993 if (stack_entry->aux.avail_ssize < 1 ||
3994 addr >= stack_entry->start ||
3995 addr < stack_entry->start - stack_entry->aux.avail_ssize) {
3999 /* Find the minimum grow amount */
4000 grow_amount = roundup (stack_entry->start - addr, PAGE_SIZE);
4001 if (grow_amount > stack_entry->aux.avail_ssize) {
4007 * If there is no longer enough space between the entries
4008 * nogo, and adjust the available space. Note: this
4009 * should only happen if the user has mapped into the
4010 * stack area after the stack was created, and is
4011 * probably an error.
4013 * This also effectively destroys any guard page the user
4014 * might have intended by limiting the stack size.
4016 if (grow_amount > stack_entry->start - end) {
4017 if (use_read_lock && vm_map_lock_upgrade(map)) {
4023 stack_entry->aux.avail_ssize = stack_entry->start - end;
4028 is_procstack = addr >= (vm_offset_t)vm->vm_maxsaddr;
4030 /* If this is the main process stack, see if we're over the
4033 if (is_procstack && (vm->vm_ssize + grow_amount >
4034 p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
4039 /* Round up the grow amount modulo SGROWSIZ */
4040 grow_amount = roundup (grow_amount, sgrowsiz);
4041 if (grow_amount > stack_entry->aux.avail_ssize) {
4042 grow_amount = stack_entry->aux.avail_ssize;
4044 if (is_procstack && (vm->vm_ssize + grow_amount >
4045 p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
4046 grow_amount = p->p_rlimit[RLIMIT_STACK].rlim_cur - vm->vm_ssize;
4049 /* If we would blow our VMEM resource limit, no go */
4050 if (map->size + grow_amount > p->p_rlimit[RLIMIT_VMEM].rlim_cur) {
4055 if (use_read_lock && vm_map_lock_upgrade(map)) {
4062 /* Get the preliminary new entry start value */
4063 addr = stack_entry->start - grow_amount;
4065 /* If this puts us into the previous entry, cut back our growth
4066 * to the available space. Also, see the note above.
4069 stack_entry->aux.avail_ssize = stack_entry->start - end;
4073 rv = vm_map_insert(map, &count, NULL, NULL,
4074 0, addr, stack_entry->start,
4076 VM_SUBSYS_STACK, VM_PROT_ALL, VM_PROT_ALL, 0);
4078 /* Adjust the available stack space by the amount we grew. */
4079 if (rv == KERN_SUCCESS) {
4081 vm_map_clip_end(map, prev_entry, addr, &count);
4082 next = vm_map_rb_tree_RB_NEXT(prev_entry);
4084 next = RB_MIN(vm_map_rb_tree, &map->rb_root);
4086 if (next->end != stack_entry->start ||
4087 next->start != addr) {
4088 panic ("Bad stack grow start/end in new stack entry");
4090 next->aux.avail_ssize =
4091 stack_entry->aux.avail_ssize -
4092 (next->end - next->start);
4094 vm->vm_ssize += next->end -
4099 if (map->flags & MAP_WIREFUTURE)
4100 vm_map_unwire(map, next->start, next->end, FALSE);
4105 vm_map_unlock_read(map);
4108 vm_map_entry_release(count);
4113 * Unshare the specified VM space for exec. If other processes are
4114 * mapped to it, then create a new one. The new vmspace is null.
4119 vmspace_exec(struct proc *p, struct vmspace *vmcopy)
4121 struct vmspace *oldvmspace = p->p_vmspace;
4122 struct vmspace *newvmspace;
4123 vm_map_t map = &p->p_vmspace->vm_map;
4126 * If we are execing a resident vmspace we fork it, otherwise
4127 * we create a new vmspace. Note that exitingcnt is not
4128 * copied to the new vmspace.
4130 lwkt_gettoken(&oldvmspace->vm_map.token);
4132 newvmspace = vmspace_fork(vmcopy);
4133 lwkt_gettoken(&newvmspace->vm_map.token);
4135 newvmspace = vmspace_alloc(vm_map_min(map), vm_map_max(map));
4136 lwkt_gettoken(&newvmspace->vm_map.token);
4137 bcopy(&oldvmspace->vm_startcopy, &newvmspace->vm_startcopy,
4138 (caddr_t)&oldvmspace->vm_endcopy -
4139 (caddr_t)&oldvmspace->vm_startcopy);
4143 * Finish initializing the vmspace before assigning it
4144 * to the process. The vmspace will become the current vmspace
4147 pmap_pinit2(vmspace_pmap(newvmspace));
4148 pmap_replacevm(p, newvmspace, 0);
4149 lwkt_reltoken(&newvmspace->vm_map.token);
4150 lwkt_reltoken(&oldvmspace->vm_map.token);
4151 vmspace_rel(oldvmspace);
4155 * Unshare the specified VM space for forcing COW. This
4156 * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
4159 vmspace_unshare(struct proc *p)
4161 struct vmspace *oldvmspace = p->p_vmspace;
4162 struct vmspace *newvmspace;
4164 lwkt_gettoken(&oldvmspace->vm_map.token);
4165 if (vmspace_getrefs(oldvmspace) == 1) {
4166 lwkt_reltoken(&oldvmspace->vm_map.token);
4169 newvmspace = vmspace_fork(oldvmspace);
4170 lwkt_gettoken(&newvmspace->vm_map.token);
4171 pmap_pinit2(vmspace_pmap(newvmspace));
4172 pmap_replacevm(p, newvmspace, 0);
4173 lwkt_reltoken(&newvmspace->vm_map.token);
4174 lwkt_reltoken(&oldvmspace->vm_map.token);
4175 vmspace_rel(oldvmspace);
4179 * vm_map_hint: return the beginning of the best area suitable for
4180 * creating a new mapping with "prot" protection.
4185 vm_map_hint(struct proc *p, vm_offset_t addr, vm_prot_t prot)
4187 struct vmspace *vms = p->p_vmspace;
4188 struct rlimit limit;
4192 * Acquire datasize limit for mmap() operation,
4193 * calculate nearest power of 2.
4195 if (kern_getrlimit(RLIMIT_DATA, &limit))
4196 limit.rlim_cur = maxdsiz;
4197 dsiz = limit.rlim_cur;
4199 if (!randomize_mmap || addr != 0) {
4201 * Set a reasonable start point for the hint if it was
4202 * not specified or if it falls within the heap space.
4203 * Hinted mmap()s do not allocate out of the heap space.
4206 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
4207 addr < round_page((vm_offset_t)vms->vm_daddr + dsiz))) {
4208 addr = round_page((vm_offset_t)vms->vm_daddr + dsiz);
4215 * randomize_mmap && addr == 0. For now randomize the
4216 * address within a dsiz range beyond the data limit.
4218 addr = (vm_offset_t)vms->vm_daddr + dsiz;
4220 addr += (karc4random64() & 0x7FFFFFFFFFFFFFFFLU) % dsiz;
4221 return (round_page(addr));
4225 * Finds the VM object, offset, and protection for a given virtual address
4226 * in the specified map, assuming a page fault of the type specified.
4228 * Leaves the map in question locked for read; return values are guaranteed
4229 * until a vm_map_lookup_done call is performed. Note that the map argument
4230 * is in/out; the returned map must be used in the call to vm_map_lookup_done.
4232 * A handle (out_entry) is returned for use in vm_map_lookup_done, to make
4235 * If a lookup is requested with "write protection" specified, the map may
4236 * be changed to perform virtual copying operations, although the data
4237 * referenced will remain the same.
4242 vm_map_lookup(vm_map_t *var_map, /* IN/OUT */
4244 vm_prot_t fault_typea,
4245 vm_map_entry_t *out_entry, /* OUT */
4246 struct vm_map_backing **bap, /* OUT */
4247 vm_pindex_t *pindex, /* OUT */
4248 vm_prot_t *out_prot, /* OUT */
4249 int *wflags) /* OUT */
4251 vm_map_entry_t entry;
4252 vm_map_t map = *var_map;
4254 vm_prot_t fault_type = fault_typea;
4255 int use_read_lock = 1;
4256 int rv = KERN_SUCCESS;
4258 thread_t td = curthread;
4261 * vm_map_entry_reserve() implements an important mitigation
4262 * against mmap() span running the kernel out of vm_map_entry
4263 * structures, but it can also cause an infinite call recursion.
4264 * Use td_nest_count to prevent an infinite recursion (allows
4265 * the vm_map code to dig into the pcpu vm_map_entry reserve).
4268 if (td->td_nest_count == 0) {
4269 ++td->td_nest_count;
4270 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
4271 --td->td_nest_count;
4275 vm_map_lock_read(map);
4280 * Always do a full lookup. The hint doesn't get us much anymore
4281 * now that the map is RB'd.
4288 vm_map_entry_t tmp_entry;
4290 if (!vm_map_lookup_entry(map, vaddr, &tmp_entry)) {
4291 rv = KERN_INVALID_ADDRESS;
4301 if (entry->maptype == VM_MAPTYPE_SUBMAP) {
4302 vm_map_t old_map = map;
4304 *var_map = map = entry->ba.sub_map;
4306 vm_map_unlock_read(old_map);
4308 vm_map_unlock(old_map);
4314 * Check whether this task is allowed to have this page.
4315 * Note the special case for MAP_ENTRY_COW pages with an override.
4316 * This is to implement a forced COW for debuggers.
4318 if (fault_type & VM_PROT_OVERRIDE_WRITE)
4319 prot = entry->max_protection;
4321 prot = entry->protection;
4323 fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
4324 if ((fault_type & prot) != fault_type) {
4325 rv = KERN_PROTECTION_FAILURE;
4329 if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
4330 (entry->eflags & MAP_ENTRY_COW) &&
4331 (fault_type & VM_PROT_WRITE) &&
4332 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
4333 rv = KERN_PROTECTION_FAILURE;
4338 * If this page is not pageable, we have to get it for all possible
4342 if (entry->wired_count) {
4343 *wflags |= FW_WIRED;
4344 prot = fault_type = entry->protection;
4348 * Virtual page tables may need to update the accessed (A) bit
4349 * in a page table entry. Upgrade the fault to a write fault for
4350 * that case if the map will support it. If the map does not support
4351 * it the page table entry simply will not be updated.
4353 if (entry->maptype == VM_MAPTYPE_VPAGETABLE) {
4354 if (prot & VM_PROT_WRITE)
4355 fault_type |= VM_PROT_WRITE;
4358 if (curthread->td_lwp && curthread->td_lwp->lwp_vmspace &&
4359 pmap_emulate_ad_bits(&curthread->td_lwp->lwp_vmspace->vm_pmap)) {
4360 if ((prot & VM_PROT_WRITE) == 0)
4361 fault_type |= VM_PROT_WRITE;
4365 * Only NORMAL and VPAGETABLE maps are object-based. UKSMAPs are not.
4367 if (entry->maptype != VM_MAPTYPE_NORMAL &&
4368 entry->maptype != VM_MAPTYPE_VPAGETABLE) {
4374 * If the entry was copy-on-write, we either ...
4376 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
4378 * If we want to write the page, we may as well handle that
4379 * now since we've got the map locked.
4381 * If we don't need to write the page, we just demote the
4382 * permissions allowed.
4384 if (fault_type & VM_PROT_WRITE) {
4386 * Not allowed if TDF_NOFAULT is set as the shadowing
4387 * operation can deadlock against the faulting
4388 * function due to the copy-on-write.
4390 if (curthread->td_flags & TDF_NOFAULT) {
4391 rv = KERN_FAILURE_NOFAULT;
4396 * Make a new vm_map_backing + object, and place it
4397 * in the object chain. Note that no new references
4398 * have appeared -- one just moved from the map to
4401 if (use_read_lock && vm_map_lock_upgrade(map)) {
4407 vm_map_entry_shadow(entry, 0);
4408 *wflags |= FW_DIDCOW;
4411 * We're attempting to read a copy-on-write page --
4412 * don't allow writes.
4414 prot &= ~VM_PROT_WRITE;
4419 * Create an object if necessary. This code also handles
4420 * partitioning large entries to improve vm_fault performance.
4422 if (entry->ba.object == NULL && !map->system_map) {
4423 if (use_read_lock && vm_map_lock_upgrade(map)) {
4431 * Partition large entries, giving each its own VM object,
4432 * to improve concurrent fault performance. This is only
4433 * applicable to userspace.
4435 if (map != &kernel_map &&
4436 entry->maptype == VM_MAPTYPE_NORMAL &&
4437 ((entry->start ^ entry->end) & ~MAP_ENTRY_PARTITION_MASK) &&
4438 vm_map_partition_enable) {
4439 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
4440 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
4441 ++mycpu->gd_cnt.v_intrans_coll;
4442 ++mycpu->gd_cnt.v_intrans_wait;
4443 vm_map_transition_wait(map, 0);
4446 vm_map_entry_partition(map, entry, vaddr, &count);
4448 vm_map_entry_allocate_object(entry);
4452 * Return the object/offset from this entry. If the entry was
4453 * copy-on-write or empty, it has been fixed up.
4458 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->ba.offset);
4461 * Return whether this is the only map sharing this data. On
4462 * success we return with a read lock held on the map. On failure
4463 * we return with the map unlocked.
4467 if (rv == KERN_SUCCESS) {
4468 if (use_read_lock == 0)
4469 vm_map_lock_downgrade(map);
4470 } else if (use_read_lock) {
4471 vm_map_unlock_read(map);
4476 vm_map_entry_release(count);
4482 * Releases locks acquired by a vm_map_lookup()
4483 * (according to the handle returned by that lookup).
4485 * No other requirements.
4488 vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry, int count)
4491 * Unlock the main-level map
4493 vm_map_unlock_read(map);
4495 vm_map_entry_release(count);
4499 vm_map_entry_partition(vm_map_t map, vm_map_entry_t entry,
4500 vm_offset_t vaddr, int *countp)
4502 vaddr &= ~MAP_ENTRY_PARTITION_MASK;
4503 vm_map_clip_start(map, entry, vaddr, countp);
4504 vaddr += MAP_ENTRY_PARTITION_SIZE;
4505 vm_map_clip_end(map, entry, vaddr, countp);
4509 * Quick hack, needs some help to make it more SMP friendly.
4512 vm_map_interlock(vm_map_t map, struct vm_map_ilock *ilock,
4513 vm_offset_t ran_beg, vm_offset_t ran_end)
4515 struct vm_map_ilock *scan;
4517 ilock->ran_beg = ran_beg;
4518 ilock->ran_end = ran_end;
4521 spin_lock(&map->ilock_spin);
4523 for (scan = map->ilock_base; scan; scan = scan->next) {
4524 if (ran_end > scan->ran_beg && ran_beg < scan->ran_end) {
4525 scan->flags |= ILOCK_WAITING;
4526 ssleep(scan, &map->ilock_spin, 0, "ilock", 0);
4530 ilock->next = map->ilock_base;
4531 map->ilock_base = ilock;
4532 spin_unlock(&map->ilock_spin);
4536 vm_map_deinterlock(vm_map_t map, struct vm_map_ilock *ilock)
4538 struct vm_map_ilock *scan;
4539 struct vm_map_ilock **scanp;
4541 spin_lock(&map->ilock_spin);
4542 scanp = &map->ilock_base;
4543 while ((scan = *scanp) != NULL) {
4544 if (scan == ilock) {
4545 *scanp = ilock->next;
4546 spin_unlock(&map->ilock_spin);
4547 if (ilock->flags & ILOCK_WAITING)
4551 scanp = &scan->next;
4553 spin_unlock(&map->ilock_spin);
4554 panic("vm_map_deinterlock: missing ilock!");
4557 #include "opt_ddb.h"
4559 #include <ddb/ddb.h>
4564 DB_SHOW_COMMAND(map, vm_map_print)
4567 /* XXX convert args. */
4568 vm_map_t map = (vm_map_t)addr;
4569 boolean_t full = have_addr;
4571 vm_map_entry_t entry;
4573 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
4575 (void *)map->pmap, map->nentries, map->timestamp);
4578 if (!full && db_indent)
4582 RB_FOREACH(entry, vm_map_rb_tree, &map->rb_root) {
4583 db_iprintf("map entry %p: start=%p, end=%p\n",
4584 (void *)entry, (void *)entry->start, (void *)entry->end);
4587 static char *inheritance_name[4] =
4588 {"share", "copy", "none", "donate_copy"};
4590 db_iprintf(" prot=%x/%x/%s",
4592 entry->max_protection,
4593 inheritance_name[(int)(unsigned char)
4594 entry->inheritance]);
4595 if (entry->wired_count != 0)
4596 db_printf(", wired");
4598 switch(entry->maptype) {
4599 case VM_MAPTYPE_SUBMAP:
4600 /* XXX no %qd in kernel. Truncate entry->ba.offset. */
4601 db_printf(", share=%p, offset=0x%lx\n",
4602 (void *)entry->ba.sub_map,
4603 (long)entry->ba.offset);
4607 vm_map_print((db_expr_t)(intptr_t)entry->ba.sub_map,
4611 case VM_MAPTYPE_NORMAL:
4612 case VM_MAPTYPE_VPAGETABLE:
4613 /* XXX no %qd in kernel. Truncate entry->ba.offset. */
4614 db_printf(", object=%p, offset=0x%lx",
4615 (void *)entry->ba.object,
4616 (long)entry->ba.offset);
4617 if (entry->eflags & MAP_ENTRY_COW)
4618 db_printf(", copy (%s)",
4619 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
4623 if (entry->ba.object) {
4625 vm_object_print((db_expr_t)(intptr_t)
4632 case VM_MAPTYPE_UKSMAP:
4633 db_printf(", uksmap=%p, offset=0x%lx",
4634 (void *)entry->ba.uksmap,
4635 (long)entry->ba.offset);
4636 if (entry->eflags & MAP_ENTRY_COW)
4637 db_printf(", copy (%s)",
4638 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
4654 DB_SHOW_COMMAND(procvm, procvm)
4659 p = (struct proc *) addr;
4664 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
4665 (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
4666 (void *)vmspace_pmap(p->p_vmspace));
4668 vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);