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_kern.h>
93 #include <ipc/ipc_port.h>
94 #include <kern/sched_prim.h>
95 #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>
105 #include <ppc/mappings.h>
108 #include <vm/vm_protos.h>
109 #include <vm/vm_shared_region.h>
111 /* Internal prototypes
114 static void vm_map_simplify_range(
116 vm_map_offset_t start
,
117 vm_map_offset_t end
); /* forward */
119 static boolean_t
vm_map_range_check(
121 vm_map_offset_t start
,
123 vm_map_entry_t
*entry
);
125 static vm_map_entry_t
_vm_map_entry_create(
126 struct vm_map_header
*map_header
);
128 static void _vm_map_entry_dispose(
129 struct vm_map_header
*map_header
,
130 vm_map_entry_t entry
);
132 static void vm_map_pmap_enter(
134 vm_map_offset_t addr
,
135 vm_map_offset_t end_addr
,
137 vm_object_offset_t offset
,
138 vm_prot_t protection
);
140 static void _vm_map_clip_end(
141 struct vm_map_header
*map_header
,
142 vm_map_entry_t entry
,
143 vm_map_offset_t end
);
145 static void _vm_map_clip_start(
146 struct vm_map_header
*map_header
,
147 vm_map_entry_t entry
,
148 vm_map_offset_t start
);
150 static void vm_map_entry_delete(
152 vm_map_entry_t entry
);
154 static kern_return_t
vm_map_delete(
156 vm_map_offset_t start
,
161 static kern_return_t
vm_map_copy_overwrite_unaligned(
163 vm_map_entry_t entry
,
165 vm_map_address_t start
);
167 static kern_return_t
vm_map_copy_overwrite_aligned(
169 vm_map_entry_t tmp_entry
,
171 vm_map_offset_t start
,
174 static kern_return_t
vm_map_copyin_kernel_buffer(
176 vm_map_address_t src_addr
,
178 boolean_t src_destroy
,
179 vm_map_copy_t
*copy_result
); /* OUT */
181 static kern_return_t
vm_map_copyout_kernel_buffer(
183 vm_map_address_t
*addr
, /* IN/OUT */
185 boolean_t overwrite
);
187 static void vm_map_fork_share(
189 vm_map_entry_t old_entry
,
192 static boolean_t
vm_map_fork_copy(
194 vm_map_entry_t
*old_entry_p
,
197 void vm_map_region_top_walk(
198 vm_map_entry_t entry
,
199 vm_region_top_info_t top
);
201 void vm_map_region_walk(
204 vm_map_entry_t entry
,
205 vm_object_offset_t offset
,
206 vm_object_size_t range
,
207 vm_region_extended_info_t extended
,
208 boolean_t look_for_pages
);
210 static kern_return_t
vm_map_wire_nested(
212 vm_map_offset_t start
,
214 vm_prot_t access_type
,
217 vm_map_offset_t pmap_addr
);
219 static kern_return_t
vm_map_unwire_nested(
221 vm_map_offset_t start
,
225 vm_map_offset_t pmap_addr
);
227 static kern_return_t
vm_map_overwrite_submap_recurse(
229 vm_map_offset_t dst_addr
,
230 vm_map_size_t dst_size
);
232 static kern_return_t
vm_map_copy_overwrite_nested(
234 vm_map_offset_t dst_addr
,
236 boolean_t interruptible
,
239 static kern_return_t
vm_map_remap_extract(
241 vm_map_offset_t addr
,
244 struct vm_map_header
*map_header
,
245 vm_prot_t
*cur_protection
,
246 vm_prot_t
*max_protection
,
247 vm_inherit_t inheritance
,
250 static kern_return_t
vm_map_remap_range_allocate(
252 vm_map_address_t
*address
,
254 vm_map_offset_t mask
,
256 vm_map_entry_t
*map_entry
);
258 static void vm_map_region_look_for_page(
262 vm_object_offset_t offset
,
265 vm_region_extended_info_t extended
);
267 static int vm_map_region_count_obj_refs(
268 vm_map_entry_t entry
,
272 * Macros to copy a vm_map_entry. We must be careful to correctly
273 * manage the wired page count. vm_map_entry_copy() creates a new
274 * map entry to the same memory - the wired count in the new entry
275 * must be set to zero. vm_map_entry_copy_full() creates a new
276 * entry that is identical to the old entry. This preserves the
277 * wire count; it's used for map splitting and zone changing in
280 #define vm_map_entry_copy(NEW,OLD) \
283 (NEW)->is_shared = FALSE; \
284 (NEW)->needs_wakeup = FALSE; \
285 (NEW)->in_transition = FALSE; \
286 (NEW)->wired_count = 0; \
287 (NEW)->user_wired_count = 0; \
290 #define vm_map_entry_copy_full(NEW,OLD) (*(NEW) = *(OLD))
293 * Decide if we want to allow processes to execute from their data or stack areas.
294 * override_nx() returns true if we do. Data/stack execution can be enabled independently
295 * for 32 and 64 bit processes. Set the VM_ABI_32 or VM_ABI_64 flags in allow_data_exec
296 * or allow_stack_exec to enable data execution for that type of data area for that particular
297 * ABI (or both by or'ing the flags together). These are initialized in the architecture
298 * specific pmap files since the default behavior varies according to architecture. The
299 * main reason it varies is because of the need to provide binary compatibility with old
300 * applications that were written before these restrictions came into being. In the old
301 * days, an app could execute anything it could read, but this has slowly been tightened
302 * up over time. The default behavior is:
304 * 32-bit PPC apps may execute from both stack and data areas
305 * 32-bit Intel apps may exeucte from data areas but not stack
306 * 64-bit PPC/Intel apps may not execute from either data or stack
308 * An application on any architecture may override these defaults by explicitly
309 * adding PROT_EXEC permission to the page in question with the mprotect(2)
310 * system call. This code here just determines what happens when an app tries to
311 * execute from a page that lacks execute permission.
313 * Note that allow_data_exec or allow_stack_exec may also be modified by sysctl to change the
314 * default behavior for both 32 and 64 bit apps on a system-wide basis.
317 extern int allow_data_exec
, allow_stack_exec
;
320 override_nx(vm_map_t map
, uint32_t user_tag
) /* map unused on arm */
325 * Determine if the app is running in 32 or 64 bit mode.
328 if (vm_map_is_64bit(map
))
329 current_abi
= VM_ABI_64
;
331 current_abi
= VM_ABI_32
;
334 * Determine if we should allow the execution based on whether it's a
335 * stack or data area and the current architecture.
338 if (user_tag
== VM_MEMORY_STACK
)
339 return allow_stack_exec
& current_abi
;
341 return allow_data_exec
& current_abi
;
346 * Virtual memory maps provide for the mapping, protection,
347 * and sharing of virtual memory objects. In addition,
348 * this module provides for an efficient virtual copy of
349 * memory from one map to another.
351 * Synchronization is required prior to most operations.
353 * Maps consist of an ordered doubly-linked list of simple
354 * entries; a single hint is used to speed up lookups.
356 * Sharing maps have been deleted from this version of Mach.
357 * All shared objects are now mapped directly into the respective
358 * maps. This requires a change in the copy on write strategy;
359 * the asymmetric (delayed) strategy is used for shared temporary
360 * objects instead of the symmetric (shadow) strategy. All maps
361 * are now "top level" maps (either task map, kernel map or submap
362 * of the kernel map).
364 * Since portions of maps are specified by start/end addreses,
365 * which may not align with existing map entries, all
366 * routines merely "clip" entries to these start/end values.
367 * [That is, an entry is split into two, bordering at a
368 * start or end value.] Note that these clippings may not
369 * always be necessary (as the two resulting entries are then
370 * not changed); however, the clipping is done for convenience.
371 * No attempt is currently made to "glue back together" two
374 * The symmetric (shadow) copy strategy implements virtual copy
375 * by copying VM object references from one map to
376 * another, and then marking both regions as copy-on-write.
377 * It is important to note that only one writeable reference
378 * to a VM object region exists in any map when this strategy
379 * is used -- this means that shadow object creation can be
380 * delayed until a write operation occurs. The symmetric (delayed)
381 * strategy allows multiple maps to have writeable references to
382 * the same region of a vm object, and hence cannot delay creating
383 * its copy objects. See vm_object_copy_quickly() in vm_object.c.
384 * Copying of permanent objects is completely different; see
385 * vm_object_copy_strategically() in vm_object.c.
388 static zone_t vm_map_zone
; /* zone for vm_map structures */
389 static zone_t vm_map_entry_zone
; /* zone for vm_map_entry structures */
390 static zone_t vm_map_kentry_zone
; /* zone for kernel entry structures */
391 static zone_t vm_map_copy_zone
; /* zone for vm_map_copy structures */
395 * Placeholder object for submap operations. This object is dropped
396 * into the range by a call to vm_map_find, and removed when
397 * vm_map_submap creates the submap.
400 vm_object_t vm_submap_object
;
405 * Initialize the vm_map module. Must be called before
406 * any other vm_map routines.
408 * Map and entry structures are allocated from zones -- we must
409 * initialize those zones.
411 * There are three zones of interest:
413 * vm_map_zone: used to allocate maps.
414 * vm_map_entry_zone: used to allocate map entries.
415 * vm_map_kentry_zone: used to allocate map entries for the kernel.
417 * The kernel allocates map entries from a special zone that is initially
418 * "crammed" with memory. It would be difficult (perhaps impossible) for
419 * the kernel to allocate more memory to a entry zone when it became
420 * empty since the very act of allocating memory implies the creation
424 static void *map_data
;
425 static vm_map_size_t map_data_size
;
426 static void *kentry_data
;
427 static vm_map_size_t kentry_data_size
;
428 static int kentry_count
= 2048; /* to init kentry_data_size */
430 #define NO_COALESCE_LIMIT (1024 * 128)
433 /* Skip acquiring locks if we're in the midst of a kernel core dump */
434 extern unsigned int not_in_kdp
;
438 vm_map_apple_protected(
440 vm_map_offset_t start
,
443 boolean_t map_locked
;
445 vm_map_entry_t map_entry
;
446 memory_object_t protected_mem_obj
;
447 vm_object_t protected_object
;
448 vm_map_offset_t map_addr
;
450 vm_map_lock_read(map
);
453 /* lookup the protected VM object */
454 if (!vm_map_lookup_entry(map
,
457 map_entry
->vme_end
!= end
||
458 map_entry
->is_sub_map
) {
459 /* that memory is not properly mapped */
460 kr
= KERN_INVALID_ARGUMENT
;
463 protected_object
= map_entry
->object
.vm_object
;
464 if (protected_object
== VM_OBJECT_NULL
) {
465 /* there should be a VM object here at this point */
466 kr
= KERN_INVALID_ARGUMENT
;
471 * Lookup (and create if necessary) the protected memory object
472 * matching that VM object.
473 * If successful, this also grabs a reference on the memory object,
474 * to guarantee that it doesn't go away before we get a chance to map
478 protected_mem_obj
= apple_protect_pager_setup(protected_object
);
479 if (protected_mem_obj
== NULL
) {
484 vm_map_unlock_read(map
);
487 /* map this memory object in place of the current one */
489 kr
= vm_map_enter_mem_object(map
,
492 (mach_vm_offset_t
) 0,
493 VM_FLAGS_FIXED
| VM_FLAGS_OVERWRITE
,
494 (ipc_port_t
) protected_mem_obj
,
496 (start
- map_entry
->vme_start
)),
498 map_entry
->protection
,
499 map_entry
->max_protection
,
500 map_entry
->inheritance
);
501 assert(map_addr
== start
);
502 if (kr
== KERN_SUCCESS
) {
503 /* let the pager know that this mem_obj is mapped */
504 apple_protect_pager_map(protected_mem_obj
);
507 * Release the reference obtained by apple_protect_pager_setup().
508 * The mapping (if it succeeded) is now holding a reference on the
511 memory_object_deallocate(protected_mem_obj
);
515 vm_map_unlock_read(map
);
519 #endif /* __i386__ */
526 vm_map_zone
= zinit((vm_map_size_t
) sizeof(struct _vm_map
), 40*1024,
529 vm_map_entry_zone
= zinit((vm_map_size_t
) sizeof(struct vm_map_entry
),
530 1024*1024, PAGE_SIZE
*5,
531 "non-kernel map entries");
533 vm_map_kentry_zone
= zinit((vm_map_size_t
) sizeof(struct vm_map_entry
),
534 kentry_data_size
, kentry_data_size
,
535 "kernel map entries");
537 vm_map_copy_zone
= zinit((vm_map_size_t
) sizeof(struct vm_map_copy
),
538 16*1024, PAGE_SIZE
, "map copies");
541 * Cram the map and kentry zones with initial data.
542 * Set kentry_zone non-collectible to aid zone_gc().
544 zone_change(vm_map_zone
, Z_COLLECT
, FALSE
);
545 zone_change(vm_map_kentry_zone
, Z_COLLECT
, FALSE
);
546 zone_change(vm_map_kentry_zone
, Z_EXPAND
, FALSE
);
547 zcram(vm_map_zone
, map_data
, map_data_size
);
548 zcram(vm_map_kentry_zone
, kentry_data
, kentry_data_size
);
555 map_data_size
= vm_map_round_page(10 * sizeof(struct _vm_map
));
556 map_data
= pmap_steal_memory(map_data_size
);
560 * Limiting worst case: vm_map_kentry_zone needs to map each "available"
561 * physical page (i.e. that beyond the kernel image and page tables)
562 * individually; we guess at most one entry per eight pages in the
563 * real world. This works out to roughly .1 of 1% of physical memory,
564 * or roughly 1900 entries (64K) for a 64M machine with 4K pages.
567 kentry_count
= pmap_free_pages() / 8;
571 vm_map_round_page(kentry_count
* sizeof(struct vm_map_entry
));
572 kentry_data
= pmap_steal_memory(kentry_data_size
);
578 * Creates and returns a new empty VM map with
579 * the given physical map structure, and having
580 * the given lower and upper address bounds.
589 static int color_seed
= 0;
590 register vm_map_t result
;
592 result
= (vm_map_t
) zalloc(vm_map_zone
);
593 if (result
== VM_MAP_NULL
)
594 panic("vm_map_create");
596 vm_map_first_entry(result
) = vm_map_to_entry(result
);
597 vm_map_last_entry(result
) = vm_map_to_entry(result
);
598 result
->hdr
.nentries
= 0;
599 result
->hdr
.entries_pageable
= pageable
;
602 result
->user_wire_limit
= MACH_VM_MAX_ADDRESS
; /* default limit is unlimited */
603 result
->user_wire_size
= 0;
604 result
->ref_count
= 1;
606 result
->res_count
= 1;
607 result
->sw_state
= MAP_SW_IN
;
608 #endif /* TASK_SWAPPER */
610 result
->min_offset
= min
;
611 result
->max_offset
= max
;
612 result
->wiring_required
= FALSE
;
613 result
->no_zero_fill
= FALSE
;
614 result
->mapped
= FALSE
;
615 result
->wait_for_space
= FALSE
;
616 result
->first_free
= vm_map_to_entry(result
);
617 result
->hint
= vm_map_to_entry(result
);
618 result
->color_rr
= (color_seed
++) & vm_color_mask
;
619 vm_map_lock_init(result
);
620 mutex_init(&result
->s_lock
, 0);
626 * vm_map_entry_create: [ internal use only ]
628 * Allocates a VM map entry for insertion in the
629 * given map (or map copy). No fields are filled.
631 #define vm_map_entry_create(map) \
632 _vm_map_entry_create(&(map)->hdr)
634 #define vm_map_copy_entry_create(copy) \
635 _vm_map_entry_create(&(copy)->cpy_hdr)
637 static vm_map_entry_t
638 _vm_map_entry_create(
639 register struct vm_map_header
*map_header
)
641 register zone_t zone
;
642 register vm_map_entry_t entry
;
644 if (map_header
->entries_pageable
)
645 zone
= vm_map_entry_zone
;
647 zone
= vm_map_kentry_zone
;
649 entry
= (vm_map_entry_t
) zalloc(zone
);
650 if (entry
== VM_MAP_ENTRY_NULL
)
651 panic("vm_map_entry_create");
657 * vm_map_entry_dispose: [ internal use only ]
659 * Inverse of vm_map_entry_create.
661 * write map lock held so no need to
662 * do anything special to insure correctness
665 #define vm_map_entry_dispose(map, entry) \
667 if((entry) == (map)->first_free) \
668 (map)->first_free = vm_map_to_entry(map); \
669 if((entry) == (map)->hint) \
670 (map)->hint = vm_map_to_entry(map); \
671 _vm_map_entry_dispose(&(map)->hdr, (entry)); \
674 #define vm_map_copy_entry_dispose(map, entry) \
675 _vm_map_entry_dispose(&(copy)->cpy_hdr, (entry))
678 _vm_map_entry_dispose(
679 register struct vm_map_header
*map_header
,
680 register vm_map_entry_t entry
)
682 register zone_t zone
;
684 if (map_header
->entries_pageable
)
685 zone
= vm_map_entry_zone
;
687 zone
= vm_map_kentry_zone
;
693 static boolean_t
first_free_is_valid(vm_map_t map
); /* forward */
694 static boolean_t first_free_check
= FALSE
;
699 vm_map_entry_t entry
, next
;
701 if (!first_free_check
)
704 entry
= vm_map_to_entry(map
);
705 next
= entry
->vme_next
;
706 while (vm_map_trunc_page(next
->vme_start
) == vm_map_trunc_page(entry
->vme_end
) ||
707 (vm_map_trunc_page(next
->vme_start
) == vm_map_trunc_page(entry
->vme_start
) &&
708 next
!= vm_map_to_entry(map
))) {
710 next
= entry
->vme_next
;
711 if (entry
== vm_map_to_entry(map
))
714 if (map
->first_free
!= entry
) {
715 printf("Bad first_free for map %p: %p should be %p\n",
716 map
, map
->first_free
, entry
);
721 #endif /* MACH_ASSERT */
726 * Updates the map->first_free pointer to the
727 * entry immediately before the first hole in the map.
728 * The map should be locked.
730 #define UPDATE_FIRST_FREE(map, new_first_free) \
733 vm_map_entry_t UFF_first_free; \
734 vm_map_entry_t UFF_next_entry; \
736 UFF_first_free = (new_first_free); \
737 UFF_next_entry = UFF_first_free->vme_next; \
738 while (vm_map_trunc_page(UFF_next_entry->vme_start) == \
739 vm_map_trunc_page(UFF_first_free->vme_end) || \
740 (vm_map_trunc_page(UFF_next_entry->vme_start) == \
741 vm_map_trunc_page(UFF_first_free->vme_start) && \
742 UFF_next_entry != vm_map_to_entry(UFF_map))) { \
743 UFF_first_free = UFF_next_entry; \
744 UFF_next_entry = UFF_first_free->vme_next; \
745 if (UFF_first_free == vm_map_to_entry(UFF_map)) \
748 UFF_map->first_free = UFF_first_free; \
749 assert(first_free_is_valid(UFF_map)); \
753 * vm_map_entry_{un,}link:
755 * Insert/remove entries from maps (or map copies).
757 #define vm_map_entry_link(map, after_where, entry) \
760 vm_map_entry_t VMEL_entry; \
762 VMEL_entry = (entry); \
763 _vm_map_entry_link(&VMEL_map->hdr, after_where, VMEL_entry); \
764 UPDATE_FIRST_FREE(VMEL_map, VMEL_map->first_free); \
768 #define vm_map_copy_entry_link(copy, after_where, entry) \
769 _vm_map_entry_link(&(copy)->cpy_hdr, after_where, (entry))
771 #define _vm_map_entry_link(hdr, after_where, entry) \
774 (entry)->vme_prev = (after_where); \
775 (entry)->vme_next = (after_where)->vme_next; \
776 (entry)->vme_prev->vme_next = (entry)->vme_next->vme_prev = (entry); \
779 #define vm_map_entry_unlink(map, entry) \
782 vm_map_entry_t VMEU_entry; \
783 vm_map_entry_t VMEU_first_free; \
785 VMEU_entry = (entry); \
786 if (VMEU_entry->vme_start <= VMEU_map->first_free->vme_start) \
787 VMEU_first_free = VMEU_entry->vme_prev; \
789 VMEU_first_free = VMEU_map->first_free; \
790 _vm_map_entry_unlink(&VMEU_map->hdr, VMEU_entry); \
791 UPDATE_FIRST_FREE(VMEU_map, VMEU_first_free); \
794 #define vm_map_copy_entry_unlink(copy, entry) \
795 _vm_map_entry_unlink(&(copy)->cpy_hdr, (entry))
797 #define _vm_map_entry_unlink(hdr, entry) \
800 (entry)->vme_next->vme_prev = (entry)->vme_prev; \
801 (entry)->vme_prev->vme_next = (entry)->vme_next; \
804 #if MACH_ASSERT && TASK_SWAPPER
806 * vm_map_res_reference:
808 * Adds another valid residence count to the given map.
810 * Map is locked so this function can be called from
814 void vm_map_res_reference(register vm_map_t map
)
816 /* assert map is locked */
817 assert(map
->res_count
>= 0);
818 assert(map
->ref_count
>= map
->res_count
);
819 if (map
->res_count
== 0) {
820 mutex_unlock(&map
->s_lock
);
823 mutex_lock(&map
->s_lock
);
831 * vm_map_reference_swap:
833 * Adds valid reference and residence counts to the given map.
835 * The map may not be in memory (i.e. zero residence count).
838 void vm_map_reference_swap(register vm_map_t map
)
840 assert(map
!= VM_MAP_NULL
);
841 mutex_lock(&map
->s_lock
);
842 assert(map
->res_count
>= 0);
843 assert(map
->ref_count
>= map
->res_count
);
845 vm_map_res_reference(map
);
846 mutex_unlock(&map
->s_lock
);
850 * vm_map_res_deallocate:
852 * Decrement residence count on a map; possibly causing swapout.
854 * The map must be in memory (i.e. non-zero residence count).
856 * The map is locked, so this function is callable from vm_map_deallocate.
859 void vm_map_res_deallocate(register vm_map_t map
)
861 assert(map
->res_count
> 0);
862 if (--map
->res_count
== 0) {
863 mutex_unlock(&map
->s_lock
);
867 mutex_lock(&map
->s_lock
);
869 assert(map
->ref_count
>= map
->res_count
);
871 #endif /* MACH_ASSERT && TASK_SWAPPER */
876 * Actually destroy a map.
885 /* clean up regular map entries */
886 (void) vm_map_delete(map
, map
->min_offset
, map
->max_offset
,
888 /* clean up leftover special mappings (commpage, etc...) */
891 * PPC51: ppc64 is limited to 51-bit addresses.
892 * Memory beyond this 51-bit limit is mapped specially at the
893 * pmap level, so do not interfere.
894 * On PPC64, the commpage is mapped beyond the addressable range
895 * via a special pmap hack, so ask pmap to clean it explicitly...
898 pmap_unmap_sharedpage(map
->pmap
);
900 /* ... and do not let regular pmap cleanup apply here */
901 flags
|= VM_MAP_REMOVE_NO_PMAP_CLEANUP
;
903 (void) vm_map_delete(map
, 0x0, 0xFFFFFFFFFFFFF000ULL
,
907 assert(map
->hdr
.nentries
== 0);
910 pmap_destroy(map
->pmap
);
912 zfree(vm_map_zone
, map
);
917 * vm_map_swapin/vm_map_swapout
919 * Swap a map in and out, either referencing or releasing its resources.
920 * These functions are internal use only; however, they must be exported
921 * because they may be called from macros, which are exported.
923 * In the case of swapout, there could be races on the residence count,
924 * so if the residence count is up, we return, assuming that a
925 * vm_map_deallocate() call in the near future will bring us back.
928 * -- We use the map write lock for synchronization among races.
929 * -- The map write lock, and not the simple s_lock, protects the
930 * swap state of the map.
931 * -- If a map entry is a share map, then we hold both locks, in
932 * hierarchical order.
934 * Synchronization Notes:
935 * 1) If a vm_map_swapin() call happens while swapout in progress, it
936 * will block on the map lock and proceed when swapout is through.
937 * 2) A vm_map_reference() call at this time is illegal, and will
938 * cause a panic. vm_map_reference() is only allowed on resident
939 * maps, since it refuses to block.
940 * 3) A vm_map_swapin() call during a swapin will block, and
941 * proceeed when the first swapin is done, turning into a nop.
942 * This is the reason the res_count is not incremented until
943 * after the swapin is complete.
944 * 4) There is a timing hole after the checks of the res_count, before
945 * the map lock is taken, during which a swapin may get the lock
946 * before a swapout about to happen. If this happens, the swapin
947 * will detect the state and increment the reference count, causing
948 * the swapout to be a nop, thereby delaying it until a later
949 * vm_map_deallocate. If the swapout gets the lock first, then
950 * the swapin will simply block until the swapout is done, and
953 * Because vm_map_swapin() is potentially an expensive operation, it
954 * should be used with caution.
957 * 1) A map with a residence count of zero is either swapped, or
959 * 2) A map with a non-zero residence count is either resident,
960 * or being swapped in.
963 int vm_map_swap_enable
= 1;
965 void vm_map_swapin (vm_map_t map
)
967 register vm_map_entry_t entry
;
969 if (!vm_map_swap_enable
) /* debug */
974 * First deal with various races.
976 if (map
->sw_state
== MAP_SW_IN
)
978 * we raced with swapout and won. Returning will incr.
979 * the res_count, turning the swapout into a nop.
984 * The residence count must be zero. If we raced with another
985 * swapin, the state would have been IN; if we raced with a
986 * swapout (after another competing swapin), we must have lost
987 * the race to get here (see above comment), in which case
988 * res_count is still 0.
990 assert(map
->res_count
== 0);
993 * There are no intermediate states of a map going out or
994 * coming in, since the map is locked during the transition.
996 assert(map
->sw_state
== MAP_SW_OUT
);
999 * We now operate upon each map entry. If the entry is a sub-
1000 * or share-map, we call vm_map_res_reference upon it.
1001 * If the entry is an object, we call vm_object_res_reference
1002 * (this may iterate through the shadow chain).
1003 * Note that we hold the map locked the entire time,
1004 * even if we get back here via a recursive call in
1005 * vm_map_res_reference.
1007 entry
= vm_map_first_entry(map
);
1009 while (entry
!= vm_map_to_entry(map
)) {
1010 if (entry
->object
.vm_object
!= VM_OBJECT_NULL
) {
1011 if (entry
->is_sub_map
) {
1012 vm_map_t lmap
= entry
->object
.sub_map
;
1013 mutex_lock(&lmap
->s_lock
);
1014 vm_map_res_reference(lmap
);
1015 mutex_unlock(&lmap
->s_lock
);
1017 vm_object_t object
= entry
->object
.vm_object
;
1018 vm_object_lock(object
);
1020 * This call may iterate through the
1023 vm_object_res_reference(object
);
1024 vm_object_unlock(object
);
1027 entry
= entry
->vme_next
;
1029 assert(map
->sw_state
== MAP_SW_OUT
);
1030 map
->sw_state
= MAP_SW_IN
;
1033 void vm_map_swapout(vm_map_t map
)
1035 register vm_map_entry_t entry
;
1039 * First deal with various races.
1040 * If we raced with a swapin and lost, the residence count
1041 * will have been incremented to 1, and we simply return.
1043 mutex_lock(&map
->s_lock
);
1044 if (map
->res_count
!= 0) {
1045 mutex_unlock(&map
->s_lock
);
1048 mutex_unlock(&map
->s_lock
);
1051 * There are no intermediate states of a map going out or
1052 * coming in, since the map is locked during the transition.
1054 assert(map
->sw_state
== MAP_SW_IN
);
1056 if (!vm_map_swap_enable
)
1060 * We now operate upon each map entry. If the entry is a sub-
1061 * or share-map, we call vm_map_res_deallocate upon it.
1062 * If the entry is an object, we call vm_object_res_deallocate
1063 * (this may iterate through the shadow chain).
1064 * Note that we hold the map locked the entire time,
1065 * even if we get back here via a recursive call in
1066 * vm_map_res_deallocate.
1068 entry
= vm_map_first_entry(map
);
1070 while (entry
!= vm_map_to_entry(map
)) {
1071 if (entry
->object
.vm_object
!= VM_OBJECT_NULL
) {
1072 if (entry
->is_sub_map
) {
1073 vm_map_t lmap
= entry
->object
.sub_map
;
1074 mutex_lock(&lmap
->s_lock
);
1075 vm_map_res_deallocate(lmap
);
1076 mutex_unlock(&lmap
->s_lock
);
1078 vm_object_t object
= entry
->object
.vm_object
;
1079 vm_object_lock(object
);
1081 * This call may take a long time,
1082 * since it could actively push
1083 * out pages (if we implement it
1086 vm_object_res_deallocate(object
);
1087 vm_object_unlock(object
);
1090 entry
= entry
->vme_next
;
1092 assert(map
->sw_state
== MAP_SW_IN
);
1093 map
->sw_state
= MAP_SW_OUT
;
1096 #endif /* TASK_SWAPPER */
1100 * SAVE_HINT_MAP_READ:
1102 * Saves the specified entry as the hint for
1103 * future lookups. only a read lock is held on map,
1104 * so make sure the store is atomic... OSCompareAndSwap
1105 * guarantees this... also, we don't care if we collide
1106 * and someone else wins and stores their 'hint'
1108 #define SAVE_HINT_MAP_READ(map,value) \
1110 OSCompareAndSwap((UInt32)((map)->hint), (UInt32)value, (UInt32 *)(&(map)->hint)); \
1115 * SAVE_HINT_MAP_WRITE:
1117 * Saves the specified entry as the hint for
1118 * future lookups. write lock held on map,
1119 * so no one else can be writing or looking
1120 * until the lock is dropped, so it's safe
1121 * to just do an assignment
1123 #define SAVE_HINT_MAP_WRITE(map,value) \
1125 (map)->hint = (value); \
1129 * vm_map_lookup_entry: [ internal use only ]
1131 * Finds the map entry containing (or
1132 * immediately preceding) the specified address
1133 * in the given map; the entry is returned
1134 * in the "entry" parameter. The boolean
1135 * result indicates whether the address is
1136 * actually contained in the map.
1139 vm_map_lookup_entry(
1140 register vm_map_t map
,
1141 register vm_map_offset_t address
,
1142 vm_map_entry_t
*entry
) /* OUT */
1144 register vm_map_entry_t cur
;
1145 register vm_map_entry_t last
;
1148 * Start looking either from the head of the
1149 * list, or from the hint.
1153 if (cur
== vm_map_to_entry(map
))
1154 cur
= cur
->vme_next
;
1156 if (address
>= cur
->vme_start
) {
1158 * Go from hint to end of list.
1160 * But first, make a quick check to see if
1161 * we are already looking at the entry we
1162 * want (which is usually the case).
1163 * Note also that we don't need to save the hint
1164 * here... it is the same hint (unless we are
1165 * at the header, in which case the hint didn't
1166 * buy us anything anyway).
1168 last
= vm_map_to_entry(map
);
1169 if ((cur
!= last
) && (cur
->vme_end
> address
)) {
1176 * Go from start to hint, *inclusively*
1178 last
= cur
->vme_next
;
1179 cur
= vm_map_first_entry(map
);
1186 while (cur
!= last
) {
1187 if (cur
->vme_end
> address
) {
1188 if (address
>= cur
->vme_start
) {
1190 * Save this lookup for future
1195 SAVE_HINT_MAP_READ(map
, cur
);
1201 cur
= cur
->vme_next
;
1203 *entry
= cur
->vme_prev
;
1204 SAVE_HINT_MAP_READ(map
, *entry
);
1210 * Routine: vm_map_find_space
1212 * Allocate a range in the specified virtual address map,
1213 * returning the entry allocated for that range.
1214 * Used by kmem_alloc, etc.
1216 * The map must be NOT be locked. It will be returned locked
1217 * on KERN_SUCCESS, unlocked on failure.
1219 * If an entry is allocated, the object/offset fields
1220 * are initialized to zero.
1224 register vm_map_t map
,
1225 vm_map_offset_t
*address
, /* OUT */
1227 vm_map_offset_t mask
,
1229 vm_map_entry_t
*o_entry
) /* OUT */
1231 register vm_map_entry_t entry
, new_entry
;
1232 register vm_map_offset_t start
;
1233 register vm_map_offset_t end
;
1237 return KERN_INVALID_ARGUMENT
;
1240 if (flags
& VM_FLAGS_GUARD_AFTER
) {
1241 /* account for the back guard page in the size */
1242 size
+= PAGE_SIZE_64
;
1245 new_entry
= vm_map_entry_create(map
);
1248 * Look for the first possible address; if there's already
1249 * something at this address, we have to start after it.
1254 assert(first_free_is_valid(map
));
1255 if ((entry
= map
->first_free
) == vm_map_to_entry(map
))
1256 start
= map
->min_offset
;
1258 start
= entry
->vme_end
;
1261 * In any case, the "entry" always precedes
1262 * the proposed new region throughout the loop:
1266 register vm_map_entry_t next
;
1269 * Find the end of the proposed new region.
1270 * Be sure we didn't go beyond the end, or
1271 * wrap around the address.
1274 if (flags
& VM_FLAGS_GUARD_BEFORE
) {
1275 /* reserve space for the front guard page */
1276 start
+= PAGE_SIZE_64
;
1278 end
= ((start
+ mask
) & ~mask
);
1281 vm_map_entry_dispose(map
, new_entry
);
1283 return(KERN_NO_SPACE
);
1288 if ((end
> map
->max_offset
) || (end
< start
)) {
1289 vm_map_entry_dispose(map
, new_entry
);
1291 return(KERN_NO_SPACE
);
1295 * If there are no more entries, we must win.
1298 next
= entry
->vme_next
;
1299 if (next
== vm_map_to_entry(map
))
1303 * If there is another entry, it must be
1304 * after the end of the potential new region.
1307 if (next
->vme_start
>= end
)
1311 * Didn't fit -- move to the next entry.
1315 start
= entry
->vme_end
;
1320 * "start" and "end" should define the endpoints of the
1321 * available new range, and
1322 * "entry" should refer to the region before the new
1325 * the map should be locked.
1328 if (flags
& VM_FLAGS_GUARD_BEFORE
) {
1329 /* go back for the front guard page */
1330 start
-= PAGE_SIZE_64
;
1334 new_entry
->vme_start
= start
;
1335 new_entry
->vme_end
= end
;
1336 assert(page_aligned(new_entry
->vme_start
));
1337 assert(page_aligned(new_entry
->vme_end
));
1339 new_entry
->is_shared
= FALSE
;
1340 new_entry
->is_sub_map
= FALSE
;
1341 new_entry
->use_pmap
= FALSE
;
1342 new_entry
->object
.vm_object
= VM_OBJECT_NULL
;
1343 new_entry
->offset
= (vm_object_offset_t
) 0;
1345 new_entry
->needs_copy
= FALSE
;
1347 new_entry
->inheritance
= VM_INHERIT_DEFAULT
;
1348 new_entry
->protection
= VM_PROT_DEFAULT
;
1349 new_entry
->max_protection
= VM_PROT_ALL
;
1350 new_entry
->behavior
= VM_BEHAVIOR_DEFAULT
;
1351 new_entry
->wired_count
= 0;
1352 new_entry
->user_wired_count
= 0;
1354 new_entry
->in_transition
= FALSE
;
1355 new_entry
->needs_wakeup
= FALSE
;
1356 new_entry
->no_cache
= FALSE
;
1358 new_entry
->alias
= 0;
1360 VM_GET_FLAGS_ALIAS(flags
, new_entry
->alias
);
1363 * Insert the new entry into the list
1366 vm_map_entry_link(map
, entry
, new_entry
);
1371 * Update the lookup hint
1373 SAVE_HINT_MAP_WRITE(map
, new_entry
);
1375 *o_entry
= new_entry
;
1376 return(KERN_SUCCESS
);
1379 int vm_map_pmap_enter_print
= FALSE
;
1380 int vm_map_pmap_enter_enable
= FALSE
;
1383 * Routine: vm_map_pmap_enter [internal only]
1386 * Force pages from the specified object to be entered into
1387 * the pmap at the specified address if they are present.
1388 * As soon as a page not found in the object the scan ends.
1393 * In/out conditions:
1394 * The source map should not be locked on entry.
1399 register vm_map_offset_t addr
,
1400 register vm_map_offset_t end_addr
,
1401 register vm_object_t object
,
1402 vm_object_offset_t offset
,
1403 vm_prot_t protection
)
1411 while (addr
< end_addr
) {
1412 register vm_page_t m
;
1414 vm_object_lock(object
);
1416 m
= vm_page_lookup(object
, offset
);
1419 * The user should never see encrypted data, so do not
1420 * enter an encrypted page in the page table.
1422 if (m
== VM_PAGE_NULL
|| m
->busy
|| m
->encrypted
||
1424 (m
->unusual
&& ( m
->error
|| m
->restart
|| m
->absent
))) {
1425 vm_object_unlock(object
);
1429 if (vm_map_pmap_enter_print
) {
1430 printf("vm_map_pmap_enter:");
1431 printf("map: %p, addr: %llx, object: %p, offset: %llx\n",
1432 map
, (unsigned long long)addr
, object
, (unsigned long long)offset
);
1434 type_of_fault
= DBG_CACHE_HIT_FAULT
;
1435 kr
= vm_fault_enter(m
, map
->pmap
, addr
, protection
,
1436 m
->wire_count
!= 0, FALSE
, FALSE
,
1439 vm_object_unlock(object
);
1441 offset
+= PAGE_SIZE_64
;
1446 boolean_t
vm_map_pmap_is_empty(
1448 vm_map_offset_t start
,
1449 vm_map_offset_t end
);
1450 boolean_t
vm_map_pmap_is_empty(
1452 vm_map_offset_t start
,
1453 vm_map_offset_t end
)
1455 #ifdef MACHINE_PMAP_IS_EMPTY
1456 return pmap_is_empty(map
->pmap
, start
, end
);
1457 #else /* MACHINE_PMAP_IS_EMPTY */
1458 vm_map_offset_t offset
;
1461 if (map
->pmap
== NULL
) {
1465 for (offset
= start
;
1467 offset
+= PAGE_SIZE
) {
1468 phys_page
= pmap_find_phys(map
->pmap
, offset
);
1470 kprintf("vm_map_pmap_is_empty(%p,0x%llx,0x%llx): "
1471 "page %d at 0x%llx\n",
1472 map
, (long long)start
, (long long)end
,
1473 phys_page
, (long long)offset
);
1478 #endif /* MACHINE_PMAP_IS_EMPTY */
1482 * Routine: vm_map_enter
1485 * Allocate a range in the specified virtual address map.
1486 * The resulting range will refer to memory defined by
1487 * the given memory object and offset into that object.
1489 * Arguments are as defined in the vm_map call.
1491 int _map_enter_debug
= 0;
1492 static unsigned int vm_map_enter_restore_successes
= 0;
1493 static unsigned int vm_map_enter_restore_failures
= 0;
1497 vm_map_offset_t
*address
, /* IN/OUT */
1499 vm_map_offset_t mask
,
1502 vm_object_offset_t offset
,
1503 boolean_t needs_copy
,
1504 vm_prot_t cur_protection
,
1505 vm_prot_t max_protection
,
1506 vm_inherit_t inheritance
)
1508 vm_map_entry_t entry
, new_entry
;
1509 vm_map_offset_t start
, tmp_start
, tmp_offset
;
1510 vm_map_offset_t end
, tmp_end
;
1511 kern_return_t result
= KERN_SUCCESS
;
1512 vm_map_t zap_old_map
= VM_MAP_NULL
;
1513 vm_map_t zap_new_map
= VM_MAP_NULL
;
1514 boolean_t map_locked
= FALSE
;
1515 boolean_t pmap_empty
= TRUE
;
1516 boolean_t new_mapping_established
= FALSE
;
1517 boolean_t anywhere
= ((flags
& VM_FLAGS_ANYWHERE
) != 0);
1518 boolean_t purgable
= ((flags
& VM_FLAGS_PURGABLE
) != 0);
1519 boolean_t overwrite
= ((flags
& VM_FLAGS_OVERWRITE
) != 0);
1520 boolean_t no_cache
= ((flags
& VM_FLAGS_NO_CACHE
) != 0);
1521 boolean_t is_submap
= ((flags
& VM_FLAGS_SUBMAP
) != 0);
1523 vm_map_offset_t effective_min_offset
, effective_max_offset
;
1527 /* submaps can not be purgeable */
1528 return KERN_INVALID_ARGUMENT
;
1530 if (object
== VM_OBJECT_NULL
) {
1531 /* submaps can not be created lazily */
1532 return KERN_INVALID_ARGUMENT
;
1535 if (flags
& VM_FLAGS_ALREADY
) {
1537 * VM_FLAGS_ALREADY says that it's OK if the same mapping
1538 * is already present. For it to be meaningul, the requested
1539 * mapping has to be at a fixed address (!VM_FLAGS_ANYWHERE) and
1540 * we shouldn't try and remove what was mapped there first
1541 * (!VM_FLAGS_OVERWRITE).
1543 if ((flags
& VM_FLAGS_ANYWHERE
) ||
1544 (flags
& VM_FLAGS_OVERWRITE
)) {
1545 return KERN_INVALID_ARGUMENT
;
1549 effective_min_offset
= map
->min_offset
;
1550 if (flags
& VM_FLAGS_BEYOND_MAX
) {
1552 * Allow an insertion beyond the map's official top boundary.
1554 if (vm_map_is_64bit(map
))
1555 effective_max_offset
= 0xFFFFFFFFFFFFF000ULL
;
1557 effective_max_offset
= 0x00000000FFFFF000ULL
;
1559 effective_max_offset
= map
->max_offset
;
1563 (offset
& PAGE_MASK_64
) != 0) {
1565 return KERN_INVALID_ARGUMENT
;
1568 VM_GET_FLAGS_ALIAS(flags
, alias
);
1570 #define RETURN(value) { result = value; goto BailOut; }
1572 assert(page_aligned(*address
));
1573 assert(page_aligned(size
));
1576 * Only zero-fill objects are allowed to be purgable.
1577 * LP64todo - limit purgable objects to 32-bits for now
1581 (object
!= VM_OBJECT_NULL
&&
1582 (object
->size
!= size
||
1583 object
->purgable
== VM_PURGABLE_DENY
))
1584 || size
> VM_MAX_ADDRESS
)) /* LP64todo: remove when dp capable */
1585 return KERN_INVALID_ARGUMENT
;
1587 if (!anywhere
&& overwrite
) {
1589 * Create a temporary VM map to hold the old mappings in the
1590 * affected area while we create the new one.
1591 * This avoids releasing the VM map lock in
1592 * vm_map_entry_delete() and allows atomicity
1593 * when we want to replace some mappings with a new one.
1594 * It also allows us to restore the old VM mappings if the
1595 * new mapping fails.
1597 zap_old_map
= vm_map_create(PMAP_NULL
,
1612 * Calculate the first possible address.
1615 if (start
< effective_min_offset
)
1616 start
= effective_min_offset
;
1617 if (start
> effective_max_offset
)
1618 RETURN(KERN_NO_SPACE
);
1621 * Look for the first possible address;
1622 * if there's already something at this
1623 * address, we have to start after it.
1626 assert(first_free_is_valid(map
));
1627 if (start
== effective_min_offset
) {
1628 if ((entry
= map
->first_free
) != vm_map_to_entry(map
))
1629 start
= entry
->vme_end
;
1631 vm_map_entry_t tmp_entry
;
1632 if (vm_map_lookup_entry(map
, start
, &tmp_entry
))
1633 start
= tmp_entry
->vme_end
;
1638 * In any case, the "entry" always precedes
1639 * the proposed new region throughout the
1644 register vm_map_entry_t next
;
1647 * Find the end of the proposed new region.
1648 * Be sure we didn't go beyond the end, or
1649 * wrap around the address.
1652 end
= ((start
+ mask
) & ~mask
);
1654 RETURN(KERN_NO_SPACE
);
1658 if ((end
> effective_max_offset
) || (end
< start
)) {
1659 if (map
->wait_for_space
) {
1660 if (size
<= (effective_max_offset
-
1661 effective_min_offset
)) {
1662 assert_wait((event_t
)map
,
1666 thread_block(THREAD_CONTINUE_NULL
);
1670 RETURN(KERN_NO_SPACE
);
1674 * If there are no more entries, we must win.
1677 next
= entry
->vme_next
;
1678 if (next
== vm_map_to_entry(map
))
1682 * If there is another entry, it must be
1683 * after the end of the potential new region.
1686 if (next
->vme_start
>= end
)
1690 * Didn't fit -- move to the next entry.
1694 start
= entry
->vme_end
;
1700 * the address doesn't itself violate
1701 * the mask requirement.
1706 if ((start
& mask
) != 0)
1707 RETURN(KERN_NO_SPACE
);
1710 * ... the address is within bounds
1715 if ((start
< effective_min_offset
) ||
1716 (end
> effective_max_offset
) ||
1718 RETURN(KERN_INVALID_ADDRESS
);
1721 if (overwrite
&& zap_old_map
!= VM_MAP_NULL
) {
1723 * Fixed mapping and "overwrite" flag: attempt to
1724 * remove all existing mappings in the specified
1725 * address range, saving them in our "zap_old_map".
1727 (void) vm_map_delete(map
, start
, end
,
1728 VM_MAP_REMOVE_SAVE_ENTRIES
,
1733 * ... the starting address isn't allocated
1736 if (vm_map_lookup_entry(map
, start
, &entry
)) {
1737 if (! (flags
& VM_FLAGS_ALREADY
)) {
1738 RETURN(KERN_NO_SPACE
);
1741 * Check if what's already there is what we want.
1744 tmp_offset
= offset
;
1745 if (entry
->vme_start
< start
) {
1746 tmp_start
-= start
- entry
->vme_start
;
1747 tmp_offset
-= start
- entry
->vme_start
;
1750 for (; entry
->vme_start
< end
;
1751 entry
= entry
->vme_next
) {
1752 if (entry
== vm_map_to_entry(map
) ||
1753 entry
->vme_start
!= tmp_start
||
1754 entry
->is_sub_map
!= is_submap
||
1755 entry
->object
.vm_object
!= object
||
1756 entry
->offset
!= tmp_offset
||
1757 entry
->needs_copy
!= needs_copy
||
1758 entry
->protection
!= cur_protection
||
1759 entry
->max_protection
!= max_protection
||
1760 entry
->inheritance
!= inheritance
||
1761 entry
->alias
!= alias
) {
1762 /* not the same mapping ! */
1763 RETURN(KERN_NO_SPACE
);
1765 tmp_offset
+= entry
->vme_end
- entry
->vme_start
;
1766 tmp_start
+= entry
->vme_end
- entry
->vme_start
;
1767 if (entry
->vme_end
>= end
) {
1768 /* reached the end of our mapping */
1772 /* it all matches: let's use what's already there ! */
1773 RETURN(KERN_MEMORY_PRESENT
);
1777 * ... the next region doesn't overlap the
1781 if ((entry
->vme_next
!= vm_map_to_entry(map
)) &&
1782 (entry
->vme_next
->vme_start
< end
))
1783 RETURN(KERN_NO_SPACE
);
1788 * "start" and "end" should define the endpoints of the
1789 * available new range, and
1790 * "entry" should refer to the region before the new
1793 * the map should be locked.
1797 * See whether we can avoid creating a new entry (and object) by
1798 * extending one of our neighbors. [So far, we only attempt to
1799 * extend from below.] Note that we can never extend/join
1800 * purgable objects because they need to remain distinct
1801 * entities in order to implement their "volatile object"
1806 if (object
== VM_OBJECT_NULL
) {
1807 object
= vm_object_allocate(size
);
1808 object
->copy_strategy
= MEMORY_OBJECT_COPY_NONE
;
1809 object
->purgable
= VM_PURGABLE_NONVOLATILE
;
1810 offset
= (vm_object_offset_t
)0;
1812 } else if ((is_submap
== FALSE
) &&
1813 (object
== VM_OBJECT_NULL
) &&
1814 (entry
!= vm_map_to_entry(map
)) &&
1815 (entry
->vme_end
== start
) &&
1816 (!entry
->is_shared
) &&
1817 (!entry
->is_sub_map
) &&
1818 (entry
->alias
== alias
) &&
1819 (entry
->inheritance
== inheritance
) &&
1820 (entry
->protection
== cur_protection
) &&
1821 (entry
->max_protection
== max_protection
) &&
1822 (entry
->behavior
== VM_BEHAVIOR_DEFAULT
) &&
1823 (entry
->in_transition
== 0) &&
1824 (entry
->no_cache
== no_cache
) &&
1825 ((alias
== VM_MEMORY_REALLOC
) ||
1826 ((entry
->vme_end
- entry
->vme_start
) + size
< NO_COALESCE_LIMIT
)) &&
1827 (entry
->wired_count
== 0)) { /* implies user_wired_count == 0 */
1828 if (vm_object_coalesce(entry
->object
.vm_object
,
1831 (vm_object_offset_t
) 0,
1832 (vm_map_size_t
)(entry
->vme_end
- entry
->vme_start
),
1833 (vm_map_size_t
)(end
- entry
->vme_end
))) {
1836 * Coalesced the two objects - can extend
1837 * the previous map entry to include the
1840 map
->size
+= (end
- entry
->vme_end
);
1841 entry
->vme_end
= end
;
1842 UPDATE_FIRST_FREE(map
, map
->first_free
);
1843 RETURN(KERN_SUCCESS
);
1848 * Create a new entry
1849 * LP64todo - for now, we can only allocate 4GB internal objects
1850 * because the default pager can't page bigger ones. Remove this
1854 * The reserved "page zero" in each process's address space can
1855 * be arbitrarily large. Splitting it into separate 4GB objects and
1856 * therefore different VM map entries serves no purpose and just
1857 * slows down operations on the VM map, so let's not split the
1858 * allocation into 4GB chunks if the max protection is NONE. That
1859 * memory should never be accessible, so it will never get to the
1863 if (object
== VM_OBJECT_NULL
&&
1864 size
> (vm_map_size_t
)VM_MAX_ADDRESS
&&
1865 max_protection
!= VM_PROT_NONE
)
1866 tmp_end
= tmp_start
+ (vm_map_size_t
)VM_MAX_ADDRESS
;
1870 new_entry
= vm_map_entry_insert(map
, entry
, tmp_start
, tmp_end
,
1871 object
, offset
, needs_copy
,
1873 cur_protection
, max_protection
,
1874 VM_BEHAVIOR_DEFAULT
,
1875 inheritance
, 0, no_cache
);
1876 new_entry
->alias
= alias
;
1879 boolean_t submap_is_64bit
;
1882 new_entry
->is_sub_map
= TRUE
;
1883 submap
= (vm_map_t
) object
;
1884 submap_is_64bit
= vm_map_is_64bit(submap
);
1885 use_pmap
= (alias
== VM_MEMORY_SHARED_PMAP
);
1886 #ifndef NO_NESTED_PMAP
1887 if (use_pmap
&& submap
->pmap
== NULL
) {
1888 /* we need a sub pmap to nest... */
1889 submap
->pmap
= pmap_create(0, submap_is_64bit
);
1890 if (submap
->pmap
== NULL
) {
1891 /* let's proceed without nesting... */
1894 if (use_pmap
&& submap
->pmap
!= NULL
) {
1897 kr
= pmap_nest(map
->pmap
,
1901 tmp_end
- tmp_start
);
1902 if (kr
!= KERN_SUCCESS
) {
1903 printf("vm_map_enter: "
1904 "pmap_nest(0x%llx,0x%llx) "
1906 (long long)tmp_start
,
1910 /* we're now nested ! */
1911 new_entry
->use_pmap
= TRUE
;
1915 #endif /* NO_NESTED_PMAP */
1918 } while (tmp_end
!= end
&&
1919 (tmp_start
= tmp_end
) &&
1920 (tmp_end
= (end
- tmp_end
> (vm_map_size_t
)VM_MAX_ADDRESS
) ?
1921 tmp_end
+ (vm_map_size_t
)VM_MAX_ADDRESS
: end
));
1926 new_mapping_established
= TRUE
;
1928 /* Wire down the new entry if the user
1929 * requested all new map entries be wired.
1931 if (map
->wiring_required
) {
1932 pmap_empty
= FALSE
; /* pmap won't be empty */
1933 result
= vm_map_wire(map
, start
, end
,
1934 new_entry
->protection
, TRUE
);
1938 if ((object
!= VM_OBJECT_NULL
) &&
1939 (vm_map_pmap_enter_enable
) &&
1942 (size
< (128*1024))) {
1943 pmap_empty
= FALSE
; /* pmap won't be empty */
1945 if (override_nx(map
, alias
) && cur_protection
)
1946 cur_protection
|= VM_PROT_EXECUTE
;
1948 vm_map_pmap_enter(map
, start
, end
,
1949 object
, offset
, cur_protection
);
1953 if (result
== KERN_SUCCESS
&&
1955 !(flags
& VM_FLAGS_NO_PMAP_CHECK
)) {
1956 assert(vm_map_pmap_is_empty(map
, *address
, *address
+size
));
1959 if (result
!= KERN_SUCCESS
) {
1960 if (new_mapping_established
) {
1962 * We have to get rid of the new mappings since we
1963 * won't make them available to the user.
1964 * Try and do that atomically, to minimize the risk
1965 * that someone else create new mappings that range.
1967 zap_new_map
= vm_map_create(PMAP_NULL
,
1975 (void) vm_map_delete(map
, *address
, *address
+size
,
1976 VM_MAP_REMOVE_SAVE_ENTRIES
,
1979 if (zap_old_map
!= VM_MAP_NULL
&&
1980 zap_old_map
->hdr
.nentries
!= 0) {
1981 vm_map_entry_t entry1
, entry2
;
1984 * The new mapping failed. Attempt to restore
1985 * the old mappings, saved in the "zap_old_map".
1992 /* first check if the coast is still clear */
1993 start
= vm_map_first_entry(zap_old_map
)->vme_start
;
1994 end
= vm_map_last_entry(zap_old_map
)->vme_end
;
1995 if (vm_map_lookup_entry(map
, start
, &entry1
) ||
1996 vm_map_lookup_entry(map
, end
, &entry2
) ||
1999 * Part of that range has already been
2000 * re-mapped: we can't restore the old
2003 vm_map_enter_restore_failures
++;
2006 * Transfer the saved map entries from
2007 * "zap_old_map" to the original "map",
2008 * inserting them all after "entry1".
2010 for (entry2
= vm_map_first_entry(zap_old_map
);
2011 entry2
!= vm_map_to_entry(zap_old_map
);
2012 entry2
= vm_map_first_entry(zap_old_map
)) {
2013 vm_map_size_t entry_size
;
2015 entry_size
= (entry2
->vme_end
-
2017 vm_map_entry_unlink(zap_old_map
,
2019 zap_old_map
->size
-= entry_size
;
2020 vm_map_entry_link(map
, entry1
, entry2
);
2021 map
->size
+= entry_size
;
2024 if (map
->wiring_required
) {
2026 * XXX TODO: we should rewire the
2030 vm_map_enter_restore_successes
++;
2040 * Get rid of the "zap_maps" and all the map entries that
2041 * they may still contain.
2043 if (zap_old_map
!= VM_MAP_NULL
) {
2044 vm_map_destroy(zap_old_map
, VM_MAP_REMOVE_NO_PMAP_CLEANUP
);
2045 zap_old_map
= VM_MAP_NULL
;
2047 if (zap_new_map
!= VM_MAP_NULL
) {
2048 vm_map_destroy(zap_new_map
, VM_MAP_REMOVE_NO_PMAP_CLEANUP
);
2049 zap_new_map
= VM_MAP_NULL
;
2058 vm_map_enter_mem_object(
2059 vm_map_t target_map
,
2060 vm_map_offset_t
*address
,
2061 vm_map_size_t initial_size
,
2062 vm_map_offset_t mask
,
2065 vm_object_offset_t offset
,
2067 vm_prot_t cur_protection
,
2068 vm_prot_t max_protection
,
2069 vm_inherit_t inheritance
)
2071 vm_map_address_t map_addr
;
2072 vm_map_size_t map_size
;
2074 vm_object_size_t size
;
2075 kern_return_t result
;
2078 * Check arguments for validity
2080 if ((target_map
== VM_MAP_NULL
) ||
2081 (cur_protection
& ~VM_PROT_ALL
) ||
2082 (max_protection
& ~VM_PROT_ALL
) ||
2083 (inheritance
> VM_INHERIT_LAST_VALID
) ||
2085 return KERN_INVALID_ARGUMENT
;
2087 map_addr
= vm_map_trunc_page(*address
);
2088 map_size
= vm_map_round_page(initial_size
);
2089 size
= vm_object_round_page(initial_size
);
2092 * Find the vm object (if any) corresponding to this port.
2094 if (!IP_VALID(port
)) {
2095 object
= VM_OBJECT_NULL
;
2098 } else if (ip_kotype(port
) == IKOT_NAMED_ENTRY
) {
2099 vm_named_entry_t named_entry
;
2101 named_entry
= (vm_named_entry_t
) port
->ip_kobject
;
2102 /* a few checks to make sure user is obeying rules */
2104 if (offset
>= named_entry
->size
)
2105 return KERN_INVALID_RIGHT
;
2106 size
= named_entry
->size
- offset
;
2108 if ((named_entry
->protection
& max_protection
) !=
2110 return KERN_INVALID_RIGHT
;
2111 if ((named_entry
->protection
& cur_protection
) !=
2113 return KERN_INVALID_RIGHT
;
2114 if (named_entry
->size
< (offset
+ size
))
2115 return KERN_INVALID_ARGUMENT
;
2117 /* the callers parameter offset is defined to be the */
2118 /* offset from beginning of named entry offset in object */
2119 offset
= offset
+ named_entry
->offset
;
2121 named_entry_lock(named_entry
);
2122 if (named_entry
->is_sub_map
) {
2125 submap
= named_entry
->backing
.map
;
2126 vm_map_lock(submap
);
2127 vm_map_reference(submap
);
2128 vm_map_unlock(submap
);
2129 named_entry_unlock(named_entry
);
2131 result
= vm_map_enter(target_map
,
2135 flags
| VM_FLAGS_SUBMAP
,
2136 (vm_object_t
) submap
,
2142 if (result
!= KERN_SUCCESS
) {
2143 vm_map_deallocate(submap
);
2146 * No need to lock "submap" just to check its
2147 * "mapped" flag: that flag is never reset
2148 * once it's been set and if we race, we'll
2149 * just end up setting it twice, which is OK.
2151 if (submap
->mapped
== FALSE
) {
2153 * This submap has never been mapped.
2154 * Set its "mapped" flag now that it
2156 * This happens only for the first ever
2157 * mapping of a "submap".
2159 vm_map_lock(submap
);
2160 submap
->mapped
= TRUE
;
2161 vm_map_unlock(submap
);
2163 *address
= map_addr
;
2167 } else if (named_entry
->is_pager
) {
2168 unsigned int access
;
2169 vm_prot_t protections
;
2170 unsigned int wimg_mode
;
2171 boolean_t cache_attr
;
2173 protections
= named_entry
->protection
& VM_PROT_ALL
;
2174 access
= GET_MAP_MEM(named_entry
->protection
);
2176 object
= vm_object_enter(named_entry
->backing
.pager
,
2178 named_entry
->internal
,
2181 if (object
== VM_OBJECT_NULL
) {
2182 named_entry_unlock(named_entry
);
2183 return KERN_INVALID_OBJECT
;
2186 /* JMM - drop reference on pager here */
2188 /* create an extra ref for the named entry */
2189 vm_object_lock(object
);
2190 vm_object_reference_locked(object
);
2191 named_entry
->backing
.object
= object
;
2192 named_entry
->is_pager
= FALSE
;
2193 named_entry_unlock(named_entry
);
2195 wimg_mode
= object
->wimg_bits
;
2196 if (access
== MAP_MEM_IO
) {
2197 wimg_mode
= VM_WIMG_IO
;
2198 } else if (access
== MAP_MEM_COPYBACK
) {
2199 wimg_mode
= VM_WIMG_USE_DEFAULT
;
2200 } else if (access
== MAP_MEM_WTHRU
) {
2201 wimg_mode
= VM_WIMG_WTHRU
;
2202 } else if (access
== MAP_MEM_WCOMB
) {
2203 wimg_mode
= VM_WIMG_WCOMB
;
2205 if (wimg_mode
== VM_WIMG_IO
||
2206 wimg_mode
== VM_WIMG_WCOMB
)
2211 /* wait for object (if any) to be ready */
2212 if (!named_entry
->internal
) {
2213 while (!object
->pager_ready
) {
2216 VM_OBJECT_EVENT_PAGER_READY
,
2218 vm_object_lock(object
);
2222 if (object
->wimg_bits
!= wimg_mode
) {
2225 vm_object_paging_wait(object
, THREAD_UNINT
);
2227 object
->wimg_bits
= wimg_mode
;
2228 queue_iterate(&object
->memq
, p
, vm_page_t
, listq
) {
2229 if (!p
->fictitious
) {
2231 pmap_disconnect(p
->phys_page
);
2233 pmap_sync_page_attributes_phys(p
->phys_page
);
2237 object
->true_share
= TRUE
;
2238 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
)
2239 object
->copy_strategy
= MEMORY_OBJECT_COPY_DELAY
;
2240 vm_object_unlock(object
);
2242 /* This is the case where we are going to map */
2243 /* an already mapped object. If the object is */
2244 /* not ready it is internal. An external */
2245 /* object cannot be mapped until it is ready */
2246 /* we can therefore avoid the ready check */
2248 object
= named_entry
->backing
.object
;
2249 assert(object
!= VM_OBJECT_NULL
);
2250 named_entry_unlock(named_entry
);
2251 vm_object_reference(object
);
2253 } else if (ip_kotype(port
) == IKOT_MEMORY_OBJECT
) {
2255 * JMM - This is temporary until we unify named entries
2256 * and raw memory objects.
2258 * Detected fake ip_kotype for a memory object. In
2259 * this case, the port isn't really a port at all, but
2260 * instead is just a raw memory object.
2263 object
= vm_object_enter((memory_object_t
)port
,
2264 size
, FALSE
, FALSE
, FALSE
);
2265 if (object
== VM_OBJECT_NULL
)
2266 return KERN_INVALID_OBJECT
;
2268 /* wait for object (if any) to be ready */
2269 if (object
!= VM_OBJECT_NULL
) {
2270 if (object
== kernel_object
) {
2271 printf("Warning: Attempt to map kernel object"
2272 " by a non-private kernel entity\n");
2273 return KERN_INVALID_OBJECT
;
2275 vm_object_lock(object
);
2276 while (!object
->pager_ready
) {
2277 vm_object_wait(object
,
2278 VM_OBJECT_EVENT_PAGER_READY
,
2280 vm_object_lock(object
);
2282 vm_object_unlock(object
);
2285 return KERN_INVALID_OBJECT
;
2289 * Perform the copy if requested
2293 vm_object_t new_object
;
2294 vm_object_offset_t new_offset
;
2296 result
= vm_object_copy_strategically(object
, offset
, size
,
2297 &new_object
, &new_offset
,
2301 if (result
== KERN_MEMORY_RESTART_COPY
) {
2303 boolean_t src_needs_copy
;
2307 * We currently ignore src_needs_copy.
2308 * This really is the issue of how to make
2309 * MEMORY_OBJECT_COPY_SYMMETRIC safe for
2310 * non-kernel users to use. Solution forthcoming.
2311 * In the meantime, since we don't allow non-kernel
2312 * memory managers to specify symmetric copy,
2313 * we won't run into problems here.
2315 new_object
= object
;
2316 new_offset
= offset
;
2317 success
= vm_object_copy_quickly(&new_object
,
2322 result
= KERN_SUCCESS
;
2325 * Throw away the reference to the
2326 * original object, as it won't be mapped.
2329 vm_object_deallocate(object
);
2331 if (result
!= KERN_SUCCESS
)
2334 object
= new_object
;
2335 offset
= new_offset
;
2338 result
= vm_map_enter(target_map
,
2339 &map_addr
, map_size
,
2340 (vm_map_offset_t
)mask
,
2344 cur_protection
, max_protection
, inheritance
);
2345 if (result
!= KERN_SUCCESS
)
2346 vm_object_deallocate(object
);
2347 *address
= map_addr
;
2354 extern pmap_paddr_t avail_start
, avail_end
;
2358 * Allocate memory in the specified map, with the caveat that
2359 * the memory is physically contiguous. This call may fail
2360 * if the system can't find sufficient contiguous memory.
2361 * This call may cause or lead to heart-stopping amounts of
2364 * Memory obtained from this call should be freed in the
2365 * normal way, viz., via vm_deallocate.
2370 vm_map_offset_t
*addr
,
2374 vm_object_t cpm_obj
;
2378 vm_map_offset_t va
, start
, end
, offset
;
2380 vm_map_offset_t prev_addr
;
2381 #endif /* MACH_ASSERT */
2383 boolean_t anywhere
= ((VM_FLAGS_ANYWHERE
& flags
) != 0);
2385 if (!vm_allocate_cpm_enabled
)
2386 return KERN_FAILURE
;
2390 return KERN_SUCCESS
;
2393 *addr
= vm_map_min(map
);
2395 *addr
= vm_map_trunc_page(*addr
);
2396 size
= vm_map_round_page(size
);
2399 * LP64todo - cpm_allocate should probably allow
2400 * allocations of >4GB, but not with the current
2401 * algorithm, so just cast down the size for now.
2403 if (size
> VM_MAX_ADDRESS
)
2404 return KERN_RESOURCE_SHORTAGE
;
2405 if ((kr
= cpm_allocate(CAST_DOWN(vm_size_t
, size
),
2406 &pages
, 0, TRUE
)) != KERN_SUCCESS
)
2409 cpm_obj
= vm_object_allocate((vm_object_size_t
)size
);
2410 assert(cpm_obj
!= VM_OBJECT_NULL
);
2411 assert(cpm_obj
->internal
);
2412 assert(cpm_obj
->size
== (vm_object_size_t
)size
);
2413 assert(cpm_obj
->can_persist
== FALSE
);
2414 assert(cpm_obj
->pager_created
== FALSE
);
2415 assert(cpm_obj
->pageout
== FALSE
);
2416 assert(cpm_obj
->shadow
== VM_OBJECT_NULL
);
2419 * Insert pages into object.
2422 vm_object_lock(cpm_obj
);
2423 for (offset
= 0; offset
< size
; offset
+= PAGE_SIZE
) {
2425 pages
= NEXT_PAGE(m
);
2426 *(NEXT_PAGE_PTR(m
)) = VM_PAGE_NULL
;
2428 assert(!m
->gobbled
);
2430 assert(!m
->pageout
);
2432 assert(m
->wire_count
);
2435 * "m" is not supposed to be pageable, so it
2436 * should not be encrypted. It wouldn't be safe
2437 * to enter it in a new VM object while encrypted.
2439 ASSERT_PAGE_DECRYPTED(m
);
2441 assert(m
->phys_page
>=(avail_start
>>PAGE_SHIFT
) && m
->phys_page
<=(avail_end
>>PAGE_SHIFT
));
2444 vm_page_insert(m
, cpm_obj
, offset
);
2446 assert(cpm_obj
->resident_page_count
== size
/ PAGE_SIZE
);
2447 vm_object_unlock(cpm_obj
);
2450 * Hang onto a reference on the object in case a
2451 * multi-threaded application for some reason decides
2452 * to deallocate the portion of the address space into
2453 * which we will insert this object.
2455 * Unfortunately, we must insert the object now before
2456 * we can talk to the pmap module about which addresses
2457 * must be wired down. Hence, the race with a multi-
2460 vm_object_reference(cpm_obj
);
2463 * Insert object into map.
2473 (vm_object_offset_t
)0,
2477 VM_INHERIT_DEFAULT
);
2479 if (kr
!= KERN_SUCCESS
) {
2481 * A CPM object doesn't have can_persist set,
2482 * so all we have to do is deallocate it to
2483 * free up these pages.
2485 assert(cpm_obj
->pager_created
== FALSE
);
2486 assert(cpm_obj
->can_persist
== FALSE
);
2487 assert(cpm_obj
->pageout
== FALSE
);
2488 assert(cpm_obj
->shadow
== VM_OBJECT_NULL
);
2489 vm_object_deallocate(cpm_obj
); /* kill acquired ref */
2490 vm_object_deallocate(cpm_obj
); /* kill creation ref */
2494 * Inform the physical mapping system that the
2495 * range of addresses may not fault, so that
2496 * page tables and such can be locked down as well.
2500 pmap
= vm_map_pmap(map
);
2501 pmap_pageable(pmap
, start
, end
, FALSE
);
2504 * Enter each page into the pmap, to avoid faults.
2505 * Note that this loop could be coded more efficiently,
2506 * if the need arose, rather than looking up each page
2509 for (offset
= 0, va
= start
; offset
< size
;
2510 va
+= PAGE_SIZE
, offset
+= PAGE_SIZE
) {
2513 vm_object_lock(cpm_obj
);
2514 m
= vm_page_lookup(cpm_obj
, (vm_object_offset_t
)offset
);
2515 assert(m
!= VM_PAGE_NULL
);
2517 vm_page_zero_fill(m
);
2519 type_of_fault
= DBG_ZERO_FILL_FAULT
;
2521 vm_fault_enter(m
, pmap
, va
, VM_PROT_ALL
,
2522 m
->wire_count
!= 0, FALSE
, FALSE
,
2525 vm_object_unlock(cpm_obj
);
2530 * Verify ordering in address space.
2532 for (offset
= 0; offset
< size
; offset
+= PAGE_SIZE
) {
2533 vm_object_lock(cpm_obj
);
2534 m
= vm_page_lookup(cpm_obj
, (vm_object_offset_t
)offset
);
2535 vm_object_unlock(cpm_obj
);
2536 if (m
== VM_PAGE_NULL
)
2537 panic("vm_allocate_cpm: obj 0x%x off 0x%x no page",
2542 assert(!m
->fictitious
);
2543 assert(!m
->private);
2546 assert(!m
->cleaning
);
2547 assert(!m
->precious
);
2548 assert(!m
->clustered
);
2550 if (m
->phys_page
!= prev_addr
+ 1) {
2551 printf("start 0x%x end 0x%x va 0x%x\n",
2553 printf("obj 0x%x off 0x%x\n", cpm_obj
, offset
);
2554 printf("m 0x%x prev_address 0x%x\n", m
,
2556 panic("vm_allocate_cpm: pages not contig!");
2559 prev_addr
= m
->phys_page
;
2561 #endif /* MACH_ASSERT */
2563 vm_object_deallocate(cpm_obj
); /* kill extra ref */
2572 * Interface is defined in all cases, but unless the kernel
2573 * is built explicitly for this option, the interface does
2579 __unused vm_map_t map
,
2580 __unused vm_map_offset_t
*addr
,
2581 __unused vm_map_size_t size
,
2584 return KERN_FAILURE
;
2589 * Clip and unnest a portion of a nested submap mapping.
2594 vm_map_entry_t entry
,
2595 vm_map_offset_t start_unnest
,
2596 vm_map_offset_t end_unnest
)
2598 assert(entry
->is_sub_map
);
2599 assert(entry
->object
.sub_map
!= NULL
);
2601 if (entry
->vme_start
> start_unnest
||
2602 entry
->vme_end
< end_unnest
) {
2603 panic("vm_map_clip_unnest(0x%llx,0x%llx): "
2604 "bad nested entry: start=0x%llx end=0x%llx\n",
2605 (long long)start_unnest
, (long long)end_unnest
,
2606 (long long)entry
->vme_start
, (long long)entry
->vme_end
);
2608 if (start_unnest
> entry
->vme_start
) {
2609 _vm_map_clip_start(&map
->hdr
,
2612 UPDATE_FIRST_FREE(map
, map
->first_free
);
2614 if (entry
->vme_end
> end_unnest
) {
2615 _vm_map_clip_end(&map
->hdr
,
2618 UPDATE_FIRST_FREE(map
, map
->first_free
);
2621 pmap_unnest(map
->pmap
,
2623 entry
->vme_end
- entry
->vme_start
);
2624 if ((map
->mapped
) && (map
->ref_count
)) {
2625 /* clean up parent map/maps */
2626 vm_map_submap_pmap_clean(
2627 map
, entry
->vme_start
,
2629 entry
->object
.sub_map
,
2632 entry
->use_pmap
= FALSE
;
2636 * vm_map_clip_start: [ internal use only ]
2638 * Asserts that the given entry begins at or after
2639 * the specified address; if necessary,
2640 * it splits the entry into two.
2645 vm_map_entry_t entry
,
2646 vm_map_offset_t startaddr
)
2648 #ifndef NO_NESTED_PMAP
2649 if (entry
->use_pmap
&&
2650 startaddr
>= entry
->vme_start
) {
2651 vm_map_offset_t start_unnest
, end_unnest
;
2654 * Make sure "startaddr" is no longer in a nested range
2655 * before we clip. Unnest only the minimum range the platform
2658 start_unnest
= startaddr
& ~(pmap_nesting_size_min
- 1);
2659 end_unnest
= start_unnest
+ pmap_nesting_size_min
;
2660 vm_map_clip_unnest(map
, entry
, start_unnest
, end_unnest
);
2662 #endif /* NO_NESTED_PMAP */
2663 if (startaddr
> entry
->vme_start
) {
2664 if (entry
->object
.vm_object
&&
2665 !entry
->is_sub_map
&&
2666 entry
->object
.vm_object
->phys_contiguous
) {
2667 pmap_remove(map
->pmap
,
2668 (addr64_t
)(entry
->vme_start
),
2669 (addr64_t
)(entry
->vme_end
));
2671 _vm_map_clip_start(&map
->hdr
, entry
, startaddr
);
2672 UPDATE_FIRST_FREE(map
, map
->first_free
);
2677 #define vm_map_copy_clip_start(copy, entry, startaddr) \
2679 if ((startaddr) > (entry)->vme_start) \
2680 _vm_map_clip_start(&(copy)->cpy_hdr,(entry),(startaddr)); \
2684 * This routine is called only when it is known that
2685 * the entry must be split.
2689 register struct vm_map_header
*map_header
,
2690 register vm_map_entry_t entry
,
2691 register vm_map_offset_t start
)
2693 register vm_map_entry_t new_entry
;
2696 * Split off the front portion --
2697 * note that we must insert the new
2698 * entry BEFORE this one, so that
2699 * this entry has the specified starting
2703 new_entry
= _vm_map_entry_create(map_header
);
2704 vm_map_entry_copy_full(new_entry
, entry
);
2706 new_entry
->vme_end
= start
;
2707 entry
->offset
+= (start
- entry
->vme_start
);
2708 entry
->vme_start
= start
;
2710 _vm_map_entry_link(map_header
, entry
->vme_prev
, new_entry
);
2712 if (entry
->is_sub_map
)
2713 vm_map_reference(new_entry
->object
.sub_map
);
2715 vm_object_reference(new_entry
->object
.vm_object
);
2720 * vm_map_clip_end: [ internal use only ]
2722 * Asserts that the given entry ends at or before
2723 * the specified address; if necessary,
2724 * it splits the entry into two.
2729 vm_map_entry_t entry
,
2730 vm_map_offset_t endaddr
)
2732 if (endaddr
> entry
->vme_end
) {
2734 * Within the scope of this clipping, limit "endaddr" to
2735 * the end of this map entry...
2737 endaddr
= entry
->vme_end
;
2739 #ifndef NO_NESTED_PMAP
2740 if (entry
->use_pmap
) {
2741 vm_map_offset_t start_unnest
, end_unnest
;
2744 * Make sure the range between the start of this entry and
2745 * the new "endaddr" is no longer nested before we clip.
2746 * Unnest only the minimum range the platform can handle.
2748 start_unnest
= entry
->vme_start
;
2750 (endaddr
+ pmap_nesting_size_min
- 1) &
2751 ~(pmap_nesting_size_min
- 1);
2752 vm_map_clip_unnest(map
, entry
, start_unnest
, end_unnest
);
2754 #endif /* NO_NESTED_PMAP */
2755 if (endaddr
< entry
->vme_end
) {
2756 if (entry
->object
.vm_object
&&
2757 !entry
->is_sub_map
&&
2758 entry
->object
.vm_object
->phys_contiguous
) {
2759 pmap_remove(map
->pmap
,
2760 (addr64_t
)(entry
->vme_start
),
2761 (addr64_t
)(entry
->vme_end
));
2763 _vm_map_clip_end(&map
->hdr
, entry
, endaddr
);
2764 UPDATE_FIRST_FREE(map
, map
->first_free
);
2769 #define vm_map_copy_clip_end(copy, entry, endaddr) \
2771 if ((endaddr) < (entry)->vme_end) \
2772 _vm_map_clip_end(&(copy)->cpy_hdr,(entry),(endaddr)); \
2776 * This routine is called only when it is known that
2777 * the entry must be split.
2781 register struct vm_map_header
*map_header
,
2782 register vm_map_entry_t entry
,
2783 register vm_map_offset_t end
)
2785 register vm_map_entry_t new_entry
;
2788 * Create a new entry and insert it
2789 * AFTER the specified entry
2792 new_entry
= _vm_map_entry_create(map_header
);
2793 vm_map_entry_copy_full(new_entry
, entry
);
2795 new_entry
->vme_start
= entry
->vme_end
= end
;
2796 new_entry
->offset
+= (end
- entry
->vme_start
);
2798 _vm_map_entry_link(map_header
, entry
, new_entry
);
2800 if (entry
->is_sub_map
)
2801 vm_map_reference(new_entry
->object
.sub_map
);
2803 vm_object_reference(new_entry
->object
.vm_object
);
2808 * VM_MAP_RANGE_CHECK: [ internal use only ]
2810 * Asserts that the starting and ending region
2811 * addresses fall within the valid range of the map.
2813 #define VM_MAP_RANGE_CHECK(map, start, end) \
2815 if (start < vm_map_min(map)) \
2816 start = vm_map_min(map); \
2817 if (end > vm_map_max(map)) \
2818 end = vm_map_max(map); \
2824 * vm_map_range_check: [ internal use only ]
2826 * Check that the region defined by the specified start and
2827 * end addresses are wholly contained within a single map
2828 * entry or set of adjacent map entries of the spacified map,
2829 * i.e. the specified region contains no unmapped space.
2830 * If any or all of the region is unmapped, FALSE is returned.
2831 * Otherwise, TRUE is returned and if the output argument 'entry'
2832 * is not NULL it points to the map entry containing the start
2835 * The map is locked for reading on entry and is left locked.
2839 register vm_map_t map
,
2840 register vm_map_offset_t start
,
2841 register vm_map_offset_t end
,
2842 vm_map_entry_t
*entry
)
2845 register vm_map_offset_t prev
;
2848 * Basic sanity checks first
2850 if (start
< vm_map_min(map
) || end
> vm_map_max(map
) || start
> end
)
2854 * Check first if the region starts within a valid
2855 * mapping for the map.
2857 if (!vm_map_lookup_entry(map
, start
, &cur
))
2861 * Optimize for the case that the region is contained
2862 * in a single map entry.
2864 if (entry
!= (vm_map_entry_t
*) NULL
)
2866 if (end
<= cur
->vme_end
)
2870 * If the region is not wholly contained within a
2871 * single entry, walk the entries looking for holes.
2873 prev
= cur
->vme_end
;
2874 cur
= cur
->vme_next
;
2875 while ((cur
!= vm_map_to_entry(map
)) && (prev
== cur
->vme_start
)) {
2876 if (end
<= cur
->vme_end
)
2878 prev
= cur
->vme_end
;
2879 cur
= cur
->vme_next
;
2885 * vm_map_submap: [ kernel use only ]
2887 * Mark the given range as handled by a subordinate map.
2889 * This range must have been created with vm_map_find using
2890 * the vm_submap_object, and no other operations may have been
2891 * performed on this range prior to calling vm_map_submap.
2893 * Only a limited number of operations can be performed
2894 * within this rage after calling vm_map_submap:
2896 * [Don't try vm_map_copyin!]
2898 * To remove a submapping, one must first remove the
2899 * range from the superior map, and then destroy the
2900 * submap (if desired). [Better yet, don't try it.]
2905 vm_map_offset_t start
,
2906 vm_map_offset_t end
,
2908 vm_map_offset_t offset
,
2909 #ifdef NO_NESTED_PMAP
2911 #endif /* NO_NESTED_PMAP */
2914 vm_map_entry_t entry
;
2915 register kern_return_t result
= KERN_INVALID_ARGUMENT
;
2916 register vm_object_t object
;
2920 if (! vm_map_lookup_entry(map
, start
, &entry
)) {
2921 entry
= entry
->vme_next
;
2924 if (entry
== vm_map_to_entry(map
) ||
2925 entry
->is_sub_map
) {
2927 return KERN_INVALID_ARGUMENT
;
2930 assert(!entry
->use_pmap
); /* we don't want to unnest anything here */
2931 vm_map_clip_start(map
, entry
, start
);
2932 vm_map_clip_end(map
, entry
, end
);
2934 if ((entry
->vme_start
== start
) && (entry
->vme_end
== end
) &&
2935 (!entry
->is_sub_map
) &&
2936 ((object
= entry
->object
.vm_object
) == vm_submap_object
) &&
2937 (object
->resident_page_count
== 0) &&
2938 (object
->copy
== VM_OBJECT_NULL
) &&
2939 (object
->shadow
== VM_OBJECT_NULL
) &&
2940 (!object
->pager_created
)) {
2941 entry
->offset
= (vm_object_offset_t
)offset
;
2942 entry
->object
.vm_object
= VM_OBJECT_NULL
;
2943 vm_object_deallocate(object
);
2944 entry
->is_sub_map
= TRUE
;
2945 entry
->object
.sub_map
= submap
;
2946 vm_map_reference(submap
);
2947 submap
->mapped
= TRUE
;
2949 #ifndef NO_NESTED_PMAP
2951 /* nest if platform code will allow */
2952 if(submap
->pmap
== NULL
) {
2953 submap
->pmap
= pmap_create((vm_map_size_t
) 0, FALSE
);
2954 if(submap
->pmap
== PMAP_NULL
) {
2956 return(KERN_NO_SPACE
);
2959 result
= pmap_nest(map
->pmap
,
2960 (entry
->object
.sub_map
)->pmap
,
2963 (uint64_t)(end
- start
));
2965 panic("vm_map_submap: pmap_nest failed, rc = %08X\n", result
);
2966 entry
->use_pmap
= TRUE
;
2968 #else /* NO_NESTED_PMAP */
2969 pmap_remove(map
->pmap
, (addr64_t
)start
, (addr64_t
)end
);
2970 #endif /* NO_NESTED_PMAP */
2971 result
= KERN_SUCCESS
;
2981 * Sets the protection of the specified address
2982 * region in the target map. If "set_max" is
2983 * specified, the maximum protection is to be set;
2984 * otherwise, only the current protection is affected.
2988 register vm_map_t map
,
2989 register vm_map_offset_t start
,
2990 register vm_map_offset_t end
,
2991 register vm_prot_t new_prot
,
2992 register boolean_t set_max
)
2994 register vm_map_entry_t current
;
2995 register vm_map_offset_t prev
;
2996 vm_map_entry_t entry
;
3000 "vm_map_protect, 0x%X start 0x%X end 0x%X, new 0x%X %d",
3001 (integer_t
)map
, start
, end
, new_prot
, set_max
);
3005 /* LP64todo - remove this check when vm_map_commpage64()
3006 * no longer has to stuff in a map_entry for the commpage
3007 * above the map's max_offset.
3009 if (start
>= map
->max_offset
) {
3011 return(KERN_INVALID_ADDRESS
);
3015 * Lookup the entry. If it doesn't start in a valid
3016 * entry, return an error.
3018 if (! vm_map_lookup_entry(map
, start
, &entry
)) {
3020 return(KERN_INVALID_ADDRESS
);
3024 * Make a first pass to check for protection and address
3029 prev
= current
->vme_start
;
3030 while ((current
!= vm_map_to_entry(map
)) &&
3031 (current
->vme_start
< end
)) {
3034 * If there is a hole, return an error.
3036 if (current
->vme_start
!= prev
) {
3038 return(KERN_INVALID_ADDRESS
);
3041 new_max
= current
->max_protection
;
3042 if(new_prot
& VM_PROT_COPY
) {
3043 new_max
|= VM_PROT_WRITE
;
3044 if ((new_prot
& (new_max
| VM_PROT_COPY
)) != new_prot
) {
3046 return(KERN_PROTECTION_FAILURE
);
3049 if ((new_prot
& new_max
) != new_prot
) {
3051 return(KERN_PROTECTION_FAILURE
);
3055 prev
= current
->vme_end
;
3056 current
= current
->vme_next
;
3060 return(KERN_INVALID_ADDRESS
);
3064 * Go back and fix up protections.
3065 * Clip to start here if the range starts within
3070 if (current
!= vm_map_to_entry(map
)) {
3071 /* clip and unnest if necessary */
3072 vm_map_clip_start(map
, current
, start
);
3075 while ((current
!= vm_map_to_entry(map
)) &&
3076 (current
->vme_start
< end
)) {
3080 vm_map_clip_end(map
, current
, end
);
3082 assert(!current
->use_pmap
); /* clipping did unnest if needed */
3084 old_prot
= current
->protection
;
3086 if(new_prot
& VM_PROT_COPY
) {
3087 /* caller is asking specifically to copy the */
3088 /* mapped data, this implies that max protection */
3089 /* will include write. Caller must be prepared */
3090 /* for loss of shared memory communication in the */
3091 /* target area after taking this step */
3092 current
->needs_copy
= TRUE
;
3093 current
->max_protection
|= VM_PROT_WRITE
;
3097 current
->protection
=
3098 (current
->max_protection
=
3099 new_prot
& ~VM_PROT_COPY
) &
3102 current
->protection
= new_prot
& ~VM_PROT_COPY
;
3105 * Update physical map if necessary.
3106 * If the request is to turn off write protection,
3107 * we won't do it for real (in pmap). This is because
3108 * it would cause copy-on-write to fail. We've already
3109 * set, the new protection in the map, so if a
3110 * write-protect fault occurred, it will be fixed up
3111 * properly, COW or not.
3113 if (current
->protection
!= old_prot
) {
3114 /* Look one level in we support nested pmaps */
3115 /* from mapped submaps which are direct entries */
3120 prot
= current
->protection
& ~VM_PROT_WRITE
;
3122 if (override_nx(map
, current
->alias
) && prot
)
3123 prot
|= VM_PROT_EXECUTE
;
3125 if (current
->is_sub_map
&& current
->use_pmap
) {
3126 pmap_protect(current
->object
.sub_map
->pmap
,
3131 pmap_protect(map
->pmap
,
3137 current
= current
->vme_next
;
3141 while ((current
!= vm_map_to_entry(map
)) &&
3142 (current
->vme_start
<= end
)) {
3143 vm_map_simplify_entry(map
, current
);
3144 current
= current
->vme_next
;
3148 return(KERN_SUCCESS
);
3154 * Sets the inheritance of the specified address
3155 * range in the target map. Inheritance
3156 * affects how the map will be shared with
3157 * child maps at the time of vm_map_fork.
3161 register vm_map_t map
,
3162 register vm_map_offset_t start
,
3163 register vm_map_offset_t end
,
3164 register vm_inherit_t new_inheritance
)
3166 register vm_map_entry_t entry
;
3167 vm_map_entry_t temp_entry
;
3171 VM_MAP_RANGE_CHECK(map
, start
, end
);
3173 if (vm_map_lookup_entry(map
, start
, &temp_entry
)) {
3177 temp_entry
= temp_entry
->vme_next
;
3181 /* first check entire range for submaps which can't support the */
3182 /* given inheritance. */
3183 while ((entry
!= vm_map_to_entry(map
)) && (entry
->vme_start
< end
)) {
3184 if(entry
->is_sub_map
) {
3185 if(new_inheritance
== VM_INHERIT_COPY
) {
3187 return(KERN_INVALID_ARGUMENT
);
3191 entry
= entry
->vme_next
;
3195 if (entry
!= vm_map_to_entry(map
)) {
3196 /* clip and unnest if necessary */
3197 vm_map_clip_start(map
, entry
, start
);
3200 while ((entry
!= vm_map_to_entry(map
)) && (entry
->vme_start
< end
)) {
3201 vm_map_clip_end(map
, entry
, end
);
3202 assert(!entry
->use_pmap
); /* clip did unnest if needed */
3204 entry
->inheritance
= new_inheritance
;
3206 entry
= entry
->vme_next
;
3210 return(KERN_SUCCESS
);
3214 * Update the accounting for the amount of wired memory in this map. If the user has
3215 * exceeded the defined limits, then we fail. Wiring on behalf of the kernel never fails.
3218 static kern_return_t
3221 vm_map_entry_t entry
,
3222 boolean_t user_wire
)
3229 * We're wiring memory at the request of the user. Check if this is the first time the user is wiring
3233 if (entry
->user_wired_count
== 0) {
3234 size
= entry
->vme_end
- entry
->vme_start
;
3237 * Since this is the first time the user is wiring this map entry, check to see if we're
3238 * exceeding the user wire limits. There is a per map limit which is the smaller of either
3239 * the process's rlimit or the global vm_user_wire_limit which caps this value. There is also
3240 * a system-wide limit on the amount of memory all users can wire. If the user is over either
3241 * limit, then we fail.
3244 if(size
+ map
->user_wire_size
> MIN(map
->user_wire_limit
, vm_user_wire_limit
) ||
3245 size
+ ptoa_64(vm_page_wire_count
) > vm_global_user_wire_limit
)
3246 return KERN_RESOURCE_SHORTAGE
;
3249 * The first time the user wires an entry, we also increment the wired_count and add this to
3250 * the total that has been wired in the map.
3253 if (entry
->wired_count
>= MAX_WIRE_COUNT
)
3254 return KERN_FAILURE
;
3256 entry
->wired_count
++;
3257 map
->user_wire_size
+= size
;
3260 if (entry
->user_wired_count
>= MAX_WIRE_COUNT
)
3261 return KERN_FAILURE
;
3263 entry
->user_wired_count
++;
3268 * The kernel's wiring the memory. Just bump the count and continue.
3271 if (entry
->wired_count
>= MAX_WIRE_COUNT
)
3272 panic("vm_map_wire: too many wirings");
3274 entry
->wired_count
++;
3277 return KERN_SUCCESS
;
3281 * Update the memory wiring accounting now that the given map entry is being unwired.
3285 subtract_wire_counts(
3287 vm_map_entry_t entry
,
3288 boolean_t user_wire
)
3294 * We're unwiring memory at the request of the user. See if we're removing the last user wire reference.
3297 if (entry
->user_wired_count
== 1) {
3300 * We're removing the last user wire reference. Decrement the wired_count and the total
3301 * user wired memory for this map.
3304 assert(entry
->wired_count
>= 1);
3305 entry
->wired_count
--;
3306 map
->user_wire_size
-= entry
->vme_end
- entry
->vme_start
;
3309 assert(entry
->user_wired_count
>= 1);
3310 entry
->user_wired_count
--;
3315 * The kernel is unwiring the memory. Just update the count.
3318 assert(entry
->wired_count
>= 1);
3319 entry
->wired_count
--;
3326 * Sets the pageability of the specified address range in the
3327 * target map as wired. Regions specified as not pageable require
3328 * locked-down physical memory and physical page maps. The
3329 * access_type variable indicates types of accesses that must not
3330 * generate page faults. This is checked against protection of
3331 * memory being locked-down.
3333 * The map must not be locked, but a reference must remain to the
3334 * map throughout the call.
3336 static kern_return_t
3338 register vm_map_t map
,
3339 register vm_map_offset_t start
,
3340 register vm_map_offset_t end
,
3341 register vm_prot_t access_type
,
3342 boolean_t user_wire
,
3344 vm_map_offset_t pmap_addr
)
3346 register vm_map_entry_t entry
;
3347 struct vm_map_entry
*first_entry
, tmp_entry
;
3349 register vm_map_offset_t s
,e
;
3351 boolean_t need_wakeup
;
3352 boolean_t main_map
= FALSE
;
3353 wait_interrupt_t interruptible_state
;
3354 thread_t cur_thread
;
3355 unsigned int last_timestamp
;
3359 if(map_pmap
== NULL
)
3361 last_timestamp
= map
->timestamp
;
3363 VM_MAP_RANGE_CHECK(map
, start
, end
);
3364 assert(page_aligned(start
));
3365 assert(page_aligned(end
));
3367 /* We wired what the caller asked for, zero pages */
3369 return KERN_SUCCESS
;
3372 need_wakeup
= FALSE
;
3373 cur_thread
= current_thread();
3378 if (vm_map_lookup_entry(map
, s
, &first_entry
)) {
3379 entry
= first_entry
;
3381 * vm_map_clip_start will be done later.
3382 * We don't want to unnest any nested submaps here !
3385 /* Start address is not in map */
3386 rc
= KERN_INVALID_ADDRESS
;
3390 while ((entry
!= vm_map_to_entry(map
)) && (s
< end
)) {
3392 * At this point, we have wired from "start" to "s".
3393 * We still need to wire from "s" to "end".
3395 * "entry" hasn't been clipped, so it could start before "s"
3396 * and/or end after "end".
3399 /* "e" is how far we want to wire in this entry */
3405 * If another thread is wiring/unwiring this entry then
3406 * block after informing other thread to wake us up.
3408 if (entry
->in_transition
) {
3409 wait_result_t wait_result
;
3412 * We have not clipped the entry. Make sure that
3413 * the start address is in range so that the lookup
3414 * below will succeed.
3415 * "s" is the current starting point: we've already
3416 * wired from "start" to "s" and we still have
3417 * to wire from "s" to "end".
3420 entry
->needs_wakeup
= TRUE
;
3423 * wake up anybody waiting on entries that we have
3427 vm_map_entry_wakeup(map
);
3428 need_wakeup
= FALSE
;
3431 * User wiring is interruptible
3433 wait_result
= vm_map_entry_wait(map
,
3434 (user_wire
) ? THREAD_ABORTSAFE
:
3436 if (user_wire
&& wait_result
== THREAD_INTERRUPTED
) {
3438 * undo the wirings we have done so far
3439 * We do not clear the needs_wakeup flag,
3440 * because we cannot tell if we were the
3448 * Cannot avoid a lookup here. reset timestamp.
3450 last_timestamp
= map
->timestamp
;
3453 * The entry could have been clipped, look it up again.
3454 * Worse that can happen is, it may not exist anymore.
3456 if (!vm_map_lookup_entry(map
, s
, &first_entry
)) {
3458 panic("vm_map_wire: re-lookup failed");
3461 * User: undo everything upto the previous
3462 * entry. let vm_map_unwire worry about
3463 * checking the validity of the range.
3468 entry
= first_entry
;
3472 if (entry
->is_sub_map
) {
3473 vm_map_offset_t sub_start
;
3474 vm_map_offset_t sub_end
;
3475 vm_map_offset_t local_start
;
3476 vm_map_offset_t local_end
;
3479 vm_map_clip_start(map
, entry
, s
);
3480 vm_map_clip_end(map
, entry
, end
);
3482 sub_start
= entry
->offset
;
3483 sub_end
= entry
->vme_end
;
3484 sub_end
+= entry
->offset
- entry
->vme_start
;
3486 local_end
= entry
->vme_end
;
3487 if(map_pmap
== NULL
) {
3489 vm_object_offset_t offset
;
3492 vm_map_entry_t local_entry
;
3493 vm_map_version_t version
;
3494 vm_map_t lookup_map
;
3496 if(entry
->use_pmap
) {
3497 pmap
= entry
->object
.sub_map
->pmap
;
3498 /* ppc implementation requires that */
3499 /* submaps pmap address ranges line */
3500 /* up with parent map */
3502 pmap_addr
= sub_start
;
3510 if (entry
->wired_count
) {
3511 if ((rc
= add_wire_counts(map
, entry
, user_wire
)) != KERN_SUCCESS
)
3515 * The map was not unlocked:
3516 * no need to goto re-lookup.
3517 * Just go directly to next entry.
3519 entry
= entry
->vme_next
;
3520 s
= entry
->vme_start
;
3525 /* call vm_map_lookup_locked to */
3526 /* cause any needs copy to be */
3528 local_start
= entry
->vme_start
;
3530 vm_map_lock_write_to_read(map
);
3531 if(vm_map_lookup_locked(
3532 &lookup_map
, local_start
,
3534 OBJECT_LOCK_EXCLUSIVE
,
3536 &offset
, &prot
, &wired
,
3540 vm_map_unlock_read(lookup_map
);
3541 vm_map_unwire(map
, start
,
3543 return(KERN_FAILURE
);
3545 if(real_map
!= lookup_map
)
3546 vm_map_unlock(real_map
);
3547 vm_map_unlock_read(lookup_map
);
3549 vm_object_unlock(object
);
3551 /* we unlocked, so must re-lookup */
3552 if (!vm_map_lookup_entry(map
,
3560 * entry could have been "simplified",
3563 entry
= local_entry
;
3564 assert(s
== local_start
);
3565 vm_map_clip_start(map
, entry
, s
);
3566 vm_map_clip_end(map
, entry
, end
);
3567 /* re-compute "e" */
3572 /* did we have a change of type? */
3573 if (!entry
->is_sub_map
) {
3574 last_timestamp
= map
->timestamp
;
3578 local_start
= entry
->vme_start
;
3582 if ((rc
= add_wire_counts(map
, entry
, user_wire
)) != KERN_SUCCESS
)
3585 entry
->in_transition
= TRUE
;
3588 rc
= vm_map_wire_nested(entry
->object
.sub_map
,
3591 user_wire
, pmap
, pmap_addr
);
3595 * Find the entry again. It could have been clipped
3596 * after we unlocked the map.
3598 if (!vm_map_lookup_entry(map
, local_start
,
3600 panic("vm_map_wire: re-lookup failed");
3601 entry
= first_entry
;
3603 assert(local_start
== s
);
3604 /* re-compute "e" */
3609 last_timestamp
= map
->timestamp
;
3610 while ((entry
!= vm_map_to_entry(map
)) &&
3611 (entry
->vme_start
< e
)) {
3612 assert(entry
->in_transition
);
3613 entry
->in_transition
= FALSE
;
3614 if (entry
->needs_wakeup
) {
3615 entry
->needs_wakeup
= FALSE
;
3618 if (rc
!= KERN_SUCCESS
) {/* from vm_*_wire */
3619 subtract_wire_counts(map
, entry
, user_wire
);
3621 entry
= entry
->vme_next
;
3623 if (rc
!= KERN_SUCCESS
) { /* from vm_*_wire */
3627 /* no need to relookup again */
3628 s
= entry
->vme_start
;
3633 * If this entry is already wired then increment
3634 * the appropriate wire reference count.
3636 if (entry
->wired_count
) {
3638 * entry is already wired down, get our reference
3639 * after clipping to our range.
3641 vm_map_clip_start(map
, entry
, s
);
3642 vm_map_clip_end(map
, entry
, end
);
3644 if ((rc
= add_wire_counts(map
, entry
, user_wire
)) != KERN_SUCCESS
)
3647 /* map was not unlocked: no need to relookup */
3648 entry
= entry
->vme_next
;
3649 s
= entry
->vme_start
;
3654 * Unwired entry or wire request transmitted via submap
3659 * Perform actions of vm_map_lookup that need the write
3660 * lock on the map: create a shadow object for a
3661 * copy-on-write region, or an object for a zero-fill
3664 size
= entry
->vme_end
- entry
->vme_start
;
3666 * If wiring a copy-on-write page, we need to copy it now
3667 * even if we're only (currently) requesting read access.
3668 * This is aggressive, but once it's wired we can't move it.
3670 if (entry
->needs_copy
) {
3671 vm_object_shadow(&entry
->object
.vm_object
,
3672 &entry
->offset
, size
);
3673 entry
->needs_copy
= FALSE
;
3674 } else if (entry
->object
.vm_object
== VM_OBJECT_NULL
) {
3675 entry
->object
.vm_object
= vm_object_allocate(size
);
3676 entry
->offset
= (vm_object_offset_t
)0;
3679 vm_map_clip_start(map
, entry
, s
);
3680 vm_map_clip_end(map
, entry
, end
);
3682 /* re-compute "e" */
3688 * Check for holes and protection mismatch.
3689 * Holes: Next entry should be contiguous unless this
3690 * is the end of the region.
3691 * Protection: Access requested must be allowed, unless
3692 * wiring is by protection class
3694 if ((entry
->vme_end
< end
) &&
3695 ((entry
->vme_next
== vm_map_to_entry(map
)) ||
3696 (entry
->vme_next
->vme_start
> entry
->vme_end
))) {
3698 rc
= KERN_INVALID_ADDRESS
;
3701 if ((entry
->protection
& access_type
) != access_type
) {
3702 /* found a protection problem */
3703 rc
= KERN_PROTECTION_FAILURE
;
3707 assert(entry
->wired_count
== 0 && entry
->user_wired_count
== 0);
3709 if ((rc
= add_wire_counts(map
, entry
, user_wire
)) != KERN_SUCCESS
)
3712 entry
->in_transition
= TRUE
;
3715 * This entry might get split once we unlock the map.
3716 * In vm_fault_wire(), we need the current range as
3717 * defined by this entry. In order for this to work
3718 * along with a simultaneous clip operation, we make a
3719 * temporary copy of this entry and use that for the
3720 * wiring. Note that the underlying objects do not
3721 * change during a clip.
3726 * The in_transition state guarentees that the entry
3727 * (or entries for this range, if split occured) will be
3728 * there when the map lock is acquired for the second time.
3732 if (!user_wire
&& cur_thread
!= THREAD_NULL
)
3733 interruptible_state
= thread_interrupt_level(THREAD_UNINT
);
3735 interruptible_state
= THREAD_UNINT
;
3738 rc
= vm_fault_wire(map
,
3739 &tmp_entry
, map_pmap
, pmap_addr
);
3741 rc
= vm_fault_wire(map
,
3742 &tmp_entry
, map
->pmap
,
3743 tmp_entry
.vme_start
);
3745 if (!user_wire
&& cur_thread
!= THREAD_NULL
)
3746 thread_interrupt_level(interruptible_state
);
3750 if (last_timestamp
+1 != map
->timestamp
) {
3752 * Find the entry again. It could have been clipped
3753 * after we unlocked the map.
3755 if (!vm_map_lookup_entry(map
, tmp_entry
.vme_start
,
3757 panic("vm_map_wire: re-lookup failed");
3759 entry
= first_entry
;
3762 last_timestamp
= map
->timestamp
;
3764 while ((entry
!= vm_map_to_entry(map
)) &&
3765 (entry
->vme_start
< tmp_entry
.vme_end
)) {
3766 assert(entry
->in_transition
);
3767 entry
->in_transition
= FALSE
;
3768 if (entry
->needs_wakeup
) {
3769 entry
->needs_wakeup
= FALSE
;
3772 if (rc
!= KERN_SUCCESS
) { /* from vm_*_wire */
3773 subtract_wire_counts(map
, entry
, user_wire
);
3775 entry
= entry
->vme_next
;
3778 if (rc
!= KERN_SUCCESS
) { /* from vm_*_wire */
3782 s
= entry
->vme_start
;
3783 } /* end while loop through map entries */
3786 if (rc
== KERN_SUCCESS
) {
3787 /* repair any damage we may have made to the VM map */
3788 vm_map_simplify_range(map
, start
, end
);
3794 * wake up anybody waiting on entries we wired.
3797 vm_map_entry_wakeup(map
);
3799 if (rc
!= KERN_SUCCESS
) {
3800 /* undo what has been wired so far */
3801 vm_map_unwire(map
, start
, s
, user_wire
);
3810 register vm_map_t map
,
3811 register vm_map_offset_t start
,
3812 register vm_map_offset_t end
,
3813 register vm_prot_t access_type
,
3814 boolean_t user_wire
)
3821 * the calls to mapping_prealloc and mapping_relpre
3822 * (along with the VM_MAP_RANGE_CHECK to insure a
3823 * resonable range was passed in) are
3824 * currently necessary because
3825 * we haven't enabled kernel pre-emption
3826 * and/or the pmap_enter cannot purge and re-use
3829 VM_MAP_RANGE_CHECK(map
, start
, end
);
3830 mapping_prealloc(end
- start
);
3832 kret
= vm_map_wire_nested(map
, start
, end
, access_type
,
3833 user_wire
, (pmap_t
)NULL
, 0);
3843 * Sets the pageability of the specified address range in the target
3844 * as pageable. Regions specified must have been wired previously.
3846 * The map must not be locked, but a reference must remain to the map
3847 * throughout the call.
3849 * Kernel will panic on failures. User unwire ignores holes and
3850 * unwired and intransition entries to avoid losing memory by leaving
3853 static kern_return_t
3854 vm_map_unwire_nested(
3855 register vm_map_t map
,
3856 register vm_map_offset_t start
,
3857 register vm_map_offset_t end
,
3858 boolean_t user_wire
,
3860 vm_map_offset_t pmap_addr
)
3862 register vm_map_entry_t entry
;
3863 struct vm_map_entry
*first_entry
, tmp_entry
;
3864 boolean_t need_wakeup
;
3865 boolean_t main_map
= FALSE
;
3866 unsigned int last_timestamp
;
3869 if(map_pmap
== NULL
)
3871 last_timestamp
= map
->timestamp
;
3873 VM_MAP_RANGE_CHECK(map
, start
, end
);
3874 assert(page_aligned(start
));
3875 assert(page_aligned(end
));
3878 /* We unwired what the caller asked for: zero pages */
3880 return KERN_SUCCESS
;
3883 if (vm_map_lookup_entry(map
, start
, &first_entry
)) {
3884 entry
= first_entry
;
3886 * vm_map_clip_start will be done later.
3887 * We don't want to unnest any nested sub maps here !
3892 panic("vm_map_unwire: start not found");
3894 /* Start address is not in map. */
3896 return(KERN_INVALID_ADDRESS
);
3899 need_wakeup
= FALSE
;
3900 while ((entry
!= vm_map_to_entry(map
)) && (entry
->vme_start
< end
)) {
3901 if (entry
->in_transition
) {
3904 * Another thread is wiring down this entry. Note
3905 * that if it is not for the other thread we would
3906 * be unwiring an unwired entry. This is not
3907 * permitted. If we wait, we will be unwiring memory
3911 * Another thread is unwiring this entry. We did not
3912 * have a reference to it, because if we did, this
3913 * entry will not be getting unwired now.
3918 * This could happen: there could be some
3919 * overlapping vslock/vsunlock operations
3921 * We should probably just wait and retry,
3922 * but then we have to be careful that this
3923 * entry could get "simplified" after
3924 * "in_transition" gets unset and before
3925 * we re-lookup the entry, so we would
3926 * have to re-clip the entry to avoid
3927 * re-unwiring what we have already unwired...
3928 * See vm_map_wire_nested().
3930 * Or we could just ignore "in_transition"
3931 * here and proceed to decement the wired
3932 * count(s) on this entry. That should be fine
3933 * as long as "wired_count" doesn't drop all
3934 * the way to 0 (and we should panic if THAT
3937 panic("vm_map_unwire: in_transition entry");
3940 entry
= entry
->vme_next
;
3944 if (entry
->is_sub_map
) {
3945 vm_map_offset_t sub_start
;
3946 vm_map_offset_t sub_end
;
3947 vm_map_offset_t local_end
;
3950 vm_map_clip_start(map
, entry
, start
);
3951 vm_map_clip_end(map
, entry
, end
);
3953 sub_start
= entry
->offset
;
3954 sub_end
= entry
->vme_end
- entry
->vme_start
;
3955 sub_end
+= entry
->offset
;
3956 local_end
= entry
->vme_end
;
3957 if(map_pmap
== NULL
) {
3958 if(entry
->use_pmap
) {
3959 pmap
= entry
->object
.sub_map
->pmap
;
3960 pmap_addr
= sub_start
;
3965 if (entry
->wired_count
== 0 ||
3966 (user_wire
&& entry
->user_wired_count
== 0)) {
3968 panic("vm_map_unwire: entry is unwired");
3969 entry
= entry
->vme_next
;
3975 * Holes: Next entry should be contiguous unless
3976 * this is the end of the region.
3978 if (((entry
->vme_end
< end
) &&
3979 ((entry
->vme_next
== vm_map_to_entry(map
)) ||
3980 (entry
->vme_next
->vme_start
3981 > entry
->vme_end
)))) {
3983 panic("vm_map_unwire: non-contiguous region");
3985 entry = entry->vme_next;
3990 subtract_wire_counts(map
, entry
, user_wire
);
3992 if (entry
->wired_count
!= 0) {
3993 entry
= entry
->vme_next
;
3997 entry
->in_transition
= TRUE
;
3998 tmp_entry
= *entry
;/* see comment in vm_map_wire() */
4001 * We can unlock the map now. The in_transition state
4002 * guarantees existance of the entry.
4005 vm_map_unwire_nested(entry
->object
.sub_map
,
4006 sub_start
, sub_end
, user_wire
, pmap
, pmap_addr
);
4009 if (last_timestamp
+1 != map
->timestamp
) {
4011 * Find the entry again. It could have been
4012 * clipped or deleted after we unlocked the map.
4014 if (!vm_map_lookup_entry(map
,
4015 tmp_entry
.vme_start
,
4018 panic("vm_map_unwire: re-lookup failed");
4019 entry
= first_entry
->vme_next
;
4021 entry
= first_entry
;
4023 last_timestamp
= map
->timestamp
;
4026 * clear transition bit for all constituent entries
4027 * that were in the original entry (saved in
4028 * tmp_entry). Also check for waiters.
4030 while ((entry
!= vm_map_to_entry(map
)) &&
4031 (entry
->vme_start
< tmp_entry
.vme_end
)) {
4032 assert(entry
->in_transition
);
4033 entry
->in_transition
= FALSE
;
4034 if (entry
->needs_wakeup
) {
4035 entry
->needs_wakeup
= FALSE
;
4038 entry
= entry
->vme_next
;
4043 vm_map_unwire_nested(entry
->object
.sub_map
,
4044 sub_start
, sub_end
, user_wire
, map_pmap
,
4048 if (last_timestamp
+1 != map
->timestamp
) {
4050 * Find the entry again. It could have been
4051 * clipped or deleted after we unlocked the map.
4053 if (!vm_map_lookup_entry(map
,
4054 tmp_entry
.vme_start
,
4057 panic("vm_map_unwire: re-lookup failed");
4058 entry
= first_entry
->vme_next
;
4060 entry
= first_entry
;
4062 last_timestamp
= map
->timestamp
;
4067 if ((entry
->wired_count
== 0) ||
4068 (user_wire
&& entry
->user_wired_count
== 0)) {
4070 panic("vm_map_unwire: entry is unwired");
4072 entry
= entry
->vme_next
;
4076 assert(entry
->wired_count
> 0 &&
4077 (!user_wire
|| entry
->user_wired_count
> 0));
4079 vm_map_clip_start(map
, entry
, start
);
4080 vm_map_clip_end(map
, entry
, end
);
4084 * Holes: Next entry should be contiguous unless
4085 * this is the end of the region.
4087 if (((entry
->vme_end
< end
) &&
4088 ((entry
->vme_next
== vm_map_to_entry(map
)) ||
4089 (entry
->vme_next
->vme_start
> entry
->vme_end
)))) {
4092 panic("vm_map_unwire: non-contiguous region");
4093 entry
= entry
->vme_next
;
4097 subtract_wire_counts(map
, entry
, user_wire
);
4099 if (entry
->wired_count
!= 0) {
4100 entry
= entry
->vme_next
;
4104 entry
->in_transition
= TRUE
;
4105 tmp_entry
= *entry
; /* see comment in vm_map_wire() */
4108 * We can unlock the map now. The in_transition state
4109 * guarantees existance of the entry.
4113 vm_fault_unwire(map
,
4114 &tmp_entry
, FALSE
, map_pmap
, pmap_addr
);
4116 vm_fault_unwire(map
,
4117 &tmp_entry
, FALSE
, map
->pmap
,
4118 tmp_entry
.vme_start
);
4122 if (last_timestamp
+1 != map
->timestamp
) {
4124 * Find the entry again. It could have been clipped
4125 * or deleted after we unlocked the map.
4127 if (!vm_map_lookup_entry(map
, tmp_entry
.vme_start
,
4130 panic("vm_map_unwire: re-lookup failed");
4131 entry
= first_entry
->vme_next
;
4133 entry
= first_entry
;
4135 last_timestamp
= map
->timestamp
;
4138 * clear transition bit for all constituent entries that
4139 * were in the original entry (saved in tmp_entry). Also
4140 * check for waiters.
4142 while ((entry
!= vm_map_to_entry(map
)) &&
4143 (entry
->vme_start
< tmp_entry
.vme_end
)) {
4144 assert(entry
->in_transition
);
4145 entry
->in_transition
= FALSE
;
4146 if (entry
->needs_wakeup
) {
4147 entry
->needs_wakeup
= FALSE
;
4150 entry
= entry
->vme_next
;
4155 * We might have fragmented the address space when we wired this
4156 * range of addresses. Attempt to re-coalesce these VM map entries
4157 * with their neighbors now that they're no longer wired.
4158 * Under some circumstances, address space fragmentation can
4159 * prevent VM object shadow chain collapsing, which can cause
4162 vm_map_simplify_range(map
, start
, end
);
4166 * wake up anybody waiting on entries that we have unwired.
4169 vm_map_entry_wakeup(map
);
4170 return(KERN_SUCCESS
);
4176 register vm_map_t map
,
4177 register vm_map_offset_t start
,
4178 register vm_map_offset_t end
,
4179 boolean_t user_wire
)
4181 return vm_map_unwire_nested(map
, start
, end
,
4182 user_wire
, (pmap_t
)NULL
, 0);
4187 * vm_map_entry_delete: [ internal use only ]
4189 * Deallocate the given entry from the target map.
4192 vm_map_entry_delete(
4193 register vm_map_t map
,
4194 register vm_map_entry_t entry
)
4196 register vm_map_offset_t s
, e
;
4197 register vm_object_t object
;
4198 register vm_map_t submap
;
4200 s
= entry
->vme_start
;
4202 assert(page_aligned(s
));
4203 assert(page_aligned(e
));
4204 assert(entry
->wired_count
== 0);
4205 assert(entry
->user_wired_count
== 0);
4207 if (entry
->is_sub_map
) {
4209 submap
= entry
->object
.sub_map
;
4212 object
= entry
->object
.vm_object
;
4215 vm_map_entry_unlink(map
, entry
);
4218 vm_map_entry_dispose(map
, entry
);
4222 * Deallocate the object only after removing all
4223 * pmap entries pointing to its pages.
4226 vm_map_deallocate(submap
);
4228 vm_object_deallocate(object
);
4233 vm_map_submap_pmap_clean(
4235 vm_map_offset_t start
,
4236 vm_map_offset_t end
,
4238 vm_map_offset_t offset
)
4240 vm_map_offset_t submap_start
;
4241 vm_map_offset_t submap_end
;
4242 vm_map_size_t remove_size
;
4243 vm_map_entry_t entry
;
4245 submap_end
= offset
+ (end
- start
);
4246 submap_start
= offset
;
4247 if(vm_map_lookup_entry(sub_map
, offset
, &entry
)) {
4249 remove_size
= (entry
->vme_end
- entry
->vme_start
);
4250 if(offset
> entry
->vme_start
)
4251 remove_size
-= offset
- entry
->vme_start
;
4254 if(submap_end
< entry
->vme_end
) {
4256 entry
->vme_end
- submap_end
;
4258 if(entry
->is_sub_map
) {
4259 vm_map_submap_pmap_clean(
4262 start
+ remove_size
,
4263 entry
->object
.sub_map
,
4267 if((map
->mapped
) && (map
->ref_count
)
4268 && (entry
->object
.vm_object
!= NULL
)) {
4269 vm_object_pmap_protect(
4270 entry
->object
.vm_object
,
4277 pmap_remove(map
->pmap
,
4279 (addr64_t
)(start
+ remove_size
));
4284 entry
= entry
->vme_next
;
4286 while((entry
!= vm_map_to_entry(sub_map
))
4287 && (entry
->vme_start
< submap_end
)) {
4288 remove_size
= (entry
->vme_end
- entry
->vme_start
);
4289 if(submap_end
< entry
->vme_end
) {
4290 remove_size
-= entry
->vme_end
- submap_end
;
4292 if(entry
->is_sub_map
) {
4293 vm_map_submap_pmap_clean(
4295 (start
+ entry
->vme_start
) - offset
,
4296 ((start
+ entry
->vme_start
) - offset
) + remove_size
,
4297 entry
->object
.sub_map
,
4300 if((map
->mapped
) && (map
->ref_count
)
4301 && (entry
->object
.vm_object
!= NULL
)) {
4302 vm_object_pmap_protect(
4303 entry
->object
.vm_object
,
4310 pmap_remove(map
->pmap
,
4311 (addr64_t
)((start
+ entry
->vme_start
)
4313 (addr64_t
)(((start
+ entry
->vme_start
)
4314 - offset
) + remove_size
));
4317 entry
= entry
->vme_next
;
4323 * vm_map_delete: [ internal use only ]
4325 * Deallocates the given address range from the target map.
4326 * Removes all user wirings. Unwires one kernel wiring if
4327 * VM_MAP_REMOVE_KUNWIRE is set. Waits for kernel wirings to go
4328 * away if VM_MAP_REMOVE_WAIT_FOR_KWIRE is set. Sleeps
4329 * interruptibly if VM_MAP_REMOVE_INTERRUPTIBLE is set.
4331 * This routine is called with map locked and leaves map locked.
4333 static kern_return_t
4336 vm_map_offset_t start
,
4337 vm_map_offset_t end
,
4341 vm_map_entry_t entry
, next
;
4342 struct vm_map_entry
*first_entry
, tmp_entry
;
4343 register vm_map_offset_t s
;
4344 register vm_object_t object
;
4345 boolean_t need_wakeup
;
4346 unsigned int last_timestamp
= ~0; /* unlikely value */
4349 interruptible
= (flags
& VM_MAP_REMOVE_INTERRUPTIBLE
) ?
4350 THREAD_ABORTSAFE
: THREAD_UNINT
;
4353 * All our DMA I/O operations in IOKit are currently done by
4354 * wiring through the map entries of the task requesting the I/O.
4355 * Because of this, we must always wait for kernel wirings
4356 * to go away on the entries before deleting them.
4358 * Any caller who wants to actually remove a kernel wiring
4359 * should explicitly set the VM_MAP_REMOVE_KUNWIRE flag to
4360 * properly remove one wiring instead of blasting through
4363 flags
|= VM_MAP_REMOVE_WAIT_FOR_KWIRE
;
4366 * Find the start of the region, and clip it
4368 if (vm_map_lookup_entry(map
, start
, &first_entry
)) {
4369 entry
= first_entry
;
4370 if (start
== entry
->vme_start
) {
4372 * No need to clip. We don't want to cause
4373 * any unnecessary unnesting in this case...
4376 vm_map_clip_start(map
, entry
, start
);
4380 * Fix the lookup hint now, rather than each
4381 * time through the loop.
4383 SAVE_HINT_MAP_WRITE(map
, entry
->vme_prev
);
4385 entry
= first_entry
->vme_next
;
4388 need_wakeup
= FALSE
;
4390 * Step through all entries in this region
4392 s
= entry
->vme_start
;
4393 while ((entry
!= vm_map_to_entry(map
)) && (s
< end
)) {
4395 * At this point, we have deleted all the memory entries
4396 * between "start" and "s". We still need to delete
4397 * all memory entries between "s" and "end".
4398 * While we were blocked and the map was unlocked, some
4399 * new memory entries could have been re-allocated between
4400 * "start" and "s" and we don't want to mess with those.
4401 * Some of those entries could even have been re-assembled
4402 * with an entry after "s" (in vm_map_simplify_entry()), so
4403 * we may have to vm_map_clip_start() again.
4406 if (entry
->vme_start
>= s
) {
4408 * This entry starts on or after "s"
4409 * so no need to clip its start.
4413 * This entry has been re-assembled by a
4414 * vm_map_simplify_entry(). We need to
4415 * re-clip its start.
4417 vm_map_clip_start(map
, entry
, s
);
4419 if (entry
->vme_end
<= end
) {
4421 * This entry is going away completely, so no need
4422 * to clip and possibly cause an unnecessary unnesting.
4425 vm_map_clip_end(map
, entry
, end
);
4427 if (entry
->in_transition
) {
4428 wait_result_t wait_result
;
4431 * Another thread is wiring/unwiring this entry.
4432 * Let the other thread know we are waiting.
4434 assert(s
== entry
->vme_start
);
4435 entry
->needs_wakeup
= TRUE
;
4438 * wake up anybody waiting on entries that we have
4439 * already unwired/deleted.
4442 vm_map_entry_wakeup(map
);
4443 need_wakeup
= FALSE
;
4446 wait_result
= vm_map_entry_wait(map
, interruptible
);
4448 if (interruptible
&&
4449 wait_result
== THREAD_INTERRUPTED
) {
4451 * We do not clear the needs_wakeup flag,
4452 * since we cannot tell if we were the only one.
4455 return KERN_ABORTED
;
4459 * The entry could have been clipped or it
4460 * may not exist anymore. Look it up again.
4462 if (!vm_map_lookup_entry(map
, s
, &first_entry
)) {
4463 assert((map
!= kernel_map
) &&
4464 (!entry
->is_sub_map
));
4466 * User: use the next entry
4468 entry
= first_entry
->vme_next
;
4469 s
= entry
->vme_start
;
4471 entry
= first_entry
;
4472 SAVE_HINT_MAP_WRITE(map
, entry
->vme_prev
);
4474 last_timestamp
= map
->timestamp
;
4476 } /* end in_transition */
4478 if (entry
->wired_count
) {
4479 boolean_t user_wire
;
4481 user_wire
= entry
->user_wired_count
> 0;
4484 * Remove a kernel wiring if requested or if
4485 * there are user wirings.
4487 if ((flags
& VM_MAP_REMOVE_KUNWIRE
) ||
4488 (entry
->user_wired_count
> 0))
4489 entry
->wired_count
--;
4491 /* remove all user wire references */
4492 entry
->user_wired_count
= 0;
4494 if (entry
->wired_count
!= 0) {
4495 assert(map
!= kernel_map
);
4497 * Cannot continue. Typical case is when
4498 * a user thread has physical io pending on
4499 * on this page. Either wait for the
4500 * kernel wiring to go away or return an
4503 if (flags
& VM_MAP_REMOVE_WAIT_FOR_KWIRE
) {
4504 wait_result_t wait_result
;
4506 assert(s
== entry
->vme_start
);
4507 entry
->needs_wakeup
= TRUE
;
4508 wait_result
= vm_map_entry_wait(map
,
4511 if (interruptible
&&
4512 wait_result
== THREAD_INTERRUPTED
) {
4514 * We do not clear the
4515 * needs_wakeup flag, since we
4516 * cannot tell if we were the
4520 return KERN_ABORTED
;
4524 * The entry could have been clipped or
4525 * it may not exist anymore. Look it
4528 if (!vm_map_lookup_entry(map
, s
,
4530 assert(map
!= kernel_map
);
4532 * User: use the next entry
4534 entry
= first_entry
->vme_next
;
4535 s
= entry
->vme_start
;
4537 entry
= first_entry
;
4538 SAVE_HINT_MAP_WRITE(map
, entry
->vme_prev
);
4540 last_timestamp
= map
->timestamp
;
4544 return KERN_FAILURE
;
4548 entry
->in_transition
= TRUE
;
4550 * copy current entry. see comment in vm_map_wire()
4553 assert(s
== entry
->vme_start
);
4556 * We can unlock the map now. The in_transition
4557 * state guarentees existance of the entry.
4561 if (tmp_entry
.is_sub_map
) {
4563 vm_map_offset_t sub_start
, sub_end
;
4565 vm_map_offset_t pmap_addr
;
4568 sub_map
= tmp_entry
.object
.sub_map
;
4569 sub_start
= tmp_entry
.offset
;
4570 sub_end
= sub_start
+ (tmp_entry
.vme_end
-
4571 tmp_entry
.vme_start
);
4572 if (tmp_entry
.use_pmap
) {
4573 pmap
= sub_map
->pmap
;
4574 pmap_addr
= tmp_entry
.vme_start
;
4577 pmap_addr
= tmp_entry
.vme_start
;
4579 (void) vm_map_unwire_nested(sub_map
,
4585 vm_fault_unwire(map
, &tmp_entry
,
4586 tmp_entry
.object
.vm_object
== kernel_object
,
4587 map
->pmap
, tmp_entry
.vme_start
);
4592 if (last_timestamp
+1 != map
->timestamp
) {
4594 * Find the entry again. It could have
4595 * been clipped after we unlocked the map.
4597 if (!vm_map_lookup_entry(map
, s
, &first_entry
)){
4598 assert((map
!= kernel_map
) &&
4599 (!entry
->is_sub_map
));
4600 first_entry
= first_entry
->vme_next
;
4601 s
= first_entry
->vme_start
;
4603 SAVE_HINT_MAP_WRITE(map
, entry
->vme_prev
);
4606 SAVE_HINT_MAP_WRITE(map
, entry
->vme_prev
);
4607 first_entry
= entry
;
4610 last_timestamp
= map
->timestamp
;
4612 entry
= first_entry
;
4613 while ((entry
!= vm_map_to_entry(map
)) &&
4614 (entry
->vme_start
< tmp_entry
.vme_end
)) {
4615 assert(entry
->in_transition
);
4616 entry
->in_transition
= FALSE
;
4617 if (entry
->needs_wakeup
) {
4618 entry
->needs_wakeup
= FALSE
;
4621 entry
= entry
->vme_next
;
4624 * We have unwired the entry(s). Go back and
4627 entry
= first_entry
;
4631 /* entry is unwired */
4632 assert(entry
->wired_count
== 0);
4633 assert(entry
->user_wired_count
== 0);
4635 assert(s
== entry
->vme_start
);
4637 if (flags
& VM_MAP_REMOVE_NO_PMAP_CLEANUP
) {
4639 * XXX with the VM_MAP_REMOVE_SAVE_ENTRIES flag to
4640 * vm_map_delete(), some map entries might have been
4641 * transferred to a "zap_map", which doesn't have a
4642 * pmap. The original pmap has already been flushed
4643 * in the vm_map_delete() call targeting the original
4644 * map, but when we get to destroying the "zap_map",
4645 * we don't have any pmap to flush, so let's just skip
4648 } else if (entry
->is_sub_map
) {
4649 if (entry
->use_pmap
) {
4650 #ifndef NO_NESTED_PMAP
4651 pmap_unnest(map
->pmap
,
4652 (addr64_t
)entry
->vme_start
,
4653 entry
->vme_end
- entry
->vme_start
);
4654 #endif /* NO_NESTED_PMAP */
4655 if ((map
->mapped
) && (map
->ref_count
)) {
4656 /* clean up parent map/maps */
4657 vm_map_submap_pmap_clean(
4658 map
, entry
->vme_start
,
4660 entry
->object
.sub_map
,
4664 vm_map_submap_pmap_clean(
4665 map
, entry
->vme_start
, entry
->vme_end
,
4666 entry
->object
.sub_map
,
4669 } else if (entry
->object
.vm_object
!= kernel_object
) {
4670 object
= entry
->object
.vm_object
;
4671 if((map
->mapped
) && (map
->ref_count
)) {
4672 vm_object_pmap_protect(
4673 object
, entry
->offset
,
4674 entry
->vme_end
- entry
->vme_start
,
4679 pmap_remove(map
->pmap
,
4680 (addr64_t
)entry
->vme_start
,
4681 (addr64_t
)entry
->vme_end
);
4686 * All pmap mappings for this map entry must have been
4689 assert(vm_map_pmap_is_empty(map
,
4693 next
= entry
->vme_next
;
4694 s
= next
->vme_start
;
4695 last_timestamp
= map
->timestamp
;
4697 if ((flags
& VM_MAP_REMOVE_SAVE_ENTRIES
) &&
4698 zap_map
!= VM_MAP_NULL
) {
4699 vm_map_size_t entry_size
;
4701 * The caller wants to save the affected VM map entries
4702 * into the "zap_map". The caller will take care of
4705 /* unlink the entry from "map" ... */
4706 vm_map_entry_unlink(map
, entry
);
4707 /* ... and add it to the end of the "zap_map" */
4708 vm_map_entry_link(zap_map
,
4709 vm_map_last_entry(zap_map
),
4711 entry_size
= entry
->vme_end
- entry
->vme_start
;
4712 map
->size
-= entry_size
;
4713 zap_map
->size
+= entry_size
;
4714 /* we didn't unlock the map, so no timestamp increase */
4717 vm_map_entry_delete(map
, entry
);
4718 /* vm_map_entry_delete unlocks the map */
4724 if(entry
== vm_map_to_entry(map
)) {
4727 if (last_timestamp
+1 != map
->timestamp
) {
4729 * we are responsible for deleting everything
4730 * from the give space, if someone has interfered
4731 * we pick up where we left off, back fills should
4732 * be all right for anyone except map_delete and
4733 * we have to assume that the task has been fully
4734 * disabled before we get here
4736 if (!vm_map_lookup_entry(map
, s
, &entry
)){
4737 entry
= entry
->vme_next
;
4738 s
= entry
->vme_start
;
4740 SAVE_HINT_MAP_WRITE(map
, entry
->vme_prev
);
4743 * others can not only allocate behind us, we can
4744 * also see coalesce while we don't have the map lock
4746 if(entry
== vm_map_to_entry(map
)) {
4750 last_timestamp
= map
->timestamp
;
4753 if (map
->wait_for_space
)
4754 thread_wakeup((event_t
) map
);
4756 * wake up anybody waiting on entries that we have already deleted.
4759 vm_map_entry_wakeup(map
);
4761 return KERN_SUCCESS
;
4767 * Remove the given address range from the target map.
4768 * This is the exported form of vm_map_delete.
4772 register vm_map_t map
,
4773 register vm_map_offset_t start
,
4774 register vm_map_offset_t end
,
4775 register boolean_t flags
)
4777 register kern_return_t result
;
4780 VM_MAP_RANGE_CHECK(map
, start
, end
);
4781 result
= vm_map_delete(map
, start
, end
, flags
, VM_MAP_NULL
);
4789 * Routine: vm_map_copy_discard
4792 * Dispose of a map copy object (returned by
4796 vm_map_copy_discard(
4799 TR_DECL("vm_map_copy_discard");
4801 /* tr3("enter: copy 0x%x type %d", copy, copy->type);*/
4803 if (copy
== VM_MAP_COPY_NULL
)
4806 switch (copy
->type
) {
4807 case VM_MAP_COPY_ENTRY_LIST
:
4808 while (vm_map_copy_first_entry(copy
) !=
4809 vm_map_copy_to_entry(copy
)) {
4810 vm_map_entry_t entry
= vm_map_copy_first_entry(copy
);
4812 vm_map_copy_entry_unlink(copy
, entry
);
4813 vm_object_deallocate(entry
->object
.vm_object
);
4814 vm_map_copy_entry_dispose(copy
, entry
);
4817 case VM_MAP_COPY_OBJECT
:
4818 vm_object_deallocate(copy
->cpy_object
);
4820 case VM_MAP_COPY_KERNEL_BUFFER
:
4823 * The vm_map_copy_t and possibly the data buffer were
4824 * allocated by a single call to kalloc(), i.e. the
4825 * vm_map_copy_t was not allocated out of the zone.
4827 kfree(copy
, copy
->cpy_kalloc_size
);
4830 zfree(vm_map_copy_zone
, copy
);
4834 * Routine: vm_map_copy_copy
4837 * Move the information in a map copy object to
4838 * a new map copy object, leaving the old one
4841 * This is used by kernel routines that need
4842 * to look at out-of-line data (in copyin form)
4843 * before deciding whether to return SUCCESS.
4844 * If the routine returns FAILURE, the original
4845 * copy object will be deallocated; therefore,
4846 * these routines must make a copy of the copy
4847 * object and leave the original empty so that
4848 * deallocation will not fail.
4854 vm_map_copy_t new_copy
;
4856 if (copy
== VM_MAP_COPY_NULL
)
4857 return VM_MAP_COPY_NULL
;
4860 * Allocate a new copy object, and copy the information
4861 * from the old one into it.
4864 new_copy
= (vm_map_copy_t
) zalloc(vm_map_copy_zone
);
4867 if (copy
->type
== VM_MAP_COPY_ENTRY_LIST
) {
4869 * The links in the entry chain must be
4870 * changed to point to the new copy object.
4872 vm_map_copy_first_entry(copy
)->vme_prev
4873 = vm_map_copy_to_entry(new_copy
);
4874 vm_map_copy_last_entry(copy
)->vme_next
4875 = vm_map_copy_to_entry(new_copy
);
4879 * Change the old copy object into one that contains
4880 * nothing to be deallocated.
4882 copy
->type
= VM_MAP_COPY_OBJECT
;
4883 copy
->cpy_object
= VM_OBJECT_NULL
;
4886 * Return the new object.
4891 static kern_return_t
4892 vm_map_overwrite_submap_recurse(
4894 vm_map_offset_t dst_addr
,
4895 vm_map_size_t dst_size
)
4897 vm_map_offset_t dst_end
;
4898 vm_map_entry_t tmp_entry
;
4899 vm_map_entry_t entry
;
4900 kern_return_t result
;
4901 boolean_t encountered_sub_map
= FALSE
;
4906 * Verify that the destination is all writeable
4907 * initially. We have to trunc the destination
4908 * address and round the copy size or we'll end up
4909 * splitting entries in strange ways.
4912 dst_end
= vm_map_round_page(dst_addr
+ dst_size
);
4913 vm_map_lock(dst_map
);
4916 if (!vm_map_lookup_entry(dst_map
, dst_addr
, &tmp_entry
)) {
4917 vm_map_unlock(dst_map
);
4918 return(KERN_INVALID_ADDRESS
);
4921 vm_map_clip_start(dst_map
, tmp_entry
, vm_map_trunc_page(dst_addr
));
4922 assert(!tmp_entry
->use_pmap
); /* clipping did unnest if needed */
4924 for (entry
= tmp_entry
;;) {
4925 vm_map_entry_t next
;
4927 next
= entry
->vme_next
;
4928 while(entry
->is_sub_map
) {
4929 vm_map_offset_t sub_start
;
4930 vm_map_offset_t sub_end
;
4931 vm_map_offset_t local_end
;
4933 if (entry
->in_transition
) {
4935 * Say that we are waiting, and wait for entry.
4937 entry
->needs_wakeup
= TRUE
;
4938 vm_map_entry_wait(dst_map
, THREAD_UNINT
);
4943 encountered_sub_map
= TRUE
;
4944 sub_start
= entry
->offset
;
4946 if(entry
->vme_end
< dst_end
)
4947 sub_end
= entry
->vme_end
;
4950 sub_end
-= entry
->vme_start
;
4951 sub_end
+= entry
->offset
;
4952 local_end
= entry
->vme_end
;
4953 vm_map_unlock(dst_map
);
4955 result
= vm_map_overwrite_submap_recurse(
4956 entry
->object
.sub_map
,
4958 sub_end
- sub_start
);
4960 if(result
!= KERN_SUCCESS
)
4962 if (dst_end
<= entry
->vme_end
)
4963 return KERN_SUCCESS
;
4964 vm_map_lock(dst_map
);
4965 if(!vm_map_lookup_entry(dst_map
, local_end
,
4967 vm_map_unlock(dst_map
);
4968 return(KERN_INVALID_ADDRESS
);
4971 next
= entry
->vme_next
;
4974 if ( ! (entry
->protection
& VM_PROT_WRITE
)) {
4975 vm_map_unlock(dst_map
);
4976 return(KERN_PROTECTION_FAILURE
);
4980 * If the entry is in transition, we must wait
4981 * for it to exit that state. Anything could happen
4982 * when we unlock the map, so start over.
4984 if (entry
->in_transition
) {
4987 * Say that we are waiting, and wait for entry.
4989 entry
->needs_wakeup
= TRUE
;
4990 vm_map_entry_wait(dst_map
, THREAD_UNINT
);
4996 * our range is contained completely within this map entry
4998 if (dst_end
<= entry
->vme_end
) {
4999 vm_map_unlock(dst_map
);
5000 return KERN_SUCCESS
;
5003 * check that range specified is contiguous region
5005 if ((next
== vm_map_to_entry(dst_map
)) ||
5006 (next
->vme_start
!= entry
->vme_end
)) {
5007 vm_map_unlock(dst_map
);
5008 return(KERN_INVALID_ADDRESS
);
5012 * Check for permanent objects in the destination.
5014 if ((entry
->object
.vm_object
!= VM_OBJECT_NULL
) &&
5015 ((!entry
->object
.vm_object
->internal
) ||
5016 (entry
->object
.vm_object
->true_share
))) {
5017 if(encountered_sub_map
) {
5018 vm_map_unlock(dst_map
);
5019 return(KERN_FAILURE
);
5026 vm_map_unlock(dst_map
);
5027 return(KERN_SUCCESS
);
5031 * Routine: vm_map_copy_overwrite
5034 * Copy the memory described by the map copy
5035 * object (copy; returned by vm_map_copyin) onto
5036 * the specified destination region (dst_map, dst_addr).
5037 * The destination must be writeable.
5039 * Unlike vm_map_copyout, this routine actually
5040 * writes over previously-mapped memory. If the
5041 * previous mapping was to a permanent (user-supplied)
5042 * memory object, it is preserved.
5044 * The attributes (protection and inheritance) of the
5045 * destination region are preserved.
5047 * If successful, consumes the copy object.
5048 * Otherwise, the caller is responsible for it.
5050 * Implementation notes:
5051 * To overwrite aligned temporary virtual memory, it is
5052 * sufficient to remove the previous mapping and insert
5053 * the new copy. This replacement is done either on
5054 * the whole region (if no permanent virtual memory
5055 * objects are embedded in the destination region) or
5056 * in individual map entries.
5058 * To overwrite permanent virtual memory , it is necessary
5059 * to copy each page, as the external memory management
5060 * interface currently does not provide any optimizations.
5062 * Unaligned memory also has to be copied. It is possible
5063 * to use 'vm_trickery' to copy the aligned data. This is
5064 * not done but not hard to implement.
5066 * Once a page of permanent memory has been overwritten,
5067 * it is impossible to interrupt this function; otherwise,
5068 * the call would be neither atomic nor location-independent.
5069 * The kernel-state portion of a user thread must be
5072 * It may be expensive to forward all requests that might
5073 * overwrite permanent memory (vm_write, vm_copy) to
5074 * uninterruptible kernel threads. This routine may be
5075 * called by interruptible threads; however, success is
5076 * not guaranteed -- if the request cannot be performed
5077 * atomically and interruptibly, an error indication is
5081 static kern_return_t
5082 vm_map_copy_overwrite_nested(
5084 vm_map_address_t dst_addr
,
5086 boolean_t interruptible
,
5089 vm_map_offset_t dst_end
;
5090 vm_map_entry_t tmp_entry
;
5091 vm_map_entry_t entry
;
5093 boolean_t aligned
= TRUE
;
5094 boolean_t contains_permanent_objects
= FALSE
;
5095 boolean_t encountered_sub_map
= FALSE
;
5096 vm_map_offset_t base_addr
;
5097 vm_map_size_t copy_size
;
5098 vm_map_size_t total_size
;
5102 * Check for null copy object.
5105 if (copy
== VM_MAP_COPY_NULL
)
5106 return(KERN_SUCCESS
);
5109 * Check for special kernel buffer allocated
5110 * by new_ipc_kmsg_copyin.
5113 if (copy
->type
== VM_MAP_COPY_KERNEL_BUFFER
) {
5114 return(vm_map_copyout_kernel_buffer(
5120 * Only works for entry lists at the moment. Will
5121 * support page lists later.
5124 assert(copy
->type
== VM_MAP_COPY_ENTRY_LIST
);
5126 if (copy
->size
== 0) {
5127 vm_map_copy_discard(copy
);
5128 return(KERN_SUCCESS
);
5132 * Verify that the destination is all writeable
5133 * initially. We have to trunc the destination
5134 * address and round the copy size or we'll end up
5135 * splitting entries in strange ways.
5138 if (!page_aligned(copy
->size
) ||
5139 !page_aligned (copy
->offset
) ||
5140 !page_aligned (dst_addr
))
5143 dst_end
= vm_map_round_page(dst_addr
+ copy
->size
);
5145 dst_end
= dst_addr
+ copy
->size
;
5148 vm_map_lock(dst_map
);
5150 /* LP64todo - remove this check when vm_map_commpage64()
5151 * no longer has to stuff in a map_entry for the commpage
5152 * above the map's max_offset.
5154 if (dst_addr
>= dst_map
->max_offset
) {
5155 vm_map_unlock(dst_map
);
5156 return(KERN_INVALID_ADDRESS
);
5160 if (!vm_map_lookup_entry(dst_map
, dst_addr
, &tmp_entry
)) {
5161 vm_map_unlock(dst_map
);
5162 return(KERN_INVALID_ADDRESS
);
5164 vm_map_clip_start(dst_map
, tmp_entry
, vm_map_trunc_page(dst_addr
));
5165 for (entry
= tmp_entry
;;) {
5166 vm_map_entry_t next
= entry
->vme_next
;
5168 while(entry
->is_sub_map
) {
5169 vm_map_offset_t sub_start
;
5170 vm_map_offset_t sub_end
;
5171 vm_map_offset_t local_end
;
5173 if (entry
->in_transition
) {
5176 * Say that we are waiting, and wait for entry.
5178 entry
->needs_wakeup
= TRUE
;
5179 vm_map_entry_wait(dst_map
, THREAD_UNINT
);
5184 local_end
= entry
->vme_end
;
5185 if (!(entry
->needs_copy
)) {
5186 /* if needs_copy we are a COW submap */
5187 /* in such a case we just replace so */
5188 /* there is no need for the follow- */
5190 encountered_sub_map
= TRUE
;
5191 sub_start
= entry
->offset
;
5193 if(entry
->vme_end
< dst_end
)
5194 sub_end
= entry
->vme_end
;
5197 sub_end
-= entry
->vme_start
;
5198 sub_end
+= entry
->offset
;
5199 vm_map_unlock(dst_map
);
5201 kr
= vm_map_overwrite_submap_recurse(
5202 entry
->object
.sub_map
,
5204 sub_end
- sub_start
);
5205 if(kr
!= KERN_SUCCESS
)
5207 vm_map_lock(dst_map
);
5210 if (dst_end
<= entry
->vme_end
)
5211 goto start_overwrite
;
5212 if(!vm_map_lookup_entry(dst_map
, local_end
,
5214 vm_map_unlock(dst_map
);
5215 return(KERN_INVALID_ADDRESS
);
5217 next
= entry
->vme_next
;
5220 if ( ! (entry
->protection
& VM_PROT_WRITE
)) {
5221 vm_map_unlock(dst_map
);
5222 return(KERN_PROTECTION_FAILURE
);
5226 * If the entry is in transition, we must wait
5227 * for it to exit that state. Anything could happen
5228 * when we unlock the map, so start over.
5230 if (entry
->in_transition
) {
5233 * Say that we are waiting, and wait for entry.
5235 entry
->needs_wakeup
= TRUE
;
5236 vm_map_entry_wait(dst_map
, THREAD_UNINT
);
5242 * our range is contained completely within this map entry
5244 if (dst_end
<= entry
->vme_end
)
5247 * check that range specified is contiguous region
5249 if ((next
== vm_map_to_entry(dst_map
)) ||
5250 (next
->vme_start
!= entry
->vme_end
)) {
5251 vm_map_unlock(dst_map
);
5252 return(KERN_INVALID_ADDRESS
);
5257 * Check for permanent objects in the destination.
5259 if ((entry
->object
.vm_object
!= VM_OBJECT_NULL
) &&
5260 ((!entry
->object
.vm_object
->internal
) ||
5261 (entry
->object
.vm_object
->true_share
))) {
5262 contains_permanent_objects
= TRUE
;
5270 * If there are permanent objects in the destination, then
5271 * the copy cannot be interrupted.
5274 if (interruptible
&& contains_permanent_objects
) {
5275 vm_map_unlock(dst_map
);
5276 return(KERN_FAILURE
); /* XXX */
5281 * Make a second pass, overwriting the data
5282 * At the beginning of each loop iteration,
5283 * the next entry to be overwritten is "tmp_entry"
5284 * (initially, the value returned from the lookup above),
5285 * and the starting address expected in that entry
5289 total_size
= copy
->size
;
5290 if(encountered_sub_map
) {
5292 /* re-calculate tmp_entry since we've had the map */
5294 if (!vm_map_lookup_entry( dst_map
, dst_addr
, &tmp_entry
)) {
5295 vm_map_unlock(dst_map
);
5296 return(KERN_INVALID_ADDRESS
);
5299 copy_size
= copy
->size
;
5302 base_addr
= dst_addr
;
5304 /* deconstruct the copy object and do in parts */
5305 /* only in sub_map, interruptable case */
5306 vm_map_entry_t copy_entry
;
5307 vm_map_entry_t previous_prev
= VM_MAP_ENTRY_NULL
;
5308 vm_map_entry_t next_copy
= VM_MAP_ENTRY_NULL
;
5310 int remaining_entries
= 0;
5313 for (entry
= tmp_entry
; copy_size
== 0;) {
5314 vm_map_entry_t next
;
5316 next
= entry
->vme_next
;
5318 /* tmp_entry and base address are moved along */
5319 /* each time we encounter a sub-map. Otherwise */
5320 /* entry can outpase tmp_entry, and the copy_size */
5321 /* may reflect the distance between them */
5322 /* if the current entry is found to be in transition */
5323 /* we will start over at the beginning or the last */
5324 /* encounter of a submap as dictated by base_addr */
5325 /* we will zero copy_size accordingly. */
5326 if (entry
->in_transition
) {
5328 * Say that we are waiting, and wait for entry.
5330 entry
->needs_wakeup
= TRUE
;
5331 vm_map_entry_wait(dst_map
, THREAD_UNINT
);
5333 if(!vm_map_lookup_entry(dst_map
, base_addr
,
5335 vm_map_unlock(dst_map
);
5336 return(KERN_INVALID_ADDRESS
);
5342 if(entry
->is_sub_map
) {
5343 vm_map_offset_t sub_start
;
5344 vm_map_offset_t sub_end
;
5345 vm_map_offset_t local_end
;
5347 if (entry
->needs_copy
) {
5348 /* if this is a COW submap */
5349 /* just back the range with a */
5350 /* anonymous entry */
5351 if(entry
->vme_end
< dst_end
)
5352 sub_end
= entry
->vme_end
;
5355 if(entry
->vme_start
< base_addr
)
5356 sub_start
= base_addr
;
5358 sub_start
= entry
->vme_start
;
5360 dst_map
, entry
, sub_end
);
5362 dst_map
, entry
, sub_start
);
5363 assert(!entry
->use_pmap
);
5364 entry
->is_sub_map
= FALSE
;
5366 entry
->object
.sub_map
);
5367 entry
->object
.sub_map
= NULL
;
5368 entry
->is_shared
= FALSE
;
5369 entry
->needs_copy
= FALSE
;
5373 * We should propagate the protections
5374 * of the submap entry here instead
5375 * of forcing them to VM_PROT_ALL...
5376 * Or better yet, we should inherit
5377 * the protection of the copy_entry.
5379 entry
->protection
= VM_PROT_ALL
;
5380 entry
->max_protection
= VM_PROT_ALL
;
5381 entry
->wired_count
= 0;
5382 entry
->user_wired_count
= 0;
5383 if(entry
->inheritance
5384 == VM_INHERIT_SHARE
)
5385 entry
->inheritance
= VM_INHERIT_COPY
;
5388 /* first take care of any non-sub_map */
5389 /* entries to send */
5390 if(base_addr
< entry
->vme_start
) {
5393 entry
->vme_start
- base_addr
;
5396 sub_start
= entry
->offset
;
5398 if(entry
->vme_end
< dst_end
)
5399 sub_end
= entry
->vme_end
;
5402 sub_end
-= entry
->vme_start
;
5403 sub_end
+= entry
->offset
;
5404 local_end
= entry
->vme_end
;
5405 vm_map_unlock(dst_map
);
5406 copy_size
= sub_end
- sub_start
;
5408 /* adjust the copy object */
5409 if (total_size
> copy_size
) {
5410 vm_map_size_t local_size
= 0;
5411 vm_map_size_t entry_size
;
5414 new_offset
= copy
->offset
;
5415 copy_entry
= vm_map_copy_first_entry(copy
);
5417 vm_map_copy_to_entry(copy
)){
5418 entry_size
= copy_entry
->vme_end
-
5419 copy_entry
->vme_start
;
5420 if((local_size
< copy_size
) &&
5421 ((local_size
+ entry_size
)
5423 vm_map_copy_clip_end(copy
,
5425 copy_entry
->vme_start
+
5426 (copy_size
- local_size
));
5427 entry_size
= copy_entry
->vme_end
-
5428 copy_entry
->vme_start
;
5429 local_size
+= entry_size
;
5430 new_offset
+= entry_size
;
5432 if(local_size
>= copy_size
) {
5433 next_copy
= copy_entry
->vme_next
;
5434 copy_entry
->vme_next
=
5435 vm_map_copy_to_entry(copy
);
5437 copy
->cpy_hdr
.links
.prev
;
5438 copy
->cpy_hdr
.links
.prev
= copy_entry
;
5439 copy
->size
= copy_size
;
5441 copy
->cpy_hdr
.nentries
;
5442 remaining_entries
-= nentries
;
5443 copy
->cpy_hdr
.nentries
= nentries
;
5446 local_size
+= entry_size
;
5447 new_offset
+= entry_size
;
5450 copy_entry
= copy_entry
->vme_next
;
5454 if((entry
->use_pmap
) && (pmap
== NULL
)) {
5455 kr
= vm_map_copy_overwrite_nested(
5456 entry
->object
.sub_map
,
5460 entry
->object
.sub_map
->pmap
);
5461 } else if (pmap
!= NULL
) {
5462 kr
= vm_map_copy_overwrite_nested(
5463 entry
->object
.sub_map
,
5466 interruptible
, pmap
);
5468 kr
= vm_map_copy_overwrite_nested(
5469 entry
->object
.sub_map
,
5475 if(kr
!= KERN_SUCCESS
) {
5476 if(next_copy
!= NULL
) {
5477 copy
->cpy_hdr
.nentries
+=
5479 copy
->cpy_hdr
.links
.prev
->vme_next
=
5481 copy
->cpy_hdr
.links
.prev
5483 copy
->size
= total_size
;
5487 if (dst_end
<= local_end
) {
5488 return(KERN_SUCCESS
);
5490 /* otherwise copy no longer exists, it was */
5491 /* destroyed after successful copy_overwrite */
5492 copy
= (vm_map_copy_t
)
5493 zalloc(vm_map_copy_zone
);
5494 vm_map_copy_first_entry(copy
) =
5495 vm_map_copy_last_entry(copy
) =
5496 vm_map_copy_to_entry(copy
);
5497 copy
->type
= VM_MAP_COPY_ENTRY_LIST
;
5498 copy
->offset
= new_offset
;
5500 total_size
-= copy_size
;
5502 /* put back remainder of copy in container */
5503 if(next_copy
!= NULL
) {
5504 copy
->cpy_hdr
.nentries
= remaining_entries
;
5505 copy
->cpy_hdr
.links
.next
= next_copy
;
5506 copy
->cpy_hdr
.links
.prev
= previous_prev
;
5507 copy
->size
= total_size
;
5508 next_copy
->vme_prev
=
5509 vm_map_copy_to_entry(copy
);
5512 base_addr
= local_end
;
5513 vm_map_lock(dst_map
);
5514 if(!vm_map_lookup_entry(dst_map
,
5515 local_end
, &tmp_entry
)) {
5516 vm_map_unlock(dst_map
);
5517 return(KERN_INVALID_ADDRESS
);
5522 if (dst_end
<= entry
->vme_end
) {
5523 copy_size
= dst_end
- base_addr
;
5527 if ((next
== vm_map_to_entry(dst_map
)) ||
5528 (next
->vme_start
!= entry
->vme_end
)) {
5529 vm_map_unlock(dst_map
);
5530 return(KERN_INVALID_ADDRESS
);
5539 /* adjust the copy object */
5540 if (total_size
> copy_size
) {
5541 vm_map_size_t local_size
= 0;
5542 vm_map_size_t entry_size
;
5544 new_offset
= copy
->offset
;
5545 copy_entry
= vm_map_copy_first_entry(copy
);
5546 while(copy_entry
!= vm_map_copy_to_entry(copy
)) {
5547 entry_size
= copy_entry
->vme_end
-
5548 copy_entry
->vme_start
;
5549 if((local_size
< copy_size
) &&
5550 ((local_size
+ entry_size
)
5552 vm_map_copy_clip_end(copy
, copy_entry
,
5553 copy_entry
->vme_start
+
5554 (copy_size
- local_size
));
5555 entry_size
= copy_entry
->vme_end
-
5556 copy_entry
->vme_start
;
5557 local_size
+= entry_size
;
5558 new_offset
+= entry_size
;
5560 if(local_size
>= copy_size
) {
5561 next_copy
= copy_entry
->vme_next
;
5562 copy_entry
->vme_next
=
5563 vm_map_copy_to_entry(copy
);
5565 copy
->cpy_hdr
.links
.prev
;
5566 copy
->cpy_hdr
.links
.prev
= copy_entry
;
5567 copy
->size
= copy_size
;
5569 copy
->cpy_hdr
.nentries
;
5570 remaining_entries
-= nentries
;
5571 copy
->cpy_hdr
.nentries
= nentries
;
5574 local_size
+= entry_size
;
5575 new_offset
+= entry_size
;
5578 copy_entry
= copy_entry
->vme_next
;
5588 local_pmap
= dst_map
->pmap
;
5590 if ((kr
= vm_map_copy_overwrite_aligned(
5591 dst_map
, tmp_entry
, copy
,
5592 base_addr
, local_pmap
)) != KERN_SUCCESS
) {
5593 if(next_copy
!= NULL
) {
5594 copy
->cpy_hdr
.nentries
+=
5596 copy
->cpy_hdr
.links
.prev
->vme_next
=
5598 copy
->cpy_hdr
.links
.prev
=
5600 copy
->size
+= copy_size
;
5604 vm_map_unlock(dst_map
);
5609 * if the copy and dst address are misaligned but the same
5610 * offset within the page we can copy_not_aligned the
5611 * misaligned parts and copy aligned the rest. If they are
5612 * aligned but len is unaligned we simply need to copy
5613 * the end bit unaligned. We'll need to split the misaligned
5614 * bits of the region in this case !
5616 /* ALWAYS UNLOCKS THE dst_map MAP */
5617 if ((kr
= vm_map_copy_overwrite_unaligned( dst_map
,
5618 tmp_entry
, copy
, base_addr
)) != KERN_SUCCESS
) {
5619 if(next_copy
!= NULL
) {
5620 copy
->cpy_hdr
.nentries
+=
5622 copy
->cpy_hdr
.links
.prev
->vme_next
=
5624 copy
->cpy_hdr
.links
.prev
=
5626 copy
->size
+= copy_size
;
5631 total_size
-= copy_size
;
5634 base_addr
+= copy_size
;
5636 copy
->offset
= new_offset
;
5637 if(next_copy
!= NULL
) {
5638 copy
->cpy_hdr
.nentries
= remaining_entries
;
5639 copy
->cpy_hdr
.links
.next
= next_copy
;
5640 copy
->cpy_hdr
.links
.prev
= previous_prev
;
5641 next_copy
->vme_prev
= vm_map_copy_to_entry(copy
);
5642 copy
->size
= total_size
;
5644 vm_map_lock(dst_map
);
5646 if (!vm_map_lookup_entry(dst_map
,
5647 base_addr
, &tmp_entry
)) {
5648 vm_map_unlock(dst_map
);
5649 return(KERN_INVALID_ADDRESS
);
5651 if (tmp_entry
->in_transition
) {
5652 entry
->needs_wakeup
= TRUE
;
5653 vm_map_entry_wait(dst_map
, THREAD_UNINT
);
5658 vm_map_clip_start(dst_map
, tmp_entry
, vm_map_trunc_page(base_addr
));
5664 * Throw away the vm_map_copy object
5666 vm_map_copy_discard(copy
);
5668 return(KERN_SUCCESS
);
5669 }/* vm_map_copy_overwrite */
5672 vm_map_copy_overwrite(
5674 vm_map_offset_t dst_addr
,
5676 boolean_t interruptible
)
5678 return vm_map_copy_overwrite_nested(
5679 dst_map
, dst_addr
, copy
, interruptible
, (pmap_t
) NULL
);
5684 * Routine: vm_map_copy_overwrite_unaligned [internal use only]
5687 * Physically copy unaligned data
5690 * Unaligned parts of pages have to be physically copied. We use
5691 * a modified form of vm_fault_copy (which understands none-aligned
5692 * page offsets and sizes) to do the copy. We attempt to copy as
5693 * much memory in one go as possibly, however vm_fault_copy copies
5694 * within 1 memory object so we have to find the smaller of "amount left"
5695 * "source object data size" and "target object data size". With
5696 * unaligned data we don't need to split regions, therefore the source
5697 * (copy) object should be one map entry, the target range may be split
5698 * over multiple map entries however. In any event we are pessimistic
5699 * about these assumptions.
5702 * dst_map is locked on entry and is return locked on success,
5703 * unlocked on error.
5706 static kern_return_t
5707 vm_map_copy_overwrite_unaligned(
5709 vm_map_entry_t entry
,
5711 vm_map_offset_t start
)
5713 vm_map_entry_t copy_entry
= vm_map_copy_first_entry(copy
);
5714 vm_map_version_t version
;
5715 vm_object_t dst_object
;
5716 vm_object_offset_t dst_offset
;
5717 vm_object_offset_t src_offset
;
5718 vm_object_offset_t entry_offset
;
5719 vm_map_offset_t entry_end
;
5720 vm_map_size_t src_size
,
5724 kern_return_t kr
= KERN_SUCCESS
;
5726 vm_map_lock_write_to_read(dst_map
);
5728 src_offset
= copy
->offset
- vm_object_trunc_page(copy
->offset
);
5729 amount_left
= copy
->size
;
5731 * unaligned so we never clipped this entry, we need the offset into
5732 * the vm_object not just the data.
5734 while (amount_left
> 0) {
5736 if (entry
== vm_map_to_entry(dst_map
)) {
5737 vm_map_unlock_read(dst_map
);
5738 return KERN_INVALID_ADDRESS
;
5741 /* "start" must be within the current map entry */
5742 assert ((start
>=entry
->vme_start
) && (start
<entry
->vme_end
));
5744 dst_offset
= start
- entry
->vme_start
;
5746 dst_size
= entry
->vme_end
- start
;
5748 src_size
= copy_entry
->vme_end
-
5749 (copy_entry
->vme_start
+ src_offset
);
5751 if (dst_size
< src_size
) {
5753 * we can only copy dst_size bytes before
5754 * we have to get the next destination entry
5756 copy_size
= dst_size
;
5759 * we can only copy src_size bytes before
5760 * we have to get the next source copy entry
5762 copy_size
= src_size
;
5765 if (copy_size
> amount_left
) {
5766 copy_size
= amount_left
;
5769 * Entry needs copy, create a shadow shadow object for
5770 * Copy on write region.
5772 if (entry
->needs_copy
&&
5773 ((entry
->protection
& VM_PROT_WRITE
) != 0))
5775 if (vm_map_lock_read_to_write(dst_map
)) {
5776 vm_map_lock_read(dst_map
);
5779 vm_object_shadow(&entry
->object
.vm_object
,
5781 (vm_map_size_t
)(entry
->vme_end
5782 - entry
->vme_start
));
5783 entry
->needs_copy
= FALSE
;
5784 vm_map_lock_write_to_read(dst_map
);
5786 dst_object
= entry
->object
.vm_object
;
5788 * unlike with the virtual (aligned) copy we're going
5789 * to fault on it therefore we need a target object.
5791 if (dst_object
== VM_OBJECT_NULL
) {
5792 if (vm_map_lock_read_to_write(dst_map
)) {
5793 vm_map_lock_read(dst_map
);
5796 dst_object
= vm_object_allocate((vm_map_size_t
)
5797 entry
->vme_end
- entry
->vme_start
);
5798 entry
->object
.vm_object
= dst_object
;
5800 vm_map_lock_write_to_read(dst_map
);
5803 * Take an object reference and unlock map. The "entry" may
5804 * disappear or change when the map is unlocked.
5806 vm_object_reference(dst_object
);
5807 version
.main_timestamp
= dst_map
->timestamp
;
5808 entry_offset
= entry
->offset
;
5809 entry_end
= entry
->vme_end
;
5810 vm_map_unlock_read(dst_map
);
5812 * Copy as much as possible in one pass
5815 copy_entry
->object
.vm_object
,
5816 copy_entry
->offset
+ src_offset
,
5819 entry_offset
+ dst_offset
,
5825 src_offset
+= copy_size
;
5826 amount_left
-= copy_size
;
5828 * Release the object reference
5830 vm_object_deallocate(dst_object
);
5832 * If a hard error occurred, return it now
5834 if (kr
!= KERN_SUCCESS
)
5837 if ((copy_entry
->vme_start
+ src_offset
) == copy_entry
->vme_end
5838 || amount_left
== 0)
5841 * all done with this copy entry, dispose.
5843 vm_map_copy_entry_unlink(copy
, copy_entry
);
5844 vm_object_deallocate(copy_entry
->object
.vm_object
);
5845 vm_map_copy_entry_dispose(copy
, copy_entry
);
5847 if ((copy_entry
= vm_map_copy_first_entry(copy
))
5848 == vm_map_copy_to_entry(copy
) && amount_left
) {
5850 * not finished copying but run out of source
5852 return KERN_INVALID_ADDRESS
;
5857 if (amount_left
== 0)
5858 return KERN_SUCCESS
;
5860 vm_map_lock_read(dst_map
);
5861 if (version
.main_timestamp
== dst_map
->timestamp
) {
5862 if (start
== entry_end
) {
5864 * destination region is split. Use the version
5865 * information to avoid a lookup in the normal
5868 entry
= entry
->vme_next
;
5870 * should be contiguous. Fail if we encounter
5871 * a hole in the destination.
5873 if (start
!= entry
->vme_start
) {
5874 vm_map_unlock_read(dst_map
);
5875 return KERN_INVALID_ADDRESS
;
5880 * Map version check failed.
5881 * we must lookup the entry because somebody
5882 * might have changed the map behind our backs.
5885 if (!vm_map_lookup_entry(dst_map
, start
, &entry
))
5887 vm_map_unlock_read(dst_map
);
5888 return KERN_INVALID_ADDRESS
;
5893 return KERN_SUCCESS
;
5894 }/* vm_map_copy_overwrite_unaligned */
5897 * Routine: vm_map_copy_overwrite_aligned [internal use only]
5900 * Does all the vm_trickery possible for whole pages.
5904 * If there are no permanent objects in the destination,
5905 * and the source and destination map entry zones match,
5906 * and the destination map entry is not shared,
5907 * then the map entries can be deleted and replaced
5908 * with those from the copy. The following code is the
5909 * basic idea of what to do, but there are lots of annoying
5910 * little details about getting protection and inheritance
5911 * right. Should add protection, inheritance, and sharing checks
5912 * to the above pass and make sure that no wiring is involved.
5915 static kern_return_t
5916 vm_map_copy_overwrite_aligned(
5918 vm_map_entry_t tmp_entry
,
5920 vm_map_offset_t start
,
5921 __unused pmap_t pmap
)
5924 vm_map_entry_t copy_entry
;
5925 vm_map_size_t copy_size
;
5927 vm_map_entry_t entry
;
5929 while ((copy_entry
= vm_map_copy_first_entry(copy
))
5930 != vm_map_copy_to_entry(copy
))
5932 copy_size
= (copy_entry
->vme_end
- copy_entry
->vme_start
);
5935 assert(!entry
->use_pmap
); /* unnested when clipped earlier */
5936 if (entry
== vm_map_to_entry(dst_map
)) {
5937 vm_map_unlock(dst_map
);
5938 return KERN_INVALID_ADDRESS
;
5940 size
= (entry
->vme_end
- entry
->vme_start
);
5942 * Make sure that no holes popped up in the
5943 * address map, and that the protection is
5944 * still valid, in case the map was unlocked
5948 if ((entry
->vme_start
!= start
) || ((entry
->is_sub_map
)
5949 && !entry
->needs_copy
)) {
5950 vm_map_unlock(dst_map
);
5951 return(KERN_INVALID_ADDRESS
);
5953 assert(entry
!= vm_map_to_entry(dst_map
));
5956 * Check protection again
5959 if ( ! (entry
->protection
& VM_PROT_WRITE
)) {
5960 vm_map_unlock(dst_map
);
5961 return(KERN_PROTECTION_FAILURE
);
5965 * Adjust to source size first
5968 if (copy_size
< size
) {
5969 vm_map_clip_end(dst_map
, entry
, entry
->vme_start
+ copy_size
);
5974 * Adjust to destination size
5977 if (size
< copy_size
) {
5978 vm_map_copy_clip_end(copy
, copy_entry
,
5979 copy_entry
->vme_start
+ size
);
5983 assert((entry
->vme_end
- entry
->vme_start
) == size
);
5984 assert((tmp_entry
->vme_end
- tmp_entry
->vme_start
) == size
);
5985 assert((copy_entry
->vme_end
- copy_entry
->vme_start
) == size
);
5988 * If the destination contains temporary unshared memory,
5989 * we can perform the copy by throwing it away and
5990 * installing the source data.
5993 object
= entry
->object
.vm_object
;
5994 if ((!entry
->is_shared
&&
5995 ((object
== VM_OBJECT_NULL
) ||
5996 (object
->internal
&& !object
->true_share
))) ||
5997 entry
->needs_copy
) {
5998 vm_object_t old_object
= entry
->object
.vm_object
;
5999 vm_object_offset_t old_offset
= entry
->offset
;
6000 vm_object_offset_t offset
;
6003 * Ensure that the source and destination aren't
6006 if (old_object
== copy_entry
->object
.vm_object
&&
6007 old_offset
== copy_entry
->offset
) {
6008 vm_map_copy_entry_unlink(copy
, copy_entry
);
6009 vm_map_copy_entry_dispose(copy
, copy_entry
);
6011 if (old_object
!= VM_OBJECT_NULL
)
6012 vm_object_deallocate(old_object
);
6014 start
= tmp_entry
->vme_end
;
6015 tmp_entry
= tmp_entry
->vme_next
;
6019 if (old_object
!= VM_OBJECT_NULL
) {
6020 if(entry
->is_sub_map
) {
6021 if(entry
->use_pmap
) {
6022 #ifndef NO_NESTED_PMAP
6023 pmap_unnest(dst_map
->pmap
,
6024 (addr64_t
)entry
->vme_start
,
6025 entry
->vme_end
- entry
->vme_start
);
6026 #endif /* NO_NESTED_PMAP */
6027 if(dst_map
->mapped
) {
6028 /* clean up parent */
6030 vm_map_submap_pmap_clean(
6031 dst_map
, entry
->vme_start
,
6033 entry
->object
.sub_map
,
6037 vm_map_submap_pmap_clean(
6038 dst_map
, entry
->vme_start
,
6040 entry
->object
.sub_map
,
6044 entry
->object
.sub_map
);
6046 if(dst_map
->mapped
) {
6047 vm_object_pmap_protect(
6048 entry
->object
.vm_object
,
6056 pmap_remove(dst_map
->pmap
,
6057 (addr64_t
)(entry
->vme_start
),
6058 (addr64_t
)(entry
->vme_end
));
6060 vm_object_deallocate(old_object
);
6064 entry
->is_sub_map
= FALSE
;
6065 entry
->object
= copy_entry
->object
;
6066 object
= entry
->object
.vm_object
;
6067 entry
->needs_copy
= copy_entry
->needs_copy
;
6068 entry
->wired_count
= 0;
6069 entry
->user_wired_count
= 0;
6070 offset
= entry
->offset
= copy_entry
->offset
;
6073 * We should propagate the submap entry's protections
6074 * here instead of forcing VM_PROT_ALL.
6075 * Or better yet, we should inherit the protection
6076 * of the copy_entry.
6078 entry
->protection
= VM_PROT_ALL
;
6079 entry
->max_protection
= VM_PROT_ALL
;
6081 vm_map_copy_entry_unlink(copy
, copy_entry
);
6082 vm_map_copy_entry_dispose(copy
, copy_entry
);
6085 * we could try to push pages into the pmap at this point, BUT
6086 * this optimization only saved on average 2 us per page if ALL
6087 * the pages in the source were currently mapped
6088 * and ALL the pages in the dest were touched, if there were fewer
6089 * than 2/3 of the pages touched, this optimization actually cost more cycles
6090 * it also puts a lot of pressure on the pmap layer w/r to mapping structures
6094 * Set up for the next iteration. The map
6095 * has not been unlocked, so the next
6096 * address should be at the end of this
6097 * entry, and the next map entry should be
6098 * the one following it.
6101 start
= tmp_entry
->vme_end
;
6102 tmp_entry
= tmp_entry
->vme_next
;
6104 vm_map_version_t version
;
6105 vm_object_t dst_object
= entry
->object
.vm_object
;
6106 vm_object_offset_t dst_offset
= entry
->offset
;
6110 * Take an object reference, and record
6111 * the map version information so that the
6112 * map can be safely unlocked.
6115 vm_object_reference(dst_object
);
6117 /* account for unlock bumping up timestamp */
6118 version
.main_timestamp
= dst_map
->timestamp
+ 1;
6120 vm_map_unlock(dst_map
);
6123 * Copy as much as possible in one pass
6128 copy_entry
->object
.vm_object
,
6138 * Release the object reference
6141 vm_object_deallocate(dst_object
);
6144 * If a hard error occurred, return it now
6147 if (r
!= KERN_SUCCESS
)
6150 if (copy_size
!= 0) {
6152 * Dispose of the copied region
6155 vm_map_copy_clip_end(copy
, copy_entry
,
6156 copy_entry
->vme_start
+ copy_size
);
6157 vm_map_copy_entry_unlink(copy
, copy_entry
);
6158 vm_object_deallocate(copy_entry
->object
.vm_object
);
6159 vm_map_copy_entry_dispose(copy
, copy_entry
);
6163 * Pick up in the destination map where we left off.
6165 * Use the version information to avoid a lookup
6166 * in the normal case.
6170 vm_map_lock(dst_map
);
6171 if (version
.main_timestamp
== dst_map
->timestamp
) {
6172 /* We can safely use saved tmp_entry value */
6174 vm_map_clip_end(dst_map
, tmp_entry
, start
);
6175 tmp_entry
= tmp_entry
->vme_next
;
6177 /* Must do lookup of tmp_entry */
6179 if (!vm_map_lookup_entry(dst_map
, start
, &tmp_entry
)) {
6180 vm_map_unlock(dst_map
);
6181 return(KERN_INVALID_ADDRESS
);
6183 vm_map_clip_start(dst_map
, tmp_entry
, start
);
6188 return(KERN_SUCCESS
);
6189 }/* vm_map_copy_overwrite_aligned */
6192 * Routine: vm_map_copyin_kernel_buffer [internal use only]
6195 * Copy in data to a kernel buffer from space in the
6196 * source map. The original space may be optionally
6199 * If successful, returns a new copy object.
6201 static kern_return_t
6202 vm_map_copyin_kernel_buffer(
6204 vm_map_offset_t src_addr
,
6206 boolean_t src_destroy
,
6207 vm_map_copy_t
*copy_result
)
6211 vm_map_size_t kalloc_size
= sizeof(struct vm_map_copy
) + len
;
6213 copy
= (vm_map_copy_t
) kalloc(kalloc_size
);
6214 if (copy
== VM_MAP_COPY_NULL
) {
6215 return KERN_RESOURCE_SHORTAGE
;
6217 copy
->type
= VM_MAP_COPY_KERNEL_BUFFER
;
6220 copy
->cpy_kdata
= (void *) (copy
+ 1);
6221 copy
->cpy_kalloc_size
= kalloc_size
;
6223 kr
= copyinmap(src_map
, src_addr
, copy
->cpy_kdata
, len
);
6224 if (kr
!= KERN_SUCCESS
) {
6225 kfree(copy
, kalloc_size
);
6229 (void) vm_map_remove(src_map
, vm_map_trunc_page(src_addr
),
6230 vm_map_round_page(src_addr
+ len
),
6231 VM_MAP_REMOVE_INTERRUPTIBLE
|
6232 VM_MAP_REMOVE_WAIT_FOR_KWIRE
|
6233 (src_map
== kernel_map
) ?
6234 VM_MAP_REMOVE_KUNWIRE
: 0);
6236 *copy_result
= copy
;
6237 return KERN_SUCCESS
;
6241 * Routine: vm_map_copyout_kernel_buffer [internal use only]
6244 * Copy out data from a kernel buffer into space in the
6245 * destination map. The space may be otpionally dynamically
6248 * If successful, consumes the copy object.
6249 * Otherwise, the caller is responsible for it.
6251 static int vm_map_copyout_kernel_buffer_failures
= 0;
6252 static kern_return_t
6253 vm_map_copyout_kernel_buffer(
6255 vm_map_address_t
*addr
, /* IN/OUT */
6257 boolean_t overwrite
)
6259 kern_return_t kr
= KERN_SUCCESS
;
6260 thread_t thread
= current_thread();
6265 * Allocate space in the target map for the data
6268 kr
= vm_map_enter(map
,
6270 vm_map_round_page(copy
->size
),
6271 (vm_map_offset_t
) 0,
6274 (vm_object_offset_t
) 0,
6278 VM_INHERIT_DEFAULT
);
6279 if (kr
!= KERN_SUCCESS
)
6284 * Copyout the data from the kernel buffer to the target map.
6286 if (thread
->map
== map
) {
6289 * If the target map is the current map, just do
6292 if (copyout(copy
->cpy_kdata
, *addr
, copy
->size
)) {
6293 kr
= KERN_INVALID_ADDRESS
;
6300 * If the target map is another map, assume the
6301 * target's address space identity for the duration
6304 vm_map_reference(map
);
6305 oldmap
= vm_map_switch(map
);
6307 if (copyout(copy
->cpy_kdata
, *addr
, copy
->size
)) {
6308 vm_map_copyout_kernel_buffer_failures
++;
6309 kr
= KERN_INVALID_ADDRESS
;
6312 (void) vm_map_switch(oldmap
);
6313 vm_map_deallocate(map
);
6316 if (kr
!= KERN_SUCCESS
) {
6317 /* the copy failed, clean up */
6320 * Deallocate the space we allocated in the target map.
6322 (void) vm_map_remove(map
,
6323 vm_map_trunc_page(*addr
),
6324 vm_map_round_page(*addr
+
6325 vm_map_round_page(copy
->size
)),
6330 /* copy was successful, dicard the copy structure */
6331 kfree(copy
, copy
->cpy_kalloc_size
);
6338 * Macro: vm_map_copy_insert
6341 * Link a copy chain ("copy") into a map at the
6342 * specified location (after "where").
6344 * The copy chain is destroyed.
6346 * The arguments are evaluated multiple times.
6348 #define vm_map_copy_insert(map, where, copy) \
6350 vm_map_t VMCI_map; \
6351 vm_map_entry_t VMCI_where; \
6352 vm_map_copy_t VMCI_copy; \
6354 VMCI_where = (where); \
6355 VMCI_copy = (copy); \
6356 ((VMCI_where->vme_next)->vme_prev = vm_map_copy_last_entry(VMCI_copy))\
6357 ->vme_next = (VMCI_where->vme_next); \
6358 ((VMCI_where)->vme_next = vm_map_copy_first_entry(VMCI_copy)) \
6359 ->vme_prev = VMCI_where; \
6360 VMCI_map->hdr.nentries += VMCI_copy->cpy_hdr.nentries; \
6361 UPDATE_FIRST_FREE(VMCI_map, VMCI_map->first_free); \
6362 zfree(vm_map_copy_zone, VMCI_copy); \
6366 * Routine: vm_map_copyout
6369 * Copy out a copy chain ("copy") into newly-allocated
6370 * space in the destination map.
6372 * If successful, consumes the copy object.
6373 * Otherwise, the caller is responsible for it.
6378 vm_map_address_t
*dst_addr
, /* OUT */
6382 vm_map_size_t adjustment
;
6383 vm_map_offset_t start
;
6384 vm_object_offset_t vm_copy_start
;
6385 vm_map_entry_t last
;
6387 vm_map_entry_t entry
;
6390 * Check for null copy object.
6393 if (copy
== VM_MAP_COPY_NULL
) {
6395 return(KERN_SUCCESS
);
6399 * Check for special copy object, created
6400 * by vm_map_copyin_object.
6403 if (copy
->type
== VM_MAP_COPY_OBJECT
) {
6404 vm_object_t object
= copy
->cpy_object
;
6406 vm_object_offset_t offset
;
6408 offset
= vm_object_trunc_page(copy
->offset
);
6409 size
= vm_map_round_page(copy
->size
+
6410 (vm_map_size_t
)(copy
->offset
- offset
));
6412 kr
= vm_map_enter(dst_map
, dst_addr
, size
,
6413 (vm_map_offset_t
) 0, VM_FLAGS_ANYWHERE
,
6414 object
, offset
, FALSE
,
6415 VM_PROT_DEFAULT
, VM_PROT_ALL
,
6416 VM_INHERIT_DEFAULT
);
6417 if (kr
!= KERN_SUCCESS
)
6419 /* Account for non-pagealigned copy object */
6420 *dst_addr
+= (vm_map_offset_t
)(copy
->offset
- offset
);
6421 zfree(vm_map_copy_zone
, copy
);
6422 return(KERN_SUCCESS
);
6426 * Check for special kernel buffer allocated
6427 * by new_ipc_kmsg_copyin.
6430 if (copy
->type
== VM_MAP_COPY_KERNEL_BUFFER
) {
6431 return(vm_map_copyout_kernel_buffer(dst_map
, dst_addr
,
6436 * Find space for the data
6439 vm_copy_start
= vm_object_trunc_page(copy
->offset
);
6440 size
= vm_map_round_page((vm_map_size_t
)copy
->offset
+ copy
->size
)
6445 vm_map_lock(dst_map
);
6446 assert(first_free_is_valid(dst_map
));
6447 start
= ((last
= dst_map
->first_free
) == vm_map_to_entry(dst_map
)) ?
6448 vm_map_min(dst_map
) : last
->vme_end
;
6451 vm_map_entry_t next
= last
->vme_next
;
6452 vm_map_offset_t end
= start
+ size
;
6454 if ((end
> dst_map
->max_offset
) || (end
< start
)) {
6455 if (dst_map
->wait_for_space
) {
6456 if (size
<= (dst_map
->max_offset
- dst_map
->min_offset
)) {
6457 assert_wait((event_t
) dst_map
,
6458 THREAD_INTERRUPTIBLE
);
6459 vm_map_unlock(dst_map
);
6460 thread_block(THREAD_CONTINUE_NULL
);
6464 vm_map_unlock(dst_map
);
6465 return(KERN_NO_SPACE
);
6468 if ((next
== vm_map_to_entry(dst_map
)) ||
6469 (next
->vme_start
>= end
))
6473 start
= last
->vme_end
;
6477 * Since we're going to just drop the map
6478 * entries from the copy into the destination
6479 * map, they must come from the same pool.
6482 if (copy
->cpy_hdr
.entries_pageable
!= dst_map
->hdr
.entries_pageable
) {
6484 * Mismatches occur when dealing with the default
6488 vm_map_entry_t next
, new;
6491 * Find the zone that the copies were allocated from
6493 old_zone
= (copy
->cpy_hdr
.entries_pageable
)
6495 : vm_map_kentry_zone
;
6496 entry
= vm_map_copy_first_entry(copy
);
6499 * Reinitialize the copy so that vm_map_copy_entry_link
6502 copy
->cpy_hdr
.nentries
= 0;
6503 copy
->cpy_hdr
.entries_pageable
= dst_map
->hdr
.entries_pageable
;
6504 vm_map_copy_first_entry(copy
) =
6505 vm_map_copy_last_entry(copy
) =
6506 vm_map_copy_to_entry(copy
);
6511 while (entry
!= vm_map_copy_to_entry(copy
)) {
6512 new = vm_map_copy_entry_create(copy
);
6513 vm_map_entry_copy_full(new, entry
);
6514 new->use_pmap
= FALSE
; /* clr address space specifics */
6515 vm_map_copy_entry_link(copy
,
6516 vm_map_copy_last_entry(copy
),
6518 next
= entry
->vme_next
;
6519 zfree(old_zone
, entry
);
6525 * Adjust the addresses in the copy chain, and
6526 * reset the region attributes.
6529 adjustment
= start
- vm_copy_start
;
6530 for (entry
= vm_map_copy_first_entry(copy
);
6531 entry
!= vm_map_copy_to_entry(copy
);
6532 entry
= entry
->vme_next
) {
6533 entry
->vme_start
+= adjustment
;
6534 entry
->vme_end
+= adjustment
;
6536 entry
->inheritance
= VM_INHERIT_DEFAULT
;
6537 entry
->protection
= VM_PROT_DEFAULT
;
6538 entry
->max_protection
= VM_PROT_ALL
;
6539 entry
->behavior
= VM_BEHAVIOR_DEFAULT
;
6542 * If the entry is now wired,
6543 * map the pages into the destination map.
6545 if (entry
->wired_count
!= 0) {
6546 register vm_map_offset_t va
;
6547 vm_object_offset_t offset
;
6548 register vm_object_t object
;
6552 object
= entry
->object
.vm_object
;
6553 offset
= entry
->offset
;
6554 va
= entry
->vme_start
;
6556 pmap_pageable(dst_map
->pmap
,
6561 while (va
< entry
->vme_end
) {
6562 register vm_page_t m
;
6565 * Look up the page in the object.
6566 * Assert that the page will be found in the
6569 * the object was newly created by
6570 * vm_object_copy_slowly, and has
6571 * copies of all of the pages from
6574 * the object was moved from the old
6575 * map entry; because the old map
6576 * entry was wired, all of the pages
6577 * were in the top-level object.
6578 * (XXX not true if we wire pages for
6581 vm_object_lock(object
);
6583 m
= vm_page_lookup(object
, offset
);
6584 if (m
== VM_PAGE_NULL
|| m
->wire_count
== 0 ||
6586 panic("vm_map_copyout: wiring %p", m
);
6590 * The page is assumed to be wired here, so it
6591 * shouldn't be encrypted. Otherwise, we
6592 * couldn't enter it in the page table, since
6593 * we don't want the user to see the encrypted
6596 ASSERT_PAGE_DECRYPTED(m
);
6598 prot
= entry
->protection
;
6600 if (override_nx(dst_map
, entry
->alias
) && prot
)
6601 prot
|= VM_PROT_EXECUTE
;
6603 type_of_fault
= DBG_CACHE_HIT_FAULT
;
6605 vm_fault_enter(m
, dst_map
->pmap
, va
, prot
,
6606 m
->wire_count
!= 0, FALSE
, FALSE
,
6609 vm_object_unlock(object
);
6611 offset
+= PAGE_SIZE_64
;
6618 * Correct the page alignment for the result
6621 *dst_addr
= start
+ (copy
->offset
- vm_copy_start
);
6624 * Update the hints and the map size
6627 SAVE_HINT_MAP_WRITE(dst_map
, vm_map_copy_last_entry(copy
));
6629 dst_map
->size
+= size
;
6635 vm_map_copy_insert(dst_map
, last
, copy
);
6637 vm_map_unlock(dst_map
);
6640 * XXX If wiring_required, call vm_map_pageable
6643 return(KERN_SUCCESS
);
6647 * Routine: vm_map_copyin
6650 * see vm_map_copyin_common. Exported via Unsupported.exports.
6654 #undef vm_map_copyin
6659 vm_map_address_t src_addr
,
6661 boolean_t src_destroy
,
6662 vm_map_copy_t
*copy_result
) /* OUT */
6664 return(vm_map_copyin_common(src_map
, src_addr
, len
, src_destroy
,
6665 FALSE
, copy_result
, FALSE
));
6669 * Routine: vm_map_copyin_common
6672 * Copy the specified region (src_addr, len) from the
6673 * source address space (src_map), possibly removing
6674 * the region from the source address space (src_destroy).
6677 * A vm_map_copy_t object (copy_result), suitable for
6678 * insertion into another address space (using vm_map_copyout),
6679 * copying over another address space region (using
6680 * vm_map_copy_overwrite). If the copy is unused, it
6681 * should be destroyed (using vm_map_copy_discard).
6683 * In/out conditions:
6684 * The source map should not be locked on entry.
6687 typedef struct submap_map
{
6688 vm_map_t parent_map
;
6689 vm_map_offset_t base_start
;
6690 vm_map_offset_t base_end
;
6691 vm_map_size_t base_len
;
6692 struct submap_map
*next
;
6696 vm_map_copyin_common(
6698 vm_map_address_t src_addr
,
6700 boolean_t src_destroy
,
6701 __unused boolean_t src_volatile
,
6702 vm_map_copy_t
*copy_result
, /* OUT */
6703 boolean_t use_maxprot
)
6705 vm_map_entry_t tmp_entry
; /* Result of last map lookup --
6706 * in multi-level lookup, this
6707 * entry contains the actual
6711 vm_map_entry_t new_entry
= VM_MAP_ENTRY_NULL
; /* Map entry for copy */
6713 vm_map_offset_t src_start
; /* Start of current entry --
6714 * where copy is taking place now
6716 vm_map_offset_t src_end
; /* End of entire region to be
6718 vm_map_offset_t src_base
;
6719 vm_map_t base_map
= src_map
;
6720 boolean_t map_share
=FALSE
;
6721 submap_map_t
*parent_maps
= NULL
;
6724 vm_map_copy_t copy
; /* Resulting copy */
6725 vm_map_address_t copy_addr
;
6728 * Check for copies of zero bytes.
6732 *copy_result
= VM_MAP_COPY_NULL
;
6733 return(KERN_SUCCESS
);
6737 * Check that the end address doesn't overflow
6739 src_end
= src_addr
+ len
;
6740 if (src_end
< src_addr
)
6741 return KERN_INVALID_ADDRESS
;
6744 * If the copy is sufficiently small, use a kernel buffer instead
6745 * of making a virtual copy. The theory being that the cost of
6746 * setting up VM (and taking C-O-W faults) dominates the copy costs
6747 * for small regions.
6749 if ((len
< msg_ool_size_small
) && !use_maxprot
)
6750 return vm_map_copyin_kernel_buffer(src_map
, src_addr
, len
,
6751 src_destroy
, copy_result
);
6754 * Compute (page aligned) start and end of region
6756 src_start
= vm_map_trunc_page(src_addr
);
6757 src_end
= vm_map_round_page(src_end
);
6759 XPR(XPR_VM_MAP
, "vm_map_copyin_common map 0x%x addr 0x%x len 0x%x dest %d\n", (natural_t
)src_map
, src_addr
, len
, src_destroy
, 0);
6762 * Allocate a header element for the list.
6764 * Use the start and end in the header to
6765 * remember the endpoints prior to rounding.
6768 copy
= (vm_map_copy_t
) zalloc(vm_map_copy_zone
);
6769 vm_map_copy_first_entry(copy
) =
6770 vm_map_copy_last_entry(copy
) = vm_map_copy_to_entry(copy
);
6771 copy
->type
= VM_MAP_COPY_ENTRY_LIST
;
6772 copy
->cpy_hdr
.nentries
= 0;
6773 copy
->cpy_hdr
.entries_pageable
= TRUE
;
6775 copy
->offset
= src_addr
;
6778 new_entry
= vm_map_copy_entry_create(copy
);
6782 vm_map_unlock(src_map); \
6783 if(src_map != base_map) \
6784 vm_map_deallocate(src_map); \
6785 if (new_entry != VM_MAP_ENTRY_NULL) \
6786 vm_map_copy_entry_dispose(copy,new_entry); \
6787 vm_map_copy_discard(copy); \
6789 submap_map_t *_ptr; \
6791 for(_ptr = parent_maps; _ptr != NULL; _ptr = parent_maps) { \
6792 parent_maps=parent_maps->next; \
6793 if (_ptr->parent_map != base_map) \
6794 vm_map_deallocate(_ptr->parent_map); \
6795 kfree(_ptr, sizeof(submap_map_t)); \
6802 * Find the beginning of the region.
6805 vm_map_lock(src_map
);
6807 if (!vm_map_lookup_entry(src_map
, src_start
, &tmp_entry
))
6808 RETURN(KERN_INVALID_ADDRESS
);
6809 if(!tmp_entry
->is_sub_map
) {
6810 vm_map_clip_start(src_map
, tmp_entry
, src_start
);
6812 /* set for later submap fix-up */
6813 copy_addr
= src_start
;
6816 * Go through entries until we get to the end.
6821 vm_map_entry_t src_entry
= tmp_entry
; /* Top-level entry */
6822 vm_map_size_t src_size
; /* Size of source
6823 * map entry (in both
6828 vm_object_t src_object
; /* Object to copy */
6829 vm_object_offset_t src_offset
;
6831 boolean_t src_needs_copy
; /* Should source map
6833 * for copy-on-write?
6836 boolean_t new_entry_needs_copy
; /* Will new entry be COW? */
6838 boolean_t was_wired
; /* Was source wired? */
6839 vm_map_version_t version
; /* Version before locks
6840 * dropped to make copy
6842 kern_return_t result
; /* Return value from
6843 * copy_strategically.
6845 while(tmp_entry
->is_sub_map
) {
6846 vm_map_size_t submap_len
;
6849 ptr
= (submap_map_t
*)kalloc(sizeof(submap_map_t
));
6850 ptr
->next
= parent_maps
;
6852 ptr
->parent_map
= src_map
;
6853 ptr
->base_start
= src_start
;
6854 ptr
->base_end
= src_end
;
6855 submap_len
= tmp_entry
->vme_end
- src_start
;
6856 if(submap_len
> (src_end
-src_start
))
6857 submap_len
= src_end
-src_start
;
6858 ptr
->base_len
= submap_len
;
6860 src_start
-= tmp_entry
->vme_start
;
6861 src_start
+= tmp_entry
->offset
;
6862 src_end
= src_start
+ submap_len
;
6863 src_map
= tmp_entry
->object
.sub_map
;
6864 vm_map_lock(src_map
);
6865 /* keep an outstanding reference for all maps in */
6866 /* the parents tree except the base map */
6867 vm_map_reference(src_map
);
6868 vm_map_unlock(ptr
->parent_map
);
6869 if (!vm_map_lookup_entry(
6870 src_map
, src_start
, &tmp_entry
))
6871 RETURN(KERN_INVALID_ADDRESS
);
6873 if(!tmp_entry
->is_sub_map
)
6874 vm_map_clip_start(src_map
, tmp_entry
, src_start
);
6875 src_entry
= tmp_entry
;
6877 /* we are now in the lowest level submap... */
6879 if ((tmp_entry
->object
.vm_object
!= VM_OBJECT_NULL
) &&
6880 (tmp_entry
->object
.vm_object
->phys_contiguous
)) {
6881 /* This is not, supported for now.In future */
6882 /* we will need to detect the phys_contig */
6883 /* condition and then upgrade copy_slowly */
6884 /* to do physical copy from the device mem */
6885 /* based object. We can piggy-back off of */
6886 /* the was wired boolean to set-up the */
6887 /* proper handling */
6888 RETURN(KERN_PROTECTION_FAILURE
);
6891 * Create a new address map entry to hold the result.
6892 * Fill in the fields from the appropriate source entries.
6893 * We must unlock the source map to do this if we need
6894 * to allocate a map entry.
6896 if (new_entry
== VM_MAP_ENTRY_NULL
) {
6897 version
.main_timestamp
= src_map
->timestamp
;
6898 vm_map_unlock(src_map
);
6900 new_entry
= vm_map_copy_entry_create(copy
);
6902 vm_map_lock(src_map
);
6903 if ((version
.main_timestamp
+ 1) != src_map
->timestamp
) {
6904 if (!vm_map_lookup_entry(src_map
, src_start
,
6906 RETURN(KERN_INVALID_ADDRESS
);
6908 if (!tmp_entry
->is_sub_map
)
6909 vm_map_clip_start(src_map
, tmp_entry
, src_start
);
6910 continue; /* restart w/ new tmp_entry */
6915 * Verify that the region can be read.
6917 if (((src_entry
->protection
& VM_PROT_READ
) == VM_PROT_NONE
&&
6919 (src_entry
->max_protection
& VM_PROT_READ
) == 0)
6920 RETURN(KERN_PROTECTION_FAILURE
);
6923 * Clip against the endpoints of the entire region.
6926 vm_map_clip_end(src_map
, src_entry
, src_end
);
6928 src_size
= src_entry
->vme_end
- src_start
;
6929 src_object
= src_entry
->object
.vm_object
;
6930 src_offset
= src_entry
->offset
;
6931 was_wired
= (src_entry
->wired_count
!= 0);
6933 vm_map_entry_copy(new_entry
, src_entry
);
6934 new_entry
->use_pmap
= FALSE
; /* clr address space specifics */
6937 * Attempt non-blocking copy-on-write optimizations.
6941 (src_object
== VM_OBJECT_NULL
||
6942 (src_object
->internal
&& !src_object
->true_share
6945 * If we are destroying the source, and the object
6946 * is internal, we can move the object reference
6947 * from the source to the copy. The copy is
6948 * copy-on-write only if the source is.
6949 * We make another reference to the object, because
6950 * destroying the source entry will deallocate it.
6952 vm_object_reference(src_object
);
6955 * Copy is always unwired. vm_map_copy_entry
6956 * set its wired count to zero.
6959 goto CopySuccessful
;
6964 XPR(XPR_VM_MAP
, "vm_map_copyin_common src_obj 0x%x ent 0x%x obj 0x%x was_wired %d\n",
6965 src_object
, new_entry
, new_entry
->object
.vm_object
,
6967 if ((src_object
== VM_OBJECT_NULL
||
6968 (!was_wired
&& !map_share
&& !tmp_entry
->is_shared
)) &&
6969 vm_object_copy_quickly(
6970 &new_entry
->object
.vm_object
,
6974 &new_entry_needs_copy
)) {
6976 new_entry
->needs_copy
= new_entry_needs_copy
;
6979 * Handle copy-on-write obligations
6982 if (src_needs_copy
&& !tmp_entry
->needs_copy
) {
6985 prot
= src_entry
->protection
& ~VM_PROT_WRITE
;
6987 if (override_nx(src_map
, src_entry
->alias
) && prot
)
6988 prot
|= VM_PROT_EXECUTE
;
6990 vm_object_pmap_protect(
6994 (src_entry
->is_shared
?
6997 src_entry
->vme_start
,
7000 tmp_entry
->needs_copy
= TRUE
;
7004 * The map has never been unlocked, so it's safe
7005 * to move to the next entry rather than doing
7009 goto CopySuccessful
;
7013 * Take an object reference, so that we may
7014 * release the map lock(s).
7017 assert(src_object
!= VM_OBJECT_NULL
);
7018 vm_object_reference(src_object
);
7021 * Record the timestamp for later verification.
7025 version
.main_timestamp
= src_map
->timestamp
;
7026 vm_map_unlock(src_map
); /* Increments timestamp once! */
7034 vm_object_lock(src_object
);
7035 result
= vm_object_copy_slowly(
7040 &new_entry
->object
.vm_object
);
7041 new_entry
->offset
= 0;
7042 new_entry
->needs_copy
= FALSE
;
7045 else if (src_object
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
&&
7046 (tmp_entry
->is_shared
|| map_share
)) {
7047 vm_object_t new_object
;
7049 vm_object_lock_shared(src_object
);
7050 new_object
= vm_object_copy_delayed(
7055 if (new_object
== VM_OBJECT_NULL
)
7058 new_entry
->object
.vm_object
= new_object
;
7059 new_entry
->needs_copy
= TRUE
;
7060 result
= KERN_SUCCESS
;
7063 result
= vm_object_copy_strategically(src_object
,
7066 &new_entry
->object
.vm_object
,
7068 &new_entry_needs_copy
);
7070 new_entry
->needs_copy
= new_entry_needs_copy
;
7073 if (result
!= KERN_SUCCESS
&&
7074 result
!= KERN_MEMORY_RESTART_COPY
) {
7075 vm_map_lock(src_map
);
7080 * Throw away the extra reference
7083 vm_object_deallocate(src_object
);
7086 * Verify that the map has not substantially
7087 * changed while the copy was being made.
7090 vm_map_lock(src_map
);
7092 if ((version
.main_timestamp
+ 1) == src_map
->timestamp
)
7093 goto VerificationSuccessful
;
7096 * Simple version comparison failed.
7098 * Retry the lookup and verify that the
7099 * same object/offset are still present.
7101 * [Note: a memory manager that colludes with
7102 * the calling task can detect that we have
7103 * cheated. While the map was unlocked, the
7104 * mapping could have been changed and restored.]
7107 if (!vm_map_lookup_entry(src_map
, src_start
, &tmp_entry
)) {
7108 RETURN(KERN_INVALID_ADDRESS
);
7111 src_entry
= tmp_entry
;
7112 vm_map_clip_start(src_map
, src_entry
, src_start
);
7114 if ((((src_entry
->protection
& VM_PROT_READ
) == VM_PROT_NONE
) &&
7116 ((src_entry
->max_protection
& VM_PROT_READ
) == 0))
7117 goto VerificationFailed
;
7119 if (src_entry
->vme_end
< new_entry
->vme_end
)
7120 src_size
= (new_entry
->vme_end
= src_entry
->vme_end
) - src_start
;
7122 if ((src_entry
->object
.vm_object
!= src_object
) ||
7123 (src_entry
->offset
!= src_offset
) ) {
7126 * Verification failed.
7128 * Start over with this top-level entry.
7131 VerificationFailed
: ;
7133 vm_object_deallocate(new_entry
->object
.vm_object
);
7134 tmp_entry
= src_entry
;
7139 * Verification succeeded.
7142 VerificationSuccessful
: ;
7144 if (result
== KERN_MEMORY_RESTART_COPY
)
7154 * Link in the new copy entry.
7157 vm_map_copy_entry_link(copy
, vm_map_copy_last_entry(copy
),
7161 * Determine whether the entire region
7164 src_base
= src_start
;
7165 src_start
= new_entry
->vme_end
;
7166 new_entry
= VM_MAP_ENTRY_NULL
;
7167 while ((src_start
>= src_end
) && (src_end
!= 0)) {
7168 if (src_map
!= base_map
) {
7172 assert(ptr
!= NULL
);
7173 parent_maps
= parent_maps
->next
;
7175 /* fix up the damage we did in that submap */
7176 vm_map_simplify_range(src_map
,
7180 vm_map_unlock(src_map
);
7181 vm_map_deallocate(src_map
);
7182 vm_map_lock(ptr
->parent_map
);
7183 src_map
= ptr
->parent_map
;
7184 src_base
= ptr
->base_start
;
7185 src_start
= ptr
->base_start
+ ptr
->base_len
;
7186 src_end
= ptr
->base_end
;
7187 if ((src_end
> src_start
) &&
7188 !vm_map_lookup_entry(
7189 src_map
, src_start
, &tmp_entry
))
7190 RETURN(KERN_INVALID_ADDRESS
);
7191 kfree(ptr
, sizeof(submap_map_t
));
7192 if(parent_maps
== NULL
)
7194 src_entry
= tmp_entry
->vme_prev
;
7198 if ((src_start
>= src_end
) && (src_end
!= 0))
7202 * Verify that there are no gaps in the region
7205 tmp_entry
= src_entry
->vme_next
;
7206 if ((tmp_entry
->vme_start
!= src_start
) ||
7207 (tmp_entry
== vm_map_to_entry(src_map
)))
7208 RETURN(KERN_INVALID_ADDRESS
);
7212 * If the source should be destroyed, do it now, since the
7213 * copy was successful.
7216 (void) vm_map_delete(src_map
,
7217 vm_map_trunc_page(src_addr
),
7219 (src_map
== kernel_map
) ?
7220 VM_MAP_REMOVE_KUNWIRE
:
7224 /* fix up the damage we did in the base map */
7225 vm_map_simplify_range(src_map
,
7226 vm_map_trunc_page(src_addr
),
7227 vm_map_round_page(src_end
));
7230 vm_map_unlock(src_map
);
7232 /* Fix-up start and end points in copy. This is necessary */
7233 /* when the various entries in the copy object were picked */
7234 /* up from different sub-maps */
7236 tmp_entry
= vm_map_copy_first_entry(copy
);
7237 while (tmp_entry
!= vm_map_copy_to_entry(copy
)) {
7238 tmp_entry
->vme_end
= copy_addr
+
7239 (tmp_entry
->vme_end
- tmp_entry
->vme_start
);
7240 tmp_entry
->vme_start
= copy_addr
;
7241 copy_addr
+= tmp_entry
->vme_end
- tmp_entry
->vme_start
;
7242 tmp_entry
= (struct vm_map_entry
*)tmp_entry
->vme_next
;
7245 *copy_result
= copy
;
7246 return(KERN_SUCCESS
);
7252 * vm_map_copyin_object:
7254 * Create a copy object from an object.
7255 * Our caller donates an object reference.
7259 vm_map_copyin_object(
7261 vm_object_offset_t offset
, /* offset of region in object */
7262 vm_object_size_t size
, /* size of region in object */
7263 vm_map_copy_t
*copy_result
) /* OUT */
7265 vm_map_copy_t copy
; /* Resulting copy */
7268 * We drop the object into a special copy object
7269 * that contains the object directly.
7272 copy
= (vm_map_copy_t
) zalloc(vm_map_copy_zone
);
7273 copy
->type
= VM_MAP_COPY_OBJECT
;
7274 copy
->cpy_object
= object
;
7275 copy
->offset
= offset
;
7278 *copy_result
= copy
;
7279 return(KERN_SUCCESS
);
7285 vm_map_entry_t old_entry
,
7289 vm_map_entry_t new_entry
;
7292 * New sharing code. New map entry
7293 * references original object. Internal
7294 * objects use asynchronous copy algorithm for
7295 * future copies. First make sure we have
7296 * the right object. If we need a shadow,
7297 * or someone else already has one, then
7298 * make a new shadow and share it.
7301 object
= old_entry
->object
.vm_object
;
7302 if (old_entry
->is_sub_map
) {
7303 assert(old_entry
->wired_count
== 0);
7304 #ifndef NO_NESTED_PMAP
7305 if(old_entry
->use_pmap
) {
7306 kern_return_t result
;
7308 result
= pmap_nest(new_map
->pmap
,
7309 (old_entry
->object
.sub_map
)->pmap
,
7310 (addr64_t
)old_entry
->vme_start
,
7311 (addr64_t
)old_entry
->vme_start
,
7312 (uint64_t)(old_entry
->vme_end
- old_entry
->vme_start
));
7314 panic("vm_map_fork_share: pmap_nest failed!");
7316 #endif /* NO_NESTED_PMAP */
7317 } else if (object
== VM_OBJECT_NULL
) {
7318 object
= vm_object_allocate((vm_map_size_t
)(old_entry
->vme_end
-
7319 old_entry
->vme_start
));
7320 old_entry
->offset
= 0;
7321 old_entry
->object
.vm_object
= object
;
7322 assert(!old_entry
->needs_copy
);
7323 } else if (object
->copy_strategy
!=
7324 MEMORY_OBJECT_COPY_SYMMETRIC
) {
7327 * We are already using an asymmetric
7328 * copy, and therefore we already have
7332 assert(! old_entry
->needs_copy
);
7334 else if (old_entry
->needs_copy
|| /* case 1 */
7335 object
->shadowed
|| /* case 2 */
7336 (!object
->true_share
&& /* case 3 */
7337 !old_entry
->is_shared
&&
7339 (vm_map_size_t
)(old_entry
->vme_end
-
7340 old_entry
->vme_start
)))) {
7343 * We need to create a shadow.
7344 * There are three cases here.
7345 * In the first case, we need to
7346 * complete a deferred symmetrical
7347 * copy that we participated in.
7348 * In the second and third cases,
7349 * we need to create the shadow so
7350 * that changes that we make to the
7351 * object do not interfere with
7352 * any symmetrical copies which
7353 * have occured (case 2) or which
7354 * might occur (case 3).
7356 * The first case is when we had
7357 * deferred shadow object creation
7358 * via the entry->needs_copy mechanism.
7359 * This mechanism only works when
7360 * only one entry points to the source
7361 * object, and we are about to create
7362 * a second entry pointing to the
7363 * same object. The problem is that
7364 * there is no way of mapping from
7365 * an object to the entries pointing
7366 * to it. (Deferred shadow creation
7367 * works with one entry because occurs
7368 * at fault time, and we walk from the
7369 * entry to the object when handling
7372 * The second case is when the object
7373 * to be shared has already been copied
7374 * with a symmetric copy, but we point
7375 * directly to the object without
7376 * needs_copy set in our entry. (This
7377 * can happen because different ranges
7378 * of an object can be pointed to by
7379 * different entries. In particular,
7380 * a single entry pointing to an object
7381 * can be split by a call to vm_inherit,
7382 * which, combined with task_create, can
7383 * result in the different entries
7384 * having different needs_copy values.)
7385 * The shadowed flag in the object allows
7386 * us to detect this case. The problem
7387 * with this case is that if this object
7388 * has or will have shadows, then we
7389 * must not perform an asymmetric copy
7390 * of this object, since such a copy
7391 * allows the object to be changed, which
7392 * will break the previous symmetrical
7393 * copies (which rely upon the object
7394 * not changing). In a sense, the shadowed
7395 * flag says "don't change this object".
7396 * We fix this by creating a shadow
7397 * object for this object, and sharing
7398 * that. This works because we are free
7399 * to change the shadow object (and thus
7400 * to use an asymmetric copy strategy);
7401 * this is also semantically correct,
7402 * since this object is temporary, and
7403 * therefore a copy of the object is
7404 * as good as the object itself. (This
7405 * is not true for permanent objects,
7406 * since the pager needs to see changes,
7407 * which won't happen if the changes
7408 * are made to a copy.)
7410 * The third case is when the object
7411 * to be shared has parts sticking
7412 * outside of the entry we're working
7413 * with, and thus may in the future
7414 * be subject to a symmetrical copy.
7415 * (This is a preemptive version of
7419 vm_object_shadow(&old_entry
->object
.vm_object
,
7421 (vm_map_size_t
) (old_entry
->vme_end
-
7422 old_entry
->vme_start
));
7425 * If we're making a shadow for other than
7426 * copy on write reasons, then we have
7427 * to remove write permission.
7430 if (!old_entry
->needs_copy
&&
7431 (old_entry
->protection
& VM_PROT_WRITE
)) {
7434 prot
= old_entry
->protection
& ~VM_PROT_WRITE
;
7436 if (override_nx(old_map
, old_entry
->alias
) && prot
)
7437 prot
|= VM_PROT_EXECUTE
;
7439 if (old_map
->mapped
) {
7440 vm_object_pmap_protect(
7441 old_entry
->object
.vm_object
,
7443 (old_entry
->vme_end
-
7444 old_entry
->vme_start
),
7446 old_entry
->vme_start
,
7449 pmap_protect(old_map
->pmap
,
7450 old_entry
->vme_start
,
7456 old_entry
->needs_copy
= FALSE
;
7457 object
= old_entry
->object
.vm_object
;
7461 * If object was using a symmetric copy strategy,
7462 * change its copy strategy to the default
7463 * asymmetric copy strategy, which is copy_delay
7464 * in the non-norma case and copy_call in the
7465 * norma case. Bump the reference count for the
7469 if(old_entry
->is_sub_map
) {
7470 vm_map_lock(old_entry
->object
.sub_map
);
7471 vm_map_reference(old_entry
->object
.sub_map
);
7472 vm_map_unlock(old_entry
->object
.sub_map
);
7474 vm_object_lock(object
);
7475 vm_object_reference_locked(object
);
7476 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
) {
7477 object
->copy_strategy
= MEMORY_OBJECT_COPY_DELAY
;
7479 vm_object_unlock(object
);
7483 * Clone the entry, using object ref from above.
7484 * Mark both entries as shared.
7487 new_entry
= vm_map_entry_create(new_map
);
7488 vm_map_entry_copy(new_entry
, old_entry
);
7489 old_entry
->is_shared
= TRUE
;
7490 new_entry
->is_shared
= TRUE
;
7493 * Insert the entry into the new map -- we
7494 * know we're inserting at the end of the new
7498 vm_map_entry_link(new_map
, vm_map_last_entry(new_map
), new_entry
);
7501 * Update the physical map
7504 if (old_entry
->is_sub_map
) {
7505 /* Bill Angell pmap support goes here */
7507 pmap_copy(new_map
->pmap
, old_map
->pmap
, new_entry
->vme_start
,
7508 old_entry
->vme_end
- old_entry
->vme_start
,
7509 old_entry
->vme_start
);
7516 vm_map_entry_t
*old_entry_p
,
7519 vm_map_entry_t old_entry
= *old_entry_p
;
7520 vm_map_size_t entry_size
= old_entry
->vme_end
- old_entry
->vme_start
;
7521 vm_map_offset_t start
= old_entry
->vme_start
;
7523 vm_map_entry_t last
= vm_map_last_entry(new_map
);
7525 vm_map_unlock(old_map
);
7527 * Use maxprot version of copyin because we
7528 * care about whether this memory can ever
7529 * be accessed, not just whether it's accessible
7532 if (vm_map_copyin_maxprot(old_map
, start
, entry_size
, FALSE
, ©
)
7535 * The map might have changed while it
7536 * was unlocked, check it again. Skip
7537 * any blank space or permanently
7538 * unreadable region.
7540 vm_map_lock(old_map
);
7541 if (!vm_map_lookup_entry(old_map
, start
, &last
) ||
7542 (last
->max_protection
& VM_PROT_READ
) == VM_PROT_NONE
) {
7543 last
= last
->vme_next
;
7545 *old_entry_p
= last
;
7548 * XXX For some error returns, want to
7549 * XXX skip to the next element. Note
7550 * that INVALID_ADDRESS and
7551 * PROTECTION_FAILURE are handled above.
7558 * Insert the copy into the new map
7561 vm_map_copy_insert(new_map
, last
, copy
);
7564 * Pick up the traversal at the end of
7565 * the copied region.
7568 vm_map_lock(old_map
);
7569 start
+= entry_size
;
7570 if (! vm_map_lookup_entry(old_map
, start
, &last
)) {
7571 last
= last
->vme_next
;
7573 if (last
->vme_start
== start
) {
7575 * No need to clip here and we don't
7576 * want to cause any unnecessary
7580 vm_map_clip_start(old_map
, last
, start
);
7583 *old_entry_p
= last
;
7591 * Create and return a new map based on the old
7592 * map, according to the inheritance values on the
7593 * regions in that map.
7595 * The source map must not be locked.
7603 vm_map_entry_t old_entry
;
7604 vm_map_size_t new_size
= 0, entry_size
;
7605 vm_map_entry_t new_entry
;
7606 boolean_t src_needs_copy
;
7607 boolean_t new_entry_needs_copy
;
7610 new_pmap
= pmap_create((vm_map_size_t
) 0,
7611 old_map
->pmap
->pm_task_map
!= TASK_MAP_32BIT
);
7612 if (old_map
->pmap
->pm_task_map
== TASK_MAP_64BIT_SHARED
)
7613 pmap_set_4GB_pagezero(new_pmap
);
7615 new_pmap
= pmap_create((vm_map_size_t
) 0, 0);
7618 vm_map_reference_swap(old_map
);
7619 vm_map_lock(old_map
);
7621 new_map
= vm_map_create(new_pmap
,
7622 old_map
->min_offset
,
7623 old_map
->max_offset
,
7624 old_map
->hdr
.entries_pageable
);
7627 old_entry
= vm_map_first_entry(old_map
);
7628 old_entry
!= vm_map_to_entry(old_map
);
7631 entry_size
= old_entry
->vme_end
- old_entry
->vme_start
;
7633 switch (old_entry
->inheritance
) {
7634 case VM_INHERIT_NONE
:
7637 case VM_INHERIT_SHARE
:
7638 vm_map_fork_share(old_map
, old_entry
, new_map
);
7639 new_size
+= entry_size
;
7642 case VM_INHERIT_COPY
:
7645 * Inline the copy_quickly case;
7646 * upon failure, fall back on call
7647 * to vm_map_fork_copy.
7650 if(old_entry
->is_sub_map
)
7652 if ((old_entry
->wired_count
!= 0) ||
7653 ((old_entry
->object
.vm_object
!= NULL
) &&
7654 (old_entry
->object
.vm_object
->true_share
))) {
7655 goto slow_vm_map_fork_copy
;
7658 new_entry
= vm_map_entry_create(new_map
);
7659 vm_map_entry_copy(new_entry
, old_entry
);
7660 /* clear address space specifics */
7661 new_entry
->use_pmap
= FALSE
;
7663 if (! vm_object_copy_quickly(
7664 &new_entry
->object
.vm_object
,
7666 (old_entry
->vme_end
-
7667 old_entry
->vme_start
),
7669 &new_entry_needs_copy
)) {
7670 vm_map_entry_dispose(new_map
, new_entry
);
7671 goto slow_vm_map_fork_copy
;
7675 * Handle copy-on-write obligations
7678 if (src_needs_copy
&& !old_entry
->needs_copy
) {
7681 prot
= old_entry
->protection
& ~VM_PROT_WRITE
;
7683 if (override_nx(old_map
, old_entry
->alias
) && prot
)
7684 prot
|= VM_PROT_EXECUTE
;
7686 vm_object_pmap_protect(
7687 old_entry
->object
.vm_object
,
7689 (old_entry
->vme_end
-
7690 old_entry
->vme_start
),
7691 ((old_entry
->is_shared
7695 old_entry
->vme_start
,
7698 old_entry
->needs_copy
= TRUE
;
7700 new_entry
->needs_copy
= new_entry_needs_copy
;
7703 * Insert the entry at the end
7707 vm_map_entry_link(new_map
, vm_map_last_entry(new_map
),
7709 new_size
+= entry_size
;
7712 slow_vm_map_fork_copy
:
7713 if (vm_map_fork_copy(old_map
, &old_entry
, new_map
)) {
7714 new_size
+= entry_size
;
7718 old_entry
= old_entry
->vme_next
;
7721 new_map
->size
= new_size
;
7722 vm_map_unlock(old_map
);
7723 vm_map_deallocate(old_map
);
7731 * Setup the "new_map" with the proper execution environment according
7732 * to the type of executable (platform, 64bit, chroot environment).
7733 * Map the comm page and shared region, etc...
7742 SHARED_REGION_TRACE_DEBUG(
7743 ("shared_region: task %p: vm_map_exec(%p,%p,%p,0x%x): ->\n",
7744 current_task(), new_map
, task
, fsroot
, cpu
));
7745 (void) vm_commpage_enter(new_map
, task
);
7746 (void) vm_shared_region_enter(new_map
, task
, fsroot
, cpu
);
7747 SHARED_REGION_TRACE_DEBUG(
7748 ("shared_region: task %p: vm_map_exec(%p,%p,%p,0x%x): <-\n",
7749 current_task(), new_map
, task
, fsroot
, cpu
));
7750 return KERN_SUCCESS
;
7754 * vm_map_lookup_locked:
7756 * Finds the VM object, offset, and
7757 * protection for a given virtual address in the
7758 * specified map, assuming a page fault of the
7761 * Returns the (object, offset, protection) for
7762 * this address, whether it is wired down, and whether
7763 * this map has the only reference to the data in question.
7764 * In order to later verify this lookup, a "version"
7767 * The map MUST be locked by the caller and WILL be
7768 * locked on exit. In order to guarantee the
7769 * existence of the returned object, it is returned
7772 * If a lookup is requested with "write protection"
7773 * specified, the map may be changed to perform virtual
7774 * copying operations, although the data referenced will
7778 vm_map_lookup_locked(
7779 vm_map_t
*var_map
, /* IN/OUT */
7780 vm_map_offset_t vaddr
,
7781 vm_prot_t fault_type
,
7782 int object_lock_type
,
7783 vm_map_version_t
*out_version
, /* OUT */
7784 vm_object_t
*object
, /* OUT */
7785 vm_object_offset_t
*offset
, /* OUT */
7786 vm_prot_t
*out_prot
, /* OUT */
7787 boolean_t
*wired
, /* OUT */
7788 vm_object_fault_info_t fault_info
, /* OUT */
7791 vm_map_entry_t entry
;
7792 register vm_map_t map
= *var_map
;
7793 vm_map_t old_map
= *var_map
;
7794 vm_map_t cow_sub_map_parent
= VM_MAP_NULL
;
7795 vm_map_offset_t cow_parent_vaddr
= 0;
7796 vm_map_offset_t old_start
= 0;
7797 vm_map_offset_t old_end
= 0;
7798 register vm_prot_t prot
;
7804 * If the map has an interesting hint, try it before calling
7805 * full blown lookup routine.
7809 if ((entry
== vm_map_to_entry(map
)) ||
7810 (vaddr
< entry
->vme_start
) || (vaddr
>= entry
->vme_end
)) {
7811 vm_map_entry_t tmp_entry
;
7814 * Entry was either not a valid hint, or the vaddr
7815 * was not contained in the entry, so do a full lookup.
7817 if (!vm_map_lookup_entry(map
, vaddr
, &tmp_entry
)) {
7818 if((cow_sub_map_parent
) && (cow_sub_map_parent
!= map
))
7819 vm_map_unlock(cow_sub_map_parent
);
7820 if((*real_map
!= map
)
7821 && (*real_map
!= cow_sub_map_parent
))
7822 vm_map_unlock(*real_map
);
7823 return KERN_INVALID_ADDRESS
;
7828 if(map
== old_map
) {
7829 old_start
= entry
->vme_start
;
7830 old_end
= entry
->vme_end
;
7834 * Handle submaps. Drop lock on upper map, submap is
7839 if (entry
->is_sub_map
) {
7840 vm_map_offset_t local_vaddr
;
7841 vm_map_offset_t end_delta
;
7842 vm_map_offset_t start_delta
;
7843 vm_map_entry_t submap_entry
;
7844 boolean_t mapped_needs_copy
=FALSE
;
7846 local_vaddr
= vaddr
;
7848 if ((entry
->use_pmap
&& !(fault_type
& VM_PROT_WRITE
))) {
7849 /* if real_map equals map we unlock below */
7850 if ((*real_map
!= map
) &&
7851 (*real_map
!= cow_sub_map_parent
))
7852 vm_map_unlock(*real_map
);
7853 *real_map
= entry
->object
.sub_map
;
7856 if(entry
->needs_copy
&& (fault_type
& VM_PROT_WRITE
)) {
7857 if (!mapped_needs_copy
) {
7858 if (vm_map_lock_read_to_write(map
)) {
7859 vm_map_lock_read(map
);
7860 /* XXX FBDP: entry still valid ? */
7861 if(*real_map
== entry
->object
.sub_map
)
7865 vm_map_lock_read(entry
->object
.sub_map
);
7866 cow_sub_map_parent
= map
;
7867 /* reset base to map before cow object */
7868 /* this is the map which will accept */
7869 /* the new cow object */
7870 old_start
= entry
->vme_start
;
7871 old_end
= entry
->vme_end
;
7872 cow_parent_vaddr
= vaddr
;
7873 mapped_needs_copy
= TRUE
;
7875 vm_map_lock_read(entry
->object
.sub_map
);
7876 if((cow_sub_map_parent
!= map
) &&
7881 vm_map_lock_read(entry
->object
.sub_map
);
7882 /* leave map locked if it is a target */
7883 /* cow sub_map above otherwise, just */
7884 /* follow the maps down to the object */
7885 /* here we unlock knowing we are not */
7886 /* revisiting the map. */
7887 if((*real_map
!= map
) && (map
!= cow_sub_map_parent
))
7888 vm_map_unlock_read(map
);
7891 /* XXX FBDP: map has been unlocked, what protects "entry" !? */
7892 *var_map
= map
= entry
->object
.sub_map
;
7894 /* calculate the offset in the submap for vaddr */
7895 local_vaddr
= (local_vaddr
- entry
->vme_start
) + entry
->offset
;
7898 if(!vm_map_lookup_entry(map
, local_vaddr
, &submap_entry
)) {
7899 if((cow_sub_map_parent
) && (cow_sub_map_parent
!= map
)){
7900 vm_map_unlock(cow_sub_map_parent
);
7902 if((*real_map
!= map
)
7903 && (*real_map
!= cow_sub_map_parent
)) {
7904 vm_map_unlock(*real_map
);
7907 return KERN_INVALID_ADDRESS
;
7910 /* find the attenuated shadow of the underlying object */
7911 /* on our target map */
7913 /* in english the submap object may extend beyond the */
7914 /* region mapped by the entry or, may only fill a portion */
7915 /* of it. For our purposes, we only care if the object */
7916 /* doesn't fill. In this case the area which will */
7917 /* ultimately be clipped in the top map will only need */
7918 /* to be as big as the portion of the underlying entry */
7919 /* which is mapped */
7920 start_delta
= submap_entry
->vme_start
> entry
->offset
?
7921 submap_entry
->vme_start
- entry
->offset
: 0;
7924 (entry
->offset
+ start_delta
+ (old_end
- old_start
)) <=
7925 submap_entry
->vme_end
?
7926 0 : (entry
->offset
+
7927 (old_end
- old_start
))
7928 - submap_entry
->vme_end
;
7930 old_start
+= start_delta
;
7931 old_end
-= end_delta
;
7933 if(submap_entry
->is_sub_map
) {
7934 entry
= submap_entry
;
7935 vaddr
= local_vaddr
;
7936 goto submap_recurse
;
7939 if(((fault_type
& VM_PROT_WRITE
) && cow_sub_map_parent
)) {
7941 vm_object_t sub_object
, copy_object
;
7942 vm_object_offset_t copy_offset
;
7943 vm_map_offset_t local_start
;
7944 vm_map_offset_t local_end
;
7945 boolean_t copied_slowly
= FALSE
;
7947 if (vm_map_lock_read_to_write(map
)) {
7948 vm_map_lock_read(map
);
7949 old_start
-= start_delta
;
7950 old_end
+= end_delta
;
7955 sub_object
= submap_entry
->object
.vm_object
;
7956 if (sub_object
== VM_OBJECT_NULL
) {
7960 (submap_entry
->vme_end
-
7961 submap_entry
->vme_start
));
7962 submap_entry
->object
.vm_object
= sub_object
;
7963 submap_entry
->offset
= 0;
7965 local_start
= local_vaddr
-
7966 (cow_parent_vaddr
- old_start
);
7967 local_end
= local_vaddr
+
7968 (old_end
- cow_parent_vaddr
);
7969 vm_map_clip_start(map
, submap_entry
, local_start
);
7970 vm_map_clip_end(map
, submap_entry
, local_end
);
7971 /* unnesting was done in vm_map_clip_start/end() */
7972 assert(!submap_entry
->use_pmap
);
7974 /* This is the COW case, lets connect */
7975 /* an entry in our space to the underlying */
7976 /* object in the submap, bypassing the */
7980 if(submap_entry
->wired_count
!= 0 ||
7981 (sub_object
->copy_strategy
!=
7982 MEMORY_OBJECT_COPY_SYMMETRIC
)) {
7983 vm_object_lock(sub_object
);
7984 vm_object_copy_slowly(sub_object
,
7985 submap_entry
->offset
,
7986 (submap_entry
->vme_end
-
7987 submap_entry
->vme_start
),
7990 copied_slowly
= TRUE
;
7993 /* set up shadow object */
7994 copy_object
= sub_object
;
7995 vm_object_reference(copy_object
);
7996 sub_object
->shadowed
= TRUE
;
7997 submap_entry
->needs_copy
= TRUE
;
7999 prot
= submap_entry
->protection
& ~VM_PROT_WRITE
;
8001 if (override_nx(map
, submap_entry
->alias
) && prot
)
8002 prot
|= VM_PROT_EXECUTE
;
8004 vm_object_pmap_protect(
8006 submap_entry
->offset
,
8007 submap_entry
->vme_end
-
8008 submap_entry
->vme_start
,
8009 (submap_entry
->is_shared
8011 PMAP_NULL
: map
->pmap
,
8012 submap_entry
->vme_start
,
8017 * Adjust the fault offset to the submap entry.
8019 copy_offset
= (local_vaddr
-
8020 submap_entry
->vme_start
+
8021 submap_entry
->offset
);
8023 /* This works diffently than the */
8024 /* normal submap case. We go back */
8025 /* to the parent of the cow map and*/
8026 /* clip out the target portion of */
8027 /* the sub_map, substituting the */
8028 /* new copy object, */
8031 local_start
= old_start
;
8032 local_end
= old_end
;
8033 map
= cow_sub_map_parent
;
8034 *var_map
= cow_sub_map_parent
;
8035 vaddr
= cow_parent_vaddr
;
8036 cow_sub_map_parent
= NULL
;
8038 if(!vm_map_lookup_entry(map
,
8040 vm_object_deallocate(
8042 vm_map_lock_write_to_read(map
);
8043 return KERN_INVALID_ADDRESS
;
8046 /* clip out the portion of space */
8047 /* mapped by the sub map which */
8048 /* corresponds to the underlying */
8052 * Clip (and unnest) the smallest nested chunk
8053 * possible around the faulting address...
8055 local_start
= vaddr
& ~(pmap_nesting_size_min
- 1);
8056 local_end
= local_start
+ pmap_nesting_size_min
;
8058 * ... but don't go beyond the "old_start" to "old_end"
8059 * range, to avoid spanning over another VM region
8060 * with a possibly different VM object and/or offset.
8062 if (local_start
< old_start
) {
8063 local_start
= old_start
;
8065 if (local_end
> old_end
) {
8066 local_end
= old_end
;
8069 * Adjust copy_offset to the start of the range.
8071 copy_offset
-= (vaddr
- local_start
);
8073 vm_map_clip_start(map
, entry
, local_start
);
8074 vm_map_clip_end(map
, entry
, local_end
);
8075 /* unnesting was done in vm_map_clip_start/end() */
8076 assert(!entry
->use_pmap
);
8078 /* substitute copy object for */
8079 /* shared map entry */
8080 vm_map_deallocate(entry
->object
.sub_map
);
8081 entry
->is_sub_map
= FALSE
;
8082 entry
->object
.vm_object
= copy_object
;
8084 /* propagate the submap entry's protections */
8085 entry
->protection
|= submap_entry
->protection
;
8086 entry
->max_protection
|= submap_entry
->max_protection
;
8090 entry
->needs_copy
= FALSE
;
8091 entry
->is_shared
= FALSE
;
8093 entry
->offset
= copy_offset
;
8094 entry
->needs_copy
= TRUE
;
8095 if(entry
->inheritance
== VM_INHERIT_SHARE
)
8096 entry
->inheritance
= VM_INHERIT_COPY
;
8098 entry
->is_shared
= TRUE
;
8100 if(entry
->inheritance
== VM_INHERIT_SHARE
)
8101 entry
->inheritance
= VM_INHERIT_COPY
;
8103 vm_map_lock_write_to_read(map
);
8105 if((cow_sub_map_parent
)
8106 && (cow_sub_map_parent
!= *real_map
)
8107 && (cow_sub_map_parent
!= map
)) {
8108 vm_map_unlock(cow_sub_map_parent
);
8110 entry
= submap_entry
;
8111 vaddr
= local_vaddr
;
8116 * Check whether this task is allowed to have
8120 prot
= entry
->protection
;
8122 if (override_nx(map
, entry
->alias
) && prot
) {
8124 * HACK -- if not a stack, then allow execution
8126 prot
|= VM_PROT_EXECUTE
;
8129 if ((fault_type
& (prot
)) != fault_type
) {
8130 if (*real_map
!= map
) {
8131 vm_map_unlock(*real_map
);
8135 if ((fault_type
& VM_PROT_EXECUTE
) && prot
)
8136 log_stack_execution_failure((addr64_t
)vaddr
, prot
);
8138 DTRACE_VM2(prot_fault
, int, 1, (uint64_t *), NULL
);
8139 return KERN_PROTECTION_FAILURE
;
8143 * If this page is not pageable, we have to get
8144 * it for all possible accesses.
8147 *wired
= (entry
->wired_count
!= 0);
8152 * If the entry was copy-on-write, we either ...
8155 if (entry
->needs_copy
) {
8157 * If we want to write the page, we may as well
8158 * handle that now since we've got the map locked.
8160 * If we don't need to write the page, we just
8161 * demote the permissions allowed.
8164 if ((fault_type
& VM_PROT_WRITE
) || *wired
) {
8166 * Make a new object, and place it in the
8167 * object chain. Note that no new references
8168 * have appeared -- one just moved from the
8169 * map to the new object.
8172 if (vm_map_lock_read_to_write(map
)) {
8173 vm_map_lock_read(map
);
8176 vm_object_shadow(&entry
->object
.vm_object
,
8178 (vm_map_size_t
) (entry
->vme_end
-
8181 entry
->object
.vm_object
->shadowed
= TRUE
;
8182 entry
->needs_copy
= FALSE
;
8183 vm_map_lock_write_to_read(map
);
8187 * We're attempting to read a copy-on-write
8188 * page -- don't allow writes.
8191 prot
&= (~VM_PROT_WRITE
);
8196 * Create an object if necessary.
8198 if (entry
->object
.vm_object
== VM_OBJECT_NULL
) {
8200 if (vm_map_lock_read_to_write(map
)) {
8201 vm_map_lock_read(map
);
8205 entry
->object
.vm_object
= vm_object_allocate(
8206 (vm_map_size_t
)(entry
->vme_end
- entry
->vme_start
));
8208 vm_map_lock_write_to_read(map
);
8212 * Return the object/offset from this entry. If the entry
8213 * was copy-on-write or empty, it has been fixed up. Also
8214 * return the protection.
8217 *offset
= (vaddr
- entry
->vme_start
) + entry
->offset
;
8218 *object
= entry
->object
.vm_object
;
8222 fault_info
->interruptible
= THREAD_UNINT
; /* for now... */
8223 /* ... the caller will change "interruptible" if needed */
8224 fault_info
->cluster_size
= 0;
8225 fault_info
->user_tag
= entry
->alias
;
8226 fault_info
->behavior
= entry
->behavior
;
8227 fault_info
->lo_offset
= entry
->offset
;
8228 fault_info
->hi_offset
= (entry
->vme_end
- entry
->vme_start
) + entry
->offset
;
8229 fault_info
->no_cache
= entry
->no_cache
;
8233 * Lock the object to prevent it from disappearing
8235 if (object_lock_type
== OBJECT_LOCK_EXCLUSIVE
)
8236 vm_object_lock(*object
);
8238 vm_object_lock_shared(*object
);
8241 * Save the version number
8244 out_version
->main_timestamp
= map
->timestamp
;
8246 return KERN_SUCCESS
;
8253 * Verifies that the map in question has not changed
8254 * since the given version. If successful, the map
8255 * will not change until vm_map_verify_done() is called.
8259 register vm_map_t map
,
8260 register vm_map_version_t
*version
) /* REF */
8264 vm_map_lock_read(map
);
8265 result
= (map
->timestamp
== version
->main_timestamp
);
8268 vm_map_unlock_read(map
);
8274 * vm_map_verify_done:
8276 * Releases locks acquired by a vm_map_verify.
8278 * This is now a macro in vm/vm_map.h. It does a
8279 * vm_map_unlock_read on the map.
8284 * TEMPORARYTEMPORARYTEMPORARYTEMPORARYTEMPORARYTEMPORARY
8285 * Goes away after regular vm_region_recurse function migrates to
8287 * vm_region_recurse: A form of vm_region which follows the
8288 * submaps in a target map
8293 vm_map_region_recurse_64(
8295 vm_map_offset_t
*address
, /* IN/OUT */
8296 vm_map_size_t
*size
, /* OUT */
8297 natural_t
*nesting_depth
, /* IN/OUT */
8298 vm_region_submap_info_64_t submap_info
, /* IN/OUT */
8299 mach_msg_type_number_t
*count
) /* IN/OUT */
8301 vm_region_extended_info_data_t extended
;
8302 vm_map_entry_t tmp_entry
;
8303 vm_map_offset_t user_address
;
8304 unsigned int user_max_depth
;
8307 * "curr_entry" is the VM map entry preceding or including the
8308 * address we're looking for.
8309 * "curr_map" is the map or sub-map containing "curr_entry".
8310 * "curr_offset" is the cumulated offset of "curr_map" in the
8311 * target task's address space.
8312 * "curr_depth" is the depth of "curr_map" in the chain of
8314 * "curr_max_offset" is the maximum offset we should take into
8315 * account in the current map. It may be smaller than the current
8316 * map's "max_offset" because we might not have mapped it all in
8317 * the upper level map.
8319 vm_map_entry_t curr_entry
;
8320 vm_map_offset_t curr_offset
;
8322 unsigned int curr_depth
;
8323 vm_map_offset_t curr_max_offset
;
8326 * "next_" is the same as "curr_" but for the VM region immediately
8327 * after the address we're looking for. We need to keep track of this
8328 * too because we want to return info about that region if the
8329 * address we're looking for is not mapped.
8331 vm_map_entry_t next_entry
;
8332 vm_map_offset_t next_offset
;
8334 unsigned int next_depth
;
8335 vm_map_offset_t next_max_offset
;
8337 boolean_t look_for_pages
;
8338 vm_region_submap_short_info_64_t short_info
;
8340 if (map
== VM_MAP_NULL
) {
8341 /* no address space to work on */
8342 return KERN_INVALID_ARGUMENT
;
8345 if (*count
< VM_REGION_SUBMAP_INFO_COUNT_64
) {
8346 if (*count
< VM_REGION_SUBMAP_SHORT_INFO_COUNT_64
) {
8348 * "info" structure is not big enough and
8351 return KERN_INVALID_ARGUMENT
;
8353 look_for_pages
= FALSE
;
8354 *count
= VM_REGION_SUBMAP_SHORT_INFO_COUNT_64
;
8355 short_info
= (vm_region_submap_short_info_64_t
) submap_info
;
8359 look_for_pages
= TRUE
;
8360 *count
= VM_REGION_SUBMAP_INFO_COUNT_64
;
8365 user_address
= *address
;
8366 user_max_depth
= *nesting_depth
;
8372 curr_max_offset
= curr_map
->max_offset
;
8378 next_max_offset
= curr_max_offset
;
8381 vm_map_lock_read(curr_map
);
8385 if (vm_map_lookup_entry(curr_map
,
8386 user_address
- curr_offset
,
8388 /* tmp_entry contains the address we're looking for */
8389 curr_entry
= tmp_entry
;
8392 * The address is not mapped. "tmp_entry" is the
8393 * map entry preceding the address. We want the next
8394 * one, if it exists.
8396 curr_entry
= tmp_entry
->vme_next
;
8397 if (curr_entry
== vm_map_to_entry(curr_map
) ||
8398 curr_entry
->vme_start
>= curr_max_offset
) {
8399 /* no next entry at this level: stop looking */
8401 vm_map_unlock_read(curr_map
);
8407 curr_max_offset
= 0;
8413 * Is the next entry at this level closer to the address (or
8414 * deeper in the submap chain) than the one we had
8417 tmp_entry
= curr_entry
->vme_next
;
8418 if (tmp_entry
== vm_map_to_entry(curr_map
)) {
8419 /* no next entry at this level */
8420 } else if (tmp_entry
->vme_start
>= curr_max_offset
) {
8422 * tmp_entry is beyond the scope of what we mapped of
8423 * this submap in the upper level: ignore it.
8425 } else if ((next_entry
== NULL
) ||
8426 (tmp_entry
->vme_start
+ curr_offset
<=
8427 next_entry
->vme_start
+ next_offset
)) {
8429 * We didn't have a "next_entry" or this one is
8430 * closer to the address we're looking for:
8431 * use this "tmp_entry" as the new "next_entry".
8433 if (next_entry
!= NULL
) {
8434 /* unlock the last "next_map" */
8435 if (next_map
!= curr_map
&& not_in_kdp
) {
8436 vm_map_unlock_read(next_map
);
8439 next_entry
= tmp_entry
;
8440 next_map
= curr_map
;
8441 next_offset
= curr_offset
;
8442 next_depth
= curr_depth
;
8443 next_max_offset
= curr_max_offset
;
8446 if (!curr_entry
->is_sub_map
||
8447 curr_depth
>= user_max_depth
) {
8449 * We hit a leaf map or we reached the maximum depth
8450 * we could, so stop looking. Keep the current map
8457 * Get down to the next submap level.
8461 * Lock the next level and unlock the current level,
8462 * unless we need to keep it locked to access the "next_entry"
8466 vm_map_lock_read(curr_entry
->object
.sub_map
);
8468 if (curr_map
== next_map
) {
8469 /* keep "next_map" locked in case we need it */
8471 /* release this map */
8472 vm_map_unlock_read(curr_map
);
8476 * Adjust the offset. "curr_entry" maps the submap
8477 * at relative address "curr_entry->vme_start" in the
8478 * curr_map but skips the first "curr_entry->offset"
8479 * bytes of the submap.
8480 * "curr_offset" always represents the offset of a virtual
8481 * address in the curr_map relative to the absolute address
8482 * space (i.e. the top-level VM map).
8485 (curr_entry
->vme_start
- curr_entry
->offset
);
8486 /* switch to the submap */
8487 curr_map
= curr_entry
->object
.sub_map
;
8490 * "curr_max_offset" allows us to keep track of the
8491 * portion of the submap that is actually mapped at this level:
8492 * the rest of that submap is irrelevant to us, since it's not
8494 * The relevant portion of the map starts at
8495 * "curr_entry->offset" up to the size of "curr_entry".
8498 curr_entry
->vme_end
- curr_entry
->vme_start
+
8503 if (curr_entry
== NULL
) {
8504 /* no VM region contains the address... */
8505 if (next_entry
== NULL
) {
8506 /* ... and no VM region follows it either */
8507 return KERN_INVALID_ADDRESS
;
8509 /* ... gather info about the next VM region */
8510 curr_entry
= next_entry
;
8511 curr_map
= next_map
; /* still locked ... */
8512 curr_offset
= next_offset
;
8513 curr_depth
= next_depth
;
8514 curr_max_offset
= next_max_offset
;
8516 /* we won't need "next_entry" after all */
8517 if (next_entry
!= NULL
) {
8518 /* release "next_map" */
8519 if (next_map
!= curr_map
&& not_in_kdp
) {
8520 vm_map_unlock_read(next_map
);
8528 next_max_offset
= 0;
8530 *nesting_depth
= curr_depth
;
8531 *size
= curr_entry
->vme_end
- curr_entry
->vme_start
;
8532 *address
= curr_entry
->vme_start
+ curr_offset
;
8534 if (look_for_pages
) {
8535 submap_info
->user_tag
= curr_entry
->alias
;
8536 submap_info
->offset
= curr_entry
->offset
;
8537 submap_info
->protection
= curr_entry
->protection
;
8538 submap_info
->inheritance
= curr_entry
->inheritance
;
8539 submap_info
->max_protection
= curr_entry
->max_protection
;
8540 submap_info
->behavior
= curr_entry
->behavior
;
8541 submap_info
->user_wired_count
= curr_entry
->user_wired_count
;
8542 submap_info
->is_submap
= curr_entry
->is_sub_map
;
8543 submap_info
->object_id
= (uint32_t) curr_entry
->object
.vm_object
;
8545 short_info
->user_tag
= curr_entry
->alias
;
8546 short_info
->offset
= curr_entry
->offset
;
8547 short_info
->protection
= curr_entry
->protection
;
8548 short_info
->inheritance
= curr_entry
->inheritance
;
8549 short_info
->max_protection
= curr_entry
->max_protection
;
8550 short_info
->behavior
= curr_entry
->behavior
;
8551 short_info
->user_wired_count
= curr_entry
->user_wired_count
;
8552 short_info
->is_submap
= curr_entry
->is_sub_map
;
8553 short_info
->object_id
= (uint32_t) curr_entry
->object
.vm_object
;
8556 extended
.pages_resident
= 0;
8557 extended
.pages_swapped_out
= 0;
8558 extended
.pages_shared_now_private
= 0;
8559 extended
.pages_dirtied
= 0;
8560 extended
.external_pager
= 0;
8561 extended
.shadow_depth
= 0;
8564 if (!curr_entry
->is_sub_map
) {
8565 vm_map_region_walk(curr_map
,
8566 curr_entry
->vme_start
,
8569 (curr_entry
->vme_end
-
8570 curr_entry
->vme_start
),
8573 if (extended
.external_pager
&&
8574 extended
.ref_count
== 2 &&
8575 extended
.share_mode
== SM_SHARED
) {
8576 extended
.share_mode
= SM_PRIVATE
;
8579 if (curr_entry
->use_pmap
) {
8580 extended
.share_mode
= SM_TRUESHARED
;
8582 extended
.share_mode
= SM_PRIVATE
;
8584 extended
.ref_count
=
8585 curr_entry
->object
.sub_map
->ref_count
;
8589 if (look_for_pages
) {
8590 submap_info
->pages_resident
= extended
.pages_resident
;
8591 submap_info
->pages_swapped_out
= extended
.pages_swapped_out
;
8592 submap_info
->pages_shared_now_private
=
8593 extended
.pages_shared_now_private
;
8594 submap_info
->pages_dirtied
= extended
.pages_dirtied
;
8595 submap_info
->external_pager
= extended
.external_pager
;
8596 submap_info
->shadow_depth
= extended
.shadow_depth
;
8597 submap_info
->share_mode
= extended
.share_mode
;
8598 submap_info
->ref_count
= extended
.ref_count
;
8600 short_info
->external_pager
= extended
.external_pager
;
8601 short_info
->shadow_depth
= extended
.shadow_depth
;
8602 short_info
->share_mode
= extended
.share_mode
;
8603 short_info
->ref_count
= extended
.ref_count
;
8607 vm_map_unlock_read(curr_map
);
8610 return KERN_SUCCESS
;
8616 * User call to obtain information about a region in
8617 * a task's address map. Currently, only one flavor is
8620 * XXX The reserved and behavior fields cannot be filled
8621 * in until the vm merge from the IK is completed, and
8622 * vm_reserve is implemented.
8628 vm_map_offset_t
*address
, /* IN/OUT */
8629 vm_map_size_t
*size
, /* OUT */
8630 vm_region_flavor_t flavor
, /* IN */
8631 vm_region_info_t info
, /* OUT */
8632 mach_msg_type_number_t
*count
, /* IN/OUT */
8633 mach_port_t
*object_name
) /* OUT */
8635 vm_map_entry_t tmp_entry
;
8636 vm_map_entry_t entry
;
8637 vm_map_offset_t start
;
8639 if (map
== VM_MAP_NULL
)
8640 return(KERN_INVALID_ARGUMENT
);
8644 case VM_REGION_BASIC_INFO
:
8645 /* legacy for old 32-bit objects info */
8647 vm_region_basic_info_t basic
;
8649 if (*count
< VM_REGION_BASIC_INFO_COUNT
)
8650 return(KERN_INVALID_ARGUMENT
);
8652 basic
= (vm_region_basic_info_t
) info
;
8653 *count
= VM_REGION_BASIC_INFO_COUNT
;
8655 vm_map_lock_read(map
);
8658 if (!vm_map_lookup_entry(map
, start
, &tmp_entry
)) {
8659 if ((entry
= tmp_entry
->vme_next
) == vm_map_to_entry(map
)) {
8660 vm_map_unlock_read(map
);
8661 return(KERN_INVALID_ADDRESS
);
8667 start
= entry
->vme_start
;
8669 basic
->offset
= (uint32_t)entry
->offset
;
8670 basic
->protection
= entry
->protection
;
8671 basic
->inheritance
= entry
->inheritance
;
8672 basic
->max_protection
= entry
->max_protection
;
8673 basic
->behavior
= entry
->behavior
;
8674 basic
->user_wired_count
= entry
->user_wired_count
;
8675 basic
->reserved
= entry
->is_sub_map
;
8677 *size
= (entry
->vme_end
- start
);
8679 if (object_name
) *object_name
= IP_NULL
;
8680 if (entry
->is_sub_map
) {
8681 basic
->shared
= FALSE
;
8683 basic
->shared
= entry
->is_shared
;
8686 vm_map_unlock_read(map
);
8687 return(KERN_SUCCESS
);
8690 case VM_REGION_BASIC_INFO_64
:
8692 vm_region_basic_info_64_t basic
;
8694 if (*count
< VM_REGION_BASIC_INFO_COUNT_64
)
8695 return(KERN_INVALID_ARGUMENT
);
8697 basic
= (vm_region_basic_info_64_t
) info
;
8698 *count
= VM_REGION_BASIC_INFO_COUNT_64
;
8700 vm_map_lock_read(map
);
8703 if (!vm_map_lookup_entry(map
, start
, &tmp_entry
)) {
8704 if ((entry
= tmp_entry
->vme_next
) == vm_map_to_entry(map
)) {
8705 vm_map_unlock_read(map
);
8706 return(KERN_INVALID_ADDRESS
);
8712 start
= entry
->vme_start
;
8714 basic
->offset
= entry
->offset
;
8715 basic
->protection
= entry
->protection
;
8716 basic
->inheritance
= entry
->inheritance
;
8717 basic
->max_protection
= entry
->max_protection
;
8718 basic
->behavior
= entry
->behavior
;
8719 basic
->user_wired_count
= entry
->user_wired_count
;
8720 basic
->reserved
= entry
->is_sub_map
;
8722 *size
= (entry
->vme_end
- start
);
8724 if (object_name
) *object_name
= IP_NULL
;
8725 if (entry
->is_sub_map
) {
8726 basic
->shared
= FALSE
;
8728 basic
->shared
= entry
->is_shared
;
8731 vm_map_unlock_read(map
);
8732 return(KERN_SUCCESS
);
8734 case VM_REGION_EXTENDED_INFO
:
8736 vm_region_extended_info_t extended
;
8738 if (*count
< VM_REGION_EXTENDED_INFO_COUNT
)
8739 return(KERN_INVALID_ARGUMENT
);
8741 extended
= (vm_region_extended_info_t
) info
;
8742 *count
= VM_REGION_EXTENDED_INFO_COUNT
;
8744 vm_map_lock_read(map
);
8747 if (!vm_map_lookup_entry(map
, start
, &tmp_entry
)) {
8748 if ((entry
= tmp_entry
->vme_next
) == vm_map_to_entry(map
)) {
8749 vm_map_unlock_read(map
);
8750 return(KERN_INVALID_ADDRESS
);
8755 start
= entry
->vme_start
;
8757 extended
->protection
= entry
->protection
;
8758 extended
->user_tag
= entry
->alias
;
8759 extended
->pages_resident
= 0;
8760 extended
->pages_swapped_out
= 0;
8761 extended
->pages_shared_now_private
= 0;
8762 extended
->pages_dirtied
= 0;
8763 extended
->external_pager
= 0;
8764 extended
->shadow_depth
= 0;
8766 vm_map_region_walk(map
, start
, entry
, entry
->offset
, entry
->vme_end
- start
, extended
, TRUE
);
8768 if (extended
->external_pager
&& extended
->ref_count
== 2 && extended
->share_mode
== SM_SHARED
)
8769 extended
->share_mode
= SM_PRIVATE
;
8772 *object_name
= IP_NULL
;
8774 *size
= (entry
->vme_end
- start
);
8776 vm_map_unlock_read(map
);
8777 return(KERN_SUCCESS
);
8779 case VM_REGION_TOP_INFO
:
8781 vm_region_top_info_t top
;
8783 if (*count
< VM_REGION_TOP_INFO_COUNT
)
8784 return(KERN_INVALID_ARGUMENT
);
8786 top
= (vm_region_top_info_t
) info
;
8787 *count
= VM_REGION_TOP_INFO_COUNT
;
8789 vm_map_lock_read(map
);
8792 if (!vm_map_lookup_entry(map
, start
, &tmp_entry
)) {
8793 if ((entry
= tmp_entry
->vme_next
) == vm_map_to_entry(map
)) {
8794 vm_map_unlock_read(map
);
8795 return(KERN_INVALID_ADDRESS
);
8801 start
= entry
->vme_start
;
8803 top
->private_pages_resident
= 0;
8804 top
->shared_pages_resident
= 0;
8806 vm_map_region_top_walk(entry
, top
);
8809 *object_name
= IP_NULL
;
8811 *size
= (entry
->vme_end
- start
);
8813 vm_map_unlock_read(map
);
8814 return(KERN_SUCCESS
);
8817 return(KERN_INVALID_ARGUMENT
);
8821 #define min(a, b) (((a) < (b)) ? (a) : (b))
8824 vm_map_region_top_walk(
8825 vm_map_entry_t entry
,
8826 vm_region_top_info_t top
)
8829 if (entry
->object
.vm_object
== 0 || entry
->is_sub_map
) {
8830 top
->share_mode
= SM_EMPTY
;
8837 struct vm_object
*obj
, *tmp_obj
;
8839 uint32_t entry_size
;
8841 entry_size
= (entry
->vme_end
- entry
->vme_start
) / PAGE_SIZE
;
8843 obj
= entry
->object
.vm_object
;
8845 vm_object_lock(obj
);
8847 if ((ref_count
= obj
->ref_count
) > 1 && obj
->paging_in_progress
)
8852 top
->private_pages_resident
= min(obj
->resident_page_count
, entry_size
);
8854 top
->shared_pages_resident
= min(obj
->resident_page_count
, entry_size
);
8855 top
->ref_count
= ref_count
;
8856 top
->share_mode
= SM_COW
;
8858 while ((tmp_obj
= obj
->shadow
)) {
8859 vm_object_lock(tmp_obj
);
8860 vm_object_unlock(obj
);
8863 if ((ref_count
= obj
->ref_count
) > 1 && obj
->paging_in_progress
)
8866 top
->shared_pages_resident
+= min(obj
->resident_page_count
, entry_size
);
8867 top
->ref_count
+= ref_count
- 1;
8870 if (entry
->needs_copy
) {
8871 top
->share_mode
= SM_COW
;
8872 top
->shared_pages_resident
= min(obj
->resident_page_count
, entry_size
);
8874 if (ref_count
== 1 ||
8875 (ref_count
== 2 && !(obj
->pager_trusted
) && !(obj
->internal
))) {
8876 top
->share_mode
= SM_PRIVATE
;
8877 top
->private_pages_resident
= min(obj
->resident_page_count
, entry_size
);
8879 top
->share_mode
= SM_SHARED
;
8880 top
->shared_pages_resident
= min(obj
->resident_page_count
, entry_size
);
8883 top
->ref_count
= ref_count
;
8885 top
->obj_id
= (int)obj
;
8887 vm_object_unlock(obj
);
8895 vm_map_entry_t entry
,
8896 vm_object_offset_t offset
,
8897 vm_object_size_t range
,
8898 vm_region_extended_info_t extended
,
8899 boolean_t look_for_pages
)
8901 register struct vm_object
*obj
, *tmp_obj
;
8902 register vm_map_offset_t last_offset
;
8904 register int ref_count
;
8905 struct vm_object
*shadow_object
;
8908 if ((entry
->object
.vm_object
== 0) ||
8909 (entry
->is_sub_map
) ||
8910 (entry
->object
.vm_object
->phys_contiguous
)) {
8911 extended
->share_mode
= SM_EMPTY
;
8912 extended
->ref_count
= 0;
8916 obj
= entry
->object
.vm_object
;
8918 vm_object_lock(obj
);
8920 if ((ref_count
= obj
->ref_count
) > 1 && obj
->paging_in_progress
)
8923 if (look_for_pages
) {
8924 for (last_offset
= offset
+ range
;
8925 offset
< last_offset
;
8926 offset
+= PAGE_SIZE_64
, va
+= PAGE_SIZE
)
8927 vm_map_region_look_for_page(map
, va
, obj
,
8932 shadow_object
= obj
->shadow
;
8934 if (shadow_object
!= VM_OBJECT_NULL
) {
8935 vm_object_lock(shadow_object
);
8937 shadow_object
!= VM_OBJECT_NULL
;
8939 vm_object_t next_shadow
;
8941 next_shadow
= shadow_object
->shadow
;
8943 vm_object_lock(next_shadow
);
8945 vm_object_unlock(shadow_object
);
8946 shadow_object
= next_shadow
;
8949 extended
->shadow_depth
= shadow_depth
;
8951 if (extended
->shadow_depth
|| entry
->needs_copy
)
8952 extended
->share_mode
= SM_COW
;
8955 extended
->share_mode
= SM_PRIVATE
;
8957 if (obj
->true_share
)
8958 extended
->share_mode
= SM_TRUESHARED
;
8960 extended
->share_mode
= SM_SHARED
;
8963 extended
->ref_count
= ref_count
- extended
->shadow_depth
;
8965 for (i
= 0; i
< extended
->shadow_depth
; i
++) {
8966 if ((tmp_obj
= obj
->shadow
) == 0)
8968 vm_object_lock(tmp_obj
);
8969 vm_object_unlock(obj
);
8971 if ((ref_count
= tmp_obj
->ref_count
) > 1 && tmp_obj
->paging_in_progress
)
8974 extended
->ref_count
+= ref_count
;
8977 vm_object_unlock(obj
);
8979 if (extended
->share_mode
== SM_SHARED
) {
8980 register vm_map_entry_t cur
;
8981 register vm_map_entry_t last
;
8984 obj
= entry
->object
.vm_object
;
8985 last
= vm_map_to_entry(map
);
8988 if ((ref_count
= obj
->ref_count
) > 1 && obj
->paging_in_progress
)
8990 for (cur
= vm_map_first_entry(map
); cur
!= last
; cur
= cur
->vme_next
)
8991 my_refs
+= vm_map_region_count_obj_refs(cur
, obj
);
8993 if (my_refs
== ref_count
)
8994 extended
->share_mode
= SM_PRIVATE_ALIASED
;
8995 else if (my_refs
> 1)
8996 extended
->share_mode
= SM_SHARED_ALIASED
;
9002 /* object is locked on entry and locked on return */
9006 vm_map_region_look_for_page(
9007 __unused vm_map_t map
,
9008 __unused vm_map_offset_t va
,
9010 vm_object_offset_t offset
,
9013 vm_region_extended_info_t extended
)
9015 register vm_page_t p
;
9016 register vm_object_t shadow
;
9017 register int ref_count
;
9018 vm_object_t caller_object
;
9022 shadow
= object
->shadow
;
9023 caller_object
= object
;
9028 if ( !(object
->pager_trusted
) && !(object
->internal
))
9029 extended
->external_pager
= 1;
9031 if ((p
= vm_page_lookup(object
, offset
)) != VM_PAGE_NULL
) {
9032 if (shadow
&& (max_refcnt
== 1))
9033 extended
->pages_shared_now_private
++;
9035 if (!p
->fictitious
&&
9036 (p
->dirty
|| pmap_is_modified(p
->phys_page
)))
9037 extended
->pages_dirtied
++;
9039 extended
->pages_resident
++;
9041 if(object
!= caller_object
)
9042 vm_object_unlock(object
);
9047 if (object
->existence_map
) {
9048 if (vm_external_state_get(object
->existence_map
, offset
) == VM_EXTERNAL_STATE_EXISTS
) {
9050 extended
->pages_swapped_out
++;
9052 if(object
!= caller_object
)
9053 vm_object_unlock(object
);
9057 } else if (object
->internal
&&
9059 !object
->terminating
&&
9060 object
->pager_ready
) {
9062 memory_object_t pager
;
9064 vm_object_paging_begin(object
);
9065 pager
= object
->pager
;
9066 vm_object_unlock(object
);
9068 kr
= memory_object_data_request(
9070 offset
+ object
->paging_offset
,
9071 0, /* just poke the pager */
9075 vm_object_lock(object
);
9076 vm_object_paging_end(object
);
9078 if (kr
== KERN_SUCCESS
) {
9079 /* the pager has that page */
9080 extended
->pages_swapped_out
++;
9081 if (object
!= caller_object
)
9082 vm_object_unlock(object
);
9086 #endif /* MACH_PAGEMAP */
9089 vm_object_lock(shadow
);
9091 if ((ref_count
= shadow
->ref_count
) > 1 && shadow
->paging_in_progress
)
9094 if (++depth
> extended
->shadow_depth
)
9095 extended
->shadow_depth
= depth
;
9097 if (ref_count
> max_refcnt
)
9098 max_refcnt
= ref_count
;
9100 if(object
!= caller_object
)
9101 vm_object_unlock(object
);
9103 offset
= offset
+ object
->shadow_offset
;
9105 shadow
= object
->shadow
;
9108 if(object
!= caller_object
)
9109 vm_object_unlock(object
);
9115 vm_map_region_count_obj_refs(
9116 vm_map_entry_t entry
,
9119 register int ref_count
;
9120 register vm_object_t chk_obj
;
9121 register vm_object_t tmp_obj
;
9123 if (entry
->object
.vm_object
== 0)
9126 if (entry
->is_sub_map
)
9131 chk_obj
= entry
->object
.vm_object
;
9132 vm_object_lock(chk_obj
);
9135 if (chk_obj
== object
)
9137 tmp_obj
= chk_obj
->shadow
;
9139 vm_object_lock(tmp_obj
);
9140 vm_object_unlock(chk_obj
);
9150 * Routine: vm_map_simplify
9153 * Attempt to simplify the map representation in
9154 * the vicinity of the given starting address.
9156 * This routine is intended primarily to keep the
9157 * kernel maps more compact -- they generally don't
9158 * benefit from the "expand a map entry" technology
9159 * at allocation time because the adjacent entry
9160 * is often wired down.
9163 vm_map_simplify_entry(
9165 vm_map_entry_t this_entry
)
9167 vm_map_entry_t prev_entry
;
9169 counter(c_vm_map_simplify_entry_called
++);
9171 prev_entry
= this_entry
->vme_prev
;
9173 if ((this_entry
!= vm_map_to_entry(map
)) &&
9174 (prev_entry
!= vm_map_to_entry(map
)) &&
9176 (prev_entry
->vme_end
== this_entry
->vme_start
) &&
9178 (prev_entry
->is_sub_map
== this_entry
->is_sub_map
) &&
9180 (prev_entry
->object
.vm_object
== this_entry
->object
.vm_object
) &&
9181 ((prev_entry
->offset
+ (prev_entry
->vme_end
-
9182 prev_entry
->vme_start
))
9183 == this_entry
->offset
) &&
9185 (prev_entry
->inheritance
== this_entry
->inheritance
) &&
9186 (prev_entry
->protection
== this_entry
->protection
) &&
9187 (prev_entry
->max_protection
== this_entry
->max_protection
) &&
9188 (prev_entry
->behavior
== this_entry
->behavior
) &&
9189 (prev_entry
->alias
== this_entry
->alias
) &&
9190 (prev_entry
->no_cache
== this_entry
->no_cache
) &&
9191 (prev_entry
->wired_count
== this_entry
->wired_count
) &&
9192 (prev_entry
->user_wired_count
== this_entry
->user_wired_count
) &&
9194 (prev_entry
->needs_copy
== this_entry
->needs_copy
) &&
9196 (prev_entry
->use_pmap
== FALSE
) &&
9197 (this_entry
->use_pmap
== FALSE
) &&
9198 (prev_entry
->in_transition
== FALSE
) &&
9199 (this_entry
->in_transition
== FALSE
) &&
9200 (prev_entry
->needs_wakeup
== FALSE
) &&
9201 (this_entry
->needs_wakeup
== FALSE
) &&
9202 (prev_entry
->is_shared
== FALSE
) &&
9203 (this_entry
->is_shared
== FALSE
)
9205 _vm_map_entry_unlink(&map
->hdr
, prev_entry
);
9206 this_entry
->vme_start
= prev_entry
->vme_start
;
9207 this_entry
->offset
= prev_entry
->offset
;
9208 if (prev_entry
->is_sub_map
) {
9209 vm_map_deallocate(prev_entry
->object
.sub_map
);
9211 vm_object_deallocate(prev_entry
->object
.vm_object
);
9213 vm_map_entry_dispose(map
, prev_entry
);
9214 SAVE_HINT_MAP_WRITE(map
, this_entry
);
9215 counter(c_vm_map_simplified
++);
9222 vm_map_offset_t start
)
9224 vm_map_entry_t this_entry
;
9227 if (vm_map_lookup_entry(map
, start
, &this_entry
)) {
9228 vm_map_simplify_entry(map
, this_entry
);
9229 vm_map_simplify_entry(map
, this_entry
->vme_next
);
9231 counter(c_vm_map_simplify_called
++);
9236 vm_map_simplify_range(
9238 vm_map_offset_t start
,
9239 vm_map_offset_t end
)
9241 vm_map_entry_t entry
;
9244 * The map should be locked (for "write") by the caller.
9248 /* invalid address range */
9252 start
= vm_map_trunc_page(start
);
9253 end
= vm_map_round_page(end
);
9255 if (!vm_map_lookup_entry(map
, start
, &entry
)) {
9256 /* "start" is not mapped and "entry" ends before "start" */
9257 if (entry
== vm_map_to_entry(map
)) {
9258 /* start with first entry in the map */
9259 entry
= vm_map_first_entry(map
);
9261 /* start with next entry */
9262 entry
= entry
->vme_next
;
9266 while (entry
!= vm_map_to_entry(map
) &&
9267 entry
->vme_start
<= end
) {
9268 /* try and coalesce "entry" with its previous entry */
9269 vm_map_simplify_entry(map
, entry
);
9270 entry
= entry
->vme_next
;
9276 * Routine: vm_map_machine_attribute
9278 * Provide machine-specific attributes to mappings,
9279 * such as cachability etc. for machines that provide
9280 * them. NUMA architectures and machines with big/strange
9281 * caches will use this.
9283 * Responsibilities for locking and checking are handled here,
9284 * everything else in the pmap module. If any non-volatile
9285 * information must be kept, the pmap module should handle
9286 * it itself. [This assumes that attributes do not
9287 * need to be inherited, which seems ok to me]
9290 vm_map_machine_attribute(
9292 vm_map_offset_t start
,
9293 vm_map_offset_t end
,
9294 vm_machine_attribute_t attribute
,
9295 vm_machine_attribute_val_t
* value
) /* IN/OUT */
9298 vm_map_size_t sync_size
;
9299 vm_map_entry_t entry
;
9301 if (start
< vm_map_min(map
) || end
> vm_map_max(map
))
9302 return KERN_INVALID_ADDRESS
;
9304 /* Figure how much memory we need to flush (in page increments) */
9305 sync_size
= end
- start
;
9309 if (attribute
!= MATTR_CACHE
) {
9310 /* If we don't have to find physical addresses, we */
9311 /* don't have to do an explicit traversal here. */
9312 ret
= pmap_attribute(map
->pmap
, start
, end
-start
,
9318 ret
= KERN_SUCCESS
; /* Assume it all worked */
9321 if (vm_map_lookup_entry(map
, start
, &entry
)) {
9322 vm_map_size_t sub_size
;
9323 if((entry
->vme_end
- start
) > sync_size
) {
9324 sub_size
= sync_size
;
9327 sub_size
= entry
->vme_end
- start
;
9328 sync_size
-= sub_size
;
9330 if(entry
->is_sub_map
) {
9331 vm_map_offset_t sub_start
;
9332 vm_map_offset_t sub_end
;
9334 sub_start
= (start
- entry
->vme_start
)
9336 sub_end
= sub_start
+ sub_size
;
9337 vm_map_machine_attribute(
9338 entry
->object
.sub_map
,
9343 if(entry
->object
.vm_object
) {
9346 vm_object_t base_object
;
9347 vm_object_t last_object
;
9348 vm_object_offset_t offset
;
9349 vm_object_offset_t base_offset
;
9350 vm_map_size_t range
;
9352 offset
= (start
- entry
->vme_start
)
9354 base_offset
= offset
;
9355 object
= entry
->object
.vm_object
;
9356 base_object
= object
;
9359 vm_object_lock(object
);
9365 if (m
&& !m
->fictitious
) {
9367 pmap_attribute_cache_sync(
9372 } else if (object
->shadow
) {
9373 offset
= offset
+ object
->shadow_offset
;
9374 last_object
= object
;
9375 object
= object
->shadow
;
9376 vm_object_lock(last_object
->shadow
);
9377 vm_object_unlock(last_object
);
9382 if (base_object
!= object
) {
9383 vm_object_unlock(object
);
9384 vm_object_lock(base_object
);
9385 object
= base_object
;
9387 /* Bump to the next page */
9388 base_offset
+= PAGE_SIZE
;
9389 offset
= base_offset
;
9391 vm_object_unlock(object
);
9397 return KERN_FAILURE
;
9408 * vm_map_behavior_set:
9410 * Sets the paging reference behavior of the specified address
9411 * range in the target map. Paging reference behavior affects
9412 * how pagein operations resulting from faults on the map will be
9416 vm_map_behavior_set(
9418 vm_map_offset_t start
,
9419 vm_map_offset_t end
,
9420 vm_behavior_t new_behavior
)
9422 register vm_map_entry_t entry
;
9423 vm_map_entry_t temp_entry
;
9426 "vm_map_behavior_set, 0x%X start 0x%X end 0x%X behavior %d",
9427 (integer_t
)map
, start
, end
, new_behavior
, 0);
9429 switch (new_behavior
) {
9430 case VM_BEHAVIOR_DEFAULT
:
9431 case VM_BEHAVIOR_RANDOM
:
9432 case VM_BEHAVIOR_SEQUENTIAL
:
9433 case VM_BEHAVIOR_RSEQNTL
:
9435 case VM_BEHAVIOR_WILLNEED
:
9436 case VM_BEHAVIOR_DONTNEED
:
9437 new_behavior
= VM_BEHAVIOR_DEFAULT
;
9440 return(KERN_INVALID_ARGUMENT
);
9446 * The entire address range must be valid for the map.
9447 * Note that vm_map_range_check() does a
9448 * vm_map_lookup_entry() internally and returns the
9449 * entry containing the start of the address range if
9450 * the entire range is valid.
9452 if (vm_map_range_check(map
, start
, end
, &temp_entry
)) {
9454 vm_map_clip_start(map
, entry
, start
);
9458 return(KERN_INVALID_ADDRESS
);
9461 while ((entry
!= vm_map_to_entry(map
)) && (entry
->vme_start
< end
)) {
9462 vm_map_clip_end(map
, entry
, end
);
9463 assert(!entry
->use_pmap
);
9465 entry
->behavior
= new_behavior
;
9467 entry
= entry
->vme_next
;
9471 return(KERN_SUCCESS
);
9475 #include <mach_kdb.h>
9477 #include <ddb/db_output.h>
9478 #include <vm/vm_print.h>
9480 #define printf db_printf
9483 * Forward declarations for internal functions.
9485 extern void vm_map_links_print(
9486 struct vm_map_links
*links
);
9488 extern void vm_map_header_print(
9489 struct vm_map_header
*header
);
9491 extern void vm_map_entry_print(
9492 vm_map_entry_t entry
);
9494 extern void vm_follow_entry(
9495 vm_map_entry_t entry
);
9497 extern void vm_follow_map(
9501 * vm_map_links_print: [ debug ]
9505 struct vm_map_links
*links
)
9507 iprintf("prev = %08X next = %08X start = %016llX end = %016llX\n",
9510 (unsigned long long)links
->start
,
9511 (unsigned long long)links
->end
);
9515 * vm_map_header_print: [ debug ]
9518 vm_map_header_print(
9519 struct vm_map_header
*header
)
9521 vm_map_links_print(&header
->links
);
9522 iprintf("nentries = %08X, %sentries_pageable\n",
9524 (header
->entries_pageable
? "" : "!"));
9528 * vm_follow_entry: [ debug ]
9532 vm_map_entry_t entry
)
9536 iprintf("map entry %08X\n", entry
);
9540 shadows
= vm_follow_object(entry
->object
.vm_object
);
9541 iprintf("Total objects : %d\n",shadows
);
9547 * vm_map_entry_print: [ debug ]
9551 register vm_map_entry_t entry
)
9553 static const char *inheritance_name
[4] =
9554 { "share", "copy", "none", "?"};
9555 static const char *behavior_name
[4] =
9556 { "dflt", "rand", "seqtl", "rseqntl" };
9558 iprintf("map entry %08X - prev = %08X next = %08X\n", entry
, entry
->vme_prev
, entry
->vme_next
);
9562 vm_map_links_print(&entry
->links
);
9564 iprintf("start = %016llX end = %016llX - prot=%x/%x/%s\n",
9565 (unsigned long long)entry
->vme_start
,
9566 (unsigned long long)entry
->vme_end
,
9568 entry
->max_protection
,
9569 inheritance_name
[(entry
->inheritance
& 0x3)]);
9571 iprintf("behavior = %s, wired_count = %d, user_wired_count = %d\n",
9572 behavior_name
[(entry
->behavior
& 0x3)],
9574 entry
->user_wired_count
);
9575 iprintf("%sin_transition, %sneeds_wakeup\n",
9576 (entry
->in_transition
? "" : "!"),
9577 (entry
->needs_wakeup
? "" : "!"));
9579 if (entry
->is_sub_map
) {
9580 iprintf("submap = %08X - offset = %016llX\n",
9581 entry
->object
.sub_map
,
9582 (unsigned long long)entry
->offset
);
9584 iprintf("object = %08X offset = %016llX - ",
9585 entry
->object
.vm_object
,
9586 (unsigned long long)entry
->offset
);
9587 printf("%sis_shared, %sneeds_copy\n",
9588 (entry
->is_shared
? "" : "!"),
9589 (entry
->needs_copy
? "" : "!"));
9596 * vm_follow_map: [ debug ]
9602 register vm_map_entry_t entry
;
9604 iprintf("task map %08X\n", map
);
9608 for (entry
= vm_map_first_entry(map
);
9609 entry
&& entry
!= vm_map_to_entry(map
);
9610 entry
= entry
->vme_next
) {
9611 vm_follow_entry(entry
);
9618 * vm_map_print: [ debug ]
9624 register vm_map_entry_t entry
;
9628 #endif /* TASK_SWAPPER */
9630 map
= (vm_map_t
)(long)
9631 inmap
; /* Make sure we have the right type */
9633 iprintf("task map %08X\n", map
);
9637 vm_map_header_print(&map
->hdr
);
9639 iprintf("pmap = %08X size = %08X ref = %d hint = %08X first_free = %08X\n",
9646 iprintf("%swait_for_space, %swiring_required, timestamp = %d\n",
9647 (map
->wait_for_space
? "" : "!"),
9648 (map
->wiring_required
? "" : "!"),
9652 switch (map
->sw_state
) {
9663 iprintf("res = %d, sw_state = %s\n", map
->res_count
, swstate
);
9664 #endif /* TASK_SWAPPER */
9666 for (entry
= vm_map_first_entry(map
);
9667 entry
&& entry
!= vm_map_to_entry(map
);
9668 entry
= entry
->vme_next
) {
9669 vm_map_entry_print(entry
);
9676 * Routine: vm_map_copy_print
9678 * Pretty-print a copy object for ddb.
9686 vm_map_entry_t entry
;
9688 copy
= (vm_map_copy_t
)(long)
9689 incopy
; /* Make sure we have the right type */
9691 printf("copy object 0x%x\n", copy
);
9695 iprintf("type=%d", copy
->type
);
9696 switch (copy
->type
) {
9697 case VM_MAP_COPY_ENTRY_LIST
:
9698 printf("[entry_list]");
9701 case VM_MAP_COPY_OBJECT
:
9705 case VM_MAP_COPY_KERNEL_BUFFER
:
9706 printf("[kernel_buffer]");
9710 printf("[bad type]");
9713 printf(", offset=0x%llx", (unsigned long long)copy
->offset
);
9714 printf(", size=0x%x\n", copy
->size
);
9716 switch (copy
->type
) {
9717 case VM_MAP_COPY_ENTRY_LIST
:
9718 vm_map_header_print(©
->cpy_hdr
);
9719 for (entry
= vm_map_copy_first_entry(copy
);
9720 entry
&& entry
!= vm_map_copy_to_entry(copy
);
9721 entry
= entry
->vme_next
) {
9722 vm_map_entry_print(entry
);
9726 case VM_MAP_COPY_OBJECT
:
9727 iprintf("object=0x%x\n", copy
->cpy_object
);
9730 case VM_MAP_COPY_KERNEL_BUFFER
:
9731 iprintf("kernel buffer=0x%x", copy
->cpy_kdata
);
9732 printf(", kalloc_size=0x%x\n", copy
->cpy_kalloc_size
);
9741 * db_vm_map_total_size(map) [ debug ]
9743 * return the total virtual size (in bytes) of the map
9746 db_vm_map_total_size(
9749 vm_map_entry_t entry
;
9750 vm_map_size_t total
;
9753 map
= (vm_map_t
)(long)
9754 inmap
; /* Make sure we have the right type */
9757 for (entry
= vm_map_first_entry(map
);
9758 entry
!= vm_map_to_entry(map
);
9759 entry
= entry
->vme_next
) {
9760 total
+= entry
->vme_end
- entry
->vme_start
;
9766 #endif /* MACH_KDB */
9769 * Routine: vm_map_entry_insert
9771 * Descritpion: This routine inserts a new vm_entry in a locked map.
9774 vm_map_entry_insert(
9776 vm_map_entry_t insp_entry
,
9777 vm_map_offset_t start
,
9778 vm_map_offset_t end
,
9780 vm_object_offset_t offset
,
9781 boolean_t needs_copy
,
9782 boolean_t is_shared
,
9783 boolean_t in_transition
,
9784 vm_prot_t cur_protection
,
9785 vm_prot_t max_protection
,
9786 vm_behavior_t behavior
,
9787 vm_inherit_t inheritance
,
9788 unsigned wired_count
,
9791 vm_map_entry_t new_entry
;
9793 assert(insp_entry
!= (vm_map_entry_t
)0);
9795 new_entry
= vm_map_entry_create(map
);
9797 new_entry
->vme_start
= start
;
9798 new_entry
->vme_end
= end
;
9799 assert(page_aligned(new_entry
->vme_start
));
9800 assert(page_aligned(new_entry
->vme_end
));
9802 new_entry
->object
.vm_object
= object
;
9803 new_entry
->offset
= offset
;
9804 new_entry
->is_shared
= is_shared
;
9805 new_entry
->is_sub_map
= FALSE
;
9806 new_entry
->needs_copy
= needs_copy
;
9807 new_entry
->in_transition
= in_transition
;
9808 new_entry
->needs_wakeup
= FALSE
;
9809 new_entry
->inheritance
= inheritance
;
9810 new_entry
->protection
= cur_protection
;
9811 new_entry
->max_protection
= max_protection
;
9812 new_entry
->behavior
= behavior
;
9813 new_entry
->wired_count
= wired_count
;
9814 new_entry
->user_wired_count
= 0;
9815 new_entry
->use_pmap
= FALSE
;
9816 new_entry
->alias
= 0;
9817 new_entry
->no_cache
= no_cache
;
9820 * Insert the new entry into the list.
9823 vm_map_entry_link(map
, insp_entry
, new_entry
);
9824 map
->size
+= end
- start
;
9827 * Update the free space hint and the lookup hint.
9830 SAVE_HINT_MAP_WRITE(map
, new_entry
);
9835 * Routine: vm_map_remap_extract
9837 * Descritpion: This routine returns a vm_entry list from a map.
9839 static kern_return_t
9840 vm_map_remap_extract(
9842 vm_map_offset_t addr
,
9845 struct vm_map_header
*map_header
,
9846 vm_prot_t
*cur_protection
,
9847 vm_prot_t
*max_protection
,
9848 /* What, no behavior? */
9849 vm_inherit_t inheritance
,
9852 kern_return_t result
;
9853 vm_map_size_t mapped_size
;
9854 vm_map_size_t tmp_size
;
9855 vm_map_entry_t src_entry
; /* result of last map lookup */
9856 vm_map_entry_t new_entry
;
9857 vm_object_offset_t offset
;
9858 vm_map_offset_t map_address
;
9859 vm_map_offset_t src_start
; /* start of entry to map */
9860 vm_map_offset_t src_end
; /* end of region to be mapped */
9862 vm_map_version_t version
;
9863 boolean_t src_needs_copy
;
9864 boolean_t new_entry_needs_copy
;
9866 assert(map
!= VM_MAP_NULL
);
9867 assert(size
!= 0 && size
== vm_map_round_page(size
));
9868 assert(inheritance
== VM_INHERIT_NONE
||
9869 inheritance
== VM_INHERIT_COPY
||
9870 inheritance
== VM_INHERIT_SHARE
);
9873 * Compute start and end of region.
9875 src_start
= vm_map_trunc_page(addr
);
9876 src_end
= vm_map_round_page(src_start
+ size
);
9879 * Initialize map_header.
9881 map_header
->links
.next
= (struct vm_map_entry
*)&map_header
->links
;
9882 map_header
->links
.prev
= (struct vm_map_entry
*)&map_header
->links
;
9883 map_header
->nentries
= 0;
9884 map_header
->entries_pageable
= pageable
;
9886 *cur_protection
= VM_PROT_ALL
;
9887 *max_protection
= VM_PROT_ALL
;
9891 result
= KERN_SUCCESS
;
9894 * The specified source virtual space might correspond to
9895 * multiple map entries, need to loop on them.
9898 while (mapped_size
!= size
) {
9899 vm_map_size_t entry_size
;
9902 * Find the beginning of the region.
9904 if (! vm_map_lookup_entry(map
, src_start
, &src_entry
)) {
9905 result
= KERN_INVALID_ADDRESS
;
9909 if (src_start
< src_entry
->vme_start
||
9910 (mapped_size
&& src_start
!= src_entry
->vme_start
)) {
9911 result
= KERN_INVALID_ADDRESS
;
9915 if(src_entry
->is_sub_map
) {
9916 result
= KERN_INVALID_ADDRESS
;
9920 tmp_size
= size
- mapped_size
;
9921 if (src_end
> src_entry
->vme_end
)
9922 tmp_size
-= (src_end
- src_entry
->vme_end
);
9924 entry_size
= (vm_map_size_t
)(src_entry
->vme_end
-
9925 src_entry
->vme_start
);
9927 if(src_entry
->is_sub_map
) {
9928 vm_map_reference(src_entry
->object
.sub_map
);
9929 object
= VM_OBJECT_NULL
;
9931 object
= src_entry
->object
.vm_object
;
9933 if (object
== VM_OBJECT_NULL
) {
9934 object
= vm_object_allocate(entry_size
);
9935 src_entry
->offset
= 0;
9936 src_entry
->object
.vm_object
= object
;
9937 } else if (object
->copy_strategy
!=
9938 MEMORY_OBJECT_COPY_SYMMETRIC
) {
9940 * We are already using an asymmetric
9941 * copy, and therefore we already have
9944 assert(!src_entry
->needs_copy
);
9945 } else if (src_entry
->needs_copy
|| object
->shadowed
||
9946 (object
->internal
&& !object
->true_share
&&
9947 !src_entry
->is_shared
&&
9948 object
->size
> entry_size
)) {
9950 vm_object_shadow(&src_entry
->object
.vm_object
,
9954 if (!src_entry
->needs_copy
&&
9955 (src_entry
->protection
& VM_PROT_WRITE
)) {
9958 prot
= src_entry
->protection
& ~VM_PROT_WRITE
;
9960 if (override_nx(map
, src_entry
->alias
) && prot
)
9961 prot
|= VM_PROT_EXECUTE
;
9964 vm_object_pmap_protect(
9965 src_entry
->object
.vm_object
,
9969 src_entry
->vme_start
,
9972 pmap_protect(vm_map_pmap(map
),
9973 src_entry
->vme_start
,
9979 object
= src_entry
->object
.vm_object
;
9980 src_entry
->needs_copy
= FALSE
;
9984 vm_object_lock(object
);
9985 vm_object_reference_locked(object
); /* object ref. for new entry */
9986 if (object
->copy_strategy
==
9987 MEMORY_OBJECT_COPY_SYMMETRIC
) {
9988 object
->copy_strategy
=
9989 MEMORY_OBJECT_COPY_DELAY
;
9991 vm_object_unlock(object
);
9994 offset
= src_entry
->offset
+ (src_start
- src_entry
->vme_start
);
9996 new_entry
= _vm_map_entry_create(map_header
);
9997 vm_map_entry_copy(new_entry
, src_entry
);
9998 new_entry
->use_pmap
= FALSE
; /* clr address space specifics */
10000 new_entry
->vme_start
= map_address
;
10001 new_entry
->vme_end
= map_address
+ tmp_size
;
10002 new_entry
->inheritance
= inheritance
;
10003 new_entry
->offset
= offset
;
10006 * The new region has to be copied now if required.
10010 src_entry
->is_shared
= TRUE
;
10011 new_entry
->is_shared
= TRUE
;
10012 if (!(new_entry
->is_sub_map
))
10013 new_entry
->needs_copy
= FALSE
;
10015 } else if (src_entry
->is_sub_map
) {
10016 /* make this a COW sub_map if not already */
10017 new_entry
->needs_copy
= TRUE
;
10018 object
= VM_OBJECT_NULL
;
10019 } else if (src_entry
->wired_count
== 0 &&
10020 vm_object_copy_quickly(&new_entry
->object
.vm_object
,
10022 (new_entry
->vme_end
-
10023 new_entry
->vme_start
),
10025 &new_entry_needs_copy
)) {
10027 new_entry
->needs_copy
= new_entry_needs_copy
;
10028 new_entry
->is_shared
= FALSE
;
10031 * Handle copy_on_write semantics.
10033 if (src_needs_copy
&& !src_entry
->needs_copy
) {
10036 prot
= src_entry
->protection
& ~VM_PROT_WRITE
;
10038 if (override_nx(map
, src_entry
->alias
) && prot
)
10039 prot
|= VM_PROT_EXECUTE
;
10041 vm_object_pmap_protect(object
,
10044 ((src_entry
->is_shared
10046 PMAP_NULL
: map
->pmap
),
10047 src_entry
->vme_start
,
10050 src_entry
->needs_copy
= TRUE
;
10053 * Throw away the old object reference of the new entry.
10055 vm_object_deallocate(object
);
10058 new_entry
->is_shared
= FALSE
;
10061 * The map can be safely unlocked since we
10062 * already hold a reference on the object.
10064 * Record the timestamp of the map for later
10065 * verification, and unlock the map.
10067 version
.main_timestamp
= map
->timestamp
;
10068 vm_map_unlock(map
); /* Increments timestamp once! */
10071 * Perform the copy.
10073 if (src_entry
->wired_count
> 0) {
10074 vm_object_lock(object
);
10075 result
= vm_object_copy_slowly(
10080 &new_entry
->object
.vm_object
);
10082 new_entry
->offset
= 0;
10083 new_entry
->needs_copy
= FALSE
;
10085 result
= vm_object_copy_strategically(
10089 &new_entry
->object
.vm_object
,
10090 &new_entry
->offset
,
10091 &new_entry_needs_copy
);
10093 new_entry
->needs_copy
= new_entry_needs_copy
;
10097 * Throw away the old object reference of the new entry.
10099 vm_object_deallocate(object
);
10101 if (result
!= KERN_SUCCESS
&&
10102 result
!= KERN_MEMORY_RESTART_COPY
) {
10103 _vm_map_entry_dispose(map_header
, new_entry
);
10108 * Verify that the map has not substantially
10109 * changed while the copy was being made.
10113 if (version
.main_timestamp
+ 1 != map
->timestamp
) {
10115 * Simple version comparison failed.
10117 * Retry the lookup and verify that the
10118 * same object/offset are still present.
10120 vm_object_deallocate(new_entry
->
10122 _vm_map_entry_dispose(map_header
, new_entry
);
10123 if (result
== KERN_MEMORY_RESTART_COPY
)
10124 result
= KERN_SUCCESS
;
10128 if (result
== KERN_MEMORY_RESTART_COPY
) {
10129 vm_object_reference(object
);
10134 _vm_map_entry_link(map_header
,
10135 map_header
->links
.prev
, new_entry
);
10137 *cur_protection
&= src_entry
->protection
;
10138 *max_protection
&= src_entry
->max_protection
;
10140 map_address
+= tmp_size
;
10141 mapped_size
+= tmp_size
;
10142 src_start
+= tmp_size
;
10146 vm_map_unlock(map
);
10147 if (result
!= KERN_SUCCESS
) {
10149 * Free all allocated elements.
10151 for (src_entry
= map_header
->links
.next
;
10152 src_entry
!= (struct vm_map_entry
*)&map_header
->links
;
10153 src_entry
= new_entry
) {
10154 new_entry
= src_entry
->vme_next
;
10155 _vm_map_entry_unlink(map_header
, src_entry
);
10156 vm_object_deallocate(src_entry
->object
.vm_object
);
10157 _vm_map_entry_dispose(map_header
, src_entry
);
10164 * Routine: vm_remap
10166 * Map portion of a task's address space.
10167 * Mapped region must not overlap more than
10168 * one vm memory object. Protections and
10169 * inheritance attributes remain the same
10170 * as in the original task and are out parameters.
10171 * Source and Target task can be identical
10172 * Other attributes are identical as for vm_map()
10176 vm_map_t target_map
,
10177 vm_map_address_t
*address
,
10178 vm_map_size_t size
,
10179 vm_map_offset_t mask
,
10180 boolean_t anywhere
,
10182 vm_map_offset_t memory_address
,
10184 vm_prot_t
*cur_protection
,
10185 vm_prot_t
*max_protection
,
10186 vm_inherit_t inheritance
)
10188 kern_return_t result
;
10189 vm_map_entry_t entry
;
10190 vm_map_entry_t insp_entry
= VM_MAP_ENTRY_NULL
;
10191 vm_map_entry_t new_entry
;
10192 struct vm_map_header map_header
;
10194 if (target_map
== VM_MAP_NULL
)
10195 return KERN_INVALID_ARGUMENT
;
10197 switch (inheritance
) {
10198 case VM_INHERIT_NONE
:
10199 case VM_INHERIT_COPY
:
10200 case VM_INHERIT_SHARE
:
10201 if (size
!= 0 && src_map
!= VM_MAP_NULL
)
10205 return KERN_INVALID_ARGUMENT
;
10208 size
= vm_map_round_page(size
);
10210 result
= vm_map_remap_extract(src_map
, memory_address
,
10211 size
, copy
, &map_header
,
10218 if (result
!= KERN_SUCCESS
) {
10223 * Allocate/check a range of free virtual address
10224 * space for the target
10226 *address
= vm_map_trunc_page(*address
);
10227 vm_map_lock(target_map
);
10228 result
= vm_map_remap_range_allocate(target_map
, address
, size
,
10229 mask
, anywhere
, &insp_entry
);
10231 for (entry
= map_header
.links
.next
;
10232 entry
!= (struct vm_map_entry
*)&map_header
.links
;
10233 entry
= new_entry
) {
10234 new_entry
= entry
->vme_next
;
10235 _vm_map_entry_unlink(&map_header
, entry
);
10236 if (result
== KERN_SUCCESS
) {
10237 entry
->vme_start
+= *address
;
10238 entry
->vme_end
+= *address
;
10239 vm_map_entry_link(target_map
, insp_entry
, entry
);
10240 insp_entry
= entry
;
10242 if (!entry
->is_sub_map
) {
10243 vm_object_deallocate(entry
->object
.vm_object
);
10245 vm_map_deallocate(entry
->object
.sub_map
);
10247 _vm_map_entry_dispose(&map_header
, entry
);
10251 if (result
== KERN_SUCCESS
) {
10252 target_map
->size
+= size
;
10253 SAVE_HINT_MAP_WRITE(target_map
, insp_entry
);
10255 vm_map_unlock(target_map
);
10257 if (result
== KERN_SUCCESS
&& target_map
->wiring_required
)
10258 result
= vm_map_wire(target_map
, *address
,
10259 *address
+ size
, *cur_protection
, TRUE
);
10264 * Routine: vm_map_remap_range_allocate
10267 * Allocate a range in the specified virtual address map.
10268 * returns the address and the map entry just before the allocated
10271 * Map must be locked.
10274 static kern_return_t
10275 vm_map_remap_range_allocate(
10277 vm_map_address_t
*address
, /* IN/OUT */
10278 vm_map_size_t size
,
10279 vm_map_offset_t mask
,
10280 boolean_t anywhere
,
10281 vm_map_entry_t
*map_entry
) /* OUT */
10283 register vm_map_entry_t entry
;
10284 register vm_map_offset_t start
;
10285 register vm_map_offset_t end
;
10294 * Calculate the first possible address.
10297 if (start
< map
->min_offset
)
10298 start
= map
->min_offset
;
10299 if (start
> map
->max_offset
)
10300 return(KERN_NO_SPACE
);
10303 * Look for the first possible address;
10304 * if there's already something at this
10305 * address, we have to start after it.
10308 assert(first_free_is_valid(map
));
10309 if (start
== map
->min_offset
) {
10310 if ((entry
= map
->first_free
) != vm_map_to_entry(map
))
10311 start
= entry
->vme_end
;
10313 vm_map_entry_t tmp_entry
;
10314 if (vm_map_lookup_entry(map
, start
, &tmp_entry
))
10315 start
= tmp_entry
->vme_end
;
10320 * In any case, the "entry" always precedes
10321 * the proposed new region throughout the
10326 register vm_map_entry_t next
;
10329 * Find the end of the proposed new region.
10330 * Be sure we didn't go beyond the end, or
10331 * wrap around the address.
10334 end
= ((start
+ mask
) & ~mask
);
10336 return(KERN_NO_SPACE
);
10340 if ((end
> map
->max_offset
) || (end
< start
)) {
10341 if (map
->wait_for_space
) {
10342 if (size
<= (map
->max_offset
-
10343 map
->min_offset
)) {
10344 assert_wait((event_t
) map
, THREAD_INTERRUPTIBLE
);
10345 vm_map_unlock(map
);
10346 thread_block(THREAD_CONTINUE_NULL
);
10352 return(KERN_NO_SPACE
);
10356 * If there are no more entries, we must win.
10359 next
= entry
->vme_next
;
10360 if (next
== vm_map_to_entry(map
))
10364 * If there is another entry, it must be
10365 * after the end of the potential new region.
10368 if (next
->vme_start
>= end
)
10372 * Didn't fit -- move to the next entry.
10376 start
= entry
->vme_end
;
10380 vm_map_entry_t temp_entry
;
10384 * the address doesn't itself violate
10385 * the mask requirement.
10388 if ((start
& mask
) != 0)
10389 return(KERN_NO_SPACE
);
10393 * ... the address is within bounds
10396 end
= start
+ size
;
10398 if ((start
< map
->min_offset
) ||
10399 (end
> map
->max_offset
) ||
10401 return(KERN_INVALID_ADDRESS
);
10405 * ... the starting address isn't allocated
10408 if (vm_map_lookup_entry(map
, start
, &temp_entry
))
10409 return(KERN_NO_SPACE
);
10411 entry
= temp_entry
;
10414 * ... the next region doesn't overlap the
10418 if ((entry
->vme_next
!= vm_map_to_entry(map
)) &&
10419 (entry
->vme_next
->vme_start
< end
))
10420 return(KERN_NO_SPACE
);
10422 *map_entry
= entry
;
10423 return(KERN_SUCCESS
);
10429 * Set the address map for the current thread to the specified map
10437 thread_t thread
= current_thread();
10438 vm_map_t oldmap
= thread
->map
;
10440 mp_disable_preemption();
10441 mycpu
= cpu_number();
10444 * Deactivate the current map and activate the requested map
10446 PMAP_SWITCH_USER(thread
, map
, mycpu
);
10448 mp_enable_preemption();
10454 * Routine: vm_map_write_user
10457 * Copy out data from a kernel space into space in the
10458 * destination map. The space must already exist in the
10460 * NOTE: This routine should only be called by threads
10461 * which can block on a page fault. i.e. kernel mode user
10469 vm_map_address_t dst_addr
,
10472 kern_return_t kr
= KERN_SUCCESS
;
10474 if(current_map() == map
) {
10475 if (copyout(src_p
, dst_addr
, size
)) {
10476 kr
= KERN_INVALID_ADDRESS
;
10481 /* take on the identity of the target map while doing */
10484 vm_map_reference(map
);
10485 oldmap
= vm_map_switch(map
);
10486 if (copyout(src_p
, dst_addr
, size
)) {
10487 kr
= KERN_INVALID_ADDRESS
;
10489 vm_map_switch(oldmap
);
10490 vm_map_deallocate(map
);
10496 * Routine: vm_map_read_user
10499 * Copy in data from a user space source map into the
10500 * kernel map. The space must already exist in the
10502 * NOTE: This routine should only be called by threads
10503 * which can block on a page fault. i.e. kernel mode user
10510 vm_map_address_t src_addr
,
10514 kern_return_t kr
= KERN_SUCCESS
;
10516 if(current_map() == map
) {
10517 if (copyin(src_addr
, dst_p
, size
)) {
10518 kr
= KERN_INVALID_ADDRESS
;
10523 /* take on the identity of the target map while doing */
10526 vm_map_reference(map
);
10527 oldmap
= vm_map_switch(map
);
10528 if (copyin(src_addr
, dst_p
, size
)) {
10529 kr
= KERN_INVALID_ADDRESS
;
10531 vm_map_switch(oldmap
);
10532 vm_map_deallocate(map
);
10539 * vm_map_check_protection:
10541 * Assert that the target map allows the specified
10542 * privilege on the entire address region given.
10543 * The entire region must be allocated.
10546 vm_map_check_protection(vm_map_t map
, vm_map_offset_t start
,
10547 vm_map_offset_t end
, vm_prot_t protection
)
10549 vm_map_entry_t entry
;
10550 vm_map_entry_t tmp_entry
;
10554 if (start
< vm_map_min(map
) || end
> vm_map_max(map
) || start
> end
)
10556 vm_map_unlock(map
);
10560 if (!vm_map_lookup_entry(map
, start
, &tmp_entry
)) {
10561 vm_map_unlock(map
);
10567 while (start
< end
) {
10568 if (entry
== vm_map_to_entry(map
)) {
10569 vm_map_unlock(map
);
10574 * No holes allowed!
10577 if (start
< entry
->vme_start
) {
10578 vm_map_unlock(map
);
10583 * Check protection associated with entry.
10586 if ((entry
->protection
& protection
) != protection
) {
10587 vm_map_unlock(map
);
10591 /* go to next entry */
10593 start
= entry
->vme_end
;
10594 entry
= entry
->vme_next
;
10596 vm_map_unlock(map
);
10601 vm_map_purgable_control(
10603 vm_map_offset_t address
,
10604 vm_purgable_t control
,
10607 vm_map_entry_t entry
;
10608 vm_object_t object
;
10612 * Vet all the input parameters and current type and state of the
10613 * underlaying object. Return with an error if anything is amiss.
10615 if (map
== VM_MAP_NULL
)
10616 return(KERN_INVALID_ARGUMENT
);
10618 if (control
!= VM_PURGABLE_SET_STATE
&&
10619 control
!= VM_PURGABLE_GET_STATE
)
10620 return(KERN_INVALID_ARGUMENT
);
10622 if (control
== VM_PURGABLE_SET_STATE
&&
10623 (((*state
& ~(VM_PURGABLE_STATE_MASK
|VM_VOLATILE_ORDER_MASK
|VM_PURGABLE_ORDERING_MASK
|VM_PURGABLE_BEHAVIOR_MASK
|VM_VOLATILE_GROUP_MASK
)) != 0) ||
10624 ((*state
& VM_PURGABLE_STATE_MASK
) > VM_PURGABLE_STATE_MASK
)))
10625 return(KERN_INVALID_ARGUMENT
);
10629 if (!vm_map_lookup_entry(map
, address
, &entry
) || entry
->is_sub_map
) {
10632 * Must pass a valid non-submap address.
10634 vm_map_unlock(map
);
10635 return(KERN_INVALID_ADDRESS
);
10638 if ((entry
->protection
& VM_PROT_WRITE
) == 0) {
10640 * Can't apply purgable controls to something you can't write.
10642 vm_map_unlock(map
);
10643 return(KERN_PROTECTION_FAILURE
);
10646 object
= entry
->object
.vm_object
;
10647 if (object
== VM_OBJECT_NULL
) {
10649 * Object must already be present or it can't be purgable.
10651 vm_map_unlock(map
);
10652 return KERN_INVALID_ARGUMENT
;
10655 vm_object_lock(object
);
10657 if (entry
->offset
!= 0 ||
10658 entry
->vme_end
- entry
->vme_start
!= object
->size
) {
10660 * Can only apply purgable controls to the whole (existing)
10663 vm_map_unlock(map
);
10664 vm_object_unlock(object
);
10665 return KERN_INVALID_ARGUMENT
;
10668 vm_map_unlock(map
);
10670 kr
= vm_object_purgable_control(object
, control
, state
);
10672 vm_object_unlock(object
);
10679 vm_map_t target_map
,
10680 vm_map_offset_t offset
,
10684 vm_map_entry_t map_entry
;
10685 vm_object_t object
;
10688 kern_return_t retval
= KERN_SUCCESS
;
10689 boolean_t top_object
= TRUE
;
10694 vm_map_lock_read(target_map
);
10696 restart_page_query
:
10697 if (!vm_map_lookup_entry(target_map
, offset
, &map_entry
)) {
10698 vm_map_unlock_read(target_map
);
10699 return KERN_FAILURE
;
10701 offset
-= map_entry
->vme_start
; /* adjust to offset within entry */
10702 offset
+= map_entry
->offset
; /* adjust to target object offset */
10704 if (map_entry
->object
.vm_object
!= VM_OBJECT_NULL
) {
10705 if (!map_entry
->is_sub_map
) {
10706 object
= map_entry
->object
.vm_object
;
10710 sub_map
= map_entry
->object
.sub_map
;
10711 vm_map_lock_read(sub_map
);
10712 vm_map_unlock_read(target_map
);
10714 target_map
= sub_map
;
10715 goto restart_page_query
;
10718 vm_map_unlock_read(target_map
);
10719 return KERN_SUCCESS
;
10721 vm_object_lock(object
);
10722 vm_map_unlock_read(target_map
);
10725 m
= vm_page_lookup(object
, offset
);
10727 if (m
!= VM_PAGE_NULL
) {
10728 *disposition
|= VM_PAGE_QUERY_PAGE_PRESENT
;
10732 if (object
->existence_map
) {
10733 if (vm_external_state_get(object
->existence_map
, offset
)
10734 == VM_EXTERNAL_STATE_EXISTS
) {
10736 * this page has been paged out
10738 *disposition
|= VM_PAGE_QUERY_PAGE_PAGED_OUT
;
10743 if (object
->internal
&&
10745 !object
->terminating
&&
10746 object
->pager_ready
) {
10748 memory_object_t pager
;
10750 vm_object_paging_begin(object
);
10751 pager
= object
->pager
;
10752 vm_object_unlock(object
);
10754 kr
= memory_object_data_request(
10756 offset
+ object
->paging_offset
,
10757 0, /* just poke the pager */
10761 vm_object_lock(object
);
10762 vm_object_paging_end(object
);
10764 if (kr
== KERN_SUCCESS
) {
10766 * the pager has this page
10768 *disposition
|= VM_PAGE_QUERY_PAGE_PAGED_OUT
;
10772 if (object
->shadow
!= VM_OBJECT_NULL
) {
10773 vm_object_t shadow
;
10775 offset
+= object
->shadow_offset
;
10776 shadow
= object
->shadow
;
10778 vm_object_lock(shadow
);
10779 vm_object_unlock(object
);
10782 top_object
= FALSE
;
10784 if (!object
->internal
)
10787 retval
= KERN_FAILURE
;
10788 goto page_query_done
;
10792 /* The ref_count is not strictly accurate, it measures the number */
10793 /* of entities holding a ref on the object, they may not be mapping */
10794 /* the object or may not be mapping the section holding the */
10795 /* target page but its still a ball park number and though an over- */
10796 /* count, it picks up the copy-on-write cases */
10798 /* We could also get a picture of page sharing from pmap_attributes */
10799 /* but this would under count as only faulted-in mappings would */
10802 *ref_count
= object
->ref_count
;
10804 if (top_object
== TRUE
&& object
->shadow
)
10805 *disposition
|= VM_PAGE_QUERY_PAGE_COPIED
;
10807 if (m
== VM_PAGE_NULL
)
10808 goto page_query_done
;
10810 if (m
->fictitious
) {
10811 *disposition
|= VM_PAGE_QUERY_PAGE_FICTITIOUS
;
10812 goto page_query_done
;
10814 if (m
->dirty
|| pmap_is_modified(m
->phys_page
))
10815 *disposition
|= VM_PAGE_QUERY_PAGE_DIRTY
;
10817 if (m
->reference
|| pmap_is_referenced(m
->phys_page
))
10818 *disposition
|= VM_PAGE_QUERY_PAGE_REF
;
10820 if (m
->speculative
)
10821 *disposition
|= VM_PAGE_QUERY_PAGE_SPECULATIVE
;
10824 vm_object_unlock(object
);
10832 * Synchronises the memory range specified with its backing store
10833 * image by either flushing or cleaning the contents to the appropriate
10834 * memory manager engaging in a memory object synchronize dialog with
10835 * the manager. The client doesn't return until the manager issues
10836 * m_o_s_completed message. MIG Magically converts user task parameter
10837 * to the task's address map.
10839 * interpretation of sync_flags
10840 * VM_SYNC_INVALIDATE - discard pages, only return precious
10841 * pages to manager.
10843 * VM_SYNC_INVALIDATE & (VM_SYNC_SYNCHRONOUS | VM_SYNC_ASYNCHRONOUS)
10844 * - discard pages, write dirty or precious
10845 * pages back to memory manager.
10847 * VM_SYNC_SYNCHRONOUS | VM_SYNC_ASYNCHRONOUS
10848 * - write dirty or precious pages back to
10849 * the memory manager.
10851 * VM_SYNC_CONTIGUOUS - does everything normally, but if there
10852 * is a hole in the region, and we would
10853 * have returned KERN_SUCCESS, return
10854 * KERN_INVALID_ADDRESS instead.
10857 * The memory object attributes have not yet been implemented, this
10858 * function will have to deal with the invalidate attribute
10861 * KERN_INVALID_TASK Bad task parameter
10862 * KERN_INVALID_ARGUMENT both sync and async were specified.
10863 * KERN_SUCCESS The usual.
10864 * KERN_INVALID_ADDRESS There was a hole in the region.
10870 vm_map_address_t address
,
10871 vm_map_size_t size
,
10872 vm_sync_t sync_flags
)
10875 msync_req_t new_msr
;
10876 queue_chain_t req_q
; /* queue of requests for this msync */
10877 vm_map_entry_t entry
;
10878 vm_map_size_t amount_left
;
10879 vm_object_offset_t offset
;
10880 boolean_t do_sync_req
;
10881 boolean_t modifiable
;
10882 boolean_t had_hole
= FALSE
;
10883 memory_object_t pager
;
10885 if ((sync_flags
& VM_SYNC_ASYNCHRONOUS
) &&
10886 (sync_flags
& VM_SYNC_SYNCHRONOUS
))
10887 return(KERN_INVALID_ARGUMENT
);
10890 * align address and size on page boundaries
10892 size
= vm_map_round_page(address
+ size
) - vm_map_trunc_page(address
);
10893 address
= vm_map_trunc_page(address
);
10895 if (map
== VM_MAP_NULL
)
10896 return(KERN_INVALID_TASK
);
10899 return(KERN_SUCCESS
);
10901 queue_init(&req_q
);
10902 amount_left
= size
;
10904 while (amount_left
> 0) {
10905 vm_object_size_t flush_size
;
10906 vm_object_t object
;
10909 if (!vm_map_lookup_entry(map
,
10910 vm_map_trunc_page(address
), &entry
)) {
10912 vm_map_size_t skip
;
10915 * hole in the address map.
10920 * Check for empty map.
10922 if (entry
== vm_map_to_entry(map
) &&
10923 entry
->vme_next
== entry
) {
10924 vm_map_unlock(map
);
10928 * Check that we don't wrap and that
10929 * we have at least one real map entry.
10931 if ((map
->hdr
.nentries
== 0) ||
10932 (entry
->vme_next
->vme_start
< address
)) {
10933 vm_map_unlock(map
);
10937 * Move up to the next entry if needed
10939 skip
= (entry
->vme_next
->vme_start
- address
);
10940 if (skip
>= amount_left
)
10943 amount_left
-= skip
;
10944 address
= entry
->vme_next
->vme_start
;
10945 vm_map_unlock(map
);
10949 offset
= address
- entry
->vme_start
;
10952 * do we have more to flush than is contained in this
10955 if (amount_left
+ entry
->vme_start
+ offset
> entry
->vme_end
) {
10956 flush_size
= entry
->vme_end
-
10957 (entry
->vme_start
+ offset
);
10959 flush_size
= amount_left
;
10961 amount_left
-= flush_size
;
10962 address
+= flush_size
;
10964 if (entry
->is_sub_map
== TRUE
) {
10965 vm_map_t local_map
;
10966 vm_map_offset_t local_offset
;
10968 local_map
= entry
->object
.sub_map
;
10969 local_offset
= entry
->offset
;
10970 vm_map_unlock(map
);
10975 sync_flags
) == KERN_INVALID_ADDRESS
) {
10980 object
= entry
->object
.vm_object
;
10983 * We can't sync this object if the object has not been
10986 if (object
== VM_OBJECT_NULL
) {
10987 vm_map_unlock(map
);
10990 offset
+= entry
->offset
;
10991 modifiable
= (entry
->protection
& VM_PROT_WRITE
)
10994 vm_object_lock(object
);
10996 if (sync_flags
& (VM_SYNC_KILLPAGES
| VM_SYNC_DEACTIVATE
)) {
10997 boolean_t kill_pages
= 0;
10999 if (sync_flags
& VM_SYNC_KILLPAGES
) {
11000 if (object
->ref_count
== 1 && !entry
->needs_copy
&& !object
->shadow
)
11005 if (kill_pages
!= -1)
11006 vm_object_deactivate_pages(object
, offset
,
11007 (vm_object_size_t
)flush_size
, kill_pages
);
11008 vm_object_unlock(object
);
11009 vm_map_unlock(map
);
11013 * We can't sync this object if there isn't a pager.
11014 * Don't bother to sync internal objects, since there can't
11015 * be any "permanent" storage for these objects anyway.
11017 if ((object
->pager
== MEMORY_OBJECT_NULL
) ||
11018 (object
->internal
) || (object
->private)) {
11019 vm_object_unlock(object
);
11020 vm_map_unlock(map
);
11024 * keep reference on the object until syncing is done
11026 vm_object_reference_locked(object
);
11027 vm_object_unlock(object
);
11029 vm_map_unlock(map
);
11031 do_sync_req
= vm_object_sync(object
,
11034 sync_flags
& VM_SYNC_INVALIDATE
,
11036 (sync_flags
& VM_SYNC_SYNCHRONOUS
||
11037 sync_flags
& VM_SYNC_ASYNCHRONOUS
)),
11038 sync_flags
& VM_SYNC_SYNCHRONOUS
);
11040 * only send a m_o_s if we returned pages or if the entry
11041 * is writable (ie dirty pages may have already been sent back)
11043 if (!do_sync_req
&& !modifiable
) {
11044 if ((sync_flags
& VM_SYNC_INVALIDATE
) && object
->resident_page_count
== 0) {
11046 * clear out the clustering and read-ahead hints
11048 vm_object_lock(object
);
11050 object
->pages_created
= 0;
11051 object
->pages_used
= 0;
11052 object
->sequential
= 0;
11053 object
->last_alloc
= 0;
11055 vm_object_unlock(object
);
11057 vm_object_deallocate(object
);
11060 msync_req_alloc(new_msr
);
11062 vm_object_lock(object
);
11063 offset
+= object
->paging_offset
;
11065 new_msr
->offset
= offset
;
11066 new_msr
->length
= flush_size
;
11067 new_msr
->object
= object
;
11068 new_msr
->flag
= VM_MSYNC_SYNCHRONIZING
;
11072 * We can't sync this object if there isn't a pager. The
11073 * pager can disappear anytime we're not holding the object
11074 * lock. So this has to be checked anytime we goto re_iterate.
11077 pager
= object
->pager
;
11079 if (pager
== MEMORY_OBJECT_NULL
) {
11080 vm_object_unlock(object
);
11081 vm_object_deallocate(object
);
11085 queue_iterate(&object
->msr_q
, msr
, msync_req_t
, msr_q
) {
11087 * need to check for overlapping entry, if found, wait
11088 * on overlapping msr to be done, then reiterate
11091 if (msr
->flag
== VM_MSYNC_SYNCHRONIZING
&&
11092 ((offset
>= msr
->offset
&&
11093 offset
< (msr
->offset
+ msr
->length
)) ||
11094 (msr
->offset
>= offset
&&
11095 msr
->offset
< (offset
+ flush_size
))))
11097 assert_wait((event_t
) msr
,THREAD_INTERRUPTIBLE
);
11099 vm_object_unlock(object
);
11100 thread_block(THREAD_CONTINUE_NULL
);
11101 vm_object_lock(object
);
11105 }/* queue_iterate */
11107 queue_enter(&object
->msr_q
, new_msr
, msync_req_t
, msr_q
);
11109 vm_object_paging_begin(object
);
11110 vm_object_unlock(object
);
11112 queue_enter(&req_q
, new_msr
, msync_req_t
, req_q
);
11114 (void) memory_object_synchronize(
11118 sync_flags
& ~VM_SYNC_CONTIGUOUS
);
11120 vm_object_lock(object
);
11121 vm_object_paging_end(object
);
11122 vm_object_unlock(object
);
11126 * wait for memory_object_sychronize_completed messages from pager(s)
11129 while (!queue_empty(&req_q
)) {
11130 msr
= (msync_req_t
)queue_first(&req_q
);
11132 while(msr
->flag
!= VM_MSYNC_DONE
) {
11133 assert_wait((event_t
) msr
, THREAD_INTERRUPTIBLE
);
11135 thread_block(THREAD_CONTINUE_NULL
);
11138 queue_remove(&req_q
, msr
, msync_req_t
, req_q
);
11140 vm_object_deallocate(msr
->object
);
11141 msync_req_free(msr
);
11142 }/* queue_iterate */
11144 /* for proper msync() behaviour */
11145 if (had_hole
== TRUE
&& (sync_flags
& VM_SYNC_CONTIGUOUS
))
11146 return(KERN_INVALID_ADDRESS
);
11148 return(KERN_SUCCESS
);
11152 * Routine: convert_port_entry_to_map
11154 * Convert from a port specifying an entry or a task
11155 * to a map. Doesn't consume the port ref; produces a map ref,
11156 * which may be null. Unlike convert_port_to_map, the
11157 * port may be task or a named entry backed.
11164 convert_port_entry_to_map(
11168 vm_named_entry_t named_entry
;
11169 uint32_t try_failed_count
= 0;
11171 if(IP_VALID(port
) && (ip_kotype(port
) == IKOT_NAMED_ENTRY
)) {
11174 if(ip_active(port
) && (ip_kotype(port
)
11175 == IKOT_NAMED_ENTRY
)) {
11177 (vm_named_entry_t
)port
->ip_kobject
;
11178 if (!(mutex_try(&(named_entry
)->Lock
))) {
11181 try_failed_count
++;
11182 mutex_pause(try_failed_count
);
11185 named_entry
->ref_count
++;
11186 mutex_unlock(&(named_entry
)->Lock
);
11188 if ((named_entry
->is_sub_map
) &&
11189 (named_entry
->protection
11190 & VM_PROT_WRITE
)) {
11191 map
= named_entry
->backing
.map
;
11193 mach_destroy_memory_entry(port
);
11194 return VM_MAP_NULL
;
11196 vm_map_reference_swap(map
);
11197 mach_destroy_memory_entry(port
);
11201 return VM_MAP_NULL
;
11205 map
= convert_port_to_map(port
);
11211 * Routine: convert_port_entry_to_object
11213 * Convert from a port specifying a named entry to an
11214 * object. Doesn't consume the port ref; produces a map ref,
11215 * which may be null.
11222 convert_port_entry_to_object(
11225 vm_object_t object
;
11226 vm_named_entry_t named_entry
;
11227 uint32_t try_failed_count
= 0;
11229 if(IP_VALID(port
) && (ip_kotype(port
) == IKOT_NAMED_ENTRY
)) {
11232 if(ip_active(port
) && (ip_kotype(port
)
11233 == IKOT_NAMED_ENTRY
)) {
11235 (vm_named_entry_t
)port
->ip_kobject
;
11236 if (!(mutex_try(&(named_entry
)->Lock
))) {
11239 try_failed_count
++;
11240 mutex_pause(try_failed_count
);
11243 named_entry
->ref_count
++;
11244 mutex_unlock(&(named_entry
)->Lock
);
11246 if ((!named_entry
->is_sub_map
) &&
11247 (!named_entry
->is_pager
) &&
11248 (named_entry
->protection
11249 & VM_PROT_WRITE
)) {
11250 object
= named_entry
->backing
.object
;
11252 mach_destroy_memory_entry(port
);
11253 return (vm_object_t
)NULL
;
11255 vm_object_reference(named_entry
->backing
.object
);
11256 mach_destroy_memory_entry(port
);
11260 return (vm_object_t
)NULL
;
11263 return (vm_object_t
)NULL
;
11270 * Export routines to other components for the things we access locally through
11277 return (current_map_fast());
11281 * vm_map_reference:
11283 * Most code internal to the osfmk will go through a
11284 * macro defining this. This is always here for the
11285 * use of other kernel components.
11287 #undef vm_map_reference
11290 register vm_map_t map
)
11292 if (map
== VM_MAP_NULL
)
11295 mutex_lock(&map
->s_lock
);
11297 assert(map
->res_count
> 0);
11298 assert(map
->ref_count
>= map
->res_count
);
11302 mutex_unlock(&map
->s_lock
);
11306 * vm_map_deallocate:
11308 * Removes a reference from the specified map,
11309 * destroying it if no references remain.
11310 * The map should not be locked.
11314 register vm_map_t map
)
11318 if (map
== VM_MAP_NULL
)
11321 mutex_lock(&map
->s_lock
);
11322 ref
= --map
->ref_count
;
11324 vm_map_res_deallocate(map
);
11325 mutex_unlock(&map
->s_lock
);
11328 assert(map
->ref_count
== 0);
11329 mutex_unlock(&map
->s_lock
);
11333 * The map residence count isn't decremented here because
11334 * the vm_map_delete below will traverse the entire map,
11335 * deleting entries, and the residence counts on objects
11336 * and sharing maps will go away then.
11340 vm_map_destroy(map
, VM_MAP_NO_FLAGS
);
11345 vm_map_disable_NX(vm_map_t map
)
11349 if (map
->pmap
== NULL
)
11352 pmap_disable_NX(map
->pmap
);
11355 /* XXX Consider making these constants (VM_MAX_ADDRESS and MACH_VM_MAX_ADDRESS)
11356 * more descriptive.
11359 vm_map_set_32bit(vm_map_t map
)
11361 map
->max_offset
= (vm_map_offset_t
)VM_MAX_ADDRESS
;
11366 vm_map_set_64bit(vm_map_t map
)
11368 map
->max_offset
= (vm_map_offset_t
)MACH_VM_MAX_ADDRESS
;
11372 vm_compute_max_offset(unsigned is64
)
11374 return (is64
? (vm_map_offset_t
)MACH_VM_MAX_ADDRESS
: (vm_map_offset_t
)VM_MAX_ADDRESS
);
11381 return map
->max_offset
> ((vm_map_offset_t
)VM_MAX_ADDRESS
);
11385 vm_map_has_4GB_pagezero(
11390 * We should lock the VM map (for read) here but we can get away
11391 * with it for now because there can't really be any race condition:
11392 * the VM map's min_offset is changed only when the VM map is created
11393 * and when the zero page is established (when the binary gets loaded),
11394 * and this routine gets called only when the task terminates and the
11395 * VM map is being torn down, and when a new map is created via
11396 * load_machfile()/execve().
11398 return (map
->min_offset
>= 0x100000000ULL
);
11402 vm_map_set_4GB_pagezero(vm_map_t map
)
11404 pmap_set_4GB_pagezero(map
->pmap
);
11408 vm_map_clear_4GB_pagezero(vm_map_t map
)
11410 pmap_clear_4GB_pagezero(map
->pmap
);
11414 * Raise a VM map's minimum offset.
11415 * To strictly enforce "page zero" reservation.
11418 vm_map_raise_min_offset(
11420 vm_map_offset_t new_min_offset
)
11422 vm_map_entry_t first_entry
;
11424 new_min_offset
= vm_map_round_page(new_min_offset
);
11428 if (new_min_offset
< map
->min_offset
) {
11430 * Can't move min_offset backwards, as that would expose
11431 * a part of the address space that was previously, and for
11432 * possibly good reasons, inaccessible.
11434 vm_map_unlock(map
);
11435 return KERN_INVALID_ADDRESS
;
11438 first_entry
= vm_map_first_entry(map
);
11439 if (first_entry
!= vm_map_to_entry(map
) &&
11440 first_entry
->vme_start
< new_min_offset
) {
11442 * Some memory was already allocated below the new
11443 * minimun offset. It's too late to change it now...
11445 vm_map_unlock(map
);
11446 return KERN_NO_SPACE
;
11449 map
->min_offset
= new_min_offset
;
11451 vm_map_unlock(map
);
11453 return KERN_SUCCESS
;
11457 * Set the limit on the maximum amount of user wired memory allowed for this map.
11458 * This is basically a copy of the MEMLOCK rlimit value maintained by the BSD side of
11459 * the kernel. The limits are checked in the mach VM side, so we keep a copy so we
11460 * don't have to reach over to the BSD data structures.
11464 vm_map_set_user_wire_limit(vm_map_t map
,
11467 map
->user_wire_limit
= limit
;