1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2020 ARM Ltd.
6 #include <linux/bitops.h>
8 #include <linux/kernel.h>
10 #include <linux/prctl.h>
11 #include <linux/sched.h>
12 #include <linux/sched/mm.h>
13 #include <linux/string.h>
14 #include <linux/swap.h>
15 #include <linux/swapops.h>
16 #include <linux/thread_info.h>
17 #include <linux/types.h>
18 #include <linux/uaccess.h>
19 #include <linux/uio.h>
21 #include <asm/barrier.h>
22 #include <asm/cpufeature.h>
24 #include <asm/ptrace.h>
25 #include <asm/sysreg.h>
27 static DEFINE_PER_CPU_READ_MOSTLY(u64, mte_tcf_preferred);
29 #ifdef CONFIG_KASAN_HW_TAGS
31 * The asynchronous and asymmetric MTE modes have the same behavior for
32 * store operations. This flag is set when either of these modes is enabled.
34 DEFINE_STATIC_KEY_FALSE(mte_async_or_asymm_mode);
35 EXPORT_SYMBOL_GPL(mte_async_or_asymm_mode);
38 void mte_sync_tags(pte_t pte, unsigned int nr_pages)
40 struct page *page = pte_page(pte);
43 /* if PG_mte_tagged is set, tags have already been initialised */
44 for (i = 0; i < nr_pages; i++, page++) {
45 if (try_page_mte_tagging(page)) {
46 mte_clear_page_tags(page_address(page));
47 set_page_mte_tagged(page);
51 /* ensure the tags are visible before the PTE is set */
55 int memcmp_pages(struct page *page1, struct page *page2)
60 addr1 = page_address(page1);
61 addr2 = page_address(page2);
62 ret = memcmp(addr1, addr2, PAGE_SIZE);
64 if (!system_supports_mte() || ret)
68 * If the page content is identical but at least one of the pages is
69 * tagged, return non-zero to avoid KSM merging. If only one of the
70 * pages is tagged, set_pte_at() may zero or change the tags of the
71 * other page via mte_sync_tags().
73 if (page_mte_tagged(page1) || page_mte_tagged(page2))
74 return addr1 != addr2;
79 static inline void __mte_enable_kernel(const char *mode, unsigned long tcf)
81 /* Enable MTE Sync Mode for EL1. */
82 sysreg_clear_set(sctlr_el1, SCTLR_EL1_TCF_MASK,
83 SYS_FIELD_PREP(SCTLR_EL1, TCF, tcf));
86 pr_info_once("MTE: enabled in %s mode at EL1\n", mode);
89 #ifdef CONFIG_KASAN_HW_TAGS
90 void mte_enable_kernel_sync(void)
93 * Make sure we enter this function when no PE has set
94 * async mode previously.
96 WARN_ONCE(system_uses_mte_async_or_asymm_mode(),
97 "MTE async mode enabled system wide!");
99 __mte_enable_kernel("synchronous", SCTLR_EL1_TCF_SYNC);
102 void mte_enable_kernel_async(void)
104 __mte_enable_kernel("asynchronous", SCTLR_EL1_TCF_ASYNC);
107 * MTE async mode is set system wide by the first PE that
108 * executes this function.
110 * Note: If in future KASAN acquires a runtime switching
111 * mode in between sync and async, this strategy needs
114 if (!system_uses_mte_async_or_asymm_mode())
115 static_branch_enable(&mte_async_or_asymm_mode);
118 void mte_enable_kernel_asymm(void)
120 if (cpus_have_cap(ARM64_MTE_ASYMM)) {
121 __mte_enable_kernel("asymmetric", SCTLR_EL1_TCF_ASYMM);
124 * MTE asymm mode behaves as async mode for store
125 * operations. The mode is set system wide by the
126 * first PE that executes this function.
128 * Note: If in future KASAN acquires a runtime switching
129 * mode in between sync and async, this strategy needs
132 if (!system_uses_mte_async_or_asymm_mode())
133 static_branch_enable(&mte_async_or_asymm_mode);
136 * If the CPU does not support MTE asymmetric mode the
137 * kernel falls back on synchronous mode which is the
138 * default for kasan=on.
140 mte_enable_kernel_sync();
145 #ifdef CONFIG_KASAN_HW_TAGS
146 void mte_check_tfsr_el1(void)
148 u64 tfsr_el1 = read_sysreg_s(SYS_TFSR_EL1);
150 if (unlikely(tfsr_el1 & SYS_TFSR_EL1_TF1)) {
152 * Note: isb() is not required after this direct write
153 * because there is no indirect read subsequent to it
154 * (per ARM DDI 0487F.c table D13-1).
156 write_sysreg_s(0, SYS_TFSR_EL1);
158 kasan_report_async();
164 * This is where we actually resolve the system and process MTE mode
165 * configuration into an actual value in SCTLR_EL1 that affects
168 static void mte_update_sctlr_user(struct task_struct *task)
171 * This must be called with preemption disabled and can only be called
172 * on the current or next task since the CPU must match where the thread
173 * is going to run. The caller is responsible for calling
174 * update_sctlr_el1() later in the same preemption disabled block.
176 unsigned long sctlr = task->thread.sctlr_user;
177 unsigned long mte_ctrl = task->thread.mte_ctrl;
178 unsigned long pref, resolved_mte_tcf;
180 pref = __this_cpu_read(mte_tcf_preferred);
182 * If there is no overlap between the system preferred and
183 * program requested values go with what was requested.
185 resolved_mte_tcf = (mte_ctrl & pref) ? pref : mte_ctrl;
186 sctlr &= ~SCTLR_EL1_TCF0_MASK;
188 * Pick an actual setting. The order in which we check for
189 * set bits and map into register values determines our
192 if (resolved_mte_tcf & MTE_CTRL_TCF_ASYMM)
193 sctlr |= SYS_FIELD_PREP_ENUM(SCTLR_EL1, TCF0, ASYMM);
194 else if (resolved_mte_tcf & MTE_CTRL_TCF_ASYNC)
195 sctlr |= SYS_FIELD_PREP_ENUM(SCTLR_EL1, TCF0, ASYNC);
196 else if (resolved_mte_tcf & MTE_CTRL_TCF_SYNC)
197 sctlr |= SYS_FIELD_PREP_ENUM(SCTLR_EL1, TCF0, SYNC);
198 task->thread.sctlr_user = sctlr;
201 static void mte_update_gcr_excl(struct task_struct *task)
204 * SYS_GCR_EL1 will be set to current->thread.mte_ctrl value by
205 * mte_set_user_gcr() in kernel_exit, but only if KASAN is enabled.
207 if (kasan_hw_tags_enabled())
211 ((task->thread.mte_ctrl >> MTE_CTRL_GCR_USER_EXCL_SHIFT) &
212 SYS_GCR_EL1_EXCL_MASK) | SYS_GCR_EL1_RRND,
216 #ifdef CONFIG_KASAN_HW_TAGS
217 /* Only called from assembly, silence sparse */
218 void __init kasan_hw_tags_enable(struct alt_instr *alt, __le32 *origptr,
219 __le32 *updptr, int nr_inst);
221 void __init kasan_hw_tags_enable(struct alt_instr *alt, __le32 *origptr,
222 __le32 *updptr, int nr_inst)
224 BUG_ON(nr_inst != 1); /* Branch -> NOP */
226 if (kasan_hw_tags_enabled())
227 *updptr = cpu_to_le32(aarch64_insn_gen_nop());
231 void mte_thread_init_user(void)
233 if (!system_supports_mte())
236 /* clear any pending asynchronous tag fault */
238 write_sysreg_s(0, SYS_TFSRE0_EL1);
239 clear_thread_flag(TIF_MTE_ASYNC_FAULT);
240 /* disable tag checking and reset tag generation mask */
241 set_mte_ctrl(current, 0);
244 void mte_thread_switch(struct task_struct *next)
246 if (!system_supports_mte())
249 mte_update_sctlr_user(next);
250 mte_update_gcr_excl(next);
252 /* TCO may not have been disabled on exception entry for the current task. */
253 mte_disable_tco_entry(next);
256 * Check if an async tag exception occurred at EL1.
258 * Note: On the context switch path we rely on the dsb() present
259 * in __switch_to() to guarantee that the indirect writes to TFSR_EL1
260 * are synchronized before this point.
263 mte_check_tfsr_el1();
266 void mte_cpu_setup(void)
271 * CnP must be enabled only after the MAIR_EL1 register has been set
272 * up. Inconsistent MAIR_EL1 between CPUs sharing the same TLB may
273 * lead to the wrong memory type being used for a brief window during
276 * CnP is not a boot feature so MTE gets enabled before CnP, but let's
277 * make sure that is the case.
279 BUG_ON(read_sysreg(ttbr0_el1) & TTBR_CNP_BIT);
280 BUG_ON(read_sysreg(ttbr1_el1) & TTBR_CNP_BIT);
282 /* Normal Tagged memory type at the corresponding MAIR index */
283 sysreg_clear_set(mair_el1,
284 MAIR_ATTRIDX(MAIR_ATTR_MASK, MT_NORMAL_TAGGED),
285 MAIR_ATTRIDX(MAIR_ATTR_NORMAL_TAGGED,
288 write_sysreg_s(KERNEL_GCR_EL1, SYS_GCR_EL1);
291 * If GCR_EL1.RRND=1 is implemented the same way as RRND=0, then
292 * RGSR_EL1.SEED must be non-zero for IRG to produce
293 * pseudorandom numbers. As RGSR_EL1 is UNKNOWN out of reset, we
294 * must initialize it.
296 rgsr = (read_sysreg(CNTVCT_EL0) & SYS_RGSR_EL1_SEED_MASK) <<
297 SYS_RGSR_EL1_SEED_SHIFT;
299 rgsr = 1 << SYS_RGSR_EL1_SEED_SHIFT;
300 write_sysreg_s(rgsr, SYS_RGSR_EL1);
302 /* clear any pending tag check faults in TFSR*_EL1 */
303 write_sysreg_s(0, SYS_TFSR_EL1);
304 write_sysreg_s(0, SYS_TFSRE0_EL1);
306 local_flush_tlb_all();
309 void mte_suspend_enter(void)
311 if (!system_supports_mte())
315 * The barriers are required to guarantee that the indirect writes
316 * to TFSR_EL1 are synchronized before we report the state.
321 /* Report SYS_TFSR_EL1 before suspend entry */
322 mte_check_tfsr_el1();
325 void mte_suspend_exit(void)
327 if (!system_supports_mte())
333 long set_mte_ctrl(struct task_struct *task, unsigned long arg)
335 u64 mte_ctrl = (~((arg & PR_MTE_TAG_MASK) >> PR_MTE_TAG_SHIFT) &
336 SYS_GCR_EL1_EXCL_MASK) << MTE_CTRL_GCR_USER_EXCL_SHIFT;
338 if (!system_supports_mte())
341 if (arg & PR_MTE_TCF_ASYNC)
342 mte_ctrl |= MTE_CTRL_TCF_ASYNC;
343 if (arg & PR_MTE_TCF_SYNC)
344 mte_ctrl |= MTE_CTRL_TCF_SYNC;
347 * If the system supports it and both sync and async modes are
348 * specified then implicitly enable asymmetric mode.
349 * Userspace could see a mix of both sync and async anyway due
350 * to differing or changing defaults on CPUs.
352 if (cpus_have_cap(ARM64_MTE_ASYMM) &&
353 (arg & PR_MTE_TCF_ASYNC) &&
354 (arg & PR_MTE_TCF_SYNC))
355 mte_ctrl |= MTE_CTRL_TCF_ASYMM;
357 task->thread.mte_ctrl = mte_ctrl;
358 if (task == current) {
360 mte_update_sctlr_user(task);
361 mte_update_gcr_excl(task);
362 update_sctlr_el1(task->thread.sctlr_user);
369 long get_mte_ctrl(struct task_struct *task)
372 u64 mte_ctrl = task->thread.mte_ctrl;
373 u64 incl = (~mte_ctrl >> MTE_CTRL_GCR_USER_EXCL_SHIFT) &
374 SYS_GCR_EL1_EXCL_MASK;
376 if (!system_supports_mte())
379 ret = incl << PR_MTE_TAG_SHIFT;
380 if (mte_ctrl & MTE_CTRL_TCF_ASYNC)
381 ret |= PR_MTE_TCF_ASYNC;
382 if (mte_ctrl & MTE_CTRL_TCF_SYNC)
383 ret |= PR_MTE_TCF_SYNC;
389 * Access MTE tags in another process' address space as given in mm. Update
390 * the number of tags copied. Return 0 if any tags copied, error otherwise.
391 * Inspired by __access_remote_vm().
393 static int __access_remote_tags(struct mm_struct *mm, unsigned long addr,
394 struct iovec *kiov, unsigned int gup_flags)
396 void __user *buf = kiov->iov_base;
397 size_t len = kiov->iov_len;
399 int write = gup_flags & FOLL_WRITE;
401 if (!access_ok(buf, len))
404 if (mmap_read_lock_killable(mm))
408 struct vm_area_struct *vma;
409 unsigned long tags, offset;
411 struct page *page = get_user_page_vma_remote(mm, addr,
420 * Only copy tags if the page has been mapped as PROT_MTE
421 * (PG_mte_tagged set). Otherwise the tags are not valid and
422 * not accessible to user. Moreover, an mprotect(PROT_MTE)
423 * would cause the existing tags to be cleared if the page
424 * was never mapped with PROT_MTE.
426 if (!(vma->vm_flags & VM_MTE)) {
431 WARN_ON_ONCE(!page_mte_tagged(page));
433 /* limit access to the end of the page */
434 offset = offset_in_page(addr);
435 tags = min(len, (PAGE_SIZE - offset) / MTE_GRANULE_SIZE);
437 maddr = page_address(page);
439 tags = mte_copy_tags_from_user(maddr + offset, buf, tags);
440 set_page_dirty_lock(page);
442 tags = mte_copy_tags_to_user(buf, maddr + offset, tags);
446 /* error accessing the tracer's buffer */
452 addr += tags * MTE_GRANULE_SIZE;
454 mmap_read_unlock(mm);
456 /* return an error if no tags copied */
457 kiov->iov_len = buf - kiov->iov_base;
458 if (!kiov->iov_len) {
459 /* check for error accessing the tracee's address space */
470 * Copy MTE tags in another process' address space at 'addr' to/from tracer's
471 * iovec buffer. Return 0 on success. Inspired by ptrace_access_vm().
473 static int access_remote_tags(struct task_struct *tsk, unsigned long addr,
474 struct iovec *kiov, unsigned int gup_flags)
476 struct mm_struct *mm;
479 mm = get_task_mm(tsk);
483 if (!tsk->ptrace || (current != tsk->parent) ||
484 ((get_dumpable(mm) != SUID_DUMP_USER) &&
485 !ptracer_capable(tsk, mm->user_ns))) {
490 ret = __access_remote_tags(mm, addr, kiov, gup_flags);
496 int mte_ptrace_copy_tags(struct task_struct *child, long request,
497 unsigned long addr, unsigned long data)
501 struct iovec __user *uiov = (void __user *)data;
502 unsigned int gup_flags = FOLL_FORCE;
504 if (!system_supports_mte())
507 if (get_user(kiov.iov_base, &uiov->iov_base) ||
508 get_user(kiov.iov_len, &uiov->iov_len))
511 if (request == PTRACE_POKEMTETAGS)
512 gup_flags |= FOLL_WRITE;
514 /* align addr to the MTE tag granule */
515 addr &= MTE_GRANULE_MASK;
517 ret = access_remote_tags(child, addr, &kiov, gup_flags);
519 ret = put_user(kiov.iov_len, &uiov->iov_len);
524 static ssize_t mte_tcf_preferred_show(struct device *dev,
525 struct device_attribute *attr, char *buf)
527 switch (per_cpu(mte_tcf_preferred, dev->id)) {
528 case MTE_CTRL_TCF_ASYNC:
529 return sysfs_emit(buf, "async\n");
530 case MTE_CTRL_TCF_SYNC:
531 return sysfs_emit(buf, "sync\n");
532 case MTE_CTRL_TCF_ASYMM:
533 return sysfs_emit(buf, "asymm\n");
535 return sysfs_emit(buf, "???\n");
539 static ssize_t mte_tcf_preferred_store(struct device *dev,
540 struct device_attribute *attr,
541 const char *buf, size_t count)
545 if (sysfs_streq(buf, "async"))
546 tcf = MTE_CTRL_TCF_ASYNC;
547 else if (sysfs_streq(buf, "sync"))
548 tcf = MTE_CTRL_TCF_SYNC;
549 else if (cpus_have_cap(ARM64_MTE_ASYMM) && sysfs_streq(buf, "asymm"))
550 tcf = MTE_CTRL_TCF_ASYMM;
555 per_cpu(mte_tcf_preferred, dev->id) = tcf;
560 static DEVICE_ATTR_RW(mte_tcf_preferred);
562 static int register_mte_tcf_preferred_sysctl(void)
566 if (!system_supports_mte())
569 for_each_possible_cpu(cpu) {
570 per_cpu(mte_tcf_preferred, cpu) = MTE_CTRL_TCF_ASYNC;
571 device_create_file(get_cpu_device(cpu),
572 &dev_attr_mte_tcf_preferred);
577 subsys_initcall(register_mte_tcf_preferred_sysctl);
580 * Return 0 on success, the number of bytes not probed otherwise.
582 size_t mte_probe_user_range(const char __user *uaddr, size_t size)
584 const char __user *end = uaddr + size;
588 __raw_get_user(val, uaddr, err);
592 uaddr = PTR_ALIGN(uaddr, MTE_GRANULE_SIZE);
593 while (uaddr < end) {
595 * A read is sufficient for mte, the caller should have probed
596 * for the pte write permission if required.
598 __raw_get_user(val, uaddr, err);
601 uaddr += MTE_GRANULE_SIZE;