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: GUO Ren (XuanTie) <[email protected]>
Reviewed-by: LIU Zhiwei <[email protected]>
---
Changes in v2:
- Rename helper_wfi()'s insn_len parameter to advance_pc, to clarify
that it is the amount env->pc advances on the normal path.
- Add a comment before gen_update_pc() explaining that env->pc is
deliberately left on the WFI until the exception checks pass, so
nobody advances the PC in the translator again by mistake.
Link to v1:
https://lore.kernel.org/qemu-devel/[email protected]/
---
target/riscv/helper.h | 2 +-
target/riscv/tcg/insn_trans/trans_privileged.c.inc | 10 ++++++++--
target/riscv/tcg/op_helper.c | 6 +++++-
3 files changed, 14 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..bd1d8f276d 100644
--- a/target/riscv/tcg/insn_trans/trans_privileged.c.inc
+++ b/target/riscv/tcg/insn_trans/trans_privileged.c.inc
@@ -167,8 +167,14 @@ 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);
+ /*
+ * Deliberately leave env->pc on the WFI: helper_wfi() may raise an
+ * exception, and with CF_PCREL the exception PC is restored from
+ * env->pc's page base. The helper advances env->pc itself only after
+ * all exception checks pass.
+ */
+ 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..aebf383ed5 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 advance_pc)
{
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 += advance_pc;
+ 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;
--
2.50.1