* [PATCH] riscv: misaligned: Do not decode cm.push as c.fsdsp without Zcd
@ 2026-12-03 5:33 Zihui Guo
2026-12-03 18:39 ` Clement Leger
0 siblings, 1 reply; 2+ messages in thread
From: Zihui Guo @ 2026-12-03 5:33 UTC (permalink / raw)
To: Paul Walmsley, Palmer Dabbelt, Albert Ou
Cc: Alexandre Ghiti, Clément Léger, linux-riscv,
linux-kernel
The misaligned store emulation decodes the c.fsdsp encoding without
checking for Zcd. Zcmp and Zcmt reuse that encoding for cm.push,
cm.pop, cm.popret, cm.popretz, cm.mvsa01, cm.mva01s, cm.jt and
cm.jalt, and both extensions are incompatible with Zcd. On a hart
with Zcmp, a cm.push with a misaligned sp traps and is emulated as
c.fsdsp: eight bytes of a floating-point register are stored instead
of the register list, sp is not adjusted, and the task continues with
the wrong stack contents.
Only accept the c.fsdsp encoding when Zcd is present. Otherwise the
store is not emulated and the task gets SIGBUS, as for any other store
the emulator does not handle.
Fixes: 7c586a555a48 ("riscv: add floating point insn support to misaligned access emulation")
Signed-off-by: Zihui Guo <youzih27@gmail.com>
---
Tested on Spike (rv64imafd_zicsr_zifencei_zicntr_zihpm_zca_zcmp, no C
or Zcd) with OpenSBI master, and Linux built with RISCV_ISA_C=n and
RISCV_SBI_FWFT_DELEGATE_MISALIGNED=y, so that misaligned stores are
emulated by the kernel ("SBI misaligned access exception delegation
ok"). A user process executes cm.push {ra}, -16 (0xb842) with
sp = 4 mod 8.
Before: the store is emulated as c.fsdsp. sp is unchanged, the value of
f16 is written at sp-8, and the process continues without a signal.
After: the process gets SIGBUS (BUS_ADRALN).
With an aligned sp, cm.push behaves correctly on both kernels.
Not tested on hardware; the test program is available on request.
Assisted-by: Claude:claude-opus-5-5
arch/riscv/kernel/traps_misaligned.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/arch/riscv/kernel/traps_misaligned.c b/arch/riscv/kernel/traps_misaligned.c
index 6e8ae6c66322..4e5d1d09a71e 100644
--- a/arch/riscv/kernel/traps_misaligned.c
+++ b/arch/riscv/kernel/traps_misaligned.c
@@ -387,7 +387,9 @@ static int handle_scalar_misaligned_store(struct pt_regs *regs)
fp = 1;
len = 8;
val.data_u64 = GET_F64_RS2S(insn, regs);
- } else if ((insn & INSN_MASK_C_FSDSP) == INSN_MATCH_C_FSDSP) {
+ } else if ((insn & INSN_MASK_C_FSDSP) == INSN_MATCH_C_FSDSP &&
+ riscv_has_extension_unlikely(RISCV_ISA_EXT_ZCD)) {
+ /* Without Zcd, this encoding is cm.push, cm.jt and the like */
fp = 1;
len = 8;
val.data_u64 = GET_F64_RS2C(insn, regs);
base-commit: 19cc08c1a6a650f8e54c0461ff198be6448f425d
--
2.43.0
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] riscv: misaligned: Do not decode cm.push as c.fsdsp without Zcd
2026-12-03 5:33 [PATCH] riscv: misaligned: Do not decode cm.push as c.fsdsp without Zcd Zihui Guo
@ 2026-12-03 18:39 ` Clement Leger
0 siblings, 0 replies; 2+ messages in thread
From: Clement Leger @ 2026-12-03 18:39 UTC (permalink / raw)
To: Zihui Guo, Paul Walmsley, Palmer Dabbelt, Albert Ou
Cc: Alexandre Ghiti, linux-riscv, linux-kernel
On 12/3/26 06:33, Zihui Guo wrote:
> The misaligned store emulation decodes the c.fsdsp encoding without
> checking for Zcd. Zcmp and Zcmt reuse that encoding for cm.push,
> cm.pop, cm.popret, cm.popretz, cm.mvsa01, cm.mva01s, cm.jt and
> cm.jalt, and both extensions are incompatible with Zcd. On a hart
> with Zcmp, a cm.push with a misaligned sp traps and is emulated as
> c.fsdsp: eight bytes of a floating-point register are stored instead
> of the register list, sp is not adjusted, and the task continues with
> the wrong stack contents.
>
> Only accept the c.fsdsp encoding when Zcd is present. Otherwise the
> store is not emulated and the task gets SIGBUS, as for any other store
> the emulator does not handle.
>
> Fixes: 7c586a555a48 ("riscv: add floating point insn support to misaligned access emulation")
> Signed-off-by: Zihui Guo <youzih27@gmail.com>
> ---
> Tested on Spike (rv64imafd_zicsr_zifencei_zicntr_zihpm_zca_zcmp, no C
> or Zcd) with OpenSBI master, and Linux built with RISCV_ISA_C=n and
> RISCV_SBI_FWFT_DELEGATE_MISALIGNED=y, so that misaligned stores are
> emulated by the kernel ("SBI misaligned access exception delegation
> ok"). A user process executes cm.push {ra}, -16 (0xb842) with
> sp = 4 mod 8.
>
> Before: the store is emulated as c.fsdsp. sp is unchanged, the value of
> f16 is written at sp-8, and the process continues without a signal.
> After: the process gets SIGBUS (BUS_ADRALN).
> With an aligned sp, cm.push behaves correctly on both kernels.
>
> Not tested on hardware; the test program is available on request.
Hi Zihui,
Could you add the reproducer as a selftest if possible ?
>
> Assisted-by: Claude:claude-opus-5-5
I think the consensus is that this line can now be omitted.
>
> arch/riscv/kernel/traps_misaligned.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/arch/riscv/kernel/traps_misaligned.c b/arch/riscv/kernel/traps_misaligned.c
> index 6e8ae6c66322..4e5d1d09a71e 100644
> --- a/arch/riscv/kernel/traps_misaligned.c
> +++ b/arch/riscv/kernel/traps_misaligned.c
> @@ -387,7 +387,9 @@ static int handle_scalar_misaligned_store(struct pt_regs *regs)
> fp = 1;
> len = 8;
> val.data_u64 = GET_F64_RS2S(insn, regs);
> - } else if ((insn & INSN_MASK_C_FSDSP) == INSN_MATCH_C_FSDSP) {
> + } else if ((insn & INSN_MASK_C_FSDSP) == INSN_MATCH_C_FSDSP &&
> + riscv_has_extension_unlikely(RISCV_ISA_EXT_ZCD)) {
> + /* Without Zcd, this encoding is cm.push, cm.jt and the like */
I wouldn't bother with the comment.
Otherwise, looks good to me (with a test added in selftests).
Thanks,
Clément
> fp = 1;
> len = 8;
> val.data_u64 = GET_F64_RS2C(insn, regs);
>
> base-commit: 19cc08c1a6a650f8e54c0461ff198be6448f425d
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-03 18:39 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 5:33 [PATCH] riscv: misaligned: Do not decode cm.push as c.fsdsp without Zcd Zihui Guo
2026-12-03 18:39 ` Clement Leger
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox