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 */
8650 vm_map_copy_validate_size(
8655 if (copy
== VM_MAP_COPY_NULL
)
8657 switch (copy
->type
) {
8658 case VM_MAP_COPY_OBJECT
:
8659 case VM_MAP_COPY_KERNEL_BUFFER
:
8660 if (size
== copy
->size
)
8663 case VM_MAP_COPY_ENTRY_LIST
:
8665 * potential page-size rounding prevents us from exactly
8666 * validating this flavor of vm_map_copy, but we can at least
8667 * assert that it's within a range.
8669 if (copy
->size
>= size
&&
8670 copy
->size
<= vm_map_round_page(size
,
8671 VM_MAP_PAGE_MASK(dst_map
)))
8682 * Routine: vm_map_copyout
8685 * Copy out a copy chain ("copy") into newly-allocated
8686 * space in the destination map.
8688 * If successful, consumes the copy object.
8689 * Otherwise, the caller is responsible for it.
8695 vm_map_address_t
*dst_addr
, /* OUT */
8698 return vm_map_copyout_internal(dst_map
, dst_addr
, copy
,
8699 TRUE
, /* consume_on_success */
8702 VM_INHERIT_DEFAULT
);
8706 vm_map_copyout_internal(
8708 vm_map_address_t
*dst_addr
, /* OUT */
8710 boolean_t consume_on_success
,
8711 vm_prot_t cur_protection
,
8712 vm_prot_t max_protection
,
8713 vm_inherit_t inheritance
)
8716 vm_map_size_t adjustment
;
8717 vm_map_offset_t start
;
8718 vm_object_offset_t vm_copy_start
;
8719 vm_map_entry_t last
;
8720 vm_map_entry_t entry
;
8721 vm_map_entry_t hole_entry
;
8724 * Check for null copy object.
8727 if (copy
== VM_MAP_COPY_NULL
) {
8729 return(KERN_SUCCESS
);
8733 * Check for special copy object, created
8734 * by vm_map_copyin_object.
8737 if (copy
->type
== VM_MAP_COPY_OBJECT
) {
8738 vm_object_t object
= copy
->cpy_object
;
8740 vm_object_offset_t offset
;
8742 offset
= vm_object_trunc_page(copy
->offset
);
8743 size
= vm_map_round_page((copy
->size
+
8744 (vm_map_size_t
)(copy
->offset
-
8746 VM_MAP_PAGE_MASK(dst_map
));
8748 kr
= vm_map_enter(dst_map
, dst_addr
, size
,
8749 (vm_map_offset_t
) 0, VM_FLAGS_ANYWHERE
,
8750 object
, offset
, FALSE
,
8751 VM_PROT_DEFAULT
, VM_PROT_ALL
,
8752 VM_INHERIT_DEFAULT
);
8753 if (kr
!= KERN_SUCCESS
)
8755 /* Account for non-pagealigned copy object */
8756 *dst_addr
+= (vm_map_offset_t
)(copy
->offset
- offset
);
8757 if (consume_on_success
)
8758 zfree(vm_map_copy_zone
, copy
);
8759 return(KERN_SUCCESS
);
8763 * Check for special kernel buffer allocated
8764 * by new_ipc_kmsg_copyin.
8767 if (copy
->type
== VM_MAP_COPY_KERNEL_BUFFER
) {
8768 return vm_map_copyout_kernel_buffer(dst_map
, dst_addr
,
8770 consume_on_success
);
8775 * Find space for the data
8778 vm_copy_start
= vm_map_trunc_page((vm_map_size_t
)copy
->offset
,
8779 VM_MAP_COPY_PAGE_MASK(copy
));
8780 size
= vm_map_round_page((vm_map_size_t
)copy
->offset
+ copy
->size
,
8781 VM_MAP_COPY_PAGE_MASK(copy
))
8787 vm_map_lock(dst_map
);
8788 if( dst_map
->disable_vmentry_reuse
== TRUE
) {
8789 VM_MAP_HIGHEST_ENTRY(dst_map
, entry
, start
);
8792 if (dst_map
->holelistenabled
) {
8793 hole_entry
= (vm_map_entry_t
)dst_map
->holes_list
;
8795 if (hole_entry
== NULL
) {
8797 * No more space in the map?
8799 vm_map_unlock(dst_map
);
8800 return(KERN_NO_SPACE
);
8804 start
= last
->vme_start
;
8806 assert(first_free_is_valid(dst_map
));
8807 start
= ((last
= dst_map
->first_free
) == vm_map_to_entry(dst_map
)) ?
8808 vm_map_min(dst_map
) : last
->vme_end
;
8810 start
= vm_map_round_page(start
,
8811 VM_MAP_PAGE_MASK(dst_map
));
8815 vm_map_entry_t next
= last
->vme_next
;
8816 vm_map_offset_t end
= start
+ size
;
8818 if ((end
> dst_map
->max_offset
) || (end
< start
)) {
8819 if (dst_map
->wait_for_space
) {
8820 if (size
<= (dst_map
->max_offset
- dst_map
->min_offset
)) {
8821 assert_wait((event_t
) dst_map
,
8822 THREAD_INTERRUPTIBLE
);
8823 vm_map_unlock(dst_map
);
8824 thread_block(THREAD_CONTINUE_NULL
);
8828 vm_map_unlock(dst_map
);
8829 return(KERN_NO_SPACE
);
8832 if (dst_map
->holelistenabled
) {
8833 if (last
->vme_end
>= end
)
8837 * If there are no more entries, we must win.
8841 * If there is another entry, it must be
8842 * after the end of the potential new region.
8845 if (next
== vm_map_to_entry(dst_map
))
8848 if (next
->vme_start
>= end
)
8854 if (dst_map
->holelistenabled
) {
8855 if (last
== (vm_map_entry_t
) dst_map
->holes_list
) {
8859 vm_map_unlock(dst_map
);
8860 return(KERN_NO_SPACE
);
8862 start
= last
->vme_start
;
8864 start
= last
->vme_end
;
8866 start
= vm_map_round_page(start
,
8867 VM_MAP_PAGE_MASK(dst_map
));
8870 if (dst_map
->holelistenabled
) {
8871 if (vm_map_lookup_entry(dst_map
, last
->vme_start
, &last
)) {
8872 panic("Found an existing entry (%p) instead of potential hole at address: 0x%llx.\n", last
, (unsigned long long)last
->vme_start
);
8877 adjustment
= start
- vm_copy_start
;
8878 if (! consume_on_success
) {
8880 * We're not allowed to consume "copy", so we'll have to
8881 * copy its map entries into the destination map below.
8882 * No need to re-allocate map entries from the correct
8883 * (pageable or not) zone, since we'll get new map entries
8884 * during the transfer.
8885 * We'll also adjust the map entries's "start" and "end"
8886 * during the transfer, to keep "copy"'s entries consistent
8887 * with its "offset".
8889 goto after_adjustments
;
8893 * Since we're going to just drop the map
8894 * entries from the copy into the destination
8895 * map, they must come from the same pool.
8898 if (copy
->cpy_hdr
.entries_pageable
!= dst_map
->hdr
.entries_pageable
) {
8900 * Mismatches occur when dealing with the default
8904 vm_map_entry_t next
, new;
8907 * Find the zone that the copies were allocated from
8910 entry
= vm_map_copy_first_entry(copy
);
8913 * Reinitialize the copy so that vm_map_copy_entry_link
8916 vm_map_store_copy_reset(copy
, entry
);
8917 copy
->cpy_hdr
.entries_pageable
= dst_map
->hdr
.entries_pageable
;
8922 while (entry
!= vm_map_copy_to_entry(copy
)) {
8923 new = vm_map_copy_entry_create(copy
, !copy
->cpy_hdr
.entries_pageable
);
8924 vm_map_entry_copy_full(new, entry
);
8925 assert(!new->iokit_acct
);
8926 if (new->is_sub_map
) {
8927 /* clr address space specifics */
8928 new->use_pmap
= FALSE
;
8930 vm_map_copy_entry_link(copy
,
8931 vm_map_copy_last_entry(copy
),
8933 next
= entry
->vme_next
;
8934 old_zone
= entry
->from_reserved_zone
? vm_map_entry_reserved_zone
: vm_map_entry_zone
;
8935 zfree(old_zone
, entry
);
8941 * Adjust the addresses in the copy chain, and
8942 * reset the region attributes.
8945 for (entry
= vm_map_copy_first_entry(copy
);
8946 entry
!= vm_map_copy_to_entry(copy
);
8947 entry
= entry
->vme_next
) {
8948 if (VM_MAP_PAGE_SHIFT(dst_map
) == PAGE_SHIFT
) {
8950 * We're injecting this copy entry into a map that
8951 * has the standard page alignment, so clear
8952 * "map_aligned" (which might have been inherited
8953 * from the original map entry).
8955 entry
->map_aligned
= FALSE
;
8958 entry
->vme_start
+= adjustment
;
8959 entry
->vme_end
+= adjustment
;
8961 if (entry
->map_aligned
) {
8962 assert(VM_MAP_PAGE_ALIGNED(entry
->vme_start
,
8963 VM_MAP_PAGE_MASK(dst_map
)));
8964 assert(VM_MAP_PAGE_ALIGNED(entry
->vme_end
,
8965 VM_MAP_PAGE_MASK(dst_map
)));
8968 entry
->inheritance
= VM_INHERIT_DEFAULT
;
8969 entry
->protection
= VM_PROT_DEFAULT
;
8970 entry
->max_protection
= VM_PROT_ALL
;
8971 entry
->behavior
= VM_BEHAVIOR_DEFAULT
;
8974 * If the entry is now wired,
8975 * map the pages into the destination map.
8977 if (entry
->wired_count
!= 0) {
8978 register vm_map_offset_t va
;
8979 vm_object_offset_t offset
;
8980 register vm_object_t object
;
8984 object
= VME_OBJECT(entry
);
8985 offset
= VME_OFFSET(entry
);
8986 va
= entry
->vme_start
;
8988 pmap_pageable(dst_map
->pmap
,
8993 while (va
< entry
->vme_end
) {
8994 register vm_page_t m
;
8997 * Look up the page in the object.
8998 * Assert that the page will be found in the
9001 * the object was newly created by
9002 * vm_object_copy_slowly, and has
9003 * copies of all of the pages from
9006 * the object was moved from the old
9007 * map entry; because the old map
9008 * entry was wired, all of the pages
9009 * were in the top-level object.
9010 * (XXX not true if we wire pages for
9013 vm_object_lock(object
);
9015 m
= vm_page_lookup(object
, offset
);
9016 if (m
== VM_PAGE_NULL
|| !VM_PAGE_WIRED(m
) ||
9018 panic("vm_map_copyout: wiring %p", m
);
9022 * The page is assumed to be wired here, so it
9023 * shouldn't be encrypted. Otherwise, we
9024 * couldn't enter it in the page table, since
9025 * we don't want the user to see the encrypted
9028 ASSERT_PAGE_DECRYPTED(m
);
9030 prot
= entry
->protection
;
9032 if (override_nx(dst_map
, VME_ALIAS(entry
)) &&
9034 prot
|= VM_PROT_EXECUTE
;
9036 type_of_fault
= DBG_CACHE_HIT_FAULT
;
9038 vm_fault_enter(m
, dst_map
->pmap
, va
, prot
, prot
,
9039 VM_PAGE_WIRED(m
), FALSE
, FALSE
,
9040 FALSE
, VME_ALIAS(entry
),
9041 ((entry
->iokit_acct
||
9042 (!entry
->is_sub_map
&&
9044 ? PMAP_OPTIONS_ALT_ACCT
9046 NULL
, &type_of_fault
);
9048 vm_object_unlock(object
);
9050 offset
+= PAGE_SIZE_64
;
9059 * Correct the page alignment for the result
9062 *dst_addr
= start
+ (copy
->offset
- vm_copy_start
);
9065 * Update the hints and the map size
9068 if (consume_on_success
) {
9069 SAVE_HINT_MAP_WRITE(dst_map
, vm_map_copy_last_entry(copy
));
9071 SAVE_HINT_MAP_WRITE(dst_map
, last
);
9074 dst_map
->size
+= size
;
9080 if (consume_on_success
) {
9081 vm_map_copy_insert(dst_map
, last
, copy
);
9083 vm_map_copy_remap(dst_map
, last
, copy
, adjustment
,
9084 cur_protection
, max_protection
,
9088 vm_map_unlock(dst_map
);
9091 * XXX If wiring_required, call vm_map_pageable
9094 return(KERN_SUCCESS
);
9098 * Routine: vm_map_copyin
9101 * see vm_map_copyin_common. Exported via Unsupported.exports.
9105 #undef vm_map_copyin
9110 vm_map_address_t src_addr
,
9112 boolean_t src_destroy
,
9113 vm_map_copy_t
*copy_result
) /* OUT */
9115 return(vm_map_copyin_common(src_map
, src_addr
, len
, src_destroy
,
9116 FALSE
, copy_result
, FALSE
));
9120 * Routine: vm_map_copyin_common
9123 * Copy the specified region (src_addr, len) from the
9124 * source address space (src_map), possibly removing
9125 * the region from the source address space (src_destroy).
9128 * A vm_map_copy_t object (copy_result), suitable for
9129 * insertion into another address space (using vm_map_copyout),
9130 * copying over another address space region (using
9131 * vm_map_copy_overwrite). If the copy is unused, it
9132 * should be destroyed (using vm_map_copy_discard).
9134 * In/out conditions:
9135 * The source map should not be locked on entry.
9138 typedef struct submap_map
{
9139 vm_map_t parent_map
;
9140 vm_map_offset_t base_start
;
9141 vm_map_offset_t base_end
;
9142 vm_map_size_t base_len
;
9143 struct submap_map
*next
;
9147 vm_map_copyin_common(
9149 vm_map_address_t src_addr
,
9151 boolean_t src_destroy
,
9152 __unused boolean_t src_volatile
,
9153 vm_map_copy_t
*copy_result
, /* OUT */
9154 boolean_t use_maxprot
)
9156 vm_map_entry_t tmp_entry
; /* Result of last map lookup --
9157 * in multi-level lookup, this
9158 * entry contains the actual
9162 vm_map_entry_t new_entry
= VM_MAP_ENTRY_NULL
; /* Map entry for copy */
9164 vm_map_offset_t src_start
; /* Start of current entry --
9165 * where copy is taking place now
9167 vm_map_offset_t src_end
; /* End of entire region to be
9169 vm_map_offset_t src_base
;
9170 vm_map_t base_map
= src_map
;
9171 boolean_t map_share
=FALSE
;
9172 submap_map_t
*parent_maps
= NULL
;
9175 vm_map_copy_t copy
; /* Resulting copy */
9176 vm_map_address_t copy_addr
;
9177 vm_map_size_t copy_size
;
9180 * Check for copies of zero bytes.
9184 *copy_result
= VM_MAP_COPY_NULL
;
9185 return(KERN_SUCCESS
);
9189 * Check that the end address doesn't overflow
9191 src_end
= src_addr
+ len
;
9192 if (src_end
< src_addr
)
9193 return KERN_INVALID_ADDRESS
;
9196 * If the copy is sufficiently small, use a kernel buffer instead
9197 * of making a virtual copy. The theory being that the cost of
9198 * setting up VM (and taking C-O-W faults) dominates the copy costs
9199 * for small regions.
9201 if ((len
< msg_ool_size_small
) && !use_maxprot
)
9202 return vm_map_copyin_kernel_buffer(src_map
, src_addr
, len
,
9203 src_destroy
, copy_result
);
9206 * Compute (page aligned) start and end of region
9208 src_start
= vm_map_trunc_page(src_addr
,
9209 VM_MAP_PAGE_MASK(src_map
));
9210 src_end
= vm_map_round_page(src_end
,
9211 VM_MAP_PAGE_MASK(src_map
));
9213 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);
9216 * Allocate a header element for the list.
9218 * Use the start and end in the header to
9219 * remember the endpoints prior to rounding.
9222 copy
= (vm_map_copy_t
) zalloc(vm_map_copy_zone
);
9223 copy
->c_u
.hdr
.rb_head_store
.rbh_root
= (void*)(int)SKIP_RB_TREE
;
9224 vm_map_copy_first_entry(copy
) =
9225 vm_map_copy_last_entry(copy
) = vm_map_copy_to_entry(copy
);
9226 copy
->type
= VM_MAP_COPY_ENTRY_LIST
;
9227 copy
->cpy_hdr
.nentries
= 0;
9228 copy
->cpy_hdr
.entries_pageable
= TRUE
;
9230 copy
->cpy_hdr
.page_shift
= src_map
->hdr
.page_shift
;
9233 * The copy entries can be broken down for a variety of reasons,
9234 * so we can't guarantee that they will remain map-aligned...
9235 * Will need to adjust the first copy_entry's "vme_start" and
9236 * the last copy_entry's "vme_end" to be rounded to PAGE_MASK
9237 * rather than the original map's alignment.
9239 copy
->cpy_hdr
.page_shift
= PAGE_SHIFT
;
9242 vm_map_store_init( &(copy
->cpy_hdr
) );
9244 copy
->offset
= src_addr
;
9247 new_entry
= vm_map_copy_entry_create(copy
, !copy
->cpy_hdr
.entries_pageable
);
9251 vm_map_unlock(src_map); \
9252 if(src_map != base_map) \
9253 vm_map_deallocate(src_map); \
9254 if (new_entry != VM_MAP_ENTRY_NULL) \
9255 vm_map_copy_entry_dispose(copy,new_entry); \
9256 vm_map_copy_discard(copy); \
9258 submap_map_t *_ptr; \
9260 for(_ptr = parent_maps; _ptr != NULL; _ptr = parent_maps) { \
9261 parent_maps=parent_maps->next; \
9262 if (_ptr->parent_map != base_map) \
9263 vm_map_deallocate(_ptr->parent_map); \
9264 kfree(_ptr, sizeof(submap_map_t)); \
9271 * Find the beginning of the region.
9274 vm_map_lock(src_map
);
9277 * Lookup the original "src_addr" rather than the truncated
9278 * "src_start", in case "src_start" falls in a non-map-aligned
9279 * map entry *before* the map entry that contains "src_addr"...
9281 if (!vm_map_lookup_entry(src_map
, src_addr
, &tmp_entry
))
9282 RETURN(KERN_INVALID_ADDRESS
);
9283 if(!tmp_entry
->is_sub_map
) {
9285 * ... but clip to the map-rounded "src_start" rather than
9286 * "src_addr" to preserve map-alignment. We'll adjust the
9287 * first copy entry at the end, if needed.
9289 vm_map_clip_start(src_map
, tmp_entry
, src_start
);
9291 if (src_start
< tmp_entry
->vme_start
) {
9293 * Move "src_start" up to the start of the
9294 * first map entry to copy.
9296 src_start
= tmp_entry
->vme_start
;
9298 /* set for later submap fix-up */
9299 copy_addr
= src_start
;
9302 * Go through entries until we get to the end.
9307 vm_map_entry_t src_entry
= tmp_entry
; /* Top-level entry */
9308 vm_map_size_t src_size
; /* Size of source
9309 * map entry (in both
9314 vm_object_t src_object
; /* Object to copy */
9315 vm_object_offset_t src_offset
;
9317 boolean_t src_needs_copy
; /* Should source map
9319 * for copy-on-write?
9322 boolean_t new_entry_needs_copy
; /* Will new entry be COW? */
9324 boolean_t was_wired
; /* Was source wired? */
9325 vm_map_version_t version
; /* Version before locks
9326 * dropped to make copy
9328 kern_return_t result
; /* Return value from
9329 * copy_strategically.
9331 while(tmp_entry
->is_sub_map
) {
9332 vm_map_size_t submap_len
;
9335 ptr
= (submap_map_t
*)kalloc(sizeof(submap_map_t
));
9336 ptr
->next
= parent_maps
;
9338 ptr
->parent_map
= src_map
;
9339 ptr
->base_start
= src_start
;
9340 ptr
->base_end
= src_end
;
9341 submap_len
= tmp_entry
->vme_end
- src_start
;
9342 if(submap_len
> (src_end
-src_start
))
9343 submap_len
= src_end
-src_start
;
9344 ptr
->base_len
= submap_len
;
9346 src_start
-= tmp_entry
->vme_start
;
9347 src_start
+= VME_OFFSET(tmp_entry
);
9348 src_end
= src_start
+ submap_len
;
9349 src_map
= VME_SUBMAP(tmp_entry
);
9350 vm_map_lock(src_map
);
9351 /* keep an outstanding reference for all maps in */
9352 /* the parents tree except the base map */
9353 vm_map_reference(src_map
);
9354 vm_map_unlock(ptr
->parent_map
);
9355 if (!vm_map_lookup_entry(
9356 src_map
, src_start
, &tmp_entry
))
9357 RETURN(KERN_INVALID_ADDRESS
);
9359 if(!tmp_entry
->is_sub_map
)
9360 vm_map_clip_start(src_map
, tmp_entry
, src_start
);
9361 src_entry
= tmp_entry
;
9363 /* we are now in the lowest level submap... */
9365 if ((VME_OBJECT(tmp_entry
) != VM_OBJECT_NULL
) &&
9366 (VME_OBJECT(tmp_entry
)->phys_contiguous
)) {
9367 /* This is not, supported for now.In future */
9368 /* we will need to detect the phys_contig */
9369 /* condition and then upgrade copy_slowly */
9370 /* to do physical copy from the device mem */
9371 /* based object. We can piggy-back off of */
9372 /* the was wired boolean to set-up the */
9373 /* proper handling */
9374 RETURN(KERN_PROTECTION_FAILURE
);
9377 * Create a new address map entry to hold the result.
9378 * Fill in the fields from the appropriate source entries.
9379 * We must unlock the source map to do this if we need
9380 * to allocate a map entry.
9382 if (new_entry
== VM_MAP_ENTRY_NULL
) {
9383 version
.main_timestamp
= src_map
->timestamp
;
9384 vm_map_unlock(src_map
);
9386 new_entry
= vm_map_copy_entry_create(copy
, !copy
->cpy_hdr
.entries_pageable
);
9388 vm_map_lock(src_map
);
9389 if ((version
.main_timestamp
+ 1) != src_map
->timestamp
) {
9390 if (!vm_map_lookup_entry(src_map
, src_start
,
9392 RETURN(KERN_INVALID_ADDRESS
);
9394 if (!tmp_entry
->is_sub_map
)
9395 vm_map_clip_start(src_map
, tmp_entry
, src_start
);
9396 continue; /* restart w/ new tmp_entry */
9401 * Verify that the region can be read.
9403 if (((src_entry
->protection
& VM_PROT_READ
) == VM_PROT_NONE
&&
9405 (src_entry
->max_protection
& VM_PROT_READ
) == 0)
9406 RETURN(KERN_PROTECTION_FAILURE
);
9409 * Clip against the endpoints of the entire region.
9412 vm_map_clip_end(src_map
, src_entry
, src_end
);
9414 src_size
= src_entry
->vme_end
- src_start
;
9415 src_object
= VME_OBJECT(src_entry
);
9416 src_offset
= VME_OFFSET(src_entry
);
9417 was_wired
= (src_entry
->wired_count
!= 0);
9419 vm_map_entry_copy(new_entry
, src_entry
);
9420 if (new_entry
->is_sub_map
) {
9421 /* clr address space specifics */
9422 new_entry
->use_pmap
= FALSE
;
9426 * Attempt non-blocking copy-on-write optimizations.
9430 (src_object
== VM_OBJECT_NULL
||
9431 (src_object
->internal
&& !src_object
->true_share
9434 * If we are destroying the source, and the object
9435 * is internal, we can move the object reference
9436 * from the source to the copy. The copy is
9437 * copy-on-write only if the source is.
9438 * We make another reference to the object, because
9439 * destroying the source entry will deallocate it.
9441 vm_object_reference(src_object
);
9444 * Copy is always unwired. vm_map_copy_entry
9445 * set its wired count to zero.
9448 goto CopySuccessful
;
9453 XPR(XPR_VM_MAP
, "vm_map_copyin_common src_obj 0x%x ent 0x%x obj 0x%x was_wired %d\n",
9454 src_object
, new_entry
, VME_OBJECT(new_entry
),
9456 if ((src_object
== VM_OBJECT_NULL
||
9457 (!was_wired
&& !map_share
&& !tmp_entry
->is_shared
)) &&
9458 vm_object_copy_quickly(
9459 &VME_OBJECT(new_entry
),
9463 &new_entry_needs_copy
)) {
9465 new_entry
->needs_copy
= new_entry_needs_copy
;
9468 * Handle copy-on-write obligations
9471 if (src_needs_copy
&& !tmp_entry
->needs_copy
) {
9474 prot
= src_entry
->protection
& ~VM_PROT_WRITE
;
9476 if (override_nx(src_map
, VME_ALIAS(src_entry
))
9478 prot
|= VM_PROT_EXECUTE
;
9480 vm_object_pmap_protect(
9484 (src_entry
->is_shared
?
9487 src_entry
->vme_start
,
9490 assert(tmp_entry
->wired_count
== 0);
9491 tmp_entry
->needs_copy
= TRUE
;
9495 * The map has never been unlocked, so it's safe
9496 * to move to the next entry rather than doing
9500 goto CopySuccessful
;
9504 * Take an object reference, so that we may
9505 * release the map lock(s).
9508 assert(src_object
!= VM_OBJECT_NULL
);
9509 vm_object_reference(src_object
);
9512 * Record the timestamp for later verification.
9516 version
.main_timestamp
= src_map
->timestamp
;
9517 vm_map_unlock(src_map
); /* Increments timestamp once! */
9525 vm_object_lock(src_object
);
9526 result
= vm_object_copy_slowly(
9531 &VME_OBJECT(new_entry
));
9532 VME_OFFSET_SET(new_entry
, 0);
9533 new_entry
->needs_copy
= FALSE
;
9536 else if (src_object
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
&&
9537 (tmp_entry
->is_shared
|| map_share
)) {
9538 vm_object_t new_object
;
9540 vm_object_lock_shared(src_object
);
9541 new_object
= vm_object_copy_delayed(
9546 if (new_object
== VM_OBJECT_NULL
)
9549 VME_OBJECT_SET(new_entry
, new_object
);
9550 assert(new_entry
->wired_count
== 0);
9551 new_entry
->needs_copy
= TRUE
;
9552 assert(!new_entry
->iokit_acct
);
9553 assert(new_object
->purgable
== VM_PURGABLE_DENY
);
9554 new_entry
->use_pmap
= TRUE
;
9555 result
= KERN_SUCCESS
;
9558 vm_object_offset_t new_offset
;
9559 new_offset
= VME_OFFSET(new_entry
);
9560 result
= vm_object_copy_strategically(src_object
,
9563 &VME_OBJECT(new_entry
),
9565 &new_entry_needs_copy
);
9566 if (new_offset
!= VME_OFFSET(new_entry
)) {
9567 VME_OFFSET_SET(new_entry
, new_offset
);
9570 new_entry
->needs_copy
= new_entry_needs_copy
;
9573 if (result
!= KERN_SUCCESS
&&
9574 result
!= KERN_MEMORY_RESTART_COPY
) {
9575 vm_map_lock(src_map
);
9580 * Throw away the extra reference
9583 vm_object_deallocate(src_object
);
9586 * Verify that the map has not substantially
9587 * changed while the copy was being made.
9590 vm_map_lock(src_map
);
9592 if ((version
.main_timestamp
+ 1) == src_map
->timestamp
)
9593 goto VerificationSuccessful
;
9596 * Simple version comparison failed.
9598 * Retry the lookup and verify that the
9599 * same object/offset are still present.
9601 * [Note: a memory manager that colludes with
9602 * the calling task can detect that we have
9603 * cheated. While the map was unlocked, the
9604 * mapping could have been changed and restored.]
9607 if (!vm_map_lookup_entry(src_map
, src_start
, &tmp_entry
)) {
9608 if (result
!= KERN_MEMORY_RESTART_COPY
) {
9609 vm_object_deallocate(VME_OBJECT(new_entry
));
9610 VME_OBJECT_SET(new_entry
, VM_OBJECT_NULL
);
9611 assert(!new_entry
->iokit_acct
);
9612 new_entry
->use_pmap
= TRUE
;
9614 RETURN(KERN_INVALID_ADDRESS
);
9617 src_entry
= tmp_entry
;
9618 vm_map_clip_start(src_map
, src_entry
, src_start
);
9620 if ((((src_entry
->protection
& VM_PROT_READ
) == VM_PROT_NONE
) &&
9622 ((src_entry
->max_protection
& VM_PROT_READ
) == 0))
9623 goto VerificationFailed
;
9625 if (src_entry
->vme_end
< new_entry
->vme_end
) {
9626 assert(VM_MAP_PAGE_ALIGNED(src_entry
->vme_end
,
9627 VM_MAP_COPY_PAGE_MASK(copy
)));
9628 new_entry
->vme_end
= src_entry
->vme_end
;
9629 src_size
= new_entry
->vme_end
- src_start
;
9632 if ((VME_OBJECT(src_entry
) != src_object
) ||
9633 (VME_OFFSET(src_entry
) != src_offset
) ) {
9636 * Verification failed.
9638 * Start over with this top-level entry.
9641 VerificationFailed
: ;
9643 vm_object_deallocate(VME_OBJECT(new_entry
));
9644 tmp_entry
= src_entry
;
9649 * Verification succeeded.
9652 VerificationSuccessful
: ;
9654 if (result
== KERN_MEMORY_RESTART_COPY
)
9664 * Link in the new copy entry.
9667 vm_map_copy_entry_link(copy
, vm_map_copy_last_entry(copy
),
9671 * Determine whether the entire region
9674 src_base
= src_start
;
9675 src_start
= new_entry
->vme_end
;
9676 new_entry
= VM_MAP_ENTRY_NULL
;
9677 while ((src_start
>= src_end
) && (src_end
!= 0)) {
9680 if (src_map
== base_map
) {
9681 /* back to the top */
9686 assert(ptr
!= NULL
);
9687 parent_maps
= parent_maps
->next
;
9689 /* fix up the damage we did in that submap */
9690 vm_map_simplify_range(src_map
,
9694 vm_map_unlock(src_map
);
9695 vm_map_deallocate(src_map
);
9696 vm_map_lock(ptr
->parent_map
);
9697 src_map
= ptr
->parent_map
;
9698 src_base
= ptr
->base_start
;
9699 src_start
= ptr
->base_start
+ ptr
->base_len
;
9700 src_end
= ptr
->base_end
;
9701 if (!vm_map_lookup_entry(src_map
,
9704 (src_end
> src_start
)) {
9705 RETURN(KERN_INVALID_ADDRESS
);
9707 kfree(ptr
, sizeof(submap_map_t
));
9708 if (parent_maps
== NULL
)
9710 src_entry
= tmp_entry
->vme_prev
;
9713 if ((VM_MAP_PAGE_SHIFT(src_map
) != PAGE_SHIFT
) &&
9714 (src_start
>= src_addr
+ len
) &&
9715 (src_addr
+ len
!= 0)) {
9717 * Stop copying now, even though we haven't reached
9718 * "src_end". We'll adjust the end of the last copy
9719 * entry at the end, if needed.
9721 * If src_map's aligment is different from the
9722 * system's page-alignment, there could be
9723 * extra non-map-aligned map entries between
9724 * the original (non-rounded) "src_addr + len"
9725 * and the rounded "src_end".
9726 * We do not want to copy those map entries since
9727 * they're not part of the copied range.
9732 if ((src_start
>= src_end
) && (src_end
!= 0))
9736 * Verify that there are no gaps in the region
9739 tmp_entry
= src_entry
->vme_next
;
9740 if ((tmp_entry
->vme_start
!= src_start
) ||
9741 (tmp_entry
== vm_map_to_entry(src_map
))) {
9742 RETURN(KERN_INVALID_ADDRESS
);
9747 * If the source should be destroyed, do it now, since the
9748 * copy was successful.
9751 (void) vm_map_delete(
9753 vm_map_trunc_page(src_addr
,
9754 VM_MAP_PAGE_MASK(src_map
)),
9756 ((src_map
== kernel_map
) ?
9757 VM_MAP_REMOVE_KUNWIRE
:
9761 /* fix up the damage we did in the base map */
9762 vm_map_simplify_range(
9764 vm_map_trunc_page(src_addr
,
9765 VM_MAP_PAGE_MASK(src_map
)),
9766 vm_map_round_page(src_end
,
9767 VM_MAP_PAGE_MASK(src_map
)));
9770 vm_map_unlock(src_map
);
9772 if (VM_MAP_PAGE_SHIFT(src_map
) != PAGE_SHIFT
) {
9773 vm_map_offset_t original_start
, original_offset
, original_end
;
9775 assert(VM_MAP_COPY_PAGE_MASK(copy
) == PAGE_MASK
);
9777 /* adjust alignment of first copy_entry's "vme_start" */
9778 tmp_entry
= vm_map_copy_first_entry(copy
);
9779 if (tmp_entry
!= vm_map_copy_to_entry(copy
)) {
9780 vm_map_offset_t adjustment
;
9782 original_start
= tmp_entry
->vme_start
;
9783 original_offset
= VME_OFFSET(tmp_entry
);
9785 /* map-align the start of the first copy entry... */
9786 adjustment
= (tmp_entry
->vme_start
-
9788 tmp_entry
->vme_start
,
9789 VM_MAP_PAGE_MASK(src_map
)));
9790 tmp_entry
->vme_start
-= adjustment
;
9791 VME_OFFSET_SET(tmp_entry
,
9792 VME_OFFSET(tmp_entry
) - adjustment
);
9793 copy_addr
-= adjustment
;
9794 assert(tmp_entry
->vme_start
< tmp_entry
->vme_end
);
9795 /* ... adjust for mis-aligned start of copy range */
9797 (vm_map_trunc_page(copy
->offset
,
9799 vm_map_trunc_page(copy
->offset
,
9800 VM_MAP_PAGE_MASK(src_map
)));
9802 assert(page_aligned(adjustment
));
9803 assert(adjustment
< VM_MAP_PAGE_SIZE(src_map
));
9804 tmp_entry
->vme_start
+= adjustment
;
9805 VME_OFFSET_SET(tmp_entry
,
9806 (VME_OFFSET(tmp_entry
) +
9808 copy_addr
+= adjustment
;
9809 assert(tmp_entry
->vme_start
< tmp_entry
->vme_end
);
9813 * Assert that the adjustments haven't exposed
9814 * more than was originally copied...
9816 assert(tmp_entry
->vme_start
>= original_start
);
9817 assert(VME_OFFSET(tmp_entry
) >= original_offset
);
9819 * ... and that it did not adjust outside of a
9820 * a single 16K page.
9822 assert(vm_map_trunc_page(tmp_entry
->vme_start
,
9823 VM_MAP_PAGE_MASK(src_map
)) ==
9824 vm_map_trunc_page(original_start
,
9825 VM_MAP_PAGE_MASK(src_map
)));
9828 /* adjust alignment of last copy_entry's "vme_end" */
9829 tmp_entry
= vm_map_copy_last_entry(copy
);
9830 if (tmp_entry
!= vm_map_copy_to_entry(copy
)) {
9831 vm_map_offset_t adjustment
;
9833 original_end
= tmp_entry
->vme_end
;
9835 /* map-align the end of the last copy entry... */
9836 tmp_entry
->vme_end
=
9837 vm_map_round_page(tmp_entry
->vme_end
,
9838 VM_MAP_PAGE_MASK(src_map
));
9839 /* ... adjust for mis-aligned end of copy range */
9841 (vm_map_round_page((copy
->offset
+
9843 VM_MAP_PAGE_MASK(src_map
)) -
9844 vm_map_round_page((copy
->offset
+
9848 assert(page_aligned(adjustment
));
9849 assert(adjustment
< VM_MAP_PAGE_SIZE(src_map
));
9850 tmp_entry
->vme_end
-= adjustment
;
9851 assert(tmp_entry
->vme_start
< tmp_entry
->vme_end
);
9855 * Assert that the adjustments haven't exposed
9856 * more than was originally copied...
9858 assert(tmp_entry
->vme_end
<= original_end
);
9860 * ... and that it did not adjust outside of a
9861 * a single 16K page.
9863 assert(vm_map_round_page(tmp_entry
->vme_end
,
9864 VM_MAP_PAGE_MASK(src_map
)) ==
9865 vm_map_round_page(original_end
,
9866 VM_MAP_PAGE_MASK(src_map
)));
9870 /* Fix-up start and end points in copy. This is necessary */
9871 /* when the various entries in the copy object were picked */
9872 /* up from different sub-maps */
9874 tmp_entry
= vm_map_copy_first_entry(copy
);
9875 copy_size
= 0; /* compute actual size */
9876 while (tmp_entry
!= vm_map_copy_to_entry(copy
)) {
9877 assert(VM_MAP_PAGE_ALIGNED(
9878 copy_addr
+ (tmp_entry
->vme_end
-
9879 tmp_entry
->vme_start
),
9880 VM_MAP_COPY_PAGE_MASK(copy
)));
9881 assert(VM_MAP_PAGE_ALIGNED(
9883 VM_MAP_COPY_PAGE_MASK(copy
)));
9886 * The copy_entries will be injected directly into the
9887 * destination map and might not be "map aligned" there...
9889 tmp_entry
->map_aligned
= FALSE
;
9891 tmp_entry
->vme_end
= copy_addr
+
9892 (tmp_entry
->vme_end
- tmp_entry
->vme_start
);
9893 tmp_entry
->vme_start
= copy_addr
;
9894 assert(tmp_entry
->vme_start
< tmp_entry
->vme_end
);
9895 copy_addr
+= tmp_entry
->vme_end
- tmp_entry
->vme_start
;
9896 copy_size
+= tmp_entry
->vme_end
- tmp_entry
->vme_start
;
9897 tmp_entry
= (struct vm_map_entry
*)tmp_entry
->vme_next
;
9900 if (VM_MAP_PAGE_SHIFT(src_map
) != PAGE_SHIFT
&&
9901 copy_size
< copy
->size
) {
9903 * The actual size of the VM map copy is smaller than what
9904 * was requested by the caller. This must be because some
9905 * PAGE_SIZE-sized pages are missing at the end of the last
9906 * VM_MAP_PAGE_SIZE(src_map)-sized chunk of the range.
9907 * The caller might not have been aware of those missing
9908 * pages and might not want to be aware of it, which is
9909 * fine as long as they don't try to access (and crash on)
9910 * those missing pages.
9911 * Let's adjust the size of the "copy", to avoid failing
9912 * in vm_map_copyout() or vm_map_copy_overwrite().
9914 assert(vm_map_round_page(copy_size
,
9915 VM_MAP_PAGE_MASK(src_map
)) ==
9916 vm_map_round_page(copy
->size
,
9917 VM_MAP_PAGE_MASK(src_map
)));
9918 copy
->size
= copy_size
;
9921 *copy_result
= copy
;
9922 return(KERN_SUCCESS
);
9928 vm_map_copy_extract(
9930 vm_map_address_t src_addr
,
9932 vm_map_copy_t
*copy_result
, /* OUT */
9933 vm_prot_t
*cur_prot
, /* OUT */
9934 vm_prot_t
*max_prot
)
9936 vm_map_offset_t src_start
, src_end
;
9941 * Check for copies of zero bytes.
9945 *copy_result
= VM_MAP_COPY_NULL
;
9946 return(KERN_SUCCESS
);
9950 * Check that the end address doesn't overflow
9952 src_end
= src_addr
+ len
;
9953 if (src_end
< src_addr
)
9954 return KERN_INVALID_ADDRESS
;
9957 * Compute (page aligned) start and end of region
9959 src_start
= vm_map_trunc_page(src_addr
, PAGE_MASK
);
9960 src_end
= vm_map_round_page(src_end
, PAGE_MASK
);
9963 * Allocate a header element for the list.
9965 * Use the start and end in the header to
9966 * remember the endpoints prior to rounding.
9969 copy
= (vm_map_copy_t
) zalloc(vm_map_copy_zone
);
9970 copy
->c_u
.hdr
.rb_head_store
.rbh_root
= (void*)(int)SKIP_RB_TREE
;
9971 vm_map_copy_first_entry(copy
) =
9972 vm_map_copy_last_entry(copy
) = vm_map_copy_to_entry(copy
);
9973 copy
->type
= VM_MAP_COPY_ENTRY_LIST
;
9974 copy
->cpy_hdr
.nentries
= 0;
9975 copy
->cpy_hdr
.entries_pageable
= TRUE
;
9977 vm_map_store_init(©
->cpy_hdr
);
9982 kr
= vm_map_remap_extract(src_map
,
9990 TRUE
); /* pageable */
9991 if (kr
!= KERN_SUCCESS
) {
9992 vm_map_copy_discard(copy
);
9996 *copy_result
= copy
;
9997 return KERN_SUCCESS
;
10001 * vm_map_copyin_object:
10003 * Create a copy object from an object.
10004 * Our caller donates an object reference.
10008 vm_map_copyin_object(
10009 vm_object_t object
,
10010 vm_object_offset_t offset
, /* offset of region in object */
10011 vm_object_size_t size
, /* size of region in object */
10012 vm_map_copy_t
*copy_result
) /* OUT */
10014 vm_map_copy_t copy
; /* Resulting copy */
10017 * We drop the object into a special copy object
10018 * that contains the object directly.
10021 copy
= (vm_map_copy_t
) zalloc(vm_map_copy_zone
);
10022 copy
->c_u
.hdr
.rb_head_store
.rbh_root
= (void*)(int)SKIP_RB_TREE
;
10023 copy
->type
= VM_MAP_COPY_OBJECT
;
10024 copy
->cpy_object
= object
;
10025 copy
->offset
= offset
;
10028 *copy_result
= copy
;
10029 return(KERN_SUCCESS
);
10035 vm_map_entry_t old_entry
,
10038 vm_object_t object
;
10039 vm_map_entry_t new_entry
;
10042 * New sharing code. New map entry
10043 * references original object. Internal
10044 * objects use asynchronous copy algorithm for
10045 * future copies. First make sure we have
10046 * the right object. If we need a shadow,
10047 * or someone else already has one, then
10048 * make a new shadow and share it.
10051 object
= VME_OBJECT(old_entry
);
10052 if (old_entry
->is_sub_map
) {
10053 assert(old_entry
->wired_count
== 0);
10054 #ifndef NO_NESTED_PMAP
10055 if(old_entry
->use_pmap
) {
10056 kern_return_t result
;
10058 result
= pmap_nest(new_map
->pmap
,
10059 (VME_SUBMAP(old_entry
))->pmap
,
10060 (addr64_t
)old_entry
->vme_start
,
10061 (addr64_t
)old_entry
->vme_start
,
10062 (uint64_t)(old_entry
->vme_end
- old_entry
->vme_start
));
10064 panic("vm_map_fork_share: pmap_nest failed!");
10066 #endif /* NO_NESTED_PMAP */
10067 } else if (object
== VM_OBJECT_NULL
) {
10068 object
= vm_object_allocate((vm_map_size_t
)(old_entry
->vme_end
-
10069 old_entry
->vme_start
));
10070 VME_OFFSET_SET(old_entry
, 0);
10071 VME_OBJECT_SET(old_entry
, object
);
10072 old_entry
->use_pmap
= TRUE
;
10073 assert(!old_entry
->needs_copy
);
10074 } else if (object
->copy_strategy
!=
10075 MEMORY_OBJECT_COPY_SYMMETRIC
) {
10078 * We are already using an asymmetric
10079 * copy, and therefore we already have
10080 * the right object.
10083 assert(! old_entry
->needs_copy
);
10085 else if (old_entry
->needs_copy
|| /* case 1 */
10086 object
->shadowed
|| /* case 2 */
10087 (!object
->true_share
&& /* case 3 */
10088 !old_entry
->is_shared
&&
10090 (vm_map_size_t
)(old_entry
->vme_end
-
10091 old_entry
->vme_start
)))) {
10094 * We need to create a shadow.
10095 * There are three cases here.
10096 * In the first case, we need to
10097 * complete a deferred symmetrical
10098 * copy that we participated in.
10099 * In the second and third cases,
10100 * we need to create the shadow so
10101 * that changes that we make to the
10102 * object do not interfere with
10103 * any symmetrical copies which
10104 * have occured (case 2) or which
10105 * might occur (case 3).
10107 * The first case is when we had
10108 * deferred shadow object creation
10109 * via the entry->needs_copy mechanism.
10110 * This mechanism only works when
10111 * only one entry points to the source
10112 * object, and we are about to create
10113 * a second entry pointing to the
10114 * same object. The problem is that
10115 * there is no way of mapping from
10116 * an object to the entries pointing
10117 * to it. (Deferred shadow creation
10118 * works with one entry because occurs
10119 * at fault time, and we walk from the
10120 * entry to the object when handling
10123 * The second case is when the object
10124 * to be shared has already been copied
10125 * with a symmetric copy, but we point
10126 * directly to the object without
10127 * needs_copy set in our entry. (This
10128 * can happen because different ranges
10129 * of an object can be pointed to by
10130 * different entries. In particular,
10131 * a single entry pointing to an object
10132 * can be split by a call to vm_inherit,
10133 * which, combined with task_create, can
10134 * result in the different entries
10135 * having different needs_copy values.)
10136 * The shadowed flag in the object allows
10137 * us to detect this case. The problem
10138 * with this case is that if this object
10139 * has or will have shadows, then we
10140 * must not perform an asymmetric copy
10141 * of this object, since such a copy
10142 * allows the object to be changed, which
10143 * will break the previous symmetrical
10144 * copies (which rely upon the object
10145 * not changing). In a sense, the shadowed
10146 * flag says "don't change this object".
10147 * We fix this by creating a shadow
10148 * object for this object, and sharing
10149 * that. This works because we are free
10150 * to change the shadow object (and thus
10151 * to use an asymmetric copy strategy);
10152 * this is also semantically correct,
10153 * since this object is temporary, and
10154 * therefore a copy of the object is
10155 * as good as the object itself. (This
10156 * is not true for permanent objects,
10157 * since the pager needs to see changes,
10158 * which won't happen if the changes
10159 * are made to a copy.)
10161 * The third case is when the object
10162 * to be shared has parts sticking
10163 * outside of the entry we're working
10164 * with, and thus may in the future
10165 * be subject to a symmetrical copy.
10166 * (This is a preemptive version of
10169 VME_OBJECT_SHADOW(old_entry
,
10170 (vm_map_size_t
) (old_entry
->vme_end
-
10171 old_entry
->vme_start
));
10174 * If we're making a shadow for other than
10175 * copy on write reasons, then we have
10176 * to remove write permission.
10179 if (!old_entry
->needs_copy
&&
10180 (old_entry
->protection
& VM_PROT_WRITE
)) {
10183 prot
= old_entry
->protection
& ~VM_PROT_WRITE
;
10185 if (override_nx(old_map
, VME_ALIAS(old_entry
)) && prot
)
10186 prot
|= VM_PROT_EXECUTE
;
10188 if (old_map
->mapped_in_other_pmaps
) {
10189 vm_object_pmap_protect(
10190 VME_OBJECT(old_entry
),
10191 VME_OFFSET(old_entry
),
10192 (old_entry
->vme_end
-
10193 old_entry
->vme_start
),
10195 old_entry
->vme_start
,
10198 pmap_protect(old_map
->pmap
,
10199 old_entry
->vme_start
,
10200 old_entry
->vme_end
,
10205 old_entry
->needs_copy
= FALSE
;
10206 object
= VME_OBJECT(old_entry
);
10211 * If object was using a symmetric copy strategy,
10212 * change its copy strategy to the default
10213 * asymmetric copy strategy, which is copy_delay
10214 * in the non-norma case and copy_call in the
10215 * norma case. Bump the reference count for the
10219 if(old_entry
->is_sub_map
) {
10220 vm_map_lock(VME_SUBMAP(old_entry
));
10221 vm_map_reference(VME_SUBMAP(old_entry
));
10222 vm_map_unlock(VME_SUBMAP(old_entry
));
10224 vm_object_lock(object
);
10225 vm_object_reference_locked(object
);
10226 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
) {
10227 object
->copy_strategy
= MEMORY_OBJECT_COPY_DELAY
;
10229 vm_object_unlock(object
);
10233 * Clone the entry, using object ref from above.
10234 * Mark both entries as shared.
10237 new_entry
= vm_map_entry_create(new_map
, FALSE
); /* Never the kernel
10238 * map or descendants */
10239 vm_map_entry_copy(new_entry
, old_entry
);
10240 old_entry
->is_shared
= TRUE
;
10241 new_entry
->is_shared
= TRUE
;
10244 * Insert the entry into the new map -- we
10245 * know we're inserting at the end of the new
10249 vm_map_store_entry_link(new_map
, vm_map_last_entry(new_map
), new_entry
);
10252 * Update the physical map
10255 if (old_entry
->is_sub_map
) {
10256 /* Bill Angell pmap support goes here */
10258 pmap_copy(new_map
->pmap
, old_map
->pmap
, new_entry
->vme_start
,
10259 old_entry
->vme_end
- old_entry
->vme_start
,
10260 old_entry
->vme_start
);
10267 vm_map_entry_t
*old_entry_p
,
10270 vm_map_entry_t old_entry
= *old_entry_p
;
10271 vm_map_size_t entry_size
= old_entry
->vme_end
- old_entry
->vme_start
;
10272 vm_map_offset_t start
= old_entry
->vme_start
;
10273 vm_map_copy_t copy
;
10274 vm_map_entry_t last
= vm_map_last_entry(new_map
);
10276 vm_map_unlock(old_map
);
10278 * Use maxprot version of copyin because we
10279 * care about whether this memory can ever
10280 * be accessed, not just whether it's accessible
10283 if (vm_map_copyin_maxprot(old_map
, start
, entry_size
, FALSE
, ©
)
10286 * The map might have changed while it
10287 * was unlocked, check it again. Skip
10288 * any blank space or permanently
10289 * unreadable region.
10291 vm_map_lock(old_map
);
10292 if (!vm_map_lookup_entry(old_map
, start
, &last
) ||
10293 (last
->max_protection
& VM_PROT_READ
) == VM_PROT_NONE
) {
10294 last
= last
->vme_next
;
10296 *old_entry_p
= last
;
10299 * XXX For some error returns, want to
10300 * XXX skip to the next element. Note
10301 * that INVALID_ADDRESS and
10302 * PROTECTION_FAILURE are handled above.
10309 * Insert the copy into the new map
10312 vm_map_copy_insert(new_map
, last
, copy
);
10315 * Pick up the traversal at the end of
10316 * the copied region.
10319 vm_map_lock(old_map
);
10320 start
+= entry_size
;
10321 if (! vm_map_lookup_entry(old_map
, start
, &last
)) {
10322 last
= last
->vme_next
;
10324 if (last
->vme_start
== start
) {
10326 * No need to clip here and we don't
10327 * want to cause any unnecessary
10331 vm_map_clip_start(old_map
, last
, start
);
10334 *old_entry_p
= last
;
10342 * Create and return a new map based on the old
10343 * map, according to the inheritance values on the
10344 * regions in that map.
10346 * The source map must not be locked.
10355 vm_map_entry_t old_entry
;
10356 vm_map_size_t new_size
= 0, entry_size
;
10357 vm_map_entry_t new_entry
;
10358 boolean_t src_needs_copy
;
10359 boolean_t new_entry_needs_copy
;
10360 boolean_t pmap_is64bit
;
10363 #if defined(__i386__) || defined(__x86_64__)
10364 old_map
->pmap
->pm_task_map
!= TASK_MAP_32BIT
;
10366 #error Unknown architecture.
10369 new_pmap
= pmap_create(ledger
, (vm_map_size_t
) 0, pmap_is64bit
);
10371 vm_map_reference_swap(old_map
);
10372 vm_map_lock(old_map
);
10374 new_map
= vm_map_create(new_pmap
,
10375 old_map
->min_offset
,
10376 old_map
->max_offset
,
10377 old_map
->hdr
.entries_pageable
);
10378 /* inherit the parent map's page size */
10379 vm_map_set_page_shift(new_map
, VM_MAP_PAGE_SHIFT(old_map
));
10381 old_entry
= vm_map_first_entry(old_map
);
10382 old_entry
!= vm_map_to_entry(old_map
);
10385 entry_size
= old_entry
->vme_end
- old_entry
->vme_start
;
10387 switch (old_entry
->inheritance
) {
10388 case VM_INHERIT_NONE
:
10391 case VM_INHERIT_SHARE
:
10392 vm_map_fork_share(old_map
, old_entry
, new_map
);
10393 new_size
+= entry_size
;
10396 case VM_INHERIT_COPY
:
10399 * Inline the copy_quickly case;
10400 * upon failure, fall back on call
10401 * to vm_map_fork_copy.
10404 if(old_entry
->is_sub_map
)
10406 if ((old_entry
->wired_count
!= 0) ||
10407 ((VME_OBJECT(old_entry
) != NULL
) &&
10408 (VME_OBJECT(old_entry
)->true_share
))) {
10409 goto slow_vm_map_fork_copy
;
10412 new_entry
= vm_map_entry_create(new_map
, FALSE
); /* never the kernel map or descendants */
10413 vm_map_entry_copy(new_entry
, old_entry
);
10414 if (new_entry
->is_sub_map
) {
10415 /* clear address space specifics */
10416 new_entry
->use_pmap
= FALSE
;
10419 if (! vm_object_copy_quickly(
10420 &VME_OBJECT(new_entry
),
10421 VME_OFFSET(old_entry
),
10422 (old_entry
->vme_end
-
10423 old_entry
->vme_start
),
10425 &new_entry_needs_copy
)) {
10426 vm_map_entry_dispose(new_map
, new_entry
);
10427 goto slow_vm_map_fork_copy
;
10431 * Handle copy-on-write obligations
10434 if (src_needs_copy
&& !old_entry
->needs_copy
) {
10437 prot
= old_entry
->protection
& ~VM_PROT_WRITE
;
10439 if (override_nx(old_map
, VME_ALIAS(old_entry
))
10441 prot
|= VM_PROT_EXECUTE
;
10443 vm_object_pmap_protect(
10444 VME_OBJECT(old_entry
),
10445 VME_OFFSET(old_entry
),
10446 (old_entry
->vme_end
-
10447 old_entry
->vme_start
),
10448 ((old_entry
->is_shared
10449 || old_map
->mapped_in_other_pmaps
)
10452 old_entry
->vme_start
,
10455 assert(old_entry
->wired_count
== 0);
10456 old_entry
->needs_copy
= TRUE
;
10458 new_entry
->needs_copy
= new_entry_needs_copy
;
10461 * Insert the entry at the end
10465 vm_map_store_entry_link(new_map
, vm_map_last_entry(new_map
),
10467 new_size
+= entry_size
;
10470 slow_vm_map_fork_copy
:
10471 if (vm_map_fork_copy(old_map
, &old_entry
, new_map
)) {
10472 new_size
+= entry_size
;
10476 old_entry
= old_entry
->vme_next
;
10480 new_map
->size
= new_size
;
10481 vm_map_unlock(old_map
);
10482 vm_map_deallocate(old_map
);
10490 * Setup the "new_map" with the proper execution environment according
10491 * to the type of executable (platform, 64bit, chroot environment).
10492 * Map the comm page and shared region, etc...
10501 SHARED_REGION_TRACE_DEBUG(
10502 ("shared_region: task %p: vm_map_exec(%p,%p,%p,0x%x): ->\n",
10503 (void *)VM_KERNEL_ADDRPERM(current_task()),
10504 (void *)VM_KERNEL_ADDRPERM(new_map
),
10505 (void *)VM_KERNEL_ADDRPERM(task
),
10506 (void *)VM_KERNEL_ADDRPERM(fsroot
),
10508 (void) vm_commpage_enter(new_map
, task
);
10509 (void) vm_shared_region_enter(new_map
, task
, fsroot
, cpu
);
10510 SHARED_REGION_TRACE_DEBUG(
10511 ("shared_region: task %p: vm_map_exec(%p,%p,%p,0x%x): <-\n",
10512 (void *)VM_KERNEL_ADDRPERM(current_task()),
10513 (void *)VM_KERNEL_ADDRPERM(new_map
),
10514 (void *)VM_KERNEL_ADDRPERM(task
),
10515 (void *)VM_KERNEL_ADDRPERM(fsroot
),
10517 return KERN_SUCCESS
;
10521 * vm_map_lookup_locked:
10523 * Finds the VM object, offset, and
10524 * protection for a given virtual address in the
10525 * specified map, assuming a page fault of the
10528 * Returns the (object, offset, protection) for
10529 * this address, whether it is wired down, and whether
10530 * this map has the only reference to the data in question.
10531 * In order to later verify this lookup, a "version"
10534 * The map MUST be locked by the caller and WILL be
10535 * locked on exit. In order to guarantee the
10536 * existence of the returned object, it is returned
10539 * If a lookup is requested with "write protection"
10540 * specified, the map may be changed to perform virtual
10541 * copying operations, although the data referenced will
10545 vm_map_lookup_locked(
10546 vm_map_t
*var_map
, /* IN/OUT */
10547 vm_map_offset_t vaddr
,
10548 vm_prot_t fault_type
,
10549 int object_lock_type
,
10550 vm_map_version_t
*out_version
, /* OUT */
10551 vm_object_t
*object
, /* OUT */
10552 vm_object_offset_t
*offset
, /* OUT */
10553 vm_prot_t
*out_prot
, /* OUT */
10554 boolean_t
*wired
, /* OUT */
10555 vm_object_fault_info_t fault_info
, /* OUT */
10556 vm_map_t
*real_map
)
10558 vm_map_entry_t entry
;
10559 register vm_map_t map
= *var_map
;
10560 vm_map_t old_map
= *var_map
;
10561 vm_map_t cow_sub_map_parent
= VM_MAP_NULL
;
10562 vm_map_offset_t cow_parent_vaddr
= 0;
10563 vm_map_offset_t old_start
= 0;
10564 vm_map_offset_t old_end
= 0;
10565 register vm_prot_t prot
;
10566 boolean_t mask_protections
;
10567 boolean_t force_copy
;
10568 vm_prot_t original_fault_type
;
10571 * VM_PROT_MASK means that the caller wants us to use "fault_type"
10572 * as a mask against the mapping's actual protections, not as an
10575 mask_protections
= (fault_type
& VM_PROT_IS_MASK
) ? TRUE
: FALSE
;
10576 force_copy
= (fault_type
& VM_PROT_COPY
) ? TRUE
: FALSE
;
10577 fault_type
&= VM_PROT_ALL
;
10578 original_fault_type
= fault_type
;
10583 fault_type
= original_fault_type
;
10586 * If the map has an interesting hint, try it before calling
10587 * full blown lookup routine.
10591 if ((entry
== vm_map_to_entry(map
)) ||
10592 (vaddr
< entry
->vme_start
) || (vaddr
>= entry
->vme_end
)) {
10593 vm_map_entry_t tmp_entry
;
10596 * Entry was either not a valid hint, or the vaddr
10597 * was not contained in the entry, so do a full lookup.
10599 if (!vm_map_lookup_entry(map
, vaddr
, &tmp_entry
)) {
10600 if((cow_sub_map_parent
) && (cow_sub_map_parent
!= map
))
10601 vm_map_unlock(cow_sub_map_parent
);
10602 if((*real_map
!= map
)
10603 && (*real_map
!= cow_sub_map_parent
))
10604 vm_map_unlock(*real_map
);
10605 return KERN_INVALID_ADDRESS
;
10610 if(map
== old_map
) {
10611 old_start
= entry
->vme_start
;
10612 old_end
= entry
->vme_end
;
10616 * Handle submaps. Drop lock on upper map, submap is
10621 if (entry
->is_sub_map
) {
10622 vm_map_offset_t local_vaddr
;
10623 vm_map_offset_t end_delta
;
10624 vm_map_offset_t start_delta
;
10625 vm_map_entry_t submap_entry
;
10626 boolean_t mapped_needs_copy
=FALSE
;
10628 local_vaddr
= vaddr
;
10630 if ((entry
->use_pmap
&& !(fault_type
& VM_PROT_WRITE
))) {
10631 /* if real_map equals map we unlock below */
10632 if ((*real_map
!= map
) &&
10633 (*real_map
!= cow_sub_map_parent
))
10634 vm_map_unlock(*real_map
);
10635 *real_map
= VME_SUBMAP(entry
);
10638 if(entry
->needs_copy
&& (fault_type
& VM_PROT_WRITE
)) {
10639 if (!mapped_needs_copy
) {
10640 if (vm_map_lock_read_to_write(map
)) {
10641 vm_map_lock_read(map
);
10645 vm_map_lock_read(VME_SUBMAP(entry
));
10646 *var_map
= VME_SUBMAP(entry
);
10647 cow_sub_map_parent
= map
;
10648 /* reset base to map before cow object */
10649 /* this is the map which will accept */
10650 /* the new cow object */
10651 old_start
= entry
->vme_start
;
10652 old_end
= entry
->vme_end
;
10653 cow_parent_vaddr
= vaddr
;
10654 mapped_needs_copy
= TRUE
;
10656 vm_map_lock_read(VME_SUBMAP(entry
));
10657 *var_map
= VME_SUBMAP(entry
);
10658 if((cow_sub_map_parent
!= map
) &&
10659 (*real_map
!= map
))
10660 vm_map_unlock(map
);
10663 vm_map_lock_read(VME_SUBMAP(entry
));
10664 *var_map
= VME_SUBMAP(entry
);
10665 /* leave map locked if it is a target */
10666 /* cow sub_map above otherwise, just */
10667 /* follow the maps down to the object */
10668 /* here we unlock knowing we are not */
10669 /* revisiting the map. */
10670 if((*real_map
!= map
) && (map
!= cow_sub_map_parent
))
10671 vm_map_unlock_read(map
);
10676 /* calculate the offset in the submap for vaddr */
10677 local_vaddr
= (local_vaddr
- entry
->vme_start
) + VME_OFFSET(entry
);
10680 if(!vm_map_lookup_entry(map
, local_vaddr
, &submap_entry
)) {
10681 if((cow_sub_map_parent
) && (cow_sub_map_parent
!= map
)){
10682 vm_map_unlock(cow_sub_map_parent
);
10684 if((*real_map
!= map
)
10685 && (*real_map
!= cow_sub_map_parent
)) {
10686 vm_map_unlock(*real_map
);
10689 return KERN_INVALID_ADDRESS
;
10692 /* find the attenuated shadow of the underlying object */
10693 /* on our target map */
10695 /* in english the submap object may extend beyond the */
10696 /* region mapped by the entry or, may only fill a portion */
10697 /* of it. For our purposes, we only care if the object */
10698 /* doesn't fill. In this case the area which will */
10699 /* ultimately be clipped in the top map will only need */
10700 /* to be as big as the portion of the underlying entry */
10701 /* which is mapped */
10702 start_delta
= submap_entry
->vme_start
> VME_OFFSET(entry
) ?
10703 submap_entry
->vme_start
- VME_OFFSET(entry
) : 0;
10706 (VME_OFFSET(entry
) + start_delta
+ (old_end
- old_start
)) <=
10707 submap_entry
->vme_end
?
10708 0 : (VME_OFFSET(entry
) +
10709 (old_end
- old_start
))
10710 - submap_entry
->vme_end
;
10712 old_start
+= start_delta
;
10713 old_end
-= end_delta
;
10715 if(submap_entry
->is_sub_map
) {
10716 entry
= submap_entry
;
10717 vaddr
= local_vaddr
;
10718 goto submap_recurse
;
10721 if(((fault_type
& VM_PROT_WRITE
) && cow_sub_map_parent
)) {
10723 vm_object_t sub_object
, copy_object
;
10724 vm_object_offset_t copy_offset
;
10725 vm_map_offset_t local_start
;
10726 vm_map_offset_t local_end
;
10727 boolean_t copied_slowly
= FALSE
;
10729 if (vm_map_lock_read_to_write(map
)) {
10730 vm_map_lock_read(map
);
10731 old_start
-= start_delta
;
10732 old_end
+= end_delta
;
10737 sub_object
= VME_OBJECT(submap_entry
);
10738 if (sub_object
== VM_OBJECT_NULL
) {
10740 vm_object_allocate(
10742 (submap_entry
->vme_end
-
10743 submap_entry
->vme_start
));
10744 VME_OBJECT_SET(submap_entry
, sub_object
);
10745 VME_OFFSET_SET(submap_entry
, 0);
10747 local_start
= local_vaddr
-
10748 (cow_parent_vaddr
- old_start
);
10749 local_end
= local_vaddr
+
10750 (old_end
- cow_parent_vaddr
);
10751 vm_map_clip_start(map
, submap_entry
, local_start
);
10752 vm_map_clip_end(map
, submap_entry
, local_end
);
10753 if (submap_entry
->is_sub_map
) {
10754 /* unnesting was done when clipping */
10755 assert(!submap_entry
->use_pmap
);
10758 /* This is the COW case, lets connect */
10759 /* an entry in our space to the underlying */
10760 /* object in the submap, bypassing the */
10764 if(submap_entry
->wired_count
!= 0 ||
10765 (sub_object
->copy_strategy
==
10766 MEMORY_OBJECT_COPY_NONE
)) {
10767 vm_object_lock(sub_object
);
10768 vm_object_copy_slowly(sub_object
,
10769 VME_OFFSET(submap_entry
),
10770 (submap_entry
->vme_end
-
10771 submap_entry
->vme_start
),
10774 copied_slowly
= TRUE
;
10777 /* set up shadow object */
10778 copy_object
= sub_object
;
10779 vm_object_reference(copy_object
);
10780 sub_object
->shadowed
= TRUE
;
10781 assert(submap_entry
->wired_count
== 0);
10782 submap_entry
->needs_copy
= TRUE
;
10784 prot
= submap_entry
->protection
& ~VM_PROT_WRITE
;
10786 if (override_nx(old_map
,
10787 VME_ALIAS(submap_entry
))
10789 prot
|= VM_PROT_EXECUTE
;
10791 vm_object_pmap_protect(
10793 VME_OFFSET(submap_entry
),
10794 submap_entry
->vme_end
-
10795 submap_entry
->vme_start
,
10796 (submap_entry
->is_shared
10797 || map
->mapped_in_other_pmaps
) ?
10798 PMAP_NULL
: map
->pmap
,
10799 submap_entry
->vme_start
,
10804 * Adjust the fault offset to the submap entry.
10806 copy_offset
= (local_vaddr
-
10807 submap_entry
->vme_start
+
10808 VME_OFFSET(submap_entry
));
10810 /* This works diffently than the */
10811 /* normal submap case. We go back */
10812 /* to the parent of the cow map and*/
10813 /* clip out the target portion of */
10814 /* the sub_map, substituting the */
10815 /* new copy object, */
10817 vm_map_unlock(map
);
10818 local_start
= old_start
;
10819 local_end
= old_end
;
10820 map
= cow_sub_map_parent
;
10821 *var_map
= cow_sub_map_parent
;
10822 vaddr
= cow_parent_vaddr
;
10823 cow_sub_map_parent
= NULL
;
10825 if(!vm_map_lookup_entry(map
,
10827 vm_object_deallocate(
10829 vm_map_lock_write_to_read(map
);
10830 return KERN_INVALID_ADDRESS
;
10833 /* clip out the portion of space */
10834 /* mapped by the sub map which */
10835 /* corresponds to the underlying */
10839 * Clip (and unnest) the smallest nested chunk
10840 * possible around the faulting address...
10842 local_start
= vaddr
& ~(pmap_nesting_size_min
- 1);
10843 local_end
= local_start
+ pmap_nesting_size_min
;
10845 * ... but don't go beyond the "old_start" to "old_end"
10846 * range, to avoid spanning over another VM region
10847 * with a possibly different VM object and/or offset.
10849 if (local_start
< old_start
) {
10850 local_start
= old_start
;
10852 if (local_end
> old_end
) {
10853 local_end
= old_end
;
10856 * Adjust copy_offset to the start of the range.
10858 copy_offset
-= (vaddr
- local_start
);
10860 vm_map_clip_start(map
, entry
, local_start
);
10861 vm_map_clip_end(map
, entry
, local_end
);
10862 if (entry
->is_sub_map
) {
10863 /* unnesting was done when clipping */
10864 assert(!entry
->use_pmap
);
10867 /* substitute copy object for */
10868 /* shared map entry */
10869 vm_map_deallocate(VME_SUBMAP(entry
));
10870 assert(!entry
->iokit_acct
);
10871 entry
->is_sub_map
= FALSE
;
10872 entry
->use_pmap
= TRUE
;
10873 VME_OBJECT_SET(entry
, copy_object
);
10875 /* propagate the submap entry's protections */
10876 entry
->protection
|= submap_entry
->protection
;
10877 entry
->max_protection
|= submap_entry
->max_protection
;
10879 if(copied_slowly
) {
10880 VME_OFFSET_SET(entry
, local_start
- old_start
);
10881 entry
->needs_copy
= FALSE
;
10882 entry
->is_shared
= FALSE
;
10884 VME_OFFSET_SET(entry
, copy_offset
);
10885 assert(entry
->wired_count
== 0);
10886 entry
->needs_copy
= TRUE
;
10887 if(entry
->inheritance
== VM_INHERIT_SHARE
)
10888 entry
->inheritance
= VM_INHERIT_COPY
;
10889 if (map
!= old_map
)
10890 entry
->is_shared
= TRUE
;
10892 if(entry
->inheritance
== VM_INHERIT_SHARE
)
10893 entry
->inheritance
= VM_INHERIT_COPY
;
10895 vm_map_lock_write_to_read(map
);
10897 if((cow_sub_map_parent
)
10898 && (cow_sub_map_parent
!= *real_map
)
10899 && (cow_sub_map_parent
!= map
)) {
10900 vm_map_unlock(cow_sub_map_parent
);
10902 entry
= submap_entry
;
10903 vaddr
= local_vaddr
;
10908 * Check whether this task is allowed to have
10912 prot
= entry
->protection
;
10914 if (override_nx(old_map
, VME_ALIAS(entry
)) && prot
) {
10916 * HACK -- if not a stack, then allow execution
10918 prot
|= VM_PROT_EXECUTE
;
10921 if (mask_protections
) {
10922 fault_type
&= prot
;
10923 if (fault_type
== VM_PROT_NONE
) {
10924 goto protection_failure
;
10927 if ((fault_type
& (prot
)) != fault_type
) {
10928 protection_failure
:
10929 if (*real_map
!= map
) {
10930 vm_map_unlock(*real_map
);
10934 if ((fault_type
& VM_PROT_EXECUTE
) && prot
)
10935 log_stack_execution_failure((addr64_t
)vaddr
, prot
);
10937 DTRACE_VM2(prot_fault
, int, 1, (uint64_t *), NULL
);
10938 return KERN_PROTECTION_FAILURE
;
10942 * If this page is not pageable, we have to get
10943 * it for all possible accesses.
10946 *wired
= (entry
->wired_count
!= 0);
10951 * If the entry was copy-on-write, we either ...
10954 if (entry
->needs_copy
) {
10956 * If we want to write the page, we may as well
10957 * handle that now since we've got the map locked.
10959 * If we don't need to write the page, we just
10960 * demote the permissions allowed.
10963 if ((fault_type
& VM_PROT_WRITE
) || *wired
|| force_copy
) {
10965 * Make a new object, and place it in the
10966 * object chain. Note that no new references
10967 * have appeared -- one just moved from the
10968 * map to the new object.
10971 if (vm_map_lock_read_to_write(map
)) {
10972 vm_map_lock_read(map
);
10975 VME_OBJECT_SHADOW(entry
,
10976 (vm_map_size_t
) (entry
->vme_end
-
10977 entry
->vme_start
));
10979 VME_OBJECT(entry
)->shadowed
= TRUE
;
10980 entry
->needs_copy
= FALSE
;
10981 vm_map_lock_write_to_read(map
);
10985 * We're attempting to read a copy-on-write
10986 * page -- don't allow writes.
10989 prot
&= (~VM_PROT_WRITE
);
10994 * Create an object if necessary.
10996 if (VME_OBJECT(entry
) == VM_OBJECT_NULL
) {
10998 if (vm_map_lock_read_to_write(map
)) {
10999 vm_map_lock_read(map
);
11003 VME_OBJECT_SET(entry
,
11004 vm_object_allocate(
11005 (vm_map_size_t
)(entry
->vme_end
-
11006 entry
->vme_start
)));
11007 VME_OFFSET_SET(entry
, 0);
11008 vm_map_lock_write_to_read(map
);
11012 * Return the object/offset from this entry. If the entry
11013 * was copy-on-write or empty, it has been fixed up. Also
11014 * return the protection.
11017 *offset
= (vaddr
- entry
->vme_start
) + VME_OFFSET(entry
);
11018 *object
= VME_OBJECT(entry
);
11022 fault_info
->interruptible
= THREAD_UNINT
; /* for now... */
11023 /* ... the caller will change "interruptible" if needed */
11024 fault_info
->cluster_size
= 0;
11025 fault_info
->user_tag
= VME_ALIAS(entry
);
11026 fault_info
->pmap_options
= 0;
11027 if (entry
->iokit_acct
||
11028 (!entry
->is_sub_map
&& !entry
->use_pmap
)) {
11029 fault_info
->pmap_options
|= PMAP_OPTIONS_ALT_ACCT
;
11031 fault_info
->behavior
= entry
->behavior
;
11032 fault_info
->lo_offset
= VME_OFFSET(entry
);
11033 fault_info
->hi_offset
=
11034 (entry
->vme_end
- entry
->vme_start
) + VME_OFFSET(entry
);
11035 fault_info
->no_cache
= entry
->no_cache
;
11036 fault_info
->stealth
= FALSE
;
11037 fault_info
->io_sync
= FALSE
;
11038 if (entry
->used_for_jit
||
11039 entry
->vme_resilient_codesign
) {
11040 fault_info
->cs_bypass
= TRUE
;
11042 fault_info
->cs_bypass
= FALSE
;
11044 fault_info
->mark_zf_absent
= FALSE
;
11045 fault_info
->batch_pmap_op
= FALSE
;
11049 * Lock the object to prevent it from disappearing
11051 if (object_lock_type
== OBJECT_LOCK_EXCLUSIVE
)
11052 vm_object_lock(*object
);
11054 vm_object_lock_shared(*object
);
11057 * Save the version number
11060 out_version
->main_timestamp
= map
->timestamp
;
11062 return KERN_SUCCESS
;
11069 * Verifies that the map in question has not changed
11070 * since the given version. If successful, the map
11071 * will not change until vm_map_verify_done() is called.
11075 register vm_map_t map
,
11076 register vm_map_version_t
*version
) /* REF */
11080 vm_map_lock_read(map
);
11081 result
= (map
->timestamp
== version
->main_timestamp
);
11084 vm_map_unlock_read(map
);
11090 * vm_map_verify_done:
11092 * Releases locks acquired by a vm_map_verify.
11094 * This is now a macro in vm/vm_map.h. It does a
11095 * vm_map_unlock_read on the map.
11100 * TEMPORARYTEMPORARYTEMPORARYTEMPORARYTEMPORARYTEMPORARY
11101 * Goes away after regular vm_region_recurse function migrates to
11103 * vm_region_recurse: A form of vm_region which follows the
11104 * submaps in a target map
11109 vm_map_region_recurse_64(
11111 vm_map_offset_t
*address
, /* IN/OUT */
11112 vm_map_size_t
*size
, /* OUT */
11113 natural_t
*nesting_depth
, /* IN/OUT */
11114 vm_region_submap_info_64_t submap_info
, /* IN/OUT */
11115 mach_msg_type_number_t
*count
) /* IN/OUT */
11117 mach_msg_type_number_t original_count
;
11118 vm_region_extended_info_data_t extended
;
11119 vm_map_entry_t tmp_entry
;
11120 vm_map_offset_t user_address
;
11121 unsigned int user_max_depth
;
11124 * "curr_entry" is the VM map entry preceding or including the
11125 * address we're looking for.
11126 * "curr_map" is the map or sub-map containing "curr_entry".
11127 * "curr_address" is the equivalent of the top map's "user_address"
11128 * in the current map.
11129 * "curr_offset" is the cumulated offset of "curr_map" in the
11130 * target task's address space.
11131 * "curr_depth" is the depth of "curr_map" in the chain of
11134 * "curr_max_below" and "curr_max_above" limit the range (around
11135 * "curr_address") we should take into account in the current (sub)map.
11136 * They limit the range to what's visible through the map entries
11137 * we've traversed from the top map to the current map.
11140 vm_map_entry_t curr_entry
;
11141 vm_map_address_t curr_address
;
11142 vm_map_offset_t curr_offset
;
11144 unsigned int curr_depth
;
11145 vm_map_offset_t curr_max_below
, curr_max_above
;
11146 vm_map_offset_t curr_skip
;
11149 * "next_" is the same as "curr_" but for the VM region immediately
11150 * after the address we're looking for. We need to keep track of this
11151 * too because we want to return info about that region if the
11152 * address we're looking for is not mapped.
11154 vm_map_entry_t next_entry
;
11155 vm_map_offset_t next_offset
;
11156 vm_map_offset_t next_address
;
11158 unsigned int next_depth
;
11159 vm_map_offset_t next_max_below
, next_max_above
;
11160 vm_map_offset_t next_skip
;
11162 boolean_t look_for_pages
;
11163 vm_region_submap_short_info_64_t short_info
;
11165 if (map
== VM_MAP_NULL
) {
11166 /* no address space to work on */
11167 return KERN_INVALID_ARGUMENT
;
11171 if (*count
< VM_REGION_SUBMAP_SHORT_INFO_COUNT_64
) {
11173 * "info" structure is not big enough and
11176 return KERN_INVALID_ARGUMENT
;
11179 original_count
= *count
;
11181 if (original_count
< VM_REGION_SUBMAP_INFO_V0_COUNT_64
) {
11182 *count
= VM_REGION_SUBMAP_SHORT_INFO_COUNT_64
;
11183 look_for_pages
= FALSE
;
11184 short_info
= (vm_region_submap_short_info_64_t
) submap_info
;
11185 submap_info
= NULL
;
11187 look_for_pages
= TRUE
;
11188 *count
= VM_REGION_SUBMAP_INFO_V0_COUNT_64
;
11191 if (original_count
>= VM_REGION_SUBMAP_INFO_V1_COUNT_64
) {
11192 *count
= VM_REGION_SUBMAP_INFO_V1_COUNT_64
;
11196 user_address
= *address
;
11197 user_max_depth
= *nesting_depth
;
11200 vm_map_lock_read(map
);
11206 curr_address
= user_address
;
11210 curr_max_above
= ((vm_map_offset_t
) -1) - curr_address
;
11211 curr_max_below
= curr_address
;
11219 next_max_above
= (vm_map_offset_t
) -1;
11220 next_max_below
= (vm_map_offset_t
) -1;
11223 if (vm_map_lookup_entry(curr_map
,
11226 /* tmp_entry contains the address we're looking for */
11227 curr_entry
= tmp_entry
;
11229 vm_map_offset_t skip
;
11231 * The address is not mapped. "tmp_entry" is the
11232 * map entry preceding the address. We want the next
11233 * one, if it exists.
11235 curr_entry
= tmp_entry
->vme_next
;
11237 if (curr_entry
== vm_map_to_entry(curr_map
) ||
11238 (curr_entry
->vme_start
>=
11239 curr_address
+ curr_max_above
)) {
11240 /* no next entry at this level: stop looking */
11242 vm_map_unlock_read(curr_map
);
11249 curr_max_above
= 0;
11250 curr_max_below
= 0;
11254 /* adjust current address and offset */
11255 skip
= curr_entry
->vme_start
- curr_address
;
11256 curr_address
= curr_entry
->vme_start
;
11258 curr_offset
+= skip
;
11259 curr_max_above
-= skip
;
11260 curr_max_below
= 0;
11264 * Is the next entry at this level closer to the address (or
11265 * deeper in the submap chain) than the one we had
11268 tmp_entry
= curr_entry
->vme_next
;
11269 if (tmp_entry
== vm_map_to_entry(curr_map
)) {
11270 /* no next entry at this level */
11271 } else if (tmp_entry
->vme_start
>=
11272 curr_address
+ curr_max_above
) {
11274 * tmp_entry is beyond the scope of what we mapped of
11275 * this submap in the upper level: ignore it.
11277 } else if ((next_entry
== NULL
) ||
11278 (tmp_entry
->vme_start
+ curr_offset
<=
11279 next_entry
->vme_start
+ next_offset
)) {
11281 * We didn't have a "next_entry" or this one is
11282 * closer to the address we're looking for:
11283 * use this "tmp_entry" as the new "next_entry".
11285 if (next_entry
!= NULL
) {
11286 /* unlock the last "next_map" */
11287 if (next_map
!= curr_map
&& not_in_kdp
) {
11288 vm_map_unlock_read(next_map
);
11291 next_entry
= tmp_entry
;
11292 next_map
= curr_map
;
11293 next_depth
= curr_depth
;
11294 next_address
= next_entry
->vme_start
;
11295 next_skip
= curr_skip
;
11296 next_skip
+= (next_address
- curr_address
);
11297 next_offset
= curr_offset
;
11298 next_offset
+= (next_address
- curr_address
);
11299 next_max_above
= MIN(next_max_above
, curr_max_above
);
11300 next_max_above
= MIN(next_max_above
,
11301 next_entry
->vme_end
- next_address
);
11302 next_max_below
= MIN(next_max_below
, curr_max_below
);
11303 next_max_below
= MIN(next_max_below
,
11304 next_address
- next_entry
->vme_start
);
11308 * "curr_max_{above,below}" allow us to keep track of the
11309 * portion of the submap that is actually mapped at this level:
11310 * the rest of that submap is irrelevant to us, since it's not
11312 * The relevant portion of the map starts at
11313 * "VME_OFFSET(curr_entry)" up to the size of "curr_entry".
11315 curr_max_above
= MIN(curr_max_above
,
11316 curr_entry
->vme_end
- curr_address
);
11317 curr_max_below
= MIN(curr_max_below
,
11318 curr_address
- curr_entry
->vme_start
);
11320 if (!curr_entry
->is_sub_map
||
11321 curr_depth
>= user_max_depth
) {
11323 * We hit a leaf map or we reached the maximum depth
11324 * we could, so stop looking. Keep the current map
11331 * Get down to the next submap level.
11335 * Lock the next level and unlock the current level,
11336 * unless we need to keep it locked to access the "next_entry"
11340 vm_map_lock_read(VME_SUBMAP(curr_entry
));
11342 if (curr_map
== next_map
) {
11343 /* keep "next_map" locked in case we need it */
11345 /* release this map */
11347 vm_map_unlock_read(curr_map
);
11351 * Adjust the offset. "curr_entry" maps the submap
11352 * at relative address "curr_entry->vme_start" in the
11353 * curr_map but skips the first "VME_OFFSET(curr_entry)"
11354 * bytes of the submap.
11355 * "curr_offset" always represents the offset of a virtual
11356 * address in the curr_map relative to the absolute address
11357 * space (i.e. the top-level VM map).
11360 (VME_OFFSET(curr_entry
) - curr_entry
->vme_start
);
11361 curr_address
= user_address
+ curr_offset
;
11362 /* switch to the submap */
11363 curr_map
= VME_SUBMAP(curr_entry
);
11368 if (curr_entry
== NULL
) {
11369 /* no VM region contains the address... */
11370 if (next_entry
== NULL
) {
11371 /* ... and no VM region follows it either */
11372 return KERN_INVALID_ADDRESS
;
11374 /* ... gather info about the next VM region */
11375 curr_entry
= next_entry
;
11376 curr_map
= next_map
; /* still locked ... */
11377 curr_address
= next_address
;
11378 curr_skip
= next_skip
;
11379 curr_offset
= next_offset
;
11380 curr_depth
= next_depth
;
11381 curr_max_above
= next_max_above
;
11382 curr_max_below
= next_max_below
;
11384 /* we won't need "next_entry" after all */
11385 if (next_entry
!= NULL
) {
11386 /* release "next_map" */
11387 if (next_map
!= curr_map
&& not_in_kdp
) {
11388 vm_map_unlock_read(next_map
);
11397 next_max_below
= -1;
11398 next_max_above
= -1;
11400 if (curr_entry
->is_sub_map
&&
11401 curr_depth
< user_max_depth
) {
11403 * We're not as deep as we could be: we must have
11404 * gone back up after not finding anything mapped
11405 * below the original top-level map entry's.
11406 * Let's move "curr_address" forward and recurse again.
11408 user_address
= curr_address
;
11409 goto recurse_again
;
11412 *nesting_depth
= curr_depth
;
11413 *size
= curr_max_above
+ curr_max_below
;
11414 *address
= user_address
+ curr_skip
- curr_max_below
;
11416 // LP64todo: all the current tools are 32bit, obviously never worked for 64b
11417 // so probably should be a real 32b ID vs. ptr.
11418 // Current users just check for equality
11419 #define INFO_MAKE_OBJECT_ID(p) ((uint32_t)(uintptr_t)VM_KERNEL_ADDRPERM(p))
11421 if (look_for_pages
) {
11422 submap_info
->user_tag
= VME_ALIAS(curr_entry
);
11423 submap_info
->offset
= VME_OFFSET(curr_entry
);
11424 submap_info
->protection
= curr_entry
->protection
;
11425 submap_info
->inheritance
= curr_entry
->inheritance
;
11426 submap_info
->max_protection
= curr_entry
->max_protection
;
11427 submap_info
->behavior
= curr_entry
->behavior
;
11428 submap_info
->user_wired_count
= curr_entry
->user_wired_count
;
11429 submap_info
->is_submap
= curr_entry
->is_sub_map
;
11430 submap_info
->object_id
= INFO_MAKE_OBJECT_ID(VME_OBJECT(curr_entry
));
11432 short_info
->user_tag
= VME_ALIAS(curr_entry
);
11433 short_info
->offset
= VME_OFFSET(curr_entry
);
11434 short_info
->protection
= curr_entry
->protection
;
11435 short_info
->inheritance
= curr_entry
->inheritance
;
11436 short_info
->max_protection
= curr_entry
->max_protection
;
11437 short_info
->behavior
= curr_entry
->behavior
;
11438 short_info
->user_wired_count
= curr_entry
->user_wired_count
;
11439 short_info
->is_submap
= curr_entry
->is_sub_map
;
11440 short_info
->object_id
= INFO_MAKE_OBJECT_ID(VME_OBJECT(curr_entry
));
11443 extended
.pages_resident
= 0;
11444 extended
.pages_swapped_out
= 0;
11445 extended
.pages_shared_now_private
= 0;
11446 extended
.pages_dirtied
= 0;
11447 extended
.pages_reusable
= 0;
11448 extended
.external_pager
= 0;
11449 extended
.shadow_depth
= 0;
11450 extended
.share_mode
= SM_EMPTY
;
11451 extended
.ref_count
= 0;
11454 if (!curr_entry
->is_sub_map
) {
11455 vm_map_offset_t range_start
, range_end
;
11456 range_start
= MAX((curr_address
- curr_max_below
),
11457 curr_entry
->vme_start
);
11458 range_end
= MIN((curr_address
+ curr_max_above
),
11459 curr_entry
->vme_end
);
11460 vm_map_region_walk(curr_map
,
11463 (VME_OFFSET(curr_entry
) +
11465 curr_entry
->vme_start
)),
11466 range_end
- range_start
,
11468 look_for_pages
, VM_REGION_EXTENDED_INFO_COUNT
);
11469 if (extended
.external_pager
&&
11470 extended
.ref_count
== 2 &&
11471 extended
.share_mode
== SM_SHARED
) {
11472 extended
.share_mode
= SM_PRIVATE
;
11475 if (curr_entry
->use_pmap
) {
11476 extended
.share_mode
= SM_TRUESHARED
;
11478 extended
.share_mode
= SM_PRIVATE
;
11480 extended
.ref_count
= VME_SUBMAP(curr_entry
)->ref_count
;
11484 if (look_for_pages
) {
11485 submap_info
->pages_resident
= extended
.pages_resident
;
11486 submap_info
->pages_swapped_out
= extended
.pages_swapped_out
;
11487 submap_info
->pages_shared_now_private
=
11488 extended
.pages_shared_now_private
;
11489 submap_info
->pages_dirtied
= extended
.pages_dirtied
;
11490 submap_info
->external_pager
= extended
.external_pager
;
11491 submap_info
->shadow_depth
= extended
.shadow_depth
;
11492 submap_info
->share_mode
= extended
.share_mode
;
11493 submap_info
->ref_count
= extended
.ref_count
;
11495 if (original_count
>= VM_REGION_SUBMAP_INFO_V1_COUNT_64
) {
11496 submap_info
->pages_reusable
= extended
.pages_reusable
;
11499 short_info
->external_pager
= extended
.external_pager
;
11500 short_info
->shadow_depth
= extended
.shadow_depth
;
11501 short_info
->share_mode
= extended
.share_mode
;
11502 short_info
->ref_count
= extended
.ref_count
;
11506 vm_map_unlock_read(curr_map
);
11509 return KERN_SUCCESS
;
11515 * User call to obtain information about a region in
11516 * a task's address map. Currently, only one flavor is
11519 * XXX The reserved and behavior fields cannot be filled
11520 * in until the vm merge from the IK is completed, and
11521 * vm_reserve is implemented.
11527 vm_map_offset_t
*address
, /* IN/OUT */
11528 vm_map_size_t
*size
, /* OUT */
11529 vm_region_flavor_t flavor
, /* IN */
11530 vm_region_info_t info
, /* OUT */
11531 mach_msg_type_number_t
*count
, /* IN/OUT */
11532 mach_port_t
*object_name
) /* OUT */
11534 vm_map_entry_t tmp_entry
;
11535 vm_map_entry_t entry
;
11536 vm_map_offset_t start
;
11538 if (map
== VM_MAP_NULL
)
11539 return(KERN_INVALID_ARGUMENT
);
11543 case VM_REGION_BASIC_INFO
:
11544 /* legacy for old 32-bit objects info */
11546 vm_region_basic_info_t basic
;
11548 if (*count
< VM_REGION_BASIC_INFO_COUNT
)
11549 return(KERN_INVALID_ARGUMENT
);
11551 basic
= (vm_region_basic_info_t
) info
;
11552 *count
= VM_REGION_BASIC_INFO_COUNT
;
11554 vm_map_lock_read(map
);
11557 if (!vm_map_lookup_entry(map
, start
, &tmp_entry
)) {
11558 if ((entry
= tmp_entry
->vme_next
) == vm_map_to_entry(map
)) {
11559 vm_map_unlock_read(map
);
11560 return(KERN_INVALID_ADDRESS
);
11566 start
= entry
->vme_start
;
11568 basic
->offset
= (uint32_t)VME_OFFSET(entry
);
11569 basic
->protection
= entry
->protection
;
11570 basic
->inheritance
= entry
->inheritance
;
11571 basic
->max_protection
= entry
->max_protection
;
11572 basic
->behavior
= entry
->behavior
;
11573 basic
->user_wired_count
= entry
->user_wired_count
;
11574 basic
->reserved
= entry
->is_sub_map
;
11576 *size
= (entry
->vme_end
- start
);
11578 if (object_name
) *object_name
= IP_NULL
;
11579 if (entry
->is_sub_map
) {
11580 basic
->shared
= FALSE
;
11582 basic
->shared
= entry
->is_shared
;
11585 vm_map_unlock_read(map
);
11586 return(KERN_SUCCESS
);
11589 case VM_REGION_BASIC_INFO_64
:
11591 vm_region_basic_info_64_t basic
;
11593 if (*count
< VM_REGION_BASIC_INFO_COUNT_64
)
11594 return(KERN_INVALID_ARGUMENT
);
11596 basic
= (vm_region_basic_info_64_t
) info
;
11597 *count
= VM_REGION_BASIC_INFO_COUNT_64
;
11599 vm_map_lock_read(map
);
11602 if (!vm_map_lookup_entry(map
, start
, &tmp_entry
)) {
11603 if ((entry
= tmp_entry
->vme_next
) == vm_map_to_entry(map
)) {
11604 vm_map_unlock_read(map
);
11605 return(KERN_INVALID_ADDRESS
);
11611 start
= entry
->vme_start
;
11613 basic
->offset
= VME_OFFSET(entry
);
11614 basic
->protection
= entry
->protection
;
11615 basic
->inheritance
= entry
->inheritance
;
11616 basic
->max_protection
= entry
->max_protection
;
11617 basic
->behavior
= entry
->behavior
;
11618 basic
->user_wired_count
= entry
->user_wired_count
;
11619 basic
->reserved
= entry
->is_sub_map
;
11621 *size
= (entry
->vme_end
- start
);
11623 if (object_name
) *object_name
= IP_NULL
;
11624 if (entry
->is_sub_map
) {
11625 basic
->shared
= FALSE
;
11627 basic
->shared
= entry
->is_shared
;
11630 vm_map_unlock_read(map
);
11631 return(KERN_SUCCESS
);
11633 case VM_REGION_EXTENDED_INFO
:
11634 if (*count
< VM_REGION_EXTENDED_INFO_COUNT
)
11635 return(KERN_INVALID_ARGUMENT
);
11637 case VM_REGION_EXTENDED_INFO__legacy
:
11638 if (*count
< VM_REGION_EXTENDED_INFO_COUNT__legacy
)
11639 return KERN_INVALID_ARGUMENT
;
11642 vm_region_extended_info_t extended
;
11643 mach_msg_type_number_t original_count
;
11645 extended
= (vm_region_extended_info_t
) info
;
11647 vm_map_lock_read(map
);
11650 if (!vm_map_lookup_entry(map
, start
, &tmp_entry
)) {
11651 if ((entry
= tmp_entry
->vme_next
) == vm_map_to_entry(map
)) {
11652 vm_map_unlock_read(map
);
11653 return(KERN_INVALID_ADDRESS
);
11658 start
= entry
->vme_start
;
11660 extended
->protection
= entry
->protection
;
11661 extended
->user_tag
= VME_ALIAS(entry
);
11662 extended
->pages_resident
= 0;
11663 extended
->pages_swapped_out
= 0;
11664 extended
->pages_shared_now_private
= 0;
11665 extended
->pages_dirtied
= 0;
11666 extended
->external_pager
= 0;
11667 extended
->shadow_depth
= 0;
11669 original_count
= *count
;
11670 if (flavor
== VM_REGION_EXTENDED_INFO__legacy
) {
11671 *count
= VM_REGION_EXTENDED_INFO_COUNT__legacy
;
11673 extended
->pages_reusable
= 0;
11674 *count
= VM_REGION_EXTENDED_INFO_COUNT
;
11677 vm_map_region_walk(map
, start
, entry
, VME_OFFSET(entry
), entry
->vme_end
- start
, extended
, TRUE
, *count
);
11679 if (extended
->external_pager
&& extended
->ref_count
== 2 && extended
->share_mode
== SM_SHARED
)
11680 extended
->share_mode
= SM_PRIVATE
;
11683 *object_name
= IP_NULL
;
11685 *size
= (entry
->vme_end
- start
);
11687 vm_map_unlock_read(map
);
11688 return(KERN_SUCCESS
);
11690 case VM_REGION_TOP_INFO
:
11692 vm_region_top_info_t top
;
11694 if (*count
< VM_REGION_TOP_INFO_COUNT
)
11695 return(KERN_INVALID_ARGUMENT
);
11697 top
= (vm_region_top_info_t
) info
;
11698 *count
= VM_REGION_TOP_INFO_COUNT
;
11700 vm_map_lock_read(map
);
11703 if (!vm_map_lookup_entry(map
, start
, &tmp_entry
)) {
11704 if ((entry
= tmp_entry
->vme_next
) == vm_map_to_entry(map
)) {
11705 vm_map_unlock_read(map
);
11706 return(KERN_INVALID_ADDRESS
);
11712 start
= entry
->vme_start
;
11714 top
->private_pages_resident
= 0;
11715 top
->shared_pages_resident
= 0;
11717 vm_map_region_top_walk(entry
, top
);
11720 *object_name
= IP_NULL
;
11722 *size
= (entry
->vme_end
- start
);
11724 vm_map_unlock_read(map
);
11725 return(KERN_SUCCESS
);
11728 return(KERN_INVALID_ARGUMENT
);
11732 #define OBJ_RESIDENT_COUNT(obj, entry_size) \
11733 MIN((entry_size), \
11734 ((obj)->all_reusable ? \
11735 (obj)->wired_page_count : \
11736 (obj)->resident_page_count - (obj)->reusable_page_count))
11739 vm_map_region_top_walk(
11740 vm_map_entry_t entry
,
11741 vm_region_top_info_t top
)
11744 if (VME_OBJECT(entry
) == 0 || entry
->is_sub_map
) {
11745 top
->share_mode
= SM_EMPTY
;
11746 top
->ref_count
= 0;
11752 struct vm_object
*obj
, *tmp_obj
;
11754 uint32_t entry_size
;
11756 entry_size
= (uint32_t) ((entry
->vme_end
- entry
->vme_start
) / PAGE_SIZE_64
);
11758 obj
= VME_OBJECT(entry
);
11760 vm_object_lock(obj
);
11762 if ((ref_count
= obj
->ref_count
) > 1 && obj
->paging_in_progress
)
11765 assert(obj
->reusable_page_count
<= obj
->resident_page_count
);
11767 if (ref_count
== 1)
11768 top
->private_pages_resident
=
11769 OBJ_RESIDENT_COUNT(obj
, entry_size
);
11771 top
->shared_pages_resident
=
11772 OBJ_RESIDENT_COUNT(obj
, entry_size
);
11773 top
->ref_count
= ref_count
;
11774 top
->share_mode
= SM_COW
;
11776 while ((tmp_obj
= obj
->shadow
)) {
11777 vm_object_lock(tmp_obj
);
11778 vm_object_unlock(obj
);
11781 if ((ref_count
= obj
->ref_count
) > 1 && obj
->paging_in_progress
)
11784 assert(obj
->reusable_page_count
<= obj
->resident_page_count
);
11785 top
->shared_pages_resident
+=
11786 OBJ_RESIDENT_COUNT(obj
, entry_size
);
11787 top
->ref_count
+= ref_count
- 1;
11790 if (entry
->superpage_size
) {
11791 top
->share_mode
= SM_LARGE_PAGE
;
11792 top
->shared_pages_resident
= 0;
11793 top
->private_pages_resident
= entry_size
;
11794 } else if (entry
->needs_copy
) {
11795 top
->share_mode
= SM_COW
;
11796 top
->shared_pages_resident
=
11797 OBJ_RESIDENT_COUNT(obj
, entry_size
);
11799 if (ref_count
== 1 ||
11800 (ref_count
== 2 && !(obj
->pager_trusted
) && !(obj
->internal
))) {
11801 top
->share_mode
= SM_PRIVATE
;
11802 top
->private_pages_resident
=
11803 OBJ_RESIDENT_COUNT(obj
,
11806 top
->share_mode
= SM_SHARED
;
11807 top
->shared_pages_resident
=
11808 OBJ_RESIDENT_COUNT(obj
,
11812 top
->ref_count
= ref_count
;
11814 /* XXX K64: obj_id will be truncated */
11815 top
->obj_id
= (unsigned int) (uintptr_t)VM_KERNEL_ADDRPERM(obj
);
11817 vm_object_unlock(obj
);
11822 vm_map_region_walk(
11824 vm_map_offset_t va
,
11825 vm_map_entry_t entry
,
11826 vm_object_offset_t offset
,
11827 vm_object_size_t range
,
11828 vm_region_extended_info_t extended
,
11829 boolean_t look_for_pages
,
11830 mach_msg_type_number_t count
)
11832 register struct vm_object
*obj
, *tmp_obj
;
11833 register vm_map_offset_t last_offset
;
11835 register int ref_count
;
11836 struct vm_object
*shadow_object
;
11839 if ((VME_OBJECT(entry
) == 0) ||
11840 (entry
->is_sub_map
) ||
11841 (VME_OBJECT(entry
)->phys_contiguous
&&
11842 !entry
->superpage_size
)) {
11843 extended
->share_mode
= SM_EMPTY
;
11844 extended
->ref_count
= 0;
11848 if (entry
->superpage_size
) {
11849 extended
->shadow_depth
= 0;
11850 extended
->share_mode
= SM_LARGE_PAGE
;
11851 extended
->ref_count
= 1;
11852 extended
->external_pager
= 0;
11853 extended
->pages_resident
= (unsigned int)(range
>> PAGE_SHIFT
);
11854 extended
->shadow_depth
= 0;
11859 obj
= VME_OBJECT(entry
);
11861 vm_object_lock(obj
);
11863 if ((ref_count
= obj
->ref_count
) > 1 && obj
->paging_in_progress
)
11866 if (look_for_pages
) {
11867 for (last_offset
= offset
+ range
;
11868 offset
< last_offset
;
11869 offset
+= PAGE_SIZE_64
, va
+= PAGE_SIZE
) {
11870 vm_map_region_look_for_page(map
, va
, obj
,
11872 0, extended
, count
);
11875 shadow_object
= obj
->shadow
;
11878 if ( !(obj
->pager_trusted
) && !(obj
->internal
))
11879 extended
->external_pager
= 1;
11881 if (shadow_object
!= VM_OBJECT_NULL
) {
11882 vm_object_lock(shadow_object
);
11884 shadow_object
!= VM_OBJECT_NULL
;
11886 vm_object_t next_shadow
;
11888 if ( !(shadow_object
->pager_trusted
) &&
11889 !(shadow_object
->internal
))
11890 extended
->external_pager
= 1;
11892 next_shadow
= shadow_object
->shadow
;
11894 vm_object_lock(next_shadow
);
11896 vm_object_unlock(shadow_object
);
11897 shadow_object
= next_shadow
;
11900 extended
->shadow_depth
= shadow_depth
;
11903 if (extended
->shadow_depth
|| entry
->needs_copy
)
11904 extended
->share_mode
= SM_COW
;
11906 if (ref_count
== 1)
11907 extended
->share_mode
= SM_PRIVATE
;
11909 if (obj
->true_share
)
11910 extended
->share_mode
= SM_TRUESHARED
;
11912 extended
->share_mode
= SM_SHARED
;
11915 extended
->ref_count
= ref_count
- extended
->shadow_depth
;
11917 for (i
= 0; i
< extended
->shadow_depth
; i
++) {
11918 if ((tmp_obj
= obj
->shadow
) == 0)
11920 vm_object_lock(tmp_obj
);
11921 vm_object_unlock(obj
);
11923 if ((ref_count
= tmp_obj
->ref_count
) > 1 && tmp_obj
->paging_in_progress
)
11926 extended
->ref_count
+= ref_count
;
11929 vm_object_unlock(obj
);
11931 if (extended
->share_mode
== SM_SHARED
) {
11932 register vm_map_entry_t cur
;
11933 register vm_map_entry_t last
;
11936 obj
= VME_OBJECT(entry
);
11937 last
= vm_map_to_entry(map
);
11940 if ((ref_count
= obj
->ref_count
) > 1 && obj
->paging_in_progress
)
11942 for (cur
= vm_map_first_entry(map
); cur
!= last
; cur
= cur
->vme_next
)
11943 my_refs
+= vm_map_region_count_obj_refs(cur
, obj
);
11945 if (my_refs
== ref_count
)
11946 extended
->share_mode
= SM_PRIVATE_ALIASED
;
11947 else if (my_refs
> 1)
11948 extended
->share_mode
= SM_SHARED_ALIASED
;
11954 /* object is locked on entry and locked on return */
11958 vm_map_region_look_for_page(
11959 __unused vm_map_t map
,
11960 __unused vm_map_offset_t va
,
11961 vm_object_t object
,
11962 vm_object_offset_t offset
,
11965 vm_region_extended_info_t extended
,
11966 mach_msg_type_number_t count
)
11968 register vm_page_t p
;
11969 register vm_object_t shadow
;
11970 register int ref_count
;
11971 vm_object_t caller_object
;
11973 shadow
= object
->shadow
;
11974 caller_object
= object
;
11979 if ( !(object
->pager_trusted
) && !(object
->internal
))
11980 extended
->external_pager
= 1;
11982 if ((p
= vm_page_lookup(object
, offset
)) != VM_PAGE_NULL
) {
11983 if (shadow
&& (max_refcnt
== 1))
11984 extended
->pages_shared_now_private
++;
11986 if (!p
->fictitious
&&
11987 (p
->dirty
|| pmap_is_modified(p
->phys_page
)))
11988 extended
->pages_dirtied
++;
11989 else if (count
>= VM_REGION_EXTENDED_INFO_COUNT
) {
11990 if (p
->reusable
|| p
->object
->all_reusable
) {
11991 extended
->pages_reusable
++;
11995 extended
->pages_resident
++;
11997 if(object
!= caller_object
)
11998 vm_object_unlock(object
);
12003 if (object
->existence_map
) {
12004 if (vm_external_state_get(object
->existence_map
, offset
) == VM_EXTERNAL_STATE_EXISTS
) {
12006 extended
->pages_swapped_out
++;
12008 if(object
!= caller_object
)
12009 vm_object_unlock(object
);
12014 #endif /* MACH_PAGEMAP */
12015 if (object
->internal
&&
12017 !object
->terminating
&&
12018 object
->pager_ready
) {
12020 if (COMPRESSED_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE
) {
12021 if (VM_COMPRESSOR_PAGER_STATE_GET(object
,
12023 == VM_EXTERNAL_STATE_EXISTS
) {
12024 /* the pager has that page */
12025 extended
->pages_swapped_out
++;
12026 if (object
!= caller_object
)
12027 vm_object_unlock(object
);
12031 memory_object_t pager
;
12033 vm_object_paging_begin(object
);
12034 pager
= object
->pager
;
12035 vm_object_unlock(object
);
12037 kr
= memory_object_data_request(
12039 offset
+ object
->paging_offset
,
12040 0, /* just poke the pager */
12044 vm_object_lock(object
);
12045 vm_object_paging_end(object
);
12047 if (kr
== KERN_SUCCESS
) {
12048 /* the pager has that page */
12049 extended
->pages_swapped_out
++;
12050 if (object
!= caller_object
)
12051 vm_object_unlock(object
);
12058 vm_object_lock(shadow
);
12060 if ((ref_count
= shadow
->ref_count
) > 1 && shadow
->paging_in_progress
)
12063 if (++depth
> extended
->shadow_depth
)
12064 extended
->shadow_depth
= depth
;
12066 if (ref_count
> max_refcnt
)
12067 max_refcnt
= ref_count
;
12069 if(object
!= caller_object
)
12070 vm_object_unlock(object
);
12072 offset
= offset
+ object
->vo_shadow_offset
;
12074 shadow
= object
->shadow
;
12077 if(object
!= caller_object
)
12078 vm_object_unlock(object
);
12084 vm_map_region_count_obj_refs(
12085 vm_map_entry_t entry
,
12086 vm_object_t object
)
12088 register int ref_count
;
12089 register vm_object_t chk_obj
;
12090 register vm_object_t tmp_obj
;
12092 if (VME_OBJECT(entry
) == 0)
12095 if (entry
->is_sub_map
)
12100 chk_obj
= VME_OBJECT(entry
);
12101 vm_object_lock(chk_obj
);
12104 if (chk_obj
== object
)
12106 tmp_obj
= chk_obj
->shadow
;
12108 vm_object_lock(tmp_obj
);
12109 vm_object_unlock(chk_obj
);
12119 * Routine: vm_map_simplify
12122 * Attempt to simplify the map representation in
12123 * the vicinity of the given starting address.
12125 * This routine is intended primarily to keep the
12126 * kernel maps more compact -- they generally don't
12127 * benefit from the "expand a map entry" technology
12128 * at allocation time because the adjacent entry
12129 * is often wired down.
12132 vm_map_simplify_entry(
12134 vm_map_entry_t this_entry
)
12136 vm_map_entry_t prev_entry
;
12138 counter(c_vm_map_simplify_entry_called
++);
12140 prev_entry
= this_entry
->vme_prev
;
12142 if ((this_entry
!= vm_map_to_entry(map
)) &&
12143 (prev_entry
!= vm_map_to_entry(map
)) &&
12145 (prev_entry
->vme_end
== this_entry
->vme_start
) &&
12147 (prev_entry
->is_sub_map
== this_entry
->is_sub_map
) &&
12148 (VME_OBJECT(prev_entry
) == VME_OBJECT(this_entry
)) &&
12149 ((VME_OFFSET(prev_entry
) + (prev_entry
->vme_end
-
12150 prev_entry
->vme_start
))
12151 == VME_OFFSET(this_entry
)) &&
12153 (prev_entry
->behavior
== this_entry
->behavior
) &&
12154 (prev_entry
->needs_copy
== this_entry
->needs_copy
) &&
12155 (prev_entry
->protection
== this_entry
->protection
) &&
12156 (prev_entry
->max_protection
== this_entry
->max_protection
) &&
12157 (prev_entry
->inheritance
== this_entry
->inheritance
) &&
12158 (prev_entry
->use_pmap
== this_entry
->use_pmap
) &&
12159 (VME_ALIAS(prev_entry
) == VME_ALIAS(this_entry
)) &&
12160 (prev_entry
->no_cache
== this_entry
->no_cache
) &&
12161 (prev_entry
->permanent
== this_entry
->permanent
) &&
12162 (prev_entry
->map_aligned
== this_entry
->map_aligned
) &&
12163 (prev_entry
->zero_wired_pages
== this_entry
->zero_wired_pages
) &&
12164 (prev_entry
->used_for_jit
== this_entry
->used_for_jit
) &&
12165 /* from_reserved_zone: OK if that field doesn't match */
12166 (prev_entry
->iokit_acct
== this_entry
->iokit_acct
) &&
12167 (prev_entry
->vme_resilient_codesign
==
12168 this_entry
->vme_resilient_codesign
) &&
12169 (prev_entry
->vme_resilient_media
==
12170 this_entry
->vme_resilient_media
) &&
12172 (prev_entry
->wired_count
== this_entry
->wired_count
) &&
12173 (prev_entry
->user_wired_count
== this_entry
->user_wired_count
) &&
12175 (prev_entry
->in_transition
== FALSE
) &&
12176 (this_entry
->in_transition
== FALSE
) &&
12177 (prev_entry
->needs_wakeup
== FALSE
) &&
12178 (this_entry
->needs_wakeup
== FALSE
) &&
12179 (prev_entry
->is_shared
== FALSE
) &&
12180 (this_entry
->is_shared
== FALSE
) &&
12181 (prev_entry
->superpage_size
== FALSE
) &&
12182 (this_entry
->superpage_size
== FALSE
)
12184 vm_map_store_entry_unlink(map
, prev_entry
);
12185 assert(prev_entry
->vme_start
< this_entry
->vme_end
);
12186 if (prev_entry
->map_aligned
)
12187 assert(VM_MAP_PAGE_ALIGNED(prev_entry
->vme_start
,
12188 VM_MAP_PAGE_MASK(map
)));
12189 this_entry
->vme_start
= prev_entry
->vme_start
;
12190 VME_OFFSET_SET(this_entry
, VME_OFFSET(prev_entry
));
12192 if (map
->holelistenabled
) {
12193 vm_map_store_update_first_free(map
, this_entry
, TRUE
);
12196 if (prev_entry
->is_sub_map
) {
12197 vm_map_deallocate(VME_SUBMAP(prev_entry
));
12199 vm_object_deallocate(VME_OBJECT(prev_entry
));
12201 vm_map_entry_dispose(map
, prev_entry
);
12202 SAVE_HINT_MAP_WRITE(map
, this_entry
);
12203 counter(c_vm_map_simplified
++);
12210 vm_map_offset_t start
)
12212 vm_map_entry_t this_entry
;
12215 if (vm_map_lookup_entry(map
, start
, &this_entry
)) {
12216 vm_map_simplify_entry(map
, this_entry
);
12217 vm_map_simplify_entry(map
, this_entry
->vme_next
);
12219 counter(c_vm_map_simplify_called
++);
12220 vm_map_unlock(map
);
12224 vm_map_simplify_range(
12226 vm_map_offset_t start
,
12227 vm_map_offset_t end
)
12229 vm_map_entry_t entry
;
12232 * The map should be locked (for "write") by the caller.
12235 if (start
>= end
) {
12236 /* invalid address range */
12240 start
= vm_map_trunc_page(start
,
12241 VM_MAP_PAGE_MASK(map
));
12242 end
= vm_map_round_page(end
,
12243 VM_MAP_PAGE_MASK(map
));
12245 if (!vm_map_lookup_entry(map
, start
, &entry
)) {
12246 /* "start" is not mapped and "entry" ends before "start" */
12247 if (entry
== vm_map_to_entry(map
)) {
12248 /* start with first entry in the map */
12249 entry
= vm_map_first_entry(map
);
12251 /* start with next entry */
12252 entry
= entry
->vme_next
;
12256 while (entry
!= vm_map_to_entry(map
) &&
12257 entry
->vme_start
<= end
) {
12258 /* try and coalesce "entry" with its previous entry */
12259 vm_map_simplify_entry(map
, entry
);
12260 entry
= entry
->vme_next
;
12266 * Routine: vm_map_machine_attribute
12268 * Provide machine-specific attributes to mappings,
12269 * such as cachability etc. for machines that provide
12270 * them. NUMA architectures and machines with big/strange
12271 * caches will use this.
12273 * Responsibilities for locking and checking are handled here,
12274 * everything else in the pmap module. If any non-volatile
12275 * information must be kept, the pmap module should handle
12276 * it itself. [This assumes that attributes do not
12277 * need to be inherited, which seems ok to me]
12280 vm_map_machine_attribute(
12282 vm_map_offset_t start
,
12283 vm_map_offset_t end
,
12284 vm_machine_attribute_t attribute
,
12285 vm_machine_attribute_val_t
* value
) /* IN/OUT */
12288 vm_map_size_t sync_size
;
12289 vm_map_entry_t entry
;
12291 if (start
< vm_map_min(map
) || end
> vm_map_max(map
))
12292 return KERN_INVALID_ADDRESS
;
12294 /* Figure how much memory we need to flush (in page increments) */
12295 sync_size
= end
- start
;
12299 if (attribute
!= MATTR_CACHE
) {
12300 /* If we don't have to find physical addresses, we */
12301 /* don't have to do an explicit traversal here. */
12302 ret
= pmap_attribute(map
->pmap
, start
, end
-start
,
12304 vm_map_unlock(map
);
12308 ret
= KERN_SUCCESS
; /* Assume it all worked */
12311 if (vm_map_lookup_entry(map
, start
, &entry
)) {
12312 vm_map_size_t sub_size
;
12313 if((entry
->vme_end
- start
) > sync_size
) {
12314 sub_size
= sync_size
;
12317 sub_size
= entry
->vme_end
- start
;
12318 sync_size
-= sub_size
;
12320 if(entry
->is_sub_map
) {
12321 vm_map_offset_t sub_start
;
12322 vm_map_offset_t sub_end
;
12324 sub_start
= (start
- entry
->vme_start
)
12325 + VME_OFFSET(entry
);
12326 sub_end
= sub_start
+ sub_size
;
12327 vm_map_machine_attribute(
12333 if (VME_OBJECT(entry
)) {
12335 vm_object_t object
;
12336 vm_object_t base_object
;
12337 vm_object_t last_object
;
12338 vm_object_offset_t offset
;
12339 vm_object_offset_t base_offset
;
12340 vm_map_size_t range
;
12342 offset
= (start
- entry
->vme_start
)
12343 + VME_OFFSET(entry
);
12344 base_offset
= offset
;
12345 object
= VME_OBJECT(entry
);
12346 base_object
= object
;
12347 last_object
= NULL
;
12349 vm_object_lock(object
);
12352 m
= vm_page_lookup(
12355 if (m
&& !m
->fictitious
) {
12357 pmap_attribute_cache_sync(
12362 } else if (object
->shadow
) {
12363 offset
= offset
+ object
->vo_shadow_offset
;
12364 last_object
= object
;
12365 object
= object
->shadow
;
12366 vm_object_lock(last_object
->shadow
);
12367 vm_object_unlock(last_object
);
12370 range
-= PAGE_SIZE
;
12372 if (base_object
!= object
) {
12373 vm_object_unlock(object
);
12374 vm_object_lock(base_object
);
12375 object
= base_object
;
12377 /* Bump to the next page */
12378 base_offset
+= PAGE_SIZE
;
12379 offset
= base_offset
;
12381 vm_object_unlock(object
);
12386 vm_map_unlock(map
);
12387 return KERN_FAILURE
;
12392 vm_map_unlock(map
);
12398 * vm_map_behavior_set:
12400 * Sets the paging reference behavior of the specified address
12401 * range in the target map. Paging reference behavior affects
12402 * how pagein operations resulting from faults on the map will be
12406 vm_map_behavior_set(
12408 vm_map_offset_t start
,
12409 vm_map_offset_t end
,
12410 vm_behavior_t new_behavior
)
12412 register vm_map_entry_t entry
;
12413 vm_map_entry_t temp_entry
;
12416 "vm_map_behavior_set, 0x%X start 0x%X end 0x%X behavior %d",
12417 map
, start
, end
, new_behavior
, 0);
12420 start
< vm_map_min(map
) ||
12421 end
> vm_map_max(map
)) {
12422 return KERN_NO_SPACE
;
12425 switch (new_behavior
) {
12428 * This first block of behaviors all set a persistent state on the specified
12429 * memory range. All we have to do here is to record the desired behavior
12430 * in the vm_map_entry_t's.
12433 case VM_BEHAVIOR_DEFAULT
:
12434 case VM_BEHAVIOR_RANDOM
:
12435 case VM_BEHAVIOR_SEQUENTIAL
:
12436 case VM_BEHAVIOR_RSEQNTL
:
12437 case VM_BEHAVIOR_ZERO_WIRED_PAGES
:
12441 * The entire address range must be valid for the map.
12442 * Note that vm_map_range_check() does a
12443 * vm_map_lookup_entry() internally and returns the
12444 * entry containing the start of the address range if
12445 * the entire range is valid.
12447 if (vm_map_range_check(map
, start
, end
, &temp_entry
)) {
12448 entry
= temp_entry
;
12449 vm_map_clip_start(map
, entry
, start
);
12452 vm_map_unlock(map
);
12453 return(KERN_INVALID_ADDRESS
);
12456 while ((entry
!= vm_map_to_entry(map
)) && (entry
->vme_start
< end
)) {
12457 vm_map_clip_end(map
, entry
, end
);
12458 if (entry
->is_sub_map
) {
12459 assert(!entry
->use_pmap
);
12462 if( new_behavior
== VM_BEHAVIOR_ZERO_WIRED_PAGES
) {
12463 entry
->zero_wired_pages
= TRUE
;
12465 entry
->behavior
= new_behavior
;
12467 entry
= entry
->vme_next
;
12470 vm_map_unlock(map
);
12474 * The rest of these are different from the above in that they cause
12475 * an immediate action to take place as opposed to setting a behavior that
12476 * affects future actions.
12479 case VM_BEHAVIOR_WILLNEED
:
12480 return vm_map_willneed(map
, start
, end
);
12482 case VM_BEHAVIOR_DONTNEED
:
12483 return vm_map_msync(map
, start
, end
- start
, VM_SYNC_DEACTIVATE
| VM_SYNC_CONTIGUOUS
);
12485 case VM_BEHAVIOR_FREE
:
12486 return vm_map_msync(map
, start
, end
- start
, VM_SYNC_KILLPAGES
| VM_SYNC_CONTIGUOUS
);
12488 case VM_BEHAVIOR_REUSABLE
:
12489 return vm_map_reusable_pages(map
, start
, end
);
12491 case VM_BEHAVIOR_REUSE
:
12492 return vm_map_reuse_pages(map
, start
, end
);
12494 case VM_BEHAVIOR_CAN_REUSE
:
12495 return vm_map_can_reuse(map
, start
, end
);
12498 case VM_BEHAVIOR_PAGEOUT
:
12499 return vm_map_pageout(map
, start
, end
);
12500 #endif /* MACH_ASSERT */
12503 return(KERN_INVALID_ARGUMENT
);
12506 return(KERN_SUCCESS
);
12511 * Internals for madvise(MADV_WILLNEED) system call.
12513 * The present implementation is to do a read-ahead if the mapping corresponds
12514 * to a mapped regular file. If it's an anonymous mapping, then we do nothing
12515 * and basically ignore the "advice" (which we are always free to do).
12519 static kern_return_t
12522 vm_map_offset_t start
,
12523 vm_map_offset_t end
12526 vm_map_entry_t entry
;
12527 vm_object_t object
;
12528 memory_object_t pager
;
12529 struct vm_object_fault_info fault_info
;
12531 vm_object_size_t len
;
12532 vm_object_offset_t offset
;
12535 * Fill in static values in fault_info. Several fields get ignored by the code
12536 * we call, but we'll fill them in anyway since uninitialized fields are bad
12537 * when it comes to future backwards compatibility.
12540 fault_info
.interruptible
= THREAD_UNINT
; /* ignored value */
12541 fault_info
.behavior
= VM_BEHAVIOR_SEQUENTIAL
;
12542 fault_info
.no_cache
= FALSE
; /* ignored value */
12543 fault_info
.stealth
= TRUE
;
12544 fault_info
.io_sync
= FALSE
;
12545 fault_info
.cs_bypass
= FALSE
;
12546 fault_info
.mark_zf_absent
= FALSE
;
12547 fault_info
.batch_pmap_op
= FALSE
;
12550 * The MADV_WILLNEED operation doesn't require any changes to the
12551 * vm_map_entry_t's, so the read lock is sufficient.
12554 vm_map_lock_read(map
);
12557 * The madvise semantics require that the address range be fully
12558 * allocated with no holes. Otherwise, we're required to return
12562 if (! vm_map_range_check(map
, start
, end
, &entry
)) {
12563 vm_map_unlock_read(map
);
12564 return KERN_INVALID_ADDRESS
;
12568 * Examine each vm_map_entry_t in the range.
12570 for (; entry
!= vm_map_to_entry(map
) && start
< end
; ) {
12573 * The first time through, the start address could be anywhere
12574 * within the vm_map_entry we found. So adjust the offset to
12575 * correspond. After that, the offset will always be zero to
12576 * correspond to the beginning of the current vm_map_entry.
12578 offset
= (start
- entry
->vme_start
) + VME_OFFSET(entry
);
12581 * Set the length so we don't go beyond the end of the
12582 * map_entry or beyond the end of the range we were given.
12583 * This range could span also multiple map entries all of which
12584 * map different files, so make sure we only do the right amount
12585 * of I/O for each object. Note that it's possible for there
12586 * to be multiple map entries all referring to the same object
12587 * but with different page permissions, but it's not worth
12588 * trying to optimize that case.
12590 len
= MIN(entry
->vme_end
- start
, end
- start
);
12592 if ((vm_size_t
) len
!= len
) {
12593 /* 32-bit overflow */
12594 len
= (vm_size_t
) (0 - PAGE_SIZE
);
12596 fault_info
.cluster_size
= (vm_size_t
) len
;
12597 fault_info
.lo_offset
= offset
;
12598 fault_info
.hi_offset
= offset
+ len
;
12599 fault_info
.user_tag
= VME_ALIAS(entry
);
12600 fault_info
.pmap_options
= 0;
12601 if (entry
->iokit_acct
||
12602 (!entry
->is_sub_map
&& !entry
->use_pmap
)) {
12603 fault_info
.pmap_options
|= PMAP_OPTIONS_ALT_ACCT
;
12607 * If there's no read permission to this mapping, then just
12610 if ((entry
->protection
& VM_PROT_READ
) == 0) {
12611 entry
= entry
->vme_next
;
12612 start
= entry
->vme_start
;
12617 * Find the file object backing this map entry. If there is
12618 * none, then we simply ignore the "will need" advice for this
12619 * entry and go on to the next one.
12621 if ((object
= find_vnode_object(entry
)) == VM_OBJECT_NULL
) {
12622 entry
= entry
->vme_next
;
12623 start
= entry
->vme_start
;
12628 * The data_request() could take a long time, so let's
12629 * release the map lock to avoid blocking other threads.
12631 vm_map_unlock_read(map
);
12633 vm_object_paging_begin(object
);
12634 pager
= object
->pager
;
12635 vm_object_unlock(object
);
12638 * Get the data from the object asynchronously.
12640 * Note that memory_object_data_request() places limits on the
12641 * amount of I/O it will do. Regardless of the len we
12642 * specified, it won't do more than MAX_UPL_TRANSFER_BYTES and it
12643 * silently truncates the len to that size. This isn't
12644 * necessarily bad since madvise shouldn't really be used to
12645 * page in unlimited amounts of data. Other Unix variants
12646 * limit the willneed case as well. If this turns out to be an
12647 * issue for developers, then we can always adjust the policy
12648 * here and still be backwards compatible since this is all
12651 kr
= memory_object_data_request(
12653 offset
+ object
->paging_offset
,
12656 (memory_object_fault_info_t
)&fault_info
);
12658 vm_object_lock(object
);
12659 vm_object_paging_end(object
);
12660 vm_object_unlock(object
);
12663 * If we couldn't do the I/O for some reason, just give up on
12664 * the madvise. We still return success to the user since
12665 * madvise isn't supposed to fail when the advice can't be
12668 if (kr
!= KERN_SUCCESS
) {
12669 return KERN_SUCCESS
;
12673 if (start
>= end
) {
12675 return KERN_SUCCESS
;
12678 /* look up next entry */
12679 vm_map_lock_read(map
);
12680 if (! vm_map_lookup_entry(map
, start
, &entry
)) {
12682 * There's a new hole in the address range.
12684 vm_map_unlock_read(map
);
12685 return KERN_INVALID_ADDRESS
;
12689 vm_map_unlock_read(map
);
12690 return KERN_SUCCESS
;
12694 vm_map_entry_is_reusable(
12695 vm_map_entry_t entry
)
12697 /* Only user map entries */
12699 vm_object_t object
;
12701 if (entry
->is_sub_map
) {
12705 switch (VME_ALIAS(entry
)) {
12706 case VM_MEMORY_MALLOC
:
12707 case VM_MEMORY_MALLOC_SMALL
:
12708 case VM_MEMORY_MALLOC_LARGE
:
12709 case VM_MEMORY_REALLOC
:
12710 case VM_MEMORY_MALLOC_TINY
:
12711 case VM_MEMORY_MALLOC_LARGE_REUSABLE
:
12712 case VM_MEMORY_MALLOC_LARGE_REUSED
:
12714 * This is a malloc() memory region: check if it's still
12715 * in its original state and can be re-used for more
12716 * malloc() allocations.
12721 * Not a malloc() memory region: let the caller decide if
12727 if (entry
->is_shared
||
12728 entry
->is_sub_map
||
12729 entry
->in_transition
||
12730 entry
->protection
!= VM_PROT_DEFAULT
||
12731 entry
->max_protection
!= VM_PROT_ALL
||
12732 entry
->inheritance
!= VM_INHERIT_DEFAULT
||
12734 entry
->permanent
||
12735 entry
->superpage_size
!= FALSE
||
12736 entry
->zero_wired_pages
||
12737 entry
->wired_count
!= 0 ||
12738 entry
->user_wired_count
!= 0) {
12742 object
= VME_OBJECT(entry
);
12743 if (object
== VM_OBJECT_NULL
) {
12749 * Let's proceed even if the VM object is potentially
12751 * We check for this later when processing the actual
12752 * VM pages, so the contents will be safe if shared.
12754 * But we can still mark this memory region as "reusable" to
12755 * acknowledge that the caller did let us know that the memory
12756 * could be re-used and should not be penalized for holding
12757 * on to it. This allows its "resident size" to not include
12758 * the reusable range.
12760 object
->ref_count
== 1 &&
12762 object
->wired_page_count
== 0 &&
12763 object
->copy
== VM_OBJECT_NULL
&&
12764 object
->shadow
== VM_OBJECT_NULL
&&
12765 object
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
&&
12766 object
->internal
&&
12767 !object
->true_share
&&
12768 object
->wimg_bits
== VM_WIMG_USE_DEFAULT
&&
12769 !object
->code_signed
) {
12777 static kern_return_t
12778 vm_map_reuse_pages(
12780 vm_map_offset_t start
,
12781 vm_map_offset_t end
)
12783 vm_map_entry_t entry
;
12784 vm_object_t object
;
12785 vm_object_offset_t start_offset
, end_offset
;
12788 * The MADV_REUSE operation doesn't require any changes to the
12789 * vm_map_entry_t's, so the read lock is sufficient.
12792 vm_map_lock_read(map
);
12793 assert(map
->pmap
!= kernel_pmap
); /* protect alias access */
12796 * The madvise semantics require that the address range be fully
12797 * allocated with no holes. Otherwise, we're required to return
12801 if (!vm_map_range_check(map
, start
, end
, &entry
)) {
12802 vm_map_unlock_read(map
);
12803 vm_page_stats_reusable
.reuse_pages_failure
++;
12804 return KERN_INVALID_ADDRESS
;
12808 * Examine each vm_map_entry_t in the range.
12810 for (; entry
!= vm_map_to_entry(map
) && entry
->vme_start
< end
;
12811 entry
= entry
->vme_next
) {
12813 * Sanity check on the VM map entry.
12815 if (! vm_map_entry_is_reusable(entry
)) {
12816 vm_map_unlock_read(map
);
12817 vm_page_stats_reusable
.reuse_pages_failure
++;
12818 return KERN_INVALID_ADDRESS
;
12822 * The first time through, the start address could be anywhere
12823 * within the vm_map_entry we found. So adjust the offset to
12826 if (entry
->vme_start
< start
) {
12827 start_offset
= start
- entry
->vme_start
;
12831 end_offset
= MIN(end
, entry
->vme_end
) - entry
->vme_start
;
12832 start_offset
+= VME_OFFSET(entry
);
12833 end_offset
+= VME_OFFSET(entry
);
12835 assert(!entry
->is_sub_map
);
12836 object
= VME_OBJECT(entry
);
12837 if (object
!= VM_OBJECT_NULL
) {
12838 vm_object_lock(object
);
12839 vm_object_reuse_pages(object
, start_offset
, end_offset
,
12841 vm_object_unlock(object
);
12844 if (VME_ALIAS(entry
) == VM_MEMORY_MALLOC_LARGE_REUSABLE
) {
12847 * We do not hold the VM map exclusively here.
12848 * The "alias" field is not that critical, so it's
12849 * safe to update it here, as long as it is the only
12850 * one that can be modified while holding the VM map
12853 VME_ALIAS_SET(entry
, VM_MEMORY_MALLOC_LARGE_REUSED
);
12857 vm_map_unlock_read(map
);
12858 vm_page_stats_reusable
.reuse_pages_success
++;
12859 return KERN_SUCCESS
;
12863 static kern_return_t
12864 vm_map_reusable_pages(
12866 vm_map_offset_t start
,
12867 vm_map_offset_t end
)
12869 vm_map_entry_t entry
;
12870 vm_object_t object
;
12871 vm_object_offset_t start_offset
, end_offset
;
12872 vm_map_offset_t pmap_offset
;
12875 * The MADV_REUSABLE operation doesn't require any changes to the
12876 * vm_map_entry_t's, so the read lock is sufficient.
12879 vm_map_lock_read(map
);
12880 assert(map
->pmap
!= kernel_pmap
); /* protect alias access */
12883 * The madvise semantics require that the address range be fully
12884 * allocated with no holes. Otherwise, we're required to return
12888 if (!vm_map_range_check(map
, start
, end
, &entry
)) {
12889 vm_map_unlock_read(map
);
12890 vm_page_stats_reusable
.reusable_pages_failure
++;
12891 return KERN_INVALID_ADDRESS
;
12895 * Examine each vm_map_entry_t in the range.
12897 for (; entry
!= vm_map_to_entry(map
) && entry
->vme_start
< end
;
12898 entry
= entry
->vme_next
) {
12899 int kill_pages
= 0;
12902 * Sanity check on the VM map entry.
12904 if (! vm_map_entry_is_reusable(entry
)) {
12905 vm_map_unlock_read(map
);
12906 vm_page_stats_reusable
.reusable_pages_failure
++;
12907 return KERN_INVALID_ADDRESS
;
12911 * The first time through, the start address could be anywhere
12912 * within the vm_map_entry we found. So adjust the offset to
12915 if (entry
->vme_start
< start
) {
12916 start_offset
= start
- entry
->vme_start
;
12917 pmap_offset
= start
;
12920 pmap_offset
= entry
->vme_start
;
12922 end_offset
= MIN(end
, entry
->vme_end
) - entry
->vme_start
;
12923 start_offset
+= VME_OFFSET(entry
);
12924 end_offset
+= VME_OFFSET(entry
);
12926 assert(!entry
->is_sub_map
);
12927 object
= VME_OBJECT(entry
);
12928 if (object
== VM_OBJECT_NULL
)
12932 vm_object_lock(object
);
12933 if (object
->ref_count
== 1 &&
12936 * "iokit_acct" entries are billed for their virtual size
12937 * (rather than for their resident pages only), so they
12938 * wouldn't benefit from making pages reusable, and it
12939 * would be hard to keep track of pages that are both
12940 * "iokit_acct" and "reusable" in the pmap stats and ledgers.
12942 !(entry
->iokit_acct
||
12943 (!entry
->is_sub_map
&& !entry
->use_pmap
)))
12947 if (kill_pages
!= -1) {
12948 vm_object_deactivate_pages(object
,
12950 end_offset
- start_offset
,
12952 TRUE
/*reusable_pages*/,
12956 vm_page_stats_reusable
.reusable_pages_shared
++;
12958 vm_object_unlock(object
);
12960 if (VME_ALIAS(entry
) == VM_MEMORY_MALLOC_LARGE
||
12961 VME_ALIAS(entry
) == VM_MEMORY_MALLOC_LARGE_REUSED
) {
12964 * We do not hold the VM map exclusively here.
12965 * The "alias" field is not that critical, so it's
12966 * safe to update it here, as long as it is the only
12967 * one that can be modified while holding the VM map
12970 VME_ALIAS_SET(entry
, VM_MEMORY_MALLOC_LARGE_REUSABLE
);
12974 vm_map_unlock_read(map
);
12975 vm_page_stats_reusable
.reusable_pages_success
++;
12976 return KERN_SUCCESS
;
12980 static kern_return_t
12983 vm_map_offset_t start
,
12984 vm_map_offset_t end
)
12986 vm_map_entry_t entry
;
12989 * The MADV_REUSABLE operation doesn't require any changes to the
12990 * vm_map_entry_t's, so the read lock is sufficient.
12993 vm_map_lock_read(map
);
12994 assert(map
->pmap
!= kernel_pmap
); /* protect alias access */
12997 * The madvise semantics require that the address range be fully
12998 * allocated with no holes. Otherwise, we're required to return
13002 if (!vm_map_range_check(map
, start
, end
, &entry
)) {
13003 vm_map_unlock_read(map
);
13004 vm_page_stats_reusable
.can_reuse_failure
++;
13005 return KERN_INVALID_ADDRESS
;
13009 * Examine each vm_map_entry_t in the range.
13011 for (; entry
!= vm_map_to_entry(map
) && entry
->vme_start
< end
;
13012 entry
= entry
->vme_next
) {
13014 * Sanity check on the VM map entry.
13016 if (! vm_map_entry_is_reusable(entry
)) {
13017 vm_map_unlock_read(map
);
13018 vm_page_stats_reusable
.can_reuse_failure
++;
13019 return KERN_INVALID_ADDRESS
;
13023 vm_map_unlock_read(map
);
13024 vm_page_stats_reusable
.can_reuse_success
++;
13025 return KERN_SUCCESS
;
13030 static kern_return_t
13033 vm_map_offset_t start
,
13034 vm_map_offset_t end
)
13036 vm_map_entry_t entry
;
13039 * The MADV_PAGEOUT operation doesn't require any changes to the
13040 * vm_map_entry_t's, so the read lock is sufficient.
13043 vm_map_lock_read(map
);
13046 * The madvise semantics require that the address range be fully
13047 * allocated with no holes. Otherwise, we're required to return
13051 if (!vm_map_range_check(map
, start
, end
, &entry
)) {
13052 vm_map_unlock_read(map
);
13053 return KERN_INVALID_ADDRESS
;
13057 * Examine each vm_map_entry_t in the range.
13059 for (; entry
!= vm_map_to_entry(map
) && entry
->vme_start
< end
;
13060 entry
= entry
->vme_next
) {
13061 vm_object_t object
;
13064 * Sanity check on the VM map entry.
13066 if (entry
->is_sub_map
) {
13068 vm_map_offset_t submap_start
;
13069 vm_map_offset_t submap_end
;
13070 vm_map_entry_t submap_entry
;
13072 submap
= VME_SUBMAP(entry
);
13073 submap_start
= VME_OFFSET(entry
);
13074 submap_end
= submap_start
+ (entry
->vme_end
-
13077 vm_map_lock_read(submap
);
13079 if (! vm_map_range_check(submap
,
13083 vm_map_unlock_read(submap
);
13084 vm_map_unlock_read(map
);
13085 return KERN_INVALID_ADDRESS
;
13088 object
= VME_OBJECT(submap_entry
);
13089 if (submap_entry
->is_sub_map
||
13090 object
== VM_OBJECT_NULL
||
13091 !object
->internal
) {
13092 vm_map_unlock_read(submap
);
13096 vm_object_pageout(object
);
13098 vm_map_unlock_read(submap
);
13099 submap
= VM_MAP_NULL
;
13100 submap_entry
= VM_MAP_ENTRY_NULL
;
13104 object
= VME_OBJECT(entry
);
13105 if (entry
->is_sub_map
||
13106 object
== VM_OBJECT_NULL
||
13107 !object
->internal
) {
13111 vm_object_pageout(object
);
13114 vm_map_unlock_read(map
);
13115 return KERN_SUCCESS
;
13117 #endif /* MACH_ASSERT */
13121 * Routine: vm_map_entry_insert
13123 * Descritpion: This routine inserts a new vm_entry in a locked map.
13126 vm_map_entry_insert(
13128 vm_map_entry_t insp_entry
,
13129 vm_map_offset_t start
,
13130 vm_map_offset_t end
,
13131 vm_object_t object
,
13132 vm_object_offset_t offset
,
13133 boolean_t needs_copy
,
13134 boolean_t is_shared
,
13135 boolean_t in_transition
,
13136 vm_prot_t cur_protection
,
13137 vm_prot_t max_protection
,
13138 vm_behavior_t behavior
,
13139 vm_inherit_t inheritance
,
13140 unsigned wired_count
,
13141 boolean_t no_cache
,
13142 boolean_t permanent
,
13143 unsigned int superpage_size
,
13144 boolean_t clear_map_aligned
,
13145 boolean_t is_submap
)
13147 vm_map_entry_t new_entry
;
13149 assert(insp_entry
!= (vm_map_entry_t
)0);
13151 new_entry
= vm_map_entry_create(map
, !map
->hdr
.entries_pageable
);
13153 if (VM_MAP_PAGE_SHIFT(map
) != PAGE_SHIFT
) {
13154 new_entry
->map_aligned
= TRUE
;
13156 new_entry
->map_aligned
= FALSE
;
13158 if (clear_map_aligned
&&
13159 (! VM_MAP_PAGE_ALIGNED(start
, VM_MAP_PAGE_MASK(map
)) ||
13160 ! VM_MAP_PAGE_ALIGNED(end
, VM_MAP_PAGE_MASK(map
)))) {
13161 new_entry
->map_aligned
= FALSE
;
13164 new_entry
->vme_start
= start
;
13165 new_entry
->vme_end
= end
;
13166 assert(page_aligned(new_entry
->vme_start
));
13167 assert(page_aligned(new_entry
->vme_end
));
13168 if (new_entry
->map_aligned
) {
13169 assert(VM_MAP_PAGE_ALIGNED(new_entry
->vme_start
,
13170 VM_MAP_PAGE_MASK(map
)));
13171 assert(VM_MAP_PAGE_ALIGNED(new_entry
->vme_end
,
13172 VM_MAP_PAGE_MASK(map
)));
13174 assert(new_entry
->vme_start
< new_entry
->vme_end
);
13176 VME_OBJECT_SET(new_entry
, object
);
13177 VME_OFFSET_SET(new_entry
, offset
);
13178 new_entry
->is_shared
= is_shared
;
13179 new_entry
->is_sub_map
= is_submap
;
13180 new_entry
->needs_copy
= needs_copy
;
13181 new_entry
->in_transition
= in_transition
;
13182 new_entry
->needs_wakeup
= FALSE
;
13183 new_entry
->inheritance
= inheritance
;
13184 new_entry
->protection
= cur_protection
;
13185 new_entry
->max_protection
= max_protection
;
13186 new_entry
->behavior
= behavior
;
13187 new_entry
->wired_count
= wired_count
;
13188 new_entry
->user_wired_count
= 0;
13191 * submap: "use_pmap" means "nested".
13194 new_entry
->use_pmap
= FALSE
;
13197 * object: "use_pmap" means "use pmap accounting" for footprint.
13200 new_entry
->use_pmap
= TRUE
;
13202 VME_ALIAS_SET(new_entry
, 0);
13203 new_entry
->zero_wired_pages
= FALSE
;
13204 new_entry
->no_cache
= no_cache
;
13205 new_entry
->permanent
= permanent
;
13206 if (superpage_size
)
13207 new_entry
->superpage_size
= TRUE
;
13209 new_entry
->superpage_size
= FALSE
;
13210 new_entry
->used_for_jit
= FALSE
;
13211 new_entry
->iokit_acct
= FALSE
;
13212 new_entry
->vme_resilient_codesign
= FALSE
;
13213 new_entry
->vme_resilient_media
= FALSE
;
13216 * Insert the new entry into the list.
13219 vm_map_store_entry_link(map
, insp_entry
, new_entry
);
13220 map
->size
+= end
- start
;
13223 * Update the free space hint and the lookup hint.
13226 SAVE_HINT_MAP_WRITE(map
, new_entry
);
13231 * Routine: vm_map_remap_extract
13233 * Descritpion: This routine returns a vm_entry list from a map.
13235 static kern_return_t
13236 vm_map_remap_extract(
13238 vm_map_offset_t addr
,
13239 vm_map_size_t size
,
13241 struct vm_map_header
*map_header
,
13242 vm_prot_t
*cur_protection
,
13243 vm_prot_t
*max_protection
,
13244 /* What, no behavior? */
13245 vm_inherit_t inheritance
,
13246 boolean_t pageable
)
13248 kern_return_t result
;
13249 vm_map_size_t mapped_size
;
13250 vm_map_size_t tmp_size
;
13251 vm_map_entry_t src_entry
; /* result of last map lookup */
13252 vm_map_entry_t new_entry
;
13253 vm_object_offset_t offset
;
13254 vm_map_offset_t map_address
;
13255 vm_map_offset_t src_start
; /* start of entry to map */
13256 vm_map_offset_t src_end
; /* end of region to be mapped */
13257 vm_object_t object
;
13258 vm_map_version_t version
;
13259 boolean_t src_needs_copy
;
13260 boolean_t new_entry_needs_copy
;
13262 assert(map
!= VM_MAP_NULL
);
13264 assert(size
== vm_map_round_page(size
, PAGE_MASK
));
13265 assert(inheritance
== VM_INHERIT_NONE
||
13266 inheritance
== VM_INHERIT_COPY
||
13267 inheritance
== VM_INHERIT_SHARE
);
13270 * Compute start and end of region.
13272 src_start
= vm_map_trunc_page(addr
, PAGE_MASK
);
13273 src_end
= vm_map_round_page(src_start
+ size
, PAGE_MASK
);
13277 * Initialize map_header.
13279 map_header
->links
.next
= (struct vm_map_entry
*)&map_header
->links
;
13280 map_header
->links
.prev
= (struct vm_map_entry
*)&map_header
->links
;
13281 map_header
->nentries
= 0;
13282 map_header
->entries_pageable
= pageable
;
13283 map_header
->page_shift
= PAGE_SHIFT
;
13285 vm_map_store_init( map_header
);
13287 *cur_protection
= VM_PROT_ALL
;
13288 *max_protection
= VM_PROT_ALL
;
13292 result
= KERN_SUCCESS
;
13295 * The specified source virtual space might correspond to
13296 * multiple map entries, need to loop on them.
13299 while (mapped_size
!= size
) {
13300 vm_map_size_t entry_size
;
13303 * Find the beginning of the region.
13305 if (! vm_map_lookup_entry(map
, src_start
, &src_entry
)) {
13306 result
= KERN_INVALID_ADDRESS
;
13310 if (src_start
< src_entry
->vme_start
||
13311 (mapped_size
&& src_start
!= src_entry
->vme_start
)) {
13312 result
= KERN_INVALID_ADDRESS
;
13316 tmp_size
= size
- mapped_size
;
13317 if (src_end
> src_entry
->vme_end
)
13318 tmp_size
-= (src_end
- src_entry
->vme_end
);
13320 entry_size
= (vm_map_size_t
)(src_entry
->vme_end
-
13321 src_entry
->vme_start
);
13323 if(src_entry
->is_sub_map
) {
13324 vm_map_reference(VME_SUBMAP(src_entry
));
13325 object
= VM_OBJECT_NULL
;
13327 object
= VME_OBJECT(src_entry
);
13328 if (src_entry
->iokit_acct
) {
13330 * This entry uses "IOKit accounting".
13332 } else if (object
!= VM_OBJECT_NULL
&&
13333 object
->purgable
!= VM_PURGABLE_DENY
) {
13335 * Purgeable objects have their own accounting:
13336 * no pmap accounting for them.
13338 assert(!src_entry
->use_pmap
);
13341 * Not IOKit or purgeable:
13342 * must be accounted by pmap stats.
13344 assert(src_entry
->use_pmap
);
13347 if (object
== VM_OBJECT_NULL
) {
13348 object
= vm_object_allocate(entry_size
);
13349 VME_OFFSET_SET(src_entry
, 0);
13350 VME_OBJECT_SET(src_entry
, object
);
13351 } else if (object
->copy_strategy
!=
13352 MEMORY_OBJECT_COPY_SYMMETRIC
) {
13354 * We are already using an asymmetric
13355 * copy, and therefore we already have
13356 * the right object.
13358 assert(!src_entry
->needs_copy
);
13359 } else if (src_entry
->needs_copy
|| object
->shadowed
||
13360 (object
->internal
&& !object
->true_share
&&
13361 !src_entry
->is_shared
&&
13362 object
->vo_size
> entry_size
)) {
13364 VME_OBJECT_SHADOW(src_entry
, entry_size
);
13366 if (!src_entry
->needs_copy
&&
13367 (src_entry
->protection
& VM_PROT_WRITE
)) {
13370 prot
= src_entry
->protection
& ~VM_PROT_WRITE
;
13372 if (override_nx(map
,
13373 VME_ALIAS(src_entry
))
13375 prot
|= VM_PROT_EXECUTE
;
13377 if(map
->mapped_in_other_pmaps
) {
13378 vm_object_pmap_protect(
13379 VME_OBJECT(src_entry
),
13380 VME_OFFSET(src_entry
),
13383 src_entry
->vme_start
,
13386 pmap_protect(vm_map_pmap(map
),
13387 src_entry
->vme_start
,
13388 src_entry
->vme_end
,
13393 object
= VME_OBJECT(src_entry
);
13394 src_entry
->needs_copy
= FALSE
;
13398 vm_object_lock(object
);
13399 vm_object_reference_locked(object
); /* object ref. for new entry */
13400 if (object
->copy_strategy
==
13401 MEMORY_OBJECT_COPY_SYMMETRIC
) {
13402 object
->copy_strategy
=
13403 MEMORY_OBJECT_COPY_DELAY
;
13405 vm_object_unlock(object
);
13408 offset
= (VME_OFFSET(src_entry
) +
13409 (src_start
- src_entry
->vme_start
));
13411 new_entry
= _vm_map_entry_create(map_header
, !map_header
->entries_pageable
);
13412 vm_map_entry_copy(new_entry
, src_entry
);
13413 if (new_entry
->is_sub_map
) {
13414 /* clr address space specifics */
13415 new_entry
->use_pmap
= FALSE
;
13418 new_entry
->map_aligned
= FALSE
;
13420 new_entry
->vme_start
= map_address
;
13421 new_entry
->vme_end
= map_address
+ tmp_size
;
13422 assert(new_entry
->vme_start
< new_entry
->vme_end
);
13423 new_entry
->inheritance
= inheritance
;
13424 VME_OFFSET_SET(new_entry
, offset
);
13427 * The new region has to be copied now if required.
13432 * Cannot allow an entry describing a JIT
13433 * region to be shared across address spaces.
13435 if (src_entry
->used_for_jit
== TRUE
) {
13436 result
= KERN_INVALID_ARGUMENT
;
13439 src_entry
->is_shared
= TRUE
;
13440 new_entry
->is_shared
= TRUE
;
13441 if (!(new_entry
->is_sub_map
))
13442 new_entry
->needs_copy
= FALSE
;
13444 } else if (src_entry
->is_sub_map
) {
13445 /* make this a COW sub_map if not already */
13446 assert(new_entry
->wired_count
== 0);
13447 new_entry
->needs_copy
= TRUE
;
13448 object
= VM_OBJECT_NULL
;
13449 } else if (src_entry
->wired_count
== 0 &&
13450 vm_object_copy_quickly(&VME_OBJECT(new_entry
),
13451 VME_OFFSET(new_entry
),
13452 (new_entry
->vme_end
-
13453 new_entry
->vme_start
),
13455 &new_entry_needs_copy
)) {
13457 new_entry
->needs_copy
= new_entry_needs_copy
;
13458 new_entry
->is_shared
= FALSE
;
13461 * Handle copy_on_write semantics.
13463 if (src_needs_copy
&& !src_entry
->needs_copy
) {
13466 prot
= src_entry
->protection
& ~VM_PROT_WRITE
;
13468 if (override_nx(map
,
13469 VME_ALIAS(src_entry
))
13471 prot
|= VM_PROT_EXECUTE
;
13473 vm_object_pmap_protect(object
,
13476 ((src_entry
->is_shared
13477 || map
->mapped_in_other_pmaps
) ?
13478 PMAP_NULL
: map
->pmap
),
13479 src_entry
->vme_start
,
13482 assert(src_entry
->wired_count
== 0);
13483 src_entry
->needs_copy
= TRUE
;
13486 * Throw away the old object reference of the new entry.
13488 vm_object_deallocate(object
);
13491 new_entry
->is_shared
= FALSE
;
13494 * The map can be safely unlocked since we
13495 * already hold a reference on the object.
13497 * Record the timestamp of the map for later
13498 * verification, and unlock the map.
13500 version
.main_timestamp
= map
->timestamp
;
13501 vm_map_unlock(map
); /* Increments timestamp once! */
13504 * Perform the copy.
13506 if (src_entry
->wired_count
> 0) {
13507 vm_object_lock(object
);
13508 result
= vm_object_copy_slowly(
13513 &VME_OBJECT(new_entry
));
13515 VME_OFFSET_SET(new_entry
, 0);
13516 new_entry
->needs_copy
= FALSE
;
13518 vm_object_offset_t new_offset
;
13520 new_offset
= VME_OFFSET(new_entry
);
13521 result
= vm_object_copy_strategically(
13525 &VME_OBJECT(new_entry
),
13527 &new_entry_needs_copy
);
13528 if (new_offset
!= VME_OFFSET(new_entry
)) {
13529 VME_OFFSET_SET(new_entry
, new_offset
);
13532 new_entry
->needs_copy
= new_entry_needs_copy
;
13536 * Throw away the old object reference of the new entry.
13538 vm_object_deallocate(object
);
13540 if (result
!= KERN_SUCCESS
&&
13541 result
!= KERN_MEMORY_RESTART_COPY
) {
13542 _vm_map_entry_dispose(map_header
, new_entry
);
13547 * Verify that the map has not substantially
13548 * changed while the copy was being made.
13552 if (version
.main_timestamp
+ 1 != map
->timestamp
) {
13554 * Simple version comparison failed.
13556 * Retry the lookup and verify that the
13557 * same object/offset are still present.
13559 vm_object_deallocate(VME_OBJECT(new_entry
));
13560 _vm_map_entry_dispose(map_header
, new_entry
);
13561 if (result
== KERN_MEMORY_RESTART_COPY
)
13562 result
= KERN_SUCCESS
;
13566 if (result
== KERN_MEMORY_RESTART_COPY
) {
13567 vm_object_reference(object
);
13572 _vm_map_store_entry_link(map_header
,
13573 map_header
->links
.prev
, new_entry
);
13575 /*Protections for submap mapping are irrelevant here*/
13576 if( !src_entry
->is_sub_map
) {
13577 *cur_protection
&= src_entry
->protection
;
13578 *max_protection
&= src_entry
->max_protection
;
13580 map_address
+= tmp_size
;
13581 mapped_size
+= tmp_size
;
13582 src_start
+= tmp_size
;
13586 vm_map_unlock(map
);
13587 if (result
!= KERN_SUCCESS
) {
13589 * Free all allocated elements.
13591 for (src_entry
= map_header
->links
.next
;
13592 src_entry
!= (struct vm_map_entry
*)&map_header
->links
;
13593 src_entry
= new_entry
) {
13594 new_entry
= src_entry
->vme_next
;
13595 _vm_map_store_entry_unlink(map_header
, src_entry
);
13596 if (src_entry
->is_sub_map
) {
13597 vm_map_deallocate(VME_SUBMAP(src_entry
));
13599 vm_object_deallocate(VME_OBJECT(src_entry
));
13601 _vm_map_entry_dispose(map_header
, src_entry
);
13608 * Routine: vm_remap
13610 * Map portion of a task's address space.
13611 * Mapped region must not overlap more than
13612 * one vm memory object. Protections and
13613 * inheritance attributes remain the same
13614 * as in the original task and are out parameters.
13615 * Source and Target task can be identical
13616 * Other attributes are identical as for vm_map()
13620 vm_map_t target_map
,
13621 vm_map_address_t
*address
,
13622 vm_map_size_t size
,
13623 vm_map_offset_t mask
,
13626 vm_map_offset_t memory_address
,
13628 vm_prot_t
*cur_protection
,
13629 vm_prot_t
*max_protection
,
13630 vm_inherit_t inheritance
)
13632 kern_return_t result
;
13633 vm_map_entry_t entry
;
13634 vm_map_entry_t insp_entry
= VM_MAP_ENTRY_NULL
;
13635 vm_map_entry_t new_entry
;
13636 struct vm_map_header map_header
;
13637 vm_map_offset_t offset_in_mapping
;
13639 if (target_map
== VM_MAP_NULL
)
13640 return KERN_INVALID_ARGUMENT
;
13642 switch (inheritance
) {
13643 case VM_INHERIT_NONE
:
13644 case VM_INHERIT_COPY
:
13645 case VM_INHERIT_SHARE
:
13646 if (size
!= 0 && src_map
!= VM_MAP_NULL
)
13650 return KERN_INVALID_ARGUMENT
;
13654 * If the user is requesting that we return the address of the
13655 * first byte of the data (rather than the base of the page),
13656 * then we use different rounding semantics: specifically,
13657 * we assume that (memory_address, size) describes a region
13658 * all of whose pages we must cover, rather than a base to be truncated
13659 * down and a size to be added to that base. So we figure out
13660 * the highest page that the requested region includes and make
13661 * sure that the size will cover it.
13663 * The key example we're worried about it is of the form:
13665 * memory_address = 0x1ff0, size = 0x20
13667 * With the old semantics, we round down the memory_address to 0x1000
13668 * and round up the size to 0x1000, resulting in our covering *only*
13669 * page 0x1000. With the new semantics, we'd realize that the region covers
13670 * 0x1ff0-0x2010, and compute a size of 0x2000. Thus, we cover both page
13671 * 0x1000 and page 0x2000 in the region we remap.
13673 if ((flags
& VM_FLAGS_RETURN_DATA_ADDR
) != 0) {
13674 offset_in_mapping
= memory_address
- vm_map_trunc_page(memory_address
, PAGE_MASK
);
13675 size
= vm_map_round_page(memory_address
+ size
- vm_map_trunc_page(memory_address
, PAGE_MASK
), PAGE_MASK
);
13677 size
= vm_map_round_page(size
, PAGE_MASK
);
13680 result
= vm_map_remap_extract(src_map
, memory_address
,
13681 size
, copy
, &map_header
,
13685 target_map
->hdr
.entries_pageable
);
13687 if (result
!= KERN_SUCCESS
) {
13692 * Allocate/check a range of free virtual address
13693 * space for the target
13695 *address
= vm_map_trunc_page(*address
,
13696 VM_MAP_PAGE_MASK(target_map
));
13697 vm_map_lock(target_map
);
13698 result
= vm_map_remap_range_allocate(target_map
, address
, size
,
13699 mask
, flags
, &insp_entry
);
13701 for (entry
= map_header
.links
.next
;
13702 entry
!= (struct vm_map_entry
*)&map_header
.links
;
13703 entry
= new_entry
) {
13704 new_entry
= entry
->vme_next
;
13705 _vm_map_store_entry_unlink(&map_header
, entry
);
13706 if (result
== KERN_SUCCESS
) {
13707 if (flags
& VM_FLAGS_RESILIENT_CODESIGN
) {
13708 /* no codesigning -> read-only access */
13709 assert(!entry
->used_for_jit
);
13710 entry
->max_protection
= VM_PROT_READ
;
13711 entry
->protection
= VM_PROT_READ
;
13712 entry
->vme_resilient_codesign
= TRUE
;
13714 entry
->vme_start
+= *address
;
13715 entry
->vme_end
+= *address
;
13716 assert(!entry
->map_aligned
);
13717 vm_map_store_entry_link(target_map
, insp_entry
, entry
);
13718 insp_entry
= entry
;
13720 if (!entry
->is_sub_map
) {
13721 vm_object_deallocate(VME_OBJECT(entry
));
13723 vm_map_deallocate(VME_SUBMAP(entry
));
13725 _vm_map_entry_dispose(&map_header
, entry
);
13729 if (flags
& VM_FLAGS_RESILIENT_CODESIGN
) {
13730 *cur_protection
= VM_PROT_READ
;
13731 *max_protection
= VM_PROT_READ
;
13734 if( target_map
->disable_vmentry_reuse
== TRUE
) {
13735 if( target_map
->highest_entry_end
< insp_entry
->vme_end
){
13736 target_map
->highest_entry_end
= insp_entry
->vme_end
;
13740 if (result
== KERN_SUCCESS
) {
13741 target_map
->size
+= size
;
13742 SAVE_HINT_MAP_WRITE(target_map
, insp_entry
);
13744 vm_map_unlock(target_map
);
13746 if (result
== KERN_SUCCESS
&& target_map
->wiring_required
)
13747 result
= vm_map_wire(target_map
, *address
,
13748 *address
+ size
, *cur_protection
| VM_PROT_MEMORY_TAG_MAKE(VM_KERN_MEMORY_MLOCK
),
13752 * If requested, return the address of the data pointed to by the
13753 * request, rather than the base of the resulting page.
13755 if ((flags
& VM_FLAGS_RETURN_DATA_ADDR
) != 0) {
13756 *address
+= offset_in_mapping
;
13763 * Routine: vm_map_remap_range_allocate
13766 * Allocate a range in the specified virtual address map.
13767 * returns the address and the map entry just before the allocated
13770 * Map must be locked.
13773 static kern_return_t
13774 vm_map_remap_range_allocate(
13776 vm_map_address_t
*address
, /* IN/OUT */
13777 vm_map_size_t size
,
13778 vm_map_offset_t mask
,
13780 vm_map_entry_t
*map_entry
) /* OUT */
13782 vm_map_entry_t entry
;
13783 vm_map_offset_t start
;
13784 vm_map_offset_t end
;
13786 vm_map_entry_t hole_entry
;
13792 if (flags
& VM_FLAGS_ANYWHERE
)
13795 * Calculate the first possible address.
13798 if (start
< map
->min_offset
)
13799 start
= map
->min_offset
;
13800 if (start
> map
->max_offset
)
13801 return(KERN_NO_SPACE
);
13804 * Look for the first possible address;
13805 * if there's already something at this
13806 * address, we have to start after it.
13809 if( map
->disable_vmentry_reuse
== TRUE
) {
13810 VM_MAP_HIGHEST_ENTRY(map
, entry
, start
);
13813 if (map
->holelistenabled
) {
13814 hole_entry
= (vm_map_entry_t
)map
->holes_list
;
13816 if (hole_entry
== NULL
) {
13818 * No more space in the map?
13820 return(KERN_NO_SPACE
);
13823 boolean_t found_hole
= FALSE
;
13826 if (hole_entry
->vme_start
>= start
) {
13827 start
= hole_entry
->vme_start
;
13832 if (hole_entry
->vme_end
> start
) {
13836 hole_entry
= hole_entry
->vme_next
;
13838 } while (hole_entry
!= (vm_map_entry_t
) map
->holes_list
);
13840 if (found_hole
== FALSE
) {
13841 return (KERN_NO_SPACE
);
13844 entry
= hole_entry
;
13847 assert(first_free_is_valid(map
));
13848 if (start
== map
->min_offset
) {
13849 if ((entry
= map
->first_free
) != vm_map_to_entry(map
))
13850 start
= entry
->vme_end
;
13852 vm_map_entry_t tmp_entry
;
13853 if (vm_map_lookup_entry(map
, start
, &tmp_entry
))
13854 start
= tmp_entry
->vme_end
;
13858 start
= vm_map_round_page(start
,
13859 VM_MAP_PAGE_MASK(map
));
13863 * In any case, the "entry" always precedes
13864 * the proposed new region throughout the
13869 register vm_map_entry_t next
;
13872 * Find the end of the proposed new region.
13873 * Be sure we didn't go beyond the end, or
13874 * wrap around the address.
13877 end
= ((start
+ mask
) & ~mask
);
13878 end
= vm_map_round_page(end
,
13879 VM_MAP_PAGE_MASK(map
));
13881 return(KERN_NO_SPACE
);
13885 if ((end
> map
->max_offset
) || (end
< start
)) {
13886 if (map
->wait_for_space
) {
13887 if (size
<= (map
->max_offset
-
13888 map
->min_offset
)) {
13889 assert_wait((event_t
) map
, THREAD_INTERRUPTIBLE
);
13890 vm_map_unlock(map
);
13891 thread_block(THREAD_CONTINUE_NULL
);
13897 return(KERN_NO_SPACE
);
13900 next
= entry
->vme_next
;
13902 if (map
->holelistenabled
) {
13903 if (entry
->vme_end
>= end
)
13907 * If there are no more entries, we must win.
13911 * If there is another entry, it must be
13912 * after the end of the potential new region.
13915 if (next
== vm_map_to_entry(map
))
13918 if (next
->vme_start
>= end
)
13923 * Didn't fit -- move to the next entry.
13928 if (map
->holelistenabled
) {
13929 if (entry
== (vm_map_entry_t
) map
->holes_list
) {
13933 return(KERN_NO_SPACE
);
13935 start
= entry
->vme_start
;
13937 start
= entry
->vme_end
;
13941 if (map
->holelistenabled
) {
13943 if (vm_map_lookup_entry(map
, entry
->vme_start
, &entry
)) {
13944 panic("Found an existing entry (%p) instead of potential hole at address: 0x%llx.\n", entry
, (unsigned long long)entry
->vme_start
);
13951 vm_map_entry_t temp_entry
;
13955 * the address doesn't itself violate
13956 * the mask requirement.
13959 if ((start
& mask
) != 0)
13960 return(KERN_NO_SPACE
);
13964 * ... the address is within bounds
13967 end
= start
+ size
;
13969 if ((start
< map
->min_offset
) ||
13970 (end
> map
->max_offset
) ||
13972 return(KERN_INVALID_ADDRESS
);
13976 * If we're asked to overwrite whatever was mapped in that
13977 * range, first deallocate that range.
13979 if (flags
& VM_FLAGS_OVERWRITE
) {
13983 * We use a "zap_map" to avoid having to unlock
13984 * the "map" in vm_map_delete(), which would compromise
13985 * the atomicity of the "deallocate" and then "remap"
13988 zap_map
= vm_map_create(PMAP_NULL
,
13991 map
->hdr
.entries_pageable
);
13992 if (zap_map
== VM_MAP_NULL
) {
13993 return KERN_RESOURCE_SHORTAGE
;
13995 vm_map_set_page_shift(zap_map
, VM_MAP_PAGE_SHIFT(map
));
13996 vm_map_disable_hole_optimization(zap_map
);
13998 kr
= vm_map_delete(map
, start
, end
,
13999 (VM_MAP_REMOVE_SAVE_ENTRIES
|
14000 VM_MAP_REMOVE_NO_MAP_ALIGN
),
14002 if (kr
== KERN_SUCCESS
) {
14003 vm_map_destroy(zap_map
,
14004 VM_MAP_REMOVE_NO_PMAP_CLEANUP
);
14005 zap_map
= VM_MAP_NULL
;
14010 * ... the starting address isn't allocated
14013 if (vm_map_lookup_entry(map
, start
, &temp_entry
))
14014 return(KERN_NO_SPACE
);
14016 entry
= temp_entry
;
14019 * ... the next region doesn't overlap the
14023 if ((entry
->vme_next
!= vm_map_to_entry(map
)) &&
14024 (entry
->vme_next
->vme_start
< end
))
14025 return(KERN_NO_SPACE
);
14027 *map_entry
= entry
;
14028 return(KERN_SUCCESS
);
14034 * Set the address map for the current thread to the specified map
14042 thread_t thread
= current_thread();
14043 vm_map_t oldmap
= thread
->map
;
14045 mp_disable_preemption();
14046 mycpu
= cpu_number();
14049 * Deactivate the current map and activate the requested map
14051 PMAP_SWITCH_USER(thread
, map
, mycpu
);
14053 mp_enable_preemption();
14059 * Routine: vm_map_write_user
14062 * Copy out data from a kernel space into space in the
14063 * destination map. The space must already exist in the
14065 * NOTE: This routine should only be called by threads
14066 * which can block on a page fault. i.e. kernel mode user
14074 vm_map_address_t dst_addr
,
14077 kern_return_t kr
= KERN_SUCCESS
;
14079 if(current_map() == map
) {
14080 if (copyout(src_p
, dst_addr
, size
)) {
14081 kr
= KERN_INVALID_ADDRESS
;
14086 /* take on the identity of the target map while doing */
14089 vm_map_reference(map
);
14090 oldmap
= vm_map_switch(map
);
14091 if (copyout(src_p
, dst_addr
, size
)) {
14092 kr
= KERN_INVALID_ADDRESS
;
14094 vm_map_switch(oldmap
);
14095 vm_map_deallocate(map
);
14101 * Routine: vm_map_read_user
14104 * Copy in data from a user space source map into the
14105 * kernel map. The space must already exist in the
14107 * NOTE: This routine should only be called by threads
14108 * which can block on a page fault. i.e. kernel mode user
14115 vm_map_address_t src_addr
,
14119 kern_return_t kr
= KERN_SUCCESS
;
14121 if(current_map() == map
) {
14122 if (copyin(src_addr
, dst_p
, size
)) {
14123 kr
= KERN_INVALID_ADDRESS
;
14128 /* take on the identity of the target map while doing */
14131 vm_map_reference(map
);
14132 oldmap
= vm_map_switch(map
);
14133 if (copyin(src_addr
, dst_p
, size
)) {
14134 kr
= KERN_INVALID_ADDRESS
;
14136 vm_map_switch(oldmap
);
14137 vm_map_deallocate(map
);
14144 * vm_map_check_protection:
14146 * Assert that the target map allows the specified
14147 * privilege on the entire address region given.
14148 * The entire region must be allocated.
14151 vm_map_check_protection(vm_map_t map
, vm_map_offset_t start
,
14152 vm_map_offset_t end
, vm_prot_t protection
)
14154 vm_map_entry_t entry
;
14155 vm_map_entry_t tmp_entry
;
14159 if (start
< vm_map_min(map
) || end
> vm_map_max(map
) || start
> end
)
14161 vm_map_unlock(map
);
14165 if (!vm_map_lookup_entry(map
, start
, &tmp_entry
)) {
14166 vm_map_unlock(map
);
14172 while (start
< end
) {
14173 if (entry
== vm_map_to_entry(map
)) {
14174 vm_map_unlock(map
);
14179 * No holes allowed!
14182 if (start
< entry
->vme_start
) {
14183 vm_map_unlock(map
);
14188 * Check protection associated with entry.
14191 if ((entry
->protection
& protection
) != protection
) {
14192 vm_map_unlock(map
);
14196 /* go to next entry */
14198 start
= entry
->vme_end
;
14199 entry
= entry
->vme_next
;
14201 vm_map_unlock(map
);
14206 vm_map_purgable_control(
14208 vm_map_offset_t address
,
14209 vm_purgable_t control
,
14212 vm_map_entry_t entry
;
14213 vm_object_t object
;
14215 boolean_t was_nonvolatile
;
14218 * Vet all the input parameters and current type and state of the
14219 * underlaying object. Return with an error if anything is amiss.
14221 if (map
== VM_MAP_NULL
)
14222 return(KERN_INVALID_ARGUMENT
);
14224 if (control
!= VM_PURGABLE_SET_STATE
&&
14225 control
!= VM_PURGABLE_GET_STATE
&&
14226 control
!= VM_PURGABLE_PURGE_ALL
)
14227 return(KERN_INVALID_ARGUMENT
);
14229 if (control
== VM_PURGABLE_PURGE_ALL
) {
14230 vm_purgeable_object_purge_all();
14231 return KERN_SUCCESS
;
14234 if (control
== VM_PURGABLE_SET_STATE
&&
14235 (((*state
& ~(VM_PURGABLE_ALL_MASKS
)) != 0) ||
14236 ((*state
& VM_PURGABLE_STATE_MASK
) > VM_PURGABLE_STATE_MASK
)))
14237 return(KERN_INVALID_ARGUMENT
);
14239 vm_map_lock_read(map
);
14241 if (!vm_map_lookup_entry(map
, address
, &entry
) || entry
->is_sub_map
) {
14244 * Must pass a valid non-submap address.
14246 vm_map_unlock_read(map
);
14247 return(KERN_INVALID_ADDRESS
);
14250 if ((entry
->protection
& VM_PROT_WRITE
) == 0) {
14252 * Can't apply purgable controls to something you can't write.
14254 vm_map_unlock_read(map
);
14255 return(KERN_PROTECTION_FAILURE
);
14258 object
= VME_OBJECT(entry
);
14259 if (object
== VM_OBJECT_NULL
||
14260 object
->purgable
== VM_PURGABLE_DENY
) {
14262 * Object must already be present and be purgeable.
14264 vm_map_unlock_read(map
);
14265 return KERN_INVALID_ARGUMENT
;
14268 vm_object_lock(object
);
14271 if (VME_OFFSET(entry
) != 0 ||
14272 entry
->vme_end
- entry
->vme_start
!= object
->vo_size
) {
14274 * Can only apply purgable controls to the whole (existing)
14277 vm_map_unlock_read(map
);
14278 vm_object_unlock(object
);
14279 return KERN_INVALID_ARGUMENT
;
14283 assert(!entry
->is_sub_map
);
14284 assert(!entry
->use_pmap
); /* purgeable has its own accounting */
14286 vm_map_unlock_read(map
);
14288 was_nonvolatile
= (object
->purgable
== VM_PURGABLE_NONVOLATILE
);
14290 kr
= vm_object_purgable_control(object
, control
, state
);
14292 if (was_nonvolatile
&&
14293 object
->purgable
!= VM_PURGABLE_NONVOLATILE
&&
14294 map
->pmap
== kernel_pmap
) {
14296 object
->vo_purgeable_volatilizer
= kernel_task
;
14300 vm_object_unlock(object
);
14306 vm_map_page_query_internal(
14307 vm_map_t target_map
,
14308 vm_map_offset_t offset
,
14313 vm_page_info_basic_data_t info
;
14314 mach_msg_type_number_t count
;
14316 count
= VM_PAGE_INFO_BASIC_COUNT
;
14317 kr
= vm_map_page_info(target_map
,
14319 VM_PAGE_INFO_BASIC
,
14320 (vm_page_info_t
) &info
,
14322 if (kr
== KERN_SUCCESS
) {
14323 *disposition
= info
.disposition
;
14324 *ref_count
= info
.ref_count
;
14336 vm_map_offset_t offset
,
14337 vm_page_info_flavor_t flavor
,
14338 vm_page_info_t info
,
14339 mach_msg_type_number_t
*count
)
14341 vm_map_entry_t map_entry
;
14342 vm_object_t object
;
14345 kern_return_t retval
= KERN_SUCCESS
;
14346 boolean_t top_object
;
14349 vm_page_info_basic_t basic_info
;
14351 vm_map_offset_t offset_in_page
;
14354 case VM_PAGE_INFO_BASIC
:
14355 if (*count
!= VM_PAGE_INFO_BASIC_COUNT
) {
14357 * The "vm_page_info_basic_data" structure was not
14358 * properly padded, so allow the size to be off by
14359 * one to maintain backwards binary compatibility...
14361 if (*count
!= VM_PAGE_INFO_BASIC_COUNT
- 1)
14362 return KERN_INVALID_ARGUMENT
;
14366 return KERN_INVALID_ARGUMENT
;
14374 retval
= KERN_SUCCESS
;
14375 offset_in_page
= offset
& PAGE_MASK
;
14376 offset
= vm_map_trunc_page(offset
, PAGE_MASK
);
14378 vm_map_lock_read(map
);
14381 * First, find the map entry covering "offset", going down
14382 * submaps if necessary.
14385 if (!vm_map_lookup_entry(map
, offset
, &map_entry
)) {
14386 vm_map_unlock_read(map
);
14387 return KERN_INVALID_ADDRESS
;
14389 /* compute offset from this map entry's start */
14390 offset
-= map_entry
->vme_start
;
14391 /* compute offset into this map entry's object (or submap) */
14392 offset
+= VME_OFFSET(map_entry
);
14394 if (map_entry
->is_sub_map
) {
14397 sub_map
= VME_SUBMAP(map_entry
);
14398 vm_map_lock_read(sub_map
);
14399 vm_map_unlock_read(map
);
14403 ref_count
= MAX(ref_count
, map
->ref_count
);
14409 object
= VME_OBJECT(map_entry
);
14410 if (object
== VM_OBJECT_NULL
) {
14411 /* no object -> no page */
14412 vm_map_unlock_read(map
);
14416 vm_object_lock(object
);
14417 vm_map_unlock_read(map
);
14420 * Go down the VM object shadow chain until we find the page
14421 * we're looking for.
14424 ref_count
= MAX(ref_count
, object
->ref_count
);
14426 m
= vm_page_lookup(object
, offset
);
14428 if (m
!= VM_PAGE_NULL
) {
14429 disposition
|= VM_PAGE_QUERY_PAGE_PRESENT
;
14433 if (object
->existence_map
) {
14434 if (vm_external_state_get(object
->existence_map
,
14436 VM_EXTERNAL_STATE_EXISTS
) {
14438 * this page has been paged out
14440 disposition
|= VM_PAGE_QUERY_PAGE_PAGED_OUT
;
14445 if (object
->internal
&&
14447 !object
->terminating
&&
14448 object
->pager_ready
) {
14450 if (COMPRESSED_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE
) {
14451 if (VM_COMPRESSOR_PAGER_STATE_GET(
14454 == VM_EXTERNAL_STATE_EXISTS
) {
14455 /* the pager has that page */
14456 disposition
|= VM_PAGE_QUERY_PAGE_PAGED_OUT
;
14460 memory_object_t pager
;
14462 vm_object_paging_begin(object
);
14463 pager
= object
->pager
;
14464 vm_object_unlock(object
);
14467 * Ask the default pager if
14468 * it has this page.
14470 kr
= memory_object_data_request(
14472 offset
+ object
->paging_offset
,
14473 0, /* just poke the pager */
14477 vm_object_lock(object
);
14478 vm_object_paging_end(object
);
14480 if (kr
== KERN_SUCCESS
) {
14481 /* the default pager has it */
14482 disposition
|= VM_PAGE_QUERY_PAGE_PAGED_OUT
;
14488 if (object
->shadow
!= VM_OBJECT_NULL
) {
14489 vm_object_t shadow
;
14491 offset
+= object
->vo_shadow_offset
;
14492 shadow
= object
->shadow
;
14494 vm_object_lock(shadow
);
14495 vm_object_unlock(object
);
14498 top_object
= FALSE
;
14501 // if (!object->internal)
14503 // retval = KERN_FAILURE;
14504 // goto done_with_object;
14509 /* The ref_count is not strictly accurate, it measures the number */
14510 /* of entities holding a ref on the object, they may not be mapping */
14511 /* the object or may not be mapping the section holding the */
14512 /* target page but its still a ball park number and though an over- */
14513 /* count, it picks up the copy-on-write cases */
14515 /* We could also get a picture of page sharing from pmap_attributes */
14516 /* but this would under count as only faulted-in mappings would */
14519 if (top_object
== TRUE
&& object
->shadow
)
14520 disposition
|= VM_PAGE_QUERY_PAGE_COPIED
;
14522 if (! object
->internal
)
14523 disposition
|= VM_PAGE_QUERY_PAGE_EXTERNAL
;
14525 if (m
== VM_PAGE_NULL
)
14526 goto done_with_object
;
14528 if (m
->fictitious
) {
14529 disposition
|= VM_PAGE_QUERY_PAGE_FICTITIOUS
;
14530 goto done_with_object
;
14532 if (m
->dirty
|| pmap_is_modified(m
->phys_page
))
14533 disposition
|= VM_PAGE_QUERY_PAGE_DIRTY
;
14535 if (m
->reference
|| pmap_is_referenced(m
->phys_page
))
14536 disposition
|= VM_PAGE_QUERY_PAGE_REF
;
14538 if (m
->speculative
)
14539 disposition
|= VM_PAGE_QUERY_PAGE_SPECULATIVE
;
14541 if (m
->cs_validated
)
14542 disposition
|= VM_PAGE_QUERY_PAGE_CS_VALIDATED
;
14544 disposition
|= VM_PAGE_QUERY_PAGE_CS_TAINTED
;
14546 disposition
|= VM_PAGE_QUERY_PAGE_CS_NX
;
14549 vm_object_unlock(object
);
14553 case VM_PAGE_INFO_BASIC
:
14554 basic_info
= (vm_page_info_basic_t
) info
;
14555 basic_info
->disposition
= disposition
;
14556 basic_info
->ref_count
= ref_count
;
14557 basic_info
->object_id
= (vm_object_id_t
) (uintptr_t)
14558 VM_KERNEL_ADDRPERM(object
);
14559 basic_info
->offset
=
14560 (memory_object_offset_t
) offset
+ offset_in_page
;
14561 basic_info
->depth
= depth
;
14571 * Synchronises the memory range specified with its backing store
14572 * image by either flushing or cleaning the contents to the appropriate
14573 * memory manager engaging in a memory object synchronize dialog with
14574 * the manager. The client doesn't return until the manager issues
14575 * m_o_s_completed message. MIG Magically converts user task parameter
14576 * to the task's address map.
14578 * interpretation of sync_flags
14579 * VM_SYNC_INVALIDATE - discard pages, only return precious
14580 * pages to manager.
14582 * VM_SYNC_INVALIDATE & (VM_SYNC_SYNCHRONOUS | VM_SYNC_ASYNCHRONOUS)
14583 * - discard pages, write dirty or precious
14584 * pages back to memory manager.
14586 * VM_SYNC_SYNCHRONOUS | VM_SYNC_ASYNCHRONOUS
14587 * - write dirty or precious pages back to
14588 * the memory manager.
14590 * VM_SYNC_CONTIGUOUS - does everything normally, but if there
14591 * is a hole in the region, and we would
14592 * have returned KERN_SUCCESS, return
14593 * KERN_INVALID_ADDRESS instead.
14596 * The memory object attributes have not yet been implemented, this
14597 * function will have to deal with the invalidate attribute
14600 * KERN_INVALID_TASK Bad task parameter
14601 * KERN_INVALID_ARGUMENT both sync and async were specified.
14602 * KERN_SUCCESS The usual.
14603 * KERN_INVALID_ADDRESS There was a hole in the region.
14609 vm_map_address_t address
,
14610 vm_map_size_t size
,
14611 vm_sync_t sync_flags
)
14614 msync_req_t new_msr
;
14615 queue_chain_t req_q
; /* queue of requests for this msync */
14616 vm_map_entry_t entry
;
14617 vm_map_size_t amount_left
;
14618 vm_object_offset_t offset
;
14619 boolean_t do_sync_req
;
14620 boolean_t had_hole
= FALSE
;
14621 memory_object_t pager
;
14622 vm_map_offset_t pmap_offset
;
14624 if ((sync_flags
& VM_SYNC_ASYNCHRONOUS
) &&
14625 (sync_flags
& VM_SYNC_SYNCHRONOUS
))
14626 return(KERN_INVALID_ARGUMENT
);
14629 * align address and size on page boundaries
14631 size
= (vm_map_round_page(address
+ size
,
14632 VM_MAP_PAGE_MASK(map
)) -
14633 vm_map_trunc_page(address
,
14634 VM_MAP_PAGE_MASK(map
)));
14635 address
= vm_map_trunc_page(address
,
14636 VM_MAP_PAGE_MASK(map
));
14638 if (map
== VM_MAP_NULL
)
14639 return(KERN_INVALID_TASK
);
14642 return(KERN_SUCCESS
);
14644 queue_init(&req_q
);
14645 amount_left
= size
;
14647 while (amount_left
> 0) {
14648 vm_object_size_t flush_size
;
14649 vm_object_t object
;
14652 if (!vm_map_lookup_entry(map
,
14656 vm_map_size_t skip
;
14659 * hole in the address map.
14664 * Check for empty map.
14666 if (entry
== vm_map_to_entry(map
) &&
14667 entry
->vme_next
== entry
) {
14668 vm_map_unlock(map
);
14672 * Check that we don't wrap and that
14673 * we have at least one real map entry.
14675 if ((map
->hdr
.nentries
== 0) ||
14676 (entry
->vme_next
->vme_start
< address
)) {
14677 vm_map_unlock(map
);
14681 * Move up to the next entry if needed
14683 skip
= (entry
->vme_next
->vme_start
- address
);
14684 if (skip
>= amount_left
)
14687 amount_left
-= skip
;
14688 address
= entry
->vme_next
->vme_start
;
14689 vm_map_unlock(map
);
14693 offset
= address
- entry
->vme_start
;
14694 pmap_offset
= address
;
14697 * do we have more to flush than is contained in this
14700 if (amount_left
+ entry
->vme_start
+ offset
> entry
->vme_end
) {
14701 flush_size
= entry
->vme_end
-
14702 (entry
->vme_start
+ offset
);
14704 flush_size
= amount_left
;
14706 amount_left
-= flush_size
;
14707 address
+= flush_size
;
14709 if (entry
->is_sub_map
== TRUE
) {
14710 vm_map_t local_map
;
14711 vm_map_offset_t local_offset
;
14713 local_map
= VME_SUBMAP(entry
);
14714 local_offset
= VME_OFFSET(entry
);
14715 vm_map_unlock(map
);
14720 sync_flags
) == KERN_INVALID_ADDRESS
) {
14725 object
= VME_OBJECT(entry
);
14728 * We can't sync this object if the object has not been
14731 if (object
== VM_OBJECT_NULL
) {
14732 vm_map_unlock(map
);
14735 offset
+= VME_OFFSET(entry
);
14737 vm_object_lock(object
);
14739 if (sync_flags
& (VM_SYNC_KILLPAGES
| VM_SYNC_DEACTIVATE
)) {
14740 int kill_pages
= 0;
14741 boolean_t reusable_pages
= FALSE
;
14743 if (sync_flags
& VM_SYNC_KILLPAGES
) {
14744 if (object
->ref_count
== 1 && !object
->shadow
)
14749 if (kill_pages
!= -1)
14750 vm_object_deactivate_pages(
14753 (vm_object_size_t
) flush_size
,
14758 vm_object_unlock(object
);
14759 vm_map_unlock(map
);
14763 * We can't sync this object if there isn't a pager.
14764 * Don't bother to sync internal objects, since there can't
14765 * be any "permanent" storage for these objects anyway.
14767 if ((object
->pager
== MEMORY_OBJECT_NULL
) ||
14768 (object
->internal
) || (object
->private)) {
14769 vm_object_unlock(object
);
14770 vm_map_unlock(map
);
14774 * keep reference on the object until syncing is done
14776 vm_object_reference_locked(object
);
14777 vm_object_unlock(object
);
14779 vm_map_unlock(map
);
14781 do_sync_req
= vm_object_sync(object
,
14784 sync_flags
& VM_SYNC_INVALIDATE
,
14785 ((sync_flags
& VM_SYNC_SYNCHRONOUS
) ||
14786 (sync_flags
& VM_SYNC_ASYNCHRONOUS
)),
14787 sync_flags
& VM_SYNC_SYNCHRONOUS
);
14789 * only send a m_o_s if we returned pages or if the entry
14790 * is writable (ie dirty pages may have already been sent back)
14792 if (!do_sync_req
) {
14793 if ((sync_flags
& VM_SYNC_INVALIDATE
) && object
->resident_page_count
== 0) {
14795 * clear out the clustering and read-ahead hints
14797 vm_object_lock(object
);
14799 object
->pages_created
= 0;
14800 object
->pages_used
= 0;
14801 object
->sequential
= 0;
14802 object
->last_alloc
= 0;
14804 vm_object_unlock(object
);
14806 vm_object_deallocate(object
);
14809 msync_req_alloc(new_msr
);
14811 vm_object_lock(object
);
14812 offset
+= object
->paging_offset
;
14814 new_msr
->offset
= offset
;
14815 new_msr
->length
= flush_size
;
14816 new_msr
->object
= object
;
14817 new_msr
->flag
= VM_MSYNC_SYNCHRONIZING
;
14821 * We can't sync this object if there isn't a pager. The
14822 * pager can disappear anytime we're not holding the object
14823 * lock. So this has to be checked anytime we goto re_iterate.
14826 pager
= object
->pager
;
14828 if (pager
== MEMORY_OBJECT_NULL
) {
14829 vm_object_unlock(object
);
14830 vm_object_deallocate(object
);
14831 msync_req_free(new_msr
);
14836 queue_iterate(&object
->msr_q
, msr
, msync_req_t
, msr_q
) {
14838 * need to check for overlapping entry, if found, wait
14839 * on overlapping msr to be done, then reiterate
14842 if (msr
->flag
== VM_MSYNC_SYNCHRONIZING
&&
14843 ((offset
>= msr
->offset
&&
14844 offset
< (msr
->offset
+ msr
->length
)) ||
14845 (msr
->offset
>= offset
&&
14846 msr
->offset
< (offset
+ flush_size
))))
14848 assert_wait((event_t
) msr
,THREAD_INTERRUPTIBLE
);
14850 vm_object_unlock(object
);
14851 thread_block(THREAD_CONTINUE_NULL
);
14852 vm_object_lock(object
);
14856 }/* queue_iterate */
14858 queue_enter(&object
->msr_q
, new_msr
, msync_req_t
, msr_q
);
14860 vm_object_paging_begin(object
);
14861 vm_object_unlock(object
);
14863 queue_enter(&req_q
, new_msr
, msync_req_t
, req_q
);
14865 (void) memory_object_synchronize(
14869 sync_flags
& ~VM_SYNC_CONTIGUOUS
);
14871 vm_object_lock(object
);
14872 vm_object_paging_end(object
);
14873 vm_object_unlock(object
);
14877 * wait for memory_object_sychronize_completed messages from pager(s)
14880 while (!queue_empty(&req_q
)) {
14881 msr
= (msync_req_t
)queue_first(&req_q
);
14883 while(msr
->flag
!= VM_MSYNC_DONE
) {
14884 assert_wait((event_t
) msr
, THREAD_INTERRUPTIBLE
);
14886 thread_block(THREAD_CONTINUE_NULL
);
14889 queue_remove(&req_q
, msr
, msync_req_t
, req_q
);
14891 vm_object_deallocate(msr
->object
);
14892 msync_req_free(msr
);
14893 }/* queue_iterate */
14895 /* for proper msync() behaviour */
14896 if (had_hole
== TRUE
&& (sync_flags
& VM_SYNC_CONTIGUOUS
))
14897 return(KERN_INVALID_ADDRESS
);
14899 return(KERN_SUCCESS
);
14903 * Routine: convert_port_entry_to_map
14905 * Convert from a port specifying an entry or a task
14906 * to a map. Doesn't consume the port ref; produces a map ref,
14907 * which may be null. Unlike convert_port_to_map, the
14908 * port may be task or a named entry backed.
14915 convert_port_entry_to_map(
14919 vm_named_entry_t named_entry
;
14920 uint32_t try_failed_count
= 0;
14922 if(IP_VALID(port
) && (ip_kotype(port
) == IKOT_NAMED_ENTRY
)) {
14925 if(ip_active(port
) && (ip_kotype(port
)
14926 == IKOT_NAMED_ENTRY
)) {
14928 (vm_named_entry_t
)port
->ip_kobject
;
14929 if (!(lck_mtx_try_lock(&(named_entry
)->Lock
))) {
14932 try_failed_count
++;
14933 mutex_pause(try_failed_count
);
14936 named_entry
->ref_count
++;
14937 lck_mtx_unlock(&(named_entry
)->Lock
);
14939 if ((named_entry
->is_sub_map
) &&
14940 (named_entry
->protection
14941 & VM_PROT_WRITE
)) {
14942 map
= named_entry
->backing
.map
;
14944 mach_destroy_memory_entry(port
);
14945 return VM_MAP_NULL
;
14947 vm_map_reference_swap(map
);
14948 mach_destroy_memory_entry(port
);
14952 return VM_MAP_NULL
;
14956 map
= convert_port_to_map(port
);
14962 * Routine: convert_port_entry_to_object
14964 * Convert from a port specifying a named entry to an
14965 * object. Doesn't consume the port ref; produces a map ref,
14966 * which may be null.
14973 convert_port_entry_to_object(
14976 vm_object_t object
= VM_OBJECT_NULL
;
14977 vm_named_entry_t named_entry
;
14978 uint32_t try_failed_count
= 0;
14980 if (IP_VALID(port
) &&
14981 (ip_kotype(port
) == IKOT_NAMED_ENTRY
)) {
14984 if (ip_active(port
) &&
14985 (ip_kotype(port
) == IKOT_NAMED_ENTRY
)) {
14986 named_entry
= (vm_named_entry_t
)port
->ip_kobject
;
14987 if (!(lck_mtx_try_lock(&(named_entry
)->Lock
))) {
14989 try_failed_count
++;
14990 mutex_pause(try_failed_count
);
14993 named_entry
->ref_count
++;
14994 lck_mtx_unlock(&(named_entry
)->Lock
);
14996 if (!(named_entry
->is_sub_map
) &&
14997 !(named_entry
->is_pager
) &&
14998 !(named_entry
->is_copy
) &&
14999 (named_entry
->protection
& VM_PROT_WRITE
)) {
15000 object
= named_entry
->backing
.object
;
15001 vm_object_reference(object
);
15003 mach_destroy_memory_entry(port
);
15011 * Export routines to other components for the things we access locally through
15018 return (current_map_fast());
15022 * vm_map_reference:
15024 * Most code internal to the osfmk will go through a
15025 * macro defining this. This is always here for the
15026 * use of other kernel components.
15028 #undef vm_map_reference
15031 register vm_map_t map
)
15033 if (map
== VM_MAP_NULL
)
15036 lck_mtx_lock(&map
->s_lock
);
15038 assert(map
->res_count
> 0);
15039 assert(map
->ref_count
>= map
->res_count
);
15043 lck_mtx_unlock(&map
->s_lock
);
15047 * vm_map_deallocate:
15049 * Removes a reference from the specified map,
15050 * destroying it if no references remain.
15051 * The map should not be locked.
15055 register vm_map_t map
)
15059 if (map
== VM_MAP_NULL
)
15062 lck_mtx_lock(&map
->s_lock
);
15063 ref
= --map
->ref_count
;
15065 vm_map_res_deallocate(map
);
15066 lck_mtx_unlock(&map
->s_lock
);
15069 assert(map
->ref_count
== 0);
15070 lck_mtx_unlock(&map
->s_lock
);
15074 * The map residence count isn't decremented here because
15075 * the vm_map_delete below will traverse the entire map,
15076 * deleting entries, and the residence counts on objects
15077 * and sharing maps will go away then.
15081 vm_map_destroy(map
, VM_MAP_NO_FLAGS
);
15086 vm_map_disable_NX(vm_map_t map
)
15090 if (map
->pmap
== NULL
)
15093 pmap_disable_NX(map
->pmap
);
15097 vm_map_disallow_data_exec(vm_map_t map
)
15102 map
->map_disallow_data_exec
= TRUE
;
15105 /* XXX Consider making these constants (VM_MAX_ADDRESS and MACH_VM_MAX_ADDRESS)
15106 * more descriptive.
15109 vm_map_set_32bit(vm_map_t map
)
15111 map
->max_offset
= (vm_map_offset_t
)VM_MAX_ADDRESS
;
15116 vm_map_set_64bit(vm_map_t map
)
15118 map
->max_offset
= (vm_map_offset_t
)MACH_VM_MAX_ADDRESS
;
15122 vm_compute_max_offset(boolean_t is64
)
15124 return (is64
? (vm_map_offset_t
)MACH_VM_MAX_ADDRESS
: (vm_map_offset_t
)VM_MAX_ADDRESS
);
15128 vm_map_get_max_aslr_slide_pages(vm_map_t map
)
15130 return (1 << (vm_map_is_64bit(map
) ? 16 : 8));
15137 return map
->max_offset
> ((vm_map_offset_t
)VM_MAX_ADDRESS
);
15141 vm_map_has_hard_pagezero(
15143 vm_map_offset_t pagezero_size
)
15147 * We should lock the VM map (for read) here but we can get away
15148 * with it for now because there can't really be any race condition:
15149 * the VM map's min_offset is changed only when the VM map is created
15150 * and when the zero page is established (when the binary gets loaded),
15151 * and this routine gets called only when the task terminates and the
15152 * VM map is being torn down, and when a new map is created via
15153 * load_machfile()/execve().
15155 return (map
->min_offset
>= pagezero_size
);
15159 * Raise a VM map's maximun offset.
15162 vm_map_raise_max_offset(
15164 vm_map_offset_t new_max_offset
)
15169 ret
= KERN_INVALID_ADDRESS
;
15171 if (new_max_offset
>= map
->max_offset
) {
15172 if (!vm_map_is_64bit(map
)) {
15173 if (new_max_offset
<= (vm_map_offset_t
)VM_MAX_ADDRESS
) {
15174 map
->max_offset
= new_max_offset
;
15175 ret
= KERN_SUCCESS
;
15178 if (new_max_offset
<= (vm_map_offset_t
)MACH_VM_MAX_ADDRESS
) {
15179 map
->max_offset
= new_max_offset
;
15180 ret
= KERN_SUCCESS
;
15185 vm_map_unlock(map
);
15191 * Raise a VM map's minimum offset.
15192 * To strictly enforce "page zero" reservation.
15195 vm_map_raise_min_offset(
15197 vm_map_offset_t new_min_offset
)
15199 vm_map_entry_t first_entry
;
15201 new_min_offset
= vm_map_round_page(new_min_offset
,
15202 VM_MAP_PAGE_MASK(map
));
15206 if (new_min_offset
< map
->min_offset
) {
15208 * Can't move min_offset backwards, as that would expose
15209 * a part of the address space that was previously, and for
15210 * possibly good reasons, inaccessible.
15212 vm_map_unlock(map
);
15213 return KERN_INVALID_ADDRESS
;
15215 if (new_min_offset
>= map
->max_offset
) {
15216 /* can't go beyond the end of the address space */
15217 vm_map_unlock(map
);
15218 return KERN_INVALID_ADDRESS
;
15221 first_entry
= vm_map_first_entry(map
);
15222 if (first_entry
!= vm_map_to_entry(map
) &&
15223 first_entry
->vme_start
< new_min_offset
) {
15225 * Some memory was already allocated below the new
15226 * minimun offset. It's too late to change it now...
15228 vm_map_unlock(map
);
15229 return KERN_NO_SPACE
;
15232 map
->min_offset
= new_min_offset
;
15234 assert(map
->holes_list
);
15235 map
->holes_list
->start
= new_min_offset
;
15236 assert(new_min_offset
< map
->holes_list
->end
);
15238 vm_map_unlock(map
);
15240 return KERN_SUCCESS
;
15244 * Set the limit on the maximum amount of user wired memory allowed for this map.
15245 * This is basically a copy of the MEMLOCK rlimit value maintained by the BSD side of
15246 * the kernel. The limits are checked in the mach VM side, so we keep a copy so we
15247 * don't have to reach over to the BSD data structures.
15251 vm_map_set_user_wire_limit(vm_map_t map
,
15254 map
->user_wire_limit
= limit
;
15258 void vm_map_switch_protect(vm_map_t map
,
15262 map
->switch_protect
=val
;
15263 vm_map_unlock(map
);
15267 * IOKit has mapped a region into this map; adjust the pmap's ledgers appropriately.
15268 * phys_footprint is a composite limit consisting of iokit + physmem, so we need to
15269 * bump both counters.
15272 vm_map_iokit_mapped_region(vm_map_t map
, vm_size_t bytes
)
15274 pmap_t pmap
= vm_map_pmap(map
);
15276 ledger_credit(pmap
->ledger
, task_ledgers
.iokit_mapped
, bytes
);
15277 ledger_credit(pmap
->ledger
, task_ledgers
.phys_footprint
, bytes
);
15281 vm_map_iokit_unmapped_region(vm_map_t map
, vm_size_t bytes
)
15283 pmap_t pmap
= vm_map_pmap(map
);
15285 ledger_debit(pmap
->ledger
, task_ledgers
.iokit_mapped
, bytes
);
15286 ledger_debit(pmap
->ledger
, task_ledgers
.phys_footprint
, bytes
);
15289 /* Add (generate) code signature for memory range */
15290 #if CONFIG_DYNAMIC_CODE_SIGNING
15291 kern_return_t
vm_map_sign(vm_map_t map
,
15292 vm_map_offset_t start
,
15293 vm_map_offset_t end
)
15295 vm_map_entry_t entry
;
15297 vm_object_t object
;
15300 * Vet all the input parameters and current type and state of the
15301 * underlaying object. Return with an error if anything is amiss.
15303 if (map
== VM_MAP_NULL
)
15304 return(KERN_INVALID_ARGUMENT
);
15306 vm_map_lock_read(map
);
15308 if (!vm_map_lookup_entry(map
, start
, &entry
) || entry
->is_sub_map
) {
15310 * Must pass a valid non-submap address.
15312 vm_map_unlock_read(map
);
15313 return(KERN_INVALID_ADDRESS
);
15316 if((entry
->vme_start
> start
) || (entry
->vme_end
< end
)) {
15318 * Map entry doesn't cover the requested range. Not handling
15319 * this situation currently.
15321 vm_map_unlock_read(map
);
15322 return(KERN_INVALID_ARGUMENT
);
15325 object
= VME_OBJECT(entry
);
15326 if (object
== VM_OBJECT_NULL
) {
15328 * Object must already be present or we can't sign.
15330 vm_map_unlock_read(map
);
15331 return KERN_INVALID_ARGUMENT
;
15334 vm_object_lock(object
);
15335 vm_map_unlock_read(map
);
15337 while(start
< end
) {
15340 m
= vm_page_lookup(object
,
15341 start
- entry
->vme_start
+ VME_OFFSET(entry
));
15342 if (m
==VM_PAGE_NULL
) {
15343 /* shoud we try to fault a page here? we can probably
15344 * demand it exists and is locked for this request */
15345 vm_object_unlock(object
);
15346 return KERN_FAILURE
;
15348 /* deal with special page status */
15350 (m
->unusual
&& (m
->error
|| m
->restart
|| m
->private || m
->absent
))) {
15351 vm_object_unlock(object
);
15352 return KERN_FAILURE
;
15355 /* Page is OK... now "validate" it */
15356 /* This is the place where we'll call out to create a code
15357 * directory, later */
15358 m
->cs_validated
= TRUE
;
15360 /* The page is now "clean" for codesigning purposes. That means
15361 * we don't consider it as modified (wpmapped) anymore. But
15362 * we'll disconnect the page so we note any future modification
15364 m
->wpmapped
= FALSE
;
15365 refmod
= pmap_disconnect(m
->phys_page
);
15367 /* Pull the dirty status from the pmap, since we cleared the
15369 if ((refmod
& VM_MEM_MODIFIED
) && !m
->dirty
) {
15370 SET_PAGE_DIRTY(m
, FALSE
);
15373 /* On to the next page */
15374 start
+= PAGE_SIZE
;
15376 vm_object_unlock(object
);
15378 return KERN_SUCCESS
;
15382 kern_return_t
vm_map_partial_reap(vm_map_t map
, unsigned int *reclaimed_resident
, unsigned int *reclaimed_compressed
)
15384 vm_map_entry_t entry
= VM_MAP_ENTRY_NULL
;
15385 vm_map_entry_t next_entry
;
15386 kern_return_t kr
= KERN_SUCCESS
;
15392 * We use a "zap_map" to avoid having to unlock
15393 * the "map" in vm_map_delete().
15395 zap_map
= vm_map_create(PMAP_NULL
,
15398 map
->hdr
.entries_pageable
);
15400 if (zap_map
== VM_MAP_NULL
) {
15401 return KERN_RESOURCE_SHORTAGE
;
15404 vm_map_set_page_shift(zap_map
,
15405 VM_MAP_PAGE_SHIFT(map
));
15406 vm_map_disable_hole_optimization(zap_map
);
15408 for (entry
= vm_map_first_entry(map
);
15409 entry
!= vm_map_to_entry(map
);
15410 entry
= next_entry
) {
15411 next_entry
= entry
->vme_next
;
15413 if (VME_OBJECT(entry
) &&
15414 !entry
->is_sub_map
&&
15415 (VME_OBJECT(entry
)->internal
== TRUE
) &&
15416 (VME_OBJECT(entry
)->ref_count
== 1)) {
15418 *reclaimed_resident
+= VME_OBJECT(entry
)->resident_page_count
;
15419 *reclaimed_compressed
+= vm_compressor_pager_get_count(VME_OBJECT(entry
)->pager
);
15421 (void)vm_map_delete(map
,
15424 VM_MAP_REMOVE_SAVE_ENTRIES
,
15429 vm_map_unlock(map
);
15432 * Get rid of the "zap_maps" and all the map entries that
15433 * they may still contain.
15435 if (zap_map
!= VM_MAP_NULL
) {
15436 vm_map_destroy(zap_map
, VM_MAP_REMOVE_NO_PMAP_CLEANUP
);
15437 zap_map
= VM_MAP_NULL
;
15445 kern_return_t
vm_map_freeze_walk(
15447 unsigned int *purgeable_count
,
15448 unsigned int *wired_count
,
15449 unsigned int *clean_count
,
15450 unsigned int *dirty_count
,
15451 unsigned int dirty_budget
,
15452 boolean_t
*has_shared
)
15454 vm_map_entry_t entry
;
15456 vm_map_lock_read(map
);
15458 *purgeable_count
= *wired_count
= *clean_count
= *dirty_count
= 0;
15459 *has_shared
= FALSE
;
15461 for (entry
= vm_map_first_entry(map
);
15462 entry
!= vm_map_to_entry(map
);
15463 entry
= entry
->vme_next
) {
15464 unsigned int purgeable
, clean
, dirty
, wired
;
15467 if ((VME_OBJECT(entry
) == 0) ||
15468 (entry
->is_sub_map
) ||
15469 (VME_OBJECT(entry
)->phys_contiguous
)) {
15473 default_freezer_pack(&purgeable
, &wired
, &clean
, &dirty
, dirty_budget
, &shared
, VME_OBJECT(entry
), NULL
);
15475 *purgeable_count
+= purgeable
;
15476 *wired_count
+= wired
;
15477 *clean_count
+= clean
;
15478 *dirty_count
+= dirty
;
15481 *has_shared
= TRUE
;
15484 /* Adjust pageout budget and finish up if reached */
15485 if (dirty_budget
) {
15486 dirty_budget
-= dirty
;
15487 if (dirty_budget
== 0) {
15493 vm_map_unlock_read(map
);
15495 return KERN_SUCCESS
;
15498 int c_freezer_swapout_count
;
15499 int c_freezer_compression_count
= 0;
15500 AbsoluteTime c_freezer_last_yield_ts
= 0;
15502 kern_return_t
vm_map_freeze(
15504 unsigned int *purgeable_count
,
15505 unsigned int *wired_count
,
15506 unsigned int *clean_count
,
15507 unsigned int *dirty_count
,
15508 unsigned int dirty_budget
,
15509 boolean_t
*has_shared
)
15511 vm_map_entry_t entry2
= VM_MAP_ENTRY_NULL
;
15512 kern_return_t kr
= KERN_SUCCESS
;
15513 boolean_t default_freezer_active
= TRUE
;
15515 *purgeable_count
= *wired_count
= *clean_count
= *dirty_count
= 0;
15516 *has_shared
= FALSE
;
15519 * We need the exclusive lock here so that we can
15520 * block any page faults or lookups while we are
15521 * in the middle of freezing this vm map.
15525 if (COMPRESSED_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE
) {
15526 default_freezer_active
= FALSE
;
15528 if (vm_compressor_low_on_space() || vm_swap_low_on_space()) {
15529 kr
= KERN_NO_SPACE
;
15533 assert(default_freezer_active
== FALSE
);
15535 if (default_freezer_active
) {
15536 if (map
->default_freezer_handle
== NULL
) {
15537 map
->default_freezer_handle
= default_freezer_handle_allocate();
15540 if ((kr
= default_freezer_handle_init(map
->default_freezer_handle
)) != KERN_SUCCESS
) {
15542 * Can happen if default_freezer_handle passed in is NULL
15543 * Or, a table has already been allocated and associated
15544 * with this handle, i.e. the map is already frozen.
15549 c_freezer_compression_count
= 0;
15550 clock_get_uptime(&c_freezer_last_yield_ts
);
15552 for (entry2
= vm_map_first_entry(map
);
15553 entry2
!= vm_map_to_entry(map
);
15554 entry2
= entry2
->vme_next
) {
15556 vm_object_t src_object
= VME_OBJECT(entry2
);
15558 if (VME_OBJECT(entry2
) &&
15559 !entry2
->is_sub_map
&&
15560 !VME_OBJECT(entry2
)->phys_contiguous
) {
15561 /* If eligible, scan the entry, moving eligible pages over to our parent object */
15562 if (default_freezer_active
) {
15563 unsigned int purgeable
, clean
, dirty
, wired
;
15566 default_freezer_pack(&purgeable
, &wired
, &clean
, &dirty
, dirty_budget
, &shared
,
15567 src_object
, map
->default_freezer_handle
);
15569 *purgeable_count
+= purgeable
;
15570 *wired_count
+= wired
;
15571 *clean_count
+= clean
;
15572 *dirty_count
+= dirty
;
15574 /* Adjust pageout budget and finish up if reached */
15575 if (dirty_budget
) {
15576 dirty_budget
-= dirty
;
15577 if (dirty_budget
== 0) {
15583 *has_shared
= TRUE
;
15586 if (VME_OBJECT(entry2
)->internal
== TRUE
) {
15588 if (DEFAULT_FREEZER_COMPRESSED_PAGER_IS_SWAPBACKED
) {
15590 * Pages belonging to this object could be swapped to disk.
15591 * Make sure it's not a shared object because we could end
15592 * up just bringing it back in again.
15594 if (VME_OBJECT(entry2
)->ref_count
> 1) {
15598 vm_object_compressed_freezer_pageout(VME_OBJECT(entry2
));
15601 if (vm_compressor_low_on_space() || vm_swap_low_on_space()) {
15602 kr
= KERN_NO_SPACE
;
15609 if (default_freezer_active
) {
15610 /* Finally, throw out the pages to swap */
15611 default_freezer_pageout(map
->default_freezer_handle
);
15615 vm_map_unlock(map
);
15617 if (!default_freezer_active
) {
15618 vm_object_compressed_freezer_done();
15620 if (DEFAULT_FREEZER_COMPRESSED_PAGER_IS_SWAPBACKED
) {
15622 * reset the counter tracking the # of swapped c_segs
15623 * because we are now done with this freeze session and task.
15625 c_freezer_swapout_count
= 0;
15634 kern_return_t kr
= KERN_SUCCESS
;
15636 if (COMPRESSED_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE
) {
15638 * We will on-demand thaw in the presence of the compressed pager.
15645 if (map
->default_freezer_handle
== NULL
) {
15647 * This map is not in a frozen state.
15653 kr
= default_freezer_unpack(map
->default_freezer_handle
);
15655 vm_map_unlock(map
);
15662 * vm_map_entry_should_cow_for_true_share:
15664 * Determines if the map entry should be clipped and setup for copy-on-write
15665 * to avoid applying "true_share" to a large VM object when only a subset is
15668 * For now, we target only the map entries created for the Objective C
15669 * Garbage Collector, which initially have the following properties:
15670 * - alias == VM_MEMORY_MALLOC
15671 * - wired_count == 0
15673 * and a VM object with:
15675 * - copy_strategy == MEMORY_OBJECT_COPY_SYMMETRIC
15677 * - vo_size == ANON_CHUNK_SIZE
15679 * Only non-kernel map entries.
15682 vm_map_entry_should_cow_for_true_share(
15683 vm_map_entry_t entry
)
15685 vm_object_t object
;
15687 if (entry
->is_sub_map
) {
15688 /* entry does not point at a VM object */
15692 if (entry
->needs_copy
) {
15693 /* already set for copy_on_write: done! */
15697 if (VME_ALIAS(entry
) != VM_MEMORY_MALLOC
&&
15698 VME_ALIAS(entry
) != VM_MEMORY_MALLOC_SMALL
) {
15699 /* not a malloc heap or Obj-C Garbage Collector heap */
15703 if (entry
->wired_count
) {
15704 /* wired: can't change the map entry... */
15705 vm_counters
.should_cow_but_wired
++;
15709 object
= VME_OBJECT(entry
);
15711 if (object
== VM_OBJECT_NULL
) {
15712 /* no object yet... */
15716 if (!object
->internal
) {
15717 /* not an internal object */
15721 if (object
->copy_strategy
!= MEMORY_OBJECT_COPY_SYMMETRIC
) {
15722 /* not the default copy strategy */
15726 if (object
->true_share
) {
15727 /* already true_share: too late to avoid it */
15731 if (VME_ALIAS(entry
) == VM_MEMORY_MALLOC
&&
15732 object
->vo_size
!= ANON_CHUNK_SIZE
) {
15733 /* ... not an object created for the ObjC Garbage Collector */
15737 if (VME_ALIAS(entry
) == VM_MEMORY_MALLOC_SMALL
&&
15738 object
->vo_size
!= 2048 * 4096) {
15739 /* ... not a "MALLOC_SMALL" heap */
15744 * All the criteria match: we have a large object being targeted for "true_share".
15745 * To limit the adverse side-effects linked with "true_share", tell the caller to
15746 * try and avoid setting up the entire object for "true_share" by clipping the
15747 * targeted range and setting it up for copy-on-write.
15753 vm_map_round_page_mask(
15754 vm_map_offset_t offset
,
15755 vm_map_offset_t mask
)
15757 return VM_MAP_ROUND_PAGE(offset
, mask
);
15761 vm_map_trunc_page_mask(
15762 vm_map_offset_t offset
,
15763 vm_map_offset_t mask
)
15765 return VM_MAP_TRUNC_PAGE(offset
, mask
);
15769 vm_map_page_aligned(
15770 vm_map_offset_t offset
,
15771 vm_map_offset_t mask
)
15773 return ((offset
) & mask
) == 0;
15780 return VM_MAP_PAGE_SHIFT(map
);
15787 return VM_MAP_PAGE_SIZE(map
);
15794 return VM_MAP_PAGE_MASK(map
);
15798 vm_map_set_page_shift(
15802 if (map
->hdr
.nentries
!= 0) {
15803 /* too late to change page size */
15804 return KERN_FAILURE
;
15807 map
->hdr
.page_shift
= pageshift
;
15809 return KERN_SUCCESS
;
15816 int num_object_purged
;
15817 vm_map_entry_t entry
;
15818 vm_map_offset_t next_address
;
15819 vm_object_t object
;
15823 num_object_purged
= 0;
15825 vm_map_lock_read(map
);
15826 entry
= vm_map_first_entry(map
);
15827 while (entry
!= vm_map_to_entry(map
)) {
15828 if (entry
->is_sub_map
) {
15831 if (! (entry
->protection
& VM_PROT_WRITE
)) {
15834 object
= VME_OBJECT(entry
);
15835 if (object
== VM_OBJECT_NULL
) {
15838 if (object
->purgable
!= VM_PURGABLE_VOLATILE
) {
15842 vm_object_lock(object
);
15844 if (VME_OFFSET(entry
) != 0 ||
15845 (entry
->vme_end
- entry
->vme_start
) != object
->vo_size
) {
15846 vm_object_unlock(object
);
15850 next_address
= entry
->vme_end
;
15851 vm_map_unlock_read(map
);
15852 state
= VM_PURGABLE_EMPTY
;
15853 kr
= vm_object_purgable_control(object
,
15854 VM_PURGABLE_SET_STATE
,
15856 if (kr
== KERN_SUCCESS
) {
15857 num_object_purged
++;
15859 vm_object_unlock(object
);
15861 vm_map_lock_read(map
);
15862 if (vm_map_lookup_entry(map
, next_address
, &entry
)) {
15866 entry
= entry
->vme_next
;
15868 vm_map_unlock_read(map
);
15870 return num_object_purged
;
15874 vm_map_query_volatile(
15876 mach_vm_size_t
*volatile_virtual_size_p
,
15877 mach_vm_size_t
*volatile_resident_size_p
,
15878 mach_vm_size_t
*volatile_compressed_size_p
,
15879 mach_vm_size_t
*volatile_pmap_size_p
,
15880 mach_vm_size_t
*volatile_compressed_pmap_size_p
)
15882 mach_vm_size_t volatile_virtual_size
;
15883 mach_vm_size_t volatile_resident_count
;
15884 mach_vm_size_t volatile_compressed_count
;
15885 mach_vm_size_t volatile_pmap_count
;
15886 mach_vm_size_t volatile_compressed_pmap_count
;
15887 mach_vm_size_t resident_count
;
15888 unsigned int compressed_count
;
15889 vm_map_entry_t entry
;
15890 vm_object_t object
;
15892 /* map should be locked by caller */
15894 volatile_virtual_size
= 0;
15895 volatile_resident_count
= 0;
15896 volatile_compressed_count
= 0;
15897 volatile_pmap_count
= 0;
15898 volatile_compressed_pmap_count
= 0;
15900 for (entry
= vm_map_first_entry(map
);
15901 entry
!= vm_map_to_entry(map
);
15902 entry
= entry
->vme_next
) {
15903 if (entry
->is_sub_map
) {
15906 if (! (entry
->protection
& VM_PROT_WRITE
)) {
15909 object
= VME_OBJECT(entry
);
15910 if (object
== VM_OBJECT_NULL
) {
15913 if (object
->purgable
!= VM_PURGABLE_VOLATILE
&&
15914 object
->purgable
!= VM_PURGABLE_EMPTY
) {
15917 if (VME_OFFSET(entry
)) {
15919 * If the map entry has been split and the object now
15920 * appears several times in the VM map, we don't want
15921 * to count the object's resident_page_count more than
15922 * once. We count it only for the first one, starting
15923 * at offset 0 and ignore the other VM map entries.
15927 resident_count
= object
->resident_page_count
;
15928 if ((VME_OFFSET(entry
) / PAGE_SIZE
) >= resident_count
) {
15929 resident_count
= 0;
15931 resident_count
-= (VME_OFFSET(entry
) / PAGE_SIZE
);
15934 volatile_virtual_size
+= entry
->vme_end
- entry
->vme_start
;
15935 volatile_resident_count
+= resident_count
;
15936 if (object
->pager
) {
15937 volatile_compressed_count
+=
15938 vm_compressor_pager_get_count(object
->pager
);
15940 compressed_count
= 0;
15941 volatile_pmap_count
+= pmap_query_resident(map
->pmap
,
15944 &compressed_count
);
15945 volatile_compressed_pmap_count
+= compressed_count
;
15948 /* map is still locked on return */
15950 *volatile_virtual_size_p
= volatile_virtual_size
;
15951 *volatile_resident_size_p
= volatile_resident_count
* PAGE_SIZE
;
15952 *volatile_compressed_size_p
= volatile_compressed_count
* PAGE_SIZE
;
15953 *volatile_pmap_size_p
= volatile_pmap_count
* PAGE_SIZE
;
15954 *volatile_compressed_pmap_size_p
= volatile_compressed_pmap_count
* PAGE_SIZE
;
15956 return KERN_SUCCESS
;
15960 vm_map_sizes(vm_map_t map
,
15961 vm_map_size_t
* psize
,
15962 vm_map_size_t
* pfree
,
15963 vm_map_size_t
* plargest_free
)
15965 vm_map_entry_t entry
;
15966 vm_map_offset_t prev
;
15967 vm_map_size_t free
, total_free
, largest_free
;
15970 total_free
= largest_free
= 0;
15972 vm_map_lock_read(map
);
15973 if (psize
) *psize
= map
->max_offset
- map
->min_offset
;
15975 prev
= map
->min_offset
;
15976 for (entry
= vm_map_first_entry(map
);; entry
= entry
->vme_next
)
15978 end
= (entry
== vm_map_to_entry(map
));
15980 if (end
) free
= entry
->vme_end
- prev
;
15981 else free
= entry
->vme_start
- prev
;
15983 total_free
+= free
;
15984 if (free
> largest_free
) largest_free
= free
;
15987 prev
= entry
->vme_end
;
15989 vm_map_unlock_read(map
);
15990 if (pfree
) *pfree
= total_free
;
15991 if (plargest_free
) *plargest_free
= largest_free
;
15994 #if VM_SCAN_FOR_SHADOW_CHAIN
15995 int vm_map_shadow_max(vm_map_t map
);
15996 int vm_map_shadow_max(
15999 int shadows
, shadows_max
;
16000 vm_map_entry_t entry
;
16001 vm_object_t object
, next_object
;
16008 vm_map_lock_read(map
);
16010 for (entry
= vm_map_first_entry(map
);
16011 entry
!= vm_map_to_entry(map
);
16012 entry
= entry
->vme_next
) {
16013 if (entry
->is_sub_map
) {
16016 object
= VME_OBJECT(entry
);
16017 if (object
== NULL
) {
16020 vm_object_lock_shared(object
);
16022 object
->shadow
!= NULL
;
16023 shadows
++, object
= next_object
) {
16024 next_object
= object
->shadow
;
16025 vm_object_lock_shared(next_object
);
16026 vm_object_unlock(object
);
16028 vm_object_unlock(object
);
16029 if (shadows
> shadows_max
) {
16030 shadows_max
= shadows
;
16034 vm_map_unlock_read(map
);
16036 return shadows_max
;
16038 #endif /* VM_SCAN_FOR_SHADOW_CHAIN */