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/machine_machdep.h>
31 #include <arm64/proc_reg.h>
33 #include <pexpert/arm64/board_config.h>
34 #include <sys/errno.h>
38 #if defined(HAS_APPLE_PAC)
41 * ml_set_kernelkey_enabled(boolean_t enable)
43 * Toggle pointer auth kernel domain key diversification. Assembly to prevent compiler reordering.
48 .globl EXT(ml_set_kernelkey_enabled)
49 LEXT(ml_set_kernelkey_enabled)
50 mrs x1, ARM64_REG_APCTL_EL1
51 orr x2, x1, #APCTL_EL1_KernKeyEn
52 and x1, x1, #~APCTL_EL1_KernKeyEn
55 msr ARM64_REG_APCTL_EL1, x1
59 #endif /* defined(HAS_APPLE_PAC) */
64 * void set_bp_ret(void)
65 * Helper function to enable branch predictor state retention
70 .globl EXT(set_bp_ret)
72 // Load bpret boot-arg
73 adrp x14, EXT(bp_ret)@page
74 add x14, x14, EXT(bp_ret)@pageoff
77 mrs x13, ARM64_REG_ACC_CFG
78 and x13, x13, (~(ARM64_REG_ACC_CFG_bpSlp_mask << ARM64_REG_ACC_CFG_bpSlp_shift))
79 and x14, x14, #(ARM64_REG_ACC_CFG_bpSlp_mask)
80 orr x13, x13, x14, lsl #(ARM64_REG_ACC_CFG_bpSlp_shift)
81 msr ARM64_REG_ACC_CFG, x13
88 .globl EXT(set_nex_pg)
91 // Skip if this isn't a p-core; NEX powergating isn't available for e-cores
92 and x14, x14, #(MPIDR_PNE)
95 // Set the SEG-recommended value of 12 additional reset cycles
96 mrs x14, ARM64_REG_HID13
97 and x14, x14, (~ARM64_REG_HID13_RstCyc_mask)
98 orr x14, x14, ARM64_REG_HID13_RstCyc_val
99 msr ARM64_REG_HID13, x14
101 // Load nexpg boot-arg
102 adrp x14, EXT(nex_pg)@page
103 add x14, x14, EXT(nex_pg)@pageoff
106 mrs x13, ARM64_REG_HID14
107 and x13, x13, (~ARM64_REG_HID14_NexPwgEn)
109 orr x13, x13, ARM64_REG_HID14_NexPwgEn
111 msr ARM64_REG_HID14, x13
118 /* uint32_t get_fpscr(void):
119 * Returns (FPSR | FPCR).
122 .globl EXT(get_fpscr)
125 mrs x1, FPSR // Grab FPSR
126 mov x4, #(FPSR_MASK & 0xFFFF)
127 mov x5, #(FPSR_MASK & 0xFFFF0000)
129 and x1, x1, x0 // Be paranoid, and clear bits we expect to
131 mrs x2, FPCR // Grab FPCR
132 mov x4, #(FPCR_MASK & 0xFFFF)
133 mov x5, #(FPCR_MASK & 0xFFFF0000)
135 and x2, x2, x0 // Be paranoid, and clear bits we expect to
137 orr x0, x1, x2 // OR them to get FPSCR equivalent state
143 .globl EXT(set_fpscr)
144 /* void set_fpscr(uint32_t value):
145 * Set the FPCR and FPSR registers, based on the given value; a
146 * noteworthy point is that unlike 32-bit mode, 64-bit mode FPSR
147 * and FPCR are not responsible for condition codes.
151 mov x4, #(FPSR_MASK & 0xFFFF)
152 mov x5, #(FPSR_MASK & 0xFFFF0000)
154 and x1, x1, x0 // Clear the bits that don't apply to FPSR
155 mov x4, #(FPCR_MASK & 0xFFFF)
156 mov x5, #(FPCR_MASK & 0xFFFF0000)
158 and x2, x2, x0 // Clear the bits that don't apply to FPCR
159 msr FPSR, x1 // Write FPCR
160 msr FPCR, x2 // Write FPSR
161 dsb ish // FPCR requires synchronization
166 * void update_mdscr(unsigned long clear, unsigned long set)
167 * Clears and sets the specified bits in MDSCR_EL1.
169 * Setting breakpoints in EL1 is effectively a KTRR bypass. The ability to do so is
170 * controlled by MDSCR.KDE. The MSR to set MDSCR must be present to allow
171 * self-hosted user mode debug. Any checks before the MRS can be skipped with ROP,
172 * so we need to put the checks after the MRS where they can't be skipped. That
173 * still leaves a small window if a breakpoint is set on the instruction
174 * immediately after the MRS. To handle that, we also do a check and then set of
175 * the breakpoint control registers. This allows us to guarantee that a given
176 * core will never have both KDE set and a breakpoint targeting EL1.
178 * If KDE gets set, unset it and then panic
181 .globl EXT(update_mdscr)
190 #if defined(CONFIG_KERNEL_INTEGRITY)
192 * verify KDE didn't get set (including via ROP)
193 * If set, clear it and then panic
199 b.ne Lupdate_mdscr_panic
204 adrp x0, Lupdate_mdscr_panic_str@page
205 add x0, x0, Lupdate_mdscr_panic_str@pageoff
209 Lupdate_mdscr_panic_str:
210 .asciz "MDSCR.KDE was set"
214 * Set MMU Translation Table Base Alternate
218 .globl EXT(set_mmu_ttb_alternate)
219 LEXT(set_mmu_ttb_alternate)
221 #if defined(KERNEL_INTEGRITY_KTRR)
223 bl EXT(pinst_set_ttbr1)
226 #if defined(HAS_VMSA_LOCK)
227 mrs x1, ARM64_REG_VMSA_LOCK_EL1
228 and x1, x1, #(VMSA_LOCK_TTBR1_EL1)
229 cbnz x1, L_set_locked_reg_panic
230 #endif /* defined(HAS_VMSA_LOCK) */
232 #endif /* defined(KERNEL_INTEGRITY_KTRR) */
237 .section __PPLTEXT,__text,regular,pure_instructions
242 .globl EXT(set_mmu_ttb)
244 #if __ARM_KERNEL_PROTECT__
245 /* All EL1-mode ASIDs are odd. */
246 orr x0, x0, #(1 << TTBR_ASID_SHIFT)
247 #endif /* __ARM_KERNEL_PROTECT__ */
254 * set AUX control register
258 .globl EXT(set_aux_control)
259 LEXT(set_aux_control)
261 // Synchronize system
265 #if __ARM_KERNEL_PROTECT__
268 .globl EXT(set_vbar_el1)
270 #if defined(KERNEL_INTEGRITY_KTRR)
271 b EXT(pinst_set_vbar)
276 #endif /* __ARM_KERNEL_PROTECT__ */
278 #if defined(HAS_VMSA_LOCK)
281 .globl EXT(vmsa_lock)
284 mov x1, #(VMSA_LOCK_SCTLR_M_BIT)
285 mov x0, #(VMSA_LOCK_TTBR1_EL1 | VMSA_LOCK_TCR_EL1 | VMSA_LOCK_VBAR_EL1)
287 msr ARM64_REG_VMSA_LOCK_EL1, x0
290 #endif /* defined(HAS_VMSA_LOCK) */
293 * set translation control register
299 #if defined(APPLE_ARM64_ARCH_FAMILY)
300 // Assert that T0Z is always equal to T1Z
301 eor x1, x0, x0, lsr #(TCR_T1SZ_SHIFT - TCR_T0SZ_SHIFT)
302 and x1, x1, #(TCR_TSZ_MASK << TCR_T0SZ_SHIFT)
303 cbnz x1, L_set_tcr_panic
304 #if defined(KERNEL_INTEGRITY_KTRR)
306 bl EXT(pinst_set_tcr)
309 #if defined(HAS_VMSA_LOCK)
310 // assert TCR unlocked
311 mrs x1, ARM64_REG_VMSA_LOCK_EL1
312 and x1, x1, #(VMSA_LOCK_TCR_EL1)
313 cbnz x1, L_set_locked_reg_panic
314 #endif /* defined(HAS_VMSA_LOCK) */
316 #endif /* defined(KERNEL_INTRITY_KTRR) */
324 adr x0, L_set_tcr_panic_str
327 L_set_locked_reg_panic:
331 adr x0, L_set_locked_reg_panic_str
336 .asciz "set_tcr: t0sz, t1sz not equal (%llx)\n"
339 L_set_locked_reg_panic_str:
340 .asciz "attempt to set locked register: (%llx)\n"
342 #if defined(KERNEL_INTEGRITY_KTRR) || defined(KERNEL_INTEGRITY_CTRR)
344 bl EXT(pinst_set_tcr)
351 #endif // defined(APPLE_ARM64_ARCH_FAMILY)
354 * MMU kernel virtual to physical address translation
358 .globl EXT(mmu_kvtop)
360 mrs x2, DAIF // Load current DAIF
361 msr DAIFSet, #(DAIFSC_IRQF | DAIFSC_FIQF) // Disable IRQ
362 at s1e1r, x0 // Translation Stage 1 EL1
363 mrs x1, PAR_EL1 // Read result
364 msr DAIF, x2 // Restore interrupt state
365 tbnz x1, #0, L_mmu_kvtop_invalid // Test Translation not valid
366 bfm x1, x0, #0, #11 // Add page offset
367 and x0, x1, #0x0000ffffffffffff // Clear non-address bits
370 mov x0, #0 // Return invalid
374 * MMU user virtual to physical address translation
378 .globl EXT(mmu_uvtop)
380 lsr x8, x0, #56 // Extract top byte
381 cbnz x8, L_mmu_uvtop_invalid // Tagged pointers are invalid
382 mrs x2, DAIF // Load current DAIF
383 msr DAIFSet, #(DAIFSC_IRQF | DAIFSC_FIQF) // Disable IRQ
384 at s1e0r, x0 // Translation Stage 1 EL0
385 mrs x1, PAR_EL1 // Read result
386 msr DAIF, x2 // Restore interrupt state
387 tbnz x1, #0, L_mmu_uvtop_invalid // Test Translation not valid
388 bfm x1, x0, #0, #11 // Add page offset
389 and x0, x1, #0x0000ffffffffffff // Clear non-address bits
392 mov x0, #0 // Return invalid
396 * MMU kernel virtual to physical address preflight write access
400 .globl EXT(mmu_kvtop_wpreflight)
401 LEXT(mmu_kvtop_wpreflight)
402 mrs x2, DAIF // Load current DAIF
403 msr DAIFSet, #(DAIFSC_IRQF | DAIFSC_FIQF) // Disable IRQ
404 at s1e1w, x0 // Translation Stage 1 EL1
405 mrs x1, PAR_EL1 // Read result
406 msr DAIF, x2 // Restore interrupt state
407 tbnz x1, #0, L_mmu_kvtop_wpreflight_invalid // Test Translation not valid
408 bfm x1, x0, #0, #11 // Add page offset
409 and x0, x1, #0x0000ffffffffffff // Clear non-address bits
411 L_mmu_kvtop_wpreflight_invalid:
412 mov x0, #0 // Return invalid
416 * SET_RECOVERY_HANDLER
418 * Sets up a page fault recovery handler
420 * arg0 - persisted thread pointer
421 * arg1 - persisted recovery handler
423 * arg3 - recovery label
425 .macro SET_RECOVERY_HANDLER
426 mrs $0, TPIDR_EL1 // Load thread pointer
427 adrp $2, $3@page // Load the recovery handler address
428 add $2, $2, $3@pageoff
429 #if defined(HAS_APPLE_PAC)
430 add $1, $0, TH_RECOVER
431 movk $1, #PAC_DISCRIMINATOR_RECOVER, lsl 48
432 pacia $2, $1 // Sign with IAKey + blended discriminator
435 ldr $1, [$0, TH_RECOVER] // Save previous recovery handler
436 str $2, [$0, TH_RECOVER] // Set new signed recovery handler
440 * CLEAR_RECOVERY_HANDLER
442 * Clears page fault handler set by SET_RECOVERY_HANDLER
444 * arg0 - thread pointer saved by SET_RECOVERY_HANDLER
445 * arg1 - old recovery handler saved by SET_RECOVERY_HANDLER
447 .macro CLEAR_RECOVERY_HANDLER
448 str $1, [$0, TH_RECOVER] // Restore the previous recovery handler
455 CLEAR_RECOVERY_HANDLER x10, x11
456 mov x0, #EFAULT // Return an EFAULT error
461 * int _bcopyin(const char *src, char *dst, vm_size_t len)
469 SET_RECOVERY_HANDLER x10, x11, x3, copyio_error
470 /* If len is less than 16 bytes, just do a bytewise copy */
475 /* 16 bytes at a time */
476 ldp x3, x4, [x0], #16
477 stp x3, x4, [x1], #16
480 /* Fixup the len and test for completion */
489 CLEAR_RECOVERY_HANDLER x10, x11
495 * int _copyin_atomic32(const char *src, uint32_t *dst)
499 .globl EXT(_copyin_atomic32)
500 LEXT(_copyin_atomic32)
503 SET_RECOVERY_HANDLER x10, x11, x3, copyio_error
507 CLEAR_RECOVERY_HANDLER x10, x11
512 * int _copyin_atomic32_wait_if_equals(const char *src, uint32_t value)
516 .globl EXT(_copyin_atomic32_wait_if_equals)
517 LEXT(_copyin_atomic32_wait_if_equals)
520 SET_RECOVERY_HANDLER x10, x11, x3, copyio_error
529 CLEAR_RECOVERY_HANDLER x10, x11
534 * int _copyin_atomic64(const char *src, uint32_t *dst)
538 .globl EXT(_copyin_atomic64)
539 LEXT(_copyin_atomic64)
542 SET_RECOVERY_HANDLER x10, x11, x3, copyio_error
546 CLEAR_RECOVERY_HANDLER x10, x11
552 * int _copyout_atomic32(uint32_t value, char *dst)
556 .globl EXT(_copyout_atomic32)
557 LEXT(_copyout_atomic32)
560 SET_RECOVERY_HANDLER x10, x11, x3, copyio_error
563 CLEAR_RECOVERY_HANDLER x10, x11
568 * int _copyout_atomic64(uint64_t value, char *dst)
572 .globl EXT(_copyout_atomic64)
573 LEXT(_copyout_atomic64)
576 SET_RECOVERY_HANDLER x10, x11, x3, copyio_error
579 CLEAR_RECOVERY_HANDLER x10, x11
585 * int _bcopyout(const char *src, char *dst, vm_size_t len)
589 .globl EXT(_bcopyout)
593 SET_RECOVERY_HANDLER x10, x11, x3, copyio_error
594 /* If len is less than 16 bytes, just do a bytewise copy */
599 /* 16 bytes at a time */
600 ldp x3, x4, [x0], #16
601 stp x3, x4, [x1], #16
604 /* Fixup the len and test for completion */
613 CLEAR_RECOVERY_HANDLER x10, x11
620 * const user_addr_t user_addr,
627 .globl EXT(_bcopyinstr)
631 adr x4, Lcopyinstr_error // Get address for recover
632 mrs x10, TPIDR_EL1 // Get thread pointer
633 ldr x11, [x10, TH_RECOVER] // Save previous recover
635 #if defined(HAS_APPLE_PAC)
636 add x5, x10, TH_RECOVER // Sign new pointer with IAKey + blended discriminator
637 movk x5, #PAC_DISCRIMINATOR_RECOVER, lsl 48
640 str x4, [x10, TH_RECOVER] // Store new recover
642 mov x4, #0 // x4 - total bytes copied
644 ldrb w5, [x0], #1 // Load a byte from the user source
645 strb w5, [x1], #1 // Store a byte to the kernel dest
646 add x4, x4, #1 // Increment bytes copied
647 cbz x5, Lcopyinstr_done // If this byte is null, we're done
648 cmp x4, x2 // If we're out of space, return an error
651 mov x5, #ENAMETOOLONG // Set current byte to error code for later return
653 str x4, [x3] // Return number of bytes copied
654 mov x0, x5 // Set error code (0 on success, ENAMETOOLONG on failure)
657 mov x0, #EFAULT // Return EFAULT on error
659 str x11, [x10, TH_RECOVER] // Restore old recover
664 * int copyinframe(const vm_address_t frame_addr, char *kernel_addr, bool is64bit)
666 * Safely copy sixteen bytes (the fixed top of an ARM64 frame) from
667 * either user or kernel memory, or 8 bytes (AArch32) from user only.
669 * x0 : address of frame to copy.
670 * x1 : kernel address at which to store data.
671 * w2 : whether to copy an AArch32 or AArch64 frame.
673 * x5 : temp (kernel virtual base)
675 * x10 : thread pointer (set by SET_RECOVERY_HANDLER)
676 * x11 : old recovery function (set by SET_RECOVERY_HANDLER)
677 * x12, x13 : backtrace data
682 .globl EXT(copyinframe)
686 SET_RECOVERY_HANDLER x10, x11, x3, copyio_error
687 cbnz w2, Lcopyinframe64 // Check frame size
688 adrp x5, EXT(gVirtBase)@page // For 32-bit frame, make sure we're not trying to copy from kernel
689 add x5, x5, EXT(gVirtBase)@pageoff
691 cmp x5, x0 // See if address is in kernel virtual range
692 b.hi Lcopyinframe32 // If below kernel virtual range, proceed.
693 mov w0, #EFAULT // Should never have a 32-bit frame in kernel virtual range
697 ldr x12, [x0] // Copy 8 bytes
699 mov w0, #0 // Success
703 mov x3, VM_MIN_KERNEL_ADDRESS // Check if kernel address
704 orr x9, x0, TBI_MASK // Hide tags in address comparison
705 cmp x9, x3 // If in kernel address range, skip tag test
706 b.hs Lcopyinframe_valid
707 tst x0, TBI_MASK // Detect tagged pointers
708 b.eq Lcopyinframe_valid
709 mov w0, #EFAULT // Tagged address, fail
712 ldp x12, x13, [x0] // Copy 16 bytes
714 mov w0, #0 // Success
717 CLEAR_RECOVERY_HANDLER x10, x11
723 * uint32_t arm_debug_read_dscr(void)
727 .globl EXT(arm_debug_read_dscr)
728 LEXT(arm_debug_read_dscr)
732 * void arm_debug_set_cp14(arm_debug_state_t *debug_state)
734 * Set debug registers to match the current thread state
735 * (NULL to disable). Assume 6 breakpoints and 2
736 * watchpoints, since that has been the case in all cores
741 .globl EXT(arm_debug_set_cp14)
742 LEXT(arm_debug_set_cp14)
745 #if defined(APPLE_ARM64_ARCH_FAMILY)
747 * Note: still have to ISB before executing wfi!
751 .globl EXT(arm64_prepare_for_sleep)
752 LEXT(arm64_prepare_for_sleep)
755 #if defined(APPLETYPHOON)
756 // <rdar://problem/15827409>
757 mrs x0, ARM64_REG_HID2 // Read HID2
758 orr x0, x0, #(ARM64_REG_HID2_disMMUmtlbPrefetch) // Set HID.DisableMTLBPrefetch
759 msr ARM64_REG_HID2, x0 // Write HID2
764 #if __ARM_GLOBAL_SLEEP_BIT__
766 mrs x1, ARM64_REG_ACC_OVRD
767 orr x1, x1, #(ARM64_REG_ACC_OVRD_enDeepSleep)
768 and x1, x1, #(~(ARM64_REG_ACC_OVRD_disL2Flush4AccSlp_mask))
769 orr x1, x1, #( ARM64_REG_ACC_OVRD_disL2Flush4AccSlp_deepsleep)
770 and x1, x1, #(~(ARM64_REG_ACC_OVRD_ok2PwrDnSRM_mask))
771 orr x1, x1, #( ARM64_REG_ACC_OVRD_ok2PwrDnSRM_deepsleep)
772 and x1, x1, #(~(ARM64_REG_ACC_OVRD_ok2TrDnLnk_mask))
773 orr x1, x1, #( ARM64_REG_ACC_OVRD_ok2TrDnLnk_deepsleep)
774 and x1, x1, #(~(ARM64_REG_ACC_OVRD_ok2PwrDnCPM_mask))
775 orr x1, x1, #( ARM64_REG_ACC_OVRD_ok2PwrDnCPM_deepsleep)
776 #if HAS_RETENTION_STATE
777 orr x1, x1, #(ARM64_REG_ACC_OVRD_disPioOnWfiCpu)
779 msr ARM64_REG_ACC_OVRD, x1
784 mov x1, ARM64_REG_CYC_CFG_deepSleep
785 msr ARM64_REG_CYC_CFG, x1
787 // Set "OK to power down" (<rdar://problem/12390433>)
788 mrs x0, ARM64_REG_CYC_OVRD
789 orr x0, x0, #(ARM64_REG_CYC_OVRD_ok2pwrdn_force_down)
790 #if HAS_RETENTION_STATE
791 orr x0, x0, #(ARM64_REG_CYC_OVRD_disWfiRetn)
793 msr ARM64_REG_CYC_OVRD, x0
795 #if defined(APPLEMONSOON) || defined(APPLEVORTEX)
797 cbz x0, Lwfi_inst // skip if not p-core
799 /* <rdar://problem/32512947>: Flush the GUPS prefetcher prior to
800 * wfi. A Skye HW bug can cause the GUPS prefetcher on p-cores
801 * to be left with valid entries that fail to drain if a
802 * subsequent wfi is issued. This can prevent the core from
803 * power-gating. For the idle case that is recoverable, but
804 * for the deep-sleep (S2R) case in which cores MUST power-gate,
805 * it can lead to a hang. This can be prevented by disabling
806 * and re-enabling GUPS, which forces the prefetch queue to
807 * drain. This should be done as close to wfi as possible, i.e.
808 * at the very end of arm64_prepare_for_sleep(). */
809 #if defined(APPLEVORTEX)
810 /* <rdar://problem/32821461>: Cyprus A0/A1 parts have a similar
811 * bug in the HSP prefetcher that can be worked around through
812 * the same method mentioned above for Skye. */
813 SKIP_IF_CPU_VERSION_GREATER_OR_EQUAL x0, VORTEX_CPU_VERSION_B0, Lwfi_inst
815 mrs x0, ARM64_REG_HID10
816 orr x0, x0, #(ARM64_REG_HID10_DisHwpGups)
817 msr ARM64_REG_HID10, x0
819 and x0, x0, #(~(ARM64_REG_HID10_DisHwpGups))
820 msr ARM64_REG_HID10, x0
830 * Force WFI to use clock gating only
835 .globl EXT(arm64_force_wfi_clock_gate)
836 LEXT(arm64_force_wfi_clock_gate)
840 mrs x0, ARM64_REG_CYC_OVRD
841 orr x0, x0, #(ARM64_REG_CYC_OVRD_ok2pwrdn_force_up)
842 msr ARM64_REG_CYC_OVRD, x0
848 #if HAS_RETENTION_STATE
851 .globl EXT(arm64_retention_wfi)
852 LEXT(arm64_retention_wfi)
854 cbz lr, Lwfi_retention // If lr is 0, we entered retention state and lost all GPRs except sp and pc
855 ret // Otherwise just return to cpu_idle()
860 bl EXT(cpu_idle_exit) // cpu_idle_exit(from_reset = FALSE)
861 b . // cpu_idle_exit() should never return
864 #if defined(APPLETYPHOON)
868 .globl EXT(typhoon_prepare_for_wfi)
870 LEXT(typhoon_prepare_for_wfi)
873 // <rdar://problem/15827409>
874 mrs x0, ARM64_REG_HID2 // Read HID2
875 orr x0, x0, #(ARM64_REG_HID2_disMMUmtlbPrefetch) // Set HID.DisableMTLBPrefetch
876 msr ARM64_REG_HID2, x0 // Write HID2
886 .globl EXT(typhoon_return_from_wfi)
887 LEXT(typhoon_return_from_wfi)
890 // <rdar://problem/15827409>
891 mrs x0, ARM64_REG_HID2 // Read HID2
892 mov x1, #(ARM64_REG_HID2_disMMUmtlbPrefetch) //
893 bic x0, x0, x1 // Clear HID.DisableMTLBPrefetchMTLBPrefetch
894 msr ARM64_REG_HID2, x0 // Write HID2
904 #define HID0_DEFEATURES_1 0x0000a0c000064010ULL
905 #define HID1_DEFEATURES_1 0x000000004005bf20ULL
906 #define HID2_DEFEATURES_1 0x0000000000102074ULL
907 #define HID3_DEFEATURES_1 0x0000000000400003ULL
908 #define HID4_DEFEATURES_1 0x83ff00e100000268ULL
909 #define HID7_DEFEATURES_1 0x000000000000000eULL
911 #define HID0_DEFEATURES_2 0x0000a1c000020010ULL
912 #define HID1_DEFEATURES_2 0x000000000005d720ULL
913 #define HID2_DEFEATURES_2 0x0000000000002074ULL
914 #define HID3_DEFEATURES_2 0x0000000000400001ULL
915 #define HID4_DEFEATURES_2 0x8390000200000208ULL
916 #define HID7_DEFEATURES_2 0x0000000000000000ULL
919 arg0 = target register
920 arg1 = 64-bit constant
923 movz $0, #(($1 >> 48) & 0xffff), lsl #48
924 movk $0, #(($1 >> 32) & 0xffff), lsl #32
925 movk $0, #(($1 >> 16) & 0xffff), lsl #16
926 movk $0, #(($1) & 0xffff)
931 .globl EXT(cpu_defeatures_set)
932 LEXT(cpu_defeatures_set)
935 b.eq cpu_defeatures_set_2
937 b.ne cpu_defeatures_set_ret
938 LOAD_UINT64 x1, HID0_DEFEATURES_1
939 mrs x0, ARM64_REG_HID0
941 msr ARM64_REG_HID0, x0
942 LOAD_UINT64 x1, HID1_DEFEATURES_1
943 mrs x0, ARM64_REG_HID1
945 msr ARM64_REG_HID1, x0
946 LOAD_UINT64 x1, HID2_DEFEATURES_1
947 mrs x0, ARM64_REG_HID2
949 msr ARM64_REG_HID2, x0
950 LOAD_UINT64 x1, HID3_DEFEATURES_1
951 mrs x0, ARM64_REG_HID3
953 msr ARM64_REG_HID3, x0
954 LOAD_UINT64 x1, HID4_DEFEATURES_1
955 mrs x0, ARM64_REG_HID4
957 msr ARM64_REG_HID4, x0
958 LOAD_UINT64 x1, HID7_DEFEATURES_1
959 mrs x0, ARM64_REG_HID7
961 msr ARM64_REG_HID7, x0
964 b cpu_defeatures_set_ret
965 cpu_defeatures_set_2:
966 LOAD_UINT64 x1, HID0_DEFEATURES_2
967 mrs x0, ARM64_REG_HID0
969 msr ARM64_REG_HID0, x0
970 LOAD_UINT64 x1, HID1_DEFEATURES_2
971 mrs x0, ARM64_REG_HID1
973 msr ARM64_REG_HID1, x0
974 LOAD_UINT64 x1, HID2_DEFEATURES_2
975 mrs x0, ARM64_REG_HID2
977 msr ARM64_REG_HID2, x0
978 LOAD_UINT64 x1, HID3_DEFEATURES_2
979 mrs x0, ARM64_REG_HID3
981 msr ARM64_REG_HID3, x0
982 LOAD_UINT64 x1, HID4_DEFEATURES_2
983 mrs x0, ARM64_REG_HID4
985 msr ARM64_REG_HID4, x0
986 LOAD_UINT64 x1, HID7_DEFEATURES_2
987 mrs x0, ARM64_REG_HID7
989 msr ARM64_REG_HID7, x0
992 b cpu_defeatures_set_ret
993 cpu_defeatures_set_ret:
998 #else /* !defined(APPLE_ARM64_ARCH_FAMILY) */
1001 .globl EXT(arm64_prepare_for_sleep)
1002 LEXT(arm64_prepare_for_sleep)
1011 * Force WFI to use clock gating only
1012 * Note: for non-Apple device, do nothing.
1016 .globl EXT(arm64_force_wfi_clock_gate)
1017 LEXT(arm64_force_wfi_clock_gate)
1022 #endif /* defined(APPLE_ARM64_ARCH_FAMILY) */
1025 * void arm64_replace_bootstack(cpu_data_t *cpu_data)
1027 * This must be called from a kernel thread context running on the boot CPU,
1028 * after setting up new exception stacks in per-CPU data. That will guarantee
1029 * that the stack(s) we're trying to replace aren't currently in use. For
1030 * KTRR-protected devices, this must also be called prior to VM prot finalization
1031 * and lockdown, as updating SP1 requires a sensitive instruction.
1035 .globl EXT(arm64_replace_bootstack)
1036 LEXT(arm64_replace_bootstack)
1039 // Set the exception stack pointer
1040 ldr x0, [x0, CPU_EXCEPSTACK_TOP]
1041 mrs x4, DAIF // Load current DAIF; use x4 as pinst may trash x1-x3
1042 msr DAIFSet, #(DAIFSC_IRQF | DAIFSC_FIQF | DAIFSC_ASYNCF) // Disable IRQ/FIQ/serror
1043 // Set SP_EL1 to exception stack
1044 #if defined(KERNEL_INTEGRITY_KTRR) || defined(KERNEL_INTEGRITY_CTRR)
1046 bl EXT(pinst_spsel_1)
1053 msr DAIF, x4 // Restore interrupt state
1059 * unsigned long monitor_call(uintptr_t callnum, uintptr_t arg1,
1060 uintptr_t arg2, uintptr_t arg3)
1062 * Call the EL3 monitor with 4 arguments in registers
1063 * The monitor interface maintains the same ABI as the C function call standard. Callee-saved
1064 * registers are preserved, temporary registers are not. Parameters and results are passed in
1069 .globl EXT(monitor_call)
1075 #ifdef HAS_APPLE_PAC
1077 * void ml_sign_thread_state(arm_saved_state_t *ss, uint64_t pc,
1078 * uint32_t cpsr, uint64_t lr, uint64_t x16,
1083 .globl EXT(ml_sign_thread_state)
1084 LEXT(ml_sign_thread_state)
1085 pacga x1, x1, x0 /* PC hash (gkey + &arm_saved_state) */
1087 * Mask off the carry flag so we don't need to re-sign when that flag is
1088 * touched by the system call return path.
1091 pacga x1, x2, x1 /* SPSR hash (gkey + pc hash) */
1092 pacga x1, x3, x1 /* LR Hash (gkey + spsr hash) */
1093 pacga x1, x4, x1 /* X16 hash (gkey + lr hash) */
1094 pacga x1, x5, x1 /* X17 hash (gkey + x16 hash) */
1095 str x1, [x0, SS64_JOPHASH]
1099 * void ml_check_signed_state(arm_saved_state_t *ss, uint64_t pc,
1100 * uint32_t cpsr, uint64_t lr, uint64_t x16,
1105 .globl EXT(ml_check_signed_state)
1106 LEXT(ml_check_signed_state)
1107 pacga x1, x1, x0 /* PC hash (gkey + &arm_saved_state) */
1109 * Mask off the carry flag so we don't need to re-sign when that flag is
1110 * touched by the system call return path.
1113 pacga x1, x2, x1 /* SPSR hash (gkey + pc hash) */
1114 pacga x1, x3, x1 /* LR Hash (gkey + spsr hash) */
1115 pacga x1, x4, x1 /* X16 hash (gkey + lr hash) */
1116 pacga x1, x5, x1 /* X17 hash (gkey + x16 hash) */
1117 ldr x2, [x0, SS64_JOPHASH]
1119 b.ne Lcheck_hash_panic
1123 adr x0, Lcheck_hash_str
1124 CALL_EXTERN panic_with_thread_kernel_state
1126 .asciz "JOP Hash Mismatch Detected (PC, CPSR, or LR corruption)"
1127 #endif /* HAS_APPLE_PAC */
1131 .globl EXT(fill32_dczva)
1142 .globl EXT(fill32_nt)
1147 stnp q0, q0, [x0, #0x20]
1148 stnp q0, q0, [x0, #0x40]
1149 stnp q0, q0, [x0, #0x60]
1155 /* vim: set sw=4 ts=4: */