blob: 5f5d07475c9154c36cf7d3b124e6ddf839e635fb [file] [log] [blame]
Alexander Grafc8621252009-10-30 05:47:09 +00001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2, as
4 * published by the Free Software Foundation.
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
14 *
15 * Copyright SUSE Linux Products GmbH 2009
16 *
17 * Authors: Alexander Graf <agraf@suse.de>
18 */
19
20#include <asm/ppc_asm.h>
21#include <asm/kvm_asm.h>
22#include <asm/reg.h>
23#include <asm/page.h>
24#include <asm/asm-offsets.h>
Alexander Graf8c3a4e02010-04-16 00:11:46 +020025
26#ifdef CONFIG_PPC_BOOK3S_64
Alexander Grafc8621252009-10-30 05:47:09 +000027#include <asm/exception-64s.h>
Alexander Graf8c3a4e02010-04-16 00:11:46 +020028#endif
Alexander Grafc8621252009-10-30 05:47:09 +000029
30/*****************************************************************************
31 * *
32 * Real Mode handlers that need to be in low physical memory *
33 * *
34 ****************************************************************************/
35
Alexander Graf8c3a4e02010-04-16 00:11:46 +020036#if defined(CONFIG_PPC_BOOK3S_64)
37
Benjamin Herrenschmidt2dd60d72011-01-20 17:50:21 +110038#define LOAD_SHADOW_VCPU(reg) GET_PACA(reg)
Alexander Graf8c3a4e02010-04-16 00:11:46 +020039#define SHADOW_VCPU_OFF PACA_KVM_SVCPU
40#define MSR_NOIRQ MSR_KERNEL & ~(MSR_IR | MSR_DR)
41#define FUNC(name) GLUE(.,name)
42
43#elif defined(CONFIG_PPC_BOOK3S_32)
44
45#define LOAD_SHADOW_VCPU(reg) \
46 mfspr reg, SPRN_SPRG_THREAD; \
47 lwz reg, THREAD_KVM_SVCPU(reg); \
48 /* PPC32 can have a NULL pointer - let's check for that */ \
49 mtspr SPRN_SPRG_SCRATCH1, r12; /* Save r12 */ \
50 mfcr r12; \
51 cmpwi reg, 0; \
52 bne 1f; \
53 mfspr reg, SPRN_SPRG_SCRATCH0; \
54 mtcr r12; \
55 mfspr r12, SPRN_SPRG_SCRATCH1; \
56 b kvmppc_resume_\intno; \
571:; \
58 mtcr r12; \
59 mfspr r12, SPRN_SPRG_SCRATCH1; \
60 tophys(reg, reg)
61
62#define SHADOW_VCPU_OFF 0
63#define MSR_NOIRQ MSR_KERNEL
64#define FUNC(name) name
65
66#endif
Alexander Grafc8621252009-10-30 05:47:09 +000067
68.macro INTERRUPT_TRAMPOLINE intno
69
70.global kvmppc_trampoline_\intno
71kvmppc_trampoline_\intno:
72
Paul Mackerras673b1892011-04-05 13:59:58 +100073 SET_SCRATCH0(r13) /* Save r13 */
Alexander Grafc8621252009-10-30 05:47:09 +000074
75 /*
76 * First thing to do is to find out if we're coming
77 * from a KVM guest or a Linux process.
78 *
Alexander Graf8c3a4e02010-04-16 00:11:46 +020079 * To distinguish, we check a magic byte in the PACA/current
Alexander Grafc8621252009-10-30 05:47:09 +000080 */
Alexander Graf8c3a4e02010-04-16 00:11:46 +020081 LOAD_SHADOW_VCPU(r13)
82 PPC_STL r12, (SHADOW_VCPU_OFF + SVCPU_SCRATCH0)(r13)
Alexander Grafc8621252009-10-30 05:47:09 +000083 mfcr r12
Alexander Graf8c3a4e02010-04-16 00:11:46 +020084 stw r12, (SHADOW_VCPU_OFF + SVCPU_SCRATCH1)(r13)
85 lbz r12, (SHADOW_VCPU_OFF + SVCPU_IN_GUEST)(r13)
Alexander Grafb4433a72010-01-08 02:58:04 +010086 cmpwi r12, KVM_GUEST_MODE_NONE
Alexander Grafc8621252009-10-30 05:47:09 +000087 bne ..kvmppc_handler_hasmagic_\intno
88 /* No KVM guest? Then jump back to the Linux handler! */
Alexander Graf8c3a4e02010-04-16 00:11:46 +020089 lwz r12, (SHADOW_VCPU_OFF + SVCPU_SCRATCH1)(r13)
Alexander Grafc8621252009-10-30 05:47:09 +000090 mtcr r12
Alexander Graf8c3a4e02010-04-16 00:11:46 +020091 PPC_LL r12, (SHADOW_VCPU_OFF + SVCPU_SCRATCH0)(r13)
Paul Mackerras673b1892011-04-05 13:59:58 +100092 GET_SCRATCH0(r13) /* r13 = original r13 */
Alexander Grafc8621252009-10-30 05:47:09 +000093 b kvmppc_resume_\intno /* Get back original handler */
94
95 /* Now we know we're handling a KVM guest */
96..kvmppc_handler_hasmagic_\intno:
Alexander Grafb4433a72010-01-08 02:58:04 +010097
98 /* Should we just skip the faulting instruction? */
99 cmpwi r12, KVM_GUEST_MODE_SKIP
100 beq kvmppc_handler_skip_ins
101
Alexander Grafc8621252009-10-30 05:47:09 +0000102 /* Let's store which interrupt we're handling */
103 li r12, \intno
104
105 /* Jump into the SLB exit code that goes to the highmem handler */
106 b kvmppc_handler_trampoline_exit
107
108.endm
109
110INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_SYSTEM_RESET
111INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_MACHINE_CHECK
112INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_DATA_STORAGE
Alexander Grafc8621252009-10-30 05:47:09 +0000113INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_INST_STORAGE
Alexander Grafc8621252009-10-30 05:47:09 +0000114INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_EXTERNAL
Paul Mackerras593adf32011-05-11 00:39:50 +0000115#ifdef CONFIG_PPC_BOOK3S_64
Benjamin Herrenschmidta5d4f3a2011-04-05 14:20:31 +1000116INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_EXTERNAL_HV
Paul Mackerras593adf32011-05-11 00:39:50 +0000117#endif
Alexander Grafc8621252009-10-30 05:47:09 +0000118INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_ALIGNMENT
119INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_PROGRAM
120INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_FP_UNAVAIL
121INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_DECREMENTER
122INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_SYSCALL
123INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_TRACE
124INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_PERFMON
125INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_ALTIVEC
Alexander Graf8c3a4e02010-04-16 00:11:46 +0200126
127/* Those are only available on 64 bit machines */
128
129#ifdef CONFIG_PPC_BOOK3S_64
130INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_DATA_SEGMENT
131INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_INST_SEGMENT
Alexander Grafc8621252009-10-30 05:47:09 +0000132INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_VSX
Alexander Graf8c3a4e02010-04-16 00:11:46 +0200133#endif
Alexander Grafc8621252009-10-30 05:47:09 +0000134
135/*
Alexander Grafb4433a72010-01-08 02:58:04 +0100136 * Bring us back to the faulting code, but skip the
137 * faulting instruction.
138 *
139 * This is a generic exit path from the interrupt
140 * trampolines above.
141 *
142 * Input Registers:
143 *
Alexander Graf8c3a4e02010-04-16 00:11:46 +0200144 * R12 = free
145 * R13 = Shadow VCPU (PACA)
146 * SVCPU.SCRATCH0 = guest R12
147 * SVCPU.SCRATCH1 = guest CR
148 * SPRG_SCRATCH0 = guest R13
Alexander Grafb4433a72010-01-08 02:58:04 +0100149 *
150 */
151kvmppc_handler_skip_ins:
152
153 /* Patch the IP to the next instruction */
154 mfsrr0 r12
155 addi r12, r12, 4
156 mtsrr0 r12
157
158 /* Clean up all state */
Alexander Graf8c3a4e02010-04-16 00:11:46 +0200159 lwz r12, (SHADOW_VCPU_OFF + SVCPU_SCRATCH1)(r13)
Alexander Grafb4433a72010-01-08 02:58:04 +0100160 mtcr r12
Alexander Graf8c3a4e02010-04-16 00:11:46 +0200161 PPC_LL r12, (SHADOW_VCPU_OFF + SVCPU_SCRATCH0)(r13)
Paul Mackerras673b1892011-04-05 13:59:58 +1000162 GET_SCRATCH0(r13)
Alexander Grafb4433a72010-01-08 02:58:04 +0100163
164 /* And get back into the code */
165 RFI
166
167/*
Alexander Grafc8621252009-10-30 05:47:09 +0000168 * This trampoline brings us back to a real mode handler
169 *
170 * Input Registers:
171 *
Alexander Graf7e57cba2010-01-08 02:58:03 +0100172 * R5 = SRR0
173 * R6 = SRR1
Alexander Grafc8621252009-10-30 05:47:09 +0000174 * LR = real-mode IP
175 *
176 */
177.global kvmppc_handler_lowmem_trampoline
178kvmppc_handler_lowmem_trampoline:
179
Alexander Graf7e57cba2010-01-08 02:58:03 +0100180 mtsrr0 r5
181 mtsrr1 r6
Alexander Grafc8621252009-10-30 05:47:09 +0000182 blr
183kvmppc_handler_lowmem_trampoline_end:
184
Alexander Graf021ec9c2010-01-08 02:58:06 +0100185/*
186 * Call a function in real mode
187 *
188 * Input Registers:
189 *
190 * R3 = function
191 * R4 = MSR
Alexander Graf8c3a4e02010-04-16 00:11:46 +0200192 * R5 = scratch register
Alexander Graf021ec9c2010-01-08 02:58:06 +0100193 *
194 */
195_GLOBAL(kvmppc_rmcall)
Alexander Graf8c3a4e02010-04-16 00:11:46 +0200196 LOAD_REG_IMMEDIATE(r5, MSR_NOIRQ)
197 mtmsr r5 /* Disable relocation and interrupts, so mtsrr
Alexander Graf021ec9c2010-01-08 02:58:06 +0100198 doesn't get interrupted */
Alexander Graf8c3a4e02010-04-16 00:11:46 +0200199 sync
Alexander Graf021ec9c2010-01-08 02:58:06 +0100200 mtsrr0 r3
201 mtsrr1 r4
202 RFI
203
Alexander Graf8c3a4e02010-04-16 00:11:46 +0200204#if defined(CONFIG_PPC_BOOK3S_32)
205#define STACK_LR INT_FRAME_SIZE+4
Alexander Graf0e677902010-07-29 15:04:20 +0200206
207/* load_up_xxx have to run with MSR_DR=0 on Book3S_32 */
208#define MSR_EXT_START \
209 PPC_STL r20, _NIP(r1); \
210 mfmsr r20; \
211 LOAD_REG_IMMEDIATE(r3, MSR_DR|MSR_EE); \
212 andc r3,r20,r3; /* Disable DR,EE */ \
213 mtmsr r3; \
214 sync
215
216#define MSR_EXT_END \
217 mtmsr r20; /* Enable DR,EE */ \
218 sync; \
219 PPC_LL r20, _NIP(r1)
220
Alexander Graf8c3a4e02010-04-16 00:11:46 +0200221#elif defined(CONFIG_PPC_BOOK3S_64)
222#define STACK_LR _LINK
Alexander Graf0e677902010-07-29 15:04:20 +0200223#define MSR_EXT_START
224#define MSR_EXT_END
Alexander Graf8c3a4e02010-04-16 00:11:46 +0200225#endif
226
Alexander Grafd5e52812010-01-15 14:49:10 +0100227/*
228 * Activate current's external feature (FPU/Altivec/VSX)
229 */
Alexander Graf8c3a4e02010-04-16 00:11:46 +0200230#define define_load_up(what) \
231 \
232_GLOBAL(kvmppc_load_up_ ## what); \
233 PPC_STLU r1, -INT_FRAME_SIZE(r1); \
234 mflr r3; \
235 PPC_STL r3, STACK_LR(r1); \
Alexander Graf0e677902010-07-29 15:04:20 +0200236 MSR_EXT_START; \
Alexander Graf8c3a4e02010-04-16 00:11:46 +0200237 \
238 bl FUNC(load_up_ ## what); \
239 \
Alexander Graf0e677902010-07-29 15:04:20 +0200240 MSR_EXT_END; \
Alexander Graf8c3a4e02010-04-16 00:11:46 +0200241 PPC_LL r3, STACK_LR(r1); \
Alexander Graf8c3a4e02010-04-16 00:11:46 +0200242 mtlr r3; \
243 addi r1, r1, INT_FRAME_SIZE; \
Alexander Grafd5e52812010-01-15 14:49:10 +0100244 blr
245
246define_load_up(fpu)
247#ifdef CONFIG_ALTIVEC
248define_load_up(altivec)
249#endif
250#ifdef CONFIG_VSX
251define_load_up(vsx)
252#endif
253
Alexander Graf53e5b8b2010-04-16 00:11:48 +0200254#include "book3s_segment.S"