]> git.saurik.com Git - apple/xnu.git/blob - osfmk/arm64/cpu.c
fc65933e54839c0f9e0dae8e88f907fcba239922
[apple/xnu.git] / osfmk / arm64 / cpu.c
1 /*
2 * Copyright (c) 2007-2016 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * File: arm64/cpu.c
30 *
31 * cpu specific routines
32 */
33
34 #include <pexpert/arm64/board_config.h>
35 #include <kern/kalloc.h>
36 #include <kern/machine.h>
37 #include <kern/cpu_number.h>
38 #include <kern/percpu.h>
39 #include <kern/thread.h>
40 #include <kern/timer_queue.h>
41 #include <arm/cpu_data.h>
42 #include <arm/cpuid.h>
43 #include <arm/caches_internal.h>
44 #include <arm/cpu_data_internal.h>
45 #include <arm/cpu_internal.h>
46 #include <arm/misc_protos.h>
47 #include <arm/machine_cpu.h>
48 #include <arm/rtclock.h>
49 #include <arm64/proc_reg.h>
50 #include <mach/processor_info.h>
51 #include <vm/pmap.h>
52 #include <vm/vm_kern.h>
53 #include <vm/vm_map.h>
54 #include <pexpert/arm/protos.h>
55 #include <pexpert/device_tree.h>
56 #include <sys/kdebug.h>
57 #include <arm/machine_routines.h>
58
59 #include <machine/atomic.h>
60
61 #include <san/kasan.h>
62
63 #if KPC
64 #include <kern/kpc.h>
65 #endif
66
67 #if MONOTONIC
68 #include <kern/monotonic.h>
69 #endif /* MONOTONIC */
70
71 #if HIBERNATION
72 #include <IOKit/IOPlatformExpert.h>
73 #include <IOKit/IOHibernatePrivate.h>
74 #endif /* HIBERNATION */
75
76
77 #include <libkern/section_keywords.h>
78
79 extern boolean_t idle_enable;
80 extern uint64_t wake_abstime;
81
82 #if WITH_CLASSIC_S2R
83 void sleep_token_buffer_init(void);
84 #endif
85
86
87 extern uintptr_t resume_idle_cpu;
88 extern uintptr_t start_cpu;
89
90 #if __ARM_KERNEL_PROTECT__
91 extern void exc_vectors_table;
92 #endif /* __ARM_KERNEL_PROTECT__ */
93
94 extern void __attribute__((noreturn)) arm64_prepare_for_sleep(boolean_t deep_sleep);
95 extern void arm64_force_wfi_clock_gate(void);
96 #if defined(APPLETYPHOON)
97 // <rdar://problem/15827409>
98 extern void typhoon_prepare_for_wfi(void);
99 extern void typhoon_return_from_wfi(void);
100 #endif
101
102 #if HAS_RETENTION_STATE
103 extern void arm64_retention_wfi(void);
104 #endif
105
106 vm_address_t start_cpu_paddr;
107
108 sysreg_restore_t sysreg_restore __attribute__((section("__DATA, __const"))) = {
109 .tcr_el1 = TCR_EL1_BOOT,
110 };
111
112
113 // wfi - wfi mode
114 // 0 : disabled
115 // 1 : normal
116 // 2 : overhead simulation (delay & flags)
117 static int wfi = 1;
118
119 #if DEVELOPMENT || DEBUG
120
121 // wfi_flags
122 // 1 << 0 : flush L1s
123 // 1 << 1 : flush TLBs
124 static int wfi_flags = 0;
125
126 // wfi_delay - delay ticks after wfi exit
127 static uint64_t wfi_delay = 0;
128
129 #endif /* DEVELOPMENT || DEBUG */
130 #if DEVELOPMENT || DEBUG
131 static bool idle_proximate_timer_wfe = true;
132 static bool idle_proximate_io_wfe = true;
133 #define CPUPM_IDLE_WFE 0x5310300
134 #else
135 static const bool idle_proximate_timer_wfe = true;
136 static const bool idle_proximate_io_wfe = true;
137 #endif
138
139 #if __ARM_GLOBAL_SLEEP_BIT__
140 volatile boolean_t arm64_stall_sleep = TRUE;
141 #endif
142
143 #if WITH_CLASSIC_S2R
144 /*
145 * These must be aligned to avoid issues with calling bcopy_phys on them before
146 * we are done with pmap initialization.
147 */
148 static const uint8_t __attribute__ ((aligned(8))) suspend_signature[] = {'X', 'S', 'O', 'M', 'P', 'S', 'U', 'S'};
149 static const uint8_t __attribute__ ((aligned(8))) running_signature[] = {'X', 'S', 'O', 'M', 'N', 'N', 'U', 'R'};
150 #endif
151
152 #if WITH_CLASSIC_S2R
153 static vm_offset_t sleepTokenBuffer = (vm_offset_t)NULL;
154 #endif
155 static boolean_t coresight_debug_enabled = FALSE;
156
157 #if defined(CONFIG_XNUPOST)
158 void arm64_ipi_test_callback(void *);
159 void arm64_immediate_ipi_test_callback(void *);
160
161 void
162 arm64_ipi_test_callback(void *parm)
163 {
164 volatile uint64_t *ipi_test_data = parm;
165 cpu_data_t *cpu_data;
166
167 cpu_data = getCpuDatap();
168
169 *ipi_test_data = cpu_data->cpu_number;
170 }
171
172 void
173 arm64_immediate_ipi_test_callback(void *parm)
174 {
175 volatile uint64_t *ipi_test_data = parm;
176 cpu_data_t *cpu_data;
177
178 cpu_data = getCpuDatap();
179
180 *ipi_test_data = cpu_data->cpu_number + MAX_CPUS;
181 }
182
183 uint64_t arm64_ipi_test_data[MAX_CPUS * 2];
184
185 void
186 arm64_ipi_test()
187 {
188 volatile uint64_t *ipi_test_data, *immediate_ipi_test_data;
189 uint32_t timeout_ms = 100;
190 uint64_t then, now, delta;
191 int current_cpu_number = getCpuDatap()->cpu_number;
192
193 /*
194 * probably the only way to have this on most systems is with the
195 * cpus=1 boot-arg, but nonetheless, if we only have 1 CPU active,
196 * IPI is not available
197 */
198 if (real_ncpus == 1) {
199 return;
200 }
201
202 const unsigned int max_cpu_id = ml_get_max_cpu_number();
203 for (unsigned int i = 0; i <= max_cpu_id; ++i) {
204 ipi_test_data = &arm64_ipi_test_data[i];
205 immediate_ipi_test_data = &arm64_ipi_test_data[i + MAX_CPUS];
206 *ipi_test_data = ~i;
207 kern_return_t error = cpu_xcall((int)i, (void *)arm64_ipi_test_callback, (void *)(uintptr_t)ipi_test_data);
208 if (error != KERN_SUCCESS) {
209 panic("CPU %d was unable to IPI CPU %u: error %d", current_cpu_number, i, error);
210 }
211
212 while ((error = cpu_immediate_xcall((int)i, (void *)arm64_immediate_ipi_test_callback,
213 (void *)(uintptr_t)immediate_ipi_test_data)) == KERN_ALREADY_WAITING) {
214 now = mach_absolute_time();
215 absolutetime_to_nanoseconds(now - then, &delta);
216 if ((delta / NSEC_PER_MSEC) > timeout_ms) {
217 panic("CPU %d was unable to immediate-IPI CPU %u within %dms", current_cpu_number, i, timeout_ms);
218 }
219 }
220
221 if (error != KERN_SUCCESS) {
222 panic("CPU %d was unable to immediate-IPI CPU %u: error %d", current_cpu_number, i, error);
223 }
224
225 then = mach_absolute_time();
226
227 while ((*ipi_test_data != i) || (*immediate_ipi_test_data != (i + MAX_CPUS))) {
228 now = mach_absolute_time();
229 absolutetime_to_nanoseconds(now - then, &delta);
230 if ((delta / NSEC_PER_MSEC) > timeout_ms) {
231 panic("CPU %d tried to IPI CPU %d but didn't get correct responses within %dms, responses: %llx, %llx",
232 current_cpu_number, i, timeout_ms, *ipi_test_data, *immediate_ipi_test_data);
233 }
234 }
235 }
236 }
237 #endif /* defined(CONFIG_XNUPOST) */
238
239 static void
240 configure_coresight_registers(cpu_data_t *cdp)
241 {
242 int i;
243
244 assert(cdp);
245 vm_offset_t coresight_regs = ml_get_topology_info()->cpus[cdp->cpu_number].coresight_regs;
246
247 /*
248 * ARMv8 coresight registers are optional. If the device tree did not
249 * provide either cpu_regmap_paddr (from the legacy "reg-private" EDT property)
250 * or coresight_regs (from the new "coresight-reg" property), assume that
251 * coresight registers are not supported.
252 */
253 if (cdp->cpu_regmap_paddr || coresight_regs) {
254 for (i = 0; i < CORESIGHT_REGIONS; ++i) {
255 if (i == CORESIGHT_CTI) {
256 continue;
257 }
258 /* Skip debug-only registers on production chips */
259 if (((i == CORESIGHT_ED) || (i == CORESIGHT_UTT)) && !coresight_debug_enabled) {
260 continue;
261 }
262
263 if (!cdp->coresight_base[i]) {
264 if (coresight_regs) {
265 cdp->coresight_base[i] = coresight_regs + CORESIGHT_OFFSET(i);
266 } else {
267 uint64_t addr = cdp->cpu_regmap_paddr + CORESIGHT_OFFSET(i);
268 cdp->coresight_base[i] = (vm_offset_t)ml_io_map(addr, CORESIGHT_SIZE);
269 }
270
271 /*
272 * At this point, failing to io map the
273 * registers is considered as an error.
274 */
275 if (!cdp->coresight_base[i]) {
276 panic("unable to ml_io_map coresight regions");
277 }
278 }
279 /* Unlock EDLAR, CTILAR, PMLAR */
280 if (i != CORESIGHT_UTT) {
281 *(volatile uint32_t *)(cdp->coresight_base[i] + ARM_DEBUG_OFFSET_DBGLAR) = ARM_DBG_LOCK_ACCESS_KEY;
282 }
283 }
284 }
285 }
286
287
288 /*
289 * Routine: cpu_bootstrap
290 * Function:
291 */
292 void
293 cpu_bootstrap(void)
294 {
295 }
296
297 /*
298 * Routine: cpu_sleep
299 * Function:
300 */
301 void
302 cpu_sleep(void)
303 {
304 cpu_data_t *cpu_data_ptr = getCpuDatap();
305
306 pmap_switch_user_ttb(kernel_pmap);
307 cpu_data_ptr->cpu_active_thread = current_thread();
308 cpu_data_ptr->cpu_reset_handler = (uintptr_t) start_cpu_paddr;
309 cpu_data_ptr->cpu_flags |= SleepState;
310 cpu_data_ptr->cpu_user_debug = NULL;
311 #if KPC
312 kpc_idle();
313 #endif /* KPC */
314 #if MONOTONIC
315 mt_cpu_down(cpu_data_ptr);
316 #endif /* MONOTONIC */
317
318 CleanPoC_Dcache();
319
320 #if USE_APPLEARMSMP
321 if (ml_is_quiescing()) {
322 PE_cpu_machine_quiesce(cpu_data_ptr->cpu_id);
323 } else {
324 bool deep_sleep = PE_cpu_down(cpu_data_ptr->cpu_id);
325 cpu_data_ptr->cpu_sleep_token = ARM_CPU_ON_SLEEP_PATH;
326 // hang CPU on spurious wakeup
327 cpu_data_ptr->cpu_reset_handler = (uintptr_t)0;
328 __builtin_arm_dsb(DSB_ISH);
329 CleanPoU_Dcache();
330 arm64_prepare_for_sleep(deep_sleep);
331 }
332 #else
333 PE_cpu_machine_quiesce(cpu_data_ptr->cpu_id);
334 #endif
335 /*NOTREACHED*/
336 }
337
338 /*
339 * Routine: cpu_interrupt_is_pending
340 * Function: Returns the value of ISR. Due to how this register is
341 * is implemented, this returns 0 if there are no
342 * interrupts pending, so it can be used as a boolean test.
343 */
344 int
345 cpu_interrupt_is_pending(void)
346 {
347 uint64_t isr_value;
348 isr_value = __builtin_arm_rsr64("ISR_EL1");
349 return (int)isr_value;
350 }
351
352 static bool
353 cpu_proximate_timer(void)
354 {
355 return !SetIdlePop();
356 }
357
358 static bool
359 wfe_to_deadline_or_interrupt(uint32_t cid, uint64_t wfe_deadline, __unused cpu_data_t *cdp)
360 {
361 bool ipending = false;
362 while ((ipending = (cpu_interrupt_is_pending() != 0)) == false) {
363 /* Assumes event stream enablement
364 * TODO: evaluate temporarily stretching the per-CPU event
365 * interval to a larger value for possible efficiency
366 * improvements.
367 */
368 __builtin_arm_wfe();
369 #if DEVELOPMENT || DEBUG
370 cdp->wfe_count++;
371 #endif
372 if (wfe_deadline != ~0ULL) {
373 #if DEVELOPMENT || DEBUG
374 cdp->wfe_deadline_checks++;
375 #endif
376 /* Check if the WFE recommendation has expired.
377 * We do not recompute the deadline here.
378 */
379 if ((ml_cluster_wfe_timeout(cid) == 0) ||
380 mach_absolute_time() >= wfe_deadline) {
381 #if DEVELOPMENT || DEBUG
382 cdp->wfe_terminations++;
383 #endif
384 break;
385 }
386 }
387 }
388 /* TODO: worth refreshing pending interrupt status? */
389 return ipending;
390 }
391
392 /*
393 * Routine: cpu_idle
394 * Function:
395 */
396 void __attribute__((noreturn))
397 cpu_idle(void)
398 {
399 cpu_data_t *cpu_data_ptr = getCpuDatap();
400 uint64_t new_idle_timeout_ticks = 0x0ULL, lastPop;
401 bool idle_disallowed = false;
402
403 if (__improbable((!idle_enable))) {
404 idle_disallowed = true;
405 } else if (__improbable(cpu_data_ptr->cpu_signal & SIGPdisabled)) {
406 idle_disallowed = true;
407 }
408
409 if (__improbable(idle_disallowed)) {
410 Idle_load_context();
411 }
412
413 bool ipending = false;
414 uint32_t cid = ~0U;
415
416 if (__probable(idle_proximate_io_wfe == true)) {
417 uint64_t wfe_deadline = 0;
418 /* Check for an active perf. controller generated
419 * WFE recommendation for this cluster.
420 */
421 cid = cpu_data_ptr->cpu_cluster_id;
422 uint64_t wfe_ttd = 0;
423 if ((wfe_ttd = ml_cluster_wfe_timeout(cid)) != 0) {
424 wfe_deadline = mach_absolute_time() + wfe_ttd;
425 }
426
427 if (wfe_deadline != 0) {
428 /* Poll issuing event-bounded WFEs until an interrupt
429 * arrives or the WFE recommendation expires
430 */
431 ipending = wfe_to_deadline_or_interrupt(cid, wfe_deadline, cpu_data_ptr);
432 #if DEVELOPMENT || DEBUG
433 KDBG(CPUPM_IDLE_WFE, ipending, cpu_data_ptr->wfe_count, wfe_deadline, 0);
434 #endif
435 if (ipending == true) {
436 /* Back to machine_idle() */
437 Idle_load_context();
438 }
439 }
440 }
441
442 if (__improbable(cpu_proximate_timer())) {
443 if (idle_proximate_timer_wfe == true) {
444 /* Poll issuing WFEs until the expected
445 * timer FIQ arrives.
446 */
447 ipending = wfe_to_deadline_or_interrupt(cid, ~0ULL, cpu_data_ptr);
448 assert(ipending == true);
449 }
450 Idle_load_context();
451 }
452
453 lastPop = cpu_data_ptr->rtcPop;
454
455 cpu_data_ptr->cpu_active_thread = current_thread();
456 if (cpu_data_ptr->cpu_user_debug) {
457 arm_debug_set(NULL);
458 }
459 cpu_data_ptr->cpu_user_debug = NULL;
460
461 if (wfi && (cpu_data_ptr->cpu_idle_notify != NULL)) {
462 cpu_data_ptr->cpu_idle_notify(cpu_data_ptr->cpu_id, TRUE, &new_idle_timeout_ticks);
463 }
464
465 if (cpu_data_ptr->idle_timer_notify != NULL) {
466 if (new_idle_timeout_ticks == 0x0ULL) {
467 /* turn off the idle timer */
468 cpu_data_ptr->idle_timer_deadline = 0x0ULL;
469 } else {
470 /* set the new idle timeout */
471 clock_absolutetime_interval_to_deadline(new_idle_timeout_ticks, &cpu_data_ptr->idle_timer_deadline);
472 }
473 timer_resync_deadlines();
474 if (cpu_data_ptr->rtcPop != lastPop) {
475 SetIdlePop();
476 }
477 }
478
479 #if KPC
480 kpc_idle();
481 #endif
482 #if MONOTONIC
483 mt_cpu_idle(cpu_data_ptr);
484 #endif /* MONOTONIC */
485
486 if (wfi) {
487 #if !defined(APPLE_ARM64_ARCH_FAMILY)
488 platform_cache_idle_enter();
489 #endif
490
491 #if DEVELOPMENT || DEBUG
492 // When simulating wfi overhead,
493 // force wfi to clock gating only
494 if (wfi == 2) {
495 arm64_force_wfi_clock_gate();
496 }
497 #endif /* DEVELOPMENT || DEBUG */
498
499 #if defined(APPLETYPHOON)
500 // <rdar://problem/15827409> CPU1 Stuck in WFIWT Because of MMU Prefetch
501 typhoon_prepare_for_wfi();
502 #endif
503 __builtin_arm_dsb(DSB_SY);
504 #if HAS_RETENTION_STATE
505 arm64_retention_wfi();
506 #else
507 __builtin_arm_wfi();
508 #endif
509
510 #if defined(APPLETYPHOON)
511 // <rdar://problem/15827409> CPU1 Stuck in WFIWT Because of MMU Prefetch
512 typhoon_return_from_wfi();
513 #endif
514
515 #if DEVELOPMENT || DEBUG
516 // Handle wfi overhead simulation
517 if (wfi == 2) {
518 uint64_t deadline;
519
520 // Calculate wfi delay deadline
521 clock_absolutetime_interval_to_deadline(wfi_delay, &deadline);
522
523 // Flush L1 caches
524 if ((wfi_flags & 1) != 0) {
525 InvalidatePoU_Icache();
526 FlushPoC_Dcache();
527 }
528
529 // Flush TLBs
530 if ((wfi_flags & 2) != 0) {
531 flush_core_tlb();
532 }
533
534 // Wait for the ballance of the wfi delay
535 clock_delay_until(deadline);
536 }
537 #endif /* DEVELOPMENT || DEBUG */
538 #if !defined(APPLE_ARM64_ARCH_FAMILY)
539 platform_cache_idle_exit();
540 #endif
541 }
542
543 ClearIdlePop(TRUE);
544
545 cpu_idle_exit(FALSE);
546 }
547
548 /*
549 * Routine: cpu_idle_exit
550 * Function:
551 */
552 void
553 cpu_idle_exit(boolean_t from_reset)
554 {
555 uint64_t new_idle_timeout_ticks = 0x0ULL;
556 cpu_data_t *cpu_data_ptr = getCpuDatap();
557
558 assert(exception_stack_pointer() != 0);
559
560 /* Back from WFI, unlock OSLAR and EDLAR. */
561 if (from_reset) {
562 configure_coresight_registers(cpu_data_ptr);
563 }
564
565 #if KPC
566 kpc_idle_exit();
567 #endif
568
569 #if MONOTONIC
570 mt_cpu_run(cpu_data_ptr);
571 #endif /* MONOTONIC */
572
573 if (wfi && (cpu_data_ptr->cpu_idle_notify != NULL)) {
574 cpu_data_ptr->cpu_idle_notify(cpu_data_ptr->cpu_id, FALSE, &new_idle_timeout_ticks);
575 }
576
577 if (cpu_data_ptr->idle_timer_notify != NULL) {
578 if (new_idle_timeout_ticks == 0x0ULL) {
579 /* turn off the idle timer */
580 cpu_data_ptr->idle_timer_deadline = 0x0ULL;
581 } else {
582 /* set the new idle timeout */
583 clock_absolutetime_interval_to_deadline(new_idle_timeout_ticks, &cpu_data_ptr->idle_timer_deadline);
584 }
585 timer_resync_deadlines();
586 }
587
588 Idle_load_context();
589 }
590
591 void
592 cpu_init(void)
593 {
594 cpu_data_t *cdp = getCpuDatap();
595 arm_cpu_info_t *cpu_info_p;
596
597 assert(exception_stack_pointer() != 0);
598
599 if (cdp->cpu_type != CPU_TYPE_ARM64) {
600 cdp->cpu_type = CPU_TYPE_ARM64;
601
602 timer_call_queue_init(&cdp->rtclock_timer.queue);
603 cdp->rtclock_timer.deadline = EndOfAllTime;
604
605 if (cdp == &BootCpuData) {
606 do_cpuid();
607 do_cacheid();
608 do_mvfpid();
609 } else {
610 /*
611 * We initialize non-boot CPUs here; the boot CPU is
612 * dealt with as part of pmap_bootstrap.
613 */
614 pmap_cpu_data_init();
615 }
616 /* ARM_SMP: Assuming identical cpu */
617 do_debugid();
618
619 cpu_info_p = cpuid_info();
620
621 /* switch based on CPU's reported architecture */
622 switch (cpu_info_p->arm_info.arm_arch) {
623 case CPU_ARCH_ARMv8:
624 cdp->cpu_subtype = CPU_SUBTYPE_ARM64_V8;
625 break;
626 default:
627 //cdp->cpu_subtype = CPU_SUBTYPE_ARM64_ALL;
628 /* this panic doesn't work this early in startup */
629 panic("Unknown CPU subtype...");
630 break;
631 }
632
633 cdp->cpu_threadtype = CPU_THREADTYPE_NONE;
634 }
635 cdp->cpu_stat.irq_ex_cnt_wake = 0;
636 cdp->cpu_stat.ipi_cnt_wake = 0;
637 #if MONOTONIC
638 cdp->cpu_stat.pmi_cnt_wake = 0;
639 #endif /* MONOTONIC */
640 cdp->cpu_running = TRUE;
641 cdp->cpu_sleep_token_last = cdp->cpu_sleep_token;
642 cdp->cpu_sleep_token = 0x0UL;
643 #if KPC
644 kpc_idle_exit();
645 #endif /* KPC */
646 #if MONOTONIC
647 mt_cpu_up(cdp);
648 #endif /* MONOTONIC */
649 }
650
651 void
652 cpu_stack_alloc(cpu_data_t *cpu_data_ptr)
653 {
654 vm_offset_t irq_stack = 0;
655 vm_offset_t exc_stack = 0;
656
657 kern_return_t kr = kernel_memory_allocate(kernel_map, &irq_stack,
658 INTSTACK_SIZE + (2 * PAGE_SIZE),
659 PAGE_MASK,
660 KMA_GUARD_FIRST | KMA_GUARD_LAST | KMA_KSTACK | KMA_KOBJECT,
661 VM_KERN_MEMORY_STACK);
662 if (kr != KERN_SUCCESS) {
663 panic("Unable to allocate cpu interrupt stack\n");
664 }
665
666 cpu_data_ptr->intstack_top = irq_stack + PAGE_SIZE + INTSTACK_SIZE;
667 cpu_data_ptr->istackptr = cpu_data_ptr->intstack_top;
668
669 kr = kernel_memory_allocate(kernel_map, &exc_stack,
670 EXCEPSTACK_SIZE + (2 * PAGE_SIZE),
671 PAGE_MASK,
672 KMA_GUARD_FIRST | KMA_GUARD_LAST | KMA_KSTACK | KMA_KOBJECT,
673 VM_KERN_MEMORY_STACK);
674 if (kr != KERN_SUCCESS) {
675 panic("Unable to allocate cpu exception stack\n");
676 }
677
678 cpu_data_ptr->excepstack_top = exc_stack + PAGE_SIZE + EXCEPSTACK_SIZE;
679 cpu_data_ptr->excepstackptr = cpu_data_ptr->excepstack_top;
680 }
681
682 void
683 cpu_data_free(cpu_data_t *cpu_data_ptr)
684 {
685 if ((cpu_data_ptr == NULL) || (cpu_data_ptr == &BootCpuData)) {
686 return;
687 }
688
689 int cpu_number = cpu_data_ptr->cpu_number;
690
691 if (CpuDataEntries[cpu_number].cpu_data_vaddr == cpu_data_ptr) {
692 CpuDataEntries[cpu_number].cpu_data_vaddr = NULL;
693 CpuDataEntries[cpu_number].cpu_data_paddr = 0;
694 __builtin_arm_dmb(DMB_ISH); // Ensure prior stores to cpu array are visible
695 }
696 (kfree)((void *)(cpu_data_ptr->intstack_top - INTSTACK_SIZE), INTSTACK_SIZE);
697 (kfree)((void *)(cpu_data_ptr->excepstack_top - EXCEPSTACK_SIZE), EXCEPSTACK_SIZE);
698 }
699
700 void
701 cpu_data_init(cpu_data_t *cpu_data_ptr)
702 {
703 uint32_t i;
704
705 cpu_data_ptr->cpu_flags = 0;
706 cpu_data_ptr->cpu_int_state = 0;
707 cpu_data_ptr->cpu_pending_ast = AST_NONE;
708 cpu_data_ptr->cpu_cache_dispatch = NULL;
709 cpu_data_ptr->rtcPop = EndOfAllTime;
710 cpu_data_ptr->rtclock_datap = &RTClockData;
711 cpu_data_ptr->cpu_user_debug = NULL;
712
713
714 cpu_data_ptr->cpu_base_timebase = 0;
715 cpu_data_ptr->cpu_idle_notify = NULL;
716 cpu_data_ptr->cpu_idle_latency = 0x0ULL;
717 cpu_data_ptr->cpu_idle_pop = 0x0ULL;
718 cpu_data_ptr->cpu_reset_type = 0x0UL;
719 cpu_data_ptr->cpu_reset_handler = 0x0UL;
720 cpu_data_ptr->cpu_reset_assist = 0x0UL;
721 cpu_data_ptr->cpu_regmap_paddr = 0x0ULL;
722 cpu_data_ptr->cpu_phys_id = 0x0UL;
723 cpu_data_ptr->cpu_l2_access_penalty = 0;
724 cpu_data_ptr->cpu_cluster_type = CLUSTER_TYPE_SMP;
725 cpu_data_ptr->cpu_cluster_id = 0;
726 cpu_data_ptr->cpu_l2_id = 0;
727 cpu_data_ptr->cpu_l2_size = 0;
728 cpu_data_ptr->cpu_l3_id = 0;
729 cpu_data_ptr->cpu_l3_size = 0;
730
731 cpu_data_ptr->cpu_signal = SIGPdisabled;
732
733 cpu_data_ptr->cpu_get_fiq_handler = NULL;
734 cpu_data_ptr->cpu_tbd_hardware_addr = NULL;
735 cpu_data_ptr->cpu_tbd_hardware_val = NULL;
736 cpu_data_ptr->cpu_get_decrementer_func = NULL;
737 cpu_data_ptr->cpu_set_decrementer_func = NULL;
738 cpu_data_ptr->cpu_sleep_token = ARM_CPU_ON_SLEEP_PATH;
739 cpu_data_ptr->cpu_sleep_token_last = 0x00000000UL;
740 cpu_data_ptr->cpu_xcall_p0 = NULL;
741 cpu_data_ptr->cpu_xcall_p1 = NULL;
742 cpu_data_ptr->cpu_imm_xcall_p0 = NULL;
743 cpu_data_ptr->cpu_imm_xcall_p1 = NULL;
744
745 for (i = 0; i < CORESIGHT_REGIONS; ++i) {
746 cpu_data_ptr->coresight_base[i] = 0;
747 }
748
749 #if !XNU_MONITOR
750 pmap_cpu_data_t * pmap_cpu_data_ptr = &cpu_data_ptr->cpu_pmap_cpu_data;
751
752 pmap_cpu_data_ptr->cpu_nested_pmap = (struct pmap *) NULL;
753 pmap_cpu_data_ptr->cpu_number = PMAP_INVALID_CPU_NUM;
754 pmap_cpu_data_ptr->pv_free.list = NULL;
755 pmap_cpu_data_ptr->pv_free.count = 0;
756 pmap_cpu_data_ptr->pv_free_tail = NULL;
757
758 bzero(&(pmap_cpu_data_ptr->cpu_sw_asids[0]), sizeof(pmap_cpu_data_ptr->cpu_sw_asids));
759 #endif
760 cpu_data_ptr->halt_status = CPU_NOT_HALTED;
761 #if __ARM_KERNEL_PROTECT__
762 cpu_data_ptr->cpu_exc_vectors = (vm_offset_t)&exc_vectors_table;
763 #endif /* __ARM_KERNEL_PROTECT__ */
764
765 #if defined(HAS_APPLE_PAC)
766 cpu_data_ptr->rop_key = 0;
767 cpu_data_ptr->jop_key = ml_default_jop_pid();
768 #endif
769
770 }
771
772 kern_return_t
773 cpu_data_register(cpu_data_t *cpu_data_ptr)
774 {
775 int cpu = cpu_data_ptr->cpu_number;
776
777 #if KASAN
778 for (int i = 0; i < CPUWINDOWS_MAX; i++) {
779 kasan_notify_address_nopoison(pmap_cpu_windows_copy_addr(cpu, i), PAGE_SIZE);
780 }
781 #endif
782
783 __builtin_arm_dmb(DMB_ISH); // Ensure prior stores to cpu data are visible
784 CpuDataEntries[cpu].cpu_data_vaddr = cpu_data_ptr;
785 CpuDataEntries[cpu].cpu_data_paddr = (void *)ml_vtophys((vm_offset_t)cpu_data_ptr);
786 return KERN_SUCCESS;
787 }
788
789 #if defined(KERNEL_INTEGRITY_CTRR)
790 /* Hibernation needs to reset this state, so data and text are in the hib segment;
791 * this allows them be accessed and executed early.
792 */
793 LCK_GRP_DECLARE(ctrr_cpu_start_lock_grp, "ctrr_cpu_start_lock");
794 LCK_SPIN_DECLARE(ctrr_cpu_start_lck, &ctrr_cpu_start_lock_grp);
795 enum ctrr_cluster_states ctrr_cluster_locked[MAX_CPU_CLUSTERS] MARK_AS_HIBERNATE_DATA;
796
797 MARK_AS_HIBERNATE_TEXT
798 void
799 init_ctrr_cluster_states(void)
800 {
801 for (int i = 0; i < MAX_CPU_CLUSTERS; i++) {
802 ctrr_cluster_locked[i] = CTRR_UNLOCKED;
803 }
804 }
805 #endif
806
807 kern_return_t
808 cpu_start(int cpu)
809 {
810 cpu_data_t *cpu_data_ptr = CpuDataEntries[cpu].cpu_data_vaddr;
811
812 kprintf("cpu_start() cpu: %d\n", cpu);
813
814 if (cpu == cpu_number()) {
815 cpu_machine_init();
816 configure_coresight_registers(cpu_data_ptr);
817 } else {
818 thread_t first_thread;
819 processor_t processor;
820
821 cpu_data_ptr->cpu_reset_handler = (vm_offset_t) start_cpu_paddr;
822
823 #if !XNU_MONITOR
824 cpu_data_ptr->cpu_pmap_cpu_data.cpu_nested_pmap = NULL;
825 #endif
826
827 processor = PERCPU_GET_RELATIVE(processor, cpu_data, cpu_data_ptr);
828 if (processor->startup_thread != THREAD_NULL) {
829 first_thread = processor->startup_thread;
830 } else {
831 first_thread = processor->idle_thread;
832 }
833 cpu_data_ptr->cpu_active_thread = first_thread;
834 first_thread->machine.CpuDatap = cpu_data_ptr;
835 first_thread->machine.pcpu_data_base =
836 (vm_address_t)cpu_data_ptr - __PERCPU_ADDR(cpu_data);
837
838 configure_coresight_registers(cpu_data_ptr);
839
840 flush_dcache((vm_offset_t)&CpuDataEntries[cpu], sizeof(cpu_data_entry_t), FALSE);
841 flush_dcache((vm_offset_t)cpu_data_ptr, sizeof(cpu_data_t), FALSE);
842 #if defined(KERNEL_INTEGRITY_CTRR)
843
844 /* First CPU being started within a cluster goes ahead to lock CTRR for cluster;
845 * other CPUs block until cluster is locked. */
846 lck_spin_lock(&ctrr_cpu_start_lck);
847 switch (ctrr_cluster_locked[cpu_data_ptr->cpu_cluster_id]) {
848 case CTRR_UNLOCKED:
849 ctrr_cluster_locked[cpu_data_ptr->cpu_cluster_id] = CTRR_LOCKING;
850 lck_spin_unlock(&ctrr_cpu_start_lck);
851 break;
852 case CTRR_LOCKING:
853 assert_wait(&ctrr_cluster_locked[cpu_data_ptr->cpu_cluster_id], THREAD_UNINT);
854 lck_spin_unlock(&ctrr_cpu_start_lck);
855 thread_block(THREAD_CONTINUE_NULL);
856 assert(ctrr_cluster_locked[cpu_data_ptr->cpu_cluster_id] != CTRR_LOCKING);
857 break;
858 default: // CTRR_LOCKED
859 lck_spin_unlock(&ctrr_cpu_start_lck);
860 break;
861 }
862 #endif
863 (void) PE_cpu_start(cpu_data_ptr->cpu_id, (vm_offset_t)NULL, (vm_offset_t)NULL);
864 }
865
866 return KERN_SUCCESS;
867 }
868
869
870 void
871 cpu_timebase_init(boolean_t from_boot)
872 {
873 cpu_data_t *cdp = getCpuDatap();
874
875 if (cdp->cpu_get_fiq_handler == NULL) {
876 cdp->cpu_get_fiq_handler = rtclock_timebase_func.tbd_fiq_handler;
877 cdp->cpu_get_decrementer_func = rtclock_timebase_func.tbd_get_decrementer;
878 cdp->cpu_set_decrementer_func = rtclock_timebase_func.tbd_set_decrementer;
879 cdp->cpu_tbd_hardware_addr = (void *)rtclock_timebase_addr;
880 cdp->cpu_tbd_hardware_val = (void *)rtclock_timebase_val;
881 }
882
883 if (!from_boot && (cdp == &BootCpuData)) {
884 /*
885 * When we wake from sleep, we have no guarantee about the state
886 * of the hardware timebase. It may have kept ticking across sleep, or
887 * it may have reset.
888 *
889 * To deal with this, we calculate an offset to the clock that will
890 * produce a timebase value wake_abstime at the point the boot
891 * CPU calls cpu_timebase_init on wake.
892 *
893 * This ensures that mach_absolute_time() stops ticking across sleep.
894 */
895 rtclock_base_abstime = wake_abstime - ml_get_hwclock();
896 } else if (from_boot) {
897 /* On initial boot, initialize time_since_reset to CNTPCT_EL0. */
898 ml_set_reset_time(ml_get_hwclock());
899 }
900
901 cdp->cpu_decrementer = 0x7FFFFFFFUL;
902 cdp->cpu_timebase = 0x0UL;
903 cdp->cpu_base_timebase = rtclock_base_abstime;
904 }
905
906 int
907 cpu_cluster_id(void)
908 {
909 return getCpuDatap()->cpu_cluster_id;
910 }
911
912 __attribute__((noreturn))
913 void
914 ml_arm_sleep(void)
915 {
916 cpu_data_t *cpu_data_ptr = getCpuDatap();
917
918 if (cpu_data_ptr == &BootCpuData) {
919 cpu_data_t *target_cdp;
920 int cpu;
921 int max_cpu;
922
923 max_cpu = ml_get_max_cpu_number();
924 for (cpu = 0; cpu <= max_cpu; cpu++) {
925 target_cdp = (cpu_data_t *)CpuDataEntries[cpu].cpu_data_vaddr;
926
927 if ((target_cdp == NULL) || (target_cdp == cpu_data_ptr)) {
928 continue;
929 }
930
931 while (target_cdp->cpu_sleep_token != ARM_CPU_ON_SLEEP_PATH) {
932 ;
933 }
934 }
935
936 /*
937 * Now that the other cores have entered the sleep path, set
938 * the abstime value we'll use when we resume.
939 */
940 wake_abstime = ml_get_timebase();
941 ml_set_reset_time(UINT64_MAX);
942 } else {
943 CleanPoU_Dcache();
944 }
945
946 cpu_data_ptr->cpu_sleep_token = ARM_CPU_ON_SLEEP_PATH;
947
948 if (cpu_data_ptr == &BootCpuData) {
949 #if WITH_CLASSIC_S2R
950 // Classic suspend to RAM writes the suspend signature into the
951 // sleep token buffer so that iBoot knows that it's on the warm
952 // boot (wake) path (as opposed to the cold boot path). Newer SoC
953 // do not go through SecureROM/iBoot on the warm boot path. The
954 // reconfig engine script brings the CPU out of reset at the kernel's
955 // reset vector which points to the warm boot initialization code.
956 if (sleepTokenBuffer != (vm_offset_t) NULL) {
957 platform_cache_shutdown();
958 bcopy((const void *)suspend_signature, (void *)sleepTokenBuffer, sizeof(SleepToken));
959 } else {
960 panic("No sleep token buffer");
961 }
962 #endif
963
964 #if __ARM_GLOBAL_SLEEP_BIT__
965 /* Allow other CPUs to go to sleep. */
966 arm64_stall_sleep = FALSE;
967 __builtin_arm_dmb(DMB_ISH);
968 #endif
969
970 /* Architectural debug state: <rdar://problem/12390433>:
971 * Grab debug lock EDLAR and clear bit 0 in EDPRCR,
972 * tell debugger to not prevent power gating .
973 */
974 if (cpu_data_ptr->coresight_base[CORESIGHT_ED]) {
975 *(volatile uint32_t *)(cpu_data_ptr->coresight_base[CORESIGHT_ED] + ARM_DEBUG_OFFSET_DBGLAR) = ARM_DBG_LOCK_ACCESS_KEY;
976 *(volatile uint32_t *)(cpu_data_ptr->coresight_base[CORESIGHT_ED] + ARM_DEBUG_OFFSET_DBGPRCR) = 0;
977 }
978
979 #if HIBERNATION
980 uint32_t mode = hibernate_write_image();
981 if (mode == kIOHibernatePostWriteHalt) {
982 HIBLOG("powering off after writing hibernation image\n");
983 int halt_result = -1;
984 if (PE_halt_restart) {
985 halt_result = (*PE_halt_restart)(kPEHaltCPU);
986 }
987 panic("can't shutdown: PE_halt_restart returned %d", halt_result);
988 }
989 #endif /* HIBERNATION */
990
991 #if MONOTONIC
992 mt_sleep();
993 #endif /* MONOTONIC */
994 /* ARM64-specific preparation */
995 arm64_prepare_for_sleep(true);
996 } else {
997 #if __ARM_GLOBAL_SLEEP_BIT__
998 /*
999 * With the exception of the CPU revisions listed above, our ARM64 CPUs have a
1000 * global register to manage entering deep sleep, as opposed to a per-CPU
1001 * register. We cannot update this register until all CPUs are ready to enter
1002 * deep sleep, because if a CPU executes WFI outside of the deep sleep context
1003 * (by idling), it will hang (due to the side effects of enabling deep sleep),
1004 * which can hang the sleep process or cause memory corruption on wake.
1005 *
1006 * To avoid these issues, we'll stall on this global value, which CPU0 will
1007 * manage.
1008 */
1009 while (arm64_stall_sleep) {
1010 __builtin_arm_wfe();
1011 }
1012 #endif
1013 CleanPoU_DcacheRegion((vm_offset_t) cpu_data_ptr, sizeof(cpu_data_t));
1014
1015 /* Architectural debug state: <rdar://problem/12390433>:
1016 * Grab debug lock EDLAR and clear bit 0 in EDPRCR,
1017 * tell debugger to not prevent power gating .
1018 */
1019 if (cpu_data_ptr->coresight_base[CORESIGHT_ED]) {
1020 *(volatile uint32_t *)(cpu_data_ptr->coresight_base[CORESIGHT_ED] + ARM_DEBUG_OFFSET_DBGLAR) = ARM_DBG_LOCK_ACCESS_KEY;
1021 *(volatile uint32_t *)(cpu_data_ptr->coresight_base[CORESIGHT_ED] + ARM_DEBUG_OFFSET_DBGPRCR) = 0;
1022 }
1023
1024 /* ARM64-specific preparation */
1025 arm64_prepare_for_sleep(true);
1026 }
1027 }
1028
1029 void
1030 cpu_machine_idle_init(boolean_t from_boot)
1031 {
1032 static vm_address_t resume_idle_cpu_paddr = (vm_address_t)NULL;
1033 cpu_data_t *cpu_data_ptr = getCpuDatap();
1034
1035 if (from_boot) {
1036 int wfi_tmp = 1;
1037 uint32_t production = 1;
1038 DTEntry entry;
1039
1040 unsigned long jtag = 0;
1041
1042 if (PE_parse_boot_argn("jtag", &jtag, sizeof(jtag))) {
1043 if (jtag != 0) {
1044 idle_enable = FALSE;
1045 } else {
1046 idle_enable = TRUE;
1047 }
1048 } else {
1049 idle_enable = TRUE;
1050 }
1051
1052 #if DEVELOPMENT || DEBUG
1053 uint32_t wfe_mode = 0;
1054 if (PE_parse_boot_argn("wfe_mode", &wfe_mode, sizeof(wfe_mode))) {
1055 idle_proximate_timer_wfe = ((wfe_mode & 1) == 1);
1056 idle_proximate_io_wfe = ((wfe_mode & 2) == 2);
1057 }
1058 #endif
1059 PE_parse_boot_argn("wfi", &wfi_tmp, sizeof(wfi_tmp));
1060
1061 // bits 7..0 give the wfi type
1062 switch (wfi_tmp & 0xff) {
1063 case 0:
1064 // disable wfi
1065 wfi = 0;
1066 break;
1067
1068 #if DEVELOPMENT || DEBUG
1069 case 2:
1070 // wfi overhead simulation
1071 // 31..16 - wfi delay is us
1072 // 15..8 - flags
1073 // 7..0 - 2
1074 wfi = 2;
1075 wfi_flags = (wfi_tmp >> 8) & 0xFF;
1076 nanoseconds_to_absolutetime(((wfi_tmp >> 16) & 0xFFFF) * NSEC_PER_MSEC, &wfi_delay);
1077 break;
1078 #endif /* DEVELOPMENT || DEBUG */
1079
1080 case 1:
1081 default:
1082 // do nothing
1083 break;
1084 }
1085
1086 ResetHandlerData.assist_reset_handler = 0;
1087 ResetHandlerData.cpu_data_entries = ml_static_vtop((vm_offset_t)CpuDataEntries);
1088
1089 #ifdef MONITOR
1090 monitor_call(MONITOR_SET_ENTRY, (uintptr_t)ml_static_vtop((vm_offset_t)&LowResetVectorBase), 0, 0);
1091 #elif !defined(NO_MONITOR)
1092 #error MONITOR undefined, WFI power gating may not operate correctly
1093 #endif /* MONITOR */
1094
1095 // Determine if we are on production or debug chip
1096 if (kSuccess == SecureDTLookupEntry(NULL, "/chosen", &entry)) {
1097 unsigned int size;
1098 void const *prop;
1099
1100 if (kSuccess == SecureDTGetProperty(entry, "effective-production-status-ap", &prop, &size)) {
1101 if (size == 4) {
1102 bcopy(prop, &production, size);
1103 }
1104 }
1105 }
1106 if (!production) {
1107 #if defined(APPLE_ARM64_ARCH_FAMILY)
1108 // Enable coresight debug registers on debug-fused chips
1109 coresight_debug_enabled = TRUE;
1110 #endif
1111 }
1112
1113 start_cpu_paddr = ml_static_vtop((vm_offset_t)&start_cpu);
1114 resume_idle_cpu_paddr = ml_static_vtop((vm_offset_t)&resume_idle_cpu);
1115 }
1116
1117 #if WITH_CLASSIC_S2R
1118 if (cpu_data_ptr == &BootCpuData) {
1119 static addr64_t SleepToken_low_paddr = (addr64_t)NULL;
1120 if (sleepTokenBuffer != (vm_offset_t) NULL) {
1121 SleepToken_low_paddr = ml_vtophys(sleepTokenBuffer);
1122 } else {
1123 panic("No sleep token buffer");
1124 }
1125
1126 bcopy_phys((addr64_t)ml_static_vtop((vm_offset_t)running_signature),
1127 SleepToken_low_paddr, sizeof(SleepToken));
1128 flush_dcache((vm_offset_t)SleepToken, sizeof(SleepToken), TRUE);
1129 }
1130 ;
1131 #endif
1132
1133 cpu_data_ptr->cpu_reset_handler = resume_idle_cpu_paddr;
1134 clean_dcache((vm_offset_t)cpu_data_ptr, sizeof(cpu_data_t), FALSE);
1135 }
1136
1137 _Atomic uint32_t cpu_idle_count = 0;
1138
1139 void
1140 machine_track_platform_idle(boolean_t entry)
1141 {
1142 if (entry) {
1143 os_atomic_inc(&cpu_idle_count, relaxed);
1144 } else {
1145 os_atomic_dec(&cpu_idle_count, relaxed);
1146 }
1147 }
1148
1149 #if WITH_CLASSIC_S2R
1150 void
1151 sleep_token_buffer_init(void)
1152 {
1153 cpu_data_t *cpu_data_ptr = getCpuDatap();
1154 DTEntry entry;
1155 size_t size;
1156 void const * const *prop;
1157
1158 if ((cpu_data_ptr == &BootCpuData) && (sleepTokenBuffer == (vm_offset_t) NULL)) {
1159 /* Find the stpage node in the device tree */
1160 if (kSuccess != SecureDTLookupEntry(0, "stram", &entry)) {
1161 return;
1162 }
1163
1164 if (kSuccess != SecureDTGetProperty(entry, "reg", (const void **)&prop, (unsigned int *)&size)) {
1165 return;
1166 }
1167
1168 /* Map the page into the kernel space */
1169 sleepTokenBuffer = ml_io_map(((vm_offset_t const *)prop)[0], ((vm_size_t const *)prop)[1]);
1170 }
1171 }
1172 #endif