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.24 2004/03/23 22:54:32 dillon Exp $
69 * Virtual memory mapping module.
72 #include <sys/param.h>
73 #include <sys/systm.h>
76 #include <sys/vmmeter.h>
78 #include <sys/vnode.h>
79 #include <sys/resourcevar.h>
83 #include <vm/vm_param.h>
85 #include <vm/vm_map.h>
86 #include <vm/vm_page.h>
87 #include <vm/vm_object.h>
88 #include <vm/vm_pager.h>
89 #include <vm/vm_kern.h>
90 #include <vm/vm_extern.h>
91 #include <vm/swap_pager.h>
92 #include <vm/vm_zone.h>
94 #include <sys/thread2.h>
97 * Virtual memory maps provide for the mapping, protection,
98 * and sharing of virtual memory objects. In addition,
99 * this module provides for an efficient virtual copy of
100 * memory from one map to another.
102 * Synchronization is required prior to most operations.
104 * Maps consist of an ordered doubly-linked list of simple
105 * entries; a single hint is used to speed up lookups.
107 * Since portions of maps are specified by start/end addresses,
108 * which may not align with existing map entries, all
109 * routines merely "clip" entries to these start/end values.
110 * [That is, an entry is split into two, bordering at a
111 * start or end value.] Note that these clippings may not
112 * always be necessary (as the two resulting entries are then
113 * not changed); however, the clipping is done for convenience.
115 * As mentioned above, virtual copy operations are performed
116 * by copying VM object references from one map to
117 * another, and then marking both regions as copy-on-write.
123 * Initialize the vm_map module. Must be called before
124 * any other vm_map routines.
126 * Map and entry structures are allocated from the general
127 * purpose memory pool with some exceptions:
129 * - The kernel map and kmem submap are allocated statically.
130 * - Kernel map entries are allocated out of a static pool.
132 * These restrictions are necessary since malloc() uses the
133 * maps and requires map entries.
136 static struct vm_zone mapentzone_store, mapzone_store;
137 static vm_zone_t mapentzone, mapzone, vmspace_zone;
138 static struct vm_object mapentobj, mapobj;
140 static struct vm_map_entry map_entry_init[MAX_MAPENT];
141 static struct vm_map map_init[MAX_KMAP];
143 static vm_map_entry_t vm_map_entry_create(vm_map_t map, int *);
144 static void vm_map_entry_dispose (vm_map_t map, vm_map_entry_t entry, int *);
145 static void _vm_map_clip_end (vm_map_t, vm_map_entry_t, vm_offset_t, int *);
146 static void _vm_map_clip_start (vm_map_t, vm_map_entry_t, vm_offset_t, int *);
147 static void vm_map_entry_delete (vm_map_t, vm_map_entry_t, int *);
148 static void vm_map_entry_unwire (vm_map_t, vm_map_entry_t);
149 static void vm_map_copy_entry (vm_map_t, vm_map_t, vm_map_entry_t,
151 static void vm_map_split (vm_map_entry_t);
152 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);
157 mapzone = &mapzone_store;
158 zbootinit(mapzone, "MAP", sizeof (struct vm_map),
160 mapentzone = &mapentzone_store;
161 zbootinit(mapentzone, "MAP ENTRY", sizeof (struct vm_map_entry),
162 map_entry_init, MAX_MAPENT);
166 * Allocate a vmspace structure, including a vm_map and pmap,
167 * and initialize those structures. The refcnt is set to 1.
168 * The remaining fields must be initialized by the caller.
171 vmspace_alloc(vm_offset_t min, vm_offset_t max)
175 vm = zalloc(vmspace_zone);
176 vm_map_init(&vm->vm_map, min, max);
177 pmap_pinit(vmspace_pmap(vm));
178 vm->vm_map.pmap = vmspace_pmap(vm); /* XXX */
181 vm->vm_exitingcnt = 0;
188 zinitna(mapentzone, &mapentobj, NULL, 0, 0, ZONE_USE_RESERVE, 1);
189 zinitna(mapzone, &mapobj, NULL, 0, 0, 0, 1);
190 vmspace_zone = zinit("VMSPACE", sizeof (struct vmspace), 0, 0, 3);
196 vmspace_dofree(struct vmspace *vm)
201 * Make sure any SysV shm is freed, it might not have in
206 KKASSERT(vm->vm_upcalls == NULL);
209 * Lock the map, to wait out all other references to it.
210 * Delete all of the mappings and pages they hold, then call
211 * the pmap module to reclaim anything left.
213 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
214 vm_map_lock(&vm->vm_map);
215 vm_map_delete(&vm->vm_map, vm->vm_map.min_offset,
216 vm->vm_map.max_offset, &count);
217 vm_map_unlock(&vm->vm_map);
218 vm_map_entry_release(count);
220 pmap_release(vmspace_pmap(vm));
221 zfree(vmspace_zone, vm);
225 vmspace_free(struct vmspace *vm)
227 if (vm->vm_refcnt == 0)
228 panic("vmspace_free: attempt to free already freed vmspace");
230 if (--vm->vm_refcnt == 0 && vm->vm_exitingcnt == 0)
235 vmspace_exitfree(struct proc *p)
243 * cleanup by parent process wait()ing on exiting child. vm_refcnt
244 * may not be 0 (e.g. fork() and child exits without exec()ing).
245 * exitingcnt may increment above 0 and drop back down to zero
246 * several times while vm_refcnt is held non-zero. vm_refcnt
247 * may also increment above 0 and drop back down to zero several
248 * times while vm_exitingcnt is held non-zero.
250 * The last wait on the exiting child's vmspace will clean up
251 * the remainder of the vmspace.
253 if (--vm->vm_exitingcnt == 0 && vm->vm_refcnt == 0)
258 * vmspace_swap_count() - count the approximate swap useage in pages for a
261 * Swap useage is determined by taking the proportional swap used by
262 * VM objects backing the VM map. To make up for fractional losses,
263 * if the VM object has any swap use at all the associated map entries
264 * count for at least 1 swap page.
267 vmspace_swap_count(struct vmspace *vmspace)
269 vm_map_t map = &vmspace->vm_map;
273 for (cur = map->header.next; cur != &map->header; cur = cur->next) {
276 if ((cur->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
277 (object = cur->object.vm_object) != NULL &&
278 object->type == OBJT_SWAP
280 int n = (cur->end - cur->start) / PAGE_SIZE;
282 if (object->un_pager.swp.swp_bcount) {
283 count += object->un_pager.swp.swp_bcount *
284 SWAP_META_PAGES * n / object->size + 1;
295 * Creates and returns a new empty VM map with
296 * the given physical map structure, and having
297 * the given lower and upper address bounds.
300 vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max)
304 result = zalloc(mapzone);
305 vm_map_init(result, min, max);
311 * Initialize an existing vm_map structure
312 * such as that in the vmspace structure.
313 * The pmap is set elsewhere.
316 vm_map_init(struct vm_map *map, vm_offset_t min, vm_offset_t max)
318 map->header.next = map->header.prev = &map->header;
323 map->min_offset = min;
324 map->max_offset = max;
325 map->first_free = &map->header;
326 map->hint = &map->header;
328 lockinit(&map->lock, 0, "thrd_sleep", 0, LK_NOPAUSE);
332 * vm_map_entry_cpu_init:
334 * Set an initial negative count so the first attempt to reserve
335 * space preloads a bunch of vm_map_entry's for this cpu. This
336 * routine is called in early boot so we cannot just call
337 * vm_map_entry_reserve().
339 * May be called for a gd other then mycpu.
342 vm_map_entry_reserve_cpu_init(globaldata_t gd)
344 gd->gd_vme_avail -= MAP_RESERVE_COUNT * 2;
348 * vm_map_entry_reserve:
350 * Reserves vm_map_entry structures so code later on can manipulate
351 * map_entry structures within a locked map without blocking trying
352 * to allocate a new vm_map_entry.
355 vm_map_entry_reserve(int count)
357 struct globaldata *gd = mycpu;
358 vm_map_entry_t entry;
361 gd->gd_vme_avail -= count;
364 * Make sure we have enough structures in gd_vme_base to handle
365 * the reservation request.
367 while (gd->gd_vme_avail < 0) {
368 entry = zalloc(mapentzone);
369 entry->next = gd->gd_vme_base;
370 gd->gd_vme_base = entry;
378 * vm_map_entry_release:
380 * Releases previously reserved vm_map_entry structures that were not
381 * used. If we have too much junk in our per-cpu cache clean some of
385 vm_map_entry_release(int count)
387 struct globaldata *gd = mycpu;
388 vm_map_entry_t entry;
391 gd->gd_vme_avail += count;
392 while (gd->gd_vme_avail > MAP_RESERVE_SLOP) {
393 entry = gd->gd_vme_base;
394 KKASSERT(entry != NULL);
395 gd->gd_vme_base = entry->next;
398 zfree(mapentzone, entry);
405 * vm_map_entry_kreserve:
407 * Reserve map entry structures for use in kernel_map or (if it exists)
408 * kmem_map. These entries have *ALREADY* been reserved on a per-cpu
409 * basis when the map was inited. This function is used by zalloc()
410 * to avoid a recursion when zalloc() itself needs to allocate additional
413 * This function should only be used when the caller intends to later
414 * call vm_map_entry_reserve() to 'normalize' the reserve cache.
417 vm_map_entry_kreserve(int count)
419 struct globaldata *gd = mycpu;
422 gd->gd_vme_kdeficit += count;
424 KKASSERT(gd->gd_vme_base != NULL);
429 * vm_map_entry_krelease:
431 * Release previously reserved map entries for kernel_map or kmem_map
432 * use. This routine determines how many entries were actually used and
433 * replentishes the kernel reserve supply from vme_avail.
435 * If there is insufficient supply vme_avail will go negative, which is
436 * ok. We cannot safely call zalloc in this function without getting
437 * into a recursion deadlock. zalloc() will call vm_map_entry_reserve()
438 * to regenerate the lost entries.
441 vm_map_entry_krelease(int count)
443 struct globaldata *gd = mycpu;
446 gd->gd_vme_kdeficit -= count;
447 gd->gd_vme_avail -= gd->gd_vme_kdeficit; /* can go negative */
448 gd->gd_vme_kdeficit = 0;
453 * vm_map_entry_create: [ internal use only ]
455 * Allocates a VM map entry for insertion. No entry fields are filled
458 * This routine may be called from an interrupt thread but not a FAST
459 * interrupt. This routine may recurse the map lock.
461 static vm_map_entry_t
462 vm_map_entry_create(vm_map_t map, int *countp)
464 struct globaldata *gd = mycpu;
465 vm_map_entry_t entry;
467 KKASSERT(*countp > 0);
470 entry = gd->gd_vme_base;
471 KASSERT(entry != NULL, ("gd_vme_base NULL! count %d", *countp));
472 gd->gd_vme_base = entry->next;
478 * vm_map_entry_dispose: [ internal use only ]
480 * Dispose of a vm_map_entry that is no longer being referenced. This
481 * function may be called from an interrupt.
484 vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry, int *countp)
486 struct globaldata *gd = mycpu;
490 entry->next = gd->gd_vme_base;
491 gd->gd_vme_base = entry;
497 * vm_map_entry_{un,}link:
499 * Insert/remove entries from maps.
502 vm_map_entry_link(vm_map_t map,
503 vm_map_entry_t after_where,
504 vm_map_entry_t entry)
507 entry->prev = after_where;
508 entry->next = after_where->next;
509 entry->next->prev = entry;
510 after_where->next = entry;
514 vm_map_entry_unlink(vm_map_t map,
515 vm_map_entry_t entry)
520 if (entry->eflags & MAP_ENTRY_IN_TRANSITION)
521 panic("vm_map_entry_unlink: attempt to mess with locked entry! %p", entry);
532 * Saves the specified entry as the hint for
535 #define SAVE_HINT(map,value) \
536 (map)->hint = (value);
539 * vm_map_lookup_entry: [ internal use only ]
541 * Finds the map entry containing (or
542 * immediately preceding) the specified address
543 * in the given map; the entry is returned
544 * in the "entry" parameter. The boolean
545 * result indicates whether the address is
546 * actually contained in the map.
549 vm_map_lookup_entry(vm_map_t map, vm_offset_t address,
550 vm_map_entry_t *entry /* OUT */)
556 * Start looking either from the head of the list, or from the hint.
561 if (cur == &map->header)
564 if (address >= cur->start) {
566 * Go from hint to end of list.
568 * But first, make a quick check to see if we are already looking
569 * at the entry we want (which is usually the case). Note also
570 * that we don't need to save the hint here... it is the same
571 * hint (unless we are at the header, in which case the hint
572 * didn't buy us anything anyway).
575 if ((cur != last) && (cur->end > address)) {
581 * Go from start to hint, *inclusively*
584 cur = map->header.next;
591 while (cur != last) {
592 if (cur->end > address) {
593 if (address >= cur->start) {
595 * Save this lookup for future hints, and
608 SAVE_HINT(map, *entry);
615 * Inserts the given whole VM object into the target
616 * map at the specified address range. The object's
617 * size should match that of the address range.
619 * Requires that the map be locked, and leaves it so. Requires that
620 * sufficient vm_map_entry structures have been reserved and tracks
621 * the use via countp.
623 * If object is non-NULL, ref count must be bumped by caller
624 * prior to making call to account for the new entry.
627 vm_map_insert(vm_map_t map, int *countp,
628 vm_object_t object, vm_ooffset_t offset,
629 vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max,
632 vm_map_entry_t new_entry;
633 vm_map_entry_t prev_entry;
634 vm_map_entry_t temp_entry;
635 vm_eflags_t protoeflags;
638 * Check that the start and end points are not bogus.
641 if ((start < map->min_offset) || (end > map->max_offset) ||
643 return (KERN_INVALID_ADDRESS);
646 * Find the entry prior to the proposed starting address; if it's part
647 * of an existing entry, this range is bogus.
650 if (vm_map_lookup_entry(map, start, &temp_entry))
651 return (KERN_NO_SPACE);
653 prev_entry = temp_entry;
656 * Assert that the next entry doesn't overlap the end point.
659 if ((prev_entry->next != &map->header) &&
660 (prev_entry->next->start < end))
661 return (KERN_NO_SPACE);
665 if (cow & MAP_COPY_ON_WRITE)
666 protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
668 if (cow & MAP_NOFAULT) {
669 protoeflags |= MAP_ENTRY_NOFAULT;
671 KASSERT(object == NULL,
672 ("vm_map_insert: paradoxical MAP_NOFAULT request"));
674 if (cow & MAP_DISABLE_SYNCER)
675 protoeflags |= MAP_ENTRY_NOSYNC;
676 if (cow & MAP_DISABLE_COREDUMP)
677 protoeflags |= MAP_ENTRY_NOCOREDUMP;
681 * When object is non-NULL, it could be shared with another
682 * process. We have to set or clear OBJ_ONEMAPPING
685 if ((object->ref_count > 1) || (object->shadow_count != 0)) {
686 vm_object_clear_flag(object, OBJ_ONEMAPPING);
689 else if ((prev_entry != &map->header) &&
690 (prev_entry->eflags == protoeflags) &&
691 (prev_entry->end == start) &&
692 (prev_entry->wired_count == 0) &&
693 ((prev_entry->object.vm_object == NULL) ||
694 vm_object_coalesce(prev_entry->object.vm_object,
695 OFF_TO_IDX(prev_entry->offset),
696 (vm_size_t)(prev_entry->end - prev_entry->start),
697 (vm_size_t)(end - prev_entry->end)))) {
699 * We were able to extend the object. Determine if we
700 * can extend the previous map entry to include the
703 if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
704 (prev_entry->protection == prot) &&
705 (prev_entry->max_protection == max)) {
706 map->size += (end - prev_entry->end);
707 prev_entry->end = end;
708 vm_map_simplify_entry(map, prev_entry, countp);
709 return (KERN_SUCCESS);
713 * If we can extend the object but cannot extend the
714 * map entry, we have to create a new map entry. We
715 * must bump the ref count on the extended object to
716 * account for it. object may be NULL.
718 object = prev_entry->object.vm_object;
719 offset = prev_entry->offset +
720 (prev_entry->end - prev_entry->start);
721 vm_object_reference(object);
725 * NOTE: if conditionals fail, object can be NULL here. This occurs
726 * in things like the buffer map where we manage kva but do not manage
734 new_entry = vm_map_entry_create(map, countp);
735 new_entry->start = start;
736 new_entry->end = end;
738 new_entry->eflags = protoeflags;
739 new_entry->object.vm_object = object;
740 new_entry->offset = offset;
741 new_entry->avail_ssize = 0;
743 new_entry->inheritance = VM_INHERIT_DEFAULT;
744 new_entry->protection = prot;
745 new_entry->max_protection = max;
746 new_entry->wired_count = 0;
749 * Insert the new entry into the list
752 vm_map_entry_link(map, prev_entry, new_entry);
753 map->size += new_entry->end - new_entry->start;
756 * Update the free space hint
758 if ((map->first_free == prev_entry) &&
759 (prev_entry->end >= new_entry->start)) {
760 map->first_free = new_entry;
765 * Temporarily removed to avoid MAP_STACK panic, due to
766 * MAP_STACK being a huge hack. Will be added back in
767 * when MAP_STACK (and the user stack mapping) is fixed.
770 * It may be possible to simplify the entry
772 vm_map_simplify_entry(map, new_entry, countp);
775 if (cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) {
776 pmap_object_init_pt(map->pmap, start,
777 object, OFF_TO_IDX(offset), end - start,
778 cow & MAP_PREFAULT_PARTIAL);
781 return (KERN_SUCCESS);
785 * Find sufficient space for `length' bytes in the given map, starting at
786 * `start'. The map must be locked. Returns 0 on success, 1 on no space.
788 * This function will returned an arbitrarily aligned pointer. If no
789 * particular alignment is required you should pass align as 1. Note that
790 * the map may return PAGE_SIZE aligned pointers if all the lengths used in
791 * the map are a multiple of PAGE_SIZE, even if you pass a smaller align
794 * 'align' should be a power of 2 but is not required to be.
804 vm_map_entry_t entry, next;
806 vm_offset_t align_mask;
808 if (start < map->min_offset)
809 start = map->min_offset;
810 if (start > map->max_offset)
814 * If the alignment is not a power of 2 we will have to use
815 * a mod/division, set align_mask to a special value.
817 if ((align | (align - 1)) + 1 != (align << 1))
818 align_mask = (vm_offset_t)-1;
820 align_mask = align - 1;
824 * Look for the first possible address; if there's already something
825 * at this address, we have to start after it.
827 if (start == map->min_offset) {
828 if ((entry = map->first_free) != &map->header)
833 if (vm_map_lookup_entry(map, start, &tmp))
839 * Look through the rest of the map, trying to fit a new region in the
840 * gap between existing regions, or after the very last region.
842 for (;; start = (entry = next)->end) {
844 * Adjust the proposed start by the requested alignment,
845 * be sure that we didn't wrap the address.
847 if (align_mask == (vm_offset_t)-1)
848 end = ((start + align - 1) / align) * align;
850 end = (start + align_mask) & ~align_mask;
855 * Find the end of the proposed new region. Be sure we didn't
856 * go beyond the end of the map, or wrap around the address.
857 * Then check to see if this is the last entry or if the
858 * proposed end fits in the gap between this and the next
861 end = start + length;
862 if (end > map->max_offset || end < start)
865 if (next == &map->header || next->start >= end)
868 SAVE_HINT(map, entry);
869 if (map == kernel_map) {
871 if ((ksize = round_page(start + length)) > kernel_vm_end) {
872 pmap_growkernel(ksize);
881 * vm_map_find finds an unallocated region in the target address
882 * map with the given length. The search is defined to be
883 * first-fit from the specified address; the region found is
884 * returned in the same parameter.
886 * If object is non-NULL, ref count must be bumped by caller
887 * prior to making call to account for the new entry.
890 vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
891 vm_offset_t *addr, /* IN/OUT */
892 vm_size_t length, boolean_t find_space, vm_prot_t prot,
893 vm_prot_t max, int cow)
901 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
904 if (vm_map_findspace(map, start, length, 1, addr)) {
906 vm_map_entry_release(count);
907 return (KERN_NO_SPACE);
911 result = vm_map_insert(map, &count, object, offset,
912 start, start + length, prot, max, cow);
914 vm_map_entry_release(count);
920 * vm_map_simplify_entry:
922 * Simplify the given map entry by merging with either neighbor. This
923 * routine also has the ability to merge with both neighbors.
925 * The map must be locked.
927 * This routine guarentees that the passed entry remains valid (though
928 * possibly extended). When merging, this routine may delete one or
929 * both neighbors. No action is taken on entries which have their
930 * in-transition flag set.
933 vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry, int *countp)
935 vm_map_entry_t next, prev;
936 vm_size_t prevsize, esize;
938 if (entry->eflags & (MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP)) {
939 ++mycpu->gd_cnt.v_intrans_coll;
944 if (prev != &map->header) {
945 prevsize = prev->end - prev->start;
946 if ( (prev->end == entry->start) &&
947 (prev->object.vm_object == entry->object.vm_object) &&
948 (!prev->object.vm_object ||
949 (prev->offset + prevsize == entry->offset)) &&
950 (prev->eflags == entry->eflags) &&
951 (prev->protection == entry->protection) &&
952 (prev->max_protection == entry->max_protection) &&
953 (prev->inheritance == entry->inheritance) &&
954 (prev->wired_count == entry->wired_count)) {
955 if (map->first_free == prev)
956 map->first_free = entry;
957 if (map->hint == prev)
959 vm_map_entry_unlink(map, prev);
960 entry->start = prev->start;
961 entry->offset = prev->offset;
962 if (prev->object.vm_object)
963 vm_object_deallocate(prev->object.vm_object);
964 vm_map_entry_dispose(map, prev, countp);
969 if (next != &map->header) {
970 esize = entry->end - entry->start;
971 if ((entry->end == next->start) &&
972 (next->object.vm_object == entry->object.vm_object) &&
973 (!entry->object.vm_object ||
974 (entry->offset + esize == next->offset)) &&
975 (next->eflags == entry->eflags) &&
976 (next->protection == entry->protection) &&
977 (next->max_protection == entry->max_protection) &&
978 (next->inheritance == entry->inheritance) &&
979 (next->wired_count == entry->wired_count)) {
980 if (map->first_free == next)
981 map->first_free = entry;
982 if (map->hint == next)
984 vm_map_entry_unlink(map, next);
985 entry->end = next->end;
986 if (next->object.vm_object)
987 vm_object_deallocate(next->object.vm_object);
988 vm_map_entry_dispose(map, next, countp);
993 * vm_map_clip_start: [ internal use only ]
995 * Asserts that the given entry begins at or after
996 * the specified address; if necessary,
997 * it splits the entry into two.
999 #define vm_map_clip_start(map, entry, startaddr, countp) \
1001 if (startaddr > entry->start) \
1002 _vm_map_clip_start(map, entry, startaddr, countp); \
1006 * This routine is called only when it is known that
1007 * the entry must be split.
1010 _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start, int *countp)
1012 vm_map_entry_t new_entry;
1015 * Split off the front portion -- note that we must insert the new
1016 * entry BEFORE this one, so that this entry has the specified
1020 vm_map_simplify_entry(map, entry, countp);
1023 * If there is no object backing this entry, we might as well create
1024 * one now. If we defer it, an object can get created after the map
1025 * is clipped, and individual objects will be created for the split-up
1026 * map. This is a bit of a hack, but is also about the best place to
1027 * put this improvement.
1030 if (entry->object.vm_object == NULL && !map->system_map) {
1032 object = vm_object_allocate(OBJT_DEFAULT,
1033 atop(entry->end - entry->start));
1034 entry->object.vm_object = object;
1038 new_entry = vm_map_entry_create(map, countp);
1039 *new_entry = *entry;
1041 new_entry->end = start;
1042 entry->offset += (start - entry->start);
1043 entry->start = start;
1045 vm_map_entry_link(map, entry->prev, new_entry);
1047 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1048 vm_object_reference(new_entry->object.vm_object);
1053 * vm_map_clip_end: [ internal use only ]
1055 * Asserts that the given entry ends at or before
1056 * the specified address; if necessary,
1057 * it splits the entry into two.
1060 #define vm_map_clip_end(map, entry, endaddr, countp) \
1062 if (endaddr < entry->end) \
1063 _vm_map_clip_end(map, entry, endaddr, countp); \
1067 * This routine is called only when it is known that
1068 * the entry must be split.
1071 _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end, int *countp)
1073 vm_map_entry_t new_entry;
1076 * If there is no object backing this entry, we might as well create
1077 * one now. If we defer it, an object can get created after the map
1078 * is clipped, and individual objects will be created for the split-up
1079 * map. This is a bit of a hack, but is also about the best place to
1080 * put this improvement.
1083 if (entry->object.vm_object == NULL && !map->system_map) {
1085 object = vm_object_allocate(OBJT_DEFAULT,
1086 atop(entry->end - entry->start));
1087 entry->object.vm_object = object;
1092 * Create a new entry and insert it AFTER the specified entry
1095 new_entry = vm_map_entry_create(map, countp);
1096 *new_entry = *entry;
1098 new_entry->start = entry->end = end;
1099 new_entry->offset += (end - entry->start);
1101 vm_map_entry_link(map, entry, new_entry);
1103 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1104 vm_object_reference(new_entry->object.vm_object);
1109 * VM_MAP_RANGE_CHECK: [ internal use only ]
1111 * Asserts that the starting and ending region
1112 * addresses fall within the valid range of the map.
1114 #define VM_MAP_RANGE_CHECK(map, start, end) \
1116 if (start < vm_map_min(map)) \
1117 start = vm_map_min(map); \
1118 if (end > vm_map_max(map)) \
1119 end = vm_map_max(map); \
1125 * vm_map_transition_wait: [ kernel use only ]
1127 * Used to block when an in-transition collison occurs. The map
1128 * is unlocked for the sleep and relocked before the return.
1132 vm_map_transition_wait(vm_map_t map)
1135 tsleep(map, 0, "vment", 0);
1143 * When we do blocking operations with the map lock held it is
1144 * possible that a clip might have occured on our in-transit entry,
1145 * requiring an adjustment to the entry in our loop. These macros
1146 * help the pageable and clip_range code deal with the case. The
1147 * conditional costs virtually nothing if no clipping has occured.
1150 #define CLIP_CHECK_BACK(entry, save_start) \
1152 while (entry->start != save_start) { \
1153 entry = entry->prev; \
1154 KASSERT(entry != &map->header, ("bad entry clip")); \
1158 #define CLIP_CHECK_FWD(entry, save_end) \
1160 while (entry->end != save_end) { \
1161 entry = entry->next; \
1162 KASSERT(entry != &map->header, ("bad entry clip")); \
1168 * vm_map_clip_range: [ kernel use only ]
1170 * Clip the specified range and return the base entry. The
1171 * range may cover several entries starting at the returned base
1172 * and the first and last entry in the covering sequence will be
1173 * properly clipped to the requested start and end address.
1175 * If no holes are allowed you should pass the MAP_CLIP_NO_HOLES
1178 * The MAP_ENTRY_IN_TRANSITION flag will be set for the entries
1179 * covered by the requested range.
1181 * The map must be exclusively locked on entry and will remain locked
1182 * on return. If no range exists or the range contains holes and you
1183 * specified that no holes were allowed, NULL will be returned. This
1184 * routine may temporarily unlock the map in order avoid a deadlock when
1189 vm_map_clip_range(vm_map_t map, vm_offset_t start, vm_offset_t end,
1190 int *countp, int flags)
1192 vm_map_entry_t start_entry;
1193 vm_map_entry_t entry;
1196 * Locate the entry and effect initial clipping. The in-transition
1197 * case does not occur very often so do not try to optimize it.
1200 if (vm_map_lookup_entry(map, start, &start_entry) == FALSE)
1202 entry = start_entry;
1203 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1204 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1205 ++mycpu->gd_cnt.v_intrans_coll;
1206 ++mycpu->gd_cnt.v_intrans_wait;
1207 vm_map_transition_wait(map);
1209 * entry and/or start_entry may have been clipped while
1210 * we slept, or may have gone away entirely. We have
1211 * to restart from the lookup.
1216 * Since we hold an exclusive map lock we do not have to restart
1217 * after clipping, even though clipping may block in zalloc.
1219 vm_map_clip_start(map, entry, start, countp);
1220 vm_map_clip_end(map, entry, end, countp);
1221 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
1224 * Scan entries covered by the range. When working on the next
1225 * entry a restart need only re-loop on the current entry which
1226 * we have already locked, since 'next' may have changed. Also,
1227 * even though entry is safe, it may have been clipped so we
1228 * have to iterate forwards through the clip after sleeping.
1230 while (entry->next != &map->header && entry->next->start < end) {
1231 vm_map_entry_t next = entry->next;
1233 if (flags & MAP_CLIP_NO_HOLES) {
1234 if (next->start > entry->end) {
1235 vm_map_unclip_range(map, start_entry,
1236 start, entry->end, countp, flags);
1241 if (next->eflags & MAP_ENTRY_IN_TRANSITION) {
1242 vm_offset_t save_end = entry->end;
1243 next->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1244 ++mycpu->gd_cnt.v_intrans_coll;
1245 ++mycpu->gd_cnt.v_intrans_wait;
1246 vm_map_transition_wait(map);
1249 * clips might have occured while we blocked.
1251 CLIP_CHECK_FWD(entry, save_end);
1252 CLIP_CHECK_BACK(start_entry, start);
1256 * No restart necessary even though clip_end may block, we
1257 * are holding the map lock.
1259 vm_map_clip_end(map, next, end, countp);
1260 next->eflags |= MAP_ENTRY_IN_TRANSITION;
1263 if (flags & MAP_CLIP_NO_HOLES) {
1264 if (entry->end != end) {
1265 vm_map_unclip_range(map, start_entry,
1266 start, entry->end, countp, flags);
1270 return(start_entry);
1274 * vm_map_unclip_range: [ kernel use only ]
1276 * Undo the effect of vm_map_clip_range(). You should pass the same
1277 * flags and the same range that you passed to vm_map_clip_range().
1278 * This code will clear the in-transition flag on the entries and
1279 * wake up anyone waiting. This code will also simplify the sequence
1280 * and attempt to merge it with entries before and after the sequence.
1282 * The map must be locked on entry and will remain locked on return.
1284 * Note that you should also pass the start_entry returned by
1285 * vm_map_clip_range(). However, if you block between the two calls
1286 * with the map unlocked please be aware that the start_entry may
1287 * have been clipped and you may need to scan it backwards to find
1288 * the entry corresponding with the original start address. You are
1289 * responsible for this, vm_map_unclip_range() expects the correct
1290 * start_entry to be passed to it and will KASSERT otherwise.
1294 vm_map_unclip_range(
1296 vm_map_entry_t start_entry,
1302 vm_map_entry_t entry;
1304 entry = start_entry;
1306 KASSERT(entry->start == start, ("unclip_range: illegal base entry"));
1307 while (entry != &map->header && entry->start < end) {
1308 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION, ("in-transition flag not set during unclip on: %p", entry));
1309 KASSERT(entry->end <= end, ("unclip_range: tail wasn't clipped"));
1310 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
1311 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
1312 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
1315 entry = entry->next;
1319 * Simplification does not block so there is no restart case.
1321 entry = start_entry;
1322 while (entry != &map->header && entry->start < end) {
1323 vm_map_simplify_entry(map, entry, countp);
1324 entry = entry->next;
1329 * vm_map_submap: [ kernel use only ]
1331 * Mark the given range as handled by a subordinate map.
1333 * This range must have been created with vm_map_find,
1334 * and no other operations may have been performed on this
1335 * range prior to calling vm_map_submap.
1337 * Only a limited number of operations can be performed
1338 * within this rage after calling vm_map_submap:
1340 * [Don't try vm_map_copy!]
1342 * To remove a submapping, one must first remove the
1343 * range from the superior map, and then destroy the
1344 * submap (if desired). [Better yet, don't try it.]
1347 vm_map_submap(vm_map_t map, vm_offset_t start, vm_offset_t end, vm_map_t submap)
1349 vm_map_entry_t entry;
1350 int result = KERN_INVALID_ARGUMENT;
1353 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1356 VM_MAP_RANGE_CHECK(map, start, end);
1358 if (vm_map_lookup_entry(map, start, &entry)) {
1359 vm_map_clip_start(map, entry, start, &count);
1361 entry = entry->next;
1364 vm_map_clip_end(map, entry, end, &count);
1366 if ((entry->start == start) && (entry->end == end) &&
1367 ((entry->eflags & MAP_ENTRY_COW) == 0) &&
1368 (entry->object.vm_object == NULL)) {
1369 entry->object.sub_map = submap;
1370 entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
1371 result = KERN_SUCCESS;
1374 vm_map_entry_release(count);
1382 * Sets the protection of the specified address
1383 * region in the target map. If "set_max" is
1384 * specified, the maximum protection is to be set;
1385 * otherwise, only the current protection is affected.
1388 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
1389 vm_prot_t new_prot, boolean_t set_max)
1391 vm_map_entry_t current;
1392 vm_map_entry_t entry;
1395 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1398 VM_MAP_RANGE_CHECK(map, start, end);
1400 if (vm_map_lookup_entry(map, start, &entry)) {
1401 vm_map_clip_start(map, entry, start, &count);
1403 entry = entry->next;
1407 * Make a first pass to check for protection violations.
1411 while ((current != &map->header) && (current->start < end)) {
1412 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1414 vm_map_entry_release(count);
1415 return (KERN_INVALID_ARGUMENT);
1417 if ((new_prot & current->max_protection) != new_prot) {
1419 vm_map_entry_release(count);
1420 return (KERN_PROTECTION_FAILURE);
1422 current = current->next;
1426 * Go back and fix up protections. [Note that clipping is not
1427 * necessary the second time.]
1431 while ((current != &map->header) && (current->start < end)) {
1434 vm_map_clip_end(map, current, end, &count);
1436 old_prot = current->protection;
1438 current->protection =
1439 (current->max_protection = new_prot) &
1442 current->protection = new_prot;
1445 * Update physical map if necessary. Worry about copy-on-write
1446 * here -- CHECK THIS XXX
1449 if (current->protection != old_prot) {
1450 #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1453 pmap_protect(map->pmap, current->start,
1455 current->protection & MASK(current));
1459 vm_map_simplify_entry(map, current, &count);
1461 current = current->next;
1465 vm_map_entry_release(count);
1466 return (KERN_SUCCESS);
1472 * This routine traverses a processes map handling the madvise
1473 * system call. Advisories are classified as either those effecting
1474 * the vm_map_entry structure, or those effecting the underlying
1479 vm_map_madvise(vm_map_t map, vm_offset_t start, vm_offset_t end, int behav)
1481 vm_map_entry_t current, entry;
1486 * Some madvise calls directly modify the vm_map_entry, in which case
1487 * we need to use an exclusive lock on the map and we need to perform
1488 * various clipping operations. Otherwise we only need a read-lock
1492 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1496 case MADV_SEQUENTIAL:
1508 vm_map_lock_read(map);
1511 vm_map_entry_release(count);
1512 return (KERN_INVALID_ARGUMENT);
1516 * Locate starting entry and clip if necessary.
1519 VM_MAP_RANGE_CHECK(map, start, end);
1521 if (vm_map_lookup_entry(map, start, &entry)) {
1523 vm_map_clip_start(map, entry, start, &count);
1525 entry = entry->next;
1530 * madvise behaviors that are implemented in the vm_map_entry.
1532 * We clip the vm_map_entry so that behavioral changes are
1533 * limited to the specified address range.
1535 for (current = entry;
1536 (current != &map->header) && (current->start < end);
1537 current = current->next
1539 if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1542 vm_map_clip_end(map, current, end, &count);
1546 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
1548 case MADV_SEQUENTIAL:
1549 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
1552 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
1555 current->eflags |= MAP_ENTRY_NOSYNC;
1558 current->eflags &= ~MAP_ENTRY_NOSYNC;
1561 current->eflags |= MAP_ENTRY_NOCOREDUMP;
1564 current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
1569 vm_map_simplify_entry(map, current, &count);
1577 * madvise behaviors that are implemented in the underlying
1580 * Since we don't clip the vm_map_entry, we have to clip
1581 * the vm_object pindex and count.
1583 for (current = entry;
1584 (current != &map->header) && (current->start < end);
1585 current = current->next
1587 vm_offset_t useStart;
1589 if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1592 pindex = OFF_TO_IDX(current->offset);
1593 count = atop(current->end - current->start);
1594 useStart = current->start;
1596 if (current->start < start) {
1597 pindex += atop(start - current->start);
1598 count -= atop(start - current->start);
1601 if (current->end > end)
1602 count -= atop(current->end - end);
1607 vm_object_madvise(current->object.vm_object,
1608 pindex, count, behav);
1609 if (behav == MADV_WILLNEED) {
1610 pmap_object_init_pt(
1613 current->object.vm_object,
1615 (count << PAGE_SHIFT),
1616 MAP_PREFAULT_MADVISE
1620 vm_map_unlock_read(map);
1622 vm_map_entry_release(count);
1630 * Sets the inheritance of the specified address
1631 * range in the target map. Inheritance
1632 * affects how the map will be shared with
1633 * child maps at the time of vm_map_fork.
1636 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
1637 vm_inherit_t new_inheritance)
1639 vm_map_entry_t entry;
1640 vm_map_entry_t temp_entry;
1643 switch (new_inheritance) {
1644 case VM_INHERIT_NONE:
1645 case VM_INHERIT_COPY:
1646 case VM_INHERIT_SHARE:
1649 return (KERN_INVALID_ARGUMENT);
1652 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1655 VM_MAP_RANGE_CHECK(map, start, end);
1657 if (vm_map_lookup_entry(map, start, &temp_entry)) {
1659 vm_map_clip_start(map, entry, start, &count);
1661 entry = temp_entry->next;
1663 while ((entry != &map->header) && (entry->start < end)) {
1664 vm_map_clip_end(map, entry, end, &count);
1666 entry->inheritance = new_inheritance;
1668 vm_map_simplify_entry(map, entry, &count);
1670 entry = entry->next;
1673 vm_map_entry_release(count);
1674 return (KERN_SUCCESS);
1678 * Implement the semantics of mlock
1681 vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t real_end,
1682 boolean_t new_pageable)
1684 vm_map_entry_t entry;
1685 vm_map_entry_t start_entry;
1687 int rv = KERN_SUCCESS;
1690 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1692 VM_MAP_RANGE_CHECK(map, start, real_end);
1695 start_entry = vm_map_clip_range(map, start, end, &count, MAP_CLIP_NO_HOLES);
1696 if (start_entry == NULL) {
1698 vm_map_entry_release(count);
1699 return (KERN_INVALID_ADDRESS);
1702 if (new_pageable == 0) {
1703 entry = start_entry;
1704 while ((entry != &map->header) && (entry->start < end)) {
1705 vm_offset_t save_start;
1706 vm_offset_t save_end;
1709 * Already user wired or hard wired (trivial cases)
1711 if (entry->eflags & MAP_ENTRY_USER_WIRED) {
1712 entry = entry->next;
1715 if (entry->wired_count != 0) {
1716 entry->wired_count++;
1717 entry->eflags |= MAP_ENTRY_USER_WIRED;
1718 entry = entry->next;
1723 * A new wiring requires instantiation of appropriate
1724 * management structures and the faulting in of the
1727 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1728 int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY;
1729 if (copyflag && ((entry->protection & VM_PROT_WRITE) != 0)) {
1731 vm_object_shadow(&entry->object.vm_object,
1733 atop(entry->end - entry->start));
1734 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
1736 } else if (entry->object.vm_object == NULL &&
1739 entry->object.vm_object =
1740 vm_object_allocate(OBJT_DEFAULT,
1741 atop(entry->end - entry->start));
1742 entry->offset = (vm_offset_t) 0;
1746 entry->wired_count++;
1747 entry->eflags |= MAP_ENTRY_USER_WIRED;
1750 * Now fault in the area. The map lock needs to be
1751 * manipulated to avoid deadlocks. The in-transition
1752 * flag protects the entries.
1754 save_start = entry->start;
1755 save_end = entry->end;
1758 rv = vm_fault_user_wire(map, save_start, save_end);
1761 CLIP_CHECK_BACK(entry, save_start);
1763 KASSERT(entry->wired_count == 1, ("bad wired_count on entry"));
1764 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
1765 entry->wired_count = 0;
1766 if (entry->end == save_end)
1768 entry = entry->next;
1769 KASSERT(entry != &map->header, ("bad entry clip during backout"));
1771 end = save_start; /* unwire the rest */
1775 * note that even though the entry might have been
1776 * clipped, the USER_WIRED flag we set prevents
1777 * duplication so we do not have to do a
1780 entry = entry->next;
1784 * If we failed fall through to the unwiring section to
1785 * unwire what we had wired so far. 'end' has already
1792 * start_entry might have been clipped if we unlocked the
1793 * map and blocked. No matter how clipped it has gotten
1794 * there should be a fragment that is on our start boundary.
1796 CLIP_CHECK_BACK(start_entry, start);
1800 * Deal with the unwiring case.
1804 * This is the unwiring case. We must first ensure that the
1805 * range to be unwired is really wired down. We know there
1808 entry = start_entry;
1809 while ((entry != &map->header) && (entry->start < end)) {
1810 if ((entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
1811 rv = KERN_INVALID_ARGUMENT;
1814 KASSERT(entry->wired_count != 0, ("wired count was 0 with USER_WIRED set! %p", entry));
1815 entry = entry->next;
1819 * Now decrement the wiring count for each region. If a region
1820 * becomes completely unwired, unwire its physical pages and
1824 * The map entries are processed in a loop, checking to
1825 * make sure the entry is wired and asserting it has a wired
1826 * count. However, another loop was inserted more-or-less in
1827 * the middle of the unwiring path. This loop picks up the
1828 * "entry" loop variable from the first loop without first
1829 * setting it to start_entry. Naturally, the secound loop
1830 * is never entered and the pages backing the entries are
1831 * never unwired. This can lead to a leak of wired pages.
1833 entry = start_entry;
1834 while ((entry != &map->header) && (entry->start < end)) {
1835 KASSERT(entry->eflags & MAP_ENTRY_USER_WIRED, ("expected USER_WIRED on entry %p", entry));
1836 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
1837 entry->wired_count--;
1838 if (entry->wired_count == 0)
1839 vm_fault_unwire(map, entry->start, entry->end);
1840 entry = entry->next;
1844 vm_map_unclip_range(map, start_entry, start, real_end, &count,
1848 vm_map_entry_release(count);
1855 * Sets the pageability of the specified address
1856 * range in the target map. Regions specified
1857 * as not pageable require locked-down physical
1858 * memory and physical page maps.
1860 * The map must not be locked, but a reference
1861 * must remain to the map throughout the call.
1863 * This function may be called via the zalloc path and must properly
1864 * reserve map entries for kernel_map.
1867 vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t real_end, int kmflags)
1869 vm_map_entry_t entry;
1870 vm_map_entry_t start_entry;
1872 int rv = KERN_SUCCESS;
1876 if (kmflags & KM_KRESERVE)
1877 count = vm_map_entry_kreserve(MAP_RESERVE_COUNT);
1879 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1881 VM_MAP_RANGE_CHECK(map, start, real_end);
1884 start_entry = vm_map_clip_range(map, start, end, &count, MAP_CLIP_NO_HOLES);
1885 if (start_entry == NULL) {
1887 rv = KERN_INVALID_ADDRESS;
1890 if ((kmflags & KM_PAGEABLE) == 0) {
1894 * 1. Holding the write lock, we create any shadow or zero-fill
1895 * objects that need to be created. Then we clip each map
1896 * entry to the region to be wired and increment its wiring
1897 * count. We create objects before clipping the map entries
1898 * to avoid object proliferation.
1900 * 2. We downgrade to a read lock, and call vm_fault_wire to
1901 * fault in the pages for any newly wired area (wired_count is
1904 * Downgrading to a read lock for vm_fault_wire avoids a
1905 * possible deadlock with another process that may have faulted
1906 * on one of the pages to be wired (it would mark the page busy,
1907 * blocking us, then in turn block on the map lock that we
1908 * hold). Because of problems in the recursive lock package,
1909 * we cannot upgrade to a write lock in vm_map_lookup. Thus,
1910 * any actions that require the write lock must be done
1911 * beforehand. Because we keep the read lock on the map, the
1912 * copy-on-write status of the entries we modify here cannot
1916 entry = start_entry;
1917 while ((entry != &map->header) && (entry->start < end)) {
1919 * Trivial case if the entry is already wired
1921 if (entry->wired_count) {
1922 entry->wired_count++;
1923 entry = entry->next;
1928 * The entry is being newly wired, we have to setup
1929 * appropriate management structures. A shadow
1930 * object is required for a copy-on-write region,
1931 * or a normal object for a zero-fill region. We
1932 * do not have to do this for entries that point to sub
1933 * maps because we won't hold the lock on the sub map.
1935 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1936 int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY;
1938 ((entry->protection & VM_PROT_WRITE) != 0)) {
1940 vm_object_shadow(&entry->object.vm_object,
1942 atop(entry->end - entry->start));
1943 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
1944 } else if (entry->object.vm_object == NULL &&
1946 entry->object.vm_object =
1947 vm_object_allocate(OBJT_DEFAULT,
1948 atop(entry->end - entry->start));
1949 entry->offset = (vm_offset_t) 0;
1953 entry->wired_count++;
1954 entry = entry->next;
1962 * HACK HACK HACK HACK
1964 * Unlock the map to avoid deadlocks. The in-transit flag
1965 * protects us from most changes but note that
1966 * clipping may still occur. To prevent clipping from
1967 * occuring after the unlock, except for when we are
1968 * blocking in vm_fault_wire, we must run at splvm().
1969 * Otherwise our accesses to entry->start and entry->end
1970 * could be corrupted. We have to set splvm() prior to
1971 * unlocking so start_entry does not change out from
1972 * under us at the very beginning of the loop.
1974 * HACK HACK HACK HACK
1980 entry = start_entry;
1981 while (entry != &map->header && entry->start < end) {
1983 * If vm_fault_wire fails for any page we need to undo
1984 * what has been done. We decrement the wiring count
1985 * for those pages which have not yet been wired (now)
1986 * and unwire those that have (later).
1988 vm_offset_t save_start = entry->start;
1989 vm_offset_t save_end = entry->end;
1991 if (entry->wired_count == 1)
1992 rv = vm_fault_wire(map, entry->start, entry->end);
1994 CLIP_CHECK_BACK(entry, save_start);
1996 KASSERT(entry->wired_count == 1, ("wired_count changed unexpectedly"));
1997 entry->wired_count = 0;
1998 if (entry->end == save_end)
2000 entry = entry->next;
2001 KASSERT(entry != &map->header, ("bad entry clip during backout"));
2006 CLIP_CHECK_FWD(entry, save_end);
2007 entry = entry->next;
2012 * relock. start_entry is still IN_TRANSITION and must
2013 * still exist, but may have been clipped (handled just
2019 * If a failure occured undo everything by falling through
2020 * to the unwiring code. 'end' has already been adjusted
2024 kmflags |= KM_PAGEABLE;
2027 * start_entry might have been clipped if we unlocked the
2028 * map and blocked. No matter how clipped it has gotten
2029 * there should be a fragment that is on our start boundary.
2031 CLIP_CHECK_BACK(start_entry, start);
2034 if (kmflags & KM_PAGEABLE) {
2036 * This is the unwiring case. We must first ensure that the
2037 * range to be unwired is really wired down. We know there
2040 entry = start_entry;
2041 while ((entry != &map->header) && (entry->start < end)) {
2042 if (entry->wired_count == 0) {
2043 rv = KERN_INVALID_ARGUMENT;
2046 entry = entry->next;
2050 * Now decrement the wiring count for each region. If a region
2051 * becomes completely unwired, unwire its physical pages and
2054 entry = start_entry;
2055 while ((entry != &map->header) && (entry->start < end)) {
2056 entry->wired_count--;
2057 if (entry->wired_count == 0)
2058 vm_fault_unwire(map, entry->start, entry->end);
2059 entry = entry->next;
2063 vm_map_unclip_range(map, start_entry, start, real_end, &count,
2068 if (kmflags & KM_KRESERVE)
2069 vm_map_entry_krelease(count);
2071 vm_map_entry_release(count);
2076 * vm_map_set_wired_quick()
2078 * Mark a newly allocated address range as wired but do not fault in
2079 * the pages. The caller is expected to load the pages into the object.
2081 * The map must be locked on entry and will remain locked on return.
2084 vm_map_set_wired_quick(vm_map_t map, vm_offset_t addr, vm_size_t size, int *countp)
2086 vm_map_entry_t scan;
2087 vm_map_entry_t entry;
2089 entry = vm_map_clip_range(map, addr, addr + size, countp, MAP_CLIP_NO_HOLES);
2090 for (scan = entry; scan != &map->header && scan->start < addr + size; scan = scan->next) {
2091 KKASSERT(entry->wired_count == 0);
2092 entry->wired_count = 1;
2094 vm_map_unclip_range(map, entry, addr, addr + size, countp, MAP_CLIP_NO_HOLES);
2100 * Push any dirty cached pages in the address range to their pager.
2101 * If syncio is TRUE, dirty pages are written synchronously.
2102 * If invalidate is TRUE, any cached pages are freed as well.
2104 * Returns an error if any part of the specified range is not mapped.
2107 vm_map_clean(vm_map_t map, vm_offset_t start, vm_offset_t end, boolean_t syncio,
2108 boolean_t invalidate)
2110 vm_map_entry_t current;
2111 vm_map_entry_t entry;
2114 vm_ooffset_t offset;
2116 vm_map_lock_read(map);
2117 VM_MAP_RANGE_CHECK(map, start, end);
2118 if (!vm_map_lookup_entry(map, start, &entry)) {
2119 vm_map_unlock_read(map);
2120 return (KERN_INVALID_ADDRESS);
2123 * Make a first pass to check for holes.
2125 for (current = entry; current->start < end; current = current->next) {
2126 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
2127 vm_map_unlock_read(map);
2128 return (KERN_INVALID_ARGUMENT);
2130 if (end > current->end &&
2131 (current->next == &map->header ||
2132 current->end != current->next->start)) {
2133 vm_map_unlock_read(map);
2134 return (KERN_INVALID_ADDRESS);
2139 pmap_remove(vm_map_pmap(map), start, end);
2141 * Make a second pass, cleaning/uncaching pages from the indicated
2144 for (current = entry; current->start < end; current = current->next) {
2145 offset = current->offset + (start - current->start);
2146 size = (end <= current->end ? end : current->end) - start;
2147 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
2149 vm_map_entry_t tentry;
2152 smap = current->object.sub_map;
2153 vm_map_lock_read(smap);
2154 (void) vm_map_lookup_entry(smap, offset, &tentry);
2155 tsize = tentry->end - offset;
2158 object = tentry->object.vm_object;
2159 offset = tentry->offset + (offset - tentry->start);
2160 vm_map_unlock_read(smap);
2162 object = current->object.vm_object;
2165 * Note that there is absolutely no sense in writing out
2166 * anonymous objects, so we track down the vnode object
2168 * We invalidate (remove) all pages from the address space
2169 * anyway, for semantic correctness.
2171 * note: certain anonymous maps, such as MAP_NOSYNC maps,
2172 * may start out with a NULL object.
2174 while (object && object->backing_object) {
2175 object = object->backing_object;
2176 offset += object->backing_object_offset;
2177 if (object->size < OFF_TO_IDX( offset + size))
2178 size = IDX_TO_OFF(object->size) - offset;
2180 if (object && (object->type == OBJT_VNODE) &&
2181 (current->protection & VM_PROT_WRITE)) {
2183 * Flush pages if writing is allowed, invalidate them
2184 * if invalidation requested. Pages undergoing I/O
2185 * will be ignored by vm_object_page_remove().
2187 * We cannot lock the vnode and then wait for paging
2188 * to complete without deadlocking against vm_fault.
2189 * Instead we simply call vm_object_page_remove() and
2190 * allow it to block internally on a page-by-page
2191 * basis when it encounters pages undergoing async
2196 vm_object_reference(object);
2197 vn_lock(object->handle, NULL,
2198 LK_EXCLUSIVE | LK_RETRY, curthread);
2199 flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
2200 flags |= invalidate ? OBJPC_INVAL : 0;
2201 vm_object_page_clean(object,
2203 OFF_TO_IDX(offset + size + PAGE_MASK),
2205 VOP_UNLOCK(object->handle, NULL, 0, curthread);
2206 vm_object_deallocate(object);
2208 if (object && invalidate &&
2209 ((object->type == OBJT_VNODE) ||
2210 (object->type == OBJT_DEVICE))) {
2211 vm_object_reference(object);
2212 vm_object_page_remove(object,
2214 OFF_TO_IDX(offset + size + PAGE_MASK),
2216 vm_object_deallocate(object);
2221 vm_map_unlock_read(map);
2222 return (KERN_SUCCESS);
2226 * vm_map_entry_unwire: [ internal use only ]
2228 * Make the region specified by this entry pageable.
2230 * The map in question should be locked.
2231 * [This is the reason for this routine's existence.]
2234 vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
2236 vm_fault_unwire(map, entry->start, entry->end);
2237 entry->wired_count = 0;
2241 * vm_map_entry_delete: [ internal use only ]
2243 * Deallocate the given entry from the target map.
2246 vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry, int *countp)
2248 vm_map_entry_unlink(map, entry);
2249 map->size -= entry->end - entry->start;
2251 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
2252 vm_object_deallocate(entry->object.vm_object);
2255 vm_map_entry_dispose(map, entry, countp);
2259 * vm_map_delete: [ internal use only ]
2261 * Deallocates the given address range from the target
2265 vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end, int *countp)
2268 vm_map_entry_t entry;
2269 vm_map_entry_t first_entry;
2272 * Find the start of the region, and clip it
2276 if (!vm_map_lookup_entry(map, start, &first_entry))
2277 entry = first_entry->next;
2279 entry = first_entry;
2280 vm_map_clip_start(map, entry, start, countp);
2282 * Fix the lookup hint now, rather than each time though the
2285 SAVE_HINT(map, entry->prev);
2289 * Save the free space hint
2292 if (entry == &map->header) {
2293 map->first_free = &map->header;
2294 } else if (map->first_free->start >= start) {
2295 map->first_free = entry->prev;
2299 * Step through all entries in this region
2302 while ((entry != &map->header) && (entry->start < end)) {
2303 vm_map_entry_t next;
2305 vm_pindex_t offidxstart, offidxend, count;
2308 * If we hit an in-transition entry we have to sleep and
2309 * retry. It's easier (and not really slower) to just retry
2310 * since this case occurs so rarely and the hint is already
2311 * pointing at the right place. We have to reset the
2312 * start offset so as not to accidently delete an entry
2313 * another process just created in vacated space.
2315 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
2316 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2317 start = entry->start;
2318 ++mycpu->gd_cnt.v_intrans_coll;
2319 ++mycpu->gd_cnt.v_intrans_wait;
2320 vm_map_transition_wait(map);
2323 vm_map_clip_end(map, entry, end, countp);
2329 offidxstart = OFF_TO_IDX(entry->offset);
2330 count = OFF_TO_IDX(e - s);
2331 object = entry->object.vm_object;
2334 * Unwire before removing addresses from the pmap; otherwise,
2335 * unwiring will put the entries back in the pmap.
2337 if (entry->wired_count != 0) {
2338 vm_map_entry_unwire(map, entry);
2341 offidxend = offidxstart + count;
2343 if ((object == kernel_object) || (object == kmem_object)) {
2344 vm_object_page_remove(object, offidxstart, offidxend, FALSE);
2346 pmap_remove(map->pmap, s, e);
2347 if (object != NULL &&
2348 object->ref_count != 1 &&
2349 (object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING &&
2350 (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2351 vm_object_collapse(object);
2352 vm_object_page_remove(object, offidxstart, offidxend, FALSE);
2353 if (object->type == OBJT_SWAP) {
2354 swap_pager_freespace(object, offidxstart, count);
2356 if (offidxend >= object->size &&
2357 offidxstart < object->size) {
2358 object->size = offidxstart;
2364 * Delete the entry (which may delete the object) only after
2365 * removing all pmap entries pointing to its pages.
2366 * (Otherwise, its page frames may be reallocated, and any
2367 * modify bits will be set in the wrong object!)
2369 vm_map_entry_delete(map, entry, countp);
2372 return (KERN_SUCCESS);
2378 * Remove the given address range from the target map.
2379 * This is the exported form of vm_map_delete.
2382 vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
2387 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
2389 VM_MAP_RANGE_CHECK(map, start, end);
2390 result = vm_map_delete(map, start, end, &count);
2392 vm_map_entry_release(count);
2398 * vm_map_check_protection:
2400 * Assert that the target map allows the specified
2401 * privilege on the entire address region given.
2402 * The entire region must be allocated.
2405 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
2406 vm_prot_t protection)
2408 vm_map_entry_t entry;
2409 vm_map_entry_t tmp_entry;
2411 if (!vm_map_lookup_entry(map, start, &tmp_entry)) {
2416 while (start < end) {
2417 if (entry == &map->header) {
2424 if (start < entry->start) {
2428 * Check protection associated with entry.
2431 if ((entry->protection & protection) != protection) {
2434 /* go to next entry */
2437 entry = entry->next;
2443 * Split the pages in a map entry into a new object. This affords
2444 * easier removal of unused pages, and keeps object inheritance from
2445 * being a negative impact on memory usage.
2448 vm_map_split(vm_map_entry_t entry)
2451 vm_object_t orig_object, new_object, source;
2453 vm_pindex_t offidxstart, offidxend, idx;
2455 vm_ooffset_t offset;
2457 orig_object = entry->object.vm_object;
2458 if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
2460 if (orig_object->ref_count <= 1)
2463 offset = entry->offset;
2467 offidxstart = OFF_TO_IDX(offset);
2468 offidxend = offidxstart + OFF_TO_IDX(e - s);
2469 size = offidxend - offidxstart;
2471 new_object = vm_pager_allocate(orig_object->type,
2472 NULL, IDX_TO_OFF(size), VM_PROT_ALL, 0LL);
2473 if (new_object == NULL)
2476 source = orig_object->backing_object;
2477 if (source != NULL) {
2478 vm_object_reference(source); /* Referenced by new_object */
2479 LIST_INSERT_HEAD(&source->shadow_head,
2480 new_object, shadow_list);
2481 vm_object_clear_flag(source, OBJ_ONEMAPPING);
2482 new_object->backing_object_offset =
2483 orig_object->backing_object_offset + IDX_TO_OFF(offidxstart);
2484 new_object->backing_object = source;
2485 source->shadow_count++;
2486 source->generation++;
2489 for (idx = 0; idx < size; idx++) {
2493 m = vm_page_lookup(orig_object, offidxstart + idx);
2498 * We must wait for pending I/O to complete before we can
2501 * We do not have to VM_PROT_NONE the page as mappings should
2502 * not be changed by this operation.
2504 if (vm_page_sleep_busy(m, TRUE, "spltwt"))
2508 vm_page_rename(m, new_object, idx);
2509 /* page automatically made dirty by rename and cache handled */
2513 if (orig_object->type == OBJT_SWAP) {
2514 vm_object_pip_add(orig_object, 1);
2516 * copy orig_object pages into new_object
2517 * and destroy unneeded pages in
2520 swap_pager_copy(orig_object, new_object, offidxstart, 0);
2521 vm_object_pip_wakeup(orig_object);
2524 for (idx = 0; idx < size; idx++) {
2525 m = vm_page_lookup(new_object, idx);
2531 entry->object.vm_object = new_object;
2532 entry->offset = 0LL;
2533 vm_object_deallocate(orig_object);
2537 * vm_map_copy_entry:
2539 * Copies the contents of the source entry to the destination
2540 * entry. The entries *must* be aligned properly.
2543 vm_map_copy_entry(vm_map_t src_map, vm_map_t dst_map,
2544 vm_map_entry_t src_entry, vm_map_entry_t dst_entry)
2546 vm_object_t src_object;
2548 if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
2551 if (src_entry->wired_count == 0) {
2554 * If the source entry is marked needs_copy, it is already
2557 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
2558 pmap_protect(src_map->pmap,
2561 src_entry->protection & ~VM_PROT_WRITE);
2565 * Make a copy of the object.
2567 if ((src_object = src_entry->object.vm_object) != NULL) {
2569 if ((src_object->handle == NULL) &&
2570 (src_object->type == OBJT_DEFAULT ||
2571 src_object->type == OBJT_SWAP)) {
2572 vm_object_collapse(src_object);
2573 if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
2574 vm_map_split(src_entry);
2575 src_object = src_entry->object.vm_object;
2579 vm_object_reference(src_object);
2580 vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
2581 dst_entry->object.vm_object = src_object;
2582 src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2583 dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2584 dst_entry->offset = src_entry->offset;
2586 dst_entry->object.vm_object = NULL;
2587 dst_entry->offset = 0;
2590 pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
2591 dst_entry->end - dst_entry->start, src_entry->start);
2594 * Of course, wired down pages can't be set copy-on-write.
2595 * Cause wired pages to be copied into the new map by
2596 * simulating faults (the new pages are pageable)
2598 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry);
2604 * Create a new process vmspace structure and vm_map
2605 * based on those of an existing process. The new map
2606 * is based on the old map, according to the inheritance
2607 * values on the regions in that map.
2609 * The source map must not be locked.
2612 vmspace_fork(struct vmspace *vm1)
2614 struct vmspace *vm2;
2615 vm_map_t old_map = &vm1->vm_map;
2617 vm_map_entry_t old_entry;
2618 vm_map_entry_t new_entry;
2622 vm_map_lock(old_map);
2623 old_map->infork = 1;
2626 * XXX Note: upcalls are not copied.
2628 vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
2629 bcopy(&vm1->vm_startcopy, &vm2->vm_startcopy,
2630 (caddr_t)&vm1->vm_endcopy - (caddr_t)&vm1->vm_startcopy);
2631 new_map = &vm2->vm_map; /* XXX */
2632 new_map->timestamp = 1;
2635 old_entry = old_map->header.next;
2636 while (old_entry != &old_map->header) {
2638 old_entry = old_entry->next;
2641 count = vm_map_entry_reserve(count + MAP_RESERVE_COUNT);
2643 old_entry = old_map->header.next;
2644 while (old_entry != &old_map->header) {
2645 if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2646 panic("vm_map_fork: encountered a submap");
2648 switch (old_entry->inheritance) {
2649 case VM_INHERIT_NONE:
2652 case VM_INHERIT_SHARE:
2654 * Clone the entry, creating the shared object if necessary.
2656 object = old_entry->object.vm_object;
2657 if (object == NULL) {
2658 object = vm_object_allocate(OBJT_DEFAULT,
2659 atop(old_entry->end - old_entry->start));
2660 old_entry->object.vm_object = object;
2661 old_entry->offset = (vm_offset_t) 0;
2665 * Add the reference before calling vm_object_shadow
2666 * to insure that a shadow object is created.
2668 vm_object_reference(object);
2669 if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
2670 vm_object_shadow(&old_entry->object.vm_object,
2672 atop(old_entry->end - old_entry->start));
2673 old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
2674 /* Transfer the second reference too. */
2675 vm_object_reference(
2676 old_entry->object.vm_object);
2677 vm_object_deallocate(object);
2678 object = old_entry->object.vm_object;
2680 vm_object_clear_flag(object, OBJ_ONEMAPPING);
2683 * Clone the entry, referencing the shared object.
2685 new_entry = vm_map_entry_create(new_map, &count);
2686 *new_entry = *old_entry;
2687 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2688 new_entry->wired_count = 0;
2691 * Insert the entry into the new map -- we know we're
2692 * inserting at the end of the new map.
2695 vm_map_entry_link(new_map, new_map->header.prev,
2699 * Update the physical map
2702 pmap_copy(new_map->pmap, old_map->pmap,
2704 (old_entry->end - old_entry->start),
2708 case VM_INHERIT_COPY:
2710 * Clone the entry and link into the map.
2712 new_entry = vm_map_entry_create(new_map, &count);
2713 *new_entry = *old_entry;
2714 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2715 new_entry->wired_count = 0;
2716 new_entry->object.vm_object = NULL;
2717 vm_map_entry_link(new_map, new_map->header.prev,
2719 vm_map_copy_entry(old_map, new_map, old_entry,
2723 old_entry = old_entry->next;
2726 new_map->size = old_map->size;
2727 old_map->infork = 0;
2728 vm_map_unlock(old_map);
2729 vm_map_entry_release(count);
2735 vm_map_stack (vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
2736 vm_prot_t prot, vm_prot_t max, int cow)
2738 vm_map_entry_t prev_entry;
2739 vm_map_entry_t new_stack_entry;
2740 vm_size_t init_ssize;
2744 if (VM_MIN_ADDRESS > 0 && addrbos < VM_MIN_ADDRESS)
2745 return (KERN_NO_SPACE);
2747 if (max_ssize < sgrowsiz)
2748 init_ssize = max_ssize;
2750 init_ssize = sgrowsiz;
2752 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
2755 /* If addr is already mapped, no go */
2756 if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
2758 vm_map_entry_release(count);
2759 return (KERN_NO_SPACE);
2762 /* If we would blow our VMEM resource limit, no go */
2763 if (map->size + init_ssize >
2764 curproc->p_rlimit[RLIMIT_VMEM].rlim_cur) {
2766 vm_map_entry_release(count);
2767 return (KERN_NO_SPACE);
2770 /* If we can't accomodate max_ssize in the current mapping,
2771 * no go. However, we need to be aware that subsequent user
2772 * mappings might map into the space we have reserved for
2773 * stack, and currently this space is not protected.
2775 * Hopefully we will at least detect this condition
2776 * when we try to grow the stack.
2778 if ((prev_entry->next != &map->header) &&
2779 (prev_entry->next->start < addrbos + max_ssize)) {
2781 vm_map_entry_release(count);
2782 return (KERN_NO_SPACE);
2785 /* We initially map a stack of only init_ssize. We will
2786 * grow as needed later. Since this is to be a grow
2787 * down stack, we map at the top of the range.
2789 * Note: we would normally expect prot and max to be
2790 * VM_PROT_ALL, and cow to be 0. Possibly we should
2791 * eliminate these as input parameters, and just
2792 * pass these values here in the insert call.
2794 rv = vm_map_insert(map, &count,
2795 NULL, 0, addrbos + max_ssize - init_ssize,
2796 addrbos + max_ssize, prot, max, cow);
2798 /* Now set the avail_ssize amount */
2799 if (rv == KERN_SUCCESS){
2800 if (prev_entry != &map->header)
2801 vm_map_clip_end(map, prev_entry, addrbos + max_ssize - init_ssize, &count);
2802 new_stack_entry = prev_entry->next;
2803 if (new_stack_entry->end != addrbos + max_ssize ||
2804 new_stack_entry->start != addrbos + max_ssize - init_ssize)
2805 panic ("Bad entry start/end for new stack entry");
2807 new_stack_entry->avail_ssize = max_ssize - init_ssize;
2811 vm_map_entry_release(count);
2815 /* Attempts to grow a vm stack entry. Returns KERN_SUCCESS if the
2816 * desired address is already mapped, or if we successfully grow
2817 * the stack. Also returns KERN_SUCCESS if addr is outside the
2818 * stack range (this is strange, but preserves compatibility with
2819 * the grow function in vm_machdep.c).
2822 vm_map_growstack (struct proc *p, vm_offset_t addr)
2824 vm_map_entry_t prev_entry;
2825 vm_map_entry_t stack_entry;
2826 vm_map_entry_t new_stack_entry;
2827 struct vmspace *vm = p->p_vmspace;
2828 vm_map_t map = &vm->vm_map;
2831 int rv = KERN_SUCCESS;
2833 int use_read_lock = 1;
2836 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
2839 vm_map_lock_read(map);
2843 /* If addr is already in the entry range, no need to grow.*/
2844 if (vm_map_lookup_entry(map, addr, &prev_entry))
2847 if ((stack_entry = prev_entry->next) == &map->header)
2849 if (prev_entry == &map->header)
2850 end = stack_entry->start - stack_entry->avail_ssize;
2852 end = prev_entry->end;
2854 /* This next test mimics the old grow function in vm_machdep.c.
2855 * It really doesn't quite make sense, but we do it anyway
2856 * for compatibility.
2858 * If not growable stack, return success. This signals the
2859 * caller to proceed as he would normally with normal vm.
2861 if (stack_entry->avail_ssize < 1 ||
2862 addr >= stack_entry->start ||
2863 addr < stack_entry->start - stack_entry->avail_ssize) {
2867 /* Find the minimum grow amount */
2868 grow_amount = roundup (stack_entry->start - addr, PAGE_SIZE);
2869 if (grow_amount > stack_entry->avail_ssize) {
2874 /* If there is no longer enough space between the entries
2875 * nogo, and adjust the available space. Note: this
2876 * should only happen if the user has mapped into the
2877 * stack area after the stack was created, and is
2878 * probably an error.
2880 * This also effectively destroys any guard page the user
2881 * might have intended by limiting the stack size.
2883 if (grow_amount > stack_entry->start - end) {
2884 if (use_read_lock && vm_map_lock_upgrade(map)) {
2889 stack_entry->avail_ssize = stack_entry->start - end;
2894 is_procstack = addr >= (vm_offset_t)vm->vm_maxsaddr;
2896 /* If this is the main process stack, see if we're over the
2899 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount >
2900 p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
2905 /* Round up the grow amount modulo SGROWSIZ */
2906 grow_amount = roundup (grow_amount, sgrowsiz);
2907 if (grow_amount > stack_entry->avail_ssize) {
2908 grow_amount = stack_entry->avail_ssize;
2910 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount >
2911 p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
2912 grow_amount = p->p_rlimit[RLIMIT_STACK].rlim_cur -
2916 /* If we would blow our VMEM resource limit, no go */
2917 if (map->size + grow_amount > p->p_rlimit[RLIMIT_VMEM].rlim_cur) {
2922 if (use_read_lock && vm_map_lock_upgrade(map)) {
2928 /* Get the preliminary new entry start value */
2929 addr = stack_entry->start - grow_amount;
2931 /* If this puts us into the previous entry, cut back our growth
2932 * to the available space. Also, see the note above.
2935 stack_entry->avail_ssize = stack_entry->start - end;
2939 rv = vm_map_insert(map, &count,
2940 NULL, 0, addr, stack_entry->start,
2945 /* Adjust the available stack space by the amount we grew. */
2946 if (rv == KERN_SUCCESS) {
2947 if (prev_entry != &map->header)
2948 vm_map_clip_end(map, prev_entry, addr, &count);
2949 new_stack_entry = prev_entry->next;
2950 if (new_stack_entry->end != stack_entry->start ||
2951 new_stack_entry->start != addr)
2952 panic ("Bad stack grow start/end in new stack entry");
2954 new_stack_entry->avail_ssize = stack_entry->avail_ssize -
2955 (new_stack_entry->end -
2956 new_stack_entry->start);
2958 vm->vm_ssize += btoc(new_stack_entry->end -
2959 new_stack_entry->start);
2965 vm_map_unlock_read(map);
2968 vm_map_entry_release(count);
2973 * Unshare the specified VM space for exec. If other processes are
2974 * mapped to it, then create a new one. The new vmspace is null.
2978 vmspace_exec(struct proc *p, struct vmspace *vmcopy)
2980 struct vmspace *oldvmspace = p->p_vmspace;
2981 struct vmspace *newvmspace;
2982 vm_map_t map = &p->p_vmspace->vm_map;
2985 * If we are execing a resident vmspace we fork it, otherwise
2986 * we create a new vmspace. Note that exitingcnt and upcalls
2987 * are not copied to the new vmspace.
2990 newvmspace = vmspace_fork(vmcopy);
2992 newvmspace = vmspace_alloc(map->min_offset, map->max_offset);
2993 bcopy(&oldvmspace->vm_startcopy, &newvmspace->vm_startcopy,
2994 (caddr_t)&oldvmspace->vm_endcopy -
2995 (caddr_t)&oldvmspace->vm_startcopy);
2999 * This code is written like this for prototype purposes. The
3000 * goal is to avoid running down the vmspace here, but let the
3001 * other process's that are still using the vmspace to finally
3002 * run it down. Even though there is little or no chance of blocking
3003 * here, it is a good idea to keep this form for future mods.
3005 p->p_vmspace = newvmspace;
3006 pmap_pinit2(vmspace_pmap(newvmspace));
3009 vmspace_free(oldvmspace);
3013 * Unshare the specified VM space for forcing COW. This
3014 * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
3016 * The exitingcnt test is not strictly necessary but has been
3017 * included for code sanity (to make the code a bit more deterministic).
3021 vmspace_unshare(struct proc *p)
3023 struct vmspace *oldvmspace = p->p_vmspace;
3024 struct vmspace *newvmspace;
3026 if (oldvmspace->vm_refcnt == 1 && oldvmspace->vm_exitingcnt == 0)
3028 newvmspace = vmspace_fork(oldvmspace);
3029 p->p_vmspace = newvmspace;
3030 pmap_pinit2(vmspace_pmap(newvmspace));
3033 vmspace_free(oldvmspace);
3039 * Finds the VM object, offset, and
3040 * protection for a given virtual address in the
3041 * specified map, assuming a page fault of the
3044 * Leaves the map in question locked for read; return
3045 * values are guaranteed until a vm_map_lookup_done
3046 * call is performed. Note that the map argument
3047 * is in/out; the returned map must be used in
3048 * the call to vm_map_lookup_done.
3050 * A handle (out_entry) is returned for use in
3051 * vm_map_lookup_done, to make that fast.
3053 * If a lookup is requested with "write protection"
3054 * specified, the map may be changed to perform virtual
3055 * copying operations, although the data referenced will
3059 vm_map_lookup(vm_map_t *var_map, /* IN/OUT */
3061 vm_prot_t fault_typea,
3062 vm_map_entry_t *out_entry, /* OUT */
3063 vm_object_t *object, /* OUT */
3064 vm_pindex_t *pindex, /* OUT */
3065 vm_prot_t *out_prot, /* OUT */
3066 boolean_t *wired) /* OUT */
3068 vm_map_entry_t entry;
3069 vm_map_t map = *var_map;
3071 vm_prot_t fault_type = fault_typea;
3072 int use_read_lock = 1;
3073 int rv = KERN_SUCCESS;
3077 vm_map_lock_read(map);
3082 * If the map has an interesting hint, try it before calling full
3083 * blown lookup routine.
3088 if ((entry == &map->header) ||
3089 (vaddr < entry->start) || (vaddr >= entry->end)) {
3090 vm_map_entry_t tmp_entry;
3093 * Entry was either not a valid hint, or the vaddr was not
3094 * contained in the entry, so do a full lookup.
3096 if (!vm_map_lookup_entry(map, vaddr, &tmp_entry)) {
3097 rv = KERN_INVALID_ADDRESS;
3109 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3110 vm_map_t old_map = map;
3112 *var_map = map = entry->object.sub_map;
3114 vm_map_unlock_read(old_map);
3116 vm_map_unlock(old_map);
3122 * Check whether this task is allowed to have this page.
3123 * Note the special case for MAP_ENTRY_COW
3124 * pages with an override. This is to implement a forced
3125 * COW for debuggers.
3128 if (fault_type & VM_PROT_OVERRIDE_WRITE)
3129 prot = entry->max_protection;
3131 prot = entry->protection;
3133 fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
3134 if ((fault_type & prot) != fault_type) {
3135 rv = KERN_PROTECTION_FAILURE;
3139 if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
3140 (entry->eflags & MAP_ENTRY_COW) &&
3141 (fault_type & VM_PROT_WRITE) &&
3142 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
3143 rv = KERN_PROTECTION_FAILURE;
3148 * If this page is not pageable, we have to get it for all possible
3152 *wired = (entry->wired_count != 0);
3154 prot = fault_type = entry->protection;
3157 * If the entry was copy-on-write, we either ...
3160 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3162 * If we want to write the page, we may as well handle that
3163 * now since we've got the map locked.
3165 * If we don't need to write the page, we just demote the
3166 * permissions allowed.
3169 if (fault_type & VM_PROT_WRITE) {
3171 * Make a new object, and place it in the object
3172 * chain. Note that no new references have appeared
3173 * -- one just moved from the map to the new
3177 if (use_read_lock && vm_map_lock_upgrade(map)) {
3184 &entry->object.vm_object,
3186 atop(entry->end - entry->start));
3188 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
3191 * We're attempting to read a copy-on-write page --
3192 * don't allow writes.
3195 prot &= ~VM_PROT_WRITE;
3200 * Create an object if necessary.
3202 if (entry->object.vm_object == NULL &&
3204 if (use_read_lock && vm_map_lock_upgrade(map)) {
3209 entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
3210 atop(entry->end - entry->start));
3215 * Return the object/offset from this entry. If the entry was
3216 * copy-on-write or empty, it has been fixed up.
3219 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
3220 *object = entry->object.vm_object;
3223 * Return whether this is the only map sharing this data. On
3224 * success we return with a read lock held on the map. On failure
3225 * we return with the map unlocked.
3229 if (rv == KERN_SUCCESS) {
3230 if (use_read_lock == 0)
3231 vm_map_lock_downgrade(map);
3232 } else if (use_read_lock) {
3233 vm_map_unlock_read(map);
3241 * vm_map_lookup_done:
3243 * Releases locks acquired by a vm_map_lookup
3244 * (according to the handle returned by that lookup).
3248 vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry, int count)
3251 * Unlock the main-level map
3253 vm_map_unlock_read(map);
3255 vm_map_entry_release(count);
3258 #ifdef ENABLE_VFS_IOOPT
3261 * Implement uiomove with VM operations. This handles (and collateral changes)
3262 * support every combination of source object modification, and COW type
3266 vm_uiomove(vm_map_t mapa, vm_object_t srcobject, off_t cp, int cnta,
3267 vm_offset_t uaddra, int *npages)
3270 vm_object_t first_object, oldobject, object;
3271 vm_map_entry_t entry;
3275 vm_offset_t uaddr, start, end, tend;
3276 vm_pindex_t first_pindex, osize, oindex;
3290 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
3292 if ((vm_map_lookup(&map, uaddr,
3293 VM_PROT_READ, &entry, &first_object,
3294 &first_pindex, &prot, &wired)) != KERN_SUCCESS) {
3298 vm_map_clip_start(map, entry, uaddr, &count);
3301 tend = uaddr + tcnt;
3302 if (tend > entry->end) {
3303 tcnt = entry->end - uaddr;
3307 vm_map_clip_end(map, entry, tend, &count);
3309 start = entry->start;
3314 oindex = OFF_TO_IDX(cp);
3317 for (idx = 0; idx < osize; idx++) {
3319 if ((m = vm_page_lookup(srcobject, oindex + idx)) == NULL) {
3320 vm_map_lookup_done(map, entry, count);
3324 * disallow busy or invalid pages, but allow
3325 * m->busy pages if they are entirely valid.
3327 if ((m->flags & PG_BUSY) ||
3328 ((m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL)) {
3329 vm_map_lookup_done(map, entry, count);
3336 * If we are changing an existing map entry, just redirect
3337 * the object, and change mappings.
3339 if ((first_object->type == OBJT_VNODE) &&
3340 ((oldobject = entry->object.vm_object) == first_object)) {
3342 if ((entry->offset != cp) || (oldobject != srcobject)) {
3344 * Remove old window into the file
3346 pmap_remove (map->pmap, uaddr, tend);
3349 * Force copy on write for mmaped regions
3351 vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
3354 * Point the object appropriately
3356 if (oldobject != srcobject) {
3359 * Set the object optimization hint flag
3361 vm_object_set_flag(srcobject, OBJ_OPT);
3362 vm_object_reference(srcobject);
3363 entry->object.vm_object = srcobject;
3366 vm_object_deallocate(oldobject);
3373 pmap_remove (map->pmap, uaddr, tend);
3376 } else if ((first_object->ref_count == 1) &&
3377 (first_object->size == osize) &&
3378 ((first_object->type == OBJT_DEFAULT) ||
3379 (first_object->type == OBJT_SWAP)) ) {
3381 oldobject = first_object->backing_object;
3383 if ((first_object->backing_object_offset != cp) ||
3384 (oldobject != srcobject)) {
3386 * Remove old window into the file
3388 pmap_remove (map->pmap, uaddr, tend);
3391 * Remove unneeded old pages
3393 vm_object_page_remove(first_object, 0, 0, 0);
3396 * Invalidate swap space
3398 if (first_object->type == OBJT_SWAP) {
3399 swap_pager_freespace(first_object,
3401 first_object->size);
3405 * Force copy on write for mmaped regions
3407 vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
3410 * Point the object appropriately
3412 if (oldobject != srcobject) {
3415 * Set the object optimization hint flag
3417 vm_object_set_flag(srcobject, OBJ_OPT);
3418 vm_object_reference(srcobject);
3422 first_object, shadow_list);
3423 oldobject->shadow_count--;
3424 /* XXX bump generation? */
3425 vm_object_deallocate(oldobject);
3428 LIST_INSERT_HEAD(&srcobject->shadow_head,
3429 first_object, shadow_list);
3430 srcobject->shadow_count++;
3431 /* XXX bump generation? */
3433 first_object->backing_object = srcobject;
3435 first_object->backing_object_offset = cp;
3438 pmap_remove (map->pmap, uaddr, tend);
3441 * Otherwise, we have to do a logical mmap.
3445 vm_object_set_flag(srcobject, OBJ_OPT);
3446 vm_object_reference(srcobject);
3448 pmap_remove (map->pmap, uaddr, tend);
3450 vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
3451 vm_map_lock_upgrade(map);
3453 if (entry == &map->header) {
3454 map->first_free = &map->header;
3455 } else if (map->first_free->start >= start) {
3456 map->first_free = entry->prev;
3459 SAVE_HINT(map, entry->prev);
3460 vm_map_entry_delete(map, entry, &count);
3465 rv = vm_map_insert(map, &count,
3466 object, ooffset, start, tend,
3467 VM_PROT_ALL, VM_PROT_ALL, MAP_COPY_ON_WRITE);
3469 if (rv != KERN_SUCCESS)
3470 panic("vm_uiomove: could not insert new entry: %d", rv);
3474 * Map the window directly, if it is already in memory
3476 pmap_object_init_pt(map->pmap, uaddr,
3477 srcobject, oindex, tcnt, 0);
3481 vm_map_entry_release(count);
3495 * Performs the copy_on_write operations necessary to allow the virtual copies
3496 * into user space to work. This has to be called for write(2) system calls
3497 * from other processes, file unlinking, and file size shrinkage.
3500 vm_freeze_copyopts(vm_object_t object, vm_pindex_t froma, vm_pindex_t toa)
3503 vm_object_t robject;
3506 if ((object == NULL) ||
3507 ((object->flags & OBJ_OPT) == 0))
3510 if (object->shadow_count > object->ref_count)
3511 panic("vm_freeze_copyopts: sc > rc");
3513 while((robject = LIST_FIRST(&object->shadow_head)) != NULL) {
3514 vm_pindex_t bo_pindex;
3515 vm_page_t m_in, m_out;
3517 bo_pindex = OFF_TO_IDX(robject->backing_object_offset);
3519 vm_object_reference(robject);
3521 vm_object_pip_wait(robject, "objfrz");
3523 if (robject->ref_count == 1) {
3524 vm_object_deallocate(robject);
3528 vm_object_pip_add(robject, 1);
3530 for (idx = 0; idx < robject->size; idx++) {
3532 m_out = vm_page_grab(robject, idx,
3533 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
3535 if (m_out->valid == 0) {
3536 m_in = vm_page_grab(object, bo_pindex + idx,
3537 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
3538 if (m_in->valid == 0) {
3539 rv = vm_pager_get_pages(object, &m_in, 1, 0);
3540 if (rv != VM_PAGER_OK) {
3541 printf("vm_freeze_copyopts: cannot read page from file: %lx\n", (long)m_in->pindex);
3544 vm_page_deactivate(m_in);
3547 vm_page_protect(m_in, VM_PROT_NONE);
3548 pmap_copy_page(VM_PAGE_TO_PHYS(m_in), VM_PAGE_TO_PHYS(m_out));
3549 m_out->valid = m_in->valid;
3550 vm_page_dirty(m_out);
3551 vm_page_activate(m_out);
3552 vm_page_wakeup(m_in);
3554 vm_page_wakeup(m_out);
3557 object->shadow_count--;
3558 object->ref_count--;
3559 LIST_REMOVE(robject, shadow_list);
3560 robject->backing_object = NULL;
3561 robject->backing_object_offset = 0;
3563 vm_object_pip_wakeup(robject);
3564 vm_object_deallocate(robject);
3567 vm_object_clear_flag(object, OBJ_OPT);
3570 #include "opt_ddb.h"
3572 #include <sys/kernel.h>
3574 #include <ddb/ddb.h>
3577 * vm_map_print: [ debug ]
3579 DB_SHOW_COMMAND(map, vm_map_print)
3582 /* XXX convert args. */
3583 vm_map_t map = (vm_map_t)addr;
3584 boolean_t full = have_addr;
3586 vm_map_entry_t entry;
3588 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
3590 (void *)map->pmap, map->nentries, map->timestamp);
3593 if (!full && db_indent)
3597 for (entry = map->header.next; entry != &map->header;
3598 entry = entry->next) {
3599 db_iprintf("map entry %p: start=%p, end=%p\n",
3600 (void *)entry, (void *)entry->start, (void *)entry->end);
3603 static char *inheritance_name[4] =
3604 {"share", "copy", "none", "donate_copy"};
3606 db_iprintf(" prot=%x/%x/%s",
3608 entry->max_protection,
3609 inheritance_name[(int)(unsigned char)entry->inheritance]);
3610 if (entry->wired_count != 0)
3611 db_printf(", wired");
3613 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3614 /* XXX no %qd in kernel. Truncate entry->offset. */
3615 db_printf(", share=%p, offset=0x%lx\n",
3616 (void *)entry->object.sub_map,
3617 (long)entry->offset);
3619 if ((entry->prev == &map->header) ||
3620 (entry->prev->object.sub_map !=
3621 entry->object.sub_map)) {
3623 vm_map_print((db_expr_t)(intptr_t)
3624 entry->object.sub_map,
3625 full, 0, (char *)0);
3629 /* XXX no %qd in kernel. Truncate entry->offset. */
3630 db_printf(", object=%p, offset=0x%lx",
3631 (void *)entry->object.vm_object,
3632 (long)entry->offset);
3633 if (entry->eflags & MAP_ENTRY_COW)
3634 db_printf(", copy (%s)",
3635 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
3639 if ((entry->prev == &map->header) ||
3640 (entry->prev->object.vm_object !=
3641 entry->object.vm_object)) {
3643 vm_object_print((db_expr_t)(intptr_t)
3644 entry->object.vm_object,
3645 full, 0, (char *)0);
3657 DB_SHOW_COMMAND(procvm, procvm)
3662 p = (struct proc *) addr;
3667 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
3668 (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
3669 (void *)vmspace_pmap(p->p_vmspace));
3671 vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);