From: Karl Mehltretter <kmehltretter@gmail.com>
To: Paul Walmsley <pjw@kernel.org>,
Palmer Dabbelt <palmer@dabbelt.com>,
Albert Ou <aou@eecs.berkeley.edu>
Cc: Karl Mehltretter <kmehltretter@gmail.com>,
Alexandre Ghiti <alex@ghiti.fr>, Andreas Schwab <schwab@suse.de>,
"Maciej W. Rozycki" <macro@orcam.me.uk>,
Nam Cao <namcao@linutronix.de>, Zishun Yi <vulab@iscas.ac.cn>,
linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org
Subject: [PATCH] riscv: traps: Fix si_addr in misaligned access SIGBUS
Date: Mon, 2 Nov 2026 05:12:37 +0100 [thread overview]
Message-ID: <20261102041237.80873-1-kmehltretter@gmail.com> (raw)
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")
Signed-off-by: Karl Mehltretter <kmehltretter@gmail.com>
---
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/20260511124101.3202094-1-vulab@iscas.ac.cn/
arch/riscv/kernel/traps.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c
index f8292adda0..0af1cdb352 100644
--- a/arch/riscv/kernel/traps.c
+++ b/arch/riscv/kernel/traps.c
@@ -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
--
2.39.5 (Apple Git-154)
reply other threads:[~2026-11-02 4:12 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261102041237.80873-1-kmehltretter@gmail.com \
--to=kmehltretter@gmail.com \
--cc=alex@ghiti.fr \
--cc=aou@eecs.berkeley.edu \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=macro@orcam.me.uk \
--cc=namcao@linutronix.de \
--cc=palmer@dabbelt.com \
--cc=pjw@kernel.org \
--cc=schwab@suse.de \
--cc=vulab@iscas.ac.cn \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®