Skip to contentSkip to conversation

target/riscv/csr.c: allow regular mstatus MPV/GVA writes again

PATCH In review Daniel Henrique Barboza 2 participants 2 messages Oct 24 – Oct 26 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/riscv/csr.c: allow regular mstatus MPV/GVA writes again

In commit 18645f195789 we removed the capability of MPV/GVA writes by software, claiming that the ISA says that these bits are changed by the implementation (i.e. hardware).

This fact alone can't be interpret as "so software can't change them". The ISA never mentions that, and we shouldn't just assume read/write accesses based on whether hardware updates it or not. M-mode software can do whatever they want, unless clearly stated in the ISA, and that includes mstatus MPV and GVA updates.

And in fact existing software is writing to these bits, as reported by Fangyu in [1], and that change ended up causing real problems in the end.

Revert "target/riscv/csr.c: do not allow mstatus MPV/GVA writes".

This reverts commit 18645f19578955ec5ff2c40cd2c8753d6bc460c2.

[1] https://lore.kernel.org/qemu-devel/[email protected]/

---
 target/riscv/tcg/csr.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
target/riscv/tcg/csr.c +4 -1
index 13675b4a84..af444e3e6f 100644
@@ -2108,6 +2108,9 @@ static RISCVException write_mstatus(CPURISCVState *env, int csrno,
     }
 
     if (xl != MXL_RV32 || env->debugger) {
+        if (riscv_has_ext(env, RVH)) {
+            mask |= MSTATUS_MPV | MSTATUS_GVA;
+        }
         if ((val & MSTATUS64_SXL) != 0) {
             mask |= MSTATUS64_SXL;
             val = riscv_write_uxl(env, val, MSTATUS64_SXL);
@@ -2150,7 +2153,7 @@ static RISCVException write_mstatush(CPURISCVState *env, int csrno,
                                      target_ulong val, uintptr_t ra)
 {
     uint64_t valh = (uint64_t)val << 32;
-    uint64_t mask = 0;
+    uint64_t mask = riscv_has_ext(env, RVH) ? MSTATUS_MPV | MSTATUS_GVA : 0;
 
     if (riscv_cpu_cfg(env)->ext_smdbltrp) {
         mask |= MSTATUS_MDT;
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 20 lines · Daniel Henrique Barboza

In commit 18645f195789 we removed the capability of MPV/GVA writes by software, claiming that the ISA says that these bits are changed by the
implementation (i.e. hardware).

This fact alone can't be interpret as "so software can't change them".
The ISA never mentions that, and we shouldn't just assume read/write accesses based on whether hardware updates it or not.  M-mode software
can do whatever they want, unless clearly stated in the ISA, and that includes mstatus MPV and GVA updates.

And in fact existing software is writing to these bits, as reported by
Fangyu in [1], and that change ended up causing real problems in the end.

Revert "target/riscv/csr.c: do not allow mstatus MPV/GVA writes".

Quoted 8 lines

This reverts commit 18645f19578955ec5ff2c40cd2c8753d6bc460c2.

[1]
https://lore.kernel.org/qemu-devel/[email protected]/

Signed-off-by Daniel Henrique Barboza

<[email protected]>

Alistair

Quoted 30 lines

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

 1 file changed, 4 insertions(+), 1 deletion(-)
target/riscv/tcg/csr.c +0 -0
index 13675b4a84..af444e3e6f 100644
@@ -2108,6 +2108,9 @@ static RISCVException
 write_mstatus(CPURISCVState *env, int csrno,
      }


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

target/riscv/tcg/csr.c +1 -1
@@ -2150,7 +2153,7 @@ static RISCVException
 write_mstatush(CPURISCVState *env, int csrno,
                                       target_ulong val, uintptr_t ra)
  {
      uint64_t valh = (uint64_t)val << 32;
-    uint64_t mask = 0;
+    uint64_t mask = riscv_has_ext(env, RVH) ? MSTATUS_MPV |
 MSTATUS_GVA : 0;


     if (riscv_cpu_cfg(env)->ext_smdbltrp) {
         mask |= MSTATUS_MDT;