Skip to contentSkip to conversation

xtensa: add division by zero exception handler

PATCH Superseded Max Filippov 1 participant 3 messages May 13, 2022 Tensilica Xtensa port (xtensa)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Max Filippov

author maintainer
#0 lore raw

cover xtensa: add division by zero exception handler

Hello,

this series adds handlers for hardware and artificial division by zero exceptions.

Max Filippov (2):
xtensa: add trap handler for division by zero
xtensa: support artificial division by 0 exception

 arch/xtensa/include/asm/thread_info.h |  4 ++
 arch/xtensa/kernel/asm-offsets.c      |  3 ++
 arch/xtensa/kernel/entry.S            |  5 +++
 arch/xtensa/kernel/traps.c            | 53 ++++++++++++++++++++++++++-
 4 files changed, 64 insertions(+), 1 deletion(-)
Signature · 1 line
2.30.2
author maintainer
#1 lore raw

PATCH 1/2 xtensa: add trap handler for division by zero

Add c-level handler for the division by zero exception and kill the task if it was thrown from the kernel space or send SIGFPE otherwise.

---
 arch/xtensa/kernel/traps.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
arch/xtensa/kernel/traps.c +7 -1
index d3e91ceb0775..7c7c4c7b33bb 100644
@@ -49,6 +49,7 @@
  */
 
 static void do_illegal_instruction(struct pt_regs *regs);
+static void do_div0(struct pt_regs *regs);
 static void do_interrupt(struct pt_regs *regs);
 #if XTENSA_FAKE_NMI
 static void do_nmi(struct pt_regs *regs);
@@ -95,7 +96,7 @@ static dispatch_init_table_t __initdata dispatch_init_table[] = {
 #ifdef SUPPORT_WINDOWED
 { EXCCAUSE_ALLOCA,		USER|KRNL, fast_alloca },
 #endif
-/* EXCCAUSE_INTEGER_DIVIDE_BY_ZERO unhandled */
+{ EXCCAUSE_INTEGER_DIVIDE_BY_ZERO, 0,	   do_div0 },
 /* EXCCAUSE_PRIVILEGED unhandled */
 #if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
 #ifdef CONFIG_XTENSA_UNALIGNED_USER
@@ -307,6 +308,11 @@ static void do_illegal_instruction(struct pt_regs *regs)
 	force_sig(SIGILL);
 }
 
+static void do_div0(struct pt_regs *regs)
+{
+	__die_if_kernel("Unhandled division by 0 in kernel", regs, SIGKILL);
+	force_sig_fault(SIGFPE, FPE_INTDIV, (void __user *)regs->pc);
+}
 
 /*
  * Handle unaligned memory accesses from user space. Kill task.
Signature · 1 line
2.30.2
author maintainer
#2 lore raw

PATCH 2/2 xtensa: support artificial division by 0 exception

In reply to Max Filippov

On xtensa cores wihout hardware division option division support functions from libgcc react to division by 0 attempt by executing illegal instruction followed by the characters 'DIV0'. Recognize this pattern in illegal instruction exception handler and convert it to division by 0.
When call0 userspace ABI support by probing is enabled instructions that cause illegal instruction exception when PS.WOE is clear are retried with PS.WOE set before calling c-level exception handler. Record user pc where PS.WOE was set in the fast exception handler and clear PS.WOE in the c-level exception handler if it was due to artificial division by 0.

---
 arch/xtensa/include/asm/thread_info.h |  4 +++
 arch/xtensa/kernel/asm-offsets.c      |  3 ++
 arch/xtensa/kernel/entry.S            |  5 +++
 arch/xtensa/kernel/traps.c            | 45 +++++++++++++++++++++++++++
 4 files changed, 57 insertions(+)
arch/xtensa/include/asm/thread_info.h +4 -0
index 52974317a6b6..326db1c1d5d8 100644
@@ -56,6 +56,10 @@ struct thread_info {
 	/* result of the most recent exclusive store */
 	unsigned long		atomctl8;
 #endif
+#ifdef CONFIG_USER_ABI_CALL0_PROBE
+	/* Address where PS.WOE was enabled by the ABI probing code */
+	unsigned long		ps_woe_fix_addr;
+#endif
 
 	/*
 	 * If i-th bit is set then coprocessor state is loaded into the
arch/xtensa/kernel/asm-offsets.c +3 -0
index 9a1db6ffcbf4..da38de20ae59 100644
@@ -88,6 +88,9 @@ int main(void)
 	OFFSET(TI_STSTUS, thread_info, status);
 	OFFSET(TI_CPU, thread_info, cpu);
 	OFFSET(TI_PRE_COUNT, thread_info, preempt_count);
+#ifdef CONFIG_USER_ABI_CALL0_PROBE
+	OFFSET(TI_PS_WOE_FIX_ADDR, thread_info, ps_woe_fix_addr);
+#endif
 
 	/* struct thread_info (offset from start_struct) */
 	DEFINE(THREAD_RA, offsetof (struct task_struct, thread.ra));
arch/xtensa/kernel/entry.S +5 -0
index 3224b4ceca34..e3eae648ba2e 100644
@@ -1056,6 +1056,11 @@ ENTRY(fast_illegal_instruction_user)
 	movi	a3, PS_WOE_MASK
 	or	a0, a0, a3
 	wsr	a0, ps
+#ifdef CONFIG_USER_ABI_CALL0_PROBE
+	GET_THREAD_INFO(a3, a2)
+	rsr	a0, epc1
+	s32i	a0, a3, TI_PS_WOE_FIX_ADDR
+#endif
 	l32i	a3, a2, PT_AREG3
 	l32i	a0, a2, PT_AREG0
 	rsr	a2, depc
arch/xtensa/kernel/traps.c +45 -0

57 lines not on this page · raw message

Signature · 1 line
2.30.2