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/mte-def.h>
12 #include <linux/types.h>
14 #ifdef CONFIG_ARM64_MTE
17 * These functions are meant to be only used from KASAN runtime through
18 * the arch_*() interface defined in asm/memory.h.
19 * These functions don't include system_supports_mte() checks,
20 * as KASAN only calls them when MTE is supported and enabled.
23 static inline u8 mte_get_ptr_tag(void *ptr)
25 /* Note: The format of KASAN tags is 0xF<x> */
26 u8 tag = 0xF0 | (u8)(((u64)(ptr)) >> MTE_TAG_SHIFT);
31 /* Get allocation tag for the address. */
32 static inline u8 mte_get_mem_tag(void *addr)
34 asm(__MTE_PREAMBLE "ldg %0, [%0]"
37 return mte_get_ptr_tag(addr);
40 /* Generate a random tag. */
41 static inline u8 mte_get_random_tag(void)
45 asm(__MTE_PREAMBLE "irg %0, %0"
48 return mte_get_ptr_tag(addr);
52 * Assign allocation tags for a region of memory based on the pointer tag.
53 * Note: The address must be non-NULL and MTE_GRANULE_SIZE aligned and
54 * size must be non-zero and MTE_GRANULE_SIZE aligned.
56 static inline void mte_set_mem_tag_range(void *addr, size_t size, u8 tag)
63 curr = (u64)__tag_set(addr, tag);
68 * 'asm volatile' is required to prevent the compiler to move
69 * the statement outside of the loop.
71 asm volatile(__MTE_PREAMBLE "stg %0, [%0]"
76 curr += MTE_GRANULE_SIZE;
77 } while (curr != end);
80 void mte_enable_kernel(void);
81 void mte_init_tags(u64 max_tag);
83 void mte_set_report_once(bool state);
84 bool mte_report_once(void);
86 #else /* CONFIG_ARM64_MTE */
88 static inline u8 mte_get_ptr_tag(void *ptr)
93 static inline u8 mte_get_mem_tag(void *addr)
98 static inline u8 mte_get_random_tag(void)
103 static inline void mte_set_mem_tag_range(void *addr, size_t size, u8 tag)
107 static inline void mte_enable_kernel(void)
111 static inline void mte_init_tags(u64 max_tag)
115 static inline void mte_set_report_once(bool state)
119 static inline bool mte_report_once(void)
124 #endif /* CONFIG_ARM64_MTE */
126 #endif /* __ASSEMBLY__ */
128 #endif /* __ASM_MTE_KASAN_H */