tcg/sparc: zero extend ld/st helper args
v2 cover tcg/sparc: zero extend ld/st helper args
These patches fix problems with the SPARC TCG backend code which calls the load and store helpers. Where the argument being passed to the helper is narrower than the size of the native register, the SPARC calling convention requires that we extend it to the register size, but we weren't doing that. This meant we passed the host code registers which might have garbage in the high parts, and if the host code was built with optimization this resulted in wrong behaviour.
Changes v1->v2:
* fix argument order to emit_extend() in 32-bit host case
* switch on (op & MO_SIZE) rather than just op
thanks
-- PMM
Peter Maydell (2):
tcg/sparc: Zero extend data argument to store helpers tcg/sparc: Zero extend address argument to ld/st helpers
tcg/sparc/tcg-target.inc.c | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-)
Signature · 1 line
2.7.4
PATCH v2 1/2 tcg/sparc: Zero extend data argument to store helpers
The C store helper functions take the data argument as a uint8_t, uint16_t, etc depending on the store size. The SPARC calling convention requires that data types smaller than the register size must be extended by the caller. We weren't doing this, which meant that if QEMU was compiled with optimizations enabled we could end up storing incorrect values to guest memory. (In particular the i386 guest BIOS would crash on startup.)
Add code to the trampolines that call the store helpers to do the zero extension as required.
---v1->v2: fix incorrect argument order to emit_extend() for sparc32 codepath; switch on op & MO_SIZE rather than just op.
--- tcg/sparc/tcg-target.inc.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)
tcg/sparc/tcg-target.inc.c +25 -0
@@ -843,6 +843,29 @@ static void tcg_out_mb(TCGContext *s, TCGArg a0)static tcg_insn_unit *qemu_ld_trampoline[16 ];static tcg_insn_unit *qemu_st_trampoline[16 ]; +static void emit_extend (TCGContext *s,TCGReg r,int op) +{ +/* Emit zero extend of 8, 16 or 32 bit data as +* required by the MO_* value op; do nothing for 64 bit. +*/ +switch (op &MO_SIZE ) { +case MO_8 : +tcg_out_arithi (s, r, r,0xff ,ARITH_AND ); +break ; +case MO_16 : +tcg_out_arithi (s, r, r,16 ,SHIFT_SLL ); +tcg_out_arithi (s, r, r,16 ,SHIFT_SRL ); +break ; +case MO_32 : +if (SPARC64 ) { +tcg_out_arith (s, r, r,0 ,SHIFT_SRL ); + } +break ; +case MO_64 : +break ; + } +} +static void build_trampolines (TCGContext *s) {static void *const qemu_ld_helpers[16 ] = {
@@ -910,6 +933,7 @@ static void build_trampolines(TCGContext *s) qemu_st_trampoline[i] = s->code_ptr;if (SPARC64 ) { +emit_extend (s,TCG_REG_O2 , i); ra =TCG_REG_O4 ; }else { ra =TCG_REG_O1 ;
@@ -925,6 +949,7 @@ static void build_trampolines(TCGContext *s)tcg_out_arithi (s, ra, ra +1 ,32 ,SHIFT_SRLX ); ra +=2 ; }else { +emit_extend (s, ra, i); ra +=1 ; }/* Skip the oi argument. */
Signature · 1 line
2.7.4
Re: [Qemu-devel] [PATCH v2 for-2.9 1/2] tcg/sparc: Zero extend data argument to store helpers
Quoted 8 lines · Peter Maydell
The C store helper functions take the data argument as a
uint8_t,uint16_t, etc depending on the store size. The SPARC calling convention requires that data types smaller than the register size must be extended by the caller. We weren't doing this, which meant that if QEMU was compiled with optimizations enabled we could end up storing incorrect values to guest memory. (In particular the i386 guest BIOS would crash on startup.)
Quoted 4 lines
Add code to the trampolines that call the store helpers to do the zero extension as required.
Signed-off-by Peter Maydell <[email protected]>
Quoted 58 lines
---
v1->v2: fix incorrect argument order toemit_extend()for sparc32 codepath; switch on op &MO_SIZErather than just op.--- tcg/sparc/tcg-target.inc.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)tcg/sparc/tcg-target.inc.c +25
-0@@ -843,6 +843,29 @@ static void tcg_out_mb(TCGContext *s, TCGArg a0)static tcg_insn_unit *qemu_ld_trampoline[16 ];static tcg_insn_unit *qemu_st_trampoline[16 ]; +static void emit_extend (TCGContext *s,TCGReg r,int op) +{ +/* Emit zero extend of 8, 16 or 32 bit data as +* required by the MO_* value op; do nothing for 64 bit. +*/ +switch (op &MO_SIZE ) { +case MO_8 : +tcg_out_arithi (s, r, r,0xff ,ARITH_AND ); +break ; +case MO_16 : +tcg_out_arithi (s, r, r,16 ,SHIFT_SLL ); +tcg_out_arithi (s, r, r,16 ,SHIFT_SRL ); +break ; +case MO_32 : +if (SPARC64 ) { +tcg_out_arith (s, r, r,0 ,SHIFT_SRL ); + } +break ; +case MO_64 : +break ; + } +} +static void build_trampolines (TCGContext *s) {static void *const qemu_ld_helpers[16 ] = {@@ -910,6 +933,7 @@ static void build_trampolines(TCGContext *s) qemu_st_trampoline[i] = s->code_ptr;if (SPARC64 ) { +emit_extend (s,TCG_REG_O2 , i); ra =TCG_REG_O4 ; }else { ra =TCG_REG_O1 ;@@ -925,6 +949,7 @@ static void build_trampolines(TCGContext *s)tcg_out_arithi (s, ra, ra +1 ,32 ,SHIFT_SRLX ); ra +=2 ; }else { +emit_extend (s, ra, i); ra +=1 ; }/* Skip the oi argument. */
PATCH v2 2/2 tcg/sparc: Zero extend address argument to ld/st helpers
In reply to Peter MaydellThe C store helper functions take the address argument as a target_ulong type; if this is 32 bit but the host is 64 bit then the SPARC calling convention requires that the caller must zero extend the value. We weren't doing this, which meant we could pass values to the caller with high bits set and QEMU would crash if it was compiled with optimizations. In particular, the i386 BIOS would not start.
--- tcg/sparc/tcg-target.inc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tcg/sparc/tcg-target.inc.c +2 -2
@@ -1144,7 +1144,7 @@ static void tcg_out_qemu_ld(TCGContext *s, TCGReg data, TCGReg addr,/* Skip the high-part; we'll perform the extract in the trampoline. */ param++; } -tcg_out_mov (s,TCG_TYPE_REG , param++, addr); +tcg_out_mov (s,TCG_TYPE_REG , param++, addrz);/* We use the helpers to extend SB and SW data, leaving the case of SL needing explicit extending below. */
@@ -1224,7 +1224,7 @@ static void tcg_out_qemu_st(TCGContext *s, TCGReg data, TCGReg addr,/* Skip the high-part; we'll perform the extract in the trampoline. */ param++; } -tcg_out_mov (s,TCG_TYPE_REG , param++, addr); +tcg_out_mov (s,TCG_TYPE_REG , param++, addrz);if (!SPARC64 && (memop &MO_SIZE ) ==MO_64 ) {/* Skip the high-part; we'll perform the extract in the trampoline. */ param++;
Signature · 1 line
2.7.4