target/riscv: fix WPRI handling in mhpmevent CSRs
PATCH v2 1/1 target/riscv: fix WPRI handling in mhpmevent CSRs
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 <[email protected]>
Signed-off-by Dmitry Kokorev <[email protected]>
---
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(-)
target/riscv/cpu_bits.h +2 -0
@@ -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 | \
target/riscv/tcg/csr.c +6 -3
@@ -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;
Signature · 1 line
2.56.0
Quoted 10 lines · Dmitry Kokorev
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 <[email protected]>Signed-off-by Dmitry Kokorev <[email protected]>---
Reviewed-by Daniel Henrique Barboza <[email protected]>
Quoted 67 lines
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(-)target/riscv/cpu_bits.h +2
-0@@ -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 | \target/riscv/tcg/csr.c +6
-3@@ -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;