1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (C) 2020 ARM Ltd.
5 #ifndef __ASM_MTE_KASAN_H
6 #define __ASM_MTE_KASAN_H
8 #include <asm/compiler.h>
9 #include <asm/cputype.h>
10 #include <asm/mte-def.h>
14 #include <linux/types.h>
16 #ifdef CONFIG_KASAN_HW_TAGS
18 /* Whether the MTE asynchronous mode is enabled. */
19 DECLARE_STATIC_KEY_FALSE(mte_async_or_asymm_mode
);
21 static inline bool system_uses_mte_async_or_asymm_mode(void)
23 return static_branch_unlikely(&mte_async_or_asymm_mode
);
26 #else /* CONFIG_KASAN_HW_TAGS */
28 static inline bool system_uses_mte_async_or_asymm_mode(void)
33 #endif /* CONFIG_KASAN_HW_TAGS */
35 #ifdef CONFIG_ARM64_MTE
38 * The Tag Check Flag (TCF) mode for MTE is per EL, hence TCF0
39 * affects EL0 and TCF affects EL1 irrespective of which TTBR is
41 * The kernel accesses TTBR0 usually with LDTR/STTR instructions
42 * when UAO is available, so these would act as EL0 accesses using
44 * However futex.h code uses exclusives which would be executed as
45 * EL1, this can potentially cause a tag check fault even if the
48 * To address the problem we set the PSTATE.TCO bit in uaccess_enable()
49 * and reset it in uaccess_disable().
51 * The Tag check override (TCO) bit disables temporarily the tag checking
52 * preventing the issue.
54 static inline void mte_disable_tco(void)
56 asm volatile(ALTERNATIVE("nop", SET_PSTATE_TCO(0),
57 ARM64_MTE
, CONFIG_KASAN_HW_TAGS
));
60 static inline void mte_enable_tco(void)
62 asm volatile(ALTERNATIVE("nop", SET_PSTATE_TCO(1),
63 ARM64_MTE
, CONFIG_KASAN_HW_TAGS
));
67 * These functions disable tag checking only if in MTE async mode
68 * since the sync mode generates exceptions synchronously and the
69 * nofault or load_unaligned_zeropad can handle them.
71 static inline void __mte_disable_tco_async(void)
73 if (system_uses_mte_async_or_asymm_mode())
77 static inline void __mte_enable_tco_async(void)
79 if (system_uses_mte_async_or_asymm_mode())
84 * These functions are meant to be only used from KASAN runtime through
85 * the arch_*() interface defined in asm/memory.h.
86 * These functions don't include system_supports_mte() checks,
87 * as KASAN only calls them when MTE is supported and enabled.
90 static inline u8
mte_get_ptr_tag(void *ptr
)
92 /* Note: The format of KASAN tags is 0xF<x> */
93 u8 tag
= 0xF0 | (u8
)(((u64
)(ptr
)) >> MTE_TAG_SHIFT
);
98 /* Get allocation tag for the address. */
99 static inline u8
mte_get_mem_tag(void *addr
)
101 asm(__MTE_PREAMBLE
"ldg %0, [%0]"
104 return mte_get_ptr_tag(addr
);
107 /* Generate a random tag. */
108 static inline u8
mte_get_random_tag(void)
112 asm(__MTE_PREAMBLE
"irg %0, %0"
115 return mte_get_ptr_tag(addr
);
118 static inline u64
__stg_post(u64 p
)
120 asm volatile(__MTE_PREAMBLE
"stg %0, [%0], #16"
127 static inline u64
__stzg_post(u64 p
)
129 asm volatile(__MTE_PREAMBLE
"stzg %0, [%0], #16"
136 static inline void __dc_gva(u64 p
)
138 asm volatile(__MTE_PREAMBLE
"dc gva, %0" : : "r"(p
) : "memory");
141 static inline void __dc_gzva(u64 p
)
143 asm volatile(__MTE_PREAMBLE
"dc gzva, %0" : : "r"(p
) : "memory");
147 * Assign allocation tags for a region of memory based on the pointer tag.
148 * Note: The address must be non-NULL and MTE_GRANULE_SIZE aligned and
149 * size must be MTE_GRANULE_SIZE aligned.
151 static inline void mte_set_mem_tag_range(void *addr
, size_t size
, u8 tag
,
154 u64 curr
, mask
, dczid
, dczid_bs
, dczid_dzp
, end1
, end2
, end3
;
156 /* Read DC G(Z)VA block size from the system register. */
157 dczid
= read_cpuid(DCZID_EL0
);
158 dczid_bs
= 4ul << (dczid
& 0xf);
159 dczid_dzp
= (dczid
>> 4) & 1;
161 curr
= (u64
)__tag_set(addr
, tag
);
163 /* STG/STZG up to the end of the first block. */
166 /* DC GVA / GZVA in [end1, end2) */
170 * The following code uses STG on the first DC GVA block even if the
171 * start address is aligned - it appears to be faster than an alignment
172 * check + conditional branch. Also, if the range size is at least 2 DC
173 * GVA blocks, the first two loops can use post-condition to save one
176 #define SET_MEMTAG_RANGE(stg_post, dc_gva) \
178 if (!dczid_dzp && size >= 2 * dczid_bs) {\
180 curr = stg_post(curr); \
181 } while (curr < end1); \
186 } while (curr < end2); \
189 while (curr < end3) \
190 curr = stg_post(curr); \
194 SET_MEMTAG_RANGE(__stzg_post
, __dc_gzva
);
196 SET_MEMTAG_RANGE(__stg_post
, __dc_gva
);
197 #undef SET_MEMTAG_RANGE
200 void mte_enable_kernel_sync(void);
201 void mte_enable_kernel_async(void);
202 void mte_enable_kernel_asymm(void);
204 #else /* CONFIG_ARM64_MTE */
206 static inline void mte_disable_tco(void)
210 static inline void mte_enable_tco(void)
214 static inline void __mte_disable_tco_async(void)
218 static inline void __mte_enable_tco_async(void)
222 static inline u8
mte_get_ptr_tag(void *ptr
)
227 static inline u8
mte_get_mem_tag(void *addr
)
232 static inline u8
mte_get_random_tag(void)
237 static inline void mte_set_mem_tag_range(void *addr
, size_t size
,
242 static inline void mte_enable_kernel_sync(void)
246 static inline void mte_enable_kernel_async(void)
250 static inline void mte_enable_kernel_asymm(void)
254 #endif /* CONFIG_ARM64_MTE */
256 #endif /* __ASSEMBLY__ */
258 #endif /* __ASM_MTE_KASAN_H */