]>
Commit | Line | Data |
---|---|---|
5ba3f43e A |
1 | /* |
2 | * Copyright (c) 2007-2008 Apple Inc. All rights reserved. | |
3 | * Copyright (c) 2005-2006 Apple Computer, Inc. All rights reserved. | |
4 | * | |
5 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ | |
6 | * | |
7 | * This file contains Original Code and/or Modifications of Original Code | |
8 | * as defined in and that are subject to the Apple Public Source License | |
9 | * Version 2.0 (the 'License'). You may not use this file except in | |
10 | * compliance with the License. The rights granted to you under the License | |
11 | * may not be used to create, or enable the creation or redistribution of, | |
12 | * unlawful or unlicensed copies of an Apple operating system, or to | |
13 | * circumvent, violate, or enable the circumvention or violation of, any | |
14 | * terms of an Apple operating system software license agreement. | |
15 | * | |
16 | * Please obtain a copy of the License at | |
17 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
18 | * | |
19 | * The Original Code and all software distributed under the License are | |
20 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
21 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
22 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
23 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
24 | * Please see the License for the specific language governing rights and | |
25 | * limitations under the License. | |
26 | * | |
27 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ | |
28 | */ | |
29 | #include <mach_debug.h> | |
30 | #include <mach_kdp.h> | |
31 | #include <debug.h> | |
32 | ||
33 | #include <mach/vm_types.h> | |
34 | #include <mach/vm_param.h> | |
35 | #include <mach/thread_status.h> | |
36 | #include <kern/misc_protos.h> | |
37 | #include <kern/assert.h> | |
38 | #include <kern/cpu_number.h> | |
39 | #include <kern/thread.h> | |
40 | #include <vm/vm_map.h> | |
41 | #include <vm/vm_page.h> | |
42 | #include <vm/pmap.h> | |
43 | ||
44 | #include <arm/proc_reg.h> | |
45 | #include <arm/caches_internal.h> | |
d9a64523 | 46 | #include <arm/cpu_data_internal.h> |
5ba3f43e A |
47 | #include <arm/pmap.h> |
48 | #include <arm/misc_protos.h> | |
49 | #include <arm/lowglobals.h> | |
50 | ||
51 | #include <pexpert/arm/boot.h> | |
d9a64523 | 52 | #include <pexpert/device_tree.h> |
5ba3f43e A |
53 | |
54 | #include <libkern/kernel_mach_header.h> | |
55 | ||
56 | /* | |
57 | * Denotes the end of xnu. | |
58 | */ | |
59 | extern void *last_kernel_symbol; | |
60 | ||
61 | /* | |
62 | * KASLR parameters | |
63 | */ | |
64 | vm_offset_t vm_kernel_base; | |
65 | vm_offset_t vm_kernel_top; | |
66 | vm_offset_t vm_kernel_stext; | |
67 | vm_offset_t vm_kernel_etext; | |
68 | vm_offset_t vm_kernel_slide; | |
69 | vm_offset_t vm_kernel_slid_base; | |
70 | vm_offset_t vm_kernel_slid_top; | |
71 | vm_offset_t vm_kext_base; | |
72 | vm_offset_t vm_kext_top; | |
73 | vm_offset_t vm_prelink_stext; | |
74 | vm_offset_t vm_prelink_etext; | |
75 | vm_offset_t vm_prelink_sinfo; | |
76 | vm_offset_t vm_prelink_einfo; | |
77 | vm_offset_t vm_slinkedit; | |
78 | vm_offset_t vm_elinkedit; | |
79 | vm_offset_t vm_prelink_sdata; | |
80 | vm_offset_t vm_prelink_edata; | |
81 | ||
d9a64523 A |
82 | vm_offset_t vm_kernel_builtinkmod_text; |
83 | vm_offset_t vm_kernel_builtinkmod_text_end; | |
84 | ||
0a7de745 | 85 | unsigned long gVirtBase, gPhysBase, gPhysSize; /* Used by <mach/arm/vm_param.h> */ |
5ba3f43e A |
86 | |
87 | vm_offset_t mem_size; /* Size of actual physical memory present | |
88 | * minus any performance buffer and possibly | |
89 | * limited by mem_limit in bytes */ | |
90 | uint64_t mem_actual; /* The "One True" physical memory size | |
91 | * actually, it's the highest physical | |
92 | * address + 1 */ | |
93 | uint64_t max_mem; /* Size of physical memory (bytes), adjusted | |
94 | * by maxmem */ | |
95 | uint64_t sane_size; /* Memory size to use for defaults | |
96 | * calculations */ | |
97 | addr64_t vm_last_addr = VM_MAX_KERNEL_ADDRESS; /* Highest kernel | |
98 | * virtual address known | |
99 | * to the VM system */ | |
100 | ||
d9a64523 A |
101 | vm_offset_t segEXTRADATA; |
102 | unsigned long segSizeEXTRADATA; | |
103 | vm_offset_t segLOWESTTEXT; | |
5ba3f43e A |
104 | static vm_offset_t segTEXTB; |
105 | static unsigned long segSizeTEXT; | |
106 | static vm_offset_t segDATAB; | |
107 | static unsigned long segSizeDATA; | |
108 | static vm_offset_t segLINKB; | |
109 | static unsigned long segSizeLINK; | |
110 | static vm_offset_t segKLDB; | |
111 | static unsigned long segSizeKLD; | |
112 | static vm_offset_t segLASTB; | |
113 | static unsigned long segSizeLAST; | |
114 | static vm_offset_t sectCONSTB; | |
115 | static unsigned long sectSizeCONST; | |
d9a64523 A |
116 | vm_offset_t segBOOTDATAB; |
117 | unsigned long segSizeBOOTDATA; | |
118 | extern vm_offset_t intstack_low_guard; | |
119 | extern vm_offset_t intstack_high_guard; | |
120 | extern vm_offset_t fiqstack_high_guard; | |
5ba3f43e A |
121 | |
122 | vm_offset_t segPRELINKTEXTB; | |
123 | unsigned long segSizePRELINKTEXT; | |
124 | vm_offset_t segPRELINKINFOB; | |
125 | unsigned long segSizePRELINKINFO; | |
126 | ||
127 | static kernel_segment_command_t *segDATA; | |
128 | static boolean_t doconstro = TRUE; | |
129 | ||
130 | vm_offset_t end_kern, etext, sdata, edata; | |
131 | ||
132 | /* | |
133 | * Bootstrap the system enough to run with virtual memory. | |
134 | * Map the kernel's code and data, and allocate the system page table. | |
135 | * Page_size must already be set. | |
136 | * | |
137 | * Parameters: | |
138 | * first_avail: first available physical page - | |
139 | * after kernel page tables | |
140 | * avail_start: PA of first physical page | |
141 | * avail_end : PA of last physical page | |
142 | */ | |
143 | vm_offset_t first_avail; | |
144 | vm_offset_t static_memory_end; | |
145 | pmap_paddr_t avail_start, avail_end; | |
146 | ||
147 | #define MEM_SIZE_MAX 0x40000000 | |
148 | ||
149 | extern vm_offset_t ExceptionVectorsBase; /* the code we want to load there */ | |
150 | ||
151 | /* The translation tables have to be 16KB aligned */ | |
152 | #define round_x_table(x) \ | |
153 | (((pmap_paddr_t)(x) + (ARM_PGBYTES<<2) - 1) & ~((ARM_PGBYTES<<2) - 1)) | |
154 | ||
d9a64523 A |
155 | vm_map_address_t |
156 | phystokv(pmap_paddr_t pa) | |
157 | { | |
0a7de745 | 158 | return pa - gPhysBase + gVirtBase; |
d9a64523 | 159 | } |
5ba3f43e A |
160 | |
161 | static void | |
0a7de745 A |
162 | arm_vm_page_granular_helper(vm_offset_t start, vm_offset_t _end, vm_offset_t va, |
163 | int pte_prot_APX, int pte_prot_XN) | |
5ba3f43e A |
164 | { |
165 | if (va & ARM_TT_L1_PT_OFFMASK) { /* ragged edge hanging over a ARM_TT_L1_PT_SIZE boundary */ | |
166 | va &= (~ARM_TT_L1_PT_OFFMASK); | |
167 | tt_entry_t *tte = &cpu_tte[ttenum(va)]; | |
168 | tt_entry_t tmplate = *tte; | |
169 | pmap_paddr_t pa; | |
170 | pt_entry_t *ppte, ptmp; | |
171 | unsigned int i; | |
172 | ||
173 | pa = va - gVirtBase + gPhysBase; | |
174 | ||
0a7de745 | 175 | if (pa >= avail_end) { |
d9a64523 | 176 | return; |
0a7de745 | 177 | } |
d9a64523 A |
178 | |
179 | assert(_end >= va); | |
180 | ||
5ba3f43e A |
181 | if (ARM_TTE_TYPE_TABLE == (tmplate & ARM_TTE_TYPE_MASK)) { |
182 | /* pick up the existing page table. */ | |
183 | ppte = (pt_entry_t *)phystokv((tmplate & ARM_TTE_TABLE_MASK)); | |
184 | } else { | |
185 | /* TTE must be reincarnated COARSE. */ | |
186 | ppte = (pt_entry_t *)phystokv(avail_start); | |
cb323159 | 187 | pmap_paddr_t l2table = avail_start; |
5ba3f43e | 188 | avail_start += ARM_PGBYTES; |
d9a64523 | 189 | bzero(ppte, ARM_PGBYTES); |
5ba3f43e | 190 | |
0a7de745 | 191 | for (i = 0; i < 4; ++i) { |
cb323159 | 192 | tte[i] = pa_to_tte(l2table + (i * 0x400)) | ARM_TTE_TYPE_TABLE; |
0a7de745 | 193 | } |
5ba3f43e A |
194 | } |
195 | ||
d9a64523 | 196 | vm_offset_t len = _end - va; |
0a7de745 | 197 | if ((pa + len) > avail_end) { |
d9a64523 | 198 | _end -= (pa + len - avail_end); |
0a7de745 | 199 | } |
d9a64523 A |
200 | assert((start - gVirtBase + gPhysBase) >= gPhysBase); |
201 | ||
5ba3f43e A |
202 | /* Apply the desired protections to the specified page range */ |
203 | for (i = 0; i < (ARM_PGBYTES / sizeof(*ppte)); i++) { | |
204 | if (start <= va && va < _end) { | |
5ba3f43e A |
205 | ptmp = pa | ARM_PTE_AF | ARM_PTE_SH | ARM_PTE_TYPE; |
206 | ptmp = ptmp | ARM_PTE_ATTRINDX(CACHE_ATTRINDX_DEFAULT); | |
207 | ptmp = ptmp | ARM_PTE_AP(pte_prot_APX); | |
0a7de745 | 208 | if (pte_prot_XN) { |
5ba3f43e | 209 | ptmp = ptmp | ARM_PTE_NX; |
0a7de745 | 210 | } |
5ba3f43e A |
211 | |
212 | ppte[i] = ptmp; | |
213 | } | |
214 | ||
215 | va += ARM_PGBYTES; | |
216 | pa += ARM_PGBYTES; | |
217 | } | |
218 | } | |
219 | } | |
220 | ||
221 | static void | |
0a7de745 A |
222 | arm_vm_page_granular_prot(vm_offset_t start, unsigned long size, |
223 | int tte_prot_XN, int pte_prot_APX, int pte_prot_XN, int force_page_granule) | |
5ba3f43e A |
224 | { |
225 | vm_offset_t _end = start + size; | |
226 | vm_offset_t align_start = (start + ARM_TT_L1_PT_OFFMASK) & ~ARM_TT_L1_PT_OFFMASK; | |
227 | vm_offset_t align_end = _end & ~ARM_TT_L1_PT_OFFMASK; | |
228 | ||
229 | arm_vm_page_granular_helper(start, _end, start, pte_prot_APX, pte_prot_XN); | |
230 | ||
231 | while (align_start < align_end) { | |
d9a64523 | 232 | if (force_page_granule) { |
0a7de745 A |
233 | arm_vm_page_granular_helper(align_start, align_end, align_start + 1, |
234 | pte_prot_APX, pte_prot_XN); | |
5ba3f43e A |
235 | } else { |
236 | tt_entry_t *tte = &cpu_tte[ttenum(align_start)]; | |
237 | for (int i = 0; i < 4; ++i) { | |
238 | tt_entry_t tmplate = tte[i]; | |
239 | ||
240 | tmplate = (tmplate & ~ARM_TTE_BLOCK_APMASK) | ARM_TTE_BLOCK_AP(pte_prot_APX); | |
241 | tmplate = (tmplate & ~ARM_TTE_BLOCK_NX_MASK); | |
0a7de745 | 242 | if (tte_prot_XN) { |
5ba3f43e | 243 | tmplate = tmplate | ARM_TTE_BLOCK_NX; |
0a7de745 | 244 | } |
5ba3f43e A |
245 | |
246 | tte[i] = tmplate; | |
247 | } | |
248 | } | |
249 | align_start += ARM_TT_L1_PT_SIZE; | |
250 | } | |
251 | ||
252 | arm_vm_page_granular_helper(start, _end, _end, pte_prot_APX, pte_prot_XN); | |
253 | } | |
254 | ||
255 | static inline void | |
d9a64523 | 256 | arm_vm_page_granular_RNX(vm_offset_t start, unsigned long size, int force_page_granule) |
5ba3f43e | 257 | { |
d9a64523 | 258 | arm_vm_page_granular_prot(start, size, 1, AP_RONA, 1, force_page_granule); |
5ba3f43e A |
259 | } |
260 | ||
261 | static inline void | |
d9a64523 | 262 | arm_vm_page_granular_ROX(vm_offset_t start, unsigned long size, int force_page_granule) |
5ba3f43e | 263 | { |
d9a64523 | 264 | arm_vm_page_granular_prot(start, size, 0, AP_RONA, 0, force_page_granule); |
5ba3f43e A |
265 | } |
266 | ||
267 | static inline void | |
d9a64523 | 268 | arm_vm_page_granular_RWNX(vm_offset_t start, unsigned long size, int force_page_granule) |
5ba3f43e | 269 | { |
d9a64523 | 270 | arm_vm_page_granular_prot(start, size, 1, AP_RWNA, 1, force_page_granule); |
5ba3f43e A |
271 | } |
272 | ||
273 | static inline void | |
d9a64523 | 274 | arm_vm_page_granular_RWX(vm_offset_t start, unsigned long size, int force_page_granule) |
5ba3f43e | 275 | { |
d9a64523 | 276 | arm_vm_page_granular_prot(start, size, 0, AP_RWNA, 0, force_page_granule); |
5ba3f43e A |
277 | } |
278 | ||
279 | void | |
280 | arm_vm_prot_init(boot_args * args) | |
281 | { | |
282 | #if __ARM_PTE_PHYSMAP__ | |
283 | boolean_t force_coarse_physmap = TRUE; | |
284 | #else | |
285 | boolean_t force_coarse_physmap = FALSE; | |
286 | #endif | |
287 | /* | |
288 | * Enforce W^X protections on segments that have been identified so far. This will be | |
0a7de745 | 289 | * further refined for each KEXT's TEXT and DATA segments in readPrelinkedExtensions() |
5ba3f43e | 290 | */ |
0a7de745 | 291 | |
5ba3f43e A |
292 | /* |
293 | * Protection on kernel text is loose here to allow shenanigans early on (e.g. copying exception vectors) | |
294 | * and storing an address into "error_buffer" (see arm_init.c) !?! | |
295 | * These protections are tightened in arm_vm_prot_finalize() | |
296 | */ | |
297 | arm_vm_page_granular_RWX(gVirtBase, segSizeTEXT + (segTEXTB - gVirtBase), FALSE); | |
298 | ||
299 | if (doconstro) { | |
300 | /* | |
301 | * We map __DATA with 3 calls, so that the __const section can have its | |
302 | * protections changed independently of the rest of the __DATA segment. | |
303 | */ | |
304 | arm_vm_page_granular_RWNX(segDATAB, sectCONSTB - segDATAB, FALSE); | |
305 | arm_vm_page_granular_RNX(sectCONSTB, sectSizeCONST, FALSE); | |
306 | arm_vm_page_granular_RWNX(sectCONSTB + sectSizeCONST, (segDATAB + segSizeDATA) - (sectCONSTB + sectSizeCONST), FALSE); | |
307 | } else { | |
308 | /* If we aren't protecting const, just map DATA as a single blob. */ | |
309 | arm_vm_page_granular_RWNX(segDATAB, segSizeDATA, FALSE); | |
310 | } | |
d9a64523 A |
311 | arm_vm_page_granular_RWNX(segBOOTDATAB, segSizeBOOTDATA, TRUE); |
312 | arm_vm_page_granular_RNX((vm_offset_t)&intstack_low_guard, PAGE_MAX_SIZE, TRUE); | |
313 | arm_vm_page_granular_RNX((vm_offset_t)&intstack_high_guard, PAGE_MAX_SIZE, TRUE); | |
314 | arm_vm_page_granular_RNX((vm_offset_t)&fiqstack_high_guard, PAGE_MAX_SIZE, TRUE); | |
5ba3f43e A |
315 | |
316 | arm_vm_page_granular_ROX(segKLDB, segSizeKLD, force_coarse_physmap); | |
317 | arm_vm_page_granular_RWNX(segLINKB, segSizeLINK, force_coarse_physmap); | |
318 | arm_vm_page_granular_RWNX(segLASTB, segSizeLAST, FALSE); // __LAST may be empty, but we cannot assume this | |
319 | arm_vm_page_granular_RWNX(segPRELINKTEXTB, segSizePRELINKTEXT, TRUE); // Refined in OSKext::readPrelinkedExtensions | |
320 | arm_vm_page_granular_RWNX(segPRELINKTEXTB + segSizePRELINKTEXT, | |
0a7de745 | 321 | end_kern - (segPRELINKTEXTB + segSizePRELINKTEXT), force_coarse_physmap); // PreLinkInfoDictionary |
d9a64523 A |
322 | arm_vm_page_granular_RWNX(end_kern, phystokv(args->topOfKernelData) - end_kern, force_coarse_physmap); // Device Tree, RAM Disk (if present), bootArgs, trust caches |
323 | arm_vm_page_granular_RNX(segEXTRADATA, segSizeEXTRADATA, FALSE); // tighter trust cache protection | |
5ba3f43e A |
324 | arm_vm_page_granular_RWNX(phystokv(args->topOfKernelData), ARM_PGBYTES * 8, FALSE); // boot_tte, cpu_tte |
325 | ||
326 | /* | |
327 | * FIXME: Any page table pages that arm_vm_page_granular_* created with ROX entries in the range | |
328 | * phystokv(args->topOfKernelData) to phystokv(prot_avail_start) should themselves be | |
329 | * write protected in the static mapping of that range. | |
330 | * [Page table pages whose page table entries grant execute (X) privileges should themselves be | |
331 | * marked read-only. This aims to thwart attacks that replace the X entries with vectors to evil code | |
332 | * (relying on some thread of execution to eventually arrive at what previously was a trusted routine).] | |
333 | */ | |
334 | arm_vm_page_granular_RWNX(phystokv(args->topOfKernelData) + ARM_PGBYTES * 8, ARM_PGBYTES, FALSE); /* Excess physMem over 1MB */ | |
335 | arm_vm_page_granular_RWX(phystokv(args->topOfKernelData) + ARM_PGBYTES * 9, ARM_PGBYTES, FALSE); /* refined in finalize */ | |
336 | ||
337 | /* Map the remainder of xnu owned memory. */ | |
338 | arm_vm_page_granular_RWNX(phystokv(args->topOfKernelData) + ARM_PGBYTES * 10, | |
0a7de745 | 339 | static_memory_end - (phystokv(args->topOfKernelData) + ARM_PGBYTES * 10), force_coarse_physmap); /* rest of physmem */ |
5ba3f43e A |
340 | |
341 | /* | |
342 | * Special case write protection for the mapping of ExceptionVectorsBase (EVB) at 0xFFFF0000. | |
343 | * Recall that start.s handcrafted a page table page for EVB mapping | |
344 | */ | |
345 | pmap_paddr_t p = (pmap_paddr_t)(args->topOfKernelData) + (ARM_PGBYTES * 9); | |
346 | pt_entry_t *ppte = (pt_entry_t *)phystokv(p); | |
cb323159 | 347 | pmap_init_pte_page(kernel_pmap, ppte, HIGH_EXC_VECTORS & ~ARM_TT_L1_PT_OFFMASK, 2, TRUE, FALSE); |
5ba3f43e | 348 | |
cb323159 | 349 | int idx = (HIGH_EXC_VECTORS & ARM_TT_L1_PT_OFFMASK) >> ARM_TT_L2_SHIFT; |
5ba3f43e A |
350 | pt_entry_t ptmp = ppte[idx]; |
351 | ||
352 | ptmp = (ptmp & ~ARM_PTE_APMASK) | ARM_PTE_AP(AP_RONA); | |
353 | ||
354 | ppte[idx] = ptmp; | |
355 | } | |
356 | ||
357 | void | |
358 | arm_vm_prot_finalize(boot_args * args) | |
359 | { | |
d9a64523 A |
360 | cpu_stack_alloc(&BootCpuData); |
361 | ml_static_mfree(segBOOTDATAB, segSizeBOOTDATA); | |
5ba3f43e A |
362 | /* |
363 | * Naively we could have: | |
364 | * arm_vm_page_granular_ROX(segTEXTB, segSizeTEXT, FALSE); | |
365 | * but, at present, that would miss a 1Mb boundary at the beginning of the segment and | |
366 | * so would force a (wasteful) coarse page (e.g. when gVirtBase is 0x80000000, segTEXTB is 0x80001000). | |
367 | */ | |
368 | arm_vm_page_granular_ROX(gVirtBase, segSizeTEXT + (segTEXTB - gVirtBase), FALSE); | |
369 | ||
370 | arm_vm_page_granular_RWNX(phystokv(args->topOfKernelData) + ARM_PGBYTES * 9, ARM_PGBYTES, FALSE); /* commpage, EVB */ | |
371 | ||
5ba3f43e A |
372 | flush_mmu_tlb(); |
373 | } | |
374 | ||
d9a64523 A |
375 | /* used in the chosen/memory-map node, populated by iBoot. */ |
376 | typedef struct MemoryMapFileInfo { | |
0a7de745 A |
377 | vm_offset_t paddr; |
378 | size_t length; | |
d9a64523 A |
379 | } MemoryMapFileInfo; |
380 | ||
381 | ||
5ba3f43e A |
382 | void |
383 | arm_vm_init(uint64_t memory_size, boot_args * args) | |
384 | { | |
385 | vm_map_address_t va, off, off_end; | |
386 | tt_entry_t *tte, *tte_limit; | |
387 | pmap_paddr_t boot_ttep; | |
388 | tt_entry_t *boot_tte; | |
389 | uint32_t mem_segments; | |
390 | kernel_section_t *sectDCONST; | |
391 | ||
392 | /* | |
393 | * Get the virtual and physical memory base from boot_args. | |
394 | */ | |
395 | gVirtBase = args->virtBase; | |
396 | gPhysBase = args->physBase; | |
397 | gPhysSize = args->memSize; | |
398 | mem_size = args->memSize; | |
0a7de745 | 399 | if ((memory_size != 0) && (mem_size > memory_size)) { |
5ba3f43e | 400 | mem_size = memory_size; |
0a7de745 A |
401 | } |
402 | if (mem_size > MEM_SIZE_MAX) { | |
5ba3f43e | 403 | mem_size = MEM_SIZE_MAX; |
0a7de745 | 404 | } |
5ba3f43e A |
405 | static_memory_end = gVirtBase + mem_size; |
406 | ||
407 | /* Calculate the nubmer of ~256MB segments of memory */ | |
408 | mem_segments = (mem_size + 0x0FFFFFFF) >> 28; | |
409 | ||
410 | /* | |
411 | * Copy the boot mmu tt to create system mmu tt. | |
412 | * System mmu tt start after the boot mmu tt. | |
413 | * Determine translation table base virtual address: - aligned at end | |
414 | * of executable. | |
415 | */ | |
416 | boot_ttep = args->topOfKernelData; | |
417 | boot_tte = (tt_entry_t *) phystokv(boot_ttep); | |
418 | ||
419 | cpu_ttep = boot_ttep + ARM_PGBYTES * 4; | |
420 | cpu_tte = (tt_entry_t *) phystokv(cpu_ttep); | |
421 | ||
422 | bcopy(boot_tte, cpu_tte, ARM_PGBYTES * 4); | |
423 | ||
424 | /* | |
425 | * Clear out any V==P mappings that may have been established in e.g. start.s | |
426 | */ | |
427 | tte = &cpu_tte[ttenum(gPhysBase)]; | |
428 | tte_limit = &cpu_tte[ttenum(gPhysBase + gPhysSize)]; | |
429 | ||
430 | /* Hands off [gVirtBase, gVirtBase + gPhysSize) please. */ | |
431 | if (gPhysBase < gVirtBase) { | |
0a7de745 | 432 | if (gPhysBase + gPhysSize > gVirtBase) { |
5ba3f43e | 433 | tte_limit = &cpu_tte[ttenum(gVirtBase)]; |
0a7de745 | 434 | } |
5ba3f43e | 435 | } else { |
0a7de745 | 436 | if (gPhysBase < gVirtBase + gPhysSize) { |
5ba3f43e | 437 | tte = &cpu_tte[ttenum(gVirtBase + gPhysSize)]; |
0a7de745 | 438 | } |
5ba3f43e A |
439 | } |
440 | ||
441 | while (tte < tte_limit) { | |
0a7de745 | 442 | *tte = ARM_TTE_TYPE_FAULT; |
d9a64523 A |
443 | tte++; |
444 | } | |
0a7de745 | 445 | |
5ba3f43e A |
446 | /* Skip 6 pages (four L1 + two L2 entries) */ |
447 | avail_start = cpu_ttep + ARM_PGBYTES * 6; | |
448 | avail_end = gPhysBase + mem_size; | |
449 | ||
450 | /* | |
451 | * Now retrieve addresses for end, edata, and etext | |
452 | * from MACH-O headers for the currently running 32 bit kernel. | |
453 | */ | |
454 | segTEXTB = (vm_offset_t) getsegdatafromheader(&_mh_execute_header, "__TEXT", &segSizeTEXT); | |
d9a64523 | 455 | segLOWESTTEXT = segTEXTB; |
5ba3f43e A |
456 | segDATAB = (vm_offset_t) getsegdatafromheader(&_mh_execute_header, "__DATA", &segSizeDATA); |
457 | segLINKB = (vm_offset_t) getsegdatafromheader(&_mh_execute_header, "__LINKEDIT", &segSizeLINK); | |
458 | segKLDB = (vm_offset_t) getsegdatafromheader(&_mh_execute_header, "__KLD", &segSizeKLD); | |
459 | segLASTB = (vm_offset_t) getsegdatafromheader(&_mh_execute_header, "__LAST", &segSizeLAST); | |
460 | segPRELINKTEXTB = (vm_offset_t) getsegdatafromheader(&_mh_execute_header, "__PRELINK_TEXT", &segSizePRELINKTEXT); | |
461 | segPRELINKINFOB = (vm_offset_t) getsegdatafromheader(&_mh_execute_header, "__PRELINK_INFO", &segSizePRELINKINFO); | |
d9a64523 A |
462 | segBOOTDATAB = (vm_offset_t) getsegdatafromheader(&_mh_execute_header, "__BOOTDATA", &segSizeBOOTDATA); |
463 | ||
464 | segEXTRADATA = 0; | |
465 | segSizeEXTRADATA = 0; | |
466 | ||
467 | DTEntry memory_map; | |
468 | MemoryMapFileInfo *trustCacheRange; | |
469 | unsigned int trustCacheRangeSize; | |
470 | int err; | |
471 | ||
472 | err = DTLookupEntry(NULL, "chosen/memory-map", &memory_map); | |
473 | assert(err == kSuccess); | |
474 | ||
475 | err = DTGetProperty(memory_map, "TrustCache", (void**)&trustCacheRange, &trustCacheRangeSize); | |
476 | if (err == kSuccess) { | |
477 | assert(trustCacheRangeSize == sizeof(MemoryMapFileInfo)); | |
478 | ||
479 | segEXTRADATA = phystokv(trustCacheRange->paddr); | |
480 | segSizeEXTRADATA = trustCacheRange->length; | |
481 | } | |
5ba3f43e A |
482 | |
483 | etext = (vm_offset_t) segTEXTB + segSizeTEXT; | |
484 | sdata = (vm_offset_t) segDATAB; | |
485 | edata = (vm_offset_t) segDATAB + segSizeDATA; | |
486 | end_kern = round_page(getlastaddr()); /* Force end to next page */ | |
487 | ||
488 | /* | |
489 | * Special handling for the __DATA,__const *section*. | |
490 | * A page of padding named lastkerneldataconst is at the end of the __DATA,__const | |
491 | * so we can safely truncate the size. __DATA,__const is also aligned, but | |
0a7de745 | 492 | * just in case we will round that to a page, too. |
5ba3f43e A |
493 | */ |
494 | segDATA = getsegbynamefromheader(&_mh_execute_header, "__DATA"); | |
495 | sectDCONST = getsectbynamefromheader(&_mh_execute_header, "__DATA", "__const"); | |
496 | sectCONSTB = sectDCONST->addr; | |
497 | sectSizeCONST = sectDCONST->size; | |
498 | ||
5ba3f43e A |
499 | if (doconstro) { |
500 | extern vm_offset_t _lastkerneldataconst; | |
501 | extern vm_size_t _lastkerneldataconst_padsize; | |
502 | vm_offset_t sdataconst = sectCONSTB; | |
503 | ||
504 | /* this should already be aligned, but so that we can protect we round */ | |
505 | sectCONSTB = round_page(sectCONSTB); | |
506 | ||
507 | /* make sure lastkerneldataconst is really last and the right size */ | |
508 | if ((_lastkerneldataconst == sdataconst + sectSizeCONST - _lastkerneldataconst_padsize) && | |
509 | (_lastkerneldataconst_padsize >= PAGE_SIZE)) { | |
510 | sectSizeCONST = trunc_page(sectSizeCONST); | |
511 | } else { | |
512 | /* otherwise see if next section is aligned then protect up to it */ | |
513 | kernel_section_t *next_sect = nextsect(segDATA, sectDCONST); | |
514 | ||
515 | if (next_sect && ((next_sect->addr & PAGE_MASK) == 0)) { | |
516 | sectSizeCONST = next_sect->addr - sectCONSTB; | |
517 | } else { | |
518 | /* lastly just go ahead and truncate so we try to protect something */ | |
519 | sectSizeCONST = trunc_page(sectSizeCONST); | |
520 | } | |
521 | } | |
522 | ||
523 | /* sanity check */ | |
524 | if ((sectSizeCONST == 0) || (sectCONSTB < sdata) || (sectCONSTB + sectSizeCONST) >= edata) { | |
525 | doconstro = FALSE; | |
526 | } | |
527 | } | |
528 | ||
529 | vm_set_page_size(); | |
530 | ||
5ba3f43e A |
531 | vm_prelink_stext = segPRELINKTEXTB; |
532 | vm_prelink_etext = segPRELINKTEXTB + segSizePRELINKTEXT; | |
533 | vm_prelink_sinfo = segPRELINKINFOB; | |
534 | vm_prelink_einfo = segPRELINKINFOB + segSizePRELINKINFO; | |
535 | vm_slinkedit = segLINKB; | |
536 | vm_elinkedit = segLINKB + segSizeLINK; | |
537 | ||
538 | sane_size = mem_size - (avail_start - gPhysBase); | |
539 | max_mem = mem_size; | |
0a7de745 | 540 | vm_kernel_slide = gVirtBase - VM_KERNEL_LINK_ADDRESS; |
5ba3f43e A |
541 | vm_kernel_stext = segTEXTB; |
542 | vm_kernel_etext = segTEXTB + segSizeTEXT; | |
543 | vm_kernel_base = gVirtBase; | |
544 | vm_kernel_top = (vm_offset_t) &last_kernel_symbol; | |
545 | vm_kext_base = segPRELINKTEXTB; | |
546 | vm_kext_top = vm_kext_base + segSizePRELINKTEXT; | |
547 | vm_kernel_slid_base = segTEXTB; | |
548 | vm_kernel_slid_top = vm_kext_top; | |
549 | ||
0a7de745 | 550 | pmap_bootstrap((gVirtBase + MEM_SIZE_MAX + 0x3FFFFF) & 0xFFC00000); |
5ba3f43e A |
551 | |
552 | arm_vm_prot_init(args); | |
553 | ||
554 | /* | |
555 | * To avoid recursing while trying to init the vm_page and object * mechanisms, | |
556 | * pre-initialize kernel pmap page table pages to cover this address range: | |
557 | * 2MB + FrameBuffer size + 3MB for each 256MB segment | |
558 | */ | |
559 | off_end = (2 + (mem_segments * 3)) << 20; | |
560 | off_end += (unsigned int) round_page(args->Video.v_height * args->Video.v_rowBytes); | |
561 | ||
0a7de745 | 562 | for (off = 0, va = (gVirtBase + MEM_SIZE_MAX + 0x3FFFFF) & 0xFFC00000; off < off_end; off += ARM_TT_L1_PT_SIZE) { |
5ba3f43e A |
563 | pt_entry_t *ptp; |
564 | pmap_paddr_t ptp_phys; | |
565 | ||
566 | ptp = (pt_entry_t *) phystokv(avail_start); | |
567 | ptp_phys = (pmap_paddr_t)avail_start; | |
568 | avail_start += ARM_PGBYTES; | |
cb323159 | 569 | pmap_init_pte_page(kernel_pmap, ptp, va + off, 2, TRUE, TRUE); |
5ba3f43e | 570 | tte = &cpu_tte[ttenum(va + off)]; |
cb323159 A |
571 | *tte = pa_to_tte((ptp_phys)) | ARM_TTE_TYPE_TABLE; |
572 | *(tte + 1) = pa_to_tte((ptp_phys + 0x400)) | ARM_TTE_TYPE_TABLE; | |
573 | *(tte + 2) = pa_to_tte((ptp_phys + 0x800)) | ARM_TTE_TYPE_TABLE; | |
574 | *(tte + 3) = pa_to_tte((ptp_phys + 0xC00)) | ARM_TTE_TYPE_TABLE; | |
5ba3f43e A |
575 | } |
576 | ||
cb323159 A |
577 | set_mmu_ttb(cpu_ttep); |
578 | set_mmu_ttb_alternate(cpu_ttep); | |
579 | flush_mmu_tlb(); | |
580 | #if __arm__ && __ARM_USER_PROTECT__ | |
581 | { | |
582 | unsigned int ttbr0_val, ttbr1_val, ttbcr_val; | |
583 | thread_t thread = current_thread(); | |
584 | ||
585 | __asm__ volatile ("mrc p15,0,%0,c2,c0,0\n" : "=r"(ttbr0_val)); | |
586 | __asm__ volatile ("mrc p15,0,%0,c2,c0,1\n" : "=r"(ttbr1_val)); | |
587 | __asm__ volatile ("mrc p15,0,%0,c2,c0,2\n" : "=r"(ttbcr_val)); | |
588 | thread->machine.uptw_ttb = ttbr0_val; | |
589 | thread->machine.kptw_ttb = ttbr1_val; | |
590 | thread->machine.uptw_ttc = ttbcr_val; | |
591 | } | |
592 | #endif | |
5ba3f43e A |
593 | avail_start = (avail_start + PAGE_MASK) & ~PAGE_MASK; |
594 | ||
595 | first_avail = avail_start; | |
596 | patch_low_glo_static_region(args->topOfKernelData, avail_start - args->topOfKernelData); | |
597 | } |