2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * The Mach Operating System project at Carnegie-Mellon University.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * from: @(#)vm_map.c 8.3 (Berkeley) 1/12/94
39 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
40 * All rights reserved.
42 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
44 * Permission to use, copy, modify and distribute this software and
45 * its documentation is hereby granted, provided that both the copyright
46 * notice and this permission notice appear in all copies of the
47 * software, derivative works or modified versions, and any portions
48 * thereof, and that both notices appear in supporting documentation.
50 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
51 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
52 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
54 * Carnegie Mellon requests users of this software to return to
56 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
57 * School of Computer Science
58 * Carnegie Mellon University
59 * Pittsburgh PA 15213-3890
61 * any improvements or extensions that they make and grant Carnegie the
62 * rights to redistribute these changes.
64 * $FreeBSD: src/sys/vm/vm_map.c,v 1.187.2.19 2003/05/27 00:47:02 alc Exp $
65 * $DragonFly: src/sys/vm/vm_map.c,v 1.2 2003/06/17 04:29:00 dillon Exp $
69 * Virtual memory mapping module.
72 #include <sys/param.h>
73 #include <sys/systm.h>
75 #include <sys/vmmeter.h>
77 #include <sys/vnode.h>
78 #include <sys/resourcevar.h>
81 #include <vm/vm_param.h>
84 #include <vm/vm_map.h>
85 #include <vm/vm_page.h>
86 #include <vm/vm_object.h>
87 #include <vm/vm_pager.h>
88 #include <vm/vm_kern.h>
89 #include <vm/vm_extern.h>
90 #include <vm/swap_pager.h>
91 #include <vm/vm_zone.h>
94 * Virtual memory maps provide for the mapping, protection,
95 * and sharing of virtual memory objects. In addition,
96 * this module provides for an efficient virtual copy of
97 * memory from one map to another.
99 * Synchronization is required prior to most operations.
101 * Maps consist of an ordered doubly-linked list of simple
102 * entries; a single hint is used to speed up lookups.
104 * Since portions of maps are specified by start/end addresses,
105 * which may not align with existing map entries, all
106 * routines merely "clip" entries to these start/end values.
107 * [That is, an entry is split into two, bordering at a
108 * start or end value.] Note that these clippings may not
109 * always be necessary (as the two resulting entries are then
110 * not changed); however, the clipping is done for convenience.
112 * As mentioned above, virtual copy operations are performed
113 * by copying VM object references from one map to
114 * another, and then marking both regions as copy-on-write.
120 * Initialize the vm_map module. Must be called before
121 * any other vm_map routines.
123 * Map and entry structures are allocated from the general
124 * purpose memory pool with some exceptions:
126 * - The kernel map and kmem submap are allocated statically.
127 * - Kernel map entries are allocated out of a static pool.
129 * These restrictions are necessary since malloc() uses the
130 * maps and requires map entries.
133 static struct vm_zone kmapentzone_store, mapentzone_store, mapzone_store;
134 static vm_zone_t mapentzone, kmapentzone, mapzone, vmspace_zone;
135 static struct vm_object kmapentobj, mapentobj, mapobj;
137 static struct vm_map_entry map_entry_init[MAX_MAPENT];
138 static struct vm_map_entry kmap_entry_init[MAX_KMAPENT];
139 static struct vm_map map_init[MAX_KMAP];
141 static void _vm_map_clip_end __P((vm_map_t, vm_map_entry_t, vm_offset_t));
142 static void _vm_map_clip_start __P((vm_map_t, vm_map_entry_t, vm_offset_t));
143 static vm_map_entry_t vm_map_entry_create __P((vm_map_t));
144 static void vm_map_entry_delete __P((vm_map_t, vm_map_entry_t));
145 static void vm_map_entry_dispose __P((vm_map_t, vm_map_entry_t));
146 static void vm_map_entry_unwire __P((vm_map_t, vm_map_entry_t));
147 static void vm_map_copy_entry __P((vm_map_t, vm_map_t, vm_map_entry_t,
149 static void vm_map_split __P((vm_map_entry_t));
150 static void vm_map_unclip_range __P((vm_map_t map, vm_map_entry_t start_entry, vm_offset_t start, vm_offset_t end, int flags));
155 mapzone = &mapzone_store;
156 zbootinit(mapzone, "MAP", sizeof (struct vm_map),
158 kmapentzone = &kmapentzone_store;
159 zbootinit(kmapentzone, "KMAP ENTRY", sizeof (struct vm_map_entry),
160 kmap_entry_init, MAX_KMAPENT);
161 mapentzone = &mapentzone_store;
162 zbootinit(mapentzone, "MAP ENTRY", sizeof (struct vm_map_entry),
163 map_entry_init, MAX_MAPENT);
167 * Allocate a vmspace structure, including a vm_map and pmap,
168 * and initialize those structures. The refcnt is set to 1.
169 * The remaining fields must be initialized by the caller.
172 vmspace_alloc(min, max)
173 vm_offset_t min, max;
177 vm = zalloc(vmspace_zone);
178 vm_map_init(&vm->vm_map, min, max);
179 pmap_pinit(vmspace_pmap(vm));
180 vm->vm_map.pmap = vmspace_pmap(vm); /* XXX */
183 vm->vm_exitingcnt = 0;
189 zinitna(kmapentzone, &kmapentobj,
190 NULL, 0, lmin((VM_MAX_KERNEL_ADDRESS - KERNBASE) / PAGE_SIZE,
191 cnt.v_page_count) / 8, ZONE_INTERRUPT, 1);
192 zinitna(mapentzone, &mapentobj,
194 zinitna(mapzone, &mapobj,
196 vmspace_zone = zinit("VMSPACE", sizeof (struct vmspace), 0, 0, 3);
202 vmspace_dofree(struct vmspace *vm)
205 * Lock the map, to wait out all other references to it.
206 * Delete all of the mappings and pages they hold, then call
207 * the pmap module to reclaim anything left.
209 vm_map_lock(&vm->vm_map);
210 (void) vm_map_delete(&vm->vm_map, vm->vm_map.min_offset,
211 vm->vm_map.max_offset);
212 vm_map_unlock(&vm->vm_map);
214 pmap_release(vmspace_pmap(vm));
215 zfree(vmspace_zone, vm);
219 vmspace_free(struct vmspace *vm)
221 if (vm->vm_refcnt == 0)
222 panic("vmspace_free: attempt to free already freed vmspace");
224 if (--vm->vm_refcnt == 0 && vm->vm_exitingcnt == 0)
229 vmspace_exitfree(struct proc *p)
237 * cleanup by parent process wait()ing on exiting child. vm_refcnt
238 * may not be 0 (e.g. fork() and child exits without exec()ing).
239 * exitingcnt may increment above 0 and drop back down to zero
240 * several times while vm_refcnt is held non-zero. vm_refcnt
241 * may also increment above 0 and drop back down to zero several
242 * times while vm_exitingcnt is held non-zero.
244 * The last wait on the exiting child's vmspace will clean up
245 * the remainder of the vmspace.
247 if (--vm->vm_exitingcnt == 0 && vm->vm_refcnt == 0)
252 * vmspace_swap_count() - count the approximate swap useage in pages for a
255 * Swap useage is determined by taking the proportional swap used by
256 * VM objects backing the VM map. To make up for fractional losses,
257 * if the VM object has any swap use at all the associated map entries
258 * count for at least 1 swap page.
261 vmspace_swap_count(struct vmspace *vmspace)
263 vm_map_t map = &vmspace->vm_map;
267 for (cur = map->header.next; cur != &map->header; cur = cur->next) {
270 if ((cur->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
271 (object = cur->object.vm_object) != NULL &&
272 object->type == OBJT_SWAP
274 int n = (cur->end - cur->start) / PAGE_SIZE;
276 if (object->un_pager.swp.swp_bcount) {
277 count += object->un_pager.swp.swp_bcount *
278 SWAP_META_PAGES * n / object->size + 1;
289 * Creates and returns a new empty VM map with
290 * the given physical map structure, and having
291 * the given lower and upper address bounds.
294 vm_map_create(pmap, min, max)
296 vm_offset_t min, max;
300 result = zalloc(mapzone);
301 vm_map_init(result, min, max);
307 * Initialize an existing vm_map structure
308 * such as that in the vmspace structure.
309 * The pmap is set elsewhere.
312 vm_map_init(map, min, max)
314 vm_offset_t min, max;
316 map->header.next = map->header.prev = &map->header;
321 map->min_offset = min;
322 map->max_offset = max;
323 map->first_free = &map->header;
324 map->hint = &map->header;
326 lockinit(&map->lock, PVM, "thrd_sleep", 0, LK_NOPAUSE);
330 * vm_map_entry_dispose: [ internal use only ]
332 * Inverse of vm_map_entry_create.
335 vm_map_entry_dispose(map, entry)
337 vm_map_entry_t entry;
339 if (map->system_map || !mapentzone)
340 zfreei(kmapentzone, entry);
342 zfree(mapentzone, entry);
346 * vm_map_entry_create: [ internal use only ]
348 * Allocates a VM map entry for insertion.
349 * No entry fields are filled in. This routine is
351 static vm_map_entry_t
352 vm_map_entry_create(map)
355 vm_map_entry_t new_entry;
357 if (map->system_map || !mapentzone)
358 new_entry = zalloci(kmapentzone);
360 new_entry = zalloc(mapentzone);
361 if (new_entry == NULL)
362 panic("vm_map_entry_create: kernel resources exhausted");
367 * vm_map_entry_{un,}link:
369 * Insert/remove entries from maps.
372 vm_map_entry_link(vm_map_t map,
373 vm_map_entry_t after_where,
374 vm_map_entry_t entry)
377 entry->prev = after_where;
378 entry->next = after_where->next;
379 entry->next->prev = entry;
380 after_where->next = entry;
384 vm_map_entry_unlink(vm_map_t map,
385 vm_map_entry_t entry)
390 if (entry->eflags & MAP_ENTRY_IN_TRANSITION)
391 panic("vm_map_entry_unlink: attempt to mess with locked entry! %p", entry);
402 * Saves the specified entry as the hint for
405 #define SAVE_HINT(map,value) \
406 (map)->hint = (value);
409 * vm_map_lookup_entry: [ internal use only ]
411 * Finds the map entry containing (or
412 * immediately preceding) the specified address
413 * in the given map; the entry is returned
414 * in the "entry" parameter. The boolean
415 * result indicates whether the address is
416 * actually contained in the map.
419 vm_map_lookup_entry(map, address, entry)
422 vm_map_entry_t *entry; /* OUT */
428 * Start looking either from the head of the list, or from the hint.
433 if (cur == &map->header)
436 if (address >= cur->start) {
438 * Go from hint to end of list.
440 * But first, make a quick check to see if we are already looking
441 * at the entry we want (which is usually the case). Note also
442 * that we don't need to save the hint here... it is the same
443 * hint (unless we are at the header, in which case the hint
444 * didn't buy us anything anyway).
447 if ((cur != last) && (cur->end > address)) {
453 * Go from start to hint, *inclusively*
456 cur = map->header.next;
463 while (cur != last) {
464 if (cur->end > address) {
465 if (address >= cur->start) {
467 * Save this lookup for future hints, and
480 SAVE_HINT(map, *entry);
487 * Inserts the given whole VM object into the target
488 * map at the specified address range. The object's
489 * size should match that of the address range.
491 * Requires that the map be locked, and leaves it so.
493 * If object is non-NULL, ref count must be bumped by caller
494 * prior to making call to account for the new entry.
497 vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
498 vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max,
501 vm_map_entry_t new_entry;
502 vm_map_entry_t prev_entry;
503 vm_map_entry_t temp_entry;
504 vm_eflags_t protoeflags;
507 * Check that the start and end points are not bogus.
510 if ((start < map->min_offset) || (end > map->max_offset) ||
512 return (KERN_INVALID_ADDRESS);
515 * Find the entry prior to the proposed starting address; if it's part
516 * of an existing entry, this range is bogus.
519 if (vm_map_lookup_entry(map, start, &temp_entry))
520 return (KERN_NO_SPACE);
522 prev_entry = temp_entry;
525 * Assert that the next entry doesn't overlap the end point.
528 if ((prev_entry->next != &map->header) &&
529 (prev_entry->next->start < end))
530 return (KERN_NO_SPACE);
534 if (cow & MAP_COPY_ON_WRITE)
535 protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
537 if (cow & MAP_NOFAULT) {
538 protoeflags |= MAP_ENTRY_NOFAULT;
540 KASSERT(object == NULL,
541 ("vm_map_insert: paradoxical MAP_NOFAULT request"));
543 if (cow & MAP_DISABLE_SYNCER)
544 protoeflags |= MAP_ENTRY_NOSYNC;
545 if (cow & MAP_DISABLE_COREDUMP)
546 protoeflags |= MAP_ENTRY_NOCOREDUMP;
550 * When object is non-NULL, it could be shared with another
551 * process. We have to set or clear OBJ_ONEMAPPING
554 if ((object->ref_count > 1) || (object->shadow_count != 0)) {
555 vm_object_clear_flag(object, OBJ_ONEMAPPING);
558 else if ((prev_entry != &map->header) &&
559 (prev_entry->eflags == protoeflags) &&
560 (prev_entry->end == start) &&
561 (prev_entry->wired_count == 0) &&
562 ((prev_entry->object.vm_object == NULL) ||
563 vm_object_coalesce(prev_entry->object.vm_object,
564 OFF_TO_IDX(prev_entry->offset),
565 (vm_size_t)(prev_entry->end - prev_entry->start),
566 (vm_size_t)(end - prev_entry->end)))) {
568 * We were able to extend the object. Determine if we
569 * can extend the previous map entry to include the
572 if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
573 (prev_entry->protection == prot) &&
574 (prev_entry->max_protection == max)) {
575 map->size += (end - prev_entry->end);
576 prev_entry->end = end;
577 vm_map_simplify_entry(map, prev_entry);
578 return (KERN_SUCCESS);
582 * If we can extend the object but cannot extend the
583 * map entry, we have to create a new map entry. We
584 * must bump the ref count on the extended object to
585 * account for it. object may be NULL.
587 object = prev_entry->object.vm_object;
588 offset = prev_entry->offset +
589 (prev_entry->end - prev_entry->start);
590 vm_object_reference(object);
594 * NOTE: if conditionals fail, object can be NULL here. This occurs
595 * in things like the buffer map where we manage kva but do not manage
603 new_entry = vm_map_entry_create(map);
604 new_entry->start = start;
605 new_entry->end = end;
607 new_entry->eflags = protoeflags;
608 new_entry->object.vm_object = object;
609 new_entry->offset = offset;
610 new_entry->avail_ssize = 0;
612 new_entry->inheritance = VM_INHERIT_DEFAULT;
613 new_entry->protection = prot;
614 new_entry->max_protection = max;
615 new_entry->wired_count = 0;
618 * Insert the new entry into the list
621 vm_map_entry_link(map, prev_entry, new_entry);
622 map->size += new_entry->end - new_entry->start;
625 * Update the free space hint
627 if ((map->first_free == prev_entry) &&
628 (prev_entry->end >= new_entry->start)) {
629 map->first_free = new_entry;
634 * Temporarily removed to avoid MAP_STACK panic, due to
635 * MAP_STACK being a huge hack. Will be added back in
636 * when MAP_STACK (and the user stack mapping) is fixed.
639 * It may be possible to simplify the entry
641 vm_map_simplify_entry(map, new_entry);
644 if (cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) {
645 pmap_object_init_pt(map->pmap, start,
646 object, OFF_TO_IDX(offset), end - start,
647 cow & MAP_PREFAULT_PARTIAL);
650 return (KERN_SUCCESS);
654 * Find sufficient space for `length' bytes in the given map, starting at
655 * `start'. The map must be locked. Returns 0 on success, 1 on no space.
658 vm_map_findspace(map, start, length, addr)
664 vm_map_entry_t entry, next;
667 if (start < map->min_offset)
668 start = map->min_offset;
669 if (start > map->max_offset)
673 * Look for the first possible address; if there's already something
674 * at this address, we have to start after it.
676 if (start == map->min_offset) {
677 if ((entry = map->first_free) != &map->header)
682 if (vm_map_lookup_entry(map, start, &tmp))
688 * Look through the rest of the map, trying to fit a new region in the
689 * gap between existing regions, or after the very last region.
691 for (;; start = (entry = next)->end) {
693 * Find the end of the proposed new region. Be sure we didn't
694 * go beyond the end of the map, or wrap around the address;
695 * if so, we lose. Otherwise, if this is the last entry, or
696 * if the proposed new region fits before the next entry, we
699 end = start + length;
700 if (end > map->max_offset || end < start)
703 if (next == &map->header || next->start >= end)
706 SAVE_HINT(map, entry);
708 if (map == kernel_map) {
710 if ((ksize = round_page(start + length)) > kernel_vm_end) {
711 pmap_growkernel(ksize);
718 * vm_map_find finds an unallocated region in the target address
719 * map with the given length. The search is defined to be
720 * first-fit from the specified address; the region found is
721 * returned in the same parameter.
723 * If object is non-NULL, ref count must be bumped by caller
724 * prior to making call to account for the new entry.
727 vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
728 vm_offset_t *addr, /* IN/OUT */
729 vm_size_t length, boolean_t find_space, vm_prot_t prot,
730 vm_prot_t max, int cow)
737 if (map == kmem_map || map == mb_map)
742 if (vm_map_findspace(map, start, length, addr)) {
744 if (map == kmem_map || map == mb_map)
746 return (KERN_NO_SPACE);
750 result = vm_map_insert(map, object, offset,
751 start, start + length, prot, max, cow);
754 if (map == kmem_map || map == mb_map)
761 * vm_map_simplify_entry:
763 * Simplify the given map entry by merging with either neighbor. This
764 * routine also has the ability to merge with both neighbors.
766 * The map must be locked.
768 * This routine guarentees that the passed entry remains valid (though
769 * possibly extended). When merging, this routine may delete one or
770 * both neighbors. No action is taken on entries which have their
771 * in-transition flag set.
774 vm_map_simplify_entry(map, entry)
776 vm_map_entry_t entry;
778 vm_map_entry_t next, prev;
779 vm_size_t prevsize, esize;
781 if (entry->eflags & (MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP)) {
782 ++cnt.v_intrans_coll;
787 if (prev != &map->header) {
788 prevsize = prev->end - prev->start;
789 if ( (prev->end == entry->start) &&
790 (prev->object.vm_object == entry->object.vm_object) &&
791 (!prev->object.vm_object ||
792 (prev->offset + prevsize == entry->offset)) &&
793 (prev->eflags == entry->eflags) &&
794 (prev->protection == entry->protection) &&
795 (prev->max_protection == entry->max_protection) &&
796 (prev->inheritance == entry->inheritance) &&
797 (prev->wired_count == entry->wired_count)) {
798 if (map->first_free == prev)
799 map->first_free = entry;
800 if (map->hint == prev)
802 vm_map_entry_unlink(map, prev);
803 entry->start = prev->start;
804 entry->offset = prev->offset;
805 if (prev->object.vm_object)
806 vm_object_deallocate(prev->object.vm_object);
807 vm_map_entry_dispose(map, prev);
812 if (next != &map->header) {
813 esize = entry->end - entry->start;
814 if ((entry->end == next->start) &&
815 (next->object.vm_object == entry->object.vm_object) &&
816 (!entry->object.vm_object ||
817 (entry->offset + esize == next->offset)) &&
818 (next->eflags == entry->eflags) &&
819 (next->protection == entry->protection) &&
820 (next->max_protection == entry->max_protection) &&
821 (next->inheritance == entry->inheritance) &&
822 (next->wired_count == entry->wired_count)) {
823 if (map->first_free == next)
824 map->first_free = entry;
825 if (map->hint == next)
827 vm_map_entry_unlink(map, next);
828 entry->end = next->end;
829 if (next->object.vm_object)
830 vm_object_deallocate(next->object.vm_object);
831 vm_map_entry_dispose(map, next);
836 * vm_map_clip_start: [ internal use only ]
838 * Asserts that the given entry begins at or after
839 * the specified address; if necessary,
840 * it splits the entry into two.
842 #define vm_map_clip_start(map, entry, startaddr) \
844 if (startaddr > entry->start) \
845 _vm_map_clip_start(map, entry, startaddr); \
849 * This routine is called only when it is known that
850 * the entry must be split.
853 _vm_map_clip_start(map, entry, start)
855 vm_map_entry_t entry;
858 vm_map_entry_t new_entry;
861 * Split off the front portion -- note that we must insert the new
862 * entry BEFORE this one, so that this entry has the specified
866 vm_map_simplify_entry(map, entry);
869 * If there is no object backing this entry, we might as well create
870 * one now. If we defer it, an object can get created after the map
871 * is clipped, and individual objects will be created for the split-up
872 * map. This is a bit of a hack, but is also about the best place to
873 * put this improvement.
876 if (entry->object.vm_object == NULL && !map->system_map) {
878 object = vm_object_allocate(OBJT_DEFAULT,
879 atop(entry->end - entry->start));
880 entry->object.vm_object = object;
884 new_entry = vm_map_entry_create(map);
887 new_entry->end = start;
888 entry->offset += (start - entry->start);
889 entry->start = start;
891 vm_map_entry_link(map, entry->prev, new_entry);
893 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
894 vm_object_reference(new_entry->object.vm_object);
899 * vm_map_clip_end: [ internal use only ]
901 * Asserts that the given entry ends at or before
902 * the specified address; if necessary,
903 * it splits the entry into two.
906 #define vm_map_clip_end(map, entry, endaddr) \
908 if (endaddr < entry->end) \
909 _vm_map_clip_end(map, entry, endaddr); \
913 * This routine is called only when it is known that
914 * the entry must be split.
917 _vm_map_clip_end(map, entry, end)
919 vm_map_entry_t entry;
922 vm_map_entry_t new_entry;
925 * If there is no object backing this entry, we might as well create
926 * one now. If we defer it, an object can get created after the map
927 * is clipped, and individual objects will be created for the split-up
928 * map. This is a bit of a hack, but is also about the best place to
929 * put this improvement.
932 if (entry->object.vm_object == NULL && !map->system_map) {
934 object = vm_object_allocate(OBJT_DEFAULT,
935 atop(entry->end - entry->start));
936 entry->object.vm_object = object;
941 * Create a new entry and insert it AFTER the specified entry
944 new_entry = vm_map_entry_create(map);
947 new_entry->start = entry->end = end;
948 new_entry->offset += (end - entry->start);
950 vm_map_entry_link(map, entry, new_entry);
952 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
953 vm_object_reference(new_entry->object.vm_object);
958 * VM_MAP_RANGE_CHECK: [ internal use only ]
960 * Asserts that the starting and ending region
961 * addresses fall within the valid range of the map.
963 #define VM_MAP_RANGE_CHECK(map, start, end) \
965 if (start < vm_map_min(map)) \
966 start = vm_map_min(map); \
967 if (end > vm_map_max(map)) \
968 end = vm_map_max(map); \
974 * vm_map_transition_wait: [ kernel use only ]
976 * Used to block when an in-transition collison occurs. The map
977 * is unlocked for the sleep and relocked before the return.
981 vm_map_transition_wait(vm_map_t map)
984 tsleep(map, PVM, "vment", 0);
992 * When we do blocking operations with the map lock held it is
993 * possible that a clip might have occured on our in-transit entry,
994 * requiring an adjustment to the entry in our loop. These macros
995 * help the pageable and clip_range code deal with the case. The
996 * conditional costs virtually nothing if no clipping has occured.
999 #define CLIP_CHECK_BACK(entry, save_start) \
1001 while (entry->start != save_start) { \
1002 entry = entry->prev; \
1003 KASSERT(entry != &map->header, ("bad entry clip")); \
1007 #define CLIP_CHECK_FWD(entry, save_end) \
1009 while (entry->end != save_end) { \
1010 entry = entry->next; \
1011 KASSERT(entry != &map->header, ("bad entry clip")); \
1017 * vm_map_clip_range: [ kernel use only ]
1019 * Clip the specified range and return the base entry. The
1020 * range may cover several entries starting at the returned base
1021 * and the first and last entry in the covering sequence will be
1022 * properly clipped to the requested start and end address.
1024 * If no holes are allowed you should pass the MAP_CLIP_NO_HOLES
1027 * The MAP_ENTRY_IN_TRANSITION flag will be set for the entries
1028 * covered by the requested range.
1030 * The map must be exclusively locked on entry and will remain locked
1031 * on return. If no range exists or the range contains holes and you
1032 * specified that no holes were allowed, NULL will be returned. This
1033 * routine may temporarily unlock the map in order avoid a deadlock when
1038 vm_map_clip_range(vm_map_t map, vm_offset_t start, vm_offset_t end, int flags)
1040 vm_map_entry_t start_entry;
1041 vm_map_entry_t entry;
1044 * Locate the entry and effect initial clipping. The in-transition
1045 * case does not occur very often so do not try to optimize it.
1048 if (vm_map_lookup_entry(map, start, &start_entry) == FALSE)
1050 entry = start_entry;
1051 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1052 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1053 ++cnt.v_intrans_coll;
1054 ++cnt.v_intrans_wait;
1055 vm_map_transition_wait(map);
1057 * entry and/or start_entry may have been clipped while
1058 * we slept, or may have gone away entirely. We have
1059 * to restart from the lookup.
1064 * Since we hold an exclusive map lock we do not have to restart
1065 * after clipping, even though clipping may block in zalloc.
1067 vm_map_clip_start(map, entry, start);
1068 vm_map_clip_end(map, entry, end);
1069 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
1072 * Scan entries covered by the range. When working on the next
1073 * entry a restart need only re-loop on the current entry which
1074 * we have already locked, since 'next' may have changed. Also,
1075 * even though entry is safe, it may have been clipped so we
1076 * have to iterate forwards through the clip after sleeping.
1078 while (entry->next != &map->header && entry->next->start < end) {
1079 vm_map_entry_t next = entry->next;
1081 if (flags & MAP_CLIP_NO_HOLES) {
1082 if (next->start > entry->end) {
1083 vm_map_unclip_range(map, start_entry,
1084 start, entry->end, flags);
1089 if (next->eflags & MAP_ENTRY_IN_TRANSITION) {
1090 vm_offset_t save_end = entry->end;
1091 next->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1092 ++cnt.v_intrans_coll;
1093 ++cnt.v_intrans_wait;
1094 vm_map_transition_wait(map);
1097 * clips might have occured while we blocked.
1099 CLIP_CHECK_FWD(entry, save_end);
1100 CLIP_CHECK_BACK(start_entry, start);
1104 * No restart necessary even though clip_end may block, we
1105 * are holding the map lock.
1107 vm_map_clip_end(map, next, end);
1108 next->eflags |= MAP_ENTRY_IN_TRANSITION;
1111 if (flags & MAP_CLIP_NO_HOLES) {
1112 if (entry->end != end) {
1113 vm_map_unclip_range(map, start_entry,
1114 start, entry->end, flags);
1118 return(start_entry);
1122 * vm_map_unclip_range: [ kernel use only ]
1124 * Undo the effect of vm_map_clip_range(). You should pass the same
1125 * flags and the same range that you passed to vm_map_clip_range().
1126 * This code will clear the in-transition flag on the entries and
1127 * wake up anyone waiting. This code will also simplify the sequence
1128 * and attempt to merge it with entries before and after the sequence.
1130 * The map must be locked on entry and will remain locked on return.
1132 * Note that you should also pass the start_entry returned by
1133 * vm_map_clip_range(). However, if you block between the two calls
1134 * with the map unlocked please be aware that the start_entry may
1135 * have been clipped and you may need to scan it backwards to find
1136 * the entry corresponding with the original start address. You are
1137 * responsible for this, vm_map_unclip_range() expects the correct
1138 * start_entry to be passed to it and will KASSERT otherwise.
1142 vm_map_unclip_range(
1144 vm_map_entry_t start_entry,
1149 vm_map_entry_t entry;
1151 entry = start_entry;
1153 KASSERT(entry->start == start, ("unclip_range: illegal base entry"));
1154 while (entry != &map->header && entry->start < end) {
1155 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION, ("in-transition flag not set during unclip on: %p", entry));
1156 KASSERT(entry->end <= end, ("unclip_range: tail wasn't clipped"));
1157 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
1158 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
1159 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
1162 entry = entry->next;
1166 * Simplification does not block so there is no restart case.
1168 entry = start_entry;
1169 while (entry != &map->header && entry->start < end) {
1170 vm_map_simplify_entry(map, entry);
1171 entry = entry->next;
1176 * vm_map_submap: [ kernel use only ]
1178 * Mark the given range as handled by a subordinate map.
1180 * This range must have been created with vm_map_find,
1181 * and no other operations may have been performed on this
1182 * range prior to calling vm_map_submap.
1184 * Only a limited number of operations can be performed
1185 * within this rage after calling vm_map_submap:
1187 * [Don't try vm_map_copy!]
1189 * To remove a submapping, one must first remove the
1190 * range from the superior map, and then destroy the
1191 * submap (if desired). [Better yet, don't try it.]
1194 vm_map_submap(map, start, end, submap)
1200 vm_map_entry_t entry;
1201 int result = KERN_INVALID_ARGUMENT;
1205 VM_MAP_RANGE_CHECK(map, start, end);
1207 if (vm_map_lookup_entry(map, start, &entry)) {
1208 vm_map_clip_start(map, entry, start);
1210 entry = entry->next;
1213 vm_map_clip_end(map, entry, end);
1215 if ((entry->start == start) && (entry->end == end) &&
1216 ((entry->eflags & MAP_ENTRY_COW) == 0) &&
1217 (entry->object.vm_object == NULL)) {
1218 entry->object.sub_map = submap;
1219 entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
1220 result = KERN_SUCCESS;
1230 * Sets the protection of the specified address
1231 * region in the target map. If "set_max" is
1232 * specified, the maximum protection is to be set;
1233 * otherwise, only the current protection is affected.
1236 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
1237 vm_prot_t new_prot, boolean_t set_max)
1239 vm_map_entry_t current;
1240 vm_map_entry_t entry;
1244 VM_MAP_RANGE_CHECK(map, start, end);
1246 if (vm_map_lookup_entry(map, start, &entry)) {
1247 vm_map_clip_start(map, entry, start);
1249 entry = entry->next;
1253 * Make a first pass to check for protection violations.
1257 while ((current != &map->header) && (current->start < end)) {
1258 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1260 return (KERN_INVALID_ARGUMENT);
1262 if ((new_prot & current->max_protection) != new_prot) {
1264 return (KERN_PROTECTION_FAILURE);
1266 current = current->next;
1270 * Go back and fix up protections. [Note that clipping is not
1271 * necessary the second time.]
1276 while ((current != &map->header) && (current->start < end)) {
1279 vm_map_clip_end(map, current, end);
1281 old_prot = current->protection;
1283 current->protection =
1284 (current->max_protection = new_prot) &
1287 current->protection = new_prot;
1290 * Update physical map if necessary. Worry about copy-on-write
1291 * here -- CHECK THIS XXX
1294 if (current->protection != old_prot) {
1295 #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1298 pmap_protect(map->pmap, current->start,
1300 current->protection & MASK(current));
1304 vm_map_simplify_entry(map, current);
1306 current = current->next;
1310 return (KERN_SUCCESS);
1316 * This routine traverses a processes map handling the madvise
1317 * system call. Advisories are classified as either those effecting
1318 * the vm_map_entry structure, or those effecting the underlying
1323 vm_map_madvise(map, start, end, behav)
1325 vm_offset_t start, end;
1328 vm_map_entry_t current, entry;
1332 * Some madvise calls directly modify the vm_map_entry, in which case
1333 * we need to use an exclusive lock on the map and we need to perform
1334 * various clipping operations. Otherwise we only need a read-lock
1340 case MADV_SEQUENTIAL:
1352 vm_map_lock_read(map);
1355 return (KERN_INVALID_ARGUMENT);
1359 * Locate starting entry and clip if necessary.
1362 VM_MAP_RANGE_CHECK(map, start, end);
1364 if (vm_map_lookup_entry(map, start, &entry)) {
1366 vm_map_clip_start(map, entry, start);
1368 entry = entry->next;
1373 * madvise behaviors that are implemented in the vm_map_entry.
1375 * We clip the vm_map_entry so that behavioral changes are
1376 * limited to the specified address range.
1378 for (current = entry;
1379 (current != &map->header) && (current->start < end);
1380 current = current->next
1382 if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1385 vm_map_clip_end(map, current, end);
1389 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
1391 case MADV_SEQUENTIAL:
1392 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
1395 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
1398 current->eflags |= MAP_ENTRY_NOSYNC;
1401 current->eflags &= ~MAP_ENTRY_NOSYNC;
1404 current->eflags |= MAP_ENTRY_NOCOREDUMP;
1407 current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
1412 vm_map_simplify_entry(map, current);
1420 * madvise behaviors that are implemented in the underlying
1423 * Since we don't clip the vm_map_entry, we have to clip
1424 * the vm_object pindex and count.
1426 for (current = entry;
1427 (current != &map->header) && (current->start < end);
1428 current = current->next
1430 vm_offset_t useStart;
1432 if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1435 pindex = OFF_TO_IDX(current->offset);
1436 count = atop(current->end - current->start);
1437 useStart = current->start;
1439 if (current->start < start) {
1440 pindex += atop(start - current->start);
1441 count -= atop(start - current->start);
1444 if (current->end > end)
1445 count -= atop(current->end - end);
1450 vm_object_madvise(current->object.vm_object,
1451 pindex, count, behav);
1452 if (behav == MADV_WILLNEED) {
1453 pmap_object_init_pt(
1456 current->object.vm_object,
1458 (count << PAGE_SHIFT),
1459 MAP_PREFAULT_MADVISE
1463 vm_map_unlock_read(map);
1472 * Sets the inheritance of the specified address
1473 * range in the target map. Inheritance
1474 * affects how the map will be shared with
1475 * child maps at the time of vm_map_fork.
1478 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
1479 vm_inherit_t new_inheritance)
1481 vm_map_entry_t entry;
1482 vm_map_entry_t temp_entry;
1484 switch (new_inheritance) {
1485 case VM_INHERIT_NONE:
1486 case VM_INHERIT_COPY:
1487 case VM_INHERIT_SHARE:
1490 return (KERN_INVALID_ARGUMENT);
1495 VM_MAP_RANGE_CHECK(map, start, end);
1497 if (vm_map_lookup_entry(map, start, &temp_entry)) {
1499 vm_map_clip_start(map, entry, start);
1501 entry = temp_entry->next;
1503 while ((entry != &map->header) && (entry->start < end)) {
1504 vm_map_clip_end(map, entry, end);
1506 entry->inheritance = new_inheritance;
1508 vm_map_simplify_entry(map, entry);
1510 entry = entry->next;
1514 return (KERN_SUCCESS);
1518 * Implement the semantics of mlock
1521 vm_map_user_pageable(map, start, real_end, new_pageable)
1524 vm_offset_t real_end;
1525 boolean_t new_pageable;
1527 vm_map_entry_t entry;
1528 vm_map_entry_t start_entry;
1530 int rv = KERN_SUCCESS;
1533 VM_MAP_RANGE_CHECK(map, start, real_end);
1536 start_entry = vm_map_clip_range(map, start, end, MAP_CLIP_NO_HOLES);
1537 if (start_entry == NULL) {
1539 return (KERN_INVALID_ADDRESS);
1542 if (new_pageable == 0) {
1543 entry = start_entry;
1544 while ((entry != &map->header) && (entry->start < end)) {
1545 vm_offset_t save_start;
1546 vm_offset_t save_end;
1549 * Already user wired or hard wired (trivial cases)
1551 if (entry->eflags & MAP_ENTRY_USER_WIRED) {
1552 entry = entry->next;
1555 if (entry->wired_count != 0) {
1556 entry->wired_count++;
1557 entry->eflags |= MAP_ENTRY_USER_WIRED;
1558 entry = entry->next;
1563 * A new wiring requires instantiation of appropriate
1564 * management structures and the faulting in of the
1567 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1568 int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY;
1569 if (copyflag && ((entry->protection & VM_PROT_WRITE) != 0)) {
1571 vm_object_shadow(&entry->object.vm_object,
1573 atop(entry->end - entry->start));
1574 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
1576 } else if (entry->object.vm_object == NULL &&
1579 entry->object.vm_object =
1580 vm_object_allocate(OBJT_DEFAULT,
1581 atop(entry->end - entry->start));
1582 entry->offset = (vm_offset_t) 0;
1586 entry->wired_count++;
1587 entry->eflags |= MAP_ENTRY_USER_WIRED;
1590 * Now fault in the area. The map lock needs to be
1591 * manipulated to avoid deadlocks. The in-transition
1592 * flag protects the entries.
1594 save_start = entry->start;
1595 save_end = entry->end;
1598 rv = vm_fault_user_wire(map, save_start, save_end);
1601 CLIP_CHECK_BACK(entry, save_start);
1603 KASSERT(entry->wired_count == 1, ("bad wired_count on entry"));
1604 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
1605 entry->wired_count = 0;
1606 if (entry->end == save_end)
1608 entry = entry->next;
1609 KASSERT(entry != &map->header, ("bad entry clip during backout"));
1611 end = save_start; /* unwire the rest */
1615 * note that even though the entry might have been
1616 * clipped, the USER_WIRED flag we set prevents
1617 * duplication so we do not have to do a
1620 entry = entry->next;
1624 * If we failed fall through to the unwiring section to
1625 * unwire what we had wired so far. 'end' has already
1632 * start_entry might have been clipped if we unlocked the
1633 * map and blocked. No matter how clipped it has gotten
1634 * there should be a fragment that is on our start boundary.
1636 CLIP_CHECK_BACK(start_entry, start);
1640 * Deal with the unwiring case.
1644 * This is the unwiring case. We must first ensure that the
1645 * range to be unwired is really wired down. We know there
1648 entry = start_entry;
1649 while ((entry != &map->header) && (entry->start < end)) {
1650 if ((entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
1651 rv = KERN_INVALID_ARGUMENT;
1654 KASSERT(entry->wired_count != 0, ("wired count was 0 with USER_WIRED set! %p", entry));
1655 entry = entry->next;
1659 * Now decrement the wiring count for each region. If a region
1660 * becomes completely unwired, unwire its physical pages and
1663 while ((entry != &map->header) && (entry->start < end)) {
1664 KASSERT(entry->eflags & MAP_ENTRY_USER_WIRED, ("expected USER_WIRED on entry %p", entry));
1665 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
1666 entry->wired_count--;
1667 if (entry->wired_count == 0)
1668 vm_fault_unwire(map, entry->start, entry->end);
1669 entry = entry->next;
1673 vm_map_unclip_range(map, start_entry, start, real_end,
1683 * Sets the pageability of the specified address
1684 * range in the target map. Regions specified
1685 * as not pageable require locked-down physical
1686 * memory and physical page maps.
1688 * The map must not be locked, but a reference
1689 * must remain to the map throughout the call.
1692 vm_map_pageable(map, start, real_end, new_pageable)
1695 vm_offset_t real_end;
1696 boolean_t new_pageable;
1698 vm_map_entry_t entry;
1699 vm_map_entry_t start_entry;
1701 int rv = KERN_SUCCESS;
1705 VM_MAP_RANGE_CHECK(map, start, real_end);
1708 start_entry = vm_map_clip_range(map, start, end, MAP_CLIP_NO_HOLES);
1709 if (start_entry == NULL) {
1711 return (KERN_INVALID_ADDRESS);
1713 if (new_pageable == 0) {
1717 * 1. Holding the write lock, we create any shadow or zero-fill
1718 * objects that need to be created. Then we clip each map
1719 * entry to the region to be wired and increment its wiring
1720 * count. We create objects before clipping the map entries
1721 * to avoid object proliferation.
1723 * 2. We downgrade to a read lock, and call vm_fault_wire to
1724 * fault in the pages for any newly wired area (wired_count is
1727 * Downgrading to a read lock for vm_fault_wire avoids a
1728 * possible deadlock with another process that may have faulted
1729 * on one of the pages to be wired (it would mark the page busy,
1730 * blocking us, then in turn block on the map lock that we
1731 * hold). Because of problems in the recursive lock package,
1732 * we cannot upgrade to a write lock in vm_map_lookup. Thus,
1733 * any actions that require the write lock must be done
1734 * beforehand. Because we keep the read lock on the map, the
1735 * copy-on-write status of the entries we modify here cannot
1739 entry = start_entry;
1740 while ((entry != &map->header) && (entry->start < end)) {
1742 * Trivial case if the entry is already wired
1744 if (entry->wired_count) {
1745 entry->wired_count++;
1746 entry = entry->next;
1751 * The entry is being newly wired, we have to setup
1752 * appropriate management structures. A shadow
1753 * object is required for a copy-on-write region,
1754 * or a normal object for a zero-fill region. We
1755 * do not have to do this for entries that point to sub
1756 * maps because we won't hold the lock on the sub map.
1758 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1759 int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY;
1761 ((entry->protection & VM_PROT_WRITE) != 0)) {
1763 vm_object_shadow(&entry->object.vm_object,
1765 atop(entry->end - entry->start));
1766 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
1767 } else if (entry->object.vm_object == NULL &&
1769 entry->object.vm_object =
1770 vm_object_allocate(OBJT_DEFAULT,
1771 atop(entry->end - entry->start));
1772 entry->offset = (vm_offset_t) 0;
1776 entry->wired_count++;
1777 entry = entry->next;
1785 * HACK HACK HACK HACK
1787 * Unlock the map to avoid deadlocks. The in-transit flag
1788 * protects us from most changes but note that
1789 * clipping may still occur. To prevent clipping from
1790 * occuring after the unlock, except for when we are
1791 * blocking in vm_fault_wire, we must run at splvm().
1792 * Otherwise our accesses to entry->start and entry->end
1793 * could be corrupted. We have to set splvm() prior to
1794 * unlocking so start_entry does not change out from
1795 * under us at the very beginning of the loop.
1797 * HACK HACK HACK HACK
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 vm_offset_t save_start = entry->start;
1812 vm_offset_t save_end = entry->end;
1814 if (entry->wired_count == 1)
1815 rv = vm_fault_wire(map, entry->start, entry->end);
1817 CLIP_CHECK_BACK(entry, save_start);
1819 KASSERT(entry->wired_count == 1, ("wired_count changed unexpectedly"));
1820 entry->wired_count = 0;
1821 if (entry->end == save_end)
1823 entry = entry->next;
1824 KASSERT(entry != &map->header, ("bad entry clip during backout"));
1829 CLIP_CHECK_FWD(entry, save_end);
1830 entry = entry->next;
1835 * relock. start_entry is still IN_TRANSITION and must
1836 * still exist, but may have been clipped (handled just
1842 * If a failure occured undo everything by falling through
1843 * to the unwiring code. 'end' has already been adjusted
1850 * start_entry might have been clipped if we unlocked the
1851 * map and blocked. No matter how clipped it has gotten
1852 * there should be a fragment that is on our start boundary.
1854 CLIP_CHECK_BACK(start_entry, start);
1859 * This is the unwiring case. We must first ensure that the
1860 * range to be unwired is really wired down. We know there
1863 entry = start_entry;
1864 while ((entry != &map->header) && (entry->start < end)) {
1865 if (entry->wired_count == 0) {
1866 rv = KERN_INVALID_ARGUMENT;
1869 entry = entry->next;
1873 * Now decrement the wiring count for each region. If a region
1874 * becomes completely unwired, unwire its physical pages and
1877 entry = start_entry;
1878 while ((entry != &map->header) && (entry->start < end)) {
1879 entry->wired_count--;
1880 if (entry->wired_count == 0)
1881 vm_fault_unwire(map, entry->start, entry->end);
1882 entry = entry->next;
1886 vm_map_unclip_range(map, start_entry, start, real_end,
1896 * Push any dirty cached pages in the address range to their pager.
1897 * If syncio is TRUE, dirty pages are written synchronously.
1898 * If invalidate is TRUE, any cached pages are freed as well.
1900 * Returns an error if any part of the specified range is not mapped.
1903 vm_map_clean(map, start, end, syncio, invalidate)
1908 boolean_t invalidate;
1910 vm_map_entry_t current;
1911 vm_map_entry_t entry;
1914 vm_ooffset_t offset;
1916 vm_map_lock_read(map);
1917 VM_MAP_RANGE_CHECK(map, start, end);
1918 if (!vm_map_lookup_entry(map, start, &entry)) {
1919 vm_map_unlock_read(map);
1920 return (KERN_INVALID_ADDRESS);
1923 * Make a first pass to check for holes.
1925 for (current = entry; current->start < end; current = current->next) {
1926 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1927 vm_map_unlock_read(map);
1928 return (KERN_INVALID_ARGUMENT);
1930 if (end > current->end &&
1931 (current->next == &map->header ||
1932 current->end != current->next->start)) {
1933 vm_map_unlock_read(map);
1934 return (KERN_INVALID_ADDRESS);
1939 pmap_remove(vm_map_pmap(map), start, end);
1941 * Make a second pass, cleaning/uncaching pages from the indicated
1944 for (current = entry; current->start < end; current = current->next) {
1945 offset = current->offset + (start - current->start);
1946 size = (end <= current->end ? end : current->end) - start;
1947 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1949 vm_map_entry_t tentry;
1952 smap = current->object.sub_map;
1953 vm_map_lock_read(smap);
1954 (void) vm_map_lookup_entry(smap, offset, &tentry);
1955 tsize = tentry->end - offset;
1958 object = tentry->object.vm_object;
1959 offset = tentry->offset + (offset - tentry->start);
1960 vm_map_unlock_read(smap);
1962 object = current->object.vm_object;
1965 * Note that there is absolutely no sense in writing out
1966 * anonymous objects, so we track down the vnode object
1968 * We invalidate (remove) all pages from the address space
1969 * anyway, for semantic correctness.
1971 * note: certain anonymous maps, such as MAP_NOSYNC maps,
1972 * may start out with a NULL object.
1974 while (object && object->backing_object) {
1975 object = object->backing_object;
1976 offset += object->backing_object_offset;
1977 if (object->size < OFF_TO_IDX( offset + size))
1978 size = IDX_TO_OFF(object->size) - offset;
1980 if (object && (object->type == OBJT_VNODE) &&
1981 (current->protection & VM_PROT_WRITE)) {
1983 * Flush pages if writing is allowed, invalidate them
1984 * if invalidation requested. Pages undergoing I/O
1985 * will be ignored by vm_object_page_remove().
1987 * We cannot lock the vnode and then wait for paging
1988 * to complete without deadlocking against vm_fault.
1989 * Instead we simply call vm_object_page_remove() and
1990 * allow it to block internally on a page-by-page
1991 * basis when it encounters pages undergoing async
1996 vm_object_reference(object);
1997 vn_lock(object->handle, LK_EXCLUSIVE | LK_RETRY, curproc);
1998 flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
1999 flags |= invalidate ? OBJPC_INVAL : 0;
2000 vm_object_page_clean(object,
2002 OFF_TO_IDX(offset + size + PAGE_MASK),
2004 VOP_UNLOCK(object->handle, 0, curproc);
2005 vm_object_deallocate(object);
2007 if (object && invalidate &&
2008 ((object->type == OBJT_VNODE) ||
2009 (object->type == OBJT_DEVICE))) {
2010 vm_object_reference(object);
2011 vm_object_page_remove(object,
2013 OFF_TO_IDX(offset + size + PAGE_MASK),
2015 vm_object_deallocate(object);
2020 vm_map_unlock_read(map);
2021 return (KERN_SUCCESS);
2025 * vm_map_entry_unwire: [ internal use only ]
2027 * Make the region specified by this entry pageable.
2029 * The map in question should be locked.
2030 * [This is the reason for this routine's existence.]
2033 vm_map_entry_unwire(map, entry)
2035 vm_map_entry_t entry;
2037 vm_fault_unwire(map, entry->start, entry->end);
2038 entry->wired_count = 0;
2042 * vm_map_entry_delete: [ internal use only ]
2044 * Deallocate the given entry from the target map.
2047 vm_map_entry_delete(map, entry)
2049 vm_map_entry_t entry;
2051 vm_map_entry_unlink(map, entry);
2052 map->size -= entry->end - entry->start;
2054 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
2055 vm_object_deallocate(entry->object.vm_object);
2058 vm_map_entry_dispose(map, entry);
2062 * vm_map_delete: [ internal use only ]
2064 * Deallocates the given address range from the target
2068 vm_map_delete(map, start, end)
2074 vm_map_entry_t entry;
2075 vm_map_entry_t first_entry;
2078 * Find the start of the region, and clip it
2082 if (!vm_map_lookup_entry(map, start, &first_entry))
2083 entry = first_entry->next;
2085 entry = first_entry;
2086 vm_map_clip_start(map, entry, start);
2088 * Fix the lookup hint now, rather than each time though the
2091 SAVE_HINT(map, entry->prev);
2095 * Save the free space hint
2098 if (entry == &map->header) {
2099 map->first_free = &map->header;
2100 } else if (map->first_free->start >= start) {
2101 map->first_free = entry->prev;
2105 * Step through all entries in this region
2108 while ((entry != &map->header) && (entry->start < end)) {
2109 vm_map_entry_t next;
2111 vm_pindex_t offidxstart, offidxend, count;
2114 * If we hit an in-transition entry we have to sleep and
2115 * retry. It's easier (and not really slower) to just retry
2116 * since this case occurs so rarely and the hint is already
2117 * pointing at the right place. We have to reset the
2118 * start offset so as not to accidently delete an entry
2119 * another process just created in vacated space.
2121 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
2122 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2123 start = entry->start;
2124 ++cnt.v_intrans_coll;
2125 ++cnt.v_intrans_wait;
2126 vm_map_transition_wait(map);
2129 vm_map_clip_end(map, entry, end);
2135 offidxstart = OFF_TO_IDX(entry->offset);
2136 count = OFF_TO_IDX(e - s);
2137 object = entry->object.vm_object;
2140 * Unwire before removing addresses from the pmap; otherwise,
2141 * unwiring will put the entries back in the pmap.
2143 if (entry->wired_count != 0) {
2144 vm_map_entry_unwire(map, entry);
2147 offidxend = offidxstart + count;
2149 if ((object == kernel_object) || (object == kmem_object)) {
2150 vm_object_page_remove(object, offidxstart, offidxend, FALSE);
2152 pmap_remove(map->pmap, s, e);
2153 if (object != NULL &&
2154 object->ref_count != 1 &&
2155 (object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING &&
2156 (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2157 vm_object_collapse(object);
2158 vm_object_page_remove(object, offidxstart, offidxend, FALSE);
2159 if (object->type == OBJT_SWAP) {
2160 swap_pager_freespace(object, offidxstart, count);
2162 if (offidxend >= object->size &&
2163 offidxstart < object->size) {
2164 object->size = offidxstart;
2170 * Delete the entry (which may delete the object) only after
2171 * removing all pmap entries pointing to its pages.
2172 * (Otherwise, its page frames may be reallocated, and any
2173 * modify bits will be set in the wrong object!)
2175 vm_map_entry_delete(map, entry);
2178 return (KERN_SUCCESS);
2184 * Remove the given address range from the target map.
2185 * This is the exported form of vm_map_delete.
2188 vm_map_remove(map, start, end)
2195 if (map == kmem_map || map == mb_map)
2199 VM_MAP_RANGE_CHECK(map, start, end);
2200 result = vm_map_delete(map, start, end);
2203 if (map == kmem_map || map == mb_map)
2210 * vm_map_check_protection:
2212 * Assert that the target map allows the specified
2213 * privilege on the entire address region given.
2214 * The entire region must be allocated.
2217 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
2218 vm_prot_t protection)
2220 vm_map_entry_t entry;
2221 vm_map_entry_t tmp_entry;
2223 if (!vm_map_lookup_entry(map, start, &tmp_entry)) {
2228 while (start < end) {
2229 if (entry == &map->header) {
2236 if (start < entry->start) {
2240 * Check protection associated with entry.
2243 if ((entry->protection & protection) != protection) {
2246 /* go to next entry */
2249 entry = entry->next;
2255 * Split the pages in a map entry into a new object. This affords
2256 * easier removal of unused pages, and keeps object inheritance from
2257 * being a negative impact on memory usage.
2261 vm_map_entry_t entry;
2264 vm_object_t orig_object, new_object, source;
2266 vm_pindex_t offidxstart, offidxend, idx;
2268 vm_ooffset_t offset;
2270 orig_object = entry->object.vm_object;
2271 if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
2273 if (orig_object->ref_count <= 1)
2276 offset = entry->offset;
2280 offidxstart = OFF_TO_IDX(offset);
2281 offidxend = offidxstart + OFF_TO_IDX(e - s);
2282 size = offidxend - offidxstart;
2284 new_object = vm_pager_allocate(orig_object->type,
2285 NULL, IDX_TO_OFF(size), VM_PROT_ALL, 0LL);
2286 if (new_object == NULL)
2289 source = orig_object->backing_object;
2290 if (source != NULL) {
2291 vm_object_reference(source); /* Referenced by new_object */
2292 LIST_INSERT_HEAD(&source->shadow_head,
2293 new_object, shadow_list);
2294 vm_object_clear_flag(source, OBJ_ONEMAPPING);
2295 new_object->backing_object_offset =
2296 orig_object->backing_object_offset + IDX_TO_OFF(offidxstart);
2297 new_object->backing_object = source;
2298 source->shadow_count++;
2299 source->generation++;
2302 for (idx = 0; idx < size; idx++) {
2306 m = vm_page_lookup(orig_object, offidxstart + idx);
2311 * We must wait for pending I/O to complete before we can
2314 * We do not have to VM_PROT_NONE the page as mappings should
2315 * not be changed by this operation.
2317 if (vm_page_sleep_busy(m, TRUE, "spltwt"))
2321 vm_page_rename(m, new_object, idx);
2322 /* page automatically made dirty by rename and cache handled */
2326 if (orig_object->type == OBJT_SWAP) {
2327 vm_object_pip_add(orig_object, 1);
2329 * copy orig_object pages into new_object
2330 * and destroy unneeded pages in
2333 swap_pager_copy(orig_object, new_object, offidxstart, 0);
2334 vm_object_pip_wakeup(orig_object);
2337 for (idx = 0; idx < size; idx++) {
2338 m = vm_page_lookup(new_object, idx);
2344 entry->object.vm_object = new_object;
2345 entry->offset = 0LL;
2346 vm_object_deallocate(orig_object);
2350 * vm_map_copy_entry:
2352 * Copies the contents of the source entry to the destination
2353 * entry. The entries *must* be aligned properly.
2356 vm_map_copy_entry(src_map, dst_map, src_entry, dst_entry)
2357 vm_map_t src_map, dst_map;
2358 vm_map_entry_t src_entry, dst_entry;
2360 vm_object_t src_object;
2362 if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
2365 if (src_entry->wired_count == 0) {
2368 * If the source entry is marked needs_copy, it is already
2371 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
2372 pmap_protect(src_map->pmap,
2375 src_entry->protection & ~VM_PROT_WRITE);
2379 * Make a copy of the object.
2381 if ((src_object = src_entry->object.vm_object) != NULL) {
2383 if ((src_object->handle == NULL) &&
2384 (src_object->type == OBJT_DEFAULT ||
2385 src_object->type == OBJT_SWAP)) {
2386 vm_object_collapse(src_object);
2387 if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
2388 vm_map_split(src_entry);
2389 src_object = src_entry->object.vm_object;
2393 vm_object_reference(src_object);
2394 vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
2395 dst_entry->object.vm_object = src_object;
2396 src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2397 dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2398 dst_entry->offset = src_entry->offset;
2400 dst_entry->object.vm_object = NULL;
2401 dst_entry->offset = 0;
2404 pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
2405 dst_entry->end - dst_entry->start, src_entry->start);
2408 * Of course, wired down pages can't be set copy-on-write.
2409 * Cause wired pages to be copied into the new map by
2410 * simulating faults (the new pages are pageable)
2412 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry);
2418 * Create a new process vmspace structure and vm_map
2419 * based on those of an existing process. The new map
2420 * is based on the old map, according to the inheritance
2421 * values on the regions in that map.
2423 * The source map must not be locked.
2427 struct vmspace *vm1;
2429 struct vmspace *vm2;
2430 vm_map_t old_map = &vm1->vm_map;
2432 vm_map_entry_t old_entry;
2433 vm_map_entry_t new_entry;
2436 vm_map_lock(old_map);
2437 old_map->infork = 1;
2439 vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
2440 bcopy(&vm1->vm_startcopy, &vm2->vm_startcopy,
2441 (caddr_t) (vm1 + 1) - (caddr_t) &vm1->vm_startcopy);
2442 new_map = &vm2->vm_map; /* XXX */
2443 new_map->timestamp = 1;
2445 old_entry = old_map->header.next;
2447 while (old_entry != &old_map->header) {
2448 if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2449 panic("vm_map_fork: encountered a submap");
2451 switch (old_entry->inheritance) {
2452 case VM_INHERIT_NONE:
2455 case VM_INHERIT_SHARE:
2457 * Clone the entry, creating the shared object if necessary.
2459 object = old_entry->object.vm_object;
2460 if (object == NULL) {
2461 object = vm_object_allocate(OBJT_DEFAULT,
2462 atop(old_entry->end - old_entry->start));
2463 old_entry->object.vm_object = object;
2464 old_entry->offset = (vm_offset_t) 0;
2468 * Add the reference before calling vm_object_shadow
2469 * to insure that a shadow object is created.
2471 vm_object_reference(object);
2472 if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
2473 vm_object_shadow(&old_entry->object.vm_object,
2475 atop(old_entry->end - old_entry->start));
2476 old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
2477 /* Transfer the second reference too. */
2478 vm_object_reference(
2479 old_entry->object.vm_object);
2480 vm_object_deallocate(object);
2481 object = old_entry->object.vm_object;
2483 vm_object_clear_flag(object, OBJ_ONEMAPPING);
2486 * Clone the entry, referencing the shared object.
2488 new_entry = vm_map_entry_create(new_map);
2489 *new_entry = *old_entry;
2490 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2491 new_entry->wired_count = 0;
2494 * Insert the entry into the new map -- we know we're
2495 * inserting at the end of the new map.
2498 vm_map_entry_link(new_map, new_map->header.prev,
2502 * Update the physical map
2505 pmap_copy(new_map->pmap, old_map->pmap,
2507 (old_entry->end - old_entry->start),
2511 case VM_INHERIT_COPY:
2513 * Clone the entry and link into the map.
2515 new_entry = vm_map_entry_create(new_map);
2516 *new_entry = *old_entry;
2517 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2518 new_entry->wired_count = 0;
2519 new_entry->object.vm_object = NULL;
2520 vm_map_entry_link(new_map, new_map->header.prev,
2522 vm_map_copy_entry(old_map, new_map, old_entry,
2526 old_entry = old_entry->next;
2529 new_map->size = old_map->size;
2530 old_map->infork = 0;
2531 vm_map_unlock(old_map);
2537 vm_map_stack (vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
2538 vm_prot_t prot, vm_prot_t max, int cow)
2540 vm_map_entry_t prev_entry;
2541 vm_map_entry_t new_stack_entry;
2542 vm_size_t init_ssize;
2545 if (VM_MIN_ADDRESS > 0 && addrbos < VM_MIN_ADDRESS)
2546 return (KERN_NO_SPACE);
2548 if (max_ssize < sgrowsiz)
2549 init_ssize = max_ssize;
2551 init_ssize = sgrowsiz;
2555 /* If addr is already mapped, no go */
2556 if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
2558 return (KERN_NO_SPACE);
2561 /* If we would blow our VMEM resource limit, no go */
2562 if (map->size + init_ssize >
2563 curproc->p_rlimit[RLIMIT_VMEM].rlim_cur) {
2565 return (KERN_NO_SPACE);
2568 /* If we can't accomodate max_ssize in the current mapping,
2569 * no go. However, we need to be aware that subsequent user
2570 * mappings might map into the space we have reserved for
2571 * stack, and currently this space is not protected.
2573 * Hopefully we will at least detect this condition
2574 * when we try to grow the stack.
2576 if ((prev_entry->next != &map->header) &&
2577 (prev_entry->next->start < addrbos + max_ssize)) {
2579 return (KERN_NO_SPACE);
2582 /* We initially map a stack of only init_ssize. We will
2583 * grow as needed later. Since this is to be a grow
2584 * down stack, we map at the top of the range.
2586 * Note: we would normally expect prot and max to be
2587 * VM_PROT_ALL, and cow to be 0. Possibly we should
2588 * eliminate these as input parameters, and just
2589 * pass these values here in the insert call.
2591 rv = vm_map_insert(map, NULL, 0, addrbos + max_ssize - init_ssize,
2592 addrbos + max_ssize, prot, max, cow);
2594 /* Now set the avail_ssize amount */
2595 if (rv == KERN_SUCCESS){
2596 if (prev_entry != &map->header)
2597 vm_map_clip_end(map, prev_entry, addrbos + max_ssize - init_ssize);
2598 new_stack_entry = prev_entry->next;
2599 if (new_stack_entry->end != addrbos + max_ssize ||
2600 new_stack_entry->start != addrbos + max_ssize - init_ssize)
2601 panic ("Bad entry start/end for new stack entry");
2603 new_stack_entry->avail_ssize = max_ssize - init_ssize;
2610 /* Attempts to grow a vm stack entry. Returns KERN_SUCCESS if the
2611 * desired address is already mapped, or if we successfully grow
2612 * the stack. Also returns KERN_SUCCESS if addr is outside the
2613 * stack range (this is strange, but preserves compatibility with
2614 * the grow function in vm_machdep.c).
2617 vm_map_growstack (struct proc *p, vm_offset_t addr)
2619 vm_map_entry_t prev_entry;
2620 vm_map_entry_t stack_entry;
2621 vm_map_entry_t new_stack_entry;
2622 struct vmspace *vm = p->p_vmspace;
2623 vm_map_t map = &vm->vm_map;
2626 int rv = KERN_SUCCESS;
2628 int use_read_lock = 1;
2632 vm_map_lock_read(map);
2636 /* If addr is already in the entry range, no need to grow.*/
2637 if (vm_map_lookup_entry(map, addr, &prev_entry))
2640 if ((stack_entry = prev_entry->next) == &map->header)
2642 if (prev_entry == &map->header)
2643 end = stack_entry->start - stack_entry->avail_ssize;
2645 end = prev_entry->end;
2647 /* This next test mimics the old grow function in vm_machdep.c.
2648 * It really doesn't quite make sense, but we do it anyway
2649 * for compatibility.
2651 * If not growable stack, return success. This signals the
2652 * caller to proceed as he would normally with normal vm.
2654 if (stack_entry->avail_ssize < 1 ||
2655 addr >= stack_entry->start ||
2656 addr < stack_entry->start - stack_entry->avail_ssize) {
2660 /* Find the minimum grow amount */
2661 grow_amount = roundup (stack_entry->start - addr, PAGE_SIZE);
2662 if (grow_amount > stack_entry->avail_ssize) {
2667 /* If there is no longer enough space between the entries
2668 * nogo, and adjust the available space. Note: this
2669 * should only happen if the user has mapped into the
2670 * stack area after the stack was created, and is
2671 * probably an error.
2673 * This also effectively destroys any guard page the user
2674 * might have intended by limiting the stack size.
2676 if (grow_amount > stack_entry->start - end) {
2677 if (use_read_lock && vm_map_lock_upgrade(map)) {
2682 stack_entry->avail_ssize = stack_entry->start - end;
2687 is_procstack = addr >= (vm_offset_t)vm->vm_maxsaddr;
2689 /* If this is the main process stack, see if we're over the
2692 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount >
2693 p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
2698 /* Round up the grow amount modulo SGROWSIZ */
2699 grow_amount = roundup (grow_amount, sgrowsiz);
2700 if (grow_amount > stack_entry->avail_ssize) {
2701 grow_amount = stack_entry->avail_ssize;
2703 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount >
2704 p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
2705 grow_amount = p->p_rlimit[RLIMIT_STACK].rlim_cur -
2709 /* If we would blow our VMEM resource limit, no go */
2710 if (map->size + grow_amount >
2711 curproc->p_rlimit[RLIMIT_VMEM].rlim_cur) {
2716 if (use_read_lock && vm_map_lock_upgrade(map)) {
2722 /* Get the preliminary new entry start value */
2723 addr = stack_entry->start - grow_amount;
2725 /* If this puts us into the previous entry, cut back our growth
2726 * to the available space. Also, see the note above.
2729 stack_entry->avail_ssize = stack_entry->start - end;
2733 rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start,
2738 /* Adjust the available stack space by the amount we grew. */
2739 if (rv == KERN_SUCCESS) {
2740 if (prev_entry != &map->header)
2741 vm_map_clip_end(map, prev_entry, addr);
2742 new_stack_entry = prev_entry->next;
2743 if (new_stack_entry->end != stack_entry->start ||
2744 new_stack_entry->start != addr)
2745 panic ("Bad stack grow start/end in new stack entry");
2747 new_stack_entry->avail_ssize = stack_entry->avail_ssize -
2748 (new_stack_entry->end -
2749 new_stack_entry->start);
2751 vm->vm_ssize += btoc(new_stack_entry->end -
2752 new_stack_entry->start);
2758 vm_map_unlock_read(map);
2765 * Unshare the specified VM space for exec. If other processes are
2766 * mapped to it, then create a new one. The new vmspace is null.
2770 vmspace_exec(struct proc *p) {
2771 struct vmspace *oldvmspace = p->p_vmspace;
2772 struct vmspace *newvmspace;
2773 vm_map_t map = &p->p_vmspace->vm_map;
2775 newvmspace = vmspace_alloc(map->min_offset, map->max_offset);
2776 bcopy(&oldvmspace->vm_startcopy, &newvmspace->vm_startcopy,
2777 (caddr_t) (newvmspace + 1) - (caddr_t) &newvmspace->vm_startcopy);
2779 * This code is written like this for prototype purposes. The
2780 * goal is to avoid running down the vmspace here, but let the
2781 * other process's that are still using the vmspace to finally
2782 * run it down. Even though there is little or no chance of blocking
2783 * here, it is a good idea to keep this form for future mods.
2785 vmspace_free(oldvmspace);
2786 p->p_vmspace = newvmspace;
2787 pmap_pinit2(vmspace_pmap(newvmspace));
2793 * Unshare the specified VM space for forcing COW. This
2794 * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
2798 vmspace_unshare(struct proc *p) {
2799 struct vmspace *oldvmspace = p->p_vmspace;
2800 struct vmspace *newvmspace;
2802 if (oldvmspace->vm_refcnt == 1)
2804 newvmspace = vmspace_fork(oldvmspace);
2805 vmspace_free(oldvmspace);
2806 p->p_vmspace = newvmspace;
2807 pmap_pinit2(vmspace_pmap(newvmspace));
2816 * Finds the VM object, offset, and
2817 * protection for a given virtual address in the
2818 * specified map, assuming a page fault of the
2821 * Leaves the map in question locked for read; return
2822 * values are guaranteed until a vm_map_lookup_done
2823 * call is performed. Note that the map argument
2824 * is in/out; the returned map must be used in
2825 * the call to vm_map_lookup_done.
2827 * A handle (out_entry) is returned for use in
2828 * vm_map_lookup_done, to make that fast.
2830 * If a lookup is requested with "write protection"
2831 * specified, the map may be changed to perform virtual
2832 * copying operations, although the data referenced will
2836 vm_map_lookup(vm_map_t *var_map, /* IN/OUT */
2838 vm_prot_t fault_typea,
2839 vm_map_entry_t *out_entry, /* OUT */
2840 vm_object_t *object, /* OUT */
2841 vm_pindex_t *pindex, /* OUT */
2842 vm_prot_t *out_prot, /* OUT */
2843 boolean_t *wired) /* OUT */
2845 vm_map_entry_t entry;
2846 vm_map_t map = *var_map;
2848 vm_prot_t fault_type = fault_typea;
2849 int use_read_lock = 1;
2850 int rv = KERN_SUCCESS;
2854 vm_map_lock_read(map);
2859 * If the map has an interesting hint, try it before calling full
2860 * blown lookup routine.
2865 if ((entry == &map->header) ||
2866 (vaddr < entry->start) || (vaddr >= entry->end)) {
2867 vm_map_entry_t tmp_entry;
2870 * Entry was either not a valid hint, or the vaddr was not
2871 * contained in the entry, so do a full lookup.
2873 if (!vm_map_lookup_entry(map, vaddr, &tmp_entry)) {
2874 rv = KERN_INVALID_ADDRESS;
2886 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
2887 vm_map_t old_map = map;
2889 *var_map = map = entry->object.sub_map;
2891 vm_map_unlock_read(old_map);
2893 vm_map_unlock(old_map);
2899 * Check whether this task is allowed to have this page.
2900 * Note the special case for MAP_ENTRY_COW
2901 * pages with an override. This is to implement a forced
2902 * COW for debuggers.
2905 if (fault_type & VM_PROT_OVERRIDE_WRITE)
2906 prot = entry->max_protection;
2908 prot = entry->protection;
2910 fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
2911 if ((fault_type & prot) != fault_type) {
2912 rv = KERN_PROTECTION_FAILURE;
2916 if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
2917 (entry->eflags & MAP_ENTRY_COW) &&
2918 (fault_type & VM_PROT_WRITE) &&
2919 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
2920 rv = KERN_PROTECTION_FAILURE;
2925 * If this page is not pageable, we have to get it for all possible
2929 *wired = (entry->wired_count != 0);
2931 prot = fault_type = entry->protection;
2934 * If the entry was copy-on-write, we either ...
2937 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
2939 * If we want to write the page, we may as well handle that
2940 * now since we've got the map locked.
2942 * If we don't need to write the page, we just demote the
2943 * permissions allowed.
2946 if (fault_type & VM_PROT_WRITE) {
2948 * Make a new object, and place it in the object
2949 * chain. Note that no new references have appeared
2950 * -- one just moved from the map to the new
2954 if (use_read_lock && vm_map_lock_upgrade(map)) {
2961 &entry->object.vm_object,
2963 atop(entry->end - entry->start));
2965 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
2968 * We're attempting to read a copy-on-write page --
2969 * don't allow writes.
2972 prot &= ~VM_PROT_WRITE;
2977 * Create an object if necessary.
2979 if (entry->object.vm_object == NULL &&
2981 if (use_read_lock && vm_map_lock_upgrade(map)) {
2986 entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
2987 atop(entry->end - entry->start));
2992 * Return the object/offset from this entry. If the entry was
2993 * copy-on-write or empty, it has been fixed up.
2996 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
2997 *object = entry->object.vm_object;
3000 * Return whether this is the only map sharing this data. On
3001 * success we return with a read lock held on the map. On failure
3002 * we return with the map unlocked.
3006 if (rv == KERN_SUCCESS) {
3007 if (use_read_lock == 0)
3008 vm_map_lock_downgrade(map);
3009 } else if (use_read_lock) {
3010 vm_map_unlock_read(map);
3018 * vm_map_lookup_done:
3020 * Releases locks acquired by a vm_map_lookup
3021 * (according to the handle returned by that lookup).
3025 vm_map_lookup_done(map, entry)
3027 vm_map_entry_t entry;
3030 * Unlock the main-level map
3033 vm_map_unlock_read(map);
3037 * Implement uiomove with VM operations. This handles (and collateral changes)
3038 * support every combination of source object modification, and COW type
3042 vm_uiomove(mapa, srcobject, cp, cnta, uaddra, npages)
3044 vm_object_t srcobject;
3051 vm_object_t first_object, oldobject, object;
3052 vm_map_entry_t entry;
3056 vm_offset_t uaddr, start, end, tend;
3057 vm_pindex_t first_pindex, osize, oindex;
3070 if ((vm_map_lookup(&map, uaddr,
3071 VM_PROT_READ, &entry, &first_object,
3072 &first_pindex, &prot, &wired)) != KERN_SUCCESS) {
3076 vm_map_clip_start(map, entry, uaddr);
3079 tend = uaddr + tcnt;
3080 if (tend > entry->end) {
3081 tcnt = entry->end - uaddr;
3085 vm_map_clip_end(map, entry, tend);
3087 start = entry->start;
3092 oindex = OFF_TO_IDX(cp);
3095 for (idx = 0; idx < osize; idx++) {
3097 if ((m = vm_page_lookup(srcobject, oindex + idx)) == NULL) {
3098 vm_map_lookup_done(map, entry);
3102 * disallow busy or invalid pages, but allow
3103 * m->busy pages if they are entirely valid.
3105 if ((m->flags & PG_BUSY) ||
3106 ((m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL)) {
3107 vm_map_lookup_done(map, entry);
3114 * If we are changing an existing map entry, just redirect
3115 * the object, and change mappings.
3117 if ((first_object->type == OBJT_VNODE) &&
3118 ((oldobject = entry->object.vm_object) == first_object)) {
3120 if ((entry->offset != cp) || (oldobject != srcobject)) {
3122 * Remove old window into the file
3124 pmap_remove (map->pmap, uaddr, tend);
3127 * Force copy on write for mmaped regions
3129 vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
3132 * Point the object appropriately
3134 if (oldobject != srcobject) {
3137 * Set the object optimization hint flag
3139 vm_object_set_flag(srcobject, OBJ_OPT);
3140 vm_object_reference(srcobject);
3141 entry->object.vm_object = srcobject;
3144 vm_object_deallocate(oldobject);
3151 pmap_remove (map->pmap, uaddr, tend);
3154 } else if ((first_object->ref_count == 1) &&
3155 (first_object->size == osize) &&
3156 ((first_object->type == OBJT_DEFAULT) ||
3157 (first_object->type == OBJT_SWAP)) ) {
3159 oldobject = first_object->backing_object;
3161 if ((first_object->backing_object_offset != cp) ||
3162 (oldobject != srcobject)) {
3164 * Remove old window into the file
3166 pmap_remove (map->pmap, uaddr, tend);
3169 * Remove unneeded old pages
3171 vm_object_page_remove(first_object, 0, 0, 0);
3174 * Invalidate swap space
3176 if (first_object->type == OBJT_SWAP) {
3177 swap_pager_freespace(first_object,
3179 first_object->size);
3183 * Force copy on write for mmaped regions
3185 vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
3188 * Point the object appropriately
3190 if (oldobject != srcobject) {
3193 * Set the object optimization hint flag
3195 vm_object_set_flag(srcobject, OBJ_OPT);
3196 vm_object_reference(srcobject);
3200 first_object, shadow_list);
3201 oldobject->shadow_count--;
3202 /* XXX bump generation? */
3203 vm_object_deallocate(oldobject);
3206 LIST_INSERT_HEAD(&srcobject->shadow_head,
3207 first_object, shadow_list);
3208 srcobject->shadow_count++;
3209 /* XXX bump generation? */
3211 first_object->backing_object = srcobject;
3213 first_object->backing_object_offset = cp;
3216 pmap_remove (map->pmap, uaddr, tend);
3219 * Otherwise, we have to do a logical mmap.
3223 vm_object_set_flag(srcobject, OBJ_OPT);
3224 vm_object_reference(srcobject);
3226 pmap_remove (map->pmap, uaddr, tend);
3228 vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
3229 vm_map_lock_upgrade(map);
3231 if (entry == &map->header) {
3232 map->first_free = &map->header;
3233 } else if (map->first_free->start >= start) {
3234 map->first_free = entry->prev;
3237 SAVE_HINT(map, entry->prev);
3238 vm_map_entry_delete(map, entry);
3243 rv = vm_map_insert(map, object, ooffset, start, tend,
3244 VM_PROT_ALL, VM_PROT_ALL, MAP_COPY_ON_WRITE);
3246 if (rv != KERN_SUCCESS)
3247 panic("vm_uiomove: could not insert new entry: %d", rv);
3251 * Map the window directly, if it is already in memory
3253 pmap_object_init_pt(map->pmap, uaddr,
3254 srcobject, oindex, tcnt, 0);
3269 * Performs the copy_on_write operations necessary to allow the virtual copies
3270 * into user space to work. This has to be called for write(2) system calls
3271 * from other processes, file unlinking, and file size shrinkage.
3274 vm_freeze_copyopts(object, froma, toa)
3276 vm_pindex_t froma, toa;
3279 vm_object_t robject;
3282 if ((object == NULL) ||
3283 ((object->flags & OBJ_OPT) == 0))
3286 if (object->shadow_count > object->ref_count)
3287 panic("vm_freeze_copyopts: sc > rc");
3289 while((robject = LIST_FIRST(&object->shadow_head)) != NULL) {
3290 vm_pindex_t bo_pindex;
3291 vm_page_t m_in, m_out;
3293 bo_pindex = OFF_TO_IDX(robject->backing_object_offset);
3295 vm_object_reference(robject);
3297 vm_object_pip_wait(robject, "objfrz");
3299 if (robject->ref_count == 1) {
3300 vm_object_deallocate(robject);
3304 vm_object_pip_add(robject, 1);
3306 for (idx = 0; idx < robject->size; idx++) {
3308 m_out = vm_page_grab(robject, idx,
3309 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
3311 if (m_out->valid == 0) {
3312 m_in = vm_page_grab(object, bo_pindex + idx,
3313 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
3314 if (m_in->valid == 0) {
3315 rv = vm_pager_get_pages(object, &m_in, 1, 0);
3316 if (rv != VM_PAGER_OK) {
3317 printf("vm_freeze_copyopts: cannot read page from file: %lx\n", (long)m_in->pindex);
3320 vm_page_deactivate(m_in);
3323 vm_page_protect(m_in, VM_PROT_NONE);
3324 pmap_copy_page(VM_PAGE_TO_PHYS(m_in), VM_PAGE_TO_PHYS(m_out));
3325 m_out->valid = m_in->valid;
3326 vm_page_dirty(m_out);
3327 vm_page_activate(m_out);
3328 vm_page_wakeup(m_in);
3330 vm_page_wakeup(m_out);
3333 object->shadow_count--;
3334 object->ref_count--;
3335 LIST_REMOVE(robject, shadow_list);
3336 robject->backing_object = NULL;
3337 robject->backing_object_offset = 0;
3339 vm_object_pip_wakeup(robject);
3340 vm_object_deallocate(robject);
3343 vm_object_clear_flag(object, OBJ_OPT);
3346 #include "opt_ddb.h"
3348 #include <sys/kernel.h>
3350 #include <ddb/ddb.h>
3353 * vm_map_print: [ debug ]
3355 DB_SHOW_COMMAND(map, vm_map_print)
3358 /* XXX convert args. */
3359 vm_map_t map = (vm_map_t)addr;
3360 boolean_t full = have_addr;
3362 vm_map_entry_t entry;
3364 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
3366 (void *)map->pmap, map->nentries, map->timestamp);
3369 if (!full && db_indent)
3373 for (entry = map->header.next; entry != &map->header;
3374 entry = entry->next) {
3375 db_iprintf("map entry %p: start=%p, end=%p\n",
3376 (void *)entry, (void *)entry->start, (void *)entry->end);
3379 static char *inheritance_name[4] =
3380 {"share", "copy", "none", "donate_copy"};
3382 db_iprintf(" prot=%x/%x/%s",
3384 entry->max_protection,
3385 inheritance_name[(int)(unsigned char)entry->inheritance]);
3386 if (entry->wired_count != 0)
3387 db_printf(", wired");
3389 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3390 /* XXX no %qd in kernel. Truncate entry->offset. */
3391 db_printf(", share=%p, offset=0x%lx\n",
3392 (void *)entry->object.sub_map,
3393 (long)entry->offset);
3395 if ((entry->prev == &map->header) ||
3396 (entry->prev->object.sub_map !=
3397 entry->object.sub_map)) {
3399 vm_map_print((db_expr_t)(intptr_t)
3400 entry->object.sub_map,
3401 full, 0, (char *)0);
3405 /* XXX no %qd in kernel. Truncate entry->offset. */
3406 db_printf(", object=%p, offset=0x%lx",
3407 (void *)entry->object.vm_object,
3408 (long)entry->offset);
3409 if (entry->eflags & MAP_ENTRY_COW)
3410 db_printf(", copy (%s)",
3411 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
3415 if ((entry->prev == &map->header) ||
3416 (entry->prev->object.vm_object !=
3417 entry->object.vm_object)) {
3419 vm_object_print((db_expr_t)(intptr_t)
3420 entry->object.vm_object,
3421 full, 0, (char *)0);
3433 DB_SHOW_COMMAND(procvm, procvm)
3438 p = (struct proc *) addr;
3443 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
3444 (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
3445 (void *)vmspace_pmap(p->p_vmspace));
3447 vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);