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

Daniel Henrique Barboza posted 1 patch 4 weeks, 1 day ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20261024065930.26750-1-daniel.barboza@oss.qualcomm.com
Maintainers: Palmer Dabbelt <palmer@dabbelt.com>, Alistair Francis <alistair.francis@wdc.com>, Weiwei Li <liwei1518@gmail.com>, Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com>, Liu Zhiwei <zhiwei_liu@linux.alibaba.com>, Chao Liu <chao.liu@processmission.com>
target/riscv/tcg/csr.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
[PATCH] target/riscv/csr.c: allow regular mstatus MPV/GVA writes again
Posted by Daniel Henrique Barboza 4 weeks, 1 day ago
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/20261022120111.24298-1-fangyu.yu@linux.alibaba.com/

Reported-by: Fangyu Yu <fangyu.yu@linux.alibaba.com>
Signed-off-by: Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com>
---
 target/riscv/tcg/csr.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/target/riscv/tcg/csr.c b/target/riscv/tcg/csr.c
index 13675b4a84..af444e3e6f 100644
--- a/target/riscv/tcg/csr.c
+++ b/target/riscv/tcg/csr.c
@@ -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;
-- 
2.43.0
Re: [PATCH] target/riscv/csr.c: allow regular mstatus MPV/GVA writes again
Posted by Alistair Francis 4 weeks ago
On Sat, 2026-10-24 at 03:59 -0300, Daniel Henrique Barboza wrote:
> 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/20261022120111.24298-1-fangyu.yu@linux.alibaba.com/
> 
> Reported-by: Fangyu Yu <fangyu.yu@linux.alibaba.com>
> Signed-off-by: Daniel Henrique Barboza
> <daniel.barboza@oss.qualcomm.com>

Reviewed-by: Alistair Francis <alistair.francis@wdc.com>

Alistair

> ---
>  target/riscv/tcg/csr.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/target/riscv/tcg/csr.c b/target/riscv/tcg/csr.c
> index 13675b4a84..af444e3e6f 100644
> --- a/target/riscv/tcg/csr.c
> +++ b/target/riscv/tcg/csr.c
> @@ -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;