From: =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= Subject: [PATCH 3/3] KVM: PPC: Book3S: MMIO emulation support for little endian guests Date: Mon, 7 Oct 2013 16:27:47 +0200 Message-ID: <1381156067-32095-4-git-send-email-clg@fr.ibm.com> References: <1381156067-32095-1-git-send-email-clg@fr.ibm.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: kvm-ppc@vger.kernel.org, kvm@vger.kernel.org, =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= To: agraf@suse.de, paulus@samba.org Return-path: In-Reply-To: <1381156067-32095-1-git-send-email-clg@fr.ibm.com> Sender: kvm-ppc-owner@vger.kernel.org List-Id: kvm.vger.kernel.org MMIO emulation reads the last instruction executed by the guest and then emulates. If the guest is running in Little Endian mode, the instruction needs to be byte-swapped before being emulated. This patch stores the last instruction in the endian order of the host, primarily doing a byte-swap if needed. The common code which fetches 'last_inst' uses a helper routine kvmppc_need_byteswap(). and the exit paths for the Book3S PV and HR guests use their own version in assembly. =46inally, kvmppc_emulate_instruction() uses kvmppc_is_bigendian() to define in which endian order the mmio needs to be done. Signed-off-by: C=C3=A9dric Le Goater --- arch/powerpc/include/asm/kvm_book3s.h | 9 +++- arch/powerpc/kvm/book3s_hv_rmhandlers.S | 12 ++++++ arch/powerpc/kvm/book3s_segment.S | 11 +++++ arch/powerpc/kvm/emulate.c | 72 +++++++++++++++++------= -------- 4 files changed, 71 insertions(+), 33 deletions(-) diff --git a/arch/powerpc/include/asm/kvm_book3s.h b/arch/powerpc/inclu= de/asm/kvm_book3s.h index 00c2061..9c2b865 100644 --- a/arch/powerpc/include/asm/kvm_book3s.h +++ b/arch/powerpc/include/asm/kvm_book3s.h @@ -289,7 +289,14 @@ static inline int kvmppc_ld32(struct kvm_vcpu *vcp= u, ulong *eaddr, static inline int kvmppc_ld_inst(struct kvm_vcpu *vcpu, ulong *eaddr, u32 *inst) { - return kvmppc_ld32(vcpu, eaddr, inst, false); + int ret; + + ret =3D kvmppc_ld32(vcpu, eaddr, inst, false); + + if (kvmppc_need_byteswap(vcpu)) + *inst =3D swab32(*inst); + + return ret; } =20 static inline u32 kvmppc_get_last_inst(struct kvm_vcpu *vcpu) diff --git a/arch/powerpc/kvm/book3s_hv_rmhandlers.S b/arch/powerpc/kvm= /book3s_hv_rmhandlers.S index 77f1baa..7c9978a 100644 --- a/arch/powerpc/kvm/book3s_hv_rmhandlers.S +++ b/arch/powerpc/kvm/book3s_hv_rmhandlers.S @@ -1404,10 +1404,22 @@ fast_interrupt_c_return: lwz r8, 0(r10) mtmsrd r3 =20 + ld r0, VCPU_MSR(r9) + + /* r10 =3D vcpu->arch.msr & MSR_LE */ + rldicl. r10, r0, 0, 63 + /* Store the result */ stw r8, VCPU_LAST_INST(r9) =20 + beq after_inst_store + + /* Swap and store the result */ + addi r11, r9, VCPU_LAST_INST + stwbrx r8, 0, r11 + /* Unset guest mode. */ +after_inst_store: li r0, KVM_GUEST_MODE_HOST_HV stb r0, HSTATE_IN_GUEST(r13) b guest_exit_cont diff --git a/arch/powerpc/kvm/book3s_segment.S b/arch/powerpc/kvm/book3= s_segment.S index 1abe478..2ceed4c 100644 --- a/arch/powerpc/kvm/book3s_segment.S +++ b/arch/powerpc/kvm/book3s_segment.S @@ -287,8 +287,19 @@ ld_last_inst: sync =20 #endif + ld r8, SVCPU_SHADOW_SRR1(r13) + + /* r10 =3D vcpu->arch.msr & MSR_LE */ + rldicl. r10, r8, 0, 63 + stw r0, SVCPU_LAST_INST(r13) =20 + beq no_ld_last_inst + + /* swap and store the result */ + addi r11, r13, SVCPU_LAST_INST + stwbrx r0, 0, r11 + no_ld_last_inst: =20 /* Unset guest mode */ diff --git a/arch/powerpc/kvm/emulate.c b/arch/powerpc/kvm/emulate.c index 751cd45..76d0a12 100644 --- a/arch/powerpc/kvm/emulate.c +++ b/arch/powerpc/kvm/emulate.c @@ -219,7 +219,6 @@ static int kvmppc_emulate_mfspr(struct kvm_vcpu *vc= pu, int sprn, int rt) * lmw * stmw * - * XXX is_bigendian should depend on MMU mapping or MSR[LE] */ /* XXX Should probably auto-generate instruction decoding for a partic= ular core * from opcode tables in the future. */ @@ -232,6 +231,7 @@ int kvmppc_emulate_instruction(struct kvm_run *run,= struct kvm_vcpu *vcpu) int sprn =3D get_sprn(inst); enum emulation_result emulated =3D EMULATE_DONE; int advance =3D 1; + int is_bigendian =3D kvmppc_is_bigendian(vcpu); =20 /* this default type might be overwritten by subcategories */ kvmppc_set_exit_type(vcpu, EMULATED_INST_EXITS); @@ -266,47 +266,53 @@ int kvmppc_emulate_instruction(struct kvm_run *ru= n, struct kvm_vcpu *vcpu) advance =3D 0; break; case OP_31_XOP_LWZX: - emulated =3D kvmppc_handle_load(run, vcpu, rt, 4, 1); + emulated =3D kvmppc_handle_load(run, vcpu, rt, 4, + is_bigendian); break; =20 case OP_31_XOP_LBZX: - emulated =3D kvmppc_handle_load(run, vcpu, rt, 1, 1); + emulated =3D kvmppc_handle_load(run, vcpu, rt, 1, + is_bigendian); break; =20 case OP_31_XOP_LBZUX: - emulated =3D kvmppc_handle_load(run, vcpu, rt, 1, 1); + emulated =3D kvmppc_handle_load(run, vcpu, rt, 1, + is_bigendian); kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed); break; =20 case OP_31_XOP_STWX: emulated =3D kvmppc_handle_store(run, vcpu, kvmppc_get_gpr(vcpu, rs), - 4, 1); + 4, is_bigendian); break; =20 case OP_31_XOP_STBX: emulated =3D kvmppc_handle_store(run, vcpu, kvmppc_get_gpr(vcpu, rs), - 1, 1); + 1, is_bigendian); break; =20 case OP_31_XOP_STBUX: emulated =3D kvmppc_handle_store(run, vcpu, kvmppc_get_gpr(vcpu, rs), - 1, 1); + 1, is_bigendian); kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed); break; =20 case OP_31_XOP_LHAX: - emulated =3D kvmppc_handle_loads(run, vcpu, rt, 2, 1); + emulated =3D kvmppc_handle_loads(run, vcpu, rt, 2, + is_bigendian); break; =20 case OP_31_XOP_LHZX: - emulated =3D kvmppc_handle_load(run, vcpu, rt, 2, 1); + emulated =3D kvmppc_handle_load(run, vcpu, rt, 2, + is_bigendian); break; =20 case OP_31_XOP_LHZUX: - emulated =3D kvmppc_handle_load(run, vcpu, rt, 2, 1); + emulated =3D kvmppc_handle_load(run, vcpu, rt, 2, + is_bigendian); kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed); break; =20 @@ -317,13 +323,13 @@ int kvmppc_emulate_instruction(struct kvm_run *ru= n, struct kvm_vcpu *vcpu) case OP_31_XOP_STHX: emulated =3D kvmppc_handle_store(run, vcpu, kvmppc_get_gpr(vcpu, rs), - 2, 1); + 2, is_bigendian); break; =20 case OP_31_XOP_STHUX: emulated =3D kvmppc_handle_store(run, vcpu, kvmppc_get_gpr(vcpu, rs), - 2, 1); + 2, is_bigendian); kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed); break; =20 @@ -342,7 +348,8 @@ int kvmppc_emulate_instruction(struct kvm_run *run,= struct kvm_vcpu *vcpu) break; =20 case OP_31_XOP_LWBRX: - emulated =3D kvmppc_handle_load(run, vcpu, rt, 4, 0); + emulated =3D kvmppc_handle_load(run, vcpu, rt, 4, + !is_bigendian); break; =20 case OP_31_XOP_TLBSYNC: @@ -351,17 +358,18 @@ int kvmppc_emulate_instruction(struct kvm_run *ru= n, struct kvm_vcpu *vcpu) case OP_31_XOP_STWBRX: emulated =3D kvmppc_handle_store(run, vcpu, kvmppc_get_gpr(vcpu, rs), - 4, 0); + 4, !is_bigendian); break; =20 case OP_31_XOP_LHBRX: - emulated =3D kvmppc_handle_load(run, vcpu, rt, 2, 0); + emulated =3D kvmppc_handle_load(run, vcpu, rt, 2, + !is_bigendian); break; =20 case OP_31_XOP_STHBRX: emulated =3D kvmppc_handle_store(run, vcpu, kvmppc_get_gpr(vcpu, rs), - 2, 0); + 2, !is_bigendian); break; =20 default: @@ -371,33 +379,33 @@ int kvmppc_emulate_instruction(struct kvm_run *ru= n, struct kvm_vcpu *vcpu) break; =20 case OP_LWZ: - emulated =3D kvmppc_handle_load(run, vcpu, rt, 4, 1); + emulated =3D kvmppc_handle_load(run, vcpu, rt, 4, is_bigendian); break; =20 /* TBD: Add support for other 64 bit load variants like ldu, ldux, ld= x etc. */ case OP_LD: rt =3D get_rt(inst); - emulated =3D kvmppc_handle_load(run, vcpu, rt, 8, 1); + emulated =3D kvmppc_handle_load(run, vcpu, rt, 8, is_bigendian); break; =20 case OP_LWZU: - emulated =3D kvmppc_handle_load(run, vcpu, rt, 4, 1); + emulated =3D kvmppc_handle_load(run, vcpu, rt, 4, is_bigendian); kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed); break; =20 case OP_LBZ: - emulated =3D kvmppc_handle_load(run, vcpu, rt, 1, 1); + emulated =3D kvmppc_handle_load(run, vcpu, rt, 1, is_bigendian); break; =20 case OP_LBZU: - emulated =3D kvmppc_handle_load(run, vcpu, rt, 1, 1); + emulated =3D kvmppc_handle_load(run, vcpu, rt, 1, is_bigendian); kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed); break; =20 case OP_STW: emulated =3D kvmppc_handle_store(run, vcpu, kvmppc_get_gpr(vcpu, rs), - 4, 1); + 4, is_bigendian); break; =20 /* TBD: Add support for other 64 bit store variants like stdu, stdux,= stdx etc. */ @@ -405,57 +413,57 @@ int kvmppc_emulate_instruction(struct kvm_run *ru= n, struct kvm_vcpu *vcpu) rs =3D get_rs(inst); emulated =3D kvmppc_handle_store(run, vcpu, kvmppc_get_gpr(vcpu, rs), - 8, 1); + 8, is_bigendian); break; =20 case OP_STWU: emulated =3D kvmppc_handle_store(run, vcpu, kvmppc_get_gpr(vcpu, rs), - 4, 1); + 4, is_bigendian); kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed); break; =20 case OP_STB: emulated =3D kvmppc_handle_store(run, vcpu, kvmppc_get_gpr(vcpu, rs), - 1, 1); + 1, is_bigendian); break; =20 case OP_STBU: emulated =3D kvmppc_handle_store(run, vcpu, kvmppc_get_gpr(vcpu, rs), - 1, 1); + 1, is_bigendian); kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed); break; =20 case OP_LHZ: - emulated =3D kvmppc_handle_load(run, vcpu, rt, 2, 1); + emulated =3D kvmppc_handle_load(run, vcpu, rt, 2, is_bigendian); break; =20 case OP_LHZU: - emulated =3D kvmppc_handle_load(run, vcpu, rt, 2, 1); + emulated =3D kvmppc_handle_load(run, vcpu, rt, 2, is_bigendian); kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed); break; =20 case OP_LHA: - emulated =3D kvmppc_handle_loads(run, vcpu, rt, 2, 1); + emulated =3D kvmppc_handle_loads(run, vcpu, rt, 2, is_bigendian); break; =20 case OP_LHAU: - emulated =3D kvmppc_handle_loads(run, vcpu, rt, 2, 1); + emulated =3D kvmppc_handle_loads(run, vcpu, rt, 2, is_bigendian); kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed); break; =20 case OP_STH: emulated =3D kvmppc_handle_store(run, vcpu, kvmppc_get_gpr(vcpu, rs), - 2, 1); + 2, is_bigendian); break; =20 case OP_STHU: emulated =3D kvmppc_handle_store(run, vcpu, kvmppc_get_gpr(vcpu, rs), - 2, 1); + 2, is_bigendian); kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed); break; =20 --=20 1.7.10.4