2 * Copyright (c) 2000-2009 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
60 * Author: Avadis Tevanian, Jr., Michael Wayne Young
63 * Virtual memory mapping module.
66 #include <task_swapper.h>
67 #include <mach_assert.h>
68 #include <libkern/OSAtomic.h>
70 #include <mach/kern_return.h>
71 #include <mach/port.h>
72 #include <mach/vm_attributes.h>
73 #include <mach/vm_param.h>
74 #include <mach/vm_behavior.h>
75 #include <mach/vm_statistics.h>
76 #include <mach/memory_object.h>
77 #include <mach/mach_vm.h>
78 #include <machine/cpu_capabilities.h>
81 #include <kern/assert.h>
82 #include <kern/counters.h>
83 #include <kern/kalloc.h>
84 #include <kern/zalloc.h>
87 #include <vm/vm_init.h>
88 #include <vm/vm_fault.h>
89 #include <vm/vm_map.h>
90 #include <vm/vm_object.h>
91 #include <vm/vm_page.h>
92 #include <vm/vm_pageout.h>
93 #include <vm/vm_kern.h>
94 #include <ipc/ipc_port.h>
95 #include <kern/sched_prim.h>
96 #include <kern/misc_protos.h>
97 #include <machine/db_machdep.h>
100 #include <mach/vm_map_server.h>
101 #include <mach/mach_host_server.h>
102 #include <vm/vm_protos.h>
103 #include <vm/vm_purgeable_internal.h>
105 #include <vm/vm_protos.h>
106 #include <vm/vm_shared_region.h>
107 #include <vm/vm_map_store.h>
109 /* Internal prototypes
112 static void vm_map_simplify_range(
114 vm_map_offset_t start
,
115 vm_map_offset_t end
); /* forward */
117 static boolean_t
vm_map_range_check(
119 vm_map_offset_t start
,
121 vm_map_entry_t
*entry
);
123 static vm_map_entry_t
_vm_map_entry_create(
124 struct vm_map_header
*map_header
);
126 static void _vm_map_entry_dispose(
127 struct vm_map_header
*map_header
,
128 vm_map_entry_t entry
);
130 static void vm_map_pmap_enter(
132 vm_map_offset_t addr
,
133 vm_map_offset_t end_addr
,
135 vm_object_offset_t offset
,
136 vm_prot_t protection
);
138 static void _vm_map_clip_end(
139 struct vm_map_header
*map_header
,
140 vm_map_entry_t entry
,
141 vm_map_offset_t end
);
143 static void _vm_map_clip_start(
144 struct vm_map_header
*map_header
,
145 vm_map_entry_t entry
,
146 vm_map_offset_t start
);
148 static void vm_map_entry_delete(
150 vm_map_entry_t entry
);
152 static kern_return_t
vm_map_delete(
154 vm_map_offset_t start
,
159 static kern_return_t
vm_map_copy_overwrite_unaligned(
161 vm_map_entry_t entry
,
163 vm_map_address_t start
);
165 static kern_return_t
vm_map_copy_overwrite_aligned(
167 vm_map_entry_t tmp_entry
,
169 vm_map_offset_t start
,
172 static kern_return_t
vm_map_copyin_kernel_buffer(
174 vm_map_address_t src_addr
,
176 boolean_t src_destroy
,
177 vm_map_copy_t
*copy_result
); /* OUT */
179 static kern_return_t
vm_map_copyout_kernel_buffer(
181 vm_map_address_t
*addr
, /* IN/OUT */
183 boolean_t overwrite
);
185 static void vm_map_fork_share(
187 vm_map_entry_t old_entry
,
190 static boolean_t
vm_map_fork_copy(
192 vm_map_entry_t
*old_entry_p
,
195 void vm_map_region_top_walk(
196 vm_map_entry_t entry
,
197 vm_region_top_info_t top
);
199 void vm_map_region_walk(
202 vm_map_entry_t entry
,
203 vm_object_offset_t offset
,
204 vm_object_size_t range
,
205 vm_region_extended_info_t extended
,
206 boolean_t look_for_pages
);
208 static kern_return_t
vm_map_wire_nested(
210 vm_map_offset_t start
,
212 vm_prot_t access_type
,
215 vm_map_offset_t pmap_addr
);
217 static kern_return_t
vm_map_unwire_nested(
219 vm_map_offset_t start
,
223 vm_map_offset_t pmap_addr
);
225 static kern_return_t
vm_map_overwrite_submap_recurse(
227 vm_map_offset_t dst_addr
,
228 vm_map_size_t dst_size
);
230 static kern_return_t
vm_map_copy_overwrite_nested(
232 vm_map_offset_t dst_addr
,
234 boolean_t interruptible
,
236 boolean_t discard_on_success
);
238 static kern_return_t
vm_map_remap_extract(
240 vm_map_offset_t addr
,
243 struct vm_map_header
*map_header
,
244 vm_prot_t
*cur_protection
,
245 vm_prot_t
*max_protection
,
246 vm_inherit_t inheritance
,
249 static kern_return_t
vm_map_remap_range_allocate(
251 vm_map_address_t
*address
,
253 vm_map_offset_t mask
,
255 vm_map_entry_t
*map_entry
);
257 static void vm_map_region_look_for_page(
261 vm_object_offset_t offset
,
264 vm_region_extended_info_t extended
);
266 static int vm_map_region_count_obj_refs(
267 vm_map_entry_t entry
,
271 static kern_return_t
vm_map_willneed(
273 vm_map_offset_t start
,
274 vm_map_offset_t end
);
276 static kern_return_t
vm_map_reuse_pages(
278 vm_map_offset_t start
,
279 vm_map_offset_t end
);
281 static kern_return_t
vm_map_reusable_pages(
283 vm_map_offset_t start
,
284 vm_map_offset_t end
);
286 static kern_return_t
vm_map_can_reuse(
288 vm_map_offset_t start
,
289 vm_map_offset_t end
);
292 struct default_freezer_table
;
293 __private_extern__
void* default_freezer_mapping_create(vm_object_t
, vm_offset_t
);
294 __private_extern__
void default_freezer_mapping_free(void**, boolean_t all
);
298 * Macros to copy a vm_map_entry. We must be careful to correctly
299 * manage the wired page count. vm_map_entry_copy() creates a new
300 * map entry to the same memory - the wired count in the new entry
301 * must be set to zero. vm_map_entry_copy_full() creates a new
302 * entry that is identical to the old entry. This preserves the
303 * wire count; it's used for map splitting and zone changing in
306 #define vm_map_entry_copy(NEW,OLD) \
309 (NEW)->is_shared = FALSE; \
310 (NEW)->needs_wakeup = FALSE; \
311 (NEW)->in_transition = FALSE; \
312 (NEW)->wired_count = 0; \
313 (NEW)->user_wired_count = 0; \
314 (NEW)->permanent = FALSE; \
317 #define vm_map_entry_copy_full(NEW,OLD) (*(NEW) = *(OLD))
320 * Decide if we want to allow processes to execute from their data or stack areas.
321 * override_nx() returns true if we do. Data/stack execution can be enabled independently
322 * for 32 and 64 bit processes. Set the VM_ABI_32 or VM_ABI_64 flags in allow_data_exec
323 * or allow_stack_exec to enable data execution for that type of data area for that particular
324 * ABI (or both by or'ing the flags together). These are initialized in the architecture
325 * specific pmap files since the default behavior varies according to architecture. The
326 * main reason it varies is because of the need to provide binary compatibility with old
327 * applications that were written before these restrictions came into being. In the old
328 * days, an app could execute anything it could read, but this has slowly been tightened
329 * up over time. The default behavior is:
331 * 32-bit PPC apps may execute from both stack and data areas
332 * 32-bit Intel apps may exeucte from data areas but not stack
333 * 64-bit PPC/Intel apps may not execute from either data or stack
335 * An application on any architecture may override these defaults by explicitly
336 * adding PROT_EXEC permission to the page in question with the mprotect(2)
337 * system call. This code here just determines what happens when an app tries to
338 * execute from a page that lacks execute permission.
340 * Note that allow_data_exec or allow_stack_exec may also be modified by sysctl to change the
341 * default behavior for both 32 and 64 bit apps on a system-wide basis. Furthermore,
342 * a Mach-O header flag bit (MH_NO_HEAP_EXECUTION) can be used to forcibly disallow
343 * execution from data areas for a particular binary even if the arch normally permits it. As
344 * a final wrinkle, a posix_spawn attribute flag can be used to negate this opt-in header bit
345 * to support some complicated use cases, notably browsers with out-of-process plugins that
346 * are not all NX-safe.
349 extern int allow_data_exec
, allow_stack_exec
;
352 override_nx(vm_map_t map
, uint32_t user_tag
) /* map unused on arm */
357 * Determine if the app is running in 32 or 64 bit mode.
360 if (vm_map_is_64bit(map
))
361 current_abi
= VM_ABI_64
;
363 current_abi
= VM_ABI_32
;
366 * Determine if we should allow the execution based on whether it's a
367 * stack or data area and the current architecture.
370 if (user_tag
== VM_MEMORY_STACK
)
371 return allow_stack_exec
& current_abi
;
373 return (allow_data_exec
& current_abi
) && (map
->map_disallow_data_exec
== FALSE
);
378 * Virtual memory maps provide for the mapping, protection,
379 * and sharing of virtual memory objects. In addition,
380 * this module provides for an efficient virtual copy of
381 * memory from one map to another.
383 * Synchronization is required prior to most operations.
385 * Maps consist of an ordered doubly-linked list of simple
386 * entries; a single hint is used to speed up lookups.
388 * Sharing maps have been deleted from this version of Mach.
389 * All shared objects are now mapped directly into the respective
390 * maps. This requires a change in the copy on write strategy;
391 * the asymmetric (delayed) strategy is used for shared temporary
392 * objects instead of the symmetric (shadow) strategy. All maps
393 * are now "top level" maps (either task map, kernel map or submap
394 * of the kernel map).
396 * Since portions of maps are specified by start/end addreses,
397 * which may not align with existing map entries, all
398 * routines merely "clip" entries to these start/end values.
399 * [That is, an entry is split into two, bordering at a
400 * start or end value.] Note that these clippings may not
401 * always be necessary (as the two resulting entries are then
402 * not changed); however, the clipping is done for convenience.
403 * No attempt is currently made to "glue back together" two
406 * The symmetric (shadow) copy strategy implements virtual copy
407 * by copying VM object references from one map to
408 * another, and then marking both regions as copy-on-write.
409 * It is important to note that only one writeable reference
410 * to a VM object region exists in any map when this strategy
411 * is used -- this means that shadow object creation can be
412 * delayed until a write operation occurs. The symmetric (delayed)
413 * strategy allows multiple maps to have writeable references to
414 * the same region of a vm object, and hence cannot delay creating
415 * its copy objects. See vm_object_copy_quickly() in vm_object.c.
416 * Copying of permanent objects is completely different; see
417 * vm_object_copy_strategically() in vm_object.c.
420 static zone_t vm_map_zone
; /* zone for vm_map structures */
421 static zone_t vm_map_entry_zone
; /* zone for vm_map_entry structures */
422 static zone_t vm_map_kentry_zone
; /* zone for kernel entry structures */
423 static zone_t vm_map_copy_zone
; /* zone for vm_map_copy structures */
427 * Placeholder object for submap operations. This object is dropped
428 * into the range by a call to vm_map_find, and removed when
429 * vm_map_submap creates the submap.
432 vm_object_t vm_submap_object
;
434 static void *map_data
;
435 static vm_size_t map_data_size
;
436 static void *kentry_data
;
437 static vm_size_t kentry_data_size
;
438 static int kentry_count
= 2048; /* to init kentry_data_size */
441 #define NO_COALESCE_LIMIT 0
443 #define NO_COALESCE_LIMIT ((1024 * 128) - 1)
446 /* Skip acquiring locks if we're in the midst of a kernel core dump */
447 unsigned int not_in_kdp
= 1;
449 unsigned int vm_map_set_cache_attr_count
= 0;
452 vm_map_set_cache_attr(
456 vm_map_entry_t map_entry
;
458 kern_return_t kr
= KERN_SUCCESS
;
460 vm_map_lock_read(map
);
462 if (!vm_map_lookup_entry(map
, va
, &map_entry
) ||
463 map_entry
->is_sub_map
) {
465 * that memory is not properly mapped
467 kr
= KERN_INVALID_ARGUMENT
;
470 object
= map_entry
->object
.vm_object
;
472 if (object
== VM_OBJECT_NULL
) {
474 * there should be a VM object here at this point
476 kr
= KERN_INVALID_ARGUMENT
;
479 vm_object_lock(object
);
480 object
->set_cache_attr
= TRUE
;
481 vm_object_unlock(object
);
483 vm_map_set_cache_attr_count
++;
485 vm_map_unlock_read(map
);
491 #if CONFIG_CODE_DECRYPTION
493 * vm_map_apple_protected:
494 * This remaps the requested part of the object with an object backed by
495 * the decrypting pager.
496 * crypt_info contains entry points and session data for the crypt module.
497 * The crypt_info block will be copied by vm_map_apple_protected. The data structures
498 * referenced in crypt_info must remain valid until crypt_info->crypt_end() is called.
501 vm_map_apple_protected(
503 vm_map_offset_t start
,
505 struct pager_crypt_info
*crypt_info
)
507 boolean_t map_locked
;
509 vm_map_entry_t map_entry
;
510 memory_object_t protected_mem_obj
;
511 vm_object_t protected_object
;
512 vm_map_offset_t map_addr
;
514 vm_map_lock_read(map
);
517 /* lookup the protected VM object */
518 if (!vm_map_lookup_entry(map
,
521 map_entry
->vme_end
< end
||
522 map_entry
->is_sub_map
) {
523 /* that memory is not properly mapped */
524 kr
= KERN_INVALID_ARGUMENT
;
527 protected_object
= map_entry
->object
.vm_object
;
528 if (protected_object
== VM_OBJECT_NULL
) {
529 /* there should be a VM object here at this point */
530 kr
= KERN_INVALID_ARGUMENT
;
534 /* make sure protected object stays alive while map is unlocked */
535 vm_object_reference(protected_object
);
537 vm_map_unlock_read(map
);
541 * Lookup (and create if necessary) the protected memory object
542 * matching that VM object.
543 * If successful, this also grabs a reference on the memory object,
544 * to guarantee that it doesn't go away before we get a chance to map
547 protected_mem_obj
= apple_protect_pager_setup(protected_object
, crypt_info
);
549 /* release extra ref on protected object */
550 vm_object_deallocate(protected_object
);
552 if (protected_mem_obj
== NULL
) {
557 /* map this memory object in place of the current one */
559 kr
= vm_map_enter_mem_object(map
,
562 (mach_vm_offset_t
) 0,
563 VM_FLAGS_FIXED
| VM_FLAGS_OVERWRITE
,
564 (ipc_port_t
) protected_mem_obj
,
566 (start
- map_entry
->vme_start
)),
568 map_entry
->protection
,
569 map_entry
->max_protection
,
570 map_entry
->inheritance
);
571 assert(map_addr
== start
);
573 * Release the reference obtained by apple_protect_pager_setup().
574 * The mapping (if it succeeded) is now holding a reference on the
577 memory_object_deallocate(protected_mem_obj
);
581 vm_map_unlock_read(map
);
585 #endif /* CONFIG_CODE_DECRYPTION */
588 lck_grp_t vm_map_lck_grp
;
589 lck_grp_attr_t vm_map_lck_grp_attr
;
590 lck_attr_t vm_map_lck_attr
;
596 * Initialize the vm_map module. Must be called before
597 * any other vm_map routines.
599 * Map and entry structures are allocated from zones -- we must
600 * initialize those zones.
602 * There are three zones of interest:
604 * vm_map_zone: used to allocate maps.
605 * vm_map_entry_zone: used to allocate map entries.
606 * vm_map_kentry_zone: used to allocate map entries for the kernel.
608 * The kernel allocates map entries from a special zone that is initially
609 * "crammed" with memory. It would be difficult (perhaps impossible) for
610 * the kernel to allocate more memory to a entry zone when it became
611 * empty since the very act of allocating memory implies the creation
618 vm_map_zone
= zinit((vm_map_size_t
) sizeof(struct _vm_map
), 40*1024,
620 zone_change(vm_map_zone
, Z_NOENCRYPT
, TRUE
);
622 vm_map_entry_zone
= zinit((vm_map_size_t
) sizeof(struct vm_map_entry
),
623 1024*1024, PAGE_SIZE
*5,
624 "non-kernel map entries");
625 zone_change(vm_map_entry_zone
, Z_NOENCRYPT
, TRUE
);
627 vm_map_kentry_zone
= zinit((vm_map_size_t
) sizeof(struct vm_map_entry
),
628 kentry_data_size
, kentry_data_size
,
629 "kernel map entries");
630 zone_change(vm_map_kentry_zone
, Z_NOENCRYPT
, TRUE
);
632 vm_map_copy_zone
= zinit((vm_map_size_t
) sizeof(struct vm_map_copy
),
633 16*1024, PAGE_SIZE
, "map copies");
634 zone_change(vm_map_copy_zone
, Z_NOENCRYPT
, TRUE
);
637 * Cram the map and kentry zones with initial data.
638 * Set kentry_zone non-collectible to aid zone_gc().
640 zone_change(vm_map_zone
, Z_COLLECT
, FALSE
);
641 zone_change(vm_map_kentry_zone
, Z_COLLECT
, FALSE
);
642 zone_change(vm_map_kentry_zone
, Z_EXPAND
, FALSE
);
643 zone_change(vm_map_kentry_zone
, Z_FOREIGN
, TRUE
);
644 zone_change(vm_map_kentry_zone
, Z_CALLERACCT
, FALSE
); /* don't charge caller */
645 zone_change(vm_map_copy_zone
, Z_CALLERACCT
, FALSE
); /* don't charge caller */
647 zcram(vm_map_zone
, map_data
, map_data_size
);
648 zcram(vm_map_kentry_zone
, kentry_data
, kentry_data_size
);
650 lck_grp_attr_setdefault(&vm_map_lck_grp_attr
);
651 lck_grp_init(&vm_map_lck_grp
, "vm_map", &vm_map_lck_grp_attr
);
652 lck_attr_setdefault(&vm_map_lck_attr
);
659 map_data_size
= round_page(10 * sizeof(struct _vm_map
));
660 map_data
= pmap_steal_memory(map_data_size
);
664 * Limiting worst case: vm_map_kentry_zone needs to map each "available"
665 * physical page (i.e. that beyond the kernel image and page tables)
666 * individually; we guess at most one entry per eight pages in the
667 * real world. This works out to roughly .1 of 1% of physical memory,
668 * or roughly 1900 entries (64K) for a 64M machine with 4K pages.
671 kentry_count
= pmap_free_pages() / 8;
675 round_page(kentry_count
* sizeof(struct vm_map_entry
));
676 kentry_data
= pmap_steal_memory(kentry_data_size
);
682 * Creates and returns a new empty VM map with
683 * the given physical map structure, and having
684 * the given lower and upper address bounds.
693 static int color_seed
= 0;
694 register vm_map_t result
;
696 result
= (vm_map_t
) zalloc(vm_map_zone
);
697 if (result
== VM_MAP_NULL
)
698 panic("vm_map_create");
700 vm_map_first_entry(result
) = vm_map_to_entry(result
);
701 vm_map_last_entry(result
) = vm_map_to_entry(result
);
702 result
->hdr
.nentries
= 0;
703 result
->hdr
.entries_pageable
= pageable
;
705 vm_map_store_init( &(result
->hdr
) );
708 result
->user_wire_limit
= MACH_VM_MAX_ADDRESS
; /* default limit is unlimited */
709 result
->user_wire_size
= 0;
710 result
->ref_count
= 1;
712 result
->res_count
= 1;
713 result
->sw_state
= MAP_SW_IN
;
714 #endif /* TASK_SWAPPER */
716 result
->min_offset
= min
;
717 result
->max_offset
= max
;
718 result
->wiring_required
= FALSE
;
719 result
->no_zero_fill
= FALSE
;
720 result
->mapped
= FALSE
;
721 result
->wait_for_space
= FALSE
;
722 result
->switch_protect
= FALSE
;
723 result
->disable_vmentry_reuse
= FALSE
;
724 result
->map_disallow_data_exec
= FALSE
;
725 result
->highest_entry_end
= 0;
726 result
->first_free
= vm_map_to_entry(result
);
727 result
->hint
= vm_map_to_entry(result
);
728 result
->color_rr
= (color_seed
++) & vm_color_mask
;
729 result
->jit_entry_exists
= FALSE
;
731 result
->default_freezer_toc
= NULL
;
733 vm_map_lock_init(result
);
734 lck_mtx_init_ext(&result
->s_lock
, &result
->s_lock_ext
, &vm_map_lck_grp
, &vm_map_lck_attr
);
740 * vm_map_entry_create: [ internal use only ]
742 * Allocates a VM map entry for insertion in the
743 * given map (or map copy). No fields are filled.
745 #define vm_map_entry_create(map) \
746 _vm_map_entry_create(&(map)->hdr)
748 #define vm_map_copy_entry_create(copy) \
749 _vm_map_entry_create(&(copy)->cpy_hdr)
751 static vm_map_entry_t
752 _vm_map_entry_create(
753 register struct vm_map_header
*map_header
)
755 register zone_t zone
;
756 register vm_map_entry_t entry
;
758 if (map_header
->entries_pageable
)
759 zone
= vm_map_entry_zone
;
761 zone
= vm_map_kentry_zone
;
763 entry
= (vm_map_entry_t
) zalloc(zone
);
764 if (entry
== VM_MAP_ENTRY_NULL
)
765 panic("vm_map_entry_create");
766 vm_map_store_update( (vm_map_t
) NULL
, entry
, VM_MAP_ENTRY_CREATE
);
772 * vm_map_entry_dispose: [ internal use only ]
774 * Inverse of vm_map_entry_create.
776 * write map lock held so no need to
777 * do anything special to insure correctness
780 #define vm_map_entry_dispose(map, entry) \
781 vm_map_store_update( map, entry, VM_MAP_ENTRY_DELETE); \
782 _vm_map_entry_dispose(&(map)->hdr, (entry))
784 #define vm_map_copy_entry_dispose(map, entry) \
785 _vm_map_entry_dispose(&(copy)->cpy_hdr, (entry))
788 _vm_map_entry_dispose(
789 register struct vm_map_header
*map_header
,
790 register vm_map_entry_t entry
)
792 register zone_t zone
;
794 if (map_header
->entries_pageable
)
795 zone
= vm_map_entry_zone
;
797 zone
= vm_map_kentry_zone
;
803 static boolean_t first_free_check
= FALSE
;
808 if (!first_free_check
)
811 return( first_free_is_valid_store( map
));
813 #endif /* MACH_ASSERT */
816 #define vm_map_copy_entry_link(copy, after_where, entry) \
817 _vm_map_store_entry_link(&(copy)->cpy_hdr, after_where, (entry))
819 #define vm_map_copy_entry_unlink(copy, entry) \
820 _vm_map_store_entry_unlink(&(copy)->cpy_hdr, (entry))
822 #if MACH_ASSERT && TASK_SWAPPER
824 * vm_map_res_reference:
826 * Adds another valid residence count to the given map.
828 * Map is locked so this function can be called from
832 void vm_map_res_reference(register vm_map_t map
)
834 /* assert map is locked */
835 assert(map
->res_count
>= 0);
836 assert(map
->ref_count
>= map
->res_count
);
837 if (map
->res_count
== 0) {
838 lck_mtx_unlock(&map
->s_lock
);
841 lck_mtx_lock(&map
->s_lock
);
849 * vm_map_reference_swap:
851 * Adds valid reference and residence counts to the given map.
853 * The map may not be in memory (i.e. zero residence count).
856 void vm_map_reference_swap(register vm_map_t map
)
858 assert(map
!= VM_MAP_NULL
);
859 lck_mtx_lock(&map
->s_lock
);
860 assert(map
->res_count
>= 0);
861 assert(map
->ref_count
>= map
->res_count
);
863 vm_map_res_reference(map
);
864 lck_mtx_unlock(&map
->s_lock
);
868 * vm_map_res_deallocate:
870 * Decrement residence count on a map; possibly causing swapout.
872 * The map must be in memory (i.e. non-zero residence count).
874 * The map is locked, so this function is callable from vm_map_deallocate.
877 void vm_map_res_deallocate(register vm_map_t map
)
879 assert(map
->res_count
> 0);
880 if (--map
->res_count
== 0) {
881 lck_mtx_unlock(&map
->s_lock
);
885 lck_mtx_lock(&map
->s_lock
);
887 assert(map
->ref_count
>= map
->res_count
);
889 #endif /* MACH_ASSERT && TASK_SWAPPER */
894 * Actually destroy a map.
903 /* clean up regular map entries */
904 (void) vm_map_delete(map
, map
->min_offset
, map
->max_offset
,
906 /* clean up leftover special mappings (commpage, etc...) */
907 (void) vm_map_delete(map
, 0x0, 0xFFFFFFFFFFFFF000ULL
,
911 if (map
->default_freezer_toc
){
912 default_freezer_mapping_free( &(map
->default_freezer_toc
), TRUE
);
917 assert(map
->hdr
.nentries
== 0);
920 pmap_destroy(map
->pmap
);
922 zfree(vm_map_zone
, map
);
927 * vm_map_swapin/vm_map_swapout
929 * Swap a map in and out, either referencing or releasing its resources.
930 * These functions are internal use only; however, they must be exported
931 * because they may be called from macros, which are exported.
933 * In the case of swapout, there could be races on the residence count,
934 * so if the residence count is up, we return, assuming that a
935 * vm_map_deallocate() call in the near future will bring us back.
938 * -- We use the map write lock for synchronization among races.
939 * -- The map write lock, and not the simple s_lock, protects the
940 * swap state of the map.
941 * -- If a map entry is a share map, then we hold both locks, in
942 * hierarchical order.
944 * Synchronization Notes:
945 * 1) If a vm_map_swapin() call happens while swapout in progress, it
946 * will block on the map lock and proceed when swapout is through.
947 * 2) A vm_map_reference() call at this time is illegal, and will
948 * cause a panic. vm_map_reference() is only allowed on resident
949 * maps, since it refuses to block.
950 * 3) A vm_map_swapin() call during a swapin will block, and
951 * proceeed when the first swapin is done, turning into a nop.
952 * This is the reason the res_count is not incremented until
953 * after the swapin is complete.
954 * 4) There is a timing hole after the checks of the res_count, before
955 * the map lock is taken, during which a swapin may get the lock
956 * before a swapout about to happen. If this happens, the swapin
957 * will detect the state and increment the reference count, causing
958 * the swapout to be a nop, thereby delaying it until a later
959 * vm_map_deallocate. If the swapout gets the lock first, then
960 * the swapin will simply block until the swapout is done, and
963 * Because vm_map_swapin() is potentially an expensive operation, it
964 * should be used with caution.
967 * 1) A map with a residence count of zero is either swapped, or
969 * 2) A map with a non-zero residence count is either resident,
970 * or being swapped in.
973 int vm_map_swap_enable
= 1;
975 void vm_map_swapin (vm_map_t map
)
977 register vm_map_entry_t entry
;
979 if (!vm_map_swap_enable
) /* debug */
984 * First deal with various races.
986 if (map
->sw_state
== MAP_SW_IN
)
988 * we raced with swapout and won. Returning will incr.
989 * the res_count, turning the swapout into a nop.
994 * The residence count must be zero. If we raced with another
995 * swapin, the state would have been IN; if we raced with a
996 * swapout (after another competing swapin), we must have lost
997 * the race to get here (see above comment), in which case
998 * res_count is still 0.
1000 assert(map
->res_count
== 0);
1003 * There are no intermediate states of a map going out or
1004 * coming in, since the map is locked during the transition.
1006 assert(map
->sw_state
== MAP_SW_OUT
);
1009 * We now operate upon each map entry. If the entry is a sub-
1010 * or share-map, we call vm_map_res_reference upon it.
1011 * If the entry is an object, we call vm_object_res_reference
1012 * (this may iterate through the shadow chain).
1013 * Note that we hold the map locked the entire time,
1014 * even if we get back here via a recursive call in
1015 * vm_map_res_reference.
1017 entry
= vm_map_first_entry(map
);
1019 while (entry
!= vm_map_to_entry(map
)) {
1020 if (entry
->object
.vm_object
!= VM_OBJECT_NULL
) {
1021 if (entry
->is_sub_map
) {
1022 vm_map_t lmap
= entry
->object
.sub_map
;
1023 lck_mtx_lock(&lmap
->s_lock
);
1024 vm_map_res_reference(lmap
);
1025 lck_mtx_unlock(&lmap
->s_lock
);
1027 vm_object_t object
= entry
->object
.vm_object
;
1028 vm_object_lock(object
);
1030 * This call may iterate through the
1033 vm_object_res_reference(object
);
1034 vm_object_unlock(object
);
1037 entry
= entry
->vme_next
;
1039 assert(map
->sw_state
== MAP_SW_OUT
);
1040 map
->sw_state
= MAP_SW_IN
;
1043 void vm_map_swapout(vm_map_t map
)
1045 register vm_map_entry_t entry
;
1049 * First deal with various races.
1050 * If we raced with a swapin and lost, the residence count
1051 * will have been incremented to 1, and we simply return.
1053 lck_mtx_lock(&map
->s_lock
);
1054 if (map
->res_count
!= 0) {
1055 lck_mtx_unlock(&map
->s_lock
);
1058 lck_mtx_unlock(&map
->s_lock
);
1061 * There are no intermediate states of a map going out or
1062 * coming in, since the map is locked during the transition.
1064 assert(map
->sw_state
== MAP_SW_IN
);
1066 if (!vm_map_swap_enable
)
1070 * We now operate upon each map entry. If the entry is a sub-
1071 * or share-map, we call vm_map_res_deallocate upon it.
1072 * If the entry is an object, we call vm_object_res_deallocate
1073 * (this may iterate through the shadow chain).
1074 * Note that we hold the map locked the entire time,
1075 * even if we get back here via a recursive call in
1076 * vm_map_res_deallocate.
1078 entry
= vm_map_first_entry(map
);
1080 while (entry
!= vm_map_to_entry(map
)) {
1081 if (entry
->object
.vm_object
!= VM_OBJECT_NULL
) {
1082 if (entry
->is_sub_map
) {
1083 vm_map_t lmap
= entry
->object
.sub_map
;
1084 lck_mtx_lock(&lmap
->s_lock
);
1085 vm_map_res_deallocate(lmap
);
1086 lck_mtx_unlock(&lmap
->s_lock
);
1088 vm_object_t object
= entry
->object
.vm_object
;
1089 vm_object_lock(object
);
1091 * This call may take a long time,
1092 * since it could actively push
1093 * out pages (if we implement it
1096 vm_object_res_deallocate(object
);
1097 vm_object_unlock(object
);
1100 entry
= entry
->vme_next
;
1102 assert(map
->sw_state
== MAP_SW_IN
);
1103 map
->sw_state
= MAP_SW_OUT
;
1106 #endif /* TASK_SWAPPER */
1109 * vm_map_lookup_entry: [ internal use only ]
1111 * Calls into the vm map store layer to find the map
1112 * entry containing (or immediately preceding) the
1113 * specified address in the given map; the entry is returned
1114 * in the "entry" parameter. The boolean
1115 * result indicates whether the address is
1116 * actually contained in the map.
1119 vm_map_lookup_entry(
1120 register vm_map_t map
,
1121 register vm_map_offset_t address
,
1122 vm_map_entry_t
*entry
) /* OUT */
1124 return ( vm_map_store_lookup_entry( map
, address
, entry
));
1128 * Routine: vm_map_find_space
1130 * Allocate a range in the specified virtual address map,
1131 * returning the entry allocated for that range.
1132 * Used by kmem_alloc, etc.
1134 * The map must be NOT be locked. It will be returned locked
1135 * on KERN_SUCCESS, unlocked on failure.
1137 * If an entry is allocated, the object/offset fields
1138 * are initialized to zero.
1142 register vm_map_t map
,
1143 vm_map_offset_t
*address
, /* OUT */
1145 vm_map_offset_t mask
,
1147 vm_map_entry_t
*o_entry
) /* OUT */
1149 register vm_map_entry_t entry
, new_entry
;
1150 register vm_map_offset_t start
;
1151 register vm_map_offset_t end
;
1155 return KERN_INVALID_ARGUMENT
;
1158 if (flags
& VM_FLAGS_GUARD_AFTER
) {
1159 /* account for the back guard page in the size */
1160 size
+= PAGE_SIZE_64
;
1163 new_entry
= vm_map_entry_create(map
);
1166 * Look for the first possible address; if there's already
1167 * something at this address, we have to start after it.
1172 if( map
->disable_vmentry_reuse
== TRUE
) {
1173 VM_MAP_HIGHEST_ENTRY(map
, entry
, start
);
1175 assert(first_free_is_valid(map
));
1176 if ((entry
= map
->first_free
) == vm_map_to_entry(map
))
1177 start
= map
->min_offset
;
1179 start
= entry
->vme_end
;
1183 * In any case, the "entry" always precedes
1184 * the proposed new region throughout the loop:
1188 register vm_map_entry_t next
;
1191 * Find the end of the proposed new region.
1192 * Be sure we didn't go beyond the end, or
1193 * wrap around the address.
1196 if (flags
& VM_FLAGS_GUARD_BEFORE
) {
1197 /* reserve space for the front guard page */
1198 start
+= PAGE_SIZE_64
;
1200 end
= ((start
+ mask
) & ~mask
);
1203 vm_map_entry_dispose(map
, new_entry
);
1205 return(KERN_NO_SPACE
);
1210 if ((end
> map
->max_offset
) || (end
< start
)) {
1211 vm_map_entry_dispose(map
, new_entry
);
1213 return(KERN_NO_SPACE
);
1217 * If there are no more entries, we must win.
1220 next
= entry
->vme_next
;
1221 if (next
== vm_map_to_entry(map
))
1225 * If there is another entry, it must be
1226 * after the end of the potential new region.
1229 if (next
->vme_start
>= end
)
1233 * Didn't fit -- move to the next entry.
1237 start
= entry
->vme_end
;
1242 * "start" and "end" should define the endpoints of the
1243 * available new range, and
1244 * "entry" should refer to the region before the new
1247 * the map should be locked.
1250 if (flags
& VM_FLAGS_GUARD_BEFORE
) {
1251 /* go back for the front guard page */
1252 start
-= PAGE_SIZE_64
;
1256 new_entry
->vme_start
= start
;
1257 new_entry
->vme_end
= end
;
1258 assert(page_aligned(new_entry
->vme_start
));
1259 assert(page_aligned(new_entry
->vme_end
));
1261 new_entry
->is_shared
= FALSE
;
1262 new_entry
->is_sub_map
= FALSE
;
1263 new_entry
->use_pmap
= FALSE
;
1264 new_entry
->object
.vm_object
= VM_OBJECT_NULL
;
1265 new_entry
->offset
= (vm_object_offset_t
) 0;
1267 new_entry
->needs_copy
= FALSE
;
1269 new_entry
->inheritance
= VM_INHERIT_DEFAULT
;
1270 new_entry
->protection
= VM_PROT_DEFAULT
;
1271 new_entry
->max_protection
= VM_PROT_ALL
;
1272 new_entry
->behavior
= VM_BEHAVIOR_DEFAULT
;
1273 new_entry
->wired_count
= 0;
1274 new_entry
->user_wired_count
= 0;
1276 new_entry
->in_transition
= FALSE
;
1277 new_entry
->needs_wakeup
= FALSE
;
1278 new_entry
->no_cache
= FALSE
;
1279 new_entry
->permanent
= FALSE
;
1280 new_entry
->superpage_size
= 0;
1282 new_entry
->alias
= 0;
1283 new_entry
->zero_wired_pages
= FALSE
;
1285 VM_GET_FLAGS_ALIAS(flags
, new_entry
->alias
);
1288 * Insert the new entry into the list
1291 vm_map_store_entry_link(map
, entry
, new_entry
);
1296 * Update the lookup hint
1298 SAVE_HINT_MAP_WRITE(map
, new_entry
);
1300 *o_entry
= new_entry
;
1301 return(KERN_SUCCESS
);
1304 int vm_map_pmap_enter_print
= FALSE
;
1305 int vm_map_pmap_enter_enable
= FALSE
;
1308 * Routine: vm_map_pmap_enter [internal only]
1311 * Force pages from the specified object to be entered into
1312 * the pmap at the specified address if they are present.
1313 * As soon as a page not found in the object the scan ends.
1318 * In/out conditions:
1319 * The source map should not be locked on entry.
1324 register vm_map_offset_t addr
,
1325 register vm_map_offset_t end_addr
,
1326 register vm_object_t object
,
1327 vm_object_offset_t offset
,
1328 vm_prot_t protection
)
1336 while (addr
< end_addr
) {
1337 register vm_page_t m
;
1339 vm_object_lock(object
);
1341 m
= vm_page_lookup(object
, offset
);
1344 * The user should never see encrypted data, so do not
1345 * enter an encrypted page in the page table.
1347 if (m
== VM_PAGE_NULL
|| m
->busy
|| m
->encrypted
||
1349 (m
->unusual
&& ( m
->error
|| m
->restart
|| m
->absent
))) {
1350 vm_object_unlock(object
);
1354 if (vm_map_pmap_enter_print
) {
1355 printf("vm_map_pmap_enter:");
1356 printf("map: %p, addr: %llx, object: %p, offset: %llx\n",
1357 map
, (unsigned long long)addr
, object
, (unsigned long long)offset
);
1359 type_of_fault
= DBG_CACHE_HIT_FAULT
;
1360 kr
= vm_fault_enter(m
, map
->pmap
, addr
, protection
, protection
,
1361 VM_PAGE_WIRED(m
), FALSE
, FALSE
, FALSE
,
1364 vm_object_unlock(object
);
1366 offset
+= PAGE_SIZE_64
;
1371 boolean_t
vm_map_pmap_is_empty(
1373 vm_map_offset_t start
,
1374 vm_map_offset_t end
);
1375 boolean_t
vm_map_pmap_is_empty(
1377 vm_map_offset_t start
,
1378 vm_map_offset_t end
)
1380 #ifdef MACHINE_PMAP_IS_EMPTY
1381 return pmap_is_empty(map
->pmap
, start
, end
);
1382 #else /* MACHINE_PMAP_IS_EMPTY */
1383 vm_map_offset_t offset
;
1386 if (map
->pmap
== NULL
) {
1390 for (offset
= start
;
1392 offset
+= PAGE_SIZE
) {
1393 phys_page
= pmap_find_phys(map
->pmap
, offset
);
1395 kprintf("vm_map_pmap_is_empty(%p,0x%llx,0x%llx): "
1396 "page %d at 0x%llx\n",
1397 map
, (long long)start
, (long long)end
,
1398 phys_page
, (long long)offset
);
1403 #endif /* MACHINE_PMAP_IS_EMPTY */
1407 * Routine: vm_map_enter
1410 * Allocate a range in the specified virtual address map.
1411 * The resulting range will refer to memory defined by
1412 * the given memory object and offset into that object.
1414 * Arguments are as defined in the vm_map call.
1416 int _map_enter_debug
= 0;
1417 static unsigned int vm_map_enter_restore_successes
= 0;
1418 static unsigned int vm_map_enter_restore_failures
= 0;
1422 vm_map_offset_t
*address
, /* IN/OUT */
1424 vm_map_offset_t mask
,
1427 vm_object_offset_t offset
,
1428 boolean_t needs_copy
,
1429 vm_prot_t cur_protection
,
1430 vm_prot_t max_protection
,
1431 vm_inherit_t inheritance
)
1433 vm_map_entry_t entry
, new_entry
;
1434 vm_map_offset_t start
, tmp_start
, tmp_offset
;
1435 vm_map_offset_t end
, tmp_end
;
1436 vm_map_offset_t tmp2_start
, tmp2_end
;
1437 vm_map_offset_t step
;
1438 kern_return_t result
= KERN_SUCCESS
;
1439 vm_map_t zap_old_map
= VM_MAP_NULL
;
1440 vm_map_t zap_new_map
= VM_MAP_NULL
;
1441 boolean_t map_locked
= FALSE
;
1442 boolean_t pmap_empty
= TRUE
;
1443 boolean_t new_mapping_established
= FALSE
;
1444 boolean_t anywhere
= ((flags
& VM_FLAGS_ANYWHERE
) != 0);
1445 boolean_t purgable
= ((flags
& VM_FLAGS_PURGABLE
) != 0);
1446 boolean_t overwrite
= ((flags
& VM_FLAGS_OVERWRITE
) != 0);
1447 boolean_t no_cache
= ((flags
& VM_FLAGS_NO_CACHE
) != 0);
1448 boolean_t is_submap
= ((flags
& VM_FLAGS_SUBMAP
) != 0);
1449 boolean_t permanent
= ((flags
& VM_FLAGS_PERMANENT
) != 0);
1450 unsigned int superpage_size
= ((flags
& VM_FLAGS_SUPERPAGE_MASK
) >> VM_FLAGS_SUPERPAGE_SHIFT
);
1452 vm_map_offset_t effective_min_offset
, effective_max_offset
;
1455 if (superpage_size
) {
1456 switch (superpage_size
) {
1458 * Note that the current implementation only supports
1459 * a single size for superpages, SUPERPAGE_SIZE, per
1460 * architecture. As soon as more sizes are supposed
1461 * to be supported, SUPERPAGE_SIZE has to be replaced
1462 * with a lookup of the size depending on superpage_size.
1465 case SUPERPAGE_SIZE_ANY
:
1466 /* handle it like 2 MB and round up to page size */
1467 size
= (size
+ 2*1024*1024 - 1) & ~(2*1024*1024 - 1);
1468 case SUPERPAGE_SIZE_2MB
:
1472 return KERN_INVALID_ARGUMENT
;
1474 mask
= SUPERPAGE_SIZE
-1;
1475 if (size
& (SUPERPAGE_SIZE
-1))
1476 return KERN_INVALID_ARGUMENT
;
1477 inheritance
= VM_INHERIT_NONE
; /* fork() children won't inherit superpages */
1482 if (cur_protection
& VM_PROT_WRITE
){
1483 if ((cur_protection
& VM_PROT_EXECUTE
) && !(flags
& VM_FLAGS_MAP_JIT
)){
1484 printf("EMBEDDED: %s curprot cannot be write+execute. turning off execute\n", __PRETTY_FUNCTION__
);
1485 cur_protection
&= ~VM_PROT_EXECUTE
;
1488 #endif /* CONFIG_EMBEDDED */
1492 /* submaps can not be purgeable */
1493 return KERN_INVALID_ARGUMENT
;
1495 if (object
== VM_OBJECT_NULL
) {
1496 /* submaps can not be created lazily */
1497 return KERN_INVALID_ARGUMENT
;
1500 if (flags
& VM_FLAGS_ALREADY
) {
1502 * VM_FLAGS_ALREADY says that it's OK if the same mapping
1503 * is already present. For it to be meaningul, the requested
1504 * mapping has to be at a fixed address (!VM_FLAGS_ANYWHERE) and
1505 * we shouldn't try and remove what was mapped there first
1506 * (!VM_FLAGS_OVERWRITE).
1508 if ((flags
& VM_FLAGS_ANYWHERE
) ||
1509 (flags
& VM_FLAGS_OVERWRITE
)) {
1510 return KERN_INVALID_ARGUMENT
;
1514 effective_min_offset
= map
->min_offset
;
1516 if (flags
& VM_FLAGS_BEYOND_MAX
) {
1518 * Allow an insertion beyond the map's max offset.
1520 if (vm_map_is_64bit(map
))
1521 effective_max_offset
= 0xFFFFFFFFFFFFF000ULL
;
1523 effective_max_offset
= 0x00000000FFFFF000ULL
;
1525 effective_max_offset
= map
->max_offset
;
1529 (offset
& PAGE_MASK_64
) != 0) {
1531 return KERN_INVALID_ARGUMENT
;
1534 VM_GET_FLAGS_ALIAS(flags
, alias
);
1536 #define RETURN(value) { result = value; goto BailOut; }
1538 assert(page_aligned(*address
));
1539 assert(page_aligned(size
));
1542 * Only zero-fill objects are allowed to be purgable.
1543 * LP64todo - limit purgable objects to 32-bits for now
1547 (object
!= VM_OBJECT_NULL
&&
1548 (object
->vo_size
!= size
||
1549 object
->purgable
== VM_PURGABLE_DENY
))
1550 || size
> ANON_MAX_SIZE
)) /* LP64todo: remove when dp capable */
1551 return KERN_INVALID_ARGUMENT
;
1553 if (!anywhere
&& overwrite
) {
1555 * Create a temporary VM map to hold the old mappings in the
1556 * affected area while we create the new one.
1557 * This avoids releasing the VM map lock in
1558 * vm_map_entry_delete() and allows atomicity
1559 * when we want to replace some mappings with a new one.
1560 * It also allows us to restore the old VM mappings if the
1561 * new mapping fails.
1563 zap_old_map
= vm_map_create(PMAP_NULL
,
1566 map
->hdr
.entries_pageable
);
1577 if ((flags
& VM_FLAGS_MAP_JIT
) && (map
->jit_entry_exists
)){
1578 result
= KERN_INVALID_ARGUMENT
;
1583 * Calculate the first possible address.
1586 if (start
< effective_min_offset
)
1587 start
= effective_min_offset
;
1588 if (start
> effective_max_offset
)
1589 RETURN(KERN_NO_SPACE
);
1592 * Look for the first possible address;
1593 * if there's already something at this
1594 * address, we have to start after it.
1597 if( map
->disable_vmentry_reuse
== TRUE
) {
1598 VM_MAP_HIGHEST_ENTRY(map
, entry
, start
);
1600 assert(first_free_is_valid(map
));
1602 entry
= map
->first_free
;
1604 if (entry
== vm_map_to_entry(map
)) {
1607 if (entry
->vme_next
== vm_map_to_entry(map
)){
1609 * Hole at the end of the map.
1613 if (start
< (entry
->vme_next
)->vme_start
) {
1614 start
= entry
->vme_end
;
1617 * Need to do a lookup.
1624 if (entry
== NULL
) {
1625 vm_map_entry_t tmp_entry
;
1626 if (vm_map_lookup_entry(map
, start
, &tmp_entry
))
1627 start
= tmp_entry
->vme_end
;
1633 * In any case, the "entry" always precedes
1634 * the proposed new region throughout the
1639 register vm_map_entry_t next
;
1642 * Find the end of the proposed new region.
1643 * Be sure we didn't go beyond the end, or
1644 * wrap around the address.
1647 end
= ((start
+ mask
) & ~mask
);
1649 RETURN(KERN_NO_SPACE
);
1653 if ((end
> effective_max_offset
) || (end
< start
)) {
1654 if (map
->wait_for_space
) {
1655 if (size
<= (effective_max_offset
-
1656 effective_min_offset
)) {
1657 assert_wait((event_t
)map
,
1661 thread_block(THREAD_CONTINUE_NULL
);
1665 RETURN(KERN_NO_SPACE
);
1669 * If there are no more entries, we must win.
1672 next
= entry
->vme_next
;
1673 if (next
== vm_map_to_entry(map
))
1677 * If there is another entry, it must be
1678 * after the end of the potential new region.
1681 if (next
->vme_start
>= end
)
1685 * Didn't fit -- move to the next entry.
1689 start
= entry
->vme_end
;
1695 * the address doesn't itself violate
1696 * the mask requirement.
1701 if ((start
& mask
) != 0)
1702 RETURN(KERN_NO_SPACE
);
1705 * ... the address is within bounds
1710 if ((start
< effective_min_offset
) ||
1711 (end
> effective_max_offset
) ||
1713 RETURN(KERN_INVALID_ADDRESS
);
1716 if (overwrite
&& zap_old_map
!= VM_MAP_NULL
) {
1718 * Fixed mapping and "overwrite" flag: attempt to
1719 * remove all existing mappings in the specified
1720 * address range, saving them in our "zap_old_map".
1722 (void) vm_map_delete(map
, start
, end
,
1723 VM_MAP_REMOVE_SAVE_ENTRIES
,
1728 * ... the starting address isn't allocated
1731 if (vm_map_lookup_entry(map
, start
, &entry
)) {
1732 if (! (flags
& VM_FLAGS_ALREADY
)) {
1733 RETURN(KERN_NO_SPACE
);
1736 * Check if what's already there is what we want.
1739 tmp_offset
= offset
;
1740 if (entry
->vme_start
< start
) {
1741 tmp_start
-= start
- entry
->vme_start
;
1742 tmp_offset
-= start
- entry
->vme_start
;
1745 for (; entry
->vme_start
< end
;
1746 entry
= entry
->vme_next
) {
1748 * Check if the mapping's attributes
1749 * match the existing map entry.
1751 if (entry
== vm_map_to_entry(map
) ||
1752 entry
->vme_start
!= tmp_start
||
1753 entry
->is_sub_map
!= is_submap
||
1754 entry
->offset
!= tmp_offset
||
1755 entry
->needs_copy
!= needs_copy
||
1756 entry
->protection
!= cur_protection
||
1757 entry
->max_protection
!= max_protection
||
1758 entry
->inheritance
!= inheritance
||
1759 entry
->alias
!= alias
) {
1760 /* not the same mapping ! */
1761 RETURN(KERN_NO_SPACE
);
1764 * Check if the same object is being mapped.
1767 if (entry
->object
.sub_map
!=
1768 (vm_map_t
) object
) {
1769 /* not the same submap */
1770 RETURN(KERN_NO_SPACE
);
1773 if (entry
->object
.vm_object
!= object
) {
1774 /* not the same VM object... */
1777 obj2
= entry
->object
.vm_object
;
1778 if ((obj2
== VM_OBJECT_NULL
||
1780 (object
== VM_OBJECT_NULL
||
1781 object
->internal
)) {
1788 RETURN(KERN_NO_SPACE
);
1793 tmp_offset
+= entry
->vme_end
- entry
->vme_start
;
1794 tmp_start
+= entry
->vme_end
- entry
->vme_start
;
1795 if (entry
->vme_end
>= end
) {
1796 /* reached the end of our mapping */
1800 /* it all matches: let's use what's already there ! */
1801 RETURN(KERN_MEMORY_PRESENT
);
1805 * ... the next region doesn't overlap the
1809 if ((entry
->vme_next
!= vm_map_to_entry(map
)) &&
1810 (entry
->vme_next
->vme_start
< end
))
1811 RETURN(KERN_NO_SPACE
);
1816 * "start" and "end" should define the endpoints of the
1817 * available new range, and
1818 * "entry" should refer to the region before the new
1821 * the map should be locked.
1825 * See whether we can avoid creating a new entry (and object) by
1826 * extending one of our neighbors. [So far, we only attempt to
1827 * extend from below.] Note that we can never extend/join
1828 * purgable objects because they need to remain distinct
1829 * entities in order to implement their "volatile object"
1834 if (object
== VM_OBJECT_NULL
) {
1835 object
= vm_object_allocate(size
);
1836 object
->copy_strategy
= MEMORY_OBJECT_COPY_NONE
;
1837 object
->purgable
= VM_PURGABLE_NONVOLATILE
;
1838 offset
= (vm_object_offset_t
)0;
1840 } else if ((is_submap
== FALSE
) &&
1841 (object
== VM_OBJECT_NULL
) &&
1842 (entry
!= vm_map_to_entry(map
)) &&
1843 (entry
->vme_end
== start
) &&
1844 (!entry
->is_shared
) &&
1845 (!entry
->is_sub_map
) &&
1846 ((alias
== VM_MEMORY_REALLOC
) || (entry
->alias
== alias
)) &&
1847 (entry
->inheritance
== inheritance
) &&
1848 (entry
->protection
== cur_protection
) &&
1849 (entry
->max_protection
== max_protection
) &&
1850 (entry
->behavior
== VM_BEHAVIOR_DEFAULT
) &&
1851 (entry
->in_transition
== 0) &&
1852 (entry
->no_cache
== no_cache
) &&
1853 ((entry
->vme_end
- entry
->vme_start
) + size
<=
1854 (alias
== VM_MEMORY_REALLOC
?
1856 NO_COALESCE_LIMIT
)) &&
1857 (entry
->wired_count
== 0)) { /* implies user_wired_count == 0 */
1858 if (vm_object_coalesce(entry
->object
.vm_object
,
1861 (vm_object_offset_t
) 0,
1862 (vm_map_size_t
)(entry
->vme_end
- entry
->vme_start
),
1863 (vm_map_size_t
)(end
- entry
->vme_end
))) {
1866 * Coalesced the two objects - can extend
1867 * the previous map entry to include the
1870 map
->size
+= (end
- entry
->vme_end
);
1871 entry
->vme_end
= end
;
1872 vm_map_store_update_first_free(map
, map
->first_free
);
1873 RETURN(KERN_SUCCESS
);
1877 step
= superpage_size
? SUPERPAGE_SIZE
: (end
- start
);
1880 for (tmp2_start
= start
; tmp2_start
<end
; tmp2_start
+= step
) {
1881 tmp2_end
= tmp2_start
+ step
;
1883 * Create a new entry
1884 * LP64todo - for now, we can only allocate 4GB internal objects
1885 * because the default pager can't page bigger ones. Remove this
1889 * The reserved "page zero" in each process's address space can
1890 * be arbitrarily large. Splitting it into separate 4GB objects and
1891 * therefore different VM map entries serves no purpose and just
1892 * slows down operations on the VM map, so let's not split the
1893 * allocation into 4GB chunks if the max protection is NONE. That
1894 * memory should never be accessible, so it will never get to the
1897 tmp_start
= tmp2_start
;
1898 if (object
== VM_OBJECT_NULL
&&
1899 size
> (vm_map_size_t
)ANON_CHUNK_SIZE
&&
1900 max_protection
!= VM_PROT_NONE
&&
1901 superpage_size
== 0)
1902 tmp_end
= tmp_start
+ (vm_map_size_t
)ANON_CHUNK_SIZE
;
1906 new_entry
= vm_map_entry_insert(map
, entry
, tmp_start
, tmp_end
,
1907 object
, offset
, needs_copy
,
1909 cur_protection
, max_protection
,
1910 VM_BEHAVIOR_DEFAULT
,
1911 (flags
& VM_FLAGS_MAP_JIT
)? VM_INHERIT_NONE
: inheritance
,
1913 permanent
, superpage_size
);
1914 new_entry
->alias
= alias
;
1915 if (flags
& VM_FLAGS_MAP_JIT
){
1916 if (!(map
->jit_entry_exists
)){
1917 new_entry
->used_for_jit
= TRUE
;
1918 map
->jit_entry_exists
= TRUE
;
1924 boolean_t submap_is_64bit
;
1927 new_entry
->is_sub_map
= TRUE
;
1928 submap
= (vm_map_t
) object
;
1929 submap_is_64bit
= vm_map_is_64bit(submap
);
1930 use_pmap
= (alias
== VM_MEMORY_SHARED_PMAP
);
1931 #ifndef NO_NESTED_PMAP
1932 if (use_pmap
&& submap
->pmap
== NULL
) {
1933 /* we need a sub pmap to nest... */
1934 submap
->pmap
= pmap_create(0, submap_is_64bit
);
1935 if (submap
->pmap
== NULL
) {
1936 /* let's proceed without nesting... */
1939 if (use_pmap
&& submap
->pmap
!= NULL
) {
1940 kr
= pmap_nest(map
->pmap
,
1944 tmp_end
- tmp_start
);
1945 if (kr
!= KERN_SUCCESS
) {
1946 printf("vm_map_enter: "
1947 "pmap_nest(0x%llx,0x%llx) "
1949 (long long)tmp_start
,
1953 /* we're now nested ! */
1954 new_entry
->use_pmap
= TRUE
;
1958 #endif /* NO_NESTED_PMAP */
1962 if (superpage_size
) {
1964 vm_object_t sp_object
;
1968 /* allocate one superpage */
1969 kr
= cpm_allocate(SUPERPAGE_SIZE
, &pages
, 0, SUPERPAGE_NBASEPAGES
-1, TRUE
, 0);
1970 if (kr
!= KERN_SUCCESS
) {
1971 new_mapping_established
= TRUE
; /* will cause deallocation of whole range */
1975 /* create one vm_object per superpage */
1976 sp_object
= vm_object_allocate((vm_map_size_t
)(entry
->vme_end
- entry
->vme_start
));
1977 sp_object
->phys_contiguous
= TRUE
;
1978 sp_object
->vo_shadow_offset
= (vm_object_offset_t
)pages
->phys_page
*PAGE_SIZE
;
1979 entry
->object
.vm_object
= sp_object
;
1981 /* enter the base pages into the object */
1982 vm_object_lock(sp_object
);
1983 for (offset
= 0; offset
< SUPERPAGE_SIZE
; offset
+= PAGE_SIZE
) {
1985 pmap_zero_page(m
->phys_page
);
1986 pages
= NEXT_PAGE(m
);
1987 *(NEXT_PAGE_PTR(m
)) = VM_PAGE_NULL
;
1988 vm_page_insert(m
, sp_object
, offset
);
1990 vm_object_unlock(sp_object
);
1992 } while (tmp_end
!= tmp2_end
&&
1993 (tmp_start
= tmp_end
) &&
1994 (tmp_end
= (tmp2_end
- tmp_end
> (vm_map_size_t
)ANON_CHUNK_SIZE
) ?
1995 tmp_end
+ (vm_map_size_t
)ANON_CHUNK_SIZE
: tmp2_end
));
2001 new_mapping_established
= TRUE
;
2003 /* Wire down the new entry if the user
2004 * requested all new map entries be wired.
2006 if ((map
->wiring_required
)||(superpage_size
)) {
2007 pmap_empty
= FALSE
; /* pmap won't be empty */
2008 result
= vm_map_wire(map
, start
, end
,
2009 new_entry
->protection
, TRUE
);
2013 if ((object
!= VM_OBJECT_NULL
) &&
2014 (vm_map_pmap_enter_enable
) &&
2017 (size
< (128*1024))) {
2018 pmap_empty
= FALSE
; /* pmap won't be empty */
2020 if (override_nx(map
, alias
) && cur_protection
)
2021 cur_protection
|= VM_PROT_EXECUTE
;
2023 vm_map_pmap_enter(map
, start
, end
,
2024 object
, offset
, cur_protection
);
2028 if (result
== KERN_SUCCESS
) {
2029 vm_prot_t pager_prot
;
2030 memory_object_t pager
;
2033 !(flags
& VM_FLAGS_NO_PMAP_CHECK
)) {
2034 assert(vm_map_pmap_is_empty(map
,
2040 * For "named" VM objects, let the pager know that the
2041 * memory object is being mapped. Some pagers need to keep
2042 * track of this, to know when they can reclaim the memory
2043 * object, for example.
2044 * VM calls memory_object_map() for each mapping (specifying
2045 * the protection of each mapping) and calls
2046 * memory_object_last_unmap() when all the mappings are gone.
2048 pager_prot
= max_protection
;
2051 * Copy-On-Write mapping: won't modify
2052 * the memory object.
2054 pager_prot
&= ~VM_PROT_WRITE
;
2057 object
!= VM_OBJECT_NULL
&&
2059 object
->pager
!= MEMORY_OBJECT_NULL
) {
2060 vm_object_lock(object
);
2061 pager
= object
->pager
;
2062 if (object
->named
&&
2063 pager
!= MEMORY_OBJECT_NULL
) {
2064 assert(object
->pager_ready
);
2065 vm_object_mapping_wait(object
, THREAD_UNINT
);
2066 vm_object_mapping_begin(object
);
2067 vm_object_unlock(object
);
2069 kr
= memory_object_map(pager
, pager_prot
);
2070 assert(kr
== KERN_SUCCESS
);
2072 vm_object_lock(object
);
2073 vm_object_mapping_end(object
);
2075 vm_object_unlock(object
);
2078 if (new_mapping_established
) {
2080 * We have to get rid of the new mappings since we
2081 * won't make them available to the user.
2082 * Try and do that atomically, to minimize the risk
2083 * that someone else create new mappings that range.
2085 zap_new_map
= vm_map_create(PMAP_NULL
,
2088 map
->hdr
.entries_pageable
);
2093 (void) vm_map_delete(map
, *address
, *address
+size
,
2094 VM_MAP_REMOVE_SAVE_ENTRIES
,
2097 if (zap_old_map
!= VM_MAP_NULL
&&
2098 zap_old_map
->hdr
.nentries
!= 0) {
2099 vm_map_entry_t entry1
, entry2
;
2102 * The new mapping failed. Attempt to restore
2103 * the old mappings, saved in the "zap_old_map".
2110 /* first check if the coast is still clear */
2111 start
= vm_map_first_entry(zap_old_map
)->vme_start
;
2112 end
= vm_map_last_entry(zap_old_map
)->vme_end
;
2113 if (vm_map_lookup_entry(map
, start
, &entry1
) ||
2114 vm_map_lookup_entry(map
, end
, &entry2
) ||
2117 * Part of that range has already been
2118 * re-mapped: we can't restore the old
2121 vm_map_enter_restore_failures
++;
2124 * Transfer the saved map entries from
2125 * "zap_old_map" to the original "map",
2126 * inserting them all after "entry1".
2128 for (entry2
= vm_map_first_entry(zap_old_map
);
2129 entry2
!= vm_map_to_entry(zap_old_map
);
2130 entry2
= vm_map_first_entry(zap_old_map
)) {
2131 vm_map_size_t entry_size
;
2133 entry_size
= (entry2
->vme_end
-
2135 vm_map_store_entry_unlink(zap_old_map
,
2137 zap_old_map
->size
-= entry_size
;
2138 vm_map_store_entry_link(map
, entry1
, entry2
);
2139 map
->size
+= entry_size
;
2142 if (map
->wiring_required
) {
2144 * XXX TODO: we should rewire the
2148 vm_map_enter_restore_successes
++;
2158 * Get rid of the "zap_maps" and all the map entries that
2159 * they may still contain.
2161 if (zap_old_map
!= VM_MAP_NULL
) {
2162 vm_map_destroy(zap_old_map
, VM_MAP_REMOVE_NO_PMAP_CLEANUP
);
2163 zap_old_map
= VM_MAP_NULL
;
2165 if (zap_new_map
!= VM_MAP_NULL
) {
2166 vm_map_destroy(zap_new_map
, VM_MAP_REMOVE_NO_PMAP_CLEANUP
);
2167 zap_new_map
= VM_MAP_NULL
;
2176 vm_map_enter_mem_object(
2177 vm_map_t target_map
,
2178 vm_map_offset_t
*address
,
2179 vm_map_size_t initial_size
,
2180 vm_map_offset_t mask
,
2183 vm_object_offset_t offset
,
2185 vm_prot_t cur_protection
,
2186 vm_prot_t max_protection
,
2187 vm_inherit_t inheritance
)
2189 vm_map_address_t map_addr
;
2190 vm_map_size_t map_size
;
2192 vm_object_size_t size
;
2193 kern_return_t result
;
2194 boolean_t mask_cur_protection
, mask_max_protection
;
2196 mask_cur_protection
= cur_protection
& VM_PROT_IS_MASK
;
2197 mask_max_protection
= max_protection
& VM_PROT_IS_MASK
;
2198 cur_protection
&= ~VM_PROT_IS_MASK
;
2199 max_protection
&= ~VM_PROT_IS_MASK
;
2202 * Check arguments for validity
2204 if ((target_map
== VM_MAP_NULL
) ||
2205 (cur_protection
& ~VM_PROT_ALL
) ||
2206 (max_protection
& ~VM_PROT_ALL
) ||
2207 (inheritance
> VM_INHERIT_LAST_VALID
) ||
2209 return KERN_INVALID_ARGUMENT
;
2211 map_addr
= vm_map_trunc_page(*address
);
2212 map_size
= vm_map_round_page(initial_size
);
2213 size
= vm_object_round_page(initial_size
);
2216 * Find the vm object (if any) corresponding to this port.
2218 if (!IP_VALID(port
)) {
2219 object
= VM_OBJECT_NULL
;
2222 } else if (ip_kotype(port
) == IKOT_NAMED_ENTRY
) {
2223 vm_named_entry_t named_entry
;
2225 named_entry
= (vm_named_entry_t
) port
->ip_kobject
;
2226 /* a few checks to make sure user is obeying rules */
2228 if (offset
>= named_entry
->size
)
2229 return KERN_INVALID_RIGHT
;
2230 size
= named_entry
->size
- offset
;
2232 if (mask_max_protection
) {
2233 max_protection
&= named_entry
->protection
;
2235 if (mask_cur_protection
) {
2236 cur_protection
&= named_entry
->protection
;
2238 if ((named_entry
->protection
& max_protection
) !=
2240 return KERN_INVALID_RIGHT
;
2241 if ((named_entry
->protection
& cur_protection
) !=
2243 return KERN_INVALID_RIGHT
;
2244 if (named_entry
->size
< (offset
+ size
))
2245 return KERN_INVALID_ARGUMENT
;
2247 /* the callers parameter offset is defined to be the */
2248 /* offset from beginning of named entry offset in object */
2249 offset
= offset
+ named_entry
->offset
;
2251 named_entry_lock(named_entry
);
2252 if (named_entry
->is_sub_map
) {
2255 submap
= named_entry
->backing
.map
;
2256 vm_map_lock(submap
);
2257 vm_map_reference(submap
);
2258 vm_map_unlock(submap
);
2259 named_entry_unlock(named_entry
);
2261 result
= vm_map_enter(target_map
,
2265 flags
| VM_FLAGS_SUBMAP
,
2266 (vm_object_t
) submap
,
2272 if (result
!= KERN_SUCCESS
) {
2273 vm_map_deallocate(submap
);
2276 * No need to lock "submap" just to check its
2277 * "mapped" flag: that flag is never reset
2278 * once it's been set and if we race, we'll
2279 * just end up setting it twice, which is OK.
2281 if (submap
->mapped
== FALSE
) {
2283 * This submap has never been mapped.
2284 * Set its "mapped" flag now that it
2286 * This happens only for the first ever
2287 * mapping of a "submap".
2289 vm_map_lock(submap
);
2290 submap
->mapped
= TRUE
;
2291 vm_map_unlock(submap
);
2293 *address
= map_addr
;
2297 } else if (named_entry
->is_pager
) {
2298 unsigned int access
;
2299 vm_prot_t protections
;
2300 unsigned int wimg_mode
;
2302 protections
= named_entry
->protection
& VM_PROT_ALL
;
2303 access
= GET_MAP_MEM(named_entry
->protection
);
2305 object
= vm_object_enter(named_entry
->backing
.pager
,
2307 named_entry
->internal
,
2310 if (object
== VM_OBJECT_NULL
) {
2311 named_entry_unlock(named_entry
);
2312 return KERN_INVALID_OBJECT
;
2315 /* JMM - drop reference on pager here */
2317 /* create an extra ref for the named entry */
2318 vm_object_lock(object
);
2319 vm_object_reference_locked(object
);
2320 named_entry
->backing
.object
= object
;
2321 named_entry
->is_pager
= FALSE
;
2322 named_entry_unlock(named_entry
);
2324 wimg_mode
= object
->wimg_bits
;
2326 if (access
== MAP_MEM_IO
) {
2327 wimg_mode
= VM_WIMG_IO
;
2328 } else if (access
== MAP_MEM_COPYBACK
) {
2329 wimg_mode
= VM_WIMG_USE_DEFAULT
;
2330 } else if (access
== MAP_MEM_WTHRU
) {
2331 wimg_mode
= VM_WIMG_WTHRU
;
2332 } else if (access
== MAP_MEM_WCOMB
) {
2333 wimg_mode
= VM_WIMG_WCOMB
;
2336 /* wait for object (if any) to be ready */
2337 if (!named_entry
->internal
) {
2338 while (!object
->pager_ready
) {
2341 VM_OBJECT_EVENT_PAGER_READY
,
2343 vm_object_lock(object
);
2347 if (object
->wimg_bits
!= wimg_mode
)
2348 vm_object_change_wimg_mode(object
, wimg_mode
);
2350 object
->true_share
= TRUE
;
2352 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
)
2353 object
->copy_strategy
= MEMORY_OBJECT_COPY_DELAY
;
2354 vm_object_unlock(object
);
2356 /* This is the case where we are going to map */
2357 /* an already mapped object. If the object is */
2358 /* not ready it is internal. An external */
2359 /* object cannot be mapped until it is ready */
2360 /* we can therefore avoid the ready check */
2362 object
= named_entry
->backing
.object
;
2363 assert(object
!= VM_OBJECT_NULL
);
2364 named_entry_unlock(named_entry
);
2365 vm_object_reference(object
);
2367 } else if (ip_kotype(port
) == IKOT_MEMORY_OBJECT
) {
2369 * JMM - This is temporary until we unify named entries
2370 * and raw memory objects.
2372 * Detected fake ip_kotype for a memory object. In
2373 * this case, the port isn't really a port at all, but
2374 * instead is just a raw memory object.
2377 object
= vm_object_enter((memory_object_t
)port
,
2378 size
, FALSE
, FALSE
, FALSE
);
2379 if (object
== VM_OBJECT_NULL
)
2380 return KERN_INVALID_OBJECT
;
2382 /* wait for object (if any) to be ready */
2383 if (object
!= VM_OBJECT_NULL
) {
2384 if (object
== kernel_object
) {
2385 printf("Warning: Attempt to map kernel object"
2386 " by a non-private kernel entity\n");
2387 return KERN_INVALID_OBJECT
;
2389 if (!object
->pager_ready
) {
2390 vm_object_lock(object
);
2392 while (!object
->pager_ready
) {
2393 vm_object_wait(object
,
2394 VM_OBJECT_EVENT_PAGER_READY
,
2396 vm_object_lock(object
);
2398 vm_object_unlock(object
);
2402 return KERN_INVALID_OBJECT
;
2405 if (object
!= VM_OBJECT_NULL
&&
2407 object
->pager
!= MEMORY_OBJECT_NULL
&&
2408 object
->copy_strategy
!= MEMORY_OBJECT_COPY_NONE
) {
2409 memory_object_t pager
;
2410 vm_prot_t pager_prot
;
2414 * For "named" VM objects, let the pager know that the
2415 * memory object is being mapped. Some pagers need to keep
2416 * track of this, to know when they can reclaim the memory
2417 * object, for example.
2418 * VM calls memory_object_map() for each mapping (specifying
2419 * the protection of each mapping) and calls
2420 * memory_object_last_unmap() when all the mappings are gone.
2422 pager_prot
= max_protection
;
2425 * Copy-On-Write mapping: won't modify the
2428 pager_prot
&= ~VM_PROT_WRITE
;
2430 vm_object_lock(object
);
2431 pager
= object
->pager
;
2432 if (object
->named
&&
2433 pager
!= MEMORY_OBJECT_NULL
&&
2434 object
->copy_strategy
!= MEMORY_OBJECT_COPY_NONE
) {
2435 assert(object
->pager_ready
);
2436 vm_object_mapping_wait(object
, THREAD_UNINT
);
2437 vm_object_mapping_begin(object
);
2438 vm_object_unlock(object
);
2440 kr
= memory_object_map(pager
, pager_prot
);
2441 assert(kr
== KERN_SUCCESS
);
2443 vm_object_lock(object
);
2444 vm_object_mapping_end(object
);
2446 vm_object_unlock(object
);
2450 * Perform the copy if requested
2454 vm_object_t new_object
;
2455 vm_object_offset_t new_offset
;
2457 result
= vm_object_copy_strategically(object
, offset
, size
,
2458 &new_object
, &new_offset
,
2462 if (result
== KERN_MEMORY_RESTART_COPY
) {
2464 boolean_t src_needs_copy
;
2468 * We currently ignore src_needs_copy.
2469 * This really is the issue of how to make
2470 * MEMORY_OBJECT_COPY_SYMMETRIC safe for
2471 * non-kernel users to use. Solution forthcoming.
2472 * In the meantime, since we don't allow non-kernel
2473 * memory managers to specify symmetric copy,
2474 * we won't run into problems here.
2476 new_object
= object
;
2477 new_offset
= offset
;
2478 success
= vm_object_copy_quickly(&new_object
,
2483 result
= KERN_SUCCESS
;
2486 * Throw away the reference to the
2487 * original object, as it won't be mapped.
2490 vm_object_deallocate(object
);
2492 if (result
!= KERN_SUCCESS
)
2495 object
= new_object
;
2496 offset
= new_offset
;
2499 result
= vm_map_enter(target_map
,
2500 &map_addr
, map_size
,
2501 (vm_map_offset_t
)mask
,
2505 cur_protection
, max_protection
, inheritance
);
2506 if (result
!= KERN_SUCCESS
)
2507 vm_object_deallocate(object
);
2508 *address
= map_addr
;
2516 vm_map_enter_mem_object_control(
2517 vm_map_t target_map
,
2518 vm_map_offset_t
*address
,
2519 vm_map_size_t initial_size
,
2520 vm_map_offset_t mask
,
2522 memory_object_control_t control
,
2523 vm_object_offset_t offset
,
2525 vm_prot_t cur_protection
,
2526 vm_prot_t max_protection
,
2527 vm_inherit_t inheritance
)
2529 vm_map_address_t map_addr
;
2530 vm_map_size_t map_size
;
2532 vm_object_size_t size
;
2533 kern_return_t result
;
2534 memory_object_t pager
;
2535 vm_prot_t pager_prot
;
2539 * Check arguments for validity
2541 if ((target_map
== VM_MAP_NULL
) ||
2542 (cur_protection
& ~VM_PROT_ALL
) ||
2543 (max_protection
& ~VM_PROT_ALL
) ||
2544 (inheritance
> VM_INHERIT_LAST_VALID
) ||
2546 return KERN_INVALID_ARGUMENT
;
2548 map_addr
= vm_map_trunc_page(*address
);
2549 map_size
= vm_map_round_page(initial_size
);
2550 size
= vm_object_round_page(initial_size
);
2552 object
= memory_object_control_to_vm_object(control
);
2554 if (object
== VM_OBJECT_NULL
)
2555 return KERN_INVALID_OBJECT
;
2557 if (object
== kernel_object
) {
2558 printf("Warning: Attempt to map kernel object"
2559 " by a non-private kernel entity\n");
2560 return KERN_INVALID_OBJECT
;
2563 vm_object_lock(object
);
2564 object
->ref_count
++;
2565 vm_object_res_reference(object
);
2568 * For "named" VM objects, let the pager know that the
2569 * memory object is being mapped. Some pagers need to keep
2570 * track of this, to know when they can reclaim the memory
2571 * object, for example.
2572 * VM calls memory_object_map() for each mapping (specifying
2573 * the protection of each mapping) and calls
2574 * memory_object_last_unmap() when all the mappings are gone.
2576 pager_prot
= max_protection
;
2578 pager_prot
&= ~VM_PROT_WRITE
;
2580 pager
= object
->pager
;
2581 if (object
->named
&&
2582 pager
!= MEMORY_OBJECT_NULL
&&
2583 object
->copy_strategy
!= MEMORY_OBJECT_COPY_NONE
) {
2584 assert(object
->pager_ready
);
2585 vm_object_mapping_wait(object
, THREAD_UNINT
);
2586 vm_object_mapping_begin(object
);
2587 vm_object_unlock(object
);
2589 kr
= memory_object_map(pager
, pager_prot
);
2590 assert(kr
== KERN_SUCCESS
);
2592 vm_object_lock(object
);
2593 vm_object_mapping_end(object
);
2595 vm_object_unlock(object
);
2598 * Perform the copy if requested
2602 vm_object_t new_object
;
2603 vm_object_offset_t new_offset
;
2605 result
= vm_object_copy_strategically(object
, offset
, size
,
2606 &new_object
, &new_offset
,
2610 if (result
== KERN_MEMORY_RESTART_COPY
) {
2612 boolean_t src_needs_copy
;
2616 * We currently ignore src_needs_copy.
2617 * This really is the issue of how to make
2618 * MEMORY_OBJECT_COPY_SYMMETRIC safe for
2619 * non-kernel users to use. Solution forthcoming.
2620 * In the meantime, since we don't allow non-kernel
2621 * memory managers to specify symmetric copy,
2622 * we won't run into problems here.
2624 new_object
= object
;
2625 new_offset
= offset
;
2626 success
= vm_object_copy_quickly(&new_object
,
2631 result
= KERN_SUCCESS
;
2634 * Throw away the reference to the
2635 * original object, as it won't be mapped.
2638 vm_object_deallocate(object
);
2640 if (result
!= KERN_SUCCESS
)
2643 object
= new_object
;
2644 offset
= new_offset
;
2647 result
= vm_map_enter(target_map
,
2648 &map_addr
, map_size
,
2649 (vm_map_offset_t
)mask
,
2653 cur_protection
, max_protection
, inheritance
);
2654 if (result
!= KERN_SUCCESS
)
2655 vm_object_deallocate(object
);
2656 *address
= map_addr
;
2665 extern pmap_paddr_t avail_start
, avail_end
;
2669 * Allocate memory in the specified map, with the caveat that
2670 * the memory is physically contiguous. This call may fail
2671 * if the system can't find sufficient contiguous memory.
2672 * This call may cause or lead to heart-stopping amounts of
2675 * Memory obtained from this call should be freed in the
2676 * normal way, viz., via vm_deallocate.
2681 vm_map_offset_t
*addr
,
2685 vm_object_t cpm_obj
;
2689 vm_map_offset_t va
, start
, end
, offset
;
2691 vm_map_offset_t prev_addr
;
2692 #endif /* MACH_ASSERT */
2694 boolean_t anywhere
= ((VM_FLAGS_ANYWHERE
& flags
) != 0);
2696 if (!vm_allocate_cpm_enabled
)
2697 return KERN_FAILURE
;
2701 return KERN_SUCCESS
;
2704 *addr
= vm_map_min(map
);
2706 *addr
= vm_map_trunc_page(*addr
);
2707 size
= vm_map_round_page(size
);
2710 * LP64todo - cpm_allocate should probably allow
2711 * allocations of >4GB, but not with the current
2712 * algorithm, so just cast down the size for now.
2714 if (size
> VM_MAX_ADDRESS
)
2715 return KERN_RESOURCE_SHORTAGE
;
2716 if ((kr
= cpm_allocate(CAST_DOWN(vm_size_t
, size
),
2717 &pages
, 0, 0, TRUE
, flags
)) != KERN_SUCCESS
)
2720 cpm_obj
= vm_object_allocate((vm_object_size_t
)size
);
2721 assert(cpm_obj
!= VM_OBJECT_NULL
);
2722 assert(cpm_obj
->internal
);
2723 assert(cpm_obj
->size
== (vm_object_size_t
)size
);
2724 assert(cpm_obj
->can_persist
== FALSE
);
2725 assert(cpm_obj
->pager_created
== FALSE
);
2726 assert(cpm_obj
->pageout
== FALSE
);
2727 assert(cpm_obj
->shadow
== VM_OBJECT_NULL
);
2730 * Insert pages into object.
2733 vm_object_lock(cpm_obj
);
2734 for (offset
= 0; offset
< size
; offset
+= PAGE_SIZE
) {
2736 pages
= NEXT_PAGE(m
);
2737 *(NEXT_PAGE_PTR(m
)) = VM_PAGE_NULL
;
2739 assert(!m
->gobbled
);
2741 assert(!m
->pageout
);
2743 assert(VM_PAGE_WIRED(m
));
2746 * "m" is not supposed to be pageable, so it
2747 * should not be encrypted. It wouldn't be safe
2748 * to enter it in a new VM object while encrypted.
2750 ASSERT_PAGE_DECRYPTED(m
);
2752 assert(m
->phys_page
>=(avail_start
>>PAGE_SHIFT
) && m
->phys_page
<=(avail_end
>>PAGE_SHIFT
));
2755 vm_page_insert(m
, cpm_obj
, offset
);
2757 assert(cpm_obj
->resident_page_count
== size
/ PAGE_SIZE
);
2758 vm_object_unlock(cpm_obj
);
2761 * Hang onto a reference on the object in case a
2762 * multi-threaded application for some reason decides
2763 * to deallocate the portion of the address space into
2764 * which we will insert this object.
2766 * Unfortunately, we must insert the object now before
2767 * we can talk to the pmap module about which addresses
2768 * must be wired down. Hence, the race with a multi-
2771 vm_object_reference(cpm_obj
);
2774 * Insert object into map.
2784 (vm_object_offset_t
)0,
2788 VM_INHERIT_DEFAULT
);
2790 if (kr
!= KERN_SUCCESS
) {
2792 * A CPM object doesn't have can_persist set,
2793 * so all we have to do is deallocate it to
2794 * free up these pages.
2796 assert(cpm_obj
->pager_created
== FALSE
);
2797 assert(cpm_obj
->can_persist
== FALSE
);
2798 assert(cpm_obj
->pageout
== FALSE
);
2799 assert(cpm_obj
->shadow
== VM_OBJECT_NULL
);
2800 vm_object_deallocate(cpm_obj
); /* kill acquired ref */
2801 vm_object_deallocate(cpm_obj
); /* kill creation ref */
2805 * Inform the physical mapping system that the
2806 * range of addresses may not fault, so that
2807 * page tables and such can be locked down as well.
2811 pmap
= vm_map_pmap(map
);
2812 pmap_pageable(pmap
, start
, end
, FALSE
);
2815 * Enter each page into the pmap, to avoid faults.
2816 * Note that this loop could be coded more efficiently,
2817 * if the need arose, rather than looking up each page
2820 for (offset
= 0, va
= start
; offset
< size
;
2821 va
+= PAGE_SIZE
, offset
+= PAGE_SIZE
) {
2824 vm_object_lock(cpm_obj
);
2825 m
= vm_page_lookup(cpm_obj
, (vm_object_offset_t
)offset
);
2826 assert(m
!= VM_PAGE_NULL
);
2828 vm_page_zero_fill(m
);
2830 type_of_fault
= DBG_ZERO_FILL_FAULT
;
2832 vm_fault_enter(m
, pmap
, va
, VM_PROT_ALL
, VM_PROT_WRITE
,
2833 VM_PAGE_WIRED(m
), FALSE
, FALSE
, FALSE
,
2836 vm_object_unlock(cpm_obj
);
2841 * Verify ordering in address space.
2843 for (offset
= 0; offset
< size
; offset
+= PAGE_SIZE
) {
2844 vm_object_lock(cpm_obj
);
2845 m
= vm_page_lookup(cpm_obj
, (vm_object_offset_t
)offset
);
2846 vm_object_unlock(cpm_obj
);
2847 if (m
== VM_PAGE_NULL
)
2848 panic("vm_allocate_cpm: obj 0x%x off 0x%x no page",
2853 assert(!m
->fictitious
);
2854 assert(!m
->private);
2857 assert(!m
->cleaning
);
2858 assert(!m
->precious
);
2859 assert(!m
->clustered
);
2861 if (m
->phys_page
!= prev_addr
+ 1) {
2862 printf("start 0x%x end 0x%x va 0x%x\n",
2864 printf("obj 0x%x off 0x%x\n", cpm_obj
, offset
);
2865 printf("m 0x%x prev_address 0x%x\n", m
,
2867 panic("vm_allocate_cpm: pages not contig!");
2870 prev_addr
= m
->phys_page
;
2872 #endif /* MACH_ASSERT */
2874 vm_object_deallocate(cpm_obj
); /* kill extra ref */
2883 * Interface is defined in all cases, but unless the kernel
2884 * is built explicitly for this option, the interface does
2890 __unused vm_map_t map
,
2891 __unused vm_map_offset_t
*addr
,
2892 __unused vm_map_size_t size
,
2895 return KERN_FAILURE
;
2899 /* Not used without nested pmaps */
2900 #ifndef NO_NESTED_PMAP
2902 * Clip and unnest a portion of a nested submap mapping.
2909 vm_map_entry_t entry
,
2910 vm_map_offset_t start_unnest
,
2911 vm_map_offset_t end_unnest
)
2913 vm_map_offset_t old_start_unnest
= start_unnest
;
2914 vm_map_offset_t old_end_unnest
= end_unnest
;
2916 assert(entry
->is_sub_map
);
2917 assert(entry
->object
.sub_map
!= NULL
);
2920 * Query the platform for the optimal unnest range.
2921 * DRK: There's some duplication of effort here, since
2922 * callers may have adjusted the range to some extent. This
2923 * routine was introduced to support 1GiB subtree nesting
2924 * for x86 platforms, which can also nest on 2MiB boundaries
2925 * depending on size/alignment.
2927 if (pmap_adjust_unnest_parameters(map
->pmap
, &start_unnest
, &end_unnest
)) {
2928 log_unnest_badness(map
, old_start_unnest
, old_end_unnest
);
2931 if (entry
->vme_start
> start_unnest
||
2932 entry
->vme_end
< end_unnest
) {
2933 panic("vm_map_clip_unnest(0x%llx,0x%llx): "
2934 "bad nested entry: start=0x%llx end=0x%llx\n",
2935 (long long)start_unnest
, (long long)end_unnest
,
2936 (long long)entry
->vme_start
, (long long)entry
->vme_end
);
2939 if (start_unnest
> entry
->vme_start
) {
2940 _vm_map_clip_start(&map
->hdr
,
2943 vm_map_store_update_first_free(map
, map
->first_free
);
2945 if (entry
->vme_end
> end_unnest
) {
2946 _vm_map_clip_end(&map
->hdr
,
2949 vm_map_store_update_first_free(map
, map
->first_free
);
2952 pmap_unnest(map
->pmap
,
2954 entry
->vme_end
- entry
->vme_start
);
2955 if ((map
->mapped
) && (map
->ref_count
)) {
2956 /* clean up parent map/maps */
2957 vm_map_submap_pmap_clean(
2958 map
, entry
->vme_start
,
2960 entry
->object
.sub_map
,
2963 entry
->use_pmap
= FALSE
;
2965 #endif /* NO_NESTED_PMAP */
2968 * vm_map_clip_start: [ internal use only ]
2970 * Asserts that the given entry begins at or after
2971 * the specified address; if necessary,
2972 * it splits the entry into two.
2977 vm_map_entry_t entry
,
2978 vm_map_offset_t startaddr
)
2980 #ifndef NO_NESTED_PMAP
2981 if (entry
->use_pmap
&&
2982 startaddr
>= entry
->vme_start
) {
2983 vm_map_offset_t start_unnest
, end_unnest
;
2986 * Make sure "startaddr" is no longer in a nested range
2987 * before we clip. Unnest only the minimum range the platform
2989 * vm_map_clip_unnest may perform additional adjustments to
2992 start_unnest
= startaddr
& ~(pmap_nesting_size_min
- 1);
2993 end_unnest
= start_unnest
+ pmap_nesting_size_min
;
2994 vm_map_clip_unnest(map
, entry
, start_unnest
, end_unnest
);
2996 #endif /* NO_NESTED_PMAP */
2997 if (startaddr
> entry
->vme_start
) {
2998 if (entry
->object
.vm_object
&&
2999 !entry
->is_sub_map
&&
3000 entry
->object
.vm_object
->phys_contiguous
) {
3001 pmap_remove(map
->pmap
,
3002 (addr64_t
)(entry
->vme_start
),
3003 (addr64_t
)(entry
->vme_end
));
3005 _vm_map_clip_start(&map
->hdr
, entry
, startaddr
);
3006 vm_map_store_update_first_free(map
, map
->first_free
);
3011 #define vm_map_copy_clip_start(copy, entry, startaddr) \
3013 if ((startaddr) > (entry)->vme_start) \
3014 _vm_map_clip_start(&(copy)->cpy_hdr,(entry),(startaddr)); \
3018 * This routine is called only when it is known that
3019 * the entry must be split.
3023 register struct vm_map_header
*map_header
,
3024 register vm_map_entry_t entry
,
3025 register vm_map_offset_t start
)
3027 register vm_map_entry_t new_entry
;
3030 * Split off the front portion --
3031 * note that we must insert the new
3032 * entry BEFORE this one, so that
3033 * this entry has the specified starting
3037 new_entry
= _vm_map_entry_create(map_header
);
3038 vm_map_entry_copy_full(new_entry
, entry
);
3040 new_entry
->vme_end
= start
;
3041 entry
->offset
+= (start
- entry
->vme_start
);
3042 entry
->vme_start
= start
;
3044 _vm_map_store_entry_link(map_header
, entry
->vme_prev
, new_entry
);
3046 if (entry
->is_sub_map
)
3047 vm_map_reference(new_entry
->object
.sub_map
);
3049 vm_object_reference(new_entry
->object
.vm_object
);
3054 * vm_map_clip_end: [ internal use only ]
3056 * Asserts that the given entry ends at or before
3057 * the specified address; if necessary,
3058 * it splits the entry into two.
3063 vm_map_entry_t entry
,
3064 vm_map_offset_t endaddr
)
3066 if (endaddr
> entry
->vme_end
) {
3068 * Within the scope of this clipping, limit "endaddr" to
3069 * the end of this map entry...
3071 endaddr
= entry
->vme_end
;
3073 #ifndef NO_NESTED_PMAP
3074 if (entry
->use_pmap
) {
3075 vm_map_offset_t start_unnest
, end_unnest
;
3078 * Make sure the range between the start of this entry and
3079 * the new "endaddr" is no longer nested before we clip.
3080 * Unnest only the minimum range the platform can handle.
3081 * vm_map_clip_unnest may perform additional adjustments to
3084 start_unnest
= entry
->vme_start
;
3086 (endaddr
+ pmap_nesting_size_min
- 1) &
3087 ~(pmap_nesting_size_min
- 1);
3088 vm_map_clip_unnest(map
, entry
, start_unnest
, end_unnest
);
3090 #endif /* NO_NESTED_PMAP */
3091 if (endaddr
< entry
->vme_end
) {
3092 if (entry
->object
.vm_object
&&
3093 !entry
->is_sub_map
&&
3094 entry
->object
.vm_object
->phys_contiguous
) {
3095 pmap_remove(map
->pmap
,
3096 (addr64_t
)(entry
->vme_start
),
3097 (addr64_t
)(entry
->vme_end
));
3099 _vm_map_clip_end(&map
->hdr
, entry
, endaddr
);
3100 vm_map_store_update_first_free(map
, map
->first_free
);
3105 #define vm_map_copy_clip_end(copy, entry, endaddr) \
3107 if ((endaddr) < (entry)->vme_end) \
3108 _vm_map_clip_end(&(copy)->cpy_hdr,(entry),(endaddr)); \
3112 * This routine is called only when it is known that
3113 * the entry must be split.
3117 register struct vm_map_header
*map_header
,
3118 register vm_map_entry_t entry
,
3119 register vm_map_offset_t end
)
3121 register vm_map_entry_t new_entry
;
3124 * Create a new entry and insert it
3125 * AFTER the specified entry
3128 new_entry
= _vm_map_entry_create(map_header
);
3129 vm_map_entry_copy_full(new_entry
, entry
);
3131 new_entry
->vme_start
= entry
->vme_end
= end
;
3132 new_entry
->offset
+= (end
- entry
->vme_start
);
3134 _vm_map_store_entry_link(map_header
, entry
, new_entry
);
3136 if (entry
->is_sub_map
)
3137 vm_map_reference(new_entry
->object
.sub_map
);
3139 vm_object_reference(new_entry
->object
.vm_object
);
3144 * VM_MAP_RANGE_CHECK: [ internal use only ]
3146 * Asserts that the starting and ending region
3147 * addresses fall within the valid range of the map.
3149 #define VM_MAP_RANGE_CHECK(map, start, end) \
3151 if (start < vm_map_min(map)) \
3152 start = vm_map_min(map); \
3153 if (end > vm_map_max(map)) \
3154 end = vm_map_max(map); \
3160 * vm_map_range_check: [ internal use only ]
3162 * Check that the region defined by the specified start and
3163 * end addresses are wholly contained within a single map
3164 * entry or set of adjacent map entries of the spacified map,
3165 * i.e. the specified region contains no unmapped space.
3166 * If any or all of the region is unmapped, FALSE is returned.
3167 * Otherwise, TRUE is returned and if the output argument 'entry'
3168 * is not NULL it points to the map entry containing the start
3171 * The map is locked for reading on entry and is left locked.
3175 register vm_map_t map
,
3176 register vm_map_offset_t start
,
3177 register vm_map_offset_t end
,
3178 vm_map_entry_t
*entry
)
3181 register vm_map_offset_t prev
;
3184 * Basic sanity checks first
3186 if (start
< vm_map_min(map
) || end
> vm_map_max(map
) || start
> end
)
3190 * Check first if the region starts within a valid
3191 * mapping for the map.
3193 if (!vm_map_lookup_entry(map
, start
, &cur
))
3197 * Optimize for the case that the region is contained
3198 * in a single map entry.
3200 if (entry
!= (vm_map_entry_t
*) NULL
)
3202 if (end
<= cur
->vme_end
)
3206 * If the region is not wholly contained within a
3207 * single entry, walk the entries looking for holes.
3209 prev
= cur
->vme_end
;
3210 cur
= cur
->vme_next
;
3211 while ((cur
!= vm_map_to_entry(map
)) && (prev
== cur
->vme_start
)) {
3212 if (end
<= cur
->vme_end
)
3214 prev
= cur
->vme_end
;
3215 cur
= cur
->vme_next
;
3221 * vm_map_submap: [ kernel use only ]
3223 * Mark the given range as handled by a subordinate map.
3225 * This range must have been created with vm_map_find using
3226 * the vm_submap_object, and no other operations may have been
3227 * performed on this range prior to calling vm_map_submap.
3229 * Only a limited number of operations can be performed
3230 * within this rage after calling vm_map_submap:
3232 * [Don't try vm_map_copyin!]
3234 * To remove a submapping, one must first remove the
3235 * range from the superior map, and then destroy the
3236 * submap (if desired). [Better yet, don't try it.]
3241 vm_map_offset_t start
,
3242 vm_map_offset_t end
,
3244 vm_map_offset_t offset
,
3245 #ifdef NO_NESTED_PMAP
3247 #endif /* NO_NESTED_PMAP */
3250 vm_map_entry_t entry
;
3251 register kern_return_t result
= KERN_INVALID_ARGUMENT
;
3252 register vm_object_t object
;
3256 if (! vm_map_lookup_entry(map
, start
, &entry
)) {
3257 entry
= entry
->vme_next
;
3260 if (entry
== vm_map_to_entry(map
) ||
3261 entry
->is_sub_map
) {
3263 return KERN_INVALID_ARGUMENT
;
3266 assert(!entry
->use_pmap
); /* we don't want to unnest anything here */
3267 vm_map_clip_start(map
, entry
, start
);
3268 vm_map_clip_end(map
, entry
, end
);
3270 if ((entry
->vme_start
== start
) && (entry
->vme_end
== end
) &&
3271 (!entry
->is_sub_map
) &&
3272 ((object
= entry
->object
.vm_object
) == vm_submap_object
) &&
3273 (object
->resident_page_count
== 0) &&
3274 (object
->copy
== VM_OBJECT_NULL
) &&
3275 (object
->shadow
== VM_OBJECT_NULL
) &&
3276 (!object
->pager_created
)) {
3277 entry
->offset
= (vm_object_offset_t
)offset
;
3278 entry
->object
.vm_object
= VM_OBJECT_NULL
;
3279 vm_object_deallocate(object
);
3280 entry
->is_sub_map
= TRUE
;
3281 entry
->object
.sub_map
= submap
;
3282 vm_map_reference(submap
);
3283 submap
->mapped
= TRUE
;
3285 #ifndef NO_NESTED_PMAP
3287 /* nest if platform code will allow */
3288 if(submap
->pmap
== NULL
) {
3289 submap
->pmap
= pmap_create((vm_map_size_t
) 0, FALSE
);
3290 if(submap
->pmap
== PMAP_NULL
) {
3292 return(KERN_NO_SPACE
);
3295 result
= pmap_nest(map
->pmap
,
3296 (entry
->object
.sub_map
)->pmap
,
3299 (uint64_t)(end
- start
));
3301 panic("vm_map_submap: pmap_nest failed, rc = %08X\n", result
);
3302 entry
->use_pmap
= TRUE
;
3304 #else /* NO_NESTED_PMAP */
3305 pmap_remove(map
->pmap
, (addr64_t
)start
, (addr64_t
)end
);
3306 #endif /* NO_NESTED_PMAP */
3307 result
= KERN_SUCCESS
;
3317 * Sets the protection of the specified address
3318 * region in the target map. If "set_max" is
3319 * specified, the maximum protection is to be set;
3320 * otherwise, only the current protection is affected.
3324 register vm_map_t map
,
3325 register vm_map_offset_t start
,
3326 register vm_map_offset_t end
,
3327 register vm_prot_t new_prot
,
3328 register boolean_t set_max
)
3330 register vm_map_entry_t current
;
3331 register vm_map_offset_t prev
;
3332 vm_map_entry_t entry
;
3336 "vm_map_protect, 0x%X start 0x%X end 0x%X, new 0x%X %d",
3337 map
, start
, end
, new_prot
, set_max
);
3341 /* LP64todo - remove this check when vm_map_commpage64()
3342 * no longer has to stuff in a map_entry for the commpage
3343 * above the map's max_offset.
3345 if (start
>= map
->max_offset
) {
3347 return(KERN_INVALID_ADDRESS
);
3352 * Lookup the entry. If it doesn't start in a valid
3353 * entry, return an error.
3355 if (! vm_map_lookup_entry(map
, start
, &entry
)) {
3357 return(KERN_INVALID_ADDRESS
);
3360 if (entry
->superpage_size
&& (start
& (SUPERPAGE_SIZE
-1))) { /* extend request to whole entry */
3361 start
= SUPERPAGE_ROUND_DOWN(start
);
3366 if (entry
->superpage_size
)
3367 end
= SUPERPAGE_ROUND_UP(end
);
3370 * Make a first pass to check for protection and address
3375 prev
= current
->vme_start
;
3376 while ((current
!= vm_map_to_entry(map
)) &&
3377 (current
->vme_start
< end
)) {
3380 * If there is a hole, return an error.
3382 if (current
->vme_start
!= prev
) {
3384 return(KERN_INVALID_ADDRESS
);
3387 new_max
= current
->max_protection
;
3388 if(new_prot
& VM_PROT_COPY
) {
3389 new_max
|= VM_PROT_WRITE
;
3390 if ((new_prot
& (new_max
| VM_PROT_COPY
)) != new_prot
) {
3392 return(KERN_PROTECTION_FAILURE
);
3395 if ((new_prot
& new_max
) != new_prot
) {
3397 return(KERN_PROTECTION_FAILURE
);
3402 if (new_prot
& VM_PROT_WRITE
) {
3403 if ((new_prot
& VM_PROT_EXECUTE
) && !(current
->used_for_jit
)) {
3404 printf("EMBEDDED: %s can't have both write and exec at the same time\n", __FUNCTION__
);
3405 new_prot
&= ~VM_PROT_EXECUTE
;
3410 prev
= current
->vme_end
;
3411 current
= current
->vme_next
;
3415 return(KERN_INVALID_ADDRESS
);
3419 * Go back and fix up protections.
3420 * Clip to start here if the range starts within
3425 if (current
!= vm_map_to_entry(map
)) {
3426 /* clip and unnest if necessary */
3427 vm_map_clip_start(map
, current
, start
);
3430 while ((current
!= vm_map_to_entry(map
)) &&
3431 (current
->vme_start
< end
)) {
3435 vm_map_clip_end(map
, current
, end
);
3437 assert(!current
->use_pmap
); /* clipping did unnest if needed */
3439 old_prot
= current
->protection
;
3441 if(new_prot
& VM_PROT_COPY
) {
3442 /* caller is asking specifically to copy the */
3443 /* mapped data, this implies that max protection */
3444 /* will include write. Caller must be prepared */
3445 /* for loss of shared memory communication in the */
3446 /* target area after taking this step */
3448 if (current
->is_sub_map
== FALSE
&& current
->object
.vm_object
== VM_OBJECT_NULL
){
3449 current
->object
.vm_object
= vm_object_allocate((vm_map_size_t
)(current
->vme_end
- current
->vme_start
));
3450 current
->offset
= 0;
3452 current
->needs_copy
= TRUE
;
3453 current
->max_protection
|= VM_PROT_WRITE
;
3457 current
->protection
=
3458 (current
->max_protection
=
3459 new_prot
& ~VM_PROT_COPY
) &
3462 current
->protection
= new_prot
& ~VM_PROT_COPY
;
3465 * Update physical map if necessary.
3466 * If the request is to turn off write protection,
3467 * we won't do it for real (in pmap). This is because
3468 * it would cause copy-on-write to fail. We've already
3469 * set, the new protection in the map, so if a
3470 * write-protect fault occurred, it will be fixed up
3471 * properly, COW or not.
3473 if (current
->protection
!= old_prot
) {
3474 /* Look one level in we support nested pmaps */
3475 /* from mapped submaps which are direct entries */
3480 prot
= current
->protection
& ~VM_PROT_WRITE
;
3482 if (override_nx(map
, current
->alias
) && prot
)
3483 prot
|= VM_PROT_EXECUTE
;
3485 if (current
->is_sub_map
&& current
->use_pmap
) {
3486 pmap_protect(current
->object
.sub_map
->pmap
,
3491 pmap_protect(map
->pmap
,
3497 current
= current
->vme_next
;
3501 while ((current
!= vm_map_to_entry(map
)) &&
3502 (current
->vme_start
<= end
)) {
3503 vm_map_simplify_entry(map
, current
);
3504 current
= current
->vme_next
;
3508 return(KERN_SUCCESS
);
3514 * Sets the inheritance of the specified address
3515 * range in the target map. Inheritance
3516 * affects how the map will be shared with
3517 * child maps at the time of vm_map_fork.
3521 register vm_map_t map
,
3522 register vm_map_offset_t start
,
3523 register vm_map_offset_t end
,
3524 register vm_inherit_t new_inheritance
)
3526 register vm_map_entry_t entry
;
3527 vm_map_entry_t temp_entry
;
3531 VM_MAP_RANGE_CHECK(map
, start
, end
);
3533 if (vm_map_lookup_entry(map
, start
, &temp_entry
)) {
3537 temp_entry
= temp_entry
->vme_next
;
3541 /* first check entire range for submaps which can't support the */
3542 /* given inheritance. */
3543 while ((entry
!= vm_map_to_entry(map
)) && (entry
->vme_start
< end
)) {
3544 if(entry
->is_sub_map
) {
3545 if(new_inheritance
== VM_INHERIT_COPY
) {
3547 return(KERN_INVALID_ARGUMENT
);
3551 entry
= entry
->vme_next
;
3555 if (entry
!= vm_map_to_entry(map
)) {
3556 /* clip and unnest if necessary */
3557 vm_map_clip_start(map
, entry
, start
);
3560 while ((entry
!= vm_map_to_entry(map
)) && (entry
->vme_start
< end
)) {
3561 vm_map_clip_end(map
, entry
, end
);
3562 assert(!entry
->use_pmap
); /* clip did unnest if needed */
3564 entry
->inheritance
= new_inheritance
;
3566 entry
= entry
->vme_next
;
3570 return(KERN_SUCCESS
);
3574 * Update the accounting for the amount of wired memory in this map. If the user has
3575 * exceeded the defined limits, then we fail. Wiring on behalf of the kernel never fails.
3578 static kern_return_t
3581 vm_map_entry_t entry
,
3582 boolean_t user_wire
)
3587 unsigned int total_wire_count
= vm_page_wire_count
+ vm_lopage_free_count
;
3590 * We're wiring memory at the request of the user. Check if this is the first time the user is wiring
3594 if (entry
->user_wired_count
== 0) {
3595 size
= entry
->vme_end
- entry
->vme_start
;
3598 * Since this is the first time the user is wiring this map entry, check to see if we're
3599 * exceeding the user wire limits. There is a per map limit which is the smaller of either
3600 * the process's rlimit or the global vm_user_wire_limit which caps this value. There is also
3601 * a system-wide limit on the amount of memory all users can wire. If the user is over either
3602 * limit, then we fail.
3605 if(size
+ map
->user_wire_size
> MIN(map
->user_wire_limit
, vm_user_wire_limit
) ||
3606 size
+ ptoa_64(total_wire_count
) > vm_global_user_wire_limit
||
3607 size
+ ptoa_64(total_wire_count
) > max_mem
- vm_global_no_user_wire_amount
)
3608 return KERN_RESOURCE_SHORTAGE
;
3611 * The first time the user wires an entry, we also increment the wired_count and add this to
3612 * the total that has been wired in the map.
3615 if (entry
->wired_count
>= MAX_WIRE_COUNT
)
3616 return KERN_FAILURE
;
3618 entry
->wired_count
++;
3619 map
->user_wire_size
+= size
;
3622 if (entry
->user_wired_count
>= MAX_WIRE_COUNT
)
3623 return KERN_FAILURE
;
3625 entry
->user_wired_count
++;
3630 * The kernel's wiring the memory. Just bump the count and continue.
3633 if (entry
->wired_count
>= MAX_WIRE_COUNT
)
3634 panic("vm_map_wire: too many wirings");
3636 entry
->wired_count
++;
3639 return KERN_SUCCESS
;
3643 * Update the memory wiring accounting now that the given map entry is being unwired.
3647 subtract_wire_counts(
3649 vm_map_entry_t entry
,
3650 boolean_t user_wire
)
3656 * We're unwiring memory at the request of the user. See if we're removing the last user wire reference.
3659 if (entry
->user_wired_count
== 1) {
3662 * We're removing the last user wire reference. Decrement the wired_count and the total
3663 * user wired memory for this map.
3666 assert(entry
->wired_count
>= 1);
3667 entry
->wired_count
--;
3668 map
->user_wire_size
-= entry
->vme_end
- entry
->vme_start
;
3671 assert(entry
->user_wired_count
>= 1);
3672 entry
->user_wired_count
--;
3677 * The kernel is unwiring the memory. Just update the count.
3680 assert(entry
->wired_count
>= 1);
3681 entry
->wired_count
--;
3688 * Sets the pageability of the specified address range in the
3689 * target map as wired. Regions specified as not pageable require
3690 * locked-down physical memory and physical page maps. The
3691 * access_type variable indicates types of accesses that must not
3692 * generate page faults. This is checked against protection of
3693 * memory being locked-down.
3695 * The map must not be locked, but a reference must remain to the
3696 * map throughout the call.
3698 static kern_return_t
3700 register vm_map_t map
,
3701 register vm_map_offset_t start
,
3702 register vm_map_offset_t end
,
3703 register vm_prot_t access_type
,
3704 boolean_t user_wire
,
3706 vm_map_offset_t pmap_addr
)
3708 register vm_map_entry_t entry
;
3709 struct vm_map_entry
*first_entry
, tmp_entry
;
3711 register vm_map_offset_t s
,e
;
3713 boolean_t need_wakeup
;
3714 boolean_t main_map
= FALSE
;
3715 wait_interrupt_t interruptible_state
;
3716 thread_t cur_thread
;
3717 unsigned int last_timestamp
;
3721 if(map_pmap
== NULL
)
3723 last_timestamp
= map
->timestamp
;
3725 VM_MAP_RANGE_CHECK(map
, start
, end
);
3726 assert(page_aligned(start
));
3727 assert(page_aligned(end
));
3729 /* We wired what the caller asked for, zero pages */
3731 return KERN_SUCCESS
;
3734 need_wakeup
= FALSE
;
3735 cur_thread
= current_thread();
3740 if (vm_map_lookup_entry(map
, s
, &first_entry
)) {
3741 entry
= first_entry
;
3743 * vm_map_clip_start will be done later.
3744 * We don't want to unnest any nested submaps here !
3747 /* Start address is not in map */
3748 rc
= KERN_INVALID_ADDRESS
;
3752 while ((entry
!= vm_map_to_entry(map
)) && (s
< end
)) {
3754 * At this point, we have wired from "start" to "s".
3755 * We still need to wire from "s" to "end".
3757 * "entry" hasn't been clipped, so it could start before "s"
3758 * and/or end after "end".
3761 /* "e" is how far we want to wire in this entry */
3767 * If another thread is wiring/unwiring this entry then
3768 * block after informing other thread to wake us up.
3770 if (entry
->in_transition
) {
3771 wait_result_t wait_result
;
3774 * We have not clipped the entry. Make sure that
3775 * the start address is in range so that the lookup
3776 * below will succeed.
3777 * "s" is the current starting point: we've already
3778 * wired from "start" to "s" and we still have
3779 * to wire from "s" to "end".
3782 entry
->needs_wakeup
= TRUE
;
3785 * wake up anybody waiting on entries that we have
3789 vm_map_entry_wakeup(map
);
3790 need_wakeup
= FALSE
;
3793 * User wiring is interruptible
3795 wait_result
= vm_map_entry_wait(map
,
3796 (user_wire
) ? THREAD_ABORTSAFE
:
3798 if (user_wire
&& wait_result
== THREAD_INTERRUPTED
) {
3800 * undo the wirings we have done so far
3801 * We do not clear the needs_wakeup flag,
3802 * because we cannot tell if we were the
3810 * Cannot avoid a lookup here. reset timestamp.
3812 last_timestamp
= map
->timestamp
;
3815 * The entry could have been clipped, look it up again.
3816 * Worse that can happen is, it may not exist anymore.
3818 if (!vm_map_lookup_entry(map
, s
, &first_entry
)) {
3820 * User: undo everything upto the previous
3821 * entry. let vm_map_unwire worry about
3822 * checking the validity of the range.
3827 entry
= first_entry
;
3831 if (entry
->is_sub_map
) {
3832 vm_map_offset_t sub_start
;
3833 vm_map_offset_t sub_end
;
3834 vm_map_offset_t local_start
;
3835 vm_map_offset_t local_end
;
3838 vm_map_clip_start(map
, entry
, s
);
3839 vm_map_clip_end(map
, entry
, end
);
3841 sub_start
= entry
->offset
;
3842 sub_end
= entry
->vme_end
;
3843 sub_end
+= entry
->offset
- entry
->vme_start
;
3845 local_end
= entry
->vme_end
;
3846 if(map_pmap
== NULL
) {
3848 vm_object_offset_t offset
;
3851 vm_map_entry_t local_entry
;
3852 vm_map_version_t version
;
3853 vm_map_t lookup_map
;
3855 if(entry
->use_pmap
) {
3856 pmap
= entry
->object
.sub_map
->pmap
;
3857 /* ppc implementation requires that */
3858 /* submaps pmap address ranges line */
3859 /* up with parent map */
3861 pmap_addr
= sub_start
;
3869 if (entry
->wired_count
) {
3870 if ((rc
= add_wire_counts(map
, entry
, user_wire
)) != KERN_SUCCESS
)
3874 * The map was not unlocked:
3875 * no need to goto re-lookup.
3876 * Just go directly to next entry.
3878 entry
= entry
->vme_next
;
3879 s
= entry
->vme_start
;
3884 /* call vm_map_lookup_locked to */
3885 /* cause any needs copy to be */
3887 local_start
= entry
->vme_start
;
3889 vm_map_lock_write_to_read(map
);
3890 if(vm_map_lookup_locked(
3891 &lookup_map
, local_start
,
3893 OBJECT_LOCK_EXCLUSIVE
,
3895 &offset
, &prot
, &wired
,
3899 vm_map_unlock_read(lookup_map
);
3900 vm_map_unwire(map
, start
,
3902 return(KERN_FAILURE
);
3904 if(real_map
!= lookup_map
)
3905 vm_map_unlock(real_map
);
3906 vm_map_unlock_read(lookup_map
);
3908 vm_object_unlock(object
);
3910 /* we unlocked, so must re-lookup */
3911 if (!vm_map_lookup_entry(map
,
3919 * entry could have been "simplified",
3922 entry
= local_entry
;
3923 assert(s
== local_start
);
3924 vm_map_clip_start(map
, entry
, s
);
3925 vm_map_clip_end(map
, entry
, end
);
3926 /* re-compute "e" */
3931 /* did we have a change of type? */
3932 if (!entry
->is_sub_map
) {
3933 last_timestamp
= map
->timestamp
;
3937 local_start
= entry
->vme_start
;
3941 if ((rc
= add_wire_counts(map
, entry
, user_wire
)) != KERN_SUCCESS
)
3944 entry
->in_transition
= TRUE
;
3947 rc
= vm_map_wire_nested(entry
->object
.sub_map
,
3950 user_wire
, pmap
, pmap_addr
);
3954 * Find the entry again. It could have been clipped
3955 * after we unlocked the map.
3957 if (!vm_map_lookup_entry(map
, local_start
,
3959 panic("vm_map_wire: re-lookup failed");
3960 entry
= first_entry
;
3962 assert(local_start
== s
);
3963 /* re-compute "e" */
3968 last_timestamp
= map
->timestamp
;
3969 while ((entry
!= vm_map_to_entry(map
)) &&
3970 (entry
->vme_start
< e
)) {
3971 assert(entry
->in_transition
);
3972 entry
->in_transition
= FALSE
;
3973 if (entry
->needs_wakeup
) {
3974 entry
->needs_wakeup
= FALSE
;
3977 if (rc
!= KERN_SUCCESS
) {/* from vm_*_wire */
3978 subtract_wire_counts(map
, entry
, user_wire
);
3980 entry
= entry
->vme_next
;
3982 if (rc
!= KERN_SUCCESS
) { /* from vm_*_wire */
3986 /* no need to relookup again */
3987 s
= entry
->vme_start
;
3992 * If this entry is already wired then increment
3993 * the appropriate wire reference count.
3995 if (entry
->wired_count
) {
3997 * entry is already wired down, get our reference
3998 * after clipping to our range.
4000 vm_map_clip_start(map
, entry
, s
);
4001 vm_map_clip_end(map
, entry
, end
);
4003 if ((rc
= add_wire_counts(map
, entry
, user_wire
)) != KERN_SUCCESS
)
4006 /* map was not unlocked: no need to relookup */
4007 entry
= entry
->vme_next
;
4008 s
= entry
->vme_start
;
4013 * Unwired entry or wire request transmitted via submap
4018 * Perform actions of vm_map_lookup that need the write
4019 * lock on the map: create a shadow object for a
4020 * copy-on-write region, or an object for a zero-fill
4023 size
= entry
->vme_end
- entry
->vme_start
;
4025 * If wiring a copy-on-write page, we need to copy it now
4026 * even if we're only (currently) requesting read access.
4027 * This is aggressive, but once it's wired we can't move it.
4029 if (entry
->needs_copy
) {
4030 vm_object_shadow(&entry
->object
.vm_object
,
4031 &entry
->offset
, size
);
4032 entry
->needs_copy
= FALSE
;
4033 } else if (entry
->object
.vm_object
== VM_OBJECT_NULL
) {
4034 entry
->object
.vm_object
= vm_object_allocate(size
);
4035 entry
->offset
= (vm_object_offset_t
)0;
4038 vm_map_clip_start(map
, entry
, s
);
4039 vm_map_clip_end(map
, entry
, end
);
4041 /* re-compute "e" */
4047 * Check for holes and protection mismatch.
4048 * Holes: Next entry should be contiguous unless this
4049 * is the end of the region.
4050 * Protection: Access requested must be allowed, unless
4051 * wiring is by protection class
4053 if ((entry
->vme_end
< end
) &&
4054 ((entry
->vme_next
== vm_map_to_entry(map
)) ||
4055 (entry
->vme_next
->vme_start
> entry
->vme_end
))) {
4057 rc
= KERN_INVALID_ADDRESS
;
4060 if ((entry
->protection
& access_type
) != access_type
) {
4061 /* found a protection problem */
4062 rc
= KERN_PROTECTION_FAILURE
;
4066 assert(entry
->wired_count
== 0 && entry
->user_wired_count
== 0);
4068 if ((rc
= add_wire_counts(map
, entry
, user_wire
)) != KERN_SUCCESS
)
4071 entry
->in_transition
= TRUE
;
4074 * This entry might get split once we unlock the map.
4075 * In vm_fault_wire(), we need the current range as
4076 * defined by this entry. In order for this to work
4077 * along with a simultaneous clip operation, we make a
4078 * temporary copy of this entry and use that for the
4079 * wiring. Note that the underlying objects do not
4080 * change during a clip.
4085 * The in_transition state guarentees that the entry
4086 * (or entries for this range, if split occured) will be
4087 * there when the map lock is acquired for the second time.
4091 if (!user_wire
&& cur_thread
!= THREAD_NULL
)
4092 interruptible_state
= thread_interrupt_level(THREAD_UNINT
);
4094 interruptible_state
= THREAD_UNINT
;
4097 rc
= vm_fault_wire(map
,
4098 &tmp_entry
, map_pmap
, pmap_addr
);
4100 rc
= vm_fault_wire(map
,
4101 &tmp_entry
, map
->pmap
,
4102 tmp_entry
.vme_start
);
4104 if (!user_wire
&& cur_thread
!= THREAD_NULL
)
4105 thread_interrupt_level(interruptible_state
);
4109 if (last_timestamp
+1 != map
->timestamp
) {
4111 * Find the entry again. It could have been clipped
4112 * after we unlocked the map.
4114 if (!vm_map_lookup_entry(map
, tmp_entry
.vme_start
,
4116 panic("vm_map_wire: re-lookup failed");
4118 entry
= first_entry
;
4121 last_timestamp
= map
->timestamp
;
4123 while ((entry
!= vm_map_to_entry(map
)) &&
4124 (entry
->vme_start
< tmp_entry
.vme_end
)) {
4125 assert(entry
->in_transition
);
4126 entry
->in_transition
= FALSE
;
4127 if (entry
->needs_wakeup
) {
4128 entry
->needs_wakeup
= FALSE
;
4131 if (rc
!= KERN_SUCCESS
) { /* from vm_*_wire */
4132 subtract_wire_counts(map
, entry
, user_wire
);
4134 entry
= entry
->vme_next
;
4137 if (rc
!= KERN_SUCCESS
) { /* from vm_*_wire */
4141 s
= entry
->vme_start
;
4142 } /* end while loop through map entries */
4145 if (rc
== KERN_SUCCESS
) {
4146 /* repair any damage we may have made to the VM map */
4147 vm_map_simplify_range(map
, start
, end
);
4153 * wake up anybody waiting on entries we wired.
4156 vm_map_entry_wakeup(map
);
4158 if (rc
!= KERN_SUCCESS
) {
4159 /* undo what has been wired so far */
4160 vm_map_unwire(map
, start
, s
, user_wire
);
4169 register vm_map_t map
,
4170 register vm_map_offset_t start
,
4171 register vm_map_offset_t end
,
4172 register vm_prot_t access_type
,
4173 boolean_t user_wire
)
4178 kret
= vm_map_wire_nested(map
, start
, end
, access_type
,
4179 user_wire
, (pmap_t
)NULL
, 0);
4186 * Sets the pageability of the specified address range in the target
4187 * as pageable. Regions specified must have been wired previously.
4189 * The map must not be locked, but a reference must remain to the map
4190 * throughout the call.
4192 * Kernel will panic on failures. User unwire ignores holes and
4193 * unwired and intransition entries to avoid losing memory by leaving
4196 static kern_return_t
4197 vm_map_unwire_nested(
4198 register vm_map_t map
,
4199 register vm_map_offset_t start
,
4200 register vm_map_offset_t end
,
4201 boolean_t user_wire
,
4203 vm_map_offset_t pmap_addr
)
4205 register vm_map_entry_t entry
;
4206 struct vm_map_entry
*first_entry
, tmp_entry
;
4207 boolean_t need_wakeup
;
4208 boolean_t main_map
= FALSE
;
4209 unsigned int last_timestamp
;
4212 if(map_pmap
== NULL
)
4214 last_timestamp
= map
->timestamp
;
4216 VM_MAP_RANGE_CHECK(map
, start
, end
);
4217 assert(page_aligned(start
));
4218 assert(page_aligned(end
));
4221 /* We unwired what the caller asked for: zero pages */
4223 return KERN_SUCCESS
;
4226 if (vm_map_lookup_entry(map
, start
, &first_entry
)) {
4227 entry
= first_entry
;
4229 * vm_map_clip_start will be done later.
4230 * We don't want to unnest any nested sub maps here !
4235 panic("vm_map_unwire: start not found");
4237 /* Start address is not in map. */
4239 return(KERN_INVALID_ADDRESS
);
4242 if (entry
->superpage_size
) {
4243 /* superpages are always wired */
4245 return KERN_INVALID_ADDRESS
;
4248 need_wakeup
= FALSE
;
4249 while ((entry
!= vm_map_to_entry(map
)) && (entry
->vme_start
< end
)) {
4250 if (entry
->in_transition
) {
4253 * Another thread is wiring down this entry. Note
4254 * that if it is not for the other thread we would
4255 * be unwiring an unwired entry. This is not
4256 * permitted. If we wait, we will be unwiring memory
4260 * Another thread is unwiring this entry. We did not
4261 * have a reference to it, because if we did, this
4262 * entry will not be getting unwired now.
4267 * This could happen: there could be some
4268 * overlapping vslock/vsunlock operations
4270 * We should probably just wait and retry,
4271 * but then we have to be careful that this
4272 * entry could get "simplified" after
4273 * "in_transition" gets unset and before
4274 * we re-lookup the entry, so we would
4275 * have to re-clip the entry to avoid
4276 * re-unwiring what we have already unwired...
4277 * See vm_map_wire_nested().
4279 * Or we could just ignore "in_transition"
4280 * here and proceed to decement the wired
4281 * count(s) on this entry. That should be fine
4282 * as long as "wired_count" doesn't drop all
4283 * the way to 0 (and we should panic if THAT
4286 panic("vm_map_unwire: in_transition entry");
4289 entry
= entry
->vme_next
;
4293 if (entry
->is_sub_map
) {
4294 vm_map_offset_t sub_start
;
4295 vm_map_offset_t sub_end
;
4296 vm_map_offset_t local_end
;
4299 vm_map_clip_start(map
, entry
, start
);
4300 vm_map_clip_end(map
, entry
, end
);
4302 sub_start
= entry
->offset
;
4303 sub_end
= entry
->vme_end
- entry
->vme_start
;
4304 sub_end
+= entry
->offset
;
4305 local_end
= entry
->vme_end
;
4306 if(map_pmap
== NULL
) {
4307 if(entry
->use_pmap
) {
4308 pmap
= entry
->object
.sub_map
->pmap
;
4309 pmap_addr
= sub_start
;
4314 if (entry
->wired_count
== 0 ||
4315 (user_wire
&& entry
->user_wired_count
== 0)) {
4317 panic("vm_map_unwire: entry is unwired");
4318 entry
= entry
->vme_next
;
4324 * Holes: Next entry should be contiguous unless
4325 * this is the end of the region.
4327 if (((entry
->vme_end
< end
) &&
4328 ((entry
->vme_next
== vm_map_to_entry(map
)) ||
4329 (entry
->vme_next
->vme_start
4330 > entry
->vme_end
)))) {
4332 panic("vm_map_unwire: non-contiguous region");
4334 entry = entry->vme_next;
4339 subtract_wire_counts(map
, entry
, user_wire
);
4341 if (entry
->wired_count
!= 0) {
4342 entry
= entry
->vme_next
;
4346 entry
->in_transition
= TRUE
;
4347 tmp_entry
= *entry
;/* see comment in vm_map_wire() */
4350 * We can unlock the map now. The in_transition state
4351 * guarantees existance of the entry.
4354 vm_map_unwire_nested(entry
->object
.sub_map
,
4355 sub_start
, sub_end
, user_wire
, pmap
, pmap_addr
);
4358 if (last_timestamp
+1 != map
->timestamp
) {
4360 * Find the entry again. It could have been
4361 * clipped or deleted after we unlocked the map.
4363 if (!vm_map_lookup_entry(map
,
4364 tmp_entry
.vme_start
,
4367 panic("vm_map_unwire: re-lookup failed");
4368 entry
= first_entry
->vme_next
;
4370 entry
= first_entry
;
4372 last_timestamp
= map
->timestamp
;
4375 * clear transition bit for all constituent entries
4376 * that were in the original entry (saved in
4377 * tmp_entry). Also check for waiters.
4379 while ((entry
!= vm_map_to_entry(map
)) &&
4380 (entry
->vme_start
< tmp_entry
.vme_end
)) {
4381 assert(entry
->in_transition
);
4382 entry
->in_transition
= FALSE
;
4383 if (entry
->needs_wakeup
) {
4384 entry
->needs_wakeup
= FALSE
;
4387 entry
= entry
->vme_next
;
4392 vm_map_unwire_nested(entry
->object
.sub_map
,
4393 sub_start
, sub_end
, user_wire
, map_pmap
,
4397 if (last_timestamp
+1 != map
->timestamp
) {
4399 * Find the entry again. It could have been
4400 * clipped or deleted after we unlocked the map.
4402 if (!vm_map_lookup_entry(map
,
4403 tmp_entry
.vme_start
,
4406 panic("vm_map_unwire: re-lookup failed");
4407 entry
= first_entry
->vme_next
;
4409 entry
= first_entry
;
4411 last_timestamp
= map
->timestamp
;
4416 if ((entry
->wired_count
== 0) ||
4417 (user_wire
&& entry
->user_wired_count
== 0)) {
4419 panic("vm_map_unwire: entry is unwired");
4421 entry
= entry
->vme_next
;
4425 assert(entry
->wired_count
> 0 &&
4426 (!user_wire
|| entry
->user_wired_count
> 0));
4428 vm_map_clip_start(map
, entry
, start
);
4429 vm_map_clip_end(map
, entry
, end
);
4433 * Holes: Next entry should be contiguous unless
4434 * this is the end of the region.
4436 if (((entry
->vme_end
< end
) &&
4437 ((entry
->vme_next
== vm_map_to_entry(map
)) ||
4438 (entry
->vme_next
->vme_start
> entry
->vme_end
)))) {
4441 panic("vm_map_unwire: non-contiguous region");
4442 entry
= entry
->vme_next
;
4446 subtract_wire_counts(map
, entry
, user_wire
);
4448 if (entry
->wired_count
!= 0) {
4449 entry
= entry
->vme_next
;
4453 if(entry
->zero_wired_pages
) {
4454 entry
->zero_wired_pages
= FALSE
;
4457 entry
->in_transition
= TRUE
;
4458 tmp_entry
= *entry
; /* see comment in vm_map_wire() */
4461 * We can unlock the map now. The in_transition state
4462 * guarantees existance of the entry.
4466 vm_fault_unwire(map
,
4467 &tmp_entry
, FALSE
, map_pmap
, pmap_addr
);
4469 vm_fault_unwire(map
,
4470 &tmp_entry
, FALSE
, map
->pmap
,
4471 tmp_entry
.vme_start
);
4475 if (last_timestamp
+1 != map
->timestamp
) {
4477 * Find the entry again. It could have been clipped
4478 * or deleted after we unlocked the map.
4480 if (!vm_map_lookup_entry(map
, tmp_entry
.vme_start
,
4483 panic("vm_map_unwire: re-lookup failed");
4484 entry
= first_entry
->vme_next
;
4486 entry
= first_entry
;
4488 last_timestamp
= map
->timestamp
;
4491 * clear transition bit for all constituent entries that
4492 * were in the original entry (saved in tmp_entry). Also
4493 * check for waiters.
4495 while ((entry
!= vm_map_to_entry(map
)) &&
4496 (entry
->vme_start
< tmp_entry
.vme_end
)) {
4497 assert(entry
->in_transition
);
4498 entry
->in_transition
= FALSE
;
4499 if (entry
->needs_wakeup
) {
4500 entry
->needs_wakeup
= FALSE
;
4503 entry
= entry
->vme_next
;
4508 * We might have fragmented the address space when we wired this
4509 * range of addresses. Attempt to re-coalesce these VM map entries
4510 * with their neighbors now that they're no longer wired.
4511 * Under some circumstances, address space fragmentation can
4512 * prevent VM object shadow chain collapsing, which can cause
4515 vm_map_simplify_range(map
, start
, end
);
4519 * wake up anybody waiting on entries that we have unwired.
4522 vm_map_entry_wakeup(map
);
4523 return(KERN_SUCCESS
);
4529 register vm_map_t map
,
4530 register vm_map_offset_t start
,
4531 register vm_map_offset_t end
,
4532 boolean_t user_wire
)
4534 return vm_map_unwire_nested(map
, start
, end
,
4535 user_wire
, (pmap_t
)NULL
, 0);
4540 * vm_map_entry_delete: [ internal use only ]
4542 * Deallocate the given entry from the target map.
4545 vm_map_entry_delete(
4546 register vm_map_t map
,
4547 register vm_map_entry_t entry
)
4549 register vm_map_offset_t s
, e
;
4550 register vm_object_t object
;
4551 register vm_map_t submap
;
4553 s
= entry
->vme_start
;
4555 assert(page_aligned(s
));
4556 assert(page_aligned(e
));
4557 assert(entry
->wired_count
== 0);
4558 assert(entry
->user_wired_count
== 0);
4559 assert(!entry
->permanent
);
4561 if (entry
->is_sub_map
) {
4563 submap
= entry
->object
.sub_map
;
4566 object
= entry
->object
.vm_object
;
4569 vm_map_store_entry_unlink(map
, entry
);
4572 vm_map_entry_dispose(map
, entry
);
4576 * Deallocate the object only after removing all
4577 * pmap entries pointing to its pages.
4580 vm_map_deallocate(submap
);
4582 vm_object_deallocate(object
);
4587 vm_map_submap_pmap_clean(
4589 vm_map_offset_t start
,
4590 vm_map_offset_t end
,
4592 vm_map_offset_t offset
)
4594 vm_map_offset_t submap_start
;
4595 vm_map_offset_t submap_end
;
4596 vm_map_size_t remove_size
;
4597 vm_map_entry_t entry
;
4599 submap_end
= offset
+ (end
- start
);
4600 submap_start
= offset
;
4602 vm_map_lock_read(sub_map
);
4603 if(vm_map_lookup_entry(sub_map
, offset
, &entry
)) {
4605 remove_size
= (entry
->vme_end
- entry
->vme_start
);
4606 if(offset
> entry
->vme_start
)
4607 remove_size
-= offset
- entry
->vme_start
;
4610 if(submap_end
< entry
->vme_end
) {
4612 entry
->vme_end
- submap_end
;
4614 if(entry
->is_sub_map
) {
4615 vm_map_submap_pmap_clean(
4618 start
+ remove_size
,
4619 entry
->object
.sub_map
,
4623 if((map
->mapped
) && (map
->ref_count
)
4624 && (entry
->object
.vm_object
!= NULL
)) {
4625 vm_object_pmap_protect(
4626 entry
->object
.vm_object
,
4627 entry
->offset
+(offset
-entry
->vme_start
),
4633 pmap_remove(map
->pmap
,
4635 (addr64_t
)(start
+ remove_size
));
4640 entry
= entry
->vme_next
;
4642 while((entry
!= vm_map_to_entry(sub_map
))
4643 && (entry
->vme_start
< submap_end
)) {
4644 remove_size
= (entry
->vme_end
- entry
->vme_start
);
4645 if(submap_end
< entry
->vme_end
) {
4646 remove_size
-= entry
->vme_end
- submap_end
;
4648 if(entry
->is_sub_map
) {
4649 vm_map_submap_pmap_clean(
4651 (start
+ entry
->vme_start
) - offset
,
4652 ((start
+ entry
->vme_start
) - offset
) + remove_size
,
4653 entry
->object
.sub_map
,
4656 if((map
->mapped
) && (map
->ref_count
)
4657 && (entry
->object
.vm_object
!= NULL
)) {
4658 vm_object_pmap_protect(
4659 entry
->object
.vm_object
,
4666 pmap_remove(map
->pmap
,
4667 (addr64_t
)((start
+ entry
->vme_start
)
4669 (addr64_t
)(((start
+ entry
->vme_start
)
4670 - offset
) + remove_size
));
4673 entry
= entry
->vme_next
;
4675 vm_map_unlock_read(sub_map
);
4680 * vm_map_delete: [ internal use only ]
4682 * Deallocates the given address range from the target map.
4683 * Removes all user wirings. Unwires one kernel wiring if
4684 * VM_MAP_REMOVE_KUNWIRE is set. Waits for kernel wirings to go
4685 * away if VM_MAP_REMOVE_WAIT_FOR_KWIRE is set. Sleeps
4686 * interruptibly if VM_MAP_REMOVE_INTERRUPTIBLE is set.
4688 * This routine is called with map locked and leaves map locked.
4690 static kern_return_t
4693 vm_map_offset_t start
,
4694 vm_map_offset_t end
,
4698 vm_map_entry_t entry
, next
;
4699 struct vm_map_entry
*first_entry
, tmp_entry
;
4700 register vm_map_offset_t s
;
4701 register vm_object_t object
;
4702 boolean_t need_wakeup
;
4703 unsigned int last_timestamp
= ~0; /* unlikely value */
4706 interruptible
= (flags
& VM_MAP_REMOVE_INTERRUPTIBLE
) ?
4707 THREAD_ABORTSAFE
: THREAD_UNINT
;
4710 * All our DMA I/O operations in IOKit are currently done by
4711 * wiring through the map entries of the task requesting the I/O.
4712 * Because of this, we must always wait for kernel wirings
4713 * to go away on the entries before deleting them.
4715 * Any caller who wants to actually remove a kernel wiring
4716 * should explicitly set the VM_MAP_REMOVE_KUNWIRE flag to
4717 * properly remove one wiring instead of blasting through
4720 flags
|= VM_MAP_REMOVE_WAIT_FOR_KWIRE
;
4724 * Find the start of the region, and clip it
4726 if (vm_map_lookup_entry(map
, start
, &first_entry
)) {
4727 entry
= first_entry
;
4728 if (entry
->superpage_size
&& (start
& ~SUPERPAGE_MASK
)) { /* extend request to whole entry */ start
= SUPERPAGE_ROUND_DOWN(start
);
4729 start
= SUPERPAGE_ROUND_DOWN(start
);
4732 if (start
== entry
->vme_start
) {
4734 * No need to clip. We don't want to cause
4735 * any unnecessary unnesting in this case...
4738 vm_map_clip_start(map
, entry
, start
);
4742 * Fix the lookup hint now, rather than each
4743 * time through the loop.
4745 SAVE_HINT_MAP_WRITE(map
, entry
->vme_prev
);
4747 entry
= first_entry
->vme_next
;
4751 if (entry
->superpage_size
)
4752 end
= SUPERPAGE_ROUND_UP(end
);
4754 need_wakeup
= FALSE
;
4756 * Step through all entries in this region
4758 s
= entry
->vme_start
;
4759 while ((entry
!= vm_map_to_entry(map
)) && (s
< end
)) {
4761 * At this point, we have deleted all the memory entries
4762 * between "start" and "s". We still need to delete
4763 * all memory entries between "s" and "end".
4764 * While we were blocked and the map was unlocked, some
4765 * new memory entries could have been re-allocated between
4766 * "start" and "s" and we don't want to mess with those.
4767 * Some of those entries could even have been re-assembled
4768 * with an entry after "s" (in vm_map_simplify_entry()), so
4769 * we may have to vm_map_clip_start() again.
4772 if (entry
->vme_start
>= s
) {
4774 * This entry starts on or after "s"
4775 * so no need to clip its start.
4779 * This entry has been re-assembled by a
4780 * vm_map_simplify_entry(). We need to
4781 * re-clip its start.
4783 vm_map_clip_start(map
, entry
, s
);
4785 if (entry
->vme_end
<= end
) {
4787 * This entry is going away completely, so no need
4788 * to clip and possibly cause an unnecessary unnesting.
4791 vm_map_clip_end(map
, entry
, end
);
4794 if (entry
->permanent
) {
4795 panic("attempt to remove permanent VM map entry "
4796 "%p [0x%llx:0x%llx]\n",
4797 entry
, (uint64_t) s
, (uint64_t) end
);
4801 if (entry
->in_transition
) {
4802 wait_result_t wait_result
;
4805 * Another thread is wiring/unwiring this entry.
4806 * Let the other thread know we are waiting.
4808 assert(s
== entry
->vme_start
);
4809 entry
->needs_wakeup
= TRUE
;
4812 * wake up anybody waiting on entries that we have
4813 * already unwired/deleted.
4816 vm_map_entry_wakeup(map
);
4817 need_wakeup
= FALSE
;
4820 wait_result
= vm_map_entry_wait(map
, interruptible
);
4822 if (interruptible
&&
4823 wait_result
== THREAD_INTERRUPTED
) {
4825 * We do not clear the needs_wakeup flag,
4826 * since we cannot tell if we were the only one.
4829 return KERN_ABORTED
;
4833 * The entry could have been clipped or it
4834 * may not exist anymore. Look it up again.
4836 if (!vm_map_lookup_entry(map
, s
, &first_entry
)) {
4837 assert((map
!= kernel_map
) &&
4838 (!entry
->is_sub_map
));
4840 * User: use the next entry
4842 entry
= first_entry
->vme_next
;
4843 s
= entry
->vme_start
;
4845 entry
= first_entry
;
4846 SAVE_HINT_MAP_WRITE(map
, entry
->vme_prev
);
4848 last_timestamp
= map
->timestamp
;
4850 } /* end in_transition */
4852 if (entry
->wired_count
) {
4853 boolean_t user_wire
;
4855 user_wire
= entry
->user_wired_count
> 0;
4858 * Remove a kernel wiring if requested
4860 if (flags
& VM_MAP_REMOVE_KUNWIRE
) {
4861 entry
->wired_count
--;
4865 * Remove all user wirings for proper accounting
4867 if (entry
->user_wired_count
> 0) {
4868 while (entry
->user_wired_count
)
4869 subtract_wire_counts(map
, entry
, user_wire
);
4872 if (entry
->wired_count
!= 0) {
4873 assert(map
!= kernel_map
);
4875 * Cannot continue. Typical case is when
4876 * a user thread has physical io pending on
4877 * on this page. Either wait for the
4878 * kernel wiring to go away or return an
4881 if (flags
& VM_MAP_REMOVE_WAIT_FOR_KWIRE
) {
4882 wait_result_t wait_result
;
4884 assert(s
== entry
->vme_start
);
4885 entry
->needs_wakeup
= TRUE
;
4886 wait_result
= vm_map_entry_wait(map
,
4889 if (interruptible
&&
4890 wait_result
== THREAD_INTERRUPTED
) {
4892 * We do not clear the
4893 * needs_wakeup flag, since we
4894 * cannot tell if we were the
4898 return KERN_ABORTED
;
4902 * The entry could have been clipped or
4903 * it may not exist anymore. Look it
4906 if (!vm_map_lookup_entry(map
, s
,
4908 assert(map
!= kernel_map
);
4910 * User: use the next entry
4912 entry
= first_entry
->vme_next
;
4913 s
= entry
->vme_start
;
4915 entry
= first_entry
;
4916 SAVE_HINT_MAP_WRITE(map
, entry
->vme_prev
);
4918 last_timestamp
= map
->timestamp
;
4922 return KERN_FAILURE
;
4926 entry
->in_transition
= TRUE
;
4928 * copy current entry. see comment in vm_map_wire()
4931 assert(s
== entry
->vme_start
);
4934 * We can unlock the map now. The in_transition
4935 * state guarentees existance of the entry.
4939 if (tmp_entry
.is_sub_map
) {
4941 vm_map_offset_t sub_start
, sub_end
;
4943 vm_map_offset_t pmap_addr
;
4946 sub_map
= tmp_entry
.object
.sub_map
;
4947 sub_start
= tmp_entry
.offset
;
4948 sub_end
= sub_start
+ (tmp_entry
.vme_end
-
4949 tmp_entry
.vme_start
);
4950 if (tmp_entry
.use_pmap
) {
4951 pmap
= sub_map
->pmap
;
4952 pmap_addr
= tmp_entry
.vme_start
;
4955 pmap_addr
= tmp_entry
.vme_start
;
4957 (void) vm_map_unwire_nested(sub_map
,
4963 vm_fault_unwire(map
, &tmp_entry
,
4964 tmp_entry
.object
.vm_object
== kernel_object
,
4965 map
->pmap
, tmp_entry
.vme_start
);
4970 if (last_timestamp
+1 != map
->timestamp
) {
4972 * Find the entry again. It could have
4973 * been clipped after we unlocked the map.
4975 if (!vm_map_lookup_entry(map
, s
, &first_entry
)){
4976 assert((map
!= kernel_map
) &&
4977 (!entry
->is_sub_map
));
4978 first_entry
= first_entry
->vme_next
;
4979 s
= first_entry
->vme_start
;
4981 SAVE_HINT_MAP_WRITE(map
, entry
->vme_prev
);
4984 SAVE_HINT_MAP_WRITE(map
, entry
->vme_prev
);
4985 first_entry
= entry
;
4988 last_timestamp
= map
->timestamp
;
4990 entry
= first_entry
;
4991 while ((entry
!= vm_map_to_entry(map
)) &&
4992 (entry
->vme_start
< tmp_entry
.vme_end
)) {
4993 assert(entry
->in_transition
);
4994 entry
->in_transition
= FALSE
;
4995 if (entry
->needs_wakeup
) {
4996 entry
->needs_wakeup
= FALSE
;
4999 entry
= entry
->vme_next
;
5002 * We have unwired the entry(s). Go back and
5005 entry
= first_entry
;
5009 /* entry is unwired */
5010 assert(entry
->wired_count
== 0);
5011 assert(entry
->user_wired_count
== 0);
5013 assert(s
== entry
->vme_start
);
5015 if (flags
& VM_MAP_REMOVE_NO_PMAP_CLEANUP
) {
5017 * XXX with the VM_MAP_REMOVE_SAVE_ENTRIES flag to
5018 * vm_map_delete(), some map entries might have been
5019 * transferred to a "zap_map", which doesn't have a
5020 * pmap. The original pmap has already been flushed
5021 * in the vm_map_delete() call targeting the original
5022 * map, but when we get to destroying the "zap_map",
5023 * we don't have any pmap to flush, so let's just skip
5026 } else if (entry
->is_sub_map
) {
5027 if (entry
->use_pmap
) {
5028 #ifndef NO_NESTED_PMAP
5029 pmap_unnest(map
->pmap
,
5030 (addr64_t
)entry
->vme_start
,
5031 entry
->vme_end
- entry
->vme_start
);
5032 #endif /* NO_NESTED_PMAP */
5033 if ((map
->mapped
) && (map
->ref_count
)) {
5034 /* clean up parent map/maps */
5035 vm_map_submap_pmap_clean(
5036 map
, entry
->vme_start
,
5038 entry
->object
.sub_map
,
5042 vm_map_submap_pmap_clean(
5043 map
, entry
->vme_start
, entry
->vme_end
,
5044 entry
->object
.sub_map
,
5047 } else if (entry
->object
.vm_object
!= kernel_object
) {
5048 object
= entry
->object
.vm_object
;
5049 if((map
->mapped
) && (map
->ref_count
)) {
5050 vm_object_pmap_protect(
5051 object
, entry
->offset
,
5052 entry
->vme_end
- entry
->vme_start
,
5057 pmap_remove(map
->pmap
,
5058 (addr64_t
)entry
->vme_start
,
5059 (addr64_t
)entry
->vme_end
);
5064 * All pmap mappings for this map entry must have been
5067 assert(vm_map_pmap_is_empty(map
,
5071 next
= entry
->vme_next
;
5072 s
= next
->vme_start
;
5073 last_timestamp
= map
->timestamp
;
5075 if ((flags
& VM_MAP_REMOVE_SAVE_ENTRIES
) &&
5076 zap_map
!= VM_MAP_NULL
) {
5077 vm_map_size_t entry_size
;
5079 * The caller wants to save the affected VM map entries
5080 * into the "zap_map". The caller will take care of
5083 /* unlink the entry from "map" ... */
5084 vm_map_store_entry_unlink(map
, entry
);
5085 /* ... and add it to the end of the "zap_map" */
5086 vm_map_store_entry_link(zap_map
,
5087 vm_map_last_entry(zap_map
),
5089 entry_size
= entry
->vme_end
- entry
->vme_start
;
5090 map
->size
-= entry_size
;
5091 zap_map
->size
+= entry_size
;
5092 /* we didn't unlock the map, so no timestamp increase */
5095 vm_map_entry_delete(map
, entry
);
5096 /* vm_map_entry_delete unlocks the map */
5102 if(entry
== vm_map_to_entry(map
)) {
5105 if (last_timestamp
+1 != map
->timestamp
) {
5107 * we are responsible for deleting everything
5108 * from the give space, if someone has interfered
5109 * we pick up where we left off, back fills should
5110 * be all right for anyone except map_delete and
5111 * we have to assume that the task has been fully
5112 * disabled before we get here
5114 if (!vm_map_lookup_entry(map
, s
, &entry
)){
5115 entry
= entry
->vme_next
;
5116 s
= entry
->vme_start
;
5118 SAVE_HINT_MAP_WRITE(map
, entry
->vme_prev
);
5121 * others can not only allocate behind us, we can
5122 * also see coalesce while we don't have the map lock
5124 if(entry
== vm_map_to_entry(map
)) {
5128 last_timestamp
= map
->timestamp
;
5131 if (map
->wait_for_space
)
5132 thread_wakeup((event_t
) map
);
5134 * wake up anybody waiting on entries that we have already deleted.
5137 vm_map_entry_wakeup(map
);
5139 return KERN_SUCCESS
;
5145 * Remove the given address range from the target map.
5146 * This is the exported form of vm_map_delete.
5150 register vm_map_t map
,
5151 register vm_map_offset_t start
,
5152 register vm_map_offset_t end
,
5153 register boolean_t flags
)
5155 register kern_return_t result
;
5158 VM_MAP_RANGE_CHECK(map
, start
, end
);
5159 result
= vm_map_delete(map
, start
, end
, flags
, VM_MAP_NULL
);
5167 * Routine: vm_map_copy_discard
5170 * Dispose of a map copy object (returned by
5174 vm_map_copy_discard(
5177 if (copy
== VM_MAP_COPY_NULL
)
5180 switch (copy
->type
) {
5181 case VM_MAP_COPY_ENTRY_LIST
:
5182 while (vm_map_copy_first_entry(copy
) !=
5183 vm_map_copy_to_entry(copy
)) {
5184 vm_map_entry_t entry
= vm_map_copy_first_entry(copy
);
5186 vm_map_copy_entry_unlink(copy
, entry
);
5187 vm_object_deallocate(entry
->object
.vm_object
);
5188 vm_map_copy_entry_dispose(copy
, entry
);
5191 case VM_MAP_COPY_OBJECT
:
5192 vm_object_deallocate(copy
->cpy_object
);
5194 case VM_MAP_COPY_KERNEL_BUFFER
:
5197 * The vm_map_copy_t and possibly the data buffer were
5198 * allocated by a single call to kalloc(), i.e. the
5199 * vm_map_copy_t was not allocated out of the zone.
5201 kfree(copy
, copy
->cpy_kalloc_size
);
5204 zfree(vm_map_copy_zone
, copy
);
5208 * Routine: vm_map_copy_copy
5211 * Move the information in a map copy object to
5212 * a new map copy object, leaving the old one
5215 * This is used by kernel routines that need
5216 * to look at out-of-line data (in copyin form)
5217 * before deciding whether to return SUCCESS.
5218 * If the routine returns FAILURE, the original
5219 * copy object will be deallocated; therefore,
5220 * these routines must make a copy of the copy
5221 * object and leave the original empty so that
5222 * deallocation will not fail.
5228 vm_map_copy_t new_copy
;
5230 if (copy
== VM_MAP_COPY_NULL
)
5231 return VM_MAP_COPY_NULL
;
5234 * Allocate a new copy object, and copy the information
5235 * from the old one into it.
5238 new_copy
= (vm_map_copy_t
) zalloc(vm_map_copy_zone
);
5241 if (copy
->type
== VM_MAP_COPY_ENTRY_LIST
) {
5243 * The links in the entry chain must be
5244 * changed to point to the new copy object.
5246 vm_map_copy_first_entry(copy
)->vme_prev
5247 = vm_map_copy_to_entry(new_copy
);
5248 vm_map_copy_last_entry(copy
)->vme_next
5249 = vm_map_copy_to_entry(new_copy
);
5253 * Change the old copy object into one that contains
5254 * nothing to be deallocated.
5256 copy
->type
= VM_MAP_COPY_OBJECT
;
5257 copy
->cpy_object
= VM_OBJECT_NULL
;
5260 * Return the new object.
5265 static kern_return_t
5266 vm_map_overwrite_submap_recurse(
5268 vm_map_offset_t dst_addr
,
5269 vm_map_size_t dst_size
)
5271 vm_map_offset_t dst_end
;
5272 vm_map_entry_t tmp_entry
;
5273 vm_map_entry_t entry
;
5274 kern_return_t result
;
5275 boolean_t encountered_sub_map
= FALSE
;
5280 * Verify that the destination is all writeable
5281 * initially. We have to trunc the destination
5282 * address and round the copy size or we'll end up
5283 * splitting entries in strange ways.
5286 dst_end
= vm_map_round_page(dst_addr
+ dst_size
);
5287 vm_map_lock(dst_map
);
5290 if (!vm_map_lookup_entry(dst_map
, dst_addr
, &tmp_entry
)) {
5291 vm_map_unlock(dst_map
);
5292 return(KERN_INVALID_ADDRESS
);
5295 vm_map_clip_start(dst_map
, tmp_entry
, vm_map_trunc_page(dst_addr
));
5296 assert(!tmp_entry
->use_pmap
); /* clipping did unnest if needed */
5298 for (entry
= tmp_entry
;;) {
5299 vm_map_entry_t next
;
5301 next
= entry
->vme_next
;
5302 while(entry
->is_sub_map
) {
5303 vm_map_offset_t sub_start
;
5304 vm_map_offset_t sub_end
;
5305 vm_map_offset_t local_end
;
5307 if (entry
->in_transition
) {
5309 * Say that we are waiting, and wait for entry.
5311 entry
->needs_wakeup
= TRUE
;
5312 vm_map_entry_wait(dst_map
, THREAD_UNINT
);
5317 encountered_sub_map
= TRUE
;
5318 sub_start
= entry
->offset
;
5320 if(entry
->vme_end
< dst_end
)
5321 sub_end
= entry
->vme_end
;
5324 sub_end
-= entry
->vme_start
;
5325 sub_end
+= entry
->offset
;
5326 local_end
= entry
->vme_end
;
5327 vm_map_unlock(dst_map
);
5329 result
= vm_map_overwrite_submap_recurse(
5330 entry
->object
.sub_map
,
5332 sub_end
- sub_start
);
5334 if(result
!= KERN_SUCCESS
)
5336 if (dst_end
<= entry
->vme_end
)
5337 return KERN_SUCCESS
;
5338 vm_map_lock(dst_map
);
5339 if(!vm_map_lookup_entry(dst_map
, local_end
,
5341 vm_map_unlock(dst_map
);
5342 return(KERN_INVALID_ADDRESS
);
5345 next
= entry
->vme_next
;
5348 if ( ! (entry
->protection
& VM_PROT_WRITE
)) {
5349 vm_map_unlock(dst_map
);
5350 return(KERN_PROTECTION_FAILURE
);
5354 * If the entry is in transition, we must wait
5355 * for it to exit that state. Anything could happen
5356 * when we unlock the map, so start over.
5358 if (entry
->in_transition
) {
5361 * Say that we are waiting, and wait for entry.
5363 entry
->needs_wakeup
= TRUE
;
5364 vm_map_entry_wait(dst_map
, THREAD_UNINT
);
5370 * our range is contained completely within this map entry
5372 if (dst_end
<= entry
->vme_end
) {
5373 vm_map_unlock(dst_map
);
5374 return KERN_SUCCESS
;
5377 * check that range specified is contiguous region
5379 if ((next
== vm_map_to_entry(dst_map
)) ||
5380 (next
->vme_start
!= entry
->vme_end
)) {
5381 vm_map_unlock(dst_map
);
5382 return(KERN_INVALID_ADDRESS
);
5386 * Check for permanent objects in the destination.
5388 if ((entry
->object
.vm_object
!= VM_OBJECT_NULL
) &&
5389 ((!entry
->object
.vm_object
->internal
) ||
5390 (entry
->object
.vm_object
->true_share
))) {
5391 if(encountered_sub_map
) {
5392 vm_map_unlock(dst_map
);
5393 return(KERN_FAILURE
);
5400 vm_map_unlock(dst_map
);
5401 return(KERN_SUCCESS
);
5405 * Routine: vm_map_copy_overwrite
5408 * Copy the memory described by the map copy
5409 * object (copy; returned by vm_map_copyin) onto
5410 * the specified destination region (dst_map, dst_addr).
5411 * The destination must be writeable.
5413 * Unlike vm_map_copyout, this routine actually
5414 * writes over previously-mapped memory. If the
5415 * previous mapping was to a permanent (user-supplied)
5416 * memory object, it is preserved.
5418 * The attributes (protection and inheritance) of the
5419 * destination region are preserved.
5421 * If successful, consumes the copy object.
5422 * Otherwise, the caller is responsible for it.
5424 * Implementation notes:
5425 * To overwrite aligned temporary virtual memory, it is
5426 * sufficient to remove the previous mapping and insert
5427 * the new copy. This replacement is done either on
5428 * the whole region (if no permanent virtual memory
5429 * objects are embedded in the destination region) or
5430 * in individual map entries.
5432 * To overwrite permanent virtual memory , it is necessary
5433 * to copy each page, as the external memory management
5434 * interface currently does not provide any optimizations.
5436 * Unaligned memory also has to be copied. It is possible
5437 * to use 'vm_trickery' to copy the aligned data. This is
5438 * not done but not hard to implement.
5440 * Once a page of permanent memory has been overwritten,
5441 * it is impossible to interrupt this function; otherwise,
5442 * the call would be neither atomic nor location-independent.
5443 * The kernel-state portion of a user thread must be
5446 * It may be expensive to forward all requests that might
5447 * overwrite permanent memory (vm_write, vm_copy) to
5448 * uninterruptible kernel threads. This routine may be
5449 * called by interruptible threads; however, success is
5450 * not guaranteed -- if the request cannot be performed
5451 * atomically and interruptibly, an error indication is
5455 static kern_return_t
5456 vm_map_copy_overwrite_nested(
5458 vm_map_address_t dst_addr
,
5460 boolean_t interruptible
,
5462 boolean_t discard_on_success
)
5464 vm_map_offset_t dst_end
;
5465 vm_map_entry_t tmp_entry
;
5466 vm_map_entry_t entry
;
5468 boolean_t aligned
= TRUE
;
5469 boolean_t contains_permanent_objects
= FALSE
;
5470 boolean_t encountered_sub_map
= FALSE
;
5471 vm_map_offset_t base_addr
;
5472 vm_map_size_t copy_size
;
5473 vm_map_size_t total_size
;
5477 * Check for null copy object.
5480 if (copy
== VM_MAP_COPY_NULL
)
5481 return(KERN_SUCCESS
);
5484 * Check for special kernel buffer allocated
5485 * by new_ipc_kmsg_copyin.
5488 if (copy
->type
== VM_MAP_COPY_KERNEL_BUFFER
) {
5489 return(vm_map_copyout_kernel_buffer(
5495 * Only works for entry lists at the moment. Will
5496 * support page lists later.
5499 assert(copy
->type
== VM_MAP_COPY_ENTRY_LIST
);
5501 if (copy
->size
== 0) {
5502 if (discard_on_success
)
5503 vm_map_copy_discard(copy
);
5504 return(KERN_SUCCESS
);
5508 * Verify that the destination is all writeable
5509 * initially. We have to trunc the destination
5510 * address and round the copy size or we'll end up
5511 * splitting entries in strange ways.
5514 if (!page_aligned(copy
->size
) ||
5515 !page_aligned (copy
->offset
) ||
5516 !page_aligned (dst_addr
))
5519 dst_end
= vm_map_round_page(dst_addr
+ copy
->size
);
5521 dst_end
= dst_addr
+ copy
->size
;
5524 vm_map_lock(dst_map
);
5526 /* LP64todo - remove this check when vm_map_commpage64()
5527 * no longer has to stuff in a map_entry for the commpage
5528 * above the map's max_offset.
5530 if (dst_addr
>= dst_map
->max_offset
) {
5531 vm_map_unlock(dst_map
);
5532 return(KERN_INVALID_ADDRESS
);
5536 if (!vm_map_lookup_entry(dst_map
, dst_addr
, &tmp_entry
)) {
5537 vm_map_unlock(dst_map
);
5538 return(KERN_INVALID_ADDRESS
);
5540 vm_map_clip_start(dst_map
, tmp_entry
, vm_map_trunc_page(dst_addr
));
5541 for (entry
= tmp_entry
;;) {
5542 vm_map_entry_t next
= entry
->vme_next
;
5544 while(entry
->is_sub_map
) {
5545 vm_map_offset_t sub_start
;
5546 vm_map_offset_t sub_end
;
5547 vm_map_offset_t local_end
;
5549 if (entry
->in_transition
) {
5552 * Say that we are waiting, and wait for entry.
5554 entry
->needs_wakeup
= TRUE
;
5555 vm_map_entry_wait(dst_map
, THREAD_UNINT
);
5560 local_end
= entry
->vme_end
;
5561 if (!(entry
->needs_copy
)) {
5562 /* if needs_copy we are a COW submap */
5563 /* in such a case we just replace so */
5564 /* there is no need for the follow- */
5566 encountered_sub_map
= TRUE
;
5567 sub_start
= entry
->offset
;
5569 if(entry
->vme_end
< dst_end
)
5570 sub_end
= entry
->vme_end
;
5573 sub_end
-= entry
->vme_start
;
5574 sub_end
+= entry
->offset
;
5575 vm_map_unlock(dst_map
);
5577 kr
= vm_map_overwrite_submap_recurse(
5578 entry
->object
.sub_map
,
5580 sub_end
- sub_start
);
5581 if(kr
!= KERN_SUCCESS
)
5583 vm_map_lock(dst_map
);
5586 if (dst_end
<= entry
->vme_end
)
5587 goto start_overwrite
;
5588 if(!vm_map_lookup_entry(dst_map
, local_end
,
5590 vm_map_unlock(dst_map
);
5591 return(KERN_INVALID_ADDRESS
);
5593 next
= entry
->vme_next
;
5596 if ( ! (entry
->protection
& VM_PROT_WRITE
)) {
5597 vm_map_unlock(dst_map
);
5598 return(KERN_PROTECTION_FAILURE
);
5602 * If the entry is in transition, we must wait
5603 * for it to exit that state. Anything could happen
5604 * when we unlock the map, so start over.
5606 if (entry
->in_transition
) {
5609 * Say that we are waiting, and wait for entry.
5611 entry
->needs_wakeup
= TRUE
;
5612 vm_map_entry_wait(dst_map
, THREAD_UNINT
);
5618 * our range is contained completely within this map entry
5620 if (dst_end
<= entry
->vme_end
)
5623 * check that range specified is contiguous region
5625 if ((next
== vm_map_to_entry(dst_map
)) ||
5626 (next
->vme_start
!= entry
->vme_end
)) {
5627 vm_map_unlock(dst_map
);
5628 return(KERN_INVALID_ADDRESS
);
5633 * Check for permanent objects in the destination.
5635 if ((entry
->object
.vm_object
!= VM_OBJECT_NULL
) &&
5636 ((!entry
->object
.vm_object
->internal
) ||
5637 (entry
->object
.vm_object
->true_share
))) {
5638 contains_permanent_objects
= TRUE
;
5646 * If there are permanent objects in the destination, then
5647 * the copy cannot be interrupted.
5650 if (interruptible
&& contains_permanent_objects
) {
5651 vm_map_unlock(dst_map
);
5652 return(KERN_FAILURE
); /* XXX */
5657 * Make a second pass, overwriting the data
5658 * At the beginning of each loop iteration,
5659 * the next entry to be overwritten is "tmp_entry"
5660 * (initially, the value returned from the lookup above),
5661 * and the starting address expected in that entry
5665 total_size
= copy
->size
;
5666 if(encountered_sub_map
) {
5668 /* re-calculate tmp_entry since we've had the map */
5670 if (!vm_map_lookup_entry( dst_map
, dst_addr
, &tmp_entry
)) {
5671 vm_map_unlock(dst_map
);
5672 return(KERN_INVALID_ADDRESS
);
5675 copy_size
= copy
->size
;
5678 base_addr
= dst_addr
;
5680 /* deconstruct the copy object and do in parts */
5681 /* only in sub_map, interruptable case */
5682 vm_map_entry_t copy_entry
;
5683 vm_map_entry_t previous_prev
= VM_MAP_ENTRY_NULL
;
5684 vm_map_entry_t next_copy
= VM_MAP_ENTRY_NULL
;
5686 int remaining_entries
= 0;
5687 vm_map_offset_t new_offset
= 0;
5689 for (entry
= tmp_entry
; copy_size
== 0;) {
5690 vm_map_entry_t next
;
5692 next
= entry
->vme_next
;
5694 /* tmp_entry and base address are moved along */
5695 /* each time we encounter a sub-map. Otherwise */
5696 /* entry can outpase tmp_entry, and the copy_size */
5697 /* may reflect the distance between them */
5698 /* if the current entry is found to be in transition */
5699 /* we will start over at the beginning or the last */
5700 /* encounter of a submap as dictated by base_addr */
5701 /* we will zero copy_size accordingly. */
5702 if (entry
->in_transition
) {
5704 * Say that we are waiting, and wait for entry.
5706 entry
->needs_wakeup
= TRUE
;
5707 vm_map_entry_wait(dst_map
, THREAD_UNINT
);
5709 if(!vm_map_lookup_entry(dst_map
, base_addr
,
5711 vm_map_unlock(dst_map
);
5712 return(KERN_INVALID_ADDRESS
);
5718 if(entry
->is_sub_map
) {
5719 vm_map_offset_t sub_start
;
5720 vm_map_offset_t sub_end
;
5721 vm_map_offset_t local_end
;
5723 if (entry
->needs_copy
) {
5724 /* if this is a COW submap */
5725 /* just back the range with a */
5726 /* anonymous entry */
5727 if(entry
->vme_end
< dst_end
)
5728 sub_end
= entry
->vme_end
;
5731 if(entry
->vme_start
< base_addr
)
5732 sub_start
= base_addr
;
5734 sub_start
= entry
->vme_start
;
5736 dst_map
, entry
, sub_end
);
5738 dst_map
, entry
, sub_start
);
5739 assert(!entry
->use_pmap
);
5740 entry
->is_sub_map
= FALSE
;
5742 entry
->object
.sub_map
);
5743 entry
->object
.sub_map
= NULL
;
5744 entry
->is_shared
= FALSE
;
5745 entry
->needs_copy
= FALSE
;
5749 * We should propagate the protections
5750 * of the submap entry here instead
5751 * of forcing them to VM_PROT_ALL...
5752 * Or better yet, we should inherit
5753 * the protection of the copy_entry.
5755 entry
->protection
= VM_PROT_ALL
;
5756 entry
->max_protection
= VM_PROT_ALL
;
5757 entry
->wired_count
= 0;
5758 entry
->user_wired_count
= 0;
5759 if(entry
->inheritance
5760 == VM_INHERIT_SHARE
)
5761 entry
->inheritance
= VM_INHERIT_COPY
;
5764 /* first take care of any non-sub_map */
5765 /* entries to send */
5766 if(base_addr
< entry
->vme_start
) {
5769 entry
->vme_start
- base_addr
;
5772 sub_start
= entry
->offset
;
5774 if(entry
->vme_end
< dst_end
)
5775 sub_end
= entry
->vme_end
;
5778 sub_end
-= entry
->vme_start
;
5779 sub_end
+= entry
->offset
;
5780 local_end
= entry
->vme_end
;
5781 vm_map_unlock(dst_map
);
5782 copy_size
= sub_end
- sub_start
;
5784 /* adjust the copy object */
5785 if (total_size
> copy_size
) {
5786 vm_map_size_t local_size
= 0;
5787 vm_map_size_t entry_size
;
5790 new_offset
= copy
->offset
;
5791 copy_entry
= vm_map_copy_first_entry(copy
);
5793 vm_map_copy_to_entry(copy
)){
5794 entry_size
= copy_entry
->vme_end
-
5795 copy_entry
->vme_start
;
5796 if((local_size
< copy_size
) &&
5797 ((local_size
+ entry_size
)
5799 vm_map_copy_clip_end(copy
,
5801 copy_entry
->vme_start
+
5802 (copy_size
- local_size
));
5803 entry_size
= copy_entry
->vme_end
-
5804 copy_entry
->vme_start
;
5805 local_size
+= entry_size
;
5806 new_offset
+= entry_size
;
5808 if(local_size
>= copy_size
) {
5809 next_copy
= copy_entry
->vme_next
;
5810 copy_entry
->vme_next
=
5811 vm_map_copy_to_entry(copy
);
5813 copy
->cpy_hdr
.links
.prev
;
5814 copy
->cpy_hdr
.links
.prev
= copy_entry
;
5815 copy
->size
= copy_size
;
5817 copy
->cpy_hdr
.nentries
;
5818 remaining_entries
-= nentries
;
5819 copy
->cpy_hdr
.nentries
= nentries
;
5822 local_size
+= entry_size
;
5823 new_offset
+= entry_size
;
5826 copy_entry
= copy_entry
->vme_next
;
5830 if((entry
->use_pmap
) && (pmap
== NULL
)) {
5831 kr
= vm_map_copy_overwrite_nested(
5832 entry
->object
.sub_map
,
5836 entry
->object
.sub_map
->pmap
,
5838 } else if (pmap
!= NULL
) {
5839 kr
= vm_map_copy_overwrite_nested(
5840 entry
->object
.sub_map
,
5843 interruptible
, pmap
,
5846 kr
= vm_map_copy_overwrite_nested(
5847 entry
->object
.sub_map
,
5854 if(kr
!= KERN_SUCCESS
) {
5855 if(next_copy
!= NULL
) {
5856 copy
->cpy_hdr
.nentries
+=
5858 copy
->cpy_hdr
.links
.prev
->vme_next
=
5860 copy
->cpy_hdr
.links
.prev
5862 copy
->size
= total_size
;
5866 if (dst_end
<= local_end
) {
5867 return(KERN_SUCCESS
);
5869 /* otherwise copy no longer exists, it was */
5870 /* destroyed after successful copy_overwrite */
5871 copy
= (vm_map_copy_t
)
5872 zalloc(vm_map_copy_zone
);
5873 vm_map_copy_first_entry(copy
) =
5874 vm_map_copy_last_entry(copy
) =
5875 vm_map_copy_to_entry(copy
);
5876 copy
->type
= VM_MAP_COPY_ENTRY_LIST
;
5877 copy
->offset
= new_offset
;
5879 total_size
-= copy_size
;
5881 /* put back remainder of copy in container */
5882 if(next_copy
!= NULL
) {
5883 copy
->cpy_hdr
.nentries
= remaining_entries
;
5884 copy
->cpy_hdr
.links
.next
= next_copy
;
5885 copy
->cpy_hdr
.links
.prev
= previous_prev
;
5886 copy
->size
= total_size
;
5887 next_copy
->vme_prev
=
5888 vm_map_copy_to_entry(copy
);
5891 base_addr
= local_end
;
5892 vm_map_lock(dst_map
);
5893 if(!vm_map_lookup_entry(dst_map
,
5894 local_end
, &tmp_entry
)) {
5895 vm_map_unlock(dst_map
);
5896 return(KERN_INVALID_ADDRESS
);
5901 if (dst_end
<= entry
->vme_end
) {
5902 copy_size
= dst_end
- base_addr
;
5906 if ((next
== vm_map_to_entry(dst_map
)) ||
5907 (next
->vme_start
!= entry
->vme_end
)) {
5908 vm_map_unlock(dst_map
);
5909 return(KERN_INVALID_ADDRESS
);
5918 /* adjust the copy object */
5919 if (total_size
> copy_size
) {
5920 vm_map_size_t local_size
= 0;
5921 vm_map_size_t entry_size
;
5923 new_offset
= copy
->offset
;
5924 copy_entry
= vm_map_copy_first_entry(copy
);
5925 while(copy_entry
!= vm_map_copy_to_entry(copy
)) {
5926 entry_size
= copy_entry
->vme_end
-
5927 copy_entry
->vme_start
;
5928 if((local_size
< copy_size
) &&
5929 ((local_size
+ entry_size
)
5931 vm_map_copy_clip_end(copy
, copy_entry
,
5932 copy_entry
->vme_start
+
5933 (copy_size
- local_size
));
5934 entry_size
= copy_entry
->vme_end
-
5935 copy_entry
->vme_start
;
5936 local_size
+= entry_size
;
5937 new_offset
+= entry_size
;
5939 if(local_size
>= copy_size
) {
5940 next_copy
= copy_entry
->vme_next
;
5941 copy_entry
->vme_next
=
5942 vm_map_copy_to_entry(copy
);
5944 copy
->cpy_hdr
.links
.prev
;
5945 copy
->cpy_hdr
.links
.prev
= copy_entry
;
5946 copy
->size
= copy_size
;
5948 copy
->cpy_hdr
.nentries
;
5949 remaining_entries
-= nentries
;
5950 copy
->cpy_hdr
.nentries
= nentries
;
5953 local_size
+= entry_size
;
5954 new_offset
+= entry_size
;
5957 copy_entry
= copy_entry
->vme_next
;
5967 local_pmap
= dst_map
->pmap
;
5969 if ((kr
= vm_map_copy_overwrite_aligned(
5970 dst_map
, tmp_entry
, copy
,
5971 base_addr
, local_pmap
)) != KERN_SUCCESS
) {
5972 if(next_copy
!= NULL
) {
5973 copy
->cpy_hdr
.nentries
+=
5975 copy
->cpy_hdr
.links
.prev
->vme_next
=
5977 copy
->cpy_hdr
.links
.prev
=
5979 copy
->size
+= copy_size
;
5983 vm_map_unlock(dst_map
);
5988 * if the copy and dst address are misaligned but the same
5989 * offset within the page we can copy_not_aligned the
5990 * misaligned parts and copy aligned the rest. If they are
5991 * aligned but len is unaligned we simply need to copy
5992 * the end bit unaligned. We'll need to split the misaligned
5993 * bits of the region in this case !
5995 /* ALWAYS UNLOCKS THE dst_map MAP */
5996 if ((kr
= vm_map_copy_overwrite_unaligned( dst_map
,
5997 tmp_entry
, copy
, base_addr
)) != KERN_SUCCESS
) {
5998 if(next_copy
!= NULL
) {
5999 copy
->cpy_hdr
.nentries
+=
6001 copy
->cpy_hdr
.links
.prev
->vme_next
=
6003 copy
->cpy_hdr
.links
.prev
=
6005 copy
->size
+= copy_size
;
6010 total_size
-= copy_size
;
6013 base_addr
+= copy_size
;
6015 copy
->offset
= new_offset
;
6016 if(next_copy
!= NULL
) {
6017 copy
->cpy_hdr
.nentries
= remaining_entries
;
6018 copy
->cpy_hdr
.links
.next
= next_copy
;
6019 copy
->cpy_hdr
.links
.prev
= previous_prev
;
6020 next_copy
->vme_prev
= vm_map_copy_to_entry(copy
);
6021 copy
->size
= total_size
;
6023 vm_map_lock(dst_map
);
6025 if (!vm_map_lookup_entry(dst_map
,
6026 base_addr
, &tmp_entry
)) {
6027 vm_map_unlock(dst_map
);
6028 return(KERN_INVALID_ADDRESS
);
6030 if (tmp_entry
->in_transition
) {
6031 entry
->needs_wakeup
= TRUE
;
6032 vm_map_entry_wait(dst_map
, THREAD_UNINT
);
6037 vm_map_clip_start(dst_map
, tmp_entry
, vm_map_trunc_page(base_addr
));
6043 * Throw away the vm_map_copy object
6045 if (discard_on_success
)
6046 vm_map_copy_discard(copy
);
6048 return(KERN_SUCCESS
);
6049 }/* vm_map_copy_overwrite */
6052 vm_map_copy_overwrite(
6054 vm_map_offset_t dst_addr
,
6056 boolean_t interruptible
)
6058 vm_map_size_t head_size
, tail_size
;
6059 vm_map_copy_t head_copy
, tail_copy
;
6060 vm_map_offset_t head_addr
, tail_addr
;
6061 vm_map_entry_t entry
;
6071 if (interruptible
||
6072 copy
== VM_MAP_COPY_NULL
||
6073 copy
->type
!= VM_MAP_COPY_ENTRY_LIST
) {
6075 * We can't split the "copy" map if we're interruptible
6076 * or if we don't have a "copy" map...
6079 return vm_map_copy_overwrite_nested(dst_map
,
6087 if (copy
->size
< 3 * PAGE_SIZE
) {
6089 * Too small to bother with optimizing...
6094 if ((dst_addr
& PAGE_MASK
) != (copy
->offset
& PAGE_MASK
)) {
6096 * Incompatible mis-alignment of source and destination...
6102 * Proper alignment or identical mis-alignment at the beginning.
6103 * Let's try and do a small unaligned copy first (if needed)
6104 * and then an aligned copy for the rest.
6106 if (!page_aligned(dst_addr
)) {
6107 head_addr
= dst_addr
;
6108 head_size
= PAGE_SIZE
- (copy
->offset
& PAGE_MASK
);
6110 if (!page_aligned(copy
->offset
+ copy
->size
)) {
6112 * Mis-alignment at the end.
6113 * Do an aligned copy up to the last page and
6114 * then an unaligned copy for the remaining bytes.
6116 tail_size
= (copy
->offset
+ copy
->size
) & PAGE_MASK
;
6117 tail_addr
= dst_addr
+ copy
->size
- tail_size
;
6120 if (head_size
+ tail_size
== copy
->size
) {
6122 * It's all unaligned, no optimization possible...
6128 * Can't optimize if there are any submaps in the
6129 * destination due to the way we free the "copy" map
6130 * progressively in vm_map_copy_overwrite_nested()
6133 vm_map_lock_read(dst_map
);
6134 if (! vm_map_lookup_entry(dst_map
, dst_addr
, &entry
)) {
6135 vm_map_unlock_read(dst_map
);
6139 (entry
!= vm_map_copy_to_entry(copy
) &&
6140 entry
->vme_start
< dst_addr
+ copy
->size
);
6141 entry
= entry
->vme_next
) {
6142 if (entry
->is_sub_map
) {
6143 vm_map_unlock_read(dst_map
);
6147 vm_map_unlock_read(dst_map
);
6151 * Unaligned copy of the first "head_size" bytes, to reach
6156 * Extract "head_copy" out of "copy".
6158 head_copy
= (vm_map_copy_t
) zalloc(vm_map_copy_zone
);
6159 vm_map_copy_first_entry(head_copy
) =
6160 vm_map_copy_to_entry(head_copy
);
6161 vm_map_copy_last_entry(head_copy
) =
6162 vm_map_copy_to_entry(head_copy
);
6163 head_copy
->type
= VM_MAP_COPY_ENTRY_LIST
;
6164 head_copy
->cpy_hdr
.nentries
= 0;
6165 head_copy
->cpy_hdr
.entries_pageable
=
6166 copy
->cpy_hdr
.entries_pageable
;
6167 vm_map_store_init(&head_copy
->cpy_hdr
);
6169 head_copy
->offset
= copy
->offset
;
6170 head_copy
->size
= head_size
;
6172 copy
->offset
+= head_size
;
6173 copy
->size
-= head_size
;
6175 entry
= vm_map_copy_first_entry(copy
);
6176 vm_map_copy_clip_end(copy
, entry
, copy
->offset
);
6177 vm_map_copy_entry_unlink(copy
, entry
);
6178 vm_map_copy_entry_link(head_copy
,
6179 vm_map_copy_to_entry(head_copy
),
6183 * Do the unaligned copy.
6185 kr
= vm_map_copy_overwrite_nested(dst_map
,
6191 if (kr
!= KERN_SUCCESS
)
6197 * Extract "tail_copy" out of "copy".
6199 tail_copy
= (vm_map_copy_t
) zalloc(vm_map_copy_zone
);
6200 vm_map_copy_first_entry(tail_copy
) =
6201 vm_map_copy_to_entry(tail_copy
);
6202 vm_map_copy_last_entry(tail_copy
) =
6203 vm_map_copy_to_entry(tail_copy
);
6204 tail_copy
->type
= VM_MAP_COPY_ENTRY_LIST
;
6205 tail_copy
->cpy_hdr
.nentries
= 0;
6206 tail_copy
->cpy_hdr
.entries_pageable
=
6207 copy
->cpy_hdr
.entries_pageable
;
6208 vm_map_store_init(&tail_copy
->cpy_hdr
);
6210 tail_copy
->offset
= copy
->offset
+ copy
->size
- tail_size
;
6211 tail_copy
->size
= tail_size
;
6213 copy
->size
-= tail_size
;
6215 entry
= vm_map_copy_last_entry(copy
);
6216 vm_map_copy_clip_start(copy
, entry
, tail_copy
->offset
);
6217 entry
= vm_map_copy_last_entry(copy
);
6218 vm_map_copy_entry_unlink(copy
, entry
);
6219 vm_map_copy_entry_link(tail_copy
,
6220 vm_map_copy_last_entry(tail_copy
),
6225 * Copy most (or possibly all) of the data.
6227 kr
= vm_map_copy_overwrite_nested(dst_map
,
6228 dst_addr
+ head_size
,
6233 if (kr
!= KERN_SUCCESS
) {
6238 kr
= vm_map_copy_overwrite_nested(dst_map
,
6247 assert(copy
->type
== VM_MAP_COPY_ENTRY_LIST
);
6248 if (kr
== KERN_SUCCESS
) {
6250 * Discard all the copy maps.
6253 vm_map_copy_discard(head_copy
);
6256 vm_map_copy_discard(copy
);
6258 vm_map_copy_discard(tail_copy
);
6263 * Re-assemble the original copy map.
6266 entry
= vm_map_copy_first_entry(head_copy
);
6267 vm_map_copy_entry_unlink(head_copy
, entry
);
6268 vm_map_copy_entry_link(copy
,
6269 vm_map_copy_to_entry(copy
),
6271 copy
->offset
-= head_size
;
6272 copy
->size
+= head_size
;
6273 vm_map_copy_discard(head_copy
);
6277 entry
= vm_map_copy_last_entry(tail_copy
);
6278 vm_map_copy_entry_unlink(tail_copy
, entry
);
6279 vm_map_copy_entry_link(copy
,
6280 vm_map_copy_last_entry(copy
),
6282 copy
->size
+= tail_size
;
6283 vm_map_copy_discard(tail_copy
);
6292 * Routine: vm_map_copy_overwrite_unaligned [internal use only]
6295 * Physically copy unaligned data
6298 * Unaligned parts of pages have to be physically copied. We use
6299 * a modified form of vm_fault_copy (which understands none-aligned
6300 * page offsets and sizes) to do the copy. We attempt to copy as
6301 * much memory in one go as possibly, however vm_fault_copy copies
6302 * within 1 memory object so we have to find the smaller of "amount left"
6303 * "source object data size" and "target object data size". With
6304 * unaligned data we don't need to split regions, therefore the source
6305 * (copy) object should be one map entry, the target range may be split
6306 * over multiple map entries however. In any event we are pessimistic
6307 * about these assumptions.
6310 * dst_map is locked on entry and is return locked on success,
6311 * unlocked on error.
6314 static kern_return_t
6315 vm_map_copy_overwrite_unaligned(
6317 vm_map_entry_t entry
,
6319 vm_map_offset_t start
)
6321 vm_map_entry_t copy_entry
= vm_map_copy_first_entry(copy
);
6322 vm_map_version_t version
;
6323 vm_object_t dst_object
;
6324 vm_object_offset_t dst_offset
;
6325 vm_object_offset_t src_offset
;
6326 vm_object_offset_t entry_offset
;
6327 vm_map_offset_t entry_end
;
6328 vm_map_size_t src_size
,
6332 kern_return_t kr
= KERN_SUCCESS
;
6334 vm_map_lock_write_to_read(dst_map
);
6336 src_offset
= copy
->offset
- vm_object_trunc_page(copy
->offset
);
6337 amount_left
= copy
->size
;
6339 * unaligned so we never clipped this entry, we need the offset into
6340 * the vm_object not just the data.
6342 while (amount_left
> 0) {
6344 if (entry
== vm_map_to_entry(dst_map
)) {
6345 vm_map_unlock_read(dst_map
);
6346 return KERN_INVALID_ADDRESS
;
6349 /* "start" must be within the current map entry */
6350 assert ((start
>=entry
->vme_start
) && (start
<entry
->vme_end
));
6352 dst_offset
= start
- entry
->vme_start
;
6354 dst_size
= entry
->vme_end
- start
;
6356 src_size
= copy_entry
->vme_end
-
6357 (copy_entry
->vme_start
+ src_offset
);
6359 if (dst_size
< src_size
) {
6361 * we can only copy dst_size bytes before
6362 * we have to get the next destination entry
6364 copy_size
= dst_size
;
6367 * we can only copy src_size bytes before
6368 * we have to get the next source copy entry
6370 copy_size
= src_size
;
6373 if (copy_size
> amount_left
) {
6374 copy_size
= amount_left
;
6377 * Entry needs copy, create a shadow shadow object for
6378 * Copy on write region.
6380 if (entry
->needs_copy
&&
6381 ((entry
->protection
& VM_PROT_WRITE
) != 0))
6383 if (vm_map_lock_read_to_write(dst_map
)) {
6384 vm_map_lock_read(dst_map
);
6387 vm_object_shadow(&entry
->object
.vm_object
,
6389 (vm_map_size_t
)(entry
->vme_end
6390 - entry
->vme_start
));
6391 entry
->needs_copy
= FALSE
;
6392 vm_map_lock_write_to_read(dst_map
);
6394 dst_object
= entry
->object
.vm_object
;
6396 * unlike with the virtual (aligned) copy we're going
6397 * to fault on it therefore we need a target object.
6399 if (dst_object
== VM_OBJECT_NULL
) {
6400 if (vm_map_lock_read_to_write(dst_map
)) {
6401 vm_map_lock_read(dst_map
);
6404 dst_object
= vm_object_allocate((vm_map_size_t
)
6405 entry
->vme_end
- entry
->vme_start
);
6406 entry
->object
.vm_object
= dst_object
;
6408 vm_map_lock_write_to_read(dst_map
);
6411 * Take an object reference and unlock map. The "entry" may
6412 * disappear or change when the map is unlocked.
6414 vm_object_reference(dst_object
);
6415 version
.main_timestamp
= dst_map
->timestamp
;
6416 entry_offset
= entry
->offset
;
6417 entry_end
= entry
->vme_end
;
6418 vm_map_unlock_read(dst_map
);
6420 * Copy as much as possible in one pass
6423 copy_entry
->object
.vm_object
,
6424 copy_entry
->offset
+ src_offset
,
6427 entry_offset
+ dst_offset
,
6433 src_offset
+= copy_size
;
6434 amount_left
-= copy_size
;
6436 * Release the object reference
6438 vm_object_deallocate(dst_object
);
6440 * If a hard error occurred, return it now
6442 if (kr
!= KERN_SUCCESS
)
6445 if ((copy_entry
->vme_start
+ src_offset
) == copy_entry
->vme_end
6446 || amount_left
== 0)
6449 * all done with this copy entry, dispose.
6451 vm_map_copy_entry_unlink(copy
, copy_entry
);
6452 vm_object_deallocate(copy_entry
->object
.vm_object
);
6453 vm_map_copy_entry_dispose(copy
, copy_entry
);
6455 if ((copy_entry
= vm_map_copy_first_entry(copy
))
6456 == vm_map_copy_to_entry(copy
) && amount_left
) {
6458 * not finished copying but run out of source
6460 return KERN_INVALID_ADDRESS
;
6465 if (amount_left
== 0)
6466 return KERN_SUCCESS
;
6468 vm_map_lock_read(dst_map
);
6469 if (version
.main_timestamp
== dst_map
->timestamp
) {
6470 if (start
== entry_end
) {
6472 * destination region is split. Use the version
6473 * information to avoid a lookup in the normal
6476 entry
= entry
->vme_next
;
6478 * should be contiguous. Fail if we encounter
6479 * a hole in the destination.
6481 if (start
!= entry
->vme_start
) {
6482 vm_map_unlock_read(dst_map
);
6483 return KERN_INVALID_ADDRESS
;
6488 * Map version check failed.
6489 * we must lookup the entry because somebody
6490 * might have changed the map behind our backs.
6493 if (!vm_map_lookup_entry(dst_map
, start
, &entry
))
6495 vm_map_unlock_read(dst_map
);
6496 return KERN_INVALID_ADDRESS
;
6501 return KERN_SUCCESS
;
6502 }/* vm_map_copy_overwrite_unaligned */
6505 * Routine: vm_map_copy_overwrite_aligned [internal use only]
6508 * Does all the vm_trickery possible for whole pages.
6512 * If there are no permanent objects in the destination,
6513 * and the source and destination map entry zones match,
6514 * and the destination map entry is not shared,
6515 * then the map entries can be deleted and replaced
6516 * with those from the copy. The following code is the
6517 * basic idea of what to do, but there are lots of annoying
6518 * little details about getting protection and inheritance
6519 * right. Should add protection, inheritance, and sharing checks
6520 * to the above pass and make sure that no wiring is involved.
6523 static kern_return_t
6524 vm_map_copy_overwrite_aligned(
6526 vm_map_entry_t tmp_entry
,
6528 vm_map_offset_t start
,
6529 __unused pmap_t pmap
)
6532 vm_map_entry_t copy_entry
;
6533 vm_map_size_t copy_size
;
6535 vm_map_entry_t entry
;
6537 while ((copy_entry
= vm_map_copy_first_entry(copy
))
6538 != vm_map_copy_to_entry(copy
))
6540 copy_size
= (copy_entry
->vme_end
- copy_entry
->vme_start
);
6543 assert(!entry
->use_pmap
); /* unnested when clipped earlier */
6544 if (entry
== vm_map_to_entry(dst_map
)) {
6545 vm_map_unlock(dst_map
);
6546 return KERN_INVALID_ADDRESS
;
6548 size
= (entry
->vme_end
- entry
->vme_start
);
6550 * Make sure that no holes popped up in the
6551 * address map, and that the protection is
6552 * still valid, in case the map was unlocked
6556 if ((entry
->vme_start
!= start
) || ((entry
->is_sub_map
)
6557 && !entry
->needs_copy
)) {
6558 vm_map_unlock(dst_map
);
6559 return(KERN_INVALID_ADDRESS
);
6561 assert(entry
!= vm_map_to_entry(dst_map
));
6564 * Check protection again
6567 if ( ! (entry
->protection
& VM_PROT_WRITE
)) {
6568 vm_map_unlock(dst_map
);
6569 return(KERN_PROTECTION_FAILURE
);
6573 * Adjust to source size first
6576 if (copy_size
< size
) {
6577 vm_map_clip_end(dst_map
, entry
, entry
->vme_start
+ copy_size
);
6582 * Adjust to destination size
6585 if (size
< copy_size
) {
6586 vm_map_copy_clip_end(copy
, copy_entry
,
6587 copy_entry
->vme_start
+ size
);
6591 assert((entry
->vme_end
- entry
->vme_start
) == size
);
6592 assert((tmp_entry
->vme_end
- tmp_entry
->vme_start
) == size
);
6593 assert((copy_entry
->vme_end
- copy_entry
->vme_start
) == size
);
6596 * If the destination contains temporary unshared memory,
6597 * we can perform the copy by throwing it away and
6598 * installing the source data.
6601 object
= entry
->object
.vm_object
;
6602 if ((!entry
->is_shared
&&
6603 ((object
== VM_OBJECT_NULL
) ||
6604 (object
->internal
&& !object
->true_share
))) ||
6605 entry
->needs_copy
) {
6606 vm_object_t old_object
= entry
->object
.vm_object
;
6607 vm_object_offset_t old_offset
= entry
->offset
;
6608 vm_object_offset_t offset
;
6611 * Ensure that the source and destination aren't
6614 if (old_object
== copy_entry
->object
.vm_object
&&
6615 old_offset
== copy_entry
->offset
) {
6616 vm_map_copy_entry_unlink(copy
, copy_entry
);
6617 vm_map_copy_entry_dispose(copy
, copy_entry
);
6619 if (old_object
!= VM_OBJECT_NULL
)
6620 vm_object_deallocate(old_object
);
6622 start
= tmp_entry
->vme_end
;
6623 tmp_entry
= tmp_entry
->vme_next
;
6627 if (old_object
!= VM_OBJECT_NULL
) {
6628 if(entry
->is_sub_map
) {
6629 if(entry
->use_pmap
) {
6630 #ifndef NO_NESTED_PMAP
6631 pmap_unnest(dst_map
->pmap
,
6632 (addr64_t
)entry
->vme_start
,
6633 entry
->vme_end
- entry
->vme_start
);
6634 #endif /* NO_NESTED_PMAP */
6635 if(dst_map
->mapped
) {
6636 /* clean up parent */
6638 vm_map_submap_pmap_clean(
6639 dst_map
, entry
->vme_start
,
6641 entry
->object
.sub_map
,
6645 vm_map_submap_pmap_clean(
6646 dst_map
, entry
->vme_start
,
6648 entry
->object
.sub_map
,
6652 entry
->object
.sub_map
);
6654 if(dst_map
->mapped
) {
6655 vm_object_pmap_protect(
6656 entry
->object
.vm_object
,
6664 pmap_remove(dst_map
->pmap
,
6665 (addr64_t
)(entry
->vme_start
),
6666 (addr64_t
)(entry
->vme_end
));
6668 vm_object_deallocate(old_object
);
6672 entry
->is_sub_map
= FALSE
;
6673 entry
->object
= copy_entry
->object
;
6674 object
= entry
->object
.vm_object
;
6675 entry
->needs_copy
= copy_entry
->needs_copy
;
6676 entry
->wired_count
= 0;
6677 entry
->user_wired_count
= 0;
6678 offset
= entry
->offset
= copy_entry
->offset
;
6680 vm_map_copy_entry_unlink(copy
, copy_entry
);
6681 vm_map_copy_entry_dispose(copy
, copy_entry
);
6684 * we could try to push pages into the pmap at this point, BUT
6685 * this optimization only saved on average 2 us per page if ALL
6686 * the pages in the source were currently mapped
6687 * and ALL the pages in the dest were touched, if there were fewer
6688 * than 2/3 of the pages touched, this optimization actually cost more cycles
6689 * it also puts a lot of pressure on the pmap layer w/r to mapping structures
6693 * Set up for the next iteration. The map
6694 * has not been unlocked, so the next
6695 * address should be at the end of this
6696 * entry, and the next map entry should be
6697 * the one following it.
6700 start
= tmp_entry
->vme_end
;
6701 tmp_entry
= tmp_entry
->vme_next
;
6703 vm_map_version_t version
;
6704 vm_object_t dst_object
= entry
->object
.vm_object
;
6705 vm_object_offset_t dst_offset
= entry
->offset
;
6709 * Take an object reference, and record
6710 * the map version information so that the
6711 * map can be safely unlocked.
6714 vm_object_reference(dst_object
);
6716 /* account for unlock bumping up timestamp */
6717 version
.main_timestamp
= dst_map
->timestamp
+ 1;
6719 vm_map_unlock(dst_map
);
6722 * Copy as much as possible in one pass
6727 copy_entry
->object
.vm_object
,
6737 * Release the object reference
6740 vm_object_deallocate(dst_object
);
6743 * If a hard error occurred, return it now
6746 if (r
!= KERN_SUCCESS
)
6749 if (copy_size
!= 0) {
6751 * Dispose of the copied region
6754 vm_map_copy_clip_end(copy
, copy_entry
,
6755 copy_entry
->vme_start
+ copy_size
);
6756 vm_map_copy_entry_unlink(copy
, copy_entry
);
6757 vm_object_deallocate(copy_entry
->object
.vm_object
);
6758 vm_map_copy_entry_dispose(copy
, copy_entry
);
6762 * Pick up in the destination map where we left off.
6764 * Use the version information to avoid a lookup
6765 * in the normal case.
6769 vm_map_lock(dst_map
);
6770 if (version
.main_timestamp
== dst_map
->timestamp
) {
6771 /* We can safely use saved tmp_entry value */
6773 vm_map_clip_end(dst_map
, tmp_entry
, start
);
6774 tmp_entry
= tmp_entry
->vme_next
;
6776 /* Must do lookup of tmp_entry */
6778 if (!vm_map_lookup_entry(dst_map
, start
, &tmp_entry
)) {
6779 vm_map_unlock(dst_map
);
6780 return(KERN_INVALID_ADDRESS
);
6782 vm_map_clip_start(dst_map
, tmp_entry
, start
);
6787 return(KERN_SUCCESS
);
6788 }/* vm_map_copy_overwrite_aligned */
6791 * Routine: vm_map_copyin_kernel_buffer [internal use only]
6794 * Copy in data to a kernel buffer from space in the
6795 * source map. The original space may be optionally
6798 * If successful, returns a new copy object.
6800 static kern_return_t
6801 vm_map_copyin_kernel_buffer(
6803 vm_map_offset_t src_addr
,
6805 boolean_t src_destroy
,
6806 vm_map_copy_t
*copy_result
)
6810 vm_size_t kalloc_size
;
6812 if ((vm_size_t
) len
!= len
) {
6813 /* "len" is too big and doesn't fit in a "vm_size_t" */
6814 return KERN_RESOURCE_SHORTAGE
;
6816 kalloc_size
= (vm_size_t
) (sizeof(struct vm_map_copy
) + len
);
6817 assert((vm_map_size_t
) kalloc_size
== sizeof (struct vm_map_copy
) + len
);
6819 copy
= (vm_map_copy_t
) kalloc(kalloc_size
);
6820 if (copy
== VM_MAP_COPY_NULL
) {
6821 return KERN_RESOURCE_SHORTAGE
;
6823 copy
->type
= VM_MAP_COPY_KERNEL_BUFFER
;
6826 copy
->cpy_kdata
= (void *) (copy
+ 1);
6827 copy
->cpy_kalloc_size
= kalloc_size
;
6829 kr
= copyinmap(src_map
, src_addr
, copy
->cpy_kdata
, (vm_size_t
) len
);
6830 if (kr
!= KERN_SUCCESS
) {
6831 kfree(copy
, kalloc_size
);
6835 (void) vm_map_remove(src_map
, vm_map_trunc_page(src_addr
),
6836 vm_map_round_page(src_addr
+ len
),
6837 VM_MAP_REMOVE_INTERRUPTIBLE
|
6838 VM_MAP_REMOVE_WAIT_FOR_KWIRE
|
6839 (src_map
== kernel_map
) ?
6840 VM_MAP_REMOVE_KUNWIRE
: 0);
6842 *copy_result
= copy
;
6843 return KERN_SUCCESS
;
6847 * Routine: vm_map_copyout_kernel_buffer [internal use only]
6850 * Copy out data from a kernel buffer into space in the
6851 * destination map. The space may be otpionally dynamically
6854 * If successful, consumes the copy object.
6855 * Otherwise, the caller is responsible for it.
6857 static int vm_map_copyout_kernel_buffer_failures
= 0;
6858 static kern_return_t
6859 vm_map_copyout_kernel_buffer(
6861 vm_map_address_t
*addr
, /* IN/OUT */
6863 boolean_t overwrite
)
6865 kern_return_t kr
= KERN_SUCCESS
;
6866 thread_t thread
= current_thread();
6871 * Allocate space in the target map for the data
6874 kr
= vm_map_enter(map
,
6876 vm_map_round_page(copy
->size
),
6877 (vm_map_offset_t
) 0,
6880 (vm_object_offset_t
) 0,
6884 VM_INHERIT_DEFAULT
);
6885 if (kr
!= KERN_SUCCESS
)
6890 * Copyout the data from the kernel buffer to the target map.
6892 if (thread
->map
== map
) {
6895 * If the target map is the current map, just do
6898 assert((vm_size_t
) copy
->size
== copy
->size
);
6899 if (copyout(copy
->cpy_kdata
, *addr
, (vm_size_t
) copy
->size
)) {
6900 kr
= KERN_INVALID_ADDRESS
;
6907 * If the target map is another map, assume the
6908 * target's address space identity for the duration
6911 vm_map_reference(map
);
6912 oldmap
= vm_map_switch(map
);
6914 assert((vm_size_t
) copy
->size
== copy
->size
);
6915 if (copyout(copy
->cpy_kdata
, *addr
, (vm_size_t
) copy
->size
)) {
6916 vm_map_copyout_kernel_buffer_failures
++;
6917 kr
= KERN_INVALID_ADDRESS
;
6920 (void) vm_map_switch(oldmap
);
6921 vm_map_deallocate(map
);
6924 if (kr
!= KERN_SUCCESS
) {
6925 /* the copy failed, clean up */
6928 * Deallocate the space we allocated in the target map.
6930 (void) vm_map_remove(map
,
6931 vm_map_trunc_page(*addr
),
6932 vm_map_round_page(*addr
+
6933 vm_map_round_page(copy
->size
)),
6938 /* copy was successful, dicard the copy structure */
6939 kfree(copy
, copy
->cpy_kalloc_size
);
6946 * Macro: vm_map_copy_insert
6949 * Link a copy chain ("copy") into a map at the
6950 * specified location (after "where").
6952 * The copy chain is destroyed.
6954 * The arguments are evaluated multiple times.
6956 #define vm_map_copy_insert(map, where, copy) \
6958 vm_map_store_copy_insert(map, where, copy); \
6959 zfree(vm_map_copy_zone, copy); \
6963 * Routine: vm_map_copyout
6966 * Copy out a copy chain ("copy") into newly-allocated
6967 * space in the destination map.
6969 * If successful, consumes the copy object.
6970 * Otherwise, the caller is responsible for it.
6975 vm_map_address_t
*dst_addr
, /* OUT */
6979 vm_map_size_t adjustment
;
6980 vm_map_offset_t start
;
6981 vm_object_offset_t vm_copy_start
;
6982 vm_map_entry_t last
;
6984 vm_map_entry_t entry
;
6987 * Check for null copy object.
6990 if (copy
== VM_MAP_COPY_NULL
) {
6992 return(KERN_SUCCESS
);
6996 * Check for special copy object, created
6997 * by vm_map_copyin_object.
7000 if (copy
->type
== VM_MAP_COPY_OBJECT
) {
7001 vm_object_t object
= copy
->cpy_object
;
7003 vm_object_offset_t offset
;
7005 offset
= vm_object_trunc_page(copy
->offset
);
7006 size
= vm_map_round_page(copy
->size
+
7007 (vm_map_size_t
)(copy
->offset
- offset
));
7009 kr
= vm_map_enter(dst_map
, dst_addr
, size
,
7010 (vm_map_offset_t
) 0, VM_FLAGS_ANYWHERE
,
7011 object
, offset
, FALSE
,
7012 VM_PROT_DEFAULT
, VM_PROT_ALL
,
7013 VM_INHERIT_DEFAULT
);
7014 if (kr
!= KERN_SUCCESS
)
7016 /* Account for non-pagealigned copy object */
7017 *dst_addr
+= (vm_map_offset_t
)(copy
->offset
- offset
);
7018 zfree(vm_map_copy_zone
, copy
);
7019 return(KERN_SUCCESS
);
7023 * Check for special kernel buffer allocated
7024 * by new_ipc_kmsg_copyin.
7027 if (copy
->type
== VM_MAP_COPY_KERNEL_BUFFER
) {
7028 return(vm_map_copyout_kernel_buffer(dst_map
, dst_addr
,
7033 * Find space for the data
7036 vm_copy_start
= vm_object_trunc_page(copy
->offset
);
7037 size
= vm_map_round_page((vm_map_size_t
)copy
->offset
+ copy
->size
)
7042 vm_map_lock(dst_map
);
7043 if( dst_map
->disable_vmentry_reuse
== TRUE
) {
7044 VM_MAP_HIGHEST_ENTRY(dst_map
, entry
, start
);
7047 assert(first_free_is_valid(dst_map
));
7048 start
= ((last
= dst_map
->first_free
) == vm_map_to_entry(dst_map
)) ?
7049 vm_map_min(dst_map
) : last
->vme_end
;
7053 vm_map_entry_t next
= last
->vme_next
;
7054 vm_map_offset_t end
= start
+ size
;
7056 if ((end
> dst_map
->max_offset
) || (end
< start
)) {
7057 if (dst_map
->wait_for_space
) {
7058 if (size
<= (dst_map
->max_offset
- dst_map
->min_offset
)) {
7059 assert_wait((event_t
) dst_map
,
7060 THREAD_INTERRUPTIBLE
);
7061 vm_map_unlock(dst_map
);
7062 thread_block(THREAD_CONTINUE_NULL
);
7066 vm_map_unlock(dst_map
);
7067 return(KERN_NO_SPACE
);
7070 if ((next
== vm_map_to_entry(dst_map
)) ||
7071 (next
->vme_start
>= end
))
7075 start
= last
->vme_end
;
7079 * Since we're going to just drop the map
7080 * entries from the copy into the destination
7081 * map, they must come from the same pool.
7084 if (copy
->cpy_hdr
.entries_pageable
!= dst_map
->hdr
.entries_pageable
) {
7086 * Mismatches occur when dealing with the default
7090 vm_map_entry_t next
, new;
7093 * Find the zone that the copies were allocated from
7095 old_zone
= (copy
->cpy_hdr
.entries_pageable
)
7097 : vm_map_kentry_zone
;
7098 entry
= vm_map_copy_first_entry(copy
);
7101 * Reinitialize the copy so that vm_map_copy_entry_link
7104 vm_map_store_copy_reset(copy
, entry
);
7105 copy
->cpy_hdr
.entries_pageable
= dst_map
->hdr
.entries_pageable
;
7110 while (entry
!= vm_map_copy_to_entry(copy
)) {
7111 new = vm_map_copy_entry_create(copy
);
7112 vm_map_entry_copy_full(new, entry
);
7113 new->use_pmap
= FALSE
; /* clr address space specifics */
7114 vm_map_copy_entry_link(copy
,
7115 vm_map_copy_last_entry(copy
),
7117 next
= entry
->vme_next
;
7118 zfree(old_zone
, entry
);
7124 * Adjust the addresses in the copy chain, and
7125 * reset the region attributes.
7128 adjustment
= start
- vm_copy_start
;
7129 for (entry
= vm_map_copy_first_entry(copy
);
7130 entry
!= vm_map_copy_to_entry(copy
);
7131 entry
= entry
->vme_next
) {
7132 entry
->vme_start
+= adjustment
;
7133 entry
->vme_end
+= adjustment
;
7135 entry
->inheritance
= VM_INHERIT_DEFAULT
;
7136 entry
->protection
= VM_PROT_DEFAULT
;
7137 entry
->max_protection
= VM_PROT_ALL
;
7138 entry
->behavior
= VM_BEHAVIOR_DEFAULT
;
7141 * If the entry is now wired,
7142 * map the pages into the destination map.
7144 if (entry
->wired_count
!= 0) {
7145 register vm_map_offset_t va
;
7146 vm_object_offset_t offset
;
7147 register vm_object_t object
;
7151 object
= entry
->object
.vm_object
;
7152 offset
= entry
->offset
;
7153 va
= entry
->vme_start
;
7155 pmap_pageable(dst_map
->pmap
,
7160 while (va
< entry
->vme_end
) {
7161 register vm_page_t m
;
7164 * Look up the page in the object.
7165 * Assert that the page will be found in the
7168 * the object was newly created by
7169 * vm_object_copy_slowly, and has
7170 * copies of all of the pages from
7173 * the object was moved from the old
7174 * map entry; because the old map
7175 * entry was wired, all of the pages
7176 * were in the top-level object.
7177 * (XXX not true if we wire pages for
7180 vm_object_lock(object
);
7182 m
= vm_page_lookup(object
, offset
);
7183 if (m
== VM_PAGE_NULL
|| !VM_PAGE_WIRED(m
) ||
7185 panic("vm_map_copyout: wiring %p", m
);
7189 * The page is assumed to be wired here, so it
7190 * shouldn't be encrypted. Otherwise, we
7191 * couldn't enter it in the page table, since
7192 * we don't want the user to see the encrypted
7195 ASSERT_PAGE_DECRYPTED(m
);
7197 prot
= entry
->protection
;
7199 if (override_nx(dst_map
, entry
->alias
) && prot
)
7200 prot
|= VM_PROT_EXECUTE
;
7202 type_of_fault
= DBG_CACHE_HIT_FAULT
;
7204 vm_fault_enter(m
, dst_map
->pmap
, va
, prot
, prot
,
7205 VM_PAGE_WIRED(m
), FALSE
, FALSE
, FALSE
,
7208 vm_object_unlock(object
);
7210 offset
+= PAGE_SIZE_64
;
7217 * Correct the page alignment for the result
7220 *dst_addr
= start
+ (copy
->offset
- vm_copy_start
);
7223 * Update the hints and the map size
7226 SAVE_HINT_MAP_WRITE(dst_map
, vm_map_copy_last_entry(copy
));
7228 dst_map
->size
+= size
;
7234 vm_map_copy_insert(dst_map
, last
, copy
);
7236 vm_map_unlock(dst_map
);
7239 * XXX If wiring_required, call vm_map_pageable
7242 return(KERN_SUCCESS
);
7246 * Routine: vm_map_copyin
7249 * see vm_map_copyin_common. Exported via Unsupported.exports.
7253 #undef vm_map_copyin
7258 vm_map_address_t src_addr
,
7260 boolean_t src_destroy
,
7261 vm_map_copy_t
*copy_result
) /* OUT */
7263 return(vm_map_copyin_common(src_map
, src_addr
, len
, src_destroy
,
7264 FALSE
, copy_result
, FALSE
));
7268 * Routine: vm_map_copyin_common
7271 * Copy the specified region (src_addr, len) from the
7272 * source address space (src_map), possibly removing
7273 * the region from the source address space (src_destroy).
7276 * A vm_map_copy_t object (copy_result), suitable for
7277 * insertion into another address space (using vm_map_copyout),
7278 * copying over another address space region (using
7279 * vm_map_copy_overwrite). If the copy is unused, it
7280 * should be destroyed (using vm_map_copy_discard).
7282 * In/out conditions:
7283 * The source map should not be locked on entry.
7286 typedef struct submap_map
{
7287 vm_map_t parent_map
;
7288 vm_map_offset_t base_start
;
7289 vm_map_offset_t base_end
;
7290 vm_map_size_t base_len
;
7291 struct submap_map
*next
;
7295 vm_map_copyin_common(
7297 vm_map_address_t src_addr
,
7299 boolean_t src_destroy
,
7300 __unused boolean_t src_volatile
,
7301 vm_map_copy_t
*copy_result
, /* OUT */
7302 boolean_t use_maxprot
)
7304 vm_map_entry_t tmp_entry
; /* Result of last map lookup --
7305 * in multi-level lookup, this
7306 * entry contains the actual
7310 vm_map_entry_t new_entry
= VM_MAP_ENTRY_NULL
; /* Map entry for copy */
7312 vm_map_offset_t src_start
; /* Start of current entry --
7313 * where copy is taking place now
7315 vm_map_offset_t src_end
; /* End of entire region to be
7317 vm_map_offset_t src_base
;
7318 vm_map_t base_map
= src_map
;
7319 boolean_t map_share
=FALSE
;
7320 submap_map_t
*parent_maps
= NULL
;
7323 vm_map_copy_t copy
; /* Resulting copy */
7324 vm_map_address_t copy_addr
;
7327 * Check for copies of zero bytes.
7331 *copy_result
= VM_MAP_COPY_NULL
;
7332 return(KERN_SUCCESS
);
7336 * Check that the end address doesn't overflow
7338 src_end
= src_addr
+ len
;
7339 if (src_end
< src_addr
)
7340 return KERN_INVALID_ADDRESS
;
7343 * If the copy is sufficiently small, use a kernel buffer instead
7344 * of making a virtual copy. The theory being that the cost of
7345 * setting up VM (and taking C-O-W faults) dominates the copy costs
7346 * for small regions.
7348 if ((len
< msg_ool_size_small
) && !use_maxprot
)
7349 return vm_map_copyin_kernel_buffer(src_map
, src_addr
, len
,
7350 src_destroy
, copy_result
);
7353 * Compute (page aligned) start and end of region
7355 src_start
= vm_map_trunc_page(src_addr
);
7356 src_end
= vm_map_round_page(src_end
);
7358 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);
7361 * Allocate a header element for the list.
7363 * Use the start and end in the header to
7364 * remember the endpoints prior to rounding.
7367 copy
= (vm_map_copy_t
) zalloc(vm_map_copy_zone
);
7368 vm_map_copy_first_entry(copy
) =
7369 vm_map_copy_last_entry(copy
) = vm_map_copy_to_entry(copy
);
7370 copy
->type
= VM_MAP_COPY_ENTRY_LIST
;
7371 copy
->cpy_hdr
.nentries
= 0;
7372 copy
->cpy_hdr
.entries_pageable
= TRUE
;
7374 vm_map_store_init( &(copy
->cpy_hdr
) );
7376 copy
->offset
= src_addr
;
7379 new_entry
= vm_map_copy_entry_create(copy
);
7383 vm_map_unlock(src_map); \
7384 if(src_map != base_map) \
7385 vm_map_deallocate(src_map); \
7386 if (new_entry != VM_MAP_ENTRY_NULL) \
7387 vm_map_copy_entry_dispose(copy,new_entry); \
7388 vm_map_copy_discard(copy); \
7390 submap_map_t *_ptr; \
7392 for(_ptr = parent_maps; _ptr != NULL; _ptr = parent_maps) { \
7393 parent_maps=parent_maps->next; \
7394 if (_ptr->parent_map != base_map) \
7395 vm_map_deallocate(_ptr->parent_map); \
7396 kfree(_ptr, sizeof(submap_map_t)); \
7403 * Find the beginning of the region.
7406 vm_map_lock(src_map
);
7408 if (!vm_map_lookup_entry(src_map
, src_start
, &tmp_entry
))
7409 RETURN(KERN_INVALID_ADDRESS
);
7410 if(!tmp_entry
->is_sub_map
) {
7411 vm_map_clip_start(src_map
, tmp_entry
, src_start
);
7413 /* set for later submap fix-up */
7414 copy_addr
= src_start
;
7417 * Go through entries until we get to the end.
7422 vm_map_entry_t src_entry
= tmp_entry
; /* Top-level entry */
7423 vm_map_size_t src_size
; /* Size of source
7424 * map entry (in both
7429 vm_object_t src_object
; /* Object to copy */
7430 vm_object_offset_t src_offset
;
7432 boolean_t src_needs_copy
; /* Should source map
7434 * for copy-on-write?
7437 boolean_t new_entry_needs_copy
; /* Will new entry be COW? */
7439 boolean_t was_wired
; /* Was source wired? */
7440 vm_map_version_t version
; /* Version before locks
7441 * dropped to make copy
7443 kern_return_t result
; /* Return value from
7444 * copy_strategically.
7446 while(tmp_entry
->is_sub_map
) {
7447 vm_map_size_t submap_len
;
7450 ptr
= (submap_map_t
*)kalloc(sizeof(submap_map_t
));
7451 ptr
->next
= parent_maps
;
7453 ptr
->parent_map
= src_map
;
7454 ptr
->base_start
= src_start
;
7455 ptr
->base_end
= src_end
;
7456 submap_len
= tmp_entry
->vme_end
- src_start
;
7457 if(submap_len
> (src_end
-src_start
))
7458 submap_len
= src_end
-src_start
;
7459 ptr
->base_len
= submap_len
;
7461 src_start
-= tmp_entry
->vme_start
;
7462 src_start
+= tmp_entry
->offset
;
7463 src_end
= src_start
+ submap_len
;
7464 src_map
= tmp_entry
->object
.sub_map
;
7465 vm_map_lock(src_map
);
7466 /* keep an outstanding reference for all maps in */
7467 /* the parents tree except the base map */
7468 vm_map_reference(src_map
);
7469 vm_map_unlock(ptr
->parent_map
);
7470 if (!vm_map_lookup_entry(
7471 src_map
, src_start
, &tmp_entry
))
7472 RETURN(KERN_INVALID_ADDRESS
);
7474 if(!tmp_entry
->is_sub_map
)
7475 vm_map_clip_start(src_map
, tmp_entry
, src_start
);
7476 src_entry
= tmp_entry
;
7478 /* we are now in the lowest level submap... */
7480 if ((tmp_entry
->object
.vm_object
!= VM_OBJECT_NULL
) &&
7481 (tmp_entry
->object
.vm_object
->phys_contiguous
)) {
7482 /* This is not, supported for now.In future */
7483 /* we will need to detect the phys_contig */
7484 /* condition and then upgrade copy_slowly */
7485 /* to do physical copy from the device mem */
7486 /* based object. We can piggy-back off of */
7487 /* the was wired boolean to set-up the */
7488 /* proper handling */
7489 RETURN(KERN_PROTECTION_FAILURE
);
7492 * Create a new address map entry to hold the result.
7493 * Fill in the fields from the appropriate source entries.
7494 * We must unlock the source map to do this if we need
7495 * to allocate a map entry.
7497 if (new_entry
== VM_MAP_ENTRY_NULL
) {
7498 version
.main_timestamp
= src_map
->timestamp
;
7499 vm_map_unlock(src_map
);
7501 new_entry
= vm_map_copy_entry_create(copy
);
7503 vm_map_lock(src_map
);
7504 if ((version
.main_timestamp
+ 1) != src_map
->timestamp
) {
7505 if (!vm_map_lookup_entry(src_map
, src_start
,
7507 RETURN(KERN_INVALID_ADDRESS
);
7509 if (!tmp_entry
->is_sub_map
)
7510 vm_map_clip_start(src_map
, tmp_entry
, src_start
);
7511 continue; /* restart w/ new tmp_entry */
7516 * Verify that the region can be read.
7518 if (((src_entry
->protection
& VM_PROT_READ
) == VM_PROT_NONE
&&
7520 (src_entry
->max_protection
& VM_PROT_READ
) == 0)
7521 RETURN(KERN_PROTECTION_FAILURE
);
7524 * Clip against the endpoints of the entire region.
7527 vm_map_clip_end(src_map
, src_entry
, src_end
);
7529 src_size
= src_entry
->vme_end
- src_start
;
7530 src_object
= src_entry
->object
.vm_object
;
7531 src_offset
= src_entry
->offset
;
7532 was_wired
= (src_entry
->wired_count
!= 0);
7534 vm_map_entry_copy(new_entry
, src_entry
);
7535 new_entry
->use_pmap
= FALSE
; /* clr address space specifics */
7538 * Attempt non-blocking copy-on-write optimizations.
7542 (src_object
== VM_OBJECT_NULL
||
7543 (src_object
->internal
&& !src_object
->true_share
7546 * If we are destroying the source, and the object
7547 * is internal, we can move the object reference
7548 * from the source to the copy. The copy is
7549 * copy-on-write only if the source is.
7550 * We make another reference to the object, because
7551 * destroying the source entry will deallocate it.
7553 vm_object_reference(src_object
);
7556 * Copy is always unwired. vm_map_copy_entry
7557 * set its wired count to zero.
7560 goto CopySuccessful
;
7565 XPR(XPR_VM_MAP
, "vm_map_copyin_common src_obj 0x%x ent 0x%x obj 0x%x was_wired %d\n",
7566 src_object
, new_entry
, new_entry
->object
.vm_object
,
7568 if ((src_object
== VM_OBJECT_NULL
||
7569 (!was_wired
&& !map_share
&& !tmp_entry
->is_shared
)) &&
7570 vm_object_copy_quickly(
7571 &new_entry
->object
.vm_object
,
7575 &new_entry_needs_copy
)) {
7577 new_entry
->needs_copy
= new_entry_needs_copy
;
7580 * Handle copy-on-write obligations
7583 if (src_needs_copy
&& !tmp_entry
->needs_copy
) {
7586 prot
= src_entry
->protection
& ~VM_PROT_WRITE
;
7588 if (override_nx(src_map
, src_entry
->alias
) && prot
)
7589 prot
|= VM_PROT_EXECUTE
;
7591 vm_object_pmap_protect(
7595 (src_entry
->is_shared
?
7598 src_entry
->vme_start
,
7601 tmp_entry
->needs_copy
= TRUE
;
7605 * The map has never been unlocked, so it's safe
7606 * to move to the next entry rather than doing
7610 goto CopySuccessful
;
7614 * Take an object reference, so that we may
7615 * release the map lock(s).
7618 assert(src_object
!= VM_OBJECT_NULL
);
7619 vm_object_reference(src_object
);
7622 * Record the timestamp for later verification.
7626 version
.main_timestamp
= src_map
->timestamp
;
7627 vm_map_unlock(src_map
); /* Increments timestamp once! */
7635 vm_object_lock(src_object
);
7636 result
= vm_object_copy_slowly(
7641 &new_entry
->object
.vm_object
);
7642 new_entry
->offset
= 0;
7643 new_entry
->needs_copy
= FALSE
;
7646 else if (src_object
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
&&
7647 (tmp_entry
->is_shared
|| map_share
)) {
7648 vm_object_t new_object
;
7650 vm_object_lock_shared(src_object
);
7651 new_object
= vm_object_copy_delayed(
7656 if (new_object
== VM_OBJECT_NULL
)
7659 new_entry
->object
.vm_object
= new_object
;
7660 new_entry
->needs_copy
= TRUE
;
7661 result
= KERN_SUCCESS
;
7664 result
= vm_object_copy_strategically(src_object
,
7667 &new_entry
->object
.vm_object
,
7669 &new_entry_needs_copy
);
7671 new_entry
->needs_copy
= new_entry_needs_copy
;
7674 if (result
!= KERN_SUCCESS
&&
7675 result
!= KERN_MEMORY_RESTART_COPY
) {
7676 vm_map_lock(src_map
);
7681 * Throw away the extra reference
7684 vm_object_deallocate(src_object
);
7687 * Verify that the map has not substantially
7688 * changed while the copy was being made.
7691 vm_map_lock(src_map
);
7693 if ((version
.main_timestamp
+ 1) == src_map
->timestamp
)
7694 goto VerificationSuccessful
;
7697 * Simple version comparison failed.
7699 * Retry the lookup and verify that the
7700 * same object/offset are still present.
7702 * [Note: a memory manager that colludes with
7703 * the calling task can detect that we have
7704 * cheated. While the map was unlocked, the
7705 * mapping could have been changed and restored.]
7708 if (!vm_map_lookup_entry(src_map
, src_start
, &tmp_entry
)) {
7709 RETURN(KERN_INVALID_ADDRESS
);
7712 src_entry
= tmp_entry
;
7713 vm_map_clip_start(src_map
, src_entry
, src_start
);
7715 if ((((src_entry
->protection
& VM_PROT_READ
) == VM_PROT_NONE
) &&
7717 ((src_entry
->max_protection
& VM_PROT_READ
) == 0))
7718 goto VerificationFailed
;
7720 if (src_entry
->vme_end
< new_entry
->vme_end
)
7721 src_size
= (new_entry
->vme_end
= src_entry
->vme_end
) - src_start
;
7723 if ((src_entry
->object
.vm_object
!= src_object
) ||
7724 (src_entry
->offset
!= src_offset
) ) {
7727 * Verification failed.
7729 * Start over with this top-level entry.
7732 VerificationFailed
: ;
7734 vm_object_deallocate(new_entry
->object
.vm_object
);
7735 tmp_entry
= src_entry
;
7740 * Verification succeeded.
7743 VerificationSuccessful
: ;
7745 if (result
== KERN_MEMORY_RESTART_COPY
)
7755 * Link in the new copy entry.
7758 vm_map_copy_entry_link(copy
, vm_map_copy_last_entry(copy
),
7762 * Determine whether the entire region
7765 src_base
= src_start
;
7766 src_start
= new_entry
->vme_end
;
7767 new_entry
= VM_MAP_ENTRY_NULL
;
7768 while ((src_start
>= src_end
) && (src_end
!= 0)) {
7769 if (src_map
!= base_map
) {
7773 assert(ptr
!= NULL
);
7774 parent_maps
= parent_maps
->next
;
7776 /* fix up the damage we did in that submap */
7777 vm_map_simplify_range(src_map
,
7781 vm_map_unlock(src_map
);
7782 vm_map_deallocate(src_map
);
7783 vm_map_lock(ptr
->parent_map
);
7784 src_map
= ptr
->parent_map
;
7785 src_base
= ptr
->base_start
;
7786 src_start
= ptr
->base_start
+ ptr
->base_len
;
7787 src_end
= ptr
->base_end
;
7788 if ((src_end
> src_start
) &&
7789 !vm_map_lookup_entry(
7790 src_map
, src_start
, &tmp_entry
))
7791 RETURN(KERN_INVALID_ADDRESS
);
7792 kfree(ptr
, sizeof(submap_map_t
));
7793 if(parent_maps
== NULL
)
7795 src_entry
= tmp_entry
->vme_prev
;
7799 if ((src_start
>= src_end
) && (src_end
!= 0))
7803 * Verify that there are no gaps in the region
7806 tmp_entry
= src_entry
->vme_next
;
7807 if ((tmp_entry
->vme_start
!= src_start
) ||
7808 (tmp_entry
== vm_map_to_entry(src_map
)))
7809 RETURN(KERN_INVALID_ADDRESS
);
7813 * If the source should be destroyed, do it now, since the
7814 * copy was successful.
7817 (void) vm_map_delete(src_map
,
7818 vm_map_trunc_page(src_addr
),
7820 (src_map
== kernel_map
) ?
7821 VM_MAP_REMOVE_KUNWIRE
:
7825 /* fix up the damage we did in the base map */
7826 vm_map_simplify_range(src_map
,
7827 vm_map_trunc_page(src_addr
),
7828 vm_map_round_page(src_end
));
7831 vm_map_unlock(src_map
);
7833 /* Fix-up start and end points in copy. This is necessary */
7834 /* when the various entries in the copy object were picked */
7835 /* up from different sub-maps */
7837 tmp_entry
= vm_map_copy_first_entry(copy
);
7838 while (tmp_entry
!= vm_map_copy_to_entry(copy
)) {
7839 tmp_entry
->vme_end
= copy_addr
+
7840 (tmp_entry
->vme_end
- tmp_entry
->vme_start
);
7841 tmp_entry
->vme_start
= copy_addr
;
7842 copy_addr
+= tmp_entry
->vme_end
- tmp_entry
->vme_start
;
7843 tmp_entry
= (struct vm_map_entry
*)tmp_entry
->vme_next
;
7846 *copy_result
= copy
;
7847 return(KERN_SUCCESS
);
7853 * vm_map_copyin_object:
7855 * Create a copy object from an object.
7856 * Our caller donates an object reference.
7860 vm_map_copyin_object(
7862 vm_object_offset_t offset
, /* offset of region in object */
7863 vm_object_size_t size
, /* size of region in object */
7864 vm_map_copy_t
*copy_result
) /* OUT */
7866 vm_map_copy_t copy
; /* Resulting copy */
7869 * We drop the object into a special copy object
7870 * that contains the object directly.
7873 copy
= (vm_map_copy_t
) zalloc(vm_map_copy_zone
);
7874 copy
->type
= VM_MAP_COPY_OBJECT
;
7875 copy
->cpy_object
= object
;
7876 copy
->offset
= offset
;
7879 *copy_result
= copy
;
7880 return(KERN_SUCCESS
);
7886 vm_map_entry_t old_entry
,
7890 vm_map_entry_t new_entry
;
7893 * New sharing code. New map entry
7894 * references original object. Internal
7895 * objects use asynchronous copy algorithm for
7896 * future copies. First make sure we have
7897 * the right object. If we need a shadow,
7898 * or someone else already has one, then
7899 * make a new shadow and share it.
7902 object
= old_entry
->object
.vm_object
;
7903 if (old_entry
->is_sub_map
) {
7904 assert(old_entry
->wired_count
== 0);
7905 #ifndef NO_NESTED_PMAP
7906 if(old_entry
->use_pmap
) {
7907 kern_return_t result
;
7909 result
= pmap_nest(new_map
->pmap
,
7910 (old_entry
->object
.sub_map
)->pmap
,
7911 (addr64_t
)old_entry
->vme_start
,
7912 (addr64_t
)old_entry
->vme_start
,
7913 (uint64_t)(old_entry
->vme_end
- old_entry
->vme_start
));
7915 panic("vm_map_fork_share: pmap_nest failed!");
7917 #endif /* NO_NESTED_PMAP */
7918 } else if (object
== VM_OBJECT_NULL
) {
7919 object
= vm_object_allocate((vm_map_size_t
)(old_entry
->vme_end
-
7920 old_entry
->vme_start
));
7921 old_entry
->offset
= 0;
7922 old_entry
->object
.vm_object
= object
;
7923 assert(!old_entry
->needs_copy
);
7924 } else if (object
->copy_strategy
!=
7925 MEMORY_OBJECT_COPY_SYMMETRIC
) {
7928 * We are already using an asymmetric
7929 * copy, and therefore we already have
7933 assert(! old_entry
->needs_copy
);
7935 else if (old_entry
->needs_copy
|| /* case 1 */
7936 object
->shadowed
|| /* case 2 */
7937 (!object
->true_share
&& /* case 3 */
7938 !old_entry
->is_shared
&&
7940 (vm_map_size_t
)(old_entry
->vme_end
-
7941 old_entry
->vme_start
)))) {
7944 * We need to create a shadow.
7945 * There are three cases here.
7946 * In the first case, we need to
7947 * complete a deferred symmetrical
7948 * copy that we participated in.
7949 * In the second and third cases,
7950 * we need to create the shadow so
7951 * that changes that we make to the
7952 * object do not interfere with
7953 * any symmetrical copies which
7954 * have occured (case 2) or which
7955 * might occur (case 3).
7957 * The first case is when we had
7958 * deferred shadow object creation
7959 * via the entry->needs_copy mechanism.
7960 * This mechanism only works when
7961 * only one entry points to the source
7962 * object, and we are about to create
7963 * a second entry pointing to the
7964 * same object. The problem is that
7965 * there is no way of mapping from
7966 * an object to the entries pointing
7967 * to it. (Deferred shadow creation
7968 * works with one entry because occurs
7969 * at fault time, and we walk from the
7970 * entry to the object when handling
7973 * The second case is when the object
7974 * to be shared has already been copied
7975 * with a symmetric copy, but we point
7976 * directly to the object without
7977 * needs_copy set in our entry. (This
7978 * can happen because different ranges
7979 * of an object can be pointed to by
7980 * different entries. In particular,
7981 * a single entry pointing to an object
7982 * can be split by a call to vm_inherit,
7983 * which, combined with task_create, can
7984 * result in the different entries
7985 * having different needs_copy values.)
7986 * The shadowed flag in the object allows
7987 * us to detect this case. The problem
7988 * with this case is that if this object
7989 * has or will have shadows, then we
7990 * must not perform an asymmetric copy
7991 * of this object, since such a copy
7992 * allows the object to be changed, which
7993 * will break the previous symmetrical
7994 * copies (which rely upon the object
7995 * not changing). In a sense, the shadowed
7996 * flag says "don't change this object".
7997 * We fix this by creating a shadow
7998 * object for this object, and sharing
7999 * that. This works because we are free
8000 * to change the shadow object (and thus
8001 * to use an asymmetric copy strategy);
8002 * this is also semantically correct,
8003 * since this object is temporary, and
8004 * therefore a copy of the object is
8005 * as good as the object itself. (This
8006 * is not true for permanent objects,
8007 * since the pager needs to see changes,
8008 * which won't happen if the changes
8009 * are made to a copy.)
8011 * The third case is when the object
8012 * to be shared has parts sticking
8013 * outside of the entry we're working
8014 * with, and thus may in the future
8015 * be subject to a symmetrical copy.
8016 * (This is a preemptive version of
8019 vm_object_shadow(&old_entry
->object
.vm_object
,
8021 (vm_map_size_t
) (old_entry
->vme_end
-
8022 old_entry
->vme_start
));
8025 * If we're making a shadow for other than
8026 * copy on write reasons, then we have
8027 * to remove write permission.
8030 if (!old_entry
->needs_copy
&&
8031 (old_entry
->protection
& VM_PROT_WRITE
)) {
8034 prot
= old_entry
->protection
& ~VM_PROT_WRITE
;
8036 if (override_nx(old_map
, old_entry
->alias
) && prot
)
8037 prot
|= VM_PROT_EXECUTE
;
8039 if (old_map
->mapped
) {
8040 vm_object_pmap_protect(
8041 old_entry
->object
.vm_object
,
8043 (old_entry
->vme_end
-
8044 old_entry
->vme_start
),
8046 old_entry
->vme_start
,
8049 pmap_protect(old_map
->pmap
,
8050 old_entry
->vme_start
,
8056 old_entry
->needs_copy
= FALSE
;
8057 object
= old_entry
->object
.vm_object
;
8062 * If object was using a symmetric copy strategy,
8063 * change its copy strategy to the default
8064 * asymmetric copy strategy, which is copy_delay
8065 * in the non-norma case and copy_call in the
8066 * norma case. Bump the reference count for the
8070 if(old_entry
->is_sub_map
) {
8071 vm_map_lock(old_entry
->object
.sub_map
);
8072 vm_map_reference(old_entry
->object
.sub_map
);
8073 vm_map_unlock(old_entry
->object
.sub_map
);
8075 vm_object_lock(object
);
8076 vm_object_reference_locked(object
);
8077 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
) {
8078 object
->copy_strategy
= MEMORY_OBJECT_COPY_DELAY
;
8080 vm_object_unlock(object
);
8084 * Clone the entry, using object ref from above.
8085 * Mark both entries as shared.
8088 new_entry
= vm_map_entry_create(new_map
);
8089 vm_map_entry_copy(new_entry
, old_entry
);
8090 old_entry
->is_shared
= TRUE
;
8091 new_entry
->is_shared
= TRUE
;
8094 * Insert the entry into the new map -- we
8095 * know we're inserting at the end of the new
8099 vm_map_store_entry_link(new_map
, vm_map_last_entry(new_map
), new_entry
);
8102 * Update the physical map
8105 if (old_entry
->is_sub_map
) {
8106 /* Bill Angell pmap support goes here */
8108 pmap_copy(new_map
->pmap
, old_map
->pmap
, new_entry
->vme_start
,
8109 old_entry
->vme_end
- old_entry
->vme_start
,
8110 old_entry
->vme_start
);
8117 vm_map_entry_t
*old_entry_p
,
8120 vm_map_entry_t old_entry
= *old_entry_p
;
8121 vm_map_size_t entry_size
= old_entry
->vme_end
- old_entry
->vme_start
;
8122 vm_map_offset_t start
= old_entry
->vme_start
;
8124 vm_map_entry_t last
= vm_map_last_entry(new_map
);
8126 vm_map_unlock(old_map
);
8128 * Use maxprot version of copyin because we
8129 * care about whether this memory can ever
8130 * be accessed, not just whether it's accessible
8133 if (vm_map_copyin_maxprot(old_map
, start
, entry_size
, FALSE
, ©
)
8136 * The map might have changed while it
8137 * was unlocked, check it again. Skip
8138 * any blank space or permanently
8139 * unreadable region.
8141 vm_map_lock(old_map
);
8142 if (!vm_map_lookup_entry(old_map
, start
, &last
) ||
8143 (last
->max_protection
& VM_PROT_READ
) == VM_PROT_NONE
) {
8144 last
= last
->vme_next
;
8146 *old_entry_p
= last
;
8149 * XXX For some error returns, want to
8150 * XXX skip to the next element. Note
8151 * that INVALID_ADDRESS and
8152 * PROTECTION_FAILURE are handled above.
8159 * Insert the copy into the new map
8162 vm_map_copy_insert(new_map
, last
, copy
);
8165 * Pick up the traversal at the end of
8166 * the copied region.
8169 vm_map_lock(old_map
);
8170 start
+= entry_size
;
8171 if (! vm_map_lookup_entry(old_map
, start
, &last
)) {
8172 last
= last
->vme_next
;
8174 if (last
->vme_start
== start
) {
8176 * No need to clip here and we don't
8177 * want to cause any unnecessary
8181 vm_map_clip_start(old_map
, last
, start
);
8184 *old_entry_p
= last
;
8192 * Create and return a new map based on the old
8193 * map, according to the inheritance values on the
8194 * regions in that map.
8196 * The source map must not be locked.
8204 vm_map_entry_t old_entry
;
8205 vm_map_size_t new_size
= 0, entry_size
;
8206 vm_map_entry_t new_entry
;
8207 boolean_t src_needs_copy
;
8208 boolean_t new_entry_needs_copy
;
8210 new_pmap
= pmap_create((vm_map_size_t
) 0,
8211 #if defined(__i386__) || defined(__x86_64__)
8212 old_map
->pmap
->pm_task_map
!= TASK_MAP_32BIT
8217 #if defined(__i386__)
8218 if (old_map
->pmap
->pm_task_map
== TASK_MAP_64BIT_SHARED
)
8219 pmap_set_4GB_pagezero(new_pmap
);
8222 vm_map_reference_swap(old_map
);
8223 vm_map_lock(old_map
);
8225 new_map
= vm_map_create(new_pmap
,
8226 old_map
->min_offset
,
8227 old_map
->max_offset
,
8228 old_map
->hdr
.entries_pageable
);
8230 old_entry
= vm_map_first_entry(old_map
);
8231 old_entry
!= vm_map_to_entry(old_map
);
8234 entry_size
= old_entry
->vme_end
- old_entry
->vme_start
;
8236 switch (old_entry
->inheritance
) {
8237 case VM_INHERIT_NONE
:
8240 case VM_INHERIT_SHARE
:
8241 vm_map_fork_share(old_map
, old_entry
, new_map
);
8242 new_size
+= entry_size
;
8245 case VM_INHERIT_COPY
:
8248 * Inline the copy_quickly case;
8249 * upon failure, fall back on call
8250 * to vm_map_fork_copy.
8253 if(old_entry
->is_sub_map
)
8255 if ((old_entry
->wired_count
!= 0) ||
8256 ((old_entry
->object
.vm_object
!= NULL
) &&
8257 (old_entry
->object
.vm_object
->true_share
))) {
8258 goto slow_vm_map_fork_copy
;
8261 new_entry
= vm_map_entry_create(new_map
);
8262 vm_map_entry_copy(new_entry
, old_entry
);
8263 /* clear address space specifics */
8264 new_entry
->use_pmap
= FALSE
;
8266 if (! vm_object_copy_quickly(
8267 &new_entry
->object
.vm_object
,
8269 (old_entry
->vme_end
-
8270 old_entry
->vme_start
),
8272 &new_entry_needs_copy
)) {
8273 vm_map_entry_dispose(new_map
, new_entry
);
8274 goto slow_vm_map_fork_copy
;
8278 * Handle copy-on-write obligations
8281 if (src_needs_copy
&& !old_entry
->needs_copy
) {
8284 prot
= old_entry
->protection
& ~VM_PROT_WRITE
;
8286 if (override_nx(old_map
, old_entry
->alias
) && prot
)
8287 prot
|= VM_PROT_EXECUTE
;
8289 vm_object_pmap_protect(
8290 old_entry
->object
.vm_object
,
8292 (old_entry
->vme_end
-
8293 old_entry
->vme_start
),
8294 ((old_entry
->is_shared
8298 old_entry
->vme_start
,
8301 old_entry
->needs_copy
= TRUE
;
8303 new_entry
->needs_copy
= new_entry_needs_copy
;
8306 * Insert the entry at the end
8310 vm_map_store_entry_link(new_map
, vm_map_last_entry(new_map
),
8312 new_size
+= entry_size
;
8315 slow_vm_map_fork_copy
:
8316 if (vm_map_fork_copy(old_map
, &old_entry
, new_map
)) {
8317 new_size
+= entry_size
;
8321 old_entry
= old_entry
->vme_next
;
8324 new_map
->size
= new_size
;
8325 vm_map_unlock(old_map
);
8326 vm_map_deallocate(old_map
);
8334 * Setup the "new_map" with the proper execution environment according
8335 * to the type of executable (platform, 64bit, chroot environment).
8336 * Map the comm page and shared region, etc...
8345 SHARED_REGION_TRACE_DEBUG(
8346 ("shared_region: task %p: vm_map_exec(%p,%p,%p,0x%x): ->\n",
8347 current_task(), new_map
, task
, fsroot
, cpu
));
8348 (void) vm_commpage_enter(new_map
, task
);
8349 (void) vm_shared_region_enter(new_map
, task
, fsroot
, cpu
);
8350 SHARED_REGION_TRACE_DEBUG(
8351 ("shared_region: task %p: vm_map_exec(%p,%p,%p,0x%x): <-\n",
8352 current_task(), new_map
, task
, fsroot
, cpu
));
8353 return KERN_SUCCESS
;
8357 * vm_map_lookup_locked:
8359 * Finds the VM object, offset, and
8360 * protection for a given virtual address in the
8361 * specified map, assuming a page fault of the
8364 * Returns the (object, offset, protection) for
8365 * this address, whether it is wired down, and whether
8366 * this map has the only reference to the data in question.
8367 * In order to later verify this lookup, a "version"
8370 * The map MUST be locked by the caller and WILL be
8371 * locked on exit. In order to guarantee the
8372 * existence of the returned object, it is returned
8375 * If a lookup is requested with "write protection"
8376 * specified, the map may be changed to perform virtual
8377 * copying operations, although the data referenced will
8381 vm_map_lookup_locked(
8382 vm_map_t
*var_map
, /* IN/OUT */
8383 vm_map_offset_t vaddr
,
8384 vm_prot_t fault_type
,
8385 int object_lock_type
,
8386 vm_map_version_t
*out_version
, /* OUT */
8387 vm_object_t
*object
, /* OUT */
8388 vm_object_offset_t
*offset
, /* OUT */
8389 vm_prot_t
*out_prot
, /* OUT */
8390 boolean_t
*wired
, /* OUT */
8391 vm_object_fault_info_t fault_info
, /* OUT */
8394 vm_map_entry_t entry
;
8395 register vm_map_t map
= *var_map
;
8396 vm_map_t old_map
= *var_map
;
8397 vm_map_t cow_sub_map_parent
= VM_MAP_NULL
;
8398 vm_map_offset_t cow_parent_vaddr
= 0;
8399 vm_map_offset_t old_start
= 0;
8400 vm_map_offset_t old_end
= 0;
8401 register vm_prot_t prot
;
8402 boolean_t mask_protections
;
8403 vm_prot_t original_fault_type
;
8406 * VM_PROT_MASK means that the caller wants us to use "fault_type"
8407 * as a mask against the mapping's actual protections, not as an
8410 mask_protections
= (fault_type
& VM_PROT_IS_MASK
) ? TRUE
: FALSE
;
8411 fault_type
&= ~VM_PROT_IS_MASK
;
8412 original_fault_type
= fault_type
;
8417 fault_type
= original_fault_type
;
8420 * If the map has an interesting hint, try it before calling
8421 * full blown lookup routine.
8425 if ((entry
== vm_map_to_entry(map
)) ||
8426 (vaddr
< entry
->vme_start
) || (vaddr
>= entry
->vme_end
)) {
8427 vm_map_entry_t tmp_entry
;
8430 * Entry was either not a valid hint, or the vaddr
8431 * was not contained in the entry, so do a full lookup.
8433 if (!vm_map_lookup_entry(map
, vaddr
, &tmp_entry
)) {
8434 if((cow_sub_map_parent
) && (cow_sub_map_parent
!= map
))
8435 vm_map_unlock(cow_sub_map_parent
);
8436 if((*real_map
!= map
)
8437 && (*real_map
!= cow_sub_map_parent
))
8438 vm_map_unlock(*real_map
);
8439 return KERN_INVALID_ADDRESS
;
8444 if(map
== old_map
) {
8445 old_start
= entry
->vme_start
;
8446 old_end
= entry
->vme_end
;
8450 * Handle submaps. Drop lock on upper map, submap is
8455 if (entry
->is_sub_map
) {
8456 vm_map_offset_t local_vaddr
;
8457 vm_map_offset_t end_delta
;
8458 vm_map_offset_t start_delta
;
8459 vm_map_entry_t submap_entry
;
8460 boolean_t mapped_needs_copy
=FALSE
;
8462 local_vaddr
= vaddr
;
8464 if ((entry
->use_pmap
&& !(fault_type
& VM_PROT_WRITE
))) {
8465 /* if real_map equals map we unlock below */
8466 if ((*real_map
!= map
) &&
8467 (*real_map
!= cow_sub_map_parent
))
8468 vm_map_unlock(*real_map
);
8469 *real_map
= entry
->object
.sub_map
;
8472 if(entry
->needs_copy
&& (fault_type
& VM_PROT_WRITE
)) {
8473 if (!mapped_needs_copy
) {
8474 if (vm_map_lock_read_to_write(map
)) {
8475 vm_map_lock_read(map
);
8476 /* XXX FBDP: entry still valid ? */
8477 if(*real_map
== entry
->object
.sub_map
)
8481 vm_map_lock_read(entry
->object
.sub_map
);
8482 cow_sub_map_parent
= map
;
8483 /* reset base to map before cow object */
8484 /* this is the map which will accept */
8485 /* the new cow object */
8486 old_start
= entry
->vme_start
;
8487 old_end
= entry
->vme_end
;
8488 cow_parent_vaddr
= vaddr
;
8489 mapped_needs_copy
= TRUE
;
8491 vm_map_lock_read(entry
->object
.sub_map
);
8492 if((cow_sub_map_parent
!= map
) &&
8497 vm_map_lock_read(entry
->object
.sub_map
);
8498 /* leave map locked if it is a target */
8499 /* cow sub_map above otherwise, just */
8500 /* follow the maps down to the object */
8501 /* here we unlock knowing we are not */
8502 /* revisiting the map. */
8503 if((*real_map
!= map
) && (map
!= cow_sub_map_parent
))
8504 vm_map_unlock_read(map
);
8507 /* XXX FBDP: map has been unlocked, what protects "entry" !? */
8508 *var_map
= map
= entry
->object
.sub_map
;
8510 /* calculate the offset in the submap for vaddr */
8511 local_vaddr
= (local_vaddr
- entry
->vme_start
) + entry
->offset
;
8514 if(!vm_map_lookup_entry(map
, local_vaddr
, &submap_entry
)) {
8515 if((cow_sub_map_parent
) && (cow_sub_map_parent
!= map
)){
8516 vm_map_unlock(cow_sub_map_parent
);
8518 if((*real_map
!= map
)
8519 && (*real_map
!= cow_sub_map_parent
)) {
8520 vm_map_unlock(*real_map
);
8523 return KERN_INVALID_ADDRESS
;
8526 /* find the attenuated shadow of the underlying object */
8527 /* on our target map */
8529 /* in english the submap object may extend beyond the */
8530 /* region mapped by the entry or, may only fill a portion */
8531 /* of it. For our purposes, we only care if the object */
8532 /* doesn't fill. In this case the area which will */
8533 /* ultimately be clipped in the top map will only need */
8534 /* to be as big as the portion of the underlying entry */
8535 /* which is mapped */
8536 start_delta
= submap_entry
->vme_start
> entry
->offset
?
8537 submap_entry
->vme_start
- entry
->offset
: 0;
8540 (entry
->offset
+ start_delta
+ (old_end
- old_start
)) <=
8541 submap_entry
->vme_end
?
8542 0 : (entry
->offset
+
8543 (old_end
- old_start
))
8544 - submap_entry
->vme_end
;
8546 old_start
+= start_delta
;
8547 old_end
-= end_delta
;
8549 if(submap_entry
->is_sub_map
) {
8550 entry
= submap_entry
;
8551 vaddr
= local_vaddr
;
8552 goto submap_recurse
;
8555 if(((fault_type
& VM_PROT_WRITE
) && cow_sub_map_parent
)) {
8557 vm_object_t sub_object
, copy_object
;
8558 vm_object_offset_t copy_offset
;
8559 vm_map_offset_t local_start
;
8560 vm_map_offset_t local_end
;
8561 boolean_t copied_slowly
= FALSE
;
8563 if (vm_map_lock_read_to_write(map
)) {
8564 vm_map_lock_read(map
);
8565 old_start
-= start_delta
;
8566 old_end
+= end_delta
;
8571 sub_object
= submap_entry
->object
.vm_object
;
8572 if (sub_object
== VM_OBJECT_NULL
) {
8576 (submap_entry
->vme_end
-
8577 submap_entry
->vme_start
));
8578 submap_entry
->object
.vm_object
= sub_object
;
8579 submap_entry
->offset
= 0;
8581 local_start
= local_vaddr
-
8582 (cow_parent_vaddr
- old_start
);
8583 local_end
= local_vaddr
+
8584 (old_end
- cow_parent_vaddr
);
8585 vm_map_clip_start(map
, submap_entry
, local_start
);
8586 vm_map_clip_end(map
, submap_entry
, local_end
);
8587 /* unnesting was done in vm_map_clip_start/end() */
8588 assert(!submap_entry
->use_pmap
);
8590 /* This is the COW case, lets connect */
8591 /* an entry in our space to the underlying */
8592 /* object in the submap, bypassing the */
8596 if(submap_entry
->wired_count
!= 0 ||
8597 (sub_object
->copy_strategy
==
8598 MEMORY_OBJECT_COPY_NONE
)) {
8599 vm_object_lock(sub_object
);
8600 vm_object_copy_slowly(sub_object
,
8601 submap_entry
->offset
,
8602 (submap_entry
->vme_end
-
8603 submap_entry
->vme_start
),
8606 copied_slowly
= TRUE
;
8609 /* set up shadow object */
8610 copy_object
= sub_object
;
8611 vm_object_reference(copy_object
);
8612 sub_object
->shadowed
= TRUE
;
8613 submap_entry
->needs_copy
= TRUE
;
8615 prot
= submap_entry
->protection
& ~VM_PROT_WRITE
;
8617 if (override_nx(map
, submap_entry
->alias
) && prot
)
8618 prot
|= VM_PROT_EXECUTE
;
8620 vm_object_pmap_protect(
8622 submap_entry
->offset
,
8623 submap_entry
->vme_end
-
8624 submap_entry
->vme_start
,
8625 (submap_entry
->is_shared
8627 PMAP_NULL
: map
->pmap
,
8628 submap_entry
->vme_start
,
8633 * Adjust the fault offset to the submap entry.
8635 copy_offset
= (local_vaddr
-
8636 submap_entry
->vme_start
+
8637 submap_entry
->offset
);
8639 /* This works diffently than the */
8640 /* normal submap case. We go back */
8641 /* to the parent of the cow map and*/
8642 /* clip out the target portion of */
8643 /* the sub_map, substituting the */
8644 /* new copy object, */
8647 local_start
= old_start
;
8648 local_end
= old_end
;
8649 map
= cow_sub_map_parent
;
8650 *var_map
= cow_sub_map_parent
;
8651 vaddr
= cow_parent_vaddr
;
8652 cow_sub_map_parent
= NULL
;
8654 if(!vm_map_lookup_entry(map
,
8656 vm_object_deallocate(
8658 vm_map_lock_write_to_read(map
);
8659 return KERN_INVALID_ADDRESS
;
8662 /* clip out the portion of space */
8663 /* mapped by the sub map which */
8664 /* corresponds to the underlying */
8668 * Clip (and unnest) the smallest nested chunk
8669 * possible around the faulting address...
8671 local_start
= vaddr
& ~(pmap_nesting_size_min
- 1);
8672 local_end
= local_start
+ pmap_nesting_size_min
;
8674 * ... but don't go beyond the "old_start" to "old_end"
8675 * range, to avoid spanning over another VM region
8676 * with a possibly different VM object and/or offset.
8678 if (local_start
< old_start
) {
8679 local_start
= old_start
;
8681 if (local_end
> old_end
) {
8682 local_end
= old_end
;
8685 * Adjust copy_offset to the start of the range.
8687 copy_offset
-= (vaddr
- local_start
);
8689 vm_map_clip_start(map
, entry
, local_start
);
8690 vm_map_clip_end(map
, entry
, local_end
);
8691 /* unnesting was done in vm_map_clip_start/end() */
8692 assert(!entry
->use_pmap
);
8694 /* substitute copy object for */
8695 /* shared map entry */
8696 vm_map_deallocate(entry
->object
.sub_map
);
8697 entry
->is_sub_map
= FALSE
;
8698 entry
->object
.vm_object
= copy_object
;
8700 /* propagate the submap entry's protections */
8701 entry
->protection
|= submap_entry
->protection
;
8702 entry
->max_protection
|= submap_entry
->max_protection
;
8705 entry
->offset
= local_start
- old_start
;
8706 entry
->needs_copy
= FALSE
;
8707 entry
->is_shared
= FALSE
;
8709 entry
->offset
= copy_offset
;
8710 entry
->needs_copy
= TRUE
;
8711 if(entry
->inheritance
== VM_INHERIT_SHARE
)
8712 entry
->inheritance
= VM_INHERIT_COPY
;
8714 entry
->is_shared
= TRUE
;
8716 if(entry
->inheritance
== VM_INHERIT_SHARE
)
8717 entry
->inheritance
= VM_INHERIT_COPY
;
8719 vm_map_lock_write_to_read(map
);
8721 if((cow_sub_map_parent
)
8722 && (cow_sub_map_parent
!= *real_map
)
8723 && (cow_sub_map_parent
!= map
)) {
8724 vm_map_unlock(cow_sub_map_parent
);
8726 entry
= submap_entry
;
8727 vaddr
= local_vaddr
;
8732 * Check whether this task is allowed to have
8736 prot
= entry
->protection
;
8738 if (override_nx(map
, entry
->alias
) && prot
) {
8740 * HACK -- if not a stack, then allow execution
8742 prot
|= VM_PROT_EXECUTE
;
8745 if (mask_protections
) {
8747 if (fault_type
== VM_PROT_NONE
) {
8748 goto protection_failure
;
8751 if ((fault_type
& (prot
)) != fault_type
) {
8753 if (*real_map
!= map
) {
8754 vm_map_unlock(*real_map
);
8758 if ((fault_type
& VM_PROT_EXECUTE
) && prot
)
8759 log_stack_execution_failure((addr64_t
)vaddr
, prot
);
8761 DTRACE_VM2(prot_fault
, int, 1, (uint64_t *), NULL
);
8762 return KERN_PROTECTION_FAILURE
;
8766 * If this page is not pageable, we have to get
8767 * it for all possible accesses.
8770 *wired
= (entry
->wired_count
!= 0);
8775 * If the entry was copy-on-write, we either ...
8778 if (entry
->needs_copy
) {
8780 * If we want to write the page, we may as well
8781 * handle that now since we've got the map locked.
8783 * If we don't need to write the page, we just
8784 * demote the permissions allowed.
8787 if ((fault_type
& VM_PROT_WRITE
) || *wired
) {
8789 * Make a new object, and place it in the
8790 * object chain. Note that no new references
8791 * have appeared -- one just moved from the
8792 * map to the new object.
8795 if (vm_map_lock_read_to_write(map
)) {
8796 vm_map_lock_read(map
);
8799 vm_object_shadow(&entry
->object
.vm_object
,
8801 (vm_map_size_t
) (entry
->vme_end
-
8804 entry
->object
.vm_object
->shadowed
= TRUE
;
8805 entry
->needs_copy
= FALSE
;
8806 vm_map_lock_write_to_read(map
);
8810 * We're attempting to read a copy-on-write
8811 * page -- don't allow writes.
8814 prot
&= (~VM_PROT_WRITE
);
8819 * Create an object if necessary.
8821 if (entry
->object
.vm_object
== VM_OBJECT_NULL
) {
8823 if (vm_map_lock_read_to_write(map
)) {
8824 vm_map_lock_read(map
);
8828 entry
->object
.vm_object
= vm_object_allocate(
8829 (vm_map_size_t
)(entry
->vme_end
- entry
->vme_start
));
8831 vm_map_lock_write_to_read(map
);
8835 * Return the object/offset from this entry. If the entry
8836 * was copy-on-write or empty, it has been fixed up. Also
8837 * return the protection.
8840 *offset
= (vaddr
- entry
->vme_start
) + entry
->offset
;
8841 *object
= entry
->object
.vm_object
;
8845 fault_info
->interruptible
= THREAD_UNINT
; /* for now... */
8846 /* ... the caller will change "interruptible" if needed */
8847 fault_info
->cluster_size
= 0;
8848 fault_info
->user_tag
= entry
->alias
;
8849 fault_info
->behavior
= entry
->behavior
;
8850 fault_info
->lo_offset
= entry
->offset
;
8851 fault_info
->hi_offset
= (entry
->vme_end
- entry
->vme_start
) + entry
->offset
;
8852 fault_info
->no_cache
= entry
->no_cache
;
8853 fault_info
->stealth
= FALSE
;
8854 fault_info
->io_sync
= FALSE
;
8855 fault_info
->cs_bypass
= (entry
->used_for_jit
)? TRUE
: FALSE
;
8856 fault_info
->mark_zf_absent
= FALSE
;
8860 * Lock the object to prevent it from disappearing
8862 if (object_lock_type
== OBJECT_LOCK_EXCLUSIVE
)
8863 vm_object_lock(*object
);
8865 vm_object_lock_shared(*object
);
8868 * Save the version number
8871 out_version
->main_timestamp
= map
->timestamp
;
8873 return KERN_SUCCESS
;
8880 * Verifies that the map in question has not changed
8881 * since the given version. If successful, the map
8882 * will not change until vm_map_verify_done() is called.
8886 register vm_map_t map
,
8887 register vm_map_version_t
*version
) /* REF */
8891 vm_map_lock_read(map
);
8892 result
= (map
->timestamp
== version
->main_timestamp
);
8895 vm_map_unlock_read(map
);
8901 * vm_map_verify_done:
8903 * Releases locks acquired by a vm_map_verify.
8905 * This is now a macro in vm/vm_map.h. It does a
8906 * vm_map_unlock_read on the map.
8911 * TEMPORARYTEMPORARYTEMPORARYTEMPORARYTEMPORARYTEMPORARY
8912 * Goes away after regular vm_region_recurse function migrates to
8914 * vm_region_recurse: A form of vm_region which follows the
8915 * submaps in a target map
8920 vm_map_region_recurse_64(
8922 vm_map_offset_t
*address
, /* IN/OUT */
8923 vm_map_size_t
*size
, /* OUT */
8924 natural_t
*nesting_depth
, /* IN/OUT */
8925 vm_region_submap_info_64_t submap_info
, /* IN/OUT */
8926 mach_msg_type_number_t
*count
) /* IN/OUT */
8928 vm_region_extended_info_data_t extended
;
8929 vm_map_entry_t tmp_entry
;
8930 vm_map_offset_t user_address
;
8931 unsigned int user_max_depth
;
8934 * "curr_entry" is the VM map entry preceding or including the
8935 * address we're looking for.
8936 * "curr_map" is the map or sub-map containing "curr_entry".
8937 * "curr_address" is the equivalent of the top map's "user_address"
8938 * in the current map.
8939 * "curr_offset" is the cumulated offset of "curr_map" in the
8940 * target task's address space.
8941 * "curr_depth" is the depth of "curr_map" in the chain of
8944 * "curr_max_below" and "curr_max_above" limit the range (around
8945 * "curr_address") we should take into account in the current (sub)map.
8946 * They limit the range to what's visible through the map entries
8947 * we've traversed from the top map to the current map.
8950 vm_map_entry_t curr_entry
;
8951 vm_map_address_t curr_address
;
8952 vm_map_offset_t curr_offset
;
8954 unsigned int curr_depth
;
8955 vm_map_offset_t curr_max_below
, curr_max_above
;
8956 vm_map_offset_t curr_skip
;
8959 * "next_" is the same as "curr_" but for the VM region immediately
8960 * after the address we're looking for. We need to keep track of this
8961 * too because we want to return info about that region if the
8962 * address we're looking for is not mapped.
8964 vm_map_entry_t next_entry
;
8965 vm_map_offset_t next_offset
;
8966 vm_map_offset_t next_address
;
8968 unsigned int next_depth
;
8969 vm_map_offset_t next_max_below
, next_max_above
;
8970 vm_map_offset_t next_skip
;
8972 boolean_t look_for_pages
;
8973 vm_region_submap_short_info_64_t short_info
;
8975 if (map
== VM_MAP_NULL
) {
8976 /* no address space to work on */
8977 return KERN_INVALID_ARGUMENT
;
8980 if (*count
< VM_REGION_SUBMAP_INFO_COUNT_64
) {
8981 if (*count
< VM_REGION_SUBMAP_SHORT_INFO_COUNT_64
) {
8983 * "info" structure is not big enough and
8986 return KERN_INVALID_ARGUMENT
;
8988 look_for_pages
= FALSE
;
8989 *count
= VM_REGION_SUBMAP_SHORT_INFO_COUNT_64
;
8990 short_info
= (vm_region_submap_short_info_64_t
) submap_info
;
8994 look_for_pages
= TRUE
;
8995 *count
= VM_REGION_SUBMAP_INFO_COUNT_64
;
9000 user_address
= *address
;
9001 user_max_depth
= *nesting_depth
;
9005 curr_address
= user_address
;
9009 curr_max_above
= ((vm_map_offset_t
) -1) - curr_address
;
9010 curr_max_below
= curr_address
;
9018 next_max_above
= (vm_map_offset_t
) -1;
9019 next_max_below
= (vm_map_offset_t
) -1;
9022 vm_map_lock_read(curr_map
);
9026 if (vm_map_lookup_entry(curr_map
,
9029 /* tmp_entry contains the address we're looking for */
9030 curr_entry
= tmp_entry
;
9032 vm_map_offset_t skip
;
9034 * The address is not mapped. "tmp_entry" is the
9035 * map entry preceding the address. We want the next
9036 * one, if it exists.
9038 curr_entry
= tmp_entry
->vme_next
;
9040 if (curr_entry
== vm_map_to_entry(curr_map
) ||
9041 (curr_entry
->vme_start
>=
9042 curr_address
+ curr_max_above
)) {
9043 /* no next entry at this level: stop looking */
9045 vm_map_unlock_read(curr_map
);
9056 /* adjust current address and offset */
9057 skip
= curr_entry
->vme_start
- curr_address
;
9058 curr_address
= curr_entry
->vme_start
;
9060 curr_offset
+= skip
;
9061 curr_max_above
-= skip
;
9066 * Is the next entry at this level closer to the address (or
9067 * deeper in the submap chain) than the one we had
9070 tmp_entry
= curr_entry
->vme_next
;
9071 if (tmp_entry
== vm_map_to_entry(curr_map
)) {
9072 /* no next entry at this level */
9073 } else if (tmp_entry
->vme_start
>=
9074 curr_address
+ curr_max_above
) {
9076 * tmp_entry is beyond the scope of what we mapped of
9077 * this submap in the upper level: ignore it.
9079 } else if ((next_entry
== NULL
) ||
9080 (tmp_entry
->vme_start
+ curr_offset
<=
9081 next_entry
->vme_start
+ next_offset
)) {
9083 * We didn't have a "next_entry" or this one is
9084 * closer to the address we're looking for:
9085 * use this "tmp_entry" as the new "next_entry".
9087 if (next_entry
!= NULL
) {
9088 /* unlock the last "next_map" */
9089 if (next_map
!= curr_map
&& not_in_kdp
) {
9090 vm_map_unlock_read(next_map
);
9093 next_entry
= tmp_entry
;
9094 next_map
= curr_map
;
9095 next_depth
= curr_depth
;
9096 next_address
= next_entry
->vme_start
;
9097 next_skip
= curr_skip
;
9098 next_offset
= curr_offset
;
9099 next_offset
+= (next_address
- curr_address
);
9100 next_max_above
= MIN(next_max_above
, curr_max_above
);
9101 next_max_above
= MIN(next_max_above
,
9102 next_entry
->vme_end
- next_address
);
9103 next_max_below
= MIN(next_max_below
, curr_max_below
);
9104 next_max_below
= MIN(next_max_below
,
9105 next_address
- next_entry
->vme_start
);
9109 * "curr_max_{above,below}" allow us to keep track of the
9110 * portion of the submap that is actually mapped at this level:
9111 * the rest of that submap is irrelevant to us, since it's not
9113 * The relevant portion of the map starts at
9114 * "curr_entry->offset" up to the size of "curr_entry".
9116 curr_max_above
= MIN(curr_max_above
,
9117 curr_entry
->vme_end
- curr_address
);
9118 curr_max_below
= MIN(curr_max_below
,
9119 curr_address
- curr_entry
->vme_start
);
9121 if (!curr_entry
->is_sub_map
||
9122 curr_depth
>= user_max_depth
) {
9124 * We hit a leaf map or we reached the maximum depth
9125 * we could, so stop looking. Keep the current map
9132 * Get down to the next submap level.
9136 * Lock the next level and unlock the current level,
9137 * unless we need to keep it locked to access the "next_entry"
9141 vm_map_lock_read(curr_entry
->object
.sub_map
);
9143 if (curr_map
== next_map
) {
9144 /* keep "next_map" locked in case we need it */
9146 /* release this map */
9148 vm_map_unlock_read(curr_map
);
9152 * Adjust the offset. "curr_entry" maps the submap
9153 * at relative address "curr_entry->vme_start" in the
9154 * curr_map but skips the first "curr_entry->offset"
9155 * bytes of the submap.
9156 * "curr_offset" always represents the offset of a virtual
9157 * address in the curr_map relative to the absolute address
9158 * space (i.e. the top-level VM map).
9161 (curr_entry
->offset
- curr_entry
->vme_start
);
9162 curr_address
= user_address
+ curr_offset
;
9163 /* switch to the submap */
9164 curr_map
= curr_entry
->object
.sub_map
;
9169 if (curr_entry
== NULL
) {
9170 /* no VM region contains the address... */
9171 if (next_entry
== NULL
) {
9172 /* ... and no VM region follows it either */
9173 return KERN_INVALID_ADDRESS
;
9175 /* ... gather info about the next VM region */
9176 curr_entry
= next_entry
;
9177 curr_map
= next_map
; /* still locked ... */
9178 curr_address
= next_address
;
9179 curr_skip
= next_skip
;
9180 curr_offset
= next_offset
;
9181 curr_depth
= next_depth
;
9182 curr_max_above
= next_max_above
;
9183 curr_max_below
= next_max_below
;
9184 if (curr_map
== map
) {
9185 user_address
= curr_address
;
9188 /* we won't need "next_entry" after all */
9189 if (next_entry
!= NULL
) {
9190 /* release "next_map" */
9191 if (next_map
!= curr_map
&& not_in_kdp
) {
9192 vm_map_unlock_read(next_map
);
9201 next_max_below
= -1;
9202 next_max_above
= -1;
9204 *nesting_depth
= curr_depth
;
9205 *size
= curr_max_above
+ curr_max_below
;
9206 *address
= user_address
+ curr_skip
- curr_max_below
;
9208 // LP64todo: all the current tools are 32bit, obviously never worked for 64b
9209 // so probably should be a real 32b ID vs. ptr.
9210 // Current users just check for equality
9211 #define INFO_MAKE_OBJECT_ID(p) ((uint32_t)(uintptr_t)p)
9213 if (look_for_pages
) {
9214 submap_info
->user_tag
= curr_entry
->alias
;
9215 submap_info
->offset
= curr_entry
->offset
;
9216 submap_info
->protection
= curr_entry
->protection
;
9217 submap_info
->inheritance
= curr_entry
->inheritance
;
9218 submap_info
->max_protection
= curr_entry
->max_protection
;
9219 submap_info
->behavior
= curr_entry
->behavior
;
9220 submap_info
->user_wired_count
= curr_entry
->user_wired_count
;
9221 submap_info
->is_submap
= curr_entry
->is_sub_map
;
9222 submap_info
->object_id
= INFO_MAKE_OBJECT_ID(curr_entry
->object
.vm_object
);
9224 short_info
->user_tag
= curr_entry
->alias
;
9225 short_info
->offset
= curr_entry
->offset
;
9226 short_info
->protection
= curr_entry
->protection
;
9227 short_info
->inheritance
= curr_entry
->inheritance
;
9228 short_info
->max_protection
= curr_entry
->max_protection
;
9229 short_info
->behavior
= curr_entry
->behavior
;
9230 short_info
->user_wired_count
= curr_entry
->user_wired_count
;
9231 short_info
->is_submap
= curr_entry
->is_sub_map
;
9232 short_info
->object_id
= INFO_MAKE_OBJECT_ID(curr_entry
->object
.vm_object
);
9235 extended
.pages_resident
= 0;
9236 extended
.pages_swapped_out
= 0;
9237 extended
.pages_shared_now_private
= 0;
9238 extended
.pages_dirtied
= 0;
9239 extended
.external_pager
= 0;
9240 extended
.shadow_depth
= 0;
9243 if (!curr_entry
->is_sub_map
) {
9244 vm_map_offset_t range_start
, range_end
;
9245 range_start
= MAX((curr_address
- curr_max_below
),
9246 curr_entry
->vme_start
);
9247 range_end
= MIN((curr_address
+ curr_max_above
),
9248 curr_entry
->vme_end
);
9249 vm_map_region_walk(curr_map
,
9252 (curr_entry
->offset
+
9254 curr_entry
->vme_start
)),
9255 range_end
- range_start
,
9258 if (extended
.external_pager
&&
9259 extended
.ref_count
== 2 &&
9260 extended
.share_mode
== SM_SHARED
) {
9261 extended
.share_mode
= SM_PRIVATE
;
9264 if (curr_entry
->use_pmap
) {
9265 extended
.share_mode
= SM_TRUESHARED
;
9267 extended
.share_mode
= SM_PRIVATE
;
9269 extended
.ref_count
=
9270 curr_entry
->object
.sub_map
->ref_count
;
9274 if (look_for_pages
) {
9275 submap_info
->pages_resident
= extended
.pages_resident
;
9276 submap_info
->pages_swapped_out
= extended
.pages_swapped_out
;
9277 submap_info
->pages_shared_now_private
=
9278 extended
.pages_shared_now_private
;
9279 submap_info
->pages_dirtied
= extended
.pages_dirtied
;
9280 submap_info
->external_pager
= extended
.external_pager
;
9281 submap_info
->shadow_depth
= extended
.shadow_depth
;
9282 submap_info
->share_mode
= extended
.share_mode
;
9283 submap_info
->ref_count
= extended
.ref_count
;
9285 short_info
->external_pager
= extended
.external_pager
;
9286 short_info
->shadow_depth
= extended
.shadow_depth
;
9287 short_info
->share_mode
= extended
.share_mode
;
9288 short_info
->ref_count
= extended
.ref_count
;
9292 vm_map_unlock_read(curr_map
);
9295 return KERN_SUCCESS
;
9301 * User call to obtain information about a region in
9302 * a task's address map. Currently, only one flavor is
9305 * XXX The reserved and behavior fields cannot be filled
9306 * in until the vm merge from the IK is completed, and
9307 * vm_reserve is implemented.
9313 vm_map_offset_t
*address
, /* IN/OUT */
9314 vm_map_size_t
*size
, /* OUT */
9315 vm_region_flavor_t flavor
, /* IN */
9316 vm_region_info_t info
, /* OUT */
9317 mach_msg_type_number_t
*count
, /* IN/OUT */
9318 mach_port_t
*object_name
) /* OUT */
9320 vm_map_entry_t tmp_entry
;
9321 vm_map_entry_t entry
;
9322 vm_map_offset_t start
;
9324 if (map
== VM_MAP_NULL
)
9325 return(KERN_INVALID_ARGUMENT
);
9329 case VM_REGION_BASIC_INFO
:
9330 /* legacy for old 32-bit objects info */
9332 vm_region_basic_info_t basic
;
9334 if (*count
< VM_REGION_BASIC_INFO_COUNT
)
9335 return(KERN_INVALID_ARGUMENT
);
9337 basic
= (vm_region_basic_info_t
) info
;
9338 *count
= VM_REGION_BASIC_INFO_COUNT
;
9340 vm_map_lock_read(map
);
9343 if (!vm_map_lookup_entry(map
, start
, &tmp_entry
)) {
9344 if ((entry
= tmp_entry
->vme_next
) == vm_map_to_entry(map
)) {
9345 vm_map_unlock_read(map
);
9346 return(KERN_INVALID_ADDRESS
);
9352 start
= entry
->vme_start
;
9354 basic
->offset
= (uint32_t)entry
->offset
;
9355 basic
->protection
= entry
->protection
;
9356 basic
->inheritance
= entry
->inheritance
;
9357 basic
->max_protection
= entry
->max_protection
;
9358 basic
->behavior
= entry
->behavior
;
9359 basic
->user_wired_count
= entry
->user_wired_count
;
9360 basic
->reserved
= entry
->is_sub_map
;
9362 *size
= (entry
->vme_end
- start
);
9364 if (object_name
) *object_name
= IP_NULL
;
9365 if (entry
->is_sub_map
) {
9366 basic
->shared
= FALSE
;
9368 basic
->shared
= entry
->is_shared
;
9371 vm_map_unlock_read(map
);
9372 return(KERN_SUCCESS
);
9375 case VM_REGION_BASIC_INFO_64
:
9377 vm_region_basic_info_64_t basic
;
9379 if (*count
< VM_REGION_BASIC_INFO_COUNT_64
)
9380 return(KERN_INVALID_ARGUMENT
);
9382 basic
= (vm_region_basic_info_64_t
) info
;
9383 *count
= VM_REGION_BASIC_INFO_COUNT_64
;
9385 vm_map_lock_read(map
);
9388 if (!vm_map_lookup_entry(map
, start
, &tmp_entry
)) {
9389 if ((entry
= tmp_entry
->vme_next
) == vm_map_to_entry(map
)) {
9390 vm_map_unlock_read(map
);
9391 return(KERN_INVALID_ADDRESS
);
9397 start
= entry
->vme_start
;
9399 basic
->offset
= entry
->offset
;
9400 basic
->protection
= entry
->protection
;
9401 basic
->inheritance
= entry
->inheritance
;
9402 basic
->max_protection
= entry
->max_protection
;
9403 basic
->behavior
= entry
->behavior
;
9404 basic
->user_wired_count
= entry
->user_wired_count
;
9405 basic
->reserved
= entry
->is_sub_map
;
9407 *size
= (entry
->vme_end
- start
);
9409 if (object_name
) *object_name
= IP_NULL
;
9410 if (entry
->is_sub_map
) {
9411 basic
->shared
= FALSE
;
9413 basic
->shared
= entry
->is_shared
;
9416 vm_map_unlock_read(map
);
9417 return(KERN_SUCCESS
);
9419 case VM_REGION_EXTENDED_INFO
:
9421 vm_region_extended_info_t extended
;
9423 if (*count
< VM_REGION_EXTENDED_INFO_COUNT
)
9424 return(KERN_INVALID_ARGUMENT
);
9426 extended
= (vm_region_extended_info_t
) info
;
9427 *count
= VM_REGION_EXTENDED_INFO_COUNT
;
9429 vm_map_lock_read(map
);
9432 if (!vm_map_lookup_entry(map
, start
, &tmp_entry
)) {
9433 if ((entry
= tmp_entry
->vme_next
) == vm_map_to_entry(map
)) {
9434 vm_map_unlock_read(map
);
9435 return(KERN_INVALID_ADDRESS
);
9440 start
= entry
->vme_start
;
9442 extended
->protection
= entry
->protection
;
9443 extended
->user_tag
= entry
->alias
;
9444 extended
->pages_resident
= 0;
9445 extended
->pages_swapped_out
= 0;
9446 extended
->pages_shared_now_private
= 0;
9447 extended
->pages_dirtied
= 0;
9448 extended
->external_pager
= 0;
9449 extended
->shadow_depth
= 0;
9451 vm_map_region_walk(map
, start
, entry
, entry
->offset
, entry
->vme_end
- start
, extended
, TRUE
);
9453 if (extended
->external_pager
&& extended
->ref_count
== 2 && extended
->share_mode
== SM_SHARED
)
9454 extended
->share_mode
= SM_PRIVATE
;
9457 *object_name
= IP_NULL
;
9459 *size
= (entry
->vme_end
- start
);
9461 vm_map_unlock_read(map
);
9462 return(KERN_SUCCESS
);
9464 case VM_REGION_TOP_INFO
:
9466 vm_region_top_info_t top
;
9468 if (*count
< VM_REGION_TOP_INFO_COUNT
)
9469 return(KERN_INVALID_ARGUMENT
);
9471 top
= (vm_region_top_info_t
) info
;
9472 *count
= VM_REGION_TOP_INFO_COUNT
;
9474 vm_map_lock_read(map
);
9477 if (!vm_map_lookup_entry(map
, start
, &tmp_entry
)) {
9478 if ((entry
= tmp_entry
->vme_next
) == vm_map_to_entry(map
)) {
9479 vm_map_unlock_read(map
);
9480 return(KERN_INVALID_ADDRESS
);
9486 start
= entry
->vme_start
;
9488 top
->private_pages_resident
= 0;
9489 top
->shared_pages_resident
= 0;
9491 vm_map_region_top_walk(entry
, top
);
9494 *object_name
= IP_NULL
;
9496 *size
= (entry
->vme_end
- start
);
9498 vm_map_unlock_read(map
);
9499 return(KERN_SUCCESS
);
9502 return(KERN_INVALID_ARGUMENT
);
9506 #define OBJ_RESIDENT_COUNT(obj, entry_size) \
9508 ((obj)->all_reusable ? \
9509 (obj)->wired_page_count : \
9510 (obj)->resident_page_count - (obj)->reusable_page_count))
9513 vm_map_region_top_walk(
9514 vm_map_entry_t entry
,
9515 vm_region_top_info_t top
)
9518 if (entry
->object
.vm_object
== 0 || entry
->is_sub_map
) {
9519 top
->share_mode
= SM_EMPTY
;
9526 struct vm_object
*obj
, *tmp_obj
;
9528 uint32_t entry_size
;
9530 entry_size
= (uint32_t) ((entry
->vme_end
- entry
->vme_start
) / PAGE_SIZE_64
);
9532 obj
= entry
->object
.vm_object
;
9534 vm_object_lock(obj
);
9536 if ((ref_count
= obj
->ref_count
) > 1 && obj
->paging_in_progress
)
9539 assert(obj
->reusable_page_count
<= obj
->resident_page_count
);
9542 top
->private_pages_resident
=
9543 OBJ_RESIDENT_COUNT(obj
, entry_size
);
9545 top
->shared_pages_resident
=
9546 OBJ_RESIDENT_COUNT(obj
, entry_size
);
9547 top
->ref_count
= ref_count
;
9548 top
->share_mode
= SM_COW
;
9550 while ((tmp_obj
= obj
->shadow
)) {
9551 vm_object_lock(tmp_obj
);
9552 vm_object_unlock(obj
);
9555 if ((ref_count
= obj
->ref_count
) > 1 && obj
->paging_in_progress
)
9558 assert(obj
->reusable_page_count
<= obj
->resident_page_count
);
9559 top
->shared_pages_resident
+=
9560 OBJ_RESIDENT_COUNT(obj
, entry_size
);
9561 top
->ref_count
+= ref_count
- 1;
9564 if (entry
->superpage_size
) {
9565 top
->share_mode
= SM_LARGE_PAGE
;
9566 top
->shared_pages_resident
= 0;
9567 top
->private_pages_resident
= entry_size
;
9568 } else if (entry
->needs_copy
) {
9569 top
->share_mode
= SM_COW
;
9570 top
->shared_pages_resident
=
9571 OBJ_RESIDENT_COUNT(obj
, entry_size
);
9573 if (ref_count
== 1 ||
9574 (ref_count
== 2 && !(obj
->pager_trusted
) && !(obj
->internal
))) {
9575 top
->share_mode
= SM_PRIVATE
;
9576 top
->private_pages_resident
=
9577 OBJ_RESIDENT_COUNT(obj
,
9580 top
->share_mode
= SM_SHARED
;
9581 top
->shared_pages_resident
=
9582 OBJ_RESIDENT_COUNT(obj
,
9586 top
->ref_count
= ref_count
;
9588 /* XXX K64: obj_id will be truncated */
9589 top
->obj_id
= (unsigned int) (uintptr_t)obj
;
9591 vm_object_unlock(obj
);
9599 vm_map_entry_t entry
,
9600 vm_object_offset_t offset
,
9601 vm_object_size_t range
,
9602 vm_region_extended_info_t extended
,
9603 boolean_t look_for_pages
)
9605 register struct vm_object
*obj
, *tmp_obj
;
9606 register vm_map_offset_t last_offset
;
9608 register int ref_count
;
9609 struct vm_object
*shadow_object
;
9612 if ((entry
->object
.vm_object
== 0) ||
9613 (entry
->is_sub_map
) ||
9614 (entry
->object
.vm_object
->phys_contiguous
&&
9615 !entry
->superpage_size
)) {
9616 extended
->share_mode
= SM_EMPTY
;
9617 extended
->ref_count
= 0;
9621 if (entry
->superpage_size
) {
9622 extended
->shadow_depth
= 0;
9623 extended
->share_mode
= SM_LARGE_PAGE
;
9624 extended
->ref_count
= 1;
9625 extended
->external_pager
= 0;
9626 extended
->pages_resident
= (unsigned int)(range
>> PAGE_SHIFT
);
9627 extended
->shadow_depth
= 0;
9632 obj
= entry
->object
.vm_object
;
9634 vm_object_lock(obj
);
9636 if ((ref_count
= obj
->ref_count
) > 1 && obj
->paging_in_progress
)
9639 if (look_for_pages
) {
9640 for (last_offset
= offset
+ range
;
9641 offset
< last_offset
;
9642 offset
+= PAGE_SIZE_64
, va
+= PAGE_SIZE
)
9643 vm_map_region_look_for_page(map
, va
, obj
,
9647 shadow_object
= obj
->shadow
;
9650 if ( !(obj
->pager_trusted
) && !(obj
->internal
))
9651 extended
->external_pager
= 1;
9653 if (shadow_object
!= VM_OBJECT_NULL
) {
9654 vm_object_lock(shadow_object
);
9656 shadow_object
!= VM_OBJECT_NULL
;
9658 vm_object_t next_shadow
;
9660 if ( !(shadow_object
->pager_trusted
) &&
9661 !(shadow_object
->internal
))
9662 extended
->external_pager
= 1;
9664 next_shadow
= shadow_object
->shadow
;
9666 vm_object_lock(next_shadow
);
9668 vm_object_unlock(shadow_object
);
9669 shadow_object
= next_shadow
;
9672 extended
->shadow_depth
= shadow_depth
;
9675 if (extended
->shadow_depth
|| entry
->needs_copy
)
9676 extended
->share_mode
= SM_COW
;
9679 extended
->share_mode
= SM_PRIVATE
;
9681 if (obj
->true_share
)
9682 extended
->share_mode
= SM_TRUESHARED
;
9684 extended
->share_mode
= SM_SHARED
;
9687 extended
->ref_count
= ref_count
- extended
->shadow_depth
;
9689 for (i
= 0; i
< extended
->shadow_depth
; i
++) {
9690 if ((tmp_obj
= obj
->shadow
) == 0)
9692 vm_object_lock(tmp_obj
);
9693 vm_object_unlock(obj
);
9695 if ((ref_count
= tmp_obj
->ref_count
) > 1 && tmp_obj
->paging_in_progress
)
9698 extended
->ref_count
+= ref_count
;
9701 vm_object_unlock(obj
);
9703 if (extended
->share_mode
== SM_SHARED
) {
9704 register vm_map_entry_t cur
;
9705 register vm_map_entry_t last
;
9708 obj
= entry
->object
.vm_object
;
9709 last
= vm_map_to_entry(map
);
9712 if ((ref_count
= obj
->ref_count
) > 1 && obj
->paging_in_progress
)
9714 for (cur
= vm_map_first_entry(map
); cur
!= last
; cur
= cur
->vme_next
)
9715 my_refs
+= vm_map_region_count_obj_refs(cur
, obj
);
9717 if (my_refs
== ref_count
)
9718 extended
->share_mode
= SM_PRIVATE_ALIASED
;
9719 else if (my_refs
> 1)
9720 extended
->share_mode
= SM_SHARED_ALIASED
;
9726 /* object is locked on entry and locked on return */
9730 vm_map_region_look_for_page(
9731 __unused vm_map_t map
,
9732 __unused vm_map_offset_t va
,
9734 vm_object_offset_t offset
,
9737 vm_region_extended_info_t extended
)
9739 register vm_page_t p
;
9740 register vm_object_t shadow
;
9741 register int ref_count
;
9742 vm_object_t caller_object
;
9746 shadow
= object
->shadow
;
9747 caller_object
= object
;
9752 if ( !(object
->pager_trusted
) && !(object
->internal
))
9753 extended
->external_pager
= 1;
9755 if ((p
= vm_page_lookup(object
, offset
)) != VM_PAGE_NULL
) {
9756 if (shadow
&& (max_refcnt
== 1))
9757 extended
->pages_shared_now_private
++;
9759 if (!p
->fictitious
&&
9760 (p
->dirty
|| pmap_is_modified(p
->phys_page
)))
9761 extended
->pages_dirtied
++;
9763 extended
->pages_resident
++;
9765 if(object
!= caller_object
)
9766 vm_object_unlock(object
);
9771 if (object
->existence_map
) {
9772 if (vm_external_state_get(object
->existence_map
, offset
) == VM_EXTERNAL_STATE_EXISTS
) {
9774 extended
->pages_swapped_out
++;
9776 if(object
!= caller_object
)
9777 vm_object_unlock(object
);
9781 } else if (object
->internal
&&
9783 !object
->terminating
&&
9784 object
->pager_ready
) {
9786 memory_object_t pager
;
9788 vm_object_paging_begin(object
);
9789 pager
= object
->pager
;
9790 vm_object_unlock(object
);
9792 kr
= memory_object_data_request(
9794 offset
+ object
->paging_offset
,
9795 0, /* just poke the pager */
9799 vm_object_lock(object
);
9800 vm_object_paging_end(object
);
9802 if (kr
== KERN_SUCCESS
) {
9803 /* the pager has that page */
9804 extended
->pages_swapped_out
++;
9805 if (object
!= caller_object
)
9806 vm_object_unlock(object
);
9810 #endif /* MACH_PAGEMAP */
9813 vm_object_lock(shadow
);
9815 if ((ref_count
= shadow
->ref_count
) > 1 && shadow
->paging_in_progress
)
9818 if (++depth
> extended
->shadow_depth
)
9819 extended
->shadow_depth
= depth
;
9821 if (ref_count
> max_refcnt
)
9822 max_refcnt
= ref_count
;
9824 if(object
!= caller_object
)
9825 vm_object_unlock(object
);
9827 offset
= offset
+ object
->vo_shadow_offset
;
9829 shadow
= object
->shadow
;
9832 if(object
!= caller_object
)
9833 vm_object_unlock(object
);
9839 vm_map_region_count_obj_refs(
9840 vm_map_entry_t entry
,
9843 register int ref_count
;
9844 register vm_object_t chk_obj
;
9845 register vm_object_t tmp_obj
;
9847 if (entry
->object
.vm_object
== 0)
9850 if (entry
->is_sub_map
)
9855 chk_obj
= entry
->object
.vm_object
;
9856 vm_object_lock(chk_obj
);
9859 if (chk_obj
== object
)
9861 tmp_obj
= chk_obj
->shadow
;
9863 vm_object_lock(tmp_obj
);
9864 vm_object_unlock(chk_obj
);
9874 * Routine: vm_map_simplify
9877 * Attempt to simplify the map representation in
9878 * the vicinity of the given starting address.
9880 * This routine is intended primarily to keep the
9881 * kernel maps more compact -- they generally don't
9882 * benefit from the "expand a map entry" technology
9883 * at allocation time because the adjacent entry
9884 * is often wired down.
9887 vm_map_simplify_entry(
9889 vm_map_entry_t this_entry
)
9891 vm_map_entry_t prev_entry
;
9893 counter(c_vm_map_simplify_entry_called
++);
9895 prev_entry
= this_entry
->vme_prev
;
9897 if ((this_entry
!= vm_map_to_entry(map
)) &&
9898 (prev_entry
!= vm_map_to_entry(map
)) &&
9900 (prev_entry
->vme_end
== this_entry
->vme_start
) &&
9902 (prev_entry
->is_sub_map
== this_entry
->is_sub_map
) &&
9904 (prev_entry
->object
.vm_object
== this_entry
->object
.vm_object
) &&
9905 ((prev_entry
->offset
+ (prev_entry
->vme_end
-
9906 prev_entry
->vme_start
))
9907 == this_entry
->offset
) &&
9909 (prev_entry
->inheritance
== this_entry
->inheritance
) &&
9910 (prev_entry
->protection
== this_entry
->protection
) &&
9911 (prev_entry
->max_protection
== this_entry
->max_protection
) &&
9912 (prev_entry
->behavior
== this_entry
->behavior
) &&
9913 (prev_entry
->alias
== this_entry
->alias
) &&
9914 (prev_entry
->zero_wired_pages
== this_entry
->zero_wired_pages
) &&
9915 (prev_entry
->no_cache
== this_entry
->no_cache
) &&
9916 (prev_entry
->wired_count
== this_entry
->wired_count
) &&
9917 (prev_entry
->user_wired_count
== this_entry
->user_wired_count
) &&
9919 (prev_entry
->needs_copy
== this_entry
->needs_copy
) &&
9920 (prev_entry
->permanent
== this_entry
->permanent
) &&
9922 (prev_entry
->use_pmap
== FALSE
) &&
9923 (this_entry
->use_pmap
== FALSE
) &&
9924 (prev_entry
->in_transition
== FALSE
) &&
9925 (this_entry
->in_transition
== FALSE
) &&
9926 (prev_entry
->needs_wakeup
== FALSE
) &&
9927 (this_entry
->needs_wakeup
== FALSE
) &&
9928 (prev_entry
->is_shared
== FALSE
) &&
9929 (this_entry
->is_shared
== FALSE
)
9931 _vm_map_store_entry_unlink(&map
->hdr
, prev_entry
);
9932 this_entry
->vme_start
= prev_entry
->vme_start
;
9933 this_entry
->offset
= prev_entry
->offset
;
9934 if (prev_entry
->is_sub_map
) {
9935 vm_map_deallocate(prev_entry
->object
.sub_map
);
9937 vm_object_deallocate(prev_entry
->object
.vm_object
);
9939 vm_map_entry_dispose(map
, prev_entry
);
9940 SAVE_HINT_MAP_WRITE(map
, this_entry
);
9941 counter(c_vm_map_simplified
++);
9948 vm_map_offset_t start
)
9950 vm_map_entry_t this_entry
;
9953 if (vm_map_lookup_entry(map
, start
, &this_entry
)) {
9954 vm_map_simplify_entry(map
, this_entry
);
9955 vm_map_simplify_entry(map
, this_entry
->vme_next
);
9957 counter(c_vm_map_simplify_called
++);
9962 vm_map_simplify_range(
9964 vm_map_offset_t start
,
9965 vm_map_offset_t end
)
9967 vm_map_entry_t entry
;
9970 * The map should be locked (for "write") by the caller.
9974 /* invalid address range */
9978 start
= vm_map_trunc_page(start
);
9979 end
= vm_map_round_page(end
);
9981 if (!vm_map_lookup_entry(map
, start
, &entry
)) {
9982 /* "start" is not mapped and "entry" ends before "start" */
9983 if (entry
== vm_map_to_entry(map
)) {
9984 /* start with first entry in the map */
9985 entry
= vm_map_first_entry(map
);
9987 /* start with next entry */
9988 entry
= entry
->vme_next
;
9992 while (entry
!= vm_map_to_entry(map
) &&
9993 entry
->vme_start
<= end
) {
9994 /* try and coalesce "entry" with its previous entry */
9995 vm_map_simplify_entry(map
, entry
);
9996 entry
= entry
->vme_next
;
10002 * Routine: vm_map_machine_attribute
10004 * Provide machine-specific attributes to mappings,
10005 * such as cachability etc. for machines that provide
10006 * them. NUMA architectures and machines with big/strange
10007 * caches will use this.
10009 * Responsibilities for locking and checking are handled here,
10010 * everything else in the pmap module. If any non-volatile
10011 * information must be kept, the pmap module should handle
10012 * it itself. [This assumes that attributes do not
10013 * need to be inherited, which seems ok to me]
10016 vm_map_machine_attribute(
10018 vm_map_offset_t start
,
10019 vm_map_offset_t end
,
10020 vm_machine_attribute_t attribute
,
10021 vm_machine_attribute_val_t
* value
) /* IN/OUT */
10024 vm_map_size_t sync_size
;
10025 vm_map_entry_t entry
;
10027 if (start
< vm_map_min(map
) || end
> vm_map_max(map
))
10028 return KERN_INVALID_ADDRESS
;
10030 /* Figure how much memory we need to flush (in page increments) */
10031 sync_size
= end
- start
;
10035 if (attribute
!= MATTR_CACHE
) {
10036 /* If we don't have to find physical addresses, we */
10037 /* don't have to do an explicit traversal here. */
10038 ret
= pmap_attribute(map
->pmap
, start
, end
-start
,
10040 vm_map_unlock(map
);
10044 ret
= KERN_SUCCESS
; /* Assume it all worked */
10047 if (vm_map_lookup_entry(map
, start
, &entry
)) {
10048 vm_map_size_t sub_size
;
10049 if((entry
->vme_end
- start
) > sync_size
) {
10050 sub_size
= sync_size
;
10053 sub_size
= entry
->vme_end
- start
;
10054 sync_size
-= sub_size
;
10056 if(entry
->is_sub_map
) {
10057 vm_map_offset_t sub_start
;
10058 vm_map_offset_t sub_end
;
10060 sub_start
= (start
- entry
->vme_start
)
10062 sub_end
= sub_start
+ sub_size
;
10063 vm_map_machine_attribute(
10064 entry
->object
.sub_map
,
10069 if(entry
->object
.vm_object
) {
10071 vm_object_t object
;
10072 vm_object_t base_object
;
10073 vm_object_t last_object
;
10074 vm_object_offset_t offset
;
10075 vm_object_offset_t base_offset
;
10076 vm_map_size_t range
;
10078 offset
= (start
- entry
->vme_start
)
10080 base_offset
= offset
;
10081 object
= entry
->object
.vm_object
;
10082 base_object
= object
;
10083 last_object
= NULL
;
10085 vm_object_lock(object
);
10088 m
= vm_page_lookup(
10091 if (m
&& !m
->fictitious
) {
10093 pmap_attribute_cache_sync(
10098 } else if (object
->shadow
) {
10099 offset
= offset
+ object
->vo_shadow_offset
;
10100 last_object
= object
;
10101 object
= object
->shadow
;
10102 vm_object_lock(last_object
->shadow
);
10103 vm_object_unlock(last_object
);
10106 range
-= PAGE_SIZE
;
10108 if (base_object
!= object
) {
10109 vm_object_unlock(object
);
10110 vm_object_lock(base_object
);
10111 object
= base_object
;
10113 /* Bump to the next page */
10114 base_offset
+= PAGE_SIZE
;
10115 offset
= base_offset
;
10117 vm_object_unlock(object
);
10122 vm_map_unlock(map
);
10123 return KERN_FAILURE
;
10128 vm_map_unlock(map
);
10134 * vm_map_behavior_set:
10136 * Sets the paging reference behavior of the specified address
10137 * range in the target map. Paging reference behavior affects
10138 * how pagein operations resulting from faults on the map will be
10142 vm_map_behavior_set(
10144 vm_map_offset_t start
,
10145 vm_map_offset_t end
,
10146 vm_behavior_t new_behavior
)
10148 register vm_map_entry_t entry
;
10149 vm_map_entry_t temp_entry
;
10152 "vm_map_behavior_set, 0x%X start 0x%X end 0x%X behavior %d",
10153 map
, start
, end
, new_behavior
, 0);
10156 start
< vm_map_min(map
) ||
10157 end
> vm_map_max(map
)) {
10158 return KERN_NO_SPACE
;
10161 switch (new_behavior
) {
10164 * This first block of behaviors all set a persistent state on the specified
10165 * memory range. All we have to do here is to record the desired behavior
10166 * in the vm_map_entry_t's.
10169 case VM_BEHAVIOR_DEFAULT
:
10170 case VM_BEHAVIOR_RANDOM
:
10171 case VM_BEHAVIOR_SEQUENTIAL
:
10172 case VM_BEHAVIOR_RSEQNTL
:
10173 case VM_BEHAVIOR_ZERO_WIRED_PAGES
:
10177 * The entire address range must be valid for the map.
10178 * Note that vm_map_range_check() does a
10179 * vm_map_lookup_entry() internally and returns the
10180 * entry containing the start of the address range if
10181 * the entire range is valid.
10183 if (vm_map_range_check(map
, start
, end
, &temp_entry
)) {
10184 entry
= temp_entry
;
10185 vm_map_clip_start(map
, entry
, start
);
10188 vm_map_unlock(map
);
10189 return(KERN_INVALID_ADDRESS
);
10192 while ((entry
!= vm_map_to_entry(map
)) && (entry
->vme_start
< end
)) {
10193 vm_map_clip_end(map
, entry
, end
);
10194 assert(!entry
->use_pmap
);
10196 if( new_behavior
== VM_BEHAVIOR_ZERO_WIRED_PAGES
) {
10197 entry
->zero_wired_pages
= TRUE
;
10199 entry
->behavior
= new_behavior
;
10201 entry
= entry
->vme_next
;
10204 vm_map_unlock(map
);
10208 * The rest of these are different from the above in that they cause
10209 * an immediate action to take place as opposed to setting a behavior that
10210 * affects future actions.
10213 case VM_BEHAVIOR_WILLNEED
:
10214 return vm_map_willneed(map
, start
, end
);
10216 case VM_BEHAVIOR_DONTNEED
:
10217 return vm_map_msync(map
, start
, end
- start
, VM_SYNC_DEACTIVATE
| VM_SYNC_CONTIGUOUS
);
10219 case VM_BEHAVIOR_FREE
:
10220 return vm_map_msync(map
, start
, end
- start
, VM_SYNC_KILLPAGES
| VM_SYNC_CONTIGUOUS
);
10222 case VM_BEHAVIOR_REUSABLE
:
10223 return vm_map_reusable_pages(map
, start
, end
);
10225 case VM_BEHAVIOR_REUSE
:
10226 return vm_map_reuse_pages(map
, start
, end
);
10228 case VM_BEHAVIOR_CAN_REUSE
:
10229 return vm_map_can_reuse(map
, start
, end
);
10232 return(KERN_INVALID_ARGUMENT
);
10235 return(KERN_SUCCESS
);
10240 * Internals for madvise(MADV_WILLNEED) system call.
10242 * The present implementation is to do a read-ahead if the mapping corresponds
10243 * to a mapped regular file. If it's an anonymous mapping, then we do nothing
10244 * and basically ignore the "advice" (which we are always free to do).
10248 static kern_return_t
10251 vm_map_offset_t start
,
10252 vm_map_offset_t end
10255 vm_map_entry_t entry
;
10256 vm_object_t object
;
10257 memory_object_t pager
;
10258 struct vm_object_fault_info fault_info
;
10260 vm_object_size_t len
;
10261 vm_object_offset_t offset
;
10264 * Fill in static values in fault_info. Several fields get ignored by the code
10265 * we call, but we'll fill them in anyway since uninitialized fields are bad
10266 * when it comes to future backwards compatibility.
10269 fault_info
.interruptible
= THREAD_UNINT
; /* ignored value */
10270 fault_info
.behavior
= VM_BEHAVIOR_SEQUENTIAL
;
10271 fault_info
.no_cache
= FALSE
; /* ignored value */
10272 fault_info
.stealth
= TRUE
;
10273 fault_info
.io_sync
= FALSE
;
10274 fault_info
.cs_bypass
= FALSE
;
10275 fault_info
.mark_zf_absent
= FALSE
;
10278 * The MADV_WILLNEED operation doesn't require any changes to the
10279 * vm_map_entry_t's, so the read lock is sufficient.
10282 vm_map_lock_read(map
);
10285 * The madvise semantics require that the address range be fully
10286 * allocated with no holes. Otherwise, we're required to return
10290 if (! vm_map_range_check(map
, start
, end
, &entry
)) {
10291 vm_map_unlock_read(map
);
10292 return KERN_INVALID_ADDRESS
;
10296 * Examine each vm_map_entry_t in the range.
10298 for (; entry
!= vm_map_to_entry(map
) && start
< end
; ) {
10301 * The first time through, the start address could be anywhere
10302 * within the vm_map_entry we found. So adjust the offset to
10303 * correspond. After that, the offset will always be zero to
10304 * correspond to the beginning of the current vm_map_entry.
10306 offset
= (start
- entry
->vme_start
) + entry
->offset
;
10309 * Set the length so we don't go beyond the end of the
10310 * map_entry or beyond the end of the range we were given.
10311 * This range could span also multiple map entries all of which
10312 * map different files, so make sure we only do the right amount
10313 * of I/O for each object. Note that it's possible for there
10314 * to be multiple map entries all referring to the same object
10315 * but with different page permissions, but it's not worth
10316 * trying to optimize that case.
10318 len
= MIN(entry
->vme_end
- start
, end
- start
);
10320 if ((vm_size_t
) len
!= len
) {
10321 /* 32-bit overflow */
10322 len
= (vm_size_t
) (0 - PAGE_SIZE
);
10324 fault_info
.cluster_size
= (vm_size_t
) len
;
10325 fault_info
.lo_offset
= offset
;
10326 fault_info
.hi_offset
= offset
+ len
;
10327 fault_info
.user_tag
= entry
->alias
;
10330 * If there's no read permission to this mapping, then just
10333 if ((entry
->protection
& VM_PROT_READ
) == 0) {
10334 entry
= entry
->vme_next
;
10335 start
= entry
->vme_start
;
10340 * Find the file object backing this map entry. If there is
10341 * none, then we simply ignore the "will need" advice for this
10342 * entry and go on to the next one.
10344 if ((object
= find_vnode_object(entry
)) == VM_OBJECT_NULL
) {
10345 entry
= entry
->vme_next
;
10346 start
= entry
->vme_start
;
10351 * The data_request() could take a long time, so let's
10352 * release the map lock to avoid blocking other threads.
10354 vm_map_unlock_read(map
);
10356 vm_object_paging_begin(object
);
10357 pager
= object
->pager
;
10358 vm_object_unlock(object
);
10361 * Get the data from the object asynchronously.
10363 * Note that memory_object_data_request() places limits on the
10364 * amount of I/O it will do. Regardless of the len we
10365 * specified, it won't do more than MAX_UPL_TRANSFER and it
10366 * silently truncates the len to that size. This isn't
10367 * necessarily bad since madvise shouldn't really be used to
10368 * page in unlimited amounts of data. Other Unix variants
10369 * limit the willneed case as well. If this turns out to be an
10370 * issue for developers, then we can always adjust the policy
10371 * here and still be backwards compatible since this is all
10374 kr
= memory_object_data_request(
10376 offset
+ object
->paging_offset
,
10379 (memory_object_fault_info_t
)&fault_info
);
10381 vm_object_lock(object
);
10382 vm_object_paging_end(object
);
10383 vm_object_unlock(object
);
10386 * If we couldn't do the I/O for some reason, just give up on
10387 * the madvise. We still return success to the user since
10388 * madvise isn't supposed to fail when the advice can't be
10391 if (kr
!= KERN_SUCCESS
) {
10392 return KERN_SUCCESS
;
10396 if (start
>= end
) {
10398 return KERN_SUCCESS
;
10401 /* look up next entry */
10402 vm_map_lock_read(map
);
10403 if (! vm_map_lookup_entry(map
, start
, &entry
)) {
10405 * There's a new hole in the address range.
10407 vm_map_unlock_read(map
);
10408 return KERN_INVALID_ADDRESS
;
10412 vm_map_unlock_read(map
);
10413 return KERN_SUCCESS
;
10417 vm_map_entry_is_reusable(
10418 vm_map_entry_t entry
)
10420 vm_object_t object
;
10422 if (entry
->is_shared
||
10423 entry
->is_sub_map
||
10424 entry
->in_transition
||
10425 entry
->protection
!= VM_PROT_DEFAULT
||
10426 entry
->max_protection
!= VM_PROT_ALL
||
10427 entry
->inheritance
!= VM_INHERIT_DEFAULT
||
10429 entry
->permanent
||
10430 entry
->superpage_size
!= 0 ||
10431 entry
->zero_wired_pages
||
10432 entry
->wired_count
!= 0 ||
10433 entry
->user_wired_count
!= 0) {
10437 object
= entry
->object
.vm_object
;
10438 if (object
== VM_OBJECT_NULL
) {
10441 if (object
->ref_count
== 1 &&
10442 object
->wired_page_count
== 0 &&
10443 object
->copy
== VM_OBJECT_NULL
&&
10444 object
->shadow
== VM_OBJECT_NULL
&&
10445 object
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
&&
10446 object
->internal
&&
10447 !object
->true_share
&&
10448 object
->wimg_bits
== VM_WIMG_USE_DEFAULT
&&
10449 !object
->code_signed
) {
10457 static kern_return_t
10458 vm_map_reuse_pages(
10460 vm_map_offset_t start
,
10461 vm_map_offset_t end
)
10463 vm_map_entry_t entry
;
10464 vm_object_t object
;
10465 vm_object_offset_t start_offset
, end_offset
;
10468 * The MADV_REUSE operation doesn't require any changes to the
10469 * vm_map_entry_t's, so the read lock is sufficient.
10472 vm_map_lock_read(map
);
10475 * The madvise semantics require that the address range be fully
10476 * allocated with no holes. Otherwise, we're required to return
10480 if (!vm_map_range_check(map
, start
, end
, &entry
)) {
10481 vm_map_unlock_read(map
);
10482 vm_page_stats_reusable
.reuse_pages_failure
++;
10483 return KERN_INVALID_ADDRESS
;
10487 * Examine each vm_map_entry_t in the range.
10489 for (; entry
!= vm_map_to_entry(map
) && entry
->vme_start
< end
;
10490 entry
= entry
->vme_next
) {
10492 * Sanity check on the VM map entry.
10494 if (! vm_map_entry_is_reusable(entry
)) {
10495 vm_map_unlock_read(map
);
10496 vm_page_stats_reusable
.reuse_pages_failure
++;
10497 return KERN_INVALID_ADDRESS
;
10501 * The first time through, the start address could be anywhere
10502 * within the vm_map_entry we found. So adjust the offset to
10505 if (entry
->vme_start
< start
) {
10506 start_offset
= start
- entry
->vme_start
;
10510 end_offset
= MIN(end
, entry
->vme_end
) - entry
->vme_start
;
10511 start_offset
+= entry
->offset
;
10512 end_offset
+= entry
->offset
;
10514 object
= entry
->object
.vm_object
;
10515 if (object
!= VM_OBJECT_NULL
) {
10516 vm_object_lock(object
);
10517 vm_object_reuse_pages(object
, start_offset
, end_offset
,
10519 vm_object_unlock(object
);
10522 if (entry
->alias
== VM_MEMORY_MALLOC_LARGE_REUSABLE
) {
10525 * We do not hold the VM map exclusively here.
10526 * The "alias" field is not that critical, so it's
10527 * safe to update it here, as long as it is the only
10528 * one that can be modified while holding the VM map
10531 entry
->alias
= VM_MEMORY_MALLOC_LARGE_REUSED
;
10535 vm_map_unlock_read(map
);
10536 vm_page_stats_reusable
.reuse_pages_success
++;
10537 return KERN_SUCCESS
;
10541 static kern_return_t
10542 vm_map_reusable_pages(
10544 vm_map_offset_t start
,
10545 vm_map_offset_t end
)
10547 vm_map_entry_t entry
;
10548 vm_object_t object
;
10549 vm_object_offset_t start_offset
, end_offset
;
10552 * The MADV_REUSABLE operation doesn't require any changes to the
10553 * vm_map_entry_t's, so the read lock is sufficient.
10556 vm_map_lock_read(map
);
10559 * The madvise semantics require that the address range be fully
10560 * allocated with no holes. Otherwise, we're required to return
10564 if (!vm_map_range_check(map
, start
, end
, &entry
)) {
10565 vm_map_unlock_read(map
);
10566 vm_page_stats_reusable
.reusable_pages_failure
++;
10567 return KERN_INVALID_ADDRESS
;
10571 * Examine each vm_map_entry_t in the range.
10573 for (; entry
!= vm_map_to_entry(map
) && entry
->vme_start
< end
;
10574 entry
= entry
->vme_next
) {
10575 int kill_pages
= 0;
10578 * Sanity check on the VM map entry.
10580 if (! vm_map_entry_is_reusable(entry
)) {
10581 vm_map_unlock_read(map
);
10582 vm_page_stats_reusable
.reusable_pages_failure
++;
10583 return KERN_INVALID_ADDRESS
;
10587 * The first time through, the start address could be anywhere
10588 * within the vm_map_entry we found. So adjust the offset to
10591 if (entry
->vme_start
< start
) {
10592 start_offset
= start
- entry
->vme_start
;
10596 end_offset
= MIN(end
, entry
->vme_end
) - entry
->vme_start
;
10597 start_offset
+= entry
->offset
;
10598 end_offset
+= entry
->offset
;
10600 object
= entry
->object
.vm_object
;
10601 if (object
== VM_OBJECT_NULL
)
10605 vm_object_lock(object
);
10606 if (object
->ref_count
== 1 && !object
->shadow
)
10610 if (kill_pages
!= -1) {
10611 vm_object_deactivate_pages(object
,
10613 end_offset
- start_offset
,
10615 TRUE
/*reusable_pages*/);
10617 vm_page_stats_reusable
.reusable_pages_shared
++;
10619 vm_object_unlock(object
);
10621 if (entry
->alias
== VM_MEMORY_MALLOC_LARGE
||
10622 entry
->alias
== VM_MEMORY_MALLOC_LARGE_REUSED
) {
10625 * We do not hold the VM map exclusively here.
10626 * The "alias" field is not that critical, so it's
10627 * safe to update it here, as long as it is the only
10628 * one that can be modified while holding the VM map
10631 entry
->alias
= VM_MEMORY_MALLOC_LARGE_REUSABLE
;
10635 vm_map_unlock_read(map
);
10636 vm_page_stats_reusable
.reusable_pages_success
++;
10637 return KERN_SUCCESS
;
10641 static kern_return_t
10644 vm_map_offset_t start
,
10645 vm_map_offset_t end
)
10647 vm_map_entry_t entry
;
10650 * The MADV_REUSABLE operation doesn't require any changes to the
10651 * vm_map_entry_t's, so the read lock is sufficient.
10654 vm_map_lock_read(map
);
10657 * The madvise semantics require that the address range be fully
10658 * allocated with no holes. Otherwise, we're required to return
10662 if (!vm_map_range_check(map
, start
, end
, &entry
)) {
10663 vm_map_unlock_read(map
);
10664 vm_page_stats_reusable
.can_reuse_failure
++;
10665 return KERN_INVALID_ADDRESS
;
10669 * Examine each vm_map_entry_t in the range.
10671 for (; entry
!= vm_map_to_entry(map
) && entry
->vme_start
< end
;
10672 entry
= entry
->vme_next
) {
10674 * Sanity check on the VM map entry.
10676 if (! vm_map_entry_is_reusable(entry
)) {
10677 vm_map_unlock_read(map
);
10678 vm_page_stats_reusable
.can_reuse_failure
++;
10679 return KERN_INVALID_ADDRESS
;
10683 vm_map_unlock_read(map
);
10684 vm_page_stats_reusable
.can_reuse_success
++;
10685 return KERN_SUCCESS
;
10690 #include <mach_kdb.h>
10692 #include <ddb/db_output.h>
10693 #include <vm/vm_print.h>
10695 #define printf db_printf
10698 * Forward declarations for internal functions.
10700 extern void vm_map_links_print(
10701 struct vm_map_links
*links
);
10703 extern void vm_map_header_print(
10704 struct vm_map_header
*header
);
10706 extern void vm_map_entry_print(
10707 vm_map_entry_t entry
);
10709 extern void vm_follow_entry(
10710 vm_map_entry_t entry
);
10712 extern void vm_follow_map(
10716 * vm_map_links_print: [ debug ]
10719 vm_map_links_print(
10720 struct vm_map_links
*links
)
10722 iprintf("prev = %08X next = %08X start = %016llX end = %016llX\n",
10725 (unsigned long long)links
->start
,
10726 (unsigned long long)links
->end
);
10730 * vm_map_header_print: [ debug ]
10733 vm_map_header_print(
10734 struct vm_map_header
*header
)
10736 vm_map_links_print(&header
->links
);
10737 iprintf("nentries = %08X, %sentries_pageable\n",
10739 (header
->entries_pageable
? "" : "!"));
10743 * vm_follow_entry: [ debug ]
10747 vm_map_entry_t entry
)
10751 iprintf("map entry %08X\n", entry
);
10755 shadows
= vm_follow_object(entry
->object
.vm_object
);
10756 iprintf("Total objects : %d\n",shadows
);
10762 * vm_map_entry_print: [ debug ]
10765 vm_map_entry_print(
10766 register vm_map_entry_t entry
)
10768 static const char *inheritance_name
[4] =
10769 { "share", "copy", "none", "?"};
10770 static const char *behavior_name
[4] =
10771 { "dflt", "rand", "seqtl", "rseqntl" };
10773 iprintf("map entry %08X - prev = %08X next = %08X\n", entry
, entry
->vme_prev
, entry
->vme_next
);
10777 vm_map_links_print(&entry
->links
);
10779 iprintf("start = %016llX end = %016llX - prot=%x/%x/%s\n",
10780 (unsigned long long)entry
->vme_start
,
10781 (unsigned long long)entry
->vme_end
,
10783 entry
->max_protection
,
10784 inheritance_name
[(entry
->inheritance
& 0x3)]);
10786 iprintf("behavior = %s, wired_count = %d, user_wired_count = %d\n",
10787 behavior_name
[(entry
->behavior
& 0x3)],
10788 entry
->wired_count
,
10789 entry
->user_wired_count
);
10790 iprintf("%sin_transition, %sneeds_wakeup\n",
10791 (entry
->in_transition
? "" : "!"),
10792 (entry
->needs_wakeup
? "" : "!"));
10794 if (entry
->is_sub_map
) {
10795 iprintf("submap = %08X - offset = %016llX\n",
10796 entry
->object
.sub_map
,
10797 (unsigned long long)entry
->offset
);
10799 iprintf("object = %08X offset = %016llX - ",
10800 entry
->object
.vm_object
,
10801 (unsigned long long)entry
->offset
);
10802 printf("%sis_shared, %sneeds_copy\n",
10803 (entry
->is_shared
? "" : "!"),
10804 (entry
->needs_copy
? "" : "!"));
10811 * vm_follow_map: [ debug ]
10817 register vm_map_entry_t entry
;
10819 iprintf("task map %08X\n", map
);
10823 for (entry
= vm_map_first_entry(map
);
10824 entry
&& entry
!= vm_map_to_entry(map
);
10825 entry
= entry
->vme_next
) {
10826 vm_follow_entry(entry
);
10833 * vm_map_print: [ debug ]
10839 register vm_map_entry_t entry
;
10843 #endif /* TASK_SWAPPER */
10845 map
= (vm_map_t
)(long)
10846 inmap
; /* Make sure we have the right type */
10848 iprintf("task map %08X\n", map
);
10852 vm_map_header_print(&map
->hdr
);
10854 iprintf("pmap = %08X size = %08X ref = %d hint = %08X first_free = %08X\n",
10861 iprintf("%swait_for_space, %swiring_required, timestamp = %d\n",
10862 (map
->wait_for_space
? "" : "!"),
10863 (map
->wiring_required
? "" : "!"),
10867 switch (map
->sw_state
) {
10872 swstate
= "SW_OUT";
10878 iprintf("res = %d, sw_state = %s\n", map
->res_count
, swstate
);
10879 #endif /* TASK_SWAPPER */
10881 for (entry
= vm_map_first_entry(map
);
10882 entry
&& entry
!= vm_map_to_entry(map
);
10883 entry
= entry
->vme_next
) {
10884 vm_map_entry_print(entry
);
10891 * Routine: vm_map_copy_print
10893 * Pretty-print a copy object for ddb.
10900 vm_map_copy_t copy
;
10901 vm_map_entry_t entry
;
10903 copy
= (vm_map_copy_t
)(long)
10904 incopy
; /* Make sure we have the right type */
10906 printf("copy object 0x%x\n", copy
);
10910 iprintf("type=%d", copy
->type
);
10911 switch (copy
->type
) {
10912 case VM_MAP_COPY_ENTRY_LIST
:
10913 printf("[entry_list]");
10916 case VM_MAP_COPY_OBJECT
:
10917 printf("[object]");
10920 case VM_MAP_COPY_KERNEL_BUFFER
:
10921 printf("[kernel_buffer]");
10925 printf("[bad type]");
10928 printf(", offset=0x%llx", (unsigned long long)copy
->offset
);
10929 printf(", size=0x%x\n", copy
->size
);
10931 switch (copy
->type
) {
10932 case VM_MAP_COPY_ENTRY_LIST
:
10933 vm_map_header_print(©
->cpy_hdr
);
10934 for (entry
= vm_map_copy_first_entry(copy
);
10935 entry
&& entry
!= vm_map_copy_to_entry(copy
);
10936 entry
= entry
->vme_next
) {
10937 vm_map_entry_print(entry
);
10941 case VM_MAP_COPY_OBJECT
:
10942 iprintf("object=0x%x\n", copy
->cpy_object
);
10945 case VM_MAP_COPY_KERNEL_BUFFER
:
10946 iprintf("kernel buffer=0x%x", copy
->cpy_kdata
);
10947 printf(", kalloc_size=0x%x\n", copy
->cpy_kalloc_size
);
10956 * db_vm_map_total_size(map) [ debug ]
10958 * return the total virtual size (in bytes) of the map
10961 db_vm_map_total_size(
10964 vm_map_entry_t entry
;
10965 vm_map_size_t total
;
10968 map
= (vm_map_t
)(long)
10969 inmap
; /* Make sure we have the right type */
10972 for (entry
= vm_map_first_entry(map
);
10973 entry
!= vm_map_to_entry(map
);
10974 entry
= entry
->vme_next
) {
10975 total
+= entry
->vme_end
- entry
->vme_start
;
10981 #endif /* MACH_KDB */
10984 * Routine: vm_map_entry_insert
10986 * Descritpion: This routine inserts a new vm_entry in a locked map.
10989 vm_map_entry_insert(
10991 vm_map_entry_t insp_entry
,
10992 vm_map_offset_t start
,
10993 vm_map_offset_t end
,
10994 vm_object_t object
,
10995 vm_object_offset_t offset
,
10996 boolean_t needs_copy
,
10997 boolean_t is_shared
,
10998 boolean_t in_transition
,
10999 vm_prot_t cur_protection
,
11000 vm_prot_t max_protection
,
11001 vm_behavior_t behavior
,
11002 vm_inherit_t inheritance
,
11003 unsigned wired_count
,
11004 boolean_t no_cache
,
11005 boolean_t permanent
,
11006 unsigned int superpage_size
)
11008 vm_map_entry_t new_entry
;
11010 assert(insp_entry
!= (vm_map_entry_t
)0);
11012 new_entry
= vm_map_entry_create(map
);
11014 new_entry
->vme_start
= start
;
11015 new_entry
->vme_end
= end
;
11016 assert(page_aligned(new_entry
->vme_start
));
11017 assert(page_aligned(new_entry
->vme_end
));
11019 new_entry
->object
.vm_object
= object
;
11020 new_entry
->offset
= offset
;
11021 new_entry
->is_shared
= is_shared
;
11022 new_entry
->is_sub_map
= FALSE
;
11023 new_entry
->needs_copy
= needs_copy
;
11024 new_entry
->in_transition
= in_transition
;
11025 new_entry
->needs_wakeup
= FALSE
;
11026 new_entry
->inheritance
= inheritance
;
11027 new_entry
->protection
= cur_protection
;
11028 new_entry
->max_protection
= max_protection
;
11029 new_entry
->behavior
= behavior
;
11030 new_entry
->wired_count
= wired_count
;
11031 new_entry
->user_wired_count
= 0;
11032 new_entry
->use_pmap
= FALSE
;
11033 new_entry
->alias
= 0;
11034 new_entry
->zero_wired_pages
= FALSE
;
11035 new_entry
->no_cache
= no_cache
;
11036 new_entry
->permanent
= permanent
;
11037 new_entry
->superpage_size
= superpage_size
;
11038 new_entry
->used_for_jit
= FALSE
;
11041 * Insert the new entry into the list.
11044 vm_map_store_entry_link(map
, insp_entry
, new_entry
);
11045 map
->size
+= end
- start
;
11048 * Update the free space hint and the lookup hint.
11051 SAVE_HINT_MAP_WRITE(map
, new_entry
);
11056 * Routine: vm_map_remap_extract
11058 * Descritpion: This routine returns a vm_entry list from a map.
11060 static kern_return_t
11061 vm_map_remap_extract(
11063 vm_map_offset_t addr
,
11064 vm_map_size_t size
,
11066 struct vm_map_header
*map_header
,
11067 vm_prot_t
*cur_protection
,
11068 vm_prot_t
*max_protection
,
11069 /* What, no behavior? */
11070 vm_inherit_t inheritance
,
11071 boolean_t pageable
)
11073 kern_return_t result
;
11074 vm_map_size_t mapped_size
;
11075 vm_map_size_t tmp_size
;
11076 vm_map_entry_t src_entry
; /* result of last map lookup */
11077 vm_map_entry_t new_entry
;
11078 vm_object_offset_t offset
;
11079 vm_map_offset_t map_address
;
11080 vm_map_offset_t src_start
; /* start of entry to map */
11081 vm_map_offset_t src_end
; /* end of region to be mapped */
11082 vm_object_t object
;
11083 vm_map_version_t version
;
11084 boolean_t src_needs_copy
;
11085 boolean_t new_entry_needs_copy
;
11087 assert(map
!= VM_MAP_NULL
);
11088 assert(size
!= 0 && size
== vm_map_round_page(size
));
11089 assert(inheritance
== VM_INHERIT_NONE
||
11090 inheritance
== VM_INHERIT_COPY
||
11091 inheritance
== VM_INHERIT_SHARE
);
11094 * Compute start and end of region.
11096 src_start
= vm_map_trunc_page(addr
);
11097 src_end
= vm_map_round_page(src_start
+ size
);
11100 * Initialize map_header.
11102 map_header
->links
.next
= (struct vm_map_entry
*)&map_header
->links
;
11103 map_header
->links
.prev
= (struct vm_map_entry
*)&map_header
->links
;
11104 map_header
->nentries
= 0;
11105 map_header
->entries_pageable
= pageable
;
11107 vm_map_store_init( map_header
);
11109 *cur_protection
= VM_PROT_ALL
;
11110 *max_protection
= VM_PROT_ALL
;
11114 result
= KERN_SUCCESS
;
11117 * The specified source virtual space might correspond to
11118 * multiple map entries, need to loop on them.
11121 while (mapped_size
!= size
) {
11122 vm_map_size_t entry_size
;
11125 * Find the beginning of the region.
11127 if (! vm_map_lookup_entry(map
, src_start
, &src_entry
)) {
11128 result
= KERN_INVALID_ADDRESS
;
11132 if (src_start
< src_entry
->vme_start
||
11133 (mapped_size
&& src_start
!= src_entry
->vme_start
)) {
11134 result
= KERN_INVALID_ADDRESS
;
11138 tmp_size
= size
- mapped_size
;
11139 if (src_end
> src_entry
->vme_end
)
11140 tmp_size
-= (src_end
- src_entry
->vme_end
);
11142 entry_size
= (vm_map_size_t
)(src_entry
->vme_end
-
11143 src_entry
->vme_start
);
11145 if(src_entry
->is_sub_map
) {
11146 vm_map_reference(src_entry
->object
.sub_map
);
11147 object
= VM_OBJECT_NULL
;
11149 object
= src_entry
->object
.vm_object
;
11151 if (object
== VM_OBJECT_NULL
) {
11152 object
= vm_object_allocate(entry_size
);
11153 src_entry
->offset
= 0;
11154 src_entry
->object
.vm_object
= object
;
11155 } else if (object
->copy_strategy
!=
11156 MEMORY_OBJECT_COPY_SYMMETRIC
) {
11158 * We are already using an asymmetric
11159 * copy, and therefore we already have
11160 * the right object.
11162 assert(!src_entry
->needs_copy
);
11163 } else if (src_entry
->needs_copy
|| object
->shadowed
||
11164 (object
->internal
&& !object
->true_share
&&
11165 !src_entry
->is_shared
&&
11166 object
->vo_size
> entry_size
)) {
11168 vm_object_shadow(&src_entry
->object
.vm_object
,
11169 &src_entry
->offset
,
11172 if (!src_entry
->needs_copy
&&
11173 (src_entry
->protection
& VM_PROT_WRITE
)) {
11176 prot
= src_entry
->protection
& ~VM_PROT_WRITE
;
11178 if (override_nx(map
, src_entry
->alias
) && prot
)
11179 prot
|= VM_PROT_EXECUTE
;
11182 vm_object_pmap_protect(
11183 src_entry
->object
.vm_object
,
11187 src_entry
->vme_start
,
11190 pmap_protect(vm_map_pmap(map
),
11191 src_entry
->vme_start
,
11192 src_entry
->vme_end
,
11197 object
= src_entry
->object
.vm_object
;
11198 src_entry
->needs_copy
= FALSE
;
11202 vm_object_lock(object
);
11203 vm_object_reference_locked(object
); /* object ref. for new entry */
11204 if (object
->copy_strategy
==
11205 MEMORY_OBJECT_COPY_SYMMETRIC
) {
11206 object
->copy_strategy
=
11207 MEMORY_OBJECT_COPY_DELAY
;
11209 vm_object_unlock(object
);
11212 offset
= src_entry
->offset
+ (src_start
- src_entry
->vme_start
);
11214 new_entry
= _vm_map_entry_create(map_header
);
11215 vm_map_entry_copy(new_entry
, src_entry
);
11216 new_entry
->use_pmap
= FALSE
; /* clr address space specifics */
11218 new_entry
->vme_start
= map_address
;
11219 new_entry
->vme_end
= map_address
+ tmp_size
;
11220 new_entry
->inheritance
= inheritance
;
11221 new_entry
->offset
= offset
;
11224 * The new region has to be copied now if required.
11228 src_entry
->is_shared
= TRUE
;
11229 new_entry
->is_shared
= TRUE
;
11230 if (!(new_entry
->is_sub_map
))
11231 new_entry
->needs_copy
= FALSE
;
11233 } else if (src_entry
->is_sub_map
) {
11234 /* make this a COW sub_map if not already */
11235 new_entry
->needs_copy
= TRUE
;
11236 object
= VM_OBJECT_NULL
;
11237 } else if (src_entry
->wired_count
== 0 &&
11238 vm_object_copy_quickly(&new_entry
->object
.vm_object
,
11240 (new_entry
->vme_end
-
11241 new_entry
->vme_start
),
11243 &new_entry_needs_copy
)) {
11245 new_entry
->needs_copy
= new_entry_needs_copy
;
11246 new_entry
->is_shared
= FALSE
;
11249 * Handle copy_on_write semantics.
11251 if (src_needs_copy
&& !src_entry
->needs_copy
) {
11254 prot
= src_entry
->protection
& ~VM_PROT_WRITE
;
11256 if (override_nx(map
, src_entry
->alias
) && prot
)
11257 prot
|= VM_PROT_EXECUTE
;
11259 vm_object_pmap_protect(object
,
11262 ((src_entry
->is_shared
11264 PMAP_NULL
: map
->pmap
),
11265 src_entry
->vme_start
,
11268 src_entry
->needs_copy
= TRUE
;
11271 * Throw away the old object reference of the new entry.
11273 vm_object_deallocate(object
);
11276 new_entry
->is_shared
= FALSE
;
11279 * The map can be safely unlocked since we
11280 * already hold a reference on the object.
11282 * Record the timestamp of the map for later
11283 * verification, and unlock the map.
11285 version
.main_timestamp
= map
->timestamp
;
11286 vm_map_unlock(map
); /* Increments timestamp once! */
11289 * Perform the copy.
11291 if (src_entry
->wired_count
> 0) {
11292 vm_object_lock(object
);
11293 result
= vm_object_copy_slowly(
11298 &new_entry
->object
.vm_object
);
11300 new_entry
->offset
= 0;
11301 new_entry
->needs_copy
= FALSE
;
11303 result
= vm_object_copy_strategically(
11307 &new_entry
->object
.vm_object
,
11308 &new_entry
->offset
,
11309 &new_entry_needs_copy
);
11311 new_entry
->needs_copy
= new_entry_needs_copy
;
11315 * Throw away the old object reference of the new entry.
11317 vm_object_deallocate(object
);
11319 if (result
!= KERN_SUCCESS
&&
11320 result
!= KERN_MEMORY_RESTART_COPY
) {
11321 _vm_map_entry_dispose(map_header
, new_entry
);
11326 * Verify that the map has not substantially
11327 * changed while the copy was being made.
11331 if (version
.main_timestamp
+ 1 != map
->timestamp
) {
11333 * Simple version comparison failed.
11335 * Retry the lookup and verify that the
11336 * same object/offset are still present.
11338 vm_object_deallocate(new_entry
->
11340 _vm_map_entry_dispose(map_header
, new_entry
);
11341 if (result
== KERN_MEMORY_RESTART_COPY
)
11342 result
= KERN_SUCCESS
;
11346 if (result
== KERN_MEMORY_RESTART_COPY
) {
11347 vm_object_reference(object
);
11352 _vm_map_store_entry_link(map_header
,
11353 map_header
->links
.prev
, new_entry
);
11355 /*Protections for submap mapping are irrelevant here*/
11356 if( !src_entry
->is_sub_map
) {
11357 *cur_protection
&= src_entry
->protection
;
11358 *max_protection
&= src_entry
->max_protection
;
11360 map_address
+= tmp_size
;
11361 mapped_size
+= tmp_size
;
11362 src_start
+= tmp_size
;
11366 vm_map_unlock(map
);
11367 if (result
!= KERN_SUCCESS
) {
11369 * Free all allocated elements.
11371 for (src_entry
= map_header
->links
.next
;
11372 src_entry
!= (struct vm_map_entry
*)&map_header
->links
;
11373 src_entry
= new_entry
) {
11374 new_entry
= src_entry
->vme_next
;
11375 _vm_map_store_entry_unlink(map_header
, src_entry
);
11376 vm_object_deallocate(src_entry
->object
.vm_object
);
11377 _vm_map_entry_dispose(map_header
, src_entry
);
11384 * Routine: vm_remap
11386 * Map portion of a task's address space.
11387 * Mapped region must not overlap more than
11388 * one vm memory object. Protections and
11389 * inheritance attributes remain the same
11390 * as in the original task and are out parameters.
11391 * Source and Target task can be identical
11392 * Other attributes are identical as for vm_map()
11396 vm_map_t target_map
,
11397 vm_map_address_t
*address
,
11398 vm_map_size_t size
,
11399 vm_map_offset_t mask
,
11402 vm_map_offset_t memory_address
,
11404 vm_prot_t
*cur_protection
,
11405 vm_prot_t
*max_protection
,
11406 vm_inherit_t inheritance
)
11408 kern_return_t result
;
11409 vm_map_entry_t entry
;
11410 vm_map_entry_t insp_entry
= VM_MAP_ENTRY_NULL
;
11411 vm_map_entry_t new_entry
;
11412 struct vm_map_header map_header
;
11414 if (target_map
== VM_MAP_NULL
)
11415 return KERN_INVALID_ARGUMENT
;
11417 switch (inheritance
) {
11418 case VM_INHERIT_NONE
:
11419 case VM_INHERIT_COPY
:
11420 case VM_INHERIT_SHARE
:
11421 if (size
!= 0 && src_map
!= VM_MAP_NULL
)
11425 return KERN_INVALID_ARGUMENT
;
11428 size
= vm_map_round_page(size
);
11430 result
= vm_map_remap_extract(src_map
, memory_address
,
11431 size
, copy
, &map_header
,
11438 if (result
!= KERN_SUCCESS
) {
11443 * Allocate/check a range of free virtual address
11444 * space for the target
11446 *address
= vm_map_trunc_page(*address
);
11447 vm_map_lock(target_map
);
11448 result
= vm_map_remap_range_allocate(target_map
, address
, size
,
11449 mask
, flags
, &insp_entry
);
11451 for (entry
= map_header
.links
.next
;
11452 entry
!= (struct vm_map_entry
*)&map_header
.links
;
11453 entry
= new_entry
) {
11454 new_entry
= entry
->vme_next
;
11455 _vm_map_store_entry_unlink(&map_header
, entry
);
11456 if (result
== KERN_SUCCESS
) {
11457 entry
->vme_start
+= *address
;
11458 entry
->vme_end
+= *address
;
11459 vm_map_store_entry_link(target_map
, insp_entry
, entry
);
11460 insp_entry
= entry
;
11462 if (!entry
->is_sub_map
) {
11463 vm_object_deallocate(entry
->object
.vm_object
);
11465 vm_map_deallocate(entry
->object
.sub_map
);
11467 _vm_map_entry_dispose(&map_header
, entry
);
11471 if( target_map
->disable_vmentry_reuse
== TRUE
) {
11472 if( target_map
->highest_entry_end
< insp_entry
->vme_end
){
11473 target_map
->highest_entry_end
= insp_entry
->vme_end
;
11477 if (result
== KERN_SUCCESS
) {
11478 target_map
->size
+= size
;
11479 SAVE_HINT_MAP_WRITE(target_map
, insp_entry
);
11481 vm_map_unlock(target_map
);
11483 if (result
== KERN_SUCCESS
&& target_map
->wiring_required
)
11484 result
= vm_map_wire(target_map
, *address
,
11485 *address
+ size
, *cur_protection
, TRUE
);
11490 * Routine: vm_map_remap_range_allocate
11493 * Allocate a range in the specified virtual address map.
11494 * returns the address and the map entry just before the allocated
11497 * Map must be locked.
11500 static kern_return_t
11501 vm_map_remap_range_allocate(
11503 vm_map_address_t
*address
, /* IN/OUT */
11504 vm_map_size_t size
,
11505 vm_map_offset_t mask
,
11507 vm_map_entry_t
*map_entry
) /* OUT */
11509 vm_map_entry_t entry
;
11510 vm_map_offset_t start
;
11511 vm_map_offset_t end
;
11518 if (flags
& VM_FLAGS_ANYWHERE
)
11521 * Calculate the first possible address.
11524 if (start
< map
->min_offset
)
11525 start
= map
->min_offset
;
11526 if (start
> map
->max_offset
)
11527 return(KERN_NO_SPACE
);
11530 * Look for the first possible address;
11531 * if there's already something at this
11532 * address, we have to start after it.
11535 if( map
->disable_vmentry_reuse
== TRUE
) {
11536 VM_MAP_HIGHEST_ENTRY(map
, entry
, start
);
11538 assert(first_free_is_valid(map
));
11539 if (start
== map
->min_offset
) {
11540 if ((entry
= map
->first_free
) != vm_map_to_entry(map
))
11541 start
= entry
->vme_end
;
11543 vm_map_entry_t tmp_entry
;
11544 if (vm_map_lookup_entry(map
, start
, &tmp_entry
))
11545 start
= tmp_entry
->vme_end
;
11551 * In any case, the "entry" always precedes
11552 * the proposed new region throughout the
11557 register vm_map_entry_t next
;
11560 * Find the end of the proposed new region.
11561 * Be sure we didn't go beyond the end, or
11562 * wrap around the address.
11565 end
= ((start
+ mask
) & ~mask
);
11567 return(KERN_NO_SPACE
);
11571 if ((end
> map
->max_offset
) || (end
< start
)) {
11572 if (map
->wait_for_space
) {
11573 if (size
<= (map
->max_offset
-
11574 map
->min_offset
)) {
11575 assert_wait((event_t
) map
, THREAD_INTERRUPTIBLE
);
11576 vm_map_unlock(map
);
11577 thread_block(THREAD_CONTINUE_NULL
);
11583 return(KERN_NO_SPACE
);
11587 * If there are no more entries, we must win.
11590 next
= entry
->vme_next
;
11591 if (next
== vm_map_to_entry(map
))
11595 * If there is another entry, it must be
11596 * after the end of the potential new region.
11599 if (next
->vme_start
>= end
)
11603 * Didn't fit -- move to the next entry.
11607 start
= entry
->vme_end
;
11611 vm_map_entry_t temp_entry
;
11615 * the address doesn't itself violate
11616 * the mask requirement.
11619 if ((start
& mask
) != 0)
11620 return(KERN_NO_SPACE
);
11624 * ... the address is within bounds
11627 end
= start
+ size
;
11629 if ((start
< map
->min_offset
) ||
11630 (end
> map
->max_offset
) ||
11632 return(KERN_INVALID_ADDRESS
);
11636 * If we're asked to overwrite whatever was mapped in that
11637 * range, first deallocate that range.
11639 if (flags
& VM_FLAGS_OVERWRITE
) {
11643 * We use a "zap_map" to avoid having to unlock
11644 * the "map" in vm_map_delete(), which would compromise
11645 * the atomicity of the "deallocate" and then "remap"
11648 zap_map
= vm_map_create(PMAP_NULL
,
11651 map
->hdr
.entries_pageable
);
11652 if (zap_map
== VM_MAP_NULL
) {
11653 return KERN_RESOURCE_SHORTAGE
;
11656 kr
= vm_map_delete(map
, start
, end
,
11657 VM_MAP_REMOVE_SAVE_ENTRIES
,
11659 if (kr
== KERN_SUCCESS
) {
11660 vm_map_destroy(zap_map
,
11661 VM_MAP_REMOVE_NO_PMAP_CLEANUP
);
11662 zap_map
= VM_MAP_NULL
;
11667 * ... the starting address isn't allocated
11670 if (vm_map_lookup_entry(map
, start
, &temp_entry
))
11671 return(KERN_NO_SPACE
);
11673 entry
= temp_entry
;
11676 * ... the next region doesn't overlap the
11680 if ((entry
->vme_next
!= vm_map_to_entry(map
)) &&
11681 (entry
->vme_next
->vme_start
< end
))
11682 return(KERN_NO_SPACE
);
11684 *map_entry
= entry
;
11685 return(KERN_SUCCESS
);
11691 * Set the address map for the current thread to the specified map
11699 thread_t thread
= current_thread();
11700 vm_map_t oldmap
= thread
->map
;
11702 mp_disable_preemption();
11703 mycpu
= cpu_number();
11706 * Deactivate the current map and activate the requested map
11708 PMAP_SWITCH_USER(thread
, map
, mycpu
);
11710 mp_enable_preemption();
11716 * Routine: vm_map_write_user
11719 * Copy out data from a kernel space into space in the
11720 * destination map. The space must already exist in the
11722 * NOTE: This routine should only be called by threads
11723 * which can block on a page fault. i.e. kernel mode user
11731 vm_map_address_t dst_addr
,
11734 kern_return_t kr
= KERN_SUCCESS
;
11736 if(current_map() == map
) {
11737 if (copyout(src_p
, dst_addr
, size
)) {
11738 kr
= KERN_INVALID_ADDRESS
;
11743 /* take on the identity of the target map while doing */
11746 vm_map_reference(map
);
11747 oldmap
= vm_map_switch(map
);
11748 if (copyout(src_p
, dst_addr
, size
)) {
11749 kr
= KERN_INVALID_ADDRESS
;
11751 vm_map_switch(oldmap
);
11752 vm_map_deallocate(map
);
11758 * Routine: vm_map_read_user
11761 * Copy in data from a user space source map into the
11762 * kernel map. The space must already exist in the
11764 * NOTE: This routine should only be called by threads
11765 * which can block on a page fault. i.e. kernel mode user
11772 vm_map_address_t src_addr
,
11776 kern_return_t kr
= KERN_SUCCESS
;
11778 if(current_map() == map
) {
11779 if (copyin(src_addr
, dst_p
, size
)) {
11780 kr
= KERN_INVALID_ADDRESS
;
11785 /* take on the identity of the target map while doing */
11788 vm_map_reference(map
);
11789 oldmap
= vm_map_switch(map
);
11790 if (copyin(src_addr
, dst_p
, size
)) {
11791 kr
= KERN_INVALID_ADDRESS
;
11793 vm_map_switch(oldmap
);
11794 vm_map_deallocate(map
);
11801 * vm_map_check_protection:
11803 * Assert that the target map allows the specified
11804 * privilege on the entire address region given.
11805 * The entire region must be allocated.
11808 vm_map_check_protection(vm_map_t map
, vm_map_offset_t start
,
11809 vm_map_offset_t end
, vm_prot_t protection
)
11811 vm_map_entry_t entry
;
11812 vm_map_entry_t tmp_entry
;
11816 if (start
< vm_map_min(map
) || end
> vm_map_max(map
) || start
> end
)
11818 vm_map_unlock(map
);
11822 if (!vm_map_lookup_entry(map
, start
, &tmp_entry
)) {
11823 vm_map_unlock(map
);
11829 while (start
< end
) {
11830 if (entry
== vm_map_to_entry(map
)) {
11831 vm_map_unlock(map
);
11836 * No holes allowed!
11839 if (start
< entry
->vme_start
) {
11840 vm_map_unlock(map
);
11845 * Check protection associated with entry.
11848 if ((entry
->protection
& protection
) != protection
) {
11849 vm_map_unlock(map
);
11853 /* go to next entry */
11855 start
= entry
->vme_end
;
11856 entry
= entry
->vme_next
;
11858 vm_map_unlock(map
);
11863 vm_map_purgable_control(
11865 vm_map_offset_t address
,
11866 vm_purgable_t control
,
11869 vm_map_entry_t entry
;
11870 vm_object_t object
;
11874 * Vet all the input parameters and current type and state of the
11875 * underlaying object. Return with an error if anything is amiss.
11877 if (map
== VM_MAP_NULL
)
11878 return(KERN_INVALID_ARGUMENT
);
11880 if (control
!= VM_PURGABLE_SET_STATE
&&
11881 control
!= VM_PURGABLE_GET_STATE
&&
11882 control
!= VM_PURGABLE_PURGE_ALL
)
11883 return(KERN_INVALID_ARGUMENT
);
11885 if (control
== VM_PURGABLE_PURGE_ALL
) {
11886 vm_purgeable_object_purge_all();
11887 return KERN_SUCCESS
;
11890 if (control
== VM_PURGABLE_SET_STATE
&&
11891 (((*state
& ~(VM_PURGABLE_ALL_MASKS
)) != 0) ||
11892 ((*state
& VM_PURGABLE_STATE_MASK
) > VM_PURGABLE_STATE_MASK
)))
11893 return(KERN_INVALID_ARGUMENT
);
11895 vm_map_lock_read(map
);
11897 if (!vm_map_lookup_entry(map
, address
, &entry
) || entry
->is_sub_map
) {
11900 * Must pass a valid non-submap address.
11902 vm_map_unlock_read(map
);
11903 return(KERN_INVALID_ADDRESS
);
11906 if ((entry
->protection
& VM_PROT_WRITE
) == 0) {
11908 * Can't apply purgable controls to something you can't write.
11910 vm_map_unlock_read(map
);
11911 return(KERN_PROTECTION_FAILURE
);
11914 object
= entry
->object
.vm_object
;
11915 if (object
== VM_OBJECT_NULL
) {
11917 * Object must already be present or it can't be purgable.
11919 vm_map_unlock_read(map
);
11920 return KERN_INVALID_ARGUMENT
;
11923 vm_object_lock(object
);
11925 if (entry
->offset
!= 0 ||
11926 entry
->vme_end
- entry
->vme_start
!= object
->vo_size
) {
11928 * Can only apply purgable controls to the whole (existing)
11931 vm_map_unlock_read(map
);
11932 vm_object_unlock(object
);
11933 return KERN_INVALID_ARGUMENT
;
11936 vm_map_unlock_read(map
);
11938 kr
= vm_object_purgable_control(object
, control
, state
);
11940 vm_object_unlock(object
);
11946 vm_map_page_query_internal(
11947 vm_map_t target_map
,
11948 vm_map_offset_t offset
,
11953 vm_page_info_basic_data_t info
;
11954 mach_msg_type_number_t count
;
11956 count
= VM_PAGE_INFO_BASIC_COUNT
;
11957 kr
= vm_map_page_info(target_map
,
11959 VM_PAGE_INFO_BASIC
,
11960 (vm_page_info_t
) &info
,
11962 if (kr
== KERN_SUCCESS
) {
11963 *disposition
= info
.disposition
;
11964 *ref_count
= info
.ref_count
;
11976 vm_map_offset_t offset
,
11977 vm_page_info_flavor_t flavor
,
11978 vm_page_info_t info
,
11979 mach_msg_type_number_t
*count
)
11981 vm_map_entry_t map_entry
;
11982 vm_object_t object
;
11985 kern_return_t retval
= KERN_SUCCESS
;
11986 boolean_t top_object
;
11989 vm_object_id_t object_id
;
11990 vm_page_info_basic_t basic_info
;
11992 vm_map_offset_t offset_in_page
;
11995 case VM_PAGE_INFO_BASIC
:
11996 if (*count
!= VM_PAGE_INFO_BASIC_COUNT
) {
11998 * The "vm_page_info_basic_data" structure was not
11999 * properly padded, so allow the size to be off by
12000 * one to maintain backwards binary compatibility...
12002 if (*count
!= VM_PAGE_INFO_BASIC_COUNT
- 1)
12003 return KERN_INVALID_ARGUMENT
;
12007 return KERN_INVALID_ARGUMENT
;
12016 retval
= KERN_SUCCESS
;
12017 offset_in_page
= offset
& PAGE_MASK
;
12018 offset
= vm_map_trunc_page(offset
);
12020 vm_map_lock_read(map
);
12023 * First, find the map entry covering "offset", going down
12024 * submaps if necessary.
12027 if (!vm_map_lookup_entry(map
, offset
, &map_entry
)) {
12028 vm_map_unlock_read(map
);
12029 return KERN_INVALID_ADDRESS
;
12031 /* compute offset from this map entry's start */
12032 offset
-= map_entry
->vme_start
;
12033 /* compute offset into this map entry's object (or submap) */
12034 offset
+= map_entry
->offset
;
12036 if (map_entry
->is_sub_map
) {
12039 sub_map
= map_entry
->object
.sub_map
;
12040 vm_map_lock_read(sub_map
);
12041 vm_map_unlock_read(map
);
12045 ref_count
= MAX(ref_count
, map
->ref_count
);
12051 object
= map_entry
->object
.vm_object
;
12052 if (object
== VM_OBJECT_NULL
) {
12053 /* no object -> no page */
12054 vm_map_unlock_read(map
);
12058 vm_object_lock(object
);
12059 vm_map_unlock_read(map
);
12062 * Go down the VM object shadow chain until we find the page
12063 * we're looking for.
12066 ref_count
= MAX(ref_count
, object
->ref_count
);
12068 m
= vm_page_lookup(object
, offset
);
12070 if (m
!= VM_PAGE_NULL
) {
12071 disposition
|= VM_PAGE_QUERY_PAGE_PRESENT
;
12075 if (object
->existence_map
) {
12076 if (vm_external_state_get(object
->existence_map
,
12078 VM_EXTERNAL_STATE_EXISTS
) {
12080 * this page has been paged out
12082 disposition
|= VM_PAGE_QUERY_PAGE_PAGED_OUT
;
12088 if (object
->internal
&&
12090 !object
->terminating
&&
12091 object
->pager_ready
) {
12093 memory_object_t pager
;
12095 vm_object_paging_begin(object
);
12096 pager
= object
->pager
;
12097 vm_object_unlock(object
);
12100 * Ask the default pager if
12101 * it has this page.
12103 kr
= memory_object_data_request(
12105 offset
+ object
->paging_offset
,
12106 0, /* just poke the pager */
12110 vm_object_lock(object
);
12111 vm_object_paging_end(object
);
12113 if (kr
== KERN_SUCCESS
) {
12114 /* the default pager has it */
12115 disposition
|= VM_PAGE_QUERY_PAGE_PAGED_OUT
;
12121 if (object
->shadow
!= VM_OBJECT_NULL
) {
12122 vm_object_t shadow
;
12124 offset
+= object
->vo_shadow_offset
;
12125 shadow
= object
->shadow
;
12127 vm_object_lock(shadow
);
12128 vm_object_unlock(object
);
12131 top_object
= FALSE
;
12134 // if (!object->internal)
12136 // retval = KERN_FAILURE;
12137 // goto done_with_object;
12142 /* The ref_count is not strictly accurate, it measures the number */
12143 /* of entities holding a ref on the object, they may not be mapping */
12144 /* the object or may not be mapping the section holding the */
12145 /* target page but its still a ball park number and though an over- */
12146 /* count, it picks up the copy-on-write cases */
12148 /* We could also get a picture of page sharing from pmap_attributes */
12149 /* but this would under count as only faulted-in mappings would */
12152 if (top_object
== TRUE
&& object
->shadow
)
12153 disposition
|= VM_PAGE_QUERY_PAGE_COPIED
;
12155 if (! object
->internal
)
12156 disposition
|= VM_PAGE_QUERY_PAGE_EXTERNAL
;
12158 if (m
== VM_PAGE_NULL
)
12159 goto done_with_object
;
12161 if (m
->fictitious
) {
12162 disposition
|= VM_PAGE_QUERY_PAGE_FICTITIOUS
;
12163 goto done_with_object
;
12165 if (m
->dirty
|| pmap_is_modified(m
->phys_page
))
12166 disposition
|= VM_PAGE_QUERY_PAGE_DIRTY
;
12168 if (m
->reference
|| pmap_is_referenced(m
->phys_page
))
12169 disposition
|= VM_PAGE_QUERY_PAGE_REF
;
12171 if (m
->speculative
)
12172 disposition
|= VM_PAGE_QUERY_PAGE_SPECULATIVE
;
12174 if (m
->cs_validated
)
12175 disposition
|= VM_PAGE_QUERY_PAGE_CS_VALIDATED
;
12177 disposition
|= VM_PAGE_QUERY_PAGE_CS_TAINTED
;
12180 vm_object_unlock(object
);
12184 case VM_PAGE_INFO_BASIC
:
12185 basic_info
= (vm_page_info_basic_t
) info
;
12186 basic_info
->disposition
= disposition
;
12187 basic_info
->ref_count
= ref_count
;
12188 basic_info
->object_id
= (vm_object_id_t
) (uintptr_t) object
;
12189 basic_info
->offset
=
12190 (memory_object_offset_t
) offset
+ offset_in_page
;
12191 basic_info
->depth
= depth
;
12201 * Synchronises the memory range specified with its backing store
12202 * image by either flushing or cleaning the contents to the appropriate
12203 * memory manager engaging in a memory object synchronize dialog with
12204 * the manager. The client doesn't return until the manager issues
12205 * m_o_s_completed message. MIG Magically converts user task parameter
12206 * to the task's address map.
12208 * interpretation of sync_flags
12209 * VM_SYNC_INVALIDATE - discard pages, only return precious
12210 * pages to manager.
12212 * VM_SYNC_INVALIDATE & (VM_SYNC_SYNCHRONOUS | VM_SYNC_ASYNCHRONOUS)
12213 * - discard pages, write dirty or precious
12214 * pages back to memory manager.
12216 * VM_SYNC_SYNCHRONOUS | VM_SYNC_ASYNCHRONOUS
12217 * - write dirty or precious pages back to
12218 * the memory manager.
12220 * VM_SYNC_CONTIGUOUS - does everything normally, but if there
12221 * is a hole in the region, and we would
12222 * have returned KERN_SUCCESS, return
12223 * KERN_INVALID_ADDRESS instead.
12226 * The memory object attributes have not yet been implemented, this
12227 * function will have to deal with the invalidate attribute
12230 * KERN_INVALID_TASK Bad task parameter
12231 * KERN_INVALID_ARGUMENT both sync and async were specified.
12232 * KERN_SUCCESS The usual.
12233 * KERN_INVALID_ADDRESS There was a hole in the region.
12239 vm_map_address_t address
,
12240 vm_map_size_t size
,
12241 vm_sync_t sync_flags
)
12244 msync_req_t new_msr
;
12245 queue_chain_t req_q
; /* queue of requests for this msync */
12246 vm_map_entry_t entry
;
12247 vm_map_size_t amount_left
;
12248 vm_object_offset_t offset
;
12249 boolean_t do_sync_req
;
12250 boolean_t had_hole
= FALSE
;
12251 memory_object_t pager
;
12253 if ((sync_flags
& VM_SYNC_ASYNCHRONOUS
) &&
12254 (sync_flags
& VM_SYNC_SYNCHRONOUS
))
12255 return(KERN_INVALID_ARGUMENT
);
12258 * align address and size on page boundaries
12260 size
= vm_map_round_page(address
+ size
) - vm_map_trunc_page(address
);
12261 address
= vm_map_trunc_page(address
);
12263 if (map
== VM_MAP_NULL
)
12264 return(KERN_INVALID_TASK
);
12267 return(KERN_SUCCESS
);
12269 queue_init(&req_q
);
12270 amount_left
= size
;
12272 while (amount_left
> 0) {
12273 vm_object_size_t flush_size
;
12274 vm_object_t object
;
12277 if (!vm_map_lookup_entry(map
,
12278 vm_map_trunc_page(address
), &entry
)) {
12280 vm_map_size_t skip
;
12283 * hole in the address map.
12288 * Check for empty map.
12290 if (entry
== vm_map_to_entry(map
) &&
12291 entry
->vme_next
== entry
) {
12292 vm_map_unlock(map
);
12296 * Check that we don't wrap and that
12297 * we have at least one real map entry.
12299 if ((map
->hdr
.nentries
== 0) ||
12300 (entry
->vme_next
->vme_start
< address
)) {
12301 vm_map_unlock(map
);
12305 * Move up to the next entry if needed
12307 skip
= (entry
->vme_next
->vme_start
- address
);
12308 if (skip
>= amount_left
)
12311 amount_left
-= skip
;
12312 address
= entry
->vme_next
->vme_start
;
12313 vm_map_unlock(map
);
12317 offset
= address
- entry
->vme_start
;
12320 * do we have more to flush than is contained in this
12323 if (amount_left
+ entry
->vme_start
+ offset
> entry
->vme_end
) {
12324 flush_size
= entry
->vme_end
-
12325 (entry
->vme_start
+ offset
);
12327 flush_size
= amount_left
;
12329 amount_left
-= flush_size
;
12330 address
+= flush_size
;
12332 if (entry
->is_sub_map
== TRUE
) {
12333 vm_map_t local_map
;
12334 vm_map_offset_t local_offset
;
12336 local_map
= entry
->object
.sub_map
;
12337 local_offset
= entry
->offset
;
12338 vm_map_unlock(map
);
12343 sync_flags
) == KERN_INVALID_ADDRESS
) {
12348 object
= entry
->object
.vm_object
;
12351 * We can't sync this object if the object has not been
12354 if (object
== VM_OBJECT_NULL
) {
12355 vm_map_unlock(map
);
12358 offset
+= entry
->offset
;
12360 vm_object_lock(object
);
12362 if (sync_flags
& (VM_SYNC_KILLPAGES
| VM_SYNC_DEACTIVATE
)) {
12363 int kill_pages
= 0;
12364 boolean_t reusable_pages
= FALSE
;
12366 if (sync_flags
& VM_SYNC_KILLPAGES
) {
12367 if (object
->ref_count
== 1 && !object
->shadow
)
12372 if (kill_pages
!= -1)
12373 vm_object_deactivate_pages(object
, offset
,
12374 (vm_object_size_t
)flush_size
, kill_pages
, reusable_pages
);
12375 vm_object_unlock(object
);
12376 vm_map_unlock(map
);
12380 * We can't sync this object if there isn't a pager.
12381 * Don't bother to sync internal objects, since there can't
12382 * be any "permanent" storage for these objects anyway.
12384 if ((object
->pager
== MEMORY_OBJECT_NULL
) ||
12385 (object
->internal
) || (object
->private)) {
12386 vm_object_unlock(object
);
12387 vm_map_unlock(map
);
12391 * keep reference on the object until syncing is done
12393 vm_object_reference_locked(object
);
12394 vm_object_unlock(object
);
12396 vm_map_unlock(map
);
12398 do_sync_req
= vm_object_sync(object
,
12401 sync_flags
& VM_SYNC_INVALIDATE
,
12402 ((sync_flags
& VM_SYNC_SYNCHRONOUS
) ||
12403 (sync_flags
& VM_SYNC_ASYNCHRONOUS
)),
12404 sync_flags
& VM_SYNC_SYNCHRONOUS
);
12406 * only send a m_o_s if we returned pages or if the entry
12407 * is writable (ie dirty pages may have already been sent back)
12409 if (!do_sync_req
) {
12410 if ((sync_flags
& VM_SYNC_INVALIDATE
) && object
->resident_page_count
== 0) {
12412 * clear out the clustering and read-ahead hints
12414 vm_object_lock(object
);
12416 object
->pages_created
= 0;
12417 object
->pages_used
= 0;
12418 object
->sequential
= 0;
12419 object
->last_alloc
= 0;
12421 vm_object_unlock(object
);
12423 vm_object_deallocate(object
);
12426 msync_req_alloc(new_msr
);
12428 vm_object_lock(object
);
12429 offset
+= object
->paging_offset
;
12431 new_msr
->offset
= offset
;
12432 new_msr
->length
= flush_size
;
12433 new_msr
->object
= object
;
12434 new_msr
->flag
= VM_MSYNC_SYNCHRONIZING
;
12438 * We can't sync this object if there isn't a pager. The
12439 * pager can disappear anytime we're not holding the object
12440 * lock. So this has to be checked anytime we goto re_iterate.
12443 pager
= object
->pager
;
12445 if (pager
== MEMORY_OBJECT_NULL
) {
12446 vm_object_unlock(object
);
12447 vm_object_deallocate(object
);
12451 queue_iterate(&object
->msr_q
, msr
, msync_req_t
, msr_q
) {
12453 * need to check for overlapping entry, if found, wait
12454 * on overlapping msr to be done, then reiterate
12457 if (msr
->flag
== VM_MSYNC_SYNCHRONIZING
&&
12458 ((offset
>= msr
->offset
&&
12459 offset
< (msr
->offset
+ msr
->length
)) ||
12460 (msr
->offset
>= offset
&&
12461 msr
->offset
< (offset
+ flush_size
))))
12463 assert_wait((event_t
) msr
,THREAD_INTERRUPTIBLE
);
12465 vm_object_unlock(object
);
12466 thread_block(THREAD_CONTINUE_NULL
);
12467 vm_object_lock(object
);
12471 }/* queue_iterate */
12473 queue_enter(&object
->msr_q
, new_msr
, msync_req_t
, msr_q
);
12475 vm_object_paging_begin(object
);
12476 vm_object_unlock(object
);
12478 queue_enter(&req_q
, new_msr
, msync_req_t
, req_q
);
12480 (void) memory_object_synchronize(
12484 sync_flags
& ~VM_SYNC_CONTIGUOUS
);
12486 vm_object_lock(object
);
12487 vm_object_paging_end(object
);
12488 vm_object_unlock(object
);
12492 * wait for memory_object_sychronize_completed messages from pager(s)
12495 while (!queue_empty(&req_q
)) {
12496 msr
= (msync_req_t
)queue_first(&req_q
);
12498 while(msr
->flag
!= VM_MSYNC_DONE
) {
12499 assert_wait((event_t
) msr
, THREAD_INTERRUPTIBLE
);
12501 thread_block(THREAD_CONTINUE_NULL
);
12504 queue_remove(&req_q
, msr
, msync_req_t
, req_q
);
12506 vm_object_deallocate(msr
->object
);
12507 msync_req_free(msr
);
12508 }/* queue_iterate */
12510 /* for proper msync() behaviour */
12511 if (had_hole
== TRUE
&& (sync_flags
& VM_SYNC_CONTIGUOUS
))
12512 return(KERN_INVALID_ADDRESS
);
12514 return(KERN_SUCCESS
);
12518 * Routine: convert_port_entry_to_map
12520 * Convert from a port specifying an entry or a task
12521 * to a map. Doesn't consume the port ref; produces a map ref,
12522 * which may be null. Unlike convert_port_to_map, the
12523 * port may be task or a named entry backed.
12530 convert_port_entry_to_map(
12534 vm_named_entry_t named_entry
;
12535 uint32_t try_failed_count
= 0;
12537 if(IP_VALID(port
) && (ip_kotype(port
) == IKOT_NAMED_ENTRY
)) {
12540 if(ip_active(port
) && (ip_kotype(port
)
12541 == IKOT_NAMED_ENTRY
)) {
12543 (vm_named_entry_t
)port
->ip_kobject
;
12544 if (!(lck_mtx_try_lock(&(named_entry
)->Lock
))) {
12547 try_failed_count
++;
12548 mutex_pause(try_failed_count
);
12551 named_entry
->ref_count
++;
12552 lck_mtx_unlock(&(named_entry
)->Lock
);
12554 if ((named_entry
->is_sub_map
) &&
12555 (named_entry
->protection
12556 & VM_PROT_WRITE
)) {
12557 map
= named_entry
->backing
.map
;
12559 mach_destroy_memory_entry(port
);
12560 return VM_MAP_NULL
;
12562 vm_map_reference_swap(map
);
12563 mach_destroy_memory_entry(port
);
12567 return VM_MAP_NULL
;
12571 map
= convert_port_to_map(port
);
12577 * Routine: convert_port_entry_to_object
12579 * Convert from a port specifying a named entry to an
12580 * object. Doesn't consume the port ref; produces a map ref,
12581 * which may be null.
12588 convert_port_entry_to_object(
12591 vm_object_t object
;
12592 vm_named_entry_t named_entry
;
12593 uint32_t try_failed_count
= 0;
12595 if(IP_VALID(port
) && (ip_kotype(port
) == IKOT_NAMED_ENTRY
)) {
12598 if(ip_active(port
) && (ip_kotype(port
)
12599 == IKOT_NAMED_ENTRY
)) {
12601 (vm_named_entry_t
)port
->ip_kobject
;
12602 if (!(lck_mtx_try_lock(&(named_entry
)->Lock
))) {
12605 try_failed_count
++;
12606 mutex_pause(try_failed_count
);
12609 named_entry
->ref_count
++;
12610 lck_mtx_unlock(&(named_entry
)->Lock
);
12612 if ((!named_entry
->is_sub_map
) &&
12613 (!named_entry
->is_pager
) &&
12614 (named_entry
->protection
12615 & VM_PROT_WRITE
)) {
12616 object
= named_entry
->backing
.object
;
12618 mach_destroy_memory_entry(port
);
12619 return (vm_object_t
)NULL
;
12621 vm_object_reference(named_entry
->backing
.object
);
12622 mach_destroy_memory_entry(port
);
12626 return (vm_object_t
)NULL
;
12629 return (vm_object_t
)NULL
;
12636 * Export routines to other components for the things we access locally through
12643 return (current_map_fast());
12647 * vm_map_reference:
12649 * Most code internal to the osfmk will go through a
12650 * macro defining this. This is always here for the
12651 * use of other kernel components.
12653 #undef vm_map_reference
12656 register vm_map_t map
)
12658 if (map
== VM_MAP_NULL
)
12661 lck_mtx_lock(&map
->s_lock
);
12663 assert(map
->res_count
> 0);
12664 assert(map
->ref_count
>= map
->res_count
);
12668 lck_mtx_unlock(&map
->s_lock
);
12672 * vm_map_deallocate:
12674 * Removes a reference from the specified map,
12675 * destroying it if no references remain.
12676 * The map should not be locked.
12680 register vm_map_t map
)
12684 if (map
== VM_MAP_NULL
)
12687 lck_mtx_lock(&map
->s_lock
);
12688 ref
= --map
->ref_count
;
12690 vm_map_res_deallocate(map
);
12691 lck_mtx_unlock(&map
->s_lock
);
12694 assert(map
->ref_count
== 0);
12695 lck_mtx_unlock(&map
->s_lock
);
12699 * The map residence count isn't decremented here because
12700 * the vm_map_delete below will traverse the entire map,
12701 * deleting entries, and the residence counts on objects
12702 * and sharing maps will go away then.
12706 vm_map_destroy(map
, VM_MAP_NO_FLAGS
);
12711 vm_map_disable_NX(vm_map_t map
)
12715 if (map
->pmap
== NULL
)
12718 pmap_disable_NX(map
->pmap
);
12722 vm_map_disallow_data_exec(vm_map_t map
)
12727 map
->map_disallow_data_exec
= TRUE
;
12730 /* XXX Consider making these constants (VM_MAX_ADDRESS and MACH_VM_MAX_ADDRESS)
12731 * more descriptive.
12734 vm_map_set_32bit(vm_map_t map
)
12736 map
->max_offset
= (vm_map_offset_t
)VM_MAX_ADDRESS
;
12741 vm_map_set_64bit(vm_map_t map
)
12743 map
->max_offset
= (vm_map_offset_t
)MACH_VM_MAX_ADDRESS
;
12747 vm_compute_max_offset(unsigned is64
)
12749 return (is64
? (vm_map_offset_t
)MACH_VM_MAX_ADDRESS
: (vm_map_offset_t
)VM_MAX_ADDRESS
);
12756 return map
->max_offset
> ((vm_map_offset_t
)VM_MAX_ADDRESS
);
12760 vm_map_has_4GB_pagezero(
12765 * We should lock the VM map (for read) here but we can get away
12766 * with it for now because there can't really be any race condition:
12767 * the VM map's min_offset is changed only when the VM map is created
12768 * and when the zero page is established (when the binary gets loaded),
12769 * and this routine gets called only when the task terminates and the
12770 * VM map is being torn down, and when a new map is created via
12771 * load_machfile()/execve().
12773 return (map
->min_offset
>= 0x100000000ULL
);
12777 vm_map_set_4GB_pagezero(vm_map_t map
)
12779 #if defined(__i386__)
12780 pmap_set_4GB_pagezero(map
->pmap
);
12782 #pragma unused(map)
12788 vm_map_clear_4GB_pagezero(vm_map_t map
)
12790 #if defined(__i386__)
12791 pmap_clear_4GB_pagezero(map
->pmap
);
12793 #pragma unused(map)
12798 * Raise a VM map's minimum offset.
12799 * To strictly enforce "page zero" reservation.
12802 vm_map_raise_min_offset(
12804 vm_map_offset_t new_min_offset
)
12806 vm_map_entry_t first_entry
;
12808 new_min_offset
= vm_map_round_page(new_min_offset
);
12812 if (new_min_offset
< map
->min_offset
) {
12814 * Can't move min_offset backwards, as that would expose
12815 * a part of the address space that was previously, and for
12816 * possibly good reasons, inaccessible.
12818 vm_map_unlock(map
);
12819 return KERN_INVALID_ADDRESS
;
12822 first_entry
= vm_map_first_entry(map
);
12823 if (first_entry
!= vm_map_to_entry(map
) &&
12824 first_entry
->vme_start
< new_min_offset
) {
12826 * Some memory was already allocated below the new
12827 * minimun offset. It's too late to change it now...
12829 vm_map_unlock(map
);
12830 return KERN_NO_SPACE
;
12833 map
->min_offset
= new_min_offset
;
12835 vm_map_unlock(map
);
12837 return KERN_SUCCESS
;
12841 * Set the limit on the maximum amount of user wired memory allowed for this map.
12842 * This is basically a copy of the MEMLOCK rlimit value maintained by the BSD side of
12843 * the kernel. The limits are checked in the mach VM side, so we keep a copy so we
12844 * don't have to reach over to the BSD data structures.
12848 vm_map_set_user_wire_limit(vm_map_t map
,
12851 map
->user_wire_limit
= limit
;
12855 void vm_map_switch_protect(vm_map_t map
,
12859 map
->switch_protect
=val
;
12860 vm_map_unlock(map
);
12863 /* Add (generate) code signature for memory range */
12864 #if CONFIG_DYNAMIC_CODE_SIGNING
12865 kern_return_t
vm_map_sign(vm_map_t map
,
12866 vm_map_offset_t start
,
12867 vm_map_offset_t end
)
12869 vm_map_entry_t entry
;
12871 vm_object_t object
;
12874 * Vet all the input parameters and current type and state of the
12875 * underlaying object. Return with an error if anything is amiss.
12877 if (map
== VM_MAP_NULL
)
12878 return(KERN_INVALID_ARGUMENT
);
12880 vm_map_lock_read(map
);
12882 if (!vm_map_lookup_entry(map
, start
, &entry
) || entry
->is_sub_map
) {
12884 * Must pass a valid non-submap address.
12886 vm_map_unlock_read(map
);
12887 return(KERN_INVALID_ADDRESS
);
12890 if((entry
->vme_start
> start
) || (entry
->vme_end
< end
)) {
12892 * Map entry doesn't cover the requested range. Not handling
12893 * this situation currently.
12895 vm_map_unlock_read(map
);
12896 return(KERN_INVALID_ARGUMENT
);
12899 object
= entry
->object
.vm_object
;
12900 if (object
== VM_OBJECT_NULL
) {
12902 * Object must already be present or we can't sign.
12904 vm_map_unlock_read(map
);
12905 return KERN_INVALID_ARGUMENT
;
12908 vm_object_lock(object
);
12909 vm_map_unlock_read(map
);
12911 while(start
< end
) {
12914 m
= vm_page_lookup(object
, start
- entry
->vme_start
+ entry
->offset
);
12915 if (m
==VM_PAGE_NULL
) {
12916 /* shoud we try to fault a page here? we can probably
12917 * demand it exists and is locked for this request */
12918 vm_object_unlock(object
);
12919 return KERN_FAILURE
;
12921 /* deal with special page status */
12923 (m
->unusual
&& (m
->error
|| m
->restart
|| m
->private || m
->absent
))) {
12924 vm_object_unlock(object
);
12925 return KERN_FAILURE
;
12928 /* Page is OK... now "validate" it */
12929 /* This is the place where we'll call out to create a code
12930 * directory, later */
12931 m
->cs_validated
= TRUE
;
12933 /* The page is now "clean" for codesigning purposes. That means
12934 * we don't consider it as modified (wpmapped) anymore. But
12935 * we'll disconnect the page so we note any future modification
12937 m
->wpmapped
= FALSE
;
12938 refmod
= pmap_disconnect(m
->phys_page
);
12940 /* Pull the dirty status from the pmap, since we cleared the
12942 if ((refmod
& VM_MEM_MODIFIED
) && !m
->dirty
) {
12946 /* On to the next page */
12947 start
+= PAGE_SIZE
;
12949 vm_object_unlock(object
);
12951 return KERN_SUCCESS
;
12957 kern_return_t
vm_map_freeze_walk(
12959 unsigned int *purgeable_count
,
12960 unsigned int *wired_count
,
12961 unsigned int *clean_count
,
12962 unsigned int *dirty_count
,
12963 boolean_t
*has_shared
)
12965 vm_map_entry_t entry
;
12967 vm_map_lock_read(map
);
12969 *purgeable_count
= *wired_count
= *clean_count
= *dirty_count
= 0;
12970 *has_shared
= FALSE
;
12972 for (entry
= vm_map_first_entry(map
);
12973 entry
!= vm_map_to_entry(map
);
12974 entry
= entry
->vme_next
) {
12975 unsigned int purgeable
, clean
, dirty
, wired
;
12978 if ((entry
->object
.vm_object
== 0) ||
12979 (entry
->is_sub_map
) ||
12980 (entry
->object
.vm_object
->phys_contiguous
)) {
12984 vm_object_pack(&purgeable
, &wired
, &clean
, &dirty
, &shared
, entry
->object
.vm_object
, VM_OBJECT_NULL
, NULL
, NULL
);
12986 *purgeable_count
+= purgeable
;
12987 *wired_count
+= wired
;
12988 *clean_count
+= clean
;
12989 *dirty_count
+= dirty
;
12992 *has_shared
= TRUE
;
12996 vm_map_unlock_read(map
);
12998 return KERN_SUCCESS
;
13001 kern_return_t
vm_map_freeze(
13003 unsigned int *purgeable_count
,
13004 unsigned int *wired_count
,
13005 unsigned int *clean_count
,
13006 unsigned int *dirty_count
,
13007 boolean_t
*has_shared
)
13009 vm_map_entry_t entry2
= VM_MAP_ENTRY_NULL
;
13010 vm_object_t compact_object
= VM_OBJECT_NULL
;
13011 vm_object_offset_t offset
= 0x0;
13012 kern_return_t kr
= KERN_SUCCESS
;
13013 void *default_freezer_toc
= NULL
;
13014 boolean_t cleanup
= FALSE
;
13016 *purgeable_count
= *wired_count
= *clean_count
= *dirty_count
= 0;
13017 *has_shared
= FALSE
;
13019 /* Create our compact object */
13020 compact_object
= vm_object_allocate((vm_map_offset_t
)(VM_MAX_ADDRESS
) - (vm_map_offset_t
)(VM_MIN_ADDRESS
));
13021 if (!compact_object
) {
13026 default_freezer_toc
= default_freezer_mapping_create(compact_object
, offset
);
13027 if (!default_freezer_toc
) {
13033 * We need the exclusive lock here so that we can
13034 * block any page faults or lookups while we are
13035 * in the middle of freezing this vm map.
13039 if (map
->default_freezer_toc
!= NULL
){
13041 * This map has already been frozen.
13048 /* Get a mapping in place for the freezing about to commence */
13049 map
->default_freezer_toc
= default_freezer_toc
;
13051 vm_object_lock(compact_object
);
13053 for (entry2
= vm_map_first_entry(map
);
13054 entry2
!= vm_map_to_entry(map
);
13055 entry2
= entry2
->vme_next
) {
13057 vm_object_t src_object
= entry2
->object
.vm_object
;
13059 /* If eligible, scan the entry, moving eligible pages over to our parent object */
13060 if (entry2
->object
.vm_object
&& !entry2
->is_sub_map
&& !entry2
->object
.vm_object
->phys_contiguous
) {
13061 unsigned int purgeable
, clean
, dirty
, wired
;
13064 vm_object_pack(&purgeable
, &wired
, &clean
, &dirty
, &shared
,
13065 src_object
, compact_object
, &default_freezer_toc
, &offset
);
13067 *purgeable_count
+= purgeable
;
13068 *wired_count
+= wired
;
13069 *clean_count
+= clean
;
13070 *dirty_count
+= dirty
;
13073 *has_shared
= TRUE
;
13078 vm_object_unlock(compact_object
);
13080 /* Finally, throw out the pages to swap */
13081 vm_object_pageout(compact_object
);
13084 vm_map_unlock(map
);
13086 /* Unwind if there was a failure */
13087 if ((cleanup
) || (KERN_SUCCESS
!= kr
)) {
13088 if (default_freezer_toc
){
13089 default_freezer_mapping_free(&map
->default_freezer_toc
, TRUE
);
13091 if (compact_object
){
13092 vm_object_deallocate(compact_object
);
13099 __private_extern__ vm_object_t
default_freezer_get_compact_vm_object( void** );
13105 void **default_freezer_toc
;
13106 vm_object_t compact_object
;
13110 if (map
->default_freezer_toc
== NULL
){
13112 * This map is not in a frozen state.
13117 default_freezer_toc
= &(map
->default_freezer_toc
);
13119 compact_object
= default_freezer_get_compact_vm_object(default_freezer_toc
);
13121 /* Bring the pages back in */
13122 vm_object_pagein(compact_object
);
13124 /* Shift pages back to their original objects */
13125 vm_object_unpack(compact_object
, default_freezer_toc
);
13127 vm_object_deallocate(compact_object
);
13129 map
->default_freezer_toc
= NULL
;
13132 vm_map_unlock(map
);