Skip to contentSkip to conversation

KVM: PPC: e500: Save/restore SPE state

PATCH In review Scott Wood 2 participants 4 messages Mar 30, 2011

Subsystems (2)
lore
Share & tools
lore

Scott Wood

author
#0 lore raw

PATCH v4 4/4 KVM: PPC: e500: Save/restore SPE state

This is done lazily. The SPE save will be done only if the guest has used SPE since the last preemption or heavyweight exit. Restore will be done only on demand, when enabling MSR_SPE in the shadow MSR, in response to an SPE fault or mtmsr emulation.

For SPEFSCR, Linux already switches it on context switch (non-lazily), so the only remaining bit is to save it between qemu and the guest.

---
v4:
- use shadow_msr rather than msr_block
- restore guest SPE state only when shadow_msr[SPE] is set, not when delivering an SPE exception to a guest with shared->msr[SPE] clear.
- use refactored SPE save/restore macros rather than duplicate

 arch/powerpc/include/asm/kvm_host.h  |    6 +++
 arch/powerpc/include/asm/reg_booke.h |    1 +
 arch/powerpc/kernel/asm-offsets.c    |    6 +++
 arch/powerpc/kvm/booke.c             |   70 +++++++++++++++++++++++++++++++++-
 arch/powerpc/kvm/booke.h             |   18 ++-------
 arch/powerpc/kvm/booke_interrupts.S  |   40 +++++++++++++++++++
 arch/powerpc/kvm/e500.c              |   19 +++++++++-
 7 files changed, 143 insertions(+), 17 deletions(-)
arch/powerpc/include/asm/kvm_host.h +6 -0
index 072ec7b..a3810ab 100644
@@ -195,6 +195,12 @@ struct kvm_vcpu_arch {
 	u64 fpr[32];
 	u64 fpscr;
 
+#ifdef CONFIG_SPE
+	ulong evr[32];
+	ulong spefscr;
+	ulong host_spefscr;
+	u64 acc;
+#endif
 #ifdef CONFIG_ALTIVEC
 	vector128 vr[32];
 	vector128 vscr;
arch/powerpc/include/asm/reg_booke.h +1 -0
index 3b1a9b7..2705f9a 100644
@@ -312,6 +312,7 @@
 #define ESR_ILK		0x00100000	/* Instr. Cache Locking */
 #define ESR_PUO		0x00040000	/* Unimplemented Operation exception */
 #define ESR_BO		0x00020000	/* Byte Ordering */
+#define ESR_SPV		0x00000080	/* Signal Processing operation */
 
 /* Bit definitions related to the DBCR0. */
 #if defined(CONFIG_40x)
arch/powerpc/kernel/asm-offsets.c +6 -0
index 5120a63..4d39f2d 100644
@@ -494,6 +494,12 @@ int main(void)
 	DEFINE(TLBCAM_MAS3, offsetof(struct tlbcam, MAS3));
 	DEFINE(TLBCAM_MAS7, offsetof(struct tlbcam, MAS7));
 #endif
+#ifdef CONFIG_SPE
+	DEFINE(VCPU_EVR, offsetof(struct kvm_vcpu, arch.evr[0]));
+	DEFINE(VCPU_ACC, offsetof(struct kvm_vcpu, arch.acc));
+	DEFINE(VCPU_SPEFSCR, offsetof(struct kvm_vcpu, arch.spefscr));
+	DEFINE(VCPU_HOST_SPEFSCR, offsetof(struct kvm_vcpu, arch.host_spefscr));
+#endif /* CONFIG_SPE */
 
 #ifdef CONFIG_KVM_EXIT_TIMING
 	DEFINE(VCPU_TIMING_EXIT_TBU, offsetof(struct kvm_vcpu,
arch/powerpc/kvm/booke.c +68 -2

96 lines not on this page · raw message

arch/powerpc/kvm/booke.h +4 -14
index 492bb70..6ebf852 100644
@@ -52,24 +52,14 @@
 
 extern unsigned long kvmppc_booke_handlers;
 
-/* Helper function for "full" MSR writes. No need to call this if only EE is
- * changing. */
-static inline void kvmppc_set_msr(struct kvm_vcpu *vcpu, u32 new_msr)
-{
-	if ((new_msr & MSR_PR) != (vcpu->arch.shared->msr & MSR_PR))
-		kvmppc_mmu_priv_switch(vcpu, new_msr & MSR_PR);
-
-	vcpu->arch.shared->msr = new_msr;
-
-	if (vcpu->arch.shared->msr & MSR_WE) {
-		kvm_vcpu_block(vcpu);
-		kvmppc_set_exit_type(vcpu, EMULATED_MTMSRWE_EXITS);
-	};
-}
+void kvmppc_set_msr(struct kvm_vcpu *vcpu, u32 new_msr);
 
 int kvmppc_booke_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
                             unsigned int inst, int *advance);
 int kvmppc_booke_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt);
 int kvmppc_booke_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, int rs);
 
+void kvmppc_load_guest_spe(struct kvm_vcpu *vcpu);
+void kvmppc_save_guest_spe(struct kvm_vcpu *vcpu);
+
 #endif /* __KVM_BOOKE_H__ */
arch/powerpc/kvm/booke_interrupts.S +40 -0
index 307771c..f85a7e0 100644
@@ -13,6 +13,7 @@
  * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  *
  * Copyright IBM Corp. 2007
+ * Copyright 2011 Freescale Semiconductor, Inc.
  *
  * Authors: Hollis Blanchard <[email protected]>
  */
@@ -239,6 +240,14 @@ _GLOBAL(kvmppc_resume_host)
 heavyweight_exit:
 	/* Not returning to guest. */
 
+#ifdef CONFIG_SPE
+	/* save guest SPEFSCR and load host SPEFSCR */
+	mfspr	r9, SPRN_SPEFSCR
+	stw	r9, VCPU_SPEFSCR(r4)
+	lwz	r9, VCPU_HOST_SPEFSCR(r4)
+	mtspr	SPRN_SPEFSCR, r9
+#endif
+
 	/* We already saved guest volatile register state; now save the
 	 * non-volatiles. */
 	stw	r15, VCPU_GPR(r15)(r4)
@@ -340,6 +349,14 @@ _GLOBAL(__kvmppc_vcpu_run)
 	lwz	r30, VCPU_GPR(r30)(r4)
 	lwz	r31, VCPU_GPR(r31)(r4)
 
+#ifdef CONFIG_SPE
+	/* save host SPEFSCR and load guest SPEFSCR */
+	mfspr	r3, SPRN_SPEFSCR
+	stw	r3, VCPU_HOST_SPEFSCR(r4)
+	lwz	r3, VCPU_SPEFSCR(r4)
+	mtspr	SPRN_SPEFSCR, r3
+#endif
+
 lightweight_exit:
 	stw	r2, HOST_R2(r1)
 
@@ -426,3 +443,26 @@ lightweight_exit:
 	lwz	r3, VCPU_GPR(r3)(r4)
 	lwz	r4, VCPU_GPR(r4)(r4)
 	rfi
+
+#ifdef CONFIG_SPE
+_GLOBAL(kvmppc_save_guest_spe)
+	cmpi	0,r3,0
+	beqlr-
+	addi	r5,r3,VCPU_EVR
+	SAVE_32EVRS(0, r4, r5, 0)
+	evxor   evr6, evr6, evr6
+	evmwumiaa evr6, evr6, evr6
+	li	r4,VCPU_ACC
+	evstddx evr6, r4, r3		/* save acc */
+	blr
+
+_GLOBAL(kvmppc_load_guest_spe)
+	cmpi	0,r3,0
+	beqlr-
+	li      r4,VCPU_ACC
+	evlddx  evr6,r4,r3
+	evmra   evr6,evr6		/* load acc */
+	addi	r5,r3,VCPU_EVR
+	REST_32EVRS(0, r4, r5, 0)
+	blr
+#endif
arch/powerpc/kvm/e500.c +18 -1
index 0c1af12..62c5719 100644
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008 Freescale Semiconductor, Inc. All rights reserved.
+ * Copyright (C) 2008-2011 Freescale Semiconductor, Inc. All rights reserved.
  *
  * Author: Yu Liu, <[email protected]>
  *
@@ -25,6 +25,22 @@
 #include "booke.h"
 #include "e500_tlb.h"
 
+#ifdef CONFIG_SPE
+static void kvmppc_vcpu_spe_put(struct kvm_vcpu *vcpu)
+{
+	if (vcpu->arch.shadow_msr & MSR_SPE) {
+		enable_kernel_spe();
+		kvmppc_save_guest_spe(vcpu);
+		vcpu->arch.shadow_msr &= ~MSR_SPE;
+	}
+}
+
+#else
+static void kvmppc_vcpu_spe_put(struct kvm_vcpu *vcpu)
+{
+}
+#endif
+
 void kvmppc_core_load_host_debugstate(struct kvm_vcpu *vcpu)
 {
 }
@@ -41,6 +57,7 @@ void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
 void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
 {
 	kvmppc_e500_tlb_put(vcpu);
+	kvmppc_vcpu_spe_put(vcpu);
 }
 
 int kvmppc_core_check_processor_compat(void)
Signature · 1 line
1.7.1
#1 lore raw
Quoted 10 lines · Scott Wood

This is done lazily. The SPE save will be done only if the guest has used SPE since the last preemption or heavyweight exit. Restore will be done only on demand, when enabling MSR_SPE in the shadow MSR, in response to an SPE fault or mtmsr emulation.

For SPEFSCR, Linux already switches it on context switch (non-lazily), so the only remaining bit is to save it between qemu and the guest.

Quoted 7 lines

---
v4:
- use shadow_msr rather than msr_block
- restore guest SPE state only when shadow_msr[SPE] is set, not when delivering an SPE exception to a guest with shared->msr[SPE] clear.
- use refactored SPE save/restore macros rather than duplicate

Very nice, it looks a lot cleaner than before and aligns better with book3s now.

Quoted 69 lines
arch/powerpc/include/asm/kvm_host.h  |    6 +++
arch/powerpc/include/asm/reg_booke.h |    1 +
arch/powerpc/kernel/asm-offsets.c    |    6 +++
arch/powerpc/kvm/booke.c             |   70 +++++++++++++++++++++++++++++++++-
arch/powerpc/kvm/booke.h             |   18 ++-------
arch/powerpc/kvm/booke_interrupts.S  |   40 +++++++++++++++++++
arch/powerpc/kvm/e500.c              |   19 +++++++++-
7 files changed, 143 insertions(+), 17 deletions(-)
arch/powerpc/include/asm/kvm_host.h +6 -0
index 072ec7b..a3810ab 100644
@@ -195,6 +195,12 @@ struct kvm_vcpu_arch {
 	u64 fpr[32];
 	u64 fpscr;
 
+#ifdef CONFIG_SPE
+	ulong evr[32];
+	ulong spefscr;
+	ulong host_spefscr;
+	u64 acc;
+#endif
 #ifdef CONFIG_ALTIVEC
 	vector128 vr[32];
 	vector128 vscr;
arch/powerpc/include/asm/reg_booke.h +1 -0
index 3b1a9b7..2705f9a 100644
@@ -312,6 +312,7 @@
 #define ESR_ILK		0x00100000	/* Instr. Cache Locking */
 #define ESR_PUO		0x00040000	/* Unimplemented Operation exception */
 #define ESR_BO		0x00020000	/* Byte Ordering */
+#define ESR_SPV		0x00000080	/* Signal Processing operation */
 
 /* Bit definitions related to the DBCR0. */
 #if defined(CONFIG_40x)
arch/powerpc/kernel/asm-offsets.c +6 -0
index 5120a63..4d39f2d 100644
@@ -494,6 +494,12 @@ int main(void)
 	DEFINE(TLBCAM_MAS3, offsetof(struct tlbcam, MAS3));
 	DEFINE(TLBCAM_MAS7, offsetof(struct tlbcam, MAS7));
 #endif
+#ifdef CONFIG_SPE
+	DEFINE(VCPU_EVR, offsetof(struct kvm_vcpu, arch.evr[0]));
+	DEFINE(VCPU_ACC, offsetof(struct kvm_vcpu, arch.acc));
+	DEFINE(VCPU_SPEFSCR, offsetof(struct kvm_vcpu, arch.spefscr));
+	DEFINE(VCPU_HOST_SPEFSCR, offsetof(struct kvm_vcpu, arch.host_spefscr));
+#endif /* CONFIG_SPE */
 
 #ifdef CONFIG_KVM_EXIT_TIMING
 	DEFINE(VCPU_TIMING_EXIT_TBU, offsetof(struct kvm_vcpu,
arch/powerpc/kvm/booke.c +1 -0
index 1204e1d..02a1aa9 100644
@@ -13,6 +13,7 @@
 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * Copyright IBM Corp. 2007
+ * Copyright 2010-2011 Freescale Semiconductor, Inc.
 *
 * Authors: Hollis Blanchard <[email protected]>
 *          Christian Ehrhardt <[email protected]>

arch/powerpc/kvm/booke.c void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu) · line 86

Quoted 8 lines
arch/powerpc/kvm/booke.c +5 -0
@@ -78,6 +79,43 @@ void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu)
 	}
 }
 
+#ifdef CONFIG_SPE
+static void kvmppc_vcpu_enable_spe(struct kvm_vcpu *vcpu)
+{
+	enable_kernel_spe();
+	kvmppc_load_guest_spe(vcpu);

Are you sure this is only ever called from !preempt code?

Alex

author
#2 lore raw
In reply to Scott Wood
Quoted 11 lines · On Thu, 31 Mar 2011 00:43:27 +0200 Alexander Graf <[email protected]> wrote:

arch/powerpc/kvm/booke.c void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu) · line 86

Quoted 9 lines
arch/powerpc/kvm/booke.c +5 -0
@@ -78,6 +79,43 @@ void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu)
 	}
 }
 
+#ifdef CONFIG_SPE
+static void kvmppc_vcpu_enable_spe(struct kvm_vcpu *vcpu)
+{
+	enable_kernel_spe();
+	kvmppc_load_guest_spe(vcpu);

Are you sure this is only ever called from !preempt code?

Hmm, I guess not. We should always have the preempt notifier in place, though, so it should suffice to stick preempt_disable/enable() at the beginning/end of this function. If we get preempted after that the notifier will clean up.

-Scott

#3 lore raw
In reply to Scott Wood
Quoted 15 lines · Scott Wood
Quoted 11 lines · On Thu, 31 Mar 2011 00:43:27 +0200 Alexander Graf <[email protected]> wrote:

arch/powerpc/kvm/booke.c void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu) · line 86

Quoted 9 lines
arch/powerpc/kvm/booke.c +5 -0
@@ -78,6 +79,43 @@ void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu)
 	}
 }
 
+#ifdef CONFIG_SPE
+static void kvmppc_vcpu_enable_spe(struct kvm_vcpu *vcpu)
+{
+	enable_kernel_spe();
+	kvmppc_load_guest_spe(vcpu);

Are you sure this is only ever called from !preempt code?

Quoted 4 lines

Hmm, I guess not. We should always have the preempt notifier in place, though, so it should suffice to stick preempt_disable/enable() at the beginning/end of this function. If we get preempted after that the notifier will clean up.

If we stick preempt_enable()/preempt_disable in the function, preemption will be enabled when being disabled before, right? So we wouldn't be able to call this from vcpu_load for example, as that occurs within the preempt notifier callback.

Alex