Skip to content

Commit 40dd442

Browse files
committed
arch: riscv: decouple mtval usefulness for FP traps from QEMU target
Introduce CONFIG_RISCV_NO_MTVAL_ON_FP_TRAP to handle implementations where the mtval CSR does not provide useful information during floating-point illegal instruction exceptions. The RISC-V specification states that mtval is "either set to zero or written with exception-specific information" on traps. Some implementations, including QEMU, do not populate mtval with the faulting instruction value during FP-related illegal instruction exceptions, making it unusable for FP exception handling. Previously, this behavior was hardcoded for QEMU targets only, but other CPU implementations may also lack useful mtval content for FP traps. Decoupling this from CONFIG_QEMU_TARGET and allows other platforms to properly declare this limitation. The new Kconfig option defaults to enabled for QEMU targets to maintain backward compatibility. Signed-off-by: Afonso Oliveira <[email protected]>
1 parent 20efb9e commit 40dd442

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

arch/riscv/Kconfig

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,26 @@ config ARCH_HAS_STACKWALK
492492
Internal config to indicate that the arch_stack_walk() API is implemented
493493
and it can be enabled.
494494

495+
config RISCV_NO_MTVAL_ON_FP_TRAP
496+
bool
497+
default y if CONFIG_QEMU_TARGET
498+
help
499+
This implementation does not provide useful information in the mtval
500+
CSR (Machine Trap Value register) when floating-point illegal
501+
instruction exceptions occur.
502+
503+
The RISC-V specification allows implementations to decide on a
504+
case-by-case basis when mtval contains meaningful values. The spec
505+
states that mtval is "either set to zero or written with
506+
exception-specific information" on traps. However, this
507+
"exception-specific information" may not necessarily be the faulting
508+
instruction value, and implementations have flexibility in what they
509+
provide.
510+
511+
When this option is enabled, the mtval content cannot be relied upon
512+
to contain the faulting FP instruction, requiring alternative methods
513+
to handle FP exceptions.
514+
495515
rsource "Kconfig.isa"
496516

497517
endmenu

arch/riscv/core/isr.S

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ SECTION_FUNC(exception.entry, _isr_wrapper)
225225
bne t1, t2, no_fp
226226
/* determine if we trapped on an FP instruction. */
227227
csrr t2, mtval /* get faulting instruction */
228-
#ifdef CONFIG_QEMU_TARGET
228+
#ifdef RISCV_NO_MTVAL_ON_FP_TRAP
229229
/*
230230
* Some implementations may not support MTVAL in this capacity.
231231
* Notably QEMU when a CSR instruction is involved.

0 commit comments

Comments
 (0)