[PATCH] LoongArch: Mask FCSR Cause bits against Enable bits in do_fpe()
From: Shirong Qu
Date: Sun Nov 29 2026 - 15:31:41 EST
force_fcsr_sig() expects its fcsr argument to contain only Cause bits
that are enabled in FCSR0, as documented in the comment right above it.
However, do_fpe() clears FCSR0 using mask_fcsr_x(fcsr) and then passes
the raw unmasked fcsr to process_fpemu_return() and force_fcsr_sig().
Per the LoongArch Reference Manual (Volume 1, Section 3.1.3.3,
"Floating-point Control and Status Register", and Section 3.1.4,
"Floating-Point Exceptions"), FPU instructions update the Cause field in
FCSR0 for all exceptions raised by the last operation regardless of the
Enables field, and trigger an FPE trap when any Cause bit and its
corresponding Enable bit are both 1.
Because force_fcsr_sig() checks Cause bits in fixed priority order, an
unmasked higher-priority Cause bit can shadow the enabled exception that
actually triggered the trap. For example, enabling only Inexact
(FPU_CSR_INE_E) and executing DBL_MAX * 2.0 sets both Cause.O and
Cause.I, causing force_fcsr_sig() to report FPE_FLTOVF instead of
FPE_FLTRES.
Save mask_fcsr_x(fcsr) in a local variable and pass it to
process_fpemu_return() so SIGFPE si_code is selected only from enabled
Cause bits.
Cc: stable@xxxxxxxxxxxxxxx
Fixes: 0603839b18f4 ("LoongArch: Add exception/interrupt handling")
Signed-off-by: Shirong Qu <qc_n3ep@xxxxxxxxxxx>
---
Tested on QEMU loongarch64 (virt) with a standalone user-space test that
enables only FPU_CSR_INE_E and evaluates DBL_MAX * 2.0: si_code changes
from FPE_FLTOVF (4) before this patch to FPE_FLTRES (6) after this patch.
AI assistance (Gemini) was used to help investigate the issue, draft the
patch and changelog, and build the QEMU test harness; the author reviewed
all changes and verified the test results.
arch/loongarch/kernel/traps.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/arch/loongarch/kernel/traps.c b/arch/loongarch/kernel/traps.c
index 20ce2c8d77a1..160d9c19d540 100644
--- a/arch/loongarch/kernel/traps.c
+++ b/arch/loongarch/kernel/traps.c
@@ -516,6 +516,7 @@ static int process_fpemu_return(int sig, void __user *fault_addr, unsigned long
asmlinkage void noinstr do_fpe(struct pt_regs *regs, unsigned long fcsr)
{
int sig;
+ unsigned long cause;
void __user *fault_addr;
irqentry_state_t state = irqentry_enter(regs);
@@ -524,7 +525,8 @@ asmlinkage void noinstr do_fpe(struct pt_regs *regs, unsigned long fcsr)
goto out;
/* Clear FCSR.Cause before enabling interrupts */
- write_fcsr(LOONGARCH_FCSR0, fcsr & ~mask_fcsr_x(fcsr));
+ cause = mask_fcsr_x(fcsr);
+ write_fcsr(LOONGARCH_FCSR0, fcsr & ~cause);
local_irq_enable();
die_if_kernel("FP exception in kernel code", regs);
@@ -533,7 +535,7 @@ asmlinkage void noinstr do_fpe(struct pt_regs *regs, unsigned long fcsr)
fault_addr = (void __user *) regs->csr_era;
/* Send a signal if required. */
- process_fpemu_return(sig, fault_addr, fcsr);
+ process_fpemu_return(sig, fault_addr, cause);
out:
local_irq_disable();
base-commit: ca20d5a011d35fc25e328fe31ac47f7aae557c5a
--
2.56.1