2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
60 * Author: Avadis Tevanian, Jr., Michael Wayne Young
63 * Virtual memory mapping module.
66 #include <task_swapper.h>
67 #include <mach_assert.h>
68 #include <libkern/OSAtomic.h>
70 #include <mach/kern_return.h>
71 #include <mach/port.h>
72 #include <mach/vm_attributes.h>
73 #include <mach/vm_param.h>
74 #include <mach/vm_behavior.h>
75 #include <mach/vm_statistics.h>
76 #include <mach/memory_object.h>
77 #include <mach/mach_vm.h>
78 #include <machine/cpu_capabilities.h>
81 #include <kern/assert.h>
82 #include <kern/counters.h>
83 #include <kern/kalloc.h>
84 #include <kern/zalloc.h>
87 #include <vm/vm_init.h>
88 #include <vm/vm_fault.h>
89 #include <vm/vm_map.h>
90 #include <vm/vm_object.h>
91 #include <vm/vm_page.h>
92 #include <vm/vm_pageout.h>
93 #include <vm/vm_kern.h>
94 #include <ipc/ipc_port.h>
95 #include <kern/sched_prim.h>
96 #include <kern/misc_protos.h>
97 #include <machine/db_machdep.h>
100 #include <mach/vm_map_server.h>
101 #include <mach/mach_host_server.h>
102 #include <vm/vm_protos.h>
103 #include <vm/vm_purgeable_internal.h>
106 #include <ppc/mappings.h>
109 #include <vm/vm_protos.h>
110 #include <vm/vm_shared_region.h>
112 /* Internal prototypes
115 static void vm_map_simplify_range(
117 vm_map_offset_t start
,
118 vm_map_offset_t end
); /* forward */
120 static boolean_t
vm_map_range_check(
122 vm_map_offset_t start
,
124 vm_map_entry_t
*entry
);
126 static vm_map_entry_t
_vm_map_entry_create(
127 struct vm_map_header
*map_header
);
129 static void _vm_map_entry_dispose(
130 struct vm_map_header
*map_header
,
131 vm_map_entry_t entry
);
133 static void vm_map_pmap_enter(
135 vm_map_offset_t addr
,
136 vm_map_offset_t end_addr
,
138 vm_object_offset_t offset
,
139 vm_prot_t protection
);
141 static void _vm_map_clip_end(
142 struct vm_map_header
*map_header
,
143 vm_map_entry_t entry
,
144 vm_map_offset_t end
);
146 static void _vm_map_clip_start(
147 struct vm_map_header
*map_header
,
148 vm_map_entry_t entry
,
149 vm_map_offset_t start
);
151 static void vm_map_entry_delete(
153 vm_map_entry_t entry
);
155 static kern_return_t
vm_map_delete(
157 vm_map_offset_t start
,
162 static kern_return_t
vm_map_copy_overwrite_unaligned(
164 vm_map_entry_t entry
,
166 vm_map_address_t start
);
168 static kern_return_t
vm_map_copy_overwrite_aligned(
170 vm_map_entry_t tmp_entry
,
172 vm_map_offset_t start
,
175 static kern_return_t
vm_map_copyin_kernel_buffer(
177 vm_map_address_t src_addr
,
179 boolean_t src_destroy
,
180 vm_map_copy_t
*copy_result
); /* OUT */
182 static kern_return_t
vm_map_copyout_kernel_buffer(
184 vm_map_address_t
*addr
, /* IN/OUT */
186 boolean_t overwrite
);
188 static void vm_map_fork_share(
190 vm_map_entry_t old_entry
,
193 static boolean_t
vm_map_fork_copy(
195 vm_map_entry_t
*old_entry_p
,
198 void vm_map_region_top_walk(
199 vm_map_entry_t entry
,
200 vm_region_top_info_t top
);
202 void vm_map_region_walk(
205 vm_map_entry_t entry
,
206 vm_object_offset_t offset
,
207 vm_object_size_t range
,
208 vm_region_extended_info_t extended
,
209 boolean_t look_for_pages
);
211 static kern_return_t
vm_map_wire_nested(
213 vm_map_offset_t start
,
215 vm_prot_t access_type
,
218 vm_map_offset_t pmap_addr
);
220 static kern_return_t
vm_map_unwire_nested(
222 vm_map_offset_t start
,
226 vm_map_offset_t pmap_addr
);
228 static kern_return_t
vm_map_overwrite_submap_recurse(
230 vm_map_offset_t dst_addr
,
231 vm_map_size_t dst_size
);
233 static kern_return_t
vm_map_copy_overwrite_nested(
235 vm_map_offset_t dst_addr
,
237 boolean_t interruptible
,
240 static kern_return_t
vm_map_remap_extract(
242 vm_map_offset_t addr
,
245 struct vm_map_header
*map_header
,
246 vm_prot_t
*cur_protection
,
247 vm_prot_t
*max_protection
,
248 vm_inherit_t inheritance
,
251 static kern_return_t
vm_map_remap_range_allocate(
253 vm_map_address_t
*address
,
255 vm_map_offset_t mask
,
257 vm_map_entry_t
*map_entry
);
259 static void vm_map_region_look_for_page(
263 vm_object_offset_t offset
,
266 vm_region_extended_info_t extended
);
268 static int vm_map_region_count_obj_refs(
269 vm_map_entry_t entry
,
273 static kern_return_t
vm_map_willneed(
275 vm_map_offset_t start
,
276 vm_map_offset_t end
);
278 static kern_return_t
vm_map_reuse_pages(
280 vm_map_offset_t start
,
281 vm_map_offset_t end
);
283 static kern_return_t
vm_map_reusable_pages(
285 vm_map_offset_t start
,
286 vm_map_offset_t end
);
288 static kern_return_t
vm_map_can_reuse(
290 vm_map_offset_t start
,
291 vm_map_offset_t end
);
294 * Macros to copy a vm_map_entry. We must be careful to correctly
295 * manage the wired page count. vm_map_entry_copy() creates a new
296 * map entry to the same memory - the wired count in the new entry
297 * must be set to zero. vm_map_entry_copy_full() creates a new
298 * entry that is identical to the old entry. This preserves the
299 * wire count; it's used for map splitting and zone changing in
302 #define vm_map_entry_copy(NEW,OLD) \
305 (NEW)->is_shared = FALSE; \
306 (NEW)->needs_wakeup = FALSE; \
307 (NEW)->in_transition = FALSE; \
308 (NEW)->wired_count = 0; \
309 (NEW)->user_wired_count = 0; \
310 (NEW)->permanent = FALSE; \
313 #define vm_map_entry_copy_full(NEW,OLD) (*(NEW) = *(OLD))
316 * Decide if we want to allow processes to execute from their data or stack areas.
317 * override_nx() returns true if we do. Data/stack execution can be enabled independently
318 * for 32 and 64 bit processes. Set the VM_ABI_32 or VM_ABI_64 flags in allow_data_exec
319 * or allow_stack_exec to enable data execution for that type of data area for that particular
320 * ABI (or both by or'ing the flags together). These are initialized in the architecture
321 * specific pmap files since the default behavior varies according to architecture. The
322 * main reason it varies is because of the need to provide binary compatibility with old
323 * applications that were written before these restrictions came into being. In the old
324 * days, an app could execute anything it could read, but this has slowly been tightened
325 * up over time. The default behavior is:
327 * 32-bit PPC apps may execute from both stack and data areas
328 * 32-bit Intel apps may exeucte from data areas but not stack
329 * 64-bit PPC/Intel apps may not execute from either data or stack
331 * An application on any architecture may override these defaults by explicitly
332 * adding PROT_EXEC permission to the page in question with the mprotect(2)
333 * system call. This code here just determines what happens when an app tries to
334 * execute from a page that lacks execute permission.
336 * Note that allow_data_exec or allow_stack_exec may also be modified by sysctl to change the
337 * default behavior for both 32 and 64 bit apps on a system-wide basis.
340 extern int allow_data_exec
, allow_stack_exec
;
343 override_nx(vm_map_t map
, uint32_t user_tag
) /* map unused on arm */
348 * Determine if the app is running in 32 or 64 bit mode.
351 if (vm_map_is_64bit(map
))
352 current_abi
= VM_ABI_64
;
354 current_abi
= VM_ABI_32
;
357 * Determine if we should allow the execution based on whether it's a
358 * stack or data area and the current architecture.
361 if (user_tag
== VM_MEMORY_STACK
)
362 return allow_stack_exec
& current_abi
;
364 return allow_data_exec
& current_abi
;
369 * Virtual memory maps provide for the mapping, protection,
370 * and sharing of virtual memory objects. In addition,
371 * this module provides for an efficient virtual copy of
372 * memory from one map to another.
374 * Synchronization is required prior to most operations.
376 * Maps consist of an ordered doubly-linked list of simple
377 * entries; a single hint is used to speed up lookups.
379 * Sharing maps have been deleted from this version of Mach.
380 * All shared objects are now mapped directly into the respective
381 * maps. This requires a change in the copy on write strategy;
382 * the asymmetric (delayed) strategy is used for shared temporary
383 * objects instead of the symmetric (shadow) strategy. All maps
384 * are now "top level" maps (either task map, kernel map or submap
385 * of the kernel map).
387 * Since portions of maps are specified by start/end addreses,
388 * which may not align with existing map entries, all
389 * routines merely "clip" entries to these start/end values.
390 * [That is, an entry is split into two, bordering at a
391 * start or end value.] Note that these clippings may not
392 * always be necessary (as the two resulting entries are then
393 * not changed); however, the clipping is done for convenience.
394 * No attempt is currently made to "glue back together" two
397 * The symmetric (shadow) copy strategy implements virtual copy
398 * by copying VM object references from one map to
399 * another, and then marking both regions as copy-on-write.
400 * It is important to note that only one writeable reference
401 * to a VM object region exists in any map when this strategy
402 * is used -- this means that shadow object creation can be
403 * delayed until a write operation occurs. The symmetric (delayed)
404 * strategy allows multiple maps to have writeable references to
405 * the same region of a vm object, and hence cannot delay creating
406 * its copy objects. See vm_object_copy_quickly() in vm_object.c.
407 * Copying of permanent objects is completely different; see
408 * vm_object_copy_strategically() in vm_object.c.
411 static zone_t vm_map_zone
; /* zone for vm_map structures */
412 static zone_t vm_map_entry_zone
; /* zone for vm_map_entry structures */
413 static zone_t vm_map_kentry_zone
; /* zone for kernel entry structures */
414 static zone_t vm_map_copy_zone
; /* zone for vm_map_copy structures */
418 * Placeholder object for submap operations. This object is dropped
419 * into the range by a call to vm_map_find, and removed when
420 * vm_map_submap creates the submap.
423 vm_object_t vm_submap_object
;
425 static void *map_data
;
426 static vm_size_t map_data_size
;
427 static void *kentry_data
;
428 static vm_size_t kentry_data_size
;
429 static int kentry_count
= 2048; /* to init kentry_data_size */
431 #define NO_COALESCE_LIMIT ((1024 * 128) - 1)
434 /* Skip acquiring locks if we're in the midst of a kernel core dump */
435 unsigned int not_in_kdp
= 1;
437 #if CONFIG_CODE_DECRYPTION
439 * vm_map_apple_protected:
440 * This remaps the requested part of the object with an object backed by
441 * the decrypting pager.
442 * crypt_info contains entry points and session data for the crypt module.
443 * The crypt_info block will be copied by vm_map_apple_protected. The data structures
444 * referenced in crypt_info must remain valid until crypt_info->crypt_end() is called.
447 vm_map_apple_protected(
449 vm_map_offset_t start
,
451 struct pager_crypt_info
*crypt_info
)
453 boolean_t map_locked
;
455 vm_map_entry_t map_entry
;
456 memory_object_t protected_mem_obj
;
457 vm_object_t protected_object
;
458 vm_map_offset_t map_addr
;
460 vm_map_lock_read(map
);
463 /* lookup the protected VM object */
464 if (!vm_map_lookup_entry(map
,
467 map_entry
->vme_end
< end
||
468 map_entry
->is_sub_map
) {
469 /* that memory is not properly mapped */
470 kr
= KERN_INVALID_ARGUMENT
;
473 protected_object
= map_entry
->object
.vm_object
;
474 if (protected_object
== VM_OBJECT_NULL
) {
475 /* there should be a VM object here at this point */
476 kr
= KERN_INVALID_ARGUMENT
;
480 /* make sure protected object stays alive while map is unlocked */
481 vm_object_reference(protected_object
);
483 vm_map_unlock_read(map
);
487 * Lookup (and create if necessary) the protected memory object
488 * matching that VM object.
489 * If successful, this also grabs a reference on the memory object,
490 * to guarantee that it doesn't go away before we get a chance to map
493 protected_mem_obj
= apple_protect_pager_setup(protected_object
, crypt_info
);
495 /* release extra ref on protected object */
496 vm_object_deallocate(protected_object
);
498 if (protected_mem_obj
== NULL
) {
503 /* map this memory object in place of the current one */
505 kr
= vm_map_enter_mem_object(map
,
508 (mach_vm_offset_t
) 0,
509 VM_FLAGS_FIXED
| VM_FLAGS_OVERWRITE
,
510 (ipc_port_t
) protected_mem_obj
,
512 (start
- map_entry
->vme_start
)),
514 map_entry
->protection
,
515 map_entry
->max_protection
,
516 map_entry
->inheritance
);
517 assert(map_addr
== start
);
519 * Release the reference obtained by apple_protect_pager_setup().
520 * The mapping (if it succeeded) is now holding a reference on the
523 memory_object_deallocate(protected_mem_obj
);
527 vm_map_unlock_read(map
);
531 #endif /* CONFIG_CODE_DECRYPTION */
534 lck_grp_t vm_map_lck_grp
;
535 lck_grp_attr_t vm_map_lck_grp_attr
;
536 lck_attr_t vm_map_lck_attr
;
542 * Initialize the vm_map module. Must be called before
543 * any other vm_map routines.
545 * Map and entry structures are allocated from zones -- we must
546 * initialize those zones.
548 * There are three zones of interest:
550 * vm_map_zone: used to allocate maps.
551 * vm_map_entry_zone: used to allocate map entries.
552 * vm_map_kentry_zone: used to allocate map entries for the kernel.
554 * The kernel allocates map entries from a special zone that is initially
555 * "crammed" with memory. It would be difficult (perhaps impossible) for
556 * the kernel to allocate more memory to a entry zone when it became
557 * empty since the very act of allocating memory implies the creation
564 vm_map_zone
= zinit((vm_map_size_t
) sizeof(struct _vm_map
), 40*1024,
567 vm_map_entry_zone
= zinit((vm_map_size_t
) sizeof(struct vm_map_entry
),
568 1024*1024, PAGE_SIZE
*5,
569 "non-kernel map entries");
571 vm_map_kentry_zone
= zinit((vm_map_size_t
) sizeof(struct vm_map_entry
),
572 kentry_data_size
, kentry_data_size
,
573 "kernel map entries");
575 vm_map_copy_zone
= zinit((vm_map_size_t
) sizeof(struct vm_map_copy
),
576 16*1024, PAGE_SIZE
, "map copies");
579 * Cram the map and kentry zones with initial data.
580 * Set kentry_zone non-collectible to aid zone_gc().
582 zone_change(vm_map_zone
, Z_COLLECT
, FALSE
);
583 zone_change(vm_map_kentry_zone
, Z_COLLECT
, FALSE
);
584 zone_change(vm_map_kentry_zone
, Z_EXPAND
, FALSE
);
585 zone_change(vm_map_kentry_zone
, Z_FOREIGN
, TRUE
);
586 zcram(vm_map_zone
, map_data
, map_data_size
);
587 zcram(vm_map_kentry_zone
, kentry_data
, kentry_data_size
);
589 lck_grp_attr_setdefault(&vm_map_lck_grp_attr
);
590 lck_grp_init(&vm_map_lck_grp
, "vm_map", &vm_map_lck_grp_attr
);
591 lck_attr_setdefault(&vm_map_lck_attr
);
598 map_data_size
= round_page(10 * sizeof(struct _vm_map
));
599 map_data
= pmap_steal_memory(map_data_size
);
603 * Limiting worst case: vm_map_kentry_zone needs to map each "available"
604 * physical page (i.e. that beyond the kernel image and page tables)
605 * individually; we guess at most one entry per eight pages in the
606 * real world. This works out to roughly .1 of 1% of physical memory,
607 * or roughly 1900 entries (64K) for a 64M machine with 4K pages.
610 kentry_count
= pmap_free_pages() / 8;
614 round_page(kentry_count
* sizeof(struct vm_map_entry
));
615 kentry_data
= pmap_steal_memory(kentry_data_size
);
621 * Creates and returns a new empty VM map with
622 * the given physical map structure, and having
623 * the given lower and upper address bounds.
632 static int color_seed
= 0;
633 register vm_map_t result
;
635 result
= (vm_map_t
) zalloc(vm_map_zone
);
636 if (result
== VM_MAP_NULL
)
637 panic("vm_map_create");
639 vm_map_first_entry(result
) = vm_map_to_entry(result
);
640 vm_map_last_entry(result
) = vm_map_to_entry(result
);
641 result
->hdr
.nentries
= 0;
642 result
->hdr
.entries_pageable
= pageable
;
645 result
->user_wire_limit
= MACH_VM_MAX_ADDRESS
; /* default limit is unlimited */
646 result
->user_wire_size
= 0;
647 result
->ref_count
= 1;
649 result
->res_count
= 1;
650 result
->sw_state
= MAP_SW_IN
;
651 #endif /* TASK_SWAPPER */
653 result
->min_offset
= min
;
654 result
->max_offset
= max
;
655 result
->wiring_required
= FALSE
;
656 result
->no_zero_fill
= FALSE
;
657 result
->mapped
= FALSE
;
658 result
->wait_for_space
= FALSE
;
659 result
->switch_protect
= FALSE
;
660 result
->first_free
= vm_map_to_entry(result
);
661 result
->hint
= vm_map_to_entry(result
);
662 result
->color_rr
= (color_seed
++) & vm_color_mask
;
663 vm_map_lock_init(result
);
664 lck_mtx_init_ext(&result
->s_lock
, &result
->s_lock_ext
, &vm_map_lck_grp
, &vm_map_lck_attr
);
670 * vm_map_entry_create: [ internal use only ]
672 * Allocates a VM map entry for insertion in the
673 * given map (or map copy). No fields are filled.
675 #define vm_map_entry_create(map) \
676 _vm_map_entry_create(&(map)->hdr)
678 #define vm_map_copy_entry_create(copy) \
679 _vm_map_entry_create(&(copy)->cpy_hdr)
681 static vm_map_entry_t
682 _vm_map_entry_create(
683 register struct vm_map_header
*map_header
)
685 register zone_t zone
;
686 register vm_map_entry_t entry
;
688 if (map_header
->entries_pageable
)
689 zone
= vm_map_entry_zone
;
691 zone
= vm_map_kentry_zone
;
693 entry
= (vm_map_entry_t
) zalloc(zone
);
694 if (entry
== VM_MAP_ENTRY_NULL
)
695 panic("vm_map_entry_create");
701 * vm_map_entry_dispose: [ internal use only ]
703 * Inverse of vm_map_entry_create.
705 * write map lock held so no need to
706 * do anything special to insure correctness
709 #define vm_map_entry_dispose(map, entry) \
711 if((entry) == (map)->first_free) \
712 (map)->first_free = vm_map_to_entry(map); \
713 if((entry) == (map)->hint) \
714 (map)->hint = vm_map_to_entry(map); \
715 _vm_map_entry_dispose(&(map)->hdr, (entry)); \
718 #define vm_map_copy_entry_dispose(map, entry) \
719 _vm_map_entry_dispose(&(copy)->cpy_hdr, (entry))
722 _vm_map_entry_dispose(
723 register struct vm_map_header
*map_header
,
724 register vm_map_entry_t entry
)
726 register zone_t zone
;
728 if (map_header
->entries_pageable
)
729 zone
= vm_map_entry_zone
;
731 zone
= vm_map_kentry_zone
;
737 static boolean_t
first_free_is_valid(vm_map_t map
); /* forward */
738 static boolean_t first_free_check
= FALSE
;
743 vm_map_entry_t entry
, next
;
745 if (!first_free_check
)
748 entry
= vm_map_to_entry(map
);
749 next
= entry
->vme_next
;
750 while (vm_map_trunc_page(next
->vme_start
) == vm_map_trunc_page(entry
->vme_end
) ||
751 (vm_map_trunc_page(next
->vme_start
) == vm_map_trunc_page(entry
->vme_start
) &&
752 next
!= vm_map_to_entry(map
))) {
754 next
= entry
->vme_next
;
755 if (entry
== vm_map_to_entry(map
))
758 if (map
->first_free
!= entry
) {
759 printf("Bad first_free for map %p: %p should be %p\n",
760 map
, map
->first_free
, entry
);
765 #endif /* MACH_ASSERT */
770 * Updates the map->first_free pointer to the
771 * entry immediately before the first hole in the map.
772 * The map should be locked.
774 #define UPDATE_FIRST_FREE(map, new_first_free) \
777 vm_map_entry_t UFF_first_free; \
778 vm_map_entry_t UFF_next_entry; \
780 UFF_first_free = (new_first_free); \
781 UFF_next_entry = UFF_first_free->vme_next; \
782 while (vm_map_trunc_page(UFF_next_entry->vme_start) == \
783 vm_map_trunc_page(UFF_first_free->vme_end) || \
784 (vm_map_trunc_page(UFF_next_entry->vme_start) == \
785 vm_map_trunc_page(UFF_first_free->vme_start) && \
786 UFF_next_entry != vm_map_to_entry(UFF_map))) { \
787 UFF_first_free = UFF_next_entry; \
788 UFF_next_entry = UFF_first_free->vme_next; \
789 if (UFF_first_free == vm_map_to_entry(UFF_map)) \
792 UFF_map->first_free = UFF_first_free; \
793 assert(first_free_is_valid(UFF_map)); \
797 * vm_map_entry_{un,}link:
799 * Insert/remove entries from maps (or map copies).
801 #define vm_map_entry_link(map, after_where, entry) \
804 vm_map_entry_t VMEL_entry; \
806 VMEL_entry = (entry); \
807 _vm_map_entry_link(&VMEL_map->hdr, after_where, VMEL_entry); \
808 UPDATE_FIRST_FREE(VMEL_map, VMEL_map->first_free); \
812 #define vm_map_copy_entry_link(copy, after_where, entry) \
813 _vm_map_entry_link(&(copy)->cpy_hdr, after_where, (entry))
815 #define _vm_map_entry_link(hdr, after_where, entry) \
818 (entry)->vme_prev = (after_where); \
819 (entry)->vme_next = (after_where)->vme_next; \
820 (entry)->vme_prev->vme_next = (entry)->vme_next->vme_prev = (entry); \
823 #define vm_map_entry_unlink(map, entry) \
826 vm_map_entry_t VMEU_entry; \
827 vm_map_entry_t VMEU_first_free; \
829 VMEU_entry = (entry); \
830 if (VMEU_entry->vme_start <= VMEU_map->first_free->vme_start) \
831 VMEU_first_free = VMEU_entry->vme_prev; \
833 VMEU_first_free = VMEU_map->first_free; \
834 _vm_map_entry_unlink(&VMEU_map->hdr, VMEU_entry); \
835 UPDATE_FIRST_FREE(VMEU_map, VMEU_first_free); \
838 #define vm_map_copy_entry_unlink(copy, entry) \
839 _vm_map_entry_unlink(&(copy)->cpy_hdr, (entry))
841 #define _vm_map_entry_unlink(hdr, entry) \
844 (entry)->vme_next->vme_prev = (entry)->vme_prev; \
845 (entry)->vme_prev->vme_next = (entry)->vme_next; \
848 #if MACH_ASSERT && TASK_SWAPPER
850 * vm_map_res_reference:
852 * Adds another valid residence count to the given map.
854 * Map is locked so this function can be called from
858 void vm_map_res_reference(register vm_map_t map
)
860 /* assert map is locked */
861 assert(map
->res_count
>= 0);
862 assert(map
->ref_count
>= map
->res_count
);
863 if (map
->res_count
== 0) {
864 lck_mtx_unlock(&map
->s_lock
);
867 lck_mtx_lock(&map
->s_lock
);
875 * vm_map_reference_swap:
877 * Adds valid reference and residence counts to the given map.
879 * The map may not be in memory (i.e. zero residence count).
882 void vm_map_reference_swap(register vm_map_t map
)
884 assert(map
!= VM_MAP_NULL
);
885 lck_mtx_lock(&map
->s_lock
);
886 assert(map
->res_count
>= 0);
887 assert(map
->ref_count
>= map
->res_count
);
889 vm_map_res_reference(map
);
890 lck_mtx_unlock(&map
->s_lock
);
894 * vm_map_res_deallocate:
896 * Decrement residence count on a map; possibly causing swapout.
898 * The map must be in memory (i.e. non-zero residence count).
900 * The map is locked, so this function is callable from vm_map_deallocate.
903 void vm_map_res_deallocate(register vm_map_t map
)
905 assert(map
->res_count
> 0);
906 if (--map
->res_count
== 0) {
907 lck_mtx_unlock(&map
->s_lock
);
911 lck_mtx_lock(&map
->s_lock
);
913 assert(map
->ref_count
>= map
->res_count
);
915 #endif /* MACH_ASSERT && TASK_SWAPPER */
920 * Actually destroy a map.
929 /* clean up regular map entries */
930 (void) vm_map_delete(map
, map
->min_offset
, map
->max_offset
,
932 /* clean up leftover special mappings (commpage, etc...) */
935 * PPC51: ppc64 is limited to 51-bit addresses.
936 * Memory beyond this 51-bit limit is mapped specially at the
937 * pmap level, so do not interfere.
938 * On PPC64, the commpage is mapped beyond the addressable range
939 * via a special pmap hack, so ask pmap to clean it explicitly...
942 pmap_unmap_sharedpage(map
->pmap
);
944 /* ... and do not let regular pmap cleanup apply here */
945 flags
|= VM_MAP_REMOVE_NO_PMAP_CLEANUP
;
947 (void) vm_map_delete(map
, 0x0, 0xFFFFFFFFFFFFF000ULL
,
951 assert(map
->hdr
.nentries
== 0);
954 pmap_destroy(map
->pmap
);
956 zfree(vm_map_zone
, map
);
961 * vm_map_swapin/vm_map_swapout
963 * Swap a map in and out, either referencing or releasing its resources.
964 * These functions are internal use only; however, they must be exported
965 * because they may be called from macros, which are exported.
967 * In the case of swapout, there could be races on the residence count,
968 * so if the residence count is up, we return, assuming that a
969 * vm_map_deallocate() call in the near future will bring us back.
972 * -- We use the map write lock for synchronization among races.
973 * -- The map write lock, and not the simple s_lock, protects the
974 * swap state of the map.
975 * -- If a map entry is a share map, then we hold both locks, in
976 * hierarchical order.
978 * Synchronization Notes:
979 * 1) If a vm_map_swapin() call happens while swapout in progress, it
980 * will block on the map lock and proceed when swapout is through.
981 * 2) A vm_map_reference() call at this time is illegal, and will
982 * cause a panic. vm_map_reference() is only allowed on resident
983 * maps, since it refuses to block.
984 * 3) A vm_map_swapin() call during a swapin will block, and
985 * proceeed when the first swapin is done, turning into a nop.
986 * This is the reason the res_count is not incremented until
987 * after the swapin is complete.
988 * 4) There is a timing hole after the checks of the res_count, before
989 * the map lock is taken, during which a swapin may get the lock
990 * before a swapout about to happen. If this happens, the swapin
991 * will detect the state and increment the reference count, causing
992 * the swapout to be a nop, thereby delaying it until a later
993 * vm_map_deallocate. If the swapout gets the lock first, then
994 * the swapin will simply block until the swapout is done, and
997 * Because vm_map_swapin() is potentially an expensive operation, it
998 * should be used with caution.
1001 * 1) A map with a residence count of zero is either swapped, or
1003 * 2) A map with a non-zero residence count is either resident,
1004 * or being swapped in.
1007 int vm_map_swap_enable
= 1;
1009 void vm_map_swapin (vm_map_t map
)
1011 register vm_map_entry_t entry
;
1013 if (!vm_map_swap_enable
) /* debug */
1018 * First deal with various races.
1020 if (map
->sw_state
== MAP_SW_IN
)
1022 * we raced with swapout and won. Returning will incr.
1023 * the res_count, turning the swapout into a nop.
1028 * The residence count must be zero. If we raced with another
1029 * swapin, the state would have been IN; if we raced with a
1030 * swapout (after another competing swapin), we must have lost
1031 * the race to get here (see above comment), in which case
1032 * res_count is still 0.
1034 assert(map
->res_count
== 0);
1037 * There are no intermediate states of a map going out or
1038 * coming in, since the map is locked during the transition.
1040 assert(map
->sw_state
== MAP_SW_OUT
);
1043 * We now operate upon each map entry. If the entry is a sub-
1044 * or share-map, we call vm_map_res_reference upon it.
1045 * If the entry is an object, we call vm_object_res_reference
1046 * (this may iterate through the shadow chain).
1047 * Note that we hold the map locked the entire time,
1048 * even if we get back here via a recursive call in
1049 * vm_map_res_reference.
1051 entry
= vm_map_first_entry(map
);
1053 while (entry
!= vm_map_to_entry(map
)) {
1054 if (entry
->object
.vm_object
!= VM_OBJECT_NULL
) {
1055 if (entry
->is_sub_map
) {
1056 vm_map_t lmap
= entry
->object
.sub_map
;
1057 lck_mtx_lock(&lmap
->s_lock
);
1058 vm_map_res_reference(lmap
);
1059 lck_mtx_unlock(&lmap
->s_lock
);
1061 vm_object_t object
= entry
->object
.vm_object
;
1062 vm_object_lock(object
);
1064 * This call may iterate through the
1067 vm_object_res_reference(object
);
1068 vm_object_unlock(object
);
1071 entry
= entry
->vme_next
;
1073 assert(map
->sw_state
== MAP_SW_OUT
);
1074 map
->sw_state
= MAP_SW_IN
;
1077 void vm_map_swapout(vm_map_t map
)
1079 register vm_map_entry_t entry
;
1083 * First deal with various races.
1084 * If we raced with a swapin and lost, the residence count
1085 * will have been incremented to 1, and we simply return.
1087 lck_mtx_lock(&map
->s_lock
);
1088 if (map
->res_count
!= 0) {
1089 lck_mtx_unlock(&map
->s_lock
);
1092 lck_mtx_unlock(&map
->s_lock
);
1095 * There are no intermediate states of a map going out or
1096 * coming in, since the map is locked during the transition.
1098 assert(map
->sw_state
== MAP_SW_IN
);
1100 if (!vm_map_swap_enable
)
1104 * We now operate upon each map entry. If the entry is a sub-
1105 * or share-map, we call vm_map_res_deallocate upon it.
1106 * If the entry is an object, we call vm_object_res_deallocate
1107 * (this may iterate through the shadow chain).
1108 * Note that we hold the map locked the entire time,
1109 * even if we get back here via a recursive call in
1110 * vm_map_res_deallocate.
1112 entry
= vm_map_first_entry(map
);
1114 while (entry
!= vm_map_to_entry(map
)) {
1115 if (entry
->object
.vm_object
!= VM_OBJECT_NULL
) {
1116 if (entry
->is_sub_map
) {
1117 vm_map_t lmap
= entry
->object
.sub_map
;
1118 lck_mtx_lock(&lmap
->s_lock
);
1119 vm_map_res_deallocate(lmap
);
1120 lck_mtx_unlock(&lmap
->s_lock
);
1122 vm_object_t object
= entry
->object
.vm_object
;
1123 vm_object_lock(object
);
1125 * This call may take a long time,
1126 * since it could actively push
1127 * out pages (if we implement it
1130 vm_object_res_deallocate(object
);
1131 vm_object_unlock(object
);
1134 entry
= entry
->vme_next
;
1136 assert(map
->sw_state
== MAP_SW_IN
);
1137 map
->sw_state
= MAP_SW_OUT
;
1140 #endif /* TASK_SWAPPER */
1144 * SAVE_HINT_MAP_READ:
1146 * Saves the specified entry as the hint for
1147 * future lookups. only a read lock is held on map,
1148 * so make sure the store is atomic... OSCompareAndSwap
1149 * guarantees this... also, we don't care if we collide
1150 * and someone else wins and stores their 'hint'
1152 #define SAVE_HINT_MAP_READ(map,value) \
1154 OSCompareAndSwapPtr((map)->hint, value, &(map)->hint); \
1159 * SAVE_HINT_MAP_WRITE:
1161 * Saves the specified entry as the hint for
1162 * future lookups. write lock held on map,
1163 * so no one else can be writing or looking
1164 * until the lock is dropped, so it's safe
1165 * to just do an assignment
1167 #define SAVE_HINT_MAP_WRITE(map,value) \
1169 (map)->hint = (value); \
1173 * vm_map_lookup_entry: [ internal use only ]
1175 * Finds the map entry containing (or
1176 * immediately preceding) the specified address
1177 * in the given map; the entry is returned
1178 * in the "entry" parameter. The boolean
1179 * result indicates whether the address is
1180 * actually contained in the map.
1183 vm_map_lookup_entry(
1184 register vm_map_t map
,
1185 register vm_map_offset_t address
,
1186 vm_map_entry_t
*entry
) /* OUT */
1188 register vm_map_entry_t cur
;
1189 register vm_map_entry_t last
;
1192 * Start looking either from the head of the
1193 * list, or from the hint.
1197 if (cur
== vm_map_to_entry(map
))
1198 cur
= cur
->vme_next
;
1200 if (address
>= cur
->vme_start
) {
1202 * Go from hint to end of list.
1204 * But first, make a quick check to see if
1205 * we are already looking at the entry we
1206 * want (which is usually the case).
1207 * Note also that we don't need to save the hint
1208 * here... it is the same hint (unless we are
1209 * at the header, in which case the hint didn't
1210 * buy us anything anyway).
1212 last
= vm_map_to_entry(map
);
1213 if ((cur
!= last
) && (cur
->vme_end
> address
)) {
1220 * Go from start to hint, *inclusively*
1222 last
= cur
->vme_next
;
1223 cur
= vm_map_first_entry(map
);
1230 while (cur
!= last
) {
1231 if (cur
->vme_end
> address
) {
1232 if (address
>= cur
->vme_start
) {
1234 * Save this lookup for future
1239 SAVE_HINT_MAP_READ(map
, cur
);
1245 cur
= cur
->vme_next
;
1247 *entry
= cur
->vme_prev
;
1248 SAVE_HINT_MAP_READ(map
, *entry
);
1254 * Routine: vm_map_find_space
1256 * Allocate a range in the specified virtual address map,
1257 * returning the entry allocated for that range.
1258 * Used by kmem_alloc, etc.
1260 * The map must be NOT be locked. It will be returned locked
1261 * on KERN_SUCCESS, unlocked on failure.
1263 * If an entry is allocated, the object/offset fields
1264 * are initialized to zero.
1268 register vm_map_t map
,
1269 vm_map_offset_t
*address
, /* OUT */
1271 vm_map_offset_t mask
,
1273 vm_map_entry_t
*o_entry
) /* OUT */
1275 register vm_map_entry_t entry
, new_entry
;
1276 register vm_map_offset_t start
;
1277 register vm_map_offset_t end
;
1281 return KERN_INVALID_ARGUMENT
;
1284 if (flags
& VM_FLAGS_GUARD_AFTER
) {
1285 /* account for the back guard page in the size */
1286 size
+= PAGE_SIZE_64
;
1289 new_entry
= vm_map_entry_create(map
);
1292 * Look for the first possible address; if there's already
1293 * something at this address, we have to start after it.
1298 assert(first_free_is_valid(map
));
1299 if ((entry
= map
->first_free
) == vm_map_to_entry(map
))
1300 start
= map
->min_offset
;
1302 start
= entry
->vme_end
;
1305 * In any case, the "entry" always precedes
1306 * the proposed new region throughout the loop:
1310 register vm_map_entry_t next
;
1313 * Find the end of the proposed new region.
1314 * Be sure we didn't go beyond the end, or
1315 * wrap around the address.
1318 if (flags
& VM_FLAGS_GUARD_BEFORE
) {
1319 /* reserve space for the front guard page */
1320 start
+= PAGE_SIZE_64
;
1322 end
= ((start
+ mask
) & ~mask
);
1325 vm_map_entry_dispose(map
, new_entry
);
1327 return(KERN_NO_SPACE
);
1332 if ((end
> map
->max_offset
) || (end
< start
)) {
1333 vm_map_entry_dispose(map
, new_entry
);
1335 return(KERN_NO_SPACE
);
1339 * If there are no more entries, we must win.
1342 next
= entry
->vme_next
;
1343 if (next
== vm_map_to_entry(map
))
1347 * If there is another entry, it must be
1348 * after the end of the potential new region.
1351 if (next
->vme_start
>= end
)
1355 * Didn't fit -- move to the next entry.
1359 start
= entry
->vme_end
;
1364 * "start" and "end" should define the endpoints of the
1365 * available new range, and
1366 * "entry" should refer to the region before the new
1369 * the map should be locked.
1372 if (flags
& VM_FLAGS_GUARD_BEFORE
) {
1373 /* go back for the front guard page */
1374 start
-= PAGE_SIZE_64
;
1378 new_entry
->vme_start
= start
;
1379 new_entry
->vme_end
= end
;
1380 assert(page_aligned(new_entry
->vme_start
));
1381 assert(page_aligned(new_entry
->vme_end
));
1383 new_entry
->is_shared
= FALSE
;
1384 new_entry
->is_sub_map
= FALSE
;
1385 new_entry
->use_pmap
= FALSE
;
1386 new_entry
->object
.vm_object
= VM_OBJECT_NULL
;
1387 new_entry
->offset
= (vm_object_offset_t
) 0;
1389 new_entry
->needs_copy
= FALSE
;
1391 new_entry
->inheritance
= VM_INHERIT_DEFAULT
;
1392 new_entry
->protection
= VM_PROT_DEFAULT
;
1393 new_entry
->max_protection
= VM_PROT_ALL
;
1394 new_entry
->behavior
= VM_BEHAVIOR_DEFAULT
;
1395 new_entry
->wired_count
= 0;
1396 new_entry
->user_wired_count
= 0;
1398 new_entry
->in_transition
= FALSE
;
1399 new_entry
->needs_wakeup
= FALSE
;
1400 new_entry
->no_cache
= FALSE
;
1401 new_entry
->permanent
= FALSE
;
1402 new_entry
->superpage_size
= 0;
1404 new_entry
->alias
= 0;
1405 new_entry
->zero_wired_pages
= FALSE
;
1407 VM_GET_FLAGS_ALIAS(flags
, new_entry
->alias
);
1410 * Insert the new entry into the list
1413 vm_map_entry_link(map
, entry
, new_entry
);
1418 * Update the lookup hint
1420 SAVE_HINT_MAP_WRITE(map
, new_entry
);
1422 *o_entry
= new_entry
;
1423 return(KERN_SUCCESS
);
1426 int vm_map_pmap_enter_print
= FALSE
;
1427 int vm_map_pmap_enter_enable
= FALSE
;
1430 * Routine: vm_map_pmap_enter [internal only]
1433 * Force pages from the specified object to be entered into
1434 * the pmap at the specified address if they are present.
1435 * As soon as a page not found in the object the scan ends.
1440 * In/out conditions:
1441 * The source map should not be locked on entry.
1446 register vm_map_offset_t addr
,
1447 register vm_map_offset_t end_addr
,
1448 register vm_object_t object
,
1449 vm_object_offset_t offset
,
1450 vm_prot_t protection
)
1458 while (addr
< end_addr
) {
1459 register vm_page_t m
;
1461 vm_object_lock(object
);
1463 m
= vm_page_lookup(object
, offset
);
1466 * The user should never see encrypted data, so do not
1467 * enter an encrypted page in the page table.
1469 if (m
== VM_PAGE_NULL
|| m
->busy
|| m
->encrypted
||
1471 (m
->unusual
&& ( m
->error
|| m
->restart
|| m
->absent
))) {
1472 vm_object_unlock(object
);
1476 if (vm_map_pmap_enter_print
) {
1477 printf("vm_map_pmap_enter:");
1478 printf("map: %p, addr: %llx, object: %p, offset: %llx\n",
1479 map
, (unsigned long long)addr
, object
, (unsigned long long)offset
);
1481 type_of_fault
= DBG_CACHE_HIT_FAULT
;
1482 kr
= vm_fault_enter(m
, map
->pmap
, addr
, protection
,
1483 VM_PAGE_WIRED(m
), FALSE
, FALSE
,
1486 vm_object_unlock(object
);
1488 offset
+= PAGE_SIZE_64
;
1493 boolean_t
vm_map_pmap_is_empty(
1495 vm_map_offset_t start
,
1496 vm_map_offset_t end
);
1497 boolean_t
vm_map_pmap_is_empty(
1499 vm_map_offset_t start
,
1500 vm_map_offset_t end
)
1502 #ifdef MACHINE_PMAP_IS_EMPTY
1503 return pmap_is_empty(map
->pmap
, start
, end
);
1504 #else /* MACHINE_PMAP_IS_EMPTY */
1505 vm_map_offset_t offset
;
1508 if (map
->pmap
== NULL
) {
1512 for (offset
= start
;
1514 offset
+= PAGE_SIZE
) {
1515 phys_page
= pmap_find_phys(map
->pmap
, offset
);
1517 kprintf("vm_map_pmap_is_empty(%p,0x%llx,0x%llx): "
1518 "page %d at 0x%llx\n",
1519 map
, (long long)start
, (long long)end
,
1520 phys_page
, (long long)offset
);
1525 #endif /* MACHINE_PMAP_IS_EMPTY */
1529 * Routine: vm_map_enter
1532 * Allocate a range in the specified virtual address map.
1533 * The resulting range will refer to memory defined by
1534 * the given memory object and offset into that object.
1536 * Arguments are as defined in the vm_map call.
1538 int _map_enter_debug
= 0;
1539 static unsigned int vm_map_enter_restore_successes
= 0;
1540 static unsigned int vm_map_enter_restore_failures
= 0;
1544 vm_map_offset_t
*address
, /* IN/OUT */
1546 vm_map_offset_t mask
,
1549 vm_object_offset_t offset
,
1550 boolean_t needs_copy
,
1551 vm_prot_t cur_protection
,
1552 vm_prot_t max_protection
,
1553 vm_inherit_t inheritance
)
1555 vm_map_entry_t entry
, new_entry
;
1556 vm_map_offset_t start
, tmp_start
, tmp_offset
;
1557 vm_map_offset_t end
, tmp_end
;
1558 vm_map_offset_t tmp2_start
, tmp2_end
;
1559 vm_map_offset_t step
;
1560 kern_return_t result
= KERN_SUCCESS
;
1561 vm_map_t zap_old_map
= VM_MAP_NULL
;
1562 vm_map_t zap_new_map
= VM_MAP_NULL
;
1563 boolean_t map_locked
= FALSE
;
1564 boolean_t pmap_empty
= TRUE
;
1565 boolean_t new_mapping_established
= FALSE
;
1566 boolean_t anywhere
= ((flags
& VM_FLAGS_ANYWHERE
) != 0);
1567 boolean_t purgable
= ((flags
& VM_FLAGS_PURGABLE
) != 0);
1568 boolean_t overwrite
= ((flags
& VM_FLAGS_OVERWRITE
) != 0);
1569 boolean_t no_cache
= ((flags
& VM_FLAGS_NO_CACHE
) != 0);
1570 boolean_t is_submap
= ((flags
& VM_FLAGS_SUBMAP
) != 0);
1571 boolean_t permanent
= ((flags
& VM_FLAGS_PERMANENT
) != 0);
1572 unsigned int superpage_size
= ((flags
& VM_FLAGS_SUPERPAGE_MASK
) >> VM_FLAGS_SUPERPAGE_SHIFT
);
1574 vm_map_offset_t effective_min_offset
, effective_max_offset
;
1577 if (superpage_size
) {
1578 switch (superpage_size
) {
1580 * Note that the current implementation only supports
1581 * a single size for superpages, SUPERPAGE_SIZE, per
1582 * architecture. As soon as more sizes are supposed
1583 * to be supported, SUPERPAGE_SIZE has to be replaced
1584 * with a lookup of the size depending on superpage_size.
1587 case SUPERPAGE_SIZE_2MB
:
1591 return KERN_INVALID_ARGUMENT
;
1593 mask
= SUPERPAGE_SIZE
-1;
1594 if (size
& (SUPERPAGE_SIZE
-1))
1595 return KERN_INVALID_ARGUMENT
;
1596 inheritance
= VM_INHERIT_NONE
; /* fork() children won't inherit superpages */
1600 if (cur_protection
& VM_PROT_WRITE
) {
1601 if (cur_protection
& VM_PROT_EXECUTE
) {
1602 printf("EMBEDDED: %s curprot cannot be write+execute. turning off execute\n", __PRETTY_FUNCTION__
);
1603 cur_protection
&= ~VM_PROT_EXECUTE
;
1606 #endif /* CONFIG_EMBEDDED */
1610 /* submaps can not be purgeable */
1611 return KERN_INVALID_ARGUMENT
;
1613 if (object
== VM_OBJECT_NULL
) {
1614 /* submaps can not be created lazily */
1615 return KERN_INVALID_ARGUMENT
;
1618 if (flags
& VM_FLAGS_ALREADY
) {
1620 * VM_FLAGS_ALREADY says that it's OK if the same mapping
1621 * is already present. For it to be meaningul, the requested
1622 * mapping has to be at a fixed address (!VM_FLAGS_ANYWHERE) and
1623 * we shouldn't try and remove what was mapped there first
1624 * (!VM_FLAGS_OVERWRITE).
1626 if ((flags
& VM_FLAGS_ANYWHERE
) ||
1627 (flags
& VM_FLAGS_OVERWRITE
)) {
1628 return KERN_INVALID_ARGUMENT
;
1632 if (flags
& VM_FLAGS_BELOW_MIN
) {
1634 * Allow an insertion below the map's min offset.
1636 effective_min_offset
= 0ULL;
1638 effective_min_offset
= map
->min_offset
;
1641 if (flags
& VM_FLAGS_BEYOND_MAX
) {
1643 * Allow an insertion beyond the map's max offset.
1645 if (vm_map_is_64bit(map
))
1646 effective_max_offset
= 0xFFFFFFFFFFFFF000ULL
;
1648 effective_max_offset
= 0x00000000FFFFF000ULL
;
1650 effective_max_offset
= map
->max_offset
;
1654 (offset
& PAGE_MASK_64
) != 0) {
1656 return KERN_INVALID_ARGUMENT
;
1659 VM_GET_FLAGS_ALIAS(flags
, alias
);
1661 #define RETURN(value) { result = value; goto BailOut; }
1663 assert(page_aligned(*address
));
1664 assert(page_aligned(size
));
1667 * Only zero-fill objects are allowed to be purgable.
1668 * LP64todo - limit purgable objects to 32-bits for now
1672 (object
!= VM_OBJECT_NULL
&&
1673 (object
->size
!= size
||
1674 object
->purgable
== VM_PURGABLE_DENY
))
1675 || size
> ANON_MAX_SIZE
)) /* LP64todo: remove when dp capable */
1676 return KERN_INVALID_ARGUMENT
;
1678 if (!anywhere
&& overwrite
) {
1680 * Create a temporary VM map to hold the old mappings in the
1681 * affected area while we create the new one.
1682 * This avoids releasing the VM map lock in
1683 * vm_map_entry_delete() and allows atomicity
1684 * when we want to replace some mappings with a new one.
1685 * It also allows us to restore the old VM mappings if the
1686 * new mapping fails.
1688 zap_old_map
= vm_map_create(PMAP_NULL
,
1691 map
->hdr
.entries_pageable
);
1703 * Calculate the first possible address.
1706 if (start
< effective_min_offset
)
1707 start
= effective_min_offset
;
1708 if (start
> effective_max_offset
)
1709 RETURN(KERN_NO_SPACE
);
1712 * Look for the first possible address;
1713 * if there's already something at this
1714 * address, we have to start after it.
1717 assert(first_free_is_valid(map
));
1718 if (start
== effective_min_offset
) {
1719 if ((entry
= map
->first_free
) != vm_map_to_entry(map
))
1720 start
= entry
->vme_end
;
1722 vm_map_entry_t tmp_entry
;
1723 if (vm_map_lookup_entry(map
, start
, &tmp_entry
))
1724 start
= tmp_entry
->vme_end
;
1729 * In any case, the "entry" always precedes
1730 * the proposed new region throughout the
1735 register vm_map_entry_t next
;
1738 * Find the end of the proposed new region.
1739 * Be sure we didn't go beyond the end, or
1740 * wrap around the address.
1743 end
= ((start
+ mask
) & ~mask
);
1745 RETURN(KERN_NO_SPACE
);
1749 if ((end
> effective_max_offset
) || (end
< start
)) {
1750 if (map
->wait_for_space
) {
1751 if (size
<= (effective_max_offset
-
1752 effective_min_offset
)) {
1753 assert_wait((event_t
)map
,
1757 thread_block(THREAD_CONTINUE_NULL
);
1761 RETURN(KERN_NO_SPACE
);
1765 * If there are no more entries, we must win.
1768 next
= entry
->vme_next
;
1769 if (next
== vm_map_to_entry(map
))
1773 * If there is another entry, it must be
1774 * after the end of the potential new region.
1777 if (next
->vme_start
>= end
)
1781 * Didn't fit -- move to the next entry.
1785 start
= entry
->vme_end
;
1791 * the address doesn't itself violate
1792 * the mask requirement.
1797 if ((start
& mask
) != 0)
1798 RETURN(KERN_NO_SPACE
);
1801 * ... the address is within bounds
1806 if ((start
< effective_min_offset
) ||
1807 (end
> effective_max_offset
) ||
1809 RETURN(KERN_INVALID_ADDRESS
);
1812 if (overwrite
&& zap_old_map
!= VM_MAP_NULL
) {
1814 * Fixed mapping and "overwrite" flag: attempt to
1815 * remove all existing mappings in the specified
1816 * address range, saving them in our "zap_old_map".
1818 (void) vm_map_delete(map
, start
, end
,
1819 VM_MAP_REMOVE_SAVE_ENTRIES
,
1824 * ... the starting address isn't allocated
1827 if (vm_map_lookup_entry(map
, start
, &entry
)) {
1828 if (! (flags
& VM_FLAGS_ALREADY
)) {
1829 RETURN(KERN_NO_SPACE
);
1832 * Check if what's already there is what we want.
1835 tmp_offset
= offset
;
1836 if (entry
->vme_start
< start
) {
1837 tmp_start
-= start
- entry
->vme_start
;
1838 tmp_offset
-= start
- entry
->vme_start
;
1841 for (; entry
->vme_start
< end
;
1842 entry
= entry
->vme_next
) {
1844 * Check if the mapping's attributes
1845 * match the existing map entry.
1847 if (entry
== vm_map_to_entry(map
) ||
1848 entry
->vme_start
!= tmp_start
||
1849 entry
->is_sub_map
!= is_submap
||
1850 entry
->offset
!= tmp_offset
||
1851 entry
->needs_copy
!= needs_copy
||
1852 entry
->protection
!= cur_protection
||
1853 entry
->max_protection
!= max_protection
||
1854 entry
->inheritance
!= inheritance
||
1855 entry
->alias
!= alias
) {
1856 /* not the same mapping ! */
1857 RETURN(KERN_NO_SPACE
);
1860 * Check if the same object is being mapped.
1863 if (entry
->object
.sub_map
!=
1864 (vm_map_t
) object
) {
1865 /* not the same submap */
1866 RETURN(KERN_NO_SPACE
);
1869 if (entry
->object
.vm_object
!= object
) {
1870 /* not the same VM object... */
1873 obj2
= entry
->object
.vm_object
;
1874 if ((obj2
== VM_OBJECT_NULL
||
1876 (object
== VM_OBJECT_NULL
||
1877 object
->internal
)) {
1884 RETURN(KERN_NO_SPACE
);
1889 tmp_offset
+= entry
->vme_end
- entry
->vme_start
;
1890 tmp_start
+= entry
->vme_end
- entry
->vme_start
;
1891 if (entry
->vme_end
>= end
) {
1892 /* reached the end of our mapping */
1896 /* it all matches: let's use what's already there ! */
1897 RETURN(KERN_MEMORY_PRESENT
);
1901 * ... the next region doesn't overlap the
1905 if ((entry
->vme_next
!= vm_map_to_entry(map
)) &&
1906 (entry
->vme_next
->vme_start
< end
))
1907 RETURN(KERN_NO_SPACE
);
1912 * "start" and "end" should define the endpoints of the
1913 * available new range, and
1914 * "entry" should refer to the region before the new
1917 * the map should be locked.
1921 * See whether we can avoid creating a new entry (and object) by
1922 * extending one of our neighbors. [So far, we only attempt to
1923 * extend from below.] Note that we can never extend/join
1924 * purgable objects because they need to remain distinct
1925 * entities in order to implement their "volatile object"
1930 if (object
== VM_OBJECT_NULL
) {
1931 object
= vm_object_allocate(size
);
1932 object
->copy_strategy
= MEMORY_OBJECT_COPY_NONE
;
1933 object
->purgable
= VM_PURGABLE_NONVOLATILE
;
1934 offset
= (vm_object_offset_t
)0;
1936 } else if ((is_submap
== FALSE
) &&
1937 (object
== VM_OBJECT_NULL
) &&
1938 (entry
!= vm_map_to_entry(map
)) &&
1939 (entry
->vme_end
== start
) &&
1940 (!entry
->is_shared
) &&
1941 (!entry
->is_sub_map
) &&
1942 (entry
->alias
== alias
) &&
1943 (entry
->inheritance
== inheritance
) &&
1944 (entry
->protection
== cur_protection
) &&
1945 (entry
->max_protection
== max_protection
) &&
1946 (entry
->behavior
== VM_BEHAVIOR_DEFAULT
) &&
1947 (entry
->in_transition
== 0) &&
1948 (entry
->no_cache
== no_cache
) &&
1949 ((entry
->vme_end
- entry
->vme_start
) + size
<=
1950 (alias
== VM_MEMORY_REALLOC
?
1952 NO_COALESCE_LIMIT
)) &&
1953 (entry
->wired_count
== 0)) { /* implies user_wired_count == 0 */
1954 if (vm_object_coalesce(entry
->object
.vm_object
,
1957 (vm_object_offset_t
) 0,
1958 (vm_map_size_t
)(entry
->vme_end
- entry
->vme_start
),
1959 (vm_map_size_t
)(end
- entry
->vme_end
))) {
1962 * Coalesced the two objects - can extend
1963 * the previous map entry to include the
1966 map
->size
+= (end
- entry
->vme_end
);
1967 entry
->vme_end
= end
;
1968 UPDATE_FIRST_FREE(map
, map
->first_free
);
1969 RETURN(KERN_SUCCESS
);
1973 step
= superpage_size
? SUPERPAGE_SIZE
: (end
- start
);
1976 for (tmp2_start
= start
; tmp2_start
<end
; tmp2_start
+= step
) {
1977 tmp2_end
= tmp2_start
+ step
;
1979 * Create a new entry
1980 * LP64todo - for now, we can only allocate 4GB internal objects
1981 * because the default pager can't page bigger ones. Remove this
1985 * The reserved "page zero" in each process's address space can
1986 * be arbitrarily large. Splitting it into separate 4GB objects and
1987 * therefore different VM map entries serves no purpose and just
1988 * slows down operations on the VM map, so let's not split the
1989 * allocation into 4GB chunks if the max protection is NONE. That
1990 * memory should never be accessible, so it will never get to the
1993 tmp_start
= tmp2_start
;
1994 if (object
== VM_OBJECT_NULL
&&
1995 size
> (vm_map_size_t
)ANON_CHUNK_SIZE
&&
1996 max_protection
!= VM_PROT_NONE
&&
1997 superpage_size
== 0)
1998 tmp_end
= tmp_start
+ (vm_map_size_t
)ANON_CHUNK_SIZE
;
2002 new_entry
= vm_map_entry_insert(map
, entry
, tmp_start
, tmp_end
,
2003 object
, offset
, needs_copy
,
2005 cur_protection
, max_protection
,
2006 VM_BEHAVIOR_DEFAULT
,
2007 inheritance
, 0, no_cache
,
2008 permanent
, superpage_size
);
2009 new_entry
->alias
= alias
;
2012 boolean_t submap_is_64bit
;
2015 new_entry
->is_sub_map
= TRUE
;
2016 submap
= (vm_map_t
) object
;
2017 submap_is_64bit
= vm_map_is_64bit(submap
);
2018 use_pmap
= (alias
== VM_MEMORY_SHARED_PMAP
);
2019 #ifndef NO_NESTED_PMAP
2020 if (use_pmap
&& submap
->pmap
== NULL
) {
2021 /* we need a sub pmap to nest... */
2022 submap
->pmap
= pmap_create(0, submap_is_64bit
);
2023 if (submap
->pmap
== NULL
) {
2024 /* let's proceed without nesting... */
2027 if (use_pmap
&& submap
->pmap
!= NULL
) {
2028 kr
= pmap_nest(map
->pmap
,
2032 tmp_end
- tmp_start
);
2033 if (kr
!= KERN_SUCCESS
) {
2034 printf("vm_map_enter: "
2035 "pmap_nest(0x%llx,0x%llx) "
2037 (long long)tmp_start
,
2041 /* we're now nested ! */
2042 new_entry
->use_pmap
= TRUE
;
2046 #endif /* NO_NESTED_PMAP */
2050 if (superpage_size
) {
2052 vm_object_t sp_object
;
2056 /* allocate one superpage */
2057 kr
= cpm_allocate(SUPERPAGE_SIZE
, &pages
, 0, SUPERPAGE_NBASEPAGES
-1, TRUE
, 0);
2058 if (kr
!= KERN_SUCCESS
) {
2059 new_mapping_established
= TRUE
; /* will cause deallocation of whole range */
2063 /* create one vm_object per superpage */
2064 sp_object
= vm_object_allocate((vm_map_size_t
)(entry
->vme_end
- entry
->vme_start
));
2065 sp_object
->phys_contiguous
= TRUE
;
2066 sp_object
->shadow_offset
= (vm_object_offset_t
)pages
->phys_page
*PAGE_SIZE
;
2067 entry
->object
.vm_object
= sp_object
;
2069 /* enter the base pages into the object */
2070 vm_object_lock(sp_object
);
2071 for (offset
= 0; offset
< SUPERPAGE_SIZE
; offset
+= PAGE_SIZE
) {
2073 pmap_zero_page(m
->phys_page
);
2074 pages
= NEXT_PAGE(m
);
2075 *(NEXT_PAGE_PTR(m
)) = VM_PAGE_NULL
;
2076 vm_page_insert(m
, sp_object
, offset
);
2078 vm_object_unlock(sp_object
);
2080 } while (tmp_end
!= tmp2_end
&&
2081 (tmp_start
= tmp_end
) &&
2082 (tmp_end
= (tmp2_end
- tmp_end
> (vm_map_size_t
)ANON_CHUNK_SIZE
) ?
2083 tmp_end
+ (vm_map_size_t
)ANON_CHUNK_SIZE
: tmp2_end
));
2089 new_mapping_established
= TRUE
;
2091 /* Wire down the new entry if the user
2092 * requested all new map entries be wired.
2094 if ((map
->wiring_required
)||(superpage_size
)) {
2095 pmap_empty
= FALSE
; /* pmap won't be empty */
2096 result
= vm_map_wire(map
, start
, end
,
2097 new_entry
->protection
, TRUE
);
2101 if ((object
!= VM_OBJECT_NULL
) &&
2102 (vm_map_pmap_enter_enable
) &&
2105 (size
< (128*1024))) {
2106 pmap_empty
= FALSE
; /* pmap won't be empty */
2108 if (override_nx(map
, alias
) && cur_protection
)
2109 cur_protection
|= VM_PROT_EXECUTE
;
2111 vm_map_pmap_enter(map
, start
, end
,
2112 object
, offset
, cur_protection
);
2116 if (result
== KERN_SUCCESS
) {
2117 vm_prot_t pager_prot
;
2118 memory_object_t pager
;
2121 !(flags
& VM_FLAGS_NO_PMAP_CHECK
)) {
2122 assert(vm_map_pmap_is_empty(map
,
2128 * For "named" VM objects, let the pager know that the
2129 * memory object is being mapped. Some pagers need to keep
2130 * track of this, to know when they can reclaim the memory
2131 * object, for example.
2132 * VM calls memory_object_map() for each mapping (specifying
2133 * the protection of each mapping) and calls
2134 * memory_object_last_unmap() when all the mappings are gone.
2136 pager_prot
= max_protection
;
2139 * Copy-On-Write mapping: won't modify
2140 * the memory object.
2142 pager_prot
&= ~VM_PROT_WRITE
;
2145 object
!= VM_OBJECT_NULL
&&
2147 object
->pager
!= MEMORY_OBJECT_NULL
) {
2148 vm_object_lock(object
);
2149 pager
= object
->pager
;
2150 if (object
->named
&&
2151 pager
!= MEMORY_OBJECT_NULL
) {
2152 assert(object
->pager_ready
);
2153 vm_object_mapping_wait(object
, THREAD_UNINT
);
2154 vm_object_mapping_begin(object
);
2155 vm_object_unlock(object
);
2157 kr
= memory_object_map(pager
, pager_prot
);
2158 assert(kr
== KERN_SUCCESS
);
2160 vm_object_lock(object
);
2161 vm_object_mapping_end(object
);
2163 vm_object_unlock(object
);
2166 if (new_mapping_established
) {
2168 * We have to get rid of the new mappings since we
2169 * won't make them available to the user.
2170 * Try and do that atomically, to minimize the risk
2171 * that someone else create new mappings that range.
2173 zap_new_map
= vm_map_create(PMAP_NULL
,
2176 map
->hdr
.entries_pageable
);
2181 (void) vm_map_delete(map
, *address
, *address
+size
,
2182 VM_MAP_REMOVE_SAVE_ENTRIES
,
2185 if (zap_old_map
!= VM_MAP_NULL
&&
2186 zap_old_map
->hdr
.nentries
!= 0) {
2187 vm_map_entry_t entry1
, entry2
;
2190 * The new mapping failed. Attempt to restore
2191 * the old mappings, saved in the "zap_old_map".
2198 /* first check if the coast is still clear */
2199 start
= vm_map_first_entry(zap_old_map
)->vme_start
;
2200 end
= vm_map_last_entry(zap_old_map
)->vme_end
;
2201 if (vm_map_lookup_entry(map
, start
, &entry1
) ||
2202 vm_map_lookup_entry(map
, end
, &entry2
) ||
2205 * Part of that range has already been
2206 * re-mapped: we can't restore the old
2209 vm_map_enter_restore_failures
++;
2212 * Transfer the saved map entries from
2213 * "zap_old_map" to the original "map",
2214 * inserting them all after "entry1".
2216 for (entry2
= vm_map_first_entry(zap_old_map
);
2217 entry2
!= vm_map_to_entry(zap_old_map
);
2218 entry2
= vm_map_first_entry(zap_old_map
)) {
2219 vm_map_size_t entry_size
;
2221 entry_size
= (entry2
->vme_end
-
2223 vm_map_entry_unlink(zap_old_map
,
2225 zap_old_map
->size
-= entry_size
;
2226 vm_map_entry_link(map
, entry1
, entry2
);
2227 map
->size
+= entry_size
;
2230 if (map
->wiring_required
) {
2232 * XXX TODO: we should rewire the
2236 vm_map_enter_restore_successes
++;
2246 * Get rid of the "zap_maps" and all the map entries that
2247 * they may still contain.
2249 if (zap_old_map
!= VM_MAP_NULL
) {
2250 vm_map_destroy(zap_old_map
, VM_MAP_REMOVE_NO_PMAP_CLEANUP
);
2251 zap_old_map
= VM_MAP_NULL
;
2253 if (zap_new_map
!= VM_MAP_NULL
) {
2254 vm_map_destroy(zap_new_map
, VM_MAP_REMOVE_NO_PMAP_CLEANUP
);
2255 zap_new_map
= VM_MAP_NULL
;
2264 vm_map_enter_mem_object(
2265 vm_map_t target_map
,
2266 vm_map_offset_t
*address
,
2267 vm_map_size_t initial_size
,
2268 vm_map_offset_t mask
,
2271 vm_object_offset_t offset
,
2273 vm_prot_t cur_protection
,
2274 vm_prot_t max_protection
,
2275 vm_inherit_t inheritance
)
2277 vm_map_address_t map_addr
;
2278 vm_map_size_t map_size
;
2280 vm_object_size_t size
;
2281 kern_return_t result
;
2284 * Check arguments for validity
2286 if ((target_map
== VM_MAP_NULL
) ||
2287 (cur_protection
& ~VM_PROT_ALL
) ||
2288 (max_protection
& ~VM_PROT_ALL
) ||
2289 (inheritance
> VM_INHERIT_LAST_VALID
) ||
2291 return KERN_INVALID_ARGUMENT
;
2293 map_addr
= vm_map_trunc_page(*address
);
2294 map_size
= vm_map_round_page(initial_size
);
2295 size
= vm_object_round_page(initial_size
);
2298 * Find the vm object (if any) corresponding to this port.
2300 if (!IP_VALID(port
)) {
2301 object
= VM_OBJECT_NULL
;
2304 } else if (ip_kotype(port
) == IKOT_NAMED_ENTRY
) {
2305 vm_named_entry_t named_entry
;
2307 named_entry
= (vm_named_entry_t
) port
->ip_kobject
;
2308 /* a few checks to make sure user is obeying rules */
2310 if (offset
>= named_entry
->size
)
2311 return KERN_INVALID_RIGHT
;
2312 size
= named_entry
->size
- offset
;
2314 if ((named_entry
->protection
& max_protection
) !=
2316 return KERN_INVALID_RIGHT
;
2317 if ((named_entry
->protection
& cur_protection
) !=
2319 return KERN_INVALID_RIGHT
;
2320 if (named_entry
->size
< (offset
+ size
))
2321 return KERN_INVALID_ARGUMENT
;
2323 /* the callers parameter offset is defined to be the */
2324 /* offset from beginning of named entry offset in object */
2325 offset
= offset
+ named_entry
->offset
;
2327 named_entry_lock(named_entry
);
2328 if (named_entry
->is_sub_map
) {
2331 submap
= named_entry
->backing
.map
;
2332 vm_map_lock(submap
);
2333 vm_map_reference(submap
);
2334 vm_map_unlock(submap
);
2335 named_entry_unlock(named_entry
);
2337 result
= vm_map_enter(target_map
,
2341 flags
| VM_FLAGS_SUBMAP
,
2342 (vm_object_t
) submap
,
2348 if (result
!= KERN_SUCCESS
) {
2349 vm_map_deallocate(submap
);
2352 * No need to lock "submap" just to check its
2353 * "mapped" flag: that flag is never reset
2354 * once it's been set and if we race, we'll
2355 * just end up setting it twice, which is OK.
2357 if (submap
->mapped
== FALSE
) {
2359 * This submap has never been mapped.
2360 * Set its "mapped" flag now that it
2362 * This happens only for the first ever
2363 * mapping of a "submap".
2365 vm_map_lock(submap
);
2366 submap
->mapped
= TRUE
;
2367 vm_map_unlock(submap
);
2369 *address
= map_addr
;
2373 } else if (named_entry
->is_pager
) {
2374 unsigned int access
;
2375 vm_prot_t protections
;
2376 unsigned int wimg_mode
;
2377 boolean_t cache_attr
;
2379 protections
= named_entry
->protection
& VM_PROT_ALL
;
2380 access
= GET_MAP_MEM(named_entry
->protection
);
2382 object
= vm_object_enter(named_entry
->backing
.pager
,
2384 named_entry
->internal
,
2387 if (object
== VM_OBJECT_NULL
) {
2388 named_entry_unlock(named_entry
);
2389 return KERN_INVALID_OBJECT
;
2392 /* JMM - drop reference on pager here */
2394 /* create an extra ref for the named entry */
2395 vm_object_lock(object
);
2396 vm_object_reference_locked(object
);
2397 named_entry
->backing
.object
= object
;
2398 named_entry
->is_pager
= FALSE
;
2399 named_entry_unlock(named_entry
);
2401 wimg_mode
= object
->wimg_bits
;
2402 if (access
== MAP_MEM_IO
) {
2403 wimg_mode
= VM_WIMG_IO
;
2404 } else if (access
== MAP_MEM_COPYBACK
) {
2405 wimg_mode
= VM_WIMG_USE_DEFAULT
;
2406 } else if (access
== MAP_MEM_WTHRU
) {
2407 wimg_mode
= VM_WIMG_WTHRU
;
2408 } else if (access
== MAP_MEM_WCOMB
) {
2409 wimg_mode
= VM_WIMG_WCOMB
;
2411 if (wimg_mode
== VM_WIMG_IO
||
2412 wimg_mode
== VM_WIMG_WCOMB
)
2417 /* wait for object (if any) to be ready */
2418 if (!named_entry
->internal
) {
2419 while (!object
->pager_ready
) {
2422 VM_OBJECT_EVENT_PAGER_READY
,
2424 vm_object_lock(object
);
2428 if (object
->wimg_bits
!= wimg_mode
) {
2431 vm_object_paging_wait(object
, THREAD_UNINT
);
2433 object
->wimg_bits
= wimg_mode
;
2434 queue_iterate(&object
->memq
, p
, vm_page_t
, listq
) {
2435 if (!p
->fictitious
) {
2437 pmap_disconnect(p
->phys_page
);
2439 pmap_sync_page_attributes_phys(p
->phys_page
);
2443 object
->true_share
= TRUE
;
2444 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
)
2445 object
->copy_strategy
= MEMORY_OBJECT_COPY_DELAY
;
2446 vm_object_unlock(object
);
2448 /* This is the case where we are going to map */
2449 /* an already mapped object. If the object is */
2450 /* not ready it is internal. An external */
2451 /* object cannot be mapped until it is ready */
2452 /* we can therefore avoid the ready check */
2454 object
= named_entry
->backing
.object
;
2455 assert(object
!= VM_OBJECT_NULL
);
2456 named_entry_unlock(named_entry
);
2457 vm_object_reference(object
);
2459 } else if (ip_kotype(port
) == IKOT_MEMORY_OBJECT
) {
2461 * JMM - This is temporary until we unify named entries
2462 * and raw memory objects.
2464 * Detected fake ip_kotype for a memory object. In
2465 * this case, the port isn't really a port at all, but
2466 * instead is just a raw memory object.
2469 object
= vm_object_enter((memory_object_t
)port
,
2470 size
, FALSE
, FALSE
, FALSE
);
2471 if (object
== VM_OBJECT_NULL
)
2472 return KERN_INVALID_OBJECT
;
2474 /* wait for object (if any) to be ready */
2475 if (object
!= VM_OBJECT_NULL
) {
2476 if (object
== kernel_object
) {
2477 printf("Warning: Attempt to map kernel object"
2478 " by a non-private kernel entity\n");
2479 return KERN_INVALID_OBJECT
;
2481 if (!object
->pager_ready
) {
2482 vm_object_lock(object
);
2484 while (!object
->pager_ready
) {
2485 vm_object_wait(object
,
2486 VM_OBJECT_EVENT_PAGER_READY
,
2488 vm_object_lock(object
);
2490 vm_object_unlock(object
);
2494 return KERN_INVALID_OBJECT
;
2497 if (object
!= VM_OBJECT_NULL
&&
2499 object
->pager
!= MEMORY_OBJECT_NULL
&&
2500 object
->copy_strategy
!= MEMORY_OBJECT_COPY_NONE
) {
2501 memory_object_t pager
;
2502 vm_prot_t pager_prot
;
2506 * For "named" VM objects, let the pager know that the
2507 * memory object is being mapped. Some pagers need to keep
2508 * track of this, to know when they can reclaim the memory
2509 * object, for example.
2510 * VM calls memory_object_map() for each mapping (specifying
2511 * the protection of each mapping) and calls
2512 * memory_object_last_unmap() when all the mappings are gone.
2514 pager_prot
= max_protection
;
2517 * Copy-On-Write mapping: won't modify the
2520 pager_prot
&= ~VM_PROT_WRITE
;
2522 vm_object_lock(object
);
2523 pager
= object
->pager
;
2524 if (object
->named
&&
2525 pager
!= MEMORY_OBJECT_NULL
&&
2526 object
->copy_strategy
!= MEMORY_OBJECT_COPY_NONE
) {
2527 assert(object
->pager_ready
);
2528 vm_object_mapping_wait(object
, THREAD_UNINT
);
2529 vm_object_mapping_begin(object
);
2530 vm_object_unlock(object
);
2532 kr
= memory_object_map(pager
, pager_prot
);
2533 assert(kr
== KERN_SUCCESS
);
2535 vm_object_lock(object
);
2536 vm_object_mapping_end(object
);
2538 vm_object_unlock(object
);
2542 * Perform the copy if requested
2546 vm_object_t new_object
;
2547 vm_object_offset_t new_offset
;
2549 result
= vm_object_copy_strategically(object
, offset
, size
,
2550 &new_object
, &new_offset
,
2554 if (result
== KERN_MEMORY_RESTART_COPY
) {
2556 boolean_t src_needs_copy
;
2560 * We currently ignore src_needs_copy.
2561 * This really is the issue of how to make
2562 * MEMORY_OBJECT_COPY_SYMMETRIC safe for
2563 * non-kernel users to use. Solution forthcoming.
2564 * In the meantime, since we don't allow non-kernel
2565 * memory managers to specify symmetric copy,
2566 * we won't run into problems here.
2568 new_object
= object
;
2569 new_offset
= offset
;
2570 success
= vm_object_copy_quickly(&new_object
,
2575 result
= KERN_SUCCESS
;
2578 * Throw away the reference to the
2579 * original object, as it won't be mapped.
2582 vm_object_deallocate(object
);
2584 if (result
!= KERN_SUCCESS
)
2587 object
= new_object
;
2588 offset
= new_offset
;
2591 result
= vm_map_enter(target_map
,
2592 &map_addr
, map_size
,
2593 (vm_map_offset_t
)mask
,
2597 cur_protection
, max_protection
, inheritance
);
2598 if (result
!= KERN_SUCCESS
)
2599 vm_object_deallocate(object
);
2600 *address
= map_addr
;
2608 vm_map_enter_mem_object_control(
2609 vm_map_t target_map
,
2610 vm_map_offset_t
*address
,
2611 vm_map_size_t initial_size
,
2612 vm_map_offset_t mask
,
2614 memory_object_control_t control
,
2615 vm_object_offset_t offset
,
2617 vm_prot_t cur_protection
,
2618 vm_prot_t max_protection
,
2619 vm_inherit_t inheritance
)
2621 vm_map_address_t map_addr
;
2622 vm_map_size_t map_size
;
2624 vm_object_size_t size
;
2625 kern_return_t result
;
2626 memory_object_t pager
;
2627 vm_prot_t pager_prot
;
2631 * Check arguments for validity
2633 if ((target_map
== VM_MAP_NULL
) ||
2634 (cur_protection
& ~VM_PROT_ALL
) ||
2635 (max_protection
& ~VM_PROT_ALL
) ||
2636 (inheritance
> VM_INHERIT_LAST_VALID
) ||
2638 return KERN_INVALID_ARGUMENT
;
2640 map_addr
= vm_map_trunc_page(*address
);
2641 map_size
= vm_map_round_page(initial_size
);
2642 size
= vm_object_round_page(initial_size
);
2644 object
= memory_object_control_to_vm_object(control
);
2646 if (object
== VM_OBJECT_NULL
)
2647 return KERN_INVALID_OBJECT
;
2649 if (object
== kernel_object
) {
2650 printf("Warning: Attempt to map kernel object"
2651 " by a non-private kernel entity\n");
2652 return KERN_INVALID_OBJECT
;
2655 vm_object_lock(object
);
2656 object
->ref_count
++;
2657 vm_object_res_reference(object
);
2660 * For "named" VM objects, let the pager know that the
2661 * memory object is being mapped. Some pagers need to keep
2662 * track of this, to know when they can reclaim the memory
2663 * object, for example.
2664 * VM calls memory_object_map() for each mapping (specifying
2665 * the protection of each mapping) and calls
2666 * memory_object_last_unmap() when all the mappings are gone.
2668 pager_prot
= max_protection
;
2670 pager_prot
&= ~VM_PROT_WRITE
;
2672 pager
= object
->pager
;
2673 if (object
->named
&&
2674 pager
!= MEMORY_OBJECT_NULL
&&
2675 object
->copy_strategy
!= MEMORY_OBJECT_COPY_NONE
) {
2676 assert(object
->pager_ready
);
2677 vm_object_mapping_wait(object
, THREAD_UNINT
);
2678 vm_object_mapping_begin(object
);
2679 vm_object_unlock(object
);
2681 kr
= memory_object_map(pager
, pager_prot
);
2682 assert(kr
== KERN_SUCCESS
);
2684 vm_object_lock(object
);
2685 vm_object_mapping_end(object
);
2687 vm_object_unlock(object
);
2690 * Perform the copy if requested
2694 vm_object_t new_object
;
2695 vm_object_offset_t new_offset
;
2697 result
= vm_object_copy_strategically(object
, offset
, size
,
2698 &new_object
, &new_offset
,
2702 if (result
== KERN_MEMORY_RESTART_COPY
) {
2704 boolean_t src_needs_copy
;
2708 * We currently ignore src_needs_copy.
2709 * This really is the issue of how to make
2710 * MEMORY_OBJECT_COPY_SYMMETRIC safe for
2711 * non-kernel users to use. Solution forthcoming.
2712 * In the meantime, since we don't allow non-kernel
2713 * memory managers to specify symmetric copy,
2714 * we won't run into problems here.
2716 new_object
= object
;
2717 new_offset
= offset
;
2718 success
= vm_object_copy_quickly(&new_object
,
2723 result
= KERN_SUCCESS
;
2726 * Throw away the reference to the
2727 * original object, as it won't be mapped.
2730 vm_object_deallocate(object
);
2732 if (result
!= KERN_SUCCESS
)
2735 object
= new_object
;
2736 offset
= new_offset
;
2739 result
= vm_map_enter(target_map
,
2740 &map_addr
, map_size
,
2741 (vm_map_offset_t
)mask
,
2745 cur_protection
, max_protection
, inheritance
);
2746 if (result
!= KERN_SUCCESS
)
2747 vm_object_deallocate(object
);
2748 *address
= map_addr
;
2757 extern pmap_paddr_t avail_start
, avail_end
;
2761 * Allocate memory in the specified map, with the caveat that
2762 * the memory is physically contiguous. This call may fail
2763 * if the system can't find sufficient contiguous memory.
2764 * This call may cause or lead to heart-stopping amounts of
2767 * Memory obtained from this call should be freed in the
2768 * normal way, viz., via vm_deallocate.
2773 vm_map_offset_t
*addr
,
2777 vm_object_t cpm_obj
;
2781 vm_map_offset_t va
, start
, end
, offset
;
2783 vm_map_offset_t prev_addr
;
2784 #endif /* MACH_ASSERT */
2786 boolean_t anywhere
= ((VM_FLAGS_ANYWHERE
& flags
) != 0);
2788 if (!vm_allocate_cpm_enabled
)
2789 return KERN_FAILURE
;
2793 return KERN_SUCCESS
;
2796 *addr
= vm_map_min(map
);
2798 *addr
= vm_map_trunc_page(*addr
);
2799 size
= vm_map_round_page(size
);
2802 * LP64todo - cpm_allocate should probably allow
2803 * allocations of >4GB, but not with the current
2804 * algorithm, so just cast down the size for now.
2806 if (size
> VM_MAX_ADDRESS
)
2807 return KERN_RESOURCE_SHORTAGE
;
2808 if ((kr
= cpm_allocate(CAST_DOWN(vm_size_t
, size
),
2809 &pages
, 0, 0, TRUE
, flags
)) != KERN_SUCCESS
)
2812 cpm_obj
= vm_object_allocate((vm_object_size_t
)size
);
2813 assert(cpm_obj
!= VM_OBJECT_NULL
);
2814 assert(cpm_obj
->internal
);
2815 assert(cpm_obj
->size
== (vm_object_size_t
)size
);
2816 assert(cpm_obj
->can_persist
== FALSE
);
2817 assert(cpm_obj
->pager_created
== FALSE
);
2818 assert(cpm_obj
->pageout
== FALSE
);
2819 assert(cpm_obj
->shadow
== VM_OBJECT_NULL
);
2822 * Insert pages into object.
2825 vm_object_lock(cpm_obj
);
2826 for (offset
= 0; offset
< size
; offset
+= PAGE_SIZE
) {
2828 pages
= NEXT_PAGE(m
);
2829 *(NEXT_PAGE_PTR(m
)) = VM_PAGE_NULL
;
2831 assert(!m
->gobbled
);
2833 assert(!m
->pageout
);
2835 assert(VM_PAGE_WIRED(m
));
2838 * "m" is not supposed to be pageable, so it
2839 * should not be encrypted. It wouldn't be safe
2840 * to enter it in a new VM object while encrypted.
2842 ASSERT_PAGE_DECRYPTED(m
);
2844 assert(m
->phys_page
>=(avail_start
>>PAGE_SHIFT
) && m
->phys_page
<=(avail_end
>>PAGE_SHIFT
));
2847 vm_page_insert(m
, cpm_obj
, offset
);
2849 assert(cpm_obj
->resident_page_count
== size
/ PAGE_SIZE
);
2850 vm_object_unlock(cpm_obj
);
2853 * Hang onto a reference on the object in case a
2854 * multi-threaded application for some reason decides
2855 * to deallocate the portion of the address space into
2856 * which we will insert this object.
2858 * Unfortunately, we must insert the object now before
2859 * we can talk to the pmap module about which addresses
2860 * must be wired down. Hence, the race with a multi-
2863 vm_object_reference(cpm_obj
);
2866 * Insert object into map.
2876 (vm_object_offset_t
)0,
2880 VM_INHERIT_DEFAULT
);
2882 if (kr
!= KERN_SUCCESS
) {
2884 * A CPM object doesn't have can_persist set,
2885 * so all we have to do is deallocate it to
2886 * free up these pages.
2888 assert(cpm_obj
->pager_created
== FALSE
);
2889 assert(cpm_obj
->can_persist
== FALSE
);
2890 assert(cpm_obj
->pageout
== FALSE
);
2891 assert(cpm_obj
->shadow
== VM_OBJECT_NULL
);
2892 vm_object_deallocate(cpm_obj
); /* kill acquired ref */
2893 vm_object_deallocate(cpm_obj
); /* kill creation ref */
2897 * Inform the physical mapping system that the
2898 * range of addresses may not fault, so that
2899 * page tables and such can be locked down as well.
2903 pmap
= vm_map_pmap(map
);
2904 pmap_pageable(pmap
, start
, end
, FALSE
);
2907 * Enter each page into the pmap, to avoid faults.
2908 * Note that this loop could be coded more efficiently,
2909 * if the need arose, rather than looking up each page
2912 for (offset
= 0, va
= start
; offset
< size
;
2913 va
+= PAGE_SIZE
, offset
+= PAGE_SIZE
) {
2916 vm_object_lock(cpm_obj
);
2917 m
= vm_page_lookup(cpm_obj
, (vm_object_offset_t
)offset
);
2918 assert(m
!= VM_PAGE_NULL
);
2920 vm_page_zero_fill(m
);
2922 type_of_fault
= DBG_ZERO_FILL_FAULT
;
2924 vm_fault_enter(m
, pmap
, va
, VM_PROT_ALL
,
2925 VM_PAGE_WIRED(m
), FALSE
, FALSE
,
2928 vm_object_unlock(cpm_obj
);
2933 * Verify ordering in address space.
2935 for (offset
= 0; offset
< size
; offset
+= PAGE_SIZE
) {
2936 vm_object_lock(cpm_obj
);
2937 m
= vm_page_lookup(cpm_obj
, (vm_object_offset_t
)offset
);
2938 vm_object_unlock(cpm_obj
);
2939 if (m
== VM_PAGE_NULL
)
2940 panic("vm_allocate_cpm: obj 0x%x off 0x%x no page",
2945 assert(!m
->fictitious
);
2946 assert(!m
->private);
2949 assert(!m
->cleaning
);
2950 assert(!m
->precious
);
2951 assert(!m
->clustered
);
2953 if (m
->phys_page
!= prev_addr
+ 1) {
2954 printf("start 0x%x end 0x%x va 0x%x\n",
2956 printf("obj 0x%x off 0x%x\n", cpm_obj
, offset
);
2957 printf("m 0x%x prev_address 0x%x\n", m
,
2959 panic("vm_allocate_cpm: pages not contig!");
2962 prev_addr
= m
->phys_page
;
2964 #endif /* MACH_ASSERT */
2966 vm_object_deallocate(cpm_obj
); /* kill extra ref */
2975 * Interface is defined in all cases, but unless the kernel
2976 * is built explicitly for this option, the interface does
2982 __unused vm_map_t map
,
2983 __unused vm_map_offset_t
*addr
,
2984 __unused vm_map_size_t size
,
2987 return KERN_FAILURE
;
2991 /* Not used without nested pmaps */
2992 #ifndef NO_NESTED_PMAP
2994 * Clip and unnest a portion of a nested submap mapping.
3001 vm_map_entry_t entry
,
3002 vm_map_offset_t start_unnest
,
3003 vm_map_offset_t end_unnest
)
3005 vm_map_offset_t old_start_unnest
= start_unnest
;
3006 vm_map_offset_t old_end_unnest
= end_unnest
;
3008 assert(entry
->is_sub_map
);
3009 assert(entry
->object
.sub_map
!= NULL
);
3012 * Query the platform for the optimal unnest range.
3013 * DRK: There's some duplication of effort here, since
3014 * callers may have adjusted the range to some extent. This
3015 * routine was introduced to support 1GiB subtree nesting
3016 * for x86 platforms, which can also nest on 2MiB boundaries
3017 * depending on size/alignment.
3019 if (pmap_adjust_unnest_parameters(map
->pmap
, &start_unnest
, &end_unnest
)) {
3020 log_unnest_badness(map
, old_start_unnest
, old_end_unnest
);
3023 if (entry
->vme_start
> start_unnest
||
3024 entry
->vme_end
< end_unnest
) {
3025 panic("vm_map_clip_unnest(0x%llx,0x%llx): "
3026 "bad nested entry: start=0x%llx end=0x%llx\n",
3027 (long long)start_unnest
, (long long)end_unnest
,
3028 (long long)entry
->vme_start
, (long long)entry
->vme_end
);
3031 if (start_unnest
> entry
->vme_start
) {
3032 _vm_map_clip_start(&map
->hdr
,
3035 UPDATE_FIRST_FREE(map
, map
->first_free
);
3037 if (entry
->vme_end
> end_unnest
) {
3038 _vm_map_clip_end(&map
->hdr
,
3041 UPDATE_FIRST_FREE(map
, map
->first_free
);
3044 pmap_unnest(map
->pmap
,
3046 entry
->vme_end
- entry
->vme_start
);
3047 if ((map
->mapped
) && (map
->ref_count
)) {
3048 /* clean up parent map/maps */
3049 vm_map_submap_pmap_clean(
3050 map
, entry
->vme_start
,
3052 entry
->object
.sub_map
,
3055 entry
->use_pmap
= FALSE
;
3057 #endif /* NO_NESTED_PMAP */
3060 * vm_map_clip_start: [ internal use only ]
3062 * Asserts that the given entry begins at or after
3063 * the specified address; if necessary,
3064 * it splits the entry into two.
3069 vm_map_entry_t entry
,
3070 vm_map_offset_t startaddr
)
3072 #ifndef NO_NESTED_PMAP
3073 if (entry
->use_pmap
&&
3074 startaddr
>= entry
->vme_start
) {
3075 vm_map_offset_t start_unnest
, end_unnest
;
3078 * Make sure "startaddr" is no longer in a nested range
3079 * before we clip. Unnest only the minimum range the platform
3081 * vm_map_clip_unnest may perform additional adjustments to
3084 start_unnest
= startaddr
& ~(pmap_nesting_size_min
- 1);
3085 end_unnest
= start_unnest
+ pmap_nesting_size_min
;
3086 vm_map_clip_unnest(map
, entry
, start_unnest
, end_unnest
);
3088 #endif /* NO_NESTED_PMAP */
3089 if (startaddr
> entry
->vme_start
) {
3090 if (entry
->object
.vm_object
&&
3091 !entry
->is_sub_map
&&
3092 entry
->object
.vm_object
->phys_contiguous
) {
3093 pmap_remove(map
->pmap
,
3094 (addr64_t
)(entry
->vme_start
),
3095 (addr64_t
)(entry
->vme_end
));
3097 _vm_map_clip_start(&map
->hdr
, entry
, startaddr
);
3098 UPDATE_FIRST_FREE(map
, map
->first_free
);
3103 #define vm_map_copy_clip_start(copy, entry, startaddr) \
3105 if ((startaddr) > (entry)->vme_start) \
3106 _vm_map_clip_start(&(copy)->cpy_hdr,(entry),(startaddr)); \
3110 * This routine is called only when it is known that
3111 * the entry must be split.
3115 register struct vm_map_header
*map_header
,
3116 register vm_map_entry_t entry
,
3117 register vm_map_offset_t start
)
3119 register vm_map_entry_t new_entry
;
3122 * Split off the front portion --
3123 * note that we must insert the new
3124 * entry BEFORE this one, so that
3125 * this entry has the specified starting
3129 new_entry
= _vm_map_entry_create(map_header
);
3130 vm_map_entry_copy_full(new_entry
, entry
);
3132 new_entry
->vme_end
= start
;
3133 entry
->offset
+= (start
- entry
->vme_start
);
3134 entry
->vme_start
= start
;
3136 _vm_map_entry_link(map_header
, entry
->vme_prev
, new_entry
);
3138 if (entry
->is_sub_map
)
3139 vm_map_reference(new_entry
->object
.sub_map
);
3141 vm_object_reference(new_entry
->object
.vm_object
);
3146 * vm_map_clip_end: [ internal use only ]
3148 * Asserts that the given entry ends at or before
3149 * the specified address; if necessary,
3150 * it splits the entry into two.
3155 vm_map_entry_t entry
,
3156 vm_map_offset_t endaddr
)
3158 if (endaddr
> entry
->vme_end
) {
3160 * Within the scope of this clipping, limit "endaddr" to
3161 * the end of this map entry...
3163 endaddr
= entry
->vme_end
;
3165 #ifndef NO_NESTED_PMAP
3166 if (entry
->use_pmap
) {
3167 vm_map_offset_t start_unnest
, end_unnest
;
3170 * Make sure the range between the start of this entry and
3171 * the new "endaddr" is no longer nested before we clip.
3172 * Unnest only the minimum range the platform can handle.
3173 * vm_map_clip_unnest may perform additional adjustments to
3176 start_unnest
= entry
->vme_start
;
3178 (endaddr
+ pmap_nesting_size_min
- 1) &
3179 ~(pmap_nesting_size_min
- 1);
3180 vm_map_clip_unnest(map
, entry
, start_unnest
, end_unnest
);
3182 #endif /* NO_NESTED_PMAP */
3183 if (endaddr
< entry
->vme_end
) {
3184 if (entry
->object
.vm_object
&&
3185 !entry
->is_sub_map
&&
3186 entry
->object
.vm_object
->phys_contiguous
) {
3187 pmap_remove(map
->pmap
,
3188 (addr64_t
)(entry
->vme_start
),
3189 (addr64_t
)(entry
->vme_end
));
3191 _vm_map_clip_end(&map
->hdr
, entry
, endaddr
);
3192 UPDATE_FIRST_FREE(map
, map
->first_free
);
3197 #define vm_map_copy_clip_end(copy, entry, endaddr) \
3199 if ((endaddr) < (entry)->vme_end) \
3200 _vm_map_clip_end(&(copy)->cpy_hdr,(entry),(endaddr)); \
3204 * This routine is called only when it is known that
3205 * the entry must be split.
3209 register struct vm_map_header
*map_header
,
3210 register vm_map_entry_t entry
,
3211 register vm_map_offset_t end
)
3213 register vm_map_entry_t new_entry
;
3216 * Create a new entry and insert it
3217 * AFTER the specified entry
3220 new_entry
= _vm_map_entry_create(map_header
);
3221 vm_map_entry_copy_full(new_entry
, entry
);
3223 new_entry
->vme_start
= entry
->vme_end
= end
;
3224 new_entry
->offset
+= (end
- entry
->vme_start
);
3226 _vm_map_entry_link(map_header
, entry
, new_entry
);
3228 if (entry
->is_sub_map
)
3229 vm_map_reference(new_entry
->object
.sub_map
);
3231 vm_object_reference(new_entry
->object
.vm_object
);
3236 * VM_MAP_RANGE_CHECK: [ internal use only ]
3238 * Asserts that the starting and ending region
3239 * addresses fall within the valid range of the map.
3241 #define VM_MAP_RANGE_CHECK(map, start, end) \
3243 if (start < vm_map_min(map)) \
3244 start = vm_map_min(map); \
3245 if (end > vm_map_max(map)) \
3246 end = vm_map_max(map); \
3252 * vm_map_range_check: [ internal use only ]
3254 * Check that the region defined by the specified start and
3255 * end addresses are wholly contained within a single map
3256 * entry or set of adjacent map entries of the spacified map,
3257 * i.e. the specified region contains no unmapped space.
3258 * If any or all of the region is unmapped, FALSE is returned.
3259 * Otherwise, TRUE is returned and if the output argument 'entry'
3260 * is not NULL it points to the map entry containing the start
3263 * The map is locked for reading on entry and is left locked.
3267 register vm_map_t map
,
3268 register vm_map_offset_t start
,
3269 register vm_map_offset_t end
,
3270 vm_map_entry_t
*entry
)
3273 register vm_map_offset_t prev
;
3276 * Basic sanity checks first
3278 if (start
< vm_map_min(map
) || end
> vm_map_max(map
) || start
> end
)
3282 * Check first if the region starts within a valid
3283 * mapping for the map.
3285 if (!vm_map_lookup_entry(map
, start
, &cur
))
3289 * Optimize for the case that the region is contained
3290 * in a single map entry.
3292 if (entry
!= (vm_map_entry_t
*) NULL
)
3294 if (end
<= cur
->vme_end
)
3298 * If the region is not wholly contained within a
3299 * single entry, walk the entries looking for holes.
3301 prev
= cur
->vme_end
;
3302 cur
= cur
->vme_next
;
3303 while ((cur
!= vm_map_to_entry(map
)) && (prev
== cur
->vme_start
)) {
3304 if (end
<= cur
->vme_end
)
3306 prev
= cur
->vme_end
;
3307 cur
= cur
->vme_next
;
3313 * vm_map_submap: [ kernel use only ]
3315 * Mark the given range as handled by a subordinate map.
3317 * This range must have been created with vm_map_find using
3318 * the vm_submap_object, and no other operations may have been
3319 * performed on this range prior to calling vm_map_submap.
3321 * Only a limited number of operations can be performed
3322 * within this rage after calling vm_map_submap:
3324 * [Don't try vm_map_copyin!]
3326 * To remove a submapping, one must first remove the
3327 * range from the superior map, and then destroy the
3328 * submap (if desired). [Better yet, don't try it.]
3333 vm_map_offset_t start
,
3334 vm_map_offset_t end
,
3336 vm_map_offset_t offset
,
3337 #ifdef NO_NESTED_PMAP
3339 #endif /* NO_NESTED_PMAP */
3342 vm_map_entry_t entry
;
3343 register kern_return_t result
= KERN_INVALID_ARGUMENT
;
3344 register vm_object_t object
;
3348 if (! vm_map_lookup_entry(map
, start
, &entry
)) {
3349 entry
= entry
->vme_next
;
3352 if (entry
== vm_map_to_entry(map
) ||
3353 entry
->is_sub_map
) {
3355 return KERN_INVALID_ARGUMENT
;
3358 assert(!entry
->use_pmap
); /* we don't want to unnest anything here */
3359 vm_map_clip_start(map
, entry
, start
);
3360 vm_map_clip_end(map
, entry
, end
);
3362 if ((entry
->vme_start
== start
) && (entry
->vme_end
== end
) &&
3363 (!entry
->is_sub_map
) &&
3364 ((object
= entry
->object
.vm_object
) == vm_submap_object
) &&
3365 (object
->resident_page_count
== 0) &&
3366 (object
->copy
== VM_OBJECT_NULL
) &&
3367 (object
->shadow
== VM_OBJECT_NULL
) &&
3368 (!object
->pager_created
)) {
3369 entry
->offset
= (vm_object_offset_t
)offset
;
3370 entry
->object
.vm_object
= VM_OBJECT_NULL
;
3371 vm_object_deallocate(object
);
3372 entry
->is_sub_map
= TRUE
;
3373 entry
->object
.sub_map
= submap
;
3374 vm_map_reference(submap
);
3375 submap
->mapped
= TRUE
;
3377 #ifndef NO_NESTED_PMAP
3379 /* nest if platform code will allow */
3380 if(submap
->pmap
== NULL
) {
3381 submap
->pmap
= pmap_create((vm_map_size_t
) 0, FALSE
);
3382 if(submap
->pmap
== PMAP_NULL
) {
3384 return(KERN_NO_SPACE
);
3387 result
= pmap_nest(map
->pmap
,
3388 (entry
->object
.sub_map
)->pmap
,
3391 (uint64_t)(end
- start
));
3393 panic("vm_map_submap: pmap_nest failed, rc = %08X\n", result
);
3394 entry
->use_pmap
= TRUE
;
3396 #else /* NO_NESTED_PMAP */
3397 pmap_remove(map
->pmap
, (addr64_t
)start
, (addr64_t
)end
);
3398 #endif /* NO_NESTED_PMAP */
3399 result
= KERN_SUCCESS
;
3409 * Sets the protection of the specified address
3410 * region in the target map. If "set_max" is
3411 * specified, the maximum protection is to be set;
3412 * otherwise, only the current protection is affected.
3416 register vm_map_t map
,
3417 register vm_map_offset_t start
,
3418 register vm_map_offset_t end
,
3419 register vm_prot_t new_prot
,
3420 register boolean_t set_max
)
3422 register vm_map_entry_t current
;
3423 register vm_map_offset_t prev
;
3424 vm_map_entry_t entry
;
3428 "vm_map_protect, 0x%X start 0x%X end 0x%X, new 0x%X %d",
3429 map
, start
, end
, new_prot
, set_max
);
3433 /* LP64todo - remove this check when vm_map_commpage64()
3434 * no longer has to stuff in a map_entry for the commpage
3435 * above the map's max_offset.
3437 if (start
>= map
->max_offset
) {
3439 return(KERN_INVALID_ADDRESS
);
3444 * Lookup the entry. If it doesn't start in a valid
3445 * entry, return an error.
3447 if (! vm_map_lookup_entry(map
, start
, &entry
)) {
3449 return(KERN_INVALID_ADDRESS
);
3452 if (entry
->superpage_size
&& (start
& (SUPERPAGE_SIZE
-1))) { /* extend request to whole entry */
3453 start
= SUPERPAGE_ROUND_DOWN(start
);
3458 if (entry
->superpage_size
)
3459 end
= SUPERPAGE_ROUND_UP(end
);
3462 * Make a first pass to check for protection and address
3467 prev
= current
->vme_start
;
3468 while ((current
!= vm_map_to_entry(map
)) &&
3469 (current
->vme_start
< end
)) {
3472 * If there is a hole, return an error.
3474 if (current
->vme_start
!= prev
) {
3476 return(KERN_INVALID_ADDRESS
);
3479 new_max
= current
->max_protection
;
3480 if(new_prot
& VM_PROT_COPY
) {
3481 new_max
|= VM_PROT_WRITE
;
3482 if ((new_prot
& (new_max
| VM_PROT_COPY
)) != new_prot
) {
3484 return(KERN_PROTECTION_FAILURE
);
3487 if ((new_prot
& new_max
) != new_prot
) {
3489 return(KERN_PROTECTION_FAILURE
);
3494 if (new_prot
& VM_PROT_WRITE
) {
3495 if (new_prot
& VM_PROT_EXECUTE
) {
3496 printf("EMBEDDED: %s can't have both write and exec at the same time\n", __FUNCTION__
);
3497 new_prot
&= ~VM_PROT_EXECUTE
;
3502 prev
= current
->vme_end
;
3503 current
= current
->vme_next
;
3507 return(KERN_INVALID_ADDRESS
);
3511 * Go back and fix up protections.
3512 * Clip to start here if the range starts within
3517 if (current
!= vm_map_to_entry(map
)) {
3518 /* clip and unnest if necessary */
3519 vm_map_clip_start(map
, current
, start
);
3522 while ((current
!= vm_map_to_entry(map
)) &&
3523 (current
->vme_start
< end
)) {
3527 vm_map_clip_end(map
, current
, end
);
3529 assert(!current
->use_pmap
); /* clipping did unnest if needed */
3531 old_prot
= current
->protection
;
3533 if(new_prot
& VM_PROT_COPY
) {
3534 /* caller is asking specifically to copy the */
3535 /* mapped data, this implies that max protection */
3536 /* will include write. Caller must be prepared */
3537 /* for loss of shared memory communication in the */
3538 /* target area after taking this step */
3539 current
->needs_copy
= TRUE
;
3540 current
->max_protection
|= VM_PROT_WRITE
;
3544 current
->protection
=
3545 (current
->max_protection
=
3546 new_prot
& ~VM_PROT_COPY
) &
3549 current
->protection
= new_prot
& ~VM_PROT_COPY
;
3552 * Update physical map if necessary.
3553 * If the request is to turn off write protection,
3554 * we won't do it for real (in pmap). This is because
3555 * it would cause copy-on-write to fail. We've already
3556 * set, the new protection in the map, so if a
3557 * write-protect fault occurred, it will be fixed up
3558 * properly, COW or not.
3560 if (current
->protection
!= old_prot
) {
3561 /* Look one level in we support nested pmaps */
3562 /* from mapped submaps which are direct entries */
3567 prot
= current
->protection
& ~VM_PROT_WRITE
;
3569 if (override_nx(map
, current
->alias
) && prot
)
3570 prot
|= VM_PROT_EXECUTE
;
3572 if (current
->is_sub_map
&& current
->use_pmap
) {
3573 pmap_protect(current
->object
.sub_map
->pmap
,
3578 pmap_protect(map
->pmap
,
3584 current
= current
->vme_next
;
3588 while ((current
!= vm_map_to_entry(map
)) &&
3589 (current
->vme_start
<= end
)) {
3590 vm_map_simplify_entry(map
, current
);
3591 current
= current
->vme_next
;
3595 return(KERN_SUCCESS
);
3601 * Sets the inheritance of the specified address
3602 * range in the target map. Inheritance
3603 * affects how the map will be shared with
3604 * child maps at the time of vm_map_fork.
3608 register vm_map_t map
,
3609 register vm_map_offset_t start
,
3610 register vm_map_offset_t end
,
3611 register vm_inherit_t new_inheritance
)
3613 register vm_map_entry_t entry
;
3614 vm_map_entry_t temp_entry
;
3618 VM_MAP_RANGE_CHECK(map
, start
, end
);
3620 if (vm_map_lookup_entry(map
, start
, &temp_entry
)) {
3624 temp_entry
= temp_entry
->vme_next
;
3628 /* first check entire range for submaps which can't support the */
3629 /* given inheritance. */
3630 while ((entry
!= vm_map_to_entry(map
)) && (entry
->vme_start
< end
)) {
3631 if(entry
->is_sub_map
) {
3632 if(new_inheritance
== VM_INHERIT_COPY
) {
3634 return(KERN_INVALID_ARGUMENT
);
3638 entry
= entry
->vme_next
;
3642 if (entry
!= vm_map_to_entry(map
)) {
3643 /* clip and unnest if necessary */
3644 vm_map_clip_start(map
, entry
, start
);
3647 while ((entry
!= vm_map_to_entry(map
)) && (entry
->vme_start
< end
)) {
3648 vm_map_clip_end(map
, entry
, end
);
3649 assert(!entry
->use_pmap
); /* clip did unnest if needed */
3651 entry
->inheritance
= new_inheritance
;
3653 entry
= entry
->vme_next
;
3657 return(KERN_SUCCESS
);
3661 * Update the accounting for the amount of wired memory in this map. If the user has
3662 * exceeded the defined limits, then we fail. Wiring on behalf of the kernel never fails.
3665 static kern_return_t
3668 vm_map_entry_t entry
,
3669 boolean_t user_wire
)
3676 * We're wiring memory at the request of the user. Check if this is the first time the user is wiring
3680 if (entry
->user_wired_count
== 0) {
3681 size
= entry
->vme_end
- entry
->vme_start
;
3684 * Since this is the first time the user is wiring this map entry, check to see if we're
3685 * exceeding the user wire limits. There is a per map limit which is the smaller of either
3686 * the process's rlimit or the global vm_user_wire_limit which caps this value. There is also
3687 * a system-wide limit on the amount of memory all users can wire. If the user is over either
3688 * limit, then we fail.
3691 if(size
+ map
->user_wire_size
> MIN(map
->user_wire_limit
, vm_user_wire_limit
) ||
3692 size
+ ptoa_64(vm_page_wire_count
) > vm_global_user_wire_limit
||
3693 size
+ ptoa_64(vm_page_wire_count
) > max_mem
- vm_global_no_user_wire_amount
)
3694 return KERN_RESOURCE_SHORTAGE
;
3697 * The first time the user wires an entry, we also increment the wired_count and add this to
3698 * the total that has been wired in the map.
3701 if (entry
->wired_count
>= MAX_WIRE_COUNT
)
3702 return KERN_FAILURE
;
3704 entry
->wired_count
++;
3705 map
->user_wire_size
+= size
;
3708 if (entry
->user_wired_count
>= MAX_WIRE_COUNT
)
3709 return KERN_FAILURE
;
3711 entry
->user_wired_count
++;
3716 * The kernel's wiring the memory. Just bump the count and continue.
3719 if (entry
->wired_count
>= MAX_WIRE_COUNT
)
3720 panic("vm_map_wire: too many wirings");
3722 entry
->wired_count
++;
3725 return KERN_SUCCESS
;
3729 * Update the memory wiring accounting now that the given map entry is being unwired.
3733 subtract_wire_counts(
3735 vm_map_entry_t entry
,
3736 boolean_t user_wire
)
3742 * We're unwiring memory at the request of the user. See if we're removing the last user wire reference.
3745 if (entry
->user_wired_count
== 1) {
3748 * We're removing the last user wire reference. Decrement the wired_count and the total
3749 * user wired memory for this map.
3752 assert(entry
->wired_count
>= 1);
3753 entry
->wired_count
--;
3754 map
->user_wire_size
-= entry
->vme_end
- entry
->vme_start
;
3757 assert(entry
->user_wired_count
>= 1);
3758 entry
->user_wired_count
--;
3763 * The kernel is unwiring the memory. Just update the count.
3766 assert(entry
->wired_count
>= 1);
3767 entry
->wired_count
--;
3774 * Sets the pageability of the specified address range in the
3775 * target map as wired. Regions specified as not pageable require
3776 * locked-down physical memory and physical page maps. The
3777 * access_type variable indicates types of accesses that must not
3778 * generate page faults. This is checked against protection of
3779 * memory being locked-down.
3781 * The map must not be locked, but a reference must remain to the
3782 * map throughout the call.
3784 static kern_return_t
3786 register vm_map_t map
,
3787 register vm_map_offset_t start
,
3788 register vm_map_offset_t end
,
3789 register vm_prot_t access_type
,
3790 boolean_t user_wire
,
3792 vm_map_offset_t pmap_addr
)
3794 register vm_map_entry_t entry
;
3795 struct vm_map_entry
*first_entry
, tmp_entry
;
3797 register vm_map_offset_t s
,e
;
3799 boolean_t need_wakeup
;
3800 boolean_t main_map
= FALSE
;
3801 wait_interrupt_t interruptible_state
;
3802 thread_t cur_thread
;
3803 unsigned int last_timestamp
;
3807 if(map_pmap
== NULL
)
3809 last_timestamp
= map
->timestamp
;
3811 VM_MAP_RANGE_CHECK(map
, start
, end
);
3812 assert(page_aligned(start
));
3813 assert(page_aligned(end
));
3815 /* We wired what the caller asked for, zero pages */
3817 return KERN_SUCCESS
;
3820 need_wakeup
= FALSE
;
3821 cur_thread
= current_thread();
3826 if (vm_map_lookup_entry(map
, s
, &first_entry
)) {
3827 entry
= first_entry
;
3829 * vm_map_clip_start will be done later.
3830 * We don't want to unnest any nested submaps here !
3833 /* Start address is not in map */
3834 rc
= KERN_INVALID_ADDRESS
;
3838 while ((entry
!= vm_map_to_entry(map
)) && (s
< end
)) {
3840 * At this point, we have wired from "start" to "s".
3841 * We still need to wire from "s" to "end".
3843 * "entry" hasn't been clipped, so it could start before "s"
3844 * and/or end after "end".
3847 /* "e" is how far we want to wire in this entry */
3853 * If another thread is wiring/unwiring this entry then
3854 * block after informing other thread to wake us up.
3856 if (entry
->in_transition
) {
3857 wait_result_t wait_result
;
3860 * We have not clipped the entry. Make sure that
3861 * the start address is in range so that the lookup
3862 * below will succeed.
3863 * "s" is the current starting point: we've already
3864 * wired from "start" to "s" and we still have
3865 * to wire from "s" to "end".
3868 entry
->needs_wakeup
= TRUE
;
3871 * wake up anybody waiting on entries that we have
3875 vm_map_entry_wakeup(map
);
3876 need_wakeup
= FALSE
;
3879 * User wiring is interruptible
3881 wait_result
= vm_map_entry_wait(map
,
3882 (user_wire
) ? THREAD_ABORTSAFE
:
3884 if (user_wire
&& wait_result
== THREAD_INTERRUPTED
) {
3886 * undo the wirings we have done so far
3887 * We do not clear the needs_wakeup flag,
3888 * because we cannot tell if we were the
3896 * Cannot avoid a lookup here. reset timestamp.
3898 last_timestamp
= map
->timestamp
;
3901 * The entry could have been clipped, look it up again.
3902 * Worse that can happen is, it may not exist anymore.
3904 if (!vm_map_lookup_entry(map
, s
, &first_entry
)) {
3906 panic("vm_map_wire: re-lookup failed");
3909 * User: undo everything upto the previous
3910 * entry. let vm_map_unwire worry about
3911 * checking the validity of the range.
3916 entry
= first_entry
;
3920 if (entry
->is_sub_map
) {
3921 vm_map_offset_t sub_start
;
3922 vm_map_offset_t sub_end
;
3923 vm_map_offset_t local_start
;
3924 vm_map_offset_t local_end
;
3927 vm_map_clip_start(map
, entry
, s
);
3928 vm_map_clip_end(map
, entry
, end
);
3930 sub_start
= entry
->offset
;
3931 sub_end
= entry
->vme_end
;
3932 sub_end
+= entry
->offset
- entry
->vme_start
;
3934 local_end
= entry
->vme_end
;
3935 if(map_pmap
== NULL
) {
3937 vm_object_offset_t offset
;
3940 vm_map_entry_t local_entry
;
3941 vm_map_version_t version
;
3942 vm_map_t lookup_map
;
3944 if(entry
->use_pmap
) {
3945 pmap
= entry
->object
.sub_map
->pmap
;
3946 /* ppc implementation requires that */
3947 /* submaps pmap address ranges line */
3948 /* up with parent map */
3950 pmap_addr
= sub_start
;
3958 if (entry
->wired_count
) {
3959 if ((rc
= add_wire_counts(map
, entry
, user_wire
)) != KERN_SUCCESS
)
3963 * The map was not unlocked:
3964 * no need to goto re-lookup.
3965 * Just go directly to next entry.
3967 entry
= entry
->vme_next
;
3968 s
= entry
->vme_start
;
3973 /* call vm_map_lookup_locked to */
3974 /* cause any needs copy to be */
3976 local_start
= entry
->vme_start
;
3978 vm_map_lock_write_to_read(map
);
3979 if(vm_map_lookup_locked(
3980 &lookup_map
, local_start
,
3982 OBJECT_LOCK_EXCLUSIVE
,
3984 &offset
, &prot
, &wired
,
3988 vm_map_unlock_read(lookup_map
);
3989 vm_map_unwire(map
, start
,
3991 return(KERN_FAILURE
);
3993 if(real_map
!= lookup_map
)
3994 vm_map_unlock(real_map
);
3995 vm_map_unlock_read(lookup_map
);
3997 vm_object_unlock(object
);
3999 /* we unlocked, so must re-lookup */
4000 if (!vm_map_lookup_entry(map
,
4008 * entry could have been "simplified",
4011 entry
= local_entry
;
4012 assert(s
== local_start
);
4013 vm_map_clip_start(map
, entry
, s
);
4014 vm_map_clip_end(map
, entry
, end
);
4015 /* re-compute "e" */
4020 /* did we have a change of type? */
4021 if (!entry
->is_sub_map
) {
4022 last_timestamp
= map
->timestamp
;
4026 local_start
= entry
->vme_start
;
4030 if ((rc
= add_wire_counts(map
, entry
, user_wire
)) != KERN_SUCCESS
)
4033 entry
->in_transition
= TRUE
;
4036 rc
= vm_map_wire_nested(entry
->object
.sub_map
,
4039 user_wire
, pmap
, pmap_addr
);
4043 * Find the entry again. It could have been clipped
4044 * after we unlocked the map.
4046 if (!vm_map_lookup_entry(map
, local_start
,
4048 panic("vm_map_wire: re-lookup failed");
4049 entry
= first_entry
;
4051 assert(local_start
== s
);
4052 /* re-compute "e" */
4057 last_timestamp
= map
->timestamp
;
4058 while ((entry
!= vm_map_to_entry(map
)) &&
4059 (entry
->vme_start
< e
)) {
4060 assert(entry
->in_transition
);
4061 entry
->in_transition
= FALSE
;
4062 if (entry
->needs_wakeup
) {
4063 entry
->needs_wakeup
= FALSE
;
4066 if (rc
!= KERN_SUCCESS
) {/* from vm_*_wire */
4067 subtract_wire_counts(map
, entry
, user_wire
);
4069 entry
= entry
->vme_next
;
4071 if (rc
!= KERN_SUCCESS
) { /* from vm_*_wire */
4075 /* no need to relookup again */
4076 s
= entry
->vme_start
;
4081 * If this entry is already wired then increment
4082 * the appropriate wire reference count.
4084 if (entry
->wired_count
) {
4086 * entry is already wired down, get our reference
4087 * after clipping to our range.
4089 vm_map_clip_start(map
, entry
, s
);
4090 vm_map_clip_end(map
, entry
, end
);
4092 if ((rc
= add_wire_counts(map
, entry
, user_wire
)) != KERN_SUCCESS
)
4095 /* map was not unlocked: no need to relookup */
4096 entry
= entry
->vme_next
;
4097 s
= entry
->vme_start
;
4102 * Unwired entry or wire request transmitted via submap
4107 * Perform actions of vm_map_lookup that need the write
4108 * lock on the map: create a shadow object for a
4109 * copy-on-write region, or an object for a zero-fill
4112 size
= entry
->vme_end
- entry
->vme_start
;
4114 * If wiring a copy-on-write page, we need to copy it now
4115 * even if we're only (currently) requesting read access.
4116 * This is aggressive, but once it's wired we can't move it.
4118 if (entry
->needs_copy
) {
4119 vm_object_shadow(&entry
->object
.vm_object
,
4120 &entry
->offset
, size
);
4121 entry
->needs_copy
= FALSE
;
4122 } else if (entry
->object
.vm_object
== VM_OBJECT_NULL
) {
4123 entry
->object
.vm_object
= vm_object_allocate(size
);
4124 entry
->offset
= (vm_object_offset_t
)0;
4127 vm_map_clip_start(map
, entry
, s
);
4128 vm_map_clip_end(map
, entry
, end
);
4130 /* re-compute "e" */
4136 * Check for holes and protection mismatch.
4137 * Holes: Next entry should be contiguous unless this
4138 * is the end of the region.
4139 * Protection: Access requested must be allowed, unless
4140 * wiring is by protection class
4142 if ((entry
->vme_end
< end
) &&
4143 ((entry
->vme_next
== vm_map_to_entry(map
)) ||
4144 (entry
->vme_next
->vme_start
> entry
->vme_end
))) {
4146 rc
= KERN_INVALID_ADDRESS
;
4149 if ((entry
->protection
& access_type
) != access_type
) {
4150 /* found a protection problem */
4151 rc
= KERN_PROTECTION_FAILURE
;
4155 assert(entry
->wired_count
== 0 && entry
->user_wired_count
== 0);
4157 if ((rc
= add_wire_counts(map
, entry
, user_wire
)) != KERN_SUCCESS
)
4160 entry
->in_transition
= TRUE
;
4163 * This entry might get split once we unlock the map.
4164 * In vm_fault_wire(), we need the current range as
4165 * defined by this entry. In order for this to work
4166 * along with a simultaneous clip operation, we make a
4167 * temporary copy of this entry and use that for the
4168 * wiring. Note that the underlying objects do not
4169 * change during a clip.
4174 * The in_transition state guarentees that the entry
4175 * (or entries for this range, if split occured) will be
4176 * there when the map lock is acquired for the second time.
4180 if (!user_wire
&& cur_thread
!= THREAD_NULL
)
4181 interruptible_state
= thread_interrupt_level(THREAD_UNINT
);
4183 interruptible_state
= THREAD_UNINT
;
4186 rc
= vm_fault_wire(map
,
4187 &tmp_entry
, map_pmap
, pmap_addr
);
4189 rc
= vm_fault_wire(map
,
4190 &tmp_entry
, map
->pmap
,
4191 tmp_entry
.vme_start
);
4193 if (!user_wire
&& cur_thread
!= THREAD_NULL
)
4194 thread_interrupt_level(interruptible_state
);
4198 if (last_timestamp
+1 != map
->timestamp
) {
4200 * Find the entry again. It could have been clipped
4201 * after we unlocked the map.
4203 if (!vm_map_lookup_entry(map
, tmp_entry
.vme_start
,
4205 panic("vm_map_wire: re-lookup failed");
4207 entry
= first_entry
;
4210 last_timestamp
= map
->timestamp
;
4212 while ((entry
!= vm_map_to_entry(map
)) &&
4213 (entry
->vme_start
< tmp_entry
.vme_end
)) {
4214 assert(entry
->in_transition
);
4215 entry
->in_transition
= FALSE
;
4216 if (entry
->needs_wakeup
) {
4217 entry
->needs_wakeup
= FALSE
;
4220 if (rc
!= KERN_SUCCESS
) { /* from vm_*_wire */
4221 subtract_wire_counts(map
, entry
, user_wire
);
4223 entry
= entry
->vme_next
;
4226 if (rc
!= KERN_SUCCESS
) { /* from vm_*_wire */
4230 s
= entry
->vme_start
;
4231 } /* end while loop through map entries */
4234 if (rc
== KERN_SUCCESS
) {
4235 /* repair any damage we may have made to the VM map */
4236 vm_map_simplify_range(map
, start
, end
);
4242 * wake up anybody waiting on entries we wired.
4245 vm_map_entry_wakeup(map
);
4247 if (rc
!= KERN_SUCCESS
) {
4248 /* undo what has been wired so far */
4249 vm_map_unwire(map
, start
, s
, user_wire
);
4258 register vm_map_t map
,
4259 register vm_map_offset_t start
,
4260 register vm_map_offset_t end
,
4261 register vm_prot_t access_type
,
4262 boolean_t user_wire
)
4269 * the calls to mapping_prealloc and mapping_relpre
4270 * (along with the VM_MAP_RANGE_CHECK to insure a
4271 * resonable range was passed in) are
4272 * currently necessary because
4273 * we haven't enabled kernel pre-emption
4274 * and/or the pmap_enter cannot purge and re-use
4277 VM_MAP_RANGE_CHECK(map
, start
, end
);
4278 assert((unsigned int) (end
- start
) == (end
- start
));
4279 mapping_prealloc((unsigned int) (end
- start
));
4281 kret
= vm_map_wire_nested(map
, start
, end
, access_type
,
4282 user_wire
, (pmap_t
)NULL
, 0);
4292 * Sets the pageability of the specified address range in the target
4293 * as pageable. Regions specified must have been wired previously.
4295 * The map must not be locked, but a reference must remain to the map
4296 * throughout the call.
4298 * Kernel will panic on failures. User unwire ignores holes and
4299 * unwired and intransition entries to avoid losing memory by leaving
4302 static kern_return_t
4303 vm_map_unwire_nested(
4304 register vm_map_t map
,
4305 register vm_map_offset_t start
,
4306 register vm_map_offset_t end
,
4307 boolean_t user_wire
,
4309 vm_map_offset_t pmap_addr
)
4311 register vm_map_entry_t entry
;
4312 struct vm_map_entry
*first_entry
, tmp_entry
;
4313 boolean_t need_wakeup
;
4314 boolean_t main_map
= FALSE
;
4315 unsigned int last_timestamp
;
4318 if(map_pmap
== NULL
)
4320 last_timestamp
= map
->timestamp
;
4322 VM_MAP_RANGE_CHECK(map
, start
, end
);
4323 assert(page_aligned(start
));
4324 assert(page_aligned(end
));
4327 /* We unwired what the caller asked for: zero pages */
4329 return KERN_SUCCESS
;
4332 if (vm_map_lookup_entry(map
, start
, &first_entry
)) {
4333 entry
= first_entry
;
4335 * vm_map_clip_start will be done later.
4336 * We don't want to unnest any nested sub maps here !
4341 panic("vm_map_unwire: start not found");
4343 /* Start address is not in map. */
4345 return(KERN_INVALID_ADDRESS
);
4348 if (entry
->superpage_size
) {
4349 /* superpages are always wired */
4351 return KERN_INVALID_ADDRESS
;
4354 need_wakeup
= FALSE
;
4355 while ((entry
!= vm_map_to_entry(map
)) && (entry
->vme_start
< end
)) {
4356 if (entry
->in_transition
) {
4359 * Another thread is wiring down this entry. Note
4360 * that if it is not for the other thread we would
4361 * be unwiring an unwired entry. This is not
4362 * permitted. If we wait, we will be unwiring memory
4366 * Another thread is unwiring this entry. We did not
4367 * have a reference to it, because if we did, this
4368 * entry will not be getting unwired now.
4373 * This could happen: there could be some
4374 * overlapping vslock/vsunlock operations
4376 * We should probably just wait and retry,
4377 * but then we have to be careful that this
4378 * entry could get "simplified" after
4379 * "in_transition" gets unset and before
4380 * we re-lookup the entry, so we would
4381 * have to re-clip the entry to avoid
4382 * re-unwiring what we have already unwired...
4383 * See vm_map_wire_nested().
4385 * Or we could just ignore "in_transition"
4386 * here and proceed to decement the wired
4387 * count(s) on this entry. That should be fine
4388 * as long as "wired_count" doesn't drop all
4389 * the way to 0 (and we should panic if THAT
4392 panic("vm_map_unwire: in_transition entry");
4395 entry
= entry
->vme_next
;
4399 if (entry
->is_sub_map
) {
4400 vm_map_offset_t sub_start
;
4401 vm_map_offset_t sub_end
;
4402 vm_map_offset_t local_end
;
4405 vm_map_clip_start(map
, entry
, start
);
4406 vm_map_clip_end(map
, entry
, end
);
4408 sub_start
= entry
->offset
;
4409 sub_end
= entry
->vme_end
- entry
->vme_start
;
4410 sub_end
+= entry
->offset
;
4411 local_end
= entry
->vme_end
;
4412 if(map_pmap
== NULL
) {
4413 if(entry
->use_pmap
) {
4414 pmap
= entry
->object
.sub_map
->pmap
;
4415 pmap_addr
= sub_start
;
4420 if (entry
->wired_count
== 0 ||
4421 (user_wire
&& entry
->user_wired_count
== 0)) {
4423 panic("vm_map_unwire: entry is unwired");
4424 entry
= entry
->vme_next
;
4430 * Holes: Next entry should be contiguous unless
4431 * this is the end of the region.
4433 if (((entry
->vme_end
< end
) &&
4434 ((entry
->vme_next
== vm_map_to_entry(map
)) ||
4435 (entry
->vme_next
->vme_start
4436 > entry
->vme_end
)))) {
4438 panic("vm_map_unwire: non-contiguous region");
4440 entry = entry->vme_next;
4445 subtract_wire_counts(map
, entry
, user_wire
);
4447 if (entry
->wired_count
!= 0) {
4448 entry
= entry
->vme_next
;
4452 entry
->in_transition
= TRUE
;
4453 tmp_entry
= *entry
;/* see comment in vm_map_wire() */
4456 * We can unlock the map now. The in_transition state
4457 * guarantees existance of the entry.
4460 vm_map_unwire_nested(entry
->object
.sub_map
,
4461 sub_start
, sub_end
, user_wire
, pmap
, pmap_addr
);
4464 if (last_timestamp
+1 != map
->timestamp
) {
4466 * Find the entry again. It could have been
4467 * clipped or deleted after we unlocked the map.
4469 if (!vm_map_lookup_entry(map
,
4470 tmp_entry
.vme_start
,
4473 panic("vm_map_unwire: re-lookup failed");
4474 entry
= first_entry
->vme_next
;
4476 entry
= first_entry
;
4478 last_timestamp
= map
->timestamp
;
4481 * clear transition bit for all constituent entries
4482 * that were in the original entry (saved in
4483 * tmp_entry). Also check for waiters.
4485 while ((entry
!= vm_map_to_entry(map
)) &&
4486 (entry
->vme_start
< tmp_entry
.vme_end
)) {
4487 assert(entry
->in_transition
);
4488 entry
->in_transition
= FALSE
;
4489 if (entry
->needs_wakeup
) {
4490 entry
->needs_wakeup
= FALSE
;
4493 entry
= entry
->vme_next
;
4498 vm_map_unwire_nested(entry
->object
.sub_map
,
4499 sub_start
, sub_end
, user_wire
, map_pmap
,
4503 if (last_timestamp
+1 != map
->timestamp
) {
4505 * Find the entry again. It could have been
4506 * clipped or deleted after we unlocked the map.
4508 if (!vm_map_lookup_entry(map
,
4509 tmp_entry
.vme_start
,
4512 panic("vm_map_unwire: re-lookup failed");
4513 entry
= first_entry
->vme_next
;
4515 entry
= first_entry
;
4517 last_timestamp
= map
->timestamp
;
4522 if ((entry
->wired_count
== 0) ||
4523 (user_wire
&& entry
->user_wired_count
== 0)) {
4525 panic("vm_map_unwire: entry is unwired");
4527 entry
= entry
->vme_next
;
4531 assert(entry
->wired_count
> 0 &&
4532 (!user_wire
|| entry
->user_wired_count
> 0));
4534 vm_map_clip_start(map
, entry
, start
);
4535 vm_map_clip_end(map
, entry
, end
);
4539 * Holes: Next entry should be contiguous unless
4540 * this is the end of the region.
4542 if (((entry
->vme_end
< end
) &&
4543 ((entry
->vme_next
== vm_map_to_entry(map
)) ||
4544 (entry
->vme_next
->vme_start
> entry
->vme_end
)))) {
4547 panic("vm_map_unwire: non-contiguous region");
4548 entry
= entry
->vme_next
;
4552 subtract_wire_counts(map
, entry
, user_wire
);
4554 if (entry
->wired_count
!= 0) {
4555 entry
= entry
->vme_next
;
4559 if(entry
->zero_wired_pages
) {
4560 entry
->zero_wired_pages
= FALSE
;
4563 entry
->in_transition
= TRUE
;
4564 tmp_entry
= *entry
; /* see comment in vm_map_wire() */
4567 * We can unlock the map now. The in_transition state
4568 * guarantees existance of the entry.
4572 vm_fault_unwire(map
,
4573 &tmp_entry
, FALSE
, map_pmap
, pmap_addr
);
4575 vm_fault_unwire(map
,
4576 &tmp_entry
, FALSE
, map
->pmap
,
4577 tmp_entry
.vme_start
);
4581 if (last_timestamp
+1 != map
->timestamp
) {
4583 * Find the entry again. It could have been clipped
4584 * or deleted after we unlocked the map.
4586 if (!vm_map_lookup_entry(map
, tmp_entry
.vme_start
,
4589 panic("vm_map_unwire: re-lookup failed");
4590 entry
= first_entry
->vme_next
;
4592 entry
= first_entry
;
4594 last_timestamp
= map
->timestamp
;
4597 * clear transition bit for all constituent entries that
4598 * were in the original entry (saved in tmp_entry). Also
4599 * check for waiters.
4601 while ((entry
!= vm_map_to_entry(map
)) &&
4602 (entry
->vme_start
< tmp_entry
.vme_end
)) {
4603 assert(entry
->in_transition
);
4604 entry
->in_transition
= FALSE
;
4605 if (entry
->needs_wakeup
) {
4606 entry
->needs_wakeup
= FALSE
;
4609 entry
= entry
->vme_next
;
4614 * We might have fragmented the address space when we wired this
4615 * range of addresses. Attempt to re-coalesce these VM map entries
4616 * with their neighbors now that they're no longer wired.
4617 * Under some circumstances, address space fragmentation can
4618 * prevent VM object shadow chain collapsing, which can cause
4621 vm_map_simplify_range(map
, start
, end
);
4625 * wake up anybody waiting on entries that we have unwired.
4628 vm_map_entry_wakeup(map
);
4629 return(KERN_SUCCESS
);
4635 register vm_map_t map
,
4636 register vm_map_offset_t start
,
4637 register vm_map_offset_t end
,
4638 boolean_t user_wire
)
4640 return vm_map_unwire_nested(map
, start
, end
,
4641 user_wire
, (pmap_t
)NULL
, 0);
4646 * vm_map_entry_delete: [ internal use only ]
4648 * Deallocate the given entry from the target map.
4651 vm_map_entry_delete(
4652 register vm_map_t map
,
4653 register vm_map_entry_t entry
)
4655 register vm_map_offset_t s
, e
;
4656 register vm_object_t object
;
4657 register vm_map_t submap
;
4659 s
= entry
->vme_start
;
4661 assert(page_aligned(s
));
4662 assert(page_aligned(e
));
4663 assert(entry
->wired_count
== 0);
4664 assert(entry
->user_wired_count
== 0);
4665 assert(!entry
->permanent
);
4667 if (entry
->is_sub_map
) {
4669 submap
= entry
->object
.sub_map
;
4672 object
= entry
->object
.vm_object
;
4675 vm_map_entry_unlink(map
, entry
);
4678 vm_map_entry_dispose(map
, entry
);
4682 * Deallocate the object only after removing all
4683 * pmap entries pointing to its pages.
4686 vm_map_deallocate(submap
);
4688 vm_object_deallocate(object
);
4693 vm_map_submap_pmap_clean(
4695 vm_map_offset_t start
,
4696 vm_map_offset_t end
,
4698 vm_map_offset_t offset
)
4700 vm_map_offset_t submap_start
;
4701 vm_map_offset_t submap_end
;
4702 vm_map_size_t remove_size
;
4703 vm_map_entry_t entry
;
4705 submap_end
= offset
+ (end
- start
);
4706 submap_start
= offset
;
4707 if(vm_map_lookup_entry(sub_map
, offset
, &entry
)) {
4709 remove_size
= (entry
->vme_end
- entry
->vme_start
);
4710 if(offset
> entry
->vme_start
)
4711 remove_size
-= offset
- entry
->vme_start
;
4714 if(submap_end
< entry
->vme_end
) {
4716 entry
->vme_end
- submap_end
;
4718 if(entry
->is_sub_map
) {
4719 vm_map_submap_pmap_clean(
4722 start
+ remove_size
,
4723 entry
->object
.sub_map
,
4727 if((map
->mapped
) && (map
->ref_count
)
4728 && (entry
->object
.vm_object
!= NULL
)) {
4729 vm_object_pmap_protect(
4730 entry
->object
.vm_object
,
4737 pmap_remove(map
->pmap
,
4739 (addr64_t
)(start
+ remove_size
));
4744 entry
= entry
->vme_next
;
4746 while((entry
!= vm_map_to_entry(sub_map
))
4747 && (entry
->vme_start
< submap_end
)) {
4748 remove_size
= (entry
->vme_end
- entry
->vme_start
);
4749 if(submap_end
< entry
->vme_end
) {
4750 remove_size
-= entry
->vme_end
- submap_end
;
4752 if(entry
->is_sub_map
) {
4753 vm_map_submap_pmap_clean(
4755 (start
+ entry
->vme_start
) - offset
,
4756 ((start
+ entry
->vme_start
) - offset
) + remove_size
,
4757 entry
->object
.sub_map
,
4760 if((map
->mapped
) && (map
->ref_count
)
4761 && (entry
->object
.vm_object
!= NULL
)) {
4762 vm_object_pmap_protect(
4763 entry
->object
.vm_object
,
4770 pmap_remove(map
->pmap
,
4771 (addr64_t
)((start
+ entry
->vme_start
)
4773 (addr64_t
)(((start
+ entry
->vme_start
)
4774 - offset
) + remove_size
));
4777 entry
= entry
->vme_next
;
4783 * vm_map_delete: [ internal use only ]
4785 * Deallocates the given address range from the target map.
4786 * Removes all user wirings. Unwires one kernel wiring if
4787 * VM_MAP_REMOVE_KUNWIRE is set. Waits for kernel wirings to go
4788 * away if VM_MAP_REMOVE_WAIT_FOR_KWIRE is set. Sleeps
4789 * interruptibly if VM_MAP_REMOVE_INTERRUPTIBLE is set.
4791 * This routine is called with map locked and leaves map locked.
4793 static kern_return_t
4796 vm_map_offset_t start
,
4797 vm_map_offset_t end
,
4801 vm_map_entry_t entry
, next
;
4802 struct vm_map_entry
*first_entry
, tmp_entry
;
4803 register vm_map_offset_t s
;
4804 register vm_object_t object
;
4805 boolean_t need_wakeup
;
4806 unsigned int last_timestamp
= ~0; /* unlikely value */
4809 interruptible
= (flags
& VM_MAP_REMOVE_INTERRUPTIBLE
) ?
4810 THREAD_ABORTSAFE
: THREAD_UNINT
;
4813 * All our DMA I/O operations in IOKit are currently done by
4814 * wiring through the map entries of the task requesting the I/O.
4815 * Because of this, we must always wait for kernel wirings
4816 * to go away on the entries before deleting them.
4818 * Any caller who wants to actually remove a kernel wiring
4819 * should explicitly set the VM_MAP_REMOVE_KUNWIRE flag to
4820 * properly remove one wiring instead of blasting through
4823 flags
|= VM_MAP_REMOVE_WAIT_FOR_KWIRE
;
4827 * Find the start of the region, and clip it
4829 if (vm_map_lookup_entry(map
, start
, &first_entry
)) {
4830 entry
= first_entry
;
4831 if (entry
->superpage_size
&& (start
& ~SUPERPAGE_MASK
)) { /* extend request to whole entry */ start
= SUPERPAGE_ROUND_DOWN(start
);
4832 start
= SUPERPAGE_ROUND_DOWN(start
);
4835 if (start
== entry
->vme_start
) {
4837 * No need to clip. We don't want to cause
4838 * any unnecessary unnesting in this case...
4841 vm_map_clip_start(map
, entry
, start
);
4845 * Fix the lookup hint now, rather than each
4846 * time through the loop.
4848 SAVE_HINT_MAP_WRITE(map
, entry
->vme_prev
);
4850 entry
= first_entry
->vme_next
;
4854 if (entry
->superpage_size
)
4855 end
= SUPERPAGE_ROUND_UP(end
);
4857 need_wakeup
= FALSE
;
4859 * Step through all entries in this region
4861 s
= entry
->vme_start
;
4862 while ((entry
!= vm_map_to_entry(map
)) && (s
< end
)) {
4864 * At this point, we have deleted all the memory entries
4865 * between "start" and "s". We still need to delete
4866 * all memory entries between "s" and "end".
4867 * While we were blocked and the map was unlocked, some
4868 * new memory entries could have been re-allocated between
4869 * "start" and "s" and we don't want to mess with those.
4870 * Some of those entries could even have been re-assembled
4871 * with an entry after "s" (in vm_map_simplify_entry()), so
4872 * we may have to vm_map_clip_start() again.
4875 if (entry
->vme_start
>= s
) {
4877 * This entry starts on or after "s"
4878 * so no need to clip its start.
4882 * This entry has been re-assembled by a
4883 * vm_map_simplify_entry(). We need to
4884 * re-clip its start.
4886 vm_map_clip_start(map
, entry
, s
);
4888 if (entry
->vme_end
<= end
) {
4890 * This entry is going away completely, so no need
4891 * to clip and possibly cause an unnecessary unnesting.
4894 vm_map_clip_end(map
, entry
, end
);
4897 if (entry
->permanent
) {
4898 panic("attempt to remove permanent VM map entry "
4899 "%p [0x%llx:0x%llx]\n",
4900 entry
, (uint64_t) s
, (uint64_t) end
);
4904 if (entry
->in_transition
) {
4905 wait_result_t wait_result
;
4908 * Another thread is wiring/unwiring this entry.
4909 * Let the other thread know we are waiting.
4911 assert(s
== entry
->vme_start
);
4912 entry
->needs_wakeup
= TRUE
;
4915 * wake up anybody waiting on entries that we have
4916 * already unwired/deleted.
4919 vm_map_entry_wakeup(map
);
4920 need_wakeup
= FALSE
;
4923 wait_result
= vm_map_entry_wait(map
, interruptible
);
4925 if (interruptible
&&
4926 wait_result
== THREAD_INTERRUPTED
) {
4928 * We do not clear the needs_wakeup flag,
4929 * since we cannot tell if we were the only one.
4932 return KERN_ABORTED
;
4936 * The entry could have been clipped or it
4937 * may not exist anymore. Look it up again.
4939 if (!vm_map_lookup_entry(map
, s
, &first_entry
)) {
4940 assert((map
!= kernel_map
) &&
4941 (!entry
->is_sub_map
));
4943 * User: use the next entry
4945 entry
= first_entry
->vme_next
;
4946 s
= entry
->vme_start
;
4948 entry
= first_entry
;
4949 SAVE_HINT_MAP_WRITE(map
, entry
->vme_prev
);
4951 last_timestamp
= map
->timestamp
;
4953 } /* end in_transition */
4955 if (entry
->wired_count
) {
4956 boolean_t user_wire
;
4958 user_wire
= entry
->user_wired_count
> 0;
4961 * Remove a kernel wiring if requested
4963 if (flags
& VM_MAP_REMOVE_KUNWIRE
) {
4964 entry
->wired_count
--;
4968 * Remove all user wirings for proper accounting
4970 if (entry
->user_wired_count
> 0) {
4971 while (entry
->user_wired_count
)
4972 subtract_wire_counts(map
, entry
, user_wire
);
4975 if (entry
->wired_count
!= 0) {
4976 assert(map
!= kernel_map
);
4978 * Cannot continue. Typical case is when
4979 * a user thread has physical io pending on
4980 * on this page. Either wait for the
4981 * kernel wiring to go away or return an
4984 if (flags
& VM_MAP_REMOVE_WAIT_FOR_KWIRE
) {
4985 wait_result_t wait_result
;
4987 assert(s
== entry
->vme_start
);
4988 entry
->needs_wakeup
= TRUE
;
4989 wait_result
= vm_map_entry_wait(map
,
4992 if (interruptible
&&
4993 wait_result
== THREAD_INTERRUPTED
) {
4995 * We do not clear the
4996 * needs_wakeup flag, since we
4997 * cannot tell if we were the
5001 return KERN_ABORTED
;
5005 * The entry could have been clipped or
5006 * it may not exist anymore. Look it
5009 if (!vm_map_lookup_entry(map
, s
,
5011 assert(map
!= kernel_map
);
5013 * User: use the next entry
5015 entry
= first_entry
->vme_next
;
5016 s
= entry
->vme_start
;
5018 entry
= first_entry
;
5019 SAVE_HINT_MAP_WRITE(map
, entry
->vme_prev
);
5021 last_timestamp
= map
->timestamp
;
5025 return KERN_FAILURE
;
5029 entry
->in_transition
= TRUE
;
5031 * copy current entry. see comment in vm_map_wire()
5034 assert(s
== entry
->vme_start
);
5037 * We can unlock the map now. The in_transition
5038 * state guarentees existance of the entry.
5042 if (tmp_entry
.is_sub_map
) {
5044 vm_map_offset_t sub_start
, sub_end
;
5046 vm_map_offset_t pmap_addr
;
5049 sub_map
= tmp_entry
.object
.sub_map
;
5050 sub_start
= tmp_entry
.offset
;
5051 sub_end
= sub_start
+ (tmp_entry
.vme_end
-
5052 tmp_entry
.vme_start
);
5053 if (tmp_entry
.use_pmap
) {
5054 pmap
= sub_map
->pmap
;
5055 pmap_addr
= tmp_entry
.vme_start
;
5058 pmap_addr
= tmp_entry
.vme_start
;
5060 (void) vm_map_unwire_nested(sub_map
,
5066 vm_fault_unwire(map
, &tmp_entry
,
5067 tmp_entry
.object
.vm_object
== kernel_object
,
5068 map
->pmap
, tmp_entry
.vme_start
);
5073 if (last_timestamp
+1 != map
->timestamp
) {
5075 * Find the entry again. It could have
5076 * been clipped after we unlocked the map.
5078 if (!vm_map_lookup_entry(map
, s
, &first_entry
)){
5079 assert((map
!= kernel_map
) &&
5080 (!entry
->is_sub_map
));
5081 first_entry
= first_entry
->vme_next
;
5082 s
= first_entry
->vme_start
;
5084 SAVE_HINT_MAP_WRITE(map
, entry
->vme_prev
);
5087 SAVE_HINT_MAP_WRITE(map
, entry
->vme_prev
);
5088 first_entry
= entry
;
5091 last_timestamp
= map
->timestamp
;
5093 entry
= first_entry
;
5094 while ((entry
!= vm_map_to_entry(map
)) &&
5095 (entry
->vme_start
< tmp_entry
.vme_end
)) {
5096 assert(entry
->in_transition
);
5097 entry
->in_transition
= FALSE
;
5098 if (entry
->needs_wakeup
) {
5099 entry
->needs_wakeup
= FALSE
;
5102 entry
= entry
->vme_next
;
5105 * We have unwired the entry(s). Go back and
5108 entry
= first_entry
;
5112 /* entry is unwired */
5113 assert(entry
->wired_count
== 0);
5114 assert(entry
->user_wired_count
== 0);
5116 assert(s
== entry
->vme_start
);
5118 if (flags
& VM_MAP_REMOVE_NO_PMAP_CLEANUP
) {
5120 * XXX with the VM_MAP_REMOVE_SAVE_ENTRIES flag to
5121 * vm_map_delete(), some map entries might have been
5122 * transferred to a "zap_map", which doesn't have a
5123 * pmap. The original pmap has already been flushed
5124 * in the vm_map_delete() call targeting the original
5125 * map, but when we get to destroying the "zap_map",
5126 * we don't have any pmap to flush, so let's just skip
5129 } else if (entry
->is_sub_map
) {
5130 if (entry
->use_pmap
) {
5131 #ifndef NO_NESTED_PMAP
5132 pmap_unnest(map
->pmap
,
5133 (addr64_t
)entry
->vme_start
,
5134 entry
->vme_end
- entry
->vme_start
);
5135 #endif /* NO_NESTED_PMAP */
5136 if ((map
->mapped
) && (map
->ref_count
)) {
5137 /* clean up parent map/maps */
5138 vm_map_submap_pmap_clean(
5139 map
, entry
->vme_start
,
5141 entry
->object
.sub_map
,
5145 vm_map_submap_pmap_clean(
5146 map
, entry
->vme_start
, entry
->vme_end
,
5147 entry
->object
.sub_map
,
5150 } else if (entry
->object
.vm_object
!= kernel_object
) {
5151 object
= entry
->object
.vm_object
;
5152 if((map
->mapped
) && (map
->ref_count
)) {
5153 vm_object_pmap_protect(
5154 object
, entry
->offset
,
5155 entry
->vme_end
- entry
->vme_start
,
5160 pmap_remove(map
->pmap
,
5161 (addr64_t
)entry
->vme_start
,
5162 (addr64_t
)entry
->vme_end
);
5167 * All pmap mappings for this map entry must have been
5170 assert(vm_map_pmap_is_empty(map
,
5174 next
= entry
->vme_next
;
5175 s
= next
->vme_start
;
5176 last_timestamp
= map
->timestamp
;
5178 if ((flags
& VM_MAP_REMOVE_SAVE_ENTRIES
) &&
5179 zap_map
!= VM_MAP_NULL
) {
5180 vm_map_size_t entry_size
;
5182 * The caller wants to save the affected VM map entries
5183 * into the "zap_map". The caller will take care of
5186 /* unlink the entry from "map" ... */
5187 vm_map_entry_unlink(map
, entry
);
5188 /* ... and add it to the end of the "zap_map" */
5189 vm_map_entry_link(zap_map
,
5190 vm_map_last_entry(zap_map
),
5192 entry_size
= entry
->vme_end
- entry
->vme_start
;
5193 map
->size
-= entry_size
;
5194 zap_map
->size
+= entry_size
;
5195 /* we didn't unlock the map, so no timestamp increase */
5198 vm_map_entry_delete(map
, entry
);
5199 /* vm_map_entry_delete unlocks the map */
5205 if(entry
== vm_map_to_entry(map
)) {
5208 if (last_timestamp
+1 != map
->timestamp
) {
5210 * we are responsible for deleting everything
5211 * from the give space, if someone has interfered
5212 * we pick up where we left off, back fills should
5213 * be all right for anyone except map_delete and
5214 * we have to assume that the task has been fully
5215 * disabled before we get here
5217 if (!vm_map_lookup_entry(map
, s
, &entry
)){
5218 entry
= entry
->vme_next
;
5219 s
= entry
->vme_start
;
5221 SAVE_HINT_MAP_WRITE(map
, entry
->vme_prev
);
5224 * others can not only allocate behind us, we can
5225 * also see coalesce while we don't have the map lock
5227 if(entry
== vm_map_to_entry(map
)) {
5231 last_timestamp
= map
->timestamp
;
5234 if (map
->wait_for_space
)
5235 thread_wakeup((event_t
) map
);
5237 * wake up anybody waiting on entries that we have already deleted.
5240 vm_map_entry_wakeup(map
);
5242 return KERN_SUCCESS
;
5248 * Remove the given address range from the target map.
5249 * This is the exported form of vm_map_delete.
5253 register vm_map_t map
,
5254 register vm_map_offset_t start
,
5255 register vm_map_offset_t end
,
5256 register boolean_t flags
)
5258 register kern_return_t result
;
5261 VM_MAP_RANGE_CHECK(map
, start
, end
);
5262 result
= vm_map_delete(map
, start
, end
, flags
, VM_MAP_NULL
);
5270 * Routine: vm_map_copy_discard
5273 * Dispose of a map copy object (returned by
5277 vm_map_copy_discard(
5280 if (copy
== VM_MAP_COPY_NULL
)
5283 switch (copy
->type
) {
5284 case VM_MAP_COPY_ENTRY_LIST
:
5285 while (vm_map_copy_first_entry(copy
) !=
5286 vm_map_copy_to_entry(copy
)) {
5287 vm_map_entry_t entry
= vm_map_copy_first_entry(copy
);
5289 vm_map_copy_entry_unlink(copy
, entry
);
5290 vm_object_deallocate(entry
->object
.vm_object
);
5291 vm_map_copy_entry_dispose(copy
, entry
);
5294 case VM_MAP_COPY_OBJECT
:
5295 vm_object_deallocate(copy
->cpy_object
);
5297 case VM_MAP_COPY_KERNEL_BUFFER
:
5300 * The vm_map_copy_t and possibly the data buffer were
5301 * allocated by a single call to kalloc(), i.e. the
5302 * vm_map_copy_t was not allocated out of the zone.
5304 kfree(copy
, copy
->cpy_kalloc_size
);
5307 zfree(vm_map_copy_zone
, copy
);
5311 * Routine: vm_map_copy_copy
5314 * Move the information in a map copy object to
5315 * a new map copy object, leaving the old one
5318 * This is used by kernel routines that need
5319 * to look at out-of-line data (in copyin form)
5320 * before deciding whether to return SUCCESS.
5321 * If the routine returns FAILURE, the original
5322 * copy object will be deallocated; therefore,
5323 * these routines must make a copy of the copy
5324 * object and leave the original empty so that
5325 * deallocation will not fail.
5331 vm_map_copy_t new_copy
;
5333 if (copy
== VM_MAP_COPY_NULL
)
5334 return VM_MAP_COPY_NULL
;
5337 * Allocate a new copy object, and copy the information
5338 * from the old one into it.
5341 new_copy
= (vm_map_copy_t
) zalloc(vm_map_copy_zone
);
5344 if (copy
->type
== VM_MAP_COPY_ENTRY_LIST
) {
5346 * The links in the entry chain must be
5347 * changed to point to the new copy object.
5349 vm_map_copy_first_entry(copy
)->vme_prev
5350 = vm_map_copy_to_entry(new_copy
);
5351 vm_map_copy_last_entry(copy
)->vme_next
5352 = vm_map_copy_to_entry(new_copy
);
5356 * Change the old copy object into one that contains
5357 * nothing to be deallocated.
5359 copy
->type
= VM_MAP_COPY_OBJECT
;
5360 copy
->cpy_object
= VM_OBJECT_NULL
;
5363 * Return the new object.
5368 static kern_return_t
5369 vm_map_overwrite_submap_recurse(
5371 vm_map_offset_t dst_addr
,
5372 vm_map_size_t dst_size
)
5374 vm_map_offset_t dst_end
;
5375 vm_map_entry_t tmp_entry
;
5376 vm_map_entry_t entry
;
5377 kern_return_t result
;
5378 boolean_t encountered_sub_map
= FALSE
;
5383 * Verify that the destination is all writeable
5384 * initially. We have to trunc the destination
5385 * address and round the copy size or we'll end up
5386 * splitting entries in strange ways.
5389 dst_end
= vm_map_round_page(dst_addr
+ dst_size
);
5390 vm_map_lock(dst_map
);
5393 if (!vm_map_lookup_entry(dst_map
, dst_addr
, &tmp_entry
)) {
5394 vm_map_unlock(dst_map
);
5395 return(KERN_INVALID_ADDRESS
);
5398 vm_map_clip_start(dst_map
, tmp_entry
, vm_map_trunc_page(dst_addr
));
5399 assert(!tmp_entry
->use_pmap
); /* clipping did unnest if needed */
5401 for (entry
= tmp_entry
;;) {
5402 vm_map_entry_t next
;
5404 next
= entry
->vme_next
;
5405 while(entry
->is_sub_map
) {
5406 vm_map_offset_t sub_start
;
5407 vm_map_offset_t sub_end
;
5408 vm_map_offset_t local_end
;
5410 if (entry
->in_transition
) {
5412 * Say that we are waiting, and wait for entry.
5414 entry
->needs_wakeup
= TRUE
;
5415 vm_map_entry_wait(dst_map
, THREAD_UNINT
);
5420 encountered_sub_map
= TRUE
;
5421 sub_start
= entry
->offset
;
5423 if(entry
->vme_end
< dst_end
)
5424 sub_end
= entry
->vme_end
;
5427 sub_end
-= entry
->vme_start
;
5428 sub_end
+= entry
->offset
;
5429 local_end
= entry
->vme_end
;
5430 vm_map_unlock(dst_map
);
5432 result
= vm_map_overwrite_submap_recurse(
5433 entry
->object
.sub_map
,
5435 sub_end
- sub_start
);
5437 if(result
!= KERN_SUCCESS
)
5439 if (dst_end
<= entry
->vme_end
)
5440 return KERN_SUCCESS
;
5441 vm_map_lock(dst_map
);
5442 if(!vm_map_lookup_entry(dst_map
, local_end
,
5444 vm_map_unlock(dst_map
);
5445 return(KERN_INVALID_ADDRESS
);
5448 next
= entry
->vme_next
;
5451 if ( ! (entry
->protection
& VM_PROT_WRITE
)) {
5452 vm_map_unlock(dst_map
);
5453 return(KERN_PROTECTION_FAILURE
);
5457 * If the entry is in transition, we must wait
5458 * for it to exit that state. Anything could happen
5459 * when we unlock the map, so start over.
5461 if (entry
->in_transition
) {
5464 * Say that we are waiting, and wait for entry.
5466 entry
->needs_wakeup
= TRUE
;
5467 vm_map_entry_wait(dst_map
, THREAD_UNINT
);
5473 * our range is contained completely within this map entry
5475 if (dst_end
<= entry
->vme_end
) {
5476 vm_map_unlock(dst_map
);
5477 return KERN_SUCCESS
;
5480 * check that range specified is contiguous region
5482 if ((next
== vm_map_to_entry(dst_map
)) ||
5483 (next
->vme_start
!= entry
->vme_end
)) {
5484 vm_map_unlock(dst_map
);
5485 return(KERN_INVALID_ADDRESS
);
5489 * Check for permanent objects in the destination.
5491 if ((entry
->object
.vm_object
!= VM_OBJECT_NULL
) &&
5492 ((!entry
->object
.vm_object
->internal
) ||
5493 (entry
->object
.vm_object
->true_share
))) {
5494 if(encountered_sub_map
) {
5495 vm_map_unlock(dst_map
);
5496 return(KERN_FAILURE
);
5503 vm_map_unlock(dst_map
);
5504 return(KERN_SUCCESS
);
5508 * Routine: vm_map_copy_overwrite
5511 * Copy the memory described by the map copy
5512 * object (copy; returned by vm_map_copyin) onto
5513 * the specified destination region (dst_map, dst_addr).
5514 * The destination must be writeable.
5516 * Unlike vm_map_copyout, this routine actually
5517 * writes over previously-mapped memory. If the
5518 * previous mapping was to a permanent (user-supplied)
5519 * memory object, it is preserved.
5521 * The attributes (protection and inheritance) of the
5522 * destination region are preserved.
5524 * If successful, consumes the copy object.
5525 * Otherwise, the caller is responsible for it.
5527 * Implementation notes:
5528 * To overwrite aligned temporary virtual memory, it is
5529 * sufficient to remove the previous mapping and insert
5530 * the new copy. This replacement is done either on
5531 * the whole region (if no permanent virtual memory
5532 * objects are embedded in the destination region) or
5533 * in individual map entries.
5535 * To overwrite permanent virtual memory , it is necessary
5536 * to copy each page, as the external memory management
5537 * interface currently does not provide any optimizations.
5539 * Unaligned memory also has to be copied. It is possible
5540 * to use 'vm_trickery' to copy the aligned data. This is
5541 * not done but not hard to implement.
5543 * Once a page of permanent memory has been overwritten,
5544 * it is impossible to interrupt this function; otherwise,
5545 * the call would be neither atomic nor location-independent.
5546 * The kernel-state portion of a user thread must be
5549 * It may be expensive to forward all requests that might
5550 * overwrite permanent memory (vm_write, vm_copy) to
5551 * uninterruptible kernel threads. This routine may be
5552 * called by interruptible threads; however, success is
5553 * not guaranteed -- if the request cannot be performed
5554 * atomically and interruptibly, an error indication is
5558 static kern_return_t
5559 vm_map_copy_overwrite_nested(
5561 vm_map_address_t dst_addr
,
5563 boolean_t interruptible
,
5566 vm_map_offset_t dst_end
;
5567 vm_map_entry_t tmp_entry
;
5568 vm_map_entry_t entry
;
5570 boolean_t aligned
= TRUE
;
5571 boolean_t contains_permanent_objects
= FALSE
;
5572 boolean_t encountered_sub_map
= FALSE
;
5573 vm_map_offset_t base_addr
;
5574 vm_map_size_t copy_size
;
5575 vm_map_size_t total_size
;
5579 * Check for null copy object.
5582 if (copy
== VM_MAP_COPY_NULL
)
5583 return(KERN_SUCCESS
);
5586 * Check for special kernel buffer allocated
5587 * by new_ipc_kmsg_copyin.
5590 if (copy
->type
== VM_MAP_COPY_KERNEL_BUFFER
) {
5591 return(vm_map_copyout_kernel_buffer(
5597 * Only works for entry lists at the moment. Will
5598 * support page lists later.
5601 assert(copy
->type
== VM_MAP_COPY_ENTRY_LIST
);
5603 if (copy
->size
== 0) {
5604 vm_map_copy_discard(copy
);
5605 return(KERN_SUCCESS
);
5609 * Verify that the destination is all writeable
5610 * initially. We have to trunc the destination
5611 * address and round the copy size or we'll end up
5612 * splitting entries in strange ways.
5615 if (!page_aligned(copy
->size
) ||
5616 !page_aligned (copy
->offset
) ||
5617 !page_aligned (dst_addr
))
5620 dst_end
= vm_map_round_page(dst_addr
+ copy
->size
);
5622 dst_end
= dst_addr
+ copy
->size
;
5625 vm_map_lock(dst_map
);
5627 /* LP64todo - remove this check when vm_map_commpage64()
5628 * no longer has to stuff in a map_entry for the commpage
5629 * above the map's max_offset.
5631 if (dst_addr
>= dst_map
->max_offset
) {
5632 vm_map_unlock(dst_map
);
5633 return(KERN_INVALID_ADDRESS
);
5637 if (!vm_map_lookup_entry(dst_map
, dst_addr
, &tmp_entry
)) {
5638 vm_map_unlock(dst_map
);
5639 return(KERN_INVALID_ADDRESS
);
5641 vm_map_clip_start(dst_map
, tmp_entry
, vm_map_trunc_page(dst_addr
));
5642 for (entry
= tmp_entry
;;) {
5643 vm_map_entry_t next
= entry
->vme_next
;
5645 while(entry
->is_sub_map
) {
5646 vm_map_offset_t sub_start
;
5647 vm_map_offset_t sub_end
;
5648 vm_map_offset_t local_end
;
5650 if (entry
->in_transition
) {
5653 * Say that we are waiting, and wait for entry.
5655 entry
->needs_wakeup
= TRUE
;
5656 vm_map_entry_wait(dst_map
, THREAD_UNINT
);
5661 local_end
= entry
->vme_end
;
5662 if (!(entry
->needs_copy
)) {
5663 /* if needs_copy we are a COW submap */
5664 /* in such a case we just replace so */
5665 /* there is no need for the follow- */
5667 encountered_sub_map
= TRUE
;
5668 sub_start
= entry
->offset
;
5670 if(entry
->vme_end
< dst_end
)
5671 sub_end
= entry
->vme_end
;
5674 sub_end
-= entry
->vme_start
;
5675 sub_end
+= entry
->offset
;
5676 vm_map_unlock(dst_map
);
5678 kr
= vm_map_overwrite_submap_recurse(
5679 entry
->object
.sub_map
,
5681 sub_end
- sub_start
);
5682 if(kr
!= KERN_SUCCESS
)
5684 vm_map_lock(dst_map
);
5687 if (dst_end
<= entry
->vme_end
)
5688 goto start_overwrite
;
5689 if(!vm_map_lookup_entry(dst_map
, local_end
,
5691 vm_map_unlock(dst_map
);
5692 return(KERN_INVALID_ADDRESS
);
5694 next
= entry
->vme_next
;
5697 if ( ! (entry
->protection
& VM_PROT_WRITE
)) {
5698 vm_map_unlock(dst_map
);
5699 return(KERN_PROTECTION_FAILURE
);
5703 * If the entry is in transition, we must wait
5704 * for it to exit that state. Anything could happen
5705 * when we unlock the map, so start over.
5707 if (entry
->in_transition
) {
5710 * Say that we are waiting, and wait for entry.
5712 entry
->needs_wakeup
= TRUE
;
5713 vm_map_entry_wait(dst_map
, THREAD_UNINT
);
5719 * our range is contained completely within this map entry
5721 if (dst_end
<= entry
->vme_end
)
5724 * check that range specified is contiguous region
5726 if ((next
== vm_map_to_entry(dst_map
)) ||
5727 (next
->vme_start
!= entry
->vme_end
)) {
5728 vm_map_unlock(dst_map
);
5729 return(KERN_INVALID_ADDRESS
);
5734 * Check for permanent objects in the destination.
5736 if ((entry
->object
.vm_object
!= VM_OBJECT_NULL
) &&
5737 ((!entry
->object
.vm_object
->internal
) ||
5738 (entry
->object
.vm_object
->true_share
))) {
5739 contains_permanent_objects
= TRUE
;
5747 * If there are permanent objects in the destination, then
5748 * the copy cannot be interrupted.
5751 if (interruptible
&& contains_permanent_objects
) {
5752 vm_map_unlock(dst_map
);
5753 return(KERN_FAILURE
); /* XXX */
5758 * Make a second pass, overwriting the data
5759 * At the beginning of each loop iteration,
5760 * the next entry to be overwritten is "tmp_entry"
5761 * (initially, the value returned from the lookup above),
5762 * and the starting address expected in that entry
5766 total_size
= copy
->size
;
5767 if(encountered_sub_map
) {
5769 /* re-calculate tmp_entry since we've had the map */
5771 if (!vm_map_lookup_entry( dst_map
, dst_addr
, &tmp_entry
)) {
5772 vm_map_unlock(dst_map
);
5773 return(KERN_INVALID_ADDRESS
);
5776 copy_size
= copy
->size
;
5779 base_addr
= dst_addr
;
5781 /* deconstruct the copy object and do in parts */
5782 /* only in sub_map, interruptable case */
5783 vm_map_entry_t copy_entry
;
5784 vm_map_entry_t previous_prev
= VM_MAP_ENTRY_NULL
;
5785 vm_map_entry_t next_copy
= VM_MAP_ENTRY_NULL
;
5787 int remaining_entries
= 0;
5788 vm_map_offset_t new_offset
= 0;
5790 for (entry
= tmp_entry
; copy_size
== 0;) {
5791 vm_map_entry_t next
;
5793 next
= entry
->vme_next
;
5795 /* tmp_entry and base address are moved along */
5796 /* each time we encounter a sub-map. Otherwise */
5797 /* entry can outpase tmp_entry, and the copy_size */
5798 /* may reflect the distance between them */
5799 /* if the current entry is found to be in transition */
5800 /* we will start over at the beginning or the last */
5801 /* encounter of a submap as dictated by base_addr */
5802 /* we will zero copy_size accordingly. */
5803 if (entry
->in_transition
) {
5805 * Say that we are waiting, and wait for entry.
5807 entry
->needs_wakeup
= TRUE
;
5808 vm_map_entry_wait(dst_map
, THREAD_UNINT
);
5810 if(!vm_map_lookup_entry(dst_map
, base_addr
,
5812 vm_map_unlock(dst_map
);
5813 return(KERN_INVALID_ADDRESS
);
5819 if(entry
->is_sub_map
) {
5820 vm_map_offset_t sub_start
;
5821 vm_map_offset_t sub_end
;
5822 vm_map_offset_t local_end
;
5824 if (entry
->needs_copy
) {
5825 /* if this is a COW submap */
5826 /* just back the range with a */
5827 /* anonymous entry */
5828 if(entry
->vme_end
< dst_end
)
5829 sub_end
= entry
->vme_end
;
5832 if(entry
->vme_start
< base_addr
)
5833 sub_start
= base_addr
;
5835 sub_start
= entry
->vme_start
;
5837 dst_map
, entry
, sub_end
);
5839 dst_map
, entry
, sub_start
);
5840 assert(!entry
->use_pmap
);
5841 entry
->is_sub_map
= FALSE
;
5843 entry
->object
.sub_map
);
5844 entry
->object
.sub_map
= NULL
;
5845 entry
->is_shared
= FALSE
;
5846 entry
->needs_copy
= FALSE
;
5850 * We should propagate the protections
5851 * of the submap entry here instead
5852 * of forcing them to VM_PROT_ALL...
5853 * Or better yet, we should inherit
5854 * the protection of the copy_entry.
5856 entry
->protection
= VM_PROT_ALL
;
5857 entry
->max_protection
= VM_PROT_ALL
;
5858 entry
->wired_count
= 0;
5859 entry
->user_wired_count
= 0;
5860 if(entry
->inheritance
5861 == VM_INHERIT_SHARE
)
5862 entry
->inheritance
= VM_INHERIT_COPY
;
5865 /* first take care of any non-sub_map */
5866 /* entries to send */
5867 if(base_addr
< entry
->vme_start
) {
5870 entry
->vme_start
- base_addr
;
5873 sub_start
= entry
->offset
;
5875 if(entry
->vme_end
< dst_end
)
5876 sub_end
= entry
->vme_end
;
5879 sub_end
-= entry
->vme_start
;
5880 sub_end
+= entry
->offset
;
5881 local_end
= entry
->vme_end
;
5882 vm_map_unlock(dst_map
);
5883 copy_size
= sub_end
- sub_start
;
5885 /* adjust the copy object */
5886 if (total_size
> copy_size
) {
5887 vm_map_size_t local_size
= 0;
5888 vm_map_size_t entry_size
;
5891 new_offset
= copy
->offset
;
5892 copy_entry
= vm_map_copy_first_entry(copy
);
5894 vm_map_copy_to_entry(copy
)){
5895 entry_size
= copy_entry
->vme_end
-
5896 copy_entry
->vme_start
;
5897 if((local_size
< copy_size
) &&
5898 ((local_size
+ entry_size
)
5900 vm_map_copy_clip_end(copy
,
5902 copy_entry
->vme_start
+
5903 (copy_size
- local_size
));
5904 entry_size
= copy_entry
->vme_end
-
5905 copy_entry
->vme_start
;
5906 local_size
+= entry_size
;
5907 new_offset
+= entry_size
;
5909 if(local_size
>= copy_size
) {
5910 next_copy
= copy_entry
->vme_next
;
5911 copy_entry
->vme_next
=
5912 vm_map_copy_to_entry(copy
);
5914 copy
->cpy_hdr
.links
.prev
;
5915 copy
->cpy_hdr
.links
.prev
= copy_entry
;
5916 copy
->size
= copy_size
;
5918 copy
->cpy_hdr
.nentries
;
5919 remaining_entries
-= nentries
;
5920 copy
->cpy_hdr
.nentries
= nentries
;
5923 local_size
+= entry_size
;
5924 new_offset
+= entry_size
;
5927 copy_entry
= copy_entry
->vme_next
;
5931 if((entry
->use_pmap
) && (pmap
== NULL
)) {
5932 kr
= vm_map_copy_overwrite_nested(
5933 entry
->object
.sub_map
,
5937 entry
->object
.sub_map
->pmap
);
5938 } else if (pmap
!= NULL
) {
5939 kr
= vm_map_copy_overwrite_nested(
5940 entry
->object
.sub_map
,
5943 interruptible
, pmap
);
5945 kr
= vm_map_copy_overwrite_nested(
5946 entry
->object
.sub_map
,
5952 if(kr
!= KERN_SUCCESS
) {
5953 if(next_copy
!= NULL
) {
5954 copy
->cpy_hdr
.nentries
+=
5956 copy
->cpy_hdr
.links
.prev
->vme_next
=
5958 copy
->cpy_hdr
.links
.prev
5960 copy
->size
= total_size
;
5964 if (dst_end
<= local_end
) {
5965 return(KERN_SUCCESS
);
5967 /* otherwise copy no longer exists, it was */
5968 /* destroyed after successful copy_overwrite */
5969 copy
= (vm_map_copy_t
)
5970 zalloc(vm_map_copy_zone
);
5971 vm_map_copy_first_entry(copy
) =
5972 vm_map_copy_last_entry(copy
) =
5973 vm_map_copy_to_entry(copy
);
5974 copy
->type
= VM_MAP_COPY_ENTRY_LIST
;
5975 copy
->offset
= new_offset
;
5977 total_size
-= copy_size
;
5979 /* put back remainder of copy in container */
5980 if(next_copy
!= NULL
) {
5981 copy
->cpy_hdr
.nentries
= remaining_entries
;
5982 copy
->cpy_hdr
.links
.next
= next_copy
;
5983 copy
->cpy_hdr
.links
.prev
= previous_prev
;
5984 copy
->size
= total_size
;
5985 next_copy
->vme_prev
=
5986 vm_map_copy_to_entry(copy
);
5989 base_addr
= local_end
;
5990 vm_map_lock(dst_map
);
5991 if(!vm_map_lookup_entry(dst_map
,
5992 local_end
, &tmp_entry
)) {
5993 vm_map_unlock(dst_map
);
5994 return(KERN_INVALID_ADDRESS
);
5999 if (dst_end
<= entry
->vme_end
) {
6000 copy_size
= dst_end
- base_addr
;
6004 if ((next
== vm_map_to_entry(dst_map
)) ||
6005 (next
->vme_start
!= entry
->vme_end
)) {
6006 vm_map_unlock(dst_map
);
6007 return(KERN_INVALID_ADDRESS
);
6016 /* adjust the copy object */
6017 if (total_size
> copy_size
) {
6018 vm_map_size_t local_size
= 0;
6019 vm_map_size_t entry_size
;
6021 new_offset
= copy
->offset
;
6022 copy_entry
= vm_map_copy_first_entry(copy
);
6023 while(copy_entry
!= vm_map_copy_to_entry(copy
)) {
6024 entry_size
= copy_entry
->vme_end
-
6025 copy_entry
->vme_start
;
6026 if((local_size
< copy_size
) &&
6027 ((local_size
+ entry_size
)
6029 vm_map_copy_clip_end(copy
, copy_entry
,
6030 copy_entry
->vme_start
+
6031 (copy_size
- local_size
));
6032 entry_size
= copy_entry
->vme_end
-
6033 copy_entry
->vme_start
;
6034 local_size
+= entry_size
;
6035 new_offset
+= entry_size
;
6037 if(local_size
>= copy_size
) {
6038 next_copy
= copy_entry
->vme_next
;
6039 copy_entry
->vme_next
=
6040 vm_map_copy_to_entry(copy
);
6042 copy
->cpy_hdr
.links
.prev
;
6043 copy
->cpy_hdr
.links
.prev
= copy_entry
;
6044 copy
->size
= copy_size
;
6046 copy
->cpy_hdr
.nentries
;
6047 remaining_entries
-= nentries
;
6048 copy
->cpy_hdr
.nentries
= nentries
;
6051 local_size
+= entry_size
;
6052 new_offset
+= entry_size
;
6055 copy_entry
= copy_entry
->vme_next
;
6065 local_pmap
= dst_map
->pmap
;
6067 if ((kr
= vm_map_copy_overwrite_aligned(
6068 dst_map
, tmp_entry
, copy
,
6069 base_addr
, local_pmap
)) != KERN_SUCCESS
) {
6070 if(next_copy
!= NULL
) {
6071 copy
->cpy_hdr
.nentries
+=
6073 copy
->cpy_hdr
.links
.prev
->vme_next
=
6075 copy
->cpy_hdr
.links
.prev
=
6077 copy
->size
+= copy_size
;
6081 vm_map_unlock(dst_map
);
6086 * if the copy and dst address are misaligned but the same
6087 * offset within the page we can copy_not_aligned the
6088 * misaligned parts and copy aligned the rest. If they are
6089 * aligned but len is unaligned we simply need to copy
6090 * the end bit unaligned. We'll need to split the misaligned
6091 * bits of the region in this case !
6093 /* ALWAYS UNLOCKS THE dst_map MAP */
6094 if ((kr
= vm_map_copy_overwrite_unaligned( dst_map
,
6095 tmp_entry
, copy
, base_addr
)) != KERN_SUCCESS
) {
6096 if(next_copy
!= NULL
) {
6097 copy
->cpy_hdr
.nentries
+=
6099 copy
->cpy_hdr
.links
.prev
->vme_next
=
6101 copy
->cpy_hdr
.links
.prev
=
6103 copy
->size
+= copy_size
;
6108 total_size
-= copy_size
;
6111 base_addr
+= copy_size
;
6113 copy
->offset
= new_offset
;
6114 if(next_copy
!= NULL
) {
6115 copy
->cpy_hdr
.nentries
= remaining_entries
;
6116 copy
->cpy_hdr
.links
.next
= next_copy
;
6117 copy
->cpy_hdr
.links
.prev
= previous_prev
;
6118 next_copy
->vme_prev
= vm_map_copy_to_entry(copy
);
6119 copy
->size
= total_size
;
6121 vm_map_lock(dst_map
);
6123 if (!vm_map_lookup_entry(dst_map
,
6124 base_addr
, &tmp_entry
)) {
6125 vm_map_unlock(dst_map
);
6126 return(KERN_INVALID_ADDRESS
);
6128 if (tmp_entry
->in_transition
) {
6129 entry
->needs_wakeup
= TRUE
;
6130 vm_map_entry_wait(dst_map
, THREAD_UNINT
);
6135 vm_map_clip_start(dst_map
, tmp_entry
, vm_map_trunc_page(base_addr
));
6141 * Throw away the vm_map_copy object
6143 vm_map_copy_discard(copy
);
6145 return(KERN_SUCCESS
);
6146 }/* vm_map_copy_overwrite */
6149 vm_map_copy_overwrite(
6151 vm_map_offset_t dst_addr
,
6153 boolean_t interruptible
)
6155 return vm_map_copy_overwrite_nested(
6156 dst_map
, dst_addr
, copy
, interruptible
, (pmap_t
) NULL
);
6161 * Routine: vm_map_copy_overwrite_unaligned [internal use only]
6164 * Physically copy unaligned data
6167 * Unaligned parts of pages have to be physically copied. We use
6168 * a modified form of vm_fault_copy (which understands none-aligned
6169 * page offsets and sizes) to do the copy. We attempt to copy as
6170 * much memory in one go as possibly, however vm_fault_copy copies
6171 * within 1 memory object so we have to find the smaller of "amount left"
6172 * "source object data size" and "target object data size". With
6173 * unaligned data we don't need to split regions, therefore the source
6174 * (copy) object should be one map entry, the target range may be split
6175 * over multiple map entries however. In any event we are pessimistic
6176 * about these assumptions.
6179 * dst_map is locked on entry and is return locked on success,
6180 * unlocked on error.
6183 static kern_return_t
6184 vm_map_copy_overwrite_unaligned(
6186 vm_map_entry_t entry
,
6188 vm_map_offset_t start
)
6190 vm_map_entry_t copy_entry
= vm_map_copy_first_entry(copy
);
6191 vm_map_version_t version
;
6192 vm_object_t dst_object
;
6193 vm_object_offset_t dst_offset
;
6194 vm_object_offset_t src_offset
;
6195 vm_object_offset_t entry_offset
;
6196 vm_map_offset_t entry_end
;
6197 vm_map_size_t src_size
,
6201 kern_return_t kr
= KERN_SUCCESS
;
6203 vm_map_lock_write_to_read(dst_map
);
6205 src_offset
= copy
->offset
- vm_object_trunc_page(copy
->offset
);
6206 amount_left
= copy
->size
;
6208 * unaligned so we never clipped this entry, we need the offset into
6209 * the vm_object not just the data.
6211 while (amount_left
> 0) {
6213 if (entry
== vm_map_to_entry(dst_map
)) {
6214 vm_map_unlock_read(dst_map
);
6215 return KERN_INVALID_ADDRESS
;
6218 /* "start" must be within the current map entry */
6219 assert ((start
>=entry
->vme_start
) && (start
<entry
->vme_end
));
6221 dst_offset
= start
- entry
->vme_start
;
6223 dst_size
= entry
->vme_end
- start
;
6225 src_size
= copy_entry
->vme_end
-
6226 (copy_entry
->vme_start
+ src_offset
);
6228 if (dst_size
< src_size
) {
6230 * we can only copy dst_size bytes before
6231 * we have to get the next destination entry
6233 copy_size
= dst_size
;
6236 * we can only copy src_size bytes before
6237 * we have to get the next source copy entry
6239 copy_size
= src_size
;
6242 if (copy_size
> amount_left
) {
6243 copy_size
= amount_left
;
6246 * Entry needs copy, create a shadow shadow object for
6247 * Copy on write region.
6249 if (entry
->needs_copy
&&
6250 ((entry
->protection
& VM_PROT_WRITE
) != 0))
6252 if (vm_map_lock_read_to_write(dst_map
)) {
6253 vm_map_lock_read(dst_map
);
6256 vm_object_shadow(&entry
->object
.vm_object
,
6258 (vm_map_size_t
)(entry
->vme_end
6259 - entry
->vme_start
));
6260 entry
->needs_copy
= FALSE
;
6261 vm_map_lock_write_to_read(dst_map
);
6263 dst_object
= entry
->object
.vm_object
;
6265 * unlike with the virtual (aligned) copy we're going
6266 * to fault on it therefore we need a target object.
6268 if (dst_object
== VM_OBJECT_NULL
) {
6269 if (vm_map_lock_read_to_write(dst_map
)) {
6270 vm_map_lock_read(dst_map
);
6273 dst_object
= vm_object_allocate((vm_map_size_t
)
6274 entry
->vme_end
- entry
->vme_start
);
6275 entry
->object
.vm_object
= dst_object
;
6277 vm_map_lock_write_to_read(dst_map
);
6280 * Take an object reference and unlock map. The "entry" may
6281 * disappear or change when the map is unlocked.
6283 vm_object_reference(dst_object
);
6284 version
.main_timestamp
= dst_map
->timestamp
;
6285 entry_offset
= entry
->offset
;
6286 entry_end
= entry
->vme_end
;
6287 vm_map_unlock_read(dst_map
);
6289 * Copy as much as possible in one pass
6292 copy_entry
->object
.vm_object
,
6293 copy_entry
->offset
+ src_offset
,
6296 entry_offset
+ dst_offset
,
6302 src_offset
+= copy_size
;
6303 amount_left
-= copy_size
;
6305 * Release the object reference
6307 vm_object_deallocate(dst_object
);
6309 * If a hard error occurred, return it now
6311 if (kr
!= KERN_SUCCESS
)
6314 if ((copy_entry
->vme_start
+ src_offset
) == copy_entry
->vme_end
6315 || amount_left
== 0)
6318 * all done with this copy entry, dispose.
6320 vm_map_copy_entry_unlink(copy
, copy_entry
);
6321 vm_object_deallocate(copy_entry
->object
.vm_object
);
6322 vm_map_copy_entry_dispose(copy
, copy_entry
);
6324 if ((copy_entry
= vm_map_copy_first_entry(copy
))
6325 == vm_map_copy_to_entry(copy
) && amount_left
) {
6327 * not finished copying but run out of source
6329 return KERN_INVALID_ADDRESS
;
6334 if (amount_left
== 0)
6335 return KERN_SUCCESS
;
6337 vm_map_lock_read(dst_map
);
6338 if (version
.main_timestamp
== dst_map
->timestamp
) {
6339 if (start
== entry_end
) {
6341 * destination region is split. Use the version
6342 * information to avoid a lookup in the normal
6345 entry
= entry
->vme_next
;
6347 * should be contiguous. Fail if we encounter
6348 * a hole in the destination.
6350 if (start
!= entry
->vme_start
) {
6351 vm_map_unlock_read(dst_map
);
6352 return KERN_INVALID_ADDRESS
;
6357 * Map version check failed.
6358 * we must lookup the entry because somebody
6359 * might have changed the map behind our backs.
6362 if (!vm_map_lookup_entry(dst_map
, start
, &entry
))
6364 vm_map_unlock_read(dst_map
);
6365 return KERN_INVALID_ADDRESS
;
6370 return KERN_SUCCESS
;
6371 }/* vm_map_copy_overwrite_unaligned */
6374 * Routine: vm_map_copy_overwrite_aligned [internal use only]
6377 * Does all the vm_trickery possible for whole pages.
6381 * If there are no permanent objects in the destination,
6382 * and the source and destination map entry zones match,
6383 * and the destination map entry is not shared,
6384 * then the map entries can be deleted and replaced
6385 * with those from the copy. The following code is the
6386 * basic idea of what to do, but there are lots of annoying
6387 * little details about getting protection and inheritance
6388 * right. Should add protection, inheritance, and sharing checks
6389 * to the above pass and make sure that no wiring is involved.
6392 static kern_return_t
6393 vm_map_copy_overwrite_aligned(
6395 vm_map_entry_t tmp_entry
,
6397 vm_map_offset_t start
,
6398 __unused pmap_t pmap
)
6401 vm_map_entry_t copy_entry
;
6402 vm_map_size_t copy_size
;
6404 vm_map_entry_t entry
;
6406 while ((copy_entry
= vm_map_copy_first_entry(copy
))
6407 != vm_map_copy_to_entry(copy
))
6409 copy_size
= (copy_entry
->vme_end
- copy_entry
->vme_start
);
6412 assert(!entry
->use_pmap
); /* unnested when clipped earlier */
6413 if (entry
== vm_map_to_entry(dst_map
)) {
6414 vm_map_unlock(dst_map
);
6415 return KERN_INVALID_ADDRESS
;
6417 size
= (entry
->vme_end
- entry
->vme_start
);
6419 * Make sure that no holes popped up in the
6420 * address map, and that the protection is
6421 * still valid, in case the map was unlocked
6425 if ((entry
->vme_start
!= start
) || ((entry
->is_sub_map
)
6426 && !entry
->needs_copy
)) {
6427 vm_map_unlock(dst_map
);
6428 return(KERN_INVALID_ADDRESS
);
6430 assert(entry
!= vm_map_to_entry(dst_map
));
6433 * Check protection again
6436 if ( ! (entry
->protection
& VM_PROT_WRITE
)) {
6437 vm_map_unlock(dst_map
);
6438 return(KERN_PROTECTION_FAILURE
);
6442 * Adjust to source size first
6445 if (copy_size
< size
) {
6446 vm_map_clip_end(dst_map
, entry
, entry
->vme_start
+ copy_size
);
6451 * Adjust to destination size
6454 if (size
< copy_size
) {
6455 vm_map_copy_clip_end(copy
, copy_entry
,
6456 copy_entry
->vme_start
+ size
);
6460 assert((entry
->vme_end
- entry
->vme_start
) == size
);
6461 assert((tmp_entry
->vme_end
- tmp_entry
->vme_start
) == size
);
6462 assert((copy_entry
->vme_end
- copy_entry
->vme_start
) == size
);
6465 * If the destination contains temporary unshared memory,
6466 * we can perform the copy by throwing it away and
6467 * installing the source data.
6470 object
= entry
->object
.vm_object
;
6471 if ((!entry
->is_shared
&&
6472 ((object
== VM_OBJECT_NULL
) ||
6473 (object
->internal
&& !object
->true_share
))) ||
6474 entry
->needs_copy
) {
6475 vm_object_t old_object
= entry
->object
.vm_object
;
6476 vm_object_offset_t old_offset
= entry
->offset
;
6477 vm_object_offset_t offset
;
6480 * Ensure that the source and destination aren't
6483 if (old_object
== copy_entry
->object
.vm_object
&&
6484 old_offset
== copy_entry
->offset
) {
6485 vm_map_copy_entry_unlink(copy
, copy_entry
);
6486 vm_map_copy_entry_dispose(copy
, copy_entry
);
6488 if (old_object
!= VM_OBJECT_NULL
)
6489 vm_object_deallocate(old_object
);
6491 start
= tmp_entry
->vme_end
;
6492 tmp_entry
= tmp_entry
->vme_next
;
6496 if (old_object
!= VM_OBJECT_NULL
) {
6497 if(entry
->is_sub_map
) {
6498 if(entry
->use_pmap
) {
6499 #ifndef NO_NESTED_PMAP
6500 pmap_unnest(dst_map
->pmap
,
6501 (addr64_t
)entry
->vme_start
,
6502 entry
->vme_end
- entry
->vme_start
);
6503 #endif /* NO_NESTED_PMAP */
6504 if(dst_map
->mapped
) {
6505 /* clean up parent */
6507 vm_map_submap_pmap_clean(
6508 dst_map
, entry
->vme_start
,
6510 entry
->object
.sub_map
,
6514 vm_map_submap_pmap_clean(
6515 dst_map
, entry
->vme_start
,
6517 entry
->object
.sub_map
,
6521 entry
->object
.sub_map
);
6523 if(dst_map
->mapped
) {
6524 vm_object_pmap_protect(
6525 entry
->object
.vm_object
,
6533 pmap_remove(dst_map
->pmap
,
6534 (addr64_t
)(entry
->vme_start
),
6535 (addr64_t
)(entry
->vme_end
));
6537 vm_object_deallocate(old_object
);
6541 entry
->is_sub_map
= FALSE
;
6542 entry
->object
= copy_entry
->object
;
6543 object
= entry
->object
.vm_object
;
6544 entry
->needs_copy
= copy_entry
->needs_copy
;
6545 entry
->wired_count
= 0;
6546 entry
->user_wired_count
= 0;
6547 offset
= entry
->offset
= copy_entry
->offset
;
6549 vm_map_copy_entry_unlink(copy
, copy_entry
);
6550 vm_map_copy_entry_dispose(copy
, copy_entry
);
6553 * we could try to push pages into the pmap at this point, BUT
6554 * this optimization only saved on average 2 us per page if ALL
6555 * the pages in the source were currently mapped
6556 * and ALL the pages in the dest were touched, if there were fewer
6557 * than 2/3 of the pages touched, this optimization actually cost more cycles
6558 * it also puts a lot of pressure on the pmap layer w/r to mapping structures
6562 * Set up for the next iteration. The map
6563 * has not been unlocked, so the next
6564 * address should be at the end of this
6565 * entry, and the next map entry should be
6566 * the one following it.
6569 start
= tmp_entry
->vme_end
;
6570 tmp_entry
= tmp_entry
->vme_next
;
6572 vm_map_version_t version
;
6573 vm_object_t dst_object
= entry
->object
.vm_object
;
6574 vm_object_offset_t dst_offset
= entry
->offset
;
6578 * Take an object reference, and record
6579 * the map version information so that the
6580 * map can be safely unlocked.
6583 vm_object_reference(dst_object
);
6585 /* account for unlock bumping up timestamp */
6586 version
.main_timestamp
= dst_map
->timestamp
+ 1;
6588 vm_map_unlock(dst_map
);
6591 * Copy as much as possible in one pass
6596 copy_entry
->object
.vm_object
,
6606 * Release the object reference
6609 vm_object_deallocate(dst_object
);
6612 * If a hard error occurred, return it now
6615 if (r
!= KERN_SUCCESS
)
6618 if (copy_size
!= 0) {
6620 * Dispose of the copied region
6623 vm_map_copy_clip_end(copy
, copy_entry
,
6624 copy_entry
->vme_start
+ copy_size
);
6625 vm_map_copy_entry_unlink(copy
, copy_entry
);
6626 vm_object_deallocate(copy_entry
->object
.vm_object
);
6627 vm_map_copy_entry_dispose(copy
, copy_entry
);
6631 * Pick up in the destination map where we left off.
6633 * Use the version information to avoid a lookup
6634 * in the normal case.
6638 vm_map_lock(dst_map
);
6639 if (version
.main_timestamp
== dst_map
->timestamp
) {
6640 /* We can safely use saved tmp_entry value */
6642 vm_map_clip_end(dst_map
, tmp_entry
, start
);
6643 tmp_entry
= tmp_entry
->vme_next
;
6645 /* Must do lookup of tmp_entry */
6647 if (!vm_map_lookup_entry(dst_map
, start
, &tmp_entry
)) {
6648 vm_map_unlock(dst_map
);
6649 return(KERN_INVALID_ADDRESS
);
6651 vm_map_clip_start(dst_map
, tmp_entry
, start
);
6656 return(KERN_SUCCESS
);
6657 }/* vm_map_copy_overwrite_aligned */
6660 * Routine: vm_map_copyin_kernel_buffer [internal use only]
6663 * Copy in data to a kernel buffer from space in the
6664 * source map. The original space may be optionally
6667 * If successful, returns a new copy object.
6669 static kern_return_t
6670 vm_map_copyin_kernel_buffer(
6672 vm_map_offset_t src_addr
,
6674 boolean_t src_destroy
,
6675 vm_map_copy_t
*copy_result
)
6679 vm_size_t kalloc_size
;
6681 if ((vm_size_t
) len
!= len
) {
6682 /* "len" is too big and doesn't fit in a "vm_size_t" */
6683 return KERN_RESOURCE_SHORTAGE
;
6685 kalloc_size
= (vm_size_t
) (sizeof(struct vm_map_copy
) + len
);
6686 assert((vm_map_size_t
) kalloc_size
== sizeof (struct vm_map_copy
) + len
);
6688 copy
= (vm_map_copy_t
) kalloc(kalloc_size
);
6689 if (copy
== VM_MAP_COPY_NULL
) {
6690 return KERN_RESOURCE_SHORTAGE
;
6692 copy
->type
= VM_MAP_COPY_KERNEL_BUFFER
;
6695 copy
->cpy_kdata
= (void *) (copy
+ 1);
6696 copy
->cpy_kalloc_size
= kalloc_size
;
6698 kr
= copyinmap(src_map
, src_addr
, copy
->cpy_kdata
, (vm_size_t
) len
);
6699 if (kr
!= KERN_SUCCESS
) {
6700 kfree(copy
, kalloc_size
);
6704 (void) vm_map_remove(src_map
, vm_map_trunc_page(src_addr
),
6705 vm_map_round_page(src_addr
+ len
),
6706 VM_MAP_REMOVE_INTERRUPTIBLE
|
6707 VM_MAP_REMOVE_WAIT_FOR_KWIRE
|
6708 (src_map
== kernel_map
) ?
6709 VM_MAP_REMOVE_KUNWIRE
: 0);
6711 *copy_result
= copy
;
6712 return KERN_SUCCESS
;
6716 * Routine: vm_map_copyout_kernel_buffer [internal use only]
6719 * Copy out data from a kernel buffer into space in the
6720 * destination map. The space may be otpionally dynamically
6723 * If successful, consumes the copy object.
6724 * Otherwise, the caller is responsible for it.
6726 static int vm_map_copyout_kernel_buffer_failures
= 0;
6727 static kern_return_t
6728 vm_map_copyout_kernel_buffer(
6730 vm_map_address_t
*addr
, /* IN/OUT */
6732 boolean_t overwrite
)
6734 kern_return_t kr
= KERN_SUCCESS
;
6735 thread_t thread
= current_thread();
6740 * Allocate space in the target map for the data
6743 kr
= vm_map_enter(map
,
6745 vm_map_round_page(copy
->size
),
6746 (vm_map_offset_t
) 0,
6749 (vm_object_offset_t
) 0,
6753 VM_INHERIT_DEFAULT
);
6754 if (kr
!= KERN_SUCCESS
)
6759 * Copyout the data from the kernel buffer to the target map.
6761 if (thread
->map
== map
) {
6764 * If the target map is the current map, just do
6767 assert((vm_size_t
) copy
->size
== copy
->size
);
6768 if (copyout(copy
->cpy_kdata
, *addr
, (vm_size_t
) copy
->size
)) {
6769 kr
= KERN_INVALID_ADDRESS
;
6776 * If the target map is another map, assume the
6777 * target's address space identity for the duration
6780 vm_map_reference(map
);
6781 oldmap
= vm_map_switch(map
);
6783 assert((vm_size_t
) copy
->size
== copy
->size
);
6784 if (copyout(copy
->cpy_kdata
, *addr
, (vm_size_t
) copy
->size
)) {
6785 vm_map_copyout_kernel_buffer_failures
++;
6786 kr
= KERN_INVALID_ADDRESS
;
6789 (void) vm_map_switch(oldmap
);
6790 vm_map_deallocate(map
);
6793 if (kr
!= KERN_SUCCESS
) {
6794 /* the copy failed, clean up */
6797 * Deallocate the space we allocated in the target map.
6799 (void) vm_map_remove(map
,
6800 vm_map_trunc_page(*addr
),
6801 vm_map_round_page(*addr
+
6802 vm_map_round_page(copy
->size
)),
6807 /* copy was successful, dicard the copy structure */
6808 kfree(copy
, copy
->cpy_kalloc_size
);
6815 * Macro: vm_map_copy_insert
6818 * Link a copy chain ("copy") into a map at the
6819 * specified location (after "where").
6821 * The copy chain is destroyed.
6823 * The arguments are evaluated multiple times.
6825 #define vm_map_copy_insert(map, where, copy) \
6827 vm_map_t VMCI_map; \
6828 vm_map_entry_t VMCI_where; \
6829 vm_map_copy_t VMCI_copy; \
6831 VMCI_where = (where); \
6832 VMCI_copy = (copy); \
6833 ((VMCI_where->vme_next)->vme_prev = vm_map_copy_last_entry(VMCI_copy))\
6834 ->vme_next = (VMCI_where->vme_next); \
6835 ((VMCI_where)->vme_next = vm_map_copy_first_entry(VMCI_copy)) \
6836 ->vme_prev = VMCI_where; \
6837 VMCI_map->hdr.nentries += VMCI_copy->cpy_hdr.nentries; \
6838 UPDATE_FIRST_FREE(VMCI_map, VMCI_map->first_free); \
6839 zfree(vm_map_copy_zone, VMCI_copy); \
6843 * Routine: vm_map_copyout
6846 * Copy out a copy chain ("copy") into newly-allocated
6847 * space in the destination map.
6849 * If successful, consumes the copy object.
6850 * Otherwise, the caller is responsible for it.
6855 vm_map_address_t
*dst_addr
, /* OUT */
6859 vm_map_size_t adjustment
;
6860 vm_map_offset_t start
;
6861 vm_object_offset_t vm_copy_start
;
6862 vm_map_entry_t last
;
6864 vm_map_entry_t entry
;
6867 * Check for null copy object.
6870 if (copy
== VM_MAP_COPY_NULL
) {
6872 return(KERN_SUCCESS
);
6876 * Check for special copy object, created
6877 * by vm_map_copyin_object.
6880 if (copy
->type
== VM_MAP_COPY_OBJECT
) {
6881 vm_object_t object
= copy
->cpy_object
;
6883 vm_object_offset_t offset
;
6885 offset
= vm_object_trunc_page(copy
->offset
);
6886 size
= vm_map_round_page(copy
->size
+
6887 (vm_map_size_t
)(copy
->offset
- offset
));
6889 kr
= vm_map_enter(dst_map
, dst_addr
, size
,
6890 (vm_map_offset_t
) 0, VM_FLAGS_ANYWHERE
,
6891 object
, offset
, FALSE
,
6892 VM_PROT_DEFAULT
, VM_PROT_ALL
,
6893 VM_INHERIT_DEFAULT
);
6894 if (kr
!= KERN_SUCCESS
)
6896 /* Account for non-pagealigned copy object */
6897 *dst_addr
+= (vm_map_offset_t
)(copy
->offset
- offset
);
6898 zfree(vm_map_copy_zone
, copy
);
6899 return(KERN_SUCCESS
);
6903 * Check for special kernel buffer allocated
6904 * by new_ipc_kmsg_copyin.
6907 if (copy
->type
== VM_MAP_COPY_KERNEL_BUFFER
) {
6908 return(vm_map_copyout_kernel_buffer(dst_map
, dst_addr
,
6913 * Find space for the data
6916 vm_copy_start
= vm_object_trunc_page(copy
->offset
);
6917 size
= vm_map_round_page((vm_map_size_t
)copy
->offset
+ copy
->size
)
6922 vm_map_lock(dst_map
);
6923 assert(first_free_is_valid(dst_map
));
6924 start
= ((last
= dst_map
->first_free
) == vm_map_to_entry(dst_map
)) ?
6925 vm_map_min(dst_map
) : last
->vme_end
;
6928 vm_map_entry_t next
= last
->vme_next
;
6929 vm_map_offset_t end
= start
+ size
;
6931 if ((end
> dst_map
->max_offset
) || (end
< start
)) {
6932 if (dst_map
->wait_for_space
) {
6933 if (size
<= (dst_map
->max_offset
- dst_map
->min_offset
)) {
6934 assert_wait((event_t
) dst_map
,
6935 THREAD_INTERRUPTIBLE
);
6936 vm_map_unlock(dst_map
);
6937 thread_block(THREAD_CONTINUE_NULL
);
6941 vm_map_unlock(dst_map
);
6942 return(KERN_NO_SPACE
);
6945 if ((next
== vm_map_to_entry(dst_map
)) ||
6946 (next
->vme_start
>= end
))
6950 start
= last
->vme_end
;
6954 * Since we're going to just drop the map
6955 * entries from the copy into the destination
6956 * map, they must come from the same pool.
6959 if (copy
->cpy_hdr
.entries_pageable
!= dst_map
->hdr
.entries_pageable
) {
6961 * Mismatches occur when dealing with the default
6965 vm_map_entry_t next
, new;
6968 * Find the zone that the copies were allocated from
6970 old_zone
= (copy
->cpy_hdr
.entries_pageable
)
6972 : vm_map_kentry_zone
;
6973 entry
= vm_map_copy_first_entry(copy
);
6976 * Reinitialize the copy so that vm_map_copy_entry_link
6979 copy
->cpy_hdr
.nentries
= 0;
6980 copy
->cpy_hdr
.entries_pageable
= dst_map
->hdr
.entries_pageable
;
6981 vm_map_copy_first_entry(copy
) =
6982 vm_map_copy_last_entry(copy
) =
6983 vm_map_copy_to_entry(copy
);
6988 while (entry
!= vm_map_copy_to_entry(copy
)) {
6989 new = vm_map_copy_entry_create(copy
);
6990 vm_map_entry_copy_full(new, entry
);
6991 new->use_pmap
= FALSE
; /* clr address space specifics */
6992 vm_map_copy_entry_link(copy
,
6993 vm_map_copy_last_entry(copy
),
6995 next
= entry
->vme_next
;
6996 zfree(old_zone
, entry
);
7002 * Adjust the addresses in the copy chain, and
7003 * reset the region attributes.
7006 adjustment
= start
- vm_copy_start
;
7007 for (entry
= vm_map_copy_first_entry(copy
);
7008 entry
!= vm_map_copy_to_entry(copy
);
7009 entry
= entry
->vme_next
) {
7010 entry
->vme_start
+= adjustment
;
7011 entry
->vme_end
+= adjustment
;
7013 entry
->inheritance
= VM_INHERIT_DEFAULT
;
7014 entry
->protection
= VM_PROT_DEFAULT
;
7015 entry
->max_protection
= VM_PROT_ALL
;
7016 entry
->behavior
= VM_BEHAVIOR_DEFAULT
;
7019 * If the entry is now wired,
7020 * map the pages into the destination map.
7022 if (entry
->wired_count
!= 0) {
7023 register vm_map_offset_t va
;
7024 vm_object_offset_t offset
;
7025 register vm_object_t object
;
7029 object
= entry
->object
.vm_object
;
7030 offset
= entry
->offset
;
7031 va
= entry
->vme_start
;
7033 pmap_pageable(dst_map
->pmap
,
7038 while (va
< entry
->vme_end
) {
7039 register vm_page_t m
;
7042 * Look up the page in the object.
7043 * Assert that the page will be found in the
7046 * the object was newly created by
7047 * vm_object_copy_slowly, and has
7048 * copies of all of the pages from
7051 * the object was moved from the old
7052 * map entry; because the old map
7053 * entry was wired, all of the pages
7054 * were in the top-level object.
7055 * (XXX not true if we wire pages for
7058 vm_object_lock(object
);
7060 m
= vm_page_lookup(object
, offset
);
7061 if (m
== VM_PAGE_NULL
|| !VM_PAGE_WIRED(m
) ||
7063 panic("vm_map_copyout: wiring %p", m
);
7067 * The page is assumed to be wired here, so it
7068 * shouldn't be encrypted. Otherwise, we
7069 * couldn't enter it in the page table, since
7070 * we don't want the user to see the encrypted
7073 ASSERT_PAGE_DECRYPTED(m
);
7075 prot
= entry
->protection
;
7077 if (override_nx(dst_map
, entry
->alias
) && prot
)
7078 prot
|= VM_PROT_EXECUTE
;
7080 type_of_fault
= DBG_CACHE_HIT_FAULT
;
7082 vm_fault_enter(m
, dst_map
->pmap
, va
, prot
,
7083 VM_PAGE_WIRED(m
), FALSE
, FALSE
,
7086 vm_object_unlock(object
);
7088 offset
+= PAGE_SIZE_64
;
7095 * Correct the page alignment for the result
7098 *dst_addr
= start
+ (copy
->offset
- vm_copy_start
);
7101 * Update the hints and the map size
7104 SAVE_HINT_MAP_WRITE(dst_map
, vm_map_copy_last_entry(copy
));
7106 dst_map
->size
+= size
;
7112 vm_map_copy_insert(dst_map
, last
, copy
);
7114 vm_map_unlock(dst_map
);
7117 * XXX If wiring_required, call vm_map_pageable
7120 return(KERN_SUCCESS
);
7124 * Routine: vm_map_copyin
7127 * see vm_map_copyin_common. Exported via Unsupported.exports.
7131 #undef vm_map_copyin
7136 vm_map_address_t src_addr
,
7138 boolean_t src_destroy
,
7139 vm_map_copy_t
*copy_result
) /* OUT */
7141 return(vm_map_copyin_common(src_map
, src_addr
, len
, src_destroy
,
7142 FALSE
, copy_result
, FALSE
));
7146 * Routine: vm_map_copyin_common
7149 * Copy the specified region (src_addr, len) from the
7150 * source address space (src_map), possibly removing
7151 * the region from the source address space (src_destroy).
7154 * A vm_map_copy_t object (copy_result), suitable for
7155 * insertion into another address space (using vm_map_copyout),
7156 * copying over another address space region (using
7157 * vm_map_copy_overwrite). If the copy is unused, it
7158 * should be destroyed (using vm_map_copy_discard).
7160 * In/out conditions:
7161 * The source map should not be locked on entry.
7164 typedef struct submap_map
{
7165 vm_map_t parent_map
;
7166 vm_map_offset_t base_start
;
7167 vm_map_offset_t base_end
;
7168 vm_map_size_t base_len
;
7169 struct submap_map
*next
;
7173 vm_map_copyin_common(
7175 vm_map_address_t src_addr
,
7177 boolean_t src_destroy
,
7178 __unused boolean_t src_volatile
,
7179 vm_map_copy_t
*copy_result
, /* OUT */
7180 boolean_t use_maxprot
)
7182 vm_map_entry_t tmp_entry
; /* Result of last map lookup --
7183 * in multi-level lookup, this
7184 * entry contains the actual
7188 vm_map_entry_t new_entry
= VM_MAP_ENTRY_NULL
; /* Map entry for copy */
7190 vm_map_offset_t src_start
; /* Start of current entry --
7191 * where copy is taking place now
7193 vm_map_offset_t src_end
; /* End of entire region to be
7195 vm_map_offset_t src_base
;
7196 vm_map_t base_map
= src_map
;
7197 boolean_t map_share
=FALSE
;
7198 submap_map_t
*parent_maps
= NULL
;
7201 vm_map_copy_t copy
; /* Resulting copy */
7202 vm_map_address_t copy_addr
;
7205 * Check for copies of zero bytes.
7209 *copy_result
= VM_MAP_COPY_NULL
;
7210 return(KERN_SUCCESS
);
7214 * Check that the end address doesn't overflow
7216 src_end
= src_addr
+ len
;
7217 if (src_end
< src_addr
)
7218 return KERN_INVALID_ADDRESS
;
7221 * If the copy is sufficiently small, use a kernel buffer instead
7222 * of making a virtual copy. The theory being that the cost of
7223 * setting up VM (and taking C-O-W faults) dominates the copy costs
7224 * for small regions.
7226 if ((len
< msg_ool_size_small
) && !use_maxprot
)
7227 return vm_map_copyin_kernel_buffer(src_map
, src_addr
, len
,
7228 src_destroy
, copy_result
);
7231 * Compute (page aligned) start and end of region
7233 src_start
= vm_map_trunc_page(src_addr
);
7234 src_end
= vm_map_round_page(src_end
);
7236 XPR(XPR_VM_MAP
, "vm_map_copyin_common map 0x%x addr 0x%x len 0x%x dest %d\n", src_map
, src_addr
, len
, src_destroy
, 0);
7239 * Allocate a header element for the list.
7241 * Use the start and end in the header to
7242 * remember the endpoints prior to rounding.
7245 copy
= (vm_map_copy_t
) zalloc(vm_map_copy_zone
);
7246 vm_map_copy_first_entry(copy
) =
7247 vm_map_copy_last_entry(copy
) = vm_map_copy_to_entry(copy
);
7248 copy
->type
= VM_MAP_COPY_ENTRY_LIST
;
7249 copy
->cpy_hdr
.nentries
= 0;
7250 copy
->cpy_hdr
.entries_pageable
= TRUE
;
7252 copy
->offset
= src_addr
;
7255 new_entry
= vm_map_copy_entry_create(copy
);
7259 vm_map_unlock(src_map); \
7260 if(src_map != base_map) \
7261 vm_map_deallocate(src_map); \
7262 if (new_entry != VM_MAP_ENTRY_NULL) \
7263 vm_map_copy_entry_dispose(copy,new_entry); \
7264 vm_map_copy_discard(copy); \
7266 submap_map_t *_ptr; \
7268 for(_ptr = parent_maps; _ptr != NULL; _ptr = parent_maps) { \
7269 parent_maps=parent_maps->next; \
7270 if (_ptr->parent_map != base_map) \
7271 vm_map_deallocate(_ptr->parent_map); \
7272 kfree(_ptr, sizeof(submap_map_t)); \
7279 * Find the beginning of the region.
7282 vm_map_lock(src_map
);
7284 if (!vm_map_lookup_entry(src_map
, src_start
, &tmp_entry
))
7285 RETURN(KERN_INVALID_ADDRESS
);
7286 if(!tmp_entry
->is_sub_map
) {
7287 vm_map_clip_start(src_map
, tmp_entry
, src_start
);
7289 /* set for later submap fix-up */
7290 copy_addr
= src_start
;
7293 * Go through entries until we get to the end.
7298 vm_map_entry_t src_entry
= tmp_entry
; /* Top-level entry */
7299 vm_map_size_t src_size
; /* Size of source
7300 * map entry (in both
7305 vm_object_t src_object
; /* Object to copy */
7306 vm_object_offset_t src_offset
;
7308 boolean_t src_needs_copy
; /* Should source map
7310 * for copy-on-write?
7313 boolean_t new_entry_needs_copy
; /* Will new entry be COW? */
7315 boolean_t was_wired
; /* Was source wired? */
7316 vm_map_version_t version
; /* Version before locks
7317 * dropped to make copy
7319 kern_return_t result
; /* Return value from
7320 * copy_strategically.
7322 while(tmp_entry
->is_sub_map
) {
7323 vm_map_size_t submap_len
;
7326 ptr
= (submap_map_t
*)kalloc(sizeof(submap_map_t
));
7327 ptr
->next
= parent_maps
;
7329 ptr
->parent_map
= src_map
;
7330 ptr
->base_start
= src_start
;
7331 ptr
->base_end
= src_end
;
7332 submap_len
= tmp_entry
->vme_end
- src_start
;
7333 if(submap_len
> (src_end
-src_start
))
7334 submap_len
= src_end
-src_start
;
7335 ptr
->base_len
= submap_len
;
7337 src_start
-= tmp_entry
->vme_start
;
7338 src_start
+= tmp_entry
->offset
;
7339 src_end
= src_start
+ submap_len
;
7340 src_map
= tmp_entry
->object
.sub_map
;
7341 vm_map_lock(src_map
);
7342 /* keep an outstanding reference for all maps in */
7343 /* the parents tree except the base map */
7344 vm_map_reference(src_map
);
7345 vm_map_unlock(ptr
->parent_map
);
7346 if (!vm_map_lookup_entry(
7347 src_map
, src_start
, &tmp_entry
))
7348 RETURN(KERN_INVALID_ADDRESS
);
7350 if(!tmp_entry
->is_sub_map
)
7351 vm_map_clip_start(src_map
, tmp_entry
, src_start
);
7352 src_entry
= tmp_entry
;
7354 /* we are now in the lowest level submap... */
7356 if ((tmp_entry
->object
.vm_object
!= VM_OBJECT_NULL
) &&
7357 (tmp_entry
->object
.vm_object
->phys_contiguous
)) {
7358 /* This is not, supported for now.In future */
7359 /* we will need to detect the phys_contig */
7360 /* condition and then upgrade copy_slowly */
7361 /* to do physical copy from the device mem */
7362 /* based object. We can piggy-back off of */
7363 /* the was wired boolean to set-up the */
7364 /* proper handling */
7365 RETURN(KERN_PROTECTION_FAILURE
);
7368 * Create a new address map entry to hold the result.
7369 * Fill in the fields from the appropriate source entries.
7370 * We must unlock the source map to do this if we need
7371 * to allocate a map entry.
7373 if (new_entry
== VM_MAP_ENTRY_NULL
) {
7374 version
.main_timestamp
= src_map
->timestamp
;
7375 vm_map_unlock(src_map
);
7377 new_entry
= vm_map_copy_entry_create(copy
);
7379 vm_map_lock(src_map
);
7380 if ((version
.main_timestamp
+ 1) != src_map
->timestamp
) {
7381 if (!vm_map_lookup_entry(src_map
, src_start
,
7383 RETURN(KERN_INVALID_ADDRESS
);
7385 if (!tmp_entry
->is_sub_map
)
7386 vm_map_clip_start(src_map
, tmp_entry
, src_start
);
7387 continue; /* restart w/ new tmp_entry */
7392 * Verify that the region can be read.
7394 if (((src_entry
->protection
& VM_PROT_READ
) == VM_PROT_NONE
&&
7396 (src_entry
->max_protection
& VM_PROT_READ
) == 0)
7397 RETURN(KERN_PROTECTION_FAILURE
);
7400 * Clip against the endpoints of the entire region.
7403 vm_map_clip_end(src_map
, src_entry
, src_end
);
7405 src_size
= src_entry
->vme_end
- src_start
;
7406 src_object
= src_entry
->object
.vm_object
;
7407 src_offset
= src_entry
->offset
;
7408 was_wired
= (src_entry
->wired_count
!= 0);
7410 vm_map_entry_copy(new_entry
, src_entry
);
7411 new_entry
->use_pmap
= FALSE
; /* clr address space specifics */
7414 * Attempt non-blocking copy-on-write optimizations.
7418 (src_object
== VM_OBJECT_NULL
||
7419 (src_object
->internal
&& !src_object
->true_share
7422 * If we are destroying the source, and the object
7423 * is internal, we can move the object reference
7424 * from the source to the copy. The copy is
7425 * copy-on-write only if the source is.
7426 * We make another reference to the object, because
7427 * destroying the source entry will deallocate it.
7429 vm_object_reference(src_object
);
7432 * Copy is always unwired. vm_map_copy_entry
7433 * set its wired count to zero.
7436 goto CopySuccessful
;
7441 XPR(XPR_VM_MAP
, "vm_map_copyin_common src_obj 0x%x ent 0x%x obj 0x%x was_wired %d\n",
7442 src_object
, new_entry
, new_entry
->object
.vm_object
,
7444 if ((src_object
== VM_OBJECT_NULL
||
7445 (!was_wired
&& !map_share
&& !tmp_entry
->is_shared
)) &&
7446 vm_object_copy_quickly(
7447 &new_entry
->object
.vm_object
,
7451 &new_entry_needs_copy
)) {
7453 new_entry
->needs_copy
= new_entry_needs_copy
;
7456 * Handle copy-on-write obligations
7459 if (src_needs_copy
&& !tmp_entry
->needs_copy
) {
7462 prot
= src_entry
->protection
& ~VM_PROT_WRITE
;
7464 if (override_nx(src_map
, src_entry
->alias
) && prot
)
7465 prot
|= VM_PROT_EXECUTE
;
7467 vm_object_pmap_protect(
7471 (src_entry
->is_shared
?
7474 src_entry
->vme_start
,
7477 tmp_entry
->needs_copy
= TRUE
;
7481 * The map has never been unlocked, so it's safe
7482 * to move to the next entry rather than doing
7486 goto CopySuccessful
;
7490 * Take an object reference, so that we may
7491 * release the map lock(s).
7494 assert(src_object
!= VM_OBJECT_NULL
);
7495 vm_object_reference(src_object
);
7498 * Record the timestamp for later verification.
7502 version
.main_timestamp
= src_map
->timestamp
;
7503 vm_map_unlock(src_map
); /* Increments timestamp once! */
7511 vm_object_lock(src_object
);
7512 result
= vm_object_copy_slowly(
7517 &new_entry
->object
.vm_object
);
7518 new_entry
->offset
= 0;
7519 new_entry
->needs_copy
= FALSE
;
7522 else if (src_object
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
&&
7523 (tmp_entry
->is_shared
|| map_share
)) {
7524 vm_object_t new_object
;
7526 vm_object_lock_shared(src_object
);
7527 new_object
= vm_object_copy_delayed(
7532 if (new_object
== VM_OBJECT_NULL
)
7535 new_entry
->object
.vm_object
= new_object
;
7536 new_entry
->needs_copy
= TRUE
;
7537 result
= KERN_SUCCESS
;
7540 result
= vm_object_copy_strategically(src_object
,
7543 &new_entry
->object
.vm_object
,
7545 &new_entry_needs_copy
);
7547 new_entry
->needs_copy
= new_entry_needs_copy
;
7550 if (result
!= KERN_SUCCESS
&&
7551 result
!= KERN_MEMORY_RESTART_COPY
) {
7552 vm_map_lock(src_map
);
7557 * Throw away the extra reference
7560 vm_object_deallocate(src_object
);
7563 * Verify that the map has not substantially
7564 * changed while the copy was being made.
7567 vm_map_lock(src_map
);
7569 if ((version
.main_timestamp
+ 1) == src_map
->timestamp
)
7570 goto VerificationSuccessful
;
7573 * Simple version comparison failed.
7575 * Retry the lookup and verify that the
7576 * same object/offset are still present.
7578 * [Note: a memory manager that colludes with
7579 * the calling task can detect that we have
7580 * cheated. While the map was unlocked, the
7581 * mapping could have been changed and restored.]
7584 if (!vm_map_lookup_entry(src_map
, src_start
, &tmp_entry
)) {
7585 RETURN(KERN_INVALID_ADDRESS
);
7588 src_entry
= tmp_entry
;
7589 vm_map_clip_start(src_map
, src_entry
, src_start
);
7591 if ((((src_entry
->protection
& VM_PROT_READ
) == VM_PROT_NONE
) &&
7593 ((src_entry
->max_protection
& VM_PROT_READ
) == 0))
7594 goto VerificationFailed
;
7596 if (src_entry
->vme_end
< new_entry
->vme_end
)
7597 src_size
= (new_entry
->vme_end
= src_entry
->vme_end
) - src_start
;
7599 if ((src_entry
->object
.vm_object
!= src_object
) ||
7600 (src_entry
->offset
!= src_offset
) ) {
7603 * Verification failed.
7605 * Start over with this top-level entry.
7608 VerificationFailed
: ;
7610 vm_object_deallocate(new_entry
->object
.vm_object
);
7611 tmp_entry
= src_entry
;
7616 * Verification succeeded.
7619 VerificationSuccessful
: ;
7621 if (result
== KERN_MEMORY_RESTART_COPY
)
7631 * Link in the new copy entry.
7634 vm_map_copy_entry_link(copy
, vm_map_copy_last_entry(copy
),
7638 * Determine whether the entire region
7641 src_base
= src_start
;
7642 src_start
= new_entry
->vme_end
;
7643 new_entry
= VM_MAP_ENTRY_NULL
;
7644 while ((src_start
>= src_end
) && (src_end
!= 0)) {
7645 if (src_map
!= base_map
) {
7649 assert(ptr
!= NULL
);
7650 parent_maps
= parent_maps
->next
;
7652 /* fix up the damage we did in that submap */
7653 vm_map_simplify_range(src_map
,
7657 vm_map_unlock(src_map
);
7658 vm_map_deallocate(src_map
);
7659 vm_map_lock(ptr
->parent_map
);
7660 src_map
= ptr
->parent_map
;
7661 src_base
= ptr
->base_start
;
7662 src_start
= ptr
->base_start
+ ptr
->base_len
;
7663 src_end
= ptr
->base_end
;
7664 if ((src_end
> src_start
) &&
7665 !vm_map_lookup_entry(
7666 src_map
, src_start
, &tmp_entry
))
7667 RETURN(KERN_INVALID_ADDRESS
);
7668 kfree(ptr
, sizeof(submap_map_t
));
7669 if(parent_maps
== NULL
)
7671 src_entry
= tmp_entry
->vme_prev
;
7675 if ((src_start
>= src_end
) && (src_end
!= 0))
7679 * Verify that there are no gaps in the region
7682 tmp_entry
= src_entry
->vme_next
;
7683 if ((tmp_entry
->vme_start
!= src_start
) ||
7684 (tmp_entry
== vm_map_to_entry(src_map
)))
7685 RETURN(KERN_INVALID_ADDRESS
);
7689 * If the source should be destroyed, do it now, since the
7690 * copy was successful.
7693 (void) vm_map_delete(src_map
,
7694 vm_map_trunc_page(src_addr
),
7696 (src_map
== kernel_map
) ?
7697 VM_MAP_REMOVE_KUNWIRE
:
7701 /* fix up the damage we did in the base map */
7702 vm_map_simplify_range(src_map
,
7703 vm_map_trunc_page(src_addr
),
7704 vm_map_round_page(src_end
));
7707 vm_map_unlock(src_map
);
7709 /* Fix-up start and end points in copy. This is necessary */
7710 /* when the various entries in the copy object were picked */
7711 /* up from different sub-maps */
7713 tmp_entry
= vm_map_copy_first_entry(copy
);
7714 while (tmp_entry
!= vm_map_copy_to_entry(copy
)) {
7715 tmp_entry
->vme_end
= copy_addr
+
7716 (tmp_entry
->vme_end
- tmp_entry
->vme_start
);
7717 tmp_entry
->vme_start
= copy_addr
;
7718 copy_addr
+= tmp_entry
->vme_end
- tmp_entry
->vme_start
;
7719 tmp_entry
= (struct vm_map_entry
*)tmp_entry
->vme_next
;
7722 *copy_result
= copy
;
7723 return(KERN_SUCCESS
);
7729 * vm_map_copyin_object:
7731 * Create a copy object from an object.
7732 * Our caller donates an object reference.
7736 vm_map_copyin_object(
7738 vm_object_offset_t offset
, /* offset of region in object */
7739 vm_object_size_t size
, /* size of region in object */
7740 vm_map_copy_t
*copy_result
) /* OUT */
7742 vm_map_copy_t copy
; /* Resulting copy */
7745 * We drop the object into a special copy object
7746 * that contains the object directly.
7749 copy
= (vm_map_copy_t
) zalloc(vm_map_copy_zone
);
7750 copy
->type
= VM_MAP_COPY_OBJECT
;
7751 copy
->cpy_object
= object
;
7752 copy
->offset
= offset
;
7755 *copy_result
= copy
;
7756 return(KERN_SUCCESS
);
7762 vm_map_entry_t old_entry
,
7766 vm_map_entry_t new_entry
;
7769 * New sharing code. New map entry
7770 * references original object. Internal
7771 * objects use asynchronous copy algorithm for
7772 * future copies. First make sure we have
7773 * the right object. If we need a shadow,
7774 * or someone else already has one, then
7775 * make a new shadow and share it.
7778 object
= old_entry
->object
.vm_object
;
7779 if (old_entry
->is_sub_map
) {
7780 assert(old_entry
->wired_count
== 0);
7781 #ifndef NO_NESTED_PMAP
7782 if(old_entry
->use_pmap
) {
7783 kern_return_t result
;
7785 result
= pmap_nest(new_map
->pmap
,
7786 (old_entry
->object
.sub_map
)->pmap
,
7787 (addr64_t
)old_entry
->vme_start
,
7788 (addr64_t
)old_entry
->vme_start
,
7789 (uint64_t)(old_entry
->vme_end
- old_entry
->vme_start
));
7791 panic("vm_map_fork_share: pmap_nest failed!");
7793 #endif /* NO_NESTED_PMAP */
7794 } else if (object
== VM_OBJECT_NULL
) {
7795 object
= vm_object_allocate((vm_map_size_t
)(old_entry
->vme_end
-
7796 old_entry
->vme_start
));
7797 old_entry
->offset
= 0;
7798 old_entry
->object
.vm_object
= object
;
7799 assert(!old_entry
->needs_copy
);
7800 } else if (object
->copy_strategy
!=
7801 MEMORY_OBJECT_COPY_SYMMETRIC
) {
7804 * We are already using an asymmetric
7805 * copy, and therefore we already have
7809 assert(! old_entry
->needs_copy
);
7811 else if (old_entry
->needs_copy
|| /* case 1 */
7812 object
->shadowed
|| /* case 2 */
7813 (!object
->true_share
&& /* case 3 */
7814 !old_entry
->is_shared
&&
7816 (vm_map_size_t
)(old_entry
->vme_end
-
7817 old_entry
->vme_start
)))) {
7820 * We need to create a shadow.
7821 * There are three cases here.
7822 * In the first case, we need to
7823 * complete a deferred symmetrical
7824 * copy that we participated in.
7825 * In the second and third cases,
7826 * we need to create the shadow so
7827 * that changes that we make to the
7828 * object do not interfere with
7829 * any symmetrical copies which
7830 * have occured (case 2) or which
7831 * might occur (case 3).
7833 * The first case is when we had
7834 * deferred shadow object creation
7835 * via the entry->needs_copy mechanism.
7836 * This mechanism only works when
7837 * only one entry points to the source
7838 * object, and we are about to create
7839 * a second entry pointing to the
7840 * same object. The problem is that
7841 * there is no way of mapping from
7842 * an object to the entries pointing
7843 * to it. (Deferred shadow creation
7844 * works with one entry because occurs
7845 * at fault time, and we walk from the
7846 * entry to the object when handling
7849 * The second case is when the object
7850 * to be shared has already been copied
7851 * with a symmetric copy, but we point
7852 * directly to the object without
7853 * needs_copy set in our entry. (This
7854 * can happen because different ranges
7855 * of an object can be pointed to by
7856 * different entries. In particular,
7857 * a single entry pointing to an object
7858 * can be split by a call to vm_inherit,
7859 * which, combined with task_create, can
7860 * result in the different entries
7861 * having different needs_copy values.)
7862 * The shadowed flag in the object allows
7863 * us to detect this case. The problem
7864 * with this case is that if this object
7865 * has or will have shadows, then we
7866 * must not perform an asymmetric copy
7867 * of this object, since such a copy
7868 * allows the object to be changed, which
7869 * will break the previous symmetrical
7870 * copies (which rely upon the object
7871 * not changing). In a sense, the shadowed
7872 * flag says "don't change this object".
7873 * We fix this by creating a shadow
7874 * object for this object, and sharing
7875 * that. This works because we are free
7876 * to change the shadow object (and thus
7877 * to use an asymmetric copy strategy);
7878 * this is also semantically correct,
7879 * since this object is temporary, and
7880 * therefore a copy of the object is
7881 * as good as the object itself. (This
7882 * is not true for permanent objects,
7883 * since the pager needs to see changes,
7884 * which won't happen if the changes
7885 * are made to a copy.)
7887 * The third case is when the object
7888 * to be shared has parts sticking
7889 * outside of the entry we're working
7890 * with, and thus may in the future
7891 * be subject to a symmetrical copy.
7892 * (This is a preemptive version of
7896 vm_object_shadow(&old_entry
->object
.vm_object
,
7898 (vm_map_size_t
) (old_entry
->vme_end
-
7899 old_entry
->vme_start
));
7902 * If we're making a shadow for other than
7903 * copy on write reasons, then we have
7904 * to remove write permission.
7907 if (!old_entry
->needs_copy
&&
7908 (old_entry
->protection
& VM_PROT_WRITE
)) {
7911 prot
= old_entry
->protection
& ~VM_PROT_WRITE
;
7913 if (override_nx(old_map
, old_entry
->alias
) && prot
)
7914 prot
|= VM_PROT_EXECUTE
;
7916 if (old_map
->mapped
) {
7917 vm_object_pmap_protect(
7918 old_entry
->object
.vm_object
,
7920 (old_entry
->vme_end
-
7921 old_entry
->vme_start
),
7923 old_entry
->vme_start
,
7926 pmap_protect(old_map
->pmap
,
7927 old_entry
->vme_start
,
7933 old_entry
->needs_copy
= FALSE
;
7934 object
= old_entry
->object
.vm_object
;
7938 * If object was using a symmetric copy strategy,
7939 * change its copy strategy to the default
7940 * asymmetric copy strategy, which is copy_delay
7941 * in the non-norma case and copy_call in the
7942 * norma case. Bump the reference count for the
7946 if(old_entry
->is_sub_map
) {
7947 vm_map_lock(old_entry
->object
.sub_map
);
7948 vm_map_reference(old_entry
->object
.sub_map
);
7949 vm_map_unlock(old_entry
->object
.sub_map
);
7951 vm_object_lock(object
);
7952 vm_object_reference_locked(object
);
7953 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
) {
7954 object
->copy_strategy
= MEMORY_OBJECT_COPY_DELAY
;
7956 vm_object_unlock(object
);
7960 * Clone the entry, using object ref from above.
7961 * Mark both entries as shared.
7964 new_entry
= vm_map_entry_create(new_map
);
7965 vm_map_entry_copy(new_entry
, old_entry
);
7966 old_entry
->is_shared
= TRUE
;
7967 new_entry
->is_shared
= TRUE
;
7970 * Insert the entry into the new map -- we
7971 * know we're inserting at the end of the new
7975 vm_map_entry_link(new_map
, vm_map_last_entry(new_map
), new_entry
);
7978 * Update the physical map
7981 if (old_entry
->is_sub_map
) {
7982 /* Bill Angell pmap support goes here */
7984 pmap_copy(new_map
->pmap
, old_map
->pmap
, new_entry
->vme_start
,
7985 old_entry
->vme_end
- old_entry
->vme_start
,
7986 old_entry
->vme_start
);
7993 vm_map_entry_t
*old_entry_p
,
7996 vm_map_entry_t old_entry
= *old_entry_p
;
7997 vm_map_size_t entry_size
= old_entry
->vme_end
- old_entry
->vme_start
;
7998 vm_map_offset_t start
= old_entry
->vme_start
;
8000 vm_map_entry_t last
= vm_map_last_entry(new_map
);
8002 vm_map_unlock(old_map
);
8004 * Use maxprot version of copyin because we
8005 * care about whether this memory can ever
8006 * be accessed, not just whether it's accessible
8009 if (vm_map_copyin_maxprot(old_map
, start
, entry_size
, FALSE
, ©
)
8012 * The map might have changed while it
8013 * was unlocked, check it again. Skip
8014 * any blank space or permanently
8015 * unreadable region.
8017 vm_map_lock(old_map
);
8018 if (!vm_map_lookup_entry(old_map
, start
, &last
) ||
8019 (last
->max_protection
& VM_PROT_READ
) == VM_PROT_NONE
) {
8020 last
= last
->vme_next
;
8022 *old_entry_p
= last
;
8025 * XXX For some error returns, want to
8026 * XXX skip to the next element. Note
8027 * that INVALID_ADDRESS and
8028 * PROTECTION_FAILURE are handled above.
8035 * Insert the copy into the new map
8038 vm_map_copy_insert(new_map
, last
, copy
);
8041 * Pick up the traversal at the end of
8042 * the copied region.
8045 vm_map_lock(old_map
);
8046 start
+= entry_size
;
8047 if (! vm_map_lookup_entry(old_map
, start
, &last
)) {
8048 last
= last
->vme_next
;
8050 if (last
->vme_start
== start
) {
8052 * No need to clip here and we don't
8053 * want to cause any unnecessary
8057 vm_map_clip_start(old_map
, last
, start
);
8060 *old_entry_p
= last
;
8068 * Create and return a new map based on the old
8069 * map, according to the inheritance values on the
8070 * regions in that map.
8072 * The source map must not be locked.
8080 vm_map_entry_t old_entry
;
8081 vm_map_size_t new_size
= 0, entry_size
;
8082 vm_map_entry_t new_entry
;
8083 boolean_t src_needs_copy
;
8084 boolean_t new_entry_needs_copy
;
8086 new_pmap
= pmap_create((vm_map_size_t
) 0,
8087 #if defined(__i386__) || defined(__x86_64__)
8088 old_map
->pmap
->pm_task_map
!= TASK_MAP_32BIT
8093 #if defined(__i386__)
8094 if (old_map
->pmap
->pm_task_map
== TASK_MAP_64BIT_SHARED
)
8095 pmap_set_4GB_pagezero(new_pmap
);
8098 vm_map_reference_swap(old_map
);
8099 vm_map_lock(old_map
);
8101 new_map
= vm_map_create(new_pmap
,
8102 old_map
->min_offset
,
8103 old_map
->max_offset
,
8104 old_map
->hdr
.entries_pageable
);
8107 old_entry
= vm_map_first_entry(old_map
);
8108 old_entry
!= vm_map_to_entry(old_map
);
8111 entry_size
= old_entry
->vme_end
- old_entry
->vme_start
;
8113 switch (old_entry
->inheritance
) {
8114 case VM_INHERIT_NONE
:
8117 case VM_INHERIT_SHARE
:
8118 vm_map_fork_share(old_map
, old_entry
, new_map
);
8119 new_size
+= entry_size
;
8122 case VM_INHERIT_COPY
:
8125 * Inline the copy_quickly case;
8126 * upon failure, fall back on call
8127 * to vm_map_fork_copy.
8130 if(old_entry
->is_sub_map
)
8132 if ((old_entry
->wired_count
!= 0) ||
8133 ((old_entry
->object
.vm_object
!= NULL
) &&
8134 (old_entry
->object
.vm_object
->true_share
))) {
8135 goto slow_vm_map_fork_copy
;
8138 new_entry
= vm_map_entry_create(new_map
);
8139 vm_map_entry_copy(new_entry
, old_entry
);
8140 /* clear address space specifics */
8141 new_entry
->use_pmap
= FALSE
;
8143 if (! vm_object_copy_quickly(
8144 &new_entry
->object
.vm_object
,
8146 (old_entry
->vme_end
-
8147 old_entry
->vme_start
),
8149 &new_entry_needs_copy
)) {
8150 vm_map_entry_dispose(new_map
, new_entry
);
8151 goto slow_vm_map_fork_copy
;
8155 * Handle copy-on-write obligations
8158 if (src_needs_copy
&& !old_entry
->needs_copy
) {
8161 prot
= old_entry
->protection
& ~VM_PROT_WRITE
;
8163 if (override_nx(old_map
, old_entry
->alias
) && prot
)
8164 prot
|= VM_PROT_EXECUTE
;
8166 vm_object_pmap_protect(
8167 old_entry
->object
.vm_object
,
8169 (old_entry
->vme_end
-
8170 old_entry
->vme_start
),
8171 ((old_entry
->is_shared
8175 old_entry
->vme_start
,
8178 old_entry
->needs_copy
= TRUE
;
8180 new_entry
->needs_copy
= new_entry_needs_copy
;
8183 * Insert the entry at the end
8187 vm_map_entry_link(new_map
, vm_map_last_entry(new_map
),
8189 new_size
+= entry_size
;
8192 slow_vm_map_fork_copy
:
8193 if (vm_map_fork_copy(old_map
, &old_entry
, new_map
)) {
8194 new_size
+= entry_size
;
8198 old_entry
= old_entry
->vme_next
;
8201 new_map
->size
= new_size
;
8202 vm_map_unlock(old_map
);
8203 vm_map_deallocate(old_map
);
8211 * Setup the "new_map" with the proper execution environment according
8212 * to the type of executable (platform, 64bit, chroot environment).
8213 * Map the comm page and shared region, etc...
8222 SHARED_REGION_TRACE_DEBUG(
8223 ("shared_region: task %p: vm_map_exec(%p,%p,%p,0x%x): ->\n",
8224 current_task(), new_map
, task
, fsroot
, cpu
));
8225 (void) vm_commpage_enter(new_map
, task
);
8226 (void) vm_shared_region_enter(new_map
, task
, fsroot
, cpu
);
8227 SHARED_REGION_TRACE_DEBUG(
8228 ("shared_region: task %p: vm_map_exec(%p,%p,%p,0x%x): <-\n",
8229 current_task(), new_map
, task
, fsroot
, cpu
));
8230 return KERN_SUCCESS
;
8234 * vm_map_lookup_locked:
8236 * Finds the VM object, offset, and
8237 * protection for a given virtual address in the
8238 * specified map, assuming a page fault of the
8241 * Returns the (object, offset, protection) for
8242 * this address, whether it is wired down, and whether
8243 * this map has the only reference to the data in question.
8244 * In order to later verify this lookup, a "version"
8247 * The map MUST be locked by the caller and WILL be
8248 * locked on exit. In order to guarantee the
8249 * existence of the returned object, it is returned
8252 * If a lookup is requested with "write protection"
8253 * specified, the map may be changed to perform virtual
8254 * copying operations, although the data referenced will
8258 vm_map_lookup_locked(
8259 vm_map_t
*var_map
, /* IN/OUT */
8260 vm_map_offset_t vaddr
,
8261 vm_prot_t fault_type
,
8262 int object_lock_type
,
8263 vm_map_version_t
*out_version
, /* OUT */
8264 vm_object_t
*object
, /* OUT */
8265 vm_object_offset_t
*offset
, /* OUT */
8266 vm_prot_t
*out_prot
, /* OUT */
8267 boolean_t
*wired
, /* OUT */
8268 vm_object_fault_info_t fault_info
, /* OUT */
8271 vm_map_entry_t entry
;
8272 register vm_map_t map
= *var_map
;
8273 vm_map_t old_map
= *var_map
;
8274 vm_map_t cow_sub_map_parent
= VM_MAP_NULL
;
8275 vm_map_offset_t cow_parent_vaddr
= 0;
8276 vm_map_offset_t old_start
= 0;
8277 vm_map_offset_t old_end
= 0;
8278 register vm_prot_t prot
;
8284 * If the map has an interesting hint, try it before calling
8285 * full blown lookup routine.
8289 if ((entry
== vm_map_to_entry(map
)) ||
8290 (vaddr
< entry
->vme_start
) || (vaddr
>= entry
->vme_end
)) {
8291 vm_map_entry_t tmp_entry
;
8294 * Entry was either not a valid hint, or the vaddr
8295 * was not contained in the entry, so do a full lookup.
8297 if (!vm_map_lookup_entry(map
, vaddr
, &tmp_entry
)) {
8298 if((cow_sub_map_parent
) && (cow_sub_map_parent
!= map
))
8299 vm_map_unlock(cow_sub_map_parent
);
8300 if((*real_map
!= map
)
8301 && (*real_map
!= cow_sub_map_parent
))
8302 vm_map_unlock(*real_map
);
8303 return KERN_INVALID_ADDRESS
;
8308 if(map
== old_map
) {
8309 old_start
= entry
->vme_start
;
8310 old_end
= entry
->vme_end
;
8314 * Handle submaps. Drop lock on upper map, submap is
8319 if (entry
->is_sub_map
) {
8320 vm_map_offset_t local_vaddr
;
8321 vm_map_offset_t end_delta
;
8322 vm_map_offset_t start_delta
;
8323 vm_map_entry_t submap_entry
;
8324 boolean_t mapped_needs_copy
=FALSE
;
8326 local_vaddr
= vaddr
;
8328 if ((entry
->use_pmap
&& !(fault_type
& VM_PROT_WRITE
))) {
8329 /* if real_map equals map we unlock below */
8330 if ((*real_map
!= map
) &&
8331 (*real_map
!= cow_sub_map_parent
))
8332 vm_map_unlock(*real_map
);
8333 *real_map
= entry
->object
.sub_map
;
8336 if(entry
->needs_copy
&& (fault_type
& VM_PROT_WRITE
)) {
8337 if (!mapped_needs_copy
) {
8338 if (vm_map_lock_read_to_write(map
)) {
8339 vm_map_lock_read(map
);
8340 /* XXX FBDP: entry still valid ? */
8341 if(*real_map
== entry
->object
.sub_map
)
8345 vm_map_lock_read(entry
->object
.sub_map
);
8346 cow_sub_map_parent
= map
;
8347 /* reset base to map before cow object */
8348 /* this is the map which will accept */
8349 /* the new cow object */
8350 old_start
= entry
->vme_start
;
8351 old_end
= entry
->vme_end
;
8352 cow_parent_vaddr
= vaddr
;
8353 mapped_needs_copy
= TRUE
;
8355 vm_map_lock_read(entry
->object
.sub_map
);
8356 if((cow_sub_map_parent
!= map
) &&
8361 vm_map_lock_read(entry
->object
.sub_map
);
8362 /* leave map locked if it is a target */
8363 /* cow sub_map above otherwise, just */
8364 /* follow the maps down to the object */
8365 /* here we unlock knowing we are not */
8366 /* revisiting the map. */
8367 if((*real_map
!= map
) && (map
!= cow_sub_map_parent
))
8368 vm_map_unlock_read(map
);
8371 /* XXX FBDP: map has been unlocked, what protects "entry" !? */
8372 *var_map
= map
= entry
->object
.sub_map
;
8374 /* calculate the offset in the submap for vaddr */
8375 local_vaddr
= (local_vaddr
- entry
->vme_start
) + entry
->offset
;
8378 if(!vm_map_lookup_entry(map
, local_vaddr
, &submap_entry
)) {
8379 if((cow_sub_map_parent
) && (cow_sub_map_parent
!= map
)){
8380 vm_map_unlock(cow_sub_map_parent
);
8382 if((*real_map
!= map
)
8383 && (*real_map
!= cow_sub_map_parent
)) {
8384 vm_map_unlock(*real_map
);
8387 return KERN_INVALID_ADDRESS
;
8390 /* find the attenuated shadow of the underlying object */
8391 /* on our target map */
8393 /* in english the submap object may extend beyond the */
8394 /* region mapped by the entry or, may only fill a portion */
8395 /* of it. For our purposes, we only care if the object */
8396 /* doesn't fill. In this case the area which will */
8397 /* ultimately be clipped in the top map will only need */
8398 /* to be as big as the portion of the underlying entry */
8399 /* which is mapped */
8400 start_delta
= submap_entry
->vme_start
> entry
->offset
?
8401 submap_entry
->vme_start
- entry
->offset
: 0;
8404 (entry
->offset
+ start_delta
+ (old_end
- old_start
)) <=
8405 submap_entry
->vme_end
?
8406 0 : (entry
->offset
+
8407 (old_end
- old_start
))
8408 - submap_entry
->vme_end
;
8410 old_start
+= start_delta
;
8411 old_end
-= end_delta
;
8413 if(submap_entry
->is_sub_map
) {
8414 entry
= submap_entry
;
8415 vaddr
= local_vaddr
;
8416 goto submap_recurse
;
8419 if(((fault_type
& VM_PROT_WRITE
) && cow_sub_map_parent
)) {
8421 vm_object_t sub_object
, copy_object
;
8422 vm_object_offset_t copy_offset
;
8423 vm_map_offset_t local_start
;
8424 vm_map_offset_t local_end
;
8425 boolean_t copied_slowly
= FALSE
;
8427 if (vm_map_lock_read_to_write(map
)) {
8428 vm_map_lock_read(map
);
8429 old_start
-= start_delta
;
8430 old_end
+= end_delta
;
8435 sub_object
= submap_entry
->object
.vm_object
;
8436 if (sub_object
== VM_OBJECT_NULL
) {
8440 (submap_entry
->vme_end
-
8441 submap_entry
->vme_start
));
8442 submap_entry
->object
.vm_object
= sub_object
;
8443 submap_entry
->offset
= 0;
8445 local_start
= local_vaddr
-
8446 (cow_parent_vaddr
- old_start
);
8447 local_end
= local_vaddr
+
8448 (old_end
- cow_parent_vaddr
);
8449 vm_map_clip_start(map
, submap_entry
, local_start
);
8450 vm_map_clip_end(map
, submap_entry
, local_end
);
8451 /* unnesting was done in vm_map_clip_start/end() */
8452 assert(!submap_entry
->use_pmap
);
8454 /* This is the COW case, lets connect */
8455 /* an entry in our space to the underlying */
8456 /* object in the submap, bypassing the */
8460 if(submap_entry
->wired_count
!= 0 ||
8461 (sub_object
->copy_strategy
==
8462 MEMORY_OBJECT_COPY_NONE
)) {
8463 vm_object_lock(sub_object
);
8464 vm_object_copy_slowly(sub_object
,
8465 submap_entry
->offset
,
8466 (submap_entry
->vme_end
-
8467 submap_entry
->vme_start
),
8470 copied_slowly
= TRUE
;
8473 /* set up shadow object */
8474 copy_object
= sub_object
;
8475 vm_object_reference(copy_object
);
8476 sub_object
->shadowed
= TRUE
;
8477 submap_entry
->needs_copy
= TRUE
;
8479 prot
= submap_entry
->protection
& ~VM_PROT_WRITE
;
8481 if (override_nx(map
, submap_entry
->alias
) && prot
)
8482 prot
|= VM_PROT_EXECUTE
;
8484 vm_object_pmap_protect(
8486 submap_entry
->offset
,
8487 submap_entry
->vme_end
-
8488 submap_entry
->vme_start
,
8489 (submap_entry
->is_shared
8491 PMAP_NULL
: map
->pmap
,
8492 submap_entry
->vme_start
,
8497 * Adjust the fault offset to the submap entry.
8499 copy_offset
= (local_vaddr
-
8500 submap_entry
->vme_start
+
8501 submap_entry
->offset
);
8503 /* This works diffently than the */
8504 /* normal submap case. We go back */
8505 /* to the parent of the cow map and*/
8506 /* clip out the target portion of */
8507 /* the sub_map, substituting the */
8508 /* new copy object, */
8511 local_start
= old_start
;
8512 local_end
= old_end
;
8513 map
= cow_sub_map_parent
;
8514 *var_map
= cow_sub_map_parent
;
8515 vaddr
= cow_parent_vaddr
;
8516 cow_sub_map_parent
= NULL
;
8518 if(!vm_map_lookup_entry(map
,
8520 vm_object_deallocate(
8522 vm_map_lock_write_to_read(map
);
8523 return KERN_INVALID_ADDRESS
;
8526 /* clip out the portion of space */
8527 /* mapped by the sub map which */
8528 /* corresponds to the underlying */
8532 * Clip (and unnest) the smallest nested chunk
8533 * possible around the faulting address...
8535 local_start
= vaddr
& ~(pmap_nesting_size_min
- 1);
8536 local_end
= local_start
+ pmap_nesting_size_min
;
8538 * ... but don't go beyond the "old_start" to "old_end"
8539 * range, to avoid spanning over another VM region
8540 * with a possibly different VM object and/or offset.
8542 if (local_start
< old_start
) {
8543 local_start
= old_start
;
8545 if (local_end
> old_end
) {
8546 local_end
= old_end
;
8549 * Adjust copy_offset to the start of the range.
8551 copy_offset
-= (vaddr
- local_start
);
8553 vm_map_clip_start(map
, entry
, local_start
);
8554 vm_map_clip_end(map
, entry
, local_end
);
8555 /* unnesting was done in vm_map_clip_start/end() */
8556 assert(!entry
->use_pmap
);
8558 /* substitute copy object for */
8559 /* shared map entry */
8560 vm_map_deallocate(entry
->object
.sub_map
);
8561 entry
->is_sub_map
= FALSE
;
8562 entry
->object
.vm_object
= copy_object
;
8564 /* propagate the submap entry's protections */
8565 entry
->protection
|= submap_entry
->protection
;
8566 entry
->max_protection
|= submap_entry
->max_protection
;
8569 entry
->offset
= local_start
- old_start
;
8570 entry
->needs_copy
= FALSE
;
8571 entry
->is_shared
= FALSE
;
8573 entry
->offset
= copy_offset
;
8574 entry
->needs_copy
= TRUE
;
8575 if(entry
->inheritance
== VM_INHERIT_SHARE
)
8576 entry
->inheritance
= VM_INHERIT_COPY
;
8578 entry
->is_shared
= TRUE
;
8580 if(entry
->inheritance
== VM_INHERIT_SHARE
)
8581 entry
->inheritance
= VM_INHERIT_COPY
;
8583 vm_map_lock_write_to_read(map
);
8585 if((cow_sub_map_parent
)
8586 && (cow_sub_map_parent
!= *real_map
)
8587 && (cow_sub_map_parent
!= map
)) {
8588 vm_map_unlock(cow_sub_map_parent
);
8590 entry
= submap_entry
;
8591 vaddr
= local_vaddr
;
8596 * Check whether this task is allowed to have
8600 prot
= entry
->protection
;
8602 if (override_nx(map
, entry
->alias
) && prot
) {
8604 * HACK -- if not a stack, then allow execution
8606 prot
|= VM_PROT_EXECUTE
;
8609 if ((fault_type
& (prot
)) != fault_type
) {
8610 if (*real_map
!= map
) {
8611 vm_map_unlock(*real_map
);
8615 if ((fault_type
& VM_PROT_EXECUTE
) && prot
)
8616 log_stack_execution_failure((addr64_t
)vaddr
, prot
);
8618 DTRACE_VM2(prot_fault
, int, 1, (uint64_t *), NULL
);
8619 return KERN_PROTECTION_FAILURE
;
8623 * If this page is not pageable, we have to get
8624 * it for all possible accesses.
8627 *wired
= (entry
->wired_count
!= 0);
8632 * If the entry was copy-on-write, we either ...
8635 if (entry
->needs_copy
) {
8637 * If we want to write the page, we may as well
8638 * handle that now since we've got the map locked.
8640 * If we don't need to write the page, we just
8641 * demote the permissions allowed.
8644 if ((fault_type
& VM_PROT_WRITE
) || *wired
) {
8646 * Make a new object, and place it in the
8647 * object chain. Note that no new references
8648 * have appeared -- one just moved from the
8649 * map to the new object.
8652 if (vm_map_lock_read_to_write(map
)) {
8653 vm_map_lock_read(map
);
8656 vm_object_shadow(&entry
->object
.vm_object
,
8658 (vm_map_size_t
) (entry
->vme_end
-
8661 entry
->object
.vm_object
->shadowed
= TRUE
;
8662 entry
->needs_copy
= FALSE
;
8663 vm_map_lock_write_to_read(map
);
8667 * We're attempting to read a copy-on-write
8668 * page -- don't allow writes.
8671 prot
&= (~VM_PROT_WRITE
);
8676 * Create an object if necessary.
8678 if (entry
->object
.vm_object
== VM_OBJECT_NULL
) {
8680 if (vm_map_lock_read_to_write(map
)) {
8681 vm_map_lock_read(map
);
8685 entry
->object
.vm_object
= vm_object_allocate(
8686 (vm_map_size_t
)(entry
->vme_end
- entry
->vme_start
));
8688 vm_map_lock_write_to_read(map
);
8692 * Return the object/offset from this entry. If the entry
8693 * was copy-on-write or empty, it has been fixed up. Also
8694 * return the protection.
8697 *offset
= (vaddr
- entry
->vme_start
) + entry
->offset
;
8698 *object
= entry
->object
.vm_object
;
8702 fault_info
->interruptible
= THREAD_UNINT
; /* for now... */
8703 /* ... the caller will change "interruptible" if needed */
8704 fault_info
->cluster_size
= 0;
8705 fault_info
->user_tag
= entry
->alias
;
8706 fault_info
->behavior
= entry
->behavior
;
8707 fault_info
->lo_offset
= entry
->offset
;
8708 fault_info
->hi_offset
= (entry
->vme_end
- entry
->vme_start
) + entry
->offset
;
8709 fault_info
->no_cache
= entry
->no_cache
;
8710 fault_info
->stealth
= FALSE
;
8714 * Lock the object to prevent it from disappearing
8716 if (object_lock_type
== OBJECT_LOCK_EXCLUSIVE
)
8717 vm_object_lock(*object
);
8719 vm_object_lock_shared(*object
);
8722 * Save the version number
8725 out_version
->main_timestamp
= map
->timestamp
;
8727 return KERN_SUCCESS
;
8734 * Verifies that the map in question has not changed
8735 * since the given version. If successful, the map
8736 * will not change until vm_map_verify_done() is called.
8740 register vm_map_t map
,
8741 register vm_map_version_t
*version
) /* REF */
8745 vm_map_lock_read(map
);
8746 result
= (map
->timestamp
== version
->main_timestamp
);
8749 vm_map_unlock_read(map
);
8755 * vm_map_verify_done:
8757 * Releases locks acquired by a vm_map_verify.
8759 * This is now a macro in vm/vm_map.h. It does a
8760 * vm_map_unlock_read on the map.
8765 * TEMPORARYTEMPORARYTEMPORARYTEMPORARYTEMPORARYTEMPORARY
8766 * Goes away after regular vm_region_recurse function migrates to
8768 * vm_region_recurse: A form of vm_region which follows the
8769 * submaps in a target map
8774 vm_map_region_recurse_64(
8776 vm_map_offset_t
*address
, /* IN/OUT */
8777 vm_map_size_t
*size
, /* OUT */
8778 natural_t
*nesting_depth
, /* IN/OUT */
8779 vm_region_submap_info_64_t submap_info
, /* IN/OUT */
8780 mach_msg_type_number_t
*count
) /* IN/OUT */
8782 vm_region_extended_info_data_t extended
;
8783 vm_map_entry_t tmp_entry
;
8784 vm_map_offset_t user_address
;
8785 unsigned int user_max_depth
;
8788 * "curr_entry" is the VM map entry preceding or including the
8789 * address we're looking for.
8790 * "curr_map" is the map or sub-map containing "curr_entry".
8791 * "curr_offset" is the cumulated offset of "curr_map" in the
8792 * target task's address space.
8793 * "curr_depth" is the depth of "curr_map" in the chain of
8795 * "curr_max_offset" is the maximum offset we should take into
8796 * account in the current map. It may be smaller than the current
8797 * map's "max_offset" because we might not have mapped it all in
8798 * the upper level map.
8800 vm_map_entry_t curr_entry
;
8801 vm_map_offset_t curr_offset
;
8803 unsigned int curr_depth
;
8804 vm_map_offset_t curr_max_offset
;
8807 * "next_" is the same as "curr_" but for the VM region immediately
8808 * after the address we're looking for. We need to keep track of this
8809 * too because we want to return info about that region if the
8810 * address we're looking for is not mapped.
8812 vm_map_entry_t next_entry
;
8813 vm_map_offset_t next_offset
;
8815 unsigned int next_depth
;
8816 vm_map_offset_t next_max_offset
;
8818 boolean_t look_for_pages
;
8819 vm_region_submap_short_info_64_t short_info
;
8821 if (map
== VM_MAP_NULL
) {
8822 /* no address space to work on */
8823 return KERN_INVALID_ARGUMENT
;
8826 if (*count
< VM_REGION_SUBMAP_INFO_COUNT_64
) {
8827 if (*count
< VM_REGION_SUBMAP_SHORT_INFO_COUNT_64
) {
8829 * "info" structure is not big enough and
8832 return KERN_INVALID_ARGUMENT
;
8834 look_for_pages
= FALSE
;
8835 *count
= VM_REGION_SUBMAP_SHORT_INFO_COUNT_64
;
8836 short_info
= (vm_region_submap_short_info_64_t
) submap_info
;
8840 look_for_pages
= TRUE
;
8841 *count
= VM_REGION_SUBMAP_INFO_COUNT_64
;
8846 user_address
= *address
;
8847 user_max_depth
= *nesting_depth
;
8853 curr_max_offset
= curr_map
->max_offset
;
8859 next_max_offset
= curr_max_offset
;
8862 vm_map_lock_read(curr_map
);
8866 if (vm_map_lookup_entry(curr_map
,
8867 user_address
- curr_offset
,
8869 /* tmp_entry contains the address we're looking for */
8870 curr_entry
= tmp_entry
;
8873 * The address is not mapped. "tmp_entry" is the
8874 * map entry preceding the address. We want the next
8875 * one, if it exists.
8877 curr_entry
= tmp_entry
->vme_next
;
8878 if (curr_entry
== vm_map_to_entry(curr_map
) ||
8879 curr_entry
->vme_start
>= curr_max_offset
) {
8880 /* no next entry at this level: stop looking */
8882 vm_map_unlock_read(curr_map
);
8888 curr_max_offset
= 0;
8894 * Is the next entry at this level closer to the address (or
8895 * deeper in the submap chain) than the one we had
8898 tmp_entry
= curr_entry
->vme_next
;
8899 if (tmp_entry
== vm_map_to_entry(curr_map
)) {
8900 /* no next entry at this level */
8901 } else if (tmp_entry
->vme_start
>= curr_max_offset
) {
8903 * tmp_entry is beyond the scope of what we mapped of
8904 * this submap in the upper level: ignore it.
8906 } else if ((next_entry
== NULL
) ||
8907 (tmp_entry
->vme_start
+ curr_offset
<=
8908 next_entry
->vme_start
+ next_offset
)) {
8910 * We didn't have a "next_entry" or this one is
8911 * closer to the address we're looking for:
8912 * use this "tmp_entry" as the new "next_entry".
8914 if (next_entry
!= NULL
) {
8915 /* unlock the last "next_map" */
8916 if (next_map
!= curr_map
&& not_in_kdp
) {
8917 vm_map_unlock_read(next_map
);
8920 next_entry
= tmp_entry
;
8921 next_map
= curr_map
;
8922 next_offset
= curr_offset
;
8923 next_depth
= curr_depth
;
8924 next_max_offset
= curr_max_offset
;
8927 if (!curr_entry
->is_sub_map
||
8928 curr_depth
>= user_max_depth
) {
8930 * We hit a leaf map or we reached the maximum depth
8931 * we could, so stop looking. Keep the current map
8938 * Get down to the next submap level.
8942 * Lock the next level and unlock the current level,
8943 * unless we need to keep it locked to access the "next_entry"
8947 vm_map_lock_read(curr_entry
->object
.sub_map
);
8949 if (curr_map
== next_map
) {
8950 /* keep "next_map" locked in case we need it */
8952 /* release this map */
8954 vm_map_unlock_read(curr_map
);
8958 * Adjust the offset. "curr_entry" maps the submap
8959 * at relative address "curr_entry->vme_start" in the
8960 * curr_map but skips the first "curr_entry->offset"
8961 * bytes of the submap.
8962 * "curr_offset" always represents the offset of a virtual
8963 * address in the curr_map relative to the absolute address
8964 * space (i.e. the top-level VM map).
8967 (curr_entry
->vme_start
- curr_entry
->offset
);
8968 /* switch to the submap */
8969 curr_map
= curr_entry
->object
.sub_map
;
8972 * "curr_max_offset" allows us to keep track of the
8973 * portion of the submap that is actually mapped at this level:
8974 * the rest of that submap is irrelevant to us, since it's not
8976 * The relevant portion of the map starts at
8977 * "curr_entry->offset" up to the size of "curr_entry".
8980 curr_entry
->vme_end
- curr_entry
->vme_start
+
8985 if (curr_entry
== NULL
) {
8986 /* no VM region contains the address... */
8987 if (next_entry
== NULL
) {
8988 /* ... and no VM region follows it either */
8989 return KERN_INVALID_ADDRESS
;
8991 /* ... gather info about the next VM region */
8992 curr_entry
= next_entry
;
8993 curr_map
= next_map
; /* still locked ... */
8994 curr_offset
= next_offset
;
8995 curr_depth
= next_depth
;
8996 curr_max_offset
= next_max_offset
;
8998 /* we won't need "next_entry" after all */
8999 if (next_entry
!= NULL
) {
9000 /* release "next_map" */
9001 if (next_map
!= curr_map
&& not_in_kdp
) {
9002 vm_map_unlock_read(next_map
);
9010 next_max_offset
= 0;
9012 *nesting_depth
= curr_depth
;
9013 *size
= curr_entry
->vme_end
- curr_entry
->vme_start
;
9014 *address
= curr_entry
->vme_start
+ curr_offset
;
9016 // LP64todo: all the current tools are 32bit, obviously never worked for 64b
9017 // so probably should be a real 32b ID vs. ptr.
9018 // Current users just check for equality
9019 #define INFO_MAKE_OBJECT_ID(p) ((uint32_t)(uintptr_t)p)
9021 if (look_for_pages
) {
9022 submap_info
->user_tag
= curr_entry
->alias
;
9023 submap_info
->offset
= curr_entry
->offset
;
9024 submap_info
->protection
= curr_entry
->protection
;
9025 submap_info
->inheritance
= curr_entry
->inheritance
;
9026 submap_info
->max_protection
= curr_entry
->max_protection
;
9027 submap_info
->behavior
= curr_entry
->behavior
;
9028 submap_info
->user_wired_count
= curr_entry
->user_wired_count
;
9029 submap_info
->is_submap
= curr_entry
->is_sub_map
;
9030 submap_info
->object_id
= INFO_MAKE_OBJECT_ID(curr_entry
->object
.vm_object
);
9032 short_info
->user_tag
= curr_entry
->alias
;
9033 short_info
->offset
= curr_entry
->offset
;
9034 short_info
->protection
= curr_entry
->protection
;
9035 short_info
->inheritance
= curr_entry
->inheritance
;
9036 short_info
->max_protection
= curr_entry
->max_protection
;
9037 short_info
->behavior
= curr_entry
->behavior
;
9038 short_info
->user_wired_count
= curr_entry
->user_wired_count
;
9039 short_info
->is_submap
= curr_entry
->is_sub_map
;
9040 short_info
->object_id
= INFO_MAKE_OBJECT_ID(curr_entry
->object
.vm_object
);
9043 extended
.pages_resident
= 0;
9044 extended
.pages_swapped_out
= 0;
9045 extended
.pages_shared_now_private
= 0;
9046 extended
.pages_dirtied
= 0;
9047 extended
.external_pager
= 0;
9048 extended
.shadow_depth
= 0;
9051 if (!curr_entry
->is_sub_map
) {
9052 vm_map_region_walk(curr_map
,
9053 curr_entry
->vme_start
,
9056 (curr_entry
->vme_end
-
9057 curr_entry
->vme_start
),
9060 if (extended
.external_pager
&&
9061 extended
.ref_count
== 2 &&
9062 extended
.share_mode
== SM_SHARED
) {
9063 extended
.share_mode
= SM_PRIVATE
;
9066 if (curr_entry
->use_pmap
) {
9067 extended
.share_mode
= SM_TRUESHARED
;
9069 extended
.share_mode
= SM_PRIVATE
;
9071 extended
.ref_count
=
9072 curr_entry
->object
.sub_map
->ref_count
;
9076 if (look_for_pages
) {
9077 submap_info
->pages_resident
= extended
.pages_resident
;
9078 submap_info
->pages_swapped_out
= extended
.pages_swapped_out
;
9079 submap_info
->pages_shared_now_private
=
9080 extended
.pages_shared_now_private
;
9081 submap_info
->pages_dirtied
= extended
.pages_dirtied
;
9082 submap_info
->external_pager
= extended
.external_pager
;
9083 submap_info
->shadow_depth
= extended
.shadow_depth
;
9084 submap_info
->share_mode
= extended
.share_mode
;
9085 submap_info
->ref_count
= extended
.ref_count
;
9087 short_info
->external_pager
= extended
.external_pager
;
9088 short_info
->shadow_depth
= extended
.shadow_depth
;
9089 short_info
->share_mode
= extended
.share_mode
;
9090 short_info
->ref_count
= extended
.ref_count
;
9094 vm_map_unlock_read(curr_map
);
9097 return KERN_SUCCESS
;
9103 * User call to obtain information about a region in
9104 * a task's address map. Currently, only one flavor is
9107 * XXX The reserved and behavior fields cannot be filled
9108 * in until the vm merge from the IK is completed, and
9109 * vm_reserve is implemented.
9115 vm_map_offset_t
*address
, /* IN/OUT */
9116 vm_map_size_t
*size
, /* OUT */
9117 vm_region_flavor_t flavor
, /* IN */
9118 vm_region_info_t info
, /* OUT */
9119 mach_msg_type_number_t
*count
, /* IN/OUT */
9120 mach_port_t
*object_name
) /* OUT */
9122 vm_map_entry_t tmp_entry
;
9123 vm_map_entry_t entry
;
9124 vm_map_offset_t start
;
9126 if (map
== VM_MAP_NULL
)
9127 return(KERN_INVALID_ARGUMENT
);
9131 case VM_REGION_BASIC_INFO
:
9132 /* legacy for old 32-bit objects info */
9134 vm_region_basic_info_t basic
;
9136 if (*count
< VM_REGION_BASIC_INFO_COUNT
)
9137 return(KERN_INVALID_ARGUMENT
);
9139 basic
= (vm_region_basic_info_t
) info
;
9140 *count
= VM_REGION_BASIC_INFO_COUNT
;
9142 vm_map_lock_read(map
);
9145 if (!vm_map_lookup_entry(map
, start
, &tmp_entry
)) {
9146 if ((entry
= tmp_entry
->vme_next
) == vm_map_to_entry(map
)) {
9147 vm_map_unlock_read(map
);
9148 return(KERN_INVALID_ADDRESS
);
9154 start
= entry
->vme_start
;
9156 basic
->offset
= (uint32_t)entry
->offset
;
9157 basic
->protection
= entry
->protection
;
9158 basic
->inheritance
= entry
->inheritance
;
9159 basic
->max_protection
= entry
->max_protection
;
9160 basic
->behavior
= entry
->behavior
;
9161 basic
->user_wired_count
= entry
->user_wired_count
;
9162 basic
->reserved
= entry
->is_sub_map
;
9164 *size
= (entry
->vme_end
- start
);
9166 if (object_name
) *object_name
= IP_NULL
;
9167 if (entry
->is_sub_map
) {
9168 basic
->shared
= FALSE
;
9170 basic
->shared
= entry
->is_shared
;
9173 vm_map_unlock_read(map
);
9174 return(KERN_SUCCESS
);
9177 case VM_REGION_BASIC_INFO_64
:
9179 vm_region_basic_info_64_t basic
;
9181 if (*count
< VM_REGION_BASIC_INFO_COUNT_64
)
9182 return(KERN_INVALID_ARGUMENT
);
9184 basic
= (vm_region_basic_info_64_t
) info
;
9185 *count
= VM_REGION_BASIC_INFO_COUNT_64
;
9187 vm_map_lock_read(map
);
9190 if (!vm_map_lookup_entry(map
, start
, &tmp_entry
)) {
9191 if ((entry
= tmp_entry
->vme_next
) == vm_map_to_entry(map
)) {
9192 vm_map_unlock_read(map
);
9193 return(KERN_INVALID_ADDRESS
);
9199 start
= entry
->vme_start
;
9201 basic
->offset
= entry
->offset
;
9202 basic
->protection
= entry
->protection
;
9203 basic
->inheritance
= entry
->inheritance
;
9204 basic
->max_protection
= entry
->max_protection
;
9205 basic
->behavior
= entry
->behavior
;
9206 basic
->user_wired_count
= entry
->user_wired_count
;
9207 basic
->reserved
= entry
->is_sub_map
;
9209 *size
= (entry
->vme_end
- start
);
9211 if (object_name
) *object_name
= IP_NULL
;
9212 if (entry
->is_sub_map
) {
9213 basic
->shared
= FALSE
;
9215 basic
->shared
= entry
->is_shared
;
9218 vm_map_unlock_read(map
);
9219 return(KERN_SUCCESS
);
9221 case VM_REGION_EXTENDED_INFO
:
9223 vm_region_extended_info_t extended
;
9225 if (*count
< VM_REGION_EXTENDED_INFO_COUNT
)
9226 return(KERN_INVALID_ARGUMENT
);
9228 extended
= (vm_region_extended_info_t
) info
;
9229 *count
= VM_REGION_EXTENDED_INFO_COUNT
;
9231 vm_map_lock_read(map
);
9234 if (!vm_map_lookup_entry(map
, start
, &tmp_entry
)) {
9235 if ((entry
= tmp_entry
->vme_next
) == vm_map_to_entry(map
)) {
9236 vm_map_unlock_read(map
);
9237 return(KERN_INVALID_ADDRESS
);
9242 start
= entry
->vme_start
;
9244 extended
->protection
= entry
->protection
;
9245 extended
->user_tag
= entry
->alias
;
9246 extended
->pages_resident
= 0;
9247 extended
->pages_swapped_out
= 0;
9248 extended
->pages_shared_now_private
= 0;
9249 extended
->pages_dirtied
= 0;
9250 extended
->external_pager
= 0;
9251 extended
->shadow_depth
= 0;
9253 vm_map_region_walk(map
, start
, entry
, entry
->offset
, entry
->vme_end
- start
, extended
, TRUE
);
9255 if (extended
->external_pager
&& extended
->ref_count
== 2 && extended
->share_mode
== SM_SHARED
)
9256 extended
->share_mode
= SM_PRIVATE
;
9259 *object_name
= IP_NULL
;
9261 *size
= (entry
->vme_end
- start
);
9263 vm_map_unlock_read(map
);
9264 return(KERN_SUCCESS
);
9266 case VM_REGION_TOP_INFO
:
9268 vm_region_top_info_t top
;
9270 if (*count
< VM_REGION_TOP_INFO_COUNT
)
9271 return(KERN_INVALID_ARGUMENT
);
9273 top
= (vm_region_top_info_t
) info
;
9274 *count
= VM_REGION_TOP_INFO_COUNT
;
9276 vm_map_lock_read(map
);
9279 if (!vm_map_lookup_entry(map
, start
, &tmp_entry
)) {
9280 if ((entry
= tmp_entry
->vme_next
) == vm_map_to_entry(map
)) {
9281 vm_map_unlock_read(map
);
9282 return(KERN_INVALID_ADDRESS
);
9288 start
= entry
->vme_start
;
9290 top
->private_pages_resident
= 0;
9291 top
->shared_pages_resident
= 0;
9293 vm_map_region_top_walk(entry
, top
);
9296 *object_name
= IP_NULL
;
9298 *size
= (entry
->vme_end
- start
);
9300 vm_map_unlock_read(map
);
9301 return(KERN_SUCCESS
);
9304 return(KERN_INVALID_ARGUMENT
);
9308 #define OBJ_RESIDENT_COUNT(obj, entry_size) \
9310 ((obj)->all_reusable ? \
9311 (obj)->wired_page_count : \
9312 (obj)->resident_page_count - (obj)->reusable_page_count))
9315 vm_map_region_top_walk(
9316 vm_map_entry_t entry
,
9317 vm_region_top_info_t top
)
9320 if (entry
->object
.vm_object
== 0 || entry
->is_sub_map
) {
9321 top
->share_mode
= SM_EMPTY
;
9328 struct vm_object
*obj
, *tmp_obj
;
9330 uint32_t entry_size
;
9332 entry_size
= (uint32_t) ((entry
->vme_end
- entry
->vme_start
) / PAGE_SIZE_64
);
9334 obj
= entry
->object
.vm_object
;
9336 vm_object_lock(obj
);
9338 if ((ref_count
= obj
->ref_count
) > 1 && obj
->paging_in_progress
)
9341 assert(obj
->reusable_page_count
<= obj
->resident_page_count
);
9344 top
->private_pages_resident
=
9345 OBJ_RESIDENT_COUNT(obj
, entry_size
);
9347 top
->shared_pages_resident
=
9348 OBJ_RESIDENT_COUNT(obj
, entry_size
);
9349 top
->ref_count
= ref_count
;
9350 top
->share_mode
= SM_COW
;
9352 while ((tmp_obj
= obj
->shadow
)) {
9353 vm_object_lock(tmp_obj
);
9354 vm_object_unlock(obj
);
9357 if ((ref_count
= obj
->ref_count
) > 1 && obj
->paging_in_progress
)
9360 assert(obj
->reusable_page_count
<= obj
->resident_page_count
);
9361 top
->shared_pages_resident
+=
9362 OBJ_RESIDENT_COUNT(obj
, entry_size
);
9363 top
->ref_count
+= ref_count
- 1;
9366 if (entry
->needs_copy
) {
9367 top
->share_mode
= SM_COW
;
9368 top
->shared_pages_resident
=
9369 OBJ_RESIDENT_COUNT(obj
, entry_size
);
9371 if (ref_count
== 1 ||
9372 (ref_count
== 2 && !(obj
->pager_trusted
) && !(obj
->internal
))) {
9373 top
->share_mode
= SM_PRIVATE
;
9374 top
->private_pages_resident
=
9375 OBJ_RESIDENT_COUNT(obj
,
9378 top
->share_mode
= SM_SHARED
;
9379 top
->shared_pages_resident
=
9380 OBJ_RESIDENT_COUNT(obj
,
9384 top
->ref_count
= ref_count
;
9386 /* XXX K64: obj_id will be truncated */
9387 top
->obj_id
= (unsigned int) (uintptr_t)obj
;
9389 vm_object_unlock(obj
);
9397 vm_map_entry_t entry
,
9398 vm_object_offset_t offset
,
9399 vm_object_size_t range
,
9400 vm_region_extended_info_t extended
,
9401 boolean_t look_for_pages
)
9403 register struct vm_object
*obj
, *tmp_obj
;
9404 register vm_map_offset_t last_offset
;
9406 register int ref_count
;
9407 struct vm_object
*shadow_object
;
9410 if ((entry
->object
.vm_object
== 0) ||
9411 (entry
->is_sub_map
) ||
9412 (entry
->object
.vm_object
->phys_contiguous
)) {
9413 extended
->share_mode
= SM_EMPTY
;
9414 extended
->ref_count
= 0;
9418 obj
= entry
->object
.vm_object
;
9420 vm_object_lock(obj
);
9422 if ((ref_count
= obj
->ref_count
) > 1 && obj
->paging_in_progress
)
9425 if (look_for_pages
) {
9426 for (last_offset
= offset
+ range
;
9427 offset
< last_offset
;
9428 offset
+= PAGE_SIZE_64
, va
+= PAGE_SIZE
)
9429 vm_map_region_look_for_page(map
, va
, obj
,
9433 shadow_object
= obj
->shadow
;
9436 if ( !(obj
->pager_trusted
) && !(obj
->internal
))
9437 extended
->external_pager
= 1;
9439 if (shadow_object
!= VM_OBJECT_NULL
) {
9440 vm_object_lock(shadow_object
);
9442 shadow_object
!= VM_OBJECT_NULL
;
9444 vm_object_t next_shadow
;
9446 if ( !(shadow_object
->pager_trusted
) &&
9447 !(shadow_object
->internal
))
9448 extended
->external_pager
= 1;
9450 next_shadow
= shadow_object
->shadow
;
9452 vm_object_lock(next_shadow
);
9454 vm_object_unlock(shadow_object
);
9455 shadow_object
= next_shadow
;
9458 extended
->shadow_depth
= shadow_depth
;
9461 if (extended
->shadow_depth
|| entry
->needs_copy
)
9462 extended
->share_mode
= SM_COW
;
9465 extended
->share_mode
= SM_PRIVATE
;
9467 if (obj
->true_share
)
9468 extended
->share_mode
= SM_TRUESHARED
;
9470 extended
->share_mode
= SM_SHARED
;
9473 extended
->ref_count
= ref_count
- extended
->shadow_depth
;
9475 for (i
= 0; i
< extended
->shadow_depth
; i
++) {
9476 if ((tmp_obj
= obj
->shadow
) == 0)
9478 vm_object_lock(tmp_obj
);
9479 vm_object_unlock(obj
);
9481 if ((ref_count
= tmp_obj
->ref_count
) > 1 && tmp_obj
->paging_in_progress
)
9484 extended
->ref_count
+= ref_count
;
9487 vm_object_unlock(obj
);
9489 if (extended
->share_mode
== SM_SHARED
) {
9490 register vm_map_entry_t cur
;
9491 register vm_map_entry_t last
;
9494 obj
= entry
->object
.vm_object
;
9495 last
= vm_map_to_entry(map
);
9498 if ((ref_count
= obj
->ref_count
) > 1 && obj
->paging_in_progress
)
9500 for (cur
= vm_map_first_entry(map
); cur
!= last
; cur
= cur
->vme_next
)
9501 my_refs
+= vm_map_region_count_obj_refs(cur
, obj
);
9503 if (my_refs
== ref_count
)
9504 extended
->share_mode
= SM_PRIVATE_ALIASED
;
9505 else if (my_refs
> 1)
9506 extended
->share_mode
= SM_SHARED_ALIASED
;
9512 /* object is locked on entry and locked on return */
9516 vm_map_region_look_for_page(
9517 __unused vm_map_t map
,
9518 __unused vm_map_offset_t va
,
9520 vm_object_offset_t offset
,
9523 vm_region_extended_info_t extended
)
9525 register vm_page_t p
;
9526 register vm_object_t shadow
;
9527 register int ref_count
;
9528 vm_object_t caller_object
;
9532 shadow
= object
->shadow
;
9533 caller_object
= object
;
9538 if ( !(object
->pager_trusted
) && !(object
->internal
))
9539 extended
->external_pager
= 1;
9541 if ((p
= vm_page_lookup(object
, offset
)) != VM_PAGE_NULL
) {
9542 if (shadow
&& (max_refcnt
== 1))
9543 extended
->pages_shared_now_private
++;
9545 if (!p
->fictitious
&&
9546 (p
->dirty
|| pmap_is_modified(p
->phys_page
)))
9547 extended
->pages_dirtied
++;
9549 extended
->pages_resident
++;
9551 if(object
!= caller_object
)
9552 vm_object_unlock(object
);
9557 if (object
->existence_map
) {
9558 if (vm_external_state_get(object
->existence_map
, offset
) == VM_EXTERNAL_STATE_EXISTS
) {
9560 extended
->pages_swapped_out
++;
9562 if(object
!= caller_object
)
9563 vm_object_unlock(object
);
9567 } else if (object
->internal
&&
9569 !object
->terminating
&&
9570 object
->pager_ready
) {
9572 memory_object_t pager
;
9574 vm_object_paging_begin(object
);
9575 pager
= object
->pager
;
9576 vm_object_unlock(object
);
9578 kr
= memory_object_data_request(
9580 offset
+ object
->paging_offset
,
9581 0, /* just poke the pager */
9585 vm_object_lock(object
);
9586 vm_object_paging_end(object
);
9588 if (kr
== KERN_SUCCESS
) {
9589 /* the pager has that page */
9590 extended
->pages_swapped_out
++;
9591 if (object
!= caller_object
)
9592 vm_object_unlock(object
);
9596 #endif /* MACH_PAGEMAP */
9599 vm_object_lock(shadow
);
9601 if ((ref_count
= shadow
->ref_count
) > 1 && shadow
->paging_in_progress
)
9604 if (++depth
> extended
->shadow_depth
)
9605 extended
->shadow_depth
= depth
;
9607 if (ref_count
> max_refcnt
)
9608 max_refcnt
= ref_count
;
9610 if(object
!= caller_object
)
9611 vm_object_unlock(object
);
9613 offset
= offset
+ object
->shadow_offset
;
9615 shadow
= object
->shadow
;
9618 if(object
!= caller_object
)
9619 vm_object_unlock(object
);
9625 vm_map_region_count_obj_refs(
9626 vm_map_entry_t entry
,
9629 register int ref_count
;
9630 register vm_object_t chk_obj
;
9631 register vm_object_t tmp_obj
;
9633 if (entry
->object
.vm_object
== 0)
9636 if (entry
->is_sub_map
)
9641 chk_obj
= entry
->object
.vm_object
;
9642 vm_object_lock(chk_obj
);
9645 if (chk_obj
== object
)
9647 tmp_obj
= chk_obj
->shadow
;
9649 vm_object_lock(tmp_obj
);
9650 vm_object_unlock(chk_obj
);
9660 * Routine: vm_map_simplify
9663 * Attempt to simplify the map representation in
9664 * the vicinity of the given starting address.
9666 * This routine is intended primarily to keep the
9667 * kernel maps more compact -- they generally don't
9668 * benefit from the "expand a map entry" technology
9669 * at allocation time because the adjacent entry
9670 * is often wired down.
9673 vm_map_simplify_entry(
9675 vm_map_entry_t this_entry
)
9677 vm_map_entry_t prev_entry
;
9679 counter(c_vm_map_simplify_entry_called
++);
9681 prev_entry
= this_entry
->vme_prev
;
9683 if ((this_entry
!= vm_map_to_entry(map
)) &&
9684 (prev_entry
!= vm_map_to_entry(map
)) &&
9686 (prev_entry
->vme_end
== this_entry
->vme_start
) &&
9688 (prev_entry
->is_sub_map
== this_entry
->is_sub_map
) &&
9690 (prev_entry
->object
.vm_object
== this_entry
->object
.vm_object
) &&
9691 ((prev_entry
->offset
+ (prev_entry
->vme_end
-
9692 prev_entry
->vme_start
))
9693 == this_entry
->offset
) &&
9695 (prev_entry
->inheritance
== this_entry
->inheritance
) &&
9696 (prev_entry
->protection
== this_entry
->protection
) &&
9697 (prev_entry
->max_protection
== this_entry
->max_protection
) &&
9698 (prev_entry
->behavior
== this_entry
->behavior
) &&
9699 (prev_entry
->alias
== this_entry
->alias
) &&
9700 (prev_entry
->zero_wired_pages
== this_entry
->zero_wired_pages
) &&
9701 (prev_entry
->no_cache
== this_entry
->no_cache
) &&
9702 (prev_entry
->wired_count
== this_entry
->wired_count
) &&
9703 (prev_entry
->user_wired_count
== this_entry
->user_wired_count
) &&
9705 (prev_entry
->needs_copy
== this_entry
->needs_copy
) &&
9706 (prev_entry
->permanent
== this_entry
->permanent
) &&
9708 (prev_entry
->use_pmap
== FALSE
) &&
9709 (this_entry
->use_pmap
== FALSE
) &&
9710 (prev_entry
->in_transition
== FALSE
) &&
9711 (this_entry
->in_transition
== FALSE
) &&
9712 (prev_entry
->needs_wakeup
== FALSE
) &&
9713 (this_entry
->needs_wakeup
== FALSE
) &&
9714 (prev_entry
->is_shared
== FALSE
) &&
9715 (this_entry
->is_shared
== FALSE
)
9717 _vm_map_entry_unlink(&map
->hdr
, prev_entry
);
9718 this_entry
->vme_start
= prev_entry
->vme_start
;
9719 this_entry
->offset
= prev_entry
->offset
;
9720 if (prev_entry
->is_sub_map
) {
9721 vm_map_deallocate(prev_entry
->object
.sub_map
);
9723 vm_object_deallocate(prev_entry
->object
.vm_object
);
9725 vm_map_entry_dispose(map
, prev_entry
);
9726 SAVE_HINT_MAP_WRITE(map
, this_entry
);
9727 counter(c_vm_map_simplified
++);
9734 vm_map_offset_t start
)
9736 vm_map_entry_t this_entry
;
9739 if (vm_map_lookup_entry(map
, start
, &this_entry
)) {
9740 vm_map_simplify_entry(map
, this_entry
);
9741 vm_map_simplify_entry(map
, this_entry
->vme_next
);
9743 counter(c_vm_map_simplify_called
++);
9748 vm_map_simplify_range(
9750 vm_map_offset_t start
,
9751 vm_map_offset_t end
)
9753 vm_map_entry_t entry
;
9756 * The map should be locked (for "write") by the caller.
9760 /* invalid address range */
9764 start
= vm_map_trunc_page(start
);
9765 end
= vm_map_round_page(end
);
9767 if (!vm_map_lookup_entry(map
, start
, &entry
)) {
9768 /* "start" is not mapped and "entry" ends before "start" */
9769 if (entry
== vm_map_to_entry(map
)) {
9770 /* start with first entry in the map */
9771 entry
= vm_map_first_entry(map
);
9773 /* start with next entry */
9774 entry
= entry
->vme_next
;
9778 while (entry
!= vm_map_to_entry(map
) &&
9779 entry
->vme_start
<= end
) {
9780 /* try and coalesce "entry" with its previous entry */
9781 vm_map_simplify_entry(map
, entry
);
9782 entry
= entry
->vme_next
;
9788 * Routine: vm_map_machine_attribute
9790 * Provide machine-specific attributes to mappings,
9791 * such as cachability etc. for machines that provide
9792 * them. NUMA architectures and machines with big/strange
9793 * caches will use this.
9795 * Responsibilities for locking and checking are handled here,
9796 * everything else in the pmap module. If any non-volatile
9797 * information must be kept, the pmap module should handle
9798 * it itself. [This assumes that attributes do not
9799 * need to be inherited, which seems ok to me]
9802 vm_map_machine_attribute(
9804 vm_map_offset_t start
,
9805 vm_map_offset_t end
,
9806 vm_machine_attribute_t attribute
,
9807 vm_machine_attribute_val_t
* value
) /* IN/OUT */
9810 vm_map_size_t sync_size
;
9811 vm_map_entry_t entry
;
9813 if (start
< vm_map_min(map
) || end
> vm_map_max(map
))
9814 return KERN_INVALID_ADDRESS
;
9816 /* Figure how much memory we need to flush (in page increments) */
9817 sync_size
= end
- start
;
9821 if (attribute
!= MATTR_CACHE
) {
9822 /* If we don't have to find physical addresses, we */
9823 /* don't have to do an explicit traversal here. */
9824 ret
= pmap_attribute(map
->pmap
, start
, end
-start
,
9830 ret
= KERN_SUCCESS
; /* Assume it all worked */
9833 if (vm_map_lookup_entry(map
, start
, &entry
)) {
9834 vm_map_size_t sub_size
;
9835 if((entry
->vme_end
- start
) > sync_size
) {
9836 sub_size
= sync_size
;
9839 sub_size
= entry
->vme_end
- start
;
9840 sync_size
-= sub_size
;
9842 if(entry
->is_sub_map
) {
9843 vm_map_offset_t sub_start
;
9844 vm_map_offset_t sub_end
;
9846 sub_start
= (start
- entry
->vme_start
)
9848 sub_end
= sub_start
+ sub_size
;
9849 vm_map_machine_attribute(
9850 entry
->object
.sub_map
,
9855 if(entry
->object
.vm_object
) {
9858 vm_object_t base_object
;
9859 vm_object_t last_object
;
9860 vm_object_offset_t offset
;
9861 vm_object_offset_t base_offset
;
9862 vm_map_size_t range
;
9864 offset
= (start
- entry
->vme_start
)
9866 base_offset
= offset
;
9867 object
= entry
->object
.vm_object
;
9868 base_object
= object
;
9871 vm_object_lock(object
);
9877 if (m
&& !m
->fictitious
) {
9879 pmap_attribute_cache_sync(
9884 } else if (object
->shadow
) {
9885 offset
= offset
+ object
->shadow_offset
;
9886 last_object
= object
;
9887 object
= object
->shadow
;
9888 vm_object_lock(last_object
->shadow
);
9889 vm_object_unlock(last_object
);
9894 if (base_object
!= object
) {
9895 vm_object_unlock(object
);
9896 vm_object_lock(base_object
);
9897 object
= base_object
;
9899 /* Bump to the next page */
9900 base_offset
+= PAGE_SIZE
;
9901 offset
= base_offset
;
9903 vm_object_unlock(object
);
9909 return KERN_FAILURE
;
9920 * vm_map_behavior_set:
9922 * Sets the paging reference behavior of the specified address
9923 * range in the target map. Paging reference behavior affects
9924 * how pagein operations resulting from faults on the map will be
9928 vm_map_behavior_set(
9930 vm_map_offset_t start
,
9931 vm_map_offset_t end
,
9932 vm_behavior_t new_behavior
)
9934 register vm_map_entry_t entry
;
9935 vm_map_entry_t temp_entry
;
9938 "vm_map_behavior_set, 0x%X start 0x%X end 0x%X behavior %d",
9939 map
, start
, end
, new_behavior
, 0);
9941 switch (new_behavior
) {
9944 * This first block of behaviors all set a persistent state on the specified
9945 * memory range. All we have to do here is to record the desired behavior
9946 * in the vm_map_entry_t's.
9949 case VM_BEHAVIOR_DEFAULT
:
9950 case VM_BEHAVIOR_RANDOM
:
9951 case VM_BEHAVIOR_SEQUENTIAL
:
9952 case VM_BEHAVIOR_RSEQNTL
:
9953 case VM_BEHAVIOR_ZERO_WIRED_PAGES
:
9957 * The entire address range must be valid for the map.
9958 * Note that vm_map_range_check() does a
9959 * vm_map_lookup_entry() internally and returns the
9960 * entry containing the start of the address range if
9961 * the entire range is valid.
9963 if (vm_map_range_check(map
, start
, end
, &temp_entry
)) {
9965 vm_map_clip_start(map
, entry
, start
);
9969 return(KERN_INVALID_ADDRESS
);
9972 while ((entry
!= vm_map_to_entry(map
)) && (entry
->vme_start
< end
)) {
9973 vm_map_clip_end(map
, entry
, end
);
9974 assert(!entry
->use_pmap
);
9976 if( new_behavior
== VM_BEHAVIOR_ZERO_WIRED_PAGES
) {
9977 entry
->zero_wired_pages
= TRUE
;
9979 entry
->behavior
= new_behavior
;
9981 entry
= entry
->vme_next
;
9988 * The rest of these are different from the above in that they cause
9989 * an immediate action to take place as opposed to setting a behavior that
9990 * affects future actions.
9993 case VM_BEHAVIOR_WILLNEED
:
9994 return vm_map_willneed(map
, start
, end
);
9996 case VM_BEHAVIOR_DONTNEED
:
9997 return vm_map_msync(map
, start
, end
- start
, VM_SYNC_DEACTIVATE
| VM_SYNC_CONTIGUOUS
);
9999 case VM_BEHAVIOR_FREE
:
10000 return vm_map_msync(map
, start
, end
- start
, VM_SYNC_KILLPAGES
| VM_SYNC_CONTIGUOUS
);
10002 case VM_BEHAVIOR_REUSABLE
:
10003 return vm_map_reusable_pages(map
, start
, end
);
10005 case VM_BEHAVIOR_REUSE
:
10006 return vm_map_reuse_pages(map
, start
, end
);
10008 case VM_BEHAVIOR_CAN_REUSE
:
10009 return vm_map_can_reuse(map
, start
, end
);
10012 return(KERN_INVALID_ARGUMENT
);
10015 return(KERN_SUCCESS
);
10020 * Internals for madvise(MADV_WILLNEED) system call.
10022 * The present implementation is to do a read-ahead if the mapping corresponds
10023 * to a mapped regular file. If it's an anonymous mapping, then we do nothing
10024 * and basically ignore the "advice" (which we are always free to do).
10028 static kern_return_t
10031 vm_map_offset_t start
,
10032 vm_map_offset_t end
10035 vm_map_entry_t entry
;
10036 vm_object_t object
;
10037 memory_object_t pager
;
10038 struct vm_object_fault_info fault_info
;
10040 vm_object_size_t len
;
10041 vm_object_offset_t offset
;
10044 * Fill in static values in fault_info. Several fields get ignored by the code
10045 * we call, but we'll fill them in anyway since uninitialized fields are bad
10046 * when it comes to future backwards compatibility.
10049 fault_info
.interruptible
= THREAD_UNINT
; /* ignored value */
10050 fault_info
.behavior
= VM_BEHAVIOR_SEQUENTIAL
;
10051 fault_info
.no_cache
= FALSE
; /* ignored value */
10052 fault_info
.stealth
= TRUE
;
10055 * The MADV_WILLNEED operation doesn't require any changes to the
10056 * vm_map_entry_t's, so the read lock is sufficient.
10059 vm_map_lock_read(map
);
10062 * The madvise semantics require that the address range be fully
10063 * allocated with no holes. Otherwise, we're required to return
10067 if (vm_map_range_check(map
, start
, end
, &entry
)) {
10070 * Examine each vm_map_entry_t in the range.
10073 for (; entry
->vme_start
< end
; start
+= len
, entry
= entry
->vme_next
) {
10076 * The first time through, the start address could be anywhere within the
10077 * vm_map_entry we found. So adjust the offset to correspond. After that,
10078 * the offset will always be zero to correspond to the beginning of the current
10082 offset
= (start
- entry
->vme_start
) + entry
->offset
;
10085 * Set the length so we don't go beyond the end of the map_entry or beyond the
10086 * end of the range we were given. This range could span also multiple map
10087 * entries all of which map different files, so make sure we only do the right
10088 * amount of I/O for each object. Note that it's possible for there to be
10089 * multiple map entries all referring to the same object but with different
10090 * page permissions, but it's not worth trying to optimize that case.
10093 len
= MIN(entry
->vme_end
- start
, end
- start
);
10095 if ((vm_size_t
) len
!= len
) {
10096 /* 32-bit overflow */
10097 len
= (vm_size_t
) (0 - PAGE_SIZE
);
10099 fault_info
.cluster_size
= (vm_size_t
) len
;
10100 fault_info
.lo_offset
= offset
;
10101 fault_info
.hi_offset
= offset
+ len
;
10102 fault_info
.user_tag
= entry
->alias
;
10105 * If there's no read permission to this mapping, then just skip it.
10108 if ((entry
->protection
& VM_PROT_READ
) == 0) {
10113 * Find the file object backing this map entry. If there is none,
10114 * then we simply ignore the "will need" advice for this entry and
10115 * go on to the next one.
10118 if ((object
= find_vnode_object(entry
)) == VM_OBJECT_NULL
) {
10122 vm_object_paging_begin(object
);
10123 pager
= object
->pager
;
10124 vm_object_unlock(object
);
10127 * Get the data from the object asynchronously.
10129 * Note that memory_object_data_request() places limits on the amount
10130 * of I/O it will do. Regardless of the len we specified, it won't do
10131 * more than MAX_UPL_TRANSFER and it silently truncates the len to that
10132 * size. This isn't necessarily bad since madvise shouldn't really be
10133 * used to page in unlimited amounts of data. Other Unix variants limit
10134 * the willneed case as well. If this turns out to be an issue for
10135 * developers, then we can always adjust the policy here and still be
10136 * backwards compatible since this is all just "advice".
10139 kr
= memory_object_data_request(
10141 offset
+ object
->paging_offset
,
10144 (memory_object_fault_info_t
)&fault_info
);
10146 vm_object_lock(object
);
10147 vm_object_paging_end(object
);
10148 vm_object_unlock(object
);
10151 * If we couldn't do the I/O for some reason, just give up on the
10152 * madvise. We still return success to the user since madvise isn't
10153 * supposed to fail when the advice can't be taken.
10156 if (kr
!= KERN_SUCCESS
) {
10163 kr
= KERN_INVALID_ADDRESS
;
10165 vm_map_unlock_read(map
);
10170 vm_map_entry_is_reusable(
10171 vm_map_entry_t entry
)
10173 vm_object_t object
;
10175 if (entry
->is_shared
||
10176 entry
->is_sub_map
||
10177 entry
->in_transition
||
10178 entry
->protection
!= VM_PROT_DEFAULT
||
10179 entry
->max_protection
!= VM_PROT_ALL
||
10180 entry
->inheritance
!= VM_INHERIT_DEFAULT
||
10182 entry
->permanent
||
10183 entry
->superpage_size
!= 0 ||
10184 entry
->zero_wired_pages
||
10185 entry
->wired_count
!= 0 ||
10186 entry
->user_wired_count
!= 0) {
10190 object
= entry
->object
.vm_object
;
10191 if (object
== VM_OBJECT_NULL
) {
10194 if (object
->ref_count
== 1 &&
10195 object
->wired_page_count
== 0 &&
10196 object
->copy
== VM_OBJECT_NULL
&&
10197 object
->shadow
== VM_OBJECT_NULL
&&
10198 object
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
&&
10199 object
->internal
&&
10200 !object
->true_share
&&
10201 object
->wimg_bits
== VM_WIMG_DEFAULT
&&
10202 !object
->code_signed
) {
10210 static kern_return_t
10211 vm_map_reuse_pages(
10213 vm_map_offset_t start
,
10214 vm_map_offset_t end
)
10216 vm_map_entry_t entry
;
10217 vm_object_t object
;
10218 vm_object_offset_t start_offset
, end_offset
;
10221 * The MADV_REUSE operation doesn't require any changes to the
10222 * vm_map_entry_t's, so the read lock is sufficient.
10225 vm_map_lock_read(map
);
10228 * The madvise semantics require that the address range be fully
10229 * allocated with no holes. Otherwise, we're required to return
10233 if (!vm_map_range_check(map
, start
, end
, &entry
)) {
10234 vm_map_unlock_read(map
);
10235 vm_page_stats_reusable
.reuse_pages_failure
++;
10236 return KERN_INVALID_ADDRESS
;
10240 * Examine each vm_map_entry_t in the range.
10242 for (; entry
!= vm_map_to_entry(map
) && entry
->vme_start
< end
;
10243 entry
= entry
->vme_next
) {
10245 * Sanity check on the VM map entry.
10247 if (! vm_map_entry_is_reusable(entry
)) {
10248 vm_map_unlock_read(map
);
10249 vm_page_stats_reusable
.reuse_pages_failure
++;
10250 return KERN_INVALID_ADDRESS
;
10254 * The first time through, the start address could be anywhere
10255 * within the vm_map_entry we found. So adjust the offset to
10258 if (entry
->vme_start
< start
) {
10259 start_offset
= start
- entry
->vme_start
;
10263 end_offset
= MIN(end
, entry
->vme_end
) - entry
->vme_start
;
10264 start_offset
+= entry
->offset
;
10265 end_offset
+= entry
->offset
;
10267 object
= entry
->object
.vm_object
;
10268 if (object
!= VM_OBJECT_NULL
) {
10269 vm_object_lock(object
);
10270 vm_object_reuse_pages(object
, start_offset
, end_offset
,
10272 vm_object_unlock(object
);
10275 if (entry
->alias
== VM_MEMORY_MALLOC_LARGE_REUSABLE
) {
10278 * We do not hold the VM map exclusively here.
10279 * The "alias" field is not that critical, so it's
10280 * safe to update it here, as long as it is the only
10281 * one that can be modified while holding the VM map
10284 entry
->alias
= VM_MEMORY_MALLOC_LARGE_REUSED
;
10288 vm_map_unlock_read(map
);
10289 vm_page_stats_reusable
.reuse_pages_success
++;
10290 return KERN_SUCCESS
;
10294 static kern_return_t
10295 vm_map_reusable_pages(
10297 vm_map_offset_t start
,
10298 vm_map_offset_t end
)
10300 vm_map_entry_t entry
;
10301 vm_object_t object
;
10302 vm_object_offset_t start_offset
, end_offset
;
10305 * The MADV_REUSABLE operation doesn't require any changes to the
10306 * vm_map_entry_t's, so the read lock is sufficient.
10309 vm_map_lock_read(map
);
10312 * The madvise semantics require that the address range be fully
10313 * allocated with no holes. Otherwise, we're required to return
10317 if (!vm_map_range_check(map
, start
, end
, &entry
)) {
10318 vm_map_unlock_read(map
);
10319 vm_page_stats_reusable
.reusable_pages_failure
++;
10320 return KERN_INVALID_ADDRESS
;
10324 * Examine each vm_map_entry_t in the range.
10326 for (; entry
!= vm_map_to_entry(map
) && entry
->vme_start
< end
;
10327 entry
= entry
->vme_next
) {
10328 int kill_pages
= 0;
10331 * Sanity check on the VM map entry.
10333 if (! vm_map_entry_is_reusable(entry
)) {
10334 vm_map_unlock_read(map
);
10335 vm_page_stats_reusable
.reusable_pages_failure
++;
10336 return KERN_INVALID_ADDRESS
;
10340 * The first time through, the start address could be anywhere
10341 * within the vm_map_entry we found. So adjust the offset to
10344 if (entry
->vme_start
< start
) {
10345 start_offset
= start
- entry
->vme_start
;
10349 end_offset
= MIN(end
, entry
->vme_end
) - entry
->vme_start
;
10350 start_offset
+= entry
->offset
;
10351 end_offset
+= entry
->offset
;
10353 object
= entry
->object
.vm_object
;
10354 if (object
== VM_OBJECT_NULL
)
10358 vm_object_lock(object
);
10359 if (object
->ref_count
== 1 && !object
->shadow
)
10363 if (kill_pages
!= -1) {
10364 vm_object_deactivate_pages(object
,
10366 end_offset
- start_offset
,
10368 TRUE
/*reusable_pages*/);
10370 vm_page_stats_reusable
.reusable_pages_shared
++;
10372 vm_object_unlock(object
);
10374 if (entry
->alias
== VM_MEMORY_MALLOC_LARGE
||
10375 entry
->alias
== VM_MEMORY_MALLOC_LARGE_REUSED
) {
10378 * We do not hold the VM map exclusively here.
10379 * The "alias" field is not that critical, so it's
10380 * safe to update it here, as long as it is the only
10381 * one that can be modified while holding the VM map
10384 entry
->alias
= VM_MEMORY_MALLOC_LARGE_REUSABLE
;
10388 vm_map_unlock_read(map
);
10389 vm_page_stats_reusable
.reusable_pages_success
++;
10390 return KERN_SUCCESS
;
10394 static kern_return_t
10397 vm_map_offset_t start
,
10398 vm_map_offset_t end
)
10400 vm_map_entry_t entry
;
10403 * The MADV_REUSABLE operation doesn't require any changes to the
10404 * vm_map_entry_t's, so the read lock is sufficient.
10407 vm_map_lock_read(map
);
10410 * The madvise semantics require that the address range be fully
10411 * allocated with no holes. Otherwise, we're required to return
10415 if (!vm_map_range_check(map
, start
, end
, &entry
)) {
10416 vm_map_unlock_read(map
);
10417 vm_page_stats_reusable
.can_reuse_failure
++;
10418 return KERN_INVALID_ADDRESS
;
10422 * Examine each vm_map_entry_t in the range.
10424 for (; entry
!= vm_map_to_entry(map
) && entry
->vme_start
< end
;
10425 entry
= entry
->vme_next
) {
10427 * Sanity check on the VM map entry.
10429 if (! vm_map_entry_is_reusable(entry
)) {
10430 vm_map_unlock_read(map
);
10431 vm_page_stats_reusable
.can_reuse_failure
++;
10432 return KERN_INVALID_ADDRESS
;
10436 vm_map_unlock_read(map
);
10437 vm_page_stats_reusable
.can_reuse_success
++;
10438 return KERN_SUCCESS
;
10443 #include <mach_kdb.h>
10445 #include <ddb/db_output.h>
10446 #include <vm/vm_print.h>
10448 #define printf db_printf
10451 * Forward declarations for internal functions.
10453 extern void vm_map_links_print(
10454 struct vm_map_links
*links
);
10456 extern void vm_map_header_print(
10457 struct vm_map_header
*header
);
10459 extern void vm_map_entry_print(
10460 vm_map_entry_t entry
);
10462 extern void vm_follow_entry(
10463 vm_map_entry_t entry
);
10465 extern void vm_follow_map(
10469 * vm_map_links_print: [ debug ]
10472 vm_map_links_print(
10473 struct vm_map_links
*links
)
10475 iprintf("prev = %08X next = %08X start = %016llX end = %016llX\n",
10478 (unsigned long long)links
->start
,
10479 (unsigned long long)links
->end
);
10483 * vm_map_header_print: [ debug ]
10486 vm_map_header_print(
10487 struct vm_map_header
*header
)
10489 vm_map_links_print(&header
->links
);
10490 iprintf("nentries = %08X, %sentries_pageable\n",
10492 (header
->entries_pageable
? "" : "!"));
10496 * vm_follow_entry: [ debug ]
10500 vm_map_entry_t entry
)
10504 iprintf("map entry %08X\n", entry
);
10508 shadows
= vm_follow_object(entry
->object
.vm_object
);
10509 iprintf("Total objects : %d\n",shadows
);
10515 * vm_map_entry_print: [ debug ]
10518 vm_map_entry_print(
10519 register vm_map_entry_t entry
)
10521 static const char *inheritance_name
[4] =
10522 { "share", "copy", "none", "?"};
10523 static const char *behavior_name
[4] =
10524 { "dflt", "rand", "seqtl", "rseqntl" };
10526 iprintf("map entry %08X - prev = %08X next = %08X\n", entry
, entry
->vme_prev
, entry
->vme_next
);
10530 vm_map_links_print(&entry
->links
);
10532 iprintf("start = %016llX end = %016llX - prot=%x/%x/%s\n",
10533 (unsigned long long)entry
->vme_start
,
10534 (unsigned long long)entry
->vme_end
,
10536 entry
->max_protection
,
10537 inheritance_name
[(entry
->inheritance
& 0x3)]);
10539 iprintf("behavior = %s, wired_count = %d, user_wired_count = %d\n",
10540 behavior_name
[(entry
->behavior
& 0x3)],
10541 entry
->wired_count
,
10542 entry
->user_wired_count
);
10543 iprintf("%sin_transition, %sneeds_wakeup\n",
10544 (entry
->in_transition
? "" : "!"),
10545 (entry
->needs_wakeup
? "" : "!"));
10547 if (entry
->is_sub_map
) {
10548 iprintf("submap = %08X - offset = %016llX\n",
10549 entry
->object
.sub_map
,
10550 (unsigned long long)entry
->offset
);
10552 iprintf("object = %08X offset = %016llX - ",
10553 entry
->object
.vm_object
,
10554 (unsigned long long)entry
->offset
);
10555 printf("%sis_shared, %sneeds_copy\n",
10556 (entry
->is_shared
? "" : "!"),
10557 (entry
->needs_copy
? "" : "!"));
10564 * vm_follow_map: [ debug ]
10570 register vm_map_entry_t entry
;
10572 iprintf("task map %08X\n", map
);
10576 for (entry
= vm_map_first_entry(map
);
10577 entry
&& entry
!= vm_map_to_entry(map
);
10578 entry
= entry
->vme_next
) {
10579 vm_follow_entry(entry
);
10586 * vm_map_print: [ debug ]
10592 register vm_map_entry_t entry
;
10596 #endif /* TASK_SWAPPER */
10598 map
= (vm_map_t
)(long)
10599 inmap
; /* Make sure we have the right type */
10601 iprintf("task map %08X\n", map
);
10605 vm_map_header_print(&map
->hdr
);
10607 iprintf("pmap = %08X size = %08X ref = %d hint = %08X first_free = %08X\n",
10614 iprintf("%swait_for_space, %swiring_required, timestamp = %d\n",
10615 (map
->wait_for_space
? "" : "!"),
10616 (map
->wiring_required
? "" : "!"),
10620 switch (map
->sw_state
) {
10625 swstate
= "SW_OUT";
10631 iprintf("res = %d, sw_state = %s\n", map
->res_count
, swstate
);
10632 #endif /* TASK_SWAPPER */
10634 for (entry
= vm_map_first_entry(map
);
10635 entry
&& entry
!= vm_map_to_entry(map
);
10636 entry
= entry
->vme_next
) {
10637 vm_map_entry_print(entry
);
10644 * Routine: vm_map_copy_print
10646 * Pretty-print a copy object for ddb.
10653 vm_map_copy_t copy
;
10654 vm_map_entry_t entry
;
10656 copy
= (vm_map_copy_t
)(long)
10657 incopy
; /* Make sure we have the right type */
10659 printf("copy object 0x%x\n", copy
);
10663 iprintf("type=%d", copy
->type
);
10664 switch (copy
->type
) {
10665 case VM_MAP_COPY_ENTRY_LIST
:
10666 printf("[entry_list]");
10669 case VM_MAP_COPY_OBJECT
:
10670 printf("[object]");
10673 case VM_MAP_COPY_KERNEL_BUFFER
:
10674 printf("[kernel_buffer]");
10678 printf("[bad type]");
10681 printf(", offset=0x%llx", (unsigned long long)copy
->offset
);
10682 printf(", size=0x%x\n", copy
->size
);
10684 switch (copy
->type
) {
10685 case VM_MAP_COPY_ENTRY_LIST
:
10686 vm_map_header_print(©
->cpy_hdr
);
10687 for (entry
= vm_map_copy_first_entry(copy
);
10688 entry
&& entry
!= vm_map_copy_to_entry(copy
);
10689 entry
= entry
->vme_next
) {
10690 vm_map_entry_print(entry
);
10694 case VM_MAP_COPY_OBJECT
:
10695 iprintf("object=0x%x\n", copy
->cpy_object
);
10698 case VM_MAP_COPY_KERNEL_BUFFER
:
10699 iprintf("kernel buffer=0x%x", copy
->cpy_kdata
);
10700 printf(", kalloc_size=0x%x\n", copy
->cpy_kalloc_size
);
10709 * db_vm_map_total_size(map) [ debug ]
10711 * return the total virtual size (in bytes) of the map
10714 db_vm_map_total_size(
10717 vm_map_entry_t entry
;
10718 vm_map_size_t total
;
10721 map
= (vm_map_t
)(long)
10722 inmap
; /* Make sure we have the right type */
10725 for (entry
= vm_map_first_entry(map
);
10726 entry
!= vm_map_to_entry(map
);
10727 entry
= entry
->vme_next
) {
10728 total
+= entry
->vme_end
- entry
->vme_start
;
10734 #endif /* MACH_KDB */
10737 * Routine: vm_map_entry_insert
10739 * Descritpion: This routine inserts a new vm_entry in a locked map.
10742 vm_map_entry_insert(
10744 vm_map_entry_t insp_entry
,
10745 vm_map_offset_t start
,
10746 vm_map_offset_t end
,
10747 vm_object_t object
,
10748 vm_object_offset_t offset
,
10749 boolean_t needs_copy
,
10750 boolean_t is_shared
,
10751 boolean_t in_transition
,
10752 vm_prot_t cur_protection
,
10753 vm_prot_t max_protection
,
10754 vm_behavior_t behavior
,
10755 vm_inherit_t inheritance
,
10756 unsigned wired_count
,
10757 boolean_t no_cache
,
10758 boolean_t permanent
,
10759 unsigned int superpage_size
)
10761 vm_map_entry_t new_entry
;
10763 assert(insp_entry
!= (vm_map_entry_t
)0);
10765 new_entry
= vm_map_entry_create(map
);
10767 new_entry
->vme_start
= start
;
10768 new_entry
->vme_end
= end
;
10769 assert(page_aligned(new_entry
->vme_start
));
10770 assert(page_aligned(new_entry
->vme_end
));
10772 new_entry
->object
.vm_object
= object
;
10773 new_entry
->offset
= offset
;
10774 new_entry
->is_shared
= is_shared
;
10775 new_entry
->is_sub_map
= FALSE
;
10776 new_entry
->needs_copy
= needs_copy
;
10777 new_entry
->in_transition
= in_transition
;
10778 new_entry
->needs_wakeup
= FALSE
;
10779 new_entry
->inheritance
= inheritance
;
10780 new_entry
->protection
= cur_protection
;
10781 new_entry
->max_protection
= max_protection
;
10782 new_entry
->behavior
= behavior
;
10783 new_entry
->wired_count
= wired_count
;
10784 new_entry
->user_wired_count
= 0;
10785 new_entry
->use_pmap
= FALSE
;
10786 new_entry
->alias
= 0;
10787 new_entry
->zero_wired_pages
= FALSE
;
10788 new_entry
->no_cache
= no_cache
;
10789 new_entry
->permanent
= permanent
;
10790 new_entry
->superpage_size
= superpage_size
;
10793 * Insert the new entry into the list.
10796 vm_map_entry_link(map
, insp_entry
, new_entry
);
10797 map
->size
+= end
- start
;
10800 * Update the free space hint and the lookup hint.
10803 SAVE_HINT_MAP_WRITE(map
, new_entry
);
10808 * Routine: vm_map_remap_extract
10810 * Descritpion: This routine returns a vm_entry list from a map.
10812 static kern_return_t
10813 vm_map_remap_extract(
10815 vm_map_offset_t addr
,
10816 vm_map_size_t size
,
10818 struct vm_map_header
*map_header
,
10819 vm_prot_t
*cur_protection
,
10820 vm_prot_t
*max_protection
,
10821 /* What, no behavior? */
10822 vm_inherit_t inheritance
,
10823 boolean_t pageable
)
10825 kern_return_t result
;
10826 vm_map_size_t mapped_size
;
10827 vm_map_size_t tmp_size
;
10828 vm_map_entry_t src_entry
; /* result of last map lookup */
10829 vm_map_entry_t new_entry
;
10830 vm_object_offset_t offset
;
10831 vm_map_offset_t map_address
;
10832 vm_map_offset_t src_start
; /* start of entry to map */
10833 vm_map_offset_t src_end
; /* end of region to be mapped */
10834 vm_object_t object
;
10835 vm_map_version_t version
;
10836 boolean_t src_needs_copy
;
10837 boolean_t new_entry_needs_copy
;
10839 assert(map
!= VM_MAP_NULL
);
10840 assert(size
!= 0 && size
== vm_map_round_page(size
));
10841 assert(inheritance
== VM_INHERIT_NONE
||
10842 inheritance
== VM_INHERIT_COPY
||
10843 inheritance
== VM_INHERIT_SHARE
);
10846 * Compute start and end of region.
10848 src_start
= vm_map_trunc_page(addr
);
10849 src_end
= vm_map_round_page(src_start
+ size
);
10852 * Initialize map_header.
10854 map_header
->links
.next
= (struct vm_map_entry
*)&map_header
->links
;
10855 map_header
->links
.prev
= (struct vm_map_entry
*)&map_header
->links
;
10856 map_header
->nentries
= 0;
10857 map_header
->entries_pageable
= pageable
;
10859 *cur_protection
= VM_PROT_ALL
;
10860 *max_protection
= VM_PROT_ALL
;
10864 result
= KERN_SUCCESS
;
10867 * The specified source virtual space might correspond to
10868 * multiple map entries, need to loop on them.
10871 while (mapped_size
!= size
) {
10872 vm_map_size_t entry_size
;
10875 * Find the beginning of the region.
10877 if (! vm_map_lookup_entry(map
, src_start
, &src_entry
)) {
10878 result
= KERN_INVALID_ADDRESS
;
10882 if (src_start
< src_entry
->vme_start
||
10883 (mapped_size
&& src_start
!= src_entry
->vme_start
)) {
10884 result
= KERN_INVALID_ADDRESS
;
10888 tmp_size
= size
- mapped_size
;
10889 if (src_end
> src_entry
->vme_end
)
10890 tmp_size
-= (src_end
- src_entry
->vme_end
);
10892 entry_size
= (vm_map_size_t
)(src_entry
->vme_end
-
10893 src_entry
->vme_start
);
10895 if(src_entry
->is_sub_map
) {
10896 vm_map_reference(src_entry
->object
.sub_map
);
10897 object
= VM_OBJECT_NULL
;
10899 object
= src_entry
->object
.vm_object
;
10901 if (object
== VM_OBJECT_NULL
) {
10902 object
= vm_object_allocate(entry_size
);
10903 src_entry
->offset
= 0;
10904 src_entry
->object
.vm_object
= object
;
10905 } else if (object
->copy_strategy
!=
10906 MEMORY_OBJECT_COPY_SYMMETRIC
) {
10908 * We are already using an asymmetric
10909 * copy, and therefore we already have
10910 * the right object.
10912 assert(!src_entry
->needs_copy
);
10913 } else if (src_entry
->needs_copy
|| object
->shadowed
||
10914 (object
->internal
&& !object
->true_share
&&
10915 !src_entry
->is_shared
&&
10916 object
->size
> entry_size
)) {
10918 vm_object_shadow(&src_entry
->object
.vm_object
,
10919 &src_entry
->offset
,
10922 if (!src_entry
->needs_copy
&&
10923 (src_entry
->protection
& VM_PROT_WRITE
)) {
10926 prot
= src_entry
->protection
& ~VM_PROT_WRITE
;
10928 if (override_nx(map
, src_entry
->alias
) && prot
)
10929 prot
|= VM_PROT_EXECUTE
;
10932 vm_object_pmap_protect(
10933 src_entry
->object
.vm_object
,
10937 src_entry
->vme_start
,
10940 pmap_protect(vm_map_pmap(map
),
10941 src_entry
->vme_start
,
10942 src_entry
->vme_end
,
10947 object
= src_entry
->object
.vm_object
;
10948 src_entry
->needs_copy
= FALSE
;
10952 vm_object_lock(object
);
10953 vm_object_reference_locked(object
); /* object ref. for new entry */
10954 if (object
->copy_strategy
==
10955 MEMORY_OBJECT_COPY_SYMMETRIC
) {
10956 object
->copy_strategy
=
10957 MEMORY_OBJECT_COPY_DELAY
;
10959 vm_object_unlock(object
);
10962 offset
= src_entry
->offset
+ (src_start
- src_entry
->vme_start
);
10964 new_entry
= _vm_map_entry_create(map_header
);
10965 vm_map_entry_copy(new_entry
, src_entry
);
10966 new_entry
->use_pmap
= FALSE
; /* clr address space specifics */
10968 new_entry
->vme_start
= map_address
;
10969 new_entry
->vme_end
= map_address
+ tmp_size
;
10970 new_entry
->inheritance
= inheritance
;
10971 new_entry
->offset
= offset
;
10974 * The new region has to be copied now if required.
10978 src_entry
->is_shared
= TRUE
;
10979 new_entry
->is_shared
= TRUE
;
10980 if (!(new_entry
->is_sub_map
))
10981 new_entry
->needs_copy
= FALSE
;
10983 } else if (src_entry
->is_sub_map
) {
10984 /* make this a COW sub_map if not already */
10985 new_entry
->needs_copy
= TRUE
;
10986 object
= VM_OBJECT_NULL
;
10987 } else if (src_entry
->wired_count
== 0 &&
10988 vm_object_copy_quickly(&new_entry
->object
.vm_object
,
10990 (new_entry
->vme_end
-
10991 new_entry
->vme_start
),
10993 &new_entry_needs_copy
)) {
10995 new_entry
->needs_copy
= new_entry_needs_copy
;
10996 new_entry
->is_shared
= FALSE
;
10999 * Handle copy_on_write semantics.
11001 if (src_needs_copy
&& !src_entry
->needs_copy
) {
11004 prot
= src_entry
->protection
& ~VM_PROT_WRITE
;
11006 if (override_nx(map
, src_entry
->alias
) && prot
)
11007 prot
|= VM_PROT_EXECUTE
;
11009 vm_object_pmap_protect(object
,
11012 ((src_entry
->is_shared
11014 PMAP_NULL
: map
->pmap
),
11015 src_entry
->vme_start
,
11018 src_entry
->needs_copy
= TRUE
;
11021 * Throw away the old object reference of the new entry.
11023 vm_object_deallocate(object
);
11026 new_entry
->is_shared
= FALSE
;
11029 * The map can be safely unlocked since we
11030 * already hold a reference on the object.
11032 * Record the timestamp of the map for later
11033 * verification, and unlock the map.
11035 version
.main_timestamp
= map
->timestamp
;
11036 vm_map_unlock(map
); /* Increments timestamp once! */
11039 * Perform the copy.
11041 if (src_entry
->wired_count
> 0) {
11042 vm_object_lock(object
);
11043 result
= vm_object_copy_slowly(
11048 &new_entry
->object
.vm_object
);
11050 new_entry
->offset
= 0;
11051 new_entry
->needs_copy
= FALSE
;
11053 result
= vm_object_copy_strategically(
11057 &new_entry
->object
.vm_object
,
11058 &new_entry
->offset
,
11059 &new_entry_needs_copy
);
11061 new_entry
->needs_copy
= new_entry_needs_copy
;
11065 * Throw away the old object reference of the new entry.
11067 vm_object_deallocate(object
);
11069 if (result
!= KERN_SUCCESS
&&
11070 result
!= KERN_MEMORY_RESTART_COPY
) {
11071 _vm_map_entry_dispose(map_header
, new_entry
);
11076 * Verify that the map has not substantially
11077 * changed while the copy was being made.
11081 if (version
.main_timestamp
+ 1 != map
->timestamp
) {
11083 * Simple version comparison failed.
11085 * Retry the lookup and verify that the
11086 * same object/offset are still present.
11088 vm_object_deallocate(new_entry
->
11090 _vm_map_entry_dispose(map_header
, new_entry
);
11091 if (result
== KERN_MEMORY_RESTART_COPY
)
11092 result
= KERN_SUCCESS
;
11096 if (result
== KERN_MEMORY_RESTART_COPY
) {
11097 vm_object_reference(object
);
11102 _vm_map_entry_link(map_header
,
11103 map_header
->links
.prev
, new_entry
);
11105 *cur_protection
&= src_entry
->protection
;
11106 *max_protection
&= src_entry
->max_protection
;
11108 map_address
+= tmp_size
;
11109 mapped_size
+= tmp_size
;
11110 src_start
+= tmp_size
;
11114 vm_map_unlock(map
);
11115 if (result
!= KERN_SUCCESS
) {
11117 * Free all allocated elements.
11119 for (src_entry
= map_header
->links
.next
;
11120 src_entry
!= (struct vm_map_entry
*)&map_header
->links
;
11121 src_entry
= new_entry
) {
11122 new_entry
= src_entry
->vme_next
;
11123 _vm_map_entry_unlink(map_header
, src_entry
);
11124 vm_object_deallocate(src_entry
->object
.vm_object
);
11125 _vm_map_entry_dispose(map_header
, src_entry
);
11132 * Routine: vm_remap
11134 * Map portion of a task's address space.
11135 * Mapped region must not overlap more than
11136 * one vm memory object. Protections and
11137 * inheritance attributes remain the same
11138 * as in the original task and are out parameters.
11139 * Source and Target task can be identical
11140 * Other attributes are identical as for vm_map()
11144 vm_map_t target_map
,
11145 vm_map_address_t
*address
,
11146 vm_map_size_t size
,
11147 vm_map_offset_t mask
,
11148 boolean_t anywhere
,
11150 vm_map_offset_t memory_address
,
11152 vm_prot_t
*cur_protection
,
11153 vm_prot_t
*max_protection
,
11154 vm_inherit_t inheritance
)
11156 kern_return_t result
;
11157 vm_map_entry_t entry
;
11158 vm_map_entry_t insp_entry
= VM_MAP_ENTRY_NULL
;
11159 vm_map_entry_t new_entry
;
11160 struct vm_map_header map_header
;
11162 if (target_map
== VM_MAP_NULL
)
11163 return KERN_INVALID_ARGUMENT
;
11165 switch (inheritance
) {
11166 case VM_INHERIT_NONE
:
11167 case VM_INHERIT_COPY
:
11168 case VM_INHERIT_SHARE
:
11169 if (size
!= 0 && src_map
!= VM_MAP_NULL
)
11173 return KERN_INVALID_ARGUMENT
;
11176 size
= vm_map_round_page(size
);
11178 result
= vm_map_remap_extract(src_map
, memory_address
,
11179 size
, copy
, &map_header
,
11186 if (result
!= KERN_SUCCESS
) {
11191 * Allocate/check a range of free virtual address
11192 * space for the target
11194 *address
= vm_map_trunc_page(*address
);
11195 vm_map_lock(target_map
);
11196 result
= vm_map_remap_range_allocate(target_map
, address
, size
,
11197 mask
, anywhere
, &insp_entry
);
11199 for (entry
= map_header
.links
.next
;
11200 entry
!= (struct vm_map_entry
*)&map_header
.links
;
11201 entry
= new_entry
) {
11202 new_entry
= entry
->vme_next
;
11203 _vm_map_entry_unlink(&map_header
, entry
);
11204 if (result
== KERN_SUCCESS
) {
11205 entry
->vme_start
+= *address
;
11206 entry
->vme_end
+= *address
;
11207 vm_map_entry_link(target_map
, insp_entry
, entry
);
11208 insp_entry
= entry
;
11210 if (!entry
->is_sub_map
) {
11211 vm_object_deallocate(entry
->object
.vm_object
);
11213 vm_map_deallocate(entry
->object
.sub_map
);
11215 _vm_map_entry_dispose(&map_header
, entry
);
11219 if (result
== KERN_SUCCESS
) {
11220 target_map
->size
+= size
;
11221 SAVE_HINT_MAP_WRITE(target_map
, insp_entry
);
11223 vm_map_unlock(target_map
);
11225 if (result
== KERN_SUCCESS
&& target_map
->wiring_required
)
11226 result
= vm_map_wire(target_map
, *address
,
11227 *address
+ size
, *cur_protection
, TRUE
);
11232 * Routine: vm_map_remap_range_allocate
11235 * Allocate a range in the specified virtual address map.
11236 * returns the address and the map entry just before the allocated
11239 * Map must be locked.
11242 static kern_return_t
11243 vm_map_remap_range_allocate(
11245 vm_map_address_t
*address
, /* IN/OUT */
11246 vm_map_size_t size
,
11247 vm_map_offset_t mask
,
11248 boolean_t anywhere
,
11249 vm_map_entry_t
*map_entry
) /* OUT */
11251 register vm_map_entry_t entry
;
11252 register vm_map_offset_t start
;
11253 register vm_map_offset_t end
;
11262 * Calculate the first possible address.
11265 if (start
< map
->min_offset
)
11266 start
= map
->min_offset
;
11267 if (start
> map
->max_offset
)
11268 return(KERN_NO_SPACE
);
11271 * Look for the first possible address;
11272 * if there's already something at this
11273 * address, we have to start after it.
11276 assert(first_free_is_valid(map
));
11277 if (start
== map
->min_offset
) {
11278 if ((entry
= map
->first_free
) != vm_map_to_entry(map
))
11279 start
= entry
->vme_end
;
11281 vm_map_entry_t tmp_entry
;
11282 if (vm_map_lookup_entry(map
, start
, &tmp_entry
))
11283 start
= tmp_entry
->vme_end
;
11288 * In any case, the "entry" always precedes
11289 * the proposed new region throughout the
11294 register vm_map_entry_t next
;
11297 * Find the end of the proposed new region.
11298 * Be sure we didn't go beyond the end, or
11299 * wrap around the address.
11302 end
= ((start
+ mask
) & ~mask
);
11304 return(KERN_NO_SPACE
);
11308 if ((end
> map
->max_offset
) || (end
< start
)) {
11309 if (map
->wait_for_space
) {
11310 if (size
<= (map
->max_offset
-
11311 map
->min_offset
)) {
11312 assert_wait((event_t
) map
, THREAD_INTERRUPTIBLE
);
11313 vm_map_unlock(map
);
11314 thread_block(THREAD_CONTINUE_NULL
);
11320 return(KERN_NO_SPACE
);
11324 * If there are no more entries, we must win.
11327 next
= entry
->vme_next
;
11328 if (next
== vm_map_to_entry(map
))
11332 * If there is another entry, it must be
11333 * after the end of the potential new region.
11336 if (next
->vme_start
>= end
)
11340 * Didn't fit -- move to the next entry.
11344 start
= entry
->vme_end
;
11348 vm_map_entry_t temp_entry
;
11352 * the address doesn't itself violate
11353 * the mask requirement.
11356 if ((start
& mask
) != 0)
11357 return(KERN_NO_SPACE
);
11361 * ... the address is within bounds
11364 end
= start
+ size
;
11366 if ((start
< map
->min_offset
) ||
11367 (end
> map
->max_offset
) ||
11369 return(KERN_INVALID_ADDRESS
);
11373 * ... the starting address isn't allocated
11376 if (vm_map_lookup_entry(map
, start
, &temp_entry
))
11377 return(KERN_NO_SPACE
);
11379 entry
= temp_entry
;
11382 * ... the next region doesn't overlap the
11386 if ((entry
->vme_next
!= vm_map_to_entry(map
)) &&
11387 (entry
->vme_next
->vme_start
< end
))
11388 return(KERN_NO_SPACE
);
11390 *map_entry
= entry
;
11391 return(KERN_SUCCESS
);
11397 * Set the address map for the current thread to the specified map
11405 thread_t thread
= current_thread();
11406 vm_map_t oldmap
= thread
->map
;
11408 mp_disable_preemption();
11409 mycpu
= cpu_number();
11412 * Deactivate the current map and activate the requested map
11414 PMAP_SWITCH_USER(thread
, map
, mycpu
);
11416 mp_enable_preemption();
11422 * Routine: vm_map_write_user
11425 * Copy out data from a kernel space into space in the
11426 * destination map. The space must already exist in the
11428 * NOTE: This routine should only be called by threads
11429 * which can block on a page fault. i.e. kernel mode user
11437 vm_map_address_t dst_addr
,
11440 kern_return_t kr
= KERN_SUCCESS
;
11442 if(current_map() == map
) {
11443 if (copyout(src_p
, dst_addr
, size
)) {
11444 kr
= KERN_INVALID_ADDRESS
;
11449 /* take on the identity of the target map while doing */
11452 vm_map_reference(map
);
11453 oldmap
= vm_map_switch(map
);
11454 if (copyout(src_p
, dst_addr
, size
)) {
11455 kr
= KERN_INVALID_ADDRESS
;
11457 vm_map_switch(oldmap
);
11458 vm_map_deallocate(map
);
11464 * Routine: vm_map_read_user
11467 * Copy in data from a user space source map into the
11468 * kernel map. The space must already exist in the
11470 * NOTE: This routine should only be called by threads
11471 * which can block on a page fault. i.e. kernel mode user
11478 vm_map_address_t src_addr
,
11482 kern_return_t kr
= KERN_SUCCESS
;
11484 if(current_map() == map
) {
11485 if (copyin(src_addr
, dst_p
, size
)) {
11486 kr
= KERN_INVALID_ADDRESS
;
11491 /* take on the identity of the target map while doing */
11494 vm_map_reference(map
);
11495 oldmap
= vm_map_switch(map
);
11496 if (copyin(src_addr
, dst_p
, size
)) {
11497 kr
= KERN_INVALID_ADDRESS
;
11499 vm_map_switch(oldmap
);
11500 vm_map_deallocate(map
);
11507 * vm_map_check_protection:
11509 * Assert that the target map allows the specified
11510 * privilege on the entire address region given.
11511 * The entire region must be allocated.
11514 vm_map_check_protection(vm_map_t map
, vm_map_offset_t start
,
11515 vm_map_offset_t end
, vm_prot_t protection
)
11517 vm_map_entry_t entry
;
11518 vm_map_entry_t tmp_entry
;
11522 if (start
< vm_map_min(map
) || end
> vm_map_max(map
) || start
> end
)
11524 vm_map_unlock(map
);
11528 if (!vm_map_lookup_entry(map
, start
, &tmp_entry
)) {
11529 vm_map_unlock(map
);
11535 while (start
< end
) {
11536 if (entry
== vm_map_to_entry(map
)) {
11537 vm_map_unlock(map
);
11542 * No holes allowed!
11545 if (start
< entry
->vme_start
) {
11546 vm_map_unlock(map
);
11551 * Check protection associated with entry.
11554 if ((entry
->protection
& protection
) != protection
) {
11555 vm_map_unlock(map
);
11559 /* go to next entry */
11561 start
= entry
->vme_end
;
11562 entry
= entry
->vme_next
;
11564 vm_map_unlock(map
);
11569 vm_map_purgable_control(
11571 vm_map_offset_t address
,
11572 vm_purgable_t control
,
11575 vm_map_entry_t entry
;
11576 vm_object_t object
;
11580 * Vet all the input parameters and current type and state of the
11581 * underlaying object. Return with an error if anything is amiss.
11583 if (map
== VM_MAP_NULL
)
11584 return(KERN_INVALID_ARGUMENT
);
11586 if (control
!= VM_PURGABLE_SET_STATE
&&
11587 control
!= VM_PURGABLE_GET_STATE
&&
11588 control
!= VM_PURGABLE_PURGE_ALL
)
11589 return(KERN_INVALID_ARGUMENT
);
11591 if (control
== VM_PURGABLE_PURGE_ALL
) {
11592 vm_purgeable_object_purge_all();
11593 return KERN_SUCCESS
;
11596 if (control
== VM_PURGABLE_SET_STATE
&&
11597 (((*state
& ~(VM_PURGABLE_ALL_MASKS
)) != 0) ||
11598 ((*state
& VM_PURGABLE_STATE_MASK
) > VM_PURGABLE_STATE_MASK
)))
11599 return(KERN_INVALID_ARGUMENT
);
11601 vm_map_lock_read(map
);
11603 if (!vm_map_lookup_entry(map
, address
, &entry
) || entry
->is_sub_map
) {
11606 * Must pass a valid non-submap address.
11608 vm_map_unlock_read(map
);
11609 return(KERN_INVALID_ADDRESS
);
11612 if ((entry
->protection
& VM_PROT_WRITE
) == 0) {
11614 * Can't apply purgable controls to something you can't write.
11616 vm_map_unlock_read(map
);
11617 return(KERN_PROTECTION_FAILURE
);
11620 object
= entry
->object
.vm_object
;
11621 if (object
== VM_OBJECT_NULL
) {
11623 * Object must already be present or it can't be purgable.
11625 vm_map_unlock_read(map
);
11626 return KERN_INVALID_ARGUMENT
;
11629 vm_object_lock(object
);
11631 if (entry
->offset
!= 0 ||
11632 entry
->vme_end
- entry
->vme_start
!= object
->size
) {
11634 * Can only apply purgable controls to the whole (existing)
11637 vm_map_unlock_read(map
);
11638 vm_object_unlock(object
);
11639 return KERN_INVALID_ARGUMENT
;
11642 vm_map_unlock_read(map
);
11644 kr
= vm_object_purgable_control(object
, control
, state
);
11646 vm_object_unlock(object
);
11652 vm_map_page_query_internal(
11653 vm_map_t target_map
,
11654 vm_map_offset_t offset
,
11659 vm_page_info_basic_data_t info
;
11660 mach_msg_type_number_t count
;
11662 count
= VM_PAGE_INFO_BASIC_COUNT
;
11663 kr
= vm_map_page_info(target_map
,
11665 VM_PAGE_INFO_BASIC
,
11666 (vm_page_info_t
) &info
,
11668 if (kr
== KERN_SUCCESS
) {
11669 *disposition
= info
.disposition
;
11670 *ref_count
= info
.ref_count
;
11682 vm_map_offset_t offset
,
11683 vm_page_info_flavor_t flavor
,
11684 vm_page_info_t info
,
11685 mach_msg_type_number_t
*count
)
11687 vm_map_entry_t map_entry
;
11688 vm_object_t object
;
11691 kern_return_t retval
= KERN_SUCCESS
;
11692 boolean_t top_object
;
11695 vm_object_id_t object_id
;
11696 vm_page_info_basic_t basic_info
;
11700 case VM_PAGE_INFO_BASIC
:
11701 if (*count
!= VM_PAGE_INFO_BASIC_COUNT
) {
11702 return KERN_INVALID_ARGUMENT
;
11706 return KERN_INVALID_ARGUMENT
;
11715 retval
= KERN_SUCCESS
;
11716 offset
= vm_map_trunc_page(offset
);
11718 vm_map_lock_read(map
);
11721 * First, find the map entry covering "offset", going down
11722 * submaps if necessary.
11725 if (!vm_map_lookup_entry(map
, offset
, &map_entry
)) {
11726 vm_map_unlock_read(map
);
11727 return KERN_INVALID_ADDRESS
;
11729 /* compute offset from this map entry's start */
11730 offset
-= map_entry
->vme_start
;
11731 /* compute offset into this map entry's object (or submap) */
11732 offset
+= map_entry
->offset
;
11734 if (map_entry
->is_sub_map
) {
11737 sub_map
= map_entry
->object
.sub_map
;
11738 vm_map_lock_read(sub_map
);
11739 vm_map_unlock_read(map
);
11743 ref_count
= MAX(ref_count
, map
->ref_count
);
11749 object
= map_entry
->object
.vm_object
;
11750 if (object
== VM_OBJECT_NULL
) {
11751 /* no object -> no page */
11752 vm_map_unlock_read(map
);
11756 vm_object_lock(object
);
11757 vm_map_unlock_read(map
);
11760 * Go down the VM object shadow chain until we find the page
11761 * we're looking for.
11764 ref_count
= MAX(ref_count
, object
->ref_count
);
11766 m
= vm_page_lookup(object
, offset
);
11768 if (m
!= VM_PAGE_NULL
) {
11769 disposition
|= VM_PAGE_QUERY_PAGE_PRESENT
;
11773 if (object
->existence_map
) {
11774 if (vm_external_state_get(object
->existence_map
,
11776 VM_EXTERNAL_STATE_EXISTS
) {
11778 * this page has been paged out
11780 disposition
|= VM_PAGE_QUERY_PAGE_PAGED_OUT
;
11786 if (object
->internal
&&
11788 !object
->terminating
&&
11789 object
->pager_ready
) {
11791 memory_object_t pager
;
11793 vm_object_paging_begin(object
);
11794 pager
= object
->pager
;
11795 vm_object_unlock(object
);
11798 * Ask the default pager if
11799 * it has this page.
11801 kr
= memory_object_data_request(
11803 offset
+ object
->paging_offset
,
11804 0, /* just poke the pager */
11808 vm_object_lock(object
);
11809 vm_object_paging_end(object
);
11811 if (kr
== KERN_SUCCESS
) {
11812 /* the default pager has it */
11813 disposition
|= VM_PAGE_QUERY_PAGE_PAGED_OUT
;
11819 if (object
->shadow
!= VM_OBJECT_NULL
) {
11820 vm_object_t shadow
;
11822 offset
+= object
->shadow_offset
;
11823 shadow
= object
->shadow
;
11825 vm_object_lock(shadow
);
11826 vm_object_unlock(object
);
11829 top_object
= FALSE
;
11832 // if (!object->internal)
11834 // retval = KERN_FAILURE;
11835 // goto done_with_object;
11840 /* The ref_count is not strictly accurate, it measures the number */
11841 /* of entities holding a ref on the object, they may not be mapping */
11842 /* the object or may not be mapping the section holding the */
11843 /* target page but its still a ball park number and though an over- */
11844 /* count, it picks up the copy-on-write cases */
11846 /* We could also get a picture of page sharing from pmap_attributes */
11847 /* but this would under count as only faulted-in mappings would */
11850 if (top_object
== TRUE
&& object
->shadow
)
11851 disposition
|= VM_PAGE_QUERY_PAGE_COPIED
;
11853 if (! object
->internal
)
11854 disposition
|= VM_PAGE_QUERY_PAGE_EXTERNAL
;
11856 if (m
== VM_PAGE_NULL
)
11857 goto done_with_object
;
11859 if (m
->fictitious
) {
11860 disposition
|= VM_PAGE_QUERY_PAGE_FICTITIOUS
;
11861 goto done_with_object
;
11863 if (m
->dirty
|| pmap_is_modified(m
->phys_page
))
11864 disposition
|= VM_PAGE_QUERY_PAGE_DIRTY
;
11866 if (m
->reference
|| pmap_is_referenced(m
->phys_page
))
11867 disposition
|= VM_PAGE_QUERY_PAGE_REF
;
11869 if (m
->speculative
)
11870 disposition
|= VM_PAGE_QUERY_PAGE_SPECULATIVE
;
11872 if (m
->cs_validated
)
11873 disposition
|= VM_PAGE_QUERY_PAGE_CS_VALIDATED
;
11875 disposition
|= VM_PAGE_QUERY_PAGE_CS_TAINTED
;
11878 vm_object_unlock(object
);
11882 case VM_PAGE_INFO_BASIC
:
11883 basic_info
= (vm_page_info_basic_t
) info
;
11884 basic_info
->disposition
= disposition
;
11885 basic_info
->ref_count
= ref_count
;
11886 basic_info
->object_id
= (vm_object_id_t
) (uintptr_t) object
;
11887 basic_info
->offset
= (memory_object_offset_t
) offset
;
11888 basic_info
->depth
= depth
;
11898 * Synchronises the memory range specified with its backing store
11899 * image by either flushing or cleaning the contents to the appropriate
11900 * memory manager engaging in a memory object synchronize dialog with
11901 * the manager. The client doesn't return until the manager issues
11902 * m_o_s_completed message. MIG Magically converts user task parameter
11903 * to the task's address map.
11905 * interpretation of sync_flags
11906 * VM_SYNC_INVALIDATE - discard pages, only return precious
11907 * pages to manager.
11909 * VM_SYNC_INVALIDATE & (VM_SYNC_SYNCHRONOUS | VM_SYNC_ASYNCHRONOUS)
11910 * - discard pages, write dirty or precious
11911 * pages back to memory manager.
11913 * VM_SYNC_SYNCHRONOUS | VM_SYNC_ASYNCHRONOUS
11914 * - write dirty or precious pages back to
11915 * the memory manager.
11917 * VM_SYNC_CONTIGUOUS - does everything normally, but if there
11918 * is a hole in the region, and we would
11919 * have returned KERN_SUCCESS, return
11920 * KERN_INVALID_ADDRESS instead.
11923 * The memory object attributes have not yet been implemented, this
11924 * function will have to deal with the invalidate attribute
11927 * KERN_INVALID_TASK Bad task parameter
11928 * KERN_INVALID_ARGUMENT both sync and async were specified.
11929 * KERN_SUCCESS The usual.
11930 * KERN_INVALID_ADDRESS There was a hole in the region.
11936 vm_map_address_t address
,
11937 vm_map_size_t size
,
11938 vm_sync_t sync_flags
)
11941 msync_req_t new_msr
;
11942 queue_chain_t req_q
; /* queue of requests for this msync */
11943 vm_map_entry_t entry
;
11944 vm_map_size_t amount_left
;
11945 vm_object_offset_t offset
;
11946 boolean_t do_sync_req
;
11947 boolean_t had_hole
= FALSE
;
11948 memory_object_t pager
;
11950 if ((sync_flags
& VM_SYNC_ASYNCHRONOUS
) &&
11951 (sync_flags
& VM_SYNC_SYNCHRONOUS
))
11952 return(KERN_INVALID_ARGUMENT
);
11955 * align address and size on page boundaries
11957 size
= vm_map_round_page(address
+ size
) - vm_map_trunc_page(address
);
11958 address
= vm_map_trunc_page(address
);
11960 if (map
== VM_MAP_NULL
)
11961 return(KERN_INVALID_TASK
);
11964 return(KERN_SUCCESS
);
11966 queue_init(&req_q
);
11967 amount_left
= size
;
11969 while (amount_left
> 0) {
11970 vm_object_size_t flush_size
;
11971 vm_object_t object
;
11974 if (!vm_map_lookup_entry(map
,
11975 vm_map_trunc_page(address
), &entry
)) {
11977 vm_map_size_t skip
;
11980 * hole in the address map.
11985 * Check for empty map.
11987 if (entry
== vm_map_to_entry(map
) &&
11988 entry
->vme_next
== entry
) {
11989 vm_map_unlock(map
);
11993 * Check that we don't wrap and that
11994 * we have at least one real map entry.
11996 if ((map
->hdr
.nentries
== 0) ||
11997 (entry
->vme_next
->vme_start
< address
)) {
11998 vm_map_unlock(map
);
12002 * Move up to the next entry if needed
12004 skip
= (entry
->vme_next
->vme_start
- address
);
12005 if (skip
>= amount_left
)
12008 amount_left
-= skip
;
12009 address
= entry
->vme_next
->vme_start
;
12010 vm_map_unlock(map
);
12014 offset
= address
- entry
->vme_start
;
12017 * do we have more to flush than is contained in this
12020 if (amount_left
+ entry
->vme_start
+ offset
> entry
->vme_end
) {
12021 flush_size
= entry
->vme_end
-
12022 (entry
->vme_start
+ offset
);
12024 flush_size
= amount_left
;
12026 amount_left
-= flush_size
;
12027 address
+= flush_size
;
12029 if (entry
->is_sub_map
== TRUE
) {
12030 vm_map_t local_map
;
12031 vm_map_offset_t local_offset
;
12033 local_map
= entry
->object
.sub_map
;
12034 local_offset
= entry
->offset
;
12035 vm_map_unlock(map
);
12040 sync_flags
) == KERN_INVALID_ADDRESS
) {
12045 object
= entry
->object
.vm_object
;
12048 * We can't sync this object if the object has not been
12051 if (object
== VM_OBJECT_NULL
) {
12052 vm_map_unlock(map
);
12055 offset
+= entry
->offset
;
12057 vm_object_lock(object
);
12059 if (sync_flags
& (VM_SYNC_KILLPAGES
| VM_SYNC_DEACTIVATE
)) {
12060 int kill_pages
= 0;
12061 boolean_t reusable_pages
= FALSE
;
12063 if (sync_flags
& VM_SYNC_KILLPAGES
) {
12064 if (object
->ref_count
== 1 && !object
->shadow
)
12069 if (kill_pages
!= -1)
12070 vm_object_deactivate_pages(object
, offset
,
12071 (vm_object_size_t
)flush_size
, kill_pages
, reusable_pages
);
12072 vm_object_unlock(object
);
12073 vm_map_unlock(map
);
12077 * We can't sync this object if there isn't a pager.
12078 * Don't bother to sync internal objects, since there can't
12079 * be any "permanent" storage for these objects anyway.
12081 if ((object
->pager
== MEMORY_OBJECT_NULL
) ||
12082 (object
->internal
) || (object
->private)) {
12083 vm_object_unlock(object
);
12084 vm_map_unlock(map
);
12088 * keep reference on the object until syncing is done
12090 vm_object_reference_locked(object
);
12091 vm_object_unlock(object
);
12093 vm_map_unlock(map
);
12095 do_sync_req
= vm_object_sync(object
,
12098 sync_flags
& VM_SYNC_INVALIDATE
,
12099 ((sync_flags
& VM_SYNC_SYNCHRONOUS
) ||
12100 (sync_flags
& VM_SYNC_ASYNCHRONOUS
)),
12101 sync_flags
& VM_SYNC_SYNCHRONOUS
);
12103 * only send a m_o_s if we returned pages or if the entry
12104 * is writable (ie dirty pages may have already been sent back)
12106 if (!do_sync_req
) {
12107 if ((sync_flags
& VM_SYNC_INVALIDATE
) && object
->resident_page_count
== 0) {
12109 * clear out the clustering and read-ahead hints
12111 vm_object_lock(object
);
12113 object
->pages_created
= 0;
12114 object
->pages_used
= 0;
12115 object
->sequential
= 0;
12116 object
->last_alloc
= 0;
12118 vm_object_unlock(object
);
12120 vm_object_deallocate(object
);
12123 msync_req_alloc(new_msr
);
12125 vm_object_lock(object
);
12126 offset
+= object
->paging_offset
;
12128 new_msr
->offset
= offset
;
12129 new_msr
->length
= flush_size
;
12130 new_msr
->object
= object
;
12131 new_msr
->flag
= VM_MSYNC_SYNCHRONIZING
;
12135 * We can't sync this object if there isn't a pager. The
12136 * pager can disappear anytime we're not holding the object
12137 * lock. So this has to be checked anytime we goto re_iterate.
12140 pager
= object
->pager
;
12142 if (pager
== MEMORY_OBJECT_NULL
) {
12143 vm_object_unlock(object
);
12144 vm_object_deallocate(object
);
12148 queue_iterate(&object
->msr_q
, msr
, msync_req_t
, msr_q
) {
12150 * need to check for overlapping entry, if found, wait
12151 * on overlapping msr to be done, then reiterate
12154 if (msr
->flag
== VM_MSYNC_SYNCHRONIZING
&&
12155 ((offset
>= msr
->offset
&&
12156 offset
< (msr
->offset
+ msr
->length
)) ||
12157 (msr
->offset
>= offset
&&
12158 msr
->offset
< (offset
+ flush_size
))))
12160 assert_wait((event_t
) msr
,THREAD_INTERRUPTIBLE
);
12162 vm_object_unlock(object
);
12163 thread_block(THREAD_CONTINUE_NULL
);
12164 vm_object_lock(object
);
12168 }/* queue_iterate */
12170 queue_enter(&object
->msr_q
, new_msr
, msync_req_t
, msr_q
);
12172 vm_object_paging_begin(object
);
12173 vm_object_unlock(object
);
12175 queue_enter(&req_q
, new_msr
, msync_req_t
, req_q
);
12177 (void) memory_object_synchronize(
12181 sync_flags
& ~VM_SYNC_CONTIGUOUS
);
12183 vm_object_lock(object
);
12184 vm_object_paging_end(object
);
12185 vm_object_unlock(object
);
12189 * wait for memory_object_sychronize_completed messages from pager(s)
12192 while (!queue_empty(&req_q
)) {
12193 msr
= (msync_req_t
)queue_first(&req_q
);
12195 while(msr
->flag
!= VM_MSYNC_DONE
) {
12196 assert_wait((event_t
) msr
, THREAD_INTERRUPTIBLE
);
12198 thread_block(THREAD_CONTINUE_NULL
);
12201 queue_remove(&req_q
, msr
, msync_req_t
, req_q
);
12203 vm_object_deallocate(msr
->object
);
12204 msync_req_free(msr
);
12205 }/* queue_iterate */
12207 /* for proper msync() behaviour */
12208 if (had_hole
== TRUE
&& (sync_flags
& VM_SYNC_CONTIGUOUS
))
12209 return(KERN_INVALID_ADDRESS
);
12211 return(KERN_SUCCESS
);
12215 * Routine: convert_port_entry_to_map
12217 * Convert from a port specifying an entry or a task
12218 * to a map. Doesn't consume the port ref; produces a map ref,
12219 * which may be null. Unlike convert_port_to_map, the
12220 * port may be task or a named entry backed.
12227 convert_port_entry_to_map(
12231 vm_named_entry_t named_entry
;
12232 uint32_t try_failed_count
= 0;
12234 if(IP_VALID(port
) && (ip_kotype(port
) == IKOT_NAMED_ENTRY
)) {
12237 if(ip_active(port
) && (ip_kotype(port
)
12238 == IKOT_NAMED_ENTRY
)) {
12240 (vm_named_entry_t
)port
->ip_kobject
;
12241 if (!(lck_mtx_try_lock(&(named_entry
)->Lock
))) {
12244 try_failed_count
++;
12245 mutex_pause(try_failed_count
);
12248 named_entry
->ref_count
++;
12249 lck_mtx_unlock(&(named_entry
)->Lock
);
12251 if ((named_entry
->is_sub_map
) &&
12252 (named_entry
->protection
12253 & VM_PROT_WRITE
)) {
12254 map
= named_entry
->backing
.map
;
12256 mach_destroy_memory_entry(port
);
12257 return VM_MAP_NULL
;
12259 vm_map_reference_swap(map
);
12260 mach_destroy_memory_entry(port
);
12264 return VM_MAP_NULL
;
12268 map
= convert_port_to_map(port
);
12274 * Routine: convert_port_entry_to_object
12276 * Convert from a port specifying a named entry to an
12277 * object. Doesn't consume the port ref; produces a map ref,
12278 * which may be null.
12285 convert_port_entry_to_object(
12288 vm_object_t object
;
12289 vm_named_entry_t named_entry
;
12290 uint32_t try_failed_count
= 0;
12292 if(IP_VALID(port
) && (ip_kotype(port
) == IKOT_NAMED_ENTRY
)) {
12295 if(ip_active(port
) && (ip_kotype(port
)
12296 == IKOT_NAMED_ENTRY
)) {
12298 (vm_named_entry_t
)port
->ip_kobject
;
12299 if (!(lck_mtx_try_lock(&(named_entry
)->Lock
))) {
12302 try_failed_count
++;
12303 mutex_pause(try_failed_count
);
12306 named_entry
->ref_count
++;
12307 lck_mtx_unlock(&(named_entry
)->Lock
);
12309 if ((!named_entry
->is_sub_map
) &&
12310 (!named_entry
->is_pager
) &&
12311 (named_entry
->protection
12312 & VM_PROT_WRITE
)) {
12313 object
= named_entry
->backing
.object
;
12315 mach_destroy_memory_entry(port
);
12316 return (vm_object_t
)NULL
;
12318 vm_object_reference(named_entry
->backing
.object
);
12319 mach_destroy_memory_entry(port
);
12323 return (vm_object_t
)NULL
;
12326 return (vm_object_t
)NULL
;
12333 * Export routines to other components for the things we access locally through
12340 return (current_map_fast());
12344 * vm_map_reference:
12346 * Most code internal to the osfmk will go through a
12347 * macro defining this. This is always here for the
12348 * use of other kernel components.
12350 #undef vm_map_reference
12353 register vm_map_t map
)
12355 if (map
== VM_MAP_NULL
)
12358 lck_mtx_lock(&map
->s_lock
);
12360 assert(map
->res_count
> 0);
12361 assert(map
->ref_count
>= map
->res_count
);
12365 lck_mtx_unlock(&map
->s_lock
);
12369 * vm_map_deallocate:
12371 * Removes a reference from the specified map,
12372 * destroying it if no references remain.
12373 * The map should not be locked.
12377 register vm_map_t map
)
12381 if (map
== VM_MAP_NULL
)
12384 lck_mtx_lock(&map
->s_lock
);
12385 ref
= --map
->ref_count
;
12387 vm_map_res_deallocate(map
);
12388 lck_mtx_unlock(&map
->s_lock
);
12391 assert(map
->ref_count
== 0);
12392 lck_mtx_unlock(&map
->s_lock
);
12396 * The map residence count isn't decremented here because
12397 * the vm_map_delete below will traverse the entire map,
12398 * deleting entries, and the residence counts on objects
12399 * and sharing maps will go away then.
12403 vm_map_destroy(map
, VM_MAP_NO_FLAGS
);
12408 vm_map_disable_NX(vm_map_t map
)
12412 if (map
->pmap
== NULL
)
12415 pmap_disable_NX(map
->pmap
);
12418 /* XXX Consider making these constants (VM_MAX_ADDRESS and MACH_VM_MAX_ADDRESS)
12419 * more descriptive.
12422 vm_map_set_32bit(vm_map_t map
)
12424 map
->max_offset
= (vm_map_offset_t
)VM_MAX_ADDRESS
;
12429 vm_map_set_64bit(vm_map_t map
)
12431 map
->max_offset
= (vm_map_offset_t
)MACH_VM_MAX_ADDRESS
;
12435 vm_compute_max_offset(unsigned is64
)
12437 return (is64
? (vm_map_offset_t
)MACH_VM_MAX_ADDRESS
: (vm_map_offset_t
)VM_MAX_ADDRESS
);
12444 return map
->max_offset
> ((vm_map_offset_t
)VM_MAX_ADDRESS
);
12448 vm_map_has_4GB_pagezero(
12453 * We should lock the VM map (for read) here but we can get away
12454 * with it for now because there can't really be any race condition:
12455 * the VM map's min_offset is changed only when the VM map is created
12456 * and when the zero page is established (when the binary gets loaded),
12457 * and this routine gets called only when the task terminates and the
12458 * VM map is being torn down, and when a new map is created via
12459 * load_machfile()/execve().
12461 return (map
->min_offset
>= 0x100000000ULL
);
12465 vm_map_set_4GB_pagezero(vm_map_t map
)
12468 pmap_set_4GB_pagezero(map
->pmap
);
12470 #pragma unused(map)
12476 vm_map_clear_4GB_pagezero(vm_map_t map
)
12479 pmap_clear_4GB_pagezero(map
->pmap
);
12481 #pragma unused(map)
12486 * Raise a VM map's minimum offset.
12487 * To strictly enforce "page zero" reservation.
12490 vm_map_raise_min_offset(
12492 vm_map_offset_t new_min_offset
)
12494 vm_map_entry_t first_entry
;
12496 new_min_offset
= vm_map_round_page(new_min_offset
);
12500 if (new_min_offset
< map
->min_offset
) {
12502 * Can't move min_offset backwards, as that would expose
12503 * a part of the address space that was previously, and for
12504 * possibly good reasons, inaccessible.
12506 vm_map_unlock(map
);
12507 return KERN_INVALID_ADDRESS
;
12510 first_entry
= vm_map_first_entry(map
);
12511 if (first_entry
!= vm_map_to_entry(map
) &&
12512 first_entry
->vme_start
< new_min_offset
) {
12514 * Some memory was already allocated below the new
12515 * minimun offset. It's too late to change it now...
12517 vm_map_unlock(map
);
12518 return KERN_NO_SPACE
;
12521 map
->min_offset
= new_min_offset
;
12523 vm_map_unlock(map
);
12525 return KERN_SUCCESS
;
12529 * Set the limit on the maximum amount of user wired memory allowed for this map.
12530 * This is basically a copy of the MEMLOCK rlimit value maintained by the BSD side of
12531 * the kernel. The limits are checked in the mach VM side, so we keep a copy so we
12532 * don't have to reach over to the BSD data structures.
12536 vm_map_set_user_wire_limit(vm_map_t map
,
12539 map
->user_wire_limit
= limit
;
12543 void vm_map_switch_protect(vm_map_t map
,
12547 map
->switch_protect
=val
;
12548 vm_map_unlock(map
);