target/riscv/cpu_bits.h | 2 ++ target/riscv/tcg/csr.c | 21 ++++++++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-)
Writes to the mhpmevent CSRs can currently modify the
reserved WPRI bits 57:56. Fix this by retaining the
previous values of these bits when writing to the mhpmevent
CSRs through both direct and indirect CSR access paths.
Signed-off-by: Dmitry Kokorev <domestos48@mail.ru>
---
target/riscv/cpu_bits.h | 2 ++
target/riscv/tcg/csr.c | 21 ++++++++++++++++-----
2 files changed, 18 insertions(+), 5 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..bc422c0014 100644
--- a/target/riscv/tcg/csr.c
+++ b/target/riscv/tcg/csr.c
@@ -1230,14 +1230,17 @@ 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;
+
+ mhpmevt_val = (val & inh_avail_mask) |
+ (env->mhpmevent_val[evt_index] & MHPMEVENT_WPRI);
}
env->mhpmevent_val[evt_index] = mhpmevt_val;
@@ -1262,7 +1265,10 @@ static RISCVException write_mhpmeventh(CPURISCVState *env, int csrno,
int evt_index = csrno - CSR_MHPMEVENT3H + 3;
target_ulong inh_avail_mask = (target_ulong)(~MHPMEVENTH_FILTER_MASK |
MHPMEVENTH_BIT_MINH);
+ target_ulong old_wpri_bits;
+ target_ulong new_high;
+ 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) &&
@@ -1270,8 +1276,13 @@ static RISCVException write_mhpmeventh(CPURISCVState *env, int csrno,
inh_avail_mask |= (riscv_has_ext(env, RVH) &&
riscv_has_ext(env, RVS)) ? MHPMEVENTH_BIT_VSINH : 0;
+ old_wpri_bits = extract64(env->mhpmevent_val[evt_index], 32, 32) &
+ MHPMEVENTH_WPRI;
+
+ new_high = (val & inh_avail_mask) | old_wpri_bits;
+
env->mhpmevent_val[evt_index] = deposit64(env->mhpmevent_val[evt_index],
- 32, 32, val & inh_avail_mask);
+ 32, 32, new_high);
riscv_pmu_update_event_map(env, env->mhpmevent_val[evt_index], evt_index);
@@ -1531,7 +1542,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 +1571,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
On Mon, 2026-11-09 at 01:02 +0300, Dmitry Kokorev via qemu development
wrote:
> Writes to the mhpmevent CSRs can currently modify the
> reserved WPRI bits 57:56. Fix this by retaining the
> previous values of these bits when writing to the mhpmevent
> CSRs through both direct and indirect CSR access paths.
Why bother keeping the existing values? When these are used in the
future we will want to update the masks anyway, I think we can just
ignore the bits entirely
Alistair
>
> Signed-off-by: Dmitry Kokorev <domestos48@mail.ru>
> ---
> target/riscv/cpu_bits.h | 2 ++
> target/riscv/tcg/csr.c | 21 ++++++++++++++++-----
> 2 files changed, 18 insertions(+), 5 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..bc422c0014 100644
> --- a/target/riscv/tcg/csr.c
> +++ b/target/riscv/tcg/csr.c
> @@ -1230,14 +1230,17 @@ 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;
> +
> + mhpmevt_val = (val & inh_avail_mask) |
> + (env->mhpmevent_val[evt_index] &
> MHPMEVENT_WPRI);
> }
>
> env->mhpmevent_val[evt_index] = mhpmevt_val;
> @@ -1262,7 +1265,10 @@ static RISCVException
> write_mhpmeventh(CPURISCVState *env, int csrno,
> int evt_index = csrno - CSR_MHPMEVENT3H + 3;
> target_ulong inh_avail_mask =
> (target_ulong)(~MHPMEVENTH_FILTER_MASK |
>
> MHPMEVENTH_BIT_MINH);
> + target_ulong old_wpri_bits;
> + target_ulong new_high;
>
> + 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) &&
> @@ -1270,8 +1276,13 @@ static RISCVException
> write_mhpmeventh(CPURISCVState *env, int csrno,
> inh_avail_mask |= (riscv_has_ext(env, RVH) &&
> riscv_has_ext(env, RVS)) ?
> MHPMEVENTH_BIT_VSINH : 0;
>
> + old_wpri_bits = extract64(env->mhpmevent_val[evt_index], 32, 32)
> &
> + MHPMEVENTH_WPRI;
> +
> + new_high = (val & inh_avail_mask) | old_wpri_bits;
> +
> env->mhpmevent_val[evt_index] = deposit64(env-
> >mhpmevent_val[evt_index],
> - 32, 32, val &
> inh_avail_mask);
> + 32, 32, new_high);
>
> riscv_pmu_update_event_map(env, env->mhpmevent_val[evt_index],
> evt_index);
>
> @@ -1531,7 +1542,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 +1571,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;
I forgot to include the Resolves tag in the commit message. This patch fixes: https://gitlab.com/qemu-project/qemu/-/work_items/4722 ( https://gitlab.com/qemu-project/qemu/-/work_items/4722 ) Could you please add the Resolves tag when applying the patch?
© 2016 - 2026 Red Hat, Inc.