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.
68 * Virtual memory mapping module.
71 #include <sys/param.h>
72 #include <sys/systm.h>
75 #include <sys/mutex.h>
77 #include <sys/vmmeter.h>
79 #include <sys/vnode.h>
80 #include <sys/resourcevar.h>
83 #include <vm/vm_param.h>
85 #include <vm/vm_map.h>
86 #include <vm/vm_page.h>
87 #include <vm/vm_object.h>
88 #include <vm/vm_pager.h>
89 #include <vm/vm_kern.h>
90 #include <vm/vm_extern.h>
91 #include <vm/swap_pager.h>
95 * Virtual memory maps provide for the mapping, protection,
96 * and sharing of virtual memory objects. In addition,
97 * this module provides for an efficient virtual copy of
98 * memory from one map to another.
100 * Synchronization is required prior to most operations.
102 * Maps consist of an ordered doubly-linked list of simple
103 * entries; a single hint is used to speed up lookups.
105 * Since portions of maps are specified by start/end addresses,
106 * which may not align with existing map entries, all
107 * routines merely "clip" entries to these start/end values.
108 * [That is, an entry is split into two, bordering at a
109 * start or end value.] Note that these clippings may not
110 * always be necessary (as the two resulting entries are then
111 * not changed); however, the clipping is done for convenience.
113 * As mentioned above, virtual copy operations are performed
114 * by copying VM object references from one map to
115 * another, and then marking both regions as copy-on-write.
121 * Initialize the vm_map module. Must be called before
122 * any other vm_map routines.
124 * Map and entry structures are allocated from the general
125 * purpose memory pool with some exceptions:
127 * - The kernel map and kmem submap are allocated statically.
128 * - Kernel map entries are allocated out of a static pool.
130 * These restrictions are necessary since malloc() uses the
131 * maps and requires map entries.
134 static uma_zone_t mapentzone;
135 static uma_zone_t kmapentzone;
136 static uma_zone_t mapzone;
137 static uma_zone_t vmspace_zone;
138 static struct vm_object kmapentobj;
139 static void vmspace_zinit(void *mem, int size);
140 static void vmspace_zfini(void *mem, int size);
141 static void vm_map_zinit(void *mem, int size);
142 static void vm_map_zfini(void *mem, int size);
143 static void _vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max);
146 static void vm_map_zdtor(void *mem, int size, void *arg);
147 static void vmspace_zdtor(void *mem, int size, void *arg);
153 mapzone = uma_zcreate("MAP", sizeof(struct vm_map), NULL,
159 vm_map_zinit, vm_map_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
160 uma_prealloc(mapzone, MAX_KMAP);
161 kmapentzone = uma_zcreate("KMAP ENTRY", sizeof(struct vm_map_entry),
162 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
163 UMA_ZONE_MTXCLASS | UMA_ZONE_VM);
164 uma_prealloc(kmapentzone, MAX_KMAPENT);
165 mapentzone = uma_zcreate("MAP ENTRY", sizeof(struct vm_map_entry),
166 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
167 uma_prealloc(mapentzone, MAX_MAPENT);
171 vmspace_zfini(void *mem, int size)
175 vm = (struct vmspace *)mem;
177 vm_map_zfini(&vm->vm_map, sizeof(vm->vm_map));
181 vmspace_zinit(void *mem, int size)
185 vm = (struct vmspace *)mem;
187 vm_map_zinit(&vm->vm_map, sizeof(vm->vm_map));
191 vm_map_zfini(void *mem, int size)
197 lockdestroy(&map->lock);
201 vm_map_zinit(void *mem, int size)
209 lockinit(&map->lock, PVM, "thrd_sleep", 0, LK_NOPAUSE);
214 vmspace_zdtor(void *mem, int size, void *arg)
218 vm = (struct vmspace *)mem;
220 vm_map_zdtor(&vm->vm_map, sizeof(vm->vm_map), arg);
223 vm_map_zdtor(void *mem, int size, void *arg)
228 KASSERT(map->nentries == 0,
229 ("map %p nentries == %d on free.",
230 map, map->nentries));
231 KASSERT(map->size == 0,
232 ("map %p size == %lu on free.",
233 map, (unsigned long)map->size));
234 KASSERT(map->infork == 0,
235 ("map %p infork == %d on free.",
238 #endif /* INVARIANTS */
241 * Allocate a vmspace structure, including a vm_map and pmap,
242 * and initialize those structures. The refcnt is set to 1.
243 * The remaining fields must be initialized by the caller.
246 vmspace_alloc(min, max)
247 vm_offset_t min, max;
252 vm = uma_zalloc(vmspace_zone, M_WAITOK);
253 CTR1(KTR_VM, "vmspace_alloc: %p", vm);
254 _vm_map_init(&vm->vm_map, min, max);
255 pmap_pinit(vmspace_pmap(vm));
256 vm->vm_map.pmap = vmspace_pmap(vm); /* XXX */
266 uma_zone_set_obj(kmapentzone, &kmapentobj, lmin(cnt.v_page_count,
267 (VM_MAX_KERNEL_ADDRESS - KERNBASE) / PAGE_SIZE) / 8);
268 vmspace_zone = uma_zcreate("VMSPACE", sizeof(struct vmspace), NULL,
274 vmspace_zinit, vmspace_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
280 vmspace_dofree(struct vmspace *vm)
282 CTR1(KTR_VM, "vmspace_free: %p", vm);
284 * Lock the map, to wait out all other references to it.
285 * Delete all of the mappings and pages they hold, then call
286 * the pmap module to reclaim anything left.
288 vm_map_lock(&vm->vm_map);
289 (void) vm_map_delete(&vm->vm_map, vm->vm_map.min_offset,
290 vm->vm_map.max_offset);
291 vm_map_unlock(&vm->vm_map);
293 pmap_release(vmspace_pmap(vm));
294 uma_zfree(vmspace_zone, vm);
298 vmspace_free(struct vmspace *vm)
302 if (vm->vm_refcnt == 0)
303 panic("vmspace_free: attempt to free already freed vmspace");
305 if (--vm->vm_refcnt == 0)
310 vmspace_exitfree(struct proc *p)
315 if (p == p->p_vmspace->vm_freer) {
323 * vmspace_swap_count() - count the approximate swap useage in pages for a
326 * Swap useage is determined by taking the proportional swap used by
327 * VM objects backing the VM map. To make up for fractional losses,
328 * if the VM object has any swap use at all the associated map entries
329 * count for at least 1 swap page.
332 vmspace_swap_count(struct vmspace *vmspace)
334 vm_map_t map = &vmspace->vm_map;
338 vm_map_lock_read(map);
339 for (cur = map->header.next; cur != &map->header; cur = cur->next) {
342 if ((cur->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
343 (object = cur->object.vm_object) != NULL &&
344 object->type == OBJT_SWAP
346 int n = (cur->end - cur->start) / PAGE_SIZE;
348 if (object->un_pager.swp.swp_bcount) {
349 count += object->un_pager.swp.swp_bcount *
350 SWAP_META_PAGES * n / object->size + 1;
354 vm_map_unlock_read(map);
359 _vm_map_lock(vm_map_t map, const char *file, int line)
365 error = lockmgr(&map->lock, LK_EXCLUSIVE, NULL, curthread);
366 KASSERT(error == 0, ("%s: failed to get lock", __func__));
371 _vm_map_unlock(vm_map_t map, const char *file, int line)
374 lockmgr(&map->lock, LK_RELEASE, NULL, curthread);
378 _vm_map_lock_read(vm_map_t map, const char *file, int line)
384 error = lockmgr(&map->lock, LK_EXCLUSIVE, NULL, curthread);
385 KASSERT(error == 0, ("%s: failed to get lock", __func__));
389 _vm_map_unlock_read(vm_map_t map, const char *file, int line)
392 lockmgr(&map->lock, LK_RELEASE, NULL, curthread);
396 _vm_map_trylock(vm_map_t map, const char *file, int line)
402 error = lockmgr(&map->lock, LK_EXCLUSIVE | LK_NOWAIT, NULL, curthread);
407 _vm_map_lock_upgrade(vm_map_t map, const char *file, int line)
410 KASSERT(lockstatus(&map->lock, curthread) == LK_EXCLUSIVE,
411 ("%s: lock not held", __func__));
417 _vm_map_lock_downgrade(vm_map_t map, const char *file, int line)
420 KASSERT(lockstatus(&map->lock, curthread) == LK_EXCLUSIVE,
421 ("%s: lock not held", __func__));
425 * vm_map_unlock_and_wait:
428 vm_map_unlock_and_wait(vm_map_t map, boolean_t user_wait)
434 retval = tsleep(&map->root, PVM, "vmmapw", 0);
443 vm_map_wakeup(vm_map_t map)
447 * Acquire and release Giant to prevent a wakeup() from being
448 * performed (and lost) between the vm_map_unlock() and the
449 * tsleep() in vm_map_unlock_and_wait().
457 vmspace_resident_count(struct vmspace *vmspace)
459 return pmap_resident_count(vmspace_pmap(vmspace));
465 * Creates and returns a new empty VM map with
466 * the given physical map structure, and having
467 * the given lower and upper address bounds.
470 vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max)
474 result = uma_zalloc(mapzone, M_WAITOK);
475 CTR1(KTR_VM, "vm_map_create: %p", result);
476 _vm_map_init(result, min, max);
482 * Initialize an existing vm_map structure
483 * such as that in the vmspace structure.
484 * The pmap is set elsewhere.
487 _vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max)
490 map->header.next = map->header.prev = &map->header;
491 map->needs_wakeup = FALSE;
493 map->min_offset = min;
494 map->max_offset = max;
495 map->first_free = &map->header;
501 vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max)
503 _vm_map_init(map, min, max);
504 lockinit(&map->lock, PVM, "thrd_sleep", 0, LK_NOPAUSE);
508 * vm_map_entry_dispose: [ internal use only ]
510 * Inverse of vm_map_entry_create.
513 vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry)
515 uma_zfree(map->system_map ? kmapentzone : mapentzone, entry);
519 * vm_map_entry_create: [ internal use only ]
521 * Allocates a VM map entry for insertion.
522 * No entry fields are filled in.
524 static vm_map_entry_t
525 vm_map_entry_create(vm_map_t map)
527 vm_map_entry_t new_entry;
530 new_entry = uma_zalloc(kmapentzone, M_NOWAIT);
532 new_entry = uma_zalloc(mapentzone, M_WAITOK);
533 if (new_entry == NULL)
534 panic("vm_map_entry_create: kernel resources exhausted");
539 * vm_map_entry_set_behavior:
541 * Set the expected access behavior, either normal, random, or
545 vm_map_entry_set_behavior(vm_map_entry_t entry, u_char behavior)
547 entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) |
548 (behavior & MAP_ENTRY_BEHAV_MASK);
552 * vm_map_entry_splay:
554 * Implements Sleator and Tarjan's top-down splay algorithm. Returns
555 * the vm_map_entry containing the given address. If, however, that
556 * address is not found in the vm_map, returns a vm_map_entry that is
557 * adjacent to the address, coming before or after it.
559 static vm_map_entry_t
560 vm_map_entry_splay(vm_offset_t address, vm_map_entry_t root)
562 struct vm_map_entry dummy;
563 vm_map_entry_t lefttreemax, righttreemin, y;
567 lefttreemax = righttreemin = &dummy;
569 if (address < root->start) {
570 if ((y = root->left) == NULL)
572 if (address < y->start) {
574 root->left = y->right;
577 if ((y = root->left) == NULL)
580 /* Link into the new root's right tree. */
581 righttreemin->left = root;
583 } else if (address >= root->end) {
584 if ((y = root->right) == NULL)
586 if (address >= y->end) {
588 root->right = y->left;
591 if ((y = root->right) == NULL)
594 /* Link into the new root's left tree. */
595 lefttreemax->right = root;
600 /* Assemble the new root. */
601 lefttreemax->right = root->left;
602 righttreemin->left = root->right;
603 root->left = dummy.right;
604 root->right = dummy.left;
609 * vm_map_entry_{un,}link:
611 * Insert/remove entries from maps.
614 vm_map_entry_link(vm_map_t map,
615 vm_map_entry_t after_where,
616 vm_map_entry_t entry)
620 "vm_map_entry_link: map %p, nentries %d, entry %p, after %p", map,
621 map->nentries, entry, after_where);
623 entry->prev = after_where;
624 entry->next = after_where->next;
625 entry->next->prev = entry;
626 after_where->next = entry;
628 if (after_where != &map->header) {
629 if (after_where != map->root)
630 vm_map_entry_splay(after_where->start, map->root);
631 entry->right = after_where->right;
632 entry->left = after_where;
633 after_where->right = NULL;
635 entry->right = map->root;
642 vm_map_entry_unlink(vm_map_t map,
643 vm_map_entry_t entry)
645 vm_map_entry_t next, prev, root;
647 if (entry != map->root)
648 vm_map_entry_splay(entry->start, map->root);
649 if (entry->left == NULL)
652 root = vm_map_entry_splay(entry->start, entry->left);
653 root->right = entry->right;
662 CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map,
663 map->nentries, entry);
667 * vm_map_lookup_entry: [ internal use only ]
669 * Finds the map entry containing (or
670 * immediately preceding) the specified address
671 * in the given map; the entry is returned
672 * in the "entry" parameter. The boolean
673 * result indicates whether the address is
674 * actually contained in the map.
680 vm_map_entry_t *entry) /* OUT */
684 cur = vm_map_entry_splay(address, map->root);
686 *entry = &map->header;
690 if (address >= cur->start) {
692 if (cur->end > address)
703 * Inserts the given whole VM object into the target
704 * map at the specified address range. The object's
705 * size should match that of the address range.
707 * Requires that the map be locked, and leaves it so.
709 * If object is non-NULL, ref count must be bumped by caller
710 * prior to making call to account for the new entry.
713 vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
714 vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max,
717 vm_map_entry_t new_entry;
718 vm_map_entry_t prev_entry;
719 vm_map_entry_t temp_entry;
720 vm_eflags_t protoeflags;
723 * Check that the start and end points are not bogus.
725 if ((start < map->min_offset) || (end > map->max_offset) ||
727 return (KERN_INVALID_ADDRESS);
730 * Find the entry prior to the proposed starting address; if it's part
731 * of an existing entry, this range is bogus.
733 if (vm_map_lookup_entry(map, start, &temp_entry))
734 return (KERN_NO_SPACE);
736 prev_entry = temp_entry;
739 * Assert that the next entry doesn't overlap the end point.
741 if ((prev_entry->next != &map->header) &&
742 (prev_entry->next->start < end))
743 return (KERN_NO_SPACE);
747 if (cow & MAP_COPY_ON_WRITE)
748 protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
750 if (cow & MAP_NOFAULT) {
751 protoeflags |= MAP_ENTRY_NOFAULT;
753 KASSERT(object == NULL,
754 ("vm_map_insert: paradoxical MAP_NOFAULT request"));
756 if (cow & MAP_DISABLE_SYNCER)
757 protoeflags |= MAP_ENTRY_NOSYNC;
758 if (cow & MAP_DISABLE_COREDUMP)
759 protoeflags |= MAP_ENTRY_NOCOREDUMP;
763 * When object is non-NULL, it could be shared with another
764 * process. We have to set or clear OBJ_ONEMAPPING
768 if ((object->ref_count > 1) || (object->shadow_count != 0)) {
769 vm_object_clear_flag(object, OBJ_ONEMAPPING);
773 else if ((prev_entry != &map->header) &&
774 (prev_entry->eflags == protoeflags) &&
775 (prev_entry->end == start) &&
776 (prev_entry->wired_count == 0) &&
777 ((prev_entry->object.vm_object == NULL) ||
778 vm_object_coalesce(prev_entry->object.vm_object,
779 OFF_TO_IDX(prev_entry->offset),
780 (vm_size_t)(prev_entry->end - prev_entry->start),
781 (vm_size_t)(end - prev_entry->end)))) {
783 * We were able to extend the object. Determine if we
784 * can extend the previous map entry to include the
787 if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
788 (prev_entry->protection == prot) &&
789 (prev_entry->max_protection == max)) {
790 map->size += (end - prev_entry->end);
791 prev_entry->end = end;
792 vm_map_simplify_entry(map, prev_entry);
793 return (KERN_SUCCESS);
797 * If we can extend the object but cannot extend the
798 * map entry, we have to create a new map entry. We
799 * must bump the ref count on the extended object to
800 * account for it. object may be NULL.
802 object = prev_entry->object.vm_object;
803 offset = prev_entry->offset +
804 (prev_entry->end - prev_entry->start);
805 vm_object_reference(object);
809 * NOTE: if conditionals fail, object can be NULL here. This occurs
810 * in things like the buffer map where we manage kva but do not manage
817 new_entry = vm_map_entry_create(map);
818 new_entry->start = start;
819 new_entry->end = end;
821 new_entry->eflags = protoeflags;
822 new_entry->object.vm_object = object;
823 new_entry->offset = offset;
824 new_entry->avail_ssize = 0;
826 new_entry->inheritance = VM_INHERIT_DEFAULT;
827 new_entry->protection = prot;
828 new_entry->max_protection = max;
829 new_entry->wired_count = 0;
832 * Insert the new entry into the list
834 vm_map_entry_link(map, prev_entry, new_entry);
835 map->size += new_entry->end - new_entry->start;
838 * Update the free space hint
840 if ((map->first_free == prev_entry) &&
841 (prev_entry->end >= new_entry->start)) {
842 map->first_free = new_entry;
847 * Temporarily removed to avoid MAP_STACK panic, due to
848 * MAP_STACK being a huge hack. Will be added back in
849 * when MAP_STACK (and the user stack mapping) is fixed.
852 * It may be possible to simplify the entry
854 vm_map_simplify_entry(map, new_entry);
857 if (cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) {
859 pmap_object_init_pt(map->pmap, start,
860 object, OFF_TO_IDX(offset), end - start,
861 cow & MAP_PREFAULT_PARTIAL);
865 return (KERN_SUCCESS);
869 * Find sufficient space for `length' bytes in the given map, starting at
870 * `start'. The map must be locked. Returns 0 on success, 1 on no space.
879 vm_map_entry_t entry, next;
882 if (start < map->min_offset)
883 start = map->min_offset;
884 if (start > map->max_offset)
888 * Look for the first possible address; if there's already something
889 * at this address, we have to start after it.
891 if (start == map->min_offset) {
892 if ((entry = map->first_free) != &map->header)
897 if (vm_map_lookup_entry(map, start, &tmp))
903 * Look through the rest of the map, trying to fit a new region in the
904 * gap between existing regions, or after the very last region.
906 for (;; start = (entry = next)->end) {
908 * Find the end of the proposed new region. Be sure we didn't
909 * go beyond the end of the map, or wrap around the address;
910 * if so, we lose. Otherwise, if this is the last entry, or
911 * if the proposed new region fits before the next entry, we
914 end = start + length;
915 if (end > map->max_offset || end < start)
918 if (next == &map->header || next->start >= end)
922 if (map == kernel_map) {
924 if ((ksize = round_page(start + length)) > kernel_vm_end) {
926 pmap_growkernel(ksize);
934 * vm_map_find finds an unallocated region in the target address
935 * map with the given length. The search is defined to be
936 * first-fit from the specified address; the region found is
937 * returned in the same parameter.
939 * If object is non-NULL, ref count must be bumped by caller
940 * prior to making call to account for the new entry.
943 vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
944 vm_offset_t *addr, /* IN/OUT */
945 vm_size_t length, boolean_t find_space, vm_prot_t prot,
946 vm_prot_t max, int cow)
958 if (vm_map_findspace(map, start, length, addr)) {
962 return (KERN_NO_SPACE);
966 result = vm_map_insert(map, object, offset,
967 start, start + length, prot, max, cow);
977 * vm_map_simplify_entry:
979 * Simplify the given map entry by merging with either neighbor. This
980 * routine also has the ability to merge with both neighbors.
982 * The map must be locked.
984 * This routine guarentees that the passed entry remains valid (though
985 * possibly extended). When merging, this routine may delete one or
989 vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry)
991 vm_map_entry_t next, prev;
992 vm_size_t prevsize, esize;
994 if (entry->eflags & (MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP))
998 if (prev != &map->header) {
999 prevsize = prev->end - prev->start;
1000 if ( (prev->end == entry->start) &&
1001 (prev->object.vm_object == entry->object.vm_object) &&
1002 (!prev->object.vm_object ||
1003 (prev->offset + prevsize == entry->offset)) &&
1004 (prev->eflags == entry->eflags) &&
1005 (prev->protection == entry->protection) &&
1006 (prev->max_protection == entry->max_protection) &&
1007 (prev->inheritance == entry->inheritance) &&
1008 (prev->wired_count == entry->wired_count)) {
1009 if (map->first_free == prev)
1010 map->first_free = entry;
1011 vm_map_entry_unlink(map, prev);
1012 entry->start = prev->start;
1013 entry->offset = prev->offset;
1014 if (prev->object.vm_object)
1015 vm_object_deallocate(prev->object.vm_object);
1016 vm_map_entry_dispose(map, prev);
1021 if (next != &map->header) {
1022 esize = entry->end - entry->start;
1023 if ((entry->end == next->start) &&
1024 (next->object.vm_object == entry->object.vm_object) &&
1025 (!entry->object.vm_object ||
1026 (entry->offset + esize == next->offset)) &&
1027 (next->eflags == entry->eflags) &&
1028 (next->protection == entry->protection) &&
1029 (next->max_protection == entry->max_protection) &&
1030 (next->inheritance == entry->inheritance) &&
1031 (next->wired_count == entry->wired_count)) {
1032 if (map->first_free == next)
1033 map->first_free = entry;
1034 vm_map_entry_unlink(map, next);
1035 entry->end = next->end;
1036 if (next->object.vm_object)
1037 vm_object_deallocate(next->object.vm_object);
1038 vm_map_entry_dispose(map, next);
1043 * vm_map_clip_start: [ internal use only ]
1045 * Asserts that the given entry begins at or after
1046 * the specified address; if necessary,
1047 * it splits the entry into two.
1049 #define vm_map_clip_start(map, entry, startaddr) \
1051 if (startaddr > entry->start) \
1052 _vm_map_clip_start(map, entry, startaddr); \
1056 * This routine is called only when it is known that
1057 * the entry must be split.
1060 _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start)
1062 vm_map_entry_t new_entry;
1065 * Split off the front portion -- note that we must insert the new
1066 * entry BEFORE this one, so that this entry has the specified
1069 vm_map_simplify_entry(map, entry);
1072 * If there is no object backing this entry, we might as well create
1073 * one now. If we defer it, an object can get created after the map
1074 * is clipped, and individual objects will be created for the split-up
1075 * map. This is a bit of a hack, but is also about the best place to
1076 * put this improvement.
1078 if (entry->object.vm_object == NULL && !map->system_map) {
1080 object = vm_object_allocate(OBJT_DEFAULT,
1081 atop(entry->end - entry->start));
1082 entry->object.vm_object = object;
1086 new_entry = vm_map_entry_create(map);
1087 *new_entry = *entry;
1089 new_entry->end = start;
1090 entry->offset += (start - entry->start);
1091 entry->start = start;
1093 vm_map_entry_link(map, entry->prev, new_entry);
1095 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1096 vm_object_reference(new_entry->object.vm_object);
1101 * vm_map_clip_end: [ internal use only ]
1103 * Asserts that the given entry ends at or before
1104 * the specified address; if necessary,
1105 * it splits the entry into two.
1107 #define vm_map_clip_end(map, entry, endaddr) \
1109 if (endaddr < entry->end) \
1110 _vm_map_clip_end(map, entry, endaddr); \
1114 * This routine is called only when it is known that
1115 * the entry must be split.
1118 _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end)
1120 vm_map_entry_t new_entry;
1123 * If there is no object backing this entry, we might as well create
1124 * one now. If we defer it, an object can get created after the map
1125 * is clipped, and individual objects will be created for the split-up
1126 * map. This is a bit of a hack, but is also about the best place to
1127 * put this improvement.
1129 if (entry->object.vm_object == NULL && !map->system_map) {
1131 object = vm_object_allocate(OBJT_DEFAULT,
1132 atop(entry->end - entry->start));
1133 entry->object.vm_object = object;
1138 * Create a new entry and insert it AFTER the specified entry
1140 new_entry = vm_map_entry_create(map);
1141 *new_entry = *entry;
1143 new_entry->start = entry->end = end;
1144 new_entry->offset += (end - entry->start);
1146 vm_map_entry_link(map, entry, new_entry);
1148 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1149 vm_object_reference(new_entry->object.vm_object);
1154 * VM_MAP_RANGE_CHECK: [ internal use only ]
1156 * Asserts that the starting and ending region
1157 * addresses fall within the valid range of the map.
1159 #define VM_MAP_RANGE_CHECK(map, start, end) \
1161 if (start < vm_map_min(map)) \
1162 start = vm_map_min(map); \
1163 if (end > vm_map_max(map)) \
1164 end = vm_map_max(map); \
1170 * vm_map_submap: [ kernel use only ]
1172 * Mark the given range as handled by a subordinate map.
1174 * This range must have been created with vm_map_find,
1175 * and no other operations may have been performed on this
1176 * range prior to calling vm_map_submap.
1178 * Only a limited number of operations can be performed
1179 * within this rage after calling vm_map_submap:
1181 * [Don't try vm_map_copy!]
1183 * To remove a submapping, one must first remove the
1184 * range from the superior map, and then destroy the
1185 * submap (if desired). [Better yet, don't try it.]
1194 vm_map_entry_t entry;
1195 int result = KERN_INVALID_ARGUMENT;
1199 VM_MAP_RANGE_CHECK(map, start, end);
1201 if (vm_map_lookup_entry(map, start, &entry)) {
1202 vm_map_clip_start(map, entry, start);
1204 entry = entry->next;
1206 vm_map_clip_end(map, entry, end);
1208 if ((entry->start == start) && (entry->end == end) &&
1209 ((entry->eflags & MAP_ENTRY_COW) == 0) &&
1210 (entry->object.vm_object == NULL)) {
1211 entry->object.sub_map = submap;
1212 entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
1213 result = KERN_SUCCESS;
1223 * Sets the protection of the specified address
1224 * region in the target map. If "set_max" is
1225 * specified, the maximum protection is to be set;
1226 * otherwise, only the current protection is affected.
1229 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
1230 vm_prot_t new_prot, boolean_t set_max)
1232 vm_map_entry_t current;
1233 vm_map_entry_t entry;
1237 VM_MAP_RANGE_CHECK(map, start, end);
1239 if (vm_map_lookup_entry(map, start, &entry)) {
1240 vm_map_clip_start(map, entry, start);
1242 entry = entry->next;
1246 * Make a first pass to check for protection violations.
1249 while ((current != &map->header) && (current->start < end)) {
1250 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1252 return (KERN_INVALID_ARGUMENT);
1254 if ((new_prot & current->max_protection) != new_prot) {
1256 return (KERN_PROTECTION_FAILURE);
1258 current = current->next;
1262 * Go back and fix up protections. [Note that clipping is not
1263 * necessary the second time.]
1266 while ((current != &map->header) && (current->start < end)) {
1269 vm_map_clip_end(map, current, end);
1271 old_prot = current->protection;
1273 current->protection =
1274 (current->max_protection = new_prot) &
1277 current->protection = new_prot;
1280 * Update physical map if necessary. Worry about copy-on-write
1281 * here -- CHECK THIS XXX
1283 if (current->protection != old_prot) {
1285 #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1287 pmap_protect(map->pmap, current->start,
1289 current->protection & MASK(current));
1293 vm_map_simplify_entry(map, current);
1294 current = current->next;
1297 return (KERN_SUCCESS);
1303 * This routine traverses a processes map handling the madvise
1304 * system call. Advisories are classified as either those effecting
1305 * the vm_map_entry structure, or those effecting the underlying
1315 vm_map_entry_t current, entry;
1319 * Some madvise calls directly modify the vm_map_entry, in which case
1320 * we need to use an exclusive lock on the map and we need to perform
1321 * various clipping operations. Otherwise we only need a read-lock
1326 case MADV_SEQUENTIAL:
1338 vm_map_lock_read(map);
1341 return (KERN_INVALID_ARGUMENT);
1345 * Locate starting entry and clip if necessary.
1347 VM_MAP_RANGE_CHECK(map, start, end);
1349 if (vm_map_lookup_entry(map, start, &entry)) {
1351 vm_map_clip_start(map, entry, start);
1353 entry = entry->next;
1358 * madvise behaviors that are implemented in the vm_map_entry.
1360 * We clip the vm_map_entry so that behavioral changes are
1361 * limited to the specified address range.
1363 for (current = entry;
1364 (current != &map->header) && (current->start < end);
1365 current = current->next
1367 if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1370 vm_map_clip_end(map, current, end);
1374 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
1376 case MADV_SEQUENTIAL:
1377 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
1380 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
1383 current->eflags |= MAP_ENTRY_NOSYNC;
1386 current->eflags &= ~MAP_ENTRY_NOSYNC;
1389 current->eflags |= MAP_ENTRY_NOCOREDUMP;
1392 current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
1397 vm_map_simplify_entry(map, current);
1405 * madvise behaviors that are implemented in the underlying
1408 * Since we don't clip the vm_map_entry, we have to clip
1409 * the vm_object pindex and count.
1411 for (current = entry;
1412 (current != &map->header) && (current->start < end);
1413 current = current->next
1415 vm_offset_t useStart;
1417 if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1420 pindex = OFF_TO_IDX(current->offset);
1421 count = atop(current->end - current->start);
1422 useStart = current->start;
1424 if (current->start < start) {
1425 pindex += atop(start - current->start);
1426 count -= atop(start - current->start);
1429 if (current->end > end)
1430 count -= atop(current->end - end);
1435 vm_object_madvise(current->object.vm_object,
1436 pindex, count, behav);
1437 if (behav == MADV_WILLNEED) {
1439 pmap_object_init_pt(
1442 current->object.vm_object,
1444 (count << PAGE_SHIFT),
1445 MAP_PREFAULT_MADVISE
1450 vm_map_unlock_read(map);
1459 * Sets the inheritance of the specified address
1460 * range in the target map. Inheritance
1461 * affects how the map will be shared with
1462 * child maps at the time of vm_map_fork.
1465 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
1466 vm_inherit_t new_inheritance)
1468 vm_map_entry_t entry;
1469 vm_map_entry_t temp_entry;
1471 switch (new_inheritance) {
1472 case VM_INHERIT_NONE:
1473 case VM_INHERIT_COPY:
1474 case VM_INHERIT_SHARE:
1477 return (KERN_INVALID_ARGUMENT);
1480 VM_MAP_RANGE_CHECK(map, start, end);
1481 if (vm_map_lookup_entry(map, start, &temp_entry)) {
1483 vm_map_clip_start(map, entry, start);
1485 entry = temp_entry->next;
1486 while ((entry != &map->header) && (entry->start < end)) {
1487 vm_map_clip_end(map, entry, end);
1488 entry->inheritance = new_inheritance;
1489 vm_map_simplify_entry(map, entry);
1490 entry = entry->next;
1493 return (KERN_SUCCESS);
1499 * Implements both kernel and user unwiring.
1502 vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
1503 boolean_t user_unwire)
1505 vm_map_entry_t entry, first_entry, tmp_entry;
1506 vm_offset_t saved_start;
1507 unsigned int last_timestamp;
1509 boolean_t need_wakeup, result;
1512 VM_MAP_RANGE_CHECK(map, start, end);
1513 if (!vm_map_lookup_entry(map, start, &first_entry)) {
1515 return (KERN_INVALID_ADDRESS);
1517 last_timestamp = map->timestamp;
1518 entry = first_entry;
1519 while (entry != &map->header && entry->start < end) {
1520 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1522 * We have not yet clipped the entry.
1524 saved_start = (start >= entry->start) ? start :
1526 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1527 if (vm_map_unlock_and_wait(map, user_unwire)) {
1529 * Allow interruption of user unwiring?
1533 if (last_timestamp+1 != map->timestamp) {
1535 * Look again for the entry because the map was
1536 * modified while it was unlocked.
1537 * Specifically, the entry may have been
1538 * clipped, merged, or deleted.
1540 if (!vm_map_lookup_entry(map, saved_start,
1542 if (saved_start == start) {
1544 * First_entry has been deleted.
1547 return (KERN_INVALID_ADDRESS);
1550 rv = KERN_INVALID_ADDRESS;
1553 if (entry == first_entry)
1554 first_entry = tmp_entry;
1559 last_timestamp = map->timestamp;
1562 vm_map_clip_start(map, entry, start);
1563 vm_map_clip_end(map, entry, end);
1565 * Mark the entry in case the map lock is released. (See
1568 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
1570 * Check the map for holes in the specified region.
1572 if (entry->end < end && (entry->next == &map->header ||
1573 entry->next->start > entry->end)) {
1575 rv = KERN_INVALID_ADDRESS;
1579 * Require that the entry is wired.
1581 if (entry->wired_count == 0 || (user_unwire &&
1582 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0)) {
1584 rv = KERN_INVALID_ARGUMENT;
1587 entry = entry->next;
1591 need_wakeup = FALSE;
1592 if (first_entry == NULL) {
1593 result = vm_map_lookup_entry(map, start, &first_entry);
1594 KASSERT(result, ("vm_map_unwire: lookup failed"));
1596 entry = first_entry;
1597 while (entry != &map->header && entry->start < end) {
1598 if (rv == KERN_SUCCESS) {
1600 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
1601 entry->wired_count--;
1602 if (entry->wired_count == 0) {
1604 * Retain the map lock.
1606 vm_fault_unwire(map, entry->start, entry->end);
1609 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
1610 ("vm_map_unwire: in-transition flag missing"));
1611 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
1612 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
1613 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
1616 vm_map_simplify_entry(map, entry);
1617 entry = entry->next;
1628 * Implements both kernel and user wiring.
1631 vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
1632 boolean_t user_wire)
1634 vm_map_entry_t entry, first_entry, tmp_entry;
1635 vm_offset_t saved_end, saved_start;
1636 unsigned int last_timestamp;
1638 boolean_t need_wakeup, result;
1641 VM_MAP_RANGE_CHECK(map, start, end);
1642 if (!vm_map_lookup_entry(map, start, &first_entry)) {
1644 return (KERN_INVALID_ADDRESS);
1646 last_timestamp = map->timestamp;
1647 entry = first_entry;
1648 while (entry != &map->header && entry->start < end) {
1649 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1651 * We have not yet clipped the entry.
1653 saved_start = (start >= entry->start) ? start :
1655 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1656 if (vm_map_unlock_and_wait(map, user_wire)) {
1658 * Allow interruption of user wiring?
1662 if (last_timestamp + 1 != map->timestamp) {
1664 * Look again for the entry because the map was
1665 * modified while it was unlocked.
1666 * Specifically, the entry may have been
1667 * clipped, merged, or deleted.
1669 if (!vm_map_lookup_entry(map, saved_start,
1671 if (saved_start == start) {
1673 * first_entry has been deleted.
1676 return (KERN_INVALID_ADDRESS);
1679 rv = KERN_INVALID_ADDRESS;
1682 if (entry == first_entry)
1683 first_entry = tmp_entry;
1688 last_timestamp = map->timestamp;
1691 vm_map_clip_start(map, entry, start);
1692 vm_map_clip_end(map, entry, end);
1694 * Mark the entry in case the map lock is released. (See
1697 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
1701 if (entry->wired_count == 0) {
1702 entry->wired_count++;
1703 saved_start = entry->start;
1704 saved_end = entry->end;
1706 * Release the map lock, relying on the in-transition
1711 rv = vm_fault_user_wire(map, saved_start,
1714 rv = vm_fault_wire(map, saved_start, saved_end);
1716 if (last_timestamp + 1 != map->timestamp) {
1718 * Look again for the entry because the map was
1719 * modified while it was unlocked. The entry
1720 * may have been clipped, but NOT merged or
1723 result = vm_map_lookup_entry(map, saved_start,
1725 KASSERT(result, ("vm_map_wire: lookup failed"));
1726 if (entry == first_entry)
1727 first_entry = tmp_entry;
1731 while (entry->end < saved_end) {
1732 if (rv != KERN_SUCCESS) {
1733 KASSERT(entry->wired_count == 1,
1734 ("vm_map_wire: bad count"));
1735 entry->wired_count = -1;
1737 entry = entry->next;
1740 last_timestamp = map->timestamp;
1741 if (rv != KERN_SUCCESS) {
1742 KASSERT(entry->wired_count == 1,
1743 ("vm_map_wire: bad count"));
1745 * Assign an out-of-range value to represent
1746 * the failure to wire this entry.
1748 entry->wired_count = -1;
1752 } else if (!user_wire ||
1753 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
1754 entry->wired_count++;
1757 * Check the map for holes in the specified region.
1759 if (entry->end < end && (entry->next == &map->header ||
1760 entry->next->start > entry->end)) {
1762 rv = KERN_INVALID_ADDRESS;
1765 entry = entry->next;
1769 need_wakeup = FALSE;
1770 if (first_entry == NULL) {
1771 result = vm_map_lookup_entry(map, start, &first_entry);
1772 KASSERT(result, ("vm_map_wire: lookup failed"));
1774 entry = first_entry;
1775 while (entry != &map->header && entry->start < end) {
1776 if (rv == KERN_SUCCESS) {
1778 entry->eflags |= MAP_ENTRY_USER_WIRED;
1779 } else if (entry->wired_count == -1) {
1781 * Wiring failed on this entry. Thus, unwiring is
1784 entry->wired_count = 0;
1786 if (!user_wire || (entry->wired_count == 1 &&
1787 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0))
1788 entry->wired_count--;
1789 if (entry->wired_count == 0) {
1791 * Retain the map lock.
1793 vm_fault_unwire(map, entry->start, entry->end);
1796 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
1797 ("vm_map_wire: in-transition flag missing"));
1798 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
1799 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
1800 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
1803 vm_map_simplify_entry(map, entry);
1804 entry = entry->next;
1815 * Push any dirty cached pages in the address range to their pager.
1816 * If syncio is TRUE, dirty pages are written synchronously.
1817 * If invalidate is TRUE, any cached pages are freed as well.
1819 * Returns an error if any part of the specified range is not mapped.
1827 boolean_t invalidate)
1829 vm_map_entry_t current;
1830 vm_map_entry_t entry;
1833 vm_ooffset_t offset;
1837 vm_map_lock_read(map);
1838 VM_MAP_RANGE_CHECK(map, start, end);
1839 if (!vm_map_lookup_entry(map, start, &entry)) {
1840 vm_map_unlock_read(map);
1841 return (KERN_INVALID_ADDRESS);
1844 * Make a first pass to check for holes.
1846 for (current = entry; current->start < end; current = current->next) {
1847 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1848 vm_map_unlock_read(map);
1849 return (KERN_INVALID_ARGUMENT);
1851 if (end > current->end &&
1852 (current->next == &map->header ||
1853 current->end != current->next->start)) {
1854 vm_map_unlock_read(map);
1855 return (KERN_INVALID_ADDRESS);
1860 pmap_remove(vm_map_pmap(map), start, end);
1862 * Make a second pass, cleaning/uncaching pages from the indicated
1865 for (current = entry; current->start < end; current = current->next) {
1866 offset = current->offset + (start - current->start);
1867 size = (end <= current->end ? end : current->end) - start;
1868 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1870 vm_map_entry_t tentry;
1873 smap = current->object.sub_map;
1874 vm_map_lock_read(smap);
1875 (void) vm_map_lookup_entry(smap, offset, &tentry);
1876 tsize = tentry->end - offset;
1879 object = tentry->object.vm_object;
1880 offset = tentry->offset + (offset - tentry->start);
1881 vm_map_unlock_read(smap);
1883 object = current->object.vm_object;
1886 * Note that there is absolutely no sense in writing out
1887 * anonymous objects, so we track down the vnode object
1889 * We invalidate (remove) all pages from the address space
1890 * anyway, for semantic correctness.
1892 * note: certain anonymous maps, such as MAP_NOSYNC maps,
1893 * may start out with a NULL object.
1895 while (object && object->backing_object) {
1896 object = object->backing_object;
1897 offset += object->backing_object_offset;
1898 if (object->size < OFF_TO_IDX(offset + size))
1899 size = IDX_TO_OFF(object->size) - offset;
1901 if (object && (object->type == OBJT_VNODE) &&
1902 (current->protection & VM_PROT_WRITE)) {
1904 * Flush pages if writing is allowed, invalidate them
1905 * if invalidation requested. Pages undergoing I/O
1906 * will be ignored by vm_object_page_remove().
1908 * We cannot lock the vnode and then wait for paging
1909 * to complete without deadlocking against vm_fault.
1910 * Instead we simply call vm_object_page_remove() and
1911 * allow it to block internally on a page-by-page
1912 * basis when it encounters pages undergoing async
1917 vm_object_reference(object);
1918 vn_lock(object->handle, LK_EXCLUSIVE | LK_RETRY, curthread);
1919 flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
1920 flags |= invalidate ? OBJPC_INVAL : 0;
1921 vm_object_page_clean(object,
1923 OFF_TO_IDX(offset + size + PAGE_MASK),
1926 /*vm_object_pip_wait(object, "objmcl");*/
1927 vm_object_page_remove(object,
1929 OFF_TO_IDX(offset + size + PAGE_MASK),
1932 VOP_UNLOCK(object->handle, 0, curthread);
1933 vm_object_deallocate(object);
1938 vm_map_unlock_read(map);
1939 return (KERN_SUCCESS);
1943 * vm_map_entry_unwire: [ internal use only ]
1945 * Make the region specified by this entry pageable.
1947 * The map in question should be locked.
1948 * [This is the reason for this routine's existence.]
1951 vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
1953 vm_fault_unwire(map, entry->start, entry->end);
1954 entry->wired_count = 0;
1958 * vm_map_entry_delete: [ internal use only ]
1960 * Deallocate the given entry from the target map.
1963 vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry)
1965 vm_map_entry_unlink(map, entry);
1966 map->size -= entry->end - entry->start;
1968 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1969 vm_object_deallocate(entry->object.vm_object);
1972 vm_map_entry_dispose(map, entry);
1976 * vm_map_delete: [ internal use only ]
1978 * Deallocates the given address range from the target
1982 vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end)
1985 vm_map_entry_t entry;
1986 vm_map_entry_t first_entry;
1989 * Find the start of the region, and clip it
1991 if (!vm_map_lookup_entry(map, start, &first_entry))
1992 entry = first_entry->next;
1994 entry = first_entry;
1995 vm_map_clip_start(map, entry, start);
1999 * Save the free space hint
2001 if (entry == &map->header) {
2002 map->first_free = &map->header;
2003 } else if (map->first_free->start >= start) {
2004 map->first_free = entry->prev;
2008 * Step through all entries in this region
2010 while ((entry != &map->header) && (entry->start < end)) {
2011 vm_map_entry_t next;
2013 vm_pindex_t offidxstart, offidxend, count;
2016 * Wait for wiring or unwiring of an entry to complete.
2018 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0) {
2019 unsigned int last_timestamp;
2020 vm_offset_t saved_start;
2021 vm_map_entry_t tmp_entry;
2023 saved_start = entry->start;
2024 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2025 last_timestamp = map->timestamp;
2026 (void) vm_map_unlock_and_wait(map, FALSE);
2028 if (last_timestamp + 1 != map->timestamp) {
2030 * Look again for the entry because the map was
2031 * modified while it was unlocked.
2032 * Specifically, the entry may have been
2033 * clipped, merged, or deleted.
2035 if (!vm_map_lookup_entry(map, saved_start,
2037 entry = tmp_entry->next;
2040 vm_map_clip_start(map, entry,
2046 vm_map_clip_end(map, entry, end);
2052 offidxstart = OFF_TO_IDX(entry->offset);
2053 count = OFF_TO_IDX(e - s);
2054 object = entry->object.vm_object;
2057 * Unwire before removing addresses from the pmap; otherwise,
2058 * unwiring will put the entries back in the pmap.
2060 if (entry->wired_count != 0) {
2061 vm_map_entry_unwire(map, entry);
2064 offidxend = offidxstart + count;
2066 if ((object == kernel_object) || (object == kmem_object)) {
2067 vm_object_page_remove(object, offidxstart, offidxend, FALSE);
2070 pmap_remove(map->pmap, s, e);
2071 if (object != NULL &&
2072 object->ref_count != 1 &&
2073 (object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING &&
2074 (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2075 vm_object_collapse(object);
2076 vm_object_page_remove(object, offidxstart, offidxend, FALSE);
2077 if (object->type == OBJT_SWAP) {
2078 swap_pager_freespace(object, offidxstart, count);
2080 if (offidxend >= object->size &&
2081 offidxstart < object->size) {
2082 object->size = offidxstart;
2089 * Delete the entry (which may delete the object) only after
2090 * removing all pmap entries pointing to its pages.
2091 * (Otherwise, its page frames may be reallocated, and any
2092 * modify bits will be set in the wrong object!)
2094 vm_map_entry_delete(map, entry);
2097 return (KERN_SUCCESS);
2103 * Remove the given address range from the target map.
2104 * This is the exported form of vm_map_delete.
2107 vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
2111 if (map == kmem_map)
2115 VM_MAP_RANGE_CHECK(map, start, end);
2116 result = vm_map_delete(map, start, end);
2119 if (map == kmem_map)
2126 * vm_map_check_protection:
2128 * Assert that the target map allows the specified
2129 * privilege on the entire address region given.
2130 * The entire region must be allocated.
2133 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
2134 vm_prot_t protection)
2136 vm_map_entry_t entry;
2137 vm_map_entry_t tmp_entry;
2139 vm_map_lock_read(map);
2140 if (!vm_map_lookup_entry(map, start, &tmp_entry)) {
2141 vm_map_unlock_read(map);
2146 while (start < end) {
2147 if (entry == &map->header) {
2148 vm_map_unlock_read(map);
2154 if (start < entry->start) {
2155 vm_map_unlock_read(map);
2159 * Check protection associated with entry.
2161 if ((entry->protection & protection) != protection) {
2162 vm_map_unlock_read(map);
2165 /* go to next entry */
2167 entry = entry->next;
2169 vm_map_unlock_read(map);
2174 * vm_map_copy_entry:
2176 * Copies the contents of the source entry to the destination
2177 * entry. The entries *must* be aligned properly.
2183 vm_map_entry_t src_entry,
2184 vm_map_entry_t dst_entry)
2186 vm_object_t src_object;
2188 if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
2191 if (src_entry->wired_count == 0) {
2194 * If the source entry is marked needs_copy, it is already
2197 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
2198 pmap_protect(src_map->pmap,
2201 src_entry->protection & ~VM_PROT_WRITE);
2205 * Make a copy of the object.
2207 if ((src_object = src_entry->object.vm_object) != NULL) {
2209 if ((src_object->handle == NULL) &&
2210 (src_object->type == OBJT_DEFAULT ||
2211 src_object->type == OBJT_SWAP)) {
2212 vm_object_collapse(src_object);
2213 if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
2214 vm_object_split(src_entry);
2215 src_object = src_entry->object.vm_object;
2219 vm_object_reference(src_object);
2220 vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
2221 dst_entry->object.vm_object = src_object;
2222 src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2223 dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2224 dst_entry->offset = src_entry->offset;
2226 dst_entry->object.vm_object = NULL;
2227 dst_entry->offset = 0;
2230 pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
2231 dst_entry->end - dst_entry->start, src_entry->start);
2234 * Of course, wired down pages can't be set copy-on-write.
2235 * Cause wired pages to be copied into the new map by
2236 * simulating faults (the new pages are pageable)
2238 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry);
2244 * Create a new process vmspace structure and vm_map
2245 * based on those of an existing process. The new map
2246 * is based on the old map, according to the inheritance
2247 * values on the regions in that map.
2249 * The source map must not be locked.
2252 vmspace_fork(struct vmspace *vm1)
2254 struct vmspace *vm2;
2255 vm_map_t old_map = &vm1->vm_map;
2257 vm_map_entry_t old_entry;
2258 vm_map_entry_t new_entry;
2263 vm_map_lock(old_map);
2264 old_map->infork = 1;
2266 vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
2267 bcopy(&vm1->vm_startcopy, &vm2->vm_startcopy,
2268 (caddr_t) &vm1->vm_endcopy - (caddr_t) &vm1->vm_startcopy);
2269 new_map = &vm2->vm_map; /* XXX */
2270 new_map->timestamp = 1;
2272 old_entry = old_map->header.next;
2274 while (old_entry != &old_map->header) {
2275 if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2276 panic("vm_map_fork: encountered a submap");
2278 switch (old_entry->inheritance) {
2279 case VM_INHERIT_NONE:
2282 case VM_INHERIT_SHARE:
2284 * Clone the entry, creating the shared object if necessary.
2286 object = old_entry->object.vm_object;
2287 if (object == NULL) {
2288 object = vm_object_allocate(OBJT_DEFAULT,
2289 atop(old_entry->end - old_entry->start));
2290 old_entry->object.vm_object = object;
2291 old_entry->offset = (vm_offset_t) 0;
2295 * Add the reference before calling vm_object_shadow
2296 * to insure that a shadow object is created.
2298 vm_object_reference(object);
2299 if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
2300 vm_object_shadow(&old_entry->object.vm_object,
2302 atop(old_entry->end - old_entry->start));
2303 old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
2304 /* Transfer the second reference too. */
2305 vm_object_reference(
2306 old_entry->object.vm_object);
2307 vm_object_deallocate(object);
2308 object = old_entry->object.vm_object;
2310 vm_object_clear_flag(object, OBJ_ONEMAPPING);
2313 * Clone the entry, referencing the shared object.
2315 new_entry = vm_map_entry_create(new_map);
2316 *new_entry = *old_entry;
2317 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2318 new_entry->wired_count = 0;
2321 * Insert the entry into the new map -- we know we're
2322 * inserting at the end of the new map.
2324 vm_map_entry_link(new_map, new_map->header.prev,
2328 * Update the physical map
2330 pmap_copy(new_map->pmap, old_map->pmap,
2332 (old_entry->end - old_entry->start),
2336 case VM_INHERIT_COPY:
2338 * Clone the entry and link into the map.
2340 new_entry = vm_map_entry_create(new_map);
2341 *new_entry = *old_entry;
2342 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2343 new_entry->wired_count = 0;
2344 new_entry->object.vm_object = NULL;
2345 vm_map_entry_link(new_map, new_map->header.prev,
2347 vm_map_copy_entry(old_map, new_map, old_entry,
2351 old_entry = old_entry->next;
2354 new_map->size = old_map->size;
2355 old_map->infork = 0;
2356 vm_map_unlock(old_map);
2362 vm_map_stack (vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
2363 vm_prot_t prot, vm_prot_t max, int cow)
2365 vm_map_entry_t prev_entry;
2366 vm_map_entry_t new_stack_entry;
2367 vm_size_t init_ssize;
2370 if (VM_MIN_ADDRESS > 0 && addrbos < VM_MIN_ADDRESS)
2371 return (KERN_NO_SPACE);
2373 if (max_ssize < sgrowsiz)
2374 init_ssize = max_ssize;
2376 init_ssize = sgrowsiz;
2380 /* If addr is already mapped, no go */
2381 if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
2383 return (KERN_NO_SPACE);
2386 /* If we would blow our VMEM resource limit, no go */
2387 if (map->size + init_ssize >
2388 curthread->td_proc->p_rlimit[RLIMIT_VMEM].rlim_cur) {
2390 return (KERN_NO_SPACE);
2393 /* If we can't accomodate max_ssize in the current mapping,
2394 * no go. However, we need to be aware that subsequent user
2395 * mappings might map into the space we have reserved for
2396 * stack, and currently this space is not protected.
2398 * Hopefully we will at least detect this condition
2399 * when we try to grow the stack.
2401 if ((prev_entry->next != &map->header) &&
2402 (prev_entry->next->start < addrbos + max_ssize)) {
2404 return (KERN_NO_SPACE);
2407 /* We initially map a stack of only init_ssize. We will
2408 * grow as needed later. Since this is to be a grow
2409 * down stack, we map at the top of the range.
2411 * Note: we would normally expect prot and max to be
2412 * VM_PROT_ALL, and cow to be 0. Possibly we should
2413 * eliminate these as input parameters, and just
2414 * pass these values here in the insert call.
2416 rv = vm_map_insert(map, NULL, 0, addrbos + max_ssize - init_ssize,
2417 addrbos + max_ssize, prot, max, cow);
2419 /* Now set the avail_ssize amount */
2420 if (rv == KERN_SUCCESS){
2421 if (prev_entry != &map->header)
2422 vm_map_clip_end(map, prev_entry, addrbos + max_ssize - init_ssize);
2423 new_stack_entry = prev_entry->next;
2424 if (new_stack_entry->end != addrbos + max_ssize ||
2425 new_stack_entry->start != addrbos + max_ssize - init_ssize)
2426 panic ("Bad entry start/end for new stack entry");
2428 new_stack_entry->avail_ssize = max_ssize - init_ssize;
2435 /* Attempts to grow a vm stack entry. Returns KERN_SUCCESS if the
2436 * desired address is already mapped, or if we successfully grow
2437 * the stack. Also returns KERN_SUCCESS if addr is outside the
2438 * stack range (this is strange, but preserves compatibility with
2439 * the grow function in vm_machdep.c).
2442 vm_map_growstack (struct proc *p, vm_offset_t addr)
2444 vm_map_entry_t prev_entry;
2445 vm_map_entry_t stack_entry;
2446 vm_map_entry_t new_stack_entry;
2447 struct vmspace *vm = p->p_vmspace;
2448 vm_map_t map = &vm->vm_map;
2457 vm_map_lock_read(map);
2459 /* If addr is already in the entry range, no need to grow.*/
2460 if (vm_map_lookup_entry(map, addr, &prev_entry)) {
2461 vm_map_unlock_read(map);
2462 return (KERN_SUCCESS);
2465 if ((stack_entry = prev_entry->next) == &map->header) {
2466 vm_map_unlock_read(map);
2467 return (KERN_SUCCESS);
2469 if (prev_entry == &map->header)
2470 end = stack_entry->start - stack_entry->avail_ssize;
2472 end = prev_entry->end;
2474 /* This next test mimics the old grow function in vm_machdep.c.
2475 * It really doesn't quite make sense, but we do it anyway
2476 * for compatibility.
2478 * If not growable stack, return success. This signals the
2479 * caller to proceed as he would normally with normal vm.
2481 if (stack_entry->avail_ssize < 1 ||
2482 addr >= stack_entry->start ||
2483 addr < stack_entry->start - stack_entry->avail_ssize) {
2484 vm_map_unlock_read(map);
2485 return (KERN_SUCCESS);
2488 /* Find the minimum grow amount */
2489 grow_amount = roundup (stack_entry->start - addr, PAGE_SIZE);
2490 if (grow_amount > stack_entry->avail_ssize) {
2491 vm_map_unlock_read(map);
2492 return (KERN_NO_SPACE);
2495 /* If there is no longer enough space between the entries
2496 * nogo, and adjust the available space. Note: this
2497 * should only happen if the user has mapped into the
2498 * stack area after the stack was created, and is
2499 * probably an error.
2501 * This also effectively destroys any guard page the user
2502 * might have intended by limiting the stack size.
2504 if (grow_amount > stack_entry->start - end) {
2505 if (vm_map_lock_upgrade(map))
2508 stack_entry->avail_ssize = stack_entry->start - end;
2511 return (KERN_NO_SPACE);
2514 is_procstack = addr >= (vm_offset_t)vm->vm_maxsaddr;
2516 /* If this is the main process stack, see if we're over the
2519 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount >
2520 p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
2521 vm_map_unlock_read(map);
2522 return (KERN_NO_SPACE);
2525 /* Round up the grow amount modulo SGROWSIZ */
2526 grow_amount = roundup (grow_amount, sgrowsiz);
2527 if (grow_amount > stack_entry->avail_ssize) {
2528 grow_amount = stack_entry->avail_ssize;
2530 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount >
2531 p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
2532 grow_amount = p->p_rlimit[RLIMIT_STACK].rlim_cur -
2536 /* If we would blow our VMEM resource limit, no go */
2537 if (map->size + grow_amount >
2538 curthread->td_proc->p_rlimit[RLIMIT_VMEM].rlim_cur) {
2539 vm_map_unlock_read(map);
2540 return (KERN_NO_SPACE);
2543 if (vm_map_lock_upgrade(map))
2546 /* Get the preliminary new entry start value */
2547 addr = stack_entry->start - grow_amount;
2549 /* If this puts us into the previous entry, cut back our growth
2550 * to the available space. Also, see the note above.
2553 stack_entry->avail_ssize = stack_entry->start - end;
2557 rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start,
2562 /* Adjust the available stack space by the amount we grew. */
2563 if (rv == KERN_SUCCESS) {
2564 if (prev_entry != &map->header)
2565 vm_map_clip_end(map, prev_entry, addr);
2566 new_stack_entry = prev_entry->next;
2567 if (new_stack_entry->end != stack_entry->start ||
2568 new_stack_entry->start != addr)
2569 panic ("Bad stack grow start/end in new stack entry");
2571 new_stack_entry->avail_ssize = stack_entry->avail_ssize -
2572 (new_stack_entry->end -
2573 new_stack_entry->start);
2575 vm->vm_ssize += btoc(new_stack_entry->end -
2576 new_stack_entry->start);
2585 * Unshare the specified VM space for exec. If other processes are
2586 * mapped to it, then create a new one. The new vmspace is null.
2589 vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser)
2591 struct vmspace *oldvmspace = p->p_vmspace;
2592 struct vmspace *newvmspace;
2595 newvmspace = vmspace_alloc(minuser, maxuser);
2596 bcopy(&oldvmspace->vm_startcopy, &newvmspace->vm_startcopy,
2597 (caddr_t) (newvmspace + 1) - (caddr_t) &newvmspace->vm_startcopy);
2599 * This code is written like this for prototype purposes. The
2600 * goal is to avoid running down the vmspace here, but let the
2601 * other process's that are still using the vmspace to finally
2602 * run it down. Even though there is little or no chance of blocking
2603 * here, it is a good idea to keep this form for future mods.
2605 p->p_vmspace = newvmspace;
2606 pmap_pinit2(vmspace_pmap(newvmspace));
2607 vmspace_free(oldvmspace);
2608 if (p == curthread->td_proc) /* XXXKSE ? */
2609 pmap_activate(curthread);
2613 * Unshare the specified VM space for forcing COW. This
2614 * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
2617 vmspace_unshare(struct proc *p)
2619 struct vmspace *oldvmspace = p->p_vmspace;
2620 struct vmspace *newvmspace;
2623 if (oldvmspace->vm_refcnt == 1)
2625 newvmspace = vmspace_fork(oldvmspace);
2626 p->p_vmspace = newvmspace;
2627 pmap_pinit2(vmspace_pmap(newvmspace));
2628 vmspace_free(oldvmspace);
2629 if (p == curthread->td_proc) /* XXXKSE ? */
2630 pmap_activate(curthread);
2636 * Finds the VM object, offset, and
2637 * protection for a given virtual address in the
2638 * specified map, assuming a page fault of the
2641 * Leaves the map in question locked for read; return
2642 * values are guaranteed until a vm_map_lookup_done
2643 * call is performed. Note that the map argument
2644 * is in/out; the returned map must be used in
2645 * the call to vm_map_lookup_done.
2647 * A handle (out_entry) is returned for use in
2648 * vm_map_lookup_done, to make that fast.
2650 * If a lookup is requested with "write protection"
2651 * specified, the map may be changed to perform virtual
2652 * copying operations, although the data referenced will
2656 vm_map_lookup(vm_map_t *var_map, /* IN/OUT */
2658 vm_prot_t fault_typea,
2659 vm_map_entry_t *out_entry, /* OUT */
2660 vm_object_t *object, /* OUT */
2661 vm_pindex_t *pindex, /* OUT */
2662 vm_prot_t *out_prot, /* OUT */
2663 boolean_t *wired) /* OUT */
2665 vm_map_entry_t entry;
2666 vm_map_t map = *var_map;
2668 vm_prot_t fault_type = fault_typea;
2672 * Lookup the faulting address.
2675 vm_map_lock_read(map);
2676 #define RETURN(why) \
2678 vm_map_unlock_read(map); \
2683 * If the map has an interesting hint, try it before calling full
2684 * blown lookup routine.
2688 if (entry == NULL ||
2689 (vaddr < entry->start) || (vaddr >= entry->end)) {
2691 * Entry was either not a valid hint, or the vaddr was not
2692 * contained in the entry, so do a full lookup.
2694 if (!vm_map_lookup_entry(map, vaddr, out_entry))
2695 RETURN(KERN_INVALID_ADDRESS);
2703 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
2704 vm_map_t old_map = map;
2706 *var_map = map = entry->object.sub_map;
2707 vm_map_unlock_read(old_map);
2712 * Check whether this task is allowed to have this page.
2713 * Note the special case for MAP_ENTRY_COW
2714 * pages with an override. This is to implement a forced
2715 * COW for debuggers.
2717 if (fault_type & VM_PROT_OVERRIDE_WRITE)
2718 prot = entry->max_protection;
2720 prot = entry->protection;
2721 fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
2722 if ((fault_type & prot) != fault_type) {
2723 RETURN(KERN_PROTECTION_FAILURE);
2725 if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
2726 (entry->eflags & MAP_ENTRY_COW) &&
2727 (fault_type & VM_PROT_WRITE) &&
2728 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
2729 RETURN(KERN_PROTECTION_FAILURE);
2733 * If this page is not pageable, we have to get it for all possible
2736 *wired = (entry->wired_count != 0);
2738 prot = fault_type = entry->protection;
2741 * If the entry was copy-on-write, we either ...
2743 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
2745 * If we want to write the page, we may as well handle that
2746 * now since we've got the map locked.
2748 * If we don't need to write the page, we just demote the
2749 * permissions allowed.
2751 if (fault_type & VM_PROT_WRITE) {
2753 * Make a new object, and place it in the object
2754 * chain. Note that no new references have appeared
2755 * -- one just moved from the map to the new
2758 if (vm_map_lock_upgrade(map))
2762 &entry->object.vm_object,
2764 atop(entry->end - entry->start));
2765 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
2767 vm_map_lock_downgrade(map);
2770 * We're attempting to read a copy-on-write page --
2771 * don't allow writes.
2773 prot &= ~VM_PROT_WRITE;
2778 * Create an object if necessary.
2780 if (entry->object.vm_object == NULL &&
2782 if (vm_map_lock_upgrade(map))
2784 entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
2785 atop(entry->end - entry->start));
2787 vm_map_lock_downgrade(map);
2791 * Return the object/offset from this entry. If the entry was
2792 * copy-on-write or empty, it has been fixed up.
2794 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
2795 *object = entry->object.vm_object;
2798 * Return whether this is the only map sharing this data.
2801 return (KERN_SUCCESS);
2807 * vm_map_lookup_done:
2809 * Releases locks acquired by a vm_map_lookup
2810 * (according to the handle returned by that lookup).
2813 vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry)
2816 * Unlock the main-level map
2818 vm_map_unlock_read(map);
2821 #ifdef ENABLE_VFS_IOOPT
2823 * Experimental support for zero-copy I/O
2825 * Implement uiomove with VM operations. This handles (and collateral changes)
2826 * support every combination of source object modification, and COW type
2832 vm_object_t srcobject,
2839 vm_object_t first_object, oldobject, object;
2840 vm_map_entry_t entry;
2844 vm_offset_t uaddr, start, end, tend;
2845 vm_pindex_t first_pindex, oindex;
2861 if ((vm_map_lookup(&map, uaddr,
2862 VM_PROT_READ, &entry, &first_object,
2863 &first_pindex, &prot, &wired)) != KERN_SUCCESS) {
2867 vm_map_clip_start(map, entry, uaddr);
2870 tend = uaddr + tcnt;
2871 if (tend > entry->end) {
2872 tcnt = entry->end - uaddr;
2876 vm_map_clip_end(map, entry, tend);
2878 start = entry->start;
2883 oindex = OFF_TO_IDX(cp);
2886 for (idx = 0; idx < osize; idx++) {
2888 if ((m = vm_page_lookup(srcobject, oindex + idx)) == NULL) {
2889 vm_map_lookup_done(map, entry);
2893 * disallow busy or invalid pages, but allow
2894 * m->busy pages if they are entirely valid.
2896 if ((m->flags & PG_BUSY) ||
2897 ((m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL)) {
2898 vm_map_lookup_done(map, entry);
2905 * If we are changing an existing map entry, just redirect
2906 * the object, and change mappings.
2908 if ((first_object->type == OBJT_VNODE) &&
2909 ((oldobject = entry->object.vm_object) == first_object)) {
2911 if ((entry->offset != cp) || (oldobject != srcobject)) {
2913 * Remove old window into the file
2915 pmap_remove (map->pmap, uaddr, tend);
2918 * Force copy on write for mmaped regions
2920 vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
2923 * Point the object appropriately
2925 if (oldobject != srcobject) {
2928 * Set the object optimization hint flag
2930 vm_object_set_flag(srcobject, OBJ_OPT);
2931 vm_object_reference(srcobject);
2932 entry->object.vm_object = srcobject;
2935 vm_object_deallocate(oldobject);
2942 pmap_remove (map->pmap, uaddr, tend);
2945 } else if ((first_object->ref_count == 1) &&
2946 (first_object->size == osize) &&
2947 ((first_object->type == OBJT_DEFAULT) ||
2948 (first_object->type == OBJT_SWAP)) ) {
2950 oldobject = first_object->backing_object;
2952 if ((first_object->backing_object_offset != cp) ||
2953 (oldobject != srcobject)) {
2955 * Remove old window into the file
2957 pmap_remove (map->pmap, uaddr, tend);
2960 * Remove unneeded old pages
2962 vm_object_page_remove(first_object, 0, 0, 0);
2965 * Invalidate swap space
2967 if (first_object->type == OBJT_SWAP) {
2968 swap_pager_freespace(first_object,
2970 first_object->size);
2974 * Force copy on write for mmaped regions
2976 vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
2979 * Point the object appropriately
2981 if (oldobject != srcobject) {
2983 * Set the object optimization hint flag
2985 vm_object_set_flag(srcobject, OBJ_OPT);
2986 vm_object_reference(srcobject);
2989 TAILQ_REMOVE(&oldobject->shadow_head,
2990 first_object, shadow_list);
2991 oldobject->shadow_count--;
2992 /* XXX bump generation? */
2993 vm_object_deallocate(oldobject);
2996 TAILQ_INSERT_TAIL(&srcobject->shadow_head,
2997 first_object, shadow_list);
2998 srcobject->shadow_count++;
2999 /* XXX bump generation? */
3001 first_object->backing_object = srcobject;
3003 first_object->backing_object_offset = cp;
3006 pmap_remove (map->pmap, uaddr, tend);
3009 * Otherwise, we have to do a logical mmap.
3013 vm_object_set_flag(srcobject, OBJ_OPT);
3014 vm_object_reference(srcobject);
3016 pmap_remove (map->pmap, uaddr, tend);
3018 vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
3019 vm_map_lock_upgrade(map);
3021 if (entry == &map->header) {
3022 map->first_free = &map->header;
3023 } else if (map->first_free->start >= start) {
3024 map->first_free = entry->prev;
3027 vm_map_entry_delete(map, entry);
3032 rv = vm_map_insert(map, object, ooffset, start, tend,
3033 VM_PROT_ALL, VM_PROT_ALL, MAP_COPY_ON_WRITE);
3035 if (rv != KERN_SUCCESS)
3036 panic("vm_uiomove: could not insert new entry: %d", rv);
3040 * Map the window directly, if it is already in memory
3042 pmap_object_init_pt(map->pmap, uaddr,
3043 srcobject, oindex, tcnt, 0);
3058 #include "opt_ddb.h"
3060 #include <sys/kernel.h>
3062 #include <ddb/ddb.h>
3065 * vm_map_print: [ debug ]
3067 DB_SHOW_COMMAND(map, vm_map_print)
3070 /* XXX convert args. */
3071 vm_map_t map = (vm_map_t)addr;
3072 boolean_t full = have_addr;
3074 vm_map_entry_t entry;
3076 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
3078 (void *)map->pmap, map->nentries, map->timestamp);
3081 if (!full && db_indent)
3085 for (entry = map->header.next; entry != &map->header;
3086 entry = entry->next) {
3087 db_iprintf("map entry %p: start=%p, end=%p\n",
3088 (void *)entry, (void *)entry->start, (void *)entry->end);
3091 static char *inheritance_name[4] =
3092 {"share", "copy", "none", "donate_copy"};
3094 db_iprintf(" prot=%x/%x/%s",
3096 entry->max_protection,
3097 inheritance_name[(int)(unsigned char)entry->inheritance]);
3098 if (entry->wired_count != 0)
3099 db_printf(", wired");
3101 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3102 /* XXX no %qd in kernel. Truncate entry->offset. */
3103 db_printf(", share=%p, offset=0x%lx\n",
3104 (void *)entry->object.sub_map,
3105 (long)entry->offset);
3107 if ((entry->prev == &map->header) ||
3108 (entry->prev->object.sub_map !=
3109 entry->object.sub_map)) {
3111 vm_map_print((db_expr_t)(intptr_t)
3112 entry->object.sub_map,
3113 full, 0, (char *)0);
3117 /* XXX no %qd in kernel. Truncate entry->offset. */
3118 db_printf(", object=%p, offset=0x%lx",
3119 (void *)entry->object.vm_object,
3120 (long)entry->offset);
3121 if (entry->eflags & MAP_ENTRY_COW)
3122 db_printf(", copy (%s)",
3123 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
3127 if ((entry->prev == &map->header) ||
3128 (entry->prev->object.vm_object !=
3129 entry->object.vm_object)) {
3131 vm_object_print((db_expr_t)(intptr_t)
3132 entry->object.vm_object,
3133 full, 0, (char *)0);
3145 DB_SHOW_COMMAND(procvm, procvm)
3150 p = (struct proc *) addr;
3155 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
3156 (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
3157 (void *)vmspace_pmap(p->p_vmspace));
3159 vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);