Skip to content

Commit 637ec83

Browse files
thule-vincenzoctmarinas
authored andcommitted
arm64: mte: Handle synchronous and asynchronous tag check faults
The Memory Tagging Extension has two modes of notifying a tag check fault at EL0, configurable through the SCTLR_EL1.TCF0 field: 1. Synchronous raising of a Data Abort exception with DFSC 17. 2. Asynchronous setting of a cumulative bit in TFSRE0_EL1. Add the exception handler for the synchronous exception and handling of the asynchronous TFSRE0_EL1.TF0 bit setting via a new TIF flag in do_notify_resume(). On a tag check failure in user-space, whether synchronous or asynchronous, a SIGSEGV will be raised on the faulting thread. Signed-off-by: Vincenzo Frascino <vincenzo.frascino@arm.com> Co-developed-by: Catalin Marinas <catalin.marinas@arm.com> Signed-off-by: Catalin Marinas <catalin.marinas@arm.com> Cc: Will Deacon <will@kernel.org>
1 parent 74f1082 commit 637ec83

9 files changed

Lines changed: 119 additions & 3 deletions

File tree

‎arch/arm64/include/asm/mte.h‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
#ifndef __ASSEMBLY__
9+
10+
#ifdef CONFIG_ARM64_MTE
11+
12+
void flush_mte_state(void);
13+
14+
#else
15+
16+
static inline void flush_mte_state(void)
17+
{
18+
}
19+
20+
#endif
21+
22+
#endif /* __ASSEMBLY__ */
23+
#endif /* __ASM_MTE_H */

‎arch/arm64/include/asm/thread_info.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ void arch_release_task_struct(struct task_struct *tsk);
6767
#define TIF_FOREIGN_FPSTATE 3 /* CPU's FP state is not current's */
6868
#define TIF_UPROBE 4 /* uprobe breakpoint or singlestep */
6969
#define TIF_FSCHECK 5 /* Check FS is USER_DS on return */
70+
#define TIF_MTE_ASYNC_FAULT 6 /* MTE Asynchronous Tag Check Fault */
7071
#define TIF_SYSCALL_TRACE 8 /* syscall trace active */
7172
#define TIF_SYSCALL_AUDIT 9 /* syscall auditing */
7273
#define TIF_SYSCALL_TRACEPOINT 10 /* syscall tracepoint for ftrace */
@@ -96,10 +97,11 @@ void arch_release_task_struct(struct task_struct *tsk);
9697
#define _TIF_SINGLESTEP (1 << TIF_SINGLESTEP)
9798
#define _TIF_32BIT (1 << TIF_32BIT)
9899
#define _TIF_SVE (1 << TIF_SVE)
100+
#define _TIF_MTE_ASYNC_FAULT (1 << TIF_MTE_ASYNC_FAULT)
99101

100102
#define _TIF_WORK_MASK (_TIF_NEED_RESCHED | _TIF_SIGPENDING | \
101103
_TIF_NOTIFY_RESUME | _TIF_FOREIGN_FPSTATE | \
102-
_TIF_UPROBE | _TIF_FSCHECK)
104+
_TIF_UPROBE | _TIF_FSCHECK | _TIF_MTE_ASYNC_FAULT)
103105

104106
#define _TIF_SYSCALL_WORK (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT | \
105107
_TIF_SYSCALL_TRACEPOINT | _TIF_SECCOMP | \

‎arch/arm64/kernel/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ obj-$(CONFIG_ARM_SDE_INTERFACE) += sdei.o
6262
obj-$(CONFIG_ARM64_SSBD) += ssbd.o
6363
obj-$(CONFIG_ARM64_PTR_AUTH) += pointer_auth.o
6464
obj-$(CONFIG_SHADOW_CALL_STACK) += scs.o
65+
obj-$(CONFIG_ARM64_MTE) += mte.o
6566

6667
obj-y += vdso/ probes/
6768
obj-$(CONFIG_COMPAT_VDSO) += vdso32/

‎arch/arm64/kernel/entry.S‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,32 @@ alternative_cb_end
149149
#endif
150150
.endm
151151

152+
/* Check for MTE asynchronous tag check faults */
153+
.macro check_mte_async_tcf, flgs, tmp
154+
#ifdef CONFIG_ARM64_MTE
155+
alternative_if_not ARM64_MTE
156+
b 1f
157+
alternative_else_nop_endif
158+
mrs_s \tmp, SYS_TFSRE0_EL1
159+
tbz \tmp, #SYS_TFSR_EL1_TF0_SHIFT, 1f
160+
/* Asynchronous TCF occurred for TTBR0 access, set the TI flag */
161+
orr \flgs, \flgs, #_TIF_MTE_ASYNC_FAULT
162+
str \flgs, [tsk, #TSK_TI_FLAGS]
163+
msr_s SYS_TFSRE0_EL1, xzr
164+
1:
165+
#endif
166+
.endm
167+
168+
/* Clear the MTE asynchronous tag check faults */
169+
.macro clear_mte_async_tcf
170+
#ifdef CONFIG_ARM64_MTE
171+
alternative_if ARM64_MTE
172+
dsb ish
173+
msr_s SYS_TFSRE0_EL1, xzr
174+
alternative_else_nop_endif
175+
#endif
176+
.endm
177+
152178
.macro kernel_entry, el, regsize = 64
153179
.if \regsize == 32
154180
mov w0, w0 // zero upper 32 bits of x0
@@ -182,6 +208,8 @@ alternative_cb_end
182208
ldr x19, [tsk, #TSK_TI_FLAGS]
183209
disable_step_tsk x19, x20
184210

211+
/* Check for asynchronous tag check faults in user space */
212+
check_mte_async_tcf x19, x22
185213
apply_ssbd 1, x22, x23
186214

187215
ptrauth_keys_install_kernel tsk, x20, x22, x23
@@ -233,6 +261,13 @@ alternative_if ARM64_HAS_IRQ_PRIO_MASKING
233261
str x20, [sp, #S_PMR_SAVE]
234262
alternative_else_nop_endif
235263

264+
/* Re-enable tag checking (TCO set on exception entry) */
265+
#ifdef CONFIG_ARM64_MTE
266+
alternative_if ARM64_MTE
267+
SET_PSTATE_TCO(0)
268+
alternative_else_nop_endif
269+
#endif
270+
236271
/*
237272
* Registers that may be useful after this macro is invoked:
238273
*
@@ -744,6 +779,8 @@ SYM_CODE_START_LOCAL(ret_to_user)
744779
and x2, x1, #_TIF_WORK_MASK
745780
cbnz x2, work_pending
746781
finish_ret_to_user:
782+
/* Ignore asynchronous tag check faults in the uaccess routines */
783+
clear_mte_async_tcf
747784
enable_step_tsk x1, x2
748785
#ifdef CONFIG_GCC_PLUGIN_STACKLEAK
749786
bl stackleak_erase

‎arch/arm64/kernel/mte.c‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* Copyright (C) 2020 ARM Ltd.
4+
*/
5+
6+
#include <linux/thread_info.h>
7+
8+
#include <asm/cpufeature.h>
9+
#include <asm/mte.h>
10+
#include <asm/sysreg.h>
11+
12+
void flush_mte_state(void)
13+
{
14+
if (!system_supports_mte())
15+
return;
16+
17+
/* clear any pending asynchronous tag fault */
18+
dsb(ish);
19+
write_sysreg_s(0, SYS_TFSRE0_EL1);
20+
clear_thread_flag(TIF_MTE_ASYNC_FAULT);
21+
}

‎arch/arm64/kernel/process.c‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#include <asm/exec.h>
5353
#include <asm/fpsimd.h>
5454
#include <asm/mmu_context.h>
55+
#include <asm/mte.h>
5556
#include <asm/processor.h>
5657
#include <asm/pointer_auth.h>
5758
#include <asm/stacktrace.h>
@@ -239,7 +240,7 @@ static void print_pstate(struct pt_regs *regs)
239240
const char *btype_str = btypes[(pstate & PSR_BTYPE_MASK) >>
240241
PSR_BTYPE_SHIFT];
241242

242-
printk("pstate: %08llx (%c%c%c%c %c%c%c%c %cPAN %cUAO BTYPE=%s)\n",
243+
printk("pstate: %08llx (%c%c%c%c %c%c%c%c %cPAN %cUAO %cTCO BTYPE=%s)\n",
243244
pstate,
244245
pstate & PSR_N_BIT ? 'N' : 'n',
245246
pstate & PSR_Z_BIT ? 'Z' : 'z',
@@ -251,6 +252,7 @@ static void print_pstate(struct pt_regs *regs)
251252
pstate & PSR_F_BIT ? 'F' : 'f',
252253
pstate & PSR_PAN_BIT ? '+' : '-',
253254
pstate & PSR_UAO_BIT ? '+' : '-',
255+
pstate & PSR_TCO_BIT ? '+' : '-',
254256
btype_str);
255257
}
256258
}
@@ -336,6 +338,7 @@ void flush_thread(void)
336338
tls_thread_flush();
337339
flush_ptrace_hw_breakpoint(current);
338340
flush_tagged_addr_state();
341+
flush_mte_state();
339342
}
340343

341344
void release_thread(struct task_struct *dead_task)
@@ -368,6 +371,9 @@ int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
368371
dst->thread.sve_state = NULL;
369372
clear_tsk_thread_flag(dst, TIF_SVE);
370373

374+
/* clear any pending asynchronous tag fault raised by the parent */
375+
clear_tsk_thread_flag(dst, TIF_MTE_ASYNC_FAULT);
376+
371377
return 0;
372378
}
373379

‎arch/arm64/kernel/signal.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,9 @@ static void setup_return(struct pt_regs *regs, struct k_sigaction *ka,
748748
regs->pstate |= PSR_BTYPE_C;
749749
}
750750

751+
/* TCO (Tag Check Override) always cleared for signal handlers */
752+
regs->pstate &= ~PSR_TCO_BIT;
753+
751754
if (ka->sa.sa_flags & SA_RESTORER)
752755
sigtramp = ka->sa.sa_restorer;
753756
else
@@ -932,6 +935,12 @@ asmlinkage void do_notify_resume(struct pt_regs *regs,
932935
if (thread_flags & _TIF_UPROBE)
933936
uprobe_notify_resume(regs);
934937

938+
if (thread_flags & _TIF_MTE_ASYNC_FAULT) {
939+
clear_thread_flag(TIF_MTE_ASYNC_FAULT);
940+
send_sig_fault(SIGSEGV, SEGV_MTEAERR,
941+
(void __user *)NULL, current);
942+
}
943+
935944
if (thread_flags & _TIF_SIGPENDING)
936945
do_signal(regs);
937946

‎arch/arm64/kernel/syscall.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,16 @@ static void el0_svc_common(struct pt_regs *regs, int scno, int sc_nr,
123123
local_daif_restore(DAIF_PROCCTX);
124124
user_exit();
125125

126+
if (system_supports_mte() && (flags & _TIF_MTE_ASYNC_FAULT)) {
127+
/*
128+
* Process the asynchronous tag check fault before the actual
129+
* syscall. do_notify_resume() will send a signal to userspace
130+
* before the syscall is restarted.
131+
*/
132+
regs->regs[0] = -ERESTARTNOINTR;
133+
return;
134+
}
135+
126136
if (has_syscall_work(flags)) {
127137
/*
128138
* The de-facto standard way to skip a system call using ptrace

‎arch/arm64/mm/fault.c‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,13 @@ static int do_sea(unsigned long addr, unsigned int esr, struct pt_regs *regs)
641641
return 0;
642642
}
643643

644+
static int do_tag_check_fault(unsigned long addr, unsigned int esr,
645+
struct pt_regs *regs)
646+
{
647+
do_bad_area(addr, esr, regs);
648+
return 0;
649+
}
650+
644651
static const struct fault_info fault_info[] = {
645652
{ do_bad, SIGKILL, SI_KERNEL, "ttbr address size fault" },
646653
{ do_bad, SIGKILL, SI_KERNEL, "level 1 address size fault" },
@@ -659,7 +666,7 @@ static const struct fault_info fault_info[] = {
659666
{ do_page_fault, SIGSEGV, SEGV_ACCERR, "level 2 permission fault" },
660667
{ do_page_fault, SIGSEGV, SEGV_ACCERR, "level 3 permission fault" },
661668
{ do_sea, SIGBUS, BUS_OBJERR, "synchronous external abort" },
662-
{ do_bad, SIGKILL, SI_KERNEL, "unknown 17" },
669+
{ do_tag_check_fault, SIGSEGV, SEGV_MTESERR, "synchronous tag check fault" },
663670
{ do_bad, SIGKILL, SI_KERNEL, "unknown 18" },
664671
{ do_bad, SIGKILL, SI_KERNEL, "unknown 19" },
665672
{ do_sea, SIGKILL, SI_KERNEL, "level 0 (translation table walk)" },

0 commit comments

Comments
 (0)