* [PATCH 1/2] riscv: csr: do not drop C bit on misa write
@ 2026-08-06 13:37 Vladimir Isaev
2026-08-06 13:37 ` [PATCH 2/2] tests/tcg/riscv64: add misa write test Vladimir Isaev
0 siblings, 1 reply; 2+ messages in thread
From: Vladimir Isaev @ 2026-08-06 13:37 UTC (permalink / raw)
To: qemu-devel
Cc: palmer, alistair.francis, liwei1518, daniel.barboza, zhiwei_liu,
chao.liu, qemu-riscv, Vladimir Isaev
According to spec:
> Writing misa may increase IALIGN, e.g., by disabling the "C" extension.
> If an instruction that would write misa increases IALIGN, and the
> subsequent instruction’s address is not IALIGN-bit aligned, the
> write to misa is suppressed, leaving misa unchanged.
So attempt to disable C extension if next instruction is not aligned should not
change the misa.
Signed-off-by: Vladimir Isaev <vvisaev@gmail.com>
---
target/riscv/tcg/csr.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/target/riscv/tcg/csr.c b/target/riscv/tcg/csr.c
index 36f2004bc5..58c206c1fa 100644
--- a/target/riscv/tcg/csr.c
+++ b/target/riscv/tcg/csr.c
@@ -2182,9 +2182,10 @@ static RISCVException write_misa(CPURISCVState *env, int csrno,
/* Mask extensions that are not supported by this hart */
val &= env->misa_ext_mask;
- /* Suppress 'C' if next instruction is not aligned. */
- if ((val & RVC) && (get_next_pc(env, ra) & 3) != 0) {
- val &= ~RVC;
+ /* drop write if RVC is cleared and next instruction is not aligned */
+ if ((env->misa_ext & RVC) && !(val & RVC) &&
+ (get_next_pc(env, ra) & 3) != 0) {
+ return RISCV_EXCP_NONE;
}
/* Disable RVG if any of its dependencies are disabled */
--
2.55.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* [PATCH 2/2] tests/tcg/riscv64: add misa write test
2026-08-06 13:37 [PATCH 1/2] riscv: csr: do not drop C bit on misa write Vladimir Isaev
@ 2026-08-06 13:37 ` Vladimir Isaev
0 siblings, 0 replies; 2+ messages in thread
From: Vladimir Isaev @ 2026-08-06 13:37 UTC (permalink / raw)
To: qemu-devel
Cc: palmer, alistair.francis, liwei1518, daniel.barboza, zhiwei_liu,
chao.liu, qemu-riscv, Vladimir Isaev
Add test to check that writing misa before compact instruction doesn't
drop any bits.
Signed-off-by: Vladimir Isaev <vvisaev@gmail.com>
---
tests/tcg/riscv64/Makefile.softmmu-target | 4 ++
tests/tcg/riscv64/test-misa-w.S | 46 +++++++++++++++++++++++
2 files changed, 50 insertions(+)
create mode 100644 tests/tcg/riscv64/test-misa-w.S
diff --git a/tests/tcg/riscv64/Makefile.softmmu-target b/tests/tcg/riscv64/Makefile.softmmu-target
index 82be8a2c91..e7d052909f 100644
--- a/tests/tcg/riscv64/Makefile.softmmu-target
+++ b/tests/tcg/riscv64/Makefile.softmmu-target
@@ -41,5 +41,9 @@ comma:= ,
run-test-crc32: test-crc32
$(call run-test, $<, $(QEMU) -cpu rv64$(comma)xlrbr=true $(QEMU_OPTS)$<)
+EXTRA_RUNS += run-test-misa-w
+run-test-misa-w: test-misa-w
+ $(call run-test, $<, $(QEMU) -cpu rv64$(comma)x-misa-w=true$(comma)c=true $(QEMU_OPTS)$<)
+
# We don't currently support the multiarch system tests
undefine MULTIARCH_TESTS
diff --git a/tests/tcg/riscv64/test-misa-w.S b/tests/tcg/riscv64/test-misa-w.S
new file mode 100644
index 0000000000..22cba98b8c
--- /dev/null
+++ b/tests/tcg/riscv64/test-misa-w.S
@@ -0,0 +1,46 @@
+#define MISA_C (1<<2)
+
+ .text
+ .globl _start
+ .balign 4
+_start:
+ .option norvc
+ lla t0, trap
+ csrw mtvec, t0
+
+ # Fail if misa after write is not the same as before
+ csrr t0, misa
+ .option rvc
+ c.nop
+ csrw misa, t0
+ c.nop
+ csrr t1, misa
+ bne t0, t1, fail
+
+ li a0, 0
+ j _exit
+
+
+ .balign 4
+ .option norvc
+trap:
+fail:
+ li a0, 1
+_exit:
+ lla a1, semiargs
+ li t0, 0x20026 # ADP_Stopped_ApplicationExit
+ sd t0, 0(a1)
+ sd a0, 8(a1)
+ li a0, 0x20 # TARGET_SYS_EXIT_EXTENDED
+
+ # Semihosting call sequence
+ .balign 16
+ slli zero, zero, 0x1f
+ ebreak
+ srai zero, zero, 0x7
+ j .
+
+ .data
+ .balign 16
+semiargs:
+ .space 16
--
2.55.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-08-06 13:39 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-06 13:37 [PATCH 1/2] riscv: csr: do not drop C bit on misa write Vladimir Isaev
2026-08-06 13:37 ` [PATCH 2/2] tests/tcg/riscv64: add misa write test Vladimir Isaev
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.