Skip to content

Commit

Permalink
target/riscv: Update $ra with current $pc in trans_cm_jalt()
Browse files Browse the repository at this point in the history
The original implementation sets $pc to the address read from the jump
vector table first and links $ra with the address of the next instruction
after the updated $pc. After jumping to the updated $pc and executing the
next ret instruction, the program jumps to $ra, which is in the same
function currently executing, which results in an infinite loop.
This commit stores the jump address in a temporary, updates $ra with the
current $pc, and copies the temporary to $pc.

Signed-off-by: Jason Chien <jason.chien@sifive.com>
Reviewed-by: Frank Chang <frank.chang@sifive.com>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Message-ID: <20240207081820.28559-1-jason.chien@sifive.com>
Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
  • Loading branch information
Jason Chien authored and alistair23 committed Mar 8, 2024
1 parent 8f6330a commit 3011c1d
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion target/riscv/insn_trans/trans_rvzce.c.inc
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,14 @@ static bool trans_cm_jalt(DisasContext *ctx, arg_cm_jalt *a)
{
REQUIRE_ZCMT(ctx);

TCGv addr = tcg_temp_new();

/*
* Update pc to current for the non-unwinding exception
* that might come from cpu_ld*_code() in the helper.
*/
gen_update_pc(ctx, 0);
gen_helper_cm_jalt(cpu_pc, tcg_env, tcg_constant_i32(a->index));
gen_helper_cm_jalt(addr, tcg_env, tcg_constant_i32(a->index));

/* c.jt vs c.jalt depends on the index. */
if (a->index >= 32) {
Expand All @@ -307,6 +309,8 @@ static bool trans_cm_jalt(DisasContext *ctx, arg_cm_jalt *a)
gen_set_gpr(ctx, xRA, succ_pc);
}

tcg_gen_mov_tl(cpu_pc, addr);

tcg_gen_lookup_and_goto_ptr();
ctx->base.is_jmp = DISAS_NORETURN;
return true;
Expand Down

0 comments on commit 3011c1d

Please sign in to comment.