1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __ASM_POINTER_AUTH_H
3 #define __ASM_POINTER_AUTH_H
5 #include <linux/bitops.h>
7 #include <asm/cpufeature.h>
8 #include <asm/memory.h>
9 #include <asm/sysreg.h>
11 #ifdef CONFIG_ARM64_PTR_AUTH
13 * Each key is a 128-bit quantity which is split across a pair of 64-bit
14 * registers (Lo and Hi).
21 * We give each process its own keys, which are shared by all threads. The keys
22 * are inherited upon fork(), and reinitialised upon exec*().
25 struct ptrauth_key apia;
26 struct ptrauth_key apib;
27 struct ptrauth_key apda;
28 struct ptrauth_key apdb;
29 struct ptrauth_key apga;
33 * Only include random.h once ptrauth_keys_* structures are defined
34 * to avoid yet another circular include hell (random.h * ends up
35 * including asm/smp.h, which requires ptrauth_keys_kernel).
37 #include <linux/random.h>
39 static inline void ptrauth_keys_init(struct ptrauth_keys *keys)
41 if (system_supports_address_auth()) {
42 get_random_bytes(&keys->apia, sizeof(keys->apia));
43 get_random_bytes(&keys->apib, sizeof(keys->apib));
44 get_random_bytes(&keys->apda, sizeof(keys->apda));
45 get_random_bytes(&keys->apdb, sizeof(keys->apdb));
48 if (system_supports_generic_auth())
49 get_random_bytes(&keys->apga, sizeof(keys->apga));
52 #define __ptrauth_key_install(k, v) \
54 struct ptrauth_key __pki_v = (v); \
55 write_sysreg_s(__pki_v.lo, SYS_ ## k ## KEYLO_EL1); \
56 write_sysreg_s(__pki_v.hi, SYS_ ## k ## KEYHI_EL1); \
59 static inline void ptrauth_keys_switch(struct ptrauth_keys *keys)
61 if (system_supports_address_auth()) {
62 __ptrauth_key_install(APIA, keys->apia);
63 __ptrauth_key_install(APIB, keys->apib);
64 __ptrauth_key_install(APDA, keys->apda);
65 __ptrauth_key_install(APDB, keys->apdb);
68 if (system_supports_generic_auth())
69 __ptrauth_key_install(APGA, keys->apga);
72 extern int ptrauth_prctl_reset_keys(struct task_struct *tsk, unsigned long arg);
75 * The EL0 pointer bits used by a pointer authentication code.
76 * This is dependent on TBI0 being enabled, or bits 63:56 would also apply.
78 #define ptrauth_user_pac_mask() GENMASK(54, vabits_actual)
80 /* Only valid for EL0 TTBR0 instruction pointers */
81 static inline unsigned long ptrauth_strip_insn_pac(unsigned long ptr)
83 return ptr & ~ptrauth_user_pac_mask();
86 #define ptrauth_thread_init_user(tsk) \
88 struct task_struct *__ptiu_tsk = (tsk); \
89 ptrauth_keys_init(&__ptiu_tsk->thread.keys_user); \
90 ptrauth_keys_switch(&__ptiu_tsk->thread.keys_user); \
93 #define ptrauth_thread_switch(tsk) \
94 ptrauth_keys_switch(&(tsk)->thread.keys_user)
96 #else /* CONFIG_ARM64_PTR_AUTH */
97 #define ptrauth_prctl_reset_keys(tsk, arg) (-EINVAL)
98 #define ptrauth_strip_insn_pac(lr) (lr)
99 #define ptrauth_thread_init_user(tsk)
100 #define ptrauth_thread_switch(tsk)
101 #endif /* CONFIG_ARM64_PTR_AUTH */
103 #endif /* __ASM_POINTER_AUTH_H */