[PATCH 15/27] KVM: PPC: Make real mode handler generic

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Alexander Graf
Date: Thursday, April 15, 2010 - 3:11 pm

The real mode handler code was originally writen for 64 bit Book3S only.
But since we not add 32 bit functionality too, we need to make some tweaks
to it.

This patch basically combines using the "long" access defines and using
fields from the shadow VCPU we just moved there.

Signed-off-by: Alexander Graf <agraf@suse.de>
---
 arch/powerpc/kvm/book3s_rmhandlers.S |  119 +++++++++++++++++++++++++---------
 1 files changed, 88 insertions(+), 31 deletions(-)

diff --git a/arch/powerpc/kvm/book3s_rmhandlers.S b/arch/powerpc/kvm/book3s_rmhandlers.S
index bd08535..0c8d331 100644
--- a/arch/powerpc/kvm/book3s_rmhandlers.S
+++ b/arch/powerpc/kvm/book3s_rmhandlers.S
@@ -22,7 +22,10 @@
 #include <asm/reg.h>
 #include <asm/page.h>
 #include <asm/asm-offsets.h>
+
+#ifdef CONFIG_PPC_BOOK3S_64
 #include <asm/exception-64s.h>
+#endif
 
 /*****************************************************************************
  *                                                                           *
@@ -30,6 +33,39 @@
  *                                                                           *
  ****************************************************************************/
 
+#if defined(CONFIG_PPC_BOOK3S_64)
+
+#define LOAD_SHADOW_VCPU(reg)				\
+	mfspr	reg, SPRN_SPRG_PACA
+
+#define SHADOW_VCPU_OFF		PACA_KVM_SVCPU
+#define MSR_NOIRQ		MSR_KERNEL & ~(MSR_IR | MSR_DR)
+#define FUNC(name) 		GLUE(.,name)
+
+#elif defined(CONFIG_PPC_BOOK3S_32)
+
+#define LOAD_SHADOW_VCPU(reg)						\
+	mfspr	reg, SPRN_SPRG_THREAD;					\
+	lwz	reg, THREAD_KVM_SVCPU(reg);				\
+	/* PPC32 can have a NULL pointer - let's check for that */	\
+	mtspr   SPRN_SPRG_SCRATCH1, r12;	/* Save r12 */		\
+	mfcr	r12;							\
+	cmpwi	reg, 0;							\
+	bne	1f;							\
+	mfspr	reg, SPRN_SPRG_SCRATCH0;				\
+	mtcr	r12;							\
+	mfspr	r12, SPRN_SPRG_SCRATCH1;				\
+	b	kvmppc_resume_\intno;					\
+1:;									\
+	mtcr	r12;							\
+	mfspr	r12, SPRN_SPRG_SCRATCH1;				\
+	tophys(reg, reg)
+
+#define SHADOW_VCPU_OFF		0
+#define MSR_NOIRQ		MSR_KERNEL
+#define FUNC(name)		name
+
+#endif
 
 .macro INTERRUPT_TRAMPOLINE intno
 
@@ -42,19 +78,19 @@ kvmppc_trampoline_\intno:
 	 * First thing to do is to find out if we're coming
 	 * from a KVM guest or a Linux process.
 	 *
-	 * To distinguish, we check a magic byte in the PACA
+	 * To distinguish, we check a magic byte in the PACA/current
 	 */
-	mfspr	r13, SPRN_SPRG_PACA		/* r13 = PACA */
-	std	r12, PACA_KVM_SCRATCH0(r13)
+	LOAD_SHADOW_VCPU(r13)
+	PPC_STL	r12, (SHADOW_VCPU_OFF + SVCPU_SCRATCH0)(r13)
 	mfcr	r12
-	stw	r12, PACA_KVM_SCRATCH1(r13)
-	lbz	r12, PACA_KVM_IN_GUEST(r13)
+	stw	r12, (SHADOW_VCPU_OFF + SVCPU_SCRATCH1)(r13)
+	lbz	r12, (SHADOW_VCPU_OFF + SVCPU_IN_GUEST)(r13)
 	cmpwi	r12, KVM_GUEST_MODE_NONE
 	bne	..kvmppc_handler_hasmagic_\intno
 	/* No KVM guest? Then jump back to the Linux handler! */
-	lwz	r12, PACA_KVM_SCRATCH1(r13)
+	lwz	r12, (SHADOW_VCPU_OFF + SVCPU_SCRATCH1)(r13)
 	mtcr	r12
-	ld	r12, PACA_KVM_SCRATCH0(r13)
+	PPC_LL	r12, (SHADOW_VCPU_OFF + SVCPU_SCRATCH0)(r13)
 	mfspr	r13, SPRN_SPRG_SCRATCH0		/* r13 = original r13 */
 	b	kvmppc_resume_\intno		/* Get back original handler */
 
@@ -76,9 +112,7 @@ kvmppc_trampoline_\intno:
 INTERRUPT_TRAMPOLINE	BOOK3S_INTERRUPT_SYSTEM_RESET
 INTERRUPT_TRAMPOLINE	BOOK3S_INTERRUPT_MACHINE_CHECK
 INTERRUPT_TRAMPOLINE	BOOK3S_INTERRUPT_DATA_STORAGE
-INTERRUPT_TRAMPOLINE	BOOK3S_INTERRUPT_DATA_SEGMENT
 INTERRUPT_TRAMPOLINE	BOOK3S_INTERRUPT_INST_STORAGE
-INTERRUPT_TRAMPOLINE	BOOK3S_INTERRUPT_INST_SEGMENT
 INTERRUPT_TRAMPOLINE	BOOK3S_INTERRUPT_EXTERNAL
 INTERRUPT_TRAMPOLINE	BOOK3S_INTERRUPT_ALIGNMENT
 INTERRUPT_TRAMPOLINE	BOOK3S_INTERRUPT_PROGRAM
@@ -88,7 +122,14 @@ INTERRUPT_TRAMPOLINE	BOOK3S_INTERRUPT_SYSCALL
 INTERRUPT_TRAMPOLINE	BOOK3S_INTERRUPT_TRACE
 INTERRUPT_TRAMPOLINE	BOOK3S_INTERRUPT_PERFMON
 INTERRUPT_TRAMPOLINE	BOOK3S_INTERRUPT_ALTIVEC
+
+/* Those are only available on 64 bit machines */
+
+#ifdef CONFIG_PPC_BOOK3S_64
+INTERRUPT_TRAMPOLINE	BOOK3S_INTERRUPT_DATA_SEGMENT
+INTERRUPT_TRAMPOLINE	BOOK3S_INTERRUPT_INST_SEGMENT
 INTERRUPT_TRAMPOLINE	BOOK3S_INTERRUPT_VSX
+#endif
 
 /*
  * Bring us back to the faulting code, but skip the
@@ -99,11 +140,11 @@ INTERRUPT_TRAMPOLINE	BOOK3S_INTERRUPT_VSX
  *
  * Input Registers:
  *
- * R12               = free
- * R13               = PACA
- * PACA.KVM.SCRATCH0 = guest R12
- * PACA.KVM.SCRATCH1 = guest CR
- * SPRG_SCRATCH0     = guest R13
+ * R12            = free
+ * R13            = Shadow VCPU (PACA)
+ * SVCPU.SCRATCH0 = guest R12
+ * SVCPU.SCRATCH1 = guest CR
+ * SPRG_SCRATCH0  = guest R13
  *
  */
 kvmppc_handler_skip_ins:
@@ -114,9 +155,9 @@ kvmppc_handler_skip_ins:
 	mtsrr0	r12
 
 	/* Clean up all state */
-	lwz	r12, PACA_KVM_SCRATCH1(r13)
+	lwz	r12, (SHADOW_VCPU_OFF + SVCPU_SCRATCH1)(r13)
 	mtcr	r12
-	ld	r12, PACA_KVM_SCRATCH0(r13)
+	PPC_LL	r12, (SHADOW_VCPU_OFF + SVCPU_SCRATCH0)(r13)
 	mfspr	r13, SPRN_SPRG_SCRATCH0
 
 	/* And get back into the code */
@@ -147,32 +188,48 @@ kvmppc_handler_lowmem_trampoline_end:
  *
  * R3 = function
  * R4 = MSR
- * R5 = CTR
+ * R5 = scratch register
  *
  */
 _GLOBAL(kvmppc_rmcall)
-	mtmsr	r4		/* Disable relocation, so mtsrr
+	LOAD_REG_IMMEDIATE(r5, MSR_NOIRQ)
+	mtmsr	r5		/* Disable relocation and interrupts, so mtsrr
 				   doesn't get interrupted */
-	mtctr	r5
+	sync
 	mtsrr0	r3
 	mtsrr1	r4
 	RFI
 
+#if defined(CONFIG_PPC_BOOK3S_32)
+#define STACK_LR	INT_FRAME_SIZE+4
+#elif defined(CONFIG_PPC_BOOK3S_64)
+#define STACK_LR	_LINK
+#endif
+
 /*
  * Activate current's external feature (FPU/Altivec/VSX)
  */
-#define define_load_up(what) 				\
-							\
-_GLOBAL(kvmppc_load_up_ ## what);			\
-	stdu	r1, -INT_FRAME_SIZE(r1);		\
-	mflr	r3;					\
-	std	r3, _LINK(r1);				\
-							\
-	bl	.load_up_ ## what;			\
-							\
-	ld	r3, _LINK(r1);				\
-	mtlr	r3;					\
-	addi	r1, r1, INT_FRAME_SIZE;			\
+#define define_load_up(what) 					\
+								\
+_GLOBAL(kvmppc_load_up_ ## what);				\
+	PPC_STLU r1, -INT_FRAME_SIZE(r1);			\
+	mflr	r3;						\
+	PPC_STL	r3, STACK_LR(r1);				\
+	PPC_STL	r20, _NIP(r1);					\
+	mfmsr	r20;						\
+	LOAD_REG_IMMEDIATE(r3, MSR_DR|MSR_EE);			\
+	andc	r3,r20,r3;		/* Disable DR,EE */	\
+	mtmsr	r3;						\
+	sync;							\
+								\
+	bl	FUNC(load_up_ ## what);				\
+								\
+	mtmsr	r20;			/* Enable DR,EE */	\
+	sync;							\
+	PPC_LL	r3, STACK_LR(r1);				\
+	PPC_LL	r20, _NIP(r1);					\
+	mtlr	r3;						\
+	addi	r1, r1, INT_FRAME_SIZE;				\
 	blr
 
 define_load_up(fpu)
-- 
1.6.0.2

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[PATCH 00/27] Book3S_32 (PPC32) KVM support, Alexander Graf, (Thu Apr 15, 3:11 pm)
[PATCH 01/27] KVM: PPC: Name generic 64-bit code generic, Alexander Graf, (Thu Apr 15, 3:11 pm)
[PATCH 02/27] KVM: PPC: Add host MMU Support, Alexander Graf, (Thu Apr 15, 3:11 pm)
[PATCH 03/27] KVM: PPC: Add SR swapping code, Alexander Graf, (Thu Apr 15, 3:11 pm)
[PATCH 04/27] KVM: PPC: Add generic segment switching code, Alexander Graf, (Thu Apr 15, 3:11 pm)
[PATCH 05/27] PPC: Split context init/destroy functions, Alexander Graf, (Thu Apr 15, 3:11 pm)
[PATCH 06/27] KVM: PPC: Add kvm_book3s_64.h, Alexander Graf, (Thu Apr 15, 3:11 pm)
[PATCH 07/27] KVM: PPC: Add kvm_book3s_32.h, Alexander Graf, (Thu Apr 15, 3:11 pm)
[PATCH 08/27] KVM: PPC: Add fields to shadow vcpu, Alexander Graf, (Thu Apr 15, 3:11 pm)
[PATCH 09/27] KVM: PPC: Improve indirect svcpu accessors, Alexander Graf, (Thu Apr 15, 3:11 pm)
[PATCH 10/27] KVM: PPC: Use KVM_BOOK3S_HANDLER, Alexander Graf, (Thu Apr 15, 3:11 pm)
[PATCH 11/27] KVM: PPC: Use CONFIG_PPC_BOOK3S define, Alexander Graf, (Thu Apr 15, 3:11 pm)
[PATCH 12/27] PPC: Add STLU, Alexander Graf, (Thu Apr 15, 3:11 pm)
[PATCH 13/27] KVM: PPC: Use now shadowed vcpu fields, Alexander Graf, (Thu Apr 15, 3:11 pm)
[PATCH 14/27] KVM: PPC: Extract MMU init, Alexander Graf, (Thu Apr 15, 3:11 pm)
[PATCH 15/27] KVM: PPC: Make real mode handler generic, Alexander Graf, (Thu Apr 15, 3:11 pm)
[PATCH 16/27] KVM: PPC: Make highmem code generic, Alexander Graf, (Thu Apr 15, 3:11 pm)
[PATCH 18/27] KVM: PPC: Release clean pages as clean, Alexander Graf, (Thu Apr 15, 3:11 pm)
[PATCH 19/27] KVM: PPC: Remove fetch fail code, Alexander Graf, (Thu Apr 15, 3:11 pm)
[PATCH 20/27] KVM: PPC: Add SVCPU to Book3S_32, Alexander Graf, (Thu Apr 15, 3:11 pm)
[PATCH 21/27] KVM: PPC: Emulate segment fault, Alexander Graf, (Thu Apr 15, 3:11 pm)
[PATCH 22/27] KVM: PPC: Add Book3S compatibility code, Alexander Graf, (Thu Apr 15, 3:11 pm)
[PATCH 23/27] KVM: PPC: Export MMU variables, Alexander Graf, (Thu Apr 15, 3:11 pm)
[PATCH 24/27] PPC: Export SWITCH_FRAME_SIZE, Alexander Graf, (Thu Apr 15, 3:11 pm)
[PATCH 25/27] KVM: PPC: Check max IRQ prio, Alexander Graf, (Thu Apr 15, 3:11 pm)
[PATCH 26/27] KVM: PPC: Add KVM intercept handlers, Alexander Graf, (Thu Apr 15, 3:11 pm)
[PATCH 27/27] KVM: PPC: Enable Book3S_32 KVM building, Alexander Graf, (Thu Apr 15, 3:11 pm)
Re: [PATCH 05/27] PPC: Split context init/destroy functions, Benjamin Herrenschmidt, (Thu Apr 15, 11:46 pm)
Re: [PATCH 12/27] PPC: Add STLU, Benjamin Herrenschmidt, (Thu Apr 15, 11:47 pm)
Re: [PATCH 23/27] KVM: PPC: Export MMU variables, Benjamin Herrenschmidt, (Thu Apr 15, 11:47 pm)
Re: [PATCH 24/27] PPC: Export SWITCH_FRAME_SIZE, Benjamin Herrenschmidt, (Thu Apr 15, 11:48 pm)
Re: [PATCH 26/27] KVM: PPC: Add KVM intercept handlers, Benjamin Herrenschmidt, (Thu Apr 15, 11:48 pm)
Re: [PATCH 23/27] KVM: PPC: Export MMU variables, Alexander Graf, (Fri Apr 16, 2:07 am)
Re: [PATCH 23/27] KVM: PPC: Export MMU variables, Benjamin Herrenschmidt, (Fri Apr 16, 2:22 am)
Re: [PATCH 23/27] KVM: PPC: Export MMU variables, Alexander Graf, (Fri Apr 16, 2:25 am)
Re: [PATCH 23/27] KVM: PPC: Export MMU variables, Alexander Graf, (Fri Apr 16, 2:31 am)
Re: [PATCH 23/27] KVM: PPC: Export MMU variables, Alexander Graf, (Fri Apr 16, 4:18 am)
Re: [PATCH 00/27] Book3S_32 (PPC32) KVM support, Avi Kivity, (Wed Apr 21, 2:42 am)