4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * The Mach Operating System project at Carnegie-Mellon University.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * from: @(#)vm_map.c 8.3 (Berkeley) 1/12/94
41 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
42 * All rights reserved.
44 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
46 * Permission to use, copy, modify and distribute this software and
47 * its documentation is hereby granted, provided that both the copyright
48 * notice and this permission notice appear in all copies of the
49 * software, derivative works or modified versions, and any portions
50 * thereof, and that both notices appear in supporting documentation.
52 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
53 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
54 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
56 * Carnegie Mellon requests users of this software to return to
58 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
59 * School of Computer Science
60 * Carnegie Mellon University
61 * Pittsburgh PA 15213-3890
63 * any improvements or extensions that they make and grant Carnegie the
64 * rights to redistribute these changes.
66 * $FreeBSD: src/sys/vm/vm_map.c,v 1.187.2.19 2003/05/27 00:47:02 alc Exp $
67 * $DragonFly: src/sys/vm/vm_map.c,v 1.56 2007/04/29 18:25:41 dillon Exp $
71 * Virtual memory mapping module.
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/kernel.h>
78 #include <sys/serialize.h>
80 #include <sys/vmmeter.h>
82 #include <sys/vnode.h>
83 #include <sys/resourcevar.h>
86 #include <sys/malloc.h>
89 #include <vm/vm_param.h>
91 #include <vm/vm_map.h>
92 #include <vm/vm_page.h>
93 #include <vm/vm_object.h>
94 #include <vm/vm_pager.h>
95 #include <vm/vm_kern.h>
96 #include <vm/vm_extern.h>
97 #include <vm/swap_pager.h>
98 #include <vm/vm_zone.h>
100 #include <sys/thread2.h>
101 #include <sys/sysref2.h>
102 #include <sys/random.h>
103 #include <sys/sysctl.h>
106 * Virtual memory maps provide for the mapping, protection, and sharing
107 * of virtual memory objects. In addition, this module provides for an
108 * efficient virtual copy of memory from one map to another.
110 * Synchronization is required prior to most operations.
112 * Maps consist of an ordered doubly-linked list of simple entries.
113 * A hint and a RB tree is used to speed-up lookups.
115 * Callers looking to modify maps specify start/end addresses which cause
116 * the related map entry to be clipped if necessary, and then later
117 * recombined if the pieces remained compatible.
119 * Virtual copy operations are performed by copying VM object references
120 * from one map to another, and then marking both regions as copy-on-write.
122 static void vmspace_terminate(struct vmspace *vm);
123 static void vmspace_lock(struct vmspace *vm);
124 static void vmspace_unlock(struct vmspace *vm);
125 static void vmspace_dtor(void *obj, void *private);
127 MALLOC_DEFINE(M_VMSPACE, "vmspace", "vmspace objcache backingstore");
129 struct sysref_class vmspace_sysref_class = {
132 .proto = SYSREF_PROTO_VMSPACE,
133 .offset = offsetof(struct vmspace, vm_sysref),
134 .objsize = sizeof(struct vmspace),
136 .flags = SRC_MANAGEDINIT,
137 .dtor = vmspace_dtor,
139 .terminate = (sysref_terminate_func_t)vmspace_terminate,
140 .lock = (sysref_lock_func_t)vmspace_lock,
141 .unlock = (sysref_lock_func_t)vmspace_unlock
146 * per-cpu page table cross mappings are initialized in early boot
147 * and might require a considerable number of vm_map_entry structures.
149 #define VMEPERCPU (MAXCPU+1)
151 static struct vm_zone mapentzone_store, mapzone_store;
152 static vm_zone_t mapentzone, mapzone;
153 static struct vm_object mapentobj, mapobj;
155 static struct vm_map_entry map_entry_init[MAX_MAPENT];
156 static struct vm_map_entry cpu_map_entry_init[MAXCPU][VMEPERCPU];
157 static struct vm_map map_init[MAX_KMAP];
159 static int randomize_mmap;
160 SYSCTL_INT(_vm, OID_AUTO, randomize_mmap, CTLFLAG_RW, &randomize_mmap, 0,
161 "Randomize mmap offsets");
163 static void vm_map_entry_shadow(vm_map_entry_t entry, int addref);
164 static vm_map_entry_t vm_map_entry_create(vm_map_t map, int *);
165 static void vm_map_entry_dispose (vm_map_t map, vm_map_entry_t entry, int *);
166 static void _vm_map_clip_end (vm_map_t, vm_map_entry_t, vm_offset_t, int *);
167 static void _vm_map_clip_start (vm_map_t, vm_map_entry_t, vm_offset_t, int *);
168 static void vm_map_entry_delete (vm_map_t, vm_map_entry_t, int *);
169 static void vm_map_entry_unwire (vm_map_t, vm_map_entry_t);
170 static void vm_map_copy_entry (vm_map_t, vm_map_t, vm_map_entry_t,
172 static void vm_map_unclip_range (vm_map_t map, vm_map_entry_t start_entry, vm_offset_t start, vm_offset_t end, int *count, int flags);
175 * Initialize the vm_map module. Must be called before any other vm_map
178 * Map and entry structures are allocated from the general purpose
179 * memory pool with some exceptions:
181 * - The kernel map is allocated statically.
182 * - Initial kernel map entries are allocated out of a static pool.
184 * These restrictions are necessary since malloc() uses the
185 * maps and requires map entries.
187 * Called from the low level boot code only.
192 mapzone = &mapzone_store;
193 zbootinit(mapzone, "MAP", sizeof (struct vm_map),
195 mapentzone = &mapentzone_store;
196 zbootinit(mapentzone, "MAP ENTRY", sizeof (struct vm_map_entry),
197 map_entry_init, MAX_MAPENT);
201 * Called prior to any vmspace allocations.
203 * Called from the low level boot code only.
208 zinitna(mapentzone, &mapentobj, NULL, 0, 0,
209 ZONE_USE_RESERVE | ZONE_SPECIAL, 1);
210 zinitna(mapzone, &mapobj, NULL, 0, 0, 0, 1);
217 * Red black tree functions
219 * The caller must hold the related map lock.
221 static int rb_vm_map_compare(vm_map_entry_t a, vm_map_entry_t b);
222 RB_GENERATE(vm_map_rb_tree, vm_map_entry, rb_entry, rb_vm_map_compare);
224 /* a->start is address, and the only field has to be initialized */
226 rb_vm_map_compare(vm_map_entry_t a, vm_map_entry_t b)
228 if (a->start < b->start)
230 else if (a->start > b->start)
236 * Allocate a vmspace structure, including a vm_map and pmap.
237 * Initialize numerous fields. While the initial allocation is zerod,
238 * subsequence reuse from the objcache leaves elements of the structure
239 * intact (particularly the pmap), so portions must be zerod.
241 * The structure is not considered activated until we call sysref_activate().
246 vmspace_alloc(vm_offset_t min, vm_offset_t max)
250 vm = sysref_alloc(&vmspace_sysref_class);
251 bzero(&vm->vm_startcopy,
252 (char *)&vm->vm_endcopy - (char *)&vm->vm_startcopy);
253 vm_map_init(&vm->vm_map, min, max, NULL); /* initializes token */
256 * Use a hold to prevent any additional racing hold from terminating
257 * the vmspace before we manage to activate it. This also acquires
258 * the token for safety.
260 KKASSERT(vm->vm_holdcount == 0);
261 KKASSERT(vm->vm_exitingcnt == 0);
263 pmap_pinit(vmspace_pmap(vm)); /* (some fields reused) */
264 vm->vm_map.pmap = vmspace_pmap(vm); /* XXX */
267 cpu_vmspace_alloc(vm);
268 sysref_activate(&vm->vm_sysref);
275 * Free a primary reference to a vmspace. This can trigger a
276 * stage-1 termination.
279 vmspace_free(struct vmspace *vm)
282 * We want all finalization to occur via vmspace_drop() so we
283 * need to hold the vm around the put.
286 sysref_put(&vm->vm_sysref);
291 vmspace_ref(struct vmspace *vm)
293 sysref_get(&vm->vm_sysref);
297 vmspace_hold(struct vmspace *vm)
299 refcount_acquire(&vm->vm_holdcount);
300 lwkt_gettoken(&vm->vm_map.token);
304 vmspace_drop(struct vmspace *vm)
306 lwkt_reltoken(&vm->vm_map.token);
307 if (refcount_release(&vm->vm_holdcount)) {
308 if (vm->vm_exitingcnt == 0 &&
309 sysref_isinactive(&vm->vm_sysref)) {
310 vmspace_terminate(vm);
316 * dtor function - Some elements of the pmap are retained in the
317 * free-cached vmspaces to improve performance. We have to clean them up
318 * here before returning the vmspace to the memory pool.
323 vmspace_dtor(void *obj, void *private)
325 struct vmspace *vm = obj;
327 pmap_puninit(vmspace_pmap(vm));
331 * Called in three cases:
333 * (1) When the last sysref is dropped and the vmspace becomes inactive.
334 * (holdcount will not be 0 because the vmspace is held through the op)
336 * (2) When exitingcount becomes 0 on the last reap
337 * (holdcount will not be 0 because the vmspace is held through the op)
339 * (3) When the holdcount becomes 0 in addition to the above two
341 * sysref will not scrap the object until we call sysref_put() once more
342 * after the last ref has been dropped.
344 * VMSPACE_EXIT1 flags the primary deactivation
345 * VMSPACE_EXIT2 flags the last reap
348 vmspace_terminate(struct vmspace *vm)
355 lwkt_gettoken(&vm->vm_map.token);
356 if ((vm->vm_flags & VMSPACE_EXIT1) == 0) {
357 vm->vm_flags |= VMSPACE_EXIT1;
359 pmap_remove_pages(vmspace_pmap(vm), VM_MIN_USER_ADDRESS,
360 VM_MAX_USER_ADDRESS);
361 vm_map_remove(&vm->vm_map, VM_MIN_USER_ADDRESS,
362 VM_MAX_USER_ADDRESS);
364 if ((vm->vm_flags & VMSPACE_EXIT2) == 0 && vm->vm_exitingcnt == 0) {
365 vm->vm_flags |= VMSPACE_EXIT2;
366 cpu_vmspace_free(vm);
368 KKASSERT(vm->vm_upcalls == NULL);
371 * Lock the map, to wait out all other references to it.
372 * Delete all of the mappings and pages they hold, then call
373 * the pmap module to reclaim anything left.
375 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
376 vm_map_lock(&vm->vm_map);
377 vm_map_delete(&vm->vm_map, vm->vm_map.min_offset,
378 vm->vm_map.max_offset, &count);
379 vm_map_unlock(&vm->vm_map);
380 vm_map_entry_release(count);
382 lwkt_gettoken(&vmspace_pmap(vm)->pm_token);
383 pmap_release(vmspace_pmap(vm));
384 lwkt_reltoken(&vmspace_pmap(vm)->pm_token);
387 lwkt_reltoken(&vm->vm_map.token);
388 if (vm->vm_exitingcnt == 0 && vm->vm_holdcount == 0) {
389 KKASSERT(vm->vm_flags & VMSPACE_EXIT1);
390 KKASSERT(vm->vm_flags & VMSPACE_EXIT2);
391 sysref_put(&vm->vm_sysref);
396 * vmspaces are not currently locked.
399 vmspace_lock(struct vmspace *vm __unused)
404 vmspace_unlock(struct vmspace *vm __unused)
409 * This is called during exit indicating that the vmspace is no
410 * longer in used by an exiting process, but the process has not yet
416 vmspace_exitbump(struct vmspace *vm)
420 vmspace_drop(vm); /* handles termination sequencing */
424 * Decrement the exitingcnt and issue the stage-2 termination if it becomes
425 * zero and the stage1 termination has already occured.
430 vmspace_exitfree(struct proc *p)
437 KKASSERT(vm->vm_exitingcnt > 0);
438 if (--vm->vm_exitingcnt == 0 && sysref_isinactive(&vm->vm_sysref))
439 vmspace_terminate(vm);
440 vmspace_drop(vm); /* handles termination sequencing */
444 * Swap useage is determined by taking the proportional swap used by
445 * VM objects backing the VM map. To make up for fractional losses,
446 * if the VM object has any swap use at all the associated map entries
447 * count for at least 1 swap page.
452 vmspace_swap_count(struct vmspace *vm)
454 vm_map_t map = &vm->vm_map;
461 for (cur = map->header.next; cur != &map->header; cur = cur->next) {
462 switch(cur->maptype) {
463 case VM_MAPTYPE_NORMAL:
464 case VM_MAPTYPE_VPAGETABLE:
465 if ((object = cur->object.vm_object) == NULL)
467 if (object->swblock_count) {
468 n = (cur->end - cur->start) / PAGE_SIZE;
469 count += object->swblock_count *
470 SWAP_META_PAGES * n / object->size + 1;
483 * Calculate the approximate number of anonymous pages in use by
484 * this vmspace. To make up for fractional losses, we count each
485 * VM object as having at least 1 anonymous page.
490 vmspace_anonymous_count(struct vmspace *vm)
492 vm_map_t map = &vm->vm_map;
498 for (cur = map->header.next; cur != &map->header; cur = cur->next) {
499 switch(cur->maptype) {
500 case VM_MAPTYPE_NORMAL:
501 case VM_MAPTYPE_VPAGETABLE:
502 if ((object = cur->object.vm_object) == NULL)
504 if (object->type != OBJT_DEFAULT &&
505 object->type != OBJT_SWAP) {
508 count += object->resident_page_count;
520 * Creates and returns a new empty VM map with the given physical map
521 * structure, and having the given lower and upper address bounds.
526 vm_map_create(vm_map_t result, pmap_t pmap, vm_offset_t min, vm_offset_t max)
529 result = zalloc(mapzone);
530 vm_map_init(result, min, max, pmap);
535 * Initialize an existing vm_map structure such as that in the vmspace
536 * structure. The pmap is initialized elsewhere.
541 vm_map_init(struct vm_map *map, vm_offset_t min, vm_offset_t max, pmap_t pmap)
543 map->header.next = map->header.prev = &map->header;
544 RB_INIT(&map->rb_root);
548 map->min_offset = min;
549 map->max_offset = max;
551 map->first_free = &map->header;
552 map->hint = &map->header;
555 lwkt_token_init(&map->token, "vm_map");
556 lockinit(&map->lock, "thrd_sleep", (hz + 9) / 10, 0);
557 TUNABLE_INT("vm.cache_vmspaces", &vmspace_sysref_class.nom_cache);
561 * Shadow the vm_map_entry's object. This typically needs to be done when
562 * a write fault is taken on an entry which had previously been cloned by
563 * fork(). The shared object (which might be NULL) must become private so
564 * we add a shadow layer above it.
566 * Object allocation for anonymous mappings is defered as long as possible.
567 * When creating a shadow, however, the underlying object must be instantiated
568 * so it can be shared.
570 * If the map segment is governed by a virtual page table then it is
571 * possible to address offsets beyond the mapped area. Just allocate
572 * a maximally sized object for this case.
574 * The vm_map must be exclusively locked.
575 * No other requirements.
579 vm_map_entry_shadow(vm_map_entry_t entry, int addref)
581 if (entry->maptype == VM_MAPTYPE_VPAGETABLE) {
582 vm_object_shadow(&entry->object.vm_object, &entry->offset,
583 0x7FFFFFFF, addref); /* XXX */
585 vm_object_shadow(&entry->object.vm_object, &entry->offset,
586 atop(entry->end - entry->start), addref);
588 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
592 * Allocate an object for a vm_map_entry.
594 * Object allocation for anonymous mappings is defered as long as possible.
595 * This function is called when we can defer no longer, generally when a map
596 * entry might be split or forked or takes a page fault.
598 * If the map segment is governed by a virtual page table then it is
599 * possible to address offsets beyond the mapped area. Just allocate
600 * a maximally sized object for this case.
602 * The vm_map must be exclusively locked.
603 * No other requirements.
606 vm_map_entry_allocate_object(vm_map_entry_t entry)
610 if (entry->maptype == VM_MAPTYPE_VPAGETABLE) {
611 obj = vm_object_allocate(OBJT_DEFAULT, 0x7FFFFFFF); /* XXX */
613 obj = vm_object_allocate(OBJT_DEFAULT,
614 atop(entry->end - entry->start));
616 entry->object.vm_object = obj;
621 * Set an initial negative count so the first attempt to reserve
622 * space preloads a bunch of vm_map_entry's for this cpu. Also
623 * pre-allocate 2 vm_map_entries which will be needed by zalloc() to
624 * map a new page for vm_map_entry structures. SMP systems are
625 * particularly sensitive.
627 * This routine is called in early boot so we cannot just call
628 * vm_map_entry_reserve().
630 * Called from the low level boot code only (for each cpu)
633 vm_map_entry_reserve_cpu_init(globaldata_t gd)
635 vm_map_entry_t entry;
638 gd->gd_vme_avail -= MAP_RESERVE_COUNT * 2;
639 entry = &cpu_map_entry_init[gd->gd_cpuid][0];
640 for (i = 0; i < VMEPERCPU; ++i, ++entry) {
641 entry->next = gd->gd_vme_base;
642 gd->gd_vme_base = entry;
647 * Reserves vm_map_entry structures so code later on can manipulate
648 * map_entry structures within a locked map without blocking trying
649 * to allocate a new vm_map_entry.
654 vm_map_entry_reserve(int count)
656 struct globaldata *gd = mycpu;
657 vm_map_entry_t entry;
660 * Make sure we have enough structures in gd_vme_base to handle
661 * the reservation request.
663 * The critical section protects access to the per-cpu gd.
666 while (gd->gd_vme_avail < count) {
667 entry = zalloc(mapentzone);
668 entry->next = gd->gd_vme_base;
669 gd->gd_vme_base = entry;
672 gd->gd_vme_avail -= count;
679 * Releases previously reserved vm_map_entry structures that were not
680 * used. If we have too much junk in our per-cpu cache clean some of
686 vm_map_entry_release(int count)
688 struct globaldata *gd = mycpu;
689 vm_map_entry_t entry;
692 gd->gd_vme_avail += count;
693 while (gd->gd_vme_avail > MAP_RESERVE_SLOP) {
694 entry = gd->gd_vme_base;
695 KKASSERT(entry != NULL);
696 gd->gd_vme_base = entry->next;
699 zfree(mapentzone, entry);
706 * Reserve map entry structures for use in kernel_map itself. These
707 * entries have *ALREADY* been reserved on a per-cpu basis when the map
708 * was inited. This function is used by zalloc() to avoid a recursion
709 * when zalloc() itself needs to allocate additional kernel memory.
711 * This function works like the normal reserve but does not load the
712 * vm_map_entry cache (because that would result in an infinite
713 * recursion). Note that gd_vme_avail may go negative. This is expected.
715 * Any caller of this function must be sure to renormalize after
716 * potentially eating entries to ensure that the reserve supply
722 vm_map_entry_kreserve(int count)
724 struct globaldata *gd = mycpu;
727 gd->gd_vme_avail -= count;
729 KASSERT(gd->gd_vme_base != NULL,
730 ("no reserved entries left, gd_vme_avail = %d\n",
736 * Release previously reserved map entries for kernel_map. We do not
737 * attempt to clean up like the normal release function as this would
738 * cause an unnecessary (but probably not fatal) deep procedure call.
743 vm_map_entry_krelease(int count)
745 struct globaldata *gd = mycpu;
748 gd->gd_vme_avail += count;
753 * Allocates a VM map entry for insertion. No entry fields are filled in.
755 * The entries should have previously been reserved. The reservation count
756 * is tracked in (*countp).
760 static vm_map_entry_t
761 vm_map_entry_create(vm_map_t map, int *countp)
763 struct globaldata *gd = mycpu;
764 vm_map_entry_t entry;
766 KKASSERT(*countp > 0);
769 entry = gd->gd_vme_base;
770 KASSERT(entry != NULL, ("gd_vme_base NULL! count %d", *countp));
771 gd->gd_vme_base = entry->next;
778 * Dispose of a vm_map_entry that is no longer being referenced.
783 vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry, int *countp)
785 struct globaldata *gd = mycpu;
787 KKASSERT(map->hint != entry);
788 KKASSERT(map->first_free != entry);
792 entry->next = gd->gd_vme_base;
793 gd->gd_vme_base = entry;
799 * Insert/remove entries from maps.
801 * The related map must be exclusively locked.
802 * The caller must hold map->token
803 * No other requirements.
806 vm_map_entry_link(vm_map_t map,
807 vm_map_entry_t after_where,
808 vm_map_entry_t entry)
810 ASSERT_VM_MAP_LOCKED(map);
813 entry->prev = after_where;
814 entry->next = after_where->next;
815 entry->next->prev = entry;
816 after_where->next = entry;
817 if (vm_map_rb_tree_RB_INSERT(&map->rb_root, entry))
818 panic("vm_map_entry_link: dup addr map %p ent %p", map, entry);
822 vm_map_entry_unlink(vm_map_t map,
823 vm_map_entry_t entry)
828 ASSERT_VM_MAP_LOCKED(map);
830 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
831 panic("vm_map_entry_unlink: attempt to mess with "
832 "locked entry! %p", entry);
838 vm_map_rb_tree_RB_REMOVE(&map->rb_root, entry);
843 * Finds the map entry containing (or immediately preceding) the specified
844 * address in the given map. The entry is returned in (*entry).
846 * The boolean result indicates whether the address is actually contained
849 * The related map must be locked.
850 * No other requirements.
853 vm_map_lookup_entry(vm_map_t map, vm_offset_t address, vm_map_entry_t *entry)
858 ASSERT_VM_MAP_LOCKED(map);
861 * XXX TEMPORARILY DISABLED. For some reason our attempt to revive
862 * the hint code with the red-black lookup meets with system crashes
863 * and lockups. We do not yet know why.
865 * It is possible that the problem is related to the setting
866 * of the hint during map_entry deletion, in the code specified
867 * at the GGG comment later on in this file.
869 * YYY More likely it's because this function can be called with
870 * a shared lock on the map, resulting in map->hint updates possibly
871 * racing. Fixed now but untested.
874 * Quickly check the cached hint, there's a good chance of a match.
878 if (tmp != &map->header) {
879 if (address >= tmp->start && address < tmp->end) {
887 * Locate the record from the top of the tree. 'last' tracks the
888 * closest prior record and is returned if no match is found, which
889 * in binary tree terms means tracking the most recent right-branch
890 * taken. If there is no prior record, &map->header is returned.
893 tmp = RB_ROOT(&map->rb_root);
896 if (address >= tmp->start) {
897 if (address < tmp->end) {
903 tmp = RB_RIGHT(tmp, rb_entry);
905 tmp = RB_LEFT(tmp, rb_entry);
913 * Inserts the given whole VM object into the target map at the specified
914 * address range. The object's size should match that of the address range.
916 * The map must be exclusively locked.
917 * The object must be held.
918 * The caller must have reserved sufficient vm_map_entry structures.
920 * If object is non-NULL, ref count must be bumped by caller prior to
921 * making call to account for the new entry.
924 vm_map_insert(vm_map_t map, int *countp,
925 vm_object_t object, vm_ooffset_t offset,
926 vm_offset_t start, vm_offset_t end,
927 vm_maptype_t maptype,
928 vm_prot_t prot, vm_prot_t max,
931 vm_map_entry_t new_entry;
932 vm_map_entry_t prev_entry;
933 vm_map_entry_t temp_entry;
934 vm_eflags_t protoeflags;
937 ASSERT_VM_MAP_LOCKED(map);
939 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
942 * Check that the start and end points are not bogus.
944 if ((start < map->min_offset) || (end > map->max_offset) ||
946 return (KERN_INVALID_ADDRESS);
949 * Find the entry prior to the proposed starting address; if it's part
950 * of an existing entry, this range is bogus.
952 if (vm_map_lookup_entry(map, start, &temp_entry))
953 return (KERN_NO_SPACE);
955 prev_entry = temp_entry;
958 * Assert that the next entry doesn't overlap the end point.
961 if ((prev_entry->next != &map->header) &&
962 (prev_entry->next->start < end))
963 return (KERN_NO_SPACE);
967 if (cow & MAP_COPY_ON_WRITE)
968 protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
970 if (cow & MAP_NOFAULT) {
971 protoeflags |= MAP_ENTRY_NOFAULT;
973 KASSERT(object == NULL,
974 ("vm_map_insert: paradoxical MAP_NOFAULT request"));
976 if (cow & MAP_DISABLE_SYNCER)
977 protoeflags |= MAP_ENTRY_NOSYNC;
978 if (cow & MAP_DISABLE_COREDUMP)
979 protoeflags |= MAP_ENTRY_NOCOREDUMP;
980 if (cow & MAP_IS_STACK)
981 protoeflags |= MAP_ENTRY_STACK;
982 if (cow & MAP_IS_KSTACK)
983 protoeflags |= MAP_ENTRY_KSTACK;
985 lwkt_gettoken(&map->token);
989 * When object is non-NULL, it could be shared with another
990 * process. We have to set or clear OBJ_ONEMAPPING
993 if ((object->ref_count > 1) || (object->shadow_count != 0)) {
994 vm_object_clear_flag(object, OBJ_ONEMAPPING);
997 else if ((prev_entry != &map->header) &&
998 (prev_entry->eflags == protoeflags) &&
999 (prev_entry->end == start) &&
1000 (prev_entry->wired_count == 0) &&
1001 prev_entry->maptype == maptype &&
1002 ((prev_entry->object.vm_object == NULL) ||
1003 vm_object_coalesce(prev_entry->object.vm_object,
1004 OFF_TO_IDX(prev_entry->offset),
1005 (vm_size_t)(prev_entry->end - prev_entry->start),
1006 (vm_size_t)(end - prev_entry->end)))) {
1008 * We were able to extend the object. Determine if we
1009 * can extend the previous map entry to include the
1010 * new range as well.
1012 if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
1013 (prev_entry->protection == prot) &&
1014 (prev_entry->max_protection == max)) {
1015 map->size += (end - prev_entry->end);
1016 prev_entry->end = end;
1017 vm_map_simplify_entry(map, prev_entry, countp);
1018 lwkt_reltoken(&map->token);
1019 return (KERN_SUCCESS);
1023 * If we can extend the object but cannot extend the
1024 * map entry, we have to create a new map entry. We
1025 * must bump the ref count on the extended object to
1026 * account for it. object may be NULL.
1028 object = prev_entry->object.vm_object;
1029 offset = prev_entry->offset +
1030 (prev_entry->end - prev_entry->start);
1032 vm_object_hold(object);
1033 vm_object_chain_wait(object);
1034 vm_object_reference_locked(object);
1040 * NOTE: if conditionals fail, object can be NULL here. This occurs
1041 * in things like the buffer map where we manage kva but do not manage
1046 * Create a new entry
1049 new_entry = vm_map_entry_create(map, countp);
1050 new_entry->start = start;
1051 new_entry->end = end;
1053 new_entry->maptype = maptype;
1054 new_entry->eflags = protoeflags;
1055 new_entry->object.vm_object = object;
1056 new_entry->offset = offset;
1057 new_entry->aux.master_pde = 0;
1059 new_entry->inheritance = VM_INHERIT_DEFAULT;
1060 new_entry->protection = prot;
1061 new_entry->max_protection = max;
1062 new_entry->wired_count = 0;
1065 * Insert the new entry into the list
1068 vm_map_entry_link(map, prev_entry, new_entry);
1069 map->size += new_entry->end - new_entry->start;
1072 * Update the free space hint. Entries cannot overlap.
1073 * An exact comparison is needed to avoid matching
1074 * against the map->header.
1076 if ((map->first_free == prev_entry) &&
1077 (prev_entry->end == new_entry->start)) {
1078 map->first_free = new_entry;
1083 * Temporarily removed to avoid MAP_STACK panic, due to
1084 * MAP_STACK being a huge hack. Will be added back in
1085 * when MAP_STACK (and the user stack mapping) is fixed.
1088 * It may be possible to simplify the entry
1090 vm_map_simplify_entry(map, new_entry, countp);
1094 * Try to pre-populate the page table. Mappings governed by virtual
1095 * page tables cannot be prepopulated without a lot of work, so
1098 if ((cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) &&
1099 maptype != VM_MAPTYPE_VPAGETABLE) {
1100 pmap_object_init_pt(map->pmap, start, prot,
1101 object, OFF_TO_IDX(offset), end - start,
1102 cow & MAP_PREFAULT_PARTIAL);
1105 vm_object_drop(object);
1107 lwkt_reltoken(&map->token);
1108 return (KERN_SUCCESS);
1112 * Find sufficient space for `length' bytes in the given map, starting at
1113 * `start'. Returns 0 on success, 1 on no space.
1115 * This function will returned an arbitrarily aligned pointer. If no
1116 * particular alignment is required you should pass align as 1. Note that
1117 * the map may return PAGE_SIZE aligned pointers if all the lengths used in
1118 * the map are a multiple of PAGE_SIZE, even if you pass a smaller align
1121 * 'align' should be a power of 2 but is not required to be.
1123 * The map must be exclusively locked.
1124 * No other requirements.
1127 vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length,
1128 vm_size_t align, int flags, vm_offset_t *addr)
1130 vm_map_entry_t entry, next;
1132 vm_offset_t align_mask;
1134 if (start < map->min_offset)
1135 start = map->min_offset;
1136 if (start > map->max_offset)
1140 * If the alignment is not a power of 2 we will have to use
1141 * a mod/division, set align_mask to a special value.
1143 if ((align | (align - 1)) + 1 != (align << 1))
1144 align_mask = (vm_offset_t)-1;
1146 align_mask = align - 1;
1149 * Look for the first possible address; if there's already something
1150 * at this address, we have to start after it.
1152 if (start == map->min_offset) {
1153 if ((entry = map->first_free) != &map->header)
1158 if (vm_map_lookup_entry(map, start, &tmp))
1164 * Look through the rest of the map, trying to fit a new region in the
1165 * gap between existing regions, or after the very last region.
1167 for (;; start = (entry = next)->end) {
1169 * Adjust the proposed start by the requested alignment,
1170 * be sure that we didn't wrap the address.
1172 if (align_mask == (vm_offset_t)-1)
1173 end = ((start + align - 1) / align) * align;
1175 end = (start + align_mask) & ~align_mask;
1180 * Find the end of the proposed new region. Be sure we didn't
1181 * go beyond the end of the map, or wrap around the address.
1182 * Then check to see if this is the last entry or if the
1183 * proposed end fits in the gap between this and the next
1186 end = start + length;
1187 if (end > map->max_offset || end < start)
1192 * If the next entry's start address is beyond the desired
1193 * end address we may have found a good entry.
1195 * If the next entry is a stack mapping we do not map into
1196 * the stack's reserved space.
1198 * XXX continue to allow mapping into the stack's reserved
1199 * space if doing a MAP_STACK mapping inside a MAP_STACK
1200 * mapping, for backwards compatibility. But the caller
1201 * really should use MAP_STACK | MAP_TRYFIXED if they
1204 if (next == &map->header)
1206 if (next->start >= end) {
1207 if ((next->eflags & MAP_ENTRY_STACK) == 0)
1209 if (flags & MAP_STACK)
1211 if (next->start - next->aux.avail_ssize >= end)
1218 * Grow the kernel_map if necessary. pmap_growkernel() will panic
1219 * if it fails. The kernel_map is locked and nothing can steal
1220 * our address space if pmap_growkernel() blocks.
1222 * NOTE: This may be unconditionally called for kldload areas on
1223 * x86_64 because these do not bump kernel_vm_end (which would
1224 * fill 128G worth of page tables!). Therefore we must not
1227 if (map == &kernel_map) {
1230 kstop = round_page(start + length);
1231 if (kstop > kernel_vm_end)
1232 pmap_growkernel(start, kstop);
1239 * vm_map_find finds an unallocated region in the target address map with
1240 * the given length and allocates it. The search is defined to be first-fit
1241 * from the specified address; the region found is returned in the same
1244 * If object is non-NULL, ref count must be bumped by caller
1245 * prior to making call to account for the new entry.
1247 * No requirements. This function will lock the map temporarily.
1250 vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1251 vm_offset_t *addr, vm_size_t length, vm_size_t align,
1253 vm_maptype_t maptype,
1254 vm_prot_t prot, vm_prot_t max,
1263 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1266 vm_object_hold(object);
1268 if (vm_map_findspace(map, start, length, align, 0, addr)) {
1270 vm_map_entry_release(count);
1271 return (KERN_NO_SPACE);
1275 result = vm_map_insert(map, &count, object, offset,
1276 start, start + length,
1281 vm_object_drop(object);
1283 vm_map_entry_release(count);
1289 * Simplify the given map entry by merging with either neighbor. This
1290 * routine also has the ability to merge with both neighbors.
1292 * This routine guarentees that the passed entry remains valid (though
1293 * possibly extended). When merging, this routine may delete one or
1294 * both neighbors. No action is taken on entries which have their
1295 * in-transition flag set.
1297 * The map must be exclusively locked.
1300 vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry, int *countp)
1302 vm_map_entry_t next, prev;
1303 vm_size_t prevsize, esize;
1305 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1306 ++mycpu->gd_cnt.v_intrans_coll;
1310 if (entry->maptype == VM_MAPTYPE_SUBMAP)
1314 if (prev != &map->header) {
1315 prevsize = prev->end - prev->start;
1316 if ( (prev->end == entry->start) &&
1317 (prev->maptype == entry->maptype) &&
1318 (prev->object.vm_object == entry->object.vm_object) &&
1319 (!prev->object.vm_object ||
1320 (prev->offset + prevsize == entry->offset)) &&
1321 (prev->eflags == entry->eflags) &&
1322 (prev->protection == entry->protection) &&
1323 (prev->max_protection == entry->max_protection) &&
1324 (prev->inheritance == entry->inheritance) &&
1325 (prev->wired_count == entry->wired_count)) {
1326 if (map->first_free == prev)
1327 map->first_free = entry;
1328 if (map->hint == prev)
1330 vm_map_entry_unlink(map, prev);
1331 entry->start = prev->start;
1332 entry->offset = prev->offset;
1333 if (prev->object.vm_object)
1334 vm_object_deallocate(prev->object.vm_object);
1335 vm_map_entry_dispose(map, prev, countp);
1340 if (next != &map->header) {
1341 esize = entry->end - entry->start;
1342 if ((entry->end == next->start) &&
1343 (next->maptype == entry->maptype) &&
1344 (next->object.vm_object == entry->object.vm_object) &&
1345 (!entry->object.vm_object ||
1346 (entry->offset + esize == next->offset)) &&
1347 (next->eflags == entry->eflags) &&
1348 (next->protection == entry->protection) &&
1349 (next->max_protection == entry->max_protection) &&
1350 (next->inheritance == entry->inheritance) &&
1351 (next->wired_count == entry->wired_count)) {
1352 if (map->first_free == next)
1353 map->first_free = entry;
1354 if (map->hint == next)
1356 vm_map_entry_unlink(map, next);
1357 entry->end = next->end;
1358 if (next->object.vm_object)
1359 vm_object_deallocate(next->object.vm_object);
1360 vm_map_entry_dispose(map, next, countp);
1366 * Asserts that the given entry begins at or after the specified address.
1367 * If necessary, it splits the entry into two.
1369 #define vm_map_clip_start(map, entry, startaddr, countp) \
1371 if (startaddr > entry->start) \
1372 _vm_map_clip_start(map, entry, startaddr, countp); \
1376 * This routine is called only when it is known that the entry must be split.
1378 * The map must be exclusively locked.
1381 _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start,
1384 vm_map_entry_t new_entry;
1387 * Split off the front portion -- note that we must insert the new
1388 * entry BEFORE this one, so that this entry has the specified
1392 vm_map_simplify_entry(map, entry, countp);
1395 * If there is no object backing this entry, we might as well create
1396 * one now. If we defer it, an object can get created after the map
1397 * is clipped, and individual objects will be created for the split-up
1398 * map. This is a bit of a hack, but is also about the best place to
1399 * put this improvement.
1401 if (entry->object.vm_object == NULL && !map->system_map) {
1402 vm_map_entry_allocate_object(entry);
1405 new_entry = vm_map_entry_create(map, countp);
1406 *new_entry = *entry;
1408 new_entry->end = start;
1409 entry->offset += (start - entry->start);
1410 entry->start = start;
1412 vm_map_entry_link(map, entry->prev, new_entry);
1414 switch(entry->maptype) {
1415 case VM_MAPTYPE_NORMAL:
1416 case VM_MAPTYPE_VPAGETABLE:
1417 if (new_entry->object.vm_object) {
1418 vm_object_hold(new_entry->object.vm_object);
1419 vm_object_chain_wait(new_entry->object.vm_object);
1420 vm_object_reference_locked(new_entry->object.vm_object);
1421 vm_object_drop(new_entry->object.vm_object);
1430 * Asserts that the given entry ends at or before the specified address.
1431 * If necessary, it splits the entry into two.
1433 * The map must be exclusively locked.
1435 #define vm_map_clip_end(map, entry, endaddr, countp) \
1437 if (endaddr < entry->end) \
1438 _vm_map_clip_end(map, entry, endaddr, countp); \
1442 * This routine is called only when it is known that the entry must be split.
1444 * The map must be exclusively locked.
1447 _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end,
1450 vm_map_entry_t new_entry;
1453 * If there is no object backing this entry, we might as well create
1454 * one now. If we defer it, an object can get created after the map
1455 * is clipped, and individual objects will be created for the split-up
1456 * map. This is a bit of a hack, but is also about the best place to
1457 * put this improvement.
1460 if (entry->object.vm_object == NULL && !map->system_map) {
1461 vm_map_entry_allocate_object(entry);
1465 * Create a new entry and insert it AFTER the specified entry
1468 new_entry = vm_map_entry_create(map, countp);
1469 *new_entry = *entry;
1471 new_entry->start = entry->end = end;
1472 new_entry->offset += (end - entry->start);
1474 vm_map_entry_link(map, entry, new_entry);
1476 switch(entry->maptype) {
1477 case VM_MAPTYPE_NORMAL:
1478 case VM_MAPTYPE_VPAGETABLE:
1479 if (new_entry->object.vm_object) {
1480 vm_object_hold(new_entry->object.vm_object);
1481 vm_object_chain_wait(new_entry->object.vm_object);
1482 vm_object_reference_locked(new_entry->object.vm_object);
1483 vm_object_drop(new_entry->object.vm_object);
1492 * Asserts that the starting and ending region addresses fall within the
1493 * valid range for the map.
1495 #define VM_MAP_RANGE_CHECK(map, start, end) \
1497 if (start < vm_map_min(map)) \
1498 start = vm_map_min(map); \
1499 if (end > vm_map_max(map)) \
1500 end = vm_map_max(map); \
1506 * Used to block when an in-transition collison occurs. The map
1507 * is unlocked for the sleep and relocked before the return.
1510 vm_map_transition_wait(vm_map_t map)
1512 tsleep_interlock(map, 0);
1514 tsleep(map, PINTERLOCKED, "vment", 0);
1519 * When we do blocking operations with the map lock held it is
1520 * possible that a clip might have occured on our in-transit entry,
1521 * requiring an adjustment to the entry in our loop. These macros
1522 * help the pageable and clip_range code deal with the case. The
1523 * conditional costs virtually nothing if no clipping has occured.
1526 #define CLIP_CHECK_BACK(entry, save_start) \
1528 while (entry->start != save_start) { \
1529 entry = entry->prev; \
1530 KASSERT(entry != &map->header, ("bad entry clip")); \
1534 #define CLIP_CHECK_FWD(entry, save_end) \
1536 while (entry->end != save_end) { \
1537 entry = entry->next; \
1538 KASSERT(entry != &map->header, ("bad entry clip")); \
1544 * Clip the specified range and return the base entry. The
1545 * range may cover several entries starting at the returned base
1546 * and the first and last entry in the covering sequence will be
1547 * properly clipped to the requested start and end address.
1549 * If no holes are allowed you should pass the MAP_CLIP_NO_HOLES
1552 * The MAP_ENTRY_IN_TRANSITION flag will be set for the entries
1553 * covered by the requested range.
1555 * The map must be exclusively locked on entry and will remain locked
1556 * on return. If no range exists or the range contains holes and you
1557 * specified that no holes were allowed, NULL will be returned. This
1558 * routine may temporarily unlock the map in order avoid a deadlock when
1563 vm_map_clip_range(vm_map_t map, vm_offset_t start, vm_offset_t end,
1564 int *countp, int flags)
1566 vm_map_entry_t start_entry;
1567 vm_map_entry_t entry;
1570 * Locate the entry and effect initial clipping. The in-transition
1571 * case does not occur very often so do not try to optimize it.
1574 if (vm_map_lookup_entry(map, start, &start_entry) == FALSE)
1576 entry = start_entry;
1577 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1578 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1579 ++mycpu->gd_cnt.v_intrans_coll;
1580 ++mycpu->gd_cnt.v_intrans_wait;
1581 vm_map_transition_wait(map);
1583 * entry and/or start_entry may have been clipped while
1584 * we slept, or may have gone away entirely. We have
1585 * to restart from the lookup.
1591 * Since we hold an exclusive map lock we do not have to restart
1592 * after clipping, even though clipping may block in zalloc.
1594 vm_map_clip_start(map, entry, start, countp);
1595 vm_map_clip_end(map, entry, end, countp);
1596 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
1599 * Scan entries covered by the range. When working on the next
1600 * entry a restart need only re-loop on the current entry which
1601 * we have already locked, since 'next' may have changed. Also,
1602 * even though entry is safe, it may have been clipped so we
1603 * have to iterate forwards through the clip after sleeping.
1605 while (entry->next != &map->header && entry->next->start < end) {
1606 vm_map_entry_t next = entry->next;
1608 if (flags & MAP_CLIP_NO_HOLES) {
1609 if (next->start > entry->end) {
1610 vm_map_unclip_range(map, start_entry,
1611 start, entry->end, countp, flags);
1616 if (next->eflags & MAP_ENTRY_IN_TRANSITION) {
1617 vm_offset_t save_end = entry->end;
1618 next->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1619 ++mycpu->gd_cnt.v_intrans_coll;
1620 ++mycpu->gd_cnt.v_intrans_wait;
1621 vm_map_transition_wait(map);
1624 * clips might have occured while we blocked.
1626 CLIP_CHECK_FWD(entry, save_end);
1627 CLIP_CHECK_BACK(start_entry, start);
1631 * No restart necessary even though clip_end may block, we
1632 * are holding the map lock.
1634 vm_map_clip_end(map, next, end, countp);
1635 next->eflags |= MAP_ENTRY_IN_TRANSITION;
1638 if (flags & MAP_CLIP_NO_HOLES) {
1639 if (entry->end != end) {
1640 vm_map_unclip_range(map, start_entry,
1641 start, entry->end, countp, flags);
1645 return(start_entry);
1649 * Undo the effect of vm_map_clip_range(). You should pass the same
1650 * flags and the same range that you passed to vm_map_clip_range().
1651 * This code will clear the in-transition flag on the entries and
1652 * wake up anyone waiting. This code will also simplify the sequence
1653 * and attempt to merge it with entries before and after the sequence.
1655 * The map must be locked on entry and will remain locked on return.
1657 * Note that you should also pass the start_entry returned by
1658 * vm_map_clip_range(). However, if you block between the two calls
1659 * with the map unlocked please be aware that the start_entry may
1660 * have been clipped and you may need to scan it backwards to find
1661 * the entry corresponding with the original start address. You are
1662 * responsible for this, vm_map_unclip_range() expects the correct
1663 * start_entry to be passed to it and will KASSERT otherwise.
1667 vm_map_unclip_range(vm_map_t map, vm_map_entry_t start_entry,
1668 vm_offset_t start, vm_offset_t end,
1669 int *countp, int flags)
1671 vm_map_entry_t entry;
1673 entry = start_entry;
1675 KASSERT(entry->start == start, ("unclip_range: illegal base entry"));
1676 while (entry != &map->header && entry->start < end) {
1677 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
1678 ("in-transition flag not set during unclip on: %p",
1680 KASSERT(entry->end <= end,
1681 ("unclip_range: tail wasn't clipped"));
1682 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
1683 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
1684 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
1687 entry = entry->next;
1691 * Simplification does not block so there is no restart case.
1693 entry = start_entry;
1694 while (entry != &map->header && entry->start < end) {
1695 vm_map_simplify_entry(map, entry, countp);
1696 entry = entry->next;
1701 * Mark the given range as handled by a subordinate map.
1703 * This range must have been created with vm_map_find(), and no other
1704 * operations may have been performed on this range prior to calling
1707 * Submappings cannot be removed.
1712 vm_map_submap(vm_map_t map, vm_offset_t start, vm_offset_t end, vm_map_t submap)
1714 vm_map_entry_t entry;
1715 int result = KERN_INVALID_ARGUMENT;
1718 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1721 VM_MAP_RANGE_CHECK(map, start, end);
1723 if (vm_map_lookup_entry(map, start, &entry)) {
1724 vm_map_clip_start(map, entry, start, &count);
1726 entry = entry->next;
1729 vm_map_clip_end(map, entry, end, &count);
1731 if ((entry->start == start) && (entry->end == end) &&
1732 ((entry->eflags & MAP_ENTRY_COW) == 0) &&
1733 (entry->object.vm_object == NULL)) {
1734 entry->object.sub_map = submap;
1735 entry->maptype = VM_MAPTYPE_SUBMAP;
1736 result = KERN_SUCCESS;
1739 vm_map_entry_release(count);
1745 * Sets the protection of the specified address region in the target map.
1746 * If "set_max" is specified, the maximum protection is to be set;
1747 * otherwise, only the current protection is affected.
1749 * The protection is not applicable to submaps, but is applicable to normal
1750 * maps and maps governed by virtual page tables. For example, when operating
1751 * on a virtual page table our protection basically controls how COW occurs
1752 * on the backing object, whereas the virtual page table abstraction itself
1753 * is an abstraction for userland.
1758 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
1759 vm_prot_t new_prot, boolean_t set_max)
1761 vm_map_entry_t current;
1762 vm_map_entry_t entry;
1765 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1768 VM_MAP_RANGE_CHECK(map, start, end);
1770 if (vm_map_lookup_entry(map, start, &entry)) {
1771 vm_map_clip_start(map, entry, start, &count);
1773 entry = entry->next;
1777 * Make a first pass to check for protection violations.
1780 while ((current != &map->header) && (current->start < end)) {
1781 if (current->maptype == VM_MAPTYPE_SUBMAP) {
1783 vm_map_entry_release(count);
1784 return (KERN_INVALID_ARGUMENT);
1786 if ((new_prot & current->max_protection) != new_prot) {
1788 vm_map_entry_release(count);
1789 return (KERN_PROTECTION_FAILURE);
1791 current = current->next;
1795 * Go back and fix up protections. [Note that clipping is not
1796 * necessary the second time.]
1800 while ((current != &map->header) && (current->start < end)) {
1803 vm_map_clip_end(map, current, end, &count);
1805 old_prot = current->protection;
1807 current->protection =
1808 (current->max_protection = new_prot) &
1811 current->protection = new_prot;
1815 * Update physical map if necessary. Worry about copy-on-write
1816 * here -- CHECK THIS XXX
1819 if (current->protection != old_prot) {
1820 #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1823 pmap_protect(map->pmap, current->start,
1825 current->protection & MASK(current));
1829 vm_map_simplify_entry(map, current, &count);
1831 current = current->next;
1835 vm_map_entry_release(count);
1836 return (KERN_SUCCESS);
1840 * This routine traverses a processes map handling the madvise
1841 * system call. Advisories are classified as either those effecting
1842 * the vm_map_entry structure, or those effecting the underlying
1845 * The <value> argument is used for extended madvise calls.
1850 vm_map_madvise(vm_map_t map, vm_offset_t start, vm_offset_t end,
1851 int behav, off_t value)
1853 vm_map_entry_t current, entry;
1859 * Some madvise calls directly modify the vm_map_entry, in which case
1860 * we need to use an exclusive lock on the map and we need to perform
1861 * various clipping operations. Otherwise we only need a read-lock
1865 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1869 case MADV_SEQUENTIAL:
1883 vm_map_lock_read(map);
1886 vm_map_entry_release(count);
1891 * Locate starting entry and clip if necessary.
1894 VM_MAP_RANGE_CHECK(map, start, end);
1896 if (vm_map_lookup_entry(map, start, &entry)) {
1898 vm_map_clip_start(map, entry, start, &count);
1900 entry = entry->next;
1905 * madvise behaviors that are implemented in the vm_map_entry.
1907 * We clip the vm_map_entry so that behavioral changes are
1908 * limited to the specified address range.
1910 for (current = entry;
1911 (current != &map->header) && (current->start < end);
1912 current = current->next
1914 if (current->maptype == VM_MAPTYPE_SUBMAP)
1917 vm_map_clip_end(map, current, end, &count);
1921 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
1923 case MADV_SEQUENTIAL:
1924 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
1927 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
1930 current->eflags |= MAP_ENTRY_NOSYNC;
1933 current->eflags &= ~MAP_ENTRY_NOSYNC;
1936 current->eflags |= MAP_ENTRY_NOCOREDUMP;
1939 current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
1943 * Invalidate the related pmap entries, used
1944 * to flush portions of the real kernel's
1945 * pmap when the caller has removed or
1946 * modified existing mappings in a virtual
1949 pmap_remove(map->pmap,
1950 current->start, current->end);
1954 * Set the page directory page for a map
1955 * governed by a virtual page table. Mark
1956 * the entry as being governed by a virtual
1957 * page table if it is not.
1959 * XXX the page directory page is stored
1960 * in the avail_ssize field if the map_entry.
1962 * XXX the map simplification code does not
1963 * compare this field so weird things may
1964 * happen if you do not apply this function
1965 * to the entire mapping governed by the
1966 * virtual page table.
1968 if (current->maptype != VM_MAPTYPE_VPAGETABLE) {
1972 current->aux.master_pde = value;
1973 pmap_remove(map->pmap,
1974 current->start, current->end);
1980 vm_map_simplify_entry(map, current, &count);
1988 * madvise behaviors that are implemented in the underlying
1991 * Since we don't clip the vm_map_entry, we have to clip
1992 * the vm_object pindex and count.
1994 * NOTE! We currently do not support these functions on
1995 * virtual page tables.
1997 for (current = entry;
1998 (current != &map->header) && (current->start < end);
1999 current = current->next
2001 vm_offset_t useStart;
2003 if (current->maptype != VM_MAPTYPE_NORMAL)
2006 pindex = OFF_TO_IDX(current->offset);
2007 count = atop(current->end - current->start);
2008 useStart = current->start;
2010 if (current->start < start) {
2011 pindex += atop(start - current->start);
2012 count -= atop(start - current->start);
2015 if (current->end > end)
2016 count -= atop(current->end - end);
2021 vm_object_madvise(current->object.vm_object,
2022 pindex, count, behav);
2025 * Try to populate the page table. Mappings governed
2026 * by virtual page tables cannot be pre-populated
2027 * without a lot of work so don't try.
2029 if (behav == MADV_WILLNEED &&
2030 current->maptype != VM_MAPTYPE_VPAGETABLE) {
2031 pmap_object_init_pt(
2034 current->protection,
2035 current->object.vm_object,
2037 (count << PAGE_SHIFT),
2038 MAP_PREFAULT_MADVISE
2042 vm_map_unlock_read(map);
2044 vm_map_entry_release(count);
2050 * Sets the inheritance of the specified address range in the target map.
2051 * Inheritance affects how the map will be shared with child maps at the
2052 * time of vm_map_fork.
2055 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
2056 vm_inherit_t new_inheritance)
2058 vm_map_entry_t entry;
2059 vm_map_entry_t temp_entry;
2062 switch (new_inheritance) {
2063 case VM_INHERIT_NONE:
2064 case VM_INHERIT_COPY:
2065 case VM_INHERIT_SHARE:
2068 return (KERN_INVALID_ARGUMENT);
2071 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
2074 VM_MAP_RANGE_CHECK(map, start, end);
2076 if (vm_map_lookup_entry(map, start, &temp_entry)) {
2078 vm_map_clip_start(map, entry, start, &count);
2080 entry = temp_entry->next;
2082 while ((entry != &map->header) && (entry->start < end)) {
2083 vm_map_clip_end(map, entry, end, &count);
2085 entry->inheritance = new_inheritance;
2087 vm_map_simplify_entry(map, entry, &count);
2089 entry = entry->next;
2092 vm_map_entry_release(count);
2093 return (KERN_SUCCESS);
2097 * Implement the semantics of mlock
2100 vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t real_end,
2101 boolean_t new_pageable)
2103 vm_map_entry_t entry;
2104 vm_map_entry_t start_entry;
2106 int rv = KERN_SUCCESS;
2109 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
2111 VM_MAP_RANGE_CHECK(map, start, real_end);
2114 start_entry = vm_map_clip_range(map, start, end, &count,
2116 if (start_entry == NULL) {
2118 vm_map_entry_release(count);
2119 return (KERN_INVALID_ADDRESS);
2122 if (new_pageable == 0) {
2123 entry = start_entry;
2124 while ((entry != &map->header) && (entry->start < end)) {
2125 vm_offset_t save_start;
2126 vm_offset_t save_end;
2129 * Already user wired or hard wired (trivial cases)
2131 if (entry->eflags & MAP_ENTRY_USER_WIRED) {
2132 entry = entry->next;
2135 if (entry->wired_count != 0) {
2136 entry->wired_count++;
2137 entry->eflags |= MAP_ENTRY_USER_WIRED;
2138 entry = entry->next;
2143 * A new wiring requires instantiation of appropriate
2144 * management structures and the faulting in of the
2147 if (entry->maptype != VM_MAPTYPE_SUBMAP) {
2148 int copyflag = entry->eflags &
2149 MAP_ENTRY_NEEDS_COPY;
2150 if (copyflag && ((entry->protection &
2151 VM_PROT_WRITE) != 0)) {
2152 vm_map_entry_shadow(entry, 0);
2153 } else if (entry->object.vm_object == NULL &&
2155 vm_map_entry_allocate_object(entry);
2158 entry->wired_count++;
2159 entry->eflags |= MAP_ENTRY_USER_WIRED;
2162 * Now fault in the area. Note that vm_fault_wire()
2163 * may release the map lock temporarily, it will be
2164 * relocked on return. The in-transition
2165 * flag protects the entries.
2167 save_start = entry->start;
2168 save_end = entry->end;
2169 rv = vm_fault_wire(map, entry, TRUE);
2171 CLIP_CHECK_BACK(entry, save_start);
2173 KASSERT(entry->wired_count == 1, ("bad wired_count on entry"));
2174 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2175 entry->wired_count = 0;
2176 if (entry->end == save_end)
2178 entry = entry->next;
2179 KASSERT(entry != &map->header, ("bad entry clip during backout"));
2181 end = save_start; /* unwire the rest */
2185 * note that even though the entry might have been
2186 * clipped, the USER_WIRED flag we set prevents
2187 * duplication so we do not have to do a
2190 entry = entry->next;
2194 * If we failed fall through to the unwiring section to
2195 * unwire what we had wired so far. 'end' has already
2202 * start_entry might have been clipped if we unlocked the
2203 * map and blocked. No matter how clipped it has gotten
2204 * there should be a fragment that is on our start boundary.
2206 CLIP_CHECK_BACK(start_entry, start);
2210 * Deal with the unwiring case.
2214 * This is the unwiring case. We must first ensure that the
2215 * range to be unwired is really wired down. We know there
2218 entry = start_entry;
2219 while ((entry != &map->header) && (entry->start < end)) {
2220 if ((entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
2221 rv = KERN_INVALID_ARGUMENT;
2224 KASSERT(entry->wired_count != 0, ("wired count was 0 with USER_WIRED set! %p", entry));
2225 entry = entry->next;
2229 * Now decrement the wiring count for each region. If a region
2230 * becomes completely unwired, unwire its physical pages and
2234 * The map entries are processed in a loop, checking to
2235 * make sure the entry is wired and asserting it has a wired
2236 * count. However, another loop was inserted more-or-less in
2237 * the middle of the unwiring path. This loop picks up the
2238 * "entry" loop variable from the first loop without first
2239 * setting it to start_entry. Naturally, the secound loop
2240 * is never entered and the pages backing the entries are
2241 * never unwired. This can lead to a leak of wired pages.
2243 entry = start_entry;
2244 while ((entry != &map->header) && (entry->start < end)) {
2245 KASSERT(entry->eflags & MAP_ENTRY_USER_WIRED,
2246 ("expected USER_WIRED on entry %p", entry));
2247 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2248 entry->wired_count--;
2249 if (entry->wired_count == 0)
2250 vm_fault_unwire(map, entry);
2251 entry = entry->next;
2255 vm_map_unclip_range(map, start_entry, start, real_end, &count,
2259 vm_map_entry_release(count);
2264 * Sets the pageability of the specified address range in the target map.
2265 * Regions specified as not pageable require locked-down physical
2266 * memory and physical page maps.
2268 * The map must not be locked, but a reference must remain to the map
2269 * throughout the call.
2271 * This function may be called via the zalloc path and must properly
2272 * reserve map entries for kernel_map.
2277 vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t real_end, int kmflags)
2279 vm_map_entry_t entry;
2280 vm_map_entry_t start_entry;
2282 int rv = KERN_SUCCESS;
2285 if (kmflags & KM_KRESERVE)
2286 count = vm_map_entry_kreserve(MAP_RESERVE_COUNT);
2288 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
2290 VM_MAP_RANGE_CHECK(map, start, real_end);
2293 start_entry = vm_map_clip_range(map, start, end, &count,
2295 if (start_entry == NULL) {
2297 rv = KERN_INVALID_ADDRESS;
2300 if ((kmflags & KM_PAGEABLE) == 0) {
2304 * 1. Holding the write lock, we create any shadow or zero-fill
2305 * objects that need to be created. Then we clip each map
2306 * entry to the region to be wired and increment its wiring
2307 * count. We create objects before clipping the map entries
2308 * to avoid object proliferation.
2310 * 2. We downgrade to a read lock, and call vm_fault_wire to
2311 * fault in the pages for any newly wired area (wired_count is
2314 * Downgrading to a read lock for vm_fault_wire avoids a
2315 * possible deadlock with another process that may have faulted
2316 * on one of the pages to be wired (it would mark the page busy,
2317 * blocking us, then in turn block on the map lock that we
2318 * hold). Because of problems in the recursive lock package,
2319 * we cannot upgrade to a write lock in vm_map_lookup. Thus,
2320 * any actions that require the write lock must be done
2321 * beforehand. Because we keep the read lock on the map, the
2322 * copy-on-write status of the entries we modify here cannot
2325 entry = start_entry;
2326 while ((entry != &map->header) && (entry->start < end)) {
2328 * Trivial case if the entry is already wired
2330 if (entry->wired_count) {
2331 entry->wired_count++;
2332 entry = entry->next;
2337 * The entry is being newly wired, we have to setup
2338 * appropriate management structures. A shadow
2339 * object is required for a copy-on-write region,
2340 * or a normal object for a zero-fill region. We
2341 * do not have to do this for entries that point to sub
2342 * maps because we won't hold the lock on the sub map.
2344 if (entry->maptype != VM_MAPTYPE_SUBMAP) {
2345 int copyflag = entry->eflags &
2346 MAP_ENTRY_NEEDS_COPY;
2347 if (copyflag && ((entry->protection &
2348 VM_PROT_WRITE) != 0)) {
2349 vm_map_entry_shadow(entry, 0);
2350 } else if (entry->object.vm_object == NULL &&
2352 vm_map_entry_allocate_object(entry);
2356 entry->wired_count++;
2357 entry = entry->next;
2365 * HACK HACK HACK HACK
2367 * vm_fault_wire() temporarily unlocks the map to avoid
2368 * deadlocks. The in-transition flag from vm_map_clip_range
2369 * call should protect us from changes while the map is
2372 * NOTE: Previously this comment stated that clipping might
2373 * still occur while the entry is unlocked, but from
2374 * what I can tell it actually cannot.
2376 * It is unclear whether the CLIP_CHECK_*() calls
2377 * are still needed but we keep them in anyway.
2379 * HACK HACK HACK HACK
2382 entry = start_entry;
2383 while (entry != &map->header && entry->start < end) {
2385 * If vm_fault_wire fails for any page we need to undo
2386 * what has been done. We decrement the wiring count
2387 * for those pages which have not yet been wired (now)
2388 * and unwire those that have (later).
2390 vm_offset_t save_start = entry->start;
2391 vm_offset_t save_end = entry->end;
2393 if (entry->wired_count == 1)
2394 rv = vm_fault_wire(map, entry, FALSE);
2396 CLIP_CHECK_BACK(entry, save_start);
2398 KASSERT(entry->wired_count == 1, ("wired_count changed unexpectedly"));
2399 entry->wired_count = 0;
2400 if (entry->end == save_end)
2402 entry = entry->next;
2403 KASSERT(entry != &map->header, ("bad entry clip during backout"));
2408 CLIP_CHECK_FWD(entry, save_end);
2409 entry = entry->next;
2413 * If a failure occured undo everything by falling through
2414 * to the unwiring code. 'end' has already been adjusted
2418 kmflags |= KM_PAGEABLE;
2421 * start_entry is still IN_TRANSITION but may have been
2422 * clipped since vm_fault_wire() unlocks and relocks the
2423 * map. No matter how clipped it has gotten there should
2424 * be a fragment that is on our start boundary.
2426 CLIP_CHECK_BACK(start_entry, start);
2429 if (kmflags & KM_PAGEABLE) {
2431 * This is the unwiring case. We must first ensure that the
2432 * range to be unwired is really wired down. We know there
2435 entry = start_entry;
2436 while ((entry != &map->header) && (entry->start < end)) {
2437 if (entry->wired_count == 0) {
2438 rv = KERN_INVALID_ARGUMENT;
2441 entry = entry->next;
2445 * Now decrement the wiring count for each region. If a region
2446 * becomes completely unwired, unwire its physical pages and
2449 entry = start_entry;
2450 while ((entry != &map->header) && (entry->start < end)) {
2451 entry->wired_count--;
2452 if (entry->wired_count == 0)
2453 vm_fault_unwire(map, entry);
2454 entry = entry->next;
2458 vm_map_unclip_range(map, start_entry, start, real_end,
2459 &count, MAP_CLIP_NO_HOLES);
2463 if (kmflags & KM_KRESERVE)
2464 vm_map_entry_krelease(count);
2466 vm_map_entry_release(count);
2471 * Mark a newly allocated address range as wired but do not fault in
2472 * the pages. The caller is expected to load the pages into the object.
2474 * The map must be locked on entry and will remain locked on return.
2475 * No other requirements.
2478 vm_map_set_wired_quick(vm_map_t map, vm_offset_t addr, vm_size_t size,
2481 vm_map_entry_t scan;
2482 vm_map_entry_t entry;
2484 entry = vm_map_clip_range(map, addr, addr + size,
2485 countp, MAP_CLIP_NO_HOLES);
2487 scan != &map->header && scan->start < addr + size;
2488 scan = scan->next) {
2489 KKASSERT(entry->wired_count == 0);
2490 entry->wired_count = 1;
2492 vm_map_unclip_range(map, entry, addr, addr + size,
2493 countp, MAP_CLIP_NO_HOLES);
2497 * Push any dirty cached pages in the address range to their pager.
2498 * If syncio is TRUE, dirty pages are written synchronously.
2499 * If invalidate is TRUE, any cached pages are freed as well.
2501 * This routine is called by sys_msync()
2503 * Returns an error if any part of the specified range is not mapped.
2508 vm_map_clean(vm_map_t map, vm_offset_t start, vm_offset_t end,
2509 boolean_t syncio, boolean_t invalidate)
2511 vm_map_entry_t current;
2512 vm_map_entry_t entry;
2516 vm_ooffset_t offset;
2518 vm_map_lock_read(map);
2519 VM_MAP_RANGE_CHECK(map, start, end);
2520 if (!vm_map_lookup_entry(map, start, &entry)) {
2521 vm_map_unlock_read(map);
2522 return (KERN_INVALID_ADDRESS);
2524 lwkt_gettoken(&map->token);
2527 * Make a first pass to check for holes.
2529 for (current = entry; current->start < end; current = current->next) {
2530 if (current->maptype == VM_MAPTYPE_SUBMAP) {
2531 lwkt_reltoken(&map->token);
2532 vm_map_unlock_read(map);
2533 return (KERN_INVALID_ARGUMENT);
2535 if (end > current->end &&
2536 (current->next == &map->header ||
2537 current->end != current->next->start)) {
2538 lwkt_reltoken(&map->token);
2539 vm_map_unlock_read(map);
2540 return (KERN_INVALID_ADDRESS);
2545 pmap_remove(vm_map_pmap(map), start, end);
2548 * Make a second pass, cleaning/uncaching pages from the indicated
2551 for (current = entry; current->start < end; current = current->next) {
2552 offset = current->offset + (start - current->start);
2553 size = (end <= current->end ? end : current->end) - start;
2554 if (current->maptype == VM_MAPTYPE_SUBMAP) {
2556 vm_map_entry_t tentry;
2559 smap = current->object.sub_map;
2560 vm_map_lock_read(smap);
2561 vm_map_lookup_entry(smap, offset, &tentry);
2562 tsize = tentry->end - offset;
2565 object = tentry->object.vm_object;
2566 offset = tentry->offset + (offset - tentry->start);
2567 vm_map_unlock_read(smap);
2569 object = current->object.vm_object;
2573 vm_object_hold(object);
2576 * Note that there is absolutely no sense in writing out
2577 * anonymous objects, so we track down the vnode object
2579 * We invalidate (remove) all pages from the address space
2580 * anyway, for semantic correctness.
2582 * note: certain anonymous maps, such as MAP_NOSYNC maps,
2583 * may start out with a NULL object.
2585 while (object && (tobj = object->backing_object) != NULL) {
2586 vm_object_hold(tobj);
2587 if (tobj == object->backing_object) {
2588 vm_object_lock_swap();
2589 offset += object->backing_object_offset;
2590 vm_object_drop(object);
2592 if (object->size < OFF_TO_IDX(offset + size))
2593 size = IDX_TO_OFF(object->size) -
2597 vm_object_drop(tobj);
2599 if (object && (object->type == OBJT_VNODE) &&
2600 (current->protection & VM_PROT_WRITE) &&
2601 (object->flags & OBJ_NOMSYNC) == 0) {
2603 * Flush pages if writing is allowed, invalidate them
2604 * if invalidation requested. Pages undergoing I/O
2605 * will be ignored by vm_object_page_remove().
2607 * We cannot lock the vnode and then wait for paging
2608 * to complete without deadlocking against vm_fault.
2609 * Instead we simply call vm_object_page_remove() and
2610 * allow it to block internally on a page-by-page
2611 * basis when it encounters pages undergoing async
2616 /* no chain wait needed for vnode objects */
2617 vm_object_reference_locked(object);
2618 vn_lock(object->handle, LK_EXCLUSIVE | LK_RETRY);
2619 flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
2620 flags |= invalidate ? OBJPC_INVAL : 0;
2623 * When operating on a virtual page table just
2624 * flush the whole object. XXX we probably ought
2627 switch(current->maptype) {
2628 case VM_MAPTYPE_NORMAL:
2629 vm_object_page_clean(object,
2631 OFF_TO_IDX(offset + size + PAGE_MASK),
2634 case VM_MAPTYPE_VPAGETABLE:
2635 vm_object_page_clean(object, 0, 0, flags);
2638 vn_unlock(((struct vnode *)object->handle));
2639 vm_object_deallocate_locked(object);
2641 if (object && invalidate &&
2642 ((object->type == OBJT_VNODE) ||
2643 (object->type == OBJT_DEVICE))) {
2645 (object->type == OBJT_DEVICE) ? FALSE : TRUE;
2646 /* no chain wait needed for vnode/device objects */
2647 vm_object_reference_locked(object);
2648 switch(current->maptype) {
2649 case VM_MAPTYPE_NORMAL:
2650 vm_object_page_remove(object,
2652 OFF_TO_IDX(offset + size + PAGE_MASK),
2655 case VM_MAPTYPE_VPAGETABLE:
2656 vm_object_page_remove(object, 0, 0, clean_only);
2659 vm_object_deallocate_locked(object);
2663 vm_object_drop(object);
2666 lwkt_reltoken(&map->token);
2667 vm_map_unlock_read(map);
2669 return (KERN_SUCCESS);
2673 * Make the region specified by this entry pageable.
2675 * The vm_map must be exclusively locked.
2678 vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
2680 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2681 entry->wired_count = 0;
2682 vm_fault_unwire(map, entry);
2686 * Deallocate the given entry from the target map.
2688 * The vm_map must be exclusively locked.
2691 vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry, int *countp)
2693 vm_map_entry_unlink(map, entry);
2694 map->size -= entry->end - entry->start;
2696 switch(entry->maptype) {
2697 case VM_MAPTYPE_NORMAL:
2698 case VM_MAPTYPE_VPAGETABLE:
2699 vm_object_deallocate(entry->object.vm_object);
2705 vm_map_entry_dispose(map, entry, countp);
2709 * Deallocates the given address range from the target map.
2711 * The vm_map must be exclusively locked.
2714 vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end, int *countp)
2717 vm_map_entry_t entry;
2718 vm_map_entry_t first_entry;
2720 ASSERT_VM_MAP_LOCKED(map);
2721 lwkt_gettoken(&map->token);
2724 * Find the start of the region, and clip it. Set entry to point
2725 * at the first record containing the requested address or, if no
2726 * such record exists, the next record with a greater address. The
2727 * loop will run from this point until a record beyond the termination
2728 * address is encountered.
2730 * map->hint must be adjusted to not point to anything we delete,
2731 * so set it to the entry prior to the one being deleted.
2733 * GGG see other GGG comment.
2735 if (vm_map_lookup_entry(map, start, &first_entry)) {
2736 entry = first_entry;
2737 vm_map_clip_start(map, entry, start, countp);
2738 map->hint = entry->prev; /* possible problem XXX */
2740 map->hint = first_entry; /* possible problem XXX */
2741 entry = first_entry->next;
2745 * If a hole opens up prior to the current first_free then
2746 * adjust first_free. As with map->hint, map->first_free
2747 * cannot be left set to anything we might delete.
2749 if (entry == &map->header) {
2750 map->first_free = &map->header;
2751 } else if (map->first_free->start >= start) {
2752 map->first_free = entry->prev;
2756 * Step through all entries in this region
2758 while ((entry != &map->header) && (entry->start < end)) {
2759 vm_map_entry_t next;
2761 vm_pindex_t offidxstart, offidxend, count;
2764 * If we hit an in-transition entry we have to sleep and
2765 * retry. It's easier (and not really slower) to just retry
2766 * since this case occurs so rarely and the hint is already
2767 * pointing at the right place. We have to reset the
2768 * start offset so as not to accidently delete an entry
2769 * another process just created in vacated space.
2771 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
2772 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2773 start = entry->start;
2774 ++mycpu->gd_cnt.v_intrans_coll;
2775 ++mycpu->gd_cnt.v_intrans_wait;
2776 vm_map_transition_wait(map);
2779 vm_map_clip_end(map, entry, end, countp);
2785 offidxstart = OFF_TO_IDX(entry->offset);
2786 count = OFF_TO_IDX(e - s);
2787 object = entry->object.vm_object;
2790 * Unwire before removing addresses from the pmap; otherwise,
2791 * unwiring will put the entries back in the pmap.
2793 if (entry->wired_count != 0)
2794 vm_map_entry_unwire(map, entry);
2796 offidxend = offidxstart + count;
2798 if (object == &kernel_object) {
2799 vm_object_hold(object);
2800 vm_object_page_remove(object, offidxstart,
2802 vm_object_drop(object);
2803 } else if (object && object->type != OBJT_DEFAULT &&
2804 object->type != OBJT_SWAP) {
2806 * vnode object routines cannot be chain-locked
2808 vm_object_hold(object);
2809 pmap_remove(map->pmap, s, e);
2810 vm_object_drop(object);
2811 } else if (object) {
2812 vm_object_hold(object);
2813 vm_object_chain_acquire(object);
2814 pmap_remove(map->pmap, s, e);
2816 if (object != NULL &&
2817 object->ref_count != 1 &&
2818 (object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) ==
2820 (object->type == OBJT_DEFAULT ||
2821 object->type == OBJT_SWAP)) {
2822 vm_object_collapse(object, NULL);
2823 vm_object_page_remove(object, offidxstart,
2825 if (object->type == OBJT_SWAP) {
2826 swap_pager_freespace(object,
2830 if (offidxend >= object->size &&
2831 offidxstart < object->size) {
2832 object->size = offidxstart;
2835 vm_object_chain_release(object);
2836 vm_object_drop(object);
2840 * Delete the entry (which may delete the object) only after
2841 * removing all pmap entries pointing to its pages.
2842 * (Otherwise, its page frames may be reallocated, and any
2843 * modify bits will be set in the wrong object!)
2845 vm_map_entry_delete(map, entry, countp);
2848 lwkt_reltoken(&map->token);
2849 return (KERN_SUCCESS);
2853 * Remove the given address range from the target map.
2854 * This is the exported form of vm_map_delete.
2859 vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
2864 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
2866 VM_MAP_RANGE_CHECK(map, start, end);
2867 result = vm_map_delete(map, start, end, &count);
2869 vm_map_entry_release(count);
2875 * Assert that the target map allows the specified privilege on the
2876 * entire address region given. The entire region must be allocated.
2878 * The caller must specify whether the vm_map is already locked or not.
2881 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
2882 vm_prot_t protection, boolean_t have_lock)
2884 vm_map_entry_t entry;
2885 vm_map_entry_t tmp_entry;
2888 if (have_lock == FALSE)
2889 vm_map_lock_read(map);
2891 if (!vm_map_lookup_entry(map, start, &tmp_entry)) {
2892 if (have_lock == FALSE)
2893 vm_map_unlock_read(map);
2899 while (start < end) {
2900 if (entry == &map->header) {
2908 if (start < entry->start) {
2913 * Check protection associated with entry.
2916 if ((entry->protection & protection) != protection) {
2920 /* go to next entry */
2923 entry = entry->next;
2925 if (have_lock == FALSE)
2926 vm_map_unlock_read(map);
2931 * If appropriate this function shadows the original object with a new object
2932 * and moves the VM pages from the original object to the new object.
2933 * The original object will also be collapsed, if possible.
2935 * We can only do this for normal memory objects with a single mapping, and
2936 * it only makes sense to do it if there are 2 or more refs on the original
2937 * object. i.e. typically a memory object that has been extended into
2938 * multiple vm_map_entry's with non-overlapping ranges.
2940 * This makes it easier to remove unused pages and keeps object inheritance
2941 * from being a negative impact on memory usage.
2943 * On return the (possibly new) entry->object.vm_object will have an
2944 * additional ref on it for the caller to dispose of (usually by cloning
2945 * the vm_map_entry). The additional ref had to be done in this routine
2946 * to avoid racing a collapse. The object's ONEMAPPING flag will also be
2949 * The vm_map must be locked and its token held.
2952 vm_map_split(vm_map_entry_t entry)
2956 vm_object_t oobject;
2958 oobject = entry->object.vm_object;
2959 vm_object_hold(oobject);
2960 vm_object_chain_wait(oobject);
2961 vm_object_reference_locked(oobject);
2962 vm_object_clear_flag(oobject, OBJ_ONEMAPPING);
2963 vm_object_drop(oobject);
2966 vm_object_t oobject, nobject, bobject;
2969 vm_pindex_t offidxstart, offidxend, idx;
2971 vm_ooffset_t offset;
2974 * Setup. Chain lock the original object throughout the entire
2975 * routine to prevent new page faults from occuring.
2977 * XXX can madvise WILLNEED interfere with us too?
2979 oobject = entry->object.vm_object;
2980 vm_object_hold(oobject);
2981 vm_object_chain_acquire(oobject);
2984 * Original object cannot be split?
2986 if (oobject->handle == NULL || (oobject->type != OBJT_DEFAULT &&
2987 oobject->type != OBJT_SWAP)) {
2988 vm_object_chain_release(oobject);
2989 vm_object_reference_locked(oobject);
2990 vm_object_clear_flag(oobject, OBJ_ONEMAPPING);
2991 vm_object_drop(oobject);
2996 * Collapse original object with its backing store as an
2997 * optimization to reduce chain lengths when possible.
2999 * If ref_count <= 1 there aren't other non-overlapping vm_map_entry's
3000 * for oobject, so there's no point collapsing it.
3002 * Then re-check whether the object can be split.
3004 vm_object_collapse(oobject, NULL);
3006 if (oobject->ref_count <= 1 ||
3007 (oobject->type != OBJT_DEFAULT && oobject->type != OBJT_SWAP) ||
3008 (oobject->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) != OBJ_ONEMAPPING) {
3009 vm_object_chain_release(oobject);
3010 vm_object_reference_locked(oobject);
3011 vm_object_clear_flag(oobject, OBJ_ONEMAPPING);
3012 vm_object_drop(oobject);
3017 * Acquire the chain lock on the backing object.
3019 * Give bobject an additional ref count for when it will be shadowed
3022 if ((bobject = oobject->backing_object) != NULL) {
3023 vm_object_hold(bobject);
3024 vm_object_chain_wait(bobject);
3025 vm_object_reference_locked(bobject);
3026 vm_object_chain_acquire(bobject);
3027 KKASSERT(bobject->backing_object == bobject);
3028 KKASSERT((bobject->flags & OBJ_DEAD) == 0);
3032 * Calculate the object page range and allocate the new object.
3034 offset = entry->offset;
3038 offidxstart = OFF_TO_IDX(offset);
3039 offidxend = offidxstart + OFF_TO_IDX(e - s);
3040 size = offidxend - offidxstart;
3042 switch(oobject->type) {
3044 nobject = default_pager_alloc(NULL, IDX_TO_OFF(size),
3048 nobject = swap_pager_alloc(NULL, IDX_TO_OFF(size),
3057 if (nobject == NULL) {
3059 vm_object_chain_release(bobject);
3060 vm_object_deallocate(bobject);
3061 vm_object_drop(bobject);
3063 vm_object_chain_release(oobject);
3064 vm_object_reference_locked(oobject);
3065 vm_object_clear_flag(oobject, OBJ_ONEMAPPING);
3066 vm_object_drop(oobject);
3071 * The new object will replace entry->object.vm_object so it needs
3072 * a second reference (the caller expects an additional ref).
3074 vm_object_hold(nobject);
3075 vm_object_reference_locked(nobject);
3076 vm_object_chain_acquire(nobject);
3079 * nobject shadows bobject (oobject already shadows bobject).
3082 nobject->backing_object_offset =
3083 oobject->backing_object_offset + IDX_TO_OFF(offidxstart);
3084 nobject->backing_object = bobject;
3085 bobject->shadow_count++;
3086 bobject->generation++;
3087 LIST_INSERT_HEAD(&bobject->shadow_head, nobject, shadow_list);
3088 vm_object_clear_flag(bobject, OBJ_ONEMAPPING); /* XXX? */
3089 vm_object_chain_release(bobject);
3090 vm_object_drop(bobject);
3094 * Move the VM pages from oobject to nobject
3096 for (idx = 0; idx < size; idx++) {
3099 m = vm_page_lookup_busy_wait(oobject, offidxstart + idx,
3105 * We must wait for pending I/O to complete before we can
3108 * We do not have to VM_PROT_NONE the page as mappings should
3109 * not be changed by this operation.
3111 * NOTE: The act of renaming a page updates chaingen for both
3114 vm_page_rename(m, nobject, idx);
3115 /* page automatically made dirty by rename and cache handled */
3116 /* page remains busy */
3119 if (oobject->type == OBJT_SWAP) {
3120 vm_object_pip_add(oobject, 1);
3122 * copy oobject pages into nobject and destroy unneeded
3123 * pages in shadow object.
3125 swap_pager_copy(oobject, nobject, offidxstart, 0);
3126 vm_object_pip_wakeup(oobject);
3130 * Wakeup the pages we played with. No spl protection is needed
3131 * for a simple wakeup.
3133 for (idx = 0; idx < size; idx++) {
3134 m = vm_page_lookup(nobject, idx);
3136 KKASSERT(m->flags & PG_BUSY);
3140 entry->object.vm_object = nobject;
3141 entry->offset = 0LL;
3146 * NOTE: There is no need to remove OBJ_ONEMAPPING from oobject, the
3147 * related pages were moved and are no longer applicable to the
3150 * NOTE: Deallocate oobject (due to its entry->object.vm_object being
3151 * replaced by nobject).
3153 vm_object_chain_release(nobject);
3154 vm_object_drop(nobject);
3156 vm_object_chain_release(bobject);
3157 vm_object_drop(bobject);
3159 vm_object_chain_release(oobject);
3160 /*vm_object_clear_flag(oobject, OBJ_ONEMAPPING);*/
3161 vm_object_deallocate_locked(oobject);
3162 vm_object_drop(oobject);
3167 * Copies the contents of the source entry to the destination
3168 * entry. The entries *must* be aligned properly.
3170 * The vm_maps must be exclusively locked.
3171 * The vm_map's token must be held.
3173 * Because the maps are locked no faults can be in progress during the
3177 vm_map_copy_entry(vm_map_t src_map, vm_map_t dst_map,
3178 vm_map_entry_t src_entry, vm_map_entry_t dst_entry)
3180 vm_object_t src_object;
3182 if (dst_entry->maptype == VM_MAPTYPE_SUBMAP)
3184 if (src_entry->maptype == VM_MAPTYPE_SUBMAP)
3187 if (src_entry->wired_count == 0) {
3189 * If the source entry is marked needs_copy, it is already
3192 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
3193 pmap_protect(src_map->pmap,
3196 src_entry->protection & ~VM_PROT_WRITE);
3200 * Make a copy of the object.
3202 * The object must be locked prior to checking the object type
3203 * and for the call to vm_object_collapse() and vm_map_split().
3204 * We cannot use *_hold() here because the split code will
3205 * probably try to destroy the object. The lock is a pool
3206 * token and doesn't care.
3208 * We must bump src_map->timestamp when setting
3209 * MAP_ENTRY_NEEDS_COPY to force any concurrent fault
3210 * to retry, otherwise the concurrent fault might improperly
3211 * install a RW pte when its supposed to be a RO(COW) pte.
3212 * This race can occur because a vnode-backed fault may have
3213 * to temporarily release the map lock.
3215 if (src_entry->object.vm_object != NULL) {
3216 vm_map_split(src_entry);
3217 src_object = src_entry->object.vm_object;
3218 dst_entry->object.vm_object = src_object;
3219 src_entry->eflags |= (MAP_ENTRY_COW |
3220 MAP_ENTRY_NEEDS_COPY);
3221 dst_entry->eflags |= (MAP_ENTRY_COW |
3222 MAP_ENTRY_NEEDS_COPY);
3223 dst_entry->offset = src_entry->offset;
3224 ++src_map->timestamp;
3226 dst_entry->object.vm_object = NULL;
3227 dst_entry->offset = 0;
3230 pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
3231 dst_entry->end - dst_entry->start, src_entry->start);
3234 * Of course, wired down pages can't be set copy-on-write.
3235 * Cause wired pages to be copied into the new map by
3236 * simulating faults (the new pages are pageable)
3238 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry);
3244 * Create a new process vmspace structure and vm_map
3245 * based on those of an existing process. The new map
3246 * is based on the old map, according to the inheritance
3247 * values on the regions in that map.
3249 * The source map must not be locked.
3253 vmspace_fork(struct vmspace *vm1)
3255 struct vmspace *vm2;
3256 vm_map_t old_map = &vm1->vm_map;
3258 vm_map_entry_t old_entry;
3259 vm_map_entry_t new_entry;
3263 lwkt_gettoken(&vm1->vm_map.token);
3264 vm_map_lock(old_map);
3267 * XXX Note: upcalls are not copied.
3269 vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
3270 lwkt_gettoken(&vm2->vm_map.token);
3271 bcopy(&vm1->vm_startcopy, &vm2->vm_startcopy,
3272 (caddr_t)&vm1->vm_endcopy - (caddr_t)&vm1->vm_startcopy);
3273 new_map = &vm2->vm_map; /* XXX */
3274 new_map->timestamp = 1;
3276 vm_map_lock(new_map);
3279 old_entry = old_map->header.next;
3280 while (old_entry != &old_map->header) {
3282 old_entry = old_entry->next;
3285 count = vm_map_entry_reserve(count + MAP_RESERVE_COUNT);
3287 old_entry = old_map->header.next;
3288 while (old_entry != &old_map->header) {
3289 if (old_entry->maptype == VM_MAPTYPE_SUBMAP)
3290 panic("vm_map_fork: encountered a submap");
3292 switch (old_entry->inheritance) {
3293 case VM_INHERIT_NONE:
3295 case VM_INHERIT_SHARE:
3297 * Clone the entry, creating the shared object if
3300 if (old_entry->object.vm_object == NULL)
3301 vm_map_entry_allocate_object(old_entry);
3303 if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3305 * Shadow a map_entry which needs a copy,
3306 * replacing its object with a new object
3307 * that points to the old one. Ask the
3308 * shadow code to automatically add an
3309 * additional ref. We can't do it afterwords
3310 * because we might race a collapse. The call
3311 * to vm_map_entry_shadow() will also clear
3314 vm_map_entry_shadow(old_entry, 1);
3317 * We will make a shared copy of the object,
3318 * and must clear OBJ_ONEMAPPING.
3320 * XXX assert that object.vm_object != NULL
3321 * since we allocate it above.
3323 if (old_entry->object.vm_object) {
3324 object = old_entry->object.vm_object;
3325 vm_object_hold(object);
3326 vm_object_chain_wait(object);
3327 vm_object_reference_locked(object);
3328 vm_object_clear_flag(object,
3330 vm_object_drop(object);
3335 * Clone the entry. We've already bumped the ref on
3338 new_entry = vm_map_entry_create(new_map, &count);
3339 *new_entry = *old_entry;
3340 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
3341 new_entry->wired_count = 0;
3344 * Insert the entry into the new map -- we know we're
3345 * inserting at the end of the new map.
3348 vm_map_entry_link(new_map, new_map->header.prev,
3352 * Update the physical map
3354 pmap_copy(new_map->pmap, old_map->pmap,
3356 (old_entry->end - old_entry->start),
3359 case VM_INHERIT_COPY:
3361 * Clone the entry and link into the map.
3363 new_entry = vm_map_entry_create(new_map, &count);
3364 *new_entry = *old_entry;
3365 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
3366 new_entry->wired_count = 0;
3367 new_entry->object.vm_object = NULL;
3368 vm_map_entry_link(new_map, new_map->header.prev,
3370 vm_map_copy_entry(old_map, new_map, old_entry,
3374 old_entry = old_entry->next;
3377 new_map->size = old_map->size;
3378 vm_map_unlock(old_map);
3379 vm_map_unlock(new_map);
3380 vm_map_entry_release(count);
3382 lwkt_reltoken(&vm2->vm_map.token);
3383 lwkt_reltoken(&vm1->vm_map.token);
3389 * Create an auto-grow stack entry
3394 vm_map_stack (vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
3395 int flags, vm_prot_t prot, vm_prot_t max, int cow)
3397 vm_map_entry_t prev_entry;
3398 vm_map_entry_t new_stack_entry;
3399 vm_size_t init_ssize;
3402 vm_offset_t tmpaddr;
3404 cow |= MAP_IS_STACK;
3406 if (max_ssize < sgrowsiz)
3407 init_ssize = max_ssize;
3409 init_ssize = sgrowsiz;
3411 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
3415 * Find space for the mapping
3417 if ((flags & (MAP_FIXED | MAP_TRYFIXED)) == 0) {
3418 if (vm_map_findspace(map, addrbos, max_ssize, 1,
3421 vm_map_entry_release(count);
3422 return (KERN_NO_SPACE);
3427 /* If addr is already mapped, no go */
3428 if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
3430 vm_map_entry_release(count);
3431 return (KERN_NO_SPACE);
3435 /* XXX already handled by kern_mmap() */
3436 /* If we would blow our VMEM resource limit, no go */
3437 if (map->size + init_ssize >
3438 curproc->p_rlimit[RLIMIT_VMEM].rlim_cur) {
3440 vm_map_entry_release(count);
3441 return (KERN_NO_SPACE);
3446 * If we can't accomodate max_ssize in the current mapping,
3447 * no go. However, we need to be aware that subsequent user
3448 * mappings might map into the space we have reserved for
3449 * stack, and currently this space is not protected.
3451 * Hopefully we will at least detect this condition
3452 * when we try to grow the stack.
3454 if ((prev_entry->next != &map->header) &&
3455 (prev_entry->next->start < addrbos + max_ssize)) {
3457 vm_map_entry_release(count);
3458 return (KERN_NO_SPACE);
3462 * We initially map a stack of only init_ssize. We will
3463 * grow as needed later. Since this is to be a grow
3464 * down stack, we map at the top of the range.
3466 * Note: we would normally expect prot and max to be
3467 * VM_PROT_ALL, and cow to be 0. Possibly we should
3468 * eliminate these as input parameters, and just
3469 * pass these values here in the insert call.
3471 rv = vm_map_insert(map, &count,
3472 NULL, 0, addrbos + max_ssize - init_ssize,
3473 addrbos + max_ssize,
3478 /* Now set the avail_ssize amount */
3479 if (rv == KERN_SUCCESS) {
3480 if (prev_entry != &map->header)
3481 vm_map_clip_end(map, prev_entry, addrbos + max_ssize - init_ssize, &count);
3482 new_stack_entry = prev_entry->next;
3483 if (new_stack_entry->end != addrbos + max_ssize ||
3484 new_stack_entry->start != addrbos + max_ssize - init_ssize)
3485 panic ("Bad entry start/end for new stack entry");
3487 new_stack_entry->aux.avail_ssize = max_ssize - init_ssize;
3491 vm_map_entry_release(count);
3496 * Attempts to grow a vm stack entry. Returns KERN_SUCCESS if the
3497 * desired address is already mapped, or if we successfully grow
3498 * the stack. Also returns KERN_SUCCESS if addr is outside the
3499 * stack range (this is strange, but preserves compatibility with
3500 * the grow function in vm_machdep.c).
3505 vm_map_growstack (struct proc *p, vm_offset_t addr)
3507 vm_map_entry_t prev_entry;
3508 vm_map_entry_t stack_entry;
3509 vm_map_entry_t new_stack_entry;
3510 struct vmspace *vm = p->p_vmspace;
3511 vm_map_t map = &vm->vm_map;
3514 int rv = KERN_SUCCESS;
3516 int use_read_lock = 1;
3519 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
3522 vm_map_lock_read(map);
3526 /* If addr is already in the entry range, no need to grow.*/
3527 if (vm_map_lookup_entry(map, addr, &prev_entry))
3530 if ((stack_entry = prev_entry->next) == &map->header)
3532 if (prev_entry == &map->header)
3533 end = stack_entry->start - stack_entry->aux.avail_ssize;
3535 end = prev_entry->end;
3538 * This next test mimics the old grow function in vm_machdep.c.
3539 * It really doesn't quite make sense, but we do it anyway
3540 * for compatibility.
3542 * If not growable stack, return success. This signals the
3543 * caller to proceed as he would normally with normal vm.
3545 if (stack_entry->aux.avail_ssize < 1 ||
3546 addr >= stack_entry->start ||
3547 addr < stack_entry->start - stack_entry->aux.avail_ssize) {
3551 /* Find the minimum grow amount */
3552 grow_amount = roundup (stack_entry->start - addr, PAGE_SIZE);
3553 if (grow_amount > stack_entry->aux.avail_ssize) {
3559 * If there is no longer enough space between the entries
3560 * nogo, and adjust the available space. Note: this
3561 * should only happen if the user has mapped into the
3562 * stack area after the stack was created, and is
3563 * probably an error.
3565 * This also effectively destroys any guard page the user
3566 * might have intended by limiting the stack size.
3568 if (grow_amount > stack_entry->start - end) {
3569 if (use_read_lock && vm_map_lock_upgrade(map)) {
3575 stack_entry->aux.avail_ssize = stack_entry->start - end;
3580 is_procstack = addr >= (vm_offset_t)vm->vm_maxsaddr;
3582 /* If this is the main process stack, see if we're over the
3585 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount >
3586 p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
3591 /* Round up the grow amount modulo SGROWSIZ */
3592 grow_amount = roundup (grow_amount, sgrowsiz);
3593 if (grow_amount > stack_entry->aux.avail_ssize) {
3594 grow_amount = stack_entry->aux.avail_ssize;
3596 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount >
3597 p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
3598 grow_amount = p->p_rlimit[RLIMIT_STACK].rlim_cur -
3602 /* If we would blow our VMEM resource limit, no go */
3603 if (map->size + grow_amount > p->p_rlimit[RLIMIT_VMEM].rlim_cur) {
3608 if (use_read_lock && vm_map_lock_upgrade(map)) {
3615 /* Get the preliminary new entry start value */
3616 addr = stack_entry->start - grow_amount;
3618 /* If this puts us into the previous entry, cut back our growth
3619 * to the available space. Also, see the note above.
3622 stack_entry->aux.avail_ssize = stack_entry->start - end;
3626 rv = vm_map_insert(map, &count,
3627 NULL, 0, addr, stack_entry->start,
3629 VM_PROT_ALL, VM_PROT_ALL,
3632 /* Adjust the available stack space by the amount we grew. */
3633 if (rv == KERN_SUCCESS) {
3634 if (prev_entry != &map->header)
3635 vm_map_clip_end(map, prev_entry, addr, &count);
3636 new_stack_entry = prev_entry->next;
3637 if (new_stack_entry->end != stack_entry->start ||
3638 new_stack_entry->start != addr)
3639 panic ("Bad stack grow start/end in new stack entry");
3641 new_stack_entry->aux.avail_ssize =
3642 stack_entry->aux.avail_ssize -
3643 (new_stack_entry->end - new_stack_entry->start);
3645 vm->vm_ssize += btoc(new_stack_entry->end -
3646 new_stack_entry->start);
3649 if (map->flags & MAP_WIREFUTURE)
3650 vm_map_unwire(map, new_stack_entry->start,
3651 new_stack_entry->end, FALSE);
3656 vm_map_unlock_read(map);
3659 vm_map_entry_release(count);
3664 * Unshare the specified VM space for exec. If other processes are
3665 * mapped to it, then create a new one. The new vmspace is null.
3670 vmspace_exec(struct proc *p, struct vmspace *vmcopy)
3672 struct vmspace *oldvmspace = p->p_vmspace;
3673 struct vmspace *newvmspace;
3674 vm_map_t map = &p->p_vmspace->vm_map;
3677 * If we are execing a resident vmspace we fork it, otherwise
3678 * we create a new vmspace. Note that exitingcnt and upcalls
3679 * are not copied to the new vmspace.
3681 lwkt_gettoken(&oldvmspace->vm_map.token);
3683 newvmspace = vmspace_fork(vmcopy);
3684 lwkt_gettoken(&newvmspace->vm_map.token);
3686 newvmspace = vmspace_alloc(map->min_offset, map->max_offset);
3687 lwkt_gettoken(&newvmspace->vm_map.token);
3688 bcopy(&oldvmspace->vm_startcopy, &newvmspace->vm_startcopy,
3689 (caddr_t)&oldvmspace->vm_endcopy -
3690 (caddr_t)&oldvmspace->vm_startcopy);
3694 * Finish initializing the vmspace before assigning it
3695 * to the process. The vmspace will become the current vmspace
3698 pmap_pinit2(vmspace_pmap(newvmspace));
3699 pmap_replacevm(p, newvmspace, 0);
3700 lwkt_reltoken(&newvmspace->vm_map.token);
3701 lwkt_reltoken(&oldvmspace->vm_map.token);
3702 vmspace_free(oldvmspace);
3706 * Unshare the specified VM space for forcing COW. This
3707 * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
3710 vmspace_unshare(struct proc *p)
3712 struct vmspace *oldvmspace = p->p_vmspace;
3713 struct vmspace *newvmspace;
3715 lwkt_gettoken(&oldvmspace->vm_map.token);
3716 if (oldvmspace->vm_sysref.refcnt == 1) {
3717 lwkt_reltoken(&oldvmspace->vm_map.token);
3720 newvmspace = vmspace_fork(oldvmspace);
3721 lwkt_gettoken(&newvmspace->vm_map.token);
3722 pmap_pinit2(vmspace_pmap(newvmspace));
3723 pmap_replacevm(p, newvmspace, 0);
3724 lwkt_reltoken(&newvmspace->vm_map.token);
3725 lwkt_reltoken(&oldvmspace->vm_map.token);
3726 vmspace_free(oldvmspace);
3730 * vm_map_hint: return the beginning of the best area suitable for
3731 * creating a new mapping with "prot" protection.
3736 vm_map_hint(struct proc *p, vm_offset_t addr, vm_prot_t prot)
3738 struct vmspace *vms = p->p_vmspace;
3740 if (!randomize_mmap) {
3742 * Set a reasonable start point for the hint if it was
3743 * not specified or if it falls within the heap space.
3744 * Hinted mmap()s do not allocate out of the heap space.
3747 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
3748 addr < round_page((vm_offset_t)vms->vm_daddr + maxdsiz))) {
3749 addr = round_page((vm_offset_t)vms->vm_daddr + maxdsiz);
3755 if (addr != 0 && addr >= (vm_offset_t)vms->vm_daddr)
3761 * If executable skip first two pages, otherwise start
3762 * after data + heap region.
3764 if ((prot & VM_PROT_EXECUTE) &&
3765 ((vm_offset_t)vms->vm_daddr >= I386_MAX_EXE_ADDR)) {
3766 addr = (PAGE_SIZE * 2) +
3767 (karc4random() & (I386_MAX_EXE_ADDR / 2 - 1));
3768 return (round_page(addr));
3770 #endif /* __i386__ */
3773 addr = (vm_offset_t)vms->vm_daddr + MAXDSIZ;
3774 addr += karc4random() & (MIN((256 * 1024 * 1024), MAXDSIZ) - 1);
3776 return (round_page(addr));
3780 * Finds the VM object, offset, and protection for a given virtual address
3781 * in the specified map, assuming a page fault of the type specified.
3783 * Leaves the map in question locked for read; return values are guaranteed
3784 * until a vm_map_lookup_done call is performed. Note that the map argument
3785 * is in/out; the returned map must be used in the call to vm_map_lookup_done.
3787 * A handle (out_entry) is returned for use in vm_map_lookup_done, to make
3790 * If a lookup is requested with "write protection" specified, the map may
3791 * be changed to perform virtual copying operations, although the data
3792 * referenced will remain the same.
3797 vm_map_lookup(vm_map_t *var_map, /* IN/OUT */
3799 vm_prot_t fault_typea,
3800 vm_map_entry_t *out_entry, /* OUT */
3801 vm_object_t *object, /* OUT */
3802 vm_pindex_t *pindex, /* OUT */
3803 vm_prot_t *out_prot, /* OUT */
3804 boolean_t *wired) /* OUT */
3806 vm_map_entry_t entry;
3807 vm_map_t map = *var_map;
3809 vm_prot_t fault_type = fault_typea;
3810 int use_read_lock = 1;
3811 int rv = KERN_SUCCESS;
3815 vm_map_lock_read(map);
3820 * If the map has an interesting hint, try it before calling full
3821 * blown lookup routine.
3828 if ((entry == &map->header) ||
3829 (vaddr < entry->start) || (vaddr >= entry->end)) {
3830 vm_map_entry_t tmp_entry;
3833 * Entry was either not a valid hint, or the vaddr was not
3834 * contained in the entry, so do a full lookup.
3836 if (!vm_map_lookup_entry(map, vaddr, &tmp_entry)) {
3837 rv = KERN_INVALID_ADDRESS;
3848 if (entry->maptype == VM_MAPTYPE_SUBMAP) {
3849 vm_map_t old_map = map;
3851 *var_map = map = entry->object.sub_map;
3853 vm_map_unlock_read(old_map);
3855 vm_map_unlock(old_map);
3861 * Check whether this task is allowed to have this page.
3862 * Note the special case for MAP_ENTRY_COW
3863 * pages with an override. This is to implement a forced
3864 * COW for debuggers.
3867 if (fault_type & VM_PROT_OVERRIDE_WRITE)
3868 prot = entry->max_protection;
3870 prot = entry->protection;
3872 fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
3873 if ((fault_type & prot) != fault_type) {
3874 rv = KERN_PROTECTION_FAILURE;
3878 if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
3879 (entry->eflags & MAP_ENTRY_COW) &&
3880 (fault_type & VM_PROT_WRITE) &&
3881 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
3882 rv = KERN_PROTECTION_FAILURE;
3887 * If this page is not pageable, we have to get it for all possible
3890 *wired = (entry->wired_count != 0);
3892 prot = fault_type = entry->protection;
3895 * Virtual page tables may need to update the accessed (A) bit
3896 * in a page table entry. Upgrade the fault to a write fault for
3897 * that case if the map will support it. If the map does not support
3898 * it the page table entry simply will not be updated.
3900 if (entry->maptype == VM_MAPTYPE_VPAGETABLE) {
3901 if (prot & VM_PROT_WRITE)
3902 fault_type |= VM_PROT_WRITE;
3906 * If the entry was copy-on-write, we either ...
3908 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3910 * If we want to write the page, we may as well handle that
3911 * now since we've got the map locked.
3913 * If we don't need to write the page, we just demote the
3914 * permissions allowed.
3917 if (fault_type & VM_PROT_WRITE) {
3919 * Make a new object, and place it in the object
3920 * chain. Note that no new references have appeared
3921 * -- one just moved from the map to the new
3925 if (use_read_lock && vm_map_lock_upgrade(map)) {
3932 vm_map_entry_shadow(entry, 0);
3935 * We're attempting to read a copy-on-write page --
3936 * don't allow writes.
3939 prot &= ~VM_PROT_WRITE;
3944 * Create an object if necessary.
3946 if (entry->object.vm_object == NULL && !map->system_map) {
3947 if (use_read_lock && vm_map_lock_upgrade(map)) {
3953 vm_map_entry_allocate_object(entry);
3957 * Return the object/offset from this entry. If the entry was
3958 * copy-on-write or empty, it has been fixed up.
3961 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
3962 *object = entry->object.vm_object;
3965 * Return whether this is the only map sharing this data. On
3966 * success we return with a read lock held on the map. On failure
3967 * we return with the map unlocked.
3971 if (rv == KERN_SUCCESS) {
3972 if (use_read_lock == 0)
3973 vm_map_lock_downgrade(map);
3974 } else if (use_read_lock) {
3975 vm_map_unlock_read(map);
3983 * Releases locks acquired by a vm_map_lookup()
3984 * (according to the handle returned by that lookup).
3986 * No other requirements.
3989 vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry, int count)
3992 * Unlock the main-level map
3994 vm_map_unlock_read(map);
3996 vm_map_entry_release(count);
3999 #include "opt_ddb.h"
4001 #include <sys/kernel.h>
4003 #include <ddb/ddb.h>
4008 DB_SHOW_COMMAND(map, vm_map_print)
4011 /* XXX convert args. */
4012 vm_map_t map = (vm_map_t)addr;
4013 boolean_t full = have_addr;
4015 vm_map_entry_t entry;
4017 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
4019 (void *)map->pmap, map->nentries, map->timestamp);
4022 if (!full && db_indent)
4026 for (entry = map->header.next; entry != &map->header;
4027 entry = entry->next) {
4028 db_iprintf("map entry %p: start=%p, end=%p\n",
4029 (void *)entry, (void *)entry->start, (void *)entry->end);
4032 static char *inheritance_name[4] =
4033 {"share", "copy", "none", "donate_copy"};
4035 db_iprintf(" prot=%x/%x/%s",
4037 entry->max_protection,
4038 inheritance_name[(int)(unsigned char)entry->inheritance]);
4039 if (entry->wired_count != 0)
4040 db_printf(", wired");
4042 if (entry->maptype == VM_MAPTYPE_SUBMAP) {
4043 /* XXX no %qd in kernel. Truncate entry->offset. */
4044 db_printf(", share=%p, offset=0x%lx\n",
4045 (void *)entry->object.sub_map,
4046 (long)entry->offset);
4048 if ((entry->prev == &map->header) ||
4049 (entry->prev->object.sub_map !=
4050 entry->object.sub_map)) {
4052 vm_map_print((db_expr_t)(intptr_t)
4053 entry->object.sub_map,
4058 /* XXX no %qd in kernel. Truncate entry->offset. */
4059 db_printf(", object=%p, offset=0x%lx",
4060 (void *)entry->object.vm_object,
4061 (long)entry->offset);
4062 if (entry->eflags & MAP_ENTRY_COW)
4063 db_printf(", copy (%s)",
4064 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
4068 if ((entry->prev == &map->header) ||
4069 (entry->prev->object.vm_object !=
4070 entry->object.vm_object)) {
4072 vm_object_print((db_expr_t)(intptr_t)
4073 entry->object.vm_object,
4088 DB_SHOW_COMMAND(procvm, procvm)
4093 p = (struct proc *) addr;
4098 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
4099 (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
4100 (void *)vmspace_pmap(p->p_vmspace));
4102 vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);