Fixes for unaligned scalar accesses
v2 cover Fixes for unaligned scalar accesses
I took some "working" programs under userspace emulation into a linux instance running on a fork with comprehensive system mode support and discovered a compiler bug causing unaligned accesses. This bug had gone undetected in userspace tests because they were not handled correctly by QEMU.
With these patches, unaligned accesses cause an exception that generates SIGBUS.
Changes for v2: moved linux-user cpu loop to its own commit.
v1: https://lore.kernel.org/qemu-devel/[email protected]/
Brian Cain (5):
target/hexagon: handle misaligned load/store cause codes in cpu_loop target/hexagon: raise SIGBUS on unaligned scalar loads target/hexagon: raise SIGBUS on unaligned scalar stores target/hexagon: raise SIGBUS on unaligned data access in sysemu tests/tcg/hexagon: add unaligned scalar test
linux-user/hexagon/cpu_loop.c | 5 + target/hexagon/cpu.c | 25 +++++ target/hexagon/genptr.c | 10 +- target/hexagon/idef-parser/parser-helpers.c | 2 +- target/hexagon/macros.h | 26 +++-- target/hexagon/op_helper.c | 13 ++- target/hexagon/translate.c | 6 +- tests/tcg/hexagon/Makefile.target | 2 + tests/tcg/hexagon/unaligned_data.c | 118 ++++++++++++++++++++ 9 files changed, 185 insertions(+), 22 deletions(-) create mode 100644 tests/tcg/hexagon/unaligned_data.c
Signature · 1 line
2.34.1
PATCH v2 1/5 target/hexagon: handle misaligned load/store cause codes in cpu_loop
Add a linux-user cpu_loop handler for HEX_CAUSE_MISALIGNED_LOAD and HEX_CAUSE_MISALIGNED_STORE, raising SIGBUS for consistency with hexswi.c's sysemu handling, even though linux-user's own alignment check in accel/tcg/user-exec.c currently raises SIGBUS before Hexagon's cause-code machinery is reached.
--- linux-user/hexagon/cpu_loop.c | 5 +++++ 1 file changed, 5 insertions(+)
linux-user/hexagon/cpu_loop.c +5 -0
@@ -76,6 +76,11 @@ void cpu_loop(CPUHexagonState *env)force_sig_fault (TARGET_SIGILL ,TARGET_ILL_ILLOPC , env->gpr[HEX_REG_PC ]);break ; +case HEX_CAUSE_MISALIGNED_LOAD : +case HEX_CAUSE_MISALIGNED_STORE : +force_sig_fault (TARGET_SIGBUS ,TARGET_BUS_ADRALN , + env->gpr[HEX_REG_PC ]); +break ;default : EXCP_DUMP(env, "\nqemu: unhandled CPU precise exception " "cause code 0x%x - aborting\n",
Signature · 1 line
2.34.1
PATCH v2 2/5 target/hexagon: raise SIGBUS on unaligned scalar loads
In reply to Brian Cainhexagon-linux-user had no enforcement of alignment for scalar load exceptions, and unaligned loads silently succeeded instead of raising SIGBUS.
Add MO_ALIGN to the MEM_LOAD* macros, the locked-load and load-frame helpers in genptr.c, and the idef-parser's fLOAD.
--- target/hexagon/genptr.c | 6 ++--- target/hexagon/idef-parser/parser-helpers.c | 2 +- target/hexagon/macros.h | 26 +++++++++++++-------- 3 files changed, 20 insertions(+), 14 deletions(-)
target/hexagon/genptr.c +3 -3
@@ -470,14 +470,14 @@ void gen_set_byte_i64(int N, TCGv_i64 result, TCGv src)static inline void gen_load_locked4u (TCGv dest,TCGv vaddr,int mem_index) { -tcg_gen_qemu_ld_tl (dest, vaddr, mem_index,MO_LE |MO_UL ); +tcg_gen_qemu_ld_tl (dest, vaddr, mem_index,MO_LE |MO_UL |MO_ALIGN );tcg_gen_mov_tl (hex_llsc_addr, vaddr);tcg_gen_mov_tl (hex_llsc_val, dest); }static inline void gen_load_locked8u (TCGv_i64 dest,TCGv vaddr,int mem_index) { -tcg_gen_qemu_ld_i64 (dest, vaddr, mem_index,MO_LE |MO_UQ ); +tcg_gen_qemu_ld_i64 (dest, vaddr, mem_index,MO_LE |MO_UQ |MO_ALIGN );tcg_gen_mov_tl (hex_llsc_addr, vaddr);tcg_gen_mov_i64 (hex_llsc_val_i64, dest); }
@@ -890,7 +890,7 @@ static void gen_load_frame(DisasContext *ctx, TCGv_i64 frame, TCGv EA) {Insn *insn = ctx->insn;/* Needed for CHECK_NOSHUF */ CHECK_NOSHUF (EA ,8 ); -tcg_gen_qemu_ld_i64 (frame,EA , ctx->mem_idx,MO_LE |MO_UQ ); +tcg_gen_qemu_ld_i64 (frame,EA , ctx->mem_idx,MO_LE |MO_UQ |MO_ALIGN ); }/* Stack overflow check */
target/hexagon/idef-parser/parser-helpers.c +1 -1
@@ -1770,7 +1770,7 @@ void gen_load(Context *c, YYLTYPE *locp, HexValue *width,if (signedness ==SIGNED ) {OUT (c, locp," | MO_SIGN" ); } -OUT (c, locp," | MO_LE);\n" ); +OUT (c, locp," | MO_LE | MO_ALIGN);\n" ); } void gen_store(Context *c, YYLTYPE *locp, HexValue *width, HexValue *ea,
target/hexagon/macros.h +16 -10
@@ -106,37 +106,37 @@#define MEM_LOAD1s (DST ,VA ) \ do { \ CHECK_NOSHUF(VA, 1); \ - tcg_gen_qemu_ld_tl(DST, VA, ctx->mem_idx, MO_SB); \ + tcg_gen_qemu_ld_tl(DST, VA, ctx->mem_idx, MO_SB | MO_ALIGN); \ } while (0)#define MEM_LOAD1u (DST ,VA ) \ do { \ CHECK_NOSHUF(VA, 1); \ - tcg_gen_qemu_ld_tl(DST, VA, ctx->mem_idx, MO_UB); \ + tcg_gen_qemu_ld_tl(DST, VA, ctx->mem_idx, MO_UB | MO_ALIGN); \ } while (0)#define MEM_LOAD2s (DST ,VA ) \ do { \ CHECK_NOSHUF(VA, 2); \ - tcg_gen_qemu_ld_tl(DST, VA, ctx->mem_idx, MO_LE | MO_SW); \ + tcg_gen_qemu_ld_tl(DST, VA, ctx->mem_idx, MO_LE | MO_SW | MO_ALIGN); \ } while (0)#define MEM_LOAD2u (DST ,VA ) \ do { \ CHECK_NOSHUF(VA, 2); \ - tcg_gen_qemu_ld_tl(DST, VA, ctx->mem_idx, MO_LE | MO_UW); \ + tcg_gen_qemu_ld_tl(DST, VA, ctx->mem_idx, MO_LE | MO_UW | MO_ALIGN); \ } while (0)#define MEM_LOAD4s (DST ,VA ) \ do { \ CHECK_NOSHUF(VA, 4); \ - tcg_gen_qemu_ld_tl(DST, VA, ctx->mem_idx, MO_LE | MO_SL); \ + tcg_gen_qemu_ld_tl(DST, VA, ctx->mem_idx, MO_LE | MO_SL | MO_ALIGN); \ } while (0)#define MEM_LOAD4u (DST ,VA ) \ do { \ CHECK_NOSHUF(VA, 4); \ - tcg_gen_qemu_ld_tl(DST, VA, ctx->mem_idx, MO_LE | MO_UL); \ + tcg_gen_qemu_ld_tl(DST, VA, ctx->mem_idx, MO_LE | MO_UL | MO_ALIGN); \ } while (0)#define MEM_LOAD8u (DST ,VA ) \ do { \ CHECK_NOSHUF(VA, 8); \ - tcg_gen_qemu_ld_i64(DST, VA, ctx->mem_idx, MO_LE | MO_UQ); \ + tcg_gen_qemu_ld_i64(DST, VA, ctx->mem_idx, MO_LE | MO_UQ | MO_ALIGN); \ } while (0)#define MEM_STORE1_FUNC (X ) \
@@ -519,9 +519,15 @@ static inline TCGv gen_read_ireg(TCGv result, TCGv val, int shift)#define fLOAD (NUM ,SIZE ,SIGN ,EA ,DST ) MEM_LOAD##SIZE##SIGN(DST, EA)#else #define MEM_LOAD1 cpu_ldub_data_ra -#define MEM_LOAD2 cpu_lduw_le_data_ra -#define MEM_LOAD4 cpu_ldl_le_data_ra -#define MEM_LOAD8 cpu_ldq_le_data_ra +#define MEM_LOAD2 (ENV ,EA ,RA ) \ + cpu_ldw_mmu(ENV, EA, make_memop_idx(MO_LEUW | MO_ALIGN, \ + cpu_mmu_index(env_cpu(ENV), false)), RA) +#define MEM_LOAD4 (ENV ,EA ,RA ) \ + cpu_ldl_mmu(ENV, EA, make_memop_idx(MO_LEUL | MO_ALIGN, \ + cpu_mmu_index(env_cpu(ENV), false)), RA) +#define MEM_LOAD8 (ENV ,EA ,RA ) \ + cpu_ldq_mmu(ENV, EA, make_memop_idx(MO_LEUQ | MO_ALIGN, \ + cpu_mmu_index(env_cpu(ENV), false)), RA)#define fLOAD (NUM ,SIZE ,SIGN ,EA ,DST ) \ do { \
Signature · 1 line
2.34.1
PATCH v2 3/5 target/hexagon: raise SIGBUS on unaligned scalar stores
In reply to Brian Cainhexagon-linux-user had no enforcement of alignment for scalar store exceptions, and unaligned stores silently succeeded instead of raising SIGBUS.
Stores are logged rather than committed immediately, so both commit paths need the fix.
--- target/hexagon/genptr.c | 4 ++-- target/hexagon/op_helper.c | 13 ++++++++++--- target/hexagon/translate.c | 6 +++--- 3 files changed, 15 insertions(+), 8 deletions(-)
target/hexagon/genptr.c +2 -2
@@ -495,7 +495,7 @@ static inline void gen_store_conditional4(DisasContext *ctx, zero =tcg_constant_tl (0 ); tmp =tcg_temp_new ();tcg_gen_atomic_cmpxchg_tl (tmp, hex_llsc_addr, hex_llsc_val, src, - ctx->mem_idx,MO_32 ); + ctx->mem_idx,MO_32 |MO_ALIGN );tcg_gen_movcond_tl (TCG_COND_EQ , pred, tmp, hex_llsc_val, one, zero);tcg_gen_br (done);
@@ -520,7 +520,7 @@ static inline void gen_store_conditional8(DisasContext *ctx, zero =tcg_constant_i64 (0 ); tmp =tcg_temp_new_i64 ();tcg_gen_atomic_cmpxchg_i64 (tmp, hex_llsc_addr, hex_llsc_val_i64, src, - ctx->mem_idx,MO_64 ); + ctx->mem_idx,MO_64 |MO_ALIGN );tcg_gen_movcond_i64 (TCG_COND_EQ , tmp, tmp, hex_llsc_val_i64, one, zero);tcg_gen_extrl_i64_i32 (pred, tmp);
target/hexagon/op_helper.c +10 -3
@@ -95,19 +95,26 @@ static void commit_store(CPUHexagonState *env, int slot_num, uintptr_t ra) {uint32_t width = env->mem_log_stores[slot_num].width;target_ulong va = env->mem_log_stores[slot_num].va; +MemOpIdx oi;switch (width) {case 1 :cpu_stb_data_ra (env, va, env->mem_log_stores[slot_num].data32, ra);break ;case 2 : -cpu_stw_le_data_ra (env, va, env->mem_log_stores[slot_num].data32, ra); + oi =make_memop_idx (MO_LEUW |MO_ALIGN , +cpu_mmu_index (env_cpu (env), false)); +cpu_stw_mmu (env, va, env->mem_log_stores[slot_num].data32, oi, ra);break ;case 4 : -cpu_stl_le_data_ra (env, va, env->mem_log_stores[slot_num].data32, ra); + oi =make_memop_idx (MO_LEUL |MO_ALIGN , +cpu_mmu_index (env_cpu (env), false)); +cpu_stl_mmu (env, va, env->mem_log_stores[slot_num].data32, oi, ra);break ;case 8 : -cpu_stq_le_data_ra (env, va, env->mem_log_stores[slot_num].data64, ra); + oi =make_memop_idx (MO_LEUQ |MO_ALIGN , +cpu_mmu_index (env_cpu (env), false)); +cpu_stq_mmu (env, va, env->mem_log_stores[slot_num].data64, oi, ra);break ;default :g_assert_not_reached ();
target/hexagon/translate.c +3 -3
@@ -896,17 +896,17 @@ void process_store(DisasContext *ctx, int slot_num)case 2 :tcg_gen_qemu_st_tl (hex_store_val32[slot_num], hex_store_addr[slot_num], - ctx->mem_idx,MO_LE |MO_UW ); + ctx->mem_idx,MO_LE |MO_UW |MO_ALIGN );break ;case 4 :tcg_gen_qemu_st_tl (hex_store_val32[slot_num], hex_store_addr[slot_num], - ctx->mem_idx,MO_LE |MO_UL ); + ctx->mem_idx,MO_LE |MO_UL |MO_ALIGN );break ;case 8 :tcg_gen_qemu_st_i64 (hex_store_val64[slot_num], hex_store_addr[slot_num], - ctx->mem_idx,MO_LE |MO_UQ ); + ctx->mem_idx,MO_LE |MO_UQ |MO_ALIGN );break ;default : {
Signature · 1 line
2.34.1
PATCH v2 4/5 target/hexagon: raise SIGBUS on unaligned data access in sysemu
In reply to Brian Cainhexagon-softmmu had no enforcement of alignment for scalar loads and
stores: the MO_ALIGN flag added by the previous two commits triggers TLB_INVALID_MASK/alignment faults in cputlb.c, but Hexagon's TCGCPUOps did not implement do_unaligned_access, so the fault was never delivered to the guest.
Add hexagon_cpu_do_unaligned_access(), which raises HEX_CAUSE_MISALIGNED_LOAD/_STORE via the existing HEX_EVENT_PRECISE path, mirroring raise_tlbmiss_exception()/raise_perm_exception().
--- target/hexagon/cpu.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)
target/hexagon/cpu.c +25 -0
@@ -637,6 +637,18 @@ static void raise_perm_exception(CPUState *cs, uint32_t VA, int slot, cs->exception_index = excp; } +static void raise_misaligned_exception (CPUState *cs,uint32_t VA ,int slot, +MMUAccessType access_type) +{ +CPUHexagonState *env =cpu_env (cs); +int32_t excp = (access_type ==MMU_DATA_STORE ) ? +HEX_CAUSE_MISALIGNED_STORE :HEX_CAUSE_MISALIGNED_LOAD ; + +set_badva_regs (env,VA , slot, access_type); + cs->exception_index =HEX_EVENT_PRECISE ; + env->cause_code = excp; +} +static const char *access_type_names[] = {"MMU_DATA_LOAD " ,"MMU_DATA_STORE" ,"MMU_INST_FETCH" };
@@ -715,6 +727,18 @@ static vaddr hexagon_pointer_wrap(CPUState *cs, int mmu_idx,return result; } +static G_NORETURN +void hexagon_cpu_do_unaligned_access(CPUState *cs, vaddr addr, +MMUAccessType access_type,int mmu_idx, +uintptr_t retaddr) +{ +CPUHexagonState *env =cpu_env (cs); + +raise_misaligned_exception (cs, addr,0 , access_type); +do_raise_exception (env, cs->exception_index, env->gpr[HEX_REG_PC ], + retaddr); +} +#endif static const TCGCPUOps hexagon_tcg_ops = {
@@ -732,6 +756,7 @@ static const TCGCPUOps hexagon_tcg_ops = { .pointer_wrap = hexagon_pointer_wrap, .cpu_exec_reset = cpu_reset, .tlb_fill = hexagon_tlb_fill, + .do_unaligned_access = hexagon_cpu_do_unaligned_access, .cpu_exec_halt = hexagon_cpu_has_work, .do_interrupt = hexagon_cpu_do_interrupt,#endif /* !CONFIG_USER_ONLY */
Signature · 1 line
2.34.1
PATCH v2 5/5 tests/tcg/hexagon: add unaligned scalar test
In reply to Brian CainAdd unaligned_data.c to exercise unaligned memh/memw/memd accesses and verify SIGBUS is raised and caught.
--- tests/tcg/hexagon/Makefile.target | 2 + tests/tcg/hexagon/unaligned_data.c | 118 +++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 tests/tcg/hexagon/unaligned_data.c
tests/tcg/hexagon/Makefile.target +2 -0
@@ -54,6 +54,7 @@ HEX_TESTS += invalid-slots HEX_TESTS += invalid-encoding HEX_TESTS += multiple-writes HEX_TESTS += unaligned_pc +HEX_TESTS += unaligned_data HEX_TESTS += test_abs HEX_TESTS += test_bitcnt
@@ -108,6 +109,7 @@ preg_alias: preg_alias.c hex_test.h read_write_overlap: read_write_overlap.c hex_test.h reg_mut: reg_mut.c hex_test.h test_pnew_jump_loads: test_pnew_jump_loads.c hex_test.h +unaligned_data: unaligned_data.c hex_test.h unaligned_pc: unaligned_pc.c # Compile for v66 so that the ELF selects a v66 CPU; the test then
tests/tcg/hexagon/unaligned_data.c new file +118 -0
118 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v2 5/5] tests/tcg/hexagon: add unaligned scalar test
Quoted 10 lines · Brian Cain
Add
unaligned_data.cto exercise unaligned memh/memw/memd accesses and verify SIGBUS is raised and caught.Signed-off-by Brian Cain <[email protected]>--- tests/tcg/hexagon/Makefile.target | 2 + tests/tcg/hexagon/unaligned_data.c | 118 +++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 tests/tcg/hexagon/unaligned_data.c