Skip to contentSkip to conversation

riscv: traps: Fix si_addr in misaligned access SIGBUS

PATCH New Karl Mehltretter 1 participant 1 message Nov 2 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 riscv: traps: Fix si_addr in misaligned access SIGBUS

do_trap_misaligned() sends SIGBUS with BUS_ADRALN when the kernel does not complete a misaligned access from user space, for example with PR_UNALIGN_SIGBUS. It passes regs->epc as the fault address, so si_addr holds the program counter, or 0 after a fault during emulation, instead of the address of the access. POSIX defines si_addr for SIGBUS as the address of the faulting memory reference.

Pass regs->badaddr instead.

Fixes 76d2a0493a17 ("RISC-V: Init and Halt Code")

---
Assisted by an LLM.

Tested on QEMU's virt machine (d73149283205, -cpu rv64,zicclsm=false) with OpenSBI v1.8.1. The kernels are abf8c33470f7, built with clang 22 from defconfig plus NONPORTABLE and RISCV_SBI_FWFT_DELEGATE_MISALIGNED. Not tested on real hardware.

The test takes SIGBUS for lw and sw crossing into an unmapped page, lw with PR_UNALIGN_SIGBUS, and amoswap.w. With this patch, si_addr is the misaligned address in all four. Before it, si_addr is 0 for the first two and the PC for the others.

The first two still report PC 0 in the ucontext. That separate bug is fixed by Zishun Yi's pending patch:
https://lore.kernel.org/r/[email protected]/

 arch/riscv/kernel/traps.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/kernel/traps.c +1 -1
index f8292adda0..0af1cdb352 100644
@@ -225,7 +225,7 @@ static void do_trap_misaligned(struct pt_regs *regs, enum misaligned_access_type
 	}
 
 	if (misaligned_handler[type].handler(regs))
-		do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
+		do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->badaddr,
 			      misaligned_handler[type].type_str);
 
 	if (user_mode(regs)) {

base-commit: abf8c33470f78e2226ee3dcdc69004660020ab47

Signature · 1 line
2.39.5 (Apple Git-154)