Hi,

Please disregard this patch.  I just re-sent it folded in a short series here:


 [PATCH 0/2] target/riscv: rv32/mstatush related fixes [GL4488,GL4827]


Thanks,

Daniel

On 11/12/2026 6:10 PM, Daniel Henrique Barboza wrote:
For RV32 we have mstatus and mstatush, both 32 bit CSRs, that shares the
same 64 bit container in env->mstatus.  mstatus will read/write the
lower 32 bits of env->mstatus, mstatush will read/write the higher 32
bits of env->mstatus.

We forgot about that in write_mstatus() and, when running in 32 bits,
we're happily setting the write mask with bits that belongs to higher
half of env->mstatus.  The result is that whatever was written there via
mstatush is being cleared.

The solution is to clear the higher half of the mask if we're running 32
bits.

Fixes: 284d697c74ef ("target/riscv: Merge m/vsstatus and m/vsstatush into one 
uint64_t unit")
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4488
Signed-off-by: Daniel Henrique Barboza <[email protected]>
---
  target/riscv/tcg/csr.c                  |  9 ++++
  tests/tcg/riscv32/system/meson.build    |  7 +++
  tests/tcg/riscv32/test-mstatush-clear.S | 57 +++++++++++++++++++++++++
  3 files changed, 73 insertions(+)
  create mode 100644 tests/tcg/riscv32/test-mstatush-clear.S

diff --git a/target/riscv/tcg/csr.c b/target/riscv/tcg/csr.c
index af444e3e6f..ab436244cf 100644
--- a/target/riscv/tcg/csr.c
+++ b/target/riscv/tcg/csr.c
@@ -2127,6 +2127,15 @@ static RISCVException write_mstatus(CPURISCVState *env, 
int csrno,
          mask |= (MSTATUS_MPELP | MSTATUS_SPELP);
      }
+ /*
+     * For 32 bit accesses we're writing just the lower 32 bits of
+     * the env->mstatus container.  The higher bits are written via
+     * write_mstatush().
+     */
+    if (xl == MXL_RV32) {
+        mask &= MAKE_64BIT_MASK(0, 32);
+    }
+
      mstatus = (mstatus & ~mask) | (val & mask);
env->mstatus = mstatus;
diff --git a/tests/tcg/riscv32/system/meson.build 
b/tests/tcg/riscv32/system/meson.build
index 117ae6fbce..c70cc12517 100644
--- a/tests/tcg/riscv32/system/meson.build
+++ b/tests/tcg/riscv32/system/meson.build
@@ -22,6 +22,13 @@ tests += {
    },
  }
+tests += {
+  'test-mstatush-clear.S': {
+    'cflags': cflags,
+    'qemu_args': ['-cpu', 'max,smdbltrp=on', qemu_args],
+  },
+}
+
  if 'qemu-system-riscv32' in emulators
    tcg_tests += {
      'riscv32-softmmu': {
diff --git a/tests/tcg/riscv32/test-mstatush-clear.S 
b/tests/tcg/riscv32/test-mstatush-clear.S
new file mode 100644
index 0000000000..5053832700
--- /dev/null
+++ b/tests/tcg/riscv32/test-mstatush-clear.S
@@ -0,0 +1,57 @@
+/*
+ * Verify that mstatush is not being cleared after writing mstatus
+ * (Gitlab 4488).
+ *
+ * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#define MSTATUSH_MDT 0x400
+
+.option norvc
+       .text
+       .global _start
+
+_start:
+       lla     t0, trap
+       csrw    mtvec, t0
+
+       /* Write mstatush.MDT.  */
+       csrr    s0, mstatush
+       li      t0, MSTATUSH_MDT
+       or      t1, s0, t0
+       csrw    mstatush, t1
+
+       /* Check if mstatush.MDT is indeed written.  */
+       csrr    s0, mstatush
+       and     t1, s0, t0
+       bne     t0, t1, _fail
+
+       /* Read mstatus, write the same stuff back to it.  */
+       csrr    t1, mstatus
+       csrw    mstatus, t1
+
+       /*
+        * Make sure that we didn't clear mstatush.MDT after
+        * writing mstatus.
+        */
+       csrr    s0, mstatush
+       and     t1, s0, t0
+       bne     t0, t1, _fail
+
+       j       _pass
+
+trap:
+       j       _fail
+
+_pass:
+       li      t0, 0x100000
+       li      t1, 0x5555      /* FINISHER_PASS */
+       sw      t1, 0(t0)
+       j       .
+
+_fail:
+       li      t0, 0x100000
+       li      t1, 0x13333     /* status = FINISHER_FAIL, code = 1 */
+       sw      t1, 0(t0)
+       j       .


Reply via email to