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 (entry
->alias
>= VM_MEMORY_MALLOC
&&
6628 entry
->alias
<= VM_MEMORY_MALLOC_LARGE_REUSED
) {
6629 vm_object_t new_object
, new_shadow
;
6632 * We're about to map something over a mapping
6633 * established by malloc()...
6635 new_object
= copy_entry
->object
.vm_object
;
6636 if (new_object
!= VM_OBJECT_NULL
) {
6637 vm_object_lock_shared(new_object
);
6639 while (new_object
!= VM_OBJECT_NULL
&&
6640 new_object
->internal
) {
6641 new_shadow
= new_object
->shadow
;
6642 if (new_shadow
== VM_OBJECT_NULL
) {
6645 vm_object_lock_shared(new_shadow
);
6646 vm_object_unlock(new_object
);
6647 new_object
= new_shadow
;
6649 if (new_object
!= VM_OBJECT_NULL
) {
6650 if (!new_object
->internal
) {
6652 * The new mapping is backed
6653 * by an external object. We
6654 * don't want malloc'ed memory
6655 * to be replaced with such a
6656 * non-anonymous mapping, so
6657 * let's go off the optimized
6660 vm_object_unlock(new_object
);
6663 vm_object_unlock(new_object
);
6666 * The new mapping is still backed by
6667 * anonymous (internal) memory, so it's
6668 * OK to substitute it for the original
6673 if (old_object
!= VM_OBJECT_NULL
) {
6674 if(entry
->is_sub_map
) {
6675 if(entry
->use_pmap
) {
6676 #ifndef NO_NESTED_PMAP
6677 pmap_unnest(dst_map
->pmap
,
6678 (addr64_t
)entry
->vme_start
,
6679 entry
->vme_end
- entry
->vme_start
);
6680 #endif /* NO_NESTED_PMAP */
6681 if(dst_map
->mapped
) {
6682 /* clean up parent */
6684 vm_map_submap_pmap_clean(
6685 dst_map
, entry
->vme_start
,
6687 entry
->object
.sub_map
,
6691 vm_map_submap_pmap_clean(
6692 dst_map
, entry
->vme_start
,
6694 entry
->object
.sub_map
,
6698 entry
->object
.sub_map
);
6700 if(dst_map
->mapped
) {
6701 vm_object_pmap_protect(
6702 entry
->object
.vm_object
,
6710 pmap_remove(dst_map
->pmap
,
6711 (addr64_t
)(entry
->vme_start
),
6712 (addr64_t
)(entry
->vme_end
));
6714 vm_object_deallocate(old_object
);
6718 entry
->is_sub_map
= FALSE
;
6719 entry
->object
= copy_entry
->object
;
6720 object
= entry
->object
.vm_object
;
6721 entry
->needs_copy
= copy_entry
->needs_copy
;
6722 entry
->wired_count
= 0;
6723 entry
->user_wired_count
= 0;
6724 offset
= entry
->offset
= copy_entry
->offset
;
6726 vm_map_copy_entry_unlink(copy
, copy_entry
);
6727 vm_map_copy_entry_dispose(copy
, copy_entry
);
6730 * we could try to push pages into the pmap at this point, BUT
6731 * this optimization only saved on average 2 us per page if ALL
6732 * the pages in the source were currently mapped
6733 * and ALL the pages in the dest were touched, if there were fewer
6734 * than 2/3 of the pages touched, this optimization actually cost more cycles
6735 * it also puts a lot of pressure on the pmap layer w/r to mapping structures
6739 * Set up for the next iteration. The map
6740 * has not been unlocked, so the next
6741 * address should be at the end of this
6742 * entry, and the next map entry should be
6743 * the one following it.
6746 start
= tmp_entry
->vme_end
;
6747 tmp_entry
= tmp_entry
->vme_next
;
6749 vm_map_version_t version
;
6750 vm_object_t dst_object
;
6751 vm_object_offset_t dst_offset
;
6755 dst_object
= entry
->object
.vm_object
;
6756 dst_offset
= entry
->offset
;
6759 * Take an object reference, and record
6760 * the map version information so that the
6761 * map can be safely unlocked.
6764 if (dst_object
== VM_OBJECT_NULL
) {
6766 * We would usually have just taken the
6767 * optimized path above if the destination
6768 * object has not been allocated yet. But we
6769 * now disable that optimization if the copy
6770 * entry's object is not backed by anonymous
6771 * memory to avoid replacing malloc'ed
6772 * (i.e. re-usable) anonymous memory with a
6773 * not-so-anonymous mapping.
6774 * So we have to handle this case here and
6775 * allocate a new VM object for this map entry.
6777 dst_object
= vm_object_allocate(
6778 entry
->vme_end
- entry
->vme_start
);
6780 entry
->object
.vm_object
= dst_object
;
6781 entry
->offset
= dst_offset
;
6785 vm_object_reference(dst_object
);
6787 /* account for unlock bumping up timestamp */
6788 version
.main_timestamp
= dst_map
->timestamp
+ 1;
6790 vm_map_unlock(dst_map
);
6793 * Copy as much as possible in one pass
6798 copy_entry
->object
.vm_object
,
6808 * Release the object reference
6811 vm_object_deallocate(dst_object
);
6814 * If a hard error occurred, return it now
6817 if (r
!= KERN_SUCCESS
)
6820 if (copy_size
!= 0) {
6822 * Dispose of the copied region
6825 vm_map_copy_clip_end(copy
, copy_entry
,
6826 copy_entry
->vme_start
+ copy_size
);
6827 vm_map_copy_entry_unlink(copy
, copy_entry
);
6828 vm_object_deallocate(copy_entry
->object
.vm_object
);
6829 vm_map_copy_entry_dispose(copy
, copy_entry
);
6833 * Pick up in the destination map where we left off.
6835 * Use the version information to avoid a lookup
6836 * in the normal case.
6840 vm_map_lock(dst_map
);
6841 if (version
.main_timestamp
== dst_map
->timestamp
) {
6842 /* We can safely use saved tmp_entry value */
6844 vm_map_clip_end(dst_map
, tmp_entry
, start
);
6845 tmp_entry
= tmp_entry
->vme_next
;
6847 /* Must do lookup of tmp_entry */
6849 if (!vm_map_lookup_entry(dst_map
, start
, &tmp_entry
)) {
6850 vm_map_unlock(dst_map
);
6851 return(KERN_INVALID_ADDRESS
);
6853 vm_map_clip_start(dst_map
, tmp_entry
, start
);
6858 return(KERN_SUCCESS
);
6859 }/* vm_map_copy_overwrite_aligned */
6862 * Routine: vm_map_copyin_kernel_buffer [internal use only]
6865 * Copy in data to a kernel buffer from space in the
6866 * source map. The original space may be optionally
6869 * If successful, returns a new copy object.
6871 static kern_return_t
6872 vm_map_copyin_kernel_buffer(
6874 vm_map_offset_t src_addr
,
6876 boolean_t src_destroy
,
6877 vm_map_copy_t
*copy_result
)
6881 vm_size_t kalloc_size
;
6883 if ((vm_size_t
) len
!= len
) {
6884 /* "len" is too big and doesn't fit in a "vm_size_t" */
6885 return KERN_RESOURCE_SHORTAGE
;
6887 kalloc_size
= (vm_size_t
) (sizeof(struct vm_map_copy
) + len
);
6888 assert((vm_map_size_t
) kalloc_size
== sizeof (struct vm_map_copy
) + len
);
6890 copy
= (vm_map_copy_t
) kalloc(kalloc_size
);
6891 if (copy
== VM_MAP_COPY_NULL
) {
6892 return KERN_RESOURCE_SHORTAGE
;
6894 copy
->type
= VM_MAP_COPY_KERNEL_BUFFER
;
6897 copy
->cpy_kdata
= (void *) (copy
+ 1);
6898 copy
->cpy_kalloc_size
= kalloc_size
;
6900 kr
= copyinmap(src_map
, src_addr
, copy
->cpy_kdata
, (vm_size_t
) len
);
6901 if (kr
!= KERN_SUCCESS
) {
6902 kfree(copy
, kalloc_size
);
6906 (void) vm_map_remove(src_map
, vm_map_trunc_page(src_addr
),
6907 vm_map_round_page(src_addr
+ len
),
6908 VM_MAP_REMOVE_INTERRUPTIBLE
|
6909 VM_MAP_REMOVE_WAIT_FOR_KWIRE
|
6910 (src_map
== kernel_map
) ?
6911 VM_MAP_REMOVE_KUNWIRE
: 0);
6913 *copy_result
= copy
;
6914 return KERN_SUCCESS
;
6918 * Routine: vm_map_copyout_kernel_buffer [internal use only]
6921 * Copy out data from a kernel buffer into space in the
6922 * destination map. The space may be otpionally dynamically
6925 * If successful, consumes the copy object.
6926 * Otherwise, the caller is responsible for it.
6928 static int vm_map_copyout_kernel_buffer_failures
= 0;
6929 static kern_return_t
6930 vm_map_copyout_kernel_buffer(
6932 vm_map_address_t
*addr
, /* IN/OUT */
6934 boolean_t overwrite
)
6936 kern_return_t kr
= KERN_SUCCESS
;
6937 thread_t thread
= current_thread();
6942 * Allocate space in the target map for the data
6945 kr
= vm_map_enter(map
,
6947 vm_map_round_page(copy
->size
),
6948 (vm_map_offset_t
) 0,
6951 (vm_object_offset_t
) 0,
6955 VM_INHERIT_DEFAULT
);
6956 if (kr
!= KERN_SUCCESS
)
6961 * Copyout the data from the kernel buffer to the target map.
6963 if (thread
->map
== map
) {
6966 * If the target map is the current map, just do
6969 assert((vm_size_t
) copy
->size
== copy
->size
);
6970 if (copyout(copy
->cpy_kdata
, *addr
, (vm_size_t
) copy
->size
)) {
6971 kr
= KERN_INVALID_ADDRESS
;
6978 * If the target map is another map, assume the
6979 * target's address space identity for the duration
6982 vm_map_reference(map
);
6983 oldmap
= vm_map_switch(map
);
6985 assert((vm_size_t
) copy
->size
== copy
->size
);
6986 if (copyout(copy
->cpy_kdata
, *addr
, (vm_size_t
) copy
->size
)) {
6987 vm_map_copyout_kernel_buffer_failures
++;
6988 kr
= KERN_INVALID_ADDRESS
;
6991 (void) vm_map_switch(oldmap
);
6992 vm_map_deallocate(map
);
6995 if (kr
!= KERN_SUCCESS
) {
6996 /* the copy failed, clean up */
6999 * Deallocate the space we allocated in the target map.
7001 (void) vm_map_remove(map
,
7002 vm_map_trunc_page(*addr
),
7003 vm_map_round_page(*addr
+
7004 vm_map_round_page(copy
->size
)),
7009 /* copy was successful, dicard the copy structure */
7010 kfree(copy
, copy
->cpy_kalloc_size
);
7017 * Macro: vm_map_copy_insert
7020 * Link a copy chain ("copy") into a map at the
7021 * specified location (after "where").
7023 * The copy chain is destroyed.
7025 * The arguments are evaluated multiple times.
7027 #define vm_map_copy_insert(map, where, copy) \
7029 vm_map_store_copy_insert(map, where, copy); \
7030 zfree(vm_map_copy_zone, copy); \
7034 * Routine: vm_map_copyout
7037 * Copy out a copy chain ("copy") into newly-allocated
7038 * space in the destination map.
7040 * If successful, consumes the copy object.
7041 * Otherwise, the caller is responsible for it.
7046 vm_map_address_t
*dst_addr
, /* OUT */
7050 vm_map_size_t adjustment
;
7051 vm_map_offset_t start
;
7052 vm_object_offset_t vm_copy_start
;
7053 vm_map_entry_t last
;
7055 vm_map_entry_t entry
;
7058 * Check for null copy object.
7061 if (copy
== VM_MAP_COPY_NULL
) {
7063 return(KERN_SUCCESS
);
7067 * Check for special copy object, created
7068 * by vm_map_copyin_object.
7071 if (copy
->type
== VM_MAP_COPY_OBJECT
) {
7072 vm_object_t object
= copy
->cpy_object
;
7074 vm_object_offset_t offset
;
7076 offset
= vm_object_trunc_page(copy
->offset
);
7077 size
= vm_map_round_page(copy
->size
+
7078 (vm_map_size_t
)(copy
->offset
- offset
));
7080 kr
= vm_map_enter(dst_map
, dst_addr
, size
,
7081 (vm_map_offset_t
) 0, VM_FLAGS_ANYWHERE
,
7082 object
, offset
, FALSE
,
7083 VM_PROT_DEFAULT
, VM_PROT_ALL
,
7084 VM_INHERIT_DEFAULT
);
7085 if (kr
!= KERN_SUCCESS
)
7087 /* Account for non-pagealigned copy object */
7088 *dst_addr
+= (vm_map_offset_t
)(copy
->offset
- offset
);
7089 zfree(vm_map_copy_zone
, copy
);
7090 return(KERN_SUCCESS
);
7094 * Check for special kernel buffer allocated
7095 * by new_ipc_kmsg_copyin.
7098 if (copy
->type
== VM_MAP_COPY_KERNEL_BUFFER
) {
7099 return(vm_map_copyout_kernel_buffer(dst_map
, dst_addr
,
7104 * Find space for the data
7107 vm_copy_start
= vm_object_trunc_page(copy
->offset
);
7108 size
= vm_map_round_page((vm_map_size_t
)copy
->offset
+ copy
->size
)
7113 vm_map_lock(dst_map
);
7114 if( dst_map
->disable_vmentry_reuse
== TRUE
) {
7115 VM_MAP_HIGHEST_ENTRY(dst_map
, entry
, start
);
7118 assert(first_free_is_valid(dst_map
));
7119 start
= ((last
= dst_map
->first_free
) == vm_map_to_entry(dst_map
)) ?
7120 vm_map_min(dst_map
) : last
->vme_end
;
7124 vm_map_entry_t next
= last
->vme_next
;
7125 vm_map_offset_t end
= start
+ size
;
7127 if ((end
> dst_map
->max_offset
) || (end
< start
)) {
7128 if (dst_map
->wait_for_space
) {
7129 if (size
<= (dst_map
->max_offset
- dst_map
->min_offset
)) {
7130 assert_wait((event_t
) dst_map
,
7131 THREAD_INTERRUPTIBLE
);
7132 vm_map_unlock(dst_map
);
7133 thread_block(THREAD_CONTINUE_NULL
);
7137 vm_map_unlock(dst_map
);
7138 return(KERN_NO_SPACE
);
7141 if ((next
== vm_map_to_entry(dst_map
)) ||
7142 (next
->vme_start
>= end
))
7146 start
= last
->vme_end
;
7150 * Since we're going to just drop the map
7151 * entries from the copy into the destination
7152 * map, they must come from the same pool.
7155 if (copy
->cpy_hdr
.entries_pageable
!= dst_map
->hdr
.entries_pageable
) {
7157 * Mismatches occur when dealing with the default
7161 vm_map_entry_t next
, new;
7164 * Find the zone that the copies were allocated from
7166 old_zone
= (copy
->cpy_hdr
.entries_pageable
)
7168 : vm_map_kentry_zone
;
7169 entry
= vm_map_copy_first_entry(copy
);
7172 * Reinitialize the copy so that vm_map_copy_entry_link
7175 vm_map_store_copy_reset(copy
, entry
);
7176 copy
->cpy_hdr
.entries_pageable
= dst_map
->hdr
.entries_pageable
;
7181 while (entry
!= vm_map_copy_to_entry(copy
)) {
7182 new = vm_map_copy_entry_create(copy
);
7183 vm_map_entry_copy_full(new, entry
);
7184 new->use_pmap
= FALSE
; /* clr address space specifics */
7185 vm_map_copy_entry_link(copy
,
7186 vm_map_copy_last_entry(copy
),
7188 next
= entry
->vme_next
;
7189 zfree(old_zone
, entry
);
7195 * Adjust the addresses in the copy chain, and
7196 * reset the region attributes.
7199 adjustment
= start
- vm_copy_start
;
7200 for (entry
= vm_map_copy_first_entry(copy
);
7201 entry
!= vm_map_copy_to_entry(copy
);
7202 entry
= entry
->vme_next
) {
7203 entry
->vme_start
+= adjustment
;
7204 entry
->vme_end
+= adjustment
;
7206 entry
->inheritance
= VM_INHERIT_DEFAULT
;
7207 entry
->protection
= VM_PROT_DEFAULT
;
7208 entry
->max_protection
= VM_PROT_ALL
;
7209 entry
->behavior
= VM_BEHAVIOR_DEFAULT
;
7212 * If the entry is now wired,
7213 * map the pages into the destination map.
7215 if (entry
->wired_count
!= 0) {
7216 register vm_map_offset_t va
;
7217 vm_object_offset_t offset
;
7218 register vm_object_t object
;
7222 object
= entry
->object
.vm_object
;
7223 offset
= entry
->offset
;
7224 va
= entry
->vme_start
;
7226 pmap_pageable(dst_map
->pmap
,
7231 while (va
< entry
->vme_end
) {
7232 register vm_page_t m
;
7235 * Look up the page in the object.
7236 * Assert that the page will be found in the
7239 * the object was newly created by
7240 * vm_object_copy_slowly, and has
7241 * copies of all of the pages from
7244 * the object was moved from the old
7245 * map entry; because the old map
7246 * entry was wired, all of the pages
7247 * were in the top-level object.
7248 * (XXX not true if we wire pages for
7251 vm_object_lock(object
);
7253 m
= vm_page_lookup(object
, offset
);
7254 if (m
== VM_PAGE_NULL
|| !VM_PAGE_WIRED(m
) ||
7256 panic("vm_map_copyout: wiring %p", m
);
7260 * The page is assumed to be wired here, so it
7261 * shouldn't be encrypted. Otherwise, we
7262 * couldn't enter it in the page table, since
7263 * we don't want the user to see the encrypted
7266 ASSERT_PAGE_DECRYPTED(m
);
7268 prot
= entry
->protection
;
7270 if (override_nx(dst_map
, entry
->alias
) && prot
)
7271 prot
|= VM_PROT_EXECUTE
;
7273 type_of_fault
= DBG_CACHE_HIT_FAULT
;
7275 vm_fault_enter(m
, dst_map
->pmap
, va
, prot
, prot
,
7276 VM_PAGE_WIRED(m
), FALSE
, FALSE
, FALSE
,
7279 vm_object_unlock(object
);
7281 offset
+= PAGE_SIZE_64
;
7288 * Correct the page alignment for the result
7291 *dst_addr
= start
+ (copy
->offset
- vm_copy_start
);
7294 * Update the hints and the map size
7297 SAVE_HINT_MAP_WRITE(dst_map
, vm_map_copy_last_entry(copy
));
7299 dst_map
->size
+= size
;
7305 vm_map_copy_insert(dst_map
, last
, copy
);
7307 vm_map_unlock(dst_map
);
7310 * XXX If wiring_required, call vm_map_pageable
7313 return(KERN_SUCCESS
);
7317 * Routine: vm_map_copyin
7320 * see vm_map_copyin_common. Exported via Unsupported.exports.
7324 #undef vm_map_copyin
7329 vm_map_address_t src_addr
,
7331 boolean_t src_destroy
,
7332 vm_map_copy_t
*copy_result
) /* OUT */
7334 return(vm_map_copyin_common(src_map
, src_addr
, len
, src_destroy
,
7335 FALSE
, copy_result
, FALSE
));
7339 * Routine: vm_map_copyin_common
7342 * Copy the specified region (src_addr, len) from the
7343 * source address space (src_map), possibly removing
7344 * the region from the source address space (src_destroy).
7347 * A vm_map_copy_t object (copy_result), suitable for
7348 * insertion into another address space (using vm_map_copyout),
7349 * copying over another address space region (using
7350 * vm_map_copy_overwrite). If the copy is unused, it
7351 * should be destroyed (using vm_map_copy_discard).
7353 * In/out conditions:
7354 * The source map should not be locked on entry.
7357 typedef struct submap_map
{
7358 vm_map_t parent_map
;
7359 vm_map_offset_t base_start
;
7360 vm_map_offset_t base_end
;
7361 vm_map_size_t base_len
;
7362 struct submap_map
*next
;
7366 vm_map_copyin_common(
7368 vm_map_address_t src_addr
,
7370 boolean_t src_destroy
,
7371 __unused boolean_t src_volatile
,
7372 vm_map_copy_t
*copy_result
, /* OUT */
7373 boolean_t use_maxprot
)
7375 vm_map_entry_t tmp_entry
; /* Result of last map lookup --
7376 * in multi-level lookup, this
7377 * entry contains the actual
7381 vm_map_entry_t new_entry
= VM_MAP_ENTRY_NULL
; /* Map entry for copy */
7383 vm_map_offset_t src_start
; /* Start of current entry --
7384 * where copy is taking place now
7386 vm_map_offset_t src_end
; /* End of entire region to be
7388 vm_map_offset_t src_base
;
7389 vm_map_t base_map
= src_map
;
7390 boolean_t map_share
=FALSE
;
7391 submap_map_t
*parent_maps
= NULL
;
7394 vm_map_copy_t copy
; /* Resulting copy */
7395 vm_map_address_t copy_addr
;
7398 * Check for copies of zero bytes.
7402 *copy_result
= VM_MAP_COPY_NULL
;
7403 return(KERN_SUCCESS
);
7407 * Check that the end address doesn't overflow
7409 src_end
= src_addr
+ len
;
7410 if (src_end
< src_addr
)
7411 return KERN_INVALID_ADDRESS
;
7414 * If the copy is sufficiently small, use a kernel buffer instead
7415 * of making a virtual copy. The theory being that the cost of
7416 * setting up VM (and taking C-O-W faults) dominates the copy costs
7417 * for small regions.
7419 if ((len
< msg_ool_size_small
) && !use_maxprot
)
7420 return vm_map_copyin_kernel_buffer(src_map
, src_addr
, len
,
7421 src_destroy
, copy_result
);
7424 * Compute (page aligned) start and end of region
7426 src_start
= vm_map_trunc_page(src_addr
);
7427 src_end
= vm_map_round_page(src_end
);
7429 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);
7432 * Allocate a header element for the list.
7434 * Use the start and end in the header to
7435 * remember the endpoints prior to rounding.
7438 copy
= (vm_map_copy_t
) zalloc(vm_map_copy_zone
);
7439 vm_map_copy_first_entry(copy
) =
7440 vm_map_copy_last_entry(copy
) = vm_map_copy_to_entry(copy
);
7441 copy
->type
= VM_MAP_COPY_ENTRY_LIST
;
7442 copy
->cpy_hdr
.nentries
= 0;
7443 copy
->cpy_hdr
.entries_pageable
= TRUE
;
7445 vm_map_store_init( &(copy
->cpy_hdr
) );
7447 copy
->offset
= src_addr
;
7450 new_entry
= vm_map_copy_entry_create(copy
);
7454 vm_map_unlock(src_map); \
7455 if(src_map != base_map) \
7456 vm_map_deallocate(src_map); \
7457 if (new_entry != VM_MAP_ENTRY_NULL) \
7458 vm_map_copy_entry_dispose(copy,new_entry); \
7459 vm_map_copy_discard(copy); \
7461 submap_map_t *_ptr; \
7463 for(_ptr = parent_maps; _ptr != NULL; _ptr = parent_maps) { \
7464 parent_maps=parent_maps->next; \
7465 if (_ptr->parent_map != base_map) \
7466 vm_map_deallocate(_ptr->parent_map); \
7467 kfree(_ptr, sizeof(submap_map_t)); \
7474 * Find the beginning of the region.
7477 vm_map_lock(src_map
);
7479 if (!vm_map_lookup_entry(src_map
, src_start
, &tmp_entry
))
7480 RETURN(KERN_INVALID_ADDRESS
);
7481 if(!tmp_entry
->is_sub_map
) {
7482 vm_map_clip_start(src_map
, tmp_entry
, src_start
);
7484 /* set for later submap fix-up */
7485 copy_addr
= src_start
;
7488 * Go through entries until we get to the end.
7493 vm_map_entry_t src_entry
= tmp_entry
; /* Top-level entry */
7494 vm_map_size_t src_size
; /* Size of source
7495 * map entry (in both
7500 vm_object_t src_object
; /* Object to copy */
7501 vm_object_offset_t src_offset
;
7503 boolean_t src_needs_copy
; /* Should source map
7505 * for copy-on-write?
7508 boolean_t new_entry_needs_copy
; /* Will new entry be COW? */
7510 boolean_t was_wired
; /* Was source wired? */
7511 vm_map_version_t version
; /* Version before locks
7512 * dropped to make copy
7514 kern_return_t result
; /* Return value from
7515 * copy_strategically.
7517 while(tmp_entry
->is_sub_map
) {
7518 vm_map_size_t submap_len
;
7521 ptr
= (submap_map_t
*)kalloc(sizeof(submap_map_t
));
7522 ptr
->next
= parent_maps
;
7524 ptr
->parent_map
= src_map
;
7525 ptr
->base_start
= src_start
;
7526 ptr
->base_end
= src_end
;
7527 submap_len
= tmp_entry
->vme_end
- src_start
;
7528 if(submap_len
> (src_end
-src_start
))
7529 submap_len
= src_end
-src_start
;
7530 ptr
->base_len
= submap_len
;
7532 src_start
-= tmp_entry
->vme_start
;
7533 src_start
+= tmp_entry
->offset
;
7534 src_end
= src_start
+ submap_len
;
7535 src_map
= tmp_entry
->object
.sub_map
;
7536 vm_map_lock(src_map
);
7537 /* keep an outstanding reference for all maps in */
7538 /* the parents tree except the base map */
7539 vm_map_reference(src_map
);
7540 vm_map_unlock(ptr
->parent_map
);
7541 if (!vm_map_lookup_entry(
7542 src_map
, src_start
, &tmp_entry
))
7543 RETURN(KERN_INVALID_ADDRESS
);
7545 if(!tmp_entry
->is_sub_map
)
7546 vm_map_clip_start(src_map
, tmp_entry
, src_start
);
7547 src_entry
= tmp_entry
;
7549 /* we are now in the lowest level submap... */
7551 if ((tmp_entry
->object
.vm_object
!= VM_OBJECT_NULL
) &&
7552 (tmp_entry
->object
.vm_object
->phys_contiguous
)) {
7553 /* This is not, supported for now.In future */
7554 /* we will need to detect the phys_contig */
7555 /* condition and then upgrade copy_slowly */
7556 /* to do physical copy from the device mem */
7557 /* based object. We can piggy-back off of */
7558 /* the was wired boolean to set-up the */
7559 /* proper handling */
7560 RETURN(KERN_PROTECTION_FAILURE
);
7563 * Create a new address map entry to hold the result.
7564 * Fill in the fields from the appropriate source entries.
7565 * We must unlock the source map to do this if we need
7566 * to allocate a map entry.
7568 if (new_entry
== VM_MAP_ENTRY_NULL
) {
7569 version
.main_timestamp
= src_map
->timestamp
;
7570 vm_map_unlock(src_map
);
7572 new_entry
= vm_map_copy_entry_create(copy
);
7574 vm_map_lock(src_map
);
7575 if ((version
.main_timestamp
+ 1) != src_map
->timestamp
) {
7576 if (!vm_map_lookup_entry(src_map
, src_start
,
7578 RETURN(KERN_INVALID_ADDRESS
);
7580 if (!tmp_entry
->is_sub_map
)
7581 vm_map_clip_start(src_map
, tmp_entry
, src_start
);
7582 continue; /* restart w/ new tmp_entry */
7587 * Verify that the region can be read.
7589 if (((src_entry
->protection
& VM_PROT_READ
) == VM_PROT_NONE
&&
7591 (src_entry
->max_protection
& VM_PROT_READ
) == 0)
7592 RETURN(KERN_PROTECTION_FAILURE
);
7595 * Clip against the endpoints of the entire region.
7598 vm_map_clip_end(src_map
, src_entry
, src_end
);
7600 src_size
= src_entry
->vme_end
- src_start
;
7601 src_object
= src_entry
->object
.vm_object
;
7602 src_offset
= src_entry
->offset
;
7603 was_wired
= (src_entry
->wired_count
!= 0);
7605 vm_map_entry_copy(new_entry
, src_entry
);
7606 new_entry
->use_pmap
= FALSE
; /* clr address space specifics */
7609 * Attempt non-blocking copy-on-write optimizations.
7613 (src_object
== VM_OBJECT_NULL
||
7614 (src_object
->internal
&& !src_object
->true_share
7617 * If we are destroying the source, and the object
7618 * is internal, we can move the object reference
7619 * from the source to the copy. The copy is
7620 * copy-on-write only if the source is.
7621 * We make another reference to the object, because
7622 * destroying the source entry will deallocate it.
7624 vm_object_reference(src_object
);
7627 * Copy is always unwired. vm_map_copy_entry
7628 * set its wired count to zero.
7631 goto CopySuccessful
;
7636 XPR(XPR_VM_MAP
, "vm_map_copyin_common src_obj 0x%x ent 0x%x obj 0x%x was_wired %d\n",
7637 src_object
, new_entry
, new_entry
->object
.vm_object
,
7639 if ((src_object
== VM_OBJECT_NULL
||
7640 (!was_wired
&& !map_share
&& !tmp_entry
->is_shared
)) &&
7641 vm_object_copy_quickly(
7642 &new_entry
->object
.vm_object
,
7646 &new_entry_needs_copy
)) {
7648 new_entry
->needs_copy
= new_entry_needs_copy
;
7651 * Handle copy-on-write obligations
7654 if (src_needs_copy
&& !tmp_entry
->needs_copy
) {
7657 prot
= src_entry
->protection
& ~VM_PROT_WRITE
;
7659 if (override_nx(src_map
, src_entry
->alias
) && prot
)
7660 prot
|= VM_PROT_EXECUTE
;
7662 vm_object_pmap_protect(
7666 (src_entry
->is_shared
?
7669 src_entry
->vme_start
,
7672 tmp_entry
->needs_copy
= TRUE
;
7676 * The map has never been unlocked, so it's safe
7677 * to move to the next entry rather than doing
7681 goto CopySuccessful
;
7685 * Take an object reference, so that we may
7686 * release the map lock(s).
7689 assert(src_object
!= VM_OBJECT_NULL
);
7690 vm_object_reference(src_object
);
7693 * Record the timestamp for later verification.
7697 version
.main_timestamp
= src_map
->timestamp
;
7698 vm_map_unlock(src_map
); /* Increments timestamp once! */
7706 vm_object_lock(src_object
);
7707 result
= vm_object_copy_slowly(
7712 &new_entry
->object
.vm_object
);
7713 new_entry
->offset
= 0;
7714 new_entry
->needs_copy
= FALSE
;
7717 else if (src_object
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
&&
7718 (tmp_entry
->is_shared
|| map_share
)) {
7719 vm_object_t new_object
;
7721 vm_object_lock_shared(src_object
);
7722 new_object
= vm_object_copy_delayed(
7727 if (new_object
== VM_OBJECT_NULL
)
7730 new_entry
->object
.vm_object
= new_object
;
7731 new_entry
->needs_copy
= TRUE
;
7732 result
= KERN_SUCCESS
;
7735 result
= vm_object_copy_strategically(src_object
,
7738 &new_entry
->object
.vm_object
,
7740 &new_entry_needs_copy
);
7742 new_entry
->needs_copy
= new_entry_needs_copy
;
7745 if (result
!= KERN_SUCCESS
&&
7746 result
!= KERN_MEMORY_RESTART_COPY
) {
7747 vm_map_lock(src_map
);
7752 * Throw away the extra reference
7755 vm_object_deallocate(src_object
);
7758 * Verify that the map has not substantially
7759 * changed while the copy was being made.
7762 vm_map_lock(src_map
);
7764 if ((version
.main_timestamp
+ 1) == src_map
->timestamp
)
7765 goto VerificationSuccessful
;
7768 * Simple version comparison failed.
7770 * Retry the lookup and verify that the
7771 * same object/offset are still present.
7773 * [Note: a memory manager that colludes with
7774 * the calling task can detect that we have
7775 * cheated. While the map was unlocked, the
7776 * mapping could have been changed and restored.]
7779 if (!vm_map_lookup_entry(src_map
, src_start
, &tmp_entry
)) {
7780 RETURN(KERN_INVALID_ADDRESS
);
7783 src_entry
= tmp_entry
;
7784 vm_map_clip_start(src_map
, src_entry
, src_start
);
7786 if ((((src_entry
->protection
& VM_PROT_READ
) == VM_PROT_NONE
) &&
7788 ((src_entry
->max_protection
& VM_PROT_READ
) == 0))
7789 goto VerificationFailed
;
7791 if (src_entry
->vme_end
< new_entry
->vme_end
)
7792 src_size
= (new_entry
->vme_end
= src_entry
->vme_end
) - src_start
;
7794 if ((src_entry
->object
.vm_object
!= src_object
) ||
7795 (src_entry
->offset
!= src_offset
) ) {
7798 * Verification failed.
7800 * Start over with this top-level entry.
7803 VerificationFailed
: ;
7805 vm_object_deallocate(new_entry
->object
.vm_object
);
7806 tmp_entry
= src_entry
;
7811 * Verification succeeded.
7814 VerificationSuccessful
: ;
7816 if (result
== KERN_MEMORY_RESTART_COPY
)
7826 * Link in the new copy entry.
7829 vm_map_copy_entry_link(copy
, vm_map_copy_last_entry(copy
),
7833 * Determine whether the entire region
7836 src_base
= src_start
;
7837 src_start
= new_entry
->vme_end
;
7838 new_entry
= VM_MAP_ENTRY_NULL
;
7839 while ((src_start
>= src_end
) && (src_end
!= 0)) {
7840 if (src_map
!= base_map
) {
7844 assert(ptr
!= NULL
);
7845 parent_maps
= parent_maps
->next
;
7847 /* fix up the damage we did in that submap */
7848 vm_map_simplify_range(src_map
,
7852 vm_map_unlock(src_map
);
7853 vm_map_deallocate(src_map
);
7854 vm_map_lock(ptr
->parent_map
);
7855 src_map
= ptr
->parent_map
;
7856 src_base
= ptr
->base_start
;
7857 src_start
= ptr
->base_start
+ ptr
->base_len
;
7858 src_end
= ptr
->base_end
;
7859 if ((src_end
> src_start
) &&
7860 !vm_map_lookup_entry(
7861 src_map
, src_start
, &tmp_entry
))
7862 RETURN(KERN_INVALID_ADDRESS
);
7863 kfree(ptr
, sizeof(submap_map_t
));
7864 if(parent_maps
== NULL
)
7866 src_entry
= tmp_entry
->vme_prev
;
7870 if ((src_start
>= src_end
) && (src_end
!= 0))
7874 * Verify that there are no gaps in the region
7877 tmp_entry
= src_entry
->vme_next
;
7878 if ((tmp_entry
->vme_start
!= src_start
) ||
7879 (tmp_entry
== vm_map_to_entry(src_map
)))
7880 RETURN(KERN_INVALID_ADDRESS
);
7884 * If the source should be destroyed, do it now, since the
7885 * copy was successful.
7888 (void) vm_map_delete(src_map
,
7889 vm_map_trunc_page(src_addr
),
7891 (src_map
== kernel_map
) ?
7892 VM_MAP_REMOVE_KUNWIRE
:
7896 /* fix up the damage we did in the base map */
7897 vm_map_simplify_range(src_map
,
7898 vm_map_trunc_page(src_addr
),
7899 vm_map_round_page(src_end
));
7902 vm_map_unlock(src_map
);
7904 /* Fix-up start and end points in copy. This is necessary */
7905 /* when the various entries in the copy object were picked */
7906 /* up from different sub-maps */
7908 tmp_entry
= vm_map_copy_first_entry(copy
);
7909 while (tmp_entry
!= vm_map_copy_to_entry(copy
)) {
7910 tmp_entry
->vme_end
= copy_addr
+
7911 (tmp_entry
->vme_end
- tmp_entry
->vme_start
);
7912 tmp_entry
->vme_start
= copy_addr
;
7913 copy_addr
+= tmp_entry
->vme_end
- tmp_entry
->vme_start
;
7914 tmp_entry
= (struct vm_map_entry
*)tmp_entry
->vme_next
;
7917 *copy_result
= copy
;
7918 return(KERN_SUCCESS
);
7924 * vm_map_copyin_object:
7926 * Create a copy object from an object.
7927 * Our caller donates an object reference.
7931 vm_map_copyin_object(
7933 vm_object_offset_t offset
, /* offset of region in object */
7934 vm_object_size_t size
, /* size of region in object */
7935 vm_map_copy_t
*copy_result
) /* OUT */
7937 vm_map_copy_t copy
; /* Resulting copy */
7940 * We drop the object into a special copy object
7941 * that contains the object directly.
7944 copy
= (vm_map_copy_t
) zalloc(vm_map_copy_zone
);
7945 copy
->type
= VM_MAP_COPY_OBJECT
;
7946 copy
->cpy_object
= object
;
7947 copy
->offset
= offset
;
7950 *copy_result
= copy
;
7951 return(KERN_SUCCESS
);
7957 vm_map_entry_t old_entry
,
7961 vm_map_entry_t new_entry
;
7964 * New sharing code. New map entry
7965 * references original object. Internal
7966 * objects use asynchronous copy algorithm for
7967 * future copies. First make sure we have
7968 * the right object. If we need a shadow,
7969 * or someone else already has one, then
7970 * make a new shadow and share it.
7973 object
= old_entry
->object
.vm_object
;
7974 if (old_entry
->is_sub_map
) {
7975 assert(old_entry
->wired_count
== 0);
7976 #ifndef NO_NESTED_PMAP
7977 if(old_entry
->use_pmap
) {
7978 kern_return_t result
;
7980 result
= pmap_nest(new_map
->pmap
,
7981 (old_entry
->object
.sub_map
)->pmap
,
7982 (addr64_t
)old_entry
->vme_start
,
7983 (addr64_t
)old_entry
->vme_start
,
7984 (uint64_t)(old_entry
->vme_end
- old_entry
->vme_start
));
7986 panic("vm_map_fork_share: pmap_nest failed!");
7988 #endif /* NO_NESTED_PMAP */
7989 } else if (object
== VM_OBJECT_NULL
) {
7990 object
= vm_object_allocate((vm_map_size_t
)(old_entry
->vme_end
-
7991 old_entry
->vme_start
));
7992 old_entry
->offset
= 0;
7993 old_entry
->object
.vm_object
= object
;
7994 assert(!old_entry
->needs_copy
);
7995 } else if (object
->copy_strategy
!=
7996 MEMORY_OBJECT_COPY_SYMMETRIC
) {
7999 * We are already using an asymmetric
8000 * copy, and therefore we already have
8004 assert(! old_entry
->needs_copy
);
8006 else if (old_entry
->needs_copy
|| /* case 1 */
8007 object
->shadowed
|| /* case 2 */
8008 (!object
->true_share
&& /* case 3 */
8009 !old_entry
->is_shared
&&
8011 (vm_map_size_t
)(old_entry
->vme_end
-
8012 old_entry
->vme_start
)))) {
8015 * We need to create a shadow.
8016 * There are three cases here.
8017 * In the first case, we need to
8018 * complete a deferred symmetrical
8019 * copy that we participated in.
8020 * In the second and third cases,
8021 * we need to create the shadow so
8022 * that changes that we make to the
8023 * object do not interfere with
8024 * any symmetrical copies which
8025 * have occured (case 2) or which
8026 * might occur (case 3).
8028 * The first case is when we had
8029 * deferred shadow object creation
8030 * via the entry->needs_copy mechanism.
8031 * This mechanism only works when
8032 * only one entry points to the source
8033 * object, and we are about to create
8034 * a second entry pointing to the
8035 * same object. The problem is that
8036 * there is no way of mapping from
8037 * an object to the entries pointing
8038 * to it. (Deferred shadow creation
8039 * works with one entry because occurs
8040 * at fault time, and we walk from the
8041 * entry to the object when handling
8044 * The second case is when the object
8045 * to be shared has already been copied
8046 * with a symmetric copy, but we point
8047 * directly to the object without
8048 * needs_copy set in our entry. (This
8049 * can happen because different ranges
8050 * of an object can be pointed to by
8051 * different entries. In particular,
8052 * a single entry pointing to an object
8053 * can be split by a call to vm_inherit,
8054 * which, combined with task_create, can
8055 * result in the different entries
8056 * having different needs_copy values.)
8057 * The shadowed flag in the object allows
8058 * us to detect this case. The problem
8059 * with this case is that if this object
8060 * has or will have shadows, then we
8061 * must not perform an asymmetric copy
8062 * of this object, since such a copy
8063 * allows the object to be changed, which
8064 * will break the previous symmetrical
8065 * copies (which rely upon the object
8066 * not changing). In a sense, the shadowed
8067 * flag says "don't change this object".
8068 * We fix this by creating a shadow
8069 * object for this object, and sharing
8070 * that. This works because we are free
8071 * to change the shadow object (and thus
8072 * to use an asymmetric copy strategy);
8073 * this is also semantically correct,
8074 * since this object is temporary, and
8075 * therefore a copy of the object is
8076 * as good as the object itself. (This
8077 * is not true for permanent objects,
8078 * since the pager needs to see changes,
8079 * which won't happen if the changes
8080 * are made to a copy.)
8082 * The third case is when the object
8083 * to be shared has parts sticking
8084 * outside of the entry we're working
8085 * with, and thus may in the future
8086 * be subject to a symmetrical copy.
8087 * (This is a preemptive version of
8090 vm_object_shadow(&old_entry
->object
.vm_object
,
8092 (vm_map_size_t
) (old_entry
->vme_end
-
8093 old_entry
->vme_start
));
8096 * If we're making a shadow for other than
8097 * copy on write reasons, then we have
8098 * to remove write permission.
8101 if (!old_entry
->needs_copy
&&
8102 (old_entry
->protection
& VM_PROT_WRITE
)) {
8105 prot
= old_entry
->protection
& ~VM_PROT_WRITE
;
8107 if (override_nx(old_map
, old_entry
->alias
) && prot
)
8108 prot
|= VM_PROT_EXECUTE
;
8110 if (old_map
->mapped
) {
8111 vm_object_pmap_protect(
8112 old_entry
->object
.vm_object
,
8114 (old_entry
->vme_end
-
8115 old_entry
->vme_start
),
8117 old_entry
->vme_start
,
8120 pmap_protect(old_map
->pmap
,
8121 old_entry
->vme_start
,
8127 old_entry
->needs_copy
= FALSE
;
8128 object
= old_entry
->object
.vm_object
;
8133 * If object was using a symmetric copy strategy,
8134 * change its copy strategy to the default
8135 * asymmetric copy strategy, which is copy_delay
8136 * in the non-norma case and copy_call in the
8137 * norma case. Bump the reference count for the
8141 if(old_entry
->is_sub_map
) {
8142 vm_map_lock(old_entry
->object
.sub_map
);
8143 vm_map_reference(old_entry
->object
.sub_map
);
8144 vm_map_unlock(old_entry
->object
.sub_map
);
8146 vm_object_lock(object
);
8147 vm_object_reference_locked(object
);
8148 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
) {
8149 object
->copy_strategy
= MEMORY_OBJECT_COPY_DELAY
;
8151 vm_object_unlock(object
);
8155 * Clone the entry, using object ref from above.
8156 * Mark both entries as shared.
8159 new_entry
= vm_map_entry_create(new_map
);
8160 vm_map_entry_copy(new_entry
, old_entry
);
8161 old_entry
->is_shared
= TRUE
;
8162 new_entry
->is_shared
= TRUE
;
8165 * Insert the entry into the new map -- we
8166 * know we're inserting at the end of the new
8170 vm_map_store_entry_link(new_map
, vm_map_last_entry(new_map
), new_entry
);
8173 * Update the physical map
8176 if (old_entry
->is_sub_map
) {
8177 /* Bill Angell pmap support goes here */
8179 pmap_copy(new_map
->pmap
, old_map
->pmap
, new_entry
->vme_start
,
8180 old_entry
->vme_end
- old_entry
->vme_start
,
8181 old_entry
->vme_start
);
8188 vm_map_entry_t
*old_entry_p
,
8191 vm_map_entry_t old_entry
= *old_entry_p
;
8192 vm_map_size_t entry_size
= old_entry
->vme_end
- old_entry
->vme_start
;
8193 vm_map_offset_t start
= old_entry
->vme_start
;
8195 vm_map_entry_t last
= vm_map_last_entry(new_map
);
8197 vm_map_unlock(old_map
);
8199 * Use maxprot version of copyin because we
8200 * care about whether this memory can ever
8201 * be accessed, not just whether it's accessible
8204 if (vm_map_copyin_maxprot(old_map
, start
, entry_size
, FALSE
, ©
)
8207 * The map might have changed while it
8208 * was unlocked, check it again. Skip
8209 * any blank space or permanently
8210 * unreadable region.
8212 vm_map_lock(old_map
);
8213 if (!vm_map_lookup_entry(old_map
, start
, &last
) ||
8214 (last
->max_protection
& VM_PROT_READ
) == VM_PROT_NONE
) {
8215 last
= last
->vme_next
;
8217 *old_entry_p
= last
;
8220 * XXX For some error returns, want to
8221 * XXX skip to the next element. Note
8222 * that INVALID_ADDRESS and
8223 * PROTECTION_FAILURE are handled above.
8230 * Insert the copy into the new map
8233 vm_map_copy_insert(new_map
, last
, copy
);
8236 * Pick up the traversal at the end of
8237 * the copied region.
8240 vm_map_lock(old_map
);
8241 start
+= entry_size
;
8242 if (! vm_map_lookup_entry(old_map
, start
, &last
)) {
8243 last
= last
->vme_next
;
8245 if (last
->vme_start
== start
) {
8247 * No need to clip here and we don't
8248 * want to cause any unnecessary
8252 vm_map_clip_start(old_map
, last
, start
);
8255 *old_entry_p
= last
;
8263 * Create and return a new map based on the old
8264 * map, according to the inheritance values on the
8265 * regions in that map.
8267 * The source map must not be locked.
8275 vm_map_entry_t old_entry
;
8276 vm_map_size_t new_size
= 0, entry_size
;
8277 vm_map_entry_t new_entry
;
8278 boolean_t src_needs_copy
;
8279 boolean_t new_entry_needs_copy
;
8281 new_pmap
= pmap_create((vm_map_size_t
) 0,
8282 #if defined(__i386__) || defined(__x86_64__)
8283 old_map
->pmap
->pm_task_map
!= TASK_MAP_32BIT
8288 #if defined(__i386__)
8289 if (old_map
->pmap
->pm_task_map
== TASK_MAP_64BIT_SHARED
)
8290 pmap_set_4GB_pagezero(new_pmap
);
8293 vm_map_reference_swap(old_map
);
8294 vm_map_lock(old_map
);
8296 new_map
= vm_map_create(new_pmap
,
8297 old_map
->min_offset
,
8298 old_map
->max_offset
,
8299 old_map
->hdr
.entries_pageable
);
8301 old_entry
= vm_map_first_entry(old_map
);
8302 old_entry
!= vm_map_to_entry(old_map
);
8305 entry_size
= old_entry
->vme_end
- old_entry
->vme_start
;
8307 switch (old_entry
->inheritance
) {
8308 case VM_INHERIT_NONE
:
8311 case VM_INHERIT_SHARE
:
8312 vm_map_fork_share(old_map
, old_entry
, new_map
);
8313 new_size
+= entry_size
;
8316 case VM_INHERIT_COPY
:
8319 * Inline the copy_quickly case;
8320 * upon failure, fall back on call
8321 * to vm_map_fork_copy.
8324 if(old_entry
->is_sub_map
)
8326 if ((old_entry
->wired_count
!= 0) ||
8327 ((old_entry
->object
.vm_object
!= NULL
) &&
8328 (old_entry
->object
.vm_object
->true_share
))) {
8329 goto slow_vm_map_fork_copy
;
8332 new_entry
= vm_map_entry_create(new_map
);
8333 vm_map_entry_copy(new_entry
, old_entry
);
8334 /* clear address space specifics */
8335 new_entry
->use_pmap
= FALSE
;
8337 if (! vm_object_copy_quickly(
8338 &new_entry
->object
.vm_object
,
8340 (old_entry
->vme_end
-
8341 old_entry
->vme_start
),
8343 &new_entry_needs_copy
)) {
8344 vm_map_entry_dispose(new_map
, new_entry
);
8345 goto slow_vm_map_fork_copy
;
8349 * Handle copy-on-write obligations
8352 if (src_needs_copy
&& !old_entry
->needs_copy
) {
8355 prot
= old_entry
->protection
& ~VM_PROT_WRITE
;
8357 if (override_nx(old_map
, old_entry
->alias
) && prot
)
8358 prot
|= VM_PROT_EXECUTE
;
8360 vm_object_pmap_protect(
8361 old_entry
->object
.vm_object
,
8363 (old_entry
->vme_end
-
8364 old_entry
->vme_start
),
8365 ((old_entry
->is_shared
8369 old_entry
->vme_start
,
8372 old_entry
->needs_copy
= TRUE
;
8374 new_entry
->needs_copy
= new_entry_needs_copy
;
8377 * Insert the entry at the end
8381 vm_map_store_entry_link(new_map
, vm_map_last_entry(new_map
),
8383 new_size
+= entry_size
;
8386 slow_vm_map_fork_copy
:
8387 if (vm_map_fork_copy(old_map
, &old_entry
, new_map
)) {
8388 new_size
+= entry_size
;
8392 old_entry
= old_entry
->vme_next
;
8395 new_map
->size
= new_size
;
8396 vm_map_unlock(old_map
);
8397 vm_map_deallocate(old_map
);
8405 * Setup the "new_map" with the proper execution environment according
8406 * to the type of executable (platform, 64bit, chroot environment).
8407 * Map the comm page and shared region, etc...
8416 SHARED_REGION_TRACE_DEBUG(
8417 ("shared_region: task %p: vm_map_exec(%p,%p,%p,0x%x): ->\n",
8418 current_task(), new_map
, task
, fsroot
, cpu
));
8419 (void) vm_commpage_enter(new_map
, task
);
8420 (void) vm_shared_region_enter(new_map
, task
, fsroot
, cpu
);
8421 SHARED_REGION_TRACE_DEBUG(
8422 ("shared_region: task %p: vm_map_exec(%p,%p,%p,0x%x): <-\n",
8423 current_task(), new_map
, task
, fsroot
, cpu
));
8424 return KERN_SUCCESS
;
8428 * vm_map_lookup_locked:
8430 * Finds the VM object, offset, and
8431 * protection for a given virtual address in the
8432 * specified map, assuming a page fault of the
8435 * Returns the (object, offset, protection) for
8436 * this address, whether it is wired down, and whether
8437 * this map has the only reference to the data in question.
8438 * In order to later verify this lookup, a "version"
8441 * The map MUST be locked by the caller and WILL be
8442 * locked on exit. In order to guarantee the
8443 * existence of the returned object, it is returned
8446 * If a lookup is requested with "write protection"
8447 * specified, the map may be changed to perform virtual
8448 * copying operations, although the data referenced will
8452 vm_map_lookup_locked(
8453 vm_map_t
*var_map
, /* IN/OUT */
8454 vm_map_offset_t vaddr
,
8455 vm_prot_t fault_type
,
8456 int object_lock_type
,
8457 vm_map_version_t
*out_version
, /* OUT */
8458 vm_object_t
*object
, /* OUT */
8459 vm_object_offset_t
*offset
, /* OUT */
8460 vm_prot_t
*out_prot
, /* OUT */
8461 boolean_t
*wired
, /* OUT */
8462 vm_object_fault_info_t fault_info
, /* OUT */
8465 vm_map_entry_t entry
;
8466 register vm_map_t map
= *var_map
;
8467 vm_map_t old_map
= *var_map
;
8468 vm_map_t cow_sub_map_parent
= VM_MAP_NULL
;
8469 vm_map_offset_t cow_parent_vaddr
= 0;
8470 vm_map_offset_t old_start
= 0;
8471 vm_map_offset_t old_end
= 0;
8472 register vm_prot_t prot
;
8473 boolean_t mask_protections
;
8474 vm_prot_t original_fault_type
;
8477 * VM_PROT_MASK means that the caller wants us to use "fault_type"
8478 * as a mask against the mapping's actual protections, not as an
8481 mask_protections
= (fault_type
& VM_PROT_IS_MASK
) ? TRUE
: FALSE
;
8482 fault_type
&= ~VM_PROT_IS_MASK
;
8483 original_fault_type
= fault_type
;
8488 fault_type
= original_fault_type
;
8491 * If the map has an interesting hint, try it before calling
8492 * full blown lookup routine.
8496 if ((entry
== vm_map_to_entry(map
)) ||
8497 (vaddr
< entry
->vme_start
) || (vaddr
>= entry
->vme_end
)) {
8498 vm_map_entry_t tmp_entry
;
8501 * Entry was either not a valid hint, or the vaddr
8502 * was not contained in the entry, so do a full lookup.
8504 if (!vm_map_lookup_entry(map
, vaddr
, &tmp_entry
)) {
8505 if((cow_sub_map_parent
) && (cow_sub_map_parent
!= map
))
8506 vm_map_unlock(cow_sub_map_parent
);
8507 if((*real_map
!= map
)
8508 && (*real_map
!= cow_sub_map_parent
))
8509 vm_map_unlock(*real_map
);
8510 return KERN_INVALID_ADDRESS
;
8515 if(map
== old_map
) {
8516 old_start
= entry
->vme_start
;
8517 old_end
= entry
->vme_end
;
8521 * Handle submaps. Drop lock on upper map, submap is
8526 if (entry
->is_sub_map
) {
8527 vm_map_offset_t local_vaddr
;
8528 vm_map_offset_t end_delta
;
8529 vm_map_offset_t start_delta
;
8530 vm_map_entry_t submap_entry
;
8531 boolean_t mapped_needs_copy
=FALSE
;
8533 local_vaddr
= vaddr
;
8535 if ((entry
->use_pmap
&& !(fault_type
& VM_PROT_WRITE
))) {
8536 /* if real_map equals map we unlock below */
8537 if ((*real_map
!= map
) &&
8538 (*real_map
!= cow_sub_map_parent
))
8539 vm_map_unlock(*real_map
);
8540 *real_map
= entry
->object
.sub_map
;
8543 if(entry
->needs_copy
&& (fault_type
& VM_PROT_WRITE
)) {
8544 if (!mapped_needs_copy
) {
8545 if (vm_map_lock_read_to_write(map
)) {
8546 vm_map_lock_read(map
);
8547 /* XXX FBDP: entry still valid ? */
8548 if(*real_map
== entry
->object
.sub_map
)
8552 vm_map_lock_read(entry
->object
.sub_map
);
8553 cow_sub_map_parent
= map
;
8554 /* reset base to map before cow object */
8555 /* this is the map which will accept */
8556 /* the new cow object */
8557 old_start
= entry
->vme_start
;
8558 old_end
= entry
->vme_end
;
8559 cow_parent_vaddr
= vaddr
;
8560 mapped_needs_copy
= TRUE
;
8562 vm_map_lock_read(entry
->object
.sub_map
);
8563 if((cow_sub_map_parent
!= map
) &&
8568 vm_map_lock_read(entry
->object
.sub_map
);
8569 /* leave map locked if it is a target */
8570 /* cow sub_map above otherwise, just */
8571 /* follow the maps down to the object */
8572 /* here we unlock knowing we are not */
8573 /* revisiting the map. */
8574 if((*real_map
!= map
) && (map
!= cow_sub_map_parent
))
8575 vm_map_unlock_read(map
);
8578 /* XXX FBDP: map has been unlocked, what protects "entry" !? */
8579 *var_map
= map
= entry
->object
.sub_map
;
8581 /* calculate the offset in the submap for vaddr */
8582 local_vaddr
= (local_vaddr
- entry
->vme_start
) + entry
->offset
;
8585 if(!vm_map_lookup_entry(map
, local_vaddr
, &submap_entry
)) {
8586 if((cow_sub_map_parent
) && (cow_sub_map_parent
!= map
)){
8587 vm_map_unlock(cow_sub_map_parent
);
8589 if((*real_map
!= map
)
8590 && (*real_map
!= cow_sub_map_parent
)) {
8591 vm_map_unlock(*real_map
);
8594 return KERN_INVALID_ADDRESS
;
8597 /* find the attenuated shadow of the underlying object */
8598 /* on our target map */
8600 /* in english the submap object may extend beyond the */
8601 /* region mapped by the entry or, may only fill a portion */
8602 /* of it. For our purposes, we only care if the object */
8603 /* doesn't fill. In this case the area which will */
8604 /* ultimately be clipped in the top map will only need */
8605 /* to be as big as the portion of the underlying entry */
8606 /* which is mapped */
8607 start_delta
= submap_entry
->vme_start
> entry
->offset
?
8608 submap_entry
->vme_start
- entry
->offset
: 0;
8611 (entry
->offset
+ start_delta
+ (old_end
- old_start
)) <=
8612 submap_entry
->vme_end
?
8613 0 : (entry
->offset
+
8614 (old_end
- old_start
))
8615 - submap_entry
->vme_end
;
8617 old_start
+= start_delta
;
8618 old_end
-= end_delta
;
8620 if(submap_entry
->is_sub_map
) {
8621 entry
= submap_entry
;
8622 vaddr
= local_vaddr
;
8623 goto submap_recurse
;
8626 if(((fault_type
& VM_PROT_WRITE
) && cow_sub_map_parent
)) {
8628 vm_object_t sub_object
, copy_object
;
8629 vm_object_offset_t copy_offset
;
8630 vm_map_offset_t local_start
;
8631 vm_map_offset_t local_end
;
8632 boolean_t copied_slowly
= FALSE
;
8634 if (vm_map_lock_read_to_write(map
)) {
8635 vm_map_lock_read(map
);
8636 old_start
-= start_delta
;
8637 old_end
+= end_delta
;
8642 sub_object
= submap_entry
->object
.vm_object
;
8643 if (sub_object
== VM_OBJECT_NULL
) {
8647 (submap_entry
->vme_end
-
8648 submap_entry
->vme_start
));
8649 submap_entry
->object
.vm_object
= sub_object
;
8650 submap_entry
->offset
= 0;
8652 local_start
= local_vaddr
-
8653 (cow_parent_vaddr
- old_start
);
8654 local_end
= local_vaddr
+
8655 (old_end
- cow_parent_vaddr
);
8656 vm_map_clip_start(map
, submap_entry
, local_start
);
8657 vm_map_clip_end(map
, submap_entry
, local_end
);
8658 /* unnesting was done in vm_map_clip_start/end() */
8659 assert(!submap_entry
->use_pmap
);
8661 /* This is the COW case, lets connect */
8662 /* an entry in our space to the underlying */
8663 /* object in the submap, bypassing the */
8667 if(submap_entry
->wired_count
!= 0 ||
8668 (sub_object
->copy_strategy
==
8669 MEMORY_OBJECT_COPY_NONE
)) {
8670 vm_object_lock(sub_object
);
8671 vm_object_copy_slowly(sub_object
,
8672 submap_entry
->offset
,
8673 (submap_entry
->vme_end
-
8674 submap_entry
->vme_start
),
8677 copied_slowly
= TRUE
;
8680 /* set up shadow object */
8681 copy_object
= sub_object
;
8682 vm_object_reference(copy_object
);
8683 sub_object
->shadowed
= TRUE
;
8684 submap_entry
->needs_copy
= TRUE
;
8686 prot
= submap_entry
->protection
& ~VM_PROT_WRITE
;
8688 if (override_nx(map
, submap_entry
->alias
) && prot
)
8689 prot
|= VM_PROT_EXECUTE
;
8691 vm_object_pmap_protect(
8693 submap_entry
->offset
,
8694 submap_entry
->vme_end
-
8695 submap_entry
->vme_start
,
8696 (submap_entry
->is_shared
8698 PMAP_NULL
: map
->pmap
,
8699 submap_entry
->vme_start
,
8704 * Adjust the fault offset to the submap entry.
8706 copy_offset
= (local_vaddr
-
8707 submap_entry
->vme_start
+
8708 submap_entry
->offset
);
8710 /* This works diffently than the */
8711 /* normal submap case. We go back */
8712 /* to the parent of the cow map and*/
8713 /* clip out the target portion of */
8714 /* the sub_map, substituting the */
8715 /* new copy object, */
8718 local_start
= old_start
;
8719 local_end
= old_end
;
8720 map
= cow_sub_map_parent
;
8721 *var_map
= cow_sub_map_parent
;
8722 vaddr
= cow_parent_vaddr
;
8723 cow_sub_map_parent
= NULL
;
8725 if(!vm_map_lookup_entry(map
,
8727 vm_object_deallocate(
8729 vm_map_lock_write_to_read(map
);
8730 return KERN_INVALID_ADDRESS
;
8733 /* clip out the portion of space */
8734 /* mapped by the sub map which */
8735 /* corresponds to the underlying */
8739 * Clip (and unnest) the smallest nested chunk
8740 * possible around the faulting address...
8742 local_start
= vaddr
& ~(pmap_nesting_size_min
- 1);
8743 local_end
= local_start
+ pmap_nesting_size_min
;
8745 * ... but don't go beyond the "old_start" to "old_end"
8746 * range, to avoid spanning over another VM region
8747 * with a possibly different VM object and/or offset.
8749 if (local_start
< old_start
) {
8750 local_start
= old_start
;
8752 if (local_end
> old_end
) {
8753 local_end
= old_end
;
8756 * Adjust copy_offset to the start of the range.
8758 copy_offset
-= (vaddr
- local_start
);
8760 vm_map_clip_start(map
, entry
, local_start
);
8761 vm_map_clip_end(map
, entry
, local_end
);
8762 /* unnesting was done in vm_map_clip_start/end() */
8763 assert(!entry
->use_pmap
);
8765 /* substitute copy object for */
8766 /* shared map entry */
8767 vm_map_deallocate(entry
->object
.sub_map
);
8768 entry
->is_sub_map
= FALSE
;
8769 entry
->object
.vm_object
= copy_object
;
8771 /* propagate the submap entry's protections */
8772 entry
->protection
|= submap_entry
->protection
;
8773 entry
->max_protection
|= submap_entry
->max_protection
;
8776 entry
->offset
= local_start
- old_start
;
8777 entry
->needs_copy
= FALSE
;
8778 entry
->is_shared
= FALSE
;
8780 entry
->offset
= copy_offset
;
8781 entry
->needs_copy
= TRUE
;
8782 if(entry
->inheritance
== VM_INHERIT_SHARE
)
8783 entry
->inheritance
= VM_INHERIT_COPY
;
8785 entry
->is_shared
= TRUE
;
8787 if(entry
->inheritance
== VM_INHERIT_SHARE
)
8788 entry
->inheritance
= VM_INHERIT_COPY
;
8790 vm_map_lock_write_to_read(map
);
8792 if((cow_sub_map_parent
)
8793 && (cow_sub_map_parent
!= *real_map
)
8794 && (cow_sub_map_parent
!= map
)) {
8795 vm_map_unlock(cow_sub_map_parent
);
8797 entry
= submap_entry
;
8798 vaddr
= local_vaddr
;
8803 * Check whether this task is allowed to have
8807 prot
= entry
->protection
;
8809 if (override_nx(map
, entry
->alias
) && prot
) {
8811 * HACK -- if not a stack, then allow execution
8813 prot
|= VM_PROT_EXECUTE
;
8816 if (mask_protections
) {
8818 if (fault_type
== VM_PROT_NONE
) {
8819 goto protection_failure
;
8822 if ((fault_type
& (prot
)) != fault_type
) {
8824 if (*real_map
!= map
) {
8825 vm_map_unlock(*real_map
);
8829 if ((fault_type
& VM_PROT_EXECUTE
) && prot
)
8830 log_stack_execution_failure((addr64_t
)vaddr
, prot
);
8832 DTRACE_VM2(prot_fault
, int, 1, (uint64_t *), NULL
);
8833 return KERN_PROTECTION_FAILURE
;
8837 * If this page is not pageable, we have to get
8838 * it for all possible accesses.
8841 *wired
= (entry
->wired_count
!= 0);
8846 * If the entry was copy-on-write, we either ...
8849 if (entry
->needs_copy
) {
8851 * If we want to write the page, we may as well
8852 * handle that now since we've got the map locked.
8854 * If we don't need to write the page, we just
8855 * demote the permissions allowed.
8858 if ((fault_type
& VM_PROT_WRITE
) || *wired
) {
8860 * Make a new object, and place it in the
8861 * object chain. Note that no new references
8862 * have appeared -- one just moved from the
8863 * map to the new object.
8866 if (vm_map_lock_read_to_write(map
)) {
8867 vm_map_lock_read(map
);
8870 vm_object_shadow(&entry
->object
.vm_object
,
8872 (vm_map_size_t
) (entry
->vme_end
-
8875 entry
->object
.vm_object
->shadowed
= TRUE
;
8876 entry
->needs_copy
= FALSE
;
8877 vm_map_lock_write_to_read(map
);
8881 * We're attempting to read a copy-on-write
8882 * page -- don't allow writes.
8885 prot
&= (~VM_PROT_WRITE
);
8890 * Create an object if necessary.
8892 if (entry
->object
.vm_object
== VM_OBJECT_NULL
) {
8894 if (vm_map_lock_read_to_write(map
)) {
8895 vm_map_lock_read(map
);
8899 entry
->object
.vm_object
= vm_object_allocate(
8900 (vm_map_size_t
)(entry
->vme_end
- entry
->vme_start
));
8902 vm_map_lock_write_to_read(map
);
8906 * Return the object/offset from this entry. If the entry
8907 * was copy-on-write or empty, it has been fixed up. Also
8908 * return the protection.
8911 *offset
= (vaddr
- entry
->vme_start
) + entry
->offset
;
8912 *object
= entry
->object
.vm_object
;
8916 fault_info
->interruptible
= THREAD_UNINT
; /* for now... */
8917 /* ... the caller will change "interruptible" if needed */
8918 fault_info
->cluster_size
= 0;
8919 fault_info
->user_tag
= entry
->alias
;
8920 fault_info
->behavior
= entry
->behavior
;
8921 fault_info
->lo_offset
= entry
->offset
;
8922 fault_info
->hi_offset
= (entry
->vme_end
- entry
->vme_start
) + entry
->offset
;
8923 fault_info
->no_cache
= entry
->no_cache
;
8924 fault_info
->stealth
= FALSE
;
8925 fault_info
->io_sync
= FALSE
;
8926 fault_info
->cs_bypass
= (entry
->used_for_jit
)? TRUE
: FALSE
;
8927 fault_info
->mark_zf_absent
= FALSE
;
8931 * Lock the object to prevent it from disappearing
8933 if (object_lock_type
== OBJECT_LOCK_EXCLUSIVE
)
8934 vm_object_lock(*object
);
8936 vm_object_lock_shared(*object
);
8939 * Save the version number
8942 out_version
->main_timestamp
= map
->timestamp
;
8944 return KERN_SUCCESS
;
8951 * Verifies that the map in question has not changed
8952 * since the given version. If successful, the map
8953 * will not change until vm_map_verify_done() is called.
8957 register vm_map_t map
,
8958 register vm_map_version_t
*version
) /* REF */
8962 vm_map_lock_read(map
);
8963 result
= (map
->timestamp
== version
->main_timestamp
);
8966 vm_map_unlock_read(map
);
8972 * vm_map_verify_done:
8974 * Releases locks acquired by a vm_map_verify.
8976 * This is now a macro in vm/vm_map.h. It does a
8977 * vm_map_unlock_read on the map.
8982 * TEMPORARYTEMPORARYTEMPORARYTEMPORARYTEMPORARYTEMPORARY
8983 * Goes away after regular vm_region_recurse function migrates to
8985 * vm_region_recurse: A form of vm_region which follows the
8986 * submaps in a target map
8991 vm_map_region_recurse_64(
8993 vm_map_offset_t
*address
, /* IN/OUT */
8994 vm_map_size_t
*size
, /* OUT */
8995 natural_t
*nesting_depth
, /* IN/OUT */
8996 vm_region_submap_info_64_t submap_info
, /* IN/OUT */
8997 mach_msg_type_number_t
*count
) /* IN/OUT */
8999 vm_region_extended_info_data_t extended
;
9000 vm_map_entry_t tmp_entry
;
9001 vm_map_offset_t user_address
;
9002 unsigned int user_max_depth
;
9005 * "curr_entry" is the VM map entry preceding or including the
9006 * address we're looking for.
9007 * "curr_map" is the map or sub-map containing "curr_entry".
9008 * "curr_address" is the equivalent of the top map's "user_address"
9009 * in the current map.
9010 * "curr_offset" is the cumulated offset of "curr_map" in the
9011 * target task's address space.
9012 * "curr_depth" is the depth of "curr_map" in the chain of
9015 * "curr_max_below" and "curr_max_above" limit the range (around
9016 * "curr_address") we should take into account in the current (sub)map.
9017 * They limit the range to what's visible through the map entries
9018 * we've traversed from the top map to the current map.
9021 vm_map_entry_t curr_entry
;
9022 vm_map_address_t curr_address
;
9023 vm_map_offset_t curr_offset
;
9025 unsigned int curr_depth
;
9026 vm_map_offset_t curr_max_below
, curr_max_above
;
9027 vm_map_offset_t curr_skip
;
9030 * "next_" is the same as "curr_" but for the VM region immediately
9031 * after the address we're looking for. We need to keep track of this
9032 * too because we want to return info about that region if the
9033 * address we're looking for is not mapped.
9035 vm_map_entry_t next_entry
;
9036 vm_map_offset_t next_offset
;
9037 vm_map_offset_t next_address
;
9039 unsigned int next_depth
;
9040 vm_map_offset_t next_max_below
, next_max_above
;
9041 vm_map_offset_t next_skip
;
9043 boolean_t look_for_pages
;
9044 vm_region_submap_short_info_64_t short_info
;
9046 if (map
== VM_MAP_NULL
) {
9047 /* no address space to work on */
9048 return KERN_INVALID_ARGUMENT
;
9051 if (*count
< VM_REGION_SUBMAP_INFO_COUNT_64
) {
9052 if (*count
< VM_REGION_SUBMAP_SHORT_INFO_COUNT_64
) {
9054 * "info" structure is not big enough and
9057 return KERN_INVALID_ARGUMENT
;
9059 look_for_pages
= FALSE
;
9060 *count
= VM_REGION_SUBMAP_SHORT_INFO_COUNT_64
;
9061 short_info
= (vm_region_submap_short_info_64_t
) submap_info
;
9065 look_for_pages
= TRUE
;
9066 *count
= VM_REGION_SUBMAP_INFO_COUNT_64
;
9071 user_address
= *address
;
9072 user_max_depth
= *nesting_depth
;
9076 curr_address
= user_address
;
9080 curr_max_above
= ((vm_map_offset_t
) -1) - curr_address
;
9081 curr_max_below
= curr_address
;
9089 next_max_above
= (vm_map_offset_t
) -1;
9090 next_max_below
= (vm_map_offset_t
) -1;
9093 vm_map_lock_read(curr_map
);
9097 if (vm_map_lookup_entry(curr_map
,
9100 /* tmp_entry contains the address we're looking for */
9101 curr_entry
= tmp_entry
;
9103 vm_map_offset_t skip
;
9105 * The address is not mapped. "tmp_entry" is the
9106 * map entry preceding the address. We want the next
9107 * one, if it exists.
9109 curr_entry
= tmp_entry
->vme_next
;
9111 if (curr_entry
== vm_map_to_entry(curr_map
) ||
9112 (curr_entry
->vme_start
>=
9113 curr_address
+ curr_max_above
)) {
9114 /* no next entry at this level: stop looking */
9116 vm_map_unlock_read(curr_map
);
9127 /* adjust current address and offset */
9128 skip
= curr_entry
->vme_start
- curr_address
;
9129 curr_address
= curr_entry
->vme_start
;
9131 curr_offset
+= skip
;
9132 curr_max_above
-= skip
;
9137 * Is the next entry at this level closer to the address (or
9138 * deeper in the submap chain) than the one we had
9141 tmp_entry
= curr_entry
->vme_next
;
9142 if (tmp_entry
== vm_map_to_entry(curr_map
)) {
9143 /* no next entry at this level */
9144 } else if (tmp_entry
->vme_start
>=
9145 curr_address
+ curr_max_above
) {
9147 * tmp_entry is beyond the scope of what we mapped of
9148 * this submap in the upper level: ignore it.
9150 } else if ((next_entry
== NULL
) ||
9151 (tmp_entry
->vme_start
+ curr_offset
<=
9152 next_entry
->vme_start
+ next_offset
)) {
9154 * We didn't have a "next_entry" or this one is
9155 * closer to the address we're looking for:
9156 * use this "tmp_entry" as the new "next_entry".
9158 if (next_entry
!= NULL
) {
9159 /* unlock the last "next_map" */
9160 if (next_map
!= curr_map
&& not_in_kdp
) {
9161 vm_map_unlock_read(next_map
);
9164 next_entry
= tmp_entry
;
9165 next_map
= curr_map
;
9166 next_depth
= curr_depth
;
9167 next_address
= next_entry
->vme_start
;
9168 next_skip
= curr_skip
;
9169 next_offset
= curr_offset
;
9170 next_offset
+= (next_address
- curr_address
);
9171 next_max_above
= MIN(next_max_above
, curr_max_above
);
9172 next_max_above
= MIN(next_max_above
,
9173 next_entry
->vme_end
- next_address
);
9174 next_max_below
= MIN(next_max_below
, curr_max_below
);
9175 next_max_below
= MIN(next_max_below
,
9176 next_address
- next_entry
->vme_start
);
9180 * "curr_max_{above,below}" allow us to keep track of the
9181 * portion of the submap that is actually mapped at this level:
9182 * the rest of that submap is irrelevant to us, since it's not
9184 * The relevant portion of the map starts at
9185 * "curr_entry->offset" up to the size of "curr_entry".
9187 curr_max_above
= MIN(curr_max_above
,
9188 curr_entry
->vme_end
- curr_address
);
9189 curr_max_below
= MIN(curr_max_below
,
9190 curr_address
- curr_entry
->vme_start
);
9192 if (!curr_entry
->is_sub_map
||
9193 curr_depth
>= user_max_depth
) {
9195 * We hit a leaf map or we reached the maximum depth
9196 * we could, so stop looking. Keep the current map
9203 * Get down to the next submap level.
9207 * Lock the next level and unlock the current level,
9208 * unless we need to keep it locked to access the "next_entry"
9212 vm_map_lock_read(curr_entry
->object
.sub_map
);
9214 if (curr_map
== next_map
) {
9215 /* keep "next_map" locked in case we need it */
9217 /* release this map */
9219 vm_map_unlock_read(curr_map
);
9223 * Adjust the offset. "curr_entry" maps the submap
9224 * at relative address "curr_entry->vme_start" in the
9225 * curr_map but skips the first "curr_entry->offset"
9226 * bytes of the submap.
9227 * "curr_offset" always represents the offset of a virtual
9228 * address in the curr_map relative to the absolute address
9229 * space (i.e. the top-level VM map).
9232 (curr_entry
->offset
- curr_entry
->vme_start
);
9233 curr_address
= user_address
+ curr_offset
;
9234 /* switch to the submap */
9235 curr_map
= curr_entry
->object
.sub_map
;
9240 if (curr_entry
== NULL
) {
9241 /* no VM region contains the address... */
9242 if (next_entry
== NULL
) {
9243 /* ... and no VM region follows it either */
9244 return KERN_INVALID_ADDRESS
;
9246 /* ... gather info about the next VM region */
9247 curr_entry
= next_entry
;
9248 curr_map
= next_map
; /* still locked ... */
9249 curr_address
= next_address
;
9250 curr_skip
= next_skip
;
9251 curr_offset
= next_offset
;
9252 curr_depth
= next_depth
;
9253 curr_max_above
= next_max_above
;
9254 curr_max_below
= next_max_below
;
9255 if (curr_map
== map
) {
9256 user_address
= curr_address
;
9259 /* we won't need "next_entry" after all */
9260 if (next_entry
!= NULL
) {
9261 /* release "next_map" */
9262 if (next_map
!= curr_map
&& not_in_kdp
) {
9263 vm_map_unlock_read(next_map
);
9272 next_max_below
= -1;
9273 next_max_above
= -1;
9275 *nesting_depth
= curr_depth
;
9276 *size
= curr_max_above
+ curr_max_below
;
9277 *address
= user_address
+ curr_skip
- curr_max_below
;
9279 // LP64todo: all the current tools are 32bit, obviously never worked for 64b
9280 // so probably should be a real 32b ID vs. ptr.
9281 // Current users just check for equality
9282 #define INFO_MAKE_OBJECT_ID(p) ((uint32_t)(uintptr_t)p)
9284 if (look_for_pages
) {
9285 submap_info
->user_tag
= curr_entry
->alias
;
9286 submap_info
->offset
= curr_entry
->offset
;
9287 submap_info
->protection
= curr_entry
->protection
;
9288 submap_info
->inheritance
= curr_entry
->inheritance
;
9289 submap_info
->max_protection
= curr_entry
->max_protection
;
9290 submap_info
->behavior
= curr_entry
->behavior
;
9291 submap_info
->user_wired_count
= curr_entry
->user_wired_count
;
9292 submap_info
->is_submap
= curr_entry
->is_sub_map
;
9293 submap_info
->object_id
= INFO_MAKE_OBJECT_ID(curr_entry
->object
.vm_object
);
9295 short_info
->user_tag
= curr_entry
->alias
;
9296 short_info
->offset
= curr_entry
->offset
;
9297 short_info
->protection
= curr_entry
->protection
;
9298 short_info
->inheritance
= curr_entry
->inheritance
;
9299 short_info
->max_protection
= curr_entry
->max_protection
;
9300 short_info
->behavior
= curr_entry
->behavior
;
9301 short_info
->user_wired_count
= curr_entry
->user_wired_count
;
9302 short_info
->is_submap
= curr_entry
->is_sub_map
;
9303 short_info
->object_id
= INFO_MAKE_OBJECT_ID(curr_entry
->object
.vm_object
);
9306 extended
.pages_resident
= 0;
9307 extended
.pages_swapped_out
= 0;
9308 extended
.pages_shared_now_private
= 0;
9309 extended
.pages_dirtied
= 0;
9310 extended
.external_pager
= 0;
9311 extended
.shadow_depth
= 0;
9314 if (!curr_entry
->is_sub_map
) {
9315 vm_map_offset_t range_start
, range_end
;
9316 range_start
= MAX((curr_address
- curr_max_below
),
9317 curr_entry
->vme_start
);
9318 range_end
= MIN((curr_address
+ curr_max_above
),
9319 curr_entry
->vme_end
);
9320 vm_map_region_walk(curr_map
,
9323 (curr_entry
->offset
+
9325 curr_entry
->vme_start
)),
9326 range_end
- range_start
,
9329 if (extended
.external_pager
&&
9330 extended
.ref_count
== 2 &&
9331 extended
.share_mode
== SM_SHARED
) {
9332 extended
.share_mode
= SM_PRIVATE
;
9335 if (curr_entry
->use_pmap
) {
9336 extended
.share_mode
= SM_TRUESHARED
;
9338 extended
.share_mode
= SM_PRIVATE
;
9340 extended
.ref_count
=
9341 curr_entry
->object
.sub_map
->ref_count
;
9345 if (look_for_pages
) {
9346 submap_info
->pages_resident
= extended
.pages_resident
;
9347 submap_info
->pages_swapped_out
= extended
.pages_swapped_out
;
9348 submap_info
->pages_shared_now_private
=
9349 extended
.pages_shared_now_private
;
9350 submap_info
->pages_dirtied
= extended
.pages_dirtied
;
9351 submap_info
->external_pager
= extended
.external_pager
;
9352 submap_info
->shadow_depth
= extended
.shadow_depth
;
9353 submap_info
->share_mode
= extended
.share_mode
;
9354 submap_info
->ref_count
= extended
.ref_count
;
9356 short_info
->external_pager
= extended
.external_pager
;
9357 short_info
->shadow_depth
= extended
.shadow_depth
;
9358 short_info
->share_mode
= extended
.share_mode
;
9359 short_info
->ref_count
= extended
.ref_count
;
9363 vm_map_unlock_read(curr_map
);
9366 return KERN_SUCCESS
;
9372 * User call to obtain information about a region in
9373 * a task's address map. Currently, only one flavor is
9376 * XXX The reserved and behavior fields cannot be filled
9377 * in until the vm merge from the IK is completed, and
9378 * vm_reserve is implemented.
9384 vm_map_offset_t
*address
, /* IN/OUT */
9385 vm_map_size_t
*size
, /* OUT */
9386 vm_region_flavor_t flavor
, /* IN */
9387 vm_region_info_t info
, /* OUT */
9388 mach_msg_type_number_t
*count
, /* IN/OUT */
9389 mach_port_t
*object_name
) /* OUT */
9391 vm_map_entry_t tmp_entry
;
9392 vm_map_entry_t entry
;
9393 vm_map_offset_t start
;
9395 if (map
== VM_MAP_NULL
)
9396 return(KERN_INVALID_ARGUMENT
);
9400 case VM_REGION_BASIC_INFO
:
9401 /* legacy for old 32-bit objects info */
9403 vm_region_basic_info_t basic
;
9405 if (*count
< VM_REGION_BASIC_INFO_COUNT
)
9406 return(KERN_INVALID_ARGUMENT
);
9408 basic
= (vm_region_basic_info_t
) info
;
9409 *count
= VM_REGION_BASIC_INFO_COUNT
;
9411 vm_map_lock_read(map
);
9414 if (!vm_map_lookup_entry(map
, start
, &tmp_entry
)) {
9415 if ((entry
= tmp_entry
->vme_next
) == vm_map_to_entry(map
)) {
9416 vm_map_unlock_read(map
);
9417 return(KERN_INVALID_ADDRESS
);
9423 start
= entry
->vme_start
;
9425 basic
->offset
= (uint32_t)entry
->offset
;
9426 basic
->protection
= entry
->protection
;
9427 basic
->inheritance
= entry
->inheritance
;
9428 basic
->max_protection
= entry
->max_protection
;
9429 basic
->behavior
= entry
->behavior
;
9430 basic
->user_wired_count
= entry
->user_wired_count
;
9431 basic
->reserved
= entry
->is_sub_map
;
9433 *size
= (entry
->vme_end
- start
);
9435 if (object_name
) *object_name
= IP_NULL
;
9436 if (entry
->is_sub_map
) {
9437 basic
->shared
= FALSE
;
9439 basic
->shared
= entry
->is_shared
;
9442 vm_map_unlock_read(map
);
9443 return(KERN_SUCCESS
);
9446 case VM_REGION_BASIC_INFO_64
:
9448 vm_region_basic_info_64_t basic
;
9450 if (*count
< VM_REGION_BASIC_INFO_COUNT_64
)
9451 return(KERN_INVALID_ARGUMENT
);
9453 basic
= (vm_region_basic_info_64_t
) info
;
9454 *count
= VM_REGION_BASIC_INFO_COUNT_64
;
9456 vm_map_lock_read(map
);
9459 if (!vm_map_lookup_entry(map
, start
, &tmp_entry
)) {
9460 if ((entry
= tmp_entry
->vme_next
) == vm_map_to_entry(map
)) {
9461 vm_map_unlock_read(map
);
9462 return(KERN_INVALID_ADDRESS
);
9468 start
= entry
->vme_start
;
9470 basic
->offset
= entry
->offset
;
9471 basic
->protection
= entry
->protection
;
9472 basic
->inheritance
= entry
->inheritance
;
9473 basic
->max_protection
= entry
->max_protection
;
9474 basic
->behavior
= entry
->behavior
;
9475 basic
->user_wired_count
= entry
->user_wired_count
;
9476 basic
->reserved
= entry
->is_sub_map
;
9478 *size
= (entry
->vme_end
- start
);
9480 if (object_name
) *object_name
= IP_NULL
;
9481 if (entry
->is_sub_map
) {
9482 basic
->shared
= FALSE
;
9484 basic
->shared
= entry
->is_shared
;
9487 vm_map_unlock_read(map
);
9488 return(KERN_SUCCESS
);
9490 case VM_REGION_EXTENDED_INFO
:
9492 vm_region_extended_info_t extended
;
9494 if (*count
< VM_REGION_EXTENDED_INFO_COUNT
)
9495 return(KERN_INVALID_ARGUMENT
);
9497 extended
= (vm_region_extended_info_t
) info
;
9498 *count
= VM_REGION_EXTENDED_INFO_COUNT
;
9500 vm_map_lock_read(map
);
9503 if (!vm_map_lookup_entry(map
, start
, &tmp_entry
)) {
9504 if ((entry
= tmp_entry
->vme_next
) == vm_map_to_entry(map
)) {
9505 vm_map_unlock_read(map
);
9506 return(KERN_INVALID_ADDRESS
);
9511 start
= entry
->vme_start
;
9513 extended
->protection
= entry
->protection
;
9514 extended
->user_tag
= entry
->alias
;
9515 extended
->pages_resident
= 0;
9516 extended
->pages_swapped_out
= 0;
9517 extended
->pages_shared_now_private
= 0;
9518 extended
->pages_dirtied
= 0;
9519 extended
->external_pager
= 0;
9520 extended
->shadow_depth
= 0;
9522 vm_map_region_walk(map
, start
, entry
, entry
->offset
, entry
->vme_end
- start
, extended
, TRUE
);
9524 if (extended
->external_pager
&& extended
->ref_count
== 2 && extended
->share_mode
== SM_SHARED
)
9525 extended
->share_mode
= SM_PRIVATE
;
9528 *object_name
= IP_NULL
;
9530 *size
= (entry
->vme_end
- start
);
9532 vm_map_unlock_read(map
);
9533 return(KERN_SUCCESS
);
9535 case VM_REGION_TOP_INFO
:
9537 vm_region_top_info_t top
;
9539 if (*count
< VM_REGION_TOP_INFO_COUNT
)
9540 return(KERN_INVALID_ARGUMENT
);
9542 top
= (vm_region_top_info_t
) info
;
9543 *count
= VM_REGION_TOP_INFO_COUNT
;
9545 vm_map_lock_read(map
);
9548 if (!vm_map_lookup_entry(map
, start
, &tmp_entry
)) {
9549 if ((entry
= tmp_entry
->vme_next
) == vm_map_to_entry(map
)) {
9550 vm_map_unlock_read(map
);
9551 return(KERN_INVALID_ADDRESS
);
9557 start
= entry
->vme_start
;
9559 top
->private_pages_resident
= 0;
9560 top
->shared_pages_resident
= 0;
9562 vm_map_region_top_walk(entry
, top
);
9565 *object_name
= IP_NULL
;
9567 *size
= (entry
->vme_end
- start
);
9569 vm_map_unlock_read(map
);
9570 return(KERN_SUCCESS
);
9573 return(KERN_INVALID_ARGUMENT
);
9577 #define OBJ_RESIDENT_COUNT(obj, entry_size) \
9579 ((obj)->all_reusable ? \
9580 (obj)->wired_page_count : \
9581 (obj)->resident_page_count - (obj)->reusable_page_count))
9584 vm_map_region_top_walk(
9585 vm_map_entry_t entry
,
9586 vm_region_top_info_t top
)
9589 if (entry
->object
.vm_object
== 0 || entry
->is_sub_map
) {
9590 top
->share_mode
= SM_EMPTY
;
9597 struct vm_object
*obj
, *tmp_obj
;
9599 uint32_t entry_size
;
9601 entry_size
= (uint32_t) ((entry
->vme_end
- entry
->vme_start
) / PAGE_SIZE_64
);
9603 obj
= entry
->object
.vm_object
;
9605 vm_object_lock(obj
);
9607 if ((ref_count
= obj
->ref_count
) > 1 && obj
->paging_in_progress
)
9610 assert(obj
->reusable_page_count
<= obj
->resident_page_count
);
9613 top
->private_pages_resident
=
9614 OBJ_RESIDENT_COUNT(obj
, entry_size
);
9616 top
->shared_pages_resident
=
9617 OBJ_RESIDENT_COUNT(obj
, entry_size
);
9618 top
->ref_count
= ref_count
;
9619 top
->share_mode
= SM_COW
;
9621 while ((tmp_obj
= obj
->shadow
)) {
9622 vm_object_lock(tmp_obj
);
9623 vm_object_unlock(obj
);
9626 if ((ref_count
= obj
->ref_count
) > 1 && obj
->paging_in_progress
)
9629 assert(obj
->reusable_page_count
<= obj
->resident_page_count
);
9630 top
->shared_pages_resident
+=
9631 OBJ_RESIDENT_COUNT(obj
, entry_size
);
9632 top
->ref_count
+= ref_count
- 1;
9635 if (entry
->superpage_size
) {
9636 top
->share_mode
= SM_LARGE_PAGE
;
9637 top
->shared_pages_resident
= 0;
9638 top
->private_pages_resident
= entry_size
;
9639 } else if (entry
->needs_copy
) {
9640 top
->share_mode
= SM_COW
;
9641 top
->shared_pages_resident
=
9642 OBJ_RESIDENT_COUNT(obj
, entry_size
);
9644 if (ref_count
== 1 ||
9645 (ref_count
== 2 && !(obj
->pager_trusted
) && !(obj
->internal
))) {
9646 top
->share_mode
= SM_PRIVATE
;
9647 top
->private_pages_resident
=
9648 OBJ_RESIDENT_COUNT(obj
,
9651 top
->share_mode
= SM_SHARED
;
9652 top
->shared_pages_resident
=
9653 OBJ_RESIDENT_COUNT(obj
,
9657 top
->ref_count
= ref_count
;
9659 /* XXX K64: obj_id will be truncated */
9660 top
->obj_id
= (unsigned int) (uintptr_t)obj
;
9662 vm_object_unlock(obj
);
9670 vm_map_entry_t entry
,
9671 vm_object_offset_t offset
,
9672 vm_object_size_t range
,
9673 vm_region_extended_info_t extended
,
9674 boolean_t look_for_pages
)
9676 register struct vm_object
*obj
, *tmp_obj
;
9677 register vm_map_offset_t last_offset
;
9679 register int ref_count
;
9680 struct vm_object
*shadow_object
;
9683 if ((entry
->object
.vm_object
== 0) ||
9684 (entry
->is_sub_map
) ||
9685 (entry
->object
.vm_object
->phys_contiguous
&&
9686 !entry
->superpage_size
)) {
9687 extended
->share_mode
= SM_EMPTY
;
9688 extended
->ref_count
= 0;
9692 if (entry
->superpage_size
) {
9693 extended
->shadow_depth
= 0;
9694 extended
->share_mode
= SM_LARGE_PAGE
;
9695 extended
->ref_count
= 1;
9696 extended
->external_pager
= 0;
9697 extended
->pages_resident
= (unsigned int)(range
>> PAGE_SHIFT
);
9698 extended
->shadow_depth
= 0;
9703 obj
= entry
->object
.vm_object
;
9705 vm_object_lock(obj
);
9707 if ((ref_count
= obj
->ref_count
) > 1 && obj
->paging_in_progress
)
9710 if (look_for_pages
) {
9711 for (last_offset
= offset
+ range
;
9712 offset
< last_offset
;
9713 offset
+= PAGE_SIZE_64
, va
+= PAGE_SIZE
)
9714 vm_map_region_look_for_page(map
, va
, obj
,
9718 shadow_object
= obj
->shadow
;
9721 if ( !(obj
->pager_trusted
) && !(obj
->internal
))
9722 extended
->external_pager
= 1;
9724 if (shadow_object
!= VM_OBJECT_NULL
) {
9725 vm_object_lock(shadow_object
);
9727 shadow_object
!= VM_OBJECT_NULL
;
9729 vm_object_t next_shadow
;
9731 if ( !(shadow_object
->pager_trusted
) &&
9732 !(shadow_object
->internal
))
9733 extended
->external_pager
= 1;
9735 next_shadow
= shadow_object
->shadow
;
9737 vm_object_lock(next_shadow
);
9739 vm_object_unlock(shadow_object
);
9740 shadow_object
= next_shadow
;
9743 extended
->shadow_depth
= shadow_depth
;
9746 if (extended
->shadow_depth
|| entry
->needs_copy
)
9747 extended
->share_mode
= SM_COW
;
9750 extended
->share_mode
= SM_PRIVATE
;
9752 if (obj
->true_share
)
9753 extended
->share_mode
= SM_TRUESHARED
;
9755 extended
->share_mode
= SM_SHARED
;
9758 extended
->ref_count
= ref_count
- extended
->shadow_depth
;
9760 for (i
= 0; i
< extended
->shadow_depth
; i
++) {
9761 if ((tmp_obj
= obj
->shadow
) == 0)
9763 vm_object_lock(tmp_obj
);
9764 vm_object_unlock(obj
);
9766 if ((ref_count
= tmp_obj
->ref_count
) > 1 && tmp_obj
->paging_in_progress
)
9769 extended
->ref_count
+= ref_count
;
9772 vm_object_unlock(obj
);
9774 if (extended
->share_mode
== SM_SHARED
) {
9775 register vm_map_entry_t cur
;
9776 register vm_map_entry_t last
;
9779 obj
= entry
->object
.vm_object
;
9780 last
= vm_map_to_entry(map
);
9783 if ((ref_count
= obj
->ref_count
) > 1 && obj
->paging_in_progress
)
9785 for (cur
= vm_map_first_entry(map
); cur
!= last
; cur
= cur
->vme_next
)
9786 my_refs
+= vm_map_region_count_obj_refs(cur
, obj
);
9788 if (my_refs
== ref_count
)
9789 extended
->share_mode
= SM_PRIVATE_ALIASED
;
9790 else if (my_refs
> 1)
9791 extended
->share_mode
= SM_SHARED_ALIASED
;
9797 /* object is locked on entry and locked on return */
9801 vm_map_region_look_for_page(
9802 __unused vm_map_t map
,
9803 __unused vm_map_offset_t va
,
9805 vm_object_offset_t offset
,
9808 vm_region_extended_info_t extended
)
9810 register vm_page_t p
;
9811 register vm_object_t shadow
;
9812 register int ref_count
;
9813 vm_object_t caller_object
;
9817 shadow
= object
->shadow
;
9818 caller_object
= object
;
9823 if ( !(object
->pager_trusted
) && !(object
->internal
))
9824 extended
->external_pager
= 1;
9826 if ((p
= vm_page_lookup(object
, offset
)) != VM_PAGE_NULL
) {
9827 if (shadow
&& (max_refcnt
== 1))
9828 extended
->pages_shared_now_private
++;
9830 if (!p
->fictitious
&&
9831 (p
->dirty
|| pmap_is_modified(p
->phys_page
)))
9832 extended
->pages_dirtied
++;
9834 extended
->pages_resident
++;
9836 if(object
!= caller_object
)
9837 vm_object_unlock(object
);
9842 if (object
->existence_map
) {
9843 if (vm_external_state_get(object
->existence_map
, offset
) == VM_EXTERNAL_STATE_EXISTS
) {
9845 extended
->pages_swapped_out
++;
9847 if(object
!= caller_object
)
9848 vm_object_unlock(object
);
9852 } else if (object
->internal
&&
9854 !object
->terminating
&&
9855 object
->pager_ready
) {
9857 memory_object_t pager
;
9859 vm_object_paging_begin(object
);
9860 pager
= object
->pager
;
9861 vm_object_unlock(object
);
9863 kr
= memory_object_data_request(
9865 offset
+ object
->paging_offset
,
9866 0, /* just poke the pager */
9870 vm_object_lock(object
);
9871 vm_object_paging_end(object
);
9873 if (kr
== KERN_SUCCESS
) {
9874 /* the pager has that page */
9875 extended
->pages_swapped_out
++;
9876 if (object
!= caller_object
)
9877 vm_object_unlock(object
);
9881 #endif /* MACH_PAGEMAP */
9884 vm_object_lock(shadow
);
9886 if ((ref_count
= shadow
->ref_count
) > 1 && shadow
->paging_in_progress
)
9889 if (++depth
> extended
->shadow_depth
)
9890 extended
->shadow_depth
= depth
;
9892 if (ref_count
> max_refcnt
)
9893 max_refcnt
= ref_count
;
9895 if(object
!= caller_object
)
9896 vm_object_unlock(object
);
9898 offset
= offset
+ object
->vo_shadow_offset
;
9900 shadow
= object
->shadow
;
9903 if(object
!= caller_object
)
9904 vm_object_unlock(object
);
9910 vm_map_region_count_obj_refs(
9911 vm_map_entry_t entry
,
9914 register int ref_count
;
9915 register vm_object_t chk_obj
;
9916 register vm_object_t tmp_obj
;
9918 if (entry
->object
.vm_object
== 0)
9921 if (entry
->is_sub_map
)
9926 chk_obj
= entry
->object
.vm_object
;
9927 vm_object_lock(chk_obj
);
9930 if (chk_obj
== object
)
9932 tmp_obj
= chk_obj
->shadow
;
9934 vm_object_lock(tmp_obj
);
9935 vm_object_unlock(chk_obj
);
9945 * Routine: vm_map_simplify
9948 * Attempt to simplify the map representation in
9949 * the vicinity of the given starting address.
9951 * This routine is intended primarily to keep the
9952 * kernel maps more compact -- they generally don't
9953 * benefit from the "expand a map entry" technology
9954 * at allocation time because the adjacent entry
9955 * is often wired down.
9958 vm_map_simplify_entry(
9960 vm_map_entry_t this_entry
)
9962 vm_map_entry_t prev_entry
;
9964 counter(c_vm_map_simplify_entry_called
++);
9966 prev_entry
= this_entry
->vme_prev
;
9968 if ((this_entry
!= vm_map_to_entry(map
)) &&
9969 (prev_entry
!= vm_map_to_entry(map
)) &&
9971 (prev_entry
->vme_end
== this_entry
->vme_start
) &&
9973 (prev_entry
->is_sub_map
== this_entry
->is_sub_map
) &&
9975 (prev_entry
->object
.vm_object
== this_entry
->object
.vm_object
) &&
9976 ((prev_entry
->offset
+ (prev_entry
->vme_end
-
9977 prev_entry
->vme_start
))
9978 == this_entry
->offset
) &&
9980 (prev_entry
->inheritance
== this_entry
->inheritance
) &&
9981 (prev_entry
->protection
== this_entry
->protection
) &&
9982 (prev_entry
->max_protection
== this_entry
->max_protection
) &&
9983 (prev_entry
->behavior
== this_entry
->behavior
) &&
9984 (prev_entry
->alias
== this_entry
->alias
) &&
9985 (prev_entry
->zero_wired_pages
== this_entry
->zero_wired_pages
) &&
9986 (prev_entry
->no_cache
== this_entry
->no_cache
) &&
9987 (prev_entry
->wired_count
== this_entry
->wired_count
) &&
9988 (prev_entry
->user_wired_count
== this_entry
->user_wired_count
) &&
9990 (prev_entry
->needs_copy
== this_entry
->needs_copy
) &&
9991 (prev_entry
->permanent
== this_entry
->permanent
) &&
9993 (prev_entry
->use_pmap
== FALSE
) &&
9994 (this_entry
->use_pmap
== FALSE
) &&
9995 (prev_entry
->in_transition
== FALSE
) &&
9996 (this_entry
->in_transition
== FALSE
) &&
9997 (prev_entry
->needs_wakeup
== FALSE
) &&
9998 (this_entry
->needs_wakeup
== FALSE
) &&
9999 (prev_entry
->is_shared
== FALSE
) &&
10000 (this_entry
->is_shared
== FALSE
)
10002 _vm_map_store_entry_unlink(&map
->hdr
, prev_entry
);
10003 this_entry
->vme_start
= prev_entry
->vme_start
;
10004 this_entry
->offset
= prev_entry
->offset
;
10005 if (prev_entry
->is_sub_map
) {
10006 vm_map_deallocate(prev_entry
->object
.sub_map
);
10008 vm_object_deallocate(prev_entry
->object
.vm_object
);
10010 vm_map_entry_dispose(map
, prev_entry
);
10011 SAVE_HINT_MAP_WRITE(map
, this_entry
);
10012 counter(c_vm_map_simplified
++);
10019 vm_map_offset_t start
)
10021 vm_map_entry_t this_entry
;
10024 if (vm_map_lookup_entry(map
, start
, &this_entry
)) {
10025 vm_map_simplify_entry(map
, this_entry
);
10026 vm_map_simplify_entry(map
, this_entry
->vme_next
);
10028 counter(c_vm_map_simplify_called
++);
10029 vm_map_unlock(map
);
10033 vm_map_simplify_range(
10035 vm_map_offset_t start
,
10036 vm_map_offset_t end
)
10038 vm_map_entry_t entry
;
10041 * The map should be locked (for "write") by the caller.
10044 if (start
>= end
) {
10045 /* invalid address range */
10049 start
= vm_map_trunc_page(start
);
10050 end
= vm_map_round_page(end
);
10052 if (!vm_map_lookup_entry(map
, start
, &entry
)) {
10053 /* "start" is not mapped and "entry" ends before "start" */
10054 if (entry
== vm_map_to_entry(map
)) {
10055 /* start with first entry in the map */
10056 entry
= vm_map_first_entry(map
);
10058 /* start with next entry */
10059 entry
= entry
->vme_next
;
10063 while (entry
!= vm_map_to_entry(map
) &&
10064 entry
->vme_start
<= end
) {
10065 /* try and coalesce "entry" with its previous entry */
10066 vm_map_simplify_entry(map
, entry
);
10067 entry
= entry
->vme_next
;
10073 * Routine: vm_map_machine_attribute
10075 * Provide machine-specific attributes to mappings,
10076 * such as cachability etc. for machines that provide
10077 * them. NUMA architectures and machines with big/strange
10078 * caches will use this.
10080 * Responsibilities for locking and checking are handled here,
10081 * everything else in the pmap module. If any non-volatile
10082 * information must be kept, the pmap module should handle
10083 * it itself. [This assumes that attributes do not
10084 * need to be inherited, which seems ok to me]
10087 vm_map_machine_attribute(
10089 vm_map_offset_t start
,
10090 vm_map_offset_t end
,
10091 vm_machine_attribute_t attribute
,
10092 vm_machine_attribute_val_t
* value
) /* IN/OUT */
10095 vm_map_size_t sync_size
;
10096 vm_map_entry_t entry
;
10098 if (start
< vm_map_min(map
) || end
> vm_map_max(map
))
10099 return KERN_INVALID_ADDRESS
;
10101 /* Figure how much memory we need to flush (in page increments) */
10102 sync_size
= end
- start
;
10106 if (attribute
!= MATTR_CACHE
) {
10107 /* If we don't have to find physical addresses, we */
10108 /* don't have to do an explicit traversal here. */
10109 ret
= pmap_attribute(map
->pmap
, start
, end
-start
,
10111 vm_map_unlock(map
);
10115 ret
= KERN_SUCCESS
; /* Assume it all worked */
10118 if (vm_map_lookup_entry(map
, start
, &entry
)) {
10119 vm_map_size_t sub_size
;
10120 if((entry
->vme_end
- start
) > sync_size
) {
10121 sub_size
= sync_size
;
10124 sub_size
= entry
->vme_end
- start
;
10125 sync_size
-= sub_size
;
10127 if(entry
->is_sub_map
) {
10128 vm_map_offset_t sub_start
;
10129 vm_map_offset_t sub_end
;
10131 sub_start
= (start
- entry
->vme_start
)
10133 sub_end
= sub_start
+ sub_size
;
10134 vm_map_machine_attribute(
10135 entry
->object
.sub_map
,
10140 if(entry
->object
.vm_object
) {
10142 vm_object_t object
;
10143 vm_object_t base_object
;
10144 vm_object_t last_object
;
10145 vm_object_offset_t offset
;
10146 vm_object_offset_t base_offset
;
10147 vm_map_size_t range
;
10149 offset
= (start
- entry
->vme_start
)
10151 base_offset
= offset
;
10152 object
= entry
->object
.vm_object
;
10153 base_object
= object
;
10154 last_object
= NULL
;
10156 vm_object_lock(object
);
10159 m
= vm_page_lookup(
10162 if (m
&& !m
->fictitious
) {
10164 pmap_attribute_cache_sync(
10169 } else if (object
->shadow
) {
10170 offset
= offset
+ object
->vo_shadow_offset
;
10171 last_object
= object
;
10172 object
= object
->shadow
;
10173 vm_object_lock(last_object
->shadow
);
10174 vm_object_unlock(last_object
);
10177 range
-= PAGE_SIZE
;
10179 if (base_object
!= object
) {
10180 vm_object_unlock(object
);
10181 vm_object_lock(base_object
);
10182 object
= base_object
;
10184 /* Bump to the next page */
10185 base_offset
+= PAGE_SIZE
;
10186 offset
= base_offset
;
10188 vm_object_unlock(object
);
10193 vm_map_unlock(map
);
10194 return KERN_FAILURE
;
10199 vm_map_unlock(map
);
10205 * vm_map_behavior_set:
10207 * Sets the paging reference behavior of the specified address
10208 * range in the target map. Paging reference behavior affects
10209 * how pagein operations resulting from faults on the map will be
10213 vm_map_behavior_set(
10215 vm_map_offset_t start
,
10216 vm_map_offset_t end
,
10217 vm_behavior_t new_behavior
)
10219 register vm_map_entry_t entry
;
10220 vm_map_entry_t temp_entry
;
10223 "vm_map_behavior_set, 0x%X start 0x%X end 0x%X behavior %d",
10224 map
, start
, end
, new_behavior
, 0);
10227 start
< vm_map_min(map
) ||
10228 end
> vm_map_max(map
)) {
10229 return KERN_NO_SPACE
;
10232 switch (new_behavior
) {
10235 * This first block of behaviors all set a persistent state on the specified
10236 * memory range. All we have to do here is to record the desired behavior
10237 * in the vm_map_entry_t's.
10240 case VM_BEHAVIOR_DEFAULT
:
10241 case VM_BEHAVIOR_RANDOM
:
10242 case VM_BEHAVIOR_SEQUENTIAL
:
10243 case VM_BEHAVIOR_RSEQNTL
:
10244 case VM_BEHAVIOR_ZERO_WIRED_PAGES
:
10248 * The entire address range must be valid for the map.
10249 * Note that vm_map_range_check() does a
10250 * vm_map_lookup_entry() internally and returns the
10251 * entry containing the start of the address range if
10252 * the entire range is valid.
10254 if (vm_map_range_check(map
, start
, end
, &temp_entry
)) {
10255 entry
= temp_entry
;
10256 vm_map_clip_start(map
, entry
, start
);
10259 vm_map_unlock(map
);
10260 return(KERN_INVALID_ADDRESS
);
10263 while ((entry
!= vm_map_to_entry(map
)) && (entry
->vme_start
< end
)) {
10264 vm_map_clip_end(map
, entry
, end
);
10265 assert(!entry
->use_pmap
);
10267 if( new_behavior
== VM_BEHAVIOR_ZERO_WIRED_PAGES
) {
10268 entry
->zero_wired_pages
= TRUE
;
10270 entry
->behavior
= new_behavior
;
10272 entry
= entry
->vme_next
;
10275 vm_map_unlock(map
);
10279 * The rest of these are different from the above in that they cause
10280 * an immediate action to take place as opposed to setting a behavior that
10281 * affects future actions.
10284 case VM_BEHAVIOR_WILLNEED
:
10285 return vm_map_willneed(map
, start
, end
);
10287 case VM_BEHAVIOR_DONTNEED
:
10288 return vm_map_msync(map
, start
, end
- start
, VM_SYNC_DEACTIVATE
| VM_SYNC_CONTIGUOUS
);
10290 case VM_BEHAVIOR_FREE
:
10291 return vm_map_msync(map
, start
, end
- start
, VM_SYNC_KILLPAGES
| VM_SYNC_CONTIGUOUS
);
10293 case VM_BEHAVIOR_REUSABLE
:
10294 return vm_map_reusable_pages(map
, start
, end
);
10296 case VM_BEHAVIOR_REUSE
:
10297 return vm_map_reuse_pages(map
, start
, end
);
10299 case VM_BEHAVIOR_CAN_REUSE
:
10300 return vm_map_can_reuse(map
, start
, end
);
10303 return(KERN_INVALID_ARGUMENT
);
10306 return(KERN_SUCCESS
);
10311 * Internals for madvise(MADV_WILLNEED) system call.
10313 * The present implementation is to do a read-ahead if the mapping corresponds
10314 * to a mapped regular file. If it's an anonymous mapping, then we do nothing
10315 * and basically ignore the "advice" (which we are always free to do).
10319 static kern_return_t
10322 vm_map_offset_t start
,
10323 vm_map_offset_t end
10326 vm_map_entry_t entry
;
10327 vm_object_t object
;
10328 memory_object_t pager
;
10329 struct vm_object_fault_info fault_info
;
10331 vm_object_size_t len
;
10332 vm_object_offset_t offset
;
10335 * Fill in static values in fault_info. Several fields get ignored by the code
10336 * we call, but we'll fill them in anyway since uninitialized fields are bad
10337 * when it comes to future backwards compatibility.
10340 fault_info
.interruptible
= THREAD_UNINT
; /* ignored value */
10341 fault_info
.behavior
= VM_BEHAVIOR_SEQUENTIAL
;
10342 fault_info
.no_cache
= FALSE
; /* ignored value */
10343 fault_info
.stealth
= TRUE
;
10344 fault_info
.io_sync
= FALSE
;
10345 fault_info
.cs_bypass
= FALSE
;
10346 fault_info
.mark_zf_absent
= FALSE
;
10349 * The MADV_WILLNEED operation doesn't require any changes to the
10350 * vm_map_entry_t's, so the read lock is sufficient.
10353 vm_map_lock_read(map
);
10356 * The madvise semantics require that the address range be fully
10357 * allocated with no holes. Otherwise, we're required to return
10361 if (! vm_map_range_check(map
, start
, end
, &entry
)) {
10362 vm_map_unlock_read(map
);
10363 return KERN_INVALID_ADDRESS
;
10367 * Examine each vm_map_entry_t in the range.
10369 for (; entry
!= vm_map_to_entry(map
) && start
< end
; ) {
10372 * The first time through, the start address could be anywhere
10373 * within the vm_map_entry we found. So adjust the offset to
10374 * correspond. After that, the offset will always be zero to
10375 * correspond to the beginning of the current vm_map_entry.
10377 offset
= (start
- entry
->vme_start
) + entry
->offset
;
10380 * Set the length so we don't go beyond the end of the
10381 * map_entry or beyond the end of the range we were given.
10382 * This range could span also multiple map entries all of which
10383 * map different files, so make sure we only do the right amount
10384 * of I/O for each object. Note that it's possible for there
10385 * to be multiple map entries all referring to the same object
10386 * but with different page permissions, but it's not worth
10387 * trying to optimize that case.
10389 len
= MIN(entry
->vme_end
- start
, end
- start
);
10391 if ((vm_size_t
) len
!= len
) {
10392 /* 32-bit overflow */
10393 len
= (vm_size_t
) (0 - PAGE_SIZE
);
10395 fault_info
.cluster_size
= (vm_size_t
) len
;
10396 fault_info
.lo_offset
= offset
;
10397 fault_info
.hi_offset
= offset
+ len
;
10398 fault_info
.user_tag
= entry
->alias
;
10401 * If there's no read permission to this mapping, then just
10404 if ((entry
->protection
& VM_PROT_READ
) == 0) {
10405 entry
= entry
->vme_next
;
10406 start
= entry
->vme_start
;
10411 * Find the file object backing this map entry. If there is
10412 * none, then we simply ignore the "will need" advice for this
10413 * entry and go on to the next one.
10415 if ((object
= find_vnode_object(entry
)) == VM_OBJECT_NULL
) {
10416 entry
= entry
->vme_next
;
10417 start
= entry
->vme_start
;
10422 * The data_request() could take a long time, so let's
10423 * release the map lock to avoid blocking other threads.
10425 vm_map_unlock_read(map
);
10427 vm_object_paging_begin(object
);
10428 pager
= object
->pager
;
10429 vm_object_unlock(object
);
10432 * Get the data from the object asynchronously.
10434 * Note that memory_object_data_request() places limits on the
10435 * amount of I/O it will do. Regardless of the len we
10436 * specified, it won't do more than MAX_UPL_TRANSFER and it
10437 * silently truncates the len to that size. This isn't
10438 * necessarily bad since madvise shouldn't really be used to
10439 * page in unlimited amounts of data. Other Unix variants
10440 * limit the willneed case as well. If this turns out to be an
10441 * issue for developers, then we can always adjust the policy
10442 * here and still be backwards compatible since this is all
10445 kr
= memory_object_data_request(
10447 offset
+ object
->paging_offset
,
10450 (memory_object_fault_info_t
)&fault_info
);
10452 vm_object_lock(object
);
10453 vm_object_paging_end(object
);
10454 vm_object_unlock(object
);
10457 * If we couldn't do the I/O for some reason, just give up on
10458 * the madvise. We still return success to the user since
10459 * madvise isn't supposed to fail when the advice can't be
10462 if (kr
!= KERN_SUCCESS
) {
10463 return KERN_SUCCESS
;
10467 if (start
>= end
) {
10469 return KERN_SUCCESS
;
10472 /* look up next entry */
10473 vm_map_lock_read(map
);
10474 if (! vm_map_lookup_entry(map
, start
, &entry
)) {
10476 * There's a new hole in the address range.
10478 vm_map_unlock_read(map
);
10479 return KERN_INVALID_ADDRESS
;
10483 vm_map_unlock_read(map
);
10484 return KERN_SUCCESS
;
10488 vm_map_entry_is_reusable(
10489 vm_map_entry_t entry
)
10491 vm_object_t object
;
10493 if (entry
->is_shared
||
10494 entry
->is_sub_map
||
10495 entry
->in_transition
||
10496 entry
->protection
!= VM_PROT_DEFAULT
||
10497 entry
->max_protection
!= VM_PROT_ALL
||
10498 entry
->inheritance
!= VM_INHERIT_DEFAULT
||
10500 entry
->permanent
||
10501 entry
->superpage_size
!= 0 ||
10502 entry
->zero_wired_pages
||
10503 entry
->wired_count
!= 0 ||
10504 entry
->user_wired_count
!= 0) {
10508 object
= entry
->object
.vm_object
;
10509 if (object
== VM_OBJECT_NULL
) {
10512 if (object
->ref_count
== 1 &&
10513 object
->wired_page_count
== 0 &&
10514 object
->copy
== VM_OBJECT_NULL
&&
10515 object
->shadow
== VM_OBJECT_NULL
&&
10516 object
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
&&
10517 object
->internal
&&
10518 !object
->true_share
&&
10519 object
->wimg_bits
== VM_WIMG_USE_DEFAULT
&&
10520 !object
->code_signed
) {
10528 static kern_return_t
10529 vm_map_reuse_pages(
10531 vm_map_offset_t start
,
10532 vm_map_offset_t end
)
10534 vm_map_entry_t entry
;
10535 vm_object_t object
;
10536 vm_object_offset_t start_offset
, end_offset
;
10539 * The MADV_REUSE operation doesn't require any changes to the
10540 * vm_map_entry_t's, so the read lock is sufficient.
10543 vm_map_lock_read(map
);
10546 * The madvise semantics require that the address range be fully
10547 * allocated with no holes. Otherwise, we're required to return
10551 if (!vm_map_range_check(map
, start
, end
, &entry
)) {
10552 vm_map_unlock_read(map
);
10553 vm_page_stats_reusable
.reuse_pages_failure
++;
10554 return KERN_INVALID_ADDRESS
;
10558 * Examine each vm_map_entry_t in the range.
10560 for (; entry
!= vm_map_to_entry(map
) && entry
->vme_start
< end
;
10561 entry
= entry
->vme_next
) {
10563 * Sanity check on the VM map entry.
10565 if (! vm_map_entry_is_reusable(entry
)) {
10566 vm_map_unlock_read(map
);
10567 vm_page_stats_reusable
.reuse_pages_failure
++;
10568 return KERN_INVALID_ADDRESS
;
10572 * The first time through, the start address could be anywhere
10573 * within the vm_map_entry we found. So adjust the offset to
10576 if (entry
->vme_start
< start
) {
10577 start_offset
= start
- entry
->vme_start
;
10581 end_offset
= MIN(end
, entry
->vme_end
) - entry
->vme_start
;
10582 start_offset
+= entry
->offset
;
10583 end_offset
+= entry
->offset
;
10585 object
= entry
->object
.vm_object
;
10586 if (object
!= VM_OBJECT_NULL
) {
10587 vm_object_lock(object
);
10588 vm_object_reuse_pages(object
, start_offset
, end_offset
,
10590 vm_object_unlock(object
);
10593 if (entry
->alias
== VM_MEMORY_MALLOC_LARGE_REUSABLE
) {
10596 * We do not hold the VM map exclusively here.
10597 * The "alias" field is not that critical, so it's
10598 * safe to update it here, as long as it is the only
10599 * one that can be modified while holding the VM map
10602 entry
->alias
= VM_MEMORY_MALLOC_LARGE_REUSED
;
10606 vm_map_unlock_read(map
);
10607 vm_page_stats_reusable
.reuse_pages_success
++;
10608 return KERN_SUCCESS
;
10612 static kern_return_t
10613 vm_map_reusable_pages(
10615 vm_map_offset_t start
,
10616 vm_map_offset_t end
)
10618 vm_map_entry_t entry
;
10619 vm_object_t object
;
10620 vm_object_offset_t start_offset
, end_offset
;
10623 * The MADV_REUSABLE operation doesn't require any changes to the
10624 * vm_map_entry_t's, so the read lock is sufficient.
10627 vm_map_lock_read(map
);
10630 * The madvise semantics require that the address range be fully
10631 * allocated with no holes. Otherwise, we're required to return
10635 if (!vm_map_range_check(map
, start
, end
, &entry
)) {
10636 vm_map_unlock_read(map
);
10637 vm_page_stats_reusable
.reusable_pages_failure
++;
10638 return KERN_INVALID_ADDRESS
;
10642 * Examine each vm_map_entry_t in the range.
10644 for (; entry
!= vm_map_to_entry(map
) && entry
->vme_start
< end
;
10645 entry
= entry
->vme_next
) {
10646 int kill_pages
= 0;
10649 * Sanity check on the VM map entry.
10651 if (! vm_map_entry_is_reusable(entry
)) {
10652 vm_map_unlock_read(map
);
10653 vm_page_stats_reusable
.reusable_pages_failure
++;
10654 return KERN_INVALID_ADDRESS
;
10658 * The first time through, the start address could be anywhere
10659 * within the vm_map_entry we found. So adjust the offset to
10662 if (entry
->vme_start
< start
) {
10663 start_offset
= start
- entry
->vme_start
;
10667 end_offset
= MIN(end
, entry
->vme_end
) - entry
->vme_start
;
10668 start_offset
+= entry
->offset
;
10669 end_offset
+= entry
->offset
;
10671 object
= entry
->object
.vm_object
;
10672 if (object
== VM_OBJECT_NULL
)
10676 vm_object_lock(object
);
10677 if (object
->ref_count
== 1 && !object
->shadow
)
10681 if (kill_pages
!= -1) {
10682 vm_object_deactivate_pages(object
,
10684 end_offset
- start_offset
,
10686 TRUE
/*reusable_pages*/);
10688 vm_page_stats_reusable
.reusable_pages_shared
++;
10690 vm_object_unlock(object
);
10692 if (entry
->alias
== VM_MEMORY_MALLOC_LARGE
||
10693 entry
->alias
== VM_MEMORY_MALLOC_LARGE_REUSED
) {
10696 * We do not hold the VM map exclusively here.
10697 * The "alias" field is not that critical, so it's
10698 * safe to update it here, as long as it is the only
10699 * one that can be modified while holding the VM map
10702 entry
->alias
= VM_MEMORY_MALLOC_LARGE_REUSABLE
;
10706 vm_map_unlock_read(map
);
10707 vm_page_stats_reusable
.reusable_pages_success
++;
10708 return KERN_SUCCESS
;
10712 static kern_return_t
10715 vm_map_offset_t start
,
10716 vm_map_offset_t end
)
10718 vm_map_entry_t entry
;
10721 * The MADV_REUSABLE operation doesn't require any changes to the
10722 * vm_map_entry_t's, so the read lock is sufficient.
10725 vm_map_lock_read(map
);
10728 * The madvise semantics require that the address range be fully
10729 * allocated with no holes. Otherwise, we're required to return
10733 if (!vm_map_range_check(map
, start
, end
, &entry
)) {
10734 vm_map_unlock_read(map
);
10735 vm_page_stats_reusable
.can_reuse_failure
++;
10736 return KERN_INVALID_ADDRESS
;
10740 * Examine each vm_map_entry_t in the range.
10742 for (; entry
!= vm_map_to_entry(map
) && entry
->vme_start
< end
;
10743 entry
= entry
->vme_next
) {
10745 * Sanity check on the VM map entry.
10747 if (! vm_map_entry_is_reusable(entry
)) {
10748 vm_map_unlock_read(map
);
10749 vm_page_stats_reusable
.can_reuse_failure
++;
10750 return KERN_INVALID_ADDRESS
;
10754 vm_map_unlock_read(map
);
10755 vm_page_stats_reusable
.can_reuse_success
++;
10756 return KERN_SUCCESS
;
10761 #include <mach_kdb.h>
10763 #include <ddb/db_output.h>
10764 #include <vm/vm_print.h>
10766 #define printf db_printf
10769 * Forward declarations for internal functions.
10771 extern void vm_map_links_print(
10772 struct vm_map_links
*links
);
10774 extern void vm_map_header_print(
10775 struct vm_map_header
*header
);
10777 extern void vm_map_entry_print(
10778 vm_map_entry_t entry
);
10780 extern void vm_follow_entry(
10781 vm_map_entry_t entry
);
10783 extern void vm_follow_map(
10787 * vm_map_links_print: [ debug ]
10790 vm_map_links_print(
10791 struct vm_map_links
*links
)
10793 iprintf("prev = %08X next = %08X start = %016llX end = %016llX\n",
10796 (unsigned long long)links
->start
,
10797 (unsigned long long)links
->end
);
10801 * vm_map_header_print: [ debug ]
10804 vm_map_header_print(
10805 struct vm_map_header
*header
)
10807 vm_map_links_print(&header
->links
);
10808 iprintf("nentries = %08X, %sentries_pageable\n",
10810 (header
->entries_pageable
? "" : "!"));
10814 * vm_follow_entry: [ debug ]
10818 vm_map_entry_t entry
)
10822 iprintf("map entry %08X\n", entry
);
10826 shadows
= vm_follow_object(entry
->object
.vm_object
);
10827 iprintf("Total objects : %d\n",shadows
);
10833 * vm_map_entry_print: [ debug ]
10836 vm_map_entry_print(
10837 register vm_map_entry_t entry
)
10839 static const char *inheritance_name
[4] =
10840 { "share", "copy", "none", "?"};
10841 static const char *behavior_name
[4] =
10842 { "dflt", "rand", "seqtl", "rseqntl" };
10844 iprintf("map entry %08X - prev = %08X next = %08X\n", entry
, entry
->vme_prev
, entry
->vme_next
);
10848 vm_map_links_print(&entry
->links
);
10850 iprintf("start = %016llX end = %016llX - prot=%x/%x/%s\n",
10851 (unsigned long long)entry
->vme_start
,
10852 (unsigned long long)entry
->vme_end
,
10854 entry
->max_protection
,
10855 inheritance_name
[(entry
->inheritance
& 0x3)]);
10857 iprintf("behavior = %s, wired_count = %d, user_wired_count = %d\n",
10858 behavior_name
[(entry
->behavior
& 0x3)],
10859 entry
->wired_count
,
10860 entry
->user_wired_count
);
10861 iprintf("%sin_transition, %sneeds_wakeup\n",
10862 (entry
->in_transition
? "" : "!"),
10863 (entry
->needs_wakeup
? "" : "!"));
10865 if (entry
->is_sub_map
) {
10866 iprintf("submap = %08X - offset = %016llX\n",
10867 entry
->object
.sub_map
,
10868 (unsigned long long)entry
->offset
);
10870 iprintf("object = %08X offset = %016llX - ",
10871 entry
->object
.vm_object
,
10872 (unsigned long long)entry
->offset
);
10873 printf("%sis_shared, %sneeds_copy\n",
10874 (entry
->is_shared
? "" : "!"),
10875 (entry
->needs_copy
? "" : "!"));
10882 * vm_follow_map: [ debug ]
10888 register vm_map_entry_t entry
;
10890 iprintf("task map %08X\n", map
);
10894 for (entry
= vm_map_first_entry(map
);
10895 entry
&& entry
!= vm_map_to_entry(map
);
10896 entry
= entry
->vme_next
) {
10897 vm_follow_entry(entry
);
10904 * vm_map_print: [ debug ]
10910 register vm_map_entry_t entry
;
10914 #endif /* TASK_SWAPPER */
10916 map
= (vm_map_t
)(long)
10917 inmap
; /* Make sure we have the right type */
10919 iprintf("task map %08X\n", map
);
10923 vm_map_header_print(&map
->hdr
);
10925 iprintf("pmap = %08X size = %08X ref = %d hint = %08X first_free = %08X\n",
10932 iprintf("%swait_for_space, %swiring_required, timestamp = %d\n",
10933 (map
->wait_for_space
? "" : "!"),
10934 (map
->wiring_required
? "" : "!"),
10938 switch (map
->sw_state
) {
10943 swstate
= "SW_OUT";
10949 iprintf("res = %d, sw_state = %s\n", map
->res_count
, swstate
);
10950 #endif /* TASK_SWAPPER */
10952 for (entry
= vm_map_first_entry(map
);
10953 entry
&& entry
!= vm_map_to_entry(map
);
10954 entry
= entry
->vme_next
) {
10955 vm_map_entry_print(entry
);
10962 * Routine: vm_map_copy_print
10964 * Pretty-print a copy object for ddb.
10971 vm_map_copy_t copy
;
10972 vm_map_entry_t entry
;
10974 copy
= (vm_map_copy_t
)(long)
10975 incopy
; /* Make sure we have the right type */
10977 printf("copy object 0x%x\n", copy
);
10981 iprintf("type=%d", copy
->type
);
10982 switch (copy
->type
) {
10983 case VM_MAP_COPY_ENTRY_LIST
:
10984 printf("[entry_list]");
10987 case VM_MAP_COPY_OBJECT
:
10988 printf("[object]");
10991 case VM_MAP_COPY_KERNEL_BUFFER
:
10992 printf("[kernel_buffer]");
10996 printf("[bad type]");
10999 printf(", offset=0x%llx", (unsigned long long)copy
->offset
);
11000 printf(", size=0x%x\n", copy
->size
);
11002 switch (copy
->type
) {
11003 case VM_MAP_COPY_ENTRY_LIST
:
11004 vm_map_header_print(©
->cpy_hdr
);
11005 for (entry
= vm_map_copy_first_entry(copy
);
11006 entry
&& entry
!= vm_map_copy_to_entry(copy
);
11007 entry
= entry
->vme_next
) {
11008 vm_map_entry_print(entry
);
11012 case VM_MAP_COPY_OBJECT
:
11013 iprintf("object=0x%x\n", copy
->cpy_object
);
11016 case VM_MAP_COPY_KERNEL_BUFFER
:
11017 iprintf("kernel buffer=0x%x", copy
->cpy_kdata
);
11018 printf(", kalloc_size=0x%x\n", copy
->cpy_kalloc_size
);
11027 * db_vm_map_total_size(map) [ debug ]
11029 * return the total virtual size (in bytes) of the map
11032 db_vm_map_total_size(
11035 vm_map_entry_t entry
;
11036 vm_map_size_t total
;
11039 map
= (vm_map_t
)(long)
11040 inmap
; /* Make sure we have the right type */
11043 for (entry
= vm_map_first_entry(map
);
11044 entry
!= vm_map_to_entry(map
);
11045 entry
= entry
->vme_next
) {
11046 total
+= entry
->vme_end
- entry
->vme_start
;
11052 #endif /* MACH_KDB */
11055 * Routine: vm_map_entry_insert
11057 * Descritpion: This routine inserts a new vm_entry in a locked map.
11060 vm_map_entry_insert(
11062 vm_map_entry_t insp_entry
,
11063 vm_map_offset_t start
,
11064 vm_map_offset_t end
,
11065 vm_object_t object
,
11066 vm_object_offset_t offset
,
11067 boolean_t needs_copy
,
11068 boolean_t is_shared
,
11069 boolean_t in_transition
,
11070 vm_prot_t cur_protection
,
11071 vm_prot_t max_protection
,
11072 vm_behavior_t behavior
,
11073 vm_inherit_t inheritance
,
11074 unsigned wired_count
,
11075 boolean_t no_cache
,
11076 boolean_t permanent
,
11077 unsigned int superpage_size
)
11079 vm_map_entry_t new_entry
;
11081 assert(insp_entry
!= (vm_map_entry_t
)0);
11083 new_entry
= vm_map_entry_create(map
);
11085 new_entry
->vme_start
= start
;
11086 new_entry
->vme_end
= end
;
11087 assert(page_aligned(new_entry
->vme_start
));
11088 assert(page_aligned(new_entry
->vme_end
));
11090 new_entry
->object
.vm_object
= object
;
11091 new_entry
->offset
= offset
;
11092 new_entry
->is_shared
= is_shared
;
11093 new_entry
->is_sub_map
= FALSE
;
11094 new_entry
->needs_copy
= needs_copy
;
11095 new_entry
->in_transition
= in_transition
;
11096 new_entry
->needs_wakeup
= FALSE
;
11097 new_entry
->inheritance
= inheritance
;
11098 new_entry
->protection
= cur_protection
;
11099 new_entry
->max_protection
= max_protection
;
11100 new_entry
->behavior
= behavior
;
11101 new_entry
->wired_count
= wired_count
;
11102 new_entry
->user_wired_count
= 0;
11103 new_entry
->use_pmap
= FALSE
;
11104 new_entry
->alias
= 0;
11105 new_entry
->zero_wired_pages
= FALSE
;
11106 new_entry
->no_cache
= no_cache
;
11107 new_entry
->permanent
= permanent
;
11108 new_entry
->superpage_size
= superpage_size
;
11109 new_entry
->used_for_jit
= FALSE
;
11112 * Insert the new entry into the list.
11115 vm_map_store_entry_link(map
, insp_entry
, new_entry
);
11116 map
->size
+= end
- start
;
11119 * Update the free space hint and the lookup hint.
11122 SAVE_HINT_MAP_WRITE(map
, new_entry
);
11127 * Routine: vm_map_remap_extract
11129 * Descritpion: This routine returns a vm_entry list from a map.
11131 static kern_return_t
11132 vm_map_remap_extract(
11134 vm_map_offset_t addr
,
11135 vm_map_size_t size
,
11137 struct vm_map_header
*map_header
,
11138 vm_prot_t
*cur_protection
,
11139 vm_prot_t
*max_protection
,
11140 /* What, no behavior? */
11141 vm_inherit_t inheritance
,
11142 boolean_t pageable
)
11144 kern_return_t result
;
11145 vm_map_size_t mapped_size
;
11146 vm_map_size_t tmp_size
;
11147 vm_map_entry_t src_entry
; /* result of last map lookup */
11148 vm_map_entry_t new_entry
;
11149 vm_object_offset_t offset
;
11150 vm_map_offset_t map_address
;
11151 vm_map_offset_t src_start
; /* start of entry to map */
11152 vm_map_offset_t src_end
; /* end of region to be mapped */
11153 vm_object_t object
;
11154 vm_map_version_t version
;
11155 boolean_t src_needs_copy
;
11156 boolean_t new_entry_needs_copy
;
11158 assert(map
!= VM_MAP_NULL
);
11159 assert(size
!= 0 && size
== vm_map_round_page(size
));
11160 assert(inheritance
== VM_INHERIT_NONE
||
11161 inheritance
== VM_INHERIT_COPY
||
11162 inheritance
== VM_INHERIT_SHARE
);
11165 * Compute start and end of region.
11167 src_start
= vm_map_trunc_page(addr
);
11168 src_end
= vm_map_round_page(src_start
+ size
);
11171 * Initialize map_header.
11173 map_header
->links
.next
= (struct vm_map_entry
*)&map_header
->links
;
11174 map_header
->links
.prev
= (struct vm_map_entry
*)&map_header
->links
;
11175 map_header
->nentries
= 0;
11176 map_header
->entries_pageable
= pageable
;
11178 vm_map_store_init( map_header
);
11180 *cur_protection
= VM_PROT_ALL
;
11181 *max_protection
= VM_PROT_ALL
;
11185 result
= KERN_SUCCESS
;
11188 * The specified source virtual space might correspond to
11189 * multiple map entries, need to loop on them.
11192 while (mapped_size
!= size
) {
11193 vm_map_size_t entry_size
;
11196 * Find the beginning of the region.
11198 if (! vm_map_lookup_entry(map
, src_start
, &src_entry
)) {
11199 result
= KERN_INVALID_ADDRESS
;
11203 if (src_start
< src_entry
->vme_start
||
11204 (mapped_size
&& src_start
!= src_entry
->vme_start
)) {
11205 result
= KERN_INVALID_ADDRESS
;
11209 tmp_size
= size
- mapped_size
;
11210 if (src_end
> src_entry
->vme_end
)
11211 tmp_size
-= (src_end
- src_entry
->vme_end
);
11213 entry_size
= (vm_map_size_t
)(src_entry
->vme_end
-
11214 src_entry
->vme_start
);
11216 if(src_entry
->is_sub_map
) {
11217 vm_map_reference(src_entry
->object
.sub_map
);
11218 object
= VM_OBJECT_NULL
;
11220 object
= src_entry
->object
.vm_object
;
11222 if (object
== VM_OBJECT_NULL
) {
11223 object
= vm_object_allocate(entry_size
);
11224 src_entry
->offset
= 0;
11225 src_entry
->object
.vm_object
= object
;
11226 } else if (object
->copy_strategy
!=
11227 MEMORY_OBJECT_COPY_SYMMETRIC
) {
11229 * We are already using an asymmetric
11230 * copy, and therefore we already have
11231 * the right object.
11233 assert(!src_entry
->needs_copy
);
11234 } else if (src_entry
->needs_copy
|| object
->shadowed
||
11235 (object
->internal
&& !object
->true_share
&&
11236 !src_entry
->is_shared
&&
11237 object
->vo_size
> entry_size
)) {
11239 vm_object_shadow(&src_entry
->object
.vm_object
,
11240 &src_entry
->offset
,
11243 if (!src_entry
->needs_copy
&&
11244 (src_entry
->protection
& VM_PROT_WRITE
)) {
11247 prot
= src_entry
->protection
& ~VM_PROT_WRITE
;
11249 if (override_nx(map
, src_entry
->alias
) && prot
)
11250 prot
|= VM_PROT_EXECUTE
;
11253 vm_object_pmap_protect(
11254 src_entry
->object
.vm_object
,
11258 src_entry
->vme_start
,
11261 pmap_protect(vm_map_pmap(map
),
11262 src_entry
->vme_start
,
11263 src_entry
->vme_end
,
11268 object
= src_entry
->object
.vm_object
;
11269 src_entry
->needs_copy
= FALSE
;
11273 vm_object_lock(object
);
11274 vm_object_reference_locked(object
); /* object ref. for new entry */
11275 if (object
->copy_strategy
==
11276 MEMORY_OBJECT_COPY_SYMMETRIC
) {
11277 object
->copy_strategy
=
11278 MEMORY_OBJECT_COPY_DELAY
;
11280 vm_object_unlock(object
);
11283 offset
= src_entry
->offset
+ (src_start
- src_entry
->vme_start
);
11285 new_entry
= _vm_map_entry_create(map_header
);
11286 vm_map_entry_copy(new_entry
, src_entry
);
11287 new_entry
->use_pmap
= FALSE
; /* clr address space specifics */
11289 new_entry
->vme_start
= map_address
;
11290 new_entry
->vme_end
= map_address
+ tmp_size
;
11291 new_entry
->inheritance
= inheritance
;
11292 new_entry
->offset
= offset
;
11295 * The new region has to be copied now if required.
11299 src_entry
->is_shared
= TRUE
;
11300 new_entry
->is_shared
= TRUE
;
11301 if (!(new_entry
->is_sub_map
))
11302 new_entry
->needs_copy
= FALSE
;
11304 } else if (src_entry
->is_sub_map
) {
11305 /* make this a COW sub_map if not already */
11306 new_entry
->needs_copy
= TRUE
;
11307 object
= VM_OBJECT_NULL
;
11308 } else if (src_entry
->wired_count
== 0 &&
11309 vm_object_copy_quickly(&new_entry
->object
.vm_object
,
11311 (new_entry
->vme_end
-
11312 new_entry
->vme_start
),
11314 &new_entry_needs_copy
)) {
11316 new_entry
->needs_copy
= new_entry_needs_copy
;
11317 new_entry
->is_shared
= FALSE
;
11320 * Handle copy_on_write semantics.
11322 if (src_needs_copy
&& !src_entry
->needs_copy
) {
11325 prot
= src_entry
->protection
& ~VM_PROT_WRITE
;
11327 if (override_nx(map
, src_entry
->alias
) && prot
)
11328 prot
|= VM_PROT_EXECUTE
;
11330 vm_object_pmap_protect(object
,
11333 ((src_entry
->is_shared
11335 PMAP_NULL
: map
->pmap
),
11336 src_entry
->vme_start
,
11339 src_entry
->needs_copy
= TRUE
;
11342 * Throw away the old object reference of the new entry.
11344 vm_object_deallocate(object
);
11347 new_entry
->is_shared
= FALSE
;
11350 * The map can be safely unlocked since we
11351 * already hold a reference on the object.
11353 * Record the timestamp of the map for later
11354 * verification, and unlock the map.
11356 version
.main_timestamp
= map
->timestamp
;
11357 vm_map_unlock(map
); /* Increments timestamp once! */
11360 * Perform the copy.
11362 if (src_entry
->wired_count
> 0) {
11363 vm_object_lock(object
);
11364 result
= vm_object_copy_slowly(
11369 &new_entry
->object
.vm_object
);
11371 new_entry
->offset
= 0;
11372 new_entry
->needs_copy
= FALSE
;
11374 result
= vm_object_copy_strategically(
11378 &new_entry
->object
.vm_object
,
11379 &new_entry
->offset
,
11380 &new_entry_needs_copy
);
11382 new_entry
->needs_copy
= new_entry_needs_copy
;
11386 * Throw away the old object reference of the new entry.
11388 vm_object_deallocate(object
);
11390 if (result
!= KERN_SUCCESS
&&
11391 result
!= KERN_MEMORY_RESTART_COPY
) {
11392 _vm_map_entry_dispose(map_header
, new_entry
);
11397 * Verify that the map has not substantially
11398 * changed while the copy was being made.
11402 if (version
.main_timestamp
+ 1 != map
->timestamp
) {
11404 * Simple version comparison failed.
11406 * Retry the lookup and verify that the
11407 * same object/offset are still present.
11409 vm_object_deallocate(new_entry
->
11411 _vm_map_entry_dispose(map_header
, new_entry
);
11412 if (result
== KERN_MEMORY_RESTART_COPY
)
11413 result
= KERN_SUCCESS
;
11417 if (result
== KERN_MEMORY_RESTART_COPY
) {
11418 vm_object_reference(object
);
11423 _vm_map_store_entry_link(map_header
,
11424 map_header
->links
.prev
, new_entry
);
11426 /*Protections for submap mapping are irrelevant here*/
11427 if( !src_entry
->is_sub_map
) {
11428 *cur_protection
&= src_entry
->protection
;
11429 *max_protection
&= src_entry
->max_protection
;
11431 map_address
+= tmp_size
;
11432 mapped_size
+= tmp_size
;
11433 src_start
+= tmp_size
;
11437 vm_map_unlock(map
);
11438 if (result
!= KERN_SUCCESS
) {
11440 * Free all allocated elements.
11442 for (src_entry
= map_header
->links
.next
;
11443 src_entry
!= (struct vm_map_entry
*)&map_header
->links
;
11444 src_entry
= new_entry
) {
11445 new_entry
= src_entry
->vme_next
;
11446 _vm_map_store_entry_unlink(map_header
, src_entry
);
11447 vm_object_deallocate(src_entry
->object
.vm_object
);
11448 _vm_map_entry_dispose(map_header
, src_entry
);
11455 * Routine: vm_remap
11457 * Map portion of a task's address space.
11458 * Mapped region must not overlap more than
11459 * one vm memory object. Protections and
11460 * inheritance attributes remain the same
11461 * as in the original task and are out parameters.
11462 * Source and Target task can be identical
11463 * Other attributes are identical as for vm_map()
11467 vm_map_t target_map
,
11468 vm_map_address_t
*address
,
11469 vm_map_size_t size
,
11470 vm_map_offset_t mask
,
11473 vm_map_offset_t memory_address
,
11475 vm_prot_t
*cur_protection
,
11476 vm_prot_t
*max_protection
,
11477 vm_inherit_t inheritance
)
11479 kern_return_t result
;
11480 vm_map_entry_t entry
;
11481 vm_map_entry_t insp_entry
= VM_MAP_ENTRY_NULL
;
11482 vm_map_entry_t new_entry
;
11483 struct vm_map_header map_header
;
11485 if (target_map
== VM_MAP_NULL
)
11486 return KERN_INVALID_ARGUMENT
;
11488 switch (inheritance
) {
11489 case VM_INHERIT_NONE
:
11490 case VM_INHERIT_COPY
:
11491 case VM_INHERIT_SHARE
:
11492 if (size
!= 0 && src_map
!= VM_MAP_NULL
)
11496 return KERN_INVALID_ARGUMENT
;
11499 size
= vm_map_round_page(size
);
11501 result
= vm_map_remap_extract(src_map
, memory_address
,
11502 size
, copy
, &map_header
,
11509 if (result
!= KERN_SUCCESS
) {
11514 * Allocate/check a range of free virtual address
11515 * space for the target
11517 *address
= vm_map_trunc_page(*address
);
11518 vm_map_lock(target_map
);
11519 result
= vm_map_remap_range_allocate(target_map
, address
, size
,
11520 mask
, flags
, &insp_entry
);
11522 for (entry
= map_header
.links
.next
;
11523 entry
!= (struct vm_map_entry
*)&map_header
.links
;
11524 entry
= new_entry
) {
11525 new_entry
= entry
->vme_next
;
11526 _vm_map_store_entry_unlink(&map_header
, entry
);
11527 if (result
== KERN_SUCCESS
) {
11528 entry
->vme_start
+= *address
;
11529 entry
->vme_end
+= *address
;
11530 vm_map_store_entry_link(target_map
, insp_entry
, entry
);
11531 insp_entry
= entry
;
11533 if (!entry
->is_sub_map
) {
11534 vm_object_deallocate(entry
->object
.vm_object
);
11536 vm_map_deallocate(entry
->object
.sub_map
);
11538 _vm_map_entry_dispose(&map_header
, entry
);
11542 if( target_map
->disable_vmentry_reuse
== TRUE
) {
11543 if( target_map
->highest_entry_end
< insp_entry
->vme_end
){
11544 target_map
->highest_entry_end
= insp_entry
->vme_end
;
11548 if (result
== KERN_SUCCESS
) {
11549 target_map
->size
+= size
;
11550 SAVE_HINT_MAP_WRITE(target_map
, insp_entry
);
11552 vm_map_unlock(target_map
);
11554 if (result
== KERN_SUCCESS
&& target_map
->wiring_required
)
11555 result
= vm_map_wire(target_map
, *address
,
11556 *address
+ size
, *cur_protection
, TRUE
);
11561 * Routine: vm_map_remap_range_allocate
11564 * Allocate a range in the specified virtual address map.
11565 * returns the address and the map entry just before the allocated
11568 * Map must be locked.
11571 static kern_return_t
11572 vm_map_remap_range_allocate(
11574 vm_map_address_t
*address
, /* IN/OUT */
11575 vm_map_size_t size
,
11576 vm_map_offset_t mask
,
11578 vm_map_entry_t
*map_entry
) /* OUT */
11580 vm_map_entry_t entry
;
11581 vm_map_offset_t start
;
11582 vm_map_offset_t end
;
11589 if (flags
& VM_FLAGS_ANYWHERE
)
11592 * Calculate the first possible address.
11595 if (start
< map
->min_offset
)
11596 start
= map
->min_offset
;
11597 if (start
> map
->max_offset
)
11598 return(KERN_NO_SPACE
);
11601 * Look for the first possible address;
11602 * if there's already something at this
11603 * address, we have to start after it.
11606 if( map
->disable_vmentry_reuse
== TRUE
) {
11607 VM_MAP_HIGHEST_ENTRY(map
, entry
, start
);
11609 assert(first_free_is_valid(map
));
11610 if (start
== map
->min_offset
) {
11611 if ((entry
= map
->first_free
) != vm_map_to_entry(map
))
11612 start
= entry
->vme_end
;
11614 vm_map_entry_t tmp_entry
;
11615 if (vm_map_lookup_entry(map
, start
, &tmp_entry
))
11616 start
= tmp_entry
->vme_end
;
11622 * In any case, the "entry" always precedes
11623 * the proposed new region throughout the
11628 register vm_map_entry_t next
;
11631 * Find the end of the proposed new region.
11632 * Be sure we didn't go beyond the end, or
11633 * wrap around the address.
11636 end
= ((start
+ mask
) & ~mask
);
11638 return(KERN_NO_SPACE
);
11642 if ((end
> map
->max_offset
) || (end
< start
)) {
11643 if (map
->wait_for_space
) {
11644 if (size
<= (map
->max_offset
-
11645 map
->min_offset
)) {
11646 assert_wait((event_t
) map
, THREAD_INTERRUPTIBLE
);
11647 vm_map_unlock(map
);
11648 thread_block(THREAD_CONTINUE_NULL
);
11654 return(KERN_NO_SPACE
);
11658 * If there are no more entries, we must win.
11661 next
= entry
->vme_next
;
11662 if (next
== vm_map_to_entry(map
))
11666 * If there is another entry, it must be
11667 * after the end of the potential new region.
11670 if (next
->vme_start
>= end
)
11674 * Didn't fit -- move to the next entry.
11678 start
= entry
->vme_end
;
11682 vm_map_entry_t temp_entry
;
11686 * the address doesn't itself violate
11687 * the mask requirement.
11690 if ((start
& mask
) != 0)
11691 return(KERN_NO_SPACE
);
11695 * ... the address is within bounds
11698 end
= start
+ size
;
11700 if ((start
< map
->min_offset
) ||
11701 (end
> map
->max_offset
) ||
11703 return(KERN_INVALID_ADDRESS
);
11707 * If we're asked to overwrite whatever was mapped in that
11708 * range, first deallocate that range.
11710 if (flags
& VM_FLAGS_OVERWRITE
) {
11714 * We use a "zap_map" to avoid having to unlock
11715 * the "map" in vm_map_delete(), which would compromise
11716 * the atomicity of the "deallocate" and then "remap"
11719 zap_map
= vm_map_create(PMAP_NULL
,
11722 map
->hdr
.entries_pageable
);
11723 if (zap_map
== VM_MAP_NULL
) {
11724 return KERN_RESOURCE_SHORTAGE
;
11727 kr
= vm_map_delete(map
, start
, end
,
11728 VM_MAP_REMOVE_SAVE_ENTRIES
,
11730 if (kr
== KERN_SUCCESS
) {
11731 vm_map_destroy(zap_map
,
11732 VM_MAP_REMOVE_NO_PMAP_CLEANUP
);
11733 zap_map
= VM_MAP_NULL
;
11738 * ... the starting address isn't allocated
11741 if (vm_map_lookup_entry(map
, start
, &temp_entry
))
11742 return(KERN_NO_SPACE
);
11744 entry
= temp_entry
;
11747 * ... the next region doesn't overlap the
11751 if ((entry
->vme_next
!= vm_map_to_entry(map
)) &&
11752 (entry
->vme_next
->vme_start
< end
))
11753 return(KERN_NO_SPACE
);
11755 *map_entry
= entry
;
11756 return(KERN_SUCCESS
);
11762 * Set the address map for the current thread to the specified map
11770 thread_t thread
= current_thread();
11771 vm_map_t oldmap
= thread
->map
;
11773 mp_disable_preemption();
11774 mycpu
= cpu_number();
11777 * Deactivate the current map and activate the requested map
11779 PMAP_SWITCH_USER(thread
, map
, mycpu
);
11781 mp_enable_preemption();
11787 * Routine: vm_map_write_user
11790 * Copy out data from a kernel space into space in the
11791 * destination map. The space must already exist in the
11793 * NOTE: This routine should only be called by threads
11794 * which can block on a page fault. i.e. kernel mode user
11802 vm_map_address_t dst_addr
,
11805 kern_return_t kr
= KERN_SUCCESS
;
11807 if(current_map() == map
) {
11808 if (copyout(src_p
, dst_addr
, size
)) {
11809 kr
= KERN_INVALID_ADDRESS
;
11814 /* take on the identity of the target map while doing */
11817 vm_map_reference(map
);
11818 oldmap
= vm_map_switch(map
);
11819 if (copyout(src_p
, dst_addr
, size
)) {
11820 kr
= KERN_INVALID_ADDRESS
;
11822 vm_map_switch(oldmap
);
11823 vm_map_deallocate(map
);
11829 * Routine: vm_map_read_user
11832 * Copy in data from a user space source map into the
11833 * kernel map. The space must already exist in the
11835 * NOTE: This routine should only be called by threads
11836 * which can block on a page fault. i.e. kernel mode user
11843 vm_map_address_t src_addr
,
11847 kern_return_t kr
= KERN_SUCCESS
;
11849 if(current_map() == map
) {
11850 if (copyin(src_addr
, dst_p
, size
)) {
11851 kr
= KERN_INVALID_ADDRESS
;
11856 /* take on the identity of the target map while doing */
11859 vm_map_reference(map
);
11860 oldmap
= vm_map_switch(map
);
11861 if (copyin(src_addr
, dst_p
, size
)) {
11862 kr
= KERN_INVALID_ADDRESS
;
11864 vm_map_switch(oldmap
);
11865 vm_map_deallocate(map
);
11872 * vm_map_check_protection:
11874 * Assert that the target map allows the specified
11875 * privilege on the entire address region given.
11876 * The entire region must be allocated.
11879 vm_map_check_protection(vm_map_t map
, vm_map_offset_t start
,
11880 vm_map_offset_t end
, vm_prot_t protection
)
11882 vm_map_entry_t entry
;
11883 vm_map_entry_t tmp_entry
;
11887 if (start
< vm_map_min(map
) || end
> vm_map_max(map
) || start
> end
)
11889 vm_map_unlock(map
);
11893 if (!vm_map_lookup_entry(map
, start
, &tmp_entry
)) {
11894 vm_map_unlock(map
);
11900 while (start
< end
) {
11901 if (entry
== vm_map_to_entry(map
)) {
11902 vm_map_unlock(map
);
11907 * No holes allowed!
11910 if (start
< entry
->vme_start
) {
11911 vm_map_unlock(map
);
11916 * Check protection associated with entry.
11919 if ((entry
->protection
& protection
) != protection
) {
11920 vm_map_unlock(map
);
11924 /* go to next entry */
11926 start
= entry
->vme_end
;
11927 entry
= entry
->vme_next
;
11929 vm_map_unlock(map
);
11934 vm_map_purgable_control(
11936 vm_map_offset_t address
,
11937 vm_purgable_t control
,
11940 vm_map_entry_t entry
;
11941 vm_object_t object
;
11945 * Vet all the input parameters and current type and state of the
11946 * underlaying object. Return with an error if anything is amiss.
11948 if (map
== VM_MAP_NULL
)
11949 return(KERN_INVALID_ARGUMENT
);
11951 if (control
!= VM_PURGABLE_SET_STATE
&&
11952 control
!= VM_PURGABLE_GET_STATE
&&
11953 control
!= VM_PURGABLE_PURGE_ALL
)
11954 return(KERN_INVALID_ARGUMENT
);
11956 if (control
== VM_PURGABLE_PURGE_ALL
) {
11957 vm_purgeable_object_purge_all();
11958 return KERN_SUCCESS
;
11961 if (control
== VM_PURGABLE_SET_STATE
&&
11962 (((*state
& ~(VM_PURGABLE_ALL_MASKS
)) != 0) ||
11963 ((*state
& VM_PURGABLE_STATE_MASK
) > VM_PURGABLE_STATE_MASK
)))
11964 return(KERN_INVALID_ARGUMENT
);
11966 vm_map_lock_read(map
);
11968 if (!vm_map_lookup_entry(map
, address
, &entry
) || entry
->is_sub_map
) {
11971 * Must pass a valid non-submap address.
11973 vm_map_unlock_read(map
);
11974 return(KERN_INVALID_ADDRESS
);
11977 if ((entry
->protection
& VM_PROT_WRITE
) == 0) {
11979 * Can't apply purgable controls to something you can't write.
11981 vm_map_unlock_read(map
);
11982 return(KERN_PROTECTION_FAILURE
);
11985 object
= entry
->object
.vm_object
;
11986 if (object
== VM_OBJECT_NULL
) {
11988 * Object must already be present or it can't be purgable.
11990 vm_map_unlock_read(map
);
11991 return KERN_INVALID_ARGUMENT
;
11994 vm_object_lock(object
);
11996 if (entry
->offset
!= 0 ||
11997 entry
->vme_end
- entry
->vme_start
!= object
->vo_size
) {
11999 * Can only apply purgable controls to the whole (existing)
12002 vm_map_unlock_read(map
);
12003 vm_object_unlock(object
);
12004 return KERN_INVALID_ARGUMENT
;
12007 vm_map_unlock_read(map
);
12009 kr
= vm_object_purgable_control(object
, control
, state
);
12011 vm_object_unlock(object
);
12017 vm_map_page_query_internal(
12018 vm_map_t target_map
,
12019 vm_map_offset_t offset
,
12024 vm_page_info_basic_data_t info
;
12025 mach_msg_type_number_t count
;
12027 count
= VM_PAGE_INFO_BASIC_COUNT
;
12028 kr
= vm_map_page_info(target_map
,
12030 VM_PAGE_INFO_BASIC
,
12031 (vm_page_info_t
) &info
,
12033 if (kr
== KERN_SUCCESS
) {
12034 *disposition
= info
.disposition
;
12035 *ref_count
= info
.ref_count
;
12047 vm_map_offset_t offset
,
12048 vm_page_info_flavor_t flavor
,
12049 vm_page_info_t info
,
12050 mach_msg_type_number_t
*count
)
12052 vm_map_entry_t map_entry
;
12053 vm_object_t object
;
12056 kern_return_t retval
= KERN_SUCCESS
;
12057 boolean_t top_object
;
12060 vm_object_id_t object_id
;
12061 vm_page_info_basic_t basic_info
;
12063 vm_map_offset_t offset_in_page
;
12066 case VM_PAGE_INFO_BASIC
:
12067 if (*count
!= VM_PAGE_INFO_BASIC_COUNT
) {
12069 * The "vm_page_info_basic_data" structure was not
12070 * properly padded, so allow the size to be off by
12071 * one to maintain backwards binary compatibility...
12073 if (*count
!= VM_PAGE_INFO_BASIC_COUNT
- 1)
12074 return KERN_INVALID_ARGUMENT
;
12078 return KERN_INVALID_ARGUMENT
;
12087 retval
= KERN_SUCCESS
;
12088 offset_in_page
= offset
& PAGE_MASK
;
12089 offset
= vm_map_trunc_page(offset
);
12091 vm_map_lock_read(map
);
12094 * First, find the map entry covering "offset", going down
12095 * submaps if necessary.
12098 if (!vm_map_lookup_entry(map
, offset
, &map_entry
)) {
12099 vm_map_unlock_read(map
);
12100 return KERN_INVALID_ADDRESS
;
12102 /* compute offset from this map entry's start */
12103 offset
-= map_entry
->vme_start
;
12104 /* compute offset into this map entry's object (or submap) */
12105 offset
+= map_entry
->offset
;
12107 if (map_entry
->is_sub_map
) {
12110 sub_map
= map_entry
->object
.sub_map
;
12111 vm_map_lock_read(sub_map
);
12112 vm_map_unlock_read(map
);
12116 ref_count
= MAX(ref_count
, map
->ref_count
);
12122 object
= map_entry
->object
.vm_object
;
12123 if (object
== VM_OBJECT_NULL
) {
12124 /* no object -> no page */
12125 vm_map_unlock_read(map
);
12129 vm_object_lock(object
);
12130 vm_map_unlock_read(map
);
12133 * Go down the VM object shadow chain until we find the page
12134 * we're looking for.
12137 ref_count
= MAX(ref_count
, object
->ref_count
);
12139 m
= vm_page_lookup(object
, offset
);
12141 if (m
!= VM_PAGE_NULL
) {
12142 disposition
|= VM_PAGE_QUERY_PAGE_PRESENT
;
12146 if (object
->existence_map
) {
12147 if (vm_external_state_get(object
->existence_map
,
12149 VM_EXTERNAL_STATE_EXISTS
) {
12151 * this page has been paged out
12153 disposition
|= VM_PAGE_QUERY_PAGE_PAGED_OUT
;
12159 if (object
->internal
&&
12161 !object
->terminating
&&
12162 object
->pager_ready
) {
12164 memory_object_t pager
;
12166 vm_object_paging_begin(object
);
12167 pager
= object
->pager
;
12168 vm_object_unlock(object
);
12171 * Ask the default pager if
12172 * it has this page.
12174 kr
= memory_object_data_request(
12176 offset
+ object
->paging_offset
,
12177 0, /* just poke the pager */
12181 vm_object_lock(object
);
12182 vm_object_paging_end(object
);
12184 if (kr
== KERN_SUCCESS
) {
12185 /* the default pager has it */
12186 disposition
|= VM_PAGE_QUERY_PAGE_PAGED_OUT
;
12192 if (object
->shadow
!= VM_OBJECT_NULL
) {
12193 vm_object_t shadow
;
12195 offset
+= object
->vo_shadow_offset
;
12196 shadow
= object
->shadow
;
12198 vm_object_lock(shadow
);
12199 vm_object_unlock(object
);
12202 top_object
= FALSE
;
12205 // if (!object->internal)
12207 // retval = KERN_FAILURE;
12208 // goto done_with_object;
12213 /* The ref_count is not strictly accurate, it measures the number */
12214 /* of entities holding a ref on the object, they may not be mapping */
12215 /* the object or may not be mapping the section holding the */
12216 /* target page but its still a ball park number and though an over- */
12217 /* count, it picks up the copy-on-write cases */
12219 /* We could also get a picture of page sharing from pmap_attributes */
12220 /* but this would under count as only faulted-in mappings would */
12223 if (top_object
== TRUE
&& object
->shadow
)
12224 disposition
|= VM_PAGE_QUERY_PAGE_COPIED
;
12226 if (! object
->internal
)
12227 disposition
|= VM_PAGE_QUERY_PAGE_EXTERNAL
;
12229 if (m
== VM_PAGE_NULL
)
12230 goto done_with_object
;
12232 if (m
->fictitious
) {
12233 disposition
|= VM_PAGE_QUERY_PAGE_FICTITIOUS
;
12234 goto done_with_object
;
12236 if (m
->dirty
|| pmap_is_modified(m
->phys_page
))
12237 disposition
|= VM_PAGE_QUERY_PAGE_DIRTY
;
12239 if (m
->reference
|| pmap_is_referenced(m
->phys_page
))
12240 disposition
|= VM_PAGE_QUERY_PAGE_REF
;
12242 if (m
->speculative
)
12243 disposition
|= VM_PAGE_QUERY_PAGE_SPECULATIVE
;
12245 if (m
->cs_validated
)
12246 disposition
|= VM_PAGE_QUERY_PAGE_CS_VALIDATED
;
12248 disposition
|= VM_PAGE_QUERY_PAGE_CS_TAINTED
;
12251 vm_object_unlock(object
);
12255 case VM_PAGE_INFO_BASIC
:
12256 basic_info
= (vm_page_info_basic_t
) info
;
12257 basic_info
->disposition
= disposition
;
12258 basic_info
->ref_count
= ref_count
;
12259 basic_info
->object_id
= (vm_object_id_t
) (uintptr_t) object
;
12260 basic_info
->offset
=
12261 (memory_object_offset_t
) offset
+ offset_in_page
;
12262 basic_info
->depth
= depth
;
12272 * Synchronises the memory range specified with its backing store
12273 * image by either flushing or cleaning the contents to the appropriate
12274 * memory manager engaging in a memory object synchronize dialog with
12275 * the manager. The client doesn't return until the manager issues
12276 * m_o_s_completed message. MIG Magically converts user task parameter
12277 * to the task's address map.
12279 * interpretation of sync_flags
12280 * VM_SYNC_INVALIDATE - discard pages, only return precious
12281 * pages to manager.
12283 * VM_SYNC_INVALIDATE & (VM_SYNC_SYNCHRONOUS | VM_SYNC_ASYNCHRONOUS)
12284 * - discard pages, write dirty or precious
12285 * pages back to memory manager.
12287 * VM_SYNC_SYNCHRONOUS | VM_SYNC_ASYNCHRONOUS
12288 * - write dirty or precious pages back to
12289 * the memory manager.
12291 * VM_SYNC_CONTIGUOUS - does everything normally, but if there
12292 * is a hole in the region, and we would
12293 * have returned KERN_SUCCESS, return
12294 * KERN_INVALID_ADDRESS instead.
12297 * The memory object attributes have not yet been implemented, this
12298 * function will have to deal with the invalidate attribute
12301 * KERN_INVALID_TASK Bad task parameter
12302 * KERN_INVALID_ARGUMENT both sync and async were specified.
12303 * KERN_SUCCESS The usual.
12304 * KERN_INVALID_ADDRESS There was a hole in the region.
12310 vm_map_address_t address
,
12311 vm_map_size_t size
,
12312 vm_sync_t sync_flags
)
12315 msync_req_t new_msr
;
12316 queue_chain_t req_q
; /* queue of requests for this msync */
12317 vm_map_entry_t entry
;
12318 vm_map_size_t amount_left
;
12319 vm_object_offset_t offset
;
12320 boolean_t do_sync_req
;
12321 boolean_t had_hole
= FALSE
;
12322 memory_object_t pager
;
12324 if ((sync_flags
& VM_SYNC_ASYNCHRONOUS
) &&
12325 (sync_flags
& VM_SYNC_SYNCHRONOUS
))
12326 return(KERN_INVALID_ARGUMENT
);
12329 * align address and size on page boundaries
12331 size
= vm_map_round_page(address
+ size
) - vm_map_trunc_page(address
);
12332 address
= vm_map_trunc_page(address
);
12334 if (map
== VM_MAP_NULL
)
12335 return(KERN_INVALID_TASK
);
12338 return(KERN_SUCCESS
);
12340 queue_init(&req_q
);
12341 amount_left
= size
;
12343 while (amount_left
> 0) {
12344 vm_object_size_t flush_size
;
12345 vm_object_t object
;
12348 if (!vm_map_lookup_entry(map
,
12349 vm_map_trunc_page(address
), &entry
)) {
12351 vm_map_size_t skip
;
12354 * hole in the address map.
12359 * Check for empty map.
12361 if (entry
== vm_map_to_entry(map
) &&
12362 entry
->vme_next
== entry
) {
12363 vm_map_unlock(map
);
12367 * Check that we don't wrap and that
12368 * we have at least one real map entry.
12370 if ((map
->hdr
.nentries
== 0) ||
12371 (entry
->vme_next
->vme_start
< address
)) {
12372 vm_map_unlock(map
);
12376 * Move up to the next entry if needed
12378 skip
= (entry
->vme_next
->vme_start
- address
);
12379 if (skip
>= amount_left
)
12382 amount_left
-= skip
;
12383 address
= entry
->vme_next
->vme_start
;
12384 vm_map_unlock(map
);
12388 offset
= address
- entry
->vme_start
;
12391 * do we have more to flush than is contained in this
12394 if (amount_left
+ entry
->vme_start
+ offset
> entry
->vme_end
) {
12395 flush_size
= entry
->vme_end
-
12396 (entry
->vme_start
+ offset
);
12398 flush_size
= amount_left
;
12400 amount_left
-= flush_size
;
12401 address
+= flush_size
;
12403 if (entry
->is_sub_map
== TRUE
) {
12404 vm_map_t local_map
;
12405 vm_map_offset_t local_offset
;
12407 local_map
= entry
->object
.sub_map
;
12408 local_offset
= entry
->offset
;
12409 vm_map_unlock(map
);
12414 sync_flags
) == KERN_INVALID_ADDRESS
) {
12419 object
= entry
->object
.vm_object
;
12422 * We can't sync this object if the object has not been
12425 if (object
== VM_OBJECT_NULL
) {
12426 vm_map_unlock(map
);
12429 offset
+= entry
->offset
;
12431 vm_object_lock(object
);
12433 if (sync_flags
& (VM_SYNC_KILLPAGES
| VM_SYNC_DEACTIVATE
)) {
12434 int kill_pages
= 0;
12435 boolean_t reusable_pages
= FALSE
;
12437 if (sync_flags
& VM_SYNC_KILLPAGES
) {
12438 if (object
->ref_count
== 1 && !object
->shadow
)
12443 if (kill_pages
!= -1)
12444 vm_object_deactivate_pages(object
, offset
,
12445 (vm_object_size_t
)flush_size
, kill_pages
, reusable_pages
);
12446 vm_object_unlock(object
);
12447 vm_map_unlock(map
);
12451 * We can't sync this object if there isn't a pager.
12452 * Don't bother to sync internal objects, since there can't
12453 * be any "permanent" storage for these objects anyway.
12455 if ((object
->pager
== MEMORY_OBJECT_NULL
) ||
12456 (object
->internal
) || (object
->private)) {
12457 vm_object_unlock(object
);
12458 vm_map_unlock(map
);
12462 * keep reference on the object until syncing is done
12464 vm_object_reference_locked(object
);
12465 vm_object_unlock(object
);
12467 vm_map_unlock(map
);
12469 do_sync_req
= vm_object_sync(object
,
12472 sync_flags
& VM_SYNC_INVALIDATE
,
12473 ((sync_flags
& VM_SYNC_SYNCHRONOUS
) ||
12474 (sync_flags
& VM_SYNC_ASYNCHRONOUS
)),
12475 sync_flags
& VM_SYNC_SYNCHRONOUS
);
12477 * only send a m_o_s if we returned pages or if the entry
12478 * is writable (ie dirty pages may have already been sent back)
12480 if (!do_sync_req
) {
12481 if ((sync_flags
& VM_SYNC_INVALIDATE
) && object
->resident_page_count
== 0) {
12483 * clear out the clustering and read-ahead hints
12485 vm_object_lock(object
);
12487 object
->pages_created
= 0;
12488 object
->pages_used
= 0;
12489 object
->sequential
= 0;
12490 object
->last_alloc
= 0;
12492 vm_object_unlock(object
);
12494 vm_object_deallocate(object
);
12497 msync_req_alloc(new_msr
);
12499 vm_object_lock(object
);
12500 offset
+= object
->paging_offset
;
12502 new_msr
->offset
= offset
;
12503 new_msr
->length
= flush_size
;
12504 new_msr
->object
= object
;
12505 new_msr
->flag
= VM_MSYNC_SYNCHRONIZING
;
12509 * We can't sync this object if there isn't a pager. The
12510 * pager can disappear anytime we're not holding the object
12511 * lock. So this has to be checked anytime we goto re_iterate.
12514 pager
= object
->pager
;
12516 if (pager
== MEMORY_OBJECT_NULL
) {
12517 vm_object_unlock(object
);
12518 vm_object_deallocate(object
);
12522 queue_iterate(&object
->msr_q
, msr
, msync_req_t
, msr_q
) {
12524 * need to check for overlapping entry, if found, wait
12525 * on overlapping msr to be done, then reiterate
12528 if (msr
->flag
== VM_MSYNC_SYNCHRONIZING
&&
12529 ((offset
>= msr
->offset
&&
12530 offset
< (msr
->offset
+ msr
->length
)) ||
12531 (msr
->offset
>= offset
&&
12532 msr
->offset
< (offset
+ flush_size
))))
12534 assert_wait((event_t
) msr
,THREAD_INTERRUPTIBLE
);
12536 vm_object_unlock(object
);
12537 thread_block(THREAD_CONTINUE_NULL
);
12538 vm_object_lock(object
);
12542 }/* queue_iterate */
12544 queue_enter(&object
->msr_q
, new_msr
, msync_req_t
, msr_q
);
12546 vm_object_paging_begin(object
);
12547 vm_object_unlock(object
);
12549 queue_enter(&req_q
, new_msr
, msync_req_t
, req_q
);
12551 (void) memory_object_synchronize(
12555 sync_flags
& ~VM_SYNC_CONTIGUOUS
);
12557 vm_object_lock(object
);
12558 vm_object_paging_end(object
);
12559 vm_object_unlock(object
);
12563 * wait for memory_object_sychronize_completed messages from pager(s)
12566 while (!queue_empty(&req_q
)) {
12567 msr
= (msync_req_t
)queue_first(&req_q
);
12569 while(msr
->flag
!= VM_MSYNC_DONE
) {
12570 assert_wait((event_t
) msr
, THREAD_INTERRUPTIBLE
);
12572 thread_block(THREAD_CONTINUE_NULL
);
12575 queue_remove(&req_q
, msr
, msync_req_t
, req_q
);
12577 vm_object_deallocate(msr
->object
);
12578 msync_req_free(msr
);
12579 }/* queue_iterate */
12581 /* for proper msync() behaviour */
12582 if (had_hole
== TRUE
&& (sync_flags
& VM_SYNC_CONTIGUOUS
))
12583 return(KERN_INVALID_ADDRESS
);
12585 return(KERN_SUCCESS
);
12589 * Routine: convert_port_entry_to_map
12591 * Convert from a port specifying an entry or a task
12592 * to a map. Doesn't consume the port ref; produces a map ref,
12593 * which may be null. Unlike convert_port_to_map, the
12594 * port may be task or a named entry backed.
12601 convert_port_entry_to_map(
12605 vm_named_entry_t named_entry
;
12606 uint32_t try_failed_count
= 0;
12608 if(IP_VALID(port
) && (ip_kotype(port
) == IKOT_NAMED_ENTRY
)) {
12611 if(ip_active(port
) && (ip_kotype(port
)
12612 == IKOT_NAMED_ENTRY
)) {
12614 (vm_named_entry_t
)port
->ip_kobject
;
12615 if (!(lck_mtx_try_lock(&(named_entry
)->Lock
))) {
12618 try_failed_count
++;
12619 mutex_pause(try_failed_count
);
12622 named_entry
->ref_count
++;
12623 lck_mtx_unlock(&(named_entry
)->Lock
);
12625 if ((named_entry
->is_sub_map
) &&
12626 (named_entry
->protection
12627 & VM_PROT_WRITE
)) {
12628 map
= named_entry
->backing
.map
;
12630 mach_destroy_memory_entry(port
);
12631 return VM_MAP_NULL
;
12633 vm_map_reference_swap(map
);
12634 mach_destroy_memory_entry(port
);
12638 return VM_MAP_NULL
;
12642 map
= convert_port_to_map(port
);
12648 * Routine: convert_port_entry_to_object
12650 * Convert from a port specifying a named entry to an
12651 * object. Doesn't consume the port ref; produces a map ref,
12652 * which may be null.
12659 convert_port_entry_to_object(
12662 vm_object_t object
;
12663 vm_named_entry_t named_entry
;
12664 uint32_t try_failed_count
= 0;
12666 if(IP_VALID(port
) && (ip_kotype(port
) == IKOT_NAMED_ENTRY
)) {
12669 if(ip_active(port
) && (ip_kotype(port
)
12670 == IKOT_NAMED_ENTRY
)) {
12672 (vm_named_entry_t
)port
->ip_kobject
;
12673 if (!(lck_mtx_try_lock(&(named_entry
)->Lock
))) {
12676 try_failed_count
++;
12677 mutex_pause(try_failed_count
);
12680 named_entry
->ref_count
++;
12681 lck_mtx_unlock(&(named_entry
)->Lock
);
12683 if ((!named_entry
->is_sub_map
) &&
12684 (!named_entry
->is_pager
) &&
12685 (named_entry
->protection
12686 & VM_PROT_WRITE
)) {
12687 object
= named_entry
->backing
.object
;
12689 mach_destroy_memory_entry(port
);
12690 return (vm_object_t
)NULL
;
12692 vm_object_reference(named_entry
->backing
.object
);
12693 mach_destroy_memory_entry(port
);
12697 return (vm_object_t
)NULL
;
12700 return (vm_object_t
)NULL
;
12707 * Export routines to other components for the things we access locally through
12714 return (current_map_fast());
12718 * vm_map_reference:
12720 * Most code internal to the osfmk will go through a
12721 * macro defining this. This is always here for the
12722 * use of other kernel components.
12724 #undef vm_map_reference
12727 register vm_map_t map
)
12729 if (map
== VM_MAP_NULL
)
12732 lck_mtx_lock(&map
->s_lock
);
12734 assert(map
->res_count
> 0);
12735 assert(map
->ref_count
>= map
->res_count
);
12739 lck_mtx_unlock(&map
->s_lock
);
12743 * vm_map_deallocate:
12745 * Removes a reference from the specified map,
12746 * destroying it if no references remain.
12747 * The map should not be locked.
12751 register vm_map_t map
)
12755 if (map
== VM_MAP_NULL
)
12758 lck_mtx_lock(&map
->s_lock
);
12759 ref
= --map
->ref_count
;
12761 vm_map_res_deallocate(map
);
12762 lck_mtx_unlock(&map
->s_lock
);
12765 assert(map
->ref_count
== 0);
12766 lck_mtx_unlock(&map
->s_lock
);
12770 * The map residence count isn't decremented here because
12771 * the vm_map_delete below will traverse the entire map,
12772 * deleting entries, and the residence counts on objects
12773 * and sharing maps will go away then.
12777 vm_map_destroy(map
, VM_MAP_NO_FLAGS
);
12782 vm_map_disable_NX(vm_map_t map
)
12786 if (map
->pmap
== NULL
)
12789 pmap_disable_NX(map
->pmap
);
12793 vm_map_disallow_data_exec(vm_map_t map
)
12798 map
->map_disallow_data_exec
= TRUE
;
12801 /* XXX Consider making these constants (VM_MAX_ADDRESS and MACH_VM_MAX_ADDRESS)
12802 * more descriptive.
12805 vm_map_set_32bit(vm_map_t map
)
12807 map
->max_offset
= (vm_map_offset_t
)VM_MAX_ADDRESS
;
12812 vm_map_set_64bit(vm_map_t map
)
12814 map
->max_offset
= (vm_map_offset_t
)MACH_VM_MAX_ADDRESS
;
12818 vm_compute_max_offset(unsigned is64
)
12820 return (is64
? (vm_map_offset_t
)MACH_VM_MAX_ADDRESS
: (vm_map_offset_t
)VM_MAX_ADDRESS
);
12827 return map
->max_offset
> ((vm_map_offset_t
)VM_MAX_ADDRESS
);
12831 vm_map_has_4GB_pagezero(
12836 * We should lock the VM map (for read) here but we can get away
12837 * with it for now because there can't really be any race condition:
12838 * the VM map's min_offset is changed only when the VM map is created
12839 * and when the zero page is established (when the binary gets loaded),
12840 * and this routine gets called only when the task terminates and the
12841 * VM map is being torn down, and when a new map is created via
12842 * load_machfile()/execve().
12844 return (map
->min_offset
>= 0x100000000ULL
);
12848 vm_map_set_4GB_pagezero(vm_map_t map
)
12850 #if defined(__i386__)
12851 pmap_set_4GB_pagezero(map
->pmap
);
12853 #pragma unused(map)
12859 vm_map_clear_4GB_pagezero(vm_map_t map
)
12861 #if defined(__i386__)
12862 pmap_clear_4GB_pagezero(map
->pmap
);
12864 #pragma unused(map)
12869 * Raise a VM map's minimum offset.
12870 * To strictly enforce "page zero" reservation.
12873 vm_map_raise_min_offset(
12875 vm_map_offset_t new_min_offset
)
12877 vm_map_entry_t first_entry
;
12879 new_min_offset
= vm_map_round_page(new_min_offset
);
12883 if (new_min_offset
< map
->min_offset
) {
12885 * Can't move min_offset backwards, as that would expose
12886 * a part of the address space that was previously, and for
12887 * possibly good reasons, inaccessible.
12889 vm_map_unlock(map
);
12890 return KERN_INVALID_ADDRESS
;
12893 first_entry
= vm_map_first_entry(map
);
12894 if (first_entry
!= vm_map_to_entry(map
) &&
12895 first_entry
->vme_start
< new_min_offset
) {
12897 * Some memory was already allocated below the new
12898 * minimun offset. It's too late to change it now...
12900 vm_map_unlock(map
);
12901 return KERN_NO_SPACE
;
12904 map
->min_offset
= new_min_offset
;
12906 vm_map_unlock(map
);
12908 return KERN_SUCCESS
;
12912 * Set the limit on the maximum amount of user wired memory allowed for this map.
12913 * This is basically a copy of the MEMLOCK rlimit value maintained by the BSD side of
12914 * the kernel. The limits are checked in the mach VM side, so we keep a copy so we
12915 * don't have to reach over to the BSD data structures.
12919 vm_map_set_user_wire_limit(vm_map_t map
,
12922 map
->user_wire_limit
= limit
;
12926 void vm_map_switch_protect(vm_map_t map
,
12930 map
->switch_protect
=val
;
12931 vm_map_unlock(map
);
12934 /* Add (generate) code signature for memory range */
12935 #if CONFIG_DYNAMIC_CODE_SIGNING
12936 kern_return_t
vm_map_sign(vm_map_t map
,
12937 vm_map_offset_t start
,
12938 vm_map_offset_t end
)
12940 vm_map_entry_t entry
;
12942 vm_object_t object
;
12945 * Vet all the input parameters and current type and state of the
12946 * underlaying object. Return with an error if anything is amiss.
12948 if (map
== VM_MAP_NULL
)
12949 return(KERN_INVALID_ARGUMENT
);
12951 vm_map_lock_read(map
);
12953 if (!vm_map_lookup_entry(map
, start
, &entry
) || entry
->is_sub_map
) {
12955 * Must pass a valid non-submap address.
12957 vm_map_unlock_read(map
);
12958 return(KERN_INVALID_ADDRESS
);
12961 if((entry
->vme_start
> start
) || (entry
->vme_end
< end
)) {
12963 * Map entry doesn't cover the requested range. Not handling
12964 * this situation currently.
12966 vm_map_unlock_read(map
);
12967 return(KERN_INVALID_ARGUMENT
);
12970 object
= entry
->object
.vm_object
;
12971 if (object
== VM_OBJECT_NULL
) {
12973 * Object must already be present or we can't sign.
12975 vm_map_unlock_read(map
);
12976 return KERN_INVALID_ARGUMENT
;
12979 vm_object_lock(object
);
12980 vm_map_unlock_read(map
);
12982 while(start
< end
) {
12985 m
= vm_page_lookup(object
, start
- entry
->vme_start
+ entry
->offset
);
12986 if (m
==VM_PAGE_NULL
) {
12987 /* shoud we try to fault a page here? we can probably
12988 * demand it exists and is locked for this request */
12989 vm_object_unlock(object
);
12990 return KERN_FAILURE
;
12992 /* deal with special page status */
12994 (m
->unusual
&& (m
->error
|| m
->restart
|| m
->private || m
->absent
))) {
12995 vm_object_unlock(object
);
12996 return KERN_FAILURE
;
12999 /* Page is OK... now "validate" it */
13000 /* This is the place where we'll call out to create a code
13001 * directory, later */
13002 m
->cs_validated
= TRUE
;
13004 /* The page is now "clean" for codesigning purposes. That means
13005 * we don't consider it as modified (wpmapped) anymore. But
13006 * we'll disconnect the page so we note any future modification
13008 m
->wpmapped
= FALSE
;
13009 refmod
= pmap_disconnect(m
->phys_page
);
13011 /* Pull the dirty status from the pmap, since we cleared the
13013 if ((refmod
& VM_MEM_MODIFIED
) && !m
->dirty
) {
13017 /* On to the next page */
13018 start
+= PAGE_SIZE
;
13020 vm_object_unlock(object
);
13022 return KERN_SUCCESS
;
13028 kern_return_t
vm_map_freeze_walk(
13030 unsigned int *purgeable_count
,
13031 unsigned int *wired_count
,
13032 unsigned int *clean_count
,
13033 unsigned int *dirty_count
,
13034 boolean_t
*has_shared
)
13036 vm_map_entry_t entry
;
13038 vm_map_lock_read(map
);
13040 *purgeable_count
= *wired_count
= *clean_count
= *dirty_count
= 0;
13041 *has_shared
= FALSE
;
13043 for (entry
= vm_map_first_entry(map
);
13044 entry
!= vm_map_to_entry(map
);
13045 entry
= entry
->vme_next
) {
13046 unsigned int purgeable
, clean
, dirty
, wired
;
13049 if ((entry
->object
.vm_object
== 0) ||
13050 (entry
->is_sub_map
) ||
13051 (entry
->object
.vm_object
->phys_contiguous
)) {
13055 vm_object_pack(&purgeable
, &wired
, &clean
, &dirty
, &shared
, entry
->object
.vm_object
, VM_OBJECT_NULL
, NULL
, NULL
);
13057 *purgeable_count
+= purgeable
;
13058 *wired_count
+= wired
;
13059 *clean_count
+= clean
;
13060 *dirty_count
+= dirty
;
13063 *has_shared
= TRUE
;
13067 vm_map_unlock_read(map
);
13069 return KERN_SUCCESS
;
13072 kern_return_t
vm_map_freeze(
13074 unsigned int *purgeable_count
,
13075 unsigned int *wired_count
,
13076 unsigned int *clean_count
,
13077 unsigned int *dirty_count
,
13078 boolean_t
*has_shared
)
13080 vm_map_entry_t entry2
= VM_MAP_ENTRY_NULL
;
13081 vm_object_t compact_object
= VM_OBJECT_NULL
;
13082 vm_object_offset_t offset
= 0x0;
13083 kern_return_t kr
= KERN_SUCCESS
;
13084 void *default_freezer_toc
= NULL
;
13085 boolean_t cleanup
= FALSE
;
13087 *purgeable_count
= *wired_count
= *clean_count
= *dirty_count
= 0;
13088 *has_shared
= FALSE
;
13090 /* Create our compact object */
13091 compact_object
= vm_object_allocate((vm_map_offset_t
)(VM_MAX_ADDRESS
) - (vm_map_offset_t
)(VM_MIN_ADDRESS
));
13092 if (!compact_object
) {
13097 default_freezer_toc
= default_freezer_mapping_create(compact_object
, offset
);
13098 if (!default_freezer_toc
) {
13104 * We need the exclusive lock here so that we can
13105 * block any page faults or lookups while we are
13106 * in the middle of freezing this vm map.
13110 if (map
->default_freezer_toc
!= NULL
){
13112 * This map has already been frozen.
13119 /* Get a mapping in place for the freezing about to commence */
13120 map
->default_freezer_toc
= default_freezer_toc
;
13122 vm_object_lock(compact_object
);
13124 for (entry2
= vm_map_first_entry(map
);
13125 entry2
!= vm_map_to_entry(map
);
13126 entry2
= entry2
->vme_next
) {
13128 vm_object_t src_object
= entry2
->object
.vm_object
;
13130 /* If eligible, scan the entry, moving eligible pages over to our parent object */
13131 if (entry2
->object
.vm_object
&& !entry2
->is_sub_map
&& !entry2
->object
.vm_object
->phys_contiguous
) {
13132 unsigned int purgeable
, clean
, dirty
, wired
;
13135 vm_object_pack(&purgeable
, &wired
, &clean
, &dirty
, &shared
,
13136 src_object
, compact_object
, &default_freezer_toc
, &offset
);
13138 *purgeable_count
+= purgeable
;
13139 *wired_count
+= wired
;
13140 *clean_count
+= clean
;
13141 *dirty_count
+= dirty
;
13144 *has_shared
= TRUE
;
13149 vm_object_unlock(compact_object
);
13151 /* Finally, throw out the pages to swap */
13152 vm_object_pageout(compact_object
);
13155 vm_map_unlock(map
);
13157 /* Unwind if there was a failure */
13158 if ((cleanup
) || (KERN_SUCCESS
!= kr
)) {
13159 if (default_freezer_toc
){
13160 default_freezer_mapping_free(&map
->default_freezer_toc
, TRUE
);
13162 if (compact_object
){
13163 vm_object_deallocate(compact_object
);
13170 __private_extern__ vm_object_t
default_freezer_get_compact_vm_object( void** );
13176 void **default_freezer_toc
;
13177 vm_object_t compact_object
;
13181 if (map
->default_freezer_toc
== NULL
){
13183 * This map is not in a frozen state.
13188 default_freezer_toc
= &(map
->default_freezer_toc
);
13190 compact_object
= default_freezer_get_compact_vm_object(default_freezer_toc
);
13192 /* Bring the pages back in */
13193 vm_object_pagein(compact_object
);
13195 /* Shift pages back to their original objects */
13196 vm_object_unpack(compact_object
, default_freezer_toc
);
13198 vm_object_deallocate(compact_object
);
13200 map
->default_freezer_toc
= NULL
;
13203 vm_map_unlock(map
);