Skip to contentSkip to conversation

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

PATCH New Daniel Henrique Barboza 1 participant 2 messages Nov 12

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover target/riscv/tcg/csr: filter bits when r/w HVIP(H) [GL3133]

Hi,

In this version we have adjustments suggested by Sashiko and one small meson.build change done after feeback on another patch. All changes are superficial and do not affect the core logic.

Changes in v2:
- fix extra whitespace between sha and opening parenthesis (Sashiko)
- fix Gitlab bug number in test-hvip-read.S (3313 -> 3133) (Sashiko)
- remove 'exe_name' field from meson.build test entry
- v1 link: https://lore.kernel.org/qemu-devel/[email protected]/

Daniel Henrique Barboza (1):
target/riscv/tcg/csr: filter bits when r/w HVIP(H) [GL3133]

 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
Signature · 1 line
2.43.0
#1 lore raw

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

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 |  8 ++++++
 tests/tcg/riscv64/test-hvip-read.S   | 41 ++++++++++++++++++++++++++++
 3 files changed, 57 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 +8 -0
index 3c8b018d9e..7eca59222c 100644
@@ -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': {
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