1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * Copyright (C) 2020 ARM Ltd.
4 */
5 #ifndef __ASM_MTE_H
6 #define __ASM_MTE_H
7
8 #include <asm/compiler.h>
9 #include <asm/mte-def.h>
10
11 #ifndef __ASSEMBLY__
12
13 #include <linux/bitfield.h>
14 #include <linux/page-flags.h>
15 #include <linux/types.h>
16
17 #include <asm/pgtable-types.h>
18
19 void mte_clear_page_tags(void *addr);
20 unsigned long mte_copy_tags_from_user(void *to, const void __user *from,
21 unsigned long n);
22 unsigned long mte_copy_tags_to_user(void __user *to, void *from,
23 unsigned long n);
24 int mte_save_tags(struct page *page);
25 void mte_save_page_tags(const void *page_addr, void *tag_storage);
26 bool mte_restore_tags(swp_entry_t entry, struct page *page);
27 void mte_restore_page_tags(void *page_addr, const void *tag_storage);
28 void mte_invalidate_tags(int type, pgoff_t offset);
29 void mte_invalidate_tags_area(int type);
30 void *mte_allocate_tag_storage(void);
31 void mte_free_tag_storage(char *storage);
32
33 #ifdef CONFIG_ARM64_MTE
34
35 /* track which pages have valid allocation tags */
36 #define PG_mte_tagged PG_arch_2
37
38 void mte_zero_clear_page_tags(void *addr);
39 void mte_sync_tags(pte_t old_pte, pte_t pte);
40 void mte_copy_page_tags(void *kto, const void *kfrom);
41 void mte_thread_init_user(void);
42 void mte_thread_switch(struct task_struct *next);
43 void mte_cpu_setup(void);
44 void mte_suspend_enter(void);
45 void mte_suspend_exit(void);
46 long set_mte_ctrl(struct task_struct *task, unsigned long arg);
47 long get_mte_ctrl(struct task_struct *task);
48 int mte_ptrace_copy_tags(struct task_struct *child, long request,
49 unsigned long addr, unsigned long data);
50
51 #else /* CONFIG_ARM64_MTE */
52
53 /* unused if !CONFIG_ARM64_MTE, silence the compiler */
54 #define PG_mte_tagged 0
55
mte_zero_clear_page_tags(void * addr)56 static inline void mte_zero_clear_page_tags(void *addr)
57 {
58 }
mte_sync_tags(pte_t old_pte,pte_t pte)59 static inline void mte_sync_tags(pte_t old_pte, pte_t pte)
60 {
61 }
mte_copy_page_tags(void * kto,const void * kfrom)62 static inline void mte_copy_page_tags(void *kto, const void *kfrom)
63 {
64 }
mte_thread_init_user(void)65 static inline void mte_thread_init_user(void)
66 {
67 }
mte_thread_switch(struct task_struct * next)68 static inline void mte_thread_switch(struct task_struct *next)
69 {
70 }
mte_suspend_enter(void)71 static inline void mte_suspend_enter(void)
72 {
73 }
mte_suspend_exit(void)74 static inline void mte_suspend_exit(void)
75 {
76 }
set_mte_ctrl(struct task_struct * task,unsigned long arg)77 static inline long set_mte_ctrl(struct task_struct *task, unsigned long arg)
78 {
79 return 0;
80 }
get_mte_ctrl(struct task_struct * task)81 static inline long get_mte_ctrl(struct task_struct *task)
82 {
83 return 0;
84 }
mte_ptrace_copy_tags(struct task_struct * child,long request,unsigned long addr,unsigned long data)85 static inline int mte_ptrace_copy_tags(struct task_struct *child,
86 long request, unsigned long addr,
87 unsigned long data)
88 {
89 return -EIO;
90 }
91
92 #endif /* CONFIG_ARM64_MTE */
93
94 #ifdef CONFIG_KASAN_HW_TAGS
95 /* Whether the MTE asynchronous mode is enabled. */
96 DECLARE_STATIC_KEY_FALSE(mte_async_or_asymm_mode);
97
system_uses_mte_async_or_asymm_mode(void)98 static inline bool system_uses_mte_async_or_asymm_mode(void)
99 {
100 return static_branch_unlikely(&mte_async_or_asymm_mode);
101 }
102
103 void mte_check_tfsr_el1(void);
104
mte_check_tfsr_entry(void)105 static inline void mte_check_tfsr_entry(void)
106 {
107 if (!system_supports_mte())
108 return;
109
110 mte_check_tfsr_el1();
111 }
112
mte_check_tfsr_exit(void)113 static inline void mte_check_tfsr_exit(void)
114 {
115 if (!system_supports_mte())
116 return;
117
118 /*
119 * The asynchronous faults are sync'ed automatically with
120 * TFSR_EL1 on kernel entry but for exit an explicit dsb()
121 * is required.
122 */
123 dsb(nsh);
124 isb();
125
126 mte_check_tfsr_el1();
127 }
128 #else
system_uses_mte_async_or_asymm_mode(void)129 static inline bool system_uses_mte_async_or_asymm_mode(void)
130 {
131 return false;
132 }
mte_check_tfsr_el1(void)133 static inline void mte_check_tfsr_el1(void)
134 {
135 }
mte_check_tfsr_entry(void)136 static inline void mte_check_tfsr_entry(void)
137 {
138 }
mte_check_tfsr_exit(void)139 static inline void mte_check_tfsr_exit(void)
140 {
141 }
142 #endif /* CONFIG_KASAN_HW_TAGS */
143
144 #endif /* __ASSEMBLY__ */
145 #endif /* __ASM_MTE_H */
146