1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (C) 2020 ARM Ltd.
8 #include <asm/compiler.h>
9 #include <asm/mte-def.h>
13 #include <linux/bitfield.h>
14 #include <linux/kasan-enabled.h>
15 #include <linux/page-flags.h>
16 #include <linux/sched.h>
17 #include <linux/types.h>
19 #include <asm/pgtable-types.h>
21 void mte_clear_page_tags(void *addr
);
22 unsigned long mte_copy_tags_from_user(void *to
, const void __user
*from
,
24 unsigned long mte_copy_tags_to_user(void __user
*to
, void *from
,
26 int mte_save_tags(struct page
*page
);
27 void mte_save_page_tags(const void *page_addr
, void *tag_storage
);
28 void mte_restore_tags(swp_entry_t entry
, struct page
*page
);
29 void mte_restore_page_tags(void *page_addr
, const void *tag_storage
);
30 void mte_invalidate_tags(int type
, pgoff_t offset
);
31 void mte_invalidate_tags_area(int type
);
32 void *mte_allocate_tag_storage(void);
33 void mte_free_tag_storage(char *storage
);
35 #ifdef CONFIG_ARM64_MTE
37 /* track which pages have valid allocation tags */
38 #define PG_mte_tagged PG_arch_2
39 /* simple lock to avoid multiple threads tagging the same page */
40 #define PG_mte_lock PG_arch_3
42 static inline void set_page_mte_tagged(struct page
*page
)
45 * Ensure that the tags written prior to this function are visible
46 * before the page flags update.
49 set_bit(PG_mte_tagged
, &page
->flags
);
52 static inline bool page_mte_tagged(struct page
*page
)
54 bool ret
= test_bit(PG_mte_tagged
, &page
->flags
);
57 * If the page is tagged, ensure ordering with a likely subsequent
66 * Lock the page for tagging and return 'true' if the page can be tagged,
67 * 'false' if already tagged. PG_mte_tagged is never cleared and therefore the
68 * locking only happens once for page initialisation.
70 * The page MTE lock state:
72 * Locked: PG_mte_lock && !PG_mte_tagged
73 * Unlocked: !PG_mte_lock || PG_mte_tagged
75 * Acquire semantics only if the page is tagged (returning 'false').
77 static inline bool try_page_mte_tagging(struct page
*page
)
79 if (!test_and_set_bit(PG_mte_lock
, &page
->flags
))
83 * The tags are either being initialised or may have been initialised
84 * already. Check if the PG_mte_tagged flag has been set or wait
87 smp_cond_load_acquire(&page
->flags
, VAL
& (1UL << PG_mte_tagged
));
92 void mte_zero_clear_page_tags(void *addr
);
93 void mte_sync_tags(pte_t pte
, unsigned int nr_pages
);
94 void mte_copy_page_tags(void *kto
, const void *kfrom
);
95 void mte_thread_init_user(void);
96 void mte_thread_switch(struct task_struct
*next
);
97 void mte_cpu_setup(void);
98 void mte_suspend_enter(void);
99 void mte_suspend_exit(void);
100 long set_mte_ctrl(struct task_struct
*task
, unsigned long arg
);
101 long get_mte_ctrl(struct task_struct
*task
);
102 int mte_ptrace_copy_tags(struct task_struct
*child
, long request
,
103 unsigned long addr
, unsigned long data
);
104 size_t mte_probe_user_range(const char __user
*uaddr
, size_t size
);
106 #else /* CONFIG_ARM64_MTE */
108 /* unused if !CONFIG_ARM64_MTE, silence the compiler */
109 #define PG_mte_tagged 0
111 static inline void set_page_mte_tagged(struct page
*page
)
114 static inline bool page_mte_tagged(struct page
*page
)
118 static inline bool try_page_mte_tagging(struct page
*page
)
122 static inline void mte_zero_clear_page_tags(void *addr
)
125 static inline void mte_sync_tags(pte_t pte
, unsigned int nr_pages
)
128 static inline void mte_copy_page_tags(void *kto
, const void *kfrom
)
131 static inline void mte_thread_init_user(void)
134 static inline void mte_thread_switch(struct task_struct
*next
)
137 static inline void mte_suspend_enter(void)
140 static inline void mte_suspend_exit(void)
143 static inline long set_mte_ctrl(struct task_struct
*task
, unsigned long arg
)
147 static inline long get_mte_ctrl(struct task_struct
*task
)
151 static inline int mte_ptrace_copy_tags(struct task_struct
*child
,
152 long request
, unsigned long addr
,
158 #endif /* CONFIG_ARM64_MTE */
160 static inline void mte_disable_tco_entry(struct task_struct
*task
)
162 if (!system_supports_mte())
166 * Re-enable tag checking (TCO set on exception entry). This is only
167 * necessary if MTE is enabled in either the kernel or the userspace
168 * task in synchronous or asymmetric mode (SCTLR_EL1.TCF0 bit 0 is set
169 * for both). With MTE disabled in the kernel and disabled or
170 * asynchronous in userspace, tag check faults (including in uaccesses)
171 * are not reported, therefore there is no need to re-enable checking.
172 * This is beneficial on microarchitectures where re-enabling TCO is
175 if (kasan_hw_tags_enabled() ||
176 (task
->thread
.sctlr_user
& (1UL << SCTLR_EL1_TCF0_SHIFT
)))
177 asm volatile(SET_PSTATE_TCO(0));
180 #ifdef CONFIG_KASAN_HW_TAGS
181 void mte_check_tfsr_el1(void);
183 static inline void mte_check_tfsr_entry(void)
185 if (!kasan_hw_tags_enabled())
188 mte_check_tfsr_el1();
191 static inline void mte_check_tfsr_exit(void)
193 if (!kasan_hw_tags_enabled())
197 * The asynchronous faults are sync'ed automatically with
198 * TFSR_EL1 on kernel entry but for exit an explicit dsb()
204 mte_check_tfsr_el1();
207 static inline void mte_check_tfsr_el1(void)
210 static inline void mte_check_tfsr_entry(void)
213 static inline void mte_check_tfsr_exit(void)
216 #endif /* CONFIG_KASAN_HW_TAGS */
218 #endif /* __ASSEMBLY__ */
219 #endif /* __ASM_MTE_H */