rmw_hvip64() is not filtering out the bits from mip/sip, hosted in the
'alias_mask' variable, when dealing with HVIP. The bug is rather
straightforward. Given:
uint64_t alias_mask = (env->hideleg | ~env->hvien) | VS_MODE_INTERRUPTS;
when env->hvien is 0, alias_mask will be a full 1s mask. This mask is
then used to retrieve the mip bits (via rmw_mip64) and then later when
filtering ret_mip. The end result is that HVIP ends up providing read
access to bits it can't write. The spec says:
"Bits of hvip that are not writable are read-only zeros."
So we're in a direct and trivial spec violation.
As a side rant: I am less than pleased with how rmw_hvip64() is turning
out. It's doing way too much with way too many CSRs (hvip, vsip, sip
...) and a bitmask salad to go along with it. Every time an issue
appears we have to patch up a specific piece of it, usually related to
one of its CSRs, and hope we're not breaking anything else. The
alternative is a full refactor of all the CSRs callbacks involved to
decouple the logic, which isn't the greatest time either. We should
definitely avoid this kind of design in the future.
Fixes: 40336d5b1d4c ("target/riscv: Add HS-mode virtual interrupt and IRQ
filtering support.")
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3133
Signed-off-by: Daniel Henrique Barboza <[email protected]>
---
target/riscv/tcg/csr.c | 8 ++++++
tests/tcg/riscv64/system/meson.build | 8 ++++++
tests/tcg/riscv64/test-hvip-read.S | 41 ++++++++++++++++++++++++++++
3 files changed, 57 insertions(+)
create mode 100644 tests/tcg/riscv64/test-hvip-read.S
diff --git a/target/riscv/tcg/csr.c b/target/riscv/tcg/csr.c
index af444e3e6f..9b67f2d764 100644
--- a/target/riscv/tcg/csr.c
+++ b/target/riscv/tcg/csr.c
@@ -5078,6 +5078,14 @@ static RISCVException rmw_hvip64(CPURISCVState *env, int
csrno,
uint64_t wr_mask_hvip;
uint64_t wr_mask_mip;
+ /*
+ * alias_mask, aka the bits that comes from sip/mip, should be capped
+ * to just VS_MODE_INTERRUPTS when reading/writing HVIP.
+ */
+ if (csrno == CSR_HVIP || csrno == CSR_HVIPH) {
+ alias_mask = VS_MODE_INTERRUPTS;
+ }
+
/*
* Both alias and non-alias mask remain same for vsip except:
* 1- For VS* bits if they are zero in hideleg.
diff --git a/tests/tcg/riscv64/system/meson.build
b/tests/tcg/riscv64/system/meson.build
index 3c8b018d9e..7eca59222c 100644
--- a/tests/tcg/riscv64/system/meson.build
+++ b/tests/tcg/riscv64/system/meson.build
@@ -89,6 +89,14 @@ tests += {
}
}
+tests += {
+ 'test-hvip-read.S': {
+ 'cflags': cflags + ['-march=rv64gcv'],
+ 'qemu_args': ['-cpu',
'max,smpmpmt=false,pmu-mask=0xfffffff8,smdbltrp=true,ssdbltrp=true', qemu_args],
+ }
+}
+
+
if 'qemu-system-riscv64' in emulators
tcg_tests += {
'riscv64-softmmu': {
diff --git a/tests/tcg/riscv64/test-hvip-read.S
b/tests/tcg/riscv64/test-hvip-read.S
new file mode 100644
index 0000000000..20a1492924
--- /dev/null
+++ b/tests/tcg/riscv64/test-hvip-read.S
@@ -0,0 +1,41 @@
+/*
+ * Test if a HVIP read is returning 0 for all bits it can't
+ * write. Based on Gitlab 3133 testcase.
+ *
+ * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+ .option norvc
+ .text
+
+ .global _start
+
+_start:
+ li t0, 2
+ csrs mip, t0
+ csrr t1, hvip
+ bnez t1, _fail
+
+ li a0, 0
+ j _exit
+
+_fail:
+ li a0, 1
+
+/* Exit through the semihosting SYS_EXIT_EXTENDED call with a0 as the code. */
+_exit:
+ la a1, semiargs
+ li t0, 0x20026 /* ADP_Stopped_ApplicationExit */
+ sd t0, 0(a1)
+ sd a0, 8(a1)
+ li a0, 0x20 /* TARGET_SYS_EXIT_EXTENDED */
+ .balign 16
+ slli zero, zero, 0x1f
+ ebreak
+ srai zero, zero, 0x7
+ j .
+
+ .data
+ .balign 8
+semiargs:
+ .space 16
--
2.43.0