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.6 2003/07/19 21:14:53 dillon Exp $
69 * Virtual memory mapping module.
72 #include <sys/param.h>
73 #include <sys/systm.h>
75 #include <sys/vmmeter.h>
77 #include <sys/vnode.h>
78 #include <sys/resourcevar.h>
81 #include <vm/vm_param.h>
84 #include <vm/vm_map.h>
85 #include <vm/vm_page.h>
86 #include <vm/vm_object.h>
87 #include <vm/vm_pager.h>
88 #include <vm/vm_kern.h>
89 #include <vm/vm_extern.h>
90 #include <vm/swap_pager.h>
91 #include <vm/vm_zone.h>
94 * Virtual memory maps provide for the mapping, protection,
95 * and sharing of virtual memory objects. In addition,
96 * this module provides for an efficient virtual copy of
97 * memory from one map to another.
99 * Synchronization is required prior to most operations.
101 * Maps consist of an ordered doubly-linked list of simple
102 * entries; a single hint is used to speed up lookups.
104 * Since portions of maps are specified by start/end addresses,
105 * which may not align with existing map entries, all
106 * routines merely "clip" entries to these start/end values.
107 * [That is, an entry is split into two, bordering at a
108 * start or end value.] Note that these clippings may not
109 * always be necessary (as the two resulting entries are then
110 * not changed); however, the clipping is done for convenience.
112 * As mentioned above, virtual copy operations are performed
113 * by copying VM object references from one map to
114 * another, and then marking both regions as copy-on-write.
120 * Initialize the vm_map module. Must be called before
121 * any other vm_map routines.
123 * Map and entry structures are allocated from the general
124 * purpose memory pool with some exceptions:
126 * - The kernel map and kmem submap are allocated statically.
127 * - Kernel map entries are allocated out of a static pool.
129 * These restrictions are necessary since malloc() uses the
130 * maps and requires map entries.
133 static struct vm_zone kmapentzone_store, mapentzone_store, mapzone_store;
134 static vm_zone_t mapentzone, kmapentzone, mapzone, vmspace_zone;
135 static struct vm_object kmapentobj, mapentobj, mapobj;
137 static struct vm_map_entry map_entry_init[MAX_MAPENT];
138 static struct vm_map_entry kmap_entry_init[MAX_KMAPENT];
139 static struct vm_map map_init[MAX_KMAP];
141 static void _vm_map_clip_end __P((vm_map_t, vm_map_entry_t, vm_offset_t));
142 static void _vm_map_clip_start __P((vm_map_t, vm_map_entry_t, vm_offset_t));
143 static vm_map_entry_t vm_map_entry_create __P((vm_map_t));
144 static void vm_map_entry_delete __P((vm_map_t, vm_map_entry_t));
145 static void vm_map_entry_dispose __P((vm_map_t, vm_map_entry_t));
146 static void vm_map_entry_unwire __P((vm_map_t, vm_map_entry_t));
147 static void vm_map_copy_entry __P((vm_map_t, vm_map_t, vm_map_entry_t,
149 static void vm_map_split __P((vm_map_entry_t));
150 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));
155 mapzone = &mapzone_store;
156 zbootinit(mapzone, "MAP", sizeof (struct vm_map),
158 kmapentzone = &kmapentzone_store;
159 zbootinit(kmapentzone, "KMAP ENTRY", sizeof (struct vm_map_entry),
160 kmap_entry_init, MAX_KMAPENT);
161 mapentzone = &mapentzone_store;
162 zbootinit(mapentzone, "MAP ENTRY", sizeof (struct vm_map_entry),
163 map_entry_init, MAX_MAPENT);
167 * Allocate a vmspace structure, including a vm_map and pmap,
168 * and initialize those structures. The refcnt is set to 1.
169 * The remaining fields must be initialized by the caller.
172 vmspace_alloc(min, max)
173 vm_offset_t min, max;
177 vm = zalloc(vmspace_zone);
178 vm_map_init(&vm->vm_map, min, max);
179 pmap_pinit(vmspace_pmap(vm));
180 vm->vm_map.pmap = vmspace_pmap(vm); /* XXX */
183 vm->vm_exitingcnt = 0;
189 zinitna(kmapentzone, &kmapentobj,
190 NULL, 0, lmin((VM_MAX_KERNEL_ADDRESS - KERNBASE) / PAGE_SIZE,
191 vmstats.v_page_count) / 8, ZONE_INTERRUPT, 1);
192 zinitna(mapentzone, &mapentobj,
194 zinitna(mapzone, &mapobj,
196 vmspace_zone = zinit("VMSPACE", sizeof (struct vmspace), 0, 0, 3);
202 vmspace_dofree(struct vmspace *vm)
205 * Lock the map, to wait out all other references to it.
206 * Delete all of the mappings and pages they hold, then call
207 * the pmap module to reclaim anything left.
209 vm_map_lock(&vm->vm_map);
210 (void) vm_map_delete(&vm->vm_map, vm->vm_map.min_offset,
211 vm->vm_map.max_offset);
212 vm_map_unlock(&vm->vm_map);
214 pmap_release(vmspace_pmap(vm));
215 zfree(vmspace_zone, vm);
219 vmspace_free(struct vmspace *vm)
221 if (vm->vm_refcnt == 0)
222 panic("vmspace_free: attempt to free already freed vmspace");
224 if (--vm->vm_refcnt == 0 && vm->vm_exitingcnt == 0)
229 vmspace_exitfree(struct proc *p)
237 * cleanup by parent process wait()ing on exiting child. vm_refcnt
238 * may not be 0 (e.g. fork() and child exits without exec()ing).
239 * exitingcnt may increment above 0 and drop back down to zero
240 * several times while vm_refcnt is held non-zero. vm_refcnt
241 * may also increment above 0 and drop back down to zero several
242 * times while vm_exitingcnt is held non-zero.
244 * The last wait on the exiting child's vmspace will clean up
245 * the remainder of the vmspace.
247 if (--vm->vm_exitingcnt == 0 && vm->vm_refcnt == 0)
252 * vmspace_swap_count() - count the approximate swap useage in pages for a
255 * Swap useage is determined by taking the proportional swap used by
256 * VM objects backing the VM map. To make up for fractional losses,
257 * if the VM object has any swap use at all the associated map entries
258 * count for at least 1 swap page.
261 vmspace_swap_count(struct vmspace *vmspace)
263 vm_map_t map = &vmspace->vm_map;
267 for (cur = map->header.next; cur != &map->header; cur = cur->next) {
270 if ((cur->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
271 (object = cur->object.vm_object) != NULL &&
272 object->type == OBJT_SWAP
274 int n = (cur->end - cur->start) / PAGE_SIZE;
276 if (object->un_pager.swp.swp_bcount) {
277 count += object->un_pager.swp.swp_bcount *
278 SWAP_META_PAGES * n / object->size + 1;
289 * Creates and returns a new empty VM map with
290 * the given physical map structure, and having
291 * the given lower and upper address bounds.
294 vm_map_create(pmap, min, max)
296 vm_offset_t min, max;
300 result = zalloc(mapzone);
301 vm_map_init(result, min, max);
307 * Initialize an existing vm_map structure
308 * such as that in the vmspace structure.
309 * The pmap is set elsewhere.
312 vm_map_init(map, min, max)
314 vm_offset_t min, max;
316 map->header.next = map->header.prev = &map->header;
321 map->min_offset = min;
322 map->max_offset = max;
323 map->first_free = &map->header;
324 map->hint = &map->header;
326 lockinit(&map->lock, 0, "thrd_sleep", 0, LK_NOPAUSE);
330 * vm_map_entry_create: [ internal use only ]
332 * Allocates a VM map entry for insertion. No entry fields are filled
333 * in. this ruotine may be called from an interrupt.
335 static vm_map_entry_t
336 vm_map_entry_create(map)
339 vm_map_entry_t new_entry;
341 if (map->system_map || !mapentzone)
342 new_entry = zalloc(kmapentzone);
344 new_entry = zalloc(mapentzone);
345 if (new_entry == NULL)
346 panic("vm_map_entry_create: kernel resources exhausted");
351 * vm_map_entry_dispose: [ internal use only ]
353 * Dispose of a vm_map_entry that is no longer being referenced. This
354 * function may be called from an interrupt.
357 vm_map_entry_dispose(map, entry)
359 vm_map_entry_t entry;
361 if (map->system_map || !mapentzone)
362 zfree(kmapentzone, entry);
364 zfree(mapentzone, entry);
369 * vm_map_entry_{un,}link:
371 * Insert/remove entries from maps.
374 vm_map_entry_link(vm_map_t map,
375 vm_map_entry_t after_where,
376 vm_map_entry_t entry)
379 entry->prev = after_where;
380 entry->next = after_where->next;
381 entry->next->prev = entry;
382 after_where->next = entry;
386 vm_map_entry_unlink(vm_map_t map,
387 vm_map_entry_t entry)
392 if (entry->eflags & MAP_ENTRY_IN_TRANSITION)
393 panic("vm_map_entry_unlink: attempt to mess with locked entry! %p", entry);
404 * Saves the specified entry as the hint for
407 #define SAVE_HINT(map,value) \
408 (map)->hint = (value);
411 * vm_map_lookup_entry: [ internal use only ]
413 * Finds the map entry containing (or
414 * immediately preceding) the specified address
415 * in the given map; the entry is returned
416 * in the "entry" parameter. The boolean
417 * result indicates whether the address is
418 * actually contained in the map.
421 vm_map_lookup_entry(map, address, entry)
424 vm_map_entry_t *entry; /* OUT */
430 * Start looking either from the head of the list, or from the hint.
435 if (cur == &map->header)
438 if (address >= cur->start) {
440 * Go from hint to end of list.
442 * But first, make a quick check to see if we are already looking
443 * at the entry we want (which is usually the case). Note also
444 * that we don't need to save the hint here... it is the same
445 * hint (unless we are at the header, in which case the hint
446 * didn't buy us anything anyway).
449 if ((cur != last) && (cur->end > address)) {
455 * Go from start to hint, *inclusively*
458 cur = map->header.next;
465 while (cur != last) {
466 if (cur->end > address) {
467 if (address >= cur->start) {
469 * Save this lookup for future hints, and
482 SAVE_HINT(map, *entry);
489 * Inserts the given whole VM object into the target
490 * map at the specified address range. The object's
491 * size should match that of the address range.
493 * Requires that the map be locked, and leaves it so.
495 * If object is non-NULL, ref count must be bumped by caller
496 * prior to making call to account for the new entry.
499 vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
500 vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max,
503 vm_map_entry_t new_entry;
504 vm_map_entry_t prev_entry;
505 vm_map_entry_t temp_entry;
506 vm_eflags_t protoeflags;
509 * Check that the start and end points are not bogus.
512 if ((start < map->min_offset) || (end > map->max_offset) ||
514 return (KERN_INVALID_ADDRESS);
517 * Find the entry prior to the proposed starting address; if it's part
518 * of an existing entry, this range is bogus.
521 if (vm_map_lookup_entry(map, start, &temp_entry))
522 return (KERN_NO_SPACE);
524 prev_entry = temp_entry;
527 * Assert that the next entry doesn't overlap the end point.
530 if ((prev_entry->next != &map->header) &&
531 (prev_entry->next->start < end))
532 return (KERN_NO_SPACE);
536 if (cow & MAP_COPY_ON_WRITE)
537 protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
539 if (cow & MAP_NOFAULT) {
540 protoeflags |= MAP_ENTRY_NOFAULT;
542 KASSERT(object == NULL,
543 ("vm_map_insert: paradoxical MAP_NOFAULT request"));
545 if (cow & MAP_DISABLE_SYNCER)
546 protoeflags |= MAP_ENTRY_NOSYNC;
547 if (cow & MAP_DISABLE_COREDUMP)
548 protoeflags |= MAP_ENTRY_NOCOREDUMP;
552 * When object is non-NULL, it could be shared with another
553 * process. We have to set or clear OBJ_ONEMAPPING
556 if ((object->ref_count > 1) || (object->shadow_count != 0)) {
557 vm_object_clear_flag(object, OBJ_ONEMAPPING);
560 else if ((prev_entry != &map->header) &&
561 (prev_entry->eflags == protoeflags) &&
562 (prev_entry->end == start) &&
563 (prev_entry->wired_count == 0) &&
564 ((prev_entry->object.vm_object == NULL) ||
565 vm_object_coalesce(prev_entry->object.vm_object,
566 OFF_TO_IDX(prev_entry->offset),
567 (vm_size_t)(prev_entry->end - prev_entry->start),
568 (vm_size_t)(end - prev_entry->end)))) {
570 * We were able to extend the object. Determine if we
571 * can extend the previous map entry to include the
574 if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
575 (prev_entry->protection == prot) &&
576 (prev_entry->max_protection == max)) {
577 map->size += (end - prev_entry->end);
578 prev_entry->end = end;
579 vm_map_simplify_entry(map, prev_entry);
580 return (KERN_SUCCESS);
584 * If we can extend the object but cannot extend the
585 * map entry, we have to create a new map entry. We
586 * must bump the ref count on the extended object to
587 * account for it. object may be NULL.
589 object = prev_entry->object.vm_object;
590 offset = prev_entry->offset +
591 (prev_entry->end - prev_entry->start);
592 vm_object_reference(object);
596 * NOTE: if conditionals fail, object can be NULL here. This occurs
597 * in things like the buffer map where we manage kva but do not manage
605 new_entry = vm_map_entry_create(map);
606 new_entry->start = start;
607 new_entry->end = end;
609 new_entry->eflags = protoeflags;
610 new_entry->object.vm_object = object;
611 new_entry->offset = offset;
612 new_entry->avail_ssize = 0;
614 new_entry->inheritance = VM_INHERIT_DEFAULT;
615 new_entry->protection = prot;
616 new_entry->max_protection = max;
617 new_entry->wired_count = 0;
620 * Insert the new entry into the list
623 vm_map_entry_link(map, prev_entry, new_entry);
624 map->size += new_entry->end - new_entry->start;
627 * Update the free space hint
629 if ((map->first_free == prev_entry) &&
630 (prev_entry->end >= new_entry->start)) {
631 map->first_free = new_entry;
636 * Temporarily removed to avoid MAP_STACK panic, due to
637 * MAP_STACK being a huge hack. Will be added back in
638 * when MAP_STACK (and the user stack mapping) is fixed.
641 * It may be possible to simplify the entry
643 vm_map_simplify_entry(map, new_entry);
646 if (cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) {
647 pmap_object_init_pt(map->pmap, start,
648 object, OFF_TO_IDX(offset), end - start,
649 cow & MAP_PREFAULT_PARTIAL);
652 return (KERN_SUCCESS);
656 * Find sufficient space for `length' bytes in the given map, starting at
657 * `start'. The map must be locked. Returns 0 on success, 1 on no space.
660 vm_map_findspace(map, start, length, addr)
666 vm_map_entry_t entry, next;
669 if (start < map->min_offset)
670 start = map->min_offset;
671 if (start > map->max_offset)
675 * Look for the first possible address; if there's already something
676 * at this address, we have to start after it.
678 if (start == map->min_offset) {
679 if ((entry = map->first_free) != &map->header)
684 if (vm_map_lookup_entry(map, start, &tmp))
690 * Look through the rest of the map, trying to fit a new region in the
691 * gap between existing regions, or after the very last region.
693 for (;; start = (entry = next)->end) {
695 * Find the end of the proposed new region. Be sure we didn't
696 * go beyond the end of the map, or wrap around the address;
697 * if so, we lose. Otherwise, if this is the last entry, or
698 * if the proposed new region fits before the next entry, we
701 end = start + length;
702 if (end > map->max_offset || end < start)
705 if (next == &map->header || next->start >= end)
708 SAVE_HINT(map, entry);
710 if (map == kernel_map) {
712 if ((ksize = round_page(start + length)) > kernel_vm_end) {
713 pmap_growkernel(ksize);
720 * vm_map_find finds an unallocated region in the target address
721 * map with the given length. The search is defined to be
722 * first-fit from the specified address; the region found is
723 * returned in the same parameter.
725 * If object is non-NULL, ref count must be bumped by caller
726 * prior to making call to account for the new entry.
729 vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
730 vm_offset_t *addr, /* IN/OUT */
731 vm_size_t length, boolean_t find_space, vm_prot_t prot,
732 vm_prot_t max, int cow)
739 if (map == kmem_map || map == mb_map)
744 if (vm_map_findspace(map, start, length, addr)) {
746 if (map == kmem_map || map == mb_map)
748 return (KERN_NO_SPACE);
752 result = vm_map_insert(map, object, offset,
753 start, start + length, prot, max, cow);
756 if (map == kmem_map || map == mb_map)
763 * vm_map_simplify_entry:
765 * Simplify the given map entry by merging with either neighbor. This
766 * routine also has the ability to merge with both neighbors.
768 * The map must be locked.
770 * This routine guarentees that the passed entry remains valid (though
771 * possibly extended). When merging, this routine may delete one or
772 * both neighbors. No action is taken on entries which have their
773 * in-transition flag set.
776 vm_map_simplify_entry(map, entry)
778 vm_map_entry_t entry;
780 vm_map_entry_t next, prev;
781 vm_size_t prevsize, esize;
783 if (entry->eflags & (MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP)) {
784 ++mycpu->gd_cnt.v_intrans_coll;
789 if (prev != &map->header) {
790 prevsize = prev->end - prev->start;
791 if ( (prev->end == entry->start) &&
792 (prev->object.vm_object == entry->object.vm_object) &&
793 (!prev->object.vm_object ||
794 (prev->offset + prevsize == entry->offset)) &&
795 (prev->eflags == entry->eflags) &&
796 (prev->protection == entry->protection) &&
797 (prev->max_protection == entry->max_protection) &&
798 (prev->inheritance == entry->inheritance) &&
799 (prev->wired_count == entry->wired_count)) {
800 if (map->first_free == prev)
801 map->first_free = entry;
802 if (map->hint == prev)
804 vm_map_entry_unlink(map, prev);
805 entry->start = prev->start;
806 entry->offset = prev->offset;
807 if (prev->object.vm_object)
808 vm_object_deallocate(prev->object.vm_object);
809 vm_map_entry_dispose(map, prev);
814 if (next != &map->header) {
815 esize = entry->end - entry->start;
816 if ((entry->end == next->start) &&
817 (next->object.vm_object == entry->object.vm_object) &&
818 (!entry->object.vm_object ||
819 (entry->offset + esize == next->offset)) &&
820 (next->eflags == entry->eflags) &&
821 (next->protection == entry->protection) &&
822 (next->max_protection == entry->max_protection) &&
823 (next->inheritance == entry->inheritance) &&
824 (next->wired_count == entry->wired_count)) {
825 if (map->first_free == next)
826 map->first_free = entry;
827 if (map->hint == next)
829 vm_map_entry_unlink(map, next);
830 entry->end = next->end;
831 if (next->object.vm_object)
832 vm_object_deallocate(next->object.vm_object);
833 vm_map_entry_dispose(map, next);
838 * vm_map_clip_start: [ internal use only ]
840 * Asserts that the given entry begins at or after
841 * the specified address; if necessary,
842 * it splits the entry into two.
844 #define vm_map_clip_start(map, entry, startaddr) \
846 if (startaddr > entry->start) \
847 _vm_map_clip_start(map, entry, startaddr); \
851 * This routine is called only when it is known that
852 * the entry must be split.
855 _vm_map_clip_start(map, entry, start)
857 vm_map_entry_t entry;
860 vm_map_entry_t new_entry;
863 * Split off the front portion -- note that we must insert the new
864 * entry BEFORE this one, so that this entry has the specified
868 vm_map_simplify_entry(map, entry);
871 * If there is no object backing this entry, we might as well create
872 * one now. If we defer it, an object can get created after the map
873 * is clipped, and individual objects will be created for the split-up
874 * map. This is a bit of a hack, but is also about the best place to
875 * put this improvement.
878 if (entry->object.vm_object == NULL && !map->system_map) {
880 object = vm_object_allocate(OBJT_DEFAULT,
881 atop(entry->end - entry->start));
882 entry->object.vm_object = object;
886 new_entry = vm_map_entry_create(map);
889 new_entry->end = start;
890 entry->offset += (start - entry->start);
891 entry->start = start;
893 vm_map_entry_link(map, entry->prev, new_entry);
895 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
896 vm_object_reference(new_entry->object.vm_object);
901 * vm_map_clip_end: [ internal use only ]
903 * Asserts that the given entry ends at or before
904 * the specified address; if necessary,
905 * it splits the entry into two.
908 #define vm_map_clip_end(map, entry, endaddr) \
910 if (endaddr < entry->end) \
911 _vm_map_clip_end(map, entry, endaddr); \
915 * This routine is called only when it is known that
916 * the entry must be split.
919 _vm_map_clip_end(map, entry, end)
921 vm_map_entry_t entry;
924 vm_map_entry_t new_entry;
927 * If there is no object backing this entry, we might as well create
928 * one now. If we defer it, an object can get created after the map
929 * is clipped, and individual objects will be created for the split-up
930 * map. This is a bit of a hack, but is also about the best place to
931 * put this improvement.
934 if (entry->object.vm_object == NULL && !map->system_map) {
936 object = vm_object_allocate(OBJT_DEFAULT,
937 atop(entry->end - entry->start));
938 entry->object.vm_object = object;
943 * Create a new entry and insert it AFTER the specified entry
946 new_entry = vm_map_entry_create(map);
949 new_entry->start = entry->end = end;
950 new_entry->offset += (end - entry->start);
952 vm_map_entry_link(map, entry, new_entry);
954 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
955 vm_object_reference(new_entry->object.vm_object);
960 * VM_MAP_RANGE_CHECK: [ internal use only ]
962 * Asserts that the starting and ending region
963 * addresses fall within the valid range of the map.
965 #define VM_MAP_RANGE_CHECK(map, start, end) \
967 if (start < vm_map_min(map)) \
968 start = vm_map_min(map); \
969 if (end > vm_map_max(map)) \
970 end = vm_map_max(map); \
976 * vm_map_transition_wait: [ kernel use only ]
978 * Used to block when an in-transition collison occurs. The map
979 * is unlocked for the sleep and relocked before the return.
983 vm_map_transition_wait(vm_map_t map)
986 tsleep(map, 0, "vment", 0);
994 * When we do blocking operations with the map lock held it is
995 * possible that a clip might have occured on our in-transit entry,
996 * requiring an adjustment to the entry in our loop. These macros
997 * help the pageable and clip_range code deal with the case. The
998 * conditional costs virtually nothing if no clipping has occured.
1001 #define CLIP_CHECK_BACK(entry, save_start) \
1003 while (entry->start != save_start) { \
1004 entry = entry->prev; \
1005 KASSERT(entry != &map->header, ("bad entry clip")); \
1009 #define CLIP_CHECK_FWD(entry, save_end) \
1011 while (entry->end != save_end) { \
1012 entry = entry->next; \
1013 KASSERT(entry != &map->header, ("bad entry clip")); \
1019 * vm_map_clip_range: [ kernel use only ]
1021 * Clip the specified range and return the base entry. The
1022 * range may cover several entries starting at the returned base
1023 * and the first and last entry in the covering sequence will be
1024 * properly clipped to the requested start and end address.
1026 * If no holes are allowed you should pass the MAP_CLIP_NO_HOLES
1029 * The MAP_ENTRY_IN_TRANSITION flag will be set for the entries
1030 * covered by the requested range.
1032 * The map must be exclusively locked on entry and will remain locked
1033 * on return. If no range exists or the range contains holes and you
1034 * specified that no holes were allowed, NULL will be returned. This
1035 * routine may temporarily unlock the map in order avoid a deadlock when
1040 vm_map_clip_range(vm_map_t map, vm_offset_t start, vm_offset_t end, int flags)
1042 vm_map_entry_t start_entry;
1043 vm_map_entry_t entry;
1046 * Locate the entry and effect initial clipping. The in-transition
1047 * case does not occur very often so do not try to optimize it.
1050 if (vm_map_lookup_entry(map, start, &start_entry) == FALSE)
1052 entry = start_entry;
1053 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1054 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1055 ++mycpu->gd_cnt.v_intrans_coll;
1056 ++mycpu->gd_cnt.v_intrans_wait;
1057 vm_map_transition_wait(map);
1059 * entry and/or start_entry may have been clipped while
1060 * we slept, or may have gone away entirely. We have
1061 * to restart from the lookup.
1066 * Since we hold an exclusive map lock we do not have to restart
1067 * after clipping, even though clipping may block in zalloc.
1069 vm_map_clip_start(map, entry, start);
1070 vm_map_clip_end(map, entry, end);
1071 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
1074 * Scan entries covered by the range. When working on the next
1075 * entry a restart need only re-loop on the current entry which
1076 * we have already locked, since 'next' may have changed. Also,
1077 * even though entry is safe, it may have been clipped so we
1078 * have to iterate forwards through the clip after sleeping.
1080 while (entry->next != &map->header && entry->next->start < end) {
1081 vm_map_entry_t next = entry->next;
1083 if (flags & MAP_CLIP_NO_HOLES) {
1084 if (next->start > entry->end) {
1085 vm_map_unclip_range(map, start_entry,
1086 start, entry->end, flags);
1091 if (next->eflags & MAP_ENTRY_IN_TRANSITION) {
1092 vm_offset_t save_end = entry->end;
1093 next->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1094 ++mycpu->gd_cnt.v_intrans_coll;
1095 ++mycpu->gd_cnt.v_intrans_wait;
1096 vm_map_transition_wait(map);
1099 * clips might have occured while we blocked.
1101 CLIP_CHECK_FWD(entry, save_end);
1102 CLIP_CHECK_BACK(start_entry, start);
1106 * No restart necessary even though clip_end may block, we
1107 * are holding the map lock.
1109 vm_map_clip_end(map, next, end);
1110 next->eflags |= MAP_ENTRY_IN_TRANSITION;
1113 if (flags & MAP_CLIP_NO_HOLES) {
1114 if (entry->end != end) {
1115 vm_map_unclip_range(map, start_entry,
1116 start, entry->end, flags);
1120 return(start_entry);
1124 * vm_map_unclip_range: [ kernel use only ]
1126 * Undo the effect of vm_map_clip_range(). You should pass the same
1127 * flags and the same range that you passed to vm_map_clip_range().
1128 * This code will clear the in-transition flag on the entries and
1129 * wake up anyone waiting. This code will also simplify the sequence
1130 * and attempt to merge it with entries before and after the sequence.
1132 * The map must be locked on entry and will remain locked on return.
1134 * Note that you should also pass the start_entry returned by
1135 * vm_map_clip_range(). However, if you block between the two calls
1136 * with the map unlocked please be aware that the start_entry may
1137 * have been clipped and you may need to scan it backwards to find
1138 * the entry corresponding with the original start address. You are
1139 * responsible for this, vm_map_unclip_range() expects the correct
1140 * start_entry to be passed to it and will KASSERT otherwise.
1144 vm_map_unclip_range(
1146 vm_map_entry_t start_entry,
1151 vm_map_entry_t entry;
1153 entry = start_entry;
1155 KASSERT(entry->start == start, ("unclip_range: illegal base entry"));
1156 while (entry != &map->header && entry->start < end) {
1157 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION, ("in-transition flag not set during unclip on: %p", entry));
1158 KASSERT(entry->end <= end, ("unclip_range: tail wasn't clipped"));
1159 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
1160 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
1161 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
1164 entry = entry->next;
1168 * Simplification does not block so there is no restart case.
1170 entry = start_entry;
1171 while (entry != &map->header && entry->start < end) {
1172 vm_map_simplify_entry(map, entry);
1173 entry = entry->next;
1178 * vm_map_submap: [ kernel use only ]
1180 * Mark the given range as handled by a subordinate map.
1182 * This range must have been created with vm_map_find,
1183 * and no other operations may have been performed on this
1184 * range prior to calling vm_map_submap.
1186 * Only a limited number of operations can be performed
1187 * within this rage after calling vm_map_submap:
1189 * [Don't try vm_map_copy!]
1191 * To remove a submapping, one must first remove the
1192 * range from the superior map, and then destroy the
1193 * submap (if desired). [Better yet, don't try it.]
1196 vm_map_submap(map, start, end, submap)
1202 vm_map_entry_t entry;
1203 int result = KERN_INVALID_ARGUMENT;
1207 VM_MAP_RANGE_CHECK(map, start, end);
1209 if (vm_map_lookup_entry(map, start, &entry)) {
1210 vm_map_clip_start(map, entry, start);
1212 entry = entry->next;
1215 vm_map_clip_end(map, entry, end);
1217 if ((entry->start == start) && (entry->end == end) &&
1218 ((entry->eflags & MAP_ENTRY_COW) == 0) &&
1219 (entry->object.vm_object == NULL)) {
1220 entry->object.sub_map = submap;
1221 entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
1222 result = KERN_SUCCESS;
1232 * Sets the protection of the specified address
1233 * region in the target map. If "set_max" is
1234 * specified, the maximum protection is to be set;
1235 * otherwise, only the current protection is affected.
1238 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
1239 vm_prot_t new_prot, boolean_t set_max)
1241 vm_map_entry_t current;
1242 vm_map_entry_t entry;
1246 VM_MAP_RANGE_CHECK(map, start, end);
1248 if (vm_map_lookup_entry(map, start, &entry)) {
1249 vm_map_clip_start(map, entry, start);
1251 entry = entry->next;
1255 * Make a first pass to check for protection violations.
1259 while ((current != &map->header) && (current->start < end)) {
1260 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1262 return (KERN_INVALID_ARGUMENT);
1264 if ((new_prot & current->max_protection) != new_prot) {
1266 return (KERN_PROTECTION_FAILURE);
1268 current = current->next;
1272 * Go back and fix up protections. [Note that clipping is not
1273 * necessary the second time.]
1278 while ((current != &map->header) && (current->start < end)) {
1281 vm_map_clip_end(map, current, end);
1283 old_prot = current->protection;
1285 current->protection =
1286 (current->max_protection = new_prot) &
1289 current->protection = new_prot;
1292 * Update physical map if necessary. Worry about copy-on-write
1293 * here -- CHECK THIS XXX
1296 if (current->protection != old_prot) {
1297 #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1300 pmap_protect(map->pmap, current->start,
1302 current->protection & MASK(current));
1306 vm_map_simplify_entry(map, current);
1308 current = current->next;
1312 return (KERN_SUCCESS);
1318 * This routine traverses a processes map handling the madvise
1319 * system call. Advisories are classified as either those effecting
1320 * the vm_map_entry structure, or those effecting the underlying
1325 vm_map_madvise(map, start, end, behav)
1327 vm_offset_t start, end;
1330 vm_map_entry_t current, entry;
1334 * Some madvise calls directly modify the vm_map_entry, in which case
1335 * we need to use an exclusive lock on the map and we need to perform
1336 * various clipping operations. Otherwise we only need a read-lock
1342 case MADV_SEQUENTIAL:
1354 vm_map_lock_read(map);
1357 return (KERN_INVALID_ARGUMENT);
1361 * Locate starting entry and clip if necessary.
1364 VM_MAP_RANGE_CHECK(map, start, end);
1366 if (vm_map_lookup_entry(map, start, &entry)) {
1368 vm_map_clip_start(map, entry, start);
1370 entry = entry->next;
1375 * madvise behaviors that are implemented in the vm_map_entry.
1377 * We clip the vm_map_entry so that behavioral changes are
1378 * limited to the specified address range.
1380 for (current = entry;
1381 (current != &map->header) && (current->start < end);
1382 current = current->next
1384 if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1387 vm_map_clip_end(map, current, end);
1391 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
1393 case MADV_SEQUENTIAL:
1394 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
1397 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
1400 current->eflags |= MAP_ENTRY_NOSYNC;
1403 current->eflags &= ~MAP_ENTRY_NOSYNC;
1406 current->eflags |= MAP_ENTRY_NOCOREDUMP;
1409 current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
1414 vm_map_simplify_entry(map, current);
1422 * madvise behaviors that are implemented in the underlying
1425 * Since we don't clip the vm_map_entry, we have to clip
1426 * the vm_object pindex and count.
1428 for (current = entry;
1429 (current != &map->header) && (current->start < end);
1430 current = current->next
1432 vm_offset_t useStart;
1434 if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1437 pindex = OFF_TO_IDX(current->offset);
1438 count = atop(current->end - current->start);
1439 useStart = current->start;
1441 if (current->start < start) {
1442 pindex += atop(start - current->start);
1443 count -= atop(start - current->start);
1446 if (current->end > end)
1447 count -= atop(current->end - end);
1452 vm_object_madvise(current->object.vm_object,
1453 pindex, count, behav);
1454 if (behav == MADV_WILLNEED) {
1455 pmap_object_init_pt(
1458 current->object.vm_object,
1460 (count << PAGE_SHIFT),
1461 MAP_PREFAULT_MADVISE
1465 vm_map_unlock_read(map);
1474 * Sets the inheritance of the specified address
1475 * range in the target map. Inheritance
1476 * affects how the map will be shared with
1477 * child maps at the time of vm_map_fork.
1480 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
1481 vm_inherit_t new_inheritance)
1483 vm_map_entry_t entry;
1484 vm_map_entry_t temp_entry;
1486 switch (new_inheritance) {
1487 case VM_INHERIT_NONE:
1488 case VM_INHERIT_COPY:
1489 case VM_INHERIT_SHARE:
1492 return (KERN_INVALID_ARGUMENT);
1497 VM_MAP_RANGE_CHECK(map, start, end);
1499 if (vm_map_lookup_entry(map, start, &temp_entry)) {
1501 vm_map_clip_start(map, entry, start);
1503 entry = temp_entry->next;
1505 while ((entry != &map->header) && (entry->start < end)) {
1506 vm_map_clip_end(map, entry, end);
1508 entry->inheritance = new_inheritance;
1510 vm_map_simplify_entry(map, entry);
1512 entry = entry->next;
1516 return (KERN_SUCCESS);
1520 * Implement the semantics of mlock
1523 vm_map_user_pageable(map, start, real_end, new_pageable)
1526 vm_offset_t real_end;
1527 boolean_t new_pageable;
1529 vm_map_entry_t entry;
1530 vm_map_entry_t start_entry;
1532 int rv = KERN_SUCCESS;
1535 VM_MAP_RANGE_CHECK(map, start, real_end);
1538 start_entry = vm_map_clip_range(map, start, end, MAP_CLIP_NO_HOLES);
1539 if (start_entry == NULL) {
1541 return (KERN_INVALID_ADDRESS);
1544 if (new_pageable == 0) {
1545 entry = start_entry;
1546 while ((entry != &map->header) && (entry->start < end)) {
1547 vm_offset_t save_start;
1548 vm_offset_t save_end;
1551 * Already user wired or hard wired (trivial cases)
1553 if (entry->eflags & MAP_ENTRY_USER_WIRED) {
1554 entry = entry->next;
1557 if (entry->wired_count != 0) {
1558 entry->wired_count++;
1559 entry->eflags |= MAP_ENTRY_USER_WIRED;
1560 entry = entry->next;
1565 * A new wiring requires instantiation of appropriate
1566 * management structures and the faulting in of the
1569 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1570 int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY;
1571 if (copyflag && ((entry->protection & VM_PROT_WRITE) != 0)) {
1573 vm_object_shadow(&entry->object.vm_object,
1575 atop(entry->end - entry->start));
1576 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
1578 } else if (entry->object.vm_object == NULL &&
1581 entry->object.vm_object =
1582 vm_object_allocate(OBJT_DEFAULT,
1583 atop(entry->end - entry->start));
1584 entry->offset = (vm_offset_t) 0;
1588 entry->wired_count++;
1589 entry->eflags |= MAP_ENTRY_USER_WIRED;
1592 * Now fault in the area. The map lock needs to be
1593 * manipulated to avoid deadlocks. The in-transition
1594 * flag protects the entries.
1596 save_start = entry->start;
1597 save_end = entry->end;
1600 rv = vm_fault_user_wire(map, save_start, save_end);
1603 CLIP_CHECK_BACK(entry, save_start);
1605 KASSERT(entry->wired_count == 1, ("bad wired_count on entry"));
1606 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
1607 entry->wired_count = 0;
1608 if (entry->end == save_end)
1610 entry = entry->next;
1611 KASSERT(entry != &map->header, ("bad entry clip during backout"));
1613 end = save_start; /* unwire the rest */
1617 * note that even though the entry might have been
1618 * clipped, the USER_WIRED flag we set prevents
1619 * duplication so we do not have to do a
1622 entry = entry->next;
1626 * If we failed fall through to the unwiring section to
1627 * unwire what we had wired so far. 'end' has already
1634 * start_entry might have been clipped if we unlocked the
1635 * map and blocked. No matter how clipped it has gotten
1636 * there should be a fragment that is on our start boundary.
1638 CLIP_CHECK_BACK(start_entry, start);
1642 * Deal with the unwiring case.
1646 * This is the unwiring case. We must first ensure that the
1647 * range to be unwired is really wired down. We know there
1650 entry = start_entry;
1651 while ((entry != &map->header) && (entry->start < end)) {
1652 if ((entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
1653 rv = KERN_INVALID_ARGUMENT;
1656 KASSERT(entry->wired_count != 0, ("wired count was 0 with USER_WIRED set! %p", entry));
1657 entry = entry->next;
1661 * Now decrement the wiring count for each region. If a region
1662 * becomes completely unwired, unwire its physical pages and
1665 while ((entry != &map->header) && (entry->start < end)) {
1666 KASSERT(entry->eflags & MAP_ENTRY_USER_WIRED, ("expected USER_WIRED on entry %p", entry));
1667 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
1668 entry->wired_count--;
1669 if (entry->wired_count == 0)
1670 vm_fault_unwire(map, entry->start, entry->end);
1671 entry = entry->next;
1675 vm_map_unclip_range(map, start_entry, start, real_end,
1685 * Sets the pageability of the specified address
1686 * range in the target map. Regions specified
1687 * as not pageable require locked-down physical
1688 * memory and physical page maps.
1690 * The map must not be locked, but a reference
1691 * must remain to the map throughout the call.
1694 vm_map_pageable(map, start, real_end, new_pageable)
1697 vm_offset_t real_end;
1698 boolean_t new_pageable;
1700 vm_map_entry_t entry;
1701 vm_map_entry_t start_entry;
1703 int rv = KERN_SUCCESS;
1707 VM_MAP_RANGE_CHECK(map, start, real_end);
1710 start_entry = vm_map_clip_range(map, start, end, MAP_CLIP_NO_HOLES);
1711 if (start_entry == NULL) {
1713 return (KERN_INVALID_ADDRESS);
1715 if (new_pageable == 0) {
1719 * 1. Holding the write lock, we create any shadow or zero-fill
1720 * objects that need to be created. Then we clip each map
1721 * entry to the region to be wired and increment its wiring
1722 * count. We create objects before clipping the map entries
1723 * to avoid object proliferation.
1725 * 2. We downgrade to a read lock, and call vm_fault_wire to
1726 * fault in the pages for any newly wired area (wired_count is
1729 * Downgrading to a read lock for vm_fault_wire avoids a
1730 * possible deadlock with another process that may have faulted
1731 * on one of the pages to be wired (it would mark the page busy,
1732 * blocking us, then in turn block on the map lock that we
1733 * hold). Because of problems in the recursive lock package,
1734 * we cannot upgrade to a write lock in vm_map_lookup. Thus,
1735 * any actions that require the write lock must be done
1736 * beforehand. Because we keep the read lock on the map, the
1737 * copy-on-write status of the entries we modify here cannot
1741 entry = start_entry;
1742 while ((entry != &map->header) && (entry->start < end)) {
1744 * Trivial case if the entry is already wired
1746 if (entry->wired_count) {
1747 entry->wired_count++;
1748 entry = entry->next;
1753 * The entry is being newly wired, we have to setup
1754 * appropriate management structures. A shadow
1755 * object is required for a copy-on-write region,
1756 * or a normal object for a zero-fill region. We
1757 * do not have to do this for entries that point to sub
1758 * maps because we won't hold the lock on the sub map.
1760 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1761 int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY;
1763 ((entry->protection & VM_PROT_WRITE) != 0)) {
1765 vm_object_shadow(&entry->object.vm_object,
1767 atop(entry->end - entry->start));
1768 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
1769 } else if (entry->object.vm_object == NULL &&
1771 entry->object.vm_object =
1772 vm_object_allocate(OBJT_DEFAULT,
1773 atop(entry->end - entry->start));
1774 entry->offset = (vm_offset_t) 0;
1778 entry->wired_count++;
1779 entry = entry->next;
1787 * HACK HACK HACK HACK
1789 * Unlock the map to avoid deadlocks. The in-transit flag
1790 * protects us from most changes but note that
1791 * clipping may still occur. To prevent clipping from
1792 * occuring after the unlock, except for when we are
1793 * blocking in vm_fault_wire, we must run at splvm().
1794 * Otherwise our accesses to entry->start and entry->end
1795 * could be corrupted. We have to set splvm() prior to
1796 * unlocking so start_entry does not change out from
1797 * under us at the very beginning of the loop.
1799 * HACK HACK HACK HACK
1805 entry = start_entry;
1806 while (entry != &map->header && entry->start < end) {
1808 * If vm_fault_wire fails for any page we need to undo
1809 * what has been done. We decrement the wiring count
1810 * for those pages which have not yet been wired (now)
1811 * and unwire those that have (later).
1813 vm_offset_t save_start = entry->start;
1814 vm_offset_t save_end = entry->end;
1816 if (entry->wired_count == 1)
1817 rv = vm_fault_wire(map, entry->start, entry->end);
1819 CLIP_CHECK_BACK(entry, save_start);
1821 KASSERT(entry->wired_count == 1, ("wired_count changed unexpectedly"));
1822 entry->wired_count = 0;
1823 if (entry->end == save_end)
1825 entry = entry->next;
1826 KASSERT(entry != &map->header, ("bad entry clip during backout"));
1831 CLIP_CHECK_FWD(entry, save_end);
1832 entry = entry->next;
1837 * relock. start_entry is still IN_TRANSITION and must
1838 * still exist, but may have been clipped (handled just
1844 * If a failure occured undo everything by falling through
1845 * to the unwiring code. 'end' has already been adjusted
1852 * start_entry might have been clipped if we unlocked the
1853 * map and blocked. No matter how clipped it has gotten
1854 * there should be a fragment that is on our start boundary.
1856 CLIP_CHECK_BACK(start_entry, start);
1861 * This is the unwiring case. We must first ensure that the
1862 * range to be unwired is really wired down. We know there
1865 entry = start_entry;
1866 while ((entry != &map->header) && (entry->start < end)) {
1867 if (entry->wired_count == 0) {
1868 rv = KERN_INVALID_ARGUMENT;
1871 entry = entry->next;
1875 * Now decrement the wiring count for each region. If a region
1876 * becomes completely unwired, unwire its physical pages and
1879 entry = start_entry;
1880 while ((entry != &map->header) && (entry->start < end)) {
1881 entry->wired_count--;
1882 if (entry->wired_count == 0)
1883 vm_fault_unwire(map, entry->start, entry->end);
1884 entry = entry->next;
1888 vm_map_unclip_range(map, start_entry, start, real_end,
1898 * Push any dirty cached pages in the address range to their pager.
1899 * If syncio is TRUE, dirty pages are written synchronously.
1900 * If invalidate is TRUE, any cached pages are freed as well.
1902 * Returns an error if any part of the specified range is not mapped.
1905 vm_map_clean(map, start, end, syncio, invalidate)
1910 boolean_t invalidate;
1912 vm_map_entry_t current;
1913 vm_map_entry_t entry;
1916 vm_ooffset_t offset;
1918 vm_map_lock_read(map);
1919 VM_MAP_RANGE_CHECK(map, start, end);
1920 if (!vm_map_lookup_entry(map, start, &entry)) {
1921 vm_map_unlock_read(map);
1922 return (KERN_INVALID_ADDRESS);
1925 * Make a first pass to check for holes.
1927 for (current = entry; current->start < end; current = current->next) {
1928 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1929 vm_map_unlock_read(map);
1930 return (KERN_INVALID_ARGUMENT);
1932 if (end > current->end &&
1933 (current->next == &map->header ||
1934 current->end != current->next->start)) {
1935 vm_map_unlock_read(map);
1936 return (KERN_INVALID_ADDRESS);
1941 pmap_remove(vm_map_pmap(map), start, end);
1943 * Make a second pass, cleaning/uncaching pages from the indicated
1946 for (current = entry; current->start < end; current = current->next) {
1947 offset = current->offset + (start - current->start);
1948 size = (end <= current->end ? end : current->end) - start;
1949 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1951 vm_map_entry_t tentry;
1954 smap = current->object.sub_map;
1955 vm_map_lock_read(smap);
1956 (void) vm_map_lookup_entry(smap, offset, &tentry);
1957 tsize = tentry->end - offset;
1960 object = tentry->object.vm_object;
1961 offset = tentry->offset + (offset - tentry->start);
1962 vm_map_unlock_read(smap);
1964 object = current->object.vm_object;
1967 * Note that there is absolutely no sense in writing out
1968 * anonymous objects, so we track down the vnode object
1970 * We invalidate (remove) all pages from the address space
1971 * anyway, for semantic correctness.
1973 * note: certain anonymous maps, such as MAP_NOSYNC maps,
1974 * may start out with a NULL object.
1976 while (object && object->backing_object) {
1977 object = object->backing_object;
1978 offset += object->backing_object_offset;
1979 if (object->size < OFF_TO_IDX( offset + size))
1980 size = IDX_TO_OFF(object->size) - offset;
1982 if (object && (object->type == OBJT_VNODE) &&
1983 (current->protection & VM_PROT_WRITE)) {
1985 * Flush pages if writing is allowed, invalidate them
1986 * if invalidation requested. Pages undergoing I/O
1987 * will be ignored by vm_object_page_remove().
1989 * We cannot lock the vnode and then wait for paging
1990 * to complete without deadlocking against vm_fault.
1991 * Instead we simply call vm_object_page_remove() and
1992 * allow it to block internally on a page-by-page
1993 * basis when it encounters pages undergoing async
1998 vm_object_reference(object);
1999 vn_lock(object->handle, LK_EXCLUSIVE | LK_RETRY, curthread);
2000 flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
2001 flags |= invalidate ? OBJPC_INVAL : 0;
2002 vm_object_page_clean(object,
2004 OFF_TO_IDX(offset + size + PAGE_MASK),
2006 VOP_UNLOCK(object->handle, 0, curthread);
2007 vm_object_deallocate(object);
2009 if (object && invalidate &&
2010 ((object->type == OBJT_VNODE) ||
2011 (object->type == OBJT_DEVICE))) {
2012 vm_object_reference(object);
2013 vm_object_page_remove(object,
2015 OFF_TO_IDX(offset + size + PAGE_MASK),
2017 vm_object_deallocate(object);
2022 vm_map_unlock_read(map);
2023 return (KERN_SUCCESS);
2027 * vm_map_entry_unwire: [ internal use only ]
2029 * Make the region specified by this entry pageable.
2031 * The map in question should be locked.
2032 * [This is the reason for this routine's existence.]
2035 vm_map_entry_unwire(map, entry)
2037 vm_map_entry_t entry;
2039 vm_fault_unwire(map, entry->start, entry->end);
2040 entry->wired_count = 0;
2044 * vm_map_entry_delete: [ internal use only ]
2046 * Deallocate the given entry from the target map.
2049 vm_map_entry_delete(map, entry)
2051 vm_map_entry_t entry;
2053 vm_map_entry_unlink(map, entry);
2054 map->size -= entry->end - entry->start;
2056 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
2057 vm_object_deallocate(entry->object.vm_object);
2060 vm_map_entry_dispose(map, entry);
2064 * vm_map_delete: [ internal use only ]
2066 * Deallocates the given address range from the target
2070 vm_map_delete(map, start, end)
2076 vm_map_entry_t entry;
2077 vm_map_entry_t first_entry;
2080 * Find the start of the region, and clip it
2084 if (!vm_map_lookup_entry(map, start, &first_entry))
2085 entry = first_entry->next;
2087 entry = first_entry;
2088 vm_map_clip_start(map, entry, start);
2090 * Fix the lookup hint now, rather than each time though the
2093 SAVE_HINT(map, entry->prev);
2097 * Save the free space hint
2100 if (entry == &map->header) {
2101 map->first_free = &map->header;
2102 } else if (map->first_free->start >= start) {
2103 map->first_free = entry->prev;
2107 * Step through all entries in this region
2110 while ((entry != &map->header) && (entry->start < end)) {
2111 vm_map_entry_t next;
2113 vm_pindex_t offidxstart, offidxend, count;
2116 * If we hit an in-transition entry we have to sleep and
2117 * retry. It's easier (and not really slower) to just retry
2118 * since this case occurs so rarely and the hint is already
2119 * pointing at the right place. We have to reset the
2120 * start offset so as not to accidently delete an entry
2121 * another process just created in vacated space.
2123 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
2124 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2125 start = entry->start;
2126 ++mycpu->gd_cnt.v_intrans_coll;
2127 ++mycpu->gd_cnt.v_intrans_wait;
2128 vm_map_transition_wait(map);
2131 vm_map_clip_end(map, entry, end);
2137 offidxstart = OFF_TO_IDX(entry->offset);
2138 count = OFF_TO_IDX(e - s);
2139 object = entry->object.vm_object;
2142 * Unwire before removing addresses from the pmap; otherwise,
2143 * unwiring will put the entries back in the pmap.
2145 if (entry->wired_count != 0) {
2146 vm_map_entry_unwire(map, entry);
2149 offidxend = offidxstart + count;
2151 if ((object == kernel_object) || (object == kmem_object)) {
2152 vm_object_page_remove(object, offidxstart, offidxend, FALSE);
2154 pmap_remove(map->pmap, s, e);
2155 if (object != NULL &&
2156 object->ref_count != 1 &&
2157 (object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING &&
2158 (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2159 vm_object_collapse(object);
2160 vm_object_page_remove(object, offidxstart, offidxend, FALSE);
2161 if (object->type == OBJT_SWAP) {
2162 swap_pager_freespace(object, offidxstart, count);
2164 if (offidxend >= object->size &&
2165 offidxstart < object->size) {
2166 object->size = offidxstart;
2172 * Delete the entry (which may delete the object) only after
2173 * removing all pmap entries pointing to its pages.
2174 * (Otherwise, its page frames may be reallocated, and any
2175 * modify bits will be set in the wrong object!)
2177 vm_map_entry_delete(map, entry);
2180 return (KERN_SUCCESS);
2186 * Remove the given address range from the target map.
2187 * This is the exported form of vm_map_delete.
2190 vm_map_remove(map, start, end)
2197 if (map == kmem_map || map == mb_map)
2201 VM_MAP_RANGE_CHECK(map, start, end);
2202 result = vm_map_delete(map, start, end);
2205 if (map == kmem_map || map == mb_map)
2212 * vm_map_check_protection:
2214 * Assert that the target map allows the specified
2215 * privilege on the entire address region given.
2216 * The entire region must be allocated.
2219 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
2220 vm_prot_t protection)
2222 vm_map_entry_t entry;
2223 vm_map_entry_t tmp_entry;
2225 if (!vm_map_lookup_entry(map, start, &tmp_entry)) {
2230 while (start < end) {
2231 if (entry == &map->header) {
2238 if (start < entry->start) {
2242 * Check protection associated with entry.
2245 if ((entry->protection & protection) != protection) {
2248 /* go to next entry */
2251 entry = entry->next;
2257 * Split the pages in a map entry into a new object. This affords
2258 * easier removal of unused pages, and keeps object inheritance from
2259 * being a negative impact on memory usage.
2263 vm_map_entry_t entry;
2266 vm_object_t orig_object, new_object, source;
2268 vm_pindex_t offidxstart, offidxend, idx;
2270 vm_ooffset_t offset;
2272 orig_object = entry->object.vm_object;
2273 if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
2275 if (orig_object->ref_count <= 1)
2278 offset = entry->offset;
2282 offidxstart = OFF_TO_IDX(offset);
2283 offidxend = offidxstart + OFF_TO_IDX(e - s);
2284 size = offidxend - offidxstart;
2286 new_object = vm_pager_allocate(orig_object->type,
2287 NULL, IDX_TO_OFF(size), VM_PROT_ALL, 0LL);
2288 if (new_object == NULL)
2291 source = orig_object->backing_object;
2292 if (source != NULL) {
2293 vm_object_reference(source); /* Referenced by new_object */
2294 LIST_INSERT_HEAD(&source->shadow_head,
2295 new_object, shadow_list);
2296 vm_object_clear_flag(source, OBJ_ONEMAPPING);
2297 new_object->backing_object_offset =
2298 orig_object->backing_object_offset + IDX_TO_OFF(offidxstart);
2299 new_object->backing_object = source;
2300 source->shadow_count++;
2301 source->generation++;
2304 for (idx = 0; idx < size; idx++) {
2308 m = vm_page_lookup(orig_object, offidxstart + idx);
2313 * We must wait for pending I/O to complete before we can
2316 * We do not have to VM_PROT_NONE the page as mappings should
2317 * not be changed by this operation.
2319 if (vm_page_sleep_busy(m, TRUE, "spltwt"))
2323 vm_page_rename(m, new_object, idx);
2324 /* page automatically made dirty by rename and cache handled */
2328 if (orig_object->type == OBJT_SWAP) {
2329 vm_object_pip_add(orig_object, 1);
2331 * copy orig_object pages into new_object
2332 * and destroy unneeded pages in
2335 swap_pager_copy(orig_object, new_object, offidxstart, 0);
2336 vm_object_pip_wakeup(orig_object);
2339 for (idx = 0; idx < size; idx++) {
2340 m = vm_page_lookup(new_object, idx);
2346 entry->object.vm_object = new_object;
2347 entry->offset = 0LL;
2348 vm_object_deallocate(orig_object);
2352 * vm_map_copy_entry:
2354 * Copies the contents of the source entry to the destination
2355 * entry. The entries *must* be aligned properly.
2358 vm_map_copy_entry(src_map, dst_map, src_entry, dst_entry)
2359 vm_map_t src_map, dst_map;
2360 vm_map_entry_t src_entry, dst_entry;
2362 vm_object_t src_object;
2364 if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
2367 if (src_entry->wired_count == 0) {
2370 * If the source entry is marked needs_copy, it is already
2373 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
2374 pmap_protect(src_map->pmap,
2377 src_entry->protection & ~VM_PROT_WRITE);
2381 * Make a copy of the object.
2383 if ((src_object = src_entry->object.vm_object) != NULL) {
2385 if ((src_object->handle == NULL) &&
2386 (src_object->type == OBJT_DEFAULT ||
2387 src_object->type == OBJT_SWAP)) {
2388 vm_object_collapse(src_object);
2389 if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
2390 vm_map_split(src_entry);
2391 src_object = src_entry->object.vm_object;
2395 vm_object_reference(src_object);
2396 vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
2397 dst_entry->object.vm_object = src_object;
2398 src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2399 dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2400 dst_entry->offset = src_entry->offset;
2402 dst_entry->object.vm_object = NULL;
2403 dst_entry->offset = 0;
2406 pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
2407 dst_entry->end - dst_entry->start, src_entry->start);
2410 * Of course, wired down pages can't be set copy-on-write.
2411 * Cause wired pages to be copied into the new map by
2412 * simulating faults (the new pages are pageable)
2414 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry);
2420 * Create a new process vmspace structure and vm_map
2421 * based on those of an existing process. The new map
2422 * is based on the old map, according to the inheritance
2423 * values on the regions in that map.
2425 * The source map must not be locked.
2429 struct vmspace *vm1;
2431 struct vmspace *vm2;
2432 vm_map_t old_map = &vm1->vm_map;
2434 vm_map_entry_t old_entry;
2435 vm_map_entry_t new_entry;
2438 vm_map_lock(old_map);
2439 old_map->infork = 1;
2441 vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
2442 bcopy(&vm1->vm_startcopy, &vm2->vm_startcopy,
2443 (caddr_t) (vm1 + 1) - (caddr_t) &vm1->vm_startcopy);
2444 new_map = &vm2->vm_map; /* XXX */
2445 new_map->timestamp = 1;
2447 old_entry = old_map->header.next;
2449 while (old_entry != &old_map->header) {
2450 if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2451 panic("vm_map_fork: encountered a submap");
2453 switch (old_entry->inheritance) {
2454 case VM_INHERIT_NONE:
2457 case VM_INHERIT_SHARE:
2459 * Clone the entry, creating the shared object if necessary.
2461 object = old_entry->object.vm_object;
2462 if (object == NULL) {
2463 object = vm_object_allocate(OBJT_DEFAULT,
2464 atop(old_entry->end - old_entry->start));
2465 old_entry->object.vm_object = object;
2466 old_entry->offset = (vm_offset_t) 0;
2470 * Add the reference before calling vm_object_shadow
2471 * to insure that a shadow object is created.
2473 vm_object_reference(object);
2474 if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
2475 vm_object_shadow(&old_entry->object.vm_object,
2477 atop(old_entry->end - old_entry->start));
2478 old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
2479 /* Transfer the second reference too. */
2480 vm_object_reference(
2481 old_entry->object.vm_object);
2482 vm_object_deallocate(object);
2483 object = old_entry->object.vm_object;
2485 vm_object_clear_flag(object, OBJ_ONEMAPPING);
2488 * Clone the entry, referencing the shared object.
2490 new_entry = vm_map_entry_create(new_map);
2491 *new_entry = *old_entry;
2492 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2493 new_entry->wired_count = 0;
2496 * Insert the entry into the new map -- we know we're
2497 * inserting at the end of the new map.
2500 vm_map_entry_link(new_map, new_map->header.prev,
2504 * Update the physical map
2507 pmap_copy(new_map->pmap, old_map->pmap,
2509 (old_entry->end - old_entry->start),
2513 case VM_INHERIT_COPY:
2515 * Clone the entry and link into the map.
2517 new_entry = vm_map_entry_create(new_map);
2518 *new_entry = *old_entry;
2519 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2520 new_entry->wired_count = 0;
2521 new_entry->object.vm_object = NULL;
2522 vm_map_entry_link(new_map, new_map->header.prev,
2524 vm_map_copy_entry(old_map, new_map, old_entry,
2528 old_entry = old_entry->next;
2531 new_map->size = old_map->size;
2532 old_map->infork = 0;
2533 vm_map_unlock(old_map);
2539 vm_map_stack (vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
2540 vm_prot_t prot, vm_prot_t max, int cow)
2542 vm_map_entry_t prev_entry;
2543 vm_map_entry_t new_stack_entry;
2544 vm_size_t init_ssize;
2547 if (VM_MIN_ADDRESS > 0 && addrbos < VM_MIN_ADDRESS)
2548 return (KERN_NO_SPACE);
2550 if (max_ssize < sgrowsiz)
2551 init_ssize = max_ssize;
2553 init_ssize = sgrowsiz;
2557 /* If addr is already mapped, no go */
2558 if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
2560 return (KERN_NO_SPACE);
2563 /* If we would blow our VMEM resource limit, no go */
2564 if (map->size + init_ssize >
2565 curproc->p_rlimit[RLIMIT_VMEM].rlim_cur) {
2567 return (KERN_NO_SPACE);
2570 /* If we can't accomodate max_ssize in the current mapping,
2571 * no go. However, we need to be aware that subsequent user
2572 * mappings might map into the space we have reserved for
2573 * stack, and currently this space is not protected.
2575 * Hopefully we will at least detect this condition
2576 * when we try to grow the stack.
2578 if ((prev_entry->next != &map->header) &&
2579 (prev_entry->next->start < addrbos + max_ssize)) {
2581 return (KERN_NO_SPACE);
2584 /* We initially map a stack of only init_ssize. We will
2585 * grow as needed later. Since this is to be a grow
2586 * down stack, we map at the top of the range.
2588 * Note: we would normally expect prot and max to be
2589 * VM_PROT_ALL, and cow to be 0. Possibly we should
2590 * eliminate these as input parameters, and just
2591 * pass these values here in the insert call.
2593 rv = vm_map_insert(map, NULL, 0, addrbos + max_ssize - init_ssize,
2594 addrbos + max_ssize, prot, max, cow);
2596 /* Now set the avail_ssize amount */
2597 if (rv == KERN_SUCCESS){
2598 if (prev_entry != &map->header)
2599 vm_map_clip_end(map, prev_entry, addrbos + max_ssize - init_ssize);
2600 new_stack_entry = prev_entry->next;
2601 if (new_stack_entry->end != addrbos + max_ssize ||
2602 new_stack_entry->start != addrbos + max_ssize - init_ssize)
2603 panic ("Bad entry start/end for new stack entry");
2605 new_stack_entry->avail_ssize = max_ssize - init_ssize;
2612 /* Attempts to grow a vm stack entry. Returns KERN_SUCCESS if the
2613 * desired address is already mapped, or if we successfully grow
2614 * the stack. Also returns KERN_SUCCESS if addr is outside the
2615 * stack range (this is strange, but preserves compatibility with
2616 * the grow function in vm_machdep.c).
2619 vm_map_growstack (struct proc *p, vm_offset_t addr)
2621 vm_map_entry_t prev_entry;
2622 vm_map_entry_t stack_entry;
2623 vm_map_entry_t new_stack_entry;
2624 struct vmspace *vm = p->p_vmspace;
2625 vm_map_t map = &vm->vm_map;
2628 int rv = KERN_SUCCESS;
2630 int use_read_lock = 1;
2634 vm_map_lock_read(map);
2638 /* If addr is already in the entry range, no need to grow.*/
2639 if (vm_map_lookup_entry(map, addr, &prev_entry))
2642 if ((stack_entry = prev_entry->next) == &map->header)
2644 if (prev_entry == &map->header)
2645 end = stack_entry->start - stack_entry->avail_ssize;
2647 end = prev_entry->end;
2649 /* This next test mimics the old grow function in vm_machdep.c.
2650 * It really doesn't quite make sense, but we do it anyway
2651 * for compatibility.
2653 * If not growable stack, return success. This signals the
2654 * caller to proceed as he would normally with normal vm.
2656 if (stack_entry->avail_ssize < 1 ||
2657 addr >= stack_entry->start ||
2658 addr < stack_entry->start - stack_entry->avail_ssize) {
2662 /* Find the minimum grow amount */
2663 grow_amount = roundup (stack_entry->start - addr, PAGE_SIZE);
2664 if (grow_amount > stack_entry->avail_ssize) {
2669 /* If there is no longer enough space between the entries
2670 * nogo, and adjust the available space. Note: this
2671 * should only happen if the user has mapped into the
2672 * stack area after the stack was created, and is
2673 * probably an error.
2675 * This also effectively destroys any guard page the user
2676 * might have intended by limiting the stack size.
2678 if (grow_amount > stack_entry->start - end) {
2679 if (use_read_lock && vm_map_lock_upgrade(map)) {
2684 stack_entry->avail_ssize = stack_entry->start - end;
2689 is_procstack = addr >= (vm_offset_t)vm->vm_maxsaddr;
2691 /* If this is the main process stack, see if we're over the
2694 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount >
2695 p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
2700 /* Round up the grow amount modulo SGROWSIZ */
2701 grow_amount = roundup (grow_amount, sgrowsiz);
2702 if (grow_amount > stack_entry->avail_ssize) {
2703 grow_amount = stack_entry->avail_ssize;
2705 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount >
2706 p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
2707 grow_amount = p->p_rlimit[RLIMIT_STACK].rlim_cur -
2711 /* If we would blow our VMEM resource limit, no go */
2712 if (map->size + grow_amount >
2713 curproc->p_rlimit[RLIMIT_VMEM].rlim_cur) {
2718 if (use_read_lock && vm_map_lock_upgrade(map)) {
2724 /* Get the preliminary new entry start value */
2725 addr = stack_entry->start - grow_amount;
2727 /* If this puts us into the previous entry, cut back our growth
2728 * to the available space. Also, see the note above.
2731 stack_entry->avail_ssize = stack_entry->start - end;
2735 rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start,
2740 /* Adjust the available stack space by the amount we grew. */
2741 if (rv == KERN_SUCCESS) {
2742 if (prev_entry != &map->header)
2743 vm_map_clip_end(map, prev_entry, addr);
2744 new_stack_entry = prev_entry->next;
2745 if (new_stack_entry->end != stack_entry->start ||
2746 new_stack_entry->start != addr)
2747 panic ("Bad stack grow start/end in new stack entry");
2749 new_stack_entry->avail_ssize = stack_entry->avail_ssize -
2750 (new_stack_entry->end -
2751 new_stack_entry->start);
2753 vm->vm_ssize += btoc(new_stack_entry->end -
2754 new_stack_entry->start);
2760 vm_map_unlock_read(map);
2767 * Unshare the specified VM space for exec. If other processes are
2768 * mapped to it, then create a new one. The new vmspace is null.
2772 vmspace_exec(struct proc *p) {
2773 struct vmspace *oldvmspace = p->p_vmspace;
2774 struct vmspace *newvmspace;
2775 vm_map_t map = &p->p_vmspace->vm_map;
2777 newvmspace = vmspace_alloc(map->min_offset, map->max_offset);
2778 bcopy(&oldvmspace->vm_startcopy, &newvmspace->vm_startcopy,
2779 (caddr_t) (newvmspace + 1) - (caddr_t) &newvmspace->vm_startcopy);
2781 * This code is written like this for prototype purposes. The
2782 * goal is to avoid running down the vmspace here, but let the
2783 * other process's that are still using the vmspace to finally
2784 * run it down. Even though there is little or no chance of blocking
2785 * here, it is a good idea to keep this form for future mods.
2787 vmspace_free(oldvmspace);
2788 p->p_vmspace = newvmspace;
2789 pmap_pinit2(vmspace_pmap(newvmspace));
2795 * Unshare the specified VM space for forcing COW. This
2796 * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
2800 vmspace_unshare(struct proc *p) {
2801 struct vmspace *oldvmspace = p->p_vmspace;
2802 struct vmspace *newvmspace;
2804 if (oldvmspace->vm_refcnt == 1)
2806 newvmspace = vmspace_fork(oldvmspace);
2807 vmspace_free(oldvmspace);
2808 p->p_vmspace = newvmspace;
2809 pmap_pinit2(vmspace_pmap(newvmspace));
2818 * Finds the VM object, offset, and
2819 * protection for a given virtual address in the
2820 * specified map, assuming a page fault of the
2823 * Leaves the map in question locked for read; return
2824 * values are guaranteed until a vm_map_lookup_done
2825 * call is performed. Note that the map argument
2826 * is in/out; the returned map must be used in
2827 * the call to vm_map_lookup_done.
2829 * A handle (out_entry) is returned for use in
2830 * vm_map_lookup_done, to make that fast.
2832 * If a lookup is requested with "write protection"
2833 * specified, the map may be changed to perform virtual
2834 * copying operations, although the data referenced will
2838 vm_map_lookup(vm_map_t *var_map, /* IN/OUT */
2840 vm_prot_t fault_typea,
2841 vm_map_entry_t *out_entry, /* OUT */
2842 vm_object_t *object, /* OUT */
2843 vm_pindex_t *pindex, /* OUT */
2844 vm_prot_t *out_prot, /* OUT */
2845 boolean_t *wired) /* OUT */
2847 vm_map_entry_t entry;
2848 vm_map_t map = *var_map;
2850 vm_prot_t fault_type = fault_typea;
2851 int use_read_lock = 1;
2852 int rv = KERN_SUCCESS;
2856 vm_map_lock_read(map);
2861 * If the map has an interesting hint, try it before calling full
2862 * blown lookup routine.
2867 if ((entry == &map->header) ||
2868 (vaddr < entry->start) || (vaddr >= entry->end)) {
2869 vm_map_entry_t tmp_entry;
2872 * Entry was either not a valid hint, or the vaddr was not
2873 * contained in the entry, so do a full lookup.
2875 if (!vm_map_lookup_entry(map, vaddr, &tmp_entry)) {
2876 rv = KERN_INVALID_ADDRESS;
2888 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
2889 vm_map_t old_map = map;
2891 *var_map = map = entry->object.sub_map;
2893 vm_map_unlock_read(old_map);
2895 vm_map_unlock(old_map);
2901 * Check whether this task is allowed to have this page.
2902 * Note the special case for MAP_ENTRY_COW
2903 * pages with an override. This is to implement a forced
2904 * COW for debuggers.
2907 if (fault_type & VM_PROT_OVERRIDE_WRITE)
2908 prot = entry->max_protection;
2910 prot = entry->protection;
2912 fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
2913 if ((fault_type & prot) != fault_type) {
2914 rv = KERN_PROTECTION_FAILURE;
2918 if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
2919 (entry->eflags & MAP_ENTRY_COW) &&
2920 (fault_type & VM_PROT_WRITE) &&
2921 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
2922 rv = KERN_PROTECTION_FAILURE;
2927 * If this page is not pageable, we have to get it for all possible
2931 *wired = (entry->wired_count != 0);
2933 prot = fault_type = entry->protection;
2936 * If the entry was copy-on-write, we either ...
2939 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
2941 * If we want to write the page, we may as well handle that
2942 * now since we've got the map locked.
2944 * If we don't need to write the page, we just demote the
2945 * permissions allowed.
2948 if (fault_type & VM_PROT_WRITE) {
2950 * Make a new object, and place it in the object
2951 * chain. Note that no new references have appeared
2952 * -- one just moved from the map to the new
2956 if (use_read_lock && vm_map_lock_upgrade(map)) {
2963 &entry->object.vm_object,
2965 atop(entry->end - entry->start));
2967 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
2970 * We're attempting to read a copy-on-write page --
2971 * don't allow writes.
2974 prot &= ~VM_PROT_WRITE;
2979 * Create an object if necessary.
2981 if (entry->object.vm_object == NULL &&
2983 if (use_read_lock && vm_map_lock_upgrade(map)) {
2988 entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
2989 atop(entry->end - entry->start));
2994 * Return the object/offset from this entry. If the entry was
2995 * copy-on-write or empty, it has been fixed up.
2998 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
2999 *object = entry->object.vm_object;
3002 * Return whether this is the only map sharing this data. On
3003 * success we return with a read lock held on the map. On failure
3004 * we return with the map unlocked.
3008 if (rv == KERN_SUCCESS) {
3009 if (use_read_lock == 0)
3010 vm_map_lock_downgrade(map);
3011 } else if (use_read_lock) {
3012 vm_map_unlock_read(map);
3020 * vm_map_lookup_done:
3022 * Releases locks acquired by a vm_map_lookup
3023 * (according to the handle returned by that lookup).
3027 vm_map_lookup_done(map, entry)
3029 vm_map_entry_t entry;
3032 * Unlock the main-level map
3035 vm_map_unlock_read(map);
3039 * Implement uiomove with VM operations. This handles (and collateral changes)
3040 * support every combination of source object modification, and COW type
3044 vm_uiomove(mapa, srcobject, cp, cnta, uaddra, npages)
3046 vm_object_t srcobject;
3053 vm_object_t first_object, oldobject, object;
3054 vm_map_entry_t entry;
3058 vm_offset_t uaddr, start, end, tend;
3059 vm_pindex_t first_pindex, osize, oindex;
3072 if ((vm_map_lookup(&map, uaddr,
3073 VM_PROT_READ, &entry, &first_object,
3074 &first_pindex, &prot, &wired)) != KERN_SUCCESS) {
3078 vm_map_clip_start(map, entry, uaddr);
3081 tend = uaddr + tcnt;
3082 if (tend > entry->end) {
3083 tcnt = entry->end - uaddr;
3087 vm_map_clip_end(map, entry, tend);
3089 start = entry->start;
3094 oindex = OFF_TO_IDX(cp);
3097 for (idx = 0; idx < osize; idx++) {
3099 if ((m = vm_page_lookup(srcobject, oindex + idx)) == NULL) {
3100 vm_map_lookup_done(map, entry);
3104 * disallow busy or invalid pages, but allow
3105 * m->busy pages if they are entirely valid.
3107 if ((m->flags & PG_BUSY) ||
3108 ((m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL)) {
3109 vm_map_lookup_done(map, entry);
3116 * If we are changing an existing map entry, just redirect
3117 * the object, and change mappings.
3119 if ((first_object->type == OBJT_VNODE) &&
3120 ((oldobject = entry->object.vm_object) == first_object)) {
3122 if ((entry->offset != cp) || (oldobject != srcobject)) {
3124 * Remove old window into the file
3126 pmap_remove (map->pmap, uaddr, tend);
3129 * Force copy on write for mmaped regions
3131 vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
3134 * Point the object appropriately
3136 if (oldobject != srcobject) {
3139 * Set the object optimization hint flag
3141 vm_object_set_flag(srcobject, OBJ_OPT);
3142 vm_object_reference(srcobject);
3143 entry->object.vm_object = srcobject;
3146 vm_object_deallocate(oldobject);
3153 pmap_remove (map->pmap, uaddr, tend);
3156 } else if ((first_object->ref_count == 1) &&
3157 (first_object->size == osize) &&
3158 ((first_object->type == OBJT_DEFAULT) ||
3159 (first_object->type == OBJT_SWAP)) ) {
3161 oldobject = first_object->backing_object;
3163 if ((first_object->backing_object_offset != cp) ||
3164 (oldobject != srcobject)) {
3166 * Remove old window into the file
3168 pmap_remove (map->pmap, uaddr, tend);
3171 * Remove unneeded old pages
3173 vm_object_page_remove(first_object, 0, 0, 0);
3176 * Invalidate swap space
3178 if (first_object->type == OBJT_SWAP) {
3179 swap_pager_freespace(first_object,
3181 first_object->size);
3185 * Force copy on write for mmaped regions
3187 vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
3190 * Point the object appropriately
3192 if (oldobject != srcobject) {
3195 * Set the object optimization hint flag
3197 vm_object_set_flag(srcobject, OBJ_OPT);
3198 vm_object_reference(srcobject);
3202 first_object, shadow_list);
3203 oldobject->shadow_count--;
3204 /* XXX bump generation? */
3205 vm_object_deallocate(oldobject);
3208 LIST_INSERT_HEAD(&srcobject->shadow_head,
3209 first_object, shadow_list);
3210 srcobject->shadow_count++;
3211 /* XXX bump generation? */
3213 first_object->backing_object = srcobject;
3215 first_object->backing_object_offset = cp;
3218 pmap_remove (map->pmap, uaddr, tend);
3221 * Otherwise, we have to do a logical mmap.
3225 vm_object_set_flag(srcobject, OBJ_OPT);
3226 vm_object_reference(srcobject);
3228 pmap_remove (map->pmap, uaddr, tend);
3230 vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
3231 vm_map_lock_upgrade(map);
3233 if (entry == &map->header) {
3234 map->first_free = &map->header;
3235 } else if (map->first_free->start >= start) {
3236 map->first_free = entry->prev;
3239 SAVE_HINT(map, entry->prev);
3240 vm_map_entry_delete(map, entry);
3245 rv = vm_map_insert(map, object, ooffset, start, tend,
3246 VM_PROT_ALL, VM_PROT_ALL, MAP_COPY_ON_WRITE);
3248 if (rv != KERN_SUCCESS)
3249 panic("vm_uiomove: could not insert new entry: %d", rv);
3253 * Map the window directly, if it is already in memory
3255 pmap_object_init_pt(map->pmap, uaddr,
3256 srcobject, oindex, tcnt, 0);
3271 * Performs the copy_on_write operations necessary to allow the virtual copies
3272 * into user space to work. This has to be called for write(2) system calls
3273 * from other processes, file unlinking, and file size shrinkage.
3276 vm_freeze_copyopts(object, froma, toa)
3278 vm_pindex_t froma, toa;
3281 vm_object_t robject;
3284 if ((object == NULL) ||
3285 ((object->flags & OBJ_OPT) == 0))
3288 if (object->shadow_count > object->ref_count)
3289 panic("vm_freeze_copyopts: sc > rc");
3291 while((robject = LIST_FIRST(&object->shadow_head)) != NULL) {
3292 vm_pindex_t bo_pindex;
3293 vm_page_t m_in, m_out;
3295 bo_pindex = OFF_TO_IDX(robject->backing_object_offset);
3297 vm_object_reference(robject);
3299 vm_object_pip_wait(robject, "objfrz");
3301 if (robject->ref_count == 1) {
3302 vm_object_deallocate(robject);
3306 vm_object_pip_add(robject, 1);
3308 for (idx = 0; idx < robject->size; idx++) {
3310 m_out = vm_page_grab(robject, idx,
3311 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
3313 if (m_out->valid == 0) {
3314 m_in = vm_page_grab(object, bo_pindex + idx,
3315 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
3316 if (m_in->valid == 0) {
3317 rv = vm_pager_get_pages(object, &m_in, 1, 0);
3318 if (rv != VM_PAGER_OK) {
3319 printf("vm_freeze_copyopts: cannot read page from file: %lx\n", (long)m_in->pindex);
3322 vm_page_deactivate(m_in);
3325 vm_page_protect(m_in, VM_PROT_NONE);
3326 pmap_copy_page(VM_PAGE_TO_PHYS(m_in), VM_PAGE_TO_PHYS(m_out));
3327 m_out->valid = m_in->valid;
3328 vm_page_dirty(m_out);
3329 vm_page_activate(m_out);
3330 vm_page_wakeup(m_in);
3332 vm_page_wakeup(m_out);
3335 object->shadow_count--;
3336 object->ref_count--;
3337 LIST_REMOVE(robject, shadow_list);
3338 robject->backing_object = NULL;
3339 robject->backing_object_offset = 0;
3341 vm_object_pip_wakeup(robject);
3342 vm_object_deallocate(robject);
3345 vm_object_clear_flag(object, OBJ_OPT);
3348 #include "opt_ddb.h"
3350 #include <sys/kernel.h>
3352 #include <ddb/ddb.h>
3355 * vm_map_print: [ debug ]
3357 DB_SHOW_COMMAND(map, vm_map_print)
3360 /* XXX convert args. */
3361 vm_map_t map = (vm_map_t)addr;
3362 boolean_t full = have_addr;
3364 vm_map_entry_t entry;
3366 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
3368 (void *)map->pmap, map->nentries, map->timestamp);
3371 if (!full && db_indent)
3375 for (entry = map->header.next; entry != &map->header;
3376 entry = entry->next) {
3377 db_iprintf("map entry %p: start=%p, end=%p\n",
3378 (void *)entry, (void *)entry->start, (void *)entry->end);
3381 static char *inheritance_name[4] =
3382 {"share", "copy", "none", "donate_copy"};
3384 db_iprintf(" prot=%x/%x/%s",
3386 entry->max_protection,
3387 inheritance_name[(int)(unsigned char)entry->inheritance]);
3388 if (entry->wired_count != 0)
3389 db_printf(", wired");
3391 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3392 /* XXX no %qd in kernel. Truncate entry->offset. */
3393 db_printf(", share=%p, offset=0x%lx\n",
3394 (void *)entry->object.sub_map,
3395 (long)entry->offset);
3397 if ((entry->prev == &map->header) ||
3398 (entry->prev->object.sub_map !=
3399 entry->object.sub_map)) {
3401 vm_map_print((db_expr_t)(intptr_t)
3402 entry->object.sub_map,
3403 full, 0, (char *)0);
3407 /* XXX no %qd in kernel. Truncate entry->offset. */
3408 db_printf(", object=%p, offset=0x%lx",
3409 (void *)entry->object.vm_object,
3410 (long)entry->offset);
3411 if (entry->eflags & MAP_ENTRY_COW)
3412 db_printf(", copy (%s)",
3413 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
3417 if ((entry->prev == &map->header) ||
3418 (entry->prev->object.vm_object !=
3419 entry->object.vm_object)) {
3421 vm_object_print((db_expr_t)(intptr_t)
3422 entry->object.vm_object,
3423 full, 0, (char *)0);
3435 DB_SHOW_COMMAND(procvm, procvm)
3440 p = (struct proc *) addr;
3445 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
3446 (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
3447 (void *)vmspace_pmap(p->p_vmspace));
3449 vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);