arm64: mte: add in-kernel MTE helpers
[platform/kernel/linux-rpi.git] / arch / arm64 / kernel / mte.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2020 ARM Ltd.
4  */
5
6 #include <linux/bitops.h>
7 #include <linux/kernel.h>
8 #include <linux/mm.h>
9 #include <linux/prctl.h>
10 #include <linux/sched.h>
11 #include <linux/sched/mm.h>
12 #include <linux/string.h>
13 #include <linux/swap.h>
14 #include <linux/swapops.h>
15 #include <linux/thread_info.h>
16 #include <linux/types.h>
17 #include <linux/uio.h>
18
19 #include <asm/barrier.h>
20 #include <asm/cpufeature.h>
21 #include <asm/mte.h>
22 #include <asm/mte-kasan.h>
23 #include <asm/ptrace.h>
24 #include <asm/sysreg.h>
25
26 static void mte_sync_page_tags(struct page *page, pte_t *ptep, bool check_swap)
27 {
28         pte_t old_pte = READ_ONCE(*ptep);
29
30         if (check_swap && is_swap_pte(old_pte)) {
31                 swp_entry_t entry = pte_to_swp_entry(old_pte);
32
33                 if (!non_swap_entry(entry) && mte_restore_tags(entry, page))
34                         return;
35         }
36
37         mte_clear_page_tags(page_address(page));
38 }
39
40 void mte_sync_tags(pte_t *ptep, pte_t pte)
41 {
42         struct page *page = pte_page(pte);
43         long i, nr_pages = compound_nr(page);
44         bool check_swap = nr_pages == 1;
45
46         /* if PG_mte_tagged is set, tags have already been initialised */
47         for (i = 0; i < nr_pages; i++, page++) {
48                 if (!test_and_set_bit(PG_mte_tagged, &page->flags))
49                         mte_sync_page_tags(page, ptep, check_swap);
50         }
51 }
52
53 int memcmp_pages(struct page *page1, struct page *page2)
54 {
55         char *addr1, *addr2;
56         int ret;
57
58         addr1 = page_address(page1);
59         addr2 = page_address(page2);
60         ret = memcmp(addr1, addr2, PAGE_SIZE);
61
62         if (!system_supports_mte() || ret)
63                 return ret;
64
65         /*
66          * If the page content is identical but at least one of the pages is
67          * tagged, return non-zero to avoid KSM merging. If only one of the
68          * pages is tagged, set_pte_at() may zero or change the tags of the
69          * other page via mte_sync_tags().
70          */
71         if (test_bit(PG_mte_tagged, &page1->flags) ||
72             test_bit(PG_mte_tagged, &page2->flags))
73                 return addr1 != addr2;
74
75         return ret;
76 }
77
78 u8 mte_get_mem_tag(void *addr)
79 {
80         if (!system_supports_mte())
81                 return 0xFF;
82
83         asm(__MTE_PREAMBLE "ldg %0, [%0]"
84             : "+r" (addr));
85
86         return mte_get_ptr_tag(addr);
87 }
88
89 u8 mte_get_random_tag(void)
90 {
91         void *addr;
92
93         if (!system_supports_mte())
94                 return 0xFF;
95
96         asm(__MTE_PREAMBLE "irg %0, %0"
97             : "+r" (addr));
98
99         return mte_get_ptr_tag(addr);
100 }
101
102 void *mte_set_mem_tag_range(void *addr, size_t size, u8 tag)
103 {
104         void *ptr = addr;
105
106         if ((!system_supports_mte()) || (size == 0))
107                 return addr;
108
109         /* Make sure that size is MTE granule aligned. */
110         WARN_ON(size & (MTE_GRANULE_SIZE - 1));
111
112         /* Make sure that the address is MTE granule aligned. */
113         WARN_ON((u64)addr & (MTE_GRANULE_SIZE - 1));
114
115         tag = 0xF0 | tag;
116         ptr = (void *)__tag_set(ptr, tag);
117
118         mte_assign_mem_tag_range(ptr, size);
119
120         return ptr;
121 }
122
123 static void update_sctlr_el1_tcf0(u64 tcf0)
124 {
125         /* ISB required for the kernel uaccess routines */
126         sysreg_clear_set(sctlr_el1, SCTLR_EL1_TCF0_MASK, tcf0);
127         isb();
128 }
129
130 static void set_sctlr_el1_tcf0(u64 tcf0)
131 {
132         /*
133          * mte_thread_switch() checks current->thread.sctlr_tcf0 as an
134          * optimisation. Disable preemption so that it does not see
135          * the variable update before the SCTLR_EL1.TCF0 one.
136          */
137         preempt_disable();
138         current->thread.sctlr_tcf0 = tcf0;
139         update_sctlr_el1_tcf0(tcf0);
140         preempt_enable();
141 }
142
143 static void update_gcr_el1_excl(u64 incl)
144 {
145         u64 excl = ~incl & SYS_GCR_EL1_EXCL_MASK;
146
147         /*
148          * Note that 'incl' is an include mask (controlled by the user via
149          * prctl()) while GCR_EL1 accepts an exclude mask.
150          * No need for ISB since this only affects EL0 currently, implicit
151          * with ERET.
152          */
153         sysreg_clear_set_s(SYS_GCR_EL1, SYS_GCR_EL1_EXCL_MASK, excl);
154 }
155
156 static void set_gcr_el1_excl(u64 incl)
157 {
158         current->thread.gcr_user_incl = incl;
159         update_gcr_el1_excl(incl);
160 }
161
162 void flush_mte_state(void)
163 {
164         if (!system_supports_mte())
165                 return;
166
167         /* clear any pending asynchronous tag fault */
168         dsb(ish);
169         write_sysreg_s(0, SYS_TFSRE0_EL1);
170         clear_thread_flag(TIF_MTE_ASYNC_FAULT);
171         /* disable tag checking */
172         set_sctlr_el1_tcf0(SCTLR_EL1_TCF0_NONE);
173         /* reset tag generation mask */
174         set_gcr_el1_excl(0);
175 }
176
177 void mte_thread_switch(struct task_struct *next)
178 {
179         if (!system_supports_mte())
180                 return;
181
182         /* avoid expensive SCTLR_EL1 accesses if no change */
183         if (current->thread.sctlr_tcf0 != next->thread.sctlr_tcf0)
184                 update_sctlr_el1_tcf0(next->thread.sctlr_tcf0);
185         update_gcr_el1_excl(next->thread.gcr_user_incl);
186 }
187
188 void mte_suspend_exit(void)
189 {
190         if (!system_supports_mte())
191                 return;
192
193         update_gcr_el1_excl(current->thread.gcr_user_incl);
194 }
195
196 long set_mte_ctrl(struct task_struct *task, unsigned long arg)
197 {
198         u64 tcf0;
199         u64 gcr_incl = (arg & PR_MTE_TAG_MASK) >> PR_MTE_TAG_SHIFT;
200
201         if (!system_supports_mte())
202                 return 0;
203
204         switch (arg & PR_MTE_TCF_MASK) {
205         case PR_MTE_TCF_NONE:
206                 tcf0 = SCTLR_EL1_TCF0_NONE;
207                 break;
208         case PR_MTE_TCF_SYNC:
209                 tcf0 = SCTLR_EL1_TCF0_SYNC;
210                 break;
211         case PR_MTE_TCF_ASYNC:
212                 tcf0 = SCTLR_EL1_TCF0_ASYNC;
213                 break;
214         default:
215                 return -EINVAL;
216         }
217
218         if (task != current) {
219                 task->thread.sctlr_tcf0 = tcf0;
220                 task->thread.gcr_user_incl = gcr_incl;
221         } else {
222                 set_sctlr_el1_tcf0(tcf0);
223                 set_gcr_el1_excl(gcr_incl);
224         }
225
226         return 0;
227 }
228
229 long get_mte_ctrl(struct task_struct *task)
230 {
231         unsigned long ret;
232
233         if (!system_supports_mte())
234                 return 0;
235
236         ret = task->thread.gcr_user_incl << PR_MTE_TAG_SHIFT;
237
238         switch (task->thread.sctlr_tcf0) {
239         case SCTLR_EL1_TCF0_NONE:
240                 ret |= PR_MTE_TCF_NONE;
241                 break;
242         case SCTLR_EL1_TCF0_SYNC:
243                 ret |= PR_MTE_TCF_SYNC;
244                 break;
245         case SCTLR_EL1_TCF0_ASYNC:
246                 ret |= PR_MTE_TCF_ASYNC;
247                 break;
248         }
249
250         return ret;
251 }
252
253 /*
254  * Access MTE tags in another process' address space as given in mm. Update
255  * the number of tags copied. Return 0 if any tags copied, error otherwise.
256  * Inspired by __access_remote_vm().
257  */
258 static int __access_remote_tags(struct mm_struct *mm, unsigned long addr,
259                                 struct iovec *kiov, unsigned int gup_flags)
260 {
261         struct vm_area_struct *vma;
262         void __user *buf = kiov->iov_base;
263         size_t len = kiov->iov_len;
264         int ret;
265         int write = gup_flags & FOLL_WRITE;
266
267         if (!access_ok(buf, len))
268                 return -EFAULT;
269
270         if (mmap_read_lock_killable(mm))
271                 return -EIO;
272
273         while (len) {
274                 unsigned long tags, offset;
275                 void *maddr;
276                 struct page *page = NULL;
277
278                 ret = get_user_pages_remote(mm, addr, 1, gup_flags, &page,
279                                             &vma, NULL);
280                 if (ret <= 0)
281                         break;
282
283                 /*
284                  * Only copy tags if the page has been mapped as PROT_MTE
285                  * (PG_mte_tagged set). Otherwise the tags are not valid and
286                  * not accessible to user. Moreover, an mprotect(PROT_MTE)
287                  * would cause the existing tags to be cleared if the page
288                  * was never mapped with PROT_MTE.
289                  */
290                 if (!test_bit(PG_mte_tagged, &page->flags)) {
291                         ret = -EOPNOTSUPP;
292                         put_page(page);
293                         break;
294                 }
295
296                 /* limit access to the end of the page */
297                 offset = offset_in_page(addr);
298                 tags = min(len, (PAGE_SIZE - offset) / MTE_GRANULE_SIZE);
299
300                 maddr = page_address(page);
301                 if (write) {
302                         tags = mte_copy_tags_from_user(maddr + offset, buf, tags);
303                         set_page_dirty_lock(page);
304                 } else {
305                         tags = mte_copy_tags_to_user(buf, maddr + offset, tags);
306                 }
307                 put_page(page);
308
309                 /* error accessing the tracer's buffer */
310                 if (!tags)
311                         break;
312
313                 len -= tags;
314                 buf += tags;
315                 addr += tags * MTE_GRANULE_SIZE;
316         }
317         mmap_read_unlock(mm);
318
319         /* return an error if no tags copied */
320         kiov->iov_len = buf - kiov->iov_base;
321         if (!kiov->iov_len) {
322                 /* check for error accessing the tracee's address space */
323                 if (ret <= 0)
324                         return -EIO;
325                 else
326                         return -EFAULT;
327         }
328
329         return 0;
330 }
331
332 /*
333  * Copy MTE tags in another process' address space at 'addr' to/from tracer's
334  * iovec buffer. Return 0 on success. Inspired by ptrace_access_vm().
335  */
336 static int access_remote_tags(struct task_struct *tsk, unsigned long addr,
337                               struct iovec *kiov, unsigned int gup_flags)
338 {
339         struct mm_struct *mm;
340         int ret;
341
342         mm = get_task_mm(tsk);
343         if (!mm)
344                 return -EPERM;
345
346         if (!tsk->ptrace || (current != tsk->parent) ||
347             ((get_dumpable(mm) != SUID_DUMP_USER) &&
348              !ptracer_capable(tsk, mm->user_ns))) {
349                 mmput(mm);
350                 return -EPERM;
351         }
352
353         ret = __access_remote_tags(mm, addr, kiov, gup_flags);
354         mmput(mm);
355
356         return ret;
357 }
358
359 int mte_ptrace_copy_tags(struct task_struct *child, long request,
360                          unsigned long addr, unsigned long data)
361 {
362         int ret;
363         struct iovec kiov;
364         struct iovec __user *uiov = (void __user *)data;
365         unsigned int gup_flags = FOLL_FORCE;
366
367         if (!system_supports_mte())
368                 return -EIO;
369
370         if (get_user(kiov.iov_base, &uiov->iov_base) ||
371             get_user(kiov.iov_len, &uiov->iov_len))
372                 return -EFAULT;
373
374         if (request == PTRACE_POKEMTETAGS)
375                 gup_flags |= FOLL_WRITE;
376
377         /* align addr to the MTE tag granule */
378         addr &= MTE_GRANULE_MASK;
379
380         ret = access_remote_tags(child, addr, &kiov, gup_flags);
381         if (!ret)
382                 ret = put_user(kiov.iov_len, &uiov->iov_len);
383
384         return ret;
385 }