Skip to contentSkip to conversation

target/riscv/tcg/csr: filter bits when r/w HVIP(H)

PATCH Superseded Daniel Henrique Barboza 1 participant 1 message Nov 11

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/riscv/tcg/csr: filter bits when r/w HVIP(H)

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

---
 target/riscv/tcg/csr.c               |  8 ++++++
 tests/tcg/riscv64/system/meson.build |  9 ++++++
 tests/tcg/riscv64/test-hvip-read.S   | 41 ++++++++++++++++++++++++++++
 3 files changed, 58 insertions(+)
 create mode 100644 tests/tcg/riscv64/test-hvip-read.S
target/riscv/tcg/csr.c +8 -0
index af444e3e6f..9b67f2d764 100644
@@ -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.
tests/tcg/riscv64/system/meson.build +9 -0
index 3c8b018d9e..db494f70c6 100644
@@ -89,6 +89,15 @@ tests += {
   }
 }
 
+tests += {
+  'test-hvip-read.S': {
+    'exe_name': 'test-hvip-read',
+    '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': {
tests/tcg/riscv64/test-hvip-read.S new file +41 -0

41 lines not on this page · raw message

Signature · 1 line
2.43.0