1/*
2 * Copyright (c) 2007 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23/*
24 *
25 * File: mach/shared_region.h
26 *
27 * protos and struct definitions for shared region
28 */
29
30#ifndef _MACH_SHARED_REGION_H_
31#define _MACH_SHARED_REGION_H_
32
33#include <sys/cdefs.h>
34#include <mach/vm_prot.h>
35#include <mach/vm_types.h>
36#include <mach/mach_types.h>
37
38#define SHARED_REGION_BASE_I386 0x90000000ULL
39#define SHARED_REGION_SIZE_I386 0x20000000ULL
40#define SHARED_REGION_NESTING_BASE_I386 0x90000000ULL
41#define SHARED_REGION_NESTING_SIZE_I386 0x20000000ULL
42#define SHARED_REGION_NESTING_MIN_I386 0x00200000ULL
43#define SHARED_REGION_NESTING_MAX_I386 0xFFE00000ULL
44
45/*
46 * Note the shared region size here seems odd for x86.
47 * The size is chosen to end the address space at a boundary
48 * that the arm64 pmap uses for pmap->max. We use this
49 * limit to prevent problems with Rosetta. Given how large
50 * the region is now, the few fewer megabytes shouldn't matter.
51 */
52#define SHARED_REGION_BASE_X86_64 0x00007FF800000000ULL
53#define SHARED_REGION_SIZE_X86_64 0x00000007FE000000ULL
54#define SHARED_REGION_NESTING_BASE_X86_64 0x00007FF800000000ULL
55#define SHARED_REGION_NESTING_SIZE_X86_64 0x00000007FE000000ULL
56#define SHARED_REGION_NESTING_MIN_X86_64 0x0000000000200000ULL
57#define SHARED_REGION_NESTING_MAX_X86_64 0xFFFFFFFFFFE00000ULL
58
59#define SHARED_REGION_BASE_PPC 0x90000000ULL
60#define SHARED_REGION_SIZE_PPC 0x20000000ULL
61#define SHARED_REGION_NESTING_BASE_PPC 0x90000000ULL
62#define SHARED_REGION_NESTING_SIZE_PPC 0x10000000ULL
63#define SHARED_REGION_NESTING_MIN_PPC 0x10000000ULL
64#define SHARED_REGION_NESTING_MAX_PPC 0x10000000ULL
65
66#define SHARED_REGION_BASE_PPC64 0x00007FFF60000000ULL
67#define SHARED_REGION_SIZE_PPC64 0x00000000A0000000ULL
68#define SHARED_REGION_NESTING_BASE_PPC64 0x00007FFF60000000ULL
69#define SHARED_REGION_NESTING_SIZE_PPC64 0x00000000A0000000ULL
70#define SHARED_REGION_NESTING_MIN_PPC64 0x0000000010000000ULL
71#define SHARED_REGION_NESTING_MAX_PPC64 0x0000000010000000ULL
72
73#define SHARED_REGION_BASE_ARM 0x40000000ULL
74#define SHARED_REGION_SIZE_ARM 0x40000000ULL
75#define SHARED_REGION_NESTING_BASE_ARM 0x40000000ULL
76#define SHARED_REGION_NESTING_SIZE_ARM 0x40000000ULL
77#define SHARED_REGION_NESTING_MIN_ARM ?
78#define SHARED_REGION_NESTING_MAX_ARM ?
79
80#define SHARED_REGION_BASE_ARM64_32 0x1A000000ULL
81#define SHARED_REGION_SIZE_ARM64_32 0xa6000000ULL /* up to 0xc0000000 */
82#define SHARED_REGION_NESTING_BASE_ARM64_32 0x1A000000ULL
83#define SHARED_REGION_NESTING_SIZE_ARM64_32 0xa6000000ULL
84#define SHARED_REGION_NESTING_MIN_ARM64_32 ?
85#define SHARED_REGION_NESTING_MAX_ARM64_32 ?
86
87#ifdef XNU_KERNEL_PRIVATE
88/* ARM64_TODO: move to higher memory */
89#endif
90#define SHARED_REGION_BASE_ARM64 0x180000000ULL
91#define SHARED_REGION_SIZE_ARM64 0x180000000ULL
92#define SHARED_REGION_NESTING_BASE_ARM64 SHARED_REGION_BASE_ARM64
93#define SHARED_REGION_NESTING_SIZE_ARM64 SHARED_REGION_SIZE_ARM64
94#define SHARED_REGION_NESTING_MIN_ARM64 ?
95#define SHARED_REGION_NESTING_MAX_ARM64 ?
96
97#if defined(__i386__)
98#define SHARED_REGION_BASE SHARED_REGION_BASE_I386
99#define SHARED_REGION_SIZE SHARED_REGION_SIZE_I386
100#define SHARED_REGION_NESTING_BASE SHARED_REGION_NESTING_BASE_I386
101#define SHARED_REGION_NESTING_SIZE SHARED_REGION_NESTING_SIZE_I386
102#define SHARED_REGION_NESTING_MIN SHARED_REGION_NESTING_MIN_I386
103#define SHARED_REGION_NESTING_MAX SHARED_REGION_NESTING_MAX_I386
104#elif defined(__x86_64__)
105#define SHARED_REGION_BASE SHARED_REGION_BASE_X86_64
106#define SHARED_REGION_SIZE SHARED_REGION_SIZE_X86_64
107#define SHARED_REGION_NESTING_BASE SHARED_REGION_NESTING_BASE_X86_64
108#define SHARED_REGION_NESTING_SIZE SHARED_REGION_NESTING_SIZE_X86_64
109#define SHARED_REGION_NESTING_MIN SHARED_REGION_NESTING_MIN_X86_64
110#define SHARED_REGION_NESTING_MAX SHARED_REGION_NESTING_MAX_X86_64
111#elif defined(__arm__)
112#define SHARED_REGION_BASE SHARED_REGION_BASE_ARM
113#define SHARED_REGION_SIZE SHARED_REGION_SIZE_ARM
114#define SHARED_REGION_NESTING_BASE SHARED_REGION_NESTING_BASE_ARM
115#define SHARED_REGION_NESTING_SIZE SHARED_REGION_NESTING_SIZE_ARM
116#define SHARED_REGION_NESTING_MIN SHARED_REGION_NESTING_MIN_ARM
117#define SHARED_REGION_NESTING_MAX SHARED_REGION_NESTING_MAX_ARM
118#elif defined(__arm64__) && !defined(__LP64__)
119#define SHARED_REGION_BASE SHARED_REGION_BASE_ARM64_32
120#define SHARED_REGION_SIZE SHARED_REGION_SIZE_ARM64_32
121#define SHARED_REGION_NESTING_BASE SHARED_REGION_NESTING_BASE_ARM64_32
122#define SHARED_REGION_NESTING_SIZE SHARED_REGION_NESTING_SIZE_ARM64_32
123#define SHARED_REGION_NESTING_MIN SHARED_REGION_NESTING_MIN_ARM64_32
124#define SHARED_REGION_NESTING_MAX SHARED_REGION_NESTING_MAX_ARM64_32
125#elif defined(__arm64__) && defined(__LP64__)
126#define SHARED_REGION_BASE SHARED_REGION_BASE_ARM64
127#define SHARED_REGION_SIZE SHARED_REGION_SIZE_ARM64
128#define SHARED_REGION_NESTING_BASE SHARED_REGION_NESTING_BASE_ARM64
129#define SHARED_REGION_NESTING_SIZE SHARED_REGION_NESTING_SIZE_ARM64
130#define SHARED_REGION_NESTING_MIN SHARED_REGION_NESTING_MIN_ARM64
131#define SHARED_REGION_NESTING_MAX SHARED_REGION_NESTING_MAX_ARM64
132#endif
133
134#ifdef KERNEL_PRIVATE
135
136/*
137 * This is routine sets the current source of power.
138 * Arguments:
139 * 0 if it is external source (connected to power )
140 * 1 if it is internal power source ie battery
141 */
142
143void post_sys_powersource(int);
144
145/*
146 * RSR interfaces for use by APFS
147 */
148extern boolean_t (*rsr_check_vnode)(void *vnode);
149extern uint32_t rsr_get_version(void);
150extern void rsr_bump_version(void);
151
152#endif /* KERNEL_PRIVATE */
153
154/*
155 * The shared_region_* declarations are a private interface between dyld and the kernel.
156 */
157
158/*
159 * This was used for the no longer present shared_region_map_and_slide_np() interface.
160 * The struct got used by other external projects to represent shared cache info, so
161 * it's left behind for now.
162 */
163struct shared_file_mapping_np {
164 mach_vm_address_t sfm_address;
165 mach_vm_size_t sfm_size;
166 mach_vm_offset_t sfm_file_offset;
167 vm_prot_t sfm_max_prot;
168 vm_prot_t sfm_init_prot;
169};
170
171struct shared_file_mapping_slide_np {
172 /* address at which to create mapping */
173 mach_vm_address_t sms_address __kernel_data_semantics;
174 /* size of region to map */
175 mach_vm_size_t sms_size;
176 /* offset into file to be mapped */
177 mach_vm_offset_t sms_file_offset __kernel_data_semantics;
178 /* size of data at sms_slide_start */
179 user_addr_t sms_slide_size;
180 /* address from which to get relocation data */
181 user_addr_t sms_slide_start;
182 /* protections, plus flags, see below */
183 vm_prot_t sms_max_prot;
184 vm_prot_t sms_init_prot;
185};
186struct shared_file_np {
187 int sf_fd; /* file to be mapped into shared region */
188 uint32_t sf_mappings_count; /* number of mappings */
189 uint32_t sf_slide; /* distance in bytes of the slide */
190};
191
192/*
193 * Extensions to sfm_max_prot that identify how to handle each mapping.
194 * These must not interfere with normal prot assignments.
195 *
196 * VM_PROT_COW - copy on write pages
197 *
198 * VM_PROT_ZF - zero fill pages
199 *
200 * VM_PROT_SLIDE - file pages which require relocation and, on arm64e, signing
201 * these will be unique per shared region.
202 *
203 * VM_PROT_NOAUTH - file pages which don't require signing. When combined
204 * with VM_PROT_SLIDE, pages are shareable across different
205 * shared regions which map the same file with the same relocation info.
206 */
207#define VM_PROT_COW 0x08
208#define VM_PROT_ZF 0x10
209#define VM_PROT_SLIDE 0x20
210#define VM_PROT_NOAUTH 0x40
211#define VM_PROT_TRANSLATED_ALLOW_EXECUTE 0x80
212
213#ifndef KERNEL
214
215__BEGIN_DECLS
216int shared_region_check_np(uint64_t *startaddress);
217int shared_region_map_np(int fd,
218 uint32_t mappingCount,
219 const struct shared_file_mapping_np *mappings);
220int shared_region_slide_np(void);
221__END_DECLS
222
223#endif /* !KERNEL */
224
225#endif /* _MACH_SHARED_REGION_H_ */
226