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(-)
From: Fangyu Yu <fangyu.yu@linux.alibaba.com>
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 <fangyu.yu@linux.alibaba.com>
---
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;
--
2.50.1
On 11/9/2026 10:56 AM, fangyu.yu@linux.alibaba.com wrote:
> From: Fangyu Yu <fangyu.yu@linux.alibaba.com>
>
> 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 <fangyu.yu@linux.alibaba.com>
> ---
> 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);
Please add a comment before this gen_update_pc() call explaining that env->pc
is deliberately left on the WFI until the exception checks pass. I know this is
explained in the commit msg, but in this case it's way too easy for someone else to
read this code. assume that this gen_update_pc() isn't intended and then try to
advance PC back again.
LGTM otherwise. Thanks,
Daniel
> + 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;
>On 11/9/2026 10:56 AM, fangyu.yu@linux.alibaba.com wrote:
>> From: Fangyu Yu <fangyu.yu@linux.alibaba.com>
>>
>> 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 <fangyu.yu@linux.alibaba.com>
>> ---
>> 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);
>
>Please add a comment before this gen_update_pc() call explaining that env->pc
>is deliberately left on the WFI until the exception checks pass. I know this is
>explained in the commit msg, but in this case it's way too easy for someone else to
>read this code. assume that this gen_update_pc() isn't intended and then try to
>advance PC back again.
>
Good point. I will add a comment above gen_update_pc()
explaining that env->pc is intentionally left on the WFI,
and the helper advances it only after all exception checks
pass.
Thanks,
Fangyu
>
>LGTM otherwise. Thanks,
>Daniel
>
>> + 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;
>
Hi,
On 9/11/26 14:56, fangyu.yu@linux.alibaba.com wrote:
> From: Fangyu Yu <fangyu.yu@linux.alibaba.com>
>
> 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 <fangyu.yu@linux.alibaba.com>
> ---
> 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;
> + }
While I'm still not convinced by the fix, 'advance_pc' might
be clearer than 'insn_len'; also we have helper to set $PC:
riscv_cpu_set_pc(env->pc + advance_pc)
> if (riscv_cpu_cfg(env)->ext_sdext && !env->debug_mode &&
> (env->dcsr & DCSR_STEP)) {
> return;
>Hi,
>
>On 9/11/26 14:56, fangyu.yu@linux.alibaba.com wrote:
>> From: Fangyu Yu <fangyu.yu@linux.alibaba.com>
>>
>> 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 <fangyu.yu@linux.alibaba.com>
>> ---
>> 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;
>> + }
>
>While I'm still not convinced by the fix, 'advance_pc' might
>be clearer than 'insn_len'; also we have helper to set $PC:
>
> riscv_cpu_set_pc(env->pc + advance_pc)
>
Thanks for the review.
The problem is the ordering between env->pc advancement and exception
raising. helper_wfi() can raise both ILLEGAL_INST and
VIRT_INSTRUCTION_FAULT via GETPC(), and the exception PC must be the
WFI's own PC. With CF_PCREL, the translator restores the faulting PC
from env->pc's page base plus the recorded page offset. trans_wfi()
used to advance env->pc before the helper call, so when WFI is the
last instruction of a page (or crosses a page boundary), the restore
takes the next page's base and the reported sepc/tval is one page too
high.
For example, with CF_PCREL and 4 KiB pages, a 4-byte WFI at
0xffffffff80000ffc (page offset 0xffc): trans_wfi() advances
env->pc to 0xffffffff80001000 before the helper raises
VIRT_INSTRUCTION_FAULT. PC restoration then computes
(env->pc & PAGE_MASK) | 0xffc = 0xffffffff80001ffc, which is one page
above the real WFI PC. Keeping env->pc on the WFI until after all
exception paths makes the restoration return 0xffffffff80000ffc, the
correct sepc.
I will rename insn_len to advance_pc in the next version. As for
riscv_cpu_set_pc(), it is a static function, maybe we should leave it
unchanged for now.
Thanks,
Fangyu
>> if (riscv_cpu_cfg(env)->ext_sdext && !env->debug_mode &&
>> (env->dcsr & DCSR_STEP)) {
>> return;
>
On 11/9/26 9:56 PM, fangyu.yu@linux.alibaba.com wrote:
> From: Fangyu Yu <fangyu.yu@linux.alibaba.com>
>
> 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 <fangyu.yu@linux.alibaba.com>
Reviewed-by: LIU Zhiwei <zhiwei_liu@linux.alibaba.com>
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;
On Mon, Nov 9, 2026 at 9:56 PM <fangyu.yu@linux.alibaba.com> wrote:
>
> From: Fangyu Yu <fangyu.yu@linux.alibaba.com>
>
> trans_wfi() advances env->pc before calling helper_wfi(), which may
> raise illegal or virtual-instruction exceptions through GETPC().
Yes, this is the problem. Good catch!
> 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 <fangyu.yu@linux.alibaba.com>
> ---
> 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)
Yes, WFI can be either a 16-bit compressed instruction or a 32-bit
instruction. The helper is runtime code and has no other way to know
the instruction length, so it must be passed in order to correctly
advance env->pc on the non-exception path. This approach is
reasonable.
> {
> 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;
uint64_t pc;
Yes, RV32's pc is sign-extended and stored in uint64_t.
Reviewed-by: GUO Ren (XuanTie) <guoren@kernel.org>
--
Best Regards
Guo Ren
© 2016 - 2026 Red Hat, Inc.