1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __ASM_POINTER_AUTH_H
3 #define __ASM_POINTER_AUTH_H
5 #include <linux/bitops.h>
6 #include <linux/random.h>
8 #include <asm/cpufeature.h>
9 #include <asm/memory.h>
10 #include <asm/sysreg.h>
12 #ifdef CONFIG_ARM64_PTR_AUTH
14 * Each key is a 128-bit quantity which is split across a pair of 64-bit
15 * registers (Lo and Hi).
22 * We give each process its own keys, which are shared by all threads. The keys
23 * are inherited upon fork(), and reinitialised upon exec*().
26 struct ptrauth_key apia
;
27 struct ptrauth_key apib
;
28 struct ptrauth_key apda
;
29 struct ptrauth_key apdb
;
30 struct ptrauth_key apga
;
33 static inline void ptrauth_keys_init(struct ptrauth_keys
*keys
)
35 if (system_supports_address_auth()) {
36 get_random_bytes(&keys
->apia
, sizeof(keys
->apia
));
37 get_random_bytes(&keys
->apib
, sizeof(keys
->apib
));
38 get_random_bytes(&keys
->apda
, sizeof(keys
->apda
));
39 get_random_bytes(&keys
->apdb
, sizeof(keys
->apdb
));
42 if (system_supports_generic_auth())
43 get_random_bytes(&keys
->apga
, sizeof(keys
->apga
));
46 #define __ptrauth_key_install(k, v) \
48 struct ptrauth_key __pki_v = (v); \
49 write_sysreg_s(__pki_v.lo, SYS_ ## k ## KEYLO_EL1); \
50 write_sysreg_s(__pki_v.hi, SYS_ ## k ## KEYHI_EL1); \
53 static inline void ptrauth_keys_switch(struct ptrauth_keys
*keys
)
55 if (system_supports_address_auth()) {
56 __ptrauth_key_install(APIA
, keys
->apia
);
57 __ptrauth_key_install(APIB
, keys
->apib
);
58 __ptrauth_key_install(APDA
, keys
->apda
);
59 __ptrauth_key_install(APDB
, keys
->apdb
);
62 if (system_supports_generic_auth())
63 __ptrauth_key_install(APGA
, keys
->apga
);
66 extern int ptrauth_prctl_reset_keys(struct task_struct
*tsk
, unsigned long arg
);
69 * The EL0 pointer bits used by a pointer authentication code.
70 * This is dependent on TBI0 being enabled, or bits 63:56 would also apply.
72 #define ptrauth_user_pac_mask() GENMASK(54, vabits_actual)
74 /* Only valid for EL0 TTBR0 instruction pointers */
75 static inline unsigned long ptrauth_strip_insn_pac(unsigned long ptr
)
77 return ptr
& ~ptrauth_user_pac_mask();
80 #define ptrauth_thread_init_user(tsk) \
82 struct task_struct *__ptiu_tsk = (tsk); \
83 ptrauth_keys_init(&__ptiu_tsk->thread.keys_user); \
84 ptrauth_keys_switch(&__ptiu_tsk->thread.keys_user); \
87 #define ptrauth_thread_switch(tsk) \
88 ptrauth_keys_switch(&(tsk)->thread.keys_user)
90 #else /* CONFIG_ARM64_PTR_AUTH */
91 #define ptrauth_prctl_reset_keys(tsk, arg) (-EINVAL)
92 #define ptrauth_strip_insn_pac(lr) (lr)
93 #define ptrauth_thread_init_user(tsk)
94 #define ptrauth_thread_switch(tsk)
95 #endif /* CONFIG_ARM64_PTR_AUTH */
97 #endif /* __ASM_POINTER_AUTH_H */