1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* arch/arm64/include/asm/kvm_ptrauth.h: Guest/host ptrauth save/restore
3 * Copyright 2019 Arm Limited
4 * Authors: Mark Rutland <mark.rutland@arm.com>
5 * Amit Daniel Kachhap <amit.kachhap@arm.com>
8 #ifndef __ASM_KVM_PTRAUTH_H
9 #define __ASM_KVM_PTRAUTH_H
13 #include <asm/sysreg.h>
15 #ifdef CONFIG_ARM64_PTR_AUTH
17 #define PTRAUTH_REG_OFFSET(x) (x - CPU_APIAKEYLO_EL1)
20 * CPU_AP*_EL1 values exceed immediate offset range (512) for stp
21 * instruction so below macros takes CPU_APIAKEYLO_EL1 as base and
22 * calculates the offset of the keys from this base to avoid an extra add
23 * instruction. These macros assumes the keys offsets follow the order of
24 * the sysreg enum in kvm_host.h.
26 .macro ptrauth_save_state base
, reg1
, reg2
27 mrs_s
\reg
1, SYS_APIAKEYLO_EL1
28 mrs_s
\reg
2, SYS_APIAKEYHI_EL1
29 stp
\reg
1, \reg
2, [\base
, #PTRAUTH_REG_OFFSET(CPU_APIAKEYLO_EL1)]
30 mrs_s
\reg
1, SYS_APIBKEYLO_EL1
31 mrs_s
\reg
2, SYS_APIBKEYHI_EL1
32 stp
\reg
1, \reg
2, [\base
, #PTRAUTH_REG_OFFSET(CPU_APIBKEYLO_EL1)]
33 mrs_s
\reg
1, SYS_APDAKEYLO_EL1
34 mrs_s
\reg
2, SYS_APDAKEYHI_EL1
35 stp
\reg
1, \reg
2, [\base
, #PTRAUTH_REG_OFFSET(CPU_APDAKEYLO_EL1)]
36 mrs_s
\reg
1, SYS_APDBKEYLO_EL1
37 mrs_s
\reg
2, SYS_APDBKEYHI_EL1
38 stp
\reg
1, \reg
2, [\base
, #PTRAUTH_REG_OFFSET(CPU_APDBKEYLO_EL1)]
39 mrs_s
\reg
1, SYS_APGAKEYLO_EL1
40 mrs_s
\reg
2, SYS_APGAKEYHI_EL1
41 stp
\reg
1, \reg
2, [\base
, #PTRAUTH_REG_OFFSET(CPU_APGAKEYLO_EL1)]
44 .macro ptrauth_restore_state base
, reg1
, reg2
45 ldp
\reg
1, \reg
2, [\base
, #PTRAUTH_REG_OFFSET(CPU_APIAKEYLO_EL1)]
46 msr_s SYS_APIAKEYLO_EL1
, \reg
1
47 msr_s SYS_APIAKEYHI_EL1
, \reg
2
48 ldp
\reg
1, \reg
2, [\base
, #PTRAUTH_REG_OFFSET(CPU_APIBKEYLO_EL1)]
49 msr_s SYS_APIBKEYLO_EL1
, \reg
1
50 msr_s SYS_APIBKEYHI_EL1
, \reg
2
51 ldp
\reg
1, \reg
2, [\base
, #PTRAUTH_REG_OFFSET(CPU_APDAKEYLO_EL1)]
52 msr_s SYS_APDAKEYLO_EL1
, \reg
1
53 msr_s SYS_APDAKEYHI_EL1
, \reg
2
54 ldp
\reg
1, \reg
2, [\base
, #PTRAUTH_REG_OFFSET(CPU_APDBKEYLO_EL1)]
55 msr_s SYS_APDBKEYLO_EL1
, \reg
1
56 msr_s SYS_APDBKEYHI_EL1
, \reg
2
57 ldp
\reg
1, \reg
2, [\base
, #PTRAUTH_REG_OFFSET(CPU_APGAKEYLO_EL1)]
58 msr_s SYS_APGAKEYLO_EL1
, \reg
1
59 msr_s SYS_APGAKEYHI_EL1
, \reg
2
63 * Both ptrauth_switch_to_guest and ptrauth_switch_to_host macros will
64 * check for the presence of one of the cpufeature flag
65 * ARM64_HAS_ADDRESS_AUTH_ARCH or ARM64_HAS_ADDRESS_AUTH_IMP_DEF and
66 * then proceed ahead with the save/restore of Pointer Authentication
69 .macro ptrauth_switch_to_guest g_ctxt
, reg1
, reg2
, reg3
70 alternative_if ARM64_HAS_ADDRESS_AUTH_ARCH
72 alternative_else_nop_endif
73 alternative_if_not ARM64_HAS_ADDRESS_AUTH_IMP_DEF
75 alternative_else_nop_endif
77 ldr
\reg
1, [\g_ctxt
, #(VCPU_HCR_EL2 - VCPU_CONTEXT)]
78 and \reg
1, \reg
1, #(HCR_API | HCR_APK)
80 add
\reg
1, \g_ctxt
, #CPU_APIAKEYLO_EL1
81 ptrauth_restore_state
\reg
1, \reg
2, \reg
3
85 .macro ptrauth_switch_to_host g_ctxt
, h_ctxt
, reg1
, reg2
, reg3
86 alternative_if ARM64_HAS_ADDRESS_AUTH_ARCH
88 alternative_else_nop_endif
89 alternative_if_not ARM64_HAS_ADDRESS_AUTH_IMP_DEF
91 alternative_else_nop_endif
93 ldr
\reg
1, [\g_ctxt
, #(VCPU_HCR_EL2 - VCPU_CONTEXT)]
94 and \reg
1, \reg
1, #(HCR_API | HCR_APK)
96 add
\reg
1, \g_ctxt
, #CPU_APIAKEYLO_EL1
97 ptrauth_save_state
\reg
1, \reg
2, \reg
3
98 add
\reg
1, \h_ctxt
, #CPU_APIAKEYLO_EL1
99 ptrauth_restore_state
\reg
1, \reg
2, \reg
3
104 #else /* !CONFIG_ARM64_PTR_AUTH */
105 .macro ptrauth_switch_to_guest g_ctxt
, reg1
, reg2
, reg3
107 .macro ptrauth_switch_to_host g_ctxt
, h_ctxt
, reg1
, reg2
, reg3
109 #endif /* CONFIG_ARM64_PTR_AUTH */
110 #endif /* __ASSEMBLY__ */
111 #endif /* __ASM_KVM_PTRAUTH_H */