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.15 2003/11/21 05:29:08 dillon Exp $
69 * Virtual memory mapping module.
72 #include <sys/param.h>
73 #include <sys/systm.h>
76 #include <sys/vmmeter.h>
78 #include <sys/vnode.h>
79 #include <sys/resourcevar.h>
83 #include <vm/vm_param.h>
85 #include <vm/vm_map.h>
86 #include <vm/vm_page.h>
87 #include <vm/vm_object.h>
88 #include <vm/vm_pager.h>
89 #include <vm/vm_kern.h>
90 #include <vm/vm_extern.h>
91 #include <vm/swap_pager.h>
92 #include <vm/vm_zone.h>
94 #include <sys/thread2.h>
97 * Virtual memory maps provide for the mapping, protection,
98 * and sharing of virtual memory objects. In addition,
99 * this module provides for an efficient virtual copy of
100 * memory from one map to another.
102 * Synchronization is required prior to most operations.
104 * Maps consist of an ordered doubly-linked list of simple
105 * entries; a single hint is used to speed up lookups.
107 * Since portions of maps are specified by start/end addresses,
108 * which may not align with existing map entries, all
109 * routines merely "clip" entries to these start/end values.
110 * [That is, an entry is split into two, bordering at a
111 * start or end value.] Note that these clippings may not
112 * always be necessary (as the two resulting entries are then
113 * not changed); however, the clipping is done for convenience.
115 * As mentioned above, virtual copy operations are performed
116 * by copying VM object references from one map to
117 * another, and then marking both regions as copy-on-write.
123 * Initialize the vm_map module. Must be called before
124 * any other vm_map routines.
126 * Map and entry structures are allocated from the general
127 * purpose memory pool with some exceptions:
129 * - The kernel map and kmem submap are allocated statically.
130 * - Kernel map entries are allocated out of a static pool.
132 * These restrictions are necessary since malloc() uses the
133 * maps and requires map entries.
136 static struct vm_zone mapentzone_store, mapzone_store;
137 static vm_zone_t mapentzone, mapzone, vmspace_zone;
138 static struct vm_object mapentobj, mapobj;
140 static struct vm_map_entry map_entry_init[MAX_MAPENT];
141 static struct vm_map map_init[MAX_KMAP];
143 static vm_map_entry_t vm_map_entry_create(vm_map_t map, int *);
144 static void vm_map_entry_dispose (vm_map_t map, vm_map_entry_t entry, int *);
145 static void _vm_map_clip_end (vm_map_t, vm_map_entry_t, vm_offset_t, int *);
146 static void _vm_map_clip_start (vm_map_t, vm_map_entry_t, vm_offset_t, int *);
147 static void vm_map_entry_delete (vm_map_t, vm_map_entry_t, int *);
148 static void vm_map_entry_unwire (vm_map_t, vm_map_entry_t);
149 static void vm_map_copy_entry (vm_map_t, vm_map_t, vm_map_entry_t,
151 static void vm_map_split (vm_map_entry_t);
152 static void vm_map_unclip_range (vm_map_t map, vm_map_entry_t start_entry, vm_offset_t start, vm_offset_t end, int *count, int flags);
157 mapzone = &mapzone_store;
158 zbootinit(mapzone, "MAP", sizeof (struct vm_map),
160 mapentzone = &mapentzone_store;
161 zbootinit(mapentzone, "MAP ENTRY", sizeof (struct vm_map_entry),
162 map_entry_init, MAX_MAPENT);
166 * Allocate a vmspace structure, including a vm_map and pmap,
167 * and initialize those structures. The refcnt is set to 1.
168 * The remaining fields must be initialized by the caller.
171 vmspace_alloc(min, max)
172 vm_offset_t min, max;
176 vm = zalloc(vmspace_zone);
177 vm_map_init(&vm->vm_map, min, max);
178 pmap_pinit(vmspace_pmap(vm));
179 vm->vm_map.pmap = vmspace_pmap(vm); /* XXX */
182 vm->vm_exitingcnt = 0;
189 zinitna(mapentzone, &mapentobj, NULL, 0, 0, ZONE_USE_RESERVE, 1);
190 zinitna(mapzone, &mapobj, NULL, 0, 0, 0, 1);
191 vmspace_zone = zinit("VMSPACE", sizeof (struct vmspace), 0, 0, 3);
197 vmspace_dofree(struct vmspace *vm)
202 * Make sure any SysV shm is freed, it might not have in
207 KKASSERT(vm->vm_upcalls == NULL);
210 * Lock the map, to wait out all other references to it.
211 * Delete all of the mappings and pages they hold, then call
212 * the pmap module to reclaim anything left.
214 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
215 vm_map_lock(&vm->vm_map);
216 vm_map_delete(&vm->vm_map, vm->vm_map.min_offset,
217 vm->vm_map.max_offset, &count);
218 vm_map_unlock(&vm->vm_map);
219 vm_map_entry_release(count);
221 pmap_release(vmspace_pmap(vm));
222 zfree(vmspace_zone, vm);
226 vmspace_free(struct vmspace *vm)
228 if (vm->vm_refcnt == 0)
229 panic("vmspace_free: attempt to free already freed vmspace");
231 if (--vm->vm_refcnt == 0 && vm->vm_exitingcnt == 0)
236 vmspace_exitfree(struct proc *p)
244 * cleanup by parent process wait()ing on exiting child. vm_refcnt
245 * may not be 0 (e.g. fork() and child exits without exec()ing).
246 * exitingcnt may increment above 0 and drop back down to zero
247 * several times while vm_refcnt is held non-zero. vm_refcnt
248 * may also increment above 0 and drop back down to zero several
249 * times while vm_exitingcnt is held non-zero.
251 * The last wait on the exiting child's vmspace will clean up
252 * the remainder of the vmspace.
254 if (--vm->vm_exitingcnt == 0 && vm->vm_refcnt == 0)
259 * vmspace_swap_count() - count the approximate swap useage in pages for a
262 * Swap useage is determined by taking the proportional swap used by
263 * VM objects backing the VM map. To make up for fractional losses,
264 * if the VM object has any swap use at all the associated map entries
265 * count for at least 1 swap page.
268 vmspace_swap_count(struct vmspace *vmspace)
270 vm_map_t map = &vmspace->vm_map;
274 for (cur = map->header.next; cur != &map->header; cur = cur->next) {
277 if ((cur->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
278 (object = cur->object.vm_object) != NULL &&
279 object->type == OBJT_SWAP
281 int n = (cur->end - cur->start) / PAGE_SIZE;
283 if (object->un_pager.swp.swp_bcount) {
284 count += object->un_pager.swp.swp_bcount *
285 SWAP_META_PAGES * n / object->size + 1;
296 * Creates and returns a new empty VM map with
297 * the given physical map structure, and having
298 * the given lower and upper address bounds.
301 vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max)
305 result = zalloc(mapzone);
306 vm_map_init(result, min, max);
312 * Initialize an existing vm_map structure
313 * such as that in the vmspace structure.
314 * The pmap is set elsewhere.
317 vm_map_init(struct vm_map *map, vm_offset_t min, vm_offset_t max)
319 map->header.next = map->header.prev = &map->header;
324 map->min_offset = min;
325 map->max_offset = max;
326 map->first_free = &map->header;
327 map->hint = &map->header;
329 lockinit(&map->lock, 0, "thrd_sleep", 0, LK_NOPAUSE);
333 * vm_map_entry_reserve:
335 * Reserves vm_map_entry structures outside of the critical path
338 vm_map_entry_reserve(int count)
340 struct globaldata *gd = mycpu;
341 vm_map_entry_t entry;
344 gd->gd_vme_avail -= count;
347 * Make sure we have enough structures in gd_vme_base to handle
348 * the reservation request.
350 while (gd->gd_vme_avail < 0) {
351 entry = zalloc(mapentzone);
352 entry->next = gd->gd_vme_base;
353 gd->gd_vme_base = entry;
361 * vm_map_entry_release:
363 * Releases previously reserved vm_map_entry structures that were not
364 * used. If we have too much junk in our per-cpu cache clean some of
368 vm_map_entry_release(int count)
370 struct globaldata *gd = mycpu;
371 vm_map_entry_t entry;
374 gd->gd_vme_avail += count;
375 while (gd->gd_vme_avail > MAP_RESERVE_SLOP) {
376 entry = gd->gd_vme_base;
377 KKASSERT(entry != NULL);
378 gd->gd_vme_base = entry->next;
381 zfree(mapentzone, entry);
388 * vm_map_entry_kreserve:
390 * Reserve map entry structures for use in kernel_map or (if it exists)
391 * kmem_map. These entries have *ALREADY* been reserved on a per-cpu
394 * XXX if multiple kernel map entries are used without any intervening
395 * use by another map the KKASSERT() may assert.
398 vm_map_entry_kreserve(int count)
400 struct globaldata *gd = mycpu;
403 gd->gd_vme_kdeficit += count;
405 KKASSERT(gd->gd_vme_base != NULL);
410 * vm_map_entry_krelease:
412 * Release previously reserved map entries for kernel_map or kmem_map
413 * use. This routine determines how many entries were actually used and
414 * replentishes the kernel reserve supply from vme_avail.
416 * If there is insufficient supply vme_avail will go negative, which is
417 * ok. We cannot safely call zalloc in this function without getting
418 * into a recursion deadlock. zalloc() will call vm_map_entry_reserve()
419 * to regenerate the lost entries.
422 vm_map_entry_krelease(int count)
424 struct globaldata *gd = mycpu;
427 gd->gd_vme_kdeficit -= count;
428 gd->gd_vme_avail -= gd->gd_vme_kdeficit; /* can go negative */
429 gd->gd_vme_kdeficit = 0;
434 * vm_map_entry_create: [ internal use only ]
436 * Allocates a VM map entry for insertion. No entry fields are filled
439 * This routine may be called from an interrupt thread but not a FAST
440 * interrupt. This routine may recurse the map lock.
442 static vm_map_entry_t
443 vm_map_entry_create(vm_map_t map, int *countp)
445 struct globaldata *gd = mycpu;
446 vm_map_entry_t entry;
448 KKASSERT(*countp > 0);
451 entry = gd->gd_vme_base;
452 KASSERT(entry != NULL, ("gd_vme_base NULL! count %d", *countp));
453 gd->gd_vme_base = entry->next;
459 * vm_map_entry_dispose: [ internal use only ]
461 * Dispose of a vm_map_entry that is no longer being referenced. This
462 * function may be called from an interrupt.
465 vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry, int *countp)
467 struct globaldata *gd = mycpu;
471 entry->next = gd->gd_vme_base;
472 gd->gd_vme_base = entry;
478 * vm_map_entry_{un,}link:
480 * Insert/remove entries from maps.
483 vm_map_entry_link(vm_map_t map,
484 vm_map_entry_t after_where,
485 vm_map_entry_t entry)
488 entry->prev = after_where;
489 entry->next = after_where->next;
490 entry->next->prev = entry;
491 after_where->next = entry;
495 vm_map_entry_unlink(vm_map_t map,
496 vm_map_entry_t entry)
501 if (entry->eflags & MAP_ENTRY_IN_TRANSITION)
502 panic("vm_map_entry_unlink: attempt to mess with locked entry! %p", entry);
513 * Saves the specified entry as the hint for
516 #define SAVE_HINT(map,value) \
517 (map)->hint = (value);
520 * vm_map_lookup_entry: [ internal use only ]
522 * Finds the map entry containing (or
523 * immediately preceding) the specified address
524 * in the given map; the entry is returned
525 * in the "entry" parameter. The boolean
526 * result indicates whether the address is
527 * actually contained in the map.
530 vm_map_lookup_entry(map, address, entry)
533 vm_map_entry_t *entry; /* OUT */
539 * Start looking either from the head of the list, or from the hint.
544 if (cur == &map->header)
547 if (address >= cur->start) {
549 * Go from hint to end of list.
551 * But first, make a quick check to see if we are already looking
552 * at the entry we want (which is usually the case). Note also
553 * that we don't need to save the hint here... it is the same
554 * hint (unless we are at the header, in which case the hint
555 * didn't buy us anything anyway).
558 if ((cur != last) && (cur->end > address)) {
564 * Go from start to hint, *inclusively*
567 cur = map->header.next;
574 while (cur != last) {
575 if (cur->end > address) {
576 if (address >= cur->start) {
578 * Save this lookup for future hints, and
591 SAVE_HINT(map, *entry);
598 * Inserts the given whole VM object into the target
599 * map at the specified address range. The object's
600 * size should match that of the address range.
602 * Requires that the map be locked, and leaves it so. Requires that
603 * sufficient vm_map_entry structures have been reserved and tracks
604 * the use via countp.
606 * If object is non-NULL, ref count must be bumped by caller
607 * prior to making call to account for the new entry.
610 vm_map_insert(vm_map_t map, int *countp,
611 vm_object_t object, vm_ooffset_t offset,
612 vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max,
615 vm_map_entry_t new_entry;
616 vm_map_entry_t prev_entry;
617 vm_map_entry_t temp_entry;
618 vm_eflags_t protoeflags;
621 * Check that the start and end points are not bogus.
624 if ((start < map->min_offset) || (end > map->max_offset) ||
626 return (KERN_INVALID_ADDRESS);
629 * Find the entry prior to the proposed starting address; if it's part
630 * of an existing entry, this range is bogus.
633 if (vm_map_lookup_entry(map, start, &temp_entry))
634 return (KERN_NO_SPACE);
636 prev_entry = temp_entry;
639 * Assert that the next entry doesn't overlap the end point.
642 if ((prev_entry->next != &map->header) &&
643 (prev_entry->next->start < end))
644 return (KERN_NO_SPACE);
648 if (cow & MAP_COPY_ON_WRITE)
649 protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
651 if (cow & MAP_NOFAULT) {
652 protoeflags |= MAP_ENTRY_NOFAULT;
654 KASSERT(object == NULL,
655 ("vm_map_insert: paradoxical MAP_NOFAULT request"));
657 if (cow & MAP_DISABLE_SYNCER)
658 protoeflags |= MAP_ENTRY_NOSYNC;
659 if (cow & MAP_DISABLE_COREDUMP)
660 protoeflags |= MAP_ENTRY_NOCOREDUMP;
664 * When object is non-NULL, it could be shared with another
665 * process. We have to set or clear OBJ_ONEMAPPING
668 if ((object->ref_count > 1) || (object->shadow_count != 0)) {
669 vm_object_clear_flag(object, OBJ_ONEMAPPING);
672 else if ((prev_entry != &map->header) &&
673 (prev_entry->eflags == protoeflags) &&
674 (prev_entry->end == start) &&
675 (prev_entry->wired_count == 0) &&
676 ((prev_entry->object.vm_object == NULL) ||
677 vm_object_coalesce(prev_entry->object.vm_object,
678 OFF_TO_IDX(prev_entry->offset),
679 (vm_size_t)(prev_entry->end - prev_entry->start),
680 (vm_size_t)(end - prev_entry->end)))) {
682 * We were able to extend the object. Determine if we
683 * can extend the previous map entry to include the
686 if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
687 (prev_entry->protection == prot) &&
688 (prev_entry->max_protection == max)) {
689 map->size += (end - prev_entry->end);
690 prev_entry->end = end;
691 vm_map_simplify_entry(map, prev_entry, countp);
692 return (KERN_SUCCESS);
696 * If we can extend the object but cannot extend the
697 * map entry, we have to create a new map entry. We
698 * must bump the ref count on the extended object to
699 * account for it. object may be NULL.
701 object = prev_entry->object.vm_object;
702 offset = prev_entry->offset +
703 (prev_entry->end - prev_entry->start);
704 vm_object_reference(object);
708 * NOTE: if conditionals fail, object can be NULL here. This occurs
709 * in things like the buffer map where we manage kva but do not manage
717 new_entry = vm_map_entry_create(map, countp);
718 new_entry->start = start;
719 new_entry->end = end;
721 new_entry->eflags = protoeflags;
722 new_entry->object.vm_object = object;
723 new_entry->offset = offset;
724 new_entry->avail_ssize = 0;
726 new_entry->inheritance = VM_INHERIT_DEFAULT;
727 new_entry->protection = prot;
728 new_entry->max_protection = max;
729 new_entry->wired_count = 0;
732 * Insert the new entry into the list
735 vm_map_entry_link(map, prev_entry, new_entry);
736 map->size += new_entry->end - new_entry->start;
739 * Update the free space hint
741 if ((map->first_free == prev_entry) &&
742 (prev_entry->end >= new_entry->start)) {
743 map->first_free = new_entry;
748 * Temporarily removed to avoid MAP_STACK panic, due to
749 * MAP_STACK being a huge hack. Will be added back in
750 * when MAP_STACK (and the user stack mapping) is fixed.
753 * It may be possible to simplify the entry
755 vm_map_simplify_entry(map, new_entry, countp);
758 if (cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) {
759 pmap_object_init_pt(map->pmap, start,
760 object, OFF_TO_IDX(offset), end - start,
761 cow & MAP_PREFAULT_PARTIAL);
764 return (KERN_SUCCESS);
768 * Find sufficient space for `length' bytes in the given map, starting at
769 * `start'. The map must be locked. Returns 0 on success, 1 on no space.
771 * This function will returned an arbitrarily aligned pointer. If no
772 * particular alignment is required you should pass align as 1. Note that
773 * the map may return PAGE_SIZE aligned pointers if all the lengths used in
774 * the map are a multiple of PAGE_SIZE, even if you pass a smaller align
777 * 'align' should be a power of 2 but is not required to be.
787 vm_map_entry_t entry, next;
789 vm_offset_t align_mask;
791 if (start < map->min_offset)
792 start = map->min_offset;
793 if (start > map->max_offset)
797 * If the alignment is not a power of 2 we will have to use
798 * a mod/division, set align_mask to a special value.
800 if ((align | (align - 1)) + 1 != (align << 1))
801 align_mask = (vm_offset_t)-1;
803 align_mask = align - 1;
807 * Look for the first possible address; if there's already something
808 * at this address, we have to start after it.
810 if (start == map->min_offset) {
811 if ((entry = map->first_free) != &map->header)
816 if (vm_map_lookup_entry(map, start, &tmp))
822 * Look through the rest of the map, trying to fit a new region in the
823 * gap between existing regions, or after the very last region.
825 for (;; start = (entry = next)->end) {
827 * Adjust the proposed start by the requested alignment,
828 * be sure that we didn't wrap the address.
830 if (align_mask == (vm_offset_t)-1)
831 end = ((start + align - 1) / align) * align;
833 end = (start + align_mask) & ~align_mask;
838 * Find the end of the proposed new region. Be sure we didn't
839 * go beyond the end of the map, or wrap around the address.
840 * Then check to see if this is the last entry or if the
841 * proposed end fits in the gap between this and the next
844 end = start + length;
845 if (end > map->max_offset || end < start)
848 if (next == &map->header || next->start >= end)
851 SAVE_HINT(map, entry);
852 if (map == kernel_map) {
854 if ((ksize = round_page(start + length)) > kernel_vm_end) {
855 pmap_growkernel(ksize);
864 * vm_map_find finds an unallocated region in the target address
865 * map with the given length. The search is defined to be
866 * first-fit from the specified address; the region found is
867 * returned in the same parameter.
869 * If object is non-NULL, ref count must be bumped by caller
870 * prior to making call to account for the new entry.
873 vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
874 vm_offset_t *addr, /* IN/OUT */
875 vm_size_t length, boolean_t find_space, vm_prot_t prot,
876 vm_prot_t max, int cow)
884 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
887 if (vm_map_findspace(map, start, length, 1, addr)) {
889 vm_map_entry_release(count);
890 return (KERN_NO_SPACE);
894 result = vm_map_insert(map, &count, object, offset,
895 start, start + length, prot, max, cow);
897 vm_map_entry_release(count);
903 * vm_map_simplify_entry:
905 * Simplify the given map entry by merging with either neighbor. This
906 * routine also has the ability to merge with both neighbors.
908 * The map must be locked.
910 * This routine guarentees that the passed entry remains valid (though
911 * possibly extended). When merging, this routine may delete one or
912 * both neighbors. No action is taken on entries which have their
913 * in-transition flag set.
916 vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry, int *countp)
918 vm_map_entry_t next, prev;
919 vm_size_t prevsize, esize;
921 if (entry->eflags & (MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP)) {
922 ++mycpu->gd_cnt.v_intrans_coll;
927 if (prev != &map->header) {
928 prevsize = prev->end - prev->start;
929 if ( (prev->end == entry->start) &&
930 (prev->object.vm_object == entry->object.vm_object) &&
931 (!prev->object.vm_object ||
932 (prev->offset + prevsize == entry->offset)) &&
933 (prev->eflags == entry->eflags) &&
934 (prev->protection == entry->protection) &&
935 (prev->max_protection == entry->max_protection) &&
936 (prev->inheritance == entry->inheritance) &&
937 (prev->wired_count == entry->wired_count)) {
938 if (map->first_free == prev)
939 map->first_free = entry;
940 if (map->hint == prev)
942 vm_map_entry_unlink(map, prev);
943 entry->start = prev->start;
944 entry->offset = prev->offset;
945 if (prev->object.vm_object)
946 vm_object_deallocate(prev->object.vm_object);
947 vm_map_entry_dispose(map, prev, countp);
952 if (next != &map->header) {
953 esize = entry->end - entry->start;
954 if ((entry->end == next->start) &&
955 (next->object.vm_object == entry->object.vm_object) &&
956 (!entry->object.vm_object ||
957 (entry->offset + esize == next->offset)) &&
958 (next->eflags == entry->eflags) &&
959 (next->protection == entry->protection) &&
960 (next->max_protection == entry->max_protection) &&
961 (next->inheritance == entry->inheritance) &&
962 (next->wired_count == entry->wired_count)) {
963 if (map->first_free == next)
964 map->first_free = entry;
965 if (map->hint == next)
967 vm_map_entry_unlink(map, next);
968 entry->end = next->end;
969 if (next->object.vm_object)
970 vm_object_deallocate(next->object.vm_object);
971 vm_map_entry_dispose(map, next, countp);
976 * vm_map_clip_start: [ internal use only ]
978 * Asserts that the given entry begins at or after
979 * the specified address; if necessary,
980 * it splits the entry into two.
982 #define vm_map_clip_start(map, entry, startaddr, countp) \
984 if (startaddr > entry->start) \
985 _vm_map_clip_start(map, entry, startaddr, countp); \
989 * This routine is called only when it is known that
990 * the entry must be split.
993 _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start, int *countp)
995 vm_map_entry_t new_entry;
998 * Split off the front portion -- note that we must insert the new
999 * entry BEFORE this one, so that this entry has the specified
1003 vm_map_simplify_entry(map, entry, countp);
1006 * If there is no object backing this entry, we might as well create
1007 * one now. If we defer it, an object can get created after the map
1008 * is clipped, and individual objects will be created for the split-up
1009 * map. This is a bit of a hack, but is also about the best place to
1010 * put this improvement.
1013 if (entry->object.vm_object == NULL && !map->system_map) {
1015 object = vm_object_allocate(OBJT_DEFAULT,
1016 atop(entry->end - entry->start));
1017 entry->object.vm_object = object;
1021 new_entry = vm_map_entry_create(map, countp);
1022 *new_entry = *entry;
1024 new_entry->end = start;
1025 entry->offset += (start - entry->start);
1026 entry->start = start;
1028 vm_map_entry_link(map, entry->prev, new_entry);
1030 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1031 vm_object_reference(new_entry->object.vm_object);
1036 * vm_map_clip_end: [ internal use only ]
1038 * Asserts that the given entry ends at or before
1039 * the specified address; if necessary,
1040 * it splits the entry into two.
1043 #define vm_map_clip_end(map, entry, endaddr, countp) \
1045 if (endaddr < entry->end) \
1046 _vm_map_clip_end(map, entry, endaddr, countp); \
1050 * This routine is called only when it is known that
1051 * the entry must be split.
1054 _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end, int *countp)
1056 vm_map_entry_t new_entry;
1059 * If there is no object backing this entry, we might as well create
1060 * one now. If we defer it, an object can get created after the map
1061 * is clipped, and individual objects will be created for the split-up
1062 * map. This is a bit of a hack, but is also about the best place to
1063 * put this improvement.
1066 if (entry->object.vm_object == NULL && !map->system_map) {
1068 object = vm_object_allocate(OBJT_DEFAULT,
1069 atop(entry->end - entry->start));
1070 entry->object.vm_object = object;
1075 * Create a new entry and insert it AFTER the specified entry
1078 new_entry = vm_map_entry_create(map, countp);
1079 *new_entry = *entry;
1081 new_entry->start = entry->end = end;
1082 new_entry->offset += (end - entry->start);
1084 vm_map_entry_link(map, entry, new_entry);
1086 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1087 vm_object_reference(new_entry->object.vm_object);
1092 * VM_MAP_RANGE_CHECK: [ internal use only ]
1094 * Asserts that the starting and ending region
1095 * addresses fall within the valid range of the map.
1097 #define VM_MAP_RANGE_CHECK(map, start, end) \
1099 if (start < vm_map_min(map)) \
1100 start = vm_map_min(map); \
1101 if (end > vm_map_max(map)) \
1102 end = vm_map_max(map); \
1108 * vm_map_transition_wait: [ kernel use only ]
1110 * Used to block when an in-transition collison occurs. The map
1111 * is unlocked for the sleep and relocked before the return.
1115 vm_map_transition_wait(vm_map_t map)
1118 tsleep(map, 0, "vment", 0);
1126 * When we do blocking operations with the map lock held it is
1127 * possible that a clip might have occured on our in-transit entry,
1128 * requiring an adjustment to the entry in our loop. These macros
1129 * help the pageable and clip_range code deal with the case. The
1130 * conditional costs virtually nothing if no clipping has occured.
1133 #define CLIP_CHECK_BACK(entry, save_start) \
1135 while (entry->start != save_start) { \
1136 entry = entry->prev; \
1137 KASSERT(entry != &map->header, ("bad entry clip")); \
1141 #define CLIP_CHECK_FWD(entry, save_end) \
1143 while (entry->end != save_end) { \
1144 entry = entry->next; \
1145 KASSERT(entry != &map->header, ("bad entry clip")); \
1151 * vm_map_clip_range: [ kernel use only ]
1153 * Clip the specified range and return the base entry. The
1154 * range may cover several entries starting at the returned base
1155 * and the first and last entry in the covering sequence will be
1156 * properly clipped to the requested start and end address.
1158 * If no holes are allowed you should pass the MAP_CLIP_NO_HOLES
1161 * The MAP_ENTRY_IN_TRANSITION flag will be set for the entries
1162 * covered by the requested range.
1164 * The map must be exclusively locked on entry and will remain locked
1165 * on return. If no range exists or the range contains holes and you
1166 * specified that no holes were allowed, NULL will be returned. This
1167 * routine may temporarily unlock the map in order avoid a deadlock when
1172 vm_map_clip_range(vm_map_t map, vm_offset_t start, vm_offset_t end,
1173 int *countp, int flags)
1175 vm_map_entry_t start_entry;
1176 vm_map_entry_t entry;
1179 * Locate the entry and effect initial clipping. The in-transition
1180 * case does not occur very often so do not try to optimize it.
1183 if (vm_map_lookup_entry(map, start, &start_entry) == FALSE)
1185 entry = start_entry;
1186 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1187 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1188 ++mycpu->gd_cnt.v_intrans_coll;
1189 ++mycpu->gd_cnt.v_intrans_wait;
1190 vm_map_transition_wait(map);
1192 * entry and/or start_entry may have been clipped while
1193 * we slept, or may have gone away entirely. We have
1194 * to restart from the lookup.
1199 * Since we hold an exclusive map lock we do not have to restart
1200 * after clipping, even though clipping may block in zalloc.
1202 vm_map_clip_start(map, entry, start, countp);
1203 vm_map_clip_end(map, entry, end, countp);
1204 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
1207 * Scan entries covered by the range. When working on the next
1208 * entry a restart need only re-loop on the current entry which
1209 * we have already locked, since 'next' may have changed. Also,
1210 * even though entry is safe, it may have been clipped so we
1211 * have to iterate forwards through the clip after sleeping.
1213 while (entry->next != &map->header && entry->next->start < end) {
1214 vm_map_entry_t next = entry->next;
1216 if (flags & MAP_CLIP_NO_HOLES) {
1217 if (next->start > entry->end) {
1218 vm_map_unclip_range(map, start_entry,
1219 start, entry->end, countp, flags);
1224 if (next->eflags & MAP_ENTRY_IN_TRANSITION) {
1225 vm_offset_t save_end = entry->end;
1226 next->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1227 ++mycpu->gd_cnt.v_intrans_coll;
1228 ++mycpu->gd_cnt.v_intrans_wait;
1229 vm_map_transition_wait(map);
1232 * clips might have occured while we blocked.
1234 CLIP_CHECK_FWD(entry, save_end);
1235 CLIP_CHECK_BACK(start_entry, start);
1239 * No restart necessary even though clip_end may block, we
1240 * are holding the map lock.
1242 vm_map_clip_end(map, next, end, countp);
1243 next->eflags |= MAP_ENTRY_IN_TRANSITION;
1246 if (flags & MAP_CLIP_NO_HOLES) {
1247 if (entry->end != end) {
1248 vm_map_unclip_range(map, start_entry,
1249 start, entry->end, countp, flags);
1253 return(start_entry);
1257 * vm_map_unclip_range: [ kernel use only ]
1259 * Undo the effect of vm_map_clip_range(). You should pass the same
1260 * flags and the same range that you passed to vm_map_clip_range().
1261 * This code will clear the in-transition flag on the entries and
1262 * wake up anyone waiting. This code will also simplify the sequence
1263 * and attempt to merge it with entries before and after the sequence.
1265 * The map must be locked on entry and will remain locked on return.
1267 * Note that you should also pass the start_entry returned by
1268 * vm_map_clip_range(). However, if you block between the two calls
1269 * with the map unlocked please be aware that the start_entry may
1270 * have been clipped and you may need to scan it backwards to find
1271 * the entry corresponding with the original start address. You are
1272 * responsible for this, vm_map_unclip_range() expects the correct
1273 * start_entry to be passed to it and will KASSERT otherwise.
1277 vm_map_unclip_range(
1279 vm_map_entry_t start_entry,
1285 vm_map_entry_t entry;
1287 entry = start_entry;
1289 KASSERT(entry->start == start, ("unclip_range: illegal base entry"));
1290 while (entry != &map->header && entry->start < end) {
1291 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION, ("in-transition flag not set during unclip on: %p", entry));
1292 KASSERT(entry->end <= end, ("unclip_range: tail wasn't clipped"));
1293 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
1294 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
1295 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
1298 entry = entry->next;
1302 * Simplification does not block so there is no restart case.
1304 entry = start_entry;
1305 while (entry != &map->header && entry->start < end) {
1306 vm_map_simplify_entry(map, entry, countp);
1307 entry = entry->next;
1312 * vm_map_submap: [ kernel use only ]
1314 * Mark the given range as handled by a subordinate map.
1316 * This range must have been created with vm_map_find,
1317 * and no other operations may have been performed on this
1318 * range prior to calling vm_map_submap.
1320 * Only a limited number of operations can be performed
1321 * within this rage after calling vm_map_submap:
1323 * [Don't try vm_map_copy!]
1325 * To remove a submapping, one must first remove the
1326 * range from the superior map, and then destroy the
1327 * submap (if desired). [Better yet, don't try it.]
1330 vm_map_submap(vm_map_t map, vm_offset_t start, vm_offset_t end, vm_map_t submap)
1332 vm_map_entry_t entry;
1333 int result = KERN_INVALID_ARGUMENT;
1336 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1339 VM_MAP_RANGE_CHECK(map, start, end);
1341 if (vm_map_lookup_entry(map, start, &entry)) {
1342 vm_map_clip_start(map, entry, start, &count);
1344 entry = entry->next;
1347 vm_map_clip_end(map, entry, end, &count);
1349 if ((entry->start == start) && (entry->end == end) &&
1350 ((entry->eflags & MAP_ENTRY_COW) == 0) &&
1351 (entry->object.vm_object == NULL)) {
1352 entry->object.sub_map = submap;
1353 entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
1354 result = KERN_SUCCESS;
1357 vm_map_entry_release(count);
1365 * Sets the protection of the specified address
1366 * region in the target map. If "set_max" is
1367 * specified, the maximum protection is to be set;
1368 * otherwise, only the current protection is affected.
1371 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
1372 vm_prot_t new_prot, boolean_t set_max)
1374 vm_map_entry_t current;
1375 vm_map_entry_t entry;
1378 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1381 VM_MAP_RANGE_CHECK(map, start, end);
1383 if (vm_map_lookup_entry(map, start, &entry)) {
1384 vm_map_clip_start(map, entry, start, &count);
1386 entry = entry->next;
1390 * Make a first pass to check for protection violations.
1394 while ((current != &map->header) && (current->start < end)) {
1395 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1397 vm_map_entry_release(count);
1398 return (KERN_INVALID_ARGUMENT);
1400 if ((new_prot & current->max_protection) != new_prot) {
1402 vm_map_entry_release(count);
1403 return (KERN_PROTECTION_FAILURE);
1405 current = current->next;
1409 * Go back and fix up protections. [Note that clipping is not
1410 * necessary the second time.]
1414 while ((current != &map->header) && (current->start < end)) {
1417 vm_map_clip_end(map, current, end, &count);
1419 old_prot = current->protection;
1421 current->protection =
1422 (current->max_protection = new_prot) &
1425 current->protection = new_prot;
1428 * Update physical map if necessary. Worry about copy-on-write
1429 * here -- CHECK THIS XXX
1432 if (current->protection != old_prot) {
1433 #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1436 pmap_protect(map->pmap, current->start,
1438 current->protection & MASK(current));
1442 vm_map_simplify_entry(map, current, &count);
1444 current = current->next;
1448 vm_map_entry_release(count);
1449 return (KERN_SUCCESS);
1455 * This routine traverses a processes map handling the madvise
1456 * system call. Advisories are classified as either those effecting
1457 * the vm_map_entry structure, or those effecting the underlying
1462 vm_map_madvise(vm_map_t map, vm_offset_t start, vm_offset_t end, int behav)
1464 vm_map_entry_t current, entry;
1469 * Some madvise calls directly modify the vm_map_entry, in which case
1470 * we need to use an exclusive lock on the map and we need to perform
1471 * various clipping operations. Otherwise we only need a read-lock
1475 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1479 case MADV_SEQUENTIAL:
1491 vm_map_lock_read(map);
1494 vm_map_entry_release(count);
1495 return (KERN_INVALID_ARGUMENT);
1499 * Locate starting entry and clip if necessary.
1502 VM_MAP_RANGE_CHECK(map, start, end);
1504 if (vm_map_lookup_entry(map, start, &entry)) {
1506 vm_map_clip_start(map, entry, start, &count);
1508 entry = entry->next;
1513 * madvise behaviors that are implemented in the vm_map_entry.
1515 * We clip the vm_map_entry so that behavioral changes are
1516 * limited to the specified address range.
1518 for (current = entry;
1519 (current != &map->header) && (current->start < end);
1520 current = current->next
1522 if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1525 vm_map_clip_end(map, current, end, &count);
1529 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
1531 case MADV_SEQUENTIAL:
1532 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
1535 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
1538 current->eflags |= MAP_ENTRY_NOSYNC;
1541 current->eflags &= ~MAP_ENTRY_NOSYNC;
1544 current->eflags |= MAP_ENTRY_NOCOREDUMP;
1547 current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
1552 vm_map_simplify_entry(map, current, &count);
1560 * madvise behaviors that are implemented in the underlying
1563 * Since we don't clip the vm_map_entry, we have to clip
1564 * the vm_object pindex and count.
1566 for (current = entry;
1567 (current != &map->header) && (current->start < end);
1568 current = current->next
1570 vm_offset_t useStart;
1572 if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1575 pindex = OFF_TO_IDX(current->offset);
1576 count = atop(current->end - current->start);
1577 useStart = current->start;
1579 if (current->start < start) {
1580 pindex += atop(start - current->start);
1581 count -= atop(start - current->start);
1584 if (current->end > end)
1585 count -= atop(current->end - end);
1590 vm_object_madvise(current->object.vm_object,
1591 pindex, count, behav);
1592 if (behav == MADV_WILLNEED) {
1593 pmap_object_init_pt(
1596 current->object.vm_object,
1598 (count << PAGE_SHIFT),
1599 MAP_PREFAULT_MADVISE
1603 vm_map_unlock_read(map);
1605 vm_map_entry_release(count);
1613 * Sets the inheritance of the specified address
1614 * range in the target map. Inheritance
1615 * affects how the map will be shared with
1616 * child maps at the time of vm_map_fork.
1619 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
1620 vm_inherit_t new_inheritance)
1622 vm_map_entry_t entry;
1623 vm_map_entry_t temp_entry;
1626 switch (new_inheritance) {
1627 case VM_INHERIT_NONE:
1628 case VM_INHERIT_COPY:
1629 case VM_INHERIT_SHARE:
1632 return (KERN_INVALID_ARGUMENT);
1635 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1638 VM_MAP_RANGE_CHECK(map, start, end);
1640 if (vm_map_lookup_entry(map, start, &temp_entry)) {
1642 vm_map_clip_start(map, entry, start, &count);
1644 entry = temp_entry->next;
1646 while ((entry != &map->header) && (entry->start < end)) {
1647 vm_map_clip_end(map, entry, end, &count);
1649 entry->inheritance = new_inheritance;
1651 vm_map_simplify_entry(map, entry, &count);
1653 entry = entry->next;
1656 vm_map_entry_release(count);
1657 return (KERN_SUCCESS);
1661 * Implement the semantics of mlock
1664 vm_map_unwire(map, start, real_end, new_pageable)
1667 vm_offset_t real_end;
1668 boolean_t new_pageable;
1670 vm_map_entry_t entry;
1671 vm_map_entry_t start_entry;
1673 int rv = KERN_SUCCESS;
1676 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1678 VM_MAP_RANGE_CHECK(map, start, real_end);
1681 start_entry = vm_map_clip_range(map, start, end, &count, MAP_CLIP_NO_HOLES);
1682 if (start_entry == NULL) {
1684 vm_map_entry_release(count);
1685 return (KERN_INVALID_ADDRESS);
1688 if (new_pageable == 0) {
1689 entry = start_entry;
1690 while ((entry != &map->header) && (entry->start < end)) {
1691 vm_offset_t save_start;
1692 vm_offset_t save_end;
1695 * Already user wired or hard wired (trivial cases)
1697 if (entry->eflags & MAP_ENTRY_USER_WIRED) {
1698 entry = entry->next;
1701 if (entry->wired_count != 0) {
1702 entry->wired_count++;
1703 entry->eflags |= MAP_ENTRY_USER_WIRED;
1704 entry = entry->next;
1709 * A new wiring requires instantiation of appropriate
1710 * management structures and the faulting in of the
1713 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1714 int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY;
1715 if (copyflag && ((entry->protection & VM_PROT_WRITE) != 0)) {
1717 vm_object_shadow(&entry->object.vm_object,
1719 atop(entry->end - entry->start));
1720 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
1722 } else if (entry->object.vm_object == NULL &&
1725 entry->object.vm_object =
1726 vm_object_allocate(OBJT_DEFAULT,
1727 atop(entry->end - entry->start));
1728 entry->offset = (vm_offset_t) 0;
1732 entry->wired_count++;
1733 entry->eflags |= MAP_ENTRY_USER_WIRED;
1736 * Now fault in the area. The map lock needs to be
1737 * manipulated to avoid deadlocks. The in-transition
1738 * flag protects the entries.
1740 save_start = entry->start;
1741 save_end = entry->end;
1744 rv = vm_fault_user_wire(map, save_start, save_end);
1747 CLIP_CHECK_BACK(entry, save_start);
1749 KASSERT(entry->wired_count == 1, ("bad wired_count on entry"));
1750 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
1751 entry->wired_count = 0;
1752 if (entry->end == save_end)
1754 entry = entry->next;
1755 KASSERT(entry != &map->header, ("bad entry clip during backout"));
1757 end = save_start; /* unwire the rest */
1761 * note that even though the entry might have been
1762 * clipped, the USER_WIRED flag we set prevents
1763 * duplication so we do not have to do a
1766 entry = entry->next;
1770 * If we failed fall through to the unwiring section to
1771 * unwire what we had wired so far. 'end' has already
1778 * start_entry might have been clipped if we unlocked the
1779 * map and blocked. No matter how clipped it has gotten
1780 * there should be a fragment that is on our start boundary.
1782 CLIP_CHECK_BACK(start_entry, start);
1786 * Deal with the unwiring case.
1790 * This is the unwiring case. We must first ensure that the
1791 * range to be unwired is really wired down. We know there
1794 entry = start_entry;
1795 while ((entry != &map->header) && (entry->start < end)) {
1796 if ((entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
1797 rv = KERN_INVALID_ARGUMENT;
1800 KASSERT(entry->wired_count != 0, ("wired count was 0 with USER_WIRED set! %p", entry));
1801 entry = entry->next;
1805 * Now decrement the wiring count for each region. If a region
1806 * becomes completely unwired, unwire its physical pages and
1809 while ((entry != &map->header) && (entry->start < end)) {
1810 KASSERT(entry->eflags & MAP_ENTRY_USER_WIRED, ("expected USER_WIRED on entry %p", entry));
1811 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
1812 entry->wired_count--;
1813 if (entry->wired_count == 0)
1814 vm_fault_unwire(map, entry->start, entry->end);
1815 entry = entry->next;
1819 vm_map_unclip_range(map, start_entry, start, real_end, &count,
1823 vm_map_entry_release(count);
1830 * Sets the pageability of the specified address
1831 * range in the target map. Regions specified
1832 * as not pageable require locked-down physical
1833 * memory and physical page maps.
1835 * The map must not be locked, but a reference
1836 * must remain to the map throughout the call.
1838 * This function may be called via the zalloc path and must properly
1839 * reserve map entries for kernel_map.
1842 vm_map_wire(vm_map_t map, vm_offset_t start,
1843 vm_offset_t real_end, boolean_t new_pageable)
1845 vm_map_entry_t entry;
1846 vm_map_entry_t start_entry;
1848 int rv = KERN_SUCCESS;
1852 if (map == kernel_map)
1853 count = vm_map_entry_kreserve(MAP_RESERVE_COUNT);
1855 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1857 VM_MAP_RANGE_CHECK(map, start, real_end);
1860 start_entry = vm_map_clip_range(map, start, end, &count, MAP_CLIP_NO_HOLES);
1861 if (start_entry == NULL) {
1863 rv = KERN_INVALID_ADDRESS;
1866 if (new_pageable == 0) {
1870 * 1. Holding the write lock, we create any shadow or zero-fill
1871 * objects that need to be created. Then we clip each map
1872 * entry to the region to be wired and increment its wiring
1873 * count. We create objects before clipping the map entries
1874 * to avoid object proliferation.
1876 * 2. We downgrade to a read lock, and call vm_fault_wire to
1877 * fault in the pages for any newly wired area (wired_count is
1880 * Downgrading to a read lock for vm_fault_wire avoids a
1881 * possible deadlock with another process that may have faulted
1882 * on one of the pages to be wired (it would mark the page busy,
1883 * blocking us, then in turn block on the map lock that we
1884 * hold). Because of problems in the recursive lock package,
1885 * we cannot upgrade to a write lock in vm_map_lookup. Thus,
1886 * any actions that require the write lock must be done
1887 * beforehand. Because we keep the read lock on the map, the
1888 * copy-on-write status of the entries we modify here cannot
1892 entry = start_entry;
1893 while ((entry != &map->header) && (entry->start < end)) {
1895 * Trivial case if the entry is already wired
1897 if (entry->wired_count) {
1898 entry->wired_count++;
1899 entry = entry->next;
1904 * The entry is being newly wired, we have to setup
1905 * appropriate management structures. A shadow
1906 * object is required for a copy-on-write region,
1907 * or a normal object for a zero-fill region. We
1908 * do not have to do this for entries that point to sub
1909 * maps because we won't hold the lock on the sub map.
1911 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1912 int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY;
1914 ((entry->protection & VM_PROT_WRITE) != 0)) {
1916 vm_object_shadow(&entry->object.vm_object,
1918 atop(entry->end - entry->start));
1919 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
1920 } else if (entry->object.vm_object == NULL &&
1922 entry->object.vm_object =
1923 vm_object_allocate(OBJT_DEFAULT,
1924 atop(entry->end - entry->start));
1925 entry->offset = (vm_offset_t) 0;
1929 entry->wired_count++;
1930 entry = entry->next;
1938 * HACK HACK HACK HACK
1940 * Unlock the map to avoid deadlocks. The in-transit flag
1941 * protects us from most changes but note that
1942 * clipping may still occur. To prevent clipping from
1943 * occuring after the unlock, except for when we are
1944 * blocking in vm_fault_wire, we must run at splvm().
1945 * Otherwise our accesses to entry->start and entry->end
1946 * could be corrupted. We have to set splvm() prior to
1947 * unlocking so start_entry does not change out from
1948 * under us at the very beginning of the loop.
1950 * HACK HACK HACK HACK
1956 entry = start_entry;
1957 while (entry != &map->header && entry->start < end) {
1959 * If vm_fault_wire fails for any page we need to undo
1960 * what has been done. We decrement the wiring count
1961 * for those pages which have not yet been wired (now)
1962 * and unwire those that have (later).
1964 vm_offset_t save_start = entry->start;
1965 vm_offset_t save_end = entry->end;
1967 if (entry->wired_count == 1)
1968 rv = vm_fault_wire(map, entry->start, entry->end);
1970 CLIP_CHECK_BACK(entry, save_start);
1972 KASSERT(entry->wired_count == 1, ("wired_count changed unexpectedly"));
1973 entry->wired_count = 0;
1974 if (entry->end == save_end)
1976 entry = entry->next;
1977 KASSERT(entry != &map->header, ("bad entry clip during backout"));
1982 CLIP_CHECK_FWD(entry, save_end);
1983 entry = entry->next;
1988 * relock. start_entry is still IN_TRANSITION and must
1989 * still exist, but may have been clipped (handled just
1995 * If a failure occured undo everything by falling through
1996 * to the unwiring code. 'end' has already been adjusted
2003 * start_entry might have been clipped if we unlocked the
2004 * map and blocked. No matter how clipped it has gotten
2005 * there should be a fragment that is on our start boundary.
2007 CLIP_CHECK_BACK(start_entry, start);
2012 * This is the unwiring case. We must first ensure that the
2013 * range to be unwired is really wired down. We know there
2016 entry = start_entry;
2017 while ((entry != &map->header) && (entry->start < end)) {
2018 if (entry->wired_count == 0) {
2019 rv = KERN_INVALID_ARGUMENT;
2022 entry = entry->next;
2026 * Now decrement the wiring count for each region. If a region
2027 * becomes completely unwired, unwire its physical pages and
2030 entry = start_entry;
2031 while ((entry != &map->header) && (entry->start < end)) {
2032 entry->wired_count--;
2033 if (entry->wired_count == 0)
2034 vm_fault_unwire(map, entry->start, entry->end);
2035 entry = entry->next;
2039 vm_map_unclip_range(map, start_entry, start, real_end, &count,
2044 if (map == kernel_map)
2045 vm_map_entry_krelease(count);
2047 vm_map_entry_release(count);
2052 * vm_map_set_wired_quick()
2054 * Mark a newly allocated address range as wired but do not fault in
2055 * the pages. The caller is expected to load the pages into the object.
2057 * The map must be locked on entry and will remain locked on return.
2060 vm_map_set_wired_quick(vm_map_t map, vm_offset_t addr, vm_size_t size, int *countp)
2062 vm_map_entry_t scan;
2063 vm_map_entry_t entry;
2065 entry = vm_map_clip_range(map, addr, addr + size, countp, MAP_CLIP_NO_HOLES);
2066 for (scan = entry; scan != &map->header && scan->start < addr + size; scan = scan->next) {
2067 KKASSERT(entry->wired_count == 0);
2068 entry->wired_count = 1;
2070 vm_map_unclip_range(map, entry, addr, addr + size, countp, MAP_CLIP_NO_HOLES);
2076 * Push any dirty cached pages in the address range to their pager.
2077 * If syncio is TRUE, dirty pages are written synchronously.
2078 * If invalidate is TRUE, any cached pages are freed as well.
2080 * Returns an error if any part of the specified range is not mapped.
2083 vm_map_clean(map, start, end, syncio, invalidate)
2088 boolean_t invalidate;
2090 vm_map_entry_t current;
2091 vm_map_entry_t entry;
2094 vm_ooffset_t offset;
2096 vm_map_lock_read(map);
2097 VM_MAP_RANGE_CHECK(map, start, end);
2098 if (!vm_map_lookup_entry(map, start, &entry)) {
2099 vm_map_unlock_read(map);
2100 return (KERN_INVALID_ADDRESS);
2103 * Make a first pass to check for holes.
2105 for (current = entry; current->start < end; current = current->next) {
2106 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
2107 vm_map_unlock_read(map);
2108 return (KERN_INVALID_ARGUMENT);
2110 if (end > current->end &&
2111 (current->next == &map->header ||
2112 current->end != current->next->start)) {
2113 vm_map_unlock_read(map);
2114 return (KERN_INVALID_ADDRESS);
2119 pmap_remove(vm_map_pmap(map), start, end);
2121 * Make a second pass, cleaning/uncaching pages from the indicated
2124 for (current = entry; current->start < end; current = current->next) {
2125 offset = current->offset + (start - current->start);
2126 size = (end <= current->end ? end : current->end) - start;
2127 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
2129 vm_map_entry_t tentry;
2132 smap = current->object.sub_map;
2133 vm_map_lock_read(smap);
2134 (void) vm_map_lookup_entry(smap, offset, &tentry);
2135 tsize = tentry->end - offset;
2138 object = tentry->object.vm_object;
2139 offset = tentry->offset + (offset - tentry->start);
2140 vm_map_unlock_read(smap);
2142 object = current->object.vm_object;
2145 * Note that there is absolutely no sense in writing out
2146 * anonymous objects, so we track down the vnode object
2148 * We invalidate (remove) all pages from the address space
2149 * anyway, for semantic correctness.
2151 * note: certain anonymous maps, such as MAP_NOSYNC maps,
2152 * may start out with a NULL object.
2154 while (object && object->backing_object) {
2155 object = object->backing_object;
2156 offset += object->backing_object_offset;
2157 if (object->size < OFF_TO_IDX( offset + size))
2158 size = IDX_TO_OFF(object->size) - offset;
2160 if (object && (object->type == OBJT_VNODE) &&
2161 (current->protection & VM_PROT_WRITE)) {
2163 * Flush pages if writing is allowed, invalidate them
2164 * if invalidation requested. Pages undergoing I/O
2165 * will be ignored by vm_object_page_remove().
2167 * We cannot lock the vnode and then wait for paging
2168 * to complete without deadlocking against vm_fault.
2169 * Instead we simply call vm_object_page_remove() and
2170 * allow it to block internally on a page-by-page
2171 * basis when it encounters pages undergoing async
2176 vm_object_reference(object);
2177 vn_lock(object->handle, LK_EXCLUSIVE | LK_RETRY, curthread);
2178 flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
2179 flags |= invalidate ? OBJPC_INVAL : 0;
2180 vm_object_page_clean(object,
2182 OFF_TO_IDX(offset + size + PAGE_MASK),
2184 VOP_UNLOCK(object->handle, 0, curthread);
2185 vm_object_deallocate(object);
2187 if (object && invalidate &&
2188 ((object->type == OBJT_VNODE) ||
2189 (object->type == OBJT_DEVICE))) {
2190 vm_object_reference(object);
2191 vm_object_page_remove(object,
2193 OFF_TO_IDX(offset + size + PAGE_MASK),
2195 vm_object_deallocate(object);
2200 vm_map_unlock_read(map);
2201 return (KERN_SUCCESS);
2205 * vm_map_entry_unwire: [ internal use only ]
2207 * Make the region specified by this entry pageable.
2209 * The map in question should be locked.
2210 * [This is the reason for this routine's existence.]
2213 vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
2215 vm_fault_unwire(map, entry->start, entry->end);
2216 entry->wired_count = 0;
2220 * vm_map_entry_delete: [ internal use only ]
2222 * Deallocate the given entry from the target map.
2225 vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry, int *countp)
2227 vm_map_entry_unlink(map, entry);
2228 map->size -= entry->end - entry->start;
2230 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
2231 vm_object_deallocate(entry->object.vm_object);
2234 vm_map_entry_dispose(map, entry, countp);
2238 * vm_map_delete: [ internal use only ]
2240 * Deallocates the given address range from the target
2244 vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end, int *countp)
2247 vm_map_entry_t entry;
2248 vm_map_entry_t first_entry;
2251 * Find the start of the region, and clip it
2255 if (!vm_map_lookup_entry(map, start, &first_entry))
2256 entry = first_entry->next;
2258 entry = first_entry;
2259 vm_map_clip_start(map, entry, start, countp);
2261 * Fix the lookup hint now, rather than each time though the
2264 SAVE_HINT(map, entry->prev);
2268 * Save the free space hint
2271 if (entry == &map->header) {
2272 map->first_free = &map->header;
2273 } else if (map->first_free->start >= start) {
2274 map->first_free = entry->prev;
2278 * Step through all entries in this region
2281 while ((entry != &map->header) && (entry->start < end)) {
2282 vm_map_entry_t next;
2284 vm_pindex_t offidxstart, offidxend, count;
2287 * If we hit an in-transition entry we have to sleep and
2288 * retry. It's easier (and not really slower) to just retry
2289 * since this case occurs so rarely and the hint is already
2290 * pointing at the right place. We have to reset the
2291 * start offset so as not to accidently delete an entry
2292 * another process just created in vacated space.
2294 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
2295 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2296 start = entry->start;
2297 ++mycpu->gd_cnt.v_intrans_coll;
2298 ++mycpu->gd_cnt.v_intrans_wait;
2299 vm_map_transition_wait(map);
2302 vm_map_clip_end(map, entry, end, countp);
2308 offidxstart = OFF_TO_IDX(entry->offset);
2309 count = OFF_TO_IDX(e - s);
2310 object = entry->object.vm_object;
2313 * Unwire before removing addresses from the pmap; otherwise,
2314 * unwiring will put the entries back in the pmap.
2316 if (entry->wired_count != 0) {
2317 vm_map_entry_unwire(map, entry);
2320 offidxend = offidxstart + count;
2322 if ((object == kernel_object) || (object == kmem_object)) {
2323 vm_object_page_remove(object, offidxstart, offidxend, FALSE);
2325 pmap_remove(map->pmap, s, e);
2326 if (object != NULL &&
2327 object->ref_count != 1 &&
2328 (object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING &&
2329 (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2330 vm_object_collapse(object);
2331 vm_object_page_remove(object, offidxstart, offidxend, FALSE);
2332 if (object->type == OBJT_SWAP) {
2333 swap_pager_freespace(object, offidxstart, count);
2335 if (offidxend >= object->size &&
2336 offidxstart < object->size) {
2337 object->size = offidxstart;
2343 * Delete the entry (which may delete the object) only after
2344 * removing all pmap entries pointing to its pages.
2345 * (Otherwise, its page frames may be reallocated, and any
2346 * modify bits will be set in the wrong object!)
2348 vm_map_entry_delete(map, entry, countp);
2351 return (KERN_SUCCESS);
2357 * Remove the given address range from the target map.
2358 * This is the exported form of vm_map_delete.
2361 vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
2366 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
2368 VM_MAP_RANGE_CHECK(map, start, end);
2369 result = vm_map_delete(map, start, end, &count);
2371 vm_map_entry_release(count);
2377 * vm_map_check_protection:
2379 * Assert that the target map allows the specified
2380 * privilege on the entire address region given.
2381 * The entire region must be allocated.
2384 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
2385 vm_prot_t protection)
2387 vm_map_entry_t entry;
2388 vm_map_entry_t tmp_entry;
2390 if (!vm_map_lookup_entry(map, start, &tmp_entry)) {
2395 while (start < end) {
2396 if (entry == &map->header) {
2403 if (start < entry->start) {
2407 * Check protection associated with entry.
2410 if ((entry->protection & protection) != protection) {
2413 /* go to next entry */
2416 entry = entry->next;
2422 * Split the pages in a map entry into a new object. This affords
2423 * easier removal of unused pages, and keeps object inheritance from
2424 * being a negative impact on memory usage.
2427 vm_map_split(vm_map_entry_t entry)
2430 vm_object_t orig_object, new_object, source;
2432 vm_pindex_t offidxstart, offidxend, idx;
2434 vm_ooffset_t offset;
2436 orig_object = entry->object.vm_object;
2437 if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
2439 if (orig_object->ref_count <= 1)
2442 offset = entry->offset;
2446 offidxstart = OFF_TO_IDX(offset);
2447 offidxend = offidxstart + OFF_TO_IDX(e - s);
2448 size = offidxend - offidxstart;
2450 new_object = vm_pager_allocate(orig_object->type,
2451 NULL, IDX_TO_OFF(size), VM_PROT_ALL, 0LL);
2452 if (new_object == NULL)
2455 source = orig_object->backing_object;
2456 if (source != NULL) {
2457 vm_object_reference(source); /* Referenced by new_object */
2458 LIST_INSERT_HEAD(&source->shadow_head,
2459 new_object, shadow_list);
2460 vm_object_clear_flag(source, OBJ_ONEMAPPING);
2461 new_object->backing_object_offset =
2462 orig_object->backing_object_offset + IDX_TO_OFF(offidxstart);
2463 new_object->backing_object = source;
2464 source->shadow_count++;
2465 source->generation++;
2468 for (idx = 0; idx < size; idx++) {
2472 m = vm_page_lookup(orig_object, offidxstart + idx);
2477 * We must wait for pending I/O to complete before we can
2480 * We do not have to VM_PROT_NONE the page as mappings should
2481 * not be changed by this operation.
2483 if (vm_page_sleep_busy(m, TRUE, "spltwt"))
2487 vm_page_rename(m, new_object, idx);
2488 /* page automatically made dirty by rename and cache handled */
2492 if (orig_object->type == OBJT_SWAP) {
2493 vm_object_pip_add(orig_object, 1);
2495 * copy orig_object pages into new_object
2496 * and destroy unneeded pages in
2499 swap_pager_copy(orig_object, new_object, offidxstart, 0);
2500 vm_object_pip_wakeup(orig_object);
2503 for (idx = 0; idx < size; idx++) {
2504 m = vm_page_lookup(new_object, idx);
2510 entry->object.vm_object = new_object;
2511 entry->offset = 0LL;
2512 vm_object_deallocate(orig_object);
2516 * vm_map_copy_entry:
2518 * Copies the contents of the source entry to the destination
2519 * entry. The entries *must* be aligned properly.
2522 vm_map_copy_entry(vm_map_t src_map, vm_map_t dst_map,
2523 vm_map_entry_t src_entry, vm_map_entry_t dst_entry)
2525 vm_object_t src_object;
2527 if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
2530 if (src_entry->wired_count == 0) {
2533 * If the source entry is marked needs_copy, it is already
2536 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
2537 pmap_protect(src_map->pmap,
2540 src_entry->protection & ~VM_PROT_WRITE);
2544 * Make a copy of the object.
2546 if ((src_object = src_entry->object.vm_object) != NULL) {
2548 if ((src_object->handle == NULL) &&
2549 (src_object->type == OBJT_DEFAULT ||
2550 src_object->type == OBJT_SWAP)) {
2551 vm_object_collapse(src_object);
2552 if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
2553 vm_map_split(src_entry);
2554 src_object = src_entry->object.vm_object;
2558 vm_object_reference(src_object);
2559 vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
2560 dst_entry->object.vm_object = src_object;
2561 src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2562 dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2563 dst_entry->offset = src_entry->offset;
2565 dst_entry->object.vm_object = NULL;
2566 dst_entry->offset = 0;
2569 pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
2570 dst_entry->end - dst_entry->start, src_entry->start);
2573 * Of course, wired down pages can't be set copy-on-write.
2574 * Cause wired pages to be copied into the new map by
2575 * simulating faults (the new pages are pageable)
2577 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry);
2583 * Create a new process vmspace structure and vm_map
2584 * based on those of an existing process. The new map
2585 * is based on the old map, according to the inheritance
2586 * values on the regions in that map.
2588 * The source map must not be locked.
2591 vmspace_fork(struct vmspace *vm1)
2593 struct vmspace *vm2;
2594 vm_map_t old_map = &vm1->vm_map;
2596 vm_map_entry_t old_entry;
2597 vm_map_entry_t new_entry;
2601 vm_map_lock(old_map);
2602 old_map->infork = 1;
2604 vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
2605 bcopy(&vm1->vm_startcopy, &vm2->vm_startcopy,
2606 (caddr_t) (vm1 + 1) - (caddr_t) &vm1->vm_startcopy);
2607 new_map = &vm2->vm_map; /* XXX */
2608 new_map->timestamp = 1;
2611 old_entry = old_map->header.next;
2612 while (old_entry != &old_map->header) {
2614 old_entry = old_entry->next;
2617 count = vm_map_entry_reserve(count + MAP_RESERVE_COUNT);
2619 old_entry = old_map->header.next;
2620 while (old_entry != &old_map->header) {
2621 if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2622 panic("vm_map_fork: encountered a submap");
2624 switch (old_entry->inheritance) {
2625 case VM_INHERIT_NONE:
2628 case VM_INHERIT_SHARE:
2630 * Clone the entry, creating the shared object if necessary.
2632 object = old_entry->object.vm_object;
2633 if (object == NULL) {
2634 object = vm_object_allocate(OBJT_DEFAULT,
2635 atop(old_entry->end - old_entry->start));
2636 old_entry->object.vm_object = object;
2637 old_entry->offset = (vm_offset_t) 0;
2641 * Add the reference before calling vm_object_shadow
2642 * to insure that a shadow object is created.
2644 vm_object_reference(object);
2645 if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
2646 vm_object_shadow(&old_entry->object.vm_object,
2648 atop(old_entry->end - old_entry->start));
2649 old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
2650 /* Transfer the second reference too. */
2651 vm_object_reference(
2652 old_entry->object.vm_object);
2653 vm_object_deallocate(object);
2654 object = old_entry->object.vm_object;
2656 vm_object_clear_flag(object, OBJ_ONEMAPPING);
2659 * Clone the entry, referencing the shared object.
2661 new_entry = vm_map_entry_create(new_map, &count);
2662 *new_entry = *old_entry;
2663 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2664 new_entry->wired_count = 0;
2667 * Insert the entry into the new map -- we know we're
2668 * inserting at the end of the new map.
2671 vm_map_entry_link(new_map, new_map->header.prev,
2675 * Update the physical map
2678 pmap_copy(new_map->pmap, old_map->pmap,
2680 (old_entry->end - old_entry->start),
2684 case VM_INHERIT_COPY:
2686 * Clone the entry and link into the map.
2688 new_entry = vm_map_entry_create(new_map, &count);
2689 *new_entry = *old_entry;
2690 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2691 new_entry->wired_count = 0;
2692 new_entry->object.vm_object = NULL;
2693 vm_map_entry_link(new_map, new_map->header.prev,
2695 vm_map_copy_entry(old_map, new_map, old_entry,
2699 old_entry = old_entry->next;
2702 new_map->size = old_map->size;
2703 old_map->infork = 0;
2704 vm_map_unlock(old_map);
2705 vm_map_entry_release(count);
2711 vm_map_stack (vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
2712 vm_prot_t prot, vm_prot_t max, int cow)
2714 vm_map_entry_t prev_entry;
2715 vm_map_entry_t new_stack_entry;
2716 vm_size_t init_ssize;
2720 if (VM_MIN_ADDRESS > 0 && addrbos < VM_MIN_ADDRESS)
2721 return (KERN_NO_SPACE);
2723 if (max_ssize < sgrowsiz)
2724 init_ssize = max_ssize;
2726 init_ssize = sgrowsiz;
2728 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
2731 /* If addr is already mapped, no go */
2732 if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
2734 vm_map_entry_release(count);
2735 return (KERN_NO_SPACE);
2738 /* If we would blow our VMEM resource limit, no go */
2739 if (map->size + init_ssize >
2740 curproc->p_rlimit[RLIMIT_VMEM].rlim_cur) {
2742 vm_map_entry_release(count);
2743 return (KERN_NO_SPACE);
2746 /* If we can't accomodate max_ssize in the current mapping,
2747 * no go. However, we need to be aware that subsequent user
2748 * mappings might map into the space we have reserved for
2749 * stack, and currently this space is not protected.
2751 * Hopefully we will at least detect this condition
2752 * when we try to grow the stack.
2754 if ((prev_entry->next != &map->header) &&
2755 (prev_entry->next->start < addrbos + max_ssize)) {
2757 vm_map_entry_release(count);
2758 return (KERN_NO_SPACE);
2761 /* We initially map a stack of only init_ssize. We will
2762 * grow as needed later. Since this is to be a grow
2763 * down stack, we map at the top of the range.
2765 * Note: we would normally expect prot and max to be
2766 * VM_PROT_ALL, and cow to be 0. Possibly we should
2767 * eliminate these as input parameters, and just
2768 * pass these values here in the insert call.
2770 rv = vm_map_insert(map, &count,
2771 NULL, 0, addrbos + max_ssize - init_ssize,
2772 addrbos + max_ssize, prot, max, cow);
2774 /* Now set the avail_ssize amount */
2775 if (rv == KERN_SUCCESS){
2776 if (prev_entry != &map->header)
2777 vm_map_clip_end(map, prev_entry, addrbos + max_ssize - init_ssize, &count);
2778 new_stack_entry = prev_entry->next;
2779 if (new_stack_entry->end != addrbos + max_ssize ||
2780 new_stack_entry->start != addrbos + max_ssize - init_ssize)
2781 panic ("Bad entry start/end for new stack entry");
2783 new_stack_entry->avail_ssize = max_ssize - init_ssize;
2787 vm_map_entry_release(count);
2791 /* Attempts to grow a vm stack entry. Returns KERN_SUCCESS if the
2792 * desired address is already mapped, or if we successfully grow
2793 * the stack. Also returns KERN_SUCCESS if addr is outside the
2794 * stack range (this is strange, but preserves compatibility with
2795 * the grow function in vm_machdep.c).
2798 vm_map_growstack (struct proc *p, vm_offset_t addr)
2800 vm_map_entry_t prev_entry;
2801 vm_map_entry_t stack_entry;
2802 vm_map_entry_t new_stack_entry;
2803 struct vmspace *vm = p->p_vmspace;
2804 vm_map_t map = &vm->vm_map;
2807 int rv = KERN_SUCCESS;
2809 int use_read_lock = 1;
2812 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
2815 vm_map_lock_read(map);
2819 /* If addr is already in the entry range, no need to grow.*/
2820 if (vm_map_lookup_entry(map, addr, &prev_entry))
2823 if ((stack_entry = prev_entry->next) == &map->header)
2825 if (prev_entry == &map->header)
2826 end = stack_entry->start - stack_entry->avail_ssize;
2828 end = prev_entry->end;
2830 /* This next test mimics the old grow function in vm_machdep.c.
2831 * It really doesn't quite make sense, but we do it anyway
2832 * for compatibility.
2834 * If not growable stack, return success. This signals the
2835 * caller to proceed as he would normally with normal vm.
2837 if (stack_entry->avail_ssize < 1 ||
2838 addr >= stack_entry->start ||
2839 addr < stack_entry->start - stack_entry->avail_ssize) {
2843 /* Find the minimum grow amount */
2844 grow_amount = roundup (stack_entry->start - addr, PAGE_SIZE);
2845 if (grow_amount > stack_entry->avail_ssize) {
2850 /* If there is no longer enough space between the entries
2851 * nogo, and adjust the available space. Note: this
2852 * should only happen if the user has mapped into the
2853 * stack area after the stack was created, and is
2854 * probably an error.
2856 * This also effectively destroys any guard page the user
2857 * might have intended by limiting the stack size.
2859 if (grow_amount > stack_entry->start - end) {
2860 if (use_read_lock && vm_map_lock_upgrade(map)) {
2865 stack_entry->avail_ssize = stack_entry->start - end;
2870 is_procstack = addr >= (vm_offset_t)vm->vm_maxsaddr;
2872 /* If this is the main process stack, see if we're over the
2875 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount >
2876 p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
2881 /* Round up the grow amount modulo SGROWSIZ */
2882 grow_amount = roundup (grow_amount, sgrowsiz);
2883 if (grow_amount > stack_entry->avail_ssize) {
2884 grow_amount = stack_entry->avail_ssize;
2886 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount >
2887 p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
2888 grow_amount = p->p_rlimit[RLIMIT_STACK].rlim_cur -
2892 /* If we would blow our VMEM resource limit, no go */
2893 if (map->size + grow_amount >
2894 curproc->p_rlimit[RLIMIT_VMEM].rlim_cur) {
2899 if (use_read_lock && vm_map_lock_upgrade(map)) {
2905 /* Get the preliminary new entry start value */
2906 addr = stack_entry->start - grow_amount;
2908 /* If this puts us into the previous entry, cut back our growth
2909 * to the available space. Also, see the note above.
2912 stack_entry->avail_ssize = stack_entry->start - end;
2916 rv = vm_map_insert(map, &count,
2917 NULL, 0, addr, stack_entry->start,
2922 /* Adjust the available stack space by the amount we grew. */
2923 if (rv == KERN_SUCCESS) {
2924 if (prev_entry != &map->header)
2925 vm_map_clip_end(map, prev_entry, addr, &count);
2926 new_stack_entry = prev_entry->next;
2927 if (new_stack_entry->end != stack_entry->start ||
2928 new_stack_entry->start != addr)
2929 panic ("Bad stack grow start/end in new stack entry");
2931 new_stack_entry->avail_ssize = stack_entry->avail_ssize -
2932 (new_stack_entry->end -
2933 new_stack_entry->start);
2935 vm->vm_ssize += btoc(new_stack_entry->end -
2936 new_stack_entry->start);
2942 vm_map_unlock_read(map);
2945 vm_map_entry_release(count);
2950 * Unshare the specified VM space for exec. If other processes are
2951 * mapped to it, then create a new one. The new vmspace is null.
2955 vmspace_exec(struct proc *p)
2957 struct vmspace *oldvmspace = p->p_vmspace;
2958 struct vmspace *newvmspace;
2959 vm_map_t map = &p->p_vmspace->vm_map;
2961 newvmspace = vmspace_alloc(map->min_offset, map->max_offset);
2962 bcopy(&oldvmspace->vm_startcopy, &newvmspace->vm_startcopy,
2963 (caddr_t) (newvmspace + 1) - (caddr_t) &newvmspace->vm_startcopy);
2965 * This code is written like this for prototype purposes. The
2966 * goal is to avoid running down the vmspace here, but let the
2967 * other process's that are still using the vmspace to finally
2968 * run it down. Even though there is little or no chance of blocking
2969 * here, it is a good idea to keep this form for future mods.
2971 vmspace_free(oldvmspace);
2972 p->p_vmspace = newvmspace;
2973 pmap_pinit2(vmspace_pmap(newvmspace));
2979 * Unshare the specified VM space for forcing COW. This
2980 * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
2984 vmspace_unshare(struct proc *p)
2986 struct vmspace *oldvmspace = p->p_vmspace;
2987 struct vmspace *newvmspace;
2989 if (oldvmspace->vm_refcnt == 1)
2991 newvmspace = vmspace_fork(oldvmspace);
2992 vmspace_free(oldvmspace);
2993 p->p_vmspace = newvmspace;
2994 pmap_pinit2(vmspace_pmap(newvmspace));
3002 * Finds the VM object, offset, and
3003 * protection for a given virtual address in the
3004 * specified map, assuming a page fault of the
3007 * Leaves the map in question locked for read; return
3008 * values are guaranteed until a vm_map_lookup_done
3009 * call is performed. Note that the map argument
3010 * is in/out; the returned map must be used in
3011 * the call to vm_map_lookup_done.
3013 * A handle (out_entry) is returned for use in
3014 * vm_map_lookup_done, to make that fast.
3016 * If a lookup is requested with "write protection"
3017 * specified, the map may be changed to perform virtual
3018 * copying operations, although the data referenced will
3022 vm_map_lookup(vm_map_t *var_map, /* IN/OUT */
3024 vm_prot_t fault_typea,
3025 vm_map_entry_t *out_entry, /* OUT */
3026 vm_object_t *object, /* OUT */
3027 vm_pindex_t *pindex, /* OUT */
3028 vm_prot_t *out_prot, /* OUT */
3029 boolean_t *wired) /* OUT */
3031 vm_map_entry_t entry;
3032 vm_map_t map = *var_map;
3034 vm_prot_t fault_type = fault_typea;
3035 int use_read_lock = 1;
3036 int rv = KERN_SUCCESS;
3040 vm_map_lock_read(map);
3045 * If the map has an interesting hint, try it before calling full
3046 * blown lookup routine.
3051 if ((entry == &map->header) ||
3052 (vaddr < entry->start) || (vaddr >= entry->end)) {
3053 vm_map_entry_t tmp_entry;
3056 * Entry was either not a valid hint, or the vaddr was not
3057 * contained in the entry, so do a full lookup.
3059 if (!vm_map_lookup_entry(map, vaddr, &tmp_entry)) {
3060 rv = KERN_INVALID_ADDRESS;
3072 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3073 vm_map_t old_map = map;
3075 *var_map = map = entry->object.sub_map;
3077 vm_map_unlock_read(old_map);
3079 vm_map_unlock(old_map);
3085 * Check whether this task is allowed to have this page.
3086 * Note the special case for MAP_ENTRY_COW
3087 * pages with an override. This is to implement a forced
3088 * COW for debuggers.
3091 if (fault_type & VM_PROT_OVERRIDE_WRITE)
3092 prot = entry->max_protection;
3094 prot = entry->protection;
3096 fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
3097 if ((fault_type & prot) != fault_type) {
3098 rv = KERN_PROTECTION_FAILURE;
3102 if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
3103 (entry->eflags & MAP_ENTRY_COW) &&
3104 (fault_type & VM_PROT_WRITE) &&
3105 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
3106 rv = KERN_PROTECTION_FAILURE;
3111 * If this page is not pageable, we have to get it for all possible
3115 *wired = (entry->wired_count != 0);
3117 prot = fault_type = entry->protection;
3120 * If the entry was copy-on-write, we either ...
3123 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3125 * If we want to write the page, we may as well handle that
3126 * now since we've got the map locked.
3128 * If we don't need to write the page, we just demote the
3129 * permissions allowed.
3132 if (fault_type & VM_PROT_WRITE) {
3134 * Make a new object, and place it in the object
3135 * chain. Note that no new references have appeared
3136 * -- one just moved from the map to the new
3140 if (use_read_lock && vm_map_lock_upgrade(map)) {
3147 &entry->object.vm_object,
3149 atop(entry->end - entry->start));
3151 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
3154 * We're attempting to read a copy-on-write page --
3155 * don't allow writes.
3158 prot &= ~VM_PROT_WRITE;
3163 * Create an object if necessary.
3165 if (entry->object.vm_object == NULL &&
3167 if (use_read_lock && vm_map_lock_upgrade(map)) {
3172 entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
3173 atop(entry->end - entry->start));
3178 * Return the object/offset from this entry. If the entry was
3179 * copy-on-write or empty, it has been fixed up.
3182 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
3183 *object = entry->object.vm_object;
3186 * Return whether this is the only map sharing this data. On
3187 * success we return with a read lock held on the map. On failure
3188 * we return with the map unlocked.
3192 if (rv == KERN_SUCCESS) {
3193 if (use_read_lock == 0)
3194 vm_map_lock_downgrade(map);
3195 } else if (use_read_lock) {
3196 vm_map_unlock_read(map);
3204 * vm_map_lookup_done:
3206 * Releases locks acquired by a vm_map_lookup
3207 * (according to the handle returned by that lookup).
3211 vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry, int count)
3214 * Unlock the main-level map
3216 vm_map_unlock_read(map);
3218 vm_map_entry_release(count);
3222 * Implement uiomove with VM operations. This handles (and collateral changes)
3223 * support every combination of source object modification, and COW type
3227 vm_uiomove(mapa, srcobject, cp, cnta, uaddra, npages)
3229 vm_object_t srcobject;
3236 vm_object_t first_object, oldobject, object;
3237 vm_map_entry_t entry;
3241 vm_offset_t uaddr, start, end, tend;
3242 vm_pindex_t first_pindex, osize, oindex;
3256 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
3258 if ((vm_map_lookup(&map, uaddr,
3259 VM_PROT_READ, &entry, &first_object,
3260 &first_pindex, &prot, &wired)) != KERN_SUCCESS) {
3264 vm_map_clip_start(map, entry, uaddr, &count);
3267 tend = uaddr + tcnt;
3268 if (tend > entry->end) {
3269 tcnt = entry->end - uaddr;
3273 vm_map_clip_end(map, entry, tend, &count);
3275 start = entry->start;
3280 oindex = OFF_TO_IDX(cp);
3283 for (idx = 0; idx < osize; idx++) {
3285 if ((m = vm_page_lookup(srcobject, oindex + idx)) == NULL) {
3286 vm_map_lookup_done(map, entry, count);
3290 * disallow busy or invalid pages, but allow
3291 * m->busy pages if they are entirely valid.
3293 if ((m->flags & PG_BUSY) ||
3294 ((m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL)) {
3295 vm_map_lookup_done(map, entry, count);
3302 * If we are changing an existing map entry, just redirect
3303 * the object, and change mappings.
3305 if ((first_object->type == OBJT_VNODE) &&
3306 ((oldobject = entry->object.vm_object) == first_object)) {
3308 if ((entry->offset != cp) || (oldobject != srcobject)) {
3310 * Remove old window into the file
3312 pmap_remove (map->pmap, uaddr, tend);
3315 * Force copy on write for mmaped regions
3317 vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
3320 * Point the object appropriately
3322 if (oldobject != srcobject) {
3325 * Set the object optimization hint flag
3327 vm_object_set_flag(srcobject, OBJ_OPT);
3328 vm_object_reference(srcobject);
3329 entry->object.vm_object = srcobject;
3332 vm_object_deallocate(oldobject);
3339 pmap_remove (map->pmap, uaddr, tend);
3342 } else if ((first_object->ref_count == 1) &&
3343 (first_object->size == osize) &&
3344 ((first_object->type == OBJT_DEFAULT) ||
3345 (first_object->type == OBJT_SWAP)) ) {
3347 oldobject = first_object->backing_object;
3349 if ((first_object->backing_object_offset != cp) ||
3350 (oldobject != srcobject)) {
3352 * Remove old window into the file
3354 pmap_remove (map->pmap, uaddr, tend);
3357 * Remove unneeded old pages
3359 vm_object_page_remove(first_object, 0, 0, 0);
3362 * Invalidate swap space
3364 if (first_object->type == OBJT_SWAP) {
3365 swap_pager_freespace(first_object,
3367 first_object->size);
3371 * Force copy on write for mmaped regions
3373 vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
3376 * Point the object appropriately
3378 if (oldobject != srcobject) {
3381 * Set the object optimization hint flag
3383 vm_object_set_flag(srcobject, OBJ_OPT);
3384 vm_object_reference(srcobject);
3388 first_object, shadow_list);
3389 oldobject->shadow_count--;
3390 /* XXX bump generation? */
3391 vm_object_deallocate(oldobject);
3394 LIST_INSERT_HEAD(&srcobject->shadow_head,
3395 first_object, shadow_list);
3396 srcobject->shadow_count++;
3397 /* XXX bump generation? */
3399 first_object->backing_object = srcobject;
3401 first_object->backing_object_offset = cp;
3404 pmap_remove (map->pmap, uaddr, tend);
3407 * Otherwise, we have to do a logical mmap.
3411 vm_object_set_flag(srcobject, OBJ_OPT);
3412 vm_object_reference(srcobject);
3414 pmap_remove (map->pmap, uaddr, tend);
3416 vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
3417 vm_map_lock_upgrade(map);
3419 if (entry == &map->header) {
3420 map->first_free = &map->header;
3421 } else if (map->first_free->start >= start) {
3422 map->first_free = entry->prev;
3425 SAVE_HINT(map, entry->prev);
3426 vm_map_entry_delete(map, entry, &count);
3431 rv = vm_map_insert(map, &count,
3432 object, ooffset, start, tend,
3433 VM_PROT_ALL, VM_PROT_ALL, MAP_COPY_ON_WRITE);
3435 if (rv != KERN_SUCCESS)
3436 panic("vm_uiomove: could not insert new entry: %d", rv);
3440 * Map the window directly, if it is already in memory
3442 pmap_object_init_pt(map->pmap, uaddr,
3443 srcobject, oindex, tcnt, 0);
3447 vm_map_entry_release(count);
3459 * Performs the copy_on_write operations necessary to allow the virtual copies
3460 * into user space to work. This has to be called for write(2) system calls
3461 * from other processes, file unlinking, and file size shrinkage.
3464 vm_freeze_copyopts(object, froma, toa)
3466 vm_pindex_t froma, toa;
3469 vm_object_t robject;
3472 if ((object == NULL) ||
3473 ((object->flags & OBJ_OPT) == 0))
3476 if (object->shadow_count > object->ref_count)
3477 panic("vm_freeze_copyopts: sc > rc");
3479 while((robject = LIST_FIRST(&object->shadow_head)) != NULL) {
3480 vm_pindex_t bo_pindex;
3481 vm_page_t m_in, m_out;
3483 bo_pindex = OFF_TO_IDX(robject->backing_object_offset);
3485 vm_object_reference(robject);
3487 vm_object_pip_wait(robject, "objfrz");
3489 if (robject->ref_count == 1) {
3490 vm_object_deallocate(robject);
3494 vm_object_pip_add(robject, 1);
3496 for (idx = 0; idx < robject->size; idx++) {
3498 m_out = vm_page_grab(robject, idx,
3499 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
3501 if (m_out->valid == 0) {
3502 m_in = vm_page_grab(object, bo_pindex + idx,
3503 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
3504 if (m_in->valid == 0) {
3505 rv = vm_pager_get_pages(object, &m_in, 1, 0);
3506 if (rv != VM_PAGER_OK) {
3507 printf("vm_freeze_copyopts: cannot read page from file: %lx\n", (long)m_in->pindex);
3510 vm_page_deactivate(m_in);
3513 vm_page_protect(m_in, VM_PROT_NONE);
3514 pmap_copy_page(VM_PAGE_TO_PHYS(m_in), VM_PAGE_TO_PHYS(m_out));
3515 m_out->valid = m_in->valid;
3516 vm_page_dirty(m_out);
3517 vm_page_activate(m_out);
3518 vm_page_wakeup(m_in);
3520 vm_page_wakeup(m_out);
3523 object->shadow_count--;
3524 object->ref_count--;
3525 LIST_REMOVE(robject, shadow_list);
3526 robject->backing_object = NULL;
3527 robject->backing_object_offset = 0;
3529 vm_object_pip_wakeup(robject);
3530 vm_object_deallocate(robject);
3533 vm_object_clear_flag(object, OBJ_OPT);
3536 #include "opt_ddb.h"
3538 #include <sys/kernel.h>
3540 #include <ddb/ddb.h>
3543 * vm_map_print: [ debug ]
3545 DB_SHOW_COMMAND(map, vm_map_print)
3548 /* XXX convert args. */
3549 vm_map_t map = (vm_map_t)addr;
3550 boolean_t full = have_addr;
3552 vm_map_entry_t entry;
3554 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
3556 (void *)map->pmap, map->nentries, map->timestamp);
3559 if (!full && db_indent)
3563 for (entry = map->header.next; entry != &map->header;
3564 entry = entry->next) {
3565 db_iprintf("map entry %p: start=%p, end=%p\n",
3566 (void *)entry, (void *)entry->start, (void *)entry->end);
3569 static char *inheritance_name[4] =
3570 {"share", "copy", "none", "donate_copy"};
3572 db_iprintf(" prot=%x/%x/%s",
3574 entry->max_protection,
3575 inheritance_name[(int)(unsigned char)entry->inheritance]);
3576 if (entry->wired_count != 0)
3577 db_printf(", wired");
3579 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3580 /* XXX no %qd in kernel. Truncate entry->offset. */
3581 db_printf(", share=%p, offset=0x%lx\n",
3582 (void *)entry->object.sub_map,
3583 (long)entry->offset);
3585 if ((entry->prev == &map->header) ||
3586 (entry->prev->object.sub_map !=
3587 entry->object.sub_map)) {
3589 vm_map_print((db_expr_t)(intptr_t)
3590 entry->object.sub_map,
3591 full, 0, (char *)0);
3595 /* XXX no %qd in kernel. Truncate entry->offset. */
3596 db_printf(", object=%p, offset=0x%lx",
3597 (void *)entry->object.vm_object,
3598 (long)entry->offset);
3599 if (entry->eflags & MAP_ENTRY_COW)
3600 db_printf(", copy (%s)",
3601 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
3605 if ((entry->prev == &map->header) ||
3606 (entry->prev->object.vm_object !=
3607 entry->object.vm_object)) {
3609 vm_object_print((db_expr_t)(intptr_t)
3610 entry->object.vm_object,
3611 full, 0, (char *)0);
3623 DB_SHOW_COMMAND(procvm, procvm)
3628 p = (struct proc *) addr;
3633 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
3634 (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
3635 (void *)vmspace_pmap(p->p_vmspace));
3637 vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);