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>
81 #include <sys/sysent.h>
84 #include <vm/vm_param.h>
86 #include <vm/vm_map.h>
87 #include <vm/vm_page.h>
88 #include <vm/vm_object.h>
89 #include <vm/vm_pager.h>
90 #include <vm/vm_kern.h>
91 #include <vm/vm_extern.h>
92 #include <vm/swap_pager.h>
96 * Virtual memory maps provide for the mapping, protection,
97 * and sharing of virtual memory objects. In addition,
98 * this module provides for an efficient virtual copy of
99 * memory from one map to another.
101 * Synchronization is required prior to most operations.
103 * Maps consist of an ordered doubly-linked list of simple
104 * entries; a single hint is used to speed up lookups.
106 * Since portions of maps are specified by start/end addresses,
107 * which may not align with existing map entries, all
108 * routines merely "clip" entries to these start/end values.
109 * [That is, an entry is split into two, bordering at a
110 * start or end value.] Note that these clippings may not
111 * always be necessary (as the two resulting entries are then
112 * not changed); however, the clipping is done for convenience.
114 * As mentioned above, virtual copy operations are performed
115 * by copying VM object references from one map to
116 * another, and then marking both regions as copy-on-write.
122 * Initialize the vm_map module. Must be called before
123 * any other vm_map routines.
125 * Map and entry structures are allocated from the general
126 * purpose memory pool with some exceptions:
128 * - The kernel map and kmem submap are allocated statically.
129 * - Kernel map entries are allocated out of a static pool.
131 * These restrictions are necessary since malloc() uses the
132 * maps and requires map entries.
135 static uma_zone_t mapentzone;
136 static uma_zone_t kmapentzone;
137 static uma_zone_t mapzone;
138 static uma_zone_t vmspace_zone;
139 static struct vm_object kmapentobj;
140 static void vmspace_zinit(void *mem, int size);
141 static void vmspace_zfini(void *mem, int size);
142 static void vm_map_zinit(void *mem, int size);
143 static void vm_map_zfini(void *mem, int size);
144 static void _vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max);
147 static void vm_map_zdtor(void *mem, int size, void *arg);
148 static void vmspace_zdtor(void *mem, int size, void *arg);
154 mapzone = uma_zcreate("MAP", sizeof(struct vm_map), NULL,
160 vm_map_zinit, vm_map_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
161 uma_prealloc(mapzone, MAX_KMAP);
162 kmapentzone = uma_zcreate("KMAP ENTRY", sizeof(struct vm_map_entry),
163 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
164 UMA_ZONE_MTXCLASS | UMA_ZONE_VM);
165 uma_prealloc(kmapentzone, MAX_KMAPENT);
166 mapentzone = uma_zcreate("MAP ENTRY", sizeof(struct vm_map_entry),
167 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
168 uma_prealloc(mapentzone, MAX_MAPENT);
172 vmspace_zfini(void *mem, int size)
176 vm = (struct vmspace *)mem;
178 vm_map_zfini(&vm->vm_map, sizeof(vm->vm_map));
182 vmspace_zinit(void *mem, int size)
186 vm = (struct vmspace *)mem;
188 vm_map_zinit(&vm->vm_map, sizeof(vm->vm_map));
192 vm_map_zfini(void *mem, int size)
198 lockdestroy(&map->lock);
202 vm_map_zinit(void *mem, int size)
210 lockinit(&map->lock, PVM, "thrd_sleep", 0, LK_NOPAUSE);
215 vmspace_zdtor(void *mem, int size, void *arg)
219 vm = (struct vmspace *)mem;
221 vm_map_zdtor(&vm->vm_map, sizeof(vm->vm_map), arg);
224 vm_map_zdtor(void *mem, int size, void *arg)
229 KASSERT(map->nentries == 0,
230 ("map %p nentries == %d on free.",
231 map, map->nentries));
232 KASSERT(map->size == 0,
233 ("map %p size == %lu on free.",
234 map, (unsigned long)map->size));
235 KASSERT(map->infork == 0,
236 ("map %p infork == %d on free.",
239 #endif /* INVARIANTS */
242 * Allocate a vmspace structure, including a vm_map and pmap,
243 * and initialize those structures. The refcnt is set to 1.
244 * The remaining fields must be initialized by the caller.
247 vmspace_alloc(min, max)
248 vm_offset_t min, max;
253 vm = uma_zalloc(vmspace_zone, M_WAITOK);
254 CTR1(KTR_VM, "vmspace_alloc: %p", vm);
255 _vm_map_init(&vm->vm_map, min, max);
256 pmap_pinit(vmspace_pmap(vm));
257 vm->vm_map.pmap = vmspace_pmap(vm); /* XXX */
267 uma_zone_set_obj(kmapentzone, &kmapentobj, lmin(cnt.v_page_count,
268 (VM_MAX_KERNEL_ADDRESS - KERNBASE) / PAGE_SIZE) / 8);
269 vmspace_zone = uma_zcreate("VMSPACE", sizeof(struct vmspace), NULL,
275 vmspace_zinit, vmspace_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
281 vmspace_dofree(struct vmspace *vm)
283 CTR1(KTR_VM, "vmspace_free: %p", vm);
285 * Lock the map, to wait out all other references to it.
286 * Delete all of the mappings and pages they hold, then call
287 * the pmap module to reclaim anything left.
289 vm_map_lock(&vm->vm_map);
290 (void) vm_map_delete(&vm->vm_map, vm->vm_map.min_offset,
291 vm->vm_map.max_offset);
292 vm_map_unlock(&vm->vm_map);
294 pmap_release(vmspace_pmap(vm));
295 uma_zfree(vmspace_zone, vm);
299 vmspace_free(struct vmspace *vm)
303 if (vm->vm_refcnt == 0)
304 panic("vmspace_free: attempt to free already freed vmspace");
306 if (--vm->vm_refcnt == 0)
311 vmspace_exitfree(struct proc *p)
316 if (p == p->p_vmspace->vm_freer) {
324 * vmspace_swap_count() - count the approximate swap useage in pages for a
327 * Swap useage is determined by taking the proportional swap used by
328 * VM objects backing the VM map. To make up for fractional losses,
329 * if the VM object has any swap use at all the associated map entries
330 * count for at least 1 swap page.
333 vmspace_swap_count(struct vmspace *vmspace)
335 vm_map_t map = &vmspace->vm_map;
339 vm_map_lock_read(map);
340 for (cur = map->header.next; cur != &map->header; cur = cur->next) {
343 if ((cur->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
344 (object = cur->object.vm_object) != NULL &&
345 object->type == OBJT_SWAP
347 int n = (cur->end - cur->start) / PAGE_SIZE;
349 if (object->un_pager.swp.swp_bcount) {
350 count += object->un_pager.swp.swp_bcount *
351 SWAP_META_PAGES * n / object->size + 1;
355 vm_map_unlock_read(map);
360 _vm_map_lock(vm_map_t map, const char *file, int line)
366 error = lockmgr(&map->lock, LK_EXCLUSIVE, NULL, curthread);
367 KASSERT(error == 0, ("%s: failed to get lock", __func__));
372 _vm_map_unlock(vm_map_t map, const char *file, int line)
375 lockmgr(&map->lock, LK_RELEASE, NULL, curthread);
379 _vm_map_lock_read(vm_map_t map, const char *file, int line)
385 error = lockmgr(&map->lock, LK_EXCLUSIVE, NULL, curthread);
386 KASSERT(error == 0, ("%s: failed to get lock", __func__));
390 _vm_map_unlock_read(vm_map_t map, const char *file, int line)
393 lockmgr(&map->lock, LK_RELEASE, NULL, curthread);
397 _vm_map_trylock(vm_map_t map, const char *file, int line)
403 error = lockmgr(&map->lock, LK_EXCLUSIVE | LK_NOWAIT, NULL, curthread);
408 _vm_map_lock_upgrade(vm_map_t map, const char *file, int line)
411 KASSERT(lockstatus(&map->lock, curthread) == LK_EXCLUSIVE,
412 ("%s: lock not held", __func__));
418 _vm_map_lock_downgrade(vm_map_t map, const char *file, int line)
421 KASSERT(lockstatus(&map->lock, curthread) == LK_EXCLUSIVE,
422 ("%s: lock not held", __func__));
426 * vm_map_unlock_and_wait:
429 vm_map_unlock_and_wait(vm_map_t map, boolean_t user_wait)
435 retval = tsleep(&map->root, PVM, "vmmapw", 0);
444 vm_map_wakeup(vm_map_t map)
448 * Acquire and release Giant to prevent a wakeup() from being
449 * performed (and lost) between the vm_map_unlock() and the
450 * tsleep() in vm_map_unlock_and_wait().
458 vmspace_resident_count(struct vmspace *vmspace)
460 return pmap_resident_count(vmspace_pmap(vmspace));
466 * Creates and returns a new empty VM map with
467 * the given physical map structure, and having
468 * the given lower and upper address bounds.
471 vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max)
475 result = uma_zalloc(mapzone, M_WAITOK);
476 CTR1(KTR_VM, "vm_map_create: %p", result);
477 _vm_map_init(result, min, max);
483 * Initialize an existing vm_map structure
484 * such as that in the vmspace structure.
485 * The pmap is set elsewhere.
488 _vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max)
491 map->header.next = map->header.prev = &map->header;
492 map->needs_wakeup = FALSE;
494 map->min_offset = min;
495 map->max_offset = max;
496 map->first_free = &map->header;
502 vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max)
504 _vm_map_init(map, min, max);
505 lockinit(&map->lock, PVM, "thrd_sleep", 0, LK_NOPAUSE);
509 * vm_map_entry_dispose: [ internal use only ]
511 * Inverse of vm_map_entry_create.
514 vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry)
516 uma_zfree(map->system_map ? kmapentzone : mapentzone, entry);
520 * vm_map_entry_create: [ internal use only ]
522 * Allocates a VM map entry for insertion.
523 * No entry fields are filled in.
525 static vm_map_entry_t
526 vm_map_entry_create(vm_map_t map)
528 vm_map_entry_t new_entry;
531 new_entry = uma_zalloc(kmapentzone, M_NOWAIT);
533 new_entry = uma_zalloc(mapentzone, M_WAITOK);
534 if (new_entry == NULL)
535 panic("vm_map_entry_create: kernel resources exhausted");
540 * vm_map_entry_set_behavior:
542 * Set the expected access behavior, either normal, random, or
546 vm_map_entry_set_behavior(vm_map_entry_t entry, u_char behavior)
548 entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) |
549 (behavior & MAP_ENTRY_BEHAV_MASK);
553 * vm_map_entry_splay:
555 * Implements Sleator and Tarjan's top-down splay algorithm. Returns
556 * the vm_map_entry containing the given address. If, however, that
557 * address is not found in the vm_map, returns a vm_map_entry that is
558 * adjacent to the address, coming before or after it.
560 static vm_map_entry_t
561 vm_map_entry_splay(vm_offset_t address, vm_map_entry_t root)
563 struct vm_map_entry dummy;
564 vm_map_entry_t lefttreemax, righttreemin, y;
568 lefttreemax = righttreemin = &dummy;
570 if (address < root->start) {
571 if ((y = root->left) == NULL)
573 if (address < y->start) {
575 root->left = y->right;
578 if ((y = root->left) == NULL)
581 /* Link into the new root's right tree. */
582 righttreemin->left = root;
584 } else if (address >= root->end) {
585 if ((y = root->right) == NULL)
587 if (address >= y->end) {
589 root->right = y->left;
592 if ((y = root->right) == NULL)
595 /* Link into the new root's left tree. */
596 lefttreemax->right = root;
601 /* Assemble the new root. */
602 lefttreemax->right = root->left;
603 righttreemin->left = root->right;
604 root->left = dummy.right;
605 root->right = dummy.left;
610 * vm_map_entry_{un,}link:
612 * Insert/remove entries from maps.
615 vm_map_entry_link(vm_map_t map,
616 vm_map_entry_t after_where,
617 vm_map_entry_t entry)
621 "vm_map_entry_link: map %p, nentries %d, entry %p, after %p", map,
622 map->nentries, entry, after_where);
624 entry->prev = after_where;
625 entry->next = after_where->next;
626 entry->next->prev = entry;
627 after_where->next = entry;
629 if (after_where != &map->header) {
630 if (after_where != map->root)
631 vm_map_entry_splay(after_where->start, map->root);
632 entry->right = after_where->right;
633 entry->left = after_where;
634 after_where->right = NULL;
636 entry->right = map->root;
643 vm_map_entry_unlink(vm_map_t map,
644 vm_map_entry_t entry)
646 vm_map_entry_t next, prev, root;
648 if (entry != map->root)
649 vm_map_entry_splay(entry->start, map->root);
650 if (entry->left == NULL)
653 root = vm_map_entry_splay(entry->start, entry->left);
654 root->right = entry->right;
663 CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map,
664 map->nentries, entry);
668 * vm_map_lookup_entry: [ internal use only ]
670 * Finds the map entry containing (or
671 * immediately preceding) the specified address
672 * in the given map; the entry is returned
673 * in the "entry" parameter. The boolean
674 * result indicates whether the address is
675 * actually contained in the map.
681 vm_map_entry_t *entry) /* OUT */
685 cur = vm_map_entry_splay(address, map->root);
687 *entry = &map->header;
691 if (address >= cur->start) {
693 if (cur->end > address)
704 * Inserts the given whole VM object into the target
705 * map at the specified address range. The object's
706 * size should match that of the address range.
708 * Requires that the map be locked, and leaves it so.
710 * If object is non-NULL, ref count must be bumped by caller
711 * prior to making call to account for the new entry.
714 vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
715 vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max,
718 vm_map_entry_t new_entry;
719 vm_map_entry_t prev_entry;
720 vm_map_entry_t temp_entry;
721 vm_eflags_t protoeflags;
724 * Check that the start and end points are not bogus.
726 if ((start < map->min_offset) || (end > map->max_offset) ||
728 return (KERN_INVALID_ADDRESS);
731 * Find the entry prior to the proposed starting address; if it's part
732 * of an existing entry, this range is bogus.
734 if (vm_map_lookup_entry(map, start, &temp_entry))
735 return (KERN_NO_SPACE);
737 prev_entry = temp_entry;
740 * Assert that the next entry doesn't overlap the end point.
742 if ((prev_entry->next != &map->header) &&
743 (prev_entry->next->start < end))
744 return (KERN_NO_SPACE);
748 if (cow & MAP_COPY_ON_WRITE)
749 protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
751 if (cow & MAP_NOFAULT) {
752 protoeflags |= MAP_ENTRY_NOFAULT;
754 KASSERT(object == NULL,
755 ("vm_map_insert: paradoxical MAP_NOFAULT request"));
757 if (cow & MAP_DISABLE_SYNCER)
758 protoeflags |= MAP_ENTRY_NOSYNC;
759 if (cow & MAP_DISABLE_COREDUMP)
760 protoeflags |= MAP_ENTRY_NOCOREDUMP;
764 * When object is non-NULL, it could be shared with another
765 * process. We have to set or clear OBJ_ONEMAPPING
768 vm_object_lock(object);
769 if ((object->ref_count > 1) || (object->shadow_count != 0)) {
770 vm_object_clear_flag(object, OBJ_ONEMAPPING);
772 vm_object_unlock(object);
774 else if ((prev_entry != &map->header) &&
775 (prev_entry->eflags == protoeflags) &&
776 (prev_entry->end == start) &&
777 (prev_entry->wired_count == 0) &&
778 ((prev_entry->object.vm_object == NULL) ||
779 vm_object_coalesce(prev_entry->object.vm_object,
780 OFF_TO_IDX(prev_entry->offset),
781 (vm_size_t)(prev_entry->end - prev_entry->start),
782 (vm_size_t)(end - prev_entry->end)))) {
784 * We were able to extend the object. Determine if we
785 * can extend the previous map entry to include the
788 if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
789 (prev_entry->protection == prot) &&
790 (prev_entry->max_protection == max)) {
791 map->size += (end - prev_entry->end);
792 prev_entry->end = end;
793 vm_map_simplify_entry(map, prev_entry);
794 return (KERN_SUCCESS);
798 * If we can extend the object but cannot extend the
799 * map entry, we have to create a new map entry. We
800 * must bump the ref count on the extended object to
801 * account for it. object may be NULL.
803 object = prev_entry->object.vm_object;
804 offset = prev_entry->offset +
805 (prev_entry->end - prev_entry->start);
806 vm_object_reference(object);
810 * NOTE: if conditionals fail, object can be NULL here. This occurs
811 * in things like the buffer map where we manage kva but do not manage
818 new_entry = vm_map_entry_create(map);
819 new_entry->start = start;
820 new_entry->end = end;
822 new_entry->eflags = protoeflags;
823 new_entry->object.vm_object = object;
824 new_entry->offset = offset;
825 new_entry->avail_ssize = 0;
827 new_entry->inheritance = VM_INHERIT_DEFAULT;
828 new_entry->protection = prot;
829 new_entry->max_protection = max;
830 new_entry->wired_count = 0;
833 * Insert the new entry into the list
835 vm_map_entry_link(map, prev_entry, new_entry);
836 map->size += new_entry->end - new_entry->start;
839 * Update the free space hint
841 if ((map->first_free == prev_entry) &&
842 (prev_entry->end >= new_entry->start)) {
843 map->first_free = new_entry;
848 * Temporarily removed to avoid MAP_STACK panic, due to
849 * MAP_STACK being a huge hack. Will be added back in
850 * when MAP_STACK (and the user stack mapping) is fixed.
853 * It may be possible to simplify the entry
855 vm_map_simplify_entry(map, new_entry);
858 if (cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) {
860 pmap_object_init_pt(map->pmap, start,
861 object, OFF_TO_IDX(offset), end - start,
862 cow & MAP_PREFAULT_PARTIAL);
866 return (KERN_SUCCESS);
870 * Find sufficient space for `length' bytes in the given map, starting at
871 * `start'. The map must be locked. Returns 0 on success, 1 on no space.
880 vm_map_entry_t entry, next;
883 if (start < map->min_offset)
884 start = map->min_offset;
885 if (start > map->max_offset)
889 * Look for the first possible address; if there's already something
890 * at this address, we have to start after it.
892 if (start == map->min_offset) {
893 if ((entry = map->first_free) != &map->header)
898 if (vm_map_lookup_entry(map, start, &tmp))
904 * Look through the rest of the map, trying to fit a new region in the
905 * gap between existing regions, or after the very last region.
907 for (;; start = (entry = next)->end) {
909 * Find the end of the proposed new region. Be sure we didn't
910 * go beyond the end of the map, or wrap around the address;
911 * if so, we lose. Otherwise, if this is the last entry, or
912 * if the proposed new region fits before the next entry, we
915 end = start + length;
916 if (end > map->max_offset || end < start)
919 if (next == &map->header || next->start >= end)
923 if (map == kernel_map) {
925 if ((ksize = round_page(start + length)) > kernel_vm_end) {
927 pmap_growkernel(ksize);
935 * vm_map_find finds an unallocated region in the target address
936 * map with the given length. The search is defined to be
937 * first-fit from the specified address; the region found is
938 * returned in the same parameter.
940 * If object is non-NULL, ref count must be bumped by caller
941 * prior to making call to account for the new entry.
944 vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
945 vm_offset_t *addr, /* IN/OUT */
946 vm_size_t length, boolean_t find_space, vm_prot_t prot,
947 vm_prot_t max, int cow)
959 if (vm_map_findspace(map, start, length, addr)) {
963 return (KERN_NO_SPACE);
967 result = vm_map_insert(map, object, offset,
968 start, start + length, prot, max, cow);
978 * vm_map_simplify_entry:
980 * Simplify the given map entry by merging with either neighbor. This
981 * routine also has the ability to merge with both neighbors.
983 * The map must be locked.
985 * This routine guarentees that the passed entry remains valid (though
986 * possibly extended). When merging, this routine may delete one or
990 vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry)
992 vm_map_entry_t next, prev;
993 vm_size_t prevsize, esize;
995 if (entry->eflags & (MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP))
999 if (prev != &map->header) {
1000 prevsize = prev->end - prev->start;
1001 if ( (prev->end == entry->start) &&
1002 (prev->object.vm_object == entry->object.vm_object) &&
1003 (!prev->object.vm_object ||
1004 (prev->offset + prevsize == entry->offset)) &&
1005 (prev->eflags == entry->eflags) &&
1006 (prev->protection == entry->protection) &&
1007 (prev->max_protection == entry->max_protection) &&
1008 (prev->inheritance == entry->inheritance) &&
1009 (prev->wired_count == entry->wired_count)) {
1010 if (map->first_free == prev)
1011 map->first_free = entry;
1012 vm_map_entry_unlink(map, prev);
1013 entry->start = prev->start;
1014 entry->offset = prev->offset;
1015 if (prev->object.vm_object)
1016 vm_object_deallocate(prev->object.vm_object);
1017 vm_map_entry_dispose(map, prev);
1022 if (next != &map->header) {
1023 esize = entry->end - entry->start;
1024 if ((entry->end == next->start) &&
1025 (next->object.vm_object == entry->object.vm_object) &&
1026 (!entry->object.vm_object ||
1027 (entry->offset + esize == next->offset)) &&
1028 (next->eflags == entry->eflags) &&
1029 (next->protection == entry->protection) &&
1030 (next->max_protection == entry->max_protection) &&
1031 (next->inheritance == entry->inheritance) &&
1032 (next->wired_count == entry->wired_count)) {
1033 if (map->first_free == next)
1034 map->first_free = entry;
1035 vm_map_entry_unlink(map, next);
1036 entry->end = next->end;
1037 if (next->object.vm_object)
1038 vm_object_deallocate(next->object.vm_object);
1039 vm_map_entry_dispose(map, next);
1044 * vm_map_clip_start: [ internal use only ]
1046 * Asserts that the given entry begins at or after
1047 * the specified address; if necessary,
1048 * it splits the entry into two.
1050 #define vm_map_clip_start(map, entry, startaddr) \
1052 if (startaddr > entry->start) \
1053 _vm_map_clip_start(map, entry, startaddr); \
1057 * This routine is called only when it is known that
1058 * the entry must be split.
1061 _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start)
1063 vm_map_entry_t new_entry;
1066 * Split off the front portion -- note that we must insert the new
1067 * entry BEFORE this one, so that this entry has the specified
1070 vm_map_simplify_entry(map, entry);
1073 * If there is no object backing this entry, we might as well create
1074 * one now. If we defer it, an object can get created after the map
1075 * is clipped, and individual objects will be created for the split-up
1076 * map. This is a bit of a hack, but is also about the best place to
1077 * put this improvement.
1079 if (entry->object.vm_object == NULL && !map->system_map) {
1081 object = vm_object_allocate(OBJT_DEFAULT,
1082 atop(entry->end - entry->start));
1083 entry->object.vm_object = object;
1087 new_entry = vm_map_entry_create(map);
1088 *new_entry = *entry;
1090 new_entry->end = start;
1091 entry->offset += (start - entry->start);
1092 entry->start = start;
1094 vm_map_entry_link(map, entry->prev, new_entry);
1096 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1097 vm_object_reference(new_entry->object.vm_object);
1102 * vm_map_clip_end: [ internal use only ]
1104 * Asserts that the given entry ends at or before
1105 * the specified address; if necessary,
1106 * it splits the entry into two.
1108 #define vm_map_clip_end(map, entry, endaddr) \
1110 if (endaddr < entry->end) \
1111 _vm_map_clip_end(map, entry, endaddr); \
1115 * This routine is called only when it is known that
1116 * the entry must be split.
1119 _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end)
1121 vm_map_entry_t new_entry;
1124 * If there is no object backing this entry, we might as well create
1125 * one now. If we defer it, an object can get created after the map
1126 * is clipped, and individual objects will be created for the split-up
1127 * map. This is a bit of a hack, but is also about the best place to
1128 * put this improvement.
1130 if (entry->object.vm_object == NULL && !map->system_map) {
1132 object = vm_object_allocate(OBJT_DEFAULT,
1133 atop(entry->end - entry->start));
1134 entry->object.vm_object = object;
1139 * Create a new entry and insert it AFTER the specified entry
1141 new_entry = vm_map_entry_create(map);
1142 *new_entry = *entry;
1144 new_entry->start = entry->end = end;
1145 new_entry->offset += (end - entry->start);
1147 vm_map_entry_link(map, entry, new_entry);
1149 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1150 vm_object_reference(new_entry->object.vm_object);
1155 * VM_MAP_RANGE_CHECK: [ internal use only ]
1157 * Asserts that the starting and ending region
1158 * addresses fall within the valid range of the map.
1160 #define VM_MAP_RANGE_CHECK(map, start, end) \
1162 if (start < vm_map_min(map)) \
1163 start = vm_map_min(map); \
1164 if (end > vm_map_max(map)) \
1165 end = vm_map_max(map); \
1171 * vm_map_submap: [ kernel use only ]
1173 * Mark the given range as handled by a subordinate map.
1175 * This range must have been created with vm_map_find,
1176 * and no other operations may have been performed on this
1177 * range prior to calling vm_map_submap.
1179 * Only a limited number of operations can be performed
1180 * within this rage after calling vm_map_submap:
1182 * [Don't try vm_map_copy!]
1184 * To remove a submapping, one must first remove the
1185 * range from the superior map, and then destroy the
1186 * submap (if desired). [Better yet, don't try it.]
1195 vm_map_entry_t entry;
1196 int result = KERN_INVALID_ARGUMENT;
1200 VM_MAP_RANGE_CHECK(map, start, end);
1202 if (vm_map_lookup_entry(map, start, &entry)) {
1203 vm_map_clip_start(map, entry, start);
1205 entry = entry->next;
1207 vm_map_clip_end(map, entry, end);
1209 if ((entry->start == start) && (entry->end == end) &&
1210 ((entry->eflags & MAP_ENTRY_COW) == 0) &&
1211 (entry->object.vm_object == NULL)) {
1212 entry->object.sub_map = submap;
1213 entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
1214 result = KERN_SUCCESS;
1224 * Sets the protection of the specified address
1225 * region in the target map. If "set_max" is
1226 * specified, the maximum protection is to be set;
1227 * otherwise, only the current protection is affected.
1230 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
1231 vm_prot_t new_prot, boolean_t set_max)
1233 vm_map_entry_t current;
1234 vm_map_entry_t entry;
1238 VM_MAP_RANGE_CHECK(map, start, end);
1240 if (vm_map_lookup_entry(map, start, &entry)) {
1241 vm_map_clip_start(map, entry, start);
1243 entry = entry->next;
1247 * Make a first pass to check for protection violations.
1250 while ((current != &map->header) && (current->start < end)) {
1251 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1253 return (KERN_INVALID_ARGUMENT);
1255 if ((new_prot & current->max_protection) != new_prot) {
1257 return (KERN_PROTECTION_FAILURE);
1259 current = current->next;
1263 * Go back and fix up protections. [Note that clipping is not
1264 * necessary the second time.]
1267 while ((current != &map->header) && (current->start < end)) {
1270 vm_map_clip_end(map, current, end);
1272 old_prot = current->protection;
1274 current->protection =
1275 (current->max_protection = new_prot) &
1278 current->protection = new_prot;
1281 * Update physical map if necessary. Worry about copy-on-write
1282 * here -- CHECK THIS XXX
1284 if (current->protection != old_prot) {
1286 #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1288 pmap_protect(map->pmap, current->start,
1290 current->protection & MASK(current));
1294 vm_map_simplify_entry(map, current);
1295 current = current->next;
1298 return (KERN_SUCCESS);
1304 * This routine traverses a processes map handling the madvise
1305 * system call. Advisories are classified as either those effecting
1306 * the vm_map_entry structure, or those effecting the underlying
1316 vm_map_entry_t current, entry;
1320 * Some madvise calls directly modify the vm_map_entry, in which case
1321 * we need to use an exclusive lock on the map and we need to perform
1322 * various clipping operations. Otherwise we only need a read-lock
1327 case MADV_SEQUENTIAL:
1339 vm_map_lock_read(map);
1342 return (KERN_INVALID_ARGUMENT);
1346 * Locate starting entry and clip if necessary.
1348 VM_MAP_RANGE_CHECK(map, start, end);
1350 if (vm_map_lookup_entry(map, start, &entry)) {
1352 vm_map_clip_start(map, entry, start);
1354 entry = entry->next;
1359 * madvise behaviors that are implemented in the vm_map_entry.
1361 * We clip the vm_map_entry so that behavioral changes are
1362 * limited to the specified address range.
1364 for (current = entry;
1365 (current != &map->header) && (current->start < end);
1366 current = current->next
1368 if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1371 vm_map_clip_end(map, current, end);
1375 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
1377 case MADV_SEQUENTIAL:
1378 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
1381 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
1384 current->eflags |= MAP_ENTRY_NOSYNC;
1387 current->eflags &= ~MAP_ENTRY_NOSYNC;
1390 current->eflags |= MAP_ENTRY_NOCOREDUMP;
1393 current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
1398 vm_map_simplify_entry(map, current);
1406 * madvise behaviors that are implemented in the underlying
1409 * Since we don't clip the vm_map_entry, we have to clip
1410 * the vm_object pindex and count.
1412 for (current = entry;
1413 (current != &map->header) && (current->start < end);
1414 current = current->next
1416 vm_offset_t useStart;
1418 if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1421 pindex = OFF_TO_IDX(current->offset);
1422 count = atop(current->end - current->start);
1423 useStart = current->start;
1425 if (current->start < start) {
1426 pindex += atop(start - current->start);
1427 count -= atop(start - current->start);
1430 if (current->end > end)
1431 count -= atop(current->end - end);
1436 vm_object_madvise(current->object.vm_object,
1437 pindex, count, behav);
1438 if (behav == MADV_WILLNEED) {
1440 pmap_object_init_pt(
1443 current->object.vm_object,
1445 (count << PAGE_SHIFT),
1446 MAP_PREFAULT_MADVISE
1451 vm_map_unlock_read(map);
1460 * Sets the inheritance of the specified address
1461 * range in the target map. Inheritance
1462 * affects how the map will be shared with
1463 * child maps at the time of vm_map_fork.
1466 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
1467 vm_inherit_t new_inheritance)
1469 vm_map_entry_t entry;
1470 vm_map_entry_t temp_entry;
1472 switch (new_inheritance) {
1473 case VM_INHERIT_NONE:
1474 case VM_INHERIT_COPY:
1475 case VM_INHERIT_SHARE:
1478 return (KERN_INVALID_ARGUMENT);
1481 VM_MAP_RANGE_CHECK(map, start, end);
1482 if (vm_map_lookup_entry(map, start, &temp_entry)) {
1484 vm_map_clip_start(map, entry, start);
1486 entry = temp_entry->next;
1487 while ((entry != &map->header) && (entry->start < end)) {
1488 vm_map_clip_end(map, entry, end);
1489 entry->inheritance = new_inheritance;
1490 vm_map_simplify_entry(map, entry);
1491 entry = entry->next;
1494 return (KERN_SUCCESS);
1500 * Implements both kernel and user unwiring.
1503 vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
1504 boolean_t user_unwire)
1506 vm_map_entry_t entry, first_entry, tmp_entry;
1507 vm_offset_t saved_start;
1508 unsigned int last_timestamp;
1510 boolean_t need_wakeup, result;
1513 VM_MAP_RANGE_CHECK(map, start, end);
1514 if (!vm_map_lookup_entry(map, start, &first_entry)) {
1516 return (KERN_INVALID_ADDRESS);
1518 last_timestamp = map->timestamp;
1519 entry = first_entry;
1520 while (entry != &map->header && entry->start < end) {
1521 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1523 * We have not yet clipped the entry.
1525 saved_start = (start >= entry->start) ? start :
1527 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1528 if (vm_map_unlock_and_wait(map, user_unwire)) {
1530 * Allow interruption of user unwiring?
1534 if (last_timestamp+1 != map->timestamp) {
1536 * Look again for the entry because the map was
1537 * modified while it was unlocked.
1538 * Specifically, the entry may have been
1539 * clipped, merged, or deleted.
1541 if (!vm_map_lookup_entry(map, saved_start,
1543 if (saved_start == start) {
1545 * First_entry has been deleted.
1548 return (KERN_INVALID_ADDRESS);
1551 rv = KERN_INVALID_ADDRESS;
1554 if (entry == first_entry)
1555 first_entry = tmp_entry;
1560 last_timestamp = map->timestamp;
1563 vm_map_clip_start(map, entry, start);
1564 vm_map_clip_end(map, entry, end);
1566 * Mark the entry in case the map lock is released. (See
1569 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
1571 * Check the map for holes in the specified region.
1573 if (entry->end < end && (entry->next == &map->header ||
1574 entry->next->start > entry->end)) {
1576 rv = KERN_INVALID_ADDRESS;
1580 * Require that the entry is wired.
1582 if (entry->wired_count == 0 || (user_unwire &&
1583 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0)) {
1585 rv = KERN_INVALID_ARGUMENT;
1588 entry = entry->next;
1592 need_wakeup = FALSE;
1593 if (first_entry == NULL) {
1594 result = vm_map_lookup_entry(map, start, &first_entry);
1595 KASSERT(result, ("vm_map_unwire: lookup failed"));
1597 entry = first_entry;
1598 while (entry != &map->header && entry->start < end) {
1599 if (rv == KERN_SUCCESS) {
1601 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
1602 entry->wired_count--;
1603 if (entry->wired_count == 0) {
1605 * Retain the map lock.
1607 vm_fault_unwire(map, entry->start, entry->end);
1610 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
1611 ("vm_map_unwire: in-transition flag missing"));
1612 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
1613 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
1614 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
1617 vm_map_simplify_entry(map, entry);
1618 entry = entry->next;
1629 * Implements both kernel and user wiring.
1632 vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
1633 boolean_t user_wire)
1635 vm_map_entry_t entry, first_entry, tmp_entry;
1636 vm_offset_t saved_end, saved_start;
1637 unsigned int last_timestamp;
1639 boolean_t need_wakeup, result;
1642 VM_MAP_RANGE_CHECK(map, start, end);
1643 if (!vm_map_lookup_entry(map, start, &first_entry)) {
1645 return (KERN_INVALID_ADDRESS);
1647 last_timestamp = map->timestamp;
1648 entry = first_entry;
1649 while (entry != &map->header && entry->start < end) {
1650 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1652 * We have not yet clipped the entry.
1654 saved_start = (start >= entry->start) ? start :
1656 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1657 if (vm_map_unlock_and_wait(map, user_wire)) {
1659 * Allow interruption of user wiring?
1663 if (last_timestamp + 1 != map->timestamp) {
1665 * Look again for the entry because the map was
1666 * modified while it was unlocked.
1667 * Specifically, the entry may have been
1668 * clipped, merged, or deleted.
1670 if (!vm_map_lookup_entry(map, saved_start,
1672 if (saved_start == start) {
1674 * first_entry has been deleted.
1677 return (KERN_INVALID_ADDRESS);
1680 rv = KERN_INVALID_ADDRESS;
1683 if (entry == first_entry)
1684 first_entry = tmp_entry;
1689 last_timestamp = map->timestamp;
1692 vm_map_clip_start(map, entry, start);
1693 vm_map_clip_end(map, entry, end);
1695 * Mark the entry in case the map lock is released. (See
1698 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
1702 if (entry->wired_count == 0) {
1703 entry->wired_count++;
1704 saved_start = entry->start;
1705 saved_end = entry->end;
1707 * Release the map lock, relying on the in-transition
1711 rv = vm_fault_wire(map, saved_start, saved_end,
1714 if (last_timestamp + 1 != map->timestamp) {
1716 * Look again for the entry because the map was
1717 * modified while it was unlocked. The entry
1718 * may have been clipped, but NOT merged or
1721 result = vm_map_lookup_entry(map, saved_start,
1723 KASSERT(result, ("vm_map_wire: lookup failed"));
1724 if (entry == first_entry)
1725 first_entry = tmp_entry;
1729 while (entry->end < saved_end) {
1730 if (rv != KERN_SUCCESS) {
1731 KASSERT(entry->wired_count == 1,
1732 ("vm_map_wire: bad count"));
1733 entry->wired_count = -1;
1735 entry = entry->next;
1738 last_timestamp = map->timestamp;
1739 if (rv != KERN_SUCCESS) {
1740 KASSERT(entry->wired_count == 1,
1741 ("vm_map_wire: bad count"));
1743 * Assign an out-of-range value to represent
1744 * the failure to wire this entry.
1746 entry->wired_count = -1;
1750 } else if (!user_wire ||
1751 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
1752 entry->wired_count++;
1755 * Check the map for holes in the specified region.
1757 if (entry->end < end && (entry->next == &map->header ||
1758 entry->next->start > entry->end)) {
1760 rv = KERN_INVALID_ADDRESS;
1763 entry = entry->next;
1767 need_wakeup = FALSE;
1768 if (first_entry == NULL) {
1769 result = vm_map_lookup_entry(map, start, &first_entry);
1770 KASSERT(result, ("vm_map_wire: lookup failed"));
1772 entry = first_entry;
1773 while (entry != &map->header && entry->start < end) {
1774 if (rv == KERN_SUCCESS) {
1776 entry->eflags |= MAP_ENTRY_USER_WIRED;
1777 } else if (entry->wired_count == -1) {
1779 * Wiring failed on this entry. Thus, unwiring is
1782 entry->wired_count = 0;
1784 if (!user_wire || (entry->wired_count == 1 &&
1785 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0))
1786 entry->wired_count--;
1787 if (entry->wired_count == 0) {
1789 * Retain the map lock.
1791 vm_fault_unwire(map, entry->start, entry->end);
1794 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
1795 ("vm_map_wire: in-transition flag missing"));
1796 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
1797 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
1798 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
1801 vm_map_simplify_entry(map, entry);
1802 entry = entry->next;
1813 * Push any dirty cached pages in the address range to their pager.
1814 * If syncio is TRUE, dirty pages are written synchronously.
1815 * If invalidate is TRUE, any cached pages are freed as well.
1817 * Returns an error if any part of the specified range is not mapped.
1825 boolean_t invalidate)
1827 vm_map_entry_t current;
1828 vm_map_entry_t entry;
1831 vm_ooffset_t offset;
1835 vm_map_lock_read(map);
1836 VM_MAP_RANGE_CHECK(map, start, end);
1837 if (!vm_map_lookup_entry(map, start, &entry)) {
1838 vm_map_unlock_read(map);
1839 return (KERN_INVALID_ADDRESS);
1842 * Make a first pass to check for holes.
1844 for (current = entry; current->start < end; current = current->next) {
1845 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1846 vm_map_unlock_read(map);
1847 return (KERN_INVALID_ARGUMENT);
1849 if (end > current->end &&
1850 (current->next == &map->header ||
1851 current->end != current->next->start)) {
1852 vm_map_unlock_read(map);
1853 return (KERN_INVALID_ADDRESS);
1858 pmap_remove(vm_map_pmap(map), start, end);
1860 * Make a second pass, cleaning/uncaching pages from the indicated
1863 for (current = entry; current->start < end; current = current->next) {
1864 offset = current->offset + (start - current->start);
1865 size = (end <= current->end ? end : current->end) - start;
1866 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1868 vm_map_entry_t tentry;
1871 smap = current->object.sub_map;
1872 vm_map_lock_read(smap);
1873 (void) vm_map_lookup_entry(smap, offset, &tentry);
1874 tsize = tentry->end - offset;
1877 object = tentry->object.vm_object;
1878 offset = tentry->offset + (offset - tentry->start);
1879 vm_map_unlock_read(smap);
1881 object = current->object.vm_object;
1884 * Note that there is absolutely no sense in writing out
1885 * anonymous objects, so we track down the vnode object
1887 * We invalidate (remove) all pages from the address space
1888 * anyway, for semantic correctness.
1890 * note: certain anonymous maps, such as MAP_NOSYNC maps,
1891 * may start out with a NULL object.
1893 while (object && object->backing_object) {
1894 object = object->backing_object;
1895 offset += object->backing_object_offset;
1896 if (object->size < OFF_TO_IDX(offset + size))
1897 size = IDX_TO_OFF(object->size) - offset;
1899 if (object && (object->type == OBJT_VNODE) &&
1900 (current->protection & VM_PROT_WRITE)) {
1902 * Flush pages if writing is allowed, invalidate them
1903 * if invalidation requested. Pages undergoing I/O
1904 * will be ignored by vm_object_page_remove().
1906 * We cannot lock the vnode and then wait for paging
1907 * to complete without deadlocking against vm_fault.
1908 * Instead we simply call vm_object_page_remove() and
1909 * allow it to block internally on a page-by-page
1910 * basis when it encounters pages undergoing async
1915 vm_object_reference(object);
1916 vn_lock(object->handle, LK_EXCLUSIVE | LK_RETRY, curthread);
1917 flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
1918 flags |= invalidate ? OBJPC_INVAL : 0;
1919 vm_object_page_clean(object,
1921 OFF_TO_IDX(offset + size + PAGE_MASK),
1923 VOP_UNLOCK(object->handle, 0, curthread);
1924 vm_object_deallocate(object);
1926 if (object && invalidate &&
1927 ((object->type == OBJT_VNODE) ||
1928 (object->type == OBJT_DEVICE))) {
1929 vm_object_reference(object);
1930 vm_object_page_remove(object,
1932 OFF_TO_IDX(offset + size + PAGE_MASK),
1934 vm_object_deallocate(object);
1939 vm_map_unlock_read(map);
1940 return (KERN_SUCCESS);
1944 * vm_map_entry_unwire: [ internal use only ]
1946 * Make the region specified by this entry pageable.
1948 * The map in question should be locked.
1949 * [This is the reason for this routine's existence.]
1952 vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
1954 vm_fault_unwire(map, entry->start, entry->end);
1955 entry->wired_count = 0;
1959 * vm_map_entry_delete: [ internal use only ]
1961 * Deallocate the given entry from the target map.
1964 vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry)
1966 vm_map_entry_unlink(map, entry);
1967 map->size -= entry->end - entry->start;
1969 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1970 vm_object_deallocate(entry->object.vm_object);
1973 vm_map_entry_dispose(map, entry);
1977 * vm_map_delete: [ internal use only ]
1979 * Deallocates the given address range from the target
1983 vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end)
1986 vm_map_entry_t entry;
1987 vm_map_entry_t first_entry;
1990 * Find the start of the region, and clip it
1992 if (!vm_map_lookup_entry(map, start, &first_entry))
1993 entry = first_entry->next;
1995 entry = first_entry;
1996 vm_map_clip_start(map, entry, start);
2000 * Save the free space hint
2002 if (entry == &map->header) {
2003 map->first_free = &map->header;
2004 } else if (map->first_free->start >= start) {
2005 map->first_free = entry->prev;
2009 * Step through all entries in this region
2011 while ((entry != &map->header) && (entry->start < end)) {
2012 vm_map_entry_t next;
2014 vm_pindex_t offidxstart, offidxend, count;
2017 * Wait for wiring or unwiring of an entry to complete.
2019 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0) {
2020 unsigned int last_timestamp;
2021 vm_offset_t saved_start;
2022 vm_map_entry_t tmp_entry;
2024 saved_start = entry->start;
2025 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2026 last_timestamp = map->timestamp;
2027 (void) vm_map_unlock_and_wait(map, FALSE);
2029 if (last_timestamp + 1 != map->timestamp) {
2031 * Look again for the entry because the map was
2032 * modified while it was unlocked.
2033 * Specifically, the entry may have been
2034 * clipped, merged, or deleted.
2036 if (!vm_map_lookup_entry(map, saved_start,
2038 entry = tmp_entry->next;
2041 vm_map_clip_start(map, entry,
2047 vm_map_clip_end(map, entry, end);
2053 offidxstart = OFF_TO_IDX(entry->offset);
2054 count = OFF_TO_IDX(e - s);
2055 object = entry->object.vm_object;
2058 * Unwire before removing addresses from the pmap; otherwise,
2059 * unwiring will put the entries back in the pmap.
2061 if (entry->wired_count != 0) {
2062 vm_map_entry_unwire(map, entry);
2065 offidxend = offidxstart + count;
2067 if ((object == kernel_object) || (object == kmem_object)) {
2068 vm_object_page_remove(object, offidxstart, offidxend, FALSE);
2071 pmap_remove(map->pmap, s, e);
2072 if (object != NULL &&
2073 object->ref_count != 1 &&
2074 (object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING &&
2075 (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2076 vm_object_collapse(object);
2077 vm_object_page_remove(object, offidxstart, offidxend, FALSE);
2078 if (object->type == OBJT_SWAP) {
2079 swap_pager_freespace(object, offidxstart, count);
2081 if (offidxend >= object->size &&
2082 offidxstart < object->size) {
2083 object->size = offidxstart;
2090 * Delete the entry (which may delete the object) only after
2091 * removing all pmap entries pointing to its pages.
2092 * (Otherwise, its page frames may be reallocated, and any
2093 * modify bits will be set in the wrong object!)
2095 vm_map_entry_delete(map, entry);
2098 return (KERN_SUCCESS);
2104 * Remove the given address range from the target map.
2105 * This is the exported form of vm_map_delete.
2108 vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
2112 if (map == kmem_map)
2116 VM_MAP_RANGE_CHECK(map, start, end);
2117 result = vm_map_delete(map, start, end);
2120 if (map == kmem_map)
2127 * vm_map_check_protection:
2129 * Assert that the target map allows the specified
2130 * privilege on the entire address region given.
2131 * The entire region must be allocated.
2134 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
2135 vm_prot_t protection)
2137 vm_map_entry_t entry;
2138 vm_map_entry_t tmp_entry;
2140 vm_map_lock_read(map);
2141 if (!vm_map_lookup_entry(map, start, &tmp_entry)) {
2142 vm_map_unlock_read(map);
2147 while (start < end) {
2148 if (entry == &map->header) {
2149 vm_map_unlock_read(map);
2155 if (start < entry->start) {
2156 vm_map_unlock_read(map);
2160 * Check protection associated with entry.
2162 if ((entry->protection & protection) != protection) {
2163 vm_map_unlock_read(map);
2166 /* go to next entry */
2168 entry = entry->next;
2170 vm_map_unlock_read(map);
2175 * vm_map_copy_entry:
2177 * Copies the contents of the source entry to the destination
2178 * entry. The entries *must* be aligned properly.
2184 vm_map_entry_t src_entry,
2185 vm_map_entry_t dst_entry)
2187 vm_object_t src_object;
2189 if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
2192 if (src_entry->wired_count == 0) {
2195 * If the source entry is marked needs_copy, it is already
2198 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
2199 pmap_protect(src_map->pmap,
2202 src_entry->protection & ~VM_PROT_WRITE);
2206 * Make a copy of the object.
2208 if ((src_object = src_entry->object.vm_object) != NULL) {
2210 if ((src_object->handle == NULL) &&
2211 (src_object->type == OBJT_DEFAULT ||
2212 src_object->type == OBJT_SWAP)) {
2213 vm_object_collapse(src_object);
2214 if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
2215 vm_object_split(src_entry);
2216 src_object = src_entry->object.vm_object;
2220 vm_object_reference(src_object);
2221 vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
2222 dst_entry->object.vm_object = src_object;
2223 src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2224 dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2225 dst_entry->offset = src_entry->offset;
2227 dst_entry->object.vm_object = NULL;
2228 dst_entry->offset = 0;
2231 pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
2232 dst_entry->end - dst_entry->start, src_entry->start);
2235 * Of course, wired down pages can't be set copy-on-write.
2236 * Cause wired pages to be copied into the new map by
2237 * simulating faults (the new pages are pageable)
2239 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry);
2245 * Create a new process vmspace structure and vm_map
2246 * based on those of an existing process. The new map
2247 * is based on the old map, according to the inheritance
2248 * values on the regions in that map.
2250 * The source map must not be locked.
2253 vmspace_fork(struct vmspace *vm1)
2255 struct vmspace *vm2;
2256 vm_map_t old_map = &vm1->vm_map;
2258 vm_map_entry_t old_entry;
2259 vm_map_entry_t new_entry;
2264 vm_map_lock(old_map);
2265 old_map->infork = 1;
2267 vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
2268 bcopy(&vm1->vm_startcopy, &vm2->vm_startcopy,
2269 (caddr_t) &vm1->vm_endcopy - (caddr_t) &vm1->vm_startcopy);
2270 new_map = &vm2->vm_map; /* XXX */
2271 new_map->timestamp = 1;
2273 old_entry = old_map->header.next;
2275 while (old_entry != &old_map->header) {
2276 if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2277 panic("vm_map_fork: encountered a submap");
2279 switch (old_entry->inheritance) {
2280 case VM_INHERIT_NONE:
2283 case VM_INHERIT_SHARE:
2285 * Clone the entry, creating the shared object if necessary.
2287 object = old_entry->object.vm_object;
2288 if (object == NULL) {
2289 object = vm_object_allocate(OBJT_DEFAULT,
2290 atop(old_entry->end - old_entry->start));
2291 old_entry->object.vm_object = object;
2292 old_entry->offset = (vm_offset_t) 0;
2296 * Add the reference before calling vm_object_shadow
2297 * to insure that a shadow object is created.
2299 vm_object_reference(object);
2300 if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
2301 vm_object_shadow(&old_entry->object.vm_object,
2303 atop(old_entry->end - old_entry->start));
2304 old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
2305 /* Transfer the second reference too. */
2306 vm_object_reference(
2307 old_entry->object.vm_object);
2308 vm_object_deallocate(object);
2309 object = old_entry->object.vm_object;
2311 vm_object_clear_flag(object, OBJ_ONEMAPPING);
2314 * Clone the entry, referencing the shared object.
2316 new_entry = vm_map_entry_create(new_map);
2317 *new_entry = *old_entry;
2318 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2319 new_entry->wired_count = 0;
2322 * Insert the entry into the new map -- we know we're
2323 * inserting at the end of the new map.
2325 vm_map_entry_link(new_map, new_map->header.prev,
2329 * Update the physical map
2331 pmap_copy(new_map->pmap, old_map->pmap,
2333 (old_entry->end - old_entry->start),
2337 case VM_INHERIT_COPY:
2339 * Clone the entry and link into the map.
2341 new_entry = vm_map_entry_create(new_map);
2342 *new_entry = *old_entry;
2343 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2344 new_entry->wired_count = 0;
2345 new_entry->object.vm_object = NULL;
2346 vm_map_entry_link(new_map, new_map->header.prev,
2348 vm_map_copy_entry(old_map, new_map, old_entry,
2352 old_entry = old_entry->next;
2355 new_map->size = old_map->size;
2356 old_map->infork = 0;
2357 vm_map_unlock(old_map);
2363 vm_map_stack (vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
2364 vm_prot_t prot, vm_prot_t max, int cow)
2366 vm_map_entry_t prev_entry;
2367 vm_map_entry_t new_stack_entry;
2368 vm_size_t init_ssize;
2371 if (addrbos < vm_map_min(map))
2372 return (KERN_NO_SPACE);
2374 if (max_ssize < sgrowsiz)
2375 init_ssize = max_ssize;
2377 init_ssize = sgrowsiz;
2381 /* If addr is already mapped, no go */
2382 if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
2384 return (KERN_NO_SPACE);
2387 /* If we would blow our VMEM resource limit, no go */
2388 if (map->size + init_ssize >
2389 curthread->td_proc->p_rlimit[RLIMIT_VMEM].rlim_cur) {
2391 return (KERN_NO_SPACE);
2394 /* If we can't accomodate max_ssize in the current mapping,
2395 * no go. However, we need to be aware that subsequent user
2396 * mappings might map into the space we have reserved for
2397 * stack, and currently this space is not protected.
2399 * Hopefully we will at least detect this condition
2400 * when we try to grow the stack.
2402 if ((prev_entry->next != &map->header) &&
2403 (prev_entry->next->start < addrbos + max_ssize)) {
2405 return (KERN_NO_SPACE);
2408 /* We initially map a stack of only init_ssize. We will
2409 * grow as needed later. Since this is to be a grow
2410 * down stack, we map at the top of the range.
2412 * Note: we would normally expect prot and max to be
2413 * VM_PROT_ALL, and cow to be 0. Possibly we should
2414 * eliminate these as input parameters, and just
2415 * pass these values here in the insert call.
2417 rv = vm_map_insert(map, NULL, 0, addrbos + max_ssize - init_ssize,
2418 addrbos + max_ssize, prot, max, cow);
2420 /* Now set the avail_ssize amount */
2421 if (rv == KERN_SUCCESS){
2422 if (prev_entry != &map->header)
2423 vm_map_clip_end(map, prev_entry, addrbos + max_ssize - init_ssize);
2424 new_stack_entry = prev_entry->next;
2425 if (new_stack_entry->end != addrbos + max_ssize ||
2426 new_stack_entry->start != addrbos + max_ssize - init_ssize)
2427 panic ("Bad entry start/end for new stack entry");
2429 new_stack_entry->avail_ssize = max_ssize - init_ssize;
2436 /* Attempts to grow a vm stack entry. Returns KERN_SUCCESS if the
2437 * desired address is already mapped, or if we successfully grow
2438 * the stack. Also returns KERN_SUCCESS if addr is outside the
2439 * stack range (this is strange, but preserves compatibility with
2440 * the grow function in vm_machdep.c).
2443 vm_map_growstack (struct proc *p, vm_offset_t addr)
2445 vm_map_entry_t prev_entry;
2446 vm_map_entry_t stack_entry;
2447 vm_map_entry_t new_stack_entry;
2448 struct vmspace *vm = p->p_vmspace;
2449 vm_map_t map = &vm->vm_map;
2458 vm_map_lock_read(map);
2460 /* If addr is already in the entry range, no need to grow.*/
2461 if (vm_map_lookup_entry(map, addr, &prev_entry)) {
2462 vm_map_unlock_read(map);
2463 return (KERN_SUCCESS);
2466 if ((stack_entry = prev_entry->next) == &map->header) {
2467 vm_map_unlock_read(map);
2468 return (KERN_SUCCESS);
2470 if (prev_entry == &map->header)
2471 end = stack_entry->start - stack_entry->avail_ssize;
2473 end = prev_entry->end;
2475 /* This next test mimics the old grow function in vm_machdep.c.
2476 * It really doesn't quite make sense, but we do it anyway
2477 * for compatibility.
2479 * If not growable stack, return success. This signals the
2480 * caller to proceed as he would normally with normal vm.
2482 if (stack_entry->avail_ssize < 1 ||
2483 addr >= stack_entry->start ||
2484 addr < stack_entry->start - stack_entry->avail_ssize) {
2485 vm_map_unlock_read(map);
2486 return (KERN_SUCCESS);
2489 /* Find the minimum grow amount */
2490 grow_amount = roundup (stack_entry->start - addr, PAGE_SIZE);
2491 if (grow_amount > stack_entry->avail_ssize) {
2492 vm_map_unlock_read(map);
2493 return (KERN_NO_SPACE);
2496 /* If there is no longer enough space between the entries
2497 * nogo, and adjust the available space. Note: this
2498 * should only happen if the user has mapped into the
2499 * stack area after the stack was created, and is
2500 * probably an error.
2502 * This also effectively destroys any guard page the user
2503 * might have intended by limiting the stack size.
2505 if (grow_amount > stack_entry->start - end) {
2506 if (vm_map_lock_upgrade(map))
2509 stack_entry->avail_ssize = stack_entry->start - end;
2512 return (KERN_NO_SPACE);
2515 is_procstack = addr >= (vm_offset_t)vm->vm_maxsaddr;
2517 /* If this is the main process stack, see if we're over the
2520 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount >
2521 p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
2522 vm_map_unlock_read(map);
2523 return (KERN_NO_SPACE);
2526 /* Round up the grow amount modulo SGROWSIZ */
2527 grow_amount = roundup (grow_amount, sgrowsiz);
2528 if (grow_amount > stack_entry->avail_ssize) {
2529 grow_amount = stack_entry->avail_ssize;
2531 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount >
2532 p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
2533 grow_amount = p->p_rlimit[RLIMIT_STACK].rlim_cur -
2537 /* If we would blow our VMEM resource limit, no go */
2538 if (map->size + grow_amount >
2539 curthread->td_proc->p_rlimit[RLIMIT_VMEM].rlim_cur) {
2540 vm_map_unlock_read(map);
2541 return (KERN_NO_SPACE);
2544 if (vm_map_lock_upgrade(map))
2547 /* Get the preliminary new entry start value */
2548 addr = stack_entry->start - grow_amount;
2550 /* If this puts us into the previous entry, cut back our growth
2551 * to the available space. Also, see the note above.
2554 stack_entry->avail_ssize = stack_entry->start - end;
2558 rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start,
2559 p->p_sysent->sv_stackprot, VM_PROT_ALL, 0);
2561 /* Adjust the available stack space by the amount we grew. */
2562 if (rv == KERN_SUCCESS) {
2563 if (prev_entry != &map->header)
2564 vm_map_clip_end(map, prev_entry, addr);
2565 new_stack_entry = prev_entry->next;
2566 if (new_stack_entry->end != stack_entry->start ||
2567 new_stack_entry->start != addr)
2568 panic ("Bad stack grow start/end in new stack entry");
2570 new_stack_entry->avail_ssize = stack_entry->avail_ssize -
2571 (new_stack_entry->end -
2572 new_stack_entry->start);
2574 vm->vm_ssize += btoc(new_stack_entry->end -
2575 new_stack_entry->start);
2584 * Unshare the specified VM space for exec. If other processes are
2585 * mapped to it, then create a new one. The new vmspace is null.
2588 vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser)
2590 struct vmspace *oldvmspace = p->p_vmspace;
2591 struct vmspace *newvmspace;
2594 newvmspace = vmspace_alloc(minuser, maxuser);
2595 bcopy(&oldvmspace->vm_startcopy, &newvmspace->vm_startcopy,
2596 (caddr_t) (newvmspace + 1) - (caddr_t) &newvmspace->vm_startcopy);
2598 * This code is written like this for prototype purposes. The
2599 * goal is to avoid running down the vmspace here, but let the
2600 * other process's that are still using the vmspace to finally
2601 * run it down. Even though there is little or no chance of blocking
2602 * here, it is a good idea to keep this form for future mods.
2604 p->p_vmspace = newvmspace;
2605 pmap_pinit2(vmspace_pmap(newvmspace));
2606 vmspace_free(oldvmspace);
2607 if (p == curthread->td_proc) /* XXXKSE ? */
2608 pmap_activate(curthread);
2612 * Unshare the specified VM space for forcing COW. This
2613 * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
2616 vmspace_unshare(struct proc *p)
2618 struct vmspace *oldvmspace = p->p_vmspace;
2619 struct vmspace *newvmspace;
2622 if (oldvmspace->vm_refcnt == 1)
2624 newvmspace = vmspace_fork(oldvmspace);
2625 p->p_vmspace = newvmspace;
2626 pmap_pinit2(vmspace_pmap(newvmspace));
2627 vmspace_free(oldvmspace);
2628 if (p == curthread->td_proc) /* XXXKSE ? */
2629 pmap_activate(curthread);
2635 * Finds the VM object, offset, and
2636 * protection for a given virtual address in the
2637 * specified map, assuming a page fault of the
2640 * Leaves the map in question locked for read; return
2641 * values are guaranteed until a vm_map_lookup_done
2642 * call is performed. Note that the map argument
2643 * is in/out; the returned map must be used in
2644 * the call to vm_map_lookup_done.
2646 * A handle (out_entry) is returned for use in
2647 * vm_map_lookup_done, to make that fast.
2649 * If a lookup is requested with "write protection"
2650 * specified, the map may be changed to perform virtual
2651 * copying operations, although the data referenced will
2655 vm_map_lookup(vm_map_t *var_map, /* IN/OUT */
2657 vm_prot_t fault_typea,
2658 vm_map_entry_t *out_entry, /* OUT */
2659 vm_object_t *object, /* OUT */
2660 vm_pindex_t *pindex, /* OUT */
2661 vm_prot_t *out_prot, /* OUT */
2662 boolean_t *wired) /* OUT */
2664 vm_map_entry_t entry;
2665 vm_map_t map = *var_map;
2667 vm_prot_t fault_type = fault_typea;
2671 * Lookup the faulting address.
2674 vm_map_lock_read(map);
2675 #define RETURN(why) \
2677 vm_map_unlock_read(map); \
2682 * If the map has an interesting hint, try it before calling full
2683 * blown lookup routine.
2687 if (entry == NULL ||
2688 (vaddr < entry->start) || (vaddr >= entry->end)) {
2690 * Entry was either not a valid hint, or the vaddr was not
2691 * contained in the entry, so do a full lookup.
2693 if (!vm_map_lookup_entry(map, vaddr, out_entry))
2694 RETURN(KERN_INVALID_ADDRESS);
2702 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
2703 vm_map_t old_map = map;
2705 *var_map = map = entry->object.sub_map;
2706 vm_map_unlock_read(old_map);
2711 * Check whether this task is allowed to have this page.
2712 * Note the special case for MAP_ENTRY_COW
2713 * pages with an override. This is to implement a forced
2714 * COW for debuggers.
2716 if (fault_type & VM_PROT_OVERRIDE_WRITE)
2717 prot = entry->max_protection;
2719 prot = entry->protection;
2720 fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
2721 if ((fault_type & prot) != fault_type) {
2722 RETURN(KERN_PROTECTION_FAILURE);
2724 if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
2725 (entry->eflags & MAP_ENTRY_COW) &&
2726 (fault_type & VM_PROT_WRITE) &&
2727 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
2728 RETURN(KERN_PROTECTION_FAILURE);
2732 * If this page is not pageable, we have to get it for all possible
2735 *wired = (entry->wired_count != 0);
2737 prot = fault_type = entry->protection;
2740 * If the entry was copy-on-write, we either ...
2742 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
2744 * If we want to write the page, we may as well handle that
2745 * now since we've got the map locked.
2747 * If we don't need to write the page, we just demote the
2748 * permissions allowed.
2750 if (fault_type & VM_PROT_WRITE) {
2752 * Make a new object, and place it in the object
2753 * chain. Note that no new references have appeared
2754 * -- one just moved from the map to the new
2757 if (vm_map_lock_upgrade(map))
2761 &entry->object.vm_object,
2763 atop(entry->end - entry->start));
2764 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
2766 vm_map_lock_downgrade(map);
2769 * We're attempting to read a copy-on-write page --
2770 * don't allow writes.
2772 prot &= ~VM_PROT_WRITE;
2777 * Create an object if necessary.
2779 if (entry->object.vm_object == NULL &&
2781 if (vm_map_lock_upgrade(map))
2783 entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
2784 atop(entry->end - entry->start));
2786 vm_map_lock_downgrade(map);
2790 * Return the object/offset from this entry. If the entry was
2791 * copy-on-write or empty, it has been fixed up.
2793 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
2794 *object = entry->object.vm_object;
2797 * Return whether this is the only map sharing this data.
2800 return (KERN_SUCCESS);
2806 * vm_map_lookup_done:
2808 * Releases locks acquired by a vm_map_lookup
2809 * (according to the handle returned by that lookup).
2812 vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry)
2815 * Unlock the main-level map
2817 vm_map_unlock_read(map);
2820 #ifdef ENABLE_VFS_IOOPT
2822 * Experimental support for zero-copy I/O
2824 * Implement uiomove with VM operations. This handles (and collateral changes)
2825 * support every combination of source object modification, and COW type
2831 vm_object_t srcobject,
2838 vm_object_t first_object, oldobject, object;
2839 vm_map_entry_t entry;
2843 vm_offset_t uaddr, start, end, tend;
2844 vm_pindex_t first_pindex, oindex;
2860 if ((vm_map_lookup(&map, uaddr,
2861 VM_PROT_READ, &entry, &first_object,
2862 &first_pindex, &prot, &wired)) != KERN_SUCCESS) {
2866 vm_map_clip_start(map, entry, uaddr);
2869 tend = uaddr + tcnt;
2870 if (tend > entry->end) {
2871 tcnt = entry->end - uaddr;
2875 vm_map_clip_end(map, entry, tend);
2877 start = entry->start;
2882 oindex = OFF_TO_IDX(cp);
2885 for (idx = 0; idx < osize; idx++) {
2887 if ((m = vm_page_lookup(srcobject, oindex + idx)) == NULL) {
2888 vm_map_lookup_done(map, entry);
2892 * disallow busy or invalid pages, but allow
2893 * m->busy pages if they are entirely valid.
2895 if ((m->flags & PG_BUSY) ||
2896 ((m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL)) {
2897 vm_map_lookup_done(map, entry);
2904 * If we are changing an existing map entry, just redirect
2905 * the object, and change mappings.
2907 if ((first_object->type == OBJT_VNODE) &&
2908 ((oldobject = entry->object.vm_object) == first_object)) {
2910 if ((entry->offset != cp) || (oldobject != srcobject)) {
2912 * Remove old window into the file
2914 pmap_remove (map->pmap, uaddr, tend);
2917 * Force copy on write for mmaped regions
2919 vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
2922 * Point the object appropriately
2924 if (oldobject != srcobject) {
2927 * Set the object optimization hint flag
2929 vm_object_set_flag(srcobject, OBJ_OPT);
2930 vm_object_reference(srcobject);
2931 entry->object.vm_object = srcobject;
2934 vm_object_deallocate(oldobject);
2941 pmap_remove (map->pmap, uaddr, tend);
2944 } else if ((first_object->ref_count == 1) &&
2945 (first_object->size == osize) &&
2946 ((first_object->type == OBJT_DEFAULT) ||
2947 (first_object->type == OBJT_SWAP)) ) {
2949 oldobject = first_object->backing_object;
2951 if ((first_object->backing_object_offset != cp) ||
2952 (oldobject != srcobject)) {
2954 * Remove old window into the file
2956 pmap_remove (map->pmap, uaddr, tend);
2959 * Remove unneeded old pages
2961 vm_object_page_remove(first_object, 0, 0, 0);
2964 * Invalidate swap space
2966 if (first_object->type == OBJT_SWAP) {
2967 swap_pager_freespace(first_object,
2969 first_object->size);
2973 * Force copy on write for mmaped regions
2975 vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
2978 * Point the object appropriately
2980 if (oldobject != srcobject) {
2982 * Set the object optimization hint flag
2984 vm_object_set_flag(srcobject, OBJ_OPT);
2985 vm_object_reference(srcobject);
2988 TAILQ_REMOVE(&oldobject->shadow_head,
2989 first_object, shadow_list);
2990 oldobject->shadow_count--;
2991 /* XXX bump generation? */
2992 vm_object_deallocate(oldobject);
2995 TAILQ_INSERT_TAIL(&srcobject->shadow_head,
2996 first_object, shadow_list);
2997 srcobject->shadow_count++;
2998 /* XXX bump generation? */
3000 first_object->backing_object = srcobject;
3002 first_object->backing_object_offset = cp;
3005 pmap_remove (map->pmap, uaddr, tend);
3008 * Otherwise, we have to do a logical mmap.
3012 vm_object_set_flag(srcobject, OBJ_OPT);
3013 vm_object_reference(srcobject);
3015 pmap_remove (map->pmap, uaddr, tend);
3017 vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
3018 vm_map_lock_upgrade(map);
3020 if (entry == &map->header) {
3021 map->first_free = &map->header;
3022 } else if (map->first_free->start >= start) {
3023 map->first_free = entry->prev;
3026 vm_map_entry_delete(map, entry);
3031 rv = vm_map_insert(map, object, ooffset, start, tend,
3032 VM_PROT_ALL, VM_PROT_ALL, MAP_COPY_ON_WRITE);
3034 if (rv != KERN_SUCCESS)
3035 panic("vm_uiomove: could not insert new entry: %d", rv);
3039 * Map the window directly, if it is already in memory
3041 pmap_object_init_pt(map->pmap, uaddr,
3042 srcobject, oindex, tcnt, 0);
3057 #include "opt_ddb.h"
3059 #include <sys/kernel.h>
3061 #include <ddb/ddb.h>
3064 * vm_map_print: [ debug ]
3066 DB_SHOW_COMMAND(map, vm_map_print)
3069 /* XXX convert args. */
3070 vm_map_t map = (vm_map_t)addr;
3071 boolean_t full = have_addr;
3073 vm_map_entry_t entry;
3075 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
3077 (void *)map->pmap, map->nentries, map->timestamp);
3080 if (!full && db_indent)
3084 for (entry = map->header.next; entry != &map->header;
3085 entry = entry->next) {
3086 db_iprintf("map entry %p: start=%p, end=%p\n",
3087 (void *)entry, (void *)entry->start, (void *)entry->end);
3090 static char *inheritance_name[4] =
3091 {"share", "copy", "none", "donate_copy"};
3093 db_iprintf(" prot=%x/%x/%s",
3095 entry->max_protection,
3096 inheritance_name[(int)(unsigned char)entry->inheritance]);
3097 if (entry->wired_count != 0)
3098 db_printf(", wired");
3100 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3101 /* XXX no %qd in kernel. Truncate entry->offset. */
3102 db_printf(", share=%p, offset=0x%lx\n",
3103 (void *)entry->object.sub_map,
3104 (long)entry->offset);
3106 if ((entry->prev == &map->header) ||
3107 (entry->prev->object.sub_map !=
3108 entry->object.sub_map)) {
3110 vm_map_print((db_expr_t)(intptr_t)
3111 entry->object.sub_map,
3112 full, 0, (char *)0);
3116 /* XXX no %qd in kernel. Truncate entry->offset. */
3117 db_printf(", object=%p, offset=0x%lx",
3118 (void *)entry->object.vm_object,
3119 (long)entry->offset);
3120 if (entry->eflags & MAP_ENTRY_COW)
3121 db_printf(", copy (%s)",
3122 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
3126 if ((entry->prev == &map->header) ||
3127 (entry->prev->object.vm_object !=
3128 entry->object.vm_object)) {
3130 vm_object_print((db_expr_t)(intptr_t)
3131 entry->object.vm_object,
3132 full, 0, (char *)0);
3144 DB_SHOW_COMMAND(procvm, procvm)
3149 p = (struct proc *) addr;
3154 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
3155 (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
3156 (void *)vmspace_pmap(p->p_vmspace));
3158 vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);