2 * Copyright (c) 2007-2009 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. 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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
33 #include <mach_ldebug.h>
36 #include <kern/misc_protos.h>
37 #include <kern/thread.h>
38 #include <kern/timer_queue.h>
39 #include <kern/processor.h>
40 #include <kern/startup.h>
41 #include <kern/debug.h>
42 #include <prng/random.h>
43 #include <machine/machine_routines.h>
44 #include <machine/commpage.h>
45 /* ARM64_TODO unify boot.h */
47 #include <pexpert/arm64/boot.h>
49 #include <pexpert/arm/boot.h>
51 #error Unsupported arch
53 #include <pexpert/arm/consistent_debug.h>
54 #include <pexpert/device_tree.h>
55 #include <arm/proc_reg.h>
57 #include <arm/caches_internal.h>
58 #include <arm/cpu_internal.h>
59 #include <arm/cpu_data_internal.h>
60 #include <arm/misc_protos.h>
61 #include <arm/machine_cpu.h>
62 #include <arm/rtclock.h>
63 #include <vm/vm_map.h>
65 #include <libkern/kernel_mach_header.h>
66 #include <libkern/stack_protector.h>
67 #include <libkern/section_keywords.h>
68 #include <san/kasan.h>
70 #include <pexpert/pexpert.h>
72 #include <console/serial_protos.h>
75 #include <kern/telemetry.h>
78 #include <kern/monotonic.h>
79 #endif /* MONOTONIC */
81 extern void patch_low_glo(void);
82 extern int serial_init(void);
83 extern void sleep_token_buffer_init(void);
85 extern vm_offset_t intstack_top
;
87 extern vm_offset_t excepstack_top
;
89 extern vm_offset_t fiqstack_top
;
92 extern const char version
[];
93 extern const char version_variant
[];
94 extern int disableConsoleOutput
;
96 int pc_trace_buf
[PC_TRACE_BUF_SIZE
] = {0};
97 int pc_trace_cnt
= PC_TRACE_BUF_SIZE
;
100 boolean_t up_style_idle_exit
= 0;
104 #if INTERRUPT_MASKED_DEBUG
105 boolean_t interrupt_masked_debug
= 1;
106 uint64_t interrupt_masked_timeout
= 0xd0000;
109 boot_args const_boot_args
__attribute__((section("__DATA, __const")));
110 boot_args
*BootArgs
__attribute__((section("__DATA, __const")));
112 unsigned int arm_diag
;
114 static unsigned cpus_defeatures
= 0x0;
115 extern void cpu_defeatures_set(unsigned int);
118 #if __arm64__ && __ARM_GLOBAL_SLEEP_BIT__
119 extern volatile boolean_t arm64_stall_sleep
;
122 extern boolean_t force_immediate_debug_halt
;
127 void arm_init(boot_args
* args
);
130 unsigned int page_shift_user32
; /* for page_size as seen by a 32-bit task */
131 #endif /* __arm64__ */
139 // Note, the following should come from a header from dyld
141 rebase_chain(uintptr_t chainStartAddress
, uint64_t stepMultiplier
, uintptr_t baseAddress __unused
, uint64_t slide
)
144 uintptr_t address
= chainStartAddress
;
146 uint64_t value
= *(uint64_t*)address
;
148 bool isAuthenticated
= (value
& (1ULL << 63)) != 0;
149 bool isRebase
= (value
& (1ULL << 62)) == 0;
151 if (isAuthenticated
) {
152 // The new value for a rebase is the low 32-bits of the threaded value plus the slide.
153 uint64_t newValue
= (value
& 0xFFFFFFFF) + slide
;
154 // Add in the offset from the mach_header
155 newValue
+= baseAddress
;
156 *(uint64_t*)address
= newValue
;
158 // Regular pointer which needs to fit in 51-bits of value.
159 // C++ RTTI uses the top bit, so we'll allow the whole top-byte
160 // and the bottom 43-bits to be fit in to 51-bits.
161 uint64_t top8Bits
= value
& 0x0007F80000000000ULL
;
162 uint64_t bottom43Bits
= value
& 0x000007FFFFFFFFFFULL
;
163 uint64_t targetValue
= (top8Bits
<< 13) | (((intptr_t)(bottom43Bits
<< 21) >> 21) & 0x00FFFFFFFFFFFFFF);
164 targetValue
= targetValue
+ slide
;
165 *(uint64_t*)address
= targetValue
;
169 // The delta is bits [51..61]
170 // And bit 62 is to tell us if we are a rebase (0) or bind (1)
171 value
&= ~(1ULL << 62);
172 delta
= (value
& 0x3FF8000000000000) >> 51;
173 address
+= delta
* stepMultiplier
;
174 } while (delta
!= 0);
177 // Note, the following method should come from a header from dyld
179 rebase_threaded_starts(uint32_t *threadArrayStart
, uint32_t *threadArrayEnd
,
180 uintptr_t macho_header_addr
, uintptr_t macho_header_vmaddr
, size_t slide
)
182 uint32_t threadStartsHeader
= *threadArrayStart
;
183 uint64_t stepMultiplier
= (threadStartsHeader
& 1) == 1 ? 8 : 4;
184 for (uint32_t* threadOffset
= threadArrayStart
+ 1; threadOffset
!= threadArrayEnd
; ++threadOffset
) {
185 if (*threadOffset
== 0xFFFFFFFF) {
188 rebase_chain(macho_header_addr
+ *threadOffset
, stepMultiplier
, macho_header_vmaddr
, slide
);
198 extern uint32_t __thread_starts_sect_start
[] __asm("section$start$__TEXT$__thread_starts");
199 extern uint32_t __thread_starts_sect_end
[] __asm("section$end$__TEXT$__thread_starts");
209 processor_t my_master_proc
;
211 // rebase and sign jops
212 if (&__thread_starts_sect_end
[0] != &__thread_starts_sect_start
[0]) {
213 uintptr_t mh
= (uintptr_t) &_mh_execute_header
;
214 uintptr_t slide
= mh
- VM_KERNEL_LINK_ADDRESS
;
215 rebase_threaded_starts( &__thread_starts_sect_start
[0],
216 &__thread_starts_sect_end
[0],
217 mh
, mh
- slide
, slide
);
220 /* If kernel integrity is supported, use a constant copy of the boot args. */
221 const_boot_args
= *args
;
222 BootArgs
= args
= &const_boot_args
;
224 cpu_data_init(&BootCpuData
);
226 PE_init_platform(FALSE
, args
); /* Get platform expert set up */
232 unsigned int tmp_16k
= 0;
236 * Select the advertised kernel page size; without the boot-arg
237 * we default to the hardware page size for the current platform.
239 if (PE_parse_boot_argn("-vm16k", &tmp_16k
, sizeof(tmp_16k
))) {
240 PAGE_SHIFT_CONST
= PAGE_MAX_SHIFT
;
242 PAGE_SHIFT_CONST
= ARM_PGSHIFT
;
246 * Select the advertised kernel page size; with the boot-arg
247 * use to the hardware page size for the current platform.
249 int radar_20804515
= 1; /* default: new mode */
250 PE_parse_boot_argn("radar_20804515", &radar_20804515
, sizeof(radar_20804515
));
251 if (radar_20804515
) {
252 if (args
->memSize
> 1ULL * 1024 * 1024 * 1024) {
254 * arm64 device with > 1GB of RAM:
255 * kernel uses 16KB pages.
257 PAGE_SHIFT_CONST
= PAGE_MAX_SHIFT
;
260 * arm64 device with <= 1GB of RAM:
261 * kernel uses hardware page size
262 * (4KB for H6/H7, 16KB for H8+).
264 PAGE_SHIFT_CONST
= ARM_PGSHIFT
;
266 /* 32-bit apps always see 16KB page size */
267 page_shift_user32
= PAGE_MAX_SHIFT
;
269 /* kernel page size: */
270 if (PE_parse_boot_argn("-use_hwpagesize", &tmp_16k
, sizeof(tmp_16k
))) {
271 PAGE_SHIFT_CONST
= ARM_PGSHIFT
;
273 PAGE_SHIFT_CONST
= PAGE_MAX_SHIFT
;
275 /* old mode: 32-bit apps see same page size as kernel */
276 page_shift_user32
= PAGE_SHIFT_CONST
;
280 if (PE_parse_boot_argn("cpus_defeatures", &cpus_defeatures
, sizeof(cpus_defeatures
))) {
281 if ((cpus_defeatures
& 0xF) != 0) {
282 cpu_defeatures_set(cpus_defeatures
& 0xF);
289 ml_parse_cpu_topology();
291 master_cpu
= ml_get_boot_cpu_number();
292 assert(master_cpu
>= 0 && master_cpu
<= ml_get_max_cpu_number());
294 BootCpuData
.cpu_number
= (unsigned short)master_cpu
;
296 BootCpuData
.cpu_exc_vectors
= (vm_offset_t
)&ExceptionVectorsTable
;
298 BootCpuData
.intstack_top
= (vm_offset_t
) &intstack_top
;
299 BootCpuData
.istackptr
= BootCpuData
.intstack_top
;
301 BootCpuData
.excepstack_top
= (vm_offset_t
) &excepstack_top
;
302 BootCpuData
.excepstackptr
= BootCpuData
.excepstack_top
;
304 BootCpuData
.fiqstack_top
= (vm_offset_t
) &fiqstack_top
;
305 BootCpuData
.fiqstackptr
= BootCpuData
.fiqstack_top
;
307 BootCpuData
.cpu_processor
= cpu_processor_alloc(TRUE
);
308 BootCpuData
.cpu_console_buf
= (void *)NULL
;
309 CpuDataEntries
[master_cpu
].cpu_data_vaddr
= &BootCpuData
;
310 CpuDataEntries
[master_cpu
].cpu_data_paddr
= (void *)((uintptr_t)(args
->physBase
)
311 + ((uintptr_t)&BootCpuData
312 - (uintptr_t)(args
->virtBase
)));
315 thread
= current_thread();
317 * Preemption is enabled for this thread so that it can lock mutexes without
318 * tripping the preemption check. In reality scheduling is not enabled until
319 * this thread completes, and there are no other threads to switch to, so
320 * preemption level is not really meaningful for the bootstrap thread.
322 thread
->machine
.preemption_count
= 0;
323 thread
->machine
.CpuDatap
= &BootCpuData
;
324 #if __arm__ && __ARM_USER_PROTECT__
326 unsigned int ttbr0_val
, ttbr1_val
, ttbcr_val
;
327 __asm__
volatile ("mrc p15,0,%0,c2,c0,0\n" : "=r"(ttbr0_val
));
328 __asm__
volatile ("mrc p15,0,%0,c2,c0,1\n" : "=r"(ttbr1_val
));
329 __asm__
volatile ("mrc p15,0,%0,c2,c0,2\n" : "=r"(ttbcr_val
));
330 thread
->machine
.uptw_ttb
= ttbr0_val
;
331 thread
->machine
.kptw_ttb
= ttbr1_val
;
332 thread
->machine
.uptw_ttc
= ttbcr_val
;
335 BootCpuData
.cpu_processor
->processor_data
.kernel_timer
= &thread
->system_timer
;
336 BootCpuData
.cpu_processor
->processor_data
.thread_timer
= &thread
->system_timer
;
340 rtclock_early_init();
342 kernel_early_bootstrap();
346 EntropyData
.index_ptr
= EntropyData
.buffer
;
348 processor_bootstrap();
349 my_master_proc
= master_processor
;
351 (void)PE_parse_boot_argn("diag", &arm_diag
, sizeof(arm_diag
));
353 if (PE_parse_boot_argn("maxmem", &maxmem
, sizeof(maxmem
))) {
354 xmaxmem
= (uint64_t) maxmem
* (1024 * 1024);
355 } else if (PE_get_default("hw.memsize", &memsize
, sizeof(memsize
))) {
356 xmaxmem
= (uint64_t) memsize
;
361 if (PE_parse_boot_argn("up_style_idle_exit", &up_style_idle_exit
, sizeof(up_style_idle_exit
))) {
362 up_style_idle_exit
= 1;
364 #if INTERRUPT_MASKED_DEBUG
365 int wdt_boot_arg
= 0;
366 /* Disable if WDT is disabled or no_interrupt_mask_debug in boot-args */
367 if (PE_parse_boot_argn("no_interrupt_masked_debug", &interrupt_masked_debug
,
368 sizeof(interrupt_masked_debug
)) || (PE_parse_boot_argn("wdt", &wdt_boot_arg
,
369 sizeof(wdt_boot_arg
)) && (wdt_boot_arg
== -1))) {
370 interrupt_masked_debug
= 0;
373 PE_parse_boot_argn("interrupt_masked_debug_timeout", &interrupt_masked_timeout
, sizeof(interrupt_masked_timeout
));
378 PE_parse_boot_argn("immediate_NMI", &force_immediate_debug_halt
, sizeof(force_immediate_debug_halt
));
380 #if __ARM_PAN_AVAILABLE__
381 __builtin_arm_wsr("pan", 1);
382 #endif /* __ARM_PAN_AVAILABLE__ */
384 arm_vm_init(xmaxmem
, args
);
387 if (PE_parse_boot_argn("debug", &debugmode
, sizeof(debugmode
)) &&
395 /* Enable asynchronous exceptions */
396 __builtin_arm_wsr("DAIFClr", DAIFSC_ASYNCF
);
398 #if __arm64__ && WITH_CLASSIC_S2R
399 sleep_token_buffer_init();
402 PE_consistent_debug_inherit();
404 /* setup debugging output if one has been chosen */
405 PE_init_kprintf(FALSE
);
407 kprintf("kprintf initialized\n");
409 serialmode
= 0; /* Assume normal keyboard and console */
410 if (PE_parse_boot_argn("serial", &serialmode
, sizeof(serialmode
))) { /* Do we want a serial
413 kprintf("Serial mode specified: %08X\n", serialmode
);
414 int force_sync
= serialmode
& SERIALMODE_SYNCDRAIN
;
415 if (force_sync
|| PE_parse_boot_argn("drain_uart_sync", &force_sync
, sizeof(force_sync
))) {
417 serialmode
|= SERIALMODE_SYNCDRAIN
;
419 "WARNING: Forcing uart driver to output synchronously."
420 "printf()s/IOLogs will impact kernel performance.\n"
421 "You are advised to avoid using 'drain_uart_sync' boot-arg.\n");
425 if (kern_feature_override(KF_SERIAL_OVRD
)) {
429 if (serialmode
& SERIALMODE_OUTPUT
) { /* Start serial if requested */
430 (void)switch_to_serial_console(); /* Switch into serial mode */
431 disableConsoleOutput
= FALSE
; /* Allow printfs to happen */
435 /* setup console output */
436 PE_init_printf(FALSE
);
444 cpu_machine_idle_init(TRUE
);
446 #if (__ARM_ARCH__ == 7)
447 if (arm_diag
& 0x8000) {
448 set_mmu_control((get_mmu_control()) ^ SCTLR_PREDIC
);
452 PE_init_platform(TRUE
, &BootCpuData
);
453 cpu_timebase_init(TRUE
);
454 fiq_context_bootstrap(TRUE
);
458 * Initialize the stack protector for all future calls
459 * to C code. Since kernel_bootstrap() eventually
460 * switches stack context without returning through this
461 * function, we do not risk failing the check even though
462 * we mutate the guard word during execution.
464 __stack_chk_guard
= (unsigned long)early_random();
465 /* Zero a byte of the protector to guard
466 * against string vulnerabilities
468 __stack_chk_guard
&= ~(0xFFULL
<< 8);
469 machine_startup(args
);
473 * Routine: arm_init_cpu
475 * Re-initialize CPU when coming out of reset
480 cpu_data_t
*cpu_data_ptr
)
482 #if __ARM_PAN_AVAILABLE__
483 __builtin_arm_wsr("pan", 1);
486 cpu_data_ptr
->cpu_flags
&= ~SleepState
;
487 #if __ARM_SMP__ && defined(ARMA7)
488 cpu_data_ptr
->cpu_CLW_active
= 1;
491 machine_set_current_thread(cpu_data_ptr
->cpu_active_thread
);
494 pmap_clear_user_ttb();
496 /* Enable asynchronous exceptions */
497 __builtin_arm_wsr("DAIFClr", DAIFSC_ASYNCF
);
500 cpu_machine_idle_init(FALSE
);
504 #if (__ARM_ARCH__ == 7)
505 if (arm_diag
& 0x8000) {
506 set_mmu_control((get_mmu_control()) ^ SCTLR_PREDIC
);
510 if ((cpus_defeatures
& (0xF << 4 * cpu_data_ptr
->cpu_number
)) != 0) {
511 cpu_defeatures_set((cpus_defeatures
>> 4 * cpu_data_ptr
->cpu_number
) & 0xF);
514 /* Initialize the timebase before serial_init, as some serial
515 * drivers use mach_absolute_time() to implement rate control
517 cpu_timebase_init(FALSE
);
519 if (cpu_data_ptr
== &BootCpuData
) {
520 #if __arm64__ && __ARM_GLOBAL_SLEEP_BIT__
522 * Prevent CPUs from going into deep sleep until all
523 * CPUs are ready to do so.
525 arm64_stall_sleep
= TRUE
;
528 PE_init_platform(TRUE
, NULL
);
529 commpage_update_timebase();
532 fiq_context_init(TRUE
);
533 cpu_data_ptr
->rtcPop
= EndOfAllTime
;
534 timer_resync_deadlines();
536 #if DEVELOPMENT || DEBUG
537 PE_arm_debug_enable_trace();
540 kprintf("arm_cpu_init(): cpu %d online\n", cpu_data_ptr
->cpu_processor
->cpu_id
);
542 if (cpu_data_ptr
== &BootCpuData
) {
544 bootprofile_wake_from_sleep();
545 #endif /* CONFIG_TELEMETRY */
547 #if MONOTONIC && defined(__arm64__)
549 #endif /* MONOTONIC && defined(__arm64__) */
556 * Routine: arm_init_idle_cpu
559 void __attribute__((noreturn
))
561 cpu_data_t
*cpu_data_ptr
)
563 #if __ARM_PAN_AVAILABLE__
564 __builtin_arm_wsr("pan", 1);
566 #if __ARM_SMP__ && defined(ARMA7)
567 cpu_data_ptr
->cpu_CLW_active
= 1;
570 machine_set_current_thread(cpu_data_ptr
->cpu_active_thread
);
573 pmap_clear_user_ttb();
575 /* Enable asynchronous exceptions */
576 __builtin_arm_wsr("DAIFClr", DAIFSC_ASYNCF
);
579 #if (__ARM_ARCH__ == 7)
580 if (arm_diag
& 0x8000) {
581 set_mmu_control((get_mmu_control()) ^ SCTLR_PREDIC
);
585 if ((cpus_defeatures
& (0xF << 4 * cpu_data_ptr
->cpu_number
)) != 0) {
586 cpu_defeatures_set((cpus_defeatures
>> 4 * cpu_data_ptr
->cpu_number
) & 0xF);
590 fiq_context_init(FALSE
);