Skip to contentSkip to conversation

target/riscv: Fix exception type when VU accesses supervisor CSRs

PATCH Applied Xu Lu 4 participants 5 messages Jul 8, 2025 – Jul 29, 2025 RISC-V

lore
Share & tools
lore

Xu Lu

author
#0 lore raw

PATCH 1/1 target/riscv: Fix exception type when VU accesses supervisor CSRs

When supervisor CSRs are accessed from VU-mode, a virtual instruction exception should be raised instead of an illegal instruction.

Fixes c1fbcecb3a (target/riscv: Fix csr number based privilege checking)
---
 target/riscv/csr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/riscv/csr.c +1 -1
index 8631be97c5..9bebfae3f0 100644
@@ -5577,7 +5577,7 @@ static inline RISCVException riscv_csrrw_check(CPURISCVState *env,
 
     csr_priv = get_field(csrno, 0x300);
     if (!env->debugger && (effective_priv < csr_priv)) {
-        if (csr_priv == (PRV_S + 1) && env->virt_enabled) {
+        if (csr_priv <= (PRV_S + 1) && env->virt_enabled) {
             return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
         }
         return RISCV_EXCP_ILLEGAL_INST;
Signature · 1 line
2.20.1
author
#1 lore raw

FYI, the discussion about whether vs insn fault or illegal insn fault should be raised can be found from [1].

[1] https://lists.riscv.org/g/tech-privileged/message/2469

Quoted 26 lines · Xu Lu

When supervisor CSRs are accessed from VU-mode, a virtual instruction exception should be raised instead of an illegal instruction.

Fixes c1fbcecb3a (target/riscv: Fix csr number based privilege checking)
---
 target/riscv/csr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/riscv/csr.c +1 -2
index 8631be97c5..9bebfae3f0 100644
@@ -5577,7 +5577,7 @@ static inline RISCVException riscv_csrrw_check(CPURISCVState *env,
 
     csr_priv = get_field(csrno, 0x300);
     if (!env->debugger && (effective_priv < csr_priv)) {
-        if (csr_priv == (PRV_S + 1) && env->virt_enabled) {
+        if (csr_priv <= (PRV_S + 1) && env->virt_enabled) {
             return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
         }
         return RISCV_EXCP_ILLEGAL_INST;
--

2.20.1

Reviewed-by on v1 1/1
#2 lore raw
In reply to Xu Lu
Quoted 6 lines · Xu Lu

When supervisor CSRs are accessed from VU-mode, a virtual instruction exception should be raised instead of an illegal instruction.

Fixes c1fbcecb3a (target/riscv: Fix csr number based privilege checking)

LGTM.

Regards,
Anup

Quoted 20 lines
---
 target/riscv/csr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/riscv/csr.c +1 -2
index 8631be97c5..9bebfae3f0 100644
@@ -5577,7 +5577,7 @@ static inline RISCVException riscv_csrrw_check(CPURISCVState *env,
 
     csr_priv = get_field(csrno, 0x300);
     if (!env->debugger && (effective_priv < csr_priv)) {
-        if (csr_priv == (PRV_S + 1) && env->virt_enabled) {
+        if (csr_priv <= (PRV_S + 1) && env->virt_enabled) {
             return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
         }
         return RISCV_EXCP_ILLEGAL_INST;
--

2.20.1

Reviewed-by on v1 1/1
#3 lore raw
In reply to Xu Lu
Quoted 14 lines · Xu Lu

When supervisor CSRs are accessed from VU-mode, a virtual instruction exception should be raised instead of an illegal instruction.

Fixes c1fbcecb3a (target/riscv: Fix csr number based privilege checking)
---
  target/riscv/csr.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
target/riscv/csr.c +0 -0
index 8631be97c5..9bebfae3f0 100644

target/riscv/csr.c static inline RISCVException riscv_csrrw_check(CPURISCVState *env, · line 5583

Quoted 8 lines
target/riscv/csr.c +1 -1
@@ -5577,7 +5577,7 @@ static inline RISCVException riscv_csrrw_check(CPURISCVState *env,
 
      csr_priv = get_field(csrno, 0x300);
      if (!env->debugger && (effective_priv < csr_priv)) {
-        if (csr_priv == (PRV_S + 1) && env->virt_enabled) {
+        if (csr_priv <= (PRV_S + 1) && env->virt_enabled) {
              return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
          }
          return RISCV_EXCP_ILLEGAL_INST;

Thanks,
Nutty

#4 lore raw
In reply to Xu Lu
Quoted 6 lines · Xu Lu

When supervisor CSRs are accessed from VU-mode, a virtual instruction exception should be raised instead of an illegal instruction.

Fixes c1fbcecb3a (target/riscv: Fix csr number based privilege checking)

Thanks!

Applied to riscv-to-apply.next

Alistair

Quoted 21 lines
---
 target/riscv/csr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/riscv/csr.c +1 -2
index 8631be97c5..9bebfae3f0 100644
@@ -5577,7 +5577,7 @@ static inline RISCVException riscv_csrrw_check(CPURISCVState *env,
 
     csr_priv = get_field(csrno, 0x300);
     if (!env->debugger && (effective_priv < csr_priv)) {
-        if (csr_priv == (PRV_S + 1) && env->virt_enabled) {
+        if (csr_priv <= (PRV_S + 1) && env->virt_enabled) {
             return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
         }
         return RISCV_EXCP_ILLEGAL_INST;
--

2.20.1