Fix and clean tcg_target_get_call_iarg_regs_count
cover Fix and clean tcg_target_get_call_iarg_regs_count
Function tcg_target_get_call_iarg_regs_count was wrong for w64 hosts and can be simplified for all TCG targets:
[PATCH 1/4] w64: Fix TCG helper functions with 5 arguments [PATCH 2/4] tcg/i386: Remove unused registers from [PATCH 3/4] tcg: Move tcg_target_get_call_iarg_regs_count to tcg.c [PATCH 4/4] tcg: Remove unused parameter from
The first patch is also needed for stable-1.2. It was already sent today as a single patch.
Regards,
Stefan Weil
PATCH 1/4 w64: Fix TCG helper functions with 5 arguments
TCG uses 6 registers for function arguments on 64 bit Linux hosts, but only 4 registers on W64 hosts.
Commit 2999a0b20074a7e4a58f56572bb1436749368f59 increased the number of arguments for some important helper functions from 4 to 5 which triggered a bug for W64 hosts: QEMU aborts when executing helper_lcall_real in the guest's BIOS because function tcg_target_get_call_iarg_regs_count always returned 6.
As W64 has only 4 registers for arguments, the 5th argument must be passed on the stack using a correct stack offset.
--- tcg/i386/tcg-target.c | 2 +- tcg/i386/tcg-target.h | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-)
tcg/i386/tcg-target.c +1 -1
@@ -118,7 +118,7 @@ static void patch_reloc(uint8_t *code_ptr, int type,static inline int tcg_target_get_call_iarg_regs_count (int flags) {if (TCG_TARGET_REG_BITS ==64 ) { -return 6 ; +return ARRAY_SIZE (tcg_target_call_iarg_regs); }return 0 ;
tcg/i386/tcg-target.h +4 -0
@@ -67,7 +67,11 @@ typedef enum {/* used for function call generation */ #define TCG_REG_CALL_STACK TCG_REG_ESP#define TCG_TARGET_STACK_ALIGN 16 +#if defined(_WIN64) +#define TCG_TARGET_CALL_STACK_OFFSET 32 +#else #define TCG_TARGET_CALL_STACK_OFFSET 0 +#endif /* optional instructions */ #define TCG_TARGET_HAS_div2_i32 1
Signature · 1 line
1.7.10
PATCH 2/4 tcg/i386: Remove unused registers from tcg_target_call_iarg_regs
In reply to Stefan Weil32 bit x86 hosts don't need registers for helper function arguments because they use the default stack based calling convention.
Removing the registers allows simpler code for function tcg_target_get_call_iarg_regs_count.
--- tcg/i386/tcg-target.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-)
tcg/i386/tcg-target.c +2 -8
@@ -75,9 +75,7 @@ static const int tcg_target_call_iarg_regs[] = { TCG_REG_R8, TCG_REG_R9,#else -TCG_REG_EAX , -TCG_REG_EDX , - TCG_REG_ECX +/* 32 bit mode uses stack based calling convention (GCC default). */ #endif };
@@ -117,11 +115,7 @@ static void patch_reloc(uint8_t *code_ptr, int type,/* maximum number of register used for input function arguments */ static inline int tcg_target_get_call_iarg_regs_count (int flags) { -if (TCG_TARGET_REG_BITS ==64 ) { -return ARRAY_SIZE (tcg_target_call_iarg_regs); - } - -return 0 ; +return ARRAY_SIZE (tcg_target_call_iarg_regs); }/* parse target specific constraints */
Signature · 1 line
1.7.10
Re: [Qemu-devel] [PATCH 2/4] tcg/i386: Remove unused registers from tcg_target_call_iarg_regs
tcg/i386/tcg-target.c static const int tcg_target_call_iarg_regs[] = { · line 80
Quoted 12 lines · Stefan Weil
tcg/i386/tcg-target.c +1
-3@@ -75,9 +75,7 @@ static const int tcg_target_call_iarg_regs[] = { TCG_REG_R8, TCG_REG_R9,#else -TCG_REG_EAX , -TCG_REG_EDX , - TCG_REG_ECX +/* 32 bit mode uses stack based calling convention (GCC default). */ #endif };
This makes the array zero-length for 32 bit targets, but functions like tcg_out_tlb_load() and tcg_out_qemu_ld() still unconditionally access elements in it...
-- PMM
Re: [Qemu-devel] [PATCH 2/4] tcg/i386: Remove unused registers from tcg_target_call_iarg_regs
Am 12.09.2012 23:18, schrieb Peter Maydell:
Quoted 17 lines · Stefan Weil
tcg/i386/tcg-target.c static const int tcg_target_call_iarg_regs[] = { · line 80
Quoted 12 lines
tcg/i386/tcg-target.c +1
-3@@ -75,9 +75,7 @@ static const int tcg_target_call_iarg_regs[] = { TCG_REG_R8, TCG_REG_R9,#else -TCG_REG_EAX , -TCG_REG_EDX , - TCG_REG_ECX +/* 32 bit mode uses stack based calling convention (GCC default). */ #endif };This makes the array zero-length for 32 bit targets, but functions like
tcg_out_tlb_load()andtcg_out_qemu_ld()still unconditionally access elements in it...
Quoted 1 line
-- PMM
Thanks for the hint. I'm afraid there are a lot more functions of that kind in i386/tcg-target.c.
I could use conditional compilation for those accesses, but first I'd like to understand why this works at all.
- sw
Re: [Qemu-devel] [PATCH 2/4] tcg/i386: Remove unused registers from tcg_target_call_iarg_regs
Quoted 2 lines · Stefan Weil
I could use conditional compilation for those accesses, but first I'd like to understand why this works at all.
Before your patch you mean?
Because those are the registers reserved by the "L" constraint.
r~
Re: [Qemu-devel] [PATCH 2/4] tcg/i386: Remove unused registers from tcg_target_call_iarg_regs
In reply to Stefan WeilQuoted 10 lines · Stefan Weil
Am 12.09.2012 23:18, schrieb Peter Maydell:
Quoted 3 lines
This makes the array zero-length for 32 bit targets, but functions like
tcg_out_tlb_load()andtcg_out_qemu_ld()still unconditionally access elements in it...Thanks for the hint. I'm afraid there are a lot more functions of that kind in
i386/tcg-target.c.I could use conditional compilation for those accesses, but first I'd like to understand why this works at all.
The functions in question are generating code that needs to use some temporary registers. (As RTH says, this lines up with the L constraint marking those registers as not usable for the relevant TCG operands.) To avoid unnecessary register copies in the old pre-commit-6a18ae2d register-based calling convention, they are forcing those temporary registers to be the same as some of the registers that were used for function arguments.
Now we no longer use a register-based calling convention it doesn't matter which registers we use as long as the functions and the constraint definitions match up. The only tricky bit will be that the 64-bit-target cases do still care about using the same registers as are used for function inputs.
-- PMM
PATCH 3/4 tcg: Move tcg_target_get_call_iarg_regs_count to tcg.c
In reply to Stefan WeilThe TCG targets no longer need individual implementations.
--- tcg/arm/tcg-target.c | 6 ------ tcg/hppa/tcg-target.c | 6 ------ tcg/i386/tcg-target.c | 6 ------ tcg/ia64/tcg-target.c | 6 ------ tcg/mips/tcg-target.c | 6 ------ tcg/ppc/tcg-target.c | 6 ------ tcg/ppc64/tcg-target.c | 6 ------ tcg/s390/tcg-target.c | 5 ----- tcg/sparc/tcg-target.c | 6 ------ tcg/tcg.c | 7 ++++++- tcg/tci/tcg-target.c | 6 ------ 11 files changed, 6 insertions(+), 60 deletions(-)
tcg/arm/tcg-target.c +0 -6
@@ -145,12 +145,6 @@ static void patch_reloc(uint8_t *code_ptr, int type, } } -/* maximum number of register used for input function arguments */ -static inline int tcg_target_get_call_iarg_regs_count (int flags) -{ -return 4 ; -} -/* parse target specific constraints */ static int target_parse_constraint (TCGArgConstraint *ct,const char **pct_str) {
tcg/hppa/tcg-target.c +0 -6
@@ -175,12 +175,6 @@ static void patch_reloc(uint8_t *code_ptr, int type, *insn_ptr = insn; } -/* maximum number of register used for input function arguments */ -static inline int tcg_target_get_call_iarg_regs_count (int flags) -{ -return 4 ; -} -/* parse target specific constraints */ static int target_parse_constraint (TCGArgConstraint *ct,const char **pct_str) {
tcg/i386/tcg-target.c +0 -6
@@ -112,12 +112,6 @@ static void patch_reloc(uint8_t *code_ptr, int type, } } -/* maximum number of register used for input function arguments */ -static inline int tcg_target_get_call_iarg_regs_count (int flags) -{ -return ARRAY_SIZE (tcg_target_call_iarg_regs); -} -/* parse target specific constraints */ static int target_parse_constraint (TCGArgConstraint *ct,const char **pct_str) {
tcg/ia64/tcg-target.c +0 -6
@@ -176,12 +176,6 @@ static const int tcg_target_call_oarg_regs[] = { TCG_REG_R8 }; -/* maximum number of register used for input function arguments */ -static inline int tcg_target_get_call_iarg_regs_count (int flags) -{ -return 8 ; -} -/* * opcode formation */
tcg/mips/tcg-target.c +0 -6
@@ -185,12 +185,6 @@ static void patch_reloc(uint8_t *code_ptr, int type, } } -/* maximum number of register used for input function arguments */ -static inline int tcg_target_get_call_iarg_regs_count (int flags) -{ -return 4 ; -} -/* parse target specific constraints */ static int target_parse_constraint (TCGArgConstraint *ct,const char **pct_str) {
tcg/ppc/tcg-target.c +0 -6
@@ -221,12 +221,6 @@ static void patch_reloc(uint8_t *code_ptr, int type, } } -/* maximum number of register used for input function arguments */ -static int tcg_target_get_call_iarg_regs_count (int flags) -{ -return ARRAY_SIZE (tcg_target_call_iarg_regs); -} -/* parse target specific constraints */ static int target_parse_constraint (TCGArgConstraint *ct,const char **pct_str) {
tcg/ppc64/tcg-target.c +0 -6
@@ -208,12 +208,6 @@ static void patch_reloc (uint8_t *code_ptr, int type, } } -/* maximum number of register used for input function arguments */ -static int tcg_target_get_call_iarg_regs_count (int flags) -{ -return ARRAY_SIZE (tcg_target_call_iarg_regs); -} -/* parse target specific constraints */ static int target_parse_constraint (TCGArgConstraint *ct,const char **pct_str) {
tcg/s390/tcg-target.c +0 -5
@@ -376,11 +376,6 @@ static void patch_reloc(uint8_t *code_ptr, int type, } } -static int tcg_target_get_call_iarg_regs_count (int flags) -{ -return sizeof (tcg_target_call_iarg_regs) /sizeof (int ); -} -/* parse target specific constraints */ static int target_parse_constraint (TCGArgConstraint *ct,const char **pct_str) {
tcg/sparc/tcg-target.c +0 -6
@@ -137,12 +137,6 @@ static void patch_reloc(uint8_t *code_ptr, int type, } } -/* maximum number of register used for input function arguments */ -static inline int tcg_target_get_call_iarg_regs_count (int flags) -{ -return 6 ; -} -/* parse target specific constraints */ static int target_parse_constraint (TCGArgConstraint *ct,const char **pct_str) {
tcg/tcg.c +6 -1
@@ -89,7 +89,6 @@ static void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg, TCGReg arg1, tcg_target_long arg2);static int tcg_target_const_match (tcg_target_long val,const TCGArgConstraint *arg_ct); -static int tcg_target_get_call_iarg_regs_count (int flags);TCGOpDef tcg_op_defs[] = { #define DEF(s, oargs, iargs, cargs, flags) { #s, oargs, iargs, cargs, iargs + oargs + cargs, flags },
@@ -182,6 +181,12 @@ int gen_new_label(void)#include "tcg-target.c" +/* Maximum number of register used for input function arguments. */ +static inline int tcg_target_get_call_iarg_regs_count (int flags) +{ +return ARRAY_SIZE (tcg_target_call_iarg_regs); +} +/* pool based memory allocation */ void *tcg_malloc_internal (TCGContext *s,int size) {
tcg/tci/tcg-target.c +0 -6
@@ -891,12 +891,6 @@ static int tcg_target_const_match(tcg_target_long val,return arg_ct->ct &TCG_CT_CONST ; } -/* Maximum number of register used for input function arguments. */ -static int tcg_target_get_call_iarg_regs_count (int flags) -{ -return ARRAY_SIZE (tcg_target_call_iarg_regs); -} -static void tcg_target_init (TCGContext *s) {#if defined(CONFIG_DEBUG_TCG_INTERPRETER )
Signature · 1 line
1.7.10
Re: [Qemu-devel] [PATCH 3/4] tcg: Move tcg_target_get_call_iarg_regs_count to tcg.c
Quoted 182 lines · Stefan Weil
The TCG targets no longer need individual implementations.
Signed-off-by Stefan Weil <[email protected]>--- tcg/arm/tcg-target.c | 6 ------ tcg/hppa/tcg-target.c | 6 ------ tcg/i386/tcg-target.c | 6 ------ tcg/ia64/tcg-target.c | 6 ------ tcg/mips/tcg-target.c | 6 ------ tcg/ppc/tcg-target.c | 6 ------ tcg/ppc64/tcg-target.c | 6 ------ tcg/s390/tcg-target.c | 5 ----- tcg/sparc/tcg-target.c | 6 ------ tcg/tcg.c | 7 ++++++- tcg/tci/tcg-target.c | 6 ------ 11 files changed, 6 insertions(+), 60 deletions(-)tcg/arm/tcg-target.c +0
-6@@ -145,12 +145,6 @@ static void patch_reloc(uint8_t *code_ptr, int type, } } -/* maximum number of register used for input function arguments */ -static inline int tcg_target_get_call_iarg_regs_count (int flags) -{ -return 4 ; -} -/* parse target specific constraints */ static int target_parse_constraint (TCGArgConstraint *ct,const char **pct_str) {tcg/hppa/tcg-target.c +0
-6@@ -175,12 +175,6 @@ static void patch_reloc(uint8_t *code_ptr, int type, *insn_ptr = insn; } -/* maximum number of register used for input function arguments */ -static inline int tcg_target_get_call_iarg_regs_count (int flags) -{ -return 4 ; -} -/* parse target specific constraints */ static int target_parse_constraint (TCGArgConstraint *ct,const char **pct_str) {tcg/i386/tcg-target.c +0
-6@@ -112,12 +112,6 @@ static void patch_reloc(uint8_t *code_ptr, int type, } } -/* maximum number of register used for input function arguments */ -static inline int tcg_target_get_call_iarg_regs_count (int flags) -{ -return ARRAY_SIZE (tcg_target_call_iarg_regs); -} -/* parse target specific constraints */ static int target_parse_constraint (TCGArgConstraint *ct,const char **pct_str) {tcg/ia64/tcg-target.c +0
-6@@ -176,12 +176,6 @@ static const int tcg_target_call_oarg_regs[] = { TCG_REG_R8 }; -/* maximum number of register used for input function arguments */ -static inline int tcg_target_get_call_iarg_regs_count (int flags) -{ -return 8 ; -} -/* * opcode formation */ tcg/mips/tcg-target.c +0
-6@@ -185,12 +185,6 @@ static void patch_reloc(uint8_t *code_ptr, int type, } } -/* maximum number of register used for input function arguments */ -static inline int tcg_target_get_call_iarg_regs_count (int flags) -{ -return 4 ; -} -/* parse target specific constraints */ static int target_parse_constraint (TCGArgConstraint *ct,const char **pct_str) {tcg/ppc/tcg-target.c +0
-6@@ -221,12 +221,6 @@ static void patch_reloc(uint8_t *code_ptr, int type, } } -/* maximum number of register used for input function arguments */ -static int tcg_target_get_call_iarg_regs_count (int flags) -{ -return ARRAY_SIZE (tcg_target_call_iarg_regs); -} -/* parse target specific constraints */ static int target_parse_constraint (TCGArgConstraint *ct,const char **pct_str) {tcg/ppc64/tcg-target.c +0
-6@@ -208,12 +208,6 @@ static void patch_reloc (uint8_t *code_ptr, int type, } } -/* maximum number of register used for input function arguments */ -static int tcg_target_get_call_iarg_regs_count (int flags) -{ -return ARRAY_SIZE (tcg_target_call_iarg_regs); -} -/* parse target specific constraints */ static int target_parse_constraint (TCGArgConstraint *ct,const char **pct_str) {tcg/s390/tcg-target.c +0
-5@@ -376,11 +376,6 @@ static void patch_reloc(uint8_t *code_ptr, int type, } } -static int tcg_target_get_call_iarg_regs_count (int flags) -{ -return sizeof (tcg_target_call_iarg_regs) /sizeof (int ); -} -/* parse target specific constraints */ static int target_parse_constraint (TCGArgConstraint *ct,const char **pct_str) {tcg/sparc/tcg-target.c +0
-6@@ -137,12 +137,6 @@ static void patch_reloc(uint8_t *code_ptr, int type, } } -/* maximum number of register used for input function arguments */ -static inline int tcg_target_get_call_iarg_regs_count (int flags) -{ -return 6 ; -} -/* parse target specific constraints */ static int target_parse_constraint (TCGArgConstraint *ct,const char **pct_str) {tcg/tcg.c +0
-1@@ -89,7 +89,6 @@ static void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg, TCGReg arg1, tcg_target_long arg2);static int tcg_target_const_match (tcg_target_long val,const TCGArgConstraint *arg_ct); -static int tcg_target_get_call_iarg_regs_count (int flags);TCGOpDef tcg_op_defs[] = { #define DEF(s, oargs, iargs, cargs, flags) { #s, oargs, iargs, cargs, iargs + oargs + cargs, flags },
tcg/tcg.c int gen_new_label(void) · line 189
Quoted 9 lines
tcg/tcg.c +6
-0@@ -182,6 +181,12 @@ int gen_new_label(void)#include "tcg-target.c" +/* Maximum number of register used for input function arguments. */ +static inline int tcg_target_get_call_iarg_regs_count (int flags) +{ +return ARRAY_SIZE (tcg_target_call_iarg_regs); +} +
Do we really need a function for that, given that it has only one caller?
For me ARRAY_SIZE(tcg_target_call_iarg_regs) is even more understandable than tcg_target_get_call_iarg_regs_count().
Quoted 24 lines
tcg/tcg.c +0
-0/* pool based memory allocation */ void *tcg_malloc_internal (TCGContext *s,int size) {tcg/tci/tcg-target.c +0
-6@@ -891,12 +891,6 @@ static int tcg_target_const_match(tcg_target_long val,return arg_ct->ct &TCG_CT_CONST ; } -/* Maximum number of register used for input function arguments. */ -static int tcg_target_get_call_iarg_regs_count (int flags) -{ -return ARRAY_SIZE (tcg_target_call_iarg_regs); -} -static void tcg_target_init (TCGContext *s) {#if defined(CONFIG_DEBUG_TCG_INTERPRETER )Signature · 1 line
1.7.10
Signature · 2 lines
Aurelien Jarno GPG: 1024D/F1BCDB73 [email protected] http://www.aurel32.net
Re: [Qemu-devel] [PATCH 3/4] tcg: Move tcg_target_get_call_iarg_regs_count to tcg.c
Am 12.09.2012 22:59, schrieb Aurelien Jarno:
Quoted 19 lines · Stefan Weil
Quoted 18 lines
The TCG targets no longer need individual implementations.
Signed-off-by Stefan Weil <[email protected]>--- tcg/arm/tcg-target.c | 6 ------ tcg/hppa/tcg-target.c | 6 ------ tcg/i386/tcg-target.c | 6 ------ tcg/ia64/tcg-target.c | 6 ------ tcg/mips/tcg-target.c | 6 ------ tcg/ppc/tcg-target.c | 6 ------ tcg/ppc64/tcg-target.c | 6 ------ tcg/s390/tcg-target.c | 5 ----- tcg/sparc/tcg-target.c | 6 ------ tcg/tcg.c | 7 ++++++- tcg/tci/tcg-target.c | 6 ------ 11 files changed, 6 insertions(+), 60 deletions(-)
[...]
Quoted 25 lines
Quoted 13 lines
tcg/tcg.c +0
-1@@ -89,7 +89,6 @@ static void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg, TCGReg arg1, tcg_target_long arg2);static int tcg_target_const_match (tcg_target_long val,const TCGArgConstraint *arg_ct); -static int tcg_target_get_call_iarg_regs_count (int flags);TCGOpDef tcg_op_defs[] = { #define DEF(s, oargs, iargs, cargs, flags) { #s, oargs, iargs, cargs, iargs + oargs + cargs, flags },tcg/tcg.c int gen_new_label(void) · line 189
Quoted 9 lines
tcg/tcg.c +6
-0@@ -182,6 +181,12 @@ int gen_new_label(void)#include "tcg-target.c" +/* Maximum number of register used for input function arguments. */ +static inline int tcg_target_get_call_iarg_regs_count (int flags) +{ +return ARRAY_SIZE (tcg_target_call_iarg_regs); +} +Do we really need a function for that, given that it has only one caller?
Quoted 3 lines
For me
ARRAY_SIZE(tcg_target_call_iarg_regs)is even more understandable thantcg_target_get_call_iarg_regs_count().
I agree. tcg_target_get_call_iarg_regs_count can be removed completely, but only if we find a solution for the problem which Peter Maydell found: some code accesses tcg_target_call_iarg_regs unconditionally even for x86 32 bit, so for the moment, we cannot reduce its size to 0.
Stefan
PATCH 4/4 tcg: Remove unused parameter from tcg_target_get_call_iarg_regs_count
In reply to Stefan WeilSince commit 6a18ae2d2947532d5c26439548afa0481c4529f9, 'flags' is no longer used in tcg_target_get_call_iarg_regs_count.
--- tcg/tcg.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tcg/tcg.c +2 -2
@@ -182,7 +182,7 @@ int gen_new_label(void)#include "tcg-target.c" /* Maximum number of register used for input function arguments. */ -static inline int tcg_target_get_call_iarg_regs_count (int flags) +static inline int tcg_target_get_call_iarg_regs_count (void ) {return ARRAY_SIZE (tcg_target_call_iarg_regs); }
@@ -1871,7 +1871,7 @@ static int tcg_reg_alloc_call(TCGContext *s, const TCGOpDef *def, flags = args[nb_oargs + nb_iargs]; - nb_regs =tcg_target_get_call_iarg_regs_count (flags); + nb_regs =tcg_target_get_call_iarg_regs_count ();if (nb_regs > nb_params) nb_regs = nb_params;
Signature · 1 line
1.7.10