Skip to contentSkip to conversation

Add S-mode checks for delegation-related CSRs

PATCH Applied Jay Chang 3 participants 7 messages Jul 1, 2025 – Jul 29, 2025 RISC-V

lore
Share & tools
lore

Jay Chang

author
#0 lore raw

v3 cover Add S-mode checks for delegation-related CSRs

Patch 1 adds a predicate to restrict access to "medeleg, mideleg, and medelegh" to harts that support S-mode.

Patch 2 adds a privilege check for the "midelegh" CSR, which is defined by the AIA extension and only valid when Smaia is supported. This is enforced via an updated predicate in aia_smode32.

Change log:
V3:
* Add cover letter

Jay Chang (2):
target/riscv: Restrict mideleg/medeleg/medelegh access to S-mode harts target/riscv: Restrict midelegh access to S-mode harts

 target/riscv/csr.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
Signature · 1 line
2.48.1
author
#1 lore raw

PATCH v3 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 6296ecd1e1..0e0ad37654 100644
@@ -5862,8 +5862,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,
@@ -5871,7 +5871,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
Reviewed-by on v3 1/2
#2 lore raw

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

Quoted 33 lines · Jay Chang

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 +2 -2
index 6296ecd1e1..0e0ad37654 100644
@@ -5862,8 +5862,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,

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

Quoted 8 lines
target/riscv/csr.c +1 -1
@@ -5871,7 +5871,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              },

Thanks,
Nutty

author
#3 lore raw

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

In reply to 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 +5 -2
index 0e0ad37654..74ec0e1c60 100644
@@ -374,8 +374,11 @@ static RISCVException aia_smode(CPURISCVState *env, int csrno)
 static RISCVException aia_smode32(CPURISCVState *env, int csrno)
 {
     int ret;
+    int csr_priv = get_field(csrno, 0x300);
 
-    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;
     }
 
@@ -5911,7 +5914,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
Reviewed-by on v3 2/2
#4 lore raw

Re: [PATCH v3 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 0e0ad37654..74ec0e1c60 100644

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

Quoted 9 lines
target/riscv/csr.c +4 -1
@@ -374,8 +374,11 @@ static RISCVException aia_smode(CPURISCVState *env, int csrno)
  static RISCVException aia_smode32(CPURISCVState *env, int csrno)
  {
      int ret;
+    int csr_priv = get_field(csrno, 0x300);
 
-    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) {

+ if ((csr_priv == PRV_M && !riscv_cpu_cfg(env)->ext_smaia) ||
+ (!riscv_cpu_cfg(env)->ext_ssaia)) {

Would the above code be better ?
Otherwise,

Thanks,
Nutty

Quoted 12 lines
target/riscv/csr.c +1 -1
          return RISCV_EXCP_ILLEGAL_INST;
      }
 
@@ -5911,7 +5914,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
#5 lore raw

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

I intended to separate the S-mode and M-mode handling. Do you think this change could improve performance?

Thanks,
Jay Chang

Quoted 63 lines · Nutty Liu

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

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 0e0ad37654..74ec0e1c60 100644
@@ -374,8 +374,11 @@ static RISCVException aia_smode(CPURISCVState *env,

int csrno)

Quoted 9 lines
target/riscv/csr.c +4 -1
  static RISCVException aia_smode32(CPURISCVState *env, int csrno)
  {
      int ret;
+    int csr_priv = get_field(csrno, 0x300);
 
-    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) {
(quoted diff) +2 -0
+    if ((csr_priv == PRV_M && !riscv_cpu_cfg(env)->ext_smaia) ||
+        (!riscv_cpu_cfg(env)->ext_ssaia)) {

Would the above code be better ?
Otherwise,

Thanks,
Nutty

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

Quoted 8 lines
target/riscv/csr.c +0 -1
          return RISCV_EXCP_ILLEGAL_INST;
      }
 
@@ -5911,7 +5914,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 5917

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 5918

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 5919

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 5920

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

},

#6 lore raw
In reply to Jay Chang
Quoted 8 lines · Jay Chang

Patch 1 adds a predicate to restrict access to "medeleg, mideleg, and medelegh" to harts that support S-mode.

Patch 2 adds a privilege check for the "midelegh" CSR, which is defined by the AIA extension and only valid when Smaia is supported. This is enforced via an updated predicate in aia_smode32.

Quoted 7 lines

Change log:
V3:
* Add cover letter

Jay Chang (2):
target/riscv: Restrict mideleg/medeleg/medelegh access to S-mode harts target/riscv: Restrict midelegh access to S-mode harts

Thanks!

Applied to riscv-to-apply.next

Alistair

Quoted 8 lines
 target/riscv/csr.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

--
2.48.1