2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
60 * Author: Avadis Tevanian, Jr., Michael Wayne Young
63 * Virtual memory mapping module.
66 #include <task_swapper.h>
67 #include <mach_assert.h>
68 #include <libkern/OSAtomic.h>
70 #include <mach/kern_return.h>
71 #include <mach/port.h>
72 #include <mach/vm_attributes.h>
73 #include <mach/vm_param.h>
74 #include <mach/vm_behavior.h>
75 #include <mach/vm_statistics.h>
76 #include <mach/memory_object.h>
77 #include <mach/mach_vm.h>
78 #include <machine/cpu_capabilities.h>
81 #include <kern/assert.h>
82 #include <kern/counters.h>
83 #include <kern/kalloc.h>
84 #include <kern/zalloc.h>
87 #include <vm/vm_init.h>
88 #include <vm/vm_fault.h>
89 #include <vm/vm_map.h>
90 #include <vm/vm_object.h>
91 #include <vm/vm_page.h>
92 #include <vm/vm_pageout.h>
93 #include <vm/vm_kern.h>
94 #include <ipc/ipc_port.h>
95 #include <kern/sched_prim.h>
96 #include <kern/misc_protos.h>
97 #include <machine/db_machdep.h>
100 #include <mach/vm_map_server.h>
101 #include <mach/mach_host_server.h>
102 #include <vm/vm_protos.h>
103 #include <vm/vm_purgeable_internal.h>
106 #include <ppc/mappings.h>
109 #include <vm/vm_protos.h>
110 #include <vm/vm_shared_region.h>
112 /* Internal prototypes
115 static void vm_map_simplify_range(
117 vm_map_offset_t start
,
118 vm_map_offset_t end
); /* forward */
120 static boolean_t
vm_map_range_check(
122 vm_map_offset_t start
,
124 vm_map_entry_t
*entry
);
126 static vm_map_entry_t
_vm_map_entry_create(
127 struct vm_map_header
*map_header
);
129 static void _vm_map_entry_dispose(
130 struct vm_map_header
*map_header
,
131 vm_map_entry_t entry
);
133 static void vm_map_pmap_enter(
135 vm_map_offset_t addr
,
136 vm_map_offset_t end_addr
,
138 vm_object_offset_t offset
,
139 vm_prot_t protection
);
141 static void _vm_map_clip_end(
142 struct vm_map_header
*map_header
,
143 vm_map_entry_t entry
,
144 vm_map_offset_t end
);
146 static void _vm_map_clip_start(
147 struct vm_map_header
*map_header
,
148 vm_map_entry_t entry
,
149 vm_map_offset_t start
);
151 static void vm_map_entry_delete(
153 vm_map_entry_t entry
);
155 static kern_return_t
vm_map_delete(
157 vm_map_offset_t start
,
162 static kern_return_t
vm_map_copy_overwrite_unaligned(
164 vm_map_entry_t entry
,
166 vm_map_address_t start
);
168 static kern_return_t
vm_map_copy_overwrite_aligned(
170 vm_map_entry_t tmp_entry
,
172 vm_map_offset_t start
,
175 static kern_return_t
vm_map_copyin_kernel_buffer(
177 vm_map_address_t src_addr
,
179 boolean_t src_destroy
,
180 vm_map_copy_t
*copy_result
); /* OUT */
182 static kern_return_t
vm_map_copyout_kernel_buffer(
184 vm_map_address_t
*addr
, /* IN/OUT */
186 boolean_t overwrite
);
188 static void vm_map_fork_share(
190 vm_map_entry_t old_entry
,
193 static boolean_t
vm_map_fork_copy(
195 vm_map_entry_t
*old_entry_p
,
198 void vm_map_region_top_walk(
199 vm_map_entry_t entry
,
200 vm_region_top_info_t top
);
202 void vm_map_region_walk(
205 vm_map_entry_t entry
,
206 vm_object_offset_t offset
,
207 vm_object_size_t range
,
208 vm_region_extended_info_t extended
,
209 boolean_t look_for_pages
);
211 static kern_return_t
vm_map_wire_nested(
213 vm_map_offset_t start
,
215 vm_prot_t access_type
,
218 vm_map_offset_t pmap_addr
);
220 static kern_return_t
vm_map_unwire_nested(
222 vm_map_offset_t start
,
226 vm_map_offset_t pmap_addr
);
228 static kern_return_t
vm_map_overwrite_submap_recurse(
230 vm_map_offset_t dst_addr
,
231 vm_map_size_t dst_size
);
233 static kern_return_t
vm_map_copy_overwrite_nested(
235 vm_map_offset_t dst_addr
,
237 boolean_t interruptible
,
240 static kern_return_t
vm_map_remap_extract(
242 vm_map_offset_t addr
,
245 struct vm_map_header
*map_header
,
246 vm_prot_t
*cur_protection
,
247 vm_prot_t
*max_protection
,
248 vm_inherit_t inheritance
,
251 static kern_return_t
vm_map_remap_range_allocate(
253 vm_map_address_t
*address
,
255 vm_map_offset_t mask
,
257 vm_map_entry_t
*map_entry
);
259 static void vm_map_region_look_for_page(
263 vm_object_offset_t offset
,
266 vm_region_extended_info_t extended
);
268 static int vm_map_region_count_obj_refs(
269 vm_map_entry_t entry
,
273 static kern_return_t
vm_map_willneed(
275 vm_map_offset_t start
,
276 vm_map_offset_t end
);
278 static kern_return_t
vm_map_reuse_pages(
280 vm_map_offset_t start
,
281 vm_map_offset_t end
);
283 static kern_return_t
vm_map_reusable_pages(
285 vm_map_offset_t start
,
286 vm_map_offset_t end
);
288 static kern_return_t
vm_map_can_reuse(
290 vm_map_offset_t start
,
291 vm_map_offset_t end
);
294 * Macros to copy a vm_map_entry. We must be careful to correctly
295 * manage the wired page count. vm_map_entry_copy() creates a new
296 * map entry to the same memory - the wired count in the new entry
297 * must be set to zero. vm_map_entry_copy_full() creates a new
298 * entry that is identical to the old entry. This preserves the
299 * wire count; it's used for map splitting and zone changing in
302 #define vm_map_entry_copy(NEW,OLD) \
305 (NEW)->is_shared = FALSE; \
306 (NEW)->needs_wakeup = FALSE; \
307 (NEW)->in_transition = FALSE; \
308 (NEW)->wired_count = 0; \
309 (NEW)->user_wired_count = 0; \
310 (NEW)->permanent = FALSE; \
313 #define vm_map_entry_copy_full(NEW,OLD) (*(NEW) = *(OLD))
316 * Decide if we want to allow processes to execute from their data or stack areas.
317 * override_nx() returns true if we do. Data/stack execution can be enabled independently
318 * for 32 and 64 bit processes. Set the VM_ABI_32 or VM_ABI_64 flags in allow_data_exec
319 * or allow_stack_exec to enable data execution for that type of data area for that particular
320 * ABI (or both by or'ing the flags together). These are initialized in the architecture
321 * specific pmap files since the default behavior varies according to architecture. The
322 * main reason it varies is because of the need to provide binary compatibility with old
323 * applications that were written before these restrictions came into being. In the old
324 * days, an app could execute anything it could read, but this has slowly been tightened
325 * up over time. The default behavior is:
327 * 32-bit PPC apps may execute from both stack and data areas
328 * 32-bit Intel apps may exeucte from data areas but not stack
329 * 64-bit PPC/Intel apps may not execute from either data or stack
331 * An application on any architecture may override these defaults by explicitly
332 * adding PROT_EXEC permission to the page in question with the mprotect(2)
333 * system call. This code here just determines what happens when an app tries to
334 * execute from a page that lacks execute permission.
336 * Note that allow_data_exec or allow_stack_exec may also be modified by sysctl to change the
337 * default behavior for both 32 and 64 bit apps on a system-wide basis.
340 extern int allow_data_exec
, allow_stack_exec
;
343 override_nx(vm_map_t map
, uint32_t user_tag
) /* map unused on arm */
348 * Determine if the app is running in 32 or 64 bit mode.
351 if (vm_map_is_64bit(map
))
352 current_abi
= VM_ABI_64
;
354 current_abi
= VM_ABI_32
;
357 * Determine if we should allow the execution based on whether it's a
358 * stack or data area and the current architecture.
361 if (user_tag
== VM_MEMORY_STACK
)
362 return allow_stack_exec
& current_abi
;
364 return allow_data_exec
& current_abi
;
369 * Virtual memory maps provide for the mapping, protection,
370 * and sharing of virtual memory objects. In addition,
371 * this module provides for an efficient virtual copy of
372 * memory from one map to another.
374 * Synchronization is required prior to most operations.
376 * Maps consist of an ordered doubly-linked list of simple
377 * entries; a single hint is used to speed up lookups.
379 * Sharing maps have been deleted from this version of Mach.
380 * All shared objects are now mapped directly into the respective
381 * maps. This requires a change in the copy on write strategy;
382 * the asymmetric (delayed) strategy is used for shared temporary
383 * objects instead of the symmetric (shadow) strategy. All maps
384 * are now "top level" maps (either task map, kernel map or submap
385 * of the kernel map).
387 * Since portions of maps are specified by start/end addreses,
388 * which may not align with existing map entries, all
389 * routines merely "clip" entries to these start/end values.
390 * [That is, an entry is split into two, bordering at a
391 * start or end value.] Note that these clippings may not
392 * always be necessary (as the two resulting entries are then
393 * not changed); however, the clipping is done for convenience.
394 * No attempt is currently made to "glue back together" two
397 * The symmetric (shadow) copy strategy implements virtual copy
398 * by copying VM object references from one map to
399 * another, and then marking both regions as copy-on-write.
400 * It is important to note that only one writeable reference
401 * to a VM object region exists in any map when this strategy
402 * is used -- this means that shadow object creation can be
403 * delayed until a write operation occurs. The symmetric (delayed)
404 * strategy allows multiple maps to have writeable references to
405 * the same region of a vm object, and hence cannot delay creating
406 * its copy objects. See vm_object_copy_quickly() in vm_object.c.
407 * Copying of permanent objects is completely different; see
408 * vm_object_copy_strategically() in vm_object.c.
411 static zone_t vm_map_zone
; /* zone for vm_map structures */
412 static zone_t vm_map_entry_zone
; /* zone for vm_map_entry structures */
413 static zone_t vm_map_kentry_zone
; /* zone for kernel entry structures */
414 static zone_t vm_map_copy_zone
; /* zone for vm_map_copy structures */
418 * Placeholder object for submap operations. This object is dropped
419 * into the range by a call to vm_map_find, and removed when
420 * vm_map_submap creates the submap.
423 vm_object_t vm_submap_object
;
425 static void *map_data
;
426 static vm_size_t map_data_size
;
427 static void *kentry_data
;
428 static vm_size_t kentry_data_size
;
429 static int kentry_count
= 2048; /* to init kentry_data_size */
431 #define NO_COALESCE_LIMIT ((1024 * 128) - 1)
434 /* Skip acquiring locks if we're in the midst of a kernel core dump */
435 unsigned int not_in_kdp
= 1;
437 #if CONFIG_CODE_DECRYPTION
439 * vm_map_apple_protected:
440 * This remaps the requested part of the object with an object backed by
441 * the decrypting pager.
442 * crypt_info contains entry points and session data for the crypt module.
443 * The crypt_info block will be copied by vm_map_apple_protected. The data structures
444 * referenced in crypt_info must remain valid until crypt_info->crypt_end() is called.
447 vm_map_apple_protected(
449 vm_map_offset_t start
,
451 struct pager_crypt_info
*crypt_info
)
453 boolean_t map_locked
;
455 vm_map_entry_t map_entry
;
456 memory_object_t protected_mem_obj
;
457 vm_object_t protected_object
;
458 vm_map_offset_t map_addr
;
460 vm_map_lock_read(map
);
463 /* lookup the protected VM object */
464 if (!vm_map_lookup_entry(map
,
467 map_entry
->vme_end
< end
||
468 map_entry
->is_sub_map
) {
469 /* that memory is not properly mapped */
470 kr
= KERN_INVALID_ARGUMENT
;
473 protected_object
= map_entry
->object
.vm_object
;
474 if (protected_object
== VM_OBJECT_NULL
) {
475 /* there should be a VM object here at this point */
476 kr
= KERN_INVALID_ARGUMENT
;
480 /* make sure protected object stays alive while map is unlocked */
481 vm_object_reference(protected_object
);
483 vm_map_unlock_read(map
);
487 * Lookup (and create if necessary) the protected memory object
488 * matching that VM object.
489 * If successful, this also grabs a reference on the memory object,
490 * to guarantee that it doesn't go away before we get a chance to map
493 protected_mem_obj
= apple_protect_pager_setup(protected_object
, crypt_info
);
495 /* release extra ref on protected object */
496 vm_object_deallocate(protected_object
);
498 if (protected_mem_obj
== NULL
) {
503 /* map this memory object in place of the current one */
505 kr
= vm_map_enter_mem_object(map
,
508 (mach_vm_offset_t
) 0,
509 VM_FLAGS_FIXED
| VM_FLAGS_OVERWRITE
,
510 (ipc_port_t
) protected_mem_obj
,
512 (start
- map_entry
->vme_start
)),
514 map_entry
->protection
,
515 map_entry
->max_protection
,
516 map_entry
->inheritance
);
517 assert(map_addr
== start
);
519 * Release the reference obtained by apple_protect_pager_setup().
520 * The mapping (if it succeeded) is now holding a reference on the
523 memory_object_deallocate(protected_mem_obj
);
527 vm_map_unlock_read(map
);
531 #endif /* CONFIG_CODE_DECRYPTION */
534 lck_grp_t vm_map_lck_grp
;
535 lck_grp_attr_t vm_map_lck_grp_attr
;
536 lck_attr_t vm_map_lck_attr
;
542 * Initialize the vm_map module. Must be called before
543 * any other vm_map routines.
545 * Map and entry structures are allocated from zones -- we must
546 * initialize those zones.
548 * There are three zones of interest:
550 * vm_map_zone: used to allocate maps.
551 * vm_map_entry_zone: used to allocate map entries.
552 * vm_map_kentry_zone: used to allocate map entries for the kernel.
554 * The kernel allocates map entries from a special zone that is initially
555 * "crammed" with memory. It would be difficult (perhaps impossible) for
556 * the kernel to allocate more memory to a entry zone when it became
557 * empty since the very act of allocating memory implies the creation
564 vm_map_zone
= zinit((vm_map_size_t
) sizeof(struct _vm_map
), 40*1024,
567 vm_map_entry_zone
= zinit((vm_map_size_t
) sizeof(struct vm_map_entry
),
568 1024*1024, PAGE_SIZE
*5,
569 "non-kernel map entries");
571 vm_map_kentry_zone
= zinit((vm_map_size_t
) sizeof(struct vm_map_entry
),
572 kentry_data_size
, kentry_data_size
,
573 "kernel map entries");
575 vm_map_copy_zone
= zinit((vm_map_size_t
) sizeof(struct vm_map_copy
),
576 16*1024, PAGE_SIZE
, "map copies");
579 * Cram the map and kentry zones with initial data.
580 * Set kentry_zone non-collectible to aid zone_gc().
582 zone_change(vm_map_zone
, Z_COLLECT
, FALSE
);
583 zone_change(vm_map_kentry_zone
, Z_COLLECT
, FALSE
);
584 zone_change(vm_map_kentry_zone
, Z_EXPAND
, FALSE
);
585 zone_change(vm_map_kentry_zone
, Z_FOREIGN
, TRUE
);
586 zcram(vm_map_zone
, map_data
, map_data_size
);
587 zcram(vm_map_kentry_zone
, kentry_data
, kentry_data_size
);
589 lck_grp_attr_setdefault(&vm_map_lck_grp_attr
);
590 lck_grp_init(&vm_map_lck_grp
, "vm_map", &vm_map_lck_grp_attr
);
591 lck_attr_setdefault(&vm_map_lck_attr
);
598 map_data_size
= round_page(10 * sizeof(struct _vm_map
));
599 map_data
= pmap_steal_memory(map_data_size
);
603 * Limiting worst case: vm_map_kentry_zone needs to map each "available"
604 * physical page (i.e. that beyond the kernel image and page tables)
605 * individually; we guess at most one entry per eight pages in the
606 * real world. This works out to roughly .1 of 1% of physical memory,
607 * or roughly 1900 entries (64K) for a 64M machine with 4K pages.
610 kentry_count
= pmap_free_pages() / 8;
614 round_page(kentry_count
* sizeof(struct vm_map_entry
));
615 kentry_data
= pmap_steal_memory(kentry_data_size
);
621 * Creates and returns a new empty VM map with
622 * the given physical map structure, and having
623 * the given lower and upper address bounds.
632 static int color_seed
= 0;
633 register vm_map_t result
;
635 result
= (vm_map_t
) zalloc(vm_map_zone
);
636 if (result
== VM_MAP_NULL
)
637 panic("vm_map_create");
639 vm_map_first_entry(result
) = vm_map_to_entry(result
);
640 vm_map_last_entry(result
) = vm_map_to_entry(result
);
641 result
->hdr
.nentries
= 0;
642 result
->hdr
.entries_pageable
= pageable
;
645 result
->user_wire_limit
= MACH_VM_MAX_ADDRESS
; /* default limit is unlimited */
646 result
->user_wire_size
= 0;
647 result
->ref_count
= 1;
649 result
->res_count
= 1;
650 result
->sw_state
= MAP_SW_IN
;
651 #endif /* TASK_SWAPPER */
653 result
->min_offset
= min
;
654 result
->max_offset
= max
;
655 result
->wiring_required
= FALSE
;
656 result
->no_zero_fill
= FALSE
;
657 result
->mapped
= FALSE
;
658 result
->wait_for_space
= FALSE
;
659 result
->switch_protect
= FALSE
;
660 result
->first_free
= vm_map_to_entry(result
);
661 result
->hint
= vm_map_to_entry(result
);
662 result
->color_rr
= (color_seed
++) & vm_color_mask
;
663 vm_map_lock_init(result
);
664 lck_mtx_init_ext(&result
->s_lock
, &result
->s_lock_ext
, &vm_map_lck_grp
, &vm_map_lck_attr
);
670 * vm_map_entry_create: [ internal use only ]
672 * Allocates a VM map entry for insertion in the
673 * given map (or map copy). No fields are filled.
675 #define vm_map_entry_create(map) \
676 _vm_map_entry_create(&(map)->hdr)
678 #define vm_map_copy_entry_create(copy) \
679 _vm_map_entry_create(&(copy)->cpy_hdr)
681 static vm_map_entry_t
682 _vm_map_entry_create(
683 register struct vm_map_header
*map_header
)
685 register zone_t zone
;
686 register vm_map_entry_t entry
;
688 if (map_header
->entries_pageable
)
689 zone
= vm_map_entry_zone
;
691 zone
= vm_map_kentry_zone
;
693 entry
= (vm_map_entry_t
) zalloc(zone
);
694 if (entry
== VM_MAP_ENTRY_NULL
)
695 panic("vm_map_entry_create");
701 * vm_map_entry_dispose: [ internal use only ]
703 * Inverse of vm_map_entry_create.
705 * write map lock held so no need to
706 * do anything special to insure correctness
709 #define vm_map_entry_dispose(map, entry) \
711 if((entry) == (map)->first_free) \
712 (map)->first_free = vm_map_to_entry(map); \
713 if((entry) == (map)->hint) \
714 (map)->hint = vm_map_to_entry(map); \
715 _vm_map_entry_dispose(&(map)->hdr, (entry)); \
718 #define vm_map_copy_entry_dispose(map, entry) \
719 _vm_map_entry_dispose(&(copy)->cpy_hdr, (entry))
722 _vm_map_entry_dispose(
723 register struct vm_map_header
*map_header
,
724 register vm_map_entry_t entry
)
726 register zone_t zone
;
728 if (map_header
->entries_pageable
)
729 zone
= vm_map_entry_zone
;
731 zone
= vm_map_kentry_zone
;
737 static boolean_t
first_free_is_valid(vm_map_t map
); /* forward */
738 static boolean_t first_free_check
= FALSE
;
743 vm_map_entry_t entry
, next
;
745 if (!first_free_check
)
748 entry
= vm_map_to_entry(map
);
749 next
= entry
->vme_next
;
750 while (vm_map_trunc_page(next
->vme_start
) == vm_map_trunc_page(entry
->vme_end
) ||
751 (vm_map_trunc_page(next
->vme_start
) == vm_map_trunc_page(entry
->vme_start
) &&
752 next
!= vm_map_to_entry(map
))) {
754 next
= entry
->vme_next
;
755 if (entry
== vm_map_to_entry(map
))
758 if (map
->first_free
!= entry
) {
759 printf("Bad first_free for map %p: %p should be %p\n",
760 map
, map
->first_free
, entry
);
765 #endif /* MACH_ASSERT */
770 * Updates the map->first_free pointer to the
771 * entry immediately before the first hole in the map.
772 * The map should be locked.
774 #define UPDATE_FIRST_FREE(map, new_first_free) \
777 vm_map_entry_t UFF_first_free; \
778 vm_map_entry_t UFF_next_entry; \
780 UFF_first_free = (new_first_free); \
781 UFF_next_entry = UFF_first_free->vme_next; \
782 while (vm_map_trunc_page(UFF_next_entry->vme_start) == \
783 vm_map_trunc_page(UFF_first_free->vme_end) || \
784 (vm_map_trunc_page(UFF_next_entry->vme_start) == \
785 vm_map_trunc_page(UFF_first_free->vme_start) && \
786 UFF_next_entry != vm_map_to_entry(UFF_map))) { \
787 UFF_first_free = UFF_next_entry; \
788 UFF_next_entry = UFF_first_free->vme_next; \
789 if (UFF_first_free == vm_map_to_entry(UFF_map)) \
792 UFF_map->first_free = UFF_first_free; \
793 assert(first_free_is_valid(UFF_map)); \
797 * vm_map_entry_{un,}link:
799 * Insert/remove entries from maps (or map copies).
801 #define vm_map_entry_link(map, after_where, entry) \
804 vm_map_entry_t VMEL_entry; \
806 VMEL_entry = (entry); \
807 _vm_map_entry_link(&VMEL_map->hdr, after_where, VMEL_entry); \
808 UPDATE_FIRST_FREE(VMEL_map, VMEL_map->first_free); \
812 #define vm_map_copy_entry_link(copy, after_where, entry) \
813 _vm_map_entry_link(&(copy)->cpy_hdr, after_where, (entry))
815 #define _vm_map_entry_link(hdr, after_where, entry) \
818 (entry)->vme_prev = (after_where); \
819 (entry)->vme_next = (after_where)->vme_next; \
820 (entry)->vme_prev->vme_next = (entry)->vme_next->vme_prev = (entry); \
823 #define vm_map_entry_unlink(map, entry) \
826 vm_map_entry_t VMEU_entry; \
827 vm_map_entry_t VMEU_first_free; \
829 VMEU_entry = (entry); \
830 if (VMEU_entry->vme_start <= VMEU_map->first_free->vme_start) \
831 VMEU_first_free = VMEU_entry->vme_prev; \
833 VMEU_first_free = VMEU_map->first_free; \
834 _vm_map_entry_unlink(&VMEU_map->hdr, VMEU_entry); \
835 UPDATE_FIRST_FREE(VMEU_map, VMEU_first_free); \
838 #define vm_map_copy_entry_unlink(copy, entry) \
839 _vm_map_entry_unlink(&(copy)->cpy_hdr, (entry))
841 #define _vm_map_entry_unlink(hdr, entry) \
844 (entry)->vme_next->vme_prev = (entry)->vme_prev; \
845 (entry)->vme_prev->vme_next = (entry)->vme_next; \
848 #if MACH_ASSERT && TASK_SWAPPER
850 * vm_map_res_reference:
852 * Adds another valid residence count to the given map.
854 * Map is locked so this function can be called from
858 void vm_map_res_reference(register vm_map_t map
)
860 /* assert map is locked */
861 assert(map
->res_count
>= 0);
862 assert(map
->ref_count
>= map
->res_count
);
863 if (map
->res_count
== 0) {
864 lck_mtx_unlock(&map
->s_lock
);
867 lck_mtx_lock(&map
->s_lock
);
875 * vm_map_reference_swap:
877 * Adds valid reference and residence counts to the given map.
879 * The map may not be in memory (i.e. zero residence count).
882 void vm_map_reference_swap(register vm_map_t map
)
884 assert(map
!= VM_MAP_NULL
);
885 lck_mtx_lock(&map
->s_lock
);
886 assert(map
->res_count
>= 0);
887 assert(map
->ref_count
>= map
->res_count
);
889 vm_map_res_reference(map
);
890 lck_mtx_unlock(&map
->s_lock
);
894 * vm_map_res_deallocate:
896 * Decrement residence count on a map; possibly causing swapout.
898 * The map must be in memory (i.e. non-zero residence count).
900 * The map is locked, so this function is callable from vm_map_deallocate.
903 void vm_map_res_deallocate(register vm_map_t map
)
905 assert(map
->res_count
> 0);
906 if (--map
->res_count
== 0) {
907 lck_mtx_unlock(&map
->s_lock
);
911 lck_mtx_lock(&map
->s_lock
);
913 assert(map
->ref_count
>= map
->res_count
);
915 #endif /* MACH_ASSERT && TASK_SWAPPER */
920 * Actually destroy a map.
929 /* clean up regular map entries */
930 (void) vm_map_delete(map
, map
->min_offset
, map
->max_offset
,
932 /* clean up leftover special mappings (commpage, etc...) */
935 * PPC51: ppc64 is limited to 51-bit addresses.
936 * Memory beyond this 51-bit limit is mapped specially at the
937 * pmap level, so do not interfere.
938 * On PPC64, the commpage is mapped beyond the addressable range
939 * via a special pmap hack, so ask pmap to clean it explicitly...
942 pmap_unmap_sharedpage(map
->pmap
);
944 /* ... and do not let regular pmap cleanup apply here */
945 flags
|= VM_MAP_REMOVE_NO_PMAP_CLEANUP
;
947 (void) vm_map_delete(map
, 0x0, 0xFFFFFFFFFFFFF000ULL
,
951 assert(map
->hdr
.nentries
== 0);
954 pmap_destroy(map
->pmap
);
956 zfree(vm_map_zone
, map
);
961 * vm_map_swapin/vm_map_swapout
963 * Swap a map in and out, either referencing or releasing its resources.
964 * These functions are internal use only; however, they must be exported
965 * because they may be called from macros, which are exported.
967 * In the case of swapout, there could be races on the residence count,
968 * so if the residence count is up, we return, assuming that a
969 * vm_map_deallocate() call in the near future will bring us back.
972 * -- We use the map write lock for synchronization among races.
973 * -- The map write lock, and not the simple s_lock, protects the
974 * swap state of the map.
975 * -- If a map entry is a share map, then we hold both locks, in
976 * hierarchical order.
978 * Synchronization Notes:
979 * 1) If a vm_map_swapin() call happens while swapout in progress, it
980 * will block on the map lock and proceed when swapout is through.
981 * 2) A vm_map_reference() call at this time is illegal, and will
982 * cause a panic. vm_map_reference() is only allowed on resident
983 * maps, since it refuses to block.
984 * 3) A vm_map_swapin() call during a swapin will block, and
985 * proceeed when the first swapin is done, turning into a nop.
986 * This is the reason the res_count is not incremented until
987 * after the swapin is complete.
988 * 4) There is a timing hole after the checks of the res_count, before
989 * the map lock is taken, during which a swapin may get the lock
990 * before a swapout about to happen. If this happens, the swapin
991 * will detect the state and increment the reference count, causing
992 * the swapout to be a nop, thereby delaying it until a later
993 * vm_map_deallocate. If the swapout gets the lock first, then
994 * the swapin will simply block until the swapout is done, and
997 * Because vm_map_swapin() is potentially an expensive operation, it
998 * should be used with caution.
1001 * 1) A map with a residence count of zero is either swapped, or
1003 * 2) A map with a non-zero residence count is either resident,
1004 * or being swapped in.
1007 int vm_map_swap_enable
= 1;
1009 void vm_map_swapin (vm_map_t map
)
1011 register vm_map_entry_t entry
;
1013 if (!vm_map_swap_enable
) /* debug */
1018 * First deal with various races.
1020 if (map
->sw_state
== MAP_SW_IN
)
1022 * we raced with swapout and won. Returning will incr.
1023 * the res_count, turning the swapout into a nop.
1028 * The residence count must be zero. If we raced with another
1029 * swapin, the state would have been IN; if we raced with a
1030 * swapout (after another competing swapin), we must have lost
1031 * the race to get here (see above comment), in which case
1032 * res_count is still 0.
1034 assert(map
->res_count
== 0);
1037 * There are no intermediate states of a map going out or
1038 * coming in, since the map is locked during the transition.
1040 assert(map
->sw_state
== MAP_SW_OUT
);
1043 * We now operate upon each map entry. If the entry is a sub-
1044 * or share-map, we call vm_map_res_reference upon it.
1045 * If the entry is an object, we call vm_object_res_reference
1046 * (this may iterate through the shadow chain).
1047 * Note that we hold the map locked the entire time,
1048 * even if we get back here via a recursive call in
1049 * vm_map_res_reference.
1051 entry
= vm_map_first_entry(map
);
1053 while (entry
!= vm_map_to_entry(map
)) {
1054 if (entry
->object
.vm_object
!= VM_OBJECT_NULL
) {
1055 if (entry
->is_sub_map
) {
1056 vm_map_t lmap
= entry
->object
.sub_map
;
1057 lck_mtx_lock(&lmap
->s_lock
);
1058 vm_map_res_reference(lmap
);
1059 lck_mtx_unlock(&lmap
->s_lock
);
1061 vm_object_t object
= entry
->object
.vm_object
;
1062 vm_object_lock(object
);
1064 * This call may iterate through the
1067 vm_object_res_reference(object
);
1068 vm_object_unlock(object
);
1071 entry
= entry
->vme_next
;
1073 assert(map
->sw_state
== MAP_SW_OUT
);
1074 map
->sw_state
= MAP_SW_IN
;
1077 void vm_map_swapout(vm_map_t map
)
1079 register vm_map_entry_t entry
;
1083 * First deal with various races.
1084 * If we raced with a swapin and lost, the residence count
1085 * will have been incremented to 1, and we simply return.
1087 lck_mtx_lock(&map
->s_lock
);
1088 if (map
->res_count
!= 0) {
1089 lck_mtx_unlock(&map
->s_lock
);
1092 lck_mtx_unlock(&map
->s_lock
);
1095 * There are no intermediate states of a map going out or
1096 * coming in, since the map is locked during the transition.
1098 assert(map
->sw_state
== MAP_SW_IN
);
1100 if (!vm_map_swap_enable
)
1104 * We now operate upon each map entry. If the entry is a sub-
1105 * or share-map, we call vm_map_res_deallocate upon it.
1106 * If the entry is an object, we call vm_object_res_deallocate
1107 * (this may iterate through the shadow chain).
1108 * Note that we hold the map locked the entire time,
1109 * even if we get back here via a recursive call in
1110 * vm_map_res_deallocate.
1112 entry
= vm_map_first_entry(map
);
1114 while (entry
!= vm_map_to_entry(map
)) {
1115 if (entry
->object
.vm_object
!= VM_OBJECT_NULL
) {
1116 if (entry
->is_sub_map
) {
1117 vm_map_t lmap
= entry
->object
.sub_map
;
1118 lck_mtx_lock(&lmap
->s_lock
);
1119 vm_map_res_deallocate(lmap
);
1120 lck_mtx_unlock(&lmap
->s_lock
);
1122 vm_object_t object
= entry
->object
.vm_object
;
1123 vm_object_lock(object
);
1125 * This call may take a long time,
1126 * since it could actively push
1127 * out pages (if we implement it
1130 vm_object_res_deallocate(object
);
1131 vm_object_unlock(object
);
1134 entry
= entry
->vme_next
;
1136 assert(map
->sw_state
== MAP_SW_IN
);
1137 map
->sw_state
= MAP_SW_OUT
;
1140 #endif /* TASK_SWAPPER */
1144 * SAVE_HINT_MAP_READ:
1146 * Saves the specified entry as the hint for
1147 * future lookups. only a read lock is held on map,
1148 * so make sure the store is atomic... OSCompareAndSwap
1149 * guarantees this... also, we don't care if we collide
1150 * and someone else wins and stores their 'hint'
1152 #define SAVE_HINT_MAP_READ(map,value) \
1154 OSCompareAndSwapPtr((map)->hint, value, &(map)->hint); \
1159 * SAVE_HINT_MAP_WRITE:
1161 * Saves the specified entry as the hint for
1162 * future lookups. write lock held on map,
1163 * so no one else can be writing or looking
1164 * until the lock is dropped, so it's safe
1165 * to just do an assignment
1167 #define SAVE_HINT_MAP_WRITE(map,value) \
1169 (map)->hint = (value); \
1173 * vm_map_lookup_entry: [ internal use only ]
1175 * Finds the map entry containing (or
1176 * immediately preceding) the specified address
1177 * in the given map; the entry is returned
1178 * in the "entry" parameter. The boolean
1179 * result indicates whether the address is
1180 * actually contained in the map.
1183 vm_map_lookup_entry(
1184 register vm_map_t map
,
1185 register vm_map_offset_t address
,
1186 vm_map_entry_t
*entry
) /* OUT */
1188 register vm_map_entry_t cur
;
1189 register vm_map_entry_t last
;
1192 * Start looking either from the head of the
1193 * list, or from the hint.
1197 if (cur
== vm_map_to_entry(map
))
1198 cur
= cur
->vme_next
;
1200 if (address
>= cur
->vme_start
) {
1202 * Go from hint to end of list.
1204 * But first, make a quick check to see if
1205 * we are already looking at the entry we
1206 * want (which is usually the case).
1207 * Note also that we don't need to save the hint
1208 * here... it is the same hint (unless we are
1209 * at the header, in which case the hint didn't
1210 * buy us anything anyway).
1212 last
= vm_map_to_entry(map
);
1213 if ((cur
!= last
) && (cur
->vme_end
> address
)) {
1220 * Go from start to hint, *inclusively*
1222 last
= cur
->vme_next
;
1223 cur
= vm_map_first_entry(map
);
1230 while (cur
!= last
) {
1231 if (cur
->vme_end
> address
) {
1232 if (address
>= cur
->vme_start
) {
1234 * Save this lookup for future
1239 SAVE_HINT_MAP_READ(map
, cur
);
1245 cur
= cur
->vme_next
;
1247 *entry
= cur
->vme_prev
;
1248 SAVE_HINT_MAP_READ(map
, *entry
);
1254 * Routine: vm_map_find_space
1256 * Allocate a range in the specified virtual address map,
1257 * returning the entry allocated for that range.
1258 * Used by kmem_alloc, etc.
1260 * The map must be NOT be locked. It will be returned locked
1261 * on KERN_SUCCESS, unlocked on failure.
1263 * If an entry is allocated, the object/offset fields
1264 * are initialized to zero.
1268 register vm_map_t map
,
1269 vm_map_offset_t
*address
, /* OUT */
1271 vm_map_offset_t mask
,
1273 vm_map_entry_t
*o_entry
) /* OUT */
1275 register vm_map_entry_t entry
, new_entry
;
1276 register vm_map_offset_t start
;
1277 register vm_map_offset_t end
;
1281 return KERN_INVALID_ARGUMENT
;
1284 if (flags
& VM_FLAGS_GUARD_AFTER
) {
1285 /* account for the back guard page in the size */
1286 size
+= PAGE_SIZE_64
;
1289 new_entry
= vm_map_entry_create(map
);
1292 * Look for the first possible address; if there's already
1293 * something at this address, we have to start after it.
1298 assert(first_free_is_valid(map
));
1299 if ((entry
= map
->first_free
) == vm_map_to_entry(map
))
1300 start
= map
->min_offset
;
1302 start
= entry
->vme_end
;
1305 * In any case, the "entry" always precedes
1306 * the proposed new region throughout the loop:
1310 register vm_map_entry_t next
;
1313 * Find the end of the proposed new region.
1314 * Be sure we didn't go beyond the end, or
1315 * wrap around the address.
1318 if (flags
& VM_FLAGS_GUARD_BEFORE
) {
1319 /* reserve space for the front guard page */
1320 start
+= PAGE_SIZE_64
;
1322 end
= ((start
+ mask
) & ~mask
);
1325 vm_map_entry_dispose(map
, new_entry
);
1327 return(KERN_NO_SPACE
);
1332 if ((end
> map
->max_offset
) || (end
< start
)) {
1333 vm_map_entry_dispose(map
, new_entry
);
1335 return(KERN_NO_SPACE
);
1339 * If there are no more entries, we must win.
1342 next
= entry
->vme_next
;
1343 if (next
== vm_map_to_entry(map
))
1347 * If there is another entry, it must be
1348 * after the end of the potential new region.
1351 if (next
->vme_start
>= end
)
1355 * Didn't fit -- move to the next entry.
1359 start
= entry
->vme_end
;
1364 * "start" and "end" should define the endpoints of the
1365 * available new range, and
1366 * "entry" should refer to the region before the new
1369 * the map should be locked.
1372 if (flags
& VM_FLAGS_GUARD_BEFORE
) {
1373 /* go back for the front guard page */
1374 start
-= PAGE_SIZE_64
;
1378 new_entry
->vme_start
= start
;
1379 new_entry
->vme_end
= end
;
1380 assert(page_aligned(new_entry
->vme_start
));
1381 assert(page_aligned(new_entry
->vme_end
));
1383 new_entry
->is_shared
= FALSE
;
1384 new_entry
->is_sub_map
= FALSE
;
1385 new_entry
->use_pmap
= FALSE
;
1386 new_entry
->object
.vm_object
= VM_OBJECT_NULL
;
1387 new_entry
->offset
= (vm_object_offset_t
) 0;
1389 new_entry
->needs_copy
= FALSE
;
1391 new_entry
->inheritance
= VM_INHERIT_DEFAULT
;
1392 new_entry
->protection
= VM_PROT_DEFAULT
;
1393 new_entry
->max_protection
= VM_PROT_ALL
;
1394 new_entry
->behavior
= VM_BEHAVIOR_DEFAULT
;
1395 new_entry
->wired_count
= 0;
1396 new_entry
->user_wired_count
= 0;
1398 new_entry
->in_transition
= FALSE
;
1399 new_entry
->needs_wakeup
= FALSE
;
1400 new_entry
->no_cache
= FALSE
;
1401 new_entry
->permanent
= FALSE
;
1402 new_entry
->superpage_size
= 0;
1404 new_entry
->alias
= 0;
1405 new_entry
->zero_wired_pages
= FALSE
;
1407 VM_GET_FLAGS_ALIAS(flags
, new_entry
->alias
);
1410 * Insert the new entry into the list
1413 vm_map_entry_link(map
, entry
, new_entry
);
1418 * Update the lookup hint
1420 SAVE_HINT_MAP_WRITE(map
, new_entry
);
1422 *o_entry
= new_entry
;
1423 return(KERN_SUCCESS
);
1426 int vm_map_pmap_enter_print
= FALSE
;
1427 int vm_map_pmap_enter_enable
= FALSE
;
1430 * Routine: vm_map_pmap_enter [internal only]
1433 * Force pages from the specified object to be entered into
1434 * the pmap at the specified address if they are present.
1435 * As soon as a page not found in the object the scan ends.
1440 * In/out conditions:
1441 * The source map should not be locked on entry.
1446 register vm_map_offset_t addr
,
1447 register vm_map_offset_t end_addr
,
1448 register vm_object_t object
,
1449 vm_object_offset_t offset
,
1450 vm_prot_t protection
)
1458 while (addr
< end_addr
) {
1459 register vm_page_t m
;
1461 vm_object_lock(object
);
1463 m
= vm_page_lookup(object
, offset
);
1466 * The user should never see encrypted data, so do not
1467 * enter an encrypted page in the page table.
1469 if (m
== VM_PAGE_NULL
|| m
->busy
|| m
->encrypted
||
1471 (m
->unusual
&& ( m
->error
|| m
->restart
|| m
->absent
))) {
1472 vm_object_unlock(object
);
1476 if (vm_map_pmap_enter_print
) {
1477 printf("vm_map_pmap_enter:");
1478 printf("map: %p, addr: %llx, object: %p, offset: %llx\n",
1479 map
, (unsigned long long)addr
, object
, (unsigned long long)offset
);
1481 type_of_fault
= DBG_CACHE_HIT_FAULT
;
1482 kr
= vm_fault_enter(m
, map
->pmap
, addr
, protection
,
1483 VM_PAGE_WIRED(m
), FALSE
, FALSE
,
1486 vm_object_unlock(object
);
1488 offset
+= PAGE_SIZE_64
;
1493 boolean_t
vm_map_pmap_is_empty(
1495 vm_map_offset_t start
,
1496 vm_map_offset_t end
);
1497 boolean_t
vm_map_pmap_is_empty(
1499 vm_map_offset_t start
,
1500 vm_map_offset_t end
)
1502 #ifdef MACHINE_PMAP_IS_EMPTY
1503 return pmap_is_empty(map
->pmap
, start
, end
);
1504 #else /* MACHINE_PMAP_IS_EMPTY */
1505 vm_map_offset_t offset
;
1508 if (map
->pmap
== NULL
) {
1512 for (offset
= start
;
1514 offset
+= PAGE_SIZE
) {
1515 phys_page
= pmap_find_phys(map
->pmap
, offset
);
1517 kprintf("vm_map_pmap_is_empty(%p,0x%llx,0x%llx): "
1518 "page %d at 0x%llx\n",
1519 map
, (long long)start
, (long long)end
,
1520 phys_page
, (long long)offset
);
1525 #endif /* MACHINE_PMAP_IS_EMPTY */
1529 * Routine: vm_map_enter
1532 * Allocate a range in the specified virtual address map.
1533 * The resulting range will refer to memory defined by
1534 * the given memory object and offset into that object.
1536 * Arguments are as defined in the vm_map call.
1538 int _map_enter_debug
= 0;
1539 static unsigned int vm_map_enter_restore_successes
= 0;
1540 static unsigned int vm_map_enter_restore_failures
= 0;
1544 vm_map_offset_t
*address
, /* IN/OUT */
1546 vm_map_offset_t mask
,
1549 vm_object_offset_t offset
,
1550 boolean_t needs_copy
,
1551 vm_prot_t cur_protection
,
1552 vm_prot_t max_protection
,
1553 vm_inherit_t inheritance
)
1555 vm_map_entry_t entry
, new_entry
;
1556 vm_map_offset_t start
, tmp_start
, tmp_offset
;
1557 vm_map_offset_t end
, tmp_end
;
1558 vm_map_offset_t tmp2_start
, tmp2_end
;
1559 vm_map_offset_t step
;
1560 kern_return_t result
= KERN_SUCCESS
;
1561 vm_map_t zap_old_map
= VM_MAP_NULL
;
1562 vm_map_t zap_new_map
= VM_MAP_NULL
;
1563 boolean_t map_locked
= FALSE
;
1564 boolean_t pmap_empty
= TRUE
;
1565 boolean_t new_mapping_established
= FALSE
;
1566 boolean_t anywhere
= ((flags
& VM_FLAGS_ANYWHERE
) != 0);
1567 boolean_t purgable
= ((flags
& VM_FLAGS_PURGABLE
) != 0);
1568 boolean_t overwrite
= ((flags
& VM_FLAGS_OVERWRITE
) != 0);
1569 boolean_t no_cache
= ((flags
& VM_FLAGS_NO_CACHE
) != 0);
1570 boolean_t is_submap
= ((flags
& VM_FLAGS_SUBMAP
) != 0);
1571 boolean_t permanent
= ((flags
& VM_FLAGS_PERMANENT
) != 0);
1572 unsigned int superpage_size
= ((flags
& VM_FLAGS_SUPERPAGE_MASK
) >> VM_FLAGS_SUPERPAGE_SHIFT
);
1574 vm_map_offset_t effective_min_offset
, effective_max_offset
;
1577 if (superpage_size
) {
1578 switch (superpage_size
) {
1580 * Note that the current implementation only supports
1581 * a single size for superpages, SUPERPAGE_SIZE, per
1582 * architecture. As soon as more sizes are supposed
1583 * to be supported, SUPERPAGE_SIZE has to be replaced
1584 * with a lookup of the size depending on superpage_size.
1587 case SUPERPAGE_SIZE_2MB
:
1591 return KERN_INVALID_ARGUMENT
;
1593 mask
= SUPERPAGE_SIZE
-1;
1594 if (size
& (SUPERPAGE_SIZE
-1))
1595 return KERN_INVALID_ARGUMENT
;
1596 inheritance
= VM_INHERIT_NONE
; /* fork() children won't inherit superpages */
1600 if (cur_protection
& VM_PROT_WRITE
) {
1601 if (cur_protection
& VM_PROT_EXECUTE
) {
1602 printf("EMBEDDED: %s curprot cannot be write+execute. turning off execute\n", __PRETTY_FUNCTION__
);
1603 cur_protection
&= ~VM_PROT_EXECUTE
;
1606 #endif /* CONFIG_EMBEDDED */
1610 /* submaps can not be purgeable */
1611 return KERN_INVALID_ARGUMENT
;
1613 if (object
== VM_OBJECT_NULL
) {
1614 /* submaps can not be created lazily */
1615 return KERN_INVALID_ARGUMENT
;
1618 if (flags
& VM_FLAGS_ALREADY
) {
1620 * VM_FLAGS_ALREADY says that it's OK if the same mapping
1621 * is already present. For it to be meaningul, the requested
1622 * mapping has to be at a fixed address (!VM_FLAGS_ANYWHERE) and
1623 * we shouldn't try and remove what was mapped there first
1624 * (!VM_FLAGS_OVERWRITE).
1626 if ((flags
& VM_FLAGS_ANYWHERE
) ||
1627 (flags
& VM_FLAGS_OVERWRITE
)) {
1628 return KERN_INVALID_ARGUMENT
;
1632 if (flags
& VM_FLAGS_BELOW_MIN
) {
1634 * Allow an insertion below the map's min offset.
1636 effective_min_offset
= 0ULL;
1638 effective_min_offset
= map
->min_offset
;
1641 if (flags
& VM_FLAGS_BEYOND_MAX
) {
1643 * Allow an insertion beyond the map's max offset.
1645 if (vm_map_is_64bit(map
))
1646 effective_max_offset
= 0xFFFFFFFFFFFFF000ULL
;
1648 effective_max_offset
= 0x00000000FFFFF000ULL
;
1650 effective_max_offset
= map
->max_offset
;
1654 (offset
& PAGE_MASK_64
) != 0) {
1656 return KERN_INVALID_ARGUMENT
;
1659 VM_GET_FLAGS_ALIAS(flags
, alias
);
1661 #define RETURN(value) { result = value; goto BailOut; }
1663 assert(page_aligned(*address
));
1664 assert(page_aligned(size
));
1667 * Only zero-fill objects are allowed to be purgable.
1668 * LP64todo - limit purgable objects to 32-bits for now
1672 (object
!= VM_OBJECT_NULL
&&
1673 (object
->size
!= size
||
1674 object
->purgable
== VM_PURGABLE_DENY
))
1675 || size
> ANON_MAX_SIZE
)) /* LP64todo: remove when dp capable */
1676 return KERN_INVALID_ARGUMENT
;
1678 if (!anywhere
&& overwrite
) {
1680 * Create a temporary VM map to hold the old mappings in the
1681 * affected area while we create the new one.
1682 * This avoids releasing the VM map lock in
1683 * vm_map_entry_delete() and allows atomicity
1684 * when we want to replace some mappings with a new one.
1685 * It also allows us to restore the old VM mappings if the
1686 * new mapping fails.
1688 zap_old_map
= vm_map_create(PMAP_NULL
,
1691 map
->hdr
.entries_pageable
);
1703 * Calculate the first possible address.
1706 if (start
< effective_min_offset
)
1707 start
= effective_min_offset
;
1708 if (start
> effective_max_offset
)
1709 RETURN(KERN_NO_SPACE
);
1712 * Look for the first possible address;
1713 * if there's already something at this
1714 * address, we have to start after it.
1717 assert(first_free_is_valid(map
));
1718 if (start
== effective_min_offset
) {
1719 if ((entry
= map
->first_free
) != vm_map_to_entry(map
))
1720 start
= entry
->vme_end
;
1722 vm_map_entry_t tmp_entry
;
1723 if (vm_map_lookup_entry(map
, start
, &tmp_entry
))
1724 start
= tmp_entry
->vme_end
;
1729 * In any case, the "entry" always precedes
1730 * the proposed new region throughout the
1735 register vm_map_entry_t next
;
1738 * Find the end of the proposed new region.
1739 * Be sure we didn't go beyond the end, or
1740 * wrap around the address.
1743 end
= ((start
+ mask
) & ~mask
);
1745 RETURN(KERN_NO_SPACE
);
1749 if ((end
> effective_max_offset
) || (end
< start
)) {
1750 if (map
->wait_for_space
) {
1751 if (size
<= (effective_max_offset
-
1752 effective_min_offset
)) {
1753 assert_wait((event_t
)map
,
1757 thread_block(THREAD_CONTINUE_NULL
);
1761 RETURN(KERN_NO_SPACE
);
1765 * If there are no more entries, we must win.
1768 next
= entry
->vme_next
;
1769 if (next
== vm_map_to_entry(map
))
1773 * If there is another entry, it must be
1774 * after the end of the potential new region.
1777 if (next
->vme_start
>= end
)
1781 * Didn't fit -- move to the next entry.
1785 start
= entry
->vme_end
;
1791 * the address doesn't itself violate
1792 * the mask requirement.
1797 if ((start
& mask
) != 0)
1798 RETURN(KERN_NO_SPACE
);
1801 * ... the address is within bounds
1806 if ((start
< effective_min_offset
) ||
1807 (end
> effective_max_offset
) ||
1809 RETURN(KERN_INVALID_ADDRESS
);
1812 if (overwrite
&& zap_old_map
!= VM_MAP_NULL
) {
1814 * Fixed mapping and "overwrite" flag: attempt to
1815 * remove all existing mappings in the specified
1816 * address range, saving them in our "zap_old_map".
1818 (void) vm_map_delete(map
, start
, end
,
1819 VM_MAP_REMOVE_SAVE_ENTRIES
,
1824 * ... the starting address isn't allocated
1827 if (vm_map_lookup_entry(map
, start
, &entry
)) {
1828 if (! (flags
& VM_FLAGS_ALREADY
)) {
1829 RETURN(KERN_NO_SPACE
);
1832 * Check if what's already there is what we want.
1835 tmp_offset
= offset
;
1836 if (entry
->vme_start
< start
) {
1837 tmp_start
-= start
- entry
->vme_start
;
1838 tmp_offset
-= start
- entry
->vme_start
;
1841 for (; entry
->vme_start
< end
;
1842 entry
= entry
->vme_next
) {
1844 * Check if the mapping's attributes
1845 * match the existing map entry.
1847 if (entry
== vm_map_to_entry(map
) ||
1848 entry
->vme_start
!= tmp_start
||
1849 entry
->is_sub_map
!= is_submap
||
1850 entry
->offset
!= tmp_offset
||
1851 entry
->needs_copy
!= needs_copy
||
1852 entry
->protection
!= cur_protection
||
1853 entry
->max_protection
!= max_protection
||
1854 entry
->inheritance
!= inheritance
||
1855 entry
->alias
!= alias
) {
1856 /* not the same mapping ! */
1857 RETURN(KERN_NO_SPACE
);
1860 * Check if the same object is being mapped.
1863 if (entry
->object
.sub_map
!=
1864 (vm_map_t
) object
) {
1865 /* not the same submap */
1866 RETURN(KERN_NO_SPACE
);
1869 if (entry
->object
.vm_object
!= object
) {
1870 /* not the same VM object... */
1873 obj2
= entry
->object
.vm_object
;
1874 if ((obj2
== VM_OBJECT_NULL
||
1876 (object
== VM_OBJECT_NULL
||
1877 object
->internal
)) {
1884 RETURN(KERN_NO_SPACE
);
1889 tmp_offset
+= entry
->vme_end
- entry
->vme_start
;
1890 tmp_start
+= entry
->vme_end
- entry
->vme_start
;
1891 if (entry
->vme_end
>= end
) {
1892 /* reached the end of our mapping */
1896 /* it all matches: let's use what's already there ! */
1897 RETURN(KERN_MEMORY_PRESENT
);
1901 * ... the next region doesn't overlap the
1905 if ((entry
->vme_next
!= vm_map_to_entry(map
)) &&
1906 (entry
->vme_next
->vme_start
< end
))
1907 RETURN(KERN_NO_SPACE
);
1912 * "start" and "end" should define the endpoints of the
1913 * available new range, and
1914 * "entry" should refer to the region before the new
1917 * the map should be locked.
1921 * See whether we can avoid creating a new entry (and object) by
1922 * extending one of our neighbors. [So far, we only attempt to
1923 * extend from below.] Note that we can never extend/join
1924 * purgable objects because they need to remain distinct
1925 * entities in order to implement their "volatile object"
1930 if (object
== VM_OBJECT_NULL
) {
1931 object
= vm_object_allocate(size
);
1932 object
->copy_strategy
= MEMORY_OBJECT_COPY_NONE
;
1933 object
->purgable
= VM_PURGABLE_NONVOLATILE
;
1934 offset
= (vm_object_offset_t
)0;
1936 } else if ((is_submap
== FALSE
) &&
1937 (object
== VM_OBJECT_NULL
) &&
1938 (entry
!= vm_map_to_entry(map
)) &&
1939 (entry
->vme_end
== start
) &&
1940 (!entry
->is_shared
) &&
1941 (!entry
->is_sub_map
) &&
1942 (entry
->alias
== alias
) &&
1943 (entry
->inheritance
== inheritance
) &&
1944 (entry
->protection
== cur_protection
) &&
1945 (entry
->max_protection
== max_protection
) &&
1946 (entry
->behavior
== VM_BEHAVIOR_DEFAULT
) &&
1947 (entry
->in_transition
== 0) &&
1948 (entry
->no_cache
== no_cache
) &&
1949 ((entry
->vme_end
- entry
->vme_start
) + size
<=
1950 (alias
== VM_MEMORY_REALLOC
?
1952 NO_COALESCE_LIMIT
)) &&
1953 (entry
->wired_count
== 0)) { /* implies user_wired_count == 0 */
1954 if (vm_object_coalesce(entry
->object
.vm_object
,
1957 (vm_object_offset_t
) 0,
1958 (vm_map_size_t
)(entry
->vme_end
- entry
->vme_start
),
1959 (vm_map_size_t
)(end
- entry
->vme_end
))) {
1962 * Coalesced the two objects - can extend
1963 * the previous map entry to include the
1966 map
->size
+= (end
- entry
->vme_end
);
1967 entry
->vme_end
= end
;
1968 UPDATE_FIRST_FREE(map
, map
->first_free
);
1969 RETURN(KERN_SUCCESS
);
1973 step
= superpage_size
? SUPERPAGE_SIZE
: (end
- start
);
1976 for (tmp2_start
= start
; tmp2_start
<end
; tmp2_start
+= step
) {
1977 tmp2_end
= tmp2_start
+ step
;
1979 * Create a new entry
1980 * LP64todo - for now, we can only allocate 4GB internal objects
1981 * because the default pager can't page bigger ones. Remove this
1985 * The reserved "page zero" in each process's address space can
1986 * be arbitrarily large. Splitting it into separate 4GB objects and
1987 * therefore different VM map entries serves no purpose and just
1988 * slows down operations on the VM map, so let's not split the
1989 * allocation into 4GB chunks if the max protection is NONE. That
1990 * memory should never be accessible, so it will never get to the
1993 tmp_start
= tmp2_start
;
1994 if (object
== VM_OBJECT_NULL
&&
1995 size
> (vm_map_size_t
)ANON_CHUNK_SIZE
&&
1996 max_protection
!= VM_PROT_NONE
&&
1997 superpage_size
== 0)
1998 tmp_end
= tmp_start
+ (vm_map_size_t
)ANON_CHUNK_SIZE
;
2002 new_entry
= vm_map_entry_insert(map
, entry
, tmp_start
, tmp_end
,
2003 object
, offset
, needs_copy
,
2005 cur_protection
, max_protection
,
2006 VM_BEHAVIOR_DEFAULT
,
2007 inheritance
, 0, no_cache
,
2008 permanent
, superpage_size
);
2009 new_entry
->alias
= alias
;
2012 boolean_t submap_is_64bit
;
2015 new_entry
->is_sub_map
= TRUE
;
2016 submap
= (vm_map_t
) object
;
2017 submap_is_64bit
= vm_map_is_64bit(submap
);
2018 use_pmap
= (alias
== VM_MEMORY_SHARED_PMAP
);
2019 #ifndef NO_NESTED_PMAP
2020 if (use_pmap
&& submap
->pmap
== NULL
) {
2021 /* we need a sub pmap to nest... */
2022 submap
->pmap
= pmap_create(0, submap_is_64bit
);
2023 if (submap
->pmap
== NULL
) {
2024 /* let's proceed without nesting... */
2027 if (use_pmap
&& submap
->pmap
!= NULL
) {
2028 kr
= pmap_nest(map
->pmap
,
2032 tmp_end
- tmp_start
);
2033 if (kr
!= KERN_SUCCESS
) {
2034 printf("vm_map_enter: "
2035 "pmap_nest(0x%llx,0x%llx) "
2037 (long long)tmp_start
,
2041 /* we're now nested ! */
2042 new_entry
->use_pmap
= TRUE
;
2046 #endif /* NO_NESTED_PMAP */
2050 if (superpage_size
) {
2052 vm_object_t sp_object
;
2056 /* allocate one superpage */
2057 kr
= cpm_allocate(SUPERPAGE_SIZE
, &pages
, 0, SUPERPAGE_NBASEPAGES
-1, TRUE
, 0);
2058 if (kr
!= KERN_SUCCESS
) {
2059 new_mapping_established
= TRUE
; /* will cause deallocation of whole range */
2063 /* create one vm_object per superpage */
2064 sp_object
= vm_object_allocate((vm_map_size_t
)(entry
->vme_end
- entry
->vme_start
));
2065 sp_object
->phys_contiguous
= TRUE
;
2066 sp_object
->shadow_offset
= (vm_object_offset_t
)pages
->phys_page
*PAGE_SIZE
;
2067 entry
->object
.vm_object
= sp_object
;
2069 /* enter the base pages into the object */
2070 vm_object_lock(sp_object
);
2071 for (offset
= 0; offset
< SUPERPAGE_SIZE
; offset
+= PAGE_SIZE
) {
2073 pmap_zero_page(m
->phys_page
);
2074 pages
= NEXT_PAGE(m
);
2075 *(NEXT_PAGE_PTR(m
)) = VM_PAGE_NULL
;
2076 vm_page_insert(m
, sp_object
, offset
);
2078 vm_object_unlock(sp_object
);
2080 } while (tmp_end
!= tmp2_end
&&
2081 (tmp_start
= tmp_end
) &&
2082 (tmp_end
= (tmp2_end
- tmp_end
> (vm_map_size_t
)ANON_CHUNK_SIZE
) ?
2083 tmp_end
+ (vm_map_size_t
)ANON_CHUNK_SIZE
: tmp2_end
));
2089 new_mapping_established
= TRUE
;
2091 /* Wire down the new entry if the user
2092 * requested all new map entries be wired.
2094 if ((map
->wiring_required
)||(superpage_size
)) {
2095 pmap_empty
= FALSE
; /* pmap won't be empty */
2096 result
= vm_map_wire(map
, start
, end
,
2097 new_entry
->protection
, TRUE
);
2101 if ((object
!= VM_OBJECT_NULL
) &&
2102 (vm_map_pmap_enter_enable
) &&
2105 (size
< (128*1024))) {
2106 pmap_empty
= FALSE
; /* pmap won't be empty */
2108 if (override_nx(map
, alias
) && cur_protection
)
2109 cur_protection
|= VM_PROT_EXECUTE
;
2111 vm_map_pmap_enter(map
, start
, end
,
2112 object
, offset
, cur_protection
);
2116 if (result
== KERN_SUCCESS
) {
2117 vm_prot_t pager_prot
;
2118 memory_object_t pager
;
2121 !(flags
& VM_FLAGS_NO_PMAP_CHECK
)) {
2122 assert(vm_map_pmap_is_empty(map
,
2128 * For "named" VM objects, let the pager know that the
2129 * memory object is being mapped. Some pagers need to keep
2130 * track of this, to know when they can reclaim the memory
2131 * object, for example.
2132 * VM calls memory_object_map() for each mapping (specifying
2133 * the protection of each mapping) and calls
2134 * memory_object_last_unmap() when all the mappings are gone.
2136 pager_prot
= max_protection
;
2139 * Copy-On-Write mapping: won't modify
2140 * the memory object.
2142 pager_prot
&= ~VM_PROT_WRITE
;
2145 object
!= VM_OBJECT_NULL
&&
2147 object
->pager
!= MEMORY_OBJECT_NULL
) {
2148 vm_object_lock(object
);
2149 pager
= object
->pager
;
2150 if (object
->named
&&
2151 pager
!= MEMORY_OBJECT_NULL
) {
2152 assert(object
->pager_ready
);
2153 vm_object_mapping_wait(object
, THREAD_UNINT
);
2154 vm_object_mapping_begin(object
);
2155 vm_object_unlock(object
);
2157 kr
= memory_object_map(pager
, pager_prot
);
2158 assert(kr
== KERN_SUCCESS
);
2160 vm_object_lock(object
);
2161 vm_object_mapping_end(object
);
2163 vm_object_unlock(object
);
2166 if (new_mapping_established
) {
2168 * We have to get rid of the new mappings since we
2169 * won't make them available to the user.
2170 * Try and do that atomically, to minimize the risk
2171 * that someone else create new mappings that range.
2173 zap_new_map
= vm_map_create(PMAP_NULL
,
2176 map
->hdr
.entries_pageable
);
2181 (void) vm_map_delete(map
, *address
, *address
+size
,
2182 VM_MAP_REMOVE_SAVE_ENTRIES
,
2185 if (zap_old_map
!= VM_MAP_NULL
&&
2186 zap_old_map
->hdr
.nentries
!= 0) {
2187 vm_map_entry_t entry1
, entry2
;
2190 * The new mapping failed. Attempt to restore
2191 * the old mappings, saved in the "zap_old_map".
2198 /* first check if the coast is still clear */
2199 start
= vm_map_first_entry(zap_old_map
)->vme_start
;
2200 end
= vm_map_last_entry(zap_old_map
)->vme_end
;
2201 if (vm_map_lookup_entry(map
, start
, &entry1
) ||
2202 vm_map_lookup_entry(map
, end
, &entry2
) ||
2205 * Part of that range has already been
2206 * re-mapped: we can't restore the old
2209 vm_map_enter_restore_failures
++;
2212 * Transfer the saved map entries from
2213 * "zap_old_map" to the original "map",
2214 * inserting them all after "entry1".
2216 for (entry2
= vm_map_first_entry(zap_old_map
);
2217 entry2
!= vm_map_to_entry(zap_old_map
);
2218 entry2
= vm_map_first_entry(zap_old_map
)) {
2219 vm_map_size_t entry_size
;
2221 entry_size
= (entry2
->vme_end
-
2223 vm_map_entry_unlink(zap_old_map
,
2225 zap_old_map
->size
-= entry_size
;
2226 vm_map_entry_link(map
, entry1
, entry2
);
2227 map
->size
+= entry_size
;
2230 if (map
->wiring_required
) {
2232 * XXX TODO: we should rewire the
2236 vm_map_enter_restore_successes
++;
2246 * Get rid of the "zap_maps" and all the map entries that
2247 * they may still contain.
2249 if (zap_old_map
!= VM_MAP_NULL
) {
2250 vm_map_destroy(zap_old_map
, VM_MAP_REMOVE_NO_PMAP_CLEANUP
);
2251 zap_old_map
= VM_MAP_NULL
;
2253 if (zap_new_map
!= VM_MAP_NULL
) {
2254 vm_map_destroy(zap_new_map
, VM_MAP_REMOVE_NO_PMAP_CLEANUP
);
2255 zap_new_map
= VM_MAP_NULL
;
2264 vm_map_enter_mem_object(
2265 vm_map_t target_map
,
2266 vm_map_offset_t
*address
,
2267 vm_map_size_t initial_size
,
2268 vm_map_offset_t mask
,
2271 vm_object_offset_t offset
,
2273 vm_prot_t cur_protection
,
2274 vm_prot_t max_protection
,
2275 vm_inherit_t inheritance
)
2277 vm_map_address_t map_addr
;
2278 vm_map_size_t map_size
;
2280 vm_object_size_t size
;
2281 kern_return_t result
;
2284 * Check arguments for validity
2286 if ((target_map
== VM_MAP_NULL
) ||
2287 (cur_protection
& ~VM_PROT_ALL
) ||
2288 (max_protection
& ~VM_PROT_ALL
) ||
2289 (inheritance
> VM_INHERIT_LAST_VALID
) ||
2291 return KERN_INVALID_ARGUMENT
;
2293 map_addr
= vm_map_trunc_page(*address
);
2294 map_size
= vm_map_round_page(initial_size
);
2295 size
= vm_object_round_page(initial_size
);
2298 * Find the vm object (if any) corresponding to this port.
2300 if (!IP_VALID(port
)) {
2301 object
= VM_OBJECT_NULL
;
2304 } else if (ip_kotype(port
) == IKOT_NAMED_ENTRY
) {
2305 vm_named_entry_t named_entry
;
2307 named_entry
= (vm_named_entry_t
) port
->ip_kobject
;
2308 /* a few checks to make sure user is obeying rules */
2310 if (offset
>= named_entry
->size
)
2311 return KERN_INVALID_RIGHT
;
2312 size
= named_entry
->size
- offset
;
2314 if ((named_entry
->protection
& max_protection
) !=
2316 return KERN_INVALID_RIGHT
;
2317 if ((named_entry
->protection
& cur_protection
) !=
2319 return KERN_INVALID_RIGHT
;
2320 if (named_entry
->size
< (offset
+ size
))
2321 return KERN_INVALID_ARGUMENT
;
2323 /* the callers parameter offset is defined to be the */
2324 /* offset from beginning of named entry offset in object */
2325 offset
= offset
+ named_entry
->offset
;
2327 named_entry_lock(named_entry
);
2328 if (named_entry
->is_sub_map
) {
2331 submap
= named_entry
->backing
.map
;
2332 vm_map_lock(submap
);
2333 vm_map_reference(submap
);
2334 vm_map_unlock(submap
);
2335 named_entry_unlock(named_entry
);
2337 result
= vm_map_enter(target_map
,
2341 flags
| VM_FLAGS_SUBMAP
,
2342 (vm_object_t
) submap
,
2348 if (result
!= KERN_SUCCESS
) {
2349 vm_map_deallocate(submap
);
2352 * No need to lock "submap" just to check its
2353 * "mapped" flag: that flag is never reset
2354 * once it's been set and if we race, we'll
2355 * just end up setting it twice, which is OK.
2357 if (submap
->mapped
== FALSE
) {
2359 * This submap has never been mapped.
2360 * Set its "mapped" flag now that it
2362 * This happens only for the first ever
2363 * mapping of a "submap".
2365 vm_map_lock(submap
);
2366 submap
->mapped
= TRUE
;
2367 vm_map_unlock(submap
);
2369 *address
= map_addr
;
2373 } else if (named_entry
->is_pager
) {
2374 unsigned int access
;
2375 vm_prot_t protections
;
2376 unsigned int wimg_mode
;
2377 boolean_t cache_attr
;
2379 protections
= named_entry
->protection
& VM_PROT_ALL
;
2380 access
= GET_MAP_MEM(named_entry
->protection
);
2382 object
= vm_object_enter(named_entry
->backing
.pager
,
2384 named_entry
->internal
,
2387 if (object
== VM_OBJECT_NULL
) {
2388 named_entry_unlock(named_entry
);
2389 return KERN_INVALID_OBJECT
;
2392 /* JMM - drop reference on pager here */
2394 /* create an extra ref for the named entry */
2395 vm_object_lock(object
);
2396 vm_object_reference_locked(object
);
2397 named_entry
->backing
.object
= object
;
2398 named_entry
->is_pager
= FALSE
;
2399 named_entry_unlock(named_entry
);
2401 wimg_mode
= object
->wimg_bits
;
2402 if (access
== MAP_MEM_IO
) {
2403 wimg_mode
= VM_WIMG_IO
;
2404 } else if (access
== MAP_MEM_COPYBACK
) {
2405 wimg_mode
= VM_WIMG_USE_DEFAULT
;
2406 } else if (access
== MAP_MEM_WTHRU
) {
2407 wimg_mode
= VM_WIMG_WTHRU
;
2408 } else if (access
== MAP_MEM_WCOMB
) {
2409 wimg_mode
= VM_WIMG_WCOMB
;
2411 if (wimg_mode
== VM_WIMG_IO
||
2412 wimg_mode
== VM_WIMG_WCOMB
)
2417 /* wait for object (if any) to be ready */
2418 if (!named_entry
->internal
) {
2419 while (!object
->pager_ready
) {
2422 VM_OBJECT_EVENT_PAGER_READY
,
2424 vm_object_lock(object
);
2428 if (object
->wimg_bits
!= wimg_mode
) {
2431 vm_object_paging_wait(object
, THREAD_UNINT
);
2433 object
->wimg_bits
= wimg_mode
;
2434 queue_iterate(&object
->memq
, p
, vm_page_t
, listq
) {
2435 if (!p
->fictitious
) {
2437 pmap_disconnect(p
->phys_page
);
2439 pmap_sync_page_attributes_phys(p
->phys_page
);
2443 object
->true_share
= TRUE
;
2444 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
)
2445 object
->copy_strategy
= MEMORY_OBJECT_COPY_DELAY
;
2446 vm_object_unlock(object
);
2448 /* This is the case where we are going to map */
2449 /* an already mapped object. If the object is */
2450 /* not ready it is internal. An external */
2451 /* object cannot be mapped until it is ready */
2452 /* we can therefore avoid the ready check */
2454 object
= named_entry
->backing
.object
;
2455 assert(object
!= VM_OBJECT_NULL
);
2456 named_entry_unlock(named_entry
);
2457 vm_object_reference(object
);
2459 } else if (ip_kotype(port
) == IKOT_MEMORY_OBJECT
) {
2461 * JMM - This is temporary until we unify named entries
2462 * and raw memory objects.
2464 * Detected fake ip_kotype for a memory object. In
2465 * this case, the port isn't really a port at all, but
2466 * instead is just a raw memory object.
2469 object
= vm_object_enter((memory_object_t
)port
,
2470 size
, FALSE
, FALSE
, FALSE
);
2471 if (object
== VM_OBJECT_NULL
)
2472 return KERN_INVALID_OBJECT
;
2474 /* wait for object (if any) to be ready */
2475 if (object
!= VM_OBJECT_NULL
) {
2476 if (object
== kernel_object
) {
2477 printf("Warning: Attempt to map kernel object"
2478 " by a non-private kernel entity\n");
2479 return KERN_INVALID_OBJECT
;
2481 if (!object
->pager_ready
) {
2482 vm_object_lock(object
);
2484 while (!object
->pager_ready
) {
2485 vm_object_wait(object
,
2486 VM_OBJECT_EVENT_PAGER_READY
,
2488 vm_object_lock(object
);
2490 vm_object_unlock(object
);
2494 return KERN_INVALID_OBJECT
;
2497 if (object
!= VM_OBJECT_NULL
&&
2499 object
->pager
!= MEMORY_OBJECT_NULL
&&
2500 object
->copy_strategy
!= MEMORY_OBJECT_COPY_NONE
) {
2501 memory_object_t pager
;
2502 vm_prot_t pager_prot
;
2506 * For "named" VM objects, let the pager know that the
2507 * memory object is being mapped. Some pagers need to keep
2508 * track of this, to know when they can reclaim the memory
2509 * object, for example.
2510 * VM calls memory_object_map() for each mapping (specifying
2511 * the protection of each mapping) and calls
2512 * memory_object_last_unmap() when all the mappings are gone.
2514 pager_prot
= max_protection
;
2517 * Copy-On-Write mapping: won't modify the
2520 pager_prot
&= ~VM_PROT_WRITE
;
2522 vm_object_lock(object
);
2523 pager
= object
->pager
;
2524 if (object
->named
&&
2525 pager
!= MEMORY_OBJECT_NULL
&&
2526 object
->copy_strategy
!= MEMORY_OBJECT_COPY_NONE
) {
2527 assert(object
->pager_ready
);
2528 vm_object_mapping_wait(object
, THREAD_UNINT
);
2529 vm_object_mapping_begin(object
);
2530 vm_object_unlock(object
);
2532 kr
= memory_object_map(pager
, pager_prot
);
2533 assert(kr
== KERN_SUCCESS
);
2535 vm_object_lock(object
);
2536 vm_object_mapping_end(object
);
2538 vm_object_unlock(object
);
2542 * Perform the copy if requested
2546 vm_object_t new_object
;
2547 vm_object_offset_t new_offset
;
2549 result
= vm_object_copy_strategically(object
, offset
, size
,
2550 &new_object
, &new_offset
,
2554 if (result
== KERN_MEMORY_RESTART_COPY
) {
2556 boolean_t src_needs_copy
;
2560 * We currently ignore src_needs_copy.
2561 * This really is the issue of how to make
2562 * MEMORY_OBJECT_COPY_SYMMETRIC safe for
2563 * non-kernel users to use. Solution forthcoming.
2564 * In the meantime, since we don't allow non-kernel
2565 * memory managers to specify symmetric copy,
2566 * we won't run into problems here.
2568 new_object
= object
;
2569 new_offset
= offset
;
2570 success
= vm_object_copy_quickly(&new_object
,
2575 result
= KERN_SUCCESS
;
2578 * Throw away the reference to the
2579 * original object, as it won't be mapped.
2582 vm_object_deallocate(object
);
2584 if (result
!= KERN_SUCCESS
)
2587 object
= new_object
;
2588 offset
= new_offset
;
2591 result
= vm_map_enter(target_map
,
2592 &map_addr
, map_size
,
2593 (vm_map_offset_t
)mask
,
2597 cur_protection
, max_protection
, inheritance
);
2598 if (result
!= KERN_SUCCESS
)
2599 vm_object_deallocate(object
);
2600 *address
= map_addr
;
2608 vm_map_enter_mem_object_control(
2609 vm_map_t target_map
,
2610 vm_map_offset_t
*address
,
2611 vm_map_size_t initial_size
,
2612 vm_map_offset_t mask
,
2614 memory_object_control_t control
,
2615 vm_object_offset_t offset
,
2617 vm_prot_t cur_protection
,
2618 vm_prot_t max_protection
,
2619 vm_inherit_t inheritance
)
2621 vm_map_address_t map_addr
;
2622 vm_map_size_t map_size
;
2624 vm_object_size_t size
;
2625 kern_return_t result
;
2626 memory_object_t pager
;
2627 vm_prot_t pager_prot
;
2631 * Check arguments for validity
2633 if ((target_map
== VM_MAP_NULL
) ||
2634 (cur_protection
& ~VM_PROT_ALL
) ||
2635 (max_protection
& ~VM_PROT_ALL
) ||
2636 (inheritance
> VM_INHERIT_LAST_VALID
) ||
2638 return KERN_INVALID_ARGUMENT
;
2640 map_addr
= vm_map_trunc_page(*address
);
2641 map_size
= vm_map_round_page(initial_size
);
2642 size
= vm_object_round_page(initial_size
);
2644 object
= memory_object_control_to_vm_object(control
);
2646 if (object
== VM_OBJECT_NULL
)
2647 return KERN_INVALID_OBJECT
;
2649 if (object
== kernel_object
) {
2650 printf("Warning: Attempt to map kernel object"
2651 " by a non-private kernel entity\n");
2652 return KERN_INVALID_OBJECT
;
2655 vm_object_lock(object
);
2656 object
->ref_count
++;
2657 vm_object_res_reference(object
);
2660 * For "named" VM objects, let the pager know that the
2661 * memory object is being mapped. Some pagers need to keep
2662 * track of this, to know when they can reclaim the memory
2663 * object, for example.
2664 * VM calls memory_object_map() for each mapping (specifying
2665 * the protection of each mapping) and calls
2666 * memory_object_last_unmap() when all the mappings are gone.
2668 pager_prot
= max_protection
;
2670 pager_prot
&= ~VM_PROT_WRITE
;
2672 pager
= object
->pager
;
2673 if (object
->named
&&
2674 pager
!= MEMORY_OBJECT_NULL
&&
2675 object
->copy_strategy
!= MEMORY_OBJECT_COPY_NONE
) {
2676 assert(object
->pager_ready
);
2677 vm_object_mapping_wait(object
, THREAD_UNINT
);
2678 vm_object_mapping_begin(object
);
2679 vm_object_unlock(object
);
2681 kr
= memory_object_map(pager
, pager_prot
);
2682 assert(kr
== KERN_SUCCESS
);
2684 vm_object_lock(object
);
2685 vm_object_mapping_end(object
);
2687 vm_object_unlock(object
);
2690 * Perform the copy if requested
2694 vm_object_t new_object
;
2695 vm_object_offset_t new_offset
;
2697 result
= vm_object_copy_strategically(object
, offset
, size
,
2698 &new_object
, &new_offset
,
2702 if (result
== KERN_MEMORY_RESTART_COPY
) {
2704 boolean_t src_needs_copy
;
2708 * We currently ignore src_needs_copy.
2709 * This really is the issue of how to make
2710 * MEMORY_OBJECT_COPY_SYMMETRIC safe for
2711 * non-kernel users to use. Solution forthcoming.
2712 * In the meantime, since we don't allow non-kernel
2713 * memory managers to specify symmetric copy,
2714 * we won't run into problems here.
2716 new_object
= object
;
2717 new_offset
= offset
;
2718 success
= vm_object_copy_quickly(&new_object
,
2723 result
= KERN_SUCCESS
;
2726 * Throw away the reference to the
2727 * original object, as it won't be mapped.
2730 vm_object_deallocate(object
);
2732 if (result
!= KERN_SUCCESS
)
2735 object
= new_object
;
2736 offset
= new_offset
;
2739 result
= vm_map_enter(target_map
,
2740 &map_addr
, map_size
,
2741 (vm_map_offset_t
)mask
,
2745 cur_protection
, max_protection
, inheritance
);
2746 if (result
!= KERN_SUCCESS
)
2747 vm_object_deallocate(object
);
2748 *address
= map_addr
;
2757 extern pmap_paddr_t avail_start
, avail_end
;
2761 * Allocate memory in the specified map, with the caveat that
2762 * the memory is physically contiguous. This call may fail
2763 * if the system can't find sufficient contiguous memory.
2764 * This call may cause or lead to heart-stopping amounts of
2767 * Memory obtained from this call should be freed in the
2768 * normal way, viz., via vm_deallocate.
2773 vm_map_offset_t
*addr
,
2777 vm_object_t cpm_obj
;
2781 vm_map_offset_t va
, start
, end
, offset
;
2783 vm_map_offset_t prev_addr
;
2784 #endif /* MACH_ASSERT */
2786 boolean_t anywhere
= ((VM_FLAGS_ANYWHERE
& flags
) != 0);
2788 if (!vm_allocate_cpm_enabled
)
2789 return KERN_FAILURE
;
2793 return KERN_SUCCESS
;
2796 *addr
= vm_map_min(map
);
2798 *addr
= vm_map_trunc_page(*addr
);
2799 size
= vm_map_round_page(size
);
2802 * LP64todo - cpm_allocate should probably allow
2803 * allocations of >4GB, but not with the current
2804 * algorithm, so just cast down the size for now.
2806 if (size
> VM_MAX_ADDRESS
)
2807 return KERN_RESOURCE_SHORTAGE
;
2808 if ((kr
= cpm_allocate(CAST_DOWN(vm_size_t
, size
),
2809 &pages
, 0, 0, TRUE
, flags
)) != KERN_SUCCESS
)
2812 cpm_obj
= vm_object_allocate((vm_object_size_t
)size
);
2813 assert(cpm_obj
!= VM_OBJECT_NULL
);
2814 assert(cpm_obj
->internal
);
2815 assert(cpm_obj
->size
== (vm_object_size_t
)size
);
2816 assert(cpm_obj
->can_persist
== FALSE
);
2817 assert(cpm_obj
->pager_created
== FALSE
);
2818 assert(cpm_obj
->pageout
== FALSE
);
2819 assert(cpm_obj
->shadow
== VM_OBJECT_NULL
);
2822 * Insert pages into object.
2825 vm_object_lock(cpm_obj
);
2826 for (offset
= 0; offset
< size
; offset
+= PAGE_SIZE
) {
2828 pages
= NEXT_PAGE(m
);
2829 *(NEXT_PAGE_PTR(m
)) = VM_PAGE_NULL
;
2831 assert(!m
->gobbled
);
2833 assert(!m
->pageout
);
2835 assert(VM_PAGE_WIRED(m
));
2838 * "m" is not supposed to be pageable, so it
2839 * should not be encrypted. It wouldn't be safe
2840 * to enter it in a new VM object while encrypted.
2842 ASSERT_PAGE_DECRYPTED(m
);
2844 assert(m
->phys_page
>=(avail_start
>>PAGE_SHIFT
) && m
->phys_page
<=(avail_end
>>PAGE_SHIFT
));
2847 vm_page_insert(m
, cpm_obj
, offset
);
2849 assert(cpm_obj
->resident_page_count
== size
/ PAGE_SIZE
);
2850 vm_object_unlock(cpm_obj
);
2853 * Hang onto a reference on the object in case a
2854 * multi-threaded application for some reason decides
2855 * to deallocate the portion of the address space into
2856 * which we will insert this object.
2858 * Unfortunately, we must insert the object now before
2859 * we can talk to the pmap module about which addresses
2860 * must be wired down. Hence, the race with a multi-
2863 vm_object_reference(cpm_obj
);
2866 * Insert object into map.
2876 (vm_object_offset_t
)0,
2880 VM_INHERIT_DEFAULT
);
2882 if (kr
!= KERN_SUCCESS
) {
2884 * A CPM object doesn't have can_persist set,
2885 * so all we have to do is deallocate it to
2886 * free up these pages.
2888 assert(cpm_obj
->pager_created
== FALSE
);
2889 assert(cpm_obj
->can_persist
== FALSE
);
2890 assert(cpm_obj
->pageout
== FALSE
);
2891 assert(cpm_obj
->shadow
== VM_OBJECT_NULL
);
2892 vm_object_deallocate(cpm_obj
); /* kill acquired ref */
2893 vm_object_deallocate(cpm_obj
); /* kill creation ref */
2897 * Inform the physical mapping system that the
2898 * range of addresses may not fault, so that
2899 * page tables and such can be locked down as well.
2903 pmap
= vm_map_pmap(map
);
2904 pmap_pageable(pmap
, start
, end
, FALSE
);
2907 * Enter each page into the pmap, to avoid faults.
2908 * Note that this loop could be coded more efficiently,
2909 * if the need arose, rather than looking up each page
2912 for (offset
= 0, va
= start
; offset
< size
;
2913 va
+= PAGE_SIZE
, offset
+= PAGE_SIZE
) {
2916 vm_object_lock(cpm_obj
);
2917 m
= vm_page_lookup(cpm_obj
, (vm_object_offset_t
)offset
);
2918 assert(m
!= VM_PAGE_NULL
);
2920 vm_page_zero_fill(m
);
2922 type_of_fault
= DBG_ZERO_FILL_FAULT
;
2924 vm_fault_enter(m
, pmap
, va
, VM_PROT_ALL
,
2925 VM_PAGE_WIRED(m
), FALSE
, FALSE
,
2928 vm_object_unlock(cpm_obj
);
2933 * Verify ordering in address space.
2935 for (offset
= 0; offset
< size
; offset
+= PAGE_SIZE
) {
2936 vm_object_lock(cpm_obj
);
2937 m
= vm_page_lookup(cpm_obj
, (vm_object_offset_t
)offset
);
2938 vm_object_unlock(cpm_obj
);
2939 if (m
== VM_PAGE_NULL
)
2940 panic("vm_allocate_cpm: obj 0x%x off 0x%x no page",
2945 assert(!m
->fictitious
);
2946 assert(!m
->private);
2949 assert(!m
->cleaning
);
2950 assert(!m
->precious
);
2951 assert(!m
->clustered
);
2953 if (m
->phys_page
!= prev_addr
+ 1) {
2954 printf("start 0x%x end 0x%x va 0x%x\n",
2956 printf("obj 0x%x off 0x%x\n", cpm_obj
, offset
);
2957 printf("m 0x%x prev_address 0x%x\n", m
,
2959 panic("vm_allocate_cpm: pages not contig!");
2962 prev_addr
= m
->phys_page
;
2964 #endif /* MACH_ASSERT */
2966 vm_object_deallocate(cpm_obj
); /* kill extra ref */
2975 * Interface is defined in all cases, but unless the kernel
2976 * is built explicitly for this option, the interface does
2982 __unused vm_map_t map
,
2983 __unused vm_map_offset_t
*addr
,
2984 __unused vm_map_size_t size
,
2987 return KERN_FAILURE
;
2991 /* Not used without nested pmaps */
2992 #ifndef NO_NESTED_PMAP
2994 * Clip and unnest a portion of a nested submap mapping.
3001 vm_map_entry_t entry
,
3002 vm_map_offset_t start_unnest
,
3003 vm_map_offset_t end_unnest
)
3005 vm_map_offset_t old_start_unnest
= start_unnest
;
3006 vm_map_offset_t old_end_unnest
= end_unnest
;
3008 assert(entry
->is_sub_map
);
3009 assert(entry
->object
.sub_map
!= NULL
);
3012 * Query the platform for the optimal unnest range.
3013 * DRK: There's some duplication of effort here, since
3014 * callers may have adjusted the range to some extent. This
3015 * routine was introduced to support 1GiB subtree nesting
3016 * for x86 platforms, which can also nest on 2MiB boundaries
3017 * depending on size/alignment.
3019 if (pmap_adjust_unnest_parameters(map
->pmap
, &start_unnest
, &end_unnest
)) {
3020 log_unnest_badness(map
, old_start_unnest
, old_end_unnest
);
3023 if (entry
->vme_start
> start_unnest
||
3024 entry
->vme_end
< end_unnest
) {
3025 panic("vm_map_clip_unnest(0x%llx,0x%llx): "
3026 "bad nested entry: start=0x%llx end=0x%llx\n",
3027 (long long)start_unnest
, (long long)end_unnest
,
3028 (long long)entry
->vme_start
, (long long)entry
->vme_end
);
3031 if (start_unnest
> entry
->vme_start
) {
3032 _vm_map_clip_start(&map
->hdr
,
3035 UPDATE_FIRST_FREE(map
, map
->first_free
);
3037 if (entry
->vme_end
> end_unnest
) {
3038 _vm_map_clip_end(&map
->hdr
,
3041 UPDATE_FIRST_FREE(map
, map
->first_free
);
3044 pmap_unnest(map
->pmap
,
3046 entry
->vme_end
- entry
->vme_start
);
3047 if ((map
->mapped
) && (map
->ref_count
)) {
3048 /* clean up parent map/maps */
3049 vm_map_submap_pmap_clean(
3050 map
, entry
->vme_start
,
3052 entry
->object
.sub_map
,
3055 entry
->use_pmap
= FALSE
;
3057 #endif /* NO_NESTED_PMAP */
3060 * vm_map_clip_start: [ internal use only ]
3062 * Asserts that the given entry begins at or after
3063 * the specified address; if necessary,
3064 * it splits the entry into two.
3069 vm_map_entry_t entry
,
3070 vm_map_offset_t startaddr
)
3072 #ifndef NO_NESTED_PMAP
3073 if (entry
->use_pmap
&&
3074 startaddr
>= entry
->vme_start
) {
3075 vm_map_offset_t start_unnest
, end_unnest
;
3078 * Make sure "startaddr" is no longer in a nested range
3079 * before we clip. Unnest only the minimum range the platform
3081 * vm_map_clip_unnest may perform additional adjustments to
3084 start_unnest
= startaddr
& ~(pmap_nesting_size_min
- 1);
3085 end_unnest
= start_unnest
+ pmap_nesting_size_min
;
3086 vm_map_clip_unnest(map
, entry
, start_unnest
, end_unnest
);
3088 #endif /* NO_NESTED_PMAP */
3089 if (startaddr
> entry
->vme_start
) {
3090 if (entry
->object
.vm_object
&&
3091 !entry
->is_sub_map
&&
3092 entry
->object
.vm_object
->phys_contiguous
) {
3093 pmap_remove(map
->pmap
,
3094 (addr64_t
)(entry
->vme_start
),
3095 (addr64_t
)(entry
->vme_end
));
3097 _vm_map_clip_start(&map
->hdr
, entry
, startaddr
);
3098 UPDATE_FIRST_FREE(map
, map
->first_free
);
3103 #define vm_map_copy_clip_start(copy, entry, startaddr) \
3105 if ((startaddr) > (entry)->vme_start) \
3106 _vm_map_clip_start(&(copy)->cpy_hdr,(entry),(startaddr)); \
3110 * This routine is called only when it is known that
3111 * the entry must be split.
3115 register struct vm_map_header
*map_header
,
3116 register vm_map_entry_t entry
,
3117 register vm_map_offset_t start
)
3119 register vm_map_entry_t new_entry
;
3122 * Split off the front portion --
3123 * note that we must insert the new
3124 * entry BEFORE this one, so that
3125 * this entry has the specified starting
3129 new_entry
= _vm_map_entry_create(map_header
);
3130 vm_map_entry_copy_full(new_entry
, entry
);
3132 new_entry
->vme_end
= start
;
3133 entry
->offset
+= (start
- entry
->vme_start
);
3134 entry
->vme_start
= start
;
3136 _vm_map_entry_link(map_header
, entry
->vme_prev
, new_entry
);
3138 if (entry
->is_sub_map
)
3139 vm_map_reference(new_entry
->object
.sub_map
);
3141 vm_object_reference(new_entry
->object
.vm_object
);
3146 * vm_map_clip_end: [ internal use only ]
3148 * Asserts that the given entry ends at or before
3149 * the specified address; if necessary,
3150 * it splits the entry into two.
3155 vm_map_entry_t entry
,
3156 vm_map_offset_t endaddr
)
3158 if (endaddr
> entry
->vme_end
) {
3160 * Within the scope of this clipping, limit "endaddr" to
3161 * the end of this map entry...
3163 endaddr
= entry
->vme_end
;
3165 #ifndef NO_NESTED_PMAP
3166 if (entry
->use_pmap
) {
3167 vm_map_offset_t start_unnest
, end_unnest
;
3170 * Make sure the range between the start of this entry and
3171 * the new "endaddr" is no longer nested before we clip.
3172 * Unnest only the minimum range the platform can handle.
3173 * vm_map_clip_unnest may perform additional adjustments to
3176 start_unnest
= entry
->vme_start
;
3178 (endaddr
+ pmap_nesting_size_min
- 1) &
3179 ~(pmap_nesting_size_min
- 1);
3180 vm_map_clip_unnest(map
, entry
, start_unnest
, end_unnest
);
3182 #endif /* NO_NESTED_PMAP */
3183 if (endaddr
< entry
->vme_end
) {
3184 if (entry
->object
.vm_object
&&
3185 !entry
->is_sub_map
&&
3186 entry
->object
.vm_object
->phys_contiguous
) {
3187 pmap_remove(map
->pmap
,
3188 (addr64_t
)(entry
->vme_start
),
3189 (addr64_t
)(entry
->vme_end
));
3191 _vm_map_clip_end(&map
->hdr
, entry
, endaddr
);
3192 UPDATE_FIRST_FREE(map
, map
->first_free
);
3197 #define vm_map_copy_clip_end(copy, entry, endaddr) \
3199 if ((endaddr) < (entry)->vme_end) \
3200 _vm_map_clip_end(&(copy)->cpy_hdr,(entry),(endaddr)); \
3204 * This routine is called only when it is known that
3205 * the entry must be split.
3209 register struct vm_map_header
*map_header
,
3210 register vm_map_entry_t entry
,
3211 register vm_map_offset_t end
)
3213 register vm_map_entry_t new_entry
;
3216 * Create a new entry and insert it
3217 * AFTER the specified entry
3220 new_entry
= _vm_map_entry_create(map_header
);
3221 vm_map_entry_copy_full(new_entry
, entry
);
3223 new_entry
->vme_start
= entry
->vme_end
= end
;
3224 new_entry
->offset
+= (end
- entry
->vme_start
);
3226 _vm_map_entry_link(map_header
, entry
, new_entry
);
3228 if (entry
->is_sub_map
)
3229 vm_map_reference(new_entry
->object
.sub_map
);
3231 vm_object_reference(new_entry
->object
.vm_object
);
3236 * VM_MAP_RANGE_CHECK: [ internal use only ]
3238 * Asserts that the starting and ending region
3239 * addresses fall within the valid range of the map.
3241 #define VM_MAP_RANGE_CHECK(map, start, end) \
3243 if (start < vm_map_min(map)) \
3244 start = vm_map_min(map); \
3245 if (end > vm_map_max(map)) \
3246 end = vm_map_max(map); \
3252 * vm_map_range_check: [ internal use only ]
3254 * Check that the region defined by the specified start and
3255 * end addresses are wholly contained within a single map
3256 * entry or set of adjacent map entries of the spacified map,
3257 * i.e. the specified region contains no unmapped space.
3258 * If any or all of the region is unmapped, FALSE is returned.
3259 * Otherwise, TRUE is returned and if the output argument 'entry'
3260 * is not NULL it points to the map entry containing the start
3263 * The map is locked for reading on entry and is left locked.
3267 register vm_map_t map
,
3268 register vm_map_offset_t start
,
3269 register vm_map_offset_t end
,
3270 vm_map_entry_t
*entry
)
3273 register vm_map_offset_t prev
;
3276 * Basic sanity checks first
3278 if (start
< vm_map_min(map
) || end
> vm_map_max(map
) || start
> end
)
3282 * Check first if the region starts within a valid
3283 * mapping for the map.
3285 if (!vm_map_lookup_entry(map
, start
, &cur
))
3289 * Optimize for the case that the region is contained
3290 * in a single map entry.
3292 if (entry
!= (vm_map_entry_t
*) NULL
)
3294 if (end
<= cur
->vme_end
)
3298 * If the region is not wholly contained within a
3299 * single entry, walk the entries looking for holes.
3301 prev
= cur
->vme_end
;
3302 cur
= cur
->vme_next
;
3303 while ((cur
!= vm_map_to_entry(map
)) && (prev
== cur
->vme_start
)) {
3304 if (end
<= cur
->vme_end
)
3306 prev
= cur
->vme_end
;
3307 cur
= cur
->vme_next
;
3313 * vm_map_submap: [ kernel use only ]
3315 * Mark the given range as handled by a subordinate map.
3317 * This range must have been created with vm_map_find using
3318 * the vm_submap_object, and no other operations may have been
3319 * performed on this range prior to calling vm_map_submap.
3321 * Only a limited number of operations can be performed
3322 * within this rage after calling vm_map_submap:
3324 * [Don't try vm_map_copyin!]
3326 * To remove a submapping, one must first remove the
3327 * range from the superior map, and then destroy the
3328 * submap (if desired). [Better yet, don't try it.]
3333 vm_map_offset_t start
,
3334 vm_map_offset_t end
,
3336 vm_map_offset_t offset
,
3337 #ifdef NO_NESTED_PMAP
3339 #endif /* NO_NESTED_PMAP */
3342 vm_map_entry_t entry
;
3343 register kern_return_t result
= KERN_INVALID_ARGUMENT
;
3344 register vm_object_t object
;
3348 if (! vm_map_lookup_entry(map
, start
, &entry
)) {
3349 entry
= entry
->vme_next
;
3352 if (entry
== vm_map_to_entry(map
) ||
3353 entry
->is_sub_map
) {
3355 return KERN_INVALID_ARGUMENT
;
3358 assert(!entry
->use_pmap
); /* we don't want to unnest anything here */
3359 vm_map_clip_start(map
, entry
, start
);
3360 vm_map_clip_end(map
, entry
, end
);
3362 if ((entry
->vme_start
== start
) && (entry
->vme_end
== end
) &&
3363 (!entry
->is_sub_map
) &&
3364 ((object
= entry
->object
.vm_object
) == vm_submap_object
) &&
3365 (object
->resident_page_count
== 0) &&
3366 (object
->copy
== VM_OBJECT_NULL
) &&
3367 (object
->shadow
== VM_OBJECT_NULL
) &&
3368 (!object
->pager_created
)) {
3369 entry
->offset
= (vm_object_offset_t
)offset
;
3370 entry
->object
.vm_object
= VM_OBJECT_NULL
;
3371 vm_object_deallocate(object
);
3372 entry
->is_sub_map
= TRUE
;
3373 entry
->object
.sub_map
= submap
;
3374 vm_map_reference(submap
);
3375 submap
->mapped
= TRUE
;
3377 #ifndef NO_NESTED_PMAP
3379 /* nest if platform code will allow */
3380 if(submap
->pmap
== NULL
) {
3381 submap
->pmap
= pmap_create((vm_map_size_t
) 0, FALSE
);
3382 if(submap
->pmap
== PMAP_NULL
) {
3384 return(KERN_NO_SPACE
);
3387 result
= pmap_nest(map
->pmap
,
3388 (entry
->object
.sub_map
)->pmap
,
3391 (uint64_t)(end
- start
));
3393 panic("vm_map_submap: pmap_nest failed, rc = %08X\n", result
);
3394 entry
->use_pmap
= TRUE
;
3396 #else /* NO_NESTED_PMAP */
3397 pmap_remove(map
->pmap
, (addr64_t
)start
, (addr64_t
)end
);
3398 #endif /* NO_NESTED_PMAP */
3399 result
= KERN_SUCCESS
;
3409 * Sets the protection of the specified address
3410 * region in the target map. If "set_max" is
3411 * specified, the maximum protection is to be set;
3412 * otherwise, only the current protection is affected.
3416 register vm_map_t map
,
3417 register vm_map_offset_t start
,
3418 register vm_map_offset_t end
,
3419 register vm_prot_t new_prot
,
3420 register boolean_t set_max
)
3422 register vm_map_entry_t current
;
3423 register vm_map_offset_t prev
;
3424 vm_map_entry_t entry
;
3428 "vm_map_protect, 0x%X start 0x%X end 0x%X, new 0x%X %d",
3429 map
, start
, end
, new_prot
, set_max
);
3433 /* LP64todo - remove this check when vm_map_commpage64()
3434 * no longer has to stuff in a map_entry for the commpage
3435 * above the map's max_offset.
3437 if (start
>= map
->max_offset
) {
3439 return(KERN_INVALID_ADDRESS
);
3444 * Lookup the entry. If it doesn't start in a valid
3445 * entry, return an error.
3447 if (! vm_map_lookup_entry(map
, start
, &entry
)) {
3449 return(KERN_INVALID_ADDRESS
);
3452 if (entry
->superpage_size
&& (start
& (SUPERPAGE_SIZE
-1))) { /* extend request to whole entry */
3453 start
= SUPERPAGE_ROUND_DOWN(start
);
3458 if (entry
->superpage_size
)
3459 end
= SUPERPAGE_ROUND_UP(end
);
3462 * Make a first pass to check for protection and address
3467 prev
= current
->vme_start
;
3468 while ((current
!= vm_map_to_entry(map
)) &&
3469 (current
->vme_start
< end
)) {
3472 * If there is a hole, return an error.
3474 if (current
->vme_start
!= prev
) {
3476 return(KERN_INVALID_ADDRESS
);
3479 new_max
= current
->max_protection
;
3480 if(new_prot
& VM_PROT_COPY
) {
3481 new_max
|= VM_PROT_WRITE
;
3482 if ((new_prot
& (new_max
| VM_PROT_COPY
)) != new_prot
) {
3484 return(KERN_PROTECTION_FAILURE
);
3487 if ((new_prot
& new_max
) != new_prot
) {
3489 return(KERN_PROTECTION_FAILURE
);
3494 if (new_prot
& VM_PROT_WRITE
) {
3495 if (new_prot
& VM_PROT_EXECUTE
) {
3496 printf("EMBEDDED: %s can't have both write and exec at the same time\n", __FUNCTION__
);
3497 new_prot
&= ~VM_PROT_EXECUTE
;
3502 prev
= current
->vme_end
;
3503 current
= current
->vme_next
;
3507 return(KERN_INVALID_ADDRESS
);
3511 * Go back and fix up protections.
3512 * Clip to start here if the range starts within
3517 if (current
!= vm_map_to_entry(map
)) {
3518 /* clip and unnest if necessary */
3519 vm_map_clip_start(map
, current
, start
);
3522 while ((current
!= vm_map_to_entry(map
)) &&
3523 (current
->vme_start
< end
)) {
3527 vm_map_clip_end(map
, current
, end
);
3529 assert(!current
->use_pmap
); /* clipping did unnest if needed */
3531 old_prot
= current
->protection
;
3533 if(new_prot
& VM_PROT_COPY
) {
3534 /* caller is asking specifically to copy the */
3535 /* mapped data, this implies that max protection */
3536 /* will include write. Caller must be prepared */
3537 /* for loss of shared memory communication in the */
3538 /* target area after taking this step */
3539 current
->needs_copy
= TRUE
;
3540 current
->max_protection
|= VM_PROT_WRITE
;
3544 current
->protection
=
3545 (current
->max_protection
=
3546 new_prot
& ~VM_PROT_COPY
) &
3549 current
->protection
= new_prot
& ~VM_PROT_COPY
;
3552 * Update physical map if necessary.
3553 * If the request is to turn off write protection,
3554 * we won't do it for real (in pmap). This is because
3555 * it would cause copy-on-write to fail. We've already
3556 * set, the new protection in the map, so if a
3557 * write-protect fault occurred, it will be fixed up
3558 * properly, COW or not.
3560 if (current
->protection
!= old_prot
) {
3561 /* Look one level in we support nested pmaps */
3562 /* from mapped submaps which are direct entries */
3567 prot
= current
->protection
& ~VM_PROT_WRITE
;
3569 if (override_nx(map
, current
->alias
) && prot
)
3570 prot
|= VM_PROT_EXECUTE
;
3572 if (current
->is_sub_map
&& current
->use_pmap
) {
3573 pmap_protect(current
->object
.sub_map
->pmap
,
3578 pmap_protect(map
->pmap
,
3584 current
= current
->vme_next
;
3588 while ((current
!= vm_map_to_entry(map
)) &&
3589 (current
->vme_start
<= end
)) {
3590 vm_map_simplify_entry(map
, current
);
3591 current
= current
->vme_next
;
3595 return(KERN_SUCCESS
);
3601 * Sets the inheritance of the specified address
3602 * range in the target map. Inheritance
3603 * affects how the map will be shared with
3604 * child maps at the time of vm_map_fork.
3608 register vm_map_t map
,
3609 register vm_map_offset_t start
,
3610 register vm_map_offset_t end
,
3611 register vm_inherit_t new_inheritance
)
3613 register vm_map_entry_t entry
;
3614 vm_map_entry_t temp_entry
;
3618 VM_MAP_RANGE_CHECK(map
, start
, end
);
3620 if (vm_map_lookup_entry(map
, start
, &temp_entry
)) {
3624 temp_entry
= temp_entry
->vme_next
;
3628 /* first check entire range for submaps which can't support the */
3629 /* given inheritance. */
3630 while ((entry
!= vm_map_to_entry(map
)) && (entry
->vme_start
< end
)) {
3631 if(entry
->is_sub_map
) {
3632 if(new_inheritance
== VM_INHERIT_COPY
) {
3634 return(KERN_INVALID_ARGUMENT
);
3638 entry
= entry
->vme_next
;
3642 if (entry
!= vm_map_to_entry(map
)) {
3643 /* clip and unnest if necessary */
3644 vm_map_clip_start(map
, entry
, start
);
3647 while ((entry
!= vm_map_to_entry(map
)) && (entry
->vme_start
< end
)) {
3648 vm_map_clip_end(map
, entry
, end
);
3649 assert(!entry
->use_pmap
); /* clip did unnest if needed */
3651 entry
->inheritance
= new_inheritance
;
3653 entry
= entry
->vme_next
;
3657 return(KERN_SUCCESS
);
3661 * Update the accounting for the amount of wired memory in this map. If the user has
3662 * exceeded the defined limits, then we fail. Wiring on behalf of the kernel never fails.
3665 static kern_return_t
3668 vm_map_entry_t entry
,
3669 boolean_t user_wire
)
3676 * We're wiring memory at the request of the user. Check if this is the first time the user is wiring
3680 if (entry
->user_wired_count
== 0) {
3681 size
= entry
->vme_end
- entry
->vme_start
;
3684 * Since this is the first time the user is wiring this map entry, check to see if we're
3685 * exceeding the user wire limits. There is a per map limit which is the smaller of either
3686 * the process's rlimit or the global vm_user_wire_limit which caps this value. There is also
3687 * a system-wide limit on the amount of memory all users can wire. If the user is over either
3688 * limit, then we fail.
3691 if(size
+ map
->user_wire_size
> MIN(map
->user_wire_limit
, vm_user_wire_limit
) ||
3692 size
+ ptoa_64(vm_page_wire_count
) > vm_global_user_wire_limit
||
3693 size
+ ptoa_64(vm_page_wire_count
) > max_mem
- vm_global_no_user_wire_amount
)
3694 return KERN_RESOURCE_SHORTAGE
;
3697 * The first time the user wires an entry, we also increment the wired_count and add this to
3698 * the total that has been wired in the map.
3701 if (entry
->wired_count
>= MAX_WIRE_COUNT
)
3702 return KERN_FAILURE
;
3704 entry
->wired_count
++;
3705 map
->user_wire_size
+= size
;
3708 if (entry
->user_wired_count
>= MAX_WIRE_COUNT
)
3709 return KERN_FAILURE
;
3711 entry
->user_wired_count
++;
3716 * The kernel's wiring the memory. Just bump the count and continue.
3719 if (entry
->wired_count
>= MAX_WIRE_COUNT
)
3720 panic("vm_map_wire: too many wirings");
3722 entry
->wired_count
++;
3725 return KERN_SUCCESS
;
3729 * Update the memory wiring accounting now that the given map entry is being unwired.
3733 subtract_wire_counts(
3735 vm_map_entry_t entry
,
3736 boolean_t user_wire
)
3742 * We're unwiring memory at the request of the user. See if we're removing the last user wire reference.
3745 if (entry
->user_wired_count
== 1) {
3748 * We're removing the last user wire reference. Decrement the wired_count and the total
3749 * user wired memory for this map.
3752 assert(entry
->wired_count
>= 1);
3753 entry
->wired_count
--;
3754 map
->user_wire_size
-= entry
->vme_end
- entry
->vme_start
;
3757 assert(entry
->user_wired_count
>= 1);
3758 entry
->user_wired_count
--;
3763 * The kernel is unwiring the memory. Just update the count.
3766 assert(entry
->wired_count
>= 1);
3767 entry
->wired_count
--;
3774 * Sets the pageability of the specified address range in the
3775 * target map as wired. Regions specified as not pageable require
3776 * locked-down physical memory and physical page maps. The
3777 * access_type variable indicates types of accesses that must not
3778 * generate page faults. This is checked against protection of
3779 * memory being locked-down.
3781 * The map must not be locked, but a reference must remain to the
3782 * map throughout the call.
3784 static kern_return_t
3786 register vm_map_t map
,
3787 register vm_map_offset_t start
,
3788 register vm_map_offset_t end
,
3789 register vm_prot_t access_type
,
3790 boolean_t user_wire
,
3792 vm_map_offset_t pmap_addr
)
3794 register vm_map_entry_t entry
;
3795 struct vm_map_entry
*first_entry
, tmp_entry
;
3797 register vm_map_offset_t s
,e
;
3799 boolean_t need_wakeup
;
3800 boolean_t main_map
= FALSE
;
3801 wait_interrupt_t interruptible_state
;
3802 thread_t cur_thread
;
3803 unsigned int last_timestamp
;
3807 if(map_pmap
== NULL
)
3809 last_timestamp
= map
->timestamp
;
3811 VM_MAP_RANGE_CHECK(map
, start
, end
);
3812 assert(page_aligned(start
));
3813 assert(page_aligned(end
));
3815 /* We wired what the caller asked for, zero pages */
3817 return KERN_SUCCESS
;
3820 need_wakeup
= FALSE
;
3821 cur_thread
= current_thread();
3826 if (vm_map_lookup_entry(map
, s
, &first_entry
)) {
3827 entry
= first_entry
;
3829 * vm_map_clip_start will be done later.
3830 * We don't want to unnest any nested submaps here !
3833 /* Start address is not in map */
3834 rc
= KERN_INVALID_ADDRESS
;
3838 while ((entry
!= vm_map_to_entry(map
)) && (s
< end
)) {
3840 * At this point, we have wired from "start" to "s".
3841 * We still need to wire from "s" to "end".
3843 * "entry" hasn't been clipped, so it could start before "s"
3844 * and/or end after "end".
3847 /* "e" is how far we want to wire in this entry */
3853 * If another thread is wiring/unwiring this entry then
3854 * block after informing other thread to wake us up.
3856 if (entry
->in_transition
) {
3857 wait_result_t wait_result
;
3860 * We have not clipped the entry. Make sure that
3861 * the start address is in range so that the lookup
3862 * below will succeed.
3863 * "s" is the current starting point: we've already
3864 * wired from "start" to "s" and we still have
3865 * to wire from "s" to "end".
3868 entry
->needs_wakeup
= TRUE
;
3871 * wake up anybody waiting on entries that we have
3875 vm_map_entry_wakeup(map
);
3876 need_wakeup
= FALSE
;
3879 * User wiring is interruptible
3881 wait_result
= vm_map_entry_wait(map
,
3882 (user_wire
) ? THREAD_ABORTSAFE
:
3884 if (user_wire
&& wait_result
== THREAD_INTERRUPTED
) {
3886 * undo the wirings we have done so far
3887 * We do not clear the needs_wakeup flag,
3888 * because we cannot tell if we were the
3896 * Cannot avoid a lookup here. reset timestamp.
3898 last_timestamp
= map
->timestamp
;
3901 * The entry could have been clipped, look it up again.
3902 * Worse that can happen is, it may not exist anymore.
3904 if (!vm_map_lookup_entry(map
, s
, &first_entry
)) {
3906 panic("vm_map_wire: re-lookup failed");
3909 * User: undo everything upto the previous
3910 * entry. let vm_map_unwire worry about
3911 * checking the validity of the range.
3916 entry
= first_entry
;
3920 if (entry
->is_sub_map
) {
3921 vm_map_offset_t sub_start
;
3922 vm_map_offset_t sub_end
;
3923 vm_map_offset_t local_start
;
3924 vm_map_offset_t local_end
;
3927 vm_map_clip_start(map
, entry
, s
);
3928 vm_map_clip_end(map
, entry
, end
);
3930 sub_start
= entry
->offset
;
3931 sub_end
= entry
->vme_end
;
3932 sub_end
+= entry
->offset
- entry
->vme_start
;
3934 local_end
= entry
->vme_end
;
3935 if(map_pmap
== NULL
) {
3937 vm_object_offset_t offset
;
3940 vm_map_entry_t local_entry
;
3941 vm_map_version_t version
;
3942 vm_map_t lookup_map
;
3944 if(entry
->use_pmap
) {
3945 pmap
= entry
->object
.sub_map
->pmap
;
3946 /* ppc implementation requires that */
3947 /* submaps pmap address ranges line */
3948 /* up with parent map */
3950 pmap_addr
= sub_start
;
3958 if (entry
->wired_count
) {
3959 if ((rc
= add_wire_counts(map
, entry
, user_wire
)) != KERN_SUCCESS
)
3963 * The map was not unlocked:
3964 * no need to goto re-lookup.
3965 * Just go directly to next entry.
3967 entry
= entry
->vme_next
;
3968 s
= entry
->vme_start
;
3973 /* call vm_map_lookup_locked to */
3974 /* cause any needs copy to be */
3976 local_start
= entry
->vme_start
;
3978 vm_map_lock_write_to_read(map
);
3979 if(vm_map_lookup_locked(
3980 &lookup_map
, local_start
,
3982 OBJECT_LOCK_EXCLUSIVE
,
3984 &offset
, &prot
, &wired
,
3988 vm_map_unlock_read(lookup_map
);
3989 vm_map_unwire(map
, start
,
3991 return(KERN_FAILURE
);
3993 if(real_map
!= lookup_map
)
3994 vm_map_unlock(real_map
);
3995 vm_map_unlock_read(lookup_map
);
3997 vm_object_unlock(object
);
3999 /* we unlocked, so must re-lookup */
4000 if (!vm_map_lookup_entry(map
,
4008 * entry could have been "simplified",
4011 entry
= local_entry
;
4012 assert(s
== local_start
);
4013 vm_map_clip_start(map
, entry
, s
);
4014 vm_map_clip_end(map
, entry
, end
);
4015 /* re-compute "e" */
4020 /* did we have a change of type? */
4021 if (!entry
->is_sub_map
) {
4022 last_timestamp
= map
->timestamp
;
4026 local_start
= entry
->vme_start
;
4030 if ((rc
= add_wire_counts(map
, entry
, user_wire
)) != KERN_SUCCESS
)
4033 entry
->in_transition
= TRUE
;
4036 rc
= vm_map_wire_nested(entry
->object
.sub_map
,
4039 user_wire
, pmap
, pmap_addr
);
4043 * Find the entry again. It could have been clipped
4044 * after we unlocked the map.
4046 if (!vm_map_lookup_entry(map
, local_start
,
4048 panic("vm_map_wire: re-lookup failed");
4049 entry
= first_entry
;
4051 assert(local_start
== s
);
4052 /* re-compute "e" */
4057 last_timestamp
= map
->timestamp
;
4058 while ((entry
!= vm_map_to_entry(map
)) &&
4059 (entry
->vme_start
< e
)) {
4060 assert(entry
->in_transition
);
4061 entry
->in_transition
= FALSE
;
4062 if (entry
->needs_wakeup
) {
4063 entry
->needs_wakeup
= FALSE
;
4066 if (rc
!= KERN_SUCCESS
) {/* from vm_*_wire */
4067 subtract_wire_counts(map
, entry
, user_wire
);
4069 entry
= entry
->vme_next
;
4071 if (rc
!= KERN_SUCCESS
) { /* from vm_*_wire */
4075 /* no need to relookup again */
4076 s
= entry
->vme_start
;
4081 * If this entry is already wired then increment
4082 * the appropriate wire reference count.
4084 if (entry
->wired_count
) {
4086 * entry is already wired down, get our reference
4087 * after clipping to our range.
4089 vm_map_clip_start(map
, entry
, s
);
4090 vm_map_clip_end(map
, entry
, end
);
4092 if ((rc
= add_wire_counts(map
, entry
, user_wire
)) != KERN_SUCCESS
)
4095 /* map was not unlocked: no need to relookup */
4096 entry
= entry
->vme_next
;
4097 s
= entry
->vme_start
;
4102 * Unwired entry or wire request transmitted via submap
4107 * Perform actions of vm_map_lookup that need the write
4108 * lock on the map: create a shadow object for a
4109 * copy-on-write region, or an object for a zero-fill
4112 size
= entry
->vme_end
- entry
->vme_start
;
4114 * If wiring a copy-on-write page, we need to copy it now
4115 * even if we're only (currently) requesting read access.
4116 * This is aggressive, but once it's wired we can't move it.
4118 if (entry
->needs_copy
) {
4119 vm_object_shadow(&entry
->object
.vm_object
,
4120 &entry
->offset
, size
);
4121 entry
->needs_copy
= FALSE
;
4122 } else if (entry
->object
.vm_object
== VM_OBJECT_NULL
) {
4123 entry
->object
.vm_object
= vm_object_allocate(size
);
4124 entry
->offset
= (vm_object_offset_t
)0;
4127 vm_map_clip_start(map
, entry
, s
);
4128 vm_map_clip_end(map
, entry
, end
);
4130 /* re-compute "e" */
4136 * Check for holes and protection mismatch.
4137 * Holes: Next entry should be contiguous unless this
4138 * is the end of the region.
4139 * Protection: Access requested must be allowed, unless
4140 * wiring is by protection class
4142 if ((entry
->vme_end
< end
) &&
4143 ((entry
->vme_next
== vm_map_to_entry(map
)) ||
4144 (entry
->vme_next
->vme_start
> entry
->vme_end
))) {
4146 rc
= KERN_INVALID_ADDRESS
;
4149 if ((entry
->protection
& access_type
) != access_type
) {
4150 /* found a protection problem */
4151 rc
= KERN_PROTECTION_FAILURE
;
4155 assert(entry
->wired_count
== 0 && entry
->user_wired_count
== 0);
4157 if ((rc
= add_wire_counts(map
, entry
, user_wire
)) != KERN_SUCCESS
)
4160 entry
->in_transition
= TRUE
;
4163 * This entry might get split once we unlock the map.
4164 * In vm_fault_wire(), we need the current range as
4165 * defined by this entry. In order for this to work
4166 * along with a simultaneous clip operation, we make a
4167 * temporary copy of this entry and use that for the
4168 * wiring. Note that the underlying objects do not
4169 * change during a clip.
4174 * The in_transition state guarentees that the entry
4175 * (or entries for this range, if split occured) will be
4176 * there when the map lock is acquired for the second time.
4180 if (!user_wire
&& cur_thread
!= THREAD_NULL
)
4181 interruptible_state
= thread_interrupt_level(THREAD_UNINT
);
4183 interruptible_state
= THREAD_UNINT
;
4186 rc
= vm_fault_wire(map
,
4187 &tmp_entry
, map_pmap
, pmap_addr
);
4189 rc
= vm_fault_wire(map
,
4190 &tmp_entry
, map
->pmap
,
4191 tmp_entry
.vme_start
);
4193 if (!user_wire
&& cur_thread
!= THREAD_NULL
)
4194 thread_interrupt_level(interruptible_state
);
4198 if (last_timestamp
+1 != map
->timestamp
) {
4200 * Find the entry again. It could have been clipped
4201 * after we unlocked the map.
4203 if (!vm_map_lookup_entry(map
, tmp_entry
.vme_start
,
4205 panic("vm_map_wire: re-lookup failed");
4207 entry
= first_entry
;
4210 last_timestamp
= map
->timestamp
;
4212 while ((entry
!= vm_map_to_entry(map
)) &&
4213 (entry
->vme_start
< tmp_entry
.vme_end
)) {
4214 assert(entry
->in_transition
);
4215 entry
->in_transition
= FALSE
;
4216 if (entry
->needs_wakeup
) {
4217 entry
->needs_wakeup
= FALSE
;
4220 if (rc
!= KERN_SUCCESS
) { /* from vm_*_wire */
4221 subtract_wire_counts(map
, entry
, user_wire
);
4223 entry
= entry
->vme_next
;
4226 if (rc
!= KERN_SUCCESS
) { /* from vm_*_wire */
4230 s
= entry
->vme_start
;
4231 } /* end while loop through map entries */
4234 if (rc
== KERN_SUCCESS
) {
4235 /* repair any damage we may have made to the VM map */
4236 vm_map_simplify_range(map
, start
, end
);
4242 * wake up anybody waiting on entries we wired.
4245 vm_map_entry_wakeup(map
);
4247 if (rc
!= KERN_SUCCESS
) {
4248 /* undo what has been wired so far */
4249 vm_map_unwire(map
, start
, s
, user_wire
);
4258 register vm_map_t map
,
4259 register vm_map_offset_t start
,
4260 register vm_map_offset_t end
,
4261 register vm_prot_t access_type
,
4262 boolean_t user_wire
)
4269 * the calls to mapping_prealloc and mapping_relpre
4270 * (along with the VM_MAP_RANGE_CHECK to insure a
4271 * resonable range was passed in) are
4272 * currently necessary because
4273 * we haven't enabled kernel pre-emption
4274 * and/or the pmap_enter cannot purge and re-use
4277 VM_MAP_RANGE_CHECK(map
, start
, end
);
4278 assert((unsigned int) (end
- start
) == (end
- start
));
4279 mapping_prealloc((unsigned int) (end
- start
));
4281 kret
= vm_map_wire_nested(map
, start
, end
, access_type
,
4282 user_wire
, (pmap_t
)NULL
, 0);
4292 * Sets the pageability of the specified address range in the target
4293 * as pageable. Regions specified must have been wired previously.
4295 * The map must not be locked, but a reference must remain to the map
4296 * throughout the call.
4298 * Kernel will panic on failures. User unwire ignores holes and
4299 * unwired and intransition entries to avoid losing memory by leaving
4302 static kern_return_t
4303 vm_map_unwire_nested(
4304 register vm_map_t map
,
4305 register vm_map_offset_t start
,
4306 register vm_map_offset_t end
,
4307 boolean_t user_wire
,
4309 vm_map_offset_t pmap_addr
)
4311 register vm_map_entry_t entry
;
4312 struct vm_map_entry
*first_entry
, tmp_entry
;
4313 boolean_t need_wakeup
;
4314 boolean_t main_map
= FALSE
;
4315 unsigned int last_timestamp
;
4318 if(map_pmap
== NULL
)
4320 last_timestamp
= map
->timestamp
;
4322 VM_MAP_RANGE_CHECK(map
, start
, end
);
4323 assert(page_aligned(start
));
4324 assert(page_aligned(end
));
4327 /* We unwired what the caller asked for: zero pages */
4329 return KERN_SUCCESS
;
4332 if (vm_map_lookup_entry(map
, start
, &first_entry
)) {
4333 entry
= first_entry
;
4335 * vm_map_clip_start will be done later.
4336 * We don't want to unnest any nested sub maps here !
4341 panic("vm_map_unwire: start not found");
4343 /* Start address is not in map. */
4345 return(KERN_INVALID_ADDRESS
);
4348 if (entry
->superpage_size
) {
4349 /* superpages are always wired */
4351 return KERN_INVALID_ADDRESS
;
4354 need_wakeup
= FALSE
;
4355 while ((entry
!= vm_map_to_entry(map
)) && (entry
->vme_start
< end
)) {
4356 if (entry
->in_transition
) {
4359 * Another thread is wiring down this entry. Note
4360 * that if it is not for the other thread we would
4361 * be unwiring an unwired entry. This is not
4362 * permitted. If we wait, we will be unwiring memory
4366 * Another thread is unwiring this entry. We did not
4367 * have a reference to it, because if we did, this
4368 * entry will not be getting unwired now.
4373 * This could happen: there could be some
4374 * overlapping vslock/vsunlock operations
4376 * We should probably just wait and retry,
4377 * but then we have to be careful that this
4378 * entry could get "simplified" after
4379 * "in_transition" gets unset and before
4380 * we re-lookup the entry, so we would
4381 * have to re-clip the entry to avoid
4382 * re-unwiring what we have already unwired...
4383 * See vm_map_wire_nested().
4385 * Or we could just ignore "in_transition"
4386 * here and proceed to decement the wired
4387 * count(s) on this entry. That should be fine
4388 * as long as "wired_count" doesn't drop all
4389 * the way to 0 (and we should panic if THAT
4392 panic("vm_map_unwire: in_transition entry");
4395 entry
= entry
->vme_next
;
4399 if (entry
->is_sub_map
) {
4400 vm_map_offset_t sub_start
;
4401 vm_map_offset_t sub_end
;
4402 vm_map_offset_t local_end
;
4405 vm_map_clip_start(map
, entry
, start
);
4406 vm_map_clip_end(map
, entry
, end
);
4408 sub_start
= entry
->offset
;
4409 sub_end
= entry
->vme_end
- entry
->vme_start
;
4410 sub_end
+= entry
->offset
;
4411 local_end
= entry
->vme_end
;
4412 if(map_pmap
== NULL
) {
4413 if(entry
->use_pmap
) {
4414 pmap
= entry
->object
.sub_map
->pmap
;
4415 pmap_addr
= sub_start
;
4420 if (entry
->wired_count
== 0 ||
4421 (user_wire
&& entry
->user_wired_count
== 0)) {
4423 panic("vm_map_unwire: entry is unwired");
4424 entry
= entry
->vme_next
;
4430 * Holes: Next entry should be contiguous unless
4431 * this is the end of the region.
4433 if (((entry
->vme_end
< end
) &&
4434 ((entry
->vme_next
== vm_map_to_entry(map
)) ||
4435 (entry
->vme_next
->vme_start
4436 > entry
->vme_end
)))) {
4438 panic("vm_map_unwire: non-contiguous region");
4440 entry = entry->vme_next;
4445 subtract_wire_counts(map
, entry
, user_wire
);
4447 if (entry
->wired_count
!= 0) {
4448 entry
= entry
->vme_next
;
4452 entry
->in_transition
= TRUE
;
4453 tmp_entry
= *entry
;/* see comment in vm_map_wire() */
4456 * We can unlock the map now. The in_transition state
4457 * guarantees existance of the entry.
4460 vm_map_unwire_nested(entry
->object
.sub_map
,
4461 sub_start
, sub_end
, user_wire
, pmap
, pmap_addr
);
4464 if (last_timestamp
+1 != map
->timestamp
) {
4466 * Find the entry again. It could have been
4467 * clipped or deleted after we unlocked the map.
4469 if (!vm_map_lookup_entry(map
,
4470 tmp_entry
.vme_start
,
4473 panic("vm_map_unwire: re-lookup failed");
4474 entry
= first_entry
->vme_next
;
4476 entry
= first_entry
;
4478 last_timestamp
= map
->timestamp
;
4481 * clear transition bit for all constituent entries
4482 * that were in the original entry (saved in
4483 * tmp_entry). Also check for waiters.
4485 while ((entry
!= vm_map_to_entry(map
)) &&
4486 (entry
->vme_start
< tmp_entry
.vme_end
)) {
4487 assert(entry
->in_transition
);
4488 entry
->in_transition
= FALSE
;
4489 if (entry
->needs_wakeup
) {
4490 entry
->needs_wakeup
= FALSE
;
4493 entry
= entry
->vme_next
;
4498 vm_map_unwire_nested(entry
->object
.sub_map
,
4499 sub_start
, sub_end
, user_wire
, map_pmap
,
4503 if (last_timestamp
+1 != map
->timestamp
) {
4505 * Find the entry again. It could have been
4506 * clipped or deleted after we unlocked the map.
4508 if (!vm_map_lookup_entry(map
,
4509 tmp_entry
.vme_start
,
4512 panic("vm_map_unwire: re-lookup failed");
4513 entry
= first_entry
->vme_next
;
4515 entry
= first_entry
;
4517 last_timestamp
= map
->timestamp
;
4522 if ((entry
->wired_count
== 0) ||
4523 (user_wire
&& entry
->user_wired_count
== 0)) {
4525 panic("vm_map_unwire: entry is unwired");
4527 entry
= entry
->vme_next
;
4531 assert(entry
->wired_count
> 0 &&
4532 (!user_wire
|| entry
->user_wired_count
> 0));
4534 vm_map_clip_start(map
, entry
, start
);
4535 vm_map_clip_end(map
, entry
, end
);
4539 * Holes: Next entry should be contiguous unless
4540 * this is the end of the region.
4542 if (((entry
->vme_end
< end
) &&
4543 ((entry
->vme_next
== vm_map_to_entry(map
)) ||
4544 (entry
->vme_next
->vme_start
> entry
->vme_end
)))) {
4547 panic("vm_map_unwire: non-contiguous region");
4548 entry
= entry
->vme_next
;
4552 subtract_wire_counts(map
, entry
, user_wire
);
4554 if (entry
->wired_count
!= 0) {
4555 entry
= entry
->vme_next
;
4559 if(entry
->zero_wired_pages
) {
4560 entry
->zero_wired_pages
= FALSE
;
4563 entry
->in_transition
= TRUE
;
4564 tmp_entry
= *entry
; /* see comment in vm_map_wire() */
4567 * We can unlock the map now. The in_transition state
4568 * guarantees existance of the entry.
4572 vm_fault_unwire(map
,
4573 &tmp_entry
, FALSE
, map_pmap
, pmap_addr
);
4575 vm_fault_unwire(map
,
4576 &tmp_entry
, FALSE
, map
->pmap
,
4577 tmp_entry
.vme_start
);
4581 if (last_timestamp
+1 != map
->timestamp
) {
4583 * Find the entry again. It could have been clipped
4584 * or deleted after we unlocked the map.
4586 if (!vm_map_lookup_entry(map
, tmp_entry
.vme_start
,
4589 panic("vm_map_unwire: re-lookup failed");
4590 entry
= first_entry
->vme_next
;
4592 entry
= first_entry
;
4594 last_timestamp
= map
->timestamp
;
4597 * clear transition bit for all constituent entries that
4598 * were in the original entry (saved in tmp_entry). Also
4599 * check for waiters.
4601 while ((entry
!= vm_map_to_entry(map
)) &&
4602 (entry
->vme_start
< tmp_entry
.vme_end
)) {
4603 assert(entry
->in_transition
);
4604 entry
->in_transition
= FALSE
;
4605 if (entry
->needs_wakeup
) {
4606 entry
->needs_wakeup
= FALSE
;
4609 entry
= entry
->vme_next
;
4614 * We might have fragmented the address space when we wired this
4615 * range of addresses. Attempt to re-coalesce these VM map entries
4616 * with their neighbors now that they're no longer wired.
4617 * Under some circumstances, address space fragmentation can
4618 * prevent VM object shadow chain collapsing, which can cause
4621 vm_map_simplify_range(map
, start
, end
);
4625 * wake up anybody waiting on entries that we have unwired.
4628 vm_map_entry_wakeup(map
);
4629 return(KERN_SUCCESS
);
4635 register vm_map_t map
,
4636 register vm_map_offset_t start
,
4637 register vm_map_offset_t end
,
4638 boolean_t user_wire
)
4640 return vm_map_unwire_nested(map
, start
, end
,
4641 user_wire
, (pmap_t
)NULL
, 0);
4646 * vm_map_entry_delete: [ internal use only ]
4648 * Deallocate the given entry from the target map.
4651 vm_map_entry_delete(
4652 register vm_map_t map
,
4653 register vm_map_entry_t entry
)
4655 register vm_map_offset_t s
, e
;
4656 register vm_object_t object
;
4657 register vm_map_t submap
;
4659 s
= entry
->vme_start
;
4661 assert(page_aligned(s
));
4662 assert(page_aligned(e
));
4663 assert(entry
->wired_count
== 0);
4664 assert(entry
->user_wired_count
== 0);
4665 assert(!entry
->permanent
);
4667 if (entry
->is_sub_map
) {
4669 submap
= entry
->object
.sub_map
;
4672 object
= entry
->object
.vm_object
;
4675 vm_map_entry_unlink(map
, entry
);
4678 vm_map_entry_dispose(map
, entry
);
4682 * Deallocate the object only after removing all
4683 * pmap entries pointing to its pages.
4686 vm_map_deallocate(submap
);
4688 vm_object_deallocate(object
);
4693 vm_map_submap_pmap_clean(
4695 vm_map_offset_t start
,
4696 vm_map_offset_t end
,
4698 vm_map_offset_t offset
)
4700 vm_map_offset_t submap_start
;
4701 vm_map_offset_t submap_end
;
4702 vm_map_size_t remove_size
;
4703 vm_map_entry_t entry
;
4705 submap_end
= offset
+ (end
- start
);
4706 submap_start
= offset
;
4708 vm_map_lock_read(sub_map
);
4709 if(vm_map_lookup_entry(sub_map
, offset
, &entry
)) {
4711 remove_size
= (entry
->vme_end
- entry
->vme_start
);
4712 if(offset
> entry
->vme_start
)
4713 remove_size
-= offset
- entry
->vme_start
;
4716 if(submap_end
< entry
->vme_end
) {
4718 entry
->vme_end
- submap_end
;
4720 if(entry
->is_sub_map
) {
4721 vm_map_submap_pmap_clean(
4724 start
+ remove_size
,
4725 entry
->object
.sub_map
,
4729 if((map
->mapped
) && (map
->ref_count
)
4730 && (entry
->object
.vm_object
!= NULL
)) {
4731 vm_object_pmap_protect(
4732 entry
->object
.vm_object
,
4739 pmap_remove(map
->pmap
,
4741 (addr64_t
)(start
+ remove_size
));
4746 entry
= entry
->vme_next
;
4748 while((entry
!= vm_map_to_entry(sub_map
))
4749 && (entry
->vme_start
< submap_end
)) {
4750 remove_size
= (entry
->vme_end
- entry
->vme_start
);
4751 if(submap_end
< entry
->vme_end
) {
4752 remove_size
-= entry
->vme_end
- submap_end
;
4754 if(entry
->is_sub_map
) {
4755 vm_map_submap_pmap_clean(
4757 (start
+ entry
->vme_start
) - offset
,
4758 ((start
+ entry
->vme_start
) - offset
) + remove_size
,
4759 entry
->object
.sub_map
,
4762 if((map
->mapped
) && (map
->ref_count
)
4763 && (entry
->object
.vm_object
!= NULL
)) {
4764 vm_object_pmap_protect(
4765 entry
->object
.vm_object
,
4772 pmap_remove(map
->pmap
,
4773 (addr64_t
)((start
+ entry
->vme_start
)
4775 (addr64_t
)(((start
+ entry
->vme_start
)
4776 - offset
) + remove_size
));
4779 entry
= entry
->vme_next
;
4781 vm_map_unlock_read(sub_map
);
4786 * vm_map_delete: [ internal use only ]
4788 * Deallocates the given address range from the target map.
4789 * Removes all user wirings. Unwires one kernel wiring if
4790 * VM_MAP_REMOVE_KUNWIRE is set. Waits for kernel wirings to go
4791 * away if VM_MAP_REMOVE_WAIT_FOR_KWIRE is set. Sleeps
4792 * interruptibly if VM_MAP_REMOVE_INTERRUPTIBLE is set.
4794 * This routine is called with map locked and leaves map locked.
4796 static kern_return_t
4799 vm_map_offset_t start
,
4800 vm_map_offset_t end
,
4804 vm_map_entry_t entry
, next
;
4805 struct vm_map_entry
*first_entry
, tmp_entry
;
4806 register vm_map_offset_t s
;
4807 register vm_object_t object
;
4808 boolean_t need_wakeup
;
4809 unsigned int last_timestamp
= ~0; /* unlikely value */
4812 interruptible
= (flags
& VM_MAP_REMOVE_INTERRUPTIBLE
) ?
4813 THREAD_ABORTSAFE
: THREAD_UNINT
;
4816 * All our DMA I/O operations in IOKit are currently done by
4817 * wiring through the map entries of the task requesting the I/O.
4818 * Because of this, we must always wait for kernel wirings
4819 * to go away on the entries before deleting them.
4821 * Any caller who wants to actually remove a kernel wiring
4822 * should explicitly set the VM_MAP_REMOVE_KUNWIRE flag to
4823 * properly remove one wiring instead of blasting through
4826 flags
|= VM_MAP_REMOVE_WAIT_FOR_KWIRE
;
4830 * Find the start of the region, and clip it
4832 if (vm_map_lookup_entry(map
, start
, &first_entry
)) {
4833 entry
= first_entry
;
4834 if (entry
->superpage_size
&& (start
& ~SUPERPAGE_MASK
)) { /* extend request to whole entry */ start
= SUPERPAGE_ROUND_DOWN(start
);
4835 start
= SUPERPAGE_ROUND_DOWN(start
);
4838 if (start
== entry
->vme_start
) {
4840 * No need to clip. We don't want to cause
4841 * any unnecessary unnesting in this case...
4844 vm_map_clip_start(map
, entry
, start
);
4848 * Fix the lookup hint now, rather than each
4849 * time through the loop.
4851 SAVE_HINT_MAP_WRITE(map
, entry
->vme_prev
);
4853 entry
= first_entry
->vme_next
;
4857 if (entry
->superpage_size
)
4858 end
= SUPERPAGE_ROUND_UP(end
);
4860 need_wakeup
= FALSE
;
4862 * Step through all entries in this region
4864 s
= entry
->vme_start
;
4865 while ((entry
!= vm_map_to_entry(map
)) && (s
< end
)) {
4867 * At this point, we have deleted all the memory entries
4868 * between "start" and "s". We still need to delete
4869 * all memory entries between "s" and "end".
4870 * While we were blocked and the map was unlocked, some
4871 * new memory entries could have been re-allocated between
4872 * "start" and "s" and we don't want to mess with those.
4873 * Some of those entries could even have been re-assembled
4874 * with an entry after "s" (in vm_map_simplify_entry()), so
4875 * we may have to vm_map_clip_start() again.
4878 if (entry
->vme_start
>= s
) {
4880 * This entry starts on or after "s"
4881 * so no need to clip its start.
4885 * This entry has been re-assembled by a
4886 * vm_map_simplify_entry(). We need to
4887 * re-clip its start.
4889 vm_map_clip_start(map
, entry
, s
);
4891 if (entry
->vme_end
<= end
) {
4893 * This entry is going away completely, so no need
4894 * to clip and possibly cause an unnecessary unnesting.
4897 vm_map_clip_end(map
, entry
, end
);
4900 if (entry
->permanent
) {
4901 panic("attempt to remove permanent VM map entry "
4902 "%p [0x%llx:0x%llx]\n",
4903 entry
, (uint64_t) s
, (uint64_t) end
);
4907 if (entry
->in_transition
) {
4908 wait_result_t wait_result
;
4911 * Another thread is wiring/unwiring this entry.
4912 * Let the other thread know we are waiting.
4914 assert(s
== entry
->vme_start
);
4915 entry
->needs_wakeup
= TRUE
;
4918 * wake up anybody waiting on entries that we have
4919 * already unwired/deleted.
4922 vm_map_entry_wakeup(map
);
4923 need_wakeup
= FALSE
;
4926 wait_result
= vm_map_entry_wait(map
, interruptible
);
4928 if (interruptible
&&
4929 wait_result
== THREAD_INTERRUPTED
) {
4931 * We do not clear the needs_wakeup flag,
4932 * since we cannot tell if we were the only one.
4935 return KERN_ABORTED
;
4939 * The entry could have been clipped or it
4940 * may not exist anymore. Look it up again.
4942 if (!vm_map_lookup_entry(map
, s
, &first_entry
)) {
4943 assert((map
!= kernel_map
) &&
4944 (!entry
->is_sub_map
));
4946 * User: use the next entry
4948 entry
= first_entry
->vme_next
;
4949 s
= entry
->vme_start
;
4951 entry
= first_entry
;
4952 SAVE_HINT_MAP_WRITE(map
, entry
->vme_prev
);
4954 last_timestamp
= map
->timestamp
;
4956 } /* end in_transition */
4958 if (entry
->wired_count
) {
4959 boolean_t user_wire
;
4961 user_wire
= entry
->user_wired_count
> 0;
4964 * Remove a kernel wiring if requested
4966 if (flags
& VM_MAP_REMOVE_KUNWIRE
) {
4967 entry
->wired_count
--;
4971 * Remove all user wirings for proper accounting
4973 if (entry
->user_wired_count
> 0) {
4974 while (entry
->user_wired_count
)
4975 subtract_wire_counts(map
, entry
, user_wire
);
4978 if (entry
->wired_count
!= 0) {
4979 assert(map
!= kernel_map
);
4981 * Cannot continue. Typical case is when
4982 * a user thread has physical io pending on
4983 * on this page. Either wait for the
4984 * kernel wiring to go away or return an
4987 if (flags
& VM_MAP_REMOVE_WAIT_FOR_KWIRE
) {
4988 wait_result_t wait_result
;
4990 assert(s
== entry
->vme_start
);
4991 entry
->needs_wakeup
= TRUE
;
4992 wait_result
= vm_map_entry_wait(map
,
4995 if (interruptible
&&
4996 wait_result
== THREAD_INTERRUPTED
) {
4998 * We do not clear the
4999 * needs_wakeup flag, since we
5000 * cannot tell if we were the
5004 return KERN_ABORTED
;
5008 * The entry could have been clipped or
5009 * it may not exist anymore. Look it
5012 if (!vm_map_lookup_entry(map
, s
,
5014 assert(map
!= kernel_map
);
5016 * User: use the next entry
5018 entry
= first_entry
->vme_next
;
5019 s
= entry
->vme_start
;
5021 entry
= first_entry
;
5022 SAVE_HINT_MAP_WRITE(map
, entry
->vme_prev
);
5024 last_timestamp
= map
->timestamp
;
5028 return KERN_FAILURE
;
5032 entry
->in_transition
= TRUE
;
5034 * copy current entry. see comment in vm_map_wire()
5037 assert(s
== entry
->vme_start
);
5040 * We can unlock the map now. The in_transition
5041 * state guarentees existance of the entry.
5045 if (tmp_entry
.is_sub_map
) {
5047 vm_map_offset_t sub_start
, sub_end
;
5049 vm_map_offset_t pmap_addr
;
5052 sub_map
= tmp_entry
.object
.sub_map
;
5053 sub_start
= tmp_entry
.offset
;
5054 sub_end
= sub_start
+ (tmp_entry
.vme_end
-
5055 tmp_entry
.vme_start
);
5056 if (tmp_entry
.use_pmap
) {
5057 pmap
= sub_map
->pmap
;
5058 pmap_addr
= tmp_entry
.vme_start
;
5061 pmap_addr
= tmp_entry
.vme_start
;
5063 (void) vm_map_unwire_nested(sub_map
,
5069 vm_fault_unwire(map
, &tmp_entry
,
5070 tmp_entry
.object
.vm_object
== kernel_object
,
5071 map
->pmap
, tmp_entry
.vme_start
);
5076 if (last_timestamp
+1 != map
->timestamp
) {
5078 * Find the entry again. It could have
5079 * been clipped after we unlocked the map.
5081 if (!vm_map_lookup_entry(map
, s
, &first_entry
)){
5082 assert((map
!= kernel_map
) &&
5083 (!entry
->is_sub_map
));
5084 first_entry
= first_entry
->vme_next
;
5085 s
= first_entry
->vme_start
;
5087 SAVE_HINT_MAP_WRITE(map
, entry
->vme_prev
);
5090 SAVE_HINT_MAP_WRITE(map
, entry
->vme_prev
);
5091 first_entry
= entry
;
5094 last_timestamp
= map
->timestamp
;
5096 entry
= first_entry
;
5097 while ((entry
!= vm_map_to_entry(map
)) &&
5098 (entry
->vme_start
< tmp_entry
.vme_end
)) {
5099 assert(entry
->in_transition
);
5100 entry
->in_transition
= FALSE
;
5101 if (entry
->needs_wakeup
) {
5102 entry
->needs_wakeup
= FALSE
;
5105 entry
= entry
->vme_next
;
5108 * We have unwired the entry(s). Go back and
5111 entry
= first_entry
;
5115 /* entry is unwired */
5116 assert(entry
->wired_count
== 0);
5117 assert(entry
->user_wired_count
== 0);
5119 assert(s
== entry
->vme_start
);
5121 if (flags
& VM_MAP_REMOVE_NO_PMAP_CLEANUP
) {
5123 * XXX with the VM_MAP_REMOVE_SAVE_ENTRIES flag to
5124 * vm_map_delete(), some map entries might have been
5125 * transferred to a "zap_map", which doesn't have a
5126 * pmap. The original pmap has already been flushed
5127 * in the vm_map_delete() call targeting the original
5128 * map, but when we get to destroying the "zap_map",
5129 * we don't have any pmap to flush, so let's just skip
5132 } else if (entry
->is_sub_map
) {
5133 if (entry
->use_pmap
) {
5134 #ifndef NO_NESTED_PMAP
5135 pmap_unnest(map
->pmap
,
5136 (addr64_t
)entry
->vme_start
,
5137 entry
->vme_end
- entry
->vme_start
);
5138 #endif /* NO_NESTED_PMAP */
5139 if ((map
->mapped
) && (map
->ref_count
)) {
5140 /* clean up parent map/maps */
5141 vm_map_submap_pmap_clean(
5142 map
, entry
->vme_start
,
5144 entry
->object
.sub_map
,
5148 vm_map_submap_pmap_clean(
5149 map
, entry
->vme_start
, entry
->vme_end
,
5150 entry
->object
.sub_map
,
5153 } else if (entry
->object
.vm_object
!= kernel_object
) {
5154 object
= entry
->object
.vm_object
;
5155 if((map
->mapped
) && (map
->ref_count
)) {
5156 vm_object_pmap_protect(
5157 object
, entry
->offset
,
5158 entry
->vme_end
- entry
->vme_start
,
5163 pmap_remove(map
->pmap
,
5164 (addr64_t
)entry
->vme_start
,
5165 (addr64_t
)entry
->vme_end
);
5170 * All pmap mappings for this map entry must have been
5173 assert(vm_map_pmap_is_empty(map
,
5177 next
= entry
->vme_next
;
5178 s
= next
->vme_start
;
5179 last_timestamp
= map
->timestamp
;
5181 if ((flags
& VM_MAP_REMOVE_SAVE_ENTRIES
) &&
5182 zap_map
!= VM_MAP_NULL
) {
5183 vm_map_size_t entry_size
;
5185 * The caller wants to save the affected VM map entries
5186 * into the "zap_map". The caller will take care of
5189 /* unlink the entry from "map" ... */
5190 vm_map_entry_unlink(map
, entry
);
5191 /* ... and add it to the end of the "zap_map" */
5192 vm_map_entry_link(zap_map
,
5193 vm_map_last_entry(zap_map
),
5195 entry_size
= entry
->vme_end
- entry
->vme_start
;
5196 map
->size
-= entry_size
;
5197 zap_map
->size
+= entry_size
;
5198 /* we didn't unlock the map, so no timestamp increase */
5201 vm_map_entry_delete(map
, entry
);
5202 /* vm_map_entry_delete unlocks the map */
5208 if(entry
== vm_map_to_entry(map
)) {
5211 if (last_timestamp
+1 != map
->timestamp
) {
5213 * we are responsible for deleting everything
5214 * from the give space, if someone has interfered
5215 * we pick up where we left off, back fills should
5216 * be all right for anyone except map_delete and
5217 * we have to assume that the task has been fully
5218 * disabled before we get here
5220 if (!vm_map_lookup_entry(map
, s
, &entry
)){
5221 entry
= entry
->vme_next
;
5222 s
= entry
->vme_start
;
5224 SAVE_HINT_MAP_WRITE(map
, entry
->vme_prev
);
5227 * others can not only allocate behind us, we can
5228 * also see coalesce while we don't have the map lock
5230 if(entry
== vm_map_to_entry(map
)) {
5234 last_timestamp
= map
->timestamp
;
5237 if (map
->wait_for_space
)
5238 thread_wakeup((event_t
) map
);
5240 * wake up anybody waiting on entries that we have already deleted.
5243 vm_map_entry_wakeup(map
);
5245 return KERN_SUCCESS
;
5251 * Remove the given address range from the target map.
5252 * This is the exported form of vm_map_delete.
5256 register vm_map_t map
,
5257 register vm_map_offset_t start
,
5258 register vm_map_offset_t end
,
5259 register boolean_t flags
)
5261 register kern_return_t result
;
5264 VM_MAP_RANGE_CHECK(map
, start
, end
);
5265 result
= vm_map_delete(map
, start
, end
, flags
, VM_MAP_NULL
);
5273 * Routine: vm_map_copy_discard
5276 * Dispose of a map copy object (returned by
5280 vm_map_copy_discard(
5283 if (copy
== VM_MAP_COPY_NULL
)
5286 switch (copy
->type
) {
5287 case VM_MAP_COPY_ENTRY_LIST
:
5288 while (vm_map_copy_first_entry(copy
) !=
5289 vm_map_copy_to_entry(copy
)) {
5290 vm_map_entry_t entry
= vm_map_copy_first_entry(copy
);
5292 vm_map_copy_entry_unlink(copy
, entry
);
5293 vm_object_deallocate(entry
->object
.vm_object
);
5294 vm_map_copy_entry_dispose(copy
, entry
);
5297 case VM_MAP_COPY_OBJECT
:
5298 vm_object_deallocate(copy
->cpy_object
);
5300 case VM_MAP_COPY_KERNEL_BUFFER
:
5303 * The vm_map_copy_t and possibly the data buffer were
5304 * allocated by a single call to kalloc(), i.e. the
5305 * vm_map_copy_t was not allocated out of the zone.
5307 kfree(copy
, copy
->cpy_kalloc_size
);
5310 zfree(vm_map_copy_zone
, copy
);
5314 * Routine: vm_map_copy_copy
5317 * Move the information in a map copy object to
5318 * a new map copy object, leaving the old one
5321 * This is used by kernel routines that need
5322 * to look at out-of-line data (in copyin form)
5323 * before deciding whether to return SUCCESS.
5324 * If the routine returns FAILURE, the original
5325 * copy object will be deallocated; therefore,
5326 * these routines must make a copy of the copy
5327 * object and leave the original empty so that
5328 * deallocation will not fail.
5334 vm_map_copy_t new_copy
;
5336 if (copy
== VM_MAP_COPY_NULL
)
5337 return VM_MAP_COPY_NULL
;
5340 * Allocate a new copy object, and copy the information
5341 * from the old one into it.
5344 new_copy
= (vm_map_copy_t
) zalloc(vm_map_copy_zone
);
5347 if (copy
->type
== VM_MAP_COPY_ENTRY_LIST
) {
5349 * The links in the entry chain must be
5350 * changed to point to the new copy object.
5352 vm_map_copy_first_entry(copy
)->vme_prev
5353 = vm_map_copy_to_entry(new_copy
);
5354 vm_map_copy_last_entry(copy
)->vme_next
5355 = vm_map_copy_to_entry(new_copy
);
5359 * Change the old copy object into one that contains
5360 * nothing to be deallocated.
5362 copy
->type
= VM_MAP_COPY_OBJECT
;
5363 copy
->cpy_object
= VM_OBJECT_NULL
;
5366 * Return the new object.
5371 static kern_return_t
5372 vm_map_overwrite_submap_recurse(
5374 vm_map_offset_t dst_addr
,
5375 vm_map_size_t dst_size
)
5377 vm_map_offset_t dst_end
;
5378 vm_map_entry_t tmp_entry
;
5379 vm_map_entry_t entry
;
5380 kern_return_t result
;
5381 boolean_t encountered_sub_map
= FALSE
;
5386 * Verify that the destination is all writeable
5387 * initially. We have to trunc the destination
5388 * address and round the copy size or we'll end up
5389 * splitting entries in strange ways.
5392 dst_end
= vm_map_round_page(dst_addr
+ dst_size
);
5393 vm_map_lock(dst_map
);
5396 if (!vm_map_lookup_entry(dst_map
, dst_addr
, &tmp_entry
)) {
5397 vm_map_unlock(dst_map
);
5398 return(KERN_INVALID_ADDRESS
);
5401 vm_map_clip_start(dst_map
, tmp_entry
, vm_map_trunc_page(dst_addr
));
5402 assert(!tmp_entry
->use_pmap
); /* clipping did unnest if needed */
5404 for (entry
= tmp_entry
;;) {
5405 vm_map_entry_t next
;
5407 next
= entry
->vme_next
;
5408 while(entry
->is_sub_map
) {
5409 vm_map_offset_t sub_start
;
5410 vm_map_offset_t sub_end
;
5411 vm_map_offset_t local_end
;
5413 if (entry
->in_transition
) {
5415 * Say that we are waiting, and wait for entry.
5417 entry
->needs_wakeup
= TRUE
;
5418 vm_map_entry_wait(dst_map
, THREAD_UNINT
);
5423 encountered_sub_map
= TRUE
;
5424 sub_start
= entry
->offset
;
5426 if(entry
->vme_end
< dst_end
)
5427 sub_end
= entry
->vme_end
;
5430 sub_end
-= entry
->vme_start
;
5431 sub_end
+= entry
->offset
;
5432 local_end
= entry
->vme_end
;
5433 vm_map_unlock(dst_map
);
5435 result
= vm_map_overwrite_submap_recurse(
5436 entry
->object
.sub_map
,
5438 sub_end
- sub_start
);
5440 if(result
!= KERN_SUCCESS
)
5442 if (dst_end
<= entry
->vme_end
)
5443 return KERN_SUCCESS
;
5444 vm_map_lock(dst_map
);
5445 if(!vm_map_lookup_entry(dst_map
, local_end
,
5447 vm_map_unlock(dst_map
);
5448 return(KERN_INVALID_ADDRESS
);
5451 next
= entry
->vme_next
;
5454 if ( ! (entry
->protection
& VM_PROT_WRITE
)) {
5455 vm_map_unlock(dst_map
);
5456 return(KERN_PROTECTION_FAILURE
);
5460 * If the entry is in transition, we must wait
5461 * for it to exit that state. Anything could happen
5462 * when we unlock the map, so start over.
5464 if (entry
->in_transition
) {
5467 * Say that we are waiting, and wait for entry.
5469 entry
->needs_wakeup
= TRUE
;
5470 vm_map_entry_wait(dst_map
, THREAD_UNINT
);
5476 * our range is contained completely within this map entry
5478 if (dst_end
<= entry
->vme_end
) {
5479 vm_map_unlock(dst_map
);
5480 return KERN_SUCCESS
;
5483 * check that range specified is contiguous region
5485 if ((next
== vm_map_to_entry(dst_map
)) ||
5486 (next
->vme_start
!= entry
->vme_end
)) {
5487 vm_map_unlock(dst_map
);
5488 return(KERN_INVALID_ADDRESS
);
5492 * Check for permanent objects in the destination.
5494 if ((entry
->object
.vm_object
!= VM_OBJECT_NULL
) &&
5495 ((!entry
->object
.vm_object
->internal
) ||
5496 (entry
->object
.vm_object
->true_share
))) {
5497 if(encountered_sub_map
) {
5498 vm_map_unlock(dst_map
);
5499 return(KERN_FAILURE
);
5506 vm_map_unlock(dst_map
);
5507 return(KERN_SUCCESS
);
5511 * Routine: vm_map_copy_overwrite
5514 * Copy the memory described by the map copy
5515 * object (copy; returned by vm_map_copyin) onto
5516 * the specified destination region (dst_map, dst_addr).
5517 * The destination must be writeable.
5519 * Unlike vm_map_copyout, this routine actually
5520 * writes over previously-mapped memory. If the
5521 * previous mapping was to a permanent (user-supplied)
5522 * memory object, it is preserved.
5524 * The attributes (protection and inheritance) of the
5525 * destination region are preserved.
5527 * If successful, consumes the copy object.
5528 * Otherwise, the caller is responsible for it.
5530 * Implementation notes:
5531 * To overwrite aligned temporary virtual memory, it is
5532 * sufficient to remove the previous mapping and insert
5533 * the new copy. This replacement is done either on
5534 * the whole region (if no permanent virtual memory
5535 * objects are embedded in the destination region) or
5536 * in individual map entries.
5538 * To overwrite permanent virtual memory , it is necessary
5539 * to copy each page, as the external memory management
5540 * interface currently does not provide any optimizations.
5542 * Unaligned memory also has to be copied. It is possible
5543 * to use 'vm_trickery' to copy the aligned data. This is
5544 * not done but not hard to implement.
5546 * Once a page of permanent memory has been overwritten,
5547 * it is impossible to interrupt this function; otherwise,
5548 * the call would be neither atomic nor location-independent.
5549 * The kernel-state portion of a user thread must be
5552 * It may be expensive to forward all requests that might
5553 * overwrite permanent memory (vm_write, vm_copy) to
5554 * uninterruptible kernel threads. This routine may be
5555 * called by interruptible threads; however, success is
5556 * not guaranteed -- if the request cannot be performed
5557 * atomically and interruptibly, an error indication is
5561 static kern_return_t
5562 vm_map_copy_overwrite_nested(
5564 vm_map_address_t dst_addr
,
5566 boolean_t interruptible
,
5569 vm_map_offset_t dst_end
;
5570 vm_map_entry_t tmp_entry
;
5571 vm_map_entry_t entry
;
5573 boolean_t aligned
= TRUE
;
5574 boolean_t contains_permanent_objects
= FALSE
;
5575 boolean_t encountered_sub_map
= FALSE
;
5576 vm_map_offset_t base_addr
;
5577 vm_map_size_t copy_size
;
5578 vm_map_size_t total_size
;
5582 * Check for null copy object.
5585 if (copy
== VM_MAP_COPY_NULL
)
5586 return(KERN_SUCCESS
);
5589 * Check for special kernel buffer allocated
5590 * by new_ipc_kmsg_copyin.
5593 if (copy
->type
== VM_MAP_COPY_KERNEL_BUFFER
) {
5594 return(vm_map_copyout_kernel_buffer(
5600 * Only works for entry lists at the moment. Will
5601 * support page lists later.
5604 assert(copy
->type
== VM_MAP_COPY_ENTRY_LIST
);
5606 if (copy
->size
== 0) {
5607 vm_map_copy_discard(copy
);
5608 return(KERN_SUCCESS
);
5612 * Verify that the destination is all writeable
5613 * initially. We have to trunc the destination
5614 * address and round the copy size or we'll end up
5615 * splitting entries in strange ways.
5618 if (!page_aligned(copy
->size
) ||
5619 !page_aligned (copy
->offset
) ||
5620 !page_aligned (dst_addr
))
5623 dst_end
= vm_map_round_page(dst_addr
+ copy
->size
);
5625 dst_end
= dst_addr
+ copy
->size
;
5628 vm_map_lock(dst_map
);
5630 /* LP64todo - remove this check when vm_map_commpage64()
5631 * no longer has to stuff in a map_entry for the commpage
5632 * above the map's max_offset.
5634 if (dst_addr
>= dst_map
->max_offset
) {
5635 vm_map_unlock(dst_map
);
5636 return(KERN_INVALID_ADDRESS
);
5640 if (!vm_map_lookup_entry(dst_map
, dst_addr
, &tmp_entry
)) {
5641 vm_map_unlock(dst_map
);
5642 return(KERN_INVALID_ADDRESS
);
5644 vm_map_clip_start(dst_map
, tmp_entry
, vm_map_trunc_page(dst_addr
));
5645 for (entry
= tmp_entry
;;) {
5646 vm_map_entry_t next
= entry
->vme_next
;
5648 while(entry
->is_sub_map
) {
5649 vm_map_offset_t sub_start
;
5650 vm_map_offset_t sub_end
;
5651 vm_map_offset_t local_end
;
5653 if (entry
->in_transition
) {
5656 * Say that we are waiting, and wait for entry.
5658 entry
->needs_wakeup
= TRUE
;
5659 vm_map_entry_wait(dst_map
, THREAD_UNINT
);
5664 local_end
= entry
->vme_end
;
5665 if (!(entry
->needs_copy
)) {
5666 /* if needs_copy we are a COW submap */
5667 /* in such a case we just replace so */
5668 /* there is no need for the follow- */
5670 encountered_sub_map
= TRUE
;
5671 sub_start
= entry
->offset
;
5673 if(entry
->vme_end
< dst_end
)
5674 sub_end
= entry
->vme_end
;
5677 sub_end
-= entry
->vme_start
;
5678 sub_end
+= entry
->offset
;
5679 vm_map_unlock(dst_map
);
5681 kr
= vm_map_overwrite_submap_recurse(
5682 entry
->object
.sub_map
,
5684 sub_end
- sub_start
);
5685 if(kr
!= KERN_SUCCESS
)
5687 vm_map_lock(dst_map
);
5690 if (dst_end
<= entry
->vme_end
)
5691 goto start_overwrite
;
5692 if(!vm_map_lookup_entry(dst_map
, local_end
,
5694 vm_map_unlock(dst_map
);
5695 return(KERN_INVALID_ADDRESS
);
5697 next
= entry
->vme_next
;
5700 if ( ! (entry
->protection
& VM_PROT_WRITE
)) {
5701 vm_map_unlock(dst_map
);
5702 return(KERN_PROTECTION_FAILURE
);
5706 * If the entry is in transition, we must wait
5707 * for it to exit that state. Anything could happen
5708 * when we unlock the map, so start over.
5710 if (entry
->in_transition
) {
5713 * Say that we are waiting, and wait for entry.
5715 entry
->needs_wakeup
= TRUE
;
5716 vm_map_entry_wait(dst_map
, THREAD_UNINT
);
5722 * our range is contained completely within this map entry
5724 if (dst_end
<= entry
->vme_end
)
5727 * check that range specified is contiguous region
5729 if ((next
== vm_map_to_entry(dst_map
)) ||
5730 (next
->vme_start
!= entry
->vme_end
)) {
5731 vm_map_unlock(dst_map
);
5732 return(KERN_INVALID_ADDRESS
);
5737 * Check for permanent objects in the destination.
5739 if ((entry
->object
.vm_object
!= VM_OBJECT_NULL
) &&
5740 ((!entry
->object
.vm_object
->internal
) ||
5741 (entry
->object
.vm_object
->true_share
))) {
5742 contains_permanent_objects
= TRUE
;
5750 * If there are permanent objects in the destination, then
5751 * the copy cannot be interrupted.
5754 if (interruptible
&& contains_permanent_objects
) {
5755 vm_map_unlock(dst_map
);
5756 return(KERN_FAILURE
); /* XXX */
5761 * Make a second pass, overwriting the data
5762 * At the beginning of each loop iteration,
5763 * the next entry to be overwritten is "tmp_entry"
5764 * (initially, the value returned from the lookup above),
5765 * and the starting address expected in that entry
5769 total_size
= copy
->size
;
5770 if(encountered_sub_map
) {
5772 /* re-calculate tmp_entry since we've had the map */
5774 if (!vm_map_lookup_entry( dst_map
, dst_addr
, &tmp_entry
)) {
5775 vm_map_unlock(dst_map
);
5776 return(KERN_INVALID_ADDRESS
);
5779 copy_size
= copy
->size
;
5782 base_addr
= dst_addr
;
5784 /* deconstruct the copy object and do in parts */
5785 /* only in sub_map, interruptable case */
5786 vm_map_entry_t copy_entry
;
5787 vm_map_entry_t previous_prev
= VM_MAP_ENTRY_NULL
;
5788 vm_map_entry_t next_copy
= VM_MAP_ENTRY_NULL
;
5790 int remaining_entries
= 0;
5791 vm_map_offset_t new_offset
= 0;
5793 for (entry
= tmp_entry
; copy_size
== 0;) {
5794 vm_map_entry_t next
;
5796 next
= entry
->vme_next
;
5798 /* tmp_entry and base address are moved along */
5799 /* each time we encounter a sub-map. Otherwise */
5800 /* entry can outpase tmp_entry, and the copy_size */
5801 /* may reflect the distance between them */
5802 /* if the current entry is found to be in transition */
5803 /* we will start over at the beginning or the last */
5804 /* encounter of a submap as dictated by base_addr */
5805 /* we will zero copy_size accordingly. */
5806 if (entry
->in_transition
) {
5808 * Say that we are waiting, and wait for entry.
5810 entry
->needs_wakeup
= TRUE
;
5811 vm_map_entry_wait(dst_map
, THREAD_UNINT
);
5813 if(!vm_map_lookup_entry(dst_map
, base_addr
,
5815 vm_map_unlock(dst_map
);
5816 return(KERN_INVALID_ADDRESS
);
5822 if(entry
->is_sub_map
) {
5823 vm_map_offset_t sub_start
;
5824 vm_map_offset_t sub_end
;
5825 vm_map_offset_t local_end
;
5827 if (entry
->needs_copy
) {
5828 /* if this is a COW submap */
5829 /* just back the range with a */
5830 /* anonymous entry */
5831 if(entry
->vme_end
< dst_end
)
5832 sub_end
= entry
->vme_end
;
5835 if(entry
->vme_start
< base_addr
)
5836 sub_start
= base_addr
;
5838 sub_start
= entry
->vme_start
;
5840 dst_map
, entry
, sub_end
);
5842 dst_map
, entry
, sub_start
);
5843 assert(!entry
->use_pmap
);
5844 entry
->is_sub_map
= FALSE
;
5846 entry
->object
.sub_map
);
5847 entry
->object
.sub_map
= NULL
;
5848 entry
->is_shared
= FALSE
;
5849 entry
->needs_copy
= FALSE
;
5853 * We should propagate the protections
5854 * of the submap entry here instead
5855 * of forcing them to VM_PROT_ALL...
5856 * Or better yet, we should inherit
5857 * the protection of the copy_entry.
5859 entry
->protection
= VM_PROT_ALL
;
5860 entry
->max_protection
= VM_PROT_ALL
;
5861 entry
->wired_count
= 0;
5862 entry
->user_wired_count
= 0;
5863 if(entry
->inheritance
5864 == VM_INHERIT_SHARE
)
5865 entry
->inheritance
= VM_INHERIT_COPY
;
5868 /* first take care of any non-sub_map */
5869 /* entries to send */
5870 if(base_addr
< entry
->vme_start
) {
5873 entry
->vme_start
- base_addr
;
5876 sub_start
= entry
->offset
;
5878 if(entry
->vme_end
< dst_end
)
5879 sub_end
= entry
->vme_end
;
5882 sub_end
-= entry
->vme_start
;
5883 sub_end
+= entry
->offset
;
5884 local_end
= entry
->vme_end
;
5885 vm_map_unlock(dst_map
);
5886 copy_size
= sub_end
- sub_start
;
5888 /* adjust the copy object */
5889 if (total_size
> copy_size
) {
5890 vm_map_size_t local_size
= 0;
5891 vm_map_size_t entry_size
;
5894 new_offset
= copy
->offset
;
5895 copy_entry
= vm_map_copy_first_entry(copy
);
5897 vm_map_copy_to_entry(copy
)){
5898 entry_size
= copy_entry
->vme_end
-
5899 copy_entry
->vme_start
;
5900 if((local_size
< copy_size
) &&
5901 ((local_size
+ entry_size
)
5903 vm_map_copy_clip_end(copy
,
5905 copy_entry
->vme_start
+
5906 (copy_size
- local_size
));
5907 entry_size
= copy_entry
->vme_end
-
5908 copy_entry
->vme_start
;
5909 local_size
+= entry_size
;
5910 new_offset
+= entry_size
;
5912 if(local_size
>= copy_size
) {
5913 next_copy
= copy_entry
->vme_next
;
5914 copy_entry
->vme_next
=
5915 vm_map_copy_to_entry(copy
);
5917 copy
->cpy_hdr
.links
.prev
;
5918 copy
->cpy_hdr
.links
.prev
= copy_entry
;
5919 copy
->size
= copy_size
;
5921 copy
->cpy_hdr
.nentries
;
5922 remaining_entries
-= nentries
;
5923 copy
->cpy_hdr
.nentries
= nentries
;
5926 local_size
+= entry_size
;
5927 new_offset
+= entry_size
;
5930 copy_entry
= copy_entry
->vme_next
;
5934 if((entry
->use_pmap
) && (pmap
== NULL
)) {
5935 kr
= vm_map_copy_overwrite_nested(
5936 entry
->object
.sub_map
,
5940 entry
->object
.sub_map
->pmap
);
5941 } else if (pmap
!= NULL
) {
5942 kr
= vm_map_copy_overwrite_nested(
5943 entry
->object
.sub_map
,
5946 interruptible
, pmap
);
5948 kr
= vm_map_copy_overwrite_nested(
5949 entry
->object
.sub_map
,
5955 if(kr
!= KERN_SUCCESS
) {
5956 if(next_copy
!= NULL
) {
5957 copy
->cpy_hdr
.nentries
+=
5959 copy
->cpy_hdr
.links
.prev
->vme_next
=
5961 copy
->cpy_hdr
.links
.prev
5963 copy
->size
= total_size
;
5967 if (dst_end
<= local_end
) {
5968 return(KERN_SUCCESS
);
5970 /* otherwise copy no longer exists, it was */
5971 /* destroyed after successful copy_overwrite */
5972 copy
= (vm_map_copy_t
)
5973 zalloc(vm_map_copy_zone
);
5974 vm_map_copy_first_entry(copy
) =
5975 vm_map_copy_last_entry(copy
) =
5976 vm_map_copy_to_entry(copy
);
5977 copy
->type
= VM_MAP_COPY_ENTRY_LIST
;
5978 copy
->offset
= new_offset
;
5980 total_size
-= copy_size
;
5982 /* put back remainder of copy in container */
5983 if(next_copy
!= NULL
) {
5984 copy
->cpy_hdr
.nentries
= remaining_entries
;
5985 copy
->cpy_hdr
.links
.next
= next_copy
;
5986 copy
->cpy_hdr
.links
.prev
= previous_prev
;
5987 copy
->size
= total_size
;
5988 next_copy
->vme_prev
=
5989 vm_map_copy_to_entry(copy
);
5992 base_addr
= local_end
;
5993 vm_map_lock(dst_map
);
5994 if(!vm_map_lookup_entry(dst_map
,
5995 local_end
, &tmp_entry
)) {
5996 vm_map_unlock(dst_map
);
5997 return(KERN_INVALID_ADDRESS
);
6002 if (dst_end
<= entry
->vme_end
) {
6003 copy_size
= dst_end
- base_addr
;
6007 if ((next
== vm_map_to_entry(dst_map
)) ||
6008 (next
->vme_start
!= entry
->vme_end
)) {
6009 vm_map_unlock(dst_map
);
6010 return(KERN_INVALID_ADDRESS
);
6019 /* adjust the copy object */
6020 if (total_size
> copy_size
) {
6021 vm_map_size_t local_size
= 0;
6022 vm_map_size_t entry_size
;
6024 new_offset
= copy
->offset
;
6025 copy_entry
= vm_map_copy_first_entry(copy
);
6026 while(copy_entry
!= vm_map_copy_to_entry(copy
)) {
6027 entry_size
= copy_entry
->vme_end
-
6028 copy_entry
->vme_start
;
6029 if((local_size
< copy_size
) &&
6030 ((local_size
+ entry_size
)
6032 vm_map_copy_clip_end(copy
, copy_entry
,
6033 copy_entry
->vme_start
+
6034 (copy_size
- local_size
));
6035 entry_size
= copy_entry
->vme_end
-
6036 copy_entry
->vme_start
;
6037 local_size
+= entry_size
;
6038 new_offset
+= entry_size
;
6040 if(local_size
>= copy_size
) {
6041 next_copy
= copy_entry
->vme_next
;
6042 copy_entry
->vme_next
=
6043 vm_map_copy_to_entry(copy
);
6045 copy
->cpy_hdr
.links
.prev
;
6046 copy
->cpy_hdr
.links
.prev
= copy_entry
;
6047 copy
->size
= copy_size
;
6049 copy
->cpy_hdr
.nentries
;
6050 remaining_entries
-= nentries
;
6051 copy
->cpy_hdr
.nentries
= nentries
;
6054 local_size
+= entry_size
;
6055 new_offset
+= entry_size
;
6058 copy_entry
= copy_entry
->vme_next
;
6068 local_pmap
= dst_map
->pmap
;
6070 if ((kr
= vm_map_copy_overwrite_aligned(
6071 dst_map
, tmp_entry
, copy
,
6072 base_addr
, local_pmap
)) != KERN_SUCCESS
) {
6073 if(next_copy
!= NULL
) {
6074 copy
->cpy_hdr
.nentries
+=
6076 copy
->cpy_hdr
.links
.prev
->vme_next
=
6078 copy
->cpy_hdr
.links
.prev
=
6080 copy
->size
+= copy_size
;
6084 vm_map_unlock(dst_map
);
6089 * if the copy and dst address are misaligned but the same
6090 * offset within the page we can copy_not_aligned the
6091 * misaligned parts and copy aligned the rest. If they are
6092 * aligned but len is unaligned we simply need to copy
6093 * the end bit unaligned. We'll need to split the misaligned
6094 * bits of the region in this case !
6096 /* ALWAYS UNLOCKS THE dst_map MAP */
6097 if ((kr
= vm_map_copy_overwrite_unaligned( dst_map
,
6098 tmp_entry
, copy
, base_addr
)) != KERN_SUCCESS
) {
6099 if(next_copy
!= NULL
) {
6100 copy
->cpy_hdr
.nentries
+=
6102 copy
->cpy_hdr
.links
.prev
->vme_next
=
6104 copy
->cpy_hdr
.links
.prev
=
6106 copy
->size
+= copy_size
;
6111 total_size
-= copy_size
;
6114 base_addr
+= copy_size
;
6116 copy
->offset
= new_offset
;
6117 if(next_copy
!= NULL
) {
6118 copy
->cpy_hdr
.nentries
= remaining_entries
;
6119 copy
->cpy_hdr
.links
.next
= next_copy
;
6120 copy
->cpy_hdr
.links
.prev
= previous_prev
;
6121 next_copy
->vme_prev
= vm_map_copy_to_entry(copy
);
6122 copy
->size
= total_size
;
6124 vm_map_lock(dst_map
);
6126 if (!vm_map_lookup_entry(dst_map
,
6127 base_addr
, &tmp_entry
)) {
6128 vm_map_unlock(dst_map
);
6129 return(KERN_INVALID_ADDRESS
);
6131 if (tmp_entry
->in_transition
) {
6132 entry
->needs_wakeup
= TRUE
;
6133 vm_map_entry_wait(dst_map
, THREAD_UNINT
);
6138 vm_map_clip_start(dst_map
, tmp_entry
, vm_map_trunc_page(base_addr
));
6144 * Throw away the vm_map_copy object
6146 vm_map_copy_discard(copy
);
6148 return(KERN_SUCCESS
);
6149 }/* vm_map_copy_overwrite */
6152 vm_map_copy_overwrite(
6154 vm_map_offset_t dst_addr
,
6156 boolean_t interruptible
)
6158 return vm_map_copy_overwrite_nested(
6159 dst_map
, dst_addr
, copy
, interruptible
, (pmap_t
) NULL
);
6164 * Routine: vm_map_copy_overwrite_unaligned [internal use only]
6167 * Physically copy unaligned data
6170 * Unaligned parts of pages have to be physically copied. We use
6171 * a modified form of vm_fault_copy (which understands none-aligned
6172 * page offsets and sizes) to do the copy. We attempt to copy as
6173 * much memory in one go as possibly, however vm_fault_copy copies
6174 * within 1 memory object so we have to find the smaller of "amount left"
6175 * "source object data size" and "target object data size". With
6176 * unaligned data we don't need to split regions, therefore the source
6177 * (copy) object should be one map entry, the target range may be split
6178 * over multiple map entries however. In any event we are pessimistic
6179 * about these assumptions.
6182 * dst_map is locked on entry and is return locked on success,
6183 * unlocked on error.
6186 static kern_return_t
6187 vm_map_copy_overwrite_unaligned(
6189 vm_map_entry_t entry
,
6191 vm_map_offset_t start
)
6193 vm_map_entry_t copy_entry
= vm_map_copy_first_entry(copy
);
6194 vm_map_version_t version
;
6195 vm_object_t dst_object
;
6196 vm_object_offset_t dst_offset
;
6197 vm_object_offset_t src_offset
;
6198 vm_object_offset_t entry_offset
;
6199 vm_map_offset_t entry_end
;
6200 vm_map_size_t src_size
,
6204 kern_return_t kr
= KERN_SUCCESS
;
6206 vm_map_lock_write_to_read(dst_map
);
6208 src_offset
= copy
->offset
- vm_object_trunc_page(copy
->offset
);
6209 amount_left
= copy
->size
;
6211 * unaligned so we never clipped this entry, we need the offset into
6212 * the vm_object not just the data.
6214 while (amount_left
> 0) {
6216 if (entry
== vm_map_to_entry(dst_map
)) {
6217 vm_map_unlock_read(dst_map
);
6218 return KERN_INVALID_ADDRESS
;
6221 /* "start" must be within the current map entry */
6222 assert ((start
>=entry
->vme_start
) && (start
<entry
->vme_end
));
6224 dst_offset
= start
- entry
->vme_start
;
6226 dst_size
= entry
->vme_end
- start
;
6228 src_size
= copy_entry
->vme_end
-
6229 (copy_entry
->vme_start
+ src_offset
);
6231 if (dst_size
< src_size
) {
6233 * we can only copy dst_size bytes before
6234 * we have to get the next destination entry
6236 copy_size
= dst_size
;
6239 * we can only copy src_size bytes before
6240 * we have to get the next source copy entry
6242 copy_size
= src_size
;
6245 if (copy_size
> amount_left
) {
6246 copy_size
= amount_left
;
6249 * Entry needs copy, create a shadow shadow object for
6250 * Copy on write region.
6252 if (entry
->needs_copy
&&
6253 ((entry
->protection
& VM_PROT_WRITE
) != 0))
6255 if (vm_map_lock_read_to_write(dst_map
)) {
6256 vm_map_lock_read(dst_map
);
6259 vm_object_shadow(&entry
->object
.vm_object
,
6261 (vm_map_size_t
)(entry
->vme_end
6262 - entry
->vme_start
));
6263 entry
->needs_copy
= FALSE
;
6264 vm_map_lock_write_to_read(dst_map
);
6266 dst_object
= entry
->object
.vm_object
;
6268 * unlike with the virtual (aligned) copy we're going
6269 * to fault on it therefore we need a target object.
6271 if (dst_object
== VM_OBJECT_NULL
) {
6272 if (vm_map_lock_read_to_write(dst_map
)) {
6273 vm_map_lock_read(dst_map
);
6276 dst_object
= vm_object_allocate((vm_map_size_t
)
6277 entry
->vme_end
- entry
->vme_start
);
6278 entry
->object
.vm_object
= dst_object
;
6280 vm_map_lock_write_to_read(dst_map
);
6283 * Take an object reference and unlock map. The "entry" may
6284 * disappear or change when the map is unlocked.
6286 vm_object_reference(dst_object
);
6287 version
.main_timestamp
= dst_map
->timestamp
;
6288 entry_offset
= entry
->offset
;
6289 entry_end
= entry
->vme_end
;
6290 vm_map_unlock_read(dst_map
);
6292 * Copy as much as possible in one pass
6295 copy_entry
->object
.vm_object
,
6296 copy_entry
->offset
+ src_offset
,
6299 entry_offset
+ dst_offset
,
6305 src_offset
+= copy_size
;
6306 amount_left
-= copy_size
;
6308 * Release the object reference
6310 vm_object_deallocate(dst_object
);
6312 * If a hard error occurred, return it now
6314 if (kr
!= KERN_SUCCESS
)
6317 if ((copy_entry
->vme_start
+ src_offset
) == copy_entry
->vme_end
6318 || amount_left
== 0)
6321 * all done with this copy entry, dispose.
6323 vm_map_copy_entry_unlink(copy
, copy_entry
);
6324 vm_object_deallocate(copy_entry
->object
.vm_object
);
6325 vm_map_copy_entry_dispose(copy
, copy_entry
);
6327 if ((copy_entry
= vm_map_copy_first_entry(copy
))
6328 == vm_map_copy_to_entry(copy
) && amount_left
) {
6330 * not finished copying but run out of source
6332 return KERN_INVALID_ADDRESS
;
6337 if (amount_left
== 0)
6338 return KERN_SUCCESS
;
6340 vm_map_lock_read(dst_map
);
6341 if (version
.main_timestamp
== dst_map
->timestamp
) {
6342 if (start
== entry_end
) {
6344 * destination region is split. Use the version
6345 * information to avoid a lookup in the normal
6348 entry
= entry
->vme_next
;
6350 * should be contiguous. Fail if we encounter
6351 * a hole in the destination.
6353 if (start
!= entry
->vme_start
) {
6354 vm_map_unlock_read(dst_map
);
6355 return KERN_INVALID_ADDRESS
;
6360 * Map version check failed.
6361 * we must lookup the entry because somebody
6362 * might have changed the map behind our backs.
6365 if (!vm_map_lookup_entry(dst_map
, start
, &entry
))
6367 vm_map_unlock_read(dst_map
);
6368 return KERN_INVALID_ADDRESS
;
6373 return KERN_SUCCESS
;
6374 }/* vm_map_copy_overwrite_unaligned */
6377 * Routine: vm_map_copy_overwrite_aligned [internal use only]
6380 * Does all the vm_trickery possible for whole pages.
6384 * If there are no permanent objects in the destination,
6385 * and the source and destination map entry zones match,
6386 * and the destination map entry is not shared,
6387 * then the map entries can be deleted and replaced
6388 * with those from the copy. The following code is the
6389 * basic idea of what to do, but there are lots of annoying
6390 * little details about getting protection and inheritance
6391 * right. Should add protection, inheritance, and sharing checks
6392 * to the above pass and make sure that no wiring is involved.
6395 static kern_return_t
6396 vm_map_copy_overwrite_aligned(
6398 vm_map_entry_t tmp_entry
,
6400 vm_map_offset_t start
,
6401 __unused pmap_t pmap
)
6404 vm_map_entry_t copy_entry
;
6405 vm_map_size_t copy_size
;
6407 vm_map_entry_t entry
;
6409 while ((copy_entry
= vm_map_copy_first_entry(copy
))
6410 != vm_map_copy_to_entry(copy
))
6412 copy_size
= (copy_entry
->vme_end
- copy_entry
->vme_start
);
6415 assert(!entry
->use_pmap
); /* unnested when clipped earlier */
6416 if (entry
== vm_map_to_entry(dst_map
)) {
6417 vm_map_unlock(dst_map
);
6418 return KERN_INVALID_ADDRESS
;
6420 size
= (entry
->vme_end
- entry
->vme_start
);
6422 * Make sure that no holes popped up in the
6423 * address map, and that the protection is
6424 * still valid, in case the map was unlocked
6428 if ((entry
->vme_start
!= start
) || ((entry
->is_sub_map
)
6429 && !entry
->needs_copy
)) {
6430 vm_map_unlock(dst_map
);
6431 return(KERN_INVALID_ADDRESS
);
6433 assert(entry
!= vm_map_to_entry(dst_map
));
6436 * Check protection again
6439 if ( ! (entry
->protection
& VM_PROT_WRITE
)) {
6440 vm_map_unlock(dst_map
);
6441 return(KERN_PROTECTION_FAILURE
);
6445 * Adjust to source size first
6448 if (copy_size
< size
) {
6449 vm_map_clip_end(dst_map
, entry
, entry
->vme_start
+ copy_size
);
6454 * Adjust to destination size
6457 if (size
< copy_size
) {
6458 vm_map_copy_clip_end(copy
, copy_entry
,
6459 copy_entry
->vme_start
+ size
);
6463 assert((entry
->vme_end
- entry
->vme_start
) == size
);
6464 assert((tmp_entry
->vme_end
- tmp_entry
->vme_start
) == size
);
6465 assert((copy_entry
->vme_end
- copy_entry
->vme_start
) == size
);
6468 * If the destination contains temporary unshared memory,
6469 * we can perform the copy by throwing it away and
6470 * installing the source data.
6473 object
= entry
->object
.vm_object
;
6474 if ((!entry
->is_shared
&&
6475 ((object
== VM_OBJECT_NULL
) ||
6476 (object
->internal
&& !object
->true_share
))) ||
6477 entry
->needs_copy
) {
6478 vm_object_t old_object
= entry
->object
.vm_object
;
6479 vm_object_offset_t old_offset
= entry
->offset
;
6480 vm_object_offset_t offset
;
6483 * Ensure that the source and destination aren't
6486 if (old_object
== copy_entry
->object
.vm_object
&&
6487 old_offset
== copy_entry
->offset
) {
6488 vm_map_copy_entry_unlink(copy
, copy_entry
);
6489 vm_map_copy_entry_dispose(copy
, copy_entry
);
6491 if (old_object
!= VM_OBJECT_NULL
)
6492 vm_object_deallocate(old_object
);
6494 start
= tmp_entry
->vme_end
;
6495 tmp_entry
= tmp_entry
->vme_next
;
6499 if (old_object
!= VM_OBJECT_NULL
) {
6500 if(entry
->is_sub_map
) {
6501 if(entry
->use_pmap
) {
6502 #ifndef NO_NESTED_PMAP
6503 pmap_unnest(dst_map
->pmap
,
6504 (addr64_t
)entry
->vme_start
,
6505 entry
->vme_end
- entry
->vme_start
);
6506 #endif /* NO_NESTED_PMAP */
6507 if(dst_map
->mapped
) {
6508 /* clean up parent */
6510 vm_map_submap_pmap_clean(
6511 dst_map
, entry
->vme_start
,
6513 entry
->object
.sub_map
,
6517 vm_map_submap_pmap_clean(
6518 dst_map
, entry
->vme_start
,
6520 entry
->object
.sub_map
,
6524 entry
->object
.sub_map
);
6526 if(dst_map
->mapped
) {
6527 vm_object_pmap_protect(
6528 entry
->object
.vm_object
,
6536 pmap_remove(dst_map
->pmap
,
6537 (addr64_t
)(entry
->vme_start
),
6538 (addr64_t
)(entry
->vme_end
));
6540 vm_object_deallocate(old_object
);
6544 entry
->is_sub_map
= FALSE
;
6545 entry
->object
= copy_entry
->object
;
6546 object
= entry
->object
.vm_object
;
6547 entry
->needs_copy
= copy_entry
->needs_copy
;
6548 entry
->wired_count
= 0;
6549 entry
->user_wired_count
= 0;
6550 offset
= entry
->offset
= copy_entry
->offset
;
6552 vm_map_copy_entry_unlink(copy
, copy_entry
);
6553 vm_map_copy_entry_dispose(copy
, copy_entry
);
6556 * we could try to push pages into the pmap at this point, BUT
6557 * this optimization only saved on average 2 us per page if ALL
6558 * the pages in the source were currently mapped
6559 * and ALL the pages in the dest were touched, if there were fewer
6560 * than 2/3 of the pages touched, this optimization actually cost more cycles
6561 * it also puts a lot of pressure on the pmap layer w/r to mapping structures
6565 * Set up for the next iteration. The map
6566 * has not been unlocked, so the next
6567 * address should be at the end of this
6568 * entry, and the next map entry should be
6569 * the one following it.
6572 start
= tmp_entry
->vme_end
;
6573 tmp_entry
= tmp_entry
->vme_next
;
6575 vm_map_version_t version
;
6576 vm_object_t dst_object
= entry
->object
.vm_object
;
6577 vm_object_offset_t dst_offset
= entry
->offset
;
6581 * Take an object reference, and record
6582 * the map version information so that the
6583 * map can be safely unlocked.
6586 vm_object_reference(dst_object
);
6588 /* account for unlock bumping up timestamp */
6589 version
.main_timestamp
= dst_map
->timestamp
+ 1;
6591 vm_map_unlock(dst_map
);
6594 * Copy as much as possible in one pass
6599 copy_entry
->object
.vm_object
,
6609 * Release the object reference
6612 vm_object_deallocate(dst_object
);
6615 * If a hard error occurred, return it now
6618 if (r
!= KERN_SUCCESS
)
6621 if (copy_size
!= 0) {
6623 * Dispose of the copied region
6626 vm_map_copy_clip_end(copy
, copy_entry
,
6627 copy_entry
->vme_start
+ copy_size
);
6628 vm_map_copy_entry_unlink(copy
, copy_entry
);
6629 vm_object_deallocate(copy_entry
->object
.vm_object
);
6630 vm_map_copy_entry_dispose(copy
, copy_entry
);
6634 * Pick up in the destination map where we left off.
6636 * Use the version information to avoid a lookup
6637 * in the normal case.
6641 vm_map_lock(dst_map
);
6642 if (version
.main_timestamp
== dst_map
->timestamp
) {
6643 /* We can safely use saved tmp_entry value */
6645 vm_map_clip_end(dst_map
, tmp_entry
, start
);
6646 tmp_entry
= tmp_entry
->vme_next
;
6648 /* Must do lookup of tmp_entry */
6650 if (!vm_map_lookup_entry(dst_map
, start
, &tmp_entry
)) {
6651 vm_map_unlock(dst_map
);
6652 return(KERN_INVALID_ADDRESS
);
6654 vm_map_clip_start(dst_map
, tmp_entry
, start
);
6659 return(KERN_SUCCESS
);
6660 }/* vm_map_copy_overwrite_aligned */
6663 * Routine: vm_map_copyin_kernel_buffer [internal use only]
6666 * Copy in data to a kernel buffer from space in the
6667 * source map. The original space may be optionally
6670 * If successful, returns a new copy object.
6672 static kern_return_t
6673 vm_map_copyin_kernel_buffer(
6675 vm_map_offset_t src_addr
,
6677 boolean_t src_destroy
,
6678 vm_map_copy_t
*copy_result
)
6682 vm_size_t kalloc_size
;
6684 if ((vm_size_t
) len
!= len
) {
6685 /* "len" is too big and doesn't fit in a "vm_size_t" */
6686 return KERN_RESOURCE_SHORTAGE
;
6688 kalloc_size
= (vm_size_t
) (sizeof(struct vm_map_copy
) + len
);
6689 assert((vm_map_size_t
) kalloc_size
== sizeof (struct vm_map_copy
) + len
);
6691 copy
= (vm_map_copy_t
) kalloc(kalloc_size
);
6692 if (copy
== VM_MAP_COPY_NULL
) {
6693 return KERN_RESOURCE_SHORTAGE
;
6695 copy
->type
= VM_MAP_COPY_KERNEL_BUFFER
;
6698 copy
->cpy_kdata
= (void *) (copy
+ 1);
6699 copy
->cpy_kalloc_size
= kalloc_size
;
6701 kr
= copyinmap(src_map
, src_addr
, copy
->cpy_kdata
, (vm_size_t
) len
);
6702 if (kr
!= KERN_SUCCESS
) {
6703 kfree(copy
, kalloc_size
);
6707 (void) vm_map_remove(src_map
, vm_map_trunc_page(src_addr
),
6708 vm_map_round_page(src_addr
+ len
),
6709 VM_MAP_REMOVE_INTERRUPTIBLE
|
6710 VM_MAP_REMOVE_WAIT_FOR_KWIRE
|
6711 (src_map
== kernel_map
) ?
6712 VM_MAP_REMOVE_KUNWIRE
: 0);
6714 *copy_result
= copy
;
6715 return KERN_SUCCESS
;
6719 * Routine: vm_map_copyout_kernel_buffer [internal use only]
6722 * Copy out data from a kernel buffer into space in the
6723 * destination map. The space may be otpionally dynamically
6726 * If successful, consumes the copy object.
6727 * Otherwise, the caller is responsible for it.
6729 static int vm_map_copyout_kernel_buffer_failures
= 0;
6730 static kern_return_t
6731 vm_map_copyout_kernel_buffer(
6733 vm_map_address_t
*addr
, /* IN/OUT */
6735 boolean_t overwrite
)
6737 kern_return_t kr
= KERN_SUCCESS
;
6738 thread_t thread
= current_thread();
6743 * Allocate space in the target map for the data
6746 kr
= vm_map_enter(map
,
6748 vm_map_round_page(copy
->size
),
6749 (vm_map_offset_t
) 0,
6752 (vm_object_offset_t
) 0,
6756 VM_INHERIT_DEFAULT
);
6757 if (kr
!= KERN_SUCCESS
)
6762 * Copyout the data from the kernel buffer to the target map.
6764 if (thread
->map
== map
) {
6767 * If the target map is the current map, just do
6770 assert((vm_size_t
) copy
->size
== copy
->size
);
6771 if (copyout(copy
->cpy_kdata
, *addr
, (vm_size_t
) copy
->size
)) {
6772 kr
= KERN_INVALID_ADDRESS
;
6779 * If the target map is another map, assume the
6780 * target's address space identity for the duration
6783 vm_map_reference(map
);
6784 oldmap
= vm_map_switch(map
);
6786 assert((vm_size_t
) copy
->size
== copy
->size
);
6787 if (copyout(copy
->cpy_kdata
, *addr
, (vm_size_t
) copy
->size
)) {
6788 vm_map_copyout_kernel_buffer_failures
++;
6789 kr
= KERN_INVALID_ADDRESS
;
6792 (void) vm_map_switch(oldmap
);
6793 vm_map_deallocate(map
);
6796 if (kr
!= KERN_SUCCESS
) {
6797 /* the copy failed, clean up */
6800 * Deallocate the space we allocated in the target map.
6802 (void) vm_map_remove(map
,
6803 vm_map_trunc_page(*addr
),
6804 vm_map_round_page(*addr
+
6805 vm_map_round_page(copy
->size
)),
6810 /* copy was successful, dicard the copy structure */
6811 kfree(copy
, copy
->cpy_kalloc_size
);
6818 * Macro: vm_map_copy_insert
6821 * Link a copy chain ("copy") into a map at the
6822 * specified location (after "where").
6824 * The copy chain is destroyed.
6826 * The arguments are evaluated multiple times.
6828 #define vm_map_copy_insert(map, where, copy) \
6830 vm_map_t VMCI_map; \
6831 vm_map_entry_t VMCI_where; \
6832 vm_map_copy_t VMCI_copy; \
6834 VMCI_where = (where); \
6835 VMCI_copy = (copy); \
6836 ((VMCI_where->vme_next)->vme_prev = vm_map_copy_last_entry(VMCI_copy))\
6837 ->vme_next = (VMCI_where->vme_next); \
6838 ((VMCI_where)->vme_next = vm_map_copy_first_entry(VMCI_copy)) \
6839 ->vme_prev = VMCI_where; \
6840 VMCI_map->hdr.nentries += VMCI_copy->cpy_hdr.nentries; \
6841 UPDATE_FIRST_FREE(VMCI_map, VMCI_map->first_free); \
6842 zfree(vm_map_copy_zone, VMCI_copy); \
6846 * Routine: vm_map_copyout
6849 * Copy out a copy chain ("copy") into newly-allocated
6850 * space in the destination map.
6852 * If successful, consumes the copy object.
6853 * Otherwise, the caller is responsible for it.
6858 vm_map_address_t
*dst_addr
, /* OUT */
6862 vm_map_size_t adjustment
;
6863 vm_map_offset_t start
;
6864 vm_object_offset_t vm_copy_start
;
6865 vm_map_entry_t last
;
6867 vm_map_entry_t entry
;
6870 * Check for null copy object.
6873 if (copy
== VM_MAP_COPY_NULL
) {
6875 return(KERN_SUCCESS
);
6879 * Check for special copy object, created
6880 * by vm_map_copyin_object.
6883 if (copy
->type
== VM_MAP_COPY_OBJECT
) {
6884 vm_object_t object
= copy
->cpy_object
;
6886 vm_object_offset_t offset
;
6888 offset
= vm_object_trunc_page(copy
->offset
);
6889 size
= vm_map_round_page(copy
->size
+
6890 (vm_map_size_t
)(copy
->offset
- offset
));
6892 kr
= vm_map_enter(dst_map
, dst_addr
, size
,
6893 (vm_map_offset_t
) 0, VM_FLAGS_ANYWHERE
,
6894 object
, offset
, FALSE
,
6895 VM_PROT_DEFAULT
, VM_PROT_ALL
,
6896 VM_INHERIT_DEFAULT
);
6897 if (kr
!= KERN_SUCCESS
)
6899 /* Account for non-pagealigned copy object */
6900 *dst_addr
+= (vm_map_offset_t
)(copy
->offset
- offset
);
6901 zfree(vm_map_copy_zone
, copy
);
6902 return(KERN_SUCCESS
);
6906 * Check for special kernel buffer allocated
6907 * by new_ipc_kmsg_copyin.
6910 if (copy
->type
== VM_MAP_COPY_KERNEL_BUFFER
) {
6911 return(vm_map_copyout_kernel_buffer(dst_map
, dst_addr
,
6916 * Find space for the data
6919 vm_copy_start
= vm_object_trunc_page(copy
->offset
);
6920 size
= vm_map_round_page((vm_map_size_t
)copy
->offset
+ copy
->size
)
6925 vm_map_lock(dst_map
);
6926 assert(first_free_is_valid(dst_map
));
6927 start
= ((last
= dst_map
->first_free
) == vm_map_to_entry(dst_map
)) ?
6928 vm_map_min(dst_map
) : last
->vme_end
;
6931 vm_map_entry_t next
= last
->vme_next
;
6932 vm_map_offset_t end
= start
+ size
;
6934 if ((end
> dst_map
->max_offset
) || (end
< start
)) {
6935 if (dst_map
->wait_for_space
) {
6936 if (size
<= (dst_map
->max_offset
- dst_map
->min_offset
)) {
6937 assert_wait((event_t
) dst_map
,
6938 THREAD_INTERRUPTIBLE
);
6939 vm_map_unlock(dst_map
);
6940 thread_block(THREAD_CONTINUE_NULL
);
6944 vm_map_unlock(dst_map
);
6945 return(KERN_NO_SPACE
);
6948 if ((next
== vm_map_to_entry(dst_map
)) ||
6949 (next
->vme_start
>= end
))
6953 start
= last
->vme_end
;
6957 * Since we're going to just drop the map
6958 * entries from the copy into the destination
6959 * map, they must come from the same pool.
6962 if (copy
->cpy_hdr
.entries_pageable
!= dst_map
->hdr
.entries_pageable
) {
6964 * Mismatches occur when dealing with the default
6968 vm_map_entry_t next
, new;
6971 * Find the zone that the copies were allocated from
6973 old_zone
= (copy
->cpy_hdr
.entries_pageable
)
6975 : vm_map_kentry_zone
;
6976 entry
= vm_map_copy_first_entry(copy
);
6979 * Reinitialize the copy so that vm_map_copy_entry_link
6982 copy
->cpy_hdr
.nentries
= 0;
6983 copy
->cpy_hdr
.entries_pageable
= dst_map
->hdr
.entries_pageable
;
6984 vm_map_copy_first_entry(copy
) =
6985 vm_map_copy_last_entry(copy
) =
6986 vm_map_copy_to_entry(copy
);
6991 while (entry
!= vm_map_copy_to_entry(copy
)) {
6992 new = vm_map_copy_entry_create(copy
);
6993 vm_map_entry_copy_full(new, entry
);
6994 new->use_pmap
= FALSE
; /* clr address space specifics */
6995 vm_map_copy_entry_link(copy
,
6996 vm_map_copy_last_entry(copy
),
6998 next
= entry
->vme_next
;
6999 zfree(old_zone
, entry
);
7005 * Adjust the addresses in the copy chain, and
7006 * reset the region attributes.
7009 adjustment
= start
- vm_copy_start
;
7010 for (entry
= vm_map_copy_first_entry(copy
);
7011 entry
!= vm_map_copy_to_entry(copy
);
7012 entry
= entry
->vme_next
) {
7013 entry
->vme_start
+= adjustment
;
7014 entry
->vme_end
+= adjustment
;
7016 entry
->inheritance
= VM_INHERIT_DEFAULT
;
7017 entry
->protection
= VM_PROT_DEFAULT
;
7018 entry
->max_protection
= VM_PROT_ALL
;
7019 entry
->behavior
= VM_BEHAVIOR_DEFAULT
;
7022 * If the entry is now wired,
7023 * map the pages into the destination map.
7025 if (entry
->wired_count
!= 0) {
7026 register vm_map_offset_t va
;
7027 vm_object_offset_t offset
;
7028 register vm_object_t object
;
7032 object
= entry
->object
.vm_object
;
7033 offset
= entry
->offset
;
7034 va
= entry
->vme_start
;
7036 pmap_pageable(dst_map
->pmap
,
7041 while (va
< entry
->vme_end
) {
7042 register vm_page_t m
;
7045 * Look up the page in the object.
7046 * Assert that the page will be found in the
7049 * the object was newly created by
7050 * vm_object_copy_slowly, and has
7051 * copies of all of the pages from
7054 * the object was moved from the old
7055 * map entry; because the old map
7056 * entry was wired, all of the pages
7057 * were in the top-level object.
7058 * (XXX not true if we wire pages for
7061 vm_object_lock(object
);
7063 m
= vm_page_lookup(object
, offset
);
7064 if (m
== VM_PAGE_NULL
|| !VM_PAGE_WIRED(m
) ||
7066 panic("vm_map_copyout: wiring %p", m
);
7070 * The page is assumed to be wired here, so it
7071 * shouldn't be encrypted. Otherwise, we
7072 * couldn't enter it in the page table, since
7073 * we don't want the user to see the encrypted
7076 ASSERT_PAGE_DECRYPTED(m
);
7078 prot
= entry
->protection
;
7080 if (override_nx(dst_map
, entry
->alias
) && prot
)
7081 prot
|= VM_PROT_EXECUTE
;
7083 type_of_fault
= DBG_CACHE_HIT_FAULT
;
7085 vm_fault_enter(m
, dst_map
->pmap
, va
, prot
,
7086 VM_PAGE_WIRED(m
), FALSE
, FALSE
,
7089 vm_object_unlock(object
);
7091 offset
+= PAGE_SIZE_64
;
7098 * Correct the page alignment for the result
7101 *dst_addr
= start
+ (copy
->offset
- vm_copy_start
);
7104 * Update the hints and the map size
7107 SAVE_HINT_MAP_WRITE(dst_map
, vm_map_copy_last_entry(copy
));
7109 dst_map
->size
+= size
;
7115 vm_map_copy_insert(dst_map
, last
, copy
);
7117 vm_map_unlock(dst_map
);
7120 * XXX If wiring_required, call vm_map_pageable
7123 return(KERN_SUCCESS
);
7127 * Routine: vm_map_copyin
7130 * see vm_map_copyin_common. Exported via Unsupported.exports.
7134 #undef vm_map_copyin
7139 vm_map_address_t src_addr
,
7141 boolean_t src_destroy
,
7142 vm_map_copy_t
*copy_result
) /* OUT */
7144 return(vm_map_copyin_common(src_map
, src_addr
, len
, src_destroy
,
7145 FALSE
, copy_result
, FALSE
));
7149 * Routine: vm_map_copyin_common
7152 * Copy the specified region (src_addr, len) from the
7153 * source address space (src_map), possibly removing
7154 * the region from the source address space (src_destroy).
7157 * A vm_map_copy_t object (copy_result), suitable for
7158 * insertion into another address space (using vm_map_copyout),
7159 * copying over another address space region (using
7160 * vm_map_copy_overwrite). If the copy is unused, it
7161 * should be destroyed (using vm_map_copy_discard).
7163 * In/out conditions:
7164 * The source map should not be locked on entry.
7167 typedef struct submap_map
{
7168 vm_map_t parent_map
;
7169 vm_map_offset_t base_start
;
7170 vm_map_offset_t base_end
;
7171 vm_map_size_t base_len
;
7172 struct submap_map
*next
;
7176 vm_map_copyin_common(
7178 vm_map_address_t src_addr
,
7180 boolean_t src_destroy
,
7181 __unused boolean_t src_volatile
,
7182 vm_map_copy_t
*copy_result
, /* OUT */
7183 boolean_t use_maxprot
)
7185 vm_map_entry_t tmp_entry
; /* Result of last map lookup --
7186 * in multi-level lookup, this
7187 * entry contains the actual
7191 vm_map_entry_t new_entry
= VM_MAP_ENTRY_NULL
; /* Map entry for copy */
7193 vm_map_offset_t src_start
; /* Start of current entry --
7194 * where copy is taking place now
7196 vm_map_offset_t src_end
; /* End of entire region to be
7198 vm_map_offset_t src_base
;
7199 vm_map_t base_map
= src_map
;
7200 boolean_t map_share
=FALSE
;
7201 submap_map_t
*parent_maps
= NULL
;
7204 vm_map_copy_t copy
; /* Resulting copy */
7205 vm_map_address_t copy_addr
;
7208 * Check for copies of zero bytes.
7212 *copy_result
= VM_MAP_COPY_NULL
;
7213 return(KERN_SUCCESS
);
7217 * Check that the end address doesn't overflow
7219 src_end
= src_addr
+ len
;
7220 if (src_end
< src_addr
)
7221 return KERN_INVALID_ADDRESS
;
7224 * If the copy is sufficiently small, use a kernel buffer instead
7225 * of making a virtual copy. The theory being that the cost of
7226 * setting up VM (and taking C-O-W faults) dominates the copy costs
7227 * for small regions.
7229 if ((len
< msg_ool_size_small
) && !use_maxprot
)
7230 return vm_map_copyin_kernel_buffer(src_map
, src_addr
, len
,
7231 src_destroy
, copy_result
);
7234 * Compute (page aligned) start and end of region
7236 src_start
= vm_map_trunc_page(src_addr
);
7237 src_end
= vm_map_round_page(src_end
);
7239 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);
7242 * Allocate a header element for the list.
7244 * Use the start and end in the header to
7245 * remember the endpoints prior to rounding.
7248 copy
= (vm_map_copy_t
) zalloc(vm_map_copy_zone
);
7249 vm_map_copy_first_entry(copy
) =
7250 vm_map_copy_last_entry(copy
) = vm_map_copy_to_entry(copy
);
7251 copy
->type
= VM_MAP_COPY_ENTRY_LIST
;
7252 copy
->cpy_hdr
.nentries
= 0;
7253 copy
->cpy_hdr
.entries_pageable
= TRUE
;
7255 copy
->offset
= src_addr
;
7258 new_entry
= vm_map_copy_entry_create(copy
);
7262 vm_map_unlock(src_map); \
7263 if(src_map != base_map) \
7264 vm_map_deallocate(src_map); \
7265 if (new_entry != VM_MAP_ENTRY_NULL) \
7266 vm_map_copy_entry_dispose(copy,new_entry); \
7267 vm_map_copy_discard(copy); \
7269 submap_map_t *_ptr; \
7271 for(_ptr = parent_maps; _ptr != NULL; _ptr = parent_maps) { \
7272 parent_maps=parent_maps->next; \
7273 if (_ptr->parent_map != base_map) \
7274 vm_map_deallocate(_ptr->parent_map); \
7275 kfree(_ptr, sizeof(submap_map_t)); \
7282 * Find the beginning of the region.
7285 vm_map_lock(src_map
);
7287 if (!vm_map_lookup_entry(src_map
, src_start
, &tmp_entry
))
7288 RETURN(KERN_INVALID_ADDRESS
);
7289 if(!tmp_entry
->is_sub_map
) {
7290 vm_map_clip_start(src_map
, tmp_entry
, src_start
);
7292 /* set for later submap fix-up */
7293 copy_addr
= src_start
;
7296 * Go through entries until we get to the end.
7301 vm_map_entry_t src_entry
= tmp_entry
; /* Top-level entry */
7302 vm_map_size_t src_size
; /* Size of source
7303 * map entry (in both
7308 vm_object_t src_object
; /* Object to copy */
7309 vm_object_offset_t src_offset
;
7311 boolean_t src_needs_copy
; /* Should source map
7313 * for copy-on-write?
7316 boolean_t new_entry_needs_copy
; /* Will new entry be COW? */
7318 boolean_t was_wired
; /* Was source wired? */
7319 vm_map_version_t version
; /* Version before locks
7320 * dropped to make copy
7322 kern_return_t result
; /* Return value from
7323 * copy_strategically.
7325 while(tmp_entry
->is_sub_map
) {
7326 vm_map_size_t submap_len
;
7329 ptr
= (submap_map_t
*)kalloc(sizeof(submap_map_t
));
7330 ptr
->next
= parent_maps
;
7332 ptr
->parent_map
= src_map
;
7333 ptr
->base_start
= src_start
;
7334 ptr
->base_end
= src_end
;
7335 submap_len
= tmp_entry
->vme_end
- src_start
;
7336 if(submap_len
> (src_end
-src_start
))
7337 submap_len
= src_end
-src_start
;
7338 ptr
->base_len
= submap_len
;
7340 src_start
-= tmp_entry
->vme_start
;
7341 src_start
+= tmp_entry
->offset
;
7342 src_end
= src_start
+ submap_len
;
7343 src_map
= tmp_entry
->object
.sub_map
;
7344 vm_map_lock(src_map
);
7345 /* keep an outstanding reference for all maps in */
7346 /* the parents tree except the base map */
7347 vm_map_reference(src_map
);
7348 vm_map_unlock(ptr
->parent_map
);
7349 if (!vm_map_lookup_entry(
7350 src_map
, src_start
, &tmp_entry
))
7351 RETURN(KERN_INVALID_ADDRESS
);
7353 if(!tmp_entry
->is_sub_map
)
7354 vm_map_clip_start(src_map
, tmp_entry
, src_start
);
7355 src_entry
= tmp_entry
;
7357 /* we are now in the lowest level submap... */
7359 if ((tmp_entry
->object
.vm_object
!= VM_OBJECT_NULL
) &&
7360 (tmp_entry
->object
.vm_object
->phys_contiguous
)) {
7361 /* This is not, supported for now.In future */
7362 /* we will need to detect the phys_contig */
7363 /* condition and then upgrade copy_slowly */
7364 /* to do physical copy from the device mem */
7365 /* based object. We can piggy-back off of */
7366 /* the was wired boolean to set-up the */
7367 /* proper handling */
7368 RETURN(KERN_PROTECTION_FAILURE
);
7371 * Create a new address map entry to hold the result.
7372 * Fill in the fields from the appropriate source entries.
7373 * We must unlock the source map to do this if we need
7374 * to allocate a map entry.
7376 if (new_entry
== VM_MAP_ENTRY_NULL
) {
7377 version
.main_timestamp
= src_map
->timestamp
;
7378 vm_map_unlock(src_map
);
7380 new_entry
= vm_map_copy_entry_create(copy
);
7382 vm_map_lock(src_map
);
7383 if ((version
.main_timestamp
+ 1) != src_map
->timestamp
) {
7384 if (!vm_map_lookup_entry(src_map
, src_start
,
7386 RETURN(KERN_INVALID_ADDRESS
);
7388 if (!tmp_entry
->is_sub_map
)
7389 vm_map_clip_start(src_map
, tmp_entry
, src_start
);
7390 continue; /* restart w/ new tmp_entry */
7395 * Verify that the region can be read.
7397 if (((src_entry
->protection
& VM_PROT_READ
) == VM_PROT_NONE
&&
7399 (src_entry
->max_protection
& VM_PROT_READ
) == 0)
7400 RETURN(KERN_PROTECTION_FAILURE
);
7403 * Clip against the endpoints of the entire region.
7406 vm_map_clip_end(src_map
, src_entry
, src_end
);
7408 src_size
= src_entry
->vme_end
- src_start
;
7409 src_object
= src_entry
->object
.vm_object
;
7410 src_offset
= src_entry
->offset
;
7411 was_wired
= (src_entry
->wired_count
!= 0);
7413 vm_map_entry_copy(new_entry
, src_entry
);
7414 new_entry
->use_pmap
= FALSE
; /* clr address space specifics */
7417 * Attempt non-blocking copy-on-write optimizations.
7421 (src_object
== VM_OBJECT_NULL
||
7422 (src_object
->internal
&& !src_object
->true_share
7425 * If we are destroying the source, and the object
7426 * is internal, we can move the object reference
7427 * from the source to the copy. The copy is
7428 * copy-on-write only if the source is.
7429 * We make another reference to the object, because
7430 * destroying the source entry will deallocate it.
7432 vm_object_reference(src_object
);
7435 * Copy is always unwired. vm_map_copy_entry
7436 * set its wired count to zero.
7439 goto CopySuccessful
;
7444 XPR(XPR_VM_MAP
, "vm_map_copyin_common src_obj 0x%x ent 0x%x obj 0x%x was_wired %d\n",
7445 src_object
, new_entry
, new_entry
->object
.vm_object
,
7447 if ((src_object
== VM_OBJECT_NULL
||
7448 (!was_wired
&& !map_share
&& !tmp_entry
->is_shared
)) &&
7449 vm_object_copy_quickly(
7450 &new_entry
->object
.vm_object
,
7454 &new_entry_needs_copy
)) {
7456 new_entry
->needs_copy
= new_entry_needs_copy
;
7459 * Handle copy-on-write obligations
7462 if (src_needs_copy
&& !tmp_entry
->needs_copy
) {
7465 prot
= src_entry
->protection
& ~VM_PROT_WRITE
;
7467 if (override_nx(src_map
, src_entry
->alias
) && prot
)
7468 prot
|= VM_PROT_EXECUTE
;
7470 vm_object_pmap_protect(
7474 (src_entry
->is_shared
?
7477 src_entry
->vme_start
,
7480 tmp_entry
->needs_copy
= TRUE
;
7484 * The map has never been unlocked, so it's safe
7485 * to move to the next entry rather than doing
7489 goto CopySuccessful
;
7493 * Take an object reference, so that we may
7494 * release the map lock(s).
7497 assert(src_object
!= VM_OBJECT_NULL
);
7498 vm_object_reference(src_object
);
7501 * Record the timestamp for later verification.
7505 version
.main_timestamp
= src_map
->timestamp
;
7506 vm_map_unlock(src_map
); /* Increments timestamp once! */
7514 vm_object_lock(src_object
);
7515 result
= vm_object_copy_slowly(
7520 &new_entry
->object
.vm_object
);
7521 new_entry
->offset
= 0;
7522 new_entry
->needs_copy
= FALSE
;
7525 else if (src_object
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
&&
7526 (tmp_entry
->is_shared
|| map_share
)) {
7527 vm_object_t new_object
;
7529 vm_object_lock_shared(src_object
);
7530 new_object
= vm_object_copy_delayed(
7535 if (new_object
== VM_OBJECT_NULL
)
7538 new_entry
->object
.vm_object
= new_object
;
7539 new_entry
->needs_copy
= TRUE
;
7540 result
= KERN_SUCCESS
;
7543 result
= vm_object_copy_strategically(src_object
,
7546 &new_entry
->object
.vm_object
,
7548 &new_entry_needs_copy
);
7550 new_entry
->needs_copy
= new_entry_needs_copy
;
7553 if (result
!= KERN_SUCCESS
&&
7554 result
!= KERN_MEMORY_RESTART_COPY
) {
7555 vm_map_lock(src_map
);
7560 * Throw away the extra reference
7563 vm_object_deallocate(src_object
);
7566 * Verify that the map has not substantially
7567 * changed while the copy was being made.
7570 vm_map_lock(src_map
);
7572 if ((version
.main_timestamp
+ 1) == src_map
->timestamp
)
7573 goto VerificationSuccessful
;
7576 * Simple version comparison failed.
7578 * Retry the lookup and verify that the
7579 * same object/offset are still present.
7581 * [Note: a memory manager that colludes with
7582 * the calling task can detect that we have
7583 * cheated. While the map was unlocked, the
7584 * mapping could have been changed and restored.]
7587 if (!vm_map_lookup_entry(src_map
, src_start
, &tmp_entry
)) {
7588 RETURN(KERN_INVALID_ADDRESS
);
7591 src_entry
= tmp_entry
;
7592 vm_map_clip_start(src_map
, src_entry
, src_start
);
7594 if ((((src_entry
->protection
& VM_PROT_READ
) == VM_PROT_NONE
) &&
7596 ((src_entry
->max_protection
& VM_PROT_READ
) == 0))
7597 goto VerificationFailed
;
7599 if (src_entry
->vme_end
< new_entry
->vme_end
)
7600 src_size
= (new_entry
->vme_end
= src_entry
->vme_end
) - src_start
;
7602 if ((src_entry
->object
.vm_object
!= src_object
) ||
7603 (src_entry
->offset
!= src_offset
) ) {
7606 * Verification failed.
7608 * Start over with this top-level entry.
7611 VerificationFailed
: ;
7613 vm_object_deallocate(new_entry
->object
.vm_object
);
7614 tmp_entry
= src_entry
;
7619 * Verification succeeded.
7622 VerificationSuccessful
: ;
7624 if (result
== KERN_MEMORY_RESTART_COPY
)
7634 * Link in the new copy entry.
7637 vm_map_copy_entry_link(copy
, vm_map_copy_last_entry(copy
),
7641 * Determine whether the entire region
7644 src_base
= src_start
;
7645 src_start
= new_entry
->vme_end
;
7646 new_entry
= VM_MAP_ENTRY_NULL
;
7647 while ((src_start
>= src_end
) && (src_end
!= 0)) {
7648 if (src_map
!= base_map
) {
7652 assert(ptr
!= NULL
);
7653 parent_maps
= parent_maps
->next
;
7655 /* fix up the damage we did in that submap */
7656 vm_map_simplify_range(src_map
,
7660 vm_map_unlock(src_map
);
7661 vm_map_deallocate(src_map
);
7662 vm_map_lock(ptr
->parent_map
);
7663 src_map
= ptr
->parent_map
;
7664 src_base
= ptr
->base_start
;
7665 src_start
= ptr
->base_start
+ ptr
->base_len
;
7666 src_end
= ptr
->base_end
;
7667 if ((src_end
> src_start
) &&
7668 !vm_map_lookup_entry(
7669 src_map
, src_start
, &tmp_entry
))
7670 RETURN(KERN_INVALID_ADDRESS
);
7671 kfree(ptr
, sizeof(submap_map_t
));
7672 if(parent_maps
== NULL
)
7674 src_entry
= tmp_entry
->vme_prev
;
7678 if ((src_start
>= src_end
) && (src_end
!= 0))
7682 * Verify that there are no gaps in the region
7685 tmp_entry
= src_entry
->vme_next
;
7686 if ((tmp_entry
->vme_start
!= src_start
) ||
7687 (tmp_entry
== vm_map_to_entry(src_map
)))
7688 RETURN(KERN_INVALID_ADDRESS
);
7692 * If the source should be destroyed, do it now, since the
7693 * copy was successful.
7696 (void) vm_map_delete(src_map
,
7697 vm_map_trunc_page(src_addr
),
7699 (src_map
== kernel_map
) ?
7700 VM_MAP_REMOVE_KUNWIRE
:
7704 /* fix up the damage we did in the base map */
7705 vm_map_simplify_range(src_map
,
7706 vm_map_trunc_page(src_addr
),
7707 vm_map_round_page(src_end
));
7710 vm_map_unlock(src_map
);
7712 /* Fix-up start and end points in copy. This is necessary */
7713 /* when the various entries in the copy object were picked */
7714 /* up from different sub-maps */
7716 tmp_entry
= vm_map_copy_first_entry(copy
);
7717 while (tmp_entry
!= vm_map_copy_to_entry(copy
)) {
7718 tmp_entry
->vme_end
= copy_addr
+
7719 (tmp_entry
->vme_end
- tmp_entry
->vme_start
);
7720 tmp_entry
->vme_start
= copy_addr
;
7721 copy_addr
+= tmp_entry
->vme_end
- tmp_entry
->vme_start
;
7722 tmp_entry
= (struct vm_map_entry
*)tmp_entry
->vme_next
;
7725 *copy_result
= copy
;
7726 return(KERN_SUCCESS
);
7732 * vm_map_copyin_object:
7734 * Create a copy object from an object.
7735 * Our caller donates an object reference.
7739 vm_map_copyin_object(
7741 vm_object_offset_t offset
, /* offset of region in object */
7742 vm_object_size_t size
, /* size of region in object */
7743 vm_map_copy_t
*copy_result
) /* OUT */
7745 vm_map_copy_t copy
; /* Resulting copy */
7748 * We drop the object into a special copy object
7749 * that contains the object directly.
7752 copy
= (vm_map_copy_t
) zalloc(vm_map_copy_zone
);
7753 copy
->type
= VM_MAP_COPY_OBJECT
;
7754 copy
->cpy_object
= object
;
7755 copy
->offset
= offset
;
7758 *copy_result
= copy
;
7759 return(KERN_SUCCESS
);
7765 vm_map_entry_t old_entry
,
7769 vm_map_entry_t new_entry
;
7772 * New sharing code. New map entry
7773 * references original object. Internal
7774 * objects use asynchronous copy algorithm for
7775 * future copies. First make sure we have
7776 * the right object. If we need a shadow,
7777 * or someone else already has one, then
7778 * make a new shadow and share it.
7781 object
= old_entry
->object
.vm_object
;
7782 if (old_entry
->is_sub_map
) {
7783 assert(old_entry
->wired_count
== 0);
7784 #ifndef NO_NESTED_PMAP
7785 if(old_entry
->use_pmap
) {
7786 kern_return_t result
;
7788 result
= pmap_nest(new_map
->pmap
,
7789 (old_entry
->object
.sub_map
)->pmap
,
7790 (addr64_t
)old_entry
->vme_start
,
7791 (addr64_t
)old_entry
->vme_start
,
7792 (uint64_t)(old_entry
->vme_end
- old_entry
->vme_start
));
7794 panic("vm_map_fork_share: pmap_nest failed!");
7796 #endif /* NO_NESTED_PMAP */
7797 } else if (object
== VM_OBJECT_NULL
) {
7798 object
= vm_object_allocate((vm_map_size_t
)(old_entry
->vme_end
-
7799 old_entry
->vme_start
));
7800 old_entry
->offset
= 0;
7801 old_entry
->object
.vm_object
= object
;
7802 assert(!old_entry
->needs_copy
);
7803 } else if (object
->copy_strategy
!=
7804 MEMORY_OBJECT_COPY_SYMMETRIC
) {
7807 * We are already using an asymmetric
7808 * copy, and therefore we already have
7812 assert(! old_entry
->needs_copy
);
7814 else if (old_entry
->needs_copy
|| /* case 1 */
7815 object
->shadowed
|| /* case 2 */
7816 (!object
->true_share
&& /* case 3 */
7817 !old_entry
->is_shared
&&
7819 (vm_map_size_t
)(old_entry
->vme_end
-
7820 old_entry
->vme_start
)))) {
7823 * We need to create a shadow.
7824 * There are three cases here.
7825 * In the first case, we need to
7826 * complete a deferred symmetrical
7827 * copy that we participated in.
7828 * In the second and third cases,
7829 * we need to create the shadow so
7830 * that changes that we make to the
7831 * object do not interfere with
7832 * any symmetrical copies which
7833 * have occured (case 2) or which
7834 * might occur (case 3).
7836 * The first case is when we had
7837 * deferred shadow object creation
7838 * via the entry->needs_copy mechanism.
7839 * This mechanism only works when
7840 * only one entry points to the source
7841 * object, and we are about to create
7842 * a second entry pointing to the
7843 * same object. The problem is that
7844 * there is no way of mapping from
7845 * an object to the entries pointing
7846 * to it. (Deferred shadow creation
7847 * works with one entry because occurs
7848 * at fault time, and we walk from the
7849 * entry to the object when handling
7852 * The second case is when the object
7853 * to be shared has already been copied
7854 * with a symmetric copy, but we point
7855 * directly to the object without
7856 * needs_copy set in our entry. (This
7857 * can happen because different ranges
7858 * of an object can be pointed to by
7859 * different entries. In particular,
7860 * a single entry pointing to an object
7861 * can be split by a call to vm_inherit,
7862 * which, combined with task_create, can
7863 * result in the different entries
7864 * having different needs_copy values.)
7865 * The shadowed flag in the object allows
7866 * us to detect this case. The problem
7867 * with this case is that if this object
7868 * has or will have shadows, then we
7869 * must not perform an asymmetric copy
7870 * of this object, since such a copy
7871 * allows the object to be changed, which
7872 * will break the previous symmetrical
7873 * copies (which rely upon the object
7874 * not changing). In a sense, the shadowed
7875 * flag says "don't change this object".
7876 * We fix this by creating a shadow
7877 * object for this object, and sharing
7878 * that. This works because we are free
7879 * to change the shadow object (and thus
7880 * to use an asymmetric copy strategy);
7881 * this is also semantically correct,
7882 * since this object is temporary, and
7883 * therefore a copy of the object is
7884 * as good as the object itself. (This
7885 * is not true for permanent objects,
7886 * since the pager needs to see changes,
7887 * which won't happen if the changes
7888 * are made to a copy.)
7890 * The third case is when the object
7891 * to be shared has parts sticking
7892 * outside of the entry we're working
7893 * with, and thus may in the future
7894 * be subject to a symmetrical copy.
7895 * (This is a preemptive version of
7899 vm_object_shadow(&old_entry
->object
.vm_object
,
7901 (vm_map_size_t
) (old_entry
->vme_end
-
7902 old_entry
->vme_start
));
7905 * If we're making a shadow for other than
7906 * copy on write reasons, then we have
7907 * to remove write permission.
7910 if (!old_entry
->needs_copy
&&
7911 (old_entry
->protection
& VM_PROT_WRITE
)) {
7914 prot
= old_entry
->protection
& ~VM_PROT_WRITE
;
7916 if (override_nx(old_map
, old_entry
->alias
) && prot
)
7917 prot
|= VM_PROT_EXECUTE
;
7919 if (old_map
->mapped
) {
7920 vm_object_pmap_protect(
7921 old_entry
->object
.vm_object
,
7923 (old_entry
->vme_end
-
7924 old_entry
->vme_start
),
7926 old_entry
->vme_start
,
7929 pmap_protect(old_map
->pmap
,
7930 old_entry
->vme_start
,
7936 old_entry
->needs_copy
= FALSE
;
7937 object
= old_entry
->object
.vm_object
;
7941 * If object was using a symmetric copy strategy,
7942 * change its copy strategy to the default
7943 * asymmetric copy strategy, which is copy_delay
7944 * in the non-norma case and copy_call in the
7945 * norma case. Bump the reference count for the
7949 if(old_entry
->is_sub_map
) {
7950 vm_map_lock(old_entry
->object
.sub_map
);
7951 vm_map_reference(old_entry
->object
.sub_map
);
7952 vm_map_unlock(old_entry
->object
.sub_map
);
7954 vm_object_lock(object
);
7955 vm_object_reference_locked(object
);
7956 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
) {
7957 object
->copy_strategy
= MEMORY_OBJECT_COPY_DELAY
;
7959 vm_object_unlock(object
);
7963 * Clone the entry, using object ref from above.
7964 * Mark both entries as shared.
7967 new_entry
= vm_map_entry_create(new_map
);
7968 vm_map_entry_copy(new_entry
, old_entry
);
7969 old_entry
->is_shared
= TRUE
;
7970 new_entry
->is_shared
= TRUE
;
7973 * Insert the entry into the new map -- we
7974 * know we're inserting at the end of the new
7978 vm_map_entry_link(new_map
, vm_map_last_entry(new_map
), new_entry
);
7981 * Update the physical map
7984 if (old_entry
->is_sub_map
) {
7985 /* Bill Angell pmap support goes here */
7987 pmap_copy(new_map
->pmap
, old_map
->pmap
, new_entry
->vme_start
,
7988 old_entry
->vme_end
- old_entry
->vme_start
,
7989 old_entry
->vme_start
);
7996 vm_map_entry_t
*old_entry_p
,
7999 vm_map_entry_t old_entry
= *old_entry_p
;
8000 vm_map_size_t entry_size
= old_entry
->vme_end
- old_entry
->vme_start
;
8001 vm_map_offset_t start
= old_entry
->vme_start
;
8003 vm_map_entry_t last
= vm_map_last_entry(new_map
);
8005 vm_map_unlock(old_map
);
8007 * Use maxprot version of copyin because we
8008 * care about whether this memory can ever
8009 * be accessed, not just whether it's accessible
8012 if (vm_map_copyin_maxprot(old_map
, start
, entry_size
, FALSE
, ©
)
8015 * The map might have changed while it
8016 * was unlocked, check it again. Skip
8017 * any blank space or permanently
8018 * unreadable region.
8020 vm_map_lock(old_map
);
8021 if (!vm_map_lookup_entry(old_map
, start
, &last
) ||
8022 (last
->max_protection
& VM_PROT_READ
) == VM_PROT_NONE
) {
8023 last
= last
->vme_next
;
8025 *old_entry_p
= last
;
8028 * XXX For some error returns, want to
8029 * XXX skip to the next element. Note
8030 * that INVALID_ADDRESS and
8031 * PROTECTION_FAILURE are handled above.
8038 * Insert the copy into the new map
8041 vm_map_copy_insert(new_map
, last
, copy
);
8044 * Pick up the traversal at the end of
8045 * the copied region.
8048 vm_map_lock(old_map
);
8049 start
+= entry_size
;
8050 if (! vm_map_lookup_entry(old_map
, start
, &last
)) {
8051 last
= last
->vme_next
;
8053 if (last
->vme_start
== start
) {
8055 * No need to clip here and we don't
8056 * want to cause any unnecessary
8060 vm_map_clip_start(old_map
, last
, start
);
8063 *old_entry_p
= last
;
8071 * Create and return a new map based on the old
8072 * map, according to the inheritance values on the
8073 * regions in that map.
8075 * The source map must not be locked.
8083 vm_map_entry_t old_entry
;
8084 vm_map_size_t new_size
= 0, entry_size
;
8085 vm_map_entry_t new_entry
;
8086 boolean_t src_needs_copy
;
8087 boolean_t new_entry_needs_copy
;
8089 new_pmap
= pmap_create((vm_map_size_t
) 0,
8090 #if defined(__i386__) || defined(__x86_64__)
8091 old_map
->pmap
->pm_task_map
!= TASK_MAP_32BIT
8096 #if defined(__i386__)
8097 if (old_map
->pmap
->pm_task_map
== TASK_MAP_64BIT_SHARED
)
8098 pmap_set_4GB_pagezero(new_pmap
);
8101 vm_map_reference_swap(old_map
);
8102 vm_map_lock(old_map
);
8104 new_map
= vm_map_create(new_pmap
,
8105 old_map
->min_offset
,
8106 old_map
->max_offset
,
8107 old_map
->hdr
.entries_pageable
);
8110 old_entry
= vm_map_first_entry(old_map
);
8111 old_entry
!= vm_map_to_entry(old_map
);
8114 entry_size
= old_entry
->vme_end
- old_entry
->vme_start
;
8116 switch (old_entry
->inheritance
) {
8117 case VM_INHERIT_NONE
:
8120 case VM_INHERIT_SHARE
:
8121 vm_map_fork_share(old_map
, old_entry
, new_map
);
8122 new_size
+= entry_size
;
8125 case VM_INHERIT_COPY
:
8128 * Inline the copy_quickly case;
8129 * upon failure, fall back on call
8130 * to vm_map_fork_copy.
8133 if(old_entry
->is_sub_map
)
8135 if ((old_entry
->wired_count
!= 0) ||
8136 ((old_entry
->object
.vm_object
!= NULL
) &&
8137 (old_entry
->object
.vm_object
->true_share
))) {
8138 goto slow_vm_map_fork_copy
;
8141 new_entry
= vm_map_entry_create(new_map
);
8142 vm_map_entry_copy(new_entry
, old_entry
);
8143 /* clear address space specifics */
8144 new_entry
->use_pmap
= FALSE
;
8146 if (! vm_object_copy_quickly(
8147 &new_entry
->object
.vm_object
,
8149 (old_entry
->vme_end
-
8150 old_entry
->vme_start
),
8152 &new_entry_needs_copy
)) {
8153 vm_map_entry_dispose(new_map
, new_entry
);
8154 goto slow_vm_map_fork_copy
;
8158 * Handle copy-on-write obligations
8161 if (src_needs_copy
&& !old_entry
->needs_copy
) {
8164 prot
= old_entry
->protection
& ~VM_PROT_WRITE
;
8166 if (override_nx(old_map
, old_entry
->alias
) && prot
)
8167 prot
|= VM_PROT_EXECUTE
;
8169 vm_object_pmap_protect(
8170 old_entry
->object
.vm_object
,
8172 (old_entry
->vme_end
-
8173 old_entry
->vme_start
),
8174 ((old_entry
->is_shared
8178 old_entry
->vme_start
,
8181 old_entry
->needs_copy
= TRUE
;
8183 new_entry
->needs_copy
= new_entry_needs_copy
;
8186 * Insert the entry at the end
8190 vm_map_entry_link(new_map
, vm_map_last_entry(new_map
),
8192 new_size
+= entry_size
;
8195 slow_vm_map_fork_copy
:
8196 if (vm_map_fork_copy(old_map
, &old_entry
, new_map
)) {
8197 new_size
+= entry_size
;
8201 old_entry
= old_entry
->vme_next
;
8204 new_map
->size
= new_size
;
8205 vm_map_unlock(old_map
);
8206 vm_map_deallocate(old_map
);
8214 * Setup the "new_map" with the proper execution environment according
8215 * to the type of executable (platform, 64bit, chroot environment).
8216 * Map the comm page and shared region, etc...
8225 SHARED_REGION_TRACE_DEBUG(
8226 ("shared_region: task %p: vm_map_exec(%p,%p,%p,0x%x): ->\n",
8227 current_task(), new_map
, task
, fsroot
, cpu
));
8228 (void) vm_commpage_enter(new_map
, task
);
8229 (void) vm_shared_region_enter(new_map
, task
, fsroot
, cpu
);
8230 SHARED_REGION_TRACE_DEBUG(
8231 ("shared_region: task %p: vm_map_exec(%p,%p,%p,0x%x): <-\n",
8232 current_task(), new_map
, task
, fsroot
, cpu
));
8233 return KERN_SUCCESS
;
8237 * vm_map_lookup_locked:
8239 * Finds the VM object, offset, and
8240 * protection for a given virtual address in the
8241 * specified map, assuming a page fault of the
8244 * Returns the (object, offset, protection) for
8245 * this address, whether it is wired down, and whether
8246 * this map has the only reference to the data in question.
8247 * In order to later verify this lookup, a "version"
8250 * The map MUST be locked by the caller and WILL be
8251 * locked on exit. In order to guarantee the
8252 * existence of the returned object, it is returned
8255 * If a lookup is requested with "write protection"
8256 * specified, the map may be changed to perform virtual
8257 * copying operations, although the data referenced will
8261 vm_map_lookup_locked(
8262 vm_map_t
*var_map
, /* IN/OUT */
8263 vm_map_offset_t vaddr
,
8264 vm_prot_t fault_type
,
8265 int object_lock_type
,
8266 vm_map_version_t
*out_version
, /* OUT */
8267 vm_object_t
*object
, /* OUT */
8268 vm_object_offset_t
*offset
, /* OUT */
8269 vm_prot_t
*out_prot
, /* OUT */
8270 boolean_t
*wired
, /* OUT */
8271 vm_object_fault_info_t fault_info
, /* OUT */
8274 vm_map_entry_t entry
;
8275 register vm_map_t map
= *var_map
;
8276 vm_map_t old_map
= *var_map
;
8277 vm_map_t cow_sub_map_parent
= VM_MAP_NULL
;
8278 vm_map_offset_t cow_parent_vaddr
= 0;
8279 vm_map_offset_t old_start
= 0;
8280 vm_map_offset_t old_end
= 0;
8281 register vm_prot_t prot
;
8287 * If the map has an interesting hint, try it before calling
8288 * full blown lookup routine.
8292 if ((entry
== vm_map_to_entry(map
)) ||
8293 (vaddr
< entry
->vme_start
) || (vaddr
>= entry
->vme_end
)) {
8294 vm_map_entry_t tmp_entry
;
8297 * Entry was either not a valid hint, or the vaddr
8298 * was not contained in the entry, so do a full lookup.
8300 if (!vm_map_lookup_entry(map
, vaddr
, &tmp_entry
)) {
8301 if((cow_sub_map_parent
) && (cow_sub_map_parent
!= map
))
8302 vm_map_unlock(cow_sub_map_parent
);
8303 if((*real_map
!= map
)
8304 && (*real_map
!= cow_sub_map_parent
))
8305 vm_map_unlock(*real_map
);
8306 return KERN_INVALID_ADDRESS
;
8311 if(map
== old_map
) {
8312 old_start
= entry
->vme_start
;
8313 old_end
= entry
->vme_end
;
8317 * Handle submaps. Drop lock on upper map, submap is
8322 if (entry
->is_sub_map
) {
8323 vm_map_offset_t local_vaddr
;
8324 vm_map_offset_t end_delta
;
8325 vm_map_offset_t start_delta
;
8326 vm_map_entry_t submap_entry
;
8327 boolean_t mapped_needs_copy
=FALSE
;
8329 local_vaddr
= vaddr
;
8331 if ((entry
->use_pmap
&& !(fault_type
& VM_PROT_WRITE
))) {
8332 /* if real_map equals map we unlock below */
8333 if ((*real_map
!= map
) &&
8334 (*real_map
!= cow_sub_map_parent
))
8335 vm_map_unlock(*real_map
);
8336 *real_map
= entry
->object
.sub_map
;
8339 if(entry
->needs_copy
&& (fault_type
& VM_PROT_WRITE
)) {
8340 if (!mapped_needs_copy
) {
8341 if (vm_map_lock_read_to_write(map
)) {
8342 vm_map_lock_read(map
);
8343 /* XXX FBDP: entry still valid ? */
8344 if(*real_map
== entry
->object
.sub_map
)
8348 vm_map_lock_read(entry
->object
.sub_map
);
8349 cow_sub_map_parent
= map
;
8350 /* reset base to map before cow object */
8351 /* this is the map which will accept */
8352 /* the new cow object */
8353 old_start
= entry
->vme_start
;
8354 old_end
= entry
->vme_end
;
8355 cow_parent_vaddr
= vaddr
;
8356 mapped_needs_copy
= TRUE
;
8358 vm_map_lock_read(entry
->object
.sub_map
);
8359 if((cow_sub_map_parent
!= map
) &&
8364 vm_map_lock_read(entry
->object
.sub_map
);
8365 /* leave map locked if it is a target */
8366 /* cow sub_map above otherwise, just */
8367 /* follow the maps down to the object */
8368 /* here we unlock knowing we are not */
8369 /* revisiting the map. */
8370 if((*real_map
!= map
) && (map
!= cow_sub_map_parent
))
8371 vm_map_unlock_read(map
);
8374 /* XXX FBDP: map has been unlocked, what protects "entry" !? */
8375 *var_map
= map
= entry
->object
.sub_map
;
8377 /* calculate the offset in the submap for vaddr */
8378 local_vaddr
= (local_vaddr
- entry
->vme_start
) + entry
->offset
;
8381 if(!vm_map_lookup_entry(map
, local_vaddr
, &submap_entry
)) {
8382 if((cow_sub_map_parent
) && (cow_sub_map_parent
!= map
)){
8383 vm_map_unlock(cow_sub_map_parent
);
8385 if((*real_map
!= map
)
8386 && (*real_map
!= cow_sub_map_parent
)) {
8387 vm_map_unlock(*real_map
);
8390 return KERN_INVALID_ADDRESS
;
8393 /* find the attenuated shadow of the underlying object */
8394 /* on our target map */
8396 /* in english the submap object may extend beyond the */
8397 /* region mapped by the entry or, may only fill a portion */
8398 /* of it. For our purposes, we only care if the object */
8399 /* doesn't fill. In this case the area which will */
8400 /* ultimately be clipped in the top map will only need */
8401 /* to be as big as the portion of the underlying entry */
8402 /* which is mapped */
8403 start_delta
= submap_entry
->vme_start
> entry
->offset
?
8404 submap_entry
->vme_start
- entry
->offset
: 0;
8407 (entry
->offset
+ start_delta
+ (old_end
- old_start
)) <=
8408 submap_entry
->vme_end
?
8409 0 : (entry
->offset
+
8410 (old_end
- old_start
))
8411 - submap_entry
->vme_end
;
8413 old_start
+= start_delta
;
8414 old_end
-= end_delta
;
8416 if(submap_entry
->is_sub_map
) {
8417 entry
= submap_entry
;
8418 vaddr
= local_vaddr
;
8419 goto submap_recurse
;
8422 if(((fault_type
& VM_PROT_WRITE
) && cow_sub_map_parent
)) {
8424 vm_object_t sub_object
, copy_object
;
8425 vm_object_offset_t copy_offset
;
8426 vm_map_offset_t local_start
;
8427 vm_map_offset_t local_end
;
8428 boolean_t copied_slowly
= FALSE
;
8430 if (vm_map_lock_read_to_write(map
)) {
8431 vm_map_lock_read(map
);
8432 old_start
-= start_delta
;
8433 old_end
+= end_delta
;
8438 sub_object
= submap_entry
->object
.vm_object
;
8439 if (sub_object
== VM_OBJECT_NULL
) {
8443 (submap_entry
->vme_end
-
8444 submap_entry
->vme_start
));
8445 submap_entry
->object
.vm_object
= sub_object
;
8446 submap_entry
->offset
= 0;
8448 local_start
= local_vaddr
-
8449 (cow_parent_vaddr
- old_start
);
8450 local_end
= local_vaddr
+
8451 (old_end
- cow_parent_vaddr
);
8452 vm_map_clip_start(map
, submap_entry
, local_start
);
8453 vm_map_clip_end(map
, submap_entry
, local_end
);
8454 /* unnesting was done in vm_map_clip_start/end() */
8455 assert(!submap_entry
->use_pmap
);
8457 /* This is the COW case, lets connect */
8458 /* an entry in our space to the underlying */
8459 /* object in the submap, bypassing the */
8463 if(submap_entry
->wired_count
!= 0 ||
8464 (sub_object
->copy_strategy
==
8465 MEMORY_OBJECT_COPY_NONE
)) {
8466 vm_object_lock(sub_object
);
8467 vm_object_copy_slowly(sub_object
,
8468 submap_entry
->offset
,
8469 (submap_entry
->vme_end
-
8470 submap_entry
->vme_start
),
8473 copied_slowly
= TRUE
;
8476 /* set up shadow object */
8477 copy_object
= sub_object
;
8478 vm_object_reference(copy_object
);
8479 sub_object
->shadowed
= TRUE
;
8480 submap_entry
->needs_copy
= TRUE
;
8482 prot
= submap_entry
->protection
& ~VM_PROT_WRITE
;
8484 if (override_nx(map
, submap_entry
->alias
) && prot
)
8485 prot
|= VM_PROT_EXECUTE
;
8487 vm_object_pmap_protect(
8489 submap_entry
->offset
,
8490 submap_entry
->vme_end
-
8491 submap_entry
->vme_start
,
8492 (submap_entry
->is_shared
8494 PMAP_NULL
: map
->pmap
,
8495 submap_entry
->vme_start
,
8500 * Adjust the fault offset to the submap entry.
8502 copy_offset
= (local_vaddr
-
8503 submap_entry
->vme_start
+
8504 submap_entry
->offset
);
8506 /* This works diffently than the */
8507 /* normal submap case. We go back */
8508 /* to the parent of the cow map and*/
8509 /* clip out the target portion of */
8510 /* the sub_map, substituting the */
8511 /* new copy object, */
8514 local_start
= old_start
;
8515 local_end
= old_end
;
8516 map
= cow_sub_map_parent
;
8517 *var_map
= cow_sub_map_parent
;
8518 vaddr
= cow_parent_vaddr
;
8519 cow_sub_map_parent
= NULL
;
8521 if(!vm_map_lookup_entry(map
,
8523 vm_object_deallocate(
8525 vm_map_lock_write_to_read(map
);
8526 return KERN_INVALID_ADDRESS
;
8529 /* clip out the portion of space */
8530 /* mapped by the sub map which */
8531 /* corresponds to the underlying */
8535 * Clip (and unnest) the smallest nested chunk
8536 * possible around the faulting address...
8538 local_start
= vaddr
& ~(pmap_nesting_size_min
- 1);
8539 local_end
= local_start
+ pmap_nesting_size_min
;
8541 * ... but don't go beyond the "old_start" to "old_end"
8542 * range, to avoid spanning over another VM region
8543 * with a possibly different VM object and/or offset.
8545 if (local_start
< old_start
) {
8546 local_start
= old_start
;
8548 if (local_end
> old_end
) {
8549 local_end
= old_end
;
8552 * Adjust copy_offset to the start of the range.
8554 copy_offset
-= (vaddr
- local_start
);
8556 vm_map_clip_start(map
, entry
, local_start
);
8557 vm_map_clip_end(map
, entry
, local_end
);
8558 /* unnesting was done in vm_map_clip_start/end() */
8559 assert(!entry
->use_pmap
);
8561 /* substitute copy object for */
8562 /* shared map entry */
8563 vm_map_deallocate(entry
->object
.sub_map
);
8564 entry
->is_sub_map
= FALSE
;
8565 entry
->object
.vm_object
= copy_object
;
8567 /* propagate the submap entry's protections */
8568 entry
->protection
|= submap_entry
->protection
;
8569 entry
->max_protection
|= submap_entry
->max_protection
;
8572 entry
->offset
= local_start
- old_start
;
8573 entry
->needs_copy
= FALSE
;
8574 entry
->is_shared
= FALSE
;
8576 entry
->offset
= copy_offset
;
8577 entry
->needs_copy
= TRUE
;
8578 if(entry
->inheritance
== VM_INHERIT_SHARE
)
8579 entry
->inheritance
= VM_INHERIT_COPY
;
8581 entry
->is_shared
= TRUE
;
8583 if(entry
->inheritance
== VM_INHERIT_SHARE
)
8584 entry
->inheritance
= VM_INHERIT_COPY
;
8586 vm_map_lock_write_to_read(map
);
8588 if((cow_sub_map_parent
)
8589 && (cow_sub_map_parent
!= *real_map
)
8590 && (cow_sub_map_parent
!= map
)) {
8591 vm_map_unlock(cow_sub_map_parent
);
8593 entry
= submap_entry
;
8594 vaddr
= local_vaddr
;
8599 * Check whether this task is allowed to have
8603 prot
= entry
->protection
;
8605 if (override_nx(map
, entry
->alias
) && prot
) {
8607 * HACK -- if not a stack, then allow execution
8609 prot
|= VM_PROT_EXECUTE
;
8612 if ((fault_type
& (prot
)) != fault_type
) {
8613 if (*real_map
!= map
) {
8614 vm_map_unlock(*real_map
);
8618 if ((fault_type
& VM_PROT_EXECUTE
) && prot
)
8619 log_stack_execution_failure((addr64_t
)vaddr
, prot
);
8621 DTRACE_VM2(prot_fault
, int, 1, (uint64_t *), NULL
);
8622 return KERN_PROTECTION_FAILURE
;
8626 * If this page is not pageable, we have to get
8627 * it for all possible accesses.
8630 *wired
= (entry
->wired_count
!= 0);
8635 * If the entry was copy-on-write, we either ...
8638 if (entry
->needs_copy
) {
8640 * If we want to write the page, we may as well
8641 * handle that now since we've got the map locked.
8643 * If we don't need to write the page, we just
8644 * demote the permissions allowed.
8647 if ((fault_type
& VM_PROT_WRITE
) || *wired
) {
8649 * Make a new object, and place it in the
8650 * object chain. Note that no new references
8651 * have appeared -- one just moved from the
8652 * map to the new object.
8655 if (vm_map_lock_read_to_write(map
)) {
8656 vm_map_lock_read(map
);
8659 vm_object_shadow(&entry
->object
.vm_object
,
8661 (vm_map_size_t
) (entry
->vme_end
-
8664 entry
->object
.vm_object
->shadowed
= TRUE
;
8665 entry
->needs_copy
= FALSE
;
8666 vm_map_lock_write_to_read(map
);
8670 * We're attempting to read a copy-on-write
8671 * page -- don't allow writes.
8674 prot
&= (~VM_PROT_WRITE
);
8679 * Create an object if necessary.
8681 if (entry
->object
.vm_object
== VM_OBJECT_NULL
) {
8683 if (vm_map_lock_read_to_write(map
)) {
8684 vm_map_lock_read(map
);
8688 entry
->object
.vm_object
= vm_object_allocate(
8689 (vm_map_size_t
)(entry
->vme_end
- entry
->vme_start
));
8691 vm_map_lock_write_to_read(map
);
8695 * Return the object/offset from this entry. If the entry
8696 * was copy-on-write or empty, it has been fixed up. Also
8697 * return the protection.
8700 *offset
= (vaddr
- entry
->vme_start
) + entry
->offset
;
8701 *object
= entry
->object
.vm_object
;
8705 fault_info
->interruptible
= THREAD_UNINT
; /* for now... */
8706 /* ... the caller will change "interruptible" if needed */
8707 fault_info
->cluster_size
= 0;
8708 fault_info
->user_tag
= entry
->alias
;
8709 fault_info
->behavior
= entry
->behavior
;
8710 fault_info
->lo_offset
= entry
->offset
;
8711 fault_info
->hi_offset
= (entry
->vme_end
- entry
->vme_start
) + entry
->offset
;
8712 fault_info
->no_cache
= entry
->no_cache
;
8713 fault_info
->stealth
= FALSE
;
8717 * Lock the object to prevent it from disappearing
8719 if (object_lock_type
== OBJECT_LOCK_EXCLUSIVE
)
8720 vm_object_lock(*object
);
8722 vm_object_lock_shared(*object
);
8725 * Save the version number
8728 out_version
->main_timestamp
= map
->timestamp
;
8730 return KERN_SUCCESS
;
8737 * Verifies that the map in question has not changed
8738 * since the given version. If successful, the map
8739 * will not change until vm_map_verify_done() is called.
8743 register vm_map_t map
,
8744 register vm_map_version_t
*version
) /* REF */
8748 vm_map_lock_read(map
);
8749 result
= (map
->timestamp
== version
->main_timestamp
);
8752 vm_map_unlock_read(map
);
8758 * vm_map_verify_done:
8760 * Releases locks acquired by a vm_map_verify.
8762 * This is now a macro in vm/vm_map.h. It does a
8763 * vm_map_unlock_read on the map.
8768 * TEMPORARYTEMPORARYTEMPORARYTEMPORARYTEMPORARYTEMPORARY
8769 * Goes away after regular vm_region_recurse function migrates to
8771 * vm_region_recurse: A form of vm_region which follows the
8772 * submaps in a target map
8777 vm_map_region_recurse_64(
8779 vm_map_offset_t
*address
, /* IN/OUT */
8780 vm_map_size_t
*size
, /* OUT */
8781 natural_t
*nesting_depth
, /* IN/OUT */
8782 vm_region_submap_info_64_t submap_info
, /* IN/OUT */
8783 mach_msg_type_number_t
*count
) /* IN/OUT */
8785 vm_region_extended_info_data_t extended
;
8786 vm_map_entry_t tmp_entry
;
8787 vm_map_offset_t user_address
;
8788 unsigned int user_max_depth
;
8791 * "curr_entry" is the VM map entry preceding or including the
8792 * address we're looking for.
8793 * "curr_map" is the map or sub-map containing "curr_entry".
8794 * "curr_offset" is the cumulated offset of "curr_map" in the
8795 * target task's address space.
8796 * "curr_depth" is the depth of "curr_map" in the chain of
8798 * "curr_max_offset" is the maximum offset we should take into
8799 * account in the current map. It may be smaller than the current
8800 * map's "max_offset" because we might not have mapped it all in
8801 * the upper level map.
8803 vm_map_entry_t curr_entry
;
8804 vm_map_offset_t curr_offset
;
8806 unsigned int curr_depth
;
8807 vm_map_offset_t curr_max_offset
;
8810 * "next_" is the same as "curr_" but for the VM region immediately
8811 * after the address we're looking for. We need to keep track of this
8812 * too because we want to return info about that region if the
8813 * address we're looking for is not mapped.
8815 vm_map_entry_t next_entry
;
8816 vm_map_offset_t next_offset
;
8818 unsigned int next_depth
;
8819 vm_map_offset_t next_max_offset
;
8821 boolean_t look_for_pages
;
8822 vm_region_submap_short_info_64_t short_info
;
8824 if (map
== VM_MAP_NULL
) {
8825 /* no address space to work on */
8826 return KERN_INVALID_ARGUMENT
;
8829 if (*count
< VM_REGION_SUBMAP_INFO_COUNT_64
) {
8830 if (*count
< VM_REGION_SUBMAP_SHORT_INFO_COUNT_64
) {
8832 * "info" structure is not big enough and
8835 return KERN_INVALID_ARGUMENT
;
8837 look_for_pages
= FALSE
;
8838 *count
= VM_REGION_SUBMAP_SHORT_INFO_COUNT_64
;
8839 short_info
= (vm_region_submap_short_info_64_t
) submap_info
;
8843 look_for_pages
= TRUE
;
8844 *count
= VM_REGION_SUBMAP_INFO_COUNT_64
;
8849 user_address
= *address
;
8850 user_max_depth
= *nesting_depth
;
8856 curr_max_offset
= curr_map
->max_offset
;
8862 next_max_offset
= curr_max_offset
;
8865 vm_map_lock_read(curr_map
);
8869 if (vm_map_lookup_entry(curr_map
,
8870 user_address
- curr_offset
,
8872 /* tmp_entry contains the address we're looking for */
8873 curr_entry
= tmp_entry
;
8876 * The address is not mapped. "tmp_entry" is the
8877 * map entry preceding the address. We want the next
8878 * one, if it exists.
8880 curr_entry
= tmp_entry
->vme_next
;
8881 if (curr_entry
== vm_map_to_entry(curr_map
) ||
8882 curr_entry
->vme_start
>= curr_max_offset
) {
8883 /* no next entry at this level: stop looking */
8885 vm_map_unlock_read(curr_map
);
8891 curr_max_offset
= 0;
8897 * Is the next entry at this level closer to the address (or
8898 * deeper in the submap chain) than the one we had
8901 tmp_entry
= curr_entry
->vme_next
;
8902 if (tmp_entry
== vm_map_to_entry(curr_map
)) {
8903 /* no next entry at this level */
8904 } else if (tmp_entry
->vme_start
>= curr_max_offset
) {
8906 * tmp_entry is beyond the scope of what we mapped of
8907 * this submap in the upper level: ignore it.
8909 } else if ((next_entry
== NULL
) ||
8910 (tmp_entry
->vme_start
+ curr_offset
<=
8911 next_entry
->vme_start
+ next_offset
)) {
8913 * We didn't have a "next_entry" or this one is
8914 * closer to the address we're looking for:
8915 * use this "tmp_entry" as the new "next_entry".
8917 if (next_entry
!= NULL
) {
8918 /* unlock the last "next_map" */
8919 if (next_map
!= curr_map
&& not_in_kdp
) {
8920 vm_map_unlock_read(next_map
);
8923 next_entry
= tmp_entry
;
8924 next_map
= curr_map
;
8925 next_offset
= curr_offset
;
8926 next_depth
= curr_depth
;
8927 next_max_offset
= curr_max_offset
;
8930 if (!curr_entry
->is_sub_map
||
8931 curr_depth
>= user_max_depth
) {
8933 * We hit a leaf map or we reached the maximum depth
8934 * we could, so stop looking. Keep the current map
8941 * Get down to the next submap level.
8945 * Lock the next level and unlock the current level,
8946 * unless we need to keep it locked to access the "next_entry"
8950 vm_map_lock_read(curr_entry
->object
.sub_map
);
8952 if (curr_map
== next_map
) {
8953 /* keep "next_map" locked in case we need it */
8955 /* release this map */
8957 vm_map_unlock_read(curr_map
);
8961 * Adjust the offset. "curr_entry" maps the submap
8962 * at relative address "curr_entry->vme_start" in the
8963 * curr_map but skips the first "curr_entry->offset"
8964 * bytes of the submap.
8965 * "curr_offset" always represents the offset of a virtual
8966 * address in the curr_map relative to the absolute address
8967 * space (i.e. the top-level VM map).
8970 (curr_entry
->vme_start
- curr_entry
->offset
);
8971 /* switch to the submap */
8972 curr_map
= curr_entry
->object
.sub_map
;
8975 * "curr_max_offset" allows us to keep track of the
8976 * portion of the submap that is actually mapped at this level:
8977 * the rest of that submap is irrelevant to us, since it's not
8979 * The relevant portion of the map starts at
8980 * "curr_entry->offset" up to the size of "curr_entry".
8983 curr_entry
->vme_end
- curr_entry
->vme_start
+
8988 if (curr_entry
== NULL
) {
8989 /* no VM region contains the address... */
8990 if (next_entry
== NULL
) {
8991 /* ... and no VM region follows it either */
8992 return KERN_INVALID_ADDRESS
;
8994 /* ... gather info about the next VM region */
8995 curr_entry
= next_entry
;
8996 curr_map
= next_map
; /* still locked ... */
8997 curr_offset
= next_offset
;
8998 curr_depth
= next_depth
;
8999 curr_max_offset
= next_max_offset
;
9001 /* we won't need "next_entry" after all */
9002 if (next_entry
!= NULL
) {
9003 /* release "next_map" */
9004 if (next_map
!= curr_map
&& not_in_kdp
) {
9005 vm_map_unlock_read(next_map
);
9013 next_max_offset
= 0;
9015 *nesting_depth
= curr_depth
;
9016 *size
= curr_entry
->vme_end
- curr_entry
->vme_start
;
9017 *address
= curr_entry
->vme_start
+ curr_offset
;
9019 // LP64todo: all the current tools are 32bit, obviously never worked for 64b
9020 // so probably should be a real 32b ID vs. ptr.
9021 // Current users just check for equality
9022 #define INFO_MAKE_OBJECT_ID(p) ((uint32_t)(uintptr_t)p)
9024 if (look_for_pages
) {
9025 submap_info
->user_tag
= curr_entry
->alias
;
9026 submap_info
->offset
= curr_entry
->offset
;
9027 submap_info
->protection
= curr_entry
->protection
;
9028 submap_info
->inheritance
= curr_entry
->inheritance
;
9029 submap_info
->max_protection
= curr_entry
->max_protection
;
9030 submap_info
->behavior
= curr_entry
->behavior
;
9031 submap_info
->user_wired_count
= curr_entry
->user_wired_count
;
9032 submap_info
->is_submap
= curr_entry
->is_sub_map
;
9033 submap_info
->object_id
= INFO_MAKE_OBJECT_ID(curr_entry
->object
.vm_object
);
9035 short_info
->user_tag
= curr_entry
->alias
;
9036 short_info
->offset
= curr_entry
->offset
;
9037 short_info
->protection
= curr_entry
->protection
;
9038 short_info
->inheritance
= curr_entry
->inheritance
;
9039 short_info
->max_protection
= curr_entry
->max_protection
;
9040 short_info
->behavior
= curr_entry
->behavior
;
9041 short_info
->user_wired_count
= curr_entry
->user_wired_count
;
9042 short_info
->is_submap
= curr_entry
->is_sub_map
;
9043 short_info
->object_id
= INFO_MAKE_OBJECT_ID(curr_entry
->object
.vm_object
);
9046 extended
.pages_resident
= 0;
9047 extended
.pages_swapped_out
= 0;
9048 extended
.pages_shared_now_private
= 0;
9049 extended
.pages_dirtied
= 0;
9050 extended
.external_pager
= 0;
9051 extended
.shadow_depth
= 0;
9054 if (!curr_entry
->is_sub_map
) {
9055 vm_map_region_walk(curr_map
,
9056 curr_entry
->vme_start
,
9059 (curr_entry
->vme_end
-
9060 curr_entry
->vme_start
),
9063 if (extended
.external_pager
&&
9064 extended
.ref_count
== 2 &&
9065 extended
.share_mode
== SM_SHARED
) {
9066 extended
.share_mode
= SM_PRIVATE
;
9069 if (curr_entry
->use_pmap
) {
9070 extended
.share_mode
= SM_TRUESHARED
;
9072 extended
.share_mode
= SM_PRIVATE
;
9074 extended
.ref_count
=
9075 curr_entry
->object
.sub_map
->ref_count
;
9079 if (look_for_pages
) {
9080 submap_info
->pages_resident
= extended
.pages_resident
;
9081 submap_info
->pages_swapped_out
= extended
.pages_swapped_out
;
9082 submap_info
->pages_shared_now_private
=
9083 extended
.pages_shared_now_private
;
9084 submap_info
->pages_dirtied
= extended
.pages_dirtied
;
9085 submap_info
->external_pager
= extended
.external_pager
;
9086 submap_info
->shadow_depth
= extended
.shadow_depth
;
9087 submap_info
->share_mode
= extended
.share_mode
;
9088 submap_info
->ref_count
= extended
.ref_count
;
9090 short_info
->external_pager
= extended
.external_pager
;
9091 short_info
->shadow_depth
= extended
.shadow_depth
;
9092 short_info
->share_mode
= extended
.share_mode
;
9093 short_info
->ref_count
= extended
.ref_count
;
9097 vm_map_unlock_read(curr_map
);
9100 return KERN_SUCCESS
;
9106 * User call to obtain information about a region in
9107 * a task's address map. Currently, only one flavor is
9110 * XXX The reserved and behavior fields cannot be filled
9111 * in until the vm merge from the IK is completed, and
9112 * vm_reserve is implemented.
9118 vm_map_offset_t
*address
, /* IN/OUT */
9119 vm_map_size_t
*size
, /* OUT */
9120 vm_region_flavor_t flavor
, /* IN */
9121 vm_region_info_t info
, /* OUT */
9122 mach_msg_type_number_t
*count
, /* IN/OUT */
9123 mach_port_t
*object_name
) /* OUT */
9125 vm_map_entry_t tmp_entry
;
9126 vm_map_entry_t entry
;
9127 vm_map_offset_t start
;
9129 if (map
== VM_MAP_NULL
)
9130 return(KERN_INVALID_ARGUMENT
);
9134 case VM_REGION_BASIC_INFO
:
9135 /* legacy for old 32-bit objects info */
9137 vm_region_basic_info_t basic
;
9139 if (*count
< VM_REGION_BASIC_INFO_COUNT
)
9140 return(KERN_INVALID_ARGUMENT
);
9142 basic
= (vm_region_basic_info_t
) info
;
9143 *count
= VM_REGION_BASIC_INFO_COUNT
;
9145 vm_map_lock_read(map
);
9148 if (!vm_map_lookup_entry(map
, start
, &tmp_entry
)) {
9149 if ((entry
= tmp_entry
->vme_next
) == vm_map_to_entry(map
)) {
9150 vm_map_unlock_read(map
);
9151 return(KERN_INVALID_ADDRESS
);
9157 start
= entry
->vme_start
;
9159 basic
->offset
= (uint32_t)entry
->offset
;
9160 basic
->protection
= entry
->protection
;
9161 basic
->inheritance
= entry
->inheritance
;
9162 basic
->max_protection
= entry
->max_protection
;
9163 basic
->behavior
= entry
->behavior
;
9164 basic
->user_wired_count
= entry
->user_wired_count
;
9165 basic
->reserved
= entry
->is_sub_map
;
9167 *size
= (entry
->vme_end
- start
);
9169 if (object_name
) *object_name
= IP_NULL
;
9170 if (entry
->is_sub_map
) {
9171 basic
->shared
= FALSE
;
9173 basic
->shared
= entry
->is_shared
;
9176 vm_map_unlock_read(map
);
9177 return(KERN_SUCCESS
);
9180 case VM_REGION_BASIC_INFO_64
:
9182 vm_region_basic_info_64_t basic
;
9184 if (*count
< VM_REGION_BASIC_INFO_COUNT_64
)
9185 return(KERN_INVALID_ARGUMENT
);
9187 basic
= (vm_region_basic_info_64_t
) info
;
9188 *count
= VM_REGION_BASIC_INFO_COUNT_64
;
9190 vm_map_lock_read(map
);
9193 if (!vm_map_lookup_entry(map
, start
, &tmp_entry
)) {
9194 if ((entry
= tmp_entry
->vme_next
) == vm_map_to_entry(map
)) {
9195 vm_map_unlock_read(map
);
9196 return(KERN_INVALID_ADDRESS
);
9202 start
= entry
->vme_start
;
9204 basic
->offset
= entry
->offset
;
9205 basic
->protection
= entry
->protection
;
9206 basic
->inheritance
= entry
->inheritance
;
9207 basic
->max_protection
= entry
->max_protection
;
9208 basic
->behavior
= entry
->behavior
;
9209 basic
->user_wired_count
= entry
->user_wired_count
;
9210 basic
->reserved
= entry
->is_sub_map
;
9212 *size
= (entry
->vme_end
- start
);
9214 if (object_name
) *object_name
= IP_NULL
;
9215 if (entry
->is_sub_map
) {
9216 basic
->shared
= FALSE
;
9218 basic
->shared
= entry
->is_shared
;
9221 vm_map_unlock_read(map
);
9222 return(KERN_SUCCESS
);
9224 case VM_REGION_EXTENDED_INFO
:
9226 vm_region_extended_info_t extended
;
9228 if (*count
< VM_REGION_EXTENDED_INFO_COUNT
)
9229 return(KERN_INVALID_ARGUMENT
);
9231 extended
= (vm_region_extended_info_t
) info
;
9232 *count
= VM_REGION_EXTENDED_INFO_COUNT
;
9234 vm_map_lock_read(map
);
9237 if (!vm_map_lookup_entry(map
, start
, &tmp_entry
)) {
9238 if ((entry
= tmp_entry
->vme_next
) == vm_map_to_entry(map
)) {
9239 vm_map_unlock_read(map
);
9240 return(KERN_INVALID_ADDRESS
);
9245 start
= entry
->vme_start
;
9247 extended
->protection
= entry
->protection
;
9248 extended
->user_tag
= entry
->alias
;
9249 extended
->pages_resident
= 0;
9250 extended
->pages_swapped_out
= 0;
9251 extended
->pages_shared_now_private
= 0;
9252 extended
->pages_dirtied
= 0;
9253 extended
->external_pager
= 0;
9254 extended
->shadow_depth
= 0;
9256 vm_map_region_walk(map
, start
, entry
, entry
->offset
, entry
->vme_end
- start
, extended
, TRUE
);
9258 if (extended
->external_pager
&& extended
->ref_count
== 2 && extended
->share_mode
== SM_SHARED
)
9259 extended
->share_mode
= SM_PRIVATE
;
9262 *object_name
= IP_NULL
;
9264 *size
= (entry
->vme_end
- start
);
9266 vm_map_unlock_read(map
);
9267 return(KERN_SUCCESS
);
9269 case VM_REGION_TOP_INFO
:
9271 vm_region_top_info_t top
;
9273 if (*count
< VM_REGION_TOP_INFO_COUNT
)
9274 return(KERN_INVALID_ARGUMENT
);
9276 top
= (vm_region_top_info_t
) info
;
9277 *count
= VM_REGION_TOP_INFO_COUNT
;
9279 vm_map_lock_read(map
);
9282 if (!vm_map_lookup_entry(map
, start
, &tmp_entry
)) {
9283 if ((entry
= tmp_entry
->vme_next
) == vm_map_to_entry(map
)) {
9284 vm_map_unlock_read(map
);
9285 return(KERN_INVALID_ADDRESS
);
9291 start
= entry
->vme_start
;
9293 top
->private_pages_resident
= 0;
9294 top
->shared_pages_resident
= 0;
9296 vm_map_region_top_walk(entry
, top
);
9299 *object_name
= IP_NULL
;
9301 *size
= (entry
->vme_end
- start
);
9303 vm_map_unlock_read(map
);
9304 return(KERN_SUCCESS
);
9307 return(KERN_INVALID_ARGUMENT
);
9311 #define OBJ_RESIDENT_COUNT(obj, entry_size) \
9313 ((obj)->all_reusable ? \
9314 (obj)->wired_page_count : \
9315 (obj)->resident_page_count - (obj)->reusable_page_count))
9318 vm_map_region_top_walk(
9319 vm_map_entry_t entry
,
9320 vm_region_top_info_t top
)
9323 if (entry
->object
.vm_object
== 0 || entry
->is_sub_map
) {
9324 top
->share_mode
= SM_EMPTY
;
9331 struct vm_object
*obj
, *tmp_obj
;
9333 uint32_t entry_size
;
9335 entry_size
= (uint32_t) ((entry
->vme_end
- entry
->vme_start
) / PAGE_SIZE_64
);
9337 obj
= entry
->object
.vm_object
;
9339 vm_object_lock(obj
);
9341 if ((ref_count
= obj
->ref_count
) > 1 && obj
->paging_in_progress
)
9344 assert(obj
->reusable_page_count
<= obj
->resident_page_count
);
9347 top
->private_pages_resident
=
9348 OBJ_RESIDENT_COUNT(obj
, entry_size
);
9350 top
->shared_pages_resident
=
9351 OBJ_RESIDENT_COUNT(obj
, entry_size
);
9352 top
->ref_count
= ref_count
;
9353 top
->share_mode
= SM_COW
;
9355 while ((tmp_obj
= obj
->shadow
)) {
9356 vm_object_lock(tmp_obj
);
9357 vm_object_unlock(obj
);
9360 if ((ref_count
= obj
->ref_count
) > 1 && obj
->paging_in_progress
)
9363 assert(obj
->reusable_page_count
<= obj
->resident_page_count
);
9364 top
->shared_pages_resident
+=
9365 OBJ_RESIDENT_COUNT(obj
, entry_size
);
9366 top
->ref_count
+= ref_count
- 1;
9369 if (entry
->needs_copy
) {
9370 top
->share_mode
= SM_COW
;
9371 top
->shared_pages_resident
=
9372 OBJ_RESIDENT_COUNT(obj
, entry_size
);
9374 if (ref_count
== 1 ||
9375 (ref_count
== 2 && !(obj
->pager_trusted
) && !(obj
->internal
))) {
9376 top
->share_mode
= SM_PRIVATE
;
9377 top
->private_pages_resident
=
9378 OBJ_RESIDENT_COUNT(obj
,
9381 top
->share_mode
= SM_SHARED
;
9382 top
->shared_pages_resident
=
9383 OBJ_RESIDENT_COUNT(obj
,
9387 top
->ref_count
= ref_count
;
9389 /* XXX K64: obj_id will be truncated */
9390 top
->obj_id
= (unsigned int) (uintptr_t)obj
;
9392 vm_object_unlock(obj
);
9400 vm_map_entry_t entry
,
9401 vm_object_offset_t offset
,
9402 vm_object_size_t range
,
9403 vm_region_extended_info_t extended
,
9404 boolean_t look_for_pages
)
9406 register struct vm_object
*obj
, *tmp_obj
;
9407 register vm_map_offset_t last_offset
;
9409 register int ref_count
;
9410 struct vm_object
*shadow_object
;
9413 if ((entry
->object
.vm_object
== 0) ||
9414 (entry
->is_sub_map
) ||
9415 (entry
->object
.vm_object
->phys_contiguous
)) {
9416 extended
->share_mode
= SM_EMPTY
;
9417 extended
->ref_count
= 0;
9421 obj
= entry
->object
.vm_object
;
9423 vm_object_lock(obj
);
9425 if ((ref_count
= obj
->ref_count
) > 1 && obj
->paging_in_progress
)
9428 if (look_for_pages
) {
9429 for (last_offset
= offset
+ range
;
9430 offset
< last_offset
;
9431 offset
+= PAGE_SIZE_64
, va
+= PAGE_SIZE
)
9432 vm_map_region_look_for_page(map
, va
, obj
,
9436 shadow_object
= obj
->shadow
;
9439 if ( !(obj
->pager_trusted
) && !(obj
->internal
))
9440 extended
->external_pager
= 1;
9442 if (shadow_object
!= VM_OBJECT_NULL
) {
9443 vm_object_lock(shadow_object
);
9445 shadow_object
!= VM_OBJECT_NULL
;
9447 vm_object_t next_shadow
;
9449 if ( !(shadow_object
->pager_trusted
) &&
9450 !(shadow_object
->internal
))
9451 extended
->external_pager
= 1;
9453 next_shadow
= shadow_object
->shadow
;
9455 vm_object_lock(next_shadow
);
9457 vm_object_unlock(shadow_object
);
9458 shadow_object
= next_shadow
;
9461 extended
->shadow_depth
= shadow_depth
;
9464 if (extended
->shadow_depth
|| entry
->needs_copy
)
9465 extended
->share_mode
= SM_COW
;
9468 extended
->share_mode
= SM_PRIVATE
;
9470 if (obj
->true_share
)
9471 extended
->share_mode
= SM_TRUESHARED
;
9473 extended
->share_mode
= SM_SHARED
;
9476 extended
->ref_count
= ref_count
- extended
->shadow_depth
;
9478 for (i
= 0; i
< extended
->shadow_depth
; i
++) {
9479 if ((tmp_obj
= obj
->shadow
) == 0)
9481 vm_object_lock(tmp_obj
);
9482 vm_object_unlock(obj
);
9484 if ((ref_count
= tmp_obj
->ref_count
) > 1 && tmp_obj
->paging_in_progress
)
9487 extended
->ref_count
+= ref_count
;
9490 vm_object_unlock(obj
);
9492 if (extended
->share_mode
== SM_SHARED
) {
9493 register vm_map_entry_t cur
;
9494 register vm_map_entry_t last
;
9497 obj
= entry
->object
.vm_object
;
9498 last
= vm_map_to_entry(map
);
9501 if ((ref_count
= obj
->ref_count
) > 1 && obj
->paging_in_progress
)
9503 for (cur
= vm_map_first_entry(map
); cur
!= last
; cur
= cur
->vme_next
)
9504 my_refs
+= vm_map_region_count_obj_refs(cur
, obj
);
9506 if (my_refs
== ref_count
)
9507 extended
->share_mode
= SM_PRIVATE_ALIASED
;
9508 else if (my_refs
> 1)
9509 extended
->share_mode
= SM_SHARED_ALIASED
;
9515 /* object is locked on entry and locked on return */
9519 vm_map_region_look_for_page(
9520 __unused vm_map_t map
,
9521 __unused vm_map_offset_t va
,
9523 vm_object_offset_t offset
,
9526 vm_region_extended_info_t extended
)
9528 register vm_page_t p
;
9529 register vm_object_t shadow
;
9530 register int ref_count
;
9531 vm_object_t caller_object
;
9535 shadow
= object
->shadow
;
9536 caller_object
= object
;
9541 if ( !(object
->pager_trusted
) && !(object
->internal
))
9542 extended
->external_pager
= 1;
9544 if ((p
= vm_page_lookup(object
, offset
)) != VM_PAGE_NULL
) {
9545 if (shadow
&& (max_refcnt
== 1))
9546 extended
->pages_shared_now_private
++;
9548 if (!p
->fictitious
&&
9549 (p
->dirty
|| pmap_is_modified(p
->phys_page
)))
9550 extended
->pages_dirtied
++;
9552 extended
->pages_resident
++;
9554 if(object
!= caller_object
)
9555 vm_object_unlock(object
);
9560 if (object
->existence_map
) {
9561 if (vm_external_state_get(object
->existence_map
, offset
) == VM_EXTERNAL_STATE_EXISTS
) {
9563 extended
->pages_swapped_out
++;
9565 if(object
!= caller_object
)
9566 vm_object_unlock(object
);
9570 } else if (object
->internal
&&
9572 !object
->terminating
&&
9573 object
->pager_ready
) {
9575 memory_object_t pager
;
9577 vm_object_paging_begin(object
);
9578 pager
= object
->pager
;
9579 vm_object_unlock(object
);
9581 kr
= memory_object_data_request(
9583 offset
+ object
->paging_offset
,
9584 0, /* just poke the pager */
9588 vm_object_lock(object
);
9589 vm_object_paging_end(object
);
9591 if (kr
== KERN_SUCCESS
) {
9592 /* the pager has that page */
9593 extended
->pages_swapped_out
++;
9594 if (object
!= caller_object
)
9595 vm_object_unlock(object
);
9599 #endif /* MACH_PAGEMAP */
9602 vm_object_lock(shadow
);
9604 if ((ref_count
= shadow
->ref_count
) > 1 && shadow
->paging_in_progress
)
9607 if (++depth
> extended
->shadow_depth
)
9608 extended
->shadow_depth
= depth
;
9610 if (ref_count
> max_refcnt
)
9611 max_refcnt
= ref_count
;
9613 if(object
!= caller_object
)
9614 vm_object_unlock(object
);
9616 offset
= offset
+ object
->shadow_offset
;
9618 shadow
= object
->shadow
;
9621 if(object
!= caller_object
)
9622 vm_object_unlock(object
);
9628 vm_map_region_count_obj_refs(
9629 vm_map_entry_t entry
,
9632 register int ref_count
;
9633 register vm_object_t chk_obj
;
9634 register vm_object_t tmp_obj
;
9636 if (entry
->object
.vm_object
== 0)
9639 if (entry
->is_sub_map
)
9644 chk_obj
= entry
->object
.vm_object
;
9645 vm_object_lock(chk_obj
);
9648 if (chk_obj
== object
)
9650 tmp_obj
= chk_obj
->shadow
;
9652 vm_object_lock(tmp_obj
);
9653 vm_object_unlock(chk_obj
);
9663 * Routine: vm_map_simplify
9666 * Attempt to simplify the map representation in
9667 * the vicinity of the given starting address.
9669 * This routine is intended primarily to keep the
9670 * kernel maps more compact -- they generally don't
9671 * benefit from the "expand a map entry" technology
9672 * at allocation time because the adjacent entry
9673 * is often wired down.
9676 vm_map_simplify_entry(
9678 vm_map_entry_t this_entry
)
9680 vm_map_entry_t prev_entry
;
9682 counter(c_vm_map_simplify_entry_called
++);
9684 prev_entry
= this_entry
->vme_prev
;
9686 if ((this_entry
!= vm_map_to_entry(map
)) &&
9687 (prev_entry
!= vm_map_to_entry(map
)) &&
9689 (prev_entry
->vme_end
== this_entry
->vme_start
) &&
9691 (prev_entry
->is_sub_map
== this_entry
->is_sub_map
) &&
9693 (prev_entry
->object
.vm_object
== this_entry
->object
.vm_object
) &&
9694 ((prev_entry
->offset
+ (prev_entry
->vme_end
-
9695 prev_entry
->vme_start
))
9696 == this_entry
->offset
) &&
9698 (prev_entry
->inheritance
== this_entry
->inheritance
) &&
9699 (prev_entry
->protection
== this_entry
->protection
) &&
9700 (prev_entry
->max_protection
== this_entry
->max_protection
) &&
9701 (prev_entry
->behavior
== this_entry
->behavior
) &&
9702 (prev_entry
->alias
== this_entry
->alias
) &&
9703 (prev_entry
->zero_wired_pages
== this_entry
->zero_wired_pages
) &&
9704 (prev_entry
->no_cache
== this_entry
->no_cache
) &&
9705 (prev_entry
->wired_count
== this_entry
->wired_count
) &&
9706 (prev_entry
->user_wired_count
== this_entry
->user_wired_count
) &&
9708 (prev_entry
->needs_copy
== this_entry
->needs_copy
) &&
9709 (prev_entry
->permanent
== this_entry
->permanent
) &&
9711 (prev_entry
->use_pmap
== FALSE
) &&
9712 (this_entry
->use_pmap
== FALSE
) &&
9713 (prev_entry
->in_transition
== FALSE
) &&
9714 (this_entry
->in_transition
== FALSE
) &&
9715 (prev_entry
->needs_wakeup
== FALSE
) &&
9716 (this_entry
->needs_wakeup
== FALSE
) &&
9717 (prev_entry
->is_shared
== FALSE
) &&
9718 (this_entry
->is_shared
== FALSE
)
9720 _vm_map_entry_unlink(&map
->hdr
, prev_entry
);
9721 this_entry
->vme_start
= prev_entry
->vme_start
;
9722 this_entry
->offset
= prev_entry
->offset
;
9723 if (prev_entry
->is_sub_map
) {
9724 vm_map_deallocate(prev_entry
->object
.sub_map
);
9726 vm_object_deallocate(prev_entry
->object
.vm_object
);
9728 vm_map_entry_dispose(map
, prev_entry
);
9729 SAVE_HINT_MAP_WRITE(map
, this_entry
);
9730 counter(c_vm_map_simplified
++);
9737 vm_map_offset_t start
)
9739 vm_map_entry_t this_entry
;
9742 if (vm_map_lookup_entry(map
, start
, &this_entry
)) {
9743 vm_map_simplify_entry(map
, this_entry
);
9744 vm_map_simplify_entry(map
, this_entry
->vme_next
);
9746 counter(c_vm_map_simplify_called
++);
9751 vm_map_simplify_range(
9753 vm_map_offset_t start
,
9754 vm_map_offset_t end
)
9756 vm_map_entry_t entry
;
9759 * The map should be locked (for "write") by the caller.
9763 /* invalid address range */
9767 start
= vm_map_trunc_page(start
);
9768 end
= vm_map_round_page(end
);
9770 if (!vm_map_lookup_entry(map
, start
, &entry
)) {
9771 /* "start" is not mapped and "entry" ends before "start" */
9772 if (entry
== vm_map_to_entry(map
)) {
9773 /* start with first entry in the map */
9774 entry
= vm_map_first_entry(map
);
9776 /* start with next entry */
9777 entry
= entry
->vme_next
;
9781 while (entry
!= vm_map_to_entry(map
) &&
9782 entry
->vme_start
<= end
) {
9783 /* try and coalesce "entry" with its previous entry */
9784 vm_map_simplify_entry(map
, entry
);
9785 entry
= entry
->vme_next
;
9791 * Routine: vm_map_machine_attribute
9793 * Provide machine-specific attributes to mappings,
9794 * such as cachability etc. for machines that provide
9795 * them. NUMA architectures and machines with big/strange
9796 * caches will use this.
9798 * Responsibilities for locking and checking are handled here,
9799 * everything else in the pmap module. If any non-volatile
9800 * information must be kept, the pmap module should handle
9801 * it itself. [This assumes that attributes do not
9802 * need to be inherited, which seems ok to me]
9805 vm_map_machine_attribute(
9807 vm_map_offset_t start
,
9808 vm_map_offset_t end
,
9809 vm_machine_attribute_t attribute
,
9810 vm_machine_attribute_val_t
* value
) /* IN/OUT */
9813 vm_map_size_t sync_size
;
9814 vm_map_entry_t entry
;
9816 if (start
< vm_map_min(map
) || end
> vm_map_max(map
))
9817 return KERN_INVALID_ADDRESS
;
9819 /* Figure how much memory we need to flush (in page increments) */
9820 sync_size
= end
- start
;
9824 if (attribute
!= MATTR_CACHE
) {
9825 /* If we don't have to find physical addresses, we */
9826 /* don't have to do an explicit traversal here. */
9827 ret
= pmap_attribute(map
->pmap
, start
, end
-start
,
9833 ret
= KERN_SUCCESS
; /* Assume it all worked */
9836 if (vm_map_lookup_entry(map
, start
, &entry
)) {
9837 vm_map_size_t sub_size
;
9838 if((entry
->vme_end
- start
) > sync_size
) {
9839 sub_size
= sync_size
;
9842 sub_size
= entry
->vme_end
- start
;
9843 sync_size
-= sub_size
;
9845 if(entry
->is_sub_map
) {
9846 vm_map_offset_t sub_start
;
9847 vm_map_offset_t sub_end
;
9849 sub_start
= (start
- entry
->vme_start
)
9851 sub_end
= sub_start
+ sub_size
;
9852 vm_map_machine_attribute(
9853 entry
->object
.sub_map
,
9858 if(entry
->object
.vm_object
) {
9861 vm_object_t base_object
;
9862 vm_object_t last_object
;
9863 vm_object_offset_t offset
;
9864 vm_object_offset_t base_offset
;
9865 vm_map_size_t range
;
9867 offset
= (start
- entry
->vme_start
)
9869 base_offset
= offset
;
9870 object
= entry
->object
.vm_object
;
9871 base_object
= object
;
9874 vm_object_lock(object
);
9880 if (m
&& !m
->fictitious
) {
9882 pmap_attribute_cache_sync(
9887 } else if (object
->shadow
) {
9888 offset
= offset
+ object
->shadow_offset
;
9889 last_object
= object
;
9890 object
= object
->shadow
;
9891 vm_object_lock(last_object
->shadow
);
9892 vm_object_unlock(last_object
);
9897 if (base_object
!= object
) {
9898 vm_object_unlock(object
);
9899 vm_object_lock(base_object
);
9900 object
= base_object
;
9902 /* Bump to the next page */
9903 base_offset
+= PAGE_SIZE
;
9904 offset
= base_offset
;
9906 vm_object_unlock(object
);
9912 return KERN_FAILURE
;
9923 * vm_map_behavior_set:
9925 * Sets the paging reference behavior of the specified address
9926 * range in the target map. Paging reference behavior affects
9927 * how pagein operations resulting from faults on the map will be
9931 vm_map_behavior_set(
9933 vm_map_offset_t start
,
9934 vm_map_offset_t end
,
9935 vm_behavior_t new_behavior
)
9937 register vm_map_entry_t entry
;
9938 vm_map_entry_t temp_entry
;
9941 "vm_map_behavior_set, 0x%X start 0x%X end 0x%X behavior %d",
9942 map
, start
, end
, new_behavior
, 0);
9944 switch (new_behavior
) {
9947 * This first block of behaviors all set a persistent state on the specified
9948 * memory range. All we have to do here is to record the desired behavior
9949 * in the vm_map_entry_t's.
9952 case VM_BEHAVIOR_DEFAULT
:
9953 case VM_BEHAVIOR_RANDOM
:
9954 case VM_BEHAVIOR_SEQUENTIAL
:
9955 case VM_BEHAVIOR_RSEQNTL
:
9956 case VM_BEHAVIOR_ZERO_WIRED_PAGES
:
9960 * The entire address range must be valid for the map.
9961 * Note that vm_map_range_check() does a
9962 * vm_map_lookup_entry() internally and returns the
9963 * entry containing the start of the address range if
9964 * the entire range is valid.
9966 if (vm_map_range_check(map
, start
, end
, &temp_entry
)) {
9968 vm_map_clip_start(map
, entry
, start
);
9972 return(KERN_INVALID_ADDRESS
);
9975 while ((entry
!= vm_map_to_entry(map
)) && (entry
->vme_start
< end
)) {
9976 vm_map_clip_end(map
, entry
, end
);
9977 assert(!entry
->use_pmap
);
9979 if( new_behavior
== VM_BEHAVIOR_ZERO_WIRED_PAGES
) {
9980 entry
->zero_wired_pages
= TRUE
;
9982 entry
->behavior
= new_behavior
;
9984 entry
= entry
->vme_next
;
9991 * The rest of these are different from the above in that they cause
9992 * an immediate action to take place as opposed to setting a behavior that
9993 * affects future actions.
9996 case VM_BEHAVIOR_WILLNEED
:
9997 return vm_map_willneed(map
, start
, end
);
9999 case VM_BEHAVIOR_DONTNEED
:
10000 return vm_map_msync(map
, start
, end
- start
, VM_SYNC_DEACTIVATE
| VM_SYNC_CONTIGUOUS
);
10002 case VM_BEHAVIOR_FREE
:
10003 return vm_map_msync(map
, start
, end
- start
, VM_SYNC_KILLPAGES
| VM_SYNC_CONTIGUOUS
);
10005 case VM_BEHAVIOR_REUSABLE
:
10006 return vm_map_reusable_pages(map
, start
, end
);
10008 case VM_BEHAVIOR_REUSE
:
10009 return vm_map_reuse_pages(map
, start
, end
);
10011 case VM_BEHAVIOR_CAN_REUSE
:
10012 return vm_map_can_reuse(map
, start
, end
);
10015 return(KERN_INVALID_ARGUMENT
);
10018 return(KERN_SUCCESS
);
10023 * Internals for madvise(MADV_WILLNEED) system call.
10025 * The present implementation is to do a read-ahead if the mapping corresponds
10026 * to a mapped regular file. If it's an anonymous mapping, then we do nothing
10027 * and basically ignore the "advice" (which we are always free to do).
10031 static kern_return_t
10034 vm_map_offset_t start
,
10035 vm_map_offset_t end
10038 vm_map_entry_t entry
;
10039 vm_object_t object
;
10040 memory_object_t pager
;
10041 struct vm_object_fault_info fault_info
;
10043 vm_object_size_t len
;
10044 vm_object_offset_t offset
;
10047 * Fill in static values in fault_info. Several fields get ignored by the code
10048 * we call, but we'll fill them in anyway since uninitialized fields are bad
10049 * when it comes to future backwards compatibility.
10052 fault_info
.interruptible
= THREAD_UNINT
; /* ignored value */
10053 fault_info
.behavior
= VM_BEHAVIOR_SEQUENTIAL
;
10054 fault_info
.no_cache
= FALSE
; /* ignored value */
10055 fault_info
.stealth
= TRUE
;
10058 * The MADV_WILLNEED operation doesn't require any changes to the
10059 * vm_map_entry_t's, so the read lock is sufficient.
10062 vm_map_lock_read(map
);
10065 * The madvise semantics require that the address range be fully
10066 * allocated with no holes. Otherwise, we're required to return
10070 if (vm_map_range_check(map
, start
, end
, &entry
)) {
10073 * Examine each vm_map_entry_t in the range.
10076 for (; entry
->vme_start
< end
; start
+= len
, entry
= entry
->vme_next
) {
10079 * The first time through, the start address could be anywhere within the
10080 * vm_map_entry we found. So adjust the offset to correspond. After that,
10081 * the offset will always be zero to correspond to the beginning of the current
10085 offset
= (start
- entry
->vme_start
) + entry
->offset
;
10088 * Set the length so we don't go beyond the end of the map_entry or beyond the
10089 * end of the range we were given. This range could span also multiple map
10090 * entries all of which map different files, so make sure we only do the right
10091 * amount of I/O for each object. Note that it's possible for there to be
10092 * multiple map entries all referring to the same object but with different
10093 * page permissions, but it's not worth trying to optimize that case.
10096 len
= MIN(entry
->vme_end
- start
, end
- start
);
10098 if ((vm_size_t
) len
!= len
) {
10099 /* 32-bit overflow */
10100 len
= (vm_size_t
) (0 - PAGE_SIZE
);
10102 fault_info
.cluster_size
= (vm_size_t
) len
;
10103 fault_info
.lo_offset
= offset
;
10104 fault_info
.hi_offset
= offset
+ len
;
10105 fault_info
.user_tag
= entry
->alias
;
10108 * If there's no read permission to this mapping, then just skip it.
10111 if ((entry
->protection
& VM_PROT_READ
) == 0) {
10116 * Find the file object backing this map entry. If there is none,
10117 * then we simply ignore the "will need" advice for this entry and
10118 * go on to the next one.
10121 if ((object
= find_vnode_object(entry
)) == VM_OBJECT_NULL
) {
10125 vm_object_paging_begin(object
);
10126 pager
= object
->pager
;
10127 vm_object_unlock(object
);
10130 * Get the data from the object asynchronously.
10132 * Note that memory_object_data_request() places limits on the amount
10133 * of I/O it will do. Regardless of the len we specified, it won't do
10134 * more than MAX_UPL_TRANSFER and it silently truncates the len to that
10135 * size. This isn't necessarily bad since madvise shouldn't really be
10136 * used to page in unlimited amounts of data. Other Unix variants limit
10137 * the willneed case as well. If this turns out to be an issue for
10138 * developers, then we can always adjust the policy here and still be
10139 * backwards compatible since this is all just "advice".
10142 kr
= memory_object_data_request(
10144 offset
+ object
->paging_offset
,
10147 (memory_object_fault_info_t
)&fault_info
);
10149 vm_object_lock(object
);
10150 vm_object_paging_end(object
);
10151 vm_object_unlock(object
);
10154 * If we couldn't do the I/O for some reason, just give up on the
10155 * madvise. We still return success to the user since madvise isn't
10156 * supposed to fail when the advice can't be taken.
10159 if (kr
!= KERN_SUCCESS
) {
10166 kr
= KERN_INVALID_ADDRESS
;
10168 vm_map_unlock_read(map
);
10173 vm_map_entry_is_reusable(
10174 vm_map_entry_t entry
)
10176 vm_object_t object
;
10178 if (entry
->is_shared
||
10179 entry
->is_sub_map
||
10180 entry
->in_transition
||
10181 entry
->protection
!= VM_PROT_DEFAULT
||
10182 entry
->max_protection
!= VM_PROT_ALL
||
10183 entry
->inheritance
!= VM_INHERIT_DEFAULT
||
10185 entry
->permanent
||
10186 entry
->superpage_size
!= 0 ||
10187 entry
->zero_wired_pages
||
10188 entry
->wired_count
!= 0 ||
10189 entry
->user_wired_count
!= 0) {
10193 object
= entry
->object
.vm_object
;
10194 if (object
== VM_OBJECT_NULL
) {
10197 if (object
->ref_count
== 1 &&
10198 object
->wired_page_count
== 0 &&
10199 object
->copy
== VM_OBJECT_NULL
&&
10200 object
->shadow
== VM_OBJECT_NULL
&&
10201 object
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
&&
10202 object
->internal
&&
10203 !object
->true_share
&&
10204 object
->wimg_bits
== VM_WIMG_DEFAULT
&&
10205 !object
->code_signed
) {
10213 static kern_return_t
10214 vm_map_reuse_pages(
10216 vm_map_offset_t start
,
10217 vm_map_offset_t end
)
10219 vm_map_entry_t entry
;
10220 vm_object_t object
;
10221 vm_object_offset_t start_offset
, end_offset
;
10224 * The MADV_REUSE operation doesn't require any changes to the
10225 * vm_map_entry_t's, so the read lock is sufficient.
10228 vm_map_lock_read(map
);
10231 * The madvise semantics require that the address range be fully
10232 * allocated with no holes. Otherwise, we're required to return
10236 if (!vm_map_range_check(map
, start
, end
, &entry
)) {
10237 vm_map_unlock_read(map
);
10238 vm_page_stats_reusable
.reuse_pages_failure
++;
10239 return KERN_INVALID_ADDRESS
;
10243 * Examine each vm_map_entry_t in the range.
10245 for (; entry
!= vm_map_to_entry(map
) && entry
->vme_start
< end
;
10246 entry
= entry
->vme_next
) {
10248 * Sanity check on the VM map entry.
10250 if (! vm_map_entry_is_reusable(entry
)) {
10251 vm_map_unlock_read(map
);
10252 vm_page_stats_reusable
.reuse_pages_failure
++;
10253 return KERN_INVALID_ADDRESS
;
10257 * The first time through, the start address could be anywhere
10258 * within the vm_map_entry we found. So adjust the offset to
10261 if (entry
->vme_start
< start
) {
10262 start_offset
= start
- entry
->vme_start
;
10266 end_offset
= MIN(end
, entry
->vme_end
) - entry
->vme_start
;
10267 start_offset
+= entry
->offset
;
10268 end_offset
+= entry
->offset
;
10270 object
= entry
->object
.vm_object
;
10271 if (object
!= VM_OBJECT_NULL
) {
10272 vm_object_lock(object
);
10273 vm_object_reuse_pages(object
, start_offset
, end_offset
,
10275 vm_object_unlock(object
);
10278 if (entry
->alias
== VM_MEMORY_MALLOC_LARGE_REUSABLE
) {
10281 * We do not hold the VM map exclusively here.
10282 * The "alias" field is not that critical, so it's
10283 * safe to update it here, as long as it is the only
10284 * one that can be modified while holding the VM map
10287 entry
->alias
= VM_MEMORY_MALLOC_LARGE_REUSED
;
10291 vm_map_unlock_read(map
);
10292 vm_page_stats_reusable
.reuse_pages_success
++;
10293 return KERN_SUCCESS
;
10297 static kern_return_t
10298 vm_map_reusable_pages(
10300 vm_map_offset_t start
,
10301 vm_map_offset_t end
)
10303 vm_map_entry_t entry
;
10304 vm_object_t object
;
10305 vm_object_offset_t start_offset
, end_offset
;
10308 * The MADV_REUSABLE operation doesn't require any changes to the
10309 * vm_map_entry_t's, so the read lock is sufficient.
10312 vm_map_lock_read(map
);
10315 * The madvise semantics require that the address range be fully
10316 * allocated with no holes. Otherwise, we're required to return
10320 if (!vm_map_range_check(map
, start
, end
, &entry
)) {
10321 vm_map_unlock_read(map
);
10322 vm_page_stats_reusable
.reusable_pages_failure
++;
10323 return KERN_INVALID_ADDRESS
;
10327 * Examine each vm_map_entry_t in the range.
10329 for (; entry
!= vm_map_to_entry(map
) && entry
->vme_start
< end
;
10330 entry
= entry
->vme_next
) {
10331 int kill_pages
= 0;
10334 * Sanity check on the VM map entry.
10336 if (! vm_map_entry_is_reusable(entry
)) {
10337 vm_map_unlock_read(map
);
10338 vm_page_stats_reusable
.reusable_pages_failure
++;
10339 return KERN_INVALID_ADDRESS
;
10343 * The first time through, the start address could be anywhere
10344 * within the vm_map_entry we found. So adjust the offset to
10347 if (entry
->vme_start
< start
) {
10348 start_offset
= start
- entry
->vme_start
;
10352 end_offset
= MIN(end
, entry
->vme_end
) - entry
->vme_start
;
10353 start_offset
+= entry
->offset
;
10354 end_offset
+= entry
->offset
;
10356 object
= entry
->object
.vm_object
;
10357 if (object
== VM_OBJECT_NULL
)
10361 vm_object_lock(object
);
10362 if (object
->ref_count
== 1 && !object
->shadow
)
10366 if (kill_pages
!= -1) {
10367 vm_object_deactivate_pages(object
,
10369 end_offset
- start_offset
,
10371 TRUE
/*reusable_pages*/);
10373 vm_page_stats_reusable
.reusable_pages_shared
++;
10375 vm_object_unlock(object
);
10377 if (entry
->alias
== VM_MEMORY_MALLOC_LARGE
||
10378 entry
->alias
== VM_MEMORY_MALLOC_LARGE_REUSED
) {
10381 * We do not hold the VM map exclusively here.
10382 * The "alias" field is not that critical, so it's
10383 * safe to update it here, as long as it is the only
10384 * one that can be modified while holding the VM map
10387 entry
->alias
= VM_MEMORY_MALLOC_LARGE_REUSABLE
;
10391 vm_map_unlock_read(map
);
10392 vm_page_stats_reusable
.reusable_pages_success
++;
10393 return KERN_SUCCESS
;
10397 static kern_return_t
10400 vm_map_offset_t start
,
10401 vm_map_offset_t end
)
10403 vm_map_entry_t entry
;
10406 * The MADV_REUSABLE operation doesn't require any changes to the
10407 * vm_map_entry_t's, so the read lock is sufficient.
10410 vm_map_lock_read(map
);
10413 * The madvise semantics require that the address range be fully
10414 * allocated with no holes. Otherwise, we're required to return
10418 if (!vm_map_range_check(map
, start
, end
, &entry
)) {
10419 vm_map_unlock_read(map
);
10420 vm_page_stats_reusable
.can_reuse_failure
++;
10421 return KERN_INVALID_ADDRESS
;
10425 * Examine each vm_map_entry_t in the range.
10427 for (; entry
!= vm_map_to_entry(map
) && entry
->vme_start
< end
;
10428 entry
= entry
->vme_next
) {
10430 * Sanity check on the VM map entry.
10432 if (! vm_map_entry_is_reusable(entry
)) {
10433 vm_map_unlock_read(map
);
10434 vm_page_stats_reusable
.can_reuse_failure
++;
10435 return KERN_INVALID_ADDRESS
;
10439 vm_map_unlock_read(map
);
10440 vm_page_stats_reusable
.can_reuse_success
++;
10441 return KERN_SUCCESS
;
10446 #include <mach_kdb.h>
10448 #include <ddb/db_output.h>
10449 #include <vm/vm_print.h>
10451 #define printf db_printf
10454 * Forward declarations for internal functions.
10456 extern void vm_map_links_print(
10457 struct vm_map_links
*links
);
10459 extern void vm_map_header_print(
10460 struct vm_map_header
*header
);
10462 extern void vm_map_entry_print(
10463 vm_map_entry_t entry
);
10465 extern void vm_follow_entry(
10466 vm_map_entry_t entry
);
10468 extern void vm_follow_map(
10472 * vm_map_links_print: [ debug ]
10475 vm_map_links_print(
10476 struct vm_map_links
*links
)
10478 iprintf("prev = %08X next = %08X start = %016llX end = %016llX\n",
10481 (unsigned long long)links
->start
,
10482 (unsigned long long)links
->end
);
10486 * vm_map_header_print: [ debug ]
10489 vm_map_header_print(
10490 struct vm_map_header
*header
)
10492 vm_map_links_print(&header
->links
);
10493 iprintf("nentries = %08X, %sentries_pageable\n",
10495 (header
->entries_pageable
? "" : "!"));
10499 * vm_follow_entry: [ debug ]
10503 vm_map_entry_t entry
)
10507 iprintf("map entry %08X\n", entry
);
10511 shadows
= vm_follow_object(entry
->object
.vm_object
);
10512 iprintf("Total objects : %d\n",shadows
);
10518 * vm_map_entry_print: [ debug ]
10521 vm_map_entry_print(
10522 register vm_map_entry_t entry
)
10524 static const char *inheritance_name
[4] =
10525 { "share", "copy", "none", "?"};
10526 static const char *behavior_name
[4] =
10527 { "dflt", "rand", "seqtl", "rseqntl" };
10529 iprintf("map entry %08X - prev = %08X next = %08X\n", entry
, entry
->vme_prev
, entry
->vme_next
);
10533 vm_map_links_print(&entry
->links
);
10535 iprintf("start = %016llX end = %016llX - prot=%x/%x/%s\n",
10536 (unsigned long long)entry
->vme_start
,
10537 (unsigned long long)entry
->vme_end
,
10539 entry
->max_protection
,
10540 inheritance_name
[(entry
->inheritance
& 0x3)]);
10542 iprintf("behavior = %s, wired_count = %d, user_wired_count = %d\n",
10543 behavior_name
[(entry
->behavior
& 0x3)],
10544 entry
->wired_count
,
10545 entry
->user_wired_count
);
10546 iprintf("%sin_transition, %sneeds_wakeup\n",
10547 (entry
->in_transition
? "" : "!"),
10548 (entry
->needs_wakeup
? "" : "!"));
10550 if (entry
->is_sub_map
) {
10551 iprintf("submap = %08X - offset = %016llX\n",
10552 entry
->object
.sub_map
,
10553 (unsigned long long)entry
->offset
);
10555 iprintf("object = %08X offset = %016llX - ",
10556 entry
->object
.vm_object
,
10557 (unsigned long long)entry
->offset
);
10558 printf("%sis_shared, %sneeds_copy\n",
10559 (entry
->is_shared
? "" : "!"),
10560 (entry
->needs_copy
? "" : "!"));
10567 * vm_follow_map: [ debug ]
10573 register vm_map_entry_t entry
;
10575 iprintf("task map %08X\n", map
);
10579 for (entry
= vm_map_first_entry(map
);
10580 entry
&& entry
!= vm_map_to_entry(map
);
10581 entry
= entry
->vme_next
) {
10582 vm_follow_entry(entry
);
10589 * vm_map_print: [ debug ]
10595 register vm_map_entry_t entry
;
10599 #endif /* TASK_SWAPPER */
10601 map
= (vm_map_t
)(long)
10602 inmap
; /* Make sure we have the right type */
10604 iprintf("task map %08X\n", map
);
10608 vm_map_header_print(&map
->hdr
);
10610 iprintf("pmap = %08X size = %08X ref = %d hint = %08X first_free = %08X\n",
10617 iprintf("%swait_for_space, %swiring_required, timestamp = %d\n",
10618 (map
->wait_for_space
? "" : "!"),
10619 (map
->wiring_required
? "" : "!"),
10623 switch (map
->sw_state
) {
10628 swstate
= "SW_OUT";
10634 iprintf("res = %d, sw_state = %s\n", map
->res_count
, swstate
);
10635 #endif /* TASK_SWAPPER */
10637 for (entry
= vm_map_first_entry(map
);
10638 entry
&& entry
!= vm_map_to_entry(map
);
10639 entry
= entry
->vme_next
) {
10640 vm_map_entry_print(entry
);
10647 * Routine: vm_map_copy_print
10649 * Pretty-print a copy object for ddb.
10656 vm_map_copy_t copy
;
10657 vm_map_entry_t entry
;
10659 copy
= (vm_map_copy_t
)(long)
10660 incopy
; /* Make sure we have the right type */
10662 printf("copy object 0x%x\n", copy
);
10666 iprintf("type=%d", copy
->type
);
10667 switch (copy
->type
) {
10668 case VM_MAP_COPY_ENTRY_LIST
:
10669 printf("[entry_list]");
10672 case VM_MAP_COPY_OBJECT
:
10673 printf("[object]");
10676 case VM_MAP_COPY_KERNEL_BUFFER
:
10677 printf("[kernel_buffer]");
10681 printf("[bad type]");
10684 printf(", offset=0x%llx", (unsigned long long)copy
->offset
);
10685 printf(", size=0x%x\n", copy
->size
);
10687 switch (copy
->type
) {
10688 case VM_MAP_COPY_ENTRY_LIST
:
10689 vm_map_header_print(©
->cpy_hdr
);
10690 for (entry
= vm_map_copy_first_entry(copy
);
10691 entry
&& entry
!= vm_map_copy_to_entry(copy
);
10692 entry
= entry
->vme_next
) {
10693 vm_map_entry_print(entry
);
10697 case VM_MAP_COPY_OBJECT
:
10698 iprintf("object=0x%x\n", copy
->cpy_object
);
10701 case VM_MAP_COPY_KERNEL_BUFFER
:
10702 iprintf("kernel buffer=0x%x", copy
->cpy_kdata
);
10703 printf(", kalloc_size=0x%x\n", copy
->cpy_kalloc_size
);
10712 * db_vm_map_total_size(map) [ debug ]
10714 * return the total virtual size (in bytes) of the map
10717 db_vm_map_total_size(
10720 vm_map_entry_t entry
;
10721 vm_map_size_t total
;
10724 map
= (vm_map_t
)(long)
10725 inmap
; /* Make sure we have the right type */
10728 for (entry
= vm_map_first_entry(map
);
10729 entry
!= vm_map_to_entry(map
);
10730 entry
= entry
->vme_next
) {
10731 total
+= entry
->vme_end
- entry
->vme_start
;
10737 #endif /* MACH_KDB */
10740 * Routine: vm_map_entry_insert
10742 * Descritpion: This routine inserts a new vm_entry in a locked map.
10745 vm_map_entry_insert(
10747 vm_map_entry_t insp_entry
,
10748 vm_map_offset_t start
,
10749 vm_map_offset_t end
,
10750 vm_object_t object
,
10751 vm_object_offset_t offset
,
10752 boolean_t needs_copy
,
10753 boolean_t is_shared
,
10754 boolean_t in_transition
,
10755 vm_prot_t cur_protection
,
10756 vm_prot_t max_protection
,
10757 vm_behavior_t behavior
,
10758 vm_inherit_t inheritance
,
10759 unsigned wired_count
,
10760 boolean_t no_cache
,
10761 boolean_t permanent
,
10762 unsigned int superpage_size
)
10764 vm_map_entry_t new_entry
;
10766 assert(insp_entry
!= (vm_map_entry_t
)0);
10768 new_entry
= vm_map_entry_create(map
);
10770 new_entry
->vme_start
= start
;
10771 new_entry
->vme_end
= end
;
10772 assert(page_aligned(new_entry
->vme_start
));
10773 assert(page_aligned(new_entry
->vme_end
));
10775 new_entry
->object
.vm_object
= object
;
10776 new_entry
->offset
= offset
;
10777 new_entry
->is_shared
= is_shared
;
10778 new_entry
->is_sub_map
= FALSE
;
10779 new_entry
->needs_copy
= needs_copy
;
10780 new_entry
->in_transition
= in_transition
;
10781 new_entry
->needs_wakeup
= FALSE
;
10782 new_entry
->inheritance
= inheritance
;
10783 new_entry
->protection
= cur_protection
;
10784 new_entry
->max_protection
= max_protection
;
10785 new_entry
->behavior
= behavior
;
10786 new_entry
->wired_count
= wired_count
;
10787 new_entry
->user_wired_count
= 0;
10788 new_entry
->use_pmap
= FALSE
;
10789 new_entry
->alias
= 0;
10790 new_entry
->zero_wired_pages
= FALSE
;
10791 new_entry
->no_cache
= no_cache
;
10792 new_entry
->permanent
= permanent
;
10793 new_entry
->superpage_size
= superpage_size
;
10796 * Insert the new entry into the list.
10799 vm_map_entry_link(map
, insp_entry
, new_entry
);
10800 map
->size
+= end
- start
;
10803 * Update the free space hint and the lookup hint.
10806 SAVE_HINT_MAP_WRITE(map
, new_entry
);
10811 * Routine: vm_map_remap_extract
10813 * Descritpion: This routine returns a vm_entry list from a map.
10815 static kern_return_t
10816 vm_map_remap_extract(
10818 vm_map_offset_t addr
,
10819 vm_map_size_t size
,
10821 struct vm_map_header
*map_header
,
10822 vm_prot_t
*cur_protection
,
10823 vm_prot_t
*max_protection
,
10824 /* What, no behavior? */
10825 vm_inherit_t inheritance
,
10826 boolean_t pageable
)
10828 kern_return_t result
;
10829 vm_map_size_t mapped_size
;
10830 vm_map_size_t tmp_size
;
10831 vm_map_entry_t src_entry
; /* result of last map lookup */
10832 vm_map_entry_t new_entry
;
10833 vm_object_offset_t offset
;
10834 vm_map_offset_t map_address
;
10835 vm_map_offset_t src_start
; /* start of entry to map */
10836 vm_map_offset_t src_end
; /* end of region to be mapped */
10837 vm_object_t object
;
10838 vm_map_version_t version
;
10839 boolean_t src_needs_copy
;
10840 boolean_t new_entry_needs_copy
;
10842 assert(map
!= VM_MAP_NULL
);
10843 assert(size
!= 0 && size
== vm_map_round_page(size
));
10844 assert(inheritance
== VM_INHERIT_NONE
||
10845 inheritance
== VM_INHERIT_COPY
||
10846 inheritance
== VM_INHERIT_SHARE
);
10849 * Compute start and end of region.
10851 src_start
= vm_map_trunc_page(addr
);
10852 src_end
= vm_map_round_page(src_start
+ size
);
10855 * Initialize map_header.
10857 map_header
->links
.next
= (struct vm_map_entry
*)&map_header
->links
;
10858 map_header
->links
.prev
= (struct vm_map_entry
*)&map_header
->links
;
10859 map_header
->nentries
= 0;
10860 map_header
->entries_pageable
= pageable
;
10862 *cur_protection
= VM_PROT_ALL
;
10863 *max_protection
= VM_PROT_ALL
;
10867 result
= KERN_SUCCESS
;
10870 * The specified source virtual space might correspond to
10871 * multiple map entries, need to loop on them.
10874 while (mapped_size
!= size
) {
10875 vm_map_size_t entry_size
;
10878 * Find the beginning of the region.
10880 if (! vm_map_lookup_entry(map
, src_start
, &src_entry
)) {
10881 result
= KERN_INVALID_ADDRESS
;
10885 if (src_start
< src_entry
->vme_start
||
10886 (mapped_size
&& src_start
!= src_entry
->vme_start
)) {
10887 result
= KERN_INVALID_ADDRESS
;
10891 tmp_size
= size
- mapped_size
;
10892 if (src_end
> src_entry
->vme_end
)
10893 tmp_size
-= (src_end
- src_entry
->vme_end
);
10895 entry_size
= (vm_map_size_t
)(src_entry
->vme_end
-
10896 src_entry
->vme_start
);
10898 if(src_entry
->is_sub_map
) {
10899 vm_map_reference(src_entry
->object
.sub_map
);
10900 object
= VM_OBJECT_NULL
;
10902 object
= src_entry
->object
.vm_object
;
10904 if (object
== VM_OBJECT_NULL
) {
10905 object
= vm_object_allocate(entry_size
);
10906 src_entry
->offset
= 0;
10907 src_entry
->object
.vm_object
= object
;
10908 } else if (object
->copy_strategy
!=
10909 MEMORY_OBJECT_COPY_SYMMETRIC
) {
10911 * We are already using an asymmetric
10912 * copy, and therefore we already have
10913 * the right object.
10915 assert(!src_entry
->needs_copy
);
10916 } else if (src_entry
->needs_copy
|| object
->shadowed
||
10917 (object
->internal
&& !object
->true_share
&&
10918 !src_entry
->is_shared
&&
10919 object
->size
> entry_size
)) {
10921 vm_object_shadow(&src_entry
->object
.vm_object
,
10922 &src_entry
->offset
,
10925 if (!src_entry
->needs_copy
&&
10926 (src_entry
->protection
& VM_PROT_WRITE
)) {
10929 prot
= src_entry
->protection
& ~VM_PROT_WRITE
;
10931 if (override_nx(map
, src_entry
->alias
) && prot
)
10932 prot
|= VM_PROT_EXECUTE
;
10935 vm_object_pmap_protect(
10936 src_entry
->object
.vm_object
,
10940 src_entry
->vme_start
,
10943 pmap_protect(vm_map_pmap(map
),
10944 src_entry
->vme_start
,
10945 src_entry
->vme_end
,
10950 object
= src_entry
->object
.vm_object
;
10951 src_entry
->needs_copy
= FALSE
;
10955 vm_object_lock(object
);
10956 vm_object_reference_locked(object
); /* object ref. for new entry */
10957 if (object
->copy_strategy
==
10958 MEMORY_OBJECT_COPY_SYMMETRIC
) {
10959 object
->copy_strategy
=
10960 MEMORY_OBJECT_COPY_DELAY
;
10962 vm_object_unlock(object
);
10965 offset
= src_entry
->offset
+ (src_start
- src_entry
->vme_start
);
10967 new_entry
= _vm_map_entry_create(map_header
);
10968 vm_map_entry_copy(new_entry
, src_entry
);
10969 new_entry
->use_pmap
= FALSE
; /* clr address space specifics */
10971 new_entry
->vme_start
= map_address
;
10972 new_entry
->vme_end
= map_address
+ tmp_size
;
10973 new_entry
->inheritance
= inheritance
;
10974 new_entry
->offset
= offset
;
10977 * The new region has to be copied now if required.
10981 src_entry
->is_shared
= TRUE
;
10982 new_entry
->is_shared
= TRUE
;
10983 if (!(new_entry
->is_sub_map
))
10984 new_entry
->needs_copy
= FALSE
;
10986 } else if (src_entry
->is_sub_map
) {
10987 /* make this a COW sub_map if not already */
10988 new_entry
->needs_copy
= TRUE
;
10989 object
= VM_OBJECT_NULL
;
10990 } else if (src_entry
->wired_count
== 0 &&
10991 vm_object_copy_quickly(&new_entry
->object
.vm_object
,
10993 (new_entry
->vme_end
-
10994 new_entry
->vme_start
),
10996 &new_entry_needs_copy
)) {
10998 new_entry
->needs_copy
= new_entry_needs_copy
;
10999 new_entry
->is_shared
= FALSE
;
11002 * Handle copy_on_write semantics.
11004 if (src_needs_copy
&& !src_entry
->needs_copy
) {
11007 prot
= src_entry
->protection
& ~VM_PROT_WRITE
;
11009 if (override_nx(map
, src_entry
->alias
) && prot
)
11010 prot
|= VM_PROT_EXECUTE
;
11012 vm_object_pmap_protect(object
,
11015 ((src_entry
->is_shared
11017 PMAP_NULL
: map
->pmap
),
11018 src_entry
->vme_start
,
11021 src_entry
->needs_copy
= TRUE
;
11024 * Throw away the old object reference of the new entry.
11026 vm_object_deallocate(object
);
11029 new_entry
->is_shared
= FALSE
;
11032 * The map can be safely unlocked since we
11033 * already hold a reference on the object.
11035 * Record the timestamp of the map for later
11036 * verification, and unlock the map.
11038 version
.main_timestamp
= map
->timestamp
;
11039 vm_map_unlock(map
); /* Increments timestamp once! */
11042 * Perform the copy.
11044 if (src_entry
->wired_count
> 0) {
11045 vm_object_lock(object
);
11046 result
= vm_object_copy_slowly(
11051 &new_entry
->object
.vm_object
);
11053 new_entry
->offset
= 0;
11054 new_entry
->needs_copy
= FALSE
;
11056 result
= vm_object_copy_strategically(
11060 &new_entry
->object
.vm_object
,
11061 &new_entry
->offset
,
11062 &new_entry_needs_copy
);
11064 new_entry
->needs_copy
= new_entry_needs_copy
;
11068 * Throw away the old object reference of the new entry.
11070 vm_object_deallocate(object
);
11072 if (result
!= KERN_SUCCESS
&&
11073 result
!= KERN_MEMORY_RESTART_COPY
) {
11074 _vm_map_entry_dispose(map_header
, new_entry
);
11079 * Verify that the map has not substantially
11080 * changed while the copy was being made.
11084 if (version
.main_timestamp
+ 1 != map
->timestamp
) {
11086 * Simple version comparison failed.
11088 * Retry the lookup and verify that the
11089 * same object/offset are still present.
11091 vm_object_deallocate(new_entry
->
11093 _vm_map_entry_dispose(map_header
, new_entry
);
11094 if (result
== KERN_MEMORY_RESTART_COPY
)
11095 result
= KERN_SUCCESS
;
11099 if (result
== KERN_MEMORY_RESTART_COPY
) {
11100 vm_object_reference(object
);
11105 _vm_map_entry_link(map_header
,
11106 map_header
->links
.prev
, new_entry
);
11108 *cur_protection
&= src_entry
->protection
;
11109 *max_protection
&= src_entry
->max_protection
;
11111 map_address
+= tmp_size
;
11112 mapped_size
+= tmp_size
;
11113 src_start
+= tmp_size
;
11117 vm_map_unlock(map
);
11118 if (result
!= KERN_SUCCESS
) {
11120 * Free all allocated elements.
11122 for (src_entry
= map_header
->links
.next
;
11123 src_entry
!= (struct vm_map_entry
*)&map_header
->links
;
11124 src_entry
= new_entry
) {
11125 new_entry
= src_entry
->vme_next
;
11126 _vm_map_entry_unlink(map_header
, src_entry
);
11127 vm_object_deallocate(src_entry
->object
.vm_object
);
11128 _vm_map_entry_dispose(map_header
, src_entry
);
11135 * Routine: vm_remap
11137 * Map portion of a task's address space.
11138 * Mapped region must not overlap more than
11139 * one vm memory object. Protections and
11140 * inheritance attributes remain the same
11141 * as in the original task and are out parameters.
11142 * Source and Target task can be identical
11143 * Other attributes are identical as for vm_map()
11147 vm_map_t target_map
,
11148 vm_map_address_t
*address
,
11149 vm_map_size_t size
,
11150 vm_map_offset_t mask
,
11151 boolean_t anywhere
,
11153 vm_map_offset_t memory_address
,
11155 vm_prot_t
*cur_protection
,
11156 vm_prot_t
*max_protection
,
11157 vm_inherit_t inheritance
)
11159 kern_return_t result
;
11160 vm_map_entry_t entry
;
11161 vm_map_entry_t insp_entry
= VM_MAP_ENTRY_NULL
;
11162 vm_map_entry_t new_entry
;
11163 struct vm_map_header map_header
;
11165 if (target_map
== VM_MAP_NULL
)
11166 return KERN_INVALID_ARGUMENT
;
11168 switch (inheritance
) {
11169 case VM_INHERIT_NONE
:
11170 case VM_INHERIT_COPY
:
11171 case VM_INHERIT_SHARE
:
11172 if (size
!= 0 && src_map
!= VM_MAP_NULL
)
11176 return KERN_INVALID_ARGUMENT
;
11179 size
= vm_map_round_page(size
);
11181 result
= vm_map_remap_extract(src_map
, memory_address
,
11182 size
, copy
, &map_header
,
11189 if (result
!= KERN_SUCCESS
) {
11194 * Allocate/check a range of free virtual address
11195 * space for the target
11197 *address
= vm_map_trunc_page(*address
);
11198 vm_map_lock(target_map
);
11199 result
= vm_map_remap_range_allocate(target_map
, address
, size
,
11200 mask
, anywhere
, &insp_entry
);
11202 for (entry
= map_header
.links
.next
;
11203 entry
!= (struct vm_map_entry
*)&map_header
.links
;
11204 entry
= new_entry
) {
11205 new_entry
= entry
->vme_next
;
11206 _vm_map_entry_unlink(&map_header
, entry
);
11207 if (result
== KERN_SUCCESS
) {
11208 entry
->vme_start
+= *address
;
11209 entry
->vme_end
+= *address
;
11210 vm_map_entry_link(target_map
, insp_entry
, entry
);
11211 insp_entry
= entry
;
11213 if (!entry
->is_sub_map
) {
11214 vm_object_deallocate(entry
->object
.vm_object
);
11216 vm_map_deallocate(entry
->object
.sub_map
);
11218 _vm_map_entry_dispose(&map_header
, entry
);
11222 if (result
== KERN_SUCCESS
) {
11223 target_map
->size
+= size
;
11224 SAVE_HINT_MAP_WRITE(target_map
, insp_entry
);
11226 vm_map_unlock(target_map
);
11228 if (result
== KERN_SUCCESS
&& target_map
->wiring_required
)
11229 result
= vm_map_wire(target_map
, *address
,
11230 *address
+ size
, *cur_protection
, TRUE
);
11235 * Routine: vm_map_remap_range_allocate
11238 * Allocate a range in the specified virtual address map.
11239 * returns the address and the map entry just before the allocated
11242 * Map must be locked.
11245 static kern_return_t
11246 vm_map_remap_range_allocate(
11248 vm_map_address_t
*address
, /* IN/OUT */
11249 vm_map_size_t size
,
11250 vm_map_offset_t mask
,
11251 boolean_t anywhere
,
11252 vm_map_entry_t
*map_entry
) /* OUT */
11254 register vm_map_entry_t entry
;
11255 register vm_map_offset_t start
;
11256 register vm_map_offset_t end
;
11265 * Calculate the first possible address.
11268 if (start
< map
->min_offset
)
11269 start
= map
->min_offset
;
11270 if (start
> map
->max_offset
)
11271 return(KERN_NO_SPACE
);
11274 * Look for the first possible address;
11275 * if there's already something at this
11276 * address, we have to start after it.
11279 assert(first_free_is_valid(map
));
11280 if (start
== map
->min_offset
) {
11281 if ((entry
= map
->first_free
) != vm_map_to_entry(map
))
11282 start
= entry
->vme_end
;
11284 vm_map_entry_t tmp_entry
;
11285 if (vm_map_lookup_entry(map
, start
, &tmp_entry
))
11286 start
= tmp_entry
->vme_end
;
11291 * In any case, the "entry" always precedes
11292 * the proposed new region throughout the
11297 register vm_map_entry_t next
;
11300 * Find the end of the proposed new region.
11301 * Be sure we didn't go beyond the end, or
11302 * wrap around the address.
11305 end
= ((start
+ mask
) & ~mask
);
11307 return(KERN_NO_SPACE
);
11311 if ((end
> map
->max_offset
) || (end
< start
)) {
11312 if (map
->wait_for_space
) {
11313 if (size
<= (map
->max_offset
-
11314 map
->min_offset
)) {
11315 assert_wait((event_t
) map
, THREAD_INTERRUPTIBLE
);
11316 vm_map_unlock(map
);
11317 thread_block(THREAD_CONTINUE_NULL
);
11323 return(KERN_NO_SPACE
);
11327 * If there are no more entries, we must win.
11330 next
= entry
->vme_next
;
11331 if (next
== vm_map_to_entry(map
))
11335 * If there is another entry, it must be
11336 * after the end of the potential new region.
11339 if (next
->vme_start
>= end
)
11343 * Didn't fit -- move to the next entry.
11347 start
= entry
->vme_end
;
11351 vm_map_entry_t temp_entry
;
11355 * the address doesn't itself violate
11356 * the mask requirement.
11359 if ((start
& mask
) != 0)
11360 return(KERN_NO_SPACE
);
11364 * ... the address is within bounds
11367 end
= start
+ size
;
11369 if ((start
< map
->min_offset
) ||
11370 (end
> map
->max_offset
) ||
11372 return(KERN_INVALID_ADDRESS
);
11376 * ... the starting address isn't allocated
11379 if (vm_map_lookup_entry(map
, start
, &temp_entry
))
11380 return(KERN_NO_SPACE
);
11382 entry
= temp_entry
;
11385 * ... the next region doesn't overlap the
11389 if ((entry
->vme_next
!= vm_map_to_entry(map
)) &&
11390 (entry
->vme_next
->vme_start
< end
))
11391 return(KERN_NO_SPACE
);
11393 *map_entry
= entry
;
11394 return(KERN_SUCCESS
);
11400 * Set the address map for the current thread to the specified map
11408 thread_t thread
= current_thread();
11409 vm_map_t oldmap
= thread
->map
;
11411 mp_disable_preemption();
11412 mycpu
= cpu_number();
11415 * Deactivate the current map and activate the requested map
11417 PMAP_SWITCH_USER(thread
, map
, mycpu
);
11419 mp_enable_preemption();
11425 * Routine: vm_map_write_user
11428 * Copy out data from a kernel space into space in the
11429 * destination map. The space must already exist in the
11431 * NOTE: This routine should only be called by threads
11432 * which can block on a page fault. i.e. kernel mode user
11440 vm_map_address_t dst_addr
,
11443 kern_return_t kr
= KERN_SUCCESS
;
11445 if(current_map() == map
) {
11446 if (copyout(src_p
, dst_addr
, size
)) {
11447 kr
= KERN_INVALID_ADDRESS
;
11452 /* take on the identity of the target map while doing */
11455 vm_map_reference(map
);
11456 oldmap
= vm_map_switch(map
);
11457 if (copyout(src_p
, dst_addr
, size
)) {
11458 kr
= KERN_INVALID_ADDRESS
;
11460 vm_map_switch(oldmap
);
11461 vm_map_deallocate(map
);
11467 * Routine: vm_map_read_user
11470 * Copy in data from a user space source map into the
11471 * kernel map. The space must already exist in the
11473 * NOTE: This routine should only be called by threads
11474 * which can block on a page fault. i.e. kernel mode user
11481 vm_map_address_t src_addr
,
11485 kern_return_t kr
= KERN_SUCCESS
;
11487 if(current_map() == map
) {
11488 if (copyin(src_addr
, dst_p
, size
)) {
11489 kr
= KERN_INVALID_ADDRESS
;
11494 /* take on the identity of the target map while doing */
11497 vm_map_reference(map
);
11498 oldmap
= vm_map_switch(map
);
11499 if (copyin(src_addr
, dst_p
, size
)) {
11500 kr
= KERN_INVALID_ADDRESS
;
11502 vm_map_switch(oldmap
);
11503 vm_map_deallocate(map
);
11510 * vm_map_check_protection:
11512 * Assert that the target map allows the specified
11513 * privilege on the entire address region given.
11514 * The entire region must be allocated.
11517 vm_map_check_protection(vm_map_t map
, vm_map_offset_t start
,
11518 vm_map_offset_t end
, vm_prot_t protection
)
11520 vm_map_entry_t entry
;
11521 vm_map_entry_t tmp_entry
;
11525 if (start
< vm_map_min(map
) || end
> vm_map_max(map
) || start
> end
)
11527 vm_map_unlock(map
);
11531 if (!vm_map_lookup_entry(map
, start
, &tmp_entry
)) {
11532 vm_map_unlock(map
);
11538 while (start
< end
) {
11539 if (entry
== vm_map_to_entry(map
)) {
11540 vm_map_unlock(map
);
11545 * No holes allowed!
11548 if (start
< entry
->vme_start
) {
11549 vm_map_unlock(map
);
11554 * Check protection associated with entry.
11557 if ((entry
->protection
& protection
) != protection
) {
11558 vm_map_unlock(map
);
11562 /* go to next entry */
11564 start
= entry
->vme_end
;
11565 entry
= entry
->vme_next
;
11567 vm_map_unlock(map
);
11572 vm_map_purgable_control(
11574 vm_map_offset_t address
,
11575 vm_purgable_t control
,
11578 vm_map_entry_t entry
;
11579 vm_object_t object
;
11583 * Vet all the input parameters and current type and state of the
11584 * underlaying object. Return with an error if anything is amiss.
11586 if (map
== VM_MAP_NULL
)
11587 return(KERN_INVALID_ARGUMENT
);
11589 if (control
!= VM_PURGABLE_SET_STATE
&&
11590 control
!= VM_PURGABLE_GET_STATE
&&
11591 control
!= VM_PURGABLE_PURGE_ALL
)
11592 return(KERN_INVALID_ARGUMENT
);
11594 if (control
== VM_PURGABLE_PURGE_ALL
) {
11595 vm_purgeable_object_purge_all();
11596 return KERN_SUCCESS
;
11599 if (control
== VM_PURGABLE_SET_STATE
&&
11600 (((*state
& ~(VM_PURGABLE_ALL_MASKS
)) != 0) ||
11601 ((*state
& VM_PURGABLE_STATE_MASK
) > VM_PURGABLE_STATE_MASK
)))
11602 return(KERN_INVALID_ARGUMENT
);
11604 vm_map_lock_read(map
);
11606 if (!vm_map_lookup_entry(map
, address
, &entry
) || entry
->is_sub_map
) {
11609 * Must pass a valid non-submap address.
11611 vm_map_unlock_read(map
);
11612 return(KERN_INVALID_ADDRESS
);
11615 if ((entry
->protection
& VM_PROT_WRITE
) == 0) {
11617 * Can't apply purgable controls to something you can't write.
11619 vm_map_unlock_read(map
);
11620 return(KERN_PROTECTION_FAILURE
);
11623 object
= entry
->object
.vm_object
;
11624 if (object
== VM_OBJECT_NULL
) {
11626 * Object must already be present or it can't be purgable.
11628 vm_map_unlock_read(map
);
11629 return KERN_INVALID_ARGUMENT
;
11632 vm_object_lock(object
);
11634 if (entry
->offset
!= 0 ||
11635 entry
->vme_end
- entry
->vme_start
!= object
->size
) {
11637 * Can only apply purgable controls to the whole (existing)
11640 vm_map_unlock_read(map
);
11641 vm_object_unlock(object
);
11642 return KERN_INVALID_ARGUMENT
;
11645 vm_map_unlock_read(map
);
11647 kr
= vm_object_purgable_control(object
, control
, state
);
11649 vm_object_unlock(object
);
11655 vm_map_page_query_internal(
11656 vm_map_t target_map
,
11657 vm_map_offset_t offset
,
11662 vm_page_info_basic_data_t info
;
11663 mach_msg_type_number_t count
;
11665 count
= VM_PAGE_INFO_BASIC_COUNT
;
11666 kr
= vm_map_page_info(target_map
,
11668 VM_PAGE_INFO_BASIC
,
11669 (vm_page_info_t
) &info
,
11671 if (kr
== KERN_SUCCESS
) {
11672 *disposition
= info
.disposition
;
11673 *ref_count
= info
.ref_count
;
11685 vm_map_offset_t offset
,
11686 vm_page_info_flavor_t flavor
,
11687 vm_page_info_t info
,
11688 mach_msg_type_number_t
*count
)
11690 vm_map_entry_t map_entry
;
11691 vm_object_t object
;
11694 kern_return_t retval
= KERN_SUCCESS
;
11695 boolean_t top_object
;
11698 vm_object_id_t object_id
;
11699 vm_page_info_basic_t basic_info
;
11703 case VM_PAGE_INFO_BASIC
:
11704 if (*count
!= VM_PAGE_INFO_BASIC_COUNT
) {
11705 return KERN_INVALID_ARGUMENT
;
11709 return KERN_INVALID_ARGUMENT
;
11718 retval
= KERN_SUCCESS
;
11719 offset
= vm_map_trunc_page(offset
);
11721 vm_map_lock_read(map
);
11724 * First, find the map entry covering "offset", going down
11725 * submaps if necessary.
11728 if (!vm_map_lookup_entry(map
, offset
, &map_entry
)) {
11729 vm_map_unlock_read(map
);
11730 return KERN_INVALID_ADDRESS
;
11732 /* compute offset from this map entry's start */
11733 offset
-= map_entry
->vme_start
;
11734 /* compute offset into this map entry's object (or submap) */
11735 offset
+= map_entry
->offset
;
11737 if (map_entry
->is_sub_map
) {
11740 sub_map
= map_entry
->object
.sub_map
;
11741 vm_map_lock_read(sub_map
);
11742 vm_map_unlock_read(map
);
11746 ref_count
= MAX(ref_count
, map
->ref_count
);
11752 object
= map_entry
->object
.vm_object
;
11753 if (object
== VM_OBJECT_NULL
) {
11754 /* no object -> no page */
11755 vm_map_unlock_read(map
);
11759 vm_object_lock(object
);
11760 vm_map_unlock_read(map
);
11763 * Go down the VM object shadow chain until we find the page
11764 * we're looking for.
11767 ref_count
= MAX(ref_count
, object
->ref_count
);
11769 m
= vm_page_lookup(object
, offset
);
11771 if (m
!= VM_PAGE_NULL
) {
11772 disposition
|= VM_PAGE_QUERY_PAGE_PRESENT
;
11776 if (object
->existence_map
) {
11777 if (vm_external_state_get(object
->existence_map
,
11779 VM_EXTERNAL_STATE_EXISTS
) {
11781 * this page has been paged out
11783 disposition
|= VM_PAGE_QUERY_PAGE_PAGED_OUT
;
11789 if (object
->internal
&&
11791 !object
->terminating
&&
11792 object
->pager_ready
) {
11794 memory_object_t pager
;
11796 vm_object_paging_begin(object
);
11797 pager
= object
->pager
;
11798 vm_object_unlock(object
);
11801 * Ask the default pager if
11802 * it has this page.
11804 kr
= memory_object_data_request(
11806 offset
+ object
->paging_offset
,
11807 0, /* just poke the pager */
11811 vm_object_lock(object
);
11812 vm_object_paging_end(object
);
11814 if (kr
== KERN_SUCCESS
) {
11815 /* the default pager has it */
11816 disposition
|= VM_PAGE_QUERY_PAGE_PAGED_OUT
;
11822 if (object
->shadow
!= VM_OBJECT_NULL
) {
11823 vm_object_t shadow
;
11825 offset
+= object
->shadow_offset
;
11826 shadow
= object
->shadow
;
11828 vm_object_lock(shadow
);
11829 vm_object_unlock(object
);
11832 top_object
= FALSE
;
11835 // if (!object->internal)
11837 // retval = KERN_FAILURE;
11838 // goto done_with_object;
11843 /* The ref_count is not strictly accurate, it measures the number */
11844 /* of entities holding a ref on the object, they may not be mapping */
11845 /* the object or may not be mapping the section holding the */
11846 /* target page but its still a ball park number and though an over- */
11847 /* count, it picks up the copy-on-write cases */
11849 /* We could also get a picture of page sharing from pmap_attributes */
11850 /* but this would under count as only faulted-in mappings would */
11853 if (top_object
== TRUE
&& object
->shadow
)
11854 disposition
|= VM_PAGE_QUERY_PAGE_COPIED
;
11856 if (! object
->internal
)
11857 disposition
|= VM_PAGE_QUERY_PAGE_EXTERNAL
;
11859 if (m
== VM_PAGE_NULL
)
11860 goto done_with_object
;
11862 if (m
->fictitious
) {
11863 disposition
|= VM_PAGE_QUERY_PAGE_FICTITIOUS
;
11864 goto done_with_object
;
11866 if (m
->dirty
|| pmap_is_modified(m
->phys_page
))
11867 disposition
|= VM_PAGE_QUERY_PAGE_DIRTY
;
11869 if (m
->reference
|| pmap_is_referenced(m
->phys_page
))
11870 disposition
|= VM_PAGE_QUERY_PAGE_REF
;
11872 if (m
->speculative
)
11873 disposition
|= VM_PAGE_QUERY_PAGE_SPECULATIVE
;
11875 if (m
->cs_validated
)
11876 disposition
|= VM_PAGE_QUERY_PAGE_CS_VALIDATED
;
11878 disposition
|= VM_PAGE_QUERY_PAGE_CS_TAINTED
;
11881 vm_object_unlock(object
);
11885 case VM_PAGE_INFO_BASIC
:
11886 basic_info
= (vm_page_info_basic_t
) info
;
11887 basic_info
->disposition
= disposition
;
11888 basic_info
->ref_count
= ref_count
;
11889 basic_info
->object_id
= (vm_object_id_t
) (uintptr_t) object
;
11890 basic_info
->offset
= (memory_object_offset_t
) offset
;
11891 basic_info
->depth
= depth
;
11901 * Synchronises the memory range specified with its backing store
11902 * image by either flushing or cleaning the contents to the appropriate
11903 * memory manager engaging in a memory object synchronize dialog with
11904 * the manager. The client doesn't return until the manager issues
11905 * m_o_s_completed message. MIG Magically converts user task parameter
11906 * to the task's address map.
11908 * interpretation of sync_flags
11909 * VM_SYNC_INVALIDATE - discard pages, only return precious
11910 * pages to manager.
11912 * VM_SYNC_INVALIDATE & (VM_SYNC_SYNCHRONOUS | VM_SYNC_ASYNCHRONOUS)
11913 * - discard pages, write dirty or precious
11914 * pages back to memory manager.
11916 * VM_SYNC_SYNCHRONOUS | VM_SYNC_ASYNCHRONOUS
11917 * - write dirty or precious pages back to
11918 * the memory manager.
11920 * VM_SYNC_CONTIGUOUS - does everything normally, but if there
11921 * is a hole in the region, and we would
11922 * have returned KERN_SUCCESS, return
11923 * KERN_INVALID_ADDRESS instead.
11926 * The memory object attributes have not yet been implemented, this
11927 * function will have to deal with the invalidate attribute
11930 * KERN_INVALID_TASK Bad task parameter
11931 * KERN_INVALID_ARGUMENT both sync and async were specified.
11932 * KERN_SUCCESS The usual.
11933 * KERN_INVALID_ADDRESS There was a hole in the region.
11939 vm_map_address_t address
,
11940 vm_map_size_t size
,
11941 vm_sync_t sync_flags
)
11944 msync_req_t new_msr
;
11945 queue_chain_t req_q
; /* queue of requests for this msync */
11946 vm_map_entry_t entry
;
11947 vm_map_size_t amount_left
;
11948 vm_object_offset_t offset
;
11949 boolean_t do_sync_req
;
11950 boolean_t had_hole
= FALSE
;
11951 memory_object_t pager
;
11953 if ((sync_flags
& VM_SYNC_ASYNCHRONOUS
) &&
11954 (sync_flags
& VM_SYNC_SYNCHRONOUS
))
11955 return(KERN_INVALID_ARGUMENT
);
11958 * align address and size on page boundaries
11960 size
= vm_map_round_page(address
+ size
) - vm_map_trunc_page(address
);
11961 address
= vm_map_trunc_page(address
);
11963 if (map
== VM_MAP_NULL
)
11964 return(KERN_INVALID_TASK
);
11967 return(KERN_SUCCESS
);
11969 queue_init(&req_q
);
11970 amount_left
= size
;
11972 while (amount_left
> 0) {
11973 vm_object_size_t flush_size
;
11974 vm_object_t object
;
11977 if (!vm_map_lookup_entry(map
,
11978 vm_map_trunc_page(address
), &entry
)) {
11980 vm_map_size_t skip
;
11983 * hole in the address map.
11988 * Check for empty map.
11990 if (entry
== vm_map_to_entry(map
) &&
11991 entry
->vme_next
== entry
) {
11992 vm_map_unlock(map
);
11996 * Check that we don't wrap and that
11997 * we have at least one real map entry.
11999 if ((map
->hdr
.nentries
== 0) ||
12000 (entry
->vme_next
->vme_start
< address
)) {
12001 vm_map_unlock(map
);
12005 * Move up to the next entry if needed
12007 skip
= (entry
->vme_next
->vme_start
- address
);
12008 if (skip
>= amount_left
)
12011 amount_left
-= skip
;
12012 address
= entry
->vme_next
->vme_start
;
12013 vm_map_unlock(map
);
12017 offset
= address
- entry
->vme_start
;
12020 * do we have more to flush than is contained in this
12023 if (amount_left
+ entry
->vme_start
+ offset
> entry
->vme_end
) {
12024 flush_size
= entry
->vme_end
-
12025 (entry
->vme_start
+ offset
);
12027 flush_size
= amount_left
;
12029 amount_left
-= flush_size
;
12030 address
+= flush_size
;
12032 if (entry
->is_sub_map
== TRUE
) {
12033 vm_map_t local_map
;
12034 vm_map_offset_t local_offset
;
12036 local_map
= entry
->object
.sub_map
;
12037 local_offset
= entry
->offset
;
12038 vm_map_unlock(map
);
12043 sync_flags
) == KERN_INVALID_ADDRESS
) {
12048 object
= entry
->object
.vm_object
;
12051 * We can't sync this object if the object has not been
12054 if (object
== VM_OBJECT_NULL
) {
12055 vm_map_unlock(map
);
12058 offset
+= entry
->offset
;
12060 vm_object_lock(object
);
12062 if (sync_flags
& (VM_SYNC_KILLPAGES
| VM_SYNC_DEACTIVATE
)) {
12063 int kill_pages
= 0;
12064 boolean_t reusable_pages
= FALSE
;
12066 if (sync_flags
& VM_SYNC_KILLPAGES
) {
12067 if (object
->ref_count
== 1 && !object
->shadow
)
12072 if (kill_pages
!= -1)
12073 vm_object_deactivate_pages(object
, offset
,
12074 (vm_object_size_t
)flush_size
, kill_pages
, reusable_pages
);
12075 vm_object_unlock(object
);
12076 vm_map_unlock(map
);
12080 * We can't sync this object if there isn't a pager.
12081 * Don't bother to sync internal objects, since there can't
12082 * be any "permanent" storage for these objects anyway.
12084 if ((object
->pager
== MEMORY_OBJECT_NULL
) ||
12085 (object
->internal
) || (object
->private)) {
12086 vm_object_unlock(object
);
12087 vm_map_unlock(map
);
12091 * keep reference on the object until syncing is done
12093 vm_object_reference_locked(object
);
12094 vm_object_unlock(object
);
12096 vm_map_unlock(map
);
12098 do_sync_req
= vm_object_sync(object
,
12101 sync_flags
& VM_SYNC_INVALIDATE
,
12102 ((sync_flags
& VM_SYNC_SYNCHRONOUS
) ||
12103 (sync_flags
& VM_SYNC_ASYNCHRONOUS
)),
12104 sync_flags
& VM_SYNC_SYNCHRONOUS
);
12106 * only send a m_o_s if we returned pages or if the entry
12107 * is writable (ie dirty pages may have already been sent back)
12109 if (!do_sync_req
) {
12110 if ((sync_flags
& VM_SYNC_INVALIDATE
) && object
->resident_page_count
== 0) {
12112 * clear out the clustering and read-ahead hints
12114 vm_object_lock(object
);
12116 object
->pages_created
= 0;
12117 object
->pages_used
= 0;
12118 object
->sequential
= 0;
12119 object
->last_alloc
= 0;
12121 vm_object_unlock(object
);
12123 vm_object_deallocate(object
);
12126 msync_req_alloc(new_msr
);
12128 vm_object_lock(object
);
12129 offset
+= object
->paging_offset
;
12131 new_msr
->offset
= offset
;
12132 new_msr
->length
= flush_size
;
12133 new_msr
->object
= object
;
12134 new_msr
->flag
= VM_MSYNC_SYNCHRONIZING
;
12138 * We can't sync this object if there isn't a pager. The
12139 * pager can disappear anytime we're not holding the object
12140 * lock. So this has to be checked anytime we goto re_iterate.
12143 pager
= object
->pager
;
12145 if (pager
== MEMORY_OBJECT_NULL
) {
12146 vm_object_unlock(object
);
12147 vm_object_deallocate(object
);
12151 queue_iterate(&object
->msr_q
, msr
, msync_req_t
, msr_q
) {
12153 * need to check for overlapping entry, if found, wait
12154 * on overlapping msr to be done, then reiterate
12157 if (msr
->flag
== VM_MSYNC_SYNCHRONIZING
&&
12158 ((offset
>= msr
->offset
&&
12159 offset
< (msr
->offset
+ msr
->length
)) ||
12160 (msr
->offset
>= offset
&&
12161 msr
->offset
< (offset
+ flush_size
))))
12163 assert_wait((event_t
) msr
,THREAD_INTERRUPTIBLE
);
12165 vm_object_unlock(object
);
12166 thread_block(THREAD_CONTINUE_NULL
);
12167 vm_object_lock(object
);
12171 }/* queue_iterate */
12173 queue_enter(&object
->msr_q
, new_msr
, msync_req_t
, msr_q
);
12175 vm_object_paging_begin(object
);
12176 vm_object_unlock(object
);
12178 queue_enter(&req_q
, new_msr
, msync_req_t
, req_q
);
12180 (void) memory_object_synchronize(
12184 sync_flags
& ~VM_SYNC_CONTIGUOUS
);
12186 vm_object_lock(object
);
12187 vm_object_paging_end(object
);
12188 vm_object_unlock(object
);
12192 * wait for memory_object_sychronize_completed messages from pager(s)
12195 while (!queue_empty(&req_q
)) {
12196 msr
= (msync_req_t
)queue_first(&req_q
);
12198 while(msr
->flag
!= VM_MSYNC_DONE
) {
12199 assert_wait((event_t
) msr
, THREAD_INTERRUPTIBLE
);
12201 thread_block(THREAD_CONTINUE_NULL
);
12204 queue_remove(&req_q
, msr
, msync_req_t
, req_q
);
12206 vm_object_deallocate(msr
->object
);
12207 msync_req_free(msr
);
12208 }/* queue_iterate */
12210 /* for proper msync() behaviour */
12211 if (had_hole
== TRUE
&& (sync_flags
& VM_SYNC_CONTIGUOUS
))
12212 return(KERN_INVALID_ADDRESS
);
12214 return(KERN_SUCCESS
);
12218 * Routine: convert_port_entry_to_map
12220 * Convert from a port specifying an entry or a task
12221 * to a map. Doesn't consume the port ref; produces a map ref,
12222 * which may be null. Unlike convert_port_to_map, the
12223 * port may be task or a named entry backed.
12230 convert_port_entry_to_map(
12234 vm_named_entry_t named_entry
;
12235 uint32_t try_failed_count
= 0;
12237 if(IP_VALID(port
) && (ip_kotype(port
) == IKOT_NAMED_ENTRY
)) {
12240 if(ip_active(port
) && (ip_kotype(port
)
12241 == IKOT_NAMED_ENTRY
)) {
12243 (vm_named_entry_t
)port
->ip_kobject
;
12244 if (!(lck_mtx_try_lock(&(named_entry
)->Lock
))) {
12247 try_failed_count
++;
12248 mutex_pause(try_failed_count
);
12251 named_entry
->ref_count
++;
12252 lck_mtx_unlock(&(named_entry
)->Lock
);
12254 if ((named_entry
->is_sub_map
) &&
12255 (named_entry
->protection
12256 & VM_PROT_WRITE
)) {
12257 map
= named_entry
->backing
.map
;
12259 mach_destroy_memory_entry(port
);
12260 return VM_MAP_NULL
;
12262 vm_map_reference_swap(map
);
12263 mach_destroy_memory_entry(port
);
12267 return VM_MAP_NULL
;
12271 map
= convert_port_to_map(port
);
12277 * Routine: convert_port_entry_to_object
12279 * Convert from a port specifying a named entry to an
12280 * object. Doesn't consume the port ref; produces a map ref,
12281 * which may be null.
12288 convert_port_entry_to_object(
12291 vm_object_t object
;
12292 vm_named_entry_t named_entry
;
12293 uint32_t try_failed_count
= 0;
12295 if(IP_VALID(port
) && (ip_kotype(port
) == IKOT_NAMED_ENTRY
)) {
12298 if(ip_active(port
) && (ip_kotype(port
)
12299 == IKOT_NAMED_ENTRY
)) {
12301 (vm_named_entry_t
)port
->ip_kobject
;
12302 if (!(lck_mtx_try_lock(&(named_entry
)->Lock
))) {
12305 try_failed_count
++;
12306 mutex_pause(try_failed_count
);
12309 named_entry
->ref_count
++;
12310 lck_mtx_unlock(&(named_entry
)->Lock
);
12312 if ((!named_entry
->is_sub_map
) &&
12313 (!named_entry
->is_pager
) &&
12314 (named_entry
->protection
12315 & VM_PROT_WRITE
)) {
12316 object
= named_entry
->backing
.object
;
12318 mach_destroy_memory_entry(port
);
12319 return (vm_object_t
)NULL
;
12321 vm_object_reference(named_entry
->backing
.object
);
12322 mach_destroy_memory_entry(port
);
12326 return (vm_object_t
)NULL
;
12329 return (vm_object_t
)NULL
;
12336 * Export routines to other components for the things we access locally through
12343 return (current_map_fast());
12347 * vm_map_reference:
12349 * Most code internal to the osfmk will go through a
12350 * macro defining this. This is always here for the
12351 * use of other kernel components.
12353 #undef vm_map_reference
12356 register vm_map_t map
)
12358 if (map
== VM_MAP_NULL
)
12361 lck_mtx_lock(&map
->s_lock
);
12363 assert(map
->res_count
> 0);
12364 assert(map
->ref_count
>= map
->res_count
);
12368 lck_mtx_unlock(&map
->s_lock
);
12372 * vm_map_deallocate:
12374 * Removes a reference from the specified map,
12375 * destroying it if no references remain.
12376 * The map should not be locked.
12380 register vm_map_t map
)
12384 if (map
== VM_MAP_NULL
)
12387 lck_mtx_lock(&map
->s_lock
);
12388 ref
= --map
->ref_count
;
12390 vm_map_res_deallocate(map
);
12391 lck_mtx_unlock(&map
->s_lock
);
12394 assert(map
->ref_count
== 0);
12395 lck_mtx_unlock(&map
->s_lock
);
12399 * The map residence count isn't decremented here because
12400 * the vm_map_delete below will traverse the entire map,
12401 * deleting entries, and the residence counts on objects
12402 * and sharing maps will go away then.
12406 vm_map_destroy(map
, VM_MAP_NO_FLAGS
);
12411 vm_map_disable_NX(vm_map_t map
)
12415 if (map
->pmap
== NULL
)
12418 pmap_disable_NX(map
->pmap
);
12421 /* XXX Consider making these constants (VM_MAX_ADDRESS and MACH_VM_MAX_ADDRESS)
12422 * more descriptive.
12425 vm_map_set_32bit(vm_map_t map
)
12427 map
->max_offset
= (vm_map_offset_t
)VM_MAX_ADDRESS
;
12432 vm_map_set_64bit(vm_map_t map
)
12434 map
->max_offset
= (vm_map_offset_t
)MACH_VM_MAX_ADDRESS
;
12438 vm_compute_max_offset(unsigned is64
)
12440 return (is64
? (vm_map_offset_t
)MACH_VM_MAX_ADDRESS
: (vm_map_offset_t
)VM_MAX_ADDRESS
);
12447 return map
->max_offset
> ((vm_map_offset_t
)VM_MAX_ADDRESS
);
12451 vm_map_has_4GB_pagezero(
12456 * We should lock the VM map (for read) here but we can get away
12457 * with it for now because there can't really be any race condition:
12458 * the VM map's min_offset is changed only when the VM map is created
12459 * and when the zero page is established (when the binary gets loaded),
12460 * and this routine gets called only when the task terminates and the
12461 * VM map is being torn down, and when a new map is created via
12462 * load_machfile()/execve().
12464 return (map
->min_offset
>= 0x100000000ULL
);
12468 vm_map_set_4GB_pagezero(vm_map_t map
)
12471 pmap_set_4GB_pagezero(map
->pmap
);
12473 #pragma unused(map)
12479 vm_map_clear_4GB_pagezero(vm_map_t map
)
12482 pmap_clear_4GB_pagezero(map
->pmap
);
12484 #pragma unused(map)
12489 * Raise a VM map's minimum offset.
12490 * To strictly enforce "page zero" reservation.
12493 vm_map_raise_min_offset(
12495 vm_map_offset_t new_min_offset
)
12497 vm_map_entry_t first_entry
;
12499 new_min_offset
= vm_map_round_page(new_min_offset
);
12503 if (new_min_offset
< map
->min_offset
) {
12505 * Can't move min_offset backwards, as that would expose
12506 * a part of the address space that was previously, and for
12507 * possibly good reasons, inaccessible.
12509 vm_map_unlock(map
);
12510 return KERN_INVALID_ADDRESS
;
12513 first_entry
= vm_map_first_entry(map
);
12514 if (first_entry
!= vm_map_to_entry(map
) &&
12515 first_entry
->vme_start
< new_min_offset
) {
12517 * Some memory was already allocated below the new
12518 * minimun offset. It's too late to change it now...
12520 vm_map_unlock(map
);
12521 return KERN_NO_SPACE
;
12524 map
->min_offset
= new_min_offset
;
12526 vm_map_unlock(map
);
12528 return KERN_SUCCESS
;
12532 * Set the limit on the maximum amount of user wired memory allowed for this map.
12533 * This is basically a copy of the MEMLOCK rlimit value maintained by the BSD side of
12534 * the kernel. The limits are checked in the mach VM side, so we keep a copy so we
12535 * don't have to reach over to the BSD data structures.
12539 vm_map_set_user_wire_limit(vm_map_t map
,
12542 map
->user_wire_limit
= limit
;
12546 void vm_map_switch_protect(vm_map_t map
,
12550 map
->switch_protect
=val
;
12551 vm_map_unlock(map
);
12554 /* Add (generate) code signature for memory range */
12555 #if CONFIG_DYNAMIC_CODE_SIGNING
12556 kern_return_t
vm_map_sign(vm_map_t map
,
12557 vm_map_offset_t start
,
12558 vm_map_offset_t end
)
12560 vm_map_entry_t entry
;
12562 vm_object_t object
;
12565 * Vet all the input parameters and current type and state of the
12566 * underlaying object. Return with an error if anything is amiss.
12568 if (map
== VM_MAP_NULL
)
12569 return(KERN_INVALID_ARGUMENT
);
12571 vm_map_lock_read(map
);
12573 if (!vm_map_lookup_entry(map
, start
, &entry
) || entry
->is_sub_map
) {
12575 * Must pass a valid non-submap address.
12577 vm_map_unlock_read(map
);
12578 return(KERN_INVALID_ADDRESS
);
12581 if((entry
->vme_start
> start
) || (entry
->vme_end
< end
)) {
12583 * Map entry doesn't cover the requested range. Not handling
12584 * this situation currently.
12586 vm_map_unlock_read(map
);
12587 return(KERN_INVALID_ARGUMENT
);
12590 object
= entry
->object
.vm_object
;
12591 if (object
== VM_OBJECT_NULL
) {
12593 * Object must already be present or we can't sign.
12595 vm_map_unlock_read(map
);
12596 return KERN_INVALID_ARGUMENT
;
12599 vm_object_lock(object
);
12600 vm_map_unlock_read(map
);
12602 while(start
< end
) {
12605 m
= vm_page_lookup(object
, start
- entry
->vme_start
+ entry
->offset
);
12606 if (m
==VM_PAGE_NULL
) {
12607 /* shoud we try to fault a page here? we can probably
12608 * demand it exists and is locked for this request */
12609 vm_object_unlock(object
);
12610 return KERN_FAILURE
;
12612 /* deal with special page status */
12614 (m
->unusual
&& (m
->error
|| m
->restart
|| m
->private || m
->absent
))) {
12615 vm_object_unlock(object
);
12616 return KERN_FAILURE
;
12619 /* Page is OK... now "validate" it */
12620 /* This is the place where we'll call out to create a code
12621 * directory, later */
12622 m
->cs_validated
= TRUE
;
12624 /* The page is now "clean" for codesigning purposes. That means
12625 * we don't consider it as modified (wpmapped) anymore. But
12626 * we'll disconnect the page so we note any future modification
12628 m
->wpmapped
= FALSE
;
12629 refmod
= pmap_disconnect(m
->phys_page
);
12631 /* Pull the dirty status from the pmap, since we cleared the
12633 if ((refmod
& VM_MEM_MODIFIED
) && !m
->dirty
) {
12637 /* On to the next page */
12638 start
+= PAGE_SIZE
;
12640 vm_object_unlock(object
);
12642 return KERN_SUCCESS
;