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);
254 pmap_pinit(vmspace_pmap(vm)); /* (some fields reused) */
255 lwkt_gettoken(&vm->vm_map.token);
256 vm->vm_map.pmap = vmspace_pmap(vm); /* XXX */
258 vm->vm_exitingcnt = 0;
259 cpu_vmspace_alloc(vm);
260 sysref_activate(&vm->vm_sysref);
261 lwkt_reltoken(&vm->vm_map.token);
267 * dtor function - Some elements of the pmap are retained in the
268 * free-cached vmspaces to improve performance. We have to clean them up
269 * here before returning the vmspace to the memory pool.
274 vmspace_dtor(void *obj, void *private)
276 struct vmspace *vm = obj;
278 pmap_puninit(vmspace_pmap(vm));
282 * Called in two cases:
284 * (1) When the last sysref is dropped, but exitingcnt might still be
287 * (2) When there are no sysrefs (i.e. refcnt is negative) left and the
288 * exitingcnt becomes zero
290 * sysref will not scrap the object until we call sysref_put() once more
291 * after the last ref has been dropped.
293 * Interlocked by the sysref API.
296 vmspace_terminate(struct vmspace *vm)
301 * If exitingcnt is non-zero we can't get rid of the entire vmspace
302 * yet, but we can scrap user memory.
304 lwkt_gettoken(&vm->vm_map.token);
305 if (vm->vm_exitingcnt) {
307 pmap_remove_pages(vmspace_pmap(vm), VM_MIN_USER_ADDRESS,
308 VM_MAX_USER_ADDRESS);
309 vm_map_remove(&vm->vm_map, VM_MIN_USER_ADDRESS,
310 VM_MAX_USER_ADDRESS);
311 lwkt_reltoken(&vm->vm_map.token);
314 cpu_vmspace_free(vm);
317 * Make sure any SysV shm is freed, it might not have in
322 KKASSERT(vm->vm_upcalls == NULL);
325 * Lock the map, to wait out all other references to it.
326 * Delete all of the mappings and pages they hold, then call
327 * the pmap module to reclaim anything left.
329 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
330 vm_map_lock(&vm->vm_map);
331 vm_map_delete(&vm->vm_map, vm->vm_map.min_offset,
332 vm->vm_map.max_offset, &count);
333 vm_map_unlock(&vm->vm_map);
334 vm_map_entry_release(count);
336 lwkt_gettoken(&vmspace_pmap(vm)->pm_token);
337 pmap_release(vmspace_pmap(vm));
338 lwkt_reltoken(&vmspace_pmap(vm)->pm_token);
339 lwkt_reltoken(&vm->vm_map.token);
340 sysref_put(&vm->vm_sysref);
344 * vmspaces are not currently locked.
347 vmspace_lock(struct vmspace *vm __unused)
352 vmspace_unlock(struct vmspace *vm __unused)
357 * This is called during exit indicating that the vmspace is no
358 * longer in used by an exiting process, but the process has not yet
364 vmspace_exitbump(struct vmspace *vm)
366 lwkt_gettoken(&vm->vm_map.token);
368 lwkt_reltoken(&vm->vm_map.token);
372 * This is called in the wait*() handling code. The vmspace can be terminated
373 * after the last wait is finished using it.
378 vmspace_exitfree(struct proc *p)
383 lwkt_gettoken(&vm->vm_map.token);
386 if (--vm->vm_exitingcnt == 0 && sysref_isinactive(&vm->vm_sysref)) {
387 lwkt_reltoken(&vm->vm_map.token);
388 vmspace_terminate(vm);
390 lwkt_reltoken(&vm->vm_map.token);
395 * Swap useage is determined by taking the proportional swap used by
396 * VM objects backing the VM map. To make up for fractional losses,
397 * if the VM object has any swap use at all the associated map entries
398 * count for at least 1 swap page.
403 vmspace_swap_count(struct vmspace *vm)
405 vm_map_t map = &vm->vm_map;
411 lwkt_gettoken(&vm->vm_map.token);
412 for (cur = map->header.next; cur != &map->header; cur = cur->next) {
413 switch(cur->maptype) {
414 case VM_MAPTYPE_NORMAL:
415 case VM_MAPTYPE_VPAGETABLE:
416 if ((object = cur->object.vm_object) == NULL)
418 if (object->swblock_count) {
419 n = (cur->end - cur->start) / PAGE_SIZE;
420 count += object->swblock_count *
421 SWAP_META_PAGES * n / object->size + 1;
428 lwkt_reltoken(&vm->vm_map.token);
433 * Calculate the approximate number of anonymous pages in use by
434 * this vmspace. To make up for fractional losses, we count each
435 * VM object as having at least 1 anonymous page.
440 vmspace_anonymous_count(struct vmspace *vm)
442 vm_map_t map = &vm->vm_map;
447 lwkt_gettoken(&vm->vm_map.token);
448 for (cur = map->header.next; cur != &map->header; cur = cur->next) {
449 switch(cur->maptype) {
450 case VM_MAPTYPE_NORMAL:
451 case VM_MAPTYPE_VPAGETABLE:
452 if ((object = cur->object.vm_object) == NULL)
454 if (object->type != OBJT_DEFAULT &&
455 object->type != OBJT_SWAP) {
458 count += object->resident_page_count;
464 lwkt_reltoken(&vm->vm_map.token);
469 * Creates and returns a new empty VM map with the given physical map
470 * structure, and having the given lower and upper address bounds.
475 vm_map_create(vm_map_t result, pmap_t pmap, vm_offset_t min, vm_offset_t max)
478 result = zalloc(mapzone);
479 vm_map_init(result, min, max, pmap);
484 * Initialize an existing vm_map structure such as that in the vmspace
485 * structure. The pmap is initialized elsewhere.
490 vm_map_init(struct vm_map *map, vm_offset_t min, vm_offset_t max, pmap_t pmap)
492 map->header.next = map->header.prev = &map->header;
493 RB_INIT(&map->rb_root);
497 map->min_offset = min;
498 map->max_offset = max;
500 map->first_free = &map->header;
501 map->hint = &map->header;
504 lwkt_token_init(&map->token, "vm_map");
505 lockinit(&map->lock, "thrd_sleep", (hz + 9) / 10, 0);
506 TUNABLE_INT("vm.cache_vmspaces", &vmspace_sysref_class.nom_cache);
510 * Shadow the vm_map_entry's object. This typically needs to be done when
511 * a write fault is taken on an entry which had previously been cloned by
512 * fork(). The shared object (which might be NULL) must become private so
513 * we add a shadow layer above it.
515 * Object allocation for anonymous mappings is defered as long as possible.
516 * When creating a shadow, however, the underlying object must be instantiated
517 * so it can be shared.
519 * If the map segment is governed by a virtual page table then it is
520 * possible to address offsets beyond the mapped area. Just allocate
521 * a maximally sized object for this case.
523 * The vm_map must be exclusively locked.
524 * No other requirements.
528 vm_map_entry_shadow(vm_map_entry_t entry, int addref)
530 if (entry->maptype == VM_MAPTYPE_VPAGETABLE) {
531 vm_object_shadow(&entry->object.vm_object, &entry->offset,
532 0x7FFFFFFF, addref); /* XXX */
534 vm_object_shadow(&entry->object.vm_object, &entry->offset,
535 atop(entry->end - entry->start), addref);
537 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
541 * Allocate an object for a vm_map_entry.
543 * Object allocation for anonymous mappings is defered as long as possible.
544 * This function is called when we can defer no longer, generally when a map
545 * entry might be split or forked or takes a page fault.
547 * If the map segment is governed by a virtual page table then it is
548 * possible to address offsets beyond the mapped area. Just allocate
549 * a maximally sized object for this case.
551 * The vm_map must be exclusively locked.
552 * No other requirements.
555 vm_map_entry_allocate_object(vm_map_entry_t entry)
559 if (entry->maptype == VM_MAPTYPE_VPAGETABLE) {
560 obj = vm_object_allocate(OBJT_DEFAULT, 0x7FFFFFFF); /* XXX */
562 obj = vm_object_allocate(OBJT_DEFAULT,
563 atop(entry->end - entry->start));
565 entry->object.vm_object = obj;
570 * Set an initial negative count so the first attempt to reserve
571 * space preloads a bunch of vm_map_entry's for this cpu. Also
572 * pre-allocate 2 vm_map_entries which will be needed by zalloc() to
573 * map a new page for vm_map_entry structures. SMP systems are
574 * particularly sensitive.
576 * This routine is called in early boot so we cannot just call
577 * vm_map_entry_reserve().
579 * Called from the low level boot code only (for each cpu)
582 vm_map_entry_reserve_cpu_init(globaldata_t gd)
584 vm_map_entry_t entry;
587 gd->gd_vme_avail -= MAP_RESERVE_COUNT * 2;
588 entry = &cpu_map_entry_init[gd->gd_cpuid][0];
589 for (i = 0; i < VMEPERCPU; ++i, ++entry) {
590 entry->next = gd->gd_vme_base;
591 gd->gd_vme_base = entry;
596 * Reserves vm_map_entry structures so code later on can manipulate
597 * map_entry structures within a locked map without blocking trying
598 * to allocate a new vm_map_entry.
603 vm_map_entry_reserve(int count)
605 struct globaldata *gd = mycpu;
606 vm_map_entry_t entry;
609 * Make sure we have enough structures in gd_vme_base to handle
610 * the reservation request.
612 * The critical section protects access to the per-cpu gd.
615 while (gd->gd_vme_avail < count) {
616 entry = zalloc(mapentzone);
617 entry->next = gd->gd_vme_base;
618 gd->gd_vme_base = entry;
621 gd->gd_vme_avail -= count;
628 * Releases previously reserved vm_map_entry structures that were not
629 * used. If we have too much junk in our per-cpu cache clean some of
635 vm_map_entry_release(int count)
637 struct globaldata *gd = mycpu;
638 vm_map_entry_t entry;
641 gd->gd_vme_avail += count;
642 while (gd->gd_vme_avail > MAP_RESERVE_SLOP) {
643 entry = gd->gd_vme_base;
644 KKASSERT(entry != NULL);
645 gd->gd_vme_base = entry->next;
648 zfree(mapentzone, entry);
655 * Reserve map entry structures for use in kernel_map itself. These
656 * entries have *ALREADY* been reserved on a per-cpu basis when the map
657 * was inited. This function is used by zalloc() to avoid a recursion
658 * when zalloc() itself needs to allocate additional kernel memory.
660 * This function works like the normal reserve but does not load the
661 * vm_map_entry cache (because that would result in an infinite
662 * recursion). Note that gd_vme_avail may go negative. This is expected.
664 * Any caller of this function must be sure to renormalize after
665 * potentially eating entries to ensure that the reserve supply
671 vm_map_entry_kreserve(int count)
673 struct globaldata *gd = mycpu;
676 gd->gd_vme_avail -= count;
678 KASSERT(gd->gd_vme_base != NULL,
679 ("no reserved entries left, gd_vme_avail = %d\n",
685 * Release previously reserved map entries for kernel_map. We do not
686 * attempt to clean up like the normal release function as this would
687 * cause an unnecessary (but probably not fatal) deep procedure call.
692 vm_map_entry_krelease(int count)
694 struct globaldata *gd = mycpu;
697 gd->gd_vme_avail += count;
702 * Allocates a VM map entry for insertion. No entry fields are filled in.
704 * The entries should have previously been reserved. The reservation count
705 * is tracked in (*countp).
709 static vm_map_entry_t
710 vm_map_entry_create(vm_map_t map, int *countp)
712 struct globaldata *gd = mycpu;
713 vm_map_entry_t entry;
715 KKASSERT(*countp > 0);
718 entry = gd->gd_vme_base;
719 KASSERT(entry != NULL, ("gd_vme_base NULL! count %d", *countp));
720 gd->gd_vme_base = entry->next;
727 * Dispose of a vm_map_entry that is no longer being referenced.
732 vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry, int *countp)
734 struct globaldata *gd = mycpu;
736 KKASSERT(map->hint != entry);
737 KKASSERT(map->first_free != entry);
741 entry->next = gd->gd_vme_base;
742 gd->gd_vme_base = entry;
748 * Insert/remove entries from maps.
750 * The related map must be exclusively locked.
751 * The caller must hold map->token
752 * No other requirements.
755 vm_map_entry_link(vm_map_t map,
756 vm_map_entry_t after_where,
757 vm_map_entry_t entry)
759 ASSERT_VM_MAP_LOCKED(map);
762 entry->prev = after_where;
763 entry->next = after_where->next;
764 entry->next->prev = entry;
765 after_where->next = entry;
766 if (vm_map_rb_tree_RB_INSERT(&map->rb_root, entry))
767 panic("vm_map_entry_link: dup addr map %p ent %p", map, entry);
771 vm_map_entry_unlink(vm_map_t map,
772 vm_map_entry_t entry)
777 ASSERT_VM_MAP_LOCKED(map);
779 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
780 panic("vm_map_entry_unlink: attempt to mess with "
781 "locked entry! %p", entry);
787 vm_map_rb_tree_RB_REMOVE(&map->rb_root, entry);
792 * Finds the map entry containing (or immediately preceding) the specified
793 * address in the given map. The entry is returned in (*entry).
795 * The boolean result indicates whether the address is actually contained
798 * The related map must be locked.
799 * No other requirements.
802 vm_map_lookup_entry(vm_map_t map, vm_offset_t address, vm_map_entry_t *entry)
807 ASSERT_VM_MAP_LOCKED(map);
810 * XXX TEMPORARILY DISABLED. For some reason our attempt to revive
811 * the hint code with the red-black lookup meets with system crashes
812 * and lockups. We do not yet know why.
814 * It is possible that the problem is related to the setting
815 * of the hint during map_entry deletion, in the code specified
816 * at the GGG comment later on in this file.
819 * Quickly check the cached hint, there's a good chance of a match.
821 if (map->hint != &map->header) {
823 if (address >= tmp->start && address < tmp->end) {
831 * Locate the record from the top of the tree. 'last' tracks the
832 * closest prior record and is returned if no match is found, which
833 * in binary tree terms means tracking the most recent right-branch
834 * taken. If there is no prior record, &map->header is returned.
837 tmp = RB_ROOT(&map->rb_root);
840 if (address >= tmp->start) {
841 if (address < tmp->end) {
847 tmp = RB_RIGHT(tmp, rb_entry);
849 tmp = RB_LEFT(tmp, rb_entry);
857 * Inserts the given whole VM object into the target map at the specified
858 * address range. The object's size should match that of the address range.
860 * The map must be exclusively locked.
861 * The object must be held.
862 * The caller must have reserved sufficient vm_map_entry structures.
864 * If object is non-NULL, ref count must be bumped by caller prior to
865 * making call to account for the new entry.
868 vm_map_insert(vm_map_t map, int *countp,
869 vm_object_t object, vm_ooffset_t offset,
870 vm_offset_t start, vm_offset_t end,
871 vm_maptype_t maptype,
872 vm_prot_t prot, vm_prot_t max,
875 vm_map_entry_t new_entry;
876 vm_map_entry_t prev_entry;
877 vm_map_entry_t temp_entry;
878 vm_eflags_t protoeflags;
881 ASSERT_VM_MAP_LOCKED(map);
883 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
886 * Check that the start and end points are not bogus.
888 if ((start < map->min_offset) || (end > map->max_offset) ||
890 return (KERN_INVALID_ADDRESS);
893 * Find the entry prior to the proposed starting address; if it's part
894 * of an existing entry, this range is bogus.
896 if (vm_map_lookup_entry(map, start, &temp_entry))
897 return (KERN_NO_SPACE);
899 prev_entry = temp_entry;
902 * Assert that the next entry doesn't overlap the end point.
905 if ((prev_entry->next != &map->header) &&
906 (prev_entry->next->start < end))
907 return (KERN_NO_SPACE);
911 if (cow & MAP_COPY_ON_WRITE)
912 protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
914 if (cow & MAP_NOFAULT) {
915 protoeflags |= MAP_ENTRY_NOFAULT;
917 KASSERT(object == NULL,
918 ("vm_map_insert: paradoxical MAP_NOFAULT request"));
920 if (cow & MAP_DISABLE_SYNCER)
921 protoeflags |= MAP_ENTRY_NOSYNC;
922 if (cow & MAP_DISABLE_COREDUMP)
923 protoeflags |= MAP_ENTRY_NOCOREDUMP;
924 if (cow & MAP_IS_STACK)
925 protoeflags |= MAP_ENTRY_STACK;
926 if (cow & MAP_IS_KSTACK)
927 protoeflags |= MAP_ENTRY_KSTACK;
929 lwkt_gettoken(&map->token);
933 * When object is non-NULL, it could be shared with another
934 * process. We have to set or clear OBJ_ONEMAPPING
937 if ((object->ref_count > 1) || (object->shadow_count != 0)) {
938 vm_object_clear_flag(object, OBJ_ONEMAPPING);
941 else if ((prev_entry != &map->header) &&
942 (prev_entry->eflags == protoeflags) &&
943 (prev_entry->end == start) &&
944 (prev_entry->wired_count == 0) &&
945 prev_entry->maptype == maptype &&
946 ((prev_entry->object.vm_object == NULL) ||
947 vm_object_coalesce(prev_entry->object.vm_object,
948 OFF_TO_IDX(prev_entry->offset),
949 (vm_size_t)(prev_entry->end - prev_entry->start),
950 (vm_size_t)(end - prev_entry->end)))) {
952 * We were able to extend the object. Determine if we
953 * can extend the previous map entry to include the
956 if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
957 (prev_entry->protection == prot) &&
958 (prev_entry->max_protection == max)) {
959 map->size += (end - prev_entry->end);
960 prev_entry->end = end;
961 vm_map_simplify_entry(map, prev_entry, countp);
962 lwkt_reltoken(&map->token);
963 return (KERN_SUCCESS);
967 * If we can extend the object but cannot extend the
968 * map entry, we have to create a new map entry. We
969 * must bump the ref count on the extended object to
970 * account for it. object may be NULL.
972 object = prev_entry->object.vm_object;
973 offset = prev_entry->offset +
974 (prev_entry->end - prev_entry->start);
976 vm_object_hold(object);
977 vm_object_chain_wait(object);
978 vm_object_reference_locked(object);
984 * NOTE: if conditionals fail, object can be NULL here. This occurs
985 * in things like the buffer map where we manage kva but do not manage
993 new_entry = vm_map_entry_create(map, countp);
994 new_entry->start = start;
995 new_entry->end = end;
997 new_entry->maptype = maptype;
998 new_entry->eflags = protoeflags;
999 new_entry->object.vm_object = object;
1000 new_entry->offset = offset;
1001 new_entry->aux.master_pde = 0;
1003 new_entry->inheritance = VM_INHERIT_DEFAULT;
1004 new_entry->protection = prot;
1005 new_entry->max_protection = max;
1006 new_entry->wired_count = 0;
1009 * Insert the new entry into the list
1012 vm_map_entry_link(map, prev_entry, new_entry);
1013 map->size += new_entry->end - new_entry->start;
1016 * Update the free space hint. Entries cannot overlap.
1017 * An exact comparison is needed to avoid matching
1018 * against the map->header.
1020 if ((map->first_free == prev_entry) &&
1021 (prev_entry->end == new_entry->start)) {
1022 map->first_free = new_entry;
1027 * Temporarily removed to avoid MAP_STACK panic, due to
1028 * MAP_STACK being a huge hack. Will be added back in
1029 * when MAP_STACK (and the user stack mapping) is fixed.
1032 * It may be possible to simplify the entry
1034 vm_map_simplify_entry(map, new_entry, countp);
1038 * Try to pre-populate the page table. Mappings governed by virtual
1039 * page tables cannot be prepopulated without a lot of work, so
1042 if ((cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) &&
1043 maptype != VM_MAPTYPE_VPAGETABLE) {
1044 pmap_object_init_pt(map->pmap, start, prot,
1045 object, OFF_TO_IDX(offset), end - start,
1046 cow & MAP_PREFAULT_PARTIAL);
1049 vm_object_drop(object);
1051 lwkt_reltoken(&map->token);
1052 return (KERN_SUCCESS);
1056 * Find sufficient space for `length' bytes in the given map, starting at
1057 * `start'. Returns 0 on success, 1 on no space.
1059 * This function will returned an arbitrarily aligned pointer. If no
1060 * particular alignment is required you should pass align as 1. Note that
1061 * the map may return PAGE_SIZE aligned pointers if all the lengths used in
1062 * the map are a multiple of PAGE_SIZE, even if you pass a smaller align
1065 * 'align' should be a power of 2 but is not required to be.
1067 * The map must be exclusively locked.
1068 * No other requirements.
1071 vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length,
1072 vm_size_t align, int flags, vm_offset_t *addr)
1074 vm_map_entry_t entry, next;
1076 vm_offset_t align_mask;
1078 if (start < map->min_offset)
1079 start = map->min_offset;
1080 if (start > map->max_offset)
1084 * If the alignment is not a power of 2 we will have to use
1085 * a mod/division, set align_mask to a special value.
1087 if ((align | (align - 1)) + 1 != (align << 1))
1088 align_mask = (vm_offset_t)-1;
1090 align_mask = align - 1;
1093 * Look for the first possible address; if there's already something
1094 * at this address, we have to start after it.
1096 if (start == map->min_offset) {
1097 if ((entry = map->first_free) != &map->header)
1102 if (vm_map_lookup_entry(map, start, &tmp))
1108 * Look through the rest of the map, trying to fit a new region in the
1109 * gap between existing regions, or after the very last region.
1111 for (;; start = (entry = next)->end) {
1113 * Adjust the proposed start by the requested alignment,
1114 * be sure that we didn't wrap the address.
1116 if (align_mask == (vm_offset_t)-1)
1117 end = ((start + align - 1) / align) * align;
1119 end = (start + align_mask) & ~align_mask;
1124 * Find the end of the proposed new region. Be sure we didn't
1125 * go beyond the end of the map, or wrap around the address.
1126 * Then check to see if this is the last entry or if the
1127 * proposed end fits in the gap between this and the next
1130 end = start + length;
1131 if (end > map->max_offset || end < start)
1136 * If the next entry's start address is beyond the desired
1137 * end address we may have found a good entry.
1139 * If the next entry is a stack mapping we do not map into
1140 * the stack's reserved space.
1142 * XXX continue to allow mapping into the stack's reserved
1143 * space if doing a MAP_STACK mapping inside a MAP_STACK
1144 * mapping, for backwards compatibility. But the caller
1145 * really should use MAP_STACK | MAP_TRYFIXED if they
1148 if (next == &map->header)
1150 if (next->start >= end) {
1151 if ((next->eflags & MAP_ENTRY_STACK) == 0)
1153 if (flags & MAP_STACK)
1155 if (next->start - next->aux.avail_ssize >= end)
1162 * Grow the kernel_map if necessary. pmap_growkernel() will panic
1163 * if it fails. The kernel_map is locked and nothing can steal
1164 * our address space if pmap_growkernel() blocks.
1166 * NOTE: This may be unconditionally called for kldload areas on
1167 * x86_64 because these do not bump kernel_vm_end (which would
1168 * fill 128G worth of page tables!). Therefore we must not
1171 if (map == &kernel_map) {
1174 kstop = round_page(start + length);
1175 if (kstop > kernel_vm_end)
1176 pmap_growkernel(start, kstop);
1183 * vm_map_find finds an unallocated region in the target address map with
1184 * the given length and allocates it. The search is defined to be first-fit
1185 * from the specified address; the region found is returned in the same
1188 * If object is non-NULL, ref count must be bumped by caller
1189 * prior to making call to account for the new entry.
1191 * No requirements. This function will lock the map temporarily.
1194 vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1195 vm_offset_t *addr, vm_size_t length, vm_size_t align,
1197 vm_maptype_t maptype,
1198 vm_prot_t prot, vm_prot_t max,
1207 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1210 vm_object_hold(object);
1212 if (vm_map_findspace(map, start, length, align, 0, addr)) {
1214 vm_map_entry_release(count);
1215 return (KERN_NO_SPACE);
1219 result = vm_map_insert(map, &count, object, offset,
1220 start, start + length,
1225 vm_object_drop(object);
1227 vm_map_entry_release(count);
1233 * Simplify the given map entry by merging with either neighbor. This
1234 * routine also has the ability to merge with both neighbors.
1236 * This routine guarentees that the passed entry remains valid (though
1237 * possibly extended). When merging, this routine may delete one or
1238 * both neighbors. No action is taken on entries which have their
1239 * in-transition flag set.
1241 * The map must be exclusively locked.
1244 vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry, int *countp)
1246 vm_map_entry_t next, prev;
1247 vm_size_t prevsize, esize;
1249 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1250 ++mycpu->gd_cnt.v_intrans_coll;
1254 if (entry->maptype == VM_MAPTYPE_SUBMAP)
1258 if (prev != &map->header) {
1259 prevsize = prev->end - prev->start;
1260 if ( (prev->end == entry->start) &&
1261 (prev->maptype == entry->maptype) &&
1262 (prev->object.vm_object == entry->object.vm_object) &&
1263 (!prev->object.vm_object ||
1264 (prev->offset + prevsize == entry->offset)) &&
1265 (prev->eflags == entry->eflags) &&
1266 (prev->protection == entry->protection) &&
1267 (prev->max_protection == entry->max_protection) &&
1268 (prev->inheritance == entry->inheritance) &&
1269 (prev->wired_count == entry->wired_count)) {
1270 if (map->first_free == prev)
1271 map->first_free = entry;
1272 if (map->hint == prev)
1274 vm_map_entry_unlink(map, prev);
1275 entry->start = prev->start;
1276 entry->offset = prev->offset;
1277 if (prev->object.vm_object)
1278 vm_object_deallocate(prev->object.vm_object);
1279 vm_map_entry_dispose(map, prev, countp);
1284 if (next != &map->header) {
1285 esize = entry->end - entry->start;
1286 if ((entry->end == next->start) &&
1287 (next->maptype == entry->maptype) &&
1288 (next->object.vm_object == entry->object.vm_object) &&
1289 (!entry->object.vm_object ||
1290 (entry->offset + esize == next->offset)) &&
1291 (next->eflags == entry->eflags) &&
1292 (next->protection == entry->protection) &&
1293 (next->max_protection == entry->max_protection) &&
1294 (next->inheritance == entry->inheritance) &&
1295 (next->wired_count == entry->wired_count)) {
1296 if (map->first_free == next)
1297 map->first_free = entry;
1298 if (map->hint == next)
1300 vm_map_entry_unlink(map, next);
1301 entry->end = next->end;
1302 if (next->object.vm_object)
1303 vm_object_deallocate(next->object.vm_object);
1304 vm_map_entry_dispose(map, next, countp);
1310 * Asserts that the given entry begins at or after the specified address.
1311 * If necessary, it splits the entry into two.
1313 #define vm_map_clip_start(map, entry, startaddr, countp) \
1315 if (startaddr > entry->start) \
1316 _vm_map_clip_start(map, entry, startaddr, countp); \
1320 * This routine is called only when it is known that the entry must be split.
1322 * The map must be exclusively locked.
1325 _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start,
1328 vm_map_entry_t new_entry;
1331 * Split off the front portion -- note that we must insert the new
1332 * entry BEFORE this one, so that this entry has the specified
1336 vm_map_simplify_entry(map, entry, countp);
1339 * If there is no object backing this entry, we might as well create
1340 * one now. If we defer it, an object can get created after the map
1341 * is clipped, and individual objects will be created for the split-up
1342 * map. This is a bit of a hack, but is also about the best place to
1343 * put this improvement.
1345 if (entry->object.vm_object == NULL && !map->system_map) {
1346 vm_map_entry_allocate_object(entry);
1349 new_entry = vm_map_entry_create(map, countp);
1350 *new_entry = *entry;
1352 new_entry->end = start;
1353 entry->offset += (start - entry->start);
1354 entry->start = start;
1356 vm_map_entry_link(map, entry->prev, new_entry);
1358 switch(entry->maptype) {
1359 case VM_MAPTYPE_NORMAL:
1360 case VM_MAPTYPE_VPAGETABLE:
1361 if (new_entry->object.vm_object) {
1362 vm_object_hold(new_entry->object.vm_object);
1363 vm_object_chain_wait(new_entry->object.vm_object);
1364 vm_object_reference_locked(new_entry->object.vm_object);
1365 vm_object_drop(new_entry->object.vm_object);
1374 * Asserts that the given entry ends at or before the specified address.
1375 * If necessary, it splits the entry into two.
1377 * The map must be exclusively locked.
1379 #define vm_map_clip_end(map, entry, endaddr, countp) \
1381 if (endaddr < entry->end) \
1382 _vm_map_clip_end(map, entry, endaddr, countp); \
1386 * This routine is called only when it is known that the entry must be split.
1388 * The map must be exclusively locked.
1391 _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end,
1394 vm_map_entry_t new_entry;
1397 * If there is no object backing this entry, we might as well create
1398 * one now. If we defer it, an object can get created after the map
1399 * is clipped, and individual objects will be created for the split-up
1400 * map. This is a bit of a hack, but is also about the best place to
1401 * put this improvement.
1404 if (entry->object.vm_object == NULL && !map->system_map) {
1405 vm_map_entry_allocate_object(entry);
1409 * Create a new entry and insert it AFTER the specified entry
1412 new_entry = vm_map_entry_create(map, countp);
1413 *new_entry = *entry;
1415 new_entry->start = entry->end = end;
1416 new_entry->offset += (end - entry->start);
1418 vm_map_entry_link(map, entry, new_entry);
1420 switch(entry->maptype) {
1421 case VM_MAPTYPE_NORMAL:
1422 case VM_MAPTYPE_VPAGETABLE:
1423 if (new_entry->object.vm_object) {
1424 vm_object_hold(new_entry->object.vm_object);
1425 vm_object_chain_wait(new_entry->object.vm_object);
1426 vm_object_reference_locked(new_entry->object.vm_object);
1427 vm_object_drop(new_entry->object.vm_object);
1436 * Asserts that the starting and ending region addresses fall within the
1437 * valid range for the map.
1439 #define VM_MAP_RANGE_CHECK(map, start, end) \
1441 if (start < vm_map_min(map)) \
1442 start = vm_map_min(map); \
1443 if (end > vm_map_max(map)) \
1444 end = vm_map_max(map); \
1450 * Used to block when an in-transition collison occurs. The map
1451 * is unlocked for the sleep and relocked before the return.
1454 vm_map_transition_wait(vm_map_t map)
1456 tsleep_interlock(map, 0);
1458 tsleep(map, PINTERLOCKED, "vment", 0);
1463 * When we do blocking operations with the map lock held it is
1464 * possible that a clip might have occured on our in-transit entry,
1465 * requiring an adjustment to the entry in our loop. These macros
1466 * help the pageable and clip_range code deal with the case. The
1467 * conditional costs virtually nothing if no clipping has occured.
1470 #define CLIP_CHECK_BACK(entry, save_start) \
1472 while (entry->start != save_start) { \
1473 entry = entry->prev; \
1474 KASSERT(entry != &map->header, ("bad entry clip")); \
1478 #define CLIP_CHECK_FWD(entry, save_end) \
1480 while (entry->end != save_end) { \
1481 entry = entry->next; \
1482 KASSERT(entry != &map->header, ("bad entry clip")); \
1488 * Clip the specified range and return the base entry. The
1489 * range may cover several entries starting at the returned base
1490 * and the first and last entry in the covering sequence will be
1491 * properly clipped to the requested start and end address.
1493 * If no holes are allowed you should pass the MAP_CLIP_NO_HOLES
1496 * The MAP_ENTRY_IN_TRANSITION flag will be set for the entries
1497 * covered by the requested range.
1499 * The map must be exclusively locked on entry and will remain locked
1500 * on return. If no range exists or the range contains holes and you
1501 * specified that no holes were allowed, NULL will be returned. This
1502 * routine may temporarily unlock the map in order avoid a deadlock when
1507 vm_map_clip_range(vm_map_t map, vm_offset_t start, vm_offset_t end,
1508 int *countp, int flags)
1510 vm_map_entry_t start_entry;
1511 vm_map_entry_t entry;
1514 * Locate the entry and effect initial clipping. The in-transition
1515 * case does not occur very often so do not try to optimize it.
1518 if (vm_map_lookup_entry(map, start, &start_entry) == FALSE)
1520 entry = start_entry;
1521 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1522 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1523 ++mycpu->gd_cnt.v_intrans_coll;
1524 ++mycpu->gd_cnt.v_intrans_wait;
1525 vm_map_transition_wait(map);
1527 * entry and/or start_entry may have been clipped while
1528 * we slept, or may have gone away entirely. We have
1529 * to restart from the lookup.
1535 * Since we hold an exclusive map lock we do not have to restart
1536 * after clipping, even though clipping may block in zalloc.
1538 vm_map_clip_start(map, entry, start, countp);
1539 vm_map_clip_end(map, entry, end, countp);
1540 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
1543 * Scan entries covered by the range. When working on the next
1544 * entry a restart need only re-loop on the current entry which
1545 * we have already locked, since 'next' may have changed. Also,
1546 * even though entry is safe, it may have been clipped so we
1547 * have to iterate forwards through the clip after sleeping.
1549 while (entry->next != &map->header && entry->next->start < end) {
1550 vm_map_entry_t next = entry->next;
1552 if (flags & MAP_CLIP_NO_HOLES) {
1553 if (next->start > entry->end) {
1554 vm_map_unclip_range(map, start_entry,
1555 start, entry->end, countp, flags);
1560 if (next->eflags & MAP_ENTRY_IN_TRANSITION) {
1561 vm_offset_t save_end = entry->end;
1562 next->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1563 ++mycpu->gd_cnt.v_intrans_coll;
1564 ++mycpu->gd_cnt.v_intrans_wait;
1565 vm_map_transition_wait(map);
1568 * clips might have occured while we blocked.
1570 CLIP_CHECK_FWD(entry, save_end);
1571 CLIP_CHECK_BACK(start_entry, start);
1575 * No restart necessary even though clip_end may block, we
1576 * are holding the map lock.
1578 vm_map_clip_end(map, next, end, countp);
1579 next->eflags |= MAP_ENTRY_IN_TRANSITION;
1582 if (flags & MAP_CLIP_NO_HOLES) {
1583 if (entry->end != end) {
1584 vm_map_unclip_range(map, start_entry,
1585 start, entry->end, countp, flags);
1589 return(start_entry);
1593 * Undo the effect of vm_map_clip_range(). You should pass the same
1594 * flags and the same range that you passed to vm_map_clip_range().
1595 * This code will clear the in-transition flag on the entries and
1596 * wake up anyone waiting. This code will also simplify the sequence
1597 * and attempt to merge it with entries before and after the sequence.
1599 * The map must be locked on entry and will remain locked on return.
1601 * Note that you should also pass the start_entry returned by
1602 * vm_map_clip_range(). However, if you block between the two calls
1603 * with the map unlocked please be aware that the start_entry may
1604 * have been clipped and you may need to scan it backwards to find
1605 * the entry corresponding with the original start address. You are
1606 * responsible for this, vm_map_unclip_range() expects the correct
1607 * start_entry to be passed to it and will KASSERT otherwise.
1611 vm_map_unclip_range(vm_map_t map, vm_map_entry_t start_entry,
1612 vm_offset_t start, vm_offset_t end,
1613 int *countp, int flags)
1615 vm_map_entry_t entry;
1617 entry = start_entry;
1619 KASSERT(entry->start == start, ("unclip_range: illegal base entry"));
1620 while (entry != &map->header && entry->start < end) {
1621 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
1622 ("in-transition flag not set during unclip on: %p",
1624 KASSERT(entry->end <= end,
1625 ("unclip_range: tail wasn't clipped"));
1626 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
1627 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
1628 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
1631 entry = entry->next;
1635 * Simplification does not block so there is no restart case.
1637 entry = start_entry;
1638 while (entry != &map->header && entry->start < end) {
1639 vm_map_simplify_entry(map, entry, countp);
1640 entry = entry->next;
1645 * Mark the given range as handled by a subordinate map.
1647 * This range must have been created with vm_map_find(), and no other
1648 * operations may have been performed on this range prior to calling
1651 * Submappings cannot be removed.
1656 vm_map_submap(vm_map_t map, vm_offset_t start, vm_offset_t end, vm_map_t submap)
1658 vm_map_entry_t entry;
1659 int result = KERN_INVALID_ARGUMENT;
1662 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1665 VM_MAP_RANGE_CHECK(map, start, end);
1667 if (vm_map_lookup_entry(map, start, &entry)) {
1668 vm_map_clip_start(map, entry, start, &count);
1670 entry = entry->next;
1673 vm_map_clip_end(map, entry, end, &count);
1675 if ((entry->start == start) && (entry->end == end) &&
1676 ((entry->eflags & MAP_ENTRY_COW) == 0) &&
1677 (entry->object.vm_object == NULL)) {
1678 entry->object.sub_map = submap;
1679 entry->maptype = VM_MAPTYPE_SUBMAP;
1680 result = KERN_SUCCESS;
1683 vm_map_entry_release(count);
1689 * Sets the protection of the specified address region in the target map.
1690 * If "set_max" is specified, the maximum protection is to be set;
1691 * otherwise, only the current protection is affected.
1693 * The protection is not applicable to submaps, but is applicable to normal
1694 * maps and maps governed by virtual page tables. For example, when operating
1695 * on a virtual page table our protection basically controls how COW occurs
1696 * on the backing object, whereas the virtual page table abstraction itself
1697 * is an abstraction for userland.
1702 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
1703 vm_prot_t new_prot, boolean_t set_max)
1705 vm_map_entry_t current;
1706 vm_map_entry_t entry;
1709 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1712 VM_MAP_RANGE_CHECK(map, start, end);
1714 if (vm_map_lookup_entry(map, start, &entry)) {
1715 vm_map_clip_start(map, entry, start, &count);
1717 entry = entry->next;
1721 * Make a first pass to check for protection violations.
1724 while ((current != &map->header) && (current->start < end)) {
1725 if (current->maptype == VM_MAPTYPE_SUBMAP) {
1727 vm_map_entry_release(count);
1728 return (KERN_INVALID_ARGUMENT);
1730 if ((new_prot & current->max_protection) != new_prot) {
1732 vm_map_entry_release(count);
1733 return (KERN_PROTECTION_FAILURE);
1735 current = current->next;
1739 * Go back and fix up protections. [Note that clipping is not
1740 * necessary the second time.]
1744 while ((current != &map->header) && (current->start < end)) {
1747 vm_map_clip_end(map, current, end, &count);
1749 old_prot = current->protection;
1751 current->protection =
1752 (current->max_protection = new_prot) &
1755 current->protection = new_prot;
1759 * Update physical map if necessary. Worry about copy-on-write
1760 * here -- CHECK THIS XXX
1763 if (current->protection != old_prot) {
1764 #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1767 pmap_protect(map->pmap, current->start,
1769 current->protection & MASK(current));
1773 vm_map_simplify_entry(map, current, &count);
1775 current = current->next;
1779 vm_map_entry_release(count);
1780 return (KERN_SUCCESS);
1784 * This routine traverses a processes map handling the madvise
1785 * system call. Advisories are classified as either those effecting
1786 * the vm_map_entry structure, or those effecting the underlying
1789 * The <value> argument is used for extended madvise calls.
1794 vm_map_madvise(vm_map_t map, vm_offset_t start, vm_offset_t end,
1795 int behav, off_t value)
1797 vm_map_entry_t current, entry;
1803 * Some madvise calls directly modify the vm_map_entry, in which case
1804 * we need to use an exclusive lock on the map and we need to perform
1805 * various clipping operations. Otherwise we only need a read-lock
1809 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1813 case MADV_SEQUENTIAL:
1827 vm_map_lock_read(map);
1830 vm_map_entry_release(count);
1835 * Locate starting entry and clip if necessary.
1838 VM_MAP_RANGE_CHECK(map, start, end);
1840 if (vm_map_lookup_entry(map, start, &entry)) {
1842 vm_map_clip_start(map, entry, start, &count);
1844 entry = entry->next;
1849 * madvise behaviors that are implemented in the vm_map_entry.
1851 * We clip the vm_map_entry so that behavioral changes are
1852 * limited to the specified address range.
1854 for (current = entry;
1855 (current != &map->header) && (current->start < end);
1856 current = current->next
1858 if (current->maptype == VM_MAPTYPE_SUBMAP)
1861 vm_map_clip_end(map, current, end, &count);
1865 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
1867 case MADV_SEQUENTIAL:
1868 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
1871 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
1874 current->eflags |= MAP_ENTRY_NOSYNC;
1877 current->eflags &= ~MAP_ENTRY_NOSYNC;
1880 current->eflags |= MAP_ENTRY_NOCOREDUMP;
1883 current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
1887 * Invalidate the related pmap entries, used
1888 * to flush portions of the real kernel's
1889 * pmap when the caller has removed or
1890 * modified existing mappings in a virtual
1893 pmap_remove(map->pmap,
1894 current->start, current->end);
1898 * Set the page directory page for a map
1899 * governed by a virtual page table. Mark
1900 * the entry as being governed by a virtual
1901 * page table if it is not.
1903 * XXX the page directory page is stored
1904 * in the avail_ssize field if the map_entry.
1906 * XXX the map simplification code does not
1907 * compare this field so weird things may
1908 * happen if you do not apply this function
1909 * to the entire mapping governed by the
1910 * virtual page table.
1912 if (current->maptype != VM_MAPTYPE_VPAGETABLE) {
1916 current->aux.master_pde = value;
1917 pmap_remove(map->pmap,
1918 current->start, current->end);
1924 vm_map_simplify_entry(map, current, &count);
1932 * madvise behaviors that are implemented in the underlying
1935 * Since we don't clip the vm_map_entry, we have to clip
1936 * the vm_object pindex and count.
1938 * NOTE! We currently do not support these functions on
1939 * virtual page tables.
1941 for (current = entry;
1942 (current != &map->header) && (current->start < end);
1943 current = current->next
1945 vm_offset_t useStart;
1947 if (current->maptype != VM_MAPTYPE_NORMAL)
1950 pindex = OFF_TO_IDX(current->offset);
1951 count = atop(current->end - current->start);
1952 useStart = current->start;
1954 if (current->start < start) {
1955 pindex += atop(start - current->start);
1956 count -= atop(start - current->start);
1959 if (current->end > end)
1960 count -= atop(current->end - end);
1965 vm_object_madvise(current->object.vm_object,
1966 pindex, count, behav);
1969 * Try to populate the page table. Mappings governed
1970 * by virtual page tables cannot be pre-populated
1971 * without a lot of work so don't try.
1973 if (behav == MADV_WILLNEED &&
1974 current->maptype != VM_MAPTYPE_VPAGETABLE) {
1975 pmap_object_init_pt(
1978 current->protection,
1979 current->object.vm_object,
1981 (count << PAGE_SHIFT),
1982 MAP_PREFAULT_MADVISE
1986 vm_map_unlock_read(map);
1988 vm_map_entry_release(count);
1994 * Sets the inheritance of the specified address range in the target map.
1995 * Inheritance affects how the map will be shared with child maps at the
1996 * time of vm_map_fork.
1999 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
2000 vm_inherit_t new_inheritance)
2002 vm_map_entry_t entry;
2003 vm_map_entry_t temp_entry;
2006 switch (new_inheritance) {
2007 case VM_INHERIT_NONE:
2008 case VM_INHERIT_COPY:
2009 case VM_INHERIT_SHARE:
2012 return (KERN_INVALID_ARGUMENT);
2015 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
2018 VM_MAP_RANGE_CHECK(map, start, end);
2020 if (vm_map_lookup_entry(map, start, &temp_entry)) {
2022 vm_map_clip_start(map, entry, start, &count);
2024 entry = temp_entry->next;
2026 while ((entry != &map->header) && (entry->start < end)) {
2027 vm_map_clip_end(map, entry, end, &count);
2029 entry->inheritance = new_inheritance;
2031 vm_map_simplify_entry(map, entry, &count);
2033 entry = entry->next;
2036 vm_map_entry_release(count);
2037 return (KERN_SUCCESS);
2041 * Implement the semantics of mlock
2044 vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t real_end,
2045 boolean_t new_pageable)
2047 vm_map_entry_t entry;
2048 vm_map_entry_t start_entry;
2050 int rv = KERN_SUCCESS;
2053 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
2055 VM_MAP_RANGE_CHECK(map, start, real_end);
2058 start_entry = vm_map_clip_range(map, start, end, &count,
2060 if (start_entry == NULL) {
2062 vm_map_entry_release(count);
2063 return (KERN_INVALID_ADDRESS);
2066 if (new_pageable == 0) {
2067 entry = start_entry;
2068 while ((entry != &map->header) && (entry->start < end)) {
2069 vm_offset_t save_start;
2070 vm_offset_t save_end;
2073 * Already user wired or hard wired (trivial cases)
2075 if (entry->eflags & MAP_ENTRY_USER_WIRED) {
2076 entry = entry->next;
2079 if (entry->wired_count != 0) {
2080 entry->wired_count++;
2081 entry->eflags |= MAP_ENTRY_USER_WIRED;
2082 entry = entry->next;
2087 * A new wiring requires instantiation of appropriate
2088 * management structures and the faulting in of the
2091 if (entry->maptype != VM_MAPTYPE_SUBMAP) {
2092 int copyflag = entry->eflags &
2093 MAP_ENTRY_NEEDS_COPY;
2094 if (copyflag && ((entry->protection &
2095 VM_PROT_WRITE) != 0)) {
2096 vm_map_entry_shadow(entry, 0);
2097 } else if (entry->object.vm_object == NULL &&
2099 vm_map_entry_allocate_object(entry);
2102 entry->wired_count++;
2103 entry->eflags |= MAP_ENTRY_USER_WIRED;
2106 * Now fault in the area. Note that vm_fault_wire()
2107 * may release the map lock temporarily, it will be
2108 * relocked on return. The in-transition
2109 * flag protects the entries.
2111 save_start = entry->start;
2112 save_end = entry->end;
2113 rv = vm_fault_wire(map, entry, TRUE);
2115 CLIP_CHECK_BACK(entry, save_start);
2117 KASSERT(entry->wired_count == 1, ("bad wired_count on entry"));
2118 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2119 entry->wired_count = 0;
2120 if (entry->end == save_end)
2122 entry = entry->next;
2123 KASSERT(entry != &map->header, ("bad entry clip during backout"));
2125 end = save_start; /* unwire the rest */
2129 * note that even though the entry might have been
2130 * clipped, the USER_WIRED flag we set prevents
2131 * duplication so we do not have to do a
2134 entry = entry->next;
2138 * If we failed fall through to the unwiring section to
2139 * unwire what we had wired so far. 'end' has already
2146 * start_entry might have been clipped if we unlocked the
2147 * map and blocked. No matter how clipped it has gotten
2148 * there should be a fragment that is on our start boundary.
2150 CLIP_CHECK_BACK(start_entry, start);
2154 * Deal with the unwiring case.
2158 * This is the unwiring case. We must first ensure that the
2159 * range to be unwired is really wired down. We know there
2162 entry = start_entry;
2163 while ((entry != &map->header) && (entry->start < end)) {
2164 if ((entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
2165 rv = KERN_INVALID_ARGUMENT;
2168 KASSERT(entry->wired_count != 0, ("wired count was 0 with USER_WIRED set! %p", entry));
2169 entry = entry->next;
2173 * Now decrement the wiring count for each region. If a region
2174 * becomes completely unwired, unwire its physical pages and
2178 * The map entries are processed in a loop, checking to
2179 * make sure the entry is wired and asserting it has a wired
2180 * count. However, another loop was inserted more-or-less in
2181 * the middle of the unwiring path. This loop picks up the
2182 * "entry" loop variable from the first loop without first
2183 * setting it to start_entry. Naturally, the secound loop
2184 * is never entered and the pages backing the entries are
2185 * never unwired. This can lead to a leak of wired pages.
2187 entry = start_entry;
2188 while ((entry != &map->header) && (entry->start < end)) {
2189 KASSERT(entry->eflags & MAP_ENTRY_USER_WIRED,
2190 ("expected USER_WIRED on entry %p", entry));
2191 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2192 entry->wired_count--;
2193 if (entry->wired_count == 0)
2194 vm_fault_unwire(map, entry);
2195 entry = entry->next;
2199 vm_map_unclip_range(map, start_entry, start, real_end, &count,
2203 vm_map_entry_release(count);
2208 * Sets the pageability of the specified address range in the target map.
2209 * Regions specified as not pageable require locked-down physical
2210 * memory and physical page maps.
2212 * The map must not be locked, but a reference must remain to the map
2213 * throughout the call.
2215 * This function may be called via the zalloc path and must properly
2216 * reserve map entries for kernel_map.
2221 vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t real_end, int kmflags)
2223 vm_map_entry_t entry;
2224 vm_map_entry_t start_entry;
2226 int rv = KERN_SUCCESS;
2229 if (kmflags & KM_KRESERVE)
2230 count = vm_map_entry_kreserve(MAP_RESERVE_COUNT);
2232 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
2234 VM_MAP_RANGE_CHECK(map, start, real_end);
2237 start_entry = vm_map_clip_range(map, start, end, &count,
2239 if (start_entry == NULL) {
2241 rv = KERN_INVALID_ADDRESS;
2244 if ((kmflags & KM_PAGEABLE) == 0) {
2248 * 1. Holding the write lock, we create any shadow or zero-fill
2249 * objects that need to be created. Then we clip each map
2250 * entry to the region to be wired and increment its wiring
2251 * count. We create objects before clipping the map entries
2252 * to avoid object proliferation.
2254 * 2. We downgrade to a read lock, and call vm_fault_wire to
2255 * fault in the pages for any newly wired area (wired_count is
2258 * Downgrading to a read lock for vm_fault_wire avoids a
2259 * possible deadlock with another process that may have faulted
2260 * on one of the pages to be wired (it would mark the page busy,
2261 * blocking us, then in turn block on the map lock that we
2262 * hold). Because of problems in the recursive lock package,
2263 * we cannot upgrade to a write lock in vm_map_lookup. Thus,
2264 * any actions that require the write lock must be done
2265 * beforehand. Because we keep the read lock on the map, the
2266 * copy-on-write status of the entries we modify here cannot
2269 entry = start_entry;
2270 while ((entry != &map->header) && (entry->start < end)) {
2272 * Trivial case if the entry is already wired
2274 if (entry->wired_count) {
2275 entry->wired_count++;
2276 entry = entry->next;
2281 * The entry is being newly wired, we have to setup
2282 * appropriate management structures. A shadow
2283 * object is required for a copy-on-write region,
2284 * or a normal object for a zero-fill region. We
2285 * do not have to do this for entries that point to sub
2286 * maps because we won't hold the lock on the sub map.
2288 if (entry->maptype != VM_MAPTYPE_SUBMAP) {
2289 int copyflag = entry->eflags &
2290 MAP_ENTRY_NEEDS_COPY;
2291 if (copyflag && ((entry->protection &
2292 VM_PROT_WRITE) != 0)) {
2293 vm_map_entry_shadow(entry, 0);
2294 } else if (entry->object.vm_object == NULL &&
2296 vm_map_entry_allocate_object(entry);
2300 entry->wired_count++;
2301 entry = entry->next;
2309 * HACK HACK HACK HACK
2311 * vm_fault_wire() temporarily unlocks the map to avoid
2312 * deadlocks. The in-transition flag from vm_map_clip_range
2313 * call should protect us from changes while the map is
2316 * NOTE: Previously this comment stated that clipping might
2317 * still occur while the entry is unlocked, but from
2318 * what I can tell it actually cannot.
2320 * It is unclear whether the CLIP_CHECK_*() calls
2321 * are still needed but we keep them in anyway.
2323 * HACK HACK HACK HACK
2326 entry = start_entry;
2327 while (entry != &map->header && entry->start < end) {
2329 * If vm_fault_wire fails for any page we need to undo
2330 * what has been done. We decrement the wiring count
2331 * for those pages which have not yet been wired (now)
2332 * and unwire those that have (later).
2334 vm_offset_t save_start = entry->start;
2335 vm_offset_t save_end = entry->end;
2337 if (entry->wired_count == 1)
2338 rv = vm_fault_wire(map, entry, FALSE);
2340 CLIP_CHECK_BACK(entry, save_start);
2342 KASSERT(entry->wired_count == 1, ("wired_count changed unexpectedly"));
2343 entry->wired_count = 0;
2344 if (entry->end == save_end)
2346 entry = entry->next;
2347 KASSERT(entry != &map->header, ("bad entry clip during backout"));
2352 CLIP_CHECK_FWD(entry, save_end);
2353 entry = entry->next;
2357 * If a failure occured undo everything by falling through
2358 * to the unwiring code. 'end' has already been adjusted
2362 kmflags |= KM_PAGEABLE;
2365 * start_entry is still IN_TRANSITION but may have been
2366 * clipped since vm_fault_wire() unlocks and relocks the
2367 * map. No matter how clipped it has gotten there should
2368 * be a fragment that is on our start boundary.
2370 CLIP_CHECK_BACK(start_entry, start);
2373 if (kmflags & KM_PAGEABLE) {
2375 * This is the unwiring case. We must first ensure that the
2376 * range to be unwired is really wired down. We know there
2379 entry = start_entry;
2380 while ((entry != &map->header) && (entry->start < end)) {
2381 if (entry->wired_count == 0) {
2382 rv = KERN_INVALID_ARGUMENT;
2385 entry = entry->next;
2389 * Now decrement the wiring count for each region. If a region
2390 * becomes completely unwired, unwire its physical pages and
2393 entry = start_entry;
2394 while ((entry != &map->header) && (entry->start < end)) {
2395 entry->wired_count--;
2396 if (entry->wired_count == 0)
2397 vm_fault_unwire(map, entry);
2398 entry = entry->next;
2402 vm_map_unclip_range(map, start_entry, start, real_end,
2403 &count, MAP_CLIP_NO_HOLES);
2407 if (kmflags & KM_KRESERVE)
2408 vm_map_entry_krelease(count);
2410 vm_map_entry_release(count);
2415 * Mark a newly allocated address range as wired but do not fault in
2416 * the pages. The caller is expected to load the pages into the object.
2418 * The map must be locked on entry and will remain locked on return.
2419 * No other requirements.
2422 vm_map_set_wired_quick(vm_map_t map, vm_offset_t addr, vm_size_t size,
2425 vm_map_entry_t scan;
2426 vm_map_entry_t entry;
2428 entry = vm_map_clip_range(map, addr, addr + size,
2429 countp, MAP_CLIP_NO_HOLES);
2431 scan != &map->header && scan->start < addr + size;
2432 scan = scan->next) {
2433 KKASSERT(entry->wired_count == 0);
2434 entry->wired_count = 1;
2436 vm_map_unclip_range(map, entry, addr, addr + size,
2437 countp, MAP_CLIP_NO_HOLES);
2441 * Push any dirty cached pages in the address range to their pager.
2442 * If syncio is TRUE, dirty pages are written synchronously.
2443 * If invalidate is TRUE, any cached pages are freed as well.
2445 * This routine is called by sys_msync()
2447 * Returns an error if any part of the specified range is not mapped.
2452 vm_map_clean(vm_map_t map, vm_offset_t start, vm_offset_t end,
2453 boolean_t syncio, boolean_t invalidate)
2455 vm_map_entry_t current;
2456 vm_map_entry_t entry;
2460 vm_ooffset_t offset;
2462 vm_map_lock_read(map);
2463 VM_MAP_RANGE_CHECK(map, start, end);
2464 if (!vm_map_lookup_entry(map, start, &entry)) {
2465 vm_map_unlock_read(map);
2466 return (KERN_INVALID_ADDRESS);
2468 lwkt_gettoken(&map->token);
2471 * Make a first pass to check for holes.
2473 for (current = entry; current->start < end; current = current->next) {
2474 if (current->maptype == VM_MAPTYPE_SUBMAP) {
2475 lwkt_reltoken(&map->token);
2476 vm_map_unlock_read(map);
2477 return (KERN_INVALID_ARGUMENT);
2479 if (end > current->end &&
2480 (current->next == &map->header ||
2481 current->end != current->next->start)) {
2482 lwkt_reltoken(&map->token);
2483 vm_map_unlock_read(map);
2484 return (KERN_INVALID_ADDRESS);
2489 pmap_remove(vm_map_pmap(map), start, end);
2492 * Make a second pass, cleaning/uncaching pages from the indicated
2495 for (current = entry; current->start < end; current = current->next) {
2496 offset = current->offset + (start - current->start);
2497 size = (end <= current->end ? end : current->end) - start;
2498 if (current->maptype == VM_MAPTYPE_SUBMAP) {
2500 vm_map_entry_t tentry;
2503 smap = current->object.sub_map;
2504 vm_map_lock_read(smap);
2505 vm_map_lookup_entry(smap, offset, &tentry);
2506 tsize = tentry->end - offset;
2509 object = tentry->object.vm_object;
2510 offset = tentry->offset + (offset - tentry->start);
2511 vm_map_unlock_read(smap);
2513 object = current->object.vm_object;
2517 vm_object_hold(object);
2520 * Note that there is absolutely no sense in writing out
2521 * anonymous objects, so we track down the vnode object
2523 * We invalidate (remove) all pages from the address space
2524 * anyway, for semantic correctness.
2526 * note: certain anonymous maps, such as MAP_NOSYNC maps,
2527 * may start out with a NULL object.
2529 while (object && (tobj = object->backing_object) != NULL) {
2530 vm_object_hold(tobj);
2531 if (tobj == object->backing_object) {
2532 vm_object_lock_swap();
2533 offset += object->backing_object_offset;
2534 vm_object_drop(object);
2536 if (object->size < OFF_TO_IDX(offset + size))
2537 size = IDX_TO_OFF(object->size) -
2541 vm_object_drop(tobj);
2543 if (object && (object->type == OBJT_VNODE) &&
2544 (current->protection & VM_PROT_WRITE) &&
2545 (object->flags & OBJ_NOMSYNC) == 0) {
2547 * Flush pages if writing is allowed, invalidate them
2548 * if invalidation requested. Pages undergoing I/O
2549 * will be ignored by vm_object_page_remove().
2551 * We cannot lock the vnode and then wait for paging
2552 * to complete without deadlocking against vm_fault.
2553 * Instead we simply call vm_object_page_remove() and
2554 * allow it to block internally on a page-by-page
2555 * basis when it encounters pages undergoing async
2560 /* no chain wait needed for vnode objects */
2561 vm_object_reference_locked(object);
2562 vn_lock(object->handle, LK_EXCLUSIVE | LK_RETRY);
2563 flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
2564 flags |= invalidate ? OBJPC_INVAL : 0;
2567 * When operating on a virtual page table just
2568 * flush the whole object. XXX we probably ought
2571 switch(current->maptype) {
2572 case VM_MAPTYPE_NORMAL:
2573 vm_object_page_clean(object,
2575 OFF_TO_IDX(offset + size + PAGE_MASK),
2578 case VM_MAPTYPE_VPAGETABLE:
2579 vm_object_page_clean(object, 0, 0, flags);
2582 vn_unlock(((struct vnode *)object->handle));
2583 vm_object_deallocate_locked(object);
2585 if (object && invalidate &&
2586 ((object->type == OBJT_VNODE) ||
2587 (object->type == OBJT_DEVICE))) {
2589 (object->type == OBJT_DEVICE) ? FALSE : TRUE;
2590 /* no chain wait needed for vnode/device objects */
2591 vm_object_reference_locked(object);
2592 switch(current->maptype) {
2593 case VM_MAPTYPE_NORMAL:
2594 vm_object_page_remove(object,
2596 OFF_TO_IDX(offset + size + PAGE_MASK),
2599 case VM_MAPTYPE_VPAGETABLE:
2600 vm_object_page_remove(object, 0, 0, clean_only);
2603 vm_object_deallocate_locked(object);
2607 vm_object_drop(object);
2610 lwkt_reltoken(&map->token);
2611 vm_map_unlock_read(map);
2613 return (KERN_SUCCESS);
2617 * Make the region specified by this entry pageable.
2619 * The vm_map must be exclusively locked.
2622 vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
2624 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2625 entry->wired_count = 0;
2626 vm_fault_unwire(map, entry);
2630 * Deallocate the given entry from the target map.
2632 * The vm_map must be exclusively locked.
2635 vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry, int *countp)
2637 vm_map_entry_unlink(map, entry);
2638 map->size -= entry->end - entry->start;
2640 switch(entry->maptype) {
2641 case VM_MAPTYPE_NORMAL:
2642 case VM_MAPTYPE_VPAGETABLE:
2643 vm_object_deallocate(entry->object.vm_object);
2649 vm_map_entry_dispose(map, entry, countp);
2653 * Deallocates the given address range from the target map.
2655 * The vm_map must be exclusively locked.
2658 vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end, int *countp)
2661 vm_map_entry_t entry;
2662 vm_map_entry_t first_entry;
2664 ASSERT_VM_MAP_LOCKED(map);
2665 lwkt_gettoken(&map->token);
2668 * Find the start of the region, and clip it. Set entry to point
2669 * at the first record containing the requested address or, if no
2670 * such record exists, the next record with a greater address. The
2671 * loop will run from this point until a record beyond the termination
2672 * address is encountered.
2674 * map->hint must be adjusted to not point to anything we delete,
2675 * so set it to the entry prior to the one being deleted.
2677 * GGG see other GGG comment.
2679 if (vm_map_lookup_entry(map, start, &first_entry)) {
2680 entry = first_entry;
2681 vm_map_clip_start(map, entry, start, countp);
2682 map->hint = entry->prev; /* possible problem XXX */
2684 map->hint = first_entry; /* possible problem XXX */
2685 entry = first_entry->next;
2689 * If a hole opens up prior to the current first_free then
2690 * adjust first_free. As with map->hint, map->first_free
2691 * cannot be left set to anything we might delete.
2693 if (entry == &map->header) {
2694 map->first_free = &map->header;
2695 } else if (map->first_free->start >= start) {
2696 map->first_free = entry->prev;
2700 * Step through all entries in this region
2702 while ((entry != &map->header) && (entry->start < end)) {
2703 vm_map_entry_t next;
2705 vm_pindex_t offidxstart, offidxend, count;
2708 * If we hit an in-transition entry we have to sleep and
2709 * retry. It's easier (and not really slower) to just retry
2710 * since this case occurs so rarely and the hint is already
2711 * pointing at the right place. We have to reset the
2712 * start offset so as not to accidently delete an entry
2713 * another process just created in vacated space.
2715 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
2716 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2717 start = entry->start;
2718 ++mycpu->gd_cnt.v_intrans_coll;
2719 ++mycpu->gd_cnt.v_intrans_wait;
2720 vm_map_transition_wait(map);
2723 vm_map_clip_end(map, entry, end, countp);
2729 offidxstart = OFF_TO_IDX(entry->offset);
2730 count = OFF_TO_IDX(e - s);
2731 object = entry->object.vm_object;
2734 * Unwire before removing addresses from the pmap; otherwise,
2735 * unwiring will put the entries back in the pmap.
2737 if (entry->wired_count != 0)
2738 vm_map_entry_unwire(map, entry);
2740 offidxend = offidxstart + count;
2742 if (object == &kernel_object) {
2743 vm_object_hold(object);
2744 vm_object_page_remove(object, offidxstart,
2746 vm_object_drop(object);
2747 } else if (object && object->type != OBJT_DEFAULT &&
2748 object->type != OBJT_SWAP) {
2750 * vnode object routines cannot be chain-locked
2752 vm_object_hold(object);
2753 pmap_remove(map->pmap, s, e);
2754 vm_object_drop(object);
2755 } else if (object) {
2756 vm_object_hold(object);
2757 vm_object_chain_acquire(object);
2758 pmap_remove(map->pmap, s, e);
2760 if (object != NULL &&
2761 object->ref_count != 1 &&
2762 (object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) ==
2764 (object->type == OBJT_DEFAULT ||
2765 object->type == OBJT_SWAP)) {
2766 vm_object_collapse(object, NULL);
2767 vm_object_page_remove(object, offidxstart,
2769 if (object->type == OBJT_SWAP) {
2770 swap_pager_freespace(object,
2774 if (offidxend >= object->size &&
2775 offidxstart < object->size) {
2776 object->size = offidxstart;
2779 vm_object_chain_release(object);
2780 vm_object_drop(object);
2784 * Delete the entry (which may delete the object) only after
2785 * removing all pmap entries pointing to its pages.
2786 * (Otherwise, its page frames may be reallocated, and any
2787 * modify bits will be set in the wrong object!)
2789 vm_map_entry_delete(map, entry, countp);
2792 lwkt_reltoken(&map->token);
2793 return (KERN_SUCCESS);
2797 * Remove the given address range from the target map.
2798 * This is the exported form of vm_map_delete.
2803 vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
2808 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
2810 VM_MAP_RANGE_CHECK(map, start, end);
2811 result = vm_map_delete(map, start, end, &count);
2813 vm_map_entry_release(count);
2819 * Assert that the target map allows the specified privilege on the
2820 * entire address region given. The entire region must be allocated.
2822 * The caller must specify whether the vm_map is already locked or not.
2825 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
2826 vm_prot_t protection, boolean_t have_lock)
2828 vm_map_entry_t entry;
2829 vm_map_entry_t tmp_entry;
2832 if (have_lock == FALSE)
2833 vm_map_lock_read(map);
2835 if (!vm_map_lookup_entry(map, start, &tmp_entry)) {
2836 if (have_lock == FALSE)
2837 vm_map_unlock_read(map);
2843 while (start < end) {
2844 if (entry == &map->header) {
2852 if (start < entry->start) {
2857 * Check protection associated with entry.
2860 if ((entry->protection & protection) != protection) {
2864 /* go to next entry */
2867 entry = entry->next;
2869 if (have_lock == FALSE)
2870 vm_map_unlock_read(map);
2875 * If appropriate this function shadows the original object with a new object
2876 * and moves the VM pages from the original object to the new object.
2877 * The original object will also be collapsed, if possible.
2879 * We can only do this for normal memory objects with a single mapping, and
2880 * it only makes sense to do it if there are 2 or more refs on the original
2881 * object. i.e. typically a memory object that has been extended into
2882 * multiple vm_map_entry's with non-overlapping ranges.
2884 * This makes it easier to remove unused pages and keeps object inheritance
2885 * from being a negative impact on memory usage.
2887 * On return the (possibly new) entry->object.vm_object will have an
2888 * additional ref on it for the caller to dispose of (usually by cloning
2889 * the vm_map_entry). The additional ref had to be done in this routine
2890 * to avoid racing a collapse. The object's ONEMAPPING flag will also be
2893 * The vm_map must be locked and its token held.
2896 vm_map_split(vm_map_entry_t entry)
2900 vm_object_t oobject;
2902 oobject = entry->object.vm_object;
2903 vm_object_hold(oobject);
2904 vm_object_chain_wait(oobject);
2905 vm_object_reference_locked(oobject);
2906 vm_object_clear_flag(oobject, OBJ_ONEMAPPING);
2907 vm_object_drop(oobject);
2910 vm_object_t oobject, nobject, bobject;
2913 vm_pindex_t offidxstart, offidxend, idx;
2915 vm_ooffset_t offset;
2918 * Setup. Chain lock the original object throughout the entire
2919 * routine to prevent new page faults from occuring.
2921 * XXX can madvise WILLNEED interfere with us too?
2923 oobject = entry->object.vm_object;
2924 vm_object_hold(oobject);
2925 vm_object_chain_acquire(oobject);
2928 * Original object cannot be split?
2930 if (oobject->handle == NULL || (oobject->type != OBJT_DEFAULT &&
2931 oobject->type != OBJT_SWAP)) {
2932 vm_object_chain_release(oobject);
2933 vm_object_reference_locked(oobject);
2934 vm_object_clear_flag(oobject, OBJ_ONEMAPPING);
2935 vm_object_drop(oobject);
2940 * Collapse original object with its backing store as an
2941 * optimization to reduce chain lengths when possible.
2943 * If ref_count <= 1 there aren't other non-overlapping vm_map_entry's
2944 * for oobject, so there's no point collapsing it.
2946 * Then re-check whether the object can be split.
2948 vm_object_collapse(oobject, NULL);
2950 if (oobject->ref_count <= 1 ||
2951 (oobject->type != OBJT_DEFAULT && oobject->type != OBJT_SWAP) ||
2952 (oobject->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) != OBJ_ONEMAPPING) {
2953 vm_object_chain_release(oobject);
2954 vm_object_reference_locked(oobject);
2955 vm_object_clear_flag(oobject, OBJ_ONEMAPPING);
2956 vm_object_drop(oobject);
2961 * Acquire the chain lock on the backing object.
2963 * Give bobject an additional ref count for when it will be shadowed
2966 if ((bobject = oobject->backing_object) != NULL) {
2967 vm_object_hold(bobject);
2968 vm_object_chain_wait(bobject);
2969 vm_object_reference_locked(bobject);
2970 vm_object_chain_acquire(bobject);
2971 KKASSERT(bobject->backing_object == bobject);
2972 KKASSERT((bobject->flags & OBJ_DEAD) == 0);
2976 * Calculate the object page range and allocate the new object.
2978 offset = entry->offset;
2982 offidxstart = OFF_TO_IDX(offset);
2983 offidxend = offidxstart + OFF_TO_IDX(e - s);
2984 size = offidxend - offidxstart;
2986 switch(oobject->type) {
2988 nobject = default_pager_alloc(NULL, IDX_TO_OFF(size),
2992 nobject = swap_pager_alloc(NULL, IDX_TO_OFF(size),
3001 if (nobject == NULL) {
3003 vm_object_chain_release(bobject);
3004 vm_object_deallocate(bobject);
3005 vm_object_drop(bobject);
3007 vm_object_chain_release(oobject);
3008 vm_object_reference_locked(oobject);
3009 vm_object_clear_flag(oobject, OBJ_ONEMAPPING);
3010 vm_object_drop(oobject);
3015 * The new object will replace entry->object.vm_object so it needs
3016 * a second reference (the caller expects an additional ref).
3018 vm_object_hold(nobject);
3019 vm_object_reference_locked(nobject);
3020 vm_object_chain_acquire(nobject);
3023 * nobject shadows bobject (oobject already shadows bobject).
3026 nobject->backing_object_offset =
3027 oobject->backing_object_offset + IDX_TO_OFF(offidxstart);
3028 nobject->backing_object = bobject;
3029 bobject->shadow_count++;
3030 bobject->generation++;
3031 LIST_INSERT_HEAD(&bobject->shadow_head, nobject, shadow_list);
3032 vm_object_clear_flag(bobject, OBJ_ONEMAPPING); /* XXX? */
3033 vm_object_chain_release(bobject);
3034 vm_object_drop(bobject);
3038 * Move the VM pages from oobject to nobject
3040 for (idx = 0; idx < size; idx++) {
3043 m = vm_page_lookup_busy_wait(oobject, offidxstart + idx,
3049 * We must wait for pending I/O to complete before we can
3052 * We do not have to VM_PROT_NONE the page as mappings should
3053 * not be changed by this operation.
3055 * NOTE: The act of renaming a page updates chaingen for both
3058 vm_page_rename(m, nobject, idx);
3059 /* page automatically made dirty by rename and cache handled */
3060 /* page remains busy */
3063 if (oobject->type == OBJT_SWAP) {
3064 vm_object_pip_add(oobject, 1);
3066 * copy oobject pages into nobject and destroy unneeded
3067 * pages in shadow object.
3069 swap_pager_copy(oobject, nobject, offidxstart, 0);
3070 vm_object_pip_wakeup(oobject);
3074 * Wakeup the pages we played with. No spl protection is needed
3075 * for a simple wakeup.
3077 for (idx = 0; idx < size; idx++) {
3078 m = vm_page_lookup(nobject, idx);
3080 KKASSERT(m->flags & PG_BUSY);
3084 entry->object.vm_object = nobject;
3085 entry->offset = 0LL;
3090 * NOTE: There is no need to remove OBJ_ONEMAPPING from oobject, the
3091 * related pages were moved and are no longer applicable to the
3094 * NOTE: Deallocate oobject (due to its entry->object.vm_object being
3095 * replaced by nobject).
3097 vm_object_chain_release(nobject);
3098 vm_object_drop(nobject);
3100 vm_object_chain_release(bobject);
3101 vm_object_drop(bobject);
3103 vm_object_chain_release(oobject);
3104 /*vm_object_clear_flag(oobject, OBJ_ONEMAPPING);*/
3105 vm_object_deallocate_locked(oobject);
3106 vm_object_drop(oobject);
3111 * Copies the contents of the source entry to the destination
3112 * entry. The entries *must* be aligned properly.
3114 * The vm_maps must be exclusively locked.
3115 * The vm_map's token must be held.
3117 * Because the maps are locked no faults can be in progress during the
3121 vm_map_copy_entry(vm_map_t src_map, vm_map_t dst_map,
3122 vm_map_entry_t src_entry, vm_map_entry_t dst_entry)
3124 vm_object_t src_object;
3126 if (dst_entry->maptype == VM_MAPTYPE_SUBMAP)
3128 if (src_entry->maptype == VM_MAPTYPE_SUBMAP)
3131 if (src_entry->wired_count == 0) {
3133 * If the source entry is marked needs_copy, it is already
3136 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
3137 pmap_protect(src_map->pmap,
3140 src_entry->protection & ~VM_PROT_WRITE);
3144 * Make a copy of the object.
3146 * The object must be locked prior to checking the object type
3147 * and for the call to vm_object_collapse() and vm_map_split().
3148 * We cannot use *_hold() here because the split code will
3149 * probably try to destroy the object. The lock is a pool
3150 * token and doesn't care.
3152 if (src_entry->object.vm_object != NULL) {
3153 vm_map_split(src_entry);
3154 src_object = src_entry->object.vm_object;
3155 dst_entry->object.vm_object = src_object;
3156 src_entry->eflags |= (MAP_ENTRY_COW |
3157 MAP_ENTRY_NEEDS_COPY);
3158 dst_entry->eflags |= (MAP_ENTRY_COW |
3159 MAP_ENTRY_NEEDS_COPY);
3160 dst_entry->offset = src_entry->offset;
3162 dst_entry->object.vm_object = NULL;
3163 dst_entry->offset = 0;
3166 pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
3167 dst_entry->end - dst_entry->start, src_entry->start);
3170 * Of course, wired down pages can't be set copy-on-write.
3171 * Cause wired pages to be copied into the new map by
3172 * simulating faults (the new pages are pageable)
3174 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry);
3180 * Create a new process vmspace structure and vm_map
3181 * based on those of an existing process. The new map
3182 * is based on the old map, according to the inheritance
3183 * values on the regions in that map.
3185 * The source map must not be locked.
3189 vmspace_fork(struct vmspace *vm1)
3191 struct vmspace *vm2;
3192 vm_map_t old_map = &vm1->vm_map;
3194 vm_map_entry_t old_entry;
3195 vm_map_entry_t new_entry;
3199 lwkt_gettoken(&vm1->vm_map.token);
3200 vm_map_lock(old_map);
3203 * XXX Note: upcalls are not copied.
3205 vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
3206 lwkt_gettoken(&vm2->vm_map.token);
3207 bcopy(&vm1->vm_startcopy, &vm2->vm_startcopy,
3208 (caddr_t)&vm1->vm_endcopy - (caddr_t)&vm1->vm_startcopy);
3209 new_map = &vm2->vm_map; /* XXX */
3210 new_map->timestamp = 1;
3212 vm_map_lock(new_map);
3215 old_entry = old_map->header.next;
3216 while (old_entry != &old_map->header) {
3218 old_entry = old_entry->next;
3221 count = vm_map_entry_reserve(count + MAP_RESERVE_COUNT);
3223 old_entry = old_map->header.next;
3224 while (old_entry != &old_map->header) {
3225 if (old_entry->maptype == VM_MAPTYPE_SUBMAP)
3226 panic("vm_map_fork: encountered a submap");
3228 switch (old_entry->inheritance) {
3229 case VM_INHERIT_NONE:
3231 case VM_INHERIT_SHARE:
3233 * Clone the entry, creating the shared object if
3236 if (old_entry->object.vm_object == NULL)
3237 vm_map_entry_allocate_object(old_entry);
3239 if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3241 * Shadow a map_entry which needs a copy,
3242 * replacing its object with a new object
3243 * that points to the old one. Ask the
3244 * shadow code to automatically add an
3245 * additional ref. We can't do it afterwords
3246 * because we might race a collapse. The call
3247 * to vm_map_entry_shadow() will also clear
3250 vm_map_entry_shadow(old_entry, 1);
3253 * We will make a shared copy of the object,
3254 * and must clear OBJ_ONEMAPPING.
3256 * XXX assert that object.vm_object != NULL
3257 * since we allocate it above.
3259 if (old_entry->object.vm_object) {
3260 object = old_entry->object.vm_object;
3261 vm_object_hold(object);
3262 vm_object_chain_wait(object);
3263 vm_object_reference_locked(object);
3264 vm_object_clear_flag(object,
3266 vm_object_drop(object);
3271 * Clone the entry. We've already bumped the ref on
3274 new_entry = vm_map_entry_create(new_map, &count);
3275 *new_entry = *old_entry;
3276 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
3277 new_entry->wired_count = 0;
3280 * Insert the entry into the new map -- we know we're
3281 * inserting at the end of the new map.
3284 vm_map_entry_link(new_map, new_map->header.prev,
3288 * Update the physical map
3290 pmap_copy(new_map->pmap, old_map->pmap,
3292 (old_entry->end - old_entry->start),
3295 case VM_INHERIT_COPY:
3297 * Clone the entry and link into the map.
3299 new_entry = vm_map_entry_create(new_map, &count);
3300 *new_entry = *old_entry;
3301 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
3302 new_entry->wired_count = 0;
3303 new_entry->object.vm_object = NULL;
3304 vm_map_entry_link(new_map, new_map->header.prev,
3306 vm_map_copy_entry(old_map, new_map, old_entry,
3310 old_entry = old_entry->next;
3313 new_map->size = old_map->size;
3314 vm_map_unlock(old_map);
3315 vm_map_unlock(new_map);
3316 vm_map_entry_release(count);
3318 lwkt_reltoken(&vm2->vm_map.token);
3319 lwkt_reltoken(&vm1->vm_map.token);
3325 * Create an auto-grow stack entry
3330 vm_map_stack (vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
3331 int flags, vm_prot_t prot, vm_prot_t max, int cow)
3333 vm_map_entry_t prev_entry;
3334 vm_map_entry_t new_stack_entry;
3335 vm_size_t init_ssize;
3338 vm_offset_t tmpaddr;
3340 cow |= MAP_IS_STACK;
3342 if (max_ssize < sgrowsiz)
3343 init_ssize = max_ssize;
3345 init_ssize = sgrowsiz;
3347 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
3351 * Find space for the mapping
3353 if ((flags & (MAP_FIXED | MAP_TRYFIXED)) == 0) {
3354 if (vm_map_findspace(map, addrbos, max_ssize, 1,
3357 vm_map_entry_release(count);
3358 return (KERN_NO_SPACE);
3363 /* If addr is already mapped, no go */
3364 if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
3366 vm_map_entry_release(count);
3367 return (KERN_NO_SPACE);
3371 /* XXX already handled by kern_mmap() */
3372 /* If we would blow our VMEM resource limit, no go */
3373 if (map->size + init_ssize >
3374 curproc->p_rlimit[RLIMIT_VMEM].rlim_cur) {
3376 vm_map_entry_release(count);
3377 return (KERN_NO_SPACE);
3382 * If we can't accomodate max_ssize in the current mapping,
3383 * no go. However, we need to be aware that subsequent user
3384 * mappings might map into the space we have reserved for
3385 * stack, and currently this space is not protected.
3387 * Hopefully we will at least detect this condition
3388 * when we try to grow the stack.
3390 if ((prev_entry->next != &map->header) &&
3391 (prev_entry->next->start < addrbos + max_ssize)) {
3393 vm_map_entry_release(count);
3394 return (KERN_NO_SPACE);
3398 * We initially map a stack of only init_ssize. We will
3399 * grow as needed later. Since this is to be a grow
3400 * down stack, we map at the top of the range.
3402 * Note: we would normally expect prot and max to be
3403 * VM_PROT_ALL, and cow to be 0. Possibly we should
3404 * eliminate these as input parameters, and just
3405 * pass these values here in the insert call.
3407 rv = vm_map_insert(map, &count,
3408 NULL, 0, addrbos + max_ssize - init_ssize,
3409 addrbos + max_ssize,
3414 /* Now set the avail_ssize amount */
3415 if (rv == KERN_SUCCESS) {
3416 if (prev_entry != &map->header)
3417 vm_map_clip_end(map, prev_entry, addrbos + max_ssize - init_ssize, &count);
3418 new_stack_entry = prev_entry->next;
3419 if (new_stack_entry->end != addrbos + max_ssize ||
3420 new_stack_entry->start != addrbos + max_ssize - init_ssize)
3421 panic ("Bad entry start/end for new stack entry");
3423 new_stack_entry->aux.avail_ssize = max_ssize - init_ssize;
3427 vm_map_entry_release(count);
3432 * Attempts to grow a vm stack entry. Returns KERN_SUCCESS if the
3433 * desired address is already mapped, or if we successfully grow
3434 * the stack. Also returns KERN_SUCCESS if addr is outside the
3435 * stack range (this is strange, but preserves compatibility with
3436 * the grow function in vm_machdep.c).
3441 vm_map_growstack (struct proc *p, vm_offset_t addr)
3443 vm_map_entry_t prev_entry;
3444 vm_map_entry_t stack_entry;
3445 vm_map_entry_t new_stack_entry;
3446 struct vmspace *vm = p->p_vmspace;
3447 vm_map_t map = &vm->vm_map;
3450 int rv = KERN_SUCCESS;
3452 int use_read_lock = 1;
3455 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
3458 vm_map_lock_read(map);
3462 /* If addr is already in the entry range, no need to grow.*/
3463 if (vm_map_lookup_entry(map, addr, &prev_entry))
3466 if ((stack_entry = prev_entry->next) == &map->header)
3468 if (prev_entry == &map->header)
3469 end = stack_entry->start - stack_entry->aux.avail_ssize;
3471 end = prev_entry->end;
3474 * This next test mimics the old grow function in vm_machdep.c.
3475 * It really doesn't quite make sense, but we do it anyway
3476 * for compatibility.
3478 * If not growable stack, return success. This signals the
3479 * caller to proceed as he would normally with normal vm.
3481 if (stack_entry->aux.avail_ssize < 1 ||
3482 addr >= stack_entry->start ||
3483 addr < stack_entry->start - stack_entry->aux.avail_ssize) {
3487 /* Find the minimum grow amount */
3488 grow_amount = roundup (stack_entry->start - addr, PAGE_SIZE);
3489 if (grow_amount > stack_entry->aux.avail_ssize) {
3495 * If there is no longer enough space between the entries
3496 * nogo, and adjust the available space. Note: this
3497 * should only happen if the user has mapped into the
3498 * stack area after the stack was created, and is
3499 * probably an error.
3501 * This also effectively destroys any guard page the user
3502 * might have intended by limiting the stack size.
3504 if (grow_amount > stack_entry->start - end) {
3505 if (use_read_lock && vm_map_lock_upgrade(map)) {
3510 stack_entry->aux.avail_ssize = stack_entry->start - end;
3515 is_procstack = addr >= (vm_offset_t)vm->vm_maxsaddr;
3517 /* If this is the main process stack, see if we're over the
3520 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount >
3521 p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
3526 /* Round up the grow amount modulo SGROWSIZ */
3527 grow_amount = roundup (grow_amount, sgrowsiz);
3528 if (grow_amount > stack_entry->aux.avail_ssize) {
3529 grow_amount = stack_entry->aux.avail_ssize;
3531 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount >
3532 p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
3533 grow_amount = p->p_rlimit[RLIMIT_STACK].rlim_cur -
3537 /* If we would blow our VMEM resource limit, no go */
3538 if (map->size + grow_amount > p->p_rlimit[RLIMIT_VMEM].rlim_cur) {
3543 if (use_read_lock && vm_map_lock_upgrade(map)) {
3549 /* Get the preliminary new entry start value */
3550 addr = stack_entry->start - grow_amount;
3552 /* If this puts us into the previous entry, cut back our growth
3553 * to the available space. Also, see the note above.
3556 stack_entry->aux.avail_ssize = stack_entry->start - end;
3560 rv = vm_map_insert(map, &count,
3561 NULL, 0, addr, stack_entry->start,
3563 VM_PROT_ALL, VM_PROT_ALL,
3566 /* Adjust the available stack space by the amount we grew. */
3567 if (rv == KERN_SUCCESS) {
3568 if (prev_entry != &map->header)
3569 vm_map_clip_end(map, prev_entry, addr, &count);
3570 new_stack_entry = prev_entry->next;
3571 if (new_stack_entry->end != stack_entry->start ||
3572 new_stack_entry->start != addr)
3573 panic ("Bad stack grow start/end in new stack entry");
3575 new_stack_entry->aux.avail_ssize =
3576 stack_entry->aux.avail_ssize -
3577 (new_stack_entry->end - new_stack_entry->start);
3579 vm->vm_ssize += btoc(new_stack_entry->end -
3580 new_stack_entry->start);
3583 if (map->flags & MAP_WIREFUTURE)
3584 vm_map_unwire(map, new_stack_entry->start,
3585 new_stack_entry->end, FALSE);
3590 vm_map_unlock_read(map);
3593 vm_map_entry_release(count);
3598 * Unshare the specified VM space for exec. If other processes are
3599 * mapped to it, then create a new one. The new vmspace is null.
3604 vmspace_exec(struct proc *p, struct vmspace *vmcopy)
3606 struct vmspace *oldvmspace = p->p_vmspace;
3607 struct vmspace *newvmspace;
3608 vm_map_t map = &p->p_vmspace->vm_map;
3611 * If we are execing a resident vmspace we fork it, otherwise
3612 * we create a new vmspace. Note that exitingcnt and upcalls
3613 * are not copied to the new vmspace.
3615 lwkt_gettoken(&oldvmspace->vm_map.token);
3617 newvmspace = vmspace_fork(vmcopy);
3618 lwkt_gettoken(&newvmspace->vm_map.token);
3620 newvmspace = vmspace_alloc(map->min_offset, map->max_offset);
3621 lwkt_gettoken(&newvmspace->vm_map.token);
3622 bcopy(&oldvmspace->vm_startcopy, &newvmspace->vm_startcopy,
3623 (caddr_t)&oldvmspace->vm_endcopy -
3624 (caddr_t)&oldvmspace->vm_startcopy);
3628 * Finish initializing the vmspace before assigning it
3629 * to the process. The vmspace will become the current vmspace
3632 pmap_pinit2(vmspace_pmap(newvmspace));
3633 pmap_replacevm(p, newvmspace, 0);
3634 lwkt_reltoken(&newvmspace->vm_map.token);
3635 lwkt_reltoken(&oldvmspace->vm_map.token);
3636 sysref_put(&oldvmspace->vm_sysref);
3640 * Unshare the specified VM space for forcing COW. This
3641 * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
3643 * The exitingcnt test is not strictly necessary but has been
3644 * included for code sanity (to make the code a bit more deterministic).
3647 vmspace_unshare(struct proc *p)
3649 struct vmspace *oldvmspace = p->p_vmspace;
3650 struct vmspace *newvmspace;
3652 lwkt_gettoken(&oldvmspace->vm_map.token);
3653 if (oldvmspace->vm_sysref.refcnt == 1 && oldvmspace->vm_exitingcnt == 0) {
3654 lwkt_reltoken(&oldvmspace->vm_map.token);
3657 newvmspace = vmspace_fork(oldvmspace);
3658 lwkt_gettoken(&newvmspace->vm_map.token);
3659 pmap_pinit2(vmspace_pmap(newvmspace));
3660 pmap_replacevm(p, newvmspace, 0);
3661 lwkt_reltoken(&newvmspace->vm_map.token);
3662 lwkt_reltoken(&oldvmspace->vm_map.token);
3663 sysref_put(&oldvmspace->vm_sysref);
3667 * vm_map_hint: return the beginning of the best area suitable for
3668 * creating a new mapping with "prot" protection.
3673 vm_map_hint(struct proc *p, vm_offset_t addr, vm_prot_t prot)
3675 struct vmspace *vms = p->p_vmspace;
3677 if (!randomize_mmap) {
3679 * Set a reasonable start point for the hint if it was
3680 * not specified or if it falls within the heap space.
3681 * Hinted mmap()s do not allocate out of the heap space.
3684 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
3685 addr < round_page((vm_offset_t)vms->vm_daddr + maxdsiz))) {
3686 addr = round_page((vm_offset_t)vms->vm_daddr + maxdsiz);
3692 if (addr != 0 && addr >= (vm_offset_t)vms->vm_daddr)
3698 * If executable skip first two pages, otherwise start
3699 * after data + heap region.
3701 if ((prot & VM_PROT_EXECUTE) &&
3702 ((vm_offset_t)vms->vm_daddr >= I386_MAX_EXE_ADDR)) {
3703 addr = (PAGE_SIZE * 2) +
3704 (karc4random() & (I386_MAX_EXE_ADDR / 2 - 1));
3705 return (round_page(addr));
3707 #endif /* __i386__ */
3710 addr = (vm_offset_t)vms->vm_daddr + MAXDSIZ;
3711 addr += karc4random() & (MIN((256 * 1024 * 1024), MAXDSIZ) - 1);
3713 return (round_page(addr));
3717 * Finds the VM object, offset, and protection for a given virtual address
3718 * in the specified map, assuming a page fault of the type specified.
3720 * Leaves the map in question locked for read; return values are guaranteed
3721 * until a vm_map_lookup_done call is performed. Note that the map argument
3722 * is in/out; the returned map must be used in the call to vm_map_lookup_done.
3724 * A handle (out_entry) is returned for use in vm_map_lookup_done, to make
3727 * If a lookup is requested with "write protection" specified, the map may
3728 * be changed to perform virtual copying operations, although the data
3729 * referenced will remain the same.
3734 vm_map_lookup(vm_map_t *var_map, /* IN/OUT */
3736 vm_prot_t fault_typea,
3737 vm_map_entry_t *out_entry, /* OUT */
3738 vm_object_t *object, /* OUT */
3739 vm_pindex_t *pindex, /* OUT */
3740 vm_prot_t *out_prot, /* OUT */
3741 boolean_t *wired) /* OUT */
3743 vm_map_entry_t entry;
3744 vm_map_t map = *var_map;
3746 vm_prot_t fault_type = fault_typea;
3747 int use_read_lock = 1;
3748 int rv = KERN_SUCCESS;
3752 vm_map_lock_read(map);
3757 * If the map has an interesting hint, try it before calling full
3758 * blown lookup routine.
3764 if ((entry == &map->header) ||
3765 (vaddr < entry->start) || (vaddr >= entry->end)) {
3766 vm_map_entry_t tmp_entry;
3769 * Entry was either not a valid hint, or the vaddr was not
3770 * contained in the entry, so do a full lookup.
3772 if (!vm_map_lookup_entry(map, vaddr, &tmp_entry)) {
3773 rv = KERN_INVALID_ADDRESS;
3784 if (entry->maptype == VM_MAPTYPE_SUBMAP) {
3785 vm_map_t old_map = map;
3787 *var_map = map = entry->object.sub_map;
3789 vm_map_unlock_read(old_map);
3791 vm_map_unlock(old_map);
3797 * Check whether this task is allowed to have this page.
3798 * Note the special case for MAP_ENTRY_COW
3799 * pages with an override. This is to implement a forced
3800 * COW for debuggers.
3803 if (fault_type & VM_PROT_OVERRIDE_WRITE)
3804 prot = entry->max_protection;
3806 prot = entry->protection;
3808 fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
3809 if ((fault_type & prot) != fault_type) {
3810 rv = KERN_PROTECTION_FAILURE;
3814 if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
3815 (entry->eflags & MAP_ENTRY_COW) &&
3816 (fault_type & VM_PROT_WRITE) &&
3817 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
3818 rv = KERN_PROTECTION_FAILURE;
3823 * If this page is not pageable, we have to get it for all possible
3826 *wired = (entry->wired_count != 0);
3828 prot = fault_type = entry->protection;
3831 * Virtual page tables may need to update the accessed (A) bit
3832 * in a page table entry. Upgrade the fault to a write fault for
3833 * that case if the map will support it. If the map does not support
3834 * it the page table entry simply will not be updated.
3836 if (entry->maptype == VM_MAPTYPE_VPAGETABLE) {
3837 if (prot & VM_PROT_WRITE)
3838 fault_type |= VM_PROT_WRITE;
3842 * If the entry was copy-on-write, we either ...
3844 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3846 * If we want to write the page, we may as well handle that
3847 * now since we've got the map locked.
3849 * If we don't need to write the page, we just demote the
3850 * permissions allowed.
3853 if (fault_type & VM_PROT_WRITE) {
3855 * Make a new object, and place it in the object
3856 * chain. Note that no new references have appeared
3857 * -- one just moved from the map to the new
3861 if (use_read_lock && vm_map_lock_upgrade(map)) {
3867 vm_map_entry_shadow(entry, 0);
3870 * We're attempting to read a copy-on-write page --
3871 * don't allow writes.
3874 prot &= ~VM_PROT_WRITE;
3879 * Create an object if necessary.
3881 if (entry->object.vm_object == NULL &&
3883 if (use_read_lock && vm_map_lock_upgrade(map)) {
3888 vm_map_entry_allocate_object(entry);
3892 * Return the object/offset from this entry. If the entry was
3893 * copy-on-write or empty, it has been fixed up.
3896 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
3897 *object = entry->object.vm_object;
3900 * Return whether this is the only map sharing this data. On
3901 * success we return with a read lock held on the map. On failure
3902 * we return with the map unlocked.
3906 if (rv == KERN_SUCCESS) {
3907 if (use_read_lock == 0)
3908 vm_map_lock_downgrade(map);
3909 } else if (use_read_lock) {
3910 vm_map_unlock_read(map);
3918 * Releases locks acquired by a vm_map_lookup()
3919 * (according to the handle returned by that lookup).
3921 * No other requirements.
3924 vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry, int count)
3927 * Unlock the main-level map
3929 vm_map_unlock_read(map);
3931 vm_map_entry_release(count);
3934 #include "opt_ddb.h"
3936 #include <sys/kernel.h>
3938 #include <ddb/ddb.h>
3943 DB_SHOW_COMMAND(map, vm_map_print)
3946 /* XXX convert args. */
3947 vm_map_t map = (vm_map_t)addr;
3948 boolean_t full = have_addr;
3950 vm_map_entry_t entry;
3952 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
3954 (void *)map->pmap, map->nentries, map->timestamp);
3957 if (!full && db_indent)
3961 for (entry = map->header.next; entry != &map->header;
3962 entry = entry->next) {
3963 db_iprintf("map entry %p: start=%p, end=%p\n",
3964 (void *)entry, (void *)entry->start, (void *)entry->end);
3967 static char *inheritance_name[4] =
3968 {"share", "copy", "none", "donate_copy"};
3970 db_iprintf(" prot=%x/%x/%s",
3972 entry->max_protection,
3973 inheritance_name[(int)(unsigned char)entry->inheritance]);
3974 if (entry->wired_count != 0)
3975 db_printf(", wired");
3977 if (entry->maptype == VM_MAPTYPE_SUBMAP) {
3978 /* XXX no %qd in kernel. Truncate entry->offset. */
3979 db_printf(", share=%p, offset=0x%lx\n",
3980 (void *)entry->object.sub_map,
3981 (long)entry->offset);
3983 if ((entry->prev == &map->header) ||
3984 (entry->prev->object.sub_map !=
3985 entry->object.sub_map)) {
3987 vm_map_print((db_expr_t)(intptr_t)
3988 entry->object.sub_map,
3993 /* XXX no %qd in kernel. Truncate entry->offset. */
3994 db_printf(", object=%p, offset=0x%lx",
3995 (void *)entry->object.vm_object,
3996 (long)entry->offset);
3997 if (entry->eflags & MAP_ENTRY_COW)
3998 db_printf(", copy (%s)",
3999 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
4003 if ((entry->prev == &map->header) ||
4004 (entry->prev->object.vm_object !=
4005 entry->object.vm_object)) {
4007 vm_object_print((db_expr_t)(intptr_t)
4008 entry->object.vm_object,
4023 DB_SHOW_COMMAND(procvm, procvm)
4028 p = (struct proc *) addr;
4033 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
4034 (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
4035 (void *)vmspace_pmap(p->p_vmspace));
4037 vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);