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
;
86 extern vm_offset_t fiqstack_top
;
88 extern vm_offset_t excepstack_top
;
91 extern const char version
[];
92 extern const char version_variant
[];
93 extern int disableConsoleOutput
;
95 int pc_trace_buf
[PC_TRACE_BUF_SIZE
] = {0};
96 int pc_trace_cnt
= PC_TRACE_BUF_SIZE
;
99 boolean_t up_style_idle_exit
= 0;
103 #if INTERRUPT_MASKED_DEBUG
104 boolean_t interrupt_masked_debug
= 1;
105 uint64_t interrupt_masked_timeout
= 0xd0000;
108 boot_args const_boot_args
__attribute__((section("__DATA, __const")));
109 boot_args
*BootArgs
__attribute__((section("__DATA, __const")));
111 unsigned int arm_diag
;
113 static unsigned cpus_defeatures
= 0x0;
114 extern void cpu_defeatures_set(unsigned int);
117 #if __arm64__ && __ARM_GLOBAL_SLEEP_BIT__
118 extern volatile boolean_t arm64_stall_sleep
;
121 extern boolean_t force_immediate_debug_halt
;
123 #define MIN_LOW_GLO_MASK (0x144)
128 void arm_init(boot_args
* args
);
131 unsigned int page_shift_user32
; /* for page_size as seen by a 32-bit task */
132 #endif /* __arm64__ */
147 processor_t my_master_proc
;
149 /* If kernel integrity is supported, use a constant copy of the boot args. */
150 const_boot_args
= *args
;
151 BootArgs
= &const_boot_args
;
153 cpu_data_init(&BootCpuData
);
155 PE_init_platform(FALSE
, args
); /* Get platform expert set up */
159 unsigned int tmp_16k
= 0;
163 * Select the advertised kernel page size; without the boot-arg
164 * we default to the hardware page size for the current platform.
166 if (PE_parse_boot_argn("-vm16k", &tmp_16k
, sizeof(tmp_16k
)))
167 PAGE_SHIFT_CONST
= PAGE_MAX_SHIFT
;
169 PAGE_SHIFT_CONST
= ARM_PGSHIFT
;
172 * Select the advertised kernel page size; with the boot-arg
173 * use to the hardware page size for the current platform.
175 int radar_20804515
= 1; /* default: new mode */
176 PE_parse_boot_argn("radar_20804515", &radar_20804515
, sizeof(radar_20804515
));
177 if (radar_20804515
) {
178 if (args
->memSize
> 1ULL*1024*1024*1024) {
180 * arm64 device with > 1GB of RAM:
181 * kernel uses 16KB pages.
183 PAGE_SHIFT_CONST
= PAGE_MAX_SHIFT
;
186 * arm64 device with <= 1GB of RAM:
187 * kernel uses hardware page size
188 * (4KB for H6/H7, 16KB for H8+).
190 PAGE_SHIFT_CONST
= ARM_PGSHIFT
;
192 /* 32-bit apps always see 16KB page size */
193 page_shift_user32
= PAGE_MAX_SHIFT
;
195 /* kernel page size: */
196 if (PE_parse_boot_argn("-use_hwpagesize", &tmp_16k
, sizeof(tmp_16k
)))
197 PAGE_SHIFT_CONST
= ARM_PGSHIFT
;
199 PAGE_SHIFT_CONST
= PAGE_MAX_SHIFT
;
200 /* old mode: 32-bit apps see same page size as kernel */
201 page_shift_user32
= PAGE_SHIFT_CONST
;
205 if (PE_parse_boot_argn("cpus_defeatures", &cpus_defeatures
, sizeof(cpus_defeatures
))) {
206 if ((cpus_defeatures
& 0xF) != 0)
207 cpu_defeatures_set(cpus_defeatures
& 0xF);
213 ml_parse_cpu_topology();
215 master_cpu
= ml_get_boot_cpu_number();
216 assert(master_cpu
>= 0 && master_cpu
<= ml_get_max_cpu_number());
218 BootCpuData
.cpu_number
= (unsigned short)master_cpu
;
220 BootCpuData
.cpu_exc_vectors
= (vm_offset_t
)&ExceptionVectorsTable
;
222 BootCpuData
.intstack_top
= (vm_offset_t
) & intstack_top
;
223 BootCpuData
.istackptr
= BootCpuData
.intstack_top
;
224 BootCpuData
.fiqstack_top
= (vm_offset_t
) & fiqstack_top
;
225 BootCpuData
.fiqstackptr
= BootCpuData
.fiqstack_top
;
227 BootCpuData
.excepstack_top
= (vm_offset_t
) & excepstack_top
;
228 BootCpuData
.excepstackptr
= BootCpuData
.excepstack_top
;
230 BootCpuData
.cpu_processor
= cpu_processor_alloc(TRUE
);
231 BootCpuData
.cpu_console_buf
= (void *)NULL
;
232 CpuDataEntries
[master_cpu
].cpu_data_vaddr
= &BootCpuData
;
233 CpuDataEntries
[master_cpu
].cpu_data_paddr
= (void *)((uintptr_t)(args
->physBase
)
234 + ((uintptr_t)&BootCpuData
235 - (uintptr_t)(args
->virtBase
)));
238 thread
= current_thread();
240 * Preemption is enabled for this thread so that it can lock mutexes without
241 * tripping the preemption check. In reality scheduling is not enabled until
242 * this thread completes, and there are no other threads to switch to, so
243 * preemption level is not really meaningful for the bootstrap thread.
245 thread
->machine
.preemption_count
= 0;
246 thread
->machine
.CpuDatap
= &BootCpuData
;
247 #if __arm__ && __ARM_USER_PROTECT__
249 unsigned int ttbr0_val
, ttbr1_val
, ttbcr_val
;
250 __asm__
volatile("mrc p15,0,%0,c2,c0,0\n" : "=r"(ttbr0_val
));
251 __asm__
volatile("mrc p15,0,%0,c2,c0,1\n" : "=r"(ttbr1_val
));
252 __asm__
volatile("mrc p15,0,%0,c2,c0,2\n" : "=r"(ttbcr_val
));
253 thread
->machine
.uptw_ttb
= ttbr0_val
;
254 thread
->machine
.kptw_ttb
= ttbr1_val
;
255 thread
->machine
.uptw_ttc
= ttbcr_val
;
258 BootCpuData
.cpu_processor
->processor_data
.kernel_timer
= &thread
->system_timer
;
259 BootCpuData
.cpu_processor
->processor_data
.thread_timer
= &thread
->system_timer
;
263 rtclock_early_init();
265 kernel_early_bootstrap();
269 EntropyData
.index_ptr
= EntropyData
.buffer
;
271 processor_bootstrap();
272 my_master_proc
= master_processor
;
274 (void)PE_parse_boot_argn("diag", &arm_diag
, sizeof (arm_diag
));
276 if (PE_parse_boot_argn("maxmem", &maxmem
, sizeof (maxmem
)))
277 xmaxmem
= (uint64_t) maxmem
*(1024 * 1024);
278 else if (PE_get_default("hw.memsize", &memsize
, sizeof (memsize
)))
279 xmaxmem
= (uint64_t) memsize
;
283 if (PE_parse_boot_argn("up_style_idle_exit", &up_style_idle_exit
, sizeof(up_style_idle_exit
))) {
284 up_style_idle_exit
= 1;
286 #if INTERRUPT_MASKED_DEBUG
287 int wdt_boot_arg
= 0;
288 /* Disable if WDT is disabled or no_interrupt_mask_debug in boot-args */
289 if (PE_parse_boot_argn("no_interrupt_masked_debug", &interrupt_masked_debug
,
290 sizeof(interrupt_masked_debug
)) || (PE_parse_boot_argn("wdt", &wdt_boot_arg
,
291 sizeof(wdt_boot_arg
)) && (wdt_boot_arg
== -1))) {
292 interrupt_masked_debug
= 0;
295 PE_parse_boot_argn("interrupt_masked_debug_timeout", &interrupt_masked_timeout
, sizeof(interrupt_masked_timeout
));
300 PE_parse_boot_argn("immediate_NMI", &force_immediate_debug_halt
, sizeof(force_immediate_debug_halt
));
302 #if __ARM_PAN_AVAILABLE__
303 __builtin_arm_wsr("pan", 1);
304 #endif /* __ARM_PAN_AVAILABLE__ */
306 arm_vm_init(xmaxmem
, args
);
309 if (PE_parse_boot_argn("debug", &debugmode
, sizeof(debugmode
)) &&
310 ((debugmode
& MIN_LOW_GLO_MASK
) == MIN_LOW_GLO_MASK
))
315 #if __arm64__ && WITH_CLASSIC_S2R
316 sleep_token_buffer_init();
319 PE_consistent_debug_inherit();
321 /* setup debugging output if one has been chosen */
322 PE_init_kprintf(FALSE
);
324 kprintf("kprintf initialized\n");
326 serialmode
= 0; /* Assume normal keyboard and console */
327 if (PE_parse_boot_argn("serial", &serialmode
, sizeof(serialmode
))) { /* Do we want a serial
330 kprintf("Serial mode specified: %08X\n", serialmode
);
331 int force_sync
= serialmode
& SERIALMODE_SYNCDRAIN
;
332 if (force_sync
|| PE_parse_boot_argn("drain_uart_sync", &force_sync
, sizeof(force_sync
))) {
334 serialmode
|= SERIALMODE_SYNCDRAIN
;
336 "WARNING: Forcing uart driver to output synchronously."
337 "printf()s/IOLogs will impact kernel performance.\n"
338 "You are advised to avoid using 'drain_uart_sync' boot-arg.\n");
342 if (kern_feature_override(KF_SERIAL_OVRD
)) {
346 if (serialmode
& SERIALMODE_OUTPUT
) { /* Start serial if requested */
347 (void)switch_to_serial_console(); /* Switch into serial mode */
348 disableConsoleOutput
= FALSE
; /* Allow printfs to happen */
352 /* setup console output */
353 PE_init_printf(FALSE
);
361 cpu_machine_idle_init(TRUE
);
363 #if (__ARM_ARCH__ == 7)
364 if (arm_diag
& 0x8000)
365 set_mmu_control((get_mmu_control()) ^ SCTLR_PREDIC
);
368 PE_init_platform(TRUE
, &BootCpuData
);
369 cpu_timebase_init(TRUE
);
370 fiq_context_init(TRUE
);
374 * Initialize the stack protector for all future calls
375 * to C code. Since kernel_bootstrap() eventually
376 * switches stack context without returning through this
377 * function, we do not risk failing the check even though
378 * we mutate the guard word during execution.
380 __stack_chk_guard
= (unsigned long)early_random();
381 /* Zero a byte of the protector to guard
382 * against string vulnerabilities
384 __stack_chk_guard
&= ~(0xFFULL
<< 8);
385 machine_startup(args
);
389 * Routine: arm_init_cpu
391 * Re-initialize CPU when coming out of reset
396 cpu_data_t
*cpu_data_ptr
)
398 #if __ARM_PAN_AVAILABLE__
399 __builtin_arm_wsr("pan", 1);
402 cpu_data_ptr
->cpu_flags
&= ~SleepState
;
403 #if __ARM_SMP__ && defined(ARMA7)
404 cpu_data_ptr
->cpu_CLW_active
= 1;
407 machine_set_current_thread(cpu_data_ptr
->cpu_active_thread
);
410 /* Enable asynchronous exceptions */
411 __builtin_arm_wsr("DAIFClr", DAIFSC_ASYNCF
);
414 cpu_machine_idle_init(FALSE
);
418 #if (__ARM_ARCH__ == 7)
419 if (arm_diag
& 0x8000)
420 set_mmu_control((get_mmu_control()) ^ SCTLR_PREDIC
);
423 if ((cpus_defeatures
& (0xF << 4*cpu_data_ptr
->cpu_number
)) != 0)
424 cpu_defeatures_set((cpus_defeatures
>> 4*cpu_data_ptr
->cpu_number
) & 0xF);
426 /* Initialize the timebase before serial_init, as some serial
427 * drivers use mach_absolute_time() to implement rate control
429 cpu_timebase_init(FALSE
);
431 if (cpu_data_ptr
== &BootCpuData
) {
432 #if __arm64__ && __ARM_GLOBAL_SLEEP_BIT__
434 * Prevent CPUs from going into deep sleep until all
435 * CPUs are ready to do so.
437 arm64_stall_sleep
= TRUE
;
440 PE_init_platform(TRUE
, NULL
);
441 commpage_update_timebase();
444 fiq_context_init(TRUE
);
445 cpu_data_ptr
->rtcPop
= EndOfAllTime
;
446 timer_resync_deadlines();
448 #if DEVELOPMENT || DEBUG
449 PE_arm_debug_enable_trace();
452 kprintf("arm_cpu_init(): cpu %d online\n", cpu_data_ptr
->cpu_processor
->cpu_id
);
454 if (cpu_data_ptr
== &BootCpuData
) {
456 bootprofile_wake_from_sleep();
457 #endif /* CONFIG_TELEMETRY */
458 #if MONOTONIC && defined(__arm64__)
460 #endif /* MONOTONIC && defined(__arm64__) */
467 * Routine: arm_init_idle_cpu
470 void __attribute__((noreturn
))
472 cpu_data_t
*cpu_data_ptr
)
474 #if __ARM_PAN_AVAILABLE__
475 __builtin_arm_wsr("pan", 1);
477 #if __ARM_SMP__ && defined(ARMA7)
478 cpu_data_ptr
->cpu_CLW_active
= 1;
481 machine_set_current_thread(cpu_data_ptr
->cpu_active_thread
);
484 /* Enable asynchronous exceptions */
485 __builtin_arm_wsr("DAIFClr", DAIFSC_ASYNCF
);
488 #if (__ARM_ARCH__ == 7)
489 if (arm_diag
& 0x8000)
490 set_mmu_control((get_mmu_control()) ^ SCTLR_PREDIC
);
493 if ((cpus_defeatures
& (0xF << 4*cpu_data_ptr
->cpu_number
)) != 0)
494 cpu_defeatures_set((cpus_defeatures
>> 4*cpu_data_ptr
->cpu_number
) & 0xF);
497 fiq_context_init(FALSE
);