riscv misaligned accesses
cover riscv misaligned accesses
Hi,
I fell into some strangeness while using RISCV:
* firstly the rvi stores / loads don't seem to generate a trap when doing a misaligned access, which is something we would like to happen. According to the documentation an EEI may or may no guarantee misaligned loads and stores are handled invisibly. Thus PATCH 1 adds an option for enabling those traps, and PATCH 2 raise it when enabled and Zama16B (which implies atomicity for misaligned access within 16bytes boundary) is disabled.
* secondly about the conditional store: the documentation states that the Zalrsc extension requires that the address held in rs1 is naturally aligned to the size of the operand. The current implementation doesn't do that check if the store doesn't happen, so the third PATCH fixes that behavior so that a misaligned trap will happen if the address is not naturally aligned even if the store shouldn't happen.
Best Regards,
Fred
Frederic Konrad (3):
target/riscv: add a trap-misaligned-access property target/riscv: generate misaligned access trap for rvi insn target/riscv: fix the trap generation for conditional store
target/riscv/cpu.c | 5 +++++ target/riscv/cpu_cfg.h | 1 + target/riscv/insn_trans/trans_rva.c.inc | 19 +++++++++++++++++++ target/riscv/insn_trans/trans_rvi.c.inc | 7 +++++++ 4 files changed, 32 insertions(+)
Signature · 1 line
2.43.5
PATCH 1/3 target/riscv: add a trap-misaligned-access property
On riscv target, misaligned accesses are either authorized and implemented in hardware, or unimplemented and generate a trap to be implemented in software.
At the moment misaligned accesses for rvi just succeed, the intention of this new property is to let the user choose to have a trap when a misaligned access happens.
--- target/riscv/cpu.c | 5 +++++ target/riscv/cpu_cfg.h | 1 + 2 files changed, 6 insertions(+)
target/riscv/cpu.c +5 -0
@@ -2697,6 +2697,11 @@ static Property riscv_cpu_properties[] = {* it with -x and default to 'false'. */ DEFINE_PROP_BOOL ("x-misa-w" ,RISCVCPU , cfg.misa_w, false), +/* +* when set, misaligned accesses will generate a trap. +*/ +DEFINE_PROP_BOOL ("trap-misaligned-access" ,RISCVCPU , + cfg.trap_misaligned_access, false), DEFINE_PROP_END_OF_LIST(), };
target/riscv/cpu_cfg.h +1 -0
@@ -173,6 +173,7 @@ struct RISCVCPUConfig {bool pmp;bool debug;bool misa_w; +bool trap_misaligned_access;bool short_isa_string;
Signature · 1 line
2.43.5
Re: [PATCH 1/3] target/riscv: add a trap-misaligned-access property
Quoted 19 lines · Frederic Konrad
On riscv target, misaligned accesses are either authorized and implemented in hardware, or unimplemented and generate a trap to be implemented in software.
At the moment misaligned accesses for rvi just succeed, the intention of this new property is to let the user choose to have a trap when a misaligned access happens.
Signed-off-by Frederic Konrad <[email protected]>--- target/riscv/cpu.c | 5 +++++ target/riscv/cpu_cfg.h | 1 + 2 files changed, 6 insertions(+)target/riscv/cpu.c +0
-0
target/riscv/cpu.c static Property riscv_cpu_properties[] = { · line 2704
Quoted 8 lines
target/riscv/cpu.c +5
-0@@ -2697,6 +2697,11 @@ static Property riscv_cpu_properties[] = {* it with -x and default to 'false'. */ DEFINE_PROP_BOOL ("x-misa-w" ,RISCVCPU , cfg.misa_w, false), +/* +* when set, misaligned accesses will generate a trap. +*/ +DEFINE_PROP_BOOL ("trap-misaligned-access" ,RISCVCPU , + cfg.trap_misaligned_access, false),
This should be the last patch, we want to implement the feature before we expose it.
Alistair
RE: [PATCH 1/3] target/riscv: add a trap-misaligned-access property
Quoted 26 lines
-----Original Message-----
From: Alistair Francis <[email protected]>
Sent: 16 December 2024 05:55
To: Konrad, Frederic <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Iglesias, Francisco <[email protected]>; Michel, Luc <[email protected]>
Subject: Re: [PATCH 1/3] target/riscv: add a trap-misaligned-access propertyQuoted 14 lines · Frederic Konrad
On riscv target, misaligned accesses are either authorized and implemented in hardware, or unimplemented and generate a trap to be implemented in software.
At the moment misaligned accesses for rvi just succeed, the intention of this new property is to let the user choose to have a trap when a misaligned access happens.
Signed-off-by Frederic Konrad <[email protected]>--- target/riscv/cpu.c | 5 +++++ target/riscv/cpu_cfg.h | 1 + 2 files changed, 6 insertions(+)
Quoted 16 lines
target/riscv/cpu.c static Property riscv_cpu_properties[] = { · line 2704
Quoted 13 lines
target/riscv/cpu.c +5
-0@@ -2697,6 +2697,11 @@ static Property riscv_cpu_properties[] = {* it with -x and default to 'false'. */ DEFINE_PROP_BOOL ("x-misa-w" ,RISCVCPU , cfg.misa_w, false), +/* +* when set, misaligned accesses will generate a trap. +*/ +DEFINE_PROP_BOOL ("trap-misaligned-access" ,RISCVCPU , + cfg.trap_misaligned_access, false),This should be the last patch, we want to implement the feature before we expose it.
Got it thanks.
Best Regards,
Fred
Quoted 2 lines
Alistair
PATCH 2/3 target/riscv: generate misaligned access trap for rvi insn
In reply to Frederic KonradNow there is an option to enable misaligned accesses traps, check the alignment during load and store for the RVI instructions. Do not generate them if the zama16b extension is there.
--- target/riscv/insn_trans/trans_rvi.c.inc | 7 +++++++ 1 file changed, 7 insertions(+)
target/riscv/insn_trans/trans_rvi.c.inc +7 -0
@@ -323,6 +323,10 @@ static bool gen_load(DisasContext *ctx, arg_lb *a, MemOp memop) { bool out; + if (ctx->cfg_ptr->trap_misaligned_access && !ctx->cfg_ptr->ext_zama16b) { + memop |= MO_ALIGN; + } + if (ctx->cfg_ptr->ext_zama16b) { memop |= MO_ATOM_WITHIN16; }
@@ -424,6 +428,9 @@ static bool gen_store_i128(DisasContext *ctx, arg_sb *a, MemOp memop) static bool gen_store(DisasContext *ctx, arg_sb *a, MemOp memop) { + if (ctx->cfg_ptr->trap_misaligned_access && !ctx->cfg_ptr->ext_zama16b) { + memop |= MO_ALIGN; + } if (ctx->cfg_ptr->ext_zama16b) { memop |= MO_ATOM_WITHIN16; }
Signature · 1 line
2.43.5
Re: [PATCH 2/3] target/riscv: generate misaligned access trap for rvi insn
Quoted 25 lines · Frederic Konrad
Now there is an option to enable misaligned accesses traps, check the alignment during load and store for the RVI instructions. Do not generate them if the zama16b extension is there.
Signed-off-by Frederic Konrad <[email protected]>--- target/riscv/insn_trans/trans_rvi.c.inc | 7 +++++++ 1 file changed, 7 insertions(+)target/riscv/insn_trans/trans_rvi.c.inc +4
-0@@ -323,6 +323,10 @@ static bool gen_load(DisasContext *ctx, arg_lb *a, MemOp memop) { bool out; + if (ctx->cfg_ptr->trap_misaligned_access && !ctx->cfg_ptr->ext_zama16b) { + memop |= MO_ALIGN; + } + if (ctx->cfg_ptr->ext_zama16b) { memop |= MO_ATOM_WITHIN16; }
target/riscv/insn_trans/trans_rvi.c.inc static bool gen_store_i128(DisasContext *ctx, arg_sb *a, MemOp memop) · line 436
Quoted 9 lines
target/riscv/insn_trans/trans_rvi.c.inc +3
-0@@ -424,6 +428,9 @@ static bool gen_store_i128(DisasContext *ctx, arg_sb *a, MemOp memop) static bool gen_store(DisasContext *ctx, arg_sb *a, MemOp memop) { + if (ctx->cfg_ptr->trap_misaligned_access && !ctx->cfg_ptr->ext_zama16b) { + memop |= MO_ALIGN; + } if (ctx->cfg_ptr->ext_zama16b) { memop |= MO_ATOM_WITHIN16; }
I would imagine that ext_zama16b would disable or conflict with trap_misaligned_access at startup.
r~
RE: [PATCH 2/3] target/riscv: generate misaligned access trap for rvi insn
Hi Richard,
Thanks for the comment.
Quoted 36 lines
-----Original Message-----
From: Richard Henderson <[email protected]>
Sent: 11 December 2024 22:50
To: Konrad, Frederic <[email protected]>; [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Iglesias, Francisco <[email protected]>; Michel, Luc <[email protected]>
Subject: Re: [PATCH 2/3] target/riscv: generate misaligned access trap for rvi insnQuoted 26 lines · Frederic Konrad
Now there is an option to enable misaligned accesses traps, check the alignment during load and store for the RVI instructions. Do not generate them if the zama16b extension is there.
Signed-off-by Frederic Konrad <[email protected]>--- target/riscv/insn_trans/trans_rvi.c.inc | 7 +++++++ 1 file changed, 7 insertions(+)target/riscv/insn_trans/trans_rvi.c.inc +4
-0@@ -323,6 +323,10 @@ static bool gen_load(DisasContext *ctx, arg_lb *a, MemOp memop) { bool out; + if (ctx->cfg_ptr->trap_misaligned_access && !ctx->cfg_ptr->ext_zama16b) { + memop |= MO_ALIGN; + } + if (ctx->cfg_ptr->ext_zama16b) { memop |= MO_ATOM_WITHIN16; }@@ -424,6 +428,9 @@ static bool gen_store_i128(DisasContext *ctx, arg_sb *a, MemOp memop)
Quoted 11 lines
target/riscv/insn_trans/trans_rvi.c.inc static bool gen_store_i128(DisasContext *ctx, arg_sb *a, MemOp memop) · line 436
Quoted 8 lines
target/riscv/insn_trans/trans_rvi.c.inc +3
-0static bool gen_store(DisasContext *ctx, arg_sb *a, MemOp memop) { + if (ctx->cfg_ptr->trap_misaligned_access && !ctx->cfg_ptr->ext_zama16b) { + memop |= MO_ALIGN; + } if (ctx->cfg_ptr->ext_zama16b) { memop |= MO_ATOM_WITHIN16; }I would imagine that
ext_zama16bwould disable or conflict withtrap_misaligned_accessat startup.
Ok I can fix that condition to:
if (ctx->cfg_ptr->trap_misaligned_access) {
And throw an error in the first patch if both are activated at the same time.
Best Regards,
Fred
Quoted 3 lines
r~
PATCH 3/3 target/riscv: fix the trap generation for conditional store
In reply to Frederic KonradFrom Unpriviledged ISA manual:
"For LR and SC, the Zalrsc extension requires that the address held in rs1 be naturally aligned to the size of the operand (i.e., eight-byte aligned for doublewords and four-byte aligned for words). If the address is not naturally aligned, an address-misaligned exception or an access-fault exception will be generated. The access-fault exception can be generated for a memory access that would otherwise be able to complete except for the misalignment, if the misaligned access should not be emulated."
Here nothing checks that the address is naturally aligned, so this fixes that wrong behavior by raising address-misaligned exception if the address in rs1 is not naturally aligned.
--- target/riscv/insn_trans/trans_rva.c.inc | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
target/riscv/insn_trans/trans_rva.c.inc +19 -0
@@ -58,11 +58,30 @@ static bool gen_lr(DisasContext *ctx, arg_atomic *a, MemOp mop) static bool gen_sc(DisasContext *ctx, arg_atomic *a, MemOp mop) { TCGv dest, src1, src2; + TCGv tmp = tcg_temp_new(); + TCGLabel *l3 = gen_new_label(); TCGLabel *l1 = gen_new_label(); TCGLabel *l2 = gen_new_label(); decode_save_opc(ctx, 0); src1 = get_address(ctx, a->rs1, 0); + /* + * A misaligned store trap should be triggered even if the store should + * fail due to the reservation. + */ + tcg_gen_andi_tl(tmp, src1, ~((uint64_t)0) << memop_alignment_bits(mop)); + tcg_gen_brcond_tl(TCG_COND_EQ, tmp, src1, l3); + + /* + * Store the faulty address, and the actual PC. Then generate the + * exception. + */ + tcg_gen_st_tl(src1, tcg_env, offsetof(CPURISCVState, badaddr)); + gen_pc_plus_diff(cpu_pc, ctx, 0); + gen_helper_raise_exception(tcg_env, + tcg_constant_i32(RISCV_EXCP_STORE_AMO_ADDR_MIS)); + + gen_set_label(l3); tcg_gen_brcond_tl(TCG_COND_NE, load_res, src1, l1); /*
Signature · 1 line
2.43.5
Re: [PATCH 3/3] target/riscv: fix the trap generation for conditional store
target/riscv/insn_trans/trans_rva.c.inc static bool gen_lr(DisasContext *ctx, arg_atomic *a, MemOp mop) · line 72
Quoted 5 lines · Frederic Konrad
target/riscv/insn_trans/trans_rva.c.inc +5
-0+ /* + * A misaligned store trap should be triggered even if the store should + * fail due to the reservation. + */ + tcg_gen_andi_tl(tmp, src1, ~((uint64_t)0) << memop_alignment_bits(mop));
The constant is incorrect for testing the low bits.
Quoted 1 line
+
tcg_gen_brcond_tl(TCG_COND_EQ, tmp, src1, l3);
Best to make the fallthrough path be the common case, as we will optimize across the extended basic block.
Use test-style comparison:
tcg_gen_brcondi_tl(TCG_COND_TSTNE, src1, memop_size(mop) - 1, l_misalign);
r~
RE: [PATCH 3/3] target/riscv: fix the trap generation for conditional store
+CC maintainers
Quoted 7 lines
-----Original Message-----
From: [email protected] <[email protected]> On Behalf Of Richard Henderson
Sent: 11 December 2024 22:43
To: [email protected]
Subject: Re: [PATCH 3/3] target/riscv: fix the trap generation for conditional store
Quoted 8 lines
target/riscv/insn_trans/trans_rva.c.inc static bool gen_lr(DisasContext *ctx, arg_atomic *a, MemOp mop) · line 72
Quoted 5 lines · Frederic Konrad
target/riscv/insn_trans/trans_rva.c.inc +5
-0+ /* + * A misaligned store trap should be triggered even if the store should + * fail due to the reservation. + */ + tcg_gen_andi_tl(tmp, src1, ~((uint64_t)0) << memop_alignment_bits(mop));The constant is incorrect for testing the low bits.
Hmm, I don't get it, basically with that I'm trying to do:
MO_8: src1 == (src1 & 0xFFFFFFFF)
MO_16: src1 == (src1 & 0xFFFFFFFE)
MO_32: src1 == (src1 & 0xFFFFFFFC)
etc
what am I missing?
Quoted 9 lines
Quoted 2 lines
+
tcg_gen_brcond_tl(TCG_COND_EQ, tmp, src1, l3);Best to make the fallthrough path be the common case, as we will optimize across the extended basic block.
Use test-style comparison:
tcg_gen_brcondi_tl(TCG_COND_TSTNE, src1, memop_size(mop) - 1, l_misalign);
In any case that one looks better thanks for the tips!
About making the fallthrough path be the common case, If I do it I'll need to jump anyway and the end of this instruction ie:
if not aligned go to misaligned label:
...
do the normal operation
...
go to done label
misaligned label
...
trigger the exception
...
done label
Is that what you had in mind?
Thanks,
Fred
Quoted 4 lines
r~
Re: [PATCH 3/3] target/riscv: fix the trap generation for conditional store
Quoted 18 lines · Frederic
+CC maintainers
Quoted 7 lines
-----Original Message-----
From: [email protected] <[email protected]> On Behalf Of Richard Henderson
Sent: 11 December 2024 22:43
To: [email protected]
Subject: Re: [PATCH 3/3] target/riscv: fix the trap generation for conditional storeQuoted 8 lines
target/riscv/insn_trans/trans_rva.c.inc static bool gen_lr(DisasContext *ctx, arg_atomic *a, MemOp mop) · line 72
Quoted 5 lines · Frederic Konrad
target/riscv/insn_trans/trans_rva.c.inc +5
-0+ /* + * A misaligned store trap should be triggered even if the store should + * fail due to the reservation. + */ + tcg_gen_andi_tl(tmp, src1, ~((uint64_t)0) << memop_alignment_bits(mop));The constant is incorrect for testing the low bits.
Quoted 10 lines
Hmm, I don't get it, basically with that I'm trying to do:
MO_8: src1 == (src1 & 0xFFFFFFFF) MO_16: src1 == (src1 & 0xFFFFFFFE) MO_32: src1 == (src1 & 0xFFFFFFFC) etcwhat am I missing?
Quoted 2 lines
Quoted 2 lines
+
tcg_gen_brcond_tl(TCG_COND_EQ, tmp, src1, l3);
Ah, I missed the form of the equality. I had been expecting
tmp = src1 & 1
brcond tmp != 0and so mis-read the mask.
Quoted 14 lines
About making the fallthrough path be the common case, If I do it I'll need to jump anyway and the end of this instruction ie:
if not aligned go to misaligned label:
...
do the normal operation
...
go to done label
misaligned label
...
trigger the exception
...
done label
Quoted 1 line
Is that what you had in mind?
There is another code block in there. We can sort as
if not aligned goto misaligned;
if reservation mismatch goto mismatch
normal operation
goto done
misaligned:
raise exception
mismatch:
mb, set failure
done:
clear reservation
so that we save adding a branch on the mismatch path.
r~