blob: 5ee66edd749df59481f567f0a335f835b0df65e8 [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>
Paul Mackerras177339d2011-07-23 17:41:11 +100023#include <asm/mmu.h>
Alexander Grafc8621252009-10-30 05:47:09 +000024#include <asm/page.h>
25#include <asm/asm-offsets.h>
Alexander Graf8c3a4e02010-04-16 00:11:46 +020026
27#ifdef CONFIG_PPC_BOOK3S_64
Alexander Grafc8621252009-10-30 05:47:09 +000028#include <asm/exception-64s.h>
Alexander Graf8c3a4e02010-04-16 00:11:46 +020029#endif
Alexander Grafc8621252009-10-30 05:47:09 +000030
31/*****************************************************************************
32 * *
33 * Real Mode handlers that need to be in low physical memory *
34 * *
35 ****************************************************************************/
36
Alexander Graf8c3a4e02010-04-16 00:11:46 +020037#if defined(CONFIG_PPC_BOOK3S_64)
38
Benjamin Herrenschmidt2dd60d72011-01-20 17:50:21 +110039#define LOAD_SHADOW_VCPU(reg) GET_PACA(reg)
Alexander Graf8c3a4e02010-04-16 00:11:46 +020040#define MSR_NOIRQ MSR_KERNEL & ~(MSR_IR | MSR_DR)
41#define FUNC(name) GLUE(.,name)
42
Paul Mackerras177339d2011-07-23 17:41:11 +100043 .globl kvmppc_skip_interrupt
Paul Mackerrasb01c8b52011-06-29 00:18:26 +000044kvmppc_skip_interrupt:
45 /*
46 * Here all GPRs are unchanged from when the interrupt happened
47 * except for r13, which is saved in SPRG_SCRATCH0.
48 */
49 mfspr r13, SPRN_SRR0
50 addi r13, r13, 4
51 mtspr SPRN_SRR0, r13
52 GET_SCRATCH0(r13)
53 rfid
54 b .
Alexander Graf8c3a4e02010-04-16 00:11:46 +020055
Paul Mackerras177339d2011-07-23 17:41:11 +100056 .globl kvmppc_skip_Hinterrupt
Paul Mackerrasb01c8b52011-06-29 00:18:26 +000057kvmppc_skip_Hinterrupt:
58 /*
59 * Here all GPRs are unchanged from when the interrupt happened
60 * except for r13, which is saved in SPRG_SCRATCH0.
61 */
62 mfspr r13, SPRN_HSRR0
63 addi r13, r13, 4
64 mtspr SPRN_HSRR0, r13
65 GET_SCRATCH0(r13)
66 hrfid
67 b .
68
69#elif defined(CONFIG_PPC_BOOK3S_32)
Alexander Graf8c3a4e02010-04-16 00:11:46 +020070
Alexander Graf8c3a4e02010-04-16 00:11:46 +020071#define MSR_NOIRQ MSR_KERNEL
72#define FUNC(name) name
73
Alexander Grafc8621252009-10-30 05:47:09 +000074.macro INTERRUPT_TRAMPOLINE intno
75
76.global kvmppc_trampoline_\intno
77kvmppc_trampoline_\intno:
78
Paul Mackerrasb01c8b52011-06-29 00:18:26 +000079 mtspr SPRN_SPRG_SCRATCH0, r13 /* Save r13 */
Alexander Grafc8621252009-10-30 05:47:09 +000080
81 /*
82 * First thing to do is to find out if we're coming
83 * from a KVM guest or a Linux process.
84 *
Alexander Graf8c3a4e02010-04-16 00:11:46 +020085 * To distinguish, we check a magic byte in the PACA/current
Alexander Grafc8621252009-10-30 05:47:09 +000086 */
Paul Mackerrasb01c8b52011-06-29 00:18:26 +000087 mfspr r13, SPRN_SPRG_THREAD
88 lwz r13, THREAD_KVM_SVCPU(r13)
89 /* PPC32 can have a NULL pointer - let's check for that */
90 mtspr SPRN_SPRG_SCRATCH1, r12 /* Save r12 */
Alexander Grafc8621252009-10-30 05:47:09 +000091 mfcr r12
Paul Mackerrasb01c8b52011-06-29 00:18:26 +000092 cmpwi r13, 0
93 bne 1f
942: mtcr r12
95 mfspr r12, SPRN_SPRG_SCRATCH1
96 mfspr r13, SPRN_SPRG_SCRATCH0 /* r13 = original r13 */
97 b kvmppc_resume_\intno /* Get back original handler */
98
991: tophys(r13, r13)
Paul Mackerras3c42bf82011-06-29 00:20:58 +0000100 stw r12, HSTATE_SCRATCH1(r13)
Paul Mackerrasb01c8b52011-06-29 00:18:26 +0000101 mfspr r12, SPRN_SPRG_SCRATCH1
Paul Mackerras3c42bf82011-06-29 00:20:58 +0000102 stw r12, HSTATE_SCRATCH0(r13)
103 lbz r12, HSTATE_IN_GUEST(r13)
Alexander Grafb4433a72010-01-08 02:58:04 +0100104 cmpwi r12, KVM_GUEST_MODE_NONE
Alexander Grafc8621252009-10-30 05:47:09 +0000105 bne ..kvmppc_handler_hasmagic_\intno
106 /* No KVM guest? Then jump back to the Linux handler! */
Paul Mackerras3c42bf82011-06-29 00:20:58 +0000107 lwz r12, HSTATE_SCRATCH1(r13)
Paul Mackerrasb01c8b52011-06-29 00:18:26 +0000108 b 2b
Alexander Grafc8621252009-10-30 05:47:09 +0000109
110 /* Now we know we're handling a KVM guest */
111..kvmppc_handler_hasmagic_\intno:
Alexander Grafb4433a72010-01-08 02:58:04 +0100112
113 /* Should we just skip the faulting instruction? */
114 cmpwi r12, KVM_GUEST_MODE_SKIP
115 beq kvmppc_handler_skip_ins
116
Alexander Grafc8621252009-10-30 05:47:09 +0000117 /* Let's store which interrupt we're handling */
118 li r12, \intno
119
120 /* Jump into the SLB exit code that goes to the highmem handler */
121 b kvmppc_handler_trampoline_exit
122
123.endm
124
125INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_SYSTEM_RESET
126INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_MACHINE_CHECK
127INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_DATA_STORAGE
Alexander Grafc8621252009-10-30 05:47:09 +0000128INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_INST_STORAGE
Alexander Grafc8621252009-10-30 05:47:09 +0000129INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_EXTERNAL
130INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_ALIGNMENT
131INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_PROGRAM
132INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_FP_UNAVAIL
133INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_DECREMENTER
134INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_SYSCALL
135INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_TRACE
136INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_PERFMON
137INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_ALTIVEC
Alexander Graf8c3a4e02010-04-16 00:11:46 +0200138
Alexander Grafc8621252009-10-30 05:47:09 +0000139/*
Alexander Grafb4433a72010-01-08 02:58:04 +0100140 * Bring us back to the faulting code, but skip the
141 * faulting instruction.
142 *
143 * This is a generic exit path from the interrupt
144 * trampolines above.
145 *
146 * Input Registers:
147 *
Alexander Graf8c3a4e02010-04-16 00:11:46 +0200148 * R12 = free
149 * R13 = Shadow VCPU (PACA)
Paul Mackerras3c42bf82011-06-29 00:20:58 +0000150 * HSTATE.SCRATCH0 = guest R12
151 * HSTATE.SCRATCH1 = guest CR
Alexander Graf8c3a4e02010-04-16 00:11:46 +0200152 * SPRG_SCRATCH0 = guest R13
Alexander Grafb4433a72010-01-08 02:58:04 +0100153 *
154 */
155kvmppc_handler_skip_ins:
156
157 /* Patch the IP to the next instruction */
158 mfsrr0 r12
159 addi r12, r12, 4
160 mtsrr0 r12
161
162 /* Clean up all state */
Paul Mackerras3c42bf82011-06-29 00:20:58 +0000163 lwz r12, HSTATE_SCRATCH1(r13)
Alexander Grafb4433a72010-01-08 02:58:04 +0100164 mtcr r12
Paul Mackerras3c42bf82011-06-29 00:20:58 +0000165 PPC_LL r12, HSTATE_SCRATCH0(r13)
Paul Mackerras673b1892011-04-05 13:59:58 +1000166 GET_SCRATCH0(r13)
Alexander Grafb4433a72010-01-08 02:58:04 +0100167
168 /* And get back into the code */
169 RFI
Paul Mackerrasb01c8b52011-06-29 00:18:26 +0000170#endif
Alexander Grafb4433a72010-01-08 02:58:04 +0100171
172/*
Alexander Grafc8621252009-10-30 05:47:09 +0000173 * This trampoline brings us back to a real mode handler
174 *
175 * Input Registers:
176 *
Alexander Graf7e57cba2010-01-08 02:58:03 +0100177 * R5 = SRR0
178 * R6 = SRR1
Alexander Grafc8621252009-10-30 05:47:09 +0000179 * LR = real-mode IP
180 *
181 */
182.global kvmppc_handler_lowmem_trampoline
183kvmppc_handler_lowmem_trampoline:
184
Alexander Graf7e57cba2010-01-08 02:58:03 +0100185 mtsrr0 r5
186 mtsrr1 r6
Alexander Grafc8621252009-10-30 05:47:09 +0000187 blr
188kvmppc_handler_lowmem_trampoline_end:
189
Alexander Graf021ec9c2010-01-08 02:58:06 +0100190/*
191 * Call a function in real mode
192 *
193 * Input Registers:
194 *
195 * R3 = function
196 * R4 = MSR
Alexander Graf8c3a4e02010-04-16 00:11:46 +0200197 * R5 = scratch register
Alexander Graf021ec9c2010-01-08 02:58:06 +0100198 *
199 */
200_GLOBAL(kvmppc_rmcall)
Alexander Graf8c3a4e02010-04-16 00:11:46 +0200201 LOAD_REG_IMMEDIATE(r5, MSR_NOIRQ)
202 mtmsr r5 /* Disable relocation and interrupts, so mtsrr
Alexander Graf021ec9c2010-01-08 02:58:06 +0100203 doesn't get interrupted */
Alexander Graf8c3a4e02010-04-16 00:11:46 +0200204 sync
Alexander Graf021ec9c2010-01-08 02:58:06 +0100205 mtsrr0 r3
206 mtsrr1 r4
207 RFI
208
Alexander Graf8c3a4e02010-04-16 00:11:46 +0200209#if defined(CONFIG_PPC_BOOK3S_32)
210#define STACK_LR INT_FRAME_SIZE+4
Alexander Graf0e677902010-07-29 15:04:20 +0200211
212/* load_up_xxx have to run with MSR_DR=0 on Book3S_32 */
213#define MSR_EXT_START \
214 PPC_STL r20, _NIP(r1); \
215 mfmsr r20; \
216 LOAD_REG_IMMEDIATE(r3, MSR_DR|MSR_EE); \
217 andc r3,r20,r3; /* Disable DR,EE */ \
218 mtmsr r3; \
219 sync
220
221#define MSR_EXT_END \
222 mtmsr r20; /* Enable DR,EE */ \
223 sync; \
224 PPC_LL r20, _NIP(r1)
225
Alexander Graf8c3a4e02010-04-16 00:11:46 +0200226#elif defined(CONFIG_PPC_BOOK3S_64)
227#define STACK_LR _LINK
Alexander Graf0e677902010-07-29 15:04:20 +0200228#define MSR_EXT_START
229#define MSR_EXT_END
Alexander Graf8c3a4e02010-04-16 00:11:46 +0200230#endif
231
Alexander Grafd5e52812010-01-15 14:49:10 +0100232/*
233 * Activate current's external feature (FPU/Altivec/VSX)
234 */
Alexander Graf8c3a4e02010-04-16 00:11:46 +0200235#define define_load_up(what) \
236 \
237_GLOBAL(kvmppc_load_up_ ## what); \
238 PPC_STLU r1, -INT_FRAME_SIZE(r1); \
239 mflr r3; \
240 PPC_STL r3, STACK_LR(r1); \
Alexander Graf0e677902010-07-29 15:04:20 +0200241 MSR_EXT_START; \
Alexander Graf8c3a4e02010-04-16 00:11:46 +0200242 \
243 bl FUNC(load_up_ ## what); \
244 \
Alexander Graf0e677902010-07-29 15:04:20 +0200245 MSR_EXT_END; \
Alexander Graf8c3a4e02010-04-16 00:11:46 +0200246 PPC_LL r3, STACK_LR(r1); \
Alexander Graf8c3a4e02010-04-16 00:11:46 +0200247 mtlr r3; \
248 addi r1, r1, INT_FRAME_SIZE; \
Alexander Grafd5e52812010-01-15 14:49:10 +0100249 blr
250
251define_load_up(fpu)
252#ifdef CONFIG_ALTIVEC
253define_load_up(altivec)
254#endif
255#ifdef CONFIG_VSX
256define_load_up(vsx)
257#endif
258
Alexander Graf53e5b8b2010-04-16 00:11:48 +0200259#include "book3s_segment.S"