2 * Copyright (c) 2007 Apple Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 * File: vm/vm_shared_region.h
27 * protos and struct definitions for shared region
30 #ifndef _VM_SHARED_REGION_H_
31 #define _VM_SHARED_REGION_H_
35 #include <mach/vm_prot.h>
36 #include <mach/mach_types.h>
37 #include <mach/shared_region.h>
39 #include <kern/kern_types.h>
40 #include <kern/macro_help.h>
42 #include <vm/vm_map.h>
44 extern int shared_region_version
;
45 extern int shared_region_persistence
;
48 extern int shared_region_debug
;
49 #define SHARED_REGION_DEBUG(args) \
51 if (shared_region_debug) { \
56 #define SHARED_REGION_DEBUG(args)
59 extern int shared_region_trace_level
;
61 extern struct vm_shared_region
*init_task_shared_region
;
63 #define SHARED_REGION_TRACE_NONE_LVL 0 /* no trace */
64 #define SHARED_REGION_TRACE_ERROR_LVL 1 /* trace abnormal events */
65 #define SHARED_REGION_TRACE_INFO_LVL 2 /* trace all events */
66 #define SHARED_REGION_TRACE_DEBUG_LVL 3 /* extra traces for debug */
67 #define SHARED_REGION_TRACE(level, args) \
69 if (shared_region_trace_level >= level) { \
73 #define SHARED_REGION_TRACE_NONE(args)
74 #define SHARED_REGION_TRACE_ERROR(args) \
76 SHARED_REGION_TRACE(SHARED_REGION_TRACE_ERROR_LVL, \
79 #define SHARED_REGION_TRACE_INFO(args) \
81 SHARED_REGION_TRACE(SHARED_REGION_TRACE_INFO_LVL, \
84 #define SHARED_REGION_TRACE_DEBUG(args) \
86 SHARED_REGION_TRACE(SHARED_REGION_TRACE_DEBUG_LVL, \
90 typedef struct vm_shared_region
*vm_shared_region_t
;
92 #ifdef MACH_KERNEL_PRIVATE
94 #include <kern/queue.h>
95 #include <vm/vm_object.h>
96 #include <vm/memory_object.h>
98 #define PAGE_SIZE_FOR_SR_SLIDE 4096
100 /* Documentation for the slide info format can be found in the dyld project in
101 * the file 'launch-cache/dyld_cache_format.h'. */
103 typedef struct vm_shared_region_slide_info_entry_v1
*vm_shared_region_slide_info_entry_v1_t
;
104 struct vm_shared_region_slide_info_entry_v1
{
106 uint32_t toc_offset
; // offset from start of header to table-of-contents
107 uint32_t toc_count
; // number of entries in toc (same as number of pages in r/w mapping)
108 uint32_t entry_offset
;
109 uint32_t entry_count
;
110 // uint16_t toc[toc_count];
111 // entrybitmap entries[entries_count];
115 #define NUM_SLIDING_BITMAPS_PER_PAGE (0x1000/sizeof(int)/NBBY) /*128*/
116 typedef struct slide_info_entry_toc
*slide_info_entry_toc_t
;
117 struct slide_info_entry_toc
{
118 uint8_t entry
[NUM_SLIDING_BITMAPS_PER_PAGE
];
121 typedef struct vm_shared_region_slide_info_entry_v2
*vm_shared_region_slide_info_entry_v2_t
;
122 struct vm_shared_region_slide_info_entry_v2
{
125 uint32_t page_starts_offset
;
126 uint32_t page_starts_count
;
127 uint32_t page_extras_offset
;
128 uint32_t page_extras_count
;
129 uint64_t delta_mask
; // which (contiguous) set of bits contains the delta to the next rebase location
131 // uint16_t page_starts[page_starts_count];
132 // uint16_t page_extras[page_extras_count];
135 #define DYLD_CACHE_SLIDE_PAGE_ATTRS 0xC000 // high bits of uint16_t are flags
136 #define DYLD_CACHE_SLIDE_PAGE_ATTR_EXTRA 0x8000 // index is into extras array (not starts array)
137 #define DYLD_CACHE_SLIDE_PAGE_ATTR_NO_REBASE 0x4000 // page has no rebasing
138 #define DYLD_CACHE_SLIDE_PAGE_ATTR_END 0x8000 // last chain entry for page
139 #define DYLD_CACHE_SLIDE_PAGE_VALUE 0x3FFF // bitwise negation of DYLD_CACHE_SLIDE_PAGE_ATTRS
140 #define DYLD_CACHE_SLIDE_PAGE_OFFSET_SHIFT 2
142 typedef struct vm_shared_region_slide_info_entry_v3
*vm_shared_region_slide_info_entry_v3_t
;
143 struct vm_shared_region_slide_info_entry_v3
145 uint32_t version
; // currently 3
146 uint32_t page_size
; // currently 4096 (may also be 16384)
147 uint32_t page_starts_count
;
149 uint16_t page_starts
[/* page_starts_count */];
152 #define DYLD_CACHE_SLIDE_V3_PAGE_ATTR_NO_REBASE 0xFFFF // page has no rebasing
155 typedef struct vm_shared_region_slide_info_entry_v4
*vm_shared_region_slide_info_entry_v4_t
;
156 struct vm_shared_region_slide_info_entry_v4
{
157 uint32_t version
; // currently 4
158 uint32_t page_size
; // currently 4096 (may also be 16384)
159 uint32_t page_starts_offset
;
160 uint32_t page_starts_count
;
161 uint32_t page_extras_offset
;
162 uint32_t page_extras_count
;
163 uint64_t delta_mask
; // which (contiguous) set of bits contains the delta to the next rebase location (0xC0000000)
164 uint64_t value_add
; // base address of cache
165 // uint16_t page_starts[page_starts_count];
166 // uint16_t page_extras[page_extras_count];
169 #define DYLD_CACHE_SLIDE4_PAGE_NO_REBASE 0xFFFF // page has no rebasing
170 #define DYLD_CACHE_SLIDE4_PAGE_INDEX 0x7FFF // index into starts or extras
171 #define DYLD_CACHE_SLIDE4_PAGE_USE_EXTRA 0x8000 // index is into extras array (not starts array)
172 #define DYLD_CACHE_SLIDE4_PAGE_EXTRA_END 0x8000 // last chain entry for page
176 typedef union vm_shared_region_slide_info_entry
*vm_shared_region_slide_info_entry_t
;
177 union vm_shared_region_slide_info_entry
{
179 struct vm_shared_region_slide_info_entry_v1 v1
;
180 struct vm_shared_region_slide_info_entry_v2 v2
;
181 struct vm_shared_region_slide_info_entry_v3 v3
;
182 struct vm_shared_region_slide_info_entry_v4 v4
;
185 typedef struct vm_shared_region_slide_info
*vm_shared_region_slide_info_t
;
186 struct vm_shared_region_slide_info
{
187 mach_vm_address_t slid_address
;
188 mach_vm_offset_t start
;
189 mach_vm_offset_t end
;
191 vm_object_t slide_object
;
192 mach_vm_size_t slide_info_size
;
193 vm_shared_region_slide_info_entry_t slide_info_entry
;
196 /* address space shared region descriptor */
197 struct vm_shared_region
{
198 uint32_t sr_ref_count
;
201 cpu_type_t sr_cpu_type
;
202 cpu_subtype_t sr_cpu_subtype
;
204 boolean_t sr_mapping_in_progress
;
205 boolean_t sr_slide_in_progress
;
206 boolean_t sr_persists
;
208 ipc_port_t sr_mem_entry
;
209 mach_vm_offset_t sr_first_mapping
;
210 mach_vm_offset_t sr_base_address
;
211 mach_vm_size_t sr_size
;
212 mach_vm_offset_t sr_pmap_nesting_start
;
213 mach_vm_size_t sr_pmap_nesting_size
;
214 thread_call_t sr_timer_call
;
215 struct vm_shared_region_slide_info sr_slide_info
;
217 boolean_t sr_uuid_copied
;
218 uint32_t sr_images_count
;
219 struct dyld_uuid_info_64
*sr_images
;
222 extern kern_return_t
vm_shared_region_slide_page(vm_shared_region_slide_info_t si
,
224 mach_vm_offset_t uservaddr
,
226 extern vm_shared_region_slide_info_t
vm_shared_region_get_slide_info(vm_shared_region_t sr
);
227 #else /* !MACH_KERNEL_PRIVATE */
229 struct vm_shared_region
;
230 struct vm_shared_region_slide_info
;
231 struct vm_shared_region_slide_info_entry
;
232 struct slide_info_entry_toc
;
234 #endif /* MACH_KERNEL_PRIVATE */
236 extern void vm_shared_region_init(void);
237 extern kern_return_t
vm_shared_region_enter(
243 cpu_subtype_t cpu_subtype
);
244 extern kern_return_t
vm_shared_region_remove(
247 extern vm_shared_region_t
vm_shared_region_get(
249 extern vm_shared_region_t
vm_shared_region_trim_and_get(
251 extern void vm_shared_region_deallocate(
252 struct vm_shared_region
*shared_region
);
253 extern mach_vm_offset_t
vm_shared_region_base_address(
254 struct vm_shared_region
*shared_region
);
255 extern mach_vm_size_t
vm_shared_region_size(
256 struct vm_shared_region
*shared_region
);
257 extern ipc_port_t
vm_shared_region_mem_entry(
258 struct vm_shared_region
*shared_region
);
259 extern vm_map_t
vm_shared_region_vm_map(
260 struct vm_shared_region
*shared_region
);
261 extern uint32_t vm_shared_region_get_slide(
262 vm_shared_region_t shared_region
);
263 extern void vm_shared_region_set(
265 struct vm_shared_region
*new_shared_region
);
266 extern vm_shared_region_t
vm_shared_region_lookup(
269 cpu_subtype_t cpu_subtype
,
271 extern kern_return_t
vm_shared_region_start_address(
272 struct vm_shared_region
*shared_region
,
273 mach_vm_offset_t
*start_address
);
274 extern void vm_shared_region_undo_mappings(
276 mach_vm_offset_t sr_base_address
,
277 struct shared_file_mapping_np
*mappings
,
278 unsigned int mappings_count
);
279 extern kern_return_t
vm_shared_region_map_file(
280 struct vm_shared_region
*shared_region
,
281 unsigned int mappings_count
,
282 struct shared_file_mapping_np
*mappings
,
283 memory_object_control_t file_control
,
284 memory_object_size_t file_size
,
287 user_addr_t slide_start
,
288 user_addr_t slide_size
);
289 extern kern_return_t
vm_shared_region_sliding_valid(uint32_t slide
);
290 extern kern_return_t
vm_shared_region_slide_sanity_check(vm_shared_region_t sr
);
291 extern void* vm_shared_region_get_slide_info_entry(vm_shared_region_t sr
);
292 extern void vm_commpage_init(void);
293 extern void vm_commpage_text_init(void);
294 extern kern_return_t
vm_commpage_enter(
298 extern kern_return_t
vm_commpage_remove(
301 int vm_shared_region_slide(uint32_t,
307 memory_object_control_t
);
309 #endif /* KERNEL_PRIVATE */
311 #endif /* _VM_SHARED_REGION_H_ */