On 11/9/26 9:56 PM, [email protected] wrote:
From: Fangyu Yu <[email protected]>

trans_wfi() advances env->pc before calling helper_wfi(), which may
raise illegal or virtual-instruction exceptions through GETPC().
With CF_PCREL, restoring the instruction PC uses env->pc's page base.
If the next instruction is in a different page, the exception PC is
restored one page too high. This affects both a WFI ending exactly at
a page boundary and one crossing it.

Keep env->pc pointing at WFI while helper_wfi() can raise an exception,
and pass the instruction length to the helper. Advance env->pc only on
the normal single-step or halt paths.

Fixes: 356c13f94dbf ("target/riscv: Enable PC-relative translation")
Signed-off-by: Fangyu Yu <[email protected]>
Reviewed-by: LIU Zhiwei <[email protected]>

Zhiwei
---
  target/riscv/helper.h                              | 2 +-
  target/riscv/tcg/insn_trans/trans_privileged.c.inc | 4 ++--
  target/riscv/tcg/op_helper.c                       | 6 +++++-
  3 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/target/riscv/helper.h b/target/riscv/helper.h
index 1cffcad580..b36e3532aa 100644
--- a/target/riscv/helper.h
+++ b/target/riscv/helper.h
@@ -135,7 +135,7 @@ DEF_HELPER_1(mret, tl, env)
  DEF_HELPER_1(dret, tl, env)
  DEF_HELPER_1(mnret, tl, env)
  DEF_HELPER_1(ctr_clear, void, env)
-DEF_HELPER_1(wfi, void, env)
+DEF_HELPER_2(wfi, void, env, i32)
  DEF_HELPER_1(wrs_nto, void, env)
  DEF_HELPER_1(tlb_flush, void, env)
  DEF_HELPER_1(tlb_flush_all, void, env)
diff --git a/target/riscv/tcg/insn_trans/trans_privileged.c.inc 
b/target/riscv/tcg/insn_trans/trans_privileged.c.inc
index e22df6fbe7..3ca55b9842 100644
--- a/target/riscv/tcg/insn_trans/trans_privileged.c.inc
+++ b/target/riscv/tcg/insn_trans/trans_privileged.c.inc
@@ -167,8 +167,8 @@ static bool trans_wfi(DisasContext *ctx, arg_wfi *a)
  {
  #ifndef CONFIG_USER_ONLY
      decode_save_opc(ctx, 0);
-    gen_update_pc(ctx, ctx->cur_insn_len);
-    gen_helper_wfi(tcg_env);
+    gen_update_pc(ctx, 0);
+    gen_helper_wfi(tcg_env, tcg_constant_i32(ctx->cur_insn_len));
      ctx->base.is_jmp = DISAS_NORETURN;
      return true;
  #else
diff --git a/target/riscv/tcg/op_helper.c b/target/riscv/tcg/op_helper.c
index f9c79fe6b9..3f71258858 100644
--- a/target/riscv/tcg/op_helper.c
+++ b/target/riscv/tcg/op_helper.c
@@ -625,7 +625,7 @@ void HELPER(ctr_clear)(CPURISCVState *env)
      riscv_ctr_clear(env);
  }
-void HELPER(wfi)(CPURISCVState *env)
+void HELPER(wfi)(CPURISCVState *env, uint32_t insn_len)
  {
      CPUState *cs = env_cpu(env);
      bool rvs = riscv_has_ext(env, RVS);
@@ -639,6 +639,10 @@ void HELPER(wfi)(CPURISCVState *env)
                 (prv_u || (prv_s && get_field(env->hstatus, HSTATUS_VTW)))) {
          riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, 
GETPC());
      } else {
+        env->pc += insn_len;
+        if (env->xl == MXL_RV32) {
+            env->pc = (int32_t)env->pc;
+        }
          if (riscv_cpu_cfg(env)->ext_sdext && !env->debug_mode &&
              (env->dcsr & DCSR_STEP)) {
              return;

Reply via email to