Skip to contentSkip to conversation

selftests: riscv: add misaligned access testing

PATCH Superseded Clément Léger 2 participants 5 messages Jul 10, 2025

Subsystems (2)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 selftests: riscv: add misaligned access testing

This selftest tests all the currently emulated instructions (except for the RV32 compressed ones which are left as a future exercise for a RV32 user). For the FPU instructions, all the FPU registers are tested.

---

Note: This test can be executed using an SBI that support FWFT or that delegates misaligned traps by default. If using QEMU, you will need the patches mentioned at [1] so that misaligned accesses will generate a trap.

Note: This commit was part of a series [2] that was partially merged.

Note: the remaining checkpatch errors are not applicable to this tests which is a userspace one and does not use the kernel headers. Macros with complex values can not be enclosed in do while loop since they are generating functions.

V4:
- Fixed a typo in test name s/load/store

V3:
- Fixed a segfault and a sign extension error found when compiling with -O<x>, x != 0 (Alex)
- Use inline assembly to generate the sigbus and avoid GCC optimizations

V2:
- Fix commit description
- Fix a few errors reported by checkpatch.pl

 tools/testing/selftests/riscv/Makefile        |   2 +-
 .../selftests/riscv/misaligned/.gitignore     |   1 +
 .../selftests/riscv/misaligned/Makefile       |  12 +
 .../selftests/riscv/misaligned/common.S       |  33 +++
 .../testing/selftests/riscv/misaligned/fpu.S  | 180 +++++++++++++
 tools/testing/selftests/riscv/misaligned/gp.S | 103 +++++++
 .../selftests/riscv/misaligned/misaligned.c   | 253 ++++++++++++++++++
 7 files changed, 583 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/riscv/misaligned/.gitignore
 create mode 100644 tools/testing/selftests/riscv/misaligned/Makefile
 create mode 100644 tools/testing/selftests/riscv/misaligned/common.S
 create mode 100644 tools/testing/selftests/riscv/misaligned/fpu.S
 create mode 100644 tools/testing/selftests/riscv/misaligned/gp.S
 create mode 100644 tools/testing/selftests/riscv/misaligned/misaligned.c
tools/testing/selftests/riscv/Makefile +1 -1
index 099b8c1f46f8..95a98ceeb3b3 100644
@@ -5,7 +5,7 @@
 ARCH ?= $(shell uname -m 2>/dev/null || echo not)
 
 ifneq (,$(filter $(ARCH),riscv))
-RISCV_SUBTARGETS ?= abi hwprobe mm sigreturn vector
+RISCV_SUBTARGETS ?= abi hwprobe mm sigreturn vector misaligned
 else
 RISCV_SUBTARGETS :=
 endif
tools/testing/selftests/riscv/misaligned/.gitignore new file +1 -0
new file mode 100644
index 000000000000..5eff15a1f981
@@ -0,0 +1 @@
+misaligned
tools/testing/selftests/riscv/misaligned/Makefile new file +12 -0
new file mode 100644
index 000000000000..1aa40110c50d
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2021 ARM Limited
+# Originally tools/testing/arm64/abi/Makefile
+
+CFLAGS += -I$(top_srcdir)/tools/include
+
+TEST_GEN_PROGS := misaligned
+
+include ../../lib.mk
+
+$(OUTPUT)/misaligned: misaligned.c fpu.S gp.S
+	$(CC) -g3 -static -o$@ -march=rv64imafdc $(CFLAGS) $(LDFLAGS) $^
tools/testing/selftests/riscv/misaligned/common.S new file +33 -0
new file mode 100644
index 000000000000..8fa00035bd5d
@@ -0,0 +1,33 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (c) 2025 Rivos Inc.
+ *
+ * Authors:
+ *     Clément Léger <[email protected]>
+ */
+
+.macro lb_sb temp, offset, src, dst
+	lb \temp, \offset(\src)
+	sb \temp, \offset(\dst)
+.endm
+
+.macro copy_long_to temp, src, dst
+	lb_sb \temp, 0, \src, \dst,
+	lb_sb \temp, 1, \src, \dst,
+	lb_sb \temp, 2, \src, \dst,
+	lb_sb \temp, 3, \src, \dst,
+	lb_sb \temp, 4, \src, \dst,
+	lb_sb \temp, 5, \src, \dst,
+	lb_sb \temp, 6, \src, \dst,
+	lb_sb \temp, 7, \src, \dst,
+.endm
+
+.macro sp_stack_prologue offset
+	addi sp, sp, -8
+	sub sp, sp, \offset
+.endm
+
+.macro sp_stack_epilogue offset
+	add sp, sp, \offset
+	addi sp, sp, 8
+.endm
tools/testing/selftests/riscv/misaligned/fpu.S new file +180 -0

180 lines not on this page · raw message

tools/testing/selftests/riscv/misaligned/gp.S new file +103 -0

103 lines not on this page · raw message

tools/testing/selftests/riscv/misaligned/misaligned.c new file +253 -0

253 lines not on this page · raw message

Signature · 1 line
2.50.0
#1 lore raw
Quoted 3 lines · Clément Léger

This selftest tests all the currently emulated instructions (except for the RV32 compressed ones which are left as a future exercise for a RV32 user). For the FPU instructions, all the FPU registers are tested.

If that didn't catch the missing sign extension that I just fixed in
<https://lore.kernel.org/linux-riscv/[email protected]>, you should consider extending the tests.

Signature · 3 lines
Andreas Schwab, SUSE Labs, [email protected]
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."
#2 lore raw
Quoted 10 lines · Andreas Schwab
Quoted 3 lines · Clément Léger

This selftest tests all the currently emulated instructions (except for the RV32 compressed ones which are left as a future exercise for a RV32 user). For the FPU instructions, all the FPU registers are tested.

If that didn't catch the missing sign extension that I just fixed in
<https://lore.kernel.org/linux-riscv/[email protected]>, you should consider extending the tests.

Hi Andreas, you link doesn't work and I didn't find anything about sign extension except a patch you wrote for arch_cmpxg().

Thanks,

Clément

#3 lore raw
Quoted 7 lines · Clément Léger
Quoted 6 lines · Andreas Schwab
Quoted 3 lines · Clément Léger

This selftest tests all the currently emulated instructions (except for the RV32 compressed ones which are left as a future exercise for a RV32 user). For the FPU instructions, all the FPU registers are tested.

Quoted 7 lines
Quoted 4 lines

If that didn't catch the missing sign extension that I just fixed in
<https://lore.kernel.org/linux-riscv/[email protected]>, you should consider extending the tests.

Hi Andreas, you link doesn't work and I didn't find anything about sign extension except a patch you wrote for arch_cmpxg().

lore.k.o is currently down, here's the patch I have sent:

From 77c8255da24ee4fac54e2371594d7210d1ddee19 Mon Sep 17 00:00:00 2001
From: Andreas Schwab <[email protected]>
Date: Thu, 10 Jul 2025 13:52:35 +0200
Subject: [PATCH] riscv: traps_misaligned: properly sign extend value in
misaligned load handler

Add missing cast to signed long.

---
 arch/riscv/kernel/traps_misaligned.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/kernel/traps_misaligned.c +1 -1
index 93043924fe6c..f760e4fcc052 100644
@@ -461,7 +461,7 @@ static int handle_scalar_misaligned_load(struct pt_regs *regs)
 	}
 
 	if (!fp)
-		SET_RD(insn, regs, val.data_ulong << shift >> shift);
+		SET_RD(insn, regs, (long)(val.data_ulong << shift) >> shift);
 	else if (len == 8)
 		set_f64_rd(insn, regs, val.data_u64);
 	else
Signature · 7 lines
2.50.0


-- 
Andreas Schwab, SUSE Labs, [email protected]
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."
#4 lore raw
Quoted 37 lines · Andreas Schwab
Quoted 7 lines · Clément Léger
Quoted 6 lines · Andreas Schwab
Quoted 3 lines · Clément Léger

This selftest tests all the currently emulated instructions (except for the RV32 compressed ones which are left as a future exercise for a RV32 user). For the FPU instructions, all the FPU registers are tested.

Quoted 7 lines
Quoted 4 lines

If that didn't catch the missing sign extension that I just fixed in
<https://lore.kernel.org/linux-riscv/[email protected]>, you should consider extending the tests.

Hi Andreas, you link doesn't work and I didn't find anything about sign extension except a patch you wrote for arch_cmpxg().

lore.k.o is currently down, here's the patch I have sent:

From 77c8255da24ee4fac54e2371594d7210d1ddee19 Mon Sep 17 00:00:00 2001
From: Andreas Schwab <[email protected]>
Date: Thu, 10 Jul 2025 13:52:35 +0200
Subject: [PATCH] riscv: traps_misaligned: properly sign extend value in
misaligned load handler

Add missing cast to signed long.

---
 arch/riscv/kernel/traps_misaligned.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/kernel/traps_misaligned.c +0 -0
index 93043924fe6c..f760e4fcc052 100644

arch/riscv/kernel/traps_misaligned.c static int handle_scalar_misaligned_load(struct pt_regs *regs) · line 464

Quoted 5 lines
arch/riscv/kernel/traps_misaligned.c +1 -1
@@ -461,7 +461,7 @@ static int handle_scalar_misaligned_load(struct pt_regs *regs)
 	}
 
 	if (!fp)
-		SET_RD(insn, regs, val.data_ulong << shift >> shift);
+		SET_RD(insn, regs, (long)(val.data_ulong << shift) >> shift);

Hi Andreas,

Nice catch, it seems like it was fixed in OpenSBI but never backported in the kernel. As you suggested, I'll modify the test to test the sign extension.

Thanks,

Clément

Quoted 3 lines
arch/riscv/kernel/traps_misaligned.c +0 -0
 	else if (len == 8)
 		set_f64_rd(insn, regs, val.data_u64);
 	else