4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * The Mach Operating System project at Carnegie-Mellon University.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * from: @(#)vm_map.c 8.3 (Berkeley) 1/12/94
37 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
38 * All rights reserved.
40 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
42 * Permission to use, copy, modify and distribute this software and
43 * its documentation is hereby granted, provided that both the copyright
44 * notice and this permission notice appear in all copies of the
45 * software, derivative works or modified versions, and any portions
46 * thereof, and that both notices appear in supporting documentation.
48 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
49 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
50 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
52 * Carnegie Mellon requests users of this software to return to
54 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
55 * School of Computer Science
56 * Carnegie Mellon University
57 * Pittsburgh PA 15213-3890
59 * any improvements or extensions that they make and grant Carnegie the
60 * rights to redistribute these changes.
62 * $FreeBSD: src/sys/vm/vm_map.c,v 1.187.2.19 2003/05/27 00:47:02 alc Exp $
66 * Virtual memory mapping module.
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/kernel.h>
73 #include <sys/serialize.h>
75 #include <sys/vmmeter.h>
77 #include <sys/vnode.h>
78 #include <sys/resourcevar.h>
81 #include <sys/malloc.h>
82 #include <sys/objcache.h>
85 #include <vm/vm_param.h>
87 #include <vm/vm_map.h>
88 #include <vm/vm_page.h>
89 #include <vm/vm_object.h>
90 #include <vm/vm_pager.h>
91 #include <vm/vm_kern.h>
92 #include <vm/vm_extern.h>
93 #include <vm/swap_pager.h>
94 #include <vm/vm_zone.h>
96 #include <sys/thread2.h>
97 #include <sys/random.h>
98 #include <sys/sysctl.h>
101 * Virtual memory maps provide for the mapping, protection, and sharing
102 * of virtual memory objects. In addition, this module provides for an
103 * efficient virtual copy of memory from one map to another.
105 * Synchronization is required prior to most operations.
107 * Maps consist of an ordered doubly-linked list of simple entries.
108 * A hint and a RB tree is used to speed-up lookups.
110 * Callers looking to modify maps specify start/end addresses which cause
111 * the related map entry to be clipped if necessary, and then later
112 * recombined if the pieces remained compatible.
114 * Virtual copy operations are performed by copying VM object references
115 * from one map to another, and then marking both regions as copy-on-write.
117 static __boolean_t
vmspace_ctor(void *obj
, void *privdata
, int ocflags
);
118 static void vmspace_dtor(void *obj
, void *privdata
);
119 static void vmspace_terminate(struct vmspace
*vm
, int final
);
121 MALLOC_DEFINE(M_VMSPACE
, "vmspace", "vmspace objcache backingstore");
122 static struct objcache
*vmspace_cache
;
125 * per-cpu page table cross mappings are initialized in early boot
126 * and might require a considerable number of vm_map_entry structures.
128 #define MAPENTRYBSP_CACHE (MAXCPU+1)
129 #define MAPENTRYAP_CACHE 8
131 static struct vm_zone mapentzone_store
, mapzone_store
;
132 static vm_zone_t mapentzone
, mapzone
;
133 static struct vm_object mapentobj
, mapobj
;
135 static struct vm_map_entry map_entry_init
[MAX_MAPENT
];
136 static struct vm_map_entry cpu_map_entry_init_bsp
[MAPENTRYBSP_CACHE
];
137 static struct vm_map_entry cpu_map_entry_init_ap
[MAXCPU
][MAPENTRYAP_CACHE
];
138 static struct vm_map map_init
[MAX_KMAP
];
140 static int randomize_mmap
;
141 SYSCTL_INT(_vm
, OID_AUTO
, randomize_mmap
, CTLFLAG_RW
, &randomize_mmap
, 0,
142 "Randomize mmap offsets");
143 static int vm_map_relock_enable
= 1;
144 SYSCTL_INT(_vm
, OID_AUTO
, map_relock_enable
, CTLFLAG_RW
,
145 &vm_map_relock_enable
, 0, "Randomize mmap offsets");
147 static void vm_map_entry_shadow(vm_map_entry_t entry
, int addref
);
148 static vm_map_entry_t
vm_map_entry_create(vm_map_t map
, int *);
149 static void vm_map_entry_dispose (vm_map_t map
, vm_map_entry_t entry
, int *);
150 static void _vm_map_clip_end (vm_map_t
, vm_map_entry_t
, vm_offset_t
, int *);
151 static void _vm_map_clip_start (vm_map_t
, vm_map_entry_t
, vm_offset_t
, int *);
152 static void vm_map_entry_delete (vm_map_t
, vm_map_entry_t
, int *);
153 static void vm_map_entry_unwire (vm_map_t
, vm_map_entry_t
);
154 static void vm_map_copy_entry (vm_map_t
, vm_map_t
, vm_map_entry_t
,
156 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
);
159 * Initialize the vm_map module. Must be called before any other vm_map
162 * Map and entry structures are allocated from the general purpose
163 * memory pool with some exceptions:
165 * - The kernel map is allocated statically.
166 * - Initial kernel map entries are allocated out of a static pool.
167 * - We must set ZONE_SPECIAL here or the early boot code can get
168 * stuck if there are >63 cores.
170 * These restrictions are necessary since malloc() uses the
171 * maps and requires map entries.
173 * Called from the low level boot code only.
178 mapzone
= &mapzone_store
;
179 zbootinit(mapzone
, "MAP", sizeof (struct vm_map
),
181 mapentzone
= &mapentzone_store
;
182 zbootinit(mapentzone
, "MAP ENTRY", sizeof (struct vm_map_entry
),
183 map_entry_init
, MAX_MAPENT
);
184 mapentzone_store
.zflags
|= ZONE_SPECIAL
;
188 * Called prior to any vmspace allocations.
190 * Called from the low level boot code only.
195 vmspace_cache
= objcache_create_mbacked(M_VMSPACE
,
196 sizeof(struct vmspace
),
198 vmspace_ctor
, vmspace_dtor
,
200 zinitna(mapentzone
, &mapentobj
, NULL
, 0, 0,
201 ZONE_USE_RESERVE
| ZONE_SPECIAL
, 1);
202 zinitna(mapzone
, &mapobj
, NULL
, 0, 0, 0, 1);
208 * objcache support. We leave the pmap root cached as long as possible
209 * for performance reasons.
213 vmspace_ctor(void *obj
, void *privdata
, int ocflags
)
215 struct vmspace
*vm
= obj
;
217 bzero(vm
, sizeof(*vm
));
218 vm
->vm_refcnt
= (u_int
)-1;
225 vmspace_dtor(void *obj
, void *privdata
)
227 struct vmspace
*vm
= obj
;
229 KKASSERT(vm
->vm_refcnt
== (u_int
)-1);
230 pmap_puninit(vmspace_pmap(vm
));
234 * Red black tree functions
236 * The caller must hold the related map lock.
238 static int rb_vm_map_compare(vm_map_entry_t a
, vm_map_entry_t b
);
239 RB_GENERATE(vm_map_rb_tree
, vm_map_entry
, rb_entry
, rb_vm_map_compare
);
241 /* a->start is address, and the only field has to be initialized */
243 rb_vm_map_compare(vm_map_entry_t a
, vm_map_entry_t b
)
245 if (a
->start
< b
->start
)
247 else if (a
->start
> b
->start
)
253 * Initialize vmspace ref/hold counts vmspace0. There is a holdcnt for
257 vmspace_initrefs(struct vmspace
*vm
)
264 * Allocate a vmspace structure, including a vm_map and pmap.
265 * Initialize numerous fields. While the initial allocation is zerod,
266 * subsequence reuse from the objcache leaves elements of the structure
267 * intact (particularly the pmap), so portions must be zerod.
269 * Returns a referenced vmspace.
274 vmspace_alloc(vm_offset_t min
, vm_offset_t max
)
278 vm
= objcache_get(vmspace_cache
, M_WAITOK
);
280 bzero(&vm
->vm_startcopy
,
281 (char *)&vm
->vm_endcopy
- (char *)&vm
->vm_startcopy
);
282 vm_map_init(&vm
->vm_map
, min
, max
, NULL
); /* initializes token */
285 * NOTE: hold to acquires token for safety.
287 * On return vmspace is referenced (refs=1, hold=1). That is,
288 * each refcnt also has a holdcnt. There can be additional holds
289 * (holdcnt) above and beyond the refcnt. Finalization is handled in
290 * two stages, one on refs 1->0, and the the second on hold 1->0.
292 KKASSERT(vm
->vm_holdcnt
== 0);
293 KKASSERT(vm
->vm_refcnt
== (u_int
)-1);
294 vmspace_initrefs(vm
);
296 pmap_pinit(vmspace_pmap(vm
)); /* (some fields reused) */
297 vm
->vm_map
.pmap
= vmspace_pmap(vm
); /* XXX */
300 cpu_vmspace_alloc(vm
);
307 * NOTE: Can return -1 if the vmspace is exiting.
310 vmspace_getrefs(struct vmspace
*vm
)
312 return ((int)vm
->vm_refcnt
);
316 * A vmspace object must already have a non-zero hold to be able to gain
317 * further holds on it.
320 vmspace_hold_notoken(struct vmspace
*vm
)
322 KKASSERT(vm
->vm_holdcnt
!= 0);
323 refcount_acquire(&vm
->vm_holdcnt
);
327 vmspace_drop_notoken(struct vmspace
*vm
)
329 if (refcount_release(&vm
->vm_holdcnt
)) {
330 if (vm
->vm_refcnt
== (u_int
)-1) {
331 vmspace_terminate(vm
, 1);
337 vmspace_hold(struct vmspace
*vm
)
339 vmspace_hold_notoken(vm
);
340 lwkt_gettoken(&vm
->vm_map
.token
);
344 vmspace_drop(struct vmspace
*vm
)
346 lwkt_reltoken(&vm
->vm_map
.token
);
347 vmspace_drop_notoken(vm
);
351 * A vmspace object must not be in a terminated state to be able to obtain
352 * additional refs on it.
354 * Ref'ing a vmspace object also increments its hold count.
357 vmspace_ref(struct vmspace
*vm
)
359 KKASSERT((int)vm
->vm_refcnt
>= 0);
360 vmspace_hold_notoken(vm
);
361 refcount_acquire(&vm
->vm_refcnt
);
365 * Release a ref on the vmspace. On the 1->0 transition we do stage-1
366 * termination of the vmspace. Then, on the final drop of the hold we
367 * will do stage-2 final termination.
370 vmspace_rel(struct vmspace
*vm
)
372 if (refcount_release(&vm
->vm_refcnt
)) {
373 vm
->vm_refcnt
= (u_int
)-1; /* no other refs possible */
374 vmspace_terminate(vm
, 0);
376 vmspace_drop_notoken(vm
);
380 * This is called during exit indicating that the vmspace is no
381 * longer in used by an exiting process, but the process has not yet
384 * We release the refcnt but not the associated holdcnt.
389 vmspace_relexit(struct vmspace
*vm
)
391 if (refcount_release(&vm
->vm_refcnt
)) {
392 vm
->vm_refcnt
= (u_int
)-1; /* no other refs possible */
393 vmspace_terminate(vm
, 0);
398 * Called during reap to disconnect the remainder of the vmspace from
399 * the process. On the hold drop the vmspace termination is finalized.
404 vmspace_exitfree(struct proc
*p
)
410 vmspace_drop_notoken(vm
);
414 * Called in two cases:
416 * (1) When the last refcnt is dropped and the vmspace becomes inactive,
417 * called with final == 0. refcnt will be (u_int)-1 at this point,
418 * and holdcnt will still be non-zero.
420 * (2) When holdcnt becomes 0, called with final == 1. There should no
421 * longer be anyone with access to the vmspace.
423 * VMSPACE_EXIT1 flags the primary deactivation
424 * VMSPACE_EXIT2 flags the last reap
427 vmspace_terminate(struct vmspace
*vm
, int final
)
431 lwkt_gettoken(&vm
->vm_map
.token
);
433 KKASSERT((vm
->vm_flags
& VMSPACE_EXIT1
) == 0);
436 * Get rid of most of the resources. Leave the kernel pmap
439 vm
->vm_flags
|= VMSPACE_EXIT1
;
441 pmap_remove_pages(vmspace_pmap(vm
), VM_MIN_USER_ADDRESS
,
442 VM_MAX_USER_ADDRESS
);
443 vm_map_remove(&vm
->vm_map
, VM_MIN_USER_ADDRESS
,
444 VM_MAX_USER_ADDRESS
);
445 lwkt_reltoken(&vm
->vm_map
.token
);
447 KKASSERT((vm
->vm_flags
& VMSPACE_EXIT1
) != 0);
448 KKASSERT((vm
->vm_flags
& VMSPACE_EXIT2
) == 0);
451 * Get rid of remaining basic resources.
453 vm
->vm_flags
|= VMSPACE_EXIT2
;
454 cpu_vmspace_free(vm
);
458 * Lock the map, to wait out all other references to it.
459 * Delete all of the mappings and pages they hold, then call
460 * the pmap module to reclaim anything left.
462 count
= vm_map_entry_reserve(MAP_RESERVE_COUNT
);
463 vm_map_lock(&vm
->vm_map
);
464 vm_map_delete(&vm
->vm_map
, vm
->vm_map
.min_offset
,
465 vm
->vm_map
.max_offset
, &count
);
466 vm_map_unlock(&vm
->vm_map
);
467 vm_map_entry_release(count
);
469 lwkt_gettoken(&vmspace_pmap(vm
)->pm_token
);
470 pmap_release(vmspace_pmap(vm
));
471 lwkt_reltoken(&vmspace_pmap(vm
)->pm_token
);
472 lwkt_reltoken(&vm
->vm_map
.token
);
473 objcache_put(vmspace_cache
, vm
);
478 * Swap useage is determined by taking the proportional swap used by
479 * VM objects backing the VM map. To make up for fractional losses,
480 * if the VM object has any swap use at all the associated map entries
481 * count for at least 1 swap page.
486 vmspace_swap_count(struct vmspace
*vm
)
488 vm_map_t map
= &vm
->vm_map
;
495 for (cur
= map
->header
.next
; cur
!= &map
->header
; cur
= cur
->next
) {
496 switch(cur
->maptype
) {
497 case VM_MAPTYPE_NORMAL
:
498 case VM_MAPTYPE_VPAGETABLE
:
499 if ((object
= cur
->object
.vm_object
) == NULL
)
501 if (object
->swblock_count
) {
502 n
= (cur
->end
- cur
->start
) / PAGE_SIZE
;
503 count
+= object
->swblock_count
*
504 SWAP_META_PAGES
* n
/ object
->size
+ 1;
517 * Calculate the approximate number of anonymous pages in use by
518 * this vmspace. To make up for fractional losses, we count each
519 * VM object as having at least 1 anonymous page.
524 vmspace_anonymous_count(struct vmspace
*vm
)
526 vm_map_t map
= &vm
->vm_map
;
532 for (cur
= map
->header
.next
; cur
!= &map
->header
; cur
= cur
->next
) {
533 switch(cur
->maptype
) {
534 case VM_MAPTYPE_NORMAL
:
535 case VM_MAPTYPE_VPAGETABLE
:
536 if ((object
= cur
->object
.vm_object
) == NULL
)
538 if (object
->type
!= OBJT_DEFAULT
&&
539 object
->type
!= OBJT_SWAP
) {
542 count
+= object
->resident_page_count
;
554 * Creates and returns a new empty VM map with the given physical map
555 * structure, and having the given lower and upper address bounds.
560 vm_map_create(vm_map_t result
, pmap_t pmap
, vm_offset_t min
, vm_offset_t max
)
563 result
= zalloc(mapzone
);
564 vm_map_init(result
, min
, max
, pmap
);
569 * Initialize an existing vm_map structure such as that in the vmspace
570 * structure. The pmap is initialized elsewhere.
575 vm_map_init(struct vm_map
*map
, vm_offset_t min
, vm_offset_t max
, pmap_t pmap
)
577 map
->header
.next
= map
->header
.prev
= &map
->header
;
578 RB_INIT(&map
->rb_root
);
582 map
->min_offset
= min
;
583 map
->max_offset
= max
;
585 map
->first_free
= &map
->header
;
586 map
->hint
= &map
->header
;
589 lwkt_token_init(&map
->token
, "vm_map");
590 lockinit(&map
->lock
, "vm_maplk", (hz
+ 9) / 10, 0);
594 * Shadow the vm_map_entry's object. This typically needs to be done when
595 * a write fault is taken on an entry which had previously been cloned by
596 * fork(). The shared object (which might be NULL) must become private so
597 * we add a shadow layer above it.
599 * Object allocation for anonymous mappings is defered as long as possible.
600 * When creating a shadow, however, the underlying object must be instantiated
601 * so it can be shared.
603 * If the map segment is governed by a virtual page table then it is
604 * possible to address offsets beyond the mapped area. Just allocate
605 * a maximally sized object for this case.
607 * The vm_map must be exclusively locked.
608 * No other requirements.
612 vm_map_entry_shadow(vm_map_entry_t entry
, int addref
)
614 if (entry
->maptype
== VM_MAPTYPE_VPAGETABLE
) {
615 vm_object_shadow(&entry
->object
.vm_object
, &entry
->offset
,
616 0x7FFFFFFF, addref
); /* XXX */
618 vm_object_shadow(&entry
->object
.vm_object
, &entry
->offset
,
619 atop(entry
->end
- entry
->start
), addref
);
621 entry
->eflags
&= ~MAP_ENTRY_NEEDS_COPY
;
625 * Allocate an object for a vm_map_entry.
627 * Object allocation for anonymous mappings is defered as long as possible.
628 * This function is called when we can defer no longer, generally when a map
629 * entry might be split or forked or takes a page fault.
631 * If the map segment is governed by a virtual page table then it is
632 * possible to address offsets beyond the mapped area. Just allocate
633 * a maximally sized object for this case.
635 * The vm_map must be exclusively locked.
636 * No other requirements.
639 vm_map_entry_allocate_object(vm_map_entry_t entry
)
643 if (entry
->maptype
== VM_MAPTYPE_VPAGETABLE
) {
644 obj
= vm_object_allocate(OBJT_DEFAULT
, 0x7FFFFFFF); /* XXX */
646 obj
= vm_object_allocate(OBJT_DEFAULT
,
647 atop(entry
->end
- entry
->start
));
649 entry
->object
.vm_object
= obj
;
654 * Set an initial negative count so the first attempt to reserve
655 * space preloads a bunch of vm_map_entry's for this cpu. Also
656 * pre-allocate 2 vm_map_entries which will be needed by zalloc() to
657 * map a new page for vm_map_entry structures. SMP systems are
658 * particularly sensitive.
660 * This routine is called in early boot so we cannot just call
661 * vm_map_entry_reserve().
663 * Called from the low level boot code only (for each cpu)
665 * WARNING! Take care not to have too-big a static/BSS structure here
666 * as MAXCPU can be 256+, otherwise the loader's 64MB heap
667 * can get blown out by the kernel plus the initrd image.
670 vm_map_entry_reserve_cpu_init(globaldata_t gd
)
672 vm_map_entry_t entry
;
676 gd
->gd_vme_avail
-= MAP_RESERVE_COUNT
* 2;
677 if (gd
->gd_cpuid
== 0) {
678 entry
= &cpu_map_entry_init_bsp
[0];
679 count
= MAPENTRYBSP_CACHE
;
681 entry
= &cpu_map_entry_init_ap
[gd
->gd_cpuid
][0];
682 count
= MAPENTRYAP_CACHE
;
684 for (i
= 0; i
< count
; ++i
, ++entry
) {
685 entry
->next
= gd
->gd_vme_base
;
686 gd
->gd_vme_base
= entry
;
691 * Reserves vm_map_entry structures so code later on can manipulate
692 * map_entry structures within a locked map without blocking trying
693 * to allocate a new vm_map_entry.
698 vm_map_entry_reserve(int count
)
700 struct globaldata
*gd
= mycpu
;
701 vm_map_entry_t entry
;
704 * Make sure we have enough structures in gd_vme_base to handle
705 * the reservation request.
707 * The critical section protects access to the per-cpu gd.
710 while (gd
->gd_vme_avail
< count
) {
711 entry
= zalloc(mapentzone
);
712 entry
->next
= gd
->gd_vme_base
;
713 gd
->gd_vme_base
= entry
;
716 gd
->gd_vme_avail
-= count
;
723 * Releases previously reserved vm_map_entry structures that were not
724 * used. If we have too much junk in our per-cpu cache clean some of
730 vm_map_entry_release(int count
)
732 struct globaldata
*gd
= mycpu
;
733 vm_map_entry_t entry
;
736 gd
->gd_vme_avail
+= count
;
737 while (gd
->gd_vme_avail
> MAP_RESERVE_SLOP
) {
738 entry
= gd
->gd_vme_base
;
739 KKASSERT(entry
!= NULL
);
740 gd
->gd_vme_base
= entry
->next
;
743 zfree(mapentzone
, entry
);
750 * Reserve map entry structures for use in kernel_map itself. These
751 * entries have *ALREADY* been reserved on a per-cpu basis when the map
752 * was inited. This function is used by zalloc() to avoid a recursion
753 * when zalloc() itself needs to allocate additional kernel memory.
755 * This function works like the normal reserve but does not load the
756 * vm_map_entry cache (because that would result in an infinite
757 * recursion). Note that gd_vme_avail may go negative. This is expected.
759 * Any caller of this function must be sure to renormalize after
760 * potentially eating entries to ensure that the reserve supply
766 vm_map_entry_kreserve(int count
)
768 struct globaldata
*gd
= mycpu
;
771 gd
->gd_vme_avail
-= count
;
773 KASSERT(gd
->gd_vme_base
!= NULL
,
774 ("no reserved entries left, gd_vme_avail = %d",
780 * Release previously reserved map entries for kernel_map. We do not
781 * attempt to clean up like the normal release function as this would
782 * cause an unnecessary (but probably not fatal) deep procedure call.
787 vm_map_entry_krelease(int count
)
789 struct globaldata
*gd
= mycpu
;
792 gd
->gd_vme_avail
+= count
;
797 * Allocates a VM map entry for insertion. No entry fields are filled in.
799 * The entries should have previously been reserved. The reservation count
800 * is tracked in (*countp).
804 static vm_map_entry_t
805 vm_map_entry_create(vm_map_t map
, int *countp
)
807 struct globaldata
*gd
= mycpu
;
808 vm_map_entry_t entry
;
810 KKASSERT(*countp
> 0);
813 entry
= gd
->gd_vme_base
;
814 KASSERT(entry
!= NULL
, ("gd_vme_base NULL! count %d", *countp
));
815 gd
->gd_vme_base
= entry
->next
;
822 * Dispose of a vm_map_entry that is no longer being referenced.
827 vm_map_entry_dispose(vm_map_t map
, vm_map_entry_t entry
, int *countp
)
829 struct globaldata
*gd
= mycpu
;
831 KKASSERT(map
->hint
!= entry
);
832 KKASSERT(map
->first_free
!= entry
);
836 entry
->next
= gd
->gd_vme_base
;
837 gd
->gd_vme_base
= entry
;
843 * Insert/remove entries from maps.
845 * The related map must be exclusively locked.
846 * The caller must hold map->token
847 * No other requirements.
850 vm_map_entry_link(vm_map_t map
,
851 vm_map_entry_t after_where
,
852 vm_map_entry_t entry
)
854 ASSERT_VM_MAP_LOCKED(map
);
857 entry
->prev
= after_where
;
858 entry
->next
= after_where
->next
;
859 entry
->next
->prev
= entry
;
860 after_where
->next
= entry
;
861 if (vm_map_rb_tree_RB_INSERT(&map
->rb_root
, entry
))
862 panic("vm_map_entry_link: dup addr map %p ent %p", map
, entry
);
866 vm_map_entry_unlink(vm_map_t map
,
867 vm_map_entry_t entry
)
872 ASSERT_VM_MAP_LOCKED(map
);
874 if (entry
->eflags
& MAP_ENTRY_IN_TRANSITION
) {
875 panic("vm_map_entry_unlink: attempt to mess with "
876 "locked entry! %p", entry
);
882 vm_map_rb_tree_RB_REMOVE(&map
->rb_root
, entry
);
887 * Finds the map entry containing (or immediately preceding) the specified
888 * address in the given map. The entry is returned in (*entry).
890 * The boolean result indicates whether the address is actually contained
893 * The related map must be locked.
894 * No other requirements.
897 vm_map_lookup_entry(vm_map_t map
, vm_offset_t address
, vm_map_entry_t
*entry
)
902 ASSERT_VM_MAP_LOCKED(map
);
905 * XXX TEMPORARILY DISABLED. For some reason our attempt to revive
906 * the hint code with the red-black lookup meets with system crashes
907 * and lockups. We do not yet know why.
909 * It is possible that the problem is related to the setting
910 * of the hint during map_entry deletion, in the code specified
911 * at the GGG comment later on in this file.
913 * YYY More likely it's because this function can be called with
914 * a shared lock on the map, resulting in map->hint updates possibly
915 * racing. Fixed now but untested.
918 * Quickly check the cached hint, there's a good chance of a match.
922 if (tmp
!= &map
->header
) {
923 if (address
>= tmp
->start
&& address
< tmp
->end
) {
931 * Locate the record from the top of the tree. 'last' tracks the
932 * closest prior record and is returned if no match is found, which
933 * in binary tree terms means tracking the most recent right-branch
934 * taken. If there is no prior record, &map->header is returned.
937 tmp
= RB_ROOT(&map
->rb_root
);
940 if (address
>= tmp
->start
) {
941 if (address
< tmp
->end
) {
947 tmp
= RB_RIGHT(tmp
, rb_entry
);
949 tmp
= RB_LEFT(tmp
, rb_entry
);
957 * Inserts the given whole VM object into the target map at the specified
958 * address range. The object's size should match that of the address range.
960 * The map must be exclusively locked.
961 * The object must be held.
962 * The caller must have reserved sufficient vm_map_entry structures.
964 * If object is non-NULL, ref count must be bumped by caller prior to
965 * making call to account for the new entry.
968 vm_map_insert(vm_map_t map
, int *countp
, void *map_object
, void *map_aux
,
969 vm_ooffset_t offset
, vm_offset_t start
, vm_offset_t end
,
970 vm_maptype_t maptype
,
971 vm_prot_t prot
, vm_prot_t max
, int cow
)
973 vm_map_entry_t new_entry
;
974 vm_map_entry_t prev_entry
;
975 vm_map_entry_t temp_entry
;
976 vm_eflags_t protoeflags
;
980 if (maptype
== VM_MAPTYPE_UKSMAP
)
985 ASSERT_VM_MAP_LOCKED(map
);
987 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object
));
990 * Check that the start and end points are not bogus.
992 if ((start
< map
->min_offset
) || (end
> map
->max_offset
) ||
994 return (KERN_INVALID_ADDRESS
);
997 * Find the entry prior to the proposed starting address; if it's part
998 * of an existing entry, this range is bogus.
1000 if (vm_map_lookup_entry(map
, start
, &temp_entry
))
1001 return (KERN_NO_SPACE
);
1003 prev_entry
= temp_entry
;
1006 * Assert that the next entry doesn't overlap the end point.
1009 if ((prev_entry
->next
!= &map
->header
) &&
1010 (prev_entry
->next
->start
< end
))
1011 return (KERN_NO_SPACE
);
1015 if (cow
& MAP_COPY_ON_WRITE
)
1016 protoeflags
|= MAP_ENTRY_COW
|MAP_ENTRY_NEEDS_COPY
;
1018 if (cow
& MAP_NOFAULT
) {
1019 protoeflags
|= MAP_ENTRY_NOFAULT
;
1021 KASSERT(object
== NULL
,
1022 ("vm_map_insert: paradoxical MAP_NOFAULT request"));
1024 if (cow
& MAP_DISABLE_SYNCER
)
1025 protoeflags
|= MAP_ENTRY_NOSYNC
;
1026 if (cow
& MAP_DISABLE_COREDUMP
)
1027 protoeflags
|= MAP_ENTRY_NOCOREDUMP
;
1028 if (cow
& MAP_IS_STACK
)
1029 protoeflags
|= MAP_ENTRY_STACK
;
1030 if (cow
& MAP_IS_KSTACK
)
1031 protoeflags
|= MAP_ENTRY_KSTACK
;
1033 lwkt_gettoken(&map
->token
);
1037 * When object is non-NULL, it could be shared with another
1038 * process. We have to set or clear OBJ_ONEMAPPING
1041 * NOTE: This flag is only applicable to DEFAULT and SWAP
1042 * objects and will already be clear in other types
1043 * of objects, so a shared object lock is ok for
1046 if ((object
->ref_count
> 1) || (object
->shadow_count
!= 0)) {
1047 vm_object_clear_flag(object
, OBJ_ONEMAPPING
);
1050 else if ((prev_entry
!= &map
->header
) &&
1051 (prev_entry
->eflags
== protoeflags
) &&
1052 (prev_entry
->end
== start
) &&
1053 (prev_entry
->wired_count
== 0) &&
1054 prev_entry
->maptype
== maptype
&&
1055 maptype
== VM_MAPTYPE_NORMAL
&&
1056 ((prev_entry
->object
.vm_object
== NULL
) ||
1057 vm_object_coalesce(prev_entry
->object
.vm_object
,
1058 OFF_TO_IDX(prev_entry
->offset
),
1059 (vm_size_t
)(prev_entry
->end
- prev_entry
->start
),
1060 (vm_size_t
)(end
- prev_entry
->end
)))) {
1062 * We were able to extend the object. Determine if we
1063 * can extend the previous map entry to include the
1064 * new range as well.
1066 if ((prev_entry
->inheritance
== VM_INHERIT_DEFAULT
) &&
1067 (prev_entry
->protection
== prot
) &&
1068 (prev_entry
->max_protection
== max
)) {
1069 map
->size
+= (end
- prev_entry
->end
);
1070 prev_entry
->end
= end
;
1071 vm_map_simplify_entry(map
, prev_entry
, countp
);
1072 lwkt_reltoken(&map
->token
);
1073 return (KERN_SUCCESS
);
1077 * If we can extend the object but cannot extend the
1078 * map entry, we have to create a new map entry. We
1079 * must bump the ref count on the extended object to
1080 * account for it. object may be NULL.
1082 * XXX if object is NULL should we set offset to 0 here ?
1084 object
= prev_entry
->object
.vm_object
;
1085 offset
= prev_entry
->offset
+
1086 (prev_entry
->end
- prev_entry
->start
);
1088 vm_object_hold(object
);
1089 vm_object_chain_wait(object
, 0);
1090 vm_object_reference_locked(object
);
1092 map_object
= object
;
1097 * NOTE: if conditionals fail, object can be NULL here. This occurs
1098 * in things like the buffer map where we manage kva but do not manage
1103 * Create a new entry
1106 new_entry
= vm_map_entry_create(map
, countp
);
1107 new_entry
->start
= start
;
1108 new_entry
->end
= end
;
1110 new_entry
->maptype
= maptype
;
1111 new_entry
->eflags
= protoeflags
;
1112 new_entry
->object
.map_object
= map_object
;
1113 new_entry
->aux
.master_pde
= 0; /* in case size is different */
1114 new_entry
->aux
.map_aux
= map_aux
;
1115 new_entry
->offset
= offset
;
1117 new_entry
->inheritance
= VM_INHERIT_DEFAULT
;
1118 new_entry
->protection
= prot
;
1119 new_entry
->max_protection
= max
;
1120 new_entry
->wired_count
= 0;
1123 * Insert the new entry into the list
1126 vm_map_entry_link(map
, prev_entry
, new_entry
);
1127 map
->size
+= new_entry
->end
- new_entry
->start
;
1130 * Update the free space hint. Entries cannot overlap.
1131 * An exact comparison is needed to avoid matching
1132 * against the map->header.
1134 if ((map
->first_free
== prev_entry
) &&
1135 (prev_entry
->end
== new_entry
->start
)) {
1136 map
->first_free
= new_entry
;
1141 * Temporarily removed to avoid MAP_STACK panic, due to
1142 * MAP_STACK being a huge hack. Will be added back in
1143 * when MAP_STACK (and the user stack mapping) is fixed.
1146 * It may be possible to simplify the entry
1148 vm_map_simplify_entry(map
, new_entry
, countp
);
1152 * Try to pre-populate the page table. Mappings governed by virtual
1153 * page tables cannot be prepopulated without a lot of work, so
1156 if ((cow
& (MAP_PREFAULT
|MAP_PREFAULT_PARTIAL
)) &&
1157 maptype
!= VM_MAPTYPE_VPAGETABLE
&&
1158 maptype
!= VM_MAPTYPE_UKSMAP
) {
1160 if (vm_map_relock_enable
&& (cow
& MAP_PREFAULT_RELOCK
)) {
1162 vm_object_lock_swap();
1163 vm_object_drop(object
);
1165 pmap_object_init_pt(map
->pmap
, start
, prot
,
1166 object
, OFF_TO_IDX(offset
), end
- start
,
1167 cow
& MAP_PREFAULT_PARTIAL
);
1169 vm_object_hold(object
);
1170 vm_object_lock_swap();
1174 vm_object_drop(object
);
1176 lwkt_reltoken(&map
->token
);
1177 return (KERN_SUCCESS
);
1181 * Find sufficient space for `length' bytes in the given map, starting at
1182 * `start'. Returns 0 on success, 1 on no space.
1184 * This function will returned an arbitrarily aligned pointer. If no
1185 * particular alignment is required you should pass align as 1. Note that
1186 * the map may return PAGE_SIZE aligned pointers if all the lengths used in
1187 * the map are a multiple of PAGE_SIZE, even if you pass a smaller align
1190 * 'align' should be a power of 2 but is not required to be.
1192 * The map must be exclusively locked.
1193 * No other requirements.
1196 vm_map_findspace(vm_map_t map
, vm_offset_t start
, vm_size_t length
,
1197 vm_size_t align
, int flags
, vm_offset_t
*addr
)
1199 vm_map_entry_t entry
, next
;
1201 vm_offset_t align_mask
;
1203 if (start
< map
->min_offset
)
1204 start
= map
->min_offset
;
1205 if (start
> map
->max_offset
)
1209 * If the alignment is not a power of 2 we will have to use
1210 * a mod/division, set align_mask to a special value.
1212 if ((align
| (align
- 1)) + 1 != (align
<< 1))
1213 align_mask
= (vm_offset_t
)-1;
1215 align_mask
= align
- 1;
1218 * Look for the first possible address; if there's already something
1219 * at this address, we have to start after it.
1221 if (start
== map
->min_offset
) {
1222 if ((entry
= map
->first_free
) != &map
->header
)
1227 if (vm_map_lookup_entry(map
, start
, &tmp
))
1233 * Look through the rest of the map, trying to fit a new region in the
1234 * gap between existing regions, or after the very last region.
1236 for (;; start
= (entry
= next
)->end
) {
1238 * Adjust the proposed start by the requested alignment,
1239 * be sure that we didn't wrap the address.
1241 if (align_mask
== (vm_offset_t
)-1)
1242 end
= ((start
+ align
- 1) / align
) * align
;
1244 end
= (start
+ align_mask
) & ~align_mask
;
1249 * Find the end of the proposed new region. Be sure we didn't
1250 * go beyond the end of the map, or wrap around the address.
1251 * Then check to see if this is the last entry or if the
1252 * proposed end fits in the gap between this and the next
1255 end
= start
+ length
;
1256 if (end
> map
->max_offset
|| end
< start
)
1261 * If the next entry's start address is beyond the desired
1262 * end address we may have found a good entry.
1264 * If the next entry is a stack mapping we do not map into
1265 * the stack's reserved space.
1267 * XXX continue to allow mapping into the stack's reserved
1268 * space if doing a MAP_STACK mapping inside a MAP_STACK
1269 * mapping, for backwards compatibility. But the caller
1270 * really should use MAP_STACK | MAP_TRYFIXED if they
1273 if (next
== &map
->header
)
1275 if (next
->start
>= end
) {
1276 if ((next
->eflags
& MAP_ENTRY_STACK
) == 0)
1278 if (flags
& MAP_STACK
)
1280 if (next
->start
- next
->aux
.avail_ssize
>= end
)
1287 * Grow the kernel_map if necessary. pmap_growkernel() will panic
1288 * if it fails. The kernel_map is locked and nothing can steal
1289 * our address space if pmap_growkernel() blocks.
1291 * NOTE: This may be unconditionally called for kldload areas on
1292 * x86_64 because these do not bump kernel_vm_end (which would
1293 * fill 128G worth of page tables!). Therefore we must not
1296 if (map
== &kernel_map
) {
1299 kstop
= round_page(start
+ length
);
1300 if (kstop
> kernel_vm_end
)
1301 pmap_growkernel(start
, kstop
);
1308 * vm_map_find finds an unallocated region in the target address map with
1309 * the given length and allocates it. The search is defined to be first-fit
1310 * from the specified address; the region found is returned in the same
1313 * If object is non-NULL, ref count must be bumped by caller
1314 * prior to making call to account for the new entry.
1316 * No requirements. This function will lock the map temporarily.
1319 vm_map_find(vm_map_t map
, void *map_object
, void *map_aux
,
1320 vm_ooffset_t offset
, vm_offset_t
*addr
,
1321 vm_size_t length
, vm_size_t align
,
1323 vm_maptype_t maptype
,
1324 vm_prot_t prot
, vm_prot_t max
,
1332 if (maptype
== VM_MAPTYPE_UKSMAP
)
1335 object
= map_object
;
1339 count
= vm_map_entry_reserve(MAP_RESERVE_COUNT
);
1342 vm_object_hold_shared(object
);
1344 if (vm_map_findspace(map
, start
, length
, align
, 0, addr
)) {
1346 vm_object_drop(object
);
1348 vm_map_entry_release(count
);
1349 return (KERN_NO_SPACE
);
1353 result
= vm_map_insert(map
, &count
, map_object
, map_aux
,
1354 offset
, start
, start
+ length
,
1355 maptype
, prot
, max
, cow
);
1357 vm_object_drop(object
);
1359 vm_map_entry_release(count
);
1365 * Simplify the given map entry by merging with either neighbor. This
1366 * routine also has the ability to merge with both neighbors.
1368 * This routine guarentees that the passed entry remains valid (though
1369 * possibly extended). When merging, this routine may delete one or
1370 * both neighbors. No action is taken on entries which have their
1371 * in-transition flag set.
1373 * The map must be exclusively locked.
1376 vm_map_simplify_entry(vm_map_t map
, vm_map_entry_t entry
, int *countp
)
1378 vm_map_entry_t next
, prev
;
1379 vm_size_t prevsize
, esize
;
1381 if (entry
->eflags
& MAP_ENTRY_IN_TRANSITION
) {
1382 ++mycpu
->gd_cnt
.v_intrans_coll
;
1386 if (entry
->maptype
== VM_MAPTYPE_SUBMAP
)
1388 if (entry
->maptype
== VM_MAPTYPE_UKSMAP
)
1392 if (prev
!= &map
->header
) {
1393 prevsize
= prev
->end
- prev
->start
;
1394 if ( (prev
->end
== entry
->start
) &&
1395 (prev
->maptype
== entry
->maptype
) &&
1396 (prev
->object
.vm_object
== entry
->object
.vm_object
) &&
1397 (!prev
->object
.vm_object
||
1398 (prev
->offset
+ prevsize
== entry
->offset
)) &&
1399 (prev
->eflags
== entry
->eflags
) &&
1400 (prev
->protection
== entry
->protection
) &&
1401 (prev
->max_protection
== entry
->max_protection
) &&
1402 (prev
->inheritance
== entry
->inheritance
) &&
1403 (prev
->wired_count
== entry
->wired_count
)) {
1404 if (map
->first_free
== prev
)
1405 map
->first_free
= entry
;
1406 if (map
->hint
== prev
)
1408 vm_map_entry_unlink(map
, prev
);
1409 entry
->start
= prev
->start
;
1410 entry
->offset
= prev
->offset
;
1411 if (prev
->object
.vm_object
)
1412 vm_object_deallocate(prev
->object
.vm_object
);
1413 vm_map_entry_dispose(map
, prev
, countp
);
1418 if (next
!= &map
->header
) {
1419 esize
= entry
->end
- entry
->start
;
1420 if ((entry
->end
== next
->start
) &&
1421 (next
->maptype
== entry
->maptype
) &&
1422 (next
->object
.vm_object
== entry
->object
.vm_object
) &&
1423 (!entry
->object
.vm_object
||
1424 (entry
->offset
+ esize
== next
->offset
)) &&
1425 (next
->eflags
== entry
->eflags
) &&
1426 (next
->protection
== entry
->protection
) &&
1427 (next
->max_protection
== entry
->max_protection
) &&
1428 (next
->inheritance
== entry
->inheritance
) &&
1429 (next
->wired_count
== entry
->wired_count
)) {
1430 if (map
->first_free
== next
)
1431 map
->first_free
= entry
;
1432 if (map
->hint
== next
)
1434 vm_map_entry_unlink(map
, next
);
1435 entry
->end
= next
->end
;
1436 if (next
->object
.vm_object
)
1437 vm_object_deallocate(next
->object
.vm_object
);
1438 vm_map_entry_dispose(map
, next
, countp
);
1444 * Asserts that the given entry begins at or after the specified address.
1445 * If necessary, it splits the entry into two.
1447 #define vm_map_clip_start(map, entry, startaddr, countp) \
1449 if (startaddr > entry->start) \
1450 _vm_map_clip_start(map, entry, startaddr, countp); \
1454 * This routine is called only when it is known that the entry must be split.
1456 * The map must be exclusively locked.
1459 _vm_map_clip_start(vm_map_t map
, vm_map_entry_t entry
, vm_offset_t start
,
1462 vm_map_entry_t new_entry
;
1465 * Split off the front portion -- note that we must insert the new
1466 * entry BEFORE this one, so that this entry has the specified
1470 vm_map_simplify_entry(map
, entry
, countp
);
1473 * If there is no object backing this entry, we might as well create
1474 * one now. If we defer it, an object can get created after the map
1475 * is clipped, and individual objects will be created for the split-up
1476 * map. This is a bit of a hack, but is also about the best place to
1477 * put this improvement.
1479 if (entry
->object
.vm_object
== NULL
&& !map
->system_map
) {
1480 vm_map_entry_allocate_object(entry
);
1483 new_entry
= vm_map_entry_create(map
, countp
);
1484 *new_entry
= *entry
;
1486 new_entry
->end
= start
;
1487 entry
->offset
+= (start
- entry
->start
);
1488 entry
->start
= start
;
1490 vm_map_entry_link(map
, entry
->prev
, new_entry
);
1492 switch(entry
->maptype
) {
1493 case VM_MAPTYPE_NORMAL
:
1494 case VM_MAPTYPE_VPAGETABLE
:
1495 if (new_entry
->object
.vm_object
) {
1496 vm_object_hold(new_entry
->object
.vm_object
);
1497 vm_object_chain_wait(new_entry
->object
.vm_object
, 0);
1498 vm_object_reference_locked(new_entry
->object
.vm_object
);
1499 vm_object_drop(new_entry
->object
.vm_object
);
1508 * Asserts that the given entry ends at or before the specified address.
1509 * If necessary, it splits the entry into two.
1511 * The map must be exclusively locked.
1513 #define vm_map_clip_end(map, entry, endaddr, countp) \
1515 if (endaddr < entry->end) \
1516 _vm_map_clip_end(map, entry, endaddr, countp); \
1520 * This routine is called only when it is known that the entry must be split.
1522 * The map must be exclusively locked.
1525 _vm_map_clip_end(vm_map_t map
, vm_map_entry_t entry
, vm_offset_t end
,
1528 vm_map_entry_t new_entry
;
1531 * If there is no object backing this entry, we might as well create
1532 * one now. If we defer it, an object can get created after the map
1533 * is clipped, and individual objects will be created for the split-up
1534 * map. This is a bit of a hack, but is also about the best place to
1535 * put this improvement.
1538 if (entry
->object
.vm_object
== NULL
&& !map
->system_map
) {
1539 vm_map_entry_allocate_object(entry
);
1543 * Create a new entry and insert it AFTER the specified entry
1546 new_entry
= vm_map_entry_create(map
, countp
);
1547 *new_entry
= *entry
;
1549 new_entry
->start
= entry
->end
= end
;
1550 new_entry
->offset
+= (end
- entry
->start
);
1552 vm_map_entry_link(map
, entry
, new_entry
);
1554 switch(entry
->maptype
) {
1555 case VM_MAPTYPE_NORMAL
:
1556 case VM_MAPTYPE_VPAGETABLE
:
1557 if (new_entry
->object
.vm_object
) {
1558 vm_object_hold(new_entry
->object
.vm_object
);
1559 vm_object_chain_wait(new_entry
->object
.vm_object
, 0);
1560 vm_object_reference_locked(new_entry
->object
.vm_object
);
1561 vm_object_drop(new_entry
->object
.vm_object
);
1570 * Asserts that the starting and ending region addresses fall within the
1571 * valid range for the map.
1573 #define VM_MAP_RANGE_CHECK(map, start, end) \
1575 if (start < vm_map_min(map)) \
1576 start = vm_map_min(map); \
1577 if (end > vm_map_max(map)) \
1578 end = vm_map_max(map); \
1584 * Used to block when an in-transition collison occurs. The map
1585 * is unlocked for the sleep and relocked before the return.
1588 vm_map_transition_wait(vm_map_t map
)
1590 tsleep_interlock(map
, 0);
1592 tsleep(map
, PINTERLOCKED
, "vment", 0);
1597 * When we do blocking operations with the map lock held it is
1598 * possible that a clip might have occured on our in-transit entry,
1599 * requiring an adjustment to the entry in our loop. These macros
1600 * help the pageable and clip_range code deal with the case. The
1601 * conditional costs virtually nothing if no clipping has occured.
1604 #define CLIP_CHECK_BACK(entry, save_start) \
1606 while (entry->start != save_start) { \
1607 entry = entry->prev; \
1608 KASSERT(entry != &map->header, ("bad entry clip")); \
1612 #define CLIP_CHECK_FWD(entry, save_end) \
1614 while (entry->end != save_end) { \
1615 entry = entry->next; \
1616 KASSERT(entry != &map->header, ("bad entry clip")); \
1622 * Clip the specified range and return the base entry. The
1623 * range may cover several entries starting at the returned base
1624 * and the first and last entry in the covering sequence will be
1625 * properly clipped to the requested start and end address.
1627 * If no holes are allowed you should pass the MAP_CLIP_NO_HOLES
1630 * The MAP_ENTRY_IN_TRANSITION flag will be set for the entries
1631 * covered by the requested range.
1633 * The map must be exclusively locked on entry and will remain locked
1634 * on return. If no range exists or the range contains holes and you
1635 * specified that no holes were allowed, NULL will be returned. This
1636 * routine may temporarily unlock the map in order avoid a deadlock when
1641 vm_map_clip_range(vm_map_t map
, vm_offset_t start
, vm_offset_t end
,
1642 int *countp
, int flags
)
1644 vm_map_entry_t start_entry
;
1645 vm_map_entry_t entry
;
1648 * Locate the entry and effect initial clipping. The in-transition
1649 * case does not occur very often so do not try to optimize it.
1652 if (vm_map_lookup_entry(map
, start
, &start_entry
) == FALSE
)
1654 entry
= start_entry
;
1655 if (entry
->eflags
& MAP_ENTRY_IN_TRANSITION
) {
1656 entry
->eflags
|= MAP_ENTRY_NEEDS_WAKEUP
;
1657 ++mycpu
->gd_cnt
.v_intrans_coll
;
1658 ++mycpu
->gd_cnt
.v_intrans_wait
;
1659 vm_map_transition_wait(map
);
1661 * entry and/or start_entry may have been clipped while
1662 * we slept, or may have gone away entirely. We have
1663 * to restart from the lookup.
1669 * Since we hold an exclusive map lock we do not have to restart
1670 * after clipping, even though clipping may block in zalloc.
1672 vm_map_clip_start(map
, entry
, start
, countp
);
1673 vm_map_clip_end(map
, entry
, end
, countp
);
1674 entry
->eflags
|= MAP_ENTRY_IN_TRANSITION
;
1677 * Scan entries covered by the range. When working on the next
1678 * entry a restart need only re-loop on the current entry which
1679 * we have already locked, since 'next' may have changed. Also,
1680 * even though entry is safe, it may have been clipped so we
1681 * have to iterate forwards through the clip after sleeping.
1683 while (entry
->next
!= &map
->header
&& entry
->next
->start
< end
) {
1684 vm_map_entry_t next
= entry
->next
;
1686 if (flags
& MAP_CLIP_NO_HOLES
) {
1687 if (next
->start
> entry
->end
) {
1688 vm_map_unclip_range(map
, start_entry
,
1689 start
, entry
->end
, countp
, flags
);
1694 if (next
->eflags
& MAP_ENTRY_IN_TRANSITION
) {
1695 vm_offset_t save_end
= entry
->end
;
1696 next
->eflags
|= MAP_ENTRY_NEEDS_WAKEUP
;
1697 ++mycpu
->gd_cnt
.v_intrans_coll
;
1698 ++mycpu
->gd_cnt
.v_intrans_wait
;
1699 vm_map_transition_wait(map
);
1702 * clips might have occured while we blocked.
1704 CLIP_CHECK_FWD(entry
, save_end
);
1705 CLIP_CHECK_BACK(start_entry
, start
);
1709 * No restart necessary even though clip_end may block, we
1710 * are holding the map lock.
1712 vm_map_clip_end(map
, next
, end
, countp
);
1713 next
->eflags
|= MAP_ENTRY_IN_TRANSITION
;
1716 if (flags
& MAP_CLIP_NO_HOLES
) {
1717 if (entry
->end
!= end
) {
1718 vm_map_unclip_range(map
, start_entry
,
1719 start
, entry
->end
, countp
, flags
);
1723 return(start_entry
);
1727 * Undo the effect of vm_map_clip_range(). You should pass the same
1728 * flags and the same range that you passed to vm_map_clip_range().
1729 * This code will clear the in-transition flag on the entries and
1730 * wake up anyone waiting. This code will also simplify the sequence
1731 * and attempt to merge it with entries before and after the sequence.
1733 * The map must be locked on entry and will remain locked on return.
1735 * Note that you should also pass the start_entry returned by
1736 * vm_map_clip_range(). However, if you block between the two calls
1737 * with the map unlocked please be aware that the start_entry may
1738 * have been clipped and you may need to scan it backwards to find
1739 * the entry corresponding with the original start address. You are
1740 * responsible for this, vm_map_unclip_range() expects the correct
1741 * start_entry to be passed to it and will KASSERT otherwise.
1745 vm_map_unclip_range(vm_map_t map
, vm_map_entry_t start_entry
,
1746 vm_offset_t start
, vm_offset_t end
,
1747 int *countp
, int flags
)
1749 vm_map_entry_t entry
;
1751 entry
= start_entry
;
1753 KASSERT(entry
->start
== start
, ("unclip_range: illegal base entry"));
1754 while (entry
!= &map
->header
&& entry
->start
< end
) {
1755 KASSERT(entry
->eflags
& MAP_ENTRY_IN_TRANSITION
,
1756 ("in-transition flag not set during unclip on: %p",
1758 KASSERT(entry
->end
<= end
,
1759 ("unclip_range: tail wasn't clipped"));
1760 entry
->eflags
&= ~MAP_ENTRY_IN_TRANSITION
;
1761 if (entry
->eflags
& MAP_ENTRY_NEEDS_WAKEUP
) {
1762 entry
->eflags
&= ~MAP_ENTRY_NEEDS_WAKEUP
;
1765 entry
= entry
->next
;
1769 * Simplification does not block so there is no restart case.
1771 entry
= start_entry
;
1772 while (entry
!= &map
->header
&& entry
->start
< end
) {
1773 vm_map_simplify_entry(map
, entry
, countp
);
1774 entry
= entry
->next
;
1779 * Mark the given range as handled by a subordinate map.
1781 * This range must have been created with vm_map_find(), and no other
1782 * operations may have been performed on this range prior to calling
1785 * Submappings cannot be removed.
1790 vm_map_submap(vm_map_t map
, vm_offset_t start
, vm_offset_t end
, vm_map_t submap
)
1792 vm_map_entry_t entry
;
1793 int result
= KERN_INVALID_ARGUMENT
;
1796 count
= vm_map_entry_reserve(MAP_RESERVE_COUNT
);
1799 VM_MAP_RANGE_CHECK(map
, start
, end
);
1801 if (vm_map_lookup_entry(map
, start
, &entry
)) {
1802 vm_map_clip_start(map
, entry
, start
, &count
);
1804 entry
= entry
->next
;
1807 vm_map_clip_end(map
, entry
, end
, &count
);
1809 if ((entry
->start
== start
) && (entry
->end
== end
) &&
1810 ((entry
->eflags
& MAP_ENTRY_COW
) == 0) &&
1811 (entry
->object
.vm_object
== NULL
)) {
1812 entry
->object
.sub_map
= submap
;
1813 entry
->maptype
= VM_MAPTYPE_SUBMAP
;
1814 result
= KERN_SUCCESS
;
1817 vm_map_entry_release(count
);
1823 * Sets the protection of the specified address region in the target map.
1824 * If "set_max" is specified, the maximum protection is to be set;
1825 * otherwise, only the current protection is affected.
1827 * The protection is not applicable to submaps, but is applicable to normal
1828 * maps and maps governed by virtual page tables. For example, when operating
1829 * on a virtual page table our protection basically controls how COW occurs
1830 * on the backing object, whereas the virtual page table abstraction itself
1831 * is an abstraction for userland.
1836 vm_map_protect(vm_map_t map
, vm_offset_t start
, vm_offset_t end
,
1837 vm_prot_t new_prot
, boolean_t set_max
)
1839 vm_map_entry_t current
;
1840 vm_map_entry_t entry
;
1843 count
= vm_map_entry_reserve(MAP_RESERVE_COUNT
);
1846 VM_MAP_RANGE_CHECK(map
, start
, end
);
1848 if (vm_map_lookup_entry(map
, start
, &entry
)) {
1849 vm_map_clip_start(map
, entry
, start
, &count
);
1851 entry
= entry
->next
;
1855 * Make a first pass to check for protection violations.
1858 while ((current
!= &map
->header
) && (current
->start
< end
)) {
1859 if (current
->maptype
== VM_MAPTYPE_SUBMAP
) {
1861 vm_map_entry_release(count
);
1862 return (KERN_INVALID_ARGUMENT
);
1864 if ((new_prot
& current
->max_protection
) != new_prot
) {
1866 vm_map_entry_release(count
);
1867 return (KERN_PROTECTION_FAILURE
);
1869 current
= current
->next
;
1873 * Go back and fix up protections. [Note that clipping is not
1874 * necessary the second time.]
1878 while ((current
!= &map
->header
) && (current
->start
< end
)) {
1881 vm_map_clip_end(map
, current
, end
, &count
);
1883 old_prot
= current
->protection
;
1885 current
->protection
=
1886 (current
->max_protection
= new_prot
) &
1889 current
->protection
= new_prot
;
1893 * Update physical map if necessary. Worry about copy-on-write
1894 * here -- CHECK THIS XXX
1897 if (current
->protection
!= old_prot
) {
1898 #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1901 pmap_protect(map
->pmap
, current
->start
,
1903 current
->protection
& MASK(current
));
1907 vm_map_simplify_entry(map
, current
, &count
);
1909 current
= current
->next
;
1913 vm_map_entry_release(count
);
1914 return (KERN_SUCCESS
);
1918 * This routine traverses a processes map handling the madvise
1919 * system call. Advisories are classified as either those effecting
1920 * the vm_map_entry structure, or those effecting the underlying
1923 * The <value> argument is used for extended madvise calls.
1928 vm_map_madvise(vm_map_t map
, vm_offset_t start
, vm_offset_t end
,
1929 int behav
, off_t value
)
1931 vm_map_entry_t current
, entry
;
1937 * Some madvise calls directly modify the vm_map_entry, in which case
1938 * we need to use an exclusive lock on the map and we need to perform
1939 * various clipping operations. Otherwise we only need a read-lock
1943 count
= vm_map_entry_reserve(MAP_RESERVE_COUNT
);
1947 case MADV_SEQUENTIAL
:
1961 vm_map_lock_read(map
);
1964 vm_map_entry_release(count
);
1969 * Locate starting entry and clip if necessary.
1972 VM_MAP_RANGE_CHECK(map
, start
, end
);
1974 if (vm_map_lookup_entry(map
, start
, &entry
)) {
1976 vm_map_clip_start(map
, entry
, start
, &count
);
1978 entry
= entry
->next
;
1983 * madvise behaviors that are implemented in the vm_map_entry.
1985 * We clip the vm_map_entry so that behavioral changes are
1986 * limited to the specified address range.
1988 for (current
= entry
;
1989 (current
!= &map
->header
) && (current
->start
< end
);
1990 current
= current
->next
1992 if (current
->maptype
== VM_MAPTYPE_SUBMAP
)
1995 vm_map_clip_end(map
, current
, end
, &count
);
1999 vm_map_entry_set_behavior(current
, MAP_ENTRY_BEHAV_NORMAL
);
2001 case MADV_SEQUENTIAL
:
2002 vm_map_entry_set_behavior(current
, MAP_ENTRY_BEHAV_SEQUENTIAL
);
2005 vm_map_entry_set_behavior(current
, MAP_ENTRY_BEHAV_RANDOM
);
2008 current
->eflags
|= MAP_ENTRY_NOSYNC
;
2011 current
->eflags
&= ~MAP_ENTRY_NOSYNC
;
2014 current
->eflags
|= MAP_ENTRY_NOCOREDUMP
;
2017 current
->eflags
&= ~MAP_ENTRY_NOCOREDUMP
;
2021 * Invalidate the related pmap entries, used
2022 * to flush portions of the real kernel's
2023 * pmap when the caller has removed or
2024 * modified existing mappings in a virtual
2027 pmap_remove(map
->pmap
,
2028 current
->start
, current
->end
);
2032 * Set the page directory page for a map
2033 * governed by a virtual page table. Mark
2034 * the entry as being governed by a virtual
2035 * page table if it is not.
2037 * XXX the page directory page is stored
2038 * in the avail_ssize field if the map_entry.
2040 * XXX the map simplification code does not
2041 * compare this field so weird things may
2042 * happen if you do not apply this function
2043 * to the entire mapping governed by the
2044 * virtual page table.
2046 if (current
->maptype
!= VM_MAPTYPE_VPAGETABLE
) {
2050 current
->aux
.master_pde
= value
;
2051 pmap_remove(map
->pmap
,
2052 current
->start
, current
->end
);
2058 vm_map_simplify_entry(map
, current
, &count
);
2066 * madvise behaviors that are implemented in the underlying
2069 * Since we don't clip the vm_map_entry, we have to clip
2070 * the vm_object pindex and count.
2072 * NOTE! We currently do not support these functions on
2073 * virtual page tables.
2075 for (current
= entry
;
2076 (current
!= &map
->header
) && (current
->start
< end
);
2077 current
= current
->next
2079 vm_offset_t useStart
;
2081 if (current
->maptype
!= VM_MAPTYPE_NORMAL
)
2084 pindex
= OFF_TO_IDX(current
->offset
);
2085 count
= atop(current
->end
- current
->start
);
2086 useStart
= current
->start
;
2088 if (current
->start
< start
) {
2089 pindex
+= atop(start
- current
->start
);
2090 count
-= atop(start
- current
->start
);
2093 if (current
->end
> end
)
2094 count
-= atop(current
->end
- end
);
2099 vm_object_madvise(current
->object
.vm_object
,
2100 pindex
, count
, behav
);
2103 * Try to populate the page table. Mappings governed
2104 * by virtual page tables cannot be pre-populated
2105 * without a lot of work so don't try.
2107 if (behav
== MADV_WILLNEED
&&
2108 current
->maptype
!= VM_MAPTYPE_VPAGETABLE
) {
2109 pmap_object_init_pt(
2112 current
->protection
,
2113 current
->object
.vm_object
,
2115 (count
<< PAGE_SHIFT
),
2116 MAP_PREFAULT_MADVISE
2120 vm_map_unlock_read(map
);
2122 vm_map_entry_release(count
);
2128 * Sets the inheritance of the specified address range in the target map.
2129 * Inheritance affects how the map will be shared with child maps at the
2130 * time of vm_map_fork.
2133 vm_map_inherit(vm_map_t map
, vm_offset_t start
, vm_offset_t end
,
2134 vm_inherit_t new_inheritance
)
2136 vm_map_entry_t entry
;
2137 vm_map_entry_t temp_entry
;
2140 switch (new_inheritance
) {
2141 case VM_INHERIT_NONE
:
2142 case VM_INHERIT_COPY
:
2143 case VM_INHERIT_SHARE
:
2146 return (KERN_INVALID_ARGUMENT
);
2149 count
= vm_map_entry_reserve(MAP_RESERVE_COUNT
);
2152 VM_MAP_RANGE_CHECK(map
, start
, end
);
2154 if (vm_map_lookup_entry(map
, start
, &temp_entry
)) {
2156 vm_map_clip_start(map
, entry
, start
, &count
);
2158 entry
= temp_entry
->next
;
2160 while ((entry
!= &map
->header
) && (entry
->start
< end
)) {
2161 vm_map_clip_end(map
, entry
, end
, &count
);
2163 entry
->inheritance
= new_inheritance
;
2165 vm_map_simplify_entry(map
, entry
, &count
);
2167 entry
= entry
->next
;
2170 vm_map_entry_release(count
);
2171 return (KERN_SUCCESS
);
2175 * Implement the semantics of mlock
2178 vm_map_unwire(vm_map_t map
, vm_offset_t start
, vm_offset_t real_end
,
2179 boolean_t new_pageable
)
2181 vm_map_entry_t entry
;
2182 vm_map_entry_t start_entry
;
2184 int rv
= KERN_SUCCESS
;
2187 count
= vm_map_entry_reserve(MAP_RESERVE_COUNT
);
2189 VM_MAP_RANGE_CHECK(map
, start
, real_end
);
2192 start_entry
= vm_map_clip_range(map
, start
, end
, &count
,
2194 if (start_entry
== NULL
) {
2196 vm_map_entry_release(count
);
2197 return (KERN_INVALID_ADDRESS
);
2200 if (new_pageable
== 0) {
2201 entry
= start_entry
;
2202 while ((entry
!= &map
->header
) && (entry
->start
< end
)) {
2203 vm_offset_t save_start
;
2204 vm_offset_t save_end
;
2207 * Already user wired or hard wired (trivial cases)
2209 if (entry
->eflags
& MAP_ENTRY_USER_WIRED
) {
2210 entry
= entry
->next
;
2213 if (entry
->wired_count
!= 0) {
2214 entry
->wired_count
++;
2215 entry
->eflags
|= MAP_ENTRY_USER_WIRED
;
2216 entry
= entry
->next
;
2221 * A new wiring requires instantiation of appropriate
2222 * management structures and the faulting in of the
2225 if (entry
->maptype
== VM_MAPTYPE_NORMAL
||
2226 entry
->maptype
== VM_MAPTYPE_VPAGETABLE
) {
2227 int copyflag
= entry
->eflags
&
2228 MAP_ENTRY_NEEDS_COPY
;
2229 if (copyflag
&& ((entry
->protection
&
2230 VM_PROT_WRITE
) != 0)) {
2231 vm_map_entry_shadow(entry
, 0);
2232 } else if (entry
->object
.vm_object
== NULL
&&
2234 vm_map_entry_allocate_object(entry
);
2237 entry
->wired_count
++;
2238 entry
->eflags
|= MAP_ENTRY_USER_WIRED
;
2241 * Now fault in the area. Note that vm_fault_wire()
2242 * may release the map lock temporarily, it will be
2243 * relocked on return. The in-transition
2244 * flag protects the entries.
2246 save_start
= entry
->start
;
2247 save_end
= entry
->end
;
2248 rv
= vm_fault_wire(map
, entry
, TRUE
, 0);
2250 CLIP_CHECK_BACK(entry
, save_start
);
2252 KASSERT(entry
->wired_count
== 1, ("bad wired_count on entry"));
2253 entry
->eflags
&= ~MAP_ENTRY_USER_WIRED
;
2254 entry
->wired_count
= 0;
2255 if (entry
->end
== save_end
)
2257 entry
= entry
->next
;
2258 KASSERT(entry
!= &map
->header
, ("bad entry clip during backout"));
2260 end
= save_start
; /* unwire the rest */
2264 * note that even though the entry might have been
2265 * clipped, the USER_WIRED flag we set prevents
2266 * duplication so we do not have to do a
2269 entry
= entry
->next
;
2273 * If we failed fall through to the unwiring section to
2274 * unwire what we had wired so far. 'end' has already
2281 * start_entry might have been clipped if we unlocked the
2282 * map and blocked. No matter how clipped it has gotten
2283 * there should be a fragment that is on our start boundary.
2285 CLIP_CHECK_BACK(start_entry
, start
);
2289 * Deal with the unwiring case.
2293 * This is the unwiring case. We must first ensure that the
2294 * range to be unwired is really wired down. We know there
2297 entry
= start_entry
;
2298 while ((entry
!= &map
->header
) && (entry
->start
< end
)) {
2299 if ((entry
->eflags
& MAP_ENTRY_USER_WIRED
) == 0) {
2300 rv
= KERN_INVALID_ARGUMENT
;
2303 KASSERT(entry
->wired_count
!= 0, ("wired count was 0 with USER_WIRED set! %p", entry
));
2304 entry
= entry
->next
;
2308 * Now decrement the wiring count for each region. If a region
2309 * becomes completely unwired, unwire its physical pages and
2313 * The map entries are processed in a loop, checking to
2314 * make sure the entry is wired and asserting it has a wired
2315 * count. However, another loop was inserted more-or-less in
2316 * the middle of the unwiring path. This loop picks up the
2317 * "entry" loop variable from the first loop without first
2318 * setting it to start_entry. Naturally, the secound loop
2319 * is never entered and the pages backing the entries are
2320 * never unwired. This can lead to a leak of wired pages.
2322 entry
= start_entry
;
2323 while ((entry
!= &map
->header
) && (entry
->start
< end
)) {
2324 KASSERT(entry
->eflags
& MAP_ENTRY_USER_WIRED
,
2325 ("expected USER_WIRED on entry %p", entry
));
2326 entry
->eflags
&= ~MAP_ENTRY_USER_WIRED
;
2327 entry
->wired_count
--;
2328 if (entry
->wired_count
== 0)
2329 vm_fault_unwire(map
, entry
);
2330 entry
= entry
->next
;
2334 vm_map_unclip_range(map
, start_entry
, start
, real_end
, &count
,
2338 vm_map_entry_release(count
);
2343 * Sets the pageability of the specified address range in the target map.
2344 * Regions specified as not pageable require locked-down physical
2345 * memory and physical page maps.
2347 * The map must not be locked, but a reference must remain to the map
2348 * throughout the call.
2350 * This function may be called via the zalloc path and must properly
2351 * reserve map entries for kernel_map.
2356 vm_map_wire(vm_map_t map
, vm_offset_t start
, vm_offset_t real_end
, int kmflags
)
2358 vm_map_entry_t entry
;
2359 vm_map_entry_t start_entry
;
2361 int rv
= KERN_SUCCESS
;
2364 if (kmflags
& KM_KRESERVE
)
2365 count
= vm_map_entry_kreserve(MAP_RESERVE_COUNT
);
2367 count
= vm_map_entry_reserve(MAP_RESERVE_COUNT
);
2369 VM_MAP_RANGE_CHECK(map
, start
, real_end
);
2372 start_entry
= vm_map_clip_range(map
, start
, end
, &count
,
2374 if (start_entry
== NULL
) {
2376 rv
= KERN_INVALID_ADDRESS
;
2379 if ((kmflags
& KM_PAGEABLE
) == 0) {
2383 * 1. Holding the write lock, we create any shadow or zero-fill
2384 * objects that need to be created. Then we clip each map
2385 * entry to the region to be wired and increment its wiring
2386 * count. We create objects before clipping the map entries
2387 * to avoid object proliferation.
2389 * 2. We downgrade to a read lock, and call vm_fault_wire to
2390 * fault in the pages for any newly wired area (wired_count is
2393 * Downgrading to a read lock for vm_fault_wire avoids a
2394 * possible deadlock with another process that may have faulted
2395 * on one of the pages to be wired (it would mark the page busy,
2396 * blocking us, then in turn block on the map lock that we
2397 * hold). Because of problems in the recursive lock package,
2398 * we cannot upgrade to a write lock in vm_map_lookup. Thus,
2399 * any actions that require the write lock must be done
2400 * beforehand. Because we keep the read lock on the map, the
2401 * copy-on-write status of the entries we modify here cannot
2404 entry
= start_entry
;
2405 while ((entry
!= &map
->header
) && (entry
->start
< end
)) {
2407 * Trivial case if the entry is already wired
2409 if (entry
->wired_count
) {
2410 entry
->wired_count
++;
2411 entry
= entry
->next
;
2416 * The entry is being newly wired, we have to setup
2417 * appropriate management structures. A shadow
2418 * object is required for a copy-on-write region,
2419 * or a normal object for a zero-fill region. We
2420 * do not have to do this for entries that point to sub
2421 * maps because we won't hold the lock on the sub map.
2423 if (entry
->maptype
== VM_MAPTYPE_NORMAL
||
2424 entry
->maptype
== VM_MAPTYPE_VPAGETABLE
) {
2425 int copyflag
= entry
->eflags
&
2426 MAP_ENTRY_NEEDS_COPY
;
2427 if (copyflag
&& ((entry
->protection
&
2428 VM_PROT_WRITE
) != 0)) {
2429 vm_map_entry_shadow(entry
, 0);
2430 } else if (entry
->object
.vm_object
== NULL
&&
2432 vm_map_entry_allocate_object(entry
);
2436 entry
->wired_count
++;
2437 entry
= entry
->next
;
2445 * HACK HACK HACK HACK
2447 * vm_fault_wire() temporarily unlocks the map to avoid
2448 * deadlocks. The in-transition flag from vm_map_clip_range
2449 * call should protect us from changes while the map is
2452 * NOTE: Previously this comment stated that clipping might
2453 * still occur while the entry is unlocked, but from
2454 * what I can tell it actually cannot.
2456 * It is unclear whether the CLIP_CHECK_*() calls
2457 * are still needed but we keep them in anyway.
2459 * HACK HACK HACK HACK
2462 entry
= start_entry
;
2463 while (entry
!= &map
->header
&& entry
->start
< end
) {
2465 * If vm_fault_wire fails for any page we need to undo
2466 * what has been done. We decrement the wiring count
2467 * for those pages which have not yet been wired (now)
2468 * and unwire those that have (later).
2470 vm_offset_t save_start
= entry
->start
;
2471 vm_offset_t save_end
= entry
->end
;
2473 if (entry
->wired_count
== 1)
2474 rv
= vm_fault_wire(map
, entry
, FALSE
, kmflags
);
2476 CLIP_CHECK_BACK(entry
, save_start
);
2478 KASSERT(entry
->wired_count
== 1, ("wired_count changed unexpectedly"));
2479 entry
->wired_count
= 0;
2480 if (entry
->end
== save_end
)
2482 entry
= entry
->next
;
2483 KASSERT(entry
!= &map
->header
, ("bad entry clip during backout"));
2488 CLIP_CHECK_FWD(entry
, save_end
);
2489 entry
= entry
->next
;
2493 * If a failure occured undo everything by falling through
2494 * to the unwiring code. 'end' has already been adjusted
2498 kmflags
|= KM_PAGEABLE
;
2501 * start_entry is still IN_TRANSITION but may have been
2502 * clipped since vm_fault_wire() unlocks and relocks the
2503 * map. No matter how clipped it has gotten there should
2504 * be a fragment that is on our start boundary.
2506 CLIP_CHECK_BACK(start_entry
, start
);
2509 if (kmflags
& KM_PAGEABLE
) {
2511 * This is the unwiring case. We must first ensure that the
2512 * range to be unwired is really wired down. We know there
2515 entry
= start_entry
;
2516 while ((entry
!= &map
->header
) && (entry
->start
< end
)) {
2517 if (entry
->wired_count
== 0) {
2518 rv
= KERN_INVALID_ARGUMENT
;
2521 entry
= entry
->next
;
2525 * Now decrement the wiring count for each region. If a region
2526 * becomes completely unwired, unwire its physical pages and
2529 entry
= start_entry
;
2530 while ((entry
!= &map
->header
) && (entry
->start
< end
)) {
2531 entry
->wired_count
--;
2532 if (entry
->wired_count
== 0)
2533 vm_fault_unwire(map
, entry
);
2534 entry
= entry
->next
;
2538 vm_map_unclip_range(map
, start_entry
, start
, real_end
,
2539 &count
, MAP_CLIP_NO_HOLES
);
2543 if (kmflags
& KM_KRESERVE
)
2544 vm_map_entry_krelease(count
);
2546 vm_map_entry_release(count
);
2551 * Mark a newly allocated address range as wired but do not fault in
2552 * the pages. The caller is expected to load the pages into the object.
2554 * The map must be locked on entry and will remain locked on return.
2555 * No other requirements.
2558 vm_map_set_wired_quick(vm_map_t map
, vm_offset_t addr
, vm_size_t size
,
2561 vm_map_entry_t scan
;
2562 vm_map_entry_t entry
;
2564 entry
= vm_map_clip_range(map
, addr
, addr
+ size
,
2565 countp
, MAP_CLIP_NO_HOLES
);
2567 scan
!= &map
->header
&& scan
->start
< addr
+ size
;
2568 scan
= scan
->next
) {
2569 KKASSERT(scan
->wired_count
== 0);
2570 scan
->wired_count
= 1;
2572 vm_map_unclip_range(map
, entry
, addr
, addr
+ size
,
2573 countp
, MAP_CLIP_NO_HOLES
);
2577 * Push any dirty cached pages in the address range to their pager.
2578 * If syncio is TRUE, dirty pages are written synchronously.
2579 * If invalidate is TRUE, any cached pages are freed as well.
2581 * This routine is called by sys_msync()
2583 * Returns an error if any part of the specified range is not mapped.
2588 vm_map_clean(vm_map_t map
, vm_offset_t start
, vm_offset_t end
,
2589 boolean_t syncio
, boolean_t invalidate
)
2591 vm_map_entry_t current
;
2592 vm_map_entry_t entry
;
2596 vm_ooffset_t offset
;
2598 vm_map_lock_read(map
);
2599 VM_MAP_RANGE_CHECK(map
, start
, end
);
2600 if (!vm_map_lookup_entry(map
, start
, &entry
)) {
2601 vm_map_unlock_read(map
);
2602 return (KERN_INVALID_ADDRESS
);
2604 lwkt_gettoken(&map
->token
);
2607 * Make a first pass to check for holes.
2609 for (current
= entry
; current
->start
< end
; current
= current
->next
) {
2610 if (current
->maptype
== VM_MAPTYPE_SUBMAP
) {
2611 lwkt_reltoken(&map
->token
);
2612 vm_map_unlock_read(map
);
2613 return (KERN_INVALID_ARGUMENT
);
2615 if (end
> current
->end
&&
2616 (current
->next
== &map
->header
||
2617 current
->end
!= current
->next
->start
)) {
2618 lwkt_reltoken(&map
->token
);
2619 vm_map_unlock_read(map
);
2620 return (KERN_INVALID_ADDRESS
);
2625 pmap_remove(vm_map_pmap(map
), start
, end
);
2628 * Make a second pass, cleaning/uncaching pages from the indicated
2631 for (current
= entry
; current
->start
< end
; current
= current
->next
) {
2632 offset
= current
->offset
+ (start
- current
->start
);
2633 size
= (end
<= current
->end
? end
: current
->end
) - start
;
2635 switch(current
->maptype
) {
2636 case VM_MAPTYPE_SUBMAP
:
2639 vm_map_entry_t tentry
;
2642 smap
= current
->object
.sub_map
;
2643 vm_map_lock_read(smap
);
2644 vm_map_lookup_entry(smap
, offset
, &tentry
);
2645 tsize
= tentry
->end
- offset
;
2648 object
= tentry
->object
.vm_object
;
2649 offset
= tentry
->offset
+ (offset
- tentry
->start
);
2650 vm_map_unlock_read(smap
);
2653 case VM_MAPTYPE_NORMAL
:
2654 case VM_MAPTYPE_VPAGETABLE
:
2655 object
= current
->object
.vm_object
;
2663 vm_object_hold(object
);
2666 * Note that there is absolutely no sense in writing out
2667 * anonymous objects, so we track down the vnode object
2669 * We invalidate (remove) all pages from the address space
2670 * anyway, for semantic correctness.
2672 * note: certain anonymous maps, such as MAP_NOSYNC maps,
2673 * may start out with a NULL object.
2675 while (object
&& (tobj
= object
->backing_object
) != NULL
) {
2676 vm_object_hold(tobj
);
2677 if (tobj
== object
->backing_object
) {
2678 vm_object_lock_swap();
2679 offset
+= object
->backing_object_offset
;
2680 vm_object_drop(object
);
2682 if (object
->size
< OFF_TO_IDX(offset
+ size
))
2683 size
= IDX_TO_OFF(object
->size
) -
2687 vm_object_drop(tobj
);
2689 if (object
&& (object
->type
== OBJT_VNODE
) &&
2690 (current
->protection
& VM_PROT_WRITE
) &&
2691 (object
->flags
& OBJ_NOMSYNC
) == 0) {
2693 * Flush pages if writing is allowed, invalidate them
2694 * if invalidation requested. Pages undergoing I/O
2695 * will be ignored by vm_object_page_remove().
2697 * We cannot lock the vnode and then wait for paging
2698 * to complete without deadlocking against vm_fault.
2699 * Instead we simply call vm_object_page_remove() and
2700 * allow it to block internally on a page-by-page
2701 * basis when it encounters pages undergoing async
2706 /* no chain wait needed for vnode objects */
2707 vm_object_reference_locked(object
);
2708 vn_lock(object
->handle
, LK_EXCLUSIVE
| LK_RETRY
);
2709 flags
= (syncio
|| invalidate
) ? OBJPC_SYNC
: 0;
2710 flags
|= invalidate
? OBJPC_INVAL
: 0;
2713 * When operating on a virtual page table just
2714 * flush the whole object. XXX we probably ought
2717 switch(current
->maptype
) {
2718 case VM_MAPTYPE_NORMAL
:
2719 vm_object_page_clean(object
,
2721 OFF_TO_IDX(offset
+ size
+ PAGE_MASK
),
2724 case VM_MAPTYPE_VPAGETABLE
:
2725 vm_object_page_clean(object
, 0, 0, flags
);
2728 vn_unlock(((struct vnode
*)object
->handle
));
2729 vm_object_deallocate_locked(object
);
2731 if (object
&& invalidate
&&
2732 ((object
->type
== OBJT_VNODE
) ||
2733 (object
->type
== OBJT_DEVICE
) ||
2734 (object
->type
== OBJT_MGTDEVICE
))) {
2736 ((object
->type
== OBJT_DEVICE
) ||
2737 (object
->type
== OBJT_MGTDEVICE
)) ? FALSE
: TRUE
;
2738 /* no chain wait needed for vnode/device objects */
2739 vm_object_reference_locked(object
);
2740 switch(current
->maptype
) {
2741 case VM_MAPTYPE_NORMAL
:
2742 vm_object_page_remove(object
,
2744 OFF_TO_IDX(offset
+ size
+ PAGE_MASK
),
2747 case VM_MAPTYPE_VPAGETABLE
:
2748 vm_object_page_remove(object
, 0, 0, clean_only
);
2751 vm_object_deallocate_locked(object
);
2755 vm_object_drop(object
);
2758 lwkt_reltoken(&map
->token
);
2759 vm_map_unlock_read(map
);
2761 return (KERN_SUCCESS
);
2765 * Make the region specified by this entry pageable.
2767 * The vm_map must be exclusively locked.
2770 vm_map_entry_unwire(vm_map_t map
, vm_map_entry_t entry
)
2772 entry
->eflags
&= ~MAP_ENTRY_USER_WIRED
;
2773 entry
->wired_count
= 0;
2774 vm_fault_unwire(map
, entry
);
2778 * Deallocate the given entry from the target map.
2780 * The vm_map must be exclusively locked.
2783 vm_map_entry_delete(vm_map_t map
, vm_map_entry_t entry
, int *countp
)
2785 vm_map_entry_unlink(map
, entry
);
2786 map
->size
-= entry
->end
- entry
->start
;
2788 switch(entry
->maptype
) {
2789 case VM_MAPTYPE_NORMAL
:
2790 case VM_MAPTYPE_VPAGETABLE
:
2791 case VM_MAPTYPE_SUBMAP
:
2792 vm_object_deallocate(entry
->object
.vm_object
);
2794 case VM_MAPTYPE_UKSMAP
:
2801 vm_map_entry_dispose(map
, entry
, countp
);
2805 * Deallocates the given address range from the target map.
2807 * The vm_map must be exclusively locked.
2810 vm_map_delete(vm_map_t map
, vm_offset_t start
, vm_offset_t end
, int *countp
)
2813 vm_map_entry_t entry
;
2814 vm_map_entry_t first_entry
;
2816 ASSERT_VM_MAP_LOCKED(map
);
2817 lwkt_gettoken(&map
->token
);
2820 * Find the start of the region, and clip it. Set entry to point
2821 * at the first record containing the requested address or, if no
2822 * such record exists, the next record with a greater address. The
2823 * loop will run from this point until a record beyond the termination
2824 * address is encountered.
2826 * map->hint must be adjusted to not point to anything we delete,
2827 * so set it to the entry prior to the one being deleted.
2829 * GGG see other GGG comment.
2831 if (vm_map_lookup_entry(map
, start
, &first_entry
)) {
2832 entry
= first_entry
;
2833 vm_map_clip_start(map
, entry
, start
, countp
);
2834 map
->hint
= entry
->prev
; /* possible problem XXX */
2836 map
->hint
= first_entry
; /* possible problem XXX */
2837 entry
= first_entry
->next
;
2841 * If a hole opens up prior to the current first_free then
2842 * adjust first_free. As with map->hint, map->first_free
2843 * cannot be left set to anything we might delete.
2845 if (entry
== &map
->header
) {
2846 map
->first_free
= &map
->header
;
2847 } else if (map
->first_free
->start
>= start
) {
2848 map
->first_free
= entry
->prev
;
2852 * Step through all entries in this region
2854 while ((entry
!= &map
->header
) && (entry
->start
< end
)) {
2855 vm_map_entry_t next
;
2857 vm_pindex_t offidxstart
, offidxend
, count
;
2860 * If we hit an in-transition entry we have to sleep and
2861 * retry. It's easier (and not really slower) to just retry
2862 * since this case occurs so rarely and the hint is already
2863 * pointing at the right place. We have to reset the
2864 * start offset so as not to accidently delete an entry
2865 * another process just created in vacated space.
2867 if (entry
->eflags
& MAP_ENTRY_IN_TRANSITION
) {
2868 entry
->eflags
|= MAP_ENTRY_NEEDS_WAKEUP
;
2869 start
= entry
->start
;
2870 ++mycpu
->gd_cnt
.v_intrans_coll
;
2871 ++mycpu
->gd_cnt
.v_intrans_wait
;
2872 vm_map_transition_wait(map
);
2875 vm_map_clip_end(map
, entry
, end
, countp
);
2881 offidxstart
= OFF_TO_IDX(entry
->offset
);
2882 count
= OFF_TO_IDX(e
- s
);
2884 switch(entry
->maptype
) {
2885 case VM_MAPTYPE_NORMAL
:
2886 case VM_MAPTYPE_VPAGETABLE
:
2887 case VM_MAPTYPE_SUBMAP
:
2888 object
= entry
->object
.vm_object
;
2896 * Unwire before removing addresses from the pmap; otherwise,
2897 * unwiring will put the entries back in the pmap.
2899 if (entry
->wired_count
!= 0)
2900 vm_map_entry_unwire(map
, entry
);
2902 offidxend
= offidxstart
+ count
;
2904 if (object
== &kernel_object
) {
2905 vm_object_hold(object
);
2906 vm_object_page_remove(object
, offidxstart
,
2908 vm_object_drop(object
);
2909 } else if (object
&& object
->type
!= OBJT_DEFAULT
&&
2910 object
->type
!= OBJT_SWAP
) {
2912 * vnode object routines cannot be chain-locked,
2913 * but since we aren't removing pages from the
2914 * object here we can use a shared hold.
2916 vm_object_hold_shared(object
);
2917 pmap_remove(map
->pmap
, s
, e
);
2918 vm_object_drop(object
);
2919 } else if (object
) {
2920 vm_object_hold(object
);
2921 vm_object_chain_acquire(object
, 0);
2922 pmap_remove(map
->pmap
, s
, e
);
2924 if (object
!= NULL
&&
2925 object
->ref_count
!= 1 &&
2926 (object
->flags
& (OBJ_NOSPLIT
|OBJ_ONEMAPPING
)) ==
2928 (object
->type
== OBJT_DEFAULT
||
2929 object
->type
== OBJT_SWAP
)) {
2930 vm_object_collapse(object
, NULL
);
2931 vm_object_page_remove(object
, offidxstart
,
2933 if (object
->type
== OBJT_SWAP
) {
2934 swap_pager_freespace(object
,
2938 if (offidxend
>= object
->size
&&
2939 offidxstart
< object
->size
) {
2940 object
->size
= offidxstart
;
2943 vm_object_chain_release(object
);
2944 vm_object_drop(object
);
2945 } else if (entry
->maptype
== VM_MAPTYPE_UKSMAP
) {
2946 pmap_remove(map
->pmap
, s
, e
);
2950 * Delete the entry (which may delete the object) only after
2951 * removing all pmap entries pointing to its pages.
2952 * (Otherwise, its page frames may be reallocated, and any
2953 * modify bits will be set in the wrong object!)
2955 vm_map_entry_delete(map
, entry
, countp
);
2958 lwkt_reltoken(&map
->token
);
2959 return (KERN_SUCCESS
);
2963 * Remove the given address range from the target map.
2964 * This is the exported form of vm_map_delete.
2969 vm_map_remove(vm_map_t map
, vm_offset_t start
, vm_offset_t end
)
2974 count
= vm_map_entry_reserve(MAP_RESERVE_COUNT
);
2976 VM_MAP_RANGE_CHECK(map
, start
, end
);
2977 result
= vm_map_delete(map
, start
, end
, &count
);
2979 vm_map_entry_release(count
);
2985 * Assert that the target map allows the specified privilege on the
2986 * entire address region given. The entire region must be allocated.
2988 * The caller must specify whether the vm_map is already locked or not.
2991 vm_map_check_protection(vm_map_t map
, vm_offset_t start
, vm_offset_t end
,
2992 vm_prot_t protection
, boolean_t have_lock
)
2994 vm_map_entry_t entry
;
2995 vm_map_entry_t tmp_entry
;
2998 if (have_lock
== FALSE
)
2999 vm_map_lock_read(map
);
3001 if (!vm_map_lookup_entry(map
, start
, &tmp_entry
)) {
3002 if (have_lock
== FALSE
)
3003 vm_map_unlock_read(map
);
3009 while (start
< end
) {
3010 if (entry
== &map
->header
) {
3018 if (start
< entry
->start
) {
3023 * Check protection associated with entry.
3026 if ((entry
->protection
& protection
) != protection
) {
3030 /* go to next entry */
3033 entry
= entry
->next
;
3035 if (have_lock
== FALSE
)
3036 vm_map_unlock_read(map
);
3041 * If appropriate this function shadows the original object with a new object
3042 * and moves the VM pages from the original object to the new object.
3043 * The original object will also be collapsed, if possible.
3045 * We can only do this for normal memory objects with a single mapping, and
3046 * it only makes sense to do it if there are 2 or more refs on the original
3047 * object. i.e. typically a memory object that has been extended into
3048 * multiple vm_map_entry's with non-overlapping ranges.
3050 * This makes it easier to remove unused pages and keeps object inheritance
3051 * from being a negative impact on memory usage.
3053 * On return the (possibly new) entry->object.vm_object will have an
3054 * additional ref on it for the caller to dispose of (usually by cloning
3055 * the vm_map_entry). The additional ref had to be done in this routine
3056 * to avoid racing a collapse. The object's ONEMAPPING flag will also be
3059 * The vm_map must be locked and its token held.
3062 vm_map_split(vm_map_entry_t entry
)
3065 vm_object_t oobject
, nobject
, bobject
;
3068 vm_pindex_t offidxstart
, offidxend
, idx
;
3070 vm_ooffset_t offset
;
3074 * Optimize away object locks for vnode objects. Important exit/exec
3077 * OBJ_ONEMAPPING doesn't apply to vnode objects but clear the flag
3080 oobject
= entry
->object
.vm_object
;
3081 if (oobject
->type
!= OBJT_DEFAULT
&& oobject
->type
!= OBJT_SWAP
) {
3082 vm_object_reference_quick(oobject
);
3083 vm_object_clear_flag(oobject
, OBJ_ONEMAPPING
);
3088 * Setup. Chain lock the original object throughout the entire
3089 * routine to prevent new page faults from occuring.
3091 * XXX can madvise WILLNEED interfere with us too?
3093 vm_object_hold(oobject
);
3094 vm_object_chain_acquire(oobject
, 0);
3097 * Original object cannot be split? Might have also changed state.
3099 if (oobject
->handle
== NULL
|| (oobject
->type
!= OBJT_DEFAULT
&&
3100 oobject
->type
!= OBJT_SWAP
)) {
3101 vm_object_chain_release(oobject
);
3102 vm_object_reference_locked(oobject
);
3103 vm_object_clear_flag(oobject
, OBJ_ONEMAPPING
);
3104 vm_object_drop(oobject
);
3109 * Collapse original object with its backing store as an
3110 * optimization to reduce chain lengths when possible.
3112 * If ref_count <= 1 there aren't other non-overlapping vm_map_entry's
3113 * for oobject, so there's no point collapsing it.
3115 * Then re-check whether the object can be split.
3117 vm_object_collapse(oobject
, NULL
);
3119 if (oobject
->ref_count
<= 1 ||
3120 (oobject
->type
!= OBJT_DEFAULT
&& oobject
->type
!= OBJT_SWAP
) ||
3121 (oobject
->flags
& (OBJ_NOSPLIT
|OBJ_ONEMAPPING
)) != OBJ_ONEMAPPING
) {
3122 vm_object_chain_release(oobject
);
3123 vm_object_reference_locked(oobject
);
3124 vm_object_clear_flag(oobject
, OBJ_ONEMAPPING
);
3125 vm_object_drop(oobject
);
3130 * Acquire the chain lock on the backing object.
3132 * Give bobject an additional ref count for when it will be shadowed
3136 if ((bobject
= oobject
->backing_object
) != NULL
) {
3137 if (bobject
->type
!= OBJT_VNODE
) {
3139 vm_object_hold(bobject
);
3140 vm_object_chain_wait(bobject
, 0);
3141 vm_object_reference_locked(bobject
);
3142 vm_object_chain_acquire(bobject
, 0);
3143 KKASSERT(bobject
->backing_object
== bobject
);
3144 KKASSERT((bobject
->flags
& OBJ_DEAD
) == 0);
3146 vm_object_reference_quick(bobject
);
3151 * Calculate the object page range and allocate the new object.
3153 offset
= entry
->offset
;
3157 offidxstart
= OFF_TO_IDX(offset
);
3158 offidxend
= offidxstart
+ OFF_TO_IDX(e
- s
);
3159 size
= offidxend
- offidxstart
;
3161 switch(oobject
->type
) {
3163 nobject
= default_pager_alloc(NULL
, IDX_TO_OFF(size
),
3167 nobject
= swap_pager_alloc(NULL
, IDX_TO_OFF(size
),
3176 if (nobject
== NULL
) {
3178 if (useshadowlist
) {
3179 vm_object_chain_release(bobject
);
3180 vm_object_deallocate(bobject
);
3181 vm_object_drop(bobject
);
3183 vm_object_deallocate(bobject
);
3186 vm_object_chain_release(oobject
);
3187 vm_object_reference_locked(oobject
);
3188 vm_object_clear_flag(oobject
, OBJ_ONEMAPPING
);
3189 vm_object_drop(oobject
);
3194 * The new object will replace entry->object.vm_object so it needs
3195 * a second reference (the caller expects an additional ref).
3197 vm_object_hold(nobject
);
3198 vm_object_reference_locked(nobject
);
3199 vm_object_chain_acquire(nobject
, 0);
3202 * nobject shadows bobject (oobject already shadows bobject).
3205 nobject
->backing_object_offset
=
3206 oobject
->backing_object_offset
+ IDX_TO_OFF(offidxstart
);
3207 nobject
->backing_object
= bobject
;
3208 if (useshadowlist
) {
3209 bobject
->shadow_count
++;
3210 bobject
->generation
++;
3211 LIST_INSERT_HEAD(&bobject
->shadow_head
,
3212 nobject
, shadow_list
);
3213 vm_object_clear_flag(bobject
, OBJ_ONEMAPPING
); /*XXX*/
3214 vm_object_chain_release(bobject
);
3215 vm_object_drop(bobject
);
3216 vm_object_set_flag(nobject
, OBJ_ONSHADOW
);
3221 * Move the VM pages from oobject to nobject
3223 for (idx
= 0; idx
< size
; idx
++) {
3226 m
= vm_page_lookup_busy_wait(oobject
, offidxstart
+ idx
,
3232 * We must wait for pending I/O to complete before we can
3235 * We do not have to VM_PROT_NONE the page as mappings should
3236 * not be changed by this operation.
3238 * NOTE: The act of renaming a page updates chaingen for both
3241 vm_page_rename(m
, nobject
, idx
);
3242 /* page automatically made dirty by rename and cache handled */
3243 /* page remains busy */
3246 if (oobject
->type
== OBJT_SWAP
) {
3247 vm_object_pip_add(oobject
, 1);
3249 * copy oobject pages into nobject and destroy unneeded
3250 * pages in shadow object.
3252 swap_pager_copy(oobject
, nobject
, offidxstart
, 0);
3253 vm_object_pip_wakeup(oobject
);
3257 * Wakeup the pages we played with. No spl protection is needed
3258 * for a simple wakeup.
3260 for (idx
= 0; idx
< size
; idx
++) {
3261 m
= vm_page_lookup(nobject
, idx
);
3263 KKASSERT(m
->flags
& PG_BUSY
);
3267 entry
->object
.vm_object
= nobject
;
3268 entry
->offset
= 0LL;
3273 * NOTE: There is no need to remove OBJ_ONEMAPPING from oobject, the
3274 * related pages were moved and are no longer applicable to the
3277 * NOTE: Deallocate oobject (due to its entry->object.vm_object being
3278 * replaced by nobject).
3280 vm_object_chain_release(nobject
);
3281 vm_object_drop(nobject
);
3282 if (bobject
&& useshadowlist
) {
3283 vm_object_chain_release(bobject
);
3284 vm_object_drop(bobject
);
3286 vm_object_chain_release(oobject
);
3287 /*vm_object_clear_flag(oobject, OBJ_ONEMAPPING);*/
3288 vm_object_deallocate_locked(oobject
);
3289 vm_object_drop(oobject
);
3293 * Copies the contents of the source entry to the destination
3294 * entry. The entries *must* be aligned properly.
3296 * The vm_maps must be exclusively locked.
3297 * The vm_map's token must be held.
3299 * Because the maps are locked no faults can be in progress during the
3303 vm_map_copy_entry(vm_map_t src_map
, vm_map_t dst_map
,
3304 vm_map_entry_t src_entry
, vm_map_entry_t dst_entry
)
3306 vm_object_t src_object
;
3308 if (dst_entry
->maptype
== VM_MAPTYPE_SUBMAP
||
3309 dst_entry
->maptype
== VM_MAPTYPE_UKSMAP
)
3311 if (src_entry
->maptype
== VM_MAPTYPE_SUBMAP
||
3312 src_entry
->maptype
== VM_MAPTYPE_UKSMAP
)
3315 if (src_entry
->wired_count
== 0) {
3317 * If the source entry is marked needs_copy, it is already
3320 if ((src_entry
->eflags
& MAP_ENTRY_NEEDS_COPY
) == 0) {
3321 pmap_protect(src_map
->pmap
,
3324 src_entry
->protection
& ~VM_PROT_WRITE
);
3328 * Make a copy of the object.
3330 * The object must be locked prior to checking the object type
3331 * and for the call to vm_object_collapse() and vm_map_split().
3332 * We cannot use *_hold() here because the split code will
3333 * probably try to destroy the object. The lock is a pool
3334 * token and doesn't care.
3336 * We must bump src_map->timestamp when setting
3337 * MAP_ENTRY_NEEDS_COPY to force any concurrent fault
3338 * to retry, otherwise the concurrent fault might improperly
3339 * install a RW pte when its supposed to be a RO(COW) pte.
3340 * This race can occur because a vnode-backed fault may have
3341 * to temporarily release the map lock.
3343 if (src_entry
->object
.vm_object
!= NULL
) {
3344 vm_map_split(src_entry
);
3345 src_object
= src_entry
->object
.vm_object
;
3346 dst_entry
->object
.vm_object
= src_object
;
3347 src_entry
->eflags
|= (MAP_ENTRY_COW
|
3348 MAP_ENTRY_NEEDS_COPY
);
3349 dst_entry
->eflags
|= (MAP_ENTRY_COW
|
3350 MAP_ENTRY_NEEDS_COPY
);
3351 dst_entry
->offset
= src_entry
->offset
;
3352 ++src_map
->timestamp
;
3354 dst_entry
->object
.vm_object
= NULL
;
3355 dst_entry
->offset
= 0;
3358 pmap_copy(dst_map
->pmap
, src_map
->pmap
, dst_entry
->start
,
3359 dst_entry
->end
- dst_entry
->start
, src_entry
->start
);
3362 * Of course, wired down pages can't be set copy-on-write.
3363 * Cause wired pages to be copied into the new map by
3364 * simulating faults (the new pages are pageable)
3366 vm_fault_copy_entry(dst_map
, src_map
, dst_entry
, src_entry
);
3372 * Create a new process vmspace structure and vm_map
3373 * based on those of an existing process. The new map
3374 * is based on the old map, according to the inheritance
3375 * values on the regions in that map.
3377 * The source map must not be locked.
3380 static void vmspace_fork_normal_entry(vm_map_t old_map
, vm_map_t new_map
,
3381 vm_map_entry_t old_entry
, int *countp
);
3382 static void vmspace_fork_uksmap_entry(vm_map_t old_map
, vm_map_t new_map
,
3383 vm_map_entry_t old_entry
, int *countp
);
3386 vmspace_fork(struct vmspace
*vm1
)
3388 struct vmspace
*vm2
;
3389 vm_map_t old_map
= &vm1
->vm_map
;
3391 vm_map_entry_t old_entry
;
3394 lwkt_gettoken(&vm1
->vm_map
.token
);
3395 vm_map_lock(old_map
);
3397 vm2
= vmspace_alloc(old_map
->min_offset
, old_map
->max_offset
);
3398 lwkt_gettoken(&vm2
->vm_map
.token
);
3399 bcopy(&vm1
->vm_startcopy
, &vm2
->vm_startcopy
,
3400 (caddr_t
)&vm1
->vm_endcopy
- (caddr_t
)&vm1
->vm_startcopy
);
3401 new_map
= &vm2
->vm_map
; /* XXX */
3402 new_map
->timestamp
= 1;
3404 vm_map_lock(new_map
);
3407 old_entry
= old_map
->header
.next
;
3408 while (old_entry
!= &old_map
->header
) {
3410 old_entry
= old_entry
->next
;
3413 count
= vm_map_entry_reserve(count
+ MAP_RESERVE_COUNT
);
3415 old_entry
= old_map
->header
.next
;
3416 while (old_entry
!= &old_map
->header
) {
3417 switch(old_entry
->maptype
) {
3418 case VM_MAPTYPE_SUBMAP
:
3419 panic("vm_map_fork: encountered a submap");
3421 case VM_MAPTYPE_UKSMAP
:
3422 vmspace_fork_uksmap_entry(old_map
, new_map
,
3425 case VM_MAPTYPE_NORMAL
:
3426 case VM_MAPTYPE_VPAGETABLE
:
3427 vmspace_fork_normal_entry(old_map
, new_map
,
3431 old_entry
= old_entry
->next
;
3434 new_map
->size
= old_map
->size
;
3435 vm_map_unlock(old_map
);
3436 vm_map_unlock(new_map
);
3437 vm_map_entry_release(count
);
3439 lwkt_reltoken(&vm2
->vm_map
.token
);
3440 lwkt_reltoken(&vm1
->vm_map
.token
);
3447 vmspace_fork_normal_entry(vm_map_t old_map
, vm_map_t new_map
,
3448 vm_map_entry_t old_entry
, int *countp
)
3450 vm_map_entry_t new_entry
;
3453 switch (old_entry
->inheritance
) {
3454 case VM_INHERIT_NONE
:
3456 case VM_INHERIT_SHARE
:
3458 * Clone the entry, creating the shared object if
3461 if (old_entry
->object
.vm_object
== NULL
)
3462 vm_map_entry_allocate_object(old_entry
);
3464 if (old_entry
->eflags
& MAP_ENTRY_NEEDS_COPY
) {
3466 * Shadow a map_entry which needs a copy,
3467 * replacing its object with a new object
3468 * that points to the old one. Ask the
3469 * shadow code to automatically add an
3470 * additional ref. We can't do it afterwords
3471 * because we might race a collapse. The call
3472 * to vm_map_entry_shadow() will also clear
3475 vm_map_entry_shadow(old_entry
, 1);
3476 } else if (old_entry
->object
.vm_object
) {
3478 * We will make a shared copy of the object,
3479 * and must clear OBJ_ONEMAPPING.
3481 * Optimize vnode objects. OBJ_ONEMAPPING
3482 * is non-applicable but clear it anyway,
3483 * and its terminal so we don'th ave to deal
3484 * with chains. Reduces SMP conflicts.
3486 * XXX assert that object.vm_object != NULL
3487 * since we allocate it above.
3489 object
= old_entry
->object
.vm_object
;
3490 if (object
->type
== OBJT_VNODE
) {
3491 vm_object_reference_quick(object
);
3492 vm_object_clear_flag(object
,
3495 vm_object_hold(object
);
3496 vm_object_chain_wait(object
, 0);
3497 vm_object_reference_locked(object
);
3498 vm_object_clear_flag(object
,
3500 vm_object_drop(object
);
3505 * Clone the entry. We've already bumped the ref on
3508 new_entry
= vm_map_entry_create(new_map
, countp
);
3509 *new_entry
= *old_entry
;
3510 new_entry
->eflags
&= ~MAP_ENTRY_USER_WIRED
;
3511 new_entry
->wired_count
= 0;
3514 * Insert the entry into the new map -- we know we're
3515 * inserting at the end of the new map.
3518 vm_map_entry_link(new_map
, new_map
->header
.prev
,
3522 * Update the physical map
3524 pmap_copy(new_map
->pmap
, old_map
->pmap
,
3526 (old_entry
->end
- old_entry
->start
),
3529 case VM_INHERIT_COPY
:
3531 * Clone the entry and link into the map.
3533 new_entry
= vm_map_entry_create(new_map
, countp
);
3534 *new_entry
= *old_entry
;
3535 new_entry
->eflags
&= ~MAP_ENTRY_USER_WIRED
;
3536 new_entry
->wired_count
= 0;
3537 new_entry
->object
.vm_object
= NULL
;
3538 vm_map_entry_link(new_map
, new_map
->header
.prev
,
3540 vm_map_copy_entry(old_map
, new_map
, old_entry
,
3547 * When forking user-kernel shared maps, the map might change in the
3548 * child so do not try to copy the underlying pmap entries.
3552 vmspace_fork_uksmap_entry(vm_map_t old_map
, vm_map_t new_map
,
3553 vm_map_entry_t old_entry
, int *countp
)
3555 vm_map_entry_t new_entry
;
3557 new_entry
= vm_map_entry_create(new_map
, countp
);
3558 *new_entry
= *old_entry
;
3559 new_entry
->eflags
&= ~MAP_ENTRY_USER_WIRED
;
3560 new_entry
->wired_count
= 0;
3561 vm_map_entry_link(new_map
, new_map
->header
.prev
,
3566 * Create an auto-grow stack entry
3571 vm_map_stack (vm_map_t map
, vm_offset_t addrbos
, vm_size_t max_ssize
,
3572 int flags
, vm_prot_t prot
, vm_prot_t max
, int cow
)
3574 vm_map_entry_t prev_entry
;
3575 vm_map_entry_t new_stack_entry
;
3576 vm_size_t init_ssize
;
3579 vm_offset_t tmpaddr
;
3581 cow
|= MAP_IS_STACK
;
3583 if (max_ssize
< sgrowsiz
)
3584 init_ssize
= max_ssize
;
3586 init_ssize
= sgrowsiz
;
3588 count
= vm_map_entry_reserve(MAP_RESERVE_COUNT
);
3592 * Find space for the mapping
3594 if ((flags
& (MAP_FIXED
| MAP_TRYFIXED
)) == 0) {
3595 if (vm_map_findspace(map
, addrbos
, max_ssize
, 1,
3598 vm_map_entry_release(count
);
3599 return (KERN_NO_SPACE
);
3604 /* If addr is already mapped, no go */
3605 if (vm_map_lookup_entry(map
, addrbos
, &prev_entry
)) {
3607 vm_map_entry_release(count
);
3608 return (KERN_NO_SPACE
);
3612 /* XXX already handled by kern_mmap() */
3613 /* If we would blow our VMEM resource limit, no go */
3614 if (map
->size
+ init_ssize
>
3615 curproc
->p_rlimit
[RLIMIT_VMEM
].rlim_cur
) {
3617 vm_map_entry_release(count
);
3618 return (KERN_NO_SPACE
);
3623 * If we can't accomodate max_ssize in the current mapping,
3624 * no go. However, we need to be aware that subsequent user
3625 * mappings might map into the space we have reserved for
3626 * stack, and currently this space is not protected.
3628 * Hopefully we will at least detect this condition
3629 * when we try to grow the stack.
3631 if ((prev_entry
->next
!= &map
->header
) &&
3632 (prev_entry
->next
->start
< addrbos
+ max_ssize
)) {
3634 vm_map_entry_release(count
);
3635 return (KERN_NO_SPACE
);
3639 * We initially map a stack of only init_ssize. We will
3640 * grow as needed later. Since this is to be a grow
3641 * down stack, we map at the top of the range.
3643 * Note: we would normally expect prot and max to be
3644 * VM_PROT_ALL, and cow to be 0. Possibly we should
3645 * eliminate these as input parameters, and just
3646 * pass these values here in the insert call.
3648 rv
= vm_map_insert(map
, &count
, NULL
, NULL
,
3649 0, addrbos
+ max_ssize
- init_ssize
,
3650 addrbos
+ max_ssize
,
3654 /* Now set the avail_ssize amount */
3655 if (rv
== KERN_SUCCESS
) {
3656 if (prev_entry
!= &map
->header
)
3657 vm_map_clip_end(map
, prev_entry
, addrbos
+ max_ssize
- init_ssize
, &count
);
3658 new_stack_entry
= prev_entry
->next
;
3659 if (new_stack_entry
->end
!= addrbos
+ max_ssize
||
3660 new_stack_entry
->start
!= addrbos
+ max_ssize
- init_ssize
)
3661 panic ("Bad entry start/end for new stack entry");
3663 new_stack_entry
->aux
.avail_ssize
= max_ssize
- init_ssize
;
3667 vm_map_entry_release(count
);
3672 * Attempts to grow a vm stack entry. Returns KERN_SUCCESS if the
3673 * desired address is already mapped, or if we successfully grow
3674 * the stack. Also returns KERN_SUCCESS if addr is outside the
3675 * stack range (this is strange, but preserves compatibility with
3676 * the grow function in vm_machdep.c).
3681 vm_map_growstack (struct proc
*p
, vm_offset_t addr
)
3683 vm_map_entry_t prev_entry
;
3684 vm_map_entry_t stack_entry
;
3685 vm_map_entry_t new_stack_entry
;
3686 struct vmspace
*vm
= p
->p_vmspace
;
3687 vm_map_t map
= &vm
->vm_map
;
3690 int rv
= KERN_SUCCESS
;
3692 int use_read_lock
= 1;
3695 count
= vm_map_entry_reserve(MAP_RESERVE_COUNT
);
3698 vm_map_lock_read(map
);
3702 /* If addr is already in the entry range, no need to grow.*/
3703 if (vm_map_lookup_entry(map
, addr
, &prev_entry
))
3706 if ((stack_entry
= prev_entry
->next
) == &map
->header
)
3708 if (prev_entry
== &map
->header
)
3709 end
= stack_entry
->start
- stack_entry
->aux
.avail_ssize
;
3711 end
= prev_entry
->end
;
3714 * This next test mimics the old grow function in vm_machdep.c.
3715 * It really doesn't quite make sense, but we do it anyway
3716 * for compatibility.
3718 * If not growable stack, return success. This signals the
3719 * caller to proceed as he would normally with normal vm.
3721 if (stack_entry
->aux
.avail_ssize
< 1 ||
3722 addr
>= stack_entry
->start
||
3723 addr
< stack_entry
->start
- stack_entry
->aux
.avail_ssize
) {
3727 /* Find the minimum grow amount */
3728 grow_amount
= roundup (stack_entry
->start
- addr
, PAGE_SIZE
);
3729 if (grow_amount
> stack_entry
->aux
.avail_ssize
) {
3735 * If there is no longer enough space between the entries
3736 * nogo, and adjust the available space. Note: this
3737 * should only happen if the user has mapped into the
3738 * stack area after the stack was created, and is
3739 * probably an error.
3741 * This also effectively destroys any guard page the user
3742 * might have intended by limiting the stack size.
3744 if (grow_amount
> stack_entry
->start
- end
) {
3745 if (use_read_lock
&& vm_map_lock_upgrade(map
)) {
3751 stack_entry
->aux
.avail_ssize
= stack_entry
->start
- end
;
3756 is_procstack
= addr
>= (vm_offset_t
)vm
->vm_maxsaddr
;
3758 /* If this is the main process stack, see if we're over the
3761 if (is_procstack
&& (ctob(vm
->vm_ssize
) + grow_amount
>
3762 p
->p_rlimit
[RLIMIT_STACK
].rlim_cur
)) {
3767 /* Round up the grow amount modulo SGROWSIZ */
3768 grow_amount
= roundup (grow_amount
, sgrowsiz
);
3769 if (grow_amount
> stack_entry
->aux
.avail_ssize
) {
3770 grow_amount
= stack_entry
->aux
.avail_ssize
;
3772 if (is_procstack
&& (ctob(vm
->vm_ssize
) + grow_amount
>
3773 p
->p_rlimit
[RLIMIT_STACK
].rlim_cur
)) {
3774 grow_amount
= p
->p_rlimit
[RLIMIT_STACK
].rlim_cur
-
3778 /* If we would blow our VMEM resource limit, no go */
3779 if (map
->size
+ grow_amount
> p
->p_rlimit
[RLIMIT_VMEM
].rlim_cur
) {
3784 if (use_read_lock
&& vm_map_lock_upgrade(map
)) {
3791 /* Get the preliminary new entry start value */
3792 addr
= stack_entry
->start
- grow_amount
;
3794 /* If this puts us into the previous entry, cut back our growth
3795 * to the available space. Also, see the note above.
3798 stack_entry
->aux
.avail_ssize
= stack_entry
->start
- end
;
3802 rv
= vm_map_insert(map
, &count
, NULL
, NULL
,
3803 0, addr
, stack_entry
->start
,
3805 VM_PROT_ALL
, VM_PROT_ALL
, 0);
3807 /* Adjust the available stack space by the amount we grew. */
3808 if (rv
== KERN_SUCCESS
) {
3809 if (prev_entry
!= &map
->header
)
3810 vm_map_clip_end(map
, prev_entry
, addr
, &count
);
3811 new_stack_entry
= prev_entry
->next
;
3812 if (new_stack_entry
->end
!= stack_entry
->start
||
3813 new_stack_entry
->start
!= addr
)
3814 panic ("Bad stack grow start/end in new stack entry");
3816 new_stack_entry
->aux
.avail_ssize
=
3817 stack_entry
->aux
.avail_ssize
-
3818 (new_stack_entry
->end
- new_stack_entry
->start
);
3820 vm
->vm_ssize
+= btoc(new_stack_entry
->end
-
3821 new_stack_entry
->start
);
3824 if (map
->flags
& MAP_WIREFUTURE
)
3825 vm_map_unwire(map
, new_stack_entry
->start
,
3826 new_stack_entry
->end
, FALSE
);
3831 vm_map_unlock_read(map
);
3834 vm_map_entry_release(count
);
3839 * Unshare the specified VM space for exec. If other processes are
3840 * mapped to it, then create a new one. The new vmspace is null.
3845 vmspace_exec(struct proc
*p
, struct vmspace
*vmcopy
)
3847 struct vmspace
*oldvmspace
= p
->p_vmspace
;
3848 struct vmspace
*newvmspace
;
3849 vm_map_t map
= &p
->p_vmspace
->vm_map
;
3852 * If we are execing a resident vmspace we fork it, otherwise
3853 * we create a new vmspace. Note that exitingcnt is not
3854 * copied to the new vmspace.
3856 lwkt_gettoken(&oldvmspace
->vm_map
.token
);
3858 newvmspace
= vmspace_fork(vmcopy
);
3859 lwkt_gettoken(&newvmspace
->vm_map
.token
);
3861 newvmspace
= vmspace_alloc(map
->min_offset
, map
->max_offset
);
3862 lwkt_gettoken(&newvmspace
->vm_map
.token
);
3863 bcopy(&oldvmspace
->vm_startcopy
, &newvmspace
->vm_startcopy
,
3864 (caddr_t
)&oldvmspace
->vm_endcopy
-
3865 (caddr_t
)&oldvmspace
->vm_startcopy
);
3869 * Finish initializing the vmspace before assigning it
3870 * to the process. The vmspace will become the current vmspace
3873 pmap_pinit2(vmspace_pmap(newvmspace
));
3874 pmap_replacevm(p
, newvmspace
, 0);
3875 lwkt_reltoken(&newvmspace
->vm_map
.token
);
3876 lwkt_reltoken(&oldvmspace
->vm_map
.token
);
3877 vmspace_rel(oldvmspace
);
3881 * Unshare the specified VM space for forcing COW. This
3882 * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
3885 vmspace_unshare(struct proc
*p
)
3887 struct vmspace
*oldvmspace
= p
->p_vmspace
;
3888 struct vmspace
*newvmspace
;
3890 lwkt_gettoken(&oldvmspace
->vm_map
.token
);
3891 if (vmspace_getrefs(oldvmspace
) == 1) {
3892 lwkt_reltoken(&oldvmspace
->vm_map
.token
);
3895 newvmspace
= vmspace_fork(oldvmspace
);
3896 lwkt_gettoken(&newvmspace
->vm_map
.token
);
3897 pmap_pinit2(vmspace_pmap(newvmspace
));
3898 pmap_replacevm(p
, newvmspace
, 0);
3899 lwkt_reltoken(&newvmspace
->vm_map
.token
);
3900 lwkt_reltoken(&oldvmspace
->vm_map
.token
);
3901 vmspace_rel(oldvmspace
);
3905 * vm_map_hint: return the beginning of the best area suitable for
3906 * creating a new mapping with "prot" protection.
3911 vm_map_hint(struct proc
*p
, vm_offset_t addr
, vm_prot_t prot
)
3913 struct vmspace
*vms
= p
->p_vmspace
;
3915 if (!randomize_mmap
|| addr
!= 0) {
3917 * Set a reasonable start point for the hint if it was
3918 * not specified or if it falls within the heap space.
3919 * Hinted mmap()s do not allocate out of the heap space.
3922 (addr
>= round_page((vm_offset_t
)vms
->vm_taddr
) &&
3923 addr
< round_page((vm_offset_t
)vms
->vm_daddr
+ maxdsiz
))) {
3924 addr
= round_page((vm_offset_t
)vms
->vm_daddr
+ maxdsiz
);
3933 * If executable skip first two pages, otherwise start
3934 * after data + heap region.
3936 if ((prot
& VM_PROT_EXECUTE
) &&
3937 ((vm_offset_t
)vms
->vm_daddr
>= I386_MAX_EXE_ADDR
)) {
3938 addr
= (PAGE_SIZE
* 2) +
3939 (karc4random() & (I386_MAX_EXE_ADDR
/ 2 - 1));
3940 return (round_page(addr
));
3942 #endif /* __i386__ */
3945 addr
= (vm_offset_t
)vms
->vm_daddr
+ MAXDSIZ
;
3946 addr
+= karc4random() & (MIN((256 * 1024 * 1024), MAXDSIZ
) - 1);
3948 return (round_page(addr
));
3952 * Finds the VM object, offset, and protection for a given virtual address
3953 * in the specified map, assuming a page fault of the type specified.
3955 * Leaves the map in question locked for read; return values are guaranteed
3956 * until a vm_map_lookup_done call is performed. Note that the map argument
3957 * is in/out; the returned map must be used in the call to vm_map_lookup_done.
3959 * A handle (out_entry) is returned for use in vm_map_lookup_done, to make
3962 * If a lookup is requested with "write protection" specified, the map may
3963 * be changed to perform virtual copying operations, although the data
3964 * referenced will remain the same.
3969 vm_map_lookup(vm_map_t
*var_map
, /* IN/OUT */
3971 vm_prot_t fault_typea
,
3972 vm_map_entry_t
*out_entry
, /* OUT */
3973 vm_object_t
*object
, /* OUT */
3974 vm_pindex_t
*pindex
, /* OUT */
3975 vm_prot_t
*out_prot
, /* OUT */
3976 boolean_t
*wired
) /* OUT */
3978 vm_map_entry_t entry
;
3979 vm_map_t map
= *var_map
;
3981 vm_prot_t fault_type
= fault_typea
;
3982 int use_read_lock
= 1;
3983 int rv
= KERN_SUCCESS
;
3987 vm_map_lock_read(map
);
3992 * If the map has an interesting hint, try it before calling full
3993 * blown lookup routine.
4000 if ((entry
== &map
->header
) ||
4001 (vaddr
< entry
->start
) || (vaddr
>= entry
->end
)) {
4002 vm_map_entry_t tmp_entry
;
4005 * Entry was either not a valid hint, or the vaddr was not
4006 * contained in the entry, so do a full lookup.
4008 if (!vm_map_lookup_entry(map
, vaddr
, &tmp_entry
)) {
4009 rv
= KERN_INVALID_ADDRESS
;
4020 if (entry
->maptype
== VM_MAPTYPE_SUBMAP
) {
4021 vm_map_t old_map
= map
;
4023 *var_map
= map
= entry
->object
.sub_map
;
4025 vm_map_unlock_read(old_map
);
4027 vm_map_unlock(old_map
);
4033 * Check whether this task is allowed to have this page.
4034 * Note the special case for MAP_ENTRY_COW
4035 * pages with an override. This is to implement a forced
4036 * COW for debuggers.
4039 if (fault_type
& VM_PROT_OVERRIDE_WRITE
)
4040 prot
= entry
->max_protection
;
4042 prot
= entry
->protection
;
4044 fault_type
&= (VM_PROT_READ
|VM_PROT_WRITE
|VM_PROT_EXECUTE
);
4045 if ((fault_type
& prot
) != fault_type
) {
4046 rv
= KERN_PROTECTION_FAILURE
;
4050 if ((entry
->eflags
& MAP_ENTRY_USER_WIRED
) &&
4051 (entry
->eflags
& MAP_ENTRY_COW
) &&
4052 (fault_type
& VM_PROT_WRITE
) &&
4053 (fault_typea
& VM_PROT_OVERRIDE_WRITE
) == 0) {
4054 rv
= KERN_PROTECTION_FAILURE
;
4059 * If this page is not pageable, we have to get it for all possible
4062 *wired
= (entry
->wired_count
!= 0);
4064 prot
= fault_type
= entry
->protection
;
4067 * Virtual page tables may need to update the accessed (A) bit
4068 * in a page table entry. Upgrade the fault to a write fault for
4069 * that case if the map will support it. If the map does not support
4070 * it the page table entry simply will not be updated.
4072 if (entry
->maptype
== VM_MAPTYPE_VPAGETABLE
) {
4073 if (prot
& VM_PROT_WRITE
)
4074 fault_type
|= VM_PROT_WRITE
;
4077 if (curthread
->td_lwp
&& curthread
->td_lwp
->lwp_vmspace
&&
4078 pmap_emulate_ad_bits(&curthread
->td_lwp
->lwp_vmspace
->vm_pmap
)) {
4079 if ((prot
& VM_PROT_WRITE
) == 0)
4080 fault_type
|= VM_PROT_WRITE
;
4084 * Only NORMAL and VPAGETABLE maps are object-based. UKSMAPs are not.
4086 if (entry
->maptype
!= VM_MAPTYPE_NORMAL
&&
4087 entry
->maptype
!= VM_MAPTYPE_VPAGETABLE
) {
4093 * If the entry was copy-on-write, we either ...
4095 if (entry
->eflags
& MAP_ENTRY_NEEDS_COPY
) {
4097 * If we want to write the page, we may as well handle that
4098 * now since we've got the map locked.
4100 * If we don't need to write the page, we just demote the
4101 * permissions allowed.
4104 if (fault_type
& VM_PROT_WRITE
) {
4106 * Make a new object, and place it in the object
4107 * chain. Note that no new references have appeared
4108 * -- one just moved from the map to the new
4112 if (use_read_lock
&& vm_map_lock_upgrade(map
)) {
4119 vm_map_entry_shadow(entry
, 0);
4122 * We're attempting to read a copy-on-write page --
4123 * don't allow writes.
4126 prot
&= ~VM_PROT_WRITE
;
4131 * Create an object if necessary.
4133 if (entry
->object
.vm_object
== NULL
&& !map
->system_map
) {
4134 if (use_read_lock
&& vm_map_lock_upgrade(map
)) {
4140 vm_map_entry_allocate_object(entry
);
4144 * Return the object/offset from this entry. If the entry was
4145 * copy-on-write or empty, it has been fixed up.
4147 *object
= entry
->object
.vm_object
;
4150 *pindex
= OFF_TO_IDX((vaddr
- entry
->start
) + entry
->offset
);
4153 * Return whether this is the only map sharing this data. On
4154 * success we return with a read lock held on the map. On failure
4155 * we return with the map unlocked.
4159 if (rv
== KERN_SUCCESS
) {
4160 if (use_read_lock
== 0)
4161 vm_map_lock_downgrade(map
);
4162 } else if (use_read_lock
) {
4163 vm_map_unlock_read(map
);
4171 * Releases locks acquired by a vm_map_lookup()
4172 * (according to the handle returned by that lookup).
4174 * No other requirements.
4177 vm_map_lookup_done(vm_map_t map
, vm_map_entry_t entry
, int count
)
4180 * Unlock the main-level map
4182 vm_map_unlock_read(map
);
4184 vm_map_entry_release(count
);
4187 #include "opt_ddb.h"
4189 #include <sys/kernel.h>
4191 #include <ddb/ddb.h>
4196 DB_SHOW_COMMAND(map
, vm_map_print
)
4199 /* XXX convert args. */
4200 vm_map_t map
= (vm_map_t
)addr
;
4201 boolean_t full
= have_addr
;
4203 vm_map_entry_t entry
;
4205 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
4207 (void *)map
->pmap
, map
->nentries
, map
->timestamp
);
4210 if (!full
&& db_indent
)
4214 for (entry
= map
->header
.next
; entry
!= &map
->header
;
4215 entry
= entry
->next
) {
4216 db_iprintf("map entry %p: start=%p, end=%p\n",
4217 (void *)entry
, (void *)entry
->start
, (void *)entry
->end
);
4220 static char *inheritance_name
[4] =
4221 {"share", "copy", "none", "donate_copy"};
4223 db_iprintf(" prot=%x/%x/%s",
4225 entry
->max_protection
,
4226 inheritance_name
[(int)(unsigned char)entry
->inheritance
]);
4227 if (entry
->wired_count
!= 0)
4228 db_printf(", wired");
4230 switch(entry
->maptype
) {
4231 case VM_MAPTYPE_SUBMAP
:
4232 /* XXX no %qd in kernel. Truncate entry->offset. */
4233 db_printf(", share=%p, offset=0x%lx\n",
4234 (void *)entry
->object
.sub_map
,
4235 (long)entry
->offset
);
4237 if ((entry
->prev
== &map
->header
) ||
4238 (entry
->prev
->object
.sub_map
!=
4239 entry
->object
.sub_map
)) {
4241 vm_map_print((db_expr_t
)(intptr_t)
4242 entry
->object
.sub_map
,
4247 case VM_MAPTYPE_NORMAL
:
4248 case VM_MAPTYPE_VPAGETABLE
:
4249 /* XXX no %qd in kernel. Truncate entry->offset. */
4250 db_printf(", object=%p, offset=0x%lx",
4251 (void *)entry
->object
.vm_object
,
4252 (long)entry
->offset
);
4253 if (entry
->eflags
& MAP_ENTRY_COW
)
4254 db_printf(", copy (%s)",
4255 (entry
->eflags
& MAP_ENTRY_NEEDS_COPY
) ? "needed" : "done");
4259 if ((entry
->prev
== &map
->header
) ||
4260 (entry
->prev
->object
.vm_object
!=
4261 entry
->object
.vm_object
)) {
4263 vm_object_print((db_expr_t
)(intptr_t)
4264 entry
->object
.vm_object
,
4270 case VM_MAPTYPE_UKSMAP
:
4271 db_printf(", uksmap=%p, offset=0x%lx",
4272 (void *)entry
->object
.uksmap
,
4273 (long)entry
->offset
);
4274 if (entry
->eflags
& MAP_ENTRY_COW
)
4275 db_printf(", copy (%s)",
4276 (entry
->eflags
& MAP_ENTRY_NEEDS_COPY
) ? "needed" : "done");
4292 DB_SHOW_COMMAND(procvm
, procvm
)
4297 p
= (struct proc
*) addr
;
4302 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
4303 (void *)p
, (void *)p
->p_vmspace
, (void *)&p
->p_vmspace
->vm_map
,
4304 (void *)vmspace_pmap(p
->p_vmspace
));
4306 vm_map_print((db_expr_t
)(intptr_t)&p
->p_vmspace
->vm_map
, 1, 0, NULL
);