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, 0);
163 uma_prealloc(kmapentzone, MAX_KMAPENT);
164 mapentzone = uma_zcreate("MAP ENTRY", sizeof(struct vm_map_entry),
165 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
166 uma_prealloc(mapentzone, MAX_MAPENT);
170 vmspace_zfini(void *mem, int size)
174 vm = (struct vmspace *)mem;
176 vm_map_zfini(&vm->vm_map, sizeof(vm->vm_map));
180 vmspace_zinit(void *mem, int size)
184 vm = (struct vmspace *)mem;
186 vm_map_zinit(&vm->vm_map, sizeof(vm->vm_map));
190 vm_map_zfini(void *mem, int size)
197 lockdestroy(&map->lock);
201 vm_map_zinit(void *mem, int size)
211 lockinit(&map->lock, PVM, "thrd_sleep", 0, LK_NOPAUSE);
216 vmspace_zdtor(void *mem, int size, void *arg)
220 vm = (struct vmspace *)mem;
222 vm_map_zdtor(&vm->vm_map, sizeof(vm->vm_map), arg);
225 vm_map_zdtor(void *mem, int size, void *arg)
230 KASSERT(map->nentries == 0,
231 ("map %p nentries == %d on free.",
232 map, map->nentries));
233 KASSERT(map->size == 0,
234 ("map %p size == %lu on free.",
235 map, (unsigned long)map->size));
236 KASSERT(map->infork == 0,
237 ("map %p infork == %d on free.",
240 #endif /* INVARIANTS */
243 * Allocate a vmspace structure, including a vm_map and pmap,
244 * and initialize those structures. The refcnt is set to 1.
245 * The remaining fields must be initialized by the caller.
248 vmspace_alloc(min, max)
249 vm_offset_t min, max;
254 vm = uma_zalloc(vmspace_zone, M_WAITOK);
255 CTR1(KTR_VM, "vmspace_alloc: %p", vm);
256 _vm_map_init(&vm->vm_map, min, max);
257 pmap_pinit(vmspace_pmap(vm));
258 vm->vm_map.pmap = vmspace_pmap(vm); /* XXX */
268 uma_zone_set_obj(kmapentzone, &kmapentobj, cnt.v_page_count / 4);
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_entry_behavior(struct vm_map_entry *entry)
362 return entry->eflags & MAP_ENTRY_BEHAV_MASK;
366 vm_map_entry_set_behavior(struct vm_map_entry *entry, u_char behavior)
368 entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) |
369 (behavior & MAP_ENTRY_BEHAV_MASK);
373 _vm_map_lock(vm_map_t map, const char *file, int line)
375 vm_map_printf("locking map LK_EXCLUSIVE: %p\n", map);
376 if (lockmgr(&map->lock, LK_EXCLUSIVE, NULL, curthread) != 0)
377 panic("vm_map_lock: failed to get lock");
382 _vm_map_unlock(vm_map_t map, const char *file, int line)
384 vm_map_printf("locking map LK_RELEASE: %p\n", map);
385 lockmgr(&(map)->lock, LK_RELEASE, NULL, curthread);
389 _vm_map_lock_read(vm_map_t map, const char *file, int line)
391 vm_map_printf("locking map LK_SHARED: %p\n", map);
392 lockmgr(&(map)->lock, LK_SHARED, NULL, curthread);
396 _vm_map_unlock_read(vm_map_t map, const char *file, int line)
398 vm_map_printf("locking map LK_RELEASE: %p\n", map);
399 lockmgr(&(map)->lock, LK_RELEASE, NULL, curthread);
403 _vm_map_trylock(vm_map_t map, const char *file, int line)
406 return (lockmgr(&map->lock, LK_EXCLUSIVE | LK_NOWAIT, NULL,
410 static __inline__ int
411 __vm_map_lock_upgrade(vm_map_t map, struct thread *td) {
414 vm_map_printf("locking map LK_EXCLUPGRADE: %p\n", map);
415 error = lockmgr(&map->lock, LK_EXCLUPGRADE, NULL, td);
422 _vm_map_lock_upgrade(vm_map_t map, const char *file, int line)
424 return (__vm_map_lock_upgrade(map, curthread));
428 _vm_map_lock_downgrade(vm_map_t map, const char *file, int line)
430 vm_map_printf("locking map LK_DOWNGRADE: %p\n", map);
431 lockmgr(&map->lock, LK_DOWNGRADE, NULL, curthread);
435 _vm_map_set_recursive(vm_map_t map, const char *file, int line)
437 mtx_lock((map)->lock.lk_interlock);
438 map->lock.lk_flags |= LK_CANRECURSE;
439 mtx_unlock((map)->lock.lk_interlock);
443 _vm_map_clear_recursive(vm_map_t map, const char *file, int line)
445 mtx_lock((map)->lock.lk_interlock);
446 map->lock.lk_flags &= ~LK_CANRECURSE;
447 mtx_unlock((map)->lock.lk_interlock);
451 vmspace_pmap(struct vmspace *vmspace)
453 return &vmspace->vm_pmap;
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)
476 result = uma_zalloc(mapzone, M_WAITOK);
477 CTR1(KTR_VM, "vm_map_create: %p", result);
478 _vm_map_init(result, min, max);
484 * Initialize an existing vm_map structure
485 * such as that in the vmspace structure.
486 * The pmap is set elsewhere.
489 _vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max)
492 map->header.next = map->header.prev = &map->header;
494 map->min_offset = min;
495 map->max_offset = max;
496 map->first_free = &map->header;
497 map->hint = &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 || !mapentzone)
517 ? kmapentzone : mapentzone, entry);
521 * vm_map_entry_create: [ internal use only ]
523 * Allocates a VM map entry for insertion.
524 * No entry fields are filled in.
526 static vm_map_entry_t
527 vm_map_entry_create(vm_map_t map)
529 vm_map_entry_t new_entry;
531 new_entry = uma_zalloc((map->system_map || !mapentzone) ?
532 kmapentzone : mapentzone, M_WAITOK);
533 if (new_entry == NULL)
534 panic("vm_map_entry_create: kernel resources exhausted");
539 * vm_map_entry_{un,}link:
541 * Insert/remove entries from maps.
544 vm_map_entry_link(vm_map_t map,
545 vm_map_entry_t after_where,
546 vm_map_entry_t entry)
550 "vm_map_entry_link: map %p, nentries %d, entry %p, after %p", map,
551 map->nentries, entry, after_where);
553 entry->prev = after_where;
554 entry->next = after_where->next;
555 entry->next->prev = entry;
556 after_where->next = entry;
560 vm_map_entry_unlink(vm_map_t map,
561 vm_map_entry_t entry)
563 vm_map_entry_t prev = entry->prev;
564 vm_map_entry_t next = entry->next;
569 CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map,
570 map->nentries, entry);
576 * Saves the specified entry as the hint for
579 #define SAVE_HINT(map,value) \
580 (map)->hint = (value);
583 * vm_map_lookup_entry: [ internal use only ]
585 * Finds the map entry containing (or
586 * immediately preceding) the specified address
587 * in the given map; the entry is returned
588 * in the "entry" parameter. The boolean
589 * result indicates whether the address is
590 * actually contained in the map.
596 vm_map_entry_t *entry) /* OUT */
603 * Start looking either from the head of the list, or from the hint.
607 if (cur == &map->header)
610 if (address >= cur->start) {
612 * Go from hint to end of list.
614 * But first, make a quick check to see if we are already looking
615 * at the entry we want (which is usually the case). Note also
616 * that we don't need to save the hint here... it is the same
617 * hint (unless we are at the header, in which case the hint
618 * didn't buy us anything anyway).
621 if ((cur != last) && (cur->end > address)) {
627 * Go from start to hint, *inclusively*
630 cur = map->header.next;
636 while (cur != last) {
637 if (cur->end > address) {
638 if (address >= cur->start) {
640 * Save this lookup for future hints, and
652 SAVE_HINT(map, *entry);
659 * Inserts the given whole VM object into the target
660 * map at the specified address range. The object's
661 * size should match that of the address range.
663 * Requires that the map be locked, and leaves it so.
665 * If object is non-NULL, ref count must be bumped by caller
666 * prior to making call to account for the new entry.
669 vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
670 vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max,
673 vm_map_entry_t new_entry;
674 vm_map_entry_t prev_entry;
675 vm_map_entry_t temp_entry;
676 vm_eflags_t protoeflags;
681 * Check that the start and end points are not bogus.
683 if ((start < map->min_offset) || (end > map->max_offset) ||
685 return (KERN_INVALID_ADDRESS);
688 * Find the entry prior to the proposed starting address; if it's part
689 * of an existing entry, this range is bogus.
691 if (vm_map_lookup_entry(map, start, &temp_entry))
692 return (KERN_NO_SPACE);
694 prev_entry = temp_entry;
697 * Assert that the next entry doesn't overlap the end point.
699 if ((prev_entry->next != &map->header) &&
700 (prev_entry->next->start < end))
701 return (KERN_NO_SPACE);
705 if (cow & MAP_COPY_ON_WRITE)
706 protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
708 if (cow & MAP_NOFAULT) {
709 protoeflags |= MAP_ENTRY_NOFAULT;
711 KASSERT(object == NULL,
712 ("vm_map_insert: paradoxical MAP_NOFAULT request"));
714 if (cow & MAP_DISABLE_SYNCER)
715 protoeflags |= MAP_ENTRY_NOSYNC;
716 if (cow & MAP_DISABLE_COREDUMP)
717 protoeflags |= MAP_ENTRY_NOCOREDUMP;
721 * When object is non-NULL, it could be shared with another
722 * process. We have to set or clear OBJ_ONEMAPPING
725 if ((object->ref_count > 1) || (object->shadow_count != 0)) {
726 vm_object_clear_flag(object, OBJ_ONEMAPPING);
729 else if ((prev_entry != &map->header) &&
730 (prev_entry->eflags == protoeflags) &&
731 (prev_entry->end == start) &&
732 (prev_entry->wired_count == 0) &&
733 ((prev_entry->object.vm_object == NULL) ||
734 vm_object_coalesce(prev_entry->object.vm_object,
735 OFF_TO_IDX(prev_entry->offset),
736 (vm_size_t)(prev_entry->end - prev_entry->start),
737 (vm_size_t)(end - prev_entry->end)))) {
739 * We were able to extend the object. Determine if we
740 * can extend the previous map entry to include the
743 if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
744 (prev_entry->protection == prot) &&
745 (prev_entry->max_protection == max)) {
746 map->size += (end - prev_entry->end);
747 prev_entry->end = end;
748 vm_map_simplify_entry(map, prev_entry);
749 return (KERN_SUCCESS);
753 * If we can extend the object but cannot extend the
754 * map entry, we have to create a new map entry. We
755 * must bump the ref count on the extended object to
756 * account for it. object may be NULL.
758 object = prev_entry->object.vm_object;
759 offset = prev_entry->offset +
760 (prev_entry->end - prev_entry->start);
761 vm_object_reference(object);
765 * NOTE: if conditionals fail, object can be NULL here. This occurs
766 * in things like the buffer map where we manage kva but do not manage
773 new_entry = vm_map_entry_create(map);
774 new_entry->start = start;
775 new_entry->end = end;
777 new_entry->eflags = protoeflags;
778 new_entry->object.vm_object = object;
779 new_entry->offset = offset;
780 new_entry->avail_ssize = 0;
782 new_entry->inheritance = VM_INHERIT_DEFAULT;
783 new_entry->protection = prot;
784 new_entry->max_protection = max;
785 new_entry->wired_count = 0;
788 * Insert the new entry into the list
790 vm_map_entry_link(map, prev_entry, new_entry);
791 map->size += new_entry->end - new_entry->start;
794 * Update the free space hint
796 if ((map->first_free == prev_entry) &&
797 (prev_entry->end >= new_entry->start)) {
798 map->first_free = new_entry;
803 * Temporarily removed to avoid MAP_STACK panic, due to
804 * MAP_STACK being a huge hack. Will be added back in
805 * when MAP_STACK (and the user stack mapping) is fixed.
808 * It may be possible to simplify the entry
810 vm_map_simplify_entry(map, new_entry);
813 if (cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) {
814 pmap_object_init_pt(map->pmap, start,
815 object, OFF_TO_IDX(offset), end - start,
816 cow & MAP_PREFAULT_PARTIAL);
819 return (KERN_SUCCESS);
823 * Find sufficient space for `length' bytes in the given map, starting at
824 * `start'. The map must be locked. Returns 0 on success, 1 on no space.
833 vm_map_entry_t entry, next;
837 if (start < map->min_offset)
838 start = map->min_offset;
839 if (start > map->max_offset)
843 * Look for the first possible address; if there's already something
844 * at this address, we have to start after it.
846 if (start == map->min_offset) {
847 if ((entry = map->first_free) != &map->header)
852 if (vm_map_lookup_entry(map, start, &tmp))
858 * Look through the rest of the map, trying to fit a new region in the
859 * gap between existing regions, or after the very last region.
861 for (;; start = (entry = next)->end) {
863 * Find the end of the proposed new region. Be sure we didn't
864 * go beyond the end of the map, or wrap around the address;
865 * if so, we lose. Otherwise, if this is the last entry, or
866 * if the proposed new region fits before the next entry, we
869 end = start + length;
870 if (end > map->max_offset || end < start)
873 if (next == &map->header || next->start >= end)
876 SAVE_HINT(map, entry);
878 if (map == kernel_map) {
880 if ((ksize = round_page(start + length)) > kernel_vm_end) {
881 pmap_growkernel(ksize);
888 * vm_map_find finds an unallocated region in the target address
889 * map with the given length. The search is defined to be
890 * first-fit from the specified address; the region found is
891 * returned in the same parameter.
893 * If object is non-NULL, ref count must be bumped by caller
894 * prior to making call to account for the new entry.
897 vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
898 vm_offset_t *addr, /* IN/OUT */
899 vm_size_t length, boolean_t find_space, vm_prot_t prot,
900 vm_prot_t max, int cow)
914 if (vm_map_findspace(map, start, length, addr)) {
918 return (KERN_NO_SPACE);
922 result = vm_map_insert(map, object, offset,
923 start, start + length, prot, max, cow);
933 * vm_map_simplify_entry:
935 * Simplify the given map entry by merging with either neighbor. This
936 * routine also has the ability to merge with both neighbors.
938 * The map must be locked.
940 * This routine guarentees that the passed entry remains valid (though
941 * possibly extended). When merging, this routine may delete one or
945 vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry)
947 vm_map_entry_t next, prev;
948 vm_size_t prevsize, esize;
952 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
956 if (prev != &map->header) {
957 prevsize = prev->end - prev->start;
958 if ( (prev->end == entry->start) &&
959 (prev->object.vm_object == entry->object.vm_object) &&
960 (!prev->object.vm_object ||
961 (prev->offset + prevsize == entry->offset)) &&
962 (prev->eflags == entry->eflags) &&
963 (prev->protection == entry->protection) &&
964 (prev->max_protection == entry->max_protection) &&
965 (prev->inheritance == entry->inheritance) &&
966 (prev->wired_count == entry->wired_count)) {
967 if (map->first_free == prev)
968 map->first_free = entry;
969 if (map->hint == prev)
971 vm_map_entry_unlink(map, prev);
972 entry->start = prev->start;
973 entry->offset = prev->offset;
974 if (prev->object.vm_object)
975 vm_object_deallocate(prev->object.vm_object);
976 vm_map_entry_dispose(map, prev);
981 if (next != &map->header) {
982 esize = entry->end - entry->start;
983 if ((entry->end == next->start) &&
984 (next->object.vm_object == entry->object.vm_object) &&
985 (!entry->object.vm_object ||
986 (entry->offset + esize == next->offset)) &&
987 (next->eflags == entry->eflags) &&
988 (next->protection == entry->protection) &&
989 (next->max_protection == entry->max_protection) &&
990 (next->inheritance == entry->inheritance) &&
991 (next->wired_count == entry->wired_count)) {
992 if (map->first_free == next)
993 map->first_free = entry;
994 if (map->hint == next)
996 vm_map_entry_unlink(map, next);
997 entry->end = next->end;
998 if (next->object.vm_object)
999 vm_object_deallocate(next->object.vm_object);
1000 vm_map_entry_dispose(map, next);
1005 * vm_map_clip_start: [ internal use only ]
1007 * Asserts that the given entry begins at or after
1008 * the specified address; if necessary,
1009 * it splits the entry into two.
1011 #define vm_map_clip_start(map, entry, startaddr) \
1013 if (startaddr > entry->start) \
1014 _vm_map_clip_start(map, entry, startaddr); \
1018 * This routine is called only when it is known that
1019 * the entry must be split.
1022 _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start)
1024 vm_map_entry_t new_entry;
1027 * Split off the front portion -- note that we must insert the new
1028 * entry BEFORE this one, so that this entry has the specified
1031 vm_map_simplify_entry(map, entry);
1034 * If there is no object backing this entry, we might as well create
1035 * one now. If we defer it, an object can get created after the map
1036 * is clipped, and individual objects will be created for the split-up
1037 * map. This is a bit of a hack, but is also about the best place to
1038 * put this improvement.
1040 if (entry->object.vm_object == NULL && !map->system_map) {
1042 object = vm_object_allocate(OBJT_DEFAULT,
1043 atop(entry->end - entry->start));
1044 entry->object.vm_object = object;
1048 new_entry = vm_map_entry_create(map);
1049 *new_entry = *entry;
1051 new_entry->end = start;
1052 entry->offset += (start - entry->start);
1053 entry->start = start;
1055 vm_map_entry_link(map, entry->prev, new_entry);
1057 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1058 vm_object_reference(new_entry->object.vm_object);
1063 * vm_map_clip_end: [ internal use only ]
1065 * Asserts that the given entry ends at or before
1066 * the specified address; if necessary,
1067 * it splits the entry into two.
1069 #define vm_map_clip_end(map, entry, endaddr) \
1071 if (endaddr < entry->end) \
1072 _vm_map_clip_end(map, entry, endaddr); \
1076 * This routine is called only when it is known that
1077 * the entry must be split.
1080 _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end)
1082 vm_map_entry_t new_entry;
1085 * If there is no object backing this entry, we might as well create
1086 * one now. If we defer it, an object can get created after the map
1087 * is clipped, and individual objects will be created for the split-up
1088 * map. This is a bit of a hack, but is also about the best place to
1089 * put this improvement.
1091 if (entry->object.vm_object == NULL && !map->system_map) {
1093 object = vm_object_allocate(OBJT_DEFAULT,
1094 atop(entry->end - entry->start));
1095 entry->object.vm_object = object;
1100 * Create a new entry and insert it AFTER the specified entry
1102 new_entry = vm_map_entry_create(map);
1103 *new_entry = *entry;
1105 new_entry->start = entry->end = end;
1106 new_entry->offset += (end - entry->start);
1108 vm_map_entry_link(map, entry, new_entry);
1110 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1111 vm_object_reference(new_entry->object.vm_object);
1116 * VM_MAP_RANGE_CHECK: [ internal use only ]
1118 * Asserts that the starting and ending region
1119 * addresses fall within the valid range of the map.
1121 #define VM_MAP_RANGE_CHECK(map, start, end) \
1123 if (start < vm_map_min(map)) \
1124 start = vm_map_min(map); \
1125 if (end > vm_map_max(map)) \
1126 end = vm_map_max(map); \
1132 * vm_map_submap: [ kernel use only ]
1134 * Mark the given range as handled by a subordinate map.
1136 * This range must have been created with vm_map_find,
1137 * and no other operations may have been performed on this
1138 * range prior to calling vm_map_submap.
1140 * Only a limited number of operations can be performed
1141 * within this rage after calling vm_map_submap:
1143 * [Don't try vm_map_copy!]
1145 * To remove a submapping, one must first remove the
1146 * range from the superior map, and then destroy the
1147 * submap (if desired). [Better yet, don't try it.]
1156 vm_map_entry_t entry;
1157 int result = KERN_INVALID_ARGUMENT;
1163 VM_MAP_RANGE_CHECK(map, start, end);
1165 if (vm_map_lookup_entry(map, start, &entry)) {
1166 vm_map_clip_start(map, entry, start);
1168 entry = entry->next;
1170 vm_map_clip_end(map, entry, end);
1172 if ((entry->start == start) && (entry->end == end) &&
1173 ((entry->eflags & MAP_ENTRY_COW) == 0) &&
1174 (entry->object.vm_object == NULL)) {
1175 entry->object.sub_map = submap;
1176 entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
1177 result = KERN_SUCCESS;
1187 * Sets the protection of the specified address
1188 * region in the target map. If "set_max" is
1189 * specified, the maximum protection is to be set;
1190 * otherwise, only the current protection is affected.
1193 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
1194 vm_prot_t new_prot, boolean_t set_max)
1196 vm_map_entry_t current;
1197 vm_map_entry_t entry;
1202 VM_MAP_RANGE_CHECK(map, start, end);
1204 if (vm_map_lookup_entry(map, start, &entry)) {
1205 vm_map_clip_start(map, entry, start);
1207 entry = entry->next;
1211 * Make a first pass to check for protection violations.
1214 while ((current != &map->header) && (current->start < end)) {
1215 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1217 return (KERN_INVALID_ARGUMENT);
1219 if ((new_prot & current->max_protection) != new_prot) {
1221 return (KERN_PROTECTION_FAILURE);
1223 current = current->next;
1227 * Go back and fix up protections. [Note that clipping is not
1228 * necessary the second time.]
1231 while ((current != &map->header) && (current->start < end)) {
1234 vm_map_clip_end(map, current, end);
1236 old_prot = current->protection;
1238 current->protection =
1239 (current->max_protection = new_prot) &
1242 current->protection = new_prot;
1245 * Update physical map if necessary. Worry about copy-on-write
1246 * here -- CHECK THIS XXX
1248 if (current->protection != old_prot) {
1249 #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1251 pmap_protect(map->pmap, current->start,
1253 current->protection & MASK(current));
1256 vm_map_simplify_entry(map, current);
1257 current = current->next;
1260 return (KERN_SUCCESS);
1266 * This routine traverses a processes map handling the madvise
1267 * system call. Advisories are classified as either those effecting
1268 * the vm_map_entry structure, or those effecting the underlying
1278 vm_map_entry_t current, entry;
1284 * Some madvise calls directly modify the vm_map_entry, in which case
1285 * we need to use an exclusive lock on the map and we need to perform
1286 * various clipping operations. Otherwise we only need a read-lock
1291 case MADV_SEQUENTIAL:
1303 vm_map_lock_read(map);
1306 return (KERN_INVALID_ARGUMENT);
1310 * Locate starting entry and clip if necessary.
1312 VM_MAP_RANGE_CHECK(map, start, end);
1314 if (vm_map_lookup_entry(map, start, &entry)) {
1316 vm_map_clip_start(map, entry, start);
1318 entry = entry->next;
1323 * madvise behaviors that are implemented in the vm_map_entry.
1325 * We clip the vm_map_entry so that behavioral changes are
1326 * limited to the specified address range.
1328 for (current = entry;
1329 (current != &map->header) && (current->start < end);
1330 current = current->next
1332 if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1335 vm_map_clip_end(map, current, end);
1339 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
1341 case MADV_SEQUENTIAL:
1342 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
1345 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
1348 current->eflags |= MAP_ENTRY_NOSYNC;
1351 current->eflags &= ~MAP_ENTRY_NOSYNC;
1354 current->eflags |= MAP_ENTRY_NOCOREDUMP;
1357 current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
1362 vm_map_simplify_entry(map, current);
1370 * madvise behaviors that are implemented in the underlying
1373 * Since we don't clip the vm_map_entry, we have to clip
1374 * the vm_object pindex and count.
1376 for (current = entry;
1377 (current != &map->header) && (current->start < end);
1378 current = current->next
1380 vm_offset_t useStart;
1382 if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1385 pindex = OFF_TO_IDX(current->offset);
1386 count = atop(current->end - current->start);
1387 useStart = current->start;
1389 if (current->start < start) {
1390 pindex += atop(start - current->start);
1391 count -= atop(start - current->start);
1394 if (current->end > end)
1395 count -= atop(current->end - end);
1400 vm_object_madvise(current->object.vm_object,
1401 pindex, count, behav);
1402 if (behav == MADV_WILLNEED) {
1403 pmap_object_init_pt(
1406 current->object.vm_object,
1408 (count << PAGE_SHIFT),
1409 MAP_PREFAULT_MADVISE
1413 vm_map_unlock_read(map);
1422 * Sets the inheritance of the specified address
1423 * range in the target map. Inheritance
1424 * affects how the map will be shared with
1425 * child maps at the time of vm_map_fork.
1428 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
1429 vm_inherit_t new_inheritance)
1431 vm_map_entry_t entry;
1432 vm_map_entry_t temp_entry;
1436 switch (new_inheritance) {
1437 case VM_INHERIT_NONE:
1438 case VM_INHERIT_COPY:
1439 case VM_INHERIT_SHARE:
1442 return (KERN_INVALID_ARGUMENT);
1447 VM_MAP_RANGE_CHECK(map, start, end);
1449 if (vm_map_lookup_entry(map, start, &temp_entry)) {
1451 vm_map_clip_start(map, entry, start);
1453 entry = temp_entry->next;
1455 while ((entry != &map->header) && (entry->start < end)) {
1456 vm_map_clip_end(map, entry, end);
1458 entry->inheritance = new_inheritance;
1460 vm_map_simplify_entry(map, entry);
1462 entry = entry->next;
1466 return (KERN_SUCCESS);
1470 * Implement the semantics of mlock
1473 vm_map_user_pageable(
1477 boolean_t new_pageable)
1479 vm_map_entry_t entry;
1480 vm_map_entry_t start_entry;
1486 VM_MAP_RANGE_CHECK(map, start, end);
1488 if (vm_map_lookup_entry(map, start, &start_entry) == FALSE) {
1490 return (KERN_INVALID_ADDRESS);
1495 entry = start_entry;
1496 vm_map_clip_start(map, entry, start);
1499 * Now decrement the wiring count for each region. If a region
1500 * becomes completely unwired, unwire its physical pages and
1503 while ((entry != &map->header) && (entry->start < end)) {
1504 if (entry->eflags & MAP_ENTRY_USER_WIRED) {
1505 vm_map_clip_end(map, entry, end);
1506 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
1507 entry->wired_count--;
1508 if (entry->wired_count == 0)
1509 vm_fault_unwire(map, entry->start, entry->end);
1511 vm_map_simplify_entry(map,entry);
1512 entry = entry->next;
1516 entry = start_entry;
1518 while ((entry != &map->header) && (entry->start < end)) {
1520 if (entry->eflags & MAP_ENTRY_USER_WIRED) {
1521 entry = entry->next;
1525 if (entry->wired_count != 0) {
1526 entry->wired_count++;
1527 entry->eflags |= MAP_ENTRY_USER_WIRED;
1528 entry = entry->next;
1532 /* Here on entry being newly wired */
1534 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1535 int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY;
1536 if (copyflag && ((entry->protection & VM_PROT_WRITE) != 0)) {
1538 vm_object_shadow(&entry->object.vm_object,
1540 atop(entry->end - entry->start));
1541 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
1543 } else if (entry->object.vm_object == NULL &&
1546 entry->object.vm_object =
1547 vm_object_allocate(OBJT_DEFAULT,
1548 atop(entry->end - entry->start));
1549 entry->offset = (vm_offset_t) 0;
1554 vm_map_clip_start(map, entry, start);
1555 vm_map_clip_end(map, entry, end);
1557 entry->wired_count++;
1558 entry->eflags |= MAP_ENTRY_USER_WIRED;
1559 estart = entry->start;
1562 /* First we need to allow map modifications */
1563 vm_map_set_recursive(map);
1564 vm_map_lock_downgrade(map);
1567 rv = vm_fault_user_wire(map, entry->start, entry->end);
1570 entry->wired_count--;
1571 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
1573 vm_map_clear_recursive(map);
1577 * At this point, the map is unlocked, and
1578 * entry might no longer be valid. Use copy
1579 * of entry start value obtained while entry
1582 (void) vm_map_user_pageable(map, start, estart,
1587 vm_map_clear_recursive(map);
1588 if (vm_map_lock_upgrade(map)) {
1590 if (vm_map_lookup_entry(map, estart, &entry)
1594 * vm_fault_user_wire succeded, thus
1595 * the area between start and eend
1596 * is wired and has to be unwired
1597 * here as part of the cleanup.
1599 (void) vm_map_user_pageable(map,
1603 return (KERN_INVALID_ADDRESS);
1606 vm_map_simplify_entry(map,entry);
1611 return KERN_SUCCESS;
1617 * Sets the pageability of the specified address
1618 * range in the target map. Regions specified
1619 * as not pageable require locked-down physical
1620 * memory and physical page maps.
1622 * The map must not be locked, but a reference
1623 * must remain to the map throughout the call.
1630 boolean_t new_pageable)
1632 vm_map_entry_t entry;
1633 vm_map_entry_t start_entry;
1634 vm_offset_t failed = 0;
1641 VM_MAP_RANGE_CHECK(map, start, end);
1644 * Only one pageability change may take place at one time, since
1645 * vm_fault assumes it will be called only once for each
1646 * wiring/unwiring. Therefore, we have to make sure we're actually
1647 * changing the pageability for the entire region. We do so before
1648 * making any changes.
1650 if (vm_map_lookup_entry(map, start, &start_entry) == FALSE) {
1652 return (KERN_INVALID_ADDRESS);
1654 entry = start_entry;
1657 * Actions are rather different for wiring and unwiring, so we have
1658 * two separate cases.
1661 vm_map_clip_start(map, entry, start);
1664 * Unwiring. First ensure that the range to be unwired is
1665 * really wired down and that there are no holes.
1667 while ((entry != &map->header) && (entry->start < end)) {
1668 if (entry->wired_count == 0 ||
1669 (entry->end < end &&
1670 (entry->next == &map->header ||
1671 entry->next->start > entry->end))) {
1673 return (KERN_INVALID_ARGUMENT);
1675 entry = entry->next;
1679 * Now decrement the wiring count for each region. If a region
1680 * becomes completely unwired, unwire its physical pages and
1683 entry = start_entry;
1684 while ((entry != &map->header) && (entry->start < end)) {
1685 vm_map_clip_end(map, entry, end);
1687 entry->wired_count--;
1688 if (entry->wired_count == 0)
1689 vm_fault_unwire(map, entry->start, entry->end);
1691 vm_map_simplify_entry(map, entry);
1693 entry = entry->next;
1697 * Wiring. We must do this in two passes:
1699 * 1. Holding the write lock, we create any shadow or zero-fill
1700 * objects that need to be created. Then we clip each map
1701 * entry to the region to be wired and increment its wiring
1702 * count. We create objects before clipping the map entries
1703 * to avoid object proliferation.
1705 * 2. We downgrade to a read lock, and call vm_fault_wire to
1706 * fault in the pages for any newly wired area (wired_count is
1709 * Downgrading to a read lock for vm_fault_wire avoids a possible
1710 * deadlock with another process that may have faulted on one
1711 * of the pages to be wired (it would mark the page busy,
1712 * blocking us, then in turn block on the map lock that we
1713 * hold). Because of problems in the recursive lock package,
1714 * we cannot upgrade to a write lock in vm_map_lookup. Thus,
1715 * any actions that require the write lock must be done
1716 * beforehand. Because we keep the read lock on the map, the
1717 * copy-on-write status of the entries we modify here cannot
1724 while ((entry != &map->header) && (entry->start < end)) {
1725 if (entry->wired_count == 0) {
1728 * Perform actions of vm_map_lookup that need
1729 * the write lock on the map: create a shadow
1730 * object for a copy-on-write region, or an
1731 * object for a zero-fill region.
1733 * We don't have to do this for entries that
1734 * point to sub maps, because we won't
1735 * hold the lock on the sub map.
1737 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1738 int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY;
1740 ((entry->protection & VM_PROT_WRITE) != 0)) {
1742 vm_object_shadow(&entry->object.vm_object,
1744 atop(entry->end - entry->start));
1745 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
1746 } else if (entry->object.vm_object == NULL &&
1748 entry->object.vm_object =
1749 vm_object_allocate(OBJT_DEFAULT,
1750 atop(entry->end - entry->start));
1751 entry->offset = (vm_offset_t) 0;
1755 vm_map_clip_start(map, entry, start);
1756 vm_map_clip_end(map, entry, end);
1757 entry->wired_count++;
1762 if (entry->end < end &&
1763 (entry->next == &map->header ||
1764 entry->next->start > entry->end)) {
1766 * Found one. Object creation actions do not
1767 * need to be undone, but the wired counts
1768 * need to be restored.
1770 while (entry != &map->header && entry->end > start) {
1771 entry->wired_count--;
1772 entry = entry->prev;
1775 return (KERN_INVALID_ARGUMENT);
1777 entry = entry->next;
1785 * HACK HACK HACK HACK
1787 * If we are wiring in the kernel map or a submap of it,
1788 * unlock the map to avoid deadlocks. We trust that the
1789 * kernel is well-behaved, and therefore will not do
1790 * anything destructive to this region of the map while
1791 * we have it unlocked. We cannot trust user processes
1794 * HACK HACK HACK HACK
1796 if (vm_map_pmap(map) == kernel_pmap) {
1797 vm_map_unlock(map); /* trust me ... */
1799 vm_map_lock_downgrade(map);
1803 entry = start_entry;
1804 while (entry != &map->header && entry->start < end) {
1806 * If vm_fault_wire fails for any page we need to undo
1807 * what has been done. We decrement the wiring count
1808 * for those pages which have not yet been wired (now)
1809 * and unwire those that have (later).
1811 * XXX this violates the locking protocol on the map,
1812 * needs to be fixed.
1815 entry->wired_count--;
1816 else if (entry->wired_count == 1) {
1817 rv = vm_fault_wire(map, entry->start, entry->end);
1819 failed = entry->start;
1820 entry->wired_count--;
1823 entry = entry->next;
1826 if (vm_map_pmap(map) == kernel_pmap) {
1831 (void) vm_map_pageable(map, start, failed, TRUE);
1835 * An exclusive lock on the map is needed in order to call
1836 * vm_map_simplify_entry(). If the current lock on the map
1837 * is only a shared lock, an upgrade is needed.
1839 if (vm_map_pmap(map) != kernel_pmap &&
1840 vm_map_lock_upgrade(map)) {
1842 if (vm_map_lookup_entry(map, start, &start_entry) ==
1845 return KERN_SUCCESS;
1848 vm_map_simplify_entry(map, start_entry);
1853 return (KERN_SUCCESS);
1859 * Push any dirty cached pages in the address range to their pager.
1860 * If syncio is TRUE, dirty pages are written synchronously.
1861 * If invalidate is TRUE, any cached pages are freed as well.
1863 * Returns an error if any part of the specified range is not mapped.
1871 boolean_t invalidate)
1873 vm_map_entry_t current;
1874 vm_map_entry_t entry;
1877 vm_ooffset_t offset;
1881 vm_map_lock_read(map);
1882 VM_MAP_RANGE_CHECK(map, start, end);
1883 if (!vm_map_lookup_entry(map, start, &entry)) {
1884 vm_map_unlock_read(map);
1885 return (KERN_INVALID_ADDRESS);
1888 * Make a first pass to check for holes.
1890 for (current = entry; current->start < end; current = current->next) {
1891 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1892 vm_map_unlock_read(map);
1893 return (KERN_INVALID_ARGUMENT);
1895 if (end > current->end &&
1896 (current->next == &map->header ||
1897 current->end != current->next->start)) {
1898 vm_map_unlock_read(map);
1899 return (KERN_INVALID_ADDRESS);
1904 pmap_remove(vm_map_pmap(map), start, end);
1906 * Make a second pass, cleaning/uncaching pages from the indicated
1909 for (current = entry; current->start < end; current = current->next) {
1910 offset = current->offset + (start - current->start);
1911 size = (end <= current->end ? end : current->end) - start;
1912 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1914 vm_map_entry_t tentry;
1917 smap = current->object.sub_map;
1918 vm_map_lock_read(smap);
1919 (void) vm_map_lookup_entry(smap, offset, &tentry);
1920 tsize = tentry->end - offset;
1923 object = tentry->object.vm_object;
1924 offset = tentry->offset + (offset - tentry->start);
1925 vm_map_unlock_read(smap);
1927 object = current->object.vm_object;
1930 * Note that there is absolutely no sense in writing out
1931 * anonymous objects, so we track down the vnode object
1933 * We invalidate (remove) all pages from the address space
1934 * anyway, for semantic correctness.
1936 * note: certain anonymous maps, such as MAP_NOSYNC maps,
1937 * may start out with a NULL object.
1939 while (object && object->backing_object) {
1940 object = object->backing_object;
1941 offset += object->backing_object_offset;
1942 if (object->size < OFF_TO_IDX(offset + size))
1943 size = IDX_TO_OFF(object->size) - offset;
1945 if (object && (object->type == OBJT_VNODE) &&
1946 (current->protection & VM_PROT_WRITE)) {
1948 * Flush pages if writing is allowed, invalidate them
1949 * if invalidation requested. Pages undergoing I/O
1950 * will be ignored by vm_object_page_remove().
1952 * We cannot lock the vnode and then wait for paging
1953 * to complete without deadlocking against vm_fault.
1954 * Instead we simply call vm_object_page_remove() and
1955 * allow it to block internally on a page-by-page
1956 * basis when it encounters pages undergoing async
1961 vm_object_reference(object);
1962 vn_lock(object->handle, LK_EXCLUSIVE | LK_RETRY, curthread);
1963 flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
1964 flags |= invalidate ? OBJPC_INVAL : 0;
1965 vm_object_page_clean(object,
1967 OFF_TO_IDX(offset + size + PAGE_MASK),
1970 /*vm_object_pip_wait(object, "objmcl");*/
1971 vm_object_page_remove(object,
1973 OFF_TO_IDX(offset + size + PAGE_MASK),
1976 VOP_UNLOCK(object->handle, 0, curthread);
1977 vm_object_deallocate(object);
1982 vm_map_unlock_read(map);
1983 return (KERN_SUCCESS);
1987 * vm_map_entry_unwire: [ internal use only ]
1989 * Make the region specified by this entry pageable.
1991 * The map in question should be locked.
1992 * [This is the reason for this routine's existence.]
1995 vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
1997 vm_fault_unwire(map, entry->start, entry->end);
1998 entry->wired_count = 0;
2002 * vm_map_entry_delete: [ internal use only ]
2004 * Deallocate the given entry from the target map.
2007 vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry)
2009 vm_map_entry_unlink(map, entry);
2010 map->size -= entry->end - entry->start;
2012 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
2013 vm_object_deallocate(entry->object.vm_object);
2016 vm_map_entry_dispose(map, entry);
2020 * vm_map_delete: [ internal use only ]
2022 * Deallocates the given address range from the target
2026 vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end)
2029 vm_map_entry_t entry;
2030 vm_map_entry_t first_entry;
2035 * Find the start of the region, and clip it
2037 if (!vm_map_lookup_entry(map, start, &first_entry))
2038 entry = first_entry->next;
2040 entry = first_entry;
2041 vm_map_clip_start(map, entry, start);
2043 * Fix the lookup hint now, rather than each time though the
2046 SAVE_HINT(map, entry->prev);
2050 * Save the free space hint
2052 if (entry == &map->header) {
2053 map->first_free = &map->header;
2054 } else if (map->first_free->start >= start) {
2055 map->first_free = entry->prev;
2059 * Step through all entries in this region
2061 while ((entry != &map->header) && (entry->start < end)) {
2062 vm_map_entry_t next;
2064 vm_pindex_t offidxstart, offidxend, count;
2066 vm_map_clip_end(map, entry, end);
2072 offidxstart = OFF_TO_IDX(entry->offset);
2073 count = OFF_TO_IDX(e - s);
2074 object = entry->object.vm_object;
2077 * Unwire before removing addresses from the pmap; otherwise,
2078 * unwiring will put the entries back in the pmap.
2080 if (entry->wired_count != 0) {
2081 vm_map_entry_unwire(map, entry);
2084 offidxend = offidxstart + count;
2086 if ((object == kernel_object) || (object == kmem_object)) {
2087 vm_object_page_remove(object, offidxstart, offidxend, FALSE);
2089 pmap_remove(map->pmap, s, e);
2090 if (object != NULL &&
2091 object->ref_count != 1 &&
2092 (object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING &&
2093 (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2094 vm_object_collapse(object);
2095 vm_object_page_remove(object, offidxstart, offidxend, FALSE);
2096 if (object->type == OBJT_SWAP) {
2097 swap_pager_freespace(object, offidxstart, count);
2099 if (offidxend >= object->size &&
2100 offidxstart < object->size) {
2101 object->size = offidxstart;
2107 * Delete the entry (which may delete the object) only after
2108 * removing all pmap entries pointing to its pages.
2109 * (Otherwise, its page frames may be reallocated, and any
2110 * modify bits will be set in the wrong object!)
2112 vm_map_entry_delete(map, entry);
2115 return (KERN_SUCCESS);
2121 * Remove the given address range from the target map.
2122 * This is the exported form of vm_map_delete.
2125 vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
2131 if (map == kmem_map)
2135 VM_MAP_RANGE_CHECK(map, start, end);
2136 result = vm_map_delete(map, start, end);
2139 if (map == kmem_map)
2146 * vm_map_check_protection:
2148 * Assert that the target map allows the specified
2149 * privilege on the entire address region given.
2150 * The entire region must be allocated.
2153 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
2154 vm_prot_t protection)
2156 vm_map_entry_t entry;
2157 vm_map_entry_t tmp_entry;
2161 vm_map_lock_read(map);
2162 if (!vm_map_lookup_entry(map, start, &tmp_entry)) {
2163 vm_map_unlock_read(map);
2168 while (start < end) {
2169 if (entry == &map->header) {
2170 vm_map_unlock_read(map);
2176 if (start < entry->start) {
2177 vm_map_unlock_read(map);
2181 * Check protection associated with entry.
2183 if ((entry->protection & protection) != protection) {
2184 vm_map_unlock_read(map);
2187 /* go to next entry */
2189 entry = entry->next;
2191 vm_map_unlock_read(map);
2196 * Split the pages in a map entry into a new object. This affords
2197 * easier removal of unused pages, and keeps object inheritance from
2198 * being a negative impact on memory usage.
2201 vm_map_split(vm_map_entry_t entry)
2204 vm_object_t orig_object, new_object, source;
2206 vm_pindex_t offidxstart, offidxend, idx;
2208 vm_ooffset_t offset;
2212 orig_object = entry->object.vm_object;
2213 if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
2215 if (orig_object->ref_count <= 1)
2218 offset = entry->offset;
2222 offidxstart = OFF_TO_IDX(offset);
2223 offidxend = offidxstart + OFF_TO_IDX(e - s);
2224 size = offidxend - offidxstart;
2226 new_object = vm_pager_allocate(orig_object->type,
2227 NULL, IDX_TO_OFF(size), VM_PROT_ALL, 0LL);
2228 if (new_object == NULL)
2231 source = orig_object->backing_object;
2232 if (source != NULL) {
2233 vm_object_reference(source); /* Referenced by new_object */
2234 TAILQ_INSERT_TAIL(&source->shadow_head,
2235 new_object, shadow_list);
2236 vm_object_clear_flag(source, OBJ_ONEMAPPING);
2237 new_object->backing_object_offset =
2238 orig_object->backing_object_offset + IDX_TO_OFF(offidxstart);
2239 new_object->backing_object = source;
2240 source->shadow_count++;
2241 source->generation++;
2244 for (idx = 0; idx < size; idx++) {
2248 m = vm_page_lookup(orig_object, offidxstart + idx);
2253 * We must wait for pending I/O to complete before we can
2256 * We do not have to VM_PROT_NONE the page as mappings should
2257 * not be changed by this operation.
2259 if (vm_page_sleep_busy(m, TRUE, "spltwt"))
2263 vm_page_rename(m, new_object, idx);
2264 /* page automatically made dirty by rename and cache handled */
2268 if (orig_object->type == OBJT_SWAP) {
2269 vm_object_pip_add(orig_object, 1);
2271 * copy orig_object pages into new_object
2272 * and destroy unneeded pages in
2275 swap_pager_copy(orig_object, new_object, offidxstart, 0);
2276 vm_object_pip_wakeup(orig_object);
2279 for (idx = 0; idx < size; idx++) {
2280 m = vm_page_lookup(new_object, idx);
2286 entry->object.vm_object = new_object;
2287 entry->offset = 0LL;
2288 vm_object_deallocate(orig_object);
2292 * vm_map_copy_entry:
2294 * Copies the contents of the source entry to the destination
2295 * entry. The entries *must* be aligned properly.
2301 vm_map_entry_t src_entry,
2302 vm_map_entry_t dst_entry)
2304 vm_object_t src_object;
2306 if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
2309 if (src_entry->wired_count == 0) {
2312 * If the source entry is marked needs_copy, it is already
2315 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
2316 pmap_protect(src_map->pmap,
2319 src_entry->protection & ~VM_PROT_WRITE);
2323 * Make a copy of the object.
2325 if ((src_object = src_entry->object.vm_object) != NULL) {
2327 if ((src_object->handle == NULL) &&
2328 (src_object->type == OBJT_DEFAULT ||
2329 src_object->type == OBJT_SWAP)) {
2330 vm_object_collapse(src_object);
2331 if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
2332 vm_map_split(src_entry);
2333 src_object = src_entry->object.vm_object;
2337 vm_object_reference(src_object);
2338 vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
2339 dst_entry->object.vm_object = src_object;
2340 src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2341 dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2342 dst_entry->offset = src_entry->offset;
2344 dst_entry->object.vm_object = NULL;
2345 dst_entry->offset = 0;
2348 pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
2349 dst_entry->end - dst_entry->start, src_entry->start);
2352 * Of course, wired down pages can't be set copy-on-write.
2353 * Cause wired pages to be copied into the new map by
2354 * simulating faults (the new pages are pageable)
2356 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry);
2362 * Create a new process vmspace structure and vm_map
2363 * based on those of an existing process. The new map
2364 * is based on the old map, according to the inheritance
2365 * values on the regions in that map.
2367 * The source map must not be locked.
2370 vmspace_fork(struct vmspace *vm1)
2372 struct vmspace *vm2;
2373 vm_map_t old_map = &vm1->vm_map;
2375 vm_map_entry_t old_entry;
2376 vm_map_entry_t new_entry;
2381 vm_map_lock(old_map);
2382 old_map->infork = 1;
2384 vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
2385 bcopy(&vm1->vm_startcopy, &vm2->vm_startcopy,
2386 (caddr_t) &vm1->vm_endcopy - (caddr_t) &vm1->vm_startcopy);
2387 new_map = &vm2->vm_map; /* XXX */
2388 new_map->timestamp = 1;
2390 old_entry = old_map->header.next;
2392 while (old_entry != &old_map->header) {
2393 if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2394 panic("vm_map_fork: encountered a submap");
2396 switch (old_entry->inheritance) {
2397 case VM_INHERIT_NONE:
2400 case VM_INHERIT_SHARE:
2402 * Clone the entry, creating the shared object if necessary.
2404 object = old_entry->object.vm_object;
2405 if (object == NULL) {
2406 object = vm_object_allocate(OBJT_DEFAULT,
2407 atop(old_entry->end - old_entry->start));
2408 old_entry->object.vm_object = object;
2409 old_entry->offset = (vm_offset_t) 0;
2413 * Add the reference before calling vm_object_shadow
2414 * to insure that a shadow object is created.
2416 vm_object_reference(object);
2417 if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
2418 vm_object_shadow(&old_entry->object.vm_object,
2420 atop(old_entry->end - old_entry->start));
2421 old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
2422 /* Transfer the second reference too. */
2423 vm_object_reference(
2424 old_entry->object.vm_object);
2425 vm_object_deallocate(object);
2426 object = old_entry->object.vm_object;
2428 vm_object_clear_flag(object, OBJ_ONEMAPPING);
2431 * Clone the entry, referencing the shared object.
2433 new_entry = vm_map_entry_create(new_map);
2434 *new_entry = *old_entry;
2435 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2436 new_entry->wired_count = 0;
2439 * Insert the entry into the new map -- we know we're
2440 * inserting at the end of the new map.
2442 vm_map_entry_link(new_map, new_map->header.prev,
2446 * Update the physical map
2448 pmap_copy(new_map->pmap, old_map->pmap,
2450 (old_entry->end - old_entry->start),
2454 case VM_INHERIT_COPY:
2456 * Clone the entry and link into the map.
2458 new_entry = vm_map_entry_create(new_map);
2459 *new_entry = *old_entry;
2460 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2461 new_entry->wired_count = 0;
2462 new_entry->object.vm_object = NULL;
2463 vm_map_entry_link(new_map, new_map->header.prev,
2465 vm_map_copy_entry(old_map, new_map, old_entry,
2469 old_entry = old_entry->next;
2472 new_map->size = old_map->size;
2473 old_map->infork = 0;
2474 vm_map_unlock(old_map);
2480 vm_map_stack (vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
2481 vm_prot_t prot, vm_prot_t max, int cow)
2483 vm_map_entry_t prev_entry;
2484 vm_map_entry_t new_stack_entry;
2485 vm_size_t init_ssize;
2490 if (VM_MIN_ADDRESS > 0 && addrbos < VM_MIN_ADDRESS)
2491 return (KERN_NO_SPACE);
2493 if (max_ssize < sgrowsiz)
2494 init_ssize = max_ssize;
2496 init_ssize = sgrowsiz;
2500 /* If addr is already mapped, no go */
2501 if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
2503 return (KERN_NO_SPACE);
2506 /* If we can't accomodate max_ssize in the current mapping,
2507 * no go. However, we need to be aware that subsequent user
2508 * mappings might map into the space we have reserved for
2509 * stack, and currently this space is not protected.
2511 * Hopefully we will at least detect this condition
2512 * when we try to grow the stack.
2514 if ((prev_entry->next != &map->header) &&
2515 (prev_entry->next->start < addrbos + max_ssize)) {
2517 return (KERN_NO_SPACE);
2520 /* We initially map a stack of only init_ssize. We will
2521 * grow as needed later. Since this is to be a grow
2522 * down stack, we map at the top of the range.
2524 * Note: we would normally expect prot and max to be
2525 * VM_PROT_ALL, and cow to be 0. Possibly we should
2526 * eliminate these as input parameters, and just
2527 * pass these values here in the insert call.
2529 rv = vm_map_insert(map, NULL, 0, addrbos + max_ssize - init_ssize,
2530 addrbos + max_ssize, prot, max, cow);
2532 /* Now set the avail_ssize amount */
2533 if (rv == KERN_SUCCESS){
2534 if (prev_entry != &map->header)
2535 vm_map_clip_end(map, prev_entry, addrbos + max_ssize - init_ssize);
2536 new_stack_entry = prev_entry->next;
2537 if (new_stack_entry->end != addrbos + max_ssize ||
2538 new_stack_entry->start != addrbos + max_ssize - init_ssize)
2539 panic ("Bad entry start/end for new stack entry");
2541 new_stack_entry->avail_ssize = max_ssize - init_ssize;
2548 /* Attempts to grow a vm stack entry. Returns KERN_SUCCESS if the
2549 * desired address is already mapped, or if we successfully grow
2550 * the stack. Also returns KERN_SUCCESS if addr is outside the
2551 * stack range (this is strange, but preserves compatibility with
2552 * the grow function in vm_machdep.c).
2555 vm_map_growstack (struct proc *p, vm_offset_t addr)
2557 vm_map_entry_t prev_entry;
2558 vm_map_entry_t stack_entry;
2559 vm_map_entry_t new_stack_entry;
2560 struct vmspace *vm = p->p_vmspace;
2561 vm_map_t map = &vm->vm_map;
2570 vm_map_lock_read(map);
2572 /* If addr is already in the entry range, no need to grow.*/
2573 if (vm_map_lookup_entry(map, addr, &prev_entry)) {
2574 vm_map_unlock_read(map);
2575 return (KERN_SUCCESS);
2578 if ((stack_entry = prev_entry->next) == &map->header) {
2579 vm_map_unlock_read(map);
2580 return (KERN_SUCCESS);
2582 if (prev_entry == &map->header)
2583 end = stack_entry->start - stack_entry->avail_ssize;
2585 end = prev_entry->end;
2587 /* This next test mimics the old grow function in vm_machdep.c.
2588 * It really doesn't quite make sense, but we do it anyway
2589 * for compatibility.
2591 * If not growable stack, return success. This signals the
2592 * caller to proceed as he would normally with normal vm.
2594 if (stack_entry->avail_ssize < 1 ||
2595 addr >= stack_entry->start ||
2596 addr < stack_entry->start - stack_entry->avail_ssize) {
2597 vm_map_unlock_read(map);
2598 return (KERN_SUCCESS);
2601 /* Find the minimum grow amount */
2602 grow_amount = roundup (stack_entry->start - addr, PAGE_SIZE);
2603 if (grow_amount > stack_entry->avail_ssize) {
2604 vm_map_unlock_read(map);
2605 return (KERN_NO_SPACE);
2608 /* If there is no longer enough space between the entries
2609 * nogo, and adjust the available space. Note: this
2610 * should only happen if the user has mapped into the
2611 * stack area after the stack was created, and is
2612 * probably an error.
2614 * This also effectively destroys any guard page the user
2615 * might have intended by limiting the stack size.
2617 if (grow_amount > stack_entry->start - end) {
2618 if (vm_map_lock_upgrade(map))
2621 stack_entry->avail_ssize = stack_entry->start - end;
2624 return (KERN_NO_SPACE);
2627 is_procstack = addr >= (vm_offset_t)vm->vm_maxsaddr;
2629 /* If this is the main process stack, see if we're over the
2632 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount >
2633 p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
2634 vm_map_unlock_read(map);
2635 return (KERN_NO_SPACE);
2638 /* Round up the grow amount modulo SGROWSIZ */
2639 grow_amount = roundup (grow_amount, sgrowsiz);
2640 if (grow_amount > stack_entry->avail_ssize) {
2641 grow_amount = stack_entry->avail_ssize;
2643 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount >
2644 p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
2645 grow_amount = p->p_rlimit[RLIMIT_STACK].rlim_cur -
2649 if (vm_map_lock_upgrade(map))
2652 /* Get the preliminary new entry start value */
2653 addr = stack_entry->start - grow_amount;
2655 /* If this puts us into the previous entry, cut back our growth
2656 * to the available space. Also, see the note above.
2659 stack_entry->avail_ssize = stack_entry->start - end;
2663 rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start,
2668 /* Adjust the available stack space by the amount we grew. */
2669 if (rv == KERN_SUCCESS) {
2670 if (prev_entry != &map->header)
2671 vm_map_clip_end(map, prev_entry, addr);
2672 new_stack_entry = prev_entry->next;
2673 if (new_stack_entry->end != stack_entry->start ||
2674 new_stack_entry->start != addr)
2675 panic ("Bad stack grow start/end in new stack entry");
2677 new_stack_entry->avail_ssize = stack_entry->avail_ssize -
2678 (new_stack_entry->end -
2679 new_stack_entry->start);
2681 vm->vm_ssize += btoc(new_stack_entry->end -
2682 new_stack_entry->start);
2691 * Unshare the specified VM space for exec. If other processes are
2692 * mapped to it, then create a new one. The new vmspace is null.
2695 vmspace_exec(struct proc *p)
2697 struct vmspace *oldvmspace = p->p_vmspace;
2698 struct vmspace *newvmspace;
2699 vm_map_t map = &p->p_vmspace->vm_map;
2702 newvmspace = vmspace_alloc(map->min_offset, map->max_offset);
2703 bcopy(&oldvmspace->vm_startcopy, &newvmspace->vm_startcopy,
2704 (caddr_t) (newvmspace + 1) - (caddr_t) &newvmspace->vm_startcopy);
2706 * This code is written like this for prototype purposes. The
2707 * goal is to avoid running down the vmspace here, but let the
2708 * other process's that are still using the vmspace to finally
2709 * run it down. Even though there is little or no chance of blocking
2710 * here, it is a good idea to keep this form for future mods.
2712 p->p_vmspace = newvmspace;
2713 pmap_pinit2(vmspace_pmap(newvmspace));
2714 vmspace_free(oldvmspace);
2715 if (p == curthread->td_proc) /* XXXKSE ? */
2716 pmap_activate(curthread);
2720 * Unshare the specified VM space for forcing COW. This
2721 * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
2724 vmspace_unshare(struct proc *p)
2726 struct vmspace *oldvmspace = p->p_vmspace;
2727 struct vmspace *newvmspace;
2730 if (oldvmspace->vm_refcnt == 1)
2732 newvmspace = vmspace_fork(oldvmspace);
2733 p->p_vmspace = newvmspace;
2734 pmap_pinit2(vmspace_pmap(newvmspace));
2735 vmspace_free(oldvmspace);
2736 if (p == curthread->td_proc) /* XXXKSE ? */
2737 pmap_activate(curthread);
2743 * Finds the VM object, offset, and
2744 * protection for a given virtual address in the
2745 * specified map, assuming a page fault of the
2748 * Leaves the map in question locked for read; return
2749 * values are guaranteed until a vm_map_lookup_done
2750 * call is performed. Note that the map argument
2751 * is in/out; the returned map must be used in
2752 * the call to vm_map_lookup_done.
2754 * A handle (out_entry) is returned for use in
2755 * vm_map_lookup_done, to make that fast.
2757 * If a lookup is requested with "write protection"
2758 * specified, the map may be changed to perform virtual
2759 * copying operations, although the data referenced will
2763 vm_map_lookup(vm_map_t *var_map, /* IN/OUT */
2765 vm_prot_t fault_typea,
2766 vm_map_entry_t *out_entry, /* OUT */
2767 vm_object_t *object, /* OUT */
2768 vm_pindex_t *pindex, /* OUT */
2769 vm_prot_t *out_prot, /* OUT */
2770 boolean_t *wired) /* OUT */
2772 vm_map_entry_t entry;
2773 vm_map_t map = *var_map;
2775 vm_prot_t fault_type = fault_typea;
2780 * Lookup the faulting address.
2783 vm_map_lock_read(map);
2784 #define RETURN(why) \
2786 vm_map_unlock_read(map); \
2791 * If the map has an interesting hint, try it before calling full
2792 * blown lookup routine.
2796 if ((entry == &map->header) ||
2797 (vaddr < entry->start) || (vaddr >= entry->end)) {
2798 vm_map_entry_t tmp_entry;
2801 * Entry was either not a valid hint, or the vaddr was not
2802 * contained in the entry, so do a full lookup.
2804 if (!vm_map_lookup_entry(map, vaddr, &tmp_entry))
2805 RETURN(KERN_INVALID_ADDRESS);
2814 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
2815 vm_map_t old_map = map;
2817 *var_map = map = entry->object.sub_map;
2818 vm_map_unlock_read(old_map);
2823 * Check whether this task is allowed to have this page.
2824 * Note the special case for MAP_ENTRY_COW
2825 * pages with an override. This is to implement a forced
2826 * COW for debuggers.
2828 if (fault_type & VM_PROT_OVERRIDE_WRITE)
2829 prot = entry->max_protection;
2831 prot = entry->protection;
2832 fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
2833 if ((fault_type & prot) != fault_type) {
2834 RETURN(KERN_PROTECTION_FAILURE);
2836 if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
2837 (entry->eflags & MAP_ENTRY_COW) &&
2838 (fault_type & VM_PROT_WRITE) &&
2839 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
2840 RETURN(KERN_PROTECTION_FAILURE);
2844 * If this page is not pageable, we have to get it for all possible
2847 *wired = (entry->wired_count != 0);
2849 prot = fault_type = entry->protection;
2852 * If the entry was copy-on-write, we either ...
2854 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
2856 * If we want to write the page, we may as well handle that
2857 * now since we've got the map locked.
2859 * If we don't need to write the page, we just demote the
2860 * permissions allowed.
2862 if (fault_type & VM_PROT_WRITE) {
2864 * Make a new object, and place it in the object
2865 * chain. Note that no new references have appeared
2866 * -- one just moved from the map to the new
2869 if (vm_map_lock_upgrade(map))
2872 &entry->object.vm_object,
2874 atop(entry->end - entry->start));
2875 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
2876 vm_map_lock_downgrade(map);
2879 * We're attempting to read a copy-on-write page --
2880 * don't allow writes.
2882 prot &= ~VM_PROT_WRITE;
2887 * Create an object if necessary.
2889 if (entry->object.vm_object == NULL &&
2891 if (vm_map_lock_upgrade(map))
2893 entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
2894 atop(entry->end - entry->start));
2896 vm_map_lock_downgrade(map);
2900 * Return the object/offset from this entry. If the entry was
2901 * copy-on-write or empty, it has been fixed up.
2903 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
2904 *object = entry->object.vm_object;
2907 * Return whether this is the only map sharing this data.
2910 return (KERN_SUCCESS);
2916 * vm_map_lookup_done:
2918 * Releases locks acquired by a vm_map_lookup
2919 * (according to the handle returned by that lookup).
2922 vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry)
2925 * Unlock the main-level map
2928 vm_map_unlock_read(map);
2932 * Implement uiomove with VM operations. This handles (and collateral changes)
2933 * support every combination of source object modification, and COW type
2939 vm_object_t srcobject,
2946 vm_object_t first_object, oldobject, object;
2947 vm_map_entry_t entry;
2951 vm_offset_t uaddr, start, end, tend;
2952 vm_pindex_t first_pindex, osize, oindex;
2967 if ((vm_map_lookup(&map, uaddr,
2968 VM_PROT_READ, &entry, &first_object,
2969 &first_pindex, &prot, &wired)) != KERN_SUCCESS) {
2973 vm_map_clip_start(map, entry, uaddr);
2976 tend = uaddr + tcnt;
2977 if (tend > entry->end) {
2978 tcnt = entry->end - uaddr;
2982 vm_map_clip_end(map, entry, tend);
2984 start = entry->start;
2989 oindex = OFF_TO_IDX(cp);
2992 for (idx = 0; idx < osize; idx++) {
2994 if ((m = vm_page_lookup(srcobject, oindex + idx)) == NULL) {
2995 vm_map_lookup_done(map, entry);
2999 * disallow busy or invalid pages, but allow
3000 * m->busy pages if they are entirely valid.
3002 if ((m->flags & PG_BUSY) ||
3003 ((m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL)) {
3004 vm_map_lookup_done(map, entry);
3011 * If we are changing an existing map entry, just redirect
3012 * the object, and change mappings.
3014 if ((first_object->type == OBJT_VNODE) &&
3015 ((oldobject = entry->object.vm_object) == first_object)) {
3017 if ((entry->offset != cp) || (oldobject != srcobject)) {
3019 * Remove old window into the file
3021 pmap_remove (map->pmap, uaddr, tend);
3024 * Force copy on write for mmaped regions
3026 vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
3029 * Point the object appropriately
3031 if (oldobject != srcobject) {
3034 * Set the object optimization hint flag
3036 vm_object_set_flag(srcobject, OBJ_OPT);
3037 vm_object_reference(srcobject);
3038 entry->object.vm_object = srcobject;
3041 vm_object_deallocate(oldobject);
3048 pmap_remove (map->pmap, uaddr, tend);
3051 } else if ((first_object->ref_count == 1) &&
3052 (first_object->size == osize) &&
3053 ((first_object->type == OBJT_DEFAULT) ||
3054 (first_object->type == OBJT_SWAP)) ) {
3056 oldobject = first_object->backing_object;
3058 if ((first_object->backing_object_offset != cp) ||
3059 (oldobject != srcobject)) {
3061 * Remove old window into the file
3063 pmap_remove (map->pmap, uaddr, tend);
3066 * Remove unneeded old pages
3068 vm_object_page_remove(first_object, 0, 0, 0);
3071 * Invalidate swap space
3073 if (first_object->type == OBJT_SWAP) {
3074 swap_pager_freespace(first_object,
3076 first_object->size);
3080 * Force copy on write for mmaped regions
3082 vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
3085 * Point the object appropriately
3087 if (oldobject != srcobject) {
3089 * Set the object optimization hint flag
3091 vm_object_set_flag(srcobject, OBJ_OPT);
3092 vm_object_reference(srcobject);
3095 TAILQ_REMOVE(&oldobject->shadow_head,
3096 first_object, shadow_list);
3097 oldobject->shadow_count--;
3098 /* XXX bump generation? */
3099 vm_object_deallocate(oldobject);
3102 TAILQ_INSERT_TAIL(&srcobject->shadow_head,
3103 first_object, shadow_list);
3104 srcobject->shadow_count++;
3105 /* XXX bump generation? */
3107 first_object->backing_object = srcobject;
3109 first_object->backing_object_offset = cp;
3112 pmap_remove (map->pmap, uaddr, tend);
3115 * Otherwise, we have to do a logical mmap.
3119 vm_object_set_flag(srcobject, OBJ_OPT);
3120 vm_object_reference(srcobject);
3122 pmap_remove (map->pmap, uaddr, tend);
3124 vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
3125 vm_map_lock_upgrade(map);
3127 if (entry == &map->header) {
3128 map->first_free = &map->header;
3129 } else if (map->first_free->start >= start) {
3130 map->first_free = entry->prev;
3133 SAVE_HINT(map, entry->prev);
3134 vm_map_entry_delete(map, entry);
3139 rv = vm_map_insert(map, object, ooffset, start, tend,
3140 VM_PROT_ALL, VM_PROT_ALL, MAP_COPY_ON_WRITE);
3142 if (rv != KERN_SUCCESS)
3143 panic("vm_uiomove: could not insert new entry: %d", rv);
3147 * Map the window directly, if it is already in memory
3149 pmap_object_init_pt(map->pmap, uaddr,
3150 srcobject, oindex, tcnt, 0);
3165 * Performs the copy_on_write operations necessary to allow the virtual copies
3166 * into user space to work. This has to be called for write(2) system calls
3167 * from other processes, file unlinking, and file size shrinkage.
3170 vm_freeze_copyopts(vm_object_t object, vm_pindex_t froma, vm_pindex_t toa)
3173 vm_object_t robject;
3177 if ((object == NULL) ||
3178 ((object->flags & OBJ_OPT) == 0))
3181 if (object->shadow_count > object->ref_count)
3182 panic("vm_freeze_copyopts: sc > rc");
3184 while ((robject = TAILQ_FIRST(&object->shadow_head)) != NULL) {
3185 vm_pindex_t bo_pindex;
3186 vm_page_t m_in, m_out;
3188 bo_pindex = OFF_TO_IDX(robject->backing_object_offset);
3190 vm_object_reference(robject);
3192 vm_object_pip_wait(robject, "objfrz");
3194 if (robject->ref_count == 1) {
3195 vm_object_deallocate(robject);
3199 vm_object_pip_add(robject, 1);
3201 for (idx = 0; idx < robject->size; idx++) {
3203 m_out = vm_page_grab(robject, idx,
3204 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
3206 if (m_out->valid == 0) {
3207 m_in = vm_page_grab(object, bo_pindex + idx,
3208 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
3209 if (m_in->valid == 0) {
3210 rv = vm_pager_get_pages(object, &m_in, 1, 0);
3211 if (rv != VM_PAGER_OK) {
3212 printf("vm_freeze_copyopts: cannot read page from file: %lx\n", (long)m_in->pindex);
3215 vm_page_deactivate(m_in);
3218 vm_page_protect(m_in, VM_PROT_NONE);
3219 pmap_copy_page(m_in, m_out);
3220 m_out->valid = m_in->valid;
3221 vm_page_dirty(m_out);
3222 vm_page_activate(m_out);
3223 vm_page_wakeup(m_in);
3225 vm_page_wakeup(m_out);
3228 object->shadow_count--;
3229 object->ref_count--;
3230 TAILQ_REMOVE(&object->shadow_head, robject, shadow_list);
3231 robject->backing_object = NULL;
3232 robject->backing_object_offset = 0;
3234 vm_object_pip_wakeup(robject);
3235 vm_object_deallocate(robject);
3238 vm_object_clear_flag(object, OBJ_OPT);
3241 #include "opt_ddb.h"
3243 #include <sys/kernel.h>
3245 #include <ddb/ddb.h>
3248 * vm_map_print: [ debug ]
3250 DB_SHOW_COMMAND(map, vm_map_print)
3253 /* XXX convert args. */
3254 vm_map_t map = (vm_map_t)addr;
3255 boolean_t full = have_addr;
3257 vm_map_entry_t entry;
3259 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
3261 (void *)map->pmap, map->nentries, map->timestamp);
3264 if (!full && db_indent)
3268 for (entry = map->header.next; entry != &map->header;
3269 entry = entry->next) {
3270 db_iprintf("map entry %p: start=%p, end=%p\n",
3271 (void *)entry, (void *)entry->start, (void *)entry->end);
3274 static char *inheritance_name[4] =
3275 {"share", "copy", "none", "donate_copy"};
3277 db_iprintf(" prot=%x/%x/%s",
3279 entry->max_protection,
3280 inheritance_name[(int)(unsigned char)entry->inheritance]);
3281 if (entry->wired_count != 0)
3282 db_printf(", wired");
3284 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3285 /* XXX no %qd in kernel. Truncate entry->offset. */
3286 db_printf(", share=%p, offset=0x%lx\n",
3287 (void *)entry->object.sub_map,
3288 (long)entry->offset);
3290 if ((entry->prev == &map->header) ||
3291 (entry->prev->object.sub_map !=
3292 entry->object.sub_map)) {
3294 vm_map_print((db_expr_t)(intptr_t)
3295 entry->object.sub_map,
3296 full, 0, (char *)0);
3300 /* XXX no %qd in kernel. Truncate entry->offset. */
3301 db_printf(", object=%p, offset=0x%lx",
3302 (void *)entry->object.vm_object,
3303 (long)entry->offset);
3304 if (entry->eflags & MAP_ENTRY_COW)
3305 db_printf(", copy (%s)",
3306 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
3310 if ((entry->prev == &map->header) ||
3311 (entry->prev->object.vm_object !=
3312 entry->object.vm_object)) {
3314 vm_object_print((db_expr_t)(intptr_t)
3315 entry->object.vm_object,
3316 full, 0, (char *)0);
3328 DB_SHOW_COMMAND(procvm, procvm)
3333 p = (struct proc *) addr;
3338 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
3339 (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
3340 (void *)vmspace_pmap(p->p_vmspace));
3342 vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);