Skip to contentSkip to conversation

target/riscv: Reject Svinval instructions in U-mode

PATCH Applied Zephyr Li 4 participants 5 messages May 22 – May 27 RISC-V

lore
Share & tools
lore

Zephyr Li

author
#0 lore raw

PATCH v2 1/1 target/riscv: Reject Svinval instructions in U-mode

The RISC-V privileged specification requires SFENCE.W.INVAL and SFENCE.INVAL.IR to raise an illegal instruction exception when executed in U-mode. Check the current privilege mode during translation and reject these instructions in U-mode, so they are reported as illegal instructions.

Add a helper to reject these instructions in U-mode during translation.

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

---
 target/riscv/insn_trans/trans_svinval.c.inc | 12 ++++++++++++
 1 file changed, 12 insertions(+)
target/riscv/insn_trans/trans_svinval.c.inc +12 -0
index a06c3b214f..4614c1489c 100644
@@ -22,11 +22,19 @@
     }                                      \
 } while (0)
 
+/* Test if priv level is M or S. */
+#define REQUIRE_PRIV_MS(ctx) do {          \
+    if (ctx->priv == PRV_U) {              \
+        return false;                      \
+    }                                      \
+} while (0)
+
 static bool trans_sinval_vma(DisasContext *ctx, arg_sinval_vma *a)
 {
     REQUIRE_SVINVAL(ctx);
     /* Do the same as sfence.vma currently */
     REQUIRE_EXT(ctx, RVS);
+    REQUIRE_PRIV_MS(ctx);
 #ifndef CONFIG_USER_ONLY
     decode_save_opc(ctx, 0);
     gen_helper_tlb_flush(tcg_env);
@@ -39,6 +47,7 @@ static bool trans_sfence_w_inval(DisasContext *ctx, arg_sfence_w_inval *a)
 {
     REQUIRE_SVINVAL(ctx);
     REQUIRE_EXT(ctx, RVS);
+    REQUIRE_PRIV_MS(ctx);
     /* Do nothing currently */
     return true;
 }
@@ -47,6 +56,7 @@ static bool trans_sfence_inval_ir(DisasContext *ctx, arg_sfence_inval_ir *a)
 {
     REQUIRE_SVINVAL(ctx);
     REQUIRE_EXT(ctx, RVS);
+    REQUIRE_PRIV_MS(ctx);
     /* Do nothing currently */
     return true;
 }
@@ -56,6 +66,7 @@ static bool trans_hinval_vvma(DisasContext *ctx, arg_hinval_vvma *a)
     REQUIRE_SVINVAL(ctx);
     /* Do the same as hfence.vvma currently */
     REQUIRE_EXT(ctx, RVH);
+    REQUIRE_PRIV_MS(ctx);
 #ifndef CONFIG_USER_ONLY
     decode_save_opc(ctx, 0);
     gen_helper_hyp_tlb_flush(tcg_env);
@@ -69,6 +80,7 @@ static bool trans_hinval_gvma(DisasContext *ctx, arg_hinval_gvma *a)
     REQUIRE_SVINVAL(ctx);
     /* Do the same as hfence.gvma currently */
     REQUIRE_EXT(ctx, RVH);
+    REQUIRE_PRIV_MS(ctx);
 #ifndef CONFIG_USER_ONLY
     decode_save_opc(ctx, 0);
     gen_helper_hyp_gvma_tlb_flush(tcg_env);
Signature · 1 line
2.43.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 5 lines · Zephyr Li

The RISC-V privileged specification requires SFENCE.W.INVAL and SFENCE.INVAL.IR to raise an illegal instruction exception when executed in U-mode. Check the current privilege mode during translation and reject these instructions in U-mode, so they are reported as illegal instructions.

Quoted 8 lines

Add a helper to reject these instructions in U-mode during translation.

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

---

Quoted 59 lines
  target/riscv/insn_trans/trans_svinval.c.inc | 12 ++++++++++++
  1 file changed, 12 insertions(+)
target/riscv/insn_trans/trans_svinval.c.inc +12 -0
index a06c3b214f..4614c1489c 100644
@@ -22,11 +22,19 @@
      }                                      \
  } while (0)
 
+/* Test if priv level is M or S. */
+#define REQUIRE_PRIV_MS(ctx) do {          \
+    if (ctx->priv == PRV_U) {              \
+        return false;                      \
+    }                                      \
+} while (0)
+
  static bool trans_sinval_vma(DisasContext *ctx, arg_sinval_vma *a)
  {
      REQUIRE_SVINVAL(ctx);
      /* Do the same as sfence.vma currently */
      REQUIRE_EXT(ctx, RVS);
+    REQUIRE_PRIV_MS(ctx);
  #ifndef CONFIG_USER_ONLY
      decode_save_opc(ctx, 0);
      gen_helper_tlb_flush(tcg_env);
@@ -39,6 +47,7 @@ static bool trans_sfence_w_inval(DisasContext *ctx, arg_sfence_w_inval *a)
  {
      REQUIRE_SVINVAL(ctx);
      REQUIRE_EXT(ctx, RVS);
+    REQUIRE_PRIV_MS(ctx);
      /* Do nothing currently */
      return true;
  }
@@ -47,6 +56,7 @@ static bool trans_sfence_inval_ir(DisasContext *ctx, arg_sfence_inval_ir *a)
  {
      REQUIRE_SVINVAL(ctx);
      REQUIRE_EXT(ctx, RVS);
+    REQUIRE_PRIV_MS(ctx);
      /* Do nothing currently */
      return true;
  }
@@ -56,6 +66,7 @@ static bool trans_hinval_vvma(DisasContext *ctx, arg_hinval_vvma *a)
      REQUIRE_SVINVAL(ctx);
      /* Do the same as hfence.vvma currently */
      REQUIRE_EXT(ctx, RVH);
+    REQUIRE_PRIV_MS(ctx);
  #ifndef CONFIG_USER_ONLY
      decode_save_opc(ctx, 0);
      gen_helper_hyp_tlb_flush(tcg_env);
@@ -69,6 +80,7 @@ static bool trans_hinval_gvma(DisasContext *ctx, arg_hinval_gvma *a)
      REQUIRE_SVINVAL(ctx);
      /* Do the same as hfence.gvma currently */
      REQUIRE_EXT(ctx, RVH);
+    REQUIRE_PRIV_MS(ctx);
  #ifndef CONFIG_USER_ONLY
      decode_save_opc(ctx, 0);
      gen_helper_hyp_gvma_tlb_flush(tcg_env);
Reviewed-by on v2 1/1
#2 lore raw
In reply to Zephyr Li
Quoted 5 lines · Zephyr Li

The RISC-V privileged specification requires SFENCE.W.INVAL and SFENCE.INVAL.IR to raise an illegal instruction exception when executed in U-mode. Check the current privilege mode during translation and reject these instructions in U-mode, so they are reported as illegal instructions.

Quoted 7 lines

Add a helper to reject these instructions in U-mode during translation.

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

Quoted 63 lines
---
 target/riscv/insn_trans/trans_svinval.c.inc | 12 ++++++++++++
 1 file changed, 12 insertions(+)
target/riscv/insn_trans/trans_svinval.c.inc +12 -0
index a06c3b214f..4614c1489c 100644
@@ -22,11 +22,19 @@
     }                                      \
 } while (0)
 
+/* Test if priv level is M or S. */
+#define REQUIRE_PRIV_MS(ctx) do {          \
+    if (ctx->priv == PRV_U) {              \
+        return false;                      \
+    }                                      \
+} while (0)
+
 static bool trans_sinval_vma(DisasContext *ctx, arg_sinval_vma *a)
 {
     REQUIRE_SVINVAL(ctx);
     /* Do the same as sfence.vma currently */
     REQUIRE_EXT(ctx, RVS);
+    REQUIRE_PRIV_MS(ctx);
 #ifndef CONFIG_USER_ONLY
     decode_save_opc(ctx, 0);
     gen_helper_tlb_flush(tcg_env);
@@ -39,6 +47,7 @@ static bool trans_sfence_w_inval(DisasContext *ctx, arg_sfence_w_inval *a)
 {
     REQUIRE_SVINVAL(ctx);
     REQUIRE_EXT(ctx, RVS);
+    REQUIRE_PRIV_MS(ctx);
     /* Do nothing currently */
     return true;
 }
@@ -47,6 +56,7 @@ static bool trans_sfence_inval_ir(DisasContext *ctx, arg_sfence_inval_ir *a)
 {
     REQUIRE_SVINVAL(ctx);
     REQUIRE_EXT(ctx, RVS);
+    REQUIRE_PRIV_MS(ctx);
     /* Do nothing currently */
     return true;
 }
@@ -56,6 +66,7 @@ static bool trans_hinval_vvma(DisasContext *ctx, arg_hinval_vvma *a)
     REQUIRE_SVINVAL(ctx);
     /* Do the same as hfence.vvma currently */
     REQUIRE_EXT(ctx, RVH);
+    REQUIRE_PRIV_MS(ctx);
 #ifndef CONFIG_USER_ONLY
     decode_save_opc(ctx, 0);
     gen_helper_hyp_tlb_flush(tcg_env);
@@ -69,6 +80,7 @@ static bool trans_hinval_gvma(DisasContext *ctx, arg_hinval_gvma *a)
     REQUIRE_SVINVAL(ctx);
     /* Do the same as hfence.gvma currently */
     REQUIRE_EXT(ctx, RVH);
+    REQUIRE_PRIV_MS(ctx);
 #ifndef CONFIG_USER_ONLY
     decode_save_opc(ctx, 0);
     gen_helper_hyp_gvma_tlb_flush(tcg_env);
Signature · 1 line
2.43.0
Reviewed-by on v2 1/1
#3 lore raw
In reply to Zephyr Li
Quoted 10 lines · Zephyr Li

The RISC-V privileged specification requires SFENCE.W.INVAL and SFENCE.INVAL.IR to raise an illegal instruction exception when executed
in U-mode. Check the current privilege mode during translation and reject these
instructions in U-mode, so they are reported as illegal instructions.

Add a helper to reject these instructions in U-mode during translation.

Quoted 6 lines

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

Suggested-by: Daniel Henrique Barboza
<[email protected]>

Alistair

Quoted 65 lines

---
 target/riscv/insn_trans/trans_svinval.c.inc | 12 ++++++++++++

 1 file changed, 12 insertions(+)
target/riscv/insn_trans/trans_svinval.c.inc +0 -0
index a06c3b214f..4614c1489c 100644
@@ -22,11 +22,19 @@
      }                                      \
  } while (0)

target/riscv/insn_trans/trans_svinval.c.inc +7 -0
+/* Test if priv level is M or S. */
+#define REQUIRE_PRIV_MS(ctx) do {          \
+    if (ctx->priv == PRV_U) {              \
+        return false;                      \
+    }                                      \
+} while (0)
+

 static bool trans_sinval_vma(DisasContext *ctx, arg_sinval_vma *a) {
     REQUIRE_SVINVAL(ctx);
     /* Do the same as sfence.vma currently */ REQUIRE_EXT(ctx, RVS);
+    REQUIRE_PRIV_MS(ctx);
 #ifndef CONFIG_USER_ONLY
     decode_save_opc(ctx, 0); gen_helper_tlb_flush(tcg_env);

target/riscv/insn_trans/trans_svinval.c.inc +1 -0
@@ -39,6 +47,7 @@ static bool trans_sfence_w_inval(DisasContext *ctx,
 arg_sfence_w_inval *a)
  {
      REQUIRE_SVINVAL(ctx);
      REQUIRE_EXT(ctx, RVS);
+    REQUIRE_PRIV_MS(ctx);
      /* Do nothing currently */
      return true;

 }

target/riscv/insn_trans/trans_svinval.c.inc +1 -0
@@ -47,6 +56,7 @@ static bool trans_sfence_inval_ir(DisasContext
 *ctx, arg_sfence_inval_ir *a)
  {
      REQUIRE_SVINVAL(ctx);
      REQUIRE_EXT(ctx, RVS);
+    REQUIRE_PRIV_MS(ctx);
      /* Do nothing currently */
      return true;

 }

target/riscv/insn_trans/trans_svinval.c.inc +1 -0
@@ -56,6 +66,7 @@ static bool trans_hinval_vvma(DisasContext *ctx,
 arg_hinval_vvma *a)
      REQUIRE_SVINVAL(ctx);
      /* Do the same as hfence.vvma currently */
      REQUIRE_EXT(ctx, RVH);
+    REQUIRE_PRIV_MS(ctx);
  #ifndef CONFIG_USER_ONLY
      decode_save_opc(ctx, 0);

     gen_helper_hyp_tlb_flush(tcg_env);

target/riscv/insn_trans/trans_svinval.c.inc +1 -0
@@ -69,6 +80,7 @@ static bool trans_hinval_gvma(DisasContext *ctx,
 arg_hinval_gvma *a)
      REQUIRE_SVINVAL(ctx);
      /* Do the same as hfence.gvma currently */
      REQUIRE_EXT(ctx, RVH);
+    REQUIRE_PRIV_MS(ctx);
  #ifndef CONFIG_USER_ONLY
      decode_save_opc(ctx, 0);

     gen_helper_hyp_gvma_tlb_flush(tcg_env);

#4 lore raw
In reply to Zephyr Li
Quoted 10 lines · Zephyr Li

The RISC-V privileged specification requires SFENCE.W.INVAL and SFENCE.INVAL.IR to raise an illegal instruction exception when executed
in U-mode. Check the current privilege mode during translation and reject these
instructions in U-mode, so they are reported as illegal instructions.

Add a helper to reject these instructions in U-mode during translation.

Quoted 6 lines

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

Suggested-by: Daniel Henrique Barboza
<[email protected]>

Thanks!

Applied to riscv-to-apply.next

Alistair

Quoted 65 lines

---
 target/riscv/insn_trans/trans_svinval.c.inc | 12 ++++++++++++

 1 file changed, 12 insertions(+)
target/riscv/insn_trans/trans_svinval.c.inc +0 -0
index a06c3b214f..4614c1489c 100644
@@ -22,11 +22,19 @@
      }                                      \
  } while (0)

target/riscv/insn_trans/trans_svinval.c.inc +7 -0
+/* Test if priv level is M or S. */
+#define REQUIRE_PRIV_MS(ctx) do {          \
+    if (ctx->priv == PRV_U) {              \
+        return false;                      \
+    }                                      \
+} while (0)
+

 static bool trans_sinval_vma(DisasContext *ctx, arg_sinval_vma *a) {
     REQUIRE_SVINVAL(ctx);
     /* Do the same as sfence.vma currently */ REQUIRE_EXT(ctx, RVS);
+    REQUIRE_PRIV_MS(ctx);
 #ifndef CONFIG_USER_ONLY
     decode_save_opc(ctx, 0); gen_helper_tlb_flush(tcg_env);

target/riscv/insn_trans/trans_svinval.c.inc +1 -0
@@ -39,6 +47,7 @@ static bool trans_sfence_w_inval(DisasContext *ctx,
 arg_sfence_w_inval *a)
  {
      REQUIRE_SVINVAL(ctx);
      REQUIRE_EXT(ctx, RVS);
+    REQUIRE_PRIV_MS(ctx);
      /* Do nothing currently */
      return true;

 }

target/riscv/insn_trans/trans_svinval.c.inc +1 -0
@@ -47,6 +56,7 @@ static bool trans_sfence_inval_ir(DisasContext
 *ctx, arg_sfence_inval_ir *a)
  {
      REQUIRE_SVINVAL(ctx);
      REQUIRE_EXT(ctx, RVS);
+    REQUIRE_PRIV_MS(ctx);
      /* Do nothing currently */
      return true;

 }

target/riscv/insn_trans/trans_svinval.c.inc +1 -0
@@ -56,6 +66,7 @@ static bool trans_hinval_vvma(DisasContext *ctx,
 arg_hinval_vvma *a)
      REQUIRE_SVINVAL(ctx);
      /* Do the same as hfence.vvma currently */
      REQUIRE_EXT(ctx, RVH);
+    REQUIRE_PRIV_MS(ctx);
  #ifndef CONFIG_USER_ONLY
      decode_save_opc(ctx, 0);

     gen_helper_hyp_tlb_flush(tcg_env);

target/riscv/insn_trans/trans_svinval.c.inc +1 -0
@@ -69,6 +80,7 @@ static bool trans_hinval_gvma(DisasContext *ctx,
 arg_hinval_gvma *a)
      REQUIRE_SVINVAL(ctx);
      /* Do the same as hfence.gvma currently */
      REQUIRE_EXT(ctx, RVH);
+    REQUIRE_PRIV_MS(ctx);
  #ifndef CONFIG_USER_ONLY
      decode_save_opc(ctx, 0);

     gen_helper_hyp_gvma_tlb_flush(tcg_env);