2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * The Mach Operating System project at Carnegie-Mellon University.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * from: @(#)vm_map.c 8.3 (Berkeley) 1/12/94
39 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
40 * All rights reserved.
42 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
44 * Permission to use, copy, modify and distribute this software and
45 * its documentation is hereby granted, provided that both the copyright
46 * notice and this permission notice appear in all copies of the
47 * software, derivative works or modified versions, and any portions
48 * thereof, and that both notices appear in supporting documentation.
50 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
51 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
52 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
54 * Carnegie Mellon requests users of this software to return to
56 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
57 * School of Computer Science
58 * Carnegie Mellon University
59 * Pittsburgh PA 15213-3890
61 * any improvements or extensions that they make and grant Carnegie the
62 * rights to redistribute these changes.
64 * $FreeBSD: src/sys/vm/vm_map.c,v 1.187.2.19 2003/05/27 00:47:02 alc Exp $
65 * $DragonFly: src/sys/vm/vm_map.c,v 1.56 2007/04/29 18:25:41 dillon Exp $
69 * Virtual memory mapping module.
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/kernel.h>
77 #include <sys/vmmeter.h>
79 #include <sys/vnode.h>
80 #include <sys/resourcevar.h>
83 #include <sys/malloc.h>
86 #include <vm/vm_param.h>
88 #include <vm/vm_map.h>
89 #include <vm/vm_page.h>
90 #include <vm/vm_object.h>
91 #include <vm/vm_pager.h>
92 #include <vm/vm_kern.h>
93 #include <vm/vm_extern.h>
94 #include <vm/swap_pager.h>
95 #include <vm/vm_zone.h>
97 #include <sys/thread2.h>
98 #include <sys/sysref2.h>
101 * Virtual memory maps provide for the mapping, protection,
102 * and sharing of virtual memory objects. In addition,
103 * this module provides for an efficient virtual copy of
104 * memory from one map to another.
106 * Synchronization is required prior to most operations.
108 * Maps consist of an ordered doubly-linked list of simple
109 * entries; a single hint is used to speed up lookups.
111 * Since portions of maps are specified by start/end addresses,
112 * which may not align with existing map entries, all
113 * routines merely "clip" entries to these start/end values.
114 * [That is, an entry is split into two, bordering at a
115 * start or end value.] Note that these clippings may not
116 * always be necessary (as the two resulting entries are then
117 * not changed); however, the clipping is done for convenience.
119 * As mentioned above, virtual copy operations are performed
120 * by copying VM object references from one map to
121 * another, and then marking both regions as copy-on-write.
124 static void vmspace_terminate(struct vmspace
*vm
);
125 static void vmspace_dtor(void *obj
, void *private);
127 MALLOC_DEFINE(M_VMSPACE
, "vmspace", "vmspace objcache backingstore");
129 struct sysref_class vmspace_sysref_class
= {
132 .proto
= SYSREF_PROTO_VMSPACE
,
133 .offset
= offsetof(struct vmspace
, vm_sysref
),
134 .objsize
= sizeof(struct vmspace
),
136 .flags
= SRC_MANAGEDINIT
,
137 .dtor
= vmspace_dtor
,
139 .terminate
= (sysref_terminate_func_t
)vmspace_terminate
145 static struct vm_zone mapentzone_store
, mapzone_store
;
146 static vm_zone_t mapentzone
, mapzone
;
147 static struct vm_object mapentobj
, mapobj
;
149 static struct vm_map_entry map_entry_init
[MAX_MAPENT
];
150 static struct vm_map_entry cpu_map_entry_init
[MAXCPU
][VMEPERCPU
];
151 static struct vm_map map_init
[MAX_KMAP
];
153 static void vm_map_entry_shadow(vm_map_entry_t entry
);
154 static vm_map_entry_t
vm_map_entry_create(vm_map_t map
, int *);
155 static void vm_map_entry_dispose (vm_map_t map
, vm_map_entry_t entry
, int *);
156 static void _vm_map_clip_end (vm_map_t
, vm_map_entry_t
, vm_offset_t
, int *);
157 static void _vm_map_clip_start (vm_map_t
, vm_map_entry_t
, vm_offset_t
, int *);
158 static void vm_map_entry_delete (vm_map_t
, vm_map_entry_t
, int *);
159 static void vm_map_entry_unwire (vm_map_t
, vm_map_entry_t
);
160 static void vm_map_copy_entry (vm_map_t
, vm_map_t
, vm_map_entry_t
,
162 static void vm_map_split (vm_map_entry_t
);
163 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
);
168 * Initialize the vm_map module. Must be called before
169 * any other vm_map routines.
171 * Map and entry structures are allocated from the general
172 * purpose memory pool with some exceptions:
174 * - The kernel map and kmem submap are allocated statically.
175 * - Kernel map entries are allocated out of a static pool.
177 * These restrictions are necessary since malloc() uses the
178 * maps and requires map entries.
183 mapzone
= &mapzone_store
;
184 zbootinit(mapzone
, "MAP", sizeof (struct vm_map
),
186 mapentzone
= &mapentzone_store
;
187 zbootinit(mapentzone
, "MAP ENTRY", sizeof (struct vm_map_entry
),
188 map_entry_init
, MAX_MAPENT
);
192 * vm_init2 - called prior to any vmspace allocations
197 zinitna(mapentzone
, &mapentobj
, NULL
, 0, 0,
198 ZONE_USE_RESERVE
| ZONE_SPECIAL
, 1);
199 zinitna(mapzone
, &mapobj
, NULL
, 0, 0, 0, 1);
206 * Red black tree functions
208 static int rb_vm_map_compare(vm_map_entry_t a
, vm_map_entry_t b
);
209 RB_GENERATE(vm_map_rb_tree
, vm_map_entry
, rb_entry
, rb_vm_map_compare
);
211 /* a->start is address, and the only field has to be initialized */
213 rb_vm_map_compare(vm_map_entry_t a
, vm_map_entry_t b
)
215 if (a
->start
< b
->start
)
217 else if (a
->start
> b
->start
)
223 * Allocate a vmspace structure, including a vm_map and pmap.
224 * Initialize numerous fields. While the initial allocation is zerod,
225 * subsequence reuse from the objcache leaves elements of the structure
226 * intact (particularly the pmap), so portions must be zerod.
228 * The structure is not considered activated until we call sysref_activate().
231 vmspace_alloc(vm_offset_t min
, vm_offset_t max
)
235 vm
= sysref_alloc(&vmspace_sysref_class
);
236 bzero(&vm
->vm_startcopy
,
237 (char *)&vm
->vm_endcopy
- (char *)&vm
->vm_startcopy
);
238 vm_map_init(&vm
->vm_map
, min
, max
, NULL
);
239 pmap_pinit(vmspace_pmap(vm
)); /* (some fields reused) */
240 vm
->vm_map
.pmap
= vmspace_pmap(vm
); /* XXX */
242 vm
->vm_exitingcnt
= 0;
243 cpu_vmspace_alloc(vm
);
244 sysref_activate(&vm
->vm_sysref
);
249 * dtor function - Some elements of the pmap are retained in the
250 * free-cached vmspaces to improve performance. We have to clean them up
251 * here before returning the vmspace to the memory pool.
254 vmspace_dtor(void *obj
, void *private)
256 struct vmspace
*vm
= obj
;
258 pmap_puninit(vmspace_pmap(vm
));
262 * Called in two cases:
264 * (1) When the last sysref is dropped, but exitingcnt might still be
267 * (2) When there are no sysrefs (i.e. refcnt is negative) left and the
268 * exitingcnt becomes zero
270 * sysref will not scrap the object until we call sysref_put() once more
271 * after the last ref has been dropped.
274 vmspace_terminate(struct vmspace
*vm
)
279 * If exitingcnt is non-zero we can't get rid of the entire vmspace
280 * yet, but we can scrap user memory.
282 if (vm
->vm_exitingcnt
) {
284 pmap_remove_pages(vmspace_pmap(vm
), VM_MIN_USER_ADDRESS
,
285 VM_MAX_USER_ADDRESS
);
286 vm_map_remove(&vm
->vm_map
, VM_MIN_USER_ADDRESS
,
287 VM_MAX_USER_ADDRESS
);
291 cpu_vmspace_free(vm
);
294 * Make sure any SysV shm is freed, it might not have in
299 KKASSERT(vm
->vm_upcalls
== NULL
);
302 * Lock the map, to wait out all other references to it.
303 * Delete all of the mappings and pages they hold, then call
304 * the pmap module to reclaim anything left.
306 count
= vm_map_entry_reserve(MAP_RESERVE_COUNT
);
307 vm_map_lock(&vm
->vm_map
);
308 vm_map_delete(&vm
->vm_map
, vm
->vm_map
.min_offset
,
309 vm
->vm_map
.max_offset
, &count
);
310 vm_map_unlock(&vm
->vm_map
);
311 vm_map_entry_release(count
);
313 pmap_release(vmspace_pmap(vm
));
314 sysref_put(&vm
->vm_sysref
);
318 * This is called in the wait*() handling code. The vmspace can be terminated
319 * after the last wait is finished using it.
322 vmspace_exitfree(struct proc
*p
)
329 if (--vm
->vm_exitingcnt
== 0 && sysref_isinactive(&vm
->vm_sysref
))
330 vmspace_terminate(vm
);
334 * vmspace_swap_count() - count the approximate swap useage in pages for a
337 * Swap useage is determined by taking the proportional swap used by
338 * VM objects backing the VM map. To make up for fractional losses,
339 * if the VM object has any swap use at all the associated map entries
340 * count for at least 1 swap page.
343 vmspace_swap_count(struct vmspace
*vmspace
)
345 vm_map_t map
= &vmspace
->vm_map
;
351 for (cur
= map
->header
.next
; cur
!= &map
->header
; cur
= cur
->next
) {
352 switch(cur
->maptype
) {
353 case VM_MAPTYPE_NORMAL
:
354 case VM_MAPTYPE_VPAGETABLE
:
355 if ((object
= cur
->object
.vm_object
) == NULL
)
357 if (object
->type
!= OBJT_SWAP
)
359 n
= (cur
->end
- cur
->start
) / PAGE_SIZE
;
360 if (object
->un_pager
.swp
.swp_bcount
) {
361 count
+= object
->un_pager
.swp
.swp_bcount
*
362 SWAP_META_PAGES
* n
/ object
->size
+ 1;
376 * Creates and returns a new empty VM map with
377 * the given physical map structure, and having
378 * the given lower and upper address bounds.
381 vm_map_create(vm_map_t result
, pmap_t pmap
, vm_offset_t min
, vm_offset_t max
)
384 result
= zalloc(mapzone
);
385 vm_map_init(result
, min
, max
, pmap
);
390 * Initialize an existing vm_map structure
391 * such as that in the vmspace structure.
392 * The pmap is set elsewhere.
395 vm_map_init(struct vm_map
*map
, vm_offset_t min
, vm_offset_t max
, pmap_t pmap
)
397 map
->header
.next
= map
->header
.prev
= &map
->header
;
398 RB_INIT(&map
->rb_root
);
403 map
->min_offset
= min
;
404 map
->max_offset
= max
;
406 map
->first_free
= &map
->header
;
407 map
->hint
= &map
->header
;
409 lockinit(&map
->lock
, "thrd_sleep", 0, 0);
413 * Shadow the vm_map_entry's object. This typically needs to be done when
414 * a write fault is taken on an entry which had previously been cloned by
415 * fork(). The shared object (which might be NULL) must become private so
416 * we add a shadow layer above it.
418 * Object allocation for anonymous mappings is defered as long as possible.
419 * When creating a shadow, however, the underlying object must be instantiated
420 * so it can be shared.
422 * If the map segment is governed by a virtual page table then it is
423 * possible to address offsets beyond the mapped area. Just allocate
424 * a maximally sized object for this case.
428 vm_map_entry_shadow(vm_map_entry_t entry
)
430 if (entry
->maptype
== VM_MAPTYPE_VPAGETABLE
) {
431 vm_object_shadow(&entry
->object
.vm_object
, &entry
->offset
,
432 0x7FFFFFFF); /* XXX */
434 vm_object_shadow(&entry
->object
.vm_object
, &entry
->offset
,
435 atop(entry
->end
- entry
->start
));
437 entry
->eflags
&= ~MAP_ENTRY_NEEDS_COPY
;
441 * Allocate an object for a vm_map_entry.
443 * Object allocation for anonymous mappings is defered as long as possible.
444 * This function is called when we can defer no longer, generally when a map
445 * entry might be split or forked or takes a page fault.
447 * If the map segment is governed by a virtual page table then it is
448 * possible to address offsets beyond the mapped area. Just allocate
449 * a maximally sized object for this case.
452 vm_map_entry_allocate_object(vm_map_entry_t entry
)
456 if (entry
->maptype
== VM_MAPTYPE_VPAGETABLE
) {
457 obj
= vm_object_allocate(OBJT_DEFAULT
, 0x7FFFFFFF); /* XXX */
459 obj
= vm_object_allocate(OBJT_DEFAULT
,
460 atop(entry
->end
- entry
->start
));
462 entry
->object
.vm_object
= obj
;
467 * vm_map_entry_reserve_cpu_init:
469 * Set an initial negative count so the first attempt to reserve
470 * space preloads a bunch of vm_map_entry's for this cpu. Also
471 * pre-allocate 2 vm_map_entries which will be needed by zalloc() to
472 * map a new page for vm_map_entry structures. SMP systems are
473 * particularly sensitive.
475 * This routine is called in early boot so we cannot just call
476 * vm_map_entry_reserve().
478 * May be called for a gd other then mycpu, but may only be called
482 vm_map_entry_reserve_cpu_init(globaldata_t gd
)
484 vm_map_entry_t entry
;
487 gd
->gd_vme_avail
-= MAP_RESERVE_COUNT
* 2;
488 entry
= &cpu_map_entry_init
[gd
->gd_cpuid
][0];
489 for (i
= 0; i
< VMEPERCPU
; ++i
, ++entry
) {
490 entry
->next
= gd
->gd_vme_base
;
491 gd
->gd_vme_base
= entry
;
496 * vm_map_entry_reserve:
498 * Reserves vm_map_entry structures so code later on can manipulate
499 * map_entry structures within a locked map without blocking trying
500 * to allocate a new vm_map_entry.
503 vm_map_entry_reserve(int count
)
505 struct globaldata
*gd
= mycpu
;
506 vm_map_entry_t entry
;
511 * Make sure we have enough structures in gd_vme_base to handle
512 * the reservation request.
514 while (gd
->gd_vme_avail
< count
) {
515 entry
= zalloc(mapentzone
);
516 entry
->next
= gd
->gd_vme_base
;
517 gd
->gd_vme_base
= entry
;
520 gd
->gd_vme_avail
-= count
;
526 * vm_map_entry_release:
528 * Releases previously reserved vm_map_entry structures that were not
529 * used. If we have too much junk in our per-cpu cache clean some of
533 vm_map_entry_release(int count
)
535 struct globaldata
*gd
= mycpu
;
536 vm_map_entry_t entry
;
539 gd
->gd_vme_avail
+= count
;
540 while (gd
->gd_vme_avail
> MAP_RESERVE_SLOP
) {
541 entry
= gd
->gd_vme_base
;
542 KKASSERT(entry
!= NULL
);
543 gd
->gd_vme_base
= entry
->next
;
546 zfree(mapentzone
, entry
);
553 * vm_map_entry_kreserve:
555 * Reserve map entry structures for use in kernel_map itself. These
556 * entries have *ALREADY* been reserved on a per-cpu basis when the map
557 * was inited. This function is used by zalloc() to avoid a recursion
558 * when zalloc() itself needs to allocate additional kernel memory.
560 * This function works like the normal reserve but does not load the
561 * vm_map_entry cache (because that would result in an infinite
562 * recursion). Note that gd_vme_avail may go negative. This is expected.
564 * Any caller of this function must be sure to renormalize after
565 * potentially eating entries to ensure that the reserve supply
569 vm_map_entry_kreserve(int count
)
571 struct globaldata
*gd
= mycpu
;
574 gd
->gd_vme_avail
-= count
;
576 KASSERT(gd
->gd_vme_base
!= NULL
, ("no reserved entries left, gd_vme_avail = %d\n", gd
->gd_vme_avail
));
581 * vm_map_entry_krelease:
583 * Release previously reserved map entries for kernel_map. We do not
584 * attempt to clean up like the normal release function as this would
585 * cause an unnecessary (but probably not fatal) deep procedure call.
588 vm_map_entry_krelease(int count
)
590 struct globaldata
*gd
= mycpu
;
593 gd
->gd_vme_avail
+= count
;
598 * vm_map_entry_create: [ internal use only ]
600 * Allocates a VM map entry for insertion. No entry fields are filled
603 * This routine may be called from an interrupt thread but not a FAST
604 * interrupt. This routine may recurse the map lock.
606 static vm_map_entry_t
607 vm_map_entry_create(vm_map_t map
, int *countp
)
609 struct globaldata
*gd
= mycpu
;
610 vm_map_entry_t entry
;
612 KKASSERT(*countp
> 0);
615 entry
= gd
->gd_vme_base
;
616 KASSERT(entry
!= NULL
, ("gd_vme_base NULL! count %d", *countp
));
617 gd
->gd_vme_base
= entry
->next
;
623 * vm_map_entry_dispose: [ internal use only ]
625 * Dispose of a vm_map_entry that is no longer being referenced. This
626 * function may be called from an interrupt.
629 vm_map_entry_dispose(vm_map_t map
, vm_map_entry_t entry
, int *countp
)
631 struct globaldata
*gd
= mycpu
;
633 KKASSERT(map
->hint
!= entry
);
634 KKASSERT(map
->first_free
!= entry
);
638 entry
->next
= gd
->gd_vme_base
;
639 gd
->gd_vme_base
= entry
;
645 * vm_map_entry_{un,}link:
647 * Insert/remove entries from maps.
650 vm_map_entry_link(vm_map_t map
,
651 vm_map_entry_t after_where
,
652 vm_map_entry_t entry
)
655 entry
->prev
= after_where
;
656 entry
->next
= after_where
->next
;
657 entry
->next
->prev
= entry
;
658 after_where
->next
= entry
;
659 if (vm_map_rb_tree_RB_INSERT(&map
->rb_root
, entry
))
660 panic("vm_map_entry_link: dup addr map %p ent %p", map
, entry
);
664 vm_map_entry_unlink(vm_map_t map
,
665 vm_map_entry_t entry
)
670 if (entry
->eflags
& MAP_ENTRY_IN_TRANSITION
)
671 panic("vm_map_entry_unlink: attempt to mess with locked entry! %p", entry
);
676 vm_map_rb_tree_RB_REMOVE(&map
->rb_root
, entry
);
681 * vm_map_lookup_entry: [ internal use only ]
683 * Finds the map entry containing (or
684 * immediately preceding) the specified address
685 * in the given map; the entry is returned
686 * in the "entry" parameter. The boolean
687 * result indicates whether the address is
688 * actually contained in the map.
691 vm_map_lookup_entry(vm_map_t map
, vm_offset_t address
,
692 vm_map_entry_t
*entry
/* OUT */)
699 * XXX TEMPORARILY DISABLED. For some reason our attempt to revive
700 * the hint code with the red-black lookup meets with system crashes
701 * and lockups. We do not yet know why.
703 * It is possible that the problem is related to the setting
704 * of the hint during map_entry deletion, in the code specified
705 * at the GGG comment later on in this file.
708 * Quickly check the cached hint, there's a good chance of a match.
710 if (map
->hint
!= &map
->header
) {
712 if (address
>= tmp
->start
&& address
< tmp
->end
) {
720 * Locate the record from the top of the tree. 'last' tracks the
721 * closest prior record and is returned if no match is found, which
722 * in binary tree terms means tracking the most recent right-branch
723 * taken. If there is no prior record, &map->header is returned.
726 tmp
= RB_ROOT(&map
->rb_root
);
729 if (address
>= tmp
->start
) {
730 if (address
< tmp
->end
) {
736 tmp
= RB_RIGHT(tmp
, rb_entry
);
738 tmp
= RB_LEFT(tmp
, rb_entry
);
748 * Inserts the given whole VM object into the target
749 * map at the specified address range. The object's
750 * size should match that of the address range.
752 * Requires that the map be locked, and leaves it so. Requires that
753 * sufficient vm_map_entry structures have been reserved and tracks
754 * the use via countp.
756 * If object is non-NULL, ref count must be bumped by caller
757 * prior to making call to account for the new entry.
760 vm_map_insert(vm_map_t map
, int *countp
,
761 vm_object_t object
, vm_ooffset_t offset
,
762 vm_offset_t start
, vm_offset_t end
,
763 vm_maptype_t maptype
,
764 vm_prot_t prot
, vm_prot_t max
,
767 vm_map_entry_t new_entry
;
768 vm_map_entry_t prev_entry
;
769 vm_map_entry_t temp_entry
;
770 vm_eflags_t protoeflags
;
773 * Check that the start and end points are not bogus.
776 if ((start
< map
->min_offset
) || (end
> map
->max_offset
) ||
778 return (KERN_INVALID_ADDRESS
);
781 * Find the entry prior to the proposed starting address; if it's part
782 * of an existing entry, this range is bogus.
785 if (vm_map_lookup_entry(map
, start
, &temp_entry
))
786 return (KERN_NO_SPACE
);
788 prev_entry
= temp_entry
;
791 * Assert that the next entry doesn't overlap the end point.
794 if ((prev_entry
->next
!= &map
->header
) &&
795 (prev_entry
->next
->start
< end
))
796 return (KERN_NO_SPACE
);
800 if (cow
& MAP_COPY_ON_WRITE
)
801 protoeflags
|= MAP_ENTRY_COW
|MAP_ENTRY_NEEDS_COPY
;
803 if (cow
& MAP_NOFAULT
) {
804 protoeflags
|= MAP_ENTRY_NOFAULT
;
806 KASSERT(object
== NULL
,
807 ("vm_map_insert: paradoxical MAP_NOFAULT request"));
809 if (cow
& MAP_DISABLE_SYNCER
)
810 protoeflags
|= MAP_ENTRY_NOSYNC
;
811 if (cow
& MAP_DISABLE_COREDUMP
)
812 protoeflags
|= MAP_ENTRY_NOCOREDUMP
;
816 * When object is non-NULL, it could be shared with another
817 * process. We have to set or clear OBJ_ONEMAPPING
820 if ((object
->ref_count
> 1) || (object
->shadow_count
!= 0)) {
821 vm_object_clear_flag(object
, OBJ_ONEMAPPING
);
824 else if ((prev_entry
!= &map
->header
) &&
825 (prev_entry
->eflags
== protoeflags
) &&
826 (prev_entry
->end
== start
) &&
827 (prev_entry
->wired_count
== 0) &&
828 prev_entry
->maptype
== maptype
&&
829 ((prev_entry
->object
.vm_object
== NULL
) ||
830 vm_object_coalesce(prev_entry
->object
.vm_object
,
831 OFF_TO_IDX(prev_entry
->offset
),
832 (vm_size_t
)(prev_entry
->end
- prev_entry
->start
),
833 (vm_size_t
)(end
- prev_entry
->end
)))) {
835 * We were able to extend the object. Determine if we
836 * can extend the previous map entry to include the
839 if ((prev_entry
->inheritance
== VM_INHERIT_DEFAULT
) &&
840 (prev_entry
->protection
== prot
) &&
841 (prev_entry
->max_protection
== max
)) {
842 map
->size
+= (end
- prev_entry
->end
);
843 prev_entry
->end
= end
;
844 vm_map_simplify_entry(map
, prev_entry
, countp
);
845 return (KERN_SUCCESS
);
849 * If we can extend the object but cannot extend the
850 * map entry, we have to create a new map entry. We
851 * must bump the ref count on the extended object to
852 * account for it. object may be NULL.
854 object
= prev_entry
->object
.vm_object
;
855 offset
= prev_entry
->offset
+
856 (prev_entry
->end
- prev_entry
->start
);
857 vm_object_reference(object
);
861 * NOTE: if conditionals fail, object can be NULL here. This occurs
862 * in things like the buffer map where we manage kva but do not manage
870 new_entry
= vm_map_entry_create(map
, countp
);
871 new_entry
->start
= start
;
872 new_entry
->end
= end
;
874 new_entry
->maptype
= maptype
;
875 new_entry
->eflags
= protoeflags
;
876 new_entry
->object
.vm_object
= object
;
877 new_entry
->offset
= offset
;
878 new_entry
->aux
.master_pde
= 0;
880 new_entry
->inheritance
= VM_INHERIT_DEFAULT
;
881 new_entry
->protection
= prot
;
882 new_entry
->max_protection
= max
;
883 new_entry
->wired_count
= 0;
886 * Insert the new entry into the list
889 vm_map_entry_link(map
, prev_entry
, new_entry
);
890 map
->size
+= new_entry
->end
- new_entry
->start
;
893 * Update the free space hint
895 if ((map
->first_free
== prev_entry
) &&
896 (prev_entry
->end
>= new_entry
->start
)) {
897 map
->first_free
= new_entry
;
902 * Temporarily removed to avoid MAP_STACK panic, due to
903 * MAP_STACK being a huge hack. Will be added back in
904 * when MAP_STACK (and the user stack mapping) is fixed.
907 * It may be possible to simplify the entry
909 vm_map_simplify_entry(map
, new_entry
, countp
);
913 * Try to pre-populate the page table. Mappings governed by virtual
914 * page tables cannot be prepopulated without a lot of work, so
917 if ((cow
& (MAP_PREFAULT
|MAP_PREFAULT_PARTIAL
)) &&
918 maptype
!= VM_MAPTYPE_VPAGETABLE
) {
919 pmap_object_init_pt(map
->pmap
, start
, prot
,
920 object
, OFF_TO_IDX(offset
), end
- start
,
921 cow
& MAP_PREFAULT_PARTIAL
);
924 return (KERN_SUCCESS
);
928 * Find sufficient space for `length' bytes in the given map, starting at
929 * `start'. The map must be locked. Returns 0 on success, 1 on no space.
931 * This function will returned an arbitrarily aligned pointer. If no
932 * particular alignment is required you should pass align as 1. Note that
933 * the map may return PAGE_SIZE aligned pointers if all the lengths used in
934 * the map are a multiple of PAGE_SIZE, even if you pass a smaller align
937 * 'align' should be a power of 2 but is not required to be.
947 vm_map_entry_t entry
, next
;
949 vm_offset_t align_mask
;
951 if (start
< map
->min_offset
)
952 start
= map
->min_offset
;
953 if (start
> map
->max_offset
)
957 * If the alignment is not a power of 2 we will have to use
958 * a mod/division, set align_mask to a special value.
960 if ((align
| (align
- 1)) + 1 != (align
<< 1))
961 align_mask
= (vm_offset_t
)-1;
963 align_mask
= align
- 1;
967 * Look for the first possible address; if there's already something
968 * at this address, we have to start after it.
970 if (start
== map
->min_offset
) {
971 if ((entry
= map
->first_free
) != &map
->header
)
976 if (vm_map_lookup_entry(map
, start
, &tmp
))
982 * Look through the rest of the map, trying to fit a new region in the
983 * gap between existing regions, or after the very last region.
985 for (;; start
= (entry
= next
)->end
) {
987 * Adjust the proposed start by the requested alignment,
988 * be sure that we didn't wrap the address.
990 if (align_mask
== (vm_offset_t
)-1)
991 end
= ((start
+ align
- 1) / align
) * align
;
993 end
= (start
+ align_mask
) & ~align_mask
;
998 * Find the end of the proposed new region. Be sure we didn't
999 * go beyond the end of the map, or wrap around the address.
1000 * Then check to see if this is the last entry or if the
1001 * proposed end fits in the gap between this and the next
1004 end
= start
+ length
;
1005 if (end
> map
->max_offset
|| end
< start
)
1008 if (next
== &map
->header
|| next
->start
>= end
)
1012 if (map
== &kernel_map
) {
1014 if ((ksize
= round_page(start
+ length
)) > kernel_vm_end
) {
1015 pmap_growkernel(ksize
);
1024 * vm_map_find finds an unallocated region in the target address
1025 * map with the given length. The search is defined to be
1026 * first-fit from the specified address; the region found is
1027 * returned in the same parameter.
1029 * If object is non-NULL, ref count must be bumped by caller
1030 * prior to making call to account for the new entry.
1033 vm_map_find(vm_map_t map
, vm_object_t object
, vm_ooffset_t offset
,
1034 vm_offset_t
*addr
, vm_size_t length
,
1035 boolean_t find_space
,
1036 vm_maptype_t maptype
,
1037 vm_prot_t prot
, vm_prot_t max
,
1046 count
= vm_map_entry_reserve(MAP_RESERVE_COUNT
);
1049 if (vm_map_findspace(map
, start
, length
, 1, addr
)) {
1051 vm_map_entry_release(count
);
1052 return (KERN_NO_SPACE
);
1056 result
= vm_map_insert(map
, &count
, object
, offset
,
1057 start
, start
+ length
,
1062 vm_map_entry_release(count
);
1068 * vm_map_simplify_entry:
1070 * Simplify the given map entry by merging with either neighbor. This
1071 * routine also has the ability to merge with both neighbors.
1073 * The map must be locked.
1075 * This routine guarentees that the passed entry remains valid (though
1076 * possibly extended). When merging, this routine may delete one or
1077 * both neighbors. No action is taken on entries which have their
1078 * in-transition flag set.
1081 vm_map_simplify_entry(vm_map_t map
, vm_map_entry_t entry
, int *countp
)
1083 vm_map_entry_t next
, prev
;
1084 vm_size_t prevsize
, esize
;
1086 if (entry
->eflags
& MAP_ENTRY_IN_TRANSITION
) {
1087 ++mycpu
->gd_cnt
.v_intrans_coll
;
1091 if (entry
->maptype
== VM_MAPTYPE_SUBMAP
)
1095 if (prev
!= &map
->header
) {
1096 prevsize
= prev
->end
- prev
->start
;
1097 if ( (prev
->end
== entry
->start
) &&
1098 (prev
->maptype
== entry
->maptype
) &&
1099 (prev
->object
.vm_object
== entry
->object
.vm_object
) &&
1100 (!prev
->object
.vm_object
||
1101 (prev
->offset
+ prevsize
== entry
->offset
)) &&
1102 (prev
->eflags
== entry
->eflags
) &&
1103 (prev
->protection
== entry
->protection
) &&
1104 (prev
->max_protection
== entry
->max_protection
) &&
1105 (prev
->inheritance
== entry
->inheritance
) &&
1106 (prev
->wired_count
== entry
->wired_count
)) {
1107 if (map
->first_free
== prev
)
1108 map
->first_free
= entry
;
1109 if (map
->hint
== prev
)
1111 vm_map_entry_unlink(map
, prev
);
1112 entry
->start
= prev
->start
;
1113 entry
->offset
= prev
->offset
;
1114 if (prev
->object
.vm_object
)
1115 vm_object_deallocate(prev
->object
.vm_object
);
1116 vm_map_entry_dispose(map
, prev
, countp
);
1121 if (next
!= &map
->header
) {
1122 esize
= entry
->end
- entry
->start
;
1123 if ((entry
->end
== next
->start
) &&
1124 (next
->maptype
== entry
->maptype
) &&
1125 (next
->object
.vm_object
== entry
->object
.vm_object
) &&
1126 (!entry
->object
.vm_object
||
1127 (entry
->offset
+ esize
== next
->offset
)) &&
1128 (next
->eflags
== entry
->eflags
) &&
1129 (next
->protection
== entry
->protection
) &&
1130 (next
->max_protection
== entry
->max_protection
) &&
1131 (next
->inheritance
== entry
->inheritance
) &&
1132 (next
->wired_count
== entry
->wired_count
)) {
1133 if (map
->first_free
== next
)
1134 map
->first_free
= entry
;
1135 if (map
->hint
== next
)
1137 vm_map_entry_unlink(map
, next
);
1138 entry
->end
= next
->end
;
1139 if (next
->object
.vm_object
)
1140 vm_object_deallocate(next
->object
.vm_object
);
1141 vm_map_entry_dispose(map
, next
, countp
);
1146 * vm_map_clip_start: [ internal use only ]
1148 * Asserts that the given entry begins at or after
1149 * the specified address; if necessary,
1150 * it splits the entry into two.
1152 #define vm_map_clip_start(map, entry, startaddr, countp) \
1154 if (startaddr > entry->start) \
1155 _vm_map_clip_start(map, entry, startaddr, countp); \
1159 * This routine is called only when it is known that
1160 * the entry must be split.
1163 _vm_map_clip_start(vm_map_t map
, vm_map_entry_t entry
, vm_offset_t start
, int *countp
)
1165 vm_map_entry_t new_entry
;
1168 * Split off the front portion -- note that we must insert the new
1169 * entry BEFORE this one, so that this entry has the specified
1173 vm_map_simplify_entry(map
, entry
, countp
);
1176 * If there is no object backing this entry, we might as well create
1177 * one now. If we defer it, an object can get created after the map
1178 * is clipped, and individual objects will be created for the split-up
1179 * map. This is a bit of a hack, but is also about the best place to
1180 * put this improvement.
1182 if (entry
->object
.vm_object
== NULL
&& !map
->system_map
) {
1183 vm_map_entry_allocate_object(entry
);
1186 new_entry
= vm_map_entry_create(map
, countp
);
1187 *new_entry
= *entry
;
1189 new_entry
->end
= start
;
1190 entry
->offset
+= (start
- entry
->start
);
1191 entry
->start
= start
;
1193 vm_map_entry_link(map
, entry
->prev
, new_entry
);
1195 switch(entry
->maptype
) {
1196 case VM_MAPTYPE_NORMAL
:
1197 case VM_MAPTYPE_VPAGETABLE
:
1198 vm_object_reference(new_entry
->object
.vm_object
);
1206 * vm_map_clip_end: [ internal use only ]
1208 * Asserts that the given entry ends at or before
1209 * the specified address; if necessary,
1210 * it splits the entry into two.
1213 #define vm_map_clip_end(map, entry, endaddr, countp) \
1215 if (endaddr < entry->end) \
1216 _vm_map_clip_end(map, entry, endaddr, countp); \
1220 * This routine is called only when it is known that
1221 * the entry must be split.
1224 _vm_map_clip_end(vm_map_t map
, vm_map_entry_t entry
, vm_offset_t end
, int *countp
)
1226 vm_map_entry_t new_entry
;
1229 * If there is no object backing this entry, we might as well create
1230 * one now. If we defer it, an object can get created after the map
1231 * is clipped, and individual objects will be created for the split-up
1232 * map. This is a bit of a hack, but is also about the best place to
1233 * put this improvement.
1236 if (entry
->object
.vm_object
== NULL
&& !map
->system_map
) {
1237 vm_map_entry_allocate_object(entry
);
1241 * Create a new entry and insert it AFTER the specified entry
1244 new_entry
= vm_map_entry_create(map
, countp
);
1245 *new_entry
= *entry
;
1247 new_entry
->start
= entry
->end
= end
;
1248 new_entry
->offset
+= (end
- entry
->start
);
1250 vm_map_entry_link(map
, entry
, new_entry
);
1252 switch(entry
->maptype
) {
1253 case VM_MAPTYPE_NORMAL
:
1254 case VM_MAPTYPE_VPAGETABLE
:
1255 vm_object_reference(new_entry
->object
.vm_object
);
1263 * VM_MAP_RANGE_CHECK: [ internal use only ]
1265 * Asserts that the starting and ending region
1266 * addresses fall within the valid range of the map.
1268 #define VM_MAP_RANGE_CHECK(map, start, end) \
1270 if (start < vm_map_min(map)) \
1271 start = vm_map_min(map); \
1272 if (end > vm_map_max(map)) \
1273 end = vm_map_max(map); \
1279 * vm_map_transition_wait: [ kernel use only ]
1281 * Used to block when an in-transition collison occurs. The map
1282 * is unlocked for the sleep and relocked before the return.
1286 vm_map_transition_wait(vm_map_t map
)
1289 tsleep(map
, 0, "vment", 0);
1297 * When we do blocking operations with the map lock held it is
1298 * possible that a clip might have occured on our in-transit entry,
1299 * requiring an adjustment to the entry in our loop. These macros
1300 * help the pageable and clip_range code deal with the case. The
1301 * conditional costs virtually nothing if no clipping has occured.
1304 #define CLIP_CHECK_BACK(entry, save_start) \
1306 while (entry->start != save_start) { \
1307 entry = entry->prev; \
1308 KASSERT(entry != &map->header, ("bad entry clip")); \
1312 #define CLIP_CHECK_FWD(entry, save_end) \
1314 while (entry->end != save_end) { \
1315 entry = entry->next; \
1316 KASSERT(entry != &map->header, ("bad entry clip")); \
1322 * vm_map_clip_range: [ kernel use only ]
1324 * Clip the specified range and return the base entry. The
1325 * range may cover several entries starting at the returned base
1326 * and the first and last entry in the covering sequence will be
1327 * properly clipped to the requested start and end address.
1329 * If no holes are allowed you should pass the MAP_CLIP_NO_HOLES
1332 * The MAP_ENTRY_IN_TRANSITION flag will be set for the entries
1333 * covered by the requested range.
1335 * The map must be exclusively locked on entry and will remain locked
1336 * on return. If no range exists or the range contains holes and you
1337 * specified that no holes were allowed, NULL will be returned. This
1338 * routine may temporarily unlock the map in order avoid a deadlock when
1343 vm_map_clip_range(vm_map_t map
, vm_offset_t start
, vm_offset_t end
,
1344 int *countp
, int flags
)
1346 vm_map_entry_t start_entry
;
1347 vm_map_entry_t entry
;
1350 * Locate the entry and effect initial clipping. The in-transition
1351 * case does not occur very often so do not try to optimize it.
1354 if (vm_map_lookup_entry(map
, start
, &start_entry
) == FALSE
)
1356 entry
= start_entry
;
1357 if (entry
->eflags
& MAP_ENTRY_IN_TRANSITION
) {
1358 entry
->eflags
|= MAP_ENTRY_NEEDS_WAKEUP
;
1359 ++mycpu
->gd_cnt
.v_intrans_coll
;
1360 ++mycpu
->gd_cnt
.v_intrans_wait
;
1361 vm_map_transition_wait(map
);
1363 * entry and/or start_entry may have been clipped while
1364 * we slept, or may have gone away entirely. We have
1365 * to restart from the lookup.
1370 * Since we hold an exclusive map lock we do not have to restart
1371 * after clipping, even though clipping may block in zalloc.
1373 vm_map_clip_start(map
, entry
, start
, countp
);
1374 vm_map_clip_end(map
, entry
, end
, countp
);
1375 entry
->eflags
|= MAP_ENTRY_IN_TRANSITION
;
1378 * Scan entries covered by the range. When working on the next
1379 * entry a restart need only re-loop on the current entry which
1380 * we have already locked, since 'next' may have changed. Also,
1381 * even though entry is safe, it may have been clipped so we
1382 * have to iterate forwards through the clip after sleeping.
1384 while (entry
->next
!= &map
->header
&& entry
->next
->start
< end
) {
1385 vm_map_entry_t next
= entry
->next
;
1387 if (flags
& MAP_CLIP_NO_HOLES
) {
1388 if (next
->start
> entry
->end
) {
1389 vm_map_unclip_range(map
, start_entry
,
1390 start
, entry
->end
, countp
, flags
);
1395 if (next
->eflags
& MAP_ENTRY_IN_TRANSITION
) {
1396 vm_offset_t save_end
= entry
->end
;
1397 next
->eflags
|= MAP_ENTRY_NEEDS_WAKEUP
;
1398 ++mycpu
->gd_cnt
.v_intrans_coll
;
1399 ++mycpu
->gd_cnt
.v_intrans_wait
;
1400 vm_map_transition_wait(map
);
1403 * clips might have occured while we blocked.
1405 CLIP_CHECK_FWD(entry
, save_end
);
1406 CLIP_CHECK_BACK(start_entry
, start
);
1410 * No restart necessary even though clip_end may block, we
1411 * are holding the map lock.
1413 vm_map_clip_end(map
, next
, end
, countp
);
1414 next
->eflags
|= MAP_ENTRY_IN_TRANSITION
;
1417 if (flags
& MAP_CLIP_NO_HOLES
) {
1418 if (entry
->end
!= end
) {
1419 vm_map_unclip_range(map
, start_entry
,
1420 start
, entry
->end
, countp
, flags
);
1424 return(start_entry
);
1428 * vm_map_unclip_range: [ kernel use only ]
1430 * Undo the effect of vm_map_clip_range(). You should pass the same
1431 * flags and the same range that you passed to vm_map_clip_range().
1432 * This code will clear the in-transition flag on the entries and
1433 * wake up anyone waiting. This code will also simplify the sequence
1434 * and attempt to merge it with entries before and after the sequence.
1436 * The map must be locked on entry and will remain locked on return.
1438 * Note that you should also pass the start_entry returned by
1439 * vm_map_clip_range(). However, if you block between the two calls
1440 * with the map unlocked please be aware that the start_entry may
1441 * have been clipped and you may need to scan it backwards to find
1442 * the entry corresponding with the original start address. You are
1443 * responsible for this, vm_map_unclip_range() expects the correct
1444 * start_entry to be passed to it and will KASSERT otherwise.
1448 vm_map_unclip_range(
1450 vm_map_entry_t start_entry
,
1456 vm_map_entry_t entry
;
1458 entry
= start_entry
;
1460 KASSERT(entry
->start
== start
, ("unclip_range: illegal base entry"));
1461 while (entry
!= &map
->header
&& entry
->start
< end
) {
1462 KASSERT(entry
->eflags
& MAP_ENTRY_IN_TRANSITION
, ("in-transition flag not set during unclip on: %p", entry
));
1463 KASSERT(entry
->end
<= end
, ("unclip_range: tail wasn't clipped"));
1464 entry
->eflags
&= ~MAP_ENTRY_IN_TRANSITION
;
1465 if (entry
->eflags
& MAP_ENTRY_NEEDS_WAKEUP
) {
1466 entry
->eflags
&= ~MAP_ENTRY_NEEDS_WAKEUP
;
1469 entry
= entry
->next
;
1473 * Simplification does not block so there is no restart case.
1475 entry
= start_entry
;
1476 while (entry
!= &map
->header
&& entry
->start
< end
) {
1477 vm_map_simplify_entry(map
, entry
, countp
);
1478 entry
= entry
->next
;
1483 * vm_map_submap: [ kernel use only ]
1485 * Mark the given range as handled by a subordinate map.
1487 * This range must have been created with vm_map_find,
1488 * and no other operations may have been performed on this
1489 * range prior to calling vm_map_submap.
1491 * Only a limited number of operations can be performed
1492 * within this rage after calling vm_map_submap:
1494 * [Don't try vm_map_copy!]
1496 * To remove a submapping, one must first remove the
1497 * range from the superior map, and then destroy the
1498 * submap (if desired). [Better yet, don't try it.]
1501 vm_map_submap(vm_map_t map
, vm_offset_t start
, vm_offset_t end
, vm_map_t submap
)
1503 vm_map_entry_t entry
;
1504 int result
= KERN_INVALID_ARGUMENT
;
1507 count
= vm_map_entry_reserve(MAP_RESERVE_COUNT
);
1510 VM_MAP_RANGE_CHECK(map
, start
, end
);
1512 if (vm_map_lookup_entry(map
, start
, &entry
)) {
1513 vm_map_clip_start(map
, entry
, start
, &count
);
1515 entry
= entry
->next
;
1518 vm_map_clip_end(map
, entry
, end
, &count
);
1520 if ((entry
->start
== start
) && (entry
->end
== end
) &&
1521 ((entry
->eflags
& MAP_ENTRY_COW
) == 0) &&
1522 (entry
->object
.vm_object
== NULL
)) {
1523 entry
->object
.sub_map
= submap
;
1524 entry
->maptype
= VM_MAPTYPE_SUBMAP
;
1525 result
= KERN_SUCCESS
;
1528 vm_map_entry_release(count
);
1536 * Sets the protection of the specified address region in the target map.
1537 * If "set_max" is specified, the maximum protection is to be set;
1538 * otherwise, only the current protection is affected.
1540 * The protection is not applicable to submaps, but is applicable to normal
1541 * maps and maps governed by virtual page tables. For example, when operating
1542 * on a virtual page table our protection basically controls how COW occurs
1543 * on the backing object, whereas the virtual page table abstraction itself
1544 * is an abstraction for userland.
1547 vm_map_protect(vm_map_t map
, vm_offset_t start
, vm_offset_t end
,
1548 vm_prot_t new_prot
, boolean_t set_max
)
1550 vm_map_entry_t current
;
1551 vm_map_entry_t entry
;
1554 count
= vm_map_entry_reserve(MAP_RESERVE_COUNT
);
1557 VM_MAP_RANGE_CHECK(map
, start
, end
);
1559 if (vm_map_lookup_entry(map
, start
, &entry
)) {
1560 vm_map_clip_start(map
, entry
, start
, &count
);
1562 entry
= entry
->next
;
1566 * Make a first pass to check for protection violations.
1569 while ((current
!= &map
->header
) && (current
->start
< end
)) {
1570 if (current
->maptype
== VM_MAPTYPE_SUBMAP
) {
1572 vm_map_entry_release(count
);
1573 return (KERN_INVALID_ARGUMENT
);
1575 if ((new_prot
& current
->max_protection
) != new_prot
) {
1577 vm_map_entry_release(count
);
1578 return (KERN_PROTECTION_FAILURE
);
1580 current
= current
->next
;
1584 * Go back and fix up protections. [Note that clipping is not
1585 * necessary the second time.]
1589 while ((current
!= &map
->header
) && (current
->start
< end
)) {
1592 vm_map_clip_end(map
, current
, end
, &count
);
1594 old_prot
= current
->protection
;
1596 current
->protection
=
1597 (current
->max_protection
= new_prot
) &
1600 current
->protection
= new_prot
;
1604 * Update physical map if necessary. Worry about copy-on-write
1605 * here -- CHECK THIS XXX
1608 if (current
->protection
!= old_prot
) {
1609 #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1612 pmap_protect(map
->pmap
, current
->start
,
1614 current
->protection
& MASK(current
));
1618 vm_map_simplify_entry(map
, current
, &count
);
1620 current
= current
->next
;
1624 vm_map_entry_release(count
);
1625 return (KERN_SUCCESS
);
1631 * This routine traverses a processes map handling the madvise
1632 * system call. Advisories are classified as either those effecting
1633 * the vm_map_entry structure, or those effecting the underlying
1636 * The <value> argument is used for extended madvise calls.
1639 vm_map_madvise(vm_map_t map
, vm_offset_t start
, vm_offset_t end
,
1640 int behav
, off_t value
)
1642 vm_map_entry_t current
, entry
;
1648 * Some madvise calls directly modify the vm_map_entry, in which case
1649 * we need to use an exclusive lock on the map and we need to perform
1650 * various clipping operations. Otherwise we only need a read-lock
1654 count
= vm_map_entry_reserve(MAP_RESERVE_COUNT
);
1658 case MADV_SEQUENTIAL
:
1672 vm_map_lock_read(map
);
1675 vm_map_entry_release(count
);
1680 * Locate starting entry and clip if necessary.
1683 VM_MAP_RANGE_CHECK(map
, start
, end
);
1685 if (vm_map_lookup_entry(map
, start
, &entry
)) {
1687 vm_map_clip_start(map
, entry
, start
, &count
);
1689 entry
= entry
->next
;
1694 * madvise behaviors that are implemented in the vm_map_entry.
1696 * We clip the vm_map_entry so that behavioral changes are
1697 * limited to the specified address range.
1699 for (current
= entry
;
1700 (current
!= &map
->header
) && (current
->start
< end
);
1701 current
= current
->next
1703 if (current
->maptype
== VM_MAPTYPE_SUBMAP
)
1706 vm_map_clip_end(map
, current
, end
, &count
);
1710 vm_map_entry_set_behavior(current
, MAP_ENTRY_BEHAV_NORMAL
);
1712 case MADV_SEQUENTIAL
:
1713 vm_map_entry_set_behavior(current
, MAP_ENTRY_BEHAV_SEQUENTIAL
);
1716 vm_map_entry_set_behavior(current
, MAP_ENTRY_BEHAV_RANDOM
);
1719 current
->eflags
|= MAP_ENTRY_NOSYNC
;
1722 current
->eflags
&= ~MAP_ENTRY_NOSYNC
;
1725 current
->eflags
|= MAP_ENTRY_NOCOREDUMP
;
1728 current
->eflags
&= ~MAP_ENTRY_NOCOREDUMP
;
1732 * Invalidate the related pmap entries, used
1733 * to flush portions of the real kernel's
1734 * pmap when the caller has removed or
1735 * modified existing mappings in a virtual
1738 pmap_remove(map
->pmap
,
1739 current
->start
, current
->end
);
1743 * Set the page directory page for a map
1744 * governed by a virtual page table. Mark
1745 * the entry as being governed by a virtual
1746 * page table if it is not.
1748 * XXX the page directory page is stored
1749 * in the avail_ssize field if the map_entry.
1751 * XXX the map simplification code does not
1752 * compare this field so weird things may
1753 * happen if you do not apply this function
1754 * to the entire mapping governed by the
1755 * virtual page table.
1757 if (current
->maptype
!= VM_MAPTYPE_VPAGETABLE
) {
1761 current
->aux
.master_pde
= value
;
1762 pmap_remove(map
->pmap
,
1763 current
->start
, current
->end
);
1769 vm_map_simplify_entry(map
, current
, &count
);
1777 * madvise behaviors that are implemented in the underlying
1780 * Since we don't clip the vm_map_entry, we have to clip
1781 * the vm_object pindex and count.
1783 * NOTE! We currently do not support these functions on
1784 * virtual page tables.
1786 for (current
= entry
;
1787 (current
!= &map
->header
) && (current
->start
< end
);
1788 current
= current
->next
1790 vm_offset_t useStart
;
1792 if (current
->maptype
!= VM_MAPTYPE_NORMAL
)
1795 pindex
= OFF_TO_IDX(current
->offset
);
1796 count
= atop(current
->end
- current
->start
);
1797 useStart
= current
->start
;
1799 if (current
->start
< start
) {
1800 pindex
+= atop(start
- current
->start
);
1801 count
-= atop(start
- current
->start
);
1804 if (current
->end
> end
)
1805 count
-= atop(current
->end
- end
);
1810 vm_object_madvise(current
->object
.vm_object
,
1811 pindex
, count
, behav
);
1814 * Try to populate the page table. Mappings governed
1815 * by virtual page tables cannot be pre-populated
1816 * without a lot of work so don't try.
1818 if (behav
== MADV_WILLNEED
&&
1819 current
->maptype
!= VM_MAPTYPE_VPAGETABLE
) {
1820 pmap_object_init_pt(
1823 current
->protection
,
1824 current
->object
.vm_object
,
1826 (count
<< PAGE_SHIFT
),
1827 MAP_PREFAULT_MADVISE
1831 vm_map_unlock_read(map
);
1833 vm_map_entry_release(count
);
1841 * Sets the inheritance of the specified address
1842 * range in the target map. Inheritance
1843 * affects how the map will be shared with
1844 * child maps at the time of vm_map_fork.
1847 vm_map_inherit(vm_map_t map
, vm_offset_t start
, vm_offset_t end
,
1848 vm_inherit_t new_inheritance
)
1850 vm_map_entry_t entry
;
1851 vm_map_entry_t temp_entry
;
1854 switch (new_inheritance
) {
1855 case VM_INHERIT_NONE
:
1856 case VM_INHERIT_COPY
:
1857 case VM_INHERIT_SHARE
:
1860 return (KERN_INVALID_ARGUMENT
);
1863 count
= vm_map_entry_reserve(MAP_RESERVE_COUNT
);
1866 VM_MAP_RANGE_CHECK(map
, start
, end
);
1868 if (vm_map_lookup_entry(map
, start
, &temp_entry
)) {
1870 vm_map_clip_start(map
, entry
, start
, &count
);
1872 entry
= temp_entry
->next
;
1874 while ((entry
!= &map
->header
) && (entry
->start
< end
)) {
1875 vm_map_clip_end(map
, entry
, end
, &count
);
1877 entry
->inheritance
= new_inheritance
;
1879 vm_map_simplify_entry(map
, entry
, &count
);
1881 entry
= entry
->next
;
1884 vm_map_entry_release(count
);
1885 return (KERN_SUCCESS
);
1889 * Implement the semantics of mlock
1892 vm_map_unwire(vm_map_t map
, vm_offset_t start
, vm_offset_t real_end
,
1893 boolean_t new_pageable
)
1895 vm_map_entry_t entry
;
1896 vm_map_entry_t start_entry
;
1898 int rv
= KERN_SUCCESS
;
1901 count
= vm_map_entry_reserve(MAP_RESERVE_COUNT
);
1903 VM_MAP_RANGE_CHECK(map
, start
, real_end
);
1906 start_entry
= vm_map_clip_range(map
, start
, end
, &count
, MAP_CLIP_NO_HOLES
);
1907 if (start_entry
== NULL
) {
1909 vm_map_entry_release(count
);
1910 return (KERN_INVALID_ADDRESS
);
1913 if (new_pageable
== 0) {
1914 entry
= start_entry
;
1915 while ((entry
!= &map
->header
) && (entry
->start
< end
)) {
1916 vm_offset_t save_start
;
1917 vm_offset_t save_end
;
1920 * Already user wired or hard wired (trivial cases)
1922 if (entry
->eflags
& MAP_ENTRY_USER_WIRED
) {
1923 entry
= entry
->next
;
1926 if (entry
->wired_count
!= 0) {
1927 entry
->wired_count
++;
1928 entry
->eflags
|= MAP_ENTRY_USER_WIRED
;
1929 entry
= entry
->next
;
1934 * A new wiring requires instantiation of appropriate
1935 * management structures and the faulting in of the
1938 if (entry
->maptype
!= VM_MAPTYPE_SUBMAP
) {
1939 int copyflag
= entry
->eflags
& MAP_ENTRY_NEEDS_COPY
;
1940 if (copyflag
&& ((entry
->protection
& VM_PROT_WRITE
) != 0)) {
1941 vm_map_entry_shadow(entry
);
1942 } else if (entry
->object
.vm_object
== NULL
&&
1944 vm_map_entry_allocate_object(entry
);
1947 entry
->wired_count
++;
1948 entry
->eflags
|= MAP_ENTRY_USER_WIRED
;
1951 * Now fault in the area. Note that vm_fault_wire()
1952 * may release the map lock temporarily, it will be
1953 * relocked on return. The in-transition
1954 * flag protects the entries.
1956 save_start
= entry
->start
;
1957 save_end
= entry
->end
;
1958 rv
= vm_fault_wire(map
, entry
, TRUE
);
1960 CLIP_CHECK_BACK(entry
, save_start
);
1962 KASSERT(entry
->wired_count
== 1, ("bad wired_count on entry"));
1963 entry
->eflags
&= ~MAP_ENTRY_USER_WIRED
;
1964 entry
->wired_count
= 0;
1965 if (entry
->end
== save_end
)
1967 entry
= entry
->next
;
1968 KASSERT(entry
!= &map
->header
, ("bad entry clip during backout"));
1970 end
= save_start
; /* unwire the rest */
1974 * note that even though the entry might have been
1975 * clipped, the USER_WIRED flag we set prevents
1976 * duplication so we do not have to do a
1979 entry
= entry
->next
;
1983 * If we failed fall through to the unwiring section to
1984 * unwire what we had wired so far. 'end' has already
1991 * start_entry might have been clipped if we unlocked the
1992 * map and blocked. No matter how clipped it has gotten
1993 * there should be a fragment that is on our start boundary.
1995 CLIP_CHECK_BACK(start_entry
, start
);
1999 * Deal with the unwiring case.
2003 * This is the unwiring case. We must first ensure that the
2004 * range to be unwired is really wired down. We know there
2007 entry
= start_entry
;
2008 while ((entry
!= &map
->header
) && (entry
->start
< end
)) {
2009 if ((entry
->eflags
& MAP_ENTRY_USER_WIRED
) == 0) {
2010 rv
= KERN_INVALID_ARGUMENT
;
2013 KASSERT(entry
->wired_count
!= 0, ("wired count was 0 with USER_WIRED set! %p", entry
));
2014 entry
= entry
->next
;
2018 * Now decrement the wiring count for each region. If a region
2019 * becomes completely unwired, unwire its physical pages and
2023 * The map entries are processed in a loop, checking to
2024 * make sure the entry is wired and asserting it has a wired
2025 * count. However, another loop was inserted more-or-less in
2026 * the middle of the unwiring path. This loop picks up the
2027 * "entry" loop variable from the first loop without first
2028 * setting it to start_entry. Naturally, the secound loop
2029 * is never entered and the pages backing the entries are
2030 * never unwired. This can lead to a leak of wired pages.
2032 entry
= start_entry
;
2033 while ((entry
!= &map
->header
) && (entry
->start
< end
)) {
2034 KASSERT(entry
->eflags
& MAP_ENTRY_USER_WIRED
,
2035 ("expected USER_WIRED on entry %p", entry
));
2036 entry
->eflags
&= ~MAP_ENTRY_USER_WIRED
;
2037 entry
->wired_count
--;
2038 if (entry
->wired_count
== 0)
2039 vm_fault_unwire(map
, entry
);
2040 entry
= entry
->next
;
2044 vm_map_unclip_range(map
, start_entry
, start
, real_end
, &count
,
2048 vm_map_entry_release(count
);
2055 * Sets the pageability of the specified address
2056 * range in the target map. Regions specified
2057 * as not pageable require locked-down physical
2058 * memory and physical page maps.
2060 * The map must not be locked, but a reference
2061 * must remain to the map throughout the call.
2063 * This function may be called via the zalloc path and must properly
2064 * reserve map entries for kernel_map.
2067 vm_map_wire(vm_map_t map
, vm_offset_t start
, vm_offset_t real_end
, int kmflags
)
2069 vm_map_entry_t entry
;
2070 vm_map_entry_t start_entry
;
2072 int rv
= KERN_SUCCESS
;
2075 if (kmflags
& KM_KRESERVE
)
2076 count
= vm_map_entry_kreserve(MAP_RESERVE_COUNT
);
2078 count
= vm_map_entry_reserve(MAP_RESERVE_COUNT
);
2080 VM_MAP_RANGE_CHECK(map
, start
, real_end
);
2083 start_entry
= vm_map_clip_range(map
, start
, end
, &count
, MAP_CLIP_NO_HOLES
);
2084 if (start_entry
== NULL
) {
2086 rv
= KERN_INVALID_ADDRESS
;
2089 if ((kmflags
& KM_PAGEABLE
) == 0) {
2093 * 1. Holding the write lock, we create any shadow or zero-fill
2094 * objects that need to be created. Then we clip each map
2095 * entry to the region to be wired and increment its wiring
2096 * count. We create objects before clipping the map entries
2097 * to avoid object proliferation.
2099 * 2. We downgrade to a read lock, and call vm_fault_wire to
2100 * fault in the pages for any newly wired area (wired_count is
2103 * Downgrading to a read lock for vm_fault_wire avoids a
2104 * possible deadlock with another process that may have faulted
2105 * on one of the pages to be wired (it would mark the page busy,
2106 * blocking us, then in turn block on the map lock that we
2107 * hold). Because of problems in the recursive lock package,
2108 * we cannot upgrade to a write lock in vm_map_lookup. Thus,
2109 * any actions that require the write lock must be done
2110 * beforehand. Because we keep the read lock on the map, the
2111 * copy-on-write status of the entries we modify here cannot
2115 entry
= start_entry
;
2116 while ((entry
!= &map
->header
) && (entry
->start
< end
)) {
2118 * Trivial case if the entry is already wired
2120 if (entry
->wired_count
) {
2121 entry
->wired_count
++;
2122 entry
= entry
->next
;
2127 * The entry is being newly wired, we have to setup
2128 * appropriate management structures. A shadow
2129 * object is required for a copy-on-write region,
2130 * or a normal object for a zero-fill region. We
2131 * do not have to do this for entries that point to sub
2132 * maps because we won't hold the lock on the sub map.
2134 if (entry
->maptype
!= VM_MAPTYPE_SUBMAP
) {
2135 int copyflag
= entry
->eflags
& MAP_ENTRY_NEEDS_COPY
;
2137 ((entry
->protection
& VM_PROT_WRITE
) != 0)) {
2138 vm_map_entry_shadow(entry
);
2139 } else if (entry
->object
.vm_object
== NULL
&&
2141 vm_map_entry_allocate_object(entry
);
2145 entry
->wired_count
++;
2146 entry
= entry
->next
;
2154 * HACK HACK HACK HACK
2156 * Unlock the map to avoid deadlocks. The in-transit flag
2157 * protects us from most changes but note that
2158 * clipping may still occur. To prevent clipping from
2159 * occuring after the unlock, except for when we are
2160 * blocking in vm_fault_wire, we must run in a critical
2161 * section, otherwise our accesses to entry->start and
2162 * entry->end could be corrupted. We have to enter the
2163 * critical section prior to unlocking so start_entry does
2164 * not change out from under us at the very beginning of the
2167 * HACK HACK HACK HACK
2172 entry
= start_entry
;
2173 while (entry
!= &map
->header
&& entry
->start
< end
) {
2175 * If vm_fault_wire fails for any page we need to undo
2176 * what has been done. We decrement the wiring count
2177 * for those pages which have not yet been wired (now)
2178 * and unwire those that have (later).
2180 vm_offset_t save_start
= entry
->start
;
2181 vm_offset_t save_end
= entry
->end
;
2183 if (entry
->wired_count
== 1)
2184 rv
= vm_fault_wire(map
, entry
, FALSE
);
2186 CLIP_CHECK_BACK(entry
, save_start
);
2188 KASSERT(entry
->wired_count
== 1, ("wired_count changed unexpectedly"));
2189 entry
->wired_count
= 0;
2190 if (entry
->end
== save_end
)
2192 entry
= entry
->next
;
2193 KASSERT(entry
!= &map
->header
, ("bad entry clip during backout"));
2198 CLIP_CHECK_FWD(entry
, save_end
);
2199 entry
= entry
->next
;
2204 * If a failure occured undo everything by falling through
2205 * to the unwiring code. 'end' has already been adjusted
2209 kmflags
|= KM_PAGEABLE
;
2212 * start_entry is still IN_TRANSITION but may have been
2213 * clipped since vm_fault_wire() unlocks and relocks the
2214 * map. No matter how clipped it has gotten there should
2215 * be a fragment that is on our start boundary.
2217 CLIP_CHECK_BACK(start_entry
, start
);
2220 if (kmflags
& KM_PAGEABLE
) {
2222 * This is the unwiring case. We must first ensure that the
2223 * range to be unwired is really wired down. We know there
2226 entry
= start_entry
;
2227 while ((entry
!= &map
->header
) && (entry
->start
< end
)) {
2228 if (entry
->wired_count
== 0) {
2229 rv
= KERN_INVALID_ARGUMENT
;
2232 entry
= entry
->next
;
2236 * Now decrement the wiring count for each region. If a region
2237 * becomes completely unwired, unwire its physical pages and
2240 entry
= start_entry
;
2241 while ((entry
!= &map
->header
) && (entry
->start
< end
)) {
2242 entry
->wired_count
--;
2243 if (entry
->wired_count
== 0)
2244 vm_fault_unwire(map
, entry
);
2245 entry
= entry
->next
;
2249 vm_map_unclip_range(map
, start_entry
, start
, real_end
, &count
,
2254 if (kmflags
& KM_KRESERVE
)
2255 vm_map_entry_krelease(count
);
2257 vm_map_entry_release(count
);
2262 * vm_map_set_wired_quick()
2264 * Mark a newly allocated address range as wired but do not fault in
2265 * the pages. The caller is expected to load the pages into the object.
2267 * The map must be locked on entry and will remain locked on return.
2270 vm_map_set_wired_quick(vm_map_t map
, vm_offset_t addr
, vm_size_t size
, int *countp
)
2272 vm_map_entry_t scan
;
2273 vm_map_entry_t entry
;
2275 entry
= vm_map_clip_range(map
, addr
, addr
+ size
, countp
, MAP_CLIP_NO_HOLES
);
2276 for (scan
= entry
; scan
!= &map
->header
&& scan
->start
< addr
+ size
; scan
= scan
->next
) {
2277 KKASSERT(entry
->wired_count
== 0);
2278 entry
->wired_count
= 1;
2280 vm_map_unclip_range(map
, entry
, addr
, addr
+ size
, countp
, MAP_CLIP_NO_HOLES
);
2286 * Push any dirty cached pages in the address range to their pager.
2287 * If syncio is TRUE, dirty pages are written synchronously.
2288 * If invalidate is TRUE, any cached pages are freed as well.
2290 * Returns an error if any part of the specified range is not mapped.
2293 vm_map_clean(vm_map_t map
, vm_offset_t start
, vm_offset_t end
, boolean_t syncio
,
2294 boolean_t invalidate
)
2296 vm_map_entry_t current
;
2297 vm_map_entry_t entry
;
2300 vm_ooffset_t offset
;
2302 vm_map_lock_read(map
);
2303 VM_MAP_RANGE_CHECK(map
, start
, end
);
2304 if (!vm_map_lookup_entry(map
, start
, &entry
)) {
2305 vm_map_unlock_read(map
);
2306 return (KERN_INVALID_ADDRESS
);
2309 * Make a first pass to check for holes.
2311 for (current
= entry
; current
->start
< end
; current
= current
->next
) {
2312 if (current
->maptype
== VM_MAPTYPE_SUBMAP
) {
2313 vm_map_unlock_read(map
);
2314 return (KERN_INVALID_ARGUMENT
);
2316 if (end
> current
->end
&&
2317 (current
->next
== &map
->header
||
2318 current
->end
!= current
->next
->start
)) {
2319 vm_map_unlock_read(map
);
2320 return (KERN_INVALID_ADDRESS
);
2325 pmap_remove(vm_map_pmap(map
), start
, end
);
2327 * Make a second pass, cleaning/uncaching pages from the indicated
2330 for (current
= entry
; current
->start
< end
; current
= current
->next
) {
2331 offset
= current
->offset
+ (start
- current
->start
);
2332 size
= (end
<= current
->end
? end
: current
->end
) - start
;
2333 if (current
->maptype
== VM_MAPTYPE_SUBMAP
) {
2335 vm_map_entry_t tentry
;
2338 smap
= current
->object
.sub_map
;
2339 vm_map_lock_read(smap
);
2340 vm_map_lookup_entry(smap
, offset
, &tentry
);
2341 tsize
= tentry
->end
- offset
;
2344 object
= tentry
->object
.vm_object
;
2345 offset
= tentry
->offset
+ (offset
- tentry
->start
);
2346 vm_map_unlock_read(smap
);
2348 object
= current
->object
.vm_object
;
2351 * Note that there is absolutely no sense in writing out
2352 * anonymous objects, so we track down the vnode object
2354 * We invalidate (remove) all pages from the address space
2355 * anyway, for semantic correctness.
2357 * note: certain anonymous maps, such as MAP_NOSYNC maps,
2358 * may start out with a NULL object.
2360 while (object
&& object
->backing_object
) {
2361 offset
+= object
->backing_object_offset
;
2362 object
= object
->backing_object
;
2363 if (object
->size
< OFF_TO_IDX( offset
+ size
))
2364 size
= IDX_TO_OFF(object
->size
) - offset
;
2366 if (object
&& (object
->type
== OBJT_VNODE
) &&
2367 (current
->protection
& VM_PROT_WRITE
)) {
2369 * Flush pages if writing is allowed, invalidate them
2370 * if invalidation requested. Pages undergoing I/O
2371 * will be ignored by vm_object_page_remove().
2373 * We cannot lock the vnode and then wait for paging
2374 * to complete without deadlocking against vm_fault.
2375 * Instead we simply call vm_object_page_remove() and
2376 * allow it to block internally on a page-by-page
2377 * basis when it encounters pages undergoing async
2382 vm_object_reference(object
);
2383 vn_lock(object
->handle
, LK_EXCLUSIVE
| LK_RETRY
);
2384 flags
= (syncio
|| invalidate
) ? OBJPC_SYNC
: 0;
2385 flags
|= invalidate
? OBJPC_INVAL
: 0;
2388 * When operating on a virtual page table just
2389 * flush the whole object. XXX we probably ought
2392 switch(current
->maptype
) {
2393 case VM_MAPTYPE_NORMAL
:
2394 vm_object_page_clean(object
,
2396 OFF_TO_IDX(offset
+ size
+ PAGE_MASK
),
2399 case VM_MAPTYPE_VPAGETABLE
:
2400 vm_object_page_clean(object
, 0, 0, flags
);
2403 vn_unlock(((struct vnode
*)object
->handle
));
2404 vm_object_deallocate(object
);
2406 if (object
&& invalidate
&&
2407 ((object
->type
== OBJT_VNODE
) ||
2408 (object
->type
== OBJT_DEVICE
))) {
2410 (object
->type
== OBJT_DEVICE
) ? FALSE
: TRUE
;
2411 vm_object_reference(object
);
2412 switch(current
->maptype
) {
2413 case VM_MAPTYPE_NORMAL
:
2414 vm_object_page_remove(object
,
2416 OFF_TO_IDX(offset
+ size
+ PAGE_MASK
),
2419 case VM_MAPTYPE_VPAGETABLE
:
2420 vm_object_page_remove(object
, 0, 0, clean_only
);
2423 vm_object_deallocate(object
);
2428 vm_map_unlock_read(map
);
2429 return (KERN_SUCCESS
);
2433 * vm_map_entry_unwire: [ internal use only ]
2435 * Make the region specified by this entry pageable.
2437 * The map in question should be locked.
2438 * [This is the reason for this routine's existence.]
2441 vm_map_entry_unwire(vm_map_t map
, vm_map_entry_t entry
)
2443 entry
->eflags
&= ~MAP_ENTRY_USER_WIRED
;
2444 entry
->wired_count
= 0;
2445 vm_fault_unwire(map
, entry
);
2449 * vm_map_entry_delete: [ internal use only ]
2451 * Deallocate the given entry from the target map.
2454 vm_map_entry_delete(vm_map_t map
, vm_map_entry_t entry
, int *countp
)
2456 vm_map_entry_unlink(map
, entry
);
2457 map
->size
-= entry
->end
- entry
->start
;
2459 switch(entry
->maptype
) {
2460 case VM_MAPTYPE_NORMAL
:
2461 case VM_MAPTYPE_VPAGETABLE
:
2462 vm_object_deallocate(entry
->object
.vm_object
);
2468 vm_map_entry_dispose(map
, entry
, countp
);
2472 * vm_map_delete: [ internal use only ]
2474 * Deallocates the given address range from the target
2478 vm_map_delete(vm_map_t map
, vm_offset_t start
, vm_offset_t end
, int *countp
)
2481 vm_map_entry_t entry
;
2482 vm_map_entry_t first_entry
;
2486 * Find the start of the region, and clip it. Set entry to point
2487 * at the first record containing the requested address or, if no
2488 * such record exists, the next record with a greater address. The
2489 * loop will run from this point until a record beyond the termination
2490 * address is encountered.
2492 * map->hint must be adjusted to not point to anything we delete,
2493 * so set it to the entry prior to the one being deleted.
2495 * GGG see other GGG comment.
2497 if (vm_map_lookup_entry(map
, start
, &first_entry
)) {
2498 entry
= first_entry
;
2499 vm_map_clip_start(map
, entry
, start
, countp
);
2500 map
->hint
= entry
->prev
; /* possible problem XXX */
2502 map
->hint
= first_entry
; /* possible problem XXX */
2503 entry
= first_entry
->next
;
2507 * If a hole opens up prior to the current first_free then
2508 * adjust first_free. As with map->hint, map->first_free
2509 * cannot be left set to anything we might delete.
2511 if (entry
== &map
->header
) {
2512 map
->first_free
= &map
->header
;
2513 } else if (map
->first_free
->start
>= start
) {
2514 map
->first_free
= entry
->prev
;
2518 * Step through all entries in this region
2521 while ((entry
!= &map
->header
) && (entry
->start
< end
)) {
2522 vm_map_entry_t next
;
2524 vm_pindex_t offidxstart
, offidxend
, count
;
2527 * If we hit an in-transition entry we have to sleep and
2528 * retry. It's easier (and not really slower) to just retry
2529 * since this case occurs so rarely and the hint is already
2530 * pointing at the right place. We have to reset the
2531 * start offset so as not to accidently delete an entry
2532 * another process just created in vacated space.
2534 if (entry
->eflags
& MAP_ENTRY_IN_TRANSITION
) {
2535 entry
->eflags
|= MAP_ENTRY_NEEDS_WAKEUP
;
2536 start
= entry
->start
;
2537 ++mycpu
->gd_cnt
.v_intrans_coll
;
2538 ++mycpu
->gd_cnt
.v_intrans_wait
;
2539 vm_map_transition_wait(map
);
2542 vm_map_clip_end(map
, entry
, end
, countp
);
2548 offidxstart
= OFF_TO_IDX(entry
->offset
);
2549 count
= OFF_TO_IDX(e
- s
);
2550 object
= entry
->object
.vm_object
;
2553 * Unwire before removing addresses from the pmap; otherwise,
2554 * unwiring will put the entries back in the pmap.
2556 if (entry
->wired_count
!= 0)
2557 vm_map_entry_unwire(map
, entry
);
2559 offidxend
= offidxstart
+ count
;
2561 if (object
== &kernel_object
) {
2562 vm_object_page_remove(object
, offidxstart
, offidxend
, FALSE
);
2564 pmap_remove(map
->pmap
, s
, e
);
2565 if (object
!= NULL
&&
2566 object
->ref_count
!= 1 &&
2567 (object
->flags
& (OBJ_NOSPLIT
|OBJ_ONEMAPPING
)) == OBJ_ONEMAPPING
&&
2568 (object
->type
== OBJT_DEFAULT
|| object
->type
== OBJT_SWAP
)) {
2569 vm_object_collapse(object
);
2570 vm_object_page_remove(object
, offidxstart
, offidxend
, FALSE
);
2571 if (object
->type
== OBJT_SWAP
) {
2572 swap_pager_freespace(object
, offidxstart
, count
);
2574 if (offidxend
>= object
->size
&&
2575 offidxstart
< object
->size
) {
2576 object
->size
= offidxstart
;
2582 * Delete the entry (which may delete the object) only after
2583 * removing all pmap entries pointing to its pages.
2584 * (Otherwise, its page frames may be reallocated, and any
2585 * modify bits will be set in the wrong object!)
2587 vm_map_entry_delete(map
, entry
, countp
);
2590 return (KERN_SUCCESS
);
2596 * Remove the given address range from the target map.
2597 * This is the exported form of vm_map_delete.
2600 vm_map_remove(vm_map_t map
, vm_offset_t start
, vm_offset_t end
)
2605 count
= vm_map_entry_reserve(MAP_RESERVE_COUNT
);
2607 VM_MAP_RANGE_CHECK(map
, start
, end
);
2608 result
= vm_map_delete(map
, start
, end
, &count
);
2610 vm_map_entry_release(count
);
2616 * vm_map_check_protection:
2618 * Assert that the target map allows the specified
2619 * privilege on the entire address region given.
2620 * The entire region must be allocated.
2623 vm_map_check_protection(vm_map_t map
, vm_offset_t start
, vm_offset_t end
,
2624 vm_prot_t protection
)
2626 vm_map_entry_t entry
;
2627 vm_map_entry_t tmp_entry
;
2629 if (!vm_map_lookup_entry(map
, start
, &tmp_entry
)) {
2634 while (start
< end
) {
2635 if (entry
== &map
->header
) {
2642 if (start
< entry
->start
) {
2646 * Check protection associated with entry.
2649 if ((entry
->protection
& protection
) != protection
) {
2652 /* go to next entry */
2655 entry
= entry
->next
;
2661 * Split the pages in a map entry into a new object. This affords
2662 * easier removal of unused pages, and keeps object inheritance from
2663 * being a negative impact on memory usage.
2666 vm_map_split(vm_map_entry_t entry
)
2669 vm_object_t orig_object
, new_object
, source
;
2671 vm_pindex_t offidxstart
, offidxend
, idx
;
2673 vm_ooffset_t offset
;
2675 orig_object
= entry
->object
.vm_object
;
2676 if (orig_object
->type
!= OBJT_DEFAULT
&& orig_object
->type
!= OBJT_SWAP
)
2678 if (orig_object
->ref_count
<= 1)
2681 offset
= entry
->offset
;
2685 offidxstart
= OFF_TO_IDX(offset
);
2686 offidxend
= offidxstart
+ OFF_TO_IDX(e
- s
);
2687 size
= offidxend
- offidxstart
;
2689 new_object
= vm_pager_allocate(orig_object
->type
, NULL
,
2690 IDX_TO_OFF(size
), VM_PROT_ALL
, 0);
2691 if (new_object
== NULL
)
2694 source
= orig_object
->backing_object
;
2695 if (source
!= NULL
) {
2696 vm_object_reference(source
); /* Referenced by new_object */
2697 LIST_INSERT_HEAD(&source
->shadow_head
,
2698 new_object
, shadow_list
);
2699 vm_object_clear_flag(source
, OBJ_ONEMAPPING
);
2700 new_object
->backing_object_offset
=
2701 orig_object
->backing_object_offset
+ IDX_TO_OFF(offidxstart
);
2702 new_object
->backing_object
= source
;
2703 source
->shadow_count
++;
2704 source
->generation
++;
2707 for (idx
= 0; idx
< size
; idx
++) {
2711 * A critical section is required to avoid a race between
2712 * the lookup and an interrupt/unbusy/free and our busy
2717 m
= vm_page_lookup(orig_object
, offidxstart
+ idx
);
2724 * We must wait for pending I/O to complete before we can
2727 * We do not have to VM_PROT_NONE the page as mappings should
2728 * not be changed by this operation.
2730 if (vm_page_sleep_busy(m
, TRUE
, "spltwt"))
2733 vm_page_rename(m
, new_object
, idx
);
2734 /* page automatically made dirty by rename and cache handled */
2739 if (orig_object
->type
== OBJT_SWAP
) {
2740 vm_object_pip_add(orig_object
, 1);
2742 * copy orig_object pages into new_object
2743 * and destroy unneeded pages in
2746 swap_pager_copy(orig_object
, new_object
, offidxstart
, 0);
2747 vm_object_pip_wakeup(orig_object
);
2751 * Wakeup the pages we played with. No spl protection is needed
2752 * for a simple wakeup.
2754 for (idx
= 0; idx
< size
; idx
++) {
2755 m
= vm_page_lookup(new_object
, idx
);
2760 entry
->object
.vm_object
= new_object
;
2761 entry
->offset
= 0LL;
2762 vm_object_deallocate(orig_object
);
2766 * vm_map_copy_entry:
2768 * Copies the contents of the source entry to the destination
2769 * entry. The entries *must* be aligned properly.
2772 vm_map_copy_entry(vm_map_t src_map
, vm_map_t dst_map
,
2773 vm_map_entry_t src_entry
, vm_map_entry_t dst_entry
)
2775 vm_object_t src_object
;
2777 if (dst_entry
->maptype
== VM_MAPTYPE_SUBMAP
)
2779 if (src_entry
->maptype
== VM_MAPTYPE_SUBMAP
)
2782 if (src_entry
->wired_count
== 0) {
2784 * If the source entry is marked needs_copy, it is already
2787 if ((src_entry
->eflags
& MAP_ENTRY_NEEDS_COPY
) == 0) {
2788 pmap_protect(src_map
->pmap
,
2791 src_entry
->protection
& ~VM_PROT_WRITE
);
2795 * Make a copy of the object.
2797 if ((src_object
= src_entry
->object
.vm_object
) != NULL
) {
2798 if ((src_object
->handle
== NULL
) &&
2799 (src_object
->type
== OBJT_DEFAULT
||
2800 src_object
->type
== OBJT_SWAP
)) {
2801 vm_object_collapse(src_object
);
2802 if ((src_object
->flags
& (OBJ_NOSPLIT
|OBJ_ONEMAPPING
)) == OBJ_ONEMAPPING
) {
2803 vm_map_split(src_entry
);
2804 src_object
= src_entry
->object
.vm_object
;
2808 vm_object_reference(src_object
);
2809 vm_object_clear_flag(src_object
, OBJ_ONEMAPPING
);
2810 dst_entry
->object
.vm_object
= src_object
;
2811 src_entry
->eflags
|= (MAP_ENTRY_COW
|MAP_ENTRY_NEEDS_COPY
);
2812 dst_entry
->eflags
|= (MAP_ENTRY_COW
|MAP_ENTRY_NEEDS_COPY
);
2813 dst_entry
->offset
= src_entry
->offset
;
2815 dst_entry
->object
.vm_object
= NULL
;
2816 dst_entry
->offset
= 0;
2819 pmap_copy(dst_map
->pmap
, src_map
->pmap
, dst_entry
->start
,
2820 dst_entry
->end
- dst_entry
->start
, src_entry
->start
);
2823 * Of course, wired down pages can't be set copy-on-write.
2824 * Cause wired pages to be copied into the new map by
2825 * simulating faults (the new pages are pageable)
2827 vm_fault_copy_entry(dst_map
, src_map
, dst_entry
, src_entry
);
2833 * Create a new process vmspace structure and vm_map
2834 * based on those of an existing process. The new map
2835 * is based on the old map, according to the inheritance
2836 * values on the regions in that map.
2838 * The source map must not be locked.
2841 vmspace_fork(struct vmspace
*vm1
)
2843 struct vmspace
*vm2
;
2844 vm_map_t old_map
= &vm1
->vm_map
;
2846 vm_map_entry_t old_entry
;
2847 vm_map_entry_t new_entry
;
2851 vm_map_lock(old_map
);
2852 old_map
->infork
= 1;
2855 * XXX Note: upcalls are not copied.
2857 vm2
= vmspace_alloc(old_map
->min_offset
, old_map
->max_offset
);
2858 bcopy(&vm1
->vm_startcopy
, &vm2
->vm_startcopy
,
2859 (caddr_t
)&vm1
->vm_endcopy
- (caddr_t
)&vm1
->vm_startcopy
);
2860 new_map
= &vm2
->vm_map
; /* XXX */
2861 new_map
->timestamp
= 1;
2864 old_entry
= old_map
->header
.next
;
2865 while (old_entry
!= &old_map
->header
) {
2867 old_entry
= old_entry
->next
;
2870 count
= vm_map_entry_reserve(count
+ MAP_RESERVE_COUNT
);
2872 old_entry
= old_map
->header
.next
;
2873 while (old_entry
!= &old_map
->header
) {
2874 if (old_entry
->maptype
== VM_MAPTYPE_SUBMAP
)
2875 panic("vm_map_fork: encountered a submap");
2877 switch (old_entry
->inheritance
) {
2878 case VM_INHERIT_NONE
:
2881 case VM_INHERIT_SHARE
:
2883 * Clone the entry, creating the shared object if
2886 object
= old_entry
->object
.vm_object
;
2887 if (object
== NULL
) {
2888 vm_map_entry_allocate_object(old_entry
);
2889 object
= old_entry
->object
.vm_object
;
2893 * Add the reference before calling vm_map_entry_shadow
2894 * to insure that a shadow object is created.
2896 vm_object_reference(object
);
2897 if (old_entry
->eflags
& MAP_ENTRY_NEEDS_COPY
) {
2898 vm_map_entry_shadow(old_entry
);
2899 /* Transfer the second reference too. */
2900 vm_object_reference(
2901 old_entry
->object
.vm_object
);
2902 vm_object_deallocate(object
);
2903 object
= old_entry
->object
.vm_object
;
2905 vm_object_clear_flag(object
, OBJ_ONEMAPPING
);
2908 * Clone the entry, referencing the shared object.
2910 new_entry
= vm_map_entry_create(new_map
, &count
);
2911 *new_entry
= *old_entry
;
2912 new_entry
->eflags
&= ~MAP_ENTRY_USER_WIRED
;
2913 new_entry
->wired_count
= 0;
2916 * Insert the entry into the new map -- we know we're
2917 * inserting at the end of the new map.
2920 vm_map_entry_link(new_map
, new_map
->header
.prev
,
2924 * Update the physical map
2927 pmap_copy(new_map
->pmap
, old_map
->pmap
,
2929 (old_entry
->end
- old_entry
->start
),
2933 case VM_INHERIT_COPY
:
2935 * Clone the entry and link into the map.
2937 new_entry
= vm_map_entry_create(new_map
, &count
);
2938 *new_entry
= *old_entry
;
2939 new_entry
->eflags
&= ~MAP_ENTRY_USER_WIRED
;
2940 new_entry
->wired_count
= 0;
2941 new_entry
->object
.vm_object
= NULL
;
2942 vm_map_entry_link(new_map
, new_map
->header
.prev
,
2944 vm_map_copy_entry(old_map
, new_map
, old_entry
,
2948 old_entry
= old_entry
->next
;
2951 new_map
->size
= old_map
->size
;
2952 old_map
->infork
= 0;
2953 vm_map_unlock(old_map
);
2954 vm_map_entry_release(count
);
2960 vm_map_stack (vm_map_t map
, vm_offset_t addrbos
, vm_size_t max_ssize
,
2961 vm_prot_t prot
, vm_prot_t max
, int cow
)
2963 vm_map_entry_t prev_entry
;
2964 vm_map_entry_t new_stack_entry
;
2965 vm_size_t init_ssize
;
2969 if (VM_MIN_USER_ADDRESS
> 0 && addrbos
< VM_MIN_USER_ADDRESS
)
2970 return (KERN_NO_SPACE
);
2972 if (max_ssize
< sgrowsiz
)
2973 init_ssize
= max_ssize
;
2975 init_ssize
= sgrowsiz
;
2977 count
= vm_map_entry_reserve(MAP_RESERVE_COUNT
);
2980 /* If addr is already mapped, no go */
2981 if (vm_map_lookup_entry(map
, addrbos
, &prev_entry
)) {
2983 vm_map_entry_release(count
);
2984 return (KERN_NO_SPACE
);
2987 /* If we would blow our VMEM resource limit, no go */
2988 if (map
->size
+ init_ssize
>
2989 curproc
->p_rlimit
[RLIMIT_VMEM
].rlim_cur
) {
2991 vm_map_entry_release(count
);
2992 return (KERN_NO_SPACE
);
2995 /* If we can't accomodate max_ssize in the current mapping,
2996 * no go. However, we need to be aware that subsequent user
2997 * mappings might map into the space we have reserved for
2998 * stack, and currently this space is not protected.
3000 * Hopefully we will at least detect this condition
3001 * when we try to grow the stack.
3003 if ((prev_entry
->next
!= &map
->header
) &&
3004 (prev_entry
->next
->start
< addrbos
+ max_ssize
)) {
3006 vm_map_entry_release(count
);
3007 return (KERN_NO_SPACE
);
3010 /* We initially map a stack of only init_ssize. We will
3011 * grow as needed later. Since this is to be a grow
3012 * down stack, we map at the top of the range.
3014 * Note: we would normally expect prot and max to be
3015 * VM_PROT_ALL, and cow to be 0. Possibly we should
3016 * eliminate these as input parameters, and just
3017 * pass these values here in the insert call.
3019 rv
= vm_map_insert(map
, &count
,
3020 NULL
, 0, addrbos
+ max_ssize
- init_ssize
,
3021 addrbos
+ max_ssize
,
3026 /* Now set the avail_ssize amount */
3027 if (rv
== KERN_SUCCESS
) {
3028 if (prev_entry
!= &map
->header
)
3029 vm_map_clip_end(map
, prev_entry
, addrbos
+ max_ssize
- init_ssize
, &count
);
3030 new_stack_entry
= prev_entry
->next
;
3031 if (new_stack_entry
->end
!= addrbos
+ max_ssize
||
3032 new_stack_entry
->start
!= addrbos
+ max_ssize
- init_ssize
)
3033 panic ("Bad entry start/end for new stack entry");
3035 new_stack_entry
->aux
.avail_ssize
= max_ssize
- init_ssize
;
3039 vm_map_entry_release(count
);
3043 /* Attempts to grow a vm stack entry. Returns KERN_SUCCESS if the
3044 * desired address is already mapped, or if we successfully grow
3045 * the stack. Also returns KERN_SUCCESS if addr is outside the
3046 * stack range (this is strange, but preserves compatibility with
3047 * the grow function in vm_machdep.c).
3050 vm_map_growstack (struct proc
*p
, vm_offset_t addr
)
3052 vm_map_entry_t prev_entry
;
3053 vm_map_entry_t stack_entry
;
3054 vm_map_entry_t new_stack_entry
;
3055 struct vmspace
*vm
= p
->p_vmspace
;
3056 vm_map_t map
= &vm
->vm_map
;
3059 int rv
= KERN_SUCCESS
;
3061 int use_read_lock
= 1;
3064 count
= vm_map_entry_reserve(MAP_RESERVE_COUNT
);
3067 vm_map_lock_read(map
);
3071 /* If addr is already in the entry range, no need to grow.*/
3072 if (vm_map_lookup_entry(map
, addr
, &prev_entry
))
3075 if ((stack_entry
= prev_entry
->next
) == &map
->header
)
3077 if (prev_entry
== &map
->header
)
3078 end
= stack_entry
->start
- stack_entry
->aux
.avail_ssize
;
3080 end
= prev_entry
->end
;
3082 /* This next test mimics the old grow function in vm_machdep.c.
3083 * It really doesn't quite make sense, but we do it anyway
3084 * for compatibility.
3086 * If not growable stack, return success. This signals the
3087 * caller to proceed as he would normally with normal vm.
3089 if (stack_entry
->aux
.avail_ssize
< 1 ||
3090 addr
>= stack_entry
->start
||
3091 addr
< stack_entry
->start
- stack_entry
->aux
.avail_ssize
) {
3095 /* Find the minimum grow amount */
3096 grow_amount
= roundup (stack_entry
->start
- addr
, PAGE_SIZE
);
3097 if (grow_amount
> stack_entry
->aux
.avail_ssize
) {
3102 /* If there is no longer enough space between the entries
3103 * nogo, and adjust the available space. Note: this
3104 * should only happen if the user has mapped into the
3105 * stack area after the stack was created, and is
3106 * probably an error.
3108 * This also effectively destroys any guard page the user
3109 * might have intended by limiting the stack size.
3111 if (grow_amount
> stack_entry
->start
- end
) {
3112 if (use_read_lock
&& vm_map_lock_upgrade(map
)) {
3117 stack_entry
->aux
.avail_ssize
= stack_entry
->start
- end
;
3122 is_procstack
= addr
>= (vm_offset_t
)vm
->vm_maxsaddr
;
3124 /* If this is the main process stack, see if we're over the
3127 if (is_procstack
&& (ctob(vm
->vm_ssize
) + grow_amount
>
3128 p
->p_rlimit
[RLIMIT_STACK
].rlim_cur
)) {
3133 /* Round up the grow amount modulo SGROWSIZ */
3134 grow_amount
= roundup (grow_amount
, sgrowsiz
);
3135 if (grow_amount
> stack_entry
->aux
.avail_ssize
) {
3136 grow_amount
= stack_entry
->aux
.avail_ssize
;
3138 if (is_procstack
&& (ctob(vm
->vm_ssize
) + grow_amount
>
3139 p
->p_rlimit
[RLIMIT_STACK
].rlim_cur
)) {
3140 grow_amount
= p
->p_rlimit
[RLIMIT_STACK
].rlim_cur
-
3144 /* If we would blow our VMEM resource limit, no go */
3145 if (map
->size
+ grow_amount
> p
->p_rlimit
[RLIMIT_VMEM
].rlim_cur
) {
3150 if (use_read_lock
&& vm_map_lock_upgrade(map
)) {
3156 /* Get the preliminary new entry start value */
3157 addr
= stack_entry
->start
- grow_amount
;
3159 /* If this puts us into the previous entry, cut back our growth
3160 * to the available space. Also, see the note above.
3163 stack_entry
->aux
.avail_ssize
= stack_entry
->start
- end
;
3167 rv
= vm_map_insert(map
, &count
,
3168 NULL
, 0, addr
, stack_entry
->start
,
3170 VM_PROT_ALL
, VM_PROT_ALL
,
3173 /* Adjust the available stack space by the amount we grew. */
3174 if (rv
== KERN_SUCCESS
) {
3175 if (prev_entry
!= &map
->header
)
3176 vm_map_clip_end(map
, prev_entry
, addr
, &count
);
3177 new_stack_entry
= prev_entry
->next
;
3178 if (new_stack_entry
->end
!= stack_entry
->start
||
3179 new_stack_entry
->start
!= addr
)
3180 panic ("Bad stack grow start/end in new stack entry");
3182 new_stack_entry
->aux
.avail_ssize
=
3183 stack_entry
->aux
.avail_ssize
-
3184 (new_stack_entry
->end
- new_stack_entry
->start
);
3186 vm
->vm_ssize
+= btoc(new_stack_entry
->end
-
3187 new_stack_entry
->start
);
3193 vm_map_unlock_read(map
);
3196 vm_map_entry_release(count
);
3201 * Unshare the specified VM space for exec. If other processes are
3202 * mapped to it, then create a new one. The new vmspace is null.
3205 vmspace_exec(struct proc
*p
, struct vmspace
*vmcopy
)
3207 struct vmspace
*oldvmspace
= p
->p_vmspace
;
3208 struct vmspace
*newvmspace
;
3209 vm_map_t map
= &p
->p_vmspace
->vm_map
;
3212 * If we are execing a resident vmspace we fork it, otherwise
3213 * we create a new vmspace. Note that exitingcnt and upcalls
3214 * are not copied to the new vmspace.
3217 newvmspace
= vmspace_fork(vmcopy
);
3219 newvmspace
= vmspace_alloc(map
->min_offset
, map
->max_offset
);
3220 bcopy(&oldvmspace
->vm_startcopy
, &newvmspace
->vm_startcopy
,
3221 (caddr_t
)&oldvmspace
->vm_endcopy
-
3222 (caddr_t
)&oldvmspace
->vm_startcopy
);
3226 * Finish initializing the vmspace before assigning it
3227 * to the process. The vmspace will become the current vmspace
3230 pmap_pinit2(vmspace_pmap(newvmspace
));
3231 pmap_replacevm(p
, newvmspace
, 0);
3232 sysref_put(&oldvmspace
->vm_sysref
);
3236 * Unshare the specified VM space for forcing COW. This
3237 * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
3239 * The exitingcnt test is not strictly necessary but has been
3240 * included for code sanity (to make the code a bit more deterministic).
3244 vmspace_unshare(struct proc
*p
)
3246 struct vmspace
*oldvmspace
= p
->p_vmspace
;
3247 struct vmspace
*newvmspace
;
3249 if (oldvmspace
->vm_sysref
.refcnt
== 1 && oldvmspace
->vm_exitingcnt
== 0)
3251 newvmspace
= vmspace_fork(oldvmspace
);
3252 pmap_pinit2(vmspace_pmap(newvmspace
));
3253 pmap_replacevm(p
, newvmspace
, 0);
3254 sysref_put(&oldvmspace
->vm_sysref
);
3260 * Finds the VM object, offset, and
3261 * protection for a given virtual address in the
3262 * specified map, assuming a page fault of the
3265 * Leaves the map in question locked for read; return
3266 * values are guaranteed until a vm_map_lookup_done
3267 * call is performed. Note that the map argument
3268 * is in/out; the returned map must be used in
3269 * the call to vm_map_lookup_done.
3271 * A handle (out_entry) is returned for use in
3272 * vm_map_lookup_done, to make that fast.
3274 * If a lookup is requested with "write protection"
3275 * specified, the map may be changed to perform virtual
3276 * copying operations, although the data referenced will
3280 vm_map_lookup(vm_map_t
*var_map
, /* IN/OUT */
3282 vm_prot_t fault_typea
,
3283 vm_map_entry_t
*out_entry
, /* OUT */
3284 vm_object_t
*object
, /* OUT */
3285 vm_pindex_t
*pindex
, /* OUT */
3286 vm_prot_t
*out_prot
, /* OUT */
3287 boolean_t
*wired
) /* OUT */
3289 vm_map_entry_t entry
;
3290 vm_map_t map
= *var_map
;
3292 vm_prot_t fault_type
= fault_typea
;
3293 int use_read_lock
= 1;
3294 int rv
= KERN_SUCCESS
;
3298 vm_map_lock_read(map
);
3303 * If the map has an interesting hint, try it before calling full
3304 * blown lookup routine.
3309 if ((entry
== &map
->header
) ||
3310 (vaddr
< entry
->start
) || (vaddr
>= entry
->end
)) {
3311 vm_map_entry_t tmp_entry
;
3314 * Entry was either not a valid hint, or the vaddr was not
3315 * contained in the entry, so do a full lookup.
3317 if (!vm_map_lookup_entry(map
, vaddr
, &tmp_entry
)) {
3318 rv
= KERN_INVALID_ADDRESS
;
3329 if (entry
->maptype
== VM_MAPTYPE_SUBMAP
) {
3330 vm_map_t old_map
= map
;
3332 *var_map
= map
= entry
->object
.sub_map
;
3334 vm_map_unlock_read(old_map
);
3336 vm_map_unlock(old_map
);
3342 * Check whether this task is allowed to have this page.
3343 * Note the special case for MAP_ENTRY_COW
3344 * pages with an override. This is to implement a forced
3345 * COW for debuggers.
3348 if (fault_type
& VM_PROT_OVERRIDE_WRITE
)
3349 prot
= entry
->max_protection
;
3351 prot
= entry
->protection
;
3353 fault_type
&= (VM_PROT_READ
|VM_PROT_WRITE
|VM_PROT_EXECUTE
);
3354 if ((fault_type
& prot
) != fault_type
) {
3355 rv
= KERN_PROTECTION_FAILURE
;
3359 if ((entry
->eflags
& MAP_ENTRY_USER_WIRED
) &&
3360 (entry
->eflags
& MAP_ENTRY_COW
) &&
3361 (fault_type
& VM_PROT_WRITE
) &&
3362 (fault_typea
& VM_PROT_OVERRIDE_WRITE
) == 0) {
3363 rv
= KERN_PROTECTION_FAILURE
;
3368 * If this page is not pageable, we have to get it for all possible
3371 *wired
= (entry
->wired_count
!= 0);
3373 prot
= fault_type
= entry
->protection
;
3376 * Virtual page tables may need to update the accessed (A) bit
3377 * in a page table entry. Upgrade the fault to a write fault for
3378 * that case if the map will support it. If the map does not support
3379 * it the page table entry simply will not be updated.
3381 if (entry
->maptype
== VM_MAPTYPE_VPAGETABLE
) {
3382 if (prot
& VM_PROT_WRITE
)
3383 fault_type
|= VM_PROT_WRITE
;
3387 * If the entry was copy-on-write, we either ...
3389 if (entry
->eflags
& MAP_ENTRY_NEEDS_COPY
) {
3391 * If we want to write the page, we may as well handle that
3392 * now since we've got the map locked.
3394 * If we don't need to write the page, we just demote the
3395 * permissions allowed.
3398 if (fault_type
& VM_PROT_WRITE
) {
3400 * Make a new object, and place it in the object
3401 * chain. Note that no new references have appeared
3402 * -- one just moved from the map to the new
3406 if (use_read_lock
&& vm_map_lock_upgrade(map
)) {
3412 vm_map_entry_shadow(entry
);
3415 * We're attempting to read a copy-on-write page --
3416 * don't allow writes.
3419 prot
&= ~VM_PROT_WRITE
;
3424 * Create an object if necessary.
3426 if (entry
->object
.vm_object
== NULL
&&
3428 if (use_read_lock
&& vm_map_lock_upgrade(map
)) {
3433 vm_map_entry_allocate_object(entry
);
3437 * Return the object/offset from this entry. If the entry was
3438 * copy-on-write or empty, it has been fixed up.
3441 *pindex
= OFF_TO_IDX((vaddr
- entry
->start
) + entry
->offset
);
3442 *object
= entry
->object
.vm_object
;
3445 * Return whether this is the only map sharing this data. On
3446 * success we return with a read lock held on the map. On failure
3447 * we return with the map unlocked.
3451 if (rv
== KERN_SUCCESS
) {
3452 if (use_read_lock
== 0)
3453 vm_map_lock_downgrade(map
);
3454 } else if (use_read_lock
) {
3455 vm_map_unlock_read(map
);
3463 * vm_map_lookup_done:
3465 * Releases locks acquired by a vm_map_lookup
3466 * (according to the handle returned by that lookup).
3470 vm_map_lookup_done(vm_map_t map
, vm_map_entry_t entry
, int count
)
3473 * Unlock the main-level map
3475 vm_map_unlock_read(map
);
3477 vm_map_entry_release(count
);
3480 #include "opt_ddb.h"
3482 #include <sys/kernel.h>
3484 #include <ddb/ddb.h>
3487 * vm_map_print: [ debug ]
3489 DB_SHOW_COMMAND(map
, vm_map_print
)
3492 /* XXX convert args. */
3493 vm_map_t map
= (vm_map_t
)addr
;
3494 boolean_t full
= have_addr
;
3496 vm_map_entry_t entry
;
3498 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
3500 (void *)map
->pmap
, map
->nentries
, map
->timestamp
);
3503 if (!full
&& db_indent
)
3507 for (entry
= map
->header
.next
; entry
!= &map
->header
;
3508 entry
= entry
->next
) {
3509 db_iprintf("map entry %p: start=%p, end=%p\n",
3510 (void *)entry
, (void *)entry
->start
, (void *)entry
->end
);
3513 static char *inheritance_name
[4] =
3514 {"share", "copy", "none", "donate_copy"};
3516 db_iprintf(" prot=%x/%x/%s",
3518 entry
->max_protection
,
3519 inheritance_name
[(int)(unsigned char)entry
->inheritance
]);
3520 if (entry
->wired_count
!= 0)
3521 db_printf(", wired");
3523 if (entry
->maptype
== VM_MAPTYPE_SUBMAP
) {
3524 /* XXX no %qd in kernel. Truncate entry->offset. */
3525 db_printf(", share=%p, offset=0x%lx\n",
3526 (void *)entry
->object
.sub_map
,
3527 (long)entry
->offset
);
3529 if ((entry
->prev
== &map
->header
) ||
3530 (entry
->prev
->object
.sub_map
!=
3531 entry
->object
.sub_map
)) {
3533 vm_map_print((db_expr_t
)(intptr_t)
3534 entry
->object
.sub_map
,
3535 full
, 0, (char *)0);
3539 /* XXX no %qd in kernel. Truncate entry->offset. */
3540 db_printf(", object=%p, offset=0x%lx",
3541 (void *)entry
->object
.vm_object
,
3542 (long)entry
->offset
);
3543 if (entry
->eflags
& MAP_ENTRY_COW
)
3544 db_printf(", copy (%s)",
3545 (entry
->eflags
& MAP_ENTRY_NEEDS_COPY
) ? "needed" : "done");
3549 if ((entry
->prev
== &map
->header
) ||
3550 (entry
->prev
->object
.vm_object
!=
3551 entry
->object
.vm_object
)) {
3553 vm_object_print((db_expr_t
)(intptr_t)
3554 entry
->object
.vm_object
,
3555 full
, 0, (char *)0);
3567 DB_SHOW_COMMAND(procvm
, procvm
)
3572 p
= (struct proc
*) addr
;
3577 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
3578 (void *)p
, (void *)p
->p_vmspace
, (void *)&p
->p_vmspace
->vm_map
,
3579 (void *)vmspace_pmap(p
->p_vmspace
));
3581 vm_map_print((db_expr_t
)(intptr_t)&p
->p_vmspace
->vm_map
, 1, 0, NULL
);