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.52 2006/11/07 17:51:24 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 void vm_map_entry_shadow(vm_map_entry_t entry);
148 static vm_map_entry_t vm_map_entry_create(vm_map_t map, int *);
149 static void vm_map_entry_dispose (vm_map_t map, vm_map_entry_t entry, int *);
150 static void _vm_map_clip_end (vm_map_t, vm_map_entry_t, vm_offset_t, int *);
151 static void _vm_map_clip_start (vm_map_t, vm_map_entry_t, vm_offset_t, int *);
152 static void vm_map_entry_delete (vm_map_t, vm_map_entry_t, int *);
153 static void vm_map_entry_unwire (vm_map_t, vm_map_entry_t);
154 static void vm_map_copy_entry (vm_map_t, vm_map_t, vm_map_entry_t,
156 static void vm_map_split (vm_map_entry_t);
157 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);
162 mapzone = &mapzone_store;
163 zbootinit(mapzone, "MAP", sizeof (struct vm_map),
165 mapentzone = &mapentzone_store;
166 zbootinit(mapentzone, "MAP ENTRY", sizeof (struct vm_map_entry),
167 map_entry_init, MAX_MAPENT);
171 * Red black tree functions
173 static int rb_vm_map_compare(vm_map_entry_t a, vm_map_entry_t b);
174 RB_GENERATE(vm_map_rb_tree, vm_map_entry, rb_entry, rb_vm_map_compare);
176 /* a->start is address, and the only field has to be initialized */
178 rb_vm_map_compare(vm_map_entry_t a, vm_map_entry_t b)
180 if (a->start < b->start)
182 else if (a->start > b->start)
188 * Allocate a vmspace structure, including a vm_map and pmap,
189 * and initialize those structures. The refcnt is set to 1.
190 * The remaining fields must be initialized by the caller.
193 vmspace_alloc(vm_offset_t min, vm_offset_t max)
197 vm = zalloc(vmspace_zone);
198 bzero(&vm->vm_startcopy,
199 (char *)&vm->vm_endcopy - (char *)&vm->vm_startcopy);
200 vm_map_init(&vm->vm_map, min, max);
201 pmap_pinit(vmspace_pmap(vm));
202 vm->vm_map.pmap = vmspace_pmap(vm); /* XXX */
205 vm->vm_exitingcnt = 0;
212 zinitna(mapentzone, &mapentobj, NULL, 0, 0,
213 ZONE_USE_RESERVE | ZONE_SPECIAL, 1);
214 zinitna(mapzone, &mapobj, NULL, 0, 0, 0, 1);
215 vmspace_zone = zinit("VMSPACE", sizeof (struct vmspace), 0, 0, 3);
221 vmspace_dofree(struct vmspace *vm)
226 * Make sure any SysV shm is freed, it might not have in
231 KKASSERT(vm->vm_upcalls == NULL);
234 * Lock the map, to wait out all other references to it.
235 * Delete all of the mappings and pages they hold, then call
236 * the pmap module to reclaim anything left.
238 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
239 vm_map_lock(&vm->vm_map);
240 vm_map_delete(&vm->vm_map, vm->vm_map.min_offset,
241 vm->vm_map.max_offset, &count);
242 vm_map_unlock(&vm->vm_map);
243 vm_map_entry_release(count);
245 pmap_release(vmspace_pmap(vm));
246 zfree(vmspace_zone, vm);
250 vmspace_free(struct vmspace *vm)
252 if (vm->vm_refcnt == 0)
253 panic("vmspace_free: attempt to free already freed vmspace");
255 if (--vm->vm_refcnt == 0 && vm->vm_exitingcnt == 0)
260 vmspace_exitfree(struct proc *p)
268 * cleanup by parent process wait()ing on exiting child. vm_refcnt
269 * may not be 0 (e.g. fork() and child exits without exec()ing).
270 * exitingcnt may increment above 0 and drop back down to zero
271 * several times while vm_refcnt is held non-zero. vm_refcnt
272 * may also increment above 0 and drop back down to zero several
273 * times while vm_exitingcnt is held non-zero.
275 * The last wait on the exiting child's vmspace will clean up
276 * the remainder of the vmspace.
278 if (--vm->vm_exitingcnt == 0 && vm->vm_refcnt == 0)
283 * vmspace_swap_count() - count the approximate swap useage in pages for a
286 * Swap useage is determined by taking the proportional swap used by
287 * VM objects backing the VM map. To make up for fractional losses,
288 * if the VM object has any swap use at all the associated map entries
289 * count for at least 1 swap page.
292 vmspace_swap_count(struct vmspace *vmspace)
294 vm_map_t map = &vmspace->vm_map;
300 for (cur = map->header.next; cur != &map->header; cur = cur->next) {
301 switch(cur->maptype) {
302 case VM_MAPTYPE_NORMAL:
303 case VM_MAPTYPE_VPAGETABLE:
304 if ((object = cur->object.vm_object) == NULL)
306 if (object->type != OBJT_SWAP)
308 n = (cur->end - cur->start) / PAGE_SIZE;
309 if (object->un_pager.swp.swp_bcount) {
310 count += object->un_pager.swp.swp_bcount *
311 SWAP_META_PAGES * n / object->size + 1;
325 * Creates and returns a new empty VM map with
326 * the given physical map structure, and having
327 * the given lower and upper address bounds.
330 vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max)
334 result = zalloc(mapzone);
335 vm_map_init(result, min, max);
341 * Initialize an existing vm_map structure
342 * such as that in the vmspace structure.
343 * The pmap is set elsewhere.
346 vm_map_init(struct vm_map *map, vm_offset_t min, vm_offset_t max)
348 map->header.next = map->header.prev = &map->header;
349 RB_INIT(&map->rb_root);
354 map->min_offset = min;
355 map->max_offset = max;
356 map->first_free = &map->header;
357 map->hint = &map->header;
359 lockinit(&map->lock, "thrd_sleep", 0, 0);
363 * Shadow the vm_map_entry's object. This typically needs to be done when
364 * a write fault is taken on an entry which had previously been cloned by
365 * fork(). The shared object (which might be NULL) must become private so
366 * we add a shadow layer above it.
368 * Object allocation for anonymous mappings is defered as long as possible.
369 * When creating a shadow, however, the underlying object must be instantiated
370 * so it can be shared.
372 * If the map segment is governed by a virtual page table then it is
373 * possible to address offsets beyond the mapped area. Just allocate
374 * a maximally sized object for this case.
378 vm_map_entry_shadow(vm_map_entry_t entry)
380 if (entry->maptype == VM_MAPTYPE_VPAGETABLE) {
381 vm_object_shadow(&entry->object.vm_object, &entry->offset,
382 0x7FFFFFFF); /* XXX */
384 vm_object_shadow(&entry->object.vm_object, &entry->offset,
385 atop(entry->end - entry->start));
387 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
391 * Allocate an object for a vm_map_entry.
393 * Object allocation for anonymous mappings is defered as long as possible.
394 * This function is called when we can defer no longer, generally when a map
395 * entry might be split or forked or takes a page fault.
397 * If the map segment is governed by a virtual page table then it is
398 * possible to address offsets beyond the mapped area. Just allocate
399 * a maximally sized object for this case.
402 vm_map_entry_allocate_object(vm_map_entry_t entry)
406 if (entry->maptype == VM_MAPTYPE_VPAGETABLE) {
407 obj = vm_object_allocate(OBJT_DEFAULT, 0x7FFFFFFF); /* XXX */
409 obj = vm_object_allocate(OBJT_DEFAULT,
410 atop(entry->end - entry->start));
412 entry->object.vm_object = obj;
417 * vm_map_entry_reserve_cpu_init:
419 * Set an initial negative count so the first attempt to reserve
420 * space preloads a bunch of vm_map_entry's for this cpu. Also
421 * pre-allocate 2 vm_map_entries which will be needed by zalloc() to
422 * map a new page for vm_map_entry structures. SMP systems are
423 * particularly sensitive.
425 * This routine is called in early boot so we cannot just call
426 * vm_map_entry_reserve().
428 * May be called for a gd other then mycpu, but may only be called
432 vm_map_entry_reserve_cpu_init(globaldata_t gd)
434 vm_map_entry_t entry;
437 gd->gd_vme_avail -= MAP_RESERVE_COUNT * 2;
438 entry = &cpu_map_entry_init[gd->gd_cpuid][0];
439 for (i = 0; i < VMEPERCPU; ++i, ++entry) {
440 entry->next = gd->gd_vme_base;
441 gd->gd_vme_base = entry;
446 * vm_map_entry_reserve:
448 * Reserves vm_map_entry structures so code later on can manipulate
449 * map_entry structures within a locked map without blocking trying
450 * to allocate a new vm_map_entry.
453 vm_map_entry_reserve(int count)
455 struct globaldata *gd = mycpu;
456 vm_map_entry_t entry;
461 * Make sure we have enough structures in gd_vme_base to handle
462 * the reservation request.
464 while (gd->gd_vme_avail < count) {
465 entry = zalloc(mapentzone);
466 entry->next = gd->gd_vme_base;
467 gd->gd_vme_base = entry;
470 gd->gd_vme_avail -= count;
476 * vm_map_entry_release:
478 * Releases previously reserved vm_map_entry structures that were not
479 * used. If we have too much junk in our per-cpu cache clean some of
483 vm_map_entry_release(int count)
485 struct globaldata *gd = mycpu;
486 vm_map_entry_t entry;
489 gd->gd_vme_avail += count;
490 while (gd->gd_vme_avail > MAP_RESERVE_SLOP) {
491 entry = gd->gd_vme_base;
492 KKASSERT(entry != NULL);
493 gd->gd_vme_base = entry->next;
496 zfree(mapentzone, entry);
503 * vm_map_entry_kreserve:
505 * Reserve map entry structures for use in kernel_map itself. These
506 * entries have *ALREADY* been reserved on a per-cpu basis when the map
507 * was inited. This function is used by zalloc() to avoid a recursion
508 * when zalloc() itself needs to allocate additional kernel memory.
510 * This function works like the normal reserve but does not load the
511 * vm_map_entry cache (because that would result in an infinite
512 * recursion). Note that gd_vme_avail may go negative. This is expected.
514 * Any caller of this function must be sure to renormalize after
515 * potentially eating entries to ensure that the reserve supply
519 vm_map_entry_kreserve(int count)
521 struct globaldata *gd = mycpu;
524 gd->gd_vme_avail -= count;
526 KASSERT(gd->gd_vme_base != NULL, ("no reserved entries left, gd_vme_avail = %d\n", gd->gd_vme_avail));
531 * vm_map_entry_krelease:
533 * Release previously reserved map entries for kernel_map. We do not
534 * attempt to clean up like the normal release function as this would
535 * cause an unnecessary (but probably not fatal) deep procedure call.
538 vm_map_entry_krelease(int count)
540 struct globaldata *gd = mycpu;
543 gd->gd_vme_avail += count;
548 * vm_map_entry_create: [ internal use only ]
550 * Allocates a VM map entry for insertion. No entry fields are filled
553 * This routine may be called from an interrupt thread but not a FAST
554 * interrupt. This routine may recurse the map lock.
556 static vm_map_entry_t
557 vm_map_entry_create(vm_map_t map, int *countp)
559 struct globaldata *gd = mycpu;
560 vm_map_entry_t entry;
562 KKASSERT(*countp > 0);
565 entry = gd->gd_vme_base;
566 KASSERT(entry != NULL, ("gd_vme_base NULL! count %d", *countp));
567 gd->gd_vme_base = entry->next;
573 * vm_map_entry_dispose: [ internal use only ]
575 * Dispose of a vm_map_entry that is no longer being referenced. This
576 * function may be called from an interrupt.
579 vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry, int *countp)
581 struct globaldata *gd = mycpu;
583 KKASSERT(map->hint != entry);
584 KKASSERT(map->first_free != entry);
588 entry->next = gd->gd_vme_base;
589 gd->gd_vme_base = entry;
595 * vm_map_entry_{un,}link:
597 * Insert/remove entries from maps.
600 vm_map_entry_link(vm_map_t map,
601 vm_map_entry_t after_where,
602 vm_map_entry_t entry)
605 entry->prev = after_where;
606 entry->next = after_where->next;
607 entry->next->prev = entry;
608 after_where->next = entry;
609 if (vm_map_rb_tree_RB_INSERT(&map->rb_root, entry))
610 panic("vm_map_entry_link: dup addr map %p ent %p", map, entry);
614 vm_map_entry_unlink(vm_map_t map,
615 vm_map_entry_t entry)
620 if (entry->eflags & MAP_ENTRY_IN_TRANSITION)
621 panic("vm_map_entry_unlink: attempt to mess with locked entry! %p", entry);
626 vm_map_rb_tree_RB_REMOVE(&map->rb_root, entry);
631 * vm_map_lookup_entry: [ internal use only ]
633 * Finds the map entry containing (or
634 * immediately preceding) the specified address
635 * in the given map; the entry is returned
636 * in the "entry" parameter. The boolean
637 * result indicates whether the address is
638 * actually contained in the map.
641 vm_map_lookup_entry(vm_map_t map, vm_offset_t address,
642 vm_map_entry_t *entry /* OUT */)
649 * XXX TEMPORARILY DISABLED. For some reason our attempt to revive
650 * the hint code with the red-black lookup meets with system crashes
651 * and lockups. We do not yet know why.
653 * It is possible that the problem is related to the setting
654 * of the hint during map_entry deletion, in the code specified
655 * at the GGG comment later on in this file.
658 * Quickly check the cached hint, there's a good chance of a match.
660 if (map->hint != &map->header) {
662 if (address >= tmp->start && address < tmp->end) {
670 * Locate the record from the top of the tree. 'last' tracks the
671 * closest prior record and is returned if no match is found, which
672 * in binary tree terms means tracking the most recent right-branch
673 * taken. If there is no prior record, &map->header is returned.
676 tmp = RB_ROOT(&map->rb_root);
679 if (address >= tmp->start) {
680 if (address < tmp->end) {
686 tmp = RB_RIGHT(tmp, rb_entry);
688 tmp = RB_LEFT(tmp, rb_entry);
698 * Inserts the given whole VM object into the target
699 * map at the specified address range. The object's
700 * size should match that of the address range.
702 * Requires that the map be locked, and leaves it so. Requires that
703 * sufficient vm_map_entry structures have been reserved and tracks
704 * the use via countp.
706 * If object is non-NULL, ref count must be bumped by caller
707 * prior to making call to account for the new entry.
710 vm_map_insert(vm_map_t map, int *countp,
711 vm_object_t object, vm_ooffset_t offset,
712 vm_offset_t start, vm_offset_t end,
713 vm_maptype_t maptype,
714 vm_prot_t prot, vm_prot_t max,
717 vm_map_entry_t new_entry;
718 vm_map_entry_t prev_entry;
719 vm_map_entry_t temp_entry;
720 vm_eflags_t protoeflags;
723 * Check that the start and end points are not bogus.
726 if ((start < map->min_offset) || (end > map->max_offset) ||
728 return (KERN_INVALID_ADDRESS);
731 * Find the entry prior to the proposed starting address; if it's part
732 * of an existing entry, this range is bogus.
735 if (vm_map_lookup_entry(map, start, &temp_entry))
736 return (KERN_NO_SPACE);
738 prev_entry = temp_entry;
741 * Assert that the next entry doesn't overlap the end point.
744 if ((prev_entry->next != &map->header) &&
745 (prev_entry->next->start < end))
746 return (KERN_NO_SPACE);
750 if (cow & MAP_COPY_ON_WRITE)
751 protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
753 if (cow & MAP_NOFAULT) {
754 protoeflags |= MAP_ENTRY_NOFAULT;
756 KASSERT(object == NULL,
757 ("vm_map_insert: paradoxical MAP_NOFAULT request"));
759 if (cow & MAP_DISABLE_SYNCER)
760 protoeflags |= MAP_ENTRY_NOSYNC;
761 if (cow & MAP_DISABLE_COREDUMP)
762 protoeflags |= MAP_ENTRY_NOCOREDUMP;
766 * When object is non-NULL, it could be shared with another
767 * process. We have to set or clear OBJ_ONEMAPPING
770 if ((object->ref_count > 1) || (object->shadow_count != 0)) {
771 vm_object_clear_flag(object, OBJ_ONEMAPPING);
774 else if ((prev_entry != &map->header) &&
775 (prev_entry->eflags == protoeflags) &&
776 (prev_entry->end == start) &&
777 (prev_entry->wired_count == 0) &&
778 prev_entry->maptype == maptype &&
779 ((prev_entry->object.vm_object == NULL) ||
780 vm_object_coalesce(prev_entry->object.vm_object,
781 OFF_TO_IDX(prev_entry->offset),
782 (vm_size_t)(prev_entry->end - prev_entry->start),
783 (vm_size_t)(end - prev_entry->end)))) {
785 * We were able to extend the object. Determine if we
786 * can extend the previous map entry to include the
789 if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
790 (prev_entry->protection == prot) &&
791 (prev_entry->max_protection == max)) {
792 map->size += (end - prev_entry->end);
793 prev_entry->end = end;
794 vm_map_simplify_entry(map, prev_entry, countp);
795 return (KERN_SUCCESS);
799 * If we can extend the object but cannot extend the
800 * map entry, we have to create a new map entry. We
801 * must bump the ref count on the extended object to
802 * account for it. object may be NULL.
804 object = prev_entry->object.vm_object;
805 offset = prev_entry->offset +
806 (prev_entry->end - prev_entry->start);
807 vm_object_reference(object);
811 * NOTE: if conditionals fail, object can be NULL here. This occurs
812 * in things like the buffer map where we manage kva but do not manage
820 new_entry = vm_map_entry_create(map, countp);
821 new_entry->start = start;
822 new_entry->end = end;
824 new_entry->maptype = maptype;
825 new_entry->eflags = protoeflags;
826 new_entry->object.vm_object = object;
827 new_entry->offset = offset;
828 new_entry->aux.master_pde = 0;
830 new_entry->inheritance = VM_INHERIT_DEFAULT;
831 new_entry->protection = prot;
832 new_entry->max_protection = max;
833 new_entry->wired_count = 0;
836 * Insert the new entry into the list
839 vm_map_entry_link(map, prev_entry, new_entry);
840 map->size += new_entry->end - new_entry->start;
843 * Update the free space hint
845 if ((map->first_free == prev_entry) &&
846 (prev_entry->end >= new_entry->start)) {
847 map->first_free = new_entry;
852 * Temporarily removed to avoid MAP_STACK panic, due to
853 * MAP_STACK being a huge hack. Will be added back in
854 * when MAP_STACK (and the user stack mapping) is fixed.
857 * It may be possible to simplify the entry
859 vm_map_simplify_entry(map, new_entry, countp);
863 * Try to pre-populate the page table. Mappings governed by virtual
864 * page tables cannot be prepopulated without a lot of work, so
867 if ((cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) &&
868 maptype != VM_MAPTYPE_VPAGETABLE) {
869 pmap_object_init_pt(map->pmap, start, prot,
870 object, OFF_TO_IDX(offset), end - start,
871 cow & MAP_PREFAULT_PARTIAL);
874 return (KERN_SUCCESS);
878 * Find sufficient space for `length' bytes in the given map, starting at
879 * `start'. The map must be locked. Returns 0 on success, 1 on no space.
881 * This function will returned an arbitrarily aligned pointer. If no
882 * particular alignment is required you should pass align as 1. Note that
883 * the map may return PAGE_SIZE aligned pointers if all the lengths used in
884 * the map are a multiple of PAGE_SIZE, even if you pass a smaller align
887 * 'align' should be a power of 2 but is not required to be.
897 vm_map_entry_t entry, next;
899 vm_offset_t align_mask;
901 if (start < map->min_offset)
902 start = map->min_offset;
903 if (start > map->max_offset)
907 * If the alignment is not a power of 2 we will have to use
908 * a mod/division, set align_mask to a special value.
910 if ((align | (align - 1)) + 1 != (align << 1))
911 align_mask = (vm_offset_t)-1;
913 align_mask = align - 1;
917 * Look for the first possible address; if there's already something
918 * at this address, we have to start after it.
920 if (start == map->min_offset) {
921 if ((entry = map->first_free) != &map->header)
926 if (vm_map_lookup_entry(map, start, &tmp))
932 * Look through the rest of the map, trying to fit a new region in the
933 * gap between existing regions, or after the very last region.
935 for (;; start = (entry = next)->end) {
937 * Adjust the proposed start by the requested alignment,
938 * be sure that we didn't wrap the address.
940 if (align_mask == (vm_offset_t)-1)
941 end = ((start + align - 1) / align) * align;
943 end = (start + align_mask) & ~align_mask;
948 * Find the end of the proposed new region. Be sure we didn't
949 * go beyond the end of the map, or wrap around the address.
950 * Then check to see if this is the last entry or if the
951 * proposed end fits in the gap between this and the next
954 end = start + length;
955 if (end > map->max_offset || end < start)
958 if (next == &map->header || next->start >= end)
962 if (map == kernel_map) {
964 if ((ksize = round_page(start + length)) > kernel_vm_end) {
965 pmap_growkernel(ksize);
974 * vm_map_find finds an unallocated region in the target address
975 * map with the given length. The search is defined to be
976 * first-fit from the specified address; the region found is
977 * returned in the same parameter.
979 * If object is non-NULL, ref count must be bumped by caller
980 * prior to making call to account for the new entry.
983 vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
984 vm_offset_t *addr, vm_size_t length,
985 boolean_t find_space,
986 vm_maptype_t maptype,
987 vm_prot_t prot, vm_prot_t max,
996 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
999 if (vm_map_findspace(map, start, length, 1, addr)) {
1001 vm_map_entry_release(count);
1002 return (KERN_NO_SPACE);
1006 result = vm_map_insert(map, &count, object, offset,
1007 start, start + length,
1012 vm_map_entry_release(count);
1018 * vm_map_simplify_entry:
1020 * Simplify the given map entry by merging with either neighbor. This
1021 * routine also has the ability to merge with both neighbors.
1023 * The map must be locked.
1025 * This routine guarentees that the passed entry remains valid (though
1026 * possibly extended). When merging, this routine may delete one or
1027 * both neighbors. No action is taken on entries which have their
1028 * in-transition flag set.
1031 vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry, int *countp)
1033 vm_map_entry_t next, prev;
1034 vm_size_t prevsize, esize;
1036 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1037 ++mycpu->gd_cnt.v_intrans_coll;
1041 if (entry->maptype == VM_MAPTYPE_SUBMAP)
1045 if (prev != &map->header) {
1046 prevsize = prev->end - prev->start;
1047 if ( (prev->end == entry->start) &&
1048 (prev->maptype == entry->maptype) &&
1049 (prev->object.vm_object == entry->object.vm_object) &&
1050 (!prev->object.vm_object ||
1051 (prev->offset + prevsize == entry->offset)) &&
1052 (prev->eflags == entry->eflags) &&
1053 (prev->protection == entry->protection) &&
1054 (prev->max_protection == entry->max_protection) &&
1055 (prev->inheritance == entry->inheritance) &&
1056 (prev->wired_count == entry->wired_count)) {
1057 if (map->first_free == prev)
1058 map->first_free = entry;
1059 if (map->hint == prev)
1061 vm_map_entry_unlink(map, prev);
1062 entry->start = prev->start;
1063 entry->offset = prev->offset;
1064 if (prev->object.vm_object)
1065 vm_object_deallocate(prev->object.vm_object);
1066 vm_map_entry_dispose(map, prev, countp);
1071 if (next != &map->header) {
1072 esize = entry->end - entry->start;
1073 if ((entry->end == next->start) &&
1074 (next->maptype == entry->maptype) &&
1075 (next->object.vm_object == entry->object.vm_object) &&
1076 (!entry->object.vm_object ||
1077 (entry->offset + esize == next->offset)) &&
1078 (next->eflags == entry->eflags) &&
1079 (next->protection == entry->protection) &&
1080 (next->max_protection == entry->max_protection) &&
1081 (next->inheritance == entry->inheritance) &&
1082 (next->wired_count == entry->wired_count)) {
1083 if (map->first_free == next)
1084 map->first_free = entry;
1085 if (map->hint == next)
1087 vm_map_entry_unlink(map, next);
1088 entry->end = next->end;
1089 if (next->object.vm_object)
1090 vm_object_deallocate(next->object.vm_object);
1091 vm_map_entry_dispose(map, next, countp);
1096 * vm_map_clip_start: [ internal use only ]
1098 * Asserts that the given entry begins at or after
1099 * the specified address; if necessary,
1100 * it splits the entry into two.
1102 #define vm_map_clip_start(map, entry, startaddr, countp) \
1104 if (startaddr > entry->start) \
1105 _vm_map_clip_start(map, entry, startaddr, countp); \
1109 * This routine is called only when it is known that
1110 * the entry must be split.
1113 _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start, int *countp)
1115 vm_map_entry_t new_entry;
1118 * Split off the front portion -- note that we must insert the new
1119 * entry BEFORE this one, so that this entry has the specified
1123 vm_map_simplify_entry(map, entry, countp);
1126 * If there is no object backing this entry, we might as well create
1127 * one now. If we defer it, an object can get created after the map
1128 * is clipped, and individual objects will be created for the split-up
1129 * map. This is a bit of a hack, but is also about the best place to
1130 * put this improvement.
1132 if (entry->object.vm_object == NULL && !map->system_map) {
1133 vm_map_entry_allocate_object(entry);
1136 new_entry = vm_map_entry_create(map, countp);
1137 *new_entry = *entry;
1139 new_entry->end = start;
1140 entry->offset += (start - entry->start);
1141 entry->start = start;
1143 vm_map_entry_link(map, entry->prev, new_entry);
1145 switch(entry->maptype) {
1146 case VM_MAPTYPE_NORMAL:
1147 case VM_MAPTYPE_VPAGETABLE:
1148 vm_object_reference(new_entry->object.vm_object);
1156 * vm_map_clip_end: [ internal use only ]
1158 * Asserts that the given entry ends at or before
1159 * the specified address; if necessary,
1160 * it splits the entry into two.
1163 #define vm_map_clip_end(map, entry, endaddr, countp) \
1165 if (endaddr < entry->end) \
1166 _vm_map_clip_end(map, entry, endaddr, countp); \
1170 * This routine is called only when it is known that
1171 * the entry must be split.
1174 _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end, int *countp)
1176 vm_map_entry_t new_entry;
1179 * If there is no object backing this entry, we might as well create
1180 * one now. If we defer it, an object can get created after the map
1181 * is clipped, and individual objects will be created for the split-up
1182 * map. This is a bit of a hack, but is also about the best place to
1183 * put this improvement.
1186 if (entry->object.vm_object == NULL && !map->system_map) {
1187 vm_map_entry_allocate_object(entry);
1191 * Create a new entry and insert it AFTER the specified entry
1194 new_entry = vm_map_entry_create(map, countp);
1195 *new_entry = *entry;
1197 new_entry->start = entry->end = end;
1198 new_entry->offset += (end - entry->start);
1200 vm_map_entry_link(map, entry, new_entry);
1202 switch(entry->maptype) {
1203 case VM_MAPTYPE_NORMAL:
1204 case VM_MAPTYPE_VPAGETABLE:
1205 vm_object_reference(new_entry->object.vm_object);
1213 * VM_MAP_RANGE_CHECK: [ internal use only ]
1215 * Asserts that the starting and ending region
1216 * addresses fall within the valid range of the map.
1218 #define VM_MAP_RANGE_CHECK(map, start, end) \
1220 if (start < vm_map_min(map)) \
1221 start = vm_map_min(map); \
1222 if (end > vm_map_max(map)) \
1223 end = vm_map_max(map); \
1229 * vm_map_transition_wait: [ kernel use only ]
1231 * Used to block when an in-transition collison occurs. The map
1232 * is unlocked for the sleep and relocked before the return.
1236 vm_map_transition_wait(vm_map_t map)
1239 tsleep(map, 0, "vment", 0);
1247 * When we do blocking operations with the map lock held it is
1248 * possible that a clip might have occured on our in-transit entry,
1249 * requiring an adjustment to the entry in our loop. These macros
1250 * help the pageable and clip_range code deal with the case. The
1251 * conditional costs virtually nothing if no clipping has occured.
1254 #define CLIP_CHECK_BACK(entry, save_start) \
1256 while (entry->start != save_start) { \
1257 entry = entry->prev; \
1258 KASSERT(entry != &map->header, ("bad entry clip")); \
1262 #define CLIP_CHECK_FWD(entry, save_end) \
1264 while (entry->end != save_end) { \
1265 entry = entry->next; \
1266 KASSERT(entry != &map->header, ("bad entry clip")); \
1272 * vm_map_clip_range: [ kernel use only ]
1274 * Clip the specified range and return the base entry. The
1275 * range may cover several entries starting at the returned base
1276 * and the first and last entry in the covering sequence will be
1277 * properly clipped to the requested start and end address.
1279 * If no holes are allowed you should pass the MAP_CLIP_NO_HOLES
1282 * The MAP_ENTRY_IN_TRANSITION flag will be set for the entries
1283 * covered by the requested range.
1285 * The map must be exclusively locked on entry and will remain locked
1286 * on return. If no range exists or the range contains holes and you
1287 * specified that no holes were allowed, NULL will be returned. This
1288 * routine may temporarily unlock the map in order avoid a deadlock when
1293 vm_map_clip_range(vm_map_t map, vm_offset_t start, vm_offset_t end,
1294 int *countp, int flags)
1296 vm_map_entry_t start_entry;
1297 vm_map_entry_t entry;
1300 * Locate the entry and effect initial clipping. The in-transition
1301 * case does not occur very often so do not try to optimize it.
1304 if (vm_map_lookup_entry(map, start, &start_entry) == FALSE)
1306 entry = start_entry;
1307 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1308 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1309 ++mycpu->gd_cnt.v_intrans_coll;
1310 ++mycpu->gd_cnt.v_intrans_wait;
1311 vm_map_transition_wait(map);
1313 * entry and/or start_entry may have been clipped while
1314 * we slept, or may have gone away entirely. We have
1315 * to restart from the lookup.
1320 * Since we hold an exclusive map lock we do not have to restart
1321 * after clipping, even though clipping may block in zalloc.
1323 vm_map_clip_start(map, entry, start, countp);
1324 vm_map_clip_end(map, entry, end, countp);
1325 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
1328 * Scan entries covered by the range. When working on the next
1329 * entry a restart need only re-loop on the current entry which
1330 * we have already locked, since 'next' may have changed. Also,
1331 * even though entry is safe, it may have been clipped so we
1332 * have to iterate forwards through the clip after sleeping.
1334 while (entry->next != &map->header && entry->next->start < end) {
1335 vm_map_entry_t next = entry->next;
1337 if (flags & MAP_CLIP_NO_HOLES) {
1338 if (next->start > entry->end) {
1339 vm_map_unclip_range(map, start_entry,
1340 start, entry->end, countp, flags);
1345 if (next->eflags & MAP_ENTRY_IN_TRANSITION) {
1346 vm_offset_t save_end = entry->end;
1347 next->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1348 ++mycpu->gd_cnt.v_intrans_coll;
1349 ++mycpu->gd_cnt.v_intrans_wait;
1350 vm_map_transition_wait(map);
1353 * clips might have occured while we blocked.
1355 CLIP_CHECK_FWD(entry, save_end);
1356 CLIP_CHECK_BACK(start_entry, start);
1360 * No restart necessary even though clip_end may block, we
1361 * are holding the map lock.
1363 vm_map_clip_end(map, next, end, countp);
1364 next->eflags |= MAP_ENTRY_IN_TRANSITION;
1367 if (flags & MAP_CLIP_NO_HOLES) {
1368 if (entry->end != end) {
1369 vm_map_unclip_range(map, start_entry,
1370 start, entry->end, countp, flags);
1374 return(start_entry);
1378 * vm_map_unclip_range: [ kernel use only ]
1380 * Undo the effect of vm_map_clip_range(). You should pass the same
1381 * flags and the same range that you passed to vm_map_clip_range().
1382 * This code will clear the in-transition flag on the entries and
1383 * wake up anyone waiting. This code will also simplify the sequence
1384 * and attempt to merge it with entries before and after the sequence.
1386 * The map must be locked on entry and will remain locked on return.
1388 * Note that you should also pass the start_entry returned by
1389 * vm_map_clip_range(). However, if you block between the two calls
1390 * with the map unlocked please be aware that the start_entry may
1391 * have been clipped and you may need to scan it backwards to find
1392 * the entry corresponding with the original start address. You are
1393 * responsible for this, vm_map_unclip_range() expects the correct
1394 * start_entry to be passed to it and will KASSERT otherwise.
1398 vm_map_unclip_range(
1400 vm_map_entry_t start_entry,
1406 vm_map_entry_t entry;
1408 entry = start_entry;
1410 KASSERT(entry->start == start, ("unclip_range: illegal base entry"));
1411 while (entry != &map->header && entry->start < end) {
1412 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION, ("in-transition flag not set during unclip on: %p", entry));
1413 KASSERT(entry->end <= end, ("unclip_range: tail wasn't clipped"));
1414 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
1415 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
1416 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
1419 entry = entry->next;
1423 * Simplification does not block so there is no restart case.
1425 entry = start_entry;
1426 while (entry != &map->header && entry->start < end) {
1427 vm_map_simplify_entry(map, entry, countp);
1428 entry = entry->next;
1433 * vm_map_submap: [ kernel use only ]
1435 * Mark the given range as handled by a subordinate map.
1437 * This range must have been created with vm_map_find,
1438 * and no other operations may have been performed on this
1439 * range prior to calling vm_map_submap.
1441 * Only a limited number of operations can be performed
1442 * within this rage after calling vm_map_submap:
1444 * [Don't try vm_map_copy!]
1446 * To remove a submapping, one must first remove the
1447 * range from the superior map, and then destroy the
1448 * submap (if desired). [Better yet, don't try it.]
1451 vm_map_submap(vm_map_t map, vm_offset_t start, vm_offset_t end, vm_map_t submap)
1453 vm_map_entry_t entry;
1454 int result = KERN_INVALID_ARGUMENT;
1457 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1460 VM_MAP_RANGE_CHECK(map, start, end);
1462 if (vm_map_lookup_entry(map, start, &entry)) {
1463 vm_map_clip_start(map, entry, start, &count);
1465 entry = entry->next;
1468 vm_map_clip_end(map, entry, end, &count);
1470 if ((entry->start == start) && (entry->end == end) &&
1471 ((entry->eflags & MAP_ENTRY_COW) == 0) &&
1472 (entry->object.vm_object == NULL)) {
1473 entry->object.sub_map = submap;
1474 entry->maptype = VM_MAPTYPE_SUBMAP;
1475 result = KERN_SUCCESS;
1478 vm_map_entry_release(count);
1486 * Sets the protection of the specified address region in the target map.
1487 * If "set_max" is specified, the maximum protection is to be set;
1488 * otherwise, only the current protection is affected.
1490 * The protection is not applicable to submaps, but is applicable to normal
1491 * maps and maps governed by virtual page tables. For example, when operating
1492 * on a virtual page table our protection basically controls how COW occurs
1493 * on the backing object, whereas the virtual page table abstraction itself
1494 * is an abstraction for userland.
1497 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
1498 vm_prot_t new_prot, boolean_t set_max)
1500 vm_map_entry_t current;
1501 vm_map_entry_t entry;
1504 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1507 VM_MAP_RANGE_CHECK(map, start, end);
1509 if (vm_map_lookup_entry(map, start, &entry)) {
1510 vm_map_clip_start(map, entry, start, &count);
1512 entry = entry->next;
1516 * Make a first pass to check for protection violations.
1519 while ((current != &map->header) && (current->start < end)) {
1520 if (current->maptype == VM_MAPTYPE_SUBMAP) {
1522 vm_map_entry_release(count);
1523 return (KERN_INVALID_ARGUMENT);
1525 if ((new_prot & current->max_protection) != new_prot) {
1527 vm_map_entry_release(count);
1528 return (KERN_PROTECTION_FAILURE);
1530 current = current->next;
1534 * Go back and fix up protections. [Note that clipping is not
1535 * necessary the second time.]
1539 while ((current != &map->header) && (current->start < end)) {
1542 vm_map_clip_end(map, current, end, &count);
1544 old_prot = current->protection;
1546 current->protection =
1547 (current->max_protection = new_prot) &
1550 current->protection = new_prot;
1554 * Update physical map if necessary. Worry about copy-on-write
1555 * here -- CHECK THIS XXX
1558 if (current->protection != old_prot) {
1559 #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1562 pmap_protect(map->pmap, current->start,
1564 current->protection & MASK(current));
1568 vm_map_simplify_entry(map, current, &count);
1570 current = current->next;
1574 vm_map_entry_release(count);
1575 return (KERN_SUCCESS);
1581 * This routine traverses a processes map handling the madvise
1582 * system call. Advisories are classified as either those effecting
1583 * the vm_map_entry structure, or those effecting the underlying
1586 * The <value> argument is used for extended madvise calls.
1589 vm_map_madvise(vm_map_t map, vm_offset_t start, vm_offset_t end,
1590 int behav, off_t value)
1592 vm_map_entry_t current, entry;
1598 * Some madvise calls directly modify the vm_map_entry, in which case
1599 * we need to use an exclusive lock on the map and we need to perform
1600 * various clipping operations. Otherwise we only need a read-lock
1604 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1608 case MADV_SEQUENTIAL:
1622 vm_map_lock_read(map);
1625 vm_map_entry_release(count);
1630 * Locate starting entry and clip if necessary.
1633 VM_MAP_RANGE_CHECK(map, start, end);
1635 if (vm_map_lookup_entry(map, start, &entry)) {
1637 vm_map_clip_start(map, entry, start, &count);
1639 entry = entry->next;
1644 * madvise behaviors that are implemented in the vm_map_entry.
1646 * We clip the vm_map_entry so that behavioral changes are
1647 * limited to the specified address range.
1649 for (current = entry;
1650 (current != &map->header) && (current->start < end);
1651 current = current->next
1653 if (current->maptype == VM_MAPTYPE_SUBMAP)
1656 vm_map_clip_end(map, current, end, &count);
1660 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
1662 case MADV_SEQUENTIAL:
1663 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
1666 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
1669 current->eflags |= MAP_ENTRY_NOSYNC;
1672 current->eflags &= ~MAP_ENTRY_NOSYNC;
1675 current->eflags |= MAP_ENTRY_NOCOREDUMP;
1678 current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
1682 * Invalidate the related pmap entries, used
1683 * to flush portions of the real kernel's
1684 * pmap when the caller has removed or
1685 * modified existing mappings in a virtual
1688 pmap_remove(map->pmap,
1689 current->start, current->end);
1693 * Set the page directory page for a map
1694 * governed by a virtual page table. Mark
1695 * the entry as being governed by a virtual
1696 * page table if it is not.
1698 * XXX the page directory page is stored
1699 * in the avail_ssize field if the map_entry.
1701 * XXX the map simplification code does not
1702 * compare this field so weird things may
1703 * happen if you do not apply this function
1704 * to the entire mapping governed by the
1705 * virtual page table.
1707 if (current->maptype != VM_MAPTYPE_VPAGETABLE) {
1711 current->aux.master_pde = value;
1712 pmap_remove(map->pmap,
1713 current->start, current->end);
1719 vm_map_simplify_entry(map, current, &count);
1727 * madvise behaviors that are implemented in the underlying
1730 * Since we don't clip the vm_map_entry, we have to clip
1731 * the vm_object pindex and count.
1733 * NOTE! We currently do not support these functions on
1734 * virtual page tables.
1736 for (current = entry;
1737 (current != &map->header) && (current->start < end);
1738 current = current->next
1740 vm_offset_t useStart;
1742 if (current->maptype != VM_MAPTYPE_NORMAL)
1745 pindex = OFF_TO_IDX(current->offset);
1746 count = atop(current->end - current->start);
1747 useStart = current->start;
1749 if (current->start < start) {
1750 pindex += atop(start - current->start);
1751 count -= atop(start - current->start);
1754 if (current->end > end)
1755 count -= atop(current->end - end);
1760 vm_object_madvise(current->object.vm_object,
1761 pindex, count, behav);
1764 * Try to populate the page table. Mappings governed
1765 * by virtual page tables cannot be pre-populated
1766 * without a lot of work so don't try.
1768 if (behav == MADV_WILLNEED &&
1769 current->maptype != VM_MAPTYPE_VPAGETABLE) {
1770 pmap_object_init_pt(
1773 current->protection,
1774 current->object.vm_object,
1776 (count << PAGE_SHIFT),
1777 MAP_PREFAULT_MADVISE
1781 vm_map_unlock_read(map);
1783 vm_map_entry_release(count);
1791 * Sets the inheritance of the specified address
1792 * range in the target map. Inheritance
1793 * affects how the map will be shared with
1794 * child maps at the time of vm_map_fork.
1797 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
1798 vm_inherit_t new_inheritance)
1800 vm_map_entry_t entry;
1801 vm_map_entry_t temp_entry;
1804 switch (new_inheritance) {
1805 case VM_INHERIT_NONE:
1806 case VM_INHERIT_COPY:
1807 case VM_INHERIT_SHARE:
1810 return (KERN_INVALID_ARGUMENT);
1813 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1816 VM_MAP_RANGE_CHECK(map, start, end);
1818 if (vm_map_lookup_entry(map, start, &temp_entry)) {
1820 vm_map_clip_start(map, entry, start, &count);
1822 entry = temp_entry->next;
1824 while ((entry != &map->header) && (entry->start < end)) {
1825 vm_map_clip_end(map, entry, end, &count);
1827 entry->inheritance = new_inheritance;
1829 vm_map_simplify_entry(map, entry, &count);
1831 entry = entry->next;
1834 vm_map_entry_release(count);
1835 return (KERN_SUCCESS);
1839 * Implement the semantics of mlock
1842 vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t real_end,
1843 boolean_t new_pageable)
1845 vm_map_entry_t entry;
1846 vm_map_entry_t start_entry;
1848 int rv = KERN_SUCCESS;
1851 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1853 VM_MAP_RANGE_CHECK(map, start, real_end);
1856 start_entry = vm_map_clip_range(map, start, end, &count, MAP_CLIP_NO_HOLES);
1857 if (start_entry == NULL) {
1859 vm_map_entry_release(count);
1860 return (KERN_INVALID_ADDRESS);
1863 if (new_pageable == 0) {
1864 entry = start_entry;
1865 while ((entry != &map->header) && (entry->start < end)) {
1866 vm_offset_t save_start;
1867 vm_offset_t save_end;
1870 * Already user wired or hard wired (trivial cases)
1872 if (entry->eflags & MAP_ENTRY_USER_WIRED) {
1873 entry = entry->next;
1876 if (entry->wired_count != 0) {
1877 entry->wired_count++;
1878 entry->eflags |= MAP_ENTRY_USER_WIRED;
1879 entry = entry->next;
1884 * A new wiring requires instantiation of appropriate
1885 * management structures and the faulting in of the
1888 if (entry->maptype != VM_MAPTYPE_SUBMAP) {
1889 int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY;
1890 if (copyflag && ((entry->protection & VM_PROT_WRITE) != 0)) {
1891 vm_map_entry_shadow(entry);
1892 } else if (entry->object.vm_object == NULL &&
1894 vm_map_entry_allocate_object(entry);
1897 entry->wired_count++;
1898 entry->eflags |= MAP_ENTRY_USER_WIRED;
1901 * Now fault in the area. Note that vm_fault_wire()
1902 * may release the map lock temporarily, it will be
1903 * relocked on return. The in-transition
1904 * flag protects the entries.
1906 save_start = entry->start;
1907 save_end = entry->end;
1908 rv = vm_fault_wire(map, entry, TRUE);
1910 CLIP_CHECK_BACK(entry, save_start);
1912 KASSERT(entry->wired_count == 1, ("bad wired_count on entry"));
1913 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
1914 entry->wired_count = 0;
1915 if (entry->end == save_end)
1917 entry = entry->next;
1918 KASSERT(entry != &map->header, ("bad entry clip during backout"));
1920 end = save_start; /* unwire the rest */
1924 * note that even though the entry might have been
1925 * clipped, the USER_WIRED flag we set prevents
1926 * duplication so we do not have to do a
1929 entry = entry->next;
1933 * If we failed fall through to the unwiring section to
1934 * unwire what we had wired so far. 'end' has already
1941 * start_entry might have been clipped if we unlocked the
1942 * map and blocked. No matter how clipped it has gotten
1943 * there should be a fragment that is on our start boundary.
1945 CLIP_CHECK_BACK(start_entry, start);
1949 * Deal with the unwiring case.
1953 * This is the unwiring case. We must first ensure that the
1954 * range to be unwired is really wired down. We know there
1957 entry = start_entry;
1958 while ((entry != &map->header) && (entry->start < end)) {
1959 if ((entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
1960 rv = KERN_INVALID_ARGUMENT;
1963 KASSERT(entry->wired_count != 0, ("wired count was 0 with USER_WIRED set! %p", entry));
1964 entry = entry->next;
1968 * Now decrement the wiring count for each region. If a region
1969 * becomes completely unwired, unwire its physical pages and
1973 * The map entries are processed in a loop, checking to
1974 * make sure the entry is wired and asserting it has a wired
1975 * count. However, another loop was inserted more-or-less in
1976 * the middle of the unwiring path. This loop picks up the
1977 * "entry" loop variable from the first loop without first
1978 * setting it to start_entry. Naturally, the secound loop
1979 * is never entered and the pages backing the entries are
1980 * never unwired. This can lead to a leak of wired pages.
1982 entry = start_entry;
1983 while ((entry != &map->header) && (entry->start < end)) {
1984 KASSERT(entry->eflags & MAP_ENTRY_USER_WIRED,
1985 ("expected USER_WIRED on entry %p", entry));
1986 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
1987 entry->wired_count--;
1988 if (entry->wired_count == 0)
1989 vm_fault_unwire(map, entry);
1990 entry = entry->next;
1994 vm_map_unclip_range(map, start_entry, start, real_end, &count,
1998 vm_map_entry_release(count);
2005 * Sets the pageability of the specified address
2006 * range in the target map. Regions specified
2007 * as not pageable require locked-down physical
2008 * memory and physical page maps.
2010 * The map must not be locked, but a reference
2011 * must remain to the map throughout the call.
2013 * This function may be called via the zalloc path and must properly
2014 * reserve map entries for kernel_map.
2017 vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t real_end, int kmflags)
2019 vm_map_entry_t entry;
2020 vm_map_entry_t start_entry;
2022 int rv = KERN_SUCCESS;
2025 if (kmflags & KM_KRESERVE)
2026 count = vm_map_entry_kreserve(MAP_RESERVE_COUNT);
2028 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
2030 VM_MAP_RANGE_CHECK(map, start, real_end);
2033 start_entry = vm_map_clip_range(map, start, end, &count, MAP_CLIP_NO_HOLES);
2034 if (start_entry == NULL) {
2036 rv = KERN_INVALID_ADDRESS;
2039 if ((kmflags & KM_PAGEABLE) == 0) {
2043 * 1. Holding the write lock, we create any shadow or zero-fill
2044 * objects that need to be created. Then we clip each map
2045 * entry to the region to be wired and increment its wiring
2046 * count. We create objects before clipping the map entries
2047 * to avoid object proliferation.
2049 * 2. We downgrade to a read lock, and call vm_fault_wire to
2050 * fault in the pages for any newly wired area (wired_count is
2053 * Downgrading to a read lock for vm_fault_wire avoids a
2054 * possible deadlock with another process that may have faulted
2055 * on one of the pages to be wired (it would mark the page busy,
2056 * blocking us, then in turn block on the map lock that we
2057 * hold). Because of problems in the recursive lock package,
2058 * we cannot upgrade to a write lock in vm_map_lookup. Thus,
2059 * any actions that require the write lock must be done
2060 * beforehand. Because we keep the read lock on the map, the
2061 * copy-on-write status of the entries we modify here cannot
2065 entry = start_entry;
2066 while ((entry != &map->header) && (entry->start < end)) {
2068 * Trivial case if the entry is already wired
2070 if (entry->wired_count) {
2071 entry->wired_count++;
2072 entry = entry->next;
2077 * The entry is being newly wired, we have to setup
2078 * appropriate management structures. A shadow
2079 * object is required for a copy-on-write region,
2080 * or a normal object for a zero-fill region. We
2081 * do not have to do this for entries that point to sub
2082 * maps because we won't hold the lock on the sub map.
2084 if (entry->maptype != VM_MAPTYPE_SUBMAP) {
2085 int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY;
2087 ((entry->protection & VM_PROT_WRITE) != 0)) {
2088 vm_map_entry_shadow(entry);
2089 } else if (entry->object.vm_object == NULL &&
2091 vm_map_entry_allocate_object(entry);
2095 entry->wired_count++;
2096 entry = entry->next;
2104 * HACK HACK HACK HACK
2106 * Unlock the map to avoid deadlocks. The in-transit flag
2107 * protects us from most changes but note that
2108 * clipping may still occur. To prevent clipping from
2109 * occuring after the unlock, except for when we are
2110 * blocking in vm_fault_wire, we must run in a critical
2111 * section, otherwise our accesses to entry->start and
2112 * entry->end could be corrupted. We have to enter the
2113 * critical section prior to unlocking so start_entry does
2114 * not change out from under us at the very beginning of the
2117 * HACK HACK HACK HACK
2122 entry = start_entry;
2123 while (entry != &map->header && entry->start < end) {
2125 * If vm_fault_wire fails for any page we need to undo
2126 * what has been done. We decrement the wiring count
2127 * for those pages which have not yet been wired (now)
2128 * and unwire those that have (later).
2130 vm_offset_t save_start = entry->start;
2131 vm_offset_t save_end = entry->end;
2133 if (entry->wired_count == 1)
2134 rv = vm_fault_wire(map, entry, FALSE);
2136 CLIP_CHECK_BACK(entry, save_start);
2138 KASSERT(entry->wired_count == 1, ("wired_count changed unexpectedly"));
2139 entry->wired_count = 0;
2140 if (entry->end == save_end)
2142 entry = entry->next;
2143 KASSERT(entry != &map->header, ("bad entry clip during backout"));
2148 CLIP_CHECK_FWD(entry, save_end);
2149 entry = entry->next;
2154 * If a failure occured undo everything by falling through
2155 * to the unwiring code. 'end' has already been adjusted
2159 kmflags |= KM_PAGEABLE;
2162 * start_entry is still IN_TRANSITION but may have been
2163 * clipped since vm_fault_wire() unlocks and relocks the
2164 * map. No matter how clipped it has gotten there should
2165 * be a fragment that is on our start boundary.
2167 CLIP_CHECK_BACK(start_entry, start);
2170 if (kmflags & KM_PAGEABLE) {
2172 * This is the unwiring case. We must first ensure that the
2173 * range to be unwired is really wired down. We know there
2176 entry = start_entry;
2177 while ((entry != &map->header) && (entry->start < end)) {
2178 if (entry->wired_count == 0) {
2179 rv = KERN_INVALID_ARGUMENT;
2182 entry = entry->next;
2186 * Now decrement the wiring count for each region. If a region
2187 * becomes completely unwired, unwire its physical pages and
2190 entry = start_entry;
2191 while ((entry != &map->header) && (entry->start < end)) {
2192 entry->wired_count--;
2193 if (entry->wired_count == 0)
2194 vm_fault_unwire(map, entry);
2195 entry = entry->next;
2199 vm_map_unclip_range(map, start_entry, start, real_end, &count,
2204 if (kmflags & KM_KRESERVE)
2205 vm_map_entry_krelease(count);
2207 vm_map_entry_release(count);
2212 * vm_map_set_wired_quick()
2214 * Mark a newly allocated address range as wired but do not fault in
2215 * the pages. The caller is expected to load the pages into the object.
2217 * The map must be locked on entry and will remain locked on return.
2220 vm_map_set_wired_quick(vm_map_t map, vm_offset_t addr, vm_size_t size, int *countp)
2222 vm_map_entry_t scan;
2223 vm_map_entry_t entry;
2225 entry = vm_map_clip_range(map, addr, addr + size, countp, MAP_CLIP_NO_HOLES);
2226 for (scan = entry; scan != &map->header && scan->start < addr + size; scan = scan->next) {
2227 KKASSERT(entry->wired_count == 0);
2228 entry->wired_count = 1;
2230 vm_map_unclip_range(map, entry, addr, addr + size, countp, MAP_CLIP_NO_HOLES);
2236 * Push any dirty cached pages in the address range to their pager.
2237 * If syncio is TRUE, dirty pages are written synchronously.
2238 * If invalidate is TRUE, any cached pages are freed as well.
2240 * Returns an error if any part of the specified range is not mapped.
2243 vm_map_clean(vm_map_t map, vm_offset_t start, vm_offset_t end, boolean_t syncio,
2244 boolean_t invalidate)
2246 vm_map_entry_t current;
2247 vm_map_entry_t entry;
2250 vm_ooffset_t offset;
2252 vm_map_lock_read(map);
2253 VM_MAP_RANGE_CHECK(map, start, end);
2254 if (!vm_map_lookup_entry(map, start, &entry)) {
2255 vm_map_unlock_read(map);
2256 return (KERN_INVALID_ADDRESS);
2259 * Make a first pass to check for holes.
2261 for (current = entry; current->start < end; current = current->next) {
2262 if (current->maptype == VM_MAPTYPE_SUBMAP) {
2263 vm_map_unlock_read(map);
2264 return (KERN_INVALID_ARGUMENT);
2266 if (end > current->end &&
2267 (current->next == &map->header ||
2268 current->end != current->next->start)) {
2269 vm_map_unlock_read(map);
2270 return (KERN_INVALID_ADDRESS);
2275 pmap_remove(vm_map_pmap(map), start, end);
2277 * Make a second pass, cleaning/uncaching pages from the indicated
2280 for (current = entry; current->start < end; current = current->next) {
2281 offset = current->offset + (start - current->start);
2282 size = (end <= current->end ? end : current->end) - start;
2283 if (current->maptype == VM_MAPTYPE_SUBMAP) {
2285 vm_map_entry_t tentry;
2288 smap = current->object.sub_map;
2289 vm_map_lock_read(smap);
2290 vm_map_lookup_entry(smap, offset, &tentry);
2291 tsize = tentry->end - offset;
2294 object = tentry->object.vm_object;
2295 offset = tentry->offset + (offset - tentry->start);
2296 vm_map_unlock_read(smap);
2298 object = current->object.vm_object;
2301 * Note that there is absolutely no sense in writing out
2302 * anonymous objects, so we track down the vnode object
2304 * We invalidate (remove) all pages from the address space
2305 * anyway, for semantic correctness.
2307 * note: certain anonymous maps, such as MAP_NOSYNC maps,
2308 * may start out with a NULL object.
2310 while (object && object->backing_object) {
2311 offset += object->backing_object_offset;
2312 object = object->backing_object;
2313 if (object->size < OFF_TO_IDX( offset + size))
2314 size = IDX_TO_OFF(object->size) - offset;
2316 if (object && (object->type == OBJT_VNODE) &&
2317 (current->protection & VM_PROT_WRITE)) {
2319 * Flush pages if writing is allowed, invalidate them
2320 * if invalidation requested. Pages undergoing I/O
2321 * will be ignored by vm_object_page_remove().
2323 * We cannot lock the vnode and then wait for paging
2324 * to complete without deadlocking against vm_fault.
2325 * Instead we simply call vm_object_page_remove() and
2326 * allow it to block internally on a page-by-page
2327 * basis when it encounters pages undergoing async
2332 vm_object_reference(object);
2333 vn_lock(object->handle, LK_EXCLUSIVE | LK_RETRY);
2334 flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
2335 flags |= invalidate ? OBJPC_INVAL : 0;
2338 * When operating on a virtual page table just
2339 * flush the whole object. XXX we probably ought
2342 switch(current->maptype) {
2343 case VM_MAPTYPE_NORMAL:
2344 vm_object_page_clean(object,
2346 OFF_TO_IDX(offset + size + PAGE_MASK),
2349 case VM_MAPTYPE_VPAGETABLE:
2350 vm_object_page_clean(object, 0, 0, flags);
2353 vn_unlock(((struct vnode *)object->handle));
2354 vm_object_deallocate(object);
2356 if (object && invalidate &&
2357 ((object->type == OBJT_VNODE) ||
2358 (object->type == OBJT_DEVICE))) {
2360 (object->type == OBJT_DEVICE) ? FALSE : TRUE;
2361 vm_object_reference(object);
2362 switch(current->maptype) {
2363 case VM_MAPTYPE_NORMAL:
2364 vm_object_page_remove(object,
2366 OFF_TO_IDX(offset + size + PAGE_MASK),
2369 case VM_MAPTYPE_VPAGETABLE:
2370 vm_object_page_remove(object, 0, 0, clean_only);
2373 vm_object_deallocate(object);
2378 vm_map_unlock_read(map);
2379 return (KERN_SUCCESS);
2383 * vm_map_entry_unwire: [ internal use only ]
2385 * Make the region specified by this entry pageable.
2387 * The map in question should be locked.
2388 * [This is the reason for this routine's existence.]
2391 vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
2393 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2394 entry->wired_count = 0;
2395 vm_fault_unwire(map, entry);
2399 * vm_map_entry_delete: [ internal use only ]
2401 * Deallocate the given entry from the target map.
2404 vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry, int *countp)
2406 vm_map_entry_unlink(map, entry);
2407 map->size -= entry->end - entry->start;
2409 switch(entry->maptype) {
2410 case VM_MAPTYPE_NORMAL:
2411 case VM_MAPTYPE_VPAGETABLE:
2412 vm_object_deallocate(entry->object.vm_object);
2418 vm_map_entry_dispose(map, entry, countp);
2422 * vm_map_delete: [ internal use only ]
2424 * Deallocates the given address range from the target
2428 vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end, int *countp)
2431 vm_map_entry_t entry;
2432 vm_map_entry_t first_entry;
2436 * Find the start of the region, and clip it. Set entry to point
2437 * at the first record containing the requested address or, if no
2438 * such record exists, the next record with a greater address. The
2439 * loop will run from this point until a record beyond the termination
2440 * address is encountered.
2442 * map->hint must be adjusted to not point to anything we delete,
2443 * so set it to the entry prior to the one being deleted.
2445 * GGG see other GGG comment.
2447 if (vm_map_lookup_entry(map, start, &first_entry)) {
2448 entry = first_entry;
2449 vm_map_clip_start(map, entry, start, countp);
2450 map->hint = entry->prev; /* possible problem XXX */
2452 map->hint = first_entry; /* possible problem XXX */
2453 entry = first_entry->next;
2457 * If a hole opens up prior to the current first_free then
2458 * adjust first_free. As with map->hint, map->first_free
2459 * cannot be left set to anything we might delete.
2461 if (entry == &map->header) {
2462 map->first_free = &map->header;
2463 } else if (map->first_free->start >= start) {
2464 map->first_free = entry->prev;
2468 * Step through all entries in this region
2471 while ((entry != &map->header) && (entry->start < end)) {
2472 vm_map_entry_t next;
2474 vm_pindex_t offidxstart, offidxend, count;
2477 * If we hit an in-transition entry we have to sleep and
2478 * retry. It's easier (and not really slower) to just retry
2479 * since this case occurs so rarely and the hint is already
2480 * pointing at the right place. We have to reset the
2481 * start offset so as not to accidently delete an entry
2482 * another process just created in vacated space.
2484 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
2485 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2486 start = entry->start;
2487 ++mycpu->gd_cnt.v_intrans_coll;
2488 ++mycpu->gd_cnt.v_intrans_wait;
2489 vm_map_transition_wait(map);
2492 vm_map_clip_end(map, entry, end, countp);
2498 offidxstart = OFF_TO_IDX(entry->offset);
2499 count = OFF_TO_IDX(e - s);
2500 object = entry->object.vm_object;
2503 * Unwire before removing addresses from the pmap; otherwise,
2504 * unwiring will put the entries back in the pmap.
2506 if (entry->wired_count != 0)
2507 vm_map_entry_unwire(map, entry);
2509 offidxend = offidxstart + count;
2511 if ((object == kernel_object) || (object == kmem_object)) {
2512 vm_object_page_remove(object, offidxstart, offidxend, FALSE);
2514 pmap_remove(map->pmap, s, e);
2515 if (object != NULL &&
2516 object->ref_count != 1 &&
2517 (object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING &&
2518 (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2519 vm_object_collapse(object);
2520 vm_object_page_remove(object, offidxstart, offidxend, FALSE);
2521 if (object->type == OBJT_SWAP) {
2522 swap_pager_freespace(object, offidxstart, count);
2524 if (offidxend >= object->size &&
2525 offidxstart < object->size) {
2526 object->size = offidxstart;
2532 * Delete the entry (which may delete the object) only after
2533 * removing all pmap entries pointing to its pages.
2534 * (Otherwise, its page frames may be reallocated, and any
2535 * modify bits will be set in the wrong object!)
2537 vm_map_entry_delete(map, entry, countp);
2540 return (KERN_SUCCESS);
2546 * Remove the given address range from the target map.
2547 * This is the exported form of vm_map_delete.
2550 vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
2555 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
2557 VM_MAP_RANGE_CHECK(map, start, end);
2558 result = vm_map_delete(map, start, end, &count);
2560 vm_map_entry_release(count);
2566 * vm_map_check_protection:
2568 * Assert that the target map allows the specified
2569 * privilege on the entire address region given.
2570 * The entire region must be allocated.
2573 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
2574 vm_prot_t protection)
2576 vm_map_entry_t entry;
2577 vm_map_entry_t tmp_entry;
2579 if (!vm_map_lookup_entry(map, start, &tmp_entry)) {
2584 while (start < end) {
2585 if (entry == &map->header) {
2592 if (start < entry->start) {
2596 * Check protection associated with entry.
2599 if ((entry->protection & protection) != protection) {
2602 /* go to next entry */
2605 entry = entry->next;
2611 * Split the pages in a map entry into a new object. This affords
2612 * easier removal of unused pages, and keeps object inheritance from
2613 * being a negative impact on memory usage.
2616 vm_map_split(vm_map_entry_t entry)
2619 vm_object_t orig_object, new_object, source;
2621 vm_pindex_t offidxstart, offidxend, idx;
2623 vm_ooffset_t offset;
2625 orig_object = entry->object.vm_object;
2626 if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
2628 if (orig_object->ref_count <= 1)
2631 offset = entry->offset;
2635 offidxstart = OFF_TO_IDX(offset);
2636 offidxend = offidxstart + OFF_TO_IDX(e - s);
2637 size = offidxend - offidxstart;
2639 new_object = vm_pager_allocate(orig_object->type, NULL,
2640 IDX_TO_OFF(size), VM_PROT_ALL, 0);
2641 if (new_object == NULL)
2644 source = orig_object->backing_object;
2645 if (source != NULL) {
2646 vm_object_reference(source); /* Referenced by new_object */
2647 LIST_INSERT_HEAD(&source->shadow_head,
2648 new_object, shadow_list);
2649 vm_object_clear_flag(source, OBJ_ONEMAPPING);
2650 new_object->backing_object_offset =
2651 orig_object->backing_object_offset + IDX_TO_OFF(offidxstart);
2652 new_object->backing_object = source;
2653 source->shadow_count++;
2654 source->generation++;
2657 for (idx = 0; idx < size; idx++) {
2661 * A critical section is required to avoid a race between
2662 * the lookup and an interrupt/unbusy/free and our busy
2667 m = vm_page_lookup(orig_object, offidxstart + idx);
2674 * We must wait for pending I/O to complete before we can
2677 * We do not have to VM_PROT_NONE the page as mappings should
2678 * not be changed by this operation.
2680 if (vm_page_sleep_busy(m, TRUE, "spltwt"))
2683 vm_page_rename(m, new_object, idx);
2684 /* page automatically made dirty by rename and cache handled */
2689 if (orig_object->type == OBJT_SWAP) {
2690 vm_object_pip_add(orig_object, 1);
2692 * copy orig_object pages into new_object
2693 * and destroy unneeded pages in
2696 swap_pager_copy(orig_object, new_object, offidxstart, 0);
2697 vm_object_pip_wakeup(orig_object);
2701 * Wakeup the pages we played with. No spl protection is needed
2702 * for a simple wakeup.
2704 for (idx = 0; idx < size; idx++) {
2705 m = vm_page_lookup(new_object, idx);
2710 entry->object.vm_object = new_object;
2711 entry->offset = 0LL;
2712 vm_object_deallocate(orig_object);
2716 * vm_map_copy_entry:
2718 * Copies the contents of the source entry to the destination
2719 * entry. The entries *must* be aligned properly.
2722 vm_map_copy_entry(vm_map_t src_map, vm_map_t dst_map,
2723 vm_map_entry_t src_entry, vm_map_entry_t dst_entry)
2725 vm_object_t src_object;
2727 if (dst_entry->maptype == VM_MAPTYPE_SUBMAP)
2729 if (src_entry->maptype == VM_MAPTYPE_SUBMAP)
2732 if (src_entry->wired_count == 0) {
2734 * If the source entry is marked needs_copy, it is already
2737 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
2738 pmap_protect(src_map->pmap,
2741 src_entry->protection & ~VM_PROT_WRITE);
2745 * Make a copy of the object.
2747 if ((src_object = src_entry->object.vm_object) != NULL) {
2748 if ((src_object->handle == NULL) &&
2749 (src_object->type == OBJT_DEFAULT ||
2750 src_object->type == OBJT_SWAP)) {
2751 vm_object_collapse(src_object);
2752 if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
2753 vm_map_split(src_entry);
2754 src_object = src_entry->object.vm_object;
2758 vm_object_reference(src_object);
2759 vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
2760 dst_entry->object.vm_object = src_object;
2761 src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2762 dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2763 dst_entry->offset = src_entry->offset;
2765 dst_entry->object.vm_object = NULL;
2766 dst_entry->offset = 0;
2769 pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
2770 dst_entry->end - dst_entry->start, src_entry->start);
2773 * Of course, wired down pages can't be set copy-on-write.
2774 * Cause wired pages to be copied into the new map by
2775 * simulating faults (the new pages are pageable)
2777 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry);
2783 * Create a new process vmspace structure and vm_map
2784 * based on those of an existing process. The new map
2785 * is based on the old map, according to the inheritance
2786 * values on the regions in that map.
2788 * The source map must not be locked.
2791 vmspace_fork(struct vmspace *vm1)
2793 struct vmspace *vm2;
2794 vm_map_t old_map = &vm1->vm_map;
2796 vm_map_entry_t old_entry;
2797 vm_map_entry_t new_entry;
2801 vm_map_lock(old_map);
2802 old_map->infork = 1;
2805 * XXX Note: upcalls are not copied.
2807 vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
2808 bcopy(&vm1->vm_startcopy, &vm2->vm_startcopy,
2809 (caddr_t)&vm1->vm_endcopy - (caddr_t)&vm1->vm_startcopy);
2810 new_map = &vm2->vm_map; /* XXX */
2811 new_map->timestamp = 1;
2814 old_entry = old_map->header.next;
2815 while (old_entry != &old_map->header) {
2817 old_entry = old_entry->next;
2820 count = vm_map_entry_reserve(count + MAP_RESERVE_COUNT);
2822 old_entry = old_map->header.next;
2823 while (old_entry != &old_map->header) {
2824 if (old_entry->maptype == VM_MAPTYPE_SUBMAP)
2825 panic("vm_map_fork: encountered a submap");
2827 switch (old_entry->inheritance) {
2828 case VM_INHERIT_NONE:
2831 case VM_INHERIT_SHARE:
2833 * Clone the entry, creating the shared object if
2836 object = old_entry->object.vm_object;
2837 if (object == NULL) {
2838 vm_map_entry_allocate_object(old_entry);
2839 object = old_entry->object.vm_object;
2843 * Add the reference before calling vm_map_entry_shadow
2844 * to insure that a shadow object is created.
2846 vm_object_reference(object);
2847 if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
2848 vm_map_entry_shadow(old_entry);
2849 /* Transfer the second reference too. */
2850 vm_object_reference(
2851 old_entry->object.vm_object);
2852 vm_object_deallocate(object);
2853 object = old_entry->object.vm_object;
2855 vm_object_clear_flag(object, OBJ_ONEMAPPING);
2858 * Clone the entry, referencing the shared object.
2860 new_entry = vm_map_entry_create(new_map, &count);
2861 *new_entry = *old_entry;
2862 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2863 new_entry->wired_count = 0;
2866 * Insert the entry into the new map -- we know we're
2867 * inserting at the end of the new map.
2870 vm_map_entry_link(new_map, new_map->header.prev,
2874 * Update the physical map
2877 pmap_copy(new_map->pmap, old_map->pmap,
2879 (old_entry->end - old_entry->start),
2883 case VM_INHERIT_COPY:
2885 * Clone the entry and link into the map.
2887 new_entry = vm_map_entry_create(new_map, &count);
2888 *new_entry = *old_entry;
2889 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2890 new_entry->wired_count = 0;
2891 new_entry->object.vm_object = NULL;
2892 vm_map_entry_link(new_map, new_map->header.prev,
2894 vm_map_copy_entry(old_map, new_map, old_entry,
2898 old_entry = old_entry->next;
2901 new_map->size = old_map->size;
2902 old_map->infork = 0;
2903 vm_map_unlock(old_map);
2904 vm_map_entry_release(count);
2910 vm_map_stack (vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
2911 vm_prot_t prot, vm_prot_t max, int cow)
2913 vm_map_entry_t prev_entry;
2914 vm_map_entry_t new_stack_entry;
2915 vm_size_t init_ssize;
2919 if (VM_MIN_USER_ADDRESS > 0 && addrbos < VM_MIN_USER_ADDRESS)
2920 return (KERN_NO_SPACE);
2922 if (max_ssize < sgrowsiz)
2923 init_ssize = max_ssize;
2925 init_ssize = sgrowsiz;
2927 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
2930 /* If addr is already mapped, no go */
2931 if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
2933 vm_map_entry_release(count);
2934 return (KERN_NO_SPACE);
2937 /* If we would blow our VMEM resource limit, no go */
2938 if (map->size + init_ssize >
2939 curproc->p_rlimit[RLIMIT_VMEM].rlim_cur) {
2941 vm_map_entry_release(count);
2942 return (KERN_NO_SPACE);
2945 /* If we can't accomodate max_ssize in the current mapping,
2946 * no go. However, we need to be aware that subsequent user
2947 * mappings might map into the space we have reserved for
2948 * stack, and currently this space is not protected.
2950 * Hopefully we will at least detect this condition
2951 * when we try to grow the stack.
2953 if ((prev_entry->next != &map->header) &&
2954 (prev_entry->next->start < addrbos + max_ssize)) {
2956 vm_map_entry_release(count);
2957 return (KERN_NO_SPACE);
2960 /* We initially map a stack of only init_ssize. We will
2961 * grow as needed later. Since this is to be a grow
2962 * down stack, we map at the top of the range.
2964 * Note: we would normally expect prot and max to be
2965 * VM_PROT_ALL, and cow to be 0. Possibly we should
2966 * eliminate these as input parameters, and just
2967 * pass these values here in the insert call.
2969 rv = vm_map_insert(map, &count,
2970 NULL, 0, addrbos + max_ssize - init_ssize,
2971 addrbos + max_ssize,
2976 /* Now set the avail_ssize amount */
2977 if (rv == KERN_SUCCESS) {
2978 if (prev_entry != &map->header)
2979 vm_map_clip_end(map, prev_entry, addrbos + max_ssize - init_ssize, &count);
2980 new_stack_entry = prev_entry->next;
2981 if (new_stack_entry->end != addrbos + max_ssize ||
2982 new_stack_entry->start != addrbos + max_ssize - init_ssize)
2983 panic ("Bad entry start/end for new stack entry");
2985 new_stack_entry->aux.avail_ssize = max_ssize - init_ssize;
2989 vm_map_entry_release(count);
2993 /* Attempts to grow a vm stack entry. Returns KERN_SUCCESS if the
2994 * desired address is already mapped, or if we successfully grow
2995 * the stack. Also returns KERN_SUCCESS if addr is outside the
2996 * stack range (this is strange, but preserves compatibility with
2997 * the grow function in vm_machdep.c).
3000 vm_map_growstack (struct proc *p, vm_offset_t addr)
3002 vm_map_entry_t prev_entry;
3003 vm_map_entry_t stack_entry;
3004 vm_map_entry_t new_stack_entry;
3005 struct vmspace *vm = p->p_vmspace;
3006 vm_map_t map = &vm->vm_map;
3009 int rv = KERN_SUCCESS;
3011 int use_read_lock = 1;
3014 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
3017 vm_map_lock_read(map);
3021 /* If addr is already in the entry range, no need to grow.*/
3022 if (vm_map_lookup_entry(map, addr, &prev_entry))
3025 if ((stack_entry = prev_entry->next) == &map->header)
3027 if (prev_entry == &map->header)
3028 end = stack_entry->start - stack_entry->aux.avail_ssize;
3030 end = prev_entry->end;
3032 /* This next test mimics the old grow function in vm_machdep.c.
3033 * It really doesn't quite make sense, but we do it anyway
3034 * for compatibility.
3036 * If not growable stack, return success. This signals the
3037 * caller to proceed as he would normally with normal vm.
3039 if (stack_entry->aux.avail_ssize < 1 ||
3040 addr >= stack_entry->start ||
3041 addr < stack_entry->start - stack_entry->aux.avail_ssize) {
3045 /* Find the minimum grow amount */
3046 grow_amount = roundup (stack_entry->start - addr, PAGE_SIZE);
3047 if (grow_amount > stack_entry->aux.avail_ssize) {
3052 /* If there is no longer enough space between the entries
3053 * nogo, and adjust the available space. Note: this
3054 * should only happen if the user has mapped into the
3055 * stack area after the stack was created, and is
3056 * probably an error.
3058 * This also effectively destroys any guard page the user
3059 * might have intended by limiting the stack size.
3061 if (grow_amount > stack_entry->start - end) {
3062 if (use_read_lock && vm_map_lock_upgrade(map)) {
3067 stack_entry->aux.avail_ssize = stack_entry->start - end;
3072 is_procstack = addr >= (vm_offset_t)vm->vm_maxsaddr;
3074 /* If this is the main process stack, see if we're over the
3077 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount >
3078 p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
3083 /* Round up the grow amount modulo SGROWSIZ */
3084 grow_amount = roundup (grow_amount, sgrowsiz);
3085 if (grow_amount > stack_entry->aux.avail_ssize) {
3086 grow_amount = stack_entry->aux.avail_ssize;
3088 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount >
3089 p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
3090 grow_amount = p->p_rlimit[RLIMIT_STACK].rlim_cur -
3094 /* If we would blow our VMEM resource limit, no go */
3095 if (map->size + grow_amount > p->p_rlimit[RLIMIT_VMEM].rlim_cur) {
3100 if (use_read_lock && vm_map_lock_upgrade(map)) {
3106 /* Get the preliminary new entry start value */
3107 addr = stack_entry->start - grow_amount;
3109 /* If this puts us into the previous entry, cut back our growth
3110 * to the available space. Also, see the note above.
3113 stack_entry->aux.avail_ssize = stack_entry->start - end;
3117 rv = vm_map_insert(map, &count,
3118 NULL, 0, addr, stack_entry->start,
3120 VM_PROT_ALL, VM_PROT_ALL,
3123 /* Adjust the available stack space by the amount we grew. */
3124 if (rv == KERN_SUCCESS) {
3125 if (prev_entry != &map->header)
3126 vm_map_clip_end(map, prev_entry, addr, &count);
3127 new_stack_entry = prev_entry->next;
3128 if (new_stack_entry->end != stack_entry->start ||
3129 new_stack_entry->start != addr)
3130 panic ("Bad stack grow start/end in new stack entry");
3132 new_stack_entry->aux.avail_ssize =
3133 stack_entry->aux.avail_ssize -
3134 (new_stack_entry->end - new_stack_entry->start);
3136 vm->vm_ssize += btoc(new_stack_entry->end -
3137 new_stack_entry->start);
3143 vm_map_unlock_read(map);
3146 vm_map_entry_release(count);
3151 * Unshare the specified VM space for exec. If other processes are
3152 * mapped to it, then create a new one. The new vmspace is null.
3156 vmspace_exec(struct proc *p, struct vmspace *vmcopy)
3158 struct vmspace *oldvmspace = p->p_vmspace;
3159 struct vmspace *newvmspace;
3160 vm_map_t map = &p->p_vmspace->vm_map;
3163 * If we are execing a resident vmspace we fork it, otherwise
3164 * we create a new vmspace. Note that exitingcnt and upcalls
3165 * are not copied to the new vmspace.
3168 newvmspace = vmspace_fork(vmcopy);
3170 newvmspace = vmspace_alloc(map->min_offset, map->max_offset);
3171 bcopy(&oldvmspace->vm_startcopy, &newvmspace->vm_startcopy,
3172 (caddr_t)&oldvmspace->vm_endcopy -
3173 (caddr_t)&oldvmspace->vm_startcopy);
3177 * This code is written like this for prototype purposes. The
3178 * goal is to avoid running down the vmspace here, but let the
3179 * other process's that are still using the vmspace to finally
3180 * run it down. Even though there is little or no chance of blocking
3181 * here, it is a good idea to keep this form for future mods.
3183 p->p_vmspace = newvmspace;
3184 pmap_pinit2(vmspace_pmap(newvmspace));
3187 vmspace_free(oldvmspace);
3191 * Unshare the specified VM space for forcing COW. This
3192 * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
3194 * The exitingcnt test is not strictly necessary but has been
3195 * included for code sanity (to make the code a bit more deterministic).
3199 vmspace_unshare(struct proc *p)
3201 struct vmspace *oldvmspace = p->p_vmspace;
3202 struct vmspace *newvmspace;
3204 if (oldvmspace->vm_refcnt == 1 && oldvmspace->vm_exitingcnt == 0)
3206 newvmspace = vmspace_fork(oldvmspace);
3207 p->p_vmspace = newvmspace;
3208 pmap_pinit2(vmspace_pmap(newvmspace));
3211 vmspace_free(oldvmspace);
3217 * Finds the VM object, offset, and
3218 * protection for a given virtual address in the
3219 * specified map, assuming a page fault of the
3222 * Leaves the map in question locked for read; return
3223 * values are guaranteed until a vm_map_lookup_done
3224 * call is performed. Note that the map argument
3225 * is in/out; the returned map must be used in
3226 * the call to vm_map_lookup_done.
3228 * A handle (out_entry) is returned for use in
3229 * vm_map_lookup_done, to make that fast.
3231 * If a lookup is requested with "write protection"
3232 * specified, the map may be changed to perform virtual
3233 * copying operations, although the data referenced will
3237 vm_map_lookup(vm_map_t *var_map, /* IN/OUT */
3239 vm_prot_t fault_typea,
3240 vm_map_entry_t *out_entry, /* OUT */
3241 vm_object_t *object, /* OUT */
3242 vm_pindex_t *pindex, /* OUT */
3243 vm_prot_t *out_prot, /* OUT */
3244 boolean_t *wired) /* OUT */
3246 vm_map_entry_t entry;
3247 vm_map_t map = *var_map;
3249 vm_prot_t fault_type = fault_typea;
3250 int use_read_lock = 1;
3251 int rv = KERN_SUCCESS;
3255 vm_map_lock_read(map);
3260 * If the map has an interesting hint, try it before calling full
3261 * blown lookup routine.
3266 if ((entry == &map->header) ||
3267 (vaddr < entry->start) || (vaddr >= entry->end)) {
3268 vm_map_entry_t tmp_entry;
3271 * Entry was either not a valid hint, or the vaddr was not
3272 * contained in the entry, so do a full lookup.
3274 if (!vm_map_lookup_entry(map, vaddr, &tmp_entry)) {
3275 rv = KERN_INVALID_ADDRESS;
3286 if (entry->maptype == VM_MAPTYPE_SUBMAP) {
3287 vm_map_t old_map = map;
3289 *var_map = map = entry->object.sub_map;
3291 vm_map_unlock_read(old_map);
3293 vm_map_unlock(old_map);
3299 * Check whether this task is allowed to have this page.
3300 * Note the special case for MAP_ENTRY_COW
3301 * pages with an override. This is to implement a forced
3302 * COW for debuggers.
3305 if (fault_type & VM_PROT_OVERRIDE_WRITE)
3306 prot = entry->max_protection;
3308 prot = entry->protection;
3310 fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
3311 if ((fault_type & prot) != fault_type) {
3312 rv = KERN_PROTECTION_FAILURE;
3316 if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
3317 (entry->eflags & MAP_ENTRY_COW) &&
3318 (fault_type & VM_PROT_WRITE) &&
3319 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
3320 rv = KERN_PROTECTION_FAILURE;
3325 * If this page is not pageable, we have to get it for all possible
3328 *wired = (entry->wired_count != 0);
3330 prot = fault_type = entry->protection;
3333 * Virtual page tables may need to update the accessed (A) bit
3334 * in a page table entry. Upgrade the fault to a write fault for
3335 * that case if the map will support it. If the map does not support
3336 * it the page table entry simply will not be updated.
3338 if (entry->maptype == VM_MAPTYPE_VPAGETABLE) {
3339 if (prot & VM_PROT_WRITE)
3340 fault_type |= VM_PROT_WRITE;
3344 * If the entry was copy-on-write, we either ...
3346 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3348 * If we want to write the page, we may as well handle that
3349 * now since we've got the map locked.
3351 * If we don't need to write the page, we just demote the
3352 * permissions allowed.
3355 if (fault_type & VM_PROT_WRITE) {
3357 * Make a new object, and place it in the object
3358 * chain. Note that no new references have appeared
3359 * -- one just moved from the map to the new
3363 if (use_read_lock && vm_map_lock_upgrade(map)) {
3369 vm_map_entry_shadow(entry);
3372 * We're attempting to read a copy-on-write page --
3373 * don't allow writes.
3376 prot &= ~VM_PROT_WRITE;
3381 * Create an object if necessary.
3383 if (entry->object.vm_object == NULL &&
3385 if (use_read_lock && vm_map_lock_upgrade(map)) {
3390 vm_map_entry_allocate_object(entry);
3394 * Return the object/offset from this entry. If the entry was
3395 * copy-on-write or empty, it has been fixed up.
3398 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
3399 *object = entry->object.vm_object;
3402 * Return whether this is the only map sharing this data. On
3403 * success we return with a read lock held on the map. On failure
3404 * we return with the map unlocked.
3408 if (rv == KERN_SUCCESS) {
3409 if (use_read_lock == 0)
3410 vm_map_lock_downgrade(map);
3411 } else if (use_read_lock) {
3412 vm_map_unlock_read(map);
3420 * vm_map_lookup_done:
3422 * Releases locks acquired by a vm_map_lookup
3423 * (according to the handle returned by that lookup).
3427 vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry, int count)
3430 * Unlock the main-level map
3432 vm_map_unlock_read(map);
3434 vm_map_entry_release(count);
3437 #include "opt_ddb.h"
3439 #include <sys/kernel.h>
3441 #include <ddb/ddb.h>
3444 * vm_map_print: [ debug ]
3446 DB_SHOW_COMMAND(map, vm_map_print)
3449 /* XXX convert args. */
3450 vm_map_t map = (vm_map_t)addr;
3451 boolean_t full = have_addr;
3453 vm_map_entry_t entry;
3455 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
3457 (void *)map->pmap, map->nentries, map->timestamp);
3460 if (!full && db_indent)
3464 for (entry = map->header.next; entry != &map->header;
3465 entry = entry->next) {
3466 db_iprintf("map entry %p: start=%p, end=%p\n",
3467 (void *)entry, (void *)entry->start, (void *)entry->end);
3470 static char *inheritance_name[4] =
3471 {"share", "copy", "none", "donate_copy"};
3473 db_iprintf(" prot=%x/%x/%s",
3475 entry->max_protection,
3476 inheritance_name[(int)(unsigned char)entry->inheritance]);
3477 if (entry->wired_count != 0)
3478 db_printf(", wired");
3480 if (entry->maptype == VM_MAPTYPE_SUBMAP) {
3481 /* XXX no %qd in kernel. Truncate entry->offset. */
3482 db_printf(", share=%p, offset=0x%lx\n",
3483 (void *)entry->object.sub_map,
3484 (long)entry->offset);
3486 if ((entry->prev == &map->header) ||
3487 (entry->prev->object.sub_map !=
3488 entry->object.sub_map)) {
3490 vm_map_print((db_expr_t)(intptr_t)
3491 entry->object.sub_map,
3492 full, 0, (char *)0);
3496 /* XXX no %qd in kernel. Truncate entry->offset. */
3497 db_printf(", object=%p, offset=0x%lx",
3498 (void *)entry->object.vm_object,
3499 (long)entry->offset);
3500 if (entry->eflags & MAP_ENTRY_COW)
3501 db_printf(", copy (%s)",
3502 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
3506 if ((entry->prev == &map->header) ||
3507 (entry->prev->object.vm_object !=
3508 entry->object.vm_object)) {
3510 vm_object_print((db_expr_t)(intptr_t)
3511 entry->object.vm_object,
3512 full, 0, (char *)0);
3524 DB_SHOW_COMMAND(procvm, procvm)
3529 p = (struct proc *) addr;
3534 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
3535 (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
3536 (void *)vmspace_pmap(p->p_vmspace));
3538 vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);