Skip to contentSkip to conversation

target/riscv: corner case fixes

PATCH Superseded Nicholas Piggin 5 participants 11 messages Sep 3, 2025 – Sep 17, 2025 RISC-V

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover target/riscv: corner case fixes

There is ongoing effort to run generated test verification on the QEMU riscv CPU which has turned out a few corner cases.

I added some fixes for these, as well as tcg tests. The interrupted vector test also catches a bug in "Generate strided vector loads/stores with tcg nodes." that I referred to in the v5 thread for that series.

Thanks,
Nick

Nicholas Piggin (3):
target/riscv: Fix IALIGN check in misa write target/risvc: Fix vector whole ldst vstart check tests/tcg: Add riscv test for interrupted vector ops

 target/riscv/csr.c                        |  16 +-
 target/riscv/vector_helper.c              |   2 +
 tests/tcg/riscv64/Makefile.softmmu-target |   5 +
 tests/tcg/riscv64/Makefile.target         |  10 ++
 tests/tcg/riscv64/misa-ialign.S           |  88 +++++++++
 tests/tcg/riscv64/test-interrupted-v.c    | 208 ++++++++++++++++++++++
 tests/tcg/riscv64/test-vstart-overflow.c  |  75 ++++++++
 7 files changed, 401 insertions(+), 3 deletions(-)
 create mode 100644 tests/tcg/riscv64/misa-ialign.S
 create mode 100644 tests/tcg/riscv64/test-interrupted-v.c
 create mode 100644 tests/tcg/riscv64/test-vstart-overflow.c
Signature · 1 line
2.51.0
#1 lore raw

PATCH 1/3 target/riscv: Fix IALIGN check in misa write

The instruction alignment check for the C extension was inverted. The new value should be checked for C bit clear (thus increasing IALIGN). If IALIGN is incompatible, then the write to misa should be suppressed, not just ignoring the update to the C bit.

From the ISA:

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.

This was found with a verification test generator based on RiESCUE.

---
 target/riscv/csr.c                        | 16 ++++-
 tests/tcg/riscv64/Makefile.softmmu-target |  5 ++
 tests/tcg/riscv64/misa-ialign.S           | 88 +++++++++++++++++++++++
 3 files changed, 106 insertions(+), 3 deletions(-)
 create mode 100644 tests/tcg/riscv64/misa-ialign.S
target/riscv/csr.c +13 -3
index 8842e07a73..64b55b7add 100644
@@ -2140,9 +2140,19 @@ 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;
+    /*
+     * misa writes that increase IALIGN beyond alignment of the next
+     * instruction cause the write to misa to be suppressed. Clearing
+     * "C" extension increases IALIGN.
+     */
+    if (!(val & RVC) && (get_next_pc(env, ra) & 3) != 0) {
+        /*
+         * If the next instruction is unaligned mod 4 then "C" must be
+         * set or this instruction could not be executing, so we know
+         * this is is clearing "C" (and not just keeping it clear).
+         */
+        g_assert(env->misa_ext & RVC);
+        return RISCV_EXCP_NONE;
     }
 
     /* Disable RVG if any of its dependencies are disabled */
tests/tcg/riscv64/Makefile.softmmu-target +5 -0
index 3ca595335d..6e470a028f 100644
@@ -24,5 +24,10 @@ EXTRA_RUNS += run-test-mepc-masking
 run-test-mepc-masking: test-mepc-masking
 	$(call run-test, $<, $(QEMU) $(QEMU_OPTS)$<)
 
+EXTRA_RUNS += run-misa-ialign
+run-misa-ialign: QEMU_OPTS := -cpu rv64,c=true,v=true,x-misa-w=on $(QEMU_OPTS)
+run-misa-ialign: misa-ialign
+	$(call run-test, $<, $(QEMU) $(QEMU_OPTS)$<)
+
 # We don't currently support the multiarch system tests
 undefine MULTIARCH_TESTS
tests/tcg/riscv64/misa-ialign.S new file +88 -0

88 lines not on this page · raw message

Signature · 1 line
2.51.0
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] target/riscv: Fix IALIGN check in misa write

Quoted 12 lines · Nicholas Piggin

The instruction alignment check for the C extension was inverted. The new value should be checked for C bit clear (thus increasing IALIGN). If IALIGN is incompatible, then the write to misa should be suppressed, not just ignoring the update to the C bit.

From the ISA:

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.

Quoted 5 lines

This was found with a verification test generator based on RiESCUE.

Fixes f18637cd61 ("RISC-V: Add misa runtime write support")
Quoted 1 line

---

Quoted 142 lines
  target/riscv/csr.c                        | 16 ++++-
  tests/tcg/riscv64/Makefile.softmmu-target |  5 ++
  tests/tcg/riscv64/misa-ialign.S           | 88 +++++++++++++++++++++++
  3 files changed, 106 insertions(+), 3 deletions(-)

create mode 100644 tests/tcg/riscv64/misa-ialign.S

target/riscv/csr.c +13 -3
index 8842e07a73..64b55b7add 100644
@@ -2140,9 +2140,19 @@ 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;
+    /*
+     * misa writes that increase IALIGN beyond alignment of the next
+     * instruction cause the write to misa to be suppressed. Clearing
+     * "C" extension increases IALIGN.
+     */
+    if (!(val & RVC) && (get_next_pc(env, ra) & 3) != 0) {
+        /*
+         * If the next instruction is unaligned mod 4 then "C" must be
+         * set or this instruction could not be executing, so we know
+         * this is is clearing "C" (and not just keeping it clear).
+         */
+        g_assert(env->misa_ext & RVC);
+        return RISCV_EXCP_NONE;
      }
 
      /* Disable RVG if any of its dependencies are disabled */
tests/tcg/riscv64/Makefile.softmmu-target +5 -0
index 3ca595335d..6e470a028f 100644
@@ -24,5 +24,10 @@ EXTRA_RUNS += run-test-mepc-masking
  run-test-mepc-masking: test-mepc-masking
  	$(call run-test, $<, $(QEMU) $(QEMU_OPTS)$<)
 
+EXTRA_RUNS += run-misa-ialign
+run-misa-ialign: QEMU_OPTS := -cpu rv64,c=true,v=true,x-misa-w=on $(QEMU_OPTS)
+run-misa-ialign: misa-ialign
+	$(call run-test, $<, $(QEMU) $(QEMU_OPTS)$<)
+
  # We don't currently support the multiarch system tests
  undefine MULTIARCH_TESTS
tests/tcg/riscv64/misa-ialign.S new file +88 -0
new file mode 100644
index 0000000000..7f1eb30023
@@ -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 set in 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
+ */
+
+#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 was cleared)
+
+	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
#3 lore raw

PATCH 2/3 target/risvc: Fix vector whole ldst vstart check

In reply to Nicholas Piggin

The whole vector ldst instructions do not include a vstart check, so an overflowed vstart can result in an underflowed memory address offset and crash:

accel/tcg/cputlb.c:1465:probe_access_flags:
assertion failed: (-(addr | TARGET_PAGE_MASK) >= size)

Add the VSTART_CHECK_EARLY_EXIT() check for these helpers.

This was found with a verification test generator based on RiESCUE.

---
 target/riscv/vector_helper.c             |  2 +
 tests/tcg/riscv64/Makefile.target        |  5 ++
 tests/tcg/riscv64/test-vstart-overflow.c | 75 ++++++++++++++++++++++++
 3 files changed, 82 insertions(+)
 create mode 100644 tests/tcg/riscv64/test-vstart-overflow.c
target/riscv/vector_helper.c +2 -0
index fc85a34a84..e0e8735000 100644
@@ -825,6 +825,8 @@ vext_ldst_whole(void *vd, target_ulong base, CPURISCVState *env, uint32_t desc,
     uint32_t esz = 1 << log2_esz;
     int mmu_index = riscv_env_mmu_index(env, false);
 
+    VSTART_CHECK_EARLY_EXIT(env, evl);
+
     /* Calculate the page range of first page */
     addr = base + (env->vstart << log2_esz);
     page_split = -(addr | TARGET_PAGE_MASK);
tests/tcg/riscv64/Makefile.target +5 -0
index 4da5b9a3b3..19a49b6467 100644
@@ -18,3 +18,8 @@ TESTS += test-fcvtmod
 test-fcvtmod: CFLAGS += -march=rv64imafdc
 test-fcvtmod: LDFLAGS += -static
 run-test-fcvtmod: QEMU_OPTS += -cpu rv64,d=true,zfa=true
+
+# Test for vstart >= vl
+TESTS += test-vstart-overflow
+test-vstart-overflow: CFLAGS += -march=rv64gcv
+run-test-vstart-overflow: QEMU_OPTS += -cpu rv64,v=on
tests/tcg/riscv64/test-vstart-overflow.c new file +75 -0

75 lines not on this page · raw message

Signature · 1 line
2.51.0
#4 lore raw

Re: [PATCH 2/3] target/risvc: Fix vector whole ldst vstart check

Hi Nick,

^ typo in the patch subject: s/risvc/riscv

Quoted 56 lines · Nicholas Piggin

The whole vector ldst instructions do not include a vstart check, so an overflowed vstart can result in an underflowed memory address offset and crash:

accel/tcg/cputlb.c:1465:probe_access_flags:
assertion failed: (-(addr | TARGET_PAGE_MASK) >= size)

Add the VSTART_CHECK_EARLY_EXIT() check for these helpers.

This was found with a verification test generator based on RiESCUE.

---
  target/riscv/vector_helper.c             |  2 +
  tests/tcg/riscv64/Makefile.target        |  5 ++
  tests/tcg/riscv64/test-vstart-overflow.c | 75 ++++++++++++++++++++++++
  3 files changed, 82 insertions(+)

create mode 100644 tests/tcg/riscv64/test-vstart-overflow.c

target/riscv/vector_helper.c +2 -0
index fc85a34a84..e0e8735000 100644
@@ -825,6 +825,8 @@ vext_ldst_whole(void *vd, target_ulong base, CPURISCVState *env, uint32_t desc,
      uint32_t esz = 1 << log2_esz;
      int mmu_index = riscv_env_mmu_index(env, false);
 
+    VSTART_CHECK_EARLY_EXIT(env, evl);
+
      /* Calculate the page range of first page */
      addr = base + (env->vstart << log2_esz);
      page_split = -(addr | TARGET_PAGE_MASK);
tests/tcg/riscv64/Makefile.target +5 -0
index 4da5b9a3b3..19a49b6467 100644
@@ -18,3 +18,8 @@ TESTS += test-fcvtmod
  test-fcvtmod: CFLAGS += -march=rv64imafdc
  test-fcvtmod: LDFLAGS += -static
  run-test-fcvtmod: QEMU_OPTS += -cpu rv64,d=true,zfa=true
+
+# Test for vstart >= vl
+TESTS += test-vstart-overflow
+test-vstart-overflow: CFLAGS += -march=rv64gcv
+run-test-vstart-overflow: QEMU_OPTS += -cpu rv64,v=on
tests/tcg/riscv64/test-vstart-overflow.c new file +3 -0
new file mode 100644
index 0000000000..72999f2c8a
@@ -0,0 +1,75 @@
+/*
+ * Test for VSTART set to overflow VL
+ *

tests/tcg/riscv64/test-vstart-overflow.c line 13

Quoted 10 lines
tests/tcg/riscv64/test-vstart-overflow.c new file +10 -0
+ * TCG vector instructions should call VSTART_CHECK_EARLY_EXIT() to check
+ * this case, otherwise memory addresses can underflow and misbehave or
+ * crash QEMU.
+ *
+ * TODO: Add stores and other instructions.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#include <stdint.h>
+#include <riscv_vector.h>

The fix in vector_helper.c is fine but this patch (and patch 3) won't execute 'make check-tcg'. It complains about this header being missing in the docker env.

To eliminate the possibility of my env being the problem I ran this series in Gitlab. Same error:

https://gitlab.com/danielhb/qemu/-/jobs/11236091281

/builds/danielhb/qemu/tests/tcg/riscv64/test-vstart-overflow.c:13:10: fatal error: riscv_vector.h: No such file or directory 3899
13 | #include <riscv_vector.h>
3900

      |          ^~~~~~~~~~~~~~~~

3901
compilation terminated.
3902
make[1]: *** [Makefile:122: test-vstart-overflow] Error 1

I believe you need to add the Docker changes you made in this patch. Same thing for patch 3. And same thing for patch 4 of:

[PATCH 0/4] linux-user/riscv: add vector state to signal

Given that you're also using riscv_vector.h in there too. Thanks,

Daniel

Quoted 62 lines
tests/tcg/riscv64/test-vstart-overflow.c new file +62 -0
+
+#define VSTART_OVERFLOW_TEST(insn)                \
+({                                                \
+    uint8_t vmem[64] = { 0 };                     \
+    uint64_t vstart;                              \
+    asm volatile("                           \r\n \
+    # Set VL=52 and VSTART=56                \r\n \
+    li          t0, 52                       \r\n \
+    vsetvli     x0, t0, e8, m4, ta, ma       \r\n \
+    li          t0, 56                       \r\n \
+    csrrw       x0, vstart, t0               \r\n \
+    li          t1, 64                       \r\n \
+    " insn "                                 \r\n \
+    csrr        %0, vstart                   \r\n \
+    " : "=r"(vstart), "+A"(vmem) :: "t0", "t1", "v24", "memory"); \
+    vstart;                                       \
+})
+
+int run_vstart_overflow_tests()
+{
+    /*
+     * An implementation is permitted to raise an illegal instruction
+     * exception when executing a vector instruction if vstart is set to a
+     * value that could not be produced by the execution of that instruction
+     * with the same vtype. If TCG is changed to do this, then this test
+     * could be updated to handle the SIGILL.
+     */
+    if (VSTART_OVERFLOW_TEST("vl1re16.v    v24, %1")) {
+        return 1;
+    }
+
+    if (VSTART_OVERFLOW_TEST("vs1r.v       v24, %1")) {
+        return 1;
+    }
+
+    if (VSTART_OVERFLOW_TEST("vle16.v      v24, %1")) {
+        return 1;
+    }
+
+    if (VSTART_OVERFLOW_TEST("vse16.v      v24, %1")) {
+        return 1;
+    }
+
+    if (VSTART_OVERFLOW_TEST("vluxei8.v    v24, %1, v20")) {
+        return 1;
+    }
+
+    if (VSTART_OVERFLOW_TEST("vlse16.v     v24, %1, t1")) {
+        return 1;
+    }
+
+    if (VSTART_OVERFLOW_TEST("vlseg2e8.v  v24, %1")) {
+        return 1;
+    }
+
+    return 0;
+}
+
+int main()
+{
+    return run_vstart_overflow_tests();
+}
#5 lore raw

Re: [PATCH 2/3] target/risvc: Fix vector whole ldst vstart check

Quoted 3 lines · Daniel Henrique Barboza

Hi Nick,

^ typo in the patch subject: s/risvc/riscv

Well I'm off to a fine start :/

Quoted 89 lines · Nicholas Piggin
Quoted 55 lines

The whole vector ldst instructions do not include a vstart check, so an overflowed vstart can result in an underflowed memory address offset and crash:

accel/tcg/cputlb.c:1465:probe_access_flags:
assertion failed: (-(addr | TARGET_PAGE_MASK) >= size)

Add the VSTART_CHECK_EARLY_EXIT() check for these helpers.

This was found with a verification test generator based on RiESCUE.

---
  target/riscv/vector_helper.c             |  2 +
  tests/tcg/riscv64/Makefile.target        |  5 ++
  tests/tcg/riscv64/test-vstart-overflow.c | 75 ++++++++++++++++++++++++
  3 files changed, 82 insertions(+)

create mode 100644 tests/tcg/riscv64/test-vstart-overflow.c

target/riscv/vector_helper.c +2 -0
index fc85a34a84..e0e8735000 100644
@@ -825,6 +825,8 @@ vext_ldst_whole(void *vd, target_ulong base, CPURISCVState *env, uint32_t desc,
      uint32_t esz = 1 << log2_esz;
      int mmu_index = riscv_env_mmu_index(env, false);
+    VSTART_CHECK_EARLY_EXIT(env, evl);
+
      /* Calculate the page range of first page */
      addr = base + (env->vstart << log2_esz);
      page_split = -(addr | TARGET_PAGE_MASK);
tests/tcg/riscv64/Makefile.target +5 -0
index 4da5b9a3b3..19a49b6467 100644
@@ -18,3 +18,8 @@ TESTS += test-fcvtmod
  test-fcvtmod: CFLAGS += -march=rv64imafdc
  test-fcvtmod: LDFLAGS += -static
  run-test-fcvtmod: QEMU_OPTS += -cpu rv64,d=true,zfa=true
+
+# Test for vstart >= vl
+TESTS += test-vstart-overflow
+test-vstart-overflow: CFLAGS += -march=rv64gcv
+run-test-vstart-overflow: QEMU_OPTS += -cpu rv64,v=on
tests/tcg/riscv64/test-vstart-overflow.c new file +3 -0
new file mode 100644
index 0000000000..72999f2c8a
@@ -0,0 +1,75 @@
+/*
+ * Test for VSTART set to overflow VL
+ *

tests/tcg/riscv64/test-vstart-overflow.c line 13

Quoted 10 lines
tests/tcg/riscv64/test-vstart-overflow.c new file +10 -0
+ * TCG vector instructions should call VSTART_CHECK_EARLY_EXIT() to check
+ * this case, otherwise memory addresses can underflow and misbehave or
+ * crash QEMU.
+ *
+ * TODO: Add stores and other instructions.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#include <stdint.h>
+#include <riscv_vector.h>

The fix in vector_helper.c is fine but this patch (and patch 3) won't execute 'make check-tcg'. It complains about this header being missing in the docker env.

To eliminate the possibility of my env being the problem I ran this series in Gitlab. Same error:

https://gitlab.com/danielhb/qemu/-/jobs/11236091281

/builds/danielhb/qemu/tests/tcg/riscv64/test-vstart-overflow.c:13:10: fatal error: riscv_vector.h: No such file or directory 3899
13 | #include <riscv_vector.h>
3900

      |          ^~~~~~~~~~~~~~~~

3901
compilation terminated.
3902
make[1]: *** [Makefile:122: test-vstart-overflow] Error 1

Quoted 6 lines

I believe you need to add the Docker changes you made in this patch. Same thing for patch 3. And same thing for patch 4 of:

[PATCH 0/4] linux-user/riscv: add vector state to signal

Given that you're also using riscv_vector.h in there too. Thanks,

Hmm, thanks. It did work for my local build.

I think the header is provided by the compiler, so I might have to work out a way to skip the test if the compiler is too old. GCC13 might have been the first one to support.

I was considering writing .S files for these. Should have done so if I realized, but nevermind.

Thanks,
Nick

#6 lore raw

Re: [PATCH 2/3] target/risvc: Fix vector whole ldst vstart check

Alex, Richard,

Quoted 101 lines · Nicholas Piggin
Quoted 3 lines · Daniel Henrique Barboza

Hi Nick,

^ typo in the patch subject: s/risvc/riscv

Well I'm off to a fine start :/

Quoted 94 lines · Nicholas Piggin
Quoted 55 lines

The whole vector ldst instructions do not include a vstart check, so an overflowed vstart can result in an underflowed memory address offset and crash:

accel/tcg/cputlb.c:1465:probe_access_flags:
assertion failed: (-(addr | TARGET_PAGE_MASK) >= size)

Add the VSTART_CHECK_EARLY_EXIT() check for these helpers.

This was found with a verification test generator based on RiESCUE.

---
   target/riscv/vector_helper.c             |  2 +
   tests/tcg/riscv64/Makefile.target        |  5 ++
   tests/tcg/riscv64/test-vstart-overflow.c | 75 ++++++++++++++++++++++++
   3 files changed, 82 insertions(+)

create mode 100644 tests/tcg/riscv64/test-vstart-overflow.c

target/riscv/vector_helper.c +2 -0
index fc85a34a84..e0e8735000 100644
@@ -825,6 +825,8 @@ vext_ldst_whole(void *vd, target_ulong base, CPURISCVState *env, uint32_t desc,
       uint32_t esz = 1 << log2_esz;
       int mmu_index = riscv_env_mmu_index(env, false);
+    VSTART_CHECK_EARLY_EXIT(env, evl);
+
       /* Calculate the page range of first page */
       addr = base + (env->vstart << log2_esz);
       page_split = -(addr | TARGET_PAGE_MASK);
tests/tcg/riscv64/Makefile.target +5 -0
index 4da5b9a3b3..19a49b6467 100644
@@ -18,3 +18,8 @@ TESTS += test-fcvtmod
   test-fcvtmod: CFLAGS += -march=rv64imafdc
   test-fcvtmod: LDFLAGS += -static
   run-test-fcvtmod: QEMU_OPTS += -cpu rv64,d=true,zfa=true
+
+# Test for vstart >= vl
+TESTS += test-vstart-overflow
+test-vstart-overflow: CFLAGS += -march=rv64gcv
+run-test-vstart-overflow: QEMU_OPTS += -cpu rv64,v=on
tests/tcg/riscv64/test-vstart-overflow.c new file +3 -0
new file mode 100644
index 0000000000..72999f2c8a
@@ -0,0 +1,75 @@
+/*
+ * Test for VSTART set to overflow VL
+ *

tests/tcg/riscv64/test-vstart-overflow.c line 13

Quoted 10 lines
tests/tcg/riscv64/test-vstart-overflow.c new file +10 -0
+ * TCG vector instructions should call VSTART_CHECK_EARLY_EXIT() to check
+ * this case, otherwise memory addresses can underflow and misbehave or
+ * crash QEMU.
+ *
+ * TODO: Add stores and other instructions.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#include <stdint.h>
+#include <riscv_vector.h>

The fix in vector_helper.c is fine but this patch (and patch 3) won't execute 'make check-tcg'. It complains about this header being missing in the docker env.

To eliminate the possibility of my env being the problem I ran this series in Gitlab. Same error:

https://gitlab.com/danielhb/qemu/-/jobs/11236091281

/builds/danielhb/qemu/tests/tcg/riscv64/test-vstart-overflow.c:13:10: fatal error: riscv_vector.h: No such file or directory 3899

    13 | #include <riscv_vector.h>
3900
       |          ^~~~~~~~~~~~~~~~

3901
compilation terminated.
3902
make[1]: *** [Makefile:122: test-vstart-overflow] Error 1

I believe you need to add the Docker changes you made in this patch. Same thing for patch 3. And same thing for patch 4 of:

[PATCH 0/4] linux-user/riscv: add vector state to signal

Quoted 7 lines
Quoted 1 line

Given that you're also using riscv_vector.h in there too. Thanks,

Hmm, thanks. It did work for my local build.

I think the header is provided by the compiler, so I might have to work out a way to skip the test if the compiler is too old. GCC13 might have been the first one to support.

How hard it is to update the GCC version we're running in the docker images for "check-tcg"? We would like to use a RISC-V vector header that isn't supported ATM.

Thanks,

Daniel

Quoted 6 lines

I was considering writing .S files for these. Should have done so if I realized, but nevermind.

Thanks,
Nick

#7 lore raw

Re: [PATCH 2/3] target/risvc: Fix vector whole ldst vstart check

Quoted 3 lines · Daniel Henrique Barboza

How hard it is to update the GCC version we're running in the docker images for "check-tcg"? We would like to use a RISC-V vector header that isn't supported ATM.

If debian packages the gcc version, then it's easy: change

gcc-riscv-linux-gnu

to

gcc-NN-riscv-linux-gnu

If the version isn't packaged, then it's harder, and we would need to either build our own gcc within the container (see dockerfiles/debian-microblaze-cross.d/build-toolchain.sh), or you can host a pre-built version somewhere (see dockerfiles/debian-loongarch-cross.docker).

r~

#8 lore raw

Re: [PATCH 2/3] target/risvc: Fix vector whole ldst vstart check

Quoted 5 lines · Richard Henderson
Quoted 3 lines · Daniel Henrique Barboza

How hard it is to update the GCC version we're running in the docker images for "check-tcg"? We would like to use a RISC-V vector header that isn't supported ATM.

Quoted 7 lines

If debian packages the gcc version, then it's easy: change

gcc-riscv-linux-gnu

to

gcc-NN-riscv-linux-gnu

The test that was failing uses debian-all-test-cross. This is based on Debian 12 which maxes out at GCC 12.

If we move to Debian 13, we get GCC 14. Something like this would do it:

tests/docker/dockerfiles/debian-all-test-cross.docker +1 -1
@@ -6,7 +6,7 @@
 # basic compilers for as many targets as possible. We shall use this
 # to build and run linux-user tests on GitLab
 #
-FROM docker.io/library/debian:12-slim
+FROM docker.io/library/debian:13-slim
 

Is updating the distro something we would consider for this development cycle?

Cheers,

Joel

#9 lore raw

Re: [PATCH 2/3] target/risvc: Fix vector whole ldst vstart check

Quoted 11 lines · Joel Stanley
Quoted 9 lines · Richard Henderson
Quoted 3 lines · Daniel Henrique Barboza

How hard it is to update the GCC version we're running in the docker images for "check-tcg"? We would like to use a RISC-V vector header that isn't supported ATM.

If debian packages the gcc version, then it's easy: change

gcc-riscv-linux-gnu

Quoted 8 lines
Quoted 3 lines

to

gcc-NN-riscv-linux-gnu

The test that was failing uses debian-all-test-cross. This is based on Debian 12 which maxes out at GCC 12.

If we move to Debian 13, we get GCC 14. Something like this would do it:

Debian 12 is a supported platform for QEMU, so we should generally expect our tests to work on that. That said IIUC riscv didn't become officially supported in Debian until 13, so we can use that as a justification for an exception to the normal platform rule.

Quoted 11 lines
tests/docker/dockerfiles/debian-all-test-cross.docker +1 -1
@@ -6,7 +6,7 @@
 # basic compilers for as many targets as possible. We shall use this
 # to build and run linux-user tests on GitLab
 #
-FROM docker.io/library/debian:12-slim
+FROM docker.io/library/debian:13-slim
 

Is updating the distro something we would consider for this development cycle?

Quoted 4 lines

Cheers,

Joel

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#10 lore raw

PATCH 3/3 tests/tcg: Add riscv test for interrupted vector ops

In reply to Nicholas Piggin

riscv vector instructions can be interrupted with a trap, and partial completion is recorded in the vstart register. Some causes are implementation dependent, for example an asynchronous interrupt (which I don't think TCG allows). Others are architectural, typically memory access faults on vector load/store instructions.

Add some TCG tests for interrupting vector load instructions and resuming partially completed ones.

This would have caught a recent (now reverted) regression in vector stride load implementation, commit 28c12c1f2f50d ("Generate strided vector loads/stores with tcg nodes.")

---
 tests/tcg/riscv64/Makefile.target      |   5 +
 tests/tcg/riscv64/test-interrupted-v.c | 208 +++++++++++++++++++++++++
 2 files changed, 213 insertions(+)
 create mode 100644 tests/tcg/riscv64/test-interrupted-v.c
tests/tcg/riscv64/Makefile.target +5 -0
index 19a49b6467..8f4690ac57 100644
@@ -23,3 +23,8 @@ run-test-fcvtmod: QEMU_OPTS += -cpu rv64,d=true,zfa=true
 TESTS += test-vstart-overflow
 test-vstart-overflow: CFLAGS += -march=rv64gcv
 run-test-vstart-overflow: QEMU_OPTS += -cpu rv64,v=on
+
+# Test for interrupted vector instructions
+TESTS += test-interrupted-v
+test-interrupted-v: CFLAGS += -march=rv64gcv
+run-test-interrupted-v: QEMU_OPTS += -cpu rv64,v=on
tests/tcg/riscv64/test-interrupted-v.c new file +208 -0

208 lines not on this page · raw message

Signature · 1 line
2.51.0