2 * Copyright (c) 2007-2015 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 <machine/asm.h>
30 #include <arm64/exception_asm.h>
31 #include <arm64/machine_machdep.h>
32 #include <arm64/pac_asm.h>
33 #include <arm64/proc_reg.h>
35 #include <pexpert/arm64/board_config.h>
36 #include <sys/errno.h>
40 #if defined(HAS_APPLE_PAC)
42 .macro SET_KERN_KEY dst, apctl_el1
43 orr \dst, \apctl_el1, #APCTL_EL1_KernKeyEn
46 .macro CLEAR_KERN_KEY dst, apctl_el1
47 and \dst, \apctl_el1, #~APCTL_EL1_KernKeyEn
51 * uint64_t ml_enable_user_jop_key(uint64_t user_jop_key)
54 .globl EXT(ml_enable_user_jop_key)
55 LEXT(ml_enable_user_jop_key)
58 ldr x2, [x2, ACT_CPUDATAP]
59 ldr x0, [x2, CPU_JOP_KEY]
62 b.eq Lskip_program_el0_jop_key
64 * We can safely write to the JOP key registers without updating
65 * current_cpu_datap()->jop_key. The complementary
66 * ml_disable_user_jop_key() call will put back the old value. Interrupts
67 * are also disabled, so nothing else will read this field in the meantime.
69 SET_JOP_KEY_REGISTERS x1, x2
70 Lskip_program_el0_jop_key:
73 * if (cpu has APCTL_EL1.UserKeyEn) {
74 * set APCTL_EL1.KernKeyEn // KERNKey is mixed into EL0 keys
76 * clear APCTL_EL1.KernKeyEn // KERNKey is not mixed into EL0 keys
79 mrs x1, ARM64_REG_APCTL_EL1
80 #if defined(HAS_APCTL_EL1_USERKEYEN)
85 msr ARM64_REG_APCTL_EL1, x1
90 * void ml_disable_user_jop_key(uint64_t user_jop_key, uint64_t saved_jop_state)
93 .globl EXT(ml_disable_user_jop_key)
94 LEXT(ml_disable_user_jop_key)
96 b.eq Lskip_program_prev_jop_key
97 SET_JOP_KEY_REGISTERS x1, x2
98 Lskip_program_prev_jop_key:
101 * if (cpu has APCTL_EL1.UserKeyEn) {
102 * clear APCTL_EL1.KernKeyEn // KERNKey is not mixed into EL1 keys
104 * set APCTL_EL1.KernKeyEn // KERNKey is mixed into EL1 keys
107 mrs x1, ARM64_REG_APCTL_EL1
108 #if defined(HAS_APCTL_EL1_USERKEYEN)
109 CLEAR_KERN_KEY x1, x1
113 msr ARM64_REG_APCTL_EL1, x1
117 #endif /* defined(HAS_APPLE_PAC) */
122 * void set_bp_ret(void)
123 * Helper function to enable branch predictor state retention
128 .globl EXT(set_bp_ret)
130 // Load bpret boot-arg
131 adrp x14, EXT(bp_ret)@page
132 add x14, x14, EXT(bp_ret)@pageoff
135 mrs x13, ARM64_REG_ACC_CFG
136 and x13, x13, (~(ARM64_REG_ACC_CFG_bpSlp_mask << ARM64_REG_ACC_CFG_bpSlp_shift))
137 and x14, x14, #(ARM64_REG_ACC_CFG_bpSlp_mask)
138 orr x13, x13, x14, lsl #(ARM64_REG_ACC_CFG_bpSlp_shift)
139 msr ARM64_REG_ACC_CFG, x13
146 .globl EXT(set_nex_pg)
149 // Skip if this isn't a p-core; NEX powergating isn't available for e-cores
150 and x14, x14, #(MPIDR_PNE)
151 cbz x14, Lnex_pg_done
153 // Set the SEG-recommended value of 12 additional reset cycles
154 HID_INSERT_BITS ARM64_REG_HID13, ARM64_REG_HID13_RstCyc_mask, ARM64_REG_HID13_RstCyc_val, x13
155 HID_SET_BITS ARM64_REG_HID14, ARM64_REG_HID14_NexPwgEn, x13
162 /* uint32_t get_fpscr(void):
163 * Returns (FPSR | FPCR).
166 .globl EXT(get_fpscr)
169 mrs x1, FPSR // Grab FPSR
170 mov x4, #(FPSR_MASK & 0xFFFF)
171 mov x5, #(FPSR_MASK & 0xFFFF0000)
173 and x1, x1, x0 // Be paranoid, and clear bits we expect to
175 mrs x2, FPCR // Grab FPCR
176 mov x4, #(FPCR_MASK & 0xFFFF)
177 mov x5, #(FPCR_MASK & 0xFFFF0000)
179 and x2, x2, x0 // Be paranoid, and clear bits we expect to
181 orr x0, x1, x2 // OR them to get FPSCR equivalent state
187 .globl EXT(set_fpscr)
188 /* void set_fpscr(uint32_t value):
189 * Set the FPCR and FPSR registers, based on the given value; a
190 * noteworthy point is that unlike 32-bit mode, 64-bit mode FPSR
191 * and FPCR are not responsible for condition codes.
195 mov x4, #(FPSR_MASK & 0xFFFF)
196 mov x5, #(FPSR_MASK & 0xFFFF0000)
198 and x1, x1, x0 // Clear the bits that don't apply to FPSR
199 mov x4, #(FPCR_MASK & 0xFFFF)
200 mov x5, #(FPCR_MASK & 0xFFFF0000)
202 and x2, x2, x0 // Clear the bits that don't apply to FPCR
203 msr FPSR, x1 // Write FPCR
204 msr FPCR, x2 // Write FPSR
205 dsb ish // FPCR requires synchronization
210 * void update_mdscr(unsigned long clear, unsigned long set)
211 * Clears and sets the specified bits in MDSCR_EL1.
213 * Setting breakpoints in EL1 is effectively a KTRR bypass. The ability to do so is
214 * controlled by MDSCR.KDE. The MSR to set MDSCR must be present to allow
215 * self-hosted user mode debug. Any checks before the MRS can be skipped with ROP,
216 * so we need to put the checks after the MRS where they can't be skipped. That
217 * still leaves a small window if a breakpoint is set on the instruction
218 * immediately after the MRS. To handle that, we also do a check and then set of
219 * the breakpoint control registers. This allows us to guarantee that a given
220 * core will never have both KDE set and a breakpoint targeting EL1.
222 * If KDE gets set, unset it and then panic
225 .globl EXT(update_mdscr)
234 #if defined(CONFIG_KERNEL_INTEGRITY)
236 * verify KDE didn't get set (including via ROP)
237 * If set, clear it and then panic
243 b.ne Lupdate_mdscr_panic
248 adrp x0, Lupdate_mdscr_panic_str@page
249 add x0, x0, Lupdate_mdscr_panic_str@pageoff
253 Lupdate_mdscr_panic_str:
254 .asciz "MDSCR.KDE was set"
258 * Set MMU Translation Table Base Alternate
262 .globl EXT(set_mmu_ttb_alternate)
263 LEXT(set_mmu_ttb_alternate)
265 #if defined(KERNEL_INTEGRITY_KTRR)
267 bl EXT(pinst_set_ttbr1)
270 #if defined(HAS_VMSA_LOCK)
271 #if DEBUG || DEVELOPMENT
272 mrs x1, ARM64_REG_VMSA_LOCK_EL1
273 and x1, x1, #(VMSA_LOCK_TTBR1_EL1)
274 cbnz x1, L_set_locked_reg_panic
275 #endif /* DEBUG || DEVELOPMENT */
276 #endif /* defined(HAS_VMSA_LOCK) */
278 #endif /* defined(KERNEL_INTEGRITY_KTRR) */
283 .section __PPLTEXT,__text,regular,pure_instructions
288 .globl EXT(set_mmu_ttb)
290 #if __ARM_KERNEL_PROTECT__
291 /* All EL1-mode ASIDs are odd. */
292 orr x0, x0, #(1 << TTBR_ASID_SHIFT)
293 #endif /* __ARM_KERNEL_PROTECT__ */
303 .globl EXT(ml_get_ppl_cpu_data)
304 LEXT(ml_get_ppl_cpu_data)
305 LOAD_PMAP_CPU_DATA x0, x1, x2
310 * set AUX control register
314 .globl EXT(set_aux_control)
315 LEXT(set_aux_control)
317 // Synchronize system
321 #if __ARM_KERNEL_PROTECT__
324 .globl EXT(set_vbar_el1)
326 #if defined(KERNEL_INTEGRITY_KTRR)
327 b EXT(pinst_set_vbar)
332 #endif /* __ARM_KERNEL_PROTECT__ */
334 #if defined(HAS_VMSA_LOCK)
337 .globl EXT(vmsa_lock)
340 mov x1, #(VMSA_LOCK_SCTLR_M_BIT)
341 #if __ARM_MIXED_PAGE_SIZE__
342 mov x0, #(VMSA_LOCK_TTBR1_EL1 | VMSA_LOCK_VBAR_EL1)
344 mov x0, #(VMSA_LOCK_TTBR1_EL1 | VMSA_LOCK_TCR_EL1 | VMSA_LOCK_VBAR_EL1)
347 msr ARM64_REG_VMSA_LOCK_EL1, x0
350 #endif /* defined(HAS_VMSA_LOCK) */
353 * set translation control register
359 #if defined(APPLE_ARM64_ARCH_FAMILY)
360 #if DEBUG || DEVELOPMENT
361 // Assert that T0Z is always equal to T1Z
362 eor x1, x0, x0, lsr #(TCR_T1SZ_SHIFT - TCR_T0SZ_SHIFT)
363 and x1, x1, #(TCR_TSZ_MASK << TCR_T0SZ_SHIFT)
364 cbnz x1, L_set_tcr_panic
365 #endif /* DEBUG || DEVELOPMENT */
366 #endif /* defined(APPLE_ARM64_ARCH_FAMILY) */
367 #if defined(KERNEL_INTEGRITY_KTRR)
369 bl EXT(pinst_set_tcr)
372 #if defined(HAS_VMSA_LOCK)
373 #if DEBUG || DEVELOPMENT
374 // assert TCR unlocked
375 mrs x1, ARM64_REG_VMSA_LOCK_EL1
376 and x1, x1, #(VMSA_LOCK_TCR_EL1)
377 cbnz x1, L_set_locked_reg_panic
378 #endif /* DEBUG || DEVELOPMENT */
379 #endif /* defined(HAS_VMSA_LOCK) */
381 #endif /* defined(KERNEL_INTRITY_KTRR) */
385 #if DEBUG || DEVELOPMENT
390 adr x0, L_set_tcr_panic_str
393 L_set_locked_reg_panic:
397 adr x0, L_set_locked_reg_panic_str
402 .asciz "set_tcr: t0sz, t1sz not equal (%llx)\n"
405 L_set_locked_reg_panic_str:
406 .asciz "attempt to set locked register: (%llx)\n"
407 #endif /* DEBUG || DEVELOPMENT */
410 * MMU kernel virtual to physical address translation
414 .globl EXT(mmu_kvtop)
416 mrs x2, DAIF // Load current DAIF
417 msr DAIFSet, #(DAIFSC_IRQF | DAIFSC_FIQF) // Disable IRQ
418 at s1e1r, x0 // Translation Stage 1 EL1
420 mrs x1, PAR_EL1 // Read result
421 msr DAIF, x2 // Restore interrupt state
422 tbnz x1, #0, L_mmu_kvtop_invalid // Test Translation not valid
423 bfm x1, x0, #0, #11 // Add page offset
424 and x0, x1, #0x0000ffffffffffff // Clear non-address bits
427 mov x0, #0 // Return invalid
431 * MMU user virtual to physical address translation
435 .globl EXT(mmu_uvtop)
437 lsr x8, x0, #56 // Extract top byte
438 cbnz x8, L_mmu_uvtop_invalid // Tagged pointers are invalid
439 mrs x2, DAIF // Load current DAIF
440 msr DAIFSet, #(DAIFSC_IRQF | DAIFSC_FIQF) // Disable IRQ
441 at s1e0r, x0 // Translation Stage 1 EL0
443 mrs x1, PAR_EL1 // Read result
444 msr DAIF, x2 // Restore interrupt state
445 tbnz x1, #0, L_mmu_uvtop_invalid // Test Translation not valid
446 bfm x1, x0, #0, #11 // Add page offset
447 and x0, x1, #0x0000ffffffffffff // Clear non-address bits
450 mov x0, #0 // Return invalid
454 * MMU kernel virtual to physical address preflight write access
458 .globl EXT(mmu_kvtop_wpreflight)
459 LEXT(mmu_kvtop_wpreflight)
460 mrs x2, DAIF // Load current DAIF
461 msr DAIFSet, #(DAIFSC_IRQF | DAIFSC_FIQF) // Disable IRQ
462 at s1e1w, x0 // Translation Stage 1 EL1
463 mrs x1, PAR_EL1 // Read result
464 msr DAIF, x2 // Restore interrupt state
465 tbnz x1, #0, L_mmu_kvtop_wpreflight_invalid // Test Translation not valid
466 bfm x1, x0, #0, #11 // Add page offset
467 and x0, x1, #0x0000ffffffffffff // Clear non-address bits
469 L_mmu_kvtop_wpreflight_invalid:
470 mov x0, #0 // Return invalid
474 * SET_RECOVERY_HANDLER
476 * Sets up a page fault recovery handler. This macro clobbers x16 and x17.
478 * label - recovery label
479 * tpidr - persisted thread pointer
480 * old_handler - persisted recovery handler
481 * label_in_adr_range - whether \label is within 1 MB of PC
483 .macro SET_RECOVERY_HANDLER label, tpidr=x16, old_handler=x10, label_in_adr_range=0
484 // Note: x16 and x17 are designated for use as temporaries in
485 // interruptible PAC routines. DO NOT CHANGE THESE REGISTER ASSIGNMENTS.
486 .if \label_in_adr_range==1 // Load the recovery handler address
489 adrp x17, \label@page
490 add x17, x17, \label@pageoff
492 #if defined(HAS_APPLE_PAC)
494 add x16, x16, TH_RECOVER
495 movk x16, #PAC_DISCRIMINATOR_RECOVER, lsl 48
496 pacia x17, x16 // Sign with IAKey + blended discriminator
499 mrs \tpidr, TPIDR_EL1 // Load thread pointer
500 ldr \old_handler, [\tpidr, TH_RECOVER] // Save previous recovery handler
501 str x17, [\tpidr, TH_RECOVER] // Set new signed recovery handler
505 * CLEAR_RECOVERY_HANDLER
507 * Clears page fault handler set by SET_RECOVERY_HANDLER
509 * tpidr - thread pointer saved by SET_RECOVERY_HANDLER
510 * old_handler - old recovery handler saved by SET_RECOVERY_HANDLER
512 .macro CLEAR_RECOVERY_HANDLER tpidr=x16, old_handler=x10
513 str \old_handler, [\tpidr, TH_RECOVER] // Restore the previous recovery handler
520 CLEAR_RECOVERY_HANDLER
521 mov x0, #EFAULT // Return an EFAULT error
526 * int _bcopyin(const char *src, char *dst, vm_size_t len)
534 SET_RECOVERY_HANDLER copyio_error
535 /* If len is less than 16 bytes, just do a bytewise copy */
540 /* 16 bytes at a time */
541 ldp x3, x4, [x0], #16
542 stp x3, x4, [x1], #16
545 /* Fixup the len and test for completion */
554 CLEAR_RECOVERY_HANDLER
560 * int _copyin_atomic32(const char *src, uint32_t *dst)
564 .globl EXT(_copyin_atomic32)
565 LEXT(_copyin_atomic32)
568 SET_RECOVERY_HANDLER copyio_error
572 CLEAR_RECOVERY_HANDLER
577 * int _copyin_atomic32_wait_if_equals(const char *src, uint32_t value)
581 .globl EXT(_copyin_atomic32_wait_if_equals)
582 LEXT(_copyin_atomic32_wait_if_equals)
585 SET_RECOVERY_HANDLER copyio_error
594 CLEAR_RECOVERY_HANDLER
599 * int _copyin_atomic64(const char *src, uint32_t *dst)
603 .globl EXT(_copyin_atomic64)
604 LEXT(_copyin_atomic64)
607 SET_RECOVERY_HANDLER copyio_error
611 CLEAR_RECOVERY_HANDLER
617 * int _copyout_atomic32(uint32_t value, char *dst)
621 .globl EXT(_copyout_atomic32)
622 LEXT(_copyout_atomic32)
625 SET_RECOVERY_HANDLER copyio_error
628 CLEAR_RECOVERY_HANDLER
633 * int _copyout_atomic64(uint64_t value, char *dst)
637 .globl EXT(_copyout_atomic64)
638 LEXT(_copyout_atomic64)
641 SET_RECOVERY_HANDLER copyio_error
644 CLEAR_RECOVERY_HANDLER
650 * int _bcopyout(const char *src, char *dst, vm_size_t len)
654 .globl EXT(_bcopyout)
658 SET_RECOVERY_HANDLER copyio_error
659 /* If len is less than 16 bytes, just do a bytewise copy */
664 /* 16 bytes at a time */
665 ldp x3, x4, [x0], #16
666 stp x3, x4, [x1], #16
669 /* Fixup the len and test for completion */
678 CLEAR_RECOVERY_HANDLER
685 * const user_addr_t user_addr,
692 .globl EXT(_bcopyinstr)
696 SET_RECOVERY_HANDLER Lcopyinstr_error, label_in_adr_range=1
697 mov x4, #0 // x4 - total bytes copied
699 ldrb w5, [x0], #1 // Load a byte from the user source
700 strb w5, [x1], #1 // Store a byte to the kernel dest
701 add x4, x4, #1 // Increment bytes copied
702 cbz x5, Lcopyinstr_done // If this byte is null, we're done
703 cmp x4, x2 // If we're out of space, return an error
706 mov x5, #ENAMETOOLONG // Set current byte to error code for later return
708 str x4, [x3] // Return number of bytes copied
709 mov x0, x5 // Set error code (0 on success, ENAMETOOLONG on failure)
712 mov x0, #EFAULT // Return EFAULT on error
714 CLEAR_RECOVERY_HANDLER
719 * int copyinframe(const vm_address_t frame_addr, char *kernel_addr, bool is64bit)
721 * Safely copy sixteen bytes (the fixed top of an ARM64 frame) from
722 * either user or kernel memory, or 8 bytes (AArch32) from user only.
724 * x0 : address of frame to copy.
725 * x1 : kernel address at which to store data.
726 * w2 : whether to copy an AArch32 or AArch64 frame.
728 * x5 : temp (kernel virtual base)
730 * x10 : old recovery function (set by SET_RECOVERY_HANDLER)
731 * x12, x13 : backtrace data
732 * x16 : thread pointer (set by SET_RECOVERY_HANDLER)
737 .globl EXT(copyinframe)
741 SET_RECOVERY_HANDLER copyio_error
742 cbnz w2, Lcopyinframe64 // Check frame size
743 adrp x5, EXT(gVirtBase)@page // For 32-bit frame, make sure we're not trying to copy from kernel
744 add x5, x5, EXT(gVirtBase)@pageoff
746 cmp x5, x0 // See if address is in kernel virtual range
747 b.hi Lcopyinframe32 // If below kernel virtual range, proceed.
748 mov w0, #EFAULT // Should never have a 32-bit frame in kernel virtual range
752 ldr x12, [x0] // Copy 8 bytes
754 mov w0, #0 // Success
758 mov x3, VM_MIN_KERNEL_ADDRESS // Check if kernel address
759 orr x9, x0, TBI_MASK // Hide tags in address comparison
760 cmp x9, x3 // If in kernel address range, skip tag test
761 b.hs Lcopyinframe_valid
762 tst x0, TBI_MASK // Detect tagged pointers
763 b.eq Lcopyinframe_valid
764 mov w0, #EFAULT // Tagged address, fail
767 ldp x12, x13, [x0] // Copy 16 bytes
769 mov w0, #0 // Success
772 CLEAR_RECOVERY_HANDLER
778 * uint32_t arm_debug_read_dscr(void)
782 .globl EXT(arm_debug_read_dscr)
783 LEXT(arm_debug_read_dscr)
787 * void arm_debug_set_cp14(arm_debug_state_t *debug_state)
789 * Set debug registers to match the current thread state
790 * (NULL to disable). Assume 6 breakpoints and 2
791 * watchpoints, since that has been the case in all cores
796 .globl EXT(arm_debug_set_cp14)
797 LEXT(arm_debug_set_cp14)
800 #if defined(APPLE_ARM64_ARCH_FAMILY)
802 * Note: still have to ISB before executing wfi!
806 .globl EXT(arm64_prepare_for_sleep)
807 LEXT(arm64_prepare_for_sleep)
810 #if defined(APPLETYPHOON)
811 // <rdar://problem/15827409>
812 HID_SET_BITS ARM64_REG_HID2, ARM64_REG_HID2_disMMUmtlbPrefetch, x9
818 cbnz x0, 1f // Skip if deep_sleep == true
819 // Mask FIQ and IRQ to avoid spurious wakeups
820 mrs x9, ARM64_REG_CYC_OVRD
821 and x9, x9, #(~(ARM64_REG_CYC_OVRD_irq_mask | ARM64_REG_CYC_OVRD_fiq_mask))
822 mov x10, #(ARM64_REG_CYC_OVRD_irq_disable | ARM64_REG_CYC_OVRD_fiq_disable)
824 msr ARM64_REG_CYC_OVRD, x9
829 cbz x0, 1f // Skip if deep_sleep == false
830 #if __ARM_GLOBAL_SLEEP_BIT__
832 mrs x1, ARM64_REG_ACC_OVRD
833 orr x1, x1, #(ARM64_REG_ACC_OVRD_enDeepSleep)
834 and x1, x1, #(~(ARM64_REG_ACC_OVRD_disL2Flush4AccSlp_mask))
835 orr x1, x1, #( ARM64_REG_ACC_OVRD_disL2Flush4AccSlp_deepsleep)
836 and x1, x1, #(~(ARM64_REG_ACC_OVRD_ok2PwrDnSRM_mask))
837 orr x1, x1, #( ARM64_REG_ACC_OVRD_ok2PwrDnSRM_deepsleep)
838 and x1, x1, #(~(ARM64_REG_ACC_OVRD_ok2TrDnLnk_mask))
839 orr x1, x1, #( ARM64_REG_ACC_OVRD_ok2TrDnLnk_deepsleep)
840 and x1, x1, #(~(ARM64_REG_ACC_OVRD_ok2PwrDnCPM_mask))
841 orr x1, x1, #( ARM64_REG_ACC_OVRD_ok2PwrDnCPM_deepsleep)
842 #if HAS_RETENTION_STATE
843 orr x1, x1, #(ARM64_REG_ACC_OVRD_disPioOnWfiCpu)
845 msr ARM64_REG_ACC_OVRD, x1
850 mov x1, ARM64_REG_CYC_CFG_deepSleep
851 msr ARM64_REG_CYC_CFG, x1
855 // Set "OK to power down" (<rdar://problem/12390433>)
856 mrs x9, ARM64_REG_CYC_OVRD
857 orr x9, x9, #(ARM64_REG_CYC_OVRD_ok2pwrdn_force_down)
858 #if HAS_RETENTION_STATE
859 orr x9, x9, #(ARM64_REG_CYC_OVRD_disWfiRetn)
861 msr ARM64_REG_CYC_OVRD, x9
863 #if defined(APPLEMONSOON) || defined(APPLEVORTEX)
865 cbz x9, Lwfi_inst // skip if not p-core
867 /* <rdar://problem/32512947>: Flush the GUPS prefetcher prior to
868 * wfi. A Skye HW bug can cause the GUPS prefetcher on p-cores
869 * to be left with valid entries that fail to drain if a
870 * subsequent wfi is issued. This can prevent the core from
871 * power-gating. For the idle case that is recoverable, but
872 * for the deep-sleep (S2R) case in which cores MUST power-gate,
873 * it can lead to a hang. This can be prevented by disabling
874 * and re-enabling GUPS, which forces the prefetch queue to
875 * drain. This should be done as close to wfi as possible, i.e.
876 * at the very end of arm64_prepare_for_sleep(). */
877 #if defined(APPLEVORTEX)
878 /* <rdar://problem/32821461>: Cyprus A0/A1 parts have a similar
879 * bug in the HSP prefetcher that can be worked around through
880 * the same method mentioned above for Skye. */
882 EXEC_COREALL_REVLO CPU_VERSION_B0, x9, x10
884 mrs x9, ARM64_REG_HID10
885 orr x9, x9, #(ARM64_REG_HID10_DisHwpGups)
886 msr ARM64_REG_HID10, x9
888 and x9, x9, #(~(ARM64_REG_HID10_DisHwpGups))
889 msr ARM64_REG_HID10, x9
901 * Force WFI to use clock gating only
906 .globl EXT(arm64_force_wfi_clock_gate)
907 LEXT(arm64_force_wfi_clock_gate)
911 mrs x0, ARM64_REG_CYC_OVRD
912 orr x0, x0, #(ARM64_REG_CYC_OVRD_ok2pwrdn_force_up)
913 msr ARM64_REG_CYC_OVRD, x0
919 #if HAS_RETENTION_STATE
922 .globl EXT(arm64_retention_wfi)
923 LEXT(arm64_retention_wfi)
925 cbz lr, Lwfi_retention // If lr is 0, we entered retention state and lost all GPRs except sp and pc
926 ret // Otherwise just return to cpu_idle()
931 bl EXT(cpu_idle_exit) // cpu_idle_exit(from_reset = FALSE)
932 b . // cpu_idle_exit() should never return
935 #if defined(APPLETYPHOON)
939 .globl EXT(typhoon_prepare_for_wfi)
941 LEXT(typhoon_prepare_for_wfi)
944 // <rdar://problem/15827409>
945 HID_SET_BITS ARM64_REG_HID2, ARM64_REG_HID2_disMMUmtlbPrefetch, x0
955 .globl EXT(typhoon_return_from_wfi)
956 LEXT(typhoon_return_from_wfi)
959 // <rdar://problem/15827409>
960 HID_CLEAR_BITS ARM64_REG_HID2, ARM64_REG_HID2_disMMUmtlbPrefetch, x0
970 #define HID0_DEFEATURES_1 0x0000a0c000064010ULL
971 #define HID1_DEFEATURES_1 0x000000004005bf20ULL
972 #define HID2_DEFEATURES_1 0x0000000000102074ULL
973 #define HID3_DEFEATURES_1 0x0000000000400003ULL
974 #define HID4_DEFEATURES_1 0x83ff00e100000268ULL
975 #define HID7_DEFEATURES_1 0x000000000000000eULL
977 #define HID0_DEFEATURES_2 0x0000a1c000020010ULL
978 #define HID1_DEFEATURES_2 0x000000000005d720ULL
979 #define HID2_DEFEATURES_2 0x0000000000002074ULL
980 #define HID3_DEFEATURES_2 0x0000000000400001ULL
981 #define HID4_DEFEATURES_2 0x8390000200000208ULL
982 #define HID7_DEFEATURES_2 0x0000000000000000ULL
985 arg0 = target register
986 arg1 = 64-bit constant
989 movz $0, #(($1 >> 48) & 0xffff), lsl #48
990 movk $0, #(($1 >> 32) & 0xffff), lsl #32
991 movk $0, #(($1 >> 16) & 0xffff), lsl #16
992 movk $0, #(($1) & 0xffff)
997 .globl EXT(cpu_defeatures_set)
998 LEXT(cpu_defeatures_set)
1001 b.eq cpu_defeatures_set_2
1003 b.ne cpu_defeatures_set_ret
1004 LOAD_UINT64 x1, HID0_DEFEATURES_1
1005 mrs x0, ARM64_REG_HID0
1007 msr ARM64_REG_HID0, x0
1008 LOAD_UINT64 x1, HID1_DEFEATURES_1
1009 mrs x0, ARM64_REG_HID1
1011 msr ARM64_REG_HID1, x0
1012 LOAD_UINT64 x1, HID2_DEFEATURES_1
1013 mrs x0, ARM64_REG_HID2
1015 msr ARM64_REG_HID2, x0
1016 LOAD_UINT64 x1, HID3_DEFEATURES_1
1017 mrs x0, ARM64_REG_HID3
1019 msr ARM64_REG_HID3, x0
1020 LOAD_UINT64 x1, HID4_DEFEATURES_1
1021 mrs x0, ARM64_REG_HID4
1023 msr ARM64_REG_HID4, x0
1024 LOAD_UINT64 x1, HID7_DEFEATURES_1
1025 mrs x0, ARM64_REG_HID7
1027 msr ARM64_REG_HID7, x0
1030 b cpu_defeatures_set_ret
1031 cpu_defeatures_set_2:
1032 LOAD_UINT64 x1, HID0_DEFEATURES_2
1033 mrs x0, ARM64_REG_HID0
1035 msr ARM64_REG_HID0, x0
1036 LOAD_UINT64 x1, HID1_DEFEATURES_2
1037 mrs x0, ARM64_REG_HID1
1039 msr ARM64_REG_HID1, x0
1040 LOAD_UINT64 x1, HID2_DEFEATURES_2
1041 mrs x0, ARM64_REG_HID2
1043 msr ARM64_REG_HID2, x0
1044 LOAD_UINT64 x1, HID3_DEFEATURES_2
1045 mrs x0, ARM64_REG_HID3
1047 msr ARM64_REG_HID3, x0
1048 LOAD_UINT64 x1, HID4_DEFEATURES_2
1049 mrs x0, ARM64_REG_HID4
1051 msr ARM64_REG_HID4, x0
1052 LOAD_UINT64 x1, HID7_DEFEATURES_2
1053 mrs x0, ARM64_REG_HID7
1055 msr ARM64_REG_HID7, x0
1058 b cpu_defeatures_set_ret
1059 cpu_defeatures_set_ret:
1064 #else /* !defined(APPLE_ARM64_ARCH_FAMILY) */
1067 .globl EXT(arm64_prepare_for_sleep)
1068 LEXT(arm64_prepare_for_sleep)
1077 * Force WFI to use clock gating only
1078 * Note: for non-Apple device, do nothing.
1082 .globl EXT(arm64_force_wfi_clock_gate)
1083 LEXT(arm64_force_wfi_clock_gate)
1088 #endif /* defined(APPLE_ARM64_ARCH_FAMILY) */
1091 * void arm64_replace_bootstack(cpu_data_t *cpu_data)
1093 * This must be called from a kernel thread context running on the boot CPU,
1094 * after setting up new exception stacks in per-CPU data. That will guarantee
1095 * that the stack(s) we're trying to replace aren't currently in use. For
1096 * KTRR-protected devices, this must also be called prior to VM prot finalization
1097 * and lockdown, as updating SP1 requires a sensitive instruction.
1101 .globl EXT(arm64_replace_bootstack)
1102 LEXT(arm64_replace_bootstack)
1105 // Set the exception stack pointer
1106 ldr x0, [x0, CPU_EXCEPSTACK_TOP]
1107 mrs x4, DAIF // Load current DAIF; use x4 as pinst may trash x1-x3
1108 msr DAIFSet, #(DAIFSC_IRQF | DAIFSC_FIQF | DAIFSC_ASYNCF) // Disable IRQ/FIQ/serror
1109 // Set SP_EL1 to exception stack
1110 #if defined(KERNEL_INTEGRITY_KTRR) || defined(KERNEL_INTEGRITY_CTRR)
1112 bl EXT(pinst_spsel_1)
1119 msr DAIF, x4 // Restore interrupt state
1125 * unsigned long monitor_call(uintptr_t callnum, uintptr_t arg1,
1126 uintptr_t arg2, uintptr_t arg3)
1128 * Call the EL3 monitor with 4 arguments in registers
1129 * The monitor interface maintains the same ABI as the C function call standard. Callee-saved
1130 * registers are preserved, temporary registers are not. Parameters and results are passed in
1135 .globl EXT(monitor_call)
1141 #ifdef HAS_APPLE_PAC
1145 * Macro that signs thread state.
1146 * $0 - Offset in arm_saved_state to store JOPHASH value.
1148 .macro SIGN_THREAD_STATE
1149 pacga x1, x1, x0 /* PC hash (gkey + &arm_saved_state) */
1151 * Mask off the carry flag so we don't need to re-sign when that flag is
1152 * touched by the system call return path.
1155 pacga x1, x2, x1 /* SPSR hash (gkey + pc hash) */
1156 pacga x1, x3, x1 /* LR Hash (gkey + spsr hash) */
1157 pacga x1, x4, x1 /* X16 hash (gkey + lr hash) */
1158 pacga x1, x5, x1 /* X17 hash (gkey + x16 hash) */
1160 #if DEBUG || DEVELOPMENT
1162 tbz x1, #DAIF_IRQF_SHIFT, Lintr_enabled_panic
1163 #endif /* DEBUG || DEVELOPMENT */
1167 * CHECK_SIGNED_STATE
1169 * Macro that checks signed thread state.
1170 * $0 - Offset in arm_saved_state to to read the JOPHASH value from.
1171 * $1 - Label to jump to when check is unsuccessful.
1173 .macro CHECK_SIGNED_STATE
1174 pacga x1, x1, x0 /* PC hash (gkey + &arm_saved_state) */
1176 * Mask off the carry flag so we don't need to re-sign when that flag is
1177 * touched by the system call return path.
1180 pacga x1, x2, x1 /* SPSR hash (gkey + pc hash) */
1181 pacga x1, x3, x1 /* LR Hash (gkey + spsr hash) */
1182 pacga x1, x4, x1 /* X16 hash (gkey + lr hash) */
1183 pacga x1, x5, x1 /* X17 hash (gkey + x16 hash) */
1187 #if DEBUG || DEVELOPMENT
1189 tbz x1, #DAIF_IRQF_SHIFT, Lintr_enabled_panic
1190 #endif /* DEBUG || DEVELOPMENT */
1194 * void ml_sign_thread_state(arm_saved_state_t *ss, uint64_t pc,
1195 * uint32_t cpsr, uint64_t lr, uint64_t x16,
1200 .globl EXT(ml_sign_thread_state)
1201 LEXT(ml_sign_thread_state)
1202 SIGN_THREAD_STATE SS64_JOPHASH
1206 * void ml_sign_kernel_thread_state(arm_kernel_saved_state *ss, uint64_t pc,
1207 * uint32_t cpsr, uint64_t lr, uint64_t x16,
1212 .globl EXT(ml_sign_kernel_thread_state)
1213 LEXT(ml_sign_kernel_thread_state)
1214 SIGN_THREAD_STATE SS64_KERNEL_JOPHASH
1218 * void ml_check_signed_state(arm_saved_state_t *ss, uint64_t pc,
1219 * uint32_t cpsr, uint64_t lr, uint64_t x16,
1224 .globl EXT(ml_check_signed_state)
1225 LEXT(ml_check_signed_state)
1226 CHECK_SIGNED_STATE SS64_JOPHASH, Lcheck_hash_panic
1230 * ml_check_signed_state normally doesn't set up a stack frame, since it
1231 * needs to work in the face of attackers that can modify the stack.
1232 * However we lazily create one in the panic path: at this point we're
1233 * *only* using the stack frame for unwinding purposes, and without one
1234 * we'd be missing information about the caller.
1239 adr x0, Lcheck_hash_str
1240 CALL_EXTERN panic_with_thread_kernel_state
1243 * void ml_check_kernel_signed_state(arm_kernel_saved_state *ss, uint64_t pc,
1244 * uint32_t cpsr, uint64_t lr, uint64_t x16,
1249 .globl EXT(ml_check_kernel_signed_state)
1250 LEXT(ml_check_kernel_signed_state)
1251 CHECK_SIGNED_STATE SS64_KERNEL_JOPHASH, Lcheck_kernel_hash_panic
1253 Lcheck_kernel_hash_panic:
1256 adr x0, Lcheck_hash_str
1260 .asciz "JOP Hash Mismatch Detected (PC, CPSR, or LR corruption)"
1262 #if DEBUG || DEVELOPMENT
1263 Lintr_enabled_panic:
1266 adr x0, Lintr_enabled_str
1270 * Please see the "Signing spilled register state" section of doc/pac.md
1271 * for an explanation of why this is bad and how it should be fixed.
1273 .asciz "Signed thread state manipulated with interrupts enabled"
1274 #endif /* DEBUG || DEVELOPMENT */
1277 * void ml_auth_thread_state_invalid_cpsr(arm_saved_state_t *ss)
1279 * Panics due to an invalid CPSR value in ss.
1283 .globl EXT(ml_auth_thread_state_invalid_cpsr)
1284 LEXT(ml_auth_thread_state_invalid_cpsr)
1288 adr x0, Linvalid_cpsr_str
1289 CALL_EXTERN panic_with_thread_kernel_state
1292 .asciz "Thread state corruption detected (PE mode == 0)"
1293 #endif /* HAS_APPLE_PAC */
1297 .globl EXT(fill32_dczva)
1308 .globl EXT(fill32_nt)
1313 stnp q0, q0, [x0, #0x20]
1314 stnp q0, q0, [x0, #0x40]
1315 stnp q0, q0, [x0, #0x60]
1321 /* vim: set sw=4 ts=4: */