2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * The Mach Operating System project at Carnegie-Mellon University.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * from: @(#)vm_map.c 8.3 (Berkeley) 1/12/94
39 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
40 * All rights reserved.
42 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
44 * Permission to use, copy, modify and distribute this software and
45 * its documentation is hereby granted, provided that both the copyright
46 * notice and this permission notice appear in all copies of the
47 * software, derivative works or modified versions, and any portions
48 * thereof, and that both notices appear in supporting documentation.
50 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
51 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
52 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
54 * Carnegie Mellon requests users of this software to return to
56 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
57 * School of Computer Science
58 * Carnegie Mellon University
59 * Pittsburgh PA 15213-3890
61 * any improvements or extensions that they make and grant Carnegie the
62 * rights to redistribute these changes.
64 * $FreeBSD: src/sys/vm/vm_map.c,v 1.187.2.19 2003/05/27 00:47:02 alc Exp $
65 * $DragonFly: src/sys/vm/vm_map.c,v 1.56 2007/04/29 18:25:41 dillon Exp $
69 * Virtual memory mapping module.
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/kernel.h>
77 #include <sys/vmmeter.h>
79 #include <sys/vnode.h>
80 #include <sys/resourcevar.h>
83 #include <sys/malloc.h>
86 #include <vm/vm_param.h>
88 #include <vm/vm_map.h>
89 #include <vm/vm_page.h>
90 #include <vm/vm_object.h>
91 #include <vm/vm_pager.h>
92 #include <vm/vm_kern.h>
93 #include <vm/vm_extern.h>
94 #include <vm/swap_pager.h>
95 #include <vm/vm_zone.h>
97 #include <sys/thread2.h>
98 #include <sys/sysref2.h>
101 * Virtual memory maps provide for the mapping, protection,
102 * and sharing of virtual memory objects. In addition,
103 * this module provides for an efficient virtual copy of
104 * memory from one map to another.
106 * Synchronization is required prior to most operations.
108 * Maps consist of an ordered doubly-linked list of simple
109 * entries; a single hint is used to speed up lookups.
111 * Since portions of maps are specified by start/end addresses,
112 * which may not align with existing map entries, all
113 * routines merely "clip" entries to these start/end values.
114 * [That is, an entry is split into two, bordering at a
115 * start or end value.] Note that these clippings may not
116 * always be necessary (as the two resulting entries are then
117 * not changed); however, the clipping is done for convenience.
119 * As mentioned above, virtual copy operations are performed
120 * by copying VM object references from one map to
121 * another, and then marking both regions as copy-on-write.
124 static void vmspace_terminate(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
145 static struct vm_zone mapentzone_store, mapzone_store;
146 static vm_zone_t mapentzone, mapzone;
147 static struct vm_object mapentobj, mapobj;
149 static struct vm_map_entry map_entry_init[MAX_MAPENT];
150 static struct vm_map_entry cpu_map_entry_init[MAXCPU][VMEPERCPU];
151 static struct vm_map map_init[MAX_KMAP];
153 static void vm_map_entry_shadow(vm_map_entry_t entry);
154 static vm_map_entry_t vm_map_entry_create(vm_map_t map, int *);
155 static void vm_map_entry_dispose (vm_map_t map, vm_map_entry_t entry, int *);
156 static void _vm_map_clip_end (vm_map_t, vm_map_entry_t, vm_offset_t, int *);
157 static void _vm_map_clip_start (vm_map_t, vm_map_entry_t, vm_offset_t, int *);
158 static void vm_map_entry_delete (vm_map_t, vm_map_entry_t, int *);
159 static void vm_map_entry_unwire (vm_map_t, vm_map_entry_t);
160 static void vm_map_copy_entry (vm_map_t, vm_map_t, vm_map_entry_t,
162 static void vm_map_split (vm_map_entry_t);
163 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);
168 * Initialize the vm_map module. Must be called before
169 * any other vm_map routines.
171 * Map and entry structures are allocated from the general
172 * purpose memory pool with some exceptions:
174 * - The kernel map and kmem submap are allocated statically.
175 * - Kernel map entries are allocated out of a static pool.
177 * These restrictions are necessary since malloc() uses the
178 * maps and requires map entries.
183 mapzone = &mapzone_store;
184 zbootinit(mapzone, "MAP", sizeof (struct vm_map),
186 mapentzone = &mapentzone_store;
187 zbootinit(mapentzone, "MAP ENTRY", sizeof (struct vm_map_entry),
188 map_entry_init, MAX_MAPENT);
192 * vm_init2 - called prior to any vmspace allocations
197 zinitna(mapentzone, &mapentobj, NULL, 0, 0,
198 ZONE_USE_RESERVE | ZONE_SPECIAL, 1);
199 zinitna(mapzone, &mapobj, NULL, 0, 0, 0, 1);
206 * Red black tree functions
208 static int rb_vm_map_compare(vm_map_entry_t a, vm_map_entry_t b);
209 RB_GENERATE(vm_map_rb_tree, vm_map_entry, rb_entry, rb_vm_map_compare);
211 /* a->start is address, and the only field has to be initialized */
213 rb_vm_map_compare(vm_map_entry_t a, vm_map_entry_t b)
215 if (a->start < b->start)
217 else if (a->start > b->start)
223 * Allocate a vmspace structure, including a vm_map and pmap.
224 * Initialize numerous fields. While the initial allocation is zerod,
225 * subsequence reuse from the objcache leaves elements of the structure
226 * intact (particularly the pmap), so portions must be zerod.
228 * The structure is not considered activated until we call sysref_activate().
231 vmspace_alloc(vm_offset_t min, vm_offset_t max)
235 vm = sysref_alloc(&vmspace_sysref_class);
236 bzero(&vm->vm_startcopy,
237 (char *)&vm->vm_endcopy - (char *)&vm->vm_startcopy);
238 vm_map_init(&vm->vm_map, min, max, NULL);
239 pmap_pinit(vmspace_pmap(vm)); /* (some fields reused) */
240 vm->vm_map.pmap = vmspace_pmap(vm); /* XXX */
242 vm->vm_exitingcnt = 0;
243 cpu_vmspace_alloc(vm);
244 sysref_activate(&vm->vm_sysref);
249 * dtor function - Some elements of the pmap are retained in the
250 * free-cached vmspaces to improve performance. We have to clean them up
251 * here before returning the vmspace to the memory pool.
254 vmspace_dtor(void *obj, void *private)
256 struct vmspace *vm = obj;
258 pmap_puninit(vmspace_pmap(vm));
262 * Called in two cases:
264 * (1) When the last sysref is dropped, but exitingcnt might still be
267 * (2) When there are no sysrefs (i.e. refcnt is negative) left and the
268 * exitingcnt becomes zero
270 * sysref will not scrap the object until we call sysref_put() once more
271 * after the last ref has been dropped.
274 vmspace_terminate(struct vmspace *vm)
279 * If exitingcnt is non-zero we can't get rid of the entire vmspace
280 * yet, but we can scrap user memory.
282 if (vm->vm_exitingcnt) {
284 pmap_remove_pages(vmspace_pmap(vm), VM_MIN_USER_ADDRESS,
285 VM_MAX_USER_ADDRESS);
286 vm_map_remove(&vm->vm_map, VM_MIN_USER_ADDRESS,
287 VM_MAX_USER_ADDRESS);
291 cpu_vmspace_free(vm);
294 * Make sure any SysV shm is freed, it might not have in
299 KKASSERT(vm->vm_upcalls == NULL);
302 * Lock the map, to wait out all other references to it.
303 * Delete all of the mappings and pages they hold, then call
304 * the pmap module to reclaim anything left.
306 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
307 vm_map_lock(&vm->vm_map);
308 vm_map_delete(&vm->vm_map, vm->vm_map.min_offset,
309 vm->vm_map.max_offset, &count);
310 vm_map_unlock(&vm->vm_map);
311 vm_map_entry_release(count);
313 pmap_release(vmspace_pmap(vm));
314 sysref_put(&vm->vm_sysref);
318 * This is called in the wait*() handling code. The vmspace can be terminated
319 * after the last wait is finished using it.
322 vmspace_exitfree(struct proc *p)
329 if (--vm->vm_exitingcnt == 0 && sysref_isinactive(&vm->vm_sysref))
330 vmspace_terminate(vm);
334 * vmspace_swap_count()
336 * Swap useage is determined by taking the proportional swap used by
337 * VM objects backing the VM map. To make up for fractional losses,
338 * if the VM object has any swap use at all the associated map entries
339 * count for at least 1 swap page.
342 vmspace_swap_count(struct vmspace *vmspace)
344 vm_map_t map = &vmspace->vm_map;
350 for (cur = map->header.next; cur != &map->header; cur = cur->next) {
351 switch(cur->maptype) {
352 case VM_MAPTYPE_NORMAL:
353 case VM_MAPTYPE_VPAGETABLE:
354 if ((object = cur->object.vm_object) == NULL)
356 if (object->swblock_count) {
357 n = (cur->end - cur->start) / PAGE_SIZE;
358 count += object->swblock_count *
359 SWAP_META_PAGES * n / object->size + 1;
370 * vmspace_anonymous_count()
372 * Calculate the approximate number of anonymous pages in use by
373 * this vmspace. To make up for fractional losses, we count each
374 * VM object as having at least 1 anonymous page.
377 vmspace_anonymous_count(struct vmspace *vmspace)
379 vm_map_t map = &vmspace->vm_map;
384 for (cur = map->header.next; cur != &map->header; cur = cur->next) {
385 switch(cur->maptype) {
386 case VM_MAPTYPE_NORMAL:
387 case VM_MAPTYPE_VPAGETABLE:
388 if ((object = cur->object.vm_object) == NULL)
390 if (object->type != OBJT_DEFAULT &&
391 object->type != OBJT_SWAP) {
394 count += object->resident_page_count;
409 * Creates and returns a new empty VM map with
410 * the given physical map structure, and having
411 * the given lower and upper address bounds.
414 vm_map_create(vm_map_t result, pmap_t pmap, vm_offset_t min, vm_offset_t max)
417 result = zalloc(mapzone);
418 vm_map_init(result, min, max, pmap);
423 * Initialize an existing vm_map structure
424 * such as that in the vmspace structure.
425 * The pmap is set elsewhere.
428 vm_map_init(struct vm_map *map, vm_offset_t min, vm_offset_t max, pmap_t pmap)
430 map->header.next = map->header.prev = &map->header;
431 RB_INIT(&map->rb_root);
436 map->min_offset = min;
437 map->max_offset = max;
439 map->first_free = &map->header;
440 map->hint = &map->header;
442 lockinit(&map->lock, "thrd_sleep", 0, 0);
446 * Shadow the vm_map_entry's object. This typically needs to be done when
447 * a write fault is taken on an entry which had previously been cloned by
448 * fork(). The shared object (which might be NULL) must become private so
449 * we add a shadow layer above it.
451 * Object allocation for anonymous mappings is defered as long as possible.
452 * When creating a shadow, however, the underlying object must be instantiated
453 * so it can be shared.
455 * If the map segment is governed by a virtual page table then it is
456 * possible to address offsets beyond the mapped area. Just allocate
457 * a maximally sized object for this case.
461 vm_map_entry_shadow(vm_map_entry_t entry)
463 if (entry->maptype == VM_MAPTYPE_VPAGETABLE) {
464 vm_object_shadow(&entry->object.vm_object, &entry->offset,
465 0x7FFFFFFF); /* XXX */
467 vm_object_shadow(&entry->object.vm_object, &entry->offset,
468 atop(entry->end - entry->start));
470 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
474 * Allocate an object for a vm_map_entry.
476 * Object allocation for anonymous mappings is defered as long as possible.
477 * This function is called when we can defer no longer, generally when a map
478 * entry might be split or forked or takes a page fault.
480 * If the map segment is governed by a virtual page table then it is
481 * possible to address offsets beyond the mapped area. Just allocate
482 * a maximally sized object for this case.
485 vm_map_entry_allocate_object(vm_map_entry_t entry)
489 if (entry->maptype == VM_MAPTYPE_VPAGETABLE) {
490 obj = vm_object_allocate(OBJT_DEFAULT, 0x7FFFFFFF); /* XXX */
492 obj = vm_object_allocate(OBJT_DEFAULT,
493 atop(entry->end - entry->start));
495 entry->object.vm_object = obj;
500 * vm_map_entry_reserve_cpu_init:
502 * Set an initial negative count so the first attempt to reserve
503 * space preloads a bunch of vm_map_entry's for this cpu. Also
504 * pre-allocate 2 vm_map_entries which will be needed by zalloc() to
505 * map a new page for vm_map_entry structures. SMP systems are
506 * particularly sensitive.
508 * This routine is called in early boot so we cannot just call
509 * vm_map_entry_reserve().
511 * May be called for a gd other then mycpu, but may only be called
515 vm_map_entry_reserve_cpu_init(globaldata_t gd)
517 vm_map_entry_t entry;
520 gd->gd_vme_avail -= MAP_RESERVE_COUNT * 2;
521 entry = &cpu_map_entry_init[gd->gd_cpuid][0];
522 for (i = 0; i < VMEPERCPU; ++i, ++entry) {
523 entry->next = gd->gd_vme_base;
524 gd->gd_vme_base = entry;
529 * vm_map_entry_reserve:
531 * Reserves vm_map_entry structures so code later on can manipulate
532 * map_entry structures within a locked map without blocking trying
533 * to allocate a new vm_map_entry.
536 vm_map_entry_reserve(int count)
538 struct globaldata *gd = mycpu;
539 vm_map_entry_t entry;
544 * Make sure we have enough structures in gd_vme_base to handle
545 * the reservation request.
547 while (gd->gd_vme_avail < count) {
548 entry = zalloc(mapentzone);
549 entry->next = gd->gd_vme_base;
550 gd->gd_vme_base = entry;
553 gd->gd_vme_avail -= count;
559 * vm_map_entry_release:
561 * Releases previously reserved vm_map_entry structures that were not
562 * used. If we have too much junk in our per-cpu cache clean some of
566 vm_map_entry_release(int count)
568 struct globaldata *gd = mycpu;
569 vm_map_entry_t entry;
572 gd->gd_vme_avail += count;
573 while (gd->gd_vme_avail > MAP_RESERVE_SLOP) {
574 entry = gd->gd_vme_base;
575 KKASSERT(entry != NULL);
576 gd->gd_vme_base = entry->next;
579 zfree(mapentzone, entry);
586 * vm_map_entry_kreserve:
588 * Reserve map entry structures for use in kernel_map itself. These
589 * entries have *ALREADY* been reserved on a per-cpu basis when the map
590 * was inited. This function is used by zalloc() to avoid a recursion
591 * when zalloc() itself needs to allocate additional kernel memory.
593 * This function works like the normal reserve but does not load the
594 * vm_map_entry cache (because that would result in an infinite
595 * recursion). Note that gd_vme_avail may go negative. This is expected.
597 * Any caller of this function must be sure to renormalize after
598 * potentially eating entries to ensure that the reserve supply
602 vm_map_entry_kreserve(int count)
604 struct globaldata *gd = mycpu;
607 gd->gd_vme_avail -= count;
609 KASSERT(gd->gd_vme_base != NULL, ("no reserved entries left, gd_vme_avail = %d\n", gd->gd_vme_avail));
614 * vm_map_entry_krelease:
616 * Release previously reserved map entries for kernel_map. We do not
617 * attempt to clean up like the normal release function as this would
618 * cause an unnecessary (but probably not fatal) deep procedure call.
621 vm_map_entry_krelease(int count)
623 struct globaldata *gd = mycpu;
626 gd->gd_vme_avail += count;
631 * vm_map_entry_create: [ internal use only ]
633 * Allocates a VM map entry for insertion. No entry fields are filled
636 * This routine may be called from an interrupt thread but not a FAST
637 * interrupt. This routine may recurse the map lock.
639 static vm_map_entry_t
640 vm_map_entry_create(vm_map_t map, int *countp)
642 struct globaldata *gd = mycpu;
643 vm_map_entry_t entry;
645 KKASSERT(*countp > 0);
648 entry = gd->gd_vme_base;
649 KASSERT(entry != NULL, ("gd_vme_base NULL! count %d", *countp));
650 gd->gd_vme_base = entry->next;
656 * vm_map_entry_dispose: [ internal use only ]
658 * Dispose of a vm_map_entry that is no longer being referenced. This
659 * function may be called from an interrupt.
662 vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry, int *countp)
664 struct globaldata *gd = mycpu;
666 KKASSERT(map->hint != entry);
667 KKASSERT(map->first_free != entry);
671 entry->next = gd->gd_vme_base;
672 gd->gd_vme_base = entry;
678 * vm_map_entry_{un,}link:
680 * Insert/remove entries from maps.
683 vm_map_entry_link(vm_map_t map,
684 vm_map_entry_t after_where,
685 vm_map_entry_t entry)
688 entry->prev = after_where;
689 entry->next = after_where->next;
690 entry->next->prev = entry;
691 after_where->next = entry;
692 if (vm_map_rb_tree_RB_INSERT(&map->rb_root, entry))
693 panic("vm_map_entry_link: dup addr map %p ent %p", map, entry);
697 vm_map_entry_unlink(vm_map_t map,
698 vm_map_entry_t entry)
703 if (entry->eflags & MAP_ENTRY_IN_TRANSITION)
704 panic("vm_map_entry_unlink: attempt to mess with locked entry! %p", entry);
709 vm_map_rb_tree_RB_REMOVE(&map->rb_root, entry);
714 * vm_map_lookup_entry: [ internal use only ]
716 * Finds the map entry containing (or
717 * immediately preceding) the specified address
718 * in the given map; the entry is returned
719 * in the "entry" parameter. The boolean
720 * result indicates whether the address is
721 * actually contained in the map.
724 vm_map_lookup_entry(vm_map_t map, vm_offset_t address,
725 vm_map_entry_t *entry /* OUT */)
732 * XXX TEMPORARILY DISABLED. For some reason our attempt to revive
733 * the hint code with the red-black lookup meets with system crashes
734 * and lockups. We do not yet know why.
736 * It is possible that the problem is related to the setting
737 * of the hint during map_entry deletion, in the code specified
738 * at the GGG comment later on in this file.
741 * Quickly check the cached hint, there's a good chance of a match.
743 if (map->hint != &map->header) {
745 if (address >= tmp->start && address < tmp->end) {
753 * Locate the record from the top of the tree. 'last' tracks the
754 * closest prior record and is returned if no match is found, which
755 * in binary tree terms means tracking the most recent right-branch
756 * taken. If there is no prior record, &map->header is returned.
759 tmp = RB_ROOT(&map->rb_root);
762 if (address >= tmp->start) {
763 if (address < tmp->end) {
769 tmp = RB_RIGHT(tmp, rb_entry);
771 tmp = RB_LEFT(tmp, rb_entry);
781 * Inserts the given whole VM object into the target
782 * map at the specified address range. The object's
783 * size should match that of the address range.
785 * Requires that the map be locked, and leaves it so. Requires that
786 * sufficient vm_map_entry structures have been reserved and tracks
787 * the use via countp.
789 * If object is non-NULL, ref count must be bumped by caller
790 * prior to making call to account for the new entry.
793 vm_map_insert(vm_map_t map, int *countp,
794 vm_object_t object, vm_ooffset_t offset,
795 vm_offset_t start, vm_offset_t end,
796 vm_maptype_t maptype,
797 vm_prot_t prot, vm_prot_t max,
800 vm_map_entry_t new_entry;
801 vm_map_entry_t prev_entry;
802 vm_map_entry_t temp_entry;
803 vm_eflags_t protoeflags;
806 * Check that the start and end points are not bogus.
809 if ((start < map->min_offset) || (end > map->max_offset) ||
811 return (KERN_INVALID_ADDRESS);
814 * Find the entry prior to the proposed starting address; if it's part
815 * of an existing entry, this range is bogus.
818 if (vm_map_lookup_entry(map, start, &temp_entry))
819 return (KERN_NO_SPACE);
821 prev_entry = temp_entry;
824 * Assert that the next entry doesn't overlap the end point.
827 if ((prev_entry->next != &map->header) &&
828 (prev_entry->next->start < end))
829 return (KERN_NO_SPACE);
833 if (cow & MAP_COPY_ON_WRITE)
834 protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
836 if (cow & MAP_NOFAULT) {
837 protoeflags |= MAP_ENTRY_NOFAULT;
839 KASSERT(object == NULL,
840 ("vm_map_insert: paradoxical MAP_NOFAULT request"));
842 if (cow & MAP_DISABLE_SYNCER)
843 protoeflags |= MAP_ENTRY_NOSYNC;
844 if (cow & MAP_DISABLE_COREDUMP)
845 protoeflags |= MAP_ENTRY_NOCOREDUMP;
846 if (cow & MAP_IS_STACK)
847 protoeflags |= MAP_ENTRY_STACK;
851 * When object is non-NULL, it could be shared with another
852 * process. We have to set or clear OBJ_ONEMAPPING
855 if ((object->ref_count > 1) || (object->shadow_count != 0)) {
856 vm_object_clear_flag(object, OBJ_ONEMAPPING);
859 else if ((prev_entry != &map->header) &&
860 (prev_entry->eflags == protoeflags) &&
861 (prev_entry->end == start) &&
862 (prev_entry->wired_count == 0) &&
863 prev_entry->maptype == maptype &&
864 ((prev_entry->object.vm_object == NULL) ||
865 vm_object_coalesce(prev_entry->object.vm_object,
866 OFF_TO_IDX(prev_entry->offset),
867 (vm_size_t)(prev_entry->end - prev_entry->start),
868 (vm_size_t)(end - prev_entry->end)))) {
870 * We were able to extend the object. Determine if we
871 * can extend the previous map entry to include the
874 if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
875 (prev_entry->protection == prot) &&
876 (prev_entry->max_protection == max)) {
877 map->size += (end - prev_entry->end);
878 prev_entry->end = end;
879 vm_map_simplify_entry(map, prev_entry, countp);
880 return (KERN_SUCCESS);
884 * If we can extend the object but cannot extend the
885 * map entry, we have to create a new map entry. We
886 * must bump the ref count on the extended object to
887 * account for it. object may be NULL.
889 object = prev_entry->object.vm_object;
890 offset = prev_entry->offset +
891 (prev_entry->end - prev_entry->start);
892 vm_object_reference(object);
896 * NOTE: if conditionals fail, object can be NULL here. This occurs
897 * in things like the buffer map where we manage kva but do not manage
905 new_entry = vm_map_entry_create(map, countp);
906 new_entry->start = start;
907 new_entry->end = end;
909 new_entry->maptype = maptype;
910 new_entry->eflags = protoeflags;
911 new_entry->object.vm_object = object;
912 new_entry->offset = offset;
913 new_entry->aux.master_pde = 0;
915 new_entry->inheritance = VM_INHERIT_DEFAULT;
916 new_entry->protection = prot;
917 new_entry->max_protection = max;
918 new_entry->wired_count = 0;
921 * Insert the new entry into the list
924 vm_map_entry_link(map, prev_entry, new_entry);
925 map->size += new_entry->end - new_entry->start;
928 * Update the free space hint
930 if ((map->first_free == prev_entry) &&
931 (prev_entry->end >= new_entry->start)) {
932 map->first_free = new_entry;
937 * Temporarily removed to avoid MAP_STACK panic, due to
938 * MAP_STACK being a huge hack. Will be added back in
939 * when MAP_STACK (and the user stack mapping) is fixed.
942 * It may be possible to simplify the entry
944 vm_map_simplify_entry(map, new_entry, countp);
948 * Try to pre-populate the page table. Mappings governed by virtual
949 * page tables cannot be prepopulated without a lot of work, so
952 if ((cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) &&
953 maptype != VM_MAPTYPE_VPAGETABLE) {
954 pmap_object_init_pt(map->pmap, start, prot,
955 object, OFF_TO_IDX(offset), end - start,
956 cow & MAP_PREFAULT_PARTIAL);
959 return (KERN_SUCCESS);
963 * Find sufficient space for `length' bytes in the given map, starting at
964 * `start'. The map must be locked. Returns 0 on success, 1 on no space.
966 * This function will returned an arbitrarily aligned pointer. If no
967 * particular alignment is required you should pass align as 1. Note that
968 * the map may return PAGE_SIZE aligned pointers if all the lengths used in
969 * the map are a multiple of PAGE_SIZE, even if you pass a smaller align
972 * 'align' should be a power of 2 but is not required to be.
975 vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length,
976 vm_offset_t align, int flags, vm_offset_t *addr)
978 vm_map_entry_t entry, next;
980 vm_offset_t align_mask;
982 if (start < map->min_offset)
983 start = map->min_offset;
984 if (start > map->max_offset)
988 * If the alignment is not a power of 2 we will have to use
989 * a mod/division, set align_mask to a special value.
991 if ((align | (align - 1)) + 1 != (align << 1))
992 align_mask = (vm_offset_t)-1;
994 align_mask = align - 1;
998 * Look for the first possible address; if there's already something
999 * at this address, we have to start after it.
1001 if (start == map->min_offset) {
1002 if ((entry = map->first_free) != &map->header)
1007 if (vm_map_lookup_entry(map, start, &tmp))
1013 * Look through the rest of the map, trying to fit a new region in the
1014 * gap between existing regions, or after the very last region.
1016 for (;; start = (entry = next)->end) {
1018 * Adjust the proposed start by the requested alignment,
1019 * be sure that we didn't wrap the address.
1021 if (align_mask == (vm_offset_t)-1)
1022 end = ((start + align - 1) / align) * align;
1024 end = (start + align_mask) & ~align_mask;
1029 * Find the end of the proposed new region. Be sure we didn't
1030 * go beyond the end of the map, or wrap around the address.
1031 * Then check to see if this is the last entry or if the
1032 * proposed end fits in the gap between this and the next
1035 end = start + length;
1036 if (end > map->max_offset || end < start)
1041 * If the next entry's start address is beyond the desired
1042 * end address we may have found a good entry.
1044 * If the next entry is a stack mapping we do not map into
1045 * the stack's reserved space.
1047 * XXX continue to allow mapping into the stack's reserved
1048 * space if doing a MAP_STACK mapping inside a MAP_STACK
1049 * mapping, for backwards compatibility. But the caller
1050 * really should use MAP_STACK | MAP_TRYFIXED if they
1053 if (next == &map->header)
1055 if (next->start >= end) {
1056 if ((next->eflags & MAP_ENTRY_STACK) == 0)
1058 if (flags & MAP_STACK)
1060 if (next->start - next->aux.avail_ssize >= end)
1065 if (map == &kernel_map) {
1067 if ((ksize = round_page(start + length)) > kernel_vm_end) {
1068 pmap_growkernel(ksize);
1077 * vm_map_find finds an unallocated region in the target address
1078 * map with the given length. The search is defined to be
1079 * first-fit from the specified address; the region found is
1080 * returned in the same parameter.
1082 * If object is non-NULL, ref count must be bumped by caller
1083 * prior to making call to account for the new entry.
1086 vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1087 vm_offset_t *addr, vm_size_t length,
1089 vm_maptype_t maptype,
1090 vm_prot_t prot, vm_prot_t max,
1099 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1102 if (vm_map_findspace(map, start, length, 1, 0, addr)) {
1104 vm_map_entry_release(count);
1105 return (KERN_NO_SPACE);
1109 result = vm_map_insert(map, &count, object, offset,
1110 start, start + length,
1115 vm_map_entry_release(count);
1121 * vm_map_simplify_entry:
1123 * Simplify the given map entry by merging with either neighbor. This
1124 * routine also has the ability to merge with both neighbors.
1126 * The map must be locked.
1128 * This routine guarentees that the passed entry remains valid (though
1129 * possibly extended). When merging, this routine may delete one or
1130 * both neighbors. No action is taken on entries which have their
1131 * in-transition flag set.
1134 vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry, int *countp)
1136 vm_map_entry_t next, prev;
1137 vm_size_t prevsize, esize;
1139 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1140 ++mycpu->gd_cnt.v_intrans_coll;
1144 if (entry->maptype == VM_MAPTYPE_SUBMAP)
1148 if (prev != &map->header) {
1149 prevsize = prev->end - prev->start;
1150 if ( (prev->end == entry->start) &&
1151 (prev->maptype == entry->maptype) &&
1152 (prev->object.vm_object == entry->object.vm_object) &&
1153 (!prev->object.vm_object ||
1154 (prev->offset + prevsize == entry->offset)) &&
1155 (prev->eflags == entry->eflags) &&
1156 (prev->protection == entry->protection) &&
1157 (prev->max_protection == entry->max_protection) &&
1158 (prev->inheritance == entry->inheritance) &&
1159 (prev->wired_count == entry->wired_count)) {
1160 if (map->first_free == prev)
1161 map->first_free = entry;
1162 if (map->hint == prev)
1164 vm_map_entry_unlink(map, prev);
1165 entry->start = prev->start;
1166 entry->offset = prev->offset;
1167 if (prev->object.vm_object)
1168 vm_object_deallocate(prev->object.vm_object);
1169 vm_map_entry_dispose(map, prev, countp);
1174 if (next != &map->header) {
1175 esize = entry->end - entry->start;
1176 if ((entry->end == next->start) &&
1177 (next->maptype == entry->maptype) &&
1178 (next->object.vm_object == entry->object.vm_object) &&
1179 (!entry->object.vm_object ||
1180 (entry->offset + esize == next->offset)) &&
1181 (next->eflags == entry->eflags) &&
1182 (next->protection == entry->protection) &&
1183 (next->max_protection == entry->max_protection) &&
1184 (next->inheritance == entry->inheritance) &&
1185 (next->wired_count == entry->wired_count)) {
1186 if (map->first_free == next)
1187 map->first_free = entry;
1188 if (map->hint == next)
1190 vm_map_entry_unlink(map, next);
1191 entry->end = next->end;
1192 if (next->object.vm_object)
1193 vm_object_deallocate(next->object.vm_object);
1194 vm_map_entry_dispose(map, next, countp);
1199 * vm_map_clip_start: [ internal use only ]
1201 * Asserts that the given entry begins at or after
1202 * the specified address; if necessary,
1203 * it splits the entry into two.
1205 #define vm_map_clip_start(map, entry, startaddr, countp) \
1207 if (startaddr > entry->start) \
1208 _vm_map_clip_start(map, entry, startaddr, countp); \
1212 * This routine is called only when it is known that
1213 * the entry must be split.
1216 _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start, int *countp)
1218 vm_map_entry_t new_entry;
1221 * Split off the front portion -- note that we must insert the new
1222 * entry BEFORE this one, so that this entry has the specified
1226 vm_map_simplify_entry(map, entry, countp);
1229 * If there is no object backing this entry, we might as well create
1230 * one now. If we defer it, an object can get created after the map
1231 * is clipped, and individual objects will be created for the split-up
1232 * map. This is a bit of a hack, but is also about the best place to
1233 * put this improvement.
1235 if (entry->object.vm_object == NULL && !map->system_map) {
1236 vm_map_entry_allocate_object(entry);
1239 new_entry = vm_map_entry_create(map, countp);
1240 *new_entry = *entry;
1242 new_entry->end = start;
1243 entry->offset += (start - entry->start);
1244 entry->start = start;
1246 vm_map_entry_link(map, entry->prev, new_entry);
1248 switch(entry->maptype) {
1249 case VM_MAPTYPE_NORMAL:
1250 case VM_MAPTYPE_VPAGETABLE:
1251 vm_object_reference(new_entry->object.vm_object);
1259 * vm_map_clip_end: [ internal use only ]
1261 * Asserts that the given entry ends at or before
1262 * the specified address; if necessary,
1263 * it splits the entry into two.
1266 #define vm_map_clip_end(map, entry, endaddr, countp) \
1268 if (endaddr < entry->end) \
1269 _vm_map_clip_end(map, entry, endaddr, countp); \
1273 * This routine is called only when it is known that
1274 * the entry must be split.
1277 _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end, int *countp)
1279 vm_map_entry_t new_entry;
1282 * If there is no object backing this entry, we might as well create
1283 * one now. If we defer it, an object can get created after the map
1284 * is clipped, and individual objects will be created for the split-up
1285 * map. This is a bit of a hack, but is also about the best place to
1286 * put this improvement.
1289 if (entry->object.vm_object == NULL && !map->system_map) {
1290 vm_map_entry_allocate_object(entry);
1294 * Create a new entry and insert it AFTER the specified entry
1297 new_entry = vm_map_entry_create(map, countp);
1298 *new_entry = *entry;
1300 new_entry->start = entry->end = end;
1301 new_entry->offset += (end - entry->start);
1303 vm_map_entry_link(map, entry, new_entry);
1305 switch(entry->maptype) {
1306 case VM_MAPTYPE_NORMAL:
1307 case VM_MAPTYPE_VPAGETABLE:
1308 vm_object_reference(new_entry->object.vm_object);
1316 * VM_MAP_RANGE_CHECK: [ internal use only ]
1318 * Asserts that the starting and ending region
1319 * addresses fall within the valid range of the map.
1321 #define VM_MAP_RANGE_CHECK(map, start, end) \
1323 if (start < vm_map_min(map)) \
1324 start = vm_map_min(map); \
1325 if (end > vm_map_max(map)) \
1326 end = vm_map_max(map); \
1332 * vm_map_transition_wait: [ kernel use only ]
1334 * Used to block when an in-transition collison occurs. The map
1335 * is unlocked for the sleep and relocked before the return.
1339 vm_map_transition_wait(vm_map_t map)
1342 tsleep(map, 0, "vment", 0);
1350 * When we do blocking operations with the map lock held it is
1351 * possible that a clip might have occured on our in-transit entry,
1352 * requiring an adjustment to the entry in our loop. These macros
1353 * help the pageable and clip_range code deal with the case. The
1354 * conditional costs virtually nothing if no clipping has occured.
1357 #define CLIP_CHECK_BACK(entry, save_start) \
1359 while (entry->start != save_start) { \
1360 entry = entry->prev; \
1361 KASSERT(entry != &map->header, ("bad entry clip")); \
1365 #define CLIP_CHECK_FWD(entry, save_end) \
1367 while (entry->end != save_end) { \
1368 entry = entry->next; \
1369 KASSERT(entry != &map->header, ("bad entry clip")); \
1375 * vm_map_clip_range: [ kernel use only ]
1377 * Clip the specified range and return the base entry. The
1378 * range may cover several entries starting at the returned base
1379 * and the first and last entry in the covering sequence will be
1380 * properly clipped to the requested start and end address.
1382 * If no holes are allowed you should pass the MAP_CLIP_NO_HOLES
1385 * The MAP_ENTRY_IN_TRANSITION flag will be set for the entries
1386 * covered by the requested range.
1388 * The map must be exclusively locked on entry and will remain locked
1389 * on return. If no range exists or the range contains holes and you
1390 * specified that no holes were allowed, NULL will be returned. This
1391 * routine may temporarily unlock the map in order avoid a deadlock when
1396 vm_map_clip_range(vm_map_t map, vm_offset_t start, vm_offset_t end,
1397 int *countp, int flags)
1399 vm_map_entry_t start_entry;
1400 vm_map_entry_t entry;
1403 * Locate the entry and effect initial clipping. The in-transition
1404 * case does not occur very often so do not try to optimize it.
1407 if (vm_map_lookup_entry(map, start, &start_entry) == FALSE)
1409 entry = start_entry;
1410 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1411 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1412 ++mycpu->gd_cnt.v_intrans_coll;
1413 ++mycpu->gd_cnt.v_intrans_wait;
1414 vm_map_transition_wait(map);
1416 * entry and/or start_entry may have been clipped while
1417 * we slept, or may have gone away entirely. We have
1418 * to restart from the lookup.
1423 * Since we hold an exclusive map lock we do not have to restart
1424 * after clipping, even though clipping may block in zalloc.
1426 vm_map_clip_start(map, entry, start, countp);
1427 vm_map_clip_end(map, entry, end, countp);
1428 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
1431 * Scan entries covered by the range. When working on the next
1432 * entry a restart need only re-loop on the current entry which
1433 * we have already locked, since 'next' may have changed. Also,
1434 * even though entry is safe, it may have been clipped so we
1435 * have to iterate forwards through the clip after sleeping.
1437 while (entry->next != &map->header && entry->next->start < end) {
1438 vm_map_entry_t next = entry->next;
1440 if (flags & MAP_CLIP_NO_HOLES) {
1441 if (next->start > entry->end) {
1442 vm_map_unclip_range(map, start_entry,
1443 start, entry->end, countp, flags);
1448 if (next->eflags & MAP_ENTRY_IN_TRANSITION) {
1449 vm_offset_t save_end = entry->end;
1450 next->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1451 ++mycpu->gd_cnt.v_intrans_coll;
1452 ++mycpu->gd_cnt.v_intrans_wait;
1453 vm_map_transition_wait(map);
1456 * clips might have occured while we blocked.
1458 CLIP_CHECK_FWD(entry, save_end);
1459 CLIP_CHECK_BACK(start_entry, start);
1463 * No restart necessary even though clip_end may block, we
1464 * are holding the map lock.
1466 vm_map_clip_end(map, next, end, countp);
1467 next->eflags |= MAP_ENTRY_IN_TRANSITION;
1470 if (flags & MAP_CLIP_NO_HOLES) {
1471 if (entry->end != end) {
1472 vm_map_unclip_range(map, start_entry,
1473 start, entry->end, countp, flags);
1477 return(start_entry);
1481 * vm_map_unclip_range: [ kernel use only ]
1483 * Undo the effect of vm_map_clip_range(). You should pass the same
1484 * flags and the same range that you passed to vm_map_clip_range().
1485 * This code will clear the in-transition flag on the entries and
1486 * wake up anyone waiting. This code will also simplify the sequence
1487 * and attempt to merge it with entries before and after the sequence.
1489 * The map must be locked on entry and will remain locked on return.
1491 * Note that you should also pass the start_entry returned by
1492 * vm_map_clip_range(). However, if you block between the two calls
1493 * with the map unlocked please be aware that the start_entry may
1494 * have been clipped and you may need to scan it backwards to find
1495 * the entry corresponding with the original start address. You are
1496 * responsible for this, vm_map_unclip_range() expects the correct
1497 * start_entry to be passed to it and will KASSERT otherwise.
1501 vm_map_unclip_range(
1503 vm_map_entry_t start_entry,
1509 vm_map_entry_t entry;
1511 entry = start_entry;
1513 KASSERT(entry->start == start, ("unclip_range: illegal base entry"));
1514 while (entry != &map->header && entry->start < end) {
1515 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION, ("in-transition flag not set during unclip on: %p", entry));
1516 KASSERT(entry->end <= end, ("unclip_range: tail wasn't clipped"));
1517 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
1518 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
1519 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
1522 entry = entry->next;
1526 * Simplification does not block so there is no restart case.
1528 entry = start_entry;
1529 while (entry != &map->header && entry->start < end) {
1530 vm_map_simplify_entry(map, entry, countp);
1531 entry = entry->next;
1536 * vm_map_submap: [ kernel use only ]
1538 * Mark the given range as handled by a subordinate map.
1540 * This range must have been created with vm_map_find,
1541 * and no other operations may have been performed on this
1542 * range prior to calling vm_map_submap.
1544 * Only a limited number of operations can be performed
1545 * within this rage after calling vm_map_submap:
1547 * [Don't try vm_map_copy!]
1549 * To remove a submapping, one must first remove the
1550 * range from the superior map, and then destroy the
1551 * submap (if desired). [Better yet, don't try it.]
1554 vm_map_submap(vm_map_t map, vm_offset_t start, vm_offset_t end, vm_map_t submap)
1556 vm_map_entry_t entry;
1557 int result = KERN_INVALID_ARGUMENT;
1560 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1563 VM_MAP_RANGE_CHECK(map, start, end);
1565 if (vm_map_lookup_entry(map, start, &entry)) {
1566 vm_map_clip_start(map, entry, start, &count);
1568 entry = entry->next;
1571 vm_map_clip_end(map, entry, end, &count);
1573 if ((entry->start == start) && (entry->end == end) &&
1574 ((entry->eflags & MAP_ENTRY_COW) == 0) &&
1575 (entry->object.vm_object == NULL)) {
1576 entry->object.sub_map = submap;
1577 entry->maptype = VM_MAPTYPE_SUBMAP;
1578 result = KERN_SUCCESS;
1581 vm_map_entry_release(count);
1589 * Sets the protection of the specified address region in the target map.
1590 * If "set_max" is specified, the maximum protection is to be set;
1591 * otherwise, only the current protection is affected.
1593 * The protection is not applicable to submaps, but is applicable to normal
1594 * maps and maps governed by virtual page tables. For example, when operating
1595 * on a virtual page table our protection basically controls how COW occurs
1596 * on the backing object, whereas the virtual page table abstraction itself
1597 * is an abstraction for userland.
1600 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
1601 vm_prot_t new_prot, boolean_t set_max)
1603 vm_map_entry_t current;
1604 vm_map_entry_t entry;
1607 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1610 VM_MAP_RANGE_CHECK(map, start, end);
1612 if (vm_map_lookup_entry(map, start, &entry)) {
1613 vm_map_clip_start(map, entry, start, &count);
1615 entry = entry->next;
1619 * Make a first pass to check for protection violations.
1622 while ((current != &map->header) && (current->start < end)) {
1623 if (current->maptype == VM_MAPTYPE_SUBMAP) {
1625 vm_map_entry_release(count);
1626 return (KERN_INVALID_ARGUMENT);
1628 if ((new_prot & current->max_protection) != new_prot) {
1630 vm_map_entry_release(count);
1631 return (KERN_PROTECTION_FAILURE);
1633 current = current->next;
1637 * Go back and fix up protections. [Note that clipping is not
1638 * necessary the second time.]
1642 while ((current != &map->header) && (current->start < end)) {
1645 vm_map_clip_end(map, current, end, &count);
1647 old_prot = current->protection;
1649 current->protection =
1650 (current->max_protection = new_prot) &
1653 current->protection = new_prot;
1657 * Update physical map if necessary. Worry about copy-on-write
1658 * here -- CHECK THIS XXX
1661 if (current->protection != old_prot) {
1662 #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1665 pmap_protect(map->pmap, current->start,
1667 current->protection & MASK(current));
1671 vm_map_simplify_entry(map, current, &count);
1673 current = current->next;
1677 vm_map_entry_release(count);
1678 return (KERN_SUCCESS);
1684 * This routine traverses a processes map handling the madvise
1685 * system call. Advisories are classified as either those effecting
1686 * the vm_map_entry structure, or those effecting the underlying
1689 * The <value> argument is used for extended madvise calls.
1692 vm_map_madvise(vm_map_t map, vm_offset_t start, vm_offset_t end,
1693 int behav, off_t value)
1695 vm_map_entry_t current, entry;
1701 * Some madvise calls directly modify the vm_map_entry, in which case
1702 * we need to use an exclusive lock on the map and we need to perform
1703 * various clipping operations. Otherwise we only need a read-lock
1707 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1711 case MADV_SEQUENTIAL:
1725 vm_map_lock_read(map);
1728 vm_map_entry_release(count);
1733 * Locate starting entry and clip if necessary.
1736 VM_MAP_RANGE_CHECK(map, start, end);
1738 if (vm_map_lookup_entry(map, start, &entry)) {
1740 vm_map_clip_start(map, entry, start, &count);
1742 entry = entry->next;
1747 * madvise behaviors that are implemented in the vm_map_entry.
1749 * We clip the vm_map_entry so that behavioral changes are
1750 * limited to the specified address range.
1752 for (current = entry;
1753 (current != &map->header) && (current->start < end);
1754 current = current->next
1756 if (current->maptype == VM_MAPTYPE_SUBMAP)
1759 vm_map_clip_end(map, current, end, &count);
1763 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
1765 case MADV_SEQUENTIAL:
1766 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
1769 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
1772 current->eflags |= MAP_ENTRY_NOSYNC;
1775 current->eflags &= ~MAP_ENTRY_NOSYNC;
1778 current->eflags |= MAP_ENTRY_NOCOREDUMP;
1781 current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
1785 * Invalidate the related pmap entries, used
1786 * to flush portions of the real kernel's
1787 * pmap when the caller has removed or
1788 * modified existing mappings in a virtual
1791 pmap_remove(map->pmap,
1792 current->start, current->end);
1796 * Set the page directory page for a map
1797 * governed by a virtual page table. Mark
1798 * the entry as being governed by a virtual
1799 * page table if it is not.
1801 * XXX the page directory page is stored
1802 * in the avail_ssize field if the map_entry.
1804 * XXX the map simplification code does not
1805 * compare this field so weird things may
1806 * happen if you do not apply this function
1807 * to the entire mapping governed by the
1808 * virtual page table.
1810 if (current->maptype != VM_MAPTYPE_VPAGETABLE) {
1814 current->aux.master_pde = value;
1815 pmap_remove(map->pmap,
1816 current->start, current->end);
1822 vm_map_simplify_entry(map, current, &count);
1830 * madvise behaviors that are implemented in the underlying
1833 * Since we don't clip the vm_map_entry, we have to clip
1834 * the vm_object pindex and count.
1836 * NOTE! We currently do not support these functions on
1837 * virtual page tables.
1839 for (current = entry;
1840 (current != &map->header) && (current->start < end);
1841 current = current->next
1843 vm_offset_t useStart;
1845 if (current->maptype != VM_MAPTYPE_NORMAL)
1848 pindex = OFF_TO_IDX(current->offset);
1849 count = atop(current->end - current->start);
1850 useStart = current->start;
1852 if (current->start < start) {
1853 pindex += atop(start - current->start);
1854 count -= atop(start - current->start);
1857 if (current->end > end)
1858 count -= atop(current->end - end);
1863 vm_object_madvise(current->object.vm_object,
1864 pindex, count, behav);
1867 * Try to populate the page table. Mappings governed
1868 * by virtual page tables cannot be pre-populated
1869 * without a lot of work so don't try.
1871 if (behav == MADV_WILLNEED &&
1872 current->maptype != VM_MAPTYPE_VPAGETABLE) {
1873 pmap_object_init_pt(
1876 current->protection,
1877 current->object.vm_object,
1879 (count << PAGE_SHIFT),
1880 MAP_PREFAULT_MADVISE
1884 vm_map_unlock_read(map);
1886 vm_map_entry_release(count);
1894 * Sets the inheritance of the specified address
1895 * range in the target map. Inheritance
1896 * affects how the map will be shared with
1897 * child maps at the time of vm_map_fork.
1900 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
1901 vm_inherit_t new_inheritance)
1903 vm_map_entry_t entry;
1904 vm_map_entry_t temp_entry;
1907 switch (new_inheritance) {
1908 case VM_INHERIT_NONE:
1909 case VM_INHERIT_COPY:
1910 case VM_INHERIT_SHARE:
1913 return (KERN_INVALID_ARGUMENT);
1916 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1919 VM_MAP_RANGE_CHECK(map, start, end);
1921 if (vm_map_lookup_entry(map, start, &temp_entry)) {
1923 vm_map_clip_start(map, entry, start, &count);
1925 entry = temp_entry->next;
1927 while ((entry != &map->header) && (entry->start < end)) {
1928 vm_map_clip_end(map, entry, end, &count);
1930 entry->inheritance = new_inheritance;
1932 vm_map_simplify_entry(map, entry, &count);
1934 entry = entry->next;
1937 vm_map_entry_release(count);
1938 return (KERN_SUCCESS);
1942 * Implement the semantics of mlock
1945 vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t real_end,
1946 boolean_t new_pageable)
1948 vm_map_entry_t entry;
1949 vm_map_entry_t start_entry;
1951 int rv = KERN_SUCCESS;
1954 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1956 VM_MAP_RANGE_CHECK(map, start, real_end);
1959 start_entry = vm_map_clip_range(map, start, end, &count, MAP_CLIP_NO_HOLES);
1960 if (start_entry == NULL) {
1962 vm_map_entry_release(count);
1963 return (KERN_INVALID_ADDRESS);
1966 if (new_pageable == 0) {
1967 entry = start_entry;
1968 while ((entry != &map->header) && (entry->start < end)) {
1969 vm_offset_t save_start;
1970 vm_offset_t save_end;
1973 * Already user wired or hard wired (trivial cases)
1975 if (entry->eflags & MAP_ENTRY_USER_WIRED) {
1976 entry = entry->next;
1979 if (entry->wired_count != 0) {
1980 entry->wired_count++;
1981 entry->eflags |= MAP_ENTRY_USER_WIRED;
1982 entry = entry->next;
1987 * A new wiring requires instantiation of appropriate
1988 * management structures and the faulting in of the
1991 if (entry->maptype != VM_MAPTYPE_SUBMAP) {
1992 int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY;
1993 if (copyflag && ((entry->protection & VM_PROT_WRITE) != 0)) {
1994 vm_map_entry_shadow(entry);
1995 } else if (entry->object.vm_object == NULL &&
1997 vm_map_entry_allocate_object(entry);
2000 entry->wired_count++;
2001 entry->eflags |= MAP_ENTRY_USER_WIRED;
2004 * Now fault in the area. Note that vm_fault_wire()
2005 * may release the map lock temporarily, it will be
2006 * relocked on return. The in-transition
2007 * flag protects the entries.
2009 save_start = entry->start;
2010 save_end = entry->end;
2011 rv = vm_fault_wire(map, entry, TRUE);
2013 CLIP_CHECK_BACK(entry, save_start);
2015 KASSERT(entry->wired_count == 1, ("bad wired_count on entry"));
2016 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2017 entry->wired_count = 0;
2018 if (entry->end == save_end)
2020 entry = entry->next;
2021 KASSERT(entry != &map->header, ("bad entry clip during backout"));
2023 end = save_start; /* unwire the rest */
2027 * note that even though the entry might have been
2028 * clipped, the USER_WIRED flag we set prevents
2029 * duplication so we do not have to do a
2032 entry = entry->next;
2036 * If we failed fall through to the unwiring section to
2037 * unwire what we had wired so far. 'end' has already
2044 * start_entry might have been clipped if we unlocked the
2045 * map and blocked. No matter how clipped it has gotten
2046 * there should be a fragment that is on our start boundary.
2048 CLIP_CHECK_BACK(start_entry, start);
2052 * Deal with the unwiring case.
2056 * This is the unwiring case. We must first ensure that the
2057 * range to be unwired is really wired down. We know there
2060 entry = start_entry;
2061 while ((entry != &map->header) && (entry->start < end)) {
2062 if ((entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
2063 rv = KERN_INVALID_ARGUMENT;
2066 KASSERT(entry->wired_count != 0, ("wired count was 0 with USER_WIRED set! %p", entry));
2067 entry = entry->next;
2071 * Now decrement the wiring count for each region. If a region
2072 * becomes completely unwired, unwire its physical pages and
2076 * The map entries are processed in a loop, checking to
2077 * make sure the entry is wired and asserting it has a wired
2078 * count. However, another loop was inserted more-or-less in
2079 * the middle of the unwiring path. This loop picks up the
2080 * "entry" loop variable from the first loop without first
2081 * setting it to start_entry. Naturally, the secound loop
2082 * is never entered and the pages backing the entries are
2083 * never unwired. This can lead to a leak of wired pages.
2085 entry = start_entry;
2086 while ((entry != &map->header) && (entry->start < end)) {
2087 KASSERT(entry->eflags & MAP_ENTRY_USER_WIRED,
2088 ("expected USER_WIRED on entry %p", entry));
2089 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2090 entry->wired_count--;
2091 if (entry->wired_count == 0)
2092 vm_fault_unwire(map, entry);
2093 entry = entry->next;
2097 vm_map_unclip_range(map, start_entry, start, real_end, &count,
2101 vm_map_entry_release(count);
2108 * Sets the pageability of the specified address
2109 * range in the target map. Regions specified
2110 * as not pageable require locked-down physical
2111 * memory and physical page maps.
2113 * The map must not be locked, but a reference
2114 * must remain to the map throughout the call.
2116 * This function may be called via the zalloc path and must properly
2117 * reserve map entries for kernel_map.
2120 vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t real_end, int kmflags)
2122 vm_map_entry_t entry;
2123 vm_map_entry_t start_entry;
2125 int rv = KERN_SUCCESS;
2128 if (kmflags & KM_KRESERVE)
2129 count = vm_map_entry_kreserve(MAP_RESERVE_COUNT);
2131 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
2133 VM_MAP_RANGE_CHECK(map, start, real_end);
2136 start_entry = vm_map_clip_range(map, start, end, &count, MAP_CLIP_NO_HOLES);
2137 if (start_entry == NULL) {
2139 rv = KERN_INVALID_ADDRESS;
2142 if ((kmflags & KM_PAGEABLE) == 0) {
2146 * 1. Holding the write lock, we create any shadow or zero-fill
2147 * objects that need to be created. Then we clip each map
2148 * entry to the region to be wired and increment its wiring
2149 * count. We create objects before clipping the map entries
2150 * to avoid object proliferation.
2152 * 2. We downgrade to a read lock, and call vm_fault_wire to
2153 * fault in the pages for any newly wired area (wired_count is
2156 * Downgrading to a read lock for vm_fault_wire avoids a
2157 * possible deadlock with another process that may have faulted
2158 * on one of the pages to be wired (it would mark the page busy,
2159 * blocking us, then in turn block on the map lock that we
2160 * hold). Because of problems in the recursive lock package,
2161 * we cannot upgrade to a write lock in vm_map_lookup. Thus,
2162 * any actions that require the write lock must be done
2163 * beforehand. Because we keep the read lock on the map, the
2164 * copy-on-write status of the entries we modify here cannot
2168 entry = start_entry;
2169 while ((entry != &map->header) && (entry->start < end)) {
2171 * Trivial case if the entry is already wired
2173 if (entry->wired_count) {
2174 entry->wired_count++;
2175 entry = entry->next;
2180 * The entry is being newly wired, we have to setup
2181 * appropriate management structures. A shadow
2182 * object is required for a copy-on-write region,
2183 * or a normal object for a zero-fill region. We
2184 * do not have to do this for entries that point to sub
2185 * maps because we won't hold the lock on the sub map.
2187 if (entry->maptype != VM_MAPTYPE_SUBMAP) {
2188 int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY;
2190 ((entry->protection & VM_PROT_WRITE) != 0)) {
2191 vm_map_entry_shadow(entry);
2192 } else if (entry->object.vm_object == NULL &&
2194 vm_map_entry_allocate_object(entry);
2198 entry->wired_count++;
2199 entry = entry->next;
2207 * HACK HACK HACK HACK
2209 * Unlock the map to avoid deadlocks. The in-transit flag
2210 * protects us from most changes but note that
2211 * clipping may still occur. To prevent clipping from
2212 * occuring after the unlock, except for when we are
2213 * blocking in vm_fault_wire, we must run in a critical
2214 * section, otherwise our accesses to entry->start and
2215 * entry->end could be corrupted. We have to enter the
2216 * critical section prior to unlocking so start_entry does
2217 * not change out from under us at the very beginning of the
2220 * HACK HACK HACK HACK
2225 entry = start_entry;
2226 while (entry != &map->header && entry->start < end) {
2228 * If vm_fault_wire fails for any page we need to undo
2229 * what has been done. We decrement the wiring count
2230 * for those pages which have not yet been wired (now)
2231 * and unwire those that have (later).
2233 vm_offset_t save_start = entry->start;
2234 vm_offset_t save_end = entry->end;
2236 if (entry->wired_count == 1)
2237 rv = vm_fault_wire(map, entry, FALSE);
2239 CLIP_CHECK_BACK(entry, save_start);
2241 KASSERT(entry->wired_count == 1, ("wired_count changed unexpectedly"));
2242 entry->wired_count = 0;
2243 if (entry->end == save_end)
2245 entry = entry->next;
2246 KASSERT(entry != &map->header, ("bad entry clip during backout"));
2251 CLIP_CHECK_FWD(entry, save_end);
2252 entry = entry->next;
2257 * If a failure occured undo everything by falling through
2258 * to the unwiring code. 'end' has already been adjusted
2262 kmflags |= KM_PAGEABLE;
2265 * start_entry is still IN_TRANSITION but may have been
2266 * clipped since vm_fault_wire() unlocks and relocks the
2267 * map. No matter how clipped it has gotten there should
2268 * be a fragment that is on our start boundary.
2270 CLIP_CHECK_BACK(start_entry, start);
2273 if (kmflags & KM_PAGEABLE) {
2275 * This is the unwiring case. We must first ensure that the
2276 * range to be unwired is really wired down. We know there
2279 entry = start_entry;
2280 while ((entry != &map->header) && (entry->start < end)) {
2281 if (entry->wired_count == 0) {
2282 rv = KERN_INVALID_ARGUMENT;
2285 entry = entry->next;
2289 * Now decrement the wiring count for each region. If a region
2290 * becomes completely unwired, unwire its physical pages and
2293 entry = start_entry;
2294 while ((entry != &map->header) && (entry->start < end)) {
2295 entry->wired_count--;
2296 if (entry->wired_count == 0)
2297 vm_fault_unwire(map, entry);
2298 entry = entry->next;
2302 vm_map_unclip_range(map, start_entry, start, real_end, &count,
2307 if (kmflags & KM_KRESERVE)
2308 vm_map_entry_krelease(count);
2310 vm_map_entry_release(count);
2315 * vm_map_set_wired_quick()
2317 * Mark a newly allocated address range as wired but do not fault in
2318 * the pages. The caller is expected to load the pages into the object.
2320 * The map must be locked on entry and will remain locked on return.
2323 vm_map_set_wired_quick(vm_map_t map, vm_offset_t addr, vm_size_t size, int *countp)
2325 vm_map_entry_t scan;
2326 vm_map_entry_t entry;
2328 entry = vm_map_clip_range(map, addr, addr + size, countp, MAP_CLIP_NO_HOLES);
2329 for (scan = entry; scan != &map->header && scan->start < addr + size; scan = scan->next) {
2330 KKASSERT(entry->wired_count == 0);
2331 entry->wired_count = 1;
2333 vm_map_unclip_range(map, entry, addr, addr + size, countp, MAP_CLIP_NO_HOLES);
2339 * Push any dirty cached pages in the address range to their pager.
2340 * If syncio is TRUE, dirty pages are written synchronously.
2341 * If invalidate is TRUE, any cached pages are freed as well.
2343 * Returns an error if any part of the specified range is not mapped.
2346 vm_map_clean(vm_map_t map, vm_offset_t start, vm_offset_t end, boolean_t syncio,
2347 boolean_t invalidate)
2349 vm_map_entry_t current;
2350 vm_map_entry_t entry;
2353 vm_ooffset_t offset;
2355 vm_map_lock_read(map);
2356 VM_MAP_RANGE_CHECK(map, start, end);
2357 if (!vm_map_lookup_entry(map, start, &entry)) {
2358 vm_map_unlock_read(map);
2359 return (KERN_INVALID_ADDRESS);
2362 * Make a first pass to check for holes.
2364 for (current = entry; current->start < end; current = current->next) {
2365 if (current->maptype == VM_MAPTYPE_SUBMAP) {
2366 vm_map_unlock_read(map);
2367 return (KERN_INVALID_ARGUMENT);
2369 if (end > current->end &&
2370 (current->next == &map->header ||
2371 current->end != current->next->start)) {
2372 vm_map_unlock_read(map);
2373 return (KERN_INVALID_ADDRESS);
2378 pmap_remove(vm_map_pmap(map), start, end);
2380 * Make a second pass, cleaning/uncaching pages from the indicated
2383 for (current = entry; current->start < end; current = current->next) {
2384 offset = current->offset + (start - current->start);
2385 size = (end <= current->end ? end : current->end) - start;
2386 if (current->maptype == VM_MAPTYPE_SUBMAP) {
2388 vm_map_entry_t tentry;
2391 smap = current->object.sub_map;
2392 vm_map_lock_read(smap);
2393 vm_map_lookup_entry(smap, offset, &tentry);
2394 tsize = tentry->end - offset;
2397 object = tentry->object.vm_object;
2398 offset = tentry->offset + (offset - tentry->start);
2399 vm_map_unlock_read(smap);
2401 object = current->object.vm_object;
2404 * Note that there is absolutely no sense in writing out
2405 * anonymous objects, so we track down the vnode object
2407 * We invalidate (remove) all pages from the address space
2408 * anyway, for semantic correctness.
2410 * note: certain anonymous maps, such as MAP_NOSYNC maps,
2411 * may start out with a NULL object.
2413 while (object && object->backing_object) {
2414 offset += object->backing_object_offset;
2415 object = object->backing_object;
2416 if (object->size < OFF_TO_IDX( offset + size))
2417 size = IDX_TO_OFF(object->size) - offset;
2419 if (object && (object->type == OBJT_VNODE) &&
2420 (current->protection & VM_PROT_WRITE)) {
2422 * Flush pages if writing is allowed, invalidate them
2423 * if invalidation requested. Pages undergoing I/O
2424 * will be ignored by vm_object_page_remove().
2426 * We cannot lock the vnode and then wait for paging
2427 * to complete without deadlocking against vm_fault.
2428 * Instead we simply call vm_object_page_remove() and
2429 * allow it to block internally on a page-by-page
2430 * basis when it encounters pages undergoing async
2435 vm_object_reference(object);
2436 vn_lock(object->handle, LK_EXCLUSIVE | LK_RETRY);
2437 flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
2438 flags |= invalidate ? OBJPC_INVAL : 0;
2441 * When operating on a virtual page table just
2442 * flush the whole object. XXX we probably ought
2445 switch(current->maptype) {
2446 case VM_MAPTYPE_NORMAL:
2447 vm_object_page_clean(object,
2449 OFF_TO_IDX(offset + size + PAGE_MASK),
2452 case VM_MAPTYPE_VPAGETABLE:
2453 vm_object_page_clean(object, 0, 0, flags);
2456 vn_unlock(((struct vnode *)object->handle));
2457 vm_object_deallocate(object);
2459 if (object && invalidate &&
2460 ((object->type == OBJT_VNODE) ||
2461 (object->type == OBJT_DEVICE))) {
2463 (object->type == OBJT_DEVICE) ? FALSE : TRUE;
2464 vm_object_reference(object);
2465 switch(current->maptype) {
2466 case VM_MAPTYPE_NORMAL:
2467 vm_object_page_remove(object,
2469 OFF_TO_IDX(offset + size + PAGE_MASK),
2472 case VM_MAPTYPE_VPAGETABLE:
2473 vm_object_page_remove(object, 0, 0, clean_only);
2476 vm_object_deallocate(object);
2481 vm_map_unlock_read(map);
2482 return (KERN_SUCCESS);
2486 * vm_map_entry_unwire: [ internal use only ]
2488 * Make the region specified by this entry pageable.
2490 * The map in question should be locked.
2491 * [This is the reason for this routine's existence.]
2494 vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
2496 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2497 entry->wired_count = 0;
2498 vm_fault_unwire(map, entry);
2502 * vm_map_entry_delete: [ internal use only ]
2504 * Deallocate the given entry from the target map.
2507 vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry, int *countp)
2509 vm_map_entry_unlink(map, entry);
2510 map->size -= entry->end - entry->start;
2512 switch(entry->maptype) {
2513 case VM_MAPTYPE_NORMAL:
2514 case VM_MAPTYPE_VPAGETABLE:
2515 vm_object_deallocate(entry->object.vm_object);
2521 vm_map_entry_dispose(map, entry, countp);
2525 * vm_map_delete: [ internal use only ]
2527 * Deallocates the given address range from the target
2531 vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end, int *countp)
2534 vm_map_entry_t entry;
2535 vm_map_entry_t first_entry;
2539 * Find the start of the region, and clip it. Set entry to point
2540 * at the first record containing the requested address or, if no
2541 * such record exists, the next record with a greater address. The
2542 * loop will run from this point until a record beyond the termination
2543 * address is encountered.
2545 * map->hint must be adjusted to not point to anything we delete,
2546 * so set it to the entry prior to the one being deleted.
2548 * GGG see other GGG comment.
2550 if (vm_map_lookup_entry(map, start, &first_entry)) {
2551 entry = first_entry;
2552 vm_map_clip_start(map, entry, start, countp);
2553 map->hint = entry->prev; /* possible problem XXX */
2555 map->hint = first_entry; /* possible problem XXX */
2556 entry = first_entry->next;
2560 * If a hole opens up prior to the current first_free then
2561 * adjust first_free. As with map->hint, map->first_free
2562 * cannot be left set to anything we might delete.
2564 if (entry == &map->header) {
2565 map->first_free = &map->header;
2566 } else if (map->first_free->start >= start) {
2567 map->first_free = entry->prev;
2571 * Step through all entries in this region
2574 while ((entry != &map->header) && (entry->start < end)) {
2575 vm_map_entry_t next;
2577 vm_pindex_t offidxstart, offidxend, count;
2580 * If we hit an in-transition entry we have to sleep and
2581 * retry. It's easier (and not really slower) to just retry
2582 * since this case occurs so rarely and the hint is already
2583 * pointing at the right place. We have to reset the
2584 * start offset so as not to accidently delete an entry
2585 * another process just created in vacated space.
2587 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
2588 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2589 start = entry->start;
2590 ++mycpu->gd_cnt.v_intrans_coll;
2591 ++mycpu->gd_cnt.v_intrans_wait;
2592 vm_map_transition_wait(map);
2595 vm_map_clip_end(map, entry, end, countp);
2601 offidxstart = OFF_TO_IDX(entry->offset);
2602 count = OFF_TO_IDX(e - s);
2603 object = entry->object.vm_object;
2606 * Unwire before removing addresses from the pmap; otherwise,
2607 * unwiring will put the entries back in the pmap.
2609 if (entry->wired_count != 0)
2610 vm_map_entry_unwire(map, entry);
2612 offidxend = offidxstart + count;
2614 if (object == &kernel_object) {
2615 vm_object_page_remove(object, offidxstart, offidxend, FALSE);
2617 pmap_remove(map->pmap, s, e);
2618 if (object != NULL &&
2619 object->ref_count != 1 &&
2620 (object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING &&
2621 (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2622 vm_object_collapse(object);
2623 vm_object_page_remove(object, offidxstart, offidxend, FALSE);
2624 if (object->type == OBJT_SWAP) {
2625 swap_pager_freespace(object, offidxstart, count);
2627 if (offidxend >= object->size &&
2628 offidxstart < object->size) {
2629 object->size = offidxstart;
2635 * Delete the entry (which may delete the object) only after
2636 * removing all pmap entries pointing to its pages.
2637 * (Otherwise, its page frames may be reallocated, and any
2638 * modify bits will be set in the wrong object!)
2640 vm_map_entry_delete(map, entry, countp);
2643 return (KERN_SUCCESS);
2649 * Remove the given address range from the target map.
2650 * This is the exported form of vm_map_delete.
2653 vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
2658 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
2660 VM_MAP_RANGE_CHECK(map, start, end);
2661 result = vm_map_delete(map, start, end, &count);
2663 vm_map_entry_release(count);
2669 * vm_map_check_protection:
2671 * Assert that the target map allows the specified
2672 * privilege on the entire address region given.
2673 * The entire region must be allocated.
2676 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
2677 vm_prot_t protection)
2679 vm_map_entry_t entry;
2680 vm_map_entry_t tmp_entry;
2682 if (!vm_map_lookup_entry(map, start, &tmp_entry)) {
2687 while (start < end) {
2688 if (entry == &map->header) {
2695 if (start < entry->start) {
2699 * Check protection associated with entry.
2702 if ((entry->protection & protection) != protection) {
2705 /* go to next entry */
2708 entry = entry->next;
2714 * Split the pages in a map entry into a new object. This affords
2715 * easier removal of unused pages, and keeps object inheritance from
2716 * being a negative impact on memory usage.
2719 vm_map_split(vm_map_entry_t entry)
2722 vm_object_t orig_object, new_object, source;
2724 vm_pindex_t offidxstart, offidxend, idx;
2726 vm_ooffset_t offset;
2728 orig_object = entry->object.vm_object;
2729 if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
2731 if (orig_object->ref_count <= 1)
2734 offset = entry->offset;
2738 offidxstart = OFF_TO_IDX(offset);
2739 offidxend = offidxstart + OFF_TO_IDX(e - s);
2740 size = offidxend - offidxstart;
2742 new_object = vm_pager_allocate(orig_object->type, NULL,
2743 IDX_TO_OFF(size), VM_PROT_ALL, 0);
2744 if (new_object == NULL)
2747 source = orig_object->backing_object;
2748 if (source != NULL) {
2749 vm_object_reference(source); /* Referenced by new_object */
2750 LIST_INSERT_HEAD(&source->shadow_head,
2751 new_object, shadow_list);
2752 vm_object_clear_flag(source, OBJ_ONEMAPPING);
2753 new_object->backing_object_offset =
2754 orig_object->backing_object_offset + IDX_TO_OFF(offidxstart);
2755 new_object->backing_object = source;
2756 source->shadow_count++;
2757 source->generation++;
2760 for (idx = 0; idx < size; idx++) {
2764 * A critical section is required to avoid a race between
2765 * the lookup and an interrupt/unbusy/free and our busy
2770 m = vm_page_lookup(orig_object, offidxstart + idx);
2777 * We must wait for pending I/O to complete before we can
2780 * We do not have to VM_PROT_NONE the page as mappings should
2781 * not be changed by this operation.
2783 if (vm_page_sleep_busy(m, TRUE, "spltwt"))
2786 vm_page_rename(m, new_object, idx);
2787 /* page automatically made dirty by rename and cache handled */
2792 if (orig_object->type == OBJT_SWAP) {
2793 vm_object_pip_add(orig_object, 1);
2795 * copy orig_object pages into new_object
2796 * and destroy unneeded pages in
2799 swap_pager_copy(orig_object, new_object, offidxstart, 0);
2800 vm_object_pip_wakeup(orig_object);
2804 * Wakeup the pages we played with. No spl protection is needed
2805 * for a simple wakeup.
2807 for (idx = 0; idx < size; idx++) {
2808 m = vm_page_lookup(new_object, idx);
2813 entry->object.vm_object = new_object;
2814 entry->offset = 0LL;
2815 vm_object_deallocate(orig_object);
2819 * vm_map_copy_entry:
2821 * Copies the contents of the source entry to the destination
2822 * entry. The entries *must* be aligned properly.
2825 vm_map_copy_entry(vm_map_t src_map, vm_map_t dst_map,
2826 vm_map_entry_t src_entry, vm_map_entry_t dst_entry)
2828 vm_object_t src_object;
2830 if (dst_entry->maptype == VM_MAPTYPE_SUBMAP)
2832 if (src_entry->maptype == VM_MAPTYPE_SUBMAP)
2835 if (src_entry->wired_count == 0) {
2837 * If the source entry is marked needs_copy, it is already
2840 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
2841 pmap_protect(src_map->pmap,
2844 src_entry->protection & ~VM_PROT_WRITE);
2848 * Make a copy of the object.
2850 if ((src_object = src_entry->object.vm_object) != NULL) {
2851 if ((src_object->handle == NULL) &&
2852 (src_object->type == OBJT_DEFAULT ||
2853 src_object->type == OBJT_SWAP)) {
2854 vm_object_collapse(src_object);
2855 if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
2856 vm_map_split(src_entry);
2857 src_object = src_entry->object.vm_object;
2861 vm_object_reference(src_object);
2862 vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
2863 dst_entry->object.vm_object = src_object;
2864 src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2865 dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2866 dst_entry->offset = src_entry->offset;
2868 dst_entry->object.vm_object = NULL;
2869 dst_entry->offset = 0;
2872 pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
2873 dst_entry->end - dst_entry->start, src_entry->start);
2876 * Of course, wired down pages can't be set copy-on-write.
2877 * Cause wired pages to be copied into the new map by
2878 * simulating faults (the new pages are pageable)
2880 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry);
2886 * Create a new process vmspace structure and vm_map
2887 * based on those of an existing process. The new map
2888 * is based on the old map, according to the inheritance
2889 * values on the regions in that map.
2891 * The source map must not be locked.
2894 vmspace_fork(struct vmspace *vm1)
2896 struct vmspace *vm2;
2897 vm_map_t old_map = &vm1->vm_map;
2899 vm_map_entry_t old_entry;
2900 vm_map_entry_t new_entry;
2904 vm_map_lock(old_map);
2905 old_map->infork = 1;
2908 * XXX Note: upcalls are not copied.
2910 vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
2911 bcopy(&vm1->vm_startcopy, &vm2->vm_startcopy,
2912 (caddr_t)&vm1->vm_endcopy - (caddr_t)&vm1->vm_startcopy);
2913 new_map = &vm2->vm_map; /* XXX */
2914 new_map->timestamp = 1;
2917 old_entry = old_map->header.next;
2918 while (old_entry != &old_map->header) {
2920 old_entry = old_entry->next;
2923 count = vm_map_entry_reserve(count + MAP_RESERVE_COUNT);
2925 old_entry = old_map->header.next;
2926 while (old_entry != &old_map->header) {
2927 if (old_entry->maptype == VM_MAPTYPE_SUBMAP)
2928 panic("vm_map_fork: encountered a submap");
2930 switch (old_entry->inheritance) {
2931 case VM_INHERIT_NONE:
2934 case VM_INHERIT_SHARE:
2936 * Clone the entry, creating the shared object if
2939 object = old_entry->object.vm_object;
2940 if (object == NULL) {
2941 vm_map_entry_allocate_object(old_entry);
2942 object = old_entry->object.vm_object;
2946 * Add the reference before calling vm_map_entry_shadow
2947 * to insure that a shadow object is created.
2949 vm_object_reference(object);
2950 if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
2951 vm_map_entry_shadow(old_entry);
2952 /* Transfer the second reference too. */
2953 vm_object_reference(
2954 old_entry->object.vm_object);
2955 vm_object_deallocate(object);
2956 object = old_entry->object.vm_object;
2958 vm_object_clear_flag(object, OBJ_ONEMAPPING);
2961 * Clone the entry, referencing the shared object.
2963 new_entry = vm_map_entry_create(new_map, &count);
2964 *new_entry = *old_entry;
2965 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2966 new_entry->wired_count = 0;
2969 * Insert the entry into the new map -- we know we're
2970 * inserting at the end of the new map.
2973 vm_map_entry_link(new_map, new_map->header.prev,
2977 * Update the physical map
2980 pmap_copy(new_map->pmap, old_map->pmap,
2982 (old_entry->end - old_entry->start),
2986 case VM_INHERIT_COPY:
2988 * Clone the entry and link into the map.
2990 new_entry = vm_map_entry_create(new_map, &count);
2991 *new_entry = *old_entry;
2992 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2993 new_entry->wired_count = 0;
2994 new_entry->object.vm_object = NULL;
2995 vm_map_entry_link(new_map, new_map->header.prev,
2997 vm_map_copy_entry(old_map, new_map, old_entry,
3001 old_entry = old_entry->next;
3004 new_map->size = old_map->size;
3005 old_map->infork = 0;
3006 vm_map_unlock(old_map);
3007 vm_map_entry_release(count);
3013 vm_map_stack (vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
3014 int flags, vm_prot_t prot, vm_prot_t max, int cow)
3016 vm_map_entry_t prev_entry;
3017 vm_map_entry_t new_stack_entry;
3018 vm_size_t init_ssize;
3021 vm_offset_t tmpaddr;
3023 cow |= MAP_IS_STACK;
3025 if (max_ssize < sgrowsiz)
3026 init_ssize = max_ssize;
3028 init_ssize = sgrowsiz;
3030 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
3034 * Find space for the mapping
3036 if ((flags & (MAP_FIXED | MAP_TRYFIXED)) == 0) {
3037 if (vm_map_findspace(map, addrbos, max_ssize, 1,
3040 vm_map_entry_release(count);
3041 return (KERN_NO_SPACE);
3046 /* If addr is already mapped, no go */
3047 if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
3049 vm_map_entry_release(count);
3050 return (KERN_NO_SPACE);
3054 /* XXX already handled by kern_mmap() */
3055 /* If we would blow our VMEM resource limit, no go */
3056 if (map->size + init_ssize >
3057 curproc->p_rlimit[RLIMIT_VMEM].rlim_cur) {
3059 vm_map_entry_release(count);
3060 return (KERN_NO_SPACE);
3065 * If we can't accomodate max_ssize in the current mapping,
3066 * no go. However, we need to be aware that subsequent user
3067 * mappings might map into the space we have reserved for
3068 * stack, and currently this space is not protected.
3070 * Hopefully we will at least detect this condition
3071 * when we try to grow the stack.
3073 if ((prev_entry->next != &map->header) &&
3074 (prev_entry->next->start < addrbos + max_ssize)) {
3076 vm_map_entry_release(count);
3077 return (KERN_NO_SPACE);
3081 * We initially map a stack of only init_ssize. We will
3082 * grow as needed later. Since this is to be a grow
3083 * down stack, we map at the top of the range.
3085 * Note: we would normally expect prot and max to be
3086 * VM_PROT_ALL, and cow to be 0. Possibly we should
3087 * eliminate these as input parameters, and just
3088 * pass these values here in the insert call.
3090 rv = vm_map_insert(map, &count,
3091 NULL, 0, addrbos + max_ssize - init_ssize,
3092 addrbos + max_ssize,
3097 /* Now set the avail_ssize amount */
3098 if (rv == KERN_SUCCESS) {
3099 if (prev_entry != &map->header)
3100 vm_map_clip_end(map, prev_entry, addrbos + max_ssize - init_ssize, &count);
3101 new_stack_entry = prev_entry->next;
3102 if (new_stack_entry->end != addrbos + max_ssize ||
3103 new_stack_entry->start != addrbos + max_ssize - init_ssize)
3104 panic ("Bad entry start/end for new stack entry");
3106 new_stack_entry->aux.avail_ssize = max_ssize - init_ssize;
3110 vm_map_entry_release(count);
3114 /* Attempts to grow a vm stack entry. Returns KERN_SUCCESS if the
3115 * desired address is already mapped, or if we successfully grow
3116 * the stack. Also returns KERN_SUCCESS if addr is outside the
3117 * stack range (this is strange, but preserves compatibility with
3118 * the grow function in vm_machdep.c).
3121 vm_map_growstack (struct proc *p, vm_offset_t addr)
3123 vm_map_entry_t prev_entry;
3124 vm_map_entry_t stack_entry;
3125 vm_map_entry_t new_stack_entry;
3126 struct vmspace *vm = p->p_vmspace;
3127 vm_map_t map = &vm->vm_map;
3130 int rv = KERN_SUCCESS;
3132 int use_read_lock = 1;
3135 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
3138 vm_map_lock_read(map);
3142 /* If addr is already in the entry range, no need to grow.*/
3143 if (vm_map_lookup_entry(map, addr, &prev_entry))
3146 if ((stack_entry = prev_entry->next) == &map->header)
3148 if (prev_entry == &map->header)
3149 end = stack_entry->start - stack_entry->aux.avail_ssize;
3151 end = prev_entry->end;
3154 * This next test mimics the old grow function in vm_machdep.c.
3155 * It really doesn't quite make sense, but we do it anyway
3156 * for compatibility.
3158 * If not growable stack, return success. This signals the
3159 * caller to proceed as he would normally with normal vm.
3161 if (stack_entry->aux.avail_ssize < 1 ||
3162 addr >= stack_entry->start ||
3163 addr < stack_entry->start - stack_entry->aux.avail_ssize) {
3167 /* Find the minimum grow amount */
3168 grow_amount = roundup (stack_entry->start - addr, PAGE_SIZE);
3169 if (grow_amount > stack_entry->aux.avail_ssize) {
3175 * If there is no longer enough space between the entries
3176 * nogo, and adjust the available space. Note: this
3177 * should only happen if the user has mapped into the
3178 * stack area after the stack was created, and is
3179 * probably an error.
3181 * This also effectively destroys any guard page the user
3182 * might have intended by limiting the stack size.
3184 if (grow_amount > stack_entry->start - end) {
3185 if (use_read_lock && vm_map_lock_upgrade(map)) {
3190 stack_entry->aux.avail_ssize = stack_entry->start - end;
3195 is_procstack = addr >= (vm_offset_t)vm->vm_maxsaddr;
3197 /* If this is the main process stack, see if we're over the
3200 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount >
3201 p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
3206 /* Round up the grow amount modulo SGROWSIZ */
3207 grow_amount = roundup (grow_amount, sgrowsiz);
3208 if (grow_amount > stack_entry->aux.avail_ssize) {
3209 grow_amount = stack_entry->aux.avail_ssize;
3211 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount >
3212 p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
3213 grow_amount = p->p_rlimit[RLIMIT_STACK].rlim_cur -
3217 /* If we would blow our VMEM resource limit, no go */
3218 if (map->size + grow_amount > p->p_rlimit[RLIMIT_VMEM].rlim_cur) {
3223 if (use_read_lock && vm_map_lock_upgrade(map)) {
3229 /* Get the preliminary new entry start value */
3230 addr = stack_entry->start - grow_amount;
3232 /* If this puts us into the previous entry, cut back our growth
3233 * to the available space. Also, see the note above.
3236 stack_entry->aux.avail_ssize = stack_entry->start - end;
3240 rv = vm_map_insert(map, &count,
3241 NULL, 0, addr, stack_entry->start,
3243 VM_PROT_ALL, VM_PROT_ALL,
3246 /* Adjust the available stack space by the amount we grew. */
3247 if (rv == KERN_SUCCESS) {
3248 if (prev_entry != &map->header)
3249 vm_map_clip_end(map, prev_entry, addr, &count);
3250 new_stack_entry = prev_entry->next;
3251 if (new_stack_entry->end != stack_entry->start ||
3252 new_stack_entry->start != addr)
3253 panic ("Bad stack grow start/end in new stack entry");
3255 new_stack_entry->aux.avail_ssize =
3256 stack_entry->aux.avail_ssize -
3257 (new_stack_entry->end - new_stack_entry->start);
3259 vm->vm_ssize += btoc(new_stack_entry->end -
3260 new_stack_entry->start);
3266 vm_map_unlock_read(map);
3269 vm_map_entry_release(count);
3274 * Unshare the specified VM space for exec. If other processes are
3275 * mapped to it, then create a new one. The new vmspace is null.
3278 vmspace_exec(struct proc *p, struct vmspace *vmcopy)
3280 struct vmspace *oldvmspace = p->p_vmspace;
3281 struct vmspace *newvmspace;
3282 vm_map_t map = &p->p_vmspace->vm_map;
3285 * If we are execing a resident vmspace we fork it, otherwise
3286 * we create a new vmspace. Note that exitingcnt and upcalls
3287 * are not copied to the new vmspace.
3290 newvmspace = vmspace_fork(vmcopy);
3292 newvmspace = vmspace_alloc(map->min_offset, map->max_offset);
3293 bcopy(&oldvmspace->vm_startcopy, &newvmspace->vm_startcopy,
3294 (caddr_t)&oldvmspace->vm_endcopy -
3295 (caddr_t)&oldvmspace->vm_startcopy);
3299 * Finish initializing the vmspace before assigning it
3300 * to the process. The vmspace will become the current vmspace
3303 pmap_pinit2(vmspace_pmap(newvmspace));
3304 pmap_replacevm(p, newvmspace, 0);
3305 sysref_put(&oldvmspace->vm_sysref);
3309 * Unshare the specified VM space for forcing COW. This
3310 * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
3312 * The exitingcnt test is not strictly necessary but has been
3313 * included for code sanity (to make the code a bit more deterministic).
3317 vmspace_unshare(struct proc *p)
3319 struct vmspace *oldvmspace = p->p_vmspace;
3320 struct vmspace *newvmspace;
3322 if (oldvmspace->vm_sysref.refcnt == 1 && oldvmspace->vm_exitingcnt == 0)
3324 newvmspace = vmspace_fork(oldvmspace);
3325 pmap_pinit2(vmspace_pmap(newvmspace));
3326 pmap_replacevm(p, newvmspace, 0);
3327 sysref_put(&oldvmspace->vm_sysref);
3333 * Finds the VM object, offset, and
3334 * protection for a given virtual address in the
3335 * specified map, assuming a page fault of the
3338 * Leaves the map in question locked for read; return
3339 * values are guaranteed until a vm_map_lookup_done
3340 * call is performed. Note that the map argument
3341 * is in/out; the returned map must be used in
3342 * the call to vm_map_lookup_done.
3344 * A handle (out_entry) is returned for use in
3345 * vm_map_lookup_done, to make that fast.
3347 * If a lookup is requested with "write protection"
3348 * specified, the map may be changed to perform virtual
3349 * copying operations, although the data referenced will
3353 vm_map_lookup(vm_map_t *var_map, /* IN/OUT */
3355 vm_prot_t fault_typea,
3356 vm_map_entry_t *out_entry, /* OUT */
3357 vm_object_t *object, /* OUT */
3358 vm_pindex_t *pindex, /* OUT */
3359 vm_prot_t *out_prot, /* OUT */
3360 boolean_t *wired) /* OUT */
3362 vm_map_entry_t entry;
3363 vm_map_t map = *var_map;
3365 vm_prot_t fault_type = fault_typea;
3366 int use_read_lock = 1;
3367 int rv = KERN_SUCCESS;
3371 vm_map_lock_read(map);
3376 * If the map has an interesting hint, try it before calling full
3377 * blown lookup routine.
3382 if ((entry == &map->header) ||
3383 (vaddr < entry->start) || (vaddr >= entry->end)) {
3384 vm_map_entry_t tmp_entry;
3387 * Entry was either not a valid hint, or the vaddr was not
3388 * contained in the entry, so do a full lookup.
3390 if (!vm_map_lookup_entry(map, vaddr, &tmp_entry)) {
3391 rv = KERN_INVALID_ADDRESS;
3402 if (entry->maptype == VM_MAPTYPE_SUBMAP) {
3403 vm_map_t old_map = map;
3405 *var_map = map = entry->object.sub_map;
3407 vm_map_unlock_read(old_map);
3409 vm_map_unlock(old_map);
3415 * Check whether this task is allowed to have this page.
3416 * Note the special case for MAP_ENTRY_COW
3417 * pages with an override. This is to implement a forced
3418 * COW for debuggers.
3421 if (fault_type & VM_PROT_OVERRIDE_WRITE)
3422 prot = entry->max_protection;
3424 prot = entry->protection;
3426 fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
3427 if ((fault_type & prot) != fault_type) {
3428 rv = KERN_PROTECTION_FAILURE;
3432 if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
3433 (entry->eflags & MAP_ENTRY_COW) &&
3434 (fault_type & VM_PROT_WRITE) &&
3435 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
3436 rv = KERN_PROTECTION_FAILURE;
3441 * If this page is not pageable, we have to get it for all possible
3444 *wired = (entry->wired_count != 0);
3446 prot = fault_type = entry->protection;
3449 * Virtual page tables may need to update the accessed (A) bit
3450 * in a page table entry. Upgrade the fault to a write fault for
3451 * that case if the map will support it. If the map does not support
3452 * it the page table entry simply will not be updated.
3454 if (entry->maptype == VM_MAPTYPE_VPAGETABLE) {
3455 if (prot & VM_PROT_WRITE)
3456 fault_type |= VM_PROT_WRITE;
3460 * If the entry was copy-on-write, we either ...
3462 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3464 * If we want to write the page, we may as well handle that
3465 * now since we've got the map locked.
3467 * If we don't need to write the page, we just demote the
3468 * permissions allowed.
3471 if (fault_type & VM_PROT_WRITE) {
3473 * Make a new object, and place it in the object
3474 * chain. Note that no new references have appeared
3475 * -- one just moved from the map to the new
3479 if (use_read_lock && vm_map_lock_upgrade(map)) {
3485 vm_map_entry_shadow(entry);
3488 * We're attempting to read a copy-on-write page --
3489 * don't allow writes.
3492 prot &= ~VM_PROT_WRITE;
3497 * Create an object if necessary.
3499 if (entry->object.vm_object == NULL &&
3501 if (use_read_lock && vm_map_lock_upgrade(map)) {
3506 vm_map_entry_allocate_object(entry);
3510 * Return the object/offset from this entry. If the entry was
3511 * copy-on-write or empty, it has been fixed up.
3514 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
3515 *object = entry->object.vm_object;
3518 * Return whether this is the only map sharing this data. On
3519 * success we return with a read lock held on the map. On failure
3520 * we return with the map unlocked.
3524 if (rv == KERN_SUCCESS) {
3525 if (use_read_lock == 0)
3526 vm_map_lock_downgrade(map);
3527 } else if (use_read_lock) {
3528 vm_map_unlock_read(map);
3536 * vm_map_lookup_done:
3538 * Releases locks acquired by a vm_map_lookup
3539 * (according to the handle returned by that lookup).
3543 vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry, int count)
3546 * Unlock the main-level map
3548 vm_map_unlock_read(map);
3550 vm_map_entry_release(count);
3553 #include "opt_ddb.h"
3555 #include <sys/kernel.h>
3557 #include <ddb/ddb.h>
3560 * vm_map_print: [ debug ]
3562 DB_SHOW_COMMAND(map, vm_map_print)
3565 /* XXX convert args. */
3566 vm_map_t map = (vm_map_t)addr;
3567 boolean_t full = have_addr;
3569 vm_map_entry_t entry;
3571 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
3573 (void *)map->pmap, map->nentries, map->timestamp);
3576 if (!full && db_indent)
3580 for (entry = map->header.next; entry != &map->header;
3581 entry = entry->next) {
3582 db_iprintf("map entry %p: start=%p, end=%p\n",
3583 (void *)entry, (void *)entry->start, (void *)entry->end);
3586 static char *inheritance_name[4] =
3587 {"share", "copy", "none", "donate_copy"};
3589 db_iprintf(" prot=%x/%x/%s",
3591 entry->max_protection,
3592 inheritance_name[(int)(unsigned char)entry->inheritance]);
3593 if (entry->wired_count != 0)
3594 db_printf(", wired");
3596 if (entry->maptype == VM_MAPTYPE_SUBMAP) {
3597 /* XXX no %qd in kernel. Truncate entry->offset. */
3598 db_printf(", share=%p, offset=0x%lx\n",
3599 (void *)entry->object.sub_map,
3600 (long)entry->offset);
3602 if ((entry->prev == &map->header) ||
3603 (entry->prev->object.sub_map !=
3604 entry->object.sub_map)) {
3606 vm_map_print((db_expr_t)(intptr_t)
3607 entry->object.sub_map,
3612 /* XXX no %qd in kernel. Truncate entry->offset. */
3613 db_printf(", object=%p, offset=0x%lx",
3614 (void *)entry->object.vm_object,
3615 (long)entry->offset);
3616 if (entry->eflags & MAP_ENTRY_COW)
3617 db_printf(", copy (%s)",
3618 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
3622 if ((entry->prev == &map->header) ||
3623 (entry->prev->object.vm_object !=
3624 entry->object.vm_object)) {
3626 vm_object_print((db_expr_t)(intptr_t)
3627 entry->object.vm_object,
3640 DB_SHOW_COMMAND(procvm, procvm)
3645 p = (struct proc *) addr;
3650 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
3651 (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
3652 (void *)vmspace_pmap(p->p_vmspace));
3654 vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);