Skip to contentSkip to conversation

target/riscv: use SXL instead of MXL for read_sstatus

PATCH Applied Abhigyan Kumar 3 participants 3 messages Jul 23 – Aug 11 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 target/riscv: use SXL instead of MXL for read_sstatus

According to the RISC-V spec, a 64-bit system can have M-mode in 64-bit with S-mode being 32-bit (SXL bits or mstatus[35:34] being 1). In this case, read_sstatus should use SXL.

QEMU doesn't allow changing the SXL bits in mstatus in M-mode. This was because of the missing MSTATUS64_SXL mask in write_mstatus. Now, both the SXL field in mstatus can be safely modified in M-mode and read_sstatus correctly uses SXL not MXL.

Fixes b550f89457 ("target/riscv: Compute mstatus.sd on demand")
---
 target/riscv/tcg/csr.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
target/riscv/tcg/csr.c +7 -2
index 36f2004bc..a90449b87 100644
@@ -2067,6 +2067,11 @@ static RISCVException write_mstatus(CPURISCVState *env, int csrno,
     }
 
     if (xl != MXL_RV32 || env->debugger) {
+        if ((val & MSTATUS64_SXL) != 0) {
+            mask |= MSTATUS64_SXL;
+            val = riscv_write_uxl(env, val, MSTATUS64_SXL);
+        }
+
         if ((val & MSTATUS64_UXL) != 0) {
             mask |= MSTATUS64_UXL;
             val = riscv_write_uxl(env, val, MSTATUS64_UXL);
@@ -4014,8 +4019,8 @@ static RISCVException read_sstatus(CPURISCVState *env, int csrno,
     if (riscv_cpu_cfg(env)->ext_ssdbltrp) {
         mask |= SSTATUS_SDT;
     }
-    /* TODO: Use SXL not MXL. */
-    *val = add_status_sd(riscv_cpu_mxl(env), env->mstatus & mask);
+
+    *val = add_status_sd(riscv_cpu_sxl(env), env->mstatus & mask);
     return RISCV_EXCP_NONE;
 }
 
Signature · 1 line
2.55.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 4 lines · Abhigyan Kumar

According to the RISC-V spec, a 64-bit system can have M-mode in 64-bit with S-mode being 32-bit (SXL bits or mstatus[35:34] being 1). In this case, read_sstatus should use SXL.

Quoted 8 lines

QEMU doesn't allow changing the SXL bits in mstatus in M-mode. This was because of the missing MSTATUS64_SXL mask in write_mstatus. Now, both the SXL field in mstatus can be safely modified in M-mode and read_sstatus correctly uses SXL not MXL.

Fixes b550f89457 ("target/riscv: Compute mstatus.sd on demand")

---

Quoted 30 lines
  target/riscv/tcg/csr.c | 9 +++++++--
  1 file changed, 7 insertions(+), 2 deletions(-)
target/riscv/tcg/csr.c +7 -2
index 36f2004bc..a90449b87 100644
@@ -2067,6 +2067,11 @@ static RISCVException write_mstatus(CPURISCVState *env, int csrno,
      }
 
      if (xl != MXL_RV32 || env->debugger) {
+        if ((val & MSTATUS64_SXL) != 0) {
+            mask |= MSTATUS64_SXL;
+            val = riscv_write_uxl(env, val, MSTATUS64_SXL);
+        }
+
          if ((val & MSTATUS64_UXL) != 0) {
              mask |= MSTATUS64_UXL;
              val = riscv_write_uxl(env, val, MSTATUS64_UXL);
@@ -4014,8 +4019,8 @@ static RISCVException read_sstatus(CPURISCVState *env, int csrno,
      if (riscv_cpu_cfg(env)->ext_ssdbltrp) {
          mask |= SSTATUS_SDT;
      }
-    /* TODO: Use SXL not MXL. */
-    *val = add_status_sd(riscv_cpu_mxl(env), env->mstatus & mask);
+
+    *val = add_status_sd(riscv_cpu_sxl(env), env->mstatus & mask);
      return RISCV_EXCP_NONE;
  }
 
Applied
#2 lore raw
In reply to Abhigyan Kumar
Quoted 6 lines · Abhigyan Kumar

According to the RISC-V spec, a 64-bit system can have M-mode in 64- bit
with S-mode being 32-bit (SXL bits or mstatus[35:34] being 1). In this
case, read_sstatus should use SXL.

Quoted 8 lines

QEMU doesn't allow changing the SXL bits in mstatus in M-mode. This was
because of the missing MSTATUS64_SXL mask in write_mstatus. Now, both the SXL field in mstatus can be safely modified in M-mode and read_sstatus correctly uses SXL not MXL.

Fixes b550f89457 ("target/riscv: Compute mstatus.sd on demand")

Thanks!

Applied to riscv-to-apply.next

Alistair

Quoted 33 lines

---
 target/riscv/tcg/csr.c | 9 +++++++--

 1 file changed, 7 insertions(+), 2 deletions(-)
target/riscv/tcg/csr.c +0 -0
index 36f2004bc..a90449b87 100644
@@ -2067,6 +2067,11 @@ static RISCVException
 write_mstatus(CPURISCVState *env, int csrno,
      }


     if (xl != MXL_RV32 || env->debugger) {
+        if ((val & MSTATUS64_SXL) != 0) {
+            mask |= MSTATUS64_SXL;
+            val = riscv_write_uxl(env, val, MSTATUS64_SXL); +        }
+
         if ((val & MSTATUS64_UXL) != 0) {
             mask |= MSTATUS64_UXL;
             val = riscv_write_uxl(env, val, MSTATUS64_UXL);

target/riscv/tcg/csr.c +2 -2
@@ -4014,8 +4019,8 @@ static RISCVException
 read_sstatus(CPURISCVState *env, int csrno,
      if (riscv_cpu_cfg(env)->ext_ssdbltrp) {
          mask |= SSTATUS_SDT;
      }
-    /* TODO: Use SXL not MXL. */
-    *val = add_status_sd(riscv_cpu_mxl(env), env->mstatus & mask);
+
+    *val = add_status_sd(riscv_cpu_sxl(env), env->mstatus & mask);
      return RISCV_EXCP_NONE;
  }