target/i386: Raise #GP on unaligned m128 accesses when required.
v2 cover target/i386: Raise #GP on unaligned m128 accesses when required.
Thanks Richard for the detailed comments/code pointers! I've switched to using MO_ALIGN_16 and implemented record_sigbus and do_unaligned_access hooks to simulate #GP(0) as suggested. Given what was said about the low likelihood of implementing #AC anytime soon, I have hardcoded #GP(0) in these hooks for now rather than plumbing through an extra bit in MemOp. Let me know if that seems reasonable, thanks!
Ricky Zhou (1):
target/i386: Raise #GP on unaligned m128 accesses when required.
target/i386/tcg/excp_helper.c | 13 ++++++++ target/i386/tcg/helper-tcg.h | 28 +++++++++------- target/i386/tcg/sysemu/excp_helper.c | 8 +++++ target/i386/tcg/tcg-cpu.c | 1 + target/i386/tcg/translate.c | 49 ++++++++++++++++++---------- target/i386/tcg/user/excp_helper.c | 7 ++++ 6 files changed, 77 insertions(+), 29 deletions(-)
Signature · 1 line
2.37.2
PATCH v2 1/1 target/i386: Raise #GP on unaligned m128 accesses when required.
Many instructions which load/store 128-bit values are supposed to raise #GP when the memory operand isn't 16-byte aligned. This includes:
- Instructions explicitly requiring memory alignment (Exceptions Type 1 in the "AVX and SSE Instruction Exception Specification" section of the SDM)
- Legacy SSE instructions that load/store 128-bit values (Exceptions Types 2 and 4).
This change sets MO_ALIGN_16 on 128-bit memory accesses that require 16-byte alignment. It adds cpu_record_sigbus and cpu_do_unaligned_access handlers that simulate a #GP exception in qemu-user and qemu-system, respectively.
One minor behavior change apart from what is described above: Prior to this change, emulation of cvttps2pi and cvtps2pi instructions would incorrectly read 16 bytes of memory instead of 8. I double-checked on real hardware that these instructions only read 8 bytes (and do not have any address alignment requirements).
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/217
--- target/i386/tcg/excp_helper.c | 13 ++++++++ target/i386/tcg/helper-tcg.h | 28 +++++++++------- target/i386/tcg/sysemu/excp_helper.c | 8 +++++ target/i386/tcg/tcg-cpu.c | 2 ++ target/i386/tcg/translate.c | 49 ++++++++++++++++++---------- target/i386/tcg/user/excp_helper.c | 7 ++++ 6 files changed, 78 insertions(+), 29 deletions(-)
target/i386/tcg/excp_helper.c +13 -0
@@ -140,3 +140,16 @@ G_NORETURN void raise_exception_ra(CPUX86State *env, int exception_index, {raise_interrupt2 (env, exception_index,0 ,0 ,0 , retaddr); } + +G_NORETURN void handle_unaligned_access(CPUX86State *env, vaddr vaddr, +MMUAccessType access_type, +uintptr_t retaddr) +{ +/* +* Unaligned accesses are currently only triggered by SSE/AVX +* instructions that impose alignment requirements on memory +* operands. These instructions raise #GP(0) upon accessing an +* unaligned address. +*/ +raise_exception_ra (env,EXCP0D_GPF , retaddr); +}
target/i386/tcg/helper-tcg.h +17 -11
@@ -42,17 +42,6 @@ void x86_cpu_do_interrupt(CPUState *cpu);bool x86_cpu_exec_interrupt (CPUState *cpu,int int_req);#endif -/* helper.c */ -#ifdef CONFIG_USER_ONLY -void x86_cpu_record_sigsegv (CPUState *cs,vaddr addr, -MMUAccessType access_type, -bool maperr,uintptr_t ra); -#else -bool x86_cpu_tlb_fill (CPUState *cs,vaddr address,int size, -MMUAccessType access_type,int mmu_idx, -bool probe,uintptr_t retaddr); -#endif -void breakpoint_handler (CPUState *cs);/* n must be a constant to be efficient */
@@ -78,6 +67,23 @@ G_NORETURN void raise_exception_err_ra(CPUX86State *env, int exception_index, int error_code, uintptr_t retaddr); G_NORETURN void raise_interrupt(CPUX86State *nenv, int intno, int is_int,int error_code,int next_eip_addend); +G_NORETURN void handle_unaligned_access(CPUX86State *env, vaddr vaddr, +MMUAccessType access_type, +uintptr_t retaddr); +#ifdef CONFIG_USER_ONLY +void x86_cpu_record_sigsegv (CPUState *cs,vaddr addr, +MMUAccessType access_type, +bool maperr,uintptr_t ra); +void x86_cpu_record_sigbus (CPUState *cs,vaddr addr, +MMUAccessType access_type,uintptr_t ra); +#else +bool x86_cpu_tlb_fill (CPUState *cs,vaddr address,int size, +MMUAccessType access_type,int mmu_idx, +bool probe,uintptr_t retaddr); +G_NORETURN void x86_cpu_do_unaligned_access(CPUState *cs, vaddr vaddr, +MMUAccessType access_type, +int mmu_idx,uintptr_t retaddr); +#endif /* cc_helper.c */ extern const uint8_t parity_table[256 ];
target/i386/tcg/sysemu/excp_helper.c +8 -0
@@ -439,3 +439,11 @@ bool x86_cpu_tlb_fill(CPUState *cs, vaddr addr, int size, }return true; } + +G_NORETURN void x86_cpu_do_unaligned_access(CPUState *cs, vaddr vaddr, +MMUAccessType access_type, +int mmu_idx,uintptr_t retaddr) +{ +X86CPU *cpu =X86_CPU (cs); +handle_unaligned_access (&cpu->env, vaddr, access_type, retaddr); +}
target/i386/tcg/tcg-cpu.c +2 -0
@@ -75,10 +75,12 @@ static const struct TCGCPUOps x86_tcg_ops = {#ifdef CONFIG_USER_ONLY .fake_user_interrupt = x86_cpu_do_interrupt, .record_sigsegv = x86_cpu_record_sigsegv, + .record_sigbus = x86_cpu_record_sigbus, #else .tlb_fill = x86_cpu_tlb_fill, .do_interrupt = x86_cpu_do_interrupt, .cpu_exec_interrupt = x86_cpu_exec_interrupt, + .do_unaligned_access = x86_cpu_do_unaligned_access, .debug_excp_handler = breakpoint_handler, .debug_check_breakpoint = x86_debug_check_breakpoint,#endif /* !CONFIG_USER_ONLY */
target/i386/tcg/translate.c +31 -18
136 lines not on this page · raw message
target/i386/tcg/user/excp_helper.c +7 -0
@@ -48,3 +48,10 @@ void x86_cpu_record_sigsegv(CPUState *cs, vaddr addr,cpu_loop_exit_restore (cs, ra); } + +void x86_cpu_record_sigbus (CPUState *cs,vaddr addr, +MMUAccessType access_type,uintptr_t ra) +{ +X86CPU *cpu =X86_CPU (cs); +handle_unaligned_access (&cpu->env, addr, access_type, ra); +}
Signature · 1 line
2.37.2
Quoted 15 lines · Ricky Zhou
Many instructions which load/store 128-bit values are supposed to raise #GP when the memory operand isn't 16-byte aligned. This includes:
- Instructions explicitly requiring memory alignment (Exceptions Type 1 in the "AVX and SSE Instruction Exception Specification" section of the SDM)
- Legacy SSE instructions that load/store 128-bit values (ExceptionsTypes 2 and 4).This change sets
MO_ALIGN_16on 128-bit memory accesses that require 16-byte alignment. It addscpu_record_sigbusandcpu_do_unaligned_accesshandlers that simulate a #GP exception in qemu-user and qemu-system, respectively.
Quoted 5 lines
One minor behavior change apart from what is described above: Prior to this change, emulation of cvttps2pi and cvtps2pi instructions would incorrectly read 16 bytes of memory instead of 8. I double-checked on real hardware that these instructions only read 8 bytes (and do not have any address alignment requirements).
This should really be split out to a separate patch.
target/i386/tcg/translate.c static void gen_sse(CPUX86State *env, DisasContext *s, int b, · line 3632
Quoted 6 lines
target/i386/tcg/translate.c +1
-1@@ -3621,7 +3629,11 @@ static void gen_sse(CPUX86State *env, DisasContext *s, int b,if (mod !=3 ) {gen_lea_modrm (env, s, modrm); op2_offset = offsetof(CPUX86State ,xmm_t0); -gen_ldo_env_A0 (s, op2_offset); +if ((b >>8 ) &1 ) {
Aka b1.
Otherwise,
r~