target/riscv: Fix mstatus.MPP related support
v2 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-v2
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 (2):
target/riscv: Fix the mstatus.MPP value after executing MRET target/riscv: Legalize MPP value in write_mstatus
target/riscv/cpu_helper.c | 5 +---- target/riscv/csr.c | 18 ++++++++++++++++++ target/riscv/op_helper.c | 3 ++- 3 files changed, 21 insertions(+), 5 deletions(-)
Signature · 1 line
2.25.1
PATCH v2 1/2 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 v2 2/2 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 | 5 +---- target/riscv/csr.c | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-)
target/riscv/cpu_helper.c +1 -4
@@ -647,12 +647,9 @@ 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 ) { +if (newpriv >PRV_M || newpriv ==PRV_H ) {g_assert_not_reached (); } -if (newpriv ==PRV_H ) { - newpriv =PRV_U ; - }if (icount_enabled () && newpriv != env->priv) {riscv_itrigger_update_priv (env); }
target/riscv/csr.c +18 -0
@@ -1230,6 +1230,18 @@ 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) +{ +target_ulong new_mpp =get_field (val,MSTATUS_MPP ); +bool mpp_invalid = (new_mpp ==PRV_S && !riscv_has_ext (env,RVS )) || + (new_mpp ==PRV_U && !riscv_has_ext (env,RVU )) || + (new_mpp ==PRV_H ); + +/* Remain field unchanged if new_mpp value is invalid */ +return mpp_invalid ?set_field (val,MSTATUS_MPP , old_mpp) : val; +} +static RISCVException write_mstatus (CPURISCVState *env,int csrno,target_ulong val) {
@@ -1237,6 +1249,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 v2 2/2] target/riscv: Legalize MPP value in write_mstatus
target/riscv/cpu_helper.c void riscv_cpu_set_aia_ireg_rmw_fn(CPURISCVState *env, uint32_t priv, · line 652
Quoted 6 lines · Weiwei Li
void riscv_cpu_set_mode(CPURISCVState *env, target_ulong newpriv) {target/riscv/cpu_helper.c +1
-1-if (newpriv >PRV_M ) { +if (newpriv >PRV_M || newpriv ==PRV_H ) {g_assert_not_reached (); }
Nit: if (test) { assert_not_reached } -> assert(!test).
which emits a more helpful error message before abort.
r~
Re: [PATCH v2 2/2] target/riscv: Legalize MPP value in write_mstatus
Quoted 8 lines · Richard Henderson
Quoted 6 lines · Weiwei Li
void
riscv_cpu_set_mode(CPURISCVState *env, target_ulong newpriv){
- if (newpriv >PRV_M) {
+ if (newpriv >PRV_M|| newpriv ==PRV_H) {g_assert_not_reached();
}
Quoted 4 lines
Nit: if (test) {
assert_not_reached} -> assert(!test).which emits a more helpful error message before abort.
OK. I'll update this.
Regards,
Weiwei Li
Quoted 2 lines
r~
Re: [PATCH v2 2/2] target/riscv: Legalize MPP value in write_mstatus
In reply to Weiwei Litarget/riscv/csr.c static bool validate_vm(CPURISCVState *env, target_ulong vm) · line 1243
Quoted 11 lines · Weiwei Li
target/riscv/csr.c +11
-0+static target_ulong legalize_mpp (CPURISCVState *env,target_ulong old_mpp, +target_ulong val) +{ +target_ulong new_mpp =get_field (val,MSTATUS_MPP ); +bool mpp_invalid = (new_mpp ==PRV_S && !riscv_has_ext (env,RVS )) || + (new_mpp ==PRV_U && !riscv_has_ext (env,RVU )) || + (new_mpp ==PRV_H ); + +/* Remain field unchanged if new_mpp value is invalid */ +return mpp_invalid ?set_field (val,MSTATUS_MPP , old_mpp) : val; +}
Does anyone find PRV_H confusing, since that's not what it is? I think it would be nice to remove it entirely.
This function might be better as
bool valid = false;
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;
}
if (!valid) {
val = set_field(...);
}
return val;r~
Re: [PATCH v2 2/2] target/riscv: Legalize MPP value in write_mstatus
Quoted 10 lines · Richard Henderson
Quoted 9 lines · Weiwei Li
+static
target_ulonglegalize_mpp(CPURISCVState *env,target_ulongold_mpp,
+target_ulongval)
+{
+target_ulongnew_mpp=get_field(val, MSTATUS_MPP); + boolmpp_invalid= (new_mpp==PRV_S&& !riscv_has_ext(env, RVS)) ||
+ (new_mpp==PRV_U&& !riscv_has_ext(env, RVU)) ||
Quoted 8 lines
target/riscv/csr.c static bool validate_vm(CPURISCVState *env, target_ulong vm) · line 1243
Quoted 5 lines
+ (
new_mpp==PRV_H);target/riscv/csr.c +4
-0+ + /* Remain field unchanged if new_mpp value is invalid */ + return mpp_invalid ? set_field(val, MSTATUS_MPP, old_mpp) : val; +}Does anyone find
PRV_Hconfusing, since that's not what it is? I think it would be nice to remove it entirely.
I agree. Maybe PRV_RESERVED is more readable in some cases.
Quoted 5 lines
This function might be better as
bool valid = false;
Quoted 17 lines
switch (
new_mpp) {
casePRV_M:
valid = true;
break;
casePRV_S:
valid =riscv_has_ext(env, RVS);
break;
casePRV_U:
valid =riscv_has_ext(env, RVU);
break;
}
if (!valid) {
val =set_field(...);
}
return val;
OK. This is more readable. I'll update this.
Regards,
Weiwei Li
Quoted 1 line
r~