1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef __ASM_ASM_POINTER_AUTH_H
3#define __ASM_ASM_POINTER_AUTH_H
4
5#include <asm/alternative.h>
6#include <asm/asm-offsets.h>
7#include <asm/cpufeature.h>
8#include <asm/sysreg.h>
9
10#ifdef CONFIG_ARM64_PTR_AUTH
11/*
12 * thread.keys_user.ap* as offset exceeds the #imm offset range
13 * so use the base value of ldp as thread.keys_user and offset as
14 * thread.keys_user.ap*.
15 */
16	.macro ptrauth_keys_install_user tsk, tmp1, tmp2, tmp3
17	mov	\tmp1, #THREAD_KEYS_USER
18	add	\tmp1, \tsk, \tmp1
19alternative_if_not ARM64_HAS_ADDRESS_AUTH
20	b	.Laddr_auth_skip_\@
21alternative_else_nop_endif
22	ldp	\tmp2, \tmp3, [\tmp1, #PTRAUTH_USER_KEY_APIA]
23	msr_s	SYS_APIAKEYLO_EL1, \tmp2
24	msr_s	SYS_APIAKEYHI_EL1, \tmp3
25	ldp	\tmp2, \tmp3, [\tmp1, #PTRAUTH_USER_KEY_APIB]
26	msr_s	SYS_APIBKEYLO_EL1, \tmp2
27	msr_s	SYS_APIBKEYHI_EL1, \tmp3
28	ldp	\tmp2, \tmp3, [\tmp1, #PTRAUTH_USER_KEY_APDA]
29	msr_s	SYS_APDAKEYLO_EL1, \tmp2
30	msr_s	SYS_APDAKEYHI_EL1, \tmp3
31	ldp	\tmp2, \tmp3, [\tmp1, #PTRAUTH_USER_KEY_APDB]
32	msr_s	SYS_APDBKEYLO_EL1, \tmp2
33	msr_s	SYS_APDBKEYHI_EL1, \tmp3
34.Laddr_auth_skip_\@:
35alternative_if ARM64_HAS_GENERIC_AUTH
36	ldp	\tmp2, \tmp3, [\tmp1, #PTRAUTH_USER_KEY_APGA]
37	msr_s	SYS_APGAKEYLO_EL1, \tmp2
38	msr_s	SYS_APGAKEYHI_EL1, \tmp3
39alternative_else_nop_endif
40	.endm
41
42	.macro __ptrauth_keys_install_kernel_nosync tsk, tmp1, tmp2, tmp3
43	mov	\tmp1, #THREAD_KEYS_KERNEL
44	add	\tmp1, \tsk, \tmp1
45	ldp	\tmp2, \tmp3, [\tmp1, #PTRAUTH_KERNEL_KEY_APIA]
46	msr_s	SYS_APIAKEYLO_EL1, \tmp2
47	msr_s	SYS_APIAKEYHI_EL1, \tmp3
48	.endm
49
50	.macro ptrauth_keys_install_kernel_nosync tsk, tmp1, tmp2, tmp3
51alternative_if ARM64_HAS_ADDRESS_AUTH
52	__ptrauth_keys_install_kernel_nosync \tsk, \tmp1, \tmp2, \tmp3
53alternative_else_nop_endif
54	.endm
55
56	.macro ptrauth_keys_install_kernel tsk, tmp1, tmp2, tmp3
57alternative_if ARM64_HAS_ADDRESS_AUTH
58	__ptrauth_keys_install_kernel_nosync \tsk, \tmp1, \tmp2, \tmp3
59	isb
60alternative_else_nop_endif
61	.endm
62
63	.macro __ptrauth_keys_init_cpu tsk, tmp1, tmp2, tmp3
64	mrs	\tmp1, id_aa64isar1_el1
65	ubfx	\tmp1, \tmp1, #ID_AA64ISAR1_APA_SHIFT, #8
66	cbz	\tmp1, .Lno_addr_auth\@
67	mov_q	\tmp1, (SCTLR_ELx_ENIA | SCTLR_ELx_ENIB | \
68			SCTLR_ELx_ENDA | SCTLR_ELx_ENDB)
69	mrs	\tmp2, sctlr_el1
70	orr	\tmp2, \tmp2, \tmp1
71	msr	sctlr_el1, \tmp2
72	__ptrauth_keys_install_kernel_nosync \tsk, \tmp1, \tmp2, \tmp3
73	isb
74.Lno_addr_auth\@:
75	.endm
76
77	.macro ptrauth_keys_init_cpu tsk, tmp1, tmp2, tmp3
78alternative_if_not ARM64_HAS_ADDRESS_AUTH
79	b	.Lno_addr_auth\@
80alternative_else_nop_endif
81	__ptrauth_keys_init_cpu \tsk, \tmp1, \tmp2, \tmp3
82.Lno_addr_auth\@:
83	.endm
84
85#else /* CONFIG_ARM64_PTR_AUTH */
86
87	.macro ptrauth_keys_install_user tsk, tmp1, tmp2, tmp3
88	.endm
89
90	.macro ptrauth_keys_install_kernel_nosync tsk, tmp1, tmp2, tmp3
91	.endm
92
93	.macro ptrauth_keys_install_kernel tsk, tmp1, tmp2, tmp3
94	.endm
95
96#endif /* CONFIG_ARM64_PTR_AUTH */
97
98#endif /* __ASM_ASM_POINTER_AUTH_H */
99