target/riscv: Fix mstatus.MPP related support
v3 cover target/riscv: Fix mstatus.MPP related support
This patchset tries to fix some problems in current implementation for mstatus.MPP
The port is available here:
https://github.com/plctlab/plct-qemu/tree/plct-mpp-fix-v3
v3:
* add patch 2 to remove PRV_H, and use PRV_RESERVED instead in some cases
* improve legalize_mpp and assert error message in patch 3
v2:
* Modify commit message and add comment to specify MPP field becomes a WARL field since priv version 1.11 in patch 2
* rebase on riscv-to-apply.next
Weiwei Li (3):
target/riscv: Fix the mstatus.MPP value after executing MRET target/riscv: Use PRV_RESERVED instead of PRV_H target/riscv: Legalize MPP value in write_mstatus
target/riscv/cpu.h | 2 +- target/riscv/cpu_bits.h | 2 +- target/riscv/cpu_helper.c | 8 ++------ target/riscv/csr.c | 32 ++++++++++++++++++++++++++++++++ target/riscv/gdbstub.c | 2 +- target/riscv/op_helper.c | 5 +++-- 6 files changed, 40 insertions(+), 11 deletions(-)
Signature · 1 line
2.25.1
PATCH v3 1/3 target/riscv: Fix the mstatus.MPP value after executing MRET
The MPP will be set to the least-privileged supported mode (U if U-mode is implemented, else M).
--- target/riscv/op_helper.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
target/riscv/op_helper.c +2 -1
@@ -338,7 +338,8 @@ target_ulong helper_mret(CPURISCVState *env) mstatus =set_field (mstatus,MSTATUS_MIE ,get_field (mstatus,MSTATUS_MPIE )); mstatus =set_field (mstatus,MSTATUS_MPIE ,1 ); - mstatus =set_field (mstatus,MSTATUS_MPP ,PRV_U ); + mstatus =set_field (mstatus,MSTATUS_MPP , +riscv_has_ext (env,RVU ) ?PRV_U :PRV_M ); mstatus =set_field (mstatus,MSTATUS_MPV ,0 );if ((env->priv_ver >=PRIV_VERSION_1_12_0 ) && (prev_priv !=PRV_M )) { mstatus =set_field (mstatus,MSTATUS_MPRV ,0 );
Signature · 1 line
2.25.1
PATCH v3 2/3 target/riscv: Use PRV_RESERVED instead of PRV_H
In reply to Weiwei LiPRV_H has no real meaning, but just a reserved privilege mode currently.
--- target/riscv/cpu.h | 2 +- target/riscv/cpu_bits.h | 2 +- target/riscv/gdbstub.c | 2 +- target/riscv/op_helper.c | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-)
target/riscv/cpu.h +1 -1
@@ -699,7 +699,7 @@ static inline RISCVMXL cpu_recompute_xl(CPURISCVState *env)case PRV_U : xl =get_field (env->mstatus,MSTATUS64_UXL );break ; -default :/* PRV_S | PRV_H */ +default :/* PRV_S */ xl =get_field (env->mstatus,MSTATUS64_SXL );break ; }
target/riscv/cpu_bits.h +1 -1
@@ -608,7 +608,7 @@ typedef enum {/* Privilege modes */ #define PRV_U 0#define PRV_S 1 -#define PRV_H 2/* Reserved */ +#define PRV_RESERVED 2#define PRV_M 3/* RV32 satp CSR field masks */
target/riscv/gdbstub.c +1 -1
@@ -203,7 +203,7 @@ static int riscv_gdb_set_virtual(CPURISCVState *cs, uint8_t *mem_buf, int n)if (n ==0 ) {#ifndef CONFIG_USER_ONLY cs->priv =ldtul_p (mem_buf) &0x3 ; -if (cs->priv ==PRV_H ) { +if (cs->priv ==PRV_RESERVED ) { cs->priv =PRV_S ; }#endif
target/riscv/op_helper.c +1 -1
@@ -141,7 +141,7 @@ static void check_zicbo_envcfg(CPURISCVState *env, target_ulong envbits, }if (env->virt_enabled && - (((env->priv <PRV_H ) && !get_field (env->henvcfg, envbits)) || + (((env->priv <=PRV_S ) && !get_field (env->henvcfg, envbits)) || ((env->priv <PRV_S ) && !get_field (env->senvcfg, envbits)))) {riscv_raise_exception (env,RISCV_EXCP_VIRT_INSTRUCTION_FAULT , ra); }
Signature · 1 line
2.25.1
Re: [PATCH v3 2/3] target/riscv: Use PRV_RESERVED instead of PRV_H
Quoted 10 lines · Weiwei Li
PRV_Hhas no real meaning, but just a reserved privilege mode currently.Signed-off-by Weiwei Li<[email protected]>Signed-off-by Junqiang Wang<[email protected]>--- target/riscv/cpu.h | 2 +- target/riscv/cpu_bits.h | 2 +- target/riscv/gdbstub.c | 2 +- target/riscv/op_helper.c | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-)
Thanks!
r~
Re: [PATCH v3 2/3] target/riscv: Use PRV_RESERVED instead of PRV_H
In reply to Weiwei LiQuoted 5 lines · Weiwei Li
PRV_Hhas no real meaning, but just a reserved privilege mode currently.Signed-off-by Weiwei Li <[email protected]>Signed-off-by Junqiang Wang <[email protected]>
Alistair
Quoted 63 lines
--- target/riscv/cpu.h | 2 +- target/riscv/cpu_bits.h | 2 +- target/riscv/gdbstub.c | 2 +- target/riscv/op_helper.c | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-)target/riscv/cpu.h +1
-1@@ -699,7 +699,7 @@ static inline RISCVMXL cpu_recompute_xl(CPURISCVState *env)case PRV_U : xl =get_field (env->mstatus,MSTATUS64_UXL );break ; -default :/* PRV_S | PRV_H */ +default :/* PRV_S */ xl =get_field (env->mstatus,MSTATUS64_SXL );break ; }target/riscv/cpu_bits.h +1
-1@@ -608,7 +608,7 @@ typedef enum {/* Privilege modes */ #define PRV_U 0#define PRV_S 1 -#define PRV_H 2/* Reserved */ +#define PRV_RESERVED 2#define PRV_M 3/* RV32 satp CSR field masks */ target/riscv/gdbstub.c +1
-1@@ -203,7 +203,7 @@ static int riscv_gdb_set_virtual(CPURISCVState *cs, uint8_t *mem_buf, int n)if (n ==0 ) {#ifndef CONFIG_USER_ONLY cs->priv =ldtul_p (mem_buf) &0x3 ; -if (cs->priv ==PRV_H ) { +if (cs->priv ==PRV_RESERVED ) { cs->priv =PRV_S ; }#endif target/riscv/op_helper.c +1
-2@@ -141,7 +141,7 @@ static void check_zicbo_envcfg(CPURISCVState *env, target_ulong envbits, }if (env->virt_enabled && - (((env->priv <PRV_H ) && !get_field (env->henvcfg, envbits)) || + (((env->priv <=PRV_S ) && !get_field (env->henvcfg, envbits)) || ((env->priv <PRV_S ) && !get_field (env->senvcfg, envbits)))) {riscv_raise_exception (env,RISCV_EXCP_VIRT_INSTRUCTION_FAULT , ra); } --2.25.1
PATCH v3 3/3 target/riscv: Legalize MPP value in write_mstatus
In reply to Weiwei Limstatus.MPP field is a WARL field since priv version 1.11, so we remain it unchanged if an invalid value is written into it. And after this, RVH shouldn't be passed to riscv_cpu_set_mode().
--- target/riscv/cpu_helper.c | 8 ++------ target/riscv/csr.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-)
target/riscv/cpu_helper.c +2 -6
@@ -647,12 +647,8 @@ void riscv_cpu_set_aia_ireg_rmw_fn(CPURISCVState *env, uint32_t priv,void riscv_cpu_set_mode (CPURISCVState *env,target_ulong newpriv) { -if (newpriv >PRV_M ) { -g_assert_not_reached (); - } -if (newpriv ==PRV_H ) { - newpriv =PRV_U ; - } +g_assert (newpriv <=PRV_M && newpriv !=PRV_RESERVED ); +if (icount_enabled () && newpriv != env->priv) {riscv_itrigger_update_priv (env); }
target/riscv/csr.c +32 -0
@@ -1230,6 +1230,32 @@ static bool validate_vm(CPURISCVState *env, target_ulong vm)satp_mode_max_from_map (riscv_cpu_cfg (env)->satp_mode.map); } +static target_ulong legalize_mpp (CPURISCVState *env,target_ulong old_mpp, +target_ulong val) +{ +bool valid = false; +target_ulong new_mpp =get_field (val,MSTATUS_MPP ); + +switch (new_mpp) { +case PRV_M : + valid = true; +break ; +case PRV_S : + valid =riscv_has_ext (env,RVS ); +break ; +case PRV_U : + valid =riscv_has_ext (env,RVU ); +break ; + } + +/* Remain field unchanged if new_mpp value is invalid */ +if (!valid) { + val =set_field (val,MSTATUS_MPP , old_mpp); + } + +return val; +} +static RISCVException write_mstatus (CPURISCVState *env,int csrno,target_ulong val) {
@@ -1237,6 +1263,12 @@ static RISCVException write_mstatus(CPURISCVState *env, int csrno,uint64_t mask =0 ;RISCVMXL xl =riscv_cpu_mxl (env); +/* +* MPP field have been made WARL since priv version 1.11. However, +* legalization for it will not break any software running on 1.10. +*/ + val =legalize_mpp (env,get_field (mstatus,MSTATUS_MPP ), val); +/* flush tlb on mstatus fields that affect VM */ if ((val ^ mstatus) & (MSTATUS_MXR |MSTATUS_MPP |MSTATUS_MPV |MSTATUS_MPRV |MSTATUS_SUM )) {
Signature · 1 line
2.25.1
Re: [PATCH v3 3/3] target/riscv: Legalize MPP value in write_mstatus
Quoted 10 lines · Weiwei Li
mstatus.MPP field is a WARL field since priv version 1.11, so we remain it unchanged if an invalid value is written into it. And after this, RVH shouldn't be passed to
riscv_cpu_set_mode().Signed-off-by Weiwei Li<[email protected]>Signed-off-by Junqiang Wang<[email protected]>--- target/riscv/cpu_helper.c | 8 ++------ target/riscv/csr.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-)
r~
Re: [PATCH v3 3/3] target/riscv: Legalize MPP value in write_mstatus
In reply to Weiwei LiQuoted 7 lines · Weiwei Li
mstatus.MPP field is a WARL field since priv version 1.11, so we remain it unchanged if an invalid value is written into it. And after this, RVH shouldn't be passed to
riscv_cpu_set_mode().Signed-off-by Weiwei Li <[email protected]>Signed-off-by Junqiang Wang <[email protected]>
Alistair
Quoted 78 lines
--- target/riscv/cpu_helper.c | 8 ++------ target/riscv/csr.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-)target/riscv/cpu_helper.c +2
-6@@ -647,12 +647,8 @@ void riscv_cpu_set_aia_ireg_rmw_fn(CPURISCVState *env, uint32_t priv,void riscv_cpu_set_mode (CPURISCVState *env,target_ulong newpriv) { -if (newpriv >PRV_M ) { -g_assert_not_reached (); - } -if (newpriv ==PRV_H ) { - newpriv =PRV_U ; - } +g_assert (newpriv <=PRV_M && newpriv !=PRV_RESERVED ); +if (icount_enabled () && newpriv != env->priv) {riscv_itrigger_update_priv (env); }target/riscv/csr.c +32
-1@@ -1230,6 +1230,32 @@ static bool validate_vm(CPURISCVState *env, target_ulong vm)satp_mode_max_from_map (riscv_cpu_cfg (env)->satp_mode.map); } +static target_ulong legalize_mpp (CPURISCVState *env,target_ulong old_mpp, +target_ulong val) +{ +bool valid = false; +target_ulong new_mpp =get_field (val,MSTATUS_MPP ); + +switch (new_mpp) { +case PRV_M : + valid = true; +break ; +case PRV_S : + valid =riscv_has_ext (env,RVS ); +break ; +case PRV_U : + valid =riscv_has_ext (env,RVU ); +break ; + } + +/* Remain field unchanged if new_mpp value is invalid */ +if (!valid) { + val =set_field (val,MSTATUS_MPP , old_mpp); + } + +return val; +} +static RISCVException write_mstatus (CPURISCVState *env,int csrno,target_ulong val) {@@ -1237,6 +1263,12 @@ static RISCVException write_mstatus(CPURISCVState *env, int csrno,uint64_t mask =0 ;RISCVMXL xl =riscv_cpu_mxl (env); +/* +* MPP field have been made WARL since priv version 1.11. However, +* legalization for it will not break any software running on 1.10. +*/ + val =legalize_mpp (env,get_field (mstatus,MSTATUS_MPP ), val); +/* flush tlb on mstatus fields that affect VM */ if ((val ^ mstatus) & (MSTATUS_MXR |MSTATUS_MPP |MSTATUS_MPV |MSTATUS_MPRV |MSTATUS_SUM )) { --2.25.1
Alistair Francis
AppliedQuoted 10 lines · Weiwei Li
This patchset tries to fix some problems in current implementation for mstatus.MPP
The port is available here:
https://github.com/plctlab/plct-qemu/tree/plct-mpp-fix-v3v3:
* add patch 2 to removePRV_H, and usePRV_RESERVEDinstead in some cases
* improvelegalize_mppand assert error message in patch 3
Quoted 8 lines
v2:
* Modify commit message and add comment to specify MPP field becomes a WARL field since priv version 1.11 in patch 2
* rebase on riscv-to-apply.nextWeiwei Li (3):
target/riscv: Fix the mstatus.MPP value after executing MRET target/riscv: UsePRV_RESERVEDinstead ofPRV_Htarget/riscv: Legalize MPP value inwrite_mstatus
Thanks!
Applied to riscv-to-apply.next
Alistair
Quoted 13 lines
target/riscv/cpu.h | 2 +- target/riscv/cpu_bits.h | 2 +- target/riscv/cpu_helper.c | 8 ++------ target/riscv/csr.c | 32 ++++++++++++++++++++++++++++++++ target/riscv/gdbstub.c | 2 +- target/riscv/op_helper.c | 5 +++-- 6 files changed, 40 insertions(+), 11 deletions(-)--
2.25.1