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(APPLEFIRESTORM)
83 tst x1, #(APCTL_EL1_UserKeyEn)
85 #elif defined(HAS_APCTL_EL1_USERKEYEN)
90 msr ARM64_REG_APCTL_EL1, x1
95 * void ml_disable_user_jop_key(uint64_t user_jop_key, uint64_t saved_jop_state)
98 .globl EXT(ml_disable_user_jop_key)
99 LEXT(ml_disable_user_jop_key)
101 b.eq Lskip_program_prev_jop_key
102 SET_JOP_KEY_REGISTERS x1, x2
103 Lskip_program_prev_jop_key:
106 * if (cpu has APCTL_EL1.UserKeyEn) {
107 * clear APCTL_EL1.KernKeyEn // KERNKey is not mixed into EL1 keys
109 * set APCTL_EL1.KernKeyEn // KERNKey is mixed into EL1 keys
112 mrs x1, ARM64_REG_APCTL_EL1
113 #if defined(APPLEFIRESTORM)
114 CLEAR_KERN_KEY x2, x1
116 tst x1, #(APCTL_EL1_UserKeyEn)
118 #elif defined(HAS_APCTL_EL1_USERKEYEN)
119 CLEAR_KERN_KEY x1, x1
123 msr ARM64_REG_APCTL_EL1, x1
127 #endif /* defined(HAS_APPLE_PAC) */
132 * void set_bp_ret(void)
133 * Helper function to enable branch predictor state retention
138 .globl EXT(set_bp_ret)
140 // Load bpret boot-arg
141 adrp x14, EXT(bp_ret)@page
142 add x14, x14, EXT(bp_ret)@pageoff
145 mrs x13, ARM64_REG_ACC_CFG
146 and x13, x13, (~(ARM64_REG_ACC_CFG_bpSlp_mask << ARM64_REG_ACC_CFG_bpSlp_shift))
147 and x14, x14, #(ARM64_REG_ACC_CFG_bpSlp_mask)
148 orr x13, x13, x14, lsl #(ARM64_REG_ACC_CFG_bpSlp_shift)
149 msr ARM64_REG_ACC_CFG, x13
156 .globl EXT(set_nex_pg)
159 // Skip if this isn't a p-core; NEX powergating isn't available for e-cores
160 and x14, x14, #(MPIDR_PNE)
161 cbz x14, Lnex_pg_done
163 // Set the SEG-recommended value of 12 additional reset cycles
164 HID_INSERT_BITS ARM64_REG_HID13, ARM64_REG_HID13_RstCyc_mask, ARM64_REG_HID13_RstCyc_val, x13
165 HID_SET_BITS ARM64_REG_HID14, ARM64_REG_HID14_NexPwgEn, x13
172 /* uint32_t get_fpscr(void):
173 * Returns (FPSR | FPCR).
176 .globl EXT(get_fpscr)
179 mrs x1, FPSR // Grab FPSR
180 mov x4, #(FPSR_MASK & 0xFFFF)
181 mov x5, #(FPSR_MASK & 0xFFFF0000)
183 and x1, x1, x0 // Be paranoid, and clear bits we expect to
185 mrs x2, FPCR // Grab FPCR
186 mov x4, #(FPCR_MASK & 0xFFFF)
187 mov x5, #(FPCR_MASK & 0xFFFF0000)
189 and x2, x2, x0 // Be paranoid, and clear bits we expect to
191 orr x0, x1, x2 // OR them to get FPSCR equivalent state
197 .globl EXT(set_fpscr)
198 /* void set_fpscr(uint32_t value):
199 * Set the FPCR and FPSR registers, based on the given value; a
200 * noteworthy point is that unlike 32-bit mode, 64-bit mode FPSR
201 * and FPCR are not responsible for condition codes.
205 mov x4, #(FPSR_MASK & 0xFFFF)
206 mov x5, #(FPSR_MASK & 0xFFFF0000)
208 and x1, x1, x0 // Clear the bits that don't apply to FPSR
209 mov x4, #(FPCR_MASK & 0xFFFF)
210 mov x5, #(FPCR_MASK & 0xFFFF0000)
212 and x2, x2, x0 // Clear the bits that don't apply to FPCR
213 msr FPSR, x1 // Write FPCR
214 msr FPCR, x2 // Write FPSR
215 dsb ish // FPCR requires synchronization
220 * void update_mdscr(unsigned long clear, unsigned long set)
221 * Clears and sets the specified bits in MDSCR_EL1.
223 * Setting breakpoints in EL1 is effectively a KTRR bypass. The ability to do so is
224 * controlled by MDSCR.KDE. The MSR to set MDSCR must be present to allow
225 * self-hosted user mode debug. Any checks before the MRS can be skipped with ROP,
226 * so we need to put the checks after the MRS where they can't be skipped. That
227 * still leaves a small window if a breakpoint is set on the instruction
228 * immediately after the MRS. To handle that, we also do a check and then set of
229 * the breakpoint control registers. This allows us to guarantee that a given
230 * core will never have both KDE set and a breakpoint targeting EL1.
232 * If KDE gets set, unset it and then panic
235 .globl EXT(update_mdscr)
244 #if defined(CONFIG_KERNEL_INTEGRITY)
246 * verify KDE didn't get set (including via ROP)
247 * If set, clear it and then panic
253 b.ne Lupdate_mdscr_panic
258 adrp x0, Lupdate_mdscr_panic_str@page
259 add x0, x0, Lupdate_mdscr_panic_str@pageoff
263 Lupdate_mdscr_panic_str:
264 .asciz "MDSCR.KDE was set"
268 * Set MMU Translation Table Base Alternate
272 .globl EXT(set_mmu_ttb_alternate)
273 LEXT(set_mmu_ttb_alternate)
275 #if defined(KERNEL_INTEGRITY_KTRR)
277 bl EXT(pinst_set_ttbr1)
280 #if defined(HAS_VMSA_LOCK)
281 #if DEBUG || DEVELOPMENT
282 mrs x1, ARM64_REG_VMSA_LOCK_EL1
283 and x1, x1, #(VMSA_LOCK_TTBR1_EL1)
284 cbnz x1, L_set_locked_reg_panic
285 #endif /* DEBUG || DEVELOPMENT */
286 #endif /* defined(HAS_VMSA_LOCK) */
288 #endif /* defined(KERNEL_INTEGRITY_KTRR) */
293 .section __PPLTEXT,__text,regular,pure_instructions
298 .globl EXT(set_mmu_ttb)
300 #if __ARM_KERNEL_PROTECT__
301 /* All EL1-mode ASIDs are odd. */
302 orr x0, x0, #(1 << TTBR_ASID_SHIFT)
303 #endif /* __ARM_KERNEL_PROTECT__ */
313 .globl EXT(ml_get_ppl_cpu_data)
314 LEXT(ml_get_ppl_cpu_data)
315 LOAD_PMAP_CPU_DATA x0, x1, x2
320 * set AUX control register
324 .globl EXT(set_aux_control)
325 LEXT(set_aux_control)
327 // Synchronize system
331 #if __ARM_KERNEL_PROTECT__
334 .globl EXT(set_vbar_el1)
336 #if defined(KERNEL_INTEGRITY_KTRR)
337 b EXT(pinst_set_vbar)
342 #endif /* __ARM_KERNEL_PROTECT__ */
344 #if defined(HAS_VMSA_LOCK)
347 .globl EXT(vmsa_lock)
350 mov x1, #(VMSA_LOCK_SCTLR_M_BIT)
351 #if __ARM_MIXED_PAGE_SIZE__
352 mov x0, #(VMSA_LOCK_TTBR1_EL1 | VMSA_LOCK_VBAR_EL1)
354 mov x0, #(VMSA_LOCK_TTBR1_EL1 | VMSA_LOCK_TCR_EL1 | VMSA_LOCK_VBAR_EL1)
357 msr ARM64_REG_VMSA_LOCK_EL1, x0
360 #endif /* defined(HAS_VMSA_LOCK) */
363 * set translation control register
369 #if defined(APPLE_ARM64_ARCH_FAMILY)
370 #if DEBUG || DEVELOPMENT
371 // Assert that T0Z is always equal to T1Z
372 eor x1, x0, x0, lsr #(TCR_T1SZ_SHIFT - TCR_T0SZ_SHIFT)
373 and x1, x1, #(TCR_TSZ_MASK << TCR_T0SZ_SHIFT)
374 cbnz x1, L_set_tcr_panic
375 #endif /* DEBUG || DEVELOPMENT */
376 #endif /* defined(APPLE_ARM64_ARCH_FAMILY) */
377 #if defined(KERNEL_INTEGRITY_KTRR)
379 bl EXT(pinst_set_tcr)
382 #if defined(HAS_VMSA_LOCK)
383 #if DEBUG || DEVELOPMENT
384 // assert TCR unlocked
385 mrs x1, ARM64_REG_VMSA_LOCK_EL1
386 and x1, x1, #(VMSA_LOCK_TCR_EL1)
387 cbnz x1, L_set_locked_reg_panic
388 #endif /* DEBUG || DEVELOPMENT */
389 #endif /* defined(HAS_VMSA_LOCK) */
391 #endif /* defined(KERNEL_INTRITY_KTRR) */
395 #if DEBUG || DEVELOPMENT
400 adr x0, L_set_tcr_panic_str
403 L_set_locked_reg_panic:
407 adr x0, L_set_locked_reg_panic_str
412 .asciz "set_tcr: t0sz, t1sz not equal (%llx)\n"
415 L_set_locked_reg_panic_str:
416 .asciz "attempt to set locked register: (%llx)\n"
417 #endif /* DEBUG || DEVELOPMENT */
420 * MMU kernel virtual to physical address translation
424 .globl EXT(mmu_kvtop)
426 mrs x2, DAIF // Load current DAIF
427 msr DAIFSet, #(DAIFSC_IRQF | DAIFSC_FIQF) // Disable IRQ
428 at s1e1r, x0 // Translation Stage 1 EL1
430 mrs x1, PAR_EL1 // Read result
431 msr DAIF, x2 // Restore interrupt state
432 tbnz x1, #0, L_mmu_kvtop_invalid // Test Translation not valid
433 bfm x1, x0, #0, #11 // Add page offset
434 and x0, x1, #0x0000ffffffffffff // Clear non-address bits
437 mov x0, #0 // Return invalid
441 * MMU user virtual to physical address translation
445 .globl EXT(mmu_uvtop)
447 lsr x8, x0, #56 // Extract top byte
448 cbnz x8, L_mmu_uvtop_invalid // Tagged pointers are invalid
449 mrs x2, DAIF // Load current DAIF
450 msr DAIFSet, #(DAIFSC_IRQF | DAIFSC_FIQF) // Disable IRQ
451 at s1e0r, x0 // Translation Stage 1 EL0
453 mrs x1, PAR_EL1 // Read result
454 msr DAIF, x2 // Restore interrupt state
455 tbnz x1, #0, L_mmu_uvtop_invalid // Test Translation not valid
456 bfm x1, x0, #0, #11 // Add page offset
457 and x0, x1, #0x0000ffffffffffff // Clear non-address bits
460 mov x0, #0 // Return invalid
464 * MMU kernel virtual to physical address preflight write access
468 .globl EXT(mmu_kvtop_wpreflight)
469 LEXT(mmu_kvtop_wpreflight)
470 mrs x2, DAIF // Load current DAIF
471 msr DAIFSet, #(DAIFSC_IRQF | DAIFSC_FIQF) // Disable IRQ
472 at s1e1w, x0 // Translation Stage 1 EL1
473 mrs x1, PAR_EL1 // Read result
474 msr DAIF, x2 // Restore interrupt state
475 tbnz x1, #0, L_mmu_kvtop_wpreflight_invalid // Test Translation not valid
476 bfm x1, x0, #0, #11 // Add page offset
477 and x0, x1, #0x0000ffffffffffff // Clear non-address bits
479 L_mmu_kvtop_wpreflight_invalid:
480 mov x0, #0 // Return invalid
484 * SET_RECOVERY_HANDLER
486 * Sets up a page fault recovery handler. This macro clobbers x16 and x17.
488 * label - recovery label
489 * tpidr - persisted thread pointer
490 * old_handler - persisted recovery handler
491 * label_in_adr_range - whether \label is within 1 MB of PC
493 .macro SET_RECOVERY_HANDLER label, tpidr=x16, old_handler=x10, label_in_adr_range=0
494 // Note: x16 and x17 are designated for use as temporaries in
495 // interruptible PAC routines. DO NOT CHANGE THESE REGISTER ASSIGNMENTS.
496 .if \label_in_adr_range==1 // Load the recovery handler address
499 adrp x17, \label@page
500 add x17, x17, \label@pageoff
502 #if defined(HAS_APPLE_PAC)
504 add x16, x16, TH_RECOVER
505 movk x16, #PAC_DISCRIMINATOR_RECOVER, lsl 48
506 pacia x17, x16 // Sign with IAKey + blended discriminator
509 mrs \tpidr, TPIDR_EL1 // Load thread pointer
510 ldr \old_handler, [\tpidr, TH_RECOVER] // Save previous recovery handler
511 str x17, [\tpidr, TH_RECOVER] // Set new signed recovery handler
515 * CLEAR_RECOVERY_HANDLER
517 * Clears page fault handler set by SET_RECOVERY_HANDLER
519 * tpidr - thread pointer saved by SET_RECOVERY_HANDLER
520 * old_handler - old recovery handler saved by SET_RECOVERY_HANDLER
522 .macro CLEAR_RECOVERY_HANDLER tpidr=x16, old_handler=x10
523 str \old_handler, [\tpidr, TH_RECOVER] // Restore the previous recovery handler
530 CLEAR_RECOVERY_HANDLER
531 mov x0, #EFAULT // Return an EFAULT error
536 * int _bcopyin(const char *src, char *dst, vm_size_t len)
544 SET_RECOVERY_HANDLER copyio_error
545 /* If len is less than 16 bytes, just do a bytewise copy */
550 /* 16 bytes at a time */
551 ldp x3, x4, [x0], #16
552 stp x3, x4, [x1], #16
555 /* Fixup the len and test for completion */
564 CLEAR_RECOVERY_HANDLER
570 * int _copyin_atomic32(const char *src, uint32_t *dst)
574 .globl EXT(_copyin_atomic32)
575 LEXT(_copyin_atomic32)
578 SET_RECOVERY_HANDLER copyio_error
582 CLEAR_RECOVERY_HANDLER
587 * int _copyin_atomic32_wait_if_equals(const char *src, uint32_t value)
591 .globl EXT(_copyin_atomic32_wait_if_equals)
592 LEXT(_copyin_atomic32_wait_if_equals)
595 SET_RECOVERY_HANDLER copyio_error
604 CLEAR_RECOVERY_HANDLER
609 * int _copyin_atomic64(const char *src, uint32_t *dst)
613 .globl EXT(_copyin_atomic64)
614 LEXT(_copyin_atomic64)
617 SET_RECOVERY_HANDLER copyio_error
621 CLEAR_RECOVERY_HANDLER
627 * int _copyout_atomic32(uint32_t value, char *dst)
631 .globl EXT(_copyout_atomic32)
632 LEXT(_copyout_atomic32)
635 SET_RECOVERY_HANDLER copyio_error
638 CLEAR_RECOVERY_HANDLER
643 * int _copyout_atomic64(uint64_t value, char *dst)
647 .globl EXT(_copyout_atomic64)
648 LEXT(_copyout_atomic64)
651 SET_RECOVERY_HANDLER copyio_error
654 CLEAR_RECOVERY_HANDLER
660 * int _bcopyout(const char *src, char *dst, vm_size_t len)
664 .globl EXT(_bcopyout)
668 SET_RECOVERY_HANDLER copyio_error
669 /* If len is less than 16 bytes, just do a bytewise copy */
674 /* 16 bytes at a time */
675 ldp x3, x4, [x0], #16
676 stp x3, x4, [x1], #16
679 /* Fixup the len and test for completion */
688 CLEAR_RECOVERY_HANDLER
695 * const user_addr_t user_addr,
702 .globl EXT(_bcopyinstr)
706 SET_RECOVERY_HANDLER Lcopyinstr_error, label_in_adr_range=1
707 mov x4, #0 // x4 - total bytes copied
709 ldrb w5, [x0], #1 // Load a byte from the user source
710 strb w5, [x1], #1 // Store a byte to the kernel dest
711 add x4, x4, #1 // Increment bytes copied
712 cbz x5, Lcopyinstr_done // If this byte is null, we're done
713 cmp x4, x2 // If we're out of space, return an error
716 mov x5, #ENAMETOOLONG // Set current byte to error code for later return
718 str x4, [x3] // Return number of bytes copied
719 mov x0, x5 // Set error code (0 on success, ENAMETOOLONG on failure)
722 mov x0, #EFAULT // Return EFAULT on error
724 CLEAR_RECOVERY_HANDLER
729 * int copyinframe(const vm_address_t frame_addr, char *kernel_addr, bool is64bit)
731 * Safely copy sixteen bytes (the fixed top of an ARM64 frame) from
732 * either user or kernel memory, or 8 bytes (AArch32) from user only.
734 * x0 : address of frame to copy.
735 * x1 : kernel address at which to store data.
736 * w2 : whether to copy an AArch32 or AArch64 frame.
738 * x5 : temp (kernel virtual base)
740 * x10 : old recovery function (set by SET_RECOVERY_HANDLER)
741 * x12, x13 : backtrace data
742 * x16 : thread pointer (set by SET_RECOVERY_HANDLER)
747 .globl EXT(copyinframe)
751 SET_RECOVERY_HANDLER copyio_error
752 cbnz w2, Lcopyinframe64 // Check frame size
753 adrp x5, EXT(gVirtBase)@page // For 32-bit frame, make sure we're not trying to copy from kernel
754 add x5, x5, EXT(gVirtBase)@pageoff
756 cmp x5, x0 // See if address is in kernel virtual range
757 b.hi Lcopyinframe32 // If below kernel virtual range, proceed.
758 mov w0, #EFAULT // Should never have a 32-bit frame in kernel virtual range
762 ldr x12, [x0] // Copy 8 bytes
764 mov w0, #0 // Success
768 mov x3, VM_MIN_KERNEL_ADDRESS // Check if kernel address
769 orr x9, x0, TBI_MASK // Hide tags in address comparison
770 cmp x9, x3 // If in kernel address range, skip tag test
771 b.hs Lcopyinframe_valid
772 tst x0, TBI_MASK // Detect tagged pointers
773 b.eq Lcopyinframe_valid
774 mov w0, #EFAULT // Tagged address, fail
777 ldp x12, x13, [x0] // Copy 16 bytes
779 mov w0, #0 // Success
782 CLEAR_RECOVERY_HANDLER
788 * uint32_t arm_debug_read_dscr(void)
792 .globl EXT(arm_debug_read_dscr)
793 LEXT(arm_debug_read_dscr)
797 * void arm_debug_set_cp14(arm_debug_state_t *debug_state)
799 * Set debug registers to match the current thread state
800 * (NULL to disable). Assume 6 breakpoints and 2
801 * watchpoints, since that has been the case in all cores
806 .globl EXT(arm_debug_set_cp14)
807 LEXT(arm_debug_set_cp14)
810 #if defined(APPLE_ARM64_ARCH_FAMILY)
812 * Note: still have to ISB before executing wfi!
816 .globl EXT(arm64_prepare_for_sleep)
817 LEXT(arm64_prepare_for_sleep)
820 #if defined(APPLETYPHOON)
821 // <rdar://problem/15827409>
822 HID_SET_BITS ARM64_REG_HID2, ARM64_REG_HID2_disMMUmtlbPrefetch, x9
828 cbnz x0, 1f // Skip if deep_sleep == true
829 // Mask FIQ and IRQ to avoid spurious wakeups
830 mrs x9, ARM64_REG_CYC_OVRD
831 and x9, x9, #(~(ARM64_REG_CYC_OVRD_irq_mask | ARM64_REG_CYC_OVRD_fiq_mask))
832 mov x10, #(ARM64_REG_CYC_OVRD_irq_disable | ARM64_REG_CYC_OVRD_fiq_disable)
834 msr ARM64_REG_CYC_OVRD, x9
839 cbz x0, 1f // Skip if deep_sleep == false
840 #if __ARM_GLOBAL_SLEEP_BIT__
842 mrs x1, ARM64_REG_ACC_OVRD
843 orr x1, x1, #(ARM64_REG_ACC_OVRD_enDeepSleep)
844 and x1, x1, #(~(ARM64_REG_ACC_OVRD_disL2Flush4AccSlp_mask))
845 orr x1, x1, #( ARM64_REG_ACC_OVRD_disL2Flush4AccSlp_deepsleep)
846 and x1, x1, #(~(ARM64_REG_ACC_OVRD_ok2PwrDnSRM_mask))
847 orr x1, x1, #( ARM64_REG_ACC_OVRD_ok2PwrDnSRM_deepsleep)
848 and x1, x1, #(~(ARM64_REG_ACC_OVRD_ok2TrDnLnk_mask))
849 orr x1, x1, #( ARM64_REG_ACC_OVRD_ok2TrDnLnk_deepsleep)
850 and x1, x1, #(~(ARM64_REG_ACC_OVRD_ok2PwrDnCPM_mask))
851 orr x1, x1, #( ARM64_REG_ACC_OVRD_ok2PwrDnCPM_deepsleep)
852 #if HAS_RETENTION_STATE
853 orr x1, x1, #(ARM64_REG_ACC_OVRD_disPioOnWfiCpu)
855 msr ARM64_REG_ACC_OVRD, x1
860 mov x1, ARM64_REG_CYC_CFG_deepSleep
861 msr ARM64_REG_CYC_CFG, x1
865 // Set "OK to power down" (<rdar://problem/12390433>)
866 mrs x9, ARM64_REG_CYC_OVRD
867 orr x9, x9, #(ARM64_REG_CYC_OVRD_ok2pwrdn_force_down)
868 #if HAS_RETENTION_STATE
869 orr x9, x9, #(ARM64_REG_CYC_OVRD_disWfiRetn)
871 msr ARM64_REG_CYC_OVRD, x9
873 #if defined(APPLEMONSOON) || defined(APPLEVORTEX)
875 cbz x9, Lwfi_inst // skip if not p-core
877 /* <rdar://problem/32512947>: Flush the GUPS prefetcher prior to
878 * wfi. A Skye HW bug can cause the GUPS prefetcher on p-cores
879 * to be left with valid entries that fail to drain if a
880 * subsequent wfi is issued. This can prevent the core from
881 * power-gating. For the idle case that is recoverable, but
882 * for the deep-sleep (S2R) case in which cores MUST power-gate,
883 * it can lead to a hang. This can be prevented by disabling
884 * and re-enabling GUPS, which forces the prefetch queue to
885 * drain. This should be done as close to wfi as possible, i.e.
886 * at the very end of arm64_prepare_for_sleep(). */
887 #if defined(APPLEVORTEX)
888 /* <rdar://problem/32821461>: Cyprus A0/A1 parts have a similar
889 * bug in the HSP prefetcher that can be worked around through
890 * the same method mentioned above for Skye. */
892 EXEC_COREALL_REVLO CPU_VERSION_B0, x9, x10
894 mrs x9, ARM64_REG_HID10
895 orr x9, x9, #(ARM64_REG_HID10_DisHwpGups)
896 msr ARM64_REG_HID10, x9
898 and x9, x9, #(~(ARM64_REG_HID10_DisHwpGups))
899 msr ARM64_REG_HID10, x9
911 * Force WFI to use clock gating only
916 .globl EXT(arm64_force_wfi_clock_gate)
917 LEXT(arm64_force_wfi_clock_gate)
921 mrs x0, ARM64_REG_CYC_OVRD
922 orr x0, x0, #(ARM64_REG_CYC_OVRD_ok2pwrdn_force_up)
923 msr ARM64_REG_CYC_OVRD, x0
929 #if HAS_RETENTION_STATE
932 .globl EXT(arm64_retention_wfi)
933 LEXT(arm64_retention_wfi)
935 cbz lr, Lwfi_retention // If lr is 0, we entered retention state and lost all GPRs except sp and pc
936 ret // Otherwise just return to cpu_idle()
941 bl EXT(cpu_idle_exit) // cpu_idle_exit(from_reset = FALSE)
942 b . // cpu_idle_exit() should never return
945 #if defined(APPLETYPHOON)
949 .globl EXT(typhoon_prepare_for_wfi)
951 LEXT(typhoon_prepare_for_wfi)
954 // <rdar://problem/15827409>
955 HID_SET_BITS ARM64_REG_HID2, ARM64_REG_HID2_disMMUmtlbPrefetch, x0
965 .globl EXT(typhoon_return_from_wfi)
966 LEXT(typhoon_return_from_wfi)
969 // <rdar://problem/15827409>
970 HID_CLEAR_BITS ARM64_REG_HID2, ARM64_REG_HID2_disMMUmtlbPrefetch, x0
980 #define HID0_DEFEATURES_1 0x0000a0c000064010ULL
981 #define HID1_DEFEATURES_1 0x000000004005bf20ULL
982 #define HID2_DEFEATURES_1 0x0000000000102074ULL
983 #define HID3_DEFEATURES_1 0x0000000000400003ULL
984 #define HID4_DEFEATURES_1 0x83ff00e100000268ULL
985 #define HID7_DEFEATURES_1 0x000000000000000eULL
987 #define HID0_DEFEATURES_2 0x0000a1c000020010ULL
988 #define HID1_DEFEATURES_2 0x000000000005d720ULL
989 #define HID2_DEFEATURES_2 0x0000000000002074ULL
990 #define HID3_DEFEATURES_2 0x0000000000400001ULL
991 #define HID4_DEFEATURES_2 0x8390000200000208ULL
992 #define HID7_DEFEATURES_2 0x0000000000000000ULL
995 arg0 = target register
996 arg1 = 64-bit constant
999 movz $0, #(($1 >> 48) & 0xffff), lsl #48
1000 movk $0, #(($1 >> 32) & 0xffff), lsl #32
1001 movk $0, #(($1 >> 16) & 0xffff), lsl #16
1002 movk $0, #(($1) & 0xffff)
1007 .globl EXT(cpu_defeatures_set)
1008 LEXT(cpu_defeatures_set)
1011 b.eq cpu_defeatures_set_2
1013 b.ne cpu_defeatures_set_ret
1014 LOAD_UINT64 x1, HID0_DEFEATURES_1
1015 mrs x0, ARM64_REG_HID0
1017 msr ARM64_REG_HID0, x0
1018 LOAD_UINT64 x1, HID1_DEFEATURES_1
1019 mrs x0, ARM64_REG_HID1
1021 msr ARM64_REG_HID1, x0
1022 LOAD_UINT64 x1, HID2_DEFEATURES_1
1023 mrs x0, ARM64_REG_HID2
1025 msr ARM64_REG_HID2, x0
1026 LOAD_UINT64 x1, HID3_DEFEATURES_1
1027 mrs x0, ARM64_REG_HID3
1029 msr ARM64_REG_HID3, x0
1030 LOAD_UINT64 x1, HID4_DEFEATURES_1
1031 mrs x0, ARM64_REG_HID4
1033 msr ARM64_REG_HID4, x0
1034 LOAD_UINT64 x1, HID7_DEFEATURES_1
1035 mrs x0, ARM64_REG_HID7
1037 msr ARM64_REG_HID7, x0
1040 b cpu_defeatures_set_ret
1041 cpu_defeatures_set_2:
1042 LOAD_UINT64 x1, HID0_DEFEATURES_2
1043 mrs x0, ARM64_REG_HID0
1045 msr ARM64_REG_HID0, x0
1046 LOAD_UINT64 x1, HID1_DEFEATURES_2
1047 mrs x0, ARM64_REG_HID1
1049 msr ARM64_REG_HID1, x0
1050 LOAD_UINT64 x1, HID2_DEFEATURES_2
1051 mrs x0, ARM64_REG_HID2
1053 msr ARM64_REG_HID2, x0
1054 LOAD_UINT64 x1, HID3_DEFEATURES_2
1055 mrs x0, ARM64_REG_HID3
1057 msr ARM64_REG_HID3, x0
1058 LOAD_UINT64 x1, HID4_DEFEATURES_2
1059 mrs x0, ARM64_REG_HID4
1061 msr ARM64_REG_HID4, x0
1062 LOAD_UINT64 x1, HID7_DEFEATURES_2
1063 mrs x0, ARM64_REG_HID7
1065 msr ARM64_REG_HID7, x0
1068 b cpu_defeatures_set_ret
1069 cpu_defeatures_set_ret:
1074 #else /* !defined(APPLE_ARM64_ARCH_FAMILY) */
1077 .globl EXT(arm64_prepare_for_sleep)
1078 LEXT(arm64_prepare_for_sleep)
1087 * Force WFI to use clock gating only
1088 * Note: for non-Apple device, do nothing.
1092 .globl EXT(arm64_force_wfi_clock_gate)
1093 LEXT(arm64_force_wfi_clock_gate)
1098 #endif /* defined(APPLE_ARM64_ARCH_FAMILY) */
1101 * void arm64_replace_bootstack(cpu_data_t *cpu_data)
1103 * This must be called from a kernel thread context running on the boot CPU,
1104 * after setting up new exception stacks in per-CPU data. That will guarantee
1105 * that the stack(s) we're trying to replace aren't currently in use. For
1106 * KTRR-protected devices, this must also be called prior to VM prot finalization
1107 * and lockdown, as updating SP1 requires a sensitive instruction.
1111 .globl EXT(arm64_replace_bootstack)
1112 LEXT(arm64_replace_bootstack)
1115 // Set the exception stack pointer
1116 ldr x0, [x0, CPU_EXCEPSTACK_TOP]
1117 mrs x4, DAIF // Load current DAIF; use x4 as pinst may trash x1-x3
1118 msr DAIFSet, #(DAIFSC_IRQF | DAIFSC_FIQF | DAIFSC_ASYNCF) // Disable IRQ/FIQ/serror
1119 // Set SP_EL1 to exception stack
1120 #if defined(KERNEL_INTEGRITY_KTRR) || defined(KERNEL_INTEGRITY_CTRR)
1122 bl EXT(pinst_spsel_1)
1129 msr DAIF, x4 // Restore interrupt state
1135 * unsigned long monitor_call(uintptr_t callnum, uintptr_t arg1,
1136 uintptr_t arg2, uintptr_t arg3)
1138 * Call the EL3 monitor with 4 arguments in registers
1139 * The monitor interface maintains the same ABI as the C function call standard. Callee-saved
1140 * registers are preserved, temporary registers are not. Parameters and results are passed in
1145 .globl EXT(monitor_call)
1151 #ifdef HAS_APPLE_PAC
1155 * Macro that signs thread state.
1156 * $0 - Offset in arm_saved_state to store JOPHASH value.
1158 .macro SIGN_THREAD_STATE
1159 pacga x1, x1, x0 /* PC hash (gkey + &arm_saved_state) */
1161 * Mask off the carry flag so we don't need to re-sign when that flag is
1162 * touched by the system call return path.
1165 pacga x1, x2, x1 /* SPSR hash (gkey + pc hash) */
1166 pacga x1, x3, x1 /* LR Hash (gkey + spsr hash) */
1167 pacga x1, x4, x1 /* X16 hash (gkey + lr hash) */
1168 pacga x1, x5, x1 /* X17 hash (gkey + x16 hash) */
1170 #if DEBUG || DEVELOPMENT
1172 tbz x1, #DAIF_IRQF_SHIFT, Lintr_enabled_panic
1173 #endif /* DEBUG || DEVELOPMENT */
1177 * CHECK_SIGNED_STATE
1179 * Macro that checks signed thread state.
1180 * $0 - Offset in arm_saved_state to to read the JOPHASH value from.
1181 * $1 - Label to jump to when check is unsuccessful.
1183 .macro CHECK_SIGNED_STATE
1184 pacga x1, x1, x0 /* PC hash (gkey + &arm_saved_state) */
1186 * Mask off the carry flag so we don't need to re-sign when that flag is
1187 * touched by the system call return path.
1190 pacga x1, x2, x1 /* SPSR hash (gkey + pc hash) */
1191 pacga x1, x3, x1 /* LR Hash (gkey + spsr hash) */
1192 pacga x1, x4, x1 /* X16 hash (gkey + lr hash) */
1193 pacga x1, x5, x1 /* X17 hash (gkey + x16 hash) */
1197 #if DEBUG || DEVELOPMENT
1199 tbz x1, #DAIF_IRQF_SHIFT, Lintr_enabled_panic
1200 #endif /* DEBUG || DEVELOPMENT */
1204 * void ml_sign_thread_state(arm_saved_state_t *ss, uint64_t pc,
1205 * uint32_t cpsr, uint64_t lr, uint64_t x16,
1210 .globl EXT(ml_sign_thread_state)
1211 LEXT(ml_sign_thread_state)
1212 SIGN_THREAD_STATE SS64_JOPHASH
1216 * void ml_sign_kernel_thread_state(arm_kernel_saved_state *ss, uint64_t pc,
1217 * uint32_t cpsr, uint64_t lr, uint64_t x16,
1222 .globl EXT(ml_sign_kernel_thread_state)
1223 LEXT(ml_sign_kernel_thread_state)
1224 SIGN_THREAD_STATE SS64_KERNEL_JOPHASH
1228 * void ml_check_signed_state(arm_saved_state_t *ss, uint64_t pc,
1229 * uint32_t cpsr, uint64_t lr, uint64_t x16,
1234 .globl EXT(ml_check_signed_state)
1235 LEXT(ml_check_signed_state)
1236 CHECK_SIGNED_STATE SS64_JOPHASH, Lcheck_hash_panic
1240 * ml_check_signed_state normally doesn't set up a stack frame, since it
1241 * needs to work in the face of attackers that can modify the stack.
1242 * However we lazily create one in the panic path: at this point we're
1243 * *only* using the stack frame for unwinding purposes, and without one
1244 * we'd be missing information about the caller.
1249 adr x0, Lcheck_hash_str
1250 CALL_EXTERN panic_with_thread_kernel_state
1253 * void ml_check_kernel_signed_state(arm_kernel_saved_state *ss, uint64_t pc,
1254 * uint32_t cpsr, uint64_t lr, uint64_t x16,
1259 .globl EXT(ml_check_kernel_signed_state)
1260 LEXT(ml_check_kernel_signed_state)
1261 CHECK_SIGNED_STATE SS64_KERNEL_JOPHASH, Lcheck_kernel_hash_panic
1263 Lcheck_kernel_hash_panic:
1266 adr x0, Lcheck_hash_str
1270 .asciz "JOP Hash Mismatch Detected (PC, CPSR, or LR corruption)"
1272 #if DEBUG || DEVELOPMENT
1273 Lintr_enabled_panic:
1276 adr x0, Lintr_enabled_str
1280 * Please see the "Signing spilled register state" section of doc/pac.md
1281 * for an explanation of why this is bad and how it should be fixed.
1283 .asciz "Signed thread state manipulated with interrupts enabled"
1284 #endif /* DEBUG || DEVELOPMENT */
1287 * void ml_auth_thread_state_invalid_cpsr(arm_saved_state_t *ss)
1289 * Panics due to an invalid CPSR value in ss.
1293 .globl EXT(ml_auth_thread_state_invalid_cpsr)
1294 LEXT(ml_auth_thread_state_invalid_cpsr)
1298 adr x0, Linvalid_cpsr_str
1299 CALL_EXTERN panic_with_thread_kernel_state
1302 .asciz "Thread state corruption detected (PE mode == 0)"
1303 #endif /* HAS_APPLE_PAC */
1307 .globl EXT(fill32_dczva)
1318 .globl EXT(fill32_nt)
1323 stnp q0, q0, [x0, #0x20]
1324 stnp q0, q0, [x0, #0x40]
1325 stnp q0, q0, [x0, #0x60]
1331 /* vim: set sw=4 ts=4: */