Writes to the mhpmevent CSRs can currently modify the
reserved WPRI bits 57:56. Fix this by ignoring writes to
these bits when writing to the mhpmevent CSRs through both
direct and indirect CSR access paths.
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4722
Suggested-by: Alistair Francis <alistair.francis@wdc.com>
Signed-off-by: Dmitry Kokorev <domestos48@mail.ru>
---
v2:
- Drop explicit preservation of mhpmevent WPRI bits.
target/riscv/cpu_bits.h | 2 ++
target/riscv/tcg/csr.c | 9 ++++++---
2 files changed, 8 insertions(+), 3 deletions(-)
diff --git a/target/riscv/cpu_bits.h b/target/riscv/cpu_bits.h
index 3579789a1f..3df9ea4ec3 100644
--- a/target/riscv/cpu_bits.h
+++ b/target/riscv/cpu_bits.h
@@ -1161,6 +1161,8 @@ typedef enum CTRType {
#define MHPMEVENTH_BIT_VSINH BIT(27)
#define MHPMEVENT_BIT_VUINH BIT_ULL(58)
#define MHPMEVENTH_BIT_VUINH BIT(26)
+#define MHPMEVENT_WPRI (BIT_ULL(57) | BIT_ULL(56))
+#define MHPMEVENTH_WPRI (BIT(25) | BIT(24))
#define MHPMEVENT_FILTER_MASK (MHPMEVENT_BIT_MINH | \
MHPMEVENT_BIT_SINH | \
diff --git a/target/riscv/tcg/csr.c b/target/riscv/tcg/csr.c
index af444e3e6f..94a57a4553 100644
--- a/target/riscv/tcg/csr.c
+++ b/target/riscv/tcg/csr.c
@@ -1230,13 +1230,15 @@ static RISCVException write_mhpmevent(CPURISCVState *env, int csrno,
if (riscv_cpu_mxl(env) == MXL_RV32) {
mhpmevt_val = deposit64(env->mhpmevent_val[evt_index], 0, 32, val);
} else {
- inh_avail_mask = ~MHPMEVENT_FILTER_MASK | MHPMEVENT_BIT_MINH;
+ inh_avail_mask = (~MHPMEVENT_FILTER_MASK | MHPMEVENT_BIT_MINH) &
+ ~MHPMEVENT_WPRI;
inh_avail_mask |= riscv_has_ext(env, RVU) ? MHPMEVENT_BIT_UINH : 0;
inh_avail_mask |= riscv_has_ext(env, RVS) ? MHPMEVENT_BIT_SINH : 0;
inh_avail_mask |= (riscv_has_ext(env, RVH) &&
riscv_has_ext(env, RVU)) ? MHPMEVENT_BIT_VUINH : 0;
inh_avail_mask |= (riscv_has_ext(env, RVH) &&
riscv_has_ext(env, RVS)) ? MHPMEVENT_BIT_VSINH : 0;
+
mhpmevt_val = val & inh_avail_mask;
}
@@ -1263,6 +1265,7 @@ static RISCVException write_mhpmeventh(CPURISCVState *env, int csrno,
target_ulong inh_avail_mask = (target_ulong)(~MHPMEVENTH_FILTER_MASK |
MHPMEVENTH_BIT_MINH);
+ inh_avail_mask &= ~MHPMEVENTH_WPRI;
inh_avail_mask |= riscv_has_ext(env, RVU) ? MHPMEVENTH_BIT_UINH : 0;
inh_avail_mask |= riscv_has_ext(env, RVS) ? MHPMEVENTH_BIT_SINH : 0;
inh_avail_mask |= (riscv_has_ext(env, RVH) &&
@@ -1531,7 +1534,7 @@ static int rmw_cd_mhpmevent(CPURISCVState *env, int evt_index,
*val &= ~MHPMEVENT_BIT_MINH;
}
} else if (wr_mask) {
- wr_mask &= ~MHPMEVENT_BIT_MINH;
+ wr_mask &= ~(MHPMEVENT_BIT_MINH | MHPMEVENT_WPRI);
/* wr_mask is 64-bit so upper 32 bits of mhpmevt_val are retained */
mhpmevt_val = (new_val & wr_mask) | (mhpmevt_val & ~wr_mask);
env->mhpmevent_val[evt_index] = mhpmevt_val;
@@ -1560,7 +1563,7 @@ static int rmw_cd_mhpmeventh(CPURISCVState *env, int evt_index,
*val &= ~MHPMEVENTH_BIT_MINH;
}
} else if (wr_mask) {
- wr_mask &= ~MHPMEVENTH_BIT_MINH;
+ wr_mask &= ~(MHPMEVENTH_BIT_MINH | MHPMEVENTH_WPRI);
mhpmevth_val = (new_val & wr_mask) | (mhpmevth_val & ~wr_mask);
mhpmevt_val = deposit64(mhpmevt_val, 32, 32, mhpmevth_val);
env->mhpmevent_val[evt_index] = mhpmevt_val;
--
2.56.0