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 vm_map_unlock_read(map);
2476 return (KERN_INVALID_ARGUMENT);
2478 if (end > current->end &&
2479 (current->next == &map->header ||
2480 current->end != current->next->start)) {
2481 vm_map_unlock_read(map);
2482 return (KERN_INVALID_ADDRESS);
2487 pmap_remove(vm_map_pmap(map), start, end);
2490 * Make a second pass, cleaning/uncaching pages from the indicated
2493 for (current = entry; current->start < end; current = current->next) {
2494 offset = current->offset + (start - current->start);
2495 size = (end <= current->end ? end : current->end) - start;
2496 if (current->maptype == VM_MAPTYPE_SUBMAP) {
2498 vm_map_entry_t tentry;
2501 smap = current->object.sub_map;
2502 vm_map_lock_read(smap);
2503 vm_map_lookup_entry(smap, offset, &tentry);
2504 tsize = tentry->end - offset;
2507 object = tentry->object.vm_object;
2508 offset = tentry->offset + (offset - tentry->start);
2509 vm_map_unlock_read(smap);
2511 object = current->object.vm_object;
2515 vm_object_hold(object);
2518 * Note that there is absolutely no sense in writing out
2519 * anonymous objects, so we track down the vnode object
2521 * We invalidate (remove) all pages from the address space
2522 * anyway, for semantic correctness.
2524 * note: certain anonymous maps, such as MAP_NOSYNC maps,
2525 * may start out with a NULL object.
2527 while (object && (tobj = object->backing_object) != NULL) {
2528 vm_object_hold(tobj);
2529 if (tobj == object->backing_object) {
2530 vm_object_lock_swap();
2531 offset += object->backing_object_offset;
2532 vm_object_drop(object);
2534 if (object->size < OFF_TO_IDX(offset + size))
2535 size = IDX_TO_OFF(object->size) -
2539 vm_object_drop(tobj);
2541 if (object && (object->type == OBJT_VNODE) &&
2542 (current->protection & VM_PROT_WRITE) &&
2543 (object->flags & OBJ_NOMSYNC) == 0) {
2545 * Flush pages if writing is allowed, invalidate them
2546 * if invalidation requested. Pages undergoing I/O
2547 * will be ignored by vm_object_page_remove().
2549 * We cannot lock the vnode and then wait for paging
2550 * to complete without deadlocking against vm_fault.
2551 * Instead we simply call vm_object_page_remove() and
2552 * allow it to block internally on a page-by-page
2553 * basis when it encounters pages undergoing async
2558 /* no chain wait needed for vnode objects */
2559 vm_object_reference_locked(object);
2560 vn_lock(object->handle, LK_EXCLUSIVE | LK_RETRY);
2561 flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
2562 flags |= invalidate ? OBJPC_INVAL : 0;
2565 * When operating on a virtual page table just
2566 * flush the whole object. XXX we probably ought
2569 switch(current->maptype) {
2570 case VM_MAPTYPE_NORMAL:
2571 vm_object_page_clean(object,
2573 OFF_TO_IDX(offset + size + PAGE_MASK),
2576 case VM_MAPTYPE_VPAGETABLE:
2577 vm_object_page_clean(object, 0, 0, flags);
2580 vn_unlock(((struct vnode *)object->handle));
2581 vm_object_deallocate_locked(object);
2583 if (object && invalidate &&
2584 ((object->type == OBJT_VNODE) ||
2585 (object->type == OBJT_DEVICE))) {
2587 (object->type == OBJT_DEVICE) ? FALSE : TRUE;
2588 /* no chain wait needed for vnode/device objects */
2589 vm_object_reference_locked(object);
2590 switch(current->maptype) {
2591 case VM_MAPTYPE_NORMAL:
2592 vm_object_page_remove(object,
2594 OFF_TO_IDX(offset + size + PAGE_MASK),
2597 case VM_MAPTYPE_VPAGETABLE:
2598 vm_object_page_remove(object, 0, 0, clean_only);
2601 vm_object_deallocate_locked(object);
2605 vm_object_drop(object);
2608 lwkt_reltoken(&map->token);
2609 vm_map_unlock_read(map);
2611 return (KERN_SUCCESS);
2615 * Make the region specified by this entry pageable.
2617 * The vm_map must be exclusively locked.
2620 vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
2622 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2623 entry->wired_count = 0;
2624 vm_fault_unwire(map, entry);
2628 * Deallocate the given entry from the target map.
2630 * The vm_map must be exclusively locked.
2633 vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry, int *countp)
2635 vm_map_entry_unlink(map, entry);
2636 map->size -= entry->end - entry->start;
2638 switch(entry->maptype) {
2639 case VM_MAPTYPE_NORMAL:
2640 case VM_MAPTYPE_VPAGETABLE:
2641 vm_object_deallocate(entry->object.vm_object);
2647 vm_map_entry_dispose(map, entry, countp);
2651 * Deallocates the given address range from the target map.
2653 * The vm_map must be exclusively locked.
2656 vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end, int *countp)
2659 vm_map_entry_t entry;
2660 vm_map_entry_t first_entry;
2662 ASSERT_VM_MAP_LOCKED(map);
2663 lwkt_gettoken(&map->token);
2666 * Find the start of the region, and clip it. Set entry to point
2667 * at the first record containing the requested address or, if no
2668 * such record exists, the next record with a greater address. The
2669 * loop will run from this point until a record beyond the termination
2670 * address is encountered.
2672 * map->hint must be adjusted to not point to anything we delete,
2673 * so set it to the entry prior to the one being deleted.
2675 * GGG see other GGG comment.
2677 if (vm_map_lookup_entry(map, start, &first_entry)) {
2678 entry = first_entry;
2679 vm_map_clip_start(map, entry, start, countp);
2680 map->hint = entry->prev; /* possible problem XXX */
2682 map->hint = first_entry; /* possible problem XXX */
2683 entry = first_entry->next;
2687 * If a hole opens up prior to the current first_free then
2688 * adjust first_free. As with map->hint, map->first_free
2689 * cannot be left set to anything we might delete.
2691 if (entry == &map->header) {
2692 map->first_free = &map->header;
2693 } else if (map->first_free->start >= start) {
2694 map->first_free = entry->prev;
2698 * Step through all entries in this region
2700 while ((entry != &map->header) && (entry->start < end)) {
2701 vm_map_entry_t next;
2703 vm_pindex_t offidxstart, offidxend, count;
2706 * If we hit an in-transition entry we have to sleep and
2707 * retry. It's easier (and not really slower) to just retry
2708 * since this case occurs so rarely and the hint is already
2709 * pointing at the right place. We have to reset the
2710 * start offset so as not to accidently delete an entry
2711 * another process just created in vacated space.
2713 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
2714 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2715 start = entry->start;
2716 ++mycpu->gd_cnt.v_intrans_coll;
2717 ++mycpu->gd_cnt.v_intrans_wait;
2718 vm_map_transition_wait(map);
2721 vm_map_clip_end(map, entry, end, countp);
2727 offidxstart = OFF_TO_IDX(entry->offset);
2728 count = OFF_TO_IDX(e - s);
2729 object = entry->object.vm_object;
2732 * Unwire before removing addresses from the pmap; otherwise,
2733 * unwiring will put the entries back in the pmap.
2735 if (entry->wired_count != 0)
2736 vm_map_entry_unwire(map, entry);
2738 offidxend = offidxstart + count;
2740 if (object == &kernel_object) {
2741 vm_object_hold(object);
2742 vm_object_page_remove(object, offidxstart,
2744 vm_object_drop(object);
2745 } else if (object && object->type != OBJT_DEFAULT &&
2746 object->type != OBJT_SWAP) {
2748 * vnode object routines cannot be chain-locked
2750 vm_object_hold(object);
2751 pmap_remove(map->pmap, s, e);
2752 vm_object_drop(object);
2753 } else if (object) {
2754 vm_object_hold(object);
2755 vm_object_chain_acquire(object);
2756 pmap_remove(map->pmap, s, e);
2758 if (object != NULL &&
2759 object->ref_count != 1 &&
2760 (object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) ==
2762 (object->type == OBJT_DEFAULT ||
2763 object->type == OBJT_SWAP)) {
2764 vm_object_collapse(object);
2765 vm_object_page_remove(object, offidxstart,
2767 if (object->type == OBJT_SWAP) {
2768 swap_pager_freespace(object,
2772 if (offidxend >= object->size &&
2773 offidxstart < object->size) {
2774 object->size = offidxstart;
2777 vm_object_chain_release(object);
2778 vm_object_drop(object);
2782 * Delete the entry (which may delete the object) only after
2783 * removing all pmap entries pointing to its pages.
2784 * (Otherwise, its page frames may be reallocated, and any
2785 * modify bits will be set in the wrong object!)
2787 vm_map_entry_delete(map, entry, countp);
2790 lwkt_reltoken(&map->token);
2791 return (KERN_SUCCESS);
2795 * Remove the given address range from the target map.
2796 * This is the exported form of vm_map_delete.
2801 vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
2806 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
2808 VM_MAP_RANGE_CHECK(map, start, end);
2809 result = vm_map_delete(map, start, end, &count);
2811 vm_map_entry_release(count);
2817 * Assert that the target map allows the specified privilege on the
2818 * entire address region given. The entire region must be allocated.
2820 * The caller must specify whether the vm_map is already locked or not.
2823 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
2824 vm_prot_t protection, boolean_t have_lock)
2826 vm_map_entry_t entry;
2827 vm_map_entry_t tmp_entry;
2830 if (have_lock == FALSE)
2831 vm_map_lock_read(map);
2833 if (!vm_map_lookup_entry(map, start, &tmp_entry)) {
2834 if (have_lock == FALSE)
2835 vm_map_unlock_read(map);
2841 while (start < end) {
2842 if (entry == &map->header) {
2850 if (start < entry->start) {
2855 * Check protection associated with entry.
2858 if ((entry->protection & protection) != protection) {
2862 /* go to next entry */
2865 entry = entry->next;
2867 if (have_lock == FALSE)
2868 vm_map_unlock_read(map);
2873 * If appropriate this function shadows the original object with a new object
2874 * and moves the VM pages from the original object to the new object.
2875 * The original object will also be collapsed, if possible.
2877 * We can only do this for normal memory objects with a single mapping, and
2878 * it only makes sense to do it if there are 2 or more refs on the original
2879 * object. i.e. typically a memory object that has been extended into
2880 * multiple vm_map_entry's with non-overlapping ranges.
2882 * This makes it easier to remove unused pages and keeps object inheritance
2883 * from being a negative impact on memory usage.
2885 * On return the (possibly new) entry->object.vm_object will have an
2886 * additional ref on it for the caller to dispose of (usually by cloning
2887 * the vm_map_entry). The additional ref had to be done in this routine
2888 * to avoid racing a collapse. The object's ONEMAPPING flag will also be
2891 * The vm_map must be locked and its token held.
2894 vm_map_split(vm_map_entry_t entry)
2898 vm_object_t oobject;
2900 oobject = entry->object.vm_object;
2901 vm_object_hold(oobject);
2902 vm_object_chain_wait(oobject);
2903 vm_object_reference_locked(oobject);
2904 vm_object_clear_flag(oobject, OBJ_ONEMAPPING);
2905 vm_object_drop(oobject);
2908 vm_object_t oobject, nobject, bobject;
2911 vm_pindex_t offidxstart, offidxend, idx;
2913 vm_ooffset_t offset;
2916 * Setup. Chain lock the original object throughout the entire
2917 * routine to prevent new page faults from occuring.
2919 * XXX can madvise WILLNEED interfere with us too?
2921 oobject = entry->object.vm_object;
2922 vm_object_hold(oobject);
2923 vm_object_chain_acquire(oobject);
2926 * Original object cannot be split?
2928 if (oobject->handle == NULL || (oobject->type != OBJT_DEFAULT &&
2929 oobject->type != OBJT_SWAP)) {
2930 vm_object_chain_release(oobject);
2931 vm_object_reference_locked(oobject);
2932 vm_object_clear_flag(oobject, OBJ_ONEMAPPING);
2933 vm_object_drop(oobject);
2938 * Collapse original object with its backing store as an
2939 * optimization to reduce chain lengths when possible.
2941 * If ref_count <= 1 there aren't other non-overlapping vm_map_entry's
2942 * for oobject, so there's no point collapsing it.
2944 * Then re-check whether the object can be split.
2946 vm_object_collapse(oobject);
2948 if (oobject->ref_count <= 1 ||
2949 (oobject->type != OBJT_DEFAULT && oobject->type != OBJT_SWAP) ||
2950 (oobject->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) != OBJ_ONEMAPPING) {
2951 vm_object_chain_release(oobject);
2952 vm_object_reference_locked(oobject);
2953 vm_object_clear_flag(oobject, OBJ_ONEMAPPING);
2954 vm_object_drop(oobject);
2959 * Acquire the chain lock on the backing object.
2961 * Give bobject an additional ref count for when it will be shadowed
2964 if ((bobject = oobject->backing_object) != NULL) {
2965 vm_object_hold(bobject);
2966 vm_object_chain_wait(bobject);
2967 vm_object_reference_locked(bobject);
2968 vm_object_chain_acquire(bobject);
2969 KKASSERT(bobject->backing_object == bobject);
2970 KKASSERT((bobject->flags & OBJ_DEAD) == 0);
2974 * Calculate the object page range and allocate the new object.
2976 offset = entry->offset;
2980 offidxstart = OFF_TO_IDX(offset);
2981 offidxend = offidxstart + OFF_TO_IDX(e - s);
2982 size = offidxend - offidxstart;
2984 switch(oobject->type) {
2986 nobject = default_pager_alloc(NULL, IDX_TO_OFF(size),
2990 nobject = swap_pager_alloc(NULL, IDX_TO_OFF(size),
2999 if (nobject == NULL) {
3001 vm_object_chain_release(bobject);
3002 vm_object_deallocate(bobject);
3003 vm_object_drop(bobject);
3005 vm_object_chain_release(oobject);
3006 vm_object_reference_locked(oobject);
3007 vm_object_clear_flag(oobject, OBJ_ONEMAPPING);
3008 vm_object_drop(oobject);
3013 * The new object will replace entry->object.vm_object so it needs
3014 * a second reference (the caller expects an additional ref).
3016 vm_object_hold(nobject);
3017 vm_object_reference_locked(nobject);
3018 vm_object_chain_acquire(nobject);
3021 * nobject shadows bobject (oobject already shadows bobject).
3024 nobject->backing_object_offset =
3025 oobject->backing_object_offset + IDX_TO_OFF(offidxstart);
3026 nobject->backing_object = bobject;
3027 bobject->shadow_count++;
3028 bobject->generation++;
3029 LIST_INSERT_HEAD(&bobject->shadow_head, nobject, shadow_list);
3030 vm_object_clear_flag(bobject, OBJ_ONEMAPPING); /* XXX? */
3031 vm_object_chain_release(bobject);
3032 vm_object_drop(bobject);
3036 * Move the VM pages from oobject to nobject
3038 for (idx = 0; idx < size; idx++) {
3041 m = vm_page_lookup_busy_wait(oobject, offidxstart + idx,
3047 * We must wait for pending I/O to complete before we can
3050 * We do not have to VM_PROT_NONE the page as mappings should
3051 * not be changed by this operation.
3053 * NOTE: The act of renaming a page updates chaingen for both
3056 vm_page_rename(m, nobject, idx);
3057 /* page automatically made dirty by rename and cache handled */
3058 /* page remains busy */
3061 if (oobject->type == OBJT_SWAP) {
3062 vm_object_pip_add(oobject, 1);
3064 * copy oobject pages into nobject and destroy unneeded
3065 * pages in shadow object.
3067 swap_pager_copy(oobject, nobject, offidxstart, 0);
3068 vm_object_pip_wakeup(oobject);
3072 * Wakeup the pages we played with. No spl protection is needed
3073 * for a simple wakeup.
3075 for (idx = 0; idx < size; idx++) {
3076 m = vm_page_lookup(nobject, idx);
3078 KKASSERT(m->flags & PG_BUSY);
3082 entry->object.vm_object = nobject;
3083 entry->offset = 0LL;
3088 * NOTE: There is no need to remove OBJ_ONEMAPPING from oobject, the
3089 * related pages were moved and are no longer applicable to the
3092 * NOTE: Deallocate oobject (due to its entry->object.vm_object being
3093 * replaced by nobject).
3095 vm_object_chain_release(nobject);
3096 vm_object_drop(nobject);
3098 vm_object_chain_release(bobject);
3099 vm_object_drop(bobject);
3101 vm_object_chain_release(oobject);
3102 /*vm_object_clear_flag(oobject, OBJ_ONEMAPPING);*/
3103 vm_object_deallocate_locked(oobject);
3104 vm_object_drop(oobject);
3109 * Copies the contents of the source entry to the destination
3110 * entry. The entries *must* be aligned properly.
3112 * The vm_map must be exclusively locked.
3113 * The vm_map's token must be held.
3116 vm_map_copy_entry(vm_map_t src_map, vm_map_t dst_map,
3117 vm_map_entry_t src_entry, vm_map_entry_t dst_entry)
3119 vm_object_t src_object;
3121 if (dst_entry->maptype == VM_MAPTYPE_SUBMAP)
3123 if (src_entry->maptype == VM_MAPTYPE_SUBMAP)
3126 if (src_entry->wired_count == 0) {
3128 * If the source entry is marked needs_copy, it is already
3131 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
3132 pmap_protect(src_map->pmap,
3135 src_entry->protection & ~VM_PROT_WRITE);
3139 * Make a copy of the object.
3141 * The object must be locked prior to checking the object type
3142 * and for the call to vm_object_collapse() and vm_map_split().
3143 * We cannot use *_hold() here because the split code will
3144 * probably try to destroy the object. The lock is a pool
3145 * token and doesn't care.
3147 if (src_entry->object.vm_object != NULL) {
3148 vm_map_split(src_entry);
3149 src_object = src_entry->object.vm_object;
3150 dst_entry->object.vm_object = src_object;
3151 src_entry->eflags |= (MAP_ENTRY_COW |
3152 MAP_ENTRY_NEEDS_COPY);
3153 dst_entry->eflags |= (MAP_ENTRY_COW |
3154 MAP_ENTRY_NEEDS_COPY);
3155 dst_entry->offset = src_entry->offset;
3157 dst_entry->object.vm_object = NULL;
3158 dst_entry->offset = 0;
3161 pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
3162 dst_entry->end - dst_entry->start, src_entry->start);
3165 * Of course, wired down pages can't be set copy-on-write.
3166 * Cause wired pages to be copied into the new map by
3167 * simulating faults (the new pages are pageable)
3169 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry);
3175 * Create a new process vmspace structure and vm_map
3176 * based on those of an existing process. The new map
3177 * is based on the old map, according to the inheritance
3178 * values on the regions in that map.
3180 * The source map must not be locked.
3184 vmspace_fork(struct vmspace *vm1)
3186 struct vmspace *vm2;
3187 vm_map_t old_map = &vm1->vm_map;
3189 vm_map_entry_t old_entry;
3190 vm_map_entry_t new_entry;
3194 lwkt_gettoken(&vm1->vm_map.token);
3195 vm_map_lock(old_map);
3198 * XXX Note: upcalls are not copied.
3200 vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
3201 lwkt_gettoken(&vm2->vm_map.token);
3202 bcopy(&vm1->vm_startcopy, &vm2->vm_startcopy,
3203 (caddr_t)&vm1->vm_endcopy - (caddr_t)&vm1->vm_startcopy);
3204 new_map = &vm2->vm_map; /* XXX */
3205 new_map->timestamp = 1;
3207 vm_map_lock(new_map);
3210 old_entry = old_map->header.next;
3211 while (old_entry != &old_map->header) {
3213 old_entry = old_entry->next;
3216 count = vm_map_entry_reserve(count + MAP_RESERVE_COUNT);
3218 old_entry = old_map->header.next;
3219 while (old_entry != &old_map->header) {
3220 if (old_entry->maptype == VM_MAPTYPE_SUBMAP)
3221 panic("vm_map_fork: encountered a submap");
3223 switch (old_entry->inheritance) {
3224 case VM_INHERIT_NONE:
3226 case VM_INHERIT_SHARE:
3228 * Clone the entry, creating the shared object if
3231 if (old_entry->object.vm_object == NULL)
3232 vm_map_entry_allocate_object(old_entry);
3235 * Shadow a map_entry which needs a copy, replacing
3236 * its object with a new object that points to the
3237 * old one. Ask the shadow code to automatically add
3238 * an additional ref. We can't do it afterwords
3239 * because we might race a collapse
3241 if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3242 vm_map_entry_shadow(old_entry, 1);
3244 if (old_entry->object.vm_object) {
3245 object = old_entry->object.vm_object;
3246 vm_object_hold(object);
3247 vm_object_chain_wait(object);
3248 vm_object_reference_locked(object);
3249 vm_object_drop(object);
3254 * Clone the entry. We've already bumped the ref on
3257 new_entry = vm_map_entry_create(new_map, &count);
3258 *new_entry = *old_entry;
3259 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
3260 new_entry->wired_count = 0;
3263 * Insert the entry into the new map -- we know we're
3264 * inserting at the end of the new map.
3267 vm_map_entry_link(new_map, new_map->header.prev,
3271 * Update the physical map
3273 pmap_copy(new_map->pmap, old_map->pmap,
3275 (old_entry->end - old_entry->start),
3278 case VM_INHERIT_COPY:
3280 * Clone the entry and link into the map.
3282 new_entry = vm_map_entry_create(new_map, &count);
3283 *new_entry = *old_entry;
3284 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
3285 new_entry->wired_count = 0;
3286 new_entry->object.vm_object = NULL;
3287 vm_map_entry_link(new_map, new_map->header.prev,
3289 vm_map_copy_entry(old_map, new_map, old_entry,
3293 old_entry = old_entry->next;
3296 new_map->size = old_map->size;
3297 vm_map_unlock(old_map);
3298 vm_map_unlock(new_map);
3299 vm_map_entry_release(count);
3301 lwkt_reltoken(&vm2->vm_map.token);
3302 lwkt_reltoken(&vm1->vm_map.token);
3308 * Create an auto-grow stack entry
3313 vm_map_stack (vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
3314 int flags, vm_prot_t prot, vm_prot_t max, int cow)
3316 vm_map_entry_t prev_entry;
3317 vm_map_entry_t new_stack_entry;
3318 vm_size_t init_ssize;
3321 vm_offset_t tmpaddr;
3323 cow |= MAP_IS_STACK;
3325 if (max_ssize < sgrowsiz)
3326 init_ssize = max_ssize;
3328 init_ssize = sgrowsiz;
3330 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
3334 * Find space for the mapping
3336 if ((flags & (MAP_FIXED | MAP_TRYFIXED)) == 0) {
3337 if (vm_map_findspace(map, addrbos, max_ssize, 1,
3340 vm_map_entry_release(count);
3341 return (KERN_NO_SPACE);
3346 /* If addr is already mapped, no go */
3347 if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
3349 vm_map_entry_release(count);
3350 return (KERN_NO_SPACE);
3354 /* XXX already handled by kern_mmap() */
3355 /* If we would blow our VMEM resource limit, no go */
3356 if (map->size + init_ssize >
3357 curproc->p_rlimit[RLIMIT_VMEM].rlim_cur) {
3359 vm_map_entry_release(count);
3360 return (KERN_NO_SPACE);
3365 * If we can't accomodate max_ssize in the current mapping,
3366 * no go. However, we need to be aware that subsequent user
3367 * mappings might map into the space we have reserved for
3368 * stack, and currently this space is not protected.
3370 * Hopefully we will at least detect this condition
3371 * when we try to grow the stack.
3373 if ((prev_entry->next != &map->header) &&
3374 (prev_entry->next->start < addrbos + max_ssize)) {
3376 vm_map_entry_release(count);
3377 return (KERN_NO_SPACE);
3381 * We initially map a stack of only init_ssize. We will
3382 * grow as needed later. Since this is to be a grow
3383 * down stack, we map at the top of the range.
3385 * Note: we would normally expect prot and max to be
3386 * VM_PROT_ALL, and cow to be 0. Possibly we should
3387 * eliminate these as input parameters, and just
3388 * pass these values here in the insert call.
3390 rv = vm_map_insert(map, &count,
3391 NULL, 0, addrbos + max_ssize - init_ssize,
3392 addrbos + max_ssize,
3397 /* Now set the avail_ssize amount */
3398 if (rv == KERN_SUCCESS) {
3399 if (prev_entry != &map->header)
3400 vm_map_clip_end(map, prev_entry, addrbos + max_ssize - init_ssize, &count);
3401 new_stack_entry = prev_entry->next;
3402 if (new_stack_entry->end != addrbos + max_ssize ||
3403 new_stack_entry->start != addrbos + max_ssize - init_ssize)
3404 panic ("Bad entry start/end for new stack entry");
3406 new_stack_entry->aux.avail_ssize = max_ssize - init_ssize;
3410 vm_map_entry_release(count);
3415 * Attempts to grow a vm stack entry. Returns KERN_SUCCESS if the
3416 * desired address is already mapped, or if we successfully grow
3417 * the stack. Also returns KERN_SUCCESS if addr is outside the
3418 * stack range (this is strange, but preserves compatibility with
3419 * the grow function in vm_machdep.c).
3424 vm_map_growstack (struct proc *p, vm_offset_t addr)
3426 vm_map_entry_t prev_entry;
3427 vm_map_entry_t stack_entry;
3428 vm_map_entry_t new_stack_entry;
3429 struct vmspace *vm = p->p_vmspace;
3430 vm_map_t map = &vm->vm_map;
3433 int rv = KERN_SUCCESS;
3435 int use_read_lock = 1;
3438 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
3441 vm_map_lock_read(map);
3445 /* If addr is already in the entry range, no need to grow.*/
3446 if (vm_map_lookup_entry(map, addr, &prev_entry))
3449 if ((stack_entry = prev_entry->next) == &map->header)
3451 if (prev_entry == &map->header)
3452 end = stack_entry->start - stack_entry->aux.avail_ssize;
3454 end = prev_entry->end;
3457 * This next test mimics the old grow function in vm_machdep.c.
3458 * It really doesn't quite make sense, but we do it anyway
3459 * for compatibility.
3461 * If not growable stack, return success. This signals the
3462 * caller to proceed as he would normally with normal vm.
3464 if (stack_entry->aux.avail_ssize < 1 ||
3465 addr >= stack_entry->start ||
3466 addr < stack_entry->start - stack_entry->aux.avail_ssize) {
3470 /* Find the minimum grow amount */
3471 grow_amount = roundup (stack_entry->start - addr, PAGE_SIZE);
3472 if (grow_amount > stack_entry->aux.avail_ssize) {
3478 * If there is no longer enough space between the entries
3479 * nogo, and adjust the available space. Note: this
3480 * should only happen if the user has mapped into the
3481 * stack area after the stack was created, and is
3482 * probably an error.
3484 * This also effectively destroys any guard page the user
3485 * might have intended by limiting the stack size.
3487 if (grow_amount > stack_entry->start - end) {
3488 if (use_read_lock && vm_map_lock_upgrade(map)) {
3493 stack_entry->aux.avail_ssize = stack_entry->start - end;
3498 is_procstack = addr >= (vm_offset_t)vm->vm_maxsaddr;
3500 /* If this is the main process stack, see if we're over the
3503 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount >
3504 p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
3509 /* Round up the grow amount modulo SGROWSIZ */
3510 grow_amount = roundup (grow_amount, sgrowsiz);
3511 if (grow_amount > stack_entry->aux.avail_ssize) {
3512 grow_amount = stack_entry->aux.avail_ssize;
3514 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount >
3515 p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
3516 grow_amount = p->p_rlimit[RLIMIT_STACK].rlim_cur -
3520 /* If we would blow our VMEM resource limit, no go */
3521 if (map->size + grow_amount > p->p_rlimit[RLIMIT_VMEM].rlim_cur) {
3526 if (use_read_lock && vm_map_lock_upgrade(map)) {
3532 /* Get the preliminary new entry start value */
3533 addr = stack_entry->start - grow_amount;
3535 /* If this puts us into the previous entry, cut back our growth
3536 * to the available space. Also, see the note above.
3539 stack_entry->aux.avail_ssize = stack_entry->start - end;
3543 rv = vm_map_insert(map, &count,
3544 NULL, 0, addr, stack_entry->start,
3546 VM_PROT_ALL, VM_PROT_ALL,
3549 /* Adjust the available stack space by the amount we grew. */
3550 if (rv == KERN_SUCCESS) {
3551 if (prev_entry != &map->header)
3552 vm_map_clip_end(map, prev_entry, addr, &count);
3553 new_stack_entry = prev_entry->next;
3554 if (new_stack_entry->end != stack_entry->start ||
3555 new_stack_entry->start != addr)
3556 panic ("Bad stack grow start/end in new stack entry");
3558 new_stack_entry->aux.avail_ssize =
3559 stack_entry->aux.avail_ssize -
3560 (new_stack_entry->end - new_stack_entry->start);
3562 vm->vm_ssize += btoc(new_stack_entry->end -
3563 new_stack_entry->start);
3566 if (map->flags & MAP_WIREFUTURE)
3567 vm_map_unwire(map, new_stack_entry->start,
3568 new_stack_entry->end, FALSE);
3573 vm_map_unlock_read(map);
3576 vm_map_entry_release(count);
3581 * Unshare the specified VM space for exec. If other processes are
3582 * mapped to it, then create a new one. The new vmspace is null.
3587 vmspace_exec(struct proc *p, struct vmspace *vmcopy)
3589 struct vmspace *oldvmspace = p->p_vmspace;
3590 struct vmspace *newvmspace;
3591 vm_map_t map = &p->p_vmspace->vm_map;
3594 * If we are execing a resident vmspace we fork it, otherwise
3595 * we create a new vmspace. Note that exitingcnt and upcalls
3596 * are not copied to the new vmspace.
3598 lwkt_gettoken(&oldvmspace->vm_map.token);
3600 newvmspace = vmspace_fork(vmcopy);
3601 lwkt_gettoken(&newvmspace->vm_map.token);
3603 newvmspace = vmspace_alloc(map->min_offset, map->max_offset);
3604 lwkt_gettoken(&newvmspace->vm_map.token);
3605 bcopy(&oldvmspace->vm_startcopy, &newvmspace->vm_startcopy,
3606 (caddr_t)&oldvmspace->vm_endcopy -
3607 (caddr_t)&oldvmspace->vm_startcopy);
3611 * Finish initializing the vmspace before assigning it
3612 * to the process. The vmspace will become the current vmspace
3615 pmap_pinit2(vmspace_pmap(newvmspace));
3616 pmap_replacevm(p, newvmspace, 0);
3617 lwkt_reltoken(&newvmspace->vm_map.token);
3618 lwkt_reltoken(&oldvmspace->vm_map.token);
3619 sysref_put(&oldvmspace->vm_sysref);
3623 * Unshare the specified VM space for forcing COW. This
3624 * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
3626 * The exitingcnt test is not strictly necessary but has been
3627 * included for code sanity (to make the code a bit more deterministic).
3630 vmspace_unshare(struct proc *p)
3632 struct vmspace *oldvmspace = p->p_vmspace;
3633 struct vmspace *newvmspace;
3635 lwkt_gettoken(&oldvmspace->vm_map.token);
3636 if (oldvmspace->vm_sysref.refcnt == 1 && oldvmspace->vm_exitingcnt == 0) {
3637 lwkt_reltoken(&oldvmspace->vm_map.token);
3640 newvmspace = vmspace_fork(oldvmspace);
3641 lwkt_gettoken(&newvmspace->vm_map.token);
3642 pmap_pinit2(vmspace_pmap(newvmspace));
3643 pmap_replacevm(p, newvmspace, 0);
3644 lwkt_reltoken(&newvmspace->vm_map.token);
3645 lwkt_reltoken(&oldvmspace->vm_map.token);
3646 sysref_put(&oldvmspace->vm_sysref);
3650 * vm_map_hint: return the beginning of the best area suitable for
3651 * creating a new mapping with "prot" protection.
3656 vm_map_hint(struct proc *p, vm_offset_t addr, vm_prot_t prot)
3658 struct vmspace *vms = p->p_vmspace;
3660 if (!randomize_mmap) {
3662 * Set a reasonable start point for the hint if it was
3663 * not specified or if it falls within the heap space.
3664 * Hinted mmap()s do not allocate out of the heap space.
3667 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
3668 addr < round_page((vm_offset_t)vms->vm_daddr + maxdsiz))) {
3669 addr = round_page((vm_offset_t)vms->vm_daddr + maxdsiz);
3675 if (addr != 0 && addr >= (vm_offset_t)vms->vm_daddr)
3681 * If executable skip first two pages, otherwise start
3682 * after data + heap region.
3684 if ((prot & VM_PROT_EXECUTE) &&
3685 ((vm_offset_t)vms->vm_daddr >= I386_MAX_EXE_ADDR)) {
3686 addr = (PAGE_SIZE * 2) +
3687 (karc4random() & (I386_MAX_EXE_ADDR / 2 - 1));
3688 return (round_page(addr));
3690 #endif /* __i386__ */
3693 addr = (vm_offset_t)vms->vm_daddr + MAXDSIZ;
3694 addr += karc4random() & (MIN((256 * 1024 * 1024), MAXDSIZ) - 1);
3696 return (round_page(addr));
3700 * Finds the VM object, offset, and protection for a given virtual address
3701 * in the specified map, assuming a page fault of the type specified.
3703 * Leaves the map in question locked for read; return values are guaranteed
3704 * until a vm_map_lookup_done call is performed. Note that the map argument
3705 * is in/out; the returned map must be used in the call to vm_map_lookup_done.
3707 * A handle (out_entry) is returned for use in vm_map_lookup_done, to make
3710 * If a lookup is requested with "write protection" specified, the map may
3711 * be changed to perform virtual copying operations, although the data
3712 * referenced will remain the same.
3717 vm_map_lookup(vm_map_t *var_map, /* IN/OUT */
3719 vm_prot_t fault_typea,
3720 vm_map_entry_t *out_entry, /* OUT */
3721 vm_object_t *object, /* OUT */
3722 vm_pindex_t *pindex, /* OUT */
3723 vm_prot_t *out_prot, /* OUT */
3724 boolean_t *wired) /* OUT */
3726 vm_map_entry_t entry;
3727 vm_map_t map = *var_map;
3729 vm_prot_t fault_type = fault_typea;
3730 int use_read_lock = 1;
3731 int rv = KERN_SUCCESS;
3735 vm_map_lock_read(map);
3740 * If the map has an interesting hint, try it before calling full
3741 * blown lookup routine.
3747 if ((entry == &map->header) ||
3748 (vaddr < entry->start) || (vaddr >= entry->end)) {
3749 vm_map_entry_t tmp_entry;
3752 * Entry was either not a valid hint, or the vaddr was not
3753 * contained in the entry, so do a full lookup.
3755 if (!vm_map_lookup_entry(map, vaddr, &tmp_entry)) {
3756 rv = KERN_INVALID_ADDRESS;
3767 if (entry->maptype == VM_MAPTYPE_SUBMAP) {
3768 vm_map_t old_map = map;
3770 *var_map = map = entry->object.sub_map;
3772 vm_map_unlock_read(old_map);
3774 vm_map_unlock(old_map);
3780 * Check whether this task is allowed to have this page.
3781 * Note the special case for MAP_ENTRY_COW
3782 * pages with an override. This is to implement a forced
3783 * COW for debuggers.
3786 if (fault_type & VM_PROT_OVERRIDE_WRITE)
3787 prot = entry->max_protection;
3789 prot = entry->protection;
3791 fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
3792 if ((fault_type & prot) != fault_type) {
3793 rv = KERN_PROTECTION_FAILURE;
3797 if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
3798 (entry->eflags & MAP_ENTRY_COW) &&
3799 (fault_type & VM_PROT_WRITE) &&
3800 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
3801 rv = KERN_PROTECTION_FAILURE;
3806 * If this page is not pageable, we have to get it for all possible
3809 *wired = (entry->wired_count != 0);
3811 prot = fault_type = entry->protection;
3814 * Virtual page tables may need to update the accessed (A) bit
3815 * in a page table entry. Upgrade the fault to a write fault for
3816 * that case if the map will support it. If the map does not support
3817 * it the page table entry simply will not be updated.
3819 if (entry->maptype == VM_MAPTYPE_VPAGETABLE) {
3820 if (prot & VM_PROT_WRITE)
3821 fault_type |= VM_PROT_WRITE;
3825 * If the entry was copy-on-write, we either ...
3827 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3829 * If we want to write the page, we may as well handle that
3830 * now since we've got the map locked.
3832 * If we don't need to write the page, we just demote the
3833 * permissions allowed.
3836 if (fault_type & VM_PROT_WRITE) {
3838 * Make a new object, and place it in the object
3839 * chain. Note that no new references have appeared
3840 * -- one just moved from the map to the new
3844 if (use_read_lock && vm_map_lock_upgrade(map)) {
3850 vm_map_entry_shadow(entry, 0);
3853 * We're attempting to read a copy-on-write page --
3854 * don't allow writes.
3857 prot &= ~VM_PROT_WRITE;
3862 * Create an object if necessary.
3864 if (entry->object.vm_object == NULL &&
3866 if (use_read_lock && vm_map_lock_upgrade(map)) {
3871 vm_map_entry_allocate_object(entry);
3875 * Return the object/offset from this entry. If the entry was
3876 * copy-on-write or empty, it has been fixed up.
3879 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
3880 *object = entry->object.vm_object;
3883 * Return whether this is the only map sharing this data. On
3884 * success we return with a read lock held on the map. On failure
3885 * we return with the map unlocked.
3889 if (rv == KERN_SUCCESS) {
3890 if (use_read_lock == 0)
3891 vm_map_lock_downgrade(map);
3892 } else if (use_read_lock) {
3893 vm_map_unlock_read(map);
3901 * Releases locks acquired by a vm_map_lookup()
3902 * (according to the handle returned by that lookup).
3904 * No other requirements.
3907 vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry, int count)
3910 * Unlock the main-level map
3912 vm_map_unlock_read(map);
3914 vm_map_entry_release(count);
3917 #include "opt_ddb.h"
3919 #include <sys/kernel.h>
3921 #include <ddb/ddb.h>
3926 DB_SHOW_COMMAND(map, vm_map_print)
3929 /* XXX convert args. */
3930 vm_map_t map = (vm_map_t)addr;
3931 boolean_t full = have_addr;
3933 vm_map_entry_t entry;
3935 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
3937 (void *)map->pmap, map->nentries, map->timestamp);
3940 if (!full && db_indent)
3944 for (entry = map->header.next; entry != &map->header;
3945 entry = entry->next) {
3946 db_iprintf("map entry %p: start=%p, end=%p\n",
3947 (void *)entry, (void *)entry->start, (void *)entry->end);
3950 static char *inheritance_name[4] =
3951 {"share", "copy", "none", "donate_copy"};
3953 db_iprintf(" prot=%x/%x/%s",
3955 entry->max_protection,
3956 inheritance_name[(int)(unsigned char)entry->inheritance]);
3957 if (entry->wired_count != 0)
3958 db_printf(", wired");
3960 if (entry->maptype == VM_MAPTYPE_SUBMAP) {
3961 /* XXX no %qd in kernel. Truncate entry->offset. */
3962 db_printf(", share=%p, offset=0x%lx\n",
3963 (void *)entry->object.sub_map,
3964 (long)entry->offset);
3966 if ((entry->prev == &map->header) ||
3967 (entry->prev->object.sub_map !=
3968 entry->object.sub_map)) {
3970 vm_map_print((db_expr_t)(intptr_t)
3971 entry->object.sub_map,
3976 /* XXX no %qd in kernel. Truncate entry->offset. */
3977 db_printf(", object=%p, offset=0x%lx",
3978 (void *)entry->object.vm_object,
3979 (long)entry->offset);
3980 if (entry->eflags & MAP_ENTRY_COW)
3981 db_printf(", copy (%s)",
3982 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
3986 if ((entry->prev == &map->header) ||
3987 (entry->prev->object.vm_object !=
3988 entry->object.vm_object)) {
3990 vm_object_print((db_expr_t)(intptr_t)
3991 entry->object.vm_object,
4006 DB_SHOW_COMMAND(procvm, procvm)
4011 p = (struct proc *) addr;
4016 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
4017 (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
4018 (void *)vmspace_pmap(p->p_vmspace));
4020 vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);