Skip to contentSkip to conversation

target/riscv/tcg: sret in virtual user mode raises virtual instruction exception

PATCH In review Christian S. Lima 2 participants 2 messages Aug 8 – Aug 20 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/riscv/tcg: sret in virtual user mode raises virtual instruction exception

Currently, when a sret is executed in virtual user mode, qemu raise an illegal instruction exception, but in this case the correct behavior is to raise a virtual instruction exception and the code already contains a check to it, but it's not enough to catch. This patch is useful to improve the correctness of the virtualization of the risc v architecture.

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

---
 target/riscv/tcg/op_helper.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
target/riscv/tcg/op_helper.c +4 -4
index ba3c7da375..429a6186a7 100644
@@ -293,6 +293,10 @@ target_ulong helper_sret(CPURISCVState *env)
     const privilege_mode_t src_priv = env->priv;
     const bool src_virt = env->virt_enabled;
 
+    if ((env->virt_enabled && env->priv < PRV_S) || (env->virt_enabled && get_field(env->hstatus, HSTATUS_VTSR))) {
+        riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, GETPC());
+    }
+
     if (!(env->priv >= PRV_S)) {
         riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
     }
@@ -308,10 +312,6 @@ target_ulong helper_sret(CPURISCVState *env)
         riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
     }
 
-    if (env->virt_enabled && get_field(env->hstatus, HSTATUS_VTSR)) {
-        riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, GETPC());
-    }
-
     mstatus = env->mstatus;
     prev_priv = get_field(mstatus, MSTATUS_SPP);
     mstatus = set_field(mstatus, MSTATUS_SIE,
Signature · 1 line
2.53.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Christian S. Lima

Currently, when a sret is executed in virtual user mode, qemu raise an illegal instruction exception, but in this case the correct
behavior is to raise a virtual instruction exception and the code already contains a check to it, but it's not enough to catch. This patch is useful to improve the correctness of the virtualization of the risc v architecture.

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

Alistair

Quoted 37 lines

---
 target/riscv/tcg/op_helper.c | 8 ++++----

 1 file changed, 4 insertions(+), 4 deletions(-)
target/riscv/tcg/op_helper.c +0 -0
index ba3c7da375..429a6186a7 100644
@@ -293,6 +293,10 @@ target_ulong helper_sret(CPURISCVState *env)
      const privilege_mode_t src_priv = env->priv;
      const bool src_virt = env->virt_enabled;


+    if ((env->virt_enabled && env->priv < PRV_S) || (env-

Quoted 1 line

virt_enabled && get_field(env->hstatus, HSTATUS_VTSR))) {

+        riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, GETPC());
+    }
+
     if (!(env->priv >= PRV_S)) { riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
     }

target/riscv/tcg/op_helper.c +0 -0
@@ -308,10 +312,6 @@ target_ulong helper_sret(CPURISCVState *env)
          riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST,
 GETPC());
      }


-    if (env->virt_enabled && get_field(env->hstatus, HSTATUS_VTSR)) {
-        riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, GETPC());
-    }
-
     mstatus = env->mstatus;
     prev_priv = get_field(mstatus, MSTATUS_SPP);
     mstatus = set_field(mstatus, MSTATUS_SIE,