target-ppc: endian fixes for KVM and gdbstub
cover target-ppc: endian fixes for KVM and gdbstub
Hi,
This series is a sequel to Anton's tentative at bringing VSX support in our gdbstub:
http://patchwork.ozlabs.org/patch/453758/
Indeed, FP, SPE and Altivec registers need to be copied to memory with the appropriate ordering, like we already do for core registers. This series reuses the maybe_bswap_register() helper to do the job, since it already handles the user mode case where the target endianness is known at build time and don't need byteswap. This is covered by patches 2 to 6.
I also found more serious issues that probably break more than gdbstub.
First one is a bug in KVM, that completely breaks the KVM_GET_ONE_REG and KVM_SET_ONE_REG ioctls for Altivec registers. I've already sent a patch:
http://patchwork.ozlabs.org/patch/557568/
Second one is a bug in QEMU that breaks synchronisation with KVM for FP, Altivec and VSX registers on little-endian hosts. I pushed the fix to patch 1 since it is needed for the gdbstub fixes to actually work, but it could even be handled separately.
And finally, patch 7 is Anton's + the byteswapping for little-endian guests.
Cheers.
---
Anton Blanchard (1):
target-ppc: gdbstub: Add VSX support
Greg Kurz (6):
target-ppc: kvm: fix floating point registers sync on little-endian hosts
target-ppc: rename and export maybe_bswap_register()
target-ppc: gdbstub: fix float registers for little-endian guests
target-ppc: gdbstub: introduce avr_need_swap()
target-ppc: gdbstub: fix altivec registers for little-endian guests
target-ppc: gdbstub: fix spe registers for little-endian guests
configure | 6 ++- gdb-xml/power-vsx.xml | 44 +++++++++++++++++++++++ target-ppc/cpu.h | 1 + target-ppc/gdbstub.c | 10 +++-- target-ppc/kvm.c | 12 ++++++ target-ppc/translate_init.c | 84 +++++++++++++++++++++++++++++++++++-------- 6 files changed, 134 insertions(+), 23 deletions(-) create mode 100644 gdb-xml/power-vsx.xml
PATCH 1/7 target-ppc: kvm: fix floating point registers sync on little-endian hosts
On VSX capable CPUs, the 32 FP registers are mapped to the high-bits of the 32 first VSX registers. So if you have:
VSR31 = (uint128) 0x0102030405060708090a0b0c0d0e0f00
then
FPR31 = (uint64) 0x0102030405060708
The kernel stores the VSX registers in the fp_state struct following the host endian element ordering.
On big-endian:
fp_state.fpr[31][0] = 0x0102030405060708
fp_state.fpr[31][1] = 0x090a0b0c0d0e0f00
On little-endian:
fp_state.fpr[31][0] = 0x090a0b0c0d0e0f00
fp_state.fpr[31][1] = 0x0102030405060708
The KVM_GET_ONE_REG and KVM_SET_ONE_REG ioctls preserve this ordering, but QEMU considers it as big-endian and always copies element [0] to the fpr[] array and element [1] to the vsr[] array. This does not work with little-endian hosts, and you will get:
(qemu) p $f310x90a0b0c0d0e0f00
instead of:
(qemu) p $f310x102030405060708
This patch fixes the element ordering for little-endian hosts.
--- target-ppc/kvm.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
target-ppc/kvm.c +12 -0
@@ -650,8 +650,13 @@ static int kvm_put_fp(CPUState *cs)for (i =0 ; i <32 ; i++) {uint64_t vsr[2 ]; +#ifdef HOST_WORDS_BIGENDIAN vsr[0 ] =float64_val (env->fpr[i]); vsr[1 ] = env->vsr[i]; +#else + vsr[0 ] = env->vsr[i]; + vsr[1 ] =float64_val (env->fpr[i]); +#endif reg.addr = (uintptr_t ) &vsr; reg.id = vsx ?KVM_REG_PPC_VSR (i) :KVM_REG_PPC_FPR (i);
@@ -721,10 +726,17 @@ static int kvm_get_fp(CPUState *cs) vsx ? "VSR" : "FPR", i, strerror(errno));return ret; }else { +#ifdef HOST_WORDS_BIGENDIAN env->fpr[i] = vsr[0 ];if (vsx) { env->vsr[i] = vsr[1 ]; } +#else + env->fpr[i] = vsr[1 ]; +if (vsx) { + env->vsr[i] = vsr[0 ]; + } +#endif } } }
PATCH 2/7 target-ppc: rename and export maybe_bswap_register()
In reply to Greg KurzThis helper will be used to support FP, Altivec and VSX registers when the guest is little-endian.
--- target-ppc/cpu.h | 1 + target-ppc/gdbstub.c | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-)
target-ppc/cpu.h +1 -0
@@ -2355,4 +2355,5 @@ int ppc_get_vcpu_dt_id(PowerPCCPU *cpu);*/ PowerPCCPU *ppc_get_vcpu_by_dt_id (int cpu_dt_id); +void ppc_maybe_bswap_register (CPUPPCState *env,uint8_t *mem_buf,int len);#endif /* !defined (__CPU_PPC_H__) */
target-ppc/gdbstub.c +5 -5
@@ -88,7 +88,7 @@ static int ppc_gdb_register_len(int n)the proper ordering for the binary, and cannot be changed. For system mode, TARGET_WORDS_BIGENDIAN is always set, and we must check the current mode of the chip to see if we're running in little-endian. */ -static void maybe_bswap_register (CPUPPCState *env,uint8_t *mem_buf,int len) +void ppc_maybe_bswap_register (CPUPPCState *env,uint8_t *mem_buf,int len) {#ifndef CONFIG_USER_ONLY if (!msr_le) {
@@ -158,7 +158,7 @@ int ppc_cpu_gdb_read_register(CPUState *cs, uint8_t *mem_buf, int n)break ; } } -maybe_bswap_register (env, mem_buf, r); +ppc_maybe_bswap_register (env, mem_buf, r);return r; }
@@ -214,7 +214,7 @@ int ppc_cpu_gdb_read_register_apple(CPUState *cs, uint8_t *mem_buf, int n)break ; } } -maybe_bswap_register (env, mem_buf, r); +ppc_maybe_bswap_register (env, mem_buf, r);return r; }
@@ -227,7 +227,7 @@ int ppc_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n)if (!r) {return r; } -maybe_bswap_register (env, mem_buf, r); +ppc_maybe_bswap_register (env, mem_buf, r);if (n <32 ) {/* gprs */ env->gpr[n] =ldtul_p (mem_buf);
@@ -277,7 +277,7 @@ int ppc_cpu_gdb_write_register_apple(CPUState *cs, uint8_t *mem_buf, int n)if (!r) {return r; } -maybe_bswap_register (env, mem_buf, r); +ppc_maybe_bswap_register (env, mem_buf, r);if (n <32 ) {/* gprs */ env->gpr[n] =ldq_p (mem_buf);
PATCH 3/7 target-ppc: gdbstub: fix float registers for little-endian guests
In reply to Greg KurzLet's reuse the ppc_maybe_bswap_register() helper, like we already do with the general registers.
--- target-ppc/translate_init.c | 4 ++++ 1 file changed, 4 insertions(+)
target-ppc/translate_init.c +4 -0
@@ -8755,10 +8755,12 @@ static int gdb_get_float_reg(CPUPPCState *env, uint8_t *mem_buf, int n) {if (n <32 ) {stfq_p (mem_buf, env->fpr[n]); +ppc_maybe_bswap_register (env, mem_buf,8 );return 8 ; }if (n ==32 ) {stl_p (mem_buf, env->fpscr); +ppc_maybe_bswap_register (env, mem_buf,4 );return 4 ; }return 0 ;
@@ -8767,10 +8769,12 @@ static int gdb_get_float_reg(CPUPPCState *env, uint8_t *mem_buf, int n)static int gdb_set_float_reg (CPUPPCState *env,uint8_t *mem_buf,int n) {if (n <32 ) { +ppc_maybe_bswap_register (env, mem_buf,8 ); env->fpr[n] =ldfq_p (mem_buf);return 8 ; }if (n ==32 ) { +ppc_maybe_bswap_register (env, mem_buf,4 );helper_store_fpscr (env,ldl_p (mem_buf),0xffffffff );return 4 ; }
PATCH 4/7 target-ppc: gdbstub: introduce avr_need_swap()
In reply to Greg KurzThis helper will be used to support Altivec registers in little-endian guests. This patch does not change functionnality.
Note: I had to put the helper some lines away from the gdb_*_avr_reg() routines to get a more readable patch.
--- target-ppc/translate_init.c | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-)
target-ppc/translate_init.c +23 -14
@@ -8751,6 +8751,15 @@ static void dump_ppc_insns (CPUPPCState *env) }#endif +static bool avr_need_swap (CPUPPCState *env) +{ +#ifdef HOST_WORDS_BIGENDIAN +return false; +#else +return true; +#endif +} +static int gdb_get_float_reg (CPUPPCState *env,uint8_t *mem_buf,int n) {if (n <32 ) {
@@ -8784,13 +8793,13 @@ static int gdb_set_float_reg(CPUPPCState *env, uint8_t *mem_buf, int n)static int gdb_get_avr_reg (CPUPPCState *env,uint8_t *mem_buf,int n) {if (n <32 ) { -#ifdef HOST_WORDS_BIGENDIAN -stq_p (mem_buf, env->avr[n].u64[0 ]); -stq_p (mem_buf+8 , env->avr[n].u64[1 ]); -#else -stq_p (mem_buf, env->avr[n].u64[1 ]); -stq_p (mem_buf+8 , env->avr[n].u64[0 ]); -#endif +if (!avr_need_swap (env)) { +stq_p (mem_buf, env->avr[n].u64[0 ]); +stq_p (mem_buf+8 , env->avr[n].u64[1 ]); + }else { +stq_p (mem_buf, env->avr[n].u64[1 ]); +stq_p (mem_buf+8 , env->avr[n].u64[0 ]); + }return 16 ; }if (n ==32 ) {
@@ -8807,13 +8816,13 @@ static int gdb_get_avr_reg(CPUPPCState *env, uint8_t *mem_buf, int n)static int gdb_set_avr_reg (CPUPPCState *env,uint8_t *mem_buf,int n) {if (n <32 ) { -#ifdef HOST_WORDS_BIGENDIAN - env->avr[n].u64[0 ] =ldq_p (mem_buf); - env->avr[n].u64[1 ] =ldq_p (mem_buf+8 ); -#else - env->avr[n].u64[1 ] =ldq_p (mem_buf); - env->avr[n].u64[0 ] =ldq_p (mem_buf+8 ); -#endif +if (!avr_need_swap (env)) { + env->avr[n].u64[0 ] =ldq_p (mem_buf); + env->avr[n].u64[1 ] =ldq_p (mem_buf+8 ); + }else { + env->avr[n].u64[1 ] =ldq_p (mem_buf); + env->avr[n].u64[0 ] =ldq_p (mem_buf+8 ); + }return 16 ; }if (n ==32 ) {
PATCH 5/7 target-ppc: gdbstub: fix altivec registers for little-endian guests
In reply to Greg KurzAltivec registers are 128-bit wide. They are stored in memory as two 64-bit values that must be byteswapped when the guest is little-endian. Let's reuse the ppc_maybe_bswap_register() helper for this.
We also need to fix the ordering of the 64-bit elements according to the target endianness, for both system and user mode.
--- target-ppc/translate_init.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
target-ppc/translate_init.c +10 -2
@@ -8754,9 +8754,9 @@ static void dump_ppc_insns (CPUPPCState *env)static bool avr_need_swap (CPUPPCState *env) {#ifdef HOST_WORDS_BIGENDIAN -return false; +return msr_le;#else -return true; +return !msr_le;#endif }
@@ -8800,14 +8800,18 @@ static int gdb_get_avr_reg(CPUPPCState *env, uint8_t *mem_buf, int n)stq_p (mem_buf, env->avr[n].u64[1 ]);stq_p (mem_buf+8 , env->avr[n].u64[0 ]); } +ppc_maybe_bswap_register (env, mem_buf,8 ); +ppc_maybe_bswap_register (env, mem_buf +8 ,8 );return 16 ; }if (n ==32 ) {stl_p (mem_buf, env->vscr); +ppc_maybe_bswap_register (env, mem_buf,4 );return 4 ; }if (n ==33 ) {stl_p (mem_buf, (uint32_t )env->spr[SPR_VRSAVE ]); +ppc_maybe_bswap_register (env, mem_buf,4 );return 4 ; }return 0 ;
@@ -8816,6 +8820,8 @@ static int gdb_get_avr_reg(CPUPPCState *env, uint8_t *mem_buf, int n)static int gdb_set_avr_reg (CPUPPCState *env,uint8_t *mem_buf,int n) {if (n <32 ) { +ppc_maybe_bswap_register (env, mem_buf,8 ); +ppc_maybe_bswap_register (env, mem_buf +8 ,8 );if (!avr_need_swap (env)) { env->avr[n].u64[0 ] =ldq_p (mem_buf); env->avr[n].u64[1 ] =ldq_p (mem_buf+8 );
@@ -8826,10 +8832,12 @@ static int gdb_set_avr_reg(CPUPPCState *env, uint8_t *mem_buf, int n)return 16 ; }if (n ==32 ) { +ppc_maybe_bswap_register (env, mem_buf,4 ); env->vscr =ldl_p (mem_buf);return 4 ; }if (n ==33 ) { +ppc_maybe_bswap_register (env, mem_buf,4 ); env->spr[SPR_VRSAVE ] = (target_ulong )ldl_p (mem_buf);return 4 ; }
PATCH 6/7 target-ppc: gdbstub: fix spe registers for little-endian guests
In reply to Greg KurzLet's reuse the ppc_maybe_bswap_register() helper, like we already do with the general registers.
--- target-ppc/translate_init.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
target-ppc/translate_init.c +10 -1
@@ -8849,6 +8849,7 @@ static int gdb_get_spe_reg(CPUPPCState *env, uint8_t *mem_buf, int n)if (n <32 ) {#if defined(TARGET_PPC64 )stl_p (mem_buf, env->gpr[n] >>32 ); +ppc_maybe_bswap_register (env, mem_buf,4 );#else stl_p (mem_buf, env->gprh[n]);#endif
@@ -8856,10 +8857,12 @@ static int gdb_get_spe_reg(CPUPPCState *env, uint8_t *mem_buf, int n) }if (n ==32 ) {stq_p (mem_buf, env->spe_acc); +ppc_maybe_bswap_register (env, mem_buf,8 );return 8 ; }if (n ==33 ) {stl_p (mem_buf, env->spe_fscr); +ppc_maybe_bswap_register (env, mem_buf,4 );return 4 ; }return 0 ;
@@ -8870,7 +8873,11 @@ static int gdb_set_spe_reg(CPUPPCState *env, uint8_t *mem_buf, int n)if (n <32 ) {#if defined(TARGET_PPC64 )target_ulong lo = (uint32_t )env->gpr[n]; -target_ulong hi = (target_ulong )ldl_p (mem_buf) <<32 ; +target_ulong hi; + +ppc_maybe_bswap_register (env, mem_buf,4 ); + + hi = (target_ulong )ldl_p (mem_buf) <<32 ; env->gpr[n] = lo | hi;#else env->gprh[n] =ldl_p (mem_buf);
@@ -8878,10 +8885,12 @@ static int gdb_set_spe_reg(CPUPPCState *env, uint8_t *mem_buf, int n)return 4 ; }if (n ==32 ) { +ppc_maybe_bswap_register (env, mem_buf,8 ); env->spe_acc =ldq_p (mem_buf);return 8 ; }if (n ==33 ) { +ppc_maybe_bswap_register (env, mem_buf,4 ); env->spe_fscr =ldl_p (mem_buf);return 4 ; }
PATCH 7/7 target-ppc: gdbstub: Add VSX support
In reply to Greg KurzFrom: Anton Blanchard <[email protected]>
Add the XML and functions to get and set VSX registers.
(fixed little-endian guests)
--- configure | 6 +++--- gdb-xml/power-vsx.xml | 44 +++++++++++++++++++++++++++++++++++++++++++ target-ppc/translate_init.c | 24 +++++++++++++++++++++++ 3 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 gdb-xml/power-vsx.xml
configure +3 -3
@@ -5617,20 +5617,20 @@ case "$target_name" in ppc64) TARGET_BASE_ARCH=ppc TARGET_ABI_DIR=ppc - gdb_xml_files="power64-core.xml power-fpu.xml power-altivec.xml power-spe.xml" + gdb_xml_files="power64-core.xml power-fpu.xml power-altivec.xml power-spe.xml power-vsx.xml" ;; ppc64le) TARGET_ARCH=ppc64 TARGET_BASE_ARCH=ppc TARGET_ABI_DIR=ppc - gdb_xml_files="power64-core.xml power-fpu.xml power-altivec.xml power-spe.xml" + gdb_xml_files="power64-core.xml power-fpu.xml power-altivec.xml power-spe.xml power-vsx.xml" ;; ppc64abi32) TARGET_ARCH=ppc64 TARGET_BASE_ARCH=ppc TARGET_ABI_DIR=ppc echo "TARGET_ABI32=y" >> $config_target_mak - gdb_xml_files="power64-core.xml power-fpu.xml power-altivec.xml power-spe.xml" + gdb_xml_files="power64-core.xml power-fpu.xml power-altivec.xml power-spe.xml power-vsx.xml" ;; sh4|sh4eb) TARGET_ARCH=sh4
gdb-xml/power-vsx.xml new file +44 -0
44 lines not on this page · raw message
target-ppc/translate_init.c +24 -0
@@ -8897,6 +8897,26 @@ static int gdb_set_spe_reg(CPUPPCState *env, uint8_t *mem_buf, int n)return 0 ; } +static int gdb_get_vsx_reg (CPUPPCState *env,uint8_t *mem_buf,int n) +{ +if (n <32 ) { +stq_p (mem_buf, env->vsr[n]); +ppc_maybe_bswap_register (env, mem_buf,8 ); +return 8 ; + } +return 0 ; +} + +static int gdb_set_vsx_reg (CPUPPCState *env,uint8_t *mem_buf,int n) +{ +if (n <32 ) { +ppc_maybe_bswap_register (env, mem_buf,8 ); + env->vsr[n] =ldq_p (mem_buf); +return 8 ; + } +return 0 ; +} +static int ppc_fixup_cpu (PowerPCCPU *cpu) {CPUPPCState *env = &cpu->env;
@@ -9002,6 +9022,10 @@ static void ppc_cpu_realizefn(DeviceState *dev, Error **errp)gdb_register_coprocessor (cs, gdb_get_spe_reg, gdb_set_spe_reg,34 ,"power-spe.xml" ,0 ); } +if (pcc->insns_flags2 &PPC2_VSX ) { +gdb_register_coprocessor (cs, gdb_get_vsx_reg, gdb_set_vsx_reg, +32 ,"power-vsx.xml" ,0 ); + }qemu_init_vcpu (cs);
Quoted 27 lines · On Fri, 18 Dec 2015 11:18:16 +0100 Greg Kurz <[email protected]> wrote:
Hi,
This series is a sequel to Anton's tentative at bringing VSX support in our gdbstub:
http://patchwork.ozlabs.org/patch/453758/
Indeed, FP, SPE and Altivec registers need to be copied to memory with the appropriate ordering, like we already do for core registers. This series reuses the
maybe_bswap_register()helper to do the job, since it already handles the user mode case where the target endianness is known at build time and don't need byteswap. This is covered by patches 2 to 6.I also found more serious issues that probably break more than gdbstub.
First one is a bug in KVM, that completely breaks the
KVM_GET_ONE_REGandKVM_SET_ONE_REGioctls for Altivec registers. I've already sent a patch:http://patchwork.ozlabs.org/patch/557568/
Second one is a bug in QEMU that breaks synchronisation with KVM for FP, Altivec and VSX registers on little-endian hosts. I pushed the fix to patch 1 since it is needed for the gdbstub fixes to actually work, but it could even be handled separately.
Quoted 5 lines
And finally, patch 7 is Anton's + the byteswapping for little-endian guests.
Happy new year !
Cheers.
Ping ?
Quoted 24 lines
---
Anton Blanchard (1):
target-ppc: gdbstub: Add VSX supportGreg Kurz (6):
target-ppc: kvm: fix floating point registers sync on little-endian hosts
target-ppc: rename and exportmaybe_bswap_register()
target-ppc: gdbstub: fix float registers for little-endian guests
target-ppc: gdbstub: introduceavr_need_swap()
target-ppc: gdbstub: fix altivec registers for little-endian guests
target-ppc: gdbstub: fix spe registers for little-endian guestsconfigure | 6 ++- gdb-xml/power-vsx.xml | 44 +++++++++++++++++++++++ target-ppc/cpu.h | 1 + target-ppc/gdbstub.c | 10 +++-- target-ppc/kvm.c | 12 ++++++ target-ppc/translate_init.c | 84 +++++++++++++++++++++++++++++++++++-------- 6 files changed, 134 insertions(+), 23 deletions(-) create mode 100644 gdb-xml/power-vsx.xml