Skip to contentSkip to conversation

target/riscv: Restrict mideleg/medeleg/medelegh access to S-mode harts

PATCH Superseded Jay Chang 2 participants 4 messages Mar 18, 2025 – Mar 26, 2025 RISC-V

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Jay Chang

author
#0 lore raw

PATCH 1/2 target/riscv: Restrict mideleg/medeleg/medelegh access to S-mode harts

RISC-V Privileged Spec states:
"In harts with S-mode, the medeleg and mideleg registers must exist, and setting a bit in medeleg or mideleg will delegate the corresponding trap , when occurring in S-mode or U-mode, to the S-mode trap handler. In harts without S-mode, the medeleg and mideleg registers should not exist."

Add smode predicate to ensure these CSRs are only accessible when S-mode is supported.

---
 target/riscv/csr.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
target/riscv/csr.c +3 -3
index 7948188356..975d6e307f 100644
@@ -5783,8 +5783,8 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = {
                           NULL,                read_mstatus_i128           },
     [CSR_MISA]        = { "misa",       any,   read_misa,    write_misa,
                           NULL,                read_misa_i128              },
-    [CSR_MIDELEG]     = { "mideleg",    any,   NULL, NULL,   rmw_mideleg   },
-    [CSR_MEDELEG]     = { "medeleg",    any,   read_medeleg, write_medeleg },
+    [CSR_MIDELEG]     = { "mideleg",    smode,   NULL, NULL,   rmw_mideleg   },
+    [CSR_MEDELEG]     = { "medeleg",    smode,   read_medeleg, write_medeleg },
     [CSR_MIE]         = { "mie",        any,   NULL, NULL,   rmw_mie       },
     [CSR_MTVEC]       = { "mtvec",      any,   read_mtvec,   write_mtvec   },
     [CSR_MCOUNTEREN]  = { "mcounteren", umode, read_mcounteren,
@@ -5792,7 +5792,7 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = {
 
     [CSR_MSTATUSH]    = { "mstatush",   any32, read_mstatush,
                           write_mstatush                                   },
-    [CSR_MEDELEGH]    = { "medelegh",   any32, read_zero, write_ignore,
+    [CSR_MEDELEGH]    = { "medelegh",   smode32, read_zero, write_ignore,
                           .min_priv_ver = PRIV_VERSION_1_13_0              },
     [CSR_HEDELEGH]    = { "hedelegh",   hmode32, read_hedelegh, write_hedelegh,
                           .min_priv_ver = PRIV_VERSION_1_13_0              },
Signature · 1 line
2.48.1
author
#1 lore raw

PATCH 2/2 target/riscv: Restrict midelegh access to S-mode harts

RISC-V AIA Spec states:
"For a machine-level environment, extension Smaia encompasses all added CSRs and all modifications to interrupt response behavior that the AIA specifies for a hart, over all privilege levels. For a supervisor-level environment, extension Ssaia is essentially the same as Smaia except excluding the machine-level CSRs and behavior not directly visible to supervisor level."

Since midelegh is an AIA machine-mode CSR, add Smaia extension check in aia_smode32 predicate.

---
 target/riscv/csr.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
target/riscv/csr.c +5 -2
index 975d6e307f..c3dd8e6cda 100644
@@ -372,8 +372,11 @@ static RISCVException aia_smode(CPURISCVState *env, int csrno)
 static RISCVException aia_smode32(CPURISCVState *env, int csrno)
 {
     int ret;
+    int csr_priv;
 
-    if (!riscv_cpu_cfg(env)->ext_ssaia) {
+    if (csr_priv == PRV_M && !riscv_cpu_cfg(env)->ext_smaia) {
+        return RISCV_EXCP_ILLEGAL_INST;
+    } else if (!riscv_cpu_cfg(env)->ext_ssaia) {
         return RISCV_EXCP_ILLEGAL_INST;
     }
 
@@ -5832,7 +5835,7 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = {
     [CSR_MVIP]     = { "mvip",     aia_any, NULL, NULL, rmw_mvip    },
 
     /* Machine-Level High-Half CSRs (AIA) */
-    [CSR_MIDELEGH] = { "midelegh", aia_any32, NULL, NULL, rmw_midelegh },
+    [CSR_MIDELEGH] = { "midelegh", aia_smode32, NULL, NULL, rmw_midelegh },
     [CSR_MIEH]     = { "mieh",     aia_any32, NULL, NULL, rmw_mieh     },
     [CSR_MVIENH]   = { "mvienh",   aia_any32, NULL, NULL, rmw_mvienh   },
     [CSR_MVIPH]    = { "mviph",    aia_any32, NULL, NULL, rmw_mviph    },
Signature · 1 line
2.48.1
#2 lore raw

Re: [PATCH 2/2] target/riscv: Restrict midelegh access to S-mode harts

Quoted 23 lines · Jay Chang

RISC-V AIA Spec states:
"For a machine-level environment, extension Smaia encompasses all added CSRs and all modifications to interrupt response behavior that the AIA specifies for a hart, over all privilege levels. For a supervisor-level environment, extension Ssaia is essentially the same as Smaia except excluding the machine-level CSRs and behavior not directly visible to supervisor level."

Since midelegh is an AIA machine-mode CSR, add Smaia extension check in aia_smode32 predicate.

---
  target/riscv/csr.c | 7 +++++--
  1 file changed, 5 insertions(+), 2 deletions(-)
target/riscv/csr.c +0 -0
index 975d6e307f..c3dd8e6cda 100644
@@ -372,8 +372,11 @@ static RISCVException aia_smode(CPURISCVState *env, int csrno)
  static RISCVException aia_smode32(CPURISCVState *env, int csrno)

target/riscv/csr.c static RISCVException aia_smode(CPURISCVState *env, int csrno) · line 381

Quoted 10 lines
target/riscv/csr.c +4 -1
  {
      int ret;
+    int csr_priv;
 
-    if (!riscv_cpu_cfg(env)->ext_ssaia) {
+    if (csr_priv == PRV_M && !riscv_cpu_cfg(env)->ext_smaia) {
+        return RISCV_EXCP_ILLEGAL_INST;
+    } else if (!riscv_cpu_cfg(env)->ext_ssaia) {
          return RISCV_EXCP_ILLEGAL_INST;
      }

I believe this won't compile:

../target/riscv/csr.c: In function ‘aia_smode32’:
../target/riscv/csr.c:377:8: error: ‘csr_priv’ is used uninitialized [-Werror=uninitialized]
377 | if (csr_priv == PRV_M && !riscv_cpu_cfg(env)->ext_smaia) {
| ^
../target/riscv/csr.c:375:9: note: ‘csr_priv’ was declared here

  375 |     int csr_priv;
      |         ^~~~~~~~

cc1: all warnings being treated as errors
[2171/2988] Compiling C object libqemu-riscv64-softmmu.a.p/target_riscv_translate.c.o
ninja: build stopped: subcommand failed.

Perhaps the idea here is doing a "int csr_priv = env->priv;", but in that case you might as well just use env->priv directly. Thanks,

Daniel

Quoted 10 lines
target/riscv/csr.c +1 -1
@@ -5832,7 +5835,7 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = {
      [CSR_MVIP]     = { "mvip",     aia_any, NULL, NULL, rmw_mvip    },
 
      /* Machine-Level High-Half CSRs (AIA) */
-    [CSR_MIDELEGH] = { "midelegh", aia_any32, NULL, NULL, rmw_midelegh },
+    [CSR_MIDELEGH] = { "midelegh", aia_smode32, NULL, NULL, rmw_midelegh },
      [CSR_MIEH]     = { "mieh",     aia_any32, NULL, NULL, rmw_mieh     },
      [CSR_MVIENH]   = { "mvienh",   aia_any32, NULL, NULL, rmw_mvienh   },
      [CSR_MVIPH]    = { "mviph",    aia_any32, NULL, NULL, rmw_mviph    },
author
#3 lore raw

Re: [PATCH 2/2] target/riscv: Restrict midelegh access to S-mode harts

Thanks for the feedback!

You're right — I missed initializing csr_priv.
In this case, we only want to check for privilege M-mode CSRs along with the smaia condition.
I’ll fix it by adding:

"int csr_priv = get_field(csrno, 0x300);"

Jay

Quoted 78 lines · Daniel Henrique Barboza

target/riscv/csr.c static RISCVException aia_smode(CPURISCVState *env,

Quoted 22 lines · Jay Chang

RISC-V AIA Spec states:
"For a machine-level environment, extension Smaia encompasses all added CSRs and all modifications to interrupt response behavior that the AIA specifies for a hart, over all privilege levels. For a supervisor-level environment, extension Ssaia is essentially the same as Smaia except excluding the machine-level CSRs and behavior not directly visible to supervisor level."

Since midelegh is an AIA machine-mode CSR, add Smaia extension check in aia_smode32 predicate.

---
  target/riscv/csr.c | 7 +++++--
  1 file changed, 5 insertions(+), 2 deletions(-)
target/riscv/csr.c +0 -0
index 975d6e307f..c3dd8e6cda 100644
@@ -372,8 +372,11 @@ static RISCVException aia_smode(CPURISCVState *env,

int csrno)

target/riscv/csr.c static RISCVException aia_smode(CPURISCVState *env, int csrno) · line 381

Quoted 11 lines
target/riscv/csr.c +4 -1
  static RISCVException aia_smode32(CPURISCVState *env, int csrno)
  {
      int ret;
+    int csr_priv;
 
-    if (!riscv_cpu_cfg(env)->ext_ssaia) {
+    if (csr_priv == PRV_M && !riscv_cpu_cfg(env)->ext_smaia) {
+        return RISCV_EXCP_ILLEGAL_INST;
+    } else if (!riscv_cpu_cfg(env)->ext_ssaia) {
          return RISCV_EXCP_ILLEGAL_INST;
      }

I believe this won't compile:

../target/riscv/csr.c: In function ‘aia_smode32’:
../target/riscv/csr.c:377:8: error: ‘csr_priv’ is used uninitialized [-Werror=uninitialized]
377 | if (csr_priv == PRV_M && !riscv_cpu_cfg(env)->ext_smaia) {
| ^
../target/riscv/csr.c:375:9: note: ‘csr_priv’ was declared here

   375 |     int csr_priv;
       |         ^~~~~~~~

cc1: all warnings being treated as errors [2171/2988] Compiling C object libqemu-riscv64-softmmu.a.p/target_riscv_translate.c.o
ninja: build stopped: subcommand failed.

Perhaps the idea here is doing a "int csr_priv = env->priv;", but in that case you
might as well just use env->priv directly. Thanks,

Daniel

target/riscv/csr.c riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { · line 5835

Quoted 6 lines
target/riscv/csr.c +0 -1
@@ -5832,7 +5835,7 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = {
      [CSR_MVIP]     = { "mvip",     aia_any, NULL, NULL, rmw_mvip    },
 
      /* Machine-Level High-Half CSRs (AIA) */
-    [CSR_MIDELEGH] = { "midelegh", aia_any32, NULL, NULL, rmw_midelegh

},

target/riscv/csr.c riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { · line 5838

Quoted 1 line
target/riscv/csr.c +1 -0
+    [CSR_MIDELEGH] = { "midelegh", aia_smode32, NULL, NULL,

rmw_midelegh },

target/riscv/csr.c riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { · line 5839

Quoted 1 line
target/riscv/csr.c +0 -0
      [CSR_MIEH]     = { "mieh",     aia_any32, NULL, NULL, rmw_mieh

},

target/riscv/csr.c riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { · line 5840

Quoted 1 line
target/riscv/csr.c +0 -0
      [CSR_MVIENH]   = { "mvienh",   aia_any32, NULL, NULL, rmw_mvienh

},

target/riscv/csr.c riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { · line 5841

Quoted 1 line
target/riscv/csr.c +0 -0
      [CSR_MVIPH]    = { "mviph",    aia_any32, NULL, NULL, rmw_mviph

},