Skip to contentSkip to conversation

target/riscv: Fix mstatus.MPP related support

PATCH Applied Weiwei Li 3 participants 9 messages Apr 7, 2023 – Apr 11, 2023 RISC-V

lore
Share & tools
lore

Weiwei Li

author
#0 lore raw

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
author
#1 lore raw

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
index ec9a384772..b8a03afebb 100644
@@ -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
author
#2 lore raw

PATCH v3 2/3 target/riscv: Use PRV_RESERVED instead of PRV_H

In reply to Weiwei Li

PRV_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
index cbf3de2708..4af8ebc558 100644
@@ -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
index 101702cb4a..a16bfaf43f 100644
@@ -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
index fa537aed74..524bede865 100644
@@ -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
index b8a03afebb..bd21c6eeef 100644
@@ -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
Reviewed-by on v3 2/3
#3 lore raw

Re: [PATCH v3 2/3] target/riscv: Use PRV_RESERVED instead of PRV_H

Quoted 10 lines · Weiwei Li

PRV_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(-)

Thanks!

r~

Reviewed-by on v3 2/3
#4 lore raw

Re: [PATCH v3 2/3] target/riscv: Use PRV_RESERVED instead of PRV_H

In reply to Weiwei Li
Quoted 5 lines · Weiwei Li

PRV_H has no real meaning, but just a reserved privilege mode currently.

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
index cbf3de2708..4af8ebc558 100644
@@ -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
index 101702cb4a..a16bfaf43f 100644
@@ -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
index fa537aed74..524bede865 100644
@@ -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
index b8a03afebb..bd21c6eeef 100644
@@ -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

author
#5 lore raw

PATCH v3 3/3 target/riscv: Legalize MPP value in write_mstatus

In reply to 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().

---
 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
index 2310c7905f..433ea529b0 100644
@@ -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
index e0b871f6dc..f4d2dcfdc8 100644
@@ -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
Reviewed-by on v3 3/3
#6 lore raw

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().

---
  target/riscv/cpu_helper.c |  8 ++------
  target/riscv/csr.c        | 32 ++++++++++++++++++++++++++++++++
  2 files changed, 34 insertions(+), 6 deletions(-)

r~

Reviewed-by on v3 3/3
#7 lore raw

Re: [PATCH v3 3/3] target/riscv: Legalize MPP value in write_mstatus

In reply to Weiwei Li
Quoted 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().

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
index 2310c7905f..433ea529b0 100644
@@ -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
index e0b871f6dc..f4d2dcfdc8 100644
@@ -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

#8 lore raw
In reply to Weiwei Li
Quoted 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-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

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.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

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