162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/* arch/arm64/include/asm/kvm_ptrauth.h: Guest/host ptrauth save/restore
362306a36Sopenharmony_ci * Copyright 2019 Arm Limited
462306a36Sopenharmony_ci * Authors: Mark Rutland <mark.rutland@arm.com>
562306a36Sopenharmony_ci *         Amit Daniel Kachhap <amit.kachhap@arm.com>
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#ifndef __ASM_KVM_PTRAUTH_H
962306a36Sopenharmony_ci#define __ASM_KVM_PTRAUTH_H
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#ifdef __ASSEMBLY__
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci#include <asm/sysreg.h>
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci#ifdef	CONFIG_ARM64_PTR_AUTH
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci#define PTRAUTH_REG_OFFSET(x)	(x - CPU_APIAKEYLO_EL1)
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci/*
2062306a36Sopenharmony_ci * CPU_AP*_EL1 values exceed immediate offset range (512) for stp
2162306a36Sopenharmony_ci * instruction so below macros takes CPU_APIAKEYLO_EL1 as base and
2262306a36Sopenharmony_ci * calculates the offset of the keys from this base to avoid an extra add
2362306a36Sopenharmony_ci * instruction. These macros assumes the keys offsets follow the order of
2462306a36Sopenharmony_ci * the sysreg enum in kvm_host.h.
2562306a36Sopenharmony_ci */
2662306a36Sopenharmony_ci.macro	ptrauth_save_state base, reg1, reg2
2762306a36Sopenharmony_ci	mrs_s	\reg1, SYS_APIAKEYLO_EL1
2862306a36Sopenharmony_ci	mrs_s	\reg2, SYS_APIAKEYHI_EL1
2962306a36Sopenharmony_ci	stp	\reg1, \reg2, [\base, #PTRAUTH_REG_OFFSET(CPU_APIAKEYLO_EL1)]
3062306a36Sopenharmony_ci	mrs_s	\reg1, SYS_APIBKEYLO_EL1
3162306a36Sopenharmony_ci	mrs_s	\reg2, SYS_APIBKEYHI_EL1
3262306a36Sopenharmony_ci	stp	\reg1, \reg2, [\base, #PTRAUTH_REG_OFFSET(CPU_APIBKEYLO_EL1)]
3362306a36Sopenharmony_ci	mrs_s	\reg1, SYS_APDAKEYLO_EL1
3462306a36Sopenharmony_ci	mrs_s	\reg2, SYS_APDAKEYHI_EL1
3562306a36Sopenharmony_ci	stp	\reg1, \reg2, [\base, #PTRAUTH_REG_OFFSET(CPU_APDAKEYLO_EL1)]
3662306a36Sopenharmony_ci	mrs_s	\reg1, SYS_APDBKEYLO_EL1
3762306a36Sopenharmony_ci	mrs_s	\reg2, SYS_APDBKEYHI_EL1
3862306a36Sopenharmony_ci	stp	\reg1, \reg2, [\base, #PTRAUTH_REG_OFFSET(CPU_APDBKEYLO_EL1)]
3962306a36Sopenharmony_ci	mrs_s	\reg1, SYS_APGAKEYLO_EL1
4062306a36Sopenharmony_ci	mrs_s	\reg2, SYS_APGAKEYHI_EL1
4162306a36Sopenharmony_ci	stp	\reg1, \reg2, [\base, #PTRAUTH_REG_OFFSET(CPU_APGAKEYLO_EL1)]
4262306a36Sopenharmony_ci.endm
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci.macro	ptrauth_restore_state base, reg1, reg2
4562306a36Sopenharmony_ci	ldp	\reg1, \reg2, [\base, #PTRAUTH_REG_OFFSET(CPU_APIAKEYLO_EL1)]
4662306a36Sopenharmony_ci	msr_s	SYS_APIAKEYLO_EL1, \reg1
4762306a36Sopenharmony_ci	msr_s	SYS_APIAKEYHI_EL1, \reg2
4862306a36Sopenharmony_ci	ldp	\reg1, \reg2, [\base, #PTRAUTH_REG_OFFSET(CPU_APIBKEYLO_EL1)]
4962306a36Sopenharmony_ci	msr_s	SYS_APIBKEYLO_EL1, \reg1
5062306a36Sopenharmony_ci	msr_s	SYS_APIBKEYHI_EL1, \reg2
5162306a36Sopenharmony_ci	ldp	\reg1, \reg2, [\base, #PTRAUTH_REG_OFFSET(CPU_APDAKEYLO_EL1)]
5262306a36Sopenharmony_ci	msr_s	SYS_APDAKEYLO_EL1, \reg1
5362306a36Sopenharmony_ci	msr_s	SYS_APDAKEYHI_EL1, \reg2
5462306a36Sopenharmony_ci	ldp	\reg1, \reg2, [\base, #PTRAUTH_REG_OFFSET(CPU_APDBKEYLO_EL1)]
5562306a36Sopenharmony_ci	msr_s	SYS_APDBKEYLO_EL1, \reg1
5662306a36Sopenharmony_ci	msr_s	SYS_APDBKEYHI_EL1, \reg2
5762306a36Sopenharmony_ci	ldp	\reg1, \reg2, [\base, #PTRAUTH_REG_OFFSET(CPU_APGAKEYLO_EL1)]
5862306a36Sopenharmony_ci	msr_s	SYS_APGAKEYLO_EL1, \reg1
5962306a36Sopenharmony_ci	msr_s	SYS_APGAKEYHI_EL1, \reg2
6062306a36Sopenharmony_ci.endm
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci/*
6362306a36Sopenharmony_ci * Both ptrauth_switch_to_guest and ptrauth_switch_to_hyp macros will
6462306a36Sopenharmony_ci * check for the presence ARM64_HAS_ADDRESS_AUTH, which is defined as
6562306a36Sopenharmony_ci * (ARM64_HAS_ADDRESS_AUTH_ARCH || ARM64_HAS_ADDRESS_AUTH_IMP_DEF) and
6662306a36Sopenharmony_ci * then proceed ahead with the save/restore of Pointer Authentication
6762306a36Sopenharmony_ci * key registers if enabled for the guest.
6862306a36Sopenharmony_ci */
6962306a36Sopenharmony_ci.macro ptrauth_switch_to_guest g_ctxt, reg1, reg2, reg3
7062306a36Sopenharmony_cialternative_if_not ARM64_HAS_ADDRESS_AUTH
7162306a36Sopenharmony_ci	b	.L__skip_switch\@
7262306a36Sopenharmony_cialternative_else_nop_endif
7362306a36Sopenharmony_ci	mrs	\reg1, hcr_el2
7462306a36Sopenharmony_ci	and	\reg1, \reg1, #(HCR_API | HCR_APK)
7562306a36Sopenharmony_ci	cbz	\reg1, .L__skip_switch\@
7662306a36Sopenharmony_ci	add	\reg1, \g_ctxt, #CPU_APIAKEYLO_EL1
7762306a36Sopenharmony_ci	ptrauth_restore_state	\reg1, \reg2, \reg3
7862306a36Sopenharmony_ci.L__skip_switch\@:
7962306a36Sopenharmony_ci.endm
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci.macro ptrauth_switch_to_hyp g_ctxt, h_ctxt, reg1, reg2, reg3
8262306a36Sopenharmony_cialternative_if_not ARM64_HAS_ADDRESS_AUTH
8362306a36Sopenharmony_ci	b	.L__skip_switch\@
8462306a36Sopenharmony_cialternative_else_nop_endif
8562306a36Sopenharmony_ci	mrs	\reg1, hcr_el2
8662306a36Sopenharmony_ci	and	\reg1, \reg1, #(HCR_API | HCR_APK)
8762306a36Sopenharmony_ci	cbz	\reg1, .L__skip_switch\@
8862306a36Sopenharmony_ci	add	\reg1, \g_ctxt, #CPU_APIAKEYLO_EL1
8962306a36Sopenharmony_ci	ptrauth_save_state	\reg1, \reg2, \reg3
9062306a36Sopenharmony_ci	add	\reg1, \h_ctxt, #CPU_APIAKEYLO_EL1
9162306a36Sopenharmony_ci	ptrauth_restore_state	\reg1, \reg2, \reg3
9262306a36Sopenharmony_ci	isb
9362306a36Sopenharmony_ci.L__skip_switch\@:
9462306a36Sopenharmony_ci.endm
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci#else /* !CONFIG_ARM64_PTR_AUTH */
9762306a36Sopenharmony_ci.macro ptrauth_switch_to_guest g_ctxt, reg1, reg2, reg3
9862306a36Sopenharmony_ci.endm
9962306a36Sopenharmony_ci.macro ptrauth_switch_to_hyp g_ctxt, h_ctxt, reg1, reg2, reg3
10062306a36Sopenharmony_ci.endm
10162306a36Sopenharmony_ci#endif /* CONFIG_ARM64_PTR_AUTH */
10262306a36Sopenharmony_ci#endif /* __ASSEMBLY__ */
10362306a36Sopenharmony_ci#endif /* __ASM_KVM_PTRAUTH_H */
104