target/riscv: Call probe_write() before atomic operations
PATCH 1/1 target/riscv: Call probe_write() before atomic operations
From: Alistair Francis <[email protected]>
If an atomic operation fails on RISC-V we want to generate a store/amo fault and not a load fault.
Currently if we have no permissions to access the memory location the atomic operation will sometimes fail with a load fault (depending on the path taken in tcg/tcg-op.c) as the atomic helpers perform a load then a store.
By performing a probe_write() on the memory first, we can ensure we have permissions to perform the atomic operation. As RISC-V doesn't have write only pages this should be pretty robust (PMP might be the exception).
Note that this only fixes the fault for memory regions. I/O and non-existant regions will still trigger a load fault.
--- target/riscv/helper.h | 2 + target/riscv/op_helper.c | 6 +++ target/riscv/insn_trans/trans_rva.c.inc | 59 +++++++++++++++++-------- 3 files changed, 48 insertions(+), 19 deletions(-)
target/riscv/helper.h +2 -0
@@ -106,6 +106,8 @@ DEF_HELPER_1(wfi, void, env) DEF_HELPER_1(tlb_flush, void, env)#endif +DEF_HELPER_4 (atomic_check, void, env, tl, int, int) + /* Hypervisor functions */#ifndef CONFIG_USER_ONLY DEF_HELPER_1 (hyp_tlb_flush, void, env)
target/riscv/op_helper.c +6 -0
@@ -114,6 +114,12 @@ target_ulong helper_csrrw_i128(CPURISCVState *env, int csr,return int128_getlo (rv); } +void helper_atomic_check (CPURISCVState *env,target_ulong address, +int width,int mmu_idx) +{ +probe_write (env, address, width, mmu_idx,GETPC ()); +} +#ifndef CONFIG_USER_ONLY target_ulong helper_sret (CPURISCVState *env)
target/riscv/insn_trans/trans_rva.c.inc +40 -19
159 lines not on this page · raw message
Signature · 1 line
2.34.1
Quoted 2 lines · Alistair Francis
Note that this only fixes the fault for memory regions. I/O and non-existant regions will still trigger a load fault.
...
target/riscv/op_helper.c target_ulong helper_csrrw_i128(CPURISCVState *env, int csr, · line 121
Quoted 5 lines
target/riscv/op_helper.c +5
-0+void helper_atomic_check (CPURISCVState *env,target_ulong address, +int width,int mmu_idx) +{ +probe_write (env, address, width, mmu_idx,GETPC ()); +}
Note that you could use probe_access_flags, whose return value is a mask. If it includes TLB_MMIO, you know that the physical address does not contain ram, but you still do not know if it is unmapped or true mmio, since unmapped gets unassigned_io_ops. It's probably not *that* hard to find out one way or another, but it might also be reasonable to set a flag to communicate with your do_transaction_failed hook.
Quoted 15 lines
static bool gen_amo(DisasContext *ctx,
arg_atomic*a,
void(*func)(TCGv, TCGv, TCGv, TCGArg, MemOp),target/riscv/insn_trans/trans_rva.c.inc +4
-1- MemOp mop) + TCGv_i32 width, MemOp mop) { TCGv dest = dest_gpr(ctx, a->rd); TCGv src1 = get_address(ctx, a->rs1, 0); TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE); + TCGv_i32 mem_idx = tcg_constant_i32(ctx->mem_idx); + + gen_helper_atomic_check(cpu_env, src1, width, mem_idx); func(dest, src1, src2, ctx->mem_idx, mop);
target/riscv/insn_trans/trans_rva.c.inc static bool trans_sc_w(DisasContext *ctx, arg_sc_w *a) · line 112
Quoted 6 lines
target/riscv/insn_trans/trans_rva.c.inc +2
-1@@ -105,55 +108,64 @@ static bool trans_sc_w(DisasContext *ctx, arg_sc_w *a) static bool trans_amoswap_w(DisasContext *ctx, arg_amoswap_w *a) { REQUIRE_EXT(ctx, RVA); - return gen_amo(ctx, a, &tcg_gen_atomic_xchg_tl, (MO_ALIGN | MO_TESL)); + return gen_amo(ctx, a, &tcg_gen_atomic_xchg_tl, tcg_constant_i32(4), + (MO_ALIGN | MO_TESL));
The width is already stored in the MemOp argument that you're passing around. I think you should do
TCGv_i32 width = tcg_constant_i32(memop_size(mop));
there in gen_amo, and not change the callers at all.
r~