[PATCH v3] target/riscv: change mstatus bits for read-only if S or U unsupported

Christian S. Lima posted 1 patch 1 week, 3 days ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20261112202356.23980-1-christianslima@proton.me
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>
There is a newer version of this series
target/riscv/cpu.c     | 12 ++++++++++--
target/riscv/tcg/csr.c | 17 ++++++++++++-----
2 files changed, 22 insertions(+), 7 deletions(-)
[PATCH v3] target/riscv: change mstatus bits for read-only if S or U unsupported
Posted by Christian S. Lima 1 week, 3 days ago
The risc v privileged architecture says that mstatus.SXL, UXL, TSR,
TVM and MPRV should be read-only when S-mode or U-mode are not
present.

changes in v3:
- Fixed grammar mistakes and now Daniel can be credited correctly by the tooling

Link to v2: https://lore.kernel.org/qemu-riscv/20261110232614.83050-1-christianslima@proton.me/

changes in v2:
- Don't change bits in target/riscv/cpu_bits.h
- Add checks to S-Mode in the mstatus.SXL and U-mode mstatus.UXL in
the target/riscv/cpu.c file that were suggested by Daniel in the
review [1]

Link to v1: https://lore.kernel.org/qemu-riscv/20261104223049.86286-1-christianslima@proton.me/

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4456

[1] https://lore.kernel.org/qemu-riscv/20261104223049.86286-1-christianslima@proton.me/T/#mddbb34a3ea837e3c707131a2e354c63c989ab029

Signed-off-by: Christian S. Lima <christianslima@proton.me>
Suggested-by: Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com>
---
 target/riscv/cpu.c     | 12 ++++++++++--
 target/riscv/tcg/csr.c | 17 ++++++++++++-----
 2 files changed, 22 insertions(+), 7 deletions(-)

diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c
index 15c27f4ce4..50bdfbe025 100644
--- a/target/riscv/cpu.c
+++ b/target/riscv/cpu.c
@@ -997,8 +997,16 @@ static void riscv_cpu_reset_hold(Object *obj, ResetType type)
          * The reset status of SXL/UXL is undefined, but mstatus is WARL
          * and we must ensure that the value after init is valid for read.
          */
-        env->mstatus = set_field(env->mstatus, MSTATUS64_SXL, env->misa_mxl);
-        env->mstatus = set_field(env->mstatus, MSTATUS64_UXL, env->misa_mxl);
+        if (riscv_has_ext(env, RVS)) {
+            env->mstatus = set_field(env->mstatus,
+                                     MSTATUS64_SXL, env->misa_mxl);
+        }
+
+        if (riscv_has_ext(env, RVU)) {
+            env->mstatus = set_field(env->mstatus,
+                                     MSTATUS64_UXL, env->misa_mxl);
+        }
+
         if (riscv_has_ext(env, RVH)) {
             env->vsstatus = set_field(env->vsstatus,
                                       MSTATUS64_SXL, env->misa_mxl);
diff --git a/target/riscv/tcg/csr.c b/target/riscv/tcg/csr.c
index af444e3e6f..24eda54bac 100644
--- a/target/riscv/tcg/csr.c
+++ b/target/riscv/tcg/csr.c
@@ -2081,18 +2081,25 @@ static RISCVException write_mstatus(CPURISCVState *env, int csrno,
         tlb_flush(env_cpu(env));
     }
     mask = MSTATUS_SIE | MSTATUS_SPIE | MSTATUS_MIE | MSTATUS_MPIE |
-        MSTATUS_SPP | MSTATUS_MPRV | MSTATUS_SUM |
-        MSTATUS_MPP | MSTATUS_MXR | MSTATUS_TVM | MSTATUS_TSR |
-        MSTATUS_TW;
+        MSTATUS_SPP | MSTATUS_SUM | MSTATUS_MPP | MSTATUS_MXR | MSTATUS_TW;
 
     if (riscv_has_ext(env, RVF)) {
         mask |= MSTATUS_FS;
     }
 
+    if (riscv_has_ext(env, RVS) || riscv_has_ext(env, RVU)) {
+        mask |= MSTATUS_MPRV;
+    }
+
     if (riscv_cpu_cfg(env)->ext_zve32x) {
         mask |= MSTATUS_VS;
     }
 
+    if (riscv_has_ext(env, RVS)) {
+        mask |= MSTATUS_TSR;
+        mask |= MSTATUS_TVM;
+    }
+
     if (riscv_env_smode_dbltrp_enabled(env, env->virt_enabled)) {
         mask |= MSTATUS_SDT;
         if ((val & MSTATUS_SDT) != 0) {
@@ -2111,12 +2118,12 @@ static RISCVException write_mstatus(CPURISCVState *env, int csrno,
         if (riscv_has_ext(env, RVH)) {
             mask |= MSTATUS_MPV | MSTATUS_GVA;
         }
-        if ((val & MSTATUS64_SXL) != 0) {
+        if (riscv_has_ext(env, RVS) && (val & MSTATUS64_SXL) != 0) {
             mask |= MSTATUS64_SXL;
             val = riscv_write_uxl(env, val, MSTATUS64_SXL);
         }
 
-        if ((val & MSTATUS64_UXL) != 0) {
+        if (riscv_has_ext(env, RVU) && (val & MSTATUS64_UXL) != 0) {
             mask |= MSTATUS64_UXL;
             val = riscv_write_uxl(env, val, MSTATUS64_UXL);
         }
-- 
2.56.0
Re: [PATCH v3] target/riscv: change mstatus bits for read-only if S or U unsupported
Posted by Daniel Henrique Barboza 1 week, 3 days ago

On 11/12/2026 5:24 PM, Christian S. Lima wrote:
> The risc v privileged architecture says that mstatus.SXL, UXL, TSR,
> TVM and MPRV should be read-only when S-mode or U-mode are not
> present.
> 
> changes in v3:
> - Fixed grammar mistakes and now Daniel can be credited correctly by the tooling

Oh! Just now realized that the tag was wrong in v2:

"Suugested-by: Daniel ..."

> 
> Link to v2: https://lore.kernel.org/qemu-riscv/20261110232614.83050-1-christianslima@proton.me/
> 
> changes in v2:
> - Don't change bits in target/riscv/cpu_bits.h
> - Add checks to S-Mode in the mstatus.SXL and U-mode mstatus.UXL in
> the target/riscv/cpu.c file that were suggested by Daniel in the
> review [1]
> 
> Link to v1: https://lore.kernel.org/qemu-riscv/20261104223049.86286-1-christianslima@proton.me/
> 
> Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4456
> 
> [1] https://lore.kernel.org/qemu-riscv/20261104223049.86286-1-christianslima@proton.me/T/#mddbb34a3ea837e3c707131a2e354c63c989ab029
> 
> Signed-off-by: Christian S. Lima <christianslima@proton.me>
> Suggested-by: Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com>
> ---

Reviewed-by: Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com>

>   target/riscv/cpu.c     | 12 ++++++++++--
>   target/riscv/tcg/csr.c | 17 ++++++++++++-----
>   2 files changed, 22 insertions(+), 7 deletions(-)
> 
> diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c
> index 15c27f4ce4..50bdfbe025 100644
> --- a/target/riscv/cpu.c
> +++ b/target/riscv/cpu.c
> @@ -997,8 +997,16 @@ static void riscv_cpu_reset_hold(Object *obj, ResetType type)
>            * The reset status of SXL/UXL is undefined, but mstatus is WARL
>            * and we must ensure that the value after init is valid for read.
>            */
> -        env->mstatus = set_field(env->mstatus, MSTATUS64_SXL, env->misa_mxl);
> -        env->mstatus = set_field(env->mstatus, MSTATUS64_UXL, env->misa_mxl);
> +        if (riscv_has_ext(env, RVS)) {
> +            env->mstatus = set_field(env->mstatus,
> +                                     MSTATUS64_SXL, env->misa_mxl);
> +        }
> +
> +        if (riscv_has_ext(env, RVU)) {
> +            env->mstatus = set_field(env->mstatus,
> +                                     MSTATUS64_UXL, env->misa_mxl);
> +        }
> +
>           if (riscv_has_ext(env, RVH)) {
>               env->vsstatus = set_field(env->vsstatus,
>                                         MSTATUS64_SXL, env->misa_mxl);
> diff --git a/target/riscv/tcg/csr.c b/target/riscv/tcg/csr.c
> index af444e3e6f..24eda54bac 100644
> --- a/target/riscv/tcg/csr.c
> +++ b/target/riscv/tcg/csr.c
> @@ -2081,18 +2081,25 @@ static RISCVException write_mstatus(CPURISCVState *env, int csrno,
>           tlb_flush(env_cpu(env));
>       }
>       mask = MSTATUS_SIE | MSTATUS_SPIE | MSTATUS_MIE | MSTATUS_MPIE |
> -        MSTATUS_SPP | MSTATUS_MPRV | MSTATUS_SUM |
> -        MSTATUS_MPP | MSTATUS_MXR | MSTATUS_TVM | MSTATUS_TSR |
> -        MSTATUS_TW;
> +        MSTATUS_SPP | MSTATUS_SUM | MSTATUS_MPP | MSTATUS_MXR | MSTATUS_TW;
>   
>       if (riscv_has_ext(env, RVF)) {
>           mask |= MSTATUS_FS;
>       }
>   
> +    if (riscv_has_ext(env, RVS) || riscv_has_ext(env, RVU)) {
> +        mask |= MSTATUS_MPRV;
> +    }
> +
>       if (riscv_cpu_cfg(env)->ext_zve32x) {
>           mask |= MSTATUS_VS;
>       }
>   
> +    if (riscv_has_ext(env, RVS)) {
> +        mask |= MSTATUS_TSR;
> +        mask |= MSTATUS_TVM;
> +    }
> +
>       if (riscv_env_smode_dbltrp_enabled(env, env->virt_enabled)) {
>           mask |= MSTATUS_SDT;
>           if ((val & MSTATUS_SDT) != 0) {
> @@ -2111,12 +2118,12 @@ static RISCVException write_mstatus(CPURISCVState *env, int csrno,
>           if (riscv_has_ext(env, RVH)) {
>               mask |= MSTATUS_MPV | MSTATUS_GVA;
>           }
> -        if ((val & MSTATUS64_SXL) != 0) {
> +        if (riscv_has_ext(env, RVS) && (val & MSTATUS64_SXL) != 0) {
>               mask |= MSTATUS64_SXL;
>               val = riscv_write_uxl(env, val, MSTATUS64_SXL);
>           }
>   
> -        if ((val & MSTATUS64_UXL) != 0) {
> +        if (riscv_has_ext(env, RVU) && (val & MSTATUS64_UXL) != 0) {
>               mask |= MSTATUS64_UXL;
>               val = riscv_write_uxl(env, val, MSTATUS64_UXL);
>           }
Re: [PATCH v3] target/riscv: change mstatus bits for read-only if S or U unsupported
Posted by Christian 1 week, 3 days ago



Enviado com um e-mail seguro do Proton Mail.

Em quinta-feira, 12 de novembro de 2026 às 5:27 PM, Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com> escreveu:

>
>
> On 11/12/2026 5:24 PM, Christian S. Lima wrote:
> > The risc v privileged architecture says that mstatus.SXL, UXL, TSR,
> > TVM and MPRV should be read-only when S-mode or U-mode are not
> > present.
> >
> > changes in v3:
> > - Fixed grammar mistakes and now Daniel can be credited correctly by the tooling
>
> Oh! Just now realized that the tag was wrong in v2:
>
> "Suugested-by: Daniel ..."

I too! And the MPRV bit was named MPVR. Tbh, I was a bit tired when I wrote the v1 and v2 messages. I'll pay more attention.  

> > Link to v2: https://lore.kernel.org/qemu-riscv/20261110232614.83050-1-christianslima@proton.me/
> >
> > changes in v2:
> > - Don't change bits in target/riscv/cpu_bits.h
> > - Add checks to S-Mode in the mstatus.SXL and U-mode mstatus.UXL in
> > the target/riscv/cpu.c file that were suggested by Daniel in the
> > review [1]
> >
> > Link to v1: https://lore.kernel.org/qemu-riscv/20261104223049.86286-1-christianslima@proton.me/
> >
> > Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4456
> >
> > [1] https://lore.kernel.org/qemu-riscv/20261104223049.86286-1-christianslima@proton.me/T/#mddbb34a3ea837e3c707131a2e354c63c989ab029
> >
> > Signed-off-by: Christian S. Lima <christianslima@proton.me>
> > Suggested-by: Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com>
> > ---
>
> Reviewed-by: Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com>
>
> >   target/riscv/cpu.c     | 12 ++++++++++--
> >   target/riscv/tcg/csr.c | 17 ++++++++++++-----
> >   2 files changed, 22 insertions(+), 7 deletions(-)
> >
> > diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c
> > index 15c27f4ce4..50bdfbe025 100644
> > --- a/target/riscv/cpu.c
> > +++ b/target/riscv/cpu.c
> > @@ -997,8 +997,16 @@ static void riscv_cpu_reset_hold(Object *obj, ResetType type)
> >            * The reset status of SXL/UXL is undefined, but mstatus is WARL
> >            * and we must ensure that the value after init is valid for read.
> >            */
> > -        env->mstatus = set_field(env->mstatus, MSTATUS64_SXL, env->misa_mxl);
> > -        env->mstatus = set_field(env->mstatus, MSTATUS64_UXL, env->misa_mxl);
> > +        if (riscv_has_ext(env, RVS)) {
> > +            env->mstatus = set_field(env->mstatus,
> > +                                     MSTATUS64_SXL, env->misa_mxl);
> > +        }
> > +
> > +        if (riscv_has_ext(env, RVU)) {
> > +            env->mstatus = set_field(env->mstatus,
> > +                                     MSTATUS64_UXL, env->misa_mxl);
> > +        }
> > +
> >           if (riscv_has_ext(env, RVH)) {
> >               env->vsstatus = set_field(env->vsstatus,
> >                                         MSTATUS64_SXL, env->misa_mxl);
> > diff --git a/target/riscv/tcg/csr.c b/target/riscv/tcg/csr.c
> > index af444e3e6f..24eda54bac 100644
> > --- a/target/riscv/tcg/csr.c
> > +++ b/target/riscv/tcg/csr.c
> > @@ -2081,18 +2081,25 @@ static RISCVException write_mstatus(CPURISCVState *env, int csrno,
> >           tlb_flush(env_cpu(env));
> >       }
> >       mask = MSTATUS_SIE | MSTATUS_SPIE | MSTATUS_MIE | MSTATUS_MPIE |
> > -        MSTATUS_SPP | MSTATUS_MPRV | MSTATUS_SUM |
> > -        MSTATUS_MPP | MSTATUS_MXR | MSTATUS_TVM | MSTATUS_TSR |
> > -        MSTATUS_TW;
> > +        MSTATUS_SPP | MSTATUS_SUM | MSTATUS_MPP | MSTATUS_MXR | MSTATUS_TW;
> >
> >       if (riscv_has_ext(env, RVF)) {
> >           mask |= MSTATUS_FS;
> >       }
> >
> > +    if (riscv_has_ext(env, RVS) || riscv_has_ext(env, RVU)) {
> > +        mask |= MSTATUS_MPRV;
> > +    }
> > +
> >       if (riscv_cpu_cfg(env)->ext_zve32x) {
> >           mask |= MSTATUS_VS;
> >       }
> >
> > +    if (riscv_has_ext(env, RVS)) {
> > +        mask |= MSTATUS_TSR;
> > +        mask |= MSTATUS_TVM;
> > +    }
> > +
> >       if (riscv_env_smode_dbltrp_enabled(env, env->virt_enabled)) {
> >           mask |= MSTATUS_SDT;
> >           if ((val & MSTATUS_SDT) != 0) {
> > @@ -2111,12 +2118,12 @@ static RISCVException write_mstatus(CPURISCVState *env, int csrno,
> >           if (riscv_has_ext(env, RVH)) {
> >               mask |= MSTATUS_MPV | MSTATUS_GVA;
> >           }
> > -        if ((val & MSTATUS64_SXL) != 0) {
> > +        if (riscv_has_ext(env, RVS) && (val & MSTATUS64_SXL) != 0) {
> >               mask |= MSTATUS64_SXL;
> >               val = riscv_write_uxl(env, val, MSTATUS64_SXL);
> >           }
> >
> > -        if ((val & MSTATUS64_UXL) != 0) {
> > +        if (riscv_has_ext(env, RVU) && (val & MSTATUS64_UXL) != 0) {
> >               mask |= MSTATUS64_UXL;
> >               val = riscv_write_uxl(env, val, MSTATUS64_UXL);
> >           }
>
>