2 * Copyright (c) 2000-2012 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>
69 #include <vm/vm_options.h>
71 #include <libkern/OSAtomic.h>
73 #include <mach/kern_return.h>
74 #include <mach/port.h>
75 #include <mach/vm_attributes.h>
76 #include <mach/vm_param.h>
77 #include <mach/vm_behavior.h>
78 #include <mach/vm_statistics.h>
79 #include <mach/memory_object.h>
80 #include <mach/mach_vm.h>
81 #include <machine/cpu_capabilities.h>
84 #include <kern/assert.h>
85 #include <kern/counters.h>
86 #include <kern/kalloc.h>
87 #include <kern/zalloc.h>
90 #include <vm/vm_compressor_pager.h>
91 #include <vm/vm_init.h>
92 #include <vm/vm_fault.h>
93 #include <vm/vm_map.h>
94 #include <vm/vm_object.h>
95 #include <vm/vm_page.h>
96 #include <vm/vm_pageout.h>
97 #include <vm/vm_kern.h>
98 #include <ipc/ipc_port.h>
99 #include <kern/sched_prim.h>
100 #include <kern/misc_protos.h>
101 #include <kern/xpr.h>
103 #include <mach/vm_map_server.h>
104 #include <mach/mach_host_server.h>
105 #include <vm/vm_protos.h>
106 #include <vm/vm_purgeable_internal.h>
108 #include <vm/vm_protos.h>
109 #include <vm/vm_shared_region.h>
110 #include <vm/vm_map_store.h>
113 extern u_int32_t
random(void); /* from <libkern/libkern.h> */
114 /* Internal prototypes
117 static void vm_map_simplify_range(
119 vm_map_offset_t start
,
120 vm_map_offset_t end
); /* forward */
122 static boolean_t
vm_map_range_check(
124 vm_map_offset_t start
,
126 vm_map_entry_t
*entry
);
128 static vm_map_entry_t
_vm_map_entry_create(
129 struct vm_map_header
*map_header
, boolean_t map_locked
);
131 static void _vm_map_entry_dispose(
132 struct vm_map_header
*map_header
,
133 vm_map_entry_t entry
);
135 static void vm_map_pmap_enter(
137 vm_map_offset_t addr
,
138 vm_map_offset_t end_addr
,
140 vm_object_offset_t offset
,
141 vm_prot_t protection
);
143 static void _vm_map_clip_end(
144 struct vm_map_header
*map_header
,
145 vm_map_entry_t entry
,
146 vm_map_offset_t end
);
148 static void _vm_map_clip_start(
149 struct vm_map_header
*map_header
,
150 vm_map_entry_t entry
,
151 vm_map_offset_t start
);
153 static void vm_map_entry_delete(
155 vm_map_entry_t entry
);
157 static kern_return_t
vm_map_delete(
159 vm_map_offset_t start
,
164 static kern_return_t
vm_map_copy_overwrite_unaligned(
166 vm_map_entry_t entry
,
168 vm_map_address_t start
,
169 boolean_t discard_on_success
);
171 static kern_return_t
vm_map_copy_overwrite_aligned(
173 vm_map_entry_t tmp_entry
,
175 vm_map_offset_t start
,
178 static kern_return_t
vm_map_copyin_kernel_buffer(
180 vm_map_address_t src_addr
,
182 boolean_t src_destroy
,
183 vm_map_copy_t
*copy_result
); /* OUT */
185 static kern_return_t
vm_map_copyout_kernel_buffer(
187 vm_map_address_t
*addr
, /* IN/OUT */
190 boolean_t consume_on_success
);
192 static void vm_map_fork_share(
194 vm_map_entry_t old_entry
,
197 static boolean_t
vm_map_fork_copy(
199 vm_map_entry_t
*old_entry_p
,
202 void vm_map_region_top_walk(
203 vm_map_entry_t entry
,
204 vm_region_top_info_t top
);
206 void vm_map_region_walk(
209 vm_map_entry_t entry
,
210 vm_object_offset_t offset
,
211 vm_object_size_t range
,
212 vm_region_extended_info_t extended
,
213 boolean_t look_for_pages
,
214 mach_msg_type_number_t count
);
216 static kern_return_t
vm_map_wire_nested(
218 vm_map_offset_t start
,
220 vm_prot_t caller_prot
,
223 vm_map_offset_t pmap_addr
,
224 ppnum_t
*physpage_p
);
226 static kern_return_t
vm_map_unwire_nested(
228 vm_map_offset_t start
,
232 vm_map_offset_t pmap_addr
);
234 static kern_return_t
vm_map_overwrite_submap_recurse(
236 vm_map_offset_t dst_addr
,
237 vm_map_size_t dst_size
);
239 static kern_return_t
vm_map_copy_overwrite_nested(
241 vm_map_offset_t dst_addr
,
243 boolean_t interruptible
,
245 boolean_t discard_on_success
);
247 static kern_return_t
vm_map_remap_extract(
249 vm_map_offset_t addr
,
252 struct vm_map_header
*map_header
,
253 vm_prot_t
*cur_protection
,
254 vm_prot_t
*max_protection
,
255 vm_inherit_t inheritance
,
258 static kern_return_t
vm_map_remap_range_allocate(
260 vm_map_address_t
*address
,
262 vm_map_offset_t mask
,
264 vm_map_entry_t
*map_entry
);
266 static void vm_map_region_look_for_page(
270 vm_object_offset_t offset
,
273 vm_region_extended_info_t extended
,
274 mach_msg_type_number_t count
);
276 static int vm_map_region_count_obj_refs(
277 vm_map_entry_t entry
,
281 static kern_return_t
vm_map_willneed(
283 vm_map_offset_t start
,
284 vm_map_offset_t end
);
286 static kern_return_t
vm_map_reuse_pages(
288 vm_map_offset_t start
,
289 vm_map_offset_t end
);
291 static kern_return_t
vm_map_reusable_pages(
293 vm_map_offset_t start
,
294 vm_map_offset_t end
);
296 static kern_return_t
vm_map_can_reuse(
298 vm_map_offset_t start
,
299 vm_map_offset_t end
);
302 static kern_return_t
vm_map_pageout(
304 vm_map_offset_t start
,
305 vm_map_offset_t end
);
306 #endif /* MACH_ASSERT */
309 * Macros to copy a vm_map_entry. We must be careful to correctly
310 * manage the wired page count. vm_map_entry_copy() creates a new
311 * map entry to the same memory - the wired count in the new entry
312 * must be set to zero. vm_map_entry_copy_full() creates a new
313 * entry that is identical to the old entry. This preserves the
314 * wire count; it's used for map splitting and zone changing in
318 #define vm_map_entry_copy(NEW,OLD) \
320 boolean_t _vmec_reserved = (NEW)->from_reserved_zone; \
322 (NEW)->is_shared = FALSE; \
323 (NEW)->needs_wakeup = FALSE; \
324 (NEW)->in_transition = FALSE; \
325 (NEW)->wired_count = 0; \
326 (NEW)->user_wired_count = 0; \
327 (NEW)->permanent = FALSE; \
328 (NEW)->used_for_jit = FALSE; \
329 (NEW)->from_reserved_zone = _vmec_reserved; \
330 (NEW)->iokit_acct = FALSE; \
331 (NEW)->vme_resilient_codesign = FALSE; \
332 (NEW)->vme_resilient_media = FALSE; \
335 #define vm_map_entry_copy_full(NEW,OLD) \
337 boolean_t _vmecf_reserved = (NEW)->from_reserved_zone; \
339 (NEW)->from_reserved_zone = _vmecf_reserved; \
343 * Decide if we want to allow processes to execute from their data or stack areas.
344 * override_nx() returns true if we do. Data/stack execution can be enabled independently
345 * for 32 and 64 bit processes. Set the VM_ABI_32 or VM_ABI_64 flags in allow_data_exec
346 * or allow_stack_exec to enable data execution for that type of data area for that particular
347 * ABI (or both by or'ing the flags together). These are initialized in the architecture
348 * specific pmap files since the default behavior varies according to architecture. The
349 * main reason it varies is because of the need to provide binary compatibility with old
350 * applications that were written before these restrictions came into being. In the old
351 * days, an app could execute anything it could read, but this has slowly been tightened
352 * up over time. The default behavior is:
354 * 32-bit PPC apps may execute from both stack and data areas
355 * 32-bit Intel apps may exeucte from data areas but not stack
356 * 64-bit PPC/Intel apps may not execute from either data or stack
358 * An application on any architecture may override these defaults by explicitly
359 * adding PROT_EXEC permission to the page in question with the mprotect(2)
360 * system call. This code here just determines what happens when an app tries to
361 * execute from a page that lacks execute permission.
363 * Note that allow_data_exec or allow_stack_exec may also be modified by sysctl to change the
364 * default behavior for both 32 and 64 bit apps on a system-wide basis. Furthermore,
365 * a Mach-O header flag bit (MH_NO_HEAP_EXECUTION) can be used to forcibly disallow
366 * execution from data areas for a particular binary even if the arch normally permits it. As
367 * a final wrinkle, a posix_spawn attribute flag can be used to negate this opt-in header bit
368 * to support some complicated use cases, notably browsers with out-of-process plugins that
369 * are not all NX-safe.
372 extern int allow_data_exec
, allow_stack_exec
;
375 override_nx(vm_map_t map
, uint32_t user_tag
) /* map unused on arm */
379 if (map
->pmap
== kernel_pmap
) return FALSE
;
382 * Determine if the app is running in 32 or 64 bit mode.
385 if (vm_map_is_64bit(map
))
386 current_abi
= VM_ABI_64
;
388 current_abi
= VM_ABI_32
;
391 * Determine if we should allow the execution based on whether it's a
392 * stack or data area and the current architecture.
395 if (user_tag
== VM_MEMORY_STACK
)
396 return allow_stack_exec
& current_abi
;
398 return (allow_data_exec
& current_abi
) && (map
->map_disallow_data_exec
== FALSE
);
403 * Virtual memory maps provide for the mapping, protection,
404 * and sharing of virtual memory objects. In addition,
405 * this module provides for an efficient virtual copy of
406 * memory from one map to another.
408 * Synchronization is required prior to most operations.
410 * Maps consist of an ordered doubly-linked list of simple
411 * entries; a single hint is used to speed up lookups.
413 * Sharing maps have been deleted from this version of Mach.
414 * All shared objects are now mapped directly into the respective
415 * maps. This requires a change in the copy on write strategy;
416 * the asymmetric (delayed) strategy is used for shared temporary
417 * objects instead of the symmetric (shadow) strategy. All maps
418 * are now "top level" maps (either task map, kernel map or submap
419 * of the kernel map).
421 * Since portions of maps are specified by start/end addreses,
422 * which may not align with existing map entries, all
423 * routines merely "clip" entries to these start/end values.
424 * [That is, an entry is split into two, bordering at a
425 * start or end value.] Note that these clippings may not
426 * always be necessary (as the two resulting entries are then
427 * not changed); however, the clipping is done for convenience.
428 * No attempt is currently made to "glue back together" two
431 * The symmetric (shadow) copy strategy implements virtual copy
432 * by copying VM object references from one map to
433 * another, and then marking both regions as copy-on-write.
434 * It is important to note that only one writeable reference
435 * to a VM object region exists in any map when this strategy
436 * is used -- this means that shadow object creation can be
437 * delayed until a write operation occurs. The symmetric (delayed)
438 * strategy allows multiple maps to have writeable references to
439 * the same region of a vm object, and hence cannot delay creating
440 * its copy objects. See vm_object_copy_quickly() in vm_object.c.
441 * Copying of permanent objects is completely different; see
442 * vm_object_copy_strategically() in vm_object.c.
445 static zone_t vm_map_zone
; /* zone for vm_map structures */
446 static zone_t vm_map_entry_zone
; /* zone for vm_map_entry structures */
447 static zone_t vm_map_entry_reserved_zone
; /* zone with reserve for non-blocking
449 static zone_t vm_map_copy_zone
; /* zone for vm_map_copy structures */
450 zone_t vm_map_holes_zone
; /* zone for vm map holes (vm_map_links) structures */
454 * Placeholder object for submap operations. This object is dropped
455 * into the range by a call to vm_map_find, and removed when
456 * vm_map_submap creates the submap.
459 vm_object_t vm_submap_object
;
461 static void *map_data
;
462 static vm_size_t map_data_size
;
463 static void *kentry_data
;
464 static vm_size_t kentry_data_size
;
465 static void *map_holes_data
;
466 static vm_size_t map_holes_data_size
;
468 #define NO_COALESCE_LIMIT ((1024 * 128) - 1)
470 /* Skip acquiring locks if we're in the midst of a kernel core dump */
471 unsigned int not_in_kdp
= 1;
473 unsigned int vm_map_set_cache_attr_count
= 0;
476 vm_map_set_cache_attr(
480 vm_map_entry_t map_entry
;
482 kern_return_t kr
= KERN_SUCCESS
;
484 vm_map_lock_read(map
);
486 if (!vm_map_lookup_entry(map
, va
, &map_entry
) ||
487 map_entry
->is_sub_map
) {
489 * that memory is not properly mapped
491 kr
= KERN_INVALID_ARGUMENT
;
494 object
= VME_OBJECT(map_entry
);
496 if (object
== VM_OBJECT_NULL
) {
498 * there should be a VM object here at this point
500 kr
= KERN_INVALID_ARGUMENT
;
503 vm_object_lock(object
);
504 object
->set_cache_attr
= TRUE
;
505 vm_object_unlock(object
);
507 vm_map_set_cache_attr_count
++;
509 vm_map_unlock_read(map
);
515 #if CONFIG_CODE_DECRYPTION
517 * vm_map_apple_protected:
518 * This remaps the requested part of the object with an object backed by
519 * the decrypting pager.
520 * crypt_info contains entry points and session data for the crypt module.
521 * The crypt_info block will be copied by vm_map_apple_protected. The data structures
522 * referenced in crypt_info must remain valid until crypt_info->crypt_end() is called.
525 vm_map_apple_protected(
527 vm_map_offset_t start
,
529 vm_object_offset_t crypto_backing_offset
,
530 struct pager_crypt_info
*crypt_info
)
532 boolean_t map_locked
;
534 vm_map_entry_t map_entry
;
535 struct vm_map_entry tmp_entry
;
536 memory_object_t unprotected_mem_obj
;
537 vm_object_t protected_object
;
538 vm_map_offset_t map_addr
;
539 vm_map_offset_t start_aligned
, end_aligned
;
540 vm_object_offset_t crypto_start
, crypto_end
;
544 unprotected_mem_obj
= MEMORY_OBJECT_NULL
;
546 start_aligned
= vm_map_trunc_page(start
, PAGE_MASK_64
);
547 end_aligned
= vm_map_round_page(end
, PAGE_MASK_64
);
548 start_aligned
= vm_map_trunc_page(start_aligned
, VM_MAP_PAGE_MASK(map
));
549 end_aligned
= vm_map_round_page(end_aligned
, VM_MAP_PAGE_MASK(map
));
551 assert(start_aligned
== start
);
552 assert(end_aligned
== end
);
554 map_addr
= start_aligned
;
555 for (map_addr
= start_aligned
;
557 map_addr
= tmp_entry
.vme_end
) {
561 /* lookup the protected VM object */
562 if (!vm_map_lookup_entry(map
,
565 map_entry
->is_sub_map
||
566 VME_OBJECT(map_entry
) == VM_OBJECT_NULL
||
567 !(map_entry
->protection
& VM_PROT_EXECUTE
)) {
568 /* that memory is not properly mapped */
569 kr
= KERN_INVALID_ARGUMENT
;
573 /* get the protected object to be decrypted */
574 protected_object
= VME_OBJECT(map_entry
);
575 if (protected_object
== VM_OBJECT_NULL
) {
576 /* there should be a VM object here at this point */
577 kr
= KERN_INVALID_ARGUMENT
;
580 /* ensure protected object stays alive while map is unlocked */
581 vm_object_reference(protected_object
);
583 /* limit the map entry to the area we want to cover */
584 vm_map_clip_start(map
, map_entry
, start_aligned
);
585 vm_map_clip_end(map
, map_entry
, end_aligned
);
587 tmp_entry
= *map_entry
;
588 map_entry
= VM_MAP_ENTRY_NULL
; /* not valid after unlocking map */
593 * This map entry might be only partially encrypted
594 * (if not fully "page-aligned").
597 crypto_end
= tmp_entry
.vme_end
- tmp_entry
.vme_start
;
598 if (tmp_entry
.vme_start
< start
) {
599 if (tmp_entry
.vme_start
!= start_aligned
) {
600 kr
= KERN_INVALID_ADDRESS
;
602 crypto_start
+= (start
- tmp_entry
.vme_start
);
604 if (tmp_entry
.vme_end
> end
) {
605 if (tmp_entry
.vme_end
!= end_aligned
) {
606 kr
= KERN_INVALID_ADDRESS
;
608 crypto_end
-= (tmp_entry
.vme_end
- end
);
612 * This "extra backing offset" is needed to get the decryption
613 * routine to use the right key. It adjusts for the possibly
614 * relative offset of an interposed "4K" pager...
616 if (crypto_backing_offset
== (vm_object_offset_t
) -1) {
617 crypto_backing_offset
= VME_OFFSET(&tmp_entry
);
621 * Lookup (and create if necessary) the protected memory object
622 * matching that VM object.
623 * If successful, this also grabs a reference on the memory object,
624 * to guarantee that it doesn't go away before we get a chance to map
627 unprotected_mem_obj
= apple_protect_pager_setup(
629 VME_OFFSET(&tmp_entry
),
630 crypto_backing_offset
,
635 /* release extra ref on protected object */
636 vm_object_deallocate(protected_object
);
638 if (unprotected_mem_obj
== NULL
) {
643 vm_flags
= VM_FLAGS_FIXED
| VM_FLAGS_OVERWRITE
;
645 /* map this memory object in place of the current one */
646 map_addr
= tmp_entry
.vme_start
;
647 kr
= vm_map_enter_mem_object(map
,
650 tmp_entry
.vme_start
),
651 (mach_vm_offset_t
) 0,
653 (ipc_port_t
) unprotected_mem_obj
,
656 tmp_entry
.protection
,
657 tmp_entry
.max_protection
,
658 tmp_entry
.inheritance
);
659 assert(kr
== KERN_SUCCESS
);
660 assert(map_addr
== tmp_entry
.vme_start
);
662 #if VM_MAP_DEBUG_APPLE_PROTECT
663 printf("APPLE_PROTECT: map %p [0x%llx:0x%llx] pager %p: "
664 "backing:[object:%p,offset:0x%llx,"
665 "crypto_backing_offset:0x%llx,"
666 "crypto_start:0x%llx,crypto_end:0x%llx]\n",
669 (uint64_t) (map_addr
+ (tmp_entry
.vme_end
-
670 tmp_entry
.vme_start
)),
673 VME_OFFSET(&tmp_entry
),
674 crypto_backing_offset
,
677 #endif /* VM_MAP_DEBUG_APPLE_PROTECT */
680 * Release the reference obtained by
681 * apple_protect_pager_setup().
682 * The mapping (if it succeeded) is now holding a reference on
685 memory_object_deallocate(unprotected_mem_obj
);
686 unprotected_mem_obj
= MEMORY_OBJECT_NULL
;
688 /* continue with next map entry */
689 crypto_backing_offset
+= (tmp_entry
.vme_end
-
690 tmp_entry
.vme_start
);
691 crypto_backing_offset
-= crypto_start
;
701 #endif /* CONFIG_CODE_DECRYPTION */
704 lck_grp_t vm_map_lck_grp
;
705 lck_grp_attr_t vm_map_lck_grp_attr
;
706 lck_attr_t vm_map_lck_attr
;
707 lck_attr_t vm_map_lck_rw_attr
;
713 * Initialize the vm_map module. Must be called before
714 * any other vm_map routines.
716 * Map and entry structures are allocated from zones -- we must
717 * initialize those zones.
719 * There are three zones of interest:
721 * vm_map_zone: used to allocate maps.
722 * vm_map_entry_zone: used to allocate map entries.
723 * vm_map_entry_reserved_zone: fallback zone for kernel map entries
725 * The kernel allocates map entries from a special zone that is initially
726 * "crammed" with memory. It would be difficult (perhaps impossible) for
727 * the kernel to allocate more memory to a entry zone when it became
728 * empty since the very act of allocating memory implies the creation
735 vm_size_t entry_zone_alloc_size
;
736 const char *mez_name
= "VM map entries";
738 vm_map_zone
= zinit((vm_map_size_t
) sizeof(struct _vm_map
), 40*1024,
740 zone_change(vm_map_zone
, Z_NOENCRYPT
, TRUE
);
741 #if defined(__LP64__)
742 entry_zone_alloc_size
= PAGE_SIZE
* 5;
744 entry_zone_alloc_size
= PAGE_SIZE
* 6;
746 vm_map_entry_zone
= zinit((vm_map_size_t
) sizeof(struct vm_map_entry
),
747 1024*1024, entry_zone_alloc_size
,
749 zone_change(vm_map_entry_zone
, Z_NOENCRYPT
, TRUE
);
750 zone_change(vm_map_entry_zone
, Z_NOCALLOUT
, TRUE
);
751 zone_change(vm_map_entry_zone
, Z_GZALLOC_EXEMPT
, TRUE
);
753 vm_map_entry_reserved_zone
= zinit((vm_map_size_t
) sizeof(struct vm_map_entry
),
754 kentry_data_size
* 64, kentry_data_size
,
755 "Reserved VM map entries");
756 zone_change(vm_map_entry_reserved_zone
, Z_NOENCRYPT
, TRUE
);
758 vm_map_copy_zone
= zinit((vm_map_size_t
) sizeof(struct vm_map_copy
),
759 16*1024, PAGE_SIZE
, "VM map copies");
760 zone_change(vm_map_copy_zone
, Z_NOENCRYPT
, TRUE
);
762 vm_map_holes_zone
= zinit((vm_map_size_t
) sizeof(struct vm_map_links
),
763 16*1024, PAGE_SIZE
, "VM map holes");
764 zone_change(vm_map_holes_zone
, Z_NOENCRYPT
, TRUE
);
767 * Cram the map and kentry zones with initial data.
768 * Set reserved_zone non-collectible to aid zone_gc().
770 zone_change(vm_map_zone
, Z_COLLECT
, FALSE
);
772 zone_change(vm_map_entry_reserved_zone
, Z_COLLECT
, FALSE
);
773 zone_change(vm_map_entry_reserved_zone
, Z_EXPAND
, FALSE
);
774 zone_change(vm_map_entry_reserved_zone
, Z_FOREIGN
, TRUE
);
775 zone_change(vm_map_entry_reserved_zone
, Z_NOCALLOUT
, TRUE
);
776 zone_change(vm_map_entry_reserved_zone
, Z_CALLERACCT
, FALSE
); /* don't charge caller */
777 zone_change(vm_map_copy_zone
, Z_CALLERACCT
, FALSE
); /* don't charge caller */
778 zone_change(vm_map_entry_reserved_zone
, Z_GZALLOC_EXEMPT
, TRUE
);
780 zone_change(vm_map_holes_zone
, Z_COLLECT
, TRUE
);
781 zone_change(vm_map_holes_zone
, Z_EXPAND
, TRUE
);
782 zone_change(vm_map_holes_zone
, Z_FOREIGN
, TRUE
);
783 zone_change(vm_map_holes_zone
, Z_NOCALLOUT
, TRUE
);
784 zone_change(vm_map_holes_zone
, Z_CALLERACCT
, TRUE
);
785 zone_change(vm_map_holes_zone
, Z_GZALLOC_EXEMPT
, TRUE
);
788 * Add the stolen memory to zones, adjust zone size and stolen counts.
790 zcram(vm_map_zone
, (vm_offset_t
)map_data
, map_data_size
);
791 zcram(vm_map_entry_reserved_zone
, (vm_offset_t
)kentry_data
, kentry_data_size
);
792 zcram(vm_map_holes_zone
, (vm_offset_t
)map_holes_data
, map_holes_data_size
);
793 VM_PAGE_MOVE_STOLEN(atop_64(map_data_size
) + atop_64(kentry_data_size
) + atop_64(map_holes_data_size
));
795 lck_grp_attr_setdefault(&vm_map_lck_grp_attr
);
796 lck_grp_init(&vm_map_lck_grp
, "vm_map", &vm_map_lck_grp_attr
);
797 lck_attr_setdefault(&vm_map_lck_attr
);
799 lck_attr_setdefault(&vm_map_lck_rw_attr
);
800 lck_attr_cleardebug(&vm_map_lck_rw_attr
);
803 default_freezer_init();
804 #endif /* CONFIG_FREEZE */
811 uint32_t kentry_initial_pages
;
813 map_data_size
= round_page(10 * sizeof(struct _vm_map
));
814 map_data
= pmap_steal_memory(map_data_size
);
817 * kentry_initial_pages corresponds to the number of kernel map entries
818 * required during bootstrap until the asynchronous replenishment
819 * scheme is activated and/or entries are available from the general
822 #if defined(__LP64__)
823 kentry_initial_pages
= 10;
825 kentry_initial_pages
= 6;
829 /* If using the guard allocator, reserve more memory for the kernel
830 * reserved map entry pool.
832 if (gzalloc_enabled())
833 kentry_initial_pages
*= 1024;
836 kentry_data_size
= kentry_initial_pages
* PAGE_SIZE
;
837 kentry_data
= pmap_steal_memory(kentry_data_size
);
839 map_holes_data_size
= kentry_data_size
;
840 map_holes_data
= pmap_steal_memory(map_holes_data_size
);
844 vm_kernel_reserved_entry_init(void) {
845 zone_prio_refill_configure(vm_map_entry_reserved_zone
, (6*PAGE_SIZE
)/sizeof(struct vm_map_entry
));
846 zone_prio_refill_configure(vm_map_holes_zone
, (6*PAGE_SIZE
)/sizeof(struct vm_map_links
));
850 vm_map_disable_hole_optimization(vm_map_t map
)
852 vm_map_entry_t head_entry
, hole_entry
, next_hole_entry
;
854 if (map
->holelistenabled
) {
856 head_entry
= hole_entry
= (vm_map_entry_t
) map
->holes_list
;
858 while (hole_entry
!= NULL
) {
860 next_hole_entry
= hole_entry
->vme_next
;
862 hole_entry
->vme_next
= NULL
;
863 hole_entry
->vme_prev
= NULL
;
864 zfree(vm_map_holes_zone
, hole_entry
);
866 if (next_hole_entry
== head_entry
) {
869 hole_entry
= next_hole_entry
;
873 map
->holes_list
= NULL
;
874 map
->holelistenabled
= FALSE
;
876 map
->first_free
= vm_map_first_entry(map
);
877 SAVE_HINT_HOLE_WRITE(map
, NULL
);
882 vm_kernel_map_is_kernel(vm_map_t map
) {
883 return (map
->pmap
== kernel_pmap
);
889 * Creates and returns a new empty VM map with
890 * the given physical map structure, and having
891 * the given lower and upper address bounds.
894 boolean_t vm_map_supports_hole_optimization
= TRUE
;
903 static int color_seed
= 0;
904 register vm_map_t result
;
905 struct vm_map_links
*hole_entry
= NULL
;
907 result
= (vm_map_t
) zalloc(vm_map_zone
);
908 if (result
== VM_MAP_NULL
)
909 panic("vm_map_create");
911 vm_map_first_entry(result
) = vm_map_to_entry(result
);
912 vm_map_last_entry(result
) = vm_map_to_entry(result
);
913 result
->hdr
.nentries
= 0;
914 result
->hdr
.entries_pageable
= pageable
;
916 vm_map_store_init( &(result
->hdr
) );
918 result
->hdr
.page_shift
= PAGE_SHIFT
;
921 result
->user_wire_limit
= MACH_VM_MAX_ADDRESS
; /* default limit is unlimited */
922 result
->user_wire_size
= 0;
923 result
->ref_count
= 1;
925 result
->res_count
= 1;
926 result
->sw_state
= MAP_SW_IN
;
927 #endif /* TASK_SWAPPER */
929 result
->min_offset
= min
;
930 result
->max_offset
= max
;
931 result
->wiring_required
= FALSE
;
932 result
->no_zero_fill
= FALSE
;
933 result
->mapped_in_other_pmaps
= FALSE
;
934 result
->wait_for_space
= FALSE
;
935 result
->switch_protect
= FALSE
;
936 result
->disable_vmentry_reuse
= FALSE
;
937 result
->map_disallow_data_exec
= FALSE
;
938 result
->highest_entry_end
= 0;
939 result
->first_free
= vm_map_to_entry(result
);
940 result
->hint
= vm_map_to_entry(result
);
941 result
->color_rr
= (color_seed
++) & vm_color_mask
;
942 result
->jit_entry_exists
= FALSE
;
944 if (vm_map_supports_hole_optimization
&& pmap
!= kernel_pmap
) {
945 hole_entry
= zalloc(vm_map_holes_zone
);
947 hole_entry
->start
= min
;
948 hole_entry
->end
= (max
> (vm_map_offset_t
)MACH_VM_MAX_ADDRESS
) ? max
: (vm_map_offset_t
)MACH_VM_MAX_ADDRESS
;
949 result
->holes_list
= result
->hole_hint
= hole_entry
;
950 hole_entry
->prev
= hole_entry
->next
= (vm_map_entry_t
) hole_entry
;
951 result
->holelistenabled
= TRUE
;
955 result
->holelistenabled
= FALSE
;
959 result
->default_freezer_handle
= NULL
;
961 vm_map_lock_init(result
);
962 lck_mtx_init_ext(&result
->s_lock
, &result
->s_lock_ext
, &vm_map_lck_grp
, &vm_map_lck_attr
);
968 * vm_map_entry_create: [ internal use only ]
970 * Allocates a VM map entry for insertion in the
971 * given map (or map copy). No fields are filled.
973 #define vm_map_entry_create(map, map_locked) _vm_map_entry_create(&(map)->hdr, map_locked)
975 #define vm_map_copy_entry_create(copy, map_locked) \
976 _vm_map_entry_create(&(copy)->cpy_hdr, map_locked)
977 unsigned reserved_zalloc_count
, nonreserved_zalloc_count
;
979 static vm_map_entry_t
980 _vm_map_entry_create(
981 struct vm_map_header
*map_header
, boolean_t __unused map_locked
)
984 vm_map_entry_t entry
;
986 zone
= vm_map_entry_zone
;
988 assert(map_header
->entries_pageable
? !map_locked
: TRUE
);
990 if (map_header
->entries_pageable
) {
991 entry
= (vm_map_entry_t
) zalloc(zone
);
994 entry
= (vm_map_entry_t
) zalloc_canblock(zone
, FALSE
);
996 if (entry
== VM_MAP_ENTRY_NULL
) {
997 zone
= vm_map_entry_reserved_zone
;
998 entry
= (vm_map_entry_t
) zalloc(zone
);
999 OSAddAtomic(1, &reserved_zalloc_count
);
1001 OSAddAtomic(1, &nonreserved_zalloc_count
);
1004 if (entry
== VM_MAP_ENTRY_NULL
)
1005 panic("vm_map_entry_create");
1006 entry
->from_reserved_zone
= (zone
== vm_map_entry_reserved_zone
);
1008 vm_map_store_update( (vm_map_t
) NULL
, entry
, VM_MAP_ENTRY_CREATE
);
1009 #if MAP_ENTRY_CREATION_DEBUG
1010 entry
->vme_creation_maphdr
= map_header
;
1011 fastbacktrace(&entry
->vme_creation_bt
[0],
1012 (sizeof(entry
->vme_creation_bt
)/sizeof(uintptr_t)));
1018 * vm_map_entry_dispose: [ internal use only ]
1020 * Inverse of vm_map_entry_create.
1022 * write map lock held so no need to
1023 * do anything special to insure correctness
1026 #define vm_map_entry_dispose(map, entry) \
1027 _vm_map_entry_dispose(&(map)->hdr, (entry))
1029 #define vm_map_copy_entry_dispose(map, entry) \
1030 _vm_map_entry_dispose(&(copy)->cpy_hdr, (entry))
1033 _vm_map_entry_dispose(
1034 register struct vm_map_header
*map_header
,
1035 register vm_map_entry_t entry
)
1037 register zone_t zone
;
1039 if (map_header
->entries_pageable
|| !(entry
->from_reserved_zone
))
1040 zone
= vm_map_entry_zone
;
1042 zone
= vm_map_entry_reserved_zone
;
1044 if (!map_header
->entries_pageable
) {
1045 if (zone
== vm_map_entry_zone
)
1046 OSAddAtomic(-1, &nonreserved_zalloc_count
);
1048 OSAddAtomic(-1, &reserved_zalloc_count
);
1055 static boolean_t first_free_check
= FALSE
;
1057 first_free_is_valid(
1060 if (!first_free_check
)
1063 return( first_free_is_valid_store( map
));
1065 #endif /* MACH_ASSERT */
1068 #define vm_map_copy_entry_link(copy, after_where, entry) \
1069 _vm_map_store_entry_link(&(copy)->cpy_hdr, after_where, (entry))
1071 #define vm_map_copy_entry_unlink(copy, entry) \
1072 _vm_map_store_entry_unlink(&(copy)->cpy_hdr, (entry))
1074 #if MACH_ASSERT && TASK_SWAPPER
1076 * vm_map_res_reference:
1078 * Adds another valid residence count to the given map.
1080 * Map is locked so this function can be called from
1084 void vm_map_res_reference(register vm_map_t map
)
1086 /* assert map is locked */
1087 assert(map
->res_count
>= 0);
1088 assert(map
->ref_count
>= map
->res_count
);
1089 if (map
->res_count
== 0) {
1090 lck_mtx_unlock(&map
->s_lock
);
1093 lck_mtx_lock(&map
->s_lock
);
1101 * vm_map_reference_swap:
1103 * Adds valid reference and residence counts to the given map.
1105 * The map may not be in memory (i.e. zero residence count).
1108 void vm_map_reference_swap(register vm_map_t map
)
1110 assert(map
!= VM_MAP_NULL
);
1111 lck_mtx_lock(&map
->s_lock
);
1112 assert(map
->res_count
>= 0);
1113 assert(map
->ref_count
>= map
->res_count
);
1115 vm_map_res_reference(map
);
1116 lck_mtx_unlock(&map
->s_lock
);
1120 * vm_map_res_deallocate:
1122 * Decrement residence count on a map; possibly causing swapout.
1124 * The map must be in memory (i.e. non-zero residence count).
1126 * The map is locked, so this function is callable from vm_map_deallocate.
1129 void vm_map_res_deallocate(register vm_map_t map
)
1131 assert(map
->res_count
> 0);
1132 if (--map
->res_count
== 0) {
1133 lck_mtx_unlock(&map
->s_lock
);
1135 vm_map_swapout(map
);
1137 lck_mtx_lock(&map
->s_lock
);
1139 assert(map
->ref_count
>= map
->res_count
);
1141 #endif /* MACH_ASSERT && TASK_SWAPPER */
1146 * Actually destroy a map.
1155 /* final cleanup: no need to unnest shared region */
1156 flags
|= VM_MAP_REMOVE_NO_UNNESTING
;
1158 /* clean up regular map entries */
1159 (void) vm_map_delete(map
, map
->min_offset
, map
->max_offset
,
1160 flags
, VM_MAP_NULL
);
1161 /* clean up leftover special mappings (commpage, etc...) */
1162 (void) vm_map_delete(map
, 0x0, 0xFFFFFFFFFFFFF000ULL
,
1163 flags
, VM_MAP_NULL
);
1166 if (map
->default_freezer_handle
) {
1167 default_freezer_handle_deallocate(map
->default_freezer_handle
);
1168 map
->default_freezer_handle
= NULL
;
1171 vm_map_disable_hole_optimization(map
);
1174 assert(map
->hdr
.nentries
== 0);
1177 pmap_destroy(map
->pmap
);
1179 zfree(vm_map_zone
, map
);
1184 * vm_map_swapin/vm_map_swapout
1186 * Swap a map in and out, either referencing or releasing its resources.
1187 * These functions are internal use only; however, they must be exported
1188 * because they may be called from macros, which are exported.
1190 * In the case of swapout, there could be races on the residence count,
1191 * so if the residence count is up, we return, assuming that a
1192 * vm_map_deallocate() call in the near future will bring us back.
1195 * -- We use the map write lock for synchronization among races.
1196 * -- The map write lock, and not the simple s_lock, protects the
1197 * swap state of the map.
1198 * -- If a map entry is a share map, then we hold both locks, in
1199 * hierarchical order.
1201 * Synchronization Notes:
1202 * 1) If a vm_map_swapin() call happens while swapout in progress, it
1203 * will block on the map lock and proceed when swapout is through.
1204 * 2) A vm_map_reference() call at this time is illegal, and will
1205 * cause a panic. vm_map_reference() is only allowed on resident
1206 * maps, since it refuses to block.
1207 * 3) A vm_map_swapin() call during a swapin will block, and
1208 * proceeed when the first swapin is done, turning into a nop.
1209 * This is the reason the res_count is not incremented until
1210 * after the swapin is complete.
1211 * 4) There is a timing hole after the checks of the res_count, before
1212 * the map lock is taken, during which a swapin may get the lock
1213 * before a swapout about to happen. If this happens, the swapin
1214 * will detect the state and increment the reference count, causing
1215 * the swapout to be a nop, thereby delaying it until a later
1216 * vm_map_deallocate. If the swapout gets the lock first, then
1217 * the swapin will simply block until the swapout is done, and
1220 * Because vm_map_swapin() is potentially an expensive operation, it
1221 * should be used with caution.
1224 * 1) A map with a residence count of zero is either swapped, or
1226 * 2) A map with a non-zero residence count is either resident,
1227 * or being swapped in.
1230 int vm_map_swap_enable
= 1;
1232 void vm_map_swapin (vm_map_t map
)
1234 register vm_map_entry_t entry
;
1236 if (!vm_map_swap_enable
) /* debug */
1241 * First deal with various races.
1243 if (map
->sw_state
== MAP_SW_IN
)
1245 * we raced with swapout and won. Returning will incr.
1246 * the res_count, turning the swapout into a nop.
1251 * The residence count must be zero. If we raced with another
1252 * swapin, the state would have been IN; if we raced with a
1253 * swapout (after another competing swapin), we must have lost
1254 * the race to get here (see above comment), in which case
1255 * res_count is still 0.
1257 assert(map
->res_count
== 0);
1260 * There are no intermediate states of a map going out or
1261 * coming in, since the map is locked during the transition.
1263 assert(map
->sw_state
== MAP_SW_OUT
);
1266 * We now operate upon each map entry. If the entry is a sub-
1267 * or share-map, we call vm_map_res_reference upon it.
1268 * If the entry is an object, we call vm_object_res_reference
1269 * (this may iterate through the shadow chain).
1270 * Note that we hold the map locked the entire time,
1271 * even if we get back here via a recursive call in
1272 * vm_map_res_reference.
1274 entry
= vm_map_first_entry(map
);
1276 while (entry
!= vm_map_to_entry(map
)) {
1277 if (VME_OBJECT(entry
) != VM_OBJECT_NULL
) {
1278 if (entry
->is_sub_map
) {
1279 vm_map_t lmap
= VME_SUBMAP(entry
);
1280 lck_mtx_lock(&lmap
->s_lock
);
1281 vm_map_res_reference(lmap
);
1282 lck_mtx_unlock(&lmap
->s_lock
);
1284 vm_object_t object
= VME_OBEJCT(entry
);
1285 vm_object_lock(object
);
1287 * This call may iterate through the
1290 vm_object_res_reference(object
);
1291 vm_object_unlock(object
);
1294 entry
= entry
->vme_next
;
1296 assert(map
->sw_state
== MAP_SW_OUT
);
1297 map
->sw_state
= MAP_SW_IN
;
1300 void vm_map_swapout(vm_map_t map
)
1302 register vm_map_entry_t entry
;
1306 * First deal with various races.
1307 * If we raced with a swapin and lost, the residence count
1308 * will have been incremented to 1, and we simply return.
1310 lck_mtx_lock(&map
->s_lock
);
1311 if (map
->res_count
!= 0) {
1312 lck_mtx_unlock(&map
->s_lock
);
1315 lck_mtx_unlock(&map
->s_lock
);
1318 * There are no intermediate states of a map going out or
1319 * coming in, since the map is locked during the transition.
1321 assert(map
->sw_state
== MAP_SW_IN
);
1323 if (!vm_map_swap_enable
)
1327 * We now operate upon each map entry. If the entry is a sub-
1328 * or share-map, we call vm_map_res_deallocate upon it.
1329 * If the entry is an object, we call vm_object_res_deallocate
1330 * (this may iterate through the shadow chain).
1331 * Note that we hold the map locked the entire time,
1332 * even if we get back here via a recursive call in
1333 * vm_map_res_deallocate.
1335 entry
= vm_map_first_entry(map
);
1337 while (entry
!= vm_map_to_entry(map
)) {
1338 if (VME_OBJECT(entry
) != VM_OBJECT_NULL
) {
1339 if (entry
->is_sub_map
) {
1340 vm_map_t lmap
= VME_SUBMAP(entry
);
1341 lck_mtx_lock(&lmap
->s_lock
);
1342 vm_map_res_deallocate(lmap
);
1343 lck_mtx_unlock(&lmap
->s_lock
);
1345 vm_object_t object
= VME_OBJECT(entry
);
1346 vm_object_lock(object
);
1348 * This call may take a long time,
1349 * since it could actively push
1350 * out pages (if we implement it
1353 vm_object_res_deallocate(object
);
1354 vm_object_unlock(object
);
1357 entry
= entry
->vme_next
;
1359 assert(map
->sw_state
== MAP_SW_IN
);
1360 map
->sw_state
= MAP_SW_OUT
;
1363 #endif /* TASK_SWAPPER */
1366 * vm_map_lookup_entry: [ internal use only ]
1368 * Calls into the vm map store layer to find the map
1369 * entry containing (or immediately preceding) the
1370 * specified address in the given map; the entry is returned
1371 * in the "entry" parameter. The boolean
1372 * result indicates whether the address is
1373 * actually contained in the map.
1376 vm_map_lookup_entry(
1377 register vm_map_t map
,
1378 register vm_map_offset_t address
,
1379 vm_map_entry_t
*entry
) /* OUT */
1381 return ( vm_map_store_lookup_entry( map
, address
, entry
));
1385 * Routine: vm_map_find_space
1387 * Allocate a range in the specified virtual address map,
1388 * returning the entry allocated for that range.
1389 * Used by kmem_alloc, etc.
1391 * The map must be NOT be locked. It will be returned locked
1392 * on KERN_SUCCESS, unlocked on failure.
1394 * If an entry is allocated, the object/offset fields
1395 * are initialized to zero.
1399 register vm_map_t map
,
1400 vm_map_offset_t
*address
, /* OUT */
1402 vm_map_offset_t mask
,
1404 vm_map_entry_t
*o_entry
) /* OUT */
1406 vm_map_entry_t entry
, new_entry
;
1407 register vm_map_offset_t start
;
1408 register vm_map_offset_t end
;
1409 vm_map_entry_t hole_entry
;
1413 return KERN_INVALID_ARGUMENT
;
1416 if (flags
& VM_FLAGS_GUARD_AFTER
) {
1417 /* account for the back guard page in the size */
1418 size
+= VM_MAP_PAGE_SIZE(map
);
1421 new_entry
= vm_map_entry_create(map
, FALSE
);
1424 * Look for the first possible address; if there's already
1425 * something at this address, we have to start after it.
1430 if( map
->disable_vmentry_reuse
== TRUE
) {
1431 VM_MAP_HIGHEST_ENTRY(map
, entry
, start
);
1433 if (map
->holelistenabled
) {
1434 hole_entry
= (vm_map_entry_t
)map
->holes_list
;
1436 if (hole_entry
== NULL
) {
1438 * No more space in the map?
1440 vm_map_entry_dispose(map
, new_entry
);
1442 return(KERN_NO_SPACE
);
1446 start
= entry
->vme_start
;
1448 assert(first_free_is_valid(map
));
1449 if ((entry
= map
->first_free
) == vm_map_to_entry(map
))
1450 start
= map
->min_offset
;
1452 start
= entry
->vme_end
;
1457 * In any case, the "entry" always precedes
1458 * the proposed new region throughout the loop:
1462 register vm_map_entry_t next
;
1465 * Find the end of the proposed new region.
1466 * Be sure we didn't go beyond the end, or
1467 * wrap around the address.
1470 if (flags
& VM_FLAGS_GUARD_BEFORE
) {
1471 /* reserve space for the front guard page */
1472 start
+= VM_MAP_PAGE_SIZE(map
);
1474 end
= ((start
+ mask
) & ~mask
);
1477 vm_map_entry_dispose(map
, new_entry
);
1479 return(KERN_NO_SPACE
);
1484 if ((end
> map
->max_offset
) || (end
< start
)) {
1485 vm_map_entry_dispose(map
, new_entry
);
1487 return(KERN_NO_SPACE
);
1490 next
= entry
->vme_next
;
1492 if (map
->holelistenabled
) {
1493 if (entry
->vme_end
>= end
)
1497 * If there are no more entries, we must win.
1501 * If there is another entry, it must be
1502 * after the end of the potential new region.
1505 if (next
== vm_map_to_entry(map
))
1508 if (next
->vme_start
>= end
)
1513 * Didn't fit -- move to the next entry.
1518 if (map
->holelistenabled
) {
1519 if (entry
== (vm_map_entry_t
) map
->holes_list
) {
1523 vm_map_entry_dispose(map
, new_entry
);
1525 return(KERN_NO_SPACE
);
1527 start
= entry
->vme_start
;
1529 start
= entry
->vme_end
;
1533 if (map
->holelistenabled
) {
1534 if (vm_map_lookup_entry(map
, entry
->vme_start
, &entry
)) {
1535 panic("Found an existing entry (%p) instead of potential hole at address: 0x%llx.\n", entry
, (unsigned long long)entry
->vme_start
);
1541 * "start" and "end" should define the endpoints of the
1542 * available new range, and
1543 * "entry" should refer to the region before the new
1546 * the map should be locked.
1549 if (flags
& VM_FLAGS_GUARD_BEFORE
) {
1550 /* go back for the front guard page */
1551 start
-= VM_MAP_PAGE_SIZE(map
);
1555 assert(start
< end
);
1556 new_entry
->vme_start
= start
;
1557 new_entry
->vme_end
= end
;
1558 assert(page_aligned(new_entry
->vme_start
));
1559 assert(page_aligned(new_entry
->vme_end
));
1560 assert(VM_MAP_PAGE_ALIGNED(new_entry
->vme_start
,
1561 VM_MAP_PAGE_MASK(map
)));
1562 assert(VM_MAP_PAGE_ALIGNED(new_entry
->vme_end
,
1563 VM_MAP_PAGE_MASK(map
)));
1565 new_entry
->is_shared
= FALSE
;
1566 new_entry
->is_sub_map
= FALSE
;
1567 new_entry
->use_pmap
= TRUE
;
1568 VME_OBJECT_SET(new_entry
, VM_OBJECT_NULL
);
1569 VME_OFFSET_SET(new_entry
, (vm_object_offset_t
) 0);
1571 new_entry
->needs_copy
= FALSE
;
1573 new_entry
->inheritance
= VM_INHERIT_DEFAULT
;
1574 new_entry
->protection
= VM_PROT_DEFAULT
;
1575 new_entry
->max_protection
= VM_PROT_ALL
;
1576 new_entry
->behavior
= VM_BEHAVIOR_DEFAULT
;
1577 new_entry
->wired_count
= 0;
1578 new_entry
->user_wired_count
= 0;
1580 new_entry
->in_transition
= FALSE
;
1581 new_entry
->needs_wakeup
= FALSE
;
1582 new_entry
->no_cache
= FALSE
;
1583 new_entry
->permanent
= FALSE
;
1584 new_entry
->superpage_size
= FALSE
;
1585 if (VM_MAP_PAGE_SHIFT(map
) != PAGE_SHIFT
) {
1586 new_entry
->map_aligned
= TRUE
;
1588 new_entry
->map_aligned
= FALSE
;
1591 new_entry
->used_for_jit
= FALSE
;
1592 new_entry
->zero_wired_pages
= FALSE
;
1593 new_entry
->iokit_acct
= FALSE
;
1594 new_entry
->vme_resilient_codesign
= FALSE
;
1595 new_entry
->vme_resilient_media
= FALSE
;
1598 VM_GET_FLAGS_ALIAS(flags
, alias
);
1599 VME_ALIAS_SET(new_entry
, alias
);
1602 * Insert the new entry into the list
1605 vm_map_store_entry_link(map
, entry
, new_entry
);
1610 * Update the lookup hint
1612 SAVE_HINT_MAP_WRITE(map
, new_entry
);
1614 *o_entry
= new_entry
;
1615 return(KERN_SUCCESS
);
1618 int vm_map_pmap_enter_print
= FALSE
;
1619 int vm_map_pmap_enter_enable
= FALSE
;
1622 * Routine: vm_map_pmap_enter [internal only]
1625 * Force pages from the specified object to be entered into
1626 * the pmap at the specified address if they are present.
1627 * As soon as a page not found in the object the scan ends.
1632 * In/out conditions:
1633 * The source map should not be locked on entry.
1635 __unused
static void
1638 register vm_map_offset_t addr
,
1639 register vm_map_offset_t end_addr
,
1640 register vm_object_t object
,
1641 vm_object_offset_t offset
,
1642 vm_prot_t protection
)
1650 while (addr
< end_addr
) {
1651 register vm_page_t m
;
1656 * From vm_map_enter(), we come into this function without the map
1657 * lock held or the object lock held.
1658 * We haven't taken a reference on the object either.
1659 * We should do a proper lookup on the map to make sure
1660 * that things are sane before we go locking objects that
1661 * could have been deallocated from under us.
1664 vm_object_lock(object
);
1666 m
= vm_page_lookup(object
, offset
);
1669 * The user should never see encrypted data, so do not
1670 * enter an encrypted page in the page table.
1672 if (m
== VM_PAGE_NULL
|| m
->busy
|| m
->encrypted
||
1674 (m
->unusual
&& ( m
->error
|| m
->restart
|| m
->absent
))) {
1675 vm_object_unlock(object
);
1679 if (vm_map_pmap_enter_print
) {
1680 printf("vm_map_pmap_enter:");
1681 printf("map: %p, addr: %llx, object: %p, offset: %llx\n",
1682 map
, (unsigned long long)addr
, object
, (unsigned long long)offset
);
1684 type_of_fault
= DBG_CACHE_HIT_FAULT
;
1685 kr
= vm_fault_enter(m
, map
->pmap
, addr
, protection
, protection
,
1686 VM_PAGE_WIRED(m
), FALSE
, FALSE
, FALSE
,
1687 0, /* XXX need user tag / alias? */
1688 0, /* alternate accounting? */
1692 vm_object_unlock(object
);
1694 offset
+= PAGE_SIZE_64
;
1699 boolean_t
vm_map_pmap_is_empty(
1701 vm_map_offset_t start
,
1702 vm_map_offset_t end
);
1703 boolean_t
vm_map_pmap_is_empty(
1705 vm_map_offset_t start
,
1706 vm_map_offset_t end
)
1708 #ifdef MACHINE_PMAP_IS_EMPTY
1709 return pmap_is_empty(map
->pmap
, start
, end
);
1710 #else /* MACHINE_PMAP_IS_EMPTY */
1711 vm_map_offset_t offset
;
1714 if (map
->pmap
== NULL
) {
1718 for (offset
= start
;
1720 offset
+= PAGE_SIZE
) {
1721 phys_page
= pmap_find_phys(map
->pmap
, offset
);
1723 kprintf("vm_map_pmap_is_empty(%p,0x%llx,0x%llx): "
1724 "page %d at 0x%llx\n",
1725 map
, (long long)start
, (long long)end
,
1726 phys_page
, (long long)offset
);
1731 #endif /* MACHINE_PMAP_IS_EMPTY */
1734 #define MAX_TRIES_TO_GET_RANDOM_ADDRESS 1000
1736 vm_map_random_address_for_size(
1738 vm_map_offset_t
*address
,
1741 kern_return_t kr
= KERN_SUCCESS
;
1743 vm_map_offset_t random_addr
= 0;
1744 vm_map_offset_t hole_end
;
1746 vm_map_entry_t next_entry
= VM_MAP_ENTRY_NULL
;
1747 vm_map_entry_t prev_entry
= VM_MAP_ENTRY_NULL
;
1748 vm_map_size_t vm_hole_size
= 0;
1749 vm_map_size_t addr_space_size
;
1751 addr_space_size
= vm_map_max(map
) - vm_map_min(map
);
1753 assert(page_aligned(size
));
1755 while (tries
< MAX_TRIES_TO_GET_RANDOM_ADDRESS
) {
1756 random_addr
= ((vm_map_offset_t
)random()) << PAGE_SHIFT
;
1757 random_addr
= vm_map_trunc_page(
1758 vm_map_min(map
) +(random_addr
% addr_space_size
),
1759 VM_MAP_PAGE_MASK(map
));
1761 if (vm_map_lookup_entry(map
, random_addr
, &prev_entry
) == FALSE
) {
1762 if (prev_entry
== vm_map_to_entry(map
)) {
1763 next_entry
= vm_map_first_entry(map
);
1765 next_entry
= prev_entry
->vme_next
;
1767 if (next_entry
== vm_map_to_entry(map
)) {
1768 hole_end
= vm_map_max(map
);
1770 hole_end
= next_entry
->vme_start
;
1772 vm_hole_size
= hole_end
- random_addr
;
1773 if (vm_hole_size
>= size
) {
1774 *address
= random_addr
;
1781 if (tries
== MAX_TRIES_TO_GET_RANDOM_ADDRESS
) {
1788 * Routine: vm_map_enter
1791 * Allocate a range in the specified virtual address map.
1792 * The resulting range will refer to memory defined by
1793 * the given memory object and offset into that object.
1795 * Arguments are as defined in the vm_map call.
1797 int _map_enter_debug
= 0;
1798 static unsigned int vm_map_enter_restore_successes
= 0;
1799 static unsigned int vm_map_enter_restore_failures
= 0;
1803 vm_map_offset_t
*address
, /* IN/OUT */
1805 vm_map_offset_t mask
,
1808 vm_object_offset_t offset
,
1809 boolean_t needs_copy
,
1810 vm_prot_t cur_protection
,
1811 vm_prot_t max_protection
,
1812 vm_inherit_t inheritance
)
1814 vm_map_entry_t entry
, new_entry
;
1815 vm_map_offset_t start
, tmp_start
, tmp_offset
;
1816 vm_map_offset_t end
, tmp_end
;
1817 vm_map_offset_t tmp2_start
, tmp2_end
;
1818 vm_map_offset_t step
;
1819 kern_return_t result
= KERN_SUCCESS
;
1820 vm_map_t zap_old_map
= VM_MAP_NULL
;
1821 vm_map_t zap_new_map
= VM_MAP_NULL
;
1822 boolean_t map_locked
= FALSE
;
1823 boolean_t pmap_empty
= TRUE
;
1824 boolean_t new_mapping_established
= FALSE
;
1825 boolean_t keep_map_locked
= ((flags
& VM_FLAGS_KEEP_MAP_LOCKED
) != 0);
1826 boolean_t anywhere
= ((flags
& VM_FLAGS_ANYWHERE
) != 0);
1827 boolean_t purgable
= ((flags
& VM_FLAGS_PURGABLE
) != 0);
1828 boolean_t overwrite
= ((flags
& VM_FLAGS_OVERWRITE
) != 0);
1829 boolean_t no_cache
= ((flags
& VM_FLAGS_NO_CACHE
) != 0);
1830 boolean_t is_submap
= ((flags
& VM_FLAGS_SUBMAP
) != 0);
1831 boolean_t permanent
= ((flags
& VM_FLAGS_PERMANENT
) != 0);
1832 boolean_t entry_for_jit
= ((flags
& VM_FLAGS_MAP_JIT
) != 0);
1833 boolean_t iokit_acct
= ((flags
& VM_FLAGS_IOKIT_ACCT
) != 0);
1834 boolean_t resilient_codesign
= ((flags
& VM_FLAGS_RESILIENT_CODESIGN
) != 0);
1835 boolean_t resilient_media
= ((flags
& VM_FLAGS_RESILIENT_MEDIA
) != 0);
1836 unsigned int superpage_size
= ((flags
& VM_FLAGS_SUPERPAGE_MASK
) >> VM_FLAGS_SUPERPAGE_SHIFT
);
1837 vm_tag_t alias
, user_alias
;
1838 vm_map_offset_t effective_min_offset
, effective_max_offset
;
1840 boolean_t clear_map_aligned
= FALSE
;
1841 vm_map_entry_t hole_entry
;
1843 if (superpage_size
) {
1844 switch (superpage_size
) {
1846 * Note that the current implementation only supports
1847 * a single size for superpages, SUPERPAGE_SIZE, per
1848 * architecture. As soon as more sizes are supposed
1849 * to be supported, SUPERPAGE_SIZE has to be replaced
1850 * with a lookup of the size depending on superpage_size.
1853 case SUPERPAGE_SIZE_ANY
:
1854 /* handle it like 2 MB and round up to page size */
1855 size
= (size
+ 2*1024*1024 - 1) & ~(2*1024*1024 - 1);
1856 case SUPERPAGE_SIZE_2MB
:
1860 return KERN_INVALID_ARGUMENT
;
1862 mask
= SUPERPAGE_SIZE
-1;
1863 if (size
& (SUPERPAGE_SIZE
-1))
1864 return KERN_INVALID_ARGUMENT
;
1865 inheritance
= VM_INHERIT_NONE
; /* fork() children won't inherit superpages */
1870 if (resilient_codesign
|| resilient_media
) {
1871 if ((cur_protection
& (VM_PROT_WRITE
| VM_PROT_EXECUTE
)) ||
1872 (max_protection
& (VM_PROT_WRITE
| VM_PROT_EXECUTE
))) {
1873 return KERN_PROTECTION_FAILURE
;
1879 /* submaps can not be purgeable */
1880 return KERN_INVALID_ARGUMENT
;
1882 if (object
== VM_OBJECT_NULL
) {
1883 /* submaps can not be created lazily */
1884 return KERN_INVALID_ARGUMENT
;
1887 if (flags
& VM_FLAGS_ALREADY
) {
1889 * VM_FLAGS_ALREADY says that it's OK if the same mapping
1890 * is already present. For it to be meaningul, the requested
1891 * mapping has to be at a fixed address (!VM_FLAGS_ANYWHERE) and
1892 * we shouldn't try and remove what was mapped there first
1893 * (!VM_FLAGS_OVERWRITE).
1895 if ((flags
& VM_FLAGS_ANYWHERE
) ||
1896 (flags
& VM_FLAGS_OVERWRITE
)) {
1897 return KERN_INVALID_ARGUMENT
;
1901 effective_min_offset
= map
->min_offset
;
1903 if (flags
& VM_FLAGS_BEYOND_MAX
) {
1905 * Allow an insertion beyond the map's max offset.
1907 if (vm_map_is_64bit(map
))
1908 effective_max_offset
= 0xFFFFFFFFFFFFF000ULL
;
1910 effective_max_offset
= 0x00000000FFFFF000ULL
;
1912 effective_max_offset
= map
->max_offset
;
1916 (offset
& PAGE_MASK_64
) != 0) {
1918 return KERN_INVALID_ARGUMENT
;
1921 VM_GET_FLAGS_ALIAS(flags
, alias
);
1922 if (map
->pmap
== kernel_pmap
) {
1923 user_alias
= VM_KERN_MEMORY_NONE
;
1928 #define RETURN(value) { result = value; goto BailOut; }
1930 assert(page_aligned(*address
));
1931 assert(page_aligned(size
));
1933 if (!VM_MAP_PAGE_ALIGNED(size
, VM_MAP_PAGE_MASK(map
))) {
1935 * In most cases, the caller rounds the size up to the
1937 * If we get a size that is explicitly not map-aligned here,
1938 * we'll have to respect the caller's wish and mark the
1939 * mapping as "not map-aligned" to avoid tripping the
1940 * map alignment checks later.
1942 clear_map_aligned
= TRUE
;
1945 !VM_MAP_PAGE_ALIGNED(*address
, VM_MAP_PAGE_MASK(map
))) {
1947 * We've been asked to map at a fixed address and that
1948 * address is not aligned to the map's specific alignment.
1949 * The caller should know what it's doing (i.e. most likely
1950 * mapping some fragmented copy map, transferring memory from
1951 * a VM map with a different alignment), so clear map_aligned
1952 * for this new VM map entry and proceed.
1954 clear_map_aligned
= TRUE
;
1958 * Only zero-fill objects are allowed to be purgable.
1959 * LP64todo - limit purgable objects to 32-bits for now
1963 (object
!= VM_OBJECT_NULL
&&
1964 (object
->vo_size
!= size
||
1965 object
->purgable
== VM_PURGABLE_DENY
))
1966 || size
> ANON_MAX_SIZE
)) /* LP64todo: remove when dp capable */
1967 return KERN_INVALID_ARGUMENT
;
1969 if (!anywhere
&& overwrite
) {
1971 * Create a temporary VM map to hold the old mappings in the
1972 * affected area while we create the new one.
1973 * This avoids releasing the VM map lock in
1974 * vm_map_entry_delete() and allows atomicity
1975 * when we want to replace some mappings with a new one.
1976 * It also allows us to restore the old VM mappings if the
1977 * new mapping fails.
1979 zap_old_map
= vm_map_create(PMAP_NULL
,
1982 map
->hdr
.entries_pageable
);
1983 vm_map_set_page_shift(zap_old_map
, VM_MAP_PAGE_SHIFT(map
));
1984 vm_map_disable_hole_optimization(zap_old_map
);
1995 if (entry_for_jit
) {
1996 if (map
->jit_entry_exists
) {
1997 result
= KERN_INVALID_ARGUMENT
;
2001 * Get a random start address.
2003 result
= vm_map_random_address_for_size(map
, address
, size
);
2004 if (result
!= KERN_SUCCESS
) {
2012 * Calculate the first possible address.
2015 if (start
< effective_min_offset
)
2016 start
= effective_min_offset
;
2017 if (start
> effective_max_offset
)
2018 RETURN(KERN_NO_SPACE
);
2021 * Look for the first possible address;
2022 * if there's already something at this
2023 * address, we have to start after it.
2026 if( map
->disable_vmentry_reuse
== TRUE
) {
2027 VM_MAP_HIGHEST_ENTRY(map
, entry
, start
);
2030 if (map
->holelistenabled
) {
2031 hole_entry
= (vm_map_entry_t
)map
->holes_list
;
2033 if (hole_entry
== NULL
) {
2035 * No more space in the map?
2037 result
= KERN_NO_SPACE
;
2041 boolean_t found_hole
= FALSE
;
2044 if (hole_entry
->vme_start
>= start
) {
2045 start
= hole_entry
->vme_start
;
2050 if (hole_entry
->vme_end
> start
) {
2054 hole_entry
= hole_entry
->vme_next
;
2056 } while (hole_entry
!= (vm_map_entry_t
) map
->holes_list
);
2058 if (found_hole
== FALSE
) {
2059 result
= KERN_NO_SPACE
;
2066 start
+= PAGE_SIZE_64
;
2069 assert(first_free_is_valid(map
));
2071 entry
= map
->first_free
;
2073 if (entry
== vm_map_to_entry(map
)) {
2076 if (entry
->vme_next
== vm_map_to_entry(map
)){
2078 * Hole at the end of the map.
2082 if (start
< (entry
->vme_next
)->vme_start
) {
2083 start
= entry
->vme_end
;
2084 start
= vm_map_round_page(start
,
2085 VM_MAP_PAGE_MASK(map
));
2088 * Need to do a lookup.
2095 if (entry
== NULL
) {
2096 vm_map_entry_t tmp_entry
;
2097 if (vm_map_lookup_entry(map
, start
, &tmp_entry
)) {
2098 assert(!entry_for_jit
);
2099 start
= tmp_entry
->vme_end
;
2100 start
= vm_map_round_page(start
,
2101 VM_MAP_PAGE_MASK(map
));
2109 * In any case, the "entry" always precedes
2110 * the proposed new region throughout the
2115 register vm_map_entry_t next
;
2118 * Find the end of the proposed new region.
2119 * Be sure we didn't go beyond the end, or
2120 * wrap around the address.
2123 end
= ((start
+ mask
) & ~mask
);
2124 end
= vm_map_round_page(end
,
2125 VM_MAP_PAGE_MASK(map
));
2127 RETURN(KERN_NO_SPACE
);
2129 assert(VM_MAP_PAGE_ALIGNED(start
,
2130 VM_MAP_PAGE_MASK(map
)));
2133 if ((end
> effective_max_offset
) || (end
< start
)) {
2134 if (map
->wait_for_space
) {
2135 assert(!keep_map_locked
);
2136 if (size
<= (effective_max_offset
-
2137 effective_min_offset
)) {
2138 assert_wait((event_t
)map
,
2142 thread_block(THREAD_CONTINUE_NULL
);
2146 RETURN(KERN_NO_SPACE
);
2149 next
= entry
->vme_next
;
2151 if (map
->holelistenabled
) {
2152 if (entry
->vme_end
>= end
)
2156 * If there are no more entries, we must win.
2160 * If there is another entry, it must be
2161 * after the end of the potential new region.
2164 if (next
== vm_map_to_entry(map
))
2167 if (next
->vme_start
>= end
)
2172 * Didn't fit -- move to the next entry.
2177 if (map
->holelistenabled
) {
2178 if (entry
== (vm_map_entry_t
) map
->holes_list
) {
2182 result
= KERN_NO_SPACE
;
2185 start
= entry
->vme_start
;
2187 start
= entry
->vme_end
;
2190 start
= vm_map_round_page(start
,
2191 VM_MAP_PAGE_MASK(map
));
2194 if (map
->holelistenabled
) {
2195 if (vm_map_lookup_entry(map
, entry
->vme_start
, &entry
)) {
2196 panic("Found an existing entry (%p) instead of potential hole at address: 0x%llx.\n", entry
, (unsigned long long)entry
->vme_start
);
2201 assert(VM_MAP_PAGE_ALIGNED(*address
,
2202 VM_MAP_PAGE_MASK(map
)));
2206 * the address doesn't itself violate
2207 * the mask requirement.
2212 if ((start
& mask
) != 0)
2213 RETURN(KERN_NO_SPACE
);
2216 * ... the address is within bounds
2221 if ((start
< effective_min_offset
) ||
2222 (end
> effective_max_offset
) ||
2224 RETURN(KERN_INVALID_ADDRESS
);
2227 if (overwrite
&& zap_old_map
!= VM_MAP_NULL
) {
2229 * Fixed mapping and "overwrite" flag: attempt to
2230 * remove all existing mappings in the specified
2231 * address range, saving them in our "zap_old_map".
2233 (void) vm_map_delete(map
, start
, end
,
2234 (VM_MAP_REMOVE_SAVE_ENTRIES
|
2235 VM_MAP_REMOVE_NO_MAP_ALIGN
),
2240 * ... the starting address isn't allocated
2243 if (vm_map_lookup_entry(map
, start
, &entry
)) {
2244 if (! (flags
& VM_FLAGS_ALREADY
)) {
2245 RETURN(KERN_NO_SPACE
);
2248 * Check if what's already there is what we want.
2251 tmp_offset
= offset
;
2252 if (entry
->vme_start
< start
) {
2253 tmp_start
-= start
- entry
->vme_start
;
2254 tmp_offset
-= start
- entry
->vme_start
;
2257 for (; entry
->vme_start
< end
;
2258 entry
= entry
->vme_next
) {
2260 * Check if the mapping's attributes
2261 * match the existing map entry.
2263 if (entry
== vm_map_to_entry(map
) ||
2264 entry
->vme_start
!= tmp_start
||
2265 entry
->is_sub_map
!= is_submap
||
2266 VME_OFFSET(entry
) != tmp_offset
||
2267 entry
->needs_copy
!= needs_copy
||
2268 entry
->protection
!= cur_protection
||
2269 entry
->max_protection
!= max_protection
||
2270 entry
->inheritance
!= inheritance
||
2271 entry
->iokit_acct
!= iokit_acct
||
2272 VME_ALIAS(entry
) != alias
) {
2273 /* not the same mapping ! */
2274 RETURN(KERN_NO_SPACE
);
2277 * Check if the same object is being mapped.
2280 if (VME_SUBMAP(entry
) !=
2281 (vm_map_t
) object
) {
2282 /* not the same submap */
2283 RETURN(KERN_NO_SPACE
);
2286 if (VME_OBJECT(entry
) != object
) {
2287 /* not the same VM object... */
2290 obj2
= VME_OBJECT(entry
);
2291 if ((obj2
== VM_OBJECT_NULL
||
2293 (object
== VM_OBJECT_NULL
||
2294 object
->internal
)) {
2301 RETURN(KERN_NO_SPACE
);
2306 tmp_offset
+= entry
->vme_end
- entry
->vme_start
;
2307 tmp_start
+= entry
->vme_end
- entry
->vme_start
;
2308 if (entry
->vme_end
>= end
) {
2309 /* reached the end of our mapping */
2313 /* it all matches: let's use what's already there ! */
2314 RETURN(KERN_MEMORY_PRESENT
);
2318 * ... the next region doesn't overlap the
2322 if ((entry
->vme_next
!= vm_map_to_entry(map
)) &&
2323 (entry
->vme_next
->vme_start
< end
))
2324 RETURN(KERN_NO_SPACE
);
2329 * "start" and "end" should define the endpoints of the
2330 * available new range, and
2331 * "entry" should refer to the region before the new
2334 * the map should be locked.
2338 * See whether we can avoid creating a new entry (and object) by
2339 * extending one of our neighbors. [So far, we only attempt to
2340 * extend from below.] Note that we can never extend/join
2341 * purgable objects because they need to remain distinct
2342 * entities in order to implement their "volatile object"
2346 if (purgable
|| entry_for_jit
) {
2347 if (object
== VM_OBJECT_NULL
) {
2349 object
= vm_object_allocate(size
);
2350 object
->copy_strategy
= MEMORY_OBJECT_COPY_NONE
;
2351 object
->true_share
= TRUE
;
2354 object
->purgable
= VM_PURGABLE_NONVOLATILE
;
2355 if (map
->pmap
== kernel_pmap
) {
2357 * Purgeable mappings made in a kernel
2358 * map are "owned" by the kernel itself
2359 * rather than the current user task
2360 * because they're likely to be used by
2361 * more than this user task (see
2362 * execargs_purgeable_allocate(), for
2365 owner
= kernel_task
;
2367 owner
= current_task();
2369 assert(object
->vo_purgeable_owner
== NULL
);
2370 assert(object
->resident_page_count
== 0);
2371 assert(object
->wired_page_count
== 0);
2372 vm_object_lock(object
);
2373 vm_purgeable_nonvolatile_enqueue(object
, owner
);
2374 vm_object_unlock(object
);
2376 offset
= (vm_object_offset_t
)0;
2378 } else if ((is_submap
== FALSE
) &&
2379 (object
== VM_OBJECT_NULL
) &&
2380 (entry
!= vm_map_to_entry(map
)) &&
2381 (entry
->vme_end
== start
) &&
2382 (!entry
->is_shared
) &&
2383 (!entry
->is_sub_map
) &&
2384 (!entry
->in_transition
) &&
2385 (!entry
->needs_wakeup
) &&
2386 (entry
->behavior
== VM_BEHAVIOR_DEFAULT
) &&
2387 (entry
->protection
== cur_protection
) &&
2388 (entry
->max_protection
== max_protection
) &&
2389 (entry
->inheritance
== inheritance
) &&
2390 ((user_alias
== VM_MEMORY_REALLOC
) ||
2391 (VME_ALIAS(entry
) == alias
)) &&
2392 (entry
->no_cache
== no_cache
) &&
2393 (entry
->permanent
== permanent
) &&
2394 (!entry
->superpage_size
&& !superpage_size
) &&
2396 * No coalescing if not map-aligned, to avoid propagating
2397 * that condition any further than needed:
2399 (!entry
->map_aligned
|| !clear_map_aligned
) &&
2400 (!entry
->zero_wired_pages
) &&
2401 (!entry
->used_for_jit
&& !entry_for_jit
) &&
2402 (entry
->iokit_acct
== iokit_acct
) &&
2403 (!entry
->vme_resilient_codesign
) &&
2404 (!entry
->vme_resilient_media
) &&
2406 ((entry
->vme_end
- entry
->vme_start
) + size
<=
2407 (user_alias
== VM_MEMORY_REALLOC
?
2409 NO_COALESCE_LIMIT
)) &&
2411 (entry
->wired_count
== 0)) { /* implies user_wired_count == 0 */
2412 if (vm_object_coalesce(VME_OBJECT(entry
),
2415 (vm_object_offset_t
) 0,
2416 (vm_map_size_t
)(entry
->vme_end
- entry
->vme_start
),
2417 (vm_map_size_t
)(end
- entry
->vme_end
))) {
2420 * Coalesced the two objects - can extend
2421 * the previous map entry to include the
2424 map
->size
+= (end
- entry
->vme_end
);
2425 assert(entry
->vme_start
< end
);
2426 assert(VM_MAP_PAGE_ALIGNED(end
,
2427 VM_MAP_PAGE_MASK(map
)));
2428 if (__improbable(vm_debug_events
))
2429 DTRACE_VM5(map_entry_extend
, vm_map_t
, map
, vm_map_entry_t
, entry
, vm_address_t
, entry
->vme_start
, vm_address_t
, entry
->vme_end
, vm_address_t
, end
);
2430 entry
->vme_end
= end
;
2431 if (map
->holelistenabled
) {
2432 vm_map_store_update_first_free(map
, entry
, TRUE
);
2434 vm_map_store_update_first_free(map
, map
->first_free
, TRUE
);
2436 new_mapping_established
= TRUE
;
2437 RETURN(KERN_SUCCESS
);
2441 step
= superpage_size
? SUPERPAGE_SIZE
: (end
- start
);
2444 for (tmp2_start
= start
; tmp2_start
<end
; tmp2_start
+= step
) {
2445 tmp2_end
= tmp2_start
+ step
;
2447 * Create a new entry
2448 * LP64todo - for now, we can only allocate 4GB internal objects
2449 * because the default pager can't page bigger ones. Remove this
2453 * The reserved "page zero" in each process's address space can
2454 * be arbitrarily large. Splitting it into separate 4GB objects and
2455 * therefore different VM map entries serves no purpose and just
2456 * slows down operations on the VM map, so let's not split the
2457 * allocation into 4GB chunks if the max protection is NONE. That
2458 * memory should never be accessible, so it will never get to the
2461 tmp_start
= tmp2_start
;
2462 if (object
== VM_OBJECT_NULL
&&
2463 size
> (vm_map_size_t
)ANON_CHUNK_SIZE
&&
2464 max_protection
!= VM_PROT_NONE
&&
2465 superpage_size
== 0)
2466 tmp_end
= tmp_start
+ (vm_map_size_t
)ANON_CHUNK_SIZE
;
2470 new_entry
= vm_map_entry_insert(map
, entry
, tmp_start
, tmp_end
,
2471 object
, offset
, needs_copy
,
2473 cur_protection
, max_protection
,
2474 VM_BEHAVIOR_DEFAULT
,
2475 (entry_for_jit
)? VM_INHERIT_NONE
: inheritance
,
2482 assert((object
!= kernel_object
) || (VM_KERN_MEMORY_NONE
!= alias
));
2483 VME_ALIAS_SET(new_entry
, alias
);
2486 if (!(map
->jit_entry_exists
)){
2487 new_entry
->used_for_jit
= TRUE
;
2488 map
->jit_entry_exists
= TRUE
;
2492 if (resilient_codesign
&&
2493 ! ((cur_protection
| max_protection
) &
2494 (VM_PROT_WRITE
| VM_PROT_EXECUTE
))) {
2495 new_entry
->vme_resilient_codesign
= TRUE
;
2498 if (resilient_media
&&
2499 ! ((cur_protection
| max_protection
) &
2500 (VM_PROT_WRITE
| VM_PROT_EXECUTE
))) {
2501 new_entry
->vme_resilient_media
= TRUE
;
2504 assert(!new_entry
->iokit_acct
);
2506 object
!= VM_OBJECT_NULL
&&
2507 object
->purgable
!= VM_PURGABLE_DENY
) {
2508 assert(new_entry
->use_pmap
);
2509 assert(!new_entry
->iokit_acct
);
2511 * Turn off pmap accounting since
2512 * purgeable objects have their
2515 new_entry
->use_pmap
= FALSE
;
2516 } else if (!is_submap
&&
2518 /* alternate accounting */
2519 assert(!new_entry
->iokit_acct
);
2520 assert(new_entry
->use_pmap
);
2521 new_entry
->iokit_acct
= TRUE
;
2522 new_entry
->use_pmap
= FALSE
;
2523 vm_map_iokit_mapped_region(
2525 (new_entry
->vme_end
-
2526 new_entry
->vme_start
));
2527 } else if (!is_submap
) {
2528 assert(!new_entry
->iokit_acct
);
2529 assert(new_entry
->use_pmap
);
2534 boolean_t submap_is_64bit
;
2537 assert(new_entry
->is_sub_map
);
2538 assert(!new_entry
->use_pmap
);
2539 assert(!new_entry
->iokit_acct
);
2540 submap
= (vm_map_t
) object
;
2541 submap_is_64bit
= vm_map_is_64bit(submap
);
2542 use_pmap
= (user_alias
== VM_MEMORY_SHARED_PMAP
);
2543 #ifndef NO_NESTED_PMAP
2544 if (use_pmap
&& submap
->pmap
== NULL
) {
2545 ledger_t ledger
= map
->pmap
->ledger
;
2546 /* we need a sub pmap to nest... */
2547 submap
->pmap
= pmap_create(ledger
, 0,
2549 if (submap
->pmap
== NULL
) {
2550 /* let's proceed without nesting... */
2553 if (use_pmap
&& submap
->pmap
!= NULL
) {
2554 kr
= pmap_nest(map
->pmap
,
2558 tmp_end
- tmp_start
);
2559 if (kr
!= KERN_SUCCESS
) {
2560 printf("vm_map_enter: "
2561 "pmap_nest(0x%llx,0x%llx) "
2563 (long long)tmp_start
,
2567 /* we're now nested ! */
2568 new_entry
->use_pmap
= TRUE
;
2572 #endif /* NO_NESTED_PMAP */
2576 if (superpage_size
) {
2578 vm_object_t sp_object
;
2580 VME_OFFSET_SET(entry
, 0);
2582 /* allocate one superpage */
2583 kr
= cpm_allocate(SUPERPAGE_SIZE
, &pages
, 0, SUPERPAGE_NBASEPAGES
-1, TRUE
, 0);
2584 if (kr
!= KERN_SUCCESS
) {
2585 /* deallocate whole range... */
2586 new_mapping_established
= TRUE
;
2587 /* ... but only up to "tmp_end" */
2588 size
-= end
- tmp_end
;
2592 /* create one vm_object per superpage */
2593 sp_object
= vm_object_allocate((vm_map_size_t
)(entry
->vme_end
- entry
->vme_start
));
2594 sp_object
->phys_contiguous
= TRUE
;
2595 sp_object
->vo_shadow_offset
= (vm_object_offset_t
)pages
->phys_page
*PAGE_SIZE
;
2596 VME_OBJECT_SET(entry
, sp_object
);
2597 assert(entry
->use_pmap
);
2599 /* enter the base pages into the object */
2600 vm_object_lock(sp_object
);
2601 for (offset
= 0; offset
< SUPERPAGE_SIZE
; offset
+= PAGE_SIZE
) {
2603 pmap_zero_page(m
->phys_page
);
2604 pages
= NEXT_PAGE(m
);
2605 *(NEXT_PAGE_PTR(m
)) = VM_PAGE_NULL
;
2606 vm_page_insert_wired(m
, sp_object
, offset
, VM_KERN_MEMORY_OSFMK
);
2608 vm_object_unlock(sp_object
);
2610 } while (tmp_end
!= tmp2_end
&&
2611 (tmp_start
= tmp_end
) &&
2612 (tmp_end
= (tmp2_end
- tmp_end
> (vm_map_size_t
)ANON_CHUNK_SIZE
) ?
2613 tmp_end
+ (vm_map_size_t
)ANON_CHUNK_SIZE
: tmp2_end
));
2616 new_mapping_established
= TRUE
;
2619 assert(map_locked
== TRUE
);
2621 if (result
== KERN_SUCCESS
) {
2622 vm_prot_t pager_prot
;
2623 memory_object_t pager
;
2627 !(flags
& VM_FLAGS_NO_PMAP_CHECK
)) {
2628 assert(vm_map_pmap_is_empty(map
,
2635 * For "named" VM objects, let the pager know that the
2636 * memory object is being mapped. Some pagers need to keep
2637 * track of this, to know when they can reclaim the memory
2638 * object, for example.
2639 * VM calls memory_object_map() for each mapping (specifying
2640 * the protection of each mapping) and calls
2641 * memory_object_last_unmap() when all the mappings are gone.
2643 pager_prot
= max_protection
;
2646 * Copy-On-Write mapping: won't modify
2647 * the memory object.
2649 pager_prot
&= ~VM_PROT_WRITE
;
2652 object
!= VM_OBJECT_NULL
&&
2654 object
->pager
!= MEMORY_OBJECT_NULL
) {
2655 vm_object_lock(object
);
2656 pager
= object
->pager
;
2657 if (object
->named
&&
2658 pager
!= MEMORY_OBJECT_NULL
) {
2659 assert(object
->pager_ready
);
2660 vm_object_mapping_wait(object
, THREAD_UNINT
);
2661 vm_object_mapping_begin(object
);
2662 vm_object_unlock(object
);
2664 kr
= memory_object_map(pager
, pager_prot
);
2665 assert(kr
== KERN_SUCCESS
);
2667 vm_object_lock(object
);
2668 vm_object_mapping_end(object
);
2670 vm_object_unlock(object
);
2674 assert(map_locked
== TRUE
);
2676 if (!keep_map_locked
) {
2682 * We can't hold the map lock if we enter this block.
2685 if (result
== KERN_SUCCESS
) {
2687 /* Wire down the new entry if the user
2688 * requested all new map entries be wired.
2690 if ((map
->wiring_required
)||(superpage_size
)) {
2691 assert(!keep_map_locked
);
2692 pmap_empty
= FALSE
; /* pmap won't be empty */
2693 kr
= vm_map_wire(map
, start
, end
,
2694 new_entry
->protection
| VM_PROT_MEMORY_TAG_MAKE(VM_KERN_MEMORY_MLOCK
),
2701 if (result
!= KERN_SUCCESS
) {
2702 if (new_mapping_established
) {
2704 * We have to get rid of the new mappings since we
2705 * won't make them available to the user.
2706 * Try and do that atomically, to minimize the risk
2707 * that someone else create new mappings that range.
2709 zap_new_map
= vm_map_create(PMAP_NULL
,
2712 map
->hdr
.entries_pageable
);
2713 vm_map_set_page_shift(zap_new_map
,
2714 VM_MAP_PAGE_SHIFT(map
));
2715 vm_map_disable_hole_optimization(zap_new_map
);
2721 (void) vm_map_delete(map
, *address
, *address
+size
,
2722 (VM_MAP_REMOVE_SAVE_ENTRIES
|
2723 VM_MAP_REMOVE_NO_MAP_ALIGN
),
2726 if (zap_old_map
!= VM_MAP_NULL
&&
2727 zap_old_map
->hdr
.nentries
!= 0) {
2728 vm_map_entry_t entry1
, entry2
;
2731 * The new mapping failed. Attempt to restore
2732 * the old mappings, saved in the "zap_old_map".
2739 /* first check if the coast is still clear */
2740 start
= vm_map_first_entry(zap_old_map
)->vme_start
;
2741 end
= vm_map_last_entry(zap_old_map
)->vme_end
;
2742 if (vm_map_lookup_entry(map
, start
, &entry1
) ||
2743 vm_map_lookup_entry(map
, end
, &entry2
) ||
2746 * Part of that range has already been
2747 * re-mapped: we can't restore the old
2750 vm_map_enter_restore_failures
++;
2753 * Transfer the saved map entries from
2754 * "zap_old_map" to the original "map",
2755 * inserting them all after "entry1".
2757 for (entry2
= vm_map_first_entry(zap_old_map
);
2758 entry2
!= vm_map_to_entry(zap_old_map
);
2759 entry2
= vm_map_first_entry(zap_old_map
)) {
2760 vm_map_size_t entry_size
;
2762 entry_size
= (entry2
->vme_end
-
2764 vm_map_store_entry_unlink(zap_old_map
,
2766 zap_old_map
->size
-= entry_size
;
2767 vm_map_store_entry_link(map
, entry1
, entry2
);
2768 map
->size
+= entry_size
;
2771 if (map
->wiring_required
) {
2773 * XXX TODO: we should rewire the
2777 vm_map_enter_restore_successes
++;
2783 * The caller is responsible for releasing the lock if it requested to
2784 * keep the map locked.
2786 if (map_locked
&& !keep_map_locked
) {
2791 * Get rid of the "zap_maps" and all the map entries that
2792 * they may still contain.
2794 if (zap_old_map
!= VM_MAP_NULL
) {
2795 vm_map_destroy(zap_old_map
, VM_MAP_REMOVE_NO_PMAP_CLEANUP
);
2796 zap_old_map
= VM_MAP_NULL
;
2798 if (zap_new_map
!= VM_MAP_NULL
) {
2799 vm_map_destroy(zap_new_map
, VM_MAP_REMOVE_NO_PMAP_CLEANUP
);
2800 zap_new_map
= VM_MAP_NULL
;
2810 * Counters for the prefault optimization.
2812 int64_t vm_prefault_nb_pages
= 0;
2813 int64_t vm_prefault_nb_bailout
= 0;
2815 static kern_return_t
2816 vm_map_enter_mem_object_helper(
2817 vm_map_t target_map
,
2818 vm_map_offset_t
*address
,
2819 vm_map_size_t initial_size
,
2820 vm_map_offset_t mask
,
2823 vm_object_offset_t offset
,
2825 vm_prot_t cur_protection
,
2826 vm_prot_t max_protection
,
2827 vm_inherit_t inheritance
,
2828 upl_page_list_ptr_t page_list
,
2829 unsigned int page_list_count
)
2831 vm_map_address_t map_addr
;
2832 vm_map_size_t map_size
;
2834 vm_object_size_t size
;
2835 kern_return_t result
;
2836 boolean_t mask_cur_protection
, mask_max_protection
;
2837 boolean_t try_prefault
= (page_list_count
!= 0);
2838 vm_map_offset_t offset_in_mapping
= 0;
2840 mask_cur_protection
= cur_protection
& VM_PROT_IS_MASK
;
2841 mask_max_protection
= max_protection
& VM_PROT_IS_MASK
;
2842 cur_protection
&= ~VM_PROT_IS_MASK
;
2843 max_protection
&= ~VM_PROT_IS_MASK
;
2846 * Check arguments for validity
2848 if ((target_map
== VM_MAP_NULL
) ||
2849 (cur_protection
& ~VM_PROT_ALL
) ||
2850 (max_protection
& ~VM_PROT_ALL
) ||
2851 (inheritance
> VM_INHERIT_LAST_VALID
) ||
2852 (try_prefault
&& (copy
|| !page_list
)) ||
2853 initial_size
== 0) {
2854 return KERN_INVALID_ARGUMENT
;
2858 map_addr
= vm_map_trunc_page(*address
,
2859 VM_MAP_PAGE_MASK(target_map
));
2860 map_size
= vm_map_round_page(initial_size
,
2861 VM_MAP_PAGE_MASK(target_map
));
2863 size
= vm_object_round_page(initial_size
);
2866 * Find the vm object (if any) corresponding to this port.
2868 if (!IP_VALID(port
)) {
2869 object
= VM_OBJECT_NULL
;
2872 } else if (ip_kotype(port
) == IKOT_NAMED_ENTRY
) {
2873 vm_named_entry_t named_entry
;
2875 named_entry
= (vm_named_entry_t
) port
->ip_kobject
;
2877 if (flags
& (VM_FLAGS_RETURN_DATA_ADDR
|
2878 VM_FLAGS_RETURN_4K_DATA_ADDR
)) {
2879 offset
+= named_entry
->data_offset
;
2882 /* a few checks to make sure user is obeying rules */
2884 if (offset
>= named_entry
->size
)
2885 return KERN_INVALID_RIGHT
;
2886 size
= named_entry
->size
- offset
;
2888 if (mask_max_protection
) {
2889 max_protection
&= named_entry
->protection
;
2891 if (mask_cur_protection
) {
2892 cur_protection
&= named_entry
->protection
;
2894 if ((named_entry
->protection
& max_protection
) !=
2896 return KERN_INVALID_RIGHT
;
2897 if ((named_entry
->protection
& cur_protection
) !=
2899 return KERN_INVALID_RIGHT
;
2900 if (offset
+ size
< offset
) {
2902 return KERN_INVALID_ARGUMENT
;
2904 if (named_entry
->size
< (offset
+ initial_size
)) {
2905 return KERN_INVALID_ARGUMENT
;
2908 if (named_entry
->is_copy
) {
2909 /* for a vm_map_copy, we can only map it whole */
2910 if ((size
!= named_entry
->size
) &&
2911 (vm_map_round_page(size
,
2912 VM_MAP_PAGE_MASK(target_map
)) ==
2913 named_entry
->size
)) {
2914 /* XXX FBDP use the rounded size... */
2915 size
= vm_map_round_page(
2917 VM_MAP_PAGE_MASK(target_map
));
2920 if (!(flags
& VM_FLAGS_ANYWHERE
) &&
2922 size
!= named_entry
->size
)) {
2924 * XXX for a mapping at a "fixed" address,
2925 * we can't trim after mapping the whole
2926 * memory entry, so reject a request for a
2929 return KERN_INVALID_ARGUMENT
;
2933 /* the callers parameter offset is defined to be the */
2934 /* offset from beginning of named entry offset in object */
2935 offset
= offset
+ named_entry
->offset
;
2937 if (! VM_MAP_PAGE_ALIGNED(size
,
2938 VM_MAP_PAGE_MASK(target_map
))) {
2940 * Let's not map more than requested;
2941 * vm_map_enter() will handle this "not map-aligned"
2947 named_entry_lock(named_entry
);
2948 if (named_entry
->is_sub_map
) {
2951 if (flags
& (VM_FLAGS_RETURN_DATA_ADDR
|
2952 VM_FLAGS_RETURN_4K_DATA_ADDR
)) {
2953 panic("VM_FLAGS_RETURN_DATA_ADDR not expected for submap.");
2956 submap
= named_entry
->backing
.map
;
2957 vm_map_lock(submap
);
2958 vm_map_reference(submap
);
2959 vm_map_unlock(submap
);
2960 named_entry_unlock(named_entry
);
2962 result
= vm_map_enter(target_map
,
2966 flags
| VM_FLAGS_SUBMAP
,
2967 (vm_object_t
) submap
,
2973 if (result
!= KERN_SUCCESS
) {
2974 vm_map_deallocate(submap
);
2977 * No need to lock "submap" just to check its
2978 * "mapped" flag: that flag is never reset
2979 * once it's been set and if we race, we'll
2980 * just end up setting it twice, which is OK.
2982 if (submap
->mapped_in_other_pmaps
== FALSE
&&
2983 vm_map_pmap(submap
) != PMAP_NULL
&&
2984 vm_map_pmap(submap
) !=
2985 vm_map_pmap(target_map
)) {
2987 * This submap is being mapped in a map
2988 * that uses a different pmap.
2989 * Set its "mapped_in_other_pmaps" flag
2990 * to indicate that we now need to
2991 * remove mappings from all pmaps rather
2992 * than just the submap's pmap.
2994 vm_map_lock(submap
);
2995 submap
->mapped_in_other_pmaps
= TRUE
;
2996 vm_map_unlock(submap
);
2998 *address
= map_addr
;
3002 } else if (named_entry
->is_pager
) {
3003 unsigned int access
;
3004 vm_prot_t protections
;
3005 unsigned int wimg_mode
;
3007 protections
= named_entry
->protection
& VM_PROT_ALL
;
3008 access
= GET_MAP_MEM(named_entry
->protection
);
3010 if (flags
& (VM_FLAGS_RETURN_DATA_ADDR
|
3011 VM_FLAGS_RETURN_4K_DATA_ADDR
)) {
3012 panic("VM_FLAGS_RETURN_DATA_ADDR not expected for submap.");
3015 object
= vm_object_enter(named_entry
->backing
.pager
,
3017 named_entry
->internal
,
3020 if (object
== VM_OBJECT_NULL
) {
3021 named_entry_unlock(named_entry
);
3022 return KERN_INVALID_OBJECT
;
3025 /* JMM - drop reference on pager here */
3027 /* create an extra ref for the named entry */
3028 vm_object_lock(object
);
3029 vm_object_reference_locked(object
);
3030 named_entry
->backing
.object
= object
;
3031 named_entry
->is_pager
= FALSE
;
3032 named_entry_unlock(named_entry
);
3034 wimg_mode
= object
->wimg_bits
;
3036 if (access
== MAP_MEM_IO
) {
3037 wimg_mode
= VM_WIMG_IO
;
3038 } else if (access
== MAP_MEM_COPYBACK
) {
3039 wimg_mode
= VM_WIMG_USE_DEFAULT
;
3040 } else if (access
== MAP_MEM_INNERWBACK
) {
3041 wimg_mode
= VM_WIMG_INNERWBACK
;
3042 } else if (access
== MAP_MEM_WTHRU
) {
3043 wimg_mode
= VM_WIMG_WTHRU
;
3044 } else if (access
== MAP_MEM_WCOMB
) {
3045 wimg_mode
= VM_WIMG_WCOMB
;
3048 /* wait for object (if any) to be ready */
3049 if (!named_entry
->internal
) {
3050 while (!object
->pager_ready
) {
3053 VM_OBJECT_EVENT_PAGER_READY
,
3055 vm_object_lock(object
);
3059 if (object
->wimg_bits
!= wimg_mode
)
3060 vm_object_change_wimg_mode(object
, wimg_mode
);
3062 #if VM_OBJECT_TRACKING_OP_TRUESHARE
3063 if (!object
->true_share
&&
3064 vm_object_tracking_inited
) {
3065 void *bt
[VM_OBJECT_TRACKING_BTDEPTH
];
3068 num
= OSBacktrace(bt
,
3069 VM_OBJECT_TRACKING_BTDEPTH
);
3070 btlog_add_entry(vm_object_tracking_btlog
,
3072 VM_OBJECT_TRACKING_OP_TRUESHARE
,
3076 #endif /* VM_OBJECT_TRACKING_OP_TRUESHARE */
3078 object
->true_share
= TRUE
;
3080 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
)
3081 object
->copy_strategy
= MEMORY_OBJECT_COPY_DELAY
;
3082 vm_object_unlock(object
);
3084 } else if (named_entry
->is_copy
) {
3086 vm_map_copy_t copy_map
;
3087 vm_map_entry_t copy_entry
;
3088 vm_map_offset_t copy_addr
;
3090 if (flags
& ~(VM_FLAGS_FIXED
|
3092 VM_FLAGS_OVERWRITE
|
3093 VM_FLAGS_RETURN_4K_DATA_ADDR
|
3094 VM_FLAGS_RETURN_DATA_ADDR
)) {
3095 named_entry_unlock(named_entry
);
3096 return KERN_INVALID_ARGUMENT
;
3099 if (flags
& (VM_FLAGS_RETURN_DATA_ADDR
|
3100 VM_FLAGS_RETURN_4K_DATA_ADDR
)) {
3101 offset_in_mapping
= offset
- vm_object_trunc_page(offset
);
3102 if (flags
& VM_FLAGS_RETURN_4K_DATA_ADDR
)
3103 offset_in_mapping
&= ~((signed)(0xFFF));
3104 offset
= vm_object_trunc_page(offset
);
3105 map_size
= vm_object_round_page(offset
+ offset_in_mapping
+ initial_size
) - offset
;
3108 copy_map
= named_entry
->backing
.copy
;
3109 assert(copy_map
->type
== VM_MAP_COPY_ENTRY_LIST
);
3110 if (copy_map
->type
!= VM_MAP_COPY_ENTRY_LIST
) {
3111 /* unsupported type; should not happen */
3112 printf("vm_map_enter_mem_object: "
3113 "memory_entry->backing.copy "
3114 "unsupported type 0x%x\n",
3116 named_entry_unlock(named_entry
);
3117 return KERN_INVALID_ARGUMENT
;
3120 /* reserve a contiguous range */
3121 kr
= vm_map_enter(target_map
,
3123 /* map whole mem entry, trim later: */
3126 flags
& (VM_FLAGS_ANYWHERE
|
3127 VM_FLAGS_OVERWRITE
|
3128 VM_FLAGS_RETURN_4K_DATA_ADDR
|
3129 VM_FLAGS_RETURN_DATA_ADDR
),
3136 if (kr
!= KERN_SUCCESS
) {
3137 named_entry_unlock(named_entry
);
3141 copy_addr
= map_addr
;
3143 for (copy_entry
= vm_map_copy_first_entry(copy_map
);
3144 copy_entry
!= vm_map_copy_to_entry(copy_map
);
3145 copy_entry
= copy_entry
->vme_next
) {
3146 int remap_flags
= 0;
3147 vm_map_t copy_submap
;
3148 vm_object_t copy_object
;
3149 vm_map_size_t copy_size
;
3150 vm_object_offset_t copy_offset
;
3152 copy_offset
= VME_OFFSET(copy_entry
);
3153 copy_size
= (copy_entry
->vme_end
-
3154 copy_entry
->vme_start
);
3157 if ((copy_addr
+ copy_size
) >
3159 named_entry
->size
/* XXX full size */ )) {
3160 /* over-mapping too much !? */
3161 kr
= KERN_INVALID_ARGUMENT
;
3166 /* take a reference on the object */
3167 if (copy_entry
->is_sub_map
) {
3168 remap_flags
|= VM_FLAGS_SUBMAP
;
3169 copy_submap
= VME_SUBMAP(copy_entry
);
3170 vm_map_lock(copy_submap
);
3171 vm_map_reference(copy_submap
);
3172 vm_map_unlock(copy_submap
);
3173 copy_object
= (vm_object_t
) copy_submap
;
3175 copy_object
= VME_OBJECT(copy_entry
);
3176 vm_object_reference(copy_object
);
3179 /* over-map the object into destination */
3180 remap_flags
|= flags
;
3181 remap_flags
|= VM_FLAGS_FIXED
;
3182 remap_flags
|= VM_FLAGS_OVERWRITE
;
3183 remap_flags
&= ~VM_FLAGS_ANYWHERE
;
3184 kr
= vm_map_enter(target_map
,
3187 (vm_map_offset_t
) 0,
3195 if (kr
!= KERN_SUCCESS
) {
3196 if (copy_entry
->is_sub_map
) {
3197 vm_map_deallocate(copy_submap
);
3199 vm_object_deallocate(copy_object
);
3206 copy_addr
+= copy_size
;
3209 if (kr
== KERN_SUCCESS
) {
3210 if (flags
& (VM_FLAGS_RETURN_DATA_ADDR
|
3211 VM_FLAGS_RETURN_4K_DATA_ADDR
)) {
3212 *address
= map_addr
+ offset_in_mapping
;
3214 *address
= map_addr
;
3219 * Trim in front, from 0 to "offset".
3221 vm_map_remove(target_map
,
3227 if (offset
+ map_size
< named_entry
->size
) {
3229 * Trim in back, from
3230 * "offset + map_size" to
3231 * "named_entry->size".
3233 vm_map_remove(target_map
,
3241 named_entry_unlock(named_entry
);
3243 if (kr
!= KERN_SUCCESS
) {
3244 if (! (flags
& VM_FLAGS_OVERWRITE
)) {
3245 /* deallocate the contiguous range */
3246 (void) vm_deallocate(target_map
,
3255 /* This is the case where we are going to map */
3256 /* an already mapped object. If the object is */
3257 /* not ready it is internal. An external */
3258 /* object cannot be mapped until it is ready */
3259 /* we can therefore avoid the ready check */
3261 if (flags
& (VM_FLAGS_RETURN_DATA_ADDR
|
3262 VM_FLAGS_RETURN_4K_DATA_ADDR
)) {
3263 offset_in_mapping
= offset
- vm_object_trunc_page(offset
);
3264 if (flags
& VM_FLAGS_RETURN_4K_DATA_ADDR
)
3265 offset_in_mapping
&= ~((signed)(0xFFF));
3266 offset
= vm_object_trunc_page(offset
);
3267 map_size
= vm_object_round_page(offset
+ offset_in_mapping
+ initial_size
) - offset
;
3270 object
= named_entry
->backing
.object
;
3271 assert(object
!= VM_OBJECT_NULL
);
3272 named_entry_unlock(named_entry
);
3273 vm_object_reference(object
);
3275 } else if (ip_kotype(port
) == IKOT_MEMORY_OBJECT
) {
3277 * JMM - This is temporary until we unify named entries
3278 * and raw memory objects.
3280 * Detected fake ip_kotype for a memory object. In
3281 * this case, the port isn't really a port at all, but
3282 * instead is just a raw memory object.
3284 if (flags
& (VM_FLAGS_RETURN_DATA_ADDR
|
3285 VM_FLAGS_RETURN_4K_DATA_ADDR
)) {
3286 panic("VM_FLAGS_RETURN_DATA_ADDR not expected for raw memory object.");
3289 object
= vm_object_enter((memory_object_t
)port
,
3290 size
, FALSE
, FALSE
, FALSE
);
3291 if (object
== VM_OBJECT_NULL
)
3292 return KERN_INVALID_OBJECT
;
3294 /* wait for object (if any) to be ready */
3295 if (object
!= VM_OBJECT_NULL
) {
3296 if (object
== kernel_object
) {
3297 printf("Warning: Attempt to map kernel object"
3298 " by a non-private kernel entity\n");
3299 return KERN_INVALID_OBJECT
;
3301 if (!object
->pager_ready
) {
3302 vm_object_lock(object
);
3304 while (!object
->pager_ready
) {
3305 vm_object_wait(object
,
3306 VM_OBJECT_EVENT_PAGER_READY
,
3308 vm_object_lock(object
);
3310 vm_object_unlock(object
);
3314 return KERN_INVALID_OBJECT
;
3317 if (object
!= VM_OBJECT_NULL
&&
3319 object
->pager
!= MEMORY_OBJECT_NULL
&&
3320 object
->copy_strategy
!= MEMORY_OBJECT_COPY_NONE
) {
3321 memory_object_t pager
;
3322 vm_prot_t pager_prot
;
3326 * For "named" VM objects, let the pager know that the
3327 * memory object is being mapped. Some pagers need to keep
3328 * track of this, to know when they can reclaim the memory
3329 * object, for example.
3330 * VM calls memory_object_map() for each mapping (specifying
3331 * the protection of each mapping) and calls
3332 * memory_object_last_unmap() when all the mappings are gone.
3334 pager_prot
= max_protection
;
3337 * Copy-On-Write mapping: won't modify the
3340 pager_prot
&= ~VM_PROT_WRITE
;
3342 vm_object_lock(object
);
3343 pager
= object
->pager
;
3344 if (object
->named
&&
3345 pager
!= MEMORY_OBJECT_NULL
&&
3346 object
->copy_strategy
!= MEMORY_OBJECT_COPY_NONE
) {
3347 assert(object
->pager_ready
);
3348 vm_object_mapping_wait(object
, THREAD_UNINT
);
3349 vm_object_mapping_begin(object
);
3350 vm_object_unlock(object
);
3352 kr
= memory_object_map(pager
, pager_prot
);
3353 assert(kr
== KERN_SUCCESS
);
3355 vm_object_lock(object
);
3356 vm_object_mapping_end(object
);
3358 vm_object_unlock(object
);
3362 * Perform the copy if requested
3366 vm_object_t new_object
;
3367 vm_object_offset_t new_offset
;
3369 result
= vm_object_copy_strategically(object
, offset
,
3371 &new_object
, &new_offset
,
3375 if (result
== KERN_MEMORY_RESTART_COPY
) {
3377 boolean_t src_needs_copy
;
3381 * We currently ignore src_needs_copy.
3382 * This really is the issue of how to make
3383 * MEMORY_OBJECT_COPY_SYMMETRIC safe for
3384 * non-kernel users to use. Solution forthcoming.
3385 * In the meantime, since we don't allow non-kernel
3386 * memory managers to specify symmetric copy,
3387 * we won't run into problems here.
3389 new_object
= object
;
3390 new_offset
= offset
;
3391 success
= vm_object_copy_quickly(&new_object
,
3397 result
= KERN_SUCCESS
;
3400 * Throw away the reference to the
3401 * original object, as it won't be mapped.
3404 vm_object_deallocate(object
);
3406 if (result
!= KERN_SUCCESS
) {
3410 object
= new_object
;
3411 offset
= new_offset
;
3415 * If users want to try to prefault pages, the mapping and prefault
3416 * needs to be atomic.
3419 flags
|= VM_FLAGS_KEEP_MAP_LOCKED
;
3422 result
= vm_map_enter(target_map
,
3423 &map_addr
, map_size
,
3424 (vm_map_offset_t
)mask
,
3428 cur_protection
, max_protection
,
3431 if (result
!= KERN_SUCCESS
)
3432 vm_object_deallocate(object
);
3435 * Try to prefault, and do not forget to release the vm map lock.
3437 if (result
== KERN_SUCCESS
&& try_prefault
) {
3438 mach_vm_address_t va
= map_addr
;
3439 kern_return_t kr
= KERN_SUCCESS
;
3442 for (i
= 0; i
< page_list_count
; ++i
) {
3443 if (UPL_VALID_PAGE(page_list
, i
)) {
3445 * If this function call failed, we should stop
3446 * trying to optimize, other calls are likely
3447 * going to fail too.
3449 * We are not gonna report an error for such
3450 * failure though. That's an optimization, not
3451 * something critical.
3453 kr
= pmap_enter_options(target_map
->pmap
,
3454 va
, UPL_PHYS_PAGE(page_list
, i
),
3455 cur_protection
, VM_PROT_NONE
,
3456 0, TRUE
, PMAP_OPTIONS_NOWAIT
, NULL
);
3457 if (kr
!= KERN_SUCCESS
) {
3458 OSIncrementAtomic64(&vm_prefault_nb_bailout
);
3461 OSIncrementAtomic64(&vm_prefault_nb_pages
);
3464 /* Next virtual address */
3467 vm_map_unlock(target_map
);
3470 if (flags
& (VM_FLAGS_RETURN_DATA_ADDR
|
3471 VM_FLAGS_RETURN_4K_DATA_ADDR
)) {
3472 *address
= map_addr
+ offset_in_mapping
;
3474 *address
= map_addr
;
3480 vm_map_enter_mem_object(
3481 vm_map_t target_map
,
3482 vm_map_offset_t
*address
,
3483 vm_map_size_t initial_size
,
3484 vm_map_offset_t mask
,
3487 vm_object_offset_t offset
,
3489 vm_prot_t cur_protection
,
3490 vm_prot_t max_protection
,
3491 vm_inherit_t inheritance
)
3493 return vm_map_enter_mem_object_helper(target_map
, address
, initial_size
, mask
, flags
,
3494 port
, offset
, copy
, cur_protection
, max_protection
,
3495 inheritance
, NULL
, 0);
3499 vm_map_enter_mem_object_prefault(
3500 vm_map_t target_map
,
3501 vm_map_offset_t
*address
,
3502 vm_map_size_t initial_size
,
3503 vm_map_offset_t mask
,
3506 vm_object_offset_t offset
,
3507 vm_prot_t cur_protection
,
3508 vm_prot_t max_protection
,
3509 upl_page_list_ptr_t page_list
,
3510 unsigned int page_list_count
)
3512 return vm_map_enter_mem_object_helper(target_map
, address
, initial_size
, mask
, flags
,
3513 port
, offset
, FALSE
, cur_protection
, max_protection
,
3514 VM_INHERIT_DEFAULT
, page_list
, page_list_count
);
3519 vm_map_enter_mem_object_control(
3520 vm_map_t target_map
,
3521 vm_map_offset_t
*address
,
3522 vm_map_size_t initial_size
,
3523 vm_map_offset_t mask
,
3525 memory_object_control_t control
,
3526 vm_object_offset_t offset
,
3528 vm_prot_t cur_protection
,
3529 vm_prot_t max_protection
,
3530 vm_inherit_t inheritance
)
3532 vm_map_address_t map_addr
;
3533 vm_map_size_t map_size
;
3535 vm_object_size_t size
;
3536 kern_return_t result
;
3537 memory_object_t pager
;
3538 vm_prot_t pager_prot
;
3542 * Check arguments for validity
3544 if ((target_map
== VM_MAP_NULL
) ||
3545 (cur_protection
& ~VM_PROT_ALL
) ||
3546 (max_protection
& ~VM_PROT_ALL
) ||
3547 (inheritance
> VM_INHERIT_LAST_VALID
) ||
3548 initial_size
== 0) {
3549 return KERN_INVALID_ARGUMENT
;
3553 map_addr
= vm_map_trunc_page(*address
,
3554 VM_MAP_PAGE_MASK(target_map
));
3555 map_size
= vm_map_round_page(initial_size
,
3556 VM_MAP_PAGE_MASK(target_map
));
3558 size
= vm_object_round_page(initial_size
);
3560 object
= memory_object_control_to_vm_object(control
);
3562 if (object
== VM_OBJECT_NULL
)
3563 return KERN_INVALID_OBJECT
;
3565 if (object
== kernel_object
) {
3566 printf("Warning: Attempt to map kernel object"
3567 " by a non-private kernel entity\n");
3568 return KERN_INVALID_OBJECT
;
3571 vm_object_lock(object
);
3572 object
->ref_count
++;
3573 vm_object_res_reference(object
);
3576 * For "named" VM objects, let the pager know that the
3577 * memory object is being mapped. Some pagers need to keep
3578 * track of this, to know when they can reclaim the memory
3579 * object, for example.
3580 * VM calls memory_object_map() for each mapping (specifying
3581 * the protection of each mapping) and calls
3582 * memory_object_last_unmap() when all the mappings are gone.
3584 pager_prot
= max_protection
;
3586 pager_prot
&= ~VM_PROT_WRITE
;
3588 pager
= object
->pager
;
3589 if (object
->named
&&
3590 pager
!= MEMORY_OBJECT_NULL
&&
3591 object
->copy_strategy
!= MEMORY_OBJECT_COPY_NONE
) {
3592 assert(object
->pager_ready
);
3593 vm_object_mapping_wait(object
, THREAD_UNINT
);
3594 vm_object_mapping_begin(object
);
3595 vm_object_unlock(object
);
3597 kr
= memory_object_map(pager
, pager_prot
);
3598 assert(kr
== KERN_SUCCESS
);
3600 vm_object_lock(object
);
3601 vm_object_mapping_end(object
);
3603 vm_object_unlock(object
);
3606 * Perform the copy if requested
3610 vm_object_t new_object
;
3611 vm_object_offset_t new_offset
;
3613 result
= vm_object_copy_strategically(object
, offset
, size
,
3614 &new_object
, &new_offset
,
3618 if (result
== KERN_MEMORY_RESTART_COPY
) {
3620 boolean_t src_needs_copy
;
3624 * We currently ignore src_needs_copy.
3625 * This really is the issue of how to make
3626 * MEMORY_OBJECT_COPY_SYMMETRIC safe for
3627 * non-kernel users to use. Solution forthcoming.
3628 * In the meantime, since we don't allow non-kernel
3629 * memory managers to specify symmetric copy,
3630 * we won't run into problems here.
3632 new_object
= object
;
3633 new_offset
= offset
;
3634 success
= vm_object_copy_quickly(&new_object
,
3639 result
= KERN_SUCCESS
;
3642 * Throw away the reference to the
3643 * original object, as it won't be mapped.
3646 vm_object_deallocate(object
);
3648 if (result
!= KERN_SUCCESS
) {
3652 object
= new_object
;
3653 offset
= new_offset
;
3657 result
= vm_map_enter(target_map
,
3658 &map_addr
, map_size
,
3659 (vm_map_offset_t
)mask
,
3663 cur_protection
, max_protection
,
3666 if (result
!= KERN_SUCCESS
)
3667 vm_object_deallocate(object
);
3668 *address
= map_addr
;
3677 extern pmap_paddr_t avail_start
, avail_end
;
3681 * Allocate memory in the specified map, with the caveat that
3682 * the memory is physically contiguous. This call may fail
3683 * if the system can't find sufficient contiguous memory.
3684 * This call may cause or lead to heart-stopping amounts of
3687 * Memory obtained from this call should be freed in the
3688 * normal way, viz., via vm_deallocate.
3693 vm_map_offset_t
*addr
,
3697 vm_object_t cpm_obj
;
3701 vm_map_offset_t va
, start
, end
, offset
;
3703 vm_map_offset_t prev_addr
= 0;
3704 #endif /* MACH_ASSERT */
3706 boolean_t anywhere
= ((VM_FLAGS_ANYWHERE
& flags
) != 0);
3709 VM_GET_FLAGS_ALIAS(flags
, tag
);
3713 return KERN_SUCCESS
;
3716 *addr
= vm_map_min(map
);
3718 *addr
= vm_map_trunc_page(*addr
,
3719 VM_MAP_PAGE_MASK(map
));
3720 size
= vm_map_round_page(size
,
3721 VM_MAP_PAGE_MASK(map
));
3724 * LP64todo - cpm_allocate should probably allow
3725 * allocations of >4GB, but not with the current
3726 * algorithm, so just cast down the size for now.
3728 if (size
> VM_MAX_ADDRESS
)
3729 return KERN_RESOURCE_SHORTAGE
;
3730 if ((kr
= cpm_allocate(CAST_DOWN(vm_size_t
, size
),
3731 &pages
, 0, 0, TRUE
, flags
)) != KERN_SUCCESS
)
3734 cpm_obj
= vm_object_allocate((vm_object_size_t
)size
);
3735 assert(cpm_obj
!= VM_OBJECT_NULL
);
3736 assert(cpm_obj
->internal
);
3737 assert(cpm_obj
->vo_size
== (vm_object_size_t
)size
);
3738 assert(cpm_obj
->can_persist
== FALSE
);
3739 assert(cpm_obj
->pager_created
== FALSE
);
3740 assert(cpm_obj
->pageout
== FALSE
);
3741 assert(cpm_obj
->shadow
== VM_OBJECT_NULL
);
3744 * Insert pages into object.
3747 vm_object_lock(cpm_obj
);
3748 for (offset
= 0; offset
< size
; offset
+= PAGE_SIZE
) {
3750 pages
= NEXT_PAGE(m
);
3751 *(NEXT_PAGE_PTR(m
)) = VM_PAGE_NULL
;
3753 assert(!m
->gobbled
);
3755 assert(!m
->pageout
);
3757 assert(VM_PAGE_WIRED(m
));
3760 * "m" is not supposed to be pageable, so it
3761 * should not be encrypted. It wouldn't be safe
3762 * to enter it in a new VM object while encrypted.
3764 ASSERT_PAGE_DECRYPTED(m
);
3766 assert(m
->phys_page
>=(avail_start
>>PAGE_SHIFT
) && m
->phys_page
<=(avail_end
>>PAGE_SHIFT
));
3769 vm_page_insert(m
, cpm_obj
, offset
);
3771 assert(cpm_obj
->resident_page_count
== size
/ PAGE_SIZE
);
3772 vm_object_unlock(cpm_obj
);
3775 * Hang onto a reference on the object in case a
3776 * multi-threaded application for some reason decides
3777 * to deallocate the portion of the address space into
3778 * which we will insert this object.
3780 * Unfortunately, we must insert the object now before
3781 * we can talk to the pmap module about which addresses
3782 * must be wired down. Hence, the race with a multi-
3785 vm_object_reference(cpm_obj
);
3788 * Insert object into map.
3798 (vm_object_offset_t
)0,
3802 VM_INHERIT_DEFAULT
);
3804 if (kr
!= KERN_SUCCESS
) {
3806 * A CPM object doesn't have can_persist set,
3807 * so all we have to do is deallocate it to
3808 * free up these pages.
3810 assert(cpm_obj
->pager_created
== FALSE
);
3811 assert(cpm_obj
->can_persist
== FALSE
);
3812 assert(cpm_obj
->pageout
== FALSE
);
3813 assert(cpm_obj
->shadow
== VM_OBJECT_NULL
);
3814 vm_object_deallocate(cpm_obj
); /* kill acquired ref */
3815 vm_object_deallocate(cpm_obj
); /* kill creation ref */
3819 * Inform the physical mapping system that the
3820 * range of addresses may not fault, so that
3821 * page tables and such can be locked down as well.
3825 pmap
= vm_map_pmap(map
);
3826 pmap_pageable(pmap
, start
, end
, FALSE
);
3829 * Enter each page into the pmap, to avoid faults.
3830 * Note that this loop could be coded more efficiently,
3831 * if the need arose, rather than looking up each page
3834 for (offset
= 0, va
= start
; offset
< size
;
3835 va
+= PAGE_SIZE
, offset
+= PAGE_SIZE
) {
3838 vm_object_lock(cpm_obj
);
3839 m
= vm_page_lookup(cpm_obj
, (vm_object_offset_t
)offset
);
3840 assert(m
!= VM_PAGE_NULL
);
3842 vm_page_zero_fill(m
);
3844 type_of_fault
= DBG_ZERO_FILL_FAULT
;
3846 vm_fault_enter(m
, pmap
, va
, VM_PROT_ALL
, VM_PROT_WRITE
,
3847 VM_PAGE_WIRED(m
), FALSE
, FALSE
, FALSE
, 0, NULL
,
3850 vm_object_unlock(cpm_obj
);
3855 * Verify ordering in address space.
3857 for (offset
= 0; offset
< size
; offset
+= PAGE_SIZE
) {
3858 vm_object_lock(cpm_obj
);
3859 m
= vm_page_lookup(cpm_obj
, (vm_object_offset_t
)offset
);
3860 vm_object_unlock(cpm_obj
);
3861 if (m
== VM_PAGE_NULL
)
3862 panic("vm_allocate_cpm: obj %p off 0x%llx no page",
3863 cpm_obj
, (uint64_t)offset
);
3867 assert(!m
->fictitious
);
3868 assert(!m
->private);
3871 assert(!m
->cleaning
);
3872 assert(!m
->laundry
);
3873 assert(!m
->precious
);
3874 assert(!m
->clustered
);
3876 if (m
->phys_page
!= prev_addr
+ 1) {
3877 printf("start 0x%llx end 0x%llx va 0x%llx\n",
3878 (uint64_t)start
, (uint64_t)end
, (uint64_t)va
);
3879 printf("obj %p off 0x%llx\n", cpm_obj
, (uint64_t)offset
);
3880 printf("m %p prev_address 0x%llx\n", m
, (uint64_t)prev_addr
);
3881 panic("vm_allocate_cpm: pages not contig!");
3884 prev_addr
= m
->phys_page
;
3886 #endif /* MACH_ASSERT */
3888 vm_object_deallocate(cpm_obj
); /* kill extra ref */
3897 * Interface is defined in all cases, but unless the kernel
3898 * is built explicitly for this option, the interface does
3904 __unused vm_map_t map
,
3905 __unused vm_map_offset_t
*addr
,
3906 __unused vm_map_size_t size
,
3909 return KERN_FAILURE
;
3913 /* Not used without nested pmaps */
3914 #ifndef NO_NESTED_PMAP
3916 * Clip and unnest a portion of a nested submap mapping.
3923 vm_map_entry_t entry
,
3924 vm_map_offset_t start_unnest
,
3925 vm_map_offset_t end_unnest
)
3927 vm_map_offset_t old_start_unnest
= start_unnest
;
3928 vm_map_offset_t old_end_unnest
= end_unnest
;
3930 assert(entry
->is_sub_map
);
3931 assert(VME_SUBMAP(entry
) != NULL
);
3932 assert(entry
->use_pmap
);
3935 * Query the platform for the optimal unnest range.
3936 * DRK: There's some duplication of effort here, since
3937 * callers may have adjusted the range to some extent. This
3938 * routine was introduced to support 1GiB subtree nesting
3939 * for x86 platforms, which can also nest on 2MiB boundaries
3940 * depending on size/alignment.
3942 if (pmap_adjust_unnest_parameters(map
->pmap
, &start_unnest
, &end_unnest
)) {
3943 log_unnest_badness(map
, old_start_unnest
, old_end_unnest
);
3946 if (entry
->vme_start
> start_unnest
||
3947 entry
->vme_end
< end_unnest
) {
3948 panic("vm_map_clip_unnest(0x%llx,0x%llx): "
3949 "bad nested entry: start=0x%llx end=0x%llx\n",
3950 (long long)start_unnest
, (long long)end_unnest
,
3951 (long long)entry
->vme_start
, (long long)entry
->vme_end
);
3954 if (start_unnest
> entry
->vme_start
) {
3955 _vm_map_clip_start(&map
->hdr
,
3958 if (map
->holelistenabled
) {
3959 vm_map_store_update_first_free(map
, NULL
, FALSE
);
3961 vm_map_store_update_first_free(map
, map
->first_free
, FALSE
);
3964 if (entry
->vme_end
> end_unnest
) {
3965 _vm_map_clip_end(&map
->hdr
,
3968 if (map
->holelistenabled
) {
3969 vm_map_store_update_first_free(map
, NULL
, FALSE
);
3971 vm_map_store_update_first_free(map
, map
->first_free
, FALSE
);
3975 pmap_unnest(map
->pmap
,
3977 entry
->vme_end
- entry
->vme_start
);
3978 if ((map
->mapped_in_other_pmaps
) && (map
->ref_count
)) {
3979 /* clean up parent map/maps */
3980 vm_map_submap_pmap_clean(
3981 map
, entry
->vme_start
,
3986 entry
->use_pmap
= FALSE
;
3987 if ((map
->pmap
!= kernel_pmap
) &&
3988 (VME_ALIAS(entry
) == VM_MEMORY_SHARED_PMAP
)) {
3989 VME_ALIAS_SET(entry
, VM_MEMORY_UNSHARED_PMAP
);
3992 #endif /* NO_NESTED_PMAP */
3995 * vm_map_clip_start: [ internal use only ]
3997 * Asserts that the given entry begins at or after
3998 * the specified address; if necessary,
3999 * it splits the entry into two.
4004 vm_map_entry_t entry
,
4005 vm_map_offset_t startaddr
)
4007 #ifndef NO_NESTED_PMAP
4008 if (entry
->is_sub_map
&&
4010 startaddr
>= entry
->vme_start
) {
4011 vm_map_offset_t start_unnest
, end_unnest
;
4014 * Make sure "startaddr" is no longer in a nested range
4015 * before we clip. Unnest only the minimum range the platform
4017 * vm_map_clip_unnest may perform additional adjustments to
4020 start_unnest
= startaddr
& ~(pmap_nesting_size_min
- 1);
4021 end_unnest
= start_unnest
+ pmap_nesting_size_min
;
4022 vm_map_clip_unnest(map
, entry
, start_unnest
, end_unnest
);
4024 #endif /* NO_NESTED_PMAP */
4025 if (startaddr
> entry
->vme_start
) {
4026 if (VME_OBJECT(entry
) &&
4027 !entry
->is_sub_map
&&
4028 VME_OBJECT(entry
)->phys_contiguous
) {
4029 pmap_remove(map
->pmap
,
4030 (addr64_t
)(entry
->vme_start
),
4031 (addr64_t
)(entry
->vme_end
));
4033 _vm_map_clip_start(&map
->hdr
, entry
, startaddr
);
4034 if (map
->holelistenabled
) {
4035 vm_map_store_update_first_free(map
, NULL
, FALSE
);
4037 vm_map_store_update_first_free(map
, map
->first_free
, FALSE
);
4043 #define vm_map_copy_clip_start(copy, entry, startaddr) \
4045 if ((startaddr) > (entry)->vme_start) \
4046 _vm_map_clip_start(&(copy)->cpy_hdr,(entry),(startaddr)); \
4050 * This routine is called only when it is known that
4051 * the entry must be split.
4055 register struct vm_map_header
*map_header
,
4056 register vm_map_entry_t entry
,
4057 register vm_map_offset_t start
)
4059 register vm_map_entry_t new_entry
;
4062 * Split off the front portion --
4063 * note that we must insert the new
4064 * entry BEFORE this one, so that
4065 * this entry has the specified starting
4069 if (entry
->map_aligned
) {
4070 assert(VM_MAP_PAGE_ALIGNED(start
,
4071 VM_MAP_HDR_PAGE_MASK(map_header
)));
4074 new_entry
= _vm_map_entry_create(map_header
, !map_header
->entries_pageable
);
4075 vm_map_entry_copy_full(new_entry
, entry
);
4077 new_entry
->vme_end
= start
;
4078 assert(new_entry
->vme_start
< new_entry
->vme_end
);
4079 VME_OFFSET_SET(entry
, VME_OFFSET(entry
) + (start
- entry
->vme_start
));
4080 assert(start
< entry
->vme_end
);
4081 entry
->vme_start
= start
;
4083 _vm_map_store_entry_link(map_header
, entry
->vme_prev
, new_entry
);
4085 if (entry
->is_sub_map
)
4086 vm_map_reference(VME_SUBMAP(new_entry
));
4088 vm_object_reference(VME_OBJECT(new_entry
));
4093 * vm_map_clip_end: [ internal use only ]
4095 * Asserts that the given entry ends at or before
4096 * the specified address; if necessary,
4097 * it splits the entry into two.
4102 vm_map_entry_t entry
,
4103 vm_map_offset_t endaddr
)
4105 if (endaddr
> entry
->vme_end
) {
4107 * Within the scope of this clipping, limit "endaddr" to
4108 * the end of this map entry...
4110 endaddr
= entry
->vme_end
;
4112 #ifndef NO_NESTED_PMAP
4113 if (entry
->is_sub_map
&& entry
->use_pmap
) {
4114 vm_map_offset_t start_unnest
, end_unnest
;
4117 * Make sure the range between the start of this entry and
4118 * the new "endaddr" is no longer nested before we clip.
4119 * Unnest only the minimum range the platform can handle.
4120 * vm_map_clip_unnest may perform additional adjustments to
4123 start_unnest
= entry
->vme_start
;
4125 (endaddr
+ pmap_nesting_size_min
- 1) &
4126 ~(pmap_nesting_size_min
- 1);
4127 vm_map_clip_unnest(map
, entry
, start_unnest
, end_unnest
);
4129 #endif /* NO_NESTED_PMAP */
4130 if (endaddr
< entry
->vme_end
) {
4131 if (VME_OBJECT(entry
) &&
4132 !entry
->is_sub_map
&&
4133 VME_OBJECT(entry
)->phys_contiguous
) {
4134 pmap_remove(map
->pmap
,
4135 (addr64_t
)(entry
->vme_start
),
4136 (addr64_t
)(entry
->vme_end
));
4138 _vm_map_clip_end(&map
->hdr
, entry
, endaddr
);
4139 if (map
->holelistenabled
) {
4140 vm_map_store_update_first_free(map
, NULL
, FALSE
);
4142 vm_map_store_update_first_free(map
, map
->first_free
, FALSE
);
4148 #define vm_map_copy_clip_end(copy, entry, endaddr) \
4150 if ((endaddr) < (entry)->vme_end) \
4151 _vm_map_clip_end(&(copy)->cpy_hdr,(entry),(endaddr)); \
4155 * This routine is called only when it is known that
4156 * the entry must be split.
4160 register struct vm_map_header
*map_header
,
4161 register vm_map_entry_t entry
,
4162 register vm_map_offset_t end
)
4164 register vm_map_entry_t new_entry
;
4167 * Create a new entry and insert it
4168 * AFTER the specified entry
4171 if (entry
->map_aligned
) {
4172 assert(VM_MAP_PAGE_ALIGNED(end
,
4173 VM_MAP_HDR_PAGE_MASK(map_header
)));
4176 new_entry
= _vm_map_entry_create(map_header
, !map_header
->entries_pageable
);
4177 vm_map_entry_copy_full(new_entry
, entry
);
4179 assert(entry
->vme_start
< end
);
4180 new_entry
->vme_start
= entry
->vme_end
= end
;
4181 VME_OFFSET_SET(new_entry
,
4182 VME_OFFSET(new_entry
) + (end
- entry
->vme_start
));
4183 assert(new_entry
->vme_start
< new_entry
->vme_end
);
4185 _vm_map_store_entry_link(map_header
, entry
, new_entry
);
4187 if (entry
->is_sub_map
)
4188 vm_map_reference(VME_SUBMAP(new_entry
));
4190 vm_object_reference(VME_OBJECT(new_entry
));
4195 * VM_MAP_RANGE_CHECK: [ internal use only ]
4197 * Asserts that the starting and ending region
4198 * addresses fall within the valid range of the map.
4200 #define VM_MAP_RANGE_CHECK(map, start, end) \
4202 if (start < vm_map_min(map)) \
4203 start = vm_map_min(map); \
4204 if (end > vm_map_max(map)) \
4205 end = vm_map_max(map); \
4211 * vm_map_range_check: [ internal use only ]
4213 * Check that the region defined by the specified start and
4214 * end addresses are wholly contained within a single map
4215 * entry or set of adjacent map entries of the spacified map,
4216 * i.e. the specified region contains no unmapped space.
4217 * If any or all of the region is unmapped, FALSE is returned.
4218 * Otherwise, TRUE is returned and if the output argument 'entry'
4219 * is not NULL it points to the map entry containing the start
4222 * The map is locked for reading on entry and is left locked.
4226 register vm_map_t map
,
4227 register vm_map_offset_t start
,
4228 register vm_map_offset_t end
,
4229 vm_map_entry_t
*entry
)
4232 register vm_map_offset_t prev
;
4235 * Basic sanity checks first
4237 if (start
< vm_map_min(map
) || end
> vm_map_max(map
) || start
> end
)
4241 * Check first if the region starts within a valid
4242 * mapping for the map.
4244 if (!vm_map_lookup_entry(map
, start
, &cur
))
4248 * Optimize for the case that the region is contained
4249 * in a single map entry.
4251 if (entry
!= (vm_map_entry_t
*) NULL
)
4253 if (end
<= cur
->vme_end
)
4257 * If the region is not wholly contained within a
4258 * single entry, walk the entries looking for holes.
4260 prev
= cur
->vme_end
;
4261 cur
= cur
->vme_next
;
4262 while ((cur
!= vm_map_to_entry(map
)) && (prev
== cur
->vme_start
)) {
4263 if (end
<= cur
->vme_end
)
4265 prev
= cur
->vme_end
;
4266 cur
= cur
->vme_next
;
4272 * vm_map_submap: [ kernel use only ]
4274 * Mark the given range as handled by a subordinate map.
4276 * This range must have been created with vm_map_find using
4277 * the vm_submap_object, and no other operations may have been
4278 * performed on this range prior to calling vm_map_submap.
4280 * Only a limited number of operations can be performed
4281 * within this rage after calling vm_map_submap:
4283 * [Don't try vm_map_copyin!]
4285 * To remove a submapping, one must first remove the
4286 * range from the superior map, and then destroy the
4287 * submap (if desired). [Better yet, don't try it.]
4292 vm_map_offset_t start
,
4293 vm_map_offset_t end
,
4295 vm_map_offset_t offset
,
4296 #ifdef NO_NESTED_PMAP
4298 #endif /* NO_NESTED_PMAP */
4301 vm_map_entry_t entry
;
4302 register kern_return_t result
= KERN_INVALID_ARGUMENT
;
4303 register vm_object_t object
;
4307 if (! vm_map_lookup_entry(map
, start
, &entry
)) {
4308 entry
= entry
->vme_next
;
4311 if (entry
== vm_map_to_entry(map
) ||
4312 entry
->is_sub_map
) {
4314 return KERN_INVALID_ARGUMENT
;
4317 vm_map_clip_start(map
, entry
, start
);
4318 vm_map_clip_end(map
, entry
, end
);
4320 if ((entry
->vme_start
== start
) && (entry
->vme_end
== end
) &&
4321 (!entry
->is_sub_map
) &&
4322 ((object
= VME_OBJECT(entry
)) == vm_submap_object
) &&
4323 (object
->resident_page_count
== 0) &&
4324 (object
->copy
== VM_OBJECT_NULL
) &&
4325 (object
->shadow
== VM_OBJECT_NULL
) &&
4326 (!object
->pager_created
)) {
4327 VME_OFFSET_SET(entry
, (vm_object_offset_t
)offset
);
4328 VME_OBJECT_SET(entry
, VM_OBJECT_NULL
);
4329 vm_object_deallocate(object
);
4330 entry
->is_sub_map
= TRUE
;
4331 entry
->use_pmap
= FALSE
;
4332 VME_SUBMAP_SET(entry
, submap
);
4333 vm_map_reference(submap
);
4334 if (submap
->mapped_in_other_pmaps
== FALSE
&&
4335 vm_map_pmap(submap
) != PMAP_NULL
&&
4336 vm_map_pmap(submap
) != vm_map_pmap(map
)) {
4338 * This submap is being mapped in a map
4339 * that uses a different pmap.
4340 * Set its "mapped_in_other_pmaps" flag
4341 * to indicate that we now need to
4342 * remove mappings from all pmaps rather
4343 * than just the submap's pmap.
4345 submap
->mapped_in_other_pmaps
= TRUE
;
4348 #ifndef NO_NESTED_PMAP
4350 /* nest if platform code will allow */
4351 if(submap
->pmap
== NULL
) {
4352 ledger_t ledger
= map
->pmap
->ledger
;
4353 submap
->pmap
= pmap_create(ledger
,
4354 (vm_map_size_t
) 0, FALSE
);
4355 if(submap
->pmap
== PMAP_NULL
) {
4357 return(KERN_NO_SPACE
);
4360 result
= pmap_nest(map
->pmap
,
4361 (VME_SUBMAP(entry
))->pmap
,
4364 (uint64_t)(end
- start
));
4366 panic("vm_map_submap: pmap_nest failed, rc = %08X\n", result
);
4367 entry
->use_pmap
= TRUE
;
4369 #else /* NO_NESTED_PMAP */
4370 pmap_remove(map
->pmap
, (addr64_t
)start
, (addr64_t
)end
);
4371 #endif /* NO_NESTED_PMAP */
4372 result
= KERN_SUCCESS
;
4382 * Sets the protection of the specified address
4383 * region in the target map. If "set_max" is
4384 * specified, the maximum protection is to be set;
4385 * otherwise, only the current protection is affected.
4389 register vm_map_t map
,
4390 register vm_map_offset_t start
,
4391 register vm_map_offset_t end
,
4392 register vm_prot_t new_prot
,
4393 register boolean_t set_max
)
4395 register vm_map_entry_t current
;
4396 register vm_map_offset_t prev
;
4397 vm_map_entry_t entry
;
4401 "vm_map_protect, 0x%X start 0x%X end 0x%X, new 0x%X %d",
4402 map
, start
, end
, new_prot
, set_max
);
4406 /* LP64todo - remove this check when vm_map_commpage64()
4407 * no longer has to stuff in a map_entry for the commpage
4408 * above the map's max_offset.
4410 if (start
>= map
->max_offset
) {
4412 return(KERN_INVALID_ADDRESS
);
4417 * Lookup the entry. If it doesn't start in a valid
4418 * entry, return an error.
4420 if (! vm_map_lookup_entry(map
, start
, &entry
)) {
4422 return(KERN_INVALID_ADDRESS
);
4425 if (entry
->superpage_size
&& (start
& (SUPERPAGE_SIZE
-1))) { /* extend request to whole entry */
4426 start
= SUPERPAGE_ROUND_DOWN(start
);
4431 if (entry
->superpage_size
)
4432 end
= SUPERPAGE_ROUND_UP(end
);
4435 * Make a first pass to check for protection and address
4440 prev
= current
->vme_start
;
4441 while ((current
!= vm_map_to_entry(map
)) &&
4442 (current
->vme_start
< end
)) {
4445 * If there is a hole, return an error.
4447 if (current
->vme_start
!= prev
) {
4449 return(KERN_INVALID_ADDRESS
);
4452 new_max
= current
->max_protection
;
4453 if(new_prot
& VM_PROT_COPY
) {
4454 new_max
|= VM_PROT_WRITE
;
4455 if ((new_prot
& (new_max
| VM_PROT_COPY
)) != new_prot
) {
4457 return(KERN_PROTECTION_FAILURE
);
4460 if ((new_prot
& new_max
) != new_prot
) {
4462 return(KERN_PROTECTION_FAILURE
);
4467 prev
= current
->vme_end
;
4468 current
= current
->vme_next
;
4472 return(KERN_INVALID_ADDRESS
);
4476 * Go back and fix up protections.
4477 * Clip to start here if the range starts within
4482 if (current
!= vm_map_to_entry(map
)) {
4483 /* clip and unnest if necessary */
4484 vm_map_clip_start(map
, current
, start
);
4487 while ((current
!= vm_map_to_entry(map
)) &&
4488 (current
->vme_start
< end
)) {
4492 vm_map_clip_end(map
, current
, end
);
4494 if (current
->is_sub_map
) {
4495 /* clipping did unnest if needed */
4496 assert(!current
->use_pmap
);
4499 old_prot
= current
->protection
;
4501 if(new_prot
& VM_PROT_COPY
) {
4502 /* caller is asking specifically to copy the */
4503 /* mapped data, this implies that max protection */
4504 /* will include write. Caller must be prepared */
4505 /* for loss of shared memory communication in the */
4506 /* target area after taking this step */
4508 if (current
->is_sub_map
== FALSE
&&
4509 VME_OBJECT(current
) == VM_OBJECT_NULL
) {
4510 VME_OBJECT_SET(current
,
4514 current
->vme_start
)));
4515 VME_OFFSET_SET(current
, 0);
4516 assert(current
->use_pmap
);
4518 assert(current
->wired_count
== 0);
4519 current
->needs_copy
= TRUE
;
4520 current
->max_protection
|= VM_PROT_WRITE
;
4524 current
->protection
=
4525 (current
->max_protection
=
4526 new_prot
& ~VM_PROT_COPY
) &
4529 current
->protection
= new_prot
& ~VM_PROT_COPY
;
4532 * Update physical map if necessary.
4533 * If the request is to turn off write protection,
4534 * we won't do it for real (in pmap). This is because
4535 * it would cause copy-on-write to fail. We've already
4536 * set, the new protection in the map, so if a
4537 * write-protect fault occurred, it will be fixed up
4538 * properly, COW or not.
4540 if (current
->protection
!= old_prot
) {
4541 /* Look one level in we support nested pmaps */
4542 /* from mapped submaps which are direct entries */
4547 prot
= current
->protection
& ~VM_PROT_WRITE
;
4549 if (override_nx(map
, VME_ALIAS(current
)) && prot
)
4550 prot
|= VM_PROT_EXECUTE
;
4552 if (current
->is_sub_map
&& current
->use_pmap
) {
4553 pmap_protect(VME_SUBMAP(current
)->pmap
,
4558 pmap_protect(map
->pmap
,
4564 current
= current
->vme_next
;
4568 while ((current
!= vm_map_to_entry(map
)) &&
4569 (current
->vme_start
<= end
)) {
4570 vm_map_simplify_entry(map
, current
);
4571 current
= current
->vme_next
;
4575 return(KERN_SUCCESS
);
4581 * Sets the inheritance of the specified address
4582 * range in the target map. Inheritance
4583 * affects how the map will be shared with
4584 * child maps at the time of vm_map_fork.
4588 register vm_map_t map
,
4589 register vm_map_offset_t start
,
4590 register vm_map_offset_t end
,
4591 register vm_inherit_t new_inheritance
)
4593 register vm_map_entry_t entry
;
4594 vm_map_entry_t temp_entry
;
4598 VM_MAP_RANGE_CHECK(map
, start
, end
);
4600 if (vm_map_lookup_entry(map
, start
, &temp_entry
)) {
4604 temp_entry
= temp_entry
->vme_next
;
4608 /* first check entire range for submaps which can't support the */
4609 /* given inheritance. */
4610 while ((entry
!= vm_map_to_entry(map
)) && (entry
->vme_start
< end
)) {
4611 if(entry
->is_sub_map
) {
4612 if(new_inheritance
== VM_INHERIT_COPY
) {
4614 return(KERN_INVALID_ARGUMENT
);
4618 entry
= entry
->vme_next
;
4622 if (entry
!= vm_map_to_entry(map
)) {
4623 /* clip and unnest if necessary */
4624 vm_map_clip_start(map
, entry
, start
);
4627 while ((entry
!= vm_map_to_entry(map
)) && (entry
->vme_start
< end
)) {
4628 vm_map_clip_end(map
, entry
, end
);
4629 if (entry
->is_sub_map
) {
4630 /* clip did unnest if needed */
4631 assert(!entry
->use_pmap
);
4634 entry
->inheritance
= new_inheritance
;
4636 entry
= entry
->vme_next
;
4640 return(KERN_SUCCESS
);
4644 * Update the accounting for the amount of wired memory in this map. If the user has
4645 * exceeded the defined limits, then we fail. Wiring on behalf of the kernel never fails.
4648 static kern_return_t
4651 vm_map_entry_t entry
,
4652 boolean_t user_wire
)
4657 unsigned int total_wire_count
= vm_page_wire_count
+ vm_lopage_free_count
;
4660 * We're wiring memory at the request of the user. Check if this is the first time the user is wiring
4664 if (entry
->user_wired_count
== 0) {
4665 size
= entry
->vme_end
- entry
->vme_start
;
4668 * Since this is the first time the user is wiring this map entry, check to see if we're
4669 * exceeding the user wire limits. There is a per map limit which is the smaller of either
4670 * the process's rlimit or the global vm_user_wire_limit which caps this value. There is also
4671 * a system-wide limit on the amount of memory all users can wire. If the user is over either
4672 * limit, then we fail.
4675 if(size
+ map
->user_wire_size
> MIN(map
->user_wire_limit
, vm_user_wire_limit
) ||
4676 size
+ ptoa_64(total_wire_count
) > vm_global_user_wire_limit
||
4677 size
+ ptoa_64(total_wire_count
) > max_mem
- vm_global_no_user_wire_amount
)
4678 return KERN_RESOURCE_SHORTAGE
;
4681 * The first time the user wires an entry, we also increment the wired_count and add this to
4682 * the total that has been wired in the map.
4685 if (entry
->wired_count
>= MAX_WIRE_COUNT
)
4686 return KERN_FAILURE
;
4688 entry
->wired_count
++;
4689 map
->user_wire_size
+= size
;
4692 if (entry
->user_wired_count
>= MAX_WIRE_COUNT
)
4693 return KERN_FAILURE
;
4695 entry
->user_wired_count
++;
4700 * The kernel's wiring the memory. Just bump the count and continue.
4703 if (entry
->wired_count
>= MAX_WIRE_COUNT
)
4704 panic("vm_map_wire: too many wirings");
4706 entry
->wired_count
++;
4709 return KERN_SUCCESS
;
4713 * Update the memory wiring accounting now that the given map entry is being unwired.
4717 subtract_wire_counts(
4719 vm_map_entry_t entry
,
4720 boolean_t user_wire
)
4726 * We're unwiring memory at the request of the user. See if we're removing the last user wire reference.
4729 if (entry
->user_wired_count
== 1) {
4732 * We're removing the last user wire reference. Decrement the wired_count and the total
4733 * user wired memory for this map.
4736 assert(entry
->wired_count
>= 1);
4737 entry
->wired_count
--;
4738 map
->user_wire_size
-= entry
->vme_end
- entry
->vme_start
;
4741 assert(entry
->user_wired_count
>= 1);
4742 entry
->user_wired_count
--;
4747 * The kernel is unwiring the memory. Just update the count.
4750 assert(entry
->wired_count
>= 1);
4751 entry
->wired_count
--;
4758 * Sets the pageability of the specified address range in the
4759 * target map as wired. Regions specified as not pageable require
4760 * locked-down physical memory and physical page maps. The
4761 * access_type variable indicates types of accesses that must not
4762 * generate page faults. This is checked against protection of
4763 * memory being locked-down.
4765 * The map must not be locked, but a reference must remain to the
4766 * map throughout the call.
4768 static kern_return_t
4770 register vm_map_t map
,
4771 register vm_map_offset_t start
,
4772 register vm_map_offset_t end
,
4773 register vm_prot_t caller_prot
,
4774 boolean_t user_wire
,
4776 vm_map_offset_t pmap_addr
,
4777 ppnum_t
*physpage_p
)
4779 register vm_map_entry_t entry
;
4780 register vm_prot_t access_type
;
4781 struct vm_map_entry
*first_entry
, tmp_entry
;
4783 register vm_map_offset_t s
,e
;
4785 boolean_t need_wakeup
;
4786 boolean_t main_map
= FALSE
;
4787 wait_interrupt_t interruptible_state
;
4788 thread_t cur_thread
;
4789 unsigned int last_timestamp
;
4791 boolean_t wire_and_extract
;
4793 access_type
= (caller_prot
& VM_PROT_ALL
);
4795 wire_and_extract
= FALSE
;
4796 if (physpage_p
!= NULL
) {
4798 * The caller wants the physical page number of the
4799 * wired page. We return only one physical page number
4800 * so this works for only one page at a time.
4802 if ((end
- start
) != PAGE_SIZE
) {
4803 return KERN_INVALID_ARGUMENT
;
4805 wire_and_extract
= TRUE
;
4810 if(map_pmap
== NULL
)
4812 last_timestamp
= map
->timestamp
;
4814 VM_MAP_RANGE_CHECK(map
, start
, end
);
4815 assert(page_aligned(start
));
4816 assert(page_aligned(end
));
4817 assert(VM_MAP_PAGE_ALIGNED(start
, VM_MAP_PAGE_MASK(map
)));
4818 assert(VM_MAP_PAGE_ALIGNED(end
, VM_MAP_PAGE_MASK(map
)));
4820 /* We wired what the caller asked for, zero pages */
4822 return KERN_SUCCESS
;
4825 need_wakeup
= FALSE
;
4826 cur_thread
= current_thread();
4831 if (vm_map_lookup_entry(map
, s
, &first_entry
)) {
4832 entry
= first_entry
;
4834 * vm_map_clip_start will be done later.
4835 * We don't want to unnest any nested submaps here !
4838 /* Start address is not in map */
4839 rc
= KERN_INVALID_ADDRESS
;
4843 while ((entry
!= vm_map_to_entry(map
)) && (s
< end
)) {
4845 * At this point, we have wired from "start" to "s".
4846 * We still need to wire from "s" to "end".
4848 * "entry" hasn't been clipped, so it could start before "s"
4849 * and/or end after "end".
4852 /* "e" is how far we want to wire in this entry */
4858 * If another thread is wiring/unwiring this entry then
4859 * block after informing other thread to wake us up.
4861 if (entry
->in_transition
) {
4862 wait_result_t wait_result
;
4865 * We have not clipped the entry. Make sure that
4866 * the start address is in range so that the lookup
4867 * below will succeed.
4868 * "s" is the current starting point: we've already
4869 * wired from "start" to "s" and we still have
4870 * to wire from "s" to "end".
4873 entry
->needs_wakeup
= TRUE
;
4876 * wake up anybody waiting on entries that we have
4880 vm_map_entry_wakeup(map
);
4881 need_wakeup
= FALSE
;
4884 * User wiring is interruptible
4886 wait_result
= vm_map_entry_wait(map
,
4887 (user_wire
) ? THREAD_ABORTSAFE
:
4889 if (user_wire
&& wait_result
== THREAD_INTERRUPTED
) {
4891 * undo the wirings we have done so far
4892 * We do not clear the needs_wakeup flag,
4893 * because we cannot tell if we were the
4901 * Cannot avoid a lookup here. reset timestamp.
4903 last_timestamp
= map
->timestamp
;
4906 * The entry could have been clipped, look it up again.
4907 * Worse that can happen is, it may not exist anymore.
4909 if (!vm_map_lookup_entry(map
, s
, &first_entry
)) {
4911 * User: undo everything upto the previous
4912 * entry. let vm_map_unwire worry about
4913 * checking the validity of the range.
4918 entry
= first_entry
;
4922 if (entry
->is_sub_map
) {
4923 vm_map_offset_t sub_start
;
4924 vm_map_offset_t sub_end
;
4925 vm_map_offset_t local_start
;
4926 vm_map_offset_t local_end
;
4929 if (wire_and_extract
) {
4931 * Wiring would result in copy-on-write
4932 * which would not be compatible with
4933 * the sharing we have with the original
4934 * provider of this memory.
4936 rc
= KERN_INVALID_ARGUMENT
;
4940 vm_map_clip_start(map
, entry
, s
);
4941 vm_map_clip_end(map
, entry
, end
);
4943 sub_start
= VME_OFFSET(entry
);
4944 sub_end
= entry
->vme_end
;
4945 sub_end
+= VME_OFFSET(entry
) - entry
->vme_start
;
4947 local_end
= entry
->vme_end
;
4948 if(map_pmap
== NULL
) {
4950 vm_object_offset_t offset
;
4953 vm_map_entry_t local_entry
;
4954 vm_map_version_t version
;
4955 vm_map_t lookup_map
;
4957 if(entry
->use_pmap
) {
4958 pmap
= VME_SUBMAP(entry
)->pmap
;
4959 /* ppc implementation requires that */
4960 /* submaps pmap address ranges line */
4961 /* up with parent map */
4963 pmap_addr
= sub_start
;
4971 if (entry
->wired_count
) {
4972 if ((rc
= add_wire_counts(map
, entry
, user_wire
)) != KERN_SUCCESS
)
4976 * The map was not unlocked:
4977 * no need to goto re-lookup.
4978 * Just go directly to next entry.
4980 entry
= entry
->vme_next
;
4981 s
= entry
->vme_start
;
4986 /* call vm_map_lookup_locked to */
4987 /* cause any needs copy to be */
4989 local_start
= entry
->vme_start
;
4991 vm_map_lock_write_to_read(map
);
4992 if(vm_map_lookup_locked(
4993 &lookup_map
, local_start
,
4995 OBJECT_LOCK_EXCLUSIVE
,
4997 &offset
, &prot
, &wired
,
5001 vm_map_unlock_read(lookup_map
);
5002 vm_map_unwire(map
, start
,
5004 return(KERN_FAILURE
);
5006 vm_object_unlock(object
);
5007 if(real_map
!= lookup_map
)
5008 vm_map_unlock(real_map
);
5009 vm_map_unlock_read(lookup_map
);
5012 /* we unlocked, so must re-lookup */
5013 if (!vm_map_lookup_entry(map
,
5021 * entry could have been "simplified",
5024 entry
= local_entry
;
5025 assert(s
== local_start
);
5026 vm_map_clip_start(map
, entry
, s
);
5027 vm_map_clip_end(map
, entry
, end
);
5028 /* re-compute "e" */
5033 /* did we have a change of type? */
5034 if (!entry
->is_sub_map
) {
5035 last_timestamp
= map
->timestamp
;
5039 local_start
= entry
->vme_start
;
5043 if ((rc
= add_wire_counts(map
, entry
, user_wire
)) != KERN_SUCCESS
)
5046 entry
->in_transition
= TRUE
;
5049 rc
= vm_map_wire_nested(VME_SUBMAP(entry
),
5052 user_wire
, pmap
, pmap_addr
,
5057 * Find the entry again. It could have been clipped
5058 * after we unlocked the map.
5060 if (!vm_map_lookup_entry(map
, local_start
,
5062 panic("vm_map_wire: re-lookup failed");
5063 entry
= first_entry
;
5065 assert(local_start
== s
);
5066 /* re-compute "e" */
5071 last_timestamp
= map
->timestamp
;
5072 while ((entry
!= vm_map_to_entry(map
)) &&
5073 (entry
->vme_start
< e
)) {
5074 assert(entry
->in_transition
);
5075 entry
->in_transition
= FALSE
;
5076 if (entry
->needs_wakeup
) {
5077 entry
->needs_wakeup
= FALSE
;
5080 if (rc
!= KERN_SUCCESS
) {/* from vm_*_wire */
5081 subtract_wire_counts(map
, entry
, user_wire
);
5083 entry
= entry
->vme_next
;
5085 if (rc
!= KERN_SUCCESS
) { /* from vm_*_wire */
5089 /* no need to relookup again */
5090 s
= entry
->vme_start
;
5095 * If this entry is already wired then increment
5096 * the appropriate wire reference count.
5098 if (entry
->wired_count
) {
5100 if ((entry
->protection
& access_type
) != access_type
) {
5101 /* found a protection problem */
5105 * We should always return an error
5106 * in this case but since we didn't
5107 * enforce it before, let's do
5108 * it only for the new "wire_and_extract"
5109 * code path for now...
5111 if (wire_and_extract
) {
5112 rc
= KERN_PROTECTION_FAILURE
;
5118 * entry is already wired down, get our reference
5119 * after clipping to our range.
5121 vm_map_clip_start(map
, entry
, s
);
5122 vm_map_clip_end(map
, entry
, end
);
5124 if ((rc
= add_wire_counts(map
, entry
, user_wire
)) != KERN_SUCCESS
)
5127 if (wire_and_extract
) {
5129 vm_object_offset_t offset
;
5133 * We don't have to "wire" the page again
5134 * bit we still have to "extract" its
5135 * physical page number, after some sanity
5138 assert((entry
->vme_end
- entry
->vme_start
)
5140 assert(!entry
->needs_copy
);
5141 assert(!entry
->is_sub_map
);
5142 assert(VME_OBJECT(entry
));
5143 if (((entry
->vme_end
- entry
->vme_start
)
5145 entry
->needs_copy
||
5146 entry
->is_sub_map
||
5147 VME_OBJECT(entry
) == VM_OBJECT_NULL
) {
5148 rc
= KERN_INVALID_ARGUMENT
;
5152 object
= VME_OBJECT(entry
);
5153 offset
= VME_OFFSET(entry
);
5154 /* need exclusive lock to update m->dirty */
5155 if (entry
->protection
& VM_PROT_WRITE
) {
5156 vm_object_lock(object
);
5158 vm_object_lock_shared(object
);
5160 m
= vm_page_lookup(object
, offset
);
5161 assert(m
!= VM_PAGE_NULL
);
5162 assert(m
->wire_count
);
5163 if (m
!= VM_PAGE_NULL
&& m
->wire_count
) {
5164 *physpage_p
= m
->phys_page
;
5165 if (entry
->protection
& VM_PROT_WRITE
) {
5166 vm_object_lock_assert_exclusive(
5171 /* not already wired !? */
5174 vm_object_unlock(object
);
5177 /* map was not unlocked: no need to relookup */
5178 entry
= entry
->vme_next
;
5179 s
= entry
->vme_start
;
5184 * Unwired entry or wire request transmitted via submap
5189 * Perform actions of vm_map_lookup that need the write
5190 * lock on the map: create a shadow object for a
5191 * copy-on-write region, or an object for a zero-fill
5194 size
= entry
->vme_end
- entry
->vme_start
;
5196 * If wiring a copy-on-write page, we need to copy it now
5197 * even if we're only (currently) requesting read access.
5198 * This is aggressive, but once it's wired we can't move it.
5200 if (entry
->needs_copy
) {
5201 if (wire_and_extract
) {
5203 * We're supposed to share with the original
5204 * provider so should not be "needs_copy"
5206 rc
= KERN_INVALID_ARGUMENT
;
5210 VME_OBJECT_SHADOW(entry
, size
);
5211 entry
->needs_copy
= FALSE
;
5212 } else if (VME_OBJECT(entry
) == VM_OBJECT_NULL
) {
5213 if (wire_and_extract
) {
5215 * We're supposed to share with the original
5216 * provider so should already have an object.
5218 rc
= KERN_INVALID_ARGUMENT
;
5221 VME_OBJECT_SET(entry
, vm_object_allocate(size
));
5222 VME_OFFSET_SET(entry
, (vm_object_offset_t
)0);
5223 assert(entry
->use_pmap
);
5226 vm_map_clip_start(map
, entry
, s
);
5227 vm_map_clip_end(map
, entry
, end
);
5229 /* re-compute "e" */
5235 * Check for holes and protection mismatch.
5236 * Holes: Next entry should be contiguous unless this
5237 * is the end of the region.
5238 * Protection: Access requested must be allowed, unless
5239 * wiring is by protection class
5241 if ((entry
->vme_end
< end
) &&
5242 ((entry
->vme_next
== vm_map_to_entry(map
)) ||
5243 (entry
->vme_next
->vme_start
> entry
->vme_end
))) {
5245 rc
= KERN_INVALID_ADDRESS
;
5248 if ((entry
->protection
& access_type
) != access_type
) {
5249 /* found a protection problem */
5250 rc
= KERN_PROTECTION_FAILURE
;
5254 assert(entry
->wired_count
== 0 && entry
->user_wired_count
== 0);
5256 if ((rc
= add_wire_counts(map
, entry
, user_wire
)) != KERN_SUCCESS
)
5259 entry
->in_transition
= TRUE
;
5262 * This entry might get split once we unlock the map.
5263 * In vm_fault_wire(), we need the current range as
5264 * defined by this entry. In order for this to work
5265 * along with a simultaneous clip operation, we make a
5266 * temporary copy of this entry and use that for the
5267 * wiring. Note that the underlying objects do not
5268 * change during a clip.
5273 * The in_transition state guarentees that the entry
5274 * (or entries for this range, if split occured) will be
5275 * there when the map lock is acquired for the second time.
5279 if (!user_wire
&& cur_thread
!= THREAD_NULL
)
5280 interruptible_state
= thread_interrupt_level(THREAD_UNINT
);
5282 interruptible_state
= THREAD_UNINT
;
5285 rc
= vm_fault_wire(map
,
5286 &tmp_entry
, caller_prot
, map_pmap
, pmap_addr
,
5289 rc
= vm_fault_wire(map
,
5290 &tmp_entry
, caller_prot
, map
->pmap
,
5291 tmp_entry
.vme_start
,
5294 if (!user_wire
&& cur_thread
!= THREAD_NULL
)
5295 thread_interrupt_level(interruptible_state
);
5299 if (last_timestamp
+1 != map
->timestamp
) {
5301 * Find the entry again. It could have been clipped
5302 * after we unlocked the map.
5304 if (!vm_map_lookup_entry(map
, tmp_entry
.vme_start
,
5306 panic("vm_map_wire: re-lookup failed");
5308 entry
= first_entry
;
5311 last_timestamp
= map
->timestamp
;
5313 while ((entry
!= vm_map_to_entry(map
)) &&
5314 (entry
->vme_start
< tmp_entry
.vme_end
)) {
5315 assert(entry
->in_transition
);
5316 entry
->in_transition
= FALSE
;
5317 if (entry
->needs_wakeup
) {
5318 entry
->needs_wakeup
= FALSE
;
5321 if (rc
!= KERN_SUCCESS
) { /* from vm_*_wire */
5322 subtract_wire_counts(map
, entry
, user_wire
);
5324 entry
= entry
->vme_next
;
5327 if (rc
!= KERN_SUCCESS
) { /* from vm_*_wire */
5331 s
= entry
->vme_start
;
5332 } /* end while loop through map entries */
5335 if (rc
== KERN_SUCCESS
) {
5336 /* repair any damage we may have made to the VM map */
5337 vm_map_simplify_range(map
, start
, end
);
5343 * wake up anybody waiting on entries we wired.
5346 vm_map_entry_wakeup(map
);
5348 if (rc
!= KERN_SUCCESS
) {
5349 /* undo what has been wired so far */
5350 vm_map_unwire(map
, start
, s
, user_wire
);
5361 vm_map_wire_external(
5362 register vm_map_t map
,
5363 register vm_map_offset_t start
,
5364 register vm_map_offset_t end
,
5365 register vm_prot_t caller_prot
,
5366 boolean_t user_wire
)
5370 caller_prot
&= ~VM_PROT_MEMORY_TAG_MASK
;
5371 caller_prot
|= VM_PROT_MEMORY_TAG_MAKE(vm_tag_bt());
5372 kret
= vm_map_wire_nested(map
, start
, end
, caller_prot
,
5373 user_wire
, (pmap_t
)NULL
, 0, NULL
);
5379 register vm_map_t map
,
5380 register vm_map_offset_t start
,
5381 register vm_map_offset_t end
,
5382 register vm_prot_t caller_prot
,
5383 boolean_t user_wire
)
5387 kret
= vm_map_wire_nested(map
, start
, end
, caller_prot
,
5388 user_wire
, (pmap_t
)NULL
, 0, NULL
);
5393 vm_map_wire_and_extract_external(
5395 vm_map_offset_t start
,
5396 vm_prot_t caller_prot
,
5397 boolean_t user_wire
,
5398 ppnum_t
*physpage_p
)
5402 caller_prot
&= ~VM_PROT_MEMORY_TAG_MASK
;
5403 caller_prot
|= VM_PROT_MEMORY_TAG_MAKE(vm_tag_bt());
5404 kret
= vm_map_wire_nested(map
,
5406 start
+VM_MAP_PAGE_SIZE(map
),
5412 if (kret
!= KERN_SUCCESS
&&
5413 physpage_p
!= NULL
) {
5420 vm_map_wire_and_extract(
5422 vm_map_offset_t start
,
5423 vm_prot_t caller_prot
,
5424 boolean_t user_wire
,
5425 ppnum_t
*physpage_p
)
5429 kret
= vm_map_wire_nested(map
,
5431 start
+VM_MAP_PAGE_SIZE(map
),
5437 if (kret
!= KERN_SUCCESS
&&
5438 physpage_p
!= NULL
) {
5447 * Sets the pageability of the specified address range in the target
5448 * as pageable. Regions specified must have been wired previously.
5450 * The map must not be locked, but a reference must remain to the map
5451 * throughout the call.
5453 * Kernel will panic on failures. User unwire ignores holes and
5454 * unwired and intransition entries to avoid losing memory by leaving
5457 static kern_return_t
5458 vm_map_unwire_nested(
5459 register vm_map_t map
,
5460 register vm_map_offset_t start
,
5461 register vm_map_offset_t end
,
5462 boolean_t user_wire
,
5464 vm_map_offset_t pmap_addr
)
5466 register vm_map_entry_t entry
;
5467 struct vm_map_entry
*first_entry
, tmp_entry
;
5468 boolean_t need_wakeup
;
5469 boolean_t main_map
= FALSE
;
5470 unsigned int last_timestamp
;
5473 if(map_pmap
== NULL
)
5475 last_timestamp
= map
->timestamp
;
5477 VM_MAP_RANGE_CHECK(map
, start
, end
);
5478 assert(page_aligned(start
));
5479 assert(page_aligned(end
));
5480 assert(VM_MAP_PAGE_ALIGNED(start
, VM_MAP_PAGE_MASK(map
)));
5481 assert(VM_MAP_PAGE_ALIGNED(end
, VM_MAP_PAGE_MASK(map
)));
5484 /* We unwired what the caller asked for: zero pages */
5486 return KERN_SUCCESS
;
5489 if (vm_map_lookup_entry(map
, start
, &first_entry
)) {
5490 entry
= first_entry
;
5492 * vm_map_clip_start will be done later.
5493 * We don't want to unnest any nested sub maps here !
5498 panic("vm_map_unwire: start not found");
5500 /* Start address is not in map. */
5502 return(KERN_INVALID_ADDRESS
);
5505 if (entry
->superpage_size
) {
5506 /* superpages are always wired */
5508 return KERN_INVALID_ADDRESS
;
5511 need_wakeup
= FALSE
;
5512 while ((entry
!= vm_map_to_entry(map
)) && (entry
->vme_start
< end
)) {
5513 if (entry
->in_transition
) {
5516 * Another thread is wiring down this entry. Note
5517 * that if it is not for the other thread we would
5518 * be unwiring an unwired entry. This is not
5519 * permitted. If we wait, we will be unwiring memory
5523 * Another thread is unwiring this entry. We did not
5524 * have a reference to it, because if we did, this
5525 * entry will not be getting unwired now.
5530 * This could happen: there could be some
5531 * overlapping vslock/vsunlock operations
5533 * We should probably just wait and retry,
5534 * but then we have to be careful that this
5535 * entry could get "simplified" after
5536 * "in_transition" gets unset and before
5537 * we re-lookup the entry, so we would
5538 * have to re-clip the entry to avoid
5539 * re-unwiring what we have already unwired...
5540 * See vm_map_wire_nested().
5542 * Or we could just ignore "in_transition"
5543 * here and proceed to decement the wired
5544 * count(s) on this entry. That should be fine
5545 * as long as "wired_count" doesn't drop all
5546 * the way to 0 (and we should panic if THAT
5549 panic("vm_map_unwire: in_transition entry");
5552 entry
= entry
->vme_next
;
5556 if (entry
->is_sub_map
) {
5557 vm_map_offset_t sub_start
;
5558 vm_map_offset_t sub_end
;
5559 vm_map_offset_t local_end
;
5562 vm_map_clip_start(map
, entry
, start
);
5563 vm_map_clip_end(map
, entry
, end
);
5565 sub_start
= VME_OFFSET(entry
);
5566 sub_end
= entry
->vme_end
- entry
->vme_start
;
5567 sub_end
+= VME_OFFSET(entry
);
5568 local_end
= entry
->vme_end
;
5569 if(map_pmap
== NULL
) {
5570 if(entry
->use_pmap
) {
5571 pmap
= VME_SUBMAP(entry
)->pmap
;
5572 pmap_addr
= sub_start
;
5577 if (entry
->wired_count
== 0 ||
5578 (user_wire
&& entry
->user_wired_count
== 0)) {
5580 panic("vm_map_unwire: entry is unwired");
5581 entry
= entry
->vme_next
;
5587 * Holes: Next entry should be contiguous unless
5588 * this is the end of the region.
5590 if (((entry
->vme_end
< end
) &&
5591 ((entry
->vme_next
== vm_map_to_entry(map
)) ||
5592 (entry
->vme_next
->vme_start
5593 > entry
->vme_end
)))) {
5595 panic("vm_map_unwire: non-contiguous region");
5597 entry = entry->vme_next;
5602 subtract_wire_counts(map
, entry
, user_wire
);
5604 if (entry
->wired_count
!= 0) {
5605 entry
= entry
->vme_next
;
5609 entry
->in_transition
= TRUE
;
5610 tmp_entry
= *entry
;/* see comment in vm_map_wire() */
5613 * We can unlock the map now. The in_transition state
5614 * guarantees existance of the entry.
5617 vm_map_unwire_nested(VME_SUBMAP(entry
),
5618 sub_start
, sub_end
, user_wire
, pmap
, pmap_addr
);
5621 if (last_timestamp
+1 != map
->timestamp
) {
5623 * Find the entry again. It could have been
5624 * clipped or deleted after we unlocked the map.
5626 if (!vm_map_lookup_entry(map
,
5627 tmp_entry
.vme_start
,
5630 panic("vm_map_unwire: re-lookup failed");
5631 entry
= first_entry
->vme_next
;
5633 entry
= first_entry
;
5635 last_timestamp
= map
->timestamp
;
5638 * clear transition bit for all constituent entries
5639 * that were in the original entry (saved in
5640 * tmp_entry). Also check for waiters.
5642 while ((entry
!= vm_map_to_entry(map
)) &&
5643 (entry
->vme_start
< tmp_entry
.vme_end
)) {
5644 assert(entry
->in_transition
);
5645 entry
->in_transition
= FALSE
;
5646 if (entry
->needs_wakeup
) {
5647 entry
->needs_wakeup
= FALSE
;
5650 entry
= entry
->vme_next
;
5655 vm_map_unwire_nested(VME_SUBMAP(entry
),
5656 sub_start
, sub_end
, user_wire
, map_pmap
,
5660 if (last_timestamp
+1 != map
->timestamp
) {
5662 * Find the entry again. It could have been
5663 * clipped or deleted after we unlocked the map.
5665 if (!vm_map_lookup_entry(map
,
5666 tmp_entry
.vme_start
,
5669 panic("vm_map_unwire: re-lookup failed");
5670 entry
= first_entry
->vme_next
;
5672 entry
= first_entry
;
5674 last_timestamp
= map
->timestamp
;
5679 if ((entry
->wired_count
== 0) ||
5680 (user_wire
&& entry
->user_wired_count
== 0)) {
5682 panic("vm_map_unwire: entry is unwired");
5684 entry
= entry
->vme_next
;
5688 assert(entry
->wired_count
> 0 &&
5689 (!user_wire
|| entry
->user_wired_count
> 0));
5691 vm_map_clip_start(map
, entry
, start
);
5692 vm_map_clip_end(map
, entry
, end
);
5696 * Holes: Next entry should be contiguous unless
5697 * this is the end of the region.
5699 if (((entry
->vme_end
< end
) &&
5700 ((entry
->vme_next
== vm_map_to_entry(map
)) ||
5701 (entry
->vme_next
->vme_start
> entry
->vme_end
)))) {
5704 panic("vm_map_unwire: non-contiguous region");
5705 entry
= entry
->vme_next
;
5709 subtract_wire_counts(map
, entry
, user_wire
);
5711 if (entry
->wired_count
!= 0) {
5712 entry
= entry
->vme_next
;
5716 if(entry
->zero_wired_pages
) {
5717 entry
->zero_wired_pages
= FALSE
;
5720 entry
->in_transition
= TRUE
;
5721 tmp_entry
= *entry
; /* see comment in vm_map_wire() */
5724 * We can unlock the map now. The in_transition state
5725 * guarantees existance of the entry.
5729 vm_fault_unwire(map
,
5730 &tmp_entry
, FALSE
, map_pmap
, pmap_addr
);
5732 vm_fault_unwire(map
,
5733 &tmp_entry
, FALSE
, map
->pmap
,
5734 tmp_entry
.vme_start
);
5738 if (last_timestamp
+1 != map
->timestamp
) {
5740 * Find the entry again. It could have been clipped
5741 * or deleted after we unlocked the map.
5743 if (!vm_map_lookup_entry(map
, tmp_entry
.vme_start
,
5746 panic("vm_map_unwire: re-lookup failed");
5747 entry
= first_entry
->vme_next
;
5749 entry
= first_entry
;
5751 last_timestamp
= map
->timestamp
;
5754 * clear transition bit for all constituent entries that
5755 * were in the original entry (saved in tmp_entry). Also
5756 * check for waiters.
5758 while ((entry
!= vm_map_to_entry(map
)) &&
5759 (entry
->vme_start
< tmp_entry
.vme_end
)) {
5760 assert(entry
->in_transition
);
5761 entry
->in_transition
= FALSE
;
5762 if (entry
->needs_wakeup
) {
5763 entry
->needs_wakeup
= FALSE
;
5766 entry
= entry
->vme_next
;
5771 * We might have fragmented the address space when we wired this
5772 * range of addresses. Attempt to re-coalesce these VM map entries
5773 * with their neighbors now that they're no longer wired.
5774 * Under some circumstances, address space fragmentation can
5775 * prevent VM object shadow chain collapsing, which can cause
5778 vm_map_simplify_range(map
, start
, end
);
5782 * wake up anybody waiting on entries that we have unwired.
5785 vm_map_entry_wakeup(map
);
5786 return(KERN_SUCCESS
);
5792 register vm_map_t map
,
5793 register vm_map_offset_t start
,
5794 register vm_map_offset_t end
,
5795 boolean_t user_wire
)
5797 return vm_map_unwire_nested(map
, start
, end
,
5798 user_wire
, (pmap_t
)NULL
, 0);
5803 * vm_map_entry_delete: [ internal use only ]
5805 * Deallocate the given entry from the target map.
5808 vm_map_entry_delete(
5809 register vm_map_t map
,
5810 register vm_map_entry_t entry
)
5812 register vm_map_offset_t s
, e
;
5813 register vm_object_t object
;
5814 register vm_map_t submap
;
5816 s
= entry
->vme_start
;
5818 assert(page_aligned(s
));
5819 assert(page_aligned(e
));
5820 if (entry
->map_aligned
== TRUE
) {
5821 assert(VM_MAP_PAGE_ALIGNED(s
, VM_MAP_PAGE_MASK(map
)));
5822 assert(VM_MAP_PAGE_ALIGNED(e
, VM_MAP_PAGE_MASK(map
)));
5824 assert(entry
->wired_count
== 0);
5825 assert(entry
->user_wired_count
== 0);
5826 assert(!entry
->permanent
);
5828 if (entry
->is_sub_map
) {
5830 submap
= VME_SUBMAP(entry
);
5833 object
= VME_OBJECT(entry
);
5836 vm_map_store_entry_unlink(map
, entry
);
5839 vm_map_entry_dispose(map
, entry
);
5843 * Deallocate the object only after removing all
5844 * pmap entries pointing to its pages.
5847 vm_map_deallocate(submap
);
5849 vm_object_deallocate(object
);
5854 vm_map_submap_pmap_clean(
5856 vm_map_offset_t start
,
5857 vm_map_offset_t end
,
5859 vm_map_offset_t offset
)
5861 vm_map_offset_t submap_start
;
5862 vm_map_offset_t submap_end
;
5863 vm_map_size_t remove_size
;
5864 vm_map_entry_t entry
;
5866 submap_end
= offset
+ (end
- start
);
5867 submap_start
= offset
;
5869 vm_map_lock_read(sub_map
);
5870 if(vm_map_lookup_entry(sub_map
, offset
, &entry
)) {
5872 remove_size
= (entry
->vme_end
- entry
->vme_start
);
5873 if(offset
> entry
->vme_start
)
5874 remove_size
-= offset
- entry
->vme_start
;
5877 if(submap_end
< entry
->vme_end
) {
5879 entry
->vme_end
- submap_end
;
5881 if(entry
->is_sub_map
) {
5882 vm_map_submap_pmap_clean(
5885 start
+ remove_size
,
5890 if((map
->mapped_in_other_pmaps
) && (map
->ref_count
)
5891 && (VME_OBJECT(entry
) != NULL
)) {
5892 vm_object_pmap_protect_options(
5894 (VME_OFFSET(entry
) +
5901 PMAP_OPTIONS_REMOVE
);
5903 pmap_remove(map
->pmap
,
5905 (addr64_t
)(start
+ remove_size
));
5910 entry
= entry
->vme_next
;
5912 while((entry
!= vm_map_to_entry(sub_map
))
5913 && (entry
->vme_start
< submap_end
)) {
5914 remove_size
= (entry
->vme_end
- entry
->vme_start
);
5915 if(submap_end
< entry
->vme_end
) {
5916 remove_size
-= entry
->vme_end
- submap_end
;
5918 if(entry
->is_sub_map
) {
5919 vm_map_submap_pmap_clean(
5921 (start
+ entry
->vme_start
) - offset
,
5922 ((start
+ entry
->vme_start
) - offset
) + remove_size
,
5926 if((map
->mapped_in_other_pmaps
) && (map
->ref_count
)
5927 && (VME_OBJECT(entry
) != NULL
)) {
5928 vm_object_pmap_protect_options(
5935 PMAP_OPTIONS_REMOVE
);
5937 pmap_remove(map
->pmap
,
5938 (addr64_t
)((start
+ entry
->vme_start
)
5940 (addr64_t
)(((start
+ entry
->vme_start
)
5941 - offset
) + remove_size
));
5944 entry
= entry
->vme_next
;
5946 vm_map_unlock_read(sub_map
);
5951 * vm_map_delete: [ internal use only ]
5953 * Deallocates the given address range from the target map.
5954 * Removes all user wirings. Unwires one kernel wiring if
5955 * VM_MAP_REMOVE_KUNWIRE is set. Waits for kernel wirings to go
5956 * away if VM_MAP_REMOVE_WAIT_FOR_KWIRE is set. Sleeps
5957 * interruptibly if VM_MAP_REMOVE_INTERRUPTIBLE is set.
5959 * This routine is called with map locked and leaves map locked.
5961 static kern_return_t
5964 vm_map_offset_t start
,
5965 vm_map_offset_t end
,
5969 vm_map_entry_t entry
, next
;
5970 struct vm_map_entry
*first_entry
, tmp_entry
;
5971 register vm_map_offset_t s
;
5972 register vm_object_t object
;
5973 boolean_t need_wakeup
;
5974 unsigned int last_timestamp
= ~0; /* unlikely value */
5977 interruptible
= (flags
& VM_MAP_REMOVE_INTERRUPTIBLE
) ?
5978 THREAD_ABORTSAFE
: THREAD_UNINT
;
5981 * All our DMA I/O operations in IOKit are currently done by
5982 * wiring through the map entries of the task requesting the I/O.
5983 * Because of this, we must always wait for kernel wirings
5984 * to go away on the entries before deleting them.
5986 * Any caller who wants to actually remove a kernel wiring
5987 * should explicitly set the VM_MAP_REMOVE_KUNWIRE flag to
5988 * properly remove one wiring instead of blasting through
5991 flags
|= VM_MAP_REMOVE_WAIT_FOR_KWIRE
;
5995 * Find the start of the region, and clip it
5997 if (vm_map_lookup_entry(map
, start
, &first_entry
)) {
5998 entry
= first_entry
;
5999 if (map
== kalloc_map
&&
6000 (entry
->vme_start
!= start
||
6001 entry
->vme_end
!= end
)) {
6002 panic("vm_map_delete(%p,0x%llx,0x%llx): "
6003 "mismatched entry %p [0x%llx:0x%llx]\n",
6008 (uint64_t)entry
->vme_start
,
6009 (uint64_t)entry
->vme_end
);
6011 if (entry
->superpage_size
&& (start
& ~SUPERPAGE_MASK
)) { /* extend request to whole entry */ start
= SUPERPAGE_ROUND_DOWN(start
);
6012 start
= SUPERPAGE_ROUND_DOWN(start
);
6015 if (start
== entry
->vme_start
) {
6017 * No need to clip. We don't want to cause
6018 * any unnecessary unnesting in this case...
6021 if ((flags
& VM_MAP_REMOVE_NO_MAP_ALIGN
) &&
6022 entry
->map_aligned
&&
6023 !VM_MAP_PAGE_ALIGNED(
6025 VM_MAP_PAGE_MASK(map
))) {
6027 * The entry will no longer be
6028 * map-aligned after clipping
6029 * and the caller said it's OK.
6031 entry
->map_aligned
= FALSE
;
6033 if (map
== kalloc_map
) {
6034 panic("vm_map_delete(%p,0x%llx,0x%llx):"
6035 " clipping %p at 0x%llx\n",
6042 vm_map_clip_start(map
, entry
, start
);
6046 * Fix the lookup hint now, rather than each
6047 * time through the loop.
6049 SAVE_HINT_MAP_WRITE(map
, entry
->vme_prev
);
6051 if (map
->pmap
== kernel_pmap
&&
6052 map
->ref_count
!= 0) {
6053 panic("vm_map_delete(%p,0x%llx,0x%llx): "
6054 "no map entry at 0x%llx\n",
6060 entry
= first_entry
->vme_next
;
6064 if (entry
->superpage_size
)
6065 end
= SUPERPAGE_ROUND_UP(end
);
6067 need_wakeup
= FALSE
;
6069 * Step through all entries in this region
6071 s
= entry
->vme_start
;
6072 while ((entry
!= vm_map_to_entry(map
)) && (s
< end
)) {
6074 * At this point, we have deleted all the memory entries
6075 * between "start" and "s". We still need to delete
6076 * all memory entries between "s" and "end".
6077 * While we were blocked and the map was unlocked, some
6078 * new memory entries could have been re-allocated between
6079 * "start" and "s" and we don't want to mess with those.
6080 * Some of those entries could even have been re-assembled
6081 * with an entry after "s" (in vm_map_simplify_entry()), so
6082 * we may have to vm_map_clip_start() again.
6085 if (entry
->vme_start
>= s
) {
6087 * This entry starts on or after "s"
6088 * so no need to clip its start.
6092 * This entry has been re-assembled by a
6093 * vm_map_simplify_entry(). We need to
6094 * re-clip its start.
6096 if ((flags
& VM_MAP_REMOVE_NO_MAP_ALIGN
) &&
6097 entry
->map_aligned
&&
6098 !VM_MAP_PAGE_ALIGNED(s
,
6099 VM_MAP_PAGE_MASK(map
))) {
6101 * The entry will no longer be map-aligned
6102 * after clipping and the caller said it's OK.
6104 entry
->map_aligned
= FALSE
;
6106 if (map
== kalloc_map
) {
6107 panic("vm_map_delete(%p,0x%llx,0x%llx): "
6108 "clipping %p at 0x%llx\n",
6115 vm_map_clip_start(map
, entry
, s
);
6117 if (entry
->vme_end
<= end
) {
6119 * This entry is going away completely, so no need
6120 * to clip and possibly cause an unnecessary unnesting.
6123 if ((flags
& VM_MAP_REMOVE_NO_MAP_ALIGN
) &&
6124 entry
->map_aligned
&&
6125 !VM_MAP_PAGE_ALIGNED(end
,
6126 VM_MAP_PAGE_MASK(map
))) {
6128 * The entry will no longer be map-aligned
6129 * after clipping and the caller said it's OK.
6131 entry
->map_aligned
= FALSE
;
6133 if (map
== kalloc_map
) {
6134 panic("vm_map_delete(%p,0x%llx,0x%llx): "
6135 "clipping %p at 0x%llx\n",
6142 vm_map_clip_end(map
, entry
, end
);
6145 if (entry
->permanent
) {
6146 panic("attempt to remove permanent VM map entry "
6147 "%p [0x%llx:0x%llx]\n",
6148 entry
, (uint64_t) s
, (uint64_t) end
);
6152 if (entry
->in_transition
) {
6153 wait_result_t wait_result
;
6156 * Another thread is wiring/unwiring this entry.
6157 * Let the other thread know we are waiting.
6159 assert(s
== entry
->vme_start
);
6160 entry
->needs_wakeup
= TRUE
;
6163 * wake up anybody waiting on entries that we have
6164 * already unwired/deleted.
6167 vm_map_entry_wakeup(map
);
6168 need_wakeup
= FALSE
;
6171 wait_result
= vm_map_entry_wait(map
, interruptible
);
6173 if (interruptible
&&
6174 wait_result
== THREAD_INTERRUPTED
) {
6176 * We do not clear the needs_wakeup flag,
6177 * since we cannot tell if we were the only one.
6179 return KERN_ABORTED
;
6183 * The entry could have been clipped or it
6184 * may not exist anymore. Look it up again.
6186 if (!vm_map_lookup_entry(map
, s
, &first_entry
)) {
6188 * User: use the next entry
6190 entry
= first_entry
->vme_next
;
6191 s
= entry
->vme_start
;
6193 entry
= first_entry
;
6194 SAVE_HINT_MAP_WRITE(map
, entry
->vme_prev
);
6196 last_timestamp
= map
->timestamp
;
6198 } /* end in_transition */
6200 if (entry
->wired_count
) {
6201 boolean_t user_wire
;
6203 user_wire
= entry
->user_wired_count
> 0;
6206 * Remove a kernel wiring if requested
6208 if (flags
& VM_MAP_REMOVE_KUNWIRE
) {
6209 entry
->wired_count
--;
6213 * Remove all user wirings for proper accounting
6215 if (entry
->user_wired_count
> 0) {
6216 while (entry
->user_wired_count
)
6217 subtract_wire_counts(map
, entry
, user_wire
);
6220 if (entry
->wired_count
!= 0) {
6221 assert(map
!= kernel_map
);
6223 * Cannot continue. Typical case is when
6224 * a user thread has physical io pending on
6225 * on this page. Either wait for the
6226 * kernel wiring to go away or return an
6229 if (flags
& VM_MAP_REMOVE_WAIT_FOR_KWIRE
) {
6230 wait_result_t wait_result
;
6232 assert(s
== entry
->vme_start
);
6233 entry
->needs_wakeup
= TRUE
;
6234 wait_result
= vm_map_entry_wait(map
,
6237 if (interruptible
&&
6238 wait_result
== THREAD_INTERRUPTED
) {
6240 * We do not clear the
6241 * needs_wakeup flag, since we
6242 * cannot tell if we were the
6245 return KERN_ABORTED
;
6249 * The entry could have been clipped or
6250 * it may not exist anymore. Look it
6253 if (!vm_map_lookup_entry(map
, s
,
6255 assert(map
!= kernel_map
);
6257 * User: use the next entry
6259 entry
= first_entry
->vme_next
;
6260 s
= entry
->vme_start
;
6262 entry
= first_entry
;
6263 SAVE_HINT_MAP_WRITE(map
, entry
->vme_prev
);
6265 last_timestamp
= map
->timestamp
;
6269 return KERN_FAILURE
;
6273 entry
->in_transition
= TRUE
;
6275 * copy current entry. see comment in vm_map_wire()
6278 assert(s
== entry
->vme_start
);
6281 * We can unlock the map now. The in_transition
6282 * state guarentees existance of the entry.
6286 if (tmp_entry
.is_sub_map
) {
6288 vm_map_offset_t sub_start
, sub_end
;
6290 vm_map_offset_t pmap_addr
;
6293 sub_map
= VME_SUBMAP(&tmp_entry
);
6294 sub_start
= VME_OFFSET(&tmp_entry
);
6295 sub_end
= sub_start
+ (tmp_entry
.vme_end
-
6296 tmp_entry
.vme_start
);
6297 if (tmp_entry
.use_pmap
) {
6298 pmap
= sub_map
->pmap
;
6299 pmap_addr
= tmp_entry
.vme_start
;
6302 pmap_addr
= tmp_entry
.vme_start
;
6304 (void) vm_map_unwire_nested(sub_map
,
6310 if (VME_OBJECT(&tmp_entry
) == kernel_object
) {
6311 pmap_protect_options(
6313 tmp_entry
.vme_start
,
6316 PMAP_OPTIONS_REMOVE
,
6319 vm_fault_unwire(map
, &tmp_entry
,
6320 VME_OBJECT(&tmp_entry
) == kernel_object
,
6321 map
->pmap
, tmp_entry
.vme_start
);
6326 if (last_timestamp
+1 != map
->timestamp
) {
6328 * Find the entry again. It could have
6329 * been clipped after we unlocked the map.
6331 if (!vm_map_lookup_entry(map
, s
, &first_entry
)){
6332 assert((map
!= kernel_map
) &&
6333 (!entry
->is_sub_map
));
6334 first_entry
= first_entry
->vme_next
;
6335 s
= first_entry
->vme_start
;
6337 SAVE_HINT_MAP_WRITE(map
, entry
->vme_prev
);
6340 SAVE_HINT_MAP_WRITE(map
, entry
->vme_prev
);
6341 first_entry
= entry
;
6344 last_timestamp
= map
->timestamp
;
6346 entry
= first_entry
;
6347 while ((entry
!= vm_map_to_entry(map
)) &&
6348 (entry
->vme_start
< tmp_entry
.vme_end
)) {
6349 assert(entry
->in_transition
);
6350 entry
->in_transition
= FALSE
;
6351 if (entry
->needs_wakeup
) {
6352 entry
->needs_wakeup
= FALSE
;
6355 entry
= entry
->vme_next
;
6358 * We have unwired the entry(s). Go back and
6361 entry
= first_entry
;
6365 /* entry is unwired */
6366 assert(entry
->wired_count
== 0);
6367 assert(entry
->user_wired_count
== 0);
6369 assert(s
== entry
->vme_start
);
6371 if (flags
& VM_MAP_REMOVE_NO_PMAP_CLEANUP
) {
6373 * XXX with the VM_MAP_REMOVE_SAVE_ENTRIES flag to
6374 * vm_map_delete(), some map entries might have been
6375 * transferred to a "zap_map", which doesn't have a
6376 * pmap. The original pmap has already been flushed
6377 * in the vm_map_delete() call targeting the original
6378 * map, but when we get to destroying the "zap_map",
6379 * we don't have any pmap to flush, so let's just skip
6382 } else if (entry
->is_sub_map
) {
6383 if (entry
->use_pmap
) {
6384 #ifndef NO_NESTED_PMAP
6387 if (flags
& VM_MAP_REMOVE_NO_UNNESTING
) {
6389 * This is the final cleanup of the
6390 * address space being terminated.
6391 * No new mappings are expected and
6392 * we don't really need to unnest the
6393 * shared region (and lose the "global"
6394 * pmap mappings, if applicable).
6396 * Tell the pmap layer that we're
6397 * "clean" wrt nesting.
6399 pmap_flags
= PMAP_UNNEST_CLEAN
;
6402 * We're unmapping part of the nested
6403 * shared region, so we can't keep the
6408 pmap_unnest_options(
6410 (addr64_t
)entry
->vme_start
,
6411 entry
->vme_end
- entry
->vme_start
,
6413 #endif /* NO_NESTED_PMAP */
6414 if ((map
->mapped_in_other_pmaps
) && (map
->ref_count
)) {
6415 /* clean up parent map/maps */
6416 vm_map_submap_pmap_clean(
6417 map
, entry
->vme_start
,
6423 vm_map_submap_pmap_clean(
6424 map
, entry
->vme_start
, entry
->vme_end
,
6428 } else if (VME_OBJECT(entry
) != kernel_object
&&
6429 VME_OBJECT(entry
) != compressor_object
) {
6430 object
= VME_OBJECT(entry
);
6431 if ((map
->mapped_in_other_pmaps
) && (map
->ref_count
)) {
6432 vm_object_pmap_protect_options(
6433 object
, VME_OFFSET(entry
),
6434 entry
->vme_end
- entry
->vme_start
,
6438 PMAP_OPTIONS_REMOVE
);
6439 } else if ((VME_OBJECT(entry
) != VM_OBJECT_NULL
) ||
6440 (map
->pmap
== kernel_pmap
)) {
6441 /* Remove translations associated
6442 * with this range unless the entry
6443 * does not have an object, or
6444 * it's the kernel map or a descendant
6445 * since the platform could potentially
6446 * create "backdoor" mappings invisible
6447 * to the VM. It is expected that
6448 * objectless, non-kernel ranges
6449 * do not have such VM invisible
6452 pmap_remove_options(map
->pmap
,
6453 (addr64_t
)entry
->vme_start
,
6454 (addr64_t
)entry
->vme_end
,
6455 PMAP_OPTIONS_REMOVE
);
6459 if (entry
->iokit_acct
) {
6460 /* alternate accounting */
6461 vm_map_iokit_unmapped_region(map
,
6464 entry
->iokit_acct
= FALSE
;
6468 * All pmap mappings for this map entry must have been
6472 assert(vm_map_pmap_is_empty(map
,
6477 next
= entry
->vme_next
;
6479 if (map
->pmap
== kernel_pmap
&&
6480 map
->ref_count
!= 0 &&
6481 entry
->vme_end
< end
&&
6482 (next
== vm_map_to_entry(map
) ||
6483 next
->vme_start
!= entry
->vme_end
)) {
6484 panic("vm_map_delete(%p,0x%llx,0x%llx): "
6485 "hole after %p at 0x%llx\n",
6490 (uint64_t)entry
->vme_end
);
6493 s
= next
->vme_start
;
6494 last_timestamp
= map
->timestamp
;
6496 if ((flags
& VM_MAP_REMOVE_SAVE_ENTRIES
) &&
6497 zap_map
!= VM_MAP_NULL
) {
6498 vm_map_size_t entry_size
;
6500 * The caller wants to save the affected VM map entries
6501 * into the "zap_map". The caller will take care of
6504 /* unlink the entry from "map" ... */
6505 vm_map_store_entry_unlink(map
, entry
);
6506 /* ... and add it to the end of the "zap_map" */
6507 vm_map_store_entry_link(zap_map
,
6508 vm_map_last_entry(zap_map
),
6510 entry_size
= entry
->vme_end
- entry
->vme_start
;
6511 map
->size
-= entry_size
;
6512 zap_map
->size
+= entry_size
;
6513 /* we didn't unlock the map, so no timestamp increase */
6516 vm_map_entry_delete(map
, entry
);
6517 /* vm_map_entry_delete unlocks the map */
6523 if(entry
== vm_map_to_entry(map
)) {
6526 if (last_timestamp
+1 != map
->timestamp
) {
6528 * we are responsible for deleting everything
6529 * from the give space, if someone has interfered
6530 * we pick up where we left off, back fills should
6531 * be all right for anyone except map_delete and
6532 * we have to assume that the task has been fully
6533 * disabled before we get here
6535 if (!vm_map_lookup_entry(map
, s
, &entry
)){
6536 entry
= entry
->vme_next
;
6537 s
= entry
->vme_start
;
6539 SAVE_HINT_MAP_WRITE(map
, entry
->vme_prev
);
6542 * others can not only allocate behind us, we can
6543 * also see coalesce while we don't have the map lock
6545 if(entry
== vm_map_to_entry(map
)) {
6549 last_timestamp
= map
->timestamp
;
6552 if (map
->wait_for_space
)
6553 thread_wakeup((event_t
) map
);
6555 * wake up anybody waiting on entries that we have already deleted.
6558 vm_map_entry_wakeup(map
);
6560 return KERN_SUCCESS
;
6566 * Remove the given address range from the target map.
6567 * This is the exported form of vm_map_delete.
6571 register vm_map_t map
,
6572 register vm_map_offset_t start
,
6573 register vm_map_offset_t end
,
6574 register boolean_t flags
)
6576 register kern_return_t result
;
6579 VM_MAP_RANGE_CHECK(map
, start
, end
);
6581 * For the zone_map, the kernel controls the allocation/freeing of memory.
6582 * Any free to the zone_map should be within the bounds of the map and
6583 * should free up memory. If the VM_MAP_RANGE_CHECK() silently converts a
6584 * free to the zone_map into a no-op, there is a problem and we should
6587 if ((map
== zone_map
) && (start
== end
))
6588 panic("Nothing being freed to the zone_map. start = end = %p\n", (void *)start
);
6589 result
= vm_map_delete(map
, start
, end
, flags
, VM_MAP_NULL
);
6597 * Routine: vm_map_copy_discard
6600 * Dispose of a map copy object (returned by
6604 vm_map_copy_discard(
6607 if (copy
== VM_MAP_COPY_NULL
)
6610 switch (copy
->type
) {
6611 case VM_MAP_COPY_ENTRY_LIST
:
6612 while (vm_map_copy_first_entry(copy
) !=
6613 vm_map_copy_to_entry(copy
)) {
6614 vm_map_entry_t entry
= vm_map_copy_first_entry(copy
);
6616 vm_map_copy_entry_unlink(copy
, entry
);
6617 if (entry
->is_sub_map
) {
6618 vm_map_deallocate(VME_SUBMAP(entry
));
6620 vm_object_deallocate(VME_OBJECT(entry
));
6622 vm_map_copy_entry_dispose(copy
, entry
);
6625 case VM_MAP_COPY_OBJECT
:
6626 vm_object_deallocate(copy
->cpy_object
);
6628 case VM_MAP_COPY_KERNEL_BUFFER
:
6631 * The vm_map_copy_t and possibly the data buffer were
6632 * allocated by a single call to kalloc(), i.e. the
6633 * vm_map_copy_t was not allocated out of the zone.
6635 if (copy
->size
> msg_ool_size_small
|| copy
->offset
)
6636 panic("Invalid vm_map_copy_t sz:%lld, ofst:%lld",
6637 (long long)copy
->size
, (long long)copy
->offset
);
6638 kfree(copy
, copy
->size
+ cpy_kdata_hdr_sz
);
6641 zfree(vm_map_copy_zone
, copy
);
6645 * Routine: vm_map_copy_copy
6648 * Move the information in a map copy object to
6649 * a new map copy object, leaving the old one
6652 * This is used by kernel routines that need
6653 * to look at out-of-line data (in copyin form)
6654 * before deciding whether to return SUCCESS.
6655 * If the routine returns FAILURE, the original
6656 * copy object will be deallocated; therefore,
6657 * these routines must make a copy of the copy
6658 * object and leave the original empty so that
6659 * deallocation will not fail.
6665 vm_map_copy_t new_copy
;
6667 if (copy
== VM_MAP_COPY_NULL
)
6668 return VM_MAP_COPY_NULL
;
6671 * Allocate a new copy object, and copy the information
6672 * from the old one into it.
6675 new_copy
= (vm_map_copy_t
) zalloc(vm_map_copy_zone
);
6676 new_copy
->c_u
.hdr
.rb_head_store
.rbh_root
= (void*)(int)SKIP_RB_TREE
;
6679 if (copy
->type
== VM_MAP_COPY_ENTRY_LIST
) {
6681 * The links in the entry chain must be
6682 * changed to point to the new copy object.
6684 vm_map_copy_first_entry(copy
)->vme_prev
6685 = vm_map_copy_to_entry(new_copy
);
6686 vm_map_copy_last_entry(copy
)->vme_next
6687 = vm_map_copy_to_entry(new_copy
);
6691 * Change the old copy object into one that contains
6692 * nothing to be deallocated.
6694 copy
->type
= VM_MAP_COPY_OBJECT
;
6695 copy
->cpy_object
= VM_OBJECT_NULL
;
6698 * Return the new object.
6703 static kern_return_t
6704 vm_map_overwrite_submap_recurse(
6706 vm_map_offset_t dst_addr
,
6707 vm_map_size_t dst_size
)
6709 vm_map_offset_t dst_end
;
6710 vm_map_entry_t tmp_entry
;
6711 vm_map_entry_t entry
;
6712 kern_return_t result
;
6713 boolean_t encountered_sub_map
= FALSE
;
6718 * Verify that the destination is all writeable
6719 * initially. We have to trunc the destination
6720 * address and round the copy size or we'll end up
6721 * splitting entries in strange ways.
6724 dst_end
= vm_map_round_page(dst_addr
+ dst_size
,
6725 VM_MAP_PAGE_MASK(dst_map
));
6726 vm_map_lock(dst_map
);
6729 if (!vm_map_lookup_entry(dst_map
, dst_addr
, &tmp_entry
)) {
6730 vm_map_unlock(dst_map
);
6731 return(KERN_INVALID_ADDRESS
);
6734 vm_map_clip_start(dst_map
,
6736 vm_map_trunc_page(dst_addr
,
6737 VM_MAP_PAGE_MASK(dst_map
)));
6738 if (tmp_entry
->is_sub_map
) {
6739 /* clipping did unnest if needed */
6740 assert(!tmp_entry
->use_pmap
);
6743 for (entry
= tmp_entry
;;) {
6744 vm_map_entry_t next
;
6746 next
= entry
->vme_next
;
6747 while(entry
->is_sub_map
) {
6748 vm_map_offset_t sub_start
;
6749 vm_map_offset_t sub_end
;
6750 vm_map_offset_t local_end
;
6752 if (entry
->in_transition
) {
6754 * Say that we are waiting, and wait for entry.
6756 entry
->needs_wakeup
= TRUE
;
6757 vm_map_entry_wait(dst_map
, THREAD_UNINT
);
6762 encountered_sub_map
= TRUE
;
6763 sub_start
= VME_OFFSET(entry
);
6765 if(entry
->vme_end
< dst_end
)
6766 sub_end
= entry
->vme_end
;
6769 sub_end
-= entry
->vme_start
;
6770 sub_end
+= VME_OFFSET(entry
);
6771 local_end
= entry
->vme_end
;
6772 vm_map_unlock(dst_map
);
6774 result
= vm_map_overwrite_submap_recurse(
6777 sub_end
- sub_start
);
6779 if(result
!= KERN_SUCCESS
)
6781 if (dst_end
<= entry
->vme_end
)
6782 return KERN_SUCCESS
;
6783 vm_map_lock(dst_map
);
6784 if(!vm_map_lookup_entry(dst_map
, local_end
,
6786 vm_map_unlock(dst_map
);
6787 return(KERN_INVALID_ADDRESS
);
6790 next
= entry
->vme_next
;
6793 if ( ! (entry
->protection
& VM_PROT_WRITE
)) {
6794 vm_map_unlock(dst_map
);
6795 return(KERN_PROTECTION_FAILURE
);
6799 * If the entry is in transition, we must wait
6800 * for it to exit that state. Anything could happen
6801 * when we unlock the map, so start over.
6803 if (entry
->in_transition
) {
6806 * Say that we are waiting, and wait for entry.
6808 entry
->needs_wakeup
= TRUE
;
6809 vm_map_entry_wait(dst_map
, THREAD_UNINT
);
6815 * our range is contained completely within this map entry
6817 if (dst_end
<= entry
->vme_end
) {
6818 vm_map_unlock(dst_map
);
6819 return KERN_SUCCESS
;
6822 * check that range specified is contiguous region
6824 if ((next
== vm_map_to_entry(dst_map
)) ||
6825 (next
->vme_start
!= entry
->vme_end
)) {
6826 vm_map_unlock(dst_map
);
6827 return(KERN_INVALID_ADDRESS
);
6831 * Check for permanent objects in the destination.
6833 if ((VME_OBJECT(entry
) != VM_OBJECT_NULL
) &&
6834 ((!VME_OBJECT(entry
)->internal
) ||
6835 (VME_OBJECT(entry
)->true_share
))) {
6836 if(encountered_sub_map
) {
6837 vm_map_unlock(dst_map
);
6838 return(KERN_FAILURE
);
6845 vm_map_unlock(dst_map
);
6846 return(KERN_SUCCESS
);
6850 * Routine: vm_map_copy_overwrite
6853 * Copy the memory described by the map copy
6854 * object (copy; returned by vm_map_copyin) onto
6855 * the specified destination region (dst_map, dst_addr).
6856 * The destination must be writeable.
6858 * Unlike vm_map_copyout, this routine actually
6859 * writes over previously-mapped memory. If the
6860 * previous mapping was to a permanent (user-supplied)
6861 * memory object, it is preserved.
6863 * The attributes (protection and inheritance) of the
6864 * destination region are preserved.
6866 * If successful, consumes the copy object.
6867 * Otherwise, the caller is responsible for it.
6869 * Implementation notes:
6870 * To overwrite aligned temporary virtual memory, it is
6871 * sufficient to remove the previous mapping and insert
6872 * the new copy. This replacement is done either on
6873 * the whole region (if no permanent virtual memory
6874 * objects are embedded in the destination region) or
6875 * in individual map entries.
6877 * To overwrite permanent virtual memory , it is necessary
6878 * to copy each page, as the external memory management
6879 * interface currently does not provide any optimizations.
6881 * Unaligned memory also has to be copied. It is possible
6882 * to use 'vm_trickery' to copy the aligned data. This is
6883 * not done but not hard to implement.
6885 * Once a page of permanent memory has been overwritten,
6886 * it is impossible to interrupt this function; otherwise,
6887 * the call would be neither atomic nor location-independent.
6888 * The kernel-state portion of a user thread must be
6891 * It may be expensive to forward all requests that might
6892 * overwrite permanent memory (vm_write, vm_copy) to
6893 * uninterruptible kernel threads. This routine may be
6894 * called by interruptible threads; however, success is
6895 * not guaranteed -- if the request cannot be performed
6896 * atomically and interruptibly, an error indication is
6900 static kern_return_t
6901 vm_map_copy_overwrite_nested(
6903 vm_map_address_t dst_addr
,
6905 boolean_t interruptible
,
6907 boolean_t discard_on_success
)
6909 vm_map_offset_t dst_end
;
6910 vm_map_entry_t tmp_entry
;
6911 vm_map_entry_t entry
;
6913 boolean_t aligned
= TRUE
;
6914 boolean_t contains_permanent_objects
= FALSE
;
6915 boolean_t encountered_sub_map
= FALSE
;
6916 vm_map_offset_t base_addr
;
6917 vm_map_size_t copy_size
;
6918 vm_map_size_t total_size
;
6922 * Check for null copy object.
6925 if (copy
== VM_MAP_COPY_NULL
)
6926 return(KERN_SUCCESS
);
6929 * Check for special kernel buffer allocated
6930 * by new_ipc_kmsg_copyin.
6933 if (copy
->type
== VM_MAP_COPY_KERNEL_BUFFER
) {
6934 return(vm_map_copyout_kernel_buffer(
6936 copy
, TRUE
, discard_on_success
));
6940 * Only works for entry lists at the moment. Will
6941 * support page lists later.
6944 assert(copy
->type
== VM_MAP_COPY_ENTRY_LIST
);
6946 if (copy
->size
== 0) {
6947 if (discard_on_success
)
6948 vm_map_copy_discard(copy
);
6949 return(KERN_SUCCESS
);
6953 * Verify that the destination is all writeable
6954 * initially. We have to trunc the destination
6955 * address and round the copy size or we'll end up
6956 * splitting entries in strange ways.
6959 if (!VM_MAP_PAGE_ALIGNED(copy
->size
,
6960 VM_MAP_PAGE_MASK(dst_map
)) ||
6961 !VM_MAP_PAGE_ALIGNED(copy
->offset
,
6962 VM_MAP_PAGE_MASK(dst_map
)) ||
6963 !VM_MAP_PAGE_ALIGNED(dst_addr
,
6964 VM_MAP_PAGE_MASK(dst_map
)))
6967 dst_end
= vm_map_round_page(dst_addr
+ copy
->size
,
6968 VM_MAP_PAGE_MASK(dst_map
));
6970 dst_end
= dst_addr
+ copy
->size
;
6973 vm_map_lock(dst_map
);
6975 /* LP64todo - remove this check when vm_map_commpage64()
6976 * no longer has to stuff in a map_entry for the commpage
6977 * above the map's max_offset.
6979 if (dst_addr
>= dst_map
->max_offset
) {
6980 vm_map_unlock(dst_map
);
6981 return(KERN_INVALID_ADDRESS
);
6985 if (!vm_map_lookup_entry(dst_map
, dst_addr
, &tmp_entry
)) {
6986 vm_map_unlock(dst_map
);
6987 return(KERN_INVALID_ADDRESS
);
6989 vm_map_clip_start(dst_map
,
6991 vm_map_trunc_page(dst_addr
,
6992 VM_MAP_PAGE_MASK(dst_map
)));
6993 for (entry
= tmp_entry
;;) {
6994 vm_map_entry_t next
= entry
->vme_next
;
6996 while(entry
->is_sub_map
) {
6997 vm_map_offset_t sub_start
;
6998 vm_map_offset_t sub_end
;
6999 vm_map_offset_t local_end
;
7001 if (entry
->in_transition
) {
7004 * Say that we are waiting, and wait for entry.
7006 entry
->needs_wakeup
= TRUE
;
7007 vm_map_entry_wait(dst_map
, THREAD_UNINT
);
7012 local_end
= entry
->vme_end
;
7013 if (!(entry
->needs_copy
)) {
7014 /* if needs_copy we are a COW submap */
7015 /* in such a case we just replace so */
7016 /* there is no need for the follow- */
7018 encountered_sub_map
= TRUE
;
7019 sub_start
= VME_OFFSET(entry
);
7021 if(entry
->vme_end
< dst_end
)
7022 sub_end
= entry
->vme_end
;
7025 sub_end
-= entry
->vme_start
;
7026 sub_end
+= VME_OFFSET(entry
);
7027 vm_map_unlock(dst_map
);
7029 kr
= vm_map_overwrite_submap_recurse(
7032 sub_end
- sub_start
);
7033 if(kr
!= KERN_SUCCESS
)
7035 vm_map_lock(dst_map
);
7038 if (dst_end
<= entry
->vme_end
)
7039 goto start_overwrite
;
7040 if(!vm_map_lookup_entry(dst_map
, local_end
,
7042 vm_map_unlock(dst_map
);
7043 return(KERN_INVALID_ADDRESS
);
7045 next
= entry
->vme_next
;
7048 if ( ! (entry
->protection
& VM_PROT_WRITE
)) {
7049 vm_map_unlock(dst_map
);
7050 return(KERN_PROTECTION_FAILURE
);
7054 * If the entry is in transition, we must wait
7055 * for it to exit that state. Anything could happen
7056 * when we unlock the map, so start over.
7058 if (entry
->in_transition
) {
7061 * Say that we are waiting, and wait for entry.
7063 entry
->needs_wakeup
= TRUE
;
7064 vm_map_entry_wait(dst_map
, THREAD_UNINT
);
7070 * our range is contained completely within this map entry
7072 if (dst_end
<= entry
->vme_end
)
7075 * check that range specified is contiguous region
7077 if ((next
== vm_map_to_entry(dst_map
)) ||
7078 (next
->vme_start
!= entry
->vme_end
)) {
7079 vm_map_unlock(dst_map
);
7080 return(KERN_INVALID_ADDRESS
);
7085 * Check for permanent objects in the destination.
7087 if ((VME_OBJECT(entry
) != VM_OBJECT_NULL
) &&
7088 ((!VME_OBJECT(entry
)->internal
) ||
7089 (VME_OBJECT(entry
)->true_share
))) {
7090 contains_permanent_objects
= TRUE
;
7098 * If there are permanent objects in the destination, then
7099 * the copy cannot be interrupted.
7102 if (interruptible
&& contains_permanent_objects
) {
7103 vm_map_unlock(dst_map
);
7104 return(KERN_FAILURE
); /* XXX */
7109 * Make a second pass, overwriting the data
7110 * At the beginning of each loop iteration,
7111 * the next entry to be overwritten is "tmp_entry"
7112 * (initially, the value returned from the lookup above),
7113 * and the starting address expected in that entry
7117 total_size
= copy
->size
;
7118 if(encountered_sub_map
) {
7120 /* re-calculate tmp_entry since we've had the map */
7122 if (!vm_map_lookup_entry( dst_map
, dst_addr
, &tmp_entry
)) {
7123 vm_map_unlock(dst_map
);
7124 return(KERN_INVALID_ADDRESS
);
7127 copy_size
= copy
->size
;
7130 base_addr
= dst_addr
;
7132 /* deconstruct the copy object and do in parts */
7133 /* only in sub_map, interruptable case */
7134 vm_map_entry_t copy_entry
;
7135 vm_map_entry_t previous_prev
= VM_MAP_ENTRY_NULL
;
7136 vm_map_entry_t next_copy
= VM_MAP_ENTRY_NULL
;
7138 int remaining_entries
= 0;
7139 vm_map_offset_t new_offset
= 0;
7141 for (entry
= tmp_entry
; copy_size
== 0;) {
7142 vm_map_entry_t next
;
7144 next
= entry
->vme_next
;
7146 /* tmp_entry and base address are moved along */
7147 /* each time we encounter a sub-map. Otherwise */
7148 /* entry can outpase tmp_entry, and the copy_size */
7149 /* may reflect the distance between them */
7150 /* if the current entry is found to be in transition */
7151 /* we will start over at the beginning or the last */
7152 /* encounter of a submap as dictated by base_addr */
7153 /* we will zero copy_size accordingly. */
7154 if (entry
->in_transition
) {
7156 * Say that we are waiting, and wait for entry.
7158 entry
->needs_wakeup
= TRUE
;
7159 vm_map_entry_wait(dst_map
, THREAD_UNINT
);
7161 if(!vm_map_lookup_entry(dst_map
, base_addr
,
7163 vm_map_unlock(dst_map
);
7164 return(KERN_INVALID_ADDRESS
);
7170 if(entry
->is_sub_map
) {
7171 vm_map_offset_t sub_start
;
7172 vm_map_offset_t sub_end
;
7173 vm_map_offset_t local_end
;
7175 if (entry
->needs_copy
) {
7176 /* if this is a COW submap */
7177 /* just back the range with a */
7178 /* anonymous entry */
7179 if(entry
->vme_end
< dst_end
)
7180 sub_end
= entry
->vme_end
;
7183 if(entry
->vme_start
< base_addr
)
7184 sub_start
= base_addr
;
7186 sub_start
= entry
->vme_start
;
7188 dst_map
, entry
, sub_end
);
7190 dst_map
, entry
, sub_start
);
7191 assert(!entry
->use_pmap
);
7192 entry
->is_sub_map
= FALSE
;
7195 VME_SUBMAP_SET(entry
, NULL
);
7196 entry
->is_shared
= FALSE
;
7197 entry
->needs_copy
= FALSE
;
7198 VME_OFFSET_SET(entry
, 0);
7201 * We should propagate the protections
7202 * of the submap entry here instead
7203 * of forcing them to VM_PROT_ALL...
7204 * Or better yet, we should inherit
7205 * the protection of the copy_entry.
7207 entry
->protection
= VM_PROT_ALL
;
7208 entry
->max_protection
= VM_PROT_ALL
;
7209 entry
->wired_count
= 0;
7210 entry
->user_wired_count
= 0;
7211 if(entry
->inheritance
7212 == VM_INHERIT_SHARE
)
7213 entry
->inheritance
= VM_INHERIT_COPY
;
7216 /* first take care of any non-sub_map */
7217 /* entries to send */
7218 if(base_addr
< entry
->vme_start
) {
7221 entry
->vme_start
- base_addr
;
7224 sub_start
= VME_OFFSET(entry
);
7226 if(entry
->vme_end
< dst_end
)
7227 sub_end
= entry
->vme_end
;
7230 sub_end
-= entry
->vme_start
;
7231 sub_end
+= VME_OFFSET(entry
);
7232 local_end
= entry
->vme_end
;
7233 vm_map_unlock(dst_map
);
7234 copy_size
= sub_end
- sub_start
;
7236 /* adjust the copy object */
7237 if (total_size
> copy_size
) {
7238 vm_map_size_t local_size
= 0;
7239 vm_map_size_t entry_size
;
7242 new_offset
= copy
->offset
;
7243 copy_entry
= vm_map_copy_first_entry(copy
);
7245 vm_map_copy_to_entry(copy
)){
7246 entry_size
= copy_entry
->vme_end
-
7247 copy_entry
->vme_start
;
7248 if((local_size
< copy_size
) &&
7249 ((local_size
+ entry_size
)
7251 vm_map_copy_clip_end(copy
,
7253 copy_entry
->vme_start
+
7254 (copy_size
- local_size
));
7255 entry_size
= copy_entry
->vme_end
-
7256 copy_entry
->vme_start
;
7257 local_size
+= entry_size
;
7258 new_offset
+= entry_size
;
7260 if(local_size
>= copy_size
) {
7261 next_copy
= copy_entry
->vme_next
;
7262 copy_entry
->vme_next
=
7263 vm_map_copy_to_entry(copy
);
7265 copy
->cpy_hdr
.links
.prev
;
7266 copy
->cpy_hdr
.links
.prev
= copy_entry
;
7267 copy
->size
= copy_size
;
7269 copy
->cpy_hdr
.nentries
;
7270 remaining_entries
-= nentries
;
7271 copy
->cpy_hdr
.nentries
= nentries
;
7274 local_size
+= entry_size
;
7275 new_offset
+= entry_size
;
7278 copy_entry
= copy_entry
->vme_next
;
7282 if((entry
->use_pmap
) && (pmap
== NULL
)) {
7283 kr
= vm_map_copy_overwrite_nested(
7288 VME_SUBMAP(entry
)->pmap
,
7290 } else if (pmap
!= NULL
) {
7291 kr
= vm_map_copy_overwrite_nested(
7295 interruptible
, pmap
,
7298 kr
= vm_map_copy_overwrite_nested(
7306 if(kr
!= KERN_SUCCESS
) {
7307 if(next_copy
!= NULL
) {
7308 copy
->cpy_hdr
.nentries
+=
7310 copy
->cpy_hdr
.links
.prev
->vme_next
=
7312 copy
->cpy_hdr
.links
.prev
7314 copy
->size
= total_size
;
7318 if (dst_end
<= local_end
) {
7319 return(KERN_SUCCESS
);
7321 /* otherwise copy no longer exists, it was */
7322 /* destroyed after successful copy_overwrite */
7323 copy
= (vm_map_copy_t
)
7324 zalloc(vm_map_copy_zone
);
7325 copy
->c_u
.hdr
.rb_head_store
.rbh_root
= (void*)(int)SKIP_RB_TREE
;
7326 vm_map_copy_first_entry(copy
) =
7327 vm_map_copy_last_entry(copy
) =
7328 vm_map_copy_to_entry(copy
);
7329 copy
->type
= VM_MAP_COPY_ENTRY_LIST
;
7330 copy
->offset
= new_offset
;
7334 * this does not seem to deal with
7335 * the VM map store (R&B tree)
7338 total_size
-= copy_size
;
7340 /* put back remainder of copy in container */
7341 if(next_copy
!= NULL
) {
7342 copy
->cpy_hdr
.nentries
= remaining_entries
;
7343 copy
->cpy_hdr
.links
.next
= next_copy
;
7344 copy
->cpy_hdr
.links
.prev
= previous_prev
;
7345 copy
->size
= total_size
;
7346 next_copy
->vme_prev
=
7347 vm_map_copy_to_entry(copy
);
7350 base_addr
= local_end
;
7351 vm_map_lock(dst_map
);
7352 if(!vm_map_lookup_entry(dst_map
,
7353 local_end
, &tmp_entry
)) {
7354 vm_map_unlock(dst_map
);
7355 return(KERN_INVALID_ADDRESS
);
7360 if (dst_end
<= entry
->vme_end
) {
7361 copy_size
= dst_end
- base_addr
;
7365 if ((next
== vm_map_to_entry(dst_map
)) ||
7366 (next
->vme_start
!= entry
->vme_end
)) {
7367 vm_map_unlock(dst_map
);
7368 return(KERN_INVALID_ADDRESS
);
7377 /* adjust the copy object */
7378 if (total_size
> copy_size
) {
7379 vm_map_size_t local_size
= 0;
7380 vm_map_size_t entry_size
;
7382 new_offset
= copy
->offset
;
7383 copy_entry
= vm_map_copy_first_entry(copy
);
7384 while(copy_entry
!= vm_map_copy_to_entry(copy
)) {
7385 entry_size
= copy_entry
->vme_end
-
7386 copy_entry
->vme_start
;
7387 if((local_size
< copy_size
) &&
7388 ((local_size
+ entry_size
)
7390 vm_map_copy_clip_end(copy
, copy_entry
,
7391 copy_entry
->vme_start
+
7392 (copy_size
- local_size
));
7393 entry_size
= copy_entry
->vme_end
-
7394 copy_entry
->vme_start
;
7395 local_size
+= entry_size
;
7396 new_offset
+= entry_size
;
7398 if(local_size
>= copy_size
) {
7399 next_copy
= copy_entry
->vme_next
;
7400 copy_entry
->vme_next
=
7401 vm_map_copy_to_entry(copy
);
7403 copy
->cpy_hdr
.links
.prev
;
7404 copy
->cpy_hdr
.links
.prev
= copy_entry
;
7405 copy
->size
= copy_size
;
7407 copy
->cpy_hdr
.nentries
;
7408 remaining_entries
-= nentries
;
7409 copy
->cpy_hdr
.nentries
= nentries
;
7412 local_size
+= entry_size
;
7413 new_offset
+= entry_size
;
7416 copy_entry
= copy_entry
->vme_next
;
7426 local_pmap
= dst_map
->pmap
;
7428 if ((kr
= vm_map_copy_overwrite_aligned(
7429 dst_map
, tmp_entry
, copy
,
7430 base_addr
, local_pmap
)) != KERN_SUCCESS
) {
7431 if(next_copy
!= NULL
) {
7432 copy
->cpy_hdr
.nentries
+=
7434 copy
->cpy_hdr
.links
.prev
->vme_next
=
7436 copy
->cpy_hdr
.links
.prev
=
7438 copy
->size
+= copy_size
;
7442 vm_map_unlock(dst_map
);
7447 * if the copy and dst address are misaligned but the same
7448 * offset within the page we can copy_not_aligned the
7449 * misaligned parts and copy aligned the rest. If they are
7450 * aligned but len is unaligned we simply need to copy
7451 * the end bit unaligned. We'll need to split the misaligned
7452 * bits of the region in this case !
7454 /* ALWAYS UNLOCKS THE dst_map MAP */
7455 kr
= vm_map_copy_overwrite_unaligned(
7460 discard_on_success
);
7461 if (kr
!= KERN_SUCCESS
) {
7462 if(next_copy
!= NULL
) {
7463 copy
->cpy_hdr
.nentries
+=
7465 copy
->cpy_hdr
.links
.prev
->vme_next
=
7467 copy
->cpy_hdr
.links
.prev
=
7469 copy
->size
+= copy_size
;
7474 total_size
-= copy_size
;
7477 base_addr
+= copy_size
;
7479 copy
->offset
= new_offset
;
7480 if(next_copy
!= NULL
) {
7481 copy
->cpy_hdr
.nentries
= remaining_entries
;
7482 copy
->cpy_hdr
.links
.next
= next_copy
;
7483 copy
->cpy_hdr
.links
.prev
= previous_prev
;
7484 next_copy
->vme_prev
= vm_map_copy_to_entry(copy
);
7485 copy
->size
= total_size
;
7487 vm_map_lock(dst_map
);
7489 if (!vm_map_lookup_entry(dst_map
,
7490 base_addr
, &tmp_entry
)) {
7491 vm_map_unlock(dst_map
);
7492 return(KERN_INVALID_ADDRESS
);
7494 if (tmp_entry
->in_transition
) {
7495 entry
->needs_wakeup
= TRUE
;
7496 vm_map_entry_wait(dst_map
, THREAD_UNINT
);
7501 vm_map_clip_start(dst_map
,
7503 vm_map_trunc_page(base_addr
,
7504 VM_MAP_PAGE_MASK(dst_map
)));
7510 * Throw away the vm_map_copy object
7512 if (discard_on_success
)
7513 vm_map_copy_discard(copy
);
7515 return(KERN_SUCCESS
);
7516 }/* vm_map_copy_overwrite */
7519 vm_map_copy_overwrite(
7521 vm_map_offset_t dst_addr
,
7523 boolean_t interruptible
)
7525 vm_map_size_t head_size
, tail_size
;
7526 vm_map_copy_t head_copy
, tail_copy
;
7527 vm_map_offset_t head_addr
, tail_addr
;
7528 vm_map_entry_t entry
;
7538 if (interruptible
||
7539 copy
== VM_MAP_COPY_NULL
||
7540 copy
->type
!= VM_MAP_COPY_ENTRY_LIST
) {
7542 * We can't split the "copy" map if we're interruptible
7543 * or if we don't have a "copy" map...
7546 return vm_map_copy_overwrite_nested(dst_map
,
7554 if (copy
->size
< 3 * PAGE_SIZE
) {
7556 * Too small to bother with optimizing...
7561 if ((dst_addr
& VM_MAP_PAGE_MASK(dst_map
)) !=
7562 (copy
->offset
& VM_MAP_PAGE_MASK(dst_map
))) {
7564 * Incompatible mis-alignment of source and destination...
7570 * Proper alignment or identical mis-alignment at the beginning.
7571 * Let's try and do a small unaligned copy first (if needed)
7572 * and then an aligned copy for the rest.
7574 if (!page_aligned(dst_addr
)) {
7575 head_addr
= dst_addr
;
7576 head_size
= (VM_MAP_PAGE_SIZE(dst_map
) -
7577 (copy
->offset
& VM_MAP_PAGE_MASK(dst_map
)));
7579 if (!page_aligned(copy
->offset
+ copy
->size
)) {
7581 * Mis-alignment at the end.
7582 * Do an aligned copy up to the last page and
7583 * then an unaligned copy for the remaining bytes.
7585 tail_size
= ((copy
->offset
+ copy
->size
) &
7586 VM_MAP_PAGE_MASK(dst_map
));
7587 tail_addr
= dst_addr
+ copy
->size
- tail_size
;
7590 if (head_size
+ tail_size
== copy
->size
) {
7592 * It's all unaligned, no optimization possible...
7598 * Can't optimize if there are any submaps in the
7599 * destination due to the way we free the "copy" map
7600 * progressively in vm_map_copy_overwrite_nested()
7603 vm_map_lock_read(dst_map
);
7604 if (! vm_map_lookup_entry(dst_map
, dst_addr
, &entry
)) {
7605 vm_map_unlock_read(dst_map
);
7609 (entry
!= vm_map_copy_to_entry(copy
) &&
7610 entry
->vme_start
< dst_addr
+ copy
->size
);
7611 entry
= entry
->vme_next
) {
7612 if (entry
->is_sub_map
) {
7613 vm_map_unlock_read(dst_map
);
7617 vm_map_unlock_read(dst_map
);
7621 * Unaligned copy of the first "head_size" bytes, to reach
7626 * Extract "head_copy" out of "copy".
7628 head_copy
= (vm_map_copy_t
) zalloc(vm_map_copy_zone
);
7629 head_copy
->c_u
.hdr
.rb_head_store
.rbh_root
= (void*)(int)SKIP_RB_TREE
;
7630 vm_map_copy_first_entry(head_copy
) =
7631 vm_map_copy_to_entry(head_copy
);
7632 vm_map_copy_last_entry(head_copy
) =
7633 vm_map_copy_to_entry(head_copy
);
7634 head_copy
->type
= VM_MAP_COPY_ENTRY_LIST
;
7635 head_copy
->cpy_hdr
.nentries
= 0;
7636 head_copy
->cpy_hdr
.entries_pageable
=
7637 copy
->cpy_hdr
.entries_pageable
;
7638 vm_map_store_init(&head_copy
->cpy_hdr
);
7640 head_copy
->offset
= copy
->offset
;
7641 head_copy
->size
= head_size
;
7643 copy
->offset
+= head_size
;
7644 copy
->size
-= head_size
;
7646 entry
= vm_map_copy_first_entry(copy
);
7647 vm_map_copy_clip_end(copy
, entry
, copy
->offset
);
7648 vm_map_copy_entry_unlink(copy
, entry
);
7649 vm_map_copy_entry_link(head_copy
,
7650 vm_map_copy_to_entry(head_copy
),
7654 * Do the unaligned copy.
7656 kr
= vm_map_copy_overwrite_nested(dst_map
,
7662 if (kr
!= KERN_SUCCESS
)
7668 * Extract "tail_copy" out of "copy".
7670 tail_copy
= (vm_map_copy_t
) zalloc(vm_map_copy_zone
);
7671 tail_copy
->c_u
.hdr
.rb_head_store
.rbh_root
= (void*)(int)SKIP_RB_TREE
;
7672 vm_map_copy_first_entry(tail_copy
) =
7673 vm_map_copy_to_entry(tail_copy
);
7674 vm_map_copy_last_entry(tail_copy
) =
7675 vm_map_copy_to_entry(tail_copy
);
7676 tail_copy
->type
= VM_MAP_COPY_ENTRY_LIST
;
7677 tail_copy
->cpy_hdr
.nentries
= 0;
7678 tail_copy
->cpy_hdr
.entries_pageable
=
7679 copy
->cpy_hdr
.entries_pageable
;
7680 vm_map_store_init(&tail_copy
->cpy_hdr
);
7682 tail_copy
->offset
= copy
->offset
+ copy
->size
- tail_size
;
7683 tail_copy
->size
= tail_size
;
7685 copy
->size
-= tail_size
;
7687 entry
= vm_map_copy_last_entry(copy
);
7688 vm_map_copy_clip_start(copy
, entry
, tail_copy
->offset
);
7689 entry
= vm_map_copy_last_entry(copy
);
7690 vm_map_copy_entry_unlink(copy
, entry
);
7691 vm_map_copy_entry_link(tail_copy
,
7692 vm_map_copy_last_entry(tail_copy
),
7697 * Copy most (or possibly all) of the data.
7699 kr
= vm_map_copy_overwrite_nested(dst_map
,
7700 dst_addr
+ head_size
,
7705 if (kr
!= KERN_SUCCESS
) {
7710 kr
= vm_map_copy_overwrite_nested(dst_map
,
7719 assert(copy
->type
== VM_MAP_COPY_ENTRY_LIST
);
7720 if (kr
== KERN_SUCCESS
) {
7722 * Discard all the copy maps.
7725 vm_map_copy_discard(head_copy
);
7728 vm_map_copy_discard(copy
);
7730 vm_map_copy_discard(tail_copy
);
7735 * Re-assemble the original copy map.
7738 entry
= vm_map_copy_first_entry(head_copy
);
7739 vm_map_copy_entry_unlink(head_copy
, entry
);
7740 vm_map_copy_entry_link(copy
,
7741 vm_map_copy_to_entry(copy
),
7743 copy
->offset
-= head_size
;
7744 copy
->size
+= head_size
;
7745 vm_map_copy_discard(head_copy
);
7749 entry
= vm_map_copy_last_entry(tail_copy
);
7750 vm_map_copy_entry_unlink(tail_copy
, entry
);
7751 vm_map_copy_entry_link(copy
,
7752 vm_map_copy_last_entry(copy
),
7754 copy
->size
+= tail_size
;
7755 vm_map_copy_discard(tail_copy
);
7764 * Routine: vm_map_copy_overwrite_unaligned [internal use only]
7767 * Physically copy unaligned data
7770 * Unaligned parts of pages have to be physically copied. We use
7771 * a modified form of vm_fault_copy (which understands none-aligned
7772 * page offsets and sizes) to do the copy. We attempt to copy as
7773 * much memory in one go as possibly, however vm_fault_copy copies
7774 * within 1 memory object so we have to find the smaller of "amount left"
7775 * "source object data size" and "target object data size". With
7776 * unaligned data we don't need to split regions, therefore the source
7777 * (copy) object should be one map entry, the target range may be split
7778 * over multiple map entries however. In any event we are pessimistic
7779 * about these assumptions.
7782 * dst_map is locked on entry and is return locked on success,
7783 * unlocked on error.
7786 static kern_return_t
7787 vm_map_copy_overwrite_unaligned(
7789 vm_map_entry_t entry
,
7791 vm_map_offset_t start
,
7792 boolean_t discard_on_success
)
7794 vm_map_entry_t copy_entry
;
7795 vm_map_entry_t copy_entry_next
;
7796 vm_map_version_t version
;
7797 vm_object_t dst_object
;
7798 vm_object_offset_t dst_offset
;
7799 vm_object_offset_t src_offset
;
7800 vm_object_offset_t entry_offset
;
7801 vm_map_offset_t entry_end
;
7802 vm_map_size_t src_size
,
7806 kern_return_t kr
= KERN_SUCCESS
;
7809 copy_entry
= vm_map_copy_first_entry(copy
);
7811 vm_map_lock_write_to_read(dst_map
);
7813 src_offset
= copy
->offset
- vm_object_trunc_page(copy
->offset
);
7814 amount_left
= copy
->size
;
7816 * unaligned so we never clipped this entry, we need the offset into
7817 * the vm_object not just the data.
7819 while (amount_left
> 0) {
7821 if (entry
== vm_map_to_entry(dst_map
)) {
7822 vm_map_unlock_read(dst_map
);
7823 return KERN_INVALID_ADDRESS
;
7826 /* "start" must be within the current map entry */
7827 assert ((start
>=entry
->vme_start
) && (start
<entry
->vme_end
));
7829 dst_offset
= start
- entry
->vme_start
;
7831 dst_size
= entry
->vme_end
- start
;
7833 src_size
= copy_entry
->vme_end
-
7834 (copy_entry
->vme_start
+ src_offset
);
7836 if (dst_size
< src_size
) {
7838 * we can only copy dst_size bytes before
7839 * we have to get the next destination entry
7841 copy_size
= dst_size
;
7844 * we can only copy src_size bytes before
7845 * we have to get the next source copy entry
7847 copy_size
= src_size
;
7850 if (copy_size
> amount_left
) {
7851 copy_size
= amount_left
;
7854 * Entry needs copy, create a shadow shadow object for
7855 * Copy on write region.
7857 if (entry
->needs_copy
&&
7858 ((entry
->protection
& VM_PROT_WRITE
) != 0))
7860 if (vm_map_lock_read_to_write(dst_map
)) {
7861 vm_map_lock_read(dst_map
);
7864 VME_OBJECT_SHADOW(entry
,
7865 (vm_map_size_t
)(entry
->vme_end
7866 - entry
->vme_start
));
7867 entry
->needs_copy
= FALSE
;
7868 vm_map_lock_write_to_read(dst_map
);
7870 dst_object
= VME_OBJECT(entry
);
7872 * unlike with the virtual (aligned) copy we're going
7873 * to fault on it therefore we need a target object.
7875 if (dst_object
== VM_OBJECT_NULL
) {
7876 if (vm_map_lock_read_to_write(dst_map
)) {
7877 vm_map_lock_read(dst_map
);
7880 dst_object
= vm_object_allocate((vm_map_size_t
)
7881 entry
->vme_end
- entry
->vme_start
);
7882 VME_OBJECT(entry
) = dst_object
;
7883 VME_OFFSET_SET(entry
, 0);
7884 assert(entry
->use_pmap
);
7885 vm_map_lock_write_to_read(dst_map
);
7888 * Take an object reference and unlock map. The "entry" may
7889 * disappear or change when the map is unlocked.
7891 vm_object_reference(dst_object
);
7892 version
.main_timestamp
= dst_map
->timestamp
;
7893 entry_offset
= VME_OFFSET(entry
);
7894 entry_end
= entry
->vme_end
;
7895 vm_map_unlock_read(dst_map
);
7897 * Copy as much as possible in one pass
7900 VME_OBJECT(copy_entry
),
7901 VME_OFFSET(copy_entry
) + src_offset
,
7904 entry_offset
+ dst_offset
,
7910 src_offset
+= copy_size
;
7911 amount_left
-= copy_size
;
7913 * Release the object reference
7915 vm_object_deallocate(dst_object
);
7917 * If a hard error occurred, return it now
7919 if (kr
!= KERN_SUCCESS
)
7922 if ((copy_entry
->vme_start
+ src_offset
) == copy_entry
->vme_end
7923 || amount_left
== 0)
7926 * all done with this copy entry, dispose.
7928 copy_entry_next
= copy_entry
->vme_next
;
7930 if (discard_on_success
) {
7931 vm_map_copy_entry_unlink(copy
, copy_entry
);
7932 assert(!copy_entry
->is_sub_map
);
7933 vm_object_deallocate(VME_OBJECT(copy_entry
));
7934 vm_map_copy_entry_dispose(copy
, copy_entry
);
7937 if (copy_entry_next
== vm_map_copy_to_entry(copy
) &&
7940 * not finished copying but run out of source
7942 return KERN_INVALID_ADDRESS
;
7945 copy_entry
= copy_entry_next
;
7950 if (amount_left
== 0)
7951 return KERN_SUCCESS
;
7953 vm_map_lock_read(dst_map
);
7954 if (version
.main_timestamp
== dst_map
->timestamp
) {
7955 if (start
== entry_end
) {
7957 * destination region is split. Use the version
7958 * information to avoid a lookup in the normal
7961 entry
= entry
->vme_next
;
7963 * should be contiguous. Fail if we encounter
7964 * a hole in the destination.
7966 if (start
!= entry
->vme_start
) {
7967 vm_map_unlock_read(dst_map
);
7968 return KERN_INVALID_ADDRESS
;
7973 * Map version check failed.
7974 * we must lookup the entry because somebody
7975 * might have changed the map behind our backs.
7978 if (!vm_map_lookup_entry(dst_map
, start
, &entry
))
7980 vm_map_unlock_read(dst_map
);
7981 return KERN_INVALID_ADDRESS
;
7986 return KERN_SUCCESS
;
7987 }/* vm_map_copy_overwrite_unaligned */
7990 * Routine: vm_map_copy_overwrite_aligned [internal use only]
7993 * Does all the vm_trickery possible for whole pages.
7997 * If there are no permanent objects in the destination,
7998 * and the source and destination map entry zones match,
7999 * and the destination map entry is not shared,
8000 * then the map entries can be deleted and replaced
8001 * with those from the copy. The following code is the
8002 * basic idea of what to do, but there are lots of annoying
8003 * little details about getting protection and inheritance
8004 * right. Should add protection, inheritance, and sharing checks
8005 * to the above pass and make sure that no wiring is involved.
8008 int vm_map_copy_overwrite_aligned_src_not_internal
= 0;
8009 int vm_map_copy_overwrite_aligned_src_not_symmetric
= 0;
8010 int vm_map_copy_overwrite_aligned_src_large
= 0;
8012 static kern_return_t
8013 vm_map_copy_overwrite_aligned(
8015 vm_map_entry_t tmp_entry
,
8017 vm_map_offset_t start
,
8018 __unused pmap_t pmap
)
8021 vm_map_entry_t copy_entry
;
8022 vm_map_size_t copy_size
;
8024 vm_map_entry_t entry
;
8026 while ((copy_entry
= vm_map_copy_first_entry(copy
))
8027 != vm_map_copy_to_entry(copy
))
8029 copy_size
= (copy_entry
->vme_end
- copy_entry
->vme_start
);
8032 if (entry
->is_sub_map
) {
8033 /* unnested when clipped earlier */
8034 assert(!entry
->use_pmap
);
8036 if (entry
== vm_map_to_entry(dst_map
)) {
8037 vm_map_unlock(dst_map
);
8038 return KERN_INVALID_ADDRESS
;
8040 size
= (entry
->vme_end
- entry
->vme_start
);
8042 * Make sure that no holes popped up in the
8043 * address map, and that the protection is
8044 * still valid, in case the map was unlocked
8048 if ((entry
->vme_start
!= start
) || ((entry
->is_sub_map
)
8049 && !entry
->needs_copy
)) {
8050 vm_map_unlock(dst_map
);
8051 return(KERN_INVALID_ADDRESS
);
8053 assert(entry
!= vm_map_to_entry(dst_map
));
8056 * Check protection again
8059 if ( ! (entry
->protection
& VM_PROT_WRITE
)) {
8060 vm_map_unlock(dst_map
);
8061 return(KERN_PROTECTION_FAILURE
);
8065 * Adjust to source size first
8068 if (copy_size
< size
) {
8069 if (entry
->map_aligned
&&
8070 !VM_MAP_PAGE_ALIGNED(entry
->vme_start
+ copy_size
,
8071 VM_MAP_PAGE_MASK(dst_map
))) {
8072 /* no longer map-aligned */
8073 entry
->map_aligned
= FALSE
;
8075 vm_map_clip_end(dst_map
, entry
, entry
->vme_start
+ copy_size
);
8080 * Adjust to destination size
8083 if (size
< copy_size
) {
8084 vm_map_copy_clip_end(copy
, copy_entry
,
8085 copy_entry
->vme_start
+ size
);
8089 assert((entry
->vme_end
- entry
->vme_start
) == size
);
8090 assert((tmp_entry
->vme_end
- tmp_entry
->vme_start
) == size
);
8091 assert((copy_entry
->vme_end
- copy_entry
->vme_start
) == size
);
8094 * If the destination contains temporary unshared memory,
8095 * we can perform the copy by throwing it away and
8096 * installing the source data.
8099 object
= VME_OBJECT(entry
);
8100 if ((!entry
->is_shared
&&
8101 ((object
== VM_OBJECT_NULL
) ||
8102 (object
->internal
&& !object
->true_share
))) ||
8103 entry
->needs_copy
) {
8104 vm_object_t old_object
= VME_OBJECT(entry
);
8105 vm_object_offset_t old_offset
= VME_OFFSET(entry
);
8106 vm_object_offset_t offset
;
8109 * Ensure that the source and destination aren't
8112 if (old_object
== VME_OBJECT(copy_entry
) &&
8113 old_offset
== VME_OFFSET(copy_entry
)) {
8114 vm_map_copy_entry_unlink(copy
, copy_entry
);
8115 vm_map_copy_entry_dispose(copy
, copy_entry
);
8117 if (old_object
!= VM_OBJECT_NULL
)
8118 vm_object_deallocate(old_object
);
8120 start
= tmp_entry
->vme_end
;
8121 tmp_entry
= tmp_entry
->vme_next
;
8125 #define __TRADEOFF1_OBJ_SIZE (64 * 1024 * 1024) /* 64 MB */
8126 #define __TRADEOFF1_COPY_SIZE (128 * 1024) /* 128 KB */
8127 if (VME_OBJECT(copy_entry
) != VM_OBJECT_NULL
&&
8128 VME_OBJECT(copy_entry
)->vo_size
>= __TRADEOFF1_OBJ_SIZE
&&
8129 copy_size
<= __TRADEOFF1_COPY_SIZE
) {
8131 * Virtual vs. Physical copy tradeoff #1.
8133 * Copying only a few pages out of a large
8134 * object: do a physical copy instead of
8135 * a virtual copy, to avoid possibly keeping
8136 * the entire large object alive because of
8137 * those few copy-on-write pages.
8139 vm_map_copy_overwrite_aligned_src_large
++;
8143 if ((dst_map
->pmap
!= kernel_pmap
) &&
8144 (VME_ALIAS(entry
) >= VM_MEMORY_MALLOC
) &&
8145 (VME_ALIAS(entry
) <= VM_MEMORY_MALLOC_LARGE_REUSED
)) {
8146 vm_object_t new_object
, new_shadow
;
8149 * We're about to map something over a mapping
8150 * established by malloc()...
8152 new_object
= VME_OBJECT(copy_entry
);
8153 if (new_object
!= VM_OBJECT_NULL
) {
8154 vm_object_lock_shared(new_object
);
8156 while (new_object
!= VM_OBJECT_NULL
&&
8157 !new_object
->true_share
&&
8158 new_object
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
&&
8159 new_object
->internal
) {
8160 new_shadow
= new_object
->shadow
;
8161 if (new_shadow
== VM_OBJECT_NULL
) {
8164 vm_object_lock_shared(new_shadow
);
8165 vm_object_unlock(new_object
);
8166 new_object
= new_shadow
;
8168 if (new_object
!= VM_OBJECT_NULL
) {
8169 if (!new_object
->internal
) {
8171 * The new mapping is backed
8172 * by an external object. We
8173 * don't want malloc'ed memory
8174 * to be replaced with such a
8175 * non-anonymous mapping, so
8176 * let's go off the optimized
8179 vm_map_copy_overwrite_aligned_src_not_internal
++;
8180 vm_object_unlock(new_object
);
8183 if (new_object
->true_share
||
8184 new_object
->copy_strategy
!= MEMORY_OBJECT_COPY_SYMMETRIC
) {
8186 * Same if there's a "true_share"
8187 * object in the shadow chain, or
8188 * an object with a non-default
8189 * (SYMMETRIC) copy strategy.
8191 vm_map_copy_overwrite_aligned_src_not_symmetric
++;
8192 vm_object_unlock(new_object
);
8195 vm_object_unlock(new_object
);
8198 * The new mapping is still backed by
8199 * anonymous (internal) memory, so it's
8200 * OK to substitute it for the original
8205 if (old_object
!= VM_OBJECT_NULL
) {
8206 if(entry
->is_sub_map
) {
8207 if(entry
->use_pmap
) {
8208 #ifndef NO_NESTED_PMAP
8209 pmap_unnest(dst_map
->pmap
,
8210 (addr64_t
)entry
->vme_start
,
8211 entry
->vme_end
- entry
->vme_start
);
8212 #endif /* NO_NESTED_PMAP */
8213 if(dst_map
->mapped_in_other_pmaps
) {
8214 /* clean up parent */
8216 vm_map_submap_pmap_clean(
8217 dst_map
, entry
->vme_start
,
8223 vm_map_submap_pmap_clean(
8224 dst_map
, entry
->vme_start
,
8229 vm_map_deallocate(VME_SUBMAP(entry
));
8231 if(dst_map
->mapped_in_other_pmaps
) {
8232 vm_object_pmap_protect_options(
8240 PMAP_OPTIONS_REMOVE
);
8242 pmap_remove_options(
8244 (addr64_t
)(entry
->vme_start
),
8245 (addr64_t
)(entry
->vme_end
),
8246 PMAP_OPTIONS_REMOVE
);
8248 vm_object_deallocate(old_object
);
8252 entry
->is_sub_map
= FALSE
;
8253 VME_OBJECT_SET(entry
, VME_OBJECT(copy_entry
));
8254 object
= VME_OBJECT(entry
);
8255 entry
->needs_copy
= copy_entry
->needs_copy
;
8256 entry
->wired_count
= 0;
8257 entry
->user_wired_count
= 0;
8258 offset
= VME_OFFSET(copy_entry
);
8259 VME_OFFSET_SET(entry
, offset
);
8261 vm_map_copy_entry_unlink(copy
, copy_entry
);
8262 vm_map_copy_entry_dispose(copy
, copy_entry
);
8265 * we could try to push pages into the pmap at this point, BUT
8266 * this optimization only saved on average 2 us per page if ALL
8267 * the pages in the source were currently mapped
8268 * and ALL the pages in the dest were touched, if there were fewer
8269 * than 2/3 of the pages touched, this optimization actually cost more cycles
8270 * it also puts a lot of pressure on the pmap layer w/r to mapping structures
8274 * Set up for the next iteration. The map
8275 * has not been unlocked, so the next
8276 * address should be at the end of this
8277 * entry, and the next map entry should be
8278 * the one following it.
8281 start
= tmp_entry
->vme_end
;
8282 tmp_entry
= tmp_entry
->vme_next
;
8284 vm_map_version_t version
;
8285 vm_object_t dst_object
;
8286 vm_object_offset_t dst_offset
;
8290 if (entry
->needs_copy
) {
8291 VME_OBJECT_SHADOW(entry
,
8294 entry
->needs_copy
= FALSE
;
8297 dst_object
= VME_OBJECT(entry
);
8298 dst_offset
= VME_OFFSET(entry
);
8301 * Take an object reference, and record
8302 * the map version information so that the
8303 * map can be safely unlocked.
8306 if (dst_object
== VM_OBJECT_NULL
) {
8308 * We would usually have just taken the
8309 * optimized path above if the destination
8310 * object has not been allocated yet. But we
8311 * now disable that optimization if the copy
8312 * entry's object is not backed by anonymous
8313 * memory to avoid replacing malloc'ed
8314 * (i.e. re-usable) anonymous memory with a
8315 * not-so-anonymous mapping.
8316 * So we have to handle this case here and
8317 * allocate a new VM object for this map entry.
8319 dst_object
= vm_object_allocate(
8320 entry
->vme_end
- entry
->vme_start
);
8322 VME_OBJECT_SET(entry
, dst_object
);
8323 VME_OFFSET_SET(entry
, dst_offset
);
8324 assert(entry
->use_pmap
);
8328 vm_object_reference(dst_object
);
8330 /* account for unlock bumping up timestamp */
8331 version
.main_timestamp
= dst_map
->timestamp
+ 1;
8333 vm_map_unlock(dst_map
);
8336 * Copy as much as possible in one pass
8341 VME_OBJECT(copy_entry
),
8342 VME_OFFSET(copy_entry
),
8351 * Release the object reference
8354 vm_object_deallocate(dst_object
);
8357 * If a hard error occurred, return it now
8360 if (r
!= KERN_SUCCESS
)
8363 if (copy_size
!= 0) {
8365 * Dispose of the copied region
8368 vm_map_copy_clip_end(copy
, copy_entry
,
8369 copy_entry
->vme_start
+ copy_size
);
8370 vm_map_copy_entry_unlink(copy
, copy_entry
);
8371 vm_object_deallocate(VME_OBJECT(copy_entry
));
8372 vm_map_copy_entry_dispose(copy
, copy_entry
);
8376 * Pick up in the destination map where we left off.
8378 * Use the version information to avoid a lookup
8379 * in the normal case.
8383 vm_map_lock(dst_map
);
8384 if (version
.main_timestamp
== dst_map
->timestamp
&&
8386 /* We can safely use saved tmp_entry value */
8388 if (tmp_entry
->map_aligned
&&
8389 !VM_MAP_PAGE_ALIGNED(
8391 VM_MAP_PAGE_MASK(dst_map
))) {
8392 /* no longer map-aligned */
8393 tmp_entry
->map_aligned
= FALSE
;
8395 vm_map_clip_end(dst_map
, tmp_entry
, start
);
8396 tmp_entry
= tmp_entry
->vme_next
;
8398 /* Must do lookup of tmp_entry */
8400 if (!vm_map_lookup_entry(dst_map
, start
, &tmp_entry
)) {
8401 vm_map_unlock(dst_map
);
8402 return(KERN_INVALID_ADDRESS
);
8404 if (tmp_entry
->map_aligned
&&
8405 !VM_MAP_PAGE_ALIGNED(
8407 VM_MAP_PAGE_MASK(dst_map
))) {
8408 /* no longer map-aligned */
8409 tmp_entry
->map_aligned
= FALSE
;
8411 vm_map_clip_start(dst_map
, tmp_entry
, start
);
8416 return(KERN_SUCCESS
);
8417 }/* vm_map_copy_overwrite_aligned */
8420 * Routine: vm_map_copyin_kernel_buffer [internal use only]
8423 * Copy in data to a kernel buffer from space in the
8424 * source map. The original space may be optionally
8427 * If successful, returns a new copy object.
8429 static kern_return_t
8430 vm_map_copyin_kernel_buffer(
8432 vm_map_offset_t src_addr
,
8434 boolean_t src_destroy
,
8435 vm_map_copy_t
*copy_result
)
8439 vm_size_t kalloc_size
;
8441 if (len
> msg_ool_size_small
)
8442 return KERN_INVALID_ARGUMENT
;
8444 kalloc_size
= (vm_size_t
)(cpy_kdata_hdr_sz
+ len
);
8446 copy
= (vm_map_copy_t
)kalloc(kalloc_size
);
8447 if (copy
== VM_MAP_COPY_NULL
)
8448 return KERN_RESOURCE_SHORTAGE
;
8449 copy
->type
= VM_MAP_COPY_KERNEL_BUFFER
;
8453 kr
= copyinmap(src_map
, src_addr
, copy
->cpy_kdata
, (vm_size_t
)len
);
8454 if (kr
!= KERN_SUCCESS
) {
8455 kfree(copy
, kalloc_size
);
8459 (void) vm_map_remove(
8461 vm_map_trunc_page(src_addr
,
8462 VM_MAP_PAGE_MASK(src_map
)),
8463 vm_map_round_page(src_addr
+ len
,
8464 VM_MAP_PAGE_MASK(src_map
)),
8465 (VM_MAP_REMOVE_INTERRUPTIBLE
|
8466 VM_MAP_REMOVE_WAIT_FOR_KWIRE
|
8467 (src_map
== kernel_map
) ? VM_MAP_REMOVE_KUNWIRE
: 0));
8469 *copy_result
= copy
;
8470 return KERN_SUCCESS
;
8474 * Routine: vm_map_copyout_kernel_buffer [internal use only]
8477 * Copy out data from a kernel buffer into space in the
8478 * destination map. The space may be otpionally dynamically
8481 * If successful, consumes the copy object.
8482 * Otherwise, the caller is responsible for it.
8484 static int vm_map_copyout_kernel_buffer_failures
= 0;
8485 static kern_return_t
8486 vm_map_copyout_kernel_buffer(
8488 vm_map_address_t
*addr
, /* IN/OUT */
8490 boolean_t overwrite
,
8491 boolean_t consume_on_success
)
8493 kern_return_t kr
= KERN_SUCCESS
;
8494 thread_t thread
= current_thread();
8497 * check for corrupted vm_map_copy structure
8499 if (copy
->size
> msg_ool_size_small
|| copy
->offset
)
8500 panic("Invalid vm_map_copy_t sz:%lld, ofst:%lld",
8501 (long long)copy
->size
, (long long)copy
->offset
);
8506 * Allocate space in the target map for the data
8509 kr
= vm_map_enter(map
,
8511 vm_map_round_page(copy
->size
,
8512 VM_MAP_PAGE_MASK(map
)),
8513 (vm_map_offset_t
) 0,
8516 (vm_object_offset_t
) 0,
8520 VM_INHERIT_DEFAULT
);
8521 if (kr
!= KERN_SUCCESS
)
8526 * Copyout the data from the kernel buffer to the target map.
8528 if (thread
->map
== map
) {
8531 * If the target map is the current map, just do
8534 assert((vm_size_t
) copy
->size
== copy
->size
);
8535 if (copyout(copy
->cpy_kdata
, *addr
, (vm_size_t
) copy
->size
)) {
8536 kr
= KERN_INVALID_ADDRESS
;
8543 * If the target map is another map, assume the
8544 * target's address space identity for the duration
8547 vm_map_reference(map
);
8548 oldmap
= vm_map_switch(map
);
8550 assert((vm_size_t
) copy
->size
== copy
->size
);
8551 if (copyout(copy
->cpy_kdata
, *addr
, (vm_size_t
) copy
->size
)) {
8552 vm_map_copyout_kernel_buffer_failures
++;
8553 kr
= KERN_INVALID_ADDRESS
;
8556 (void) vm_map_switch(oldmap
);
8557 vm_map_deallocate(map
);
8560 if (kr
!= KERN_SUCCESS
) {
8561 /* the copy failed, clean up */
8564 * Deallocate the space we allocated in the target map.
8566 (void) vm_map_remove(
8568 vm_map_trunc_page(*addr
,
8569 VM_MAP_PAGE_MASK(map
)),
8570 vm_map_round_page((*addr
+
8571 vm_map_round_page(copy
->size
,
8572 VM_MAP_PAGE_MASK(map
))),
8573 VM_MAP_PAGE_MASK(map
)),
8578 /* copy was successful, dicard the copy structure */
8579 if (consume_on_success
) {
8580 kfree(copy
, copy
->size
+ cpy_kdata_hdr_sz
);
8588 * Macro: vm_map_copy_insert
8591 * Link a copy chain ("copy") into a map at the
8592 * specified location (after "where").
8594 * The copy chain is destroyed.
8596 * The arguments are evaluated multiple times.
8598 #define vm_map_copy_insert(map, where, copy) \
8600 vm_map_store_copy_insert(map, where, copy); \
8601 zfree(vm_map_copy_zone, copy); \
8607 vm_map_entry_t where
,
8609 vm_map_offset_t adjustment
,
8612 vm_inherit_t inheritance
)
8614 vm_map_entry_t copy_entry
, new_entry
;
8616 for (copy_entry
= vm_map_copy_first_entry(copy
);
8617 copy_entry
!= vm_map_copy_to_entry(copy
);
8618 copy_entry
= copy_entry
->vme_next
) {
8619 /* get a new VM map entry for the map */
8620 new_entry
= vm_map_entry_create(map
,
8621 !map
->hdr
.entries_pageable
);
8622 /* copy the "copy entry" to the new entry */
8623 vm_map_entry_copy(new_entry
, copy_entry
);
8624 /* adjust "start" and "end" */
8625 new_entry
->vme_start
+= adjustment
;
8626 new_entry
->vme_end
+= adjustment
;
8627 /* clear some attributes */
8628 new_entry
->inheritance
= inheritance
;
8629 new_entry
->protection
= cur_prot
;
8630 new_entry
->max_protection
= max_prot
;
8631 new_entry
->behavior
= VM_BEHAVIOR_DEFAULT
;
8632 /* take an extra reference on the entry's "object" */
8633 if (new_entry
->is_sub_map
) {
8634 assert(!new_entry
->use_pmap
); /* not nested */
8635 vm_map_lock(VME_SUBMAP(new_entry
));
8636 vm_map_reference(VME_SUBMAP(new_entry
));
8637 vm_map_unlock(VME_SUBMAP(new_entry
));
8639 vm_object_reference(VME_OBJECT(new_entry
));
8641 /* insert the new entry in the map */
8642 vm_map_store_entry_link(map
, where
, new_entry
);
8643 /* continue inserting the "copy entries" after the new entry */
8649 * Routine: vm_map_copyout
8652 * Copy out a copy chain ("copy") into newly-allocated
8653 * space in the destination map.
8655 * If successful, consumes the copy object.
8656 * Otherwise, the caller is responsible for it.
8662 vm_map_address_t
*dst_addr
, /* OUT */
8665 return vm_map_copyout_internal(dst_map
, dst_addr
, copy
,
8666 TRUE
, /* consume_on_success */
8669 VM_INHERIT_DEFAULT
);
8673 vm_map_copyout_internal(
8675 vm_map_address_t
*dst_addr
, /* OUT */
8677 boolean_t consume_on_success
,
8678 vm_prot_t cur_protection
,
8679 vm_prot_t max_protection
,
8680 vm_inherit_t inheritance
)
8683 vm_map_size_t adjustment
;
8684 vm_map_offset_t start
;
8685 vm_object_offset_t vm_copy_start
;
8686 vm_map_entry_t last
;
8687 vm_map_entry_t entry
;
8688 vm_map_entry_t hole_entry
;
8691 * Check for null copy object.
8694 if (copy
== VM_MAP_COPY_NULL
) {
8696 return(KERN_SUCCESS
);
8700 * Check for special copy object, created
8701 * by vm_map_copyin_object.
8704 if (copy
->type
== VM_MAP_COPY_OBJECT
) {
8705 vm_object_t object
= copy
->cpy_object
;
8707 vm_object_offset_t offset
;
8709 offset
= vm_object_trunc_page(copy
->offset
);
8710 size
= vm_map_round_page((copy
->size
+
8711 (vm_map_size_t
)(copy
->offset
-
8713 VM_MAP_PAGE_MASK(dst_map
));
8715 kr
= vm_map_enter(dst_map
, dst_addr
, size
,
8716 (vm_map_offset_t
) 0, VM_FLAGS_ANYWHERE
,
8717 object
, offset
, FALSE
,
8718 VM_PROT_DEFAULT
, VM_PROT_ALL
,
8719 VM_INHERIT_DEFAULT
);
8720 if (kr
!= KERN_SUCCESS
)
8722 /* Account for non-pagealigned copy object */
8723 *dst_addr
+= (vm_map_offset_t
)(copy
->offset
- offset
);
8724 if (consume_on_success
)
8725 zfree(vm_map_copy_zone
, copy
);
8726 return(KERN_SUCCESS
);
8730 * Check for special kernel buffer allocated
8731 * by new_ipc_kmsg_copyin.
8734 if (copy
->type
== VM_MAP_COPY_KERNEL_BUFFER
) {
8735 return vm_map_copyout_kernel_buffer(dst_map
, dst_addr
,
8737 consume_on_success
);
8742 * Find space for the data
8745 vm_copy_start
= vm_map_trunc_page((vm_map_size_t
)copy
->offset
,
8746 VM_MAP_COPY_PAGE_MASK(copy
));
8747 size
= vm_map_round_page((vm_map_size_t
)copy
->offset
+ copy
->size
,
8748 VM_MAP_COPY_PAGE_MASK(copy
))
8754 vm_map_lock(dst_map
);
8755 if( dst_map
->disable_vmentry_reuse
== TRUE
) {
8756 VM_MAP_HIGHEST_ENTRY(dst_map
, entry
, start
);
8759 if (dst_map
->holelistenabled
) {
8760 hole_entry
= (vm_map_entry_t
)dst_map
->holes_list
;
8762 if (hole_entry
== NULL
) {
8764 * No more space in the map?
8766 vm_map_unlock(dst_map
);
8767 return(KERN_NO_SPACE
);
8771 start
= last
->vme_start
;
8773 assert(first_free_is_valid(dst_map
));
8774 start
= ((last
= dst_map
->first_free
) == vm_map_to_entry(dst_map
)) ?
8775 vm_map_min(dst_map
) : last
->vme_end
;
8777 start
= vm_map_round_page(start
,
8778 VM_MAP_PAGE_MASK(dst_map
));
8782 vm_map_entry_t next
= last
->vme_next
;
8783 vm_map_offset_t end
= start
+ size
;
8785 if ((end
> dst_map
->max_offset
) || (end
< start
)) {
8786 if (dst_map
->wait_for_space
) {
8787 if (size
<= (dst_map
->max_offset
- dst_map
->min_offset
)) {
8788 assert_wait((event_t
) dst_map
,
8789 THREAD_INTERRUPTIBLE
);
8790 vm_map_unlock(dst_map
);
8791 thread_block(THREAD_CONTINUE_NULL
);
8795 vm_map_unlock(dst_map
);
8796 return(KERN_NO_SPACE
);
8799 if (dst_map
->holelistenabled
) {
8800 if (last
->vme_end
>= end
)
8804 * If there are no more entries, we must win.
8808 * If there is another entry, it must be
8809 * after the end of the potential new region.
8812 if (next
== vm_map_to_entry(dst_map
))
8815 if (next
->vme_start
>= end
)
8821 if (dst_map
->holelistenabled
) {
8822 if (last
== (vm_map_entry_t
) dst_map
->holes_list
) {
8826 vm_map_unlock(dst_map
);
8827 return(KERN_NO_SPACE
);
8829 start
= last
->vme_start
;
8831 start
= last
->vme_end
;
8833 start
= vm_map_round_page(start
,
8834 VM_MAP_PAGE_MASK(dst_map
));
8837 if (dst_map
->holelistenabled
) {
8838 if (vm_map_lookup_entry(dst_map
, last
->vme_start
, &last
)) {
8839 panic("Found an existing entry (%p) instead of potential hole at address: 0x%llx.\n", last
, (unsigned long long)last
->vme_start
);
8844 adjustment
= start
- vm_copy_start
;
8845 if (! consume_on_success
) {
8847 * We're not allowed to consume "copy", so we'll have to
8848 * copy its map entries into the destination map below.
8849 * No need to re-allocate map entries from the correct
8850 * (pageable or not) zone, since we'll get new map entries
8851 * during the transfer.
8852 * We'll also adjust the map entries's "start" and "end"
8853 * during the transfer, to keep "copy"'s entries consistent
8854 * with its "offset".
8856 goto after_adjustments
;
8860 * Since we're going to just drop the map
8861 * entries from the copy into the destination
8862 * map, they must come from the same pool.
8865 if (copy
->cpy_hdr
.entries_pageable
!= dst_map
->hdr
.entries_pageable
) {
8867 * Mismatches occur when dealing with the default
8871 vm_map_entry_t next
, new;
8874 * Find the zone that the copies were allocated from
8877 entry
= vm_map_copy_first_entry(copy
);
8880 * Reinitialize the copy so that vm_map_copy_entry_link
8883 vm_map_store_copy_reset(copy
, entry
);
8884 copy
->cpy_hdr
.entries_pageable
= dst_map
->hdr
.entries_pageable
;
8889 while (entry
!= vm_map_copy_to_entry(copy
)) {
8890 new = vm_map_copy_entry_create(copy
, !copy
->cpy_hdr
.entries_pageable
);
8891 vm_map_entry_copy_full(new, entry
);
8892 assert(!new->iokit_acct
);
8893 if (new->is_sub_map
) {
8894 /* clr address space specifics */
8895 new->use_pmap
= FALSE
;
8897 vm_map_copy_entry_link(copy
,
8898 vm_map_copy_last_entry(copy
),
8900 next
= entry
->vme_next
;
8901 old_zone
= entry
->from_reserved_zone
? vm_map_entry_reserved_zone
: vm_map_entry_zone
;
8902 zfree(old_zone
, entry
);
8908 * Adjust the addresses in the copy chain, and
8909 * reset the region attributes.
8912 for (entry
= vm_map_copy_first_entry(copy
);
8913 entry
!= vm_map_copy_to_entry(copy
);
8914 entry
= entry
->vme_next
) {
8915 if (VM_MAP_PAGE_SHIFT(dst_map
) == PAGE_SHIFT
) {
8917 * We're injecting this copy entry into a map that
8918 * has the standard page alignment, so clear
8919 * "map_aligned" (which might have been inherited
8920 * from the original map entry).
8922 entry
->map_aligned
= FALSE
;
8925 entry
->vme_start
+= adjustment
;
8926 entry
->vme_end
+= adjustment
;
8928 if (entry
->map_aligned
) {
8929 assert(VM_MAP_PAGE_ALIGNED(entry
->vme_start
,
8930 VM_MAP_PAGE_MASK(dst_map
)));
8931 assert(VM_MAP_PAGE_ALIGNED(entry
->vme_end
,
8932 VM_MAP_PAGE_MASK(dst_map
)));
8935 entry
->inheritance
= VM_INHERIT_DEFAULT
;
8936 entry
->protection
= VM_PROT_DEFAULT
;
8937 entry
->max_protection
= VM_PROT_ALL
;
8938 entry
->behavior
= VM_BEHAVIOR_DEFAULT
;
8941 * If the entry is now wired,
8942 * map the pages into the destination map.
8944 if (entry
->wired_count
!= 0) {
8945 register vm_map_offset_t va
;
8946 vm_object_offset_t offset
;
8947 register vm_object_t object
;
8951 object
= VME_OBJECT(entry
);
8952 offset
= VME_OFFSET(entry
);
8953 va
= entry
->vme_start
;
8955 pmap_pageable(dst_map
->pmap
,
8960 while (va
< entry
->vme_end
) {
8961 register vm_page_t m
;
8964 * Look up the page in the object.
8965 * Assert that the page will be found in the
8968 * the object was newly created by
8969 * vm_object_copy_slowly, and has
8970 * copies of all of the pages from
8973 * the object was moved from the old
8974 * map entry; because the old map
8975 * entry was wired, all of the pages
8976 * were in the top-level object.
8977 * (XXX not true if we wire pages for
8980 vm_object_lock(object
);
8982 m
= vm_page_lookup(object
, offset
);
8983 if (m
== VM_PAGE_NULL
|| !VM_PAGE_WIRED(m
) ||
8985 panic("vm_map_copyout: wiring %p", m
);
8989 * The page is assumed to be wired here, so it
8990 * shouldn't be encrypted. Otherwise, we
8991 * couldn't enter it in the page table, since
8992 * we don't want the user to see the encrypted
8995 ASSERT_PAGE_DECRYPTED(m
);
8997 prot
= entry
->protection
;
8999 if (override_nx(dst_map
, VME_ALIAS(entry
)) &&
9001 prot
|= VM_PROT_EXECUTE
;
9003 type_of_fault
= DBG_CACHE_HIT_FAULT
;
9005 vm_fault_enter(m
, dst_map
->pmap
, va
, prot
, prot
,
9006 VM_PAGE_WIRED(m
), FALSE
, FALSE
,
9007 FALSE
, VME_ALIAS(entry
),
9008 ((entry
->iokit_acct
||
9009 (!entry
->is_sub_map
&&
9011 ? PMAP_OPTIONS_ALT_ACCT
9013 NULL
, &type_of_fault
);
9015 vm_object_unlock(object
);
9017 offset
+= PAGE_SIZE_64
;
9026 * Correct the page alignment for the result
9029 *dst_addr
= start
+ (copy
->offset
- vm_copy_start
);
9032 * Update the hints and the map size
9035 if (consume_on_success
) {
9036 SAVE_HINT_MAP_WRITE(dst_map
, vm_map_copy_last_entry(copy
));
9038 SAVE_HINT_MAP_WRITE(dst_map
, last
);
9041 dst_map
->size
+= size
;
9047 if (consume_on_success
) {
9048 vm_map_copy_insert(dst_map
, last
, copy
);
9050 vm_map_copy_remap(dst_map
, last
, copy
, adjustment
,
9051 cur_protection
, max_protection
,
9055 vm_map_unlock(dst_map
);
9058 * XXX If wiring_required, call vm_map_pageable
9061 return(KERN_SUCCESS
);
9065 * Routine: vm_map_copyin
9068 * see vm_map_copyin_common. Exported via Unsupported.exports.
9072 #undef vm_map_copyin
9077 vm_map_address_t src_addr
,
9079 boolean_t src_destroy
,
9080 vm_map_copy_t
*copy_result
) /* OUT */
9082 return(vm_map_copyin_common(src_map
, src_addr
, len
, src_destroy
,
9083 FALSE
, copy_result
, FALSE
));
9087 * Routine: vm_map_copyin_common
9090 * Copy the specified region (src_addr, len) from the
9091 * source address space (src_map), possibly removing
9092 * the region from the source address space (src_destroy).
9095 * A vm_map_copy_t object (copy_result), suitable for
9096 * insertion into another address space (using vm_map_copyout),
9097 * copying over another address space region (using
9098 * vm_map_copy_overwrite). If the copy is unused, it
9099 * should be destroyed (using vm_map_copy_discard).
9101 * In/out conditions:
9102 * The source map should not be locked on entry.
9105 typedef struct submap_map
{
9106 vm_map_t parent_map
;
9107 vm_map_offset_t base_start
;
9108 vm_map_offset_t base_end
;
9109 vm_map_size_t base_len
;
9110 struct submap_map
*next
;
9114 vm_map_copyin_common(
9116 vm_map_address_t src_addr
,
9118 boolean_t src_destroy
,
9119 __unused boolean_t src_volatile
,
9120 vm_map_copy_t
*copy_result
, /* OUT */
9121 boolean_t use_maxprot
)
9123 vm_map_entry_t tmp_entry
; /* Result of last map lookup --
9124 * in multi-level lookup, this
9125 * entry contains the actual
9129 vm_map_entry_t new_entry
= VM_MAP_ENTRY_NULL
; /* Map entry for copy */
9131 vm_map_offset_t src_start
; /* Start of current entry --
9132 * where copy is taking place now
9134 vm_map_offset_t src_end
; /* End of entire region to be
9136 vm_map_offset_t src_base
;
9137 vm_map_t base_map
= src_map
;
9138 boolean_t map_share
=FALSE
;
9139 submap_map_t
*parent_maps
= NULL
;
9142 vm_map_copy_t copy
; /* Resulting copy */
9143 vm_map_address_t copy_addr
;
9144 vm_map_size_t copy_size
;
9147 * Check for copies of zero bytes.
9151 *copy_result
= VM_MAP_COPY_NULL
;
9152 return(KERN_SUCCESS
);
9156 * Check that the end address doesn't overflow
9158 src_end
= src_addr
+ len
;
9159 if (src_end
< src_addr
)
9160 return KERN_INVALID_ADDRESS
;
9163 * If the copy is sufficiently small, use a kernel buffer instead
9164 * of making a virtual copy. The theory being that the cost of
9165 * setting up VM (and taking C-O-W faults) dominates the copy costs
9166 * for small regions.
9168 if ((len
< msg_ool_size_small
) && !use_maxprot
)
9169 return vm_map_copyin_kernel_buffer(src_map
, src_addr
, len
,
9170 src_destroy
, copy_result
);
9173 * Compute (page aligned) start and end of region
9175 src_start
= vm_map_trunc_page(src_addr
,
9176 VM_MAP_PAGE_MASK(src_map
));
9177 src_end
= vm_map_round_page(src_end
,
9178 VM_MAP_PAGE_MASK(src_map
));
9180 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);
9183 * Allocate a header element for the list.
9185 * Use the start and end in the header to
9186 * remember the endpoints prior to rounding.
9189 copy
= (vm_map_copy_t
) zalloc(vm_map_copy_zone
);
9190 copy
->c_u
.hdr
.rb_head_store
.rbh_root
= (void*)(int)SKIP_RB_TREE
;
9191 vm_map_copy_first_entry(copy
) =
9192 vm_map_copy_last_entry(copy
) = vm_map_copy_to_entry(copy
);
9193 copy
->type
= VM_MAP_COPY_ENTRY_LIST
;
9194 copy
->cpy_hdr
.nentries
= 0;
9195 copy
->cpy_hdr
.entries_pageable
= TRUE
;
9197 copy
->cpy_hdr
.page_shift
= src_map
->hdr
.page_shift
;
9200 * The copy entries can be broken down for a variety of reasons,
9201 * so we can't guarantee that they will remain map-aligned...
9202 * Will need to adjust the first copy_entry's "vme_start" and
9203 * the last copy_entry's "vme_end" to be rounded to PAGE_MASK
9204 * rather than the original map's alignment.
9206 copy
->cpy_hdr
.page_shift
= PAGE_SHIFT
;
9209 vm_map_store_init( &(copy
->cpy_hdr
) );
9211 copy
->offset
= src_addr
;
9214 new_entry
= vm_map_copy_entry_create(copy
, !copy
->cpy_hdr
.entries_pageable
);
9218 vm_map_unlock(src_map); \
9219 if(src_map != base_map) \
9220 vm_map_deallocate(src_map); \
9221 if (new_entry != VM_MAP_ENTRY_NULL) \
9222 vm_map_copy_entry_dispose(copy,new_entry); \
9223 vm_map_copy_discard(copy); \
9225 submap_map_t *_ptr; \
9227 for(_ptr = parent_maps; _ptr != NULL; _ptr = parent_maps) { \
9228 parent_maps=parent_maps->next; \
9229 if (_ptr->parent_map != base_map) \
9230 vm_map_deallocate(_ptr->parent_map); \
9231 kfree(_ptr, sizeof(submap_map_t)); \
9238 * Find the beginning of the region.
9241 vm_map_lock(src_map
);
9244 * Lookup the original "src_addr" rather than the truncated
9245 * "src_start", in case "src_start" falls in a non-map-aligned
9246 * map entry *before* the map entry that contains "src_addr"...
9248 if (!vm_map_lookup_entry(src_map
, src_addr
, &tmp_entry
))
9249 RETURN(KERN_INVALID_ADDRESS
);
9250 if(!tmp_entry
->is_sub_map
) {
9252 * ... but clip to the map-rounded "src_start" rather than
9253 * "src_addr" to preserve map-alignment. We'll adjust the
9254 * first copy entry at the end, if needed.
9256 vm_map_clip_start(src_map
, tmp_entry
, src_start
);
9258 if (src_start
< tmp_entry
->vme_start
) {
9260 * Move "src_start" up to the start of the
9261 * first map entry to copy.
9263 src_start
= tmp_entry
->vme_start
;
9265 /* set for later submap fix-up */
9266 copy_addr
= src_start
;
9269 * Go through entries until we get to the end.
9274 vm_map_entry_t src_entry
= tmp_entry
; /* Top-level entry */
9275 vm_map_size_t src_size
; /* Size of source
9276 * map entry (in both
9281 vm_object_t src_object
; /* Object to copy */
9282 vm_object_offset_t src_offset
;
9284 boolean_t src_needs_copy
; /* Should source map
9286 * for copy-on-write?
9289 boolean_t new_entry_needs_copy
; /* Will new entry be COW? */
9291 boolean_t was_wired
; /* Was source wired? */
9292 vm_map_version_t version
; /* Version before locks
9293 * dropped to make copy
9295 kern_return_t result
; /* Return value from
9296 * copy_strategically.
9298 while(tmp_entry
->is_sub_map
) {
9299 vm_map_size_t submap_len
;
9302 ptr
= (submap_map_t
*)kalloc(sizeof(submap_map_t
));
9303 ptr
->next
= parent_maps
;
9305 ptr
->parent_map
= src_map
;
9306 ptr
->base_start
= src_start
;
9307 ptr
->base_end
= src_end
;
9308 submap_len
= tmp_entry
->vme_end
- src_start
;
9309 if(submap_len
> (src_end
-src_start
))
9310 submap_len
= src_end
-src_start
;
9311 ptr
->base_len
= submap_len
;
9313 src_start
-= tmp_entry
->vme_start
;
9314 src_start
+= VME_OFFSET(tmp_entry
);
9315 src_end
= src_start
+ submap_len
;
9316 src_map
= VME_SUBMAP(tmp_entry
);
9317 vm_map_lock(src_map
);
9318 /* keep an outstanding reference for all maps in */
9319 /* the parents tree except the base map */
9320 vm_map_reference(src_map
);
9321 vm_map_unlock(ptr
->parent_map
);
9322 if (!vm_map_lookup_entry(
9323 src_map
, src_start
, &tmp_entry
))
9324 RETURN(KERN_INVALID_ADDRESS
);
9326 if(!tmp_entry
->is_sub_map
)
9327 vm_map_clip_start(src_map
, tmp_entry
, src_start
);
9328 src_entry
= tmp_entry
;
9330 /* we are now in the lowest level submap... */
9332 if ((VME_OBJECT(tmp_entry
) != VM_OBJECT_NULL
) &&
9333 (VME_OBJECT(tmp_entry
)->phys_contiguous
)) {
9334 /* This is not, supported for now.In future */
9335 /* we will need to detect the phys_contig */
9336 /* condition and then upgrade copy_slowly */
9337 /* to do physical copy from the device mem */
9338 /* based object. We can piggy-back off of */
9339 /* the was wired boolean to set-up the */
9340 /* proper handling */
9341 RETURN(KERN_PROTECTION_FAILURE
);
9344 * Create a new address map entry to hold the result.
9345 * Fill in the fields from the appropriate source entries.
9346 * We must unlock the source map to do this if we need
9347 * to allocate a map entry.
9349 if (new_entry
== VM_MAP_ENTRY_NULL
) {
9350 version
.main_timestamp
= src_map
->timestamp
;
9351 vm_map_unlock(src_map
);
9353 new_entry
= vm_map_copy_entry_create(copy
, !copy
->cpy_hdr
.entries_pageable
);
9355 vm_map_lock(src_map
);
9356 if ((version
.main_timestamp
+ 1) != src_map
->timestamp
) {
9357 if (!vm_map_lookup_entry(src_map
, src_start
,
9359 RETURN(KERN_INVALID_ADDRESS
);
9361 if (!tmp_entry
->is_sub_map
)
9362 vm_map_clip_start(src_map
, tmp_entry
, src_start
);
9363 continue; /* restart w/ new tmp_entry */
9368 * Verify that the region can be read.
9370 if (((src_entry
->protection
& VM_PROT_READ
) == VM_PROT_NONE
&&
9372 (src_entry
->max_protection
& VM_PROT_READ
) == 0)
9373 RETURN(KERN_PROTECTION_FAILURE
);
9376 * Clip against the endpoints of the entire region.
9379 vm_map_clip_end(src_map
, src_entry
, src_end
);
9381 src_size
= src_entry
->vme_end
- src_start
;
9382 src_object
= VME_OBJECT(src_entry
);
9383 src_offset
= VME_OFFSET(src_entry
);
9384 was_wired
= (src_entry
->wired_count
!= 0);
9386 vm_map_entry_copy(new_entry
, src_entry
);
9387 if (new_entry
->is_sub_map
) {
9388 /* clr address space specifics */
9389 new_entry
->use_pmap
= FALSE
;
9393 * Attempt non-blocking copy-on-write optimizations.
9397 (src_object
== VM_OBJECT_NULL
||
9398 (src_object
->internal
&& !src_object
->true_share
9401 * If we are destroying the source, and the object
9402 * is internal, we can move the object reference
9403 * from the source to the copy. The copy is
9404 * copy-on-write only if the source is.
9405 * We make another reference to the object, because
9406 * destroying the source entry will deallocate it.
9408 vm_object_reference(src_object
);
9411 * Copy is always unwired. vm_map_copy_entry
9412 * set its wired count to zero.
9415 goto CopySuccessful
;
9420 XPR(XPR_VM_MAP
, "vm_map_copyin_common src_obj 0x%x ent 0x%x obj 0x%x was_wired %d\n",
9421 src_object
, new_entry
, VME_OBJECT(new_entry
),
9423 if ((src_object
== VM_OBJECT_NULL
||
9424 (!was_wired
&& !map_share
&& !tmp_entry
->is_shared
)) &&
9425 vm_object_copy_quickly(
9426 &VME_OBJECT(new_entry
),
9430 &new_entry_needs_copy
)) {
9432 new_entry
->needs_copy
= new_entry_needs_copy
;
9435 * Handle copy-on-write obligations
9438 if (src_needs_copy
&& !tmp_entry
->needs_copy
) {
9441 prot
= src_entry
->protection
& ~VM_PROT_WRITE
;
9443 if (override_nx(src_map
, VME_ALIAS(src_entry
))
9445 prot
|= VM_PROT_EXECUTE
;
9447 vm_object_pmap_protect(
9451 (src_entry
->is_shared
?
9454 src_entry
->vme_start
,
9457 assert(tmp_entry
->wired_count
== 0);
9458 tmp_entry
->needs_copy
= TRUE
;
9462 * The map has never been unlocked, so it's safe
9463 * to move to the next entry rather than doing
9467 goto CopySuccessful
;
9471 * Take an object reference, so that we may
9472 * release the map lock(s).
9475 assert(src_object
!= VM_OBJECT_NULL
);
9476 vm_object_reference(src_object
);
9479 * Record the timestamp for later verification.
9483 version
.main_timestamp
= src_map
->timestamp
;
9484 vm_map_unlock(src_map
); /* Increments timestamp once! */
9492 vm_object_lock(src_object
);
9493 result
= vm_object_copy_slowly(
9498 &VME_OBJECT(new_entry
));
9499 VME_OFFSET_SET(new_entry
, 0);
9500 new_entry
->needs_copy
= FALSE
;
9503 else if (src_object
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
&&
9504 (tmp_entry
->is_shared
|| map_share
)) {
9505 vm_object_t new_object
;
9507 vm_object_lock_shared(src_object
);
9508 new_object
= vm_object_copy_delayed(
9513 if (new_object
== VM_OBJECT_NULL
)
9516 VME_OBJECT_SET(new_entry
, new_object
);
9517 assert(new_entry
->wired_count
== 0);
9518 new_entry
->needs_copy
= TRUE
;
9519 assert(!new_entry
->iokit_acct
);
9520 assert(new_object
->purgable
== VM_PURGABLE_DENY
);
9521 new_entry
->use_pmap
= TRUE
;
9522 result
= KERN_SUCCESS
;
9525 vm_object_offset_t new_offset
;
9526 new_offset
= VME_OFFSET(new_entry
);
9527 result
= vm_object_copy_strategically(src_object
,
9530 &VME_OBJECT(new_entry
),
9532 &new_entry_needs_copy
);
9533 if (new_offset
!= VME_OFFSET(new_entry
)) {
9534 VME_OFFSET_SET(new_entry
, new_offset
);
9537 new_entry
->needs_copy
= new_entry_needs_copy
;
9540 if (result
!= KERN_SUCCESS
&&
9541 result
!= KERN_MEMORY_RESTART_COPY
) {
9542 vm_map_lock(src_map
);
9547 * Throw away the extra reference
9550 vm_object_deallocate(src_object
);
9553 * Verify that the map has not substantially
9554 * changed while the copy was being made.
9557 vm_map_lock(src_map
);
9559 if ((version
.main_timestamp
+ 1) == src_map
->timestamp
)
9560 goto VerificationSuccessful
;
9563 * Simple version comparison failed.
9565 * Retry the lookup and verify that the
9566 * same object/offset are still present.
9568 * [Note: a memory manager that colludes with
9569 * the calling task can detect that we have
9570 * cheated. While the map was unlocked, the
9571 * mapping could have been changed and restored.]
9574 if (!vm_map_lookup_entry(src_map
, src_start
, &tmp_entry
)) {
9575 if (result
!= KERN_MEMORY_RESTART_COPY
) {
9576 vm_object_deallocate(VME_OBJECT(new_entry
));
9577 VME_OBJECT_SET(new_entry
, VM_OBJECT_NULL
);
9578 assert(!new_entry
->iokit_acct
);
9579 new_entry
->use_pmap
= TRUE
;
9581 RETURN(KERN_INVALID_ADDRESS
);
9584 src_entry
= tmp_entry
;
9585 vm_map_clip_start(src_map
, src_entry
, src_start
);
9587 if ((((src_entry
->protection
& VM_PROT_READ
) == VM_PROT_NONE
) &&
9589 ((src_entry
->max_protection
& VM_PROT_READ
) == 0))
9590 goto VerificationFailed
;
9592 if (src_entry
->vme_end
< new_entry
->vme_end
) {
9593 assert(VM_MAP_PAGE_ALIGNED(src_entry
->vme_end
,
9594 VM_MAP_COPY_PAGE_MASK(copy
)));
9595 new_entry
->vme_end
= src_entry
->vme_end
;
9596 src_size
= new_entry
->vme_end
- src_start
;
9599 if ((VME_OBJECT(src_entry
) != src_object
) ||
9600 (VME_OFFSET(src_entry
) != src_offset
) ) {
9603 * Verification failed.
9605 * Start over with this top-level entry.
9608 VerificationFailed
: ;
9610 vm_object_deallocate(VME_OBJECT(new_entry
));
9611 tmp_entry
= src_entry
;
9616 * Verification succeeded.
9619 VerificationSuccessful
: ;
9621 if (result
== KERN_MEMORY_RESTART_COPY
)
9631 * Link in the new copy entry.
9634 vm_map_copy_entry_link(copy
, vm_map_copy_last_entry(copy
),
9638 * Determine whether the entire region
9641 src_base
= src_start
;
9642 src_start
= new_entry
->vme_end
;
9643 new_entry
= VM_MAP_ENTRY_NULL
;
9644 while ((src_start
>= src_end
) && (src_end
!= 0)) {
9647 if (src_map
== base_map
) {
9648 /* back to the top */
9653 assert(ptr
!= NULL
);
9654 parent_maps
= parent_maps
->next
;
9656 /* fix up the damage we did in that submap */
9657 vm_map_simplify_range(src_map
,
9661 vm_map_unlock(src_map
);
9662 vm_map_deallocate(src_map
);
9663 vm_map_lock(ptr
->parent_map
);
9664 src_map
= ptr
->parent_map
;
9665 src_base
= ptr
->base_start
;
9666 src_start
= ptr
->base_start
+ ptr
->base_len
;
9667 src_end
= ptr
->base_end
;
9668 if (!vm_map_lookup_entry(src_map
,
9671 (src_end
> src_start
)) {
9672 RETURN(KERN_INVALID_ADDRESS
);
9674 kfree(ptr
, sizeof(submap_map_t
));
9675 if (parent_maps
== NULL
)
9677 src_entry
= tmp_entry
->vme_prev
;
9680 if ((VM_MAP_PAGE_SHIFT(src_map
) != PAGE_SHIFT
) &&
9681 (src_start
>= src_addr
+ len
) &&
9682 (src_addr
+ len
!= 0)) {
9684 * Stop copying now, even though we haven't reached
9685 * "src_end". We'll adjust the end of the last copy
9686 * entry at the end, if needed.
9688 * If src_map's aligment is different from the
9689 * system's page-alignment, there could be
9690 * extra non-map-aligned map entries between
9691 * the original (non-rounded) "src_addr + len"
9692 * and the rounded "src_end".
9693 * We do not want to copy those map entries since
9694 * they're not part of the copied range.
9699 if ((src_start
>= src_end
) && (src_end
!= 0))
9703 * Verify that there are no gaps in the region
9706 tmp_entry
= src_entry
->vme_next
;
9707 if ((tmp_entry
->vme_start
!= src_start
) ||
9708 (tmp_entry
== vm_map_to_entry(src_map
))) {
9709 RETURN(KERN_INVALID_ADDRESS
);
9714 * If the source should be destroyed, do it now, since the
9715 * copy was successful.
9718 (void) vm_map_delete(
9720 vm_map_trunc_page(src_addr
,
9721 VM_MAP_PAGE_MASK(src_map
)),
9723 ((src_map
== kernel_map
) ?
9724 VM_MAP_REMOVE_KUNWIRE
:
9728 /* fix up the damage we did in the base map */
9729 vm_map_simplify_range(
9731 vm_map_trunc_page(src_addr
,
9732 VM_MAP_PAGE_MASK(src_map
)),
9733 vm_map_round_page(src_end
,
9734 VM_MAP_PAGE_MASK(src_map
)));
9737 vm_map_unlock(src_map
);
9739 if (VM_MAP_PAGE_SHIFT(src_map
) != PAGE_SHIFT
) {
9740 vm_map_offset_t original_start
, original_offset
, original_end
;
9742 assert(VM_MAP_COPY_PAGE_MASK(copy
) == PAGE_MASK
);
9744 /* adjust alignment of first copy_entry's "vme_start" */
9745 tmp_entry
= vm_map_copy_first_entry(copy
);
9746 if (tmp_entry
!= vm_map_copy_to_entry(copy
)) {
9747 vm_map_offset_t adjustment
;
9749 original_start
= tmp_entry
->vme_start
;
9750 original_offset
= VME_OFFSET(tmp_entry
);
9752 /* map-align the start of the first copy entry... */
9753 adjustment
= (tmp_entry
->vme_start
-
9755 tmp_entry
->vme_start
,
9756 VM_MAP_PAGE_MASK(src_map
)));
9757 tmp_entry
->vme_start
-= adjustment
;
9758 VME_OFFSET_SET(tmp_entry
,
9759 VME_OFFSET(tmp_entry
) - adjustment
);
9760 copy_addr
-= adjustment
;
9761 assert(tmp_entry
->vme_start
< tmp_entry
->vme_end
);
9762 /* ... adjust for mis-aligned start of copy range */
9764 (vm_map_trunc_page(copy
->offset
,
9766 vm_map_trunc_page(copy
->offset
,
9767 VM_MAP_PAGE_MASK(src_map
)));
9769 assert(page_aligned(adjustment
));
9770 assert(adjustment
< VM_MAP_PAGE_SIZE(src_map
));
9771 tmp_entry
->vme_start
+= adjustment
;
9772 VME_OFFSET_SET(tmp_entry
,
9773 (VME_OFFSET(tmp_entry
) +
9775 copy_addr
+= adjustment
;
9776 assert(tmp_entry
->vme_start
< tmp_entry
->vme_end
);
9780 * Assert that the adjustments haven't exposed
9781 * more than was originally copied...
9783 assert(tmp_entry
->vme_start
>= original_start
);
9784 assert(VME_OFFSET(tmp_entry
) >= original_offset
);
9786 * ... and that it did not adjust outside of a
9787 * a single 16K page.
9789 assert(vm_map_trunc_page(tmp_entry
->vme_start
,
9790 VM_MAP_PAGE_MASK(src_map
)) ==
9791 vm_map_trunc_page(original_start
,
9792 VM_MAP_PAGE_MASK(src_map
)));
9795 /* adjust alignment of last copy_entry's "vme_end" */
9796 tmp_entry
= vm_map_copy_last_entry(copy
);
9797 if (tmp_entry
!= vm_map_copy_to_entry(copy
)) {
9798 vm_map_offset_t adjustment
;
9800 original_end
= tmp_entry
->vme_end
;
9802 /* map-align the end of the last copy entry... */
9803 tmp_entry
->vme_end
=
9804 vm_map_round_page(tmp_entry
->vme_end
,
9805 VM_MAP_PAGE_MASK(src_map
));
9806 /* ... adjust for mis-aligned end of copy range */
9808 (vm_map_round_page((copy
->offset
+
9810 VM_MAP_PAGE_MASK(src_map
)) -
9811 vm_map_round_page((copy
->offset
+
9815 assert(page_aligned(adjustment
));
9816 assert(adjustment
< VM_MAP_PAGE_SIZE(src_map
));
9817 tmp_entry
->vme_end
-= adjustment
;
9818 assert(tmp_entry
->vme_start
< tmp_entry
->vme_end
);
9822 * Assert that the adjustments haven't exposed
9823 * more than was originally copied...
9825 assert(tmp_entry
->vme_end
<= original_end
);
9827 * ... and that it did not adjust outside of a
9828 * a single 16K page.
9830 assert(vm_map_round_page(tmp_entry
->vme_end
,
9831 VM_MAP_PAGE_MASK(src_map
)) ==
9832 vm_map_round_page(original_end
,
9833 VM_MAP_PAGE_MASK(src_map
)));
9837 /* Fix-up start and end points in copy. This is necessary */
9838 /* when the various entries in the copy object were picked */
9839 /* up from different sub-maps */
9841 tmp_entry
= vm_map_copy_first_entry(copy
);
9842 copy_size
= 0; /* compute actual size */
9843 while (tmp_entry
!= vm_map_copy_to_entry(copy
)) {
9844 assert(VM_MAP_PAGE_ALIGNED(
9845 copy_addr
+ (tmp_entry
->vme_end
-
9846 tmp_entry
->vme_start
),
9847 VM_MAP_COPY_PAGE_MASK(copy
)));
9848 assert(VM_MAP_PAGE_ALIGNED(
9850 VM_MAP_COPY_PAGE_MASK(copy
)));
9853 * The copy_entries will be injected directly into the
9854 * destination map and might not be "map aligned" there...
9856 tmp_entry
->map_aligned
= FALSE
;
9858 tmp_entry
->vme_end
= copy_addr
+
9859 (tmp_entry
->vme_end
- tmp_entry
->vme_start
);
9860 tmp_entry
->vme_start
= copy_addr
;
9861 assert(tmp_entry
->vme_start
< tmp_entry
->vme_end
);
9862 copy_addr
+= tmp_entry
->vme_end
- tmp_entry
->vme_start
;
9863 copy_size
+= tmp_entry
->vme_end
- tmp_entry
->vme_start
;
9864 tmp_entry
= (struct vm_map_entry
*)tmp_entry
->vme_next
;
9867 if (VM_MAP_PAGE_SHIFT(src_map
) != PAGE_SHIFT
&&
9868 copy_size
< copy
->size
) {
9870 * The actual size of the VM map copy is smaller than what
9871 * was requested by the caller. This must be because some
9872 * PAGE_SIZE-sized pages are missing at the end of the last
9873 * VM_MAP_PAGE_SIZE(src_map)-sized chunk of the range.
9874 * The caller might not have been aware of those missing
9875 * pages and might not want to be aware of it, which is
9876 * fine as long as they don't try to access (and crash on)
9877 * those missing pages.
9878 * Let's adjust the size of the "copy", to avoid failing
9879 * in vm_map_copyout() or vm_map_copy_overwrite().
9881 assert(vm_map_round_page(copy_size
,
9882 VM_MAP_PAGE_MASK(src_map
)) ==
9883 vm_map_round_page(copy
->size
,
9884 VM_MAP_PAGE_MASK(src_map
)));
9885 copy
->size
= copy_size
;
9888 *copy_result
= copy
;
9889 return(KERN_SUCCESS
);
9895 vm_map_copy_extract(
9897 vm_map_address_t src_addr
,
9899 vm_map_copy_t
*copy_result
, /* OUT */
9900 vm_prot_t
*cur_prot
, /* OUT */
9901 vm_prot_t
*max_prot
)
9903 vm_map_offset_t src_start
, src_end
;
9908 * Check for copies of zero bytes.
9912 *copy_result
= VM_MAP_COPY_NULL
;
9913 return(KERN_SUCCESS
);
9917 * Check that the end address doesn't overflow
9919 src_end
= src_addr
+ len
;
9920 if (src_end
< src_addr
)
9921 return KERN_INVALID_ADDRESS
;
9924 * Compute (page aligned) start and end of region
9926 src_start
= vm_map_trunc_page(src_addr
, PAGE_MASK
);
9927 src_end
= vm_map_round_page(src_end
, PAGE_MASK
);
9930 * Allocate a header element for the list.
9932 * Use the start and end in the header to
9933 * remember the endpoints prior to rounding.
9936 copy
= (vm_map_copy_t
) zalloc(vm_map_copy_zone
);
9937 copy
->c_u
.hdr
.rb_head_store
.rbh_root
= (void*)(int)SKIP_RB_TREE
;
9938 vm_map_copy_first_entry(copy
) =
9939 vm_map_copy_last_entry(copy
) = vm_map_copy_to_entry(copy
);
9940 copy
->type
= VM_MAP_COPY_ENTRY_LIST
;
9941 copy
->cpy_hdr
.nentries
= 0;
9942 copy
->cpy_hdr
.entries_pageable
= TRUE
;
9944 vm_map_store_init(©
->cpy_hdr
);
9949 kr
= vm_map_remap_extract(src_map
,
9957 TRUE
); /* pageable */
9958 if (kr
!= KERN_SUCCESS
) {
9959 vm_map_copy_discard(copy
);
9963 *copy_result
= copy
;
9964 return KERN_SUCCESS
;
9968 * vm_map_copyin_object:
9970 * Create a copy object from an object.
9971 * Our caller donates an object reference.
9975 vm_map_copyin_object(
9977 vm_object_offset_t offset
, /* offset of region in object */
9978 vm_object_size_t size
, /* size of region in object */
9979 vm_map_copy_t
*copy_result
) /* OUT */
9981 vm_map_copy_t copy
; /* Resulting copy */
9984 * We drop the object into a special copy object
9985 * that contains the object directly.
9988 copy
= (vm_map_copy_t
) zalloc(vm_map_copy_zone
);
9989 copy
->c_u
.hdr
.rb_head_store
.rbh_root
= (void*)(int)SKIP_RB_TREE
;
9990 copy
->type
= VM_MAP_COPY_OBJECT
;
9991 copy
->cpy_object
= object
;
9992 copy
->offset
= offset
;
9995 *copy_result
= copy
;
9996 return(KERN_SUCCESS
);
10002 vm_map_entry_t old_entry
,
10005 vm_object_t object
;
10006 vm_map_entry_t new_entry
;
10009 * New sharing code. New map entry
10010 * references original object. Internal
10011 * objects use asynchronous copy algorithm for
10012 * future copies. First make sure we have
10013 * the right object. If we need a shadow,
10014 * or someone else already has one, then
10015 * make a new shadow and share it.
10018 object
= VME_OBJECT(old_entry
);
10019 if (old_entry
->is_sub_map
) {
10020 assert(old_entry
->wired_count
== 0);
10021 #ifndef NO_NESTED_PMAP
10022 if(old_entry
->use_pmap
) {
10023 kern_return_t result
;
10025 result
= pmap_nest(new_map
->pmap
,
10026 (VME_SUBMAP(old_entry
))->pmap
,
10027 (addr64_t
)old_entry
->vme_start
,
10028 (addr64_t
)old_entry
->vme_start
,
10029 (uint64_t)(old_entry
->vme_end
- old_entry
->vme_start
));
10031 panic("vm_map_fork_share: pmap_nest failed!");
10033 #endif /* NO_NESTED_PMAP */
10034 } else if (object
== VM_OBJECT_NULL
) {
10035 object
= vm_object_allocate((vm_map_size_t
)(old_entry
->vme_end
-
10036 old_entry
->vme_start
));
10037 VME_OFFSET_SET(old_entry
, 0);
10038 VME_OBJECT_SET(old_entry
, object
);
10039 old_entry
->use_pmap
= TRUE
;
10040 assert(!old_entry
->needs_copy
);
10041 } else if (object
->copy_strategy
!=
10042 MEMORY_OBJECT_COPY_SYMMETRIC
) {
10045 * We are already using an asymmetric
10046 * copy, and therefore we already have
10047 * the right object.
10050 assert(! old_entry
->needs_copy
);
10052 else if (old_entry
->needs_copy
|| /* case 1 */
10053 object
->shadowed
|| /* case 2 */
10054 (!object
->true_share
&& /* case 3 */
10055 !old_entry
->is_shared
&&
10057 (vm_map_size_t
)(old_entry
->vme_end
-
10058 old_entry
->vme_start
)))) {
10061 * We need to create a shadow.
10062 * There are three cases here.
10063 * In the first case, we need to
10064 * complete a deferred symmetrical
10065 * copy that we participated in.
10066 * In the second and third cases,
10067 * we need to create the shadow so
10068 * that changes that we make to the
10069 * object do not interfere with
10070 * any symmetrical copies which
10071 * have occured (case 2) or which
10072 * might occur (case 3).
10074 * The first case is when we had
10075 * deferred shadow object creation
10076 * via the entry->needs_copy mechanism.
10077 * This mechanism only works when
10078 * only one entry points to the source
10079 * object, and we are about to create
10080 * a second entry pointing to the
10081 * same object. The problem is that
10082 * there is no way of mapping from
10083 * an object to the entries pointing
10084 * to it. (Deferred shadow creation
10085 * works with one entry because occurs
10086 * at fault time, and we walk from the
10087 * entry to the object when handling
10090 * The second case is when the object
10091 * to be shared has already been copied
10092 * with a symmetric copy, but we point
10093 * directly to the object without
10094 * needs_copy set in our entry. (This
10095 * can happen because different ranges
10096 * of an object can be pointed to by
10097 * different entries. In particular,
10098 * a single entry pointing to an object
10099 * can be split by a call to vm_inherit,
10100 * which, combined with task_create, can
10101 * result in the different entries
10102 * having different needs_copy values.)
10103 * The shadowed flag in the object allows
10104 * us to detect this case. The problem
10105 * with this case is that if this object
10106 * has or will have shadows, then we
10107 * must not perform an asymmetric copy
10108 * of this object, since such a copy
10109 * allows the object to be changed, which
10110 * will break the previous symmetrical
10111 * copies (which rely upon the object
10112 * not changing). In a sense, the shadowed
10113 * flag says "don't change this object".
10114 * We fix this by creating a shadow
10115 * object for this object, and sharing
10116 * that. This works because we are free
10117 * to change the shadow object (and thus
10118 * to use an asymmetric copy strategy);
10119 * this is also semantically correct,
10120 * since this object is temporary, and
10121 * therefore a copy of the object is
10122 * as good as the object itself. (This
10123 * is not true for permanent objects,
10124 * since the pager needs to see changes,
10125 * which won't happen if the changes
10126 * are made to a copy.)
10128 * The third case is when the object
10129 * to be shared has parts sticking
10130 * outside of the entry we're working
10131 * with, and thus may in the future
10132 * be subject to a symmetrical copy.
10133 * (This is a preemptive version of
10136 VME_OBJECT_SHADOW(old_entry
,
10137 (vm_map_size_t
) (old_entry
->vme_end
-
10138 old_entry
->vme_start
));
10141 * If we're making a shadow for other than
10142 * copy on write reasons, then we have
10143 * to remove write permission.
10146 if (!old_entry
->needs_copy
&&
10147 (old_entry
->protection
& VM_PROT_WRITE
)) {
10150 prot
= old_entry
->protection
& ~VM_PROT_WRITE
;
10152 if (override_nx(old_map
, VME_ALIAS(old_entry
)) && prot
)
10153 prot
|= VM_PROT_EXECUTE
;
10155 if (old_map
->mapped_in_other_pmaps
) {
10156 vm_object_pmap_protect(
10157 VME_OBJECT(old_entry
),
10158 VME_OFFSET(old_entry
),
10159 (old_entry
->vme_end
-
10160 old_entry
->vme_start
),
10162 old_entry
->vme_start
,
10165 pmap_protect(old_map
->pmap
,
10166 old_entry
->vme_start
,
10167 old_entry
->vme_end
,
10172 old_entry
->needs_copy
= FALSE
;
10173 object
= VME_OBJECT(old_entry
);
10178 * If object was using a symmetric copy strategy,
10179 * change its copy strategy to the default
10180 * asymmetric copy strategy, which is copy_delay
10181 * in the non-norma case and copy_call in the
10182 * norma case. Bump the reference count for the
10186 if(old_entry
->is_sub_map
) {
10187 vm_map_lock(VME_SUBMAP(old_entry
));
10188 vm_map_reference(VME_SUBMAP(old_entry
));
10189 vm_map_unlock(VME_SUBMAP(old_entry
));
10191 vm_object_lock(object
);
10192 vm_object_reference_locked(object
);
10193 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
) {
10194 object
->copy_strategy
= MEMORY_OBJECT_COPY_DELAY
;
10196 vm_object_unlock(object
);
10200 * Clone the entry, using object ref from above.
10201 * Mark both entries as shared.
10204 new_entry
= vm_map_entry_create(new_map
, FALSE
); /* Never the kernel
10205 * map or descendants */
10206 vm_map_entry_copy(new_entry
, old_entry
);
10207 old_entry
->is_shared
= TRUE
;
10208 new_entry
->is_shared
= TRUE
;
10211 * Insert the entry into the new map -- we
10212 * know we're inserting at the end of the new
10216 vm_map_store_entry_link(new_map
, vm_map_last_entry(new_map
), new_entry
);
10219 * Update the physical map
10222 if (old_entry
->is_sub_map
) {
10223 /* Bill Angell pmap support goes here */
10225 pmap_copy(new_map
->pmap
, old_map
->pmap
, new_entry
->vme_start
,
10226 old_entry
->vme_end
- old_entry
->vme_start
,
10227 old_entry
->vme_start
);
10234 vm_map_entry_t
*old_entry_p
,
10237 vm_map_entry_t old_entry
= *old_entry_p
;
10238 vm_map_size_t entry_size
= old_entry
->vme_end
- old_entry
->vme_start
;
10239 vm_map_offset_t start
= old_entry
->vme_start
;
10240 vm_map_copy_t copy
;
10241 vm_map_entry_t last
= vm_map_last_entry(new_map
);
10243 vm_map_unlock(old_map
);
10245 * Use maxprot version of copyin because we
10246 * care about whether this memory can ever
10247 * be accessed, not just whether it's accessible
10250 if (vm_map_copyin_maxprot(old_map
, start
, entry_size
, FALSE
, ©
)
10253 * The map might have changed while it
10254 * was unlocked, check it again. Skip
10255 * any blank space or permanently
10256 * unreadable region.
10258 vm_map_lock(old_map
);
10259 if (!vm_map_lookup_entry(old_map
, start
, &last
) ||
10260 (last
->max_protection
& VM_PROT_READ
) == VM_PROT_NONE
) {
10261 last
= last
->vme_next
;
10263 *old_entry_p
= last
;
10266 * XXX For some error returns, want to
10267 * XXX skip to the next element. Note
10268 * that INVALID_ADDRESS and
10269 * PROTECTION_FAILURE are handled above.
10276 * Insert the copy into the new map
10279 vm_map_copy_insert(new_map
, last
, copy
);
10282 * Pick up the traversal at the end of
10283 * the copied region.
10286 vm_map_lock(old_map
);
10287 start
+= entry_size
;
10288 if (! vm_map_lookup_entry(old_map
, start
, &last
)) {
10289 last
= last
->vme_next
;
10291 if (last
->vme_start
== start
) {
10293 * No need to clip here and we don't
10294 * want to cause any unnecessary
10298 vm_map_clip_start(old_map
, last
, start
);
10301 *old_entry_p
= last
;
10309 * Create and return a new map based on the old
10310 * map, according to the inheritance values on the
10311 * regions in that map.
10313 * The source map must not be locked.
10322 vm_map_entry_t old_entry
;
10323 vm_map_size_t new_size
= 0, entry_size
;
10324 vm_map_entry_t new_entry
;
10325 boolean_t src_needs_copy
;
10326 boolean_t new_entry_needs_copy
;
10327 boolean_t pmap_is64bit
;
10330 #if defined(__i386__) || defined(__x86_64__)
10331 old_map
->pmap
->pm_task_map
!= TASK_MAP_32BIT
;
10333 #error Unknown architecture.
10336 new_pmap
= pmap_create(ledger
, (vm_map_size_t
) 0, pmap_is64bit
);
10338 vm_map_reference_swap(old_map
);
10339 vm_map_lock(old_map
);
10341 new_map
= vm_map_create(new_pmap
,
10342 old_map
->min_offset
,
10343 old_map
->max_offset
,
10344 old_map
->hdr
.entries_pageable
);
10345 /* inherit the parent map's page size */
10346 vm_map_set_page_shift(new_map
, VM_MAP_PAGE_SHIFT(old_map
));
10348 old_entry
= vm_map_first_entry(old_map
);
10349 old_entry
!= vm_map_to_entry(old_map
);
10352 entry_size
= old_entry
->vme_end
- old_entry
->vme_start
;
10354 switch (old_entry
->inheritance
) {
10355 case VM_INHERIT_NONE
:
10358 case VM_INHERIT_SHARE
:
10359 vm_map_fork_share(old_map
, old_entry
, new_map
);
10360 new_size
+= entry_size
;
10363 case VM_INHERIT_COPY
:
10366 * Inline the copy_quickly case;
10367 * upon failure, fall back on call
10368 * to vm_map_fork_copy.
10371 if(old_entry
->is_sub_map
)
10373 if ((old_entry
->wired_count
!= 0) ||
10374 ((VME_OBJECT(old_entry
) != NULL
) &&
10375 (VME_OBJECT(old_entry
)->true_share
))) {
10376 goto slow_vm_map_fork_copy
;
10379 new_entry
= vm_map_entry_create(new_map
, FALSE
); /* never the kernel map or descendants */
10380 vm_map_entry_copy(new_entry
, old_entry
);
10381 if (new_entry
->is_sub_map
) {
10382 /* clear address space specifics */
10383 new_entry
->use_pmap
= FALSE
;
10386 if (! vm_object_copy_quickly(
10387 &VME_OBJECT(new_entry
),
10388 VME_OFFSET(old_entry
),
10389 (old_entry
->vme_end
-
10390 old_entry
->vme_start
),
10392 &new_entry_needs_copy
)) {
10393 vm_map_entry_dispose(new_map
, new_entry
);
10394 goto slow_vm_map_fork_copy
;
10398 * Handle copy-on-write obligations
10401 if (src_needs_copy
&& !old_entry
->needs_copy
) {
10404 prot
= old_entry
->protection
& ~VM_PROT_WRITE
;
10406 if (override_nx(old_map
, VME_ALIAS(old_entry
))
10408 prot
|= VM_PROT_EXECUTE
;
10410 vm_object_pmap_protect(
10411 VME_OBJECT(old_entry
),
10412 VME_OFFSET(old_entry
),
10413 (old_entry
->vme_end
-
10414 old_entry
->vme_start
),
10415 ((old_entry
->is_shared
10416 || old_map
->mapped_in_other_pmaps
)
10419 old_entry
->vme_start
,
10422 assert(old_entry
->wired_count
== 0);
10423 old_entry
->needs_copy
= TRUE
;
10425 new_entry
->needs_copy
= new_entry_needs_copy
;
10428 * Insert the entry at the end
10432 vm_map_store_entry_link(new_map
, vm_map_last_entry(new_map
),
10434 new_size
+= entry_size
;
10437 slow_vm_map_fork_copy
:
10438 if (vm_map_fork_copy(old_map
, &old_entry
, new_map
)) {
10439 new_size
+= entry_size
;
10443 old_entry
= old_entry
->vme_next
;
10447 new_map
->size
= new_size
;
10448 vm_map_unlock(old_map
);
10449 vm_map_deallocate(old_map
);
10457 * Setup the "new_map" with the proper execution environment according
10458 * to the type of executable (platform, 64bit, chroot environment).
10459 * Map the comm page and shared region, etc...
10468 SHARED_REGION_TRACE_DEBUG(
10469 ("shared_region: task %p: vm_map_exec(%p,%p,%p,0x%x): ->\n",
10470 (void *)VM_KERNEL_ADDRPERM(current_task()),
10471 (void *)VM_KERNEL_ADDRPERM(new_map
),
10472 (void *)VM_KERNEL_ADDRPERM(task
),
10473 (void *)VM_KERNEL_ADDRPERM(fsroot
),
10475 (void) vm_commpage_enter(new_map
, task
);
10476 (void) vm_shared_region_enter(new_map
, task
, fsroot
, cpu
);
10477 SHARED_REGION_TRACE_DEBUG(
10478 ("shared_region: task %p: vm_map_exec(%p,%p,%p,0x%x): <-\n",
10479 (void *)VM_KERNEL_ADDRPERM(current_task()),
10480 (void *)VM_KERNEL_ADDRPERM(new_map
),
10481 (void *)VM_KERNEL_ADDRPERM(task
),
10482 (void *)VM_KERNEL_ADDRPERM(fsroot
),
10484 return KERN_SUCCESS
;
10488 * vm_map_lookup_locked:
10490 * Finds the VM object, offset, and
10491 * protection for a given virtual address in the
10492 * specified map, assuming a page fault of the
10495 * Returns the (object, offset, protection) for
10496 * this address, whether it is wired down, and whether
10497 * this map has the only reference to the data in question.
10498 * In order to later verify this lookup, a "version"
10501 * The map MUST be locked by the caller and WILL be
10502 * locked on exit. In order to guarantee the
10503 * existence of the returned object, it is returned
10506 * If a lookup is requested with "write protection"
10507 * specified, the map may be changed to perform virtual
10508 * copying operations, although the data referenced will
10512 vm_map_lookup_locked(
10513 vm_map_t
*var_map
, /* IN/OUT */
10514 vm_map_offset_t vaddr
,
10515 vm_prot_t fault_type
,
10516 int object_lock_type
,
10517 vm_map_version_t
*out_version
, /* OUT */
10518 vm_object_t
*object
, /* OUT */
10519 vm_object_offset_t
*offset
, /* OUT */
10520 vm_prot_t
*out_prot
, /* OUT */
10521 boolean_t
*wired
, /* OUT */
10522 vm_object_fault_info_t fault_info
, /* OUT */
10523 vm_map_t
*real_map
)
10525 vm_map_entry_t entry
;
10526 register vm_map_t map
= *var_map
;
10527 vm_map_t old_map
= *var_map
;
10528 vm_map_t cow_sub_map_parent
= VM_MAP_NULL
;
10529 vm_map_offset_t cow_parent_vaddr
= 0;
10530 vm_map_offset_t old_start
= 0;
10531 vm_map_offset_t old_end
= 0;
10532 register vm_prot_t prot
;
10533 boolean_t mask_protections
;
10534 boolean_t force_copy
;
10535 vm_prot_t original_fault_type
;
10538 * VM_PROT_MASK means that the caller wants us to use "fault_type"
10539 * as a mask against the mapping's actual protections, not as an
10542 mask_protections
= (fault_type
& VM_PROT_IS_MASK
) ? TRUE
: FALSE
;
10543 force_copy
= (fault_type
& VM_PROT_COPY
) ? TRUE
: FALSE
;
10544 fault_type
&= VM_PROT_ALL
;
10545 original_fault_type
= fault_type
;
10550 fault_type
= original_fault_type
;
10553 * If the map has an interesting hint, try it before calling
10554 * full blown lookup routine.
10558 if ((entry
== vm_map_to_entry(map
)) ||
10559 (vaddr
< entry
->vme_start
) || (vaddr
>= entry
->vme_end
)) {
10560 vm_map_entry_t tmp_entry
;
10563 * Entry was either not a valid hint, or the vaddr
10564 * was not contained in the entry, so do a full lookup.
10566 if (!vm_map_lookup_entry(map
, vaddr
, &tmp_entry
)) {
10567 if((cow_sub_map_parent
) && (cow_sub_map_parent
!= map
))
10568 vm_map_unlock(cow_sub_map_parent
);
10569 if((*real_map
!= map
)
10570 && (*real_map
!= cow_sub_map_parent
))
10571 vm_map_unlock(*real_map
);
10572 return KERN_INVALID_ADDRESS
;
10577 if(map
== old_map
) {
10578 old_start
= entry
->vme_start
;
10579 old_end
= entry
->vme_end
;
10583 * Handle submaps. Drop lock on upper map, submap is
10588 if (entry
->is_sub_map
) {
10589 vm_map_offset_t local_vaddr
;
10590 vm_map_offset_t end_delta
;
10591 vm_map_offset_t start_delta
;
10592 vm_map_entry_t submap_entry
;
10593 boolean_t mapped_needs_copy
=FALSE
;
10595 local_vaddr
= vaddr
;
10597 if ((entry
->use_pmap
&& !(fault_type
& VM_PROT_WRITE
))) {
10598 /* if real_map equals map we unlock below */
10599 if ((*real_map
!= map
) &&
10600 (*real_map
!= cow_sub_map_parent
))
10601 vm_map_unlock(*real_map
);
10602 *real_map
= VME_SUBMAP(entry
);
10605 if(entry
->needs_copy
&& (fault_type
& VM_PROT_WRITE
)) {
10606 if (!mapped_needs_copy
) {
10607 if (vm_map_lock_read_to_write(map
)) {
10608 vm_map_lock_read(map
);
10612 vm_map_lock_read(VME_SUBMAP(entry
));
10613 *var_map
= VME_SUBMAP(entry
);
10614 cow_sub_map_parent
= map
;
10615 /* reset base to map before cow object */
10616 /* this is the map which will accept */
10617 /* the new cow object */
10618 old_start
= entry
->vme_start
;
10619 old_end
= entry
->vme_end
;
10620 cow_parent_vaddr
= vaddr
;
10621 mapped_needs_copy
= TRUE
;
10623 vm_map_lock_read(VME_SUBMAP(entry
));
10624 *var_map
= VME_SUBMAP(entry
);
10625 if((cow_sub_map_parent
!= map
) &&
10626 (*real_map
!= map
))
10627 vm_map_unlock(map
);
10630 vm_map_lock_read(VME_SUBMAP(entry
));
10631 *var_map
= VME_SUBMAP(entry
);
10632 /* leave map locked if it is a target */
10633 /* cow sub_map above otherwise, just */
10634 /* follow the maps down to the object */
10635 /* here we unlock knowing we are not */
10636 /* revisiting the map. */
10637 if((*real_map
!= map
) && (map
!= cow_sub_map_parent
))
10638 vm_map_unlock_read(map
);
10643 /* calculate the offset in the submap for vaddr */
10644 local_vaddr
= (local_vaddr
- entry
->vme_start
) + VME_OFFSET(entry
);
10647 if(!vm_map_lookup_entry(map
, local_vaddr
, &submap_entry
)) {
10648 if((cow_sub_map_parent
) && (cow_sub_map_parent
!= map
)){
10649 vm_map_unlock(cow_sub_map_parent
);
10651 if((*real_map
!= map
)
10652 && (*real_map
!= cow_sub_map_parent
)) {
10653 vm_map_unlock(*real_map
);
10656 return KERN_INVALID_ADDRESS
;
10659 /* find the attenuated shadow of the underlying object */
10660 /* on our target map */
10662 /* in english the submap object may extend beyond the */
10663 /* region mapped by the entry or, may only fill a portion */
10664 /* of it. For our purposes, we only care if the object */
10665 /* doesn't fill. In this case the area which will */
10666 /* ultimately be clipped in the top map will only need */
10667 /* to be as big as the portion of the underlying entry */
10668 /* which is mapped */
10669 start_delta
= submap_entry
->vme_start
> VME_OFFSET(entry
) ?
10670 submap_entry
->vme_start
- VME_OFFSET(entry
) : 0;
10673 (VME_OFFSET(entry
) + start_delta
+ (old_end
- old_start
)) <=
10674 submap_entry
->vme_end
?
10675 0 : (VME_OFFSET(entry
) +
10676 (old_end
- old_start
))
10677 - submap_entry
->vme_end
;
10679 old_start
+= start_delta
;
10680 old_end
-= end_delta
;
10682 if(submap_entry
->is_sub_map
) {
10683 entry
= submap_entry
;
10684 vaddr
= local_vaddr
;
10685 goto submap_recurse
;
10688 if(((fault_type
& VM_PROT_WRITE
) && cow_sub_map_parent
)) {
10690 vm_object_t sub_object
, copy_object
;
10691 vm_object_offset_t copy_offset
;
10692 vm_map_offset_t local_start
;
10693 vm_map_offset_t local_end
;
10694 boolean_t copied_slowly
= FALSE
;
10696 if (vm_map_lock_read_to_write(map
)) {
10697 vm_map_lock_read(map
);
10698 old_start
-= start_delta
;
10699 old_end
+= end_delta
;
10704 sub_object
= VME_OBJECT(submap_entry
);
10705 if (sub_object
== VM_OBJECT_NULL
) {
10707 vm_object_allocate(
10709 (submap_entry
->vme_end
-
10710 submap_entry
->vme_start
));
10711 VME_OBJECT_SET(submap_entry
, sub_object
);
10712 VME_OFFSET_SET(submap_entry
, 0);
10714 local_start
= local_vaddr
-
10715 (cow_parent_vaddr
- old_start
);
10716 local_end
= local_vaddr
+
10717 (old_end
- cow_parent_vaddr
);
10718 vm_map_clip_start(map
, submap_entry
, local_start
);
10719 vm_map_clip_end(map
, submap_entry
, local_end
);
10720 if (submap_entry
->is_sub_map
) {
10721 /* unnesting was done when clipping */
10722 assert(!submap_entry
->use_pmap
);
10725 /* This is the COW case, lets connect */
10726 /* an entry in our space to the underlying */
10727 /* object in the submap, bypassing the */
10731 if(submap_entry
->wired_count
!= 0 ||
10732 (sub_object
->copy_strategy
==
10733 MEMORY_OBJECT_COPY_NONE
)) {
10734 vm_object_lock(sub_object
);
10735 vm_object_copy_slowly(sub_object
,
10736 VME_OFFSET(submap_entry
),
10737 (submap_entry
->vme_end
-
10738 submap_entry
->vme_start
),
10741 copied_slowly
= TRUE
;
10744 /* set up shadow object */
10745 copy_object
= sub_object
;
10746 vm_object_reference(copy_object
);
10747 sub_object
->shadowed
= TRUE
;
10748 assert(submap_entry
->wired_count
== 0);
10749 submap_entry
->needs_copy
= TRUE
;
10751 prot
= submap_entry
->protection
& ~VM_PROT_WRITE
;
10753 if (override_nx(old_map
,
10754 VME_ALIAS(submap_entry
))
10756 prot
|= VM_PROT_EXECUTE
;
10758 vm_object_pmap_protect(
10760 VME_OFFSET(submap_entry
),
10761 submap_entry
->vme_end
-
10762 submap_entry
->vme_start
,
10763 (submap_entry
->is_shared
10764 || map
->mapped_in_other_pmaps
) ?
10765 PMAP_NULL
: map
->pmap
,
10766 submap_entry
->vme_start
,
10771 * Adjust the fault offset to the submap entry.
10773 copy_offset
= (local_vaddr
-
10774 submap_entry
->vme_start
+
10775 VME_OFFSET(submap_entry
));
10777 /* This works diffently than the */
10778 /* normal submap case. We go back */
10779 /* to the parent of the cow map and*/
10780 /* clip out the target portion of */
10781 /* the sub_map, substituting the */
10782 /* new copy object, */
10784 vm_map_unlock(map
);
10785 local_start
= old_start
;
10786 local_end
= old_end
;
10787 map
= cow_sub_map_parent
;
10788 *var_map
= cow_sub_map_parent
;
10789 vaddr
= cow_parent_vaddr
;
10790 cow_sub_map_parent
= NULL
;
10792 if(!vm_map_lookup_entry(map
,
10794 vm_object_deallocate(
10796 vm_map_lock_write_to_read(map
);
10797 return KERN_INVALID_ADDRESS
;
10800 /* clip out the portion of space */
10801 /* mapped by the sub map which */
10802 /* corresponds to the underlying */
10806 * Clip (and unnest) the smallest nested chunk
10807 * possible around the faulting address...
10809 local_start
= vaddr
& ~(pmap_nesting_size_min
- 1);
10810 local_end
= local_start
+ pmap_nesting_size_min
;
10812 * ... but don't go beyond the "old_start" to "old_end"
10813 * range, to avoid spanning over another VM region
10814 * with a possibly different VM object and/or offset.
10816 if (local_start
< old_start
) {
10817 local_start
= old_start
;
10819 if (local_end
> old_end
) {
10820 local_end
= old_end
;
10823 * Adjust copy_offset to the start of the range.
10825 copy_offset
-= (vaddr
- local_start
);
10827 vm_map_clip_start(map
, entry
, local_start
);
10828 vm_map_clip_end(map
, entry
, local_end
);
10829 if (entry
->is_sub_map
) {
10830 /* unnesting was done when clipping */
10831 assert(!entry
->use_pmap
);
10834 /* substitute copy object for */
10835 /* shared map entry */
10836 vm_map_deallocate(VME_SUBMAP(entry
));
10837 assert(!entry
->iokit_acct
);
10838 entry
->is_sub_map
= FALSE
;
10839 entry
->use_pmap
= TRUE
;
10840 VME_OBJECT_SET(entry
, copy_object
);
10842 /* propagate the submap entry's protections */
10843 entry
->protection
|= submap_entry
->protection
;
10844 entry
->max_protection
|= submap_entry
->max_protection
;
10846 if(copied_slowly
) {
10847 VME_OFFSET_SET(entry
, local_start
- old_start
);
10848 entry
->needs_copy
= FALSE
;
10849 entry
->is_shared
= FALSE
;
10851 VME_OFFSET_SET(entry
, copy_offset
);
10852 assert(entry
->wired_count
== 0);
10853 entry
->needs_copy
= TRUE
;
10854 if(entry
->inheritance
== VM_INHERIT_SHARE
)
10855 entry
->inheritance
= VM_INHERIT_COPY
;
10856 if (map
!= old_map
)
10857 entry
->is_shared
= TRUE
;
10859 if(entry
->inheritance
== VM_INHERIT_SHARE
)
10860 entry
->inheritance
= VM_INHERIT_COPY
;
10862 vm_map_lock_write_to_read(map
);
10864 if((cow_sub_map_parent
)
10865 && (cow_sub_map_parent
!= *real_map
)
10866 && (cow_sub_map_parent
!= map
)) {
10867 vm_map_unlock(cow_sub_map_parent
);
10869 entry
= submap_entry
;
10870 vaddr
= local_vaddr
;
10875 * Check whether this task is allowed to have
10879 prot
= entry
->protection
;
10881 if (override_nx(old_map
, VME_ALIAS(entry
)) && prot
) {
10883 * HACK -- if not a stack, then allow execution
10885 prot
|= VM_PROT_EXECUTE
;
10888 if (mask_protections
) {
10889 fault_type
&= prot
;
10890 if (fault_type
== VM_PROT_NONE
) {
10891 goto protection_failure
;
10894 if ((fault_type
& (prot
)) != fault_type
) {
10895 protection_failure
:
10896 if (*real_map
!= map
) {
10897 vm_map_unlock(*real_map
);
10901 if ((fault_type
& VM_PROT_EXECUTE
) && prot
)
10902 log_stack_execution_failure((addr64_t
)vaddr
, prot
);
10904 DTRACE_VM2(prot_fault
, int, 1, (uint64_t *), NULL
);
10905 return KERN_PROTECTION_FAILURE
;
10909 * If this page is not pageable, we have to get
10910 * it for all possible accesses.
10913 *wired
= (entry
->wired_count
!= 0);
10918 * If the entry was copy-on-write, we either ...
10921 if (entry
->needs_copy
) {
10923 * If we want to write the page, we may as well
10924 * handle that now since we've got the map locked.
10926 * If we don't need to write the page, we just
10927 * demote the permissions allowed.
10930 if ((fault_type
& VM_PROT_WRITE
) || *wired
|| force_copy
) {
10932 * Make a new object, and place it in the
10933 * object chain. Note that no new references
10934 * have appeared -- one just moved from the
10935 * map to the new object.
10938 if (vm_map_lock_read_to_write(map
)) {
10939 vm_map_lock_read(map
);
10942 VME_OBJECT_SHADOW(entry
,
10943 (vm_map_size_t
) (entry
->vme_end
-
10944 entry
->vme_start
));
10946 VME_OBJECT(entry
)->shadowed
= TRUE
;
10947 entry
->needs_copy
= FALSE
;
10948 vm_map_lock_write_to_read(map
);
10952 * We're attempting to read a copy-on-write
10953 * page -- don't allow writes.
10956 prot
&= (~VM_PROT_WRITE
);
10961 * Create an object if necessary.
10963 if (VME_OBJECT(entry
) == VM_OBJECT_NULL
) {
10965 if (vm_map_lock_read_to_write(map
)) {
10966 vm_map_lock_read(map
);
10970 VME_OBJECT_SET(entry
,
10971 vm_object_allocate(
10972 (vm_map_size_t
)(entry
->vme_end
-
10973 entry
->vme_start
)));
10974 VME_OFFSET_SET(entry
, 0);
10975 vm_map_lock_write_to_read(map
);
10979 * Return the object/offset from this entry. If the entry
10980 * was copy-on-write or empty, it has been fixed up. Also
10981 * return the protection.
10984 *offset
= (vaddr
- entry
->vme_start
) + VME_OFFSET(entry
);
10985 *object
= VME_OBJECT(entry
);
10989 fault_info
->interruptible
= THREAD_UNINT
; /* for now... */
10990 /* ... the caller will change "interruptible" if needed */
10991 fault_info
->cluster_size
= 0;
10992 fault_info
->user_tag
= VME_ALIAS(entry
);
10993 fault_info
->pmap_options
= 0;
10994 if (entry
->iokit_acct
||
10995 (!entry
->is_sub_map
&& !entry
->use_pmap
)) {
10996 fault_info
->pmap_options
|= PMAP_OPTIONS_ALT_ACCT
;
10998 fault_info
->behavior
= entry
->behavior
;
10999 fault_info
->lo_offset
= VME_OFFSET(entry
);
11000 fault_info
->hi_offset
=
11001 (entry
->vme_end
- entry
->vme_start
) + VME_OFFSET(entry
);
11002 fault_info
->no_cache
= entry
->no_cache
;
11003 fault_info
->stealth
= FALSE
;
11004 fault_info
->io_sync
= FALSE
;
11005 if (entry
->used_for_jit
||
11006 entry
->vme_resilient_codesign
) {
11007 fault_info
->cs_bypass
= TRUE
;
11009 fault_info
->cs_bypass
= FALSE
;
11011 fault_info
->mark_zf_absent
= FALSE
;
11012 fault_info
->batch_pmap_op
= FALSE
;
11016 * Lock the object to prevent it from disappearing
11018 if (object_lock_type
== OBJECT_LOCK_EXCLUSIVE
)
11019 vm_object_lock(*object
);
11021 vm_object_lock_shared(*object
);
11024 * Save the version number
11027 out_version
->main_timestamp
= map
->timestamp
;
11029 return KERN_SUCCESS
;
11036 * Verifies that the map in question has not changed
11037 * since the given version. If successful, the map
11038 * will not change until vm_map_verify_done() is called.
11042 register vm_map_t map
,
11043 register vm_map_version_t
*version
) /* REF */
11047 vm_map_lock_read(map
);
11048 result
= (map
->timestamp
== version
->main_timestamp
);
11051 vm_map_unlock_read(map
);
11057 * vm_map_verify_done:
11059 * Releases locks acquired by a vm_map_verify.
11061 * This is now a macro in vm/vm_map.h. It does a
11062 * vm_map_unlock_read on the map.
11067 * TEMPORARYTEMPORARYTEMPORARYTEMPORARYTEMPORARYTEMPORARY
11068 * Goes away after regular vm_region_recurse function migrates to
11070 * vm_region_recurse: A form of vm_region which follows the
11071 * submaps in a target map
11076 vm_map_region_recurse_64(
11078 vm_map_offset_t
*address
, /* IN/OUT */
11079 vm_map_size_t
*size
, /* OUT */
11080 natural_t
*nesting_depth
, /* IN/OUT */
11081 vm_region_submap_info_64_t submap_info
, /* IN/OUT */
11082 mach_msg_type_number_t
*count
) /* IN/OUT */
11084 mach_msg_type_number_t original_count
;
11085 vm_region_extended_info_data_t extended
;
11086 vm_map_entry_t tmp_entry
;
11087 vm_map_offset_t user_address
;
11088 unsigned int user_max_depth
;
11091 * "curr_entry" is the VM map entry preceding or including the
11092 * address we're looking for.
11093 * "curr_map" is the map or sub-map containing "curr_entry".
11094 * "curr_address" is the equivalent of the top map's "user_address"
11095 * in the current map.
11096 * "curr_offset" is the cumulated offset of "curr_map" in the
11097 * target task's address space.
11098 * "curr_depth" is the depth of "curr_map" in the chain of
11101 * "curr_max_below" and "curr_max_above" limit the range (around
11102 * "curr_address") we should take into account in the current (sub)map.
11103 * They limit the range to what's visible through the map entries
11104 * we've traversed from the top map to the current map.
11107 vm_map_entry_t curr_entry
;
11108 vm_map_address_t curr_address
;
11109 vm_map_offset_t curr_offset
;
11111 unsigned int curr_depth
;
11112 vm_map_offset_t curr_max_below
, curr_max_above
;
11113 vm_map_offset_t curr_skip
;
11116 * "next_" is the same as "curr_" but for the VM region immediately
11117 * after the address we're looking for. We need to keep track of this
11118 * too because we want to return info about that region if the
11119 * address we're looking for is not mapped.
11121 vm_map_entry_t next_entry
;
11122 vm_map_offset_t next_offset
;
11123 vm_map_offset_t next_address
;
11125 unsigned int next_depth
;
11126 vm_map_offset_t next_max_below
, next_max_above
;
11127 vm_map_offset_t next_skip
;
11129 boolean_t look_for_pages
;
11130 vm_region_submap_short_info_64_t short_info
;
11132 if (map
== VM_MAP_NULL
) {
11133 /* no address space to work on */
11134 return KERN_INVALID_ARGUMENT
;
11138 if (*count
< VM_REGION_SUBMAP_SHORT_INFO_COUNT_64
) {
11140 * "info" structure is not big enough and
11143 return KERN_INVALID_ARGUMENT
;
11146 original_count
= *count
;
11148 if (original_count
< VM_REGION_SUBMAP_INFO_V0_COUNT_64
) {
11149 *count
= VM_REGION_SUBMAP_SHORT_INFO_COUNT_64
;
11150 look_for_pages
= FALSE
;
11151 short_info
= (vm_region_submap_short_info_64_t
) submap_info
;
11152 submap_info
= NULL
;
11154 look_for_pages
= TRUE
;
11155 *count
= VM_REGION_SUBMAP_INFO_V0_COUNT_64
;
11158 if (original_count
>= VM_REGION_SUBMAP_INFO_V1_COUNT_64
) {
11159 *count
= VM_REGION_SUBMAP_INFO_V1_COUNT_64
;
11163 user_address
= *address
;
11164 user_max_depth
= *nesting_depth
;
11167 vm_map_lock_read(map
);
11173 curr_address
= user_address
;
11177 curr_max_above
= ((vm_map_offset_t
) -1) - curr_address
;
11178 curr_max_below
= curr_address
;
11186 next_max_above
= (vm_map_offset_t
) -1;
11187 next_max_below
= (vm_map_offset_t
) -1;
11190 if (vm_map_lookup_entry(curr_map
,
11193 /* tmp_entry contains the address we're looking for */
11194 curr_entry
= tmp_entry
;
11196 vm_map_offset_t skip
;
11198 * The address is not mapped. "tmp_entry" is the
11199 * map entry preceding the address. We want the next
11200 * one, if it exists.
11202 curr_entry
= tmp_entry
->vme_next
;
11204 if (curr_entry
== vm_map_to_entry(curr_map
) ||
11205 (curr_entry
->vme_start
>=
11206 curr_address
+ curr_max_above
)) {
11207 /* no next entry at this level: stop looking */
11209 vm_map_unlock_read(curr_map
);
11216 curr_max_above
= 0;
11217 curr_max_below
= 0;
11221 /* adjust current address and offset */
11222 skip
= curr_entry
->vme_start
- curr_address
;
11223 curr_address
= curr_entry
->vme_start
;
11225 curr_offset
+= skip
;
11226 curr_max_above
-= skip
;
11227 curr_max_below
= 0;
11231 * Is the next entry at this level closer to the address (or
11232 * deeper in the submap chain) than the one we had
11235 tmp_entry
= curr_entry
->vme_next
;
11236 if (tmp_entry
== vm_map_to_entry(curr_map
)) {
11237 /* no next entry at this level */
11238 } else if (tmp_entry
->vme_start
>=
11239 curr_address
+ curr_max_above
) {
11241 * tmp_entry is beyond the scope of what we mapped of
11242 * this submap in the upper level: ignore it.
11244 } else if ((next_entry
== NULL
) ||
11245 (tmp_entry
->vme_start
+ curr_offset
<=
11246 next_entry
->vme_start
+ next_offset
)) {
11248 * We didn't have a "next_entry" or this one is
11249 * closer to the address we're looking for:
11250 * use this "tmp_entry" as the new "next_entry".
11252 if (next_entry
!= NULL
) {
11253 /* unlock the last "next_map" */
11254 if (next_map
!= curr_map
&& not_in_kdp
) {
11255 vm_map_unlock_read(next_map
);
11258 next_entry
= tmp_entry
;
11259 next_map
= curr_map
;
11260 next_depth
= curr_depth
;
11261 next_address
= next_entry
->vme_start
;
11262 next_skip
= curr_skip
;
11263 next_skip
+= (next_address
- curr_address
);
11264 next_offset
= curr_offset
;
11265 next_offset
+= (next_address
- curr_address
);
11266 next_max_above
= MIN(next_max_above
, curr_max_above
);
11267 next_max_above
= MIN(next_max_above
,
11268 next_entry
->vme_end
- next_address
);
11269 next_max_below
= MIN(next_max_below
, curr_max_below
);
11270 next_max_below
= MIN(next_max_below
,
11271 next_address
- next_entry
->vme_start
);
11275 * "curr_max_{above,below}" allow us to keep track of the
11276 * portion of the submap that is actually mapped at this level:
11277 * the rest of that submap is irrelevant to us, since it's not
11279 * The relevant portion of the map starts at
11280 * "VME_OFFSET(curr_entry)" up to the size of "curr_entry".
11282 curr_max_above
= MIN(curr_max_above
,
11283 curr_entry
->vme_end
- curr_address
);
11284 curr_max_below
= MIN(curr_max_below
,
11285 curr_address
- curr_entry
->vme_start
);
11287 if (!curr_entry
->is_sub_map
||
11288 curr_depth
>= user_max_depth
) {
11290 * We hit a leaf map or we reached the maximum depth
11291 * we could, so stop looking. Keep the current map
11298 * Get down to the next submap level.
11302 * Lock the next level and unlock the current level,
11303 * unless we need to keep it locked to access the "next_entry"
11307 vm_map_lock_read(VME_SUBMAP(curr_entry
));
11309 if (curr_map
== next_map
) {
11310 /* keep "next_map" locked in case we need it */
11312 /* release this map */
11314 vm_map_unlock_read(curr_map
);
11318 * Adjust the offset. "curr_entry" maps the submap
11319 * at relative address "curr_entry->vme_start" in the
11320 * curr_map but skips the first "VME_OFFSET(curr_entry)"
11321 * bytes of the submap.
11322 * "curr_offset" always represents the offset of a virtual
11323 * address in the curr_map relative to the absolute address
11324 * space (i.e. the top-level VM map).
11327 (VME_OFFSET(curr_entry
) - curr_entry
->vme_start
);
11328 curr_address
= user_address
+ curr_offset
;
11329 /* switch to the submap */
11330 curr_map
= VME_SUBMAP(curr_entry
);
11335 if (curr_entry
== NULL
) {
11336 /* no VM region contains the address... */
11337 if (next_entry
== NULL
) {
11338 /* ... and no VM region follows it either */
11339 return KERN_INVALID_ADDRESS
;
11341 /* ... gather info about the next VM region */
11342 curr_entry
= next_entry
;
11343 curr_map
= next_map
; /* still locked ... */
11344 curr_address
= next_address
;
11345 curr_skip
= next_skip
;
11346 curr_offset
= next_offset
;
11347 curr_depth
= next_depth
;
11348 curr_max_above
= next_max_above
;
11349 curr_max_below
= next_max_below
;
11351 /* we won't need "next_entry" after all */
11352 if (next_entry
!= NULL
) {
11353 /* release "next_map" */
11354 if (next_map
!= curr_map
&& not_in_kdp
) {
11355 vm_map_unlock_read(next_map
);
11364 next_max_below
= -1;
11365 next_max_above
= -1;
11367 if (curr_entry
->is_sub_map
&&
11368 curr_depth
< user_max_depth
) {
11370 * We're not as deep as we could be: we must have
11371 * gone back up after not finding anything mapped
11372 * below the original top-level map entry's.
11373 * Let's move "curr_address" forward and recurse again.
11375 user_address
= curr_address
;
11376 goto recurse_again
;
11379 *nesting_depth
= curr_depth
;
11380 *size
= curr_max_above
+ curr_max_below
;
11381 *address
= user_address
+ curr_skip
- curr_max_below
;
11383 // LP64todo: all the current tools are 32bit, obviously never worked for 64b
11384 // so probably should be a real 32b ID vs. ptr.
11385 // Current users just check for equality
11386 #define INFO_MAKE_OBJECT_ID(p) ((uint32_t)(uintptr_t)VM_KERNEL_ADDRPERM(p))
11388 if (look_for_pages
) {
11389 submap_info
->user_tag
= VME_ALIAS(curr_entry
);
11390 submap_info
->offset
= VME_OFFSET(curr_entry
);
11391 submap_info
->protection
= curr_entry
->protection
;
11392 submap_info
->inheritance
= curr_entry
->inheritance
;
11393 submap_info
->max_protection
= curr_entry
->max_protection
;
11394 submap_info
->behavior
= curr_entry
->behavior
;
11395 submap_info
->user_wired_count
= curr_entry
->user_wired_count
;
11396 submap_info
->is_submap
= curr_entry
->is_sub_map
;
11397 submap_info
->object_id
= INFO_MAKE_OBJECT_ID(VME_OBJECT(curr_entry
));
11399 short_info
->user_tag
= VME_ALIAS(curr_entry
);
11400 short_info
->offset
= VME_OFFSET(curr_entry
);
11401 short_info
->protection
= curr_entry
->protection
;
11402 short_info
->inheritance
= curr_entry
->inheritance
;
11403 short_info
->max_protection
= curr_entry
->max_protection
;
11404 short_info
->behavior
= curr_entry
->behavior
;
11405 short_info
->user_wired_count
= curr_entry
->user_wired_count
;
11406 short_info
->is_submap
= curr_entry
->is_sub_map
;
11407 short_info
->object_id
= INFO_MAKE_OBJECT_ID(VME_OBJECT(curr_entry
));
11410 extended
.pages_resident
= 0;
11411 extended
.pages_swapped_out
= 0;
11412 extended
.pages_shared_now_private
= 0;
11413 extended
.pages_dirtied
= 0;
11414 extended
.pages_reusable
= 0;
11415 extended
.external_pager
= 0;
11416 extended
.shadow_depth
= 0;
11417 extended
.share_mode
= SM_EMPTY
;
11418 extended
.ref_count
= 0;
11421 if (!curr_entry
->is_sub_map
) {
11422 vm_map_offset_t range_start
, range_end
;
11423 range_start
= MAX((curr_address
- curr_max_below
),
11424 curr_entry
->vme_start
);
11425 range_end
= MIN((curr_address
+ curr_max_above
),
11426 curr_entry
->vme_end
);
11427 vm_map_region_walk(curr_map
,
11430 (VME_OFFSET(curr_entry
) +
11432 curr_entry
->vme_start
)),
11433 range_end
- range_start
,
11435 look_for_pages
, VM_REGION_EXTENDED_INFO_COUNT
);
11436 if (extended
.external_pager
&&
11437 extended
.ref_count
== 2 &&
11438 extended
.share_mode
== SM_SHARED
) {
11439 extended
.share_mode
= SM_PRIVATE
;
11442 if (curr_entry
->use_pmap
) {
11443 extended
.share_mode
= SM_TRUESHARED
;
11445 extended
.share_mode
= SM_PRIVATE
;
11447 extended
.ref_count
= VME_SUBMAP(curr_entry
)->ref_count
;
11451 if (look_for_pages
) {
11452 submap_info
->pages_resident
= extended
.pages_resident
;
11453 submap_info
->pages_swapped_out
= extended
.pages_swapped_out
;
11454 submap_info
->pages_shared_now_private
=
11455 extended
.pages_shared_now_private
;
11456 submap_info
->pages_dirtied
= extended
.pages_dirtied
;
11457 submap_info
->external_pager
= extended
.external_pager
;
11458 submap_info
->shadow_depth
= extended
.shadow_depth
;
11459 submap_info
->share_mode
= extended
.share_mode
;
11460 submap_info
->ref_count
= extended
.ref_count
;
11462 if (original_count
>= VM_REGION_SUBMAP_INFO_V1_COUNT_64
) {
11463 submap_info
->pages_reusable
= extended
.pages_reusable
;
11466 short_info
->external_pager
= extended
.external_pager
;
11467 short_info
->shadow_depth
= extended
.shadow_depth
;
11468 short_info
->share_mode
= extended
.share_mode
;
11469 short_info
->ref_count
= extended
.ref_count
;
11473 vm_map_unlock_read(curr_map
);
11476 return KERN_SUCCESS
;
11482 * User call to obtain information about a region in
11483 * a task's address map. Currently, only one flavor is
11486 * XXX The reserved and behavior fields cannot be filled
11487 * in until the vm merge from the IK is completed, and
11488 * vm_reserve is implemented.
11494 vm_map_offset_t
*address
, /* IN/OUT */
11495 vm_map_size_t
*size
, /* OUT */
11496 vm_region_flavor_t flavor
, /* IN */
11497 vm_region_info_t info
, /* OUT */
11498 mach_msg_type_number_t
*count
, /* IN/OUT */
11499 mach_port_t
*object_name
) /* OUT */
11501 vm_map_entry_t tmp_entry
;
11502 vm_map_entry_t entry
;
11503 vm_map_offset_t start
;
11505 if (map
== VM_MAP_NULL
)
11506 return(KERN_INVALID_ARGUMENT
);
11510 case VM_REGION_BASIC_INFO
:
11511 /* legacy for old 32-bit objects info */
11513 vm_region_basic_info_t basic
;
11515 if (*count
< VM_REGION_BASIC_INFO_COUNT
)
11516 return(KERN_INVALID_ARGUMENT
);
11518 basic
= (vm_region_basic_info_t
) info
;
11519 *count
= VM_REGION_BASIC_INFO_COUNT
;
11521 vm_map_lock_read(map
);
11524 if (!vm_map_lookup_entry(map
, start
, &tmp_entry
)) {
11525 if ((entry
= tmp_entry
->vme_next
) == vm_map_to_entry(map
)) {
11526 vm_map_unlock_read(map
);
11527 return(KERN_INVALID_ADDRESS
);
11533 start
= entry
->vme_start
;
11535 basic
->offset
= (uint32_t)VME_OFFSET(entry
);
11536 basic
->protection
= entry
->protection
;
11537 basic
->inheritance
= entry
->inheritance
;
11538 basic
->max_protection
= entry
->max_protection
;
11539 basic
->behavior
= entry
->behavior
;
11540 basic
->user_wired_count
= entry
->user_wired_count
;
11541 basic
->reserved
= entry
->is_sub_map
;
11543 *size
= (entry
->vme_end
- start
);
11545 if (object_name
) *object_name
= IP_NULL
;
11546 if (entry
->is_sub_map
) {
11547 basic
->shared
= FALSE
;
11549 basic
->shared
= entry
->is_shared
;
11552 vm_map_unlock_read(map
);
11553 return(KERN_SUCCESS
);
11556 case VM_REGION_BASIC_INFO_64
:
11558 vm_region_basic_info_64_t basic
;
11560 if (*count
< VM_REGION_BASIC_INFO_COUNT_64
)
11561 return(KERN_INVALID_ARGUMENT
);
11563 basic
= (vm_region_basic_info_64_t
) info
;
11564 *count
= VM_REGION_BASIC_INFO_COUNT_64
;
11566 vm_map_lock_read(map
);
11569 if (!vm_map_lookup_entry(map
, start
, &tmp_entry
)) {
11570 if ((entry
= tmp_entry
->vme_next
) == vm_map_to_entry(map
)) {
11571 vm_map_unlock_read(map
);
11572 return(KERN_INVALID_ADDRESS
);
11578 start
= entry
->vme_start
;
11580 basic
->offset
= VME_OFFSET(entry
);
11581 basic
->protection
= entry
->protection
;
11582 basic
->inheritance
= entry
->inheritance
;
11583 basic
->max_protection
= entry
->max_protection
;
11584 basic
->behavior
= entry
->behavior
;
11585 basic
->user_wired_count
= entry
->user_wired_count
;
11586 basic
->reserved
= entry
->is_sub_map
;
11588 *size
= (entry
->vme_end
- start
);
11590 if (object_name
) *object_name
= IP_NULL
;
11591 if (entry
->is_sub_map
) {
11592 basic
->shared
= FALSE
;
11594 basic
->shared
= entry
->is_shared
;
11597 vm_map_unlock_read(map
);
11598 return(KERN_SUCCESS
);
11600 case VM_REGION_EXTENDED_INFO
:
11601 if (*count
< VM_REGION_EXTENDED_INFO_COUNT
)
11602 return(KERN_INVALID_ARGUMENT
);
11604 case VM_REGION_EXTENDED_INFO__legacy
:
11605 if (*count
< VM_REGION_EXTENDED_INFO_COUNT__legacy
)
11606 return KERN_INVALID_ARGUMENT
;
11609 vm_region_extended_info_t extended
;
11610 mach_msg_type_number_t original_count
;
11612 extended
= (vm_region_extended_info_t
) info
;
11614 vm_map_lock_read(map
);
11617 if (!vm_map_lookup_entry(map
, start
, &tmp_entry
)) {
11618 if ((entry
= tmp_entry
->vme_next
) == vm_map_to_entry(map
)) {
11619 vm_map_unlock_read(map
);
11620 return(KERN_INVALID_ADDRESS
);
11625 start
= entry
->vme_start
;
11627 extended
->protection
= entry
->protection
;
11628 extended
->user_tag
= VME_ALIAS(entry
);
11629 extended
->pages_resident
= 0;
11630 extended
->pages_swapped_out
= 0;
11631 extended
->pages_shared_now_private
= 0;
11632 extended
->pages_dirtied
= 0;
11633 extended
->external_pager
= 0;
11634 extended
->shadow_depth
= 0;
11636 original_count
= *count
;
11637 if (flavor
== VM_REGION_EXTENDED_INFO__legacy
) {
11638 *count
= VM_REGION_EXTENDED_INFO_COUNT__legacy
;
11640 extended
->pages_reusable
= 0;
11641 *count
= VM_REGION_EXTENDED_INFO_COUNT
;
11644 vm_map_region_walk(map
, start
, entry
, VME_OFFSET(entry
), entry
->vme_end
- start
, extended
, TRUE
, *count
);
11646 if (extended
->external_pager
&& extended
->ref_count
== 2 && extended
->share_mode
== SM_SHARED
)
11647 extended
->share_mode
= SM_PRIVATE
;
11650 *object_name
= IP_NULL
;
11652 *size
= (entry
->vme_end
- start
);
11654 vm_map_unlock_read(map
);
11655 return(KERN_SUCCESS
);
11657 case VM_REGION_TOP_INFO
:
11659 vm_region_top_info_t top
;
11661 if (*count
< VM_REGION_TOP_INFO_COUNT
)
11662 return(KERN_INVALID_ARGUMENT
);
11664 top
= (vm_region_top_info_t
) info
;
11665 *count
= VM_REGION_TOP_INFO_COUNT
;
11667 vm_map_lock_read(map
);
11670 if (!vm_map_lookup_entry(map
, start
, &tmp_entry
)) {
11671 if ((entry
= tmp_entry
->vme_next
) == vm_map_to_entry(map
)) {
11672 vm_map_unlock_read(map
);
11673 return(KERN_INVALID_ADDRESS
);
11679 start
= entry
->vme_start
;
11681 top
->private_pages_resident
= 0;
11682 top
->shared_pages_resident
= 0;
11684 vm_map_region_top_walk(entry
, top
);
11687 *object_name
= IP_NULL
;
11689 *size
= (entry
->vme_end
- start
);
11691 vm_map_unlock_read(map
);
11692 return(KERN_SUCCESS
);
11695 return(KERN_INVALID_ARGUMENT
);
11699 #define OBJ_RESIDENT_COUNT(obj, entry_size) \
11700 MIN((entry_size), \
11701 ((obj)->all_reusable ? \
11702 (obj)->wired_page_count : \
11703 (obj)->resident_page_count - (obj)->reusable_page_count))
11706 vm_map_region_top_walk(
11707 vm_map_entry_t entry
,
11708 vm_region_top_info_t top
)
11711 if (VME_OBJECT(entry
) == 0 || entry
->is_sub_map
) {
11712 top
->share_mode
= SM_EMPTY
;
11713 top
->ref_count
= 0;
11719 struct vm_object
*obj
, *tmp_obj
;
11721 uint32_t entry_size
;
11723 entry_size
= (uint32_t) ((entry
->vme_end
- entry
->vme_start
) / PAGE_SIZE_64
);
11725 obj
= VME_OBJECT(entry
);
11727 vm_object_lock(obj
);
11729 if ((ref_count
= obj
->ref_count
) > 1 && obj
->paging_in_progress
)
11732 assert(obj
->reusable_page_count
<= obj
->resident_page_count
);
11734 if (ref_count
== 1)
11735 top
->private_pages_resident
=
11736 OBJ_RESIDENT_COUNT(obj
, entry_size
);
11738 top
->shared_pages_resident
=
11739 OBJ_RESIDENT_COUNT(obj
, entry_size
);
11740 top
->ref_count
= ref_count
;
11741 top
->share_mode
= SM_COW
;
11743 while ((tmp_obj
= obj
->shadow
)) {
11744 vm_object_lock(tmp_obj
);
11745 vm_object_unlock(obj
);
11748 if ((ref_count
= obj
->ref_count
) > 1 && obj
->paging_in_progress
)
11751 assert(obj
->reusable_page_count
<= obj
->resident_page_count
);
11752 top
->shared_pages_resident
+=
11753 OBJ_RESIDENT_COUNT(obj
, entry_size
);
11754 top
->ref_count
+= ref_count
- 1;
11757 if (entry
->superpage_size
) {
11758 top
->share_mode
= SM_LARGE_PAGE
;
11759 top
->shared_pages_resident
= 0;
11760 top
->private_pages_resident
= entry_size
;
11761 } else if (entry
->needs_copy
) {
11762 top
->share_mode
= SM_COW
;
11763 top
->shared_pages_resident
=
11764 OBJ_RESIDENT_COUNT(obj
, entry_size
);
11766 if (ref_count
== 1 ||
11767 (ref_count
== 2 && !(obj
->pager_trusted
) && !(obj
->internal
))) {
11768 top
->share_mode
= SM_PRIVATE
;
11769 top
->private_pages_resident
=
11770 OBJ_RESIDENT_COUNT(obj
,
11773 top
->share_mode
= SM_SHARED
;
11774 top
->shared_pages_resident
=
11775 OBJ_RESIDENT_COUNT(obj
,
11779 top
->ref_count
= ref_count
;
11781 /* XXX K64: obj_id will be truncated */
11782 top
->obj_id
= (unsigned int) (uintptr_t)VM_KERNEL_ADDRPERM(obj
);
11784 vm_object_unlock(obj
);
11789 vm_map_region_walk(
11791 vm_map_offset_t va
,
11792 vm_map_entry_t entry
,
11793 vm_object_offset_t offset
,
11794 vm_object_size_t range
,
11795 vm_region_extended_info_t extended
,
11796 boolean_t look_for_pages
,
11797 mach_msg_type_number_t count
)
11799 register struct vm_object
*obj
, *tmp_obj
;
11800 register vm_map_offset_t last_offset
;
11802 register int ref_count
;
11803 struct vm_object
*shadow_object
;
11806 if ((VME_OBJECT(entry
) == 0) ||
11807 (entry
->is_sub_map
) ||
11808 (VME_OBJECT(entry
)->phys_contiguous
&&
11809 !entry
->superpage_size
)) {
11810 extended
->share_mode
= SM_EMPTY
;
11811 extended
->ref_count
= 0;
11815 if (entry
->superpage_size
) {
11816 extended
->shadow_depth
= 0;
11817 extended
->share_mode
= SM_LARGE_PAGE
;
11818 extended
->ref_count
= 1;
11819 extended
->external_pager
= 0;
11820 extended
->pages_resident
= (unsigned int)(range
>> PAGE_SHIFT
);
11821 extended
->shadow_depth
= 0;
11826 obj
= VME_OBJECT(entry
);
11828 vm_object_lock(obj
);
11830 if ((ref_count
= obj
->ref_count
) > 1 && obj
->paging_in_progress
)
11833 if (look_for_pages
) {
11834 for (last_offset
= offset
+ range
;
11835 offset
< last_offset
;
11836 offset
+= PAGE_SIZE_64
, va
+= PAGE_SIZE
) {
11837 vm_map_region_look_for_page(map
, va
, obj
,
11839 0, extended
, count
);
11842 shadow_object
= obj
->shadow
;
11845 if ( !(obj
->pager_trusted
) && !(obj
->internal
))
11846 extended
->external_pager
= 1;
11848 if (shadow_object
!= VM_OBJECT_NULL
) {
11849 vm_object_lock(shadow_object
);
11851 shadow_object
!= VM_OBJECT_NULL
;
11853 vm_object_t next_shadow
;
11855 if ( !(shadow_object
->pager_trusted
) &&
11856 !(shadow_object
->internal
))
11857 extended
->external_pager
= 1;
11859 next_shadow
= shadow_object
->shadow
;
11861 vm_object_lock(next_shadow
);
11863 vm_object_unlock(shadow_object
);
11864 shadow_object
= next_shadow
;
11867 extended
->shadow_depth
= shadow_depth
;
11870 if (extended
->shadow_depth
|| entry
->needs_copy
)
11871 extended
->share_mode
= SM_COW
;
11873 if (ref_count
== 1)
11874 extended
->share_mode
= SM_PRIVATE
;
11876 if (obj
->true_share
)
11877 extended
->share_mode
= SM_TRUESHARED
;
11879 extended
->share_mode
= SM_SHARED
;
11882 extended
->ref_count
= ref_count
- extended
->shadow_depth
;
11884 for (i
= 0; i
< extended
->shadow_depth
; i
++) {
11885 if ((tmp_obj
= obj
->shadow
) == 0)
11887 vm_object_lock(tmp_obj
);
11888 vm_object_unlock(obj
);
11890 if ((ref_count
= tmp_obj
->ref_count
) > 1 && tmp_obj
->paging_in_progress
)
11893 extended
->ref_count
+= ref_count
;
11896 vm_object_unlock(obj
);
11898 if (extended
->share_mode
== SM_SHARED
) {
11899 register vm_map_entry_t cur
;
11900 register vm_map_entry_t last
;
11903 obj
= VME_OBJECT(entry
);
11904 last
= vm_map_to_entry(map
);
11907 if ((ref_count
= obj
->ref_count
) > 1 && obj
->paging_in_progress
)
11909 for (cur
= vm_map_first_entry(map
); cur
!= last
; cur
= cur
->vme_next
)
11910 my_refs
+= vm_map_region_count_obj_refs(cur
, obj
);
11912 if (my_refs
== ref_count
)
11913 extended
->share_mode
= SM_PRIVATE_ALIASED
;
11914 else if (my_refs
> 1)
11915 extended
->share_mode
= SM_SHARED_ALIASED
;
11921 /* object is locked on entry and locked on return */
11925 vm_map_region_look_for_page(
11926 __unused vm_map_t map
,
11927 __unused vm_map_offset_t va
,
11928 vm_object_t object
,
11929 vm_object_offset_t offset
,
11932 vm_region_extended_info_t extended
,
11933 mach_msg_type_number_t count
)
11935 register vm_page_t p
;
11936 register vm_object_t shadow
;
11937 register int ref_count
;
11938 vm_object_t caller_object
;
11940 shadow
= object
->shadow
;
11941 caller_object
= object
;
11946 if ( !(object
->pager_trusted
) && !(object
->internal
))
11947 extended
->external_pager
= 1;
11949 if ((p
= vm_page_lookup(object
, offset
)) != VM_PAGE_NULL
) {
11950 if (shadow
&& (max_refcnt
== 1))
11951 extended
->pages_shared_now_private
++;
11953 if (!p
->fictitious
&&
11954 (p
->dirty
|| pmap_is_modified(p
->phys_page
)))
11955 extended
->pages_dirtied
++;
11956 else if (count
>= VM_REGION_EXTENDED_INFO_COUNT
) {
11957 if (p
->reusable
|| p
->object
->all_reusable
) {
11958 extended
->pages_reusable
++;
11962 extended
->pages_resident
++;
11964 if(object
!= caller_object
)
11965 vm_object_unlock(object
);
11970 if (object
->existence_map
) {
11971 if (vm_external_state_get(object
->existence_map
, offset
) == VM_EXTERNAL_STATE_EXISTS
) {
11973 extended
->pages_swapped_out
++;
11975 if(object
!= caller_object
)
11976 vm_object_unlock(object
);
11981 #endif /* MACH_PAGEMAP */
11982 if (object
->internal
&&
11984 !object
->terminating
&&
11985 object
->pager_ready
) {
11987 if (COMPRESSED_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE
) {
11988 if (VM_COMPRESSOR_PAGER_STATE_GET(object
,
11990 == VM_EXTERNAL_STATE_EXISTS
) {
11991 /* the pager has that page */
11992 extended
->pages_swapped_out
++;
11993 if (object
!= caller_object
)
11994 vm_object_unlock(object
);
11998 memory_object_t pager
;
12000 vm_object_paging_begin(object
);
12001 pager
= object
->pager
;
12002 vm_object_unlock(object
);
12004 kr
= memory_object_data_request(
12006 offset
+ object
->paging_offset
,
12007 0, /* just poke the pager */
12011 vm_object_lock(object
);
12012 vm_object_paging_end(object
);
12014 if (kr
== KERN_SUCCESS
) {
12015 /* the pager has that page */
12016 extended
->pages_swapped_out
++;
12017 if (object
!= caller_object
)
12018 vm_object_unlock(object
);
12025 vm_object_lock(shadow
);
12027 if ((ref_count
= shadow
->ref_count
) > 1 && shadow
->paging_in_progress
)
12030 if (++depth
> extended
->shadow_depth
)
12031 extended
->shadow_depth
= depth
;
12033 if (ref_count
> max_refcnt
)
12034 max_refcnt
= ref_count
;
12036 if(object
!= caller_object
)
12037 vm_object_unlock(object
);
12039 offset
= offset
+ object
->vo_shadow_offset
;
12041 shadow
= object
->shadow
;
12044 if(object
!= caller_object
)
12045 vm_object_unlock(object
);
12051 vm_map_region_count_obj_refs(
12052 vm_map_entry_t entry
,
12053 vm_object_t object
)
12055 register int ref_count
;
12056 register vm_object_t chk_obj
;
12057 register vm_object_t tmp_obj
;
12059 if (VME_OBJECT(entry
) == 0)
12062 if (entry
->is_sub_map
)
12067 chk_obj
= VME_OBJECT(entry
);
12068 vm_object_lock(chk_obj
);
12071 if (chk_obj
== object
)
12073 tmp_obj
= chk_obj
->shadow
;
12075 vm_object_lock(tmp_obj
);
12076 vm_object_unlock(chk_obj
);
12086 * Routine: vm_map_simplify
12089 * Attempt to simplify the map representation in
12090 * the vicinity of the given starting address.
12092 * This routine is intended primarily to keep the
12093 * kernel maps more compact -- they generally don't
12094 * benefit from the "expand a map entry" technology
12095 * at allocation time because the adjacent entry
12096 * is often wired down.
12099 vm_map_simplify_entry(
12101 vm_map_entry_t this_entry
)
12103 vm_map_entry_t prev_entry
;
12105 counter(c_vm_map_simplify_entry_called
++);
12107 prev_entry
= this_entry
->vme_prev
;
12109 if ((this_entry
!= vm_map_to_entry(map
)) &&
12110 (prev_entry
!= vm_map_to_entry(map
)) &&
12112 (prev_entry
->vme_end
== this_entry
->vme_start
) &&
12114 (prev_entry
->is_sub_map
== this_entry
->is_sub_map
) &&
12115 (VME_OBJECT(prev_entry
) == VME_OBJECT(this_entry
)) &&
12116 ((VME_OFFSET(prev_entry
) + (prev_entry
->vme_end
-
12117 prev_entry
->vme_start
))
12118 == VME_OFFSET(this_entry
)) &&
12120 (prev_entry
->behavior
== this_entry
->behavior
) &&
12121 (prev_entry
->needs_copy
== this_entry
->needs_copy
) &&
12122 (prev_entry
->protection
== this_entry
->protection
) &&
12123 (prev_entry
->max_protection
== this_entry
->max_protection
) &&
12124 (prev_entry
->inheritance
== this_entry
->inheritance
) &&
12125 (prev_entry
->use_pmap
== this_entry
->use_pmap
) &&
12126 (VME_ALIAS(prev_entry
) == VME_ALIAS(this_entry
)) &&
12127 (prev_entry
->no_cache
== this_entry
->no_cache
) &&
12128 (prev_entry
->permanent
== this_entry
->permanent
) &&
12129 (prev_entry
->map_aligned
== this_entry
->map_aligned
) &&
12130 (prev_entry
->zero_wired_pages
== this_entry
->zero_wired_pages
) &&
12131 (prev_entry
->used_for_jit
== this_entry
->used_for_jit
) &&
12132 /* from_reserved_zone: OK if that field doesn't match */
12133 (prev_entry
->iokit_acct
== this_entry
->iokit_acct
) &&
12134 (prev_entry
->vme_resilient_codesign
==
12135 this_entry
->vme_resilient_codesign
) &&
12136 (prev_entry
->vme_resilient_media
==
12137 this_entry
->vme_resilient_media
) &&
12139 (prev_entry
->wired_count
== this_entry
->wired_count
) &&
12140 (prev_entry
->user_wired_count
== this_entry
->user_wired_count
) &&
12142 (prev_entry
->in_transition
== FALSE
) &&
12143 (this_entry
->in_transition
== FALSE
) &&
12144 (prev_entry
->needs_wakeup
== FALSE
) &&
12145 (this_entry
->needs_wakeup
== FALSE
) &&
12146 (prev_entry
->is_shared
== FALSE
) &&
12147 (this_entry
->is_shared
== FALSE
) &&
12148 (prev_entry
->superpage_size
== FALSE
) &&
12149 (this_entry
->superpage_size
== FALSE
)
12151 vm_map_store_entry_unlink(map
, prev_entry
);
12152 assert(prev_entry
->vme_start
< this_entry
->vme_end
);
12153 if (prev_entry
->map_aligned
)
12154 assert(VM_MAP_PAGE_ALIGNED(prev_entry
->vme_start
,
12155 VM_MAP_PAGE_MASK(map
)));
12156 this_entry
->vme_start
= prev_entry
->vme_start
;
12157 VME_OFFSET_SET(this_entry
, VME_OFFSET(prev_entry
));
12159 if (map
->holelistenabled
) {
12160 vm_map_store_update_first_free(map
, this_entry
, TRUE
);
12163 if (prev_entry
->is_sub_map
) {
12164 vm_map_deallocate(VME_SUBMAP(prev_entry
));
12166 vm_object_deallocate(VME_OBJECT(prev_entry
));
12168 vm_map_entry_dispose(map
, prev_entry
);
12169 SAVE_HINT_MAP_WRITE(map
, this_entry
);
12170 counter(c_vm_map_simplified
++);
12177 vm_map_offset_t start
)
12179 vm_map_entry_t this_entry
;
12182 if (vm_map_lookup_entry(map
, start
, &this_entry
)) {
12183 vm_map_simplify_entry(map
, this_entry
);
12184 vm_map_simplify_entry(map
, this_entry
->vme_next
);
12186 counter(c_vm_map_simplify_called
++);
12187 vm_map_unlock(map
);
12191 vm_map_simplify_range(
12193 vm_map_offset_t start
,
12194 vm_map_offset_t end
)
12196 vm_map_entry_t entry
;
12199 * The map should be locked (for "write") by the caller.
12202 if (start
>= end
) {
12203 /* invalid address range */
12207 start
= vm_map_trunc_page(start
,
12208 VM_MAP_PAGE_MASK(map
));
12209 end
= vm_map_round_page(end
,
12210 VM_MAP_PAGE_MASK(map
));
12212 if (!vm_map_lookup_entry(map
, start
, &entry
)) {
12213 /* "start" is not mapped and "entry" ends before "start" */
12214 if (entry
== vm_map_to_entry(map
)) {
12215 /* start with first entry in the map */
12216 entry
= vm_map_first_entry(map
);
12218 /* start with next entry */
12219 entry
= entry
->vme_next
;
12223 while (entry
!= vm_map_to_entry(map
) &&
12224 entry
->vme_start
<= end
) {
12225 /* try and coalesce "entry" with its previous entry */
12226 vm_map_simplify_entry(map
, entry
);
12227 entry
= entry
->vme_next
;
12233 * Routine: vm_map_machine_attribute
12235 * Provide machine-specific attributes to mappings,
12236 * such as cachability etc. for machines that provide
12237 * them. NUMA architectures and machines with big/strange
12238 * caches will use this.
12240 * Responsibilities for locking and checking are handled here,
12241 * everything else in the pmap module. If any non-volatile
12242 * information must be kept, the pmap module should handle
12243 * it itself. [This assumes that attributes do not
12244 * need to be inherited, which seems ok to me]
12247 vm_map_machine_attribute(
12249 vm_map_offset_t start
,
12250 vm_map_offset_t end
,
12251 vm_machine_attribute_t attribute
,
12252 vm_machine_attribute_val_t
* value
) /* IN/OUT */
12255 vm_map_size_t sync_size
;
12256 vm_map_entry_t entry
;
12258 if (start
< vm_map_min(map
) || end
> vm_map_max(map
))
12259 return KERN_INVALID_ADDRESS
;
12261 /* Figure how much memory we need to flush (in page increments) */
12262 sync_size
= end
- start
;
12266 if (attribute
!= MATTR_CACHE
) {
12267 /* If we don't have to find physical addresses, we */
12268 /* don't have to do an explicit traversal here. */
12269 ret
= pmap_attribute(map
->pmap
, start
, end
-start
,
12271 vm_map_unlock(map
);
12275 ret
= KERN_SUCCESS
; /* Assume it all worked */
12278 if (vm_map_lookup_entry(map
, start
, &entry
)) {
12279 vm_map_size_t sub_size
;
12280 if((entry
->vme_end
- start
) > sync_size
) {
12281 sub_size
= sync_size
;
12284 sub_size
= entry
->vme_end
- start
;
12285 sync_size
-= sub_size
;
12287 if(entry
->is_sub_map
) {
12288 vm_map_offset_t sub_start
;
12289 vm_map_offset_t sub_end
;
12291 sub_start
= (start
- entry
->vme_start
)
12292 + VME_OFFSET(entry
);
12293 sub_end
= sub_start
+ sub_size
;
12294 vm_map_machine_attribute(
12300 if (VME_OBJECT(entry
)) {
12302 vm_object_t object
;
12303 vm_object_t base_object
;
12304 vm_object_t last_object
;
12305 vm_object_offset_t offset
;
12306 vm_object_offset_t base_offset
;
12307 vm_map_size_t range
;
12309 offset
= (start
- entry
->vme_start
)
12310 + VME_OFFSET(entry
);
12311 base_offset
= offset
;
12312 object
= VME_OBJECT(entry
);
12313 base_object
= object
;
12314 last_object
= NULL
;
12316 vm_object_lock(object
);
12319 m
= vm_page_lookup(
12322 if (m
&& !m
->fictitious
) {
12324 pmap_attribute_cache_sync(
12329 } else if (object
->shadow
) {
12330 offset
= offset
+ object
->vo_shadow_offset
;
12331 last_object
= object
;
12332 object
= object
->shadow
;
12333 vm_object_lock(last_object
->shadow
);
12334 vm_object_unlock(last_object
);
12337 range
-= PAGE_SIZE
;
12339 if (base_object
!= object
) {
12340 vm_object_unlock(object
);
12341 vm_object_lock(base_object
);
12342 object
= base_object
;
12344 /* Bump to the next page */
12345 base_offset
+= PAGE_SIZE
;
12346 offset
= base_offset
;
12348 vm_object_unlock(object
);
12353 vm_map_unlock(map
);
12354 return KERN_FAILURE
;
12359 vm_map_unlock(map
);
12365 * vm_map_behavior_set:
12367 * Sets the paging reference behavior of the specified address
12368 * range in the target map. Paging reference behavior affects
12369 * how pagein operations resulting from faults on the map will be
12373 vm_map_behavior_set(
12375 vm_map_offset_t start
,
12376 vm_map_offset_t end
,
12377 vm_behavior_t new_behavior
)
12379 register vm_map_entry_t entry
;
12380 vm_map_entry_t temp_entry
;
12383 "vm_map_behavior_set, 0x%X start 0x%X end 0x%X behavior %d",
12384 map
, start
, end
, new_behavior
, 0);
12387 start
< vm_map_min(map
) ||
12388 end
> vm_map_max(map
)) {
12389 return KERN_NO_SPACE
;
12392 switch (new_behavior
) {
12395 * This first block of behaviors all set a persistent state on the specified
12396 * memory range. All we have to do here is to record the desired behavior
12397 * in the vm_map_entry_t's.
12400 case VM_BEHAVIOR_DEFAULT
:
12401 case VM_BEHAVIOR_RANDOM
:
12402 case VM_BEHAVIOR_SEQUENTIAL
:
12403 case VM_BEHAVIOR_RSEQNTL
:
12404 case VM_BEHAVIOR_ZERO_WIRED_PAGES
:
12408 * The entire address range must be valid for the map.
12409 * Note that vm_map_range_check() does a
12410 * vm_map_lookup_entry() internally and returns the
12411 * entry containing the start of the address range if
12412 * the entire range is valid.
12414 if (vm_map_range_check(map
, start
, end
, &temp_entry
)) {
12415 entry
= temp_entry
;
12416 vm_map_clip_start(map
, entry
, start
);
12419 vm_map_unlock(map
);
12420 return(KERN_INVALID_ADDRESS
);
12423 while ((entry
!= vm_map_to_entry(map
)) && (entry
->vme_start
< end
)) {
12424 vm_map_clip_end(map
, entry
, end
);
12425 if (entry
->is_sub_map
) {
12426 assert(!entry
->use_pmap
);
12429 if( new_behavior
== VM_BEHAVIOR_ZERO_WIRED_PAGES
) {
12430 entry
->zero_wired_pages
= TRUE
;
12432 entry
->behavior
= new_behavior
;
12434 entry
= entry
->vme_next
;
12437 vm_map_unlock(map
);
12441 * The rest of these are different from the above in that they cause
12442 * an immediate action to take place as opposed to setting a behavior that
12443 * affects future actions.
12446 case VM_BEHAVIOR_WILLNEED
:
12447 return vm_map_willneed(map
, start
, end
);
12449 case VM_BEHAVIOR_DONTNEED
:
12450 return vm_map_msync(map
, start
, end
- start
, VM_SYNC_DEACTIVATE
| VM_SYNC_CONTIGUOUS
);
12452 case VM_BEHAVIOR_FREE
:
12453 return vm_map_msync(map
, start
, end
- start
, VM_SYNC_KILLPAGES
| VM_SYNC_CONTIGUOUS
);
12455 case VM_BEHAVIOR_REUSABLE
:
12456 return vm_map_reusable_pages(map
, start
, end
);
12458 case VM_BEHAVIOR_REUSE
:
12459 return vm_map_reuse_pages(map
, start
, end
);
12461 case VM_BEHAVIOR_CAN_REUSE
:
12462 return vm_map_can_reuse(map
, start
, end
);
12465 case VM_BEHAVIOR_PAGEOUT
:
12466 return vm_map_pageout(map
, start
, end
);
12467 #endif /* MACH_ASSERT */
12470 return(KERN_INVALID_ARGUMENT
);
12473 return(KERN_SUCCESS
);
12478 * Internals for madvise(MADV_WILLNEED) system call.
12480 * The present implementation is to do a read-ahead if the mapping corresponds
12481 * to a mapped regular file. If it's an anonymous mapping, then we do nothing
12482 * and basically ignore the "advice" (which we are always free to do).
12486 static kern_return_t
12489 vm_map_offset_t start
,
12490 vm_map_offset_t end
12493 vm_map_entry_t entry
;
12494 vm_object_t object
;
12495 memory_object_t pager
;
12496 struct vm_object_fault_info fault_info
;
12498 vm_object_size_t len
;
12499 vm_object_offset_t offset
;
12502 * Fill in static values in fault_info. Several fields get ignored by the code
12503 * we call, but we'll fill them in anyway since uninitialized fields are bad
12504 * when it comes to future backwards compatibility.
12507 fault_info
.interruptible
= THREAD_UNINT
; /* ignored value */
12508 fault_info
.behavior
= VM_BEHAVIOR_SEQUENTIAL
;
12509 fault_info
.no_cache
= FALSE
; /* ignored value */
12510 fault_info
.stealth
= TRUE
;
12511 fault_info
.io_sync
= FALSE
;
12512 fault_info
.cs_bypass
= FALSE
;
12513 fault_info
.mark_zf_absent
= FALSE
;
12514 fault_info
.batch_pmap_op
= FALSE
;
12517 * The MADV_WILLNEED operation doesn't require any changes to the
12518 * vm_map_entry_t's, so the read lock is sufficient.
12521 vm_map_lock_read(map
);
12524 * The madvise semantics require that the address range be fully
12525 * allocated with no holes. Otherwise, we're required to return
12529 if (! vm_map_range_check(map
, start
, end
, &entry
)) {
12530 vm_map_unlock_read(map
);
12531 return KERN_INVALID_ADDRESS
;
12535 * Examine each vm_map_entry_t in the range.
12537 for (; entry
!= vm_map_to_entry(map
) && start
< end
; ) {
12540 * The first time through, the start address could be anywhere
12541 * within the vm_map_entry we found. So adjust the offset to
12542 * correspond. After that, the offset will always be zero to
12543 * correspond to the beginning of the current vm_map_entry.
12545 offset
= (start
- entry
->vme_start
) + VME_OFFSET(entry
);
12548 * Set the length so we don't go beyond the end of the
12549 * map_entry or beyond the end of the range we were given.
12550 * This range could span also multiple map entries all of which
12551 * map different files, so make sure we only do the right amount
12552 * of I/O for each object. Note that it's possible for there
12553 * to be multiple map entries all referring to the same object
12554 * but with different page permissions, but it's not worth
12555 * trying to optimize that case.
12557 len
= MIN(entry
->vme_end
- start
, end
- start
);
12559 if ((vm_size_t
) len
!= len
) {
12560 /* 32-bit overflow */
12561 len
= (vm_size_t
) (0 - PAGE_SIZE
);
12563 fault_info
.cluster_size
= (vm_size_t
) len
;
12564 fault_info
.lo_offset
= offset
;
12565 fault_info
.hi_offset
= offset
+ len
;
12566 fault_info
.user_tag
= VME_ALIAS(entry
);
12567 fault_info
.pmap_options
= 0;
12568 if (entry
->iokit_acct
||
12569 (!entry
->is_sub_map
&& !entry
->use_pmap
)) {
12570 fault_info
.pmap_options
|= PMAP_OPTIONS_ALT_ACCT
;
12574 * If there's no read permission to this mapping, then just
12577 if ((entry
->protection
& VM_PROT_READ
) == 0) {
12578 entry
= entry
->vme_next
;
12579 start
= entry
->vme_start
;
12584 * Find the file object backing this map entry. If there is
12585 * none, then we simply ignore the "will need" advice for this
12586 * entry and go on to the next one.
12588 if ((object
= find_vnode_object(entry
)) == VM_OBJECT_NULL
) {
12589 entry
= entry
->vme_next
;
12590 start
= entry
->vme_start
;
12595 * The data_request() could take a long time, so let's
12596 * release the map lock to avoid blocking other threads.
12598 vm_map_unlock_read(map
);
12600 vm_object_paging_begin(object
);
12601 pager
= object
->pager
;
12602 vm_object_unlock(object
);
12605 * Get the data from the object asynchronously.
12607 * Note that memory_object_data_request() places limits on the
12608 * amount of I/O it will do. Regardless of the len we
12609 * specified, it won't do more than MAX_UPL_TRANSFER_BYTES and it
12610 * silently truncates the len to that size. This isn't
12611 * necessarily bad since madvise shouldn't really be used to
12612 * page in unlimited amounts of data. Other Unix variants
12613 * limit the willneed case as well. If this turns out to be an
12614 * issue for developers, then we can always adjust the policy
12615 * here and still be backwards compatible since this is all
12618 kr
= memory_object_data_request(
12620 offset
+ object
->paging_offset
,
12623 (memory_object_fault_info_t
)&fault_info
);
12625 vm_object_lock(object
);
12626 vm_object_paging_end(object
);
12627 vm_object_unlock(object
);
12630 * If we couldn't do the I/O for some reason, just give up on
12631 * the madvise. We still return success to the user since
12632 * madvise isn't supposed to fail when the advice can't be
12635 if (kr
!= KERN_SUCCESS
) {
12636 return KERN_SUCCESS
;
12640 if (start
>= end
) {
12642 return KERN_SUCCESS
;
12645 /* look up next entry */
12646 vm_map_lock_read(map
);
12647 if (! vm_map_lookup_entry(map
, start
, &entry
)) {
12649 * There's a new hole in the address range.
12651 vm_map_unlock_read(map
);
12652 return KERN_INVALID_ADDRESS
;
12656 vm_map_unlock_read(map
);
12657 return KERN_SUCCESS
;
12661 vm_map_entry_is_reusable(
12662 vm_map_entry_t entry
)
12664 /* Only user map entries */
12666 vm_object_t object
;
12668 switch (VME_ALIAS(entry
)) {
12669 case VM_MEMORY_MALLOC
:
12670 case VM_MEMORY_MALLOC_SMALL
:
12671 case VM_MEMORY_MALLOC_LARGE
:
12672 case VM_MEMORY_REALLOC
:
12673 case VM_MEMORY_MALLOC_TINY
:
12674 case VM_MEMORY_MALLOC_LARGE_REUSABLE
:
12675 case VM_MEMORY_MALLOC_LARGE_REUSED
:
12677 * This is a malloc() memory region: check if it's still
12678 * in its original state and can be re-used for more
12679 * malloc() allocations.
12684 * Not a malloc() memory region: let the caller decide if
12690 if (entry
->is_shared
||
12691 entry
->is_sub_map
||
12692 entry
->in_transition
||
12693 entry
->protection
!= VM_PROT_DEFAULT
||
12694 entry
->max_protection
!= VM_PROT_ALL
||
12695 entry
->inheritance
!= VM_INHERIT_DEFAULT
||
12697 entry
->permanent
||
12698 entry
->superpage_size
!= FALSE
||
12699 entry
->zero_wired_pages
||
12700 entry
->wired_count
!= 0 ||
12701 entry
->user_wired_count
!= 0) {
12705 object
= VME_OBJECT(entry
);
12706 if (object
== VM_OBJECT_NULL
) {
12712 * Let's proceed even if the VM object is potentially
12714 * We check for this later when processing the actual
12715 * VM pages, so the contents will be safe if shared.
12717 * But we can still mark this memory region as "reusable" to
12718 * acknowledge that the caller did let us know that the memory
12719 * could be re-used and should not be penalized for holding
12720 * on to it. This allows its "resident size" to not include
12721 * the reusable range.
12723 object
->ref_count
== 1 &&
12725 object
->wired_page_count
== 0 &&
12726 object
->copy
== VM_OBJECT_NULL
&&
12727 object
->shadow
== VM_OBJECT_NULL
&&
12728 object
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
&&
12729 object
->internal
&&
12730 !object
->true_share
&&
12731 object
->wimg_bits
== VM_WIMG_USE_DEFAULT
&&
12732 !object
->code_signed
) {
12740 static kern_return_t
12741 vm_map_reuse_pages(
12743 vm_map_offset_t start
,
12744 vm_map_offset_t end
)
12746 vm_map_entry_t entry
;
12747 vm_object_t object
;
12748 vm_object_offset_t start_offset
, end_offset
;
12751 * The MADV_REUSE operation doesn't require any changes to the
12752 * vm_map_entry_t's, so the read lock is sufficient.
12755 vm_map_lock_read(map
);
12756 assert(map
->pmap
!= kernel_pmap
); /* protect alias access */
12759 * The madvise semantics require that the address range be fully
12760 * allocated with no holes. Otherwise, we're required to return
12764 if (!vm_map_range_check(map
, start
, end
, &entry
)) {
12765 vm_map_unlock_read(map
);
12766 vm_page_stats_reusable
.reuse_pages_failure
++;
12767 return KERN_INVALID_ADDRESS
;
12771 * Examine each vm_map_entry_t in the range.
12773 for (; entry
!= vm_map_to_entry(map
) && entry
->vme_start
< end
;
12774 entry
= entry
->vme_next
) {
12776 * Sanity check on the VM map entry.
12778 if (! vm_map_entry_is_reusable(entry
)) {
12779 vm_map_unlock_read(map
);
12780 vm_page_stats_reusable
.reuse_pages_failure
++;
12781 return KERN_INVALID_ADDRESS
;
12785 * The first time through, the start address could be anywhere
12786 * within the vm_map_entry we found. So adjust the offset to
12789 if (entry
->vme_start
< start
) {
12790 start_offset
= start
- entry
->vme_start
;
12794 end_offset
= MIN(end
, entry
->vme_end
) - entry
->vme_start
;
12795 start_offset
+= VME_OFFSET(entry
);
12796 end_offset
+= VME_OFFSET(entry
);
12798 object
= VME_OBJECT(entry
);
12799 if (object
!= VM_OBJECT_NULL
) {
12800 vm_object_lock(object
);
12801 vm_object_reuse_pages(object
, start_offset
, end_offset
,
12803 vm_object_unlock(object
);
12806 if (VME_ALIAS(entry
) == VM_MEMORY_MALLOC_LARGE_REUSABLE
) {
12809 * We do not hold the VM map exclusively here.
12810 * The "alias" field is not that critical, so it's
12811 * safe to update it here, as long as it is the only
12812 * one that can be modified while holding the VM map
12815 VME_ALIAS_SET(entry
, VM_MEMORY_MALLOC_LARGE_REUSED
);
12819 vm_map_unlock_read(map
);
12820 vm_page_stats_reusable
.reuse_pages_success
++;
12821 return KERN_SUCCESS
;
12825 static kern_return_t
12826 vm_map_reusable_pages(
12828 vm_map_offset_t start
,
12829 vm_map_offset_t end
)
12831 vm_map_entry_t entry
;
12832 vm_object_t object
;
12833 vm_object_offset_t start_offset
, end_offset
;
12834 vm_map_offset_t pmap_offset
;
12837 * The MADV_REUSABLE operation doesn't require any changes to the
12838 * vm_map_entry_t's, so the read lock is sufficient.
12841 vm_map_lock_read(map
);
12842 assert(map
->pmap
!= kernel_pmap
); /* protect alias access */
12845 * The madvise semantics require that the address range be fully
12846 * allocated with no holes. Otherwise, we're required to return
12850 if (!vm_map_range_check(map
, start
, end
, &entry
)) {
12851 vm_map_unlock_read(map
);
12852 vm_page_stats_reusable
.reusable_pages_failure
++;
12853 return KERN_INVALID_ADDRESS
;
12857 * Examine each vm_map_entry_t in the range.
12859 for (; entry
!= vm_map_to_entry(map
) && entry
->vme_start
< end
;
12860 entry
= entry
->vme_next
) {
12861 int kill_pages
= 0;
12864 * Sanity check on the VM map entry.
12866 if (! vm_map_entry_is_reusable(entry
)) {
12867 vm_map_unlock_read(map
);
12868 vm_page_stats_reusable
.reusable_pages_failure
++;
12869 return KERN_INVALID_ADDRESS
;
12873 * The first time through, the start address could be anywhere
12874 * within the vm_map_entry we found. So adjust the offset to
12877 if (entry
->vme_start
< start
) {
12878 start_offset
= start
- entry
->vme_start
;
12879 pmap_offset
= start
;
12882 pmap_offset
= entry
->vme_start
;
12884 end_offset
= MIN(end
, entry
->vme_end
) - entry
->vme_start
;
12885 start_offset
+= VME_OFFSET(entry
);
12886 end_offset
+= VME_OFFSET(entry
);
12888 object
= VME_OBJECT(entry
);
12889 if (object
== VM_OBJECT_NULL
)
12893 vm_object_lock(object
);
12894 if (object
->ref_count
== 1 &&
12897 * "iokit_acct" entries are billed for their virtual size
12898 * (rather than for their resident pages only), so they
12899 * wouldn't benefit from making pages reusable, and it
12900 * would be hard to keep track of pages that are both
12901 * "iokit_acct" and "reusable" in the pmap stats and ledgers.
12903 !(entry
->iokit_acct
||
12904 (!entry
->is_sub_map
&& !entry
->use_pmap
)))
12908 if (kill_pages
!= -1) {
12909 vm_object_deactivate_pages(object
,
12911 end_offset
- start_offset
,
12913 TRUE
/*reusable_pages*/,
12917 vm_page_stats_reusable
.reusable_pages_shared
++;
12919 vm_object_unlock(object
);
12921 if (VME_ALIAS(entry
) == VM_MEMORY_MALLOC_LARGE
||
12922 VME_ALIAS(entry
) == VM_MEMORY_MALLOC_LARGE_REUSED
) {
12925 * We do not hold the VM map exclusively here.
12926 * The "alias" field is not that critical, so it's
12927 * safe to update it here, as long as it is the only
12928 * one that can be modified while holding the VM map
12931 VME_ALIAS_SET(entry
, VM_MEMORY_MALLOC_LARGE_REUSABLE
);
12935 vm_map_unlock_read(map
);
12936 vm_page_stats_reusable
.reusable_pages_success
++;
12937 return KERN_SUCCESS
;
12941 static kern_return_t
12944 vm_map_offset_t start
,
12945 vm_map_offset_t end
)
12947 vm_map_entry_t entry
;
12950 * The MADV_REUSABLE operation doesn't require any changes to the
12951 * vm_map_entry_t's, so the read lock is sufficient.
12954 vm_map_lock_read(map
);
12955 assert(map
->pmap
!= kernel_pmap
); /* protect alias access */
12958 * The madvise semantics require that the address range be fully
12959 * allocated with no holes. Otherwise, we're required to return
12963 if (!vm_map_range_check(map
, start
, end
, &entry
)) {
12964 vm_map_unlock_read(map
);
12965 vm_page_stats_reusable
.can_reuse_failure
++;
12966 return KERN_INVALID_ADDRESS
;
12970 * Examine each vm_map_entry_t in the range.
12972 for (; entry
!= vm_map_to_entry(map
) && entry
->vme_start
< end
;
12973 entry
= entry
->vme_next
) {
12975 * Sanity check on the VM map entry.
12977 if (! vm_map_entry_is_reusable(entry
)) {
12978 vm_map_unlock_read(map
);
12979 vm_page_stats_reusable
.can_reuse_failure
++;
12980 return KERN_INVALID_ADDRESS
;
12984 vm_map_unlock_read(map
);
12985 vm_page_stats_reusable
.can_reuse_success
++;
12986 return KERN_SUCCESS
;
12991 static kern_return_t
12994 vm_map_offset_t start
,
12995 vm_map_offset_t end
)
12997 vm_map_entry_t entry
;
13000 * The MADV_PAGEOUT operation doesn't require any changes to the
13001 * vm_map_entry_t's, so the read lock is sufficient.
13004 vm_map_lock_read(map
);
13007 * The madvise semantics require that the address range be fully
13008 * allocated with no holes. Otherwise, we're required to return
13012 if (!vm_map_range_check(map
, start
, end
, &entry
)) {
13013 vm_map_unlock_read(map
);
13014 return KERN_INVALID_ADDRESS
;
13018 * Examine each vm_map_entry_t in the range.
13020 for (; entry
!= vm_map_to_entry(map
) && entry
->vme_start
< end
;
13021 entry
= entry
->vme_next
) {
13022 vm_object_t object
;
13025 * Sanity check on the VM map entry.
13027 if (entry
->is_sub_map
) {
13029 vm_map_offset_t submap_start
;
13030 vm_map_offset_t submap_end
;
13031 vm_map_entry_t submap_entry
;
13033 submap
= VME_SUBMAP(entry
);
13034 submap_start
= VME_OFFSET(entry
);
13035 submap_end
= submap_start
+ (entry
->vme_end
-
13038 vm_map_lock_read(submap
);
13040 if (! vm_map_range_check(submap
,
13044 vm_map_unlock_read(submap
);
13045 vm_map_unlock_read(map
);
13046 return KERN_INVALID_ADDRESS
;
13049 object
= VME_OBJECT(submap_entry
);
13050 if (submap_entry
->is_sub_map
||
13051 object
== VM_OBJECT_NULL
||
13052 !object
->internal
) {
13053 vm_map_unlock_read(submap
);
13057 vm_object_pageout(object
);
13059 vm_map_unlock_read(submap
);
13060 submap
= VM_MAP_NULL
;
13061 submap_entry
= VM_MAP_ENTRY_NULL
;
13065 object
= VME_OBJECT(entry
);
13066 if (entry
->is_sub_map
||
13067 object
== VM_OBJECT_NULL
||
13068 !object
->internal
) {
13072 vm_object_pageout(object
);
13075 vm_map_unlock_read(map
);
13076 return KERN_SUCCESS
;
13078 #endif /* MACH_ASSERT */
13082 * Routine: vm_map_entry_insert
13084 * Descritpion: This routine inserts a new vm_entry in a locked map.
13087 vm_map_entry_insert(
13089 vm_map_entry_t insp_entry
,
13090 vm_map_offset_t start
,
13091 vm_map_offset_t end
,
13092 vm_object_t object
,
13093 vm_object_offset_t offset
,
13094 boolean_t needs_copy
,
13095 boolean_t is_shared
,
13096 boolean_t in_transition
,
13097 vm_prot_t cur_protection
,
13098 vm_prot_t max_protection
,
13099 vm_behavior_t behavior
,
13100 vm_inherit_t inheritance
,
13101 unsigned wired_count
,
13102 boolean_t no_cache
,
13103 boolean_t permanent
,
13104 unsigned int superpage_size
,
13105 boolean_t clear_map_aligned
,
13106 boolean_t is_submap
)
13108 vm_map_entry_t new_entry
;
13110 assert(insp_entry
!= (vm_map_entry_t
)0);
13112 new_entry
= vm_map_entry_create(map
, !map
->hdr
.entries_pageable
);
13114 if (VM_MAP_PAGE_SHIFT(map
) != PAGE_SHIFT
) {
13115 new_entry
->map_aligned
= TRUE
;
13117 new_entry
->map_aligned
= FALSE
;
13119 if (clear_map_aligned
&&
13120 (! VM_MAP_PAGE_ALIGNED(start
, VM_MAP_PAGE_MASK(map
)) ||
13121 ! VM_MAP_PAGE_ALIGNED(end
, VM_MAP_PAGE_MASK(map
)))) {
13122 new_entry
->map_aligned
= FALSE
;
13125 new_entry
->vme_start
= start
;
13126 new_entry
->vme_end
= end
;
13127 assert(page_aligned(new_entry
->vme_start
));
13128 assert(page_aligned(new_entry
->vme_end
));
13129 if (new_entry
->map_aligned
) {
13130 assert(VM_MAP_PAGE_ALIGNED(new_entry
->vme_start
,
13131 VM_MAP_PAGE_MASK(map
)));
13132 assert(VM_MAP_PAGE_ALIGNED(new_entry
->vme_end
,
13133 VM_MAP_PAGE_MASK(map
)));
13135 assert(new_entry
->vme_start
< new_entry
->vme_end
);
13137 VME_OBJECT_SET(new_entry
, object
);
13138 VME_OFFSET_SET(new_entry
, offset
);
13139 new_entry
->is_shared
= is_shared
;
13140 new_entry
->is_sub_map
= is_submap
;
13141 new_entry
->needs_copy
= needs_copy
;
13142 new_entry
->in_transition
= in_transition
;
13143 new_entry
->needs_wakeup
= FALSE
;
13144 new_entry
->inheritance
= inheritance
;
13145 new_entry
->protection
= cur_protection
;
13146 new_entry
->max_protection
= max_protection
;
13147 new_entry
->behavior
= behavior
;
13148 new_entry
->wired_count
= wired_count
;
13149 new_entry
->user_wired_count
= 0;
13152 * submap: "use_pmap" means "nested".
13155 new_entry
->use_pmap
= FALSE
;
13158 * object: "use_pmap" means "use pmap accounting" for footprint.
13161 new_entry
->use_pmap
= TRUE
;
13163 VME_ALIAS_SET(new_entry
, 0);
13164 new_entry
->zero_wired_pages
= FALSE
;
13165 new_entry
->no_cache
= no_cache
;
13166 new_entry
->permanent
= permanent
;
13167 if (superpage_size
)
13168 new_entry
->superpage_size
= TRUE
;
13170 new_entry
->superpage_size
= FALSE
;
13171 new_entry
->used_for_jit
= FALSE
;
13172 new_entry
->iokit_acct
= FALSE
;
13173 new_entry
->vme_resilient_codesign
= FALSE
;
13174 new_entry
->vme_resilient_media
= FALSE
;
13177 * Insert the new entry into the list.
13180 vm_map_store_entry_link(map
, insp_entry
, new_entry
);
13181 map
->size
+= end
- start
;
13184 * Update the free space hint and the lookup hint.
13187 SAVE_HINT_MAP_WRITE(map
, new_entry
);
13192 * Routine: vm_map_remap_extract
13194 * Descritpion: This routine returns a vm_entry list from a map.
13196 static kern_return_t
13197 vm_map_remap_extract(
13199 vm_map_offset_t addr
,
13200 vm_map_size_t size
,
13202 struct vm_map_header
*map_header
,
13203 vm_prot_t
*cur_protection
,
13204 vm_prot_t
*max_protection
,
13205 /* What, no behavior? */
13206 vm_inherit_t inheritance
,
13207 boolean_t pageable
)
13209 kern_return_t result
;
13210 vm_map_size_t mapped_size
;
13211 vm_map_size_t tmp_size
;
13212 vm_map_entry_t src_entry
; /* result of last map lookup */
13213 vm_map_entry_t new_entry
;
13214 vm_object_offset_t offset
;
13215 vm_map_offset_t map_address
;
13216 vm_map_offset_t src_start
; /* start of entry to map */
13217 vm_map_offset_t src_end
; /* end of region to be mapped */
13218 vm_object_t object
;
13219 vm_map_version_t version
;
13220 boolean_t src_needs_copy
;
13221 boolean_t new_entry_needs_copy
;
13223 assert(map
!= VM_MAP_NULL
);
13225 assert(size
== vm_map_round_page(size
, PAGE_MASK
));
13226 assert(inheritance
== VM_INHERIT_NONE
||
13227 inheritance
== VM_INHERIT_COPY
||
13228 inheritance
== VM_INHERIT_SHARE
);
13231 * Compute start and end of region.
13233 src_start
= vm_map_trunc_page(addr
, PAGE_MASK
);
13234 src_end
= vm_map_round_page(src_start
+ size
, PAGE_MASK
);
13238 * Initialize map_header.
13240 map_header
->links
.next
= (struct vm_map_entry
*)&map_header
->links
;
13241 map_header
->links
.prev
= (struct vm_map_entry
*)&map_header
->links
;
13242 map_header
->nentries
= 0;
13243 map_header
->entries_pageable
= pageable
;
13244 map_header
->page_shift
= PAGE_SHIFT
;
13246 vm_map_store_init( map_header
);
13248 *cur_protection
= VM_PROT_ALL
;
13249 *max_protection
= VM_PROT_ALL
;
13253 result
= KERN_SUCCESS
;
13256 * The specified source virtual space might correspond to
13257 * multiple map entries, need to loop on them.
13260 while (mapped_size
!= size
) {
13261 vm_map_size_t entry_size
;
13264 * Find the beginning of the region.
13266 if (! vm_map_lookup_entry(map
, src_start
, &src_entry
)) {
13267 result
= KERN_INVALID_ADDRESS
;
13271 if (src_start
< src_entry
->vme_start
||
13272 (mapped_size
&& src_start
!= src_entry
->vme_start
)) {
13273 result
= KERN_INVALID_ADDRESS
;
13277 tmp_size
= size
- mapped_size
;
13278 if (src_end
> src_entry
->vme_end
)
13279 tmp_size
-= (src_end
- src_entry
->vme_end
);
13281 entry_size
= (vm_map_size_t
)(src_entry
->vme_end
-
13282 src_entry
->vme_start
);
13284 if(src_entry
->is_sub_map
) {
13285 vm_map_reference(VME_SUBMAP(src_entry
));
13286 object
= VM_OBJECT_NULL
;
13288 object
= VME_OBJECT(src_entry
);
13289 if (src_entry
->iokit_acct
) {
13291 * This entry uses "IOKit accounting".
13293 } else if (object
!= VM_OBJECT_NULL
&&
13294 object
->purgable
!= VM_PURGABLE_DENY
) {
13296 * Purgeable objects have their own accounting:
13297 * no pmap accounting for them.
13299 assert(!src_entry
->use_pmap
);
13302 * Not IOKit or purgeable:
13303 * must be accounted by pmap stats.
13305 assert(src_entry
->use_pmap
);
13308 if (object
== VM_OBJECT_NULL
) {
13309 object
= vm_object_allocate(entry_size
);
13310 VME_OFFSET_SET(src_entry
, 0);
13311 VME_OBJECT_SET(src_entry
, object
);
13312 } else if (object
->copy_strategy
!=
13313 MEMORY_OBJECT_COPY_SYMMETRIC
) {
13315 * We are already using an asymmetric
13316 * copy, and therefore we already have
13317 * the right object.
13319 assert(!src_entry
->needs_copy
);
13320 } else if (src_entry
->needs_copy
|| object
->shadowed
||
13321 (object
->internal
&& !object
->true_share
&&
13322 !src_entry
->is_shared
&&
13323 object
->vo_size
> entry_size
)) {
13325 VME_OBJECT_SHADOW(src_entry
, entry_size
);
13327 if (!src_entry
->needs_copy
&&
13328 (src_entry
->protection
& VM_PROT_WRITE
)) {
13331 prot
= src_entry
->protection
& ~VM_PROT_WRITE
;
13333 if (override_nx(map
,
13334 VME_ALIAS(src_entry
))
13336 prot
|= VM_PROT_EXECUTE
;
13338 if(map
->mapped_in_other_pmaps
) {
13339 vm_object_pmap_protect(
13340 VME_OBJECT(src_entry
),
13341 VME_OFFSET(src_entry
),
13344 src_entry
->vme_start
,
13347 pmap_protect(vm_map_pmap(map
),
13348 src_entry
->vme_start
,
13349 src_entry
->vme_end
,
13354 object
= VME_OBJECT(src_entry
);
13355 src_entry
->needs_copy
= FALSE
;
13359 vm_object_lock(object
);
13360 vm_object_reference_locked(object
); /* object ref. for new entry */
13361 if (object
->copy_strategy
==
13362 MEMORY_OBJECT_COPY_SYMMETRIC
) {
13363 object
->copy_strategy
=
13364 MEMORY_OBJECT_COPY_DELAY
;
13366 vm_object_unlock(object
);
13369 offset
= (VME_OFFSET(src_entry
) +
13370 (src_start
- src_entry
->vme_start
));
13372 new_entry
= _vm_map_entry_create(map_header
, !map_header
->entries_pageable
);
13373 vm_map_entry_copy(new_entry
, src_entry
);
13374 if (new_entry
->is_sub_map
) {
13375 /* clr address space specifics */
13376 new_entry
->use_pmap
= FALSE
;
13379 new_entry
->map_aligned
= FALSE
;
13381 new_entry
->vme_start
= map_address
;
13382 new_entry
->vme_end
= map_address
+ tmp_size
;
13383 assert(new_entry
->vme_start
< new_entry
->vme_end
);
13384 new_entry
->inheritance
= inheritance
;
13385 VME_OFFSET_SET(new_entry
, offset
);
13388 * The new region has to be copied now if required.
13393 * Cannot allow an entry describing a JIT
13394 * region to be shared across address spaces.
13396 if (src_entry
->used_for_jit
== TRUE
) {
13397 result
= KERN_INVALID_ARGUMENT
;
13400 src_entry
->is_shared
= TRUE
;
13401 new_entry
->is_shared
= TRUE
;
13402 if (!(new_entry
->is_sub_map
))
13403 new_entry
->needs_copy
= FALSE
;
13405 } else if (src_entry
->is_sub_map
) {
13406 /* make this a COW sub_map if not already */
13407 assert(new_entry
->wired_count
== 0);
13408 new_entry
->needs_copy
= TRUE
;
13409 object
= VM_OBJECT_NULL
;
13410 } else if (src_entry
->wired_count
== 0 &&
13411 vm_object_copy_quickly(&VME_OBJECT(new_entry
),
13412 VME_OFFSET(new_entry
),
13413 (new_entry
->vme_end
-
13414 new_entry
->vme_start
),
13416 &new_entry_needs_copy
)) {
13418 new_entry
->needs_copy
= new_entry_needs_copy
;
13419 new_entry
->is_shared
= FALSE
;
13422 * Handle copy_on_write semantics.
13424 if (src_needs_copy
&& !src_entry
->needs_copy
) {
13427 prot
= src_entry
->protection
& ~VM_PROT_WRITE
;
13429 if (override_nx(map
,
13430 VME_ALIAS(src_entry
))
13432 prot
|= VM_PROT_EXECUTE
;
13434 vm_object_pmap_protect(object
,
13437 ((src_entry
->is_shared
13438 || map
->mapped_in_other_pmaps
) ?
13439 PMAP_NULL
: map
->pmap
),
13440 src_entry
->vme_start
,
13443 assert(src_entry
->wired_count
== 0);
13444 src_entry
->needs_copy
= TRUE
;
13447 * Throw away the old object reference of the new entry.
13449 vm_object_deallocate(object
);
13452 new_entry
->is_shared
= FALSE
;
13455 * The map can be safely unlocked since we
13456 * already hold a reference on the object.
13458 * Record the timestamp of the map for later
13459 * verification, and unlock the map.
13461 version
.main_timestamp
= map
->timestamp
;
13462 vm_map_unlock(map
); /* Increments timestamp once! */
13465 * Perform the copy.
13467 if (src_entry
->wired_count
> 0) {
13468 vm_object_lock(object
);
13469 result
= vm_object_copy_slowly(
13474 &VME_OBJECT(new_entry
));
13476 VME_OFFSET_SET(new_entry
, 0);
13477 new_entry
->needs_copy
= FALSE
;
13479 vm_object_offset_t new_offset
;
13481 new_offset
= VME_OFFSET(new_entry
);
13482 result
= vm_object_copy_strategically(
13486 &VME_OBJECT(new_entry
),
13488 &new_entry_needs_copy
);
13489 if (new_offset
!= VME_OFFSET(new_entry
)) {
13490 VME_OFFSET_SET(new_entry
, new_offset
);
13493 new_entry
->needs_copy
= new_entry_needs_copy
;
13497 * Throw away the old object reference of the new entry.
13499 vm_object_deallocate(object
);
13501 if (result
!= KERN_SUCCESS
&&
13502 result
!= KERN_MEMORY_RESTART_COPY
) {
13503 _vm_map_entry_dispose(map_header
, new_entry
);
13508 * Verify that the map has not substantially
13509 * changed while the copy was being made.
13513 if (version
.main_timestamp
+ 1 != map
->timestamp
) {
13515 * Simple version comparison failed.
13517 * Retry the lookup and verify that the
13518 * same object/offset are still present.
13520 vm_object_deallocate(VME_OBJECT(new_entry
));
13521 _vm_map_entry_dispose(map_header
, new_entry
);
13522 if (result
== KERN_MEMORY_RESTART_COPY
)
13523 result
= KERN_SUCCESS
;
13527 if (result
== KERN_MEMORY_RESTART_COPY
) {
13528 vm_object_reference(object
);
13533 _vm_map_store_entry_link(map_header
,
13534 map_header
->links
.prev
, new_entry
);
13536 /*Protections for submap mapping are irrelevant here*/
13537 if( !src_entry
->is_sub_map
) {
13538 *cur_protection
&= src_entry
->protection
;
13539 *max_protection
&= src_entry
->max_protection
;
13541 map_address
+= tmp_size
;
13542 mapped_size
+= tmp_size
;
13543 src_start
+= tmp_size
;
13547 vm_map_unlock(map
);
13548 if (result
!= KERN_SUCCESS
) {
13550 * Free all allocated elements.
13552 for (src_entry
= map_header
->links
.next
;
13553 src_entry
!= (struct vm_map_entry
*)&map_header
->links
;
13554 src_entry
= new_entry
) {
13555 new_entry
= src_entry
->vme_next
;
13556 _vm_map_store_entry_unlink(map_header
, src_entry
);
13557 if (src_entry
->is_sub_map
) {
13558 vm_map_deallocate(VME_SUBMAP(src_entry
));
13560 vm_object_deallocate(VME_OBJECT(src_entry
));
13562 _vm_map_entry_dispose(map_header
, src_entry
);
13569 * Routine: vm_remap
13571 * Map portion of a task's address space.
13572 * Mapped region must not overlap more than
13573 * one vm memory object. Protections and
13574 * inheritance attributes remain the same
13575 * as in the original task and are out parameters.
13576 * Source and Target task can be identical
13577 * Other attributes are identical as for vm_map()
13581 vm_map_t target_map
,
13582 vm_map_address_t
*address
,
13583 vm_map_size_t size
,
13584 vm_map_offset_t mask
,
13587 vm_map_offset_t memory_address
,
13589 vm_prot_t
*cur_protection
,
13590 vm_prot_t
*max_protection
,
13591 vm_inherit_t inheritance
)
13593 kern_return_t result
;
13594 vm_map_entry_t entry
;
13595 vm_map_entry_t insp_entry
= VM_MAP_ENTRY_NULL
;
13596 vm_map_entry_t new_entry
;
13597 struct vm_map_header map_header
;
13598 vm_map_offset_t offset_in_mapping
;
13600 if (target_map
== VM_MAP_NULL
)
13601 return KERN_INVALID_ARGUMENT
;
13603 switch (inheritance
) {
13604 case VM_INHERIT_NONE
:
13605 case VM_INHERIT_COPY
:
13606 case VM_INHERIT_SHARE
:
13607 if (size
!= 0 && src_map
!= VM_MAP_NULL
)
13611 return KERN_INVALID_ARGUMENT
;
13615 * If the user is requesting that we return the address of the
13616 * first byte of the data (rather than the base of the page),
13617 * then we use different rounding semantics: specifically,
13618 * we assume that (memory_address, size) describes a region
13619 * all of whose pages we must cover, rather than a base to be truncated
13620 * down and a size to be added to that base. So we figure out
13621 * the highest page that the requested region includes and make
13622 * sure that the size will cover it.
13624 * The key example we're worried about it is of the form:
13626 * memory_address = 0x1ff0, size = 0x20
13628 * With the old semantics, we round down the memory_address to 0x1000
13629 * and round up the size to 0x1000, resulting in our covering *only*
13630 * page 0x1000. With the new semantics, we'd realize that the region covers
13631 * 0x1ff0-0x2010, and compute a size of 0x2000. Thus, we cover both page
13632 * 0x1000 and page 0x2000 in the region we remap.
13634 if ((flags
& VM_FLAGS_RETURN_DATA_ADDR
) != 0) {
13635 offset_in_mapping
= memory_address
- vm_map_trunc_page(memory_address
, PAGE_MASK
);
13636 size
= vm_map_round_page(memory_address
+ size
- vm_map_trunc_page(memory_address
, PAGE_MASK
), PAGE_MASK
);
13638 size
= vm_map_round_page(size
, PAGE_MASK
);
13641 result
= vm_map_remap_extract(src_map
, memory_address
,
13642 size
, copy
, &map_header
,
13646 target_map
->hdr
.entries_pageable
);
13648 if (result
!= KERN_SUCCESS
) {
13653 * Allocate/check a range of free virtual address
13654 * space for the target
13656 *address
= vm_map_trunc_page(*address
,
13657 VM_MAP_PAGE_MASK(target_map
));
13658 vm_map_lock(target_map
);
13659 result
= vm_map_remap_range_allocate(target_map
, address
, size
,
13660 mask
, flags
, &insp_entry
);
13662 for (entry
= map_header
.links
.next
;
13663 entry
!= (struct vm_map_entry
*)&map_header
.links
;
13664 entry
= new_entry
) {
13665 new_entry
= entry
->vme_next
;
13666 _vm_map_store_entry_unlink(&map_header
, entry
);
13667 if (result
== KERN_SUCCESS
) {
13668 if (flags
& VM_FLAGS_RESILIENT_CODESIGN
) {
13669 /* no codesigning -> read-only access */
13670 assert(!entry
->used_for_jit
);
13671 entry
->max_protection
= VM_PROT_READ
;
13672 entry
->protection
= VM_PROT_READ
;
13673 entry
->vme_resilient_codesign
= TRUE
;
13675 entry
->vme_start
+= *address
;
13676 entry
->vme_end
+= *address
;
13677 assert(!entry
->map_aligned
);
13678 vm_map_store_entry_link(target_map
, insp_entry
, entry
);
13679 insp_entry
= entry
;
13681 if (!entry
->is_sub_map
) {
13682 vm_object_deallocate(VME_OBJECT(entry
));
13684 vm_map_deallocate(VME_SUBMAP(entry
));
13686 _vm_map_entry_dispose(&map_header
, entry
);
13690 if (flags
& VM_FLAGS_RESILIENT_CODESIGN
) {
13691 *cur_protection
= VM_PROT_READ
;
13692 *max_protection
= VM_PROT_READ
;
13695 if( target_map
->disable_vmentry_reuse
== TRUE
) {
13696 if( target_map
->highest_entry_end
< insp_entry
->vme_end
){
13697 target_map
->highest_entry_end
= insp_entry
->vme_end
;
13701 if (result
== KERN_SUCCESS
) {
13702 target_map
->size
+= size
;
13703 SAVE_HINT_MAP_WRITE(target_map
, insp_entry
);
13705 vm_map_unlock(target_map
);
13707 if (result
== KERN_SUCCESS
&& target_map
->wiring_required
)
13708 result
= vm_map_wire(target_map
, *address
,
13709 *address
+ size
, *cur_protection
| VM_PROT_MEMORY_TAG_MAKE(VM_KERN_MEMORY_MLOCK
),
13713 * If requested, return the address of the data pointed to by the
13714 * request, rather than the base of the resulting page.
13716 if ((flags
& VM_FLAGS_RETURN_DATA_ADDR
) != 0) {
13717 *address
+= offset_in_mapping
;
13724 * Routine: vm_map_remap_range_allocate
13727 * Allocate a range in the specified virtual address map.
13728 * returns the address and the map entry just before the allocated
13731 * Map must be locked.
13734 static kern_return_t
13735 vm_map_remap_range_allocate(
13737 vm_map_address_t
*address
, /* IN/OUT */
13738 vm_map_size_t size
,
13739 vm_map_offset_t mask
,
13741 vm_map_entry_t
*map_entry
) /* OUT */
13743 vm_map_entry_t entry
;
13744 vm_map_offset_t start
;
13745 vm_map_offset_t end
;
13747 vm_map_entry_t hole_entry
;
13753 if (flags
& VM_FLAGS_ANYWHERE
)
13756 * Calculate the first possible address.
13759 if (start
< map
->min_offset
)
13760 start
= map
->min_offset
;
13761 if (start
> map
->max_offset
)
13762 return(KERN_NO_SPACE
);
13765 * Look for the first possible address;
13766 * if there's already something at this
13767 * address, we have to start after it.
13770 if( map
->disable_vmentry_reuse
== TRUE
) {
13771 VM_MAP_HIGHEST_ENTRY(map
, entry
, start
);
13774 if (map
->holelistenabled
) {
13775 hole_entry
= (vm_map_entry_t
)map
->holes_list
;
13777 if (hole_entry
== NULL
) {
13779 * No more space in the map?
13781 return(KERN_NO_SPACE
);
13784 boolean_t found_hole
= FALSE
;
13787 if (hole_entry
->vme_start
>= start
) {
13788 start
= hole_entry
->vme_start
;
13793 if (hole_entry
->vme_end
> start
) {
13797 hole_entry
= hole_entry
->vme_next
;
13799 } while (hole_entry
!= (vm_map_entry_t
) map
->holes_list
);
13801 if (found_hole
== FALSE
) {
13802 return (KERN_NO_SPACE
);
13805 entry
= hole_entry
;
13808 assert(first_free_is_valid(map
));
13809 if (start
== map
->min_offset
) {
13810 if ((entry
= map
->first_free
) != vm_map_to_entry(map
))
13811 start
= entry
->vme_end
;
13813 vm_map_entry_t tmp_entry
;
13814 if (vm_map_lookup_entry(map
, start
, &tmp_entry
))
13815 start
= tmp_entry
->vme_end
;
13819 start
= vm_map_round_page(start
,
13820 VM_MAP_PAGE_MASK(map
));
13824 * In any case, the "entry" always precedes
13825 * the proposed new region throughout the
13830 register vm_map_entry_t next
;
13833 * Find the end of the proposed new region.
13834 * Be sure we didn't go beyond the end, or
13835 * wrap around the address.
13838 end
= ((start
+ mask
) & ~mask
);
13839 end
= vm_map_round_page(end
,
13840 VM_MAP_PAGE_MASK(map
));
13842 return(KERN_NO_SPACE
);
13846 if ((end
> map
->max_offset
) || (end
< start
)) {
13847 if (map
->wait_for_space
) {
13848 if (size
<= (map
->max_offset
-
13849 map
->min_offset
)) {
13850 assert_wait((event_t
) map
, THREAD_INTERRUPTIBLE
);
13851 vm_map_unlock(map
);
13852 thread_block(THREAD_CONTINUE_NULL
);
13858 return(KERN_NO_SPACE
);
13861 next
= entry
->vme_next
;
13863 if (map
->holelistenabled
) {
13864 if (entry
->vme_end
>= end
)
13868 * If there are no more entries, we must win.
13872 * If there is another entry, it must be
13873 * after the end of the potential new region.
13876 if (next
== vm_map_to_entry(map
))
13879 if (next
->vme_start
>= end
)
13884 * Didn't fit -- move to the next entry.
13889 if (map
->holelistenabled
) {
13890 if (entry
== (vm_map_entry_t
) map
->holes_list
) {
13894 return(KERN_NO_SPACE
);
13896 start
= entry
->vme_start
;
13898 start
= entry
->vme_end
;
13902 if (map
->holelistenabled
) {
13904 if (vm_map_lookup_entry(map
, entry
->vme_start
, &entry
)) {
13905 panic("Found an existing entry (%p) instead of potential hole at address: 0x%llx.\n", entry
, (unsigned long long)entry
->vme_start
);
13912 vm_map_entry_t temp_entry
;
13916 * the address doesn't itself violate
13917 * the mask requirement.
13920 if ((start
& mask
) != 0)
13921 return(KERN_NO_SPACE
);
13925 * ... the address is within bounds
13928 end
= start
+ size
;
13930 if ((start
< map
->min_offset
) ||
13931 (end
> map
->max_offset
) ||
13933 return(KERN_INVALID_ADDRESS
);
13937 * If we're asked to overwrite whatever was mapped in that
13938 * range, first deallocate that range.
13940 if (flags
& VM_FLAGS_OVERWRITE
) {
13944 * We use a "zap_map" to avoid having to unlock
13945 * the "map" in vm_map_delete(), which would compromise
13946 * the atomicity of the "deallocate" and then "remap"
13949 zap_map
= vm_map_create(PMAP_NULL
,
13952 map
->hdr
.entries_pageable
);
13953 if (zap_map
== VM_MAP_NULL
) {
13954 return KERN_RESOURCE_SHORTAGE
;
13956 vm_map_set_page_shift(zap_map
, VM_MAP_PAGE_SHIFT(map
));
13957 vm_map_disable_hole_optimization(zap_map
);
13959 kr
= vm_map_delete(map
, start
, end
,
13960 (VM_MAP_REMOVE_SAVE_ENTRIES
|
13961 VM_MAP_REMOVE_NO_MAP_ALIGN
),
13963 if (kr
== KERN_SUCCESS
) {
13964 vm_map_destroy(zap_map
,
13965 VM_MAP_REMOVE_NO_PMAP_CLEANUP
);
13966 zap_map
= VM_MAP_NULL
;
13971 * ... the starting address isn't allocated
13974 if (vm_map_lookup_entry(map
, start
, &temp_entry
))
13975 return(KERN_NO_SPACE
);
13977 entry
= temp_entry
;
13980 * ... the next region doesn't overlap the
13984 if ((entry
->vme_next
!= vm_map_to_entry(map
)) &&
13985 (entry
->vme_next
->vme_start
< end
))
13986 return(KERN_NO_SPACE
);
13988 *map_entry
= entry
;
13989 return(KERN_SUCCESS
);
13995 * Set the address map for the current thread to the specified map
14003 thread_t thread
= current_thread();
14004 vm_map_t oldmap
= thread
->map
;
14006 mp_disable_preemption();
14007 mycpu
= cpu_number();
14010 * Deactivate the current map and activate the requested map
14012 PMAP_SWITCH_USER(thread
, map
, mycpu
);
14014 mp_enable_preemption();
14020 * Routine: vm_map_write_user
14023 * Copy out data from a kernel space into space in the
14024 * destination map. The space must already exist in the
14026 * NOTE: This routine should only be called by threads
14027 * which can block on a page fault. i.e. kernel mode user
14035 vm_map_address_t dst_addr
,
14038 kern_return_t kr
= KERN_SUCCESS
;
14040 if(current_map() == map
) {
14041 if (copyout(src_p
, dst_addr
, size
)) {
14042 kr
= KERN_INVALID_ADDRESS
;
14047 /* take on the identity of the target map while doing */
14050 vm_map_reference(map
);
14051 oldmap
= vm_map_switch(map
);
14052 if (copyout(src_p
, dst_addr
, size
)) {
14053 kr
= KERN_INVALID_ADDRESS
;
14055 vm_map_switch(oldmap
);
14056 vm_map_deallocate(map
);
14062 * Routine: vm_map_read_user
14065 * Copy in data from a user space source map into the
14066 * kernel map. The space must already exist in the
14068 * NOTE: This routine should only be called by threads
14069 * which can block on a page fault. i.e. kernel mode user
14076 vm_map_address_t src_addr
,
14080 kern_return_t kr
= KERN_SUCCESS
;
14082 if(current_map() == map
) {
14083 if (copyin(src_addr
, dst_p
, size
)) {
14084 kr
= KERN_INVALID_ADDRESS
;
14089 /* take on the identity of the target map while doing */
14092 vm_map_reference(map
);
14093 oldmap
= vm_map_switch(map
);
14094 if (copyin(src_addr
, dst_p
, size
)) {
14095 kr
= KERN_INVALID_ADDRESS
;
14097 vm_map_switch(oldmap
);
14098 vm_map_deallocate(map
);
14105 * vm_map_check_protection:
14107 * Assert that the target map allows the specified
14108 * privilege on the entire address region given.
14109 * The entire region must be allocated.
14112 vm_map_check_protection(vm_map_t map
, vm_map_offset_t start
,
14113 vm_map_offset_t end
, vm_prot_t protection
)
14115 vm_map_entry_t entry
;
14116 vm_map_entry_t tmp_entry
;
14120 if (start
< vm_map_min(map
) || end
> vm_map_max(map
) || start
> end
)
14122 vm_map_unlock(map
);
14126 if (!vm_map_lookup_entry(map
, start
, &tmp_entry
)) {
14127 vm_map_unlock(map
);
14133 while (start
< end
) {
14134 if (entry
== vm_map_to_entry(map
)) {
14135 vm_map_unlock(map
);
14140 * No holes allowed!
14143 if (start
< entry
->vme_start
) {
14144 vm_map_unlock(map
);
14149 * Check protection associated with entry.
14152 if ((entry
->protection
& protection
) != protection
) {
14153 vm_map_unlock(map
);
14157 /* go to next entry */
14159 start
= entry
->vme_end
;
14160 entry
= entry
->vme_next
;
14162 vm_map_unlock(map
);
14167 vm_map_purgable_control(
14169 vm_map_offset_t address
,
14170 vm_purgable_t control
,
14173 vm_map_entry_t entry
;
14174 vm_object_t object
;
14176 boolean_t was_nonvolatile
;
14179 * Vet all the input parameters and current type and state of the
14180 * underlaying object. Return with an error if anything is amiss.
14182 if (map
== VM_MAP_NULL
)
14183 return(KERN_INVALID_ARGUMENT
);
14185 if (control
!= VM_PURGABLE_SET_STATE
&&
14186 control
!= VM_PURGABLE_GET_STATE
&&
14187 control
!= VM_PURGABLE_PURGE_ALL
)
14188 return(KERN_INVALID_ARGUMENT
);
14190 if (control
== VM_PURGABLE_PURGE_ALL
) {
14191 vm_purgeable_object_purge_all();
14192 return KERN_SUCCESS
;
14195 if (control
== VM_PURGABLE_SET_STATE
&&
14196 (((*state
& ~(VM_PURGABLE_ALL_MASKS
)) != 0) ||
14197 ((*state
& VM_PURGABLE_STATE_MASK
) > VM_PURGABLE_STATE_MASK
)))
14198 return(KERN_INVALID_ARGUMENT
);
14200 vm_map_lock_read(map
);
14202 if (!vm_map_lookup_entry(map
, address
, &entry
) || entry
->is_sub_map
) {
14205 * Must pass a valid non-submap address.
14207 vm_map_unlock_read(map
);
14208 return(KERN_INVALID_ADDRESS
);
14211 if ((entry
->protection
& VM_PROT_WRITE
) == 0) {
14213 * Can't apply purgable controls to something you can't write.
14215 vm_map_unlock_read(map
);
14216 return(KERN_PROTECTION_FAILURE
);
14219 object
= VME_OBJECT(entry
);
14220 if (object
== VM_OBJECT_NULL
||
14221 object
->purgable
== VM_PURGABLE_DENY
) {
14223 * Object must already be present and be purgeable.
14225 vm_map_unlock_read(map
);
14226 return KERN_INVALID_ARGUMENT
;
14229 vm_object_lock(object
);
14232 if (VME_OFFSET(entry
) != 0 ||
14233 entry
->vme_end
- entry
->vme_start
!= object
->vo_size
) {
14235 * Can only apply purgable controls to the whole (existing)
14238 vm_map_unlock_read(map
);
14239 vm_object_unlock(object
);
14240 return KERN_INVALID_ARGUMENT
;
14244 assert(!entry
->is_sub_map
);
14245 assert(!entry
->use_pmap
); /* purgeable has its own accounting */
14247 vm_map_unlock_read(map
);
14249 was_nonvolatile
= (object
->purgable
== VM_PURGABLE_NONVOLATILE
);
14251 kr
= vm_object_purgable_control(object
, control
, state
);
14253 if (was_nonvolatile
&&
14254 object
->purgable
!= VM_PURGABLE_NONVOLATILE
&&
14255 map
->pmap
== kernel_pmap
) {
14257 object
->vo_purgeable_volatilizer
= kernel_task
;
14261 vm_object_unlock(object
);
14267 vm_map_page_query_internal(
14268 vm_map_t target_map
,
14269 vm_map_offset_t offset
,
14274 vm_page_info_basic_data_t info
;
14275 mach_msg_type_number_t count
;
14277 count
= VM_PAGE_INFO_BASIC_COUNT
;
14278 kr
= vm_map_page_info(target_map
,
14280 VM_PAGE_INFO_BASIC
,
14281 (vm_page_info_t
) &info
,
14283 if (kr
== KERN_SUCCESS
) {
14284 *disposition
= info
.disposition
;
14285 *ref_count
= info
.ref_count
;
14297 vm_map_offset_t offset
,
14298 vm_page_info_flavor_t flavor
,
14299 vm_page_info_t info
,
14300 mach_msg_type_number_t
*count
)
14302 vm_map_entry_t map_entry
;
14303 vm_object_t object
;
14306 kern_return_t retval
= KERN_SUCCESS
;
14307 boolean_t top_object
;
14310 vm_page_info_basic_t basic_info
;
14312 vm_map_offset_t offset_in_page
;
14315 case VM_PAGE_INFO_BASIC
:
14316 if (*count
!= VM_PAGE_INFO_BASIC_COUNT
) {
14318 * The "vm_page_info_basic_data" structure was not
14319 * properly padded, so allow the size to be off by
14320 * one to maintain backwards binary compatibility...
14322 if (*count
!= VM_PAGE_INFO_BASIC_COUNT
- 1)
14323 return KERN_INVALID_ARGUMENT
;
14327 return KERN_INVALID_ARGUMENT
;
14335 retval
= KERN_SUCCESS
;
14336 offset_in_page
= offset
& PAGE_MASK
;
14337 offset
= vm_map_trunc_page(offset
, PAGE_MASK
);
14339 vm_map_lock_read(map
);
14342 * First, find the map entry covering "offset", going down
14343 * submaps if necessary.
14346 if (!vm_map_lookup_entry(map
, offset
, &map_entry
)) {
14347 vm_map_unlock_read(map
);
14348 return KERN_INVALID_ADDRESS
;
14350 /* compute offset from this map entry's start */
14351 offset
-= map_entry
->vme_start
;
14352 /* compute offset into this map entry's object (or submap) */
14353 offset
+= VME_OFFSET(map_entry
);
14355 if (map_entry
->is_sub_map
) {
14358 sub_map
= VME_SUBMAP(map_entry
);
14359 vm_map_lock_read(sub_map
);
14360 vm_map_unlock_read(map
);
14364 ref_count
= MAX(ref_count
, map
->ref_count
);
14370 object
= VME_OBJECT(map_entry
);
14371 if (object
== VM_OBJECT_NULL
) {
14372 /* no object -> no page */
14373 vm_map_unlock_read(map
);
14377 vm_object_lock(object
);
14378 vm_map_unlock_read(map
);
14381 * Go down the VM object shadow chain until we find the page
14382 * we're looking for.
14385 ref_count
= MAX(ref_count
, object
->ref_count
);
14387 m
= vm_page_lookup(object
, offset
);
14389 if (m
!= VM_PAGE_NULL
) {
14390 disposition
|= VM_PAGE_QUERY_PAGE_PRESENT
;
14394 if (object
->existence_map
) {
14395 if (vm_external_state_get(object
->existence_map
,
14397 VM_EXTERNAL_STATE_EXISTS
) {
14399 * this page has been paged out
14401 disposition
|= VM_PAGE_QUERY_PAGE_PAGED_OUT
;
14406 if (object
->internal
&&
14408 !object
->terminating
&&
14409 object
->pager_ready
) {
14411 if (COMPRESSED_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE
) {
14412 if (VM_COMPRESSOR_PAGER_STATE_GET(
14415 == VM_EXTERNAL_STATE_EXISTS
) {
14416 /* the pager has that page */
14417 disposition
|= VM_PAGE_QUERY_PAGE_PAGED_OUT
;
14421 memory_object_t pager
;
14423 vm_object_paging_begin(object
);
14424 pager
= object
->pager
;
14425 vm_object_unlock(object
);
14428 * Ask the default pager if
14429 * it has this page.
14431 kr
= memory_object_data_request(
14433 offset
+ object
->paging_offset
,
14434 0, /* just poke the pager */
14438 vm_object_lock(object
);
14439 vm_object_paging_end(object
);
14441 if (kr
== KERN_SUCCESS
) {
14442 /* the default pager has it */
14443 disposition
|= VM_PAGE_QUERY_PAGE_PAGED_OUT
;
14449 if (object
->shadow
!= VM_OBJECT_NULL
) {
14450 vm_object_t shadow
;
14452 offset
+= object
->vo_shadow_offset
;
14453 shadow
= object
->shadow
;
14455 vm_object_lock(shadow
);
14456 vm_object_unlock(object
);
14459 top_object
= FALSE
;
14462 // if (!object->internal)
14464 // retval = KERN_FAILURE;
14465 // goto done_with_object;
14470 /* The ref_count is not strictly accurate, it measures the number */
14471 /* of entities holding a ref on the object, they may not be mapping */
14472 /* the object or may not be mapping the section holding the */
14473 /* target page but its still a ball park number and though an over- */
14474 /* count, it picks up the copy-on-write cases */
14476 /* We could also get a picture of page sharing from pmap_attributes */
14477 /* but this would under count as only faulted-in mappings would */
14480 if (top_object
== TRUE
&& object
->shadow
)
14481 disposition
|= VM_PAGE_QUERY_PAGE_COPIED
;
14483 if (! object
->internal
)
14484 disposition
|= VM_PAGE_QUERY_PAGE_EXTERNAL
;
14486 if (m
== VM_PAGE_NULL
)
14487 goto done_with_object
;
14489 if (m
->fictitious
) {
14490 disposition
|= VM_PAGE_QUERY_PAGE_FICTITIOUS
;
14491 goto done_with_object
;
14493 if (m
->dirty
|| pmap_is_modified(m
->phys_page
))
14494 disposition
|= VM_PAGE_QUERY_PAGE_DIRTY
;
14496 if (m
->reference
|| pmap_is_referenced(m
->phys_page
))
14497 disposition
|= VM_PAGE_QUERY_PAGE_REF
;
14499 if (m
->speculative
)
14500 disposition
|= VM_PAGE_QUERY_PAGE_SPECULATIVE
;
14502 if (m
->cs_validated
)
14503 disposition
|= VM_PAGE_QUERY_PAGE_CS_VALIDATED
;
14505 disposition
|= VM_PAGE_QUERY_PAGE_CS_TAINTED
;
14507 disposition
|= VM_PAGE_QUERY_PAGE_CS_NX
;
14510 vm_object_unlock(object
);
14514 case VM_PAGE_INFO_BASIC
:
14515 basic_info
= (vm_page_info_basic_t
) info
;
14516 basic_info
->disposition
= disposition
;
14517 basic_info
->ref_count
= ref_count
;
14518 basic_info
->object_id
= (vm_object_id_t
) (uintptr_t)
14519 VM_KERNEL_ADDRPERM(object
);
14520 basic_info
->offset
=
14521 (memory_object_offset_t
) offset
+ offset_in_page
;
14522 basic_info
->depth
= depth
;
14532 * Synchronises the memory range specified with its backing store
14533 * image by either flushing or cleaning the contents to the appropriate
14534 * memory manager engaging in a memory object synchronize dialog with
14535 * the manager. The client doesn't return until the manager issues
14536 * m_o_s_completed message. MIG Magically converts user task parameter
14537 * to the task's address map.
14539 * interpretation of sync_flags
14540 * VM_SYNC_INVALIDATE - discard pages, only return precious
14541 * pages to manager.
14543 * VM_SYNC_INVALIDATE & (VM_SYNC_SYNCHRONOUS | VM_SYNC_ASYNCHRONOUS)
14544 * - discard pages, write dirty or precious
14545 * pages back to memory manager.
14547 * VM_SYNC_SYNCHRONOUS | VM_SYNC_ASYNCHRONOUS
14548 * - write dirty or precious pages back to
14549 * the memory manager.
14551 * VM_SYNC_CONTIGUOUS - does everything normally, but if there
14552 * is a hole in the region, and we would
14553 * have returned KERN_SUCCESS, return
14554 * KERN_INVALID_ADDRESS instead.
14557 * The memory object attributes have not yet been implemented, this
14558 * function will have to deal with the invalidate attribute
14561 * KERN_INVALID_TASK Bad task parameter
14562 * KERN_INVALID_ARGUMENT both sync and async were specified.
14563 * KERN_SUCCESS The usual.
14564 * KERN_INVALID_ADDRESS There was a hole in the region.
14570 vm_map_address_t address
,
14571 vm_map_size_t size
,
14572 vm_sync_t sync_flags
)
14575 msync_req_t new_msr
;
14576 queue_chain_t req_q
; /* queue of requests for this msync */
14577 vm_map_entry_t entry
;
14578 vm_map_size_t amount_left
;
14579 vm_object_offset_t offset
;
14580 boolean_t do_sync_req
;
14581 boolean_t had_hole
= FALSE
;
14582 memory_object_t pager
;
14583 vm_map_offset_t pmap_offset
;
14585 if ((sync_flags
& VM_SYNC_ASYNCHRONOUS
) &&
14586 (sync_flags
& VM_SYNC_SYNCHRONOUS
))
14587 return(KERN_INVALID_ARGUMENT
);
14590 * align address and size on page boundaries
14592 size
= (vm_map_round_page(address
+ size
,
14593 VM_MAP_PAGE_MASK(map
)) -
14594 vm_map_trunc_page(address
,
14595 VM_MAP_PAGE_MASK(map
)));
14596 address
= vm_map_trunc_page(address
,
14597 VM_MAP_PAGE_MASK(map
));
14599 if (map
== VM_MAP_NULL
)
14600 return(KERN_INVALID_TASK
);
14603 return(KERN_SUCCESS
);
14605 queue_init(&req_q
);
14606 amount_left
= size
;
14608 while (amount_left
> 0) {
14609 vm_object_size_t flush_size
;
14610 vm_object_t object
;
14613 if (!vm_map_lookup_entry(map
,
14617 vm_map_size_t skip
;
14620 * hole in the address map.
14625 * Check for empty map.
14627 if (entry
== vm_map_to_entry(map
) &&
14628 entry
->vme_next
== entry
) {
14629 vm_map_unlock(map
);
14633 * Check that we don't wrap and that
14634 * we have at least one real map entry.
14636 if ((map
->hdr
.nentries
== 0) ||
14637 (entry
->vme_next
->vme_start
< address
)) {
14638 vm_map_unlock(map
);
14642 * Move up to the next entry if needed
14644 skip
= (entry
->vme_next
->vme_start
- address
);
14645 if (skip
>= amount_left
)
14648 amount_left
-= skip
;
14649 address
= entry
->vme_next
->vme_start
;
14650 vm_map_unlock(map
);
14654 offset
= address
- entry
->vme_start
;
14655 pmap_offset
= address
;
14658 * do we have more to flush than is contained in this
14661 if (amount_left
+ entry
->vme_start
+ offset
> entry
->vme_end
) {
14662 flush_size
= entry
->vme_end
-
14663 (entry
->vme_start
+ offset
);
14665 flush_size
= amount_left
;
14667 amount_left
-= flush_size
;
14668 address
+= flush_size
;
14670 if (entry
->is_sub_map
== TRUE
) {
14671 vm_map_t local_map
;
14672 vm_map_offset_t local_offset
;
14674 local_map
= VME_SUBMAP(entry
);
14675 local_offset
= VME_OFFSET(entry
);
14676 vm_map_unlock(map
);
14681 sync_flags
) == KERN_INVALID_ADDRESS
) {
14686 object
= VME_OBJECT(entry
);
14689 * We can't sync this object if the object has not been
14692 if (object
== VM_OBJECT_NULL
) {
14693 vm_map_unlock(map
);
14696 offset
+= VME_OFFSET(entry
);
14698 vm_object_lock(object
);
14700 if (sync_flags
& (VM_SYNC_KILLPAGES
| VM_SYNC_DEACTIVATE
)) {
14701 int kill_pages
= 0;
14702 boolean_t reusable_pages
= FALSE
;
14704 if (sync_flags
& VM_SYNC_KILLPAGES
) {
14705 if (object
->ref_count
== 1 && !object
->shadow
)
14710 if (kill_pages
!= -1)
14711 vm_object_deactivate_pages(
14714 (vm_object_size_t
) flush_size
,
14719 vm_object_unlock(object
);
14720 vm_map_unlock(map
);
14724 * We can't sync this object if there isn't a pager.
14725 * Don't bother to sync internal objects, since there can't
14726 * be any "permanent" storage for these objects anyway.
14728 if ((object
->pager
== MEMORY_OBJECT_NULL
) ||
14729 (object
->internal
) || (object
->private)) {
14730 vm_object_unlock(object
);
14731 vm_map_unlock(map
);
14735 * keep reference on the object until syncing is done
14737 vm_object_reference_locked(object
);
14738 vm_object_unlock(object
);
14740 vm_map_unlock(map
);
14742 do_sync_req
= vm_object_sync(object
,
14745 sync_flags
& VM_SYNC_INVALIDATE
,
14746 ((sync_flags
& VM_SYNC_SYNCHRONOUS
) ||
14747 (sync_flags
& VM_SYNC_ASYNCHRONOUS
)),
14748 sync_flags
& VM_SYNC_SYNCHRONOUS
);
14750 * only send a m_o_s if we returned pages or if the entry
14751 * is writable (ie dirty pages may have already been sent back)
14753 if (!do_sync_req
) {
14754 if ((sync_flags
& VM_SYNC_INVALIDATE
) && object
->resident_page_count
== 0) {
14756 * clear out the clustering and read-ahead hints
14758 vm_object_lock(object
);
14760 object
->pages_created
= 0;
14761 object
->pages_used
= 0;
14762 object
->sequential
= 0;
14763 object
->last_alloc
= 0;
14765 vm_object_unlock(object
);
14767 vm_object_deallocate(object
);
14770 msync_req_alloc(new_msr
);
14772 vm_object_lock(object
);
14773 offset
+= object
->paging_offset
;
14775 new_msr
->offset
= offset
;
14776 new_msr
->length
= flush_size
;
14777 new_msr
->object
= object
;
14778 new_msr
->flag
= VM_MSYNC_SYNCHRONIZING
;
14782 * We can't sync this object if there isn't a pager. The
14783 * pager can disappear anytime we're not holding the object
14784 * lock. So this has to be checked anytime we goto re_iterate.
14787 pager
= object
->pager
;
14789 if (pager
== MEMORY_OBJECT_NULL
) {
14790 vm_object_unlock(object
);
14791 vm_object_deallocate(object
);
14792 msync_req_free(new_msr
);
14797 queue_iterate(&object
->msr_q
, msr
, msync_req_t
, msr_q
) {
14799 * need to check for overlapping entry, if found, wait
14800 * on overlapping msr to be done, then reiterate
14803 if (msr
->flag
== VM_MSYNC_SYNCHRONIZING
&&
14804 ((offset
>= msr
->offset
&&
14805 offset
< (msr
->offset
+ msr
->length
)) ||
14806 (msr
->offset
>= offset
&&
14807 msr
->offset
< (offset
+ flush_size
))))
14809 assert_wait((event_t
) msr
,THREAD_INTERRUPTIBLE
);
14811 vm_object_unlock(object
);
14812 thread_block(THREAD_CONTINUE_NULL
);
14813 vm_object_lock(object
);
14817 }/* queue_iterate */
14819 queue_enter(&object
->msr_q
, new_msr
, msync_req_t
, msr_q
);
14821 vm_object_paging_begin(object
);
14822 vm_object_unlock(object
);
14824 queue_enter(&req_q
, new_msr
, msync_req_t
, req_q
);
14826 (void) memory_object_synchronize(
14830 sync_flags
& ~VM_SYNC_CONTIGUOUS
);
14832 vm_object_lock(object
);
14833 vm_object_paging_end(object
);
14834 vm_object_unlock(object
);
14838 * wait for memory_object_sychronize_completed messages from pager(s)
14841 while (!queue_empty(&req_q
)) {
14842 msr
= (msync_req_t
)queue_first(&req_q
);
14844 while(msr
->flag
!= VM_MSYNC_DONE
) {
14845 assert_wait((event_t
) msr
, THREAD_INTERRUPTIBLE
);
14847 thread_block(THREAD_CONTINUE_NULL
);
14850 queue_remove(&req_q
, msr
, msync_req_t
, req_q
);
14852 vm_object_deallocate(msr
->object
);
14853 msync_req_free(msr
);
14854 }/* queue_iterate */
14856 /* for proper msync() behaviour */
14857 if (had_hole
== TRUE
&& (sync_flags
& VM_SYNC_CONTIGUOUS
))
14858 return(KERN_INVALID_ADDRESS
);
14860 return(KERN_SUCCESS
);
14864 * Routine: convert_port_entry_to_map
14866 * Convert from a port specifying an entry or a task
14867 * to a map. Doesn't consume the port ref; produces a map ref,
14868 * which may be null. Unlike convert_port_to_map, the
14869 * port may be task or a named entry backed.
14876 convert_port_entry_to_map(
14880 vm_named_entry_t named_entry
;
14881 uint32_t try_failed_count
= 0;
14883 if(IP_VALID(port
) && (ip_kotype(port
) == IKOT_NAMED_ENTRY
)) {
14886 if(ip_active(port
) && (ip_kotype(port
)
14887 == IKOT_NAMED_ENTRY
)) {
14889 (vm_named_entry_t
)port
->ip_kobject
;
14890 if (!(lck_mtx_try_lock(&(named_entry
)->Lock
))) {
14893 try_failed_count
++;
14894 mutex_pause(try_failed_count
);
14897 named_entry
->ref_count
++;
14898 lck_mtx_unlock(&(named_entry
)->Lock
);
14900 if ((named_entry
->is_sub_map
) &&
14901 (named_entry
->protection
14902 & VM_PROT_WRITE
)) {
14903 map
= named_entry
->backing
.map
;
14905 mach_destroy_memory_entry(port
);
14906 return VM_MAP_NULL
;
14908 vm_map_reference_swap(map
);
14909 mach_destroy_memory_entry(port
);
14913 return VM_MAP_NULL
;
14917 map
= convert_port_to_map(port
);
14923 * Routine: convert_port_entry_to_object
14925 * Convert from a port specifying a named entry to an
14926 * object. Doesn't consume the port ref; produces a map ref,
14927 * which may be null.
14934 convert_port_entry_to_object(
14937 vm_object_t object
= VM_OBJECT_NULL
;
14938 vm_named_entry_t named_entry
;
14939 uint32_t try_failed_count
= 0;
14941 if (IP_VALID(port
) &&
14942 (ip_kotype(port
) == IKOT_NAMED_ENTRY
)) {
14945 if (ip_active(port
) &&
14946 (ip_kotype(port
) == IKOT_NAMED_ENTRY
)) {
14947 named_entry
= (vm_named_entry_t
)port
->ip_kobject
;
14948 if (!(lck_mtx_try_lock(&(named_entry
)->Lock
))) {
14950 try_failed_count
++;
14951 mutex_pause(try_failed_count
);
14954 named_entry
->ref_count
++;
14955 lck_mtx_unlock(&(named_entry
)->Lock
);
14957 if (!(named_entry
->is_sub_map
) &&
14958 !(named_entry
->is_pager
) &&
14959 !(named_entry
->is_copy
) &&
14960 (named_entry
->protection
& VM_PROT_WRITE
)) {
14961 object
= named_entry
->backing
.object
;
14962 vm_object_reference(object
);
14964 mach_destroy_memory_entry(port
);
14972 * Export routines to other components for the things we access locally through
14979 return (current_map_fast());
14983 * vm_map_reference:
14985 * Most code internal to the osfmk will go through a
14986 * macro defining this. This is always here for the
14987 * use of other kernel components.
14989 #undef vm_map_reference
14992 register vm_map_t map
)
14994 if (map
== VM_MAP_NULL
)
14997 lck_mtx_lock(&map
->s_lock
);
14999 assert(map
->res_count
> 0);
15000 assert(map
->ref_count
>= map
->res_count
);
15004 lck_mtx_unlock(&map
->s_lock
);
15008 * vm_map_deallocate:
15010 * Removes a reference from the specified map,
15011 * destroying it if no references remain.
15012 * The map should not be locked.
15016 register vm_map_t map
)
15020 if (map
== VM_MAP_NULL
)
15023 lck_mtx_lock(&map
->s_lock
);
15024 ref
= --map
->ref_count
;
15026 vm_map_res_deallocate(map
);
15027 lck_mtx_unlock(&map
->s_lock
);
15030 assert(map
->ref_count
== 0);
15031 lck_mtx_unlock(&map
->s_lock
);
15035 * The map residence count isn't decremented here because
15036 * the vm_map_delete below will traverse the entire map,
15037 * deleting entries, and the residence counts on objects
15038 * and sharing maps will go away then.
15042 vm_map_destroy(map
, VM_MAP_NO_FLAGS
);
15047 vm_map_disable_NX(vm_map_t map
)
15051 if (map
->pmap
== NULL
)
15054 pmap_disable_NX(map
->pmap
);
15058 vm_map_disallow_data_exec(vm_map_t map
)
15063 map
->map_disallow_data_exec
= TRUE
;
15066 /* XXX Consider making these constants (VM_MAX_ADDRESS and MACH_VM_MAX_ADDRESS)
15067 * more descriptive.
15070 vm_map_set_32bit(vm_map_t map
)
15072 map
->max_offset
= (vm_map_offset_t
)VM_MAX_ADDRESS
;
15077 vm_map_set_64bit(vm_map_t map
)
15079 map
->max_offset
= (vm_map_offset_t
)MACH_VM_MAX_ADDRESS
;
15083 vm_compute_max_offset(boolean_t is64
)
15085 return (is64
? (vm_map_offset_t
)MACH_VM_MAX_ADDRESS
: (vm_map_offset_t
)VM_MAX_ADDRESS
);
15089 vm_map_get_max_aslr_slide_pages(vm_map_t map
)
15091 return (1 << (vm_map_is_64bit(map
) ? 16 : 8));
15098 return map
->max_offset
> ((vm_map_offset_t
)VM_MAX_ADDRESS
);
15102 vm_map_has_hard_pagezero(
15104 vm_map_offset_t pagezero_size
)
15108 * We should lock the VM map (for read) here but we can get away
15109 * with it for now because there can't really be any race condition:
15110 * the VM map's min_offset is changed only when the VM map is created
15111 * and when the zero page is established (when the binary gets loaded),
15112 * and this routine gets called only when the task terminates and the
15113 * VM map is being torn down, and when a new map is created via
15114 * load_machfile()/execve().
15116 return (map
->min_offset
>= pagezero_size
);
15120 * Raise a VM map's maximun offset.
15123 vm_map_raise_max_offset(
15125 vm_map_offset_t new_max_offset
)
15130 ret
= KERN_INVALID_ADDRESS
;
15132 if (new_max_offset
>= map
->max_offset
) {
15133 if (!vm_map_is_64bit(map
)) {
15134 if (new_max_offset
<= (vm_map_offset_t
)VM_MAX_ADDRESS
) {
15135 map
->max_offset
= new_max_offset
;
15136 ret
= KERN_SUCCESS
;
15139 if (new_max_offset
<= (vm_map_offset_t
)MACH_VM_MAX_ADDRESS
) {
15140 map
->max_offset
= new_max_offset
;
15141 ret
= KERN_SUCCESS
;
15146 vm_map_unlock(map
);
15152 * Raise a VM map's minimum offset.
15153 * To strictly enforce "page zero" reservation.
15156 vm_map_raise_min_offset(
15158 vm_map_offset_t new_min_offset
)
15160 vm_map_entry_t first_entry
;
15162 new_min_offset
= vm_map_round_page(new_min_offset
,
15163 VM_MAP_PAGE_MASK(map
));
15167 if (new_min_offset
< map
->min_offset
) {
15169 * Can't move min_offset backwards, as that would expose
15170 * a part of the address space that was previously, and for
15171 * possibly good reasons, inaccessible.
15173 vm_map_unlock(map
);
15174 return KERN_INVALID_ADDRESS
;
15176 if (new_min_offset
>= map
->max_offset
) {
15177 /* can't go beyond the end of the address space */
15178 vm_map_unlock(map
);
15179 return KERN_INVALID_ADDRESS
;
15182 first_entry
= vm_map_first_entry(map
);
15183 if (first_entry
!= vm_map_to_entry(map
) &&
15184 first_entry
->vme_start
< new_min_offset
) {
15186 * Some memory was already allocated below the new
15187 * minimun offset. It's too late to change it now...
15189 vm_map_unlock(map
);
15190 return KERN_NO_SPACE
;
15193 map
->min_offset
= new_min_offset
;
15195 assert(map
->holes_list
);
15196 map
->holes_list
->start
= new_min_offset
;
15197 assert(new_min_offset
< map
->holes_list
->end
);
15199 vm_map_unlock(map
);
15201 return KERN_SUCCESS
;
15205 * Set the limit on the maximum amount of user wired memory allowed for this map.
15206 * This is basically a copy of the MEMLOCK rlimit value maintained by the BSD side of
15207 * the kernel. The limits are checked in the mach VM side, so we keep a copy so we
15208 * don't have to reach over to the BSD data structures.
15212 vm_map_set_user_wire_limit(vm_map_t map
,
15215 map
->user_wire_limit
= limit
;
15219 void vm_map_switch_protect(vm_map_t map
,
15223 map
->switch_protect
=val
;
15224 vm_map_unlock(map
);
15228 * IOKit has mapped a region into this map; adjust the pmap's ledgers appropriately.
15229 * phys_footprint is a composite limit consisting of iokit + physmem, so we need to
15230 * bump both counters.
15233 vm_map_iokit_mapped_region(vm_map_t map
, vm_size_t bytes
)
15235 pmap_t pmap
= vm_map_pmap(map
);
15237 ledger_credit(pmap
->ledger
, task_ledgers
.iokit_mapped
, bytes
);
15238 ledger_credit(pmap
->ledger
, task_ledgers
.phys_footprint
, bytes
);
15242 vm_map_iokit_unmapped_region(vm_map_t map
, vm_size_t bytes
)
15244 pmap_t pmap
= vm_map_pmap(map
);
15246 ledger_debit(pmap
->ledger
, task_ledgers
.iokit_mapped
, bytes
);
15247 ledger_debit(pmap
->ledger
, task_ledgers
.phys_footprint
, bytes
);
15250 /* Add (generate) code signature for memory range */
15251 #if CONFIG_DYNAMIC_CODE_SIGNING
15252 kern_return_t
vm_map_sign(vm_map_t map
,
15253 vm_map_offset_t start
,
15254 vm_map_offset_t end
)
15256 vm_map_entry_t entry
;
15258 vm_object_t object
;
15261 * Vet all the input parameters and current type and state of the
15262 * underlaying object. Return with an error if anything is amiss.
15264 if (map
== VM_MAP_NULL
)
15265 return(KERN_INVALID_ARGUMENT
);
15267 vm_map_lock_read(map
);
15269 if (!vm_map_lookup_entry(map
, start
, &entry
) || entry
->is_sub_map
) {
15271 * Must pass a valid non-submap address.
15273 vm_map_unlock_read(map
);
15274 return(KERN_INVALID_ADDRESS
);
15277 if((entry
->vme_start
> start
) || (entry
->vme_end
< end
)) {
15279 * Map entry doesn't cover the requested range. Not handling
15280 * this situation currently.
15282 vm_map_unlock_read(map
);
15283 return(KERN_INVALID_ARGUMENT
);
15286 object
= VME_OBJECT(entry
);
15287 if (object
== VM_OBJECT_NULL
) {
15289 * Object must already be present or we can't sign.
15291 vm_map_unlock_read(map
);
15292 return KERN_INVALID_ARGUMENT
;
15295 vm_object_lock(object
);
15296 vm_map_unlock_read(map
);
15298 while(start
< end
) {
15301 m
= vm_page_lookup(object
,
15302 start
- entry
->vme_start
+ VME_OFFSET(entry
));
15303 if (m
==VM_PAGE_NULL
) {
15304 /* shoud we try to fault a page here? we can probably
15305 * demand it exists and is locked for this request */
15306 vm_object_unlock(object
);
15307 return KERN_FAILURE
;
15309 /* deal with special page status */
15311 (m
->unusual
&& (m
->error
|| m
->restart
|| m
->private || m
->absent
))) {
15312 vm_object_unlock(object
);
15313 return KERN_FAILURE
;
15316 /* Page is OK... now "validate" it */
15317 /* This is the place where we'll call out to create a code
15318 * directory, later */
15319 m
->cs_validated
= TRUE
;
15321 /* The page is now "clean" for codesigning purposes. That means
15322 * we don't consider it as modified (wpmapped) anymore. But
15323 * we'll disconnect the page so we note any future modification
15325 m
->wpmapped
= FALSE
;
15326 refmod
= pmap_disconnect(m
->phys_page
);
15328 /* Pull the dirty status from the pmap, since we cleared the
15330 if ((refmod
& VM_MEM_MODIFIED
) && !m
->dirty
) {
15331 SET_PAGE_DIRTY(m
, FALSE
);
15334 /* On to the next page */
15335 start
+= PAGE_SIZE
;
15337 vm_object_unlock(object
);
15339 return KERN_SUCCESS
;
15343 kern_return_t
vm_map_partial_reap(vm_map_t map
, unsigned int *reclaimed_resident
, unsigned int *reclaimed_compressed
)
15345 vm_map_entry_t entry
= VM_MAP_ENTRY_NULL
;
15346 vm_map_entry_t next_entry
;
15347 kern_return_t kr
= KERN_SUCCESS
;
15353 * We use a "zap_map" to avoid having to unlock
15354 * the "map" in vm_map_delete().
15356 zap_map
= vm_map_create(PMAP_NULL
,
15359 map
->hdr
.entries_pageable
);
15361 if (zap_map
== VM_MAP_NULL
) {
15362 return KERN_RESOURCE_SHORTAGE
;
15365 vm_map_set_page_shift(zap_map
,
15366 VM_MAP_PAGE_SHIFT(map
));
15367 vm_map_disable_hole_optimization(zap_map
);
15369 for (entry
= vm_map_first_entry(map
);
15370 entry
!= vm_map_to_entry(map
);
15371 entry
= next_entry
) {
15372 next_entry
= entry
->vme_next
;
15374 if (VME_OBJECT(entry
) &&
15375 !entry
->is_sub_map
&&
15376 (VME_OBJECT(entry
)->internal
== TRUE
) &&
15377 (VME_OBJECT(entry
)->ref_count
== 1)) {
15379 *reclaimed_resident
+= VME_OBJECT(entry
)->resident_page_count
;
15380 *reclaimed_compressed
+= vm_compressor_pager_get_count(VME_OBJECT(entry
)->pager
);
15382 (void)vm_map_delete(map
,
15385 VM_MAP_REMOVE_SAVE_ENTRIES
,
15390 vm_map_unlock(map
);
15393 * Get rid of the "zap_maps" and all the map entries that
15394 * they may still contain.
15396 if (zap_map
!= VM_MAP_NULL
) {
15397 vm_map_destroy(zap_map
, VM_MAP_REMOVE_NO_PMAP_CLEANUP
);
15398 zap_map
= VM_MAP_NULL
;
15406 kern_return_t
vm_map_freeze_walk(
15408 unsigned int *purgeable_count
,
15409 unsigned int *wired_count
,
15410 unsigned int *clean_count
,
15411 unsigned int *dirty_count
,
15412 unsigned int dirty_budget
,
15413 boolean_t
*has_shared
)
15415 vm_map_entry_t entry
;
15417 vm_map_lock_read(map
);
15419 *purgeable_count
= *wired_count
= *clean_count
= *dirty_count
= 0;
15420 *has_shared
= FALSE
;
15422 for (entry
= vm_map_first_entry(map
);
15423 entry
!= vm_map_to_entry(map
);
15424 entry
= entry
->vme_next
) {
15425 unsigned int purgeable
, clean
, dirty
, wired
;
15428 if ((VME_OBJECT(entry
) == 0) ||
15429 (entry
->is_sub_map
) ||
15430 (VME_OBJECT(entry
)->phys_contiguous
)) {
15434 default_freezer_pack(&purgeable
, &wired
, &clean
, &dirty
, dirty_budget
, &shared
, VME_OBJECT(entry
), NULL
);
15436 *purgeable_count
+= purgeable
;
15437 *wired_count
+= wired
;
15438 *clean_count
+= clean
;
15439 *dirty_count
+= dirty
;
15442 *has_shared
= TRUE
;
15445 /* Adjust pageout budget and finish up if reached */
15446 if (dirty_budget
) {
15447 dirty_budget
-= dirty
;
15448 if (dirty_budget
== 0) {
15454 vm_map_unlock_read(map
);
15456 return KERN_SUCCESS
;
15459 int c_freezer_swapout_count
;
15460 int c_freezer_compression_count
= 0;
15461 AbsoluteTime c_freezer_last_yield_ts
= 0;
15463 kern_return_t
vm_map_freeze(
15465 unsigned int *purgeable_count
,
15466 unsigned int *wired_count
,
15467 unsigned int *clean_count
,
15468 unsigned int *dirty_count
,
15469 unsigned int dirty_budget
,
15470 boolean_t
*has_shared
)
15472 vm_map_entry_t entry2
= VM_MAP_ENTRY_NULL
;
15473 kern_return_t kr
= KERN_SUCCESS
;
15474 boolean_t default_freezer_active
= TRUE
;
15476 *purgeable_count
= *wired_count
= *clean_count
= *dirty_count
= 0;
15477 *has_shared
= FALSE
;
15480 * We need the exclusive lock here so that we can
15481 * block any page faults or lookups while we are
15482 * in the middle of freezing this vm map.
15486 if (COMPRESSED_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE
) {
15487 default_freezer_active
= FALSE
;
15489 if (vm_compressor_low_on_space() || vm_swap_low_on_space()) {
15490 kr
= KERN_NO_SPACE
;
15494 assert(default_freezer_active
== FALSE
);
15496 if (default_freezer_active
) {
15497 if (map
->default_freezer_handle
== NULL
) {
15498 map
->default_freezer_handle
= default_freezer_handle_allocate();
15501 if ((kr
= default_freezer_handle_init(map
->default_freezer_handle
)) != KERN_SUCCESS
) {
15503 * Can happen if default_freezer_handle passed in is NULL
15504 * Or, a table has already been allocated and associated
15505 * with this handle, i.e. the map is already frozen.
15510 c_freezer_compression_count
= 0;
15511 clock_get_uptime(&c_freezer_last_yield_ts
);
15513 for (entry2
= vm_map_first_entry(map
);
15514 entry2
!= vm_map_to_entry(map
);
15515 entry2
= entry2
->vme_next
) {
15517 vm_object_t src_object
= VME_OBJECT(entry2
);
15519 if (VME_OBJECT(entry2
) &&
15520 !entry2
->is_sub_map
&&
15521 !VME_OBJECT(entry2
)->phys_contiguous
) {
15522 /* If eligible, scan the entry, moving eligible pages over to our parent object */
15523 if (default_freezer_active
) {
15524 unsigned int purgeable
, clean
, dirty
, wired
;
15527 default_freezer_pack(&purgeable
, &wired
, &clean
, &dirty
, dirty_budget
, &shared
,
15528 src_object
, map
->default_freezer_handle
);
15530 *purgeable_count
+= purgeable
;
15531 *wired_count
+= wired
;
15532 *clean_count
+= clean
;
15533 *dirty_count
+= dirty
;
15535 /* Adjust pageout budget and finish up if reached */
15536 if (dirty_budget
) {
15537 dirty_budget
-= dirty
;
15538 if (dirty_budget
== 0) {
15544 *has_shared
= TRUE
;
15547 if (VME_OBJECT(entry2
)->internal
== TRUE
) {
15549 if (DEFAULT_FREEZER_COMPRESSED_PAGER_IS_SWAPBACKED
) {
15551 * Pages belonging to this object could be swapped to disk.
15552 * Make sure it's not a shared object because we could end
15553 * up just bringing it back in again.
15555 if (VME_OBJECT(entry2
)->ref_count
> 1) {
15559 vm_object_compressed_freezer_pageout(VME_OBJECT(entry2
));
15562 if (vm_compressor_low_on_space() || vm_swap_low_on_space()) {
15563 kr
= KERN_NO_SPACE
;
15570 if (default_freezer_active
) {
15571 /* Finally, throw out the pages to swap */
15572 default_freezer_pageout(map
->default_freezer_handle
);
15576 vm_map_unlock(map
);
15578 if (!default_freezer_active
) {
15579 vm_object_compressed_freezer_done();
15581 if (DEFAULT_FREEZER_COMPRESSED_PAGER_IS_SWAPBACKED
) {
15583 * reset the counter tracking the # of swapped c_segs
15584 * because we are now done with this freeze session and task.
15586 c_freezer_swapout_count
= 0;
15595 kern_return_t kr
= KERN_SUCCESS
;
15597 if (COMPRESSED_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE
) {
15599 * We will on-demand thaw in the presence of the compressed pager.
15606 if (map
->default_freezer_handle
== NULL
) {
15608 * This map is not in a frozen state.
15614 kr
= default_freezer_unpack(map
->default_freezer_handle
);
15616 vm_map_unlock(map
);
15623 * vm_map_entry_should_cow_for_true_share:
15625 * Determines if the map entry should be clipped and setup for copy-on-write
15626 * to avoid applying "true_share" to a large VM object when only a subset is
15629 * For now, we target only the map entries created for the Objective C
15630 * Garbage Collector, which initially have the following properties:
15631 * - alias == VM_MEMORY_MALLOC
15632 * - wired_count == 0
15634 * and a VM object with:
15636 * - copy_strategy == MEMORY_OBJECT_COPY_SYMMETRIC
15638 * - vo_size == ANON_CHUNK_SIZE
15640 * Only non-kernel map entries.
15643 vm_map_entry_should_cow_for_true_share(
15644 vm_map_entry_t entry
)
15646 vm_object_t object
;
15648 if (entry
->is_sub_map
) {
15649 /* entry does not point at a VM object */
15653 if (entry
->needs_copy
) {
15654 /* already set for copy_on_write: done! */
15658 if (VME_ALIAS(entry
) != VM_MEMORY_MALLOC
&&
15659 VME_ALIAS(entry
) != VM_MEMORY_MALLOC_SMALL
) {
15660 /* not a malloc heap or Obj-C Garbage Collector heap */
15664 if (entry
->wired_count
) {
15665 /* wired: can't change the map entry... */
15666 vm_counters
.should_cow_but_wired
++;
15670 object
= VME_OBJECT(entry
);
15672 if (object
== VM_OBJECT_NULL
) {
15673 /* no object yet... */
15677 if (!object
->internal
) {
15678 /* not an internal object */
15682 if (object
->copy_strategy
!= MEMORY_OBJECT_COPY_SYMMETRIC
) {
15683 /* not the default copy strategy */
15687 if (object
->true_share
) {
15688 /* already true_share: too late to avoid it */
15692 if (VME_ALIAS(entry
) == VM_MEMORY_MALLOC
&&
15693 object
->vo_size
!= ANON_CHUNK_SIZE
) {
15694 /* ... not an object created for the ObjC Garbage Collector */
15698 if (VME_ALIAS(entry
) == VM_MEMORY_MALLOC_SMALL
&&
15699 object
->vo_size
!= 2048 * 4096) {
15700 /* ... not a "MALLOC_SMALL" heap */
15705 * All the criteria match: we have a large object being targeted for "true_share".
15706 * To limit the adverse side-effects linked with "true_share", tell the caller to
15707 * try and avoid setting up the entire object for "true_share" by clipping the
15708 * targeted range and setting it up for copy-on-write.
15714 vm_map_round_page_mask(
15715 vm_map_offset_t offset
,
15716 vm_map_offset_t mask
)
15718 return VM_MAP_ROUND_PAGE(offset
, mask
);
15722 vm_map_trunc_page_mask(
15723 vm_map_offset_t offset
,
15724 vm_map_offset_t mask
)
15726 return VM_MAP_TRUNC_PAGE(offset
, mask
);
15730 vm_map_page_aligned(
15731 vm_map_offset_t offset
,
15732 vm_map_offset_t mask
)
15734 return ((offset
) & mask
) == 0;
15741 return VM_MAP_PAGE_SHIFT(map
);
15748 return VM_MAP_PAGE_SIZE(map
);
15755 return VM_MAP_PAGE_MASK(map
);
15759 vm_map_set_page_shift(
15763 if (map
->hdr
.nentries
!= 0) {
15764 /* too late to change page size */
15765 return KERN_FAILURE
;
15768 map
->hdr
.page_shift
= pageshift
;
15770 return KERN_SUCCESS
;
15777 int num_object_purged
;
15778 vm_map_entry_t entry
;
15779 vm_map_offset_t next_address
;
15780 vm_object_t object
;
15784 num_object_purged
= 0;
15786 vm_map_lock_read(map
);
15787 entry
= vm_map_first_entry(map
);
15788 while (entry
!= vm_map_to_entry(map
)) {
15789 if (entry
->is_sub_map
) {
15792 if (! (entry
->protection
& VM_PROT_WRITE
)) {
15795 object
= VME_OBJECT(entry
);
15796 if (object
== VM_OBJECT_NULL
) {
15799 if (object
->purgable
!= VM_PURGABLE_VOLATILE
) {
15803 vm_object_lock(object
);
15805 if (VME_OFFSET(entry
) != 0 ||
15806 (entry
->vme_end
- entry
->vme_start
) != object
->vo_size
) {
15807 vm_object_unlock(object
);
15811 next_address
= entry
->vme_end
;
15812 vm_map_unlock_read(map
);
15813 state
= VM_PURGABLE_EMPTY
;
15814 kr
= vm_object_purgable_control(object
,
15815 VM_PURGABLE_SET_STATE
,
15817 if (kr
== KERN_SUCCESS
) {
15818 num_object_purged
++;
15820 vm_object_unlock(object
);
15822 vm_map_lock_read(map
);
15823 if (vm_map_lookup_entry(map
, next_address
, &entry
)) {
15827 entry
= entry
->vme_next
;
15829 vm_map_unlock_read(map
);
15831 return num_object_purged
;
15835 vm_map_query_volatile(
15837 mach_vm_size_t
*volatile_virtual_size_p
,
15838 mach_vm_size_t
*volatile_resident_size_p
,
15839 mach_vm_size_t
*volatile_compressed_size_p
,
15840 mach_vm_size_t
*volatile_pmap_size_p
,
15841 mach_vm_size_t
*volatile_compressed_pmap_size_p
)
15843 mach_vm_size_t volatile_virtual_size
;
15844 mach_vm_size_t volatile_resident_count
;
15845 mach_vm_size_t volatile_compressed_count
;
15846 mach_vm_size_t volatile_pmap_count
;
15847 mach_vm_size_t volatile_compressed_pmap_count
;
15848 mach_vm_size_t resident_count
;
15849 unsigned int compressed_count
;
15850 vm_map_entry_t entry
;
15851 vm_object_t object
;
15853 /* map should be locked by caller */
15855 volatile_virtual_size
= 0;
15856 volatile_resident_count
= 0;
15857 volatile_compressed_count
= 0;
15858 volatile_pmap_count
= 0;
15859 volatile_compressed_pmap_count
= 0;
15861 for (entry
= vm_map_first_entry(map
);
15862 entry
!= vm_map_to_entry(map
);
15863 entry
= entry
->vme_next
) {
15864 if (entry
->is_sub_map
) {
15867 if (! (entry
->protection
& VM_PROT_WRITE
)) {
15870 object
= VME_OBJECT(entry
);
15871 if (object
== VM_OBJECT_NULL
) {
15874 if (object
->purgable
!= VM_PURGABLE_VOLATILE
&&
15875 object
->purgable
!= VM_PURGABLE_EMPTY
) {
15878 if (VME_OFFSET(entry
)) {
15880 * If the map entry has been split and the object now
15881 * appears several times in the VM map, we don't want
15882 * to count the object's resident_page_count more than
15883 * once. We count it only for the first one, starting
15884 * at offset 0 and ignore the other VM map entries.
15888 resident_count
= object
->resident_page_count
;
15889 if ((VME_OFFSET(entry
) / PAGE_SIZE
) >= resident_count
) {
15890 resident_count
= 0;
15892 resident_count
-= (VME_OFFSET(entry
) / PAGE_SIZE
);
15895 volatile_virtual_size
+= entry
->vme_end
- entry
->vme_start
;
15896 volatile_resident_count
+= resident_count
;
15897 if (object
->pager
) {
15898 volatile_compressed_count
+=
15899 vm_compressor_pager_get_count(object
->pager
);
15901 compressed_count
= 0;
15902 volatile_pmap_count
+= pmap_query_resident(map
->pmap
,
15905 &compressed_count
);
15906 volatile_compressed_pmap_count
+= compressed_count
;
15909 /* map is still locked on return */
15911 *volatile_virtual_size_p
= volatile_virtual_size
;
15912 *volatile_resident_size_p
= volatile_resident_count
* PAGE_SIZE
;
15913 *volatile_compressed_size_p
= volatile_compressed_count
* PAGE_SIZE
;
15914 *volatile_pmap_size_p
= volatile_pmap_count
* PAGE_SIZE
;
15915 *volatile_compressed_pmap_size_p
= volatile_compressed_pmap_count
* PAGE_SIZE
;
15917 return KERN_SUCCESS
;
15921 vm_map_sizes(vm_map_t map
,
15922 vm_map_size_t
* psize
,
15923 vm_map_size_t
* pfree
,
15924 vm_map_size_t
* plargest_free
)
15926 vm_map_entry_t entry
;
15927 vm_map_offset_t prev
;
15928 vm_map_size_t free
, total_free
, largest_free
;
15931 total_free
= largest_free
= 0;
15933 vm_map_lock_read(map
);
15934 if (psize
) *psize
= map
->max_offset
- map
->min_offset
;
15936 prev
= map
->min_offset
;
15937 for (entry
= vm_map_first_entry(map
);; entry
= entry
->vme_next
)
15939 end
= (entry
== vm_map_to_entry(map
));
15941 if (end
) free
= entry
->vme_end
- prev
;
15942 else free
= entry
->vme_start
- prev
;
15944 total_free
+= free
;
15945 if (free
> largest_free
) largest_free
= free
;
15948 prev
= entry
->vme_end
;
15950 vm_map_unlock_read(map
);
15951 if (pfree
) *pfree
= total_free
;
15952 if (plargest_free
) *plargest_free
= largest_free
;
15955 #if VM_SCAN_FOR_SHADOW_CHAIN
15956 int vm_map_shadow_max(vm_map_t map
);
15957 int vm_map_shadow_max(
15960 int shadows
, shadows_max
;
15961 vm_map_entry_t entry
;
15962 vm_object_t object
, next_object
;
15969 vm_map_lock_read(map
);
15971 for (entry
= vm_map_first_entry(map
);
15972 entry
!= vm_map_to_entry(map
);
15973 entry
= entry
->vme_next
) {
15974 if (entry
->is_sub_map
) {
15977 object
= VME_OBJECT(entry
);
15978 if (object
== NULL
) {
15981 vm_object_lock_shared(object
);
15983 object
->shadow
!= NULL
;
15984 shadows
++, object
= next_object
) {
15985 next_object
= object
->shadow
;
15986 vm_object_lock_shared(next_object
);
15987 vm_object_unlock(object
);
15989 vm_object_unlock(object
);
15990 if (shadows
> shadows_max
) {
15991 shadows_max
= shadows
;
15995 vm_map_unlock_read(map
);
15997 return shadows_max
;
15999 #endif /* VM_SCAN_FOR_SHADOW_CHAIN */