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.48 2006/09/12 18:41: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>
84 #include <vm/vm_param.h>
86 #include <vm/vm_map.h>
87 #include <vm/vm_page.h>
88 #include <vm/vm_object.h>
89 #include <vm/vm_pager.h>
90 #include <vm/vm_kern.h>
91 #include <vm/vm_extern.h>
92 #include <vm/swap_pager.h>
93 #include <vm/vm_zone.h>
95 #include <sys/thread2.h>
98 * Virtual memory maps provide for the mapping, protection,
99 * and sharing of virtual memory objects. In addition,
100 * this module provides for an efficient virtual copy of
101 * memory from one map to another.
103 * Synchronization is required prior to most operations.
105 * Maps consist of an ordered doubly-linked list of simple
106 * entries; a single hint is used to speed up lookups.
108 * Since portions of maps are specified by start/end addresses,
109 * which may not align with existing map entries, all
110 * routines merely "clip" entries to these start/end values.
111 * [That is, an entry is split into two, bordering at a
112 * start or end value.] Note that these clippings may not
113 * always be necessary (as the two resulting entries are then
114 * not changed); however, the clipping is done for convenience.
116 * As mentioned above, virtual copy operations are performed
117 * by copying VM object references from one map to
118 * another, and then marking both regions as copy-on-write.
124 * Initialize the vm_map module. Must be called before
125 * any other vm_map routines.
127 * Map and entry structures are allocated from the general
128 * purpose memory pool with some exceptions:
130 * - The kernel map and kmem submap are allocated statically.
131 * - Kernel map entries are allocated out of a static pool.
133 * These restrictions are necessary since malloc() uses the
134 * maps and requires map entries.
139 static struct vm_zone mapentzone_store, mapzone_store;
140 static vm_zone_t mapentzone, mapzone, vmspace_zone;
141 static struct vm_object mapentobj, mapobj;
143 static struct vm_map_entry map_entry_init[MAX_MAPENT];
144 static struct vm_map_entry cpu_map_entry_init[MAXCPU][VMEPERCPU];
145 static struct vm_map map_init[MAX_KMAP];
147 static vm_map_entry_t vm_map_entry_create(vm_map_t map, int *);
148 static void vm_map_entry_dispose (vm_map_t map, vm_map_entry_t entry, int *);
149 static void _vm_map_clip_end (vm_map_t, vm_map_entry_t, vm_offset_t, int *);
150 static void _vm_map_clip_start (vm_map_t, vm_map_entry_t, vm_offset_t, int *);
151 static void vm_map_entry_delete (vm_map_t, vm_map_entry_t, int *);
152 static void vm_map_entry_unwire (vm_map_t, vm_map_entry_t);
153 static void vm_map_copy_entry (vm_map_t, vm_map_t, vm_map_entry_t,
155 static void vm_map_split (vm_map_entry_t);
156 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);
161 mapzone = &mapzone_store;
162 zbootinit(mapzone, "MAP", sizeof (struct vm_map),
164 mapentzone = &mapentzone_store;
165 zbootinit(mapentzone, "MAP ENTRY", sizeof (struct vm_map_entry),
166 map_entry_init, MAX_MAPENT);
170 * Red black tree functions
172 static int rb_vm_map_compare(vm_map_entry_t a, vm_map_entry_t b);
173 RB_GENERATE(vm_map_rb_tree, vm_map_entry, rb_entry, rb_vm_map_compare);
175 /* a->start is address, and the only field has to be initialized */
177 rb_vm_map_compare(vm_map_entry_t a, vm_map_entry_t b)
179 if (a->start < b->start)
181 else if (a->start > b->start)
187 * Allocate a vmspace structure, including a vm_map and pmap,
188 * and initialize those structures. The refcnt is set to 1.
189 * The remaining fields must be initialized by the caller.
192 vmspace_alloc(vm_offset_t min, vm_offset_t max)
196 vm = zalloc(vmspace_zone);
197 vm_map_init(&vm->vm_map, min, max);
198 pmap_pinit(vmspace_pmap(vm));
199 vm->vm_map.pmap = vmspace_pmap(vm); /* XXX */
202 vm->vm_exitingcnt = 0;
209 zinitna(mapentzone, &mapentobj, NULL, 0, 0,
210 ZONE_USE_RESERVE | ZONE_SPECIAL, 1);
211 zinitna(mapzone, &mapobj, NULL, 0, 0, 0, 1);
212 vmspace_zone = zinit("VMSPACE", sizeof (struct vmspace), 0, 0, 3);
218 vmspace_dofree(struct vmspace *vm)
223 * Make sure any SysV shm is freed, it might not have in
228 KKASSERT(vm->vm_upcalls == NULL);
231 * Lock the map, to wait out all other references to it.
232 * Delete all of the mappings and pages they hold, then call
233 * the pmap module to reclaim anything left.
235 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
236 vm_map_lock(&vm->vm_map);
237 vm_map_delete(&vm->vm_map, vm->vm_map.min_offset,
238 vm->vm_map.max_offset, &count);
239 vm_map_unlock(&vm->vm_map);
240 vm_map_entry_release(count);
242 pmap_release(vmspace_pmap(vm));
243 zfree(vmspace_zone, vm);
247 vmspace_free(struct vmspace *vm)
249 if (vm->vm_refcnt == 0)
250 panic("vmspace_free: attempt to free already freed vmspace");
252 if (--vm->vm_refcnt == 0 && vm->vm_exitingcnt == 0)
257 vmspace_exitfree(struct proc *p)
265 * cleanup by parent process wait()ing on exiting child. vm_refcnt
266 * may not be 0 (e.g. fork() and child exits without exec()ing).
267 * exitingcnt may increment above 0 and drop back down to zero
268 * several times while vm_refcnt is held non-zero. vm_refcnt
269 * may also increment above 0 and drop back down to zero several
270 * times while vm_exitingcnt is held non-zero.
272 * The last wait on the exiting child's vmspace will clean up
273 * the remainder of the vmspace.
275 if (--vm->vm_exitingcnt == 0 && vm->vm_refcnt == 0)
280 * vmspace_swap_count() - count the approximate swap useage in pages for a
283 * Swap useage is determined by taking the proportional swap used by
284 * VM objects backing the VM map. To make up for fractional losses,
285 * if the VM object has any swap use at all the associated map entries
286 * count for at least 1 swap page.
289 vmspace_swap_count(struct vmspace *vmspace)
291 vm_map_t map = &vmspace->vm_map;
297 for (cur = map->header.next; cur != &map->header; cur = cur->next) {
298 switch(cur->maptype) {
299 case VM_MAPTYPE_NORMAL:
300 case VM_MAPTYPE_VPAGETABLE:
301 if ((object = cur->object.vm_object) == NULL)
303 if (object->type != OBJT_SWAP)
305 n = (cur->end - cur->start) / PAGE_SIZE;
306 if (object->un_pager.swp.swp_bcount) {
307 count += object->un_pager.swp.swp_bcount *
308 SWAP_META_PAGES * n / object->size + 1;
322 * Creates and returns a new empty VM map with
323 * the given physical map structure, and having
324 * the given lower and upper address bounds.
327 vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max)
331 result = zalloc(mapzone);
332 vm_map_init(result, min, max);
338 * Initialize an existing vm_map structure
339 * such as that in the vmspace structure.
340 * The pmap is set elsewhere.
343 vm_map_init(struct vm_map *map, vm_offset_t min, vm_offset_t max)
345 map->header.next = map->header.prev = &map->header;
346 RB_INIT(&map->rb_root);
351 map->min_offset = min;
352 map->max_offset = max;
353 map->first_free = &map->header;
354 map->hint = &map->header;
356 lockinit(&map->lock, "thrd_sleep", 0, 0);
360 * vm_map_entry_reserve_cpu_init:
362 * Set an initial negative count so the first attempt to reserve
363 * space preloads a bunch of vm_map_entry's for this cpu. Also
364 * pre-allocate 2 vm_map_entries which will be needed by zalloc() to
365 * map a new page for vm_map_entry structures. SMP systems are
366 * particularly sensitive.
368 * This routine is called in early boot so we cannot just call
369 * vm_map_entry_reserve().
371 * May be called for a gd other then mycpu, but may only be called
375 vm_map_entry_reserve_cpu_init(globaldata_t gd)
377 vm_map_entry_t entry;
380 gd->gd_vme_avail -= MAP_RESERVE_COUNT * 2;
381 entry = &cpu_map_entry_init[gd->gd_cpuid][0];
382 for (i = 0; i < VMEPERCPU; ++i, ++entry) {
383 entry->next = gd->gd_vme_base;
384 gd->gd_vme_base = entry;
389 * vm_map_entry_reserve:
391 * Reserves vm_map_entry structures so code later on can manipulate
392 * map_entry structures within a locked map without blocking trying
393 * to allocate a new vm_map_entry.
396 vm_map_entry_reserve(int count)
398 struct globaldata *gd = mycpu;
399 vm_map_entry_t entry;
404 * Make sure we have enough structures in gd_vme_base to handle
405 * the reservation request.
407 while (gd->gd_vme_avail < count) {
408 entry = zalloc(mapentzone);
409 entry->next = gd->gd_vme_base;
410 gd->gd_vme_base = entry;
413 gd->gd_vme_avail -= count;
419 * vm_map_entry_release:
421 * Releases previously reserved vm_map_entry structures that were not
422 * used. If we have too much junk in our per-cpu cache clean some of
426 vm_map_entry_release(int count)
428 struct globaldata *gd = mycpu;
429 vm_map_entry_t entry;
432 gd->gd_vme_avail += count;
433 while (gd->gd_vme_avail > MAP_RESERVE_SLOP) {
434 entry = gd->gd_vme_base;
435 KKASSERT(entry != NULL);
436 gd->gd_vme_base = entry->next;
439 zfree(mapentzone, entry);
446 * vm_map_entry_kreserve:
448 * Reserve map entry structures for use in kernel_map itself. These
449 * entries have *ALREADY* been reserved on a per-cpu basis when the map
450 * was inited. This function is used by zalloc() to avoid a recursion
451 * when zalloc() itself needs to allocate additional kernel memory.
453 * This function works like the normal reserve but does not load the
454 * vm_map_entry cache (because that would result in an infinite
455 * recursion). Note that gd_vme_avail may go negative. This is expected.
457 * Any caller of this function must be sure to renormalize after
458 * potentially eating entries to ensure that the reserve supply
462 vm_map_entry_kreserve(int count)
464 struct globaldata *gd = mycpu;
467 gd->gd_vme_avail -= count;
469 KASSERT(gd->gd_vme_base != NULL, ("no reserved entries left, gd_vme_avail = %d\n", gd->gd_vme_avail));
474 * vm_map_entry_krelease:
476 * Release previously reserved map entries for kernel_map. We do not
477 * attempt to clean up like the normal release function as this would
478 * cause an unnecessary (but probably not fatal) deep procedure call.
481 vm_map_entry_krelease(int count)
483 struct globaldata *gd = mycpu;
486 gd->gd_vme_avail += count;
491 * vm_map_entry_create: [ internal use only ]
493 * Allocates a VM map entry for insertion. No entry fields are filled
496 * This routine may be called from an interrupt thread but not a FAST
497 * interrupt. This routine may recurse the map lock.
499 static vm_map_entry_t
500 vm_map_entry_create(vm_map_t map, int *countp)
502 struct globaldata *gd = mycpu;
503 vm_map_entry_t entry;
505 KKASSERT(*countp > 0);
508 entry = gd->gd_vme_base;
509 KASSERT(entry != NULL, ("gd_vme_base NULL! count %d", *countp));
510 gd->gd_vme_base = entry->next;
516 * vm_map_entry_dispose: [ internal use only ]
518 * Dispose of a vm_map_entry that is no longer being referenced. This
519 * function may be called from an interrupt.
522 vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry, int *countp)
524 struct globaldata *gd = mycpu;
526 KKASSERT(map->hint != entry);
527 KKASSERT(map->first_free != entry);
531 entry->next = gd->gd_vme_base;
532 gd->gd_vme_base = entry;
538 * vm_map_entry_{un,}link:
540 * Insert/remove entries from maps.
543 vm_map_entry_link(vm_map_t map,
544 vm_map_entry_t after_where,
545 vm_map_entry_t entry)
548 entry->prev = after_where;
549 entry->next = after_where->next;
550 entry->next->prev = entry;
551 after_where->next = entry;
552 if (vm_map_rb_tree_RB_INSERT(&map->rb_root, entry))
553 panic("vm_map_entry_link: dup addr map %p ent %p", map, entry);
557 vm_map_entry_unlink(vm_map_t map,
558 vm_map_entry_t entry)
563 if (entry->eflags & MAP_ENTRY_IN_TRANSITION)
564 panic("vm_map_entry_unlink: attempt to mess with locked entry! %p", entry);
569 vm_map_rb_tree_RB_REMOVE(&map->rb_root, entry);
574 * vm_map_lookup_entry: [ internal use only ]
576 * Finds the map entry containing (or
577 * immediately preceding) the specified address
578 * in the given map; the entry is returned
579 * in the "entry" parameter. The boolean
580 * result indicates whether the address is
581 * actually contained in the map.
584 vm_map_lookup_entry(vm_map_t map, vm_offset_t address,
585 vm_map_entry_t *entry /* OUT */)
592 * XXX TEMPORARILY DISABLED. For some reason our attempt to revive
593 * the hint code with the red-black lookup meets with system crashes
594 * and lockups. We do not yet know why.
596 * It is possible that the problem is related to the setting
597 * of the hint during map_entry deletion, in the code specified
598 * at the GGG comment later on in this file.
601 * Quickly check the cached hint, there's a good chance of a match.
603 if (map->hint != &map->header) {
605 if (address >= tmp->start && address < tmp->end) {
613 * Locate the record from the top of the tree. 'last' tracks the
614 * closest prior record and is returned if no match is found, which
615 * in binary tree terms means tracking the most recent right-branch
616 * taken. If there is no prior record, &map->header is returned.
619 tmp = RB_ROOT(&map->rb_root);
622 if (address >= tmp->start) {
623 if (address < tmp->end) {
629 tmp = RB_RIGHT(tmp, rb_entry);
631 tmp = RB_LEFT(tmp, rb_entry);
641 * Inserts the given whole VM object into the target
642 * map at the specified address range. The object's
643 * size should match that of the address range.
645 * Requires that the map be locked, and leaves it so. Requires that
646 * sufficient vm_map_entry structures have been reserved and tracks
647 * the use via countp.
649 * If object is non-NULL, ref count must be bumped by caller
650 * prior to making call to account for the new entry.
653 vm_map_insert(vm_map_t map, int *countp,
654 vm_object_t object, vm_ooffset_t offset,
655 vm_offset_t start, vm_offset_t end,
656 vm_maptype_t maptype,
657 vm_prot_t prot, vm_prot_t max,
660 vm_map_entry_t new_entry;
661 vm_map_entry_t prev_entry;
662 vm_map_entry_t temp_entry;
663 vm_eflags_t protoeflags;
666 * Check that the start and end points are not bogus.
669 if ((start < map->min_offset) || (end > map->max_offset) ||
671 return (KERN_INVALID_ADDRESS);
674 * Find the entry prior to the proposed starting address; if it's part
675 * of an existing entry, this range is bogus.
678 if (vm_map_lookup_entry(map, start, &temp_entry))
679 return (KERN_NO_SPACE);
681 prev_entry = temp_entry;
684 * Assert that the next entry doesn't overlap the end point.
687 if ((prev_entry->next != &map->header) &&
688 (prev_entry->next->start < end))
689 return (KERN_NO_SPACE);
693 if (cow & MAP_COPY_ON_WRITE)
694 protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
696 if (cow & MAP_NOFAULT) {
697 protoeflags |= MAP_ENTRY_NOFAULT;
699 KASSERT(object == NULL,
700 ("vm_map_insert: paradoxical MAP_NOFAULT request"));
702 if (cow & MAP_DISABLE_SYNCER)
703 protoeflags |= MAP_ENTRY_NOSYNC;
704 if (cow & MAP_DISABLE_COREDUMP)
705 protoeflags |= MAP_ENTRY_NOCOREDUMP;
709 * When object is non-NULL, it could be shared with another
710 * process. We have to set or clear OBJ_ONEMAPPING
713 if ((object->ref_count > 1) || (object->shadow_count != 0)) {
714 vm_object_clear_flag(object, OBJ_ONEMAPPING);
717 else if ((prev_entry != &map->header) &&
718 (prev_entry->eflags == protoeflags) &&
719 (prev_entry->end == start) &&
720 (prev_entry->wired_count == 0) &&
721 prev_entry->maptype == maptype &&
722 ((prev_entry->object.vm_object == NULL) ||
723 vm_object_coalesce(prev_entry->object.vm_object,
724 OFF_TO_IDX(prev_entry->offset),
725 (vm_size_t)(prev_entry->end - prev_entry->start),
726 (vm_size_t)(end - prev_entry->end)))) {
728 * We were able to extend the object. Determine if we
729 * can extend the previous map entry to include the
732 if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
733 (prev_entry->protection == prot) &&
734 (prev_entry->max_protection == max)) {
735 map->size += (end - prev_entry->end);
736 prev_entry->end = end;
737 vm_map_simplify_entry(map, prev_entry, countp);
738 return (KERN_SUCCESS);
742 * If we can extend the object but cannot extend the
743 * map entry, we have to create a new map entry. We
744 * must bump the ref count on the extended object to
745 * account for it. object may be NULL.
747 object = prev_entry->object.vm_object;
748 offset = prev_entry->offset +
749 (prev_entry->end - prev_entry->start);
750 vm_object_reference(object);
754 * NOTE: if conditionals fail, object can be NULL here. This occurs
755 * in things like the buffer map where we manage kva but do not manage
763 new_entry = vm_map_entry_create(map, countp);
764 new_entry->start = start;
765 new_entry->end = end;
767 new_entry->maptype = maptype;
768 new_entry->eflags = protoeflags;
769 new_entry->object.vm_object = object;
770 new_entry->offset = offset;
771 new_entry->avail_ssize = 0;
773 new_entry->inheritance = VM_INHERIT_DEFAULT;
774 new_entry->protection = prot;
775 new_entry->max_protection = max;
776 new_entry->wired_count = 0;
779 * Insert the new entry into the list
782 vm_map_entry_link(map, prev_entry, new_entry);
783 map->size += new_entry->end - new_entry->start;
786 * Update the free space hint
788 if ((map->first_free == prev_entry) &&
789 (prev_entry->end >= new_entry->start)) {
790 map->first_free = new_entry;
795 * Temporarily removed to avoid MAP_STACK panic, due to
796 * MAP_STACK being a huge hack. Will be added back in
797 * when MAP_STACK (and the user stack mapping) is fixed.
800 * It may be possible to simplify the entry
802 vm_map_simplify_entry(map, new_entry, countp);
805 if (cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) {
806 pmap_object_init_pt(map->pmap, start, prot,
807 object, OFF_TO_IDX(offset), end - start,
808 cow & MAP_PREFAULT_PARTIAL);
811 return (KERN_SUCCESS);
815 * Find sufficient space for `length' bytes in the given map, starting at
816 * `start'. The map must be locked. Returns 0 on success, 1 on no space.
818 * This function will returned an arbitrarily aligned pointer. If no
819 * particular alignment is required you should pass align as 1. Note that
820 * the map may return PAGE_SIZE aligned pointers if all the lengths used in
821 * the map are a multiple of PAGE_SIZE, even if you pass a smaller align
824 * 'align' should be a power of 2 but is not required to be.
834 vm_map_entry_t entry, next;
836 vm_offset_t align_mask;
838 if (start < map->min_offset)
839 start = map->min_offset;
840 if (start > map->max_offset)
844 * If the alignment is not a power of 2 we will have to use
845 * a mod/division, set align_mask to a special value.
847 if ((align | (align - 1)) + 1 != (align << 1))
848 align_mask = (vm_offset_t)-1;
850 align_mask = align - 1;
854 * Look for the first possible address; if there's already something
855 * at this address, we have to start after it.
857 if (start == map->min_offset) {
858 if ((entry = map->first_free) != &map->header)
863 if (vm_map_lookup_entry(map, start, &tmp))
869 * Look through the rest of the map, trying to fit a new region in the
870 * gap between existing regions, or after the very last region.
872 for (;; start = (entry = next)->end) {
874 * Adjust the proposed start by the requested alignment,
875 * be sure that we didn't wrap the address.
877 if (align_mask == (vm_offset_t)-1)
878 end = ((start + align - 1) / align) * align;
880 end = (start + align_mask) & ~align_mask;
885 * Find the end of the proposed new region. Be sure we didn't
886 * go beyond the end of the map, or wrap around the address.
887 * Then check to see if this is the last entry or if the
888 * proposed end fits in the gap between this and the next
891 end = start + length;
892 if (end > map->max_offset || end < start)
895 if (next == &map->header || next->start >= end)
899 if (map == kernel_map) {
901 if ((ksize = round_page(start + length)) > kernel_vm_end) {
902 pmap_growkernel(ksize);
911 * vm_map_find finds an unallocated region in the target address
912 * map with the given length. The search is defined to be
913 * first-fit from the specified address; the region found is
914 * returned in the same parameter.
916 * If object is non-NULL, ref count must be bumped by caller
917 * prior to making call to account for the new entry.
920 vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
921 vm_offset_t *addr, vm_size_t length,
922 boolean_t find_space,
923 vm_maptype_t maptype,
924 vm_prot_t prot, vm_prot_t max,
933 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
936 if (vm_map_findspace(map, start, length, 1, addr)) {
938 vm_map_entry_release(count);
939 return (KERN_NO_SPACE);
943 result = vm_map_insert(map, &count, object, offset,
944 start, start + length,
949 vm_map_entry_release(count);
955 * vm_map_simplify_entry:
957 * Simplify the given map entry by merging with either neighbor. This
958 * routine also has the ability to merge with both neighbors.
960 * The map must be locked.
962 * This routine guarentees that the passed entry remains valid (though
963 * possibly extended). When merging, this routine may delete one or
964 * both neighbors. No action is taken on entries which have their
965 * in-transition flag set.
968 vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry, int *countp)
970 vm_map_entry_t next, prev;
971 vm_size_t prevsize, esize;
973 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
974 ++mycpu->gd_cnt.v_intrans_coll;
978 if (entry->maptype == VM_MAPTYPE_SUBMAP)
982 if (prev != &map->header) {
983 prevsize = prev->end - prev->start;
984 if ( (prev->end == entry->start) &&
985 (prev->maptype == entry->maptype) &&
986 (prev->object.vm_object == entry->object.vm_object) &&
987 (!prev->object.vm_object ||
988 (prev->offset + prevsize == entry->offset)) &&
989 (prev->eflags == entry->eflags) &&
990 (prev->protection == entry->protection) &&
991 (prev->max_protection == entry->max_protection) &&
992 (prev->inheritance == entry->inheritance) &&
993 (prev->wired_count == entry->wired_count)) {
994 if (map->first_free == prev)
995 map->first_free = entry;
996 if (map->hint == prev)
998 vm_map_entry_unlink(map, prev);
999 entry->start = prev->start;
1000 entry->offset = prev->offset;
1001 if (prev->object.vm_object)
1002 vm_object_deallocate(prev->object.vm_object);
1003 vm_map_entry_dispose(map, prev, countp);
1008 if (next != &map->header) {
1009 esize = entry->end - entry->start;
1010 if ((entry->end == next->start) &&
1011 (next->maptype == entry->maptype) &&
1012 (next->object.vm_object == entry->object.vm_object) &&
1013 (!entry->object.vm_object ||
1014 (entry->offset + esize == next->offset)) &&
1015 (next->eflags == entry->eflags) &&
1016 (next->protection == entry->protection) &&
1017 (next->max_protection == entry->max_protection) &&
1018 (next->inheritance == entry->inheritance) &&
1019 (next->wired_count == entry->wired_count)) {
1020 if (map->first_free == next)
1021 map->first_free = entry;
1022 if (map->hint == next)
1024 vm_map_entry_unlink(map, next);
1025 entry->end = next->end;
1026 if (next->object.vm_object)
1027 vm_object_deallocate(next->object.vm_object);
1028 vm_map_entry_dispose(map, next, countp);
1033 * vm_map_clip_start: [ internal use only ]
1035 * Asserts that the given entry begins at or after
1036 * the specified address; if necessary,
1037 * it splits the entry into two.
1039 #define vm_map_clip_start(map, entry, startaddr, countp) \
1041 if (startaddr > entry->start) \
1042 _vm_map_clip_start(map, entry, startaddr, countp); \
1046 * This routine is called only when it is known that
1047 * the entry must be split.
1050 _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start, int *countp)
1052 vm_map_entry_t new_entry;
1055 * Split off the front portion -- note that we must insert the new
1056 * entry BEFORE this one, so that this entry has the specified
1060 vm_map_simplify_entry(map, entry, countp);
1063 * If there is no object backing this entry, we might as well create
1064 * one now. If we defer it, an object can get created after the map
1065 * is clipped, and individual objects will be created for the split-up
1066 * map. This is a bit of a hack, but is also about the best place to
1067 * put this improvement.
1070 if (entry->object.vm_object == NULL && !map->system_map) {
1072 object = vm_object_allocate(OBJT_DEFAULT,
1073 atop(entry->end - entry->start));
1074 entry->object.vm_object = object;
1078 new_entry = vm_map_entry_create(map, countp);
1079 *new_entry = *entry;
1081 new_entry->end = start;
1082 entry->offset += (start - entry->start);
1083 entry->start = start;
1085 vm_map_entry_link(map, entry->prev, new_entry);
1087 switch(entry->maptype) {
1088 case VM_MAPTYPE_NORMAL:
1089 case VM_MAPTYPE_VPAGETABLE:
1090 vm_object_reference(new_entry->object.vm_object);
1098 * vm_map_clip_end: [ internal use only ]
1100 * Asserts that the given entry ends at or before
1101 * the specified address; if necessary,
1102 * it splits the entry into two.
1105 #define vm_map_clip_end(map, entry, endaddr, countp) \
1107 if (endaddr < entry->end) \
1108 _vm_map_clip_end(map, entry, endaddr, countp); \
1112 * This routine is called only when it is known that
1113 * the entry must be split.
1116 _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end, int *countp)
1118 vm_map_entry_t new_entry;
1121 * If there is no object backing this entry, we might as well create
1122 * one now. If we defer it, an object can get created after the map
1123 * is clipped, and individual objects will be created for the split-up
1124 * map. This is a bit of a hack, but is also about the best place to
1125 * put this improvement.
1128 if (entry->object.vm_object == NULL && !map->system_map) {
1130 object = vm_object_allocate(OBJT_DEFAULT,
1131 atop(entry->end - entry->start));
1132 entry->object.vm_object = object;
1137 * Create a new entry and insert it AFTER the specified entry
1140 new_entry = vm_map_entry_create(map, countp);
1141 *new_entry = *entry;
1143 new_entry->start = entry->end = end;
1144 new_entry->offset += (end - entry->start);
1146 vm_map_entry_link(map, entry, new_entry);
1148 switch(entry->maptype) {
1149 case VM_MAPTYPE_NORMAL:
1150 case VM_MAPTYPE_VPAGETABLE:
1151 vm_object_reference(new_entry->object.vm_object);
1159 * VM_MAP_RANGE_CHECK: [ internal use only ]
1161 * Asserts that the starting and ending region
1162 * addresses fall within the valid range of the map.
1164 #define VM_MAP_RANGE_CHECK(map, start, end) \
1166 if (start < vm_map_min(map)) \
1167 start = vm_map_min(map); \
1168 if (end > vm_map_max(map)) \
1169 end = vm_map_max(map); \
1175 * vm_map_transition_wait: [ kernel use only ]
1177 * Used to block when an in-transition collison occurs. The map
1178 * is unlocked for the sleep and relocked before the return.
1182 vm_map_transition_wait(vm_map_t map)
1185 tsleep(map, 0, "vment", 0);
1193 * When we do blocking operations with the map lock held it is
1194 * possible that a clip might have occured on our in-transit entry,
1195 * requiring an adjustment to the entry in our loop. These macros
1196 * help the pageable and clip_range code deal with the case. The
1197 * conditional costs virtually nothing if no clipping has occured.
1200 #define CLIP_CHECK_BACK(entry, save_start) \
1202 while (entry->start != save_start) { \
1203 entry = entry->prev; \
1204 KASSERT(entry != &map->header, ("bad entry clip")); \
1208 #define CLIP_CHECK_FWD(entry, save_end) \
1210 while (entry->end != save_end) { \
1211 entry = entry->next; \
1212 KASSERT(entry != &map->header, ("bad entry clip")); \
1218 * vm_map_clip_range: [ kernel use only ]
1220 * Clip the specified range and return the base entry. The
1221 * range may cover several entries starting at the returned base
1222 * and the first and last entry in the covering sequence will be
1223 * properly clipped to the requested start and end address.
1225 * If no holes are allowed you should pass the MAP_CLIP_NO_HOLES
1228 * The MAP_ENTRY_IN_TRANSITION flag will be set for the entries
1229 * covered by the requested range.
1231 * The map must be exclusively locked on entry and will remain locked
1232 * on return. If no range exists or the range contains holes and you
1233 * specified that no holes were allowed, NULL will be returned. This
1234 * routine may temporarily unlock the map in order avoid a deadlock when
1239 vm_map_clip_range(vm_map_t map, vm_offset_t start, vm_offset_t end,
1240 int *countp, int flags)
1242 vm_map_entry_t start_entry;
1243 vm_map_entry_t entry;
1246 * Locate the entry and effect initial clipping. The in-transition
1247 * case does not occur very often so do not try to optimize it.
1250 if (vm_map_lookup_entry(map, start, &start_entry) == FALSE)
1252 entry = start_entry;
1253 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1254 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1255 ++mycpu->gd_cnt.v_intrans_coll;
1256 ++mycpu->gd_cnt.v_intrans_wait;
1257 vm_map_transition_wait(map);
1259 * entry and/or start_entry may have been clipped while
1260 * we slept, or may have gone away entirely. We have
1261 * to restart from the lookup.
1266 * Since we hold an exclusive map lock we do not have to restart
1267 * after clipping, even though clipping may block in zalloc.
1269 vm_map_clip_start(map, entry, start, countp);
1270 vm_map_clip_end(map, entry, end, countp);
1271 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
1274 * Scan entries covered by the range. When working on the next
1275 * entry a restart need only re-loop on the current entry which
1276 * we have already locked, since 'next' may have changed. Also,
1277 * even though entry is safe, it may have been clipped so we
1278 * have to iterate forwards through the clip after sleeping.
1280 while (entry->next != &map->header && entry->next->start < end) {
1281 vm_map_entry_t next = entry->next;
1283 if (flags & MAP_CLIP_NO_HOLES) {
1284 if (next->start > entry->end) {
1285 vm_map_unclip_range(map, start_entry,
1286 start, entry->end, countp, flags);
1291 if (next->eflags & MAP_ENTRY_IN_TRANSITION) {
1292 vm_offset_t save_end = entry->end;
1293 next->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1294 ++mycpu->gd_cnt.v_intrans_coll;
1295 ++mycpu->gd_cnt.v_intrans_wait;
1296 vm_map_transition_wait(map);
1299 * clips might have occured while we blocked.
1301 CLIP_CHECK_FWD(entry, save_end);
1302 CLIP_CHECK_BACK(start_entry, start);
1306 * No restart necessary even though clip_end may block, we
1307 * are holding the map lock.
1309 vm_map_clip_end(map, next, end, countp);
1310 next->eflags |= MAP_ENTRY_IN_TRANSITION;
1313 if (flags & MAP_CLIP_NO_HOLES) {
1314 if (entry->end != end) {
1315 vm_map_unclip_range(map, start_entry,
1316 start, entry->end, countp, flags);
1320 return(start_entry);
1324 * vm_map_unclip_range: [ kernel use only ]
1326 * Undo the effect of vm_map_clip_range(). You should pass the same
1327 * flags and the same range that you passed to vm_map_clip_range().
1328 * This code will clear the in-transition flag on the entries and
1329 * wake up anyone waiting. This code will also simplify the sequence
1330 * and attempt to merge it with entries before and after the sequence.
1332 * The map must be locked on entry and will remain locked on return.
1334 * Note that you should also pass the start_entry returned by
1335 * vm_map_clip_range(). However, if you block between the two calls
1336 * with the map unlocked please be aware that the start_entry may
1337 * have been clipped and you may need to scan it backwards to find
1338 * the entry corresponding with the original start address. You are
1339 * responsible for this, vm_map_unclip_range() expects the correct
1340 * start_entry to be passed to it and will KASSERT otherwise.
1344 vm_map_unclip_range(
1346 vm_map_entry_t start_entry,
1352 vm_map_entry_t entry;
1354 entry = start_entry;
1356 KASSERT(entry->start == start, ("unclip_range: illegal base entry"));
1357 while (entry != &map->header && entry->start < end) {
1358 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION, ("in-transition flag not set during unclip on: %p", entry));
1359 KASSERT(entry->end <= end, ("unclip_range: tail wasn't clipped"));
1360 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
1361 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
1362 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
1365 entry = entry->next;
1369 * Simplification does not block so there is no restart case.
1371 entry = start_entry;
1372 while (entry != &map->header && entry->start < end) {
1373 vm_map_simplify_entry(map, entry, countp);
1374 entry = entry->next;
1379 * vm_map_submap: [ kernel use only ]
1381 * Mark the given range as handled by a subordinate map.
1383 * This range must have been created with vm_map_find,
1384 * and no other operations may have been performed on this
1385 * range prior to calling vm_map_submap.
1387 * Only a limited number of operations can be performed
1388 * within this rage after calling vm_map_submap:
1390 * [Don't try vm_map_copy!]
1392 * To remove a submapping, one must first remove the
1393 * range from the superior map, and then destroy the
1394 * submap (if desired). [Better yet, don't try it.]
1397 vm_map_submap(vm_map_t map, vm_offset_t start, vm_offset_t end, vm_map_t submap)
1399 vm_map_entry_t entry;
1400 int result = KERN_INVALID_ARGUMENT;
1403 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1406 VM_MAP_RANGE_CHECK(map, start, end);
1408 if (vm_map_lookup_entry(map, start, &entry)) {
1409 vm_map_clip_start(map, entry, start, &count);
1411 entry = entry->next;
1414 vm_map_clip_end(map, entry, end, &count);
1416 if ((entry->start == start) && (entry->end == end) &&
1417 ((entry->eflags & MAP_ENTRY_COW) == 0) &&
1418 (entry->object.vm_object == NULL)) {
1419 entry->object.sub_map = submap;
1420 entry->maptype = VM_MAPTYPE_SUBMAP;
1421 result = KERN_SUCCESS;
1424 vm_map_entry_release(count);
1432 * Sets the protection of the specified address region in the target map.
1433 * If "set_max" is specified, the maximum protection is to be set;
1434 * otherwise, only the current protection is affected.
1436 * The protection is not applicable to submaps, but is applicable to normal
1437 * maps and maps governed by virtual page tables. For example, when operating
1438 * on a virtual page table our protection basically controls how COW occurs
1439 * on the backing object, whereas the virtual page table abstraction itself
1440 * is an abstraction for userland.
1443 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
1444 vm_prot_t new_prot, boolean_t set_max)
1446 vm_map_entry_t current;
1447 vm_map_entry_t entry;
1450 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1453 VM_MAP_RANGE_CHECK(map, start, end);
1455 if (vm_map_lookup_entry(map, start, &entry)) {
1456 vm_map_clip_start(map, entry, start, &count);
1458 entry = entry->next;
1462 * Make a first pass to check for protection violations.
1465 while ((current != &map->header) && (current->start < end)) {
1466 if (current->maptype == VM_MAPTYPE_SUBMAP) {
1468 vm_map_entry_release(count);
1469 return (KERN_INVALID_ARGUMENT);
1471 if ((new_prot & current->max_protection) != new_prot) {
1473 vm_map_entry_release(count);
1474 return (KERN_PROTECTION_FAILURE);
1476 current = current->next;
1480 * Go back and fix up protections. [Note that clipping is not
1481 * necessary the second time.]
1485 while ((current != &map->header) && (current->start < end)) {
1488 vm_map_clip_end(map, current, end, &count);
1490 old_prot = current->protection;
1492 current->protection =
1493 (current->max_protection = new_prot) &
1496 current->protection = new_prot;
1500 * Update physical map if necessary. Worry about copy-on-write
1501 * here -- CHECK THIS XXX
1504 if (current->protection != old_prot) {
1505 #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1508 pmap_protect(map->pmap, current->start,
1510 current->protection & MASK(current));
1514 vm_map_simplify_entry(map, current, &count);
1516 current = current->next;
1520 vm_map_entry_release(count);
1521 return (KERN_SUCCESS);
1527 * This routine traverses a processes map handling the madvise
1528 * system call. Advisories are classified as either those effecting
1529 * the vm_map_entry structure, or those effecting the underlying
1534 vm_map_madvise(vm_map_t map, vm_offset_t start, vm_offset_t end, int behav)
1536 vm_map_entry_t current, entry;
1541 * Some madvise calls directly modify the vm_map_entry, in which case
1542 * we need to use an exclusive lock on the map and we need to perform
1543 * various clipping operations. Otherwise we only need a read-lock
1547 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1551 case MADV_SEQUENTIAL:
1563 vm_map_lock_read(map);
1566 vm_map_entry_release(count);
1567 return (KERN_INVALID_ARGUMENT);
1571 * Locate starting entry and clip if necessary.
1574 VM_MAP_RANGE_CHECK(map, start, end);
1576 if (vm_map_lookup_entry(map, start, &entry)) {
1578 vm_map_clip_start(map, entry, start, &count);
1580 entry = entry->next;
1585 * madvise behaviors that are implemented in the vm_map_entry.
1587 * We clip the vm_map_entry so that behavioral changes are
1588 * limited to the specified address range.
1590 for (current = entry;
1591 (current != &map->header) && (current->start < end);
1592 current = current->next
1594 if (current->maptype == VM_MAPTYPE_SUBMAP)
1597 vm_map_clip_end(map, current, end, &count);
1601 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
1603 case MADV_SEQUENTIAL:
1604 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
1607 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
1610 current->eflags |= MAP_ENTRY_NOSYNC;
1613 current->eflags &= ~MAP_ENTRY_NOSYNC;
1616 current->eflags |= MAP_ENTRY_NOCOREDUMP;
1619 current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
1624 vm_map_simplify_entry(map, current, &count);
1632 * madvise behaviors that are implemented in the underlying
1635 * Since we don't clip the vm_map_entry, we have to clip
1636 * the vm_object pindex and count.
1638 * NOTE! We currently do not support these functions on
1639 * virtual page tables.
1641 for (current = entry;
1642 (current != &map->header) && (current->start < end);
1643 current = current->next
1645 vm_offset_t useStart;
1647 if (current->maptype != VM_MAPTYPE_NORMAL)
1650 pindex = OFF_TO_IDX(current->offset);
1651 count = atop(current->end - current->start);
1652 useStart = current->start;
1654 if (current->start < start) {
1655 pindex += atop(start - current->start);
1656 count -= atop(start - current->start);
1659 if (current->end > end)
1660 count -= atop(current->end - end);
1665 vm_object_madvise(current->object.vm_object,
1666 pindex, count, behav);
1667 if (behav == MADV_WILLNEED) {
1668 pmap_object_init_pt(
1671 current->protection,
1672 current->object.vm_object,
1674 (count << PAGE_SHIFT),
1675 MAP_PREFAULT_MADVISE
1679 vm_map_unlock_read(map);
1681 vm_map_entry_release(count);
1689 * Sets the inheritance of the specified address
1690 * range in the target map. Inheritance
1691 * affects how the map will be shared with
1692 * child maps at the time of vm_map_fork.
1695 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
1696 vm_inherit_t new_inheritance)
1698 vm_map_entry_t entry;
1699 vm_map_entry_t temp_entry;
1702 switch (new_inheritance) {
1703 case VM_INHERIT_NONE:
1704 case VM_INHERIT_COPY:
1705 case VM_INHERIT_SHARE:
1708 return (KERN_INVALID_ARGUMENT);
1711 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1714 VM_MAP_RANGE_CHECK(map, start, end);
1716 if (vm_map_lookup_entry(map, start, &temp_entry)) {
1718 vm_map_clip_start(map, entry, start, &count);
1720 entry = temp_entry->next;
1722 while ((entry != &map->header) && (entry->start < end)) {
1723 vm_map_clip_end(map, entry, end, &count);
1725 entry->inheritance = new_inheritance;
1727 vm_map_simplify_entry(map, entry, &count);
1729 entry = entry->next;
1732 vm_map_entry_release(count);
1733 return (KERN_SUCCESS);
1737 * Implement the semantics of mlock
1740 vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t real_end,
1741 boolean_t new_pageable)
1743 vm_map_entry_t entry;
1744 vm_map_entry_t start_entry;
1746 int rv = KERN_SUCCESS;
1749 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1751 VM_MAP_RANGE_CHECK(map, start, real_end);
1754 start_entry = vm_map_clip_range(map, start, end, &count, MAP_CLIP_NO_HOLES);
1755 if (start_entry == NULL) {
1757 vm_map_entry_release(count);
1758 return (KERN_INVALID_ADDRESS);
1761 if (new_pageable == 0) {
1762 entry = start_entry;
1763 while ((entry != &map->header) && (entry->start < end)) {
1764 vm_offset_t save_start;
1765 vm_offset_t save_end;
1768 * Already user wired or hard wired (trivial cases)
1770 if (entry->eflags & MAP_ENTRY_USER_WIRED) {
1771 entry = entry->next;
1774 if (entry->wired_count != 0) {
1775 entry->wired_count++;
1776 entry->eflags |= MAP_ENTRY_USER_WIRED;
1777 entry = entry->next;
1782 * A new wiring requires instantiation of appropriate
1783 * management structures and the faulting in of the
1786 if (entry->maptype != VM_MAPTYPE_SUBMAP) {
1787 int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY;
1788 if (copyflag && ((entry->protection & VM_PROT_WRITE) != 0)) {
1790 vm_object_shadow(&entry->object.vm_object,
1792 atop(entry->end - entry->start));
1793 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
1795 } else if (entry->object.vm_object == NULL &&
1798 entry->object.vm_object =
1799 vm_object_allocate(OBJT_DEFAULT,
1800 atop(entry->end - entry->start));
1801 entry->offset = (vm_offset_t) 0;
1805 entry->wired_count++;
1806 entry->eflags |= MAP_ENTRY_USER_WIRED;
1809 * Now fault in the area. Note that vm_fault_wire()
1810 * may release the map lock temporarily, it will be
1811 * relocked on return. The in-transition
1812 * flag protects the entries.
1814 save_start = entry->start;
1815 save_end = entry->end;
1816 rv = vm_fault_wire(map, entry, TRUE);
1818 CLIP_CHECK_BACK(entry, save_start);
1820 KASSERT(entry->wired_count == 1, ("bad wired_count on entry"));
1821 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
1822 entry->wired_count = 0;
1823 if (entry->end == save_end)
1825 entry = entry->next;
1826 KASSERT(entry != &map->header, ("bad entry clip during backout"));
1828 end = save_start; /* unwire the rest */
1832 * note that even though the entry might have been
1833 * clipped, the USER_WIRED flag we set prevents
1834 * duplication so we do not have to do a
1837 entry = entry->next;
1841 * If we failed fall through to the unwiring section to
1842 * unwire what we had wired so far. 'end' has already
1849 * start_entry might have been clipped if we unlocked the
1850 * map and blocked. No matter how clipped it has gotten
1851 * there should be a fragment that is on our start boundary.
1853 CLIP_CHECK_BACK(start_entry, start);
1857 * Deal with the unwiring case.
1861 * This is the unwiring case. We must first ensure that the
1862 * range to be unwired is really wired down. We know there
1865 entry = start_entry;
1866 while ((entry != &map->header) && (entry->start < end)) {
1867 if ((entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
1868 rv = KERN_INVALID_ARGUMENT;
1871 KASSERT(entry->wired_count != 0, ("wired count was 0 with USER_WIRED set! %p", entry));
1872 entry = entry->next;
1876 * Now decrement the wiring count for each region. If a region
1877 * becomes completely unwired, unwire its physical pages and
1881 * The map entries are processed in a loop, checking to
1882 * make sure the entry is wired and asserting it has a wired
1883 * count. However, another loop was inserted more-or-less in
1884 * the middle of the unwiring path. This loop picks up the
1885 * "entry" loop variable from the first loop without first
1886 * setting it to start_entry. Naturally, the secound loop
1887 * is never entered and the pages backing the entries are
1888 * never unwired. This can lead to a leak of wired pages.
1890 entry = start_entry;
1891 while ((entry != &map->header) && (entry->start < end)) {
1892 KASSERT(entry->eflags & MAP_ENTRY_USER_WIRED,
1893 ("expected USER_WIRED on entry %p", entry));
1894 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
1895 entry->wired_count--;
1896 if (entry->wired_count == 0)
1897 vm_fault_unwire(map, entry);
1898 entry = entry->next;
1902 vm_map_unclip_range(map, start_entry, start, real_end, &count,
1906 vm_map_entry_release(count);
1913 * Sets the pageability of the specified address
1914 * range in the target map. Regions specified
1915 * as not pageable require locked-down physical
1916 * memory and physical page maps.
1918 * The map must not be locked, but a reference
1919 * must remain to the map throughout the call.
1921 * This function may be called via the zalloc path and must properly
1922 * reserve map entries for kernel_map.
1925 vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t real_end, int kmflags)
1927 vm_map_entry_t entry;
1928 vm_map_entry_t start_entry;
1930 int rv = KERN_SUCCESS;
1933 if (kmflags & KM_KRESERVE)
1934 count = vm_map_entry_kreserve(MAP_RESERVE_COUNT);
1936 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1938 VM_MAP_RANGE_CHECK(map, start, real_end);
1941 start_entry = vm_map_clip_range(map, start, end, &count, MAP_CLIP_NO_HOLES);
1942 if (start_entry == NULL) {
1944 rv = KERN_INVALID_ADDRESS;
1947 if ((kmflags & KM_PAGEABLE) == 0) {
1951 * 1. Holding the write lock, we create any shadow or zero-fill
1952 * objects that need to be created. Then we clip each map
1953 * entry to the region to be wired and increment its wiring
1954 * count. We create objects before clipping the map entries
1955 * to avoid object proliferation.
1957 * 2. We downgrade to a read lock, and call vm_fault_wire to
1958 * fault in the pages for any newly wired area (wired_count is
1961 * Downgrading to a read lock for vm_fault_wire avoids a
1962 * possible deadlock with another process that may have faulted
1963 * on one of the pages to be wired (it would mark the page busy,
1964 * blocking us, then in turn block on the map lock that we
1965 * hold). Because of problems in the recursive lock package,
1966 * we cannot upgrade to a write lock in vm_map_lookup. Thus,
1967 * any actions that require the write lock must be done
1968 * beforehand. Because we keep the read lock on the map, the
1969 * copy-on-write status of the entries we modify here cannot
1973 entry = start_entry;
1974 while ((entry != &map->header) && (entry->start < end)) {
1976 * Trivial case if the entry is already wired
1978 if (entry->wired_count) {
1979 entry->wired_count++;
1980 entry = entry->next;
1985 * The entry is being newly wired, we have to setup
1986 * appropriate management structures. A shadow
1987 * object is required for a copy-on-write region,
1988 * or a normal object for a zero-fill region. We
1989 * do not have to do this for entries that point to sub
1990 * maps because we won't hold the lock on the sub map.
1992 if (entry->maptype != VM_MAPTYPE_SUBMAP) {
1993 int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY;
1995 ((entry->protection & VM_PROT_WRITE) != 0)) {
1997 vm_object_shadow(&entry->object.vm_object,
1999 atop(entry->end - entry->start));
2000 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
2001 } else if (entry->object.vm_object == NULL &&
2003 entry->object.vm_object =
2004 vm_object_allocate(OBJT_DEFAULT,
2005 atop(entry->end - entry->start));
2006 entry->offset = (vm_offset_t) 0;
2010 entry->wired_count++;
2011 entry = entry->next;
2019 * HACK HACK HACK HACK
2021 * Unlock the map to avoid deadlocks. The in-transit flag
2022 * protects us from most changes but note that
2023 * clipping may still occur. To prevent clipping from
2024 * occuring after the unlock, except for when we are
2025 * blocking in vm_fault_wire, we must run in a critical
2026 * section, otherwise our accesses to entry->start and
2027 * entry->end could be corrupted. We have to enter the
2028 * critical section prior to unlocking so start_entry does
2029 * not change out from under us at the very beginning of the
2032 * HACK HACK HACK HACK
2037 entry = start_entry;
2038 while (entry != &map->header && entry->start < end) {
2040 * If vm_fault_wire fails for any page we need to undo
2041 * what has been done. We decrement the wiring count
2042 * for those pages which have not yet been wired (now)
2043 * and unwire those that have (later).
2045 vm_offset_t save_start = entry->start;
2046 vm_offset_t save_end = entry->end;
2048 if (entry->wired_count == 1)
2049 rv = vm_fault_wire(map, entry, FALSE);
2051 CLIP_CHECK_BACK(entry, save_start);
2053 KASSERT(entry->wired_count == 1, ("wired_count changed unexpectedly"));
2054 entry->wired_count = 0;
2055 if (entry->end == save_end)
2057 entry = entry->next;
2058 KASSERT(entry != &map->header, ("bad entry clip during backout"));
2063 CLIP_CHECK_FWD(entry, save_end);
2064 entry = entry->next;
2069 * If a failure occured undo everything by falling through
2070 * to the unwiring code. 'end' has already been adjusted
2074 kmflags |= KM_PAGEABLE;
2077 * start_entry is still IN_TRANSITION but may have been
2078 * clipped since vm_fault_wire() unlocks and relocks the
2079 * map. No matter how clipped it has gotten there should
2080 * be a fragment that is on our start boundary.
2082 CLIP_CHECK_BACK(start_entry, start);
2085 if (kmflags & KM_PAGEABLE) {
2087 * This is the unwiring case. We must first ensure that the
2088 * range to be unwired is really wired down. We know there
2091 entry = start_entry;
2092 while ((entry != &map->header) && (entry->start < end)) {
2093 if (entry->wired_count == 0) {
2094 rv = KERN_INVALID_ARGUMENT;
2097 entry = entry->next;
2101 * Now decrement the wiring count for each region. If a region
2102 * becomes completely unwired, unwire its physical pages and
2105 entry = start_entry;
2106 while ((entry != &map->header) && (entry->start < end)) {
2107 entry->wired_count--;
2108 if (entry->wired_count == 0)
2109 vm_fault_unwire(map, entry);
2110 entry = entry->next;
2114 vm_map_unclip_range(map, start_entry, start, real_end, &count,
2119 if (kmflags & KM_KRESERVE)
2120 vm_map_entry_krelease(count);
2122 vm_map_entry_release(count);
2127 * vm_map_set_wired_quick()
2129 * Mark a newly allocated address range as wired but do not fault in
2130 * the pages. The caller is expected to load the pages into the object.
2132 * The map must be locked on entry and will remain locked on return.
2135 vm_map_set_wired_quick(vm_map_t map, vm_offset_t addr, vm_size_t size, int *countp)
2137 vm_map_entry_t scan;
2138 vm_map_entry_t entry;
2140 entry = vm_map_clip_range(map, addr, addr + size, countp, MAP_CLIP_NO_HOLES);
2141 for (scan = entry; scan != &map->header && scan->start < addr + size; scan = scan->next) {
2142 KKASSERT(entry->wired_count == 0);
2143 entry->wired_count = 1;
2145 vm_map_unclip_range(map, entry, addr, addr + size, countp, MAP_CLIP_NO_HOLES);
2151 * Push any dirty cached pages in the address range to their pager.
2152 * If syncio is TRUE, dirty pages are written synchronously.
2153 * If invalidate is TRUE, any cached pages are freed as well.
2155 * Returns an error if any part of the specified range is not mapped.
2158 vm_map_clean(vm_map_t map, vm_offset_t start, vm_offset_t end, boolean_t syncio,
2159 boolean_t invalidate)
2161 vm_map_entry_t current;
2162 vm_map_entry_t entry;
2165 vm_ooffset_t offset;
2167 vm_map_lock_read(map);
2168 VM_MAP_RANGE_CHECK(map, start, end);
2169 if (!vm_map_lookup_entry(map, start, &entry)) {
2170 vm_map_unlock_read(map);
2171 return (KERN_INVALID_ADDRESS);
2174 * Make a first pass to check for holes.
2176 for (current = entry; current->start < end; current = current->next) {
2177 if (current->maptype == VM_MAPTYPE_SUBMAP) {
2178 vm_map_unlock_read(map);
2179 return (KERN_INVALID_ARGUMENT);
2181 if (end > current->end &&
2182 (current->next == &map->header ||
2183 current->end != current->next->start)) {
2184 vm_map_unlock_read(map);
2185 return (KERN_INVALID_ADDRESS);
2190 pmap_remove(vm_map_pmap(map), start, end);
2192 * Make a second pass, cleaning/uncaching pages from the indicated
2195 for (current = entry; current->start < end; current = current->next) {
2196 offset = current->offset + (start - current->start);
2197 size = (end <= current->end ? end : current->end) - start;
2198 if (current->maptype == VM_MAPTYPE_SUBMAP) {
2200 vm_map_entry_t tentry;
2203 smap = current->object.sub_map;
2204 vm_map_lock_read(smap);
2205 vm_map_lookup_entry(smap, offset, &tentry);
2206 tsize = tentry->end - offset;
2209 object = tentry->object.vm_object;
2210 offset = tentry->offset + (offset - tentry->start);
2211 vm_map_unlock_read(smap);
2213 object = current->object.vm_object;
2216 * Note that there is absolutely no sense in writing out
2217 * anonymous objects, so we track down the vnode object
2219 * We invalidate (remove) all pages from the address space
2220 * anyway, for semantic correctness.
2222 * note: certain anonymous maps, such as MAP_NOSYNC maps,
2223 * may start out with a NULL object.
2225 while (object && object->backing_object) {
2226 offset += object->backing_object_offset;
2227 object = object->backing_object;
2228 if (object->size < OFF_TO_IDX( offset + size))
2229 size = IDX_TO_OFF(object->size) - offset;
2231 if (object && (object->type == OBJT_VNODE) &&
2232 (current->protection & VM_PROT_WRITE)) {
2234 * Flush pages if writing is allowed, invalidate them
2235 * if invalidation requested. Pages undergoing I/O
2236 * will be ignored by vm_object_page_remove().
2238 * We cannot lock the vnode and then wait for paging
2239 * to complete without deadlocking against vm_fault.
2240 * Instead we simply call vm_object_page_remove() and
2241 * allow it to block internally on a page-by-page
2242 * basis when it encounters pages undergoing async
2247 vm_object_reference(object);
2248 vn_lock(object->handle, LK_EXCLUSIVE | LK_RETRY);
2249 flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
2250 flags |= invalidate ? OBJPC_INVAL : 0;
2253 * When operating on a virtual page table just
2254 * flush the whole object. XXX we probably ought
2257 switch(current->maptype) {
2258 case VM_MAPTYPE_NORMAL:
2259 vm_object_page_clean(object,
2261 OFF_TO_IDX(offset + size + PAGE_MASK),
2264 case VM_MAPTYPE_VPAGETABLE:
2265 vm_object_page_clean(object, 0, 0, flags);
2268 vn_unlock(((struct vnode *)object->handle));
2269 vm_object_deallocate(object);
2271 if (object && invalidate &&
2272 ((object->type == OBJT_VNODE) ||
2273 (object->type == OBJT_DEVICE))) {
2275 (object->type == OBJT_DEVICE) ? FALSE : TRUE;
2276 vm_object_reference(object);
2277 switch(current->maptype) {
2278 case VM_MAPTYPE_NORMAL:
2279 vm_object_page_remove(object,
2281 OFF_TO_IDX(offset + size + PAGE_MASK),
2284 case VM_MAPTYPE_VPAGETABLE:
2285 vm_object_page_remove(object, 0, 0, clean_only);
2288 vm_object_deallocate(object);
2293 vm_map_unlock_read(map);
2294 return (KERN_SUCCESS);
2298 * vm_map_entry_unwire: [ internal use only ]
2300 * Make the region specified by this entry pageable.
2302 * The map in question should be locked.
2303 * [This is the reason for this routine's existence.]
2306 vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
2308 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2309 entry->wired_count = 0;
2310 vm_fault_unwire(map, entry);
2314 * vm_map_entry_delete: [ internal use only ]
2316 * Deallocate the given entry from the target map.
2319 vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry, int *countp)
2321 vm_map_entry_unlink(map, entry);
2322 map->size -= entry->end - entry->start;
2324 switch(entry->maptype) {
2325 case VM_MAPTYPE_NORMAL:
2326 case VM_MAPTYPE_VPAGETABLE:
2327 vm_object_deallocate(entry->object.vm_object);
2333 vm_map_entry_dispose(map, entry, countp);
2337 * vm_map_delete: [ internal use only ]
2339 * Deallocates the given address range from the target
2343 vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end, int *countp)
2346 vm_map_entry_t entry;
2347 vm_map_entry_t first_entry;
2351 * Find the start of the region, and clip it. Set entry to point
2352 * at the first record containing the requested address or, if no
2353 * such record exists, the next record with a greater address. The
2354 * loop will run from this point until a record beyond the termination
2355 * address is encountered.
2357 * map->hint must be adjusted to not point to anything we delete,
2358 * so set it to the entry prior to the one being deleted.
2360 * GGG see other GGG comment.
2362 if (vm_map_lookup_entry(map, start, &first_entry)) {
2363 entry = first_entry;
2364 vm_map_clip_start(map, entry, start, countp);
2365 map->hint = entry->prev; /* possible problem XXX */
2367 map->hint = first_entry; /* possible problem XXX */
2368 entry = first_entry->next;
2372 * If a hole opens up prior to the current first_free then
2373 * adjust first_free. As with map->hint, map->first_free
2374 * cannot be left set to anything we might delete.
2376 if (entry == &map->header) {
2377 map->first_free = &map->header;
2378 } else if (map->first_free->start >= start) {
2379 map->first_free = entry->prev;
2383 * Step through all entries in this region
2386 while ((entry != &map->header) && (entry->start < end)) {
2387 vm_map_entry_t next;
2389 vm_pindex_t offidxstart, offidxend, count;
2392 * If we hit an in-transition entry we have to sleep and
2393 * retry. It's easier (and not really slower) to just retry
2394 * since this case occurs so rarely and the hint is already
2395 * pointing at the right place. We have to reset the
2396 * start offset so as not to accidently delete an entry
2397 * another process just created in vacated space.
2399 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
2400 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2401 start = entry->start;
2402 ++mycpu->gd_cnt.v_intrans_coll;
2403 ++mycpu->gd_cnt.v_intrans_wait;
2404 vm_map_transition_wait(map);
2407 vm_map_clip_end(map, entry, end, countp);
2413 offidxstart = OFF_TO_IDX(entry->offset);
2414 count = OFF_TO_IDX(e - s);
2415 object = entry->object.vm_object;
2418 * Unwire before removing addresses from the pmap; otherwise,
2419 * unwiring will put the entries back in the pmap.
2421 if (entry->wired_count != 0)
2422 vm_map_entry_unwire(map, entry);
2424 offidxend = offidxstart + count;
2426 if ((object == kernel_object) || (object == kmem_object)) {
2427 vm_object_page_remove(object, offidxstart, offidxend, FALSE);
2429 pmap_remove(map->pmap, s, e);
2430 if (object != NULL &&
2431 object->ref_count != 1 &&
2432 (object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING &&
2433 (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2434 vm_object_collapse(object);
2435 vm_object_page_remove(object, offidxstart, offidxend, FALSE);
2436 if (object->type == OBJT_SWAP) {
2437 swap_pager_freespace(object, offidxstart, count);
2439 if (offidxend >= object->size &&
2440 offidxstart < object->size) {
2441 object->size = offidxstart;
2447 * Delete the entry (which may delete the object) only after
2448 * removing all pmap entries pointing to its pages.
2449 * (Otherwise, its page frames may be reallocated, and any
2450 * modify bits will be set in the wrong object!)
2452 vm_map_entry_delete(map, entry, countp);
2455 return (KERN_SUCCESS);
2461 * Remove the given address range from the target map.
2462 * This is the exported form of vm_map_delete.
2465 vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
2470 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
2472 VM_MAP_RANGE_CHECK(map, start, end);
2473 result = vm_map_delete(map, start, end, &count);
2475 vm_map_entry_release(count);
2481 * vm_map_check_protection:
2483 * Assert that the target map allows the specified
2484 * privilege on the entire address region given.
2485 * The entire region must be allocated.
2488 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
2489 vm_prot_t protection)
2491 vm_map_entry_t entry;
2492 vm_map_entry_t tmp_entry;
2494 if (!vm_map_lookup_entry(map, start, &tmp_entry)) {
2499 while (start < end) {
2500 if (entry == &map->header) {
2507 if (start < entry->start) {
2511 * Check protection associated with entry.
2514 if ((entry->protection & protection) != protection) {
2517 /* go to next entry */
2520 entry = entry->next;
2526 * Split the pages in a map entry into a new object. This affords
2527 * easier removal of unused pages, and keeps object inheritance from
2528 * being a negative impact on memory usage.
2531 vm_map_split(vm_map_entry_t entry)
2534 vm_object_t orig_object, new_object, source;
2536 vm_pindex_t offidxstart, offidxend, idx;
2538 vm_ooffset_t offset;
2540 orig_object = entry->object.vm_object;
2541 if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
2543 if (orig_object->ref_count <= 1)
2546 offset = entry->offset;
2550 offidxstart = OFF_TO_IDX(offset);
2551 offidxend = offidxstart + OFF_TO_IDX(e - s);
2552 size = offidxend - offidxstart;
2554 new_object = vm_pager_allocate(orig_object->type, NULL,
2555 IDX_TO_OFF(size), VM_PROT_ALL, 0);
2556 if (new_object == NULL)
2559 source = orig_object->backing_object;
2560 if (source != NULL) {
2561 vm_object_reference(source); /* Referenced by new_object */
2562 LIST_INSERT_HEAD(&source->shadow_head,
2563 new_object, shadow_list);
2564 vm_object_clear_flag(source, OBJ_ONEMAPPING);
2565 new_object->backing_object_offset =
2566 orig_object->backing_object_offset + IDX_TO_OFF(offidxstart);
2567 new_object->backing_object = source;
2568 source->shadow_count++;
2569 source->generation++;
2572 for (idx = 0; idx < size; idx++) {
2576 * A critical section is required to avoid a race between
2577 * the lookup and an interrupt/unbusy/free and our busy
2582 m = vm_page_lookup(orig_object, offidxstart + idx);
2589 * We must wait for pending I/O to complete before we can
2592 * We do not have to VM_PROT_NONE the page as mappings should
2593 * not be changed by this operation.
2595 if (vm_page_sleep_busy(m, TRUE, "spltwt"))
2598 vm_page_rename(m, new_object, idx);
2599 /* page automatically made dirty by rename and cache handled */
2604 if (orig_object->type == OBJT_SWAP) {
2605 vm_object_pip_add(orig_object, 1);
2607 * copy orig_object pages into new_object
2608 * and destroy unneeded pages in
2611 swap_pager_copy(orig_object, new_object, offidxstart, 0);
2612 vm_object_pip_wakeup(orig_object);
2616 * Wakeup the pages we played with. No spl protection is needed
2617 * for a simple wakeup.
2619 for (idx = 0; idx < size; idx++) {
2620 m = vm_page_lookup(new_object, idx);
2625 entry->object.vm_object = new_object;
2626 entry->offset = 0LL;
2627 vm_object_deallocate(orig_object);
2631 * vm_map_copy_entry:
2633 * Copies the contents of the source entry to the destination
2634 * entry. The entries *must* be aligned properly.
2637 vm_map_copy_entry(vm_map_t src_map, vm_map_t dst_map,
2638 vm_map_entry_t src_entry, vm_map_entry_t dst_entry)
2640 vm_object_t src_object;
2642 if (dst_entry->maptype == VM_MAPTYPE_SUBMAP)
2644 if (src_entry->maptype == VM_MAPTYPE_SUBMAP)
2647 if (src_entry->wired_count == 0) {
2649 * If the source entry is marked needs_copy, it is already
2652 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
2653 pmap_protect(src_map->pmap,
2656 src_entry->protection & ~VM_PROT_WRITE);
2660 * Make a copy of the object.
2662 if ((src_object = src_entry->object.vm_object) != NULL) {
2663 if ((src_object->handle == NULL) &&
2664 (src_object->type == OBJT_DEFAULT ||
2665 src_object->type == OBJT_SWAP)) {
2666 vm_object_collapse(src_object);
2667 if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
2668 vm_map_split(src_entry);
2669 src_object = src_entry->object.vm_object;
2673 vm_object_reference(src_object);
2674 vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
2675 dst_entry->object.vm_object = src_object;
2676 src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2677 dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2678 dst_entry->offset = src_entry->offset;
2680 dst_entry->object.vm_object = NULL;
2681 dst_entry->offset = 0;
2684 pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
2685 dst_entry->end - dst_entry->start, src_entry->start);
2688 * Of course, wired down pages can't be set copy-on-write.
2689 * Cause wired pages to be copied into the new map by
2690 * simulating faults (the new pages are pageable)
2692 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry);
2698 * Create a new process vmspace structure and vm_map
2699 * based on those of an existing process. The new map
2700 * is based on the old map, according to the inheritance
2701 * values on the regions in that map.
2703 * The source map must not be locked.
2706 vmspace_fork(struct vmspace *vm1)
2708 struct vmspace *vm2;
2709 vm_map_t old_map = &vm1->vm_map;
2711 vm_map_entry_t old_entry;
2712 vm_map_entry_t new_entry;
2716 vm_map_lock(old_map);
2717 old_map->infork = 1;
2720 * XXX Note: upcalls are not copied.
2722 vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
2723 bcopy(&vm1->vm_startcopy, &vm2->vm_startcopy,
2724 (caddr_t)&vm1->vm_endcopy - (caddr_t)&vm1->vm_startcopy);
2725 new_map = &vm2->vm_map; /* XXX */
2726 new_map->timestamp = 1;
2729 old_entry = old_map->header.next;
2730 while (old_entry != &old_map->header) {
2732 old_entry = old_entry->next;
2735 count = vm_map_entry_reserve(count + MAP_RESERVE_COUNT);
2737 old_entry = old_map->header.next;
2738 while (old_entry != &old_map->header) {
2739 if (old_entry->maptype == VM_MAPTYPE_SUBMAP)
2740 panic("vm_map_fork: encountered a submap");
2742 switch (old_entry->inheritance) {
2743 case VM_INHERIT_NONE:
2746 case VM_INHERIT_SHARE:
2748 * Clone the entry, creating the shared object if
2751 object = old_entry->object.vm_object;
2752 if (object == NULL) {
2753 object = vm_object_allocate(OBJT_DEFAULT,
2754 atop(old_entry->end - old_entry->start));
2755 old_entry->object.vm_object = object;
2756 old_entry->offset = (vm_offset_t) 0;
2760 * Add the reference before calling vm_object_shadow
2761 * to insure that a shadow object is created.
2763 vm_object_reference(object);
2764 if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
2765 vm_object_shadow(&old_entry->object.vm_object,
2767 atop(old_entry->end - old_entry->start));
2768 old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
2769 /* Transfer the second reference too. */
2770 vm_object_reference(
2771 old_entry->object.vm_object);
2772 vm_object_deallocate(object);
2773 object = old_entry->object.vm_object;
2775 vm_object_clear_flag(object, OBJ_ONEMAPPING);
2778 * Clone the entry, referencing the shared object.
2780 new_entry = vm_map_entry_create(new_map, &count);
2781 *new_entry = *old_entry;
2782 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2783 new_entry->wired_count = 0;
2786 * Insert the entry into the new map -- we know we're
2787 * inserting at the end of the new map.
2790 vm_map_entry_link(new_map, new_map->header.prev,
2794 * Update the physical map
2797 pmap_copy(new_map->pmap, old_map->pmap,
2799 (old_entry->end - old_entry->start),
2803 case VM_INHERIT_COPY:
2805 * Clone the entry and link into the map.
2807 new_entry = vm_map_entry_create(new_map, &count);
2808 *new_entry = *old_entry;
2809 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2810 new_entry->wired_count = 0;
2811 new_entry->object.vm_object = NULL;
2812 vm_map_entry_link(new_map, new_map->header.prev,
2814 vm_map_copy_entry(old_map, new_map, old_entry,
2818 old_entry = old_entry->next;
2821 new_map->size = old_map->size;
2822 old_map->infork = 0;
2823 vm_map_unlock(old_map);
2824 vm_map_entry_release(count);
2830 vm_map_stack (vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
2831 vm_prot_t prot, vm_prot_t max, int cow)
2833 vm_map_entry_t prev_entry;
2834 vm_map_entry_t new_stack_entry;
2835 vm_size_t init_ssize;
2839 if (VM_MIN_ADDRESS > 0 && addrbos < VM_MIN_ADDRESS)
2840 return (KERN_NO_SPACE);
2842 if (max_ssize < sgrowsiz)
2843 init_ssize = max_ssize;
2845 init_ssize = sgrowsiz;
2847 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
2850 /* If addr is already mapped, no go */
2851 if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
2853 vm_map_entry_release(count);
2854 return (KERN_NO_SPACE);
2857 /* If we would blow our VMEM resource limit, no go */
2858 if (map->size + init_ssize >
2859 curproc->p_rlimit[RLIMIT_VMEM].rlim_cur) {
2861 vm_map_entry_release(count);
2862 return (KERN_NO_SPACE);
2865 /* If we can't accomodate max_ssize in the current mapping,
2866 * no go. However, we need to be aware that subsequent user
2867 * mappings might map into the space we have reserved for
2868 * stack, and currently this space is not protected.
2870 * Hopefully we will at least detect this condition
2871 * when we try to grow the stack.
2873 if ((prev_entry->next != &map->header) &&
2874 (prev_entry->next->start < addrbos + max_ssize)) {
2876 vm_map_entry_release(count);
2877 return (KERN_NO_SPACE);
2880 /* We initially map a stack of only init_ssize. We will
2881 * grow as needed later. Since this is to be a grow
2882 * down stack, we map at the top of the range.
2884 * Note: we would normally expect prot and max to be
2885 * VM_PROT_ALL, and cow to be 0. Possibly we should
2886 * eliminate these as input parameters, and just
2887 * pass these values here in the insert call.
2889 rv = vm_map_insert(map, &count,
2890 NULL, 0, addrbos + max_ssize - init_ssize,
2891 addrbos + max_ssize,
2896 /* Now set the avail_ssize amount */
2897 if (rv == KERN_SUCCESS) {
2898 if (prev_entry != &map->header)
2899 vm_map_clip_end(map, prev_entry, addrbos + max_ssize - init_ssize, &count);
2900 new_stack_entry = prev_entry->next;
2901 if (new_stack_entry->end != addrbos + max_ssize ||
2902 new_stack_entry->start != addrbos + max_ssize - init_ssize)
2903 panic ("Bad entry start/end for new stack entry");
2905 new_stack_entry->avail_ssize = max_ssize - init_ssize;
2909 vm_map_entry_release(count);
2913 /* Attempts to grow a vm stack entry. Returns KERN_SUCCESS if the
2914 * desired address is already mapped, or if we successfully grow
2915 * the stack. Also returns KERN_SUCCESS if addr is outside the
2916 * stack range (this is strange, but preserves compatibility with
2917 * the grow function in vm_machdep.c).
2920 vm_map_growstack (struct proc *p, vm_offset_t addr)
2922 vm_map_entry_t prev_entry;
2923 vm_map_entry_t stack_entry;
2924 vm_map_entry_t new_stack_entry;
2925 struct vmspace *vm = p->p_vmspace;
2926 vm_map_t map = &vm->vm_map;
2929 int rv = KERN_SUCCESS;
2931 int use_read_lock = 1;
2934 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
2937 vm_map_lock_read(map);
2941 /* If addr is already in the entry range, no need to grow.*/
2942 if (vm_map_lookup_entry(map, addr, &prev_entry))
2945 if ((stack_entry = prev_entry->next) == &map->header)
2947 if (prev_entry == &map->header)
2948 end = stack_entry->start - stack_entry->avail_ssize;
2950 end = prev_entry->end;
2952 /* This next test mimics the old grow function in vm_machdep.c.
2953 * It really doesn't quite make sense, but we do it anyway
2954 * for compatibility.
2956 * If not growable stack, return success. This signals the
2957 * caller to proceed as he would normally with normal vm.
2959 if (stack_entry->avail_ssize < 1 ||
2960 addr >= stack_entry->start ||
2961 addr < stack_entry->start - stack_entry->avail_ssize) {
2965 /* Find the minimum grow amount */
2966 grow_amount = roundup (stack_entry->start - addr, PAGE_SIZE);
2967 if (grow_amount > stack_entry->avail_ssize) {
2972 /* If there is no longer enough space between the entries
2973 * nogo, and adjust the available space. Note: this
2974 * should only happen if the user has mapped into the
2975 * stack area after the stack was created, and is
2976 * probably an error.
2978 * This also effectively destroys any guard page the user
2979 * might have intended by limiting the stack size.
2981 if (grow_amount > stack_entry->start - end) {
2982 if (use_read_lock && vm_map_lock_upgrade(map)) {
2987 stack_entry->avail_ssize = stack_entry->start - end;
2992 is_procstack = addr >= (vm_offset_t)vm->vm_maxsaddr;
2994 /* If this is the main process stack, see if we're over the
2997 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount >
2998 p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
3003 /* Round up the grow amount modulo SGROWSIZ */
3004 grow_amount = roundup (grow_amount, sgrowsiz);
3005 if (grow_amount > stack_entry->avail_ssize) {
3006 grow_amount = stack_entry->avail_ssize;
3008 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount >
3009 p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
3010 grow_amount = p->p_rlimit[RLIMIT_STACK].rlim_cur -
3014 /* If we would blow our VMEM resource limit, no go */
3015 if (map->size + grow_amount > p->p_rlimit[RLIMIT_VMEM].rlim_cur) {
3020 if (use_read_lock && vm_map_lock_upgrade(map)) {
3026 /* Get the preliminary new entry start value */
3027 addr = stack_entry->start - grow_amount;
3029 /* If this puts us into the previous entry, cut back our growth
3030 * to the available space. Also, see the note above.
3033 stack_entry->avail_ssize = stack_entry->start - end;
3037 rv = vm_map_insert(map, &count,
3038 NULL, 0, addr, stack_entry->start,
3040 VM_PROT_ALL, VM_PROT_ALL,
3043 /* Adjust the available stack space by the amount we grew. */
3044 if (rv == KERN_SUCCESS) {
3045 if (prev_entry != &map->header)
3046 vm_map_clip_end(map, prev_entry, addr, &count);
3047 new_stack_entry = prev_entry->next;
3048 if (new_stack_entry->end != stack_entry->start ||
3049 new_stack_entry->start != addr)
3050 panic ("Bad stack grow start/end in new stack entry");
3052 new_stack_entry->avail_ssize = stack_entry->avail_ssize -
3053 (new_stack_entry->end -
3054 new_stack_entry->start);
3056 vm->vm_ssize += btoc(new_stack_entry->end -
3057 new_stack_entry->start);
3063 vm_map_unlock_read(map);
3066 vm_map_entry_release(count);
3071 * Unshare the specified VM space for exec. If other processes are
3072 * mapped to it, then create a new one. The new vmspace is null.
3076 vmspace_exec(struct proc *p, struct vmspace *vmcopy)
3078 struct vmspace *oldvmspace = p->p_vmspace;
3079 struct vmspace *newvmspace;
3080 vm_map_t map = &p->p_vmspace->vm_map;
3083 * If we are execing a resident vmspace we fork it, otherwise
3084 * we create a new vmspace. Note that exitingcnt and upcalls
3085 * are not copied to the new vmspace.
3088 newvmspace = vmspace_fork(vmcopy);
3090 newvmspace = vmspace_alloc(map->min_offset, map->max_offset);
3091 bcopy(&oldvmspace->vm_startcopy, &newvmspace->vm_startcopy,
3092 (caddr_t)&oldvmspace->vm_endcopy -
3093 (caddr_t)&oldvmspace->vm_startcopy);
3097 * This code is written like this for prototype purposes. The
3098 * goal is to avoid running down the vmspace here, but let the
3099 * other process's that are still using the vmspace to finally
3100 * run it down. Even though there is little or no chance of blocking
3101 * here, it is a good idea to keep this form for future mods.
3103 p->p_vmspace = newvmspace;
3104 pmap_pinit2(vmspace_pmap(newvmspace));
3107 vmspace_free(oldvmspace);
3111 * Unshare the specified VM space for forcing COW. This
3112 * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
3114 * The exitingcnt test is not strictly necessary but has been
3115 * included for code sanity (to make the code a bit more deterministic).
3119 vmspace_unshare(struct proc *p)
3121 struct vmspace *oldvmspace = p->p_vmspace;
3122 struct vmspace *newvmspace;
3124 if (oldvmspace->vm_refcnt == 1 && oldvmspace->vm_exitingcnt == 0)
3126 newvmspace = vmspace_fork(oldvmspace);
3127 p->p_vmspace = newvmspace;
3128 pmap_pinit2(vmspace_pmap(newvmspace));
3131 vmspace_free(oldvmspace);
3137 * Finds the VM object, offset, and
3138 * protection for a given virtual address in the
3139 * specified map, assuming a page fault of the
3142 * Leaves the map in question locked for read; return
3143 * values are guaranteed until a vm_map_lookup_done
3144 * call is performed. Note that the map argument
3145 * is in/out; the returned map must be used in
3146 * the call to vm_map_lookup_done.
3148 * A handle (out_entry) is returned for use in
3149 * vm_map_lookup_done, to make that fast.
3151 * If a lookup is requested with "write protection"
3152 * specified, the map may be changed to perform virtual
3153 * copying operations, although the data referenced will
3157 vm_map_lookup(vm_map_t *var_map, /* IN/OUT */
3159 vm_prot_t fault_typea,
3160 vm_map_entry_t *out_entry, /* OUT */
3161 vm_object_t *object, /* OUT */
3162 vm_pindex_t *pindex, /* OUT */
3163 vm_prot_t *out_prot, /* OUT */
3164 boolean_t *wired) /* OUT */
3166 vm_map_entry_t entry;
3167 vm_map_t map = *var_map;
3169 vm_prot_t fault_type = fault_typea;
3170 int use_read_lock = 1;
3171 int rv = KERN_SUCCESS;
3175 vm_map_lock_read(map);
3180 * If the map has an interesting hint, try it before calling full
3181 * blown lookup routine.
3186 if ((entry == &map->header) ||
3187 (vaddr < entry->start) || (vaddr >= entry->end)) {
3188 vm_map_entry_t tmp_entry;
3191 * Entry was either not a valid hint, or the vaddr was not
3192 * contained in the entry, so do a full lookup.
3194 if (!vm_map_lookup_entry(map, vaddr, &tmp_entry)) {
3195 rv = KERN_INVALID_ADDRESS;
3206 if (entry->maptype == VM_MAPTYPE_SUBMAP) {
3207 vm_map_t old_map = map;
3209 *var_map = map = entry->object.sub_map;
3211 vm_map_unlock_read(old_map);
3213 vm_map_unlock(old_map);
3219 * Check whether this task is allowed to have this page.
3220 * Note the special case for MAP_ENTRY_COW
3221 * pages with an override. This is to implement a forced
3222 * COW for debuggers.
3225 if (fault_type & VM_PROT_OVERRIDE_WRITE)
3226 prot = entry->max_protection;
3228 prot = entry->protection;
3230 fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
3231 if ((fault_type & prot) != fault_type) {
3232 rv = KERN_PROTECTION_FAILURE;
3236 if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
3237 (entry->eflags & MAP_ENTRY_COW) &&
3238 (fault_type & VM_PROT_WRITE) &&
3239 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
3240 rv = KERN_PROTECTION_FAILURE;
3245 * If this page is not pageable, we have to get it for all possible
3248 *wired = (entry->wired_count != 0);
3250 prot = fault_type = entry->protection;
3253 * Virtual page tables may need to update the accessed (A) bit
3254 * in a page table entry. Upgrade the fault to a write fault for
3255 * that case if the map will support it. If the map does not support
3256 * it the page table entry simply will not be updated.
3258 if (entry->maptype == VM_MAPTYPE_VPAGETABLE) {
3259 if (prot & VM_PROT_WRITE)
3260 fault_type |= VM_PROT_WRITE;
3264 * If the entry was copy-on-write, we either ...
3266 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3268 * If we want to write the page, we may as well handle that
3269 * now since we've got the map locked.
3271 * If we don't need to write the page, we just demote the
3272 * permissions allowed.
3275 if (fault_type & VM_PROT_WRITE) {
3277 * Make a new object, and place it in the object
3278 * chain. Note that no new references have appeared
3279 * -- one just moved from the map to the new
3283 if (use_read_lock && vm_map_lock_upgrade(map)) {
3290 &entry->object.vm_object,
3292 atop(entry->end - entry->start));
3294 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
3297 * We're attempting to read a copy-on-write page --
3298 * don't allow writes.
3301 prot &= ~VM_PROT_WRITE;
3306 * Create an object if necessary.
3308 if (entry->object.vm_object == NULL &&
3310 if (use_read_lock && vm_map_lock_upgrade(map)) {
3315 entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
3316 atop(entry->end - entry->start));
3321 * Return the object/offset from this entry. If the entry was
3322 * copy-on-write or empty, it has been fixed up.
3325 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
3326 *object = entry->object.vm_object;
3329 * Return whether this is the only map sharing this data. On
3330 * success we return with a read lock held on the map. On failure
3331 * we return with the map unlocked.
3335 if (rv == KERN_SUCCESS) {
3336 if (use_read_lock == 0)
3337 vm_map_lock_downgrade(map);
3338 } else if (use_read_lock) {
3339 vm_map_unlock_read(map);
3347 * vm_map_lookup_done:
3349 * Releases locks acquired by a vm_map_lookup
3350 * (according to the handle returned by that lookup).
3354 vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry, int count)
3357 * Unlock the main-level map
3359 vm_map_unlock_read(map);
3361 vm_map_entry_release(count);
3364 #include "opt_ddb.h"
3366 #include <sys/kernel.h>
3368 #include <ddb/ddb.h>
3371 * vm_map_print: [ debug ]
3373 DB_SHOW_COMMAND(map, vm_map_print)
3376 /* XXX convert args. */
3377 vm_map_t map = (vm_map_t)addr;
3378 boolean_t full = have_addr;
3380 vm_map_entry_t entry;
3382 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
3384 (void *)map->pmap, map->nentries, map->timestamp);
3387 if (!full && db_indent)
3391 for (entry = map->header.next; entry != &map->header;
3392 entry = entry->next) {
3393 db_iprintf("map entry %p: start=%p, end=%p\n",
3394 (void *)entry, (void *)entry->start, (void *)entry->end);
3397 static char *inheritance_name[4] =
3398 {"share", "copy", "none", "donate_copy"};
3400 db_iprintf(" prot=%x/%x/%s",
3402 entry->max_protection,
3403 inheritance_name[(int)(unsigned char)entry->inheritance]);
3404 if (entry->wired_count != 0)
3405 db_printf(", wired");
3407 if (entry->maptype == VM_MAPTYPE_SUBMAP) {
3408 /* XXX no %qd in kernel. Truncate entry->offset. */
3409 db_printf(", share=%p, offset=0x%lx\n",
3410 (void *)entry->object.sub_map,
3411 (long)entry->offset);
3413 if ((entry->prev == &map->header) ||
3414 (entry->prev->object.sub_map !=
3415 entry->object.sub_map)) {
3417 vm_map_print((db_expr_t)(intptr_t)
3418 entry->object.sub_map,
3419 full, 0, (char *)0);
3423 /* XXX no %qd in kernel. Truncate entry->offset. */
3424 db_printf(", object=%p, offset=0x%lx",
3425 (void *)entry->object.vm_object,
3426 (long)entry->offset);
3427 if (entry->eflags & MAP_ENTRY_COW)
3428 db_printf(", copy (%s)",
3429 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
3433 if ((entry->prev == &map->header) ||
3434 (entry->prev->object.vm_object !=
3435 entry->object.vm_object)) {
3437 vm_object_print((db_expr_t)(intptr_t)
3438 entry->object.vm_object,
3439 full, 0, (char *)0);
3451 DB_SHOW_COMMAND(procvm, procvm)
3456 p = (struct proc *) addr;
3461 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
3462 (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
3463 (void *)vmspace_pmap(p->p_vmspace));
3465 vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);