2 * Copyright (c) 2007-2017 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@
29 #include <arm64/proc_reg.h>
30 #include <arm/machine_cpu.h>
31 #include <arm/cpu_internal.h>
32 #include <arm/cpuid.h>
33 #include <arm/io_map_entries.h>
34 #include <arm/cpu_data.h>
35 #include <arm/cpu_data_internal.h>
36 #include <arm/caches_internal.h>
37 #include <arm/misc_protos.h>
38 #include <arm/machdep_call.h>
39 #include <arm/machine_routines.h>
40 #include <arm/rtclock.h>
41 #include <arm/cpuid_internal.h>
42 #include <arm/cpu_capabilities.h>
43 #include <console/serial_protos.h>
44 #include <kern/machine.h>
45 #include <prng/random.h>
46 #include <kern/startup.h>
47 #include <kern/thread.h>
48 #include <kern/timer_queue.h>
49 #include <mach/machine.h>
50 #include <machine/atomic.h>
52 #include <vm/vm_page.h>
53 #include <sys/kdebug.h>
54 #include <kern/coalition.h>
55 #include <pexpert/device_tree.h>
57 #include <IOKit/IOPlatformExpert.h>
59 #if defined(KERNEL_INTEGRITY_KTRR)
60 #include <libkern/kernel_mach_header.h>
63 #include <libkern/section_keywords.h>
71 static int max_cpus_initialized
= 0;
72 #define MAX_CPUS_SET 0x1
73 #define MAX_CPUS_WAIT 0x2
76 uint32_t LockTimeOutUsec
;
77 uint64_t TLockTimeOut
;
79 boolean_t is_clock_configured
= FALSE
;
81 uint32_t yield_delay_us
= 0; /* Must be less than cpu_idle_latency to ensure ml_delay_should_spin is true */
83 #if CONFIG_NONFATAL_ASSERTS
84 extern int mach_assert
;
86 extern volatile uint32_t debug_enabled
;
88 extern vm_offset_t segLOWEST
;
89 extern vm_offset_t segLOWESTTEXT
;
90 extern vm_offset_t segLASTB
;
91 extern unsigned long segSizeLAST
;
94 void machine_conf(void);
96 thread_t
Idle_context(void);
98 SECURITY_READ_ONLY_LATE(static uint32_t) cpu_phys_ids
[MAX_CPUS
] = {[0 ... MAX_CPUS
- 1] = (uint32_t)-1};
99 SECURITY_READ_ONLY_LATE(static unsigned int) avail_cpus
= 0;
100 SECURITY_READ_ONLY_LATE(static int) boot_cpu
= -1;
101 SECURITY_READ_ONLY_LATE(static int) max_cpu_number
= 0;
102 SECURITY_READ_ONLY_LATE(cluster_type_t
) boot_cluster
= CLUSTER_TYPE_SMP
;
104 SECURITY_READ_ONLY_LATE(static uint32_t) fiq_eventi
= UINT32_MAX
;
106 lockdown_handler_t lockdown_handler
;
108 lck_mtx_t lockdown_handler_lck
;
109 lck_grp_t
*lockdown_handler_grp
;
112 void ml_lockdown_init(void);
113 void ml_lockdown_run_handler(void);
114 uint32_t get_arm_cpu_version(void);
119 ml_cpu_signal(unsigned int cpu_mpidr __unused
)
121 panic("Platform does not support ACC Fast IPI");
126 ml_cpu_signal_deferred_adjust_timer(uint64_t nanosecs
)
129 panic("Platform does not support ACC Fast IPI");
133 ml_cpu_signal_deferred_get_timer()
140 ml_cpu_signal_deferred(unsigned int cpu_mpidr __unused
)
142 panic("Platform does not support ACC Fast IPI deferral");
147 ml_cpu_signal_retract(unsigned int cpu_mpidr __unused
)
149 panic("Platform does not support ACC Fast IPI retraction");
155 __builtin_arm_wsr("DAIFSet", (DAIFSC_IRQF
| DAIFSC_FIQF
));
157 __builtin_arm_wsr("DAIFClr", (DAIFSC_IRQF
| DAIFSC_FIQF
));
167 get_vfp_enabled(void)
173 OSSynchronizeIO(void)
175 __builtin_arm_dsb(DSB_SY
);
179 get_aux_control(void)
183 MRS(value
, "ACTLR_EL1");
188 get_mmu_control(void)
192 MRS(value
, "SCTLR_EL1");
201 MRS(value
, "TCR_EL1");
206 ml_get_interrupts_enabled(void)
211 if (value
& DAIF_IRQF
) {
222 MRS(value
, "TTBR0_EL1");
227 get_arm_cpu_version(void)
229 uint32_t value
= machine_read_midr();
231 /* Compose the register values into 8 bits; variant[7:4], revision[3:0]. */
232 return ((value
& MIDR_EL1_REV_MASK
) >> MIDR_EL1_REV_SHIFT
) | ((value
& MIDR_EL1_VAR_MASK
) >> (MIDR_EL1_VAR_SHIFT
- 4));
236 * user_cont_hwclock_allowed()
238 * Indicates whether we allow EL0 to read the physical timebase (CNTPCT_EL0)
239 * as a continuous time source (e.g. from mach_continuous_time)
242 user_cont_hwclock_allowed(void)
249 user_timebase_type(void)
251 return USER_TIMEBASE_SPEC
;
255 arm64_wfe_allowed(void)
260 #if defined(KERNEL_INTEGRITY_KTRR)
262 uint64_t rorgn_begin
__attribute__((section("__DATA, __const"))) = 0;
263 uint64_t rorgn_end
__attribute__((section("__DATA, __const"))) = 0;
264 vm_offset_t amcc_base
;
266 static void assert_unlocked(void);
267 static void assert_amcc_cache_disabled(void);
268 static void lock_amcc(void);
269 static void lock_mmu(uint64_t begin
, uint64_t end
);
272 rorgn_stash_range(void)
274 #if DEVELOPMENT || DEBUG
275 boolean_t rorgn_disable
= FALSE
;
277 PE_parse_boot_argn("-unsafe_kernel_text", &rorgn_disable
, sizeof(rorgn_disable
));
280 /* take early out if boot arg present, don't query any machine registers to avoid
281 * dependency on amcc DT entry
287 /* Get the AMC values, and stash them into rorgn_begin, rorgn_end.
288 * gPhysBase is the base of DRAM managed by xnu. we need DRAM_BASE as
289 * the AMCC RO region begin/end registers are in units of 16KB page
290 * numbers from DRAM_BASE so we'll truncate gPhysBase at 512MB granule
291 * and assert the value is the canonical DRAM_BASE PA of 0x8_0000_0000 for arm64.
294 uint64_t dram_base
= gPhysBase
& ~0x1FFFFFFFULL
; /* 512MB */
295 assert(dram_base
== 0x800000000ULL
);
297 #if defined(KERNEL_INTEGRITY_KTRR)
298 uint64_t soc_base
= 0;
299 DTEntry entryP
= NULL
;
300 uintptr_t *reg_prop
= NULL
;
301 uint32_t prop_size
= 0;
304 soc_base
= pe_arm_get_soc_base_phys();
305 rc
= DTFindEntry("name", "mcc", &entryP
);
306 assert(rc
== kSuccess
);
307 rc
= DTGetProperty(entryP
, "reg", (void **)®_prop
, &prop_size
);
308 assert(rc
== kSuccess
);
309 amcc_base
= ml_io_map(soc_base
+ *reg_prop
, *(reg_prop
+ 1));
311 #error "KERNEL_INTEGRITY config error"
314 #if defined(KERNEL_INTEGRITY_KTRR)
315 assert(rRORGNENDADDR
> rRORGNBASEADDR
);
316 rorgn_begin
= (rRORGNBASEADDR
<< AMCC_PGSHIFT
) + dram_base
;
317 rorgn_end
= (rRORGNENDADDR
<< AMCC_PGSHIFT
) + dram_base
;
319 #error KERNEL_INTEGRITY config error
320 #endif /* defined (KERNEL_INTEGRITY_KTRR) */
326 uint64_t ktrr_lock
= 0;
327 uint32_t rorgn_lock
= 0;
330 #if defined(KERNEL_INTEGRITY_KTRR)
331 rorgn_lock
= rRORGNLOCK
;
332 ktrr_lock
= __builtin_arm_rsr64(ARM64_REG_KTRR_LOCK_EL1
);
334 #error KERNEL_INTEGRITY config error
335 #endif /* defined(KERNEL_INTEGRITY_KTRR) */
344 #if defined(KERNEL_INTEGRITY_KTRR)
346 __builtin_arm_isb(ISB_SY
);
348 #error KERNEL_INTEGRITY config error
353 lock_mmu(uint64_t begin
, uint64_t end
)
355 #if defined(KERNEL_INTEGRITY_KTRR)
357 __builtin_arm_wsr64(ARM64_REG_KTRR_LOWER_EL1
, begin
);
358 __builtin_arm_wsr64(ARM64_REG_KTRR_UPPER_EL1
, end
);
359 __builtin_arm_wsr64(ARM64_REG_KTRR_LOCK_EL1
, 1ULL);
363 __builtin_arm_isb(ISB_SY
);
366 #else /* defined(KERNEL_INTEGRITY_KTRR) */
367 #error KERNEL_INTEGRITY config error
368 #endif /* defined(KERNEL_INTEGRITY_KTRR) */
372 assert_amcc_cache_disabled()
374 #if defined(KERNEL_INTEGRITY_KTRR)
375 assert((rMCCGEN
& 1) == 0); /* assert M$ disabled or LLC clean will be unreliable */
377 #error KERNEL_INTEGRITY config error
382 * void rorgn_lockdown(void)
384 * Lock the MMU and AMCC RORegion within lower and upper boundaries if not already locked
386 * [ ] - ensure this is being called ASAP on secondary CPUs: KTRR programming and lockdown handled in
387 * start.s:start_cpu() for subsequent wake/resume of all cores
392 vm_offset_t ktrr_begin
, ktrr_end
;
393 unsigned long last_segsz
;
395 #if DEVELOPMENT || DEBUG
396 boolean_t ktrr_disable
= FALSE
;
398 PE_parse_boot_argn("-unsafe_kernel_text", &ktrr_disable
, sizeof(ktrr_disable
));
402 * take early out if boot arg present, since we may not have amcc DT entry present
403 * we can't assert that iboot hasn't programmed the RO region lockdown registers
407 #endif /* DEVELOPMENT || DEBUG */
411 /* [x] - Use final method of determining all kernel text range or expect crashes */
412 ktrr_begin
= segLOWEST
;
413 assert(ktrr_begin
&& gVirtBase
&& gPhysBase
);
415 ktrr_begin
= kvtophys(ktrr_begin
);
417 ktrr_end
= kvtophys(segLASTB
);
418 last_segsz
= segSizeLAST
;
419 #if defined(KERNEL_INTEGRITY_KTRR)
420 /* __LAST is not part of the MMU KTRR region (it is however part of the AMCC KTRR region) */
421 ktrr_end
= (ktrr_end
- 1) & ~AMCC_PGMASK
;
422 /* ensure that iboot and xnu agree on the ktrr range */
423 assert(rorgn_begin
== ktrr_begin
&& rorgn_end
== (ktrr_end
+ last_segsz
));
424 /* assert that __LAST segment containing privileged insns is only a single page */
425 assert(last_segsz
== PAGE_SIZE
);
429 #if DEBUG || DEVELOPMENT
430 printf("KTRR Begin: %p End: %p, setting lockdown\n", (void *)ktrr_begin
, (void *)ktrr_end
);
433 /* [x] - ensure all in flight writes are flushed to AMCC before enabling RO Region Lock */
435 assert_amcc_cache_disabled();
437 CleanPoC_DcacheRegion_Force(phystokv(ktrr_begin
),
438 (unsigned)((ktrr_end
+ last_segsz
) - ktrr_begin
+ AMCC_PGMASK
));
442 lock_mmu(ktrr_begin
, ktrr_end
);
444 #if DEVELOPMENT || DEBUG
448 /* now we can run lockdown handler */
449 ml_lockdown_run_handler();
452 #endif /* defined(KERNEL_INTEGRITY_KTRR)*/
455 machine_startup(__unused boot_args
* args
)
460 #if CONFIG_NONFATAL_ASSERTS
461 PE_parse_boot_argn("assert", &mach_assert
, sizeof(mach_assert
));
464 if (PE_parse_boot_argn("preempt", &boot_arg
, sizeof(boot_arg
))) {
465 default_preemption_rate
= boot_arg
;
467 if (PE_parse_boot_argn("bg_preempt", &boot_arg
, sizeof(boot_arg
))) {
468 default_bg_preemption_rate
= boot_arg
;
471 PE_parse_boot_argn("yield_delay_us", &yield_delay_us
, sizeof(yield_delay_us
));
476 * Kick off the kernel bootstrap.
483 machine_lockdown_preflight(void)
485 #if CONFIG_KERNEL_INTEGRITY
487 #if defined(KERNEL_INTEGRITY_KTRR)
495 machine_lockdown(void)
497 #if CONFIG_KERNEL_INTEGRITY
498 #if KERNEL_INTEGRITY_WT
501 * Notify the monitor about the completion of early kernel bootstrap.
502 * From this point forward it will enforce the integrity of kernel text,
503 * rodata and page tables.
507 monitor_call(MONITOR_LOCKDOWN
, 0, 0, 0);
509 #endif /* KERNEL_INTEGRITY_WT */
512 #if defined(KERNEL_INTEGRITY_KTRR)
515 * Lock physical KTRR region. KTRR region is read-only. Memory outside
516 * the region is not executable at EL1.
520 #endif /* defined(KERNEL_INTEGRITY_KTRR)*/
523 #endif /* CONFIG_KERNEL_INTEGRITY */
529 __unused vm_size_t size
)
531 return PE_boot_args();
538 * This is known to be inaccurate. mem_size should always be capped at 2 GB
540 machine_info
.memory_size
= (uint32_t)mem_size
;
548 is_clock_configured
= TRUE
;
555 slave_machine_init(__unused
void *param
)
557 cpu_machine_init(); /* Initialize the processor */
558 clock_init(); /* Init the clock */
562 * Routine: machine_processor_shutdown
566 machine_processor_shutdown(
567 __unused thread_t thread
,
568 void (*doshutdown
)(processor_t
),
569 processor_t processor
)
571 return Shutdown_context(doshutdown
, processor
);
575 * Routine: ml_init_max_cpus
579 ml_init_max_cpus(unsigned int max_cpus
)
581 boolean_t current_state
;
583 current_state
= ml_set_interrupts_enabled(FALSE
);
584 if (max_cpus_initialized
!= MAX_CPUS_SET
) {
585 machine_info
.max_cpus
= max_cpus
;
586 machine_info
.physical_cpu_max
= max_cpus
;
587 machine_info
.logical_cpu_max
= max_cpus
;
588 if (max_cpus_initialized
== MAX_CPUS_WAIT
) {
589 thread_wakeup((event_t
) &max_cpus_initialized
);
591 max_cpus_initialized
= MAX_CPUS_SET
;
593 (void) ml_set_interrupts_enabled(current_state
);
597 * Routine: ml_get_max_cpus
601 ml_get_max_cpus(void)
603 boolean_t current_state
;
605 current_state
= ml_set_interrupts_enabled(FALSE
);
606 if (max_cpus_initialized
!= MAX_CPUS_SET
) {
607 max_cpus_initialized
= MAX_CPUS_WAIT
;
608 assert_wait((event_t
) &max_cpus_initialized
, THREAD_UNINT
);
609 (void) thread_block(THREAD_CONTINUE_NULL
);
611 (void) ml_set_interrupts_enabled(current_state
);
612 return machine_info
.max_cpus
;
616 * Routine: ml_init_lock_timeout
620 ml_init_lock_timeout(void)
624 uint64_t default_timeout_ns
= NSEC_PER_SEC
>> 2;
627 if (PE_parse_boot_argn("slto_us", &slto
, sizeof(slto
))) {
628 default_timeout_ns
= slto
* NSEC_PER_USEC
;
631 nanoseconds_to_absolutetime(default_timeout_ns
, &abstime
);
632 LockTimeOutUsec
= (uint32_t) (default_timeout_ns
/ NSEC_PER_USEC
);
633 LockTimeOut
= (uint32_t)abstime
;
635 if (PE_parse_boot_argn("tlto_us", &slto
, sizeof(slto
))) {
636 nanoseconds_to_absolutetime(slto
* NSEC_PER_USEC
, &abstime
);
637 TLockTimeOut
= abstime
;
639 TLockTimeOut
= LockTimeOut
>> 1;
642 if (PE_parse_boot_argn("mtxspin", &mtxspin
, sizeof(mtxspin
))) {
643 if (mtxspin
> USEC_PER_SEC
>> 4) {
644 mtxspin
= USEC_PER_SEC
>> 4;
646 nanoseconds_to_absolutetime(mtxspin
* NSEC_PER_USEC
, &abstime
);
648 nanoseconds_to_absolutetime(10 * NSEC_PER_USEC
, &abstime
);
654 * This is called from the machine-independent routine cpu_up()
655 * to perform machine-dependent info updates.
660 os_atomic_inc(&machine_info
.physical_cpu
, relaxed
);
661 os_atomic_inc(&machine_info
.logical_cpu
, relaxed
);
665 * This is called from the machine-independent routine cpu_down()
666 * to perform machine-dependent info updates.
671 cpu_data_t
*cpu_data_ptr
;
673 os_atomic_dec(&machine_info
.physical_cpu
, relaxed
);
674 os_atomic_dec(&machine_info
.logical_cpu
, relaxed
);
677 * If we want to deal with outstanding IPIs, we need to
678 * do relatively early in the processor_doshutdown path,
679 * as we pend decrementer interrupts using the IPI
680 * mechanism if we cannot immediately service them (if
681 * IRQ is masked). Do so now.
683 * We aren't on the interrupt stack here; would it make
684 * more sense to disable signaling and then enable
685 * interrupts? It might be a bit cleaner.
687 cpu_data_ptr
= getCpuDatap();
688 cpu_data_ptr
->cpu_running
= FALSE
;
690 if (cpu_data_ptr
!= &BootCpuData
) {
692 * Move all of this cpu's timers to the master/boot cpu,
693 * and poke it in case there's a sooner deadline for it to schedule.
695 timer_queue_shutdown(&cpu_data_ptr
->rtclock_timer
.queue
);
696 cpu_xcall(BootCpuData
.cpu_number
, &timer_queue_expire_local
, NULL
);
699 cpu_signal_handler_internal(TRUE
);
703 * Routine: ml_cpu_get_info
707 ml_cpu_get_info(ml_cpu_info_t
* ml_cpu_info
)
709 cache_info_t
*cpuid_cache_info
;
711 cpuid_cache_info
= cache_info();
712 ml_cpu_info
->vector_unit
= 0;
713 ml_cpu_info
->cache_line_size
= cpuid_cache_info
->c_linesz
;
714 ml_cpu_info
->l1_icache_size
= cpuid_cache_info
->c_isize
;
715 ml_cpu_info
->l1_dcache_size
= cpuid_cache_info
->c_dsize
;
717 #if (__ARM_ARCH__ >= 7)
718 ml_cpu_info
->l2_settings
= 1;
719 ml_cpu_info
->l2_cache_size
= cpuid_cache_info
->c_l2size
;
721 ml_cpu_info
->l2_settings
= 0;
722 ml_cpu_info
->l2_cache_size
= 0xFFFFFFFF;
724 ml_cpu_info
->l3_settings
= 0;
725 ml_cpu_info
->l3_cache_size
= 0xFFFFFFFF;
729 ml_get_machine_mem(void)
731 return machine_info
.memory_size
;
734 __attribute__((noreturn
))
736 halt_all_cpus(boolean_t reboot
)
739 printf("MACH Reboot\n");
740 PEHaltRestart(kPERestartCPU
);
742 printf("CPU halted\n");
743 PEHaltRestart(kPEHaltCPU
);
750 __attribute__((noreturn
))
754 halt_all_cpus(FALSE
);
758 * Routine: machine_signal_idle
763 processor_t processor
)
765 cpu_signal(processor_to_cpu_datap(processor
), SIGPnop
, (void *)NULL
, (void *)NULL
);
766 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_REMOTE_AST
), processor
->cpu_id
, 0 /* nop */, 0, 0, 0);
770 machine_signal_idle_deferred(
771 processor_t processor
)
773 cpu_signal_deferred(processor_to_cpu_datap(processor
));
774 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_REMOTE_DEFERRED_AST
), processor
->cpu_id
, 0 /* nop */, 0, 0, 0);
778 machine_signal_idle_cancel(
779 processor_t processor
)
781 cpu_signal_cancel(processor_to_cpu_datap(processor
));
782 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_REMOTE_CANCEL_AST
), processor
->cpu_id
, 0 /* nop */, 0, 0, 0);
786 * Routine: ml_install_interrupt_handler
787 * Function: Initialize Interrupt Handler
790 ml_install_interrupt_handler(
794 IOInterruptHandler handler
,
797 cpu_data_t
*cpu_data_ptr
;
798 boolean_t current_state
;
800 current_state
= ml_set_interrupts_enabled(FALSE
);
801 cpu_data_ptr
= getCpuDatap();
803 cpu_data_ptr
->interrupt_nub
= nub
;
804 cpu_data_ptr
->interrupt_source
= source
;
805 cpu_data_ptr
->interrupt_target
= target
;
806 cpu_data_ptr
->interrupt_handler
= handler
;
807 cpu_data_ptr
->interrupt_refCon
= refCon
;
809 cpu_data_ptr
->interrupts_enabled
= TRUE
;
810 (void) ml_set_interrupts_enabled(current_state
);
812 initialize_screen(NULL
, kPEAcquireScreen
);
816 * Routine: ml_init_interrupt
817 * Function: Initialize Interrupts
820 ml_init_interrupt(void)
825 * Routine: ml_init_timebase
826 * Function: register and setup Timebase, Decremeter services
832 vm_offset_t int_address
,
833 vm_offset_t int_value __unused
)
835 cpu_data_t
*cpu_data_ptr
;
837 cpu_data_ptr
= (cpu_data_t
*)args
;
839 if ((cpu_data_ptr
== &BootCpuData
)
840 && (rtclock_timebase_func
.tbd_fiq_handler
== (void *)NULL
)) {
841 rtclock_timebase_func
= *tbd_funcs
;
842 rtclock_timebase_addr
= int_address
;
847 ml_parse_cpu_topology(void)
849 DTEntry entry
, child __unused
;
850 OpaqueDTEntryIterator iter
;
851 uint32_t cpu_boot_arg
;
854 cpu_boot_arg
= MAX_CPUS
;
856 PE_parse_boot_argn("cpus", &cpu_boot_arg
, sizeof(cpu_boot_arg
));
858 err
= DTLookupEntry(NULL
, "/cpus", &entry
);
859 assert(err
== kSuccess
);
861 err
= DTInitEntryIterator(entry
, &iter
);
862 assert(err
== kSuccess
);
864 while (kSuccess
== DTIterateEntries(&iter
, &child
)) {
865 unsigned int propSize
;
867 int cpu_id
= avail_cpus
++;
869 if (kSuccess
== DTGetProperty(child
, "cpu-id", &prop
, &propSize
)) {
870 cpu_id
= *((int32_t*)prop
);
873 assert(cpu_id
< MAX_CPUS
);
874 assert(cpu_phys_ids
[cpu_id
] == (uint32_t)-1);
876 if (boot_cpu
== -1) {
877 if (kSuccess
!= DTGetProperty(child
, "state", &prop
, &propSize
)) {
878 panic("unable to retrieve state for cpu %d", cpu_id
);
881 if (strncmp((char*)prop
, "running", propSize
) == 0) {
885 if (kSuccess
!= DTGetProperty(child
, "reg", &prop
, &propSize
)) {
886 panic("unable to retrieve physical ID for cpu %d", cpu_id
);
889 cpu_phys_ids
[cpu_id
] = *((uint32_t*)prop
);
891 if ((cpu_id
> max_cpu_number
) && ((cpu_id
== boot_cpu
) || (avail_cpus
<= cpu_boot_arg
))) {
892 max_cpu_number
= cpu_id
;
896 if (avail_cpus
> cpu_boot_arg
) {
897 avail_cpus
= cpu_boot_arg
;
900 if (avail_cpus
== 0) {
901 panic("No cpus found!");
904 if (boot_cpu
== -1) {
905 panic("unable to determine boot cpu!");
909 * Set TPIDRRO_EL0 to indicate the correct cpu number, as we may
910 * not be booting from cpu 0. Userspace will consume the current
911 * CPU number through this register. For non-boot cores, this is
912 * done in start.s (start_cpu) using the cpu_number field of the
913 * per-cpu data object.
915 assert(__builtin_arm_rsr64("TPIDRRO_EL0") == 0);
916 __builtin_arm_wsr64("TPIDRRO_EL0", (uint64_t)boot_cpu
);
920 ml_get_cpu_count(void)
926 ml_get_boot_cpu_number(void)
932 ml_get_boot_cluster(void)
938 ml_get_cpu_number(uint32_t phys_id
)
940 for (int log_id
= 0; log_id
<= ml_get_max_cpu_number(); ++log_id
) {
941 if (cpu_phys_ids
[log_id
] == phys_id
) {
949 ml_get_max_cpu_number(void)
951 return max_cpu_number
;
958 lockdown_handler_grp
= lck_grp_alloc_init("lockdown_handler", NULL
);
959 assert(lockdown_handler_grp
!= NULL
);
961 lck_mtx_init(&lockdown_handler_lck
, lockdown_handler_grp
, NULL
);
966 ml_lockdown_handler_register(lockdown_handler_t f
, void *this)
968 if (lockdown_handler
|| !f
) {
972 lck_mtx_lock(&lockdown_handler_lck
);
973 lockdown_handler
= f
;
974 lockdown_this
= this;
976 #if !(defined(KERNEL_INTEGRITY_KTRR))
978 lockdown_handler(this);
981 lockdown_handler(this);
984 lck_mtx_unlock(&lockdown_handler_lck
);
990 ml_lockdown_run_handler()
992 lck_mtx_lock(&lockdown_handler_lck
);
993 assert(!lockdown_done
);
996 if (lockdown_handler
) {
997 lockdown_handler(lockdown_this
);
999 lck_mtx_unlock(&lockdown_handler_lck
);
1003 ml_processor_register(ml_processor_info_t
*in_processor_info
,
1004 processor_t
*processor_out
, ipi_handler_t
*ipi_handler_out
,
1005 perfmon_interrupt_handler_func
*pmi_handler_out
)
1007 cpu_data_t
*this_cpu_datap
;
1008 processor_set_t pset
;
1009 boolean_t is_boot_cpu
;
1010 static unsigned int reg_cpu_count
= 0;
1012 if (in_processor_info
->log_id
> (uint32_t)ml_get_max_cpu_number()) {
1013 return KERN_FAILURE
;
1016 if ((unsigned int)OSIncrementAtomic((SInt32
*)®_cpu_count
) >= avail_cpus
) {
1017 return KERN_FAILURE
;
1020 if (in_processor_info
->log_id
!= (uint32_t)ml_get_boot_cpu_number()) {
1021 is_boot_cpu
= FALSE
;
1022 this_cpu_datap
= cpu_data_alloc(FALSE
);
1023 cpu_data_init(this_cpu_datap
);
1025 this_cpu_datap
= &BootCpuData
;
1029 assert(in_processor_info
->log_id
< MAX_CPUS
);
1031 this_cpu_datap
->cpu_id
= in_processor_info
->cpu_id
;
1033 this_cpu_datap
->cpu_console_buf
= console_cpu_alloc(is_boot_cpu
);
1034 if (this_cpu_datap
->cpu_console_buf
== (void *)(NULL
)) {
1035 goto processor_register_error
;
1039 this_cpu_datap
->cpu_number
= in_processor_info
->log_id
;
1041 if (cpu_data_register(this_cpu_datap
) != KERN_SUCCESS
) {
1042 goto processor_register_error
;
1046 this_cpu_datap
->cpu_idle_notify
= (void *) in_processor_info
->processor_idle
;
1047 this_cpu_datap
->cpu_cache_dispatch
= in_processor_info
->platform_cache_dispatch
;
1048 nanoseconds_to_absolutetime((uint64_t) in_processor_info
->powergate_latency
, &this_cpu_datap
->cpu_idle_latency
);
1049 this_cpu_datap
->cpu_reset_assist
= kvtophys(in_processor_info
->powergate_stub_addr
);
1051 this_cpu_datap
->idle_timer_notify
= (void *) in_processor_info
->idle_timer
;
1052 this_cpu_datap
->idle_timer_refcon
= in_processor_info
->idle_timer_refcon
;
1054 this_cpu_datap
->platform_error_handler
= (void *) in_processor_info
->platform_error_handler
;
1055 this_cpu_datap
->cpu_regmap_paddr
= in_processor_info
->regmap_paddr
;
1056 this_cpu_datap
->cpu_phys_id
= in_processor_info
->phys_id
;
1057 this_cpu_datap
->cpu_l2_access_penalty
= in_processor_info
->l2_access_penalty
;
1059 this_cpu_datap
->cpu_cluster_type
= in_processor_info
->cluster_type
;
1060 this_cpu_datap
->cpu_cluster_id
= in_processor_info
->cluster_id
;
1061 this_cpu_datap
->cpu_l2_id
= in_processor_info
->l2_cache_id
;
1062 this_cpu_datap
->cpu_l2_size
= in_processor_info
->l2_cache_size
;
1063 this_cpu_datap
->cpu_l3_id
= in_processor_info
->l3_cache_id
;
1064 this_cpu_datap
->cpu_l3_size
= in_processor_info
->l3_cache_size
;
1066 this_cpu_datap
->cluster_master
= is_boot_cpu
;
1068 pset
= pset_find(in_processor_info
->cluster_id
, processor_pset(master_processor
));
1069 assert(pset
!= NULL
);
1070 kprintf("%s>cpu_id %p cluster_id %d cpu_number %d is type %d\n", __FUNCTION__
, in_processor_info
->cpu_id
, in_processor_info
->cluster_id
, this_cpu_datap
->cpu_number
, in_processor_info
->cluster_type
);
1073 processor_init((struct processor
*)this_cpu_datap
->cpu_processor
,
1074 this_cpu_datap
->cpu_number
, pset
);
1076 if (this_cpu_datap
->cpu_l2_access_penalty
) {
1078 * Cores that have a non-zero L2 access penalty compared
1079 * to the boot processor should be de-prioritized by the
1080 * scheduler, so that threads use the cores with better L2
1083 processor_set_primary(this_cpu_datap
->cpu_processor
,
1088 *processor_out
= this_cpu_datap
->cpu_processor
;
1089 *ipi_handler_out
= cpu_signal_handler
;
1090 #if CPMU_AIC_PMI && MONOTONIC
1091 *pmi_handler_out
= mt_cpmu_aic_pmi
;
1093 *pmi_handler_out
= NULL
;
1094 #endif /* CPMU_AIC_PMI && MONOTONIC */
1095 if (in_processor_info
->idle_tickle
!= (idle_tickle_t
*) NULL
) {
1096 *in_processor_info
->idle_tickle
= (idle_tickle_t
) cpu_idle_tickle
;
1100 if (kpc_register_cpu(this_cpu_datap
) != TRUE
) {
1101 goto processor_register_error
;
1106 random_cpu_init(this_cpu_datap
->cpu_number
);
1107 // now let next CPU register itself
1108 OSIncrementAtomic((SInt32
*)&real_ncpus
);
1111 return KERN_SUCCESS
;
1113 processor_register_error
:
1115 kpc_unregister_cpu(this_cpu_datap
);
1118 cpu_data_free(this_cpu_datap
);
1121 return KERN_FAILURE
;
1125 ml_init_arm_debug_interface(
1126 void * in_cpu_datap
,
1127 vm_offset_t virt_address
)
1129 ((cpu_data_t
*)in_cpu_datap
)->cpu_debug_interface_map
= virt_address
;
1134 * Routine: init_ast_check
1139 __unused processor_t processor
)
1144 * Routine: cause_ast_check
1149 processor_t processor
)
1151 if (current_processor() != processor
) {
1152 cpu_signal(processor_to_cpu_datap(processor
), SIGPast
, (void *)NULL
, (void *)NULL
);
1153 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_REMOTE_AST
), processor
->cpu_id
, 1 /* ast */, 0, 0, 0);
1157 extern uint32_t cpu_idle_count
;
1160 ml_get_power_state(boolean_t
*icp
, boolean_t
*pidlep
)
1162 *icp
= ml_at_interrupt_context();
1163 *pidlep
= (cpu_idle_count
== real_ncpus
);
1167 * Routine: ml_cause_interrupt
1168 * Function: Generate a fake interrupt
1171 ml_cause_interrupt(void)
1173 return; /* BS_XXX */
1176 /* Map memory map IO space */
1179 vm_offset_t phys_addr
,
1182 return io_map(phys_addr
, size
, VM_WIMG_IO
);
1185 /* Map memory map IO space (with protections specified) */
1187 ml_io_map_with_prot(
1188 vm_offset_t phys_addr
,
1192 return io_map_with_prot(phys_addr
, size
, VM_WIMG_IO
, prot
);
1197 vm_offset_t phys_addr
,
1200 return io_map(phys_addr
, size
, VM_WIMG_WCOMB
);
1203 /* boot memory allocation */
1206 __unused vm_size_t size
)
1208 return (vm_offset_t
) NULL
;
1213 vm_offset_t phys_addr
,
1216 return pmap_map_high_window_bd(phys_addr
, len
, VM_PROT_READ
| VM_PROT_WRITE
);
1223 return phystokv(paddr
);
1230 return phystokv(vaddr
+ vm_kernel_slide
- gVirtBase
+ gPhysBase
);
1237 return ml_static_vtop(vaddr
) - gPhysBase
+ gVirtBase
- vm_kernel_slide
;
1240 extern tt_entry_t
*arm_kva_to_tte(vm_offset_t va
);
1244 vm_offset_t vaddr
, /* kernel virtual address */
1248 pt_entry_t arm_prot
= 0;
1249 pt_entry_t arm_block_prot
= 0;
1250 vm_offset_t vaddr_cur
;
1252 kern_return_t result
= KERN_SUCCESS
;
1254 if (vaddr
< VM_MIN_KERNEL_ADDRESS
) {
1255 panic("ml_static_protect(): %p < %p", (void *) vaddr
, (void *) VM_MIN_KERNEL_ADDRESS
);
1256 return KERN_FAILURE
;
1259 assert((vaddr
& (PAGE_SIZE
- 1)) == 0); /* must be page aligned */
1261 if ((new_prot
& VM_PROT_WRITE
) && (new_prot
& VM_PROT_EXECUTE
)) {
1262 panic("ml_static_protect(): WX request on %p", (void *) vaddr
);
1265 /* Set up the protection bits, and block bits so we can validate block mappings. */
1266 if (new_prot
& VM_PROT_WRITE
) {
1267 arm_prot
|= ARM_PTE_AP(AP_RWNA
);
1268 arm_block_prot
|= ARM_TTE_BLOCK_AP(AP_RWNA
);
1270 arm_prot
|= ARM_PTE_AP(AP_RONA
);
1271 arm_block_prot
|= ARM_TTE_BLOCK_AP(AP_RONA
);
1274 arm_prot
|= ARM_PTE_NX
;
1275 arm_block_prot
|= ARM_TTE_BLOCK_NX
;
1277 if (!(new_prot
& VM_PROT_EXECUTE
)) {
1278 arm_prot
|= ARM_PTE_PNX
;
1279 arm_block_prot
|= ARM_TTE_BLOCK_PNX
;
1282 for (vaddr_cur
= vaddr
;
1283 vaddr_cur
< trunc_page_64(vaddr
+ size
);
1284 vaddr_cur
+= PAGE_SIZE
) {
1285 ppn
= pmap_find_phys(kernel_pmap
, vaddr_cur
);
1286 if (ppn
!= (vm_offset_t
) NULL
) {
1292 tte2
= arm_kva_to_tte(vaddr_cur
);
1294 if (((*tte2
) & ARM_TTE_TYPE_MASK
) != ARM_TTE_TYPE_TABLE
) {
1295 if ((((*tte2
) & ARM_TTE_TYPE_MASK
) == ARM_TTE_TYPE_BLOCK
) &&
1296 ((*tte2
& (ARM_TTE_BLOCK_NXMASK
| ARM_TTE_BLOCK_PNXMASK
| ARM_TTE_BLOCK_APMASK
)) == arm_block_prot
)) {
1298 * We can support ml_static_protect on a block mapping if the mapping already has
1299 * the desired protections. We still want to run checks on a per-page basis.
1304 result
= KERN_FAILURE
;
1308 pte_p
= (pt_entry_t
*)&((tt_entry_t
*)(phystokv((*tte2
) & ARM_TTE_TABLE_MASK
)))[(((vaddr_cur
) & ARM_TT_L3_INDEX_MASK
) >> ARM_TT_L3_SHIFT
)];
1311 if ((ptmp
& ARM_PTE_HINT_MASK
) && ((ptmp
& (ARM_PTE_APMASK
| ARM_PTE_PNXMASK
| ARM_PTE_NXMASK
)) != arm_prot
)) {
1313 * The contiguous hint is similar to a block mapping for ml_static_protect; if the existing
1314 * protections do not match the desired protections, then we will fail (as we cannot update
1315 * this mapping without updating other mappings as well).
1317 result
= KERN_FAILURE
;
1321 __unreachable_ok_push
1322 if (TEST_PAGE_RATIO_4
) {
1325 pt_entry_t
*ptep_iter
;
1328 for (i
= 0; i
< 4; i
++, ptep_iter
++) {
1329 /* Note that there is a hole in the HINT sanity checking here. */
1332 /* We only need to update the page tables if the protections do not match. */
1333 if ((ptmp
& (ARM_PTE_APMASK
| ARM_PTE_PNXMASK
| ARM_PTE_NXMASK
)) != arm_prot
) {
1334 ptmp
= (ptmp
& ~(ARM_PTE_APMASK
| ARM_PTE_PNXMASK
| ARM_PTE_NXMASK
)) | arm_prot
;
1342 /* We only need to update the page tables if the protections do not match. */
1343 if ((ptmp
& (ARM_PTE_APMASK
| ARM_PTE_PNXMASK
| ARM_PTE_NXMASK
)) != arm_prot
) {
1344 ptmp
= (ptmp
& ~(ARM_PTE_APMASK
| ARM_PTE_PNXMASK
| ARM_PTE_NXMASK
)) | arm_prot
;
1348 __unreachable_ok_pop
1352 if (vaddr_cur
> vaddr
) {
1353 assert(((vaddr_cur
- vaddr
) & 0xFFFFFFFF00000000ULL
) == 0);
1354 flush_mmu_tlb_region(vaddr
, (uint32_t)(vaddr_cur
- vaddr
));
1362 * Routine: ml_static_mfree
1370 vm_offset_t vaddr_cur
;
1372 uint32_t freed_pages
= 0;
1374 /* It is acceptable (if bad) to fail to free. */
1375 if (vaddr
< VM_MIN_KERNEL_ADDRESS
) {
1379 assert((vaddr
& (PAGE_SIZE
- 1)) == 0); /* must be page aligned */
1381 for (vaddr_cur
= vaddr
;
1382 vaddr_cur
< trunc_page_64(vaddr
+ size
);
1383 vaddr_cur
+= PAGE_SIZE
) {
1384 ppn
= pmap_find_phys(kernel_pmap
, vaddr_cur
);
1385 if (ppn
!= (vm_offset_t
) NULL
) {
1387 * It is not acceptable to fail to update the protections on a page
1388 * we will release to the VM. We need to either panic or continue.
1389 * For now, we'll panic (to help flag if there is memory we can
1392 if (ml_static_protect(vaddr_cur
, PAGE_SIZE
, VM_PROT_WRITE
| VM_PROT_READ
) != KERN_SUCCESS
) {
1393 panic("Failed ml_static_mfree on %p", (void *) vaddr_cur
);
1398 * Must NOT tear down the "V==P" mapping for vaddr_cur as the zone alias scheme
1399 * relies on the persistence of these mappings for all time.
1401 // pmap_remove(kernel_pmap, (addr64_t) vaddr_cur, (addr64_t) (vaddr_cur + PAGE_SIZE));
1404 vm_page_create(ppn
, (ppn
+ 1));
1408 vm_page_lockspin_queues();
1409 vm_page_wire_count
-= freed_pages
;
1410 vm_page_wire_count_initial
-= freed_pages
;
1411 vm_page_unlock_queues();
1413 kprintf("ml_static_mfree: Released 0x%x pages at VA %p, size:0x%llx, last ppn: 0x%x\n", freed_pages
, (void *)vaddr
, (uint64_t)size
, ppn
);
1418 /* virtual to physical on wired pages */
1420 ml_vtophys(vm_offset_t vaddr
)
1422 return kvtophys(vaddr
);
1426 * Routine: ml_nofault_copy
1427 * Function: Perform a physical mode copy if the source and destination have
1428 * valid translations in the kernel pmap. If translations are present, they are
1429 * assumed to be wired; e.g., no attempt is made to guarantee that the
1430 * translations obtained remain valid for the duration of the copy process.
1433 ml_nofault_copy(vm_offset_t virtsrc
, vm_offset_t virtdst
, vm_size_t size
)
1435 addr64_t cur_phys_dst
, cur_phys_src
;
1436 vm_size_t count
, nbytes
= 0;
1439 if (!(cur_phys_src
= kvtophys(virtsrc
))) {
1442 if (!(cur_phys_dst
= kvtophys(virtdst
))) {
1445 if (!pmap_valid_address(trunc_page_64(cur_phys_dst
)) ||
1446 !pmap_valid_address(trunc_page_64(cur_phys_src
))) {
1449 count
= PAGE_SIZE
- (cur_phys_src
& PAGE_MASK
);
1450 if (count
> (PAGE_SIZE
- (cur_phys_dst
& PAGE_MASK
))) {
1451 count
= PAGE_SIZE
- (cur_phys_dst
& PAGE_MASK
);
1457 bcopy_phys(cur_phys_src
, cur_phys_dst
, count
);
1469 * Routine: ml_validate_nofault
1470 * Function: Validate that ths address range has a valid translations
1471 * in the kernel pmap. If translations are present, they are
1472 * assumed to be wired; i.e. no attempt is made to guarantee
1473 * that the translation persist after the check.
1474 * Returns: TRUE if the range is mapped and will not cause a fault,
1479 ml_validate_nofault(
1480 vm_offset_t virtsrc
, vm_size_t size
)
1482 addr64_t cur_phys_src
;
1486 if (!(cur_phys_src
= kvtophys(virtsrc
))) {
1489 if (!pmap_valid_address(trunc_page_64(cur_phys_src
))) {
1492 count
= (uint32_t)(PAGE_SIZE
- (cur_phys_src
& PAGE_MASK
));
1494 count
= (uint32_t)size
;
1505 ml_get_bouncepool_info(vm_offset_t
* phys_addr
, vm_size_t
* size
)
1512 active_rt_threads(__unused boolean_t active
)
1517 cpu_qos_cb_default(__unused
int urgency
, __unused
uint64_t qos_param1
, __unused
uint64_t qos_param2
)
1522 cpu_qos_update_t cpu_qos_update
= cpu_qos_cb_default
;
1525 cpu_qos_update_register(cpu_qos_update_t cpu_qos_cb
)
1527 if (cpu_qos_cb
!= NULL
) {
1528 cpu_qos_update
= cpu_qos_cb
;
1530 cpu_qos_update
= cpu_qos_cb_default
;
1535 thread_tell_urgency(thread_urgency_t urgency
, uint64_t rt_period
, uint64_t rt_deadline
, uint64_t sched_latency __unused
, __unused thread_t nthread
)
1537 SCHED_DEBUG_PLATFORM_KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_URGENCY
) | DBG_FUNC_START
, urgency
, rt_period
, rt_deadline
, sched_latency
, 0);
1539 cpu_qos_update((int)urgency
, rt_period
, rt_deadline
);
1541 SCHED_DEBUG_PLATFORM_KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_URGENCY
) | DBG_FUNC_END
, urgency
, rt_period
, rt_deadline
, 0, 0);
1545 machine_run_count(__unused
uint32_t count
)
1550 machine_choose_processor(__unused processor_set_t pset
, processor_t processor
)
1556 vm_offset_t
ml_stack_base(void);
1557 vm_size_t
ml_stack_size(void);
1562 uintptr_t local
= (uintptr_t) &local
;
1563 vm_offset_t intstack_top_ptr
;
1565 intstack_top_ptr
= getCpuDatap()->intstack_top
;
1566 if ((local
< intstack_top_ptr
) && (local
> intstack_top_ptr
- INTSTACK_SIZE
)) {
1567 return intstack_top_ptr
- INTSTACK_SIZE
;
1569 return current_thread()->kernel_stack
;
1575 uintptr_t local
= (uintptr_t) &local
;
1576 vm_offset_t intstack_top_ptr
;
1578 intstack_top_ptr
= getCpuDatap()->intstack_top
;
1579 if ((local
< intstack_top_ptr
) && (local
> intstack_top_ptr
- INTSTACK_SIZE
)) {
1580 return INTSTACK_SIZE
;
1582 return kernel_stack_size
;
1588 machine_timeout_suspended(void)
1594 ml_interrupt_prewarm(__unused
uint64_t deadline
)
1596 return KERN_FAILURE
;
1600 * Assumes fiq, irq disabled.
1603 ml_set_decrementer(uint32_t dec_value
)
1605 cpu_data_t
*cdp
= getCpuDatap();
1607 assert(ml_get_interrupts_enabled() == FALSE
);
1608 cdp
->cpu_decrementer
= dec_value
;
1610 if (cdp
->cpu_set_decrementer_func
) {
1611 ((void (*)(uint32_t))cdp
->cpu_set_decrementer_func
)(dec_value
);
1613 __asm__
volatile ("msr CNTP_TVAL_EL0, %0" : : "r"((uint64_t)dec_value
));
1622 // ISB required by ARMV7C.b section B8.1.2 & ARMv8 section D6.1.2
1623 // "Reads of CNTPCT[_EL0] can occur speculatively and out of order relative
1624 // to other instructions executed on the same processor."
1625 __builtin_arm_isb(ISB_SY
);
1626 timebase
= __builtin_arm_rsr64("CNTPCT_EL0");
1634 return ml_get_hwclock() + getCpuDatap()->cpu_base_timebase
;
1638 ml_get_decrementer()
1640 cpu_data_t
*cdp
= getCpuDatap();
1643 assert(ml_get_interrupts_enabled() == FALSE
);
1645 if (cdp
->cpu_get_decrementer_func
) {
1646 dec
= ((uint32_t (*)(void))cdp
->cpu_get_decrementer_func
)();
1650 __asm__
volatile ("mrs %0, CNTP_TVAL_EL0" : "=r"(wide_val
));
1651 dec
= (uint32_t)wide_val
;
1652 assert(wide_val
== (uint64_t)dec
);
1659 ml_get_timer_pending()
1663 __asm__
volatile ("mrs %0, CNTP_CTL_EL0" : "=r"(cntp_ctl
));
1664 return ((cntp_ctl
& CNTP_CTL_EL0_ISTATUS
) != 0) ? TRUE
: FALSE
;
1668 ml_wants_panic_trap_to_debugger(void)
1670 boolean_t result
= FALSE
;
1675 cache_trap_error(thread_t thread
, vm_map_address_t fault_addr
)
1677 mach_exception_data_type_t exc_data
[2];
1678 arm_saved_state_t
*regs
= get_user_regs(thread
);
1680 set_saved_state_far(regs
, fault_addr
);
1682 exc_data
[0] = KERN_INVALID_ADDRESS
;
1683 exc_data
[1] = fault_addr
;
1685 exception_triage(EXC_BAD_ACCESS
, exc_data
, 2);
1689 cache_trap_recover()
1691 vm_map_address_t fault_addr
;
1693 __asm__
volatile ("mrs %0, FAR_EL1" : "=r"(fault_addr
));
1695 cache_trap_error(current_thread(), fault_addr
);
1699 set_cache_trap_recover(thread_t thread
)
1701 #if defined(HAS_APPLE_PAC)
1702 thread
->recover
= (vm_address_t
)ptrauth_auth_and_resign(&cache_trap_recover
,
1703 ptrauth_key_function_pointer
, 0,
1704 ptrauth_key_function_pointer
, ptrauth_blend_discriminator(&thread
->recover
, PAC_DISCRIMINATOR_RECOVER
));
1705 #else /* defined(HAS_APPLE_PAC) */
1706 thread
->recover
= (vm_address_t
)cache_trap_recover
;
1707 #endif /* defined(HAS_APPLE_PAC) */
1711 dcache_flush_trap(vm_map_address_t start
, vm_map_size_t size
)
1713 vm_map_address_t end
= start
+ size
;
1714 thread_t thread
= current_thread();
1715 vm_offset_t old_recover
= thread
->recover
;
1718 if (task_has_64Bit_addr(current_task())) {
1719 if (end
> MACH_VM_MAX_ADDRESS
) {
1720 cache_trap_error(thread
, end
& ((1 << ARM64_CLINE_SHIFT
) - 1));
1723 if (end
> VM_MAX_ADDRESS
) {
1724 cache_trap_error(thread
, end
& ((1 << ARM64_CLINE_SHIFT
) - 1));
1729 cache_trap_error(thread
, start
& ((1 << ARM64_CLINE_SHIFT
) - 1));
1732 set_cache_trap_recover(thread
);
1735 * We're coherent on Apple ARM64 CPUs, so this could be a nop. However,
1736 * if the region given us is bad, it would be good to catch it and
1737 * crash, ergo we still do the flush.
1739 FlushPoC_DcacheRegion(start
, (uint32_t)size
);
1741 /* Restore recovery function */
1742 thread
->recover
= old_recover
;
1744 /* Return (caller does exception return) */
1748 icache_invalidate_trap(vm_map_address_t start
, vm_map_size_t size
)
1750 vm_map_address_t end
= start
+ size
;
1751 thread_t thread
= current_thread();
1752 vm_offset_t old_recover
= thread
->recover
;
1755 if (task_has_64Bit_addr(current_task())) {
1756 if (end
> MACH_VM_MAX_ADDRESS
) {
1757 cache_trap_error(thread
, end
& ((1 << ARM64_CLINE_SHIFT
) - 1));
1760 if (end
> VM_MAX_ADDRESS
) {
1761 cache_trap_error(thread
, end
& ((1 << ARM64_CLINE_SHIFT
) - 1));
1766 cache_trap_error(thread
, start
& ((1 << ARM64_CLINE_SHIFT
) - 1));
1769 set_cache_trap_recover(thread
);
1771 /* Invalidate iCache to point of unification */
1772 InvalidatePoU_IcacheRegion(start
, (uint32_t)size
);
1774 /* Restore recovery function */
1775 thread
->recover
= old_recover
;
1777 /* Return (caller does exception return) */
1780 __attribute__((noreturn
))
1782 platform_syscall(arm_saved_state_t
*state
)
1786 #define platform_syscall_kprintf(x...) /* kprintf("platform_syscall: " x) */
1788 code
= (uint32_t)get_saved_state_reg(state
, 3);
1792 platform_syscall_kprintf("icache flush requested.\n");
1793 icache_invalidate_trap(get_saved_state_reg(state
, 0), get_saved_state_reg(state
, 1));
1797 platform_syscall_kprintf("dcache flush requested.\n");
1798 dcache_flush_trap(get_saved_state_reg(state
, 0), get_saved_state_reg(state
, 1));
1802 platform_syscall_kprintf("set cthread self.\n");
1803 thread_set_cthread_self(get_saved_state_reg(state
, 0));
1807 platform_syscall_kprintf("get cthread self.\n");
1808 set_saved_state_reg(state
, 0, thread_get_cthread_self());
1811 platform_syscall_kprintf("unknown: %d\n", code
);
1815 thread_exception_return();
1819 _enable_timebase_event_stream(uint32_t bit_index
)
1821 uint64_t cntkctl
; /* One wants to use 32 bits, but "mrs" prefers it this way */
1823 if (bit_index
>= 64) {
1824 panic("%s: invalid bit index (%u)", __FUNCTION__
, bit_index
);
1827 __asm__
volatile ("mrs %0, CNTKCTL_EL1" : "=r"(cntkctl
));
1829 cntkctl
|= (bit_index
<< CNTKCTL_EL1_EVENTI_SHIFT
);
1830 cntkctl
|= CNTKCTL_EL1_EVNTEN
;
1831 cntkctl
|= CNTKCTL_EL1_EVENTDIR
; /* 1->0; why not? */
1834 * If the SOC supports it (and it isn't broken), enable
1835 * EL0 access to the physical timebase register.
1837 if (user_timebase_type() != USER_TIMEBASE_NONE
) {
1838 cntkctl
|= CNTKCTL_EL1_PL0PCTEN
;
1841 __asm__
volatile ("msr CNTKCTL_EL1, %0" : : "r"(cntkctl
));
1845 * Turn timer on, unmask that interrupt.
1848 _enable_virtual_timer(void)
1850 uint64_t cntvctl
= CNTP_CTL_EL0_ENABLE
; /* One wants to use 32 bits, but "mrs" prefers it this way */
1852 __asm__
volatile ("msr CNTP_CTL_EL0, %0" : : "r"(cntvctl
));
1855 uint64_t events_per_sec
= 0;
1858 fiq_context_init(boolean_t enable_fiq __unused
)
1860 _enable_timebase_event_stream(fiq_eventi
);
1862 /* Interrupts still disabled. */
1863 assert(ml_get_interrupts_enabled() == FALSE
);
1864 _enable_virtual_timer();
1868 fiq_context_bootstrap(boolean_t enable_fiq
)
1870 #if defined(APPLE_ARM64_ARCH_FAMILY) || defined(BCM2837)
1871 /* Could fill in our own ops here, if we needed them */
1872 uint64_t ticks_per_sec
, ticks_per_event
;
1875 ticks_per_sec
= gPEClockFrequencyInfo
.timebase_frequency_hz
;
1876 ticks_per_event
= ticks_per_sec
/ events_per_sec
;
1877 bit_index
= flsll(ticks_per_event
) - 1; /* Highest bit set */
1879 /* Round up to power of two */
1880 if ((ticks_per_event
& ((1 << bit_index
) - 1)) != 0) {
1885 * The timer can only trigger on rising or falling edge,
1886 * not both; we don't care which we trigger on, but we
1887 * do need to adjust which bit we are interested in to
1890 if (bit_index
!= 0) {
1894 fiq_eventi
= bit_index
;
1896 #error Need a board configuration.
1898 fiq_context_init(enable_fiq
);
1902 ml_delay_should_spin(uint64_t interval
)
1904 cpu_data_t
*cdp
= getCpuDatap();
1906 if (cdp
->cpu_idle_latency
) {
1907 return (interval
< cdp
->cpu_idle_latency
) ? TRUE
: FALSE
;
1910 * Early boot, latency is unknown. Err on the side of blocking,
1911 * which should always be safe, even if slow
1918 ml_thread_is64bit(thread_t thread
)
1920 return thread_is_64bit_addr(thread
);
1924 ml_delay_on_yield(void)
1926 #if DEVELOPMENT || DEBUG
1927 if (yield_delay_us
) {
1928 delay(yield_delay_us
);
1934 ml_timer_evaluate(void)
1939 ml_timer_forced_evaluation(void)
1945 ml_energy_stat(thread_t t
)
1947 return t
->machine
.energy_estimate_nj
;
1952 ml_gpu_stat_update(__unused
uint64_t gpu_ns_delta
)
1956 * For now: update the resource coalition stats of the
1957 * current thread's coalition
1959 task_coalition_update_gpu_stats(current_task(), gpu_ns_delta
);
1964 ml_gpu_stat(__unused thread_t t
)
1969 #if !CONFIG_SKIP_PRECISE_USER_KERNEL_TIME
1971 timer_state_event(boolean_t switch_to_kernel
)
1973 thread_t thread
= current_thread();
1974 if (!thread
->precise_user_kernel_time
) {
1978 processor_data_t
*pd
= &getCpuDatap()->cpu_processor
->processor_data
;
1979 uint64_t now
= ml_get_timebase();
1981 timer_stop(pd
->current_state
, now
);
1982 pd
->current_state
= (switch_to_kernel
) ? &pd
->system_state
: &pd
->user_state
;
1983 timer_start(pd
->current_state
, now
);
1985 timer_stop(pd
->thread_timer
, now
);
1986 pd
->thread_timer
= (switch_to_kernel
) ? &thread
->system_timer
: &thread
->user_timer
;
1987 timer_start(pd
->thread_timer
, now
);
1991 timer_state_event_user_to_kernel(void)
1993 timer_state_event(TRUE
);
1997 timer_state_event_kernel_to_user(void)
1999 timer_state_event(FALSE
);
2001 #endif /* !CONFIG_SKIP_PRECISE_USER_KERNEL_TIME */
2004 * The following are required for parts of the kernel
2005 * that cannot resolve these functions as inlines:
2007 extern thread_t
current_act(void) __attribute__((const));
2011 return current_thread_fast();
2014 #undef current_thread
2015 extern thread_t
current_thread(void) __attribute__((const));
2017 current_thread(void)
2019 return current_thread_fast();
2028 ex_cb_info_t ex_cb_info
[EXCB_CLASS_MAX
];
2031 * Callback registration
2032 * Currently we support only one registered callback per class but
2033 * it should be possible to support more callbacks
2037 ex_cb_class_t cb_class
,
2041 ex_cb_info_t
*pInfo
= &ex_cb_info
[cb_class
];
2043 if ((NULL
== cb
) || (cb_class
>= EXCB_CLASS_MAX
)) {
2044 return KERN_INVALID_VALUE
;
2047 if (NULL
== pInfo
->cb
) {
2049 pInfo
->refcon
= refcon
;
2050 return KERN_SUCCESS
;
2052 return KERN_FAILURE
;
2056 * Called internally by platform kernel to invoke the registered callback for class
2060 ex_cb_class_t cb_class
,
2063 ex_cb_info_t
*pInfo
= &ex_cb_info
[cb_class
];
2064 ex_cb_state_t state
= {far
};
2066 if (cb_class
>= EXCB_CLASS_MAX
) {
2067 panic("Invalid exception callback class 0x%x\n", cb_class
);
2071 return pInfo
->cb(cb_class
, pInfo
->refcon
, &state
);
2073 return EXCB_ACTION_NONE
;
2076 #if defined(HAS_APPLE_PAC)
2078 ml_task_set_disable_user_jop(task_t task
, boolean_t disable_user_jop
)
2081 task
->disable_user_jop
= disable_user_jop
;
2085 ml_thread_set_disable_user_jop(thread_t thread
, boolean_t disable_user_jop
)
2088 thread
->machine
.disable_user_jop
= disable_user_jop
;
2092 ml_task_set_rop_pid(task_t task
, task_t parent_task
, boolean_t inherit
)
2095 task
->rop_pid
= parent_task
->rop_pid
;
2097 task
->rop_pid
= early_random();
2100 #endif /* defined(HAS_APPLE_PAC) */
2103 #if defined(HAS_APPLE_PAC)
2106 * ml_auth_ptr_unchecked: call this instead of ptrauth_auth_data
2107 * instrinsic when you don't want to trap on auth fail.
2112 ml_auth_ptr_unchecked(void *ptr
, ptrauth_key key
, uint64_t modifier
)
2114 switch (key
& 0x3) {
2115 case ptrauth_key_asia
:
2116 asm volatile ("autia %[ptr], %[modifier]" : [ptr
] "+r"(ptr
) : [modifier
] "r"(modifier
));
2118 case ptrauth_key_asib
:
2119 asm volatile ("autib %[ptr], %[modifier]" : [ptr
] "+r"(ptr
) : [modifier
] "r"(modifier
));
2121 case ptrauth_key_asda
:
2122 asm volatile ("autda %[ptr], %[modifier]" : [ptr
] "+r"(ptr
) : [modifier
] "r"(modifier
));
2124 case ptrauth_key_asdb
:
2125 asm volatile ("autdb %[ptr], %[modifier]" : [ptr
] "+r"(ptr
) : [modifier
] "r"(modifier
));
2131 #endif /* defined(HAS_APPLE_PAC) */