On 11/4/2026 7:30 PM, Christian S. Lima wrote:
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

Note that both values were correct - UXL is defined as 33:32 and SXL is 35:34.
If you had to change them to make this fix works this means that there's 
something else
missing.


I took a look to check if there were other places where they're used.  They are 
used
when setting the reset value of mstatus in target/riscv/cpu.c, 
riscv_cpu_reset_hold():

    if (env->misa_mxl > MXL_RV32) {
        /*
         * The reset status of SXL/UXL is undefined, but mstatus is WARL
         * and we must ensure that the value after init is valid for read.
         */
        env->mstatus = set_field(env->mstatus, MSTATUS64_SXL, sxl);
        env->mstatus = set_field(env->mstatus, MSTATUS64_UXL, uxl);



Note that there's no check if we have RVS or RVU before setting them.


If I squash this diff into this patch we can have the bug fixed without changing
these macros:

$ git diff
diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c
index 15c27f4ce4..4d42e20ce9 100644
--- a/target/riscv/cpu.c
+++ b/target/riscv/cpu.c
@@ -997,8 +997,10 @@ static void riscv_cpu_reset_hold(Object *obj, ResetType 
type)
          * The reset status of SXL/UXL is undefined, but mstatus is WARL
          * and we must ensure that the value after init is valid for read.
          */
-        env->mstatus = set_field(env->mstatus, MSTATUS64_SXL, env->misa_mxl);
-        env->mstatus = set_field(env->mstatus, MSTATUS64_UXL, env->misa_mxl);
+        uint32_t sxl = riscv_has_ext(env, RVS) ? env->misa_mxl : 0;
+        uint32_t uxl = riscv_has_ext(env, RVU) ? env->misa_mxl : 0;
+        env->mstatus = set_field(env->mstatus, MSTATUS64_SXL, sxl);
+        env->mstatus = set_field(env->mstatus, MSTATUS64_UXL, uxl);
         if (riscv_has_ext(env, RVH)) {
             env->vsstatus = set_field(env->vsstatus,
                                       MSTATUS64_SXL, env->misa_mxl);
diff --git a/target/riscv/cpu_bits.h b/target/riscv/cpu_bits.h
index ee7981c91e..3579789a1f 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       0x0000000200000000ULL
-#define MSTATUS64_SXL       0x0000000A00000000ULL
+#define MSTATUS64_UXL       0x0000000300000000ULL
+#define MSTATUS64_SXL       0x0000000C00000000ULL

 #define MSTATUS32_SD        0x80000000
 #define MSTATUS64_SD        0x8000000000000000ULL


I.e keep MSTATUS64_UXL and MSTATUS64_SXL untouched and add the RVS/RVU checks in
reset_hold().


Everything else LGTM.  Thanks,

Daniel


#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);
          }


Reply via email to