Skip to contentSkip to conversation

target/riscv: Fix seed CSR sign extension

PATCH Applied Ivan Efremov 5 participants 10 messages Aug 2 – Aug 28 RISC-V

lore
Share & tools
lore
#0 lore raw

cover target/riscv: Fix seed CSR sign extension

The seed CSR value on RV64 was sign extended while converting to target_ulong.

The first patch makes SEED_OPST_* unsigned.
The second patch adds qtest to check that upper 32 bits are zero.

Tests ran:
- riscv-csr-test
- make check: 324 passed, 11 skipped
- RISC-V Zkr-00 architectural test

Ivan Efremov (2):
target/riscv: Fix seed CSR sign extension
tests/qtest: Add seed CSR zero extension test

 target/riscv/cpu_bits.h      | 10 +++++-----
 tests/qtest/riscv-csr-test.c | 26 ++++++++++++++++++++++++++
 2 files changed, 31 insertions(+), 5 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/2 target/riscv: Fix seed CSR sign extension

The SEED_OPST_* macros expanded to int expressions. When bit 31 was present, converting a CSR seed value to target_ulong on RV64 sign extended the value and incorrectly set the upper 32 bits.

Make the SEED_OPST_* constants unsigned so that CSR values are zero extended on RV64.

Fixes 77442380ecbe ("target/riscv: rvk: add CSR support for Zkr")

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4121

---
 target/riscv/cpu_bits.h | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
target/riscv/cpu_bits.h +5 -5
index 3f146a43fe..c01050ce2b 100644
@@ -1086,11 +1086,11 @@ typedef enum CTRType {
     (HVICTL_VTI | HVICTL_IID | HVICTL_IPRIOM | HVICTL_IPRIO)
 
 /* seed CSR bits */
-#define SEED_OPST                        (0b11 << 30)
-#define SEED_OPST_BIST                   (0b00 << 30)
-#define SEED_OPST_WAIT                   (0b01 << 30)
-#define SEED_OPST_ES16                   (0b10 << 30)
-#define SEED_OPST_DEAD                   (0b11 << 30)
+#define SEED_OPST                        (0b11U << 30)
+#define SEED_OPST_BIST                   (0b00U << 30)
+#define SEED_OPST_WAIT                   (0b01U << 30)
+#define SEED_OPST_ES16                   (0b10U << 30)
+#define SEED_OPST_DEAD                   (0b11U << 30)
 /* PMU related bits */
 #define MIE_LCOFIE                         (1 << IRQ_PMU_OVF)
 
Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/2 tests/qtest: Add seed CSR zero extension test

In reply to Ivan Efremov

Add a qtest that reads the seed CSR on RV64 machine with Zkr support and verifies that the upper 32 bits are clear.

---
 tests/qtest/riscv-csr-test.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
tests/qtest/riscv-csr-test.c +26 -0
index bb1b0ffed3..7479a5c5bc 100644
@@ -20,6 +20,12 @@
 #define CSR_MVENDORID       0xf11
 #define CSR_MISELECT        0x350
 
+#define CSR_SEED           0x015
+
+#define SEED_OPST_MASK     (UINT64_C(0x3) << 30)
+#define SEED_OPST_ES16     (UINT64_C(0x2) << 30)
+#define SEED_OPST_DEAD     (UINT64_C(0x3) << 30)
+
 static void run_test_csr(void)
 {
     uint64_t res;
@@ -46,12 +52,32 @@ static void run_test_csr(void)
     qtest_quit(qts);
 }
 
+static void run_test_seed_csr(void)
+{
+    uint64_t val = 0;
+    uint64_t opst;
+    QTestState *qts;
+
+    qts = qtest_init("-machine virt -cpu tt-ascalon");
+
+    qtest_csr_call(qts, "get_csr", 0, CSR_SEED, &val);
+
+    opst = val & SEED_OPST_MASK;
+    g_assert_true(opst == SEED_OPST_ES16 ||
+                  opst == SEED_OPST_DEAD);
+
+    g_assert_cmphex(val >> 32, ==, 0);
+
+    qtest_quit(qts);
+}
+
 int main(int argc, char **argv)
 {
     g_test_init(&argc, &argv, NULL);
 
     if (qtest_has_machine("virt")) {
         qtest_add_func("/cpu/csr", run_test_csr);
+        qtest_add_func("/cpu/csr/seed", run_test_seed_csr);
     }
 
     return g_test_run();
Signature · 1 line
2.43.0
Reviewed-by Alistair Francis on the v1 series
#3 lore raw
In reply to Ivan Efremov
Quoted 10 lines · Ivan Efremov

The seed CSR value on RV64 was sign extended while converting to target_ulong.

The first patch makes SEED_OPST_* unsigned.
The second patch adds qtest to check that upper 32 bits are zero.

Tests ran:
- riscv-csr-test
- make check: 324 passed, 11 skipped
- RISC-V Zkr-00 architectural test

Alistair

Quoted 8 lines

Ivan Efremov (2):
  target/riscv: Fix seed CSR sign extension
  tests/qtest: Add seed CSR zero extension test

 target/riscv/cpu_bits.h      | 10 +++++----- tests/qtest/riscv-csr-test.c | 26 ++++++++++++++++++++++++++

 2 files changed, 31 insertions(+), 5 deletions(-)
Applied
#4 lore raw
In reply to Ivan Efremov
Quoted 10 lines · Ivan Efremov

The seed CSR value on RV64 was sign extended while converting to target_ulong.

The first patch makes SEED_OPST_* unsigned.
The second patch adds qtest to check that upper 32 bits are zero.

Tests ran:
- riscv-csr-test
- make check: 324 passed, 11 skipped
- RISC-V Zkr-00 architectural test

Thanks!

Applied to riscv-to-apply.next

Alistair

Quoted 8 lines

Ivan Efremov (2):
  target/riscv: Fix seed CSR sign extension
  tests/qtest: Add seed CSR zero extension test

 target/riscv/cpu_bits.h      | 10 +++++----- tests/qtest/riscv-csr-test.c | 26 ++++++++++++++++++++++++++

 2 files changed, 31 insertions(+), 5 deletions(-)
#5 lore raw
In reply to Ivan Efremov
Quoted 5 lines · Ivan Efremov

The seed CSR value on RV64 was sign extended while converting to target_ulong.

The first patch makes SEED_OPST_* unsigned.
The second patch adds qtest to check that upper 32 bits are zero.

Quoted 8 lines

Tests ran:
- riscv-csr-test
- make check: 324 passed, 11 skipped
- RISC-V Zkr-00 architectural test

Ivan Efremov (2):
target/riscv: Fix seed CSR sign extension
tests/qtest: Add seed CSR zero extension test

I'm picking this patch set for the currently active qemu stable series (10.0 LTS, 11.0, 11.1). However, it does not work on 11.0.x already (it works ok on 11.1.x), the test fails:

TAP version 14

# random seed: R02Sac6635c76a7dc335fadd70845da72f99
1..2
# Start of riscv64 tests
# Start of cpu tests
# starting QEMU: exec ./qemu-system-riscv64 -qtest unix:/tmp/qtest-182934.sock -qtest-log /dev/null -chardev socket,path=/tmp/qtest-182934.qmp,id=char0 -mon chardev=char0,mode=control -display none -audio none -run-with exit-with-parent=on -machine virt -cpu veyron-v1 -accel qtest
ok 1 /riscv64/cpu/csr
# Start of csr tests
# starting QEMU: exec ./qemu-system-riscv64 -qtest unix:/tmp/qtest-182934.sock -qtest-log /dev/null -chardev socket,path=/tmp/qtest-182934.qmp,id=char0 -mon chardev=char0,mode=control -display none -audio none -run-with exit-with-parent=on -machine virt -cpu tt-ascalon -accel qtest
**
ERROR:../../build/qemu/11.0/hw/riscv/riscv_hart.c:78:csr_call: assertion failed: (ret == RISCV_EXCP_NONE)
Bail out! ERROR:../../build/qemu/11.0/hw/riscv/riscv_hart.c:78:csr_call: assertion failed: (ret == RISCV_EXCP_NONE)
Broken pipe
../../build/qemu/11.0/tests/qtest/libqtest.c:210: kill_qemu() detected QEMU death from signal 6 (Aborted)
Aborted

Should I pick something else for 11.0 and 10.0 (on 10.0 it fails the same way), so the test succeeds, or should I drop this patch set from 11.1.x?

Thanks,

/mjt

#6 lore raw
Quoted 35 lines · Michael Tokarev
Quoted 5 lines · Ivan Efremov

The seed CSR value on RV64 was sign extended while converting to target_ulong.

The first patch makes SEED_OPST_* unsigned.
The second patch adds qtest to check that upper 32 bits are zero.

Quoted 8 lines

Tests ran:
- riscv-csr-test
- make check: 324 passed, 11 skipped
- RISC-V Zkr-00 architectural test

Ivan Efremov (2):
target/riscv: Fix seed CSR sign extension
tests/qtest: Add seed CSR zero extension test

I'm picking this patch set for the currently active qemu stable series (10.0 LTS, 11.0, 11.1). However, it does not work on 11.0.x already (it works ok on 11.1.x), the test fails:

TAP version 14

# random seed: R02Sac6635c76a7dc335fadd70845da72f99
1..2
# Start of riscv64 tests
# Start of cpu tests
# starting QEMU: exec ./qemu-system-riscv64 -qtest unix:/tmp/qtest-182934.sock -qtest-log /dev/null -chardev socket,path=/tmp/qtest-182934.qmp,id=char0 -mon chardev=char0,mode=control -display none -audio none -run-with exit-with-parent=on -machine virt -cpu veyron-v1 -accel qtest
ok 1 /riscv64/cpu/csr
# Start of csr tests
# starting QEMU: exec ./qemu-system-riscv64 -qtest unix:/tmp/qtest-182934.sock -qtest-log /dev/null -chardev socket,path=/tmp/qtest-182934.qmp,id=char0 -mon chardev=char0,mode=control -display none -audio none -run-with exit-with-parent=on -machine virt -cpu tt-ascalon -accel qtest
**
ERROR:../../build/qemu/11.0/hw/riscv/riscv_hart.c:78:csr_call: assertion failed: (ret == RISCV_EXCP_NONE)
Bail out! ERROR:../../build/qemu/11.0/hw/riscv/riscv_hart.c:78:csr_call: assertion failed: (ret == RISCV_EXCP_NONE)
Broken pipe
../../build/qemu/11.0/tests/qtest/libqtest.c:210: kill_qemu() detected QEMU death from signal 6 (Aborted)
Aborted
Quoted 6 lines

Should I pick something else for 11.0 and 10.0 (on 10.0 it fails the same way), so the test succeeds, or should I drop this patch set from 11.1.x?

Thanks,

/mjt

Thank you for testing. From my runs it seems that 10.0, 11.0 lack commit 569aa628 (target/riscv: tt-ascalon: Enable Zkr extension), which enables Zkr for tt-ascalon, so reading the seed triggers an assertion in csr_call. Apologies if Im missing something here. Im not sure which is better for stable: backporting 569aa628, or skipping the test patch.

Best regards,
Ivan Efremov

#7 lore raw
Quoted 8 lines · On Thu, 27 Aug 2026 at 07:57, <[email protected]> wrote:
Quoted 8 lines

I'm picking this patch set for the currently active qemu stable series (10.0 LTS, 11.0, 11.1). However, it does not work on 11.0.x already (it works ok on 11.1.x), the test fails:

Should I pick something else for 11.0 and 10.0 (on 10.0 it fails the same way),
so the test succeeds, or should I drop this patch set from 11.1.x?

Quoted 8 lines
Quoted 3 lines

Thanks,

/mjt

Thank you for testing. From my runs it seems that 10.0, 11.0 lack commit 569aa628 (target/riscv: tt-ascalon: Enable Zkr extension), which enables Zkr for tt-ascalon, so reading the seed triggers an assertion in csr_call. Apologies if Im missing something here. Im not sure which is better for stable: backporting 569aa628, or skipping the test patch.

Backporting 569aa628 seems reasonable to me.

Cheers,

Joel

#8 lore raw
Quoted 9 lines · Joel Stanley
Quoted 7 lines · On Thu, 27 Aug 2026 at 07:57, <[email protected]> wrote:
Quoted 7 lines

I'm picking this patch set for the currently active qemu stable series (10.0 LTS, 11.0, 11.1). However, it does not work on 11.0.x already (it works ok on 11.1.x), the test fails:

Should I pick something else for 11.0 and 10.0 (on 10.0 it fails the same way),
so the test succeeds, or should I drop this patch set from 11.1.x?

Quoted 5 lines
Quoted 5 lines

Thank you for testing. From my runs it seems that 10.0, 11.0 lack commit 569aa628 (target/riscv: tt-ascalon: Enable Zkr extension), which enables Zkr for tt-ascalon, so reading the seed triggers an assertion in csr_call. Apologies if Im missing something here. Im not sure which is better for stable: backporting 569aa628, or skipping the test patch.

Aha. This is why I'm asking - I don't know much about riscv internals :)

Ivan, there's nothing to apologize about, you did nothing wrong in this context, - it is just me who does not have knowledge about all these riscv details, what does it all mean, and which parts are important or not.

Quoted 1 line

Backporting 569aa628 seems reasonable to me.

I agree it's best to pick up 569aa628 too, this is my initial thought when I was reading reply from Ivan.

However it has another issue: this commit works fine on 11.0.x, but 10.0.x pre-dates RISCVCPUDef conversion.

As far as I can see, 569aa628 is about TYPE_RISCV_CPU_TT_ASCALON, and hence it is 09ef7d97454 "target/riscv: convert TT Ascalon to RISCVCPUDef", so in 10.0.x it becomes:

target/riscv/cpu.c +1 -0
index bf4855ec02d..5e37cc3645d 100644
@@ -646,6 +646,7 @@ static void rv64_tt_ascalon_cpu_init(Object *obj)
     cpu->cfg.ext_zba = true;
     cpu->cfg.ext_zbb = true;
     cpu->cfg.ext_zbs = true;
+    cpu->cfg.ext_zkr = true;
     cpu->cfg.ext_zkt = true;
     cpu->cfg.ext_zvbb = true;
     cpu->cfg.ext_zvbc = true;

(the RISCVCPUDef conversion made this stuff a bit more difficult to see in a diff, since now the diff does not show which cpu type the change belongs to).

With that, it all should work, and I'm picking it up for the next stable series.

But overall, it looks like it all is a rather niche thing, and might not really belong to stable series at all (*). Since we already spent quite some time on this and resolved all issues, let's pick it up anyway :)

(*) I mean, all these small fixes together, which should be either picked up or not, but all together, - 569aa628, this patch I'm replying to, and the test for it.

Thanks,

/mjt

#9 lore raw
Quoted 52 lines · Michael Tokarev
Quoted 11 lines · Joel Stanley
Quoted 9 lines · On Thu, 27 Aug 2026 at 07:57, <[email protected]> wrote:
Quoted 9 lines

I'm picking this patch set for the currently active qemu stable series (10.0 LTS, 11.0, 11.1).  However, it does not work on 11.0.x already (it works ok on 11.1.x), the test fails:

Should I pick something else for 11.0 and 10.0 (on 10.0 it fails the
same way),
so the test succeeds, or should I drop this patch set from 11.1.x?

Quoted 9 lines
Quoted 9 lines

Thank you for testing. From my runs it seems that 10.0, 11.0 lack commit
569aa628 (target/riscv: tt-ascalon: Enable Zkr extension), which enables
Zkr for tt-ascalon, so reading the seed triggers an assertion in csr_call. Apologies if Im missing something here. Im not sure which is
better for stable: backporting 569aa628, or skipping the test patch.

Aha.  This is why I'm asking - I don't know much about riscv internals :)

Ivan, there's nothing to apologize about, you did nothing wrong in this
context, - it is just me who does not have knowledge about all these riscv
details, what does it all mean, and which parts are important or not.

Quoted 1 line

Backporting 569aa628 seems reasonable to me.

I agree it's best to pick up 569aa628 too, this is my initial thought when
I was reading reply from Ivan.

However it has another issue: this commit works fine on 11.0.x, but 10.0.x
pre-dates RISCVCPUDef conversion.

As far as I can see, 569aa628 is about TYPE_RISCV_CPU_TT_ASCALON, and hence
it is 09ef7d97454 "target/riscv: convert TT Ascalon to RISCVCPUDef", so in
10.0.x it becomes:

target/riscv/cpu.c +0 -0
index bf4855ec02d..5e37cc3645d 100644

target/riscv/cpu.c static void rv64_tt_ascalon_cpu_init(Object *obj) · line 652

Quoted 7 lines
target/riscv/cpu.c +1 -0
@@ -646,6 +646,7 @@ static void rv64_tt_ascalon_cpu_init(Object *obj)
       cpu->cfg.ext_zba = true;
       cpu->cfg.ext_zbb = true;
       cpu->cfg.ext_zbs = true;
+    cpu->cfg.ext_zkr = true;
       cpu->cfg.ext_zkt = true;
       cpu->cfg.ext_zvbb = true;
       cpu->cfg.ext_zvbc = true;

That looks correct

Quoted 7 lines

(the RISCVCPUDef conversion made this stuff a bit more difficult to see in a diff, since now the diff does not show which cpu type the change belongs to).

With that, it all should work, and I'm picking it up for the next stable series.

Thank you!

Alistair

Quoted 13 lines

But overall, it looks like it all is a rather niche thing, and might not really belong to stable series at all (*).  Since we already spent quite some time on this and resolved all issues, let's pick it up anyway :)

(*) I mean, all these small fixes together, which should be either picked up or not, but all together, - 569aa628, this patch I'm replying to, and the test for it.

Thanks,

/mjt