Skip to contentSkip to conversation

Add common PPC64 KVM asm helpers

PATCH In review Alexander Graf 2 participants 6 messages Jul 7, 2009 – Jul 8, 2009

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

PATCH 5/23 Add common PPC64 KVM asm helpers

We will have to fiddle with a bunch of assembly later on. In order to make this more readable, let's define some macros we can use then.

---
 arch/powerpc/include/asm/kvm_970_asm.h |  128 ++++++++++++++++++++++++++++++++
 1 files changed, 128 insertions(+), 0 deletions(-)
 create mode 100644 arch/powerpc/include/asm/kvm_970_asm.h
arch/powerpc/include/asm/kvm_970_asm.h new file +128 -0
new file mode 100644
index 0000000..9b715e2
@@ -0,0 +1,128 @@
+/*
+ * 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 SUSE Linux Products GmbH 2009
+ *
+ * Authors: Alexander Graf <[email protected]>
+ */
+
+#ifndef __ASM_KVM_970_ASM_H__
+#define __ASM_KVM_970_ASM_H__
+
+#ifdef CONFIG_KVM_970_HANDLER
+
+#include <asm/ppc_asm.h>
+#include <asm/kvm_asm.h>
+#include <asm/reg.h>
+#include <asm/page.h>
+#include <asm/asm-offsets.h>
+#include <asm/exception.h>
+
+#define ULONG_SIZE 8
+#define KVMPPC_HANDLE_EXIT .kvmppc_handle_exit
+
+.macro to_phys dest_reg, source_reg
+#if PAGE_OFFSET = 0xc000000000000000
+	clrldi	\dest_reg, \source_reg, 2
+#else
+	#error Unknown PAGE_OFFSET
+#endif
+.endm
+
+.macro loadimm reg, imm
+	lis	\reg,       \imm@highest
+	ori	\reg, \reg, \imm@higher
+	rldicr	\reg, \reg, 32, 31
+	oris	\reg, \reg, \imm@h
+	ori	\reg, \reg, \imm@l
+.endm
+
+.macro DISABLE_INTERRUPTS
+	mfmsr	r0
+	rldicl	r0,r0,48,1
+	rotldi	r0,r0,16
+	mtmsrd	r0,1
+.endm
+
+.macro setmagc reg
+	ori	\reg, r13, 1		/* r3 = PACA | 1 */
+	mtspr	SPRN_SPRG3, \reg	/* SPRG3 =PACA | 1 */
+.endm
+
+#define HOST_STACK_R1	(0 * ULONG_SIZE)
+/* We need to keep some space here that the C function */
+/* we jump into later can clobber */
+#define HOST_STACK_LR	(8 * ULONG_SIZE)
+#define HOST_STACK_RUN	(9 * ULONG_SIZE)
+#define HOST_STACK_VCPU	(10 * ULONG_SIZE)
+#define HOST_STACK_R14	(14 * ULONG_SIZE)
+#define HOST_STACK_R15	(15 * ULONG_SIZE)
+#define HOST_STACK_R16	(16 * ULONG_SIZE)
+#define HOST_STACK_R17	(17 * ULONG_SIZE)
+#define HOST_STACK_R18	(18 * ULONG_SIZE)
+#define HOST_STACK_R19	(19 * ULONG_SIZE)
+#define HOST_STACK_R20	(20 * ULONG_SIZE)
+#define HOST_STACK_R21	(21 * ULONG_SIZE)
+#define HOST_STACK_R22	(22 * ULONG_SIZE)
+#define HOST_STACK_R23	(23 * ULONG_SIZE)
+#define HOST_STACK_R24	(24 * ULONG_SIZE)
+#define HOST_STACK_R25	(25 * ULONG_SIZE)
+#define HOST_STACK_R26	(26 * ULONG_SIZE)
+#define HOST_STACK_R27	(27 * ULONG_SIZE)
+#define HOST_STACK_R28	(28 * ULONG_SIZE)
+#define HOST_STACK_R29	(29 * ULONG_SIZE)
+#define HOST_STACK_R30	(30 * ULONG_SIZE)
+#define HOST_STACK_R31	(31 * ULONG_SIZE)
+#define HOST_STACK_MIN_SIZE (HOST_STACK_R31 + ULONG_SIZE)
+#define HOST_STACK_SIZE (((HOST_STACK_MIN_SIZE + 15) / 16) * 16) /* Align. */
+#define VCPU_GPR(n)     (VCPU_GPRS + (n * ULONG_SIZE))
+
+.macro mfpaca tmp_reg, src_reg, offset, vcpu_reg
+	ld	\tmp_reg, (PACA_EXMC+\offset)(r13)
+	std	\tmp_reg, VCPU_GPR(\src_reg)(\vcpu_reg)
+.endm
+
+
+.macro DO_KVM intno
+	.if (\intno = PPC970_INTERRUPT_SYSTEM_RESET) || \
+	    (\intno = PPC970_INTERRUPT_MACHINE_CHECK) || \
+	    (\intno = PPC970_INTERRUPT_DATA_STORAGE) || \
+	    (\intno = PPC970_INTERRUPT_INST_STORAGE) || \
+	    (\intno = PPC970_INTERRUPT_DATA_SEGMENT) || \
+	    (\intno = PPC970_INTERRUPT_INST_SEGMENT) || \
+	    (\intno = PPC970_INTERRUPT_EXTERNAL) || \
+	    (\intno = PPC970_INTERRUPT_ALIGNMENT) || \
+	    (\intno = PPC970_INTERRUPT_PROGRAM) || \
+	    (\intno = PPC970_INTERRUPT_FP_UNAVAIL) || \
+	    (\intno = PPC970_INTERRUPT_DECREMENTER) || \
+	    (\intno = PPC970_INTERRUPT_SYSCALL) || \
+	    (\intno = PPC970_INTERRUPT_TRACE) || \
+	    (\intno = PPC970_INTERRUPT_PERFMON) || \
+	    (\intno = PPC970_INTERRUPT_ALTIVEC) || \
+	    (\intno = PPC970_INTERRUPT_VSX)
+
+	b	kvmppc_trampoline_\intno
+kvmppc_resume_\intno:
+
+	.endif
+.endm
+
+#else
+
+.macro DO_KVM intno
+.endm
+
+#endif /* CONFIG_KVM_970_HANDLER */
+
+#endif /* __ASM_KVM_970_ASM_H__ */
Signature · 1 line
1.6.0.2
#1 lore raw

arch/powerpc/include/asm/kvm_970_asm.h line 40

Quoted 6 lines · Alexander Graf

+.macro to_phys dest_reg, source_reg

arch/powerpc/include/asm/kvm_970_asm.h new file +5 -0
+#if PAGE_OFFSET = 0xc000000000000000
+	clrldi	\dest_reg, \source_reg, 2
+#else
+	#error Unknown PAGE_OFFSET
+#endif

We already have tophys() for that in ppc_asm.h

BTW. We prefer using C-style #define for asm macros ... bad habit maybe but at least it's consistent :-)

arch/powerpc/include/asm/kvm_970_asm.h line 49

Quoted 9 lines

+.endm

arch/powerpc/include/asm/kvm_970_asm.h new file +8 -0
+
+.macro loadimm reg, imm
+	lis	\reg,       \imm@highest
+	ori	\reg, \reg, \imm@higher
+	rldicr	\reg, \reg, 32, 31
+	oris	\reg, \reg, \imm@h
+	ori	\reg, \reg, \imm@l
+.endm

We already have LOAD_REG_IMMEDIATE() in ppc_asm.h :-)

arch/powerpc/include/asm/kvm_970_asm.h line 61

Quoted 4 lines

+.macro setmagc reg

arch/powerpc/include/asm/kvm_970_asm.h new file +3 -0
+	ori	\reg, r13, 1		/* r3 = PACA | 1 */
+	mtspr	SPRN_SPRG3, \reg	/* SPRG3 =PACA | 1 */
+.endm

Fun :-) So you set the PACA low bit to indicate you are running in the guest ? That's a pretty good way to do it I suppose.

Quoted 17 lines
arch/powerpc/include/asm/kvm_970_asm.h new file +17 -0
+#define HOST_STACK_R1	(0 * ULONG_SIZE)
+/* We need to keep some space here that the C function */
+/* we jump into later can clobber */
+#define HOST_STACK_LR	(8 * ULONG_SIZE)
+#define HOST_STACK_RUN	(9 * ULONG_SIZE)
+#define HOST_STACK_VCPU	(10 * ULONG_SIZE)
+#define HOST_STACK_R14	(14 * ULONG_SIZE)
+#define HOST_STACK_R15	(15 * ULONG_SIZE)
+#define HOST_STACK_R16	(16 * ULONG_SIZE)
+#define HOST_STACK_R17	(17 * ULONG_SIZE)
+#define HOST_STACK_R18	(18 * ULONG_SIZE)
+#define HOST_STACK_R19	(19 * ULONG_SIZE)
+#define HOST_STACK_R20	(20 * ULONG_SIZE)
+#define HOST_STACK_R21	(21 * ULONG_SIZE)
+#define HOST_STACK_R22	(22 * ULONG_SIZE)
+#define HOST_STACK_R23	(23 * ULONG_SIZE)
+#define HOST_STACK_R24	(24 * ULONG_SIZE)

arch/powerpc/include/asm/kvm_970_asm.h line 89

Quoted 10 lines
arch/powerpc/include/asm/kvm_970_asm.h new file +10 -0
+#define HOST_STACK_R25	(25 * ULONG_SIZE)
+#define HOST_STACK_R26	(26 * ULONG_SIZE)
+#define HOST_STACK_R27	(27 * ULONG_SIZE)
+#define HOST_STACK_R28	(28 * ULONG_SIZE)
+#define HOST_STACK_R29	(29 * ULONG_SIZE)
+#define HOST_STACK_R30	(30 * ULONG_SIZE)
+#define HOST_STACK_R31	(31 * ULONG_SIZE)
+#define HOST_STACK_MIN_SIZE (HOST_STACK_R31 + ULONG_SIZE)
+#define HOST_STACK_SIZE (((HOST_STACK_MIN_SIZE + 15) / 16) * 16) /* Align. */
+#define VCPU_GPR(n)     (VCPU_GPRS + (n * ULONG_SIZE))

Can't you define a C structure and generate offsets in asm-offsets.h using asm-offsets.c like we do for most other stuff ?

Cheers,
Ben.

Quoted 38 lines

+.macro mfpaca tmp_reg, src_reg, offset, vcpu_reg

arch/powerpc/include/asm/kvm_970_asm.h new file +37 -0
+	ld	\tmp_reg, (PACA_EXMC+\offset)(r13)
+	std	\tmp_reg, VCPU_GPR(\src_reg)(\vcpu_reg)
+.endm
+
+
+.macro DO_KVM intno
+	.if (\intno = PPC970_INTERRUPT_SYSTEM_RESET) || \
+	    (\intno = PPC970_INTERRUPT_MACHINE_CHECK) || \
+	    (\intno = PPC970_INTERRUPT_DATA_STORAGE) || \
+	    (\intno = PPC970_INTERRUPT_INST_STORAGE) || \
+	    (\intno = PPC970_INTERRUPT_DATA_SEGMENT) || \
+	    (\intno = PPC970_INTERRUPT_INST_SEGMENT) || \
+	    (\intno = PPC970_INTERRUPT_EXTERNAL) || \
+	    (\intno = PPC970_INTERRUPT_ALIGNMENT) || \
+	    (\intno = PPC970_INTERRUPT_PROGRAM) || \
+	    (\intno = PPC970_INTERRUPT_FP_UNAVAIL) || \
+	    (\intno = PPC970_INTERRUPT_DECREMENTER) || \
+	    (\intno = PPC970_INTERRUPT_SYSCALL) || \
+	    (\intno = PPC970_INTERRUPT_TRACE) || \
+	    (\intno = PPC970_INTERRUPT_PERFMON) || \
+	    (\intno = PPC970_INTERRUPT_ALTIVEC) || \
+	    (\intno = PPC970_INTERRUPT_VSX)
+
+	b	kvmppc_trampoline_\intno
+kvmppc_resume_\intno:
+
+	.endif
+.endm
+
+#else
+
+.macro DO_KVM intno
+.endm
+
+#endif /* CONFIG_KVM_970_HANDLER */
+
+#endif /* __ASM_KVM_970_ASM_H__ */
#2 lore raw
In reply to Alexander Graf
Quoted 10 lines · Benjamin Herrenschmidt

arch/powerpc/include/asm/kvm_970_asm.h line 40

Quoted 6 lines · Alexander Graf

+.macro to_phys dest_reg, source_reg

arch/powerpc/include/asm/kvm_970_asm.h new file +5 -0
+#if PAGE_OFFSET = 0xc000000000000000
+	clrldi	\dest_reg, \source_reg, 2
+#else
+	#error Unknown PAGE_OFFSET
+#endif

We already have tophys() for that in ppc_asm.h

Heh - looks like I reimplemented the wheel :-)

Quoted 3 lines

BTW. We prefer using C-style #define for asm macros ... bad habit maybe
but at least it's consistent :-)

Hum. I personally prefer to have assembly code look like assembly code. Guess it's a matter of personal taste.

Quoted 13 lines

arch/powerpc/include/asm/kvm_970_asm.h line 49

Quoted 10 lines

+.endm

arch/powerpc/include/asm/kvm_970_asm.h new file +8 -0
+
+.macro loadimm reg, imm
+	lis	\reg,       \imm@highest
+	ori	\reg, \reg, \imm@higher
+	rldicr	\reg, \reg, 32, 31
+	oris	\reg, \reg, \imm@h
+	ori	\reg, \reg, \imm@l
+.endm

We already have LOAD_REG_IMMEDIATE() in ppc_asm.h :-)

Quoted 7 lines

arch/powerpc/include/asm/kvm_970_asm.h line 61

Quoted 4 lines

+.macro setmagc reg

arch/powerpc/include/asm/kvm_970_asm.h new file +3 -0
+	ori	\reg, r13, 1		/* r3 = PACA | 1 */
+	mtspr	SPRN_SPRG3, \reg	/* SPRG3 =PACA | 1 */
+.endm

Fun :-) So you set the PACA low bit to indicate you are running in the guest ? That's a pretty good way to do it I suppose.

Yeah, this is about the most clever way to do it I found. MOL stores a magic value in an SPRG IIRC, which I did too at first, but then I run out of free SPRGs in the handler code.

Quoted 24 lines
Quoted 24 lines
arch/powerpc/include/asm/kvm_970_asm.h new file +23 -0
+#define HOST_STACK_R1	(0 * ULONG_SIZE)
+/* We need to keep some space here that the C function */
+/* we jump into later can clobber */
+#define HOST_STACK_LR	(8 * ULONG_SIZE)
+#define HOST_STACK_RUN	(9 * ULONG_SIZE)
+#define HOST_STACK_VCPU	(10 * ULONG_SIZE)
+#define HOST_STACK_R14	(14 * ULONG_SIZE)
+#define HOST_STACK_R15	(15 * ULONG_SIZE)
+#define HOST_STACK_R16	(16 * ULONG_SIZE)
+#define HOST_STACK_R17	(17 * ULONG_SIZE)
+#define HOST_STACK_R18	(18 * ULONG_SIZE)
+#define HOST_STACK_R19	(19 * ULONG_SIZE)
+#define HOST_STACK_R20	(20 * ULONG_SIZE)
+#define HOST_STACK_R21	(21 * ULONG_SIZE)
+#define HOST_STACK_R22	(22 * ULONG_SIZE)
+#define HOST_STACK_R23	(23 * ULONG_SIZE)
+#define HOST_STACK_R24	(24 * ULONG_SIZE)
+#define HOST_STACK_R25	(25 * ULONG_SIZE)
+#define HOST_STACK_R26	(26 * ULONG_SIZE)
+#define HOST_STACK_R27	(27 * ULONG_SIZE)
+#define HOST_STACK_R28	(28 * ULONG_SIZE)
+#define HOST_STACK_R29	(29 * ULONG_SIZE)
+#define HOST_STACK_R30	(30 * ULONG_SIZE)
Quoted 8 lines
Quoted 5 lines
arch/powerpc/include/asm/kvm_970_asm.h new file +3 -0
+#define HOST_STACK_R31	(31 * ULONG_SIZE)
+#define HOST_STACK_MIN_SIZE (HOST_STACK_R31 + ULONG_SIZE)
+#define HOST_STACK_SIZE (((HOST_STACK_MIN_SIZE + 15) / 16) * 16) / 

* Align. */
+#define VCPU_GPR(n) (VCPU_GPRS + (n * ULONG_SIZE))

Can't you define a C structure and generate offsets in asm-offsets.h using asm-offsets.c like we do for most other stuff ?

You define the stack in a struct? I also don't see why I should have 31 single #defines when all I really need is simply packed in an easy algorithm.

Basically VCPU_GPR points to an index of a C defined array.

Alex

#3 lore raw
In reply to Alexander Graf
Quoted 25 lines · Benjamin Herrenschmidt
Quoted 23 lines · Alexander Graf
arch/powerpc/include/asm/kvm_970_asm.h new file +23 -0
+#define HOST_STACK_R1	(0 * ULONG_SIZE)
+/* We need to keep some space here that the C function */
+/* we jump into later can clobber */
+#define HOST_STACK_LR	(8 * ULONG_SIZE)
+#define HOST_STACK_RUN	(9 * ULONG_SIZE)
+#define HOST_STACK_VCPU	(10 * ULONG_SIZE)
+#define HOST_STACK_R14	(14 * ULONG_SIZE)
+#define HOST_STACK_R15	(15 * ULONG_SIZE)
+#define HOST_STACK_R16	(16 * ULONG_SIZE)
+#define HOST_STACK_R17	(17 * ULONG_SIZE)
+#define HOST_STACK_R18	(18 * ULONG_SIZE)
+#define HOST_STACK_R19	(19 * ULONG_SIZE)
+#define HOST_STACK_R20	(20 * ULONG_SIZE)
+#define HOST_STACK_R21	(21 * ULONG_SIZE)
+#define HOST_STACK_R22	(22 * ULONG_SIZE)
+#define HOST_STACK_R23	(23 * ULONG_SIZE)
+#define HOST_STACK_R24	(24 * ULONG_SIZE)
+#define HOST_STACK_R25	(25 * ULONG_SIZE)
+#define HOST_STACK_R26	(26 * ULONG_SIZE)
+#define HOST_STACK_R27	(27 * ULONG_SIZE)
+#define HOST_STACK_R28	(28 * ULONG_SIZE)
+#define HOST_STACK_R29	(29 * ULONG_SIZE)
+#define HOST_STACK_R30	(30 * ULONG_SIZE)
Quoted 8 lines
Quoted 5 lines
arch/powerpc/include/asm/kvm_970_asm.h new file +3 -0
+#define HOST_STACK_R31	(31 * ULONG_SIZE)
+#define HOST_STACK_MIN_SIZE (HOST_STACK_R31 + ULONG_SIZE)
+#define HOST_STACK_SIZE (((HOST_STACK_MIN_SIZE + 15) / 16) * 16) / 

* Align. */
+#define VCPU_GPR(n) (VCPU_GPRS + (n * ULONG_SIZE))

Can't you define a C structure and generate offsets in asm-offsets.h using asm-offsets.c like we do for most other stuff ?

Oh looking at asm-offsets.c you already do define a pt_regs structure on stack. I guess I could reuse that.

Alex

#4 lore raw
In reply to Alexander Graf
Quoted 7 lines · Alexander Graf

You define the stack in a struct? I also don't see why I should have 31 single #defines when all I really need is simply packed in an easy algorithm.

Basically VCPU_GPR points to an index of a C defined array.

Ok, so the stuff there is just a stack frame ? fair enough.

Cheers,
Ben.

#5 lore raw
In reply to Alexander Graf
Quoted 7 lines · Alexander Graf
Quoted 2 lines

Can't you define a C structure and generate offsets in asm-offsets.h using asm-offsets.c like we do for most other stuff ?

Oh looking at asm-offsets.c you already do define a pt_regs structure
on stack. I guess I could reuse that.

Yes. Beware that it also includes STACK_FRAME_OVERHEAD so you need to take that into account.

Cheers,
Ben.