]> git.kernelconcepts.de Git - karo-tx-linux.git/commitdiff
KVM: PPC: Book3S: MMIO emulation support for little endian guests
authorCédric Le Goater <clg@fr.ibm.com>
Thu, 9 Jan 2014 10:51:16 +0000 (11:51 +0100)
committerAlexander Graf <agraf@suse.de>
Mon, 27 Jan 2014 15:00:39 +0000 (16:00 +0100)
MMIO emulation reads the last instruction executed by the guest
and then emulates. If the guest is running in Little Endian order,
or more generally in a different endian order of the host, the
instruction needs to be byte-swapped before being emulated.

This patch adds a helper routine which tests the endian order of
the host and the guest in order to decide whether a byteswap is
needed or not. It is then used to byteswap the last instruction
of the guest in the endian order of the host before MMIO emulation
is performed.

Finally, kvmppc_handle_load() of kvmppc_handle_store() are modified
to reverse the endianness of the MMIO if required.

Signed-off-by: Cédric Le Goater <clg@fr.ibm.com>
[agraf: add booke handling]
Signed-off-by: Alexander Graf <agraf@suse.de>
arch/powerpc/include/asm/kvm_book3s.h
arch/powerpc/include/asm/kvm_booke.h
arch/powerpc/include/asm/kvm_ppc.h
arch/powerpc/kvm/book3s_64_mmu_hv.c
arch/powerpc/kvm/emulate.c
arch/powerpc/kvm/powerpc.c

index f8b23201c105aace7fcbc6636002c6d42a9daa87..1e9c26f45d185656651d42565782399a3a604458 100644 (file)
@@ -264,6 +264,11 @@ static inline ulong kvmppc_get_pc(struct kvm_vcpu *vcpu)
        return vcpu->arch.pc;
 }
 
+static inline bool kvmppc_need_byteswap(struct kvm_vcpu *vcpu)
+{
+       return (vcpu->arch.shared->msr & MSR_LE) != (MSR_KERNEL & MSR_LE);
+}
+
 static inline u32 kvmppc_get_last_inst_internal(struct kvm_vcpu *vcpu, ulong pc)
 {
        /* Load the instruction manually if it failed to do so in the
@@ -271,7 +276,8 @@ static inline u32 kvmppc_get_last_inst_internal(struct kvm_vcpu *vcpu, ulong pc)
        if (vcpu->arch.last_inst == KVM_INST_FETCH_FAILED)
                kvmppc_ld(vcpu, &pc, sizeof(u32), &vcpu->arch.last_inst, false);
 
-       return vcpu->arch.last_inst;
+       return kvmppc_need_byteswap(vcpu) ? swab32(vcpu->arch.last_inst) :
+               vcpu->arch.last_inst;
 }
 
 static inline u32 kvmppc_get_last_inst(struct kvm_vcpu *vcpu)
index dd8f61510dfd01151b5d7035dd8cee7c30ac45a7..80d46b5a7efb49cc6de436e45da6b0b5e690d9cf 100644 (file)
@@ -63,6 +63,12 @@ static inline u32 kvmppc_get_xer(struct kvm_vcpu *vcpu)
        return vcpu->arch.xer;
 }
 
+static inline bool kvmppc_need_byteswap(struct kvm_vcpu *vcpu)
+{
+       /* XXX Would need to check TLB entry */
+       return false;
+}
+
 static inline u32 kvmppc_get_last_inst(struct kvm_vcpu *vcpu)
 {
        return vcpu->arch.last_inst;
index c8317fbf92c470f03842da5cf363efd97aed99b4..629277df47986d13b427f46519505b2e230a0cf7 100644 (file)
@@ -54,12 +54,13 @@ extern void kvmppc_handler_highmem(void);
 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);
+                             int is_default_endian);
 extern int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
                                unsigned int rt, unsigned int bytes,
-                               int is_bigendian);
+                              int is_default_endian);
 extern int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
-                               u64 val, unsigned int bytes, int is_bigendian);
+                              u64 val, unsigned int bytes,
+                              int is_default_endian);
 
 extern int kvmppc_emulate_instruction(struct kvm_run *run,
                                       struct kvm_vcpu *vcpu);
index f3ff587a8b7d58e064e6364606e98a0e6473f106..efb8aa544876f05cfbbb6e0f7a86b06931f88dca 100644 (file)
@@ -558,7 +558,7 @@ static int kvmppc_hv_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu,
         * we just return and retry the instruction.
         */
 
-       if (instruction_is_store(vcpu->arch.last_inst) != !!is_store)
+       if (instruction_is_store(kvmppc_get_last_inst(vcpu)) != !!is_store)
                return RESUME_GUEST;
 
        /*
index 2f9a0873b44fe21d106988004d2c48ae40ce210e..c2b887be2c2922bebf772cedbba0ab14cc74c52f 100644 (file)
@@ -219,7 +219,6 @@ static int kvmppc_emulate_mfspr(struct kvm_vcpu *vcpu, 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 particular core
  * from opcode tables in the future. */
index 7ca9e0a8049965f6ecd4c6d1ea849525ab24ae4c..026dfaaa4772acefa276e8569228fc1a55c620e8 100644 (file)
@@ -673,9 +673,19 @@ static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
 }
 
 int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
-                       unsigned int rt, unsigned int bytes, int is_bigendian)
+                      unsigned int rt, unsigned int bytes,
+                      int is_default_endian)
 {
        int idx, ret;
+       int is_bigendian;
+
+       if (kvmppc_need_byteswap(vcpu)) {
+               /* Default endianness is "little endian". */
+               is_bigendian = !is_default_endian;
+       } else {
+               /* Default endianness is "big endian". */
+               is_bigendian = is_default_endian;
+       }
 
        if (bytes > sizeof(run->mmio.data)) {
                printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
@@ -711,21 +721,31 @@ EXPORT_SYMBOL_GPL(kvmppc_handle_load);
 
 /* 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)
+                       unsigned int rt, unsigned int bytes,
+                       int is_default_endian)
 {
        int r;
 
        vcpu->arch.mmio_sign_extend = 1;
-       r = kvmppc_handle_load(run, vcpu, rt, bytes, is_bigendian);
+       r = kvmppc_handle_load(run, vcpu, rt, bytes, is_default_endian);
 
        return r;
 }
 
 int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
-                        u64 val, unsigned int bytes, int is_bigendian)
+                       u64 val, unsigned int bytes, int is_default_endian)
 {
        void *data = run->mmio.data;
        int idx, ret;
+       int is_bigendian;
+
+       if (kvmppc_need_byteswap(vcpu)) {
+               /* Default endianness is "little endian". */
+               is_bigendian = !is_default_endian;
+       } else {
+               /* Default endianness is "big endian". */
+               is_bigendian = is_default_endian;
+       }
 
        if (bytes > sizeof(run->mmio.data)) {
                printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,