Skip to contentSkip to conversation

KVM: PPC: Virtualize Gekko guests v2

PATCH Applied Alexander Graf 3 participants 23 messages Feb 19, 2010 – Feb 21, 2010 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover KVM: PPC: Virtualize Gekko guests v2

In an effort to get KVM on PPC more useful for other userspace users than Qemu, I figured it'd be a nice idea to implement virtualization of the Gekko CPU.

The Gekko is the CPU used in the GameCube. In a slightly more modern fashion it lives on in the Wii today.

Using this patch set and a modified version of Dolphin, I was able to virtualize simple GameCube demos on a 970MP system.

As always, while getting this to run I stumbled across several broken parts and fixed them as they came up. So expect some bug fixes in this patch set too.

v1 -> v2:

- rename REG_ constants to KVM_REG_
- make mmio sign extension more readable
- replace binary patching emulator with function calls
- add capability bit
- make fpscr 64 bit wide
- add secondary htab patch
- make fpu work on ps3 again

Alexander Graf (21):
KVM: PPC: Add QPR registers
KVM: PPC: Make fpscr 64-bit
KVM: PPC: Enable MMIO to do 64 bits, fprs and qprs
KVM: PPC: Teach MMIO Signedness
KVM: PPC: Add AGAIN type for emulation return
KVM: PPC: Add hidden flag for paired singles
KVM: PPC: Add Gekko SPRs
KVM: PPC: Combine extension interrupt handlers
KVM: PPC: Preload FPU when possible
KVM: PPC: Fix typo in book3s_32 debug code
KVM: PPC: Implement mtsr instruction emulation
KVM: PPC: Make software load/store return eaddr
KVM: PPC: Make ext giveup non-static
KVM: PPC: Add helpers to call FPU instructions
KVM: PPC: Fix error in BAT assignment
KVM: PPC: Add helpers to modify ppc fields
KVM: PPC: Enable program interrupt to do MMIO
KVM: PPC: Implement Paired Single emulation
KVM: PPC: Add capability for paired singles
KVM: PPC: Enable use of secondary htab bucket
KVM: PPC: Simplify kvmppc_load_up_(FPU|VMX|VSX)

 arch/powerpc/include/asm/kvm.h           |    7 +
 arch/powerpc/include/asm/kvm_asm.h       |    1 +
 arch/powerpc/include/asm/kvm_book3s.h    |    7 +-
 arch/powerpc/include/asm/kvm_fpu.h       |   85 ++
 arch/powerpc/include/asm/kvm_host.h      |    8 +-
 arch/powerpc/include/asm/kvm_ppc.h       |   39 +-
 arch/powerpc/include/asm/reg.h           |   10 +
 arch/powerpc/kernel/ppc_ksyms.c          |    2 +
 arch/powerpc/kvm/Makefile                |    2 +
 arch/powerpc/kvm/book3s.c                |  118 +++-
 arch/powerpc/kvm/book3s_32_mmu.c         |    2 +-
 arch/powerpc/kvm/book3s_64_emulate.c     |   94 ++-
 arch/powerpc/kvm/book3s_64_mmu_host.c    |    9 +-
 arch/powerpc/kvm/book3s_64_rmhandlers.S  |   13 +-
 arch/powerpc/kvm/book3s_paired_singles.c | 1289 ++++++++++++++++++++++++++++++
 arch/powerpc/kvm/emulate.c               |   18 +-
 arch/powerpc/kvm/fpu.S                   |  274 +++++++
 arch/powerpc/kvm/powerpc.c               |   54 ++-
 include/linux/kvm.h                      |    1 +
 19 files changed, 1976 insertions(+), 57 deletions(-)
 create mode 100644 arch/powerpc/include/asm/kvm_fpu.h
 create mode 100644 arch/powerpc/kvm/book3s_paired_singles.c
 create mode 100644 arch/powerpc/kvm/fpu.S

A message that is not in the archive (sent off-list or not yet received): 1266573647-30153-1-git-send-email-agraf-l3A5Bk7waGM@public.gmane.org

#2 lore raw

PATCH 1/21 KVM: PPC: Add QPR registers

The Gekko has GPRs, SPRs and FPRs like normal PowerPC codes, but it also has QPRs which are basically single precision only FPU registers that get used when in paired single mode.

The following patches depend on them being around, so let's add the definitions early.

---
 arch/powerpc/include/asm/kvm_host.h |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)
arch/powerpc/include/asm/kvm_host.h +5 -0
index 5e5bae7..4b14dcd 100644
@@ -186,6 +186,11 @@ struct kvm_vcpu_arch {
 	u64 vsr[32];
 #endif
 
+#ifdef CONFIG_PPC_BOOK3S
+	/* For Gekko paired singles */
+	u32 qpr[32];
+#endif
+
 	ulong pc;
 	ulong ctr;
 	ulong lr;
Signature · 1 line
1.6.0.2
#3 lore raw

PATCH 2/21 KVM: PPC: Make fpscr 64-bit

In reply to a message not in the archive

Modern PowerPCs have a 64 bit wide FPSCR register. Let's accomodate for that and make it 64 bits in our vcpu struct too.

---
 arch/powerpc/include/asm/kvm_host.h |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
arch/powerpc/include/asm/kvm_host.h +1 -1
index 4b14dcd..fb87dcf 100644
@@ -175,7 +175,7 @@ struct kvm_vcpu_arch {
 	ulong gpr[32];
 
 	u64 fpr[32];
-	u32 fpscr;
+	u64 fpscr;
 
 #ifdef CONFIG_ALTIVEC
 	vector128 vr[32];
Signature · 1 line
1.6.0.2
#4 lore raw

PATCH 3/21 KVM: PPC: Enable MMIO to do 64 bits, fprs and qprs

In reply to a message not in the archive

Right now MMIO access can only happen for GPRs and is at most 32 bit wide. That's actually enough for almost all types of hardware out there.

Unfortunately, the guest I was using used FPU writes to MMIO regions, so it ended up writing 64 bit MMIOs using FPRs and QPRs.

So let's add code to handle those odd cases too.

---

v1 -> v2:

- rename REG_ to KVM_REG_

---
 arch/powerpc/include/asm/kvm.h     |    7 +++++++
 arch/powerpc/include/asm/kvm_ppc.h |    2 +-
 arch/powerpc/kvm/powerpc.c         |   24 ++++++++++++++++++++++--
 3 files changed, 30 insertions(+), 3 deletions(-)
arch/powerpc/include/asm/kvm.h +7 -0
index 81f3b0b..19bae31 100644
@@ -77,4 +77,11 @@ struct kvm_debug_exit_arch {
 struct kvm_guest_debug_arch {
 };
 
+#define KVM_REG_MASK		0x001f
+#define KVM_REG_EXT_MASK	0xffe0
+#define KVM_REG_GPR		0x0000
+#define KVM_REG_FPR		0x0020
+#define KVM_REG_QPR		0x0040
+#define KVM_REG_FQPR		0x0060
+
 #endif /* __LINUX_KVM_POWERPC_H */
arch/powerpc/include/asm/kvm_ppc.h +1 -1
index e264282..c011170 100644
@@ -49,7 +49,7 @@ 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_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
-                               u32 val, unsigned int bytes, int is_bigendian);
+                               u64 val, unsigned int bytes, int is_bigendian);
 
 extern int kvmppc_emulate_instruction(struct kvm_run *run,
                                       struct kvm_vcpu *vcpu);
arch/powerpc/kvm/powerpc.c +22 -2
index 51aedd7..14b431e 100644
@@ -277,7 +277,7 @@ static void kvmppc_complete_dcr_load(struct kvm_vcpu *vcpu,
 static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
                                       struct kvm_run *run)
 {
-	ulong gpr;
+	u64 gpr;
 
 	if (run->mmio.len > sizeof(gpr)) {
 		printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
@@ -286,6 +286,7 @@ static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
 
 	if (vcpu->arch.mmio_is_bigendian) {
 		switch (run->mmio.len) {
+		case 8: gpr = *(u64 *)run->mmio.data; break;
 		case 4: gpr = *(u32 *)run->mmio.data; break;
 		case 2: gpr = *(u16 *)run->mmio.data; break;
 		case 1: gpr = *(u8 *)run->mmio.data; break;
@@ -300,6 +301,24 @@ static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
 	}
 
 	kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
+
+	switch (vcpu->arch.io_gpr & KVM_REG_EXT_MASK) {
+	case KVM_REG_GPR:
+		kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
+		break;
+	case KVM_REG_FPR:
+		vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr;
+		break;
+	case KVM_REG_QPR:
+		vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr;
+		break;
+	case KVM_REG_FQPR:
+		vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr;
+		vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr;
+		break;
+	default:
+		BUG();
+	}
 }
 
 int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
@@ -323,7 +342,7 @@ int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
 }
 
 int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
-                        u32 val, unsigned int bytes, int is_bigendian)
+                        u64 val, unsigned int bytes, int is_bigendian)
 {
 	void *data = run->mmio.data;
 
@@ -341,6 +360,7 @@ int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
 	/* Store the value at the lowest bytes in 'data'. */
 	if (is_bigendian) {
 		switch (bytes) {
+		case 8: *(u64 *)data = val; break;
 		case 4: *(u32 *)data = val; break;
 		case 2: *(u16 *)data = val; break;
 		case 1: *(u8  *)data = val; break;
Signature · 1 line
1.6.0.2
#5 lore raw

PATCH 5/21 KVM: PPC: Add AGAIN type for emulation return

In reply to a message not in the archive

Emulation of an instruction can have different outcomes. It can succeed, fail, require MMIO, do funky BookE stuff - or it can just realize something's odd and will be fixed the next time around.

Exactly that is what EMULATE_AGAIN means. Using that flag we can now tell the caller that nothing happened, but we still want to go back to the guest and see what happens next time we come around.

---
 arch/powerpc/include/asm/kvm_ppc.h |    1 +
 arch/powerpc/kvm/book3s.c          |    3 +++
 arch/powerpc/kvm/emulate.c         |    4 +++-
 3 files changed, 7 insertions(+), 1 deletions(-)
arch/powerpc/include/asm/kvm_ppc.h +1 -0
index a288dd2..0761218 100644
@@ -37,6 +37,7 @@ enum emulation_result {
 	EMULATE_DO_MMIO,      /* kvm_run filled with MMIO request */
 	EMULATE_DO_DCR,       /* kvm_run filled with DCR request */
 	EMULATE_FAIL,         /* can't emulate this instruction */
+	EMULATE_AGAIN,        /* something went wrong. go again */
 };
 
 extern int __kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
arch/powerpc/kvm/book3s.c +3 -0
index 9a271f0..1e5e0fc 100644
@@ -788,6 +788,9 @@ int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
 		case EMULATE_DONE:
 			r = RESUME_GUEST_NV;
 			break;
+		case EMULATE_AGAIN:
+			r = RESUME_GUEST;
+			break;
 		case EMULATE_FAIL:
 			printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
 			       __func__, vcpu->arch.pc, vcpu->arch.last_inst);
arch/powerpc/kvm/emulate.c +3 -1
index 11789dd..2410ec2 100644
@@ -486,7 +486,9 @@ int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
 
 	if (emulated == EMULATE_FAIL) {
 		emulated = kvmppc_core_emulate_op(run, vcpu, inst, &advance);
-		if (emulated == EMULATE_FAIL) {
+		if (emulated == EMULATE_AGAIN) {
+			advance = 0;
+		} else if (emulated == EMULATE_FAIL) {
 			advance = 0;
 			printk(KERN_ERR "Couldn't emulate instruction 0x%08x "
 			       "(op %d xop %d)\n", inst, get_op(inst), get_xop(inst));
Signature · 1 line
1.6.0.2
#6 lore raw

PATCH 6/21 KVM: PPC: Add hidden flag for paired singles

In reply to a message not in the archive

The Gekko implements an extension called paired singles. When the guest wants to use that extension, we need to make sure we're not running the host FPU, because all FPU instructions need to get emulated to accomodate for additional operations that occur.

This patch adds an hflag to track if we're in paired single mode or not.

---
 arch/powerpc/include/asm/kvm_asm.h |    1 +
 arch/powerpc/kvm/book3s.c          |    4 ++++
 2 files changed, 5 insertions(+), 0 deletions(-)
arch/powerpc/include/asm/kvm_asm.h +1 -0
index aadf2dd..7238c04 100644
@@ -88,6 +88,7 @@
 
 #define BOOK3S_HFLAG_DCBZ32			0x1
 #define BOOK3S_HFLAG_SLB			0x2
+#define BOOK3S_HFLAG_PAIRED_SINGLE		0x4
 
 #define RESUME_FLAG_NV          (1<<0)  /* Reload guest nonvolatile state? */
 #define RESUME_FLAG_HOST        (1<<1)  /* Resume host? */
arch/powerpc/kvm/book3s.c +4 -0
index 1e5e0fc..96f7be4 100644
@@ -638,6 +638,10 @@ static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
 	u64 *thread_fpr = (u64*)t->fpr;
 	int i;
 
+	/* When we have paired singles, we emulate in software */
+	if (vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE)
+		return RESUME_GUEST;
+
 	if (!(vcpu->arch.msr & msr)) {
 		kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
 		return RESUME_GUEST;
Signature · 1 line
1.6.0.2
#7 lore raw

PATCH 7/21 KVM: PPC: Add Gekko SPRs

In reply to a message not in the archive

The Gekko has some SPR values that differ from other PPC core values and also some additional ones.

Let's add support for them in our mfspr/mtspr emulator.

---
 arch/powerpc/include/asm/kvm_book3s.h |    1 +
 arch/powerpc/include/asm/reg.h        |   10 +++++
 arch/powerpc/kvm/book3s_64_emulate.c  |   70 +++++++++++++++++++++++++++++++++
 3 files changed, 81 insertions(+), 0 deletions(-)
arch/powerpc/include/asm/kvm_book3s.h +1 -0
index db7db0a..d28ee83 100644
@@ -82,6 +82,7 @@ struct kvmppc_vcpu_book3s {
 	struct kvmppc_bat ibat[8];
 	struct kvmppc_bat dbat[8];
 	u64 hid[6];
+	u64 gqr[8];
 	int slb_nr;
 	u64 sdr1;
 	u64 dsisr;
arch/powerpc/include/asm/reg.h +10 -0
index 5572e86..8a69a39 100644
@@ -293,10 +293,12 @@
 #define HID1_ABE	(1<<10)		/* 7450 Address Broadcast Enable */
 #define HID1_PS		(1<<16)		/* 750FX PLL selection */
 #define SPRN_HID2	0x3F8		/* Hardware Implementation Register 2 */
+#define SPRN_HID2_GEKKO	0x398		/* Gekko HID2 Register */
 #define SPRN_IABR	0x3F2	/* Instruction Address Breakpoint Register */
 #define SPRN_IABR2	0x3FA		/* 83xx */
 #define SPRN_IBCR	0x135		/* 83xx Insn Breakpoint Control Reg */
 #define SPRN_HID4	0x3F4		/* 970 HID4 */
+#define SPRN_HID4_GEKKO	0x3F3		/* Gekko HID4 */
 #define SPRN_HID5	0x3F6		/* 970 HID5 */
 #define SPRN_HID6	0x3F9	/* BE HID 6 */
 #define   HID6_LB	(0x0F<<12) /* Concurrent Large Page Modes */
@@ -465,6 +467,14 @@
 #define SPRN_VRSAVE	0x100	/* Vector Register Save Register */
 #define SPRN_XER	0x001	/* Fixed Point Exception Register */
 
+#define SPRN_MMCR0_GEKKO 0x3B8 /* Gekko Monitor Mode Control Register 0 */
+#define SPRN_MMCR1_GEKKO 0x3BC /* Gekko Monitor Mode Control Register 1 */
+#define SPRN_PMC1_GEKKO  0x3B9 /* Gekko Performance Monitor Control 1 */
+#define SPRN_PMC2_GEKKO  0x3BA /* Gekko Performance Monitor Control 2 */
+#define SPRN_PMC3_GEKKO  0x3BD /* Gekko Performance Monitor Control 3 */
+#define SPRN_PMC4_GEKKO  0x3BE /* Gekko Performance Monitor Control 4 */
+#define SPRN_WPAR_GEKKO  0x399 /* Gekko Write Pipe Address Register */
+
 #define SPRN_SCOMC	0x114	/* SCOM Access Control */
 #define SPRN_SCOMD	0x115	/* SCOM Access DATA */
 
arch/powerpc/kvm/book3s_64_emulate.c +70 -0

114 lines not on this page · raw message

Signature · 1 line
1.6.0.2
#8 lore raw

PATCH 10/21 KVM: PPC: Fix typo in book3s_32 debug code

In reply to a message not in the archive

There's a typo in the debug ifdef of the book3s_32 mmu emulation. While trying to debug something I stumbled across that and wanted to save anyone after me (or myself later) from having to debug that again.

So let's fix the ifdef.

---
 arch/powerpc/kvm/book3s_32_mmu.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
arch/powerpc/kvm/book3s_32_mmu.c +1 -1
index faf99f2..1483a9b 100644
@@ -37,7 +37,7 @@
 #define dprintk(X...) do { } while(0)
 #endif
 
-#ifdef DEBUG_PTE
+#ifdef DEBUG_MMU_PTE
 #define dprintk_pte(X...) printk(KERN_INFO X)
 #else
 #define dprintk_pte(X...) do { } while(0)
Signature · 1 line
1.6.0.2
#9 lore raw

PATCH 13/21 KVM: PPC: Make ext giveup non-static

In reply to a message not in the archive

We need to call the ext giveup handlers from code outside of book3s.c. So let's make it non-static.

---
 arch/powerpc/include/asm/kvm_book3s.h |    1 +
 arch/powerpc/kvm/book3s.c             |    3 +--
 2 files changed, 2 insertions(+), 2 deletions(-)
arch/powerpc/include/asm/kvm_book3s.h +1 -0
index 8463976..fd43210 100644
@@ -120,6 +120,7 @@ extern int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, b
 extern void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec);
 extern void kvmppc_set_bat(struct kvm_vcpu *vcpu, struct kvmppc_bat *bat,
 			   bool upper, u32 val);
+extern void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr);
 
 extern u32 kvmppc_trampoline_lowmem;
 extern u32 kvmppc_trampoline_enter;
arch/powerpc/kvm/book3s.c +1 -2
index e8dccc6..99e9e07 100644
@@ -35,7 +35,6 @@
 /* #define EXIT_DEBUG_SIMPLE */
 /* #define DEBUG_EXT */
 
-static void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr);
 static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
 			     ulong msr);
 
@@ -597,7 +596,7 @@ static inline int get_fpr_index(int i)
 }
 
 /* Give up external provider (FPU, Altivec, VSX) */
-static void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr)
+void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr)
 {
 	struct thread_struct *t = &current->thread;
 	u64 *vcpu_fpr = vcpu->arch.fpr;
Signature · 1 line
1.6.0.2
#10 lore raw

PATCH 14/21 KVM: PPC: Add helpers to call FPU instructions

In reply to a message not in the archive

To emulate paired single instructions, we need to be able to call FPU operations from within the kernel. Since we don't want gcc to spill arbitrary FPU code everywhere, we tell it to use a soft fpu.

Since we know we can really call the FPU in safe areas, let's also add some calls that we can later use to actually execute real world FPU operations on the host's FPU.

---

v1 -> v2:
- add double functions

---
 arch/powerpc/include/asm/kvm_fpu.h |   85 +++++++++++
 arch/powerpc/kernel/ppc_ksyms.c    |    2 +
 arch/powerpc/kvm/Makefile          |    1 +
 arch/powerpc/kvm/fpu.S             |  274 ++++++++++++++++++++++++++++++++++++
 4 files changed, 362 insertions(+), 0 deletions(-)
 create mode 100644 arch/powerpc/include/asm/kvm_fpu.h
 create mode 100644 arch/powerpc/kvm/fpu.S
arch/powerpc/include/asm/kvm_fpu.h new file +85 -0
new file mode 100644
index 0000000..94f05de
@@ -0,0 +1,85 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License, version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ * Copyright Novell Inc. 2010
+ *
+ * Authors: Alexander Graf <[email protected]>
+ */
+
+#ifndef __ASM_KVM_FPU_H__
+#define __ASM_KVM_FPU_H__
+
+#include <linux/types.h>
+
+extern void fps_fres(struct thread_struct *t, u32 *dst, u32 *src1);
+extern void fps_frsqrte(struct thread_struct *t, u32 *dst, u32 *src1);
+extern void fps_fsqrts(struct thread_struct *t, u32 *dst, u32 *src1);
+
+extern void fps_fadds(struct thread_struct *t, u32 *dst, u32 *src1, u32 *src2);
+extern void fps_fdivs(struct thread_struct *t, u32 *dst, u32 *src1, u32 *src2);
+extern void fps_fmuls(struct thread_struct *t, u32 *dst, u32 *src1, u32 *src2);
+extern void fps_fsubs(struct thread_struct *t, u32 *dst, u32 *src1, u32 *src2);
+
+extern void fps_fmadds(struct thread_struct *t, u32 *dst, u32 *src1, u32 *src2,
+		       u32 *src3);
+extern void fps_fmsubs(struct thread_struct *t, u32 *dst, u32 *src1, u32 *src2,
+		       u32 *src3);
+extern void fps_fnmadds(struct thread_struct *t, u32 *dst, u32 *src1, u32 *src2,
+		        u32 *src3);
+extern void fps_fnmsubs(struct thread_struct *t, u32 *dst, u32 *src1, u32 *src2,
+		        u32 *src3);
+extern void fps_fsel(struct thread_struct *t, u32 *dst, u32 *src1, u32 *src2,
+		     u32 *src3);
+
+#define FPD_ONE_IN(name) extern void fpd_ ## name(u64 *fpscr, u32 *cr, \
+				u64 *dst, u64 *src1);
+#define FPD_TWO_IN(name) extern void fpd_ ## name(u64 *fpscr, u32 *cr, \
+				u64 *dst, u64 *src1, u64 *src2);
+#define FPD_THREE_IN(name) extern void fpd_ ## name(u64 *fpscr, u32 *cr, \
+				u64 *dst, u64 *src1, u64 *src2, u64 *src3);
+
+extern void fpd_fcmpu(u64 *fpscr, u32 *cr, u64 *src1, u64 *src2);
+extern void fpd_fcmpo(u64 *fpscr, u32 *cr, u64 *src1, u64 *src2);
+
+FPD_ONE_IN(fsqrts)
+FPD_ONE_IN(frsqrtes)
+FPD_ONE_IN(fres)
+FPD_ONE_IN(frsp)
+FPD_ONE_IN(fctiw)
+FPD_ONE_IN(fctiwz)
+FPD_ONE_IN(fsqrt)
+FPD_ONE_IN(fre)
+FPD_ONE_IN(frsqrte)
+FPD_ONE_IN(fneg)
+FPD_ONE_IN(fabs)
+FPD_TWO_IN(fadds)
+FPD_TWO_IN(fsubs)
+FPD_TWO_IN(fdivs)
+FPD_TWO_IN(fmuls)
+FPD_TWO_IN(fcpsgn)
+FPD_TWO_IN(fdiv)
+FPD_TWO_IN(fadd)
+FPD_TWO_IN(fmul)
+FPD_TWO_IN(fsub)
+FPD_THREE_IN(fmsubs)
+FPD_THREE_IN(fmadds)
+FPD_THREE_IN(fnmsubs)
+FPD_THREE_IN(fnmadds)
+FPD_THREE_IN(fsel)
+FPD_THREE_IN(fmsub)
+FPD_THREE_IN(fmadd)
+FPD_THREE_IN(fnmsub)
+FPD_THREE_IN(fnmadd)
+
+#endif
arch/powerpc/kernel/ppc_ksyms.c +2 -0
index ab3e392..58fdb3a 100644
@@ -101,6 +101,8 @@ EXPORT_SYMBOL(pci_dram_offset);
 EXPORT_SYMBOL(start_thread);
 EXPORT_SYMBOL(kernel_thread);
 
+EXPORT_SYMBOL_GPL(cvt_df);
+EXPORT_SYMBOL_GPL(cvt_fd);
 EXPORT_SYMBOL(giveup_fpu);
 #ifdef CONFIG_ALTIVEC
 EXPORT_SYMBOL(giveup_altivec);
arch/powerpc/kvm/Makefile +1 -0
index 56484d6..e575cfd 100644
@@ -40,6 +40,7 @@ kvm-objs-$(CONFIG_KVM_E500) := $(kvm-e500-objs)
 
 kvm-book3s_64-objs := \
 	$(common-objs-y) \
+	fpu.o \
 	book3s.o \
 	book3s_64_emulate.o \
 	book3s_64_interrupts.o \
arch/powerpc/kvm/fpu.S new file +274 -0

274 lines not on this page · raw message

Signature · 1 line
1.6.0.2
#11 lore raw

PATCH 16/21 KVM: PPC: Add helpers to modify ppc fields

In reply to a message not in the archive

The PowerPC specification always lists bits from MSB to LSB. That is really confusing when you're trying to write C code, because it fits in pretty badly with the normal (1 << xx) schemes.

So I came up with some nice wrappers that allow to get and set fields in a u64 with bit numbers exactly as given in the spec. That makes the code in KVM and the spec easier comparable.

---
 arch/powerpc/include/asm/kvm_ppc.h |   33 +++++++++++++++++++++++++++++++++
 1 files changed, 33 insertions(+), 0 deletions(-)
arch/powerpc/include/asm/kvm_ppc.h +33 -0
index 0761218..c7fcdd7 100644
@@ -103,6 +103,39 @@ extern void kvmppc_booke_exit(void);
 
 extern void kvmppc_core_destroy_mmu(struct kvm_vcpu *vcpu);
 
+/*
+ * Cuts out inst bits with ordering according to spec.
+ * That means the leftmost bit is zero. All given bits are included.
+ */
+static inline u32 kvmppc_get_field(u64 inst, int msb, int lsb)
+{
+	u32 r;
+	u32 mask;
+
+	BUG_ON(msb > lsb);
+
+	mask = (1 << (lsb - msb + 1)) - 1;
+	r = (inst >> (63 - lsb)) & mask;
+
+	return r;
+}
+
+/*
+ * Replaces inst bits with ordering according to spec.
+ */
+static inline u32 kvmppc_set_field(u64 inst, int msb, int lsb, int value)
+{
+	u32 r;
+	u32 mask;
+
+	BUG_ON(msb > lsb);
+
+	mask = ((1 << (lsb - msb + 1)) - 1) << (63 - lsb);
+	r = (inst & ~mask) | ((value << (63 - lsb)) & mask);
+
+	return r;
+}
+
 #ifdef CONFIG_PPC_BOOK3S
 
 /* We assume we're always acting on the current vcpu */
Signature · 1 line
1.6.0.2
#12 lore raw

PATCH 17/21 KVM: PPC: Enable program interrupt to do MMIO

In reply to a message not in the archive

When we get a program interrupt we usually don't expect it to perform an MMIO operation. But why not? When we emulate paired singles, we can end up loading or storing to an MMIO address - and the handling of those happens in the program interrupt handler.

So let's teach the program interrupt handler how to deal with EMULATE_MMIO.

---
 arch/powerpc/kvm/book3s.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
arch/powerpc/kvm/book3s.c +4 -0
index 99e9e07..f842d1d 100644
@@ -840,6 +840,10 @@ program_interrupt:
 			kvmppc_core_queue_program(vcpu, flags);
 			r = RESUME_GUEST;
 			break;
+		case EMULATE_DO_MMIO:
+			run->exit_reason = KVM_EXIT_MMIO;
+			r = RESUME_HOST_NV;
+			break;
 		default:
 			BUG();
 		}
Signature · 1 line
1.6.0.2
#13 lore raw

PATCH 18/21 KVM: PPC: Implement Paired Single emulation

In reply to a message not in the archive

The one big thing about the Gekko is paired singles.

Paired singles are an extension to the instruction set, that adds 32 single precision floating point registers (qprs), some SPRs to modify the behavior of paired singled operations and instructions to deal with qprs to the instruction set.

Unfortunately, it also changes semantics of existing operations that affect single values in FPRs. In most cases they get mirrored to the coresponding QPR.

Thanks to that we need to emulate all FPU operations and all the new paired single operations too.

In order to achieve that, we use the just introduced FPU call helpers to call the real FPU whenever the guest wants to modify an FPR. Additionally we also fix up the QPR values along the way.

That way we can execute paired single FPU operations without implementing a soft fpu.

---

v1 -> v2:
- make all calls explicit, thus removing binary patching
- use function calls for paired single emulation
- use KVM_ constants

---
 arch/powerpc/include/asm/kvm_book3s.h    |    1 +
 arch/powerpc/kvm/Makefile                |    1 +
 arch/powerpc/kvm/book3s_64_emulate.c     |    3 +
 arch/powerpc/kvm/book3s_paired_singles.c | 1289 ++++++++++++++++++++++++++++++
 4 files changed, 1294 insertions(+), 0 deletions(-)
 create mode 100644 arch/powerpc/kvm/book3s_paired_singles.c
arch/powerpc/include/asm/kvm_book3s.h +1 -0
index fd43210..e6ea974 100644
@@ -121,6 +121,7 @@ extern void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec)
 extern void kvmppc_set_bat(struct kvm_vcpu *vcpu, struct kvmppc_bat *bat,
 			   bool upper, u32 val);
 extern void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr);
+extern int kvmppc_emulate_paired_single(struct kvm_run *run, struct kvm_vcpu *vcpu);
 
 extern u32 kvmppc_trampoline_lowmem;
 extern u32 kvmppc_trampoline_enter;
arch/powerpc/kvm/Makefile +1 -0
index e575cfd..eba721e 100644
@@ -41,6 +41,7 @@ kvm-objs-$(CONFIG_KVM_E500) := $(kvm-e500-objs)
 kvm-book3s_64-objs := \
 	$(common-objs-y) \
 	fpu.o \
+	book3s_paired_singles.o \
 	book3s.o \
 	book3s_64_emulate.o \
 	book3s_64_interrupts.o \
arch/powerpc/kvm/book3s_64_emulate.c +3 -0
index 1d1b952..c989214 100644
@@ -200,6 +200,9 @@ int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
 		emulated = EMULATE_FAIL;
 	}
 
+	if (emulated == EMULATE_FAIL)
+		emulated = kvmppc_emulate_paired_single(run, vcpu);
+
 	return emulated;
 }
 
arch/powerpc/kvm/book3s_paired_singles.c new file +1289 -0

1,289 lines not on this page · raw message

Signature · 1 line
1.6.0.2
#14 lore raw

PATCH 20/21 KVM: PPC: Enable use of secondary htab bucket

In reply to a message not in the archive

We had code to make use of the secondary htab buckets, but kept that disabled because it was unstable when I put it in.

I checked again if that's still the case and apparently it was only exposing some instability that was there anyways before. I haven't seen any badness related to usage of secondary htab entries so far.

This should speed up guest memory allocations by quite a bit, because we now have more space to put PTEs in.

---
 arch/powerpc/kvm/book3s_64_mmu_host.c |    9 +--------
 1 files changed, 1 insertions(+), 8 deletions(-)
arch/powerpc/kvm/book3s_64_mmu_host.c +1 -8
index f2899b2..25bd4ed 100644
@@ -257,16 +257,9 @@ map_again:
 
 	if (ret < 0) {
 		/* If we couldn't map a primary PTE, try a secondary */
-#ifdef USE_SECONDARY
 		hash = ~hash;
+		vflags ^= HPTE_V_SECONDARY;
 		attempt++;
-		if (attempt % 2)
-			vflags = HPTE_V_SECONDARY;
-		else
-			vflags = 0;
-#else
-		attempt = 2;
-#endif
 		goto map_again;
 	} else {
 		int hpte_id = kvmppc_mmu_hpte_cache_next(vcpu);
Signature · 1 line
1.6.0.2
Applied
#15 lore raw
In reply to a message not in the archive
Quoted 7 lines · Alexander Graf

In an effort to get KVM on PPC more useful for other userspace users than Qemu, I figured it'd be a nice idea to implement virtualization of the Gekko CPU.

The Gekko is the CPU used in the GameCube. In a slightly more modern fashion it lives on in the Wii today.

Quoted 7 lines

Using this patch set and a modified version of Dolphin, I was able to virtualize simple GameCube demos on a 970MP system.

As always, while getting this to run I stumbled across several broken parts and fixed them as they came up. So expect some bug fixes in this patch set too.

Applied all, thanks.

Signature · 1 line
error compiling committee.c: too many arguments to function
#16 lore raw

PATCH 4/21 KVM: PPC: Teach MMIO Signedness

In reply to Alexander Graf

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.

---

v1 -> v2:

- make mmio sign extension more readable using casts

---
 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          |   29 +++++++++++++++++++++++++++++
 4 files changed, 47 insertions(+), 0 deletions(-)
arch/powerpc/include/asm/kvm_host.h +1 -0
index fb87dcf..119deb4 100644
@@ -270,6 +270,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;
 
arch/powerpc/include/asm/kvm_ppc.h +3 -0
index c011170..a288dd2 100644
@@ -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);
 
arch/powerpc/kvm/emulate.c +14 -0
index cb72a65..11789dd 100644
@@ -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,
arch/powerpc/kvm/powerpc.c +29 -0
index 14b431e..a1f9626 100644
@@ -300,6 +300,22 @@ 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:
+			gpr = (s64)(s32)gpr;
+			break;
+#endif
+		case 2:
+			gpr = (s64)(s16)gpr;
+			break;
+		case 1:
+			gpr = (s64)(s8)gpr;
+			break;
+		}
+	}
+
 	kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
 
 	switch (vcpu->arch.io_gpr & KVM_REG_EXT_MASK) {
@@ -337,10 +353,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)
 {
Signature · 1 line
1.6.0.2
#17 lore raw

PATCH 8/21 KVM: PPC: Combine extension interrupt handlers

In reply to Alexander Graf

When we for example get an Altivec interrupt, but our guest doesn't support altivec, we need to inject a program interrupt, not an altivec interrupt.

The same goes for paired singles. When an altivec interrupt arrives, we're pretty sure we need to emulate the instruction because it's a paired single operation.

So let's make all the ext handlers aware that they need to jump to the program interrupt handler when an extension interrupt arrives that was not supposed to arrive for the guest CPU.

---
 arch/powerpc/kvm/book3s.c |   55 ++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 50 insertions(+), 5 deletions(-)
arch/powerpc/kvm/book3s.c +50 -5
index 96f7be4..6bdf7f2 100644
@@ -36,6 +36,8 @@
 /* #define DEBUG_EXT */
 
 static void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr);
+static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
+			     ulong msr);
 
 struct kvm_stats_debugfs_item debugfs_entries[] = {
 	{ "exits",       VCPU_STAT(sum_exits) },
@@ -628,6 +630,30 @@ static void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr)
 	kvmppc_recalc_shadow_msr(vcpu);
 }
 
+static int kvmppc_check_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr)
+{
+	ulong srr0 = vcpu->arch.pc;
+	int ret;
+
+	/* Need to do paired single emulation? */
+	if (!(vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE))
+		return EMULATE_DONE;
+
+	/* Read out the instruction */
+	ret = kvmppc_ld(vcpu, &srr0, sizeof(u32), &vcpu->arch.last_inst, false);
+	if (ret = -ENOENT) {
+		vcpu->arch.msr = kvmppc_set_field(vcpu->arch.msr, 33, 33, 1);
+		vcpu->arch.msr = kvmppc_set_field(vcpu->arch.msr, 34, 36, 0);
+		vcpu->arch.msr = kvmppc_set_field(vcpu->arch.msr, 42, 47, 0);
+		kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_INST_STORAGE);
+	} else if(ret = EMULATE_DONE) {
+		/* Need to emulate */
+		return EMULATE_FAIL;
+	}
+
+	return EMULATE_AGAIN;
+}
+
 /* Handle external providers (FPU, Altivec, VSX) */
 static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
 			     ulong msr)
@@ -772,6 +798,7 @@ int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
 		enum emulation_result er;
 		ulong flags;
 
+program_interrupt:
 		flags = vcpu->arch.shadow_srr1 & 0x1f0000ull;
 
 		if (vcpu->arch.msr & MSR_PR) {
@@ -815,14 +842,32 @@ int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
 		r = RESUME_GUEST;
 		break;
 	case BOOK3S_INTERRUPT_FP_UNAVAIL:
-		r = kvmppc_handle_ext(vcpu, exit_nr, MSR_FP);
-		break;
 	case BOOK3S_INTERRUPT_ALTIVEC:
-		r = kvmppc_handle_ext(vcpu, exit_nr, MSR_VEC);
-		break;
 	case BOOK3S_INTERRUPT_VSX:
-		r = kvmppc_handle_ext(vcpu, exit_nr, MSR_VSX);
+	{
+		int ext_msr = 0;
+
+		switch (exit_nr) {
+		case BOOK3S_INTERRUPT_FP_UNAVAIL: ext_msr = MSR_FP;  break;
+		case BOOK3S_INTERRUPT_ALTIVEC:    ext_msr = MSR_VEC; break;
+		case BOOK3S_INTERRUPT_VSX:        ext_msr = MSR_VSX; break;
+		}
+
+		switch (kvmppc_check_ext(vcpu, exit_nr)) {
+		case EMULATE_DONE:
+			/* everything ok - let's enable the ext */
+			r = kvmppc_handle_ext(vcpu, exit_nr, ext_msr);
+			break;
+		case EMULATE_FAIL:
+			/* we need to emulate this instruction */
+			goto program_interrupt;
+			break;
+		default:
+			/* nothing to worry about - go again */
+			break;
+		}
 		break;
+	}
 	case BOOK3S_INTERRUPT_MACHINE_CHECK:
 	case BOOK3S_INTERRUPT_TRACE:
 		kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
Signature · 1 line
1.6.0.2
#18 lore raw

PATCH 9/21 KVM: PPC: Preload FPU when possible

In reply to Alexander Graf

There are some situations when we're pretty sure the guest will use the FPU soon. So we can save the churn of going into the guest, finding out it does want to use the FPU and going out again.

This patch adds preloading of the FPU when it's reasonable.

---
 arch/powerpc/kvm/book3s.c |    8 ++++++++
 1 files changed, 8 insertions(+), 0 deletions(-)
arch/powerpc/kvm/book3s.c +8 -0
index 6bdf7f2..07f8b42 100644
@@ -137,6 +137,10 @@ void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
 		kvmppc_mmu_flush_segments(vcpu);
 		kvmppc_mmu_map_segment(vcpu, vcpu->arch.pc);
 	}
+
+	/* Preload FPU if it's enabled */
+	if (vcpu->arch.msr & MSR_FP)
+		kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
 }
 
 void kvmppc_inject_interrupt(struct kvm_vcpu *vcpu, int vec, u64 flags)
@@ -1194,6 +1198,10 @@ int __kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
 	/* XXX we get called with irq disabled - change that! */
 	local_irq_enable();
 
+	/* Preload FPU if it's enabled */
+	if (vcpu->arch.msr & MSR_FP)
+		kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
+
 	ret = __kvmppc_vcpu_entry(kvm_run, vcpu);
 
 	local_irq_disable();
Signature · 1 line
1.6.0.2
#19 lore raw

PATCH 11/21 KVM: PPC: Implement mtsr instruction emulation

In reply to Alexander Graf

The Book3S_32 specifications allows for two instructions to modify segment
registers: mtsrin and mtsr.

Most normal operating systems use mtsrin, because it allows to define which segment it wants to change using a register. But since I was trying to run an embedded guest, it turned out to be using mtsr with hardcoded values.

So let's also emulate mtsr. It's a valid instruction after all.

---
 arch/powerpc/kvm/book3s_64_emulate.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)
arch/powerpc/kvm/book3s_64_emulate.c +6 -0
index bb4a7c1..e4e7ec3 100644
@@ -28,6 +28,7 @@
 #define OP_31_XOP_MFMSR		83
 #define OP_31_XOP_MTMSR		146
 #define OP_31_XOP_MTMSRD	178
+#define OP_31_XOP_MTSR		210
 #define OP_31_XOP_MTSRIN	242
 #define OP_31_XOP_TLBIEL	274
 #define OP_31_XOP_TLBIE		306
@@ -101,6 +102,11 @@ int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
 			}
 			break;
 		}
+		case OP_31_XOP_MTSR:
+			vcpu->arch.mmu.mtsrin(vcpu,
+				(inst >> 16) & 0xf,
+				kvmppc_get_gpr(vcpu, get_rs(inst)));
+			break;
 		case OP_31_XOP_MTSRIN:
 			vcpu->arch.mmu.mtsrin(vcpu,
 				(kvmppc_get_gpr(vcpu, get_rb(inst)) >> 28) & 0xf,
Signature · 1 line
1.6.0.2
#20 lore raw

PATCH 12/21 KVM: PPC: Make software load/store return eaddr

In reply to Alexander Graf

The Book3S KVM implementation contains some helper functions to load and store data from and to virtual addresses.

Unfortunately, this helper used to keep the physical address it so nicely found out for us to itself. So let's change that and make it return the physical address it resolved.

---
 arch/powerpc/include/asm/kvm_book3s.h |    4 +-
 arch/powerpc/kvm/book3s.c             |   41 ++++++++++++++++++++-------------
 arch/powerpc/kvm/book3s_64_emulate.c  |   11 +++++----
 3 files changed, 33 insertions(+), 23 deletions(-)
arch/powerpc/include/asm/kvm_book3s.h +2 -2
index d28ee83..8463976 100644
@@ -115,8 +115,8 @@ extern int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte);
 extern int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr);
 extern void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu);
 extern struct kvmppc_pte *kvmppc_mmu_find_pte(struct kvm_vcpu *vcpu, u64 ea, bool data);
-extern int kvmppc_ld(struct kvm_vcpu *vcpu, ulong eaddr, int size, void *ptr, bool data);
-extern int kvmppc_st(struct kvm_vcpu *vcpu, ulong eaddr, int size, void *ptr);
+extern int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, bool data);
+extern int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, bool data);
 extern void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec);
 extern void kvmppc_set_bat(struct kvm_vcpu *vcpu, struct kvmppc_bat *bat,
 			   bool upper, u32 val);
arch/powerpc/kvm/book3s.c +25 -16

80 lines not on this page · raw message

arch/powerpc/kvm/book3s_64_emulate.c +6 -5
index e4e7ec3..a93aa47 100644
@@ -169,7 +169,7 @@ int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
 		{
 			ulong rb = kvmppc_get_gpr(vcpu, get_rb(inst));
 			ulong ra = 0;
-			ulong addr;
+			ulong addr, vaddr;
 			u32 zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
 
 			if (get_ra(inst))
@@ -178,15 +178,16 @@ int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
 			addr = (ra + rb) & ~31ULL;
 			if (!(vcpu->arch.msr & MSR_SF))
 				addr &= 0xffffffff;
+			vaddr = addr;
 
-			if (kvmppc_st(vcpu, addr, 32, zeros)) {
-				vcpu->arch.dear = addr;
-				vcpu->arch.fault_dear = addr;
+			if (kvmppc_st(vcpu, &addr, 32, zeros, true)) {
+				vcpu->arch.dear = vaddr;
+				vcpu->arch.fault_dear = vaddr;
 				to_book3s(vcpu)->dsisr = DSISR_PROTFAULT |
 						      DSISR_ISSTORE;
 				kvmppc_book3s_queue_irqprio(vcpu,
 					BOOK3S_INTERRUPT_DATA_STORAGE);
-				kvmppc_mmu_pte_flush(vcpu, addr, ~0xFFFULL);
+				kvmppc_mmu_pte_flush(vcpu, vaddr, ~0xFFFULL);
 			}
 
 			break;
Signature · 1 line
1.6.0.2
#21 lore raw

PATCH 15/21 KVM: PPC: Fix error in BAT assignment

In reply to Alexander Graf

BATs didn't work. Well, they did, but only up to BAT3. As soon as we came to BAT4 the offset calculation was screwed up and we ended up overwriting BAT0-3.

Fortunately, Linux hasn't been using BAT4+. It's still a good idea to write correct code though.

---
 arch/powerpc/kvm/book3s_64_emulate.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
arch/powerpc/kvm/book3s_64_emulate.c +2 -2
index a93aa47..1d1b952 100644
@@ -233,13 +233,13 @@ static void kvmppc_write_bat(struct kvm_vcpu *vcpu, int sprn, u32 val)
 		bat = &vcpu_book3s->ibat[(sprn - SPRN_IBAT0U) / 2];
 		break;
 	case SPRN_IBAT4U ... SPRN_IBAT7L:
-		bat = &vcpu_book3s->ibat[(sprn - SPRN_IBAT4U) / 2];
+		bat = &vcpu_book3s->ibat[4 + ((sprn - SPRN_IBAT4U) / 2)];
 		break;
 	case SPRN_DBAT0U ... SPRN_DBAT3L:
 		bat = &vcpu_book3s->dbat[(sprn - SPRN_DBAT0U) / 2];
 		break;
 	case SPRN_DBAT4U ... SPRN_DBAT7L:
-		bat = &vcpu_book3s->dbat[(sprn - SPRN_DBAT4U) / 2];
+		bat = &vcpu_book3s->dbat[4 + ((sprn - SPRN_DBAT4U) / 2)];
 		break;
 	default:
 		BUG();
Signature · 1 line
1.6.0.2
#22 lore raw

PATCH 19/21 KVM: PPC: Add capability for paired singles

In reply to Alexander Graf

We need to tell userspace that we can emulate paired single instructions. So let's add a capability export.

---
 arch/powerpc/kvm/powerpc.c |    1 +
 include/linux/kvm.h        |    1 +
 2 files changed, 2 insertions(+), 0 deletions(-)
arch/powerpc/kvm/powerpc.c +1 -0
index a1f9626..8902935 100644
@@ -147,6 +147,7 @@ int kvm_dev_ioctl_check_extension(long ext)
 
 	switch (ext) {
 	case KVM_CAP_PPC_SEGSTATE:
+	case KVM_CAP_PPC_PAIRED_SINGLES:
 		r = 1;
 		break;
 	case KVM_CAP_COALESCED_MMIO:
include/linux/kvm.h +1 -0
index dfa54be..6adec59 100644
@@ -501,6 +501,7 @@ struct kvm_ioeventfd {
 #define KVM_CAP_HYPERV_VAPIC 45
 #define KVM_CAP_HYPERV_SPIN 46
 #define KVM_CAP_PCI_SEGMENT 47
+#define KVM_CAP_PPC_PAIRED_SINGLES 48
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
Signature · 1 line
1.6.0.2
#23 lore raw

PATCH 21/21 KVM: PPC: Simplify kvmppc_load_up_(FPU|VMX|VSX)

In reply to Alexander Graf

We don't need as complex code. I had some thinkos while writing it, figuring I needed to support PPC32 paths on PPC64 which would have required DR=0, but everything just runs fine with DR=1.

So let's make the functions simple C call wrappers that reserve some space on the stack for the respective functions to clobber.

Fixes out-of-RMA-access (and thus guest FPU loading) on the PS3.

---
 arch/powerpc/kvm/book3s_64_rmhandlers.S |   13 ++-----------
 1 files changed, 2 insertions(+), 11 deletions(-)
arch/powerpc/kvm/book3s_64_rmhandlers.S +2 -11
index c83c60a..bd08535 100644
@@ -164,24 +164,15 @@ _GLOBAL(kvmppc_rmcall)
 #define define_load_up(what) 				\
 							\
 _GLOBAL(kvmppc_load_up_ ## what);			\
-	subi	r1, r1, INT_FRAME_SIZE;			\
+	stdu	r1, -INT_FRAME_SIZE(r1);		\
 	mflr	r3;					\
 	std	r3, _LINK(r1);				\
-	mfmsr	r4;					\
-	std	r31, GPR3(r1);				\
-	mr	r31, r4;				\
-	li	r5, MSR_DR;				\
-	oris	r5, r5, MSR_EE@h;			\
-	andc	r4, r4, r5;				\
-	mtmsr	r4;					\
 							\
 	bl	.load_up_ ## what;			\
 							\
-	mtmsr	r31;					\
 	ld	r3, _LINK(r1);				\
-	ld	r31, GPR3(r1);				\
-	addi	r1, r1, INT_FRAME_SIZE;			\
 	mtlr	r3;					\
+	addi	r1, r1, INT_FRAME_SIZE;			\
 	blr
 
 define_load_up(fpu)
Signature · 1 line
1.6.0.2