]> git.saurik.com Git - apple/xnu.git/blob - osfmk/arm64/arm_vm_init.c
fe6523791d3523ade1843417137d00ff94245cf3
[apple/xnu.git] / osfmk / arm64 / arm_vm_init.c
1 /*
2 * Copyright (c) 2007-2011 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 #include <mach_debug.h>
30 #include <mach_kdp.h>
31 #include <debug.h>
32
33 #include <kern/assert.h>
34 #include <kern/misc_protos.h>
35 #include <kern/monotonic.h>
36 #include <mach/vm_types.h>
37 #include <mach/vm_param.h>
38 #include <vm/vm_kern.h>
39 #include <vm/vm_page.h>
40 #include <vm/pmap.h>
41
42 #include <machine/atomic.h>
43 #include <arm64/proc_reg.h>
44 #include <arm64/lowglobals.h>
45 #include <arm/cpu_data_internal.h>
46 #include <arm/misc_protos.h>
47 #include <pexpert/arm64/boot.h>
48 #include <pexpert/device_tree.h>
49
50 #include <libkern/kernel_mach_header.h>
51 #include <libkern/section_keywords.h>
52
53 #include <san/kasan.h>
54
55 #if __ARM_KERNEL_PROTECT__
56 /*
57 * If we want to support __ARM_KERNEL_PROTECT__, we need a sufficient amount of
58 * mappable space preceeding the kernel (as we unmap the kernel by cutting the
59 * range covered by TTBR1 in half). This must also cover the exception vectors.
60 */
61 static_assert(KERNEL_PMAP_HEAP_RANGE_START > ARM_KERNEL_PROTECT_EXCEPTION_START);
62
63 /* The exception vectors and the kernel cannot share root TTEs. */
64 static_assert((KERNEL_PMAP_HEAP_RANGE_START & ~ARM_TT_ROOT_OFFMASK) > ARM_KERNEL_PROTECT_EXCEPTION_START);
65
66 /*
67 * We must have enough space in the TTBR1_EL1 range to create the EL0 mapping of
68 * the exception vectors.
69 */
70 static_assert((((~ARM_KERNEL_PROTECT_EXCEPTION_START) + 1) * 2ULL) <= (ARM_TT_ROOT_SIZE + ARM_TT_ROOT_INDEX_MASK));
71 #endif /* __ARM_KERNEL_PROTECT__ */
72
73 #if __APRR_SUPPORTED__ && XNU_MONITOR
74 #define ARM_DYNAMIC_TABLE_XN ARM_TTE_TABLE_PXN
75 #else
76 #define ARM_DYNAMIC_TABLE_XN (ARM_TTE_TABLE_PXN | ARM_TTE_TABLE_XN)
77 #endif
78
79 #if KASAN
80 extern vm_offset_t shadow_pbase;
81 extern vm_offset_t shadow_ptop;
82 extern vm_offset_t physmap_vbase;
83 extern vm_offset_t physmap_vtop;
84 #endif
85
86 /*
87 * We explicitly place this in const, as it is not const from a language
88 * perspective, but it is only modified before we actually switch away from
89 * the bootstrap page tables.
90 */
91 SECURITY_READ_ONLY_LATE(uint8_t) bootstrap_pagetables[BOOTSTRAP_TABLE_SIZE] __attribute__((aligned(ARM_PGBYTES)));
92
93 /*
94 * Denotes the end of xnu.
95 */
96 extern void *last_kernel_symbol;
97
98 extern void arm64_replace_bootstack(cpu_data_t*);
99 extern void PE_slide_devicetree(vm_offset_t);
100
101 /*
102 * KASLR parameters
103 */
104 SECURITY_READ_ONLY_LATE(vm_offset_t) vm_kernel_base;
105 SECURITY_READ_ONLY_LATE(vm_offset_t) vm_kernel_top;
106 SECURITY_READ_ONLY_LATE(vm_offset_t) vm_kext_base;
107 SECURITY_READ_ONLY_LATE(vm_offset_t) vm_kext_top;
108 SECURITY_READ_ONLY_LATE(vm_offset_t) vm_kernel_stext;
109 SECURITY_READ_ONLY_LATE(vm_offset_t) vm_kernel_etext;
110 SECURITY_READ_ONLY_LATE(vm_offset_t) vm_kernel_slide;
111 SECURITY_READ_ONLY_LATE(vm_offset_t) vm_kernel_slid_base;
112 SECURITY_READ_ONLY_LATE(vm_offset_t) vm_kernel_slid_top;
113
114 SECURITY_READ_ONLY_LATE(vm_offset_t) vm_prelink_stext;
115 SECURITY_READ_ONLY_LATE(vm_offset_t) vm_prelink_etext;
116 SECURITY_READ_ONLY_LATE(vm_offset_t) vm_prelink_sdata;
117 SECURITY_READ_ONLY_LATE(vm_offset_t) vm_prelink_edata;
118 SECURITY_READ_ONLY_LATE(vm_offset_t) vm_prelink_sinfo;
119 SECURITY_READ_ONLY_LATE(vm_offset_t) vm_prelink_einfo;
120 SECURITY_READ_ONLY_LATE(vm_offset_t) vm_slinkedit;
121 SECURITY_READ_ONLY_LATE(vm_offset_t) vm_elinkedit;
122
123 SECURITY_READ_ONLY_LATE(vm_offset_t) vm_kernel_builtinkmod_text;
124 SECURITY_READ_ONLY_LATE(vm_offset_t) vm_kernel_builtinkmod_text_end;
125
126 SECURITY_READ_ONLY_LATE(vm_offset_t) vm_kernelcache_base;
127 SECURITY_READ_ONLY_LATE(vm_offset_t) vm_kernelcache_top;
128
129 /* Used by <mach/arm/vm_param.h> */
130 SECURITY_READ_ONLY_LATE(unsigned long) gVirtBase;
131 SECURITY_READ_ONLY_LATE(unsigned long) gPhysBase;
132 SECURITY_READ_ONLY_LATE(unsigned long) gPhysSize;
133 SECURITY_READ_ONLY_LATE(unsigned long) gT0Sz = T0SZ_BOOT;
134 SECURITY_READ_ONLY_LATE(unsigned long) gT1Sz = T1SZ_BOOT;
135
136 /* 23543331 - step 1 of kext / kernel __TEXT and __DATA colocation is to move
137 * all kexts before the kernel. This is only for arm64 devices and looks
138 * something like the following:
139 * -- vmaddr order --
140 * 0xffffff8004004000 __PRELINK_TEXT
141 * 0xffffff8007004000 __TEXT (xnu)
142 * 0xffffff80075ec000 __DATA (xnu)
143 * 0xffffff80076dc000 __KLD (xnu)
144 * 0xffffff80076e0000 __LAST (xnu)
145 * 0xffffff80076e4000 __LINKEDIT (xnu)
146 * 0xffffff80076e4000 __PRELINK_DATA (not used yet)
147 * 0xffffff800782c000 __PRELINK_INFO
148 * 0xffffff80078e4000 -- End of kernelcache
149 */
150
151 /* 24921709 - make XNU ready for KTRR
152 *
153 * Two possible kernel cache layouts, depending on which kcgen is being used.
154 * VAs increasing downwards.
155 * Old KCGEN:
156 *
157 * __PRELINK_TEXT
158 * __TEXT
159 * __DATA_CONST
160 * __TEXT_EXEC
161 * __KLD
162 * __LAST
163 * __DATA
164 * __PRELINK_DATA (expected empty)
165 * __LINKEDIT
166 * __PRELINK_INFO
167 *
168 * New kcgen:
169 *
170 * __PRELINK_TEXT <--- First KTRR (ReadOnly) segment
171 * __PLK_DATA_CONST
172 * __PLK_TEXT_EXEC
173 * __TEXT
174 * __DATA_CONST
175 * __TEXT_EXEC
176 * __KLD
177 * __LAST <--- Last KTRR (ReadOnly) segment
178 * __DATA
179 * __BOOTDATA (if present)
180 * __LINKEDIT
181 * __PRELINK_DATA (expected populated now)
182 * __PLK_LINKEDIT
183 * __PRELINK_INFO
184 *
185 */
186
187 vm_offset_t mem_size; /* Size of actual physical memory present
188 * minus any performance buffer and possibly
189 * limited by mem_limit in bytes */
190 uint64_t mem_actual; /* The "One True" physical memory size
191 * actually, it's the highest physical
192 * address + 1 */
193 uint64_t max_mem; /* Size of physical memory (bytes), adjusted
194 * by maxmem */
195 uint64_t max_mem_actual; /* Actual size of physical memory (bytes),
196 * adjusted by the maxmem boot-arg */
197 uint64_t sane_size; /* Memory size to use for defaults
198 * calculations */
199 /* This no longer appears to be used; kill it? */
200 addr64_t vm_last_addr = VM_MAX_KERNEL_ADDRESS; /* Highest kernel
201 * virtual address known
202 * to the VM system */
203
204 SECURITY_READ_ONLY_LATE(vm_offset_t) segEXTRADATA;
205 SECURITY_READ_ONLY_LATE(unsigned long) segSizeEXTRADATA;
206
207 SECURITY_READ_ONLY_LATE(vm_offset_t) segLOWESTTEXT;
208 SECURITY_READ_ONLY_LATE(vm_offset_t) segLOWEST;
209 SECURITY_READ_ONLY_LATE(vm_offset_t) segLOWESTRO;
210 SECURITY_READ_ONLY_LATE(vm_offset_t) segHIGHESTRO;
211
212 /* Only set when booted from MH_FILESET kernel collections */
213 SECURITY_READ_ONLY_LATE(vm_offset_t) segLOWESTKC;
214 SECURITY_READ_ONLY_LATE(vm_offset_t) segHIGHESTKC;
215 SECURITY_READ_ONLY_LATE(vm_offset_t) segLOWESTROKC;
216 SECURITY_READ_ONLY_LATE(vm_offset_t) segHIGHESTROKC;
217 SECURITY_READ_ONLY_LATE(vm_offset_t) segLOWESTAuxKC;
218 SECURITY_READ_ONLY_LATE(vm_offset_t) segHIGHESTAuxKC;
219 SECURITY_READ_ONLY_LATE(vm_offset_t) segLOWESTROAuxKC;
220 SECURITY_READ_ONLY_LATE(vm_offset_t) segHIGHESTROAuxKC;
221 SECURITY_READ_ONLY_LATE(vm_offset_t) segLOWESTRXAuxKC;
222 SECURITY_READ_ONLY_LATE(vm_offset_t) segHIGHESTRXAuxKC;
223 SECURITY_READ_ONLY_LATE(vm_offset_t) segHIGHESTNLEAuxKC;
224
225 SECURITY_READ_ONLY_LATE(static vm_offset_t) segTEXTB;
226 SECURITY_READ_ONLY_LATE(static unsigned long) segSizeTEXT;
227
228 #if XNU_MONITOR
229 SECURITY_READ_ONLY_LATE(vm_offset_t) segPPLTEXTB;
230 SECURITY_READ_ONLY_LATE(unsigned long) segSizePPLTEXT;
231
232 SECURITY_READ_ONLY_LATE(vm_offset_t) segPPLTRAMPB;
233 SECURITY_READ_ONLY_LATE(unsigned long) segSizePPLTRAMP;
234
235 SECURITY_READ_ONLY_LATE(vm_offset_t) segPPLDATACONSTB;
236 SECURITY_READ_ONLY_LATE(unsigned long) segSizePPLDATACONST;
237 SECURITY_READ_ONLY_LATE(void *) pmap_stacks_start = NULL;
238 SECURITY_READ_ONLY_LATE(void *) pmap_stacks_end = NULL;
239 #endif
240
241 SECURITY_READ_ONLY_LATE(static vm_offset_t) segDATACONSTB;
242 SECURITY_READ_ONLY_LATE(static unsigned long) segSizeDATACONST;
243
244 SECURITY_READ_ONLY_LATE(vm_offset_t) segTEXTEXECB;
245 SECURITY_READ_ONLY_LATE(unsigned long) segSizeTEXTEXEC;
246
247 SECURITY_READ_ONLY_LATE(static vm_offset_t) segDATAB;
248 SECURITY_READ_ONLY_LATE(static unsigned long) segSizeDATA;
249
250 #if XNU_MONITOR
251 SECURITY_READ_ONLY_LATE(vm_offset_t) segPPLDATAB;
252 SECURITY_READ_ONLY_LATE(unsigned long) segSizePPLDATA;
253 #endif
254
255 SECURITY_READ_ONLY_LATE(vm_offset_t) segBOOTDATAB;
256 SECURITY_READ_ONLY_LATE(unsigned long) segSizeBOOTDATA;
257 extern vm_offset_t intstack_low_guard;
258 extern vm_offset_t intstack_high_guard;
259 extern vm_offset_t excepstack_high_guard;
260
261 SECURITY_READ_ONLY_LATE(vm_offset_t) segLINKB;
262 SECURITY_READ_ONLY_LATE(static unsigned long) segSizeLINK;
263
264 SECURITY_READ_ONLY_LATE(static vm_offset_t) segKLDB;
265 SECURITY_READ_ONLY_LATE(static unsigned long) segSizeKLD;
266 SECURITY_READ_ONLY_LATE(vm_offset_t) segLASTB;
267 SECURITY_READ_ONLY_LATE(unsigned long) segSizeLAST;
268 SECURITY_READ_ONLY_LATE(vm_offset_t) segLASTDATACONSTB;
269 SECURITY_READ_ONLY_LATE(unsigned long) segSizeLASTDATACONST;
270
271 SECURITY_READ_ONLY_LATE(vm_offset_t) sectHIBTEXTB;
272 SECURITY_READ_ONLY_LATE(unsigned long) sectSizeHIBTEXT;
273 SECURITY_READ_ONLY_LATE(vm_offset_t) segHIBDATAB;
274 SECURITY_READ_ONLY_LATE(unsigned long) segSizeHIBDATA;
275 SECURITY_READ_ONLY_LATE(vm_offset_t) sectHIBDATACONSTB;
276 SECURITY_READ_ONLY_LATE(unsigned long) sectSizeHIBDATACONST;
277
278 SECURITY_READ_ONLY_LATE(vm_offset_t) segPRELINKTEXTB;
279 SECURITY_READ_ONLY_LATE(unsigned long) segSizePRELINKTEXT;
280
281 SECURITY_READ_ONLY_LATE(static vm_offset_t) segPLKTEXTEXECB;
282 SECURITY_READ_ONLY_LATE(static unsigned long) segSizePLKTEXTEXEC;
283
284 SECURITY_READ_ONLY_LATE(static vm_offset_t) segPLKDATACONSTB;
285 SECURITY_READ_ONLY_LATE(static unsigned long) segSizePLKDATACONST;
286
287 SECURITY_READ_ONLY_LATE(static vm_offset_t) segPRELINKDATAB;
288 SECURITY_READ_ONLY_LATE(static unsigned long) segSizePRELINKDATA;
289
290 SECURITY_READ_ONLY_LATE(static vm_offset_t) segPLKLLVMCOVB = 0;
291 SECURITY_READ_ONLY_LATE(static unsigned long) segSizePLKLLVMCOV = 0;
292
293 SECURITY_READ_ONLY_LATE(static vm_offset_t) segPLKLINKEDITB;
294 SECURITY_READ_ONLY_LATE(static unsigned long) segSizePLKLINKEDIT;
295
296 SECURITY_READ_ONLY_LATE(static vm_offset_t) segPRELINKINFOB;
297 SECURITY_READ_ONLY_LATE(static unsigned long) segSizePRELINKINFO;
298
299 /* Only set when booted from MH_FILESET primary kernel collection */
300 SECURITY_READ_ONLY_LATE(vm_offset_t) segKCTEXTEXECB;
301 SECURITY_READ_ONLY_LATE(unsigned long) segSizeKCTEXTEXEC;
302 SECURITY_READ_ONLY_LATE(static vm_offset_t) segKCDATACONSTB;
303 SECURITY_READ_ONLY_LATE(static unsigned long) segSizeKCDATACONST;
304 SECURITY_READ_ONLY_LATE(static vm_offset_t) segKCDATAB;
305 SECURITY_READ_ONLY_LATE(static unsigned long) segSizeKCDATA;
306
307 SECURITY_READ_ONLY_LATE(static boolean_t) use_contiguous_hint = TRUE;
308
309 SECURITY_READ_ONLY_LATE(int) PAGE_SHIFT_CONST;
310
311 SECURITY_READ_ONLY_LATE(vm_offset_t) end_kern;
312 SECURITY_READ_ONLY_LATE(vm_offset_t) etext;
313 SECURITY_READ_ONLY_LATE(vm_offset_t) sdata;
314 SECURITY_READ_ONLY_LATE(vm_offset_t) edata;
315
316 SECURITY_READ_ONLY_LATE(static vm_offset_t) auxkc_mh, auxkc_base, auxkc_right_above;
317
318 vm_offset_t alloc_ptpage(boolean_t map_static);
319 SECURITY_READ_ONLY_LATE(vm_offset_t) ropage_next;
320
321 /*
322 * Bootstrap the system enough to run with virtual memory.
323 * Map the kernel's code and data, and allocate the system page table.
324 * Page_size must already be set.
325 *
326 * Parameters:
327 * first_avail: first available physical page -
328 * after kernel page tables
329 * avail_start: PA of first physical page
330 * avail_end: PA of last physical page
331 */
332 SECURITY_READ_ONLY_LATE(vm_offset_t) first_avail;
333 SECURITY_READ_ONLY_LATE(vm_offset_t) static_memory_end;
334 SECURITY_READ_ONLY_LATE(pmap_paddr_t) avail_start;
335 SECURITY_READ_ONLY_LATE(pmap_paddr_t) avail_end;
336 SECURITY_READ_ONLY_LATE(pmap_paddr_t) real_avail_end;
337 SECURITY_READ_ONLY_LATE(unsigned long) real_phys_size;
338 SECURITY_READ_ONLY_LATE(vm_map_address_t) physmap_base = (vm_map_address_t)0;
339 SECURITY_READ_ONLY_LATE(vm_map_address_t) physmap_end = (vm_map_address_t)0;
340
341 #if __ARM_KERNEL_PROTECT__
342 extern void ExceptionVectorsBase;
343 extern void ExceptionVectorsEnd;
344 #endif /* __ARM_KERNEL_PROTECT__ */
345
346 typedef struct {
347 pmap_paddr_t pa;
348 vm_map_address_t va;
349 vm_size_t len;
350 } ptov_table_entry;
351
352 #define PTOV_TABLE_SIZE 8
353 SECURITY_READ_ONLY_LATE(static ptov_table_entry) ptov_table[PTOV_TABLE_SIZE];
354 SECURITY_READ_ONLY_LATE(static boolean_t) kva_active = FALSE;
355
356
357 vm_map_address_t
358 phystokv(pmap_paddr_t pa)
359 {
360 for (size_t i = 0; (i < PTOV_TABLE_SIZE) && (ptov_table[i].len != 0); i++) {
361 if ((pa >= ptov_table[i].pa) && (pa < (ptov_table[i].pa + ptov_table[i].len))) {
362 return pa - ptov_table[i].pa + ptov_table[i].va;
363 }
364 }
365 assertf((pa - gPhysBase) < real_phys_size, "%s: illegal PA: 0x%llx", __func__, (uint64_t)pa);
366 return pa - gPhysBase + gVirtBase;
367 }
368
369 vm_map_address_t
370 phystokv_range(pmap_paddr_t pa, vm_size_t *max_len)
371 {
372 vm_size_t len;
373 for (size_t i = 0; (i < PTOV_TABLE_SIZE) && (ptov_table[i].len != 0); i++) {
374 if ((pa >= ptov_table[i].pa) && (pa < (ptov_table[i].pa + ptov_table[i].len))) {
375 len = ptov_table[i].len - (pa - ptov_table[i].pa);
376 if (*max_len > len) {
377 *max_len = len;
378 }
379 return pa - ptov_table[i].pa + ptov_table[i].va;
380 }
381 }
382 len = PAGE_SIZE - (pa & PAGE_MASK);
383 if (*max_len > len) {
384 *max_len = len;
385 }
386 assertf((pa - gPhysBase) < real_phys_size, "%s: illegal PA: 0x%llx", __func__, (uint64_t)pa);
387 return pa - gPhysBase + gVirtBase;
388 }
389
390 vm_offset_t
391 ml_static_vtop(vm_offset_t va)
392 {
393 for (size_t i = 0; (i < PTOV_TABLE_SIZE) && (ptov_table[i].len != 0); i++) {
394 if ((va >= ptov_table[i].va) && (va < (ptov_table[i].va + ptov_table[i].len))) {
395 return va - ptov_table[i].va + ptov_table[i].pa;
396 }
397 }
398 assertf(((vm_address_t)(va) - gVirtBase) < gPhysSize, "%s: illegal VA: %p", __func__, (void*)va);
399 return (vm_address_t)(va) - gVirtBase + gPhysBase;
400 }
401
402 /*
403 * This rounds the given address up to the nearest boundary for a PTE contiguous
404 * hint.
405 */
406 static vm_offset_t
407 round_up_pte_hint_address(vm_offset_t address)
408 {
409 vm_offset_t hint_size = ARM_PTE_SIZE << ARM_PTE_HINT_ENTRIES_SHIFT;
410 return (address + (hint_size - 1)) & ~(hint_size - 1);
411 }
412
413 /* allocate a page for a page table: we support static and dynamic mappings.
414 *
415 * returns a virtual address for the allocated page
416 *
417 * for static mappings, we allocate from the region ropagetable_begin to ro_pagetable_end-1,
418 * which is defined in the DATA_CONST segment and will be protected RNX when vm_prot_finalize runs.
419 *
420 * for dynamic mappings, we allocate from avail_start, which should remain RWNX.
421 */
422
423 vm_offset_t
424 alloc_ptpage(boolean_t map_static)
425 {
426 vm_offset_t vaddr;
427
428 #if !(defined(KERNEL_INTEGRITY_KTRR) || defined(KERNEL_INTEGRITY_CTRR))
429 map_static = FALSE;
430 #endif
431
432 if (!ropage_next) {
433 ropage_next = (vm_offset_t)&ropagetable_begin;
434 }
435
436 if (map_static) {
437 assert(ropage_next < (vm_offset_t)&ropagetable_end);
438
439 vaddr = ropage_next;
440 ropage_next += ARM_PGBYTES;
441
442 return vaddr;
443 } else {
444 vaddr = phystokv(avail_start);
445 avail_start += ARM_PGBYTES;
446
447 return vaddr;
448 }
449 }
450
451 #if DEBUG
452
453 void dump_kva_l2(vm_offset_t tt_base, tt_entry_t *tt, int indent, uint64_t *rosz_out, uint64_t *rwsz_out);
454
455 void
456 dump_kva_l2(vm_offset_t tt_base, tt_entry_t *tt, int indent, uint64_t *rosz_out, uint64_t *rwsz_out)
457 {
458 unsigned int i;
459 boolean_t cur_ro, prev_ro = 0;
460 int start_entry = -1;
461 tt_entry_t cur, prev = 0;
462 pmap_paddr_t robegin = kvtophys((vm_offset_t)&ropagetable_begin);
463 pmap_paddr_t roend = kvtophys((vm_offset_t)&ropagetable_end);
464 boolean_t tt_static = kvtophys((vm_offset_t)tt) >= robegin &&
465 kvtophys((vm_offset_t)tt) < roend;
466
467 for (i = 0; i < TTE_PGENTRIES; i++) {
468 int tte_type = tt[i] & ARM_TTE_TYPE_MASK;
469 cur = tt[i] & ARM_TTE_TABLE_MASK;
470
471 if (tt_static) {
472 /* addresses mapped by this entry are static if it is a block mapping,
473 * or the table was allocated from the RO page table region */
474 cur_ro = (tte_type == ARM_TTE_TYPE_BLOCK) || (cur >= robegin && cur < roend);
475 } else {
476 cur_ro = 0;
477 }
478
479 if ((cur == 0 && prev != 0) || (cur_ro != prev_ro && prev != 0)) { // falling edge
480 uintptr_t start, end, sz;
481
482 start = (uintptr_t)start_entry << ARM_TT_L2_SHIFT;
483 start += tt_base;
484 end = ((uintptr_t)i << ARM_TT_L2_SHIFT) - 1;
485 end += tt_base;
486
487 sz = end - start + 1;
488 printf("%*s0x%08x_%08x-0x%08x_%08x %s (%luMB)\n",
489 indent * 4, "",
490 (uint32_t)(start >> 32), (uint32_t)start,
491 (uint32_t)(end >> 32), (uint32_t)end,
492 prev_ro ? "Static " : "Dynamic",
493 (sz >> 20));
494
495 if (prev_ro) {
496 *rosz_out += sz;
497 } else {
498 *rwsz_out += sz;
499 }
500 }
501
502 if ((prev == 0 && cur != 0) || cur_ro != prev_ro) { // rising edge: set start
503 start_entry = i;
504 }
505
506 prev = cur;
507 prev_ro = cur_ro;
508 }
509 }
510
511 void
512 dump_kva_space()
513 {
514 uint64_t tot_rosz = 0, tot_rwsz = 0;
515 int ro_ptpages, rw_ptpages;
516 pmap_paddr_t robegin = kvtophys((vm_offset_t)&ropagetable_begin);
517 pmap_paddr_t roend = kvtophys((vm_offset_t)&ropagetable_end);
518 boolean_t root_static = kvtophys((vm_offset_t)cpu_tte) >= robegin &&
519 kvtophys((vm_offset_t)cpu_tte) < roend;
520 uint64_t kva_base = ~((1ULL << (64 - T1SZ_BOOT)) - 1);
521
522 printf("Root page table: %s\n", root_static ? "Static" : "Dynamic");
523
524 for (unsigned int i = 0; i < TTE_PGENTRIES; i++) {
525 pmap_paddr_t cur;
526 boolean_t cur_ro;
527 uintptr_t start, end;
528 uint64_t rosz = 0, rwsz = 0;
529
530 if ((cpu_tte[i] & ARM_TTE_VALID) == 0) {
531 continue;
532 }
533
534 cur = cpu_tte[i] & ARM_TTE_TABLE_MASK;
535 start = (uint64_t)i << ARM_TT_L1_SHIFT;
536 start = start + kva_base;
537 end = start + (ARM_TT_L1_SIZE - 1);
538 cur_ro = cur >= robegin && cur < roend;
539
540 printf("0x%08x_%08x-0x%08x_%08x %s\n",
541 (uint32_t)(start >> 32), (uint32_t)start,
542 (uint32_t)(end >> 32), (uint32_t)end,
543 cur_ro ? "Static " : "Dynamic");
544
545 dump_kva_l2(start, (tt_entry_t*)phystokv(cur), 1, &rosz, &rwsz);
546 tot_rosz += rosz;
547 tot_rwsz += rwsz;
548 }
549
550 printf("L2 Address space mapped: Static %lluMB Dynamic %lluMB Total %lluMB\n",
551 tot_rosz >> 20,
552 tot_rwsz >> 20,
553 (tot_rosz >> 20) + (tot_rwsz >> 20));
554
555 ro_ptpages = (int)((ropage_next - (vm_offset_t)&ropagetable_begin) >> ARM_PGSHIFT);
556 rw_ptpages = (int)(lowGlo.lgStaticSize >> ARM_PGSHIFT);
557 printf("Pages used: static %d dynamic %d\n", ro_ptpages, rw_ptpages);
558 }
559
560 #endif /* DEBUG */
561
562 #if __ARM_KERNEL_PROTECT__ || XNU_MONITOR
563 /*
564 * arm_vm_map:
565 * root_ttp: The kernel virtual address for the root of the target page tables
566 * vaddr: The target virtual address
567 * pte: A page table entry value (may be ARM_PTE_EMPTY)
568 *
569 * This function installs pte at vaddr in root_ttp. Any page table pages needed
570 * to install pte will be allocated by this function.
571 */
572 static void
573 arm_vm_map(tt_entry_t * root_ttp, vm_offset_t vaddr, pt_entry_t pte)
574 {
575 vm_offset_t ptpage = 0;
576 tt_entry_t * ttp = root_ttp;
577
578 tt_entry_t * l1_ttep = NULL;
579 tt_entry_t l1_tte = 0;
580
581 tt_entry_t * l2_ttep = NULL;
582 tt_entry_t l2_tte = 0;
583 pt_entry_t * ptep = NULL;
584 pt_entry_t cpte = 0;
585
586 /*
587 * Walk the target page table to find the PTE for the given virtual
588 * address. Allocate any page table pages needed to do this.
589 */
590 l1_ttep = ttp + ((vaddr & ARM_TT_L1_INDEX_MASK) >> ARM_TT_L1_SHIFT);
591 l1_tte = *l1_ttep;
592
593 if (l1_tte == ARM_TTE_EMPTY) {
594 ptpage = alloc_ptpage(TRUE);
595 bzero((void *)ptpage, ARM_PGBYTES);
596 l1_tte = kvtophys(ptpage);
597 l1_tte &= ARM_TTE_TABLE_MASK;
598 l1_tte |= ARM_TTE_VALID | ARM_TTE_TYPE_TABLE;
599 *l1_ttep = l1_tte;
600 ptpage = 0;
601 }
602
603 ttp = (tt_entry_t *)phystokv(l1_tte & ARM_TTE_TABLE_MASK);
604
605 l2_ttep = ttp + ((vaddr & ARM_TT_L2_INDEX_MASK) >> ARM_TT_L2_SHIFT);
606 l2_tte = *l2_ttep;
607
608 if (l2_tte == ARM_TTE_EMPTY) {
609 ptpage = alloc_ptpage(TRUE);
610 bzero((void *)ptpage, ARM_PGBYTES);
611 l2_tte = kvtophys(ptpage);
612 l2_tte &= ARM_TTE_TABLE_MASK;
613 l2_tte |= ARM_TTE_VALID | ARM_TTE_TYPE_TABLE;
614 *l2_ttep = l2_tte;
615 ptpage = 0;
616 }
617
618 ttp = (tt_entry_t *)phystokv(l2_tte & ARM_TTE_TABLE_MASK);
619
620 ptep = ttp + ((vaddr & ARM_TT_L3_INDEX_MASK) >> ARM_TT_L3_SHIFT);
621 cpte = *ptep;
622
623 /*
624 * If the existing PTE is not empty, then we are replacing a valid
625 * mapping.
626 */
627 if (cpte != ARM_PTE_EMPTY) {
628 panic("%s: cpte=%#llx is not empty, "
629 "vaddr=%#lx, pte=%#llx",
630 __FUNCTION__, cpte,
631 vaddr, pte);
632 }
633
634 *ptep = pte;
635 }
636
637 #endif // __ARM_KERNEL_PROTECT || XNU_MONITOR
638
639 #if __ARM_KERNEL_PROTECT__
640
641 /*
642 * arm_vm_kernel_el0_map:
643 * vaddr: The target virtual address
644 * pte: A page table entry value (may be ARM_PTE_EMPTY)
645 *
646 * This function installs pte at vaddr for the EL0 kernel mappings.
647 */
648 static void
649 arm_vm_kernel_el0_map(vm_offset_t vaddr, pt_entry_t pte)
650 {
651 /* Calculate where vaddr will be in the EL1 kernel page tables. */
652 vm_offset_t kernel_pmap_vaddr = vaddr - ((ARM_TT_ROOT_INDEX_MASK + ARM_TT_ROOT_SIZE) / 2ULL);
653 arm_vm_map(cpu_tte, kernel_pmap_vaddr, pte);
654 }
655
656 /*
657 * arm_vm_kernel_el1_map:
658 * vaddr: The target virtual address
659 * pte: A page table entry value (may be ARM_PTE_EMPTY)
660 *
661 * This function installs pte at vaddr for the EL1 kernel mappings.
662 */
663 static void
664 arm_vm_kernel_el1_map(vm_offset_t vaddr, pt_entry_t pte)
665 {
666 arm_vm_map(cpu_tte, vaddr, pte);
667 }
668
669 /*
670 * arm_vm_kernel_pte:
671 * vaddr: The target virtual address
672 *
673 * This function returns the PTE value for the given vaddr from the kernel page
674 * tables. If the region has been been block mapped, we return what an
675 * equivalent PTE value would be (as regards permissions and flags). We also
676 * remove the HINT bit (as we are not necessarily creating contiguous mappings.
677 */
678 static pt_entry_t
679 arm_vm_kernel_pte(vm_offset_t vaddr)
680 {
681 tt_entry_t * ttp = cpu_tte;
682 tt_entry_t * ttep = NULL;
683 tt_entry_t tte = 0;
684 pt_entry_t * ptep = NULL;
685 pt_entry_t pte = 0;
686
687 ttep = ttp + ((vaddr & ARM_TT_L1_INDEX_MASK) >> ARM_TT_L1_SHIFT);
688 tte = *ttep;
689
690 assert(tte & ARM_TTE_VALID);
691
692 if ((tte & ARM_TTE_TYPE_MASK) == ARM_TTE_TYPE_BLOCK) {
693 /* This is a block mapping; return the equivalent PTE value. */
694 pte = (pt_entry_t)(tte & ~ARM_TTE_TYPE_MASK);
695 pte |= ARM_PTE_TYPE_VALID;
696 pte |= vaddr & ((ARM_TT_L1_SIZE - 1) & ARM_PTE_PAGE_MASK);
697 pte &= ~ARM_PTE_HINT_MASK;
698 return pte;
699 }
700
701 ttp = (tt_entry_t *)phystokv(tte & ARM_TTE_TABLE_MASK);
702 ttep = ttp + ((vaddr & ARM_TT_L2_INDEX_MASK) >> ARM_TT_L2_SHIFT);
703 tte = *ttep;
704
705 assert(tte & ARM_TTE_VALID);
706
707 if ((tte & ARM_TTE_TYPE_MASK) == ARM_TTE_TYPE_BLOCK) {
708 /* This is a block mapping; return the equivalent PTE value. */
709 pte = (pt_entry_t)(tte & ~ARM_TTE_TYPE_MASK);
710 pte |= ARM_PTE_TYPE_VALID;
711 pte |= vaddr & ((ARM_TT_L2_SIZE - 1) & ARM_PTE_PAGE_MASK);
712 pte &= ~ARM_PTE_HINT_MASK;
713 return pte;
714 }
715
716 ttp = (tt_entry_t *)phystokv(tte & ARM_TTE_TABLE_MASK);
717
718 ptep = ttp + ((vaddr & ARM_TT_L3_INDEX_MASK) >> ARM_TT_L3_SHIFT);
719 pte = *ptep;
720 pte &= ~ARM_PTE_HINT_MASK;
721 return pte;
722 }
723
724 /*
725 * arm_vm_prepare_kernel_el0_mappings:
726 * alloc_only: Indicates if PTE values should be copied from the EL1 kernel
727 * mappings.
728 *
729 * This function expands the kernel page tables to support the EL0 kernel
730 * mappings, and conditionally installs the PTE values for the EL0 kernel
731 * mappings (if alloc_only is false).
732 */
733 static void
734 arm_vm_prepare_kernel_el0_mappings(bool alloc_only)
735 {
736 pt_entry_t pte = 0;
737 vm_offset_t start = ((vm_offset_t)&ExceptionVectorsBase) & ~PAGE_MASK;
738 vm_offset_t end = (((vm_offset_t)&ExceptionVectorsEnd) + PAGE_MASK) & ~PAGE_MASK;
739 vm_offset_t cur = 0;
740 vm_offset_t cur_fixed = 0;
741
742 /* Expand for/map the exceptions vectors in the EL0 kernel mappings. */
743 for (cur = start, cur_fixed = ARM_KERNEL_PROTECT_EXCEPTION_START; cur < end; cur += ARM_PGBYTES, cur_fixed += ARM_PGBYTES) {
744 /*
745 * We map the exception vectors at a different address than that
746 * of the kernelcache to avoid sharing page table pages with the
747 * kernelcache (as this may cause issues with TLB caching of
748 * page table pages.
749 */
750 if (!alloc_only) {
751 pte = arm_vm_kernel_pte(cur);
752 }
753
754 arm_vm_kernel_el1_map(cur_fixed, pte);
755 arm_vm_kernel_el0_map(cur_fixed, pte);
756 }
757
758 __builtin_arm_dmb(DMB_ISH);
759 __builtin_arm_isb(ISB_SY);
760
761 if (!alloc_only) {
762 /*
763 * If we have created the alternate exception vector mappings,
764 * the boot CPU may now switch over to them.
765 */
766 set_vbar_el1(ARM_KERNEL_PROTECT_EXCEPTION_START);
767 __builtin_arm_isb(ISB_SY);
768 }
769 }
770
771 /*
772 * arm_vm_populate_kernel_el0_mappings:
773 *
774 * This function adds all required mappings to the EL0 kernel mappings.
775 */
776 static void
777 arm_vm_populate_kernel_el0_mappings(void)
778 {
779 arm_vm_prepare_kernel_el0_mappings(FALSE);
780 }
781
782 /*
783 * arm_vm_expand_kernel_el0_mappings:
784 *
785 * This function expands the kernel page tables to accomodate the EL0 kernel
786 * mappings.
787 */
788 static void
789 arm_vm_expand_kernel_el0_mappings(void)
790 {
791 arm_vm_prepare_kernel_el0_mappings(TRUE);
792 }
793 #endif /* __ARM_KERNEL_PROTECT__ */
794
795 #if defined(KERNEL_INTEGRITY_KTRR) || defined(KERNEL_INTEGRITY_CTRR)
796 extern void bootstrap_instructions;
797
798 /*
799 * arm_replace_identity_map takes the V=P map that we construct in start.s
800 * and repurposes it in order to have it map only the page we need in order
801 * to turn on the MMU. This prevents us from running into issues where
802 * KTRR will cause us to fault on executable block mappings that cross the
803 * KTRR boundary.
804 */
805 static void
806 arm_replace_identity_map(void)
807 {
808 vm_offset_t addr;
809 pmap_paddr_t paddr;
810
811 pmap_paddr_t l1_ptp_phys = 0;
812 tt_entry_t *l1_ptp_virt = NULL;
813 tt_entry_t *tte1 = NULL;
814 pmap_paddr_t l2_ptp_phys = 0;
815 tt_entry_t *l2_ptp_virt = NULL;
816 tt_entry_t *tte2 = NULL;
817 pmap_paddr_t l3_ptp_phys = 0;
818 pt_entry_t *l3_ptp_virt = NULL;
819 pt_entry_t *ptep = NULL;
820
821 addr = ((vm_offset_t)&bootstrap_instructions) & ~ARM_PGMASK;
822 paddr = kvtophys(addr);
823
824 /*
825 * Grab references to the V=P page tables, and allocate an L3 page.
826 */
827 l1_ptp_phys = kvtophys((vm_offset_t)&bootstrap_pagetables);
828 l1_ptp_virt = (tt_entry_t *)phystokv(l1_ptp_phys);
829 tte1 = &l1_ptp_virt[L1_TABLE_INDEX(paddr)];
830
831 l2_ptp_virt = L2_TABLE_VA(tte1);
832 l2_ptp_phys = (*tte1) & ARM_TTE_TABLE_MASK;
833 tte2 = &l2_ptp_virt[L2_TABLE_INDEX(paddr)];
834
835 l3_ptp_virt = (pt_entry_t *)alloc_ptpage(TRUE);
836 l3_ptp_phys = kvtophys((vm_offset_t)l3_ptp_virt);
837 ptep = &l3_ptp_virt[L3_TABLE_INDEX(paddr)];
838
839 /*
840 * Replace the large V=P mapping with a mapping that provides only the
841 * mappings needed to turn on the MMU.
842 */
843
844 bzero(l1_ptp_virt, ARM_PGBYTES);
845 *tte1 = ARM_TTE_BOOT_TABLE | (l2_ptp_phys & ARM_TTE_TABLE_MASK);
846
847 bzero(l2_ptp_virt, ARM_PGBYTES);
848 *tte2 = ARM_TTE_BOOT_TABLE | (l3_ptp_phys & ARM_TTE_TABLE_MASK);
849
850 *ptep = (paddr & ARM_PTE_MASK) |
851 ARM_PTE_TYPE_VALID |
852 ARM_PTE_SH(SH_OUTER_MEMORY) |
853 ARM_PTE_ATTRINDX(CACHE_ATTRINDX_WRITEBACK) |
854 ARM_PTE_AF |
855 ARM_PTE_AP(AP_RONA) |
856 ARM_PTE_NX;
857 }
858 #endif /* defined(KERNEL_INTEGRITY_KTRR) || defined(KERNEL_INTEGRITY_CTRR) */
859
860 tt_entry_t *arm_kva_to_tte(vm_offset_t);
861
862 tt_entry_t *
863 arm_kva_to_tte(vm_offset_t va)
864 {
865 tt_entry_t *tte1, *tte2;
866 tte1 = cpu_tte + L1_TABLE_INDEX(va);
867 tte2 = L2_TABLE_VA(tte1) + L2_TABLE_INDEX(va);
868
869 return tte2;
870 }
871
872 #if XNU_MONITOR
873
874 static inline pt_entry_t *
875 arm_kva_to_pte(vm_offset_t va)
876 {
877 tt_entry_t *tte2 = arm_kva_to_tte(va);
878 return L3_TABLE_VA(tte2) + L3_TABLE_INDEX(va);
879 }
880
881 #endif
882
883 #define ARM64_GRANULE_ALLOW_BLOCK (1 << 0)
884 #define ARM64_GRANULE_ALLOW_HINT (1 << 1)
885
886 /*
887 * arm_vm_page_granular_helper updates protections at the L3 level. It will (if
888 * neccessary) allocate a page for the L3 table and update the corresponding L2
889 * entry. Then, it will iterate over the L3 table, updating protections as necessary.
890 * This expects to be invoked on a L2 entry or sub L2 entry granularity, so this should
891 * not be invoked from a context that does not do L2 iteration separately (basically,
892 * don't call this except from arm_vm_page_granular_prot).
893 *
894 * unsigned granule: 0 => force to page granule, or a combination of
895 * ARM64_GRANULE_* flags declared above.
896 */
897
898 static void
899 arm_vm_page_granular_helper(vm_offset_t start, vm_offset_t _end, vm_offset_t va, pmap_paddr_t pa_offset,
900 int pte_prot_APX, int pte_prot_XN, unsigned granule,
901 pt_entry_t **deferred_pte, pt_entry_t *deferred_ptmp)
902 {
903 if (va & ARM_TT_L2_OFFMASK) { /* ragged edge hanging over a ARM_TT_L2_SIZE boundary */
904 tt_entry_t *tte2;
905 tt_entry_t tmplate;
906 pmap_paddr_t pa;
907 pt_entry_t *ppte, *recursive_pte = NULL, ptmp, recursive_ptmp = 0;
908 addr64_t ppte_phys;
909 unsigned i;
910
911 va &= ~ARM_TT_L2_OFFMASK;
912 pa = va - gVirtBase + gPhysBase - pa_offset;
913
914 if (pa >= real_avail_end) {
915 return;
916 }
917
918 tte2 = arm_kva_to_tte(va);
919
920 assert(_end >= va);
921 tmplate = *tte2;
922
923 if (ARM_TTE_TYPE_TABLE == (tmplate & ARM_TTE_TYPE_MASK)) {
924 /* pick up the existing page table. */
925 ppte = (pt_entry_t *)phystokv((tmplate & ARM_TTE_TABLE_MASK));
926 } else {
927 // TTE must be reincarnated with page level mappings.
928
929 // ... but we don't want to break up blocks on live
930 // translation tables.
931 assert(!kva_active);
932
933 ppte = (pt_entry_t*)alloc_ptpage(pa_offset == 0);
934 bzero(ppte, ARM_PGBYTES);
935 ppte_phys = kvtophys((vm_offset_t)ppte);
936
937 *tte2 = pa_to_tte(ppte_phys) | ARM_TTE_TYPE_TABLE | ARM_TTE_VALID;
938 }
939
940 vm_offset_t len = _end - va;
941 if ((pa + len) > real_avail_end) {
942 _end -= (pa + len - real_avail_end);
943 }
944 assert((start - gVirtBase + gPhysBase - pa_offset) >= gPhysBase);
945
946 /* Round up to the nearest PAGE_SIZE boundary when creating mappings:
947 * PAGE_SIZE may be a multiple of ARM_PGBYTES, and we don't want to leave
948 * a ragged non-PAGE_SIZE-aligned edge. */
949 vm_offset_t rounded_end = round_page(_end);
950 /* Apply the desired protections to the specified page range */
951 for (i = 0; i <= (ARM_TT_L3_INDEX_MASK >> ARM_TT_L3_SHIFT); i++) {
952 if ((start <= va) && (va < rounded_end)) {
953 ptmp = pa | ARM_PTE_AF | ARM_PTE_SH(SH_OUTER_MEMORY) | ARM_PTE_TYPE;
954 ptmp = ptmp | ARM_PTE_ATTRINDX(CACHE_ATTRINDX_DEFAULT);
955 ptmp = ptmp | ARM_PTE_AP(pte_prot_APX);
956 ptmp = ptmp | ARM_PTE_NX;
957 #if __ARM_KERNEL_PROTECT__
958 ptmp = ptmp | ARM_PTE_NG;
959 #endif /* __ARM_KERNEL_PROTECT__ */
960
961 if (pte_prot_XN) {
962 ptmp = ptmp | ARM_PTE_PNX;
963 }
964
965 /*
966 * If we can, apply the contiguous hint to this range. The hint is
967 * applicable if the current address falls within a hint-sized range that will
968 * be fully covered by this mapping request.
969 */
970 if ((va >= round_up_pte_hint_address(start)) && (round_up_pte_hint_address(va + 1) <= _end) &&
971 (granule & ARM64_GRANULE_ALLOW_HINT) && use_contiguous_hint) {
972 assert((va & ((1 << ARM_PTE_HINT_ADDR_SHIFT) - 1)) == ((pa & ((1 << ARM_PTE_HINT_ADDR_SHIFT) - 1))));
973 ptmp |= ARM_PTE_HINT;
974 /* Do not attempt to reapply the hint bit to an already-active mapping.
975 * This very likely means we're attempting to change attributes on an already-active mapping,
976 * which violates the requirement of the hint bit.*/
977 assert(!kva_active || (ppte[i] == ARM_PTE_TYPE_FAULT));
978 }
979 /*
980 * Do not change the contiguous bit on an active mapping. Even in a single-threaded
981 * environment, it's possible for prefetch to produce a TLB conflict by trying to pull in
982 * a hint-sized entry on top of one or more existing page-sized entries. It's also useful
983 * to make sure we're not trying to unhint a sub-range of a larger hinted range, which
984 * could produce a later TLB conflict.
985 */
986 assert(!kva_active || (ppte[i] == ARM_PTE_TYPE_FAULT) || ((ppte[i] & ARM_PTE_HINT) == (ptmp & ARM_PTE_HINT)));
987
988 /*
989 * If we reach an entry that maps the current pte page, delay updating it until the very end.
990 * Otherwise we might end up making the PTE page read-only, leading to a fault later on in
991 * this function if we manage to outrun the TLB. This can happen on KTRR-enabled devices when
992 * marking segDATACONST read-only. Mappings for this region may straddle a PT page boundary,
993 * so we must also defer assignment of the following PTE. We will assume that if the region
994 * were to require one or more full L3 pages, it would instead use L2 blocks where possible,
995 * therefore only requiring at most one L3 page at the beginning and one at the end.
996 */
997 if (kva_active && ((pt_entry_t*)(phystokv(pa)) == ppte)) {
998 assert(recursive_pte == NULL);
999 assert(granule & ARM64_GRANULE_ALLOW_BLOCK);
1000 recursive_pte = &ppte[i];
1001 recursive_ptmp = ptmp;
1002 } else if ((deferred_pte != NULL) && (&ppte[i] == &recursive_pte[1])) {
1003 assert(*deferred_pte == NULL);
1004 assert(deferred_ptmp != NULL);
1005 *deferred_pte = &ppte[i];
1006 *deferred_ptmp = ptmp;
1007 } else {
1008 ppte[i] = ptmp;
1009 }
1010 }
1011
1012 va += ARM_PGBYTES;
1013 pa += ARM_PGBYTES;
1014 }
1015 if (recursive_pte != NULL) {
1016 *recursive_pte = recursive_ptmp;
1017 }
1018 }
1019 }
1020
1021 /*
1022 * arm_vm_page_granular_prot updates protections by iterating over the L2 entries and
1023 * changing them. If a particular chunk necessitates L3 entries (for reasons of
1024 * alignment or length, or an explicit request that the entry be fully expanded), we
1025 * hand off to arm_vm_page_granular_helper to deal with the L3 chunk of the logic.
1026 */
1027 static void
1028 arm_vm_page_granular_prot(vm_offset_t start, unsigned long size, pmap_paddr_t pa_offset,
1029 int tte_prot_XN, int pte_prot_APX, int pte_prot_XN,
1030 unsigned granule)
1031 {
1032 pt_entry_t *deferred_pte = NULL, deferred_ptmp = 0;
1033 vm_offset_t _end = start + size;
1034 vm_offset_t align_start = (start + ARM_TT_L2_OFFMASK) & ~ARM_TT_L2_OFFMASK;
1035
1036 if (size == 0x0UL) {
1037 return;
1038 }
1039
1040 if (align_start > _end) {
1041 arm_vm_page_granular_helper(start, _end, start, pa_offset, pte_prot_APX, pte_prot_XN, granule, NULL, NULL);
1042 return;
1043 }
1044
1045 arm_vm_page_granular_helper(start, align_start, start, pa_offset, pte_prot_APX, pte_prot_XN, granule, &deferred_pte, &deferred_ptmp);
1046
1047 while ((_end - align_start) >= ARM_TT_L2_SIZE) {
1048 if (!(granule & ARM64_GRANULE_ALLOW_BLOCK)) {
1049 arm_vm_page_granular_helper(align_start, align_start + ARM_TT_L2_SIZE, align_start + 1, pa_offset,
1050 pte_prot_APX, pte_prot_XN, granule, NULL, NULL);
1051 } else {
1052 pmap_paddr_t pa = align_start - gVirtBase + gPhysBase - pa_offset;
1053 assert((pa & ARM_TT_L2_OFFMASK) == 0);
1054 tt_entry_t *tte2;
1055 tt_entry_t tmplate;
1056
1057 tte2 = arm_kva_to_tte(align_start);
1058
1059 if ((pa >= gPhysBase) && (pa < real_avail_end)) {
1060 tmplate = (pa & ARM_TTE_BLOCK_L2_MASK) | ARM_TTE_TYPE_BLOCK
1061 | ARM_TTE_VALID | ARM_TTE_BLOCK_AF | ARM_TTE_BLOCK_NX
1062 | ARM_TTE_BLOCK_AP(pte_prot_APX) | ARM_TTE_BLOCK_SH(SH_OUTER_MEMORY)
1063 | ARM_TTE_BLOCK_ATTRINDX(CACHE_ATTRINDX_WRITEBACK);
1064
1065 #if __ARM_KERNEL_PROTECT__
1066 tmplate = tmplate | ARM_TTE_BLOCK_NG;
1067 #endif /* __ARM_KERNEL_PROTECT__ */
1068 if (tte_prot_XN) {
1069 tmplate = tmplate | ARM_TTE_BLOCK_PNX;
1070 }
1071
1072 *tte2 = tmplate;
1073 }
1074 }
1075 align_start += ARM_TT_L2_SIZE;
1076 }
1077
1078 if (align_start < _end) {
1079 arm_vm_page_granular_helper(align_start, _end, _end, pa_offset, pte_prot_APX, pte_prot_XN, granule, &deferred_pte, &deferred_ptmp);
1080 }
1081
1082 if (deferred_pte != NULL) {
1083 *deferred_pte = deferred_ptmp;
1084 }
1085 }
1086
1087 static inline void
1088 arm_vm_page_granular_RNX(vm_offset_t start, unsigned long size, unsigned granule)
1089 {
1090 arm_vm_page_granular_prot(start, size, 0, 1, AP_RONA, 1, granule);
1091 }
1092
1093 static inline void
1094 arm_vm_page_granular_ROX(vm_offset_t start, unsigned long size, unsigned granule)
1095 {
1096 arm_vm_page_granular_prot(start, size, 0, 0, AP_RONA, 0, granule);
1097 }
1098
1099 static inline void
1100 arm_vm_page_granular_RWNX(vm_offset_t start, unsigned long size, unsigned granule)
1101 {
1102 arm_vm_page_granular_prot(start, size, 0, 1, AP_RWNA, 1, granule);
1103 }
1104
1105 /* used in the chosen/memory-map node, populated by iBoot. */
1106 typedef struct MemoryMapFileInfo {
1107 vm_offset_t paddr;
1108 size_t length;
1109 } MemoryMapFileInfo;
1110
1111 // Populate seg...AuxKC and fixup AuxKC permissions
1112 static bool
1113 arm_vm_auxkc_init(void)
1114 {
1115 if (auxkc_mh == 0 || auxkc_base == 0) {
1116 return false; // no auxKC.
1117 }
1118
1119 /* Fixup AuxKC and populate seg*AuxKC globals used below */
1120 arm_auxkc_init((void*)auxkc_mh, (void*)auxkc_base);
1121
1122 if (segLOWESTAuxKC != segLOWEST) {
1123 panic("segLOWESTAuxKC (%p) not equal to segLOWEST (%p). auxkc_mh: %p, auxkc_base: %p",
1124 (void*)segLOWESTAuxKC, (void*)segLOWEST,
1125 (void*)auxkc_mh, (void*)auxkc_base);
1126 }
1127
1128 /*
1129 * The AuxKC LINKEDIT segment needs to be covered by the RO region but is excluded
1130 * from the RO address range returned by kernel_collection_adjust_mh_addrs().
1131 * Ensure the highest non-LINKEDIT address in the AuxKC is the current end of
1132 * its RO region before extending it.
1133 */
1134 assert(segHIGHESTROAuxKC == segHIGHESTNLEAuxKC);
1135 assert(segHIGHESTAuxKC >= segHIGHESTROAuxKC);
1136 if (segHIGHESTAuxKC > segHIGHESTROAuxKC) {
1137 segHIGHESTROAuxKC = segHIGHESTAuxKC;
1138 }
1139
1140 /*
1141 * The AuxKC RO region must be right below the device tree/trustcache so that it can be covered
1142 * by CTRR, and the AuxKC RX region must be within the RO region.
1143 */
1144 assert(segHIGHESTROAuxKC == auxkc_right_above);
1145 assert(segHIGHESTRXAuxKC <= segHIGHESTROAuxKC);
1146 assert(segLOWESTRXAuxKC <= segHIGHESTRXAuxKC);
1147 assert(segLOWESTROAuxKC <= segLOWESTRXAuxKC);
1148 assert(segLOWESTAuxKC <= segLOWESTROAuxKC);
1149
1150 if (segHIGHESTRXAuxKC < segLOWEST) {
1151 arm_vm_page_granular_RNX(segHIGHESTRXAuxKC, segLOWEST - segHIGHESTRXAuxKC, 0);
1152 }
1153 if (segLOWESTRXAuxKC < segHIGHESTRXAuxKC) {
1154 arm_vm_page_granular_ROX(segLOWESTRXAuxKC, segHIGHESTRXAuxKC - segLOWESTRXAuxKC, 0); // Refined in OSKext::readPrelinkedExtensions
1155 }
1156 if (segLOWESTROAuxKC < segLOWESTRXAuxKC) {
1157 arm_vm_page_granular_RNX(segLOWESTROAuxKC, segLOWESTRXAuxKC - segLOWESTROAuxKC, 0);
1158 }
1159 if (segLOWESTAuxKC < segLOWESTROAuxKC) {
1160 arm_vm_page_granular_RWNX(segLOWESTAuxKC, segLOWESTROAuxKC - segLOWESTAuxKC, 0);
1161 }
1162
1163 return true;
1164 }
1165
1166 void
1167 arm_vm_prot_init(__unused boot_args * args)
1168 {
1169 segLOWESTTEXT = UINT64_MAX;
1170 if (segSizePRELINKTEXT && (segPRELINKTEXTB < segLOWESTTEXT)) {
1171 segLOWESTTEXT = segPRELINKTEXTB;
1172 }
1173 assert(segSizeTEXT);
1174 if (segTEXTB < segLOWESTTEXT) {
1175 segLOWESTTEXT = segTEXTB;
1176 }
1177 assert(segLOWESTTEXT < UINT64_MAX);
1178
1179 segEXTRADATA = segLOWESTTEXT;
1180 segSizeEXTRADATA = 0;
1181
1182 segLOWEST = segLOWESTTEXT;
1183 segLOWESTRO = segLOWESTTEXT;
1184
1185 if (segLOWESTKC && segLOWESTKC < segLOWEST) {
1186 /*
1187 * kernel collections have segments below the kernel. In particular the collection mach header
1188 * is below PRELINK_TEXT and is not covered by any other segments already tracked.
1189 */
1190 arm_vm_page_granular_RNX(segLOWESTKC, segLOWEST - segLOWESTKC, ARM64_GRANULE_ALLOW_BLOCK | ARM64_GRANULE_ALLOW_HINT);
1191 segLOWEST = segLOWESTKC;
1192 if (segLOWESTROKC && segLOWESTROKC < segLOWESTRO) {
1193 segLOWESTRO = segLOWESTROKC;
1194 }
1195 if (segHIGHESTROKC && segHIGHESTROKC > segHIGHESTRO) {
1196 segHIGHESTRO = segHIGHESTROKC;
1197 }
1198 }
1199
1200 DTEntry memory_map;
1201 MemoryMapFileInfo const *trustCacheRange;
1202 unsigned int trustCacheRangeSize;
1203 int err;
1204
1205 if (SecureDTIsLockedDown()) {
1206 segEXTRADATA = (vm_offset_t)PE_state.deviceTreeHead;
1207 segSizeEXTRADATA = PE_state.deviceTreeSize;
1208 }
1209
1210 err = SecureDTLookupEntry(NULL, "chosen/memory-map", &memory_map);
1211 assert(err == kSuccess);
1212
1213 err = SecureDTGetProperty(memory_map, "TrustCache", (void const **)&trustCacheRange, &trustCacheRangeSize);
1214 if (err == kSuccess) {
1215 assert(trustCacheRangeSize == sizeof(MemoryMapFileInfo));
1216
1217 if (segSizeEXTRADATA == 0) {
1218 segEXTRADATA = phystokv(trustCacheRange->paddr);
1219 segSizeEXTRADATA = trustCacheRange->length;
1220 } else {
1221 segSizeEXTRADATA += trustCacheRange->length;
1222 }
1223 }
1224
1225 if (segSizeEXTRADATA != 0) {
1226 if (segEXTRADATA <= segLOWEST) {
1227 segLOWEST = segEXTRADATA;
1228 if (segEXTRADATA <= segLOWESTRO) {
1229 segLOWESTRO = segEXTRADATA;
1230 }
1231 }
1232 #if !(DEBUG || DEVELOPMENT)
1233
1234
1235 else {
1236 panic("EXTRADATA is in an unexpected place: %#lx > %#lx", segEXTRADATA, segLOWEST);
1237 }
1238 #endif /* !(DEBUG || DEVELOPMENT) */
1239
1240 arm_vm_page_granular_RNX(segEXTRADATA, segSizeEXTRADATA, ARM64_GRANULE_ALLOW_BLOCK | ARM64_GRANULE_ALLOW_HINT);
1241 }
1242
1243 const MemoryMapFileInfo *auxKC_range, *auxKC_header_range;
1244 unsigned int auxKC_range_size, auxKC_header_range_size;
1245
1246 err = SecureDTGetProperty(memory_map, "AuxKC", (const void**)&auxKC_range,
1247 &auxKC_range_size);
1248 if (err != kSuccess) {
1249 goto noAuxKC;
1250 }
1251 assert(auxKC_range_size == sizeof(MemoryMapFileInfo));
1252 err = SecureDTGetProperty(memory_map, "AuxKC-mach_header",
1253 (const void**)&auxKC_header_range, &auxKC_header_range_size);
1254 if (err != kSuccess) {
1255 goto noAuxKC;
1256 }
1257 assert(auxKC_header_range_size == sizeof(MemoryMapFileInfo));
1258
1259 auxkc_mh = phystokv(auxKC_header_range->paddr);
1260 auxkc_base = phystokv(auxKC_range->paddr);
1261 if (!auxkc_mh || !auxkc_base) {
1262 goto noAuxKC;
1263 }
1264
1265 if (auxkc_base < segLOWEST) {
1266 auxkc_right_above = segLOWEST;
1267 segLOWEST = auxkc_base;
1268 } else {
1269 panic("auxkc_base (%p) not below segLOWEST (%p)", (void*)auxkc_base, (void*)segLOWEST);
1270 }
1271
1272 /* Map AuxKC RWNX initially so that arm_vm_auxkc_init can traverse
1273 * it and apply fixups (after we're off the bootstrap translation
1274 * tables).
1275 */
1276 arm_vm_page_granular_RWNX(auxkc_base, auxKC_range->length, 0);
1277
1278 noAuxKC:
1279 /* Map coalesced kext TEXT segment RWNX for now */
1280 arm_vm_page_granular_RWNX(segPRELINKTEXTB, segSizePRELINKTEXT, ARM64_GRANULE_ALLOW_BLOCK); // Refined in OSKext::readPrelinkedExtensions
1281
1282 /* Map coalesced kext DATA_CONST segment RWNX (could be empty) */
1283 arm_vm_page_granular_RWNX(segPLKDATACONSTB, segSizePLKDATACONST, ARM64_GRANULE_ALLOW_BLOCK); // Refined in OSKext::readPrelinkedExtensions
1284
1285 /* Map coalesced kext TEXT_EXEC segment RX (could be empty) */
1286 arm_vm_page_granular_ROX(segPLKTEXTEXECB, segSizePLKTEXTEXEC, ARM64_GRANULE_ALLOW_BLOCK | ARM64_GRANULE_ALLOW_HINT); // Refined in OSKext::readPrelinkedExtensions
1287
1288 /* if new segments not present, set space between PRELINK_TEXT and xnu TEXT to RWNX
1289 * otherwise we no longer expect any space between the coalesced kext read only segments and xnu rosegments
1290 */
1291 if (!segSizePLKDATACONST && !segSizePLKTEXTEXEC) {
1292 if (segSizePRELINKTEXT) {
1293 arm_vm_page_granular_RWNX(segPRELINKTEXTB + segSizePRELINKTEXT, segTEXTB - (segPRELINKTEXTB + segSizePRELINKTEXT),
1294 ARM64_GRANULE_ALLOW_BLOCK | ARM64_GRANULE_ALLOW_HINT);
1295 }
1296 } else {
1297 /*
1298 * If we have the new segments, we should still protect the gap between kext
1299 * read-only pages and kernel read-only pages, in the event that this gap
1300 * exists.
1301 */
1302 if ((segPLKDATACONSTB + segSizePLKDATACONST) < segTEXTB) {
1303 arm_vm_page_granular_RWNX(segPLKDATACONSTB + segSizePLKDATACONST, segTEXTB - (segPLKDATACONSTB + segSizePLKDATACONST),
1304 ARM64_GRANULE_ALLOW_BLOCK | ARM64_GRANULE_ALLOW_HINT);
1305 }
1306 }
1307
1308 /*
1309 * Protection on kernel text is loose here to allow shenanigans early on. These
1310 * protections are tightened in arm_vm_prot_finalize(). This is necessary because
1311 * we currently patch LowResetVectorBase in cpu.c.
1312 *
1313 * TEXT segment contains mach headers and other non-executable data. This will become RONX later.
1314 */
1315 arm_vm_page_granular_RNX(segTEXTB, segSizeTEXT, ARM64_GRANULE_ALLOW_BLOCK | ARM64_GRANULE_ALLOW_HINT);
1316
1317 /* Can DATACONST start out and stay RNX?
1318 * NO, stuff in this segment gets modified during startup (viz. mac_policy_init()/mac_policy_list)
1319 * Make RNX in prot_finalize
1320 */
1321 arm_vm_page_granular_RWNX(segDATACONSTB, segSizeDATACONST, ARM64_GRANULE_ALLOW_BLOCK);
1322
1323 arm_vm_page_granular_ROX(segTEXTEXECB, segSizeTEXTEXEC, ARM64_GRANULE_ALLOW_BLOCK | ARM64_GRANULE_ALLOW_HINT);
1324
1325 #if XNU_MONITOR
1326 arm_vm_page_granular_ROX(segPPLTEXTB, segSizePPLTEXT, ARM64_GRANULE_ALLOW_BLOCK | ARM64_GRANULE_ALLOW_HINT);
1327 arm_vm_page_granular_ROX(segPPLTRAMPB, segSizePPLTRAMP, ARM64_GRANULE_ALLOW_BLOCK | ARM64_GRANULE_ALLOW_HINT);
1328 arm_vm_page_granular_RNX(segPPLDATACONSTB, segSizePPLDATACONST, ARM64_GRANULE_ALLOW_BLOCK | ARM64_GRANULE_ALLOW_HINT);
1329 #endif
1330
1331 /* DATA segment will remain RWNX */
1332 arm_vm_page_granular_RWNX(segDATAB, segSizeDATA, ARM64_GRANULE_ALLOW_BLOCK | ARM64_GRANULE_ALLOW_HINT);
1333 #if XNU_MONITOR
1334 arm_vm_page_granular_RWNX(segPPLDATAB, segSizePPLDATA, ARM64_GRANULE_ALLOW_BLOCK | ARM64_GRANULE_ALLOW_HINT);
1335 #endif
1336
1337 arm_vm_page_granular_RWNX(segHIBDATAB, segSizeHIBDATA, ARM64_GRANULE_ALLOW_BLOCK | ARM64_GRANULE_ALLOW_HINT);
1338
1339 arm_vm_page_granular_RWNX(segBOOTDATAB, segSizeBOOTDATA, 0);
1340 arm_vm_page_granular_RNX((vm_offset_t)&intstack_low_guard, PAGE_MAX_SIZE, 0);
1341 arm_vm_page_granular_RNX((vm_offset_t)&intstack_high_guard, PAGE_MAX_SIZE, 0);
1342 arm_vm_page_granular_RNX((vm_offset_t)&excepstack_high_guard, PAGE_MAX_SIZE, 0);
1343
1344 arm_vm_page_granular_ROX(segKLDB, segSizeKLD, ARM64_GRANULE_ALLOW_BLOCK | ARM64_GRANULE_ALLOW_HINT);
1345 arm_vm_page_granular_RWNX(segLINKB, segSizeLINK, ARM64_GRANULE_ALLOW_BLOCK | ARM64_GRANULE_ALLOW_HINT);
1346 arm_vm_page_granular_RWNX(segPLKLINKEDITB, segSizePLKLINKEDIT, ARM64_GRANULE_ALLOW_BLOCK | ARM64_GRANULE_ALLOW_HINT); // Coalesced kext LINKEDIT segment
1347 arm_vm_page_granular_ROX(segLASTB, segSizeLAST, ARM64_GRANULE_ALLOW_BLOCK); // __LAST may be empty, but we cannot assume this
1348 if (segLASTDATACONSTB) {
1349 arm_vm_page_granular_RWNX(segLASTDATACONSTB, segSizeLASTDATACONST, ARM64_GRANULE_ALLOW_BLOCK); // __LASTDATA_CONST may be empty, but we cannot assume this
1350 }
1351 arm_vm_page_granular_RWNX(segPRELINKDATAB, segSizePRELINKDATA, ARM64_GRANULE_ALLOW_BLOCK | ARM64_GRANULE_ALLOW_HINT); // Prelink __DATA for kexts (RW data)
1352
1353 if (segSizePLKLLVMCOV > 0) {
1354 arm_vm_page_granular_RWNX(segPLKLLVMCOVB, segSizePLKLLVMCOV, ARM64_GRANULE_ALLOW_BLOCK | ARM64_GRANULE_ALLOW_HINT); // LLVM code coverage data
1355 }
1356 arm_vm_page_granular_RWNX(segPRELINKINFOB, segSizePRELINKINFO, ARM64_GRANULE_ALLOW_BLOCK | ARM64_GRANULE_ALLOW_HINT); /* PreLinkInfoDictionary */
1357
1358 /* Record the bounds of the kernelcache. */
1359 vm_kernelcache_base = segLOWEST;
1360 vm_kernelcache_top = end_kern;
1361 }
1362
1363 /*
1364 * return < 0 for a < b
1365 * 0 for a == b
1366 * > 0 for a > b
1367 */
1368 typedef int (*cmpfunc_t)(const void *a, const void *b);
1369
1370 extern void
1371 qsort(void *a, size_t n, size_t es, cmpfunc_t cmp);
1372
1373 static int
1374 cmp_ptov_entries(const void *a, const void *b)
1375 {
1376 const ptov_table_entry *entry_a = a;
1377 const ptov_table_entry *entry_b = b;
1378 // Sort in descending order of segment length
1379 if (entry_a->len < entry_b->len) {
1380 return 1;
1381 } else if (entry_a->len > entry_b->len) {
1382 return -1;
1383 } else {
1384 return 0;
1385 }
1386 }
1387
1388 SECURITY_READ_ONLY_LATE(static unsigned int) ptov_index = 0;
1389
1390 #define ROUND_L1(addr) (((addr) + ARM_TT_L1_OFFMASK) & ~(ARM_TT_L1_OFFMASK))
1391 #define ROUND_TWIG(addr) (((addr) + ARM_TT_TWIG_OFFMASK) & ~(ARM_TT_TWIG_OFFMASK))
1392
1393 static void
1394 arm_vm_physmap_slide(ptov_table_entry *temp_ptov_table, vm_map_address_t orig_va, vm_size_t len, int pte_prot_APX, unsigned granule)
1395 {
1396 pmap_paddr_t pa_offset;
1397
1398 assert(ptov_index < PTOV_TABLE_SIZE);
1399 assert((orig_va & ARM_PGMASK) == 0);
1400 temp_ptov_table[ptov_index].pa = orig_va - gVirtBase + gPhysBase;
1401 if (ptov_index == 0) {
1402 temp_ptov_table[ptov_index].va = physmap_base;
1403 } else {
1404 temp_ptov_table[ptov_index].va = temp_ptov_table[ptov_index - 1].va + temp_ptov_table[ptov_index - 1].len;
1405 }
1406 if (granule & ARM64_GRANULE_ALLOW_BLOCK) {
1407 vm_map_address_t orig_offset = temp_ptov_table[ptov_index].pa & ARM_TT_TWIG_OFFMASK;
1408 vm_map_address_t new_offset = temp_ptov_table[ptov_index].va & ARM_TT_TWIG_OFFMASK;
1409 if (new_offset < orig_offset) {
1410 temp_ptov_table[ptov_index].va += (orig_offset - new_offset);
1411 } else if (new_offset > orig_offset) {
1412 temp_ptov_table[ptov_index].va = ROUND_TWIG(temp_ptov_table[ptov_index].va) + orig_offset;
1413 }
1414 }
1415 assert((temp_ptov_table[ptov_index].va & ARM_PGMASK) == 0);
1416 temp_ptov_table[ptov_index].len = round_page(len);
1417 pa_offset = temp_ptov_table[ptov_index].va - orig_va;
1418 arm_vm_page_granular_prot(temp_ptov_table[ptov_index].va, temp_ptov_table[ptov_index].len, pa_offset, 1, pte_prot_APX, 1, granule);
1419 ++ptov_index;
1420 }
1421
1422 #if XNU_MONITOR
1423
1424 SECURITY_READ_ONLY_LATE(static boolean_t) keep_linkedit = FALSE;
1425
1426 static void
1427 arm_vm_physmap_init(boot_args *args)
1428 {
1429 ptov_table_entry temp_ptov_table[PTOV_TABLE_SIZE];
1430 bzero(temp_ptov_table, sizeof(temp_ptov_table));
1431
1432 // This is memory that will either be handed back to the VM layer via ml_static_mfree(),
1433 // or will be available for general-purpose use. Physical aperture mappings for this memory
1434 // must be at page granularity, so that PPL ownership or cache attribute changes can be reflected
1435 // in the physical aperture mappings.
1436
1437 // Slid region between gPhysBase and beginning of protected text
1438 arm_vm_physmap_slide(temp_ptov_table, gVirtBase, segLOWEST - gVirtBase, AP_RWNA, 0);
1439
1440 // kext bootstrap segment
1441 arm_vm_physmap_slide(temp_ptov_table, segKLDB, segSizeKLD, AP_RONA, 0);
1442
1443 // Early-boot data
1444 arm_vm_physmap_slide(temp_ptov_table, segBOOTDATAB, segSizeBOOTDATA, AP_RONA, 0);
1445
1446 #if KASAN_DYNAMIC_BLACKLIST
1447 /* KASAN's dynamic blacklist needs to query the LINKEDIT segment at runtime. As such, the
1448 * kext bootstrap code will not jettison LINKEDIT on kasan kernels, so don't bother to relocate it. */
1449 keep_linkedit = TRUE;
1450 #else
1451 PE_parse_boot_argn("keepsyms", &keep_linkedit, sizeof(keep_linkedit));
1452 if (kernel_mach_header_is_in_fileset(&_mh_execute_header)) {
1453 keep_linkedit = TRUE;
1454 }
1455 #endif
1456 if (!keep_linkedit) {
1457 // Kernel LINKEDIT
1458 arm_vm_physmap_slide(temp_ptov_table, segLINKB, segSizeLINK, AP_RWNA, 0);
1459
1460 // Prelinked kernel LINKEDIT
1461 arm_vm_physmap_slide(temp_ptov_table, segPLKLINKEDITB, segSizePLKLINKEDIT, AP_RWNA, 0);
1462 }
1463
1464 // Prelinked kernel plists
1465 arm_vm_physmap_slide(temp_ptov_table, segPRELINKINFOB, segSizePRELINKINFO, AP_RWNA, 0);
1466
1467 // Device tree (if not locked down), ramdisk, boot args
1468 arm_vm_physmap_slide(temp_ptov_table, end_kern, (args->topOfKernelData - gPhysBase + gVirtBase) - end_kern, AP_RWNA, 0);
1469 if (!SecureDTIsLockedDown()) {
1470 PE_slide_devicetree(temp_ptov_table[ptov_index - 1].va - end_kern);
1471 }
1472
1473 // Remainder of physical memory
1474 arm_vm_physmap_slide(temp_ptov_table, (args->topOfKernelData - gPhysBase + gVirtBase),
1475 real_avail_end - args->topOfKernelData, AP_RWNA, 0);
1476
1477 assert((temp_ptov_table[ptov_index - 1].va + temp_ptov_table[ptov_index - 1].len) <= physmap_end);
1478
1479 // Sort in descending order of segment length. LUT traversal is linear, so largest (most likely used)
1480 // segments should be placed earliest in the table to optimize lookup performance.
1481 qsort(temp_ptov_table, PTOV_TABLE_SIZE, sizeof(temp_ptov_table[0]), cmp_ptov_entries);
1482
1483 memcpy(ptov_table, temp_ptov_table, sizeof(ptov_table));
1484 }
1485
1486 #else
1487
1488 static void
1489 arm_vm_physmap_init(boot_args *args)
1490 {
1491 ptov_table_entry temp_ptov_table[PTOV_TABLE_SIZE];
1492 bzero(temp_ptov_table, sizeof(temp_ptov_table));
1493
1494 // Will be handed back to VM layer through ml_static_mfree() in arm_vm_prot_finalize()
1495 arm_vm_physmap_slide(temp_ptov_table, gVirtBase, segLOWEST - gVirtBase, AP_RWNA,
1496 ARM64_GRANULE_ALLOW_BLOCK | ARM64_GRANULE_ALLOW_HINT);
1497
1498 arm_vm_page_granular_RWNX(end_kern, phystokv(args->topOfKernelData) - end_kern,
1499 ARM64_GRANULE_ALLOW_BLOCK | ARM64_GRANULE_ALLOW_HINT); /* Device Tree (if not locked down), RAM Disk (if present), bootArgs */
1500
1501 arm_vm_physmap_slide(temp_ptov_table, (args->topOfKernelData - gPhysBase + gVirtBase),
1502 real_avail_end - args->topOfKernelData, AP_RWNA, ARM64_GRANULE_ALLOW_BLOCK | ARM64_GRANULE_ALLOW_HINT); // rest of physmem
1503
1504 assert((temp_ptov_table[ptov_index - 1].va + temp_ptov_table[ptov_index - 1].len) <= physmap_end);
1505
1506 // Sort in descending order of segment length. LUT traversal is linear, so largest (most likely used)
1507 // segments should be placed earliest in the table to optimize lookup performance.
1508 qsort(temp_ptov_table, PTOV_TABLE_SIZE, sizeof(temp_ptov_table[0]), cmp_ptov_entries);
1509
1510 memcpy(ptov_table, temp_ptov_table, sizeof(ptov_table));
1511 }
1512
1513 #endif // XNU_MONITOR
1514
1515 void
1516 arm_vm_prot_finalize(boot_args * args __unused)
1517 {
1518 /*
1519 * At this point, we are far enough along in the boot process that it will be
1520 * safe to free up all of the memory preceeding the kernel. It may in fact
1521 * be safe to do this earlier.
1522 *
1523 * This keeps the memory in the V-to-P mapping, but advertises it to the VM
1524 * as usable.
1525 */
1526
1527 /*
1528 * if old style PRELINK segment exists, free memory before it, and after it before XNU text
1529 * otherwise we're dealing with a new style kernel cache, so we should just free the
1530 * memory before PRELINK_TEXT segment, since the rest of the KEXT read only data segments
1531 * should be immediately followed by XNU's TEXT segment
1532 */
1533
1534 ml_static_mfree(phystokv(gPhysBase), segLOWEST - gVirtBase);
1535
1536 /*
1537 * KTRR support means we will be mucking with these pages and trying to
1538 * protect them; we cannot free the pages to the VM if we do this.
1539 */
1540 if (!segSizePLKDATACONST && !segSizePLKTEXTEXEC && segSizePRELINKTEXT) {
1541 /* If new segments not present, PRELINK_TEXT is not dynamically sized, free DRAM between it and xnu TEXT */
1542 ml_static_mfree(segPRELINKTEXTB + segSizePRELINKTEXT, segTEXTB - (segPRELINKTEXTB + segSizePRELINKTEXT));
1543 }
1544
1545 /* tighten permissions on kext read only data and code */
1546 arm_vm_page_granular_RNX(segPRELINKTEXTB, segSizePRELINKTEXT, ARM64_GRANULE_ALLOW_BLOCK);
1547 arm_vm_page_granular_RNX(segPLKDATACONSTB, segSizePLKDATACONST, ARM64_GRANULE_ALLOW_BLOCK);
1548
1549 cpu_stack_alloc(&BootCpuData);
1550 arm64_replace_bootstack(&BootCpuData);
1551 ml_static_mfree(phystokv(segBOOTDATAB - gVirtBase + gPhysBase), segSizeBOOTDATA);
1552
1553 #if __ARM_KERNEL_PROTECT__
1554 arm_vm_populate_kernel_el0_mappings();
1555 #endif /* __ARM_KERNEL_PROTECT__ */
1556
1557 #if XNU_MONITOR
1558 for (vm_offset_t va = segKLDB; va < (segKLDB + segSizeKLD); va += ARM_PGBYTES) {
1559 pt_entry_t *pte = arm_kva_to_pte(va);
1560 *pte = ARM_PTE_EMPTY;
1561 }
1562 /* Clear the original stack mappings; these pages should be mapped through ptov_table. */
1563 for (vm_offset_t va = segBOOTDATAB; va < (segBOOTDATAB + segSizeBOOTDATA); va += ARM_PGBYTES) {
1564 pt_entry_t *pte = arm_kva_to_pte(va);
1565 *pte = ARM_PTE_EMPTY;
1566 }
1567 /* Clear the original PRELINKINFO mapping. This segment should be jettisoned during I/O Kit
1568 * initialization before we reach this point. */
1569 for (vm_offset_t va = segPRELINKINFOB; va < (segPRELINKINFOB + segSizePRELINKINFO); va += ARM_PGBYTES) {
1570 pt_entry_t *pte = arm_kva_to_pte(va);
1571 *pte = ARM_PTE_EMPTY;
1572 }
1573 if (!keep_linkedit) {
1574 for (vm_offset_t va = segLINKB; va < (segLINKB + segSizeLINK); va += ARM_PGBYTES) {
1575 pt_entry_t *pte = arm_kva_to_pte(va);
1576 *pte = ARM_PTE_EMPTY;
1577 }
1578 for (vm_offset_t va = segPLKLINKEDITB; va < (segPLKLINKEDITB + segSizePLKLINKEDIT); va += ARM_PGBYTES) {
1579 pt_entry_t *pte = arm_kva_to_pte(va);
1580 *pte = ARM_PTE_EMPTY;
1581 }
1582 }
1583 #endif /* XNU_MONITOR */
1584
1585 #if defined(KERNEL_INTEGRITY_KTRR) || defined(KERNEL_INTEGRITY_CTRR)
1586 /*
1587 * __LAST,__pinst should no longer be executable.
1588 */
1589 arm_vm_page_granular_RNX(segLASTB, segSizeLAST, ARM64_GRANULE_ALLOW_BLOCK);
1590
1591 /* __LASTDATA_CONST should no longer be writable. */
1592 if (segLASTDATACONSTB) {
1593 arm_vm_page_granular_RNX(segLASTDATACONSTB, segSizeLASTDATACONST, ARM64_GRANULE_ALLOW_BLOCK);
1594 }
1595
1596 /*
1597 * Must wait until all other region permissions are set before locking down DATA_CONST
1598 * as the kernel static page tables live in DATA_CONST on KTRR enabled systems
1599 * and will become immutable.
1600 */
1601 #endif
1602
1603 arm_vm_page_granular_RNX(segDATACONSTB, segSizeDATACONST, ARM64_GRANULE_ALLOW_BLOCK);
1604
1605 __builtin_arm_dsb(DSB_ISH);
1606 flush_mmu_tlb();
1607 }
1608
1609 #define TBI_USER 0x1
1610 #define TBI_KERNEL 0x2
1611
1612 /*
1613 * TBI (top-byte ignore) is an ARMv8 feature for ignoring the top 8 bits of
1614 * address accesses. It can be enabled separately for TTBR0 (user) and
1615 * TTBR1 (kernel). We enable it by default for user only.
1616 */
1617 static void
1618 set_tbi(void)
1619 {
1620 #if !__ARM_KERNEL_PROTECT__
1621 uint64_t old_tcr, new_tcr;
1622
1623 old_tcr = new_tcr = get_tcr();
1624 new_tcr |= TCR_TBI0_TOPBYTE_IGNORED;
1625
1626 if (old_tcr != new_tcr) {
1627 set_tcr(new_tcr);
1628 sysreg_restore.tcr_el1 = new_tcr;
1629 }
1630 #endif /* !__ARM_KERNEL_PROTECT__ */
1631 }
1632
1633 /*
1634 * Initialize and enter blank (invalid) page tables in a L1 translation table for a given VA range.
1635 *
1636 * This is a helper function used to build up the initial page tables for the kernel translation table.
1637 * With KERNEL_INTEGRITY we keep at least the root level of the kernel page table immutable, thus the need
1638 * to preallocate before machine_lockdown any L1 entries necessary during the entire kernel runtime.
1639 *
1640 * For a given VA range, if necessary, allocate new L2 translation tables and install the table entries in
1641 * the appropriate L1 table indexes. called before the translation table is active
1642 *
1643 * parameters:
1644 *
1645 * tt: virtual address of L1 translation table to modify
1646 * start: beginning of VA range
1647 * end: end of VA range
1648 * static_map: whether to allocate the new translation table page from read only memory
1649 * table_attrs: attributes of new table entry in addition to VALID and TYPE_TABLE attributes
1650 *
1651 */
1652
1653 static void
1654 init_ptpages(tt_entry_t *tt, vm_map_address_t start, vm_map_address_t end, bool static_map, uint64_t table_attrs)
1655 {
1656 tt_entry_t *l1_tte;
1657 vm_offset_t ptpage_vaddr;
1658
1659 l1_tte = tt + ((start & ARM_TT_L1_INDEX_MASK) >> ARM_TT_L1_SHIFT);
1660
1661 while (start < end) {
1662 if (*l1_tte == ARM_TTE_EMPTY) {
1663 /* Allocate a page and setup L1 Table TTE in L1 */
1664 ptpage_vaddr = alloc_ptpage(static_map);
1665 *l1_tte = (kvtophys(ptpage_vaddr) & ARM_TTE_TABLE_MASK) | ARM_TTE_TYPE_TABLE | ARM_TTE_VALID | table_attrs;
1666 bzero((void *)ptpage_vaddr, ARM_PGBYTES);
1667 }
1668
1669 if ((start + ARM_TT_L1_SIZE) < start) {
1670 /* If this is the last L1 entry, it must cover the last mapping. */
1671 break;
1672 }
1673
1674 start += ARM_TT_L1_SIZE;
1675 l1_tte++;
1676 }
1677 }
1678
1679 #define ARM64_PHYSMAP_SLIDE_RANGE (1ULL << 30) // 1 GB
1680 #define ARM64_PHYSMAP_SLIDE_MASK (ARM64_PHYSMAP_SLIDE_RANGE - 1)
1681
1682 void
1683 arm_vm_init(uint64_t memory_size, boot_args * args)
1684 {
1685 vm_map_address_t va_l1, va_l1_end;
1686 tt_entry_t *cpu_l1_tte;
1687 vm_map_address_t va_l2, va_l2_end;
1688 tt_entry_t *cpu_l2_tte;
1689 pmap_paddr_t boot_ttep;
1690 tt_entry_t *boot_tte;
1691 uint64_t mem_segments;
1692 vm_offset_t ptpage_vaddr;
1693 vm_map_address_t dynamic_memory_begin;
1694
1695 /*
1696 * Get the virtual and physical kernel-managed memory base from boot_args.
1697 */
1698 gVirtBase = args->virtBase;
1699 gPhysBase = args->physBase;
1700 #if KASAN
1701 real_phys_size = args->memSize + (shadow_ptop - shadow_pbase);
1702 #else
1703 real_phys_size = args->memSize;
1704 #endif
1705 /*
1706 * Ensure the physical region we specify for the VM to manage ends on a
1707 * software page boundary. Note that the software page size (PAGE_SIZE)
1708 * may be a multiple of the hardware page size specified in ARM_PGBYTES.
1709 * We must round the reported memory size down to the nearest PAGE_SIZE
1710 * boundary to ensure the VM does not try to manage a page it does not
1711 * completely own. The KASAN shadow region, if present, is managed entirely
1712 * in units of the hardware page size and should not need similar treatment.
1713 */
1714 gPhysSize = mem_size = ((gPhysBase + args->memSize) & ~PAGE_MASK) - gPhysBase;
1715
1716 mem_actual = args->memSizeActual ? args->memSizeActual : mem_size;
1717
1718 if ((memory_size != 0) && (mem_size > memory_size)) {
1719 mem_size = memory_size;
1720 max_mem_actual = memory_size;
1721 } else {
1722 max_mem_actual = mem_actual;
1723 }
1724 if (mem_size >= ((VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS) / 2)) {
1725 panic("Unsupported memory configuration %lx\n", mem_size);
1726 }
1727
1728 #if defined(ARM_LARGE_MEMORY)
1729 unsigned long physmap_l1_entries = ((real_phys_size + ARM64_PHYSMAP_SLIDE_RANGE) >> ARM_TT_L1_SHIFT) + 1;
1730 physmap_base = VM_MIN_KERNEL_ADDRESS - (physmap_l1_entries << ARM_TT_L1_SHIFT);
1731 #else
1732 physmap_base = phystokv(args->topOfKernelData);
1733 #endif
1734
1735 // Slide the physical aperture to a random page-aligned location within the slide range
1736 uint64_t physmap_slide = early_random() & ARM64_PHYSMAP_SLIDE_MASK & ~((uint64_t)PAGE_MASK);
1737 assert(physmap_slide < ARM64_PHYSMAP_SLIDE_RANGE);
1738
1739 physmap_base += physmap_slide;
1740
1741 #if XNU_MONITOR
1742 physmap_base = ROUND_TWIG(physmap_base);
1743 #if defined(ARM_LARGE_MEMORY)
1744 static_memory_end = phystokv(args->topOfKernelData);
1745 #else
1746 static_memory_end = physmap_base + mem_size;
1747 #endif // ARM_LARGE_MEMORY
1748 physmap_end = physmap_base + real_phys_size;
1749 #else
1750 static_memory_end = physmap_base + mem_size + (PTOV_TABLE_SIZE * ARM_TT_TWIG_SIZE); // worst possible case for block alignment
1751 physmap_end = physmap_base + real_phys_size + (PTOV_TABLE_SIZE * ARM_TT_TWIG_SIZE);
1752 #endif
1753
1754 #if KASAN && !defined(ARM_LARGE_MEMORY)
1755 /* add the KASAN stolen memory to the physmap */
1756 dynamic_memory_begin = static_memory_end + (shadow_ptop - shadow_pbase);
1757 #else
1758 dynamic_memory_begin = static_memory_end;
1759 #endif
1760 #if XNU_MONITOR
1761 pmap_stacks_start = (void*)dynamic_memory_begin;
1762 dynamic_memory_begin += PPL_STACK_REGION_SIZE;
1763 pmap_stacks_end = (void*)dynamic_memory_begin;
1764 #endif
1765 if (dynamic_memory_begin > VM_MAX_KERNEL_ADDRESS) {
1766 panic("Unsupported memory configuration %lx\n", mem_size);
1767 }
1768
1769 boot_tte = (tt_entry_t *)&bootstrap_pagetables;
1770 boot_ttep = kvtophys((vm_offset_t)boot_tte);
1771
1772 #if DEVELOPMENT || DEBUG
1773 /* Sanity check - assert that BOOTSTRAP_TABLE_SIZE is sufficiently-large to
1774 * hold our bootstrap mappings for any possible slide */
1775 size_t bytes_mapped = dynamic_memory_begin - gVirtBase;
1776 size_t l1_entries = 1 + ((bytes_mapped + ARM_TT_L1_SIZE - 1) / ARM_TT_L1_SIZE);
1777 /* 1 L1 each for V=P and KVA, plus 1 page for each L2 */
1778 size_t pages_used = 2 * (l1_entries + 1);
1779 if (pages_used > BOOTSTRAP_TABLE_SIZE) {
1780 panic("BOOTSTRAP_TABLE_SIZE too small for memory config\n");
1781 }
1782 #endif
1783
1784 /*
1785 * TTBR0 L1, TTBR0 L2 - 1:1 bootstrap mapping.
1786 * TTBR1 L1, TTBR1 L2 - kernel mapping
1787 */
1788
1789 /*
1790 * TODO: free bootstrap table memory back to allocator.
1791 * on large memory systems bootstrap tables could be quite large.
1792 * after bootstrap complete, xnu can warm start with a single 16KB page mapping
1793 * to trampoline to KVA. this requires only 3 pages to stay resident.
1794 */
1795 avail_start = args->topOfKernelData;
1796
1797 #if defined(KERNEL_INTEGRITY_KTRR) || defined(KERNEL_INTEGRITY_CTRR)
1798 arm_replace_identity_map();
1799 #endif
1800
1801 /* Initialize invalid tte page */
1802 invalid_tte = (tt_entry_t *)alloc_ptpage(TRUE);
1803 invalid_ttep = kvtophys((vm_offset_t)invalid_tte);
1804 bzero(invalid_tte, ARM_PGBYTES);
1805
1806 /*
1807 * Initialize l1 page table page
1808 */
1809 cpu_tte = (tt_entry_t *)alloc_ptpage(TRUE);
1810 cpu_ttep = kvtophys((vm_offset_t)cpu_tte);
1811 bzero(cpu_tte, ARM_PGBYTES);
1812 avail_end = gPhysBase + mem_size;
1813 assert(!(avail_end & PAGE_MASK));
1814
1815 #if KASAN
1816 real_avail_end = gPhysBase + real_phys_size;
1817 #else
1818 real_avail_end = avail_end;
1819 #endif
1820
1821 /*
1822 * Initialize l1 and l2 page table pages :
1823 * map physical memory at the kernel base virtual address
1824 * cover the kernel dynamic address range section
1825 *
1826 * the so called physical aperture should be statically mapped
1827 */
1828 init_ptpages(cpu_tte, gVirtBase, dynamic_memory_begin, TRUE, 0);
1829
1830 #if defined(ARM_LARGE_MEMORY)
1831 /*
1832 * Initialize l1 page table pages :
1833 * on large memory systems the physical aperture exists separately below
1834 * the rest of the kernel virtual address space
1835 */
1836 init_ptpages(cpu_tte, physmap_base, ROUND_L1(physmap_end), TRUE, ARM_DYNAMIC_TABLE_XN);
1837 #endif
1838
1839
1840 #if __ARM_KERNEL_PROTECT__
1841 /* Expand the page tables to prepare for the EL0 mappings. */
1842 arm_vm_expand_kernel_el0_mappings();
1843 #endif /* __ARM_KERNEL_PROTECT__ */
1844
1845 /*
1846 * Now retrieve addresses for various segments from kernel mach-o header
1847 */
1848 segPRELINKTEXTB = (vm_offset_t) getsegdatafromheader(&_mh_execute_header, "__PRELINK_TEXT", &segSizePRELINKTEXT);
1849 segPLKDATACONSTB = (vm_offset_t) getsegdatafromheader(&_mh_execute_header, "__PLK_DATA_CONST", &segSizePLKDATACONST);
1850 segPLKTEXTEXECB = (vm_offset_t) getsegdatafromheader(&_mh_execute_header, "__PLK_TEXT_EXEC", &segSizePLKTEXTEXEC);
1851 segTEXTB = (vm_offset_t) getsegdatafromheader(&_mh_execute_header, "__TEXT", &segSizeTEXT);
1852 segDATACONSTB = (vm_offset_t) getsegdatafromheader(&_mh_execute_header, "__DATA_CONST", &segSizeDATACONST);
1853 segTEXTEXECB = (vm_offset_t) getsegdatafromheader(&_mh_execute_header, "__TEXT_EXEC", &segSizeTEXTEXEC);
1854 #if XNU_MONITOR
1855 segPPLTEXTB = (vm_offset_t) getsegdatafromheader(&_mh_execute_header, "__PPLTEXT", &segSizePPLTEXT);
1856 segPPLTRAMPB = (vm_offset_t) getsegdatafromheader(&_mh_execute_header, "__PPLTRAMP", &segSizePPLTRAMP);
1857 segPPLDATACONSTB = (vm_offset_t) getsegdatafromheader(&_mh_execute_header, "__PPLDATA_CONST", &segSizePPLDATACONST);
1858 #endif
1859 segDATAB = (vm_offset_t) getsegdatafromheader(&_mh_execute_header, "__DATA", &segSizeDATA);
1860 #if XNU_MONITOR
1861 segPPLDATAB = (vm_offset_t) getsegdatafromheader(&_mh_execute_header, "__PPLDATA", &segSizePPLDATA);
1862 #endif
1863
1864 segBOOTDATAB = (vm_offset_t) getsegdatafromheader(&_mh_execute_header, "__BOOTDATA", &segSizeBOOTDATA);
1865 segLINKB = (vm_offset_t) getsegdatafromheader(&_mh_execute_header, "__LINKEDIT", &segSizeLINK);
1866 segKLDB = (vm_offset_t) getsegdatafromheader(&_mh_execute_header, "__KLD", &segSizeKLD);
1867 segPRELINKDATAB = (vm_offset_t) getsegdatafromheader(&_mh_execute_header, "__PRELINK_DATA", &segSizePRELINKDATA);
1868 segPRELINKINFOB = (vm_offset_t) getsegdatafromheader(&_mh_execute_header, "__PRELINK_INFO", &segSizePRELINKINFO);
1869 segPLKLLVMCOVB = (vm_offset_t) getsegdatafromheader(&_mh_execute_header, "__PLK_LLVM_COV", &segSizePLKLLVMCOV);
1870 segPLKLINKEDITB = (vm_offset_t) getsegdatafromheader(&_mh_execute_header, "__PLK_LINKEDIT", &segSizePLKLINKEDIT);
1871 segLASTB = (vm_offset_t) getsegdatafromheader(&_mh_execute_header, "__LAST", &segSizeLAST);
1872 segLASTDATACONSTB = (vm_offset_t) getsegdatafromheader(&_mh_execute_header, "__LASTDATA_CONST", &segSizeLASTDATACONST);
1873
1874 sectHIBTEXTB = (vm_offset_t) getsectdatafromheader(&_mh_execute_header, "__TEXT_EXEC", "__hib_text", &sectSizeHIBTEXT);
1875 sectHIBDATACONSTB = (vm_offset_t) getsectdatafromheader(&_mh_execute_header, "__DATA_CONST", "__hib_const", &sectSizeHIBDATACONST);
1876 segHIBDATAB = (vm_offset_t) getsegdatafromheader(&_mh_execute_header, "__HIBDATA", &segSizeHIBDATA);
1877
1878 if (kernel_mach_header_is_in_fileset(&_mh_execute_header)) {
1879 kernel_mach_header_t *kc_mh = PE_get_kc_header(KCKindPrimary);
1880
1881 // fileset has kext PLK_TEXT_EXEC under kernel collection TEXT_EXEC following kernel's LAST
1882 segKCTEXTEXECB = (vm_offset_t) getsegdatafromheader(kc_mh, "__TEXT_EXEC", &segSizeKCTEXTEXEC);
1883 assert(segPLKTEXTEXECB && !segSizePLKTEXTEXEC); // kernel PLK_TEXT_EXEC must be empty
1884 assert(segLASTB && segSizeLAST); // kernel LAST must not be empty
1885 assert(segKCTEXTEXECB <= segLASTB); // KC TEXT_EXEC must contain kernel LAST
1886 assert(segKCTEXTEXECB + segSizeKCTEXTEXEC >= segLASTB + segSizeLAST);
1887 segPLKTEXTEXECB = segLASTB + segSizeLAST;
1888 segSizePLKTEXTEXEC = segSizeKCTEXTEXEC - (segPLKTEXTEXECB - segKCTEXTEXECB);
1889
1890 // fileset has kext PLK_DATA_CONST under kernel collection DATA_CONST following kernel's LASTDATA_CONST
1891 segKCDATACONSTB = (vm_offset_t) getsegdatafromheader(kc_mh, "__DATA_CONST", &segSizeKCDATACONST);
1892 assert(segPLKDATACONSTB && !segSizePLKDATACONST); // kernel PLK_DATA_CONST must be empty
1893 assert(segLASTDATACONSTB && segSizeLASTDATACONST); // kernel LASTDATA_CONST must be non-empty
1894 assert(segKCDATACONSTB <= segLASTDATACONSTB); // KC DATA_CONST must contain kernel LASTDATA_CONST
1895 assert(segKCDATACONSTB + segSizeKCDATACONST >= segLASTDATACONSTB + segSizeLASTDATACONST);
1896 segPLKDATACONSTB = segLASTDATACONSTB + segSizeLASTDATACONST;
1897 segSizePLKDATACONST = segSizeKCDATACONST - (segPLKDATACONSTB - segKCDATACONSTB);
1898
1899 // fileset has kext PRELINK_DATA under kernel collection DATA following kernel's empty PRELINK_DATA
1900 segKCDATAB = (vm_offset_t) getsegdatafromheader(kc_mh, "__DATA", &segSizeKCDATA);
1901 assert(segPRELINKDATAB && !segSizePRELINKDATA); // kernel PRELINK_DATA must be empty
1902 assert(segKCDATAB <= segPRELINKDATAB); // KC DATA must contain kernel PRELINK_DATA
1903 assert(segKCDATAB + segSizeKCDATA >= segPRELINKDATAB + segSizePRELINKDATA);
1904 segSizePRELINKDATA = segSizeKCDATA - (segPRELINKDATAB - segKCDATAB);
1905
1906 // fileset has consolidated PRELINK_TEXT, PRELINK_INFO and LINKEDIT at the kernel collection level
1907 assert(segPRELINKTEXTB && !segSizePRELINKTEXT); // kernel PRELINK_TEXT must be empty
1908 segPRELINKTEXTB = (vm_offset_t) getsegdatafromheader(kc_mh, "__PRELINK_TEXT", &segSizePRELINKTEXT);
1909 assert(segPRELINKINFOB && !segSizePRELINKINFO); // kernel PRELINK_INFO must be empty
1910 segPRELINKINFOB = (vm_offset_t) getsegdatafromheader(kc_mh, "__PRELINK_INFO", &segSizePRELINKINFO);
1911 segLINKB = (vm_offset_t) getsegdatafromheader(kc_mh, "__LINKEDIT", &segSizeLINK);
1912 }
1913
1914 (void) PE_parse_boot_argn("use_contiguous_hint", &use_contiguous_hint, sizeof(use_contiguous_hint));
1915 assert(segSizePRELINKTEXT < 0x03000000); /* 23355738 */
1916
1917 /* if one of the new segments is present, the other one better be as well */
1918 if (segSizePLKDATACONST || segSizePLKTEXTEXEC) {
1919 assert(segSizePLKDATACONST && segSizePLKTEXTEXEC);
1920 }
1921
1922 etext = (vm_offset_t) segTEXTB + segSizeTEXT;
1923 sdata = (vm_offset_t) segDATAB;
1924 edata = (vm_offset_t) segDATAB + segSizeDATA;
1925 end_kern = round_page(segHIGHESTKC ? segHIGHESTKC : getlastaddr()); /* Force end to next page */
1926
1927 vm_set_page_size();
1928
1929 vm_kernel_base = segTEXTB;
1930 vm_kernel_top = (vm_offset_t) &last_kernel_symbol;
1931 vm_kext_base = segPRELINKTEXTB;
1932 vm_kext_top = vm_kext_base + segSizePRELINKTEXT;
1933
1934 vm_prelink_stext = segPRELINKTEXTB;
1935 if (!segSizePLKTEXTEXEC && !segSizePLKDATACONST) {
1936 vm_prelink_etext = segPRELINKTEXTB + segSizePRELINKTEXT;
1937 } else {
1938 vm_prelink_etext = segPRELINKTEXTB + segSizePRELINKTEXT + segSizePLKDATACONST + segSizePLKTEXTEXEC;
1939 }
1940 vm_prelink_sinfo = segPRELINKINFOB;
1941 vm_prelink_einfo = segPRELINKINFOB + segSizePRELINKINFO;
1942 vm_slinkedit = segLINKB;
1943 vm_elinkedit = segLINKB + segSizeLINK;
1944
1945 vm_prelink_sdata = segPRELINKDATAB;
1946 vm_prelink_edata = segPRELINKDATAB + segSizePRELINKDATA;
1947
1948 arm_vm_prot_init(args);
1949
1950 vm_page_kernelcache_count = (unsigned int) (atop_64(end_kern - segLOWEST));
1951
1952 /*
1953 * Initialize the page tables for the low globals:
1954 * cover this address range:
1955 * LOW_GLOBAL_BASE_ADDRESS + 2MB
1956 */
1957 va_l1 = va_l2 = LOW_GLOBAL_BASE_ADDRESS;
1958 cpu_l1_tte = cpu_tte + ((va_l1 & ARM_TT_L1_INDEX_MASK) >> ARM_TT_L1_SHIFT);
1959 cpu_l2_tte = ((tt_entry_t *) phystokv(((*cpu_l1_tte) & ARM_TTE_TABLE_MASK))) + ((va_l2 & ARM_TT_L2_INDEX_MASK) >> ARM_TT_L2_SHIFT);
1960 ptpage_vaddr = alloc_ptpage(TRUE);
1961 *cpu_l2_tte = (kvtophys(ptpage_vaddr) & ARM_TTE_TABLE_MASK) | ARM_TTE_TYPE_TABLE | ARM_TTE_VALID | ARM_TTE_TABLE_PXN | ARM_TTE_TABLE_XN;
1962 bzero((void *)ptpage_vaddr, ARM_PGBYTES);
1963
1964 /*
1965 * Initialize l2 page table pages :
1966 * cover this address range:
1967 * KERNEL_DYNAMIC_ADDR - VM_MAX_KERNEL_ADDRESS
1968 */
1969 #if defined(ARM_LARGE_MEMORY)
1970 /*
1971 * dynamic mapped memory outside the VM allocator VA range required to bootstrap VM system
1972 * don't expect to exceed 64GB, no sense mapping any more space between here and the VM heap range
1973 */
1974 init_ptpages(cpu_tte, dynamic_memory_begin, ROUND_L1(dynamic_memory_begin), FALSE, ARM_DYNAMIC_TABLE_XN);
1975 #else
1976 /*
1977 * TODO: do these pages really need to come from RO memory?
1978 * With legacy 3 level table systems we never mapped more than a single L1 entry so this may be dead code
1979 */
1980 init_ptpages(cpu_tte, dynamic_memory_begin, VM_MAX_KERNEL_ADDRESS, TRUE, ARM_DYNAMIC_TABLE_XN);
1981 #endif
1982
1983 #if KASAN
1984 /* record the extent of the physmap */
1985 physmap_vbase = physmap_base;
1986 physmap_vtop = physmap_end;
1987 kasan_init();
1988 #endif /* KASAN */
1989
1990 #if MONOTONIC
1991 mt_early_init();
1992 #endif /* MONOTONIC */
1993
1994 set_tbi();
1995
1996 arm_vm_physmap_init(args);
1997 set_mmu_ttb_alternate(cpu_ttep & TTBR_BADDR_MASK);
1998
1999 ml_enable_monitor();
2000
2001 set_mmu_ttb(invalid_ttep & TTBR_BADDR_MASK);
2002
2003 flush_mmu_tlb();
2004 #if defined(HAS_VMSA_LOCK)
2005 vmsa_lock();
2006 #endif
2007 kva_active = TRUE;
2008 // global table pointers may need to be different due to physical aperture remapping
2009 cpu_tte = (tt_entry_t*)(phystokv(cpu_ttep));
2010 invalid_tte = (tt_entry_t*)(phystokv(invalid_ttep));
2011
2012 // From here on out, we're off the bootstrap translation tables.
2013
2014
2015 /* AuxKC initialization has to be deferred until this point, since
2016 * the AuxKC may not have been fully mapped in the bootstrap
2017 * tables, if it spilled downwards into the prior L2 block.
2018 *
2019 * Now that its mapping set up by arm_vm_prot_init() is active,
2020 * we can traverse and fix it up.
2021 */
2022
2023 if (arm_vm_auxkc_init()) {
2024 if (segLOWESTROAuxKC < segLOWESTRO) {
2025 segLOWESTRO = segLOWESTROAuxKC;
2026 }
2027 if (segHIGHESTROAuxKC > segHIGHESTRO) {
2028 segHIGHESTRO = segHIGHESTROAuxKC;
2029 }
2030 if (segLOWESTRXAuxKC < segLOWESTTEXT) {
2031 segLOWESTTEXT = segLOWESTRXAuxKC;
2032 }
2033 assert(segLOWEST == segLOWESTAuxKC);
2034
2035 // The preliminary auxKC mapping has been broken up.
2036 flush_mmu_tlb();
2037 }
2038
2039 sane_size = mem_size - (avail_start - gPhysBase);
2040 max_mem = mem_size;
2041 vm_kernel_slid_base = segLOWESTTEXT;
2042 vm_kernel_slid_top = vm_prelink_einfo;
2043 // vm_kernel_slide is set by arm_init()->arm_slide_rebase_and_sign_image()
2044 vm_kernel_stext = segTEXTB;
2045
2046 if (kernel_mach_header_is_in_fileset(&_mh_execute_header)) {
2047 // fileset has kext TEXT before kernel DATA_CONST
2048 assert(segTEXTEXECB == segTEXTB + segSizeTEXT);
2049 vm_kernel_etext = segTEXTB + segSizeTEXT + segSizeTEXTEXEC;
2050 } else {
2051 assert(segDATACONSTB == segTEXTB + segSizeTEXT);
2052 assert(segTEXTEXECB == segDATACONSTB + segSizeDATACONST);
2053 vm_kernel_etext = segTEXTB + segSizeTEXT + segSizeDATACONST + segSizeTEXTEXEC;
2054 }
2055
2056 dynamic_memory_begin = ROUND_TWIG(dynamic_memory_begin);
2057 #if defined(KERNEL_INTEGRITY_CTRR) && defined(CONFIG_XNUPOST)
2058 // reserve a 32MB region without permission overrides to use later for a CTRR unit test
2059 {
2060 extern vm_offset_t ctrr_test_page;
2061 tt_entry_t *new_tte;
2062
2063 ctrr_test_page = dynamic_memory_begin;
2064 dynamic_memory_begin += ARM_TT_L2_SIZE;
2065 cpu_l1_tte = cpu_tte + ((ctrr_test_page & ARM_TT_L1_INDEX_MASK) >> ARM_TT_L1_SHIFT);
2066 assert((*cpu_l1_tte) & ARM_TTE_VALID);
2067 cpu_l2_tte = ((tt_entry_t *) phystokv(((*cpu_l1_tte) & ARM_TTE_TABLE_MASK))) + ((ctrr_test_page & ARM_TT_L2_INDEX_MASK) >> ARM_TT_L2_SHIFT);
2068 assert((*cpu_l2_tte) == ARM_TTE_EMPTY);
2069 new_tte = (tt_entry_t *)alloc_ptpage(FALSE);
2070 bzero(new_tte, ARM_PGBYTES);
2071 *cpu_l2_tte = (kvtophys((vm_offset_t)new_tte) & ARM_TTE_TABLE_MASK) | ARM_TTE_TYPE_TABLE | ARM_TTE_VALID;
2072 }
2073 #endif /* defined(KERNEL_INTEGRITY_CTRR) && defined(CONFIG_XNUPOST) */
2074 #if XNU_MONITOR
2075 for (vm_offset_t cur = (vm_offset_t)pmap_stacks_start; cur < (vm_offset_t)pmap_stacks_end; cur += ARM_PGBYTES) {
2076 arm_vm_map(cpu_tte, cur, ARM_PTE_EMPTY);
2077 }
2078 #endif
2079 pmap_bootstrap(dynamic_memory_begin);
2080
2081 disable_preemption();
2082
2083 /*
2084 * Initialize l3 page table pages :
2085 * cover this address range:
2086 * 2MB + FrameBuffer size + 10MB for each 256MB segment
2087 */
2088
2089 mem_segments = (mem_size + 0x0FFFFFFF) >> 28;
2090
2091 va_l1 = dynamic_memory_begin;
2092 va_l1_end = va_l1 + ((2 + (mem_segments * 10)) << 20);
2093 va_l1_end += round_page(args->Video.v_height * args->Video.v_rowBytes);
2094 va_l1_end = (va_l1_end + 0x00000000007FFFFFULL) & 0xFFFFFFFFFF800000ULL;
2095
2096 cpu_l1_tte = cpu_tte + ((va_l1 & ARM_TT_L1_INDEX_MASK) >> ARM_TT_L1_SHIFT);
2097
2098 while (va_l1 < va_l1_end) {
2099 va_l2 = va_l1;
2100
2101 if (((va_l1 & ~ARM_TT_L1_OFFMASK) + ARM_TT_L1_SIZE) < va_l1) {
2102 /* If this is the last L1 entry, it must cover the last mapping. */
2103 va_l2_end = va_l1_end;
2104 } else {
2105 va_l2_end = MIN((va_l1 & ~ARM_TT_L1_OFFMASK) + ARM_TT_L1_SIZE, va_l1_end);
2106 }
2107
2108 cpu_l2_tte = ((tt_entry_t *) phystokv(((*cpu_l1_tte) & ARM_TTE_TABLE_MASK))) + ((va_l2 & ARM_TT_L2_INDEX_MASK) >> ARM_TT_L2_SHIFT);
2109
2110 while (va_l2 < va_l2_end) {
2111 pt_entry_t * ptp;
2112 pmap_paddr_t ptp_phys;
2113
2114 /* Allocate a page and setup L3 Table TTE in L2 */
2115 ptp = (pt_entry_t *) alloc_ptpage(FALSE);
2116 ptp_phys = (pmap_paddr_t)kvtophys((vm_offset_t)ptp);
2117
2118 bzero(ptp, ARM_PGBYTES);
2119 pmap_init_pte_page(kernel_pmap, ptp, va_l2, 3, TRUE);
2120
2121 *cpu_l2_tte = (pa_to_tte(ptp_phys)) | ARM_TTE_TYPE_TABLE | ARM_TTE_VALID | ARM_DYNAMIC_TABLE_XN;
2122
2123 va_l2 += ARM_TT_L2_SIZE;
2124 cpu_l2_tte++;
2125 }
2126
2127 va_l1 = va_l2_end;
2128 cpu_l1_tte++;
2129 }
2130
2131 #if defined(KERNEL_INTEGRITY_KTRR) || defined(KERNEL_INTEGRITY_CTRR)
2132 /*
2133 * In this configuration, the bootstrap mappings (arm_vm_init) and
2134 * the heap mappings occupy separate L1 regions. Explicitly set up
2135 * the heap L1 allocations here.
2136 */
2137 #if defined(ARM_LARGE_MEMORY)
2138 init_ptpages(cpu_tte, KERNEL_PMAP_HEAP_RANGE_START & ~ARM_TT_L1_OFFMASK, VM_MAX_KERNEL_ADDRESS, FALSE, ARM_DYNAMIC_TABLE_XN);
2139 #else // defined(ARM_LARGE_MEMORY)
2140 va_l1 = VM_MIN_KERNEL_ADDRESS & ~ARM_TT_L1_OFFMASK;
2141 init_ptpages(cpu_tte, VM_MIN_KERNEL_ADDRESS & ~ARM_TT_L1_OFFMASK, VM_MAX_KERNEL_ADDRESS, FALSE, ARM_DYNAMIC_TABLE_XN);
2142 #endif // defined(ARM_LARGE_MEMORY)
2143 #endif // defined(KERNEL_INTEGRITY_KTRR) || defined(KERNEL_INTEGRITY_CTRR)
2144
2145 /*
2146 * Initialize l3 page table pages :
2147 * cover this address range:
2148 * ((VM_MAX_KERNEL_ADDRESS & CPUWINDOWS_BASE_MASK) - PE_EARLY_BOOT_VA) to VM_MAX_KERNEL_ADDRESS
2149 */
2150 va_l1 = (VM_MAX_KERNEL_ADDRESS & CPUWINDOWS_BASE_MASK) - PE_EARLY_BOOT_VA;
2151 va_l1_end = VM_MAX_KERNEL_ADDRESS;
2152
2153 cpu_l1_tte = cpu_tte + ((va_l1 & ARM_TT_L1_INDEX_MASK) >> ARM_TT_L1_SHIFT);
2154
2155 while (va_l1 < va_l1_end) {
2156 va_l2 = va_l1;
2157
2158 if (((va_l1 & ~ARM_TT_L1_OFFMASK) + ARM_TT_L1_SIZE) < va_l1) {
2159 /* If this is the last L1 entry, it must cover the last mapping. */
2160 va_l2_end = va_l1_end;
2161 } else {
2162 va_l2_end = MIN((va_l1 & ~ARM_TT_L1_OFFMASK) + ARM_TT_L1_SIZE, va_l1_end);
2163 }
2164
2165 cpu_l2_tte = ((tt_entry_t *) phystokv(((*cpu_l1_tte) & ARM_TTE_TABLE_MASK))) + ((va_l2 & ARM_TT_L2_INDEX_MASK) >> ARM_TT_L2_SHIFT);
2166
2167 while (va_l2 < va_l2_end) {
2168 pt_entry_t * ptp;
2169 pmap_paddr_t ptp_phys;
2170
2171 /* Allocate a page and setup L3 Table TTE in L2 */
2172 ptp = (pt_entry_t *) alloc_ptpage(FALSE);
2173 ptp_phys = (pmap_paddr_t)kvtophys((vm_offset_t)ptp);
2174
2175 bzero(ptp, ARM_PGBYTES);
2176 pmap_init_pte_page(kernel_pmap, ptp, va_l2, 3, TRUE);
2177
2178 *cpu_l2_tte = (pa_to_tte(ptp_phys)) | ARM_TTE_TYPE_TABLE | ARM_TTE_VALID | ARM_DYNAMIC_TABLE_XN;
2179
2180 va_l2 += ARM_TT_L2_SIZE;
2181 cpu_l2_tte++;
2182 }
2183
2184 va_l1 = va_l2_end;
2185 cpu_l1_tte++;
2186 }
2187
2188
2189 /*
2190 * Adjust avail_start so that the range that the VM owns
2191 * starts on a PAGE_SIZE aligned boundary.
2192 */
2193 avail_start = (avail_start + PAGE_MASK) & ~PAGE_MASK;
2194
2195 #if XNU_MONITOR
2196 pmap_static_allocations_done();
2197 #endif
2198 first_avail = avail_start;
2199 patch_low_glo_static_region(args->topOfKernelData, avail_start - args->topOfKernelData);
2200 enable_preemption();
2201 }