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/rtclock.h>
40 #include <console/serial_protos.h>
41 #include <kern/machine.h>
42 #include <prng/random.h>
43 #include <kern/startup.h>
44 #include <kern/thread.h>
45 #include <mach/machine.h>
46 #include <machine/atomic.h>
48 #include <vm/vm_page.h>
49 #include <sys/kdebug.h>
50 #include <kern/coalition.h>
51 #include <pexpert/device_tree.h>
53 #include <IOKit/IOPlatformExpert.h>
55 #if defined(KERNEL_INTEGRITY_KTRR)
56 #include <libkern/kernel_mach_header.h>
59 #include <libkern/section_keywords.h>
66 static int max_cpus_initialized
= 0;
67 #define MAX_CPUS_SET 0x1
68 #define MAX_CPUS_WAIT 0x2
71 uint32_t LockTimeOutUsec
;
73 boolean_t is_clock_configured
= FALSE
;
75 uint32_t yield_delay_us
= 42; /* Less than cpu_idle_latency to ensure ml_delay_should_spin is true */
77 extern int mach_assert
;
78 extern volatile uint32_t debug_enabled
;
80 extern vm_offset_t segEXTRADATA
;
81 extern vm_offset_t segLOWESTTEXT
;
82 extern vm_offset_t segLASTB
;
83 extern unsigned long segSizeLAST
;
86 void machine_conf(void);
88 thread_t
Idle_context(void);
90 SECURITY_READ_ONLY_LATE(static uint32_t) cpu_phys_ids
[MAX_CPUS
] = {[0 ... MAX_CPUS
- 1] = (uint32_t)-1};
91 SECURITY_READ_ONLY_LATE(static unsigned int) avail_cpus
= 0;
92 SECURITY_READ_ONLY_LATE(static int) boot_cpu
= -1;
93 SECURITY_READ_ONLY_LATE(static int) max_cpu_number
= 0;
94 SECURITY_READ_ONLY_LATE(cluster_type_t
) boot_cluster
= CLUSTER_TYPE_SMP
;
96 SECURITY_READ_ONLY_LATE(static uint32_t) fiq_eventi
= UINT32_MAX
;
98 lockdown_handler_t lockdown_handler
;
100 lck_mtx_t lockdown_handler_lck
;
101 lck_grp_t
*lockdown_handler_grp
;
104 void ml_lockdown_init(void);
105 void ml_lockdown_run_handler(void);
106 uint32_t get_arm_cpu_version(void);
109 void ml_cpu_signal(unsigned int cpu_id __unused
)
111 panic("Platform does not support ACC Fast IPI");
114 void ml_cpu_signal_deferred_adjust_timer(uint64_t nanosecs
) {
116 panic("Platform does not support ACC Fast IPI");
119 uint64_t ml_cpu_signal_deferred_get_timer() {
123 void ml_cpu_signal_deferred(unsigned int cpu_id __unused
)
125 panic("Platform does not support ACC Fast IPI deferral");
128 void ml_cpu_signal_retract(unsigned int cpu_id __unused
)
130 panic("Platform does not support ACC Fast IPI retraction");
133 void machine_idle(void)
135 __asm__
volatile ("msr DAIFSet, %[mask]" ::[mask
] "i" (DAIFSC_IRQF
| DAIFSC_FIQF
));
137 __asm__
volatile ("msr DAIFClr, %[mask]" ::[mask
] "i" (DAIFSC_IRQF
| DAIFSC_FIQF
));
145 boolean_t
get_vfp_enabled(void)
150 void OSSynchronizeIO(void)
152 __builtin_arm_dsb(DSB_SY
);
155 uint64_t get_aux_control(void)
159 MRS(value
, "ACTLR_EL1");
163 uint64_t get_mmu_control(void)
167 MRS(value
, "SCTLR_EL1");
171 uint64_t get_tcr(void)
175 MRS(value
, "TCR_EL1");
179 boolean_t
ml_get_interrupts_enabled(void)
184 if (value
& DAIF_IRQF
)
189 pmap_paddr_t
get_mmu_ttb(void)
193 MRS(value
, "TTBR0_EL1");
197 static uint32_t get_midr_el1(void)
201 MRS(value
, "MIDR_EL1");
203 /* This is a 32-bit register. */
204 return (uint32_t) value
;
207 uint32_t get_arm_cpu_version(void)
209 uint32_t value
= get_midr_el1();
211 /* Compose the register values into 8 bits; variant[7:4], revision[3:0]. */
212 return ((value
& MIDR_EL1_REV_MASK
) >> MIDR_EL1_REV_SHIFT
) | ((value
& MIDR_EL1_VAR_MASK
) >> (MIDR_EL1_VAR_SHIFT
- 4));
216 * user_cont_hwclock_allowed()
218 * Indicates whether we allow EL0 to read the physical timebase (CNTPCT_EL0)
219 * as a continuous time source (e.g. from mach_continuous_time)
221 boolean_t
user_cont_hwclock_allowed(void)
227 * user_timebase_allowed()
229 * Indicates whether we allow EL0 to read the physical timebase (CNTPCT_EL0).
231 boolean_t
user_timebase_allowed(void)
236 boolean_t
arm64_wfe_allowed(void)
241 #if defined(KERNEL_INTEGRITY_KTRR)
243 uint64_t rorgn_begin
__attribute__((section("__DATA, __const"))) = 0;
244 uint64_t rorgn_end
__attribute__((section("__DATA, __const"))) = 0;
245 vm_offset_t amcc_base
;
247 static void assert_unlocked(void);
248 static void assert_amcc_cache_disabled(void);
249 static void lock_amcc(void);
250 static void lock_mmu(uint64_t begin
, uint64_t end
);
252 void rorgn_stash_range(void)
255 #if DEVELOPMENT || DEBUG
256 boolean_t rorgn_disable
= FALSE
;
258 PE_parse_boot_argn("-unsafe_kernel_text", &rorgn_disable
, sizeof(rorgn_disable
));
261 /* take early out if boot arg present, don't query any machine registers to avoid
262 * dependency on amcc DT entry
268 /* Get the AMC values, and stash them into rorgn_begin, rorgn_end.
269 * gPhysBase is the base of DRAM managed by xnu. we need DRAM_BASE as
270 * the AMCC RO region begin/end registers are in units of 16KB page
271 * numbers from DRAM_BASE so we'll truncate gPhysBase at 512MB granule
272 * and assert the value is the canonical DRAM_BASE PA of 0x8_0000_0000 for arm64.
275 uint64_t dram_base
= gPhysBase
& ~0x1FFFFFFFULL
; /* 512MB */
276 assert(dram_base
== 0x800000000ULL
);
278 #if defined(KERNEL_INTEGRITY_KTRR)
279 uint64_t soc_base
= 0;
280 DTEntry entryP
= NULL
;
281 uintptr_t *reg_prop
= NULL
;
282 uint32_t prop_size
= 0;
285 soc_base
= pe_arm_get_soc_base_phys();
286 rc
= DTFindEntry("name", "mcc", &entryP
);
287 assert(rc
== kSuccess
);
288 rc
= DTGetProperty(entryP
, "reg", (void **)®_prop
, &prop_size
);
289 assert(rc
== kSuccess
);
290 amcc_base
= ml_io_map(soc_base
+ *reg_prop
, *(reg_prop
+ 1));
292 #error "KERNEL_INTEGRITY config error"
295 #if defined(KERNEL_INTEGRITY_KTRR)
296 assert(rRORGNENDADDR
> rRORGNBASEADDR
);
297 rorgn_begin
= (rRORGNBASEADDR
<< AMCC_PGSHIFT
) + dram_base
;
298 rorgn_end
= (rRORGNENDADDR
<< AMCC_PGSHIFT
) + dram_base
;
300 #error KERNEL_INTEGRITY config error
301 #endif /* defined (KERNEL_INTEGRITY_KTRR) */
304 static void assert_unlocked() {
305 uint64_t ktrr_lock
= 0;
306 uint32_t rorgn_lock
= 0;
309 #if defined(KERNEL_INTEGRITY_KTRR)
310 rorgn_lock
= rRORGNLOCK
;
311 ktrr_lock
= __builtin_arm_rsr64(ARM64_REG_KTRR_LOCK_EL1
);
313 #error KERNEL_INTEGRITY config error
314 #endif /* defined(KERNEL_INTEGRITY_KTRR) */
320 static void lock_amcc() {
321 #if defined(KERNEL_INTEGRITY_KTRR)
323 __builtin_arm_isb(ISB_SY
);
325 #error KERNEL_INTEGRITY config error
329 static void lock_mmu(uint64_t begin
, uint64_t end
) {
331 #if defined(KERNEL_INTEGRITY_KTRR)
333 __builtin_arm_wsr64(ARM64_REG_KTRR_LOWER_EL1
, begin
);
334 __builtin_arm_wsr64(ARM64_REG_KTRR_UPPER_EL1
, end
);
335 __builtin_arm_wsr64(ARM64_REG_KTRR_LOCK_EL1
, 1ULL);
339 __builtin_arm_isb(ISB_SY
);
343 #error KERNEL_INTEGRITY config error
348 static void assert_amcc_cache_disabled() {
349 #if defined(KERNEL_INTEGRITY_KTRR)
350 assert((rMCCGEN
& 1) == 0); /* assert M$ disabled or LLC clean will be unreliable */
352 #error KERNEL_INTEGRITY config error
357 * void rorgn_lockdown(void)
359 * Lock the MMU and AMCC RORegion within lower and upper boundaries if not already locked
361 * [ ] - ensure this is being called ASAP on secondary CPUs: KTRR programming and lockdown handled in
362 * start.s:start_cpu() for subsequent wake/resume of all cores
364 void rorgn_lockdown(void)
366 vm_offset_t ktrr_begin
, ktrr_end
;
367 unsigned long last_segsz
;
369 #if DEVELOPMENT || DEBUG
370 boolean_t ktrr_disable
= FALSE
;
372 PE_parse_boot_argn("-unsafe_kernel_text", &ktrr_disable
, sizeof(ktrr_disable
));
376 * take early out if boot arg present, since we may not have amcc DT entry present
377 * we can't assert that iboot hasn't programmed the RO region lockdown registers
381 #endif /* DEVELOPMENT || DEBUG */
385 /* [x] - Use final method of determining all kernel text range or expect crashes */
386 ktrr_begin
= segEXTRADATA
;
387 assert(ktrr_begin
&& gVirtBase
&& gPhysBase
);
389 ktrr_begin
= kvtophys(ktrr_begin
);
391 ktrr_end
= kvtophys(segLASTB
);
392 last_segsz
= segSizeLAST
;
393 #if defined(KERNEL_INTEGRITY_KTRR)
394 /* __LAST is not part of the MMU KTRR region (it is however part of the AMCC KTRR region) */
395 ktrr_end
= (ktrr_end
- 1) & ~AMCC_PGMASK
;
396 /* ensure that iboot and xnu agree on the ktrr range */
397 assert(rorgn_begin
== ktrr_begin
&& rorgn_end
== (ktrr_end
+ last_segsz
));
398 /* assert that __LAST segment containing privileged insns is only a single page */
399 assert(last_segsz
== PAGE_SIZE
);
403 #if DEBUG || DEVELOPMENT
404 printf("KTRR Begin: %p End: %p, setting lockdown\n", (void *)ktrr_begin
, (void *)ktrr_end
);
407 /* [x] - ensure all in flight writes are flushed to AMCC before enabling RO Region Lock */
409 assert_amcc_cache_disabled();
411 CleanPoC_DcacheRegion_Force(phystokv(ktrr_begin
),
412 (unsigned)((ktrr_end
+ last_segsz
) - ktrr_begin
+ AMCC_PGMASK
));
416 lock_mmu(ktrr_begin
, ktrr_end
);
418 #if DEVELOPMENT || DEBUG
422 /* now we can run lockdown handler */
423 ml_lockdown_run_handler();
426 #endif /* defined(KERNEL_INTEGRITY_KTRR)*/
429 machine_startup(__unused boot_args
* args
)
434 PE_parse_boot_argn("assert", &mach_assert
, sizeof (mach_assert
));
436 if (PE_parse_boot_argn("preempt", &boot_arg
, sizeof (boot_arg
))) {
437 default_preemption_rate
= boot_arg
;
439 if (PE_parse_boot_argn("bg_preempt", &boot_arg
, sizeof (boot_arg
))) {
440 default_bg_preemption_rate
= boot_arg
;
443 PE_parse_boot_argn("yield_delay_us", &yield_delay_us
, sizeof (yield_delay_us
));
448 * Kick off the kernel bootstrap.
454 void machine_lockdown_preflight(void)
456 #if CONFIG_KERNEL_INTEGRITY
458 #if defined(KERNEL_INTEGRITY_KTRR)
465 void machine_lockdown(void)
467 #if CONFIG_KERNEL_INTEGRITY
468 #if KERNEL_INTEGRITY_WT
471 * Notify the monitor about the completion of early kernel bootstrap.
472 * From this point forward it will enforce the integrity of kernel text,
473 * rodata and page tables.
477 monitor_call(MONITOR_LOCKDOWN
, 0, 0, 0);
479 #endif /* KERNEL_INTEGRITY_WT */
482 #if defined(KERNEL_INTEGRITY_KTRR)
485 * Lock physical KTRR region. KTRR region is read-only. Memory outside
486 * the region is not executable at EL1.
490 #endif /* defined(KERNEL_INTEGRITY_KTRR)*/
493 #endif /* CONFIG_KERNEL_INTEGRITY */
499 __unused vm_size_t size
)
501 return (PE_boot_args());
508 * This is known to be inaccurate. mem_size should always be capped at 2 GB
510 machine_info
.memory_size
= (uint32_t)mem_size
;
518 is_clock_configured
= TRUE
;
524 slave_machine_init(__unused
void *param
)
526 cpu_machine_init(); /* Initialize the processor */
527 clock_init(); /* Init the clock */
531 * Routine: machine_processor_shutdown
535 machine_processor_shutdown(
536 __unused thread_t thread
,
537 void (*doshutdown
) (processor_t
),
538 processor_t processor
)
540 return (Shutdown_context(doshutdown
, processor
));
544 * Routine: ml_init_max_cpus
548 ml_init_max_cpus(unsigned int max_cpus
)
550 boolean_t current_state
;
552 current_state
= ml_set_interrupts_enabled(FALSE
);
553 if (max_cpus_initialized
!= MAX_CPUS_SET
) {
554 machine_info
.max_cpus
= max_cpus
;
555 machine_info
.physical_cpu_max
= max_cpus
;
556 machine_info
.logical_cpu_max
= max_cpus
;
557 if (max_cpus_initialized
== MAX_CPUS_WAIT
)
558 thread_wakeup((event_t
) & max_cpus_initialized
);
559 max_cpus_initialized
= MAX_CPUS_SET
;
561 (void) ml_set_interrupts_enabled(current_state
);
565 * Routine: ml_get_max_cpus
569 ml_get_max_cpus(void)
571 boolean_t current_state
;
573 current_state
= ml_set_interrupts_enabled(FALSE
);
574 if (max_cpus_initialized
!= MAX_CPUS_SET
) {
575 max_cpus_initialized
= MAX_CPUS_WAIT
;
576 assert_wait((event_t
) & max_cpus_initialized
, THREAD_UNINT
);
577 (void) thread_block(THREAD_CONTINUE_NULL
);
579 (void) ml_set_interrupts_enabled(current_state
);
580 return (machine_info
.max_cpus
);
584 * Routine: ml_init_lock_timeout
588 ml_init_lock_timeout(void)
592 uint64_t default_timeout_ns
= NSEC_PER_SEC
>>2;
595 if (PE_parse_boot_argn("slto_us", &slto
, sizeof (slto
)))
596 default_timeout_ns
= slto
* NSEC_PER_USEC
;
598 nanoseconds_to_absolutetime(default_timeout_ns
, &abstime
);
599 LockTimeOutUsec
= (uint32_t)(abstime
/ NSEC_PER_USEC
);
600 LockTimeOut
= (uint32_t)abstime
;
602 if (PE_parse_boot_argn("mtxspin", &mtxspin
, sizeof (mtxspin
))) {
603 if (mtxspin
> USEC_PER_SEC
>>4)
604 mtxspin
= USEC_PER_SEC
>>4;
605 nanoseconds_to_absolutetime(mtxspin
*NSEC_PER_USEC
, &abstime
);
607 nanoseconds_to_absolutetime(10*NSEC_PER_USEC
, &abstime
);
613 * This is called from the machine-independent routine cpu_up()
614 * to perform machine-dependent info updates.
619 hw_atomic_add(&machine_info
.physical_cpu
, 1);
620 hw_atomic_add(&machine_info
.logical_cpu
, 1);
624 * This is called from the machine-independent routine cpu_down()
625 * to perform machine-dependent info updates.
630 cpu_data_t
*cpu_data_ptr
;
632 hw_atomic_sub(&machine_info
.physical_cpu
, 1);
633 hw_atomic_sub(&machine_info
.logical_cpu
, 1);
636 * If we want to deal with outstanding IPIs, we need to
637 * do relatively early in the processor_doshutdown path,
638 * as we pend decrementer interrupts using the IPI
639 * mechanism if we cannot immediately service them (if
640 * IRQ is masked). Do so now.
642 * We aren't on the interrupt stack here; would it make
643 * more sense to disable signaling and then enable
644 * interrupts? It might be a bit cleaner.
646 cpu_data_ptr
= getCpuDatap();
647 cpu_data_ptr
->cpu_running
= FALSE
;
648 cpu_signal_handler_internal(TRUE
);
652 * Routine: ml_cpu_get_info
656 ml_cpu_get_info(ml_cpu_info_t
* ml_cpu_info
)
658 cache_info_t
*cpuid_cache_info
;
660 cpuid_cache_info
= cache_info();
661 ml_cpu_info
->vector_unit
= 0;
662 ml_cpu_info
->cache_line_size
= cpuid_cache_info
->c_linesz
;
663 ml_cpu_info
->l1_icache_size
= cpuid_cache_info
->c_isize
;
664 ml_cpu_info
->l1_dcache_size
= cpuid_cache_info
->c_dsize
;
666 #if (__ARM_ARCH__ >= 7)
667 ml_cpu_info
->l2_settings
= 1;
668 ml_cpu_info
->l2_cache_size
= cpuid_cache_info
->c_l2size
;
670 ml_cpu_info
->l2_settings
= 0;
671 ml_cpu_info
->l2_cache_size
= 0xFFFFFFFF;
673 ml_cpu_info
->l3_settings
= 0;
674 ml_cpu_info
->l3_cache_size
= 0xFFFFFFFF;
678 ml_get_machine_mem(void)
680 return (machine_info
.memory_size
);
683 __attribute__((noreturn
))
685 halt_all_cpus(boolean_t reboot
)
688 printf("MACH Reboot\n");
689 PEHaltRestart(kPERestartCPU
);
691 printf("CPU halted\n");
692 PEHaltRestart(kPEHaltCPU
);
697 __attribute__((noreturn
))
701 halt_all_cpus(FALSE
);
705 * Routine: machine_signal_idle
710 processor_t processor
)
712 cpu_signal(processor_to_cpu_datap(processor
), SIGPnop
, (void *)NULL
, (void *)NULL
);
713 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_REMOTE_AST
), processor
->cpu_id
, 0 /* nop */, 0, 0, 0);
717 machine_signal_idle_deferred(
718 processor_t processor
)
720 cpu_signal_deferred(processor_to_cpu_datap(processor
));
721 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_REMOTE_DEFERRED_AST
), processor
->cpu_id
, 0 /* nop */, 0, 0, 0);
725 machine_signal_idle_cancel(
726 processor_t processor
)
728 cpu_signal_cancel(processor_to_cpu_datap(processor
));
729 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_REMOTE_CANCEL_AST
), processor
->cpu_id
, 0 /* nop */, 0, 0, 0);
733 * Routine: ml_install_interrupt_handler
734 * Function: Initialize Interrupt Handler
737 ml_install_interrupt_handler(
741 IOInterruptHandler handler
,
744 cpu_data_t
*cpu_data_ptr
;
745 boolean_t current_state
;
747 current_state
= ml_set_interrupts_enabled(FALSE
);
748 cpu_data_ptr
= getCpuDatap();
750 cpu_data_ptr
->interrupt_nub
= nub
;
751 cpu_data_ptr
->interrupt_source
= source
;
752 cpu_data_ptr
->interrupt_target
= target
;
753 cpu_data_ptr
->interrupt_handler
= handler
;
754 cpu_data_ptr
->interrupt_refCon
= refCon
;
756 cpu_data_ptr
->interrupts_enabled
= TRUE
;
757 (void) ml_set_interrupts_enabled(current_state
);
759 initialize_screen(NULL
, kPEAcquireScreen
);
763 * Routine: ml_init_interrupt
764 * Function: Initialize Interrupts
767 ml_init_interrupt(void)
772 * Routine: ml_init_timebase
773 * Function: register and setup Timebase, Decremeter services
775 void ml_init_timebase(
778 vm_offset_t int_address
,
779 vm_offset_t int_value __unused
)
781 cpu_data_t
*cpu_data_ptr
;
783 cpu_data_ptr
= (cpu_data_t
*)args
;
785 if ((cpu_data_ptr
== &BootCpuData
)
786 && (rtclock_timebase_func
.tbd_fiq_handler
== (void *)NULL
)) {
787 rtclock_timebase_func
= *tbd_funcs
;
788 rtclock_timebase_addr
= int_address
;
793 ml_parse_cpu_topology(void)
795 DTEntry entry
, child __unused
;
796 OpaqueDTEntryIterator iter
;
797 uint32_t cpu_boot_arg
;
800 cpu_boot_arg
= MAX_CPUS
;
802 PE_parse_boot_argn("cpus", &cpu_boot_arg
, sizeof(cpu_boot_arg
));
804 err
= DTLookupEntry(NULL
, "/cpus", &entry
);
805 assert(err
== kSuccess
);
807 err
= DTInitEntryIterator(entry
, &iter
);
808 assert(err
== kSuccess
);
810 while (kSuccess
== DTIterateEntries(&iter
, &child
)) {
811 unsigned int propSize
;
813 int cpu_id
= avail_cpus
++;
815 if (kSuccess
== DTGetProperty(child
, "cpu-id", &prop
, &propSize
))
816 cpu_id
= *((int32_t*)prop
);
818 assert(cpu_id
< MAX_CPUS
);
819 assert(cpu_phys_ids
[cpu_id
] == (uint32_t)-1);
821 if (boot_cpu
== -1) {
822 if (kSuccess
!= DTGetProperty(child
, "state", &prop
, &propSize
))
823 panic("unable to retrieve state for cpu %d", cpu_id
);
825 if (strncmp((char*)prop
, "running", propSize
) == 0) {
829 if (kSuccess
!= DTGetProperty(child
, "reg", &prop
, &propSize
))
830 panic("unable to retrieve physical ID for cpu %d", cpu_id
);
832 cpu_phys_ids
[cpu_id
] = *((uint32_t*)prop
);
834 if ((cpu_id
> max_cpu_number
) && ((cpu_id
== boot_cpu
) || (avail_cpus
<= cpu_boot_arg
)))
835 max_cpu_number
= cpu_id
;
838 if (avail_cpus
> cpu_boot_arg
)
839 avail_cpus
= cpu_boot_arg
;
842 panic("No cpus found!");
845 panic("unable to determine boot cpu!");
848 * Set TPIDRRO_EL0 to indicate the correct cpu number, as we may
849 * not be booting from cpu 0. Userspace will consume the current
850 * CPU number through this register. For non-boot cores, this is
851 * done in start.s (start_cpu) using the cpu_number field of the
852 * per-cpu data object.
854 assert(__builtin_arm_rsr64("TPIDRRO_EL0") == 0);
855 __builtin_arm_wsr64("TPIDRRO_EL0", (uint64_t)boot_cpu
);
859 ml_get_cpu_count(void)
865 ml_get_boot_cpu_number(void)
871 ml_get_boot_cluster(void)
877 ml_get_cpu_number(uint32_t phys_id
)
879 for (int log_id
= 0; log_id
<= ml_get_max_cpu_number(); ++log_id
) {
880 if (cpu_phys_ids
[log_id
] == phys_id
)
887 ml_get_max_cpu_number(void)
889 return max_cpu_number
;
893 void ml_lockdown_init() {
894 lockdown_handler_grp
= lck_grp_alloc_init("lockdown_handler", NULL
);
895 assert(lockdown_handler_grp
!= NULL
);
897 lck_mtx_init(&lockdown_handler_lck
, lockdown_handler_grp
, NULL
);
902 ml_lockdown_handler_register(lockdown_handler_t f
, void *this)
904 if (lockdown_handler
|| !f
) {
908 lck_mtx_lock(&lockdown_handler_lck
);
909 lockdown_handler
= f
;
910 lockdown_this
= this;
912 #if !(defined(KERNEL_INTEGRITY_KTRR))
914 lockdown_handler(this);
917 lockdown_handler(this);
920 lck_mtx_unlock(&lockdown_handler_lck
);
925 void ml_lockdown_run_handler() {
926 lck_mtx_lock(&lockdown_handler_lck
);
927 assert(!lockdown_done
);
930 if (lockdown_handler
) {
931 lockdown_handler(lockdown_this
);
933 lck_mtx_unlock(&lockdown_handler_lck
);
937 ml_processor_register(
938 ml_processor_info_t
* in_processor_info
,
939 processor_t
* processor_out
,
940 ipi_handler_t
* ipi_handler
)
942 cpu_data_t
*this_cpu_datap
;
943 processor_set_t pset
;
944 boolean_t is_boot_cpu
;
945 static unsigned int reg_cpu_count
= 0;
947 if (in_processor_info
->log_id
> (uint32_t)ml_get_max_cpu_number())
950 if ((unsigned int)OSIncrementAtomic((SInt32
*)®_cpu_count
) >= avail_cpus
)
953 if (in_processor_info
->log_id
!= (uint32_t)ml_get_boot_cpu_number()) {
955 this_cpu_datap
= cpu_data_alloc(FALSE
);
956 cpu_data_init(this_cpu_datap
);
958 this_cpu_datap
= &BootCpuData
;
962 assert(in_processor_info
->log_id
< MAX_CPUS
);
964 this_cpu_datap
->cpu_id
= in_processor_info
->cpu_id
;
966 this_cpu_datap
->cpu_console_buf
= console_cpu_alloc(is_boot_cpu
);
967 if (this_cpu_datap
->cpu_console_buf
== (void *)(NULL
))
968 goto processor_register_error
;
971 this_cpu_datap
->cpu_number
= in_processor_info
->log_id
;
973 if (cpu_data_register(this_cpu_datap
) != KERN_SUCCESS
)
974 goto processor_register_error
;
977 this_cpu_datap
->cpu_idle_notify
= (void *) in_processor_info
->processor_idle
;
978 this_cpu_datap
->cpu_cache_dispatch
= in_processor_info
->platform_cache_dispatch
;
979 nanoseconds_to_absolutetime((uint64_t) in_processor_info
->powergate_latency
, &this_cpu_datap
->cpu_idle_latency
);
980 this_cpu_datap
->cpu_reset_assist
= kvtophys(in_processor_info
->powergate_stub_addr
);
982 this_cpu_datap
->idle_timer_notify
= (void *) in_processor_info
->idle_timer
;
983 this_cpu_datap
->idle_timer_refcon
= in_processor_info
->idle_timer_refcon
;
985 this_cpu_datap
->platform_error_handler
= (void *) in_processor_info
->platform_error_handler
;
986 this_cpu_datap
->cpu_regmap_paddr
= in_processor_info
->regmap_paddr
;
987 this_cpu_datap
->cpu_phys_id
= in_processor_info
->phys_id
;
988 this_cpu_datap
->cpu_l2_access_penalty
= in_processor_info
->l2_access_penalty
;
990 this_cpu_datap
->cpu_cluster_type
= in_processor_info
->cluster_type
;
991 this_cpu_datap
->cpu_cluster_id
= in_processor_info
->cluster_id
;
992 this_cpu_datap
->cpu_l2_id
= in_processor_info
->l2_cache_id
;
993 this_cpu_datap
->cpu_l2_size
= in_processor_info
->l2_cache_size
;
994 this_cpu_datap
->cpu_l3_id
= in_processor_info
->l3_cache_id
;
995 this_cpu_datap
->cpu_l3_size
= in_processor_info
->l3_cache_size
;
997 this_cpu_datap
->cluster_master
= is_boot_cpu
;
999 pset
= pset_find(in_processor_info
->cluster_id
, processor_pset(master_processor
));
1000 assert(pset
!= NULL
);
1001 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
);
1004 processor_init((struct processor
*)this_cpu_datap
->cpu_processor
,
1005 this_cpu_datap
->cpu_number
, pset
);
1007 if (this_cpu_datap
->cpu_l2_access_penalty
) {
1009 * Cores that have a non-zero L2 access penalty compared
1010 * to the boot processor should be de-prioritized by the
1011 * scheduler, so that threads use the cores with better L2
1014 processor_set_primary(this_cpu_datap
->cpu_processor
,
1019 *processor_out
= this_cpu_datap
->cpu_processor
;
1020 *ipi_handler
= cpu_signal_handler
;
1021 if (in_processor_info
->idle_tickle
!= (idle_tickle_t
*) NULL
)
1022 *in_processor_info
->idle_tickle
= (idle_tickle_t
) cpu_idle_tickle
;
1025 if (kpc_register_cpu(this_cpu_datap
) != TRUE
)
1026 goto processor_register_error
;
1030 early_random_cpu_init(this_cpu_datap
->cpu_number
);
1031 // now let next CPU register itself
1032 OSIncrementAtomic((SInt32
*)&real_ncpus
);
1035 return KERN_SUCCESS
;
1037 processor_register_error
:
1039 kpc_unregister_cpu(this_cpu_datap
);
1042 cpu_data_free(this_cpu_datap
);
1044 return KERN_FAILURE
;
1048 ml_init_arm_debug_interface(
1049 void * in_cpu_datap
,
1050 vm_offset_t virt_address
)
1052 ((cpu_data_t
*)in_cpu_datap
)->cpu_debug_interface_map
= virt_address
;
1057 * Routine: init_ast_check
1062 __unused processor_t processor
)
1067 * Routine: cause_ast_check
1072 processor_t processor
)
1074 if (current_processor() != processor
) {
1075 cpu_signal(processor_to_cpu_datap(processor
), SIGPast
, (void *)NULL
, (void *)NULL
);
1076 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_REMOTE_AST
), processor
->cpu_id
, 1 /* ast */, 0, 0, 0);
1080 extern uint32_t cpu_idle_count
;
1082 void ml_get_power_state(boolean_t
*icp
, boolean_t
*pidlep
) {
1083 *icp
= ml_at_interrupt_context();
1084 *pidlep
= (cpu_idle_count
== real_ncpus
);
1088 * Routine: ml_cause_interrupt
1089 * Function: Generate a fake interrupt
1092 ml_cause_interrupt(void)
1094 return; /* BS_XXX */
1097 /* Map memory map IO space */
1100 vm_offset_t phys_addr
,
1103 return (io_map(phys_addr
, size
, VM_WIMG_IO
));
1108 vm_offset_t phys_addr
,
1111 return (io_map(phys_addr
, size
, VM_WIMG_WCOMB
));
1114 /* boot memory allocation */
1117 __unused vm_size_t size
)
1119 return ((vm_offset_t
) NULL
);
1124 vm_offset_t phys_addr
,
1127 return pmap_map_high_window_bd(phys_addr
, len
, VM_PROT_READ
| VM_PROT_WRITE
);
1134 return phystokv(paddr
);
1141 return phystokv(vaddr
+ vm_kernel_slide
- gVirtBase
+ gPhysBase
);
1148 return (ml_static_vtop(vaddr
) - gPhysBase
+ gVirtBase
- vm_kernel_slide
) ;
1151 extern tt_entry_t
*arm_kva_to_tte(vm_offset_t va
);
1155 vm_offset_t vaddr
, /* kernel virtual address */
1159 pt_entry_t arm_prot
= 0;
1160 pt_entry_t arm_block_prot
= 0;
1161 vm_offset_t vaddr_cur
;
1163 kern_return_t result
= KERN_SUCCESS
;
1165 if (vaddr
< VM_MIN_KERNEL_ADDRESS
) {
1166 panic("ml_static_protect(): %p < %p", (void *) vaddr
, (void *) VM_MIN_KERNEL_ADDRESS
);
1167 return KERN_FAILURE
;
1170 assert((vaddr
& (PAGE_SIZE
- 1)) == 0); /* must be page aligned */
1172 if ((new_prot
& VM_PROT_WRITE
) && (new_prot
& VM_PROT_EXECUTE
)) {
1173 panic("ml_static_protect(): WX request on %p", (void *) vaddr
);
1176 /* Set up the protection bits, and block bits so we can validate block mappings. */
1177 if (new_prot
& VM_PROT_WRITE
) {
1178 arm_prot
|= ARM_PTE_AP(AP_RWNA
);
1179 arm_block_prot
|= ARM_TTE_BLOCK_AP(AP_RWNA
);
1181 arm_prot
|= ARM_PTE_AP(AP_RONA
);
1182 arm_block_prot
|= ARM_TTE_BLOCK_AP(AP_RONA
);
1185 arm_prot
|= ARM_PTE_NX
;
1186 arm_block_prot
|= ARM_TTE_BLOCK_NX
;
1188 if (!(new_prot
& VM_PROT_EXECUTE
)) {
1189 arm_prot
|= ARM_PTE_PNX
;
1190 arm_block_prot
|= ARM_TTE_BLOCK_PNX
;
1193 for (vaddr_cur
= vaddr
;
1194 vaddr_cur
< trunc_page_64(vaddr
+ size
);
1195 vaddr_cur
+= PAGE_SIZE
) {
1196 ppn
= pmap_find_phys(kernel_pmap
, vaddr_cur
);
1197 if (ppn
!= (vm_offset_t
) NULL
) {
1203 tte2
= arm_kva_to_tte(vaddr_cur
);
1205 if (((*tte2
) & ARM_TTE_TYPE_MASK
) != ARM_TTE_TYPE_TABLE
) {
1206 if ((((*tte2
) & ARM_TTE_TYPE_MASK
) == ARM_TTE_TYPE_BLOCK
) &&
1207 ((*tte2
& (ARM_TTE_BLOCK_NXMASK
| ARM_TTE_BLOCK_PNXMASK
| ARM_TTE_BLOCK_APMASK
)) == arm_block_prot
)) {
1209 * We can support ml_static_protect on a block mapping if the mapping already has
1210 * the desired protections. We still want to run checks on a per-page basis.
1215 result
= KERN_FAILURE
;
1219 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
)];
1222 if ((ptmp
& ARM_PTE_HINT_MASK
) && ((ptmp
& (ARM_PTE_APMASK
| ARM_PTE_PNXMASK
| ARM_PTE_NXMASK
)) != arm_prot
)) {
1224 * The contiguous hint is similar to a block mapping for ml_static_protect; if the existing
1225 * protections do not match the desired protections, then we will fail (as we cannot update
1226 * this mapping without updating other mappings as well).
1228 result
= KERN_FAILURE
;
1232 __unreachable_ok_push
1233 if (TEST_PAGE_RATIO_4
) {
1236 pt_entry_t
*ptep_iter
;
1239 for (i
=0; i
<4; i
++, ptep_iter
++) {
1240 /* Note that there is a hole in the HINT sanity checking here. */
1243 /* We only need to update the page tables if the protections do not match. */
1244 if ((ptmp
& (ARM_PTE_APMASK
| ARM_PTE_PNXMASK
| ARM_PTE_NXMASK
)) != arm_prot
) {
1245 ptmp
= (ptmp
& ~(ARM_PTE_APMASK
| ARM_PTE_PNXMASK
| ARM_PTE_NXMASK
)) | arm_prot
;
1250 #ifndef __ARM_L1_PTW__
1251 FlushPoC_DcacheRegion( trunc_page_32(pte_p
), 4*sizeof(*pte_p
));
1256 /* We only need to update the page tables if the protections do not match. */
1257 if ((ptmp
& (ARM_PTE_APMASK
| ARM_PTE_PNXMASK
| ARM_PTE_NXMASK
)) != arm_prot
) {
1258 ptmp
= (ptmp
& ~(ARM_PTE_APMASK
| ARM_PTE_PNXMASK
| ARM_PTE_NXMASK
)) | arm_prot
;
1262 #ifndef __ARM_L1_PTW__
1263 FlushPoC_DcacheRegion( trunc_page_32(pte_p
), sizeof(*pte_p
));
1266 __unreachable_ok_pop
1270 if (vaddr_cur
> vaddr
) {
1271 assert(((vaddr_cur
- vaddr
) & 0xFFFFFFFF00000000ULL
) == 0);
1272 flush_mmu_tlb_region(vaddr
, (uint32_t)(vaddr_cur
- vaddr
));
1280 * Routine: ml_static_mfree
1288 vm_offset_t vaddr_cur
;
1290 uint32_t freed_pages
= 0;
1292 /* It is acceptable (if bad) to fail to free. */
1293 if (vaddr
< VM_MIN_KERNEL_ADDRESS
)
1296 assert((vaddr
& (PAGE_SIZE
- 1)) == 0); /* must be page aligned */
1298 for (vaddr_cur
= vaddr
;
1299 vaddr_cur
< trunc_page_64(vaddr
+ size
);
1300 vaddr_cur
+= PAGE_SIZE
) {
1302 ppn
= pmap_find_phys(kernel_pmap
, vaddr_cur
);
1303 if (ppn
!= (vm_offset_t
) NULL
) {
1305 * It is not acceptable to fail to update the protections on a page
1306 * we will release to the VM. We need to either panic or continue.
1307 * For now, we'll panic (to help flag if there is memory we can
1310 if (ml_static_protect(vaddr_cur
, PAGE_SIZE
, VM_PROT_WRITE
| VM_PROT_READ
) != KERN_SUCCESS
) {
1311 panic("Failed ml_static_mfree on %p", (void *) vaddr_cur
);
1316 * Must NOT tear down the "V==P" mapping for vaddr_cur as the zone alias scheme
1317 * relies on the persistence of these mappings for all time.
1319 // pmap_remove(kernel_pmap, (addr64_t) vaddr_cur, (addr64_t) (vaddr_cur + PAGE_SIZE));
1322 vm_page_create(ppn
, (ppn
+ 1));
1326 vm_page_lockspin_queues();
1327 vm_page_wire_count
-= freed_pages
;
1328 vm_page_wire_count_initial
-= freed_pages
;
1329 vm_page_unlock_queues();
1331 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
);
1336 /* virtual to physical on wired pages */
1338 ml_vtophys(vm_offset_t vaddr
)
1340 return kvtophys(vaddr
);
1344 * Routine: ml_nofault_copy
1345 * Function: Perform a physical mode copy if the source and destination have
1346 * valid translations in the kernel pmap. If translations are present, they are
1347 * assumed to be wired; e.g., no attempt is made to guarantee that the
1348 * translations obtained remain valid for the duration of the copy process.
1351 ml_nofault_copy(vm_offset_t virtsrc
, vm_offset_t virtdst
, vm_size_t size
)
1353 addr64_t cur_phys_dst
, cur_phys_src
;
1354 vm_size_t count
, nbytes
= 0;
1357 if (!(cur_phys_src
= kvtophys(virtsrc
)))
1359 if (!(cur_phys_dst
= kvtophys(virtdst
)))
1361 if (!pmap_valid_address(trunc_page_64(cur_phys_dst
)) ||
1362 !pmap_valid_address(trunc_page_64(cur_phys_src
)))
1364 count
= PAGE_SIZE
- (cur_phys_src
& PAGE_MASK
);
1365 if (count
> (PAGE_SIZE
- (cur_phys_dst
& PAGE_MASK
)))
1366 count
= PAGE_SIZE
- (cur_phys_dst
& PAGE_MASK
);
1370 bcopy_phys(cur_phys_src
, cur_phys_dst
, count
);
1382 * Routine: ml_validate_nofault
1383 * Function: Validate that ths address range has a valid translations
1384 * in the kernel pmap. If translations are present, they are
1385 * assumed to be wired; i.e. no attempt is made to guarantee
1386 * that the translation persist after the check.
1387 * Returns: TRUE if the range is mapped and will not cause a fault,
1391 boolean_t
ml_validate_nofault(
1392 vm_offset_t virtsrc
, vm_size_t size
)
1394 addr64_t cur_phys_src
;
1398 if (!(cur_phys_src
= kvtophys(virtsrc
)))
1400 if (!pmap_valid_address(trunc_page_64(cur_phys_src
)))
1402 count
= (uint32_t)(PAGE_SIZE
- (cur_phys_src
& PAGE_MASK
));
1404 count
= (uint32_t)size
;
1414 ml_get_bouncepool_info(vm_offset_t
* phys_addr
, vm_size_t
* size
)
1421 active_rt_threads(__unused boolean_t active
)
1425 static void cpu_qos_cb_default(__unused
int urgency
, __unused
uint64_t qos_param1
, __unused
uint64_t qos_param2
) {
1429 cpu_qos_update_t cpu_qos_update
= cpu_qos_cb_default
;
1431 void cpu_qos_update_register(cpu_qos_update_t cpu_qos_cb
) {
1432 if (cpu_qos_cb
!= NULL
) {
1433 cpu_qos_update
= cpu_qos_cb
;
1435 cpu_qos_update
= cpu_qos_cb_default
;
1440 thread_tell_urgency(int urgency
, uint64_t rt_period
, uint64_t rt_deadline
, uint64_t sched_latency __unused
, __unused thread_t nthread
)
1442 SCHED_DEBUG_PLATFORM_KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
,MACH_URGENCY
) | DBG_FUNC_START
, urgency
, rt_period
, rt_deadline
, sched_latency
, 0);
1444 cpu_qos_update(urgency
, rt_period
, rt_deadline
);
1446 SCHED_DEBUG_PLATFORM_KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
,MACH_URGENCY
) | DBG_FUNC_END
, urgency
, rt_period
, rt_deadline
, 0, 0);
1450 machine_run_count(__unused
uint32_t count
)
1455 machine_choose_processor(__unused processor_set_t pset
, processor_t processor
)
1461 vm_offset_t
ml_stack_base(void);
1462 vm_size_t
ml_stack_size(void);
1467 uintptr_t local
= (uintptr_t) &local
;
1468 vm_offset_t intstack_top_ptr
;
1470 intstack_top_ptr
= getCpuDatap()->intstack_top
;
1471 if ((local
< intstack_top_ptr
) && (local
> intstack_top_ptr
- INTSTACK_SIZE
)) {
1472 return intstack_top_ptr
- INTSTACK_SIZE
;
1474 return current_thread()->kernel_stack
;
1480 uintptr_t local
= (uintptr_t) &local
;
1481 vm_offset_t intstack_top_ptr
;
1483 intstack_top_ptr
= getCpuDatap()->intstack_top
;
1484 if ((local
< intstack_top_ptr
) && (local
> intstack_top_ptr
- INTSTACK_SIZE
)) {
1485 return INTSTACK_SIZE
;
1487 return kernel_stack_size
;
1492 boolean_t
machine_timeout_suspended(void) {
1497 ml_interrupt_prewarm(__unused
uint64_t deadline
)
1499 return KERN_FAILURE
;
1503 * Assumes fiq, irq disabled.
1506 ml_set_decrementer(uint32_t dec_value
)
1508 cpu_data_t
*cdp
= getCpuDatap();
1510 assert(ml_get_interrupts_enabled() == FALSE
);
1511 cdp
->cpu_decrementer
= dec_value
;
1513 if (cdp
->cpu_set_decrementer_func
) {
1514 ((void (*)(uint32_t))cdp
->cpu_set_decrementer_func
)(dec_value
);
1516 __asm__
volatile("msr CNTP_TVAL_EL0, %0" : : "r"((uint64_t)dec_value
));
1520 uint64_t ml_get_hwclock()
1524 // ISB required by ARMV7C.b section B8.1.2 & ARMv8 section D6.1.2
1525 // "Reads of CNTPCT[_EL0] can occur speculatively and out of order relative
1526 // to other instructions executed on the same processor."
1527 __asm__
volatile("isb\n"
1528 "mrs %0, CNTPCT_EL0"
1537 return (ml_get_hwclock() + getCpuDatap()->cpu_base_timebase
);
1541 ml_get_decrementer()
1543 cpu_data_t
*cdp
= getCpuDatap();
1546 assert(ml_get_interrupts_enabled() == FALSE
);
1548 if (cdp
->cpu_get_decrementer_func
) {
1549 dec
= ((uint32_t (*)(void))cdp
->cpu_get_decrementer_func
)();
1553 __asm__
volatile("mrs %0, CNTP_TVAL_EL0" : "=r"(wide_val
));
1554 dec
= (uint32_t)wide_val
;
1555 assert(wide_val
== (uint64_t)dec
);
1562 ml_get_timer_pending()
1566 __asm__
volatile("mrs %0, CNTP_CTL_EL0" : "=r"(cntp_ctl
));
1567 return ((cntp_ctl
& CNTP_CTL_EL0_ISTATUS
) != 0) ? TRUE
: FALSE
;
1571 ml_wants_panic_trap_to_debugger(void)
1573 boolean_t result
= FALSE
;
1578 cache_trap_error(thread_t thread
, vm_map_address_t fault_addr
)
1580 mach_exception_data_type_t exc_data
[2];
1581 arm_saved_state_t
*regs
= get_user_regs(thread
);
1583 set_saved_state_far(regs
, fault_addr
);
1585 exc_data
[0] = KERN_INVALID_ADDRESS
;
1586 exc_data
[1] = fault_addr
;
1588 exception_triage(EXC_BAD_ACCESS
, exc_data
, 2);
1592 cache_trap_recover()
1594 vm_map_address_t fault_addr
;
1596 __asm__
volatile("mrs %0, FAR_EL1" : "=r"(fault_addr
));
1598 cache_trap_error(current_thread(), fault_addr
);
1602 dcache_flush_trap(vm_map_address_t start
, vm_map_size_t size
)
1604 vm_map_address_t end
= start
+ size
;
1605 thread_t thread
= current_thread();
1606 vm_offset_t old_recover
= thread
->recover
;
1609 if (task_has_64Bit_addr(current_task())) {
1610 if (end
> MACH_VM_MAX_ADDRESS
) {
1611 cache_trap_error(thread
, end
& ((1 << ARM64_CLINE_SHIFT
) - 1));
1614 if (end
> VM_MAX_ADDRESS
) {
1615 cache_trap_error(thread
, end
& ((1 << ARM64_CLINE_SHIFT
) - 1));
1620 cache_trap_error(thread
, start
& ((1 << ARM64_CLINE_SHIFT
) - 1));
1623 /* Set recovery function */
1624 thread
->recover
= (vm_address_t
)cache_trap_recover
;
1627 * We're coherent on Apple ARM64 CPUs, so this could be a nop. However,
1628 * if the region given us is bad, it would be good to catch it and
1629 * crash, ergo we still do the flush.
1631 FlushPoC_DcacheRegion(start
, (uint32_t)size
);
1633 /* Restore recovery function */
1634 thread
->recover
= old_recover
;
1636 /* Return (caller does exception return) */
1640 icache_invalidate_trap(vm_map_address_t start
, vm_map_size_t size
)
1642 vm_map_address_t end
= start
+ size
;
1643 thread_t thread
= current_thread();
1644 vm_offset_t old_recover
= thread
->recover
;
1647 if (task_has_64Bit_addr(current_task())) {
1648 if (end
> MACH_VM_MAX_ADDRESS
) {
1649 cache_trap_error(thread
, end
& ((1 << ARM64_CLINE_SHIFT
) - 1));
1652 if (end
> VM_MAX_ADDRESS
) {
1653 cache_trap_error(thread
, end
& ((1 << ARM64_CLINE_SHIFT
) - 1));
1658 cache_trap_error(thread
, start
& ((1 << ARM64_CLINE_SHIFT
) - 1));
1661 /* Set recovery function */
1662 thread
->recover
= (vm_address_t
)cache_trap_recover
;
1664 CleanPoU_DcacheRegion(start
, (uint32_t) size
);
1666 /* Invalidate iCache to point of unification */
1667 #if __ARM_IC_NOALIAS_ICACHE__
1668 InvalidatePoU_IcacheRegion(start
, (uint32_t)size
);
1670 InvalidatePoU_Icache();
1673 /* Restore recovery function */
1674 thread
->recover
= old_recover
;
1676 /* Return (caller does exception return) */
1679 __attribute__((noreturn
))
1681 platform_syscall(arm_saved_state_t
*state
)
1685 #define platform_syscall_kprintf(x...) /* kprintf("platform_syscall: " x) */
1687 code
= (uint32_t)get_saved_state_reg(state
, 3);
1691 platform_syscall_kprintf("icache flush requested.\n");
1692 icache_invalidate_trap(get_saved_state_reg(state
, 0), get_saved_state_reg(state
, 1));
1696 platform_syscall_kprintf("dcache flush requested.\n");
1697 dcache_flush_trap(get_saved_state_reg(state
, 0), get_saved_state_reg(state
, 1));
1701 platform_syscall_kprintf("set cthread self.\n");
1702 thread_set_cthread_self(get_saved_state_reg(state
, 0));
1706 platform_syscall_kprintf("get cthread self.\n");
1707 set_saved_state_reg(state
, 0, thread_get_cthread_self());
1710 platform_syscall_kprintf("unknown: %d\n", code
);
1714 thread_exception_return();
1718 _enable_timebase_event_stream(uint32_t bit_index
)
1720 uint64_t cntkctl
; /* One wants to use 32 bits, but "mrs" prefers it this way */
1722 if (bit_index
>= 64) {
1723 panic("%s: invalid bit index (%u)", __FUNCTION__
, bit_index
);
1726 __asm__
volatile ("mrs %0, CNTKCTL_EL1" : "=r"(cntkctl
));
1728 cntkctl
|= (bit_index
<< CNTKCTL_EL1_EVENTI_SHIFT
);
1729 cntkctl
|= CNTKCTL_EL1_EVNTEN
;
1730 cntkctl
|= CNTKCTL_EL1_EVENTDIR
; /* 1->0; why not? */
1733 * If the SOC supports it (and it isn't broken), enable
1734 * EL0 access to the physical timebase register.
1736 if (user_timebase_allowed()) {
1737 cntkctl
|= CNTKCTL_EL1_PL0PCTEN
;
1740 __asm__
volatile ("msr CNTKCTL_EL1, %0" : : "r"(cntkctl
));
1744 * Turn timer on, unmask that interrupt.
1747 _enable_virtual_timer(void)
1749 uint64_t cntvctl
= CNTP_CTL_EL0_ENABLE
; /* One wants to use 32 bits, but "mrs" prefers it this way */
1751 __asm__
volatile ("msr CNTP_CTL_EL0, %0" : : "r"(cntvctl
));
1755 fiq_context_init(boolean_t enable_fiq __unused
)
1757 _enable_timebase_event_stream(fiq_eventi
);
1759 /* Interrupts still disabled. */
1760 assert(ml_get_interrupts_enabled() == FALSE
);
1761 _enable_virtual_timer();
1765 fiq_context_bootstrap(boolean_t enable_fiq
)
1767 #if defined(APPLE_ARM64_ARCH_FAMILY) || defined(BCM2837)
1768 /* Could fill in our own ops here, if we needed them */
1769 uint64_t ticks_per_sec
, ticks_per_event
, events_per_sec
;
1772 ticks_per_sec
= gPEClockFrequencyInfo
.timebase_frequency_hz
;
1773 #if defined(ARM_BOARD_WFE_TIMEOUT_NS)
1774 events_per_sec
= 1000000000 / ARM_BOARD_WFE_TIMEOUT_NS
;
1776 /* Default to 1usec (or as close as we can get) */
1777 events_per_sec
= 1000000;
1779 ticks_per_event
= ticks_per_sec
/ events_per_sec
;
1780 bit_index
= flsll(ticks_per_event
) - 1; /* Highest bit set */
1782 /* Round up to power of two */
1783 if ((ticks_per_event
& ((1 << bit_index
) - 1)) != 0)
1787 * The timer can only trigger on rising or falling edge,
1788 * not both; we don't care which we trigger on, but we
1789 * do need to adjust which bit we are interested in to
1795 fiq_eventi
= bit_index
;
1797 #error Need a board configuration.
1799 fiq_context_init(enable_fiq
);
1803 ml_delay_should_spin(uint64_t interval
)
1805 cpu_data_t
*cdp
= getCpuDatap();
1807 if (cdp
->cpu_idle_latency
) {
1808 return (interval
< cdp
->cpu_idle_latency
) ? TRUE
: FALSE
;
1811 * Early boot, latency is unknown. Err on the side of blocking,
1812 * which should always be safe, even if slow
1819 ml_delay_on_yield(void)
1823 boolean_t
ml_thread_is64bit(thread_t thread
) {
1824 return (thread_is_64bit_addr(thread
));
1827 void ml_timer_evaluate(void) {
1831 ml_timer_forced_evaluation(void) {
1836 ml_energy_stat(thread_t t
) {
1837 return t
->machine
.energy_estimate_nj
;
1842 ml_gpu_stat_update(__unused
uint64_t gpu_ns_delta
) {
1845 * For now: update the resource coalition stats of the
1846 * current thread's coalition
1848 task_coalition_update_gpu_stats(current_task(), gpu_ns_delta
);
1853 ml_gpu_stat(__unused thread_t t
) {
1857 #if !CONFIG_SKIP_PRECISE_USER_KERNEL_TIME
1859 timer_state_event(boolean_t switch_to_kernel
)
1861 thread_t thread
= current_thread();
1862 if (!thread
->precise_user_kernel_time
) return;
1864 processor_data_t
*pd
= &getCpuDatap()->cpu_processor
->processor_data
;
1865 uint64_t now
= ml_get_timebase();
1867 timer_stop(pd
->current_state
, now
);
1868 pd
->current_state
= (switch_to_kernel
) ? &pd
->system_state
: &pd
->user_state
;
1869 timer_start(pd
->current_state
, now
);
1871 timer_stop(pd
->thread_timer
, now
);
1872 pd
->thread_timer
= (switch_to_kernel
) ? &thread
->system_timer
: &thread
->user_timer
;
1873 timer_start(pd
->thread_timer
, now
);
1877 timer_state_event_user_to_kernel(void)
1879 timer_state_event(TRUE
);
1883 timer_state_event_kernel_to_user(void)
1885 timer_state_event(FALSE
);
1887 #endif /* !CONFIG_SKIP_PRECISE_USER_KERNEL_TIME */
1890 * The following are required for parts of the kernel
1891 * that cannot resolve these functions as inlines:
1893 extern thread_t
current_act(void);
1897 return current_thread_fast();
1900 #undef current_thread
1901 extern thread_t
current_thread(void);
1903 current_thread(void)
1905 return current_thread_fast();
1915 ex_cb_info_t ex_cb_info
[EXCB_CLASS_MAX
];
1918 * Callback registration
1919 * Currently we support only one registered callback per class but
1920 * it should be possible to support more callbacks
1922 kern_return_t
ex_cb_register(
1923 ex_cb_class_t cb_class
,
1927 ex_cb_info_t
*pInfo
= &ex_cb_info
[cb_class
];
1929 if ((NULL
== cb
) || (cb_class
>= EXCB_CLASS_MAX
))
1931 return KERN_INVALID_VALUE
;
1934 if (NULL
== pInfo
->cb
)
1937 pInfo
->refcon
= refcon
;
1938 return KERN_SUCCESS
;
1940 return KERN_FAILURE
;
1944 * Called internally by platform kernel to invoke the registered callback for class
1946 ex_cb_action_t
ex_cb_invoke(
1947 ex_cb_class_t cb_class
,
1950 ex_cb_info_t
*pInfo
= &ex_cb_info
[cb_class
];
1951 ex_cb_state_t state
= {far
};
1953 if (cb_class
>= EXCB_CLASS_MAX
)
1955 panic("Invalid exception callback class 0x%x\n", cb_class
);
1960 return pInfo
->cb(cb_class
, pInfo
->refcon
, &state
);
1962 return EXCB_ACTION_NONE
;