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 $
68 * Virtual memory mapping module.
71 #include <sys/param.h>
72 #include <sys/systm.h>
74 #include <sys/vmmeter.h>
76 #include <sys/vnode.h>
77 #include <sys/resourcevar.h>
80 #include <vm/vm_param.h>
83 #include <vm/vm_map.h>
84 #include <vm/vm_page.h>
85 #include <vm/vm_object.h>
86 #include <vm/vm_pager.h>
87 #include <vm/vm_kern.h>
88 #include <vm/vm_extern.h>
89 #include <vm/swap_pager.h>
90 #include <vm/vm_zone.h>
93 * Virtual memory maps provide for the mapping, protection,
94 * and sharing of virtual memory objects. In addition,
95 * this module provides for an efficient virtual copy of
96 * memory from one map to another.
98 * Synchronization is required prior to most operations.
100 * Maps consist of an ordered doubly-linked list of simple
101 * entries; a single hint is used to speed up lookups.
103 * Since portions of maps are specified by start/end addresses,
104 * which may not align with existing map entries, all
105 * routines merely "clip" entries to these start/end values.
106 * [That is, an entry is split into two, bordering at a
107 * start or end value.] Note that these clippings may not
108 * always be necessary (as the two resulting entries are then
109 * not changed); however, the clipping is done for convenience.
111 * As mentioned above, virtual copy operations are performed
112 * by copying VM object references from one map to
113 * another, and then marking both regions as copy-on-write.
119 * Initialize the vm_map module. Must be called before
120 * any other vm_map routines.
122 * Map and entry structures are allocated from the general
123 * purpose memory pool with some exceptions:
125 * - The kernel map and kmem submap are allocated statically.
126 * - Kernel map entries are allocated out of a static pool.
128 * These restrictions are necessary since malloc() uses the
129 * maps and requires map entries.
132 static struct vm_zone kmapentzone_store, mapentzone_store, mapzone_store;
133 static vm_zone_t mapentzone, kmapentzone, mapzone, vmspace_zone;
134 static struct vm_object kmapentobj, mapentobj, mapobj;
136 static struct vm_map_entry map_entry_init[MAX_MAPENT];
137 static struct vm_map_entry kmap_entry_init[MAX_KMAPENT];
138 static struct vm_map map_init[MAX_KMAP];
140 static void _vm_map_clip_end __P((vm_map_t, vm_map_entry_t, vm_offset_t));
141 static void _vm_map_clip_start __P((vm_map_t, vm_map_entry_t, vm_offset_t));
142 static vm_map_entry_t vm_map_entry_create __P((vm_map_t));
143 static void vm_map_entry_delete __P((vm_map_t, vm_map_entry_t));
144 static void vm_map_entry_dispose __P((vm_map_t, vm_map_entry_t));
145 static void vm_map_entry_unwire __P((vm_map_t, vm_map_entry_t));
146 static void vm_map_copy_entry __P((vm_map_t, vm_map_t, vm_map_entry_t,
148 static void vm_map_split __P((vm_map_entry_t));
149 static void vm_map_unclip_range __P((vm_map_t map, vm_map_entry_t start_entry, vm_offset_t start, vm_offset_t end, int flags));
154 mapzone = &mapzone_store;
155 zbootinit(mapzone, "MAP", sizeof (struct vm_map),
157 kmapentzone = &kmapentzone_store;
158 zbootinit(kmapentzone, "KMAP ENTRY", sizeof (struct vm_map_entry),
159 kmap_entry_init, MAX_KMAPENT);
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;
188 zinitna(kmapentzone, &kmapentobj,
189 NULL, 0, lmin((VM_MAX_KERNEL_ADDRESS - KERNBASE) / PAGE_SIZE,
190 cnt.v_page_count) / 8, ZONE_INTERRUPT, 1);
191 zinitna(mapentzone, &mapentobj,
193 zinitna(mapzone, &mapobj,
195 vmspace_zone = zinit("VMSPACE", sizeof (struct vmspace), 0, 0, 3);
201 vmspace_dofree(struct vmspace *vm)
204 * Lock the map, to wait out all other references to it.
205 * Delete all of the mappings and pages they hold, then call
206 * the pmap module to reclaim anything left.
208 vm_map_lock(&vm->vm_map);
209 (void) vm_map_delete(&vm->vm_map, vm->vm_map.min_offset,
210 vm->vm_map.max_offset);
211 vm_map_unlock(&vm->vm_map);
213 pmap_release(vmspace_pmap(vm));
214 zfree(vmspace_zone, vm);
218 vmspace_free(struct vmspace *vm)
220 if (vm->vm_refcnt == 0)
221 panic("vmspace_free: attempt to free already freed vmspace");
223 if (--vm->vm_refcnt == 0 && vm->vm_exitingcnt == 0)
228 vmspace_exitfree(struct proc *p)
236 * cleanup by parent process wait()ing on exiting child. vm_refcnt
237 * may not be 0 (e.g. fork() and child exits without exec()ing).
238 * exitingcnt may increment above 0 and drop back down to zero
239 * several times while vm_refcnt is held non-zero. vm_refcnt
240 * may also increment above 0 and drop back down to zero several
241 * times while vm_exitingcnt is held non-zero.
243 * The last wait on the exiting child's vmspace will clean up
244 * the remainder of the vmspace.
246 if (--vm->vm_exitingcnt == 0 && vm->vm_refcnt == 0)
251 * vmspace_swap_count() - count the approximate swap useage in pages for a
254 * Swap useage is determined by taking the proportional swap used by
255 * VM objects backing the VM map. To make up for fractional losses,
256 * if the VM object has any swap use at all the associated map entries
257 * count for at least 1 swap page.
260 vmspace_swap_count(struct vmspace *vmspace)
262 vm_map_t map = &vmspace->vm_map;
266 for (cur = map->header.next; cur != &map->header; cur = cur->next) {
269 if ((cur->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
270 (object = cur->object.vm_object) != NULL &&
271 object->type == OBJT_SWAP
273 int n = (cur->end - cur->start) / PAGE_SIZE;
275 if (object->un_pager.swp.swp_bcount) {
276 count += object->un_pager.swp.swp_bcount *
277 SWAP_META_PAGES * n / object->size + 1;
288 * Creates and returns a new empty VM map with
289 * the given physical map structure, and having
290 * the given lower and upper address bounds.
293 vm_map_create(pmap, min, max)
295 vm_offset_t min, max;
299 result = zalloc(mapzone);
300 vm_map_init(result, min, max);
306 * Initialize an existing vm_map structure
307 * such as that in the vmspace structure.
308 * The pmap is set elsewhere.
311 vm_map_init(map, min, max)
313 vm_offset_t min, max;
315 map->header.next = map->header.prev = &map->header;
320 map->min_offset = min;
321 map->max_offset = max;
322 map->first_free = &map->header;
323 map->hint = &map->header;
325 lockinit(&map->lock, PVM, "thrd_sleep", 0, LK_NOPAUSE);
329 * vm_map_entry_dispose: [ internal use only ]
331 * Inverse of vm_map_entry_create.
334 vm_map_entry_dispose(map, entry)
336 vm_map_entry_t entry;
338 if (map->system_map || !mapentzone)
339 zfreei(kmapentzone, entry);
341 zfree(mapentzone, entry);
345 * vm_map_entry_create: [ internal use only ]
347 * Allocates a VM map entry for insertion.
348 * No entry fields are filled in. This routine is
350 static vm_map_entry_t
351 vm_map_entry_create(map)
354 vm_map_entry_t new_entry;
356 if (map->system_map || !mapentzone)
357 new_entry = zalloci(kmapentzone);
359 new_entry = zalloc(mapentzone);
360 if (new_entry == NULL)
361 panic("vm_map_entry_create: kernel resources exhausted");
366 * vm_map_entry_{un,}link:
368 * Insert/remove entries from maps.
371 vm_map_entry_link(vm_map_t map,
372 vm_map_entry_t after_where,
373 vm_map_entry_t entry)
376 entry->prev = after_where;
377 entry->next = after_where->next;
378 entry->next->prev = entry;
379 after_where->next = entry;
383 vm_map_entry_unlink(vm_map_t map,
384 vm_map_entry_t entry)
389 if (entry->eflags & MAP_ENTRY_IN_TRANSITION)
390 panic("vm_map_entry_unlink: attempt to mess with locked entry! %p", entry);
401 * Saves the specified entry as the hint for
404 #define SAVE_HINT(map,value) \
405 (map)->hint = (value);
408 * vm_map_lookup_entry: [ internal use only ]
410 * Finds the map entry containing (or
411 * immediately preceding) the specified address
412 * in the given map; the entry is returned
413 * in the "entry" parameter. The boolean
414 * result indicates whether the address is
415 * actually contained in the map.
418 vm_map_lookup_entry(map, address, entry)
421 vm_map_entry_t *entry; /* OUT */
427 * Start looking either from the head of the list, or from the hint.
432 if (cur == &map->header)
435 if (address >= cur->start) {
437 * Go from hint to end of list.
439 * But first, make a quick check to see if we are already looking
440 * at the entry we want (which is usually the case). Note also
441 * that we don't need to save the hint here... it is the same
442 * hint (unless we are at the header, in which case the hint
443 * didn't buy us anything anyway).
446 if ((cur != last) && (cur->end > address)) {
452 * Go from start to hint, *inclusively*
455 cur = map->header.next;
462 while (cur != last) {
463 if (cur->end > address) {
464 if (address >= cur->start) {
466 * Save this lookup for future hints, and
479 SAVE_HINT(map, *entry);
486 * Inserts the given whole VM object into the target
487 * map at the specified address range. The object's
488 * size should match that of the address range.
490 * Requires that the map be locked, and leaves it so.
492 * If object is non-NULL, ref count must be bumped by caller
493 * prior to making call to account for the new entry.
496 vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
497 vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max,
500 vm_map_entry_t new_entry;
501 vm_map_entry_t prev_entry;
502 vm_map_entry_t temp_entry;
503 vm_eflags_t protoeflags;
506 * Check that the start and end points are not bogus.
509 if ((start < map->min_offset) || (end > map->max_offset) ||
511 return (KERN_INVALID_ADDRESS);
514 * Find the entry prior to the proposed starting address; if it's part
515 * of an existing entry, this range is bogus.
518 if (vm_map_lookup_entry(map, start, &temp_entry))
519 return (KERN_NO_SPACE);
521 prev_entry = temp_entry;
524 * Assert that the next entry doesn't overlap the end point.
527 if ((prev_entry->next != &map->header) &&
528 (prev_entry->next->start < end))
529 return (KERN_NO_SPACE);
533 if (cow & MAP_COPY_ON_WRITE)
534 protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
536 if (cow & MAP_NOFAULT) {
537 protoeflags |= MAP_ENTRY_NOFAULT;
539 KASSERT(object == NULL,
540 ("vm_map_insert: paradoxical MAP_NOFAULT request"));
542 if (cow & MAP_DISABLE_SYNCER)
543 protoeflags |= MAP_ENTRY_NOSYNC;
544 if (cow & MAP_DISABLE_COREDUMP)
545 protoeflags |= MAP_ENTRY_NOCOREDUMP;
549 * When object is non-NULL, it could be shared with another
550 * process. We have to set or clear OBJ_ONEMAPPING
553 if ((object->ref_count > 1) || (object->shadow_count != 0)) {
554 vm_object_clear_flag(object, OBJ_ONEMAPPING);
557 else if ((prev_entry != &map->header) &&
558 (prev_entry->eflags == protoeflags) &&
559 (prev_entry->end == start) &&
560 (prev_entry->wired_count == 0) &&
561 ((prev_entry->object.vm_object == NULL) ||
562 vm_object_coalesce(prev_entry->object.vm_object,
563 OFF_TO_IDX(prev_entry->offset),
564 (vm_size_t)(prev_entry->end - prev_entry->start),
565 (vm_size_t)(end - prev_entry->end)))) {
567 * We were able to extend the object. Determine if we
568 * can extend the previous map entry to include the
571 if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
572 (prev_entry->protection == prot) &&
573 (prev_entry->max_protection == max)) {
574 map->size += (end - prev_entry->end);
575 prev_entry->end = end;
576 vm_map_simplify_entry(map, prev_entry);
577 return (KERN_SUCCESS);
581 * If we can extend the object but cannot extend the
582 * map entry, we have to create a new map entry. We
583 * must bump the ref count on the extended object to
584 * account for it. object may be NULL.
586 object = prev_entry->object.vm_object;
587 offset = prev_entry->offset +
588 (prev_entry->end - prev_entry->start);
589 vm_object_reference(object);
593 * NOTE: if conditionals fail, object can be NULL here. This occurs
594 * in things like the buffer map where we manage kva but do not manage
602 new_entry = vm_map_entry_create(map);
603 new_entry->start = start;
604 new_entry->end = end;
606 new_entry->eflags = protoeflags;
607 new_entry->object.vm_object = object;
608 new_entry->offset = offset;
609 new_entry->avail_ssize = 0;
611 new_entry->inheritance = VM_INHERIT_DEFAULT;
612 new_entry->protection = prot;
613 new_entry->max_protection = max;
614 new_entry->wired_count = 0;
617 * Insert the new entry into the list
620 vm_map_entry_link(map, prev_entry, new_entry);
621 map->size += new_entry->end - new_entry->start;
624 * Update the free space hint
626 if ((map->first_free == prev_entry) &&
627 (prev_entry->end >= new_entry->start)) {
628 map->first_free = new_entry;
633 * Temporarily removed to avoid MAP_STACK panic, due to
634 * MAP_STACK being a huge hack. Will be added back in
635 * when MAP_STACK (and the user stack mapping) is fixed.
638 * It may be possible to simplify the entry
640 vm_map_simplify_entry(map, new_entry);
643 if (cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) {
644 pmap_object_init_pt(map->pmap, start,
645 object, OFF_TO_IDX(offset), end - start,
646 cow & MAP_PREFAULT_PARTIAL);
649 return (KERN_SUCCESS);
653 * Find sufficient space for `length' bytes in the given map, starting at
654 * `start'. The map must be locked. Returns 0 on success, 1 on no space.
657 vm_map_findspace(map, start, length, addr)
663 vm_map_entry_t entry, next;
666 if (start < map->min_offset)
667 start = map->min_offset;
668 if (start > map->max_offset)
672 * Look for the first possible address; if there's already something
673 * at this address, we have to start after it.
675 if (start == map->min_offset) {
676 if ((entry = map->first_free) != &map->header)
681 if (vm_map_lookup_entry(map, start, &tmp))
687 * Look through the rest of the map, trying to fit a new region in the
688 * gap between existing regions, or after the very last region.
690 for (;; start = (entry = next)->end) {
692 * Find the end of the proposed new region. Be sure we didn't
693 * go beyond the end of the map, or wrap around the address;
694 * if so, we lose. Otherwise, if this is the last entry, or
695 * if the proposed new region fits before the next entry, we
698 end = start + length;
699 if (end > map->max_offset || end < start)
702 if (next == &map->header || next->start >= end)
705 SAVE_HINT(map, entry);
707 if (map == kernel_map) {
709 if ((ksize = round_page(start + length)) > kernel_vm_end) {
710 pmap_growkernel(ksize);
717 * vm_map_find finds an unallocated region in the target address
718 * map with the given length. The search is defined to be
719 * first-fit from the specified address; the region found is
720 * returned in the same parameter.
722 * If object is non-NULL, ref count must be bumped by caller
723 * prior to making call to account for the new entry.
726 vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
727 vm_offset_t *addr, /* IN/OUT */
728 vm_size_t length, boolean_t find_space, vm_prot_t prot,
729 vm_prot_t max, int cow)
736 if (map == kmem_map || map == mb_map)
741 if (vm_map_findspace(map, start, length, addr)) {
743 if (map == kmem_map || map == mb_map)
745 return (KERN_NO_SPACE);
749 result = vm_map_insert(map, object, offset,
750 start, start + length, prot, max, cow);
753 if (map == kmem_map || map == mb_map)
760 * vm_map_simplify_entry:
762 * Simplify the given map entry by merging with either neighbor. This
763 * routine also has the ability to merge with both neighbors.
765 * The map must be locked.
767 * This routine guarentees that the passed entry remains valid (though
768 * possibly extended). When merging, this routine may delete one or
769 * both neighbors. No action is taken on entries which have their
770 * in-transition flag set.
773 vm_map_simplify_entry(map, entry)
775 vm_map_entry_t entry;
777 vm_map_entry_t next, prev;
778 vm_size_t prevsize, esize;
780 if (entry->eflags & (MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP)) {
781 ++cnt.v_intrans_coll;
786 if (prev != &map->header) {
787 prevsize = prev->end - prev->start;
788 if ( (prev->end == entry->start) &&
789 (prev->object.vm_object == entry->object.vm_object) &&
790 (!prev->object.vm_object ||
791 (prev->offset + prevsize == entry->offset)) &&
792 (prev->eflags == entry->eflags) &&
793 (prev->protection == entry->protection) &&
794 (prev->max_protection == entry->max_protection) &&
795 (prev->inheritance == entry->inheritance) &&
796 (prev->wired_count == entry->wired_count)) {
797 if (map->first_free == prev)
798 map->first_free = entry;
799 if (map->hint == prev)
801 vm_map_entry_unlink(map, prev);
802 entry->start = prev->start;
803 entry->offset = prev->offset;
804 if (prev->object.vm_object)
805 vm_object_deallocate(prev->object.vm_object);
806 vm_map_entry_dispose(map, prev);
811 if (next != &map->header) {
812 esize = entry->end - entry->start;
813 if ((entry->end == next->start) &&
814 (next->object.vm_object == entry->object.vm_object) &&
815 (!entry->object.vm_object ||
816 (entry->offset + esize == next->offset)) &&
817 (next->eflags == entry->eflags) &&
818 (next->protection == entry->protection) &&
819 (next->max_protection == entry->max_protection) &&
820 (next->inheritance == entry->inheritance) &&
821 (next->wired_count == entry->wired_count)) {
822 if (map->first_free == next)
823 map->first_free = entry;
824 if (map->hint == next)
826 vm_map_entry_unlink(map, next);
827 entry->end = next->end;
828 if (next->object.vm_object)
829 vm_object_deallocate(next->object.vm_object);
830 vm_map_entry_dispose(map, next);
835 * vm_map_clip_start: [ internal use only ]
837 * Asserts that the given entry begins at or after
838 * the specified address; if necessary,
839 * it splits the entry into two.
841 #define vm_map_clip_start(map, entry, startaddr) \
843 if (startaddr > entry->start) \
844 _vm_map_clip_start(map, entry, startaddr); \
848 * This routine is called only when it is known that
849 * the entry must be split.
852 _vm_map_clip_start(map, entry, start)
854 vm_map_entry_t entry;
857 vm_map_entry_t new_entry;
860 * Split off the front portion -- note that we must insert the new
861 * entry BEFORE this one, so that this entry has the specified
865 vm_map_simplify_entry(map, entry);
868 * If there is no object backing this entry, we might as well create
869 * one now. If we defer it, an object can get created after the map
870 * is clipped, and individual objects will be created for the split-up
871 * map. This is a bit of a hack, but is also about the best place to
872 * put this improvement.
875 if (entry->object.vm_object == NULL && !map->system_map) {
877 object = vm_object_allocate(OBJT_DEFAULT,
878 atop(entry->end - entry->start));
879 entry->object.vm_object = object;
883 new_entry = vm_map_entry_create(map);
886 new_entry->end = start;
887 entry->offset += (start - entry->start);
888 entry->start = start;
890 vm_map_entry_link(map, entry->prev, new_entry);
892 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
893 vm_object_reference(new_entry->object.vm_object);
898 * vm_map_clip_end: [ internal use only ]
900 * Asserts that the given entry ends at or before
901 * the specified address; if necessary,
902 * it splits the entry into two.
905 #define vm_map_clip_end(map, entry, endaddr) \
907 if (endaddr < entry->end) \
908 _vm_map_clip_end(map, entry, endaddr); \
912 * This routine is called only when it is known that
913 * the entry must be split.
916 _vm_map_clip_end(map, entry, end)
918 vm_map_entry_t entry;
921 vm_map_entry_t new_entry;
924 * If there is no object backing this entry, we might as well create
925 * one now. If we defer it, an object can get created after the map
926 * is clipped, and individual objects will be created for the split-up
927 * map. This is a bit of a hack, but is also about the best place to
928 * put this improvement.
931 if (entry->object.vm_object == NULL && !map->system_map) {
933 object = vm_object_allocate(OBJT_DEFAULT,
934 atop(entry->end - entry->start));
935 entry->object.vm_object = object;
940 * Create a new entry and insert it AFTER the specified entry
943 new_entry = vm_map_entry_create(map);
946 new_entry->start = entry->end = end;
947 new_entry->offset += (end - entry->start);
949 vm_map_entry_link(map, entry, new_entry);
951 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
952 vm_object_reference(new_entry->object.vm_object);
957 * VM_MAP_RANGE_CHECK: [ internal use only ]
959 * Asserts that the starting and ending region
960 * addresses fall within the valid range of the map.
962 #define VM_MAP_RANGE_CHECK(map, start, end) \
964 if (start < vm_map_min(map)) \
965 start = vm_map_min(map); \
966 if (end > vm_map_max(map)) \
967 end = vm_map_max(map); \
973 * vm_map_transition_wait: [ kernel use only ]
975 * Used to block when an in-transition collison occurs. The map
976 * is unlocked for the sleep and relocked before the return.
980 vm_map_transition_wait(vm_map_t map)
983 tsleep(map, PVM, "vment", 0);
991 * When we do blocking operations with the map lock held it is
992 * possible that a clip might have occured on our in-transit entry,
993 * requiring an adjustment to the entry in our loop. These macros
994 * help the pageable and clip_range code deal with the case. The
995 * conditional costs virtually nothing if no clipping has occured.
998 #define CLIP_CHECK_BACK(entry, save_start) \
1000 while (entry->start != save_start) { \
1001 entry = entry->prev; \
1002 KASSERT(entry != &map->header, ("bad entry clip")); \
1006 #define CLIP_CHECK_FWD(entry, save_end) \
1008 while (entry->end != save_end) { \
1009 entry = entry->next; \
1010 KASSERT(entry != &map->header, ("bad entry clip")); \
1016 * vm_map_clip_range: [ kernel use only ]
1018 * Clip the specified range and return the base entry. The
1019 * range may cover several entries starting at the returned base
1020 * and the first and last entry in the covering sequence will be
1021 * properly clipped to the requested start and end address.
1023 * If no holes are allowed you should pass the MAP_CLIP_NO_HOLES
1026 * The MAP_ENTRY_IN_TRANSITION flag will be set for the entries
1027 * covered by the requested range.
1029 * The map must be exclusively locked on entry and will remain locked
1030 * on return. If no range exists or the range contains holes and you
1031 * specified that no holes were allowed, NULL will be returned. This
1032 * routine may temporarily unlock the map in order avoid a deadlock when
1037 vm_map_clip_range(vm_map_t map, vm_offset_t start, vm_offset_t end, int flags)
1039 vm_map_entry_t start_entry;
1040 vm_map_entry_t entry;
1043 * Locate the entry and effect initial clipping. The in-transition
1044 * case does not occur very often so do not try to optimize it.
1047 if (vm_map_lookup_entry(map, start, &start_entry) == FALSE)
1049 entry = start_entry;
1050 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1051 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1052 ++cnt.v_intrans_coll;
1053 ++cnt.v_intrans_wait;
1054 vm_map_transition_wait(map);
1056 * entry and/or start_entry may have been clipped while
1057 * we slept, or may have gone away entirely. We have
1058 * to restart from the lookup.
1063 * Since we hold an exclusive map lock we do not have to restart
1064 * after clipping, even though clipping may block in zalloc.
1066 vm_map_clip_start(map, entry, start);
1067 vm_map_clip_end(map, entry, end);
1068 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
1071 * Scan entries covered by the range. When working on the next
1072 * entry a restart need only re-loop on the current entry which
1073 * we have already locked, since 'next' may have changed. Also,
1074 * even though entry is safe, it may have been clipped so we
1075 * have to iterate forwards through the clip after sleeping.
1077 while (entry->next != &map->header && entry->next->start < end) {
1078 vm_map_entry_t next = entry->next;
1080 if (flags & MAP_CLIP_NO_HOLES) {
1081 if (next->start > entry->end) {
1082 vm_map_unclip_range(map, start_entry,
1083 start, entry->end, flags);
1088 if (next->eflags & MAP_ENTRY_IN_TRANSITION) {
1089 vm_offset_t save_end = entry->end;
1090 next->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1091 ++cnt.v_intrans_coll;
1092 ++cnt.v_intrans_wait;
1093 vm_map_transition_wait(map);
1096 * clips might have occured while we blocked.
1098 CLIP_CHECK_FWD(entry, save_end);
1099 CLIP_CHECK_BACK(start_entry, start);
1103 * No restart necessary even though clip_end may block, we
1104 * are holding the map lock.
1106 vm_map_clip_end(map, next, end);
1107 next->eflags |= MAP_ENTRY_IN_TRANSITION;
1110 if (flags & MAP_CLIP_NO_HOLES) {
1111 if (entry->end != end) {
1112 vm_map_unclip_range(map, start_entry,
1113 start, entry->end, flags);
1117 return(start_entry);
1121 * vm_map_unclip_range: [ kernel use only ]
1123 * Undo the effect of vm_map_clip_range(). You should pass the same
1124 * flags and the same range that you passed to vm_map_clip_range().
1125 * This code will clear the in-transition flag on the entries and
1126 * wake up anyone waiting. This code will also simplify the sequence
1127 * and attempt to merge it with entries before and after the sequence.
1129 * The map must be locked on entry and will remain locked on return.
1131 * Note that you should also pass the start_entry returned by
1132 * vm_map_clip_range(). However, if you block between the two calls
1133 * with the map unlocked please be aware that the start_entry may
1134 * have been clipped and you may need to scan it backwards to find
1135 * the entry corresponding with the original start address. You are
1136 * responsible for this, vm_map_unclip_range() expects the correct
1137 * start_entry to be passed to it and will KASSERT otherwise.
1141 vm_map_unclip_range(
1143 vm_map_entry_t start_entry,
1148 vm_map_entry_t entry;
1150 entry = start_entry;
1152 KASSERT(entry->start == start, ("unclip_range: illegal base entry"));
1153 while (entry != &map->header && entry->start < end) {
1154 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION, ("in-transition flag not set during unclip on: %p", entry));
1155 KASSERT(entry->end <= end, ("unclip_range: tail wasn't clipped"));
1156 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
1157 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
1158 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
1161 entry = entry->next;
1165 * Simplification does not block so there is no restart case.
1167 entry = start_entry;
1168 while (entry != &map->header && entry->start < end) {
1169 vm_map_simplify_entry(map, entry);
1170 entry = entry->next;
1175 * vm_map_submap: [ kernel use only ]
1177 * Mark the given range as handled by a subordinate map.
1179 * This range must have been created with vm_map_find,
1180 * and no other operations may have been performed on this
1181 * range prior to calling vm_map_submap.
1183 * Only a limited number of operations can be performed
1184 * within this rage after calling vm_map_submap:
1186 * [Don't try vm_map_copy!]
1188 * To remove a submapping, one must first remove the
1189 * range from the superior map, and then destroy the
1190 * submap (if desired). [Better yet, don't try it.]
1193 vm_map_submap(map, start, end, submap)
1199 vm_map_entry_t entry;
1200 int result = KERN_INVALID_ARGUMENT;
1204 VM_MAP_RANGE_CHECK(map, start, end);
1206 if (vm_map_lookup_entry(map, start, &entry)) {
1207 vm_map_clip_start(map, entry, start);
1209 entry = entry->next;
1212 vm_map_clip_end(map, entry, end);
1214 if ((entry->start == start) && (entry->end == end) &&
1215 ((entry->eflags & MAP_ENTRY_COW) == 0) &&
1216 (entry->object.vm_object == NULL)) {
1217 entry->object.sub_map = submap;
1218 entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
1219 result = KERN_SUCCESS;
1229 * Sets the protection of the specified address
1230 * region in the target map. If "set_max" is
1231 * specified, the maximum protection is to be set;
1232 * otherwise, only the current protection is affected.
1235 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
1236 vm_prot_t new_prot, boolean_t set_max)
1238 vm_map_entry_t current;
1239 vm_map_entry_t entry;
1243 VM_MAP_RANGE_CHECK(map, start, end);
1245 if (vm_map_lookup_entry(map, start, &entry)) {
1246 vm_map_clip_start(map, entry, start);
1248 entry = entry->next;
1252 * Make a first pass to check for protection violations.
1256 while ((current != &map->header) && (current->start < end)) {
1257 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1259 return (KERN_INVALID_ARGUMENT);
1261 if ((new_prot & current->max_protection) != new_prot) {
1263 return (KERN_PROTECTION_FAILURE);
1265 current = current->next;
1269 * Go back and fix up protections. [Note that clipping is not
1270 * necessary the second time.]
1275 while ((current != &map->header) && (current->start < end)) {
1278 vm_map_clip_end(map, current, end);
1280 old_prot = current->protection;
1282 current->protection =
1283 (current->max_protection = new_prot) &
1286 current->protection = new_prot;
1289 * Update physical map if necessary. Worry about copy-on-write
1290 * here -- CHECK THIS XXX
1293 if (current->protection != old_prot) {
1294 #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1297 pmap_protect(map->pmap, current->start,
1299 current->protection & MASK(current));
1303 vm_map_simplify_entry(map, current);
1305 current = current->next;
1309 return (KERN_SUCCESS);
1315 * This routine traverses a processes map handling the madvise
1316 * system call. Advisories are classified as either those effecting
1317 * the vm_map_entry structure, or those effecting the underlying
1322 vm_map_madvise(map, start, end, behav)
1324 vm_offset_t start, end;
1327 vm_map_entry_t current, entry;
1331 * Some madvise calls directly modify the vm_map_entry, in which case
1332 * we need to use an exclusive lock on the map and we need to perform
1333 * various clipping operations. Otherwise we only need a read-lock
1339 case MADV_SEQUENTIAL:
1351 vm_map_lock_read(map);
1354 return (KERN_INVALID_ARGUMENT);
1358 * Locate starting entry and clip if necessary.
1361 VM_MAP_RANGE_CHECK(map, start, end);
1363 if (vm_map_lookup_entry(map, start, &entry)) {
1365 vm_map_clip_start(map, entry, start);
1367 entry = entry->next;
1372 * madvise behaviors that are implemented in the vm_map_entry.
1374 * We clip the vm_map_entry so that behavioral changes are
1375 * limited to the specified address range.
1377 for (current = entry;
1378 (current != &map->header) && (current->start < end);
1379 current = current->next
1381 if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1384 vm_map_clip_end(map, current, end);
1388 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
1390 case MADV_SEQUENTIAL:
1391 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
1394 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
1397 current->eflags |= MAP_ENTRY_NOSYNC;
1400 current->eflags &= ~MAP_ENTRY_NOSYNC;
1403 current->eflags |= MAP_ENTRY_NOCOREDUMP;
1406 current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
1411 vm_map_simplify_entry(map, current);
1419 * madvise behaviors that are implemented in the underlying
1422 * Since we don't clip the vm_map_entry, we have to clip
1423 * the vm_object pindex and count.
1425 for (current = entry;
1426 (current != &map->header) && (current->start < end);
1427 current = current->next
1429 vm_offset_t useStart;
1431 if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1434 pindex = OFF_TO_IDX(current->offset);
1435 count = atop(current->end - current->start);
1436 useStart = current->start;
1438 if (current->start < start) {
1439 pindex += atop(start - current->start);
1440 count -= atop(start - current->start);
1443 if (current->end > end)
1444 count -= atop(current->end - end);
1449 vm_object_madvise(current->object.vm_object,
1450 pindex, count, behav);
1451 if (behav == MADV_WILLNEED) {
1452 pmap_object_init_pt(
1455 current->object.vm_object,
1457 (count << PAGE_SHIFT),
1458 MAP_PREFAULT_MADVISE
1462 vm_map_unlock_read(map);
1471 * Sets the inheritance of the specified address
1472 * range in the target map. Inheritance
1473 * affects how the map will be shared with
1474 * child maps at the time of vm_map_fork.
1477 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
1478 vm_inherit_t new_inheritance)
1480 vm_map_entry_t entry;
1481 vm_map_entry_t temp_entry;
1483 switch (new_inheritance) {
1484 case VM_INHERIT_NONE:
1485 case VM_INHERIT_COPY:
1486 case VM_INHERIT_SHARE:
1489 return (KERN_INVALID_ARGUMENT);
1494 VM_MAP_RANGE_CHECK(map, start, end);
1496 if (vm_map_lookup_entry(map, start, &temp_entry)) {
1498 vm_map_clip_start(map, entry, start);
1500 entry = temp_entry->next;
1502 while ((entry != &map->header) && (entry->start < end)) {
1503 vm_map_clip_end(map, entry, end);
1505 entry->inheritance = new_inheritance;
1507 vm_map_simplify_entry(map, entry);
1509 entry = entry->next;
1513 return (KERN_SUCCESS);
1517 * Implement the semantics of mlock
1520 vm_map_user_pageable(map, start, real_end, new_pageable)
1523 vm_offset_t real_end;
1524 boolean_t new_pageable;
1526 vm_map_entry_t entry;
1527 vm_map_entry_t start_entry;
1529 int rv = KERN_SUCCESS;
1532 VM_MAP_RANGE_CHECK(map, start, real_end);
1535 start_entry = vm_map_clip_range(map, start, end, MAP_CLIP_NO_HOLES);
1536 if (start_entry == NULL) {
1538 return (KERN_INVALID_ADDRESS);
1541 if (new_pageable == 0) {
1542 entry = start_entry;
1543 while ((entry != &map->header) && (entry->start < end)) {
1544 vm_offset_t save_start;
1545 vm_offset_t save_end;
1548 * Already user wired or hard wired (trivial cases)
1550 if (entry->eflags & MAP_ENTRY_USER_WIRED) {
1551 entry = entry->next;
1554 if (entry->wired_count != 0) {
1555 entry->wired_count++;
1556 entry->eflags |= MAP_ENTRY_USER_WIRED;
1557 entry = entry->next;
1562 * A new wiring requires instantiation of appropriate
1563 * management structures and the faulting in of the
1566 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1567 int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY;
1568 if (copyflag && ((entry->protection & VM_PROT_WRITE) != 0)) {
1570 vm_object_shadow(&entry->object.vm_object,
1572 atop(entry->end - entry->start));
1573 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
1575 } else if (entry->object.vm_object == NULL &&
1578 entry->object.vm_object =
1579 vm_object_allocate(OBJT_DEFAULT,
1580 atop(entry->end - entry->start));
1581 entry->offset = (vm_offset_t) 0;
1585 entry->wired_count++;
1586 entry->eflags |= MAP_ENTRY_USER_WIRED;
1589 * Now fault in the area. The map lock needs to be
1590 * manipulated to avoid deadlocks. The in-transition
1591 * flag protects the entries.
1593 save_start = entry->start;
1594 save_end = entry->end;
1597 rv = vm_fault_user_wire(map, save_start, save_end);
1600 CLIP_CHECK_BACK(entry, save_start);
1602 KASSERT(entry->wired_count == 1, ("bad wired_count on entry"));
1603 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
1604 entry->wired_count = 0;
1605 if (entry->end == save_end)
1607 entry = entry->next;
1608 KASSERT(entry != &map->header, ("bad entry clip during backout"));
1610 end = save_start; /* unwire the rest */
1614 * note that even though the entry might have been
1615 * clipped, the USER_WIRED flag we set prevents
1616 * duplication so we do not have to do a
1619 entry = entry->next;
1623 * If we failed fall through to the unwiring section to
1624 * unwire what we had wired so far. 'end' has already
1631 * start_entry might have been clipped if we unlocked the
1632 * map and blocked. No matter how clipped it has gotten
1633 * there should be a fragment that is on our start boundary.
1635 CLIP_CHECK_BACK(start_entry, start);
1639 * Deal with the unwiring case.
1643 * This is the unwiring case. We must first ensure that the
1644 * range to be unwired is really wired down. We know there
1647 entry = start_entry;
1648 while ((entry != &map->header) && (entry->start < end)) {
1649 if ((entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
1650 rv = KERN_INVALID_ARGUMENT;
1653 KASSERT(entry->wired_count != 0, ("wired count was 0 with USER_WIRED set! %p", entry));
1654 entry = entry->next;
1658 * Now decrement the wiring count for each region. If a region
1659 * becomes completely unwired, unwire its physical pages and
1662 while ((entry != &map->header) && (entry->start < end)) {
1663 KASSERT(entry->eflags & MAP_ENTRY_USER_WIRED, ("expected USER_WIRED on entry %p", entry));
1664 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
1665 entry->wired_count--;
1666 if (entry->wired_count == 0)
1667 vm_fault_unwire(map, entry->start, entry->end);
1668 entry = entry->next;
1672 vm_map_unclip_range(map, start_entry, start, real_end,
1682 * Sets the pageability of the specified address
1683 * range in the target map. Regions specified
1684 * as not pageable require locked-down physical
1685 * memory and physical page maps.
1687 * The map must not be locked, but a reference
1688 * must remain to the map throughout the call.
1691 vm_map_pageable(map, start, real_end, new_pageable)
1694 vm_offset_t real_end;
1695 boolean_t new_pageable;
1697 vm_map_entry_t entry;
1698 vm_map_entry_t start_entry;
1700 int rv = KERN_SUCCESS;
1704 VM_MAP_RANGE_CHECK(map, start, real_end);
1707 start_entry = vm_map_clip_range(map, start, end, MAP_CLIP_NO_HOLES);
1708 if (start_entry == NULL) {
1710 return (KERN_INVALID_ADDRESS);
1712 if (new_pageable == 0) {
1716 * 1. Holding the write lock, we create any shadow or zero-fill
1717 * objects that need to be created. Then we clip each map
1718 * entry to the region to be wired and increment its wiring
1719 * count. We create objects before clipping the map entries
1720 * to avoid object proliferation.
1722 * 2. We downgrade to a read lock, and call vm_fault_wire to
1723 * fault in the pages for any newly wired area (wired_count is
1726 * Downgrading to a read lock for vm_fault_wire avoids a
1727 * possible deadlock with another process that may have faulted
1728 * on one of the pages to be wired (it would mark the page busy,
1729 * blocking us, then in turn block on the map lock that we
1730 * hold). Because of problems in the recursive lock package,
1731 * we cannot upgrade to a write lock in vm_map_lookup. Thus,
1732 * any actions that require the write lock must be done
1733 * beforehand. Because we keep the read lock on the map, the
1734 * copy-on-write status of the entries we modify here cannot
1738 entry = start_entry;
1739 while ((entry != &map->header) && (entry->start < end)) {
1741 * Trivial case if the entry is already wired
1743 if (entry->wired_count) {
1744 entry->wired_count++;
1745 entry = entry->next;
1750 * The entry is being newly wired, we have to setup
1751 * appropriate management structures. A shadow
1752 * object is required for a copy-on-write region,
1753 * or a normal object for a zero-fill region. We
1754 * do not have to do this for entries that point to sub
1755 * maps because we won't hold the lock on the sub map.
1757 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1758 int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY;
1760 ((entry->protection & VM_PROT_WRITE) != 0)) {
1762 vm_object_shadow(&entry->object.vm_object,
1764 atop(entry->end - entry->start));
1765 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
1766 } else if (entry->object.vm_object == NULL &&
1768 entry->object.vm_object =
1769 vm_object_allocate(OBJT_DEFAULT,
1770 atop(entry->end - entry->start));
1771 entry->offset = (vm_offset_t) 0;
1775 entry->wired_count++;
1776 entry = entry->next;
1784 * HACK HACK HACK HACK
1786 * Unlock the map to avoid deadlocks. The in-transit flag
1787 * protects us from most changes but note that
1788 * clipping may still occur. To prevent clipping from
1789 * occuring after the unlock, except for when we are
1790 * blocking in vm_fault_wire, we must run at splvm().
1791 * Otherwise our accesses to entry->start and entry->end
1792 * could be corrupted. We have to set splvm() prior to
1793 * unlocking so start_entry does not change out from
1794 * under us at the very beginning of the loop.
1796 * HACK HACK HACK HACK
1802 entry = start_entry;
1803 while (entry != &map->header && entry->start < end) {
1805 * If vm_fault_wire fails for any page we need to undo
1806 * what has been done. We decrement the wiring count
1807 * for those pages which have not yet been wired (now)
1808 * and unwire those that have (later).
1810 vm_offset_t save_start = entry->start;
1811 vm_offset_t save_end = entry->end;
1813 if (entry->wired_count == 1)
1814 rv = vm_fault_wire(map, entry->start, entry->end);
1816 CLIP_CHECK_BACK(entry, save_start);
1818 KASSERT(entry->wired_count == 1, ("wired_count changed unexpectedly"));
1819 entry->wired_count = 0;
1820 if (entry->end == save_end)
1822 entry = entry->next;
1823 KASSERT(entry != &map->header, ("bad entry clip during backout"));
1828 CLIP_CHECK_FWD(entry, save_end);
1829 entry = entry->next;
1834 * relock. start_entry is still IN_TRANSITION and must
1835 * still exist, but may have been clipped (handled just
1841 * If a failure occured undo everything by falling through
1842 * to the unwiring code. 'end' has already been adjusted
1849 * start_entry might have been clipped if we unlocked the
1850 * map and blocked. No matter how clipped it has gotten
1851 * there should be a fragment that is on our start boundary.
1853 CLIP_CHECK_BACK(start_entry, start);
1858 * This is the unwiring case. We must first ensure that the
1859 * range to be unwired is really wired down. We know there
1862 entry = start_entry;
1863 while ((entry != &map->header) && (entry->start < end)) {
1864 if (entry->wired_count == 0) {
1865 rv = KERN_INVALID_ARGUMENT;
1868 entry = entry->next;
1872 * Now decrement the wiring count for each region. If a region
1873 * becomes completely unwired, unwire its physical pages and
1876 entry = start_entry;
1877 while ((entry != &map->header) && (entry->start < end)) {
1878 entry->wired_count--;
1879 if (entry->wired_count == 0)
1880 vm_fault_unwire(map, entry->start, entry->end);
1881 entry = entry->next;
1885 vm_map_unclip_range(map, start_entry, start, real_end,
1895 * Push any dirty cached pages in the address range to their pager.
1896 * If syncio is TRUE, dirty pages are written synchronously.
1897 * If invalidate is TRUE, any cached pages are freed as well.
1899 * Returns an error if any part of the specified range is not mapped.
1902 vm_map_clean(map, start, end, syncio, invalidate)
1907 boolean_t invalidate;
1909 vm_map_entry_t current;
1910 vm_map_entry_t entry;
1913 vm_ooffset_t offset;
1915 vm_map_lock_read(map);
1916 VM_MAP_RANGE_CHECK(map, start, end);
1917 if (!vm_map_lookup_entry(map, start, &entry)) {
1918 vm_map_unlock_read(map);
1919 return (KERN_INVALID_ADDRESS);
1922 * Make a first pass to check for holes.
1924 for (current = entry; current->start < end; current = current->next) {
1925 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1926 vm_map_unlock_read(map);
1927 return (KERN_INVALID_ARGUMENT);
1929 if (end > current->end &&
1930 (current->next == &map->header ||
1931 current->end != current->next->start)) {
1932 vm_map_unlock_read(map);
1933 return (KERN_INVALID_ADDRESS);
1938 pmap_remove(vm_map_pmap(map), start, end);
1940 * Make a second pass, cleaning/uncaching pages from the indicated
1943 for (current = entry; current->start < end; current = current->next) {
1944 offset = current->offset + (start - current->start);
1945 size = (end <= current->end ? end : current->end) - start;
1946 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1948 vm_map_entry_t tentry;
1951 smap = current->object.sub_map;
1952 vm_map_lock_read(smap);
1953 (void) vm_map_lookup_entry(smap, offset, &tentry);
1954 tsize = tentry->end - offset;
1957 object = tentry->object.vm_object;
1958 offset = tentry->offset + (offset - tentry->start);
1959 vm_map_unlock_read(smap);
1961 object = current->object.vm_object;
1964 * Note that there is absolutely no sense in writing out
1965 * anonymous objects, so we track down the vnode object
1967 * We invalidate (remove) all pages from the address space
1968 * anyway, for semantic correctness.
1970 * note: certain anonymous maps, such as MAP_NOSYNC maps,
1971 * may start out with a NULL object.
1973 while (object && object->backing_object) {
1974 object = object->backing_object;
1975 offset += object->backing_object_offset;
1976 if (object->size < OFF_TO_IDX( offset + size))
1977 size = IDX_TO_OFF(object->size) - offset;
1979 if (object && (object->type == OBJT_VNODE) &&
1980 (current->protection & VM_PROT_WRITE)) {
1982 * Flush pages if writing is allowed, invalidate them
1983 * if invalidation requested. Pages undergoing I/O
1984 * will be ignored by vm_object_page_remove().
1986 * We cannot lock the vnode and then wait for paging
1987 * to complete without deadlocking against vm_fault.
1988 * Instead we simply call vm_object_page_remove() and
1989 * allow it to block internally on a page-by-page
1990 * basis when it encounters pages undergoing async
1995 vm_object_reference(object);
1996 vn_lock(object->handle, LK_EXCLUSIVE | LK_RETRY, curproc);
1997 flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
1998 flags |= invalidate ? OBJPC_INVAL : 0;
1999 vm_object_page_clean(object,
2001 OFF_TO_IDX(offset + size + PAGE_MASK),
2003 VOP_UNLOCK(object->handle, 0, curproc);
2004 vm_object_deallocate(object);
2006 if (object && invalidate &&
2007 ((object->type == OBJT_VNODE) ||
2008 (object->type == OBJT_DEVICE))) {
2009 vm_object_reference(object);
2010 vm_object_page_remove(object,
2012 OFF_TO_IDX(offset + size + PAGE_MASK),
2014 vm_object_deallocate(object);
2019 vm_map_unlock_read(map);
2020 return (KERN_SUCCESS);
2024 * vm_map_entry_unwire: [ internal use only ]
2026 * Make the region specified by this entry pageable.
2028 * The map in question should be locked.
2029 * [This is the reason for this routine's existence.]
2032 vm_map_entry_unwire(map, entry)
2034 vm_map_entry_t entry;
2036 vm_fault_unwire(map, entry->start, entry->end);
2037 entry->wired_count = 0;
2041 * vm_map_entry_delete: [ internal use only ]
2043 * Deallocate the given entry from the target map.
2046 vm_map_entry_delete(map, entry)
2048 vm_map_entry_t entry;
2050 vm_map_entry_unlink(map, entry);
2051 map->size -= entry->end - entry->start;
2053 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
2054 vm_object_deallocate(entry->object.vm_object);
2057 vm_map_entry_dispose(map, entry);
2061 * vm_map_delete: [ internal use only ]
2063 * Deallocates the given address range from the target
2067 vm_map_delete(map, start, end)
2073 vm_map_entry_t entry;
2074 vm_map_entry_t first_entry;
2077 * Find the start of the region, and clip it
2081 if (!vm_map_lookup_entry(map, start, &first_entry))
2082 entry = first_entry->next;
2084 entry = first_entry;
2085 vm_map_clip_start(map, entry, start);
2087 * Fix the lookup hint now, rather than each time though the
2090 SAVE_HINT(map, entry->prev);
2094 * Save the free space hint
2097 if (entry == &map->header) {
2098 map->first_free = &map->header;
2099 } else if (map->first_free->start >= start) {
2100 map->first_free = entry->prev;
2104 * Step through all entries in this region
2107 while ((entry != &map->header) && (entry->start < end)) {
2108 vm_map_entry_t next;
2110 vm_pindex_t offidxstart, offidxend, count;
2113 * If we hit an in-transition entry we have to sleep and
2114 * retry. It's easier (and not really slower) to just retry
2115 * since this case occurs so rarely and the hint is already
2116 * pointing at the right place. We have to reset the
2117 * start offset so as not to accidently delete an entry
2118 * another process just created in vacated space.
2120 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
2121 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2122 start = entry->start;
2123 ++cnt.v_intrans_coll;
2124 ++cnt.v_intrans_wait;
2125 vm_map_transition_wait(map);
2128 vm_map_clip_end(map, entry, end);
2134 offidxstart = OFF_TO_IDX(entry->offset);
2135 count = OFF_TO_IDX(e - s);
2136 object = entry->object.vm_object;
2139 * Unwire before removing addresses from the pmap; otherwise,
2140 * unwiring will put the entries back in the pmap.
2142 if (entry->wired_count != 0) {
2143 vm_map_entry_unwire(map, entry);
2146 offidxend = offidxstart + count;
2148 if ((object == kernel_object) || (object == kmem_object)) {
2149 vm_object_page_remove(object, offidxstart, offidxend, FALSE);
2151 pmap_remove(map->pmap, s, e);
2152 if (object != NULL &&
2153 object->ref_count != 1 &&
2154 (object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING &&
2155 (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2156 vm_object_collapse(object);
2157 vm_object_page_remove(object, offidxstart, offidxend, FALSE);
2158 if (object->type == OBJT_SWAP) {
2159 swap_pager_freespace(object, offidxstart, count);
2161 if (offidxend >= object->size &&
2162 offidxstart < object->size) {
2163 object->size = offidxstart;
2169 * Delete the entry (which may delete the object) only after
2170 * removing all pmap entries pointing to its pages.
2171 * (Otherwise, its page frames may be reallocated, and any
2172 * modify bits will be set in the wrong object!)
2174 vm_map_entry_delete(map, entry);
2177 return (KERN_SUCCESS);
2183 * Remove the given address range from the target map.
2184 * This is the exported form of vm_map_delete.
2187 vm_map_remove(map, start, end)
2194 if (map == kmem_map || map == mb_map)
2198 VM_MAP_RANGE_CHECK(map, start, end);
2199 result = vm_map_delete(map, start, end);
2202 if (map == kmem_map || map == mb_map)
2209 * vm_map_check_protection:
2211 * Assert that the target map allows the specified
2212 * privilege on the entire address region given.
2213 * The entire region must be allocated.
2216 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
2217 vm_prot_t protection)
2219 vm_map_entry_t entry;
2220 vm_map_entry_t tmp_entry;
2222 if (!vm_map_lookup_entry(map, start, &tmp_entry)) {
2227 while (start < end) {
2228 if (entry == &map->header) {
2235 if (start < entry->start) {
2239 * Check protection associated with entry.
2242 if ((entry->protection & protection) != protection) {
2245 /* go to next entry */
2248 entry = entry->next;
2254 * Split the pages in a map entry into a new object. This affords
2255 * easier removal of unused pages, and keeps object inheritance from
2256 * being a negative impact on memory usage.
2260 vm_map_entry_t entry;
2263 vm_object_t orig_object, new_object, source;
2265 vm_pindex_t offidxstart, offidxend, idx;
2267 vm_ooffset_t offset;
2269 orig_object = entry->object.vm_object;
2270 if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
2272 if (orig_object->ref_count <= 1)
2275 offset = entry->offset;
2279 offidxstart = OFF_TO_IDX(offset);
2280 offidxend = offidxstart + OFF_TO_IDX(e - s);
2281 size = offidxend - offidxstart;
2283 new_object = vm_pager_allocate(orig_object->type,
2284 NULL, IDX_TO_OFF(size), VM_PROT_ALL, 0LL);
2285 if (new_object == NULL)
2288 source = orig_object->backing_object;
2289 if (source != NULL) {
2290 vm_object_reference(source); /* Referenced by new_object */
2291 LIST_INSERT_HEAD(&source->shadow_head,
2292 new_object, shadow_list);
2293 vm_object_clear_flag(source, OBJ_ONEMAPPING);
2294 new_object->backing_object_offset =
2295 orig_object->backing_object_offset + IDX_TO_OFF(offidxstart);
2296 new_object->backing_object = source;
2297 source->shadow_count++;
2298 source->generation++;
2301 for (idx = 0; idx < size; idx++) {
2305 m = vm_page_lookup(orig_object, offidxstart + idx);
2310 * We must wait for pending I/O to complete before we can
2313 * We do not have to VM_PROT_NONE the page as mappings should
2314 * not be changed by this operation.
2316 if (vm_page_sleep_busy(m, TRUE, "spltwt"))
2320 vm_page_rename(m, new_object, idx);
2321 /* page automatically made dirty by rename and cache handled */
2325 if (orig_object->type == OBJT_SWAP) {
2326 vm_object_pip_add(orig_object, 1);
2328 * copy orig_object pages into new_object
2329 * and destroy unneeded pages in
2332 swap_pager_copy(orig_object, new_object, offidxstart, 0);
2333 vm_object_pip_wakeup(orig_object);
2336 for (idx = 0; idx < size; idx++) {
2337 m = vm_page_lookup(new_object, idx);
2343 entry->object.vm_object = new_object;
2344 entry->offset = 0LL;
2345 vm_object_deallocate(orig_object);
2349 * vm_map_copy_entry:
2351 * Copies the contents of the source entry to the destination
2352 * entry. The entries *must* be aligned properly.
2355 vm_map_copy_entry(src_map, dst_map, src_entry, dst_entry)
2356 vm_map_t src_map, dst_map;
2357 vm_map_entry_t src_entry, dst_entry;
2359 vm_object_t src_object;
2361 if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
2364 if (src_entry->wired_count == 0) {
2367 * If the source entry is marked needs_copy, it is already
2370 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
2371 pmap_protect(src_map->pmap,
2374 src_entry->protection & ~VM_PROT_WRITE);
2378 * Make a copy of the object.
2380 if ((src_object = src_entry->object.vm_object) != NULL) {
2382 if ((src_object->handle == NULL) &&
2383 (src_object->type == OBJT_DEFAULT ||
2384 src_object->type == OBJT_SWAP)) {
2385 vm_object_collapse(src_object);
2386 if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
2387 vm_map_split(src_entry);
2388 src_object = src_entry->object.vm_object;
2392 vm_object_reference(src_object);
2393 vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
2394 dst_entry->object.vm_object = src_object;
2395 src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2396 dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2397 dst_entry->offset = src_entry->offset;
2399 dst_entry->object.vm_object = NULL;
2400 dst_entry->offset = 0;
2403 pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
2404 dst_entry->end - dst_entry->start, src_entry->start);
2407 * Of course, wired down pages can't be set copy-on-write.
2408 * Cause wired pages to be copied into the new map by
2409 * simulating faults (the new pages are pageable)
2411 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry);
2417 * Create a new process vmspace structure and vm_map
2418 * based on those of an existing process. The new map
2419 * is based on the old map, according to the inheritance
2420 * values on the regions in that map.
2422 * The source map must not be locked.
2426 struct vmspace *vm1;
2428 struct vmspace *vm2;
2429 vm_map_t old_map = &vm1->vm_map;
2431 vm_map_entry_t old_entry;
2432 vm_map_entry_t new_entry;
2435 vm_map_lock(old_map);
2436 old_map->infork = 1;
2438 vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
2439 bcopy(&vm1->vm_startcopy, &vm2->vm_startcopy,
2440 (caddr_t) (vm1 + 1) - (caddr_t) &vm1->vm_startcopy);
2441 new_map = &vm2->vm_map; /* XXX */
2442 new_map->timestamp = 1;
2444 old_entry = old_map->header.next;
2446 while (old_entry != &old_map->header) {
2447 if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2448 panic("vm_map_fork: encountered a submap");
2450 switch (old_entry->inheritance) {
2451 case VM_INHERIT_NONE:
2454 case VM_INHERIT_SHARE:
2456 * Clone the entry, creating the shared object if necessary.
2458 object = old_entry->object.vm_object;
2459 if (object == NULL) {
2460 object = vm_object_allocate(OBJT_DEFAULT,
2461 atop(old_entry->end - old_entry->start));
2462 old_entry->object.vm_object = object;
2463 old_entry->offset = (vm_offset_t) 0;
2467 * Add the reference before calling vm_object_shadow
2468 * to insure that a shadow object is created.
2470 vm_object_reference(object);
2471 if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
2472 vm_object_shadow(&old_entry->object.vm_object,
2474 atop(old_entry->end - old_entry->start));
2475 old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
2476 /* Transfer the second reference too. */
2477 vm_object_reference(
2478 old_entry->object.vm_object);
2479 vm_object_deallocate(object);
2480 object = old_entry->object.vm_object;
2482 vm_object_clear_flag(object, OBJ_ONEMAPPING);
2485 * Clone the entry, referencing the shared object.
2487 new_entry = vm_map_entry_create(new_map);
2488 *new_entry = *old_entry;
2489 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2490 new_entry->wired_count = 0;
2493 * Insert the entry into the new map -- we know we're
2494 * inserting at the end of the new map.
2497 vm_map_entry_link(new_map, new_map->header.prev,
2501 * Update the physical map
2504 pmap_copy(new_map->pmap, old_map->pmap,
2506 (old_entry->end - old_entry->start),
2510 case VM_INHERIT_COPY:
2512 * Clone the entry and link into the map.
2514 new_entry = vm_map_entry_create(new_map);
2515 *new_entry = *old_entry;
2516 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2517 new_entry->wired_count = 0;
2518 new_entry->object.vm_object = NULL;
2519 vm_map_entry_link(new_map, new_map->header.prev,
2521 vm_map_copy_entry(old_map, new_map, old_entry,
2525 old_entry = old_entry->next;
2528 new_map->size = old_map->size;
2529 old_map->infork = 0;
2530 vm_map_unlock(old_map);
2536 vm_map_stack (vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
2537 vm_prot_t prot, vm_prot_t max, int cow)
2539 vm_map_entry_t prev_entry;
2540 vm_map_entry_t new_stack_entry;
2541 vm_size_t init_ssize;
2544 if (VM_MIN_ADDRESS > 0 && addrbos < VM_MIN_ADDRESS)
2545 return (KERN_NO_SPACE);
2547 if (max_ssize < sgrowsiz)
2548 init_ssize = max_ssize;
2550 init_ssize = sgrowsiz;
2554 /* If addr is already mapped, no go */
2555 if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
2557 return (KERN_NO_SPACE);
2560 /* If we would blow our VMEM resource limit, no go */
2561 if (map->size + init_ssize >
2562 curproc->p_rlimit[RLIMIT_VMEM].rlim_cur) {
2564 return (KERN_NO_SPACE);
2567 /* If we can't accomodate max_ssize in the current mapping,
2568 * no go. However, we need to be aware that subsequent user
2569 * mappings might map into the space we have reserved for
2570 * stack, and currently this space is not protected.
2572 * Hopefully we will at least detect this condition
2573 * when we try to grow the stack.
2575 if ((prev_entry->next != &map->header) &&
2576 (prev_entry->next->start < addrbos + max_ssize)) {
2578 return (KERN_NO_SPACE);
2581 /* We initially map a stack of only init_ssize. We will
2582 * grow as needed later. Since this is to be a grow
2583 * down stack, we map at the top of the range.
2585 * Note: we would normally expect prot and max to be
2586 * VM_PROT_ALL, and cow to be 0. Possibly we should
2587 * eliminate these as input parameters, and just
2588 * pass these values here in the insert call.
2590 rv = vm_map_insert(map, NULL, 0, addrbos + max_ssize - init_ssize,
2591 addrbos + max_ssize, prot, max, cow);
2593 /* Now set the avail_ssize amount */
2594 if (rv == KERN_SUCCESS){
2595 if (prev_entry != &map->header)
2596 vm_map_clip_end(map, prev_entry, addrbos + max_ssize - init_ssize);
2597 new_stack_entry = prev_entry->next;
2598 if (new_stack_entry->end != addrbos + max_ssize ||
2599 new_stack_entry->start != addrbos + max_ssize - init_ssize)
2600 panic ("Bad entry start/end for new stack entry");
2602 new_stack_entry->avail_ssize = max_ssize - init_ssize;
2609 /* Attempts to grow a vm stack entry. Returns KERN_SUCCESS if the
2610 * desired address is already mapped, or if we successfully grow
2611 * the stack. Also returns KERN_SUCCESS if addr is outside the
2612 * stack range (this is strange, but preserves compatibility with
2613 * the grow function in vm_machdep.c).
2616 vm_map_growstack (struct proc *p, vm_offset_t addr)
2618 vm_map_entry_t prev_entry;
2619 vm_map_entry_t stack_entry;
2620 vm_map_entry_t new_stack_entry;
2621 struct vmspace *vm = p->p_vmspace;
2622 vm_map_t map = &vm->vm_map;
2625 int rv = KERN_SUCCESS;
2627 int use_read_lock = 1;
2631 vm_map_lock_read(map);
2635 /* If addr is already in the entry range, no need to grow.*/
2636 if (vm_map_lookup_entry(map, addr, &prev_entry))
2639 if ((stack_entry = prev_entry->next) == &map->header)
2641 if (prev_entry == &map->header)
2642 end = stack_entry->start - stack_entry->avail_ssize;
2644 end = prev_entry->end;
2646 /* This next test mimics the old grow function in vm_machdep.c.
2647 * It really doesn't quite make sense, but we do it anyway
2648 * for compatibility.
2650 * If not growable stack, return success. This signals the
2651 * caller to proceed as he would normally with normal vm.
2653 if (stack_entry->avail_ssize < 1 ||
2654 addr >= stack_entry->start ||
2655 addr < stack_entry->start - stack_entry->avail_ssize) {
2659 /* Find the minimum grow amount */
2660 grow_amount = roundup (stack_entry->start - addr, PAGE_SIZE);
2661 if (grow_amount > stack_entry->avail_ssize) {
2666 /* If there is no longer enough space between the entries
2667 * nogo, and adjust the available space. Note: this
2668 * should only happen if the user has mapped into the
2669 * stack area after the stack was created, and is
2670 * probably an error.
2672 * This also effectively destroys any guard page the user
2673 * might have intended by limiting the stack size.
2675 if (grow_amount > stack_entry->start - end) {
2676 if (use_read_lock && vm_map_lock_upgrade(map)) {
2681 stack_entry->avail_ssize = stack_entry->start - end;
2686 is_procstack = addr >= (vm_offset_t)vm->vm_maxsaddr;
2688 /* If this is the main process stack, see if we're over the
2691 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount >
2692 p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
2697 /* Round up the grow amount modulo SGROWSIZ */
2698 grow_amount = roundup (grow_amount, sgrowsiz);
2699 if (grow_amount > stack_entry->avail_ssize) {
2700 grow_amount = stack_entry->avail_ssize;
2702 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount >
2703 p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
2704 grow_amount = p->p_rlimit[RLIMIT_STACK].rlim_cur -
2708 /* If we would blow our VMEM resource limit, no go */
2709 if (map->size + grow_amount >
2710 curproc->p_rlimit[RLIMIT_VMEM].rlim_cur) {
2715 if (use_read_lock && vm_map_lock_upgrade(map)) {
2721 /* Get the preliminary new entry start value */
2722 addr = stack_entry->start - grow_amount;
2724 /* If this puts us into the previous entry, cut back our growth
2725 * to the available space. Also, see the note above.
2728 stack_entry->avail_ssize = stack_entry->start - end;
2732 rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start,
2737 /* Adjust the available stack space by the amount we grew. */
2738 if (rv == KERN_SUCCESS) {
2739 if (prev_entry != &map->header)
2740 vm_map_clip_end(map, prev_entry, addr);
2741 new_stack_entry = prev_entry->next;
2742 if (new_stack_entry->end != stack_entry->start ||
2743 new_stack_entry->start != addr)
2744 panic ("Bad stack grow start/end in new stack entry");
2746 new_stack_entry->avail_ssize = stack_entry->avail_ssize -
2747 (new_stack_entry->end -
2748 new_stack_entry->start);
2750 vm->vm_ssize += btoc(new_stack_entry->end -
2751 new_stack_entry->start);
2757 vm_map_unlock_read(map);
2764 * Unshare the specified VM space for exec. If other processes are
2765 * mapped to it, then create a new one. The new vmspace is null.
2769 vmspace_exec(struct proc *p) {
2770 struct vmspace *oldvmspace = p->p_vmspace;
2771 struct vmspace *newvmspace;
2772 vm_map_t map = &p->p_vmspace->vm_map;
2774 newvmspace = vmspace_alloc(map->min_offset, map->max_offset);
2775 bcopy(&oldvmspace->vm_startcopy, &newvmspace->vm_startcopy,
2776 (caddr_t) (newvmspace + 1) - (caddr_t) &newvmspace->vm_startcopy);
2778 * This code is written like this for prototype purposes. The
2779 * goal is to avoid running down the vmspace here, but let the
2780 * other process's that are still using the vmspace to finally
2781 * run it down. Even though there is little or no chance of blocking
2782 * here, it is a good idea to keep this form for future mods.
2784 vmspace_free(oldvmspace);
2785 p->p_vmspace = newvmspace;
2786 pmap_pinit2(vmspace_pmap(newvmspace));
2792 * Unshare the specified VM space for forcing COW. This
2793 * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
2797 vmspace_unshare(struct proc *p) {
2798 struct vmspace *oldvmspace = p->p_vmspace;
2799 struct vmspace *newvmspace;
2801 if (oldvmspace->vm_refcnt == 1)
2803 newvmspace = vmspace_fork(oldvmspace);
2804 vmspace_free(oldvmspace);
2805 p->p_vmspace = newvmspace;
2806 pmap_pinit2(vmspace_pmap(newvmspace));
2815 * Finds the VM object, offset, and
2816 * protection for a given virtual address in the
2817 * specified map, assuming a page fault of the
2820 * Leaves the map in question locked for read; return
2821 * values are guaranteed until a vm_map_lookup_done
2822 * call is performed. Note that the map argument
2823 * is in/out; the returned map must be used in
2824 * the call to vm_map_lookup_done.
2826 * A handle (out_entry) is returned for use in
2827 * vm_map_lookup_done, to make that fast.
2829 * If a lookup is requested with "write protection"
2830 * specified, the map may be changed to perform virtual
2831 * copying operations, although the data referenced will
2835 vm_map_lookup(vm_map_t *var_map, /* IN/OUT */
2837 vm_prot_t fault_typea,
2838 vm_map_entry_t *out_entry, /* OUT */
2839 vm_object_t *object, /* OUT */
2840 vm_pindex_t *pindex, /* OUT */
2841 vm_prot_t *out_prot, /* OUT */
2842 boolean_t *wired) /* OUT */
2844 vm_map_entry_t entry;
2845 vm_map_t map = *var_map;
2847 vm_prot_t fault_type = fault_typea;
2848 int use_read_lock = 1;
2849 int rv = KERN_SUCCESS;
2853 vm_map_lock_read(map);
2858 * If the map has an interesting hint, try it before calling full
2859 * blown lookup routine.
2864 if ((entry == &map->header) ||
2865 (vaddr < entry->start) || (vaddr >= entry->end)) {
2866 vm_map_entry_t tmp_entry;
2869 * Entry was either not a valid hint, or the vaddr was not
2870 * contained in the entry, so do a full lookup.
2872 if (!vm_map_lookup_entry(map, vaddr, &tmp_entry)) {
2873 rv = KERN_INVALID_ADDRESS;
2885 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
2886 vm_map_t old_map = map;
2888 *var_map = map = entry->object.sub_map;
2890 vm_map_unlock_read(old_map);
2892 vm_map_unlock(old_map);
2898 * Check whether this task is allowed to have this page.
2899 * Note the special case for MAP_ENTRY_COW
2900 * pages with an override. This is to implement a forced
2901 * COW for debuggers.
2904 if (fault_type & VM_PROT_OVERRIDE_WRITE)
2905 prot = entry->max_protection;
2907 prot = entry->protection;
2909 fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
2910 if ((fault_type & prot) != fault_type) {
2911 rv = KERN_PROTECTION_FAILURE;
2915 if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
2916 (entry->eflags & MAP_ENTRY_COW) &&
2917 (fault_type & VM_PROT_WRITE) &&
2918 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
2919 rv = KERN_PROTECTION_FAILURE;
2924 * If this page is not pageable, we have to get it for all possible
2928 *wired = (entry->wired_count != 0);
2930 prot = fault_type = entry->protection;
2933 * If the entry was copy-on-write, we either ...
2936 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
2938 * If we want to write the page, we may as well handle that
2939 * now since we've got the map locked.
2941 * If we don't need to write the page, we just demote the
2942 * permissions allowed.
2945 if (fault_type & VM_PROT_WRITE) {
2947 * Make a new object, and place it in the object
2948 * chain. Note that no new references have appeared
2949 * -- one just moved from the map to the new
2953 if (use_read_lock && vm_map_lock_upgrade(map)) {
2960 &entry->object.vm_object,
2962 atop(entry->end - entry->start));
2964 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
2967 * We're attempting to read a copy-on-write page --
2968 * don't allow writes.
2971 prot &= ~VM_PROT_WRITE;
2976 * Create an object if necessary.
2978 if (entry->object.vm_object == NULL &&
2980 if (use_read_lock && vm_map_lock_upgrade(map)) {
2985 entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
2986 atop(entry->end - entry->start));
2991 * Return the object/offset from this entry. If the entry was
2992 * copy-on-write or empty, it has been fixed up.
2995 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
2996 *object = entry->object.vm_object;
2999 * Return whether this is the only map sharing this data. On
3000 * success we return with a read lock held on the map. On failure
3001 * we return with the map unlocked.
3005 if (rv == KERN_SUCCESS) {
3006 if (use_read_lock == 0)
3007 vm_map_lock_downgrade(map);
3008 } else if (use_read_lock) {
3009 vm_map_unlock_read(map);
3017 * vm_map_lookup_done:
3019 * Releases locks acquired by a vm_map_lookup
3020 * (according to the handle returned by that lookup).
3024 vm_map_lookup_done(map, entry)
3026 vm_map_entry_t entry;
3029 * Unlock the main-level map
3032 vm_map_unlock_read(map);
3036 * Implement uiomove with VM operations. This handles (and collateral changes)
3037 * support every combination of source object modification, and COW type
3041 vm_uiomove(mapa, srcobject, cp, cnta, uaddra, npages)
3043 vm_object_t srcobject;
3050 vm_object_t first_object, oldobject, object;
3051 vm_map_entry_t entry;
3055 vm_offset_t uaddr, start, end, tend;
3056 vm_pindex_t first_pindex, osize, oindex;
3069 if ((vm_map_lookup(&map, uaddr,
3070 VM_PROT_READ, &entry, &first_object,
3071 &first_pindex, &prot, &wired)) != KERN_SUCCESS) {
3075 vm_map_clip_start(map, entry, uaddr);
3078 tend = uaddr + tcnt;
3079 if (tend > entry->end) {
3080 tcnt = entry->end - uaddr;
3084 vm_map_clip_end(map, entry, tend);
3086 start = entry->start;
3091 oindex = OFF_TO_IDX(cp);
3094 for (idx = 0; idx < osize; idx++) {
3096 if ((m = vm_page_lookup(srcobject, oindex + idx)) == NULL) {
3097 vm_map_lookup_done(map, entry);
3101 * disallow busy or invalid pages, but allow
3102 * m->busy pages if they are entirely valid.
3104 if ((m->flags & PG_BUSY) ||
3105 ((m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL)) {
3106 vm_map_lookup_done(map, entry);
3113 * If we are changing an existing map entry, just redirect
3114 * the object, and change mappings.
3116 if ((first_object->type == OBJT_VNODE) &&
3117 ((oldobject = entry->object.vm_object) == first_object)) {
3119 if ((entry->offset != cp) || (oldobject != srcobject)) {
3121 * Remove old window into the file
3123 pmap_remove (map->pmap, uaddr, tend);
3126 * Force copy on write for mmaped regions
3128 vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
3131 * Point the object appropriately
3133 if (oldobject != srcobject) {
3136 * Set the object optimization hint flag
3138 vm_object_set_flag(srcobject, OBJ_OPT);
3139 vm_object_reference(srcobject);
3140 entry->object.vm_object = srcobject;
3143 vm_object_deallocate(oldobject);
3150 pmap_remove (map->pmap, uaddr, tend);
3153 } else if ((first_object->ref_count == 1) &&
3154 (first_object->size == osize) &&
3155 ((first_object->type == OBJT_DEFAULT) ||
3156 (first_object->type == OBJT_SWAP)) ) {
3158 oldobject = first_object->backing_object;
3160 if ((first_object->backing_object_offset != cp) ||
3161 (oldobject != srcobject)) {
3163 * Remove old window into the file
3165 pmap_remove (map->pmap, uaddr, tend);
3168 * Remove unneeded old pages
3170 vm_object_page_remove(first_object, 0, 0, 0);
3173 * Invalidate swap space
3175 if (first_object->type == OBJT_SWAP) {
3176 swap_pager_freespace(first_object,
3178 first_object->size);
3182 * Force copy on write for mmaped regions
3184 vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
3187 * Point the object appropriately
3189 if (oldobject != srcobject) {
3192 * Set the object optimization hint flag
3194 vm_object_set_flag(srcobject, OBJ_OPT);
3195 vm_object_reference(srcobject);
3199 first_object, shadow_list);
3200 oldobject->shadow_count--;
3201 /* XXX bump generation? */
3202 vm_object_deallocate(oldobject);
3205 LIST_INSERT_HEAD(&srcobject->shadow_head,
3206 first_object, shadow_list);
3207 srcobject->shadow_count++;
3208 /* XXX bump generation? */
3210 first_object->backing_object = srcobject;
3212 first_object->backing_object_offset = cp;
3215 pmap_remove (map->pmap, uaddr, tend);
3218 * Otherwise, we have to do a logical mmap.
3222 vm_object_set_flag(srcobject, OBJ_OPT);
3223 vm_object_reference(srcobject);
3225 pmap_remove (map->pmap, uaddr, tend);
3227 vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
3228 vm_map_lock_upgrade(map);
3230 if (entry == &map->header) {
3231 map->first_free = &map->header;
3232 } else if (map->first_free->start >= start) {
3233 map->first_free = entry->prev;
3236 SAVE_HINT(map, entry->prev);
3237 vm_map_entry_delete(map, entry);
3242 rv = vm_map_insert(map, object, ooffset, start, tend,
3243 VM_PROT_ALL, VM_PROT_ALL, MAP_COPY_ON_WRITE);
3245 if (rv != KERN_SUCCESS)
3246 panic("vm_uiomove: could not insert new entry: %d", rv);
3250 * Map the window directly, if it is already in memory
3252 pmap_object_init_pt(map->pmap, uaddr,
3253 srcobject, oindex, tcnt, 0);
3268 * Performs the copy_on_write operations necessary to allow the virtual copies
3269 * into user space to work. This has to be called for write(2) system calls
3270 * from other processes, file unlinking, and file size shrinkage.
3273 vm_freeze_copyopts(object, froma, toa)
3275 vm_pindex_t froma, toa;
3278 vm_object_t robject;
3281 if ((object == NULL) ||
3282 ((object->flags & OBJ_OPT) == 0))
3285 if (object->shadow_count > object->ref_count)
3286 panic("vm_freeze_copyopts: sc > rc");
3288 while((robject = LIST_FIRST(&object->shadow_head)) != NULL) {
3289 vm_pindex_t bo_pindex;
3290 vm_page_t m_in, m_out;
3292 bo_pindex = OFF_TO_IDX(robject->backing_object_offset);
3294 vm_object_reference(robject);
3296 vm_object_pip_wait(robject, "objfrz");
3298 if (robject->ref_count == 1) {
3299 vm_object_deallocate(robject);
3303 vm_object_pip_add(robject, 1);
3305 for (idx = 0; idx < robject->size; idx++) {
3307 m_out = vm_page_grab(robject, idx,
3308 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
3310 if (m_out->valid == 0) {
3311 m_in = vm_page_grab(object, bo_pindex + idx,
3312 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
3313 if (m_in->valid == 0) {
3314 rv = vm_pager_get_pages(object, &m_in, 1, 0);
3315 if (rv != VM_PAGER_OK) {
3316 printf("vm_freeze_copyopts: cannot read page from file: %lx\n", (long)m_in->pindex);
3319 vm_page_deactivate(m_in);
3322 vm_page_protect(m_in, VM_PROT_NONE);
3323 pmap_copy_page(VM_PAGE_TO_PHYS(m_in), VM_PAGE_TO_PHYS(m_out));
3324 m_out->valid = m_in->valid;
3325 vm_page_dirty(m_out);
3326 vm_page_activate(m_out);
3327 vm_page_wakeup(m_in);
3329 vm_page_wakeup(m_out);
3332 object->shadow_count--;
3333 object->ref_count--;
3334 LIST_REMOVE(robject, shadow_list);
3335 robject->backing_object = NULL;
3336 robject->backing_object_offset = 0;
3338 vm_object_pip_wakeup(robject);
3339 vm_object_deallocate(robject);
3342 vm_object_clear_flag(object, OBJ_OPT);
3345 #include "opt_ddb.h"
3347 #include <sys/kernel.h>
3349 #include <ddb/ddb.h>
3352 * vm_map_print: [ debug ]
3354 DB_SHOW_COMMAND(map, vm_map_print)
3357 /* XXX convert args. */
3358 vm_map_t map = (vm_map_t)addr;
3359 boolean_t full = have_addr;
3361 vm_map_entry_t entry;
3363 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
3365 (void *)map->pmap, map->nentries, map->timestamp);
3368 if (!full && db_indent)
3372 for (entry = map->header.next; entry != &map->header;
3373 entry = entry->next) {
3374 db_iprintf("map entry %p: start=%p, end=%p\n",
3375 (void *)entry, (void *)entry->start, (void *)entry->end);
3378 static char *inheritance_name[4] =
3379 {"share", "copy", "none", "donate_copy"};
3381 db_iprintf(" prot=%x/%x/%s",
3383 entry->max_protection,
3384 inheritance_name[(int)(unsigned char)entry->inheritance]);
3385 if (entry->wired_count != 0)
3386 db_printf(", wired");
3388 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3389 /* XXX no %qd in kernel. Truncate entry->offset. */
3390 db_printf(", share=%p, offset=0x%lx\n",
3391 (void *)entry->object.sub_map,
3392 (long)entry->offset);
3394 if ((entry->prev == &map->header) ||
3395 (entry->prev->object.sub_map !=
3396 entry->object.sub_map)) {
3398 vm_map_print((db_expr_t)(intptr_t)
3399 entry->object.sub_map,
3400 full, 0, (char *)0);
3404 /* XXX no %qd in kernel. Truncate entry->offset. */
3405 db_printf(", object=%p, offset=0x%lx",
3406 (void *)entry->object.vm_object,
3407 (long)entry->offset);
3408 if (entry->eflags & MAP_ENTRY_COW)
3409 db_printf(", copy (%s)",
3410 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
3414 if ((entry->prev == &map->header) ||
3415 (entry->prev->object.vm_object !=
3416 entry->object.vm_object)) {
3418 vm_object_print((db_expr_t)(intptr_t)
3419 entry->object.vm_object,
3420 full, 0, (char *)0);
3432 DB_SHOW_COMMAND(procvm, procvm)
3437 p = (struct proc *) addr;
3442 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
3443 (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
3444 (void *)vmspace_pmap(p->p_vmspace));
3446 vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);