@samitouri / QOSamiQemu / commits / 6ba16ebfac

target/riscv: Fix size of ssp

As ssp holds a pointer, fix to 64 bits in size and make sure stores from TCG use the correct size to avoid problems on big endian hosts. Note, the cpu/ssp VMSTATE version is bumped, breaking migration from older versions. Signed-off-by: Anton Johansson <anjo@rev.ng> Reviewed-by: Pierrick Bouvier <pierrick.bouvier@linaro.org> Acked-by: Alistair Francis <alistair.francis@wdc.com> Message-ID: <20260520125406.28693-10-anjo@rev.ng> Signed-off-by: Alistair Francis <alistair.francis@wdc.com>

Anton Johansson committed May 20, 2026 at 14:53 UTC 6ba16ebfaccc89b36a748bffd4da2d4111c5326a
3 files changed +17 -9
target/riscv/cpu.h
+1 -1
@@ -257,7 +257,7 @@ struct CPUArchState {
257 /* elp state for zicfilp extension */
258 bool elp;
259 /* shadow stack register for zicfiss extension */
260 - target_ulong ssp;
260 + uint64_t ssp;
261 /* env place holder for extra word 2 during unwind */
262 target_ulong excp_uw2;
263 /* sw check code for sw check exception */
target/riscv/insn_trans/trans_rvzicfiss.c.inc
+13 -5
@@ -32,7 +32,9 @@ static bool trans_sspopchk(DisasContext *ctx, arg_sspopchk *a)
32 TCGLabel *skip = gen_new_label();
33 uint32_t tmp = (get_xl(ctx) == MXL_RV64) ? 8 : 4;
34 TCGv data = tcg_temp_new();
35 - tcg_gen_ld_tl(addr, tcg_env, offsetof(CPURISCVState, ssp));
35 + TCGv_i64 wide_addr = tcg_temp_new_i64();
36 + tcg_gen_ld_i64(wide_addr, tcg_env, offsetof(CPURISCVState, ssp));
37 + tcg_gen_trunc_i64_tl(addr, wide_addr);
38 decode_save_opc(ctx, RISCV_UW2_ALWAYS_STORE_AMO);
39 tcg_gen_qemu_ld_tl(data, addr, SS_MMU_INDEX(ctx),
40 mxl_memop(ctx) | MO_ALIGN);
@@ -45,7 +47,8 @@ static bool trans_sspopchk(DisasContext *ctx, arg_sspopchk *a)
47 tcg_constant_i32(RISCV_EXCP_SW_CHECK));
48 gen_set_label(skip);
49 tcg_gen_addi_tl(addr, addr, tmp);
48 - tcg_gen_st_tl(addr, tcg_env, offsetof(CPURISCVState, ssp));
50 + tcg_gen_ext_tl_i64(wide_addr, addr);
51 + tcg_gen_st_i64(wide_addr, tcg_env, offsetof(CPURISCVState, ssp));
52
53 return true;
54 }
@@ -59,12 +62,15 @@ static bool trans_sspush(DisasContext *ctx, arg_sspush *a)
62 TCGv addr = tcg_temp_new();
63 int tmp = (get_xl(ctx) == MXL_RV64) ? -8 : -4;
64 TCGv data = get_gpr(ctx, a->rs2, EXT_NONE);
65 + TCGv_i64 wide_addr = tcg_temp_new_i64();
66 decode_save_opc(ctx, RISCV_UW2_ALWAYS_STORE_AMO);
63 - tcg_gen_ld_tl(addr, tcg_env, offsetof(CPURISCVState, ssp));
67 + tcg_gen_ld_i64(wide_addr, tcg_env, offsetof(CPURISCVState, ssp));
68 + tcg_gen_trunc_i64_tl(addr, wide_addr);
69 tcg_gen_addi_tl(addr, addr, tmp);
70 tcg_gen_qemu_st_tl(data, addr, SS_MMU_INDEX(ctx),
71 mxl_memop(ctx) | MO_ALIGN);
67 - tcg_gen_st_tl(addr, tcg_env, offsetof(CPURISCVState, ssp));
72 + tcg_gen_ext_tl_i64(wide_addr, addr);
73 + tcg_gen_st_i64(wide_addr, tcg_env, offsetof(CPURISCVState, ssp));
74
75 return true;
76 }
@@ -76,7 +82,9 @@ static bool trans_ssrdp(DisasContext *ctx, arg_ssrdp *a)
82 }
83
84 TCGv dest = dest_gpr(ctx, a->rd);
79 - tcg_gen_ld_tl(dest, tcg_env, offsetof(CPURISCVState, ssp));
85 + TCGv_i64 wide_addr = tcg_temp_new_i64();
86 + tcg_gen_ld_i64(wide_addr, tcg_env, offsetof(CPURISCVState, ssp));
87 + tcg_gen_trunc_i64_tl(dest, wide_addr);
88 gen_set_gpr(ctx, a->rd, dest);
89
90 return true;
target/riscv/machine.c
+3 -3
@@ -391,11 +391,11 @@ static bool ssp_needed(void *opaque)
391
392 static const VMStateDescription vmstate_ssp = {
393 .name = "cpu/ssp",
394 - .version_id = 1,
395 - .minimum_version_id = 1,
394 + .version_id = 2,
395 + .minimum_version_id = 2,
396 .needed = ssp_needed,
397 .fields = (const VMStateField[]) {
398 - VMSTATE_UINTTL(env.ssp, RISCVCPU),
398 + VMSTATE_UINT64(env.ssp, RISCVCPU),
399 VMSTATE_END_OF_LIST()
400 }
401 };