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.56 2007/04/29 18:25:41 dillon Exp $
69 * Virtual memory mapping module.
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/kernel.h>
77 #include <sys/vmmeter.h>
79 #include <sys/vnode.h>
80 #include <sys/resourcevar.h>
83 #include <sys/malloc.h>
86 #include <vm/vm_param.h>
88 #include <vm/vm_map.h>
89 #include <vm/vm_page.h>
90 #include <vm/vm_object.h>
91 #include <vm/vm_pager.h>
92 #include <vm/vm_kern.h>
93 #include <vm/vm_extern.h>
94 #include <vm/swap_pager.h>
95 #include <vm/vm_zone.h>
97 #include <sys/thread2.h>
98 #include <sys/sysref2.h>
101 * Virtual memory maps provide for the mapping, protection,
102 * and sharing of virtual memory objects. In addition,
103 * this module provides for an efficient virtual copy of
104 * memory from one map to another.
106 * Synchronization is required prior to most operations.
108 * Maps consist of an ordered doubly-linked list of simple
109 * entries; a single hint is used to speed up lookups.
111 * Since portions of maps are specified by start/end addresses,
112 * which may not align with existing map entries, all
113 * routines merely "clip" entries to these start/end values.
114 * [That is, an entry is split into two, bordering at a
115 * start or end value.] Note that these clippings may not
116 * always be necessary (as the two resulting entries are then
117 * not changed); however, the clipping is done for convenience.
119 * As mentioned above, virtual copy operations are performed
120 * by copying VM object references from one map to
121 * another, and then marking both regions as copy-on-write.
124 static void vmspace_terminate(struct vmspace
*vm
);
125 static void vmspace_lock(struct vmspace
*vm
);
126 static void vmspace_unlock(struct vmspace
*vm
);
127 static void vmspace_dtor(void *obj
, void *private);
129 MALLOC_DEFINE(M_VMSPACE
, "vmspace", "vmspace objcache backingstore");
131 struct sysref_class vmspace_sysref_class
= {
134 .proto
= SYSREF_PROTO_VMSPACE
,
135 .offset
= offsetof(struct vmspace
, vm_sysref
),
136 .objsize
= sizeof(struct vmspace
),
138 .flags
= SRC_MANAGEDINIT
,
139 .dtor
= vmspace_dtor
,
141 .terminate
= (sysref_terminate_func_t
)vmspace_terminate
,
142 .lock
= (sysref_lock_func_t
)vmspace_lock
,
143 .unlock
= (sysref_lock_func_t
)vmspace_unlock
149 static struct vm_zone mapentzone_store
, mapzone_store
;
150 static vm_zone_t mapentzone
, mapzone
;
151 static struct vm_object mapentobj
, mapobj
;
153 static struct vm_map_entry map_entry_init
[MAX_MAPENT
];
154 static struct vm_map_entry cpu_map_entry_init
[MAXCPU
][VMEPERCPU
];
155 static struct vm_map map_init
[MAX_KMAP
];
157 static void vm_map_entry_shadow(vm_map_entry_t entry
);
158 static vm_map_entry_t
vm_map_entry_create(vm_map_t map
, int *);
159 static void vm_map_entry_dispose (vm_map_t map
, vm_map_entry_t entry
, int *);
160 static void _vm_map_clip_end (vm_map_t
, vm_map_entry_t
, vm_offset_t
, int *);
161 static void _vm_map_clip_start (vm_map_t
, vm_map_entry_t
, vm_offset_t
, int *);
162 static void vm_map_entry_delete (vm_map_t
, vm_map_entry_t
, int *);
163 static void vm_map_entry_unwire (vm_map_t
, vm_map_entry_t
);
164 static void vm_map_copy_entry (vm_map_t
, vm_map_t
, vm_map_entry_t
,
166 static void vm_map_split (vm_map_entry_t
);
167 static void vm_map_unclip_range (vm_map_t map
, vm_map_entry_t start_entry
, vm_offset_t start
, vm_offset_t end
, int *count
, int flags
);
172 * Initialize the vm_map module. Must be called before
173 * any other vm_map routines.
175 * Map and entry structures are allocated from the general
176 * purpose memory pool with some exceptions:
178 * - The kernel map and kmem submap are allocated statically.
179 * - Kernel map entries are allocated out of a static pool.
181 * These restrictions are necessary since malloc() uses the
182 * maps and requires map entries.
187 mapzone
= &mapzone_store
;
188 zbootinit(mapzone
, "MAP", sizeof (struct vm_map
),
190 mapentzone
= &mapentzone_store
;
191 zbootinit(mapentzone
, "MAP ENTRY", sizeof (struct vm_map_entry
),
192 map_entry_init
, MAX_MAPENT
);
196 * vm_init2 - called prior to any vmspace allocations
201 zinitna(mapentzone
, &mapentobj
, NULL
, 0, 0,
202 ZONE_USE_RESERVE
| ZONE_SPECIAL
, 1);
203 zinitna(mapzone
, &mapobj
, NULL
, 0, 0, 0, 1);
210 * Red black tree functions
212 static int rb_vm_map_compare(vm_map_entry_t a
, vm_map_entry_t b
);
213 RB_GENERATE(vm_map_rb_tree
, vm_map_entry
, rb_entry
, rb_vm_map_compare
);
215 /* a->start is address, and the only field has to be initialized */
217 rb_vm_map_compare(vm_map_entry_t a
, vm_map_entry_t b
)
219 if (a
->start
< b
->start
)
221 else if (a
->start
> b
->start
)
227 * Allocate a vmspace structure, including a vm_map and pmap.
228 * Initialize numerous fields. While the initial allocation is zerod,
229 * subsequence reuse from the objcache leaves elements of the structure
230 * intact (particularly the pmap), so portions must be zerod.
232 * The structure is not considered activated until we call sysref_activate().
235 vmspace_alloc(vm_offset_t min
, vm_offset_t max
)
239 vm
= sysref_alloc(&vmspace_sysref_class
);
240 bzero(&vm
->vm_startcopy
,
241 (char *)&vm
->vm_endcopy
- (char *)&vm
->vm_startcopy
);
242 vm_map_init(&vm
->vm_map
, min
, max
, NULL
);
243 pmap_pinit(vmspace_pmap(vm
)); /* (some fields reused) */
244 vm
->vm_map
.pmap
= vmspace_pmap(vm
); /* XXX */
246 vm
->vm_exitingcnt
= 0;
247 cpu_vmspace_alloc(vm
);
248 sysref_activate(&vm
->vm_sysref
);
253 * dtor function - Some elements of the pmap are retained in the
254 * free-cached vmspaces to improve performance. We have to clean them up
255 * here before returning the vmspace to the memory pool.
258 vmspace_dtor(void *obj
, void *private)
260 struct vmspace
*vm
= obj
;
262 pmap_puninit(vmspace_pmap(vm
));
266 * Called in two cases:
268 * (1) When the last sysref is dropped, but exitingcnt might still be
271 * (2) When there are no sysrefs (i.e. refcnt is negative) left and the
272 * exitingcnt becomes zero
274 * sysref will not scrap the object until we call sysref_put() once more
275 * after the last ref has been dropped.
278 vmspace_terminate(struct vmspace
*vm
)
283 * If exitingcnt is non-zero we can't get rid of the entire vmspace
284 * yet, but we can scrap user memory.
286 if (vm
->vm_exitingcnt
) {
288 pmap_remove_pages(vmspace_pmap(vm
), VM_MIN_USER_ADDRESS
,
289 VM_MAX_USER_ADDRESS
);
290 vm_map_remove(&vm
->vm_map
, VM_MIN_USER_ADDRESS
,
291 VM_MAX_USER_ADDRESS
);
295 cpu_vmspace_free(vm
);
298 * Make sure any SysV shm is freed, it might not have in
303 KKASSERT(vm
->vm_upcalls
== NULL
);
306 * Lock the map, to wait out all other references to it.
307 * Delete all of the mappings and pages they hold, then call
308 * the pmap module to reclaim anything left.
310 count
= vm_map_entry_reserve(MAP_RESERVE_COUNT
);
311 vm_map_lock(&vm
->vm_map
);
312 vm_map_delete(&vm
->vm_map
, vm
->vm_map
.min_offset
,
313 vm
->vm_map
.max_offset
, &count
);
314 vm_map_unlock(&vm
->vm_map
);
315 vm_map_entry_release(count
);
317 pmap_release(vmspace_pmap(vm
));
318 sysref_put(&vm
->vm_sysref
);
322 vmspace_lock(struct vmspace
*vm __unused
)
327 vmspace_unlock(struct vmspace
*vm __unused
)
332 * This is called in the wait*() handling code. The vmspace can be terminated
333 * after the last wait is finished using it.
336 vmspace_exitfree(struct proc
*p
)
343 if (--vm
->vm_exitingcnt
== 0 && sysref_isinactive(&vm
->vm_sysref
))
344 vmspace_terminate(vm
);
348 * vmspace_swap_count()
350 * Swap useage is determined by taking the proportional swap used by
351 * VM objects backing the VM map. To make up for fractional losses,
352 * if the VM object has any swap use at all the associated map entries
353 * count for at least 1 swap page.
356 vmspace_swap_count(struct vmspace
*vmspace
)
358 vm_map_t map
= &vmspace
->vm_map
;
364 for (cur
= map
->header
.next
; cur
!= &map
->header
; cur
= cur
->next
) {
365 switch(cur
->maptype
) {
366 case VM_MAPTYPE_NORMAL
:
367 case VM_MAPTYPE_VPAGETABLE
:
368 if ((object
= cur
->object
.vm_object
) == NULL
)
370 if (object
->swblock_count
) {
371 n
= (cur
->end
- cur
->start
) / PAGE_SIZE
;
372 count
+= object
->swblock_count
*
373 SWAP_META_PAGES
* n
/ object
->size
+ 1;
384 * vmspace_anonymous_count()
386 * Calculate the approximate number of anonymous pages in use by
387 * this vmspace. To make up for fractional losses, we count each
388 * VM object as having at least 1 anonymous page.
391 vmspace_anonymous_count(struct vmspace
*vmspace
)
393 vm_map_t map
= &vmspace
->vm_map
;
398 for (cur
= map
->header
.next
; cur
!= &map
->header
; cur
= cur
->next
) {
399 switch(cur
->maptype
) {
400 case VM_MAPTYPE_NORMAL
:
401 case VM_MAPTYPE_VPAGETABLE
:
402 if ((object
= cur
->object
.vm_object
) == NULL
)
404 if (object
->type
!= OBJT_DEFAULT
&&
405 object
->type
!= OBJT_SWAP
) {
408 count
+= object
->resident_page_count
;
423 * Creates and returns a new empty VM map with
424 * the given physical map structure, and having
425 * the given lower and upper address bounds.
428 vm_map_create(vm_map_t result
, pmap_t pmap
, vm_offset_t min
, vm_offset_t max
)
431 result
= zalloc(mapzone
);
432 vm_map_init(result
, min
, max
, pmap
);
437 * Initialize an existing vm_map structure
438 * such as that in the vmspace structure.
439 * The pmap is set elsewhere.
442 vm_map_init(struct vm_map
*map
, vm_offset_t min
, vm_offset_t max
, pmap_t pmap
)
444 map
->header
.next
= map
->header
.prev
= &map
->header
;
445 RB_INIT(&map
->rb_root
);
450 map
->min_offset
= min
;
451 map
->max_offset
= max
;
453 map
->first_free
= &map
->header
;
454 map
->hint
= &map
->header
;
456 lockinit(&map
->lock
, "thrd_sleep", 0, 0);
460 * Shadow the vm_map_entry's object. This typically needs to be done when
461 * a write fault is taken on an entry which had previously been cloned by
462 * fork(). The shared object (which might be NULL) must become private so
463 * we add a shadow layer above it.
465 * Object allocation for anonymous mappings is defered as long as possible.
466 * When creating a shadow, however, the underlying object must be instantiated
467 * so it can be shared.
469 * If the map segment is governed by a virtual page table then it is
470 * possible to address offsets beyond the mapped area. Just allocate
471 * a maximally sized object for this case.
475 vm_map_entry_shadow(vm_map_entry_t entry
)
477 if (entry
->maptype
== VM_MAPTYPE_VPAGETABLE
) {
478 vm_object_shadow(&entry
->object
.vm_object
, &entry
->offset
,
479 0x7FFFFFFF); /* XXX */
481 vm_object_shadow(&entry
->object
.vm_object
, &entry
->offset
,
482 atop(entry
->end
- entry
->start
));
484 entry
->eflags
&= ~MAP_ENTRY_NEEDS_COPY
;
488 * Allocate an object for a vm_map_entry.
490 * Object allocation for anonymous mappings is defered as long as possible.
491 * This function is called when we can defer no longer, generally when a map
492 * entry might be split or forked or takes a page fault.
494 * If the map segment is governed by a virtual page table then it is
495 * possible to address offsets beyond the mapped area. Just allocate
496 * a maximally sized object for this case.
499 vm_map_entry_allocate_object(vm_map_entry_t entry
)
503 if (entry
->maptype
== VM_MAPTYPE_VPAGETABLE
) {
504 obj
= vm_object_allocate(OBJT_DEFAULT
, 0x7FFFFFFF); /* XXX */
506 obj
= vm_object_allocate(OBJT_DEFAULT
,
507 atop(entry
->end
- entry
->start
));
509 entry
->object
.vm_object
= obj
;
514 * vm_map_entry_reserve_cpu_init:
516 * Set an initial negative count so the first attempt to reserve
517 * space preloads a bunch of vm_map_entry's for this cpu. Also
518 * pre-allocate 2 vm_map_entries which will be needed by zalloc() to
519 * map a new page for vm_map_entry structures. SMP systems are
520 * particularly sensitive.
522 * This routine is called in early boot so we cannot just call
523 * vm_map_entry_reserve().
525 * May be called for a gd other then mycpu, but may only be called
529 vm_map_entry_reserve_cpu_init(globaldata_t gd
)
531 vm_map_entry_t entry
;
534 gd
->gd_vme_avail
-= MAP_RESERVE_COUNT
* 2;
535 entry
= &cpu_map_entry_init
[gd
->gd_cpuid
][0];
536 for (i
= 0; i
< VMEPERCPU
; ++i
, ++entry
) {
537 entry
->next
= gd
->gd_vme_base
;
538 gd
->gd_vme_base
= entry
;
543 * vm_map_entry_reserve:
545 * Reserves vm_map_entry structures so code later on can manipulate
546 * map_entry structures within a locked map without blocking trying
547 * to allocate a new vm_map_entry.
550 vm_map_entry_reserve(int count
)
552 struct globaldata
*gd
= mycpu
;
553 vm_map_entry_t entry
;
558 * Make sure we have enough structures in gd_vme_base to handle
559 * the reservation request.
561 while (gd
->gd_vme_avail
< count
) {
562 entry
= zalloc(mapentzone
);
563 entry
->next
= gd
->gd_vme_base
;
564 gd
->gd_vme_base
= entry
;
567 gd
->gd_vme_avail
-= count
;
573 * vm_map_entry_release:
575 * Releases previously reserved vm_map_entry structures that were not
576 * used. If we have too much junk in our per-cpu cache clean some of
580 vm_map_entry_release(int count
)
582 struct globaldata
*gd
= mycpu
;
583 vm_map_entry_t entry
;
586 gd
->gd_vme_avail
+= count
;
587 while (gd
->gd_vme_avail
> MAP_RESERVE_SLOP
) {
588 entry
= gd
->gd_vme_base
;
589 KKASSERT(entry
!= NULL
);
590 gd
->gd_vme_base
= entry
->next
;
593 zfree(mapentzone
, entry
);
600 * vm_map_entry_kreserve:
602 * Reserve map entry structures for use in kernel_map itself. These
603 * entries have *ALREADY* been reserved on a per-cpu basis when the map
604 * was inited. This function is used by zalloc() to avoid a recursion
605 * when zalloc() itself needs to allocate additional kernel memory.
607 * This function works like the normal reserve but does not load the
608 * vm_map_entry cache (because that would result in an infinite
609 * recursion). Note that gd_vme_avail may go negative. This is expected.
611 * Any caller of this function must be sure to renormalize after
612 * potentially eating entries to ensure that the reserve supply
616 vm_map_entry_kreserve(int count
)
618 struct globaldata
*gd
= mycpu
;
621 gd
->gd_vme_avail
-= count
;
623 KASSERT(gd
->gd_vme_base
!= NULL
, ("no reserved entries left, gd_vme_avail = %d\n", gd
->gd_vme_avail
));
628 * vm_map_entry_krelease:
630 * Release previously reserved map entries for kernel_map. We do not
631 * attempt to clean up like the normal release function as this would
632 * cause an unnecessary (but probably not fatal) deep procedure call.
635 vm_map_entry_krelease(int count
)
637 struct globaldata
*gd
= mycpu
;
640 gd
->gd_vme_avail
+= count
;
645 * vm_map_entry_create: [ internal use only ]
647 * Allocates a VM map entry for insertion. No entry fields are filled
650 * This routine may be called from an interrupt thread but not a FAST
651 * interrupt. This routine may recurse the map lock.
653 static vm_map_entry_t
654 vm_map_entry_create(vm_map_t map
, int *countp
)
656 struct globaldata
*gd
= mycpu
;
657 vm_map_entry_t entry
;
659 KKASSERT(*countp
> 0);
662 entry
= gd
->gd_vme_base
;
663 KASSERT(entry
!= NULL
, ("gd_vme_base NULL! count %d", *countp
));
664 gd
->gd_vme_base
= entry
->next
;
670 * vm_map_entry_dispose: [ internal use only ]
672 * Dispose of a vm_map_entry that is no longer being referenced. This
673 * function may be called from an interrupt.
676 vm_map_entry_dispose(vm_map_t map
, vm_map_entry_t entry
, int *countp
)
678 struct globaldata
*gd
= mycpu
;
680 KKASSERT(map
->hint
!= entry
);
681 KKASSERT(map
->first_free
!= entry
);
685 entry
->next
= gd
->gd_vme_base
;
686 gd
->gd_vme_base
= entry
;
692 * vm_map_entry_{un,}link:
694 * Insert/remove entries from maps.
697 vm_map_entry_link(vm_map_t map
,
698 vm_map_entry_t after_where
,
699 vm_map_entry_t entry
)
702 entry
->prev
= after_where
;
703 entry
->next
= after_where
->next
;
704 entry
->next
->prev
= entry
;
705 after_where
->next
= entry
;
706 if (vm_map_rb_tree_RB_INSERT(&map
->rb_root
, entry
))
707 panic("vm_map_entry_link: dup addr map %p ent %p", map
, entry
);
711 vm_map_entry_unlink(vm_map_t map
,
712 vm_map_entry_t entry
)
717 if (entry
->eflags
& MAP_ENTRY_IN_TRANSITION
)
718 panic("vm_map_entry_unlink: attempt to mess with locked entry! %p", entry
);
723 vm_map_rb_tree_RB_REMOVE(&map
->rb_root
, entry
);
728 * vm_map_lookup_entry: [ internal use only ]
730 * Finds the map entry containing (or
731 * immediately preceding) the specified address
732 * in the given map; the entry is returned
733 * in the "entry" parameter. The boolean
734 * result indicates whether the address is
735 * actually contained in the map.
738 vm_map_lookup_entry(vm_map_t map
, vm_offset_t address
,
739 vm_map_entry_t
*entry
/* OUT */)
746 * XXX TEMPORARILY DISABLED. For some reason our attempt to revive
747 * the hint code with the red-black lookup meets with system crashes
748 * and lockups. We do not yet know why.
750 * It is possible that the problem is related to the setting
751 * of the hint during map_entry deletion, in the code specified
752 * at the GGG comment later on in this file.
755 * Quickly check the cached hint, there's a good chance of a match.
757 if (map
->hint
!= &map
->header
) {
759 if (address
>= tmp
->start
&& address
< tmp
->end
) {
767 * Locate the record from the top of the tree. 'last' tracks the
768 * closest prior record and is returned if no match is found, which
769 * in binary tree terms means tracking the most recent right-branch
770 * taken. If there is no prior record, &map->header is returned.
773 tmp
= RB_ROOT(&map
->rb_root
);
776 if (address
>= tmp
->start
) {
777 if (address
< tmp
->end
) {
783 tmp
= RB_RIGHT(tmp
, rb_entry
);
785 tmp
= RB_LEFT(tmp
, rb_entry
);
795 * Inserts the given whole VM object into the target
796 * map at the specified address range. The object's
797 * size should match that of the address range.
799 * Requires that the map be locked, and leaves it so. Requires that
800 * sufficient vm_map_entry structures have been reserved and tracks
801 * the use via countp.
803 * If object is non-NULL, ref count must be bumped by caller
804 * prior to making call to account for the new entry.
807 vm_map_insert(vm_map_t map
, int *countp
,
808 vm_object_t object
, vm_ooffset_t offset
,
809 vm_offset_t start
, vm_offset_t end
,
810 vm_maptype_t maptype
,
811 vm_prot_t prot
, vm_prot_t max
,
814 vm_map_entry_t new_entry
;
815 vm_map_entry_t prev_entry
;
816 vm_map_entry_t temp_entry
;
817 vm_eflags_t protoeflags
;
820 * Check that the start and end points are not bogus.
823 if ((start
< map
->min_offset
) || (end
> map
->max_offset
) ||
825 return (KERN_INVALID_ADDRESS
);
828 * Find the entry prior to the proposed starting address; if it's part
829 * of an existing entry, this range is bogus.
832 if (vm_map_lookup_entry(map
, start
, &temp_entry
))
833 return (KERN_NO_SPACE
);
835 prev_entry
= temp_entry
;
838 * Assert that the next entry doesn't overlap the end point.
841 if ((prev_entry
->next
!= &map
->header
) &&
842 (prev_entry
->next
->start
< end
))
843 return (KERN_NO_SPACE
);
847 if (cow
& MAP_COPY_ON_WRITE
)
848 protoeflags
|= MAP_ENTRY_COW
|MAP_ENTRY_NEEDS_COPY
;
850 if (cow
& MAP_NOFAULT
) {
851 protoeflags
|= MAP_ENTRY_NOFAULT
;
853 KASSERT(object
== NULL
,
854 ("vm_map_insert: paradoxical MAP_NOFAULT request"));
856 if (cow
& MAP_DISABLE_SYNCER
)
857 protoeflags
|= MAP_ENTRY_NOSYNC
;
858 if (cow
& MAP_DISABLE_COREDUMP
)
859 protoeflags
|= MAP_ENTRY_NOCOREDUMP
;
860 if (cow
& MAP_IS_STACK
)
861 protoeflags
|= MAP_ENTRY_STACK
;
865 * When object is non-NULL, it could be shared with another
866 * process. We have to set or clear OBJ_ONEMAPPING
869 if ((object
->ref_count
> 1) || (object
->shadow_count
!= 0)) {
870 vm_object_clear_flag(object
, OBJ_ONEMAPPING
);
873 else if ((prev_entry
!= &map
->header
) &&
874 (prev_entry
->eflags
== protoeflags
) &&
875 (prev_entry
->end
== start
) &&
876 (prev_entry
->wired_count
== 0) &&
877 prev_entry
->maptype
== maptype
&&
878 ((prev_entry
->object
.vm_object
== NULL
) ||
879 vm_object_coalesce(prev_entry
->object
.vm_object
,
880 OFF_TO_IDX(prev_entry
->offset
),
881 (vm_size_t
)(prev_entry
->end
- prev_entry
->start
),
882 (vm_size_t
)(end
- prev_entry
->end
)))) {
884 * We were able to extend the object. Determine if we
885 * can extend the previous map entry to include the
888 if ((prev_entry
->inheritance
== VM_INHERIT_DEFAULT
) &&
889 (prev_entry
->protection
== prot
) &&
890 (prev_entry
->max_protection
== max
)) {
891 map
->size
+= (end
- prev_entry
->end
);
892 prev_entry
->end
= end
;
893 vm_map_simplify_entry(map
, prev_entry
, countp
);
894 return (KERN_SUCCESS
);
898 * If we can extend the object but cannot extend the
899 * map entry, we have to create a new map entry. We
900 * must bump the ref count on the extended object to
901 * account for it. object may be NULL.
903 object
= prev_entry
->object
.vm_object
;
904 offset
= prev_entry
->offset
+
905 (prev_entry
->end
- prev_entry
->start
);
906 vm_object_reference(object
);
910 * NOTE: if conditionals fail, object can be NULL here. This occurs
911 * in things like the buffer map where we manage kva but do not manage
919 new_entry
= vm_map_entry_create(map
, countp
);
920 new_entry
->start
= start
;
921 new_entry
->end
= end
;
923 new_entry
->maptype
= maptype
;
924 new_entry
->eflags
= protoeflags
;
925 new_entry
->object
.vm_object
= object
;
926 new_entry
->offset
= offset
;
927 new_entry
->aux
.master_pde
= 0;
929 new_entry
->inheritance
= VM_INHERIT_DEFAULT
;
930 new_entry
->protection
= prot
;
931 new_entry
->max_protection
= max
;
932 new_entry
->wired_count
= 0;
935 * Insert the new entry into the list
938 vm_map_entry_link(map
, prev_entry
, new_entry
);
939 map
->size
+= new_entry
->end
- new_entry
->start
;
942 * Update the free space hint. Entries cannot overlap.
943 * An exact comparison is needed to avoid matching
944 * against the map->header.
946 if ((map
->first_free
== prev_entry
) &&
947 (prev_entry
->end
== new_entry
->start
)) {
948 map
->first_free
= new_entry
;
953 * Temporarily removed to avoid MAP_STACK panic, due to
954 * MAP_STACK being a huge hack. Will be added back in
955 * when MAP_STACK (and the user stack mapping) is fixed.
958 * It may be possible to simplify the entry
960 vm_map_simplify_entry(map
, new_entry
, countp
);
964 * Try to pre-populate the page table. Mappings governed by virtual
965 * page tables cannot be prepopulated without a lot of work, so
968 if ((cow
& (MAP_PREFAULT
|MAP_PREFAULT_PARTIAL
)) &&
969 maptype
!= VM_MAPTYPE_VPAGETABLE
) {
970 pmap_object_init_pt(map
->pmap
, start
, prot
,
971 object
, OFF_TO_IDX(offset
), end
- start
,
972 cow
& MAP_PREFAULT_PARTIAL
);
975 return (KERN_SUCCESS
);
979 * Find sufficient space for `length' bytes in the given map, starting at
980 * `start'. The map must be locked. Returns 0 on success, 1 on no space.
982 * This function will returned an arbitrarily aligned pointer. If no
983 * particular alignment is required you should pass align as 1. Note that
984 * the map may return PAGE_SIZE aligned pointers if all the lengths used in
985 * the map are a multiple of PAGE_SIZE, even if you pass a smaller align
988 * 'align' should be a power of 2 but is not required to be.
991 vm_map_findspace(vm_map_t map
, vm_offset_t start
, vm_size_t length
,
992 vm_offset_t align
, int flags
, vm_offset_t
*addr
)
994 vm_map_entry_t entry
, next
;
996 vm_offset_t align_mask
;
998 if (start
< map
->min_offset
)
999 start
= map
->min_offset
;
1000 if (start
> map
->max_offset
)
1004 * If the alignment is not a power of 2 we will have to use
1005 * a mod/division, set align_mask to a special value.
1007 if ((align
| (align
- 1)) + 1 != (align
<< 1))
1008 align_mask
= (vm_offset_t
)-1;
1010 align_mask
= align
- 1;
1014 * Look for the first possible address; if there's already something
1015 * at this address, we have to start after it.
1017 if (start
== map
->min_offset
) {
1018 if ((entry
= map
->first_free
) != &map
->header
)
1023 if (vm_map_lookup_entry(map
, start
, &tmp
))
1029 * Look through the rest of the map, trying to fit a new region in the
1030 * gap between existing regions, or after the very last region.
1032 for (;; start
= (entry
= next
)->end
) {
1034 * Adjust the proposed start by the requested alignment,
1035 * be sure that we didn't wrap the address.
1037 if (align_mask
== (vm_offset_t
)-1)
1038 end
= ((start
+ align
- 1) / align
) * align
;
1040 end
= (start
+ align_mask
) & ~align_mask
;
1045 * Find the end of the proposed new region. Be sure we didn't
1046 * go beyond the end of the map, or wrap around the address.
1047 * Then check to see if this is the last entry or if the
1048 * proposed end fits in the gap between this and the next
1051 end
= start
+ length
;
1052 if (end
> map
->max_offset
|| end
< start
)
1057 * If the next entry's start address is beyond the desired
1058 * end address we may have found a good entry.
1060 * If the next entry is a stack mapping we do not map into
1061 * the stack's reserved space.
1063 * XXX continue to allow mapping into the stack's reserved
1064 * space if doing a MAP_STACK mapping inside a MAP_STACK
1065 * mapping, for backwards compatibility. But the caller
1066 * really should use MAP_STACK | MAP_TRYFIXED if they
1069 if (next
== &map
->header
)
1071 if (next
->start
>= end
) {
1072 if ((next
->eflags
& MAP_ENTRY_STACK
) == 0)
1074 if (flags
& MAP_STACK
)
1076 if (next
->start
- next
->aux
.avail_ssize
>= end
)
1081 if (map
== &kernel_map
) {
1083 if ((ksize
= round_page(start
+ length
)) > kernel_vm_end
) {
1084 pmap_growkernel(ksize
);
1093 * vm_map_find finds an unallocated region in the target address
1094 * map with the given length. The search is defined to be
1095 * first-fit from the specified address; the region found is
1096 * returned in the same parameter.
1098 * If object is non-NULL, ref count must be bumped by caller
1099 * prior to making call to account for the new entry.
1102 vm_map_find(vm_map_t map
, vm_object_t object
, vm_ooffset_t offset
,
1103 vm_offset_t
*addr
, vm_size_t length
,
1105 vm_maptype_t maptype
,
1106 vm_prot_t prot
, vm_prot_t max
,
1115 count
= vm_map_entry_reserve(MAP_RESERVE_COUNT
);
1118 if (vm_map_findspace(map
, start
, length
, 1, 0, addr
)) {
1120 vm_map_entry_release(count
);
1121 return (KERN_NO_SPACE
);
1125 result
= vm_map_insert(map
, &count
, object
, offset
,
1126 start
, start
+ length
,
1131 vm_map_entry_release(count
);
1137 * vm_map_simplify_entry:
1139 * Simplify the given map entry by merging with either neighbor. This
1140 * routine also has the ability to merge with both neighbors.
1142 * The map must be locked.
1144 * This routine guarentees that the passed entry remains valid (though
1145 * possibly extended). When merging, this routine may delete one or
1146 * both neighbors. No action is taken on entries which have their
1147 * in-transition flag set.
1150 vm_map_simplify_entry(vm_map_t map
, vm_map_entry_t entry
, int *countp
)
1152 vm_map_entry_t next
, prev
;
1153 vm_size_t prevsize
, esize
;
1155 if (entry
->eflags
& MAP_ENTRY_IN_TRANSITION
) {
1156 ++mycpu
->gd_cnt
.v_intrans_coll
;
1160 if (entry
->maptype
== VM_MAPTYPE_SUBMAP
)
1164 if (prev
!= &map
->header
) {
1165 prevsize
= prev
->end
- prev
->start
;
1166 if ( (prev
->end
== entry
->start
) &&
1167 (prev
->maptype
== entry
->maptype
) &&
1168 (prev
->object
.vm_object
== entry
->object
.vm_object
) &&
1169 (!prev
->object
.vm_object
||
1170 (prev
->offset
+ prevsize
== entry
->offset
)) &&
1171 (prev
->eflags
== entry
->eflags
) &&
1172 (prev
->protection
== entry
->protection
) &&
1173 (prev
->max_protection
== entry
->max_protection
) &&
1174 (prev
->inheritance
== entry
->inheritance
) &&
1175 (prev
->wired_count
== entry
->wired_count
)) {
1176 if (map
->first_free
== prev
)
1177 map
->first_free
= entry
;
1178 if (map
->hint
== prev
)
1180 vm_map_entry_unlink(map
, prev
);
1181 entry
->start
= prev
->start
;
1182 entry
->offset
= prev
->offset
;
1183 if (prev
->object
.vm_object
)
1184 vm_object_deallocate(prev
->object
.vm_object
);
1185 vm_map_entry_dispose(map
, prev
, countp
);
1190 if (next
!= &map
->header
) {
1191 esize
= entry
->end
- entry
->start
;
1192 if ((entry
->end
== next
->start
) &&
1193 (next
->maptype
== entry
->maptype
) &&
1194 (next
->object
.vm_object
== entry
->object
.vm_object
) &&
1195 (!entry
->object
.vm_object
||
1196 (entry
->offset
+ esize
== next
->offset
)) &&
1197 (next
->eflags
== entry
->eflags
) &&
1198 (next
->protection
== entry
->protection
) &&
1199 (next
->max_protection
== entry
->max_protection
) &&
1200 (next
->inheritance
== entry
->inheritance
) &&
1201 (next
->wired_count
== entry
->wired_count
)) {
1202 if (map
->first_free
== next
)
1203 map
->first_free
= entry
;
1204 if (map
->hint
== next
)
1206 vm_map_entry_unlink(map
, next
);
1207 entry
->end
= next
->end
;
1208 if (next
->object
.vm_object
)
1209 vm_object_deallocate(next
->object
.vm_object
);
1210 vm_map_entry_dispose(map
, next
, countp
);
1215 * vm_map_clip_start: [ internal use only ]
1217 * Asserts that the given entry begins at or after
1218 * the specified address; if necessary,
1219 * it splits the entry into two.
1221 #define vm_map_clip_start(map, entry, startaddr, countp) \
1223 if (startaddr > entry->start) \
1224 _vm_map_clip_start(map, entry, startaddr, countp); \
1228 * This routine is called only when it is known that
1229 * the entry must be split.
1232 _vm_map_clip_start(vm_map_t map
, vm_map_entry_t entry
, vm_offset_t start
, int *countp
)
1234 vm_map_entry_t new_entry
;
1237 * Split off the front portion -- note that we must insert the new
1238 * entry BEFORE this one, so that this entry has the specified
1242 vm_map_simplify_entry(map
, entry
, countp
);
1245 * If there is no object backing this entry, we might as well create
1246 * one now. If we defer it, an object can get created after the map
1247 * is clipped, and individual objects will be created for the split-up
1248 * map. This is a bit of a hack, but is also about the best place to
1249 * put this improvement.
1251 if (entry
->object
.vm_object
== NULL
&& !map
->system_map
) {
1252 vm_map_entry_allocate_object(entry
);
1255 new_entry
= vm_map_entry_create(map
, countp
);
1256 *new_entry
= *entry
;
1258 new_entry
->end
= start
;
1259 entry
->offset
+= (start
- entry
->start
);
1260 entry
->start
= start
;
1262 vm_map_entry_link(map
, entry
->prev
, new_entry
);
1264 switch(entry
->maptype
) {
1265 case VM_MAPTYPE_NORMAL
:
1266 case VM_MAPTYPE_VPAGETABLE
:
1267 vm_object_reference(new_entry
->object
.vm_object
);
1275 * vm_map_clip_end: [ internal use only ]
1277 * Asserts that the given entry ends at or before
1278 * the specified address; if necessary,
1279 * it splits the entry into two.
1282 #define vm_map_clip_end(map, entry, endaddr, countp) \
1284 if (endaddr < entry->end) \
1285 _vm_map_clip_end(map, entry, endaddr, countp); \
1289 * This routine is called only when it is known that
1290 * the entry must be split.
1293 _vm_map_clip_end(vm_map_t map
, vm_map_entry_t entry
, vm_offset_t end
, int *countp
)
1295 vm_map_entry_t new_entry
;
1298 * If there is no object backing this entry, we might as well create
1299 * one now. If we defer it, an object can get created after the map
1300 * is clipped, and individual objects will be created for the split-up
1301 * map. This is a bit of a hack, but is also about the best place to
1302 * put this improvement.
1305 if (entry
->object
.vm_object
== NULL
&& !map
->system_map
) {
1306 vm_map_entry_allocate_object(entry
);
1310 * Create a new entry and insert it AFTER the specified entry
1313 new_entry
= vm_map_entry_create(map
, countp
);
1314 *new_entry
= *entry
;
1316 new_entry
->start
= entry
->end
= end
;
1317 new_entry
->offset
+= (end
- entry
->start
);
1319 vm_map_entry_link(map
, entry
, new_entry
);
1321 switch(entry
->maptype
) {
1322 case VM_MAPTYPE_NORMAL
:
1323 case VM_MAPTYPE_VPAGETABLE
:
1324 vm_object_reference(new_entry
->object
.vm_object
);
1332 * VM_MAP_RANGE_CHECK: [ internal use only ]
1334 * Asserts that the starting and ending region
1335 * addresses fall within the valid range of the map.
1337 #define VM_MAP_RANGE_CHECK(map, start, end) \
1339 if (start < vm_map_min(map)) \
1340 start = vm_map_min(map); \
1341 if (end > vm_map_max(map)) \
1342 end = vm_map_max(map); \
1348 * vm_map_transition_wait: [ kernel use only ]
1350 * Used to block when an in-transition collison occurs. The map
1351 * is unlocked for the sleep and relocked before the return.
1355 vm_map_transition_wait(vm_map_t map
)
1358 tsleep(map
, 0, "vment", 0);
1366 * When we do blocking operations with the map lock held it is
1367 * possible that a clip might have occured on our in-transit entry,
1368 * requiring an adjustment to the entry in our loop. These macros
1369 * help the pageable and clip_range code deal with the case. The
1370 * conditional costs virtually nothing if no clipping has occured.
1373 #define CLIP_CHECK_BACK(entry, save_start) \
1375 while (entry->start != save_start) { \
1376 entry = entry->prev; \
1377 KASSERT(entry != &map->header, ("bad entry clip")); \
1381 #define CLIP_CHECK_FWD(entry, save_end) \
1383 while (entry->end != save_end) { \
1384 entry = entry->next; \
1385 KASSERT(entry != &map->header, ("bad entry clip")); \
1391 * vm_map_clip_range: [ kernel use only ]
1393 * Clip the specified range and return the base entry. The
1394 * range may cover several entries starting at the returned base
1395 * and the first and last entry in the covering sequence will be
1396 * properly clipped to the requested start and end address.
1398 * If no holes are allowed you should pass the MAP_CLIP_NO_HOLES
1401 * The MAP_ENTRY_IN_TRANSITION flag will be set for the entries
1402 * covered by the requested range.
1404 * The map must be exclusively locked on entry and will remain locked
1405 * on return. If no range exists or the range contains holes and you
1406 * specified that no holes were allowed, NULL will be returned. This
1407 * routine may temporarily unlock the map in order avoid a deadlock when
1412 vm_map_clip_range(vm_map_t map
, vm_offset_t start
, vm_offset_t end
,
1413 int *countp
, int flags
)
1415 vm_map_entry_t start_entry
;
1416 vm_map_entry_t entry
;
1419 * Locate the entry and effect initial clipping. The in-transition
1420 * case does not occur very often so do not try to optimize it.
1423 if (vm_map_lookup_entry(map
, start
, &start_entry
) == FALSE
)
1425 entry
= start_entry
;
1426 if (entry
->eflags
& MAP_ENTRY_IN_TRANSITION
) {
1427 entry
->eflags
|= MAP_ENTRY_NEEDS_WAKEUP
;
1428 ++mycpu
->gd_cnt
.v_intrans_coll
;
1429 ++mycpu
->gd_cnt
.v_intrans_wait
;
1430 vm_map_transition_wait(map
);
1432 * entry and/or start_entry may have been clipped while
1433 * we slept, or may have gone away entirely. We have
1434 * to restart from the lookup.
1439 * Since we hold an exclusive map lock we do not have to restart
1440 * after clipping, even though clipping may block in zalloc.
1442 vm_map_clip_start(map
, entry
, start
, countp
);
1443 vm_map_clip_end(map
, entry
, end
, countp
);
1444 entry
->eflags
|= MAP_ENTRY_IN_TRANSITION
;
1447 * Scan entries covered by the range. When working on the next
1448 * entry a restart need only re-loop on the current entry which
1449 * we have already locked, since 'next' may have changed. Also,
1450 * even though entry is safe, it may have been clipped so we
1451 * have to iterate forwards through the clip after sleeping.
1453 while (entry
->next
!= &map
->header
&& entry
->next
->start
< end
) {
1454 vm_map_entry_t next
= entry
->next
;
1456 if (flags
& MAP_CLIP_NO_HOLES
) {
1457 if (next
->start
> entry
->end
) {
1458 vm_map_unclip_range(map
, start_entry
,
1459 start
, entry
->end
, countp
, flags
);
1464 if (next
->eflags
& MAP_ENTRY_IN_TRANSITION
) {
1465 vm_offset_t save_end
= entry
->end
;
1466 next
->eflags
|= MAP_ENTRY_NEEDS_WAKEUP
;
1467 ++mycpu
->gd_cnt
.v_intrans_coll
;
1468 ++mycpu
->gd_cnt
.v_intrans_wait
;
1469 vm_map_transition_wait(map
);
1472 * clips might have occured while we blocked.
1474 CLIP_CHECK_FWD(entry
, save_end
);
1475 CLIP_CHECK_BACK(start_entry
, start
);
1479 * No restart necessary even though clip_end may block, we
1480 * are holding the map lock.
1482 vm_map_clip_end(map
, next
, end
, countp
);
1483 next
->eflags
|= MAP_ENTRY_IN_TRANSITION
;
1486 if (flags
& MAP_CLIP_NO_HOLES
) {
1487 if (entry
->end
!= end
) {
1488 vm_map_unclip_range(map
, start_entry
,
1489 start
, entry
->end
, countp
, flags
);
1493 return(start_entry
);
1497 * vm_map_unclip_range: [ kernel use only ]
1499 * Undo the effect of vm_map_clip_range(). You should pass the same
1500 * flags and the same range that you passed to vm_map_clip_range().
1501 * This code will clear the in-transition flag on the entries and
1502 * wake up anyone waiting. This code will also simplify the sequence
1503 * and attempt to merge it with entries before and after the sequence.
1505 * The map must be locked on entry and will remain locked on return.
1507 * Note that you should also pass the start_entry returned by
1508 * vm_map_clip_range(). However, if you block between the two calls
1509 * with the map unlocked please be aware that the start_entry may
1510 * have been clipped and you may need to scan it backwards to find
1511 * the entry corresponding with the original start address. You are
1512 * responsible for this, vm_map_unclip_range() expects the correct
1513 * start_entry to be passed to it and will KASSERT otherwise.
1517 vm_map_unclip_range(
1519 vm_map_entry_t start_entry
,
1525 vm_map_entry_t entry
;
1527 entry
= start_entry
;
1529 KASSERT(entry
->start
== start
, ("unclip_range: illegal base entry"));
1530 while (entry
!= &map
->header
&& entry
->start
< end
) {
1531 KASSERT(entry
->eflags
& MAP_ENTRY_IN_TRANSITION
, ("in-transition flag not set during unclip on: %p", entry
));
1532 KASSERT(entry
->end
<= end
, ("unclip_range: tail wasn't clipped"));
1533 entry
->eflags
&= ~MAP_ENTRY_IN_TRANSITION
;
1534 if (entry
->eflags
& MAP_ENTRY_NEEDS_WAKEUP
) {
1535 entry
->eflags
&= ~MAP_ENTRY_NEEDS_WAKEUP
;
1538 entry
= entry
->next
;
1542 * Simplification does not block so there is no restart case.
1544 entry
= start_entry
;
1545 while (entry
!= &map
->header
&& entry
->start
< end
) {
1546 vm_map_simplify_entry(map
, entry
, countp
);
1547 entry
= entry
->next
;
1552 * vm_map_submap: [ kernel use only ]
1554 * Mark the given range as handled by a subordinate map.
1556 * This range must have been created with vm_map_find,
1557 * and no other operations may have been performed on this
1558 * range prior to calling vm_map_submap.
1560 * Only a limited number of operations can be performed
1561 * within this rage after calling vm_map_submap:
1563 * [Don't try vm_map_copy!]
1565 * To remove a submapping, one must first remove the
1566 * range from the superior map, and then destroy the
1567 * submap (if desired). [Better yet, don't try it.]
1570 vm_map_submap(vm_map_t map
, vm_offset_t start
, vm_offset_t end
, vm_map_t submap
)
1572 vm_map_entry_t entry
;
1573 int result
= KERN_INVALID_ARGUMENT
;
1576 count
= vm_map_entry_reserve(MAP_RESERVE_COUNT
);
1579 VM_MAP_RANGE_CHECK(map
, start
, end
);
1581 if (vm_map_lookup_entry(map
, start
, &entry
)) {
1582 vm_map_clip_start(map
, entry
, start
, &count
);
1584 entry
= entry
->next
;
1587 vm_map_clip_end(map
, entry
, end
, &count
);
1589 if ((entry
->start
== start
) && (entry
->end
== end
) &&
1590 ((entry
->eflags
& MAP_ENTRY_COW
) == 0) &&
1591 (entry
->object
.vm_object
== NULL
)) {
1592 entry
->object
.sub_map
= submap
;
1593 entry
->maptype
= VM_MAPTYPE_SUBMAP
;
1594 result
= KERN_SUCCESS
;
1597 vm_map_entry_release(count
);
1605 * Sets the protection of the specified address region in the target map.
1606 * If "set_max" is specified, the maximum protection is to be set;
1607 * otherwise, only the current protection is affected.
1609 * The protection is not applicable to submaps, but is applicable to normal
1610 * maps and maps governed by virtual page tables. For example, when operating
1611 * on a virtual page table our protection basically controls how COW occurs
1612 * on the backing object, whereas the virtual page table abstraction itself
1613 * is an abstraction for userland.
1616 vm_map_protect(vm_map_t map
, vm_offset_t start
, vm_offset_t end
,
1617 vm_prot_t new_prot
, boolean_t set_max
)
1619 vm_map_entry_t current
;
1620 vm_map_entry_t entry
;
1623 count
= vm_map_entry_reserve(MAP_RESERVE_COUNT
);
1626 VM_MAP_RANGE_CHECK(map
, start
, end
);
1628 if (vm_map_lookup_entry(map
, start
, &entry
)) {
1629 vm_map_clip_start(map
, entry
, start
, &count
);
1631 entry
= entry
->next
;
1635 * Make a first pass to check for protection violations.
1638 while ((current
!= &map
->header
) && (current
->start
< end
)) {
1639 if (current
->maptype
== VM_MAPTYPE_SUBMAP
) {
1641 vm_map_entry_release(count
);
1642 return (KERN_INVALID_ARGUMENT
);
1644 if ((new_prot
& current
->max_protection
) != new_prot
) {
1646 vm_map_entry_release(count
);
1647 return (KERN_PROTECTION_FAILURE
);
1649 current
= current
->next
;
1653 * Go back and fix up protections. [Note that clipping is not
1654 * necessary the second time.]
1658 while ((current
!= &map
->header
) && (current
->start
< end
)) {
1661 vm_map_clip_end(map
, current
, end
, &count
);
1663 old_prot
= current
->protection
;
1665 current
->protection
=
1666 (current
->max_protection
= new_prot
) &
1669 current
->protection
= new_prot
;
1673 * Update physical map if necessary. Worry about copy-on-write
1674 * here -- CHECK THIS XXX
1677 if (current
->protection
!= old_prot
) {
1678 #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1681 pmap_protect(map
->pmap
, current
->start
,
1683 current
->protection
& MASK(current
));
1687 vm_map_simplify_entry(map
, current
, &count
);
1689 current
= current
->next
;
1693 vm_map_entry_release(count
);
1694 return (KERN_SUCCESS
);
1700 * This routine traverses a processes map handling the madvise
1701 * system call. Advisories are classified as either those effecting
1702 * the vm_map_entry structure, or those effecting the underlying
1705 * The <value> argument is used for extended madvise calls.
1708 vm_map_madvise(vm_map_t map
, vm_offset_t start
, vm_offset_t end
,
1709 int behav
, off_t value
)
1711 vm_map_entry_t current
, entry
;
1717 * Some madvise calls directly modify the vm_map_entry, in which case
1718 * we need to use an exclusive lock on the map and we need to perform
1719 * various clipping operations. Otherwise we only need a read-lock
1723 count
= vm_map_entry_reserve(MAP_RESERVE_COUNT
);
1727 case MADV_SEQUENTIAL
:
1741 vm_map_lock_read(map
);
1744 vm_map_entry_release(count
);
1749 * Locate starting entry and clip if necessary.
1752 VM_MAP_RANGE_CHECK(map
, start
, end
);
1754 if (vm_map_lookup_entry(map
, start
, &entry
)) {
1756 vm_map_clip_start(map
, entry
, start
, &count
);
1758 entry
= entry
->next
;
1763 * madvise behaviors that are implemented in the vm_map_entry.
1765 * We clip the vm_map_entry so that behavioral changes are
1766 * limited to the specified address range.
1768 for (current
= entry
;
1769 (current
!= &map
->header
) && (current
->start
< end
);
1770 current
= current
->next
1772 if (current
->maptype
== VM_MAPTYPE_SUBMAP
)
1775 vm_map_clip_end(map
, current
, end
, &count
);
1779 vm_map_entry_set_behavior(current
, MAP_ENTRY_BEHAV_NORMAL
);
1781 case MADV_SEQUENTIAL
:
1782 vm_map_entry_set_behavior(current
, MAP_ENTRY_BEHAV_SEQUENTIAL
);
1785 vm_map_entry_set_behavior(current
, MAP_ENTRY_BEHAV_RANDOM
);
1788 current
->eflags
|= MAP_ENTRY_NOSYNC
;
1791 current
->eflags
&= ~MAP_ENTRY_NOSYNC
;
1794 current
->eflags
|= MAP_ENTRY_NOCOREDUMP
;
1797 current
->eflags
&= ~MAP_ENTRY_NOCOREDUMP
;
1801 * Invalidate the related pmap entries, used
1802 * to flush portions of the real kernel's
1803 * pmap when the caller has removed or
1804 * modified existing mappings in a virtual
1807 pmap_remove(map
->pmap
,
1808 current
->start
, current
->end
);
1812 * Set the page directory page for a map
1813 * governed by a virtual page table. Mark
1814 * the entry as being governed by a virtual
1815 * page table if it is not.
1817 * XXX the page directory page is stored
1818 * in the avail_ssize field if the map_entry.
1820 * XXX the map simplification code does not
1821 * compare this field so weird things may
1822 * happen if you do not apply this function
1823 * to the entire mapping governed by the
1824 * virtual page table.
1826 if (current
->maptype
!= VM_MAPTYPE_VPAGETABLE
) {
1830 current
->aux
.master_pde
= value
;
1831 pmap_remove(map
->pmap
,
1832 current
->start
, current
->end
);
1838 vm_map_simplify_entry(map
, current
, &count
);
1846 * madvise behaviors that are implemented in the underlying
1849 * Since we don't clip the vm_map_entry, we have to clip
1850 * the vm_object pindex and count.
1852 * NOTE! We currently do not support these functions on
1853 * virtual page tables.
1855 for (current
= entry
;
1856 (current
!= &map
->header
) && (current
->start
< end
);
1857 current
= current
->next
1859 vm_offset_t useStart
;
1861 if (current
->maptype
!= VM_MAPTYPE_NORMAL
)
1864 pindex
= OFF_TO_IDX(current
->offset
);
1865 count
= atop(current
->end
- current
->start
);
1866 useStart
= current
->start
;
1868 if (current
->start
< start
) {
1869 pindex
+= atop(start
- current
->start
);
1870 count
-= atop(start
- current
->start
);
1873 if (current
->end
> end
)
1874 count
-= atop(current
->end
- end
);
1879 vm_object_madvise(current
->object
.vm_object
,
1880 pindex
, count
, behav
);
1883 * Try to populate the page table. Mappings governed
1884 * by virtual page tables cannot be pre-populated
1885 * without a lot of work so don't try.
1887 if (behav
== MADV_WILLNEED
&&
1888 current
->maptype
!= VM_MAPTYPE_VPAGETABLE
) {
1889 pmap_object_init_pt(
1892 current
->protection
,
1893 current
->object
.vm_object
,
1895 (count
<< PAGE_SHIFT
),
1896 MAP_PREFAULT_MADVISE
1900 vm_map_unlock_read(map
);
1902 vm_map_entry_release(count
);
1910 * Sets the inheritance of the specified address
1911 * range in the target map. Inheritance
1912 * affects how the map will be shared with
1913 * child maps at the time of vm_map_fork.
1916 vm_map_inherit(vm_map_t map
, vm_offset_t start
, vm_offset_t end
,
1917 vm_inherit_t new_inheritance
)
1919 vm_map_entry_t entry
;
1920 vm_map_entry_t temp_entry
;
1923 switch (new_inheritance
) {
1924 case VM_INHERIT_NONE
:
1925 case VM_INHERIT_COPY
:
1926 case VM_INHERIT_SHARE
:
1929 return (KERN_INVALID_ARGUMENT
);
1932 count
= vm_map_entry_reserve(MAP_RESERVE_COUNT
);
1935 VM_MAP_RANGE_CHECK(map
, start
, end
);
1937 if (vm_map_lookup_entry(map
, start
, &temp_entry
)) {
1939 vm_map_clip_start(map
, entry
, start
, &count
);
1941 entry
= temp_entry
->next
;
1943 while ((entry
!= &map
->header
) && (entry
->start
< end
)) {
1944 vm_map_clip_end(map
, entry
, end
, &count
);
1946 entry
->inheritance
= new_inheritance
;
1948 vm_map_simplify_entry(map
, entry
, &count
);
1950 entry
= entry
->next
;
1953 vm_map_entry_release(count
);
1954 return (KERN_SUCCESS
);
1958 * Implement the semantics of mlock
1961 vm_map_unwire(vm_map_t map
, vm_offset_t start
, vm_offset_t real_end
,
1962 boolean_t new_pageable
)
1964 vm_map_entry_t entry
;
1965 vm_map_entry_t start_entry
;
1967 int rv
= KERN_SUCCESS
;
1970 count
= vm_map_entry_reserve(MAP_RESERVE_COUNT
);
1972 VM_MAP_RANGE_CHECK(map
, start
, real_end
);
1975 start_entry
= vm_map_clip_range(map
, start
, end
, &count
, MAP_CLIP_NO_HOLES
);
1976 if (start_entry
== NULL
) {
1978 vm_map_entry_release(count
);
1979 return (KERN_INVALID_ADDRESS
);
1982 if (new_pageable
== 0) {
1983 entry
= start_entry
;
1984 while ((entry
!= &map
->header
) && (entry
->start
< end
)) {
1985 vm_offset_t save_start
;
1986 vm_offset_t save_end
;
1989 * Already user wired or hard wired (trivial cases)
1991 if (entry
->eflags
& MAP_ENTRY_USER_WIRED
) {
1992 entry
= entry
->next
;
1995 if (entry
->wired_count
!= 0) {
1996 entry
->wired_count
++;
1997 entry
->eflags
|= MAP_ENTRY_USER_WIRED
;
1998 entry
= entry
->next
;
2003 * A new wiring requires instantiation of appropriate
2004 * management structures and the faulting in of the
2007 if (entry
->maptype
!= VM_MAPTYPE_SUBMAP
) {
2008 int copyflag
= entry
->eflags
& MAP_ENTRY_NEEDS_COPY
;
2009 if (copyflag
&& ((entry
->protection
& VM_PROT_WRITE
) != 0)) {
2010 vm_map_entry_shadow(entry
);
2011 } else if (entry
->object
.vm_object
== NULL
&&
2013 vm_map_entry_allocate_object(entry
);
2016 entry
->wired_count
++;
2017 entry
->eflags
|= MAP_ENTRY_USER_WIRED
;
2020 * Now fault in the area. Note that vm_fault_wire()
2021 * may release the map lock temporarily, it will be
2022 * relocked on return. The in-transition
2023 * flag protects the entries.
2025 save_start
= entry
->start
;
2026 save_end
= entry
->end
;
2027 rv
= vm_fault_wire(map
, entry
, TRUE
);
2029 CLIP_CHECK_BACK(entry
, save_start
);
2031 KASSERT(entry
->wired_count
== 1, ("bad wired_count on entry"));
2032 entry
->eflags
&= ~MAP_ENTRY_USER_WIRED
;
2033 entry
->wired_count
= 0;
2034 if (entry
->end
== save_end
)
2036 entry
= entry
->next
;
2037 KASSERT(entry
!= &map
->header
, ("bad entry clip during backout"));
2039 end
= save_start
; /* unwire the rest */
2043 * note that even though the entry might have been
2044 * clipped, the USER_WIRED flag we set prevents
2045 * duplication so we do not have to do a
2048 entry
= entry
->next
;
2052 * If we failed fall through to the unwiring section to
2053 * unwire what we had wired so far. 'end' has already
2060 * start_entry might have been clipped if we unlocked the
2061 * map and blocked. No matter how clipped it has gotten
2062 * there should be a fragment that is on our start boundary.
2064 CLIP_CHECK_BACK(start_entry
, start
);
2068 * Deal with the unwiring case.
2072 * This is the unwiring case. We must first ensure that the
2073 * range to be unwired is really wired down. We know there
2076 entry
= start_entry
;
2077 while ((entry
!= &map
->header
) && (entry
->start
< end
)) {
2078 if ((entry
->eflags
& MAP_ENTRY_USER_WIRED
) == 0) {
2079 rv
= KERN_INVALID_ARGUMENT
;
2082 KASSERT(entry
->wired_count
!= 0, ("wired count was 0 with USER_WIRED set! %p", entry
));
2083 entry
= entry
->next
;
2087 * Now decrement the wiring count for each region. If a region
2088 * becomes completely unwired, unwire its physical pages and
2092 * The map entries are processed in a loop, checking to
2093 * make sure the entry is wired and asserting it has a wired
2094 * count. However, another loop was inserted more-or-less in
2095 * the middle of the unwiring path. This loop picks up the
2096 * "entry" loop variable from the first loop without first
2097 * setting it to start_entry. Naturally, the secound loop
2098 * is never entered and the pages backing the entries are
2099 * never unwired. This can lead to a leak of wired pages.
2101 entry
= start_entry
;
2102 while ((entry
!= &map
->header
) && (entry
->start
< end
)) {
2103 KASSERT(entry
->eflags
& MAP_ENTRY_USER_WIRED
,
2104 ("expected USER_WIRED on entry %p", entry
));
2105 entry
->eflags
&= ~MAP_ENTRY_USER_WIRED
;
2106 entry
->wired_count
--;
2107 if (entry
->wired_count
== 0)
2108 vm_fault_unwire(map
, entry
);
2109 entry
= entry
->next
;
2113 vm_map_unclip_range(map
, start_entry
, start
, real_end
, &count
,
2117 vm_map_entry_release(count
);
2124 * Sets the pageability of the specified address
2125 * range in the target map. Regions specified
2126 * as not pageable require locked-down physical
2127 * memory and physical page maps.
2129 * The map must not be locked, but a reference
2130 * must remain to the map throughout the call.
2132 * This function may be called via the zalloc path and must properly
2133 * reserve map entries for kernel_map.
2136 vm_map_wire(vm_map_t map
, vm_offset_t start
, vm_offset_t real_end
, int kmflags
)
2138 vm_map_entry_t entry
;
2139 vm_map_entry_t start_entry
;
2141 int rv
= KERN_SUCCESS
;
2144 if (kmflags
& KM_KRESERVE
)
2145 count
= vm_map_entry_kreserve(MAP_RESERVE_COUNT
);
2147 count
= vm_map_entry_reserve(MAP_RESERVE_COUNT
);
2149 VM_MAP_RANGE_CHECK(map
, start
, real_end
);
2152 start_entry
= vm_map_clip_range(map
, start
, end
, &count
, MAP_CLIP_NO_HOLES
);
2153 if (start_entry
== NULL
) {
2155 rv
= KERN_INVALID_ADDRESS
;
2158 if ((kmflags
& KM_PAGEABLE
) == 0) {
2162 * 1. Holding the write lock, we create any shadow or zero-fill
2163 * objects that need to be created. Then we clip each map
2164 * entry to the region to be wired and increment its wiring
2165 * count. We create objects before clipping the map entries
2166 * to avoid object proliferation.
2168 * 2. We downgrade to a read lock, and call vm_fault_wire to
2169 * fault in the pages for any newly wired area (wired_count is
2172 * Downgrading to a read lock for vm_fault_wire avoids a
2173 * possible deadlock with another process that may have faulted
2174 * on one of the pages to be wired (it would mark the page busy,
2175 * blocking us, then in turn block on the map lock that we
2176 * hold). Because of problems in the recursive lock package,
2177 * we cannot upgrade to a write lock in vm_map_lookup. Thus,
2178 * any actions that require the write lock must be done
2179 * beforehand. Because we keep the read lock on the map, the
2180 * copy-on-write status of the entries we modify here cannot
2184 entry
= start_entry
;
2185 while ((entry
!= &map
->header
) && (entry
->start
< end
)) {
2187 * Trivial case if the entry is already wired
2189 if (entry
->wired_count
) {
2190 entry
->wired_count
++;
2191 entry
= entry
->next
;
2196 * The entry is being newly wired, we have to setup
2197 * appropriate management structures. A shadow
2198 * object is required for a copy-on-write region,
2199 * or a normal object for a zero-fill region. We
2200 * do not have to do this for entries that point to sub
2201 * maps because we won't hold the lock on the sub map.
2203 if (entry
->maptype
!= VM_MAPTYPE_SUBMAP
) {
2204 int copyflag
= entry
->eflags
& MAP_ENTRY_NEEDS_COPY
;
2206 ((entry
->protection
& VM_PROT_WRITE
) != 0)) {
2207 vm_map_entry_shadow(entry
);
2208 } else if (entry
->object
.vm_object
== NULL
&&
2210 vm_map_entry_allocate_object(entry
);
2214 entry
->wired_count
++;
2215 entry
= entry
->next
;
2223 * HACK HACK HACK HACK
2225 * Unlock the map to avoid deadlocks. The in-transit flag
2226 * protects us from most changes but note that
2227 * clipping may still occur. To prevent clipping from
2228 * occuring after the unlock, except for when we are
2229 * blocking in vm_fault_wire, we must run in a critical
2230 * section, otherwise our accesses to entry->start and
2231 * entry->end could be corrupted. We have to enter the
2232 * critical section prior to unlocking so start_entry does
2233 * not change out from under us at the very beginning of the
2236 * HACK HACK HACK HACK
2241 entry
= start_entry
;
2242 while (entry
!= &map
->header
&& entry
->start
< end
) {
2244 * If vm_fault_wire fails for any page we need to undo
2245 * what has been done. We decrement the wiring count
2246 * for those pages which have not yet been wired (now)
2247 * and unwire those that have (later).
2249 vm_offset_t save_start
= entry
->start
;
2250 vm_offset_t save_end
= entry
->end
;
2252 if (entry
->wired_count
== 1)
2253 rv
= vm_fault_wire(map
, entry
, FALSE
);
2255 CLIP_CHECK_BACK(entry
, save_start
);
2257 KASSERT(entry
->wired_count
== 1, ("wired_count changed unexpectedly"));
2258 entry
->wired_count
= 0;
2259 if (entry
->end
== save_end
)
2261 entry
= entry
->next
;
2262 KASSERT(entry
!= &map
->header
, ("bad entry clip during backout"));
2267 CLIP_CHECK_FWD(entry
, save_end
);
2268 entry
= entry
->next
;
2273 * If a failure occured undo everything by falling through
2274 * to the unwiring code. 'end' has already been adjusted
2278 kmflags
|= KM_PAGEABLE
;
2281 * start_entry is still IN_TRANSITION but may have been
2282 * clipped since vm_fault_wire() unlocks and relocks the
2283 * map. No matter how clipped it has gotten there should
2284 * be a fragment that is on our start boundary.
2286 CLIP_CHECK_BACK(start_entry
, start
);
2289 if (kmflags
& KM_PAGEABLE
) {
2291 * This is the unwiring case. We must first ensure that the
2292 * range to be unwired is really wired down. We know there
2295 entry
= start_entry
;
2296 while ((entry
!= &map
->header
) && (entry
->start
< end
)) {
2297 if (entry
->wired_count
== 0) {
2298 rv
= KERN_INVALID_ARGUMENT
;
2301 entry
= entry
->next
;
2305 * Now decrement the wiring count for each region. If a region
2306 * becomes completely unwired, unwire its physical pages and
2309 entry
= start_entry
;
2310 while ((entry
!= &map
->header
) && (entry
->start
< end
)) {
2311 entry
->wired_count
--;
2312 if (entry
->wired_count
== 0)
2313 vm_fault_unwire(map
, entry
);
2314 entry
= entry
->next
;
2318 vm_map_unclip_range(map
, start_entry
, start
, real_end
, &count
,
2323 if (kmflags
& KM_KRESERVE
)
2324 vm_map_entry_krelease(count
);
2326 vm_map_entry_release(count
);
2331 * vm_map_set_wired_quick()
2333 * Mark a newly allocated address range as wired but do not fault in
2334 * the pages. The caller is expected to load the pages into the object.
2336 * The map must be locked on entry and will remain locked on return.
2339 vm_map_set_wired_quick(vm_map_t map
, vm_offset_t addr
, vm_size_t size
, int *countp
)
2341 vm_map_entry_t scan
;
2342 vm_map_entry_t entry
;
2344 entry
= vm_map_clip_range(map
, addr
, addr
+ size
, countp
, MAP_CLIP_NO_HOLES
);
2345 for (scan
= entry
; scan
!= &map
->header
&& scan
->start
< addr
+ size
; scan
= scan
->next
) {
2346 KKASSERT(entry
->wired_count
== 0);
2347 entry
->wired_count
= 1;
2349 vm_map_unclip_range(map
, entry
, addr
, addr
+ size
, countp
, MAP_CLIP_NO_HOLES
);
2355 * Push any dirty cached pages in the address range to their pager.
2356 * If syncio is TRUE, dirty pages are written synchronously.
2357 * If invalidate is TRUE, any cached pages are freed as well.
2359 * Returns an error if any part of the specified range is not mapped.
2362 vm_map_clean(vm_map_t map
, vm_offset_t start
, vm_offset_t end
, boolean_t syncio
,
2363 boolean_t invalidate
)
2365 vm_map_entry_t current
;
2366 vm_map_entry_t entry
;
2369 vm_ooffset_t offset
;
2371 vm_map_lock_read(map
);
2372 VM_MAP_RANGE_CHECK(map
, start
, end
);
2373 if (!vm_map_lookup_entry(map
, start
, &entry
)) {
2374 vm_map_unlock_read(map
);
2375 return (KERN_INVALID_ADDRESS
);
2378 * Make a first pass to check for holes.
2380 for (current
= entry
; current
->start
< end
; current
= current
->next
) {
2381 if (current
->maptype
== VM_MAPTYPE_SUBMAP
) {
2382 vm_map_unlock_read(map
);
2383 return (KERN_INVALID_ARGUMENT
);
2385 if (end
> current
->end
&&
2386 (current
->next
== &map
->header
||
2387 current
->end
!= current
->next
->start
)) {
2388 vm_map_unlock_read(map
);
2389 return (KERN_INVALID_ADDRESS
);
2394 pmap_remove(vm_map_pmap(map
), start
, end
);
2396 * Make a second pass, cleaning/uncaching pages from the indicated
2399 for (current
= entry
; current
->start
< end
; current
= current
->next
) {
2400 offset
= current
->offset
+ (start
- current
->start
);
2401 size
= (end
<= current
->end
? end
: current
->end
) - start
;
2402 if (current
->maptype
== VM_MAPTYPE_SUBMAP
) {
2404 vm_map_entry_t tentry
;
2407 smap
= current
->object
.sub_map
;
2408 vm_map_lock_read(smap
);
2409 vm_map_lookup_entry(smap
, offset
, &tentry
);
2410 tsize
= tentry
->end
- offset
;
2413 object
= tentry
->object
.vm_object
;
2414 offset
= tentry
->offset
+ (offset
- tentry
->start
);
2415 vm_map_unlock_read(smap
);
2417 object
= current
->object
.vm_object
;
2420 * Note that there is absolutely no sense in writing out
2421 * anonymous objects, so we track down the vnode object
2423 * We invalidate (remove) all pages from the address space
2424 * anyway, for semantic correctness.
2426 * note: certain anonymous maps, such as MAP_NOSYNC maps,
2427 * may start out with a NULL object.
2429 while (object
&& object
->backing_object
) {
2430 offset
+= object
->backing_object_offset
;
2431 object
= object
->backing_object
;
2432 if (object
->size
< OFF_TO_IDX( offset
+ size
))
2433 size
= IDX_TO_OFF(object
->size
) - offset
;
2435 if (object
&& (object
->type
== OBJT_VNODE
) &&
2436 (current
->protection
& VM_PROT_WRITE
)) {
2438 * Flush pages if writing is allowed, invalidate them
2439 * if invalidation requested. Pages undergoing I/O
2440 * will be ignored by vm_object_page_remove().
2442 * We cannot lock the vnode and then wait for paging
2443 * to complete without deadlocking against vm_fault.
2444 * Instead we simply call vm_object_page_remove() and
2445 * allow it to block internally on a page-by-page
2446 * basis when it encounters pages undergoing async
2451 vm_object_reference(object
);
2452 vn_lock(object
->handle
, LK_EXCLUSIVE
| LK_RETRY
);
2453 flags
= (syncio
|| invalidate
) ? OBJPC_SYNC
: 0;
2454 flags
|= invalidate
? OBJPC_INVAL
: 0;
2457 * When operating on a virtual page table just
2458 * flush the whole object. XXX we probably ought
2461 switch(current
->maptype
) {
2462 case VM_MAPTYPE_NORMAL
:
2463 vm_object_page_clean(object
,
2465 OFF_TO_IDX(offset
+ size
+ PAGE_MASK
),
2468 case VM_MAPTYPE_VPAGETABLE
:
2469 vm_object_page_clean(object
, 0, 0, flags
);
2472 vn_unlock(((struct vnode
*)object
->handle
));
2473 vm_object_deallocate(object
);
2475 if (object
&& invalidate
&&
2476 ((object
->type
== OBJT_VNODE
) ||
2477 (object
->type
== OBJT_DEVICE
))) {
2479 (object
->type
== OBJT_DEVICE
) ? FALSE
: TRUE
;
2480 vm_object_reference(object
);
2481 switch(current
->maptype
) {
2482 case VM_MAPTYPE_NORMAL
:
2483 vm_object_page_remove(object
,
2485 OFF_TO_IDX(offset
+ size
+ PAGE_MASK
),
2488 case VM_MAPTYPE_VPAGETABLE
:
2489 vm_object_page_remove(object
, 0, 0, clean_only
);
2492 vm_object_deallocate(object
);
2497 vm_map_unlock_read(map
);
2498 return (KERN_SUCCESS
);
2502 * vm_map_entry_unwire: [ internal use only ]
2504 * Make the region specified by this entry pageable.
2506 * The map in question should be locked.
2507 * [This is the reason for this routine's existence.]
2510 vm_map_entry_unwire(vm_map_t map
, vm_map_entry_t entry
)
2512 entry
->eflags
&= ~MAP_ENTRY_USER_WIRED
;
2513 entry
->wired_count
= 0;
2514 vm_fault_unwire(map
, entry
);
2518 * vm_map_entry_delete: [ internal use only ]
2520 * Deallocate the given entry from the target map.
2523 vm_map_entry_delete(vm_map_t map
, vm_map_entry_t entry
, int *countp
)
2525 vm_map_entry_unlink(map
, entry
);
2526 map
->size
-= entry
->end
- entry
->start
;
2528 switch(entry
->maptype
) {
2529 case VM_MAPTYPE_NORMAL
:
2530 case VM_MAPTYPE_VPAGETABLE
:
2531 vm_object_deallocate(entry
->object
.vm_object
);
2537 vm_map_entry_dispose(map
, entry
, countp
);
2541 * vm_map_delete: [ internal use only ]
2543 * Deallocates the given address range from the target
2547 vm_map_delete(vm_map_t map
, vm_offset_t start
, vm_offset_t end
, int *countp
)
2550 vm_map_entry_t entry
;
2551 vm_map_entry_t first_entry
;
2555 * Find the start of the region, and clip it. Set entry to point
2556 * at the first record containing the requested address or, if no
2557 * such record exists, the next record with a greater address. The
2558 * loop will run from this point until a record beyond the termination
2559 * address is encountered.
2561 * map->hint must be adjusted to not point to anything we delete,
2562 * so set it to the entry prior to the one being deleted.
2564 * GGG see other GGG comment.
2566 if (vm_map_lookup_entry(map
, start
, &first_entry
)) {
2567 entry
= first_entry
;
2568 vm_map_clip_start(map
, entry
, start
, countp
);
2569 map
->hint
= entry
->prev
; /* possible problem XXX */
2571 map
->hint
= first_entry
; /* possible problem XXX */
2572 entry
= first_entry
->next
;
2576 * If a hole opens up prior to the current first_free then
2577 * adjust first_free. As with map->hint, map->first_free
2578 * cannot be left set to anything we might delete.
2580 if (entry
== &map
->header
) {
2581 map
->first_free
= &map
->header
;
2582 } else if (map
->first_free
->start
>= start
) {
2583 map
->first_free
= entry
->prev
;
2587 * Step through all entries in this region
2590 while ((entry
!= &map
->header
) && (entry
->start
< end
)) {
2591 vm_map_entry_t next
;
2593 vm_pindex_t offidxstart
, offidxend
, count
;
2596 * If we hit an in-transition entry we have to sleep and
2597 * retry. It's easier (and not really slower) to just retry
2598 * since this case occurs so rarely and the hint is already
2599 * pointing at the right place. We have to reset the
2600 * start offset so as not to accidently delete an entry
2601 * another process just created in vacated space.
2603 if (entry
->eflags
& MAP_ENTRY_IN_TRANSITION
) {
2604 entry
->eflags
|= MAP_ENTRY_NEEDS_WAKEUP
;
2605 start
= entry
->start
;
2606 ++mycpu
->gd_cnt
.v_intrans_coll
;
2607 ++mycpu
->gd_cnt
.v_intrans_wait
;
2608 vm_map_transition_wait(map
);
2611 vm_map_clip_end(map
, entry
, end
, countp
);
2617 offidxstart
= OFF_TO_IDX(entry
->offset
);
2618 count
= OFF_TO_IDX(e
- s
);
2619 object
= entry
->object
.vm_object
;
2622 * Unwire before removing addresses from the pmap; otherwise,
2623 * unwiring will put the entries back in the pmap.
2625 if (entry
->wired_count
!= 0)
2626 vm_map_entry_unwire(map
, entry
);
2628 offidxend
= offidxstart
+ count
;
2630 if (object
== &kernel_object
) {
2631 vm_object_page_remove(object
, offidxstart
, offidxend
, FALSE
);
2633 pmap_remove(map
->pmap
, s
, e
);
2634 if (object
!= NULL
&&
2635 object
->ref_count
!= 1 &&
2636 (object
->flags
& (OBJ_NOSPLIT
|OBJ_ONEMAPPING
)) == OBJ_ONEMAPPING
&&
2637 (object
->type
== OBJT_DEFAULT
|| object
->type
== OBJT_SWAP
)) {
2638 vm_object_collapse(object
);
2639 vm_object_page_remove(object
, offidxstart
, offidxend
, FALSE
);
2640 if (object
->type
== OBJT_SWAP
) {
2641 swap_pager_freespace(object
, offidxstart
, count
);
2643 if (offidxend
>= object
->size
&&
2644 offidxstart
< object
->size
) {
2645 object
->size
= offidxstart
;
2651 * Delete the entry (which may delete the object) only after
2652 * removing all pmap entries pointing to its pages.
2653 * (Otherwise, its page frames may be reallocated, and any
2654 * modify bits will be set in the wrong object!)
2656 vm_map_entry_delete(map
, entry
, countp
);
2659 return (KERN_SUCCESS
);
2665 * Remove the given address range from the target map.
2666 * This is the exported form of vm_map_delete.
2669 vm_map_remove(vm_map_t map
, vm_offset_t start
, vm_offset_t end
)
2674 count
= vm_map_entry_reserve(MAP_RESERVE_COUNT
);
2676 VM_MAP_RANGE_CHECK(map
, start
, end
);
2677 result
= vm_map_delete(map
, start
, end
, &count
);
2679 vm_map_entry_release(count
);
2685 * vm_map_check_protection:
2687 * Assert that the target map allows the specified
2688 * privilege on the entire address region given.
2689 * The entire region must be allocated.
2692 vm_map_check_protection(vm_map_t map
, vm_offset_t start
, vm_offset_t end
,
2693 vm_prot_t protection
)
2695 vm_map_entry_t entry
;
2696 vm_map_entry_t tmp_entry
;
2698 if (!vm_map_lookup_entry(map
, start
, &tmp_entry
)) {
2703 while (start
< end
) {
2704 if (entry
== &map
->header
) {
2711 if (start
< entry
->start
) {
2715 * Check protection associated with entry.
2718 if ((entry
->protection
& protection
) != protection
) {
2721 /* go to next entry */
2724 entry
= entry
->next
;
2730 * Split the pages in a map entry into a new object. This affords
2731 * easier removal of unused pages, and keeps object inheritance from
2732 * being a negative impact on memory usage.
2735 vm_map_split(vm_map_entry_t entry
)
2738 vm_object_t orig_object
, new_object
, source
;
2740 vm_pindex_t offidxstart
, offidxend
, idx
;
2742 vm_ooffset_t offset
;
2744 orig_object
= entry
->object
.vm_object
;
2745 if (orig_object
->type
!= OBJT_DEFAULT
&& orig_object
->type
!= OBJT_SWAP
)
2747 if (orig_object
->ref_count
<= 1)
2750 offset
= entry
->offset
;
2754 offidxstart
= OFF_TO_IDX(offset
);
2755 offidxend
= offidxstart
+ OFF_TO_IDX(e
- s
);
2756 size
= offidxend
- offidxstart
;
2758 new_object
= vm_pager_allocate(orig_object
->type
, NULL
,
2759 IDX_TO_OFF(size
), VM_PROT_ALL
, 0);
2760 if (new_object
== NULL
)
2763 source
= orig_object
->backing_object
;
2764 if (source
!= NULL
) {
2765 vm_object_reference(source
); /* Referenced by new_object */
2766 LIST_INSERT_HEAD(&source
->shadow_head
,
2767 new_object
, shadow_list
);
2768 vm_object_clear_flag(source
, OBJ_ONEMAPPING
);
2769 new_object
->backing_object_offset
=
2770 orig_object
->backing_object_offset
+ IDX_TO_OFF(offidxstart
);
2771 new_object
->backing_object
= source
;
2772 source
->shadow_count
++;
2773 source
->generation
++;
2776 for (idx
= 0; idx
< size
; idx
++) {
2780 * A critical section is required to avoid a race between
2781 * the lookup and an interrupt/unbusy/free and our busy
2786 m
= vm_page_lookup(orig_object
, offidxstart
+ idx
);
2793 * We must wait for pending I/O to complete before we can
2796 * We do not have to VM_PROT_NONE the page as mappings should
2797 * not be changed by this operation.
2799 if (vm_page_sleep_busy(m
, TRUE
, "spltwt"))
2802 vm_page_rename(m
, new_object
, idx
);
2803 /* page automatically made dirty by rename and cache handled */
2808 if (orig_object
->type
== OBJT_SWAP
) {
2809 vm_object_pip_add(orig_object
, 1);
2811 * copy orig_object pages into new_object
2812 * and destroy unneeded pages in
2815 swap_pager_copy(orig_object
, new_object
, offidxstart
, 0);
2816 vm_object_pip_wakeup(orig_object
);
2820 * Wakeup the pages we played with. No spl protection is needed
2821 * for a simple wakeup.
2823 for (idx
= 0; idx
< size
; idx
++) {
2824 m
= vm_page_lookup(new_object
, idx
);
2829 entry
->object
.vm_object
= new_object
;
2830 entry
->offset
= 0LL;
2831 vm_object_deallocate(orig_object
);
2835 * vm_map_copy_entry:
2837 * Copies the contents of the source entry to the destination
2838 * entry. The entries *must* be aligned properly.
2841 vm_map_copy_entry(vm_map_t src_map
, vm_map_t dst_map
,
2842 vm_map_entry_t src_entry
, vm_map_entry_t dst_entry
)
2844 vm_object_t src_object
;
2846 if (dst_entry
->maptype
== VM_MAPTYPE_SUBMAP
)
2848 if (src_entry
->maptype
== VM_MAPTYPE_SUBMAP
)
2851 if (src_entry
->wired_count
== 0) {
2853 * If the source entry is marked needs_copy, it is already
2856 if ((src_entry
->eflags
& MAP_ENTRY_NEEDS_COPY
) == 0) {
2857 pmap_protect(src_map
->pmap
,
2860 src_entry
->protection
& ~VM_PROT_WRITE
);
2864 * Make a copy of the object.
2866 if ((src_object
= src_entry
->object
.vm_object
) != NULL
) {
2867 if ((src_object
->handle
== NULL
) &&
2868 (src_object
->type
== OBJT_DEFAULT
||
2869 src_object
->type
== OBJT_SWAP
)) {
2870 vm_object_collapse(src_object
);
2871 if ((src_object
->flags
& (OBJ_NOSPLIT
|OBJ_ONEMAPPING
)) == OBJ_ONEMAPPING
) {
2872 vm_map_split(src_entry
);
2873 src_object
= src_entry
->object
.vm_object
;
2877 vm_object_reference(src_object
);
2878 vm_object_clear_flag(src_object
, OBJ_ONEMAPPING
);
2879 dst_entry
->object
.vm_object
= src_object
;
2880 src_entry
->eflags
|= (MAP_ENTRY_COW
|MAP_ENTRY_NEEDS_COPY
);
2881 dst_entry
->eflags
|= (MAP_ENTRY_COW
|MAP_ENTRY_NEEDS_COPY
);
2882 dst_entry
->offset
= src_entry
->offset
;
2884 dst_entry
->object
.vm_object
= NULL
;
2885 dst_entry
->offset
= 0;
2888 pmap_copy(dst_map
->pmap
, src_map
->pmap
, dst_entry
->start
,
2889 dst_entry
->end
- dst_entry
->start
, src_entry
->start
);
2892 * Of course, wired down pages can't be set copy-on-write.
2893 * Cause wired pages to be copied into the new map by
2894 * simulating faults (the new pages are pageable)
2896 vm_fault_copy_entry(dst_map
, src_map
, dst_entry
, src_entry
);
2902 * Create a new process vmspace structure and vm_map
2903 * based on those of an existing process. The new map
2904 * is based on the old map, according to the inheritance
2905 * values on the regions in that map.
2907 * The source map must not be locked.
2910 vmspace_fork(struct vmspace
*vm1
)
2912 struct vmspace
*vm2
;
2913 vm_map_t old_map
= &vm1
->vm_map
;
2915 vm_map_entry_t old_entry
;
2916 vm_map_entry_t new_entry
;
2920 vm_map_lock(old_map
);
2921 old_map
->infork
= 1;
2924 * XXX Note: upcalls are not copied.
2926 vm2
= vmspace_alloc(old_map
->min_offset
, old_map
->max_offset
);
2927 bcopy(&vm1
->vm_startcopy
, &vm2
->vm_startcopy
,
2928 (caddr_t
)&vm1
->vm_endcopy
- (caddr_t
)&vm1
->vm_startcopy
);
2929 new_map
= &vm2
->vm_map
; /* XXX */
2930 new_map
->timestamp
= 1;
2933 old_entry
= old_map
->header
.next
;
2934 while (old_entry
!= &old_map
->header
) {
2936 old_entry
= old_entry
->next
;
2939 count
= vm_map_entry_reserve(count
+ MAP_RESERVE_COUNT
);
2941 old_entry
= old_map
->header
.next
;
2942 while (old_entry
!= &old_map
->header
) {
2943 if (old_entry
->maptype
== VM_MAPTYPE_SUBMAP
)
2944 panic("vm_map_fork: encountered a submap");
2946 switch (old_entry
->inheritance
) {
2947 case VM_INHERIT_NONE
:
2950 case VM_INHERIT_SHARE
:
2952 * Clone the entry, creating the shared object if
2955 object
= old_entry
->object
.vm_object
;
2956 if (object
== NULL
) {
2957 vm_map_entry_allocate_object(old_entry
);
2958 object
= old_entry
->object
.vm_object
;
2962 * Add the reference before calling vm_map_entry_shadow
2963 * to insure that a shadow object is created.
2965 vm_object_reference(object
);
2966 if (old_entry
->eflags
& MAP_ENTRY_NEEDS_COPY
) {
2967 vm_map_entry_shadow(old_entry
);
2968 /* Transfer the second reference too. */
2969 vm_object_reference(
2970 old_entry
->object
.vm_object
);
2971 vm_object_deallocate(object
);
2972 object
= old_entry
->object
.vm_object
;
2974 vm_object_clear_flag(object
, OBJ_ONEMAPPING
);
2977 * Clone the entry, referencing the shared object.
2979 new_entry
= vm_map_entry_create(new_map
, &count
);
2980 *new_entry
= *old_entry
;
2981 new_entry
->eflags
&= ~MAP_ENTRY_USER_WIRED
;
2982 new_entry
->wired_count
= 0;
2985 * Insert the entry into the new map -- we know we're
2986 * inserting at the end of the new map.
2989 vm_map_entry_link(new_map
, new_map
->header
.prev
,
2993 * Update the physical map
2996 pmap_copy(new_map
->pmap
, old_map
->pmap
,
2998 (old_entry
->end
- old_entry
->start
),
3002 case VM_INHERIT_COPY
:
3004 * Clone the entry and link into the map.
3006 new_entry
= vm_map_entry_create(new_map
, &count
);
3007 *new_entry
= *old_entry
;
3008 new_entry
->eflags
&= ~MAP_ENTRY_USER_WIRED
;
3009 new_entry
->wired_count
= 0;
3010 new_entry
->object
.vm_object
= NULL
;
3011 vm_map_entry_link(new_map
, new_map
->header
.prev
,
3013 vm_map_copy_entry(old_map
, new_map
, old_entry
,
3017 old_entry
= old_entry
->next
;
3020 new_map
->size
= old_map
->size
;
3021 old_map
->infork
= 0;
3022 vm_map_unlock(old_map
);
3023 vm_map_entry_release(count
);
3029 vm_map_stack (vm_map_t map
, vm_offset_t addrbos
, vm_size_t max_ssize
,
3030 int flags
, vm_prot_t prot
, vm_prot_t max
, int cow
)
3032 vm_map_entry_t prev_entry
;
3033 vm_map_entry_t new_stack_entry
;
3034 vm_size_t init_ssize
;
3037 vm_offset_t tmpaddr
;
3039 cow
|= MAP_IS_STACK
;
3041 if (max_ssize
< sgrowsiz
)
3042 init_ssize
= max_ssize
;
3044 init_ssize
= sgrowsiz
;
3046 count
= vm_map_entry_reserve(MAP_RESERVE_COUNT
);
3050 * Find space for the mapping
3052 if ((flags
& (MAP_FIXED
| MAP_TRYFIXED
)) == 0) {
3053 if (vm_map_findspace(map
, addrbos
, max_ssize
, 1,
3056 vm_map_entry_release(count
);
3057 return (KERN_NO_SPACE
);
3062 /* If addr is already mapped, no go */
3063 if (vm_map_lookup_entry(map
, addrbos
, &prev_entry
)) {
3065 vm_map_entry_release(count
);
3066 return (KERN_NO_SPACE
);
3070 /* XXX already handled by kern_mmap() */
3071 /* If we would blow our VMEM resource limit, no go */
3072 if (map
->size
+ init_ssize
>
3073 curproc
->p_rlimit
[RLIMIT_VMEM
].rlim_cur
) {
3075 vm_map_entry_release(count
);
3076 return (KERN_NO_SPACE
);
3081 * If we can't accomodate max_ssize in the current mapping,
3082 * no go. However, we need to be aware that subsequent user
3083 * mappings might map into the space we have reserved for
3084 * stack, and currently this space is not protected.
3086 * Hopefully we will at least detect this condition
3087 * when we try to grow the stack.
3089 if ((prev_entry
->next
!= &map
->header
) &&
3090 (prev_entry
->next
->start
< addrbos
+ max_ssize
)) {
3092 vm_map_entry_release(count
);
3093 return (KERN_NO_SPACE
);
3097 * We initially map a stack of only init_ssize. We will
3098 * grow as needed later. Since this is to be a grow
3099 * down stack, we map at the top of the range.
3101 * Note: we would normally expect prot and max to be
3102 * VM_PROT_ALL, and cow to be 0. Possibly we should
3103 * eliminate these as input parameters, and just
3104 * pass these values here in the insert call.
3106 rv
= vm_map_insert(map
, &count
,
3107 NULL
, 0, addrbos
+ max_ssize
- init_ssize
,
3108 addrbos
+ max_ssize
,
3113 /* Now set the avail_ssize amount */
3114 if (rv
== KERN_SUCCESS
) {
3115 if (prev_entry
!= &map
->header
)
3116 vm_map_clip_end(map
, prev_entry
, addrbos
+ max_ssize
- init_ssize
, &count
);
3117 new_stack_entry
= prev_entry
->next
;
3118 if (new_stack_entry
->end
!= addrbos
+ max_ssize
||
3119 new_stack_entry
->start
!= addrbos
+ max_ssize
- init_ssize
)
3120 panic ("Bad entry start/end for new stack entry");
3122 new_stack_entry
->aux
.avail_ssize
= max_ssize
- init_ssize
;
3126 vm_map_entry_release(count
);
3130 /* Attempts to grow a vm stack entry. Returns KERN_SUCCESS if the
3131 * desired address is already mapped, or if we successfully grow
3132 * the stack. Also returns KERN_SUCCESS if addr is outside the
3133 * stack range (this is strange, but preserves compatibility with
3134 * the grow function in vm_machdep.c).
3137 vm_map_growstack (struct proc
*p
, vm_offset_t addr
)
3139 vm_map_entry_t prev_entry
;
3140 vm_map_entry_t stack_entry
;
3141 vm_map_entry_t new_stack_entry
;
3142 struct vmspace
*vm
= p
->p_vmspace
;
3143 vm_map_t map
= &vm
->vm_map
;
3146 int rv
= KERN_SUCCESS
;
3148 int use_read_lock
= 1;
3151 count
= vm_map_entry_reserve(MAP_RESERVE_COUNT
);
3154 vm_map_lock_read(map
);
3158 /* If addr is already in the entry range, no need to grow.*/
3159 if (vm_map_lookup_entry(map
, addr
, &prev_entry
))
3162 if ((stack_entry
= prev_entry
->next
) == &map
->header
)
3164 if (prev_entry
== &map
->header
)
3165 end
= stack_entry
->start
- stack_entry
->aux
.avail_ssize
;
3167 end
= prev_entry
->end
;
3170 * This next test mimics the old grow function in vm_machdep.c.
3171 * It really doesn't quite make sense, but we do it anyway
3172 * for compatibility.
3174 * If not growable stack, return success. This signals the
3175 * caller to proceed as he would normally with normal vm.
3177 if (stack_entry
->aux
.avail_ssize
< 1 ||
3178 addr
>= stack_entry
->start
||
3179 addr
< stack_entry
->start
- stack_entry
->aux
.avail_ssize
) {
3183 /* Find the minimum grow amount */
3184 grow_amount
= roundup (stack_entry
->start
- addr
, PAGE_SIZE
);
3185 if (grow_amount
> stack_entry
->aux
.avail_ssize
) {
3191 * If there is no longer enough space between the entries
3192 * nogo, and adjust the available space. Note: this
3193 * should only happen if the user has mapped into the
3194 * stack area after the stack was created, and is
3195 * probably an error.
3197 * This also effectively destroys any guard page the user
3198 * might have intended by limiting the stack size.
3200 if (grow_amount
> stack_entry
->start
- end
) {
3201 if (use_read_lock
&& vm_map_lock_upgrade(map
)) {
3206 stack_entry
->aux
.avail_ssize
= stack_entry
->start
- end
;
3211 is_procstack
= addr
>= (vm_offset_t
)vm
->vm_maxsaddr
;
3213 /* If this is the main process stack, see if we're over the
3216 if (is_procstack
&& (ctob(vm
->vm_ssize
) + grow_amount
>
3217 p
->p_rlimit
[RLIMIT_STACK
].rlim_cur
)) {
3222 /* Round up the grow amount modulo SGROWSIZ */
3223 grow_amount
= roundup (grow_amount
, sgrowsiz
);
3224 if (grow_amount
> stack_entry
->aux
.avail_ssize
) {
3225 grow_amount
= stack_entry
->aux
.avail_ssize
;
3227 if (is_procstack
&& (ctob(vm
->vm_ssize
) + grow_amount
>
3228 p
->p_rlimit
[RLIMIT_STACK
].rlim_cur
)) {
3229 grow_amount
= p
->p_rlimit
[RLIMIT_STACK
].rlim_cur
-
3233 /* If we would blow our VMEM resource limit, no go */
3234 if (map
->size
+ grow_amount
> p
->p_rlimit
[RLIMIT_VMEM
].rlim_cur
) {
3239 if (use_read_lock
&& vm_map_lock_upgrade(map
)) {
3245 /* Get the preliminary new entry start value */
3246 addr
= stack_entry
->start
- grow_amount
;
3248 /* If this puts us into the previous entry, cut back our growth
3249 * to the available space. Also, see the note above.
3252 stack_entry
->aux
.avail_ssize
= stack_entry
->start
- end
;
3256 rv
= vm_map_insert(map
, &count
,
3257 NULL
, 0, addr
, stack_entry
->start
,
3259 VM_PROT_ALL
, VM_PROT_ALL
,
3262 /* Adjust the available stack space by the amount we grew. */
3263 if (rv
== KERN_SUCCESS
) {
3264 if (prev_entry
!= &map
->header
)
3265 vm_map_clip_end(map
, prev_entry
, addr
, &count
);
3266 new_stack_entry
= prev_entry
->next
;
3267 if (new_stack_entry
->end
!= stack_entry
->start
||
3268 new_stack_entry
->start
!= addr
)
3269 panic ("Bad stack grow start/end in new stack entry");
3271 new_stack_entry
->aux
.avail_ssize
=
3272 stack_entry
->aux
.avail_ssize
-
3273 (new_stack_entry
->end
- new_stack_entry
->start
);
3275 vm
->vm_ssize
+= btoc(new_stack_entry
->end
-
3276 new_stack_entry
->start
);
3282 vm_map_unlock_read(map
);
3285 vm_map_entry_release(count
);
3290 * Unshare the specified VM space for exec. If other processes are
3291 * mapped to it, then create a new one. The new vmspace is null.
3294 vmspace_exec(struct proc
*p
, struct vmspace
*vmcopy
)
3296 struct vmspace
*oldvmspace
= p
->p_vmspace
;
3297 struct vmspace
*newvmspace
;
3298 vm_map_t map
= &p
->p_vmspace
->vm_map
;
3301 * If we are execing a resident vmspace we fork it, otherwise
3302 * we create a new vmspace. Note that exitingcnt and upcalls
3303 * are not copied to the new vmspace.
3306 newvmspace
= vmspace_fork(vmcopy
);
3308 newvmspace
= vmspace_alloc(map
->min_offset
, map
->max_offset
);
3309 bcopy(&oldvmspace
->vm_startcopy
, &newvmspace
->vm_startcopy
,
3310 (caddr_t
)&oldvmspace
->vm_endcopy
-
3311 (caddr_t
)&oldvmspace
->vm_startcopy
);
3315 * Finish initializing the vmspace before assigning it
3316 * to the process. The vmspace will become the current vmspace
3319 pmap_pinit2(vmspace_pmap(newvmspace
));
3320 pmap_replacevm(p
, newvmspace
, 0);
3321 sysref_put(&oldvmspace
->vm_sysref
);
3325 * Unshare the specified VM space for forcing COW. This
3326 * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
3328 * The exitingcnt test is not strictly necessary but has been
3329 * included for code sanity (to make the code a bit more deterministic).
3333 vmspace_unshare(struct proc
*p
)
3335 struct vmspace
*oldvmspace
= p
->p_vmspace
;
3336 struct vmspace
*newvmspace
;
3338 if (oldvmspace
->vm_sysref
.refcnt
== 1 && oldvmspace
->vm_exitingcnt
== 0)
3340 newvmspace
= vmspace_fork(oldvmspace
);
3341 pmap_pinit2(vmspace_pmap(newvmspace
));
3342 pmap_replacevm(p
, newvmspace
, 0);
3343 sysref_put(&oldvmspace
->vm_sysref
);
3349 * Finds the VM object, offset, and
3350 * protection for a given virtual address in the
3351 * specified map, assuming a page fault of the
3354 * Leaves the map in question locked for read; return
3355 * values are guaranteed until a vm_map_lookup_done
3356 * call is performed. Note that the map argument
3357 * is in/out; the returned map must be used in
3358 * the call to vm_map_lookup_done.
3360 * A handle (out_entry) is returned for use in
3361 * vm_map_lookup_done, to make that fast.
3363 * If a lookup is requested with "write protection"
3364 * specified, the map may be changed to perform virtual
3365 * copying operations, although the data referenced will
3369 vm_map_lookup(vm_map_t
*var_map
, /* IN/OUT */
3371 vm_prot_t fault_typea
,
3372 vm_map_entry_t
*out_entry
, /* OUT */
3373 vm_object_t
*object
, /* OUT */
3374 vm_pindex_t
*pindex
, /* OUT */
3375 vm_prot_t
*out_prot
, /* OUT */
3376 boolean_t
*wired
) /* OUT */
3378 vm_map_entry_t entry
;
3379 vm_map_t map
= *var_map
;
3381 vm_prot_t fault_type
= fault_typea
;
3382 int use_read_lock
= 1;
3383 int rv
= KERN_SUCCESS
;
3387 vm_map_lock_read(map
);
3392 * If the map has an interesting hint, try it before calling full
3393 * blown lookup routine.
3398 if ((entry
== &map
->header
) ||
3399 (vaddr
< entry
->start
) || (vaddr
>= entry
->end
)) {
3400 vm_map_entry_t tmp_entry
;
3403 * Entry was either not a valid hint, or the vaddr was not
3404 * contained in the entry, so do a full lookup.
3406 if (!vm_map_lookup_entry(map
, vaddr
, &tmp_entry
)) {
3407 rv
= KERN_INVALID_ADDRESS
;
3418 if (entry
->maptype
== VM_MAPTYPE_SUBMAP
) {
3419 vm_map_t old_map
= map
;
3421 *var_map
= map
= entry
->object
.sub_map
;
3423 vm_map_unlock_read(old_map
);
3425 vm_map_unlock(old_map
);
3431 * Check whether this task is allowed to have this page.
3432 * Note the special case for MAP_ENTRY_COW
3433 * pages with an override. This is to implement a forced
3434 * COW for debuggers.
3437 if (fault_type
& VM_PROT_OVERRIDE_WRITE
)
3438 prot
= entry
->max_protection
;
3440 prot
= entry
->protection
;
3442 fault_type
&= (VM_PROT_READ
|VM_PROT_WRITE
|VM_PROT_EXECUTE
);
3443 if ((fault_type
& prot
) != fault_type
) {
3444 rv
= KERN_PROTECTION_FAILURE
;
3448 if ((entry
->eflags
& MAP_ENTRY_USER_WIRED
) &&
3449 (entry
->eflags
& MAP_ENTRY_COW
) &&
3450 (fault_type
& VM_PROT_WRITE
) &&
3451 (fault_typea
& VM_PROT_OVERRIDE_WRITE
) == 0) {
3452 rv
= KERN_PROTECTION_FAILURE
;
3457 * If this page is not pageable, we have to get it for all possible
3460 *wired
= (entry
->wired_count
!= 0);
3462 prot
= fault_type
= entry
->protection
;
3465 * Virtual page tables may need to update the accessed (A) bit
3466 * in a page table entry. Upgrade the fault to a write fault for
3467 * that case if the map will support it. If the map does not support
3468 * it the page table entry simply will not be updated.
3470 if (entry
->maptype
== VM_MAPTYPE_VPAGETABLE
) {
3471 if (prot
& VM_PROT_WRITE
)
3472 fault_type
|= VM_PROT_WRITE
;
3476 * If the entry was copy-on-write, we either ...
3478 if (entry
->eflags
& MAP_ENTRY_NEEDS_COPY
) {
3480 * If we want to write the page, we may as well handle that
3481 * now since we've got the map locked.
3483 * If we don't need to write the page, we just demote the
3484 * permissions allowed.
3487 if (fault_type
& VM_PROT_WRITE
) {
3489 * Make a new object, and place it in the object
3490 * chain. Note that no new references have appeared
3491 * -- one just moved from the map to the new
3495 if (use_read_lock
&& vm_map_lock_upgrade(map
)) {
3501 vm_map_entry_shadow(entry
);
3504 * We're attempting to read a copy-on-write page --
3505 * don't allow writes.
3508 prot
&= ~VM_PROT_WRITE
;
3513 * Create an object if necessary.
3515 if (entry
->object
.vm_object
== NULL
&&
3517 if (use_read_lock
&& vm_map_lock_upgrade(map
)) {
3522 vm_map_entry_allocate_object(entry
);
3526 * Return the object/offset from this entry. If the entry was
3527 * copy-on-write or empty, it has been fixed up.
3530 *pindex
= OFF_TO_IDX((vaddr
- entry
->start
) + entry
->offset
);
3531 *object
= entry
->object
.vm_object
;
3534 * Return whether this is the only map sharing this data. On
3535 * success we return with a read lock held on the map. On failure
3536 * we return with the map unlocked.
3540 if (rv
== KERN_SUCCESS
) {
3541 if (use_read_lock
== 0)
3542 vm_map_lock_downgrade(map
);
3543 } else if (use_read_lock
) {
3544 vm_map_unlock_read(map
);
3552 * vm_map_lookup_done:
3554 * Releases locks acquired by a vm_map_lookup
3555 * (according to the handle returned by that lookup).
3559 vm_map_lookup_done(vm_map_t map
, vm_map_entry_t entry
, int count
)
3562 * Unlock the main-level map
3564 vm_map_unlock_read(map
);
3566 vm_map_entry_release(count
);
3569 #include "opt_ddb.h"
3571 #include <sys/kernel.h>
3573 #include <ddb/ddb.h>
3576 * vm_map_print: [ debug ]
3578 DB_SHOW_COMMAND(map
, vm_map_print
)
3581 /* XXX convert args. */
3582 vm_map_t map
= (vm_map_t
)addr
;
3583 boolean_t full
= have_addr
;
3585 vm_map_entry_t entry
;
3587 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
3589 (void *)map
->pmap
, map
->nentries
, map
->timestamp
);
3592 if (!full
&& db_indent
)
3596 for (entry
= map
->header
.next
; entry
!= &map
->header
;
3597 entry
= entry
->next
) {
3598 db_iprintf("map entry %p: start=%p, end=%p\n",
3599 (void *)entry
, (void *)entry
->start
, (void *)entry
->end
);
3602 static char *inheritance_name
[4] =
3603 {"share", "copy", "none", "donate_copy"};
3605 db_iprintf(" prot=%x/%x/%s",
3607 entry
->max_protection
,
3608 inheritance_name
[(int)(unsigned char)entry
->inheritance
]);
3609 if (entry
->wired_count
!= 0)
3610 db_printf(", wired");
3612 if (entry
->maptype
== VM_MAPTYPE_SUBMAP
) {
3613 /* XXX no %qd in kernel. Truncate entry->offset. */
3614 db_printf(", share=%p, offset=0x%lx\n",
3615 (void *)entry
->object
.sub_map
,
3616 (long)entry
->offset
);
3618 if ((entry
->prev
== &map
->header
) ||
3619 (entry
->prev
->object
.sub_map
!=
3620 entry
->object
.sub_map
)) {
3622 vm_map_print((db_expr_t
)(intptr_t)
3623 entry
->object
.sub_map
,
3628 /* XXX no %qd in kernel. Truncate entry->offset. */
3629 db_printf(", object=%p, offset=0x%lx",
3630 (void *)entry
->object
.vm_object
,
3631 (long)entry
->offset
);
3632 if (entry
->eflags
& MAP_ENTRY_COW
)
3633 db_printf(", copy (%s)",
3634 (entry
->eflags
& MAP_ENTRY_NEEDS_COPY
) ? "needed" : "done");
3638 if ((entry
->prev
== &map
->header
) ||
3639 (entry
->prev
->object
.vm_object
!=
3640 entry
->object
.vm_object
)) {
3642 vm_object_print((db_expr_t
)(intptr_t)
3643 entry
->object
.vm_object
,
3656 DB_SHOW_COMMAND(procvm
, procvm
)
3661 p
= (struct proc
*) addr
;
3666 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
3667 (void *)p
, (void *)p
->p_vmspace
, (void *)&p
->p_vmspace
->vm_map
,
3668 (void *)vmspace_pmap(p
->p_vmspace
));
3670 vm_map_print((db_expr_t
)(intptr_t)&p
->p_vmspace
->vm_map
, 1, 0, NULL
);