>Hi,
>
>On 9/11/26 14:56, [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]>
>> ---
>>   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;
>

Reply via email to