Skip to contentSkip to conversation

target/riscv: Exit current TB after an sfence.vma

PATCH Applied Idan Horowitz 3 participants 5 messages Mar 15, 2022 – Mar 30, 2022 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/riscv: Exit current TB after an sfence.vma

If the pages which control the translation of the currently executing instructions are changed, and then the TLB is flushed using sfence.vma we have to exit the current TB early, to ensure we don't execute stale instructions.

---
 target/riscv/insn_trans/trans_privileged.c.inc | 7 +++++++
 1 file changed, 7 insertions(+)
target/riscv/insn_trans/trans_privileged.c.inc +7 -0
index 53613682e8..f265e8202d 100644
@@ -114,6 +114,13 @@ static bool trans_sfence_vma(DisasContext *ctx, arg_sfence_vma *a)
 {
 #ifndef CONFIG_USER_ONLY
     gen_helper_tlb_flush(cpu_env);
+    /*
+     * The flush might have changed the backing physical memory of
+     * the instructions we're currently executing
+     */
+    gen_set_pc_imm(ctx, ctx->pc_succ_insn);
+    tcg_gen_exit_tb(NULL, 0);
+    ctx->base.is_jmp = DISAS_NORETURN;
     return true;
 #endif
     return false;
Signature · 1 line
2.35.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Idan Horowitz

If the pages which control the translation of the currently executing instructions are changed, and then the TLB is flushed using sfence.vma we have to exit the current TB early, to ensure we don't execute stale instructions.

---
  target/riscv/insn_trans/trans_privileged.c.inc | 7 +++++++
  1 file changed, 7 insertions(+)

r~

Reviewed-by on v1 1/1
#2 lore raw
In reply to Idan Horowitz
Quoted 7 lines · Idan Horowitz

If the pages which control the translation of the currently executing instructions are changed, and then the TLB is flushed using sfence.vma we have to exit the current TB early, to ensure we don't execute stale instructions.

Alistair

Quoted 26 lines
---
 target/riscv/insn_trans/trans_privileged.c.inc | 7 +++++++
 1 file changed, 7 insertions(+)
target/riscv/insn_trans/trans_privileged.c.inc +7 -1
index 53613682e8..f265e8202d 100644
@@ -114,6 +114,13 @@ static bool trans_sfence_vma(DisasContext *ctx, arg_sfence_vma *a)
 {
 #ifndef CONFIG_USER_ONLY
     gen_helper_tlb_flush(cpu_env);
+    /*
+     * The flush might have changed the backing physical memory of
+     * the instructions we're currently executing
+     */
+    gen_set_pc_imm(ctx, ctx->pc_succ_insn);
+    tcg_gen_exit_tb(NULL, 0);
+    ctx->base.is_jmp = DISAS_NORETURN;
     return true;
 #endif
     return false;
--

2.35.1

#3 lore raw
In reply to Idan Horowitz
Quoted 7 lines · Idan Horowitz

If the pages which control the translation of the currently executing instructions are changed, and then the TLB is flushed using sfence.vma we have to exit the current TB early, to ensure we don't execute stale instructions.

Thanks!

Applied to riscv-to-apply.next

Alistair

Quoted 26 lines
---
 target/riscv/insn_trans/trans_privileged.c.inc | 7 +++++++
 1 file changed, 7 insertions(+)
target/riscv/insn_trans/trans_privileged.c.inc +7 -1
index 53613682e8..f265e8202d 100644
@@ -114,6 +114,13 @@ static bool trans_sfence_vma(DisasContext *ctx, arg_sfence_vma *a)
 {
 #ifndef CONFIG_USER_ONLY
     gen_helper_tlb_flush(cpu_env);
+    /*
+     * The flush might have changed the backing physical memory of
+     * the instructions we're currently executing
+     */
+    gen_set_pc_imm(ctx, ctx->pc_succ_insn);
+    tcg_gen_exit_tb(NULL, 0);
+    ctx->base.is_jmp = DISAS_NORETURN;
     return true;
 #endif
     return false;
--

2.35.1

#4 lore raw
Quoted 8 lines · Alistair Francis
Quoted 6 lines · Idan Horowitz

If the pages which control the translation of the currently executing instructions are changed, and then the TLB is flushed using sfence.vma we have to exit the current TB early, to ensure we don't execute stale instructions.

Quoted 5 lines
Quoted 1 line

Thanks!

Applied to riscv-to-apply.next

This results in failed Linux boots, I have dropped this patch

Alistair