xref: /xnu-8019.80.24/osfmk/mach/arm/vm_param.h (revision a325d9c4a84054e40bbe985afedcb50ab80993ea)
1 /*
2  * Copyright (c) 2007 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_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. 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.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
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.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 /*
29  * FILE_ID: vm_param.h
30  */
31 
32 /*
33  *	ARM machine dependent virtual memory parameters.
34  */
35 
36 #ifndef _MACH_ARM_VM_PARAM_H_
37 #define _MACH_ARM_VM_PARAM_H_
38 
39 #if defined (__arm__) || defined (__arm64__)
40 
41 #if defined(KERNEL_PRIVATE) && __ARM_16K_PG__
42 #include <arm64/proc_reg.h>
43 #endif
44 
45 #if !defined (KERNEL) && !defined (__ASSEMBLER__)
46 #include <mach/vm_page_size.h>
47 #endif
48 
49 #define BYTE_SIZE       8       /* byte size in bits */
50 
51 #if defined (KERNEL)
52 
53 #ifndef __ASSEMBLER__
54 
55 #ifdef  __arm__
56 #define PAGE_SHIFT_CONST        12
57 #elif defined(__arm64__)
58 extern int PAGE_SHIFT_CONST;
59 #else
60 #error Unsupported arch
61 #endif
62 
63 #if defined(KERNEL_PRIVATE) && __ARM_16K_PG__
64 #define PAGE_SHIFT              ARM_PGSHIFT
65 #else
66 #define PAGE_SHIFT              PAGE_SHIFT_CONST
67 #endif
68 #define PAGE_SIZE               (1 << PAGE_SHIFT)
69 #define PAGE_MASK               (PAGE_SIZE-1)
70 
71 #define VM_PAGE_SIZE            PAGE_SIZE
72 
73 #define machine_ptob(x)         ((x) << PAGE_SHIFT)
74 
75 /*
76  * Defined for the purpose of testing the pmap advertised page
77  * size; this does not necessarily match the hardware page size.
78  */
79 #define TEST_PAGE_SIZE_16K      ((PAGE_SHIFT_CONST == 14))
80 #define TEST_PAGE_SIZE_4K       ((PAGE_SHIFT_CONST == 12))
81 
82 #endif  /* !__ASSEMBLER__ */
83 
84 #else
85 
86 #define PAGE_SHIFT                      vm_page_shift
87 #define PAGE_SIZE                       vm_page_size
88 #define PAGE_MASK                       vm_page_mask
89 
90 #define VM_PAGE_SIZE            vm_page_size
91 
92 #define machine_ptob(x)         ((x) << PAGE_SHIFT)
93 
94 #endif
95 
96 #define PAGE_MAX_SHIFT          14
97 #define PAGE_MAX_SIZE           (1 << PAGE_MAX_SHIFT)
98 #define PAGE_MAX_MASK           (PAGE_MAX_SIZE-1)
99 
100 #define PAGE_MIN_SHIFT          12
101 #define PAGE_MIN_SIZE           (1 << PAGE_MIN_SHIFT)
102 #define PAGE_MIN_MASK           (PAGE_MIN_SIZE-1)
103 
104 #define VM_MAX_PAGE_ADDRESS     MACH_VM_MAX_ADDRESS
105 
106 #ifndef __ASSEMBLER__
107 
108 #ifdef  MACH_KERNEL_PRIVATE
109 
110 #define VM32_SUPPORT            1
111 #define VM32_MIN_ADDRESS        ((vm32_offset_t) 0)
112 #define VM32_MAX_ADDRESS        ((vm32_offset_t) (VM_MAX_ADDRESS & 0xFFFFFFFF))
113 
114 /*
115  * kalloc() parameters:
116  *
117  * Historically kalloc's underlying zones were power-of-2 sizes, with a
118  * KALLOC_MINSIZE of 16 bytes.  Thus the allocator ensured that
119  * (sizeof == alignof) >= 16 for all kalloc allocations.
120  *
121  * Today kalloc may use zones with intermediate (small) sizes, constrained by
122  * KALLOC_MINSIZE and a minimum alignment, expressed by KALLOC_LOG2_MINALIGN.
123  *
124  * Note that most dynamically allocated data structures contain more than
125  * one int/long/pointer member, so KALLOC_MINSIZE should probably start at 8.
126  */
127 
128 #if defined (__arm__)
129 
130 #define KALLOC_MINSIZE          8       /* minimum allocation size */
131 #define KALLOC_LOG2_MINALIGN    3       /* log2 minimum alignment */
132 
133 #elif defined(__arm64__)
134 
135 #define KALLOC_MINSIZE          16      /* minimum allocation size */
136 #define KALLOC_LOG2_MINALIGN    4       /* log2 minimum alignment */
137 
138 #else
139 #error Unsupported arch
140 #endif
141 
142 #if defined (__arm__)
143 /* existing zone map size limit moved from osfmk/vm/vm_init.c */
144 #define ZONE_MAP_MAX (1024 * 1024 * 1536) /* 1.5GB for 32bit systems */
145 #elif defined(__arm64__)
146 /*
147  * Limits the physical pages in the zone map
148  */
149 #define ZONE_MAP_MAX (31ULL << 30) /* 31GB for 64bit systems */
150 #else
151 #error Unsupported arch
152 #endif
153 
154 #endif
155 
156 #if defined (__arm__)
157 
158 #define VM_MIN_ADDRESS          ((vm_address_t) 0x00000000)
159 #define VM_MAX_ADDRESS          ((vm_address_t) 0x80000000)
160 
161 /* system-wide values */
162 #define MACH_VM_MIN_ADDRESS     ((mach_vm_offset_t) 0)
163 #define MACH_VM_MAX_ADDRESS     ((mach_vm_offset_t) VM_MAX_ADDRESS)
164 
165 #elif defined (__arm64__)
166 
167 #define VM_MIN_ADDRESS          ((vm_address_t) 0x0000000000000000ULL)
168 #define VM_MAX_ADDRESS          ((vm_address_t) 0x0000000080000000ULL)
169 
170 /* system-wide values */
171 #define MACH_VM_MIN_ADDRESS_RAW 0x0ULL
172 #if defined(PLATFORM_MacOSX) || defined(PLATFORM_DriverKit)
173 #define MACH_VM_MAX_ADDRESS_RAW 0x00007FFFFE000000ULL
174 #else
175 #define MACH_VM_MAX_ADDRESS_RAW 0x0000000FC0000000ULL
176 #endif
177 
178 #define MACH_VM_MIN_ADDRESS     ((mach_vm_offset_t) MACH_VM_MIN_ADDRESS_RAW)
179 #define MACH_VM_MAX_ADDRESS     ((mach_vm_offset_t) MACH_VM_MAX_ADDRESS_RAW)
180 
181 #define MACH_VM_MIN_GPU_CARVEOUT_ADDRESS_RAW 0x0000001000000000ULL
182 #define MACH_VM_MAX_GPU_CARVEOUT_ADDRESS_RAW 0x0000007000000000ULL
183 #define MACH_VM_MIN_GPU_CARVEOUT_ADDRESS     ((mach_vm_offset_t) MACH_VM_MIN_GPU_CARVEOUT_ADDRESS_RAW)
184 #define MACH_VM_MAX_GPU_CARVEOUT_ADDRESS     ((mach_vm_offset_t) MACH_VM_MAX_GPU_CARVEOUT_ADDRESS_RAW)
185 
186 #else /* defined(__arm64__) */
187 #error architecture not supported
188 #endif
189 
190 #define VM_MAP_MIN_ADDRESS      VM_MIN_ADDRESS
191 #define VM_MAP_MAX_ADDRESS      VM_MAX_ADDRESS
192 
193 #ifdef  KERNEL
194 
195 #if defined (__arm__)
196 #define VM_KERNEL_POINTER_SIGNIFICANT_BITS  31
197 #define VM_MIN_KERNEL_ADDRESS   ((vm_address_t) 0x80000000)
198 #define VM_MAX_KERNEL_ADDRESS   ((vm_address_t) 0xFFFEFFFF)
199 #define VM_HIGH_KERNEL_WINDOW   ((vm_address_t) 0xFFFE0000)
200 #elif defined (__arm64__)
201 /*
202  * The minimum and maximum kernel address; some configurations may
203  * constrain the address space further.
204  */
205 #define TiB(x) ((0ULL + (x)) << 40)
206 #define GiB(x) ((0ULL + (x)) << 30)
207 
208 #if XNU_KERNEL_PRIVATE
209 #if defined(ARM_LARGE_MEMORY)
210 /*
211  * +-----------------------+--------+--------+------------------------+
212  * | 0xffff_fe90_0000_0000 |-1472GB |  576GB | KASAN_SHADOW_MIN       |
213  * |                       |        |        | VM_MAX_KERNEL_ADDRESS  |
214  * +-----------------------+--------+--------+------------------------+
215  * | 0xffff_fe10_0000_0000 |-1984GB |   64GB | PMAP_HEAP_RANGE_START  |
216  * +-----------------------+--------+--------+------------------------+
217  * | 0xffff_fe00_0700_4000 |        |        | VM_KERNEL_LINK_ADDRESS |
218  * +-----------------------+--------+--------+------------------------+
219  * | 0xffff_fe00_0000_0000 |   -2TB |    0GB | VM_MIN_KERNEL_ADDRESS  |
220  * |                       |        |        | LOW_GLOBALS            |
221  * +-----------------------+--------+--------+------------------------+
222  */
223 #define VM_KERNEL_POINTER_SIGNIFICANT_BITS  41
224 
225 // Kernel VA space starts at -2TB
226 #define VM_MIN_KERNEL_ADDRESS   ((vm_address_t) (0ULL - TiB(2)))
227 
228 // 1.25TB for static_memory_region, 512GB for kernel heap, 256GB for KASAN
229 #define VM_MAX_KERNEL_ADDRESS   ((vm_address_t) (VM_MIN_KERNEL_ADDRESS + GiB(64) + GiB(512) - 1))
230 #else // ARM_LARGE_MEMORY
231 /*
232  * +-----------------------+--------+--------+------------------------+
233  * | 0xffff_fffc_0000_0000 |  -16GB |  112GB | KASAN_SHADOW_MIN       |
234  * |                       |        |        | VM_MAX_KERNEL_ADDRESS  |
235  * +-----------------------+--------+--------+------------------------+
236  * | 0xffff_fff0_0700_4000 |        |        | VM_KERNEL_LINK_ADDRESS |
237  * +-----------------------+--------+--------+------------------------+
238  * | 0xffff_fff0_0000_0000 |  -64GB |   64GB | LOW_GLOBALS            |
239  * |                       |        |        | PMAP_HEAP_RANGE_START  | <= H8
240  * +-----------------------+--------+--------+------------------------+
241  * | 0xffff_ffe0_0000_0000 | -128GB |    0GB | VM_MIN_KERNEL_ADDRESS  |
242  * |                       |        |        | PMAP_HEAP_RANGE_START  | >= H9
243  * +-----------------------+--------+--------+------------------------+
244  */
245 #define VM_KERNEL_POINTER_SIGNIFICANT_BITS  37
246 #define VM_MIN_KERNEL_ADDRESS   ((vm_address_t) 0xffffffe000000000ULL)
247 #define VM_MAX_KERNEL_ADDRESS   ((vm_address_t) 0xfffffffbffffffffULL)
248 #endif // ARM_LARGE_MEMORY
249 
250 #else // !XNU_KERNEL_PRIVATE
251 // Inform kexts about largest possible kernel address space
252 #define VM_MIN_KERNEL_ADDRESS   ((vm_address_t) (0ULL - TiB(2)))
253 #define VM_MAX_KERNEL_ADDRESS   ((vm_address_t) 0xfffffffbffffffffULL)
254 #endif // XNU_KERNEL_PRIVATE
255 #else
256 #error architecture not supported
257 #endif
258 
259 #define VM_MIN_KERNEL_AND_KEXT_ADDRESS  VM_MIN_KERNEL_ADDRESS
260 
261 #if defined (__arm64__)
262 /* Top-Byte-Ignore */
263 #define TBI_MASK           0xff00000000000000ULL
264 #define tbi_clear(addr)    ((typeof (addr))(((uintptr_t)(addr)) &~ (TBI_MASK)))
265 #define tbi_fill(addr)     ((typeof (addr))(((uintptr_t)(addr)) | (TBI_MASK)))
266 #endif /* __arm64__ */
267 
268 #if CONFIG_KERNEL_TBI
269 /*
270  * 'strip' in PAC sense, therefore replacing the stripped bits sign extending
271  * the sign bit.
272  */
273 #define VM_KERNEL_TBI_FILL(_v)  (tbi_fill(_v))
274 #define VM_KERNEL_TBI_CLEAR(_v) (tbi_clear(_v))
275 #define VM_KERNEL_STRIP_TBI(_v) (VM_KERNEL_TBI_FILL(_v))
276 #else /* CONFIG_KERNEL_TBI */
277 #define VM_KERNEL_TBI_FILL(_v)  (_v)
278 #define VM_KERNEL_TBI_CLEAR(_v) (_v)
279 #define VM_KERNEL_STRIP_TBI(_v) (_v)
280 #endif /* CONFIG_KERNE_TBI */
281 
282 #if __has_feature(ptrauth_calls)
283 #include <ptrauth.h>
284 #define VM_KERNEL_STRIP_PAC(_v) (ptrauth_strip((void *)(uintptr_t)(_v), ptrauth_key_asia))
285 #else /* !ptrauth_calls */
286 #define VM_KERNEL_STRIP_PAC(_v) (_v)
287 #endif /* ptrauth_calls */
288 
289 #define VM_KERNEL_STRIP_PTR(_va)        ((VM_KERNEL_STRIP_TBI(VM_KERNEL_STRIP_PAC(_va))))
290 #define VM_KERNEL_STRIP_UPTR(_va)       ((vm_address_t)VM_KERNEL_STRIP_PTR((uintptr_t)(_va)))
291 #define VM_KERNEL_ADDRESS(_va)  \
292 	((VM_KERNEL_STRIP_UPTR(_va) >= VM_MIN_KERNEL_ADDRESS) && \
293 	 (VM_KERNEL_STRIP_UPTR(_va) <= VM_MAX_KERNEL_ADDRESS))
294 
295 #ifdef  MACH_KERNEL_PRIVATE
296 /*
297  *	Physical memory is mapped linearly at an offset virtual memory.
298  */
299 extern unsigned long            gVirtBase, gPhysBase, gPhysSize;
300 
301 #define isphysmem(a)            (((vm_address_t)(a) - gPhysBase) < gPhysSize)
302 #define physmap_enclosed(a)     isphysmem(a)
303 
304 /*
305  * gPhysBase/Size only represent kernel-managed memory. These globals represent
306  * the actual DRAM base address and size as reported by iBoot through the device
307  * tree.
308  */
309 #include <stdint.h>
310 extern uint64_t                 gDramBase, gDramSize;
311 #define is_dram_addr(addr)      (((uint64_t)(addr) - gDramBase) < gDramSize)
312 
313 #endif /* MACH_KERNEL_PRIVATE */
314 
315 #ifdef  XNU_KERNEL_PRIVATE
316 
317 #if KASAN
318 /* Increase the stack sizes to account for the redzones that get added to every
319  * stack object. */
320 # define KERNEL_STACK_SIZE      (4*4*4096)
321 #elif DEBUG
322 /**
323  * Increase the stack size to account for less efficient use of stack space when
324  * compiling with -O0.
325  */
326 # define KERNEL_STACK_SIZE      (2*4*4096)
327 #else
328 /*
329  * KERNEL_STACK_MULTIPLIER can be defined externally to get a larger
330  * kernel stack size. For example, adding "-DKERNEL_STACK_MULTIPLIER=2"
331  * helps avoid kernel stack overflows when compiling with "-O0".
332  */
333 #ifndef KERNEL_STACK_MULTIPLIER
334 #define KERNEL_STACK_MULTIPLIER (1)
335 #endif /* KERNEL_STACK_MULTIPLIER */
336 # define KERNEL_STACK_SIZE      (4*4096*KERNEL_STACK_MULTIPLIER)
337 #endif /* XNU_KERNEL_PRIVATE */
338 
339 #define INTSTACK_SIZE           (4*4096)
340 
341 #ifdef __arm64__
342 #define EXCEPSTACK_SIZE         (4*4096)
343 #else
344 #define FIQSTACK_SIZE           (4096)
345 #endif
346 
347 #if defined (__arm__)
348 #define HIGH_EXC_VECTORS        ((vm_address_t) 0xFFFF0000)
349 #endif
350 
351 /*
352  * TODO: We're hardcoding the expected virtual TEXT base here;
353  * that gives us an ugly dependency on a linker argument in
354  * the make files.  Clean this up, so we don't hardcode it
355  * twice; this is nothing but trouble.
356  */
357 #if defined (__arm__)
358 #define VM_KERNEL_LINK_ADDRESS  ((vm_address_t) 0x80000000)
359 #elif defined (__arm64__)
360 /* VM_KERNEL_LINK_ADDRESS defined in makedefs/MakeInc.def for arm64 platforms */
361 #else
362 #error architecture not supported
363 #endif
364 
365 #endif  /* MACH_KERNEL_PRIVATE */
366 #endif  /* KERNEL */
367 
368 #endif  /* !__ASSEMBLER__ */
369 
370 #define SWI_SYSCALL     0x80
371 
372 #endif /* defined (__arm__) || defined (__arm64__) */
373 
374 #endif  /* _MACH_ARM_VM_PARAM_H_ */
375