Priv spec says in the hvip section: "Bits VSEIP, VSTIP, and VSSIP of
hvip are writable.". There's no reservation about hvip.VSTIP being
writable, i.e. this should be the case at all times.
The issue lies in rmw_mip64(). The function, used by multiple CSRs, is
filtering out VSTIP based on the STCE bits being set.
Make an exception for hvip.VSTIP.
Fixes: 3cb2edae7401 ("target/riscv: Fix VSTIP bit in sstc extension.")
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4593
Signed-off-by: Daniel Henrique Barboza <[email protected]>
---
target/riscv/tcg/csr.c | 8 +++-
tests/tcg/riscv64/system/meson.build | 7 +++
tests/tcg/riscv64/test-hvip-vstip.S | 65 ++++++++++++++++++++++++++++
3 files changed, 78 insertions(+), 2 deletions(-)
create mode 100644 tests/tcg/riscv64/test-hvip-vstip.S
diff --git a/target/riscv/tcg/csr.c b/target/riscv/tcg/csr.c
index af444e3e6f..82e315c56f 100644
--- a/target/riscv/tcg/csr.c
+++ b/target/riscv/tcg/csr.c
@@ -4006,8 +4006,12 @@ static RISCVException rmw_mip64(CPURISCVState *env, int
csrno,
/* STIP is not writable when menvcfg.STCE is enabled. */
mask = mask & ~MIP_STIP;
- /* VSTIP is not writable when both [mh]envcfg.STCE are enabled. */
- if (get_field(env->henvcfg, HENVCFG_STCE)) {
+ /*
+ * VSTIP is not writable when both [mh]envcfg.STCE are enabled.
+ * Note: VSTIP is writable via CSR_HVIP at all times.
+ */
+ if (csrno != CSR_HVIP
+ && get_field(env->henvcfg, HENVCFG_STCE)) {
mask = mask & ~MIP_VSTIP;
}
}
diff --git a/tests/tcg/riscv64/system/meson.build
b/tests/tcg/riscv64/system/meson.build
index 3c8b018d9e..b73b09781a 100644
--- a/tests/tcg/riscv64/system/meson.build
+++ b/tests/tcg/riscv64/system/meson.build
@@ -89,6 +89,13 @@ tests += {
}
}
+tests += {
+ 'test-hvip-vstip.S': {
+ 'cflags': cflags,
+ 'qemu_args': ['-cpu', 'max', qemu_args],
+ }
+}
+
if 'qemu-system-riscv64' in emulators
tcg_tests += {
'riscv64-softmmu': {
diff --git a/tests/tcg/riscv64/test-hvip-vstip.S
b/tests/tcg/riscv64/test-hvip-vstip.S
new file mode 100644
index 0000000000..f89e3e32b5
--- /dev/null
+++ b/tests/tcg/riscv64/test-hvip-vstip.S
@@ -0,0 +1,65 @@
+/*
+ * Test if hvip.VSTIP is writable with menvcfg.STCE and
+ * henvcfg.STCE set. See Gitlab 4593 for more info.
+ *
+ * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+.option norvc
+ .text
+ .global _start
+
+_start:
+ lla t0, trap
+ csrw mtvec, t0
+
+ /* Enable menvcfg.STCE and henvcfg.STCE (bit 63). */
+ li t0, 1
+ slli t0, t0, 63
+ csrs menvcfg, t0
+ csrs henvcfg, t0
+
+ /*
+ * write hvip.VSTIP, read hvip again and verify that
+ * VSTIP was indeed written.
+ */
+ csrr t0, hvip
+ li t1, 1
+ slli t1, t1, 6 # bit 6: VSTIP
+ or t0, t0, t1
+ csrw hvip, t0
+
+ csrr t1, hvip
+ bne t0, t1, _fail
+
+ li a0, 0
+ j _exit
+
+_fail:
+ li a0, 1
+ 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
--
2.43.0