The risc v privileged architecture says that mstatus.SXL, UXL, TSR,
TVM and MPVR should be read-only when S-mode or U-mode are not present.

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4456

Signed-off-by: Christian S. Lima <[email protected]>
---
 target/riscv/cpu_bits.h |  4 ++--
 target/riscv/tcg/csr.c  | 17 ++++++++++++-----
 2 files changed, 14 insertions(+), 7 deletions(-)

diff --git a/target/riscv/cpu_bits.h b/target/riscv/cpu_bits.h
index 3579789a1f..ee7981c91e 100644
--- a/target/riscv/cpu_bits.h
+++ b/target/riscv/cpu_bits.h
@@ -668,8 +668,8 @@
 #define MSTATUS_MPV         0x8000000000ULL
 #define MSTATUS_MDT         0x40000000000ULL /* Smdbltrp extension */
 
-#define MSTATUS64_UXL       0x0000000300000000ULL
-#define MSTATUS64_SXL       0x0000000C00000000ULL
+#define MSTATUS64_UXL       0x0000000200000000ULL
+#define MSTATUS64_SXL       0x0000000A00000000ULL
 
 #define MSTATUS32_SD        0x80000000
 #define MSTATUS64_SD        0x8000000000000000ULL
diff --git a/target/riscv/tcg/csr.c b/target/riscv/tcg/csr.c
index af444e3e6f..24eda54bac 100644
--- a/target/riscv/tcg/csr.c
+++ b/target/riscv/tcg/csr.c
@@ -2081,18 +2081,25 @@ static RISCVException write_mstatus(CPURISCVState *env, 
int csrno,
         tlb_flush(env_cpu(env));
     }
     mask = MSTATUS_SIE | MSTATUS_SPIE | MSTATUS_MIE | MSTATUS_MPIE |
-        MSTATUS_SPP | MSTATUS_MPRV | MSTATUS_SUM |
-        MSTATUS_MPP | MSTATUS_MXR | MSTATUS_TVM | MSTATUS_TSR |
-        MSTATUS_TW;
+        MSTATUS_SPP | MSTATUS_SUM | MSTATUS_MPP | MSTATUS_MXR | MSTATUS_TW;
 
     if (riscv_has_ext(env, RVF)) {
         mask |= MSTATUS_FS;
     }
 
+    if (riscv_has_ext(env, RVS) || riscv_has_ext(env, RVU)) {
+        mask |= MSTATUS_MPRV;
+    }
+
     if (riscv_cpu_cfg(env)->ext_zve32x) {
         mask |= MSTATUS_VS;
     }
 
+    if (riscv_has_ext(env, RVS)) {
+        mask |= MSTATUS_TSR;
+        mask |= MSTATUS_TVM;
+    }
+
     if (riscv_env_smode_dbltrp_enabled(env, env->virt_enabled)) {
         mask |= MSTATUS_SDT;
         if ((val & MSTATUS_SDT) != 0) {
@@ -2111,12 +2118,12 @@ static RISCVException write_mstatus(CPURISCVState *env, 
int csrno,
         if (riscv_has_ext(env, RVH)) {
             mask |= MSTATUS_MPV | MSTATUS_GVA;
         }
-        if ((val & MSTATUS64_SXL) != 0) {
+        if (riscv_has_ext(env, RVS) && (val & MSTATUS64_SXL) != 0) {
             mask |= MSTATUS64_SXL;
             val = riscv_write_uxl(env, val, MSTATUS64_SXL);
         }
 
-        if ((val & MSTATUS64_UXL) != 0) {
+        if (riscv_has_ext(env, RVU) && (val & MSTATUS64_UXL) != 0) {
             mask |= MSTATUS64_UXL;
             val = riscv_write_uxl(env, val, MSTATUS64_UXL);
         }
-- 
2.56.0



Reply via email to