blob: 6bae0a9414ba02ceba80c472f38b5ce894a87cad [file] [log] [blame]
Alexander Graf07372792010-04-16 00:11:35 +02001/*
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 2010
16 *
17 * Authors: Alexander Graf <agraf@suse.de>
18 */
19
20/* Real mode helpers */
21
22#if defined(CONFIG_PPC_BOOK3S_64)
23
24#define GET_SHADOW_VCPU(reg) \
Paul Mackerras3c42bf82011-06-29 00:20:58 +000025 mr reg, r13
Paul Mackerras02143942011-07-23 17:41:44 +100026#define MTMSR_EERI(reg) mtmsrd (reg),1
Alexander Graf07372792010-04-16 00:11:35 +020027
28#elif defined(CONFIG_PPC_BOOK3S_32)
29
30#define GET_SHADOW_VCPU(reg) \
31 tophys(reg, r2); \
32 lwz reg, (THREAD + THREAD_KVM_SVCPU)(reg); \
33 tophys(reg, reg)
Paul Mackerras02143942011-07-23 17:41:44 +100034#define MTMSR_EERI(reg) mtmsr (reg)
Alexander Graf07372792010-04-16 00:11:35 +020035
36#endif
37
38/* Disable for nested KVM */
39#define USE_QUICK_LAST_INST
40
41
42/* Get helper functions for subarch specific functionality */
43
44#if defined(CONFIG_PPC_BOOK3S_64)
45#include "book3s_64_slb.S"
46#elif defined(CONFIG_PPC_BOOK3S_32)
47#include "book3s_32_sr.S"
48#endif
49
50/******************************************************************************
51 * *
52 * Entry code *
53 * *
54 *****************************************************************************/
55
56.global kvmppc_handler_trampoline_enter
57kvmppc_handler_trampoline_enter:
58
59 /* Required state:
60 *
61 * MSR = ~IR|DR
Alexander Graf07372792010-04-16 00:11:35 +020062 * R1 = host R1
63 * R2 = host R2
Paul Mackerras02143942011-07-23 17:41:44 +100064 * R4 = guest shadow MSR
65 * R5 = normal host MSR
66 * R6 = current host MSR (EE, IR, DR off)
67 * LR = highmem guest exit code
Alexander Graf07372792010-04-16 00:11:35 +020068 * all other volatile GPRS = free
69 * SVCPU[CR] = guest CR
70 * SVCPU[XER] = guest XER
71 * SVCPU[CTR] = guest CTR
72 * SVCPU[LR] = guest LR
73 */
74
75 /* r3 = shadow vcpu */
76 GET_SHADOW_VCPU(r3)
77
Paul Mackerras02143942011-07-23 17:41:44 +100078 /* Save guest exit handler address and MSR */
79 mflr r0
80 PPC_STL r0, HSTATE_VMHANDLER(r3)
81 PPC_STL r5, HSTATE_HOST_MSR(r3)
82
Paul Mackerras3c42bf82011-06-29 00:20:58 +000083 /* Save R1/R2 in the PACA (64-bit) or shadow_vcpu (32-bit) */
84 PPC_STL r1, HSTATE_HOST_R1(r3)
85 PPC_STL r2, HSTATE_HOST_R2(r3)
86
Alexander Graf07372792010-04-16 00:11:35 +020087 /* Activate guest mode, so faults get handled by KVM */
88 li r11, KVM_GUEST_MODE_GUEST
Paul Mackerras3c42bf82011-06-29 00:20:58 +000089 stb r11, HSTATE_IN_GUEST(r3)
Alexander Graf07372792010-04-16 00:11:35 +020090
91 /* Switch to guest segment. This is subarch specific. */
92 LOAD_GUEST_SEGMENTS
93
Paul Mackerras02143942011-07-23 17:41:44 +100094#ifdef CONFIG_PPC_BOOK3S_64
95 /* Some guests may need to have dcbz set to 32 byte length.
96 *
97 * Usually we ensure that by patching the guest's instructions
98 * to trap on dcbz and emulate it in the hypervisor.
99 *
100 * If we can, we should tell the CPU to use 32 byte dcbz though,
101 * because that's a lot faster.
102 */
103 lbz r0, HSTATE_RESTORE_HID5(r3)
104 cmpwi r0, 0
105 beq no_dcbz32_on
106
107 mfspr r0,SPRN_HID5
108 ori r0, r0, 0x80 /* XXX HID5_dcbz32 = 0x80 */
109 mtspr SPRN_HID5,r0
110no_dcbz32_on:
111
112#endif /* CONFIG_PPC_BOOK3S_64 */
113
Alexander Graf07372792010-04-16 00:11:35 +0200114 /* Enter guest */
115
Paul Mackerras02143942011-07-23 17:41:44 +1000116 PPC_LL r8, SVCPU_CTR(r3)
117 PPC_LL r9, SVCPU_LR(r3)
118 lwz r10, SVCPU_CR(r3)
119 lwz r11, SVCPU_XER(r3)
Alexander Graf07372792010-04-16 00:11:35 +0200120
Paul Mackerras02143942011-07-23 17:41:44 +1000121 mtctr r8
122 mtlr r9
123 mtcr r10
124 mtxer r11
125
126 /* Move SRR0 and SRR1 into the respective regs */
127 PPC_LL r9, SVCPU_PC(r3)
128 /* First clear RI in our current MSR value */
129 li r0, MSR_RI
130 andc r6, r6, r0
Alexander Graf07372792010-04-16 00:11:35 +0200131
Paul Mackerrasde56a942011-06-29 00:21:34 +0000132 PPC_LL r0, SVCPU_R0(r3)
133 PPC_LL r1, SVCPU_R1(r3)
134 PPC_LL r2, SVCPU_R2(r3)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000135 PPC_LL r5, SVCPU_R5(r3)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000136 PPC_LL r7, SVCPU_R7(r3)
137 PPC_LL r8, SVCPU_R8(r3)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000138 PPC_LL r10, SVCPU_R10(r3)
139 PPC_LL r11, SVCPU_R11(r3)
140 PPC_LL r12, SVCPU_R12(r3)
141 PPC_LL r13, SVCPU_R13(r3)
Alexander Graf07372792010-04-16 00:11:35 +0200142
Alexander Graf8c2d0be2012-04-25 14:28:23 +0200143 MTMSR_EERI(r6)
144 mtsrr0 r9
145 mtsrr1 r4
146
147 PPC_LL r4, SVCPU_R4(r3)
148 PPC_LL r6, SVCPU_R6(r3)
149 PPC_LL r9, SVCPU_R9(r3)
Alexander Graf07372792010-04-16 00:11:35 +0200150 PPC_LL r3, (SVCPU_R3)(r3)
151
152 RFI
153kvmppc_handler_trampoline_enter_end:
154
155
156
157/******************************************************************************
158 * *
159 * Exit code *
160 * *
161 *****************************************************************************/
162
163.global kvmppc_handler_trampoline_exit
164kvmppc_handler_trampoline_exit:
165
Paul Mackerrasb01c8b52011-06-29 00:18:26 +0000166.global kvmppc_interrupt
167kvmppc_interrupt:
168
Alexander Graf07372792010-04-16 00:11:35 +0200169 /* Register usage at this point:
170 *
171 * SPRG_SCRATCH0 = guest R13
172 * R12 = exit handler id
Paul Mackerras3c42bf82011-06-29 00:20:58 +0000173 * R13 = shadow vcpu (32-bit) or PACA (64-bit)
174 * HSTATE.SCRATCH0 = guest R12
175 * HSTATE.SCRATCH1 = guest CR
Alexander Graf07372792010-04-16 00:11:35 +0200176 *
177 */
178
179 /* Save registers */
180
Paul Mackerras3c42bf82011-06-29 00:20:58 +0000181 PPC_STL r0, SVCPU_R0(r13)
182 PPC_STL r1, SVCPU_R1(r13)
183 PPC_STL r2, SVCPU_R2(r13)
184 PPC_STL r3, SVCPU_R3(r13)
185 PPC_STL r4, SVCPU_R4(r13)
186 PPC_STL r5, SVCPU_R5(r13)
187 PPC_STL r6, SVCPU_R6(r13)
188 PPC_STL r7, SVCPU_R7(r13)
189 PPC_STL r8, SVCPU_R8(r13)
190 PPC_STL r9, SVCPU_R9(r13)
191 PPC_STL r10, SVCPU_R10(r13)
192 PPC_STL r11, SVCPU_R11(r13)
Alexander Graf07372792010-04-16 00:11:35 +0200193
194 /* Restore R1/R2 so we can handle faults */
Paul Mackerras3c42bf82011-06-29 00:20:58 +0000195 PPC_LL r1, HSTATE_HOST_R1(r13)
196 PPC_LL r2, HSTATE_HOST_R2(r13)
Alexander Graf07372792010-04-16 00:11:35 +0200197
198 /* Save guest PC and MSR */
Paul Mackerrasb01c8b52011-06-29 00:18:26 +0000199#ifdef CONFIG_PPC64
200BEGIN_FTR_SECTION
Benjamin Herrenschmidta5d4f3a2011-04-05 14:20:31 +1000201 andi. r0,r12,0x2
202 beq 1f
203 mfspr r3,SPRN_HSRR0
204 mfspr r4,SPRN_HSRR1
205 andi. r12,r12,0x3ffd
206 b 2f
Paul Mackerras969391c2011-06-29 00:26:11 +0000207END_FTR_SECTION_IFSET(CPU_FTR_HVMODE)
Paul Mackerrasb01c8b52011-06-29 00:18:26 +0000208#endif
Benjamin Herrenschmidta5d4f3a2011-04-05 14:20:31 +10002091: mfsrr0 r3
Alexander Graf07372792010-04-16 00:11:35 +0200210 mfsrr1 r4
Benjamin Herrenschmidta5d4f3a2011-04-05 14:20:31 +10002112:
Paul Mackerras3c42bf82011-06-29 00:20:58 +0000212 PPC_STL r3, SVCPU_PC(r13)
213 PPC_STL r4, SVCPU_SHADOW_SRR1(r13)
Alexander Graf07372792010-04-16 00:11:35 +0200214
215 /* Get scratch'ed off registers */
Paul Mackerras673b1892011-04-05 13:59:58 +1000216 GET_SCRATCH0(r9)
Paul Mackerras3c42bf82011-06-29 00:20:58 +0000217 PPC_LL r8, HSTATE_SCRATCH0(r13)
218 lwz r7, HSTATE_SCRATCH1(r13)
Alexander Graf07372792010-04-16 00:11:35 +0200219
Paul Mackerras3c42bf82011-06-29 00:20:58 +0000220 PPC_STL r9, SVCPU_R13(r13)
221 PPC_STL r8, SVCPU_R12(r13)
222 stw r7, SVCPU_CR(r13)
Alexander Graf07372792010-04-16 00:11:35 +0200223
224 /* Save more register state */
225
226 mfxer r5
227 mfdar r6
228 mfdsisr r7
229 mfctr r8
230 mflr r9
231
Paul Mackerras3c42bf82011-06-29 00:20:58 +0000232 stw r5, SVCPU_XER(r13)
233 PPC_STL r6, SVCPU_FAULT_DAR(r13)
234 stw r7, SVCPU_FAULT_DSISR(r13)
235 PPC_STL r8, SVCPU_CTR(r13)
236 PPC_STL r9, SVCPU_LR(r13)
Alexander Graf07372792010-04-16 00:11:35 +0200237
238 /*
239 * In order for us to easily get the last instruction,
240 * we got the #vmexit at, we exploit the fact that the
241 * virtual layout is still the same here, so we can just
242 * ld from the guest's PC address
243 */
244
245 /* We only load the last instruction when it's safe */
246 cmpwi r12, BOOK3S_INTERRUPT_DATA_STORAGE
247 beq ld_last_inst
248 cmpwi r12, BOOK3S_INTERRUPT_PROGRAM
249 beq ld_last_inst
Alexander Graf77e675a2011-08-08 16:11:36 +0200250 cmpwi r12, BOOK3S_INTERRUPT_SYSCALL
251 beq ld_last_prev_inst
Alexander Graf6fc55822010-04-20 02:49:49 +0200252 cmpwi r12, BOOK3S_INTERRUPT_ALIGNMENT
253 beq- ld_last_inst
Alexander Graf07372792010-04-16 00:11:35 +0200254
255 b no_ld_last_inst
256
Alexander Graf77e675a2011-08-08 16:11:36 +0200257ld_last_prev_inst:
258 addi r3, r3, -4
259
Alexander Graf07372792010-04-16 00:11:35 +0200260ld_last_inst:
261 /* Save off the guest instruction we're at */
262
263 /* In case lwz faults */
264 li r0, KVM_INST_FETCH_FAILED
265
266#ifdef USE_QUICK_LAST_INST
267
268 /* Set guest mode to 'jump over instruction' so if lwz faults
269 * we'll just continue at the next IP. */
270 li r9, KVM_GUEST_MODE_SKIP
Paul Mackerras3c42bf82011-06-29 00:20:58 +0000271 stb r9, HSTATE_IN_GUEST(r13)
Alexander Graf07372792010-04-16 00:11:35 +0200272
273 /* 1) enable paging for data */
274 mfmsr r9
275 ori r11, r9, MSR_DR /* Enable paging for data */
276 mtmsr r11
277 sync
278 /* 2) fetch the instruction */
279 lwz r0, 0(r3)
280 /* 3) disable paging again */
281 mtmsr r9
282 sync
283
284#endif
Paul Mackerras3c42bf82011-06-29 00:20:58 +0000285 stw r0, SVCPU_LAST_INST(r13)
Alexander Graf07372792010-04-16 00:11:35 +0200286
287no_ld_last_inst:
288
289 /* Unset guest mode */
290 li r9, KVM_GUEST_MODE_NONE
Paul Mackerras3c42bf82011-06-29 00:20:58 +0000291 stb r9, HSTATE_IN_GUEST(r13)
Alexander Graf07372792010-04-16 00:11:35 +0200292
293 /* Switch back to host MMU */
294 LOAD_HOST_SEGMENTS
295
Paul Mackerras02143942011-07-23 17:41:44 +1000296#ifdef CONFIG_PPC_BOOK3S_64
297
298 lbz r5, HSTATE_RESTORE_HID5(r13)
299 cmpwi r5, 0
300 beq no_dcbz32_off
301
302 li r4, 0
303 mfspr r5,SPRN_HID5
304 rldimi r5,r4,6,56
305 mtspr SPRN_HID5,r5
306
307no_dcbz32_off:
308
309#endif /* CONFIG_PPC_BOOK3S_64 */
310
311 /*
312 * For some interrupts, we need to call the real Linux
313 * handler, so it can do work for us. This has to happen
314 * as if the interrupt arrived from the kernel though,
315 * so let's fake it here where most state is restored.
316 *
317 * Having set up SRR0/1 with the address where we want
318 * to continue with relocation on (potentially in module
319 * space), we either just go straight there with rfi[d],
320 * or we jump to an interrupt handler with bctr if there
321 * is an interrupt to be handled first. In the latter
322 * case, the rfi[d] at the end of the interrupt handler
323 * will get us back to where we want to continue.
324 */
325
326 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
327 beq 1f
328 cmpwi r12, BOOK3S_INTERRUPT_DECREMENTER
329 beq 1f
330 cmpwi r12, BOOK3S_INTERRUPT_PERFMON
3311: mtctr r12
332
Alexander Graf07372792010-04-16 00:11:35 +0200333 /* Register usage at this point:
334 *
335 * R1 = host R1
336 * R2 = host R2
337 * R12 = exit handler id
Paul Mackerras3c42bf82011-06-29 00:20:58 +0000338 * R13 = shadow vcpu (32-bit) or PACA (64-bit)
Alexander Graf07372792010-04-16 00:11:35 +0200339 * SVCPU.* = guest *
340 *
341 */
342
Paul Mackerras02143942011-07-23 17:41:44 +1000343 PPC_LL r6, HSTATE_HOST_MSR(r13)
Paul Mackerras3c42bf82011-06-29 00:20:58 +0000344 PPC_LL r8, HSTATE_VMHANDLER(r13)
Paul Mackerras02143942011-07-23 17:41:44 +1000345
346 /* Restore host msr -> SRR1 */
347 mtsrr1 r6
348 /* Load highmem handler address */
Alexander Graf07372792010-04-16 00:11:35 +0200349 mtsrr0 r8
350
Paul Mackerras02143942011-07-23 17:41:44 +1000351 /* RFI into the highmem handler, or jump to interrupt handler */
352 beqctr
Alexander Graf07372792010-04-16 00:11:35 +0200353 RFI
354kvmppc_handler_trampoline_exit_end: