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 static struct objcache *vmspace_cache;
131 * per-cpu page table cross mappings are initialized in early boot
132 * and might require a considerable number of vm_map_entry structures.
134 #define MAPENTRYBSP_CACHE (MAXCPU+1)
135 #define MAPENTRYAP_CACHE 8
138 * Partioning threaded programs with large anonymous memory areas can
139 * improve concurrent fault performance.
141 #define MAP_ENTRY_PARTITION_SIZE ((vm_offset_t)(32 * 1024 * 1024))
142 #define MAP_ENTRY_PARTITION_MASK (MAP_ENTRY_PARTITION_SIZE - 1)
144 #define VM_MAP_ENTRY_WITHIN_PARTITION(entry) \
145 ((((entry)->start ^ (entry)->end) & ~MAP_ENTRY_PARTITION_MASK) == 0)
147 static struct vm_zone mapentzone_store;
148 static vm_zone_t mapentzone;
150 static struct vm_map_entry map_entry_init[MAX_MAPENT];
151 static struct vm_map_entry cpu_map_entry_init_bsp[MAPENTRYBSP_CACHE];
152 static struct vm_map_entry cpu_map_entry_init_ap[MAXCPU][MAPENTRYAP_CACHE];
154 static int randomize_mmap;
155 SYSCTL_INT(_vm, OID_AUTO, randomize_mmap, CTLFLAG_RW, &randomize_mmap, 0,
156 "Randomize mmap offsets");
157 static int vm_map_relock_enable = 1;
158 SYSCTL_INT(_vm, OID_AUTO, map_relock_enable, CTLFLAG_RW,
159 &vm_map_relock_enable, 0, "insert pop pgtable optimization");
160 static int vm_map_partition_enable = 1;
161 SYSCTL_INT(_vm, OID_AUTO, map_partition_enable, CTLFLAG_RW,
162 &vm_map_partition_enable, 0, "Break up larger vm_map_entry's");
164 static void vmspace_drop_notoken(struct vmspace *vm);
165 static void vm_map_entry_shadow(vm_map_entry_t entry, int addref);
166 static vm_map_entry_t vm_map_entry_create(vm_map_t map, int *);
167 static void vm_map_entry_dispose (vm_map_t map, vm_map_entry_t entry, int *);
168 static void _vm_map_clip_end (vm_map_t, vm_map_entry_t, vm_offset_t, int *);
169 static void _vm_map_clip_start (vm_map_t, vm_map_entry_t, vm_offset_t, int *);
170 static void vm_map_entry_delete (vm_map_t, vm_map_entry_t, int *);
171 static void vm_map_entry_unwire (vm_map_t, vm_map_entry_t);
172 static void vm_map_copy_entry (vm_map_t, vm_map_t, vm_map_entry_t,
174 static void vm_map_unclip_range (vm_map_t map, vm_map_entry_t start_entry,
175 vm_offset_t start, vm_offset_t end, int *countp, int flags);
176 static void vm_map_entry_partition(vm_map_t map, vm_map_entry_t entry,
177 vm_offset_t vaddr, int *countp);
180 * Initialize the vm_map module. Must be called before any other vm_map
183 * Map and entry structures are allocated from the general purpose
184 * memory pool with some exceptions:
186 * - The kernel map is allocated statically.
187 * - Initial kernel map entries are allocated out of a static pool.
188 * - We must set ZONE_SPECIAL here or the early boot code can get
189 * stuck if there are >63 cores.
191 * These restrictions are necessary since malloc() uses the
192 * maps and requires map entries.
194 * Called from the low level boot code only.
199 mapentzone = &mapentzone_store;
200 zbootinit(mapentzone, "MAP ENTRY", sizeof (struct vm_map_entry),
201 map_entry_init, MAX_MAPENT);
202 mapentzone_store.zflags |= ZONE_SPECIAL;
206 * Called prior to any vmspace allocations.
208 * Called from the low level boot code only.
213 vmspace_cache = objcache_create_mbacked(M_VMSPACE,
214 sizeof(struct vmspace),
216 vmspace_ctor, vmspace_dtor,
218 zinitna(mapentzone, NULL, 0, 0, ZONE_USE_RESERVE | ZONE_SPECIAL);
224 * objcache support. We leave the pmap root cached as long as possible
225 * for performance reasons.
229 vmspace_ctor(void *obj, void *privdata, int ocflags)
231 struct vmspace *vm = obj;
233 bzero(vm, sizeof(*vm));
234 vm->vm_refcnt = VM_REF_DELETED;
241 vmspace_dtor(void *obj, void *privdata)
243 struct vmspace *vm = obj;
245 KKASSERT(vm->vm_refcnt == VM_REF_DELETED);
246 pmap_puninit(vmspace_pmap(vm));
250 * Red black tree functions
252 * The caller must hold the related map lock.
254 static int rb_vm_map_compare(vm_map_entry_t a, vm_map_entry_t b);
255 RB_GENERATE(vm_map_rb_tree, vm_map_entry, rb_entry, rb_vm_map_compare);
257 /* a->start is address, and the only field has to be initialized */
259 rb_vm_map_compare(vm_map_entry_t a, vm_map_entry_t b)
261 if (a->start < b->start)
263 else if (a->start > b->start)
269 * Initialize vmspace ref/hold counts vmspace0. There is a holdcnt for
273 vmspace_initrefs(struct vmspace *vm)
280 * Allocate a vmspace structure, including a vm_map and pmap.
281 * Initialize numerous fields. While the initial allocation is zerod,
282 * subsequence reuse from the objcache leaves elements of the structure
283 * intact (particularly the pmap), so portions must be zerod.
285 * Returns a referenced vmspace.
290 vmspace_alloc(vm_offset_t min, vm_offset_t max)
294 vm = objcache_get(vmspace_cache, M_WAITOK);
296 bzero(&vm->vm_startcopy,
297 (char *)&vm->vm_endcopy - (char *)&vm->vm_startcopy);
298 vm_map_init(&vm->vm_map, min, max, NULL); /* initializes token */
301 * NOTE: hold to acquires token for safety.
303 * On return vmspace is referenced (refs=1, hold=1). That is,
304 * each refcnt also has a holdcnt. There can be additional holds
305 * (holdcnt) above and beyond the refcnt. Finalization is handled in
306 * two stages, one on refs 1->0, and the the second on hold 1->0.
308 KKASSERT(vm->vm_holdcnt == 0);
309 KKASSERT(vm->vm_refcnt == VM_REF_DELETED);
310 vmspace_initrefs(vm);
312 pmap_pinit(vmspace_pmap(vm)); /* (some fields reused) */
313 vm->vm_map.pmap = vmspace_pmap(vm); /* XXX */
316 cpu_vmspace_alloc(vm);
323 * NOTE: Can return 0 if the vmspace is exiting.
326 vmspace_getrefs(struct vmspace *vm)
332 if (n & VM_REF_DELETED)
338 vmspace_hold(struct vmspace *vm)
340 atomic_add_int(&vm->vm_holdcnt, 1);
341 lwkt_gettoken(&vm->vm_map.token);
345 * Drop with final termination interlock.
348 vmspace_drop(struct vmspace *vm)
350 lwkt_reltoken(&vm->vm_map.token);
351 vmspace_drop_notoken(vm);
355 vmspace_drop_notoken(struct vmspace *vm)
357 if (atomic_fetchadd_int(&vm->vm_holdcnt, -1) == 1) {
358 if (vm->vm_refcnt & VM_REF_DELETED)
359 vmspace_terminate(vm, 1);
364 * A vmspace object must not be in a terminated state to be able to obtain
365 * additional refs on it.
367 * These are official references to the vmspace, the count is used to check
368 * for vmspace sharing. Foreign accessors should use 'hold' and not 'ref'.
370 * XXX we need to combine hold & ref together into one 64-bit field to allow
371 * holds to prevent stage-1 termination.
374 vmspace_ref(struct vmspace *vm)
378 atomic_add_int(&vm->vm_holdcnt, 1);
379 n = atomic_fetchadd_int(&vm->vm_refcnt, 1);
380 KKASSERT((n & VM_REF_DELETED) == 0);
384 * Release a ref on the vmspace. On the 1->0 transition we do stage-1
385 * termination of the vmspace. Then, on the final drop of the hold we
386 * will do stage-2 final termination.
389 vmspace_rel(struct vmspace *vm)
394 * Drop refs. Each ref also has a hold which is also dropped.
396 * When refs hits 0 compete to get the VM_REF_DELETED flag (hold
397 * prevent finalization) to start termination processing.
398 * Finalization occurs when the last hold count drops to 0.
400 n = atomic_fetchadd_int(&vm->vm_refcnt, -1) - 1;
402 if (atomic_cmpset_int(&vm->vm_refcnt, 0, VM_REF_DELETED)) {
403 vmspace_terminate(vm, 0);
409 vmspace_drop_notoken(vm);
413 * This is called during exit indicating that the vmspace is no
414 * longer in used by an exiting process, but the process has not yet
417 * We drop refs, allowing for stage-1 termination, but maintain a holdcnt
418 * to prevent stage-2 until the process is reaped. Note hte order of
419 * operation, we must hold first.
424 vmspace_relexit(struct vmspace *vm)
426 atomic_add_int(&vm->vm_holdcnt, 1);
431 * Called during reap to disconnect the remainder of the vmspace from
432 * the process. On the hold drop the vmspace termination is finalized.
437 vmspace_exitfree(struct proc *p)
443 vmspace_drop_notoken(vm);
447 * Called in two cases:
449 * (1) When the last refcnt is dropped and the vmspace becomes inactive,
450 * called with final == 0. refcnt will be (u_int)-1 at this point,
451 * and holdcnt will still be non-zero.
453 * (2) When holdcnt becomes 0, called with final == 1. There should no
454 * longer be anyone with access to the vmspace.
456 * VMSPACE_EXIT1 flags the primary deactivation
457 * VMSPACE_EXIT2 flags the last reap
460 vmspace_terminate(struct vmspace *vm, int final)
464 lwkt_gettoken(&vm->vm_map.token);
466 KKASSERT((vm->vm_flags & VMSPACE_EXIT1) == 0);
467 vm->vm_flags |= VMSPACE_EXIT1;
470 * Get rid of most of the resources. Leave the kernel pmap
473 * If the pmap does not contain wired pages we can bulk-delete
474 * the pmap as a performance optimization before removing the
477 * If the pmap contains wired pages we cannot do this
478 * pre-optimization because currently vm_fault_unwire()
479 * expects the pmap pages to exist and will not decrement
480 * p->wire_count if they do not.
483 if (vmspace_pmap(vm)->pm_stats.wired_count) {
484 vm_map_remove(&vm->vm_map, VM_MIN_USER_ADDRESS,
485 VM_MAX_USER_ADDRESS);
486 pmap_remove_pages(vmspace_pmap(vm), VM_MIN_USER_ADDRESS,
487 VM_MAX_USER_ADDRESS);
489 pmap_remove_pages(vmspace_pmap(vm), VM_MIN_USER_ADDRESS,
490 VM_MAX_USER_ADDRESS);
491 vm_map_remove(&vm->vm_map, VM_MIN_USER_ADDRESS,
492 VM_MAX_USER_ADDRESS);
494 lwkt_reltoken(&vm->vm_map.token);
496 KKASSERT((vm->vm_flags & VMSPACE_EXIT1) != 0);
497 KKASSERT((vm->vm_flags & VMSPACE_EXIT2) == 0);
500 * Get rid of remaining basic resources.
502 vm->vm_flags |= VMSPACE_EXIT2;
505 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
506 vm_map_lock(&vm->vm_map);
507 cpu_vmspace_free(vm);
510 * Lock the map, to wait out all other references to it.
511 * Delete all of the mappings and pages they hold, then call
512 * the pmap module to reclaim anything left.
514 vm_map_delete(&vm->vm_map,
515 vm_map_min(&vm->vm_map),
516 vm_map_max(&vm->vm_map),
518 vm_map_unlock(&vm->vm_map);
519 vm_map_entry_release(count);
521 pmap_release(vmspace_pmap(vm));
522 lwkt_reltoken(&vm->vm_map.token);
523 objcache_put(vmspace_cache, vm);
528 * Swap useage is determined by taking the proportional swap used by
529 * VM objects backing the VM map. To make up for fractional losses,
530 * if the VM object has any swap use at all the associated map entries
531 * count for at least 1 swap page.
536 vmspace_swap_count(struct vmspace *vm)
538 vm_map_t map = &vm->vm_map;
541 vm_offset_t count = 0;
546 RB_FOREACH(cur, vm_map_rb_tree, &map->rb_root) {
547 switch(cur->maptype) {
548 case VM_MAPTYPE_NORMAL:
549 case VM_MAPTYPE_VPAGETABLE:
550 if ((object = cur->object.vm_object) == NULL)
552 if (object->swblock_count) {
553 n = (cur->end - cur->start) / PAGE_SIZE;
554 count += object->swblock_count *
555 SWAP_META_PAGES * n / object->size + 1;
568 * Calculate the approximate number of anonymous pages in use by
569 * this vmspace. To make up for fractional losses, we count each
570 * VM object as having at least 1 anonymous page.
575 vmspace_anonymous_count(struct vmspace *vm)
577 vm_map_t map = &vm->vm_map;
580 vm_offset_t count = 0;
583 RB_FOREACH(cur, vm_map_rb_tree, &map->rb_root) {
584 switch(cur->maptype) {
585 case VM_MAPTYPE_NORMAL:
586 case VM_MAPTYPE_VPAGETABLE:
587 if ((object = cur->object.vm_object) == NULL)
589 if (object->type != OBJT_DEFAULT &&
590 object->type != OBJT_SWAP) {
593 count += object->resident_page_count;
605 * Initialize an existing vm_map structure such as that in the vmspace
606 * structure. The pmap is initialized elsewhere.
611 vm_map_init(struct vm_map *map, vm_offset_t min_addr, vm_offset_t max_addr,
614 RB_INIT(&map->rb_root);
615 spin_init(&map->ilock_spin, "ilock");
616 map->ilock_base = NULL;
620 vm_map_min(map) = min_addr;
621 vm_map_max(map) = max_addr;
625 bzero(&map->freehint, sizeof(map->freehint));
626 lwkt_token_init(&map->token, "vm_map");
627 lockinit(&map->lock, "vm_maplk", (hz + 9) / 10, 0);
631 * Find the first possible free address for the specified request length.
632 * Returns 0 if we don't have one cached.
636 vm_map_freehint_find(vm_map_t map, vm_size_t length, vm_size_t align)
638 vm_map_freehint_t *scan;
640 scan = &map->freehint[0];
641 while (scan < &map->freehint[VM_MAP_FFCOUNT]) {
642 if (scan->length == length && scan->align == align)
650 * Unconditionally set the freehint. Called by vm_map_findspace() after
651 * it finds an address. This will help us iterate optimally on the next
656 vm_map_freehint_update(vm_map_t map, vm_offset_t start,
657 vm_size_t length, vm_size_t align)
659 vm_map_freehint_t *scan;
661 scan = &map->freehint[0];
662 while (scan < &map->freehint[VM_MAP_FFCOUNT]) {
663 if (scan->length == length && scan->align == align) {
669 scan = &map->freehint[map->freehint_newindex & VM_MAP_FFMASK];
672 scan->length = length;
673 ++map->freehint_newindex;
677 * Update any existing freehints (for any alignment), for the hole we just
682 vm_map_freehint_hole(vm_map_t map, vm_offset_t start, vm_size_t length)
684 vm_map_freehint_t *scan;
686 scan = &map->freehint[0];
687 while (scan < &map->freehint[VM_MAP_FFCOUNT]) {
688 if (scan->length <= length && scan->start > start)
695 * Shadow the vm_map_entry's object. This typically needs to be done when
696 * a write fault is taken on an entry which had previously been cloned by
697 * fork(). The shared object (which might be NULL) must become private so
698 * we add a shadow layer above it.
700 * Object allocation for anonymous mappings is defered as long as possible.
701 * When creating a shadow, however, the underlying object must be instantiated
702 * so it can be shared.
704 * If the map segment is governed by a virtual page table then it is
705 * possible to address offsets beyond the mapped area. Just allocate
706 * a maximally sized object for this case.
708 * If addref is non-zero an additional reference is added to the returned
709 * entry. This mechanic exists because the additional reference might have
710 * to be added atomically and not after return to prevent a premature
713 * The vm_map must be exclusively locked.
714 * No other requirements.
718 vm_map_entry_shadow(vm_map_entry_t entry, int addref)
720 if (entry->maptype == VM_MAPTYPE_VPAGETABLE) {
721 vm_object_shadow(&entry->object.vm_object, &entry->offset,
722 0x7FFFFFFF, addref); /* XXX */
724 vm_object_shadow(&entry->object.vm_object, &entry->offset,
725 atop(entry->end - entry->start), addref);
727 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
731 * Allocate an object for a vm_map_entry.
733 * Object allocation for anonymous mappings is defered as long as possible.
734 * This function is called when we can defer no longer, generally when a map
735 * entry might be split or forked or takes a page fault.
737 * If the map segment is governed by a virtual page table then it is
738 * possible to address offsets beyond the mapped area. Just allocate
739 * a maximally sized object for this case.
741 * The vm_map must be exclusively locked.
742 * No other requirements.
745 vm_map_entry_allocate_object(vm_map_entry_t entry)
749 if (entry->maptype == VM_MAPTYPE_VPAGETABLE) {
750 obj = vm_object_allocate(OBJT_DEFAULT, 0x7FFFFFFF); /* XXX */
752 obj = vm_object_allocate(OBJT_DEFAULT,
753 atop(entry->end - entry->start));
755 entry->object.vm_object = obj;
760 * Set an initial negative count so the first attempt to reserve
761 * space preloads a bunch of vm_map_entry's for this cpu. Also
762 * pre-allocate 2 vm_map_entries which will be needed by zalloc() to
763 * map a new page for vm_map_entry structures. SMP systems are
764 * particularly sensitive.
766 * This routine is called in early boot so we cannot just call
767 * vm_map_entry_reserve().
769 * Called from the low level boot code only (for each cpu)
771 * WARNING! Take care not to have too-big a static/BSS structure here
772 * as MAXCPU can be 256+, otherwise the loader's 64MB heap
773 * can get blown out by the kernel plus the initrd image.
776 vm_map_entry_reserve_cpu_init(globaldata_t gd)
778 vm_map_entry_t entry;
782 atomic_add_int(&gd->gd_vme_avail, -MAP_RESERVE_COUNT * 2);
783 if (gd->gd_cpuid == 0) {
784 entry = &cpu_map_entry_init_bsp[0];
785 count = MAPENTRYBSP_CACHE;
787 entry = &cpu_map_entry_init_ap[gd->gd_cpuid][0];
788 count = MAPENTRYAP_CACHE;
790 for (i = 0; i < count; ++i, ++entry) {
791 MAPENT_FREELIST(entry) = gd->gd_vme_base;
792 gd->gd_vme_base = entry;
797 * Reserves vm_map_entry structures so code later-on can manipulate
798 * map_entry structures within a locked map without blocking trying
799 * to allocate a new vm_map_entry.
803 * WARNING! We must not decrement gd_vme_avail until after we have
804 * ensured that sufficient entries exist, otherwise we can
805 * get into an endless call recursion in the zalloc code
809 vm_map_entry_reserve(int count)
811 struct globaldata *gd = mycpu;
812 vm_map_entry_t entry;
815 * Make sure we have enough structures in gd_vme_base to handle
816 * the reservation request.
818 * Use a critical section to protect against VM faults. It might
819 * not be needed, but we have to be careful here.
821 if (gd->gd_vme_avail < count) {
823 while (gd->gd_vme_avail < count) {
824 entry = zalloc(mapentzone);
825 MAPENT_FREELIST(entry) = gd->gd_vme_base;
826 gd->gd_vme_base = entry;
827 atomic_add_int(&gd->gd_vme_avail, 1);
831 atomic_add_int(&gd->gd_vme_avail, -count);
837 * Releases previously reserved vm_map_entry structures that were not
838 * used. If we have too much junk in our per-cpu cache clean some of
844 vm_map_entry_release(int count)
846 struct globaldata *gd = mycpu;
847 vm_map_entry_t entry;
848 vm_map_entry_t efree;
850 count = atomic_fetchadd_int(&gd->gd_vme_avail, count) + count;
851 if (gd->gd_vme_avail > MAP_RESERVE_SLOP) {
854 while (gd->gd_vme_avail > MAP_RESERVE_HYST) {
855 entry = gd->gd_vme_base;
856 KKASSERT(entry != NULL);
857 gd->gd_vme_base = MAPENT_FREELIST(entry);
858 atomic_add_int(&gd->gd_vme_avail, -1);
859 MAPENT_FREELIST(entry) = efree;
863 while ((entry = efree) != NULL) {
864 efree = MAPENT_FREELIST(efree);
865 zfree(mapentzone, entry);
871 * Reserve map entry structures for use in kernel_map itself. These
872 * entries have *ALREADY* been reserved on a per-cpu basis when the map
873 * was inited. This function is used by zalloc() to avoid a recursion
874 * when zalloc() itself needs to allocate additional kernel memory.
876 * This function works like the normal reserve but does not load the
877 * vm_map_entry cache (because that would result in an infinite
878 * recursion). Note that gd_vme_avail may go negative. This is expected.
880 * Any caller of this function must be sure to renormalize after
881 * potentially eating entries to ensure that the reserve supply
887 vm_map_entry_kreserve(int count)
889 struct globaldata *gd = mycpu;
891 atomic_add_int(&gd->gd_vme_avail, -count);
892 KASSERT(gd->gd_vme_base != NULL,
893 ("no reserved entries left, gd_vme_avail = %d",
899 * Release previously reserved map entries for kernel_map. We do not
900 * attempt to clean up like the normal release function as this would
901 * cause an unnecessary (but probably not fatal) deep procedure call.
906 vm_map_entry_krelease(int count)
908 struct globaldata *gd = mycpu;
910 atomic_add_int(&gd->gd_vme_avail, count);
914 * Allocates a VM map entry for insertion. No entry fields are filled in.
916 * The entries should have previously been reserved. The reservation count
917 * is tracked in (*countp).
921 static vm_map_entry_t
922 vm_map_entry_create(vm_map_t map, int *countp)
924 struct globaldata *gd = mycpu;
925 vm_map_entry_t entry;
927 KKASSERT(*countp > 0);
930 entry = gd->gd_vme_base;
931 KASSERT(entry != NULL, ("gd_vme_base NULL! count %d", *countp));
932 gd->gd_vme_base = MAPENT_FREELIST(entry);
939 * Dispose of a vm_map_entry that is no longer being referenced.
944 vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry, int *countp)
946 struct globaldata *gd = mycpu;
950 MAPENT_FREELIST(entry) = gd->gd_vme_base;
951 gd->gd_vme_base = entry;
957 * Insert/remove entries from maps.
959 * The related map must be exclusively locked.
960 * The caller must hold map->token
961 * No other requirements.
964 vm_map_entry_link(vm_map_t map, vm_map_entry_t entry)
966 ASSERT_VM_MAP_LOCKED(map);
969 if (vm_map_rb_tree_RB_INSERT(&map->rb_root, entry))
970 panic("vm_map_entry_link: dup addr map %p ent %p", map, entry);
974 vm_map_entry_unlink(vm_map_t map,
975 vm_map_entry_t entry)
977 ASSERT_VM_MAP_LOCKED(map);
979 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
980 panic("vm_map_entry_unlink: attempt to mess with "
981 "locked entry! %p", entry);
983 vm_map_rb_tree_RB_REMOVE(&map->rb_root, entry);
988 * Finds the map entry containing (or immediately preceding) the specified
989 * address in the given map. The entry is returned in (*entry).
991 * The boolean result indicates whether the address is actually contained
994 * The related map must be locked.
995 * No other requirements.
998 vm_map_lookup_entry(vm_map_t map, vm_offset_t address, vm_map_entry_t *entry)
1001 vm_map_entry_t last;
1003 ASSERT_VM_MAP_LOCKED(map);
1006 * Locate the record from the top of the tree. 'last' tracks the
1007 * closest prior record and is returned if no match is found, which
1008 * in binary tree terms means tracking the most recent right-branch
1009 * taken. If there is no prior record, *entry is set to NULL.
1012 tmp = RB_ROOT(&map->rb_root);
1015 if (address >= tmp->start) {
1016 if (address < tmp->end) {
1021 tmp = RB_RIGHT(tmp, rb_entry);
1023 tmp = RB_LEFT(tmp, rb_entry);
1031 * Inserts the given whole VM object into the target map at the specified
1032 * address range. The object's size should match that of the address range.
1034 * The map must be exclusively locked.
1035 * The object must be held.
1036 * The caller must have reserved sufficient vm_map_entry structures.
1038 * If object is non-NULL, ref count must be bumped by caller prior to
1039 * making call to account for the new entry.
1042 vm_map_insert(vm_map_t map, int *countp, void *map_object, void *map_aux,
1043 vm_ooffset_t offset, vm_offset_t start, vm_offset_t end,
1044 vm_maptype_t maptype, vm_subsys_t id,
1045 vm_prot_t prot, vm_prot_t max, int cow)
1047 vm_map_entry_t new_entry;
1048 vm_map_entry_t prev_entry;
1049 vm_map_entry_t next;
1050 vm_map_entry_t temp_entry;
1051 vm_eflags_t protoeflags;
1055 if (maptype == VM_MAPTYPE_UKSMAP)
1058 object = map_object;
1060 ASSERT_VM_MAP_LOCKED(map);
1062 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
1065 * Check that the start and end points are not bogus.
1067 if ((start < vm_map_min(map)) || (end > vm_map_max(map)) ||
1069 return (KERN_INVALID_ADDRESS);
1073 * Find the entry prior to the proposed starting address; if it's part
1074 * of an existing entry, this range is bogus.
1076 if (vm_map_lookup_entry(map, start, &temp_entry))
1077 return (KERN_NO_SPACE);
1078 prev_entry = temp_entry;
1081 * Assert that the next entry doesn't overlap the end point.
1084 next = vm_map_rb_tree_RB_NEXT(prev_entry);
1086 next = RB_MIN(vm_map_rb_tree, &map->rb_root);
1087 if (next && next->start < end)
1088 return (KERN_NO_SPACE);
1092 if (cow & MAP_COPY_ON_WRITE)
1093 protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
1095 if (cow & MAP_NOFAULT) {
1096 protoeflags |= MAP_ENTRY_NOFAULT;
1098 KASSERT(object == NULL,
1099 ("vm_map_insert: paradoxical MAP_NOFAULT request"));
1101 if (cow & MAP_DISABLE_SYNCER)
1102 protoeflags |= MAP_ENTRY_NOSYNC;
1103 if (cow & MAP_DISABLE_COREDUMP)
1104 protoeflags |= MAP_ENTRY_NOCOREDUMP;
1105 if (cow & MAP_IS_STACK)
1106 protoeflags |= MAP_ENTRY_STACK;
1107 if (cow & MAP_IS_KSTACK)
1108 protoeflags |= MAP_ENTRY_KSTACK;
1110 lwkt_gettoken(&map->token);
1114 * When object is non-NULL, it could be shared with another
1115 * process. We have to set or clear OBJ_ONEMAPPING
1118 * NOTE: This flag is only applicable to DEFAULT and SWAP
1119 * objects and will already be clear in other types
1120 * of objects, so a shared object lock is ok for
1123 if ((object->ref_count > 1) || (object->shadow_count != 0)) {
1124 vm_object_clear_flag(object, OBJ_ONEMAPPING);
1127 else if (prev_entry &&
1128 (prev_entry->eflags == protoeflags) &&
1129 (prev_entry->end == start) &&
1130 (prev_entry->wired_count == 0) &&
1131 (prev_entry->id == id) &&
1132 prev_entry->maptype == maptype &&
1133 maptype == VM_MAPTYPE_NORMAL &&
1134 ((prev_entry->object.vm_object == NULL) ||
1135 vm_object_coalesce(prev_entry->object.vm_object,
1136 OFF_TO_IDX(prev_entry->offset),
1137 (vm_size_t)(prev_entry->end - prev_entry->start),
1138 (vm_size_t)(end - prev_entry->end)))) {
1140 * We were able to extend the object. Determine if we
1141 * can extend the previous map entry to include the
1142 * new range as well.
1144 if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
1145 (prev_entry->protection == prot) &&
1146 (prev_entry->max_protection == max)) {
1147 map->size += (end - prev_entry->end);
1148 prev_entry->end = end;
1149 vm_map_simplify_entry(map, prev_entry, countp);
1150 lwkt_reltoken(&map->token);
1151 return (KERN_SUCCESS);
1155 * If we can extend the object but cannot extend the
1156 * map entry, we have to create a new map entry. We
1157 * must bump the ref count on the extended object to
1158 * account for it. object may be NULL.
1160 * XXX if object is NULL should we set offset to 0 here ?
1162 object = prev_entry->object.vm_object;
1163 offset = prev_entry->offset +
1164 (prev_entry->end - prev_entry->start);
1166 vm_object_hold(object);
1167 vm_object_chain_wait(object, 0);
1168 vm_object_reference_locked(object);
1170 map_object = object;
1175 * NOTE: if conditionals fail, object can be NULL here. This occurs
1176 * in things like the buffer map where we manage kva but do not manage
1181 * Create a new entry
1184 new_entry = vm_map_entry_create(map, countp);
1185 new_entry->start = start;
1186 new_entry->end = end;
1189 new_entry->maptype = maptype;
1190 new_entry->eflags = protoeflags;
1191 new_entry->object.map_object = map_object;
1192 new_entry->aux.master_pde = 0; /* in case size is different */
1193 new_entry->aux.map_aux = map_aux;
1194 new_entry->offset = offset;
1196 new_entry->inheritance = VM_INHERIT_DEFAULT;
1197 new_entry->protection = prot;
1198 new_entry->max_protection = max;
1199 new_entry->wired_count = 0;
1202 * Insert the new entry into the list
1205 vm_map_entry_link(map, new_entry);
1206 map->size += new_entry->end - new_entry->start;
1209 * Don't worry about updating freehint[] when inserting, allow
1210 * addresses to be lower than the actual first free spot.
1214 * Temporarily removed to avoid MAP_STACK panic, due to
1215 * MAP_STACK being a huge hack. Will be added back in
1216 * when MAP_STACK (and the user stack mapping) is fixed.
1219 * It may be possible to simplify the entry
1221 vm_map_simplify_entry(map, new_entry, countp);
1225 * Try to pre-populate the page table. Mappings governed by virtual
1226 * page tables cannot be prepopulated without a lot of work, so
1229 if ((cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) &&
1230 maptype != VM_MAPTYPE_VPAGETABLE &&
1231 maptype != VM_MAPTYPE_UKSMAP) {
1233 if (vm_map_relock_enable && (cow & MAP_PREFAULT_RELOCK)) {
1235 vm_object_lock_swap();
1236 vm_object_drop(object);
1238 pmap_object_init_pt(map->pmap, start, prot,
1239 object, OFF_TO_IDX(offset), end - start,
1240 cow & MAP_PREFAULT_PARTIAL);
1242 vm_object_hold(object);
1243 vm_object_lock_swap();
1247 vm_object_drop(object);
1249 lwkt_reltoken(&map->token);
1250 return (KERN_SUCCESS);
1254 * Find sufficient space for `length' bytes in the given map, starting at
1255 * `start'. Returns 0 on success, 1 on no space.
1257 * This function will returned an arbitrarily aligned pointer. If no
1258 * particular alignment is required you should pass align as 1. Note that
1259 * the map may return PAGE_SIZE aligned pointers if all the lengths used in
1260 * the map are a multiple of PAGE_SIZE, even if you pass a smaller align
1263 * 'align' should be a power of 2 but is not required to be.
1265 * The map must be exclusively locked.
1266 * No other requirements.
1269 vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length,
1270 vm_size_t align, int flags, vm_offset_t *addr)
1272 vm_map_entry_t entry;
1274 vm_offset_t hole_start;
1276 vm_offset_t align_mask;
1278 if (start < vm_map_min(map))
1279 start = vm_map_min(map);
1280 if (start > vm_map_max(map))
1284 * If the alignment is not a power of 2 we will have to use
1285 * a mod/division, set align_mask to a special value.
1287 if ((align | (align - 1)) + 1 != (align << 1))
1288 align_mask = (vm_offset_t)-1;
1290 align_mask = align - 1;
1293 * Use freehint to adjust the start point, hopefully reducing
1294 * the iteration to O(1).
1296 hole_start = vm_map_freehint_find(map, length, align);
1297 if (start < hole_start)
1299 if (vm_map_lookup_entry(map, start, &tmp))
1301 entry = tmp; /* may be NULL */
1304 * Look through the rest of the map, trying to fit a new region in the
1305 * gap between existing regions, or after the very last region.
1309 * Adjust the proposed start by the requested alignment,
1310 * be sure that we didn't wrap the address.
1312 if (align_mask == (vm_offset_t)-1)
1313 end = roundup(start, align);
1315 end = (start + align_mask) & ~align_mask;
1321 * Find the end of the proposed new region. Be sure we didn't
1322 * go beyond the end of the map, or wrap around the address.
1323 * Then check to see if this is the last entry or if the
1324 * proposed end fits in the gap between this and the next
1327 end = start + length;
1328 if (end > vm_map_max(map) || end < start)
1332 * Locate the next entry, we can stop if this is the
1333 * last entry (we know we are in-bounds so that would
1337 entry = vm_map_rb_tree_RB_NEXT(entry);
1339 entry = RB_MIN(vm_map_rb_tree, &map->rb_root);
1344 * Determine if the proposed area would overlap the
1347 * When matching against a STACK entry, only allow the
1348 * memory map to intrude on the ungrown portion of the
1349 * STACK entry when MAP_TRYFIXED is set.
1351 if (entry->start >= end) {
1352 if ((entry->eflags & MAP_ENTRY_STACK) == 0)
1354 if (flags & MAP_TRYFIXED)
1356 if (entry->start - entry->aux.avail_ssize >= end)
1363 * Update the freehint
1365 vm_map_freehint_update(map, start, length, align);
1368 * Grow the kernel_map if necessary. pmap_growkernel() will panic
1369 * if it fails. The kernel_map is locked and nothing can steal
1370 * our address space if pmap_growkernel() blocks.
1372 * NOTE: This may be unconditionally called for kldload areas on
1373 * x86_64 because these do not bump kernel_vm_end (which would
1374 * fill 128G worth of page tables!). Therefore we must not
1377 if (map == &kernel_map) {
1380 kstop = round_page(start + length);
1381 if (kstop > kernel_vm_end)
1382 pmap_growkernel(start, kstop);
1389 * vm_map_find finds an unallocated region in the target address map with
1390 * the given length and allocates it. The search is defined to be first-fit
1391 * from the specified address; the region found is returned in the same
1394 * If object is non-NULL, ref count must be bumped by caller
1395 * prior to making call to account for the new entry.
1397 * No requirements. This function will lock the map temporarily.
1400 vm_map_find(vm_map_t map, void *map_object, void *map_aux,
1401 vm_ooffset_t offset, vm_offset_t *addr,
1402 vm_size_t length, vm_size_t align, boolean_t fitit,
1403 vm_maptype_t maptype, vm_subsys_t id,
1404 vm_prot_t prot, vm_prot_t max, int cow)
1411 if (maptype == VM_MAPTYPE_UKSMAP)
1414 object = map_object;
1418 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1421 vm_object_hold_shared(object);
1423 if (vm_map_findspace(map, start, length, align, 0, addr)) {
1425 vm_object_drop(object);
1427 vm_map_entry_release(count);
1428 return (KERN_NO_SPACE);
1432 result = vm_map_insert(map, &count, map_object, map_aux,
1433 offset, start, start + length,
1434 maptype, id, prot, max, cow);
1436 vm_object_drop(object);
1438 vm_map_entry_release(count);
1444 * Simplify the given map entry by merging with either neighbor. This
1445 * routine also has the ability to merge with both neighbors.
1447 * This routine guarentees that the passed entry remains valid (though
1448 * possibly extended). When merging, this routine may delete one or
1449 * both neighbors. No action is taken on entries which have their
1450 * in-transition flag set.
1452 * The map must be exclusively locked.
1455 vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry, int *countp)
1457 vm_map_entry_t next, prev;
1458 vm_size_t prevsize, esize;
1460 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1461 ++mycpu->gd_cnt.v_intrans_coll;
1465 if (entry->maptype == VM_MAPTYPE_SUBMAP)
1467 if (entry->maptype == VM_MAPTYPE_UKSMAP)
1470 prev = vm_map_rb_tree_RB_PREV(entry);
1472 prevsize = prev->end - prev->start;
1473 if ( (prev->end == entry->start) &&
1474 (prev->maptype == entry->maptype) &&
1475 (prev->object.vm_object == entry->object.vm_object) &&
1476 (!prev->object.vm_object ||
1477 (prev->offset + prevsize == entry->offset)) &&
1478 (prev->eflags == entry->eflags) &&
1479 (prev->protection == entry->protection) &&
1480 (prev->max_protection == entry->max_protection) &&
1481 (prev->inheritance == entry->inheritance) &&
1482 (prev->id == entry->id) &&
1483 (prev->wired_count == entry->wired_count)) {
1484 vm_map_entry_unlink(map, prev);
1485 entry->start = prev->start;
1486 entry->offset = prev->offset;
1487 if (prev->object.vm_object)
1488 vm_object_deallocate(prev->object.vm_object);
1489 vm_map_entry_dispose(map, prev, countp);
1493 next = vm_map_rb_tree_RB_NEXT(entry);
1495 esize = entry->end - entry->start;
1496 if ((entry->end == next->start) &&
1497 (next->maptype == entry->maptype) &&
1498 (next->object.vm_object == entry->object.vm_object) &&
1499 (!entry->object.vm_object ||
1500 (entry->offset + esize == next->offset)) &&
1501 (next->eflags == entry->eflags) &&
1502 (next->protection == entry->protection) &&
1503 (next->max_protection == entry->max_protection) &&
1504 (next->inheritance == entry->inheritance) &&
1505 (next->id == entry->id) &&
1506 (next->wired_count == entry->wired_count)) {
1507 vm_map_entry_unlink(map, next);
1508 entry->end = next->end;
1509 if (next->object.vm_object)
1510 vm_object_deallocate(next->object.vm_object);
1511 vm_map_entry_dispose(map, next, countp);
1517 * Asserts that the given entry begins at or after the specified address.
1518 * If necessary, it splits the entry into two.
1520 #define vm_map_clip_start(map, entry, startaddr, countp) \
1522 if (startaddr > entry->start) \
1523 _vm_map_clip_start(map, entry, startaddr, countp); \
1527 * This routine is called only when it is known that the entry must be split.
1529 * The map must be exclusively locked.
1532 _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start,
1535 vm_map_entry_t new_entry;
1538 * Split off the front portion -- note that we must insert the new
1539 * entry BEFORE this one, so that this entry has the specified
1543 vm_map_simplify_entry(map, entry, countp);
1546 * If there is no object backing this entry, we might as well create
1547 * one now. If we defer it, an object can get created after the map
1548 * is clipped, and individual objects will be created for the split-up
1549 * map. This is a bit of a hack, but is also about the best place to
1550 * put this improvement.
1552 if (entry->object.vm_object == NULL && !map->system_map &&
1553 VM_MAP_ENTRY_WITHIN_PARTITION(entry)) {
1554 vm_map_entry_allocate_object(entry);
1557 new_entry = vm_map_entry_create(map, countp);
1558 *new_entry = *entry;
1560 new_entry->end = start;
1561 entry->offset += (start - entry->start);
1562 entry->start = start;
1564 vm_map_entry_link(map, new_entry);
1566 switch(entry->maptype) {
1567 case VM_MAPTYPE_NORMAL:
1568 case VM_MAPTYPE_VPAGETABLE:
1569 if (new_entry->object.vm_object) {
1570 vm_object_hold(new_entry->object.vm_object);
1571 vm_object_chain_wait(new_entry->object.vm_object, 0);
1572 vm_object_reference_locked(new_entry->object.vm_object);
1573 vm_object_drop(new_entry->object.vm_object);
1582 * Asserts that the given entry ends at or before the specified address.
1583 * If necessary, it splits the entry into two.
1585 * The map must be exclusively locked.
1587 #define vm_map_clip_end(map, entry, endaddr, countp) \
1589 if (endaddr < entry->end) \
1590 _vm_map_clip_end(map, entry, endaddr, countp); \
1594 * This routine is called only when it is known that the entry must be split.
1596 * The map must be exclusively locked.
1599 _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end,
1602 vm_map_entry_t new_entry;
1605 * If there is no object backing this entry, we might as well create
1606 * one now. If we defer it, an object can get created after the map
1607 * is clipped, and individual objects will be created for the split-up
1608 * map. This is a bit of a hack, but is also about the best place to
1609 * put this improvement.
1612 if (entry->object.vm_object == NULL && !map->system_map &&
1613 VM_MAP_ENTRY_WITHIN_PARTITION(entry)) {
1614 vm_map_entry_allocate_object(entry);
1618 * Create a new entry and insert it AFTER the specified entry
1620 new_entry = vm_map_entry_create(map, countp);
1621 *new_entry = *entry;
1623 new_entry->start = entry->end = end;
1624 new_entry->offset += (end - entry->start);
1626 vm_map_entry_link(map, new_entry);
1628 switch(entry->maptype) {
1629 case VM_MAPTYPE_NORMAL:
1630 case VM_MAPTYPE_VPAGETABLE:
1631 if (new_entry->object.vm_object) {
1632 vm_object_hold(new_entry->object.vm_object);
1633 vm_object_chain_wait(new_entry->object.vm_object, 0);
1634 vm_object_reference_locked(new_entry->object.vm_object);
1635 vm_object_drop(new_entry->object.vm_object);
1644 * Asserts that the starting and ending region addresses fall within the
1645 * valid range for the map.
1647 #define VM_MAP_RANGE_CHECK(map, start, end) \
1649 if (start < vm_map_min(map)) \
1650 start = vm_map_min(map); \
1651 if (end > vm_map_max(map)) \
1652 end = vm_map_max(map); \
1658 * Used to block when an in-transition collison occurs. The map
1659 * is unlocked for the sleep and relocked before the return.
1662 vm_map_transition_wait(vm_map_t map, int relock)
1664 tsleep_interlock(map, 0);
1666 tsleep(map, PINTERLOCKED, "vment", 0);
1672 * When we do blocking operations with the map lock held it is
1673 * possible that a clip might have occured on our in-transit entry,
1674 * requiring an adjustment to the entry in our loop. These macros
1675 * help the pageable and clip_range code deal with the case. The
1676 * conditional costs virtually nothing if no clipping has occured.
1679 #define CLIP_CHECK_BACK(entry, save_start) \
1681 while (entry->start != save_start) { \
1682 entry = vm_map_rb_tree_RB_PREV(entry); \
1683 KASSERT(entry, ("bad entry clip")); \
1687 #define CLIP_CHECK_FWD(entry, save_end) \
1689 while (entry->end != save_end) { \
1690 entry = vm_map_rb_tree_RB_NEXT(entry); \
1691 KASSERT(entry, ("bad entry clip")); \
1697 * Clip the specified range and return the base entry. The
1698 * range may cover several entries starting at the returned base
1699 * and the first and last entry in the covering sequence will be
1700 * properly clipped to the requested start and end address.
1702 * If no holes are allowed you should pass the MAP_CLIP_NO_HOLES
1705 * The MAP_ENTRY_IN_TRANSITION flag will be set for the entries
1706 * covered by the requested range.
1708 * The map must be exclusively locked on entry and will remain locked
1709 * on return. If no range exists or the range contains holes and you
1710 * specified that no holes were allowed, NULL will be returned. This
1711 * routine may temporarily unlock the map in order avoid a deadlock when
1716 vm_map_clip_range(vm_map_t map, vm_offset_t start, vm_offset_t end,
1717 int *countp, int flags)
1719 vm_map_entry_t start_entry;
1720 vm_map_entry_t entry;
1721 vm_map_entry_t next;
1724 * Locate the entry and effect initial clipping. The in-transition
1725 * case does not occur very often so do not try to optimize it.
1728 if (vm_map_lookup_entry(map, start, &start_entry) == FALSE)
1730 entry = start_entry;
1731 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1732 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1733 ++mycpu->gd_cnt.v_intrans_coll;
1734 ++mycpu->gd_cnt.v_intrans_wait;
1735 vm_map_transition_wait(map, 1);
1737 * entry and/or start_entry may have been clipped while
1738 * we slept, or may have gone away entirely. We have
1739 * to restart from the lookup.
1745 * Since we hold an exclusive map lock we do not have to restart
1746 * after clipping, even though clipping may block in zalloc.
1748 vm_map_clip_start(map, entry, start, countp);
1749 vm_map_clip_end(map, entry, end, countp);
1750 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
1753 * Scan entries covered by the range. When working on the next
1754 * entry a restart need only re-loop on the current entry which
1755 * we have already locked, since 'next' may have changed. Also,
1756 * even though entry is safe, it may have been clipped so we
1757 * have to iterate forwards through the clip after sleeping.
1760 next = vm_map_rb_tree_RB_NEXT(entry);
1761 if (next == NULL || next->start >= end)
1763 if (flags & MAP_CLIP_NO_HOLES) {
1764 if (next->start > entry->end) {
1765 vm_map_unclip_range(map, start_entry,
1766 start, entry->end, countp, flags);
1771 if (next->eflags & MAP_ENTRY_IN_TRANSITION) {
1772 vm_offset_t save_end = entry->end;
1773 next->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1774 ++mycpu->gd_cnt.v_intrans_coll;
1775 ++mycpu->gd_cnt.v_intrans_wait;
1776 vm_map_transition_wait(map, 1);
1779 * clips might have occured while we blocked.
1781 CLIP_CHECK_FWD(entry, save_end);
1782 CLIP_CHECK_BACK(start_entry, start);
1787 * No restart necessary even though clip_end may block, we
1788 * are holding the map lock.
1790 vm_map_clip_end(map, next, end, countp);
1791 next->eflags |= MAP_ENTRY_IN_TRANSITION;
1794 if (flags & MAP_CLIP_NO_HOLES) {
1795 if (entry->end != end) {
1796 vm_map_unclip_range(map, start_entry,
1797 start, entry->end, countp, flags);
1801 return(start_entry);
1805 * Undo the effect of vm_map_clip_range(). You should pass the same
1806 * flags and the same range that you passed to vm_map_clip_range().
1807 * This code will clear the in-transition flag on the entries and
1808 * wake up anyone waiting. This code will also simplify the sequence
1809 * and attempt to merge it with entries before and after the sequence.
1811 * The map must be locked on entry and will remain locked on return.
1813 * Note that you should also pass the start_entry returned by
1814 * vm_map_clip_range(). However, if you block between the two calls
1815 * with the map unlocked please be aware that the start_entry may
1816 * have been clipped and you may need to scan it backwards to find
1817 * the entry corresponding with the original start address. You are
1818 * responsible for this, vm_map_unclip_range() expects the correct
1819 * start_entry to be passed to it and will KASSERT otherwise.
1823 vm_map_unclip_range(vm_map_t map, vm_map_entry_t start_entry,
1824 vm_offset_t start, vm_offset_t end,
1825 int *countp, int flags)
1827 vm_map_entry_t entry;
1829 entry = start_entry;
1831 KASSERT(entry->start == start, ("unclip_range: illegal base entry"));
1832 while (entry && entry->start < end) {
1833 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
1834 ("in-transition flag not set during unclip on: %p",
1836 KASSERT(entry->end <= end,
1837 ("unclip_range: tail wasn't clipped"));
1838 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
1839 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
1840 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
1843 entry = vm_map_rb_tree_RB_NEXT(entry);
1847 * Simplification does not block so there is no restart case.
1849 entry = start_entry;
1850 while (entry && entry->start < end) {
1851 vm_map_simplify_entry(map, entry, countp);
1852 entry = vm_map_rb_tree_RB_NEXT(entry);
1857 * Mark the given range as handled by a subordinate map.
1859 * This range must have been created with vm_map_find(), and no other
1860 * operations may have been performed on this range prior to calling
1863 * Submappings cannot be removed.
1868 vm_map_submap(vm_map_t map, vm_offset_t start, vm_offset_t end, vm_map_t submap)
1870 vm_map_entry_t entry;
1871 int result = KERN_INVALID_ARGUMENT;
1874 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1877 VM_MAP_RANGE_CHECK(map, start, end);
1879 if (vm_map_lookup_entry(map, start, &entry)) {
1880 vm_map_clip_start(map, entry, start, &count);
1882 entry = vm_map_rb_tree_RB_NEXT(entry);
1884 entry = RB_MIN(vm_map_rb_tree, &map->rb_root);
1887 vm_map_clip_end(map, entry, end, &count);
1889 if ((entry->start == start) && (entry->end == end) &&
1890 ((entry->eflags & MAP_ENTRY_COW) == 0) &&
1891 (entry->object.vm_object == NULL)) {
1892 entry->object.sub_map = submap;
1893 entry->maptype = VM_MAPTYPE_SUBMAP;
1894 result = KERN_SUCCESS;
1897 vm_map_entry_release(count);
1903 * Sets the protection of the specified address region in the target map.
1904 * If "set_max" is specified, the maximum protection is to be set;
1905 * otherwise, only the current protection is affected.
1907 * The protection is not applicable to submaps, but is applicable to normal
1908 * maps and maps governed by virtual page tables. For example, when operating
1909 * on a virtual page table our protection basically controls how COW occurs
1910 * on the backing object, whereas the virtual page table abstraction itself
1911 * is an abstraction for userland.
1916 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
1917 vm_prot_t new_prot, boolean_t set_max)
1919 vm_map_entry_t current;
1920 vm_map_entry_t entry;
1923 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1926 VM_MAP_RANGE_CHECK(map, start, end);
1928 if (vm_map_lookup_entry(map, start, &entry)) {
1929 vm_map_clip_start(map, entry, start, &count);
1931 entry = vm_map_rb_tree_RB_NEXT(entry);
1933 entry = RB_MIN(vm_map_rb_tree, &map->rb_root);
1937 * Make a first pass to check for protection violations.
1940 while (current && current->start < end) {
1941 if (current->maptype == VM_MAPTYPE_SUBMAP) {
1943 vm_map_entry_release(count);
1944 return (KERN_INVALID_ARGUMENT);
1946 if ((new_prot & current->max_protection) != new_prot) {
1948 vm_map_entry_release(count);
1949 return (KERN_PROTECTION_FAILURE);
1953 * When making a SHARED+RW file mmap writable, update
1956 if (new_prot & PROT_WRITE &&
1957 (current->eflags & MAP_ENTRY_NEEDS_COPY) == 0 &&
1958 (current->maptype == VM_MAPTYPE_NORMAL ||
1959 current->maptype == VM_MAPTYPE_VPAGETABLE) &&
1960 current->object.vm_object &&
1961 current->object.vm_object->type == OBJT_VNODE) {
1964 vp = current->object.vm_object->handle;
1965 if (vp && vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_NOWAIT) == 0) {
1966 vfs_timestamp(&vp->v_lastwrite_ts);
1967 vsetflags(vp, VLASTWRITETS);
1971 current = vm_map_rb_tree_RB_NEXT(current);
1975 * Go back and fix up protections. [Note that clipping is not
1976 * necessary the second time.]
1980 while (current && current->start < end) {
1983 vm_map_clip_end(map, current, end, &count);
1985 old_prot = current->protection;
1987 current->max_protection = new_prot;
1988 current->protection = new_prot & old_prot;
1990 current->protection = new_prot;
1994 * Update physical map if necessary. Worry about copy-on-write
1995 * here -- CHECK THIS XXX
1997 if (current->protection != old_prot) {
1998 #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
2001 pmap_protect(map->pmap, current->start,
2003 current->protection & MASK(current));
2007 vm_map_simplify_entry(map, current, &count);
2009 current = vm_map_rb_tree_RB_NEXT(current);
2012 vm_map_entry_release(count);
2013 return (KERN_SUCCESS);
2017 * This routine traverses a processes map handling the madvise
2018 * system call. Advisories are classified as either those effecting
2019 * the vm_map_entry structure, or those effecting the underlying
2022 * The <value> argument is used for extended madvise calls.
2027 vm_map_madvise(vm_map_t map, vm_offset_t start, vm_offset_t end,
2028 int behav, off_t value)
2030 vm_map_entry_t current, entry;
2036 * Some madvise calls directly modify the vm_map_entry, in which case
2037 * we need to use an exclusive lock on the map and we need to perform
2038 * various clipping operations. Otherwise we only need a read-lock
2041 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
2045 case MADV_SEQUENTIAL:
2059 vm_map_lock_read(map);
2062 vm_map_entry_release(count);
2067 * Locate starting entry and clip if necessary.
2070 VM_MAP_RANGE_CHECK(map, start, end);
2072 if (vm_map_lookup_entry(map, start, &entry)) {
2074 vm_map_clip_start(map, entry, start, &count);
2076 entry = vm_map_rb_tree_RB_NEXT(entry);
2078 entry = RB_MIN(vm_map_rb_tree, &map->rb_root);
2083 * madvise behaviors that are implemented in the vm_map_entry.
2085 * We clip the vm_map_entry so that behavioral changes are
2086 * limited to the specified address range.
2088 for (current = entry;
2089 current && current->start < end;
2090 current = vm_map_rb_tree_RB_NEXT(current)) {
2094 if (current->maptype == VM_MAPTYPE_SUBMAP)
2097 vm_map_clip_end(map, current, end, &count);
2101 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
2103 case MADV_SEQUENTIAL:
2104 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
2107 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
2110 current->eflags |= MAP_ENTRY_NOSYNC;
2113 current->eflags &= ~MAP_ENTRY_NOSYNC;
2116 current->eflags |= MAP_ENTRY_NOCOREDUMP;
2119 current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
2123 * Set the page directory page for a map
2124 * governed by a virtual page table. Mark
2125 * the entry as being governed by a virtual
2126 * page table if it is not.
2128 * XXX the page directory page is stored
2129 * in the avail_ssize field if the map_entry.
2131 * XXX the map simplification code does not
2132 * compare this field so weird things may
2133 * happen if you do not apply this function
2134 * to the entire mapping governed by the
2135 * virtual page table.
2137 if (current->maptype != VM_MAPTYPE_VPAGETABLE) {
2141 current->aux.master_pde = value;
2142 pmap_remove(map->pmap,
2143 current->start, current->end);
2147 * Invalidate the related pmap entries, used
2148 * to flush portions of the real kernel's
2149 * pmap when the caller has removed or
2150 * modified existing mappings in a virtual
2153 * (exclusive locked map version does not
2154 * need the range interlock).
2156 pmap_remove(map->pmap,
2157 current->start, current->end);
2163 vm_map_simplify_entry(map, current, &count);
2171 * madvise behaviors that are implemented in the underlying
2174 * Since we don't clip the vm_map_entry, we have to clip
2175 * the vm_object pindex and count.
2177 * NOTE! These functions are only supported on normal maps,
2178 * except MADV_INVAL which is also supported on
2179 * virtual page tables.
2181 for (current = entry;
2182 current && current->start < end;
2183 current = vm_map_rb_tree_RB_NEXT(current)) {
2184 vm_offset_t useStart;
2186 if (current->maptype != VM_MAPTYPE_NORMAL &&
2187 (current->maptype != VM_MAPTYPE_VPAGETABLE ||
2188 behav != MADV_INVAL)) {
2192 pindex = OFF_TO_IDX(current->offset);
2193 delta = atop(current->end - current->start);
2194 useStart = current->start;
2196 if (current->start < start) {
2197 pindex += atop(start - current->start);
2198 delta -= atop(start - current->start);
2201 if (current->end > end)
2202 delta -= atop(current->end - end);
2204 if ((vm_spindex_t)delta <= 0)
2207 if (behav == MADV_INVAL) {
2209 * Invalidate the related pmap entries, used
2210 * to flush portions of the real kernel's
2211 * pmap when the caller has removed or
2212 * modified existing mappings in a virtual
2215 * (shared locked map version needs the
2216 * interlock, see vm_fault()).
2218 struct vm_map_ilock ilock;
2220 KASSERT(useStart >= VM_MIN_USER_ADDRESS &&
2221 useStart + ptoa(delta) <=
2222 VM_MAX_USER_ADDRESS,
2223 ("Bad range %016jx-%016jx (%016jx)",
2224 useStart, useStart + ptoa(delta),
2226 vm_map_interlock(map, &ilock,
2228 useStart + ptoa(delta));
2229 pmap_remove(map->pmap,
2231 useStart + ptoa(delta));
2232 vm_map_deinterlock(map, &ilock);
2234 vm_object_madvise(current->object.vm_object,
2235 pindex, delta, behav);
2239 * Try to populate the page table. Mappings governed
2240 * by virtual page tables cannot be pre-populated
2241 * without a lot of work so don't try.
2243 if (behav == MADV_WILLNEED &&
2244 current->maptype != VM_MAPTYPE_VPAGETABLE) {
2245 pmap_object_init_pt(
2248 current->protection,
2249 current->object.vm_object,
2251 (count << PAGE_SHIFT),
2252 MAP_PREFAULT_MADVISE
2256 vm_map_unlock_read(map);
2258 vm_map_entry_release(count);
2264 * Sets the inheritance of the specified address range in the target map.
2265 * Inheritance affects how the map will be shared with child maps at the
2266 * time of vm_map_fork.
2269 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
2270 vm_inherit_t new_inheritance)
2272 vm_map_entry_t entry;
2273 vm_map_entry_t temp_entry;
2276 switch (new_inheritance) {
2277 case VM_INHERIT_NONE:
2278 case VM_INHERIT_COPY:
2279 case VM_INHERIT_SHARE:
2282 return (KERN_INVALID_ARGUMENT);
2285 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
2288 VM_MAP_RANGE_CHECK(map, start, end);
2290 if (vm_map_lookup_entry(map, start, &temp_entry)) {
2292 vm_map_clip_start(map, entry, start, &count);
2293 } else if (temp_entry) {
2294 entry = vm_map_rb_tree_RB_NEXT(temp_entry);
2296 entry = RB_MIN(vm_map_rb_tree, &map->rb_root);
2299 while (entry && entry->start < end) {
2300 vm_map_clip_end(map, entry, end, &count);
2302 entry->inheritance = new_inheritance;
2304 vm_map_simplify_entry(map, entry, &count);
2306 entry = vm_map_rb_tree_RB_NEXT(entry);
2309 vm_map_entry_release(count);
2310 return (KERN_SUCCESS);
2314 * Implement the semantics of mlock
2317 vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t real_end,
2318 boolean_t new_pageable)
2320 vm_map_entry_t entry;
2321 vm_map_entry_t start_entry;
2323 int rv = KERN_SUCCESS;
2326 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
2328 VM_MAP_RANGE_CHECK(map, start, real_end);
2331 start_entry = vm_map_clip_range(map, start, end, &count,
2333 if (start_entry == NULL) {
2335 vm_map_entry_release(count);
2336 return (KERN_INVALID_ADDRESS);
2339 if (new_pageable == 0) {
2340 entry = start_entry;
2341 while (entry && entry->start < end) {
2342 vm_offset_t save_start;
2343 vm_offset_t save_end;
2346 * Already user wired or hard wired (trivial cases)
2348 if (entry->eflags & MAP_ENTRY_USER_WIRED) {
2349 entry = vm_map_rb_tree_RB_NEXT(entry);
2352 if (entry->wired_count != 0) {
2353 entry->wired_count++;
2354 entry->eflags |= MAP_ENTRY_USER_WIRED;
2355 entry = vm_map_rb_tree_RB_NEXT(entry);
2360 * A new wiring requires instantiation of appropriate
2361 * management structures and the faulting in of the
2364 if (entry->maptype == VM_MAPTYPE_NORMAL ||
2365 entry->maptype == VM_MAPTYPE_VPAGETABLE) {
2366 int copyflag = entry->eflags &
2367 MAP_ENTRY_NEEDS_COPY;
2368 if (copyflag && ((entry->protection &
2369 VM_PROT_WRITE) != 0)) {
2370 vm_map_entry_shadow(entry, 0);
2371 } else if (entry->object.vm_object == NULL &&
2373 vm_map_entry_allocate_object(entry);
2376 entry->wired_count++;
2377 entry->eflags |= MAP_ENTRY_USER_WIRED;
2380 * Now fault in the area. Note that vm_fault_wire()
2381 * may release the map lock temporarily, it will be
2382 * relocked on return. The in-transition
2383 * flag protects the entries.
2385 save_start = entry->start;
2386 save_end = entry->end;
2387 rv = vm_fault_wire(map, entry, TRUE, 0);
2389 CLIP_CHECK_BACK(entry, save_start);
2391 KASSERT(entry->wired_count == 1, ("bad wired_count on entry"));
2392 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2393 entry->wired_count = 0;
2394 if (entry->end == save_end)
2396 entry = vm_map_rb_tree_RB_NEXT(entry);
2398 ("bad entry clip during backout"));
2400 end = save_start; /* unwire the rest */
2404 * note that even though the entry might have been
2405 * clipped, the USER_WIRED flag we set prevents
2406 * duplication so we do not have to do a
2409 entry = vm_map_rb_tree_RB_NEXT(entry);
2413 * If we failed fall through to the unwiring section to
2414 * unwire what we had wired so far. 'end' has already
2421 * start_entry might have been clipped if we unlocked the
2422 * map and blocked. No matter how clipped it has gotten
2423 * there should be a fragment that is on our start boundary.
2425 CLIP_CHECK_BACK(start_entry, start);
2429 * Deal with the unwiring case.
2433 * This is the unwiring case. We must first ensure that the
2434 * range to be unwired is really wired down. We know there
2437 entry = start_entry;
2438 while (entry && entry->start < end) {
2439 if ((entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
2440 rv = KERN_INVALID_ARGUMENT;
2443 KASSERT(entry->wired_count != 0,
2444 ("wired count was 0 with USER_WIRED set! %p",
2446 entry = vm_map_rb_tree_RB_NEXT(entry);
2450 * Now decrement the wiring count for each region. If a region
2451 * becomes completely unwired, unwire its physical pages and
2455 * The map entries are processed in a loop, checking to
2456 * make sure the entry is wired and asserting it has a wired
2457 * count. However, another loop was inserted more-or-less in
2458 * the middle of the unwiring path. This loop picks up the
2459 * "entry" loop variable from the first loop without first
2460 * setting it to start_entry. Naturally, the secound loop
2461 * is never entered and the pages backing the entries are
2462 * never unwired. This can lead to a leak of wired pages.
2464 entry = start_entry;
2465 while (entry && entry->start < end) {
2466 KASSERT(entry->eflags & MAP_ENTRY_USER_WIRED,
2467 ("expected USER_WIRED on entry %p", entry));
2468 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2469 entry->wired_count--;
2470 if (entry->wired_count == 0)
2471 vm_fault_unwire(map, entry);
2472 entry = vm_map_rb_tree_RB_NEXT(entry);
2476 vm_map_unclip_range(map, start_entry, start, real_end, &count,
2479 vm_map_entry_release(count);
2485 * Sets the pageability of the specified address range in the target map.
2486 * Regions specified as not pageable require locked-down physical
2487 * memory and physical page maps.
2489 * The map must not be locked, but a reference must remain to the map
2490 * throughout the call.
2492 * This function may be called via the zalloc path and must properly
2493 * reserve map entries for kernel_map.
2498 vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t real_end, int kmflags)
2500 vm_map_entry_t entry;
2501 vm_map_entry_t start_entry;
2503 int rv = KERN_SUCCESS;
2506 if (kmflags & KM_KRESERVE)
2507 count = vm_map_entry_kreserve(MAP_RESERVE_COUNT);
2509 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
2511 VM_MAP_RANGE_CHECK(map, start, real_end);
2514 start_entry = vm_map_clip_range(map, start, end, &count,
2516 if (start_entry == NULL) {
2518 rv = KERN_INVALID_ADDRESS;
2521 if ((kmflags & KM_PAGEABLE) == 0) {
2525 * 1. Holding the write lock, we create any shadow or zero-fill
2526 * objects that need to be created. Then we clip each map
2527 * entry to the region to be wired and increment its wiring
2528 * count. We create objects before clipping the map entries
2529 * to avoid object proliferation.
2531 * 2. We downgrade to a read lock, and call vm_fault_wire to
2532 * fault in the pages for any newly wired area (wired_count is
2535 * Downgrading to a read lock for vm_fault_wire avoids a
2536 * possible deadlock with another process that may have faulted
2537 * on one of the pages to be wired (it would mark the page busy,
2538 * blocking us, then in turn block on the map lock that we
2539 * hold). Because of problems in the recursive lock package,
2540 * we cannot upgrade to a write lock in vm_map_lookup. Thus,
2541 * any actions that require the write lock must be done
2542 * beforehand. Because we keep the read lock on the map, the
2543 * copy-on-write status of the entries we modify here cannot
2546 entry = start_entry;
2547 while (entry && entry->start < end) {
2549 * Trivial case if the entry is already wired
2551 if (entry->wired_count) {
2552 entry->wired_count++;
2553 entry = vm_map_rb_tree_RB_NEXT(entry);
2558 * The entry is being newly wired, we have to setup
2559 * appropriate management structures. A shadow
2560 * object is required for a copy-on-write region,
2561 * or a normal object for a zero-fill region. We
2562 * do not have to do this for entries that point to sub
2563 * maps because we won't hold the lock on the sub map.
2565 if (entry->maptype == VM_MAPTYPE_NORMAL ||
2566 entry->maptype == VM_MAPTYPE_VPAGETABLE) {
2567 int copyflag = entry->eflags &
2568 MAP_ENTRY_NEEDS_COPY;
2569 if (copyflag && ((entry->protection &
2570 VM_PROT_WRITE) != 0)) {
2571 vm_map_entry_shadow(entry, 0);
2572 } else if (entry->object.vm_object == NULL &&
2574 vm_map_entry_allocate_object(entry);
2577 entry->wired_count++;
2578 entry = vm_map_rb_tree_RB_NEXT(entry);
2586 * HACK HACK HACK HACK
2588 * vm_fault_wire() temporarily unlocks the map to avoid
2589 * deadlocks. The in-transition flag from vm_map_clip_range
2590 * call should protect us from changes while the map is
2593 * NOTE: Previously this comment stated that clipping might
2594 * still occur while the entry is unlocked, but from
2595 * what I can tell it actually cannot.
2597 * It is unclear whether the CLIP_CHECK_*() calls
2598 * are still needed but we keep them in anyway.
2600 * HACK HACK HACK HACK
2603 entry = start_entry;
2604 while (entry && entry->start < end) {
2606 * If vm_fault_wire fails for any page we need to undo
2607 * what has been done. We decrement the wiring count
2608 * for those pages which have not yet been wired (now)
2609 * and unwire those that have (later).
2611 vm_offset_t save_start = entry->start;
2612 vm_offset_t save_end = entry->end;
2614 if (entry->wired_count == 1)
2615 rv = vm_fault_wire(map, entry, FALSE, kmflags);
2617 CLIP_CHECK_BACK(entry, save_start);
2619 KASSERT(entry->wired_count == 1,
2620 ("wired_count changed unexpectedly"));
2621 entry->wired_count = 0;
2622 if (entry->end == save_end)
2624 entry = vm_map_rb_tree_RB_NEXT(entry);
2626 ("bad entry clip during backout"));
2631 CLIP_CHECK_FWD(entry, save_end);
2632 entry = vm_map_rb_tree_RB_NEXT(entry);
2636 * If a failure occured undo everything by falling through
2637 * to the unwiring code. 'end' has already been adjusted
2641 kmflags |= KM_PAGEABLE;
2644 * start_entry is still IN_TRANSITION but may have been
2645 * clipped since vm_fault_wire() unlocks and relocks the
2646 * map. No matter how clipped it has gotten there should
2647 * be a fragment that is on our start boundary.
2649 CLIP_CHECK_BACK(start_entry, start);
2652 if (kmflags & KM_PAGEABLE) {
2654 * This is the unwiring case. We must first ensure that the
2655 * range to be unwired is really wired down. We know there
2658 entry = start_entry;
2659 while (entry && entry->start < end) {
2660 if (entry->wired_count == 0) {
2661 rv = KERN_INVALID_ARGUMENT;
2664 entry = vm_map_rb_tree_RB_NEXT(entry);
2668 * Now decrement the wiring count for each region. If a region
2669 * becomes completely unwired, unwire its physical pages and
2672 entry = start_entry;
2673 while (entry && entry->start < end) {
2674 entry->wired_count--;
2675 if (entry->wired_count == 0)
2676 vm_fault_unwire(map, entry);
2677 entry = vm_map_rb_tree_RB_NEXT(entry);
2681 vm_map_unclip_range(map, start_entry, start, real_end,
2682 &count, MAP_CLIP_NO_HOLES);
2685 if (kmflags & KM_KRESERVE)
2686 vm_map_entry_krelease(count);
2688 vm_map_entry_release(count);
2693 * Mark a newly allocated address range as wired but do not fault in
2694 * the pages. The caller is expected to load the pages into the object.
2696 * The map must be locked on entry and will remain locked on return.
2697 * No other requirements.
2700 vm_map_set_wired_quick(vm_map_t map, vm_offset_t addr, vm_size_t size,
2703 vm_map_entry_t scan;
2704 vm_map_entry_t entry;
2706 entry = vm_map_clip_range(map, addr, addr + size,
2707 countp, MAP_CLIP_NO_HOLES);
2709 while (scan && scan->start < addr + size) {
2710 KKASSERT(scan->wired_count == 0);
2711 scan->wired_count = 1;
2712 scan = vm_map_rb_tree_RB_NEXT(scan);
2714 vm_map_unclip_range(map, entry, addr, addr + size,
2715 countp, MAP_CLIP_NO_HOLES);
2719 * Push any dirty cached pages in the address range to their pager.
2720 * If syncio is TRUE, dirty pages are written synchronously.
2721 * If invalidate is TRUE, any cached pages are freed as well.
2723 * This routine is called by sys_msync()
2725 * Returns an error if any part of the specified range is not mapped.
2730 vm_map_clean(vm_map_t map, vm_offset_t start, vm_offset_t end,
2731 boolean_t syncio, boolean_t invalidate)
2733 vm_map_entry_t current;
2734 vm_map_entry_t next;
2735 vm_map_entry_t entry;
2739 vm_ooffset_t offset;
2741 vm_map_lock_read(map);
2742 VM_MAP_RANGE_CHECK(map, start, end);
2743 if (!vm_map_lookup_entry(map, start, &entry)) {
2744 vm_map_unlock_read(map);
2745 return (KERN_INVALID_ADDRESS);
2747 lwkt_gettoken(&map->token);
2750 * Make a first pass to check for holes.
2753 while (current && current->start < end) {
2754 if (current->maptype == VM_MAPTYPE_SUBMAP) {
2755 lwkt_reltoken(&map->token);
2756 vm_map_unlock_read(map);
2757 return (KERN_INVALID_ARGUMENT);
2759 next = vm_map_rb_tree_RB_NEXT(current);
2760 if (end > current->end &&
2762 current->end != next->start)) {
2763 lwkt_reltoken(&map->token);
2764 vm_map_unlock_read(map);
2765 return (KERN_INVALID_ADDRESS);
2771 pmap_remove(vm_map_pmap(map), start, end);
2774 * Make a second pass, cleaning/uncaching pages from the indicated
2778 while (current && current->start < end) {
2779 offset = current->offset + (start - current->start);
2780 size = (end <= current->end ? end : current->end) - start;
2782 switch(current->maptype) {
2783 case VM_MAPTYPE_SUBMAP:
2786 vm_map_entry_t tentry;
2789 smap = current->object.sub_map;
2790 vm_map_lock_read(smap);
2791 vm_map_lookup_entry(smap, offset, &tentry);
2792 if (tentry == NULL) {
2793 tsize = vm_map_max(smap) - offset;
2795 offset = 0 + (offset - vm_map_min(smap));
2797 tsize = tentry->end - offset;
2798 object = tentry->object.vm_object;
2799 offset = tentry->offset +
2800 (offset - tentry->start);
2802 vm_map_unlock_read(smap);
2807 case VM_MAPTYPE_NORMAL:
2808 case VM_MAPTYPE_VPAGETABLE:
2809 object = current->object.vm_object;
2817 vm_object_hold(object);
2820 * Note that there is absolutely no sense in writing out
2821 * anonymous objects, so we track down the vnode object
2823 * We invalidate (remove) all pages from the address space
2824 * anyway, for semantic correctness.
2826 * note: certain anonymous maps, such as MAP_NOSYNC maps,
2827 * may start out with a NULL object.
2829 while (object && (tobj = object->backing_object) != NULL) {
2830 vm_object_hold(tobj);
2831 if (tobj == object->backing_object) {
2832 vm_object_lock_swap();
2833 offset += object->backing_object_offset;
2834 vm_object_drop(object);
2836 if (object->size < OFF_TO_IDX(offset + size))
2837 size = IDX_TO_OFF(object->size) -
2841 vm_object_drop(tobj);
2843 if (object && (object->type == OBJT_VNODE) &&
2844 (current->protection & VM_PROT_WRITE) &&
2845 (object->flags & OBJ_NOMSYNC) == 0) {
2847 * Flush pages if writing is allowed, invalidate them
2848 * if invalidation requested. Pages undergoing I/O
2849 * will be ignored by vm_object_page_remove().
2851 * We cannot lock the vnode and then wait for paging
2852 * to complete without deadlocking against vm_fault.
2853 * Instead we simply call vm_object_page_remove() and
2854 * allow it to block internally on a page-by-page
2855 * basis when it encounters pages undergoing async
2860 /* no chain wait needed for vnode objects */
2861 vm_object_reference_locked(object);
2862 vn_lock(object->handle, LK_EXCLUSIVE | LK_RETRY);
2863 flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
2864 flags |= invalidate ? OBJPC_INVAL : 0;
2867 * When operating on a virtual page table just
2868 * flush the whole object. XXX we probably ought
2871 switch(current->maptype) {
2872 case VM_MAPTYPE_NORMAL:
2873 vm_object_page_clean(object,
2875 OFF_TO_IDX(offset + size + PAGE_MASK),
2878 case VM_MAPTYPE_VPAGETABLE:
2879 vm_object_page_clean(object, 0, 0, flags);
2882 vn_unlock(((struct vnode *)object->handle));
2883 vm_object_deallocate_locked(object);
2885 if (object && invalidate &&
2886 ((object->type == OBJT_VNODE) ||
2887 (object->type == OBJT_DEVICE) ||
2888 (object->type == OBJT_MGTDEVICE))) {
2890 ((object->type == OBJT_DEVICE) ||
2891 (object->type == OBJT_MGTDEVICE)) ? FALSE : TRUE;
2892 /* no chain wait needed for vnode/device objects */
2893 vm_object_reference_locked(object);
2894 switch(current->maptype) {
2895 case VM_MAPTYPE_NORMAL:
2896 vm_object_page_remove(object,
2898 OFF_TO_IDX(offset + size + PAGE_MASK),
2901 case VM_MAPTYPE_VPAGETABLE:
2902 vm_object_page_remove(object, 0, 0, clean_only);
2905 vm_object_deallocate_locked(object);
2909 vm_object_drop(object);
2910 current = vm_map_rb_tree_RB_NEXT(current);
2913 lwkt_reltoken(&map->token);
2914 vm_map_unlock_read(map);
2916 return (KERN_SUCCESS);
2920 * Make the region specified by this entry pageable.
2922 * The vm_map must be exclusively locked.
2925 vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
2927 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2928 entry->wired_count = 0;
2929 vm_fault_unwire(map, entry);
2933 * Deallocate the given entry from the target map.
2935 * The vm_map must be exclusively locked.
2938 vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry, int *countp)
2940 vm_map_entry_unlink(map, entry);
2941 map->size -= entry->end - entry->start;
2943 switch(entry->maptype) {
2944 case VM_MAPTYPE_NORMAL:
2945 case VM_MAPTYPE_VPAGETABLE:
2946 case VM_MAPTYPE_SUBMAP:
2947 vm_object_deallocate(entry->object.vm_object);
2949 case VM_MAPTYPE_UKSMAP:
2956 vm_map_entry_dispose(map, entry, countp);
2960 * Deallocates the given address range from the target map.
2962 * The vm_map must be exclusively locked.
2965 vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end, int *countp)
2968 vm_map_entry_t entry;
2969 vm_map_entry_t first_entry;
2970 vm_offset_t hole_start;
2972 ASSERT_VM_MAP_LOCKED(map);
2973 lwkt_gettoken(&map->token);
2976 * Find the start of the region, and clip it. Set entry to point
2977 * at the first record containing the requested address or, if no
2978 * such record exists, the next record with a greater address. The
2979 * loop will run from this point until a record beyond the termination
2980 * address is encountered.
2982 * Adjust freehint[] for either the clip case or the extension case.
2984 * GGG see other GGG comment.
2986 if (vm_map_lookup_entry(map, start, &first_entry)) {
2987 entry = first_entry;
2988 vm_map_clip_start(map, entry, start, countp);
2992 entry = vm_map_rb_tree_RB_NEXT(first_entry);
2994 hole_start = first_entry->start;
2996 hole_start = first_entry->end;
2998 entry = RB_MIN(vm_map_rb_tree, &map->rb_root);
3000 hole_start = vm_map_min(map);
3002 hole_start = vm_map_max(map);
3007 * Step through all entries in this region
3009 while (entry && entry->start < end) {
3010 vm_map_entry_t next;
3012 vm_pindex_t offidxstart, offidxend, count;
3015 * If we hit an in-transition entry we have to sleep and
3016 * retry. It's easier (and not really slower) to just retry
3017 * since this case occurs so rarely and the hint is already
3018 * pointing at the right place. We have to reset the
3019 * start offset so as not to accidently delete an entry
3020 * another process just created in vacated space.
3022 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
3023 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
3024 start = entry->start;
3025 ++mycpu->gd_cnt.v_intrans_coll;
3026 ++mycpu->gd_cnt.v_intrans_wait;
3027 vm_map_transition_wait(map, 1);
3030 vm_map_clip_end(map, entry, end, countp);
3034 next = vm_map_rb_tree_RB_NEXT(entry);
3036 offidxstart = OFF_TO_IDX(entry->offset);
3037 count = OFF_TO_IDX(e - s);
3039 switch(entry->maptype) {
3040 case VM_MAPTYPE_NORMAL:
3041 case VM_MAPTYPE_VPAGETABLE:
3042 case VM_MAPTYPE_SUBMAP:
3043 object = entry->object.vm_object;
3051 * Unwire before removing addresses from the pmap; otherwise,
3052 * unwiring will put the entries back in the pmap.
3054 * Generally speaking, doing a bulk pmap_remove() before
3055 * removing the pages from the VM object is better at
3056 * reducing unnecessary IPIs. The pmap code is now optimized
3057 * to not blindly iterate the range when pt and pd pages
3060 if (entry->wired_count != 0)
3061 vm_map_entry_unwire(map, entry);
3063 offidxend = offidxstart + count;
3065 if (object == &kernel_object) {
3066 pmap_remove(map->pmap, s, e);
3067 vm_object_hold(object);
3068 vm_object_page_remove(object, offidxstart,
3070 vm_object_drop(object);
3071 } else if (object && object->type != OBJT_DEFAULT &&
3072 object->type != OBJT_SWAP) {
3074 * vnode object routines cannot be chain-locked,
3075 * but since we aren't removing pages from the
3076 * object here we can use a shared hold.
3078 vm_object_hold_shared(object);
3079 pmap_remove(map->pmap, s, e);
3080 vm_object_drop(object);
3081 } else if (object) {
3082 vm_object_hold(object);
3083 vm_object_chain_acquire(object, 0);
3084 pmap_remove(map->pmap, s, e);
3086 if (object != NULL &&
3087 object->ref_count != 1 &&
3088 (object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) ==
3090 (object->type == OBJT_DEFAULT ||
3091 object->type == OBJT_SWAP)) {
3093 * When ONEMAPPING is set we can destroy the
3094 * pages underlying the entry's range.
3096 vm_object_collapse(object, NULL);
3097 vm_object_page_remove(object, offidxstart,
3099 if (object->type == OBJT_SWAP) {
3100 swap_pager_freespace(object,
3104 if (offidxend >= object->size &&
3105 offidxstart < object->size) {
3106 object->size = offidxstart;
3109 vm_object_chain_release(object);
3110 vm_object_drop(object);
3111 } else if (entry->maptype == VM_MAPTYPE_UKSMAP) {
3112 pmap_remove(map->pmap, s, e);
3116 * Delete the entry (which may delete the object) only after
3117 * removing all pmap entries pointing to its pages.
3118 * (Otherwise, its page frames may be reallocated, and any
3119 * modify bits will be set in the wrong object!)
3121 vm_map_entry_delete(map, entry, countp);
3126 * We either reached the end and use vm_map_max as the end
3127 * address, or we didn't and we use the next entry as the
3130 if (entry == NULL) {
3131 vm_map_freehint_hole(map, hole_start,
3132 vm_map_max(map) - hole_start);
3134 vm_map_freehint_hole(map, hole_start,
3135 entry->start - hole_start);
3138 lwkt_reltoken(&map->token);
3140 return (KERN_SUCCESS);
3144 * Remove the given address range from the target map.
3145 * This is the exported form of vm_map_delete.
3150 vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
3155 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
3157 VM_MAP_RANGE_CHECK(map, start, end);
3158 result = vm_map_delete(map, start, end, &count);
3160 vm_map_entry_release(count);
3166 * Assert that the target map allows the specified privilege on the
3167 * entire address region given. The entire region must be allocated.
3169 * The caller must specify whether the vm_map is already locked or not.
3172 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
3173 vm_prot_t protection, boolean_t have_lock)
3175 vm_map_entry_t entry;
3176 vm_map_entry_t tmp_entry;
3179 if (have_lock == FALSE)
3180 vm_map_lock_read(map);
3182 if (!vm_map_lookup_entry(map, start, &tmp_entry)) {
3183 if (have_lock == FALSE)
3184 vm_map_unlock_read(map);
3190 while (start < end) {
3191 if (entry == NULL) {
3200 if (start < entry->start) {
3205 * Check protection associated with entry.
3208 if ((entry->protection & protection) != protection) {
3212 /* go to next entry */
3214 entry = vm_map_rb_tree_RB_NEXT(entry);
3216 if (have_lock == FALSE)
3217 vm_map_unlock_read(map);
3222 * If appropriate this function shadows the original object with a new object
3223 * and moves the VM pages from the original object to the new object.
3224 * The original object will also be collapsed, if possible.
3226 * Caller must supply entry->object.vm_object held and chain_acquired, and
3227 * should chain_release and drop the object upon return.
3229 * We can only do this for normal memory objects with a single mapping, and
3230 * it only makes sense to do it if there are 2 or more refs on the original
3231 * object. i.e. typically a memory object that has been extended into
3232 * multiple vm_map_entry's with non-overlapping ranges.
3234 * This makes it easier to remove unused pages and keeps object inheritance
3235 * from being a negative impact on memory usage.
3237 * On return the (possibly new) entry->object.vm_object will have an
3238 * additional ref on it for the caller to dispose of (usually by cloning
3239 * the vm_map_entry). The additional ref had to be done in this routine
3240 * to avoid racing a collapse. The object's ONEMAPPING flag will also be
3243 * The vm_map must be locked and its token held.
3246 vm_map_split(vm_map_entry_t entry, vm_object_t oobject)
3249 vm_object_t nobject, bobject;
3252 vm_pindex_t offidxstart, offidxend, idx;
3254 vm_ooffset_t offset;
3258 * Optimize away object locks for vnode objects. Important exit/exec
3261 * OBJ_ONEMAPPING doesn't apply to vnode objects but clear the flag
3264 if (oobject->type != OBJT_DEFAULT && oobject->type != OBJT_SWAP) {
3265 vm_object_reference_quick(oobject);
3266 vm_object_clear_flag(oobject, OBJ_ONEMAPPING);
3272 * Original object cannot be split?
3274 if (oobject->handle == NULL) {
3275 vm_object_reference_locked_chain_held(oobject);
3276 vm_object_clear_flag(oobject, OBJ_ONEMAPPING);
3282 * Collapse original object with its backing store as an
3283 * optimization to reduce chain lengths when possible.
3285 * If ref_count <= 1 there aren't other non-overlapping vm_map_entry's
3286 * for oobject, so there's no point collapsing it.
3288 * Then re-check whether the object can be split.
3290 vm_object_collapse(oobject, NULL);
3292 if (oobject->ref_count <= 1 ||
3293 (oobject->type != OBJT_DEFAULT && oobject->type != OBJT_SWAP) ||
3294 (oobject->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) != OBJ_ONEMAPPING) {
3295 vm_object_reference_locked_chain_held(oobject);
3296 vm_object_clear_flag(oobject, OBJ_ONEMAPPING);
3301 * Acquire the chain lock on the backing object.
3303 * Give bobject an additional ref count for when it will be shadowed
3307 if ((bobject = oobject->backing_object) != NULL) {
3308 if (bobject->type != OBJT_VNODE) {
3310 vm_object_hold(bobject);
3311 vm_object_chain_wait(bobject, 0);
3312 /* ref for shadowing below */
3313 vm_object_reference_locked(bobject);
3314 vm_object_chain_acquire(bobject, 0);
3315 KKASSERT(oobject->backing_object == bobject);
3316 KKASSERT((bobject->flags & OBJ_DEAD) == 0);
3319 * vnodes are not placed on the shadow list but
3320 * they still get another ref for the backing_object
3323 vm_object_reference_quick(bobject);
3328 * Calculate the object page range and allocate the new object.
3330 offset = entry->offset;
3334 offidxstart = OFF_TO_IDX(offset);
3335 offidxend = offidxstart + OFF_TO_IDX(e - s);
3336 size = offidxend - offidxstart;
3338 switch(oobject->type) {
3340 nobject = default_pager_alloc(NULL, IDX_TO_OFF(size),
3344 nobject = swap_pager_alloc(NULL, IDX_TO_OFF(size),
3354 * If we could not allocate nobject just clear ONEMAPPING on
3355 * oobject and return.
3357 if (nobject == NULL) {
3359 if (useshadowlist) {
3360 vm_object_chain_release(bobject);
3361 vm_object_deallocate(bobject);
3362 vm_object_drop(bobject);
3364 vm_object_deallocate(bobject);
3367 vm_object_reference_locked_chain_held(oobject);
3368 vm_object_clear_flag(oobject, OBJ_ONEMAPPING);
3373 * The new object will replace entry->object.vm_object so it needs
3374 * a second reference (the caller expects an additional ref).
3376 vm_object_hold(nobject);
3377 vm_object_reference_locked(nobject);
3378 vm_object_chain_acquire(nobject, 0);
3381 * nobject shadows bobject (oobject already shadows bobject).
3383 * Adding an object to bobject's shadow list requires refing bobject
3384 * which we did above in the useshadowlist case.
3386 * XXX it is unclear if we need to clear ONEMAPPING on bobject here
3390 nobject->backing_object_offset =
3391 oobject->backing_object_offset + IDX_TO_OFF(offidxstart);
3392 nobject->backing_object = bobject;
3393 if (useshadowlist) {
3394 bobject->shadow_count++;
3395 atomic_add_int(&bobject->generation, 1);
3396 LIST_INSERT_HEAD(&bobject->shadow_head,
3397 nobject, shadow_list);
3398 vm_object_clear_flag(bobject, OBJ_ONEMAPPING); /*XXX*/
3399 vm_object_set_flag(nobject, OBJ_ONSHADOW);
3404 * Move the VM pages from oobject to nobject
3406 for (idx = 0; idx < size; idx++) {
3409 m = vm_page_lookup_busy_wait(oobject, offidxstart + idx,
3415 * We must wait for pending I/O to complete before we can
3418 * We do not have to VM_PROT_NONE the page as mappings should
3419 * not be changed by this operation.
3421 * NOTE: The act of renaming a page updates chaingen for both
3424 vm_page_rename(m, nobject, idx);
3425 /* page automatically made dirty by rename and cache handled */
3426 /* page remains busy */
3429 if (oobject->type == OBJT_SWAP) {
3430 vm_object_pip_add(oobject, 1);
3432 * copy oobject pages into nobject and destroy unneeded
3433 * pages in shadow object.
3435 swap_pager_copy(oobject, nobject, offidxstart, 0);
3436 vm_object_pip_wakeup(oobject);
3440 * Wakeup the pages we played with. No spl protection is needed
3441 * for a simple wakeup.
3443 for (idx = 0; idx < size; idx++) {
3444 m = vm_page_lookup(nobject, idx);
3446 KKASSERT(m->busy_count & PBUSY_LOCKED);
3450 entry->object.vm_object = nobject;
3451 entry->offset = 0LL;
3454 * The map is being split and nobject is going to wind up on both
3455 * vm_map_entry's, so make sure OBJ_ONEMAPPING is cleared on
3458 vm_object_clear_flag(nobject, OBJ_ONEMAPPING);
3463 * NOTE: There is no need to remove OBJ_ONEMAPPING from oobject, the
3464 * related pages were moved and are no longer applicable to the
3467 * NOTE: Deallocate oobject (due to its entry->object.vm_object being
3468 * replaced by nobject).
3470 vm_object_chain_release(nobject);
3471 vm_object_drop(nobject);
3472 if (bobject && useshadowlist) {
3473 vm_object_chain_release(bobject);
3474 vm_object_drop(bobject);
3478 if (oobject->resident_page_count) {
3479 kprintf("oobject %p still contains %jd pages!\n",
3480 oobject, (intmax_t)oobject->resident_page_count);
3481 for (idx = 0; idx < size; idx++) {
3484 m = vm_page_lookup_busy_wait(oobject, offidxstart + idx,
3487 kprintf("oobject %p idx %jd\n",
3495 /*vm_object_clear_flag(oobject, OBJ_ONEMAPPING);*/
3496 vm_object_deallocate_locked(oobject);
3500 * Copies the contents of the source entry to the destination
3501 * entry. The entries *must* be aligned properly.
3503 * The vm_maps must be exclusively locked.
3504 * The vm_map's token must be held.
3506 * Because the maps are locked no faults can be in progress during the
3510 vm_map_copy_entry(vm_map_t src_map, vm_map_t dst_map,
3511 vm_map_entry_t src_entry, vm_map_entry_t dst_entry)
3513 vm_object_t src_object;
3514 vm_object_t oobject;
3516 if (dst_entry->maptype == VM_MAPTYPE_SUBMAP ||
3517 dst_entry->maptype == VM_MAPTYPE_UKSMAP)
3519 if (src_entry->maptype == VM_MAPTYPE_SUBMAP ||
3520 src_entry->maptype == VM_MAPTYPE_UKSMAP)
3523 if (src_entry->wired_count == 0) {
3525 * If the source entry is marked needs_copy, it is already
3528 * To avoid interacting with a vm_fault that might have
3529 * released its vm_map, we must acquire the fronting
3532 oobject = src_entry->object.vm_object;
3534 vm_object_hold(oobject);
3535 vm_object_chain_acquire(oobject, 0);
3538 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
3539 pmap_protect(src_map->pmap,
3542 src_entry->protection & ~VM_PROT_WRITE);
3546 * Make a copy of the object.
3548 * The object must be locked prior to checking the object type
3549 * and for the call to vm_object_collapse() and vm_map_split().
3550 * We cannot use *_hold() here because the split code will
3551 * probably try to destroy the object. The lock is a pool
3552 * token and doesn't care.
3554 * We must bump src_map->timestamp when setting
3555 * MAP_ENTRY_NEEDS_COPY to force any concurrent fault
3556 * to retry, otherwise the concurrent fault might improperly
3557 * install a RW pte when its supposed to be a RO(COW) pte.
3558 * This race can occur because a vnode-backed fault may have
3559 * to temporarily release the map lock. This was handled
3560 * when the caller locked the map exclusively.
3563 vm_map_split(src_entry, oobject);
3565 src_object = src_entry->object.vm_object;
3566 dst_entry->object.vm_object = src_object;
3567 src_entry->eflags |= (MAP_ENTRY_COW |
3568 MAP_ENTRY_NEEDS_COPY);
3569 dst_entry->eflags |= (MAP_ENTRY_COW |
3570 MAP_ENTRY_NEEDS_COPY);
3571 dst_entry->offset = src_entry->offset;
3573 dst_entry->object.vm_object = NULL;
3574 dst_entry->offset = 0;
3576 pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
3577 dst_entry->end - dst_entry->start,
3580 vm_object_chain_release(oobject);
3581 vm_object_drop(oobject);
3585 * Of course, wired down pages can't be set copy-on-write.
3586 * Cause wired pages to be copied into the new map by
3587 * simulating faults (the new pages are pageable)
3589 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry);
3595 * Create a new process vmspace structure and vm_map
3596 * based on those of an existing process. The new map
3597 * is based on the old map, according to the inheritance
3598 * values on the regions in that map.
3600 * The source map must not be locked.
3603 static void vmspace_fork_normal_entry(vm_map_t old_map, vm_map_t new_map,
3604 vm_map_entry_t old_entry, int *countp);
3605 static void vmspace_fork_uksmap_entry(vm_map_t old_map, vm_map_t new_map,
3606 vm_map_entry_t old_entry, int *countp);
3609 vmspace_fork(struct vmspace *vm1)
3611 struct vmspace *vm2;
3612 vm_map_t old_map = &vm1->vm_map;
3614 vm_map_entry_t old_entry;
3617 lwkt_gettoken(&vm1->vm_map.token);
3618 vm_map_lock(old_map);
3620 vm2 = vmspace_alloc(vm_map_min(old_map), vm_map_max(old_map));
3621 lwkt_gettoken(&vm2->vm_map.token);
3624 * We must bump the timestamp to force any concurrent fault
3627 bcopy(&vm1->vm_startcopy, &vm2->vm_startcopy,
3628 (caddr_t)&vm1->vm_endcopy - (caddr_t)&vm1->vm_startcopy);
3629 new_map = &vm2->vm_map; /* XXX */
3630 new_map->timestamp = 1;
3632 vm_map_lock(new_map);
3634 count = old_map->nentries;
3635 count = vm_map_entry_reserve(count + MAP_RESERVE_COUNT);
3637 RB_FOREACH(old_entry, vm_map_rb_tree, &old_map->rb_root) {
3638 switch(old_entry->maptype) {
3639 case VM_MAPTYPE_SUBMAP:
3640 panic("vm_map_fork: encountered a submap");
3642 case VM_MAPTYPE_UKSMAP:
3643 vmspace_fork_uksmap_entry(old_map, new_map,
3646 case VM_MAPTYPE_NORMAL:
3647 case VM_MAPTYPE_VPAGETABLE:
3648 vmspace_fork_normal_entry(old_map, new_map,
3654 new_map->size = old_map->size;
3655 vm_map_unlock(old_map);
3656 vm_map_unlock(new_map);
3657 vm_map_entry_release(count);
3659 lwkt_reltoken(&vm2->vm_map.token);
3660 lwkt_reltoken(&vm1->vm_map.token);
3667 vmspace_fork_normal_entry(vm_map_t old_map, vm_map_t new_map,
3668 vm_map_entry_t old_entry, int *countp)
3670 vm_map_entry_t new_entry;
3673 switch (old_entry->inheritance) {
3674 case VM_INHERIT_NONE:
3676 case VM_INHERIT_SHARE:
3678 * Clone the entry, creating the shared object if
3681 if (old_entry->object.vm_object == NULL)
3682 vm_map_entry_allocate_object(old_entry);
3684 if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3686 * Shadow a map_entry which needs a copy,
3687 * replacing its object with a new object
3688 * that points to the old one. Ask the
3689 * shadow code to automatically add an
3690 * additional ref. We can't do it afterwords
3691 * because we might race a collapse. The call
3692 * to vm_map_entry_shadow() will also clear
3695 vm_map_entry_shadow(old_entry, 1);
3696 } else if (old_entry->object.vm_object) {
3698 * We will make a shared copy of the object,
3699 * and must clear OBJ_ONEMAPPING.
3701 * Optimize vnode objects. OBJ_ONEMAPPING
3702 * is non-applicable but clear it anyway,
3703 * and its terminal so we don't have to deal
3704 * with chains. Reduces SMP conflicts.
3706 * XXX assert that object.vm_object != NULL
3707 * since we allocate it above.
3709 object = old_entry->object.vm_object;
3710 if (object->type == OBJT_VNODE) {
3711 vm_object_reference_quick(object);
3712 vm_object_clear_flag(object,
3715 vm_object_hold(object);
3716 vm_object_chain_wait(object, 0);
3717 vm_object_reference_locked(object);
3718 vm_object_clear_flag(object,
3720 vm_object_drop(object);
3725 * Clone the entry. We've already bumped the ref on
3728 new_entry = vm_map_entry_create(new_map, countp);
3729 *new_entry = *old_entry;
3730 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
3731 new_entry->wired_count = 0;
3734 * Insert the entry into the new map -- we know we're
3735 * inserting at the end of the new map.
3737 vm_map_entry_link(new_map, new_entry);
3740 * Update the physical map
3742 pmap_copy(new_map->pmap, old_map->pmap,
3744 (old_entry->end - old_entry->start),
3747 case VM_INHERIT_COPY:
3749 * Clone the entry and link into the map.
3751 new_entry = vm_map_entry_create(new_map, countp);
3752 *new_entry = *old_entry;
3753 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
3754 new_entry->wired_count = 0;
3755 new_entry->object.vm_object = NULL;
3756 vm_map_entry_link(new_map, new_entry);
3757 vm_map_copy_entry(old_map, new_map, old_entry,
3764 * When forking user-kernel shared maps, the map might change in the
3765 * child so do not try to copy the underlying pmap entries.
3769 vmspace_fork_uksmap_entry(vm_map_t old_map, vm_map_t new_map,
3770 vm_map_entry_t old_entry, int *countp)
3772 vm_map_entry_t new_entry;
3774 new_entry = vm_map_entry_create(new_map, countp);
3775 *new_entry = *old_entry;
3776 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
3777 new_entry->wired_count = 0;
3778 vm_map_entry_link(new_map, new_entry);
3782 * Create an auto-grow stack entry
3787 vm_map_stack (vm_map_t map, vm_offset_t *addrbos, vm_size_t max_ssize,
3788 int flags, vm_prot_t prot, vm_prot_t max, int cow)
3790 vm_map_entry_t prev_entry;
3791 vm_map_entry_t next;
3792 vm_size_t init_ssize;
3795 vm_offset_t tmpaddr;
3797 cow |= MAP_IS_STACK;
3799 if (max_ssize < sgrowsiz)
3800 init_ssize = max_ssize;
3802 init_ssize = sgrowsiz;
3804 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
3808 * Find space for the mapping
3810 if ((flags & (MAP_FIXED | MAP_TRYFIXED)) == 0) {
3811 if (vm_map_findspace(map, *addrbos, max_ssize, 1,
3814 vm_map_entry_release(count);
3815 return (KERN_NO_SPACE);
3820 /* If addr is already mapped, no go */
3821 if (vm_map_lookup_entry(map, *addrbos, &prev_entry)) {
3823 vm_map_entry_release(count);
3824 return (KERN_NO_SPACE);
3828 /* XXX already handled by kern_mmap() */
3829 /* If we would blow our VMEM resource limit, no go */
3830 if (map->size + init_ssize >
3831 curproc->p_rlimit[RLIMIT_VMEM].rlim_cur) {
3833 vm_map_entry_release(count);
3834 return (KERN_NO_SPACE);
3839 * If we can't accomodate max_ssize in the current mapping,
3840 * no go. However, we need to be aware that subsequent user
3841 * mappings might map into the space we have reserved for
3842 * stack, and currently this space is not protected.
3844 * Hopefully we will at least detect this condition
3845 * when we try to grow the stack.
3848 next = vm_map_rb_tree_RB_NEXT(prev_entry);
3850 next = RB_MIN(vm_map_rb_tree, &map->rb_root);
3852 if (next && next->start < *addrbos + max_ssize) {
3854 vm_map_entry_release(count);
3855 return (KERN_NO_SPACE);
3859 * We initially map a stack of only init_ssize. We will
3860 * grow as needed later. Since this is to be a grow
3861 * down stack, we map at the top of the range.
3863 * Note: we would normally expect prot and max to be
3864 * VM_PROT_ALL, and cow to be 0. Possibly we should
3865 * eliminate these as input parameters, and just
3866 * pass these values here in the insert call.
3868 rv = vm_map_insert(map, &count, NULL, NULL,
3869 0, *addrbos + max_ssize - init_ssize,
3870 *addrbos + max_ssize,
3872 VM_SUBSYS_STACK, prot, max, cow);
3874 /* Now set the avail_ssize amount */
3875 if (rv == KERN_SUCCESS) {
3877 next = vm_map_rb_tree_RB_NEXT(prev_entry);
3879 next = RB_MIN(vm_map_rb_tree, &map->rb_root);
3880 if (prev_entry != NULL) {
3881 vm_map_clip_end(map,
3883 *addrbos + max_ssize - init_ssize,
3886 if (next->end != *addrbos + max_ssize ||
3887 next->start != *addrbos + max_ssize - init_ssize){
3888 panic ("Bad entry start/end for new stack entry");
3890 next->aux.avail_ssize = max_ssize - init_ssize;
3895 vm_map_entry_release(count);
3900 * Attempts to grow a vm stack entry. Returns KERN_SUCCESS if the
3901 * desired address is already mapped, or if we successfully grow
3902 * the stack. Also returns KERN_SUCCESS if addr is outside the
3903 * stack range (this is strange, but preserves compatibility with
3904 * the grow function in vm_machdep.c).
3909 vm_map_growstack (vm_map_t map, vm_offset_t addr)
3911 vm_map_entry_t prev_entry;
3912 vm_map_entry_t stack_entry;
3913 vm_map_entry_t next;
3919 int rv = KERN_SUCCESS;
3921 int use_read_lock = 1;
3927 lp = curthread->td_lwp;
3928 p = curthread->td_proc;
3929 KKASSERT(lp != NULL);
3930 vm = lp->lwp_vmspace;
3933 * Growstack is only allowed on the current process. We disallow
3934 * other use cases, e.g. trying to access memory via procfs that
3935 * the stack hasn't grown into.
3937 if (map != &vm->vm_map) {
3938 return KERN_FAILURE;
3941 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
3944 vm_map_lock_read(map);
3949 * If addr is already in the entry range, no need to grow.
3950 * prev_entry returns NULL if addr is at the head.
3952 if (vm_map_lookup_entry(map, addr, &prev_entry))
3955 stack_entry = vm_map_rb_tree_RB_NEXT(prev_entry);
3957 stack_entry = RB_MIN(vm_map_rb_tree, &map->rb_root);
3959 if (stack_entry == NULL)
3961 if (prev_entry == NULL)
3962 end = stack_entry->start - stack_entry->aux.avail_ssize;
3964 end = prev_entry->end;
3967 * This next test mimics the old grow function in vm_machdep.c.
3968 * It really doesn't quite make sense, but we do it anyway
3969 * for compatibility.
3971 * If not growable stack, return success. This signals the
3972 * caller to proceed as he would normally with normal vm.
3974 if (stack_entry->aux.avail_ssize < 1 ||
3975 addr >= stack_entry->start ||
3976 addr < stack_entry->start - stack_entry->aux.avail_ssize) {
3980 /* Find the minimum grow amount */
3981 grow_amount = roundup (stack_entry->start - addr, PAGE_SIZE);
3982 if (grow_amount > stack_entry->aux.avail_ssize) {
3988 * If there is no longer enough space between the entries
3989 * nogo, and adjust the available space. Note: this
3990 * should only happen if the user has mapped into the
3991 * stack area after the stack was created, and is
3992 * probably an error.
3994 * This also effectively destroys any guard page the user
3995 * might have intended by limiting the stack size.
3997 if (grow_amount > stack_entry->start - end) {
3998 if (use_read_lock && vm_map_lock_upgrade(map)) {
4004 stack_entry->aux.avail_ssize = stack_entry->start - end;
4009 is_procstack = addr >= (vm_offset_t)vm->vm_maxsaddr;
4011 /* If this is the main process stack, see if we're over the
4014 if (is_procstack && (vm->vm_ssize + grow_amount >
4015 p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
4020 /* Round up the grow amount modulo SGROWSIZ */
4021 grow_amount = roundup (grow_amount, sgrowsiz);
4022 if (grow_amount > stack_entry->aux.avail_ssize) {
4023 grow_amount = stack_entry->aux.avail_ssize;
4025 if (is_procstack && (vm->vm_ssize + grow_amount >
4026 p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
4027 grow_amount = p->p_rlimit[RLIMIT_STACK].rlim_cur - vm->vm_ssize;
4030 /* If we would blow our VMEM resource limit, no go */
4031 if (map->size + grow_amount > p->p_rlimit[RLIMIT_VMEM].rlim_cur) {
4036 if (use_read_lock && vm_map_lock_upgrade(map)) {
4043 /* Get the preliminary new entry start value */
4044 addr = stack_entry->start - grow_amount;
4046 /* If this puts us into the previous entry, cut back our growth
4047 * to the available space. Also, see the note above.
4050 stack_entry->aux.avail_ssize = stack_entry->start - end;
4054 rv = vm_map_insert(map, &count, NULL, NULL,
4055 0, addr, stack_entry->start,
4057 VM_SUBSYS_STACK, VM_PROT_ALL, VM_PROT_ALL, 0);
4059 /* Adjust the available stack space by the amount we grew. */
4060 if (rv == KERN_SUCCESS) {
4062 vm_map_clip_end(map, prev_entry, addr, &count);
4063 next = vm_map_rb_tree_RB_NEXT(prev_entry);
4065 next = RB_MIN(vm_map_rb_tree, &map->rb_root);
4067 if (next->end != stack_entry->start ||
4068 next->start != addr) {
4069 panic ("Bad stack grow start/end in new stack entry");
4071 next->aux.avail_ssize =
4072 stack_entry->aux.avail_ssize -
4073 (next->end - next->start);
4075 vm->vm_ssize += next->end -
4080 if (map->flags & MAP_WIREFUTURE)
4081 vm_map_unwire(map, next->start, next->end, FALSE);
4086 vm_map_unlock_read(map);
4089 vm_map_entry_release(count);
4094 * Unshare the specified VM space for exec. If other processes are
4095 * mapped to it, then create a new one. The new vmspace is null.
4100 vmspace_exec(struct proc *p, struct vmspace *vmcopy)
4102 struct vmspace *oldvmspace = p->p_vmspace;
4103 struct vmspace *newvmspace;
4104 vm_map_t map = &p->p_vmspace->vm_map;
4107 * If we are execing a resident vmspace we fork it, otherwise
4108 * we create a new vmspace. Note that exitingcnt is not
4109 * copied to the new vmspace.
4111 lwkt_gettoken(&oldvmspace->vm_map.token);
4113 newvmspace = vmspace_fork(vmcopy);
4114 lwkt_gettoken(&newvmspace->vm_map.token);
4116 newvmspace = vmspace_alloc(vm_map_min(map), vm_map_max(map));
4117 lwkt_gettoken(&newvmspace->vm_map.token);
4118 bcopy(&oldvmspace->vm_startcopy, &newvmspace->vm_startcopy,
4119 (caddr_t)&oldvmspace->vm_endcopy -
4120 (caddr_t)&oldvmspace->vm_startcopy);
4124 * Finish initializing the vmspace before assigning it
4125 * to the process. The vmspace will become the current vmspace
4128 pmap_pinit2(vmspace_pmap(newvmspace));
4129 pmap_replacevm(p, newvmspace, 0);
4130 lwkt_reltoken(&newvmspace->vm_map.token);
4131 lwkt_reltoken(&oldvmspace->vm_map.token);
4132 vmspace_rel(oldvmspace);
4136 * Unshare the specified VM space for forcing COW. This
4137 * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
4140 vmspace_unshare(struct proc *p)
4142 struct vmspace *oldvmspace = p->p_vmspace;
4143 struct vmspace *newvmspace;
4145 lwkt_gettoken(&oldvmspace->vm_map.token);
4146 if (vmspace_getrefs(oldvmspace) == 1) {
4147 lwkt_reltoken(&oldvmspace->vm_map.token);
4150 newvmspace = vmspace_fork(oldvmspace);
4151 lwkt_gettoken(&newvmspace->vm_map.token);
4152 pmap_pinit2(vmspace_pmap(newvmspace));
4153 pmap_replacevm(p, newvmspace, 0);
4154 lwkt_reltoken(&newvmspace->vm_map.token);
4155 lwkt_reltoken(&oldvmspace->vm_map.token);
4156 vmspace_rel(oldvmspace);
4160 * vm_map_hint: return the beginning of the best area suitable for
4161 * creating a new mapping with "prot" protection.
4166 vm_map_hint(struct proc *p, vm_offset_t addr, vm_prot_t prot)
4168 struct vmspace *vms = p->p_vmspace;
4169 struct rlimit limit;
4173 * Acquire datasize limit for mmap() operation,
4174 * calculate nearest power of 2.
4176 if (kern_getrlimit(RLIMIT_DATA, &limit))
4177 limit.rlim_cur = maxdsiz;
4178 dsiz = limit.rlim_cur;
4180 if (!randomize_mmap || addr != 0) {
4182 * Set a reasonable start point for the hint if it was
4183 * not specified or if it falls within the heap space.
4184 * Hinted mmap()s do not allocate out of the heap space.
4187 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
4188 addr < round_page((vm_offset_t)vms->vm_daddr + dsiz))) {
4189 addr = round_page((vm_offset_t)vms->vm_daddr + dsiz);
4196 * randomize_mmap && addr == 0. For now randomize the
4197 * address within a dsiz range beyond the data limit.
4199 addr = (vm_offset_t)vms->vm_daddr + dsiz;
4201 addr += (karc4random64() & 0x7FFFFFFFFFFFFFFFLU) % dsiz;
4202 return (round_page(addr));
4206 * Finds the VM object, offset, and protection for a given virtual address
4207 * in the specified map, assuming a page fault of the type specified.
4209 * Leaves the map in question locked for read; return values are guaranteed
4210 * until a vm_map_lookup_done call is performed. Note that the map argument
4211 * is in/out; the returned map must be used in the call to vm_map_lookup_done.
4213 * A handle (out_entry) is returned for use in vm_map_lookup_done, to make
4216 * If a lookup is requested with "write protection" specified, the map may
4217 * be changed to perform virtual copying operations, although the data
4218 * referenced will remain the same.
4223 vm_map_lookup(vm_map_t *var_map, /* IN/OUT */
4225 vm_prot_t fault_typea,
4226 vm_map_entry_t *out_entry, /* OUT */
4227 vm_object_t *object, /* OUT */
4228 vm_pindex_t *pindex, /* OUT */
4229 vm_prot_t *out_prot, /* OUT */
4230 int *wflags) /* OUT */
4232 vm_map_entry_t entry;
4233 vm_map_t map = *var_map;
4235 vm_prot_t fault_type = fault_typea;
4236 int use_read_lock = 1;
4237 int rv = KERN_SUCCESS;
4239 thread_t td = curthread;
4242 * vm_map_entry_reserve() implements an important mitigation
4243 * against mmap() span running the kernel out of vm_map_entry
4244 * structures, but it can also cause an infinite call recursion.
4245 * Use td_nest_count to prevent an infinite recursion (allows
4246 * the vm_map code to dig into the pcpu vm_map_entry reserve).
4249 if (td->td_nest_count == 0) {
4250 ++td->td_nest_count;
4251 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
4252 --td->td_nest_count;
4256 vm_map_lock_read(map);
4261 * Always do a full lookup. The hint doesn't get us much anymore
4262 * now that the map is RB'd.
4269 vm_map_entry_t tmp_entry;
4271 if (!vm_map_lookup_entry(map, vaddr, &tmp_entry)) {
4272 rv = KERN_INVALID_ADDRESS;
4282 if (entry->maptype == VM_MAPTYPE_SUBMAP) {
4283 vm_map_t old_map = map;
4285 *var_map = map = entry->object.sub_map;
4287 vm_map_unlock_read(old_map);
4289 vm_map_unlock(old_map);
4295 * Check whether this task is allowed to have this page.
4296 * Note the special case for MAP_ENTRY_COW pages with an override.
4297 * This is to implement a forced COW for debuggers.
4299 if (fault_type & VM_PROT_OVERRIDE_WRITE)
4300 prot = entry->max_protection;
4302 prot = entry->protection;
4304 fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
4305 if ((fault_type & prot) != fault_type) {
4306 rv = KERN_PROTECTION_FAILURE;
4310 if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
4311 (entry->eflags & MAP_ENTRY_COW) &&
4312 (fault_type & VM_PROT_WRITE) &&
4313 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
4314 rv = KERN_PROTECTION_FAILURE;
4319 * If this page is not pageable, we have to get it for all possible
4323 if (entry->wired_count) {
4324 *wflags |= FW_WIRED;
4325 prot = fault_type = entry->protection;
4329 * Virtual page tables may need to update the accessed (A) bit
4330 * in a page table entry. Upgrade the fault to a write fault for
4331 * that case if the map will support it. If the map does not support
4332 * it the page table entry simply will not be updated.
4334 if (entry->maptype == VM_MAPTYPE_VPAGETABLE) {
4335 if (prot & VM_PROT_WRITE)
4336 fault_type |= VM_PROT_WRITE;
4339 if (curthread->td_lwp && curthread->td_lwp->lwp_vmspace &&
4340 pmap_emulate_ad_bits(&curthread->td_lwp->lwp_vmspace->vm_pmap)) {
4341 if ((prot & VM_PROT_WRITE) == 0)
4342 fault_type |= VM_PROT_WRITE;
4346 * Only NORMAL and VPAGETABLE maps are object-based. UKSMAPs are not.
4348 if (entry->maptype != VM_MAPTYPE_NORMAL &&
4349 entry->maptype != VM_MAPTYPE_VPAGETABLE) {
4355 * If the entry was copy-on-write, we either ...
4357 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
4359 * If we want to write the page, we may as well handle that
4360 * now since we've got the map locked.
4362 * If we don't need to write the page, we just demote the
4363 * permissions allowed.
4365 if (fault_type & VM_PROT_WRITE) {
4367 * Not allowed if TDF_NOFAULT is set as the shadowing
4368 * operation can deadlock against the faulting
4369 * function due to the copy-on-write.
4371 if (curthread->td_flags & TDF_NOFAULT) {
4372 rv = KERN_FAILURE_NOFAULT;
4377 * Make a new object, and place it in the object
4378 * chain. Note that no new references have appeared
4379 * -- one just moved from the map to the new
4382 if (use_read_lock && vm_map_lock_upgrade(map)) {
4388 vm_map_entry_shadow(entry, 0);
4389 *wflags |= FW_DIDCOW;
4392 * We're attempting to read a copy-on-write page --
4393 * don't allow writes.
4395 prot &= ~VM_PROT_WRITE;
4400 * Create an object if necessary. This code also handles
4401 * partitioning large entries to improve vm_fault performance.
4403 if (entry->object.vm_object == NULL && !map->system_map) {
4404 if (use_read_lock && vm_map_lock_upgrade(map)) {
4412 * Partition large entries, giving each its own VM object,
4413 * to improve concurrent fault performance. This is only
4414 * applicable to userspace.
4416 if (map != &kernel_map &&
4417 entry->maptype == VM_MAPTYPE_NORMAL &&
4418 ((entry->start ^ entry->end) & ~MAP_ENTRY_PARTITION_MASK) &&
4419 vm_map_partition_enable) {
4420 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
4421 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
4422 ++mycpu->gd_cnt.v_intrans_coll;
4423 ++mycpu->gd_cnt.v_intrans_wait;
4424 vm_map_transition_wait(map, 0);
4427 vm_map_entry_partition(map, entry, vaddr, &count);
4429 vm_map_entry_allocate_object(entry);
4433 * Return the object/offset from this entry. If the entry was
4434 * copy-on-write or empty, it has been fixed up.
4436 *object = entry->object.vm_object;
4439 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
4442 * Return whether this is the only map sharing this data. On
4443 * success we return with a read lock held on the map. On failure
4444 * we return with the map unlocked.
4448 if (rv == KERN_SUCCESS) {
4449 if (use_read_lock == 0)
4450 vm_map_lock_downgrade(map);
4451 } else if (use_read_lock) {
4452 vm_map_unlock_read(map);
4457 vm_map_entry_release(count);
4463 * Releases locks acquired by a vm_map_lookup()
4464 * (according to the handle returned by that lookup).
4466 * No other requirements.
4469 vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry, int count)
4472 * Unlock the main-level map
4474 vm_map_unlock_read(map);
4476 vm_map_entry_release(count);
4480 vm_map_entry_partition(vm_map_t map, vm_map_entry_t entry,
4481 vm_offset_t vaddr, int *countp)
4483 vaddr &= ~MAP_ENTRY_PARTITION_MASK;
4484 vm_map_clip_start(map, entry, vaddr, countp);
4485 vaddr += MAP_ENTRY_PARTITION_SIZE;
4486 vm_map_clip_end(map, entry, vaddr, countp);
4490 * Quick hack, needs some help to make it more SMP friendly.
4493 vm_map_interlock(vm_map_t map, struct vm_map_ilock *ilock,
4494 vm_offset_t ran_beg, vm_offset_t ran_end)
4496 struct vm_map_ilock *scan;
4498 ilock->ran_beg = ran_beg;
4499 ilock->ran_end = ran_end;
4502 spin_lock(&map->ilock_spin);
4504 for (scan = map->ilock_base; scan; scan = scan->next) {
4505 if (ran_end > scan->ran_beg && ran_beg < scan->ran_end) {
4506 scan->flags |= ILOCK_WAITING;
4507 ssleep(scan, &map->ilock_spin, 0, "ilock", 0);
4511 ilock->next = map->ilock_base;
4512 map->ilock_base = ilock;
4513 spin_unlock(&map->ilock_spin);
4517 vm_map_deinterlock(vm_map_t map, struct vm_map_ilock *ilock)
4519 struct vm_map_ilock *scan;
4520 struct vm_map_ilock **scanp;
4522 spin_lock(&map->ilock_spin);
4523 scanp = &map->ilock_base;
4524 while ((scan = *scanp) != NULL) {
4525 if (scan == ilock) {
4526 *scanp = ilock->next;
4527 spin_unlock(&map->ilock_spin);
4528 if (ilock->flags & ILOCK_WAITING)
4532 scanp = &scan->next;
4534 spin_unlock(&map->ilock_spin);
4535 panic("vm_map_deinterlock: missing ilock!");
4538 #include "opt_ddb.h"
4540 #include <ddb/ddb.h>
4545 DB_SHOW_COMMAND(map, vm_map_print)
4548 /* XXX convert args. */
4549 vm_map_t map = (vm_map_t)addr;
4550 boolean_t full = have_addr;
4552 vm_map_entry_t entry;
4554 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
4556 (void *)map->pmap, map->nentries, map->timestamp);
4559 if (!full && db_indent)
4563 RB_FOREACH(entry, vm_map_rb_tree, &map->rb_root) {
4564 db_iprintf("map entry %p: start=%p, end=%p\n",
4565 (void *)entry, (void *)entry->start, (void *)entry->end);
4568 static char *inheritance_name[4] =
4569 {"share", "copy", "none", "donate_copy"};
4571 db_iprintf(" prot=%x/%x/%s",
4573 entry->max_protection,
4574 inheritance_name[(int)(unsigned char)
4575 entry->inheritance]);
4576 if (entry->wired_count != 0)
4577 db_printf(", wired");
4579 switch(entry->maptype) {
4580 case VM_MAPTYPE_SUBMAP:
4581 /* XXX no %qd in kernel. Truncate entry->offset. */
4582 db_printf(", share=%p, offset=0x%lx\n",
4583 (void *)entry->object.sub_map,
4584 (long)entry->offset);
4588 vm_map_print((db_expr_t)(intptr_t)
4589 entry->object.sub_map,
4593 case VM_MAPTYPE_NORMAL:
4594 case VM_MAPTYPE_VPAGETABLE:
4595 /* XXX no %qd in kernel. Truncate entry->offset. */
4596 db_printf(", object=%p, offset=0x%lx",
4597 (void *)entry->object.vm_object,
4598 (long)entry->offset);
4599 if (entry->eflags & MAP_ENTRY_COW)
4600 db_printf(", copy (%s)",
4601 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
4605 if (entry->object.vm_object) {
4607 vm_object_print((db_expr_t)(intptr_t)
4608 entry->object.vm_object,
4614 case VM_MAPTYPE_UKSMAP:
4615 db_printf(", uksmap=%p, offset=0x%lx",
4616 (void *)entry->object.uksmap,
4617 (long)entry->offset);
4618 if (entry->eflags & MAP_ENTRY_COW)
4619 db_printf(", copy (%s)",
4620 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
4636 DB_SHOW_COMMAND(procvm, procvm)
4641 p = (struct proc *) addr;
4646 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
4647 (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
4648 (void *)vmspace_pmap(p->p_vmspace));
4650 vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);