Skip to contentSkip to conversation

target/riscv: Fix vsstatus.UXL reserved value

PATCH In review SeungJu Cheon 2 participants 3 messages Jun 24 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/riscv: Fix vsstatus.UXL reserved value

By the priv spec the value "3" is marked as 'Reserved' for mstatus.UXL. write_mstatus() rejects this value, but write_vsstatus() does not, allowing the reserved value to be written and read back unchanged.

Handle a vsstatus.UXL = 3 write by writing the current 'xl' instead.

Fixes f310df58bd2 ("target/riscv: Enable uxl field write")
---
 target/riscv/csr.c | 8 ++++++++
 1 file changed, 8 insertions(+)
target/riscv/csr.c +8 -0
index dd9726fcf4..4c1da61824 100644
@@ -5226,7 +5226,15 @@ static RISCVException write_vsstatus(CPURISCVState *env, int csrno,
     uint64_t mask = (target_ulong)-1;
     if ((val & VSSTATUS64_UXL) == 0) {
         mask &= ~VSSTATUS64_UXL;
+    } else {
+        uint64_t uxl = (val & VSSTATUS64_UXL) >> 32;
+        if (uxl == 3) {
+            int xl = riscv_cpu_mxl(env);
+            val = deposit64(val, 32, 2,
+                            xl == MXL_RV128 ? MXL_RV64 : xl);
+        }
     }
+
     if ((env->henvcfg & HENVCFG_DTE)) {
         if ((val & SSTATUS_SDT) != 0) {
             val &= ~SSTATUS_SIE;
Signature · 1 line
2.52.0
#1 lore raw

Hello,

Quoted 20 lines · SeungJu Cheon

By the priv spec the value "3" is marked as 'Reserved' for mstatus.UXL. write_mstatus() rejects this value, but write_vsstatus() does not, allowing the reserved value to be written and read back unchanged.

Handle a vsstatus.UXL = 3 write by writing the current 'xl' instead.

Fixes f310df58bd2 ("target/riscv: Enable uxl field write")
---
  target/riscv/csr.c | 8 ++++++++
  1 file changed, 8 insertions(+)
target/riscv/csr.c +0 -0
index dd9726fcf4..4c1da61824 100644
@@ -5226,7 +5226,15 @@ static RISCVException write_vsstatus(CPURISCVState *env, int csrno,
      uint64_t mask = (target_ulong)-1;

target/riscv/csr.c static RISCVException write_vsstatus(CPURISCVState *env, int csrno, · line 5236

Quoted 10 lines
target/riscv/csr.c +7 -0
      if ((val & VSSTATUS64_UXL) == 0) {
          mask &= ~VSSTATUS64_UXL;
+    } else {
+        uint64_t uxl = (val & VSSTATUS64_UXL) >> 32;
+        if (uxl == 3) {
+            int xl = riscv_cpu_mxl(env);
+            val = deposit64(val, 32, 2,
+                            xl == MXL_RV128 ? MXL_RV64 : xl);
+        }
      }

This is similar to what was done here:

commit ddfd33f1965804fc4a718d8d46bc150525c2f9db

Author: Daniel Henrique Barboza <[email protected]>
Date:   Thu May 14 16:45:37 2026 -0300

target/riscv/csr.c: fix mstatus.UXL reserved value

target/riscv/csr.c +10 -0
index 83ca354bf0..d004a4bfb4 100644
@@ -2045,7 +2045,17 @@ static RISCVException write_mstatus(CPURISCVState *env, int csrno,
 
     if (xl != MXL_RV32 || env->debugger) {
         if ((val & MSTATUS64_UXL) != 0) {
+            uint64_t uxl = val & MSTATUS64_UXL >> 32;
             mask |= MSTATUS64_UXL;
+
+            /*
+             * uxl = 3 is reserved so write the current xl instead.
+             * In case xl = MXL_RV128 (3) write MXL_RV64.
+             */
+            if (uxl == 3) {
+                uxl = xl == MXL_RV128 ? MXL_RV64 : xl;
+                val = deposit64(val, 32, 2, uxl);
+            }
         }
     }
 

But that patch fixed just write_mstatus().

I suggest adding a local helper riscv_write_uxl (feel free to choose another better name) to encapsulate this logic and then use it in both mstatus and vsstatus to write UXL:

val = riscv_write_uxl(val)

Thanks,
Daniel

Quoted 4 lines
target/riscv/csr.c +1 -0
+
      if ((env->henvcfg & HENVCFG_DTE)) {
          if ((val & SSTATUS_SDT) != 0) {
              val &= ~SSTATUS_SIE;
#2 lore raw

Thanks for the review, Daniel.

I'll add a helper function and resend as v2.

Best regards,
SeungJu

Quoted 86 lines · Daniel Henrique Barboza

Hello,

Quoted 20 lines · SeungJu Cheon

By the priv spec the value "3" is marked as 'Reserved' for mstatus.UXL. write_mstatus() rejects this value, but write_vsstatus() does not, allowing the reserved value to be written and read back unchanged.

Handle a vsstatus.UXL = 3 write by writing the current 'xl' instead.

Fixes f310df58bd2 ("target/riscv: Enable uxl field write")
---
  target/riscv/csr.c | 8 ++++++++
  1 file changed, 8 insertions(+)
target/riscv/csr.c +0 -0
index dd9726fcf4..4c1da61824 100644
@@ -5226,7 +5226,15 @@ static RISCVException write_vsstatus(CPURISCVState *env, int csrno,
      uint64_t mask = (target_ulong)-1;

target/riscv/csr.c static RISCVException write_vsstatus(CPURISCVState *env, int csrno, · line 5236

Quoted 10 lines
target/riscv/csr.c +7 -0
      if ((val & VSSTATUS64_UXL) == 0) {
          mask &= ~VSSTATUS64_UXL;
+    } else {
+        uint64_t uxl = (val & VSSTATUS64_UXL) >> 32;
+        if (uxl == 3) {
+            int xl = riscv_cpu_mxl(env);
+            val = deposit64(val, 32, 2,
+                            xl == MXL_RV128 ? MXL_RV64 : xl);
+        }
      }

This is similar to what was done here:

commit ddfd33f1965804fc4a718d8d46bc150525c2f9db

Author: Daniel Henrique Barboza <[email protected]>
Date:   Thu May 14 16:45:37 2026 -0300

target/riscv/csr.c: fix mstatus.UXL reserved value

target/riscv/csr.c +10 -0
index 83ca354bf0..d004a4bfb4 100644
@@ -2045,7 +2045,17 @@ static RISCVException write_mstatus(CPURISCVState *env, int csrno,
 
      if (xl != MXL_RV32 || env->debugger) {
          if ((val & MSTATUS64_UXL) != 0) {
+            uint64_t uxl = val & MSTATUS64_UXL >> 32;
              mask |= MSTATUS64_UXL;
+
+            /*
+             * uxl = 3 is reserved so write the current xl instead.
+             * In case xl = MXL_RV128 (3) write MXL_RV64.
+             */
+            if (uxl == 3) {
+                uxl = xl == MXL_RV128 ? MXL_RV64 : xl;
+                val = deposit64(val, 32, 2, uxl);
+            }
          }
      }
 

But that patch fixed just write_mstatus().

I suggest adding a local helper riscv_write_uxl (feel free to choose another better name) to encapsulate this logic and then use it in both mstatus and vsstatus to write UXL:

val = riscv_write_uxl(val)

Thanks,
Daniel

Quoted 4 lines
target/riscv/csr.c +1 -0
+
      if ((env->henvcfg & HENVCFG_DTE)) {
          if ((val & SSTATUS_SDT) != 0) {
              val &= ~SSTATUS_SIE;