blob: 68e45080cf93d544d56f81161ddd80e519e05813 [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
Alexander Graf07372792010-04-16 00:11:35 +020026
27#elif defined(CONFIG_PPC_BOOK3S_32)
28
29#define GET_SHADOW_VCPU(reg) \
30 tophys(reg, r2); \
31 lwz reg, (THREAD + THREAD_KVM_SVCPU)(reg); \
32 tophys(reg, reg)
33
34#endif
35
36/* Disable for nested KVM */
37#define USE_QUICK_LAST_INST
38
39
40/* Get helper functions for subarch specific functionality */
41
42#if defined(CONFIG_PPC_BOOK3S_64)
43#include "book3s_64_slb.S"
44#elif defined(CONFIG_PPC_BOOK3S_32)
45#include "book3s_32_sr.S"
46#endif
47
48/******************************************************************************
49 * *
50 * Entry code *
51 * *
52 *****************************************************************************/
53
54.global kvmppc_handler_trampoline_enter
55kvmppc_handler_trampoline_enter:
56
57 /* Required state:
58 *
59 * MSR = ~IR|DR
Alexander Graf07372792010-04-16 00:11:35 +020060 * R1 = host R1
61 * R2 = host R2
Paul Mackerras02143942011-07-23 17:41:44 +100062 * R4 = guest shadow MSR
63 * R5 = normal host MSR
64 * R6 = current host MSR (EE, IR, DR off)
65 * LR = highmem guest exit code
Alexander Graf07372792010-04-16 00:11:35 +020066 * all other volatile GPRS = free
67 * SVCPU[CR] = guest CR
68 * SVCPU[XER] = guest XER
69 * SVCPU[CTR] = guest CTR
70 * SVCPU[LR] = guest LR
71 */
72
73 /* r3 = shadow vcpu */
74 GET_SHADOW_VCPU(r3)
75
Paul Mackerras02143942011-07-23 17:41:44 +100076 /* Save guest exit handler address and MSR */
77 mflr r0
78 PPC_STL r0, HSTATE_VMHANDLER(r3)
79 PPC_STL r5, HSTATE_HOST_MSR(r3)
80
Paul Mackerras3c42bf82011-06-29 00:20:58 +000081 /* Save R1/R2 in the PACA (64-bit) or shadow_vcpu (32-bit) */
82 PPC_STL r1, HSTATE_HOST_R1(r3)
83 PPC_STL r2, HSTATE_HOST_R2(r3)
84
Alexander Graf07372792010-04-16 00:11:35 +020085 /* Activate guest mode, so faults get handled by KVM */
86 li r11, KVM_GUEST_MODE_GUEST
Paul Mackerras3c42bf82011-06-29 00:20:58 +000087 stb r11, HSTATE_IN_GUEST(r3)
Alexander Graf07372792010-04-16 00:11:35 +020088
89 /* Switch to guest segment. This is subarch specific. */
90 LOAD_GUEST_SEGMENTS
91
Paul Mackerras02143942011-07-23 17:41:44 +100092#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf616dff82014-04-29 16:48:44 +020093BEGIN_FTR_SECTION
94 /* Save host FSCR */
95 mfspr r8, SPRN_FSCR
96 std r8, HSTATE_HOST_FSCR(r13)
97 /* Set FSCR during guest execution */
98 ld r9, SVCPU_SHADOW_FSCR(r13)
99 mtspr SPRN_FSCR, r9
100END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
101
Paul Mackerras02143942011-07-23 17:41:44 +1000102 /* Some guests may need to have dcbz set to 32 byte length.
103 *
104 * Usually we ensure that by patching the guest's instructions
105 * to trap on dcbz and emulate it in the hypervisor.
106 *
107 * If we can, we should tell the CPU to use 32 byte dcbz though,
108 * because that's a lot faster.
109 */
110 lbz r0, HSTATE_RESTORE_HID5(r3)
111 cmpwi r0, 0
112 beq no_dcbz32_on
113
114 mfspr r0,SPRN_HID5
115 ori r0, r0, 0x80 /* XXX HID5_dcbz32 = 0x80 */
116 mtspr SPRN_HID5,r0
117no_dcbz32_on:
118
119#endif /* CONFIG_PPC_BOOK3S_64 */
120
Alexander Graf07372792010-04-16 00:11:35 +0200121 /* Enter guest */
122
Paul Mackerras02143942011-07-23 17:41:44 +1000123 PPC_LL r8, SVCPU_CTR(r3)
124 PPC_LL r9, SVCPU_LR(r3)
125 lwz r10, SVCPU_CR(r3)
Sam bobroffc63517c2015-05-27 09:56:57 +1000126 PPC_LL r11, SVCPU_XER(r3)
Alexander Graf07372792010-04-16 00:11:35 +0200127
Paul Mackerras02143942011-07-23 17:41:44 +1000128 mtctr r8
129 mtlr r9
130 mtcr r10
131 mtxer r11
132
133 /* Move SRR0 and SRR1 into the respective regs */
134 PPC_LL r9, SVCPU_PC(r3)
135 /* First clear RI in our current MSR value */
136 li r0, MSR_RI
137 andc r6, r6, r0
Alexander Graf07372792010-04-16 00:11:35 +0200138
Paul Mackerrasde56a942011-06-29 00:21:34 +0000139 PPC_LL r0, SVCPU_R0(r3)
140 PPC_LL r1, SVCPU_R1(r3)
141 PPC_LL r2, SVCPU_R2(r3)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000142 PPC_LL r5, SVCPU_R5(r3)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000143 PPC_LL r7, SVCPU_R7(r3)
144 PPC_LL r8, SVCPU_R8(r3)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000145 PPC_LL r10, SVCPU_R10(r3)
146 PPC_LL r11, SVCPU_R11(r3)
147 PPC_LL r12, SVCPU_R12(r3)
148 PPC_LL r13, SVCPU_R13(r3)
Alexander Graf07372792010-04-16 00:11:35 +0200149
Alexander Graf8c2d0be2012-04-25 14:28:23 +0200150 MTMSR_EERI(r6)
151 mtsrr0 r9
152 mtsrr1 r4
153
154 PPC_LL r4, SVCPU_R4(r3)
155 PPC_LL r6, SVCPU_R6(r3)
156 PPC_LL r9, SVCPU_R9(r3)
Alexander Graf07372792010-04-16 00:11:35 +0200157 PPC_LL r3, (SVCPU_R3)(r3)
158
159 RFI
160kvmppc_handler_trampoline_enter_end:
161
162
163
164/******************************************************************************
165 * *
166 * Exit code *
167 * *
168 *****************************************************************************/
169
Aneesh Kumar K.Vdd96b2c2013-10-07 22:17:55 +0530170.global kvmppc_interrupt_pr
171kvmppc_interrupt_pr:
Nicholas Piggind3918e72016-12-22 04:29:25 +1000172 /* 64-bit entry. Register usage at this point:
173 *
174 * SPRG_SCRATCH0 = guest R13
175 * R12 = (guest CR << 32) | exit handler id
176 * R13 = PACA
177 * HSTATE.SCRATCH0 = guest R12
178 */
179#ifdef CONFIG_PPC64
180 /* Match 32-bit entry */
181 rotldi r12, r12, 32 /* Flip R12 halves for stw */
182 stw r12, HSTATE_SCRATCH1(r13) /* CR is now in the low half */
183 srdi r12, r12, 32 /* shift trap into low half */
184#endif
Paul Mackerrasb01c8b52011-06-29 00:18:26 +0000185
Nicholas Piggind3918e72016-12-22 04:29:25 +1000186.global kvmppc_handler_trampoline_exit
187kvmppc_handler_trampoline_exit:
Alexander Graf07372792010-04-16 00:11:35 +0200188 /* Register usage at this point:
189 *
Nicholas Piggind3918e72016-12-22 04:29:25 +1000190 * SPRG_SCRATCH0 = guest R13
191 * R12 = exit handler id
192 * R13 = shadow vcpu (32-bit) or PACA (64-bit)
Paul Mackerras3c42bf82011-06-29 00:20:58 +0000193 * HSTATE.SCRATCH0 = guest R12
194 * HSTATE.SCRATCH1 = guest CR
Alexander Graf07372792010-04-16 00:11:35 +0200195 */
196
197 /* Save registers */
198
Paul Mackerras3c42bf82011-06-29 00:20:58 +0000199 PPC_STL r0, SVCPU_R0(r13)
200 PPC_STL r1, SVCPU_R1(r13)
201 PPC_STL r2, SVCPU_R2(r13)
202 PPC_STL r3, SVCPU_R3(r13)
203 PPC_STL r4, SVCPU_R4(r13)
204 PPC_STL r5, SVCPU_R5(r13)
205 PPC_STL r6, SVCPU_R6(r13)
206 PPC_STL r7, SVCPU_R7(r13)
207 PPC_STL r8, SVCPU_R8(r13)
208 PPC_STL r9, SVCPU_R9(r13)
209 PPC_STL r10, SVCPU_R10(r13)
210 PPC_STL r11, SVCPU_R11(r13)
Alexander Graf07372792010-04-16 00:11:35 +0200211
212 /* Restore R1/R2 so we can handle faults */
Paul Mackerras3c42bf82011-06-29 00:20:58 +0000213 PPC_LL r1, HSTATE_HOST_R1(r13)
214 PPC_LL r2, HSTATE_HOST_R2(r13)
Alexander Graf07372792010-04-16 00:11:35 +0200215
216 /* Save guest PC and MSR */
Paul Mackerrasb01c8b52011-06-29 00:18:26 +0000217#ifdef CONFIG_PPC64
218BEGIN_FTR_SECTION
Alexander Graf32c7dbf2012-05-10 03:58:50 +0200219 andi. r0, r12, 0x2
220 cmpwi cr1, r0, 0
Benjamin Herrenschmidta5d4f3a2011-04-05 14:20:31 +1000221 beq 1f
222 mfspr r3,SPRN_HSRR0
223 mfspr r4,SPRN_HSRR1
224 andi. r12,r12,0x3ffd
225 b 2f
Paul Mackerras969391c2011-06-29 00:26:11 +0000226END_FTR_SECTION_IFSET(CPU_FTR_HVMODE)
Paul Mackerrasb01c8b52011-06-29 00:18:26 +0000227#endif
Benjamin Herrenschmidta5d4f3a2011-04-05 14:20:31 +10002281: mfsrr0 r3
Alexander Graf07372792010-04-16 00:11:35 +0200229 mfsrr1 r4
Benjamin Herrenschmidta5d4f3a2011-04-05 14:20:31 +10002302:
Paul Mackerras3c42bf82011-06-29 00:20:58 +0000231 PPC_STL r3, SVCPU_PC(r13)
232 PPC_STL r4, SVCPU_SHADOW_SRR1(r13)
Alexander Graf07372792010-04-16 00:11:35 +0200233
234 /* Get scratch'ed off registers */
Paul Mackerras673b1892011-04-05 13:59:58 +1000235 GET_SCRATCH0(r9)
Paul Mackerras3c42bf82011-06-29 00:20:58 +0000236 PPC_LL r8, HSTATE_SCRATCH0(r13)
237 lwz r7, HSTATE_SCRATCH1(r13)
Alexander Graf07372792010-04-16 00:11:35 +0200238
Paul Mackerras3c42bf82011-06-29 00:20:58 +0000239 PPC_STL r9, SVCPU_R13(r13)
240 PPC_STL r8, SVCPU_R12(r13)
241 stw r7, SVCPU_CR(r13)
Alexander Graf07372792010-04-16 00:11:35 +0200242
243 /* Save more register state */
244
245 mfxer r5
246 mfdar r6
247 mfdsisr r7
248 mfctr r8
249 mflr r9
250
Sam bobroffc63517c2015-05-27 09:56:57 +1000251 PPC_STL r5, SVCPU_XER(r13)
Paul Mackerras3c42bf82011-06-29 00:20:58 +0000252 PPC_STL r6, SVCPU_FAULT_DAR(r13)
253 stw r7, SVCPU_FAULT_DSISR(r13)
254 PPC_STL r8, SVCPU_CTR(r13)
255 PPC_STL r9, SVCPU_LR(r13)
Alexander Graf07372792010-04-16 00:11:35 +0200256
257 /*
258 * In order for us to easily get the last instruction,
259 * we got the #vmexit at, we exploit the fact that the
260 * virtual layout is still the same here, so we can just
261 * ld from the guest's PC address
262 */
263
264 /* We only load the last instruction when it's safe */
265 cmpwi r12, BOOK3S_INTERRUPT_DATA_STORAGE
266 beq ld_last_inst
267 cmpwi r12, BOOK3S_INTERRUPT_PROGRAM
268 beq ld_last_inst
Alexander Graf77e675a2011-08-08 16:11:36 +0200269 cmpwi r12, BOOK3S_INTERRUPT_SYSCALL
270 beq ld_last_prev_inst
Alexander Graf6fc55822010-04-20 02:49:49 +0200271 cmpwi r12, BOOK3S_INTERRUPT_ALIGNMENT
272 beq- ld_last_inst
Alexander Graf7ef4e982012-05-10 03:54:58 +0200273#ifdef CONFIG_PPC64
274BEGIN_FTR_SECTION
275 cmpwi r12, BOOK3S_INTERRUPT_H_EMUL_ASSIST
276 beq- ld_last_inst
277END_FTR_SECTION_IFSET(CPU_FTR_HVMODE)
Alexander Graf616dff82014-04-29 16:48:44 +0200278BEGIN_FTR_SECTION
279 cmpwi r12, BOOK3S_INTERRUPT_FAC_UNAVAIL
280 beq- ld_last_inst
281END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Alexander Graf7ef4e982012-05-10 03:54:58 +0200282#endif
Alexander Graf07372792010-04-16 00:11:35 +0200283
284 b no_ld_last_inst
285
Alexander Graf77e675a2011-08-08 16:11:36 +0200286ld_last_prev_inst:
287 addi r3, r3, -4
288
Alexander Graf07372792010-04-16 00:11:35 +0200289ld_last_inst:
290 /* Save off the guest instruction we're at */
291
292 /* In case lwz faults */
293 li r0, KVM_INST_FETCH_FAILED
294
295#ifdef USE_QUICK_LAST_INST
296
297 /* Set guest mode to 'jump over instruction' so if lwz faults
298 * we'll just continue at the next IP. */
299 li r9, KVM_GUEST_MODE_SKIP
Paul Mackerras3c42bf82011-06-29 00:20:58 +0000300 stb r9, HSTATE_IN_GUEST(r13)
Alexander Graf07372792010-04-16 00:11:35 +0200301
302 /* 1) enable paging for data */
303 mfmsr r9
304 ori r11, r9, MSR_DR /* Enable paging for data */
305 mtmsr r11
306 sync
307 /* 2) fetch the instruction */
308 lwz r0, 0(r3)
309 /* 3) disable paging again */
310 mtmsr r9
311 sync
312
313#endif
Paul Mackerras3c42bf82011-06-29 00:20:58 +0000314 stw r0, SVCPU_LAST_INST(r13)
Alexander Graf07372792010-04-16 00:11:35 +0200315
316no_ld_last_inst:
317
318 /* Unset guest mode */
319 li r9, KVM_GUEST_MODE_NONE
Paul Mackerras3c42bf82011-06-29 00:20:58 +0000320 stb r9, HSTATE_IN_GUEST(r13)
Alexander Graf07372792010-04-16 00:11:35 +0200321
322 /* Switch back to host MMU */
323 LOAD_HOST_SEGMENTS
324
Paul Mackerras02143942011-07-23 17:41:44 +1000325#ifdef CONFIG_PPC_BOOK3S_64
326
327 lbz r5, HSTATE_RESTORE_HID5(r13)
328 cmpwi r5, 0
329 beq no_dcbz32_off
330
331 li r4, 0
332 mfspr r5,SPRN_HID5
333 rldimi r5,r4,6,56
334 mtspr SPRN_HID5,r5
335
336no_dcbz32_off:
337
Alexander Graf616dff82014-04-29 16:48:44 +0200338BEGIN_FTR_SECTION
339 /* Save guest FSCR on a FAC_UNAVAIL interrupt */
340 cmpwi r12, BOOK3S_INTERRUPT_FAC_UNAVAIL
341 bne+ no_fscr_save
342 mfspr r7, SPRN_FSCR
343 std r7, SVCPU_SHADOW_FSCR(r13)
344no_fscr_save:
345 /* Restore host FSCR */
346 ld r8, HSTATE_HOST_FSCR(r13)
347 mtspr SPRN_FSCR, r8
348END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
349
Paul Mackerras02143942011-07-23 17:41:44 +1000350#endif /* CONFIG_PPC_BOOK3S_64 */
351
352 /*
353 * For some interrupts, we need to call the real Linux
354 * handler, so it can do work for us. This has to happen
355 * as if the interrupt arrived from the kernel though,
356 * so let's fake it here where most state is restored.
357 *
358 * Having set up SRR0/1 with the address where we want
359 * to continue with relocation on (potentially in module
360 * space), we either just go straight there with rfi[d],
Alexander Graf56e13db2012-04-27 16:33:35 +0200361 * or we jump to an interrupt handler if there is an
362 * interrupt to be handled first. In the latter case,
363 * the rfi[d] at the end of the interrupt handler will
364 * get us back to where we want to continue.
Paul Mackerras02143942011-07-23 17:41:44 +1000365 */
366
Alexander Graf07372792010-04-16 00:11:35 +0200367 /* Register usage at this point:
368 *
369 * R1 = host R1
370 * R2 = host R2
Alexander Graf56e13db2012-04-27 16:33:35 +0200371 * R10 = raw exit handler id
Alexander Graf07372792010-04-16 00:11:35 +0200372 * R12 = exit handler id
Paul Mackerras3c42bf82011-06-29 00:20:58 +0000373 * R13 = shadow vcpu (32-bit) or PACA (64-bit)
Alexander Graf07372792010-04-16 00:11:35 +0200374 * SVCPU.* = guest *
375 *
376 */
377
Paul Mackerras02143942011-07-23 17:41:44 +1000378 PPC_LL r6, HSTATE_HOST_MSR(r13)
Paul Mackerras3c42bf82011-06-29 00:20:58 +0000379 PPC_LL r8, HSTATE_VMHANDLER(r13)
Paul Mackerras02143942011-07-23 17:41:44 +1000380
Alexander Graf56e13db2012-04-27 16:33:35 +0200381#ifdef CONFIG_PPC64
382BEGIN_FTR_SECTION
Alexander Graf32c7dbf2012-05-10 03:58:50 +0200383 beq cr1, 1f
Alexander Graf56e13db2012-04-27 16:33:35 +0200384 mtspr SPRN_HSRR1, r6
385 mtspr SPRN_HSRR0, r8
386END_FTR_SECTION_IFSET(CPU_FTR_HVMODE)
387#endif
3881: /* Restore host msr -> SRR1 */
Paul Mackerras02143942011-07-23 17:41:44 +1000389 mtsrr1 r6
390 /* Load highmem handler address */
Alexander Graf07372792010-04-16 00:11:35 +0200391 mtsrr0 r8
392
Paul Mackerras02143942011-07-23 17:41:44 +1000393 /* RFI into the highmem handler, or jump to interrupt handler */
Alexander Graf56e13db2012-04-27 16:33:35 +0200394 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
395 beqa BOOK3S_INTERRUPT_EXTERNAL
396 cmpwi r12, BOOK3S_INTERRUPT_DECREMENTER
397 beqa BOOK3S_INTERRUPT_DECREMENTER
398 cmpwi r12, BOOK3S_INTERRUPT_PERFMON
399 beqa BOOK3S_INTERRUPT_PERFMON
Paul Mackerras40688902014-01-08 21:25:36 +1100400 cmpwi r12, BOOK3S_INTERRUPT_DOORBELL
401 beqa BOOK3S_INTERRUPT_DOORBELL
Alexander Graf56e13db2012-04-27 16:33:35 +0200402
Alexander Graf07372792010-04-16 00:11:35 +0200403 RFI
404kvmppc_handler_trampoline_exit_end: