Fix alignment check for misa write
v2 cover Fix alignment check for misa write
Current version disable misa.C on misa write when next instruciton is not aligned, fix it and add test.
Changes from v1:
- Use the test from https://lore.kernel.org/r/[email protected],
since it is much more comprehensive than mine.
- Add GUEST_ERROR_LOG on a bad write attempt.
Vladimir Isaev (2):
riscv: csr: do not drop C bit on misa write
tests/tcg/riscv64: add misa write test
target/riscv/tcg/csr.c | 10 ++- tests/tcg/riscv64/Makefile.softmmu-target | 4 ++ tests/tcg/riscv64/test-misa-w.S | 88 +++++++++++++++++++++++ 3 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 tests/tcg/riscv64/test-misa-w.S
Signature · 1 line
2.55.0
PATCH v2 1/2 riscv: csr: do not drop C bit on misa write
According to spec:
Quoted 4 lines
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.
--- target/riscv/tcg/csr.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
target/riscv/tcg/csr.c +7 -3
@@ -2182,9 +2182,13 @@ 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 ) { +qemu_log_mask (LOG_GUEST_ERROR ,"Unable to write MISA ext value " +"0x%x, MISA.C disable failed\n" , env->misa_ext); + +return RISCV_EXCP_NONE ; }/* Disable RVG if any of its dependencies are disabled */
Signature · 1 line
2.55.0
PATCH v2 2/2 tests/tcg/riscv64: add misa write test
In reply to Vladimir Isaev--- tests/tcg/riscv64/Makefile.softmmu-target | 4 ++ tests/tcg/riscv64/test-misa-w.S | 88 +++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 tests/tcg/riscv64/test-misa-w.S
tests/tcg/riscv64/Makefile.softmmu-target +4 -0
@@ -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$(comma)v=true $(QEMU_OPTS)$<) + # We don't currently support the multiarch system tests undefine MULTIARCH_TESTS
tests/tcg/riscv64/test-misa-w.S new file +88 -0
88 lines not on this page · raw message
Signature · 1 line
2.55.0
Re: [PATCH v2 1/2] riscv: csr: do not drop C bit on misa write
In reply to Vladimir IsaevQuoted 11 lines · Vladimir Isaev
According to spec:
Quoted 4 lines
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.
Reviewed-by Alistair Francis <[email protected]>Signed-off-by Vladimir Isaev <[email protected]>
Thanks,
Chao
Quoted 28 lines
--- target/riscv/tcg/csr.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)target/riscv/tcg/csr.c +7
-3@@ -2182,9 +2182,13 @@ 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 ) { +qemu_log_mask (LOG_GUEST_ERROR ,"Unable to write MISA ext value " +"0x%x, MISA.C disable failed\n" , env->misa_ext); + +return RISCV_EXCP_NONE ; }/* Disable RVG if any of its dependencies are disabled */ Signature · 1 line
2.55.0
Re: [PATCH v2 2/2] tests/tcg/riscv64: add misa write test
In reply to Vladimir IsaevQuoted 4 lines · Vladimir Isaev
Link:
https://lore.kernel.org/r/[email protected]Suggested-by Nicholas Piggin <[email protected]>Signed-off-by Vladimir Isaev <[email protected]>
Alistair
Quoted 123 lines
---
tests/tcg/riscv64/Makefile.softmmu-target | 4 ++ tests/tcg/riscv64/test-misa-w.S | 88
+++++++++++++++++++++++2 files changed, 92 insertions(+)create mode 100644
tests/tcg/riscv64/test-misa-w.Stests/tcg/riscv64/Makefile.softmmu-target +0
-0@@ -41,5 +41,9 @@ comma:= ,run-test-crc32: test-crc32
$(call run-test, $<, $(QEMU) -cpu rv64$(comma)xlrbr=true $(QEMU_OPTS)$<)tests/tcg/riscv64/Makefile.softmmu-target +3
-0+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$(comma)v=true $(QEMU_OPTS)$<) +
# We don't currently support the multiarch system tests undefineMULTIARCH_TESTStests/tcg/riscv64/test-misa-w.S new file +4
-0@@ -0,0 +1,88 @@ +/* + * Test for MISA changing C and related IALIGN alignment cases + * + * This test verifies that the "C" extension can be cleared and setin MISA,
+ * that a branch to 2-byte aligned instructions can be executed when "C" is
+ * enabled, and that a write to MISA which would increase IALIGN and cause
+ * the next instruction to be unaligned is ignored.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */tests/tcg/riscv64/test-misa-w.S new file +46
-0+ +#define RVC (1 << ('C'-'A')) +#define RVV (1 << ('V'-'A')) + +.option norvc + .text + .global _start +_start: + lla t0, trap + csrw mtvec, t0 + + csrr t0, misa + li t1, RVC + not t1, t1 + and t0, t0, t1 + csrw misa, t0 + csrr t1, misa + li a0, 2 # fail code + bne t0, t1, _exit # Could not clear RVC in MISA + + li t1, RVC + or t0, t0, t1 + csrw misa, t0 + csrr t1, misa + li a0, 3 # fail code + bne t0, t1, _exit # Could not set RVC in MISA + + j unalign +. = . + 2 +unalign: + + li t1, RVC + not t1, t1 + and t0, t0, t1 + csrw misa, t0 + csrr t1, misa + li a0, 4 # fail code + beq t0, t1, _exit # Was able to clear RVC in MISA + + li t0, (RVC|RVV) + not t0, t0 + and t0, t0, t1 + csrw misa, t0 + csrr t0, misa + li a0, 5 # fail code + bne t0, t1, _exit # MISA write was not ignored (RVV wascleared)
tests/tcg/riscv64/test-misa-w.S new file +32
-0+ + j realign +. = . + 2 +realign: + + # Success! + li a0, 0 + j _exit + +trap: + # Any trap is a fail code 1 + li a0, 1 + +# Exit code in a0 +_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