162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci#ifndef __ASM_ASM_POINTER_AUTH_H
362306a36Sopenharmony_ci#define __ASM_ASM_POINTER_AUTH_H
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci#include <asm/alternative.h>
662306a36Sopenharmony_ci#include <asm/asm-offsets.h>
762306a36Sopenharmony_ci#include <asm/cpufeature.h>
862306a36Sopenharmony_ci#include <asm/sysreg.h>
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#ifdef CONFIG_ARM64_PTR_AUTH_KERNEL
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci	.macro __ptrauth_keys_install_kernel_nosync tsk, tmp1, tmp2, tmp3
1362306a36Sopenharmony_ci	mov	\tmp1, #THREAD_KEYS_KERNEL
1462306a36Sopenharmony_ci	add	\tmp1, \tsk, \tmp1
1562306a36Sopenharmony_ci	ldp	\tmp2, \tmp3, [\tmp1, #PTRAUTH_KERNEL_KEY_APIA]
1662306a36Sopenharmony_ci	msr_s	SYS_APIAKEYLO_EL1, \tmp2
1762306a36Sopenharmony_ci	msr_s	SYS_APIAKEYHI_EL1, \tmp3
1862306a36Sopenharmony_ci	.endm
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci	.macro ptrauth_keys_install_kernel_nosync tsk, tmp1, tmp2, tmp3
2162306a36Sopenharmony_cialternative_if ARM64_HAS_ADDRESS_AUTH
2262306a36Sopenharmony_ci	__ptrauth_keys_install_kernel_nosync \tsk, \tmp1, \tmp2, \tmp3
2362306a36Sopenharmony_cialternative_else_nop_endif
2462306a36Sopenharmony_ci	.endm
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci	.macro ptrauth_keys_install_kernel tsk, tmp1, tmp2, tmp3
2762306a36Sopenharmony_cialternative_if ARM64_HAS_ADDRESS_AUTH
2862306a36Sopenharmony_ci	__ptrauth_keys_install_kernel_nosync \tsk, \tmp1, \tmp2, \tmp3
2962306a36Sopenharmony_ci	isb
3062306a36Sopenharmony_cialternative_else_nop_endif
3162306a36Sopenharmony_ci	.endm
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci#else /* CONFIG_ARM64_PTR_AUTH_KERNEL */
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci	.macro __ptrauth_keys_install_kernel_nosync tsk, tmp1, tmp2, tmp3
3662306a36Sopenharmony_ci	.endm
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci	.macro ptrauth_keys_install_kernel_nosync tsk, tmp1, tmp2, tmp3
3962306a36Sopenharmony_ci	.endm
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci	.macro ptrauth_keys_install_kernel tsk, tmp1, tmp2, tmp3
4262306a36Sopenharmony_ci	.endm
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci#endif /* CONFIG_ARM64_PTR_AUTH_KERNEL */
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci#ifdef CONFIG_ARM64_PTR_AUTH
4762306a36Sopenharmony_ci/*
4862306a36Sopenharmony_ci * thread.keys_user.ap* as offset exceeds the #imm offset range
4962306a36Sopenharmony_ci * so use the base value of ldp as thread.keys_user and offset as
5062306a36Sopenharmony_ci * thread.keys_user.ap*.
5162306a36Sopenharmony_ci */
5262306a36Sopenharmony_ci	.macro __ptrauth_keys_install_user tsk, tmp1, tmp2, tmp3
5362306a36Sopenharmony_ci	mov	\tmp1, #THREAD_KEYS_USER
5462306a36Sopenharmony_ci	add	\tmp1, \tsk, \tmp1
5562306a36Sopenharmony_ci	ldp	\tmp2, \tmp3, [\tmp1, #PTRAUTH_USER_KEY_APIA]
5662306a36Sopenharmony_ci	msr_s	SYS_APIAKEYLO_EL1, \tmp2
5762306a36Sopenharmony_ci	msr_s	SYS_APIAKEYHI_EL1, \tmp3
5862306a36Sopenharmony_ci	.endm
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci	.macro __ptrauth_keys_init_cpu tsk, tmp1, tmp2, tmp3
6162306a36Sopenharmony_ci	mrs	\tmp1, id_aa64isar1_el1
6262306a36Sopenharmony_ci	ubfx	\tmp1, \tmp1, #ID_AA64ISAR1_EL1_APA_SHIFT, #8
6362306a36Sopenharmony_ci	mrs_s	\tmp2, SYS_ID_AA64ISAR2_EL1
6462306a36Sopenharmony_ci	ubfx	\tmp2, \tmp2, #ID_AA64ISAR2_EL1_APA3_SHIFT, #4
6562306a36Sopenharmony_ci	orr	\tmp1, \tmp1, \tmp2
6662306a36Sopenharmony_ci	cbz	\tmp1, .Lno_addr_auth\@
6762306a36Sopenharmony_ci	mov_q	\tmp1, (SCTLR_ELx_ENIA | SCTLR_ELx_ENIB | \
6862306a36Sopenharmony_ci			SCTLR_ELx_ENDA | SCTLR_ELx_ENDB)
6962306a36Sopenharmony_ci	mrs	\tmp2, sctlr_el1
7062306a36Sopenharmony_ci	orr	\tmp2, \tmp2, \tmp1
7162306a36Sopenharmony_ci	msr	sctlr_el1, \tmp2
7262306a36Sopenharmony_ci	__ptrauth_keys_install_kernel_nosync \tsk, \tmp1, \tmp2, \tmp3
7362306a36Sopenharmony_ci	isb
7462306a36Sopenharmony_ci.Lno_addr_auth\@:
7562306a36Sopenharmony_ci	.endm
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci	.macro ptrauth_keys_init_cpu tsk, tmp1, tmp2, tmp3
7862306a36Sopenharmony_cialternative_if_not ARM64_HAS_ADDRESS_AUTH
7962306a36Sopenharmony_ci	b	.Lno_addr_auth\@
8062306a36Sopenharmony_cialternative_else_nop_endif
8162306a36Sopenharmony_ci	__ptrauth_keys_init_cpu \tsk, \tmp1, \tmp2, \tmp3
8262306a36Sopenharmony_ci.Lno_addr_auth\@:
8362306a36Sopenharmony_ci	.endm
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci#else /* !CONFIG_ARM64_PTR_AUTH */
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci	.macro ptrauth_keys_install_user tsk, tmp1, tmp2, tmp3
8862306a36Sopenharmony_ci	.endm
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci#endif /* CONFIG_ARM64_PTR_AUTH */
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci#endif /* __ASM_ASM_POINTER_AUTH_H */
93