[PATCH 03/18] KVM: PPC: Teach MMIO Signedness
The guest I was trying to get to run uses the LHA and LHAU instructions. Those instructions basically do a load, but also sign extend the result. Since we need to fill our registers by hand when doing MMIO, we also need to sign extend manually. This patch implements sign extended MMIO and the LHA(U) instructions. Signed-off-by: Alexander Graf --- arch/powerpc/include/asm/kvm_host.h |1 + arch/powerpc/include/asm/kvm_ppc.h |3 +++ arch/powerpc/kvm/emulate.c | 14 ++ arch/powerpc/kvm/powerpc.c | 32 4 files changed, 50 insertions(+), 0 deletions(-) diff --git a/arch/powerpc/include/asm/kvm_host.h b/arch/powerpc/include/asm/kvm_host.h index 2ed954e..4dd98fa 100644 --- a/arch/powerpc/include/asm/kvm_host.h +++ b/arch/powerpc/include/asm/kvm_host.h @@ -268,6 +268,7 @@ struct kvm_vcpu_arch { u8 io_gpr; /* GPR used as IO source/target */ u8 mmio_is_bigendian; + u8 mmio_sign_extend; u8 dcr_needed; u8 dcr_is_write; diff --git a/arch/powerpc/include/asm/kvm_ppc.h b/arch/powerpc/include/asm/kvm_ppc.h index c011170..a288dd2 100644 --- a/arch/powerpc/include/asm/kvm_ppc.h +++ b/arch/powerpc/include/asm/kvm_ppc.h @@ -48,6 +48,9 @@ extern void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu); extern int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu, unsigned int rt, unsigned int bytes, int is_bigendian); +extern int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu, + unsigned int rt, unsigned int bytes, + int is_bigendian); extern int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu, u64 val, unsigned int bytes, int is_bigendian); diff --git a/arch/powerpc/kvm/emulate.c b/arch/powerpc/kvm/emulate.c index b905623..ef2ff59 100644 --- a/arch/powerpc/kvm/emulate.c +++ b/arch/powerpc/kvm/emulate.c @@ -62,6 +62,8 @@ #define OP_STBU 39 #define OP_LHZ 40 #define OP_LHZU 41 +#define OP_LHA 42 +#define OP_LHAU 43 #define OP_STH 44 #define OP_STHU 45 @@ -450,6 +452,18 @@ int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu) kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed); break; + case OP_LHA: + rt = get_rt(inst); + emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1); + break; + + case OP_LHAU: + ra = get_ra(inst); + rt = get_rt(inst); + emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1); + kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed); + break; + case OP_STH: rs = get_rs(inst); emulated = kvmppc_handle_store(run, vcpu, diff --git a/arch/powerpc/kvm/powerpc.c b/arch/powerpc/kvm/powerpc.c index 98d5e6d..a235369 100644 --- a/arch/powerpc/kvm/powerpc.c +++ b/arch/powerpc/kvm/powerpc.c @@ -300,6 +300,25 @@ static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu, } } + if (vcpu->arch.mmio_sign_extend) { + switch (run->mmio.len) { +#ifdef CONFIG_PPC64 + case 4: + if (gpr & 0x8000) + gpr |= 0xULL; + break; +#endif + case 2: + if (gpr & 0x8000) + gpr |= 0xULL; + break; + case 1: + if (gpr & 0x80) + gpr |= 0xff00ULL; + break; + } + } + kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr); switch (vcpu->arch.io_gpr & REG_EXT_MASK) { @@ -337,10 +356,23 @@ int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu, vcpu->arch.mmio_is_bigendian = is_bigendian; vcpu->mmio_needed = 1; vcpu->mmio_is_write = 0; + vcpu->arch.mmio_sign_extend = 0; return EMULATE_DO_MMIO; } +/* Same as above, but sign extends */ +int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu, +unsigned int rt, unsigned int bytes, int is_bigendian) +{ + int r; + + r = kvmppc_handle_load(run, vcpu, rt, bytes, is_bigendian); + vcpu->arch.mmio_sign_extend = 1; + + return r; +} + int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu, u64 val, unsigned int bytes, int is_bigendian) { -- 1.6.0.2 -- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH 03/18] KVM: PPC: Teach MMIO Signedness
On 02/04/2010 05:55 PM, Alexander Graf wrote: The guest I was trying to get to run uses the LHA and LHAU instructions. Those instructions basically do a load, but also sign extend the result. Since we need to fill our registers by hand when doing MMIO, we also need to sign extend manually. This patch implements sign extended MMIO and the LHA(U) instructions. @@ -300,6 +300,25 @@ static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu, } } + if (vcpu->arch.mmio_sign_extend) { + switch (run->mmio.len) { +#ifdef CONFIG_PPC64 + case 4: + if (gpr& 0x8000) + gpr |= 0xULL; + break; Wouldn't gpr = (s64)(gpr << 32) >> 32; work? Not sure if >> is guaranteed to sign extend. -- error compiling committee.c: too many arguments to function -- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH 03/18] KVM: PPC: Teach MMIO Signedness
Am 07.02.2010 um 13:32 schrieb Avi Kivity : On 02/04/2010 05:55 PM, Alexander Graf wrote: The guest I was trying to get to run uses the LHA and LHAU instructions. Those instructions basically do a load, but also sign extend the result. Since we need to fill our registers by hand when doing MMIO, we also need to sign extend manually. This patch implements sign extended MMIO and the LHA(U) instructions. @@ -300,6 +300,25 @@ static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu, } } +if (vcpu->arch.mmio_sign_extend) { +switch (run->mmio.len) { +#ifdef CONFIG_PPC64 +case 4: +if (gpr& 0x8000) +gpr |= 0xULL; +break; Wouldn't gpr = (s64)(gpr << 32) >> 32; work? Not sure if >> is guaranteed to sign extend. Not sure either. The code as is is rather obvious imho, so I wouldn't want to replace it with anything that's even remotely magical. Alex -- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH 03/18] KVM: PPC: Teach MMIO Signedness
On 02/07/2010 05:51 PM, Alexander Graf wrote: +if (vcpu->arch.mmio_sign_extend) { +switch (run->mmio.len) { +#ifdef CONFIG_PPC64 +case 4: +if (gpr& 0x8000) +gpr |= 0xULL; +break; Wouldn't gpr = (s64)(gpr << 32) >> 32; work? Not sure if >> is guaranteed to sign extend. Not sure either. The code as is is rather obvious imho, so I wouldn't want to replace it with anything that's even remotely magical. That's fair. -- error compiling committee.c: too many arguments to function -- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH 03/18] KVM: PPC: Teach MMIO Signedness
On 02/07/2010 06:32 AM, Avi Kivity wrote: On 02/04/2010 05:55 PM, Alexander Graf wrote: The guest I was trying to get to run uses the LHA and LHAU instructions. Those instructions basically do a load, but also sign extend the result. Since we need to fill our registers by hand when doing MMIO, we also need to sign extend manually. This patch implements sign extended MMIO and the LHA(U) instructions. @@ -300,6 +300,25 @@ static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu, } } +if (vcpu->arch.mmio_sign_extend) { +switch (run->mmio.len) { +#ifdef CONFIG_PPC64 +case 4: +if (gpr& 0x8000) +gpr |= 0xULL; +break; Wouldn't gpr = (s64)(gpr << 32) >> 32; work? Not sure if >> is guaranteed to sign extend. It's technically implementation dependent but I don't know of an implementation that doesn't sign extend. Regards, Anthony Liguori -- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH 03/18] KVM: PPC: Teach MMIO Signedness
Am 07.02.2010 um 17:27 schrieb Anthony Liguori : On 02/07/2010 06:32 AM, Avi Kivity wrote: On 02/04/2010 05:55 PM, Alexander Graf wrote: The guest I was trying to get to run uses the LHA and LHAU instructions. Those instructions basically do a load, but also sign extend the result. Since we need to fill our registers by hand when doing MMIO, we also need to sign extend manually. This patch implements sign extended MMIO and the LHA(U) instructions. @@ -300,6 +300,25 @@ static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu, } } +if (vcpu->arch.mmio_sign_extend) { +switch (run->mmio.len) { +#ifdef CONFIG_PPC64 +case 4: +if (gpr& 0x8000) +gpr |= 0xULL; +break; Wouldn't gpr = (s64)(gpr << 32) >> 32; work? Not sure if >> is guaranteed to sign extend. It's technically implementation dependent but I don't know of an implementation that doesn't sign extend. Hrm, would gpr = (s64)(s32)gpr; work? :) Alex -- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH 03/18] KVM: PPC: Teach MMIO Signedness
On 02/07/2010 03:35 PM, Alexander Graf wrote: It's technically implementation dependent but I don't know of an implementation that doesn't sign extend. Hrm, would gpr = (s64)(s32)gpr; work? :) Yes. Integer promotion does guarantee sign extension. Regards, Anthony Liguori Alex -- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html