blob: 82b06dfe2d274e9fc6456cfee4900c9d1895a1d7 [file] [log] [blame]
Paul Mackerrasde56a942011-06-29 00:21:34 +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 * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
12 *
13 * Derived from book3s_rmhandlers.S and other files, which are:
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>
Paul Mackerrasde56a942011-06-29 00:21:34 +000024#include <asm/page.h>
Paul Mackerras177339d2011-07-23 17:41:11 +100025#include <asm/ptrace.h>
26#include <asm/hvcall.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000027#include <asm/asm-offsets.h>
28#include <asm/exception-64s.h>
Paul Mackerrasf0888f72012-02-03 00:54:17 +000029#include <asm/kvm_book3s_asm.h>
Paul Mackerrasb4072df2012-11-23 22:37:50 +000030#include <asm/mmu-hash64.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000031
Anton Blanchard7ffcf8e2013-08-07 02:01:46 +100032#ifdef __LITTLE_ENDIAN__
33#error Need to fix lppaca and SLB shadow accesses in little endian mode
34#endif
35
Paul Mackerrasde56a942011-06-29 00:21:34 +000036/*****************************************************************************
37 * *
38 * Real Mode handlers that need to be in the linear mapping *
39 * *
40 ****************************************************************************/
41
Paul Mackerrasde56a942011-06-29 00:21:34 +000042 .globl kvmppc_skip_interrupt
43kvmppc_skip_interrupt:
44 mfspr r13,SPRN_SRR0
45 addi r13,r13,4
46 mtspr SPRN_SRR0,r13
47 GET_SCRATCH0(r13)
48 rfid
49 b .
50
51 .globl kvmppc_skip_Hinterrupt
52kvmppc_skip_Hinterrupt:
53 mfspr r13,SPRN_HSRR0
54 addi r13,r13,4
55 mtspr SPRN_HSRR0,r13
56 GET_SCRATCH0(r13)
57 hrfid
58 b .
59
60/*
Paul Mackerras19ccb762011-07-23 17:42:46 +100061 * Call kvmppc_hv_entry in real mode.
Paul Mackerrasde56a942011-06-29 00:21:34 +000062 * Must be called with interrupts hard-disabled.
63 *
64 * Input Registers:
65 *
66 * LR = return address to continue at after eventually re-enabling MMU
67 */
68_GLOBAL(kvmppc_hv_entry_trampoline)
69 mfmsr r10
70 LOAD_REG_ADDR(r5, kvmppc_hv_entry)
71 li r0,MSR_RI
72 andc r0,r10,r0
73 li r6,MSR_IR | MSR_DR
74 andc r6,r10,r6
75 mtmsrd r0,1 /* clear RI in MSR */
76 mtsrr0 r5
77 mtsrr1 r6
78 RFI
79
Paul Mackerrasde56a942011-06-29 00:21:34 +000080/******************************************************************************
81 * *
82 * Entry code *
83 * *
84 *****************************************************************************/
85
Paul Mackerras371fefd2011-06-29 00:23:08 +000086/*
87 * We come in here when wakened from nap mode on a secondary hw thread.
88 * Relocation is off and most register values are lost.
89 * r13 points to the PACA.
90 */
91 .globl kvm_start_guest
92kvm_start_guest:
93 ld r1,PACAEMERGSP(r13)
94 subi r1,r1,STACK_FRAME_OVERHEAD
Paul Mackerras19ccb762011-07-23 17:42:46 +100095 ld r2,PACATOC(r13)
96
Paul Mackerrasf0888f72012-02-03 00:54:17 +000097 li r0,KVM_HWTHREAD_IN_KVM
98 stb r0,HSTATE_HWTHREAD_STATE(r13)
99
100 /* NV GPR values from power7_idle() will no longer be valid */
101 li r0,1
102 stb r0,PACA_NAPSTATELOST(r13)
103
Paul Mackerras4619ac82013-04-17 20:31:41 +0000104 /* were we napping due to cede? */
105 lbz r0,HSTATE_NAPPING(r13)
106 cmpwi r0,0
107 bne kvm_end_cede
108
109 /*
110 * We weren't napping due to cede, so this must be a secondary
111 * thread being woken up to run a guest, or being woken up due
112 * to a stray IPI. (Or due to some machine check or hypervisor
113 * maintenance interrupt while the core is in KVM.)
114 */
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000115
116 /* Check the wake reason in SRR1 to see why we got here */
117 mfspr r3,SPRN_SRR1
118 rlwinm r3,r3,44-31,0x7 /* extract wake reason field */
119 cmpwi r3,4 /* was it an external interrupt? */
Paul Mackerras4619ac82013-04-17 20:31:41 +0000120 bne 27f /* if not */
121 ld r5,HSTATE_XICS_PHYS(r13)
122 li r7,XICS_XIRR /* if it was an external interrupt, */
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000123 lwzcix r8,r5,r7 /* get and ack the interrupt */
124 sync
125 clrldi. r9,r8,40 /* get interrupt source ID. */
Paul Mackerras4619ac82013-04-17 20:31:41 +0000126 beq 28f /* none there? */
127 cmpwi r9,XICS_IPI /* was it an IPI? */
128 bne 29f
129 li r0,0xff
130 li r6,XICS_MFRR
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000131 stbcix r0,r5,r6 /* clear IPI */
Paul Mackerras4619ac82013-04-17 20:31:41 +0000132 stwcix r8,r5,r7 /* EOI the interrupt */
133 sync /* order loading of vcpu after that */
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000134
Paul Mackerras4619ac82013-04-17 20:31:41 +0000135 /* get vcpu pointer, NULL if we have no vcpu to run */
Paul Mackerras7b444c62012-10-15 01:16:14 +0000136 ld r4,HSTATE_KVM_VCPU(r13)
137 cmpdi r4,0
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000138 /* if we have no vcpu to run, go back to sleep */
Paul Mackerras7b444c62012-10-15 01:16:14 +0000139 beq kvm_no_guest
Paul Mackerras4619ac82013-04-17 20:31:41 +0000140 b kvmppc_hv_entry
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000141
Paul Mackerras4619ac82013-04-17 20:31:41 +000014227: /* XXX should handle hypervisor maintenance interrupts etc. here */
143 b kvm_no_guest
14428: /* SRR1 said external but ICP said nope?? */
145 b kvm_no_guest
14629: /* External non-IPI interrupt to offline secondary thread? help?? */
147 stw r8,HSTATE_SAVED_XIRR(r13)
148 b kvm_no_guest
Paul Mackerras371fefd2011-06-29 00:23:08 +0000149
Paul Mackerrasde56a942011-06-29 00:21:34 +0000150.global kvmppc_hv_entry
151kvmppc_hv_entry:
152
153 /* Required state:
154 *
155 * R4 = vcpu pointer
156 * MSR = ~IR|DR
157 * R13 = PACA
158 * R1 = host R1
159 * all other volatile GPRS = free
160 */
161 mflr r0
162 std r0, HSTATE_VMHANDLER(r13)
163
Paul Mackerras89436332012-03-02 01:38:23 +0000164 /* Set partition DABR */
165 /* Do this before re-enabling PMU to avoid P7 DABR corruption bug */
166 li r5,3
167 ld r6,VCPU_DABR(r4)
168 mtspr SPRN_DABRX,r5
169 mtspr SPRN_DABR,r6
170BEGIN_FTR_SECTION
171 isync
172END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000173
174 /* Load guest PMU registers */
175 /* R4 is live here (vcpu pointer) */
176 li r3, 1
177 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
178 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
179 isync
180 lwz r3, VCPU_PMC(r4) /* always load up guest PMU registers */
181 lwz r5, VCPU_PMC + 4(r4) /* to prevent information leak */
182 lwz r6, VCPU_PMC + 8(r4)
183 lwz r7, VCPU_PMC + 12(r4)
184 lwz r8, VCPU_PMC + 16(r4)
185 lwz r9, VCPU_PMC + 20(r4)
Paul Mackerras9e368f22011-06-29 00:40:08 +0000186BEGIN_FTR_SECTION
187 lwz r10, VCPU_PMC + 24(r4)
188 lwz r11, VCPU_PMC + 28(r4)
189END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000190 mtspr SPRN_PMC1, r3
191 mtspr SPRN_PMC2, r5
192 mtspr SPRN_PMC3, r6
193 mtspr SPRN_PMC4, r7
194 mtspr SPRN_PMC5, r8
195 mtspr SPRN_PMC6, r9
Paul Mackerras9e368f22011-06-29 00:40:08 +0000196BEGIN_FTR_SECTION
197 mtspr SPRN_PMC7, r10
198 mtspr SPRN_PMC8, r11
199END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000200 ld r3, VCPU_MMCR(r4)
201 ld r5, VCPU_MMCR + 8(r4)
202 ld r6, VCPU_MMCR + 16(r4)
Paul Mackerras14941782013-09-06 13:11:18 +1000203 ld r7, VCPU_SIAR(r4)
204 ld r8, VCPU_SDAR(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000205 mtspr SPRN_MMCR1, r5
206 mtspr SPRN_MMCRA, r6
Paul Mackerras14941782013-09-06 13:11:18 +1000207 mtspr SPRN_SIAR, r7
208 mtspr SPRN_SDAR, r8
Paul Mackerrasde56a942011-06-29 00:21:34 +0000209 mtspr SPRN_MMCR0, r3
210 isync
211
212 /* Load up FP, VMX and VSX registers */
213 bl kvmppc_load_fp
214
Michael Neulingc75df6f2012-06-25 13:33:10 +0000215 ld r14, VCPU_GPR(R14)(r4)
216 ld r15, VCPU_GPR(R15)(r4)
217 ld r16, VCPU_GPR(R16)(r4)
218 ld r17, VCPU_GPR(R17)(r4)
219 ld r18, VCPU_GPR(R18)(r4)
220 ld r19, VCPU_GPR(R19)(r4)
221 ld r20, VCPU_GPR(R20)(r4)
222 ld r21, VCPU_GPR(R21)(r4)
223 ld r22, VCPU_GPR(R22)(r4)
224 ld r23, VCPU_GPR(R23)(r4)
225 ld r24, VCPU_GPR(R24)(r4)
226 ld r25, VCPU_GPR(R25)(r4)
227 ld r26, VCPU_GPR(R26)(r4)
228 ld r27, VCPU_GPR(R27)(r4)
229 ld r28, VCPU_GPR(R28)(r4)
230 ld r29, VCPU_GPR(R29)(r4)
231 ld r30, VCPU_GPR(R30)(r4)
232 ld r31, VCPU_GPR(R31)(r4)
Paul Mackerras89436332012-03-02 01:38:23 +0000233
Paul Mackerras9e368f22011-06-29 00:40:08 +0000234BEGIN_FTR_SECTION
Paul Mackerrasde56a942011-06-29 00:21:34 +0000235 /* Switch DSCR to guest value */
236 ld r5, VCPU_DSCR(r4)
237 mtspr SPRN_DSCR, r5
Paul Mackerras9e368f22011-06-29 00:40:08 +0000238END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000239
240 /*
241 * Set the decrementer to the guest decrementer.
242 */
243 ld r8,VCPU_DEC_EXPIRES(r4)
244 mftb r7
245 subf r3,r7,r8
246 mtspr SPRN_DEC,r3
247 stw r3,VCPU_DEC(r4)
248
249 ld r5, VCPU_SPRG0(r4)
250 ld r6, VCPU_SPRG1(r4)
251 ld r7, VCPU_SPRG2(r4)
252 ld r8, VCPU_SPRG3(r4)
253 mtspr SPRN_SPRG0, r5
254 mtspr SPRN_SPRG1, r6
255 mtspr SPRN_SPRG2, r7
256 mtspr SPRN_SPRG3, r8
257
258 /* Save R1 in the PACA */
259 std r1, HSTATE_HOST_R1(r13)
260
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000261 /* Increment yield count if they have a VPA */
262 ld r3, VCPU_VPA(r4)
263 cmpdi r3, 0
264 beq 25f
265 lwz r5, LPPACA_YIELDCOUNT(r3)
266 addi r5, r5, 1
267 stw r5, LPPACA_YIELDCOUNT(r3)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000268 li r6, 1
269 stb r6, VCPU_VPA_DIRTY(r4)
Paul Mackerrasa8606e22011-06-29 00:22:05 +000027025:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000271 /* Load up DAR and DSISR */
272 ld r5, VCPU_DAR(r4)
273 lwz r6, VCPU_DSISR(r4)
274 mtspr SPRN_DAR, r5
275 mtspr SPRN_DSISR, r6
276
Paul Mackerras9e368f22011-06-29 00:40:08 +0000277BEGIN_FTR_SECTION
Paul Mackerrasde56a942011-06-29 00:21:34 +0000278 /* Restore AMR and UAMOR, set AMOR to all 1s */
279 ld r5,VCPU_AMR(r4)
280 ld r6,VCPU_UAMOR(r4)
281 li r7,-1
282 mtspr SPRN_AMR,r5
283 mtspr SPRN_UAMOR,r6
284 mtspr SPRN_AMOR,r7
Paul Mackerras9e368f22011-06-29 00:40:08 +0000285END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000286
287 /* Clear out SLB */
288 li r6,0
289 slbmte r6,r6
290 slbia
291 ptesync
292
Paul Mackerras9e368f22011-06-29 00:40:08 +0000293BEGIN_FTR_SECTION
294 b 30f
295END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
296 /*
297 * POWER7 host -> guest partition switch code.
298 * We don't have to lock against concurrent tlbies,
299 * but we do have to coordinate across hardware threads.
300 */
Paul Mackerras371fefd2011-06-29 00:23:08 +0000301 /* Increment entry count iff exit count is zero. */
302 ld r5,HSTATE_KVM_VCORE(r13)
303 addi r9,r5,VCORE_ENTRY_EXIT
30421: lwarx r3,0,r9
305 cmpwi r3,0x100 /* any threads starting to exit? */
306 bge secondary_too_late /* if so we're too late to the party */
307 addi r3,r3,1
308 stwcx. r3,0,r9
309 bne 21b
310
311 /* Primary thread switches to guest partition. */
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000312 ld r9,VCPU_KVM(r4) /* pointer to struct kvm */
Paul Mackerras371fefd2011-06-29 00:23:08 +0000313 lwz r6,VCPU_PTID(r4)
314 cmpwi r6,0
315 bne 20f
Paul Mackerrasde56a942011-06-29 00:21:34 +0000316 ld r6,KVM_SDR1(r9)
317 lwz r7,KVM_LPID(r9)
318 li r0,LPID_RSVD /* switch to reserved LPID */
319 mtspr SPRN_LPID,r0
320 ptesync
321 mtspr SPRN_SDR1,r6 /* switch to partition page table */
322 mtspr SPRN_LPID,r7
323 isync
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000324
325 /* See if we need to flush the TLB */
326 lhz r6,PACAPACAINDEX(r13) /* test_bit(cpu, need_tlb_flush) */
327 clrldi r7,r6,64-6 /* extract bit number (6 bits) */
328 srdi r6,r6,6 /* doubleword number */
329 sldi r6,r6,3 /* address offset */
330 add r6,r6,r9
331 addi r6,r6,KVM_NEED_FLUSH /* dword in kvm->arch.need_tlb_flush */
Paul Mackerras371fefd2011-06-29 00:23:08 +0000332 li r0,1
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000333 sld r0,r0,r7
334 ld r7,0(r6)
335 and. r7,r7,r0
336 beq 22f
33723: ldarx r7,0,r6 /* if set, clear the bit */
338 andc r7,r7,r0
339 stdcx. r7,0,r6
340 bne 23b
341 li r6,128 /* and flush the TLB */
342 mtctr r6
343 li r7,0x800 /* IS field = 0b10 */
344 ptesync
34528: tlbiel r7
346 addi r7,r7,0x1000
347 bdnz 28b
348 ptesync
349
Paul Mackerras93b0f4d2013-09-06 13:17:46 +1000350 /* Add timebase offset onto timebase */
35122: ld r8,VCORE_TB_OFFSET(r5)
352 cmpdi r8,0
353 beq 37f
354 mftb r6 /* current host timebase */
355 add r8,r8,r6
356 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
357 mftb r7 /* check if lower 24 bits overflowed */
358 clrldi r6,r6,40
359 clrldi r7,r7,40
360 cmpld r7,r6
361 bge 37f
362 addis r8,r8,0x100 /* if so, increment upper 40 bits */
363 mtspr SPRN_TBU40,r8
364
36537: li r0,1
Paul Mackerras371fefd2011-06-29 00:23:08 +0000366 stb r0,VCORE_IN_GUEST(r5) /* signal secondaries to continue */
367 b 10f
368
369 /* Secondary threads wait for primary to have done partition switch */
37020: lbz r0,VCORE_IN_GUEST(r5)
371 cmpwi r0,0
372 beq 20b
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000373
Paul Mackerras19ccb762011-07-23 17:42:46 +1000374 /* Set LPCR and RMOR. */
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +000037510: ld r8,KVM_LPCR(r9)
Paul Mackerras19ccb762011-07-23 17:42:46 +1000376 mtspr SPRN_LPCR,r8
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000377 ld r8,KVM_RMOR(r9)
378 mtspr SPRN_RMOR,r8
Paul Mackerrasde56a942011-06-29 00:21:34 +0000379 isync
380
381 /* Check if HDEC expires soon */
382 mfspr r3,SPRN_HDEC
383 cmpwi r3,10
384 li r12,BOOK3S_INTERRUPT_HV_DECREMENTER
385 mr r9,r4
386 blt hdec_soon
387
Paul Mackerrasde56a942011-06-29 00:21:34 +0000388 /* Save purr/spurr */
389 mfspr r5,SPRN_PURR
390 mfspr r6,SPRN_SPURR
391 std r5,HSTATE_PURR(r13)
392 std r6,HSTATE_SPURR(r13)
393 ld r7,VCPU_PURR(r4)
394 ld r8,VCPU_SPURR(r4)
395 mtspr SPRN_PURR,r7
396 mtspr SPRN_SPURR,r8
Paul Mackerras9e368f22011-06-29 00:40:08 +0000397 b 31f
398
399 /*
400 * PPC970 host -> guest partition switch code.
401 * We have to lock against concurrent tlbies,
402 * using native_tlbie_lock to lock against host tlbies
403 * and kvm->arch.tlbie_lock to lock against guest tlbies.
404 * We also have to invalidate the TLB since its
405 * entries aren't tagged with the LPID.
406 */
40730: ld r9,VCPU_KVM(r4) /* pointer to struct kvm */
408
409 /* first take native_tlbie_lock */
410 .section ".toc","aw"
411toc_tlbie_lock:
412 .tc native_tlbie_lock[TC],native_tlbie_lock
413 .previous
414 ld r3,toc_tlbie_lock@toc(2)
Anton Blanchard54bb7f42013-08-07 02:01:51 +1000415#ifdef __BIG_ENDIAN__
Paul Mackerras9e368f22011-06-29 00:40:08 +0000416 lwz r8,PACA_LOCK_TOKEN(r13)
Anton Blanchard54bb7f42013-08-07 02:01:51 +1000417#else
418 lwz r8,PACAPACAINDEX(r13)
419#endif
Paul Mackerras9e368f22011-06-29 00:40:08 +000042024: lwarx r0,0,r3
421 cmpwi r0,0
422 bne 24b
423 stwcx. r8,0,r3
424 bne 24b
425 isync
426
427 ld r7,KVM_LPCR(r9) /* use kvm->arch.lpcr to store HID4 */
428 li r0,0x18f
429 rotldi r0,r0,HID4_LPID5_SH /* all lpid bits in HID4 = 1 */
430 or r0,r7,r0
431 ptesync
432 sync
433 mtspr SPRN_HID4,r0 /* switch to reserved LPID */
434 isync
435 li r0,0
436 stw r0,0(r3) /* drop native_tlbie_lock */
437
438 /* invalidate the whole TLB */
439 li r0,256
440 mtctr r0
441 li r6,0
44225: tlbiel r6
443 addi r6,r6,0x1000
444 bdnz 25b
445 ptesync
446
447 /* Take the guest's tlbie_lock */
448 addi r3,r9,KVM_TLBIE_LOCK
44924: lwarx r0,0,r3
450 cmpwi r0,0
451 bne 24b
452 stwcx. r8,0,r3
453 bne 24b
454 isync
455 ld r6,KVM_SDR1(r9)
456 mtspr SPRN_SDR1,r6 /* switch to partition page table */
457
458 /* Set up HID4 with the guest's LPID etc. */
459 sync
460 mtspr SPRN_HID4,r7
461 isync
462
463 /* drop the guest's tlbie_lock */
464 li r0,0
465 stw r0,0(r3)
466
467 /* Check if HDEC expires soon */
468 mfspr r3,SPRN_HDEC
469 cmpwi r3,10
470 li r12,BOOK3S_INTERRUPT_HV_DECREMENTER
471 mr r9,r4
472 blt hdec_soon
473
474 /* Enable HDEC interrupts */
475 mfspr r0,SPRN_HID0
476 li r3,1
477 rldimi r0,r3, HID0_HDICE_SH, 64-HID0_HDICE_SH-1
478 sync
479 mtspr SPRN_HID0,r0
480 mfspr r0,SPRN_HID0
481 mfspr r0,SPRN_HID0
482 mfspr r0,SPRN_HID0
483 mfspr r0,SPRN_HID0
484 mfspr r0,SPRN_HID0
485 mfspr r0,SPRN_HID0
Paul Mackerrasde56a942011-06-29 00:21:34 +0000486
487 /* Load up guest SLB entries */
Paul Mackerras9e368f22011-06-29 00:40:08 +000048831: lwz r5,VCPU_SLB_MAX(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000489 cmpwi r5,0
490 beq 9f
491 mtctr r5
492 addi r6,r4,VCPU_SLB
4931: ld r8,VCPU_SLB_E(r6)
494 ld r9,VCPU_SLB_V(r6)
495 slbmte r9,r8
496 addi r6,r6,VCPU_SLB_SIZE
497 bdnz 1b
4989:
499
500 /* Restore state of CTRL run bit; assume 1 on entry */
501 lwz r5,VCPU_CTRL(r4)
502 andi. r5,r5,1
503 bne 4f
504 mfspr r6,SPRN_CTRLF
505 clrrdi r6,r6,1
506 mtspr SPRN_CTRLT,r6
5074:
508 ld r6, VCPU_CTR(r4)
509 lwz r7, VCPU_XER(r4)
510
511 mtctr r6
512 mtxer r7
513
Paul Mackerras4619ac82013-04-17 20:31:41 +0000514 ld r10, VCPU_PC(r4)
515 ld r11, VCPU_MSR(r4)
Paul Mackerras19ccb762011-07-23 17:42:46 +1000516kvmppc_cede_reentry: /* r4 = vcpu, r13 = paca */
Paul Mackerrasde56a942011-06-29 00:21:34 +0000517 ld r6, VCPU_SRR0(r4)
518 ld r7, VCPU_SRR1(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000519
Paul Mackerras4619ac82013-04-17 20:31:41 +0000520 /* r11 = vcpu->arch.msr & ~MSR_HV */
Paul Mackerrasde56a942011-06-29 00:21:34 +0000521 rldicl r11, r11, 63 - MSR_HV_LG, 1
522 rotldi r11, r11, 1 + MSR_HV_LG
523 ori r11, r11, MSR_ME
524
Paul Mackerras19ccb762011-07-23 17:42:46 +1000525 /* Check if we can deliver an external or decrementer interrupt now */
526 ld r0,VCPU_PENDING_EXC(r4)
Paul Mackerras4619ac82013-04-17 20:31:41 +0000527 lis r8,(1 << BOOK3S_IRQPRIO_EXTERNAL_LEVEL)@h
Paul Mackerras19ccb762011-07-23 17:42:46 +1000528 and r0,r0,r8
529 cmpdi cr1,r0,0
530 andi. r0,r11,MSR_EE
531 beq cr1,11f
532BEGIN_FTR_SECTION
533 mfspr r8,SPRN_LPCR
534 ori r8,r8,LPCR_MER
535 mtspr SPRN_LPCR,r8
536 isync
537END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
538 beq 5f
539 li r0,BOOK3S_INTERRUPT_EXTERNAL
54012: mr r6,r10
541 mr r10,r0
542 mr r7,r11
543 li r11,(MSR_ME << 1) | 1 /* synthesize MSR_SF | MSR_ME */
544 rotldi r11,r11,63
545 b 5f
54611: beq 5f
547 mfspr r0,SPRN_DEC
548 cmpwi r0,0
549 li r0,BOOK3S_INTERRUPT_DECREMENTER
550 blt 12b
551
552 /* Move SRR0 and SRR1 into the respective regs */
5535: mtspr SPRN_SRR0, r6
554 mtspr SPRN_SRR1, r7
Paul Mackerras19ccb762011-07-23 17:42:46 +1000555
Paul Mackerrasde56a942011-06-29 00:21:34 +0000556fast_guest_return:
Paul Mackerras4619ac82013-04-17 20:31:41 +0000557 li r0,0
558 stb r0,VCPU_CEDED(r4) /* cancel cede */
Paul Mackerrasde56a942011-06-29 00:21:34 +0000559 mtspr SPRN_HSRR0,r10
560 mtspr SPRN_HSRR1,r11
561
562 /* Activate guest mode, so faults get handled by KVM */
563 li r9, KVM_GUEST_MODE_GUEST
564 stb r9, HSTATE_IN_GUEST(r13)
565
566 /* Enter guest */
567
Paul Mackerras0acb9112013-02-04 18:10:51 +0000568BEGIN_FTR_SECTION
569 ld r5, VCPU_CFAR(r4)
570 mtspr SPRN_CFAR, r5
571END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
572
Paul Mackerrasde56a942011-06-29 00:21:34 +0000573 ld r5, VCPU_LR(r4)
574 lwz r6, VCPU_CR(r4)
575 mtlr r5
576 mtcr r6
577
Michael Neulingc75df6f2012-06-25 13:33:10 +0000578 ld r0, VCPU_GPR(R0)(r4)
579 ld r1, VCPU_GPR(R1)(r4)
580 ld r2, VCPU_GPR(R2)(r4)
581 ld r3, VCPU_GPR(R3)(r4)
582 ld r5, VCPU_GPR(R5)(r4)
583 ld r6, VCPU_GPR(R6)(r4)
584 ld r7, VCPU_GPR(R7)(r4)
585 ld r8, VCPU_GPR(R8)(r4)
586 ld r9, VCPU_GPR(R9)(r4)
587 ld r10, VCPU_GPR(R10)(r4)
588 ld r11, VCPU_GPR(R11)(r4)
589 ld r12, VCPU_GPR(R12)(r4)
590 ld r13, VCPU_GPR(R13)(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000591
Michael Neulingc75df6f2012-06-25 13:33:10 +0000592 ld r4, VCPU_GPR(R4)(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000593
594 hrfid
595 b .
596
597/******************************************************************************
598 * *
599 * Exit code *
600 * *
601 *****************************************************************************/
602
603/*
604 * We come here from the first-level interrupt handlers.
605 */
606 .globl kvmppc_interrupt
607kvmppc_interrupt:
608 /*
609 * Register contents:
610 * R12 = interrupt vector
611 * R13 = PACA
612 * guest CR, R12 saved in shadow VCPU SCRATCH1/0
613 * guest R13 saved in SPRN_SCRATCH0
614 */
615 /* abuse host_r2 as third scratch area; we get r2 from PACATOC(r13) */
616 std r9, HSTATE_HOST_R2(r13)
617 ld r9, HSTATE_KVM_VCPU(r13)
618
619 /* Save registers */
620
Michael Neulingc75df6f2012-06-25 13:33:10 +0000621 std r0, VCPU_GPR(R0)(r9)
622 std r1, VCPU_GPR(R1)(r9)
623 std r2, VCPU_GPR(R2)(r9)
624 std r3, VCPU_GPR(R3)(r9)
625 std r4, VCPU_GPR(R4)(r9)
626 std r5, VCPU_GPR(R5)(r9)
627 std r6, VCPU_GPR(R6)(r9)
628 std r7, VCPU_GPR(R7)(r9)
629 std r8, VCPU_GPR(R8)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000630 ld r0, HSTATE_HOST_R2(r13)
Michael Neulingc75df6f2012-06-25 13:33:10 +0000631 std r0, VCPU_GPR(R9)(r9)
632 std r10, VCPU_GPR(R10)(r9)
633 std r11, VCPU_GPR(R11)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000634 ld r3, HSTATE_SCRATCH0(r13)
635 lwz r4, HSTATE_SCRATCH1(r13)
Michael Neulingc75df6f2012-06-25 13:33:10 +0000636 std r3, VCPU_GPR(R12)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000637 stw r4, VCPU_CR(r9)
Paul Mackerras0acb9112013-02-04 18:10:51 +0000638BEGIN_FTR_SECTION
639 ld r3, HSTATE_CFAR(r13)
640 std r3, VCPU_CFAR(r9)
641END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000642
643 /* Restore R1/R2 so we can handle faults */
644 ld r1, HSTATE_HOST_R1(r13)
645 ld r2, PACATOC(r13)
646
647 mfspr r10, SPRN_SRR0
648 mfspr r11, SPRN_SRR1
649 std r10, VCPU_SRR0(r9)
650 std r11, VCPU_SRR1(r9)
651 andi. r0, r12, 2 /* need to read HSRR0/1? */
652 beq 1f
653 mfspr r10, SPRN_HSRR0
654 mfspr r11, SPRN_HSRR1
655 clrrdi r12, r12, 2
6561: std r10, VCPU_PC(r9)
657 std r11, VCPU_MSR(r9)
658
659 GET_SCRATCH0(r3)
660 mflr r4
Michael Neulingc75df6f2012-06-25 13:33:10 +0000661 std r3, VCPU_GPR(R13)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000662 std r4, VCPU_LR(r9)
663
664 /* Unset guest mode */
665 li r0, KVM_GUEST_MODE_NONE
666 stb r0, HSTATE_IN_GUEST(r13)
667
668 stw r12,VCPU_TRAP(r9)
669
Paul Mackerras697d3892011-12-12 12:36:37 +0000670 /* Save HEIR (HV emulation assist reg) in last_inst
671 if this is an HEI (HV emulation interrupt, e40) */
672 li r3,KVM_INST_FETCH_FAILED
673BEGIN_FTR_SECTION
674 cmpwi r12,BOOK3S_INTERRUPT_H_EMUL_ASSIST
675 bne 11f
676 mfspr r3,SPRN_HEIR
677END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
67811: stw r3,VCPU_LAST_INST(r9)
679
680 /* these are volatile across C function calls */
681 mfctr r3
682 mfxer r4
683 std r3, VCPU_CTR(r9)
684 stw r4, VCPU_XER(r9)
685
686BEGIN_FTR_SECTION
687 /* If this is a page table miss then see if it's theirs or ours */
688 cmpwi r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
689 beq kvmppc_hdsi
Paul Mackerras342d3db2011-12-12 12:38:05 +0000690 cmpwi r12, BOOK3S_INTERRUPT_H_INST_STORAGE
691 beq kvmppc_hisi
Paul Mackerras697d3892011-12-12 12:36:37 +0000692END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
693
Paul Mackerrasde56a942011-06-29 00:21:34 +0000694 /* See if this is a leftover HDEC interrupt */
695 cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER
696 bne 2f
697 mfspr r3,SPRN_HDEC
698 cmpwi r3,0
699 bge ignore_hdec
7002:
Paul Mackerras697d3892011-12-12 12:36:37 +0000701 /* See if this is an hcall we can handle in real mode */
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000702 cmpwi r12,BOOK3S_INTERRUPT_SYSCALL
703 beq hcall_try_real_mode
Paul Mackerrasde56a942011-06-29 00:21:34 +0000704
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000705 /* Only handle external interrupts here on arch 206 and later */
Paul Mackerras9e368f22011-06-29 00:40:08 +0000706BEGIN_FTR_SECTION
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000707 b ext_interrupt_to_host
708END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_206)
709
710 /* External interrupt ? */
711 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
712 bne+ ext_interrupt_to_host
713
714 /* External interrupt, first check for host_ipi. If this is
715 * set, we know the host wants us out so let's do it now
716 */
Paul Mackerras4619ac82013-04-17 20:31:41 +0000717do_ext_interrupt:
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000718 lbz r0, HSTATE_HOST_IPI(r13)
719 cmpwi r0, 0
720 bne ext_interrupt_to_host
721
722 /* Now read the interrupt from the ICP */
723 ld r5, HSTATE_XICS_PHYS(r13)
724 li r7, XICS_XIRR
725 cmpdi r5, 0
726 beq- ext_interrupt_to_host
727 lwzcix r3, r5, r7
728 rlwinm. r0, r3, 0, 0xffffff
729 sync
Paul Mackerras4619ac82013-04-17 20:31:41 +0000730 beq 3f /* if nothing pending in the ICP */
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000731
Paul Mackerras4619ac82013-04-17 20:31:41 +0000732 /* We found something in the ICP...
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000733 *
734 * If it's not an IPI, stash it in the PACA and return to
735 * the host, we don't (yet) handle directing real external
736 * interrupts directly to the guest
737 */
738 cmpwi r0, XICS_IPI
739 bne ext_stash_for_host
740
741 /* It's an IPI, clear the MFRR and EOI it */
742 li r0, 0xff
743 li r6, XICS_MFRR
744 stbcix r0, r5, r6 /* clear the IPI */
745 stwcix r3, r5, r7 /* EOI it */
746 sync
747
748 /* We need to re-check host IPI now in case it got set in the
749 * meantime. If it's clear, we bounce the interrupt to the
750 * guest
751 */
752 lbz r0, HSTATE_HOST_IPI(r13)
753 cmpwi r0, 0
754 bne- 1f
755
756 /* Allright, looks like an IPI for the guest, we need to set MER */
Paul Mackerras4619ac82013-04-17 20:31:41 +00007573:
758 /* Check if any CPU is heading out to the host, if so head out too */
759 ld r5, HSTATE_KVM_VCORE(r13)
760 lwz r0, VCORE_ENTRY_EXIT(r5)
761 cmpwi r0, 0x100
762 bge ext_interrupt_to_host
763
764 /* See if there is a pending interrupt for the guest */
765 mfspr r8, SPRN_LPCR
766 ld r0, VCPU_PENDING_EXC(r9)
767 /* Insert EXTERNAL_LEVEL bit into LPCR at the MER bit position */
768 rldicl. r0, r0, 64 - BOOK3S_IRQPRIO_EXTERNAL_LEVEL, 63
769 rldimi r8, r0, LPCR_MER_SH, 63 - LPCR_MER_SH
770 beq 2f
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000771
772 /* And if the guest EE is set, we can deliver immediately, else
773 * we return to the guest with MER set
774 */
775 andi. r0, r11, MSR_EE
Paul Mackerras4619ac82013-04-17 20:31:41 +0000776 beq 2f
777 mtspr SPRN_SRR0, r10
778 mtspr SPRN_SRR1, r11
779 li r10, BOOK3S_INTERRUPT_EXTERNAL
780 li r11, (MSR_ME << 1) | 1 /* synthesize MSR_SF | MSR_ME */
781 rotldi r11, r11, 63
7822: mr r4, r9
783 mtspr SPRN_LPCR, r8
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000784 b fast_guest_return
785
786 /* We raced with the host, we need to resend that IPI, bummer */
7871: li r0, IPI_PRIORITY
788 stbcix r0, r5, r6 /* set the IPI */
789 sync
790 b ext_interrupt_to_host
791
792ext_stash_for_host:
793 /* It's not an IPI and it's for the host, stash it in the PACA
794 * before exit, it will be picked up by the host ICP driver
795 */
796 stw r3, HSTATE_SAVED_XIRR(r13)
797ext_interrupt_to_host:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000798
Paul Mackerrasb4072df2012-11-23 22:37:50 +0000799guest_exit_cont: /* r9 = vcpu, r12 = trap, r13 = paca */
Paul Mackerrasde56a942011-06-29 00:21:34 +0000800 /* Save more register state */
Paul Mackerrasde56a942011-06-29 00:21:34 +0000801 mfdar r6
802 mfdsisr r7
Paul Mackerrasde56a942011-06-29 00:21:34 +0000803 std r6, VCPU_DAR(r9)
804 stw r7, VCPU_DSISR(r9)
Paul Mackerras9e368f22011-06-29 00:40:08 +0000805BEGIN_FTR_SECTION
Paul Mackerras697d3892011-12-12 12:36:37 +0000806 /* don't overwrite fault_dar/fault_dsisr if HDSI */
Paul Mackerrasde56a942011-06-29 00:21:34 +0000807 cmpwi r12,BOOK3S_INTERRUPT_H_DATA_STORAGE
808 beq 6f
Paul Mackerras9e368f22011-06-29 00:40:08 +0000809END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
Paul Mackerras697d3892011-12-12 12:36:37 +0000810 std r6, VCPU_FAULT_DAR(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000811 stw r7, VCPU_FAULT_DSISR(r9)
812
Paul Mackerrasb4072df2012-11-23 22:37:50 +0000813 /* See if it is a machine check */
814 cmpwi r12, BOOK3S_INTERRUPT_MACHINE_CHECK
815 beq machine_check_realmode
816mc_cont:
817
Paul Mackerrasde56a942011-06-29 00:21:34 +0000818 /* Save guest CTRL register, set runlatch to 1 */
Paul Mackerras697d3892011-12-12 12:36:37 +00008196: mfspr r6,SPRN_CTRLF
Paul Mackerrasde56a942011-06-29 00:21:34 +0000820 stw r6,VCPU_CTRL(r9)
821 andi. r0,r6,1
822 bne 4f
823 ori r6,r6,1
824 mtspr SPRN_CTRLT,r6
8254:
826 /* Read the guest SLB and save it away */
827 lwz r0,VCPU_SLB_NR(r9) /* number of entries in SLB */
828 mtctr r0
829 li r6,0
830 addi r7,r9,VCPU_SLB
831 li r5,0
8321: slbmfee r8,r6
833 andis. r0,r8,SLB_ESID_V@h
834 beq 2f
835 add r8,r8,r6 /* put index in */
836 slbmfev r3,r6
837 std r8,VCPU_SLB_E(r7)
838 std r3,VCPU_SLB_V(r7)
839 addi r7,r7,VCPU_SLB_SIZE
840 addi r5,r5,1
8412: addi r6,r6,1
842 bdnz 1b
843 stw r5,VCPU_SLB_MAX(r9)
844
845 /*
846 * Save the guest PURR/SPURR
847 */
Paul Mackerras9e368f22011-06-29 00:40:08 +0000848BEGIN_FTR_SECTION
Paul Mackerrasde56a942011-06-29 00:21:34 +0000849 mfspr r5,SPRN_PURR
850 mfspr r6,SPRN_SPURR
851 ld r7,VCPU_PURR(r9)
852 ld r8,VCPU_SPURR(r9)
853 std r5,VCPU_PURR(r9)
854 std r6,VCPU_SPURR(r9)
855 subf r5,r7,r5
856 subf r6,r8,r6
857
858 /*
859 * Restore host PURR/SPURR and add guest times
860 * so that the time in the guest gets accounted.
861 */
862 ld r3,HSTATE_PURR(r13)
863 ld r4,HSTATE_SPURR(r13)
864 add r3,r3,r5
865 add r4,r4,r6
866 mtspr SPRN_PURR,r3
867 mtspr SPRN_SPURR,r4
Paul Mackerras9e368f22011-06-29 00:40:08 +0000868END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_201)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000869
870 /* Clear out SLB */
871 li r5,0
872 slbmte r5,r5
873 slbia
874 ptesync
875
Paul Mackerras19ccb762011-07-23 17:42:46 +1000876hdec_soon: /* r9 = vcpu, r12 = trap, r13 = paca */
Paul Mackerras9e368f22011-06-29 00:40:08 +0000877BEGIN_FTR_SECTION
878 b 32f
879END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
880 /*
881 * POWER7 guest -> host partition switch code.
882 * We don't have to lock against tlbies but we do
883 * have to coordinate the hardware threads.
884 */
Paul Mackerras371fefd2011-06-29 00:23:08 +0000885 /* Increment the threads-exiting-guest count in the 0xff00
886 bits of vcore->entry_exit_count */
887 lwsync
888 ld r5,HSTATE_KVM_VCORE(r13)
889 addi r6,r5,VCORE_ENTRY_EXIT
89041: lwarx r3,0,r6
891 addi r0,r3,0x100
892 stwcx. r0,0,r6
893 bne 41b
Paul Mackerras19ccb762011-07-23 17:42:46 +1000894 lwsync
Paul Mackerras371fefd2011-06-29 00:23:08 +0000895
896 /*
897 * At this point we have an interrupt that we have to pass
898 * up to the kernel or qemu; we can't handle it in real mode.
899 * Thus we have to do a partition switch, so we have to
900 * collect the other threads, if we are the first thread
901 * to take an interrupt. To do this, we set the HDEC to 0,
902 * which causes an HDEC interrupt in all threads within 2ns
903 * because the HDEC register is shared between all 4 threads.
904 * However, we don't need to bother if this is an HDEC
905 * interrupt, since the other threads will already be on their
906 * way here in that case.
907 */
Paul Mackerras19ccb762011-07-23 17:42:46 +1000908 cmpwi r3,0x100 /* Are we the first here? */
909 bge 43f
910 cmpwi r3,1 /* Are any other threads in the guest? */
911 ble 43f
Paul Mackerras371fefd2011-06-29 00:23:08 +0000912 cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER
913 beq 40f
Paul Mackerras371fefd2011-06-29 00:23:08 +0000914 li r0,0
915 mtspr SPRN_HDEC,r0
91640:
Paul Mackerras19ccb762011-07-23 17:42:46 +1000917 /*
918 * Send an IPI to any napping threads, since an HDEC interrupt
919 * doesn't wake CPUs up from nap.
920 */
921 lwz r3,VCORE_NAPPING_THREADS(r5)
922 lwz r4,VCPU_PTID(r9)
923 li r0,1
Michael Neuling2f584a12012-06-25 13:33:11 +0000924 sld r0,r0,r4
Paul Mackerras19ccb762011-07-23 17:42:46 +1000925 andc. r3,r3,r0 /* no sense IPI'ing ourselves */
926 beq 43f
927 mulli r4,r4,PACA_SIZE /* get paca for thread 0 */
928 subf r6,r4,r13
92942: andi. r0,r3,1
930 beq 44f
931 ld r8,HSTATE_XICS_PHYS(r6) /* get thread's XICS reg addr */
932 li r0,IPI_PRIORITY
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000933 li r7,XICS_MFRR
Paul Mackerras19ccb762011-07-23 17:42:46 +1000934 stbcix r0,r7,r8 /* trigger the IPI */
93544: srdi. r3,r3,1
936 addi r6,r6,PACA_SIZE
937 bne 42b
Paul Mackerras371fefd2011-06-29 00:23:08 +0000938
939 /* Secondary threads wait for primary to do partition switch */
Paul Mackerras19ccb762011-07-23 17:42:46 +100094043: ld r4,VCPU_KVM(r9) /* pointer to struct kvm */
Paul Mackerras371fefd2011-06-29 00:23:08 +0000941 ld r5,HSTATE_KVM_VCORE(r13)
942 lwz r3,VCPU_PTID(r9)
943 cmpwi r3,0
944 beq 15f
945 HMT_LOW
94613: lbz r3,VCORE_IN_GUEST(r5)
947 cmpwi r3,0
948 bne 13b
949 HMT_MEDIUM
950 b 16f
951
952 /* Primary thread waits for all the secondaries to exit guest */
95315: lwz r3,VCORE_ENTRY_EXIT(r5)
954 srwi r0,r3,8
955 clrldi r3,r3,56
956 cmpw r3,r0
957 bne 15b
958 isync
959
960 /* Primary thread switches back to host partition */
Paul Mackerrasde56a942011-06-29 00:21:34 +0000961 ld r6,KVM_HOST_SDR1(r4)
962 lwz r7,KVM_HOST_LPID(r4)
963 li r8,LPID_RSVD /* switch to reserved LPID */
964 mtspr SPRN_LPID,r8
965 ptesync
966 mtspr SPRN_SDR1,r6 /* switch to partition page table */
967 mtspr SPRN_LPID,r7
968 isync
Paul Mackerras93b0f4d2013-09-06 13:17:46 +1000969
970 /* Subtract timebase offset from timebase */
971 ld r8,VCORE_TB_OFFSET(r5)
972 cmpdi r8,0
973 beq 17f
974 mftb r6 /* current host timebase */
975 subf r8,r8,r6
976 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
977 mftb r7 /* check if lower 24 bits overflowed */
978 clrldi r6,r6,40
979 clrldi r7,r7,40
980 cmpld r7,r6
981 bge 17f
982 addis r8,r8,0x100 /* if so, increment upper 40 bits */
983 mtspr SPRN_TBU40,r8
984
985 /* Signal secondary CPUs to continue */
98617: li r0,0
Paul Mackerras371fefd2011-06-29 00:23:08 +0000987 stb r0,VCORE_IN_GUEST(r5)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000988 lis r8,0x7fff /* MAX_INT@h */
989 mtspr SPRN_HDEC,r8
990
Paul Mackerras371fefd2011-06-29 00:23:08 +000099116: ld r8,KVM_HOST_LPCR(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000992 mtspr SPRN_LPCR,r8
993 isync
Paul Mackerras9e368f22011-06-29 00:40:08 +0000994 b 33f
995
996 /*
997 * PPC970 guest -> host partition switch code.
998 * We have to lock against concurrent tlbies, and
999 * we have to flush the whole TLB.
1000 */
100132: ld r4,VCPU_KVM(r9) /* pointer to struct kvm */
1002
1003 /* Take the guest's tlbie_lock */
Anton Blanchard54bb7f42013-08-07 02:01:51 +10001004#ifdef __BIG_ENDIAN__
Paul Mackerras9e368f22011-06-29 00:40:08 +00001005 lwz r8,PACA_LOCK_TOKEN(r13)
Anton Blanchard54bb7f42013-08-07 02:01:51 +10001006#else
1007 lwz r8,PACAPACAINDEX(r13)
1008#endif
Paul Mackerras9e368f22011-06-29 00:40:08 +00001009 addi r3,r4,KVM_TLBIE_LOCK
101024: lwarx r0,0,r3
1011 cmpwi r0,0
1012 bne 24b
1013 stwcx. r8,0,r3
1014 bne 24b
1015 isync
1016
1017 ld r7,KVM_HOST_LPCR(r4) /* use kvm->arch.host_lpcr for HID4 */
1018 li r0,0x18f
1019 rotldi r0,r0,HID4_LPID5_SH /* all lpid bits in HID4 = 1 */
1020 or r0,r7,r0
1021 ptesync
1022 sync
1023 mtspr SPRN_HID4,r0 /* switch to reserved LPID */
1024 isync
1025 li r0,0
1026 stw r0,0(r3) /* drop guest tlbie_lock */
1027
1028 /* invalidate the whole TLB */
1029 li r0,256
1030 mtctr r0
1031 li r6,0
103225: tlbiel r6
1033 addi r6,r6,0x1000
1034 bdnz 25b
1035 ptesync
1036
1037 /* take native_tlbie_lock */
1038 ld r3,toc_tlbie_lock@toc(2)
103924: lwarx r0,0,r3
1040 cmpwi r0,0
1041 bne 24b
1042 stwcx. r8,0,r3
1043 bne 24b
1044 isync
1045
1046 ld r6,KVM_HOST_SDR1(r4)
1047 mtspr SPRN_SDR1,r6 /* switch to host page table */
1048
1049 /* Set up host HID4 value */
1050 sync
1051 mtspr SPRN_HID4,r7
1052 isync
1053 li r0,0
1054 stw r0,0(r3) /* drop native_tlbie_lock */
1055
1056 lis r8,0x7fff /* MAX_INT@h */
1057 mtspr SPRN_HDEC,r8
1058
1059 /* Disable HDEC interrupts */
1060 mfspr r0,SPRN_HID0
1061 li r3,0
1062 rldimi r0,r3, HID0_HDICE_SH, 64-HID0_HDICE_SH-1
1063 sync
1064 mtspr SPRN_HID0,r0
1065 mfspr r0,SPRN_HID0
1066 mfspr r0,SPRN_HID0
1067 mfspr r0,SPRN_HID0
1068 mfspr r0,SPRN_HID0
1069 mfspr r0,SPRN_HID0
1070 mfspr r0,SPRN_HID0
Paul Mackerrasde56a942011-06-29 00:21:34 +00001071
1072 /* load host SLB entries */
Paul Mackerras9e368f22011-06-29 00:40:08 +0000107333: ld r8,PACA_SLBSHADOWPTR(r13)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001074
1075 .rept SLB_NUM_BOLTED
1076 ld r5,SLBSHADOW_SAVEAREA(r8)
1077 ld r6,SLBSHADOW_SAVEAREA+8(r8)
1078 andis. r7,r5,SLB_ESID_V@h
1079 beq 1f
1080 slbmte r6,r5
10811: addi r8,r8,16
1082 .endr
1083
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001084 /* Save DEC */
1085 mfspr r5,SPRN_DEC
1086 mftb r6
1087 extsw r5,r5
1088 add r5,r5,r6
1089 std r5,VCPU_DEC_EXPIRES(r9)
1090
Paul Mackerrasde56a942011-06-29 00:21:34 +00001091 /* Save and reset AMR and UAMOR before turning on the MMU */
Paul Mackerras9e368f22011-06-29 00:40:08 +00001092BEGIN_FTR_SECTION
Paul Mackerrasde56a942011-06-29 00:21:34 +00001093 mfspr r5,SPRN_AMR
1094 mfspr r6,SPRN_UAMOR
1095 std r5,VCPU_AMR(r9)
1096 std r6,VCPU_UAMOR(r9)
1097 li r6,0
1098 mtspr SPRN_AMR,r6
Paul Mackerras9e368f22011-06-29 00:40:08 +00001099END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001100
Paul Mackerrasde56a942011-06-29 00:21:34 +00001101 /* Switch DSCR back to host value */
Paul Mackerras9e368f22011-06-29 00:40:08 +00001102BEGIN_FTR_SECTION
Paul Mackerrasde56a942011-06-29 00:21:34 +00001103 mfspr r8, SPRN_DSCR
1104 ld r7, HSTATE_DSCR(r13)
1105 std r8, VCPU_DSCR(r7)
1106 mtspr SPRN_DSCR, r7
Paul Mackerras9e368f22011-06-29 00:40:08 +00001107END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001108
1109 /* Save non-volatile GPRs */
Michael Neulingc75df6f2012-06-25 13:33:10 +00001110 std r14, VCPU_GPR(R14)(r9)
1111 std r15, VCPU_GPR(R15)(r9)
1112 std r16, VCPU_GPR(R16)(r9)
1113 std r17, VCPU_GPR(R17)(r9)
1114 std r18, VCPU_GPR(R18)(r9)
1115 std r19, VCPU_GPR(R19)(r9)
1116 std r20, VCPU_GPR(R20)(r9)
1117 std r21, VCPU_GPR(R21)(r9)
1118 std r22, VCPU_GPR(R22)(r9)
1119 std r23, VCPU_GPR(R23)(r9)
1120 std r24, VCPU_GPR(R24)(r9)
1121 std r25, VCPU_GPR(R25)(r9)
1122 std r26, VCPU_GPR(R26)(r9)
1123 std r27, VCPU_GPR(R27)(r9)
1124 std r28, VCPU_GPR(R28)(r9)
1125 std r29, VCPU_GPR(R29)(r9)
1126 std r30, VCPU_GPR(R30)(r9)
1127 std r31, VCPU_GPR(R31)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001128
1129 /* Save SPRGs */
1130 mfspr r3, SPRN_SPRG0
1131 mfspr r4, SPRN_SPRG1
1132 mfspr r5, SPRN_SPRG2
1133 mfspr r6, SPRN_SPRG3
1134 std r3, VCPU_SPRG0(r9)
1135 std r4, VCPU_SPRG1(r9)
1136 std r5, VCPU_SPRG2(r9)
1137 std r6, VCPU_SPRG3(r9)
1138
Paul Mackerras89436332012-03-02 01:38:23 +00001139 /* save FP state */
1140 mr r3, r9
1141 bl .kvmppc_save_fp
1142
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001143 /* Increment yield count if they have a VPA */
1144 ld r8, VCPU_VPA(r9) /* do they have a VPA? */
1145 cmpdi r8, 0
1146 beq 25f
1147 lwz r3, LPPACA_YIELDCOUNT(r8)
1148 addi r3, r3, 1
1149 stw r3, LPPACA_YIELDCOUNT(r8)
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001150 li r3, 1
1151 stb r3, VCPU_VPA_DIRTY(r9)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000115225:
1153 /* Save PMU registers if requested */
1154 /* r8 and cr0.eq are live here */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001155 li r3, 1
1156 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
1157 mfspr r4, SPRN_MMCR0 /* save MMCR0 */
1158 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
Paul Mackerras89436332012-03-02 01:38:23 +00001159 mfspr r6, SPRN_MMCRA
1160BEGIN_FTR_SECTION
1161 /* On P7, clear MMCRA in order to disable SDAR updates */
1162 li r7, 0
1163 mtspr SPRN_MMCRA, r7
1164END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001165 isync
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001166 beq 21f /* if no VPA, save PMU stuff anyway */
1167 lbz r7, LPPACA_PMCINUSE(r8)
1168 cmpwi r7, 0 /* did they ask for PMU stuff to be saved? */
1169 bne 21f
1170 std r3, VCPU_MMCR(r9) /* if not, set saved MMCR0 to FC */
1171 b 22f
117221: mfspr r5, SPRN_MMCR1
Paul Mackerras14941782013-09-06 13:11:18 +10001173 mfspr r7, SPRN_SIAR
1174 mfspr r8, SPRN_SDAR
Paul Mackerrasde56a942011-06-29 00:21:34 +00001175 std r4, VCPU_MMCR(r9)
1176 std r5, VCPU_MMCR + 8(r9)
1177 std r6, VCPU_MMCR + 16(r9)
Paul Mackerras14941782013-09-06 13:11:18 +10001178 std r7, VCPU_SIAR(r9)
1179 std r8, VCPU_SDAR(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001180 mfspr r3, SPRN_PMC1
1181 mfspr r4, SPRN_PMC2
1182 mfspr r5, SPRN_PMC3
1183 mfspr r6, SPRN_PMC4
1184 mfspr r7, SPRN_PMC5
1185 mfspr r8, SPRN_PMC6
Paul Mackerras9e368f22011-06-29 00:40:08 +00001186BEGIN_FTR_SECTION
1187 mfspr r10, SPRN_PMC7
1188 mfspr r11, SPRN_PMC8
1189END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001190 stw r3, VCPU_PMC(r9)
1191 stw r4, VCPU_PMC + 4(r9)
1192 stw r5, VCPU_PMC + 8(r9)
1193 stw r6, VCPU_PMC + 12(r9)
1194 stw r7, VCPU_PMC + 16(r9)
1195 stw r8, VCPU_PMC + 20(r9)
Paul Mackerras9e368f22011-06-29 00:40:08 +00001196BEGIN_FTR_SECTION
1197 stw r10, VCPU_PMC + 24(r9)
1198 stw r11, VCPU_PMC + 28(r9)
1199END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000120022:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001201
Paul Mackerras9e368f22011-06-29 00:40:08 +00001202 /* Secondary threads go off to take a nap on POWER7 */
1203BEGIN_FTR_SECTION
Paul Mackerras89436332012-03-02 01:38:23 +00001204 lwz r0,VCPU_PTID(r9)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001205 cmpwi r0,0
1206 bne secondary_nap
Paul Mackerras9e368f22011-06-29 00:40:08 +00001207END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001208
Paul Mackerras89436332012-03-02 01:38:23 +00001209 /* Restore host DABR and DABRX */
1210 ld r5,HSTATE_DABR(r13)
1211 li r6,7
1212 mtspr SPRN_DABR,r5
1213 mtspr SPRN_DABRX,r6
1214
Anton Blanchard18ad51d2012-07-04 20:37:11 +00001215 /* Restore SPRG3 */
Mihai Caraman01272622012-09-06 02:49:44 +00001216 ld r3,PACA_SPRG3(r13)
Anton Blanchard18ad51d2012-07-04 20:37:11 +00001217 mtspr SPRN_SPRG3,r3
1218
Paul Mackerrasde56a942011-06-29 00:21:34 +00001219 /*
1220 * Reload DEC. HDEC interrupts were disabled when
1221 * we reloaded the host's LPCR value.
1222 */
1223 ld r3, HSTATE_DECEXP(r13)
1224 mftb r4
1225 subf r4, r4, r3
1226 mtspr SPRN_DEC, r4
1227
1228 /* Reload the host's PMU registers */
1229 ld r3, PACALPPACAPTR(r13) /* is the host using the PMU? */
1230 lbz r4, LPPACA_PMCINUSE(r3)
1231 cmpwi r4, 0
1232 beq 23f /* skip if not */
1233 lwz r3, HSTATE_PMC(r13)
1234 lwz r4, HSTATE_PMC + 4(r13)
1235 lwz r5, HSTATE_PMC + 8(r13)
1236 lwz r6, HSTATE_PMC + 12(r13)
1237 lwz r8, HSTATE_PMC + 16(r13)
1238 lwz r9, HSTATE_PMC + 20(r13)
Paul Mackerras9e368f22011-06-29 00:40:08 +00001239BEGIN_FTR_SECTION
1240 lwz r10, HSTATE_PMC + 24(r13)
1241 lwz r11, HSTATE_PMC + 28(r13)
1242END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001243 mtspr SPRN_PMC1, r3
1244 mtspr SPRN_PMC2, r4
1245 mtspr SPRN_PMC3, r5
1246 mtspr SPRN_PMC4, r6
1247 mtspr SPRN_PMC5, r8
1248 mtspr SPRN_PMC6, r9
Paul Mackerras9e368f22011-06-29 00:40:08 +00001249BEGIN_FTR_SECTION
1250 mtspr SPRN_PMC7, r10
1251 mtspr SPRN_PMC8, r11
1252END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001253 ld r3, HSTATE_MMCR(r13)
1254 ld r4, HSTATE_MMCR + 8(r13)
1255 ld r5, HSTATE_MMCR + 16(r13)
1256 mtspr SPRN_MMCR1, r4
1257 mtspr SPRN_MMCRA, r5
1258 mtspr SPRN_MMCR0, r3
1259 isync
126023:
1261 /*
1262 * For external and machine check interrupts, we need
1263 * to call the Linux handler to process the interrupt.
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001264 * We do that by jumping to absolute address 0x500 for
1265 * external interrupts, or the machine_check_fwnmi label
1266 * for machine checks (since firmware might have patched
1267 * the vector area at 0x200). The [h]rfid at the end of the
Paul Mackerrasde56a942011-06-29 00:21:34 +00001268 * handler will return to the book3s_hv_interrupts.S code.
1269 * For other interrupts we do the rfid to get back
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001270 * to the book3s_hv_interrupts.S code here.
Paul Mackerrasde56a942011-06-29 00:21:34 +00001271 */
1272 ld r8, HSTATE_VMHANDLER(r13)
1273 ld r7, HSTATE_HOST_MSR(r13)
1274
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001275 cmpwi cr1, r12, BOOK3S_INTERRUPT_MACHINE_CHECK
Paul Mackerrasde56a942011-06-29 00:21:34 +00001276 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001277BEGIN_FTR_SECTION
Paul Mackerrasde56a942011-06-29 00:21:34 +00001278 beq 11f
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001279END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001280
1281 /* RFI into the highmem handler, or branch to interrupt handler */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001282 mfmsr r6
Paul Mackerrasde56a942011-06-29 00:21:34 +00001283 li r0, MSR_RI
1284 andc r6, r6, r0
1285 mtmsrd r6, 1 /* Clear RI in MSR */
1286 mtsrr0 r8
1287 mtsrr1 r7
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001288 beqa 0x500 /* external interrupt (PPC970) */
1289 beq cr1, 13f /* machine check */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001290 RFI
1291
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001292 /* On POWER7, we have external interrupts set to use HSRR0/1 */
129311: mtspr SPRN_HSRR0, r8
Paul Mackerrasde56a942011-06-29 00:21:34 +00001294 mtspr SPRN_HSRR1, r7
1295 ba 0x500
1296
Paul Mackerrasb4072df2012-11-23 22:37:50 +0000129713: b machine_check_fwnmi
1298
Paul Mackerras697d3892011-12-12 12:36:37 +00001299/*
1300 * Check whether an HDSI is an HPTE not found fault or something else.
1301 * If it is an HPTE not found fault that is due to the guest accessing
1302 * a page that they have mapped but which we have paged out, then
1303 * we continue on with the guest exit path. In all other cases,
1304 * reflect the HDSI to the guest as a DSI.
1305 */
1306kvmppc_hdsi:
1307 mfspr r4, SPRN_HDAR
1308 mfspr r6, SPRN_HDSISR
Paul Mackerras4cf302b2011-12-12 12:38:51 +00001309 /* HPTE not found fault or protection fault? */
1310 andis. r0, r6, (DSISR_NOHPTE | DSISR_PROTFAULT)@h
Paul Mackerras697d3892011-12-12 12:36:37 +00001311 beq 1f /* if not, send it to the guest */
1312 andi. r0, r11, MSR_DR /* data relocation enabled? */
1313 beq 3f
1314 clrrdi r0, r4, 28
Michael Neulingc75df6f2012-06-25 13:33:10 +00001315 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
Paul Mackerras697d3892011-12-12 12:36:37 +00001316 bne 1f /* if no SLB entry found */
13174: std r4, VCPU_FAULT_DAR(r9)
1318 stw r6, VCPU_FAULT_DSISR(r9)
1319
1320 /* Search the hash table. */
1321 mr r3, r9 /* vcpu pointer */
Paul Mackerras342d3db2011-12-12 12:38:05 +00001322 li r7, 1 /* data fault */
Paul Mackerras697d3892011-12-12 12:36:37 +00001323 bl .kvmppc_hpte_hv_fault
1324 ld r9, HSTATE_KVM_VCPU(r13)
1325 ld r10, VCPU_PC(r9)
1326 ld r11, VCPU_MSR(r9)
1327 li r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1328 cmpdi r3, 0 /* retry the instruction */
1329 beq 6f
1330 cmpdi r3, -1 /* handle in kernel mode */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001331 beq guest_exit_cont
Paul Mackerras697d3892011-12-12 12:36:37 +00001332 cmpdi r3, -2 /* MMIO emulation; need instr word */
1333 beq 2f
1334
1335 /* Synthesize a DSI for the guest */
1336 ld r4, VCPU_FAULT_DAR(r9)
1337 mr r6, r3
13381: mtspr SPRN_DAR, r4
1339 mtspr SPRN_DSISR, r6
1340 mtspr SPRN_SRR0, r10
1341 mtspr SPRN_SRR1, r11
1342 li r10, BOOK3S_INTERRUPT_DATA_STORAGE
1343 li r11, (MSR_ME << 1) | 1 /* synthesize MSR_SF | MSR_ME */
1344 rotldi r11, r11, 63
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001345fast_interrupt_c_return:
Paul Mackerras697d3892011-12-12 12:36:37 +000013466: ld r7, VCPU_CTR(r9)
1347 lwz r8, VCPU_XER(r9)
1348 mtctr r7
1349 mtxer r8
1350 mr r4, r9
1351 b fast_guest_return
1352
13533: ld r5, VCPU_KVM(r9) /* not relocated, use VRMA */
1354 ld r5, KVM_VRMA_SLB_V(r5)
1355 b 4b
1356
1357 /* If this is for emulated MMIO, load the instruction word */
13582: li r8, KVM_INST_FETCH_FAILED /* In case lwz faults */
1359
1360 /* Set guest mode to 'jump over instruction' so if lwz faults
1361 * we'll just continue at the next IP. */
1362 li r0, KVM_GUEST_MODE_SKIP
1363 stb r0, HSTATE_IN_GUEST(r13)
1364
1365 /* Do the access with MSR:DR enabled */
1366 mfmsr r3
1367 ori r4, r3, MSR_DR /* Enable paging for data */
1368 mtmsrd r4
1369 lwz r8, 0(r10)
1370 mtmsrd r3
1371
1372 /* Store the result */
1373 stw r8, VCPU_LAST_INST(r9)
1374
1375 /* Unset guest mode. */
1376 li r0, KVM_GUEST_MODE_NONE
1377 stb r0, HSTATE_IN_GUEST(r13)
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001378 b guest_exit_cont
Paul Mackerrasde56a942011-06-29 00:21:34 +00001379
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001380/*
Paul Mackerras342d3db2011-12-12 12:38:05 +00001381 * Similarly for an HISI, reflect it to the guest as an ISI unless
1382 * it is an HPTE not found fault for a page that we have paged out.
1383 */
1384kvmppc_hisi:
1385 andis. r0, r11, SRR1_ISI_NOPT@h
1386 beq 1f
1387 andi. r0, r11, MSR_IR /* instruction relocation enabled? */
1388 beq 3f
1389 clrrdi r0, r10, 28
Michael Neulingc75df6f2012-06-25 13:33:10 +00001390 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
Paul Mackerras342d3db2011-12-12 12:38:05 +00001391 bne 1f /* if no SLB entry found */
13924:
1393 /* Search the hash table. */
1394 mr r3, r9 /* vcpu pointer */
1395 mr r4, r10
1396 mr r6, r11
1397 li r7, 0 /* instruction fault */
1398 bl .kvmppc_hpte_hv_fault
1399 ld r9, HSTATE_KVM_VCPU(r13)
1400 ld r10, VCPU_PC(r9)
1401 ld r11, VCPU_MSR(r9)
1402 li r12, BOOK3S_INTERRUPT_H_INST_STORAGE
1403 cmpdi r3, 0 /* retry the instruction */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001404 beq fast_interrupt_c_return
Paul Mackerras342d3db2011-12-12 12:38:05 +00001405 cmpdi r3, -1 /* handle in kernel mode */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001406 beq guest_exit_cont
Paul Mackerras342d3db2011-12-12 12:38:05 +00001407
1408 /* Synthesize an ISI for the guest */
1409 mr r11, r3
14101: mtspr SPRN_SRR0, r10
1411 mtspr SPRN_SRR1, r11
1412 li r10, BOOK3S_INTERRUPT_INST_STORAGE
1413 li r11, (MSR_ME << 1) | 1 /* synthesize MSR_SF | MSR_ME */
1414 rotldi r11, r11, 63
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001415 b fast_interrupt_c_return
Paul Mackerras342d3db2011-12-12 12:38:05 +00001416
14173: ld r6, VCPU_KVM(r9) /* not relocated, use VRMA */
1418 ld r5, KVM_VRMA_SLB_V(r6)
1419 b 4b
1420
1421/*
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001422 * Try to handle an hcall in real mode.
1423 * Returns to the guest if we handle it, or continues on up to
1424 * the kernel if we can't (i.e. if we don't have a handler for
1425 * it, or if the handler returns H_TOO_HARD).
1426 */
1427 .globl hcall_try_real_mode
1428hcall_try_real_mode:
Michael Neulingc75df6f2012-06-25 13:33:10 +00001429 ld r3,VCPU_GPR(R3)(r9)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001430 andi. r0,r11,MSR_PR
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001431 bne guest_exit_cont
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001432 clrrdi r3,r3,2
1433 cmpldi r3,hcall_real_table_end - hcall_real_table
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001434 bge guest_exit_cont
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001435 LOAD_REG_ADDR(r4, hcall_real_table)
Paul Mackerras4baa1d82013-07-08 20:09:53 +10001436 lwax r3,r3,r4
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001437 cmpwi r3,0
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001438 beq guest_exit_cont
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001439 add r3,r3,r4
1440 mtctr r3
1441 mr r3,r9 /* get vcpu pointer */
Michael Neulingc75df6f2012-06-25 13:33:10 +00001442 ld r4,VCPU_GPR(R4)(r9)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001443 bctrl
1444 cmpdi r3,H_TOO_HARD
1445 beq hcall_real_fallback
1446 ld r4,HSTATE_KVM_VCPU(r13)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001447 std r3,VCPU_GPR(R3)(r4)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001448 ld r10,VCPU_PC(r4)
1449 ld r11,VCPU_MSR(r4)
1450 b fast_guest_return
1451
1452 /* We've attempted a real mode hcall, but it's punted it back
1453 * to userspace. We need to restore some clobbered volatiles
1454 * before resuming the pass-it-to-qemu path */
1455hcall_real_fallback:
1456 li r12,BOOK3S_INTERRUPT_SYSCALL
1457 ld r9, HSTATE_KVM_VCPU(r13)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001458
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001459 b guest_exit_cont
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001460
1461 .globl hcall_real_table
1462hcall_real_table:
1463 .long 0 /* 0 - unused */
1464 .long .kvmppc_h_remove - hcall_real_table
1465 .long .kvmppc_h_enter - hcall_real_table
1466 .long .kvmppc_h_read - hcall_real_table
1467 .long 0 /* 0x10 - H_CLEAR_MOD */
1468 .long 0 /* 0x14 - H_CLEAR_REF */
1469 .long .kvmppc_h_protect - hcall_real_table
1470 .long 0 /* 0x1c - H_GET_TCE */
David Gibson54738c02011-06-29 00:22:41 +00001471 .long .kvmppc_h_put_tce - hcall_real_table
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001472 .long 0 /* 0x24 - H_SET_SPRG0 */
1473 .long .kvmppc_h_set_dabr - hcall_real_table
1474 .long 0 /* 0x2c */
1475 .long 0 /* 0x30 */
1476 .long 0 /* 0x34 */
1477 .long 0 /* 0x38 */
1478 .long 0 /* 0x3c */
1479 .long 0 /* 0x40 */
1480 .long 0 /* 0x44 */
1481 .long 0 /* 0x48 */
1482 .long 0 /* 0x4c */
1483 .long 0 /* 0x50 */
1484 .long 0 /* 0x54 */
1485 .long 0 /* 0x58 */
1486 .long 0 /* 0x5c */
1487 .long 0 /* 0x60 */
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +00001488#ifdef CONFIG_KVM_XICS
1489 .long .kvmppc_rm_h_eoi - hcall_real_table
1490 .long .kvmppc_rm_h_cppr - hcall_real_table
1491 .long .kvmppc_rm_h_ipi - hcall_real_table
1492 .long 0 /* 0x70 - H_IPOLL */
1493 .long .kvmppc_rm_h_xirr - hcall_real_table
1494#else
1495 .long 0 /* 0x64 - H_EOI */
1496 .long 0 /* 0x68 - H_CPPR */
1497 .long 0 /* 0x6c - H_IPI */
1498 .long 0 /* 0x70 - H_IPOLL */
1499 .long 0 /* 0x74 - H_XIRR */
1500#endif
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001501 .long 0 /* 0x78 */
1502 .long 0 /* 0x7c */
1503 .long 0 /* 0x80 */
1504 .long 0 /* 0x84 */
1505 .long 0 /* 0x88 */
1506 .long 0 /* 0x8c */
1507 .long 0 /* 0x90 */
1508 .long 0 /* 0x94 */
1509 .long 0 /* 0x98 */
1510 .long 0 /* 0x9c */
1511 .long 0 /* 0xa0 */
1512 .long 0 /* 0xa4 */
1513 .long 0 /* 0xa8 */
1514 .long 0 /* 0xac */
1515 .long 0 /* 0xb0 */
1516 .long 0 /* 0xb4 */
1517 .long 0 /* 0xb8 */
1518 .long 0 /* 0xbc */
1519 .long 0 /* 0xc0 */
1520 .long 0 /* 0xc4 */
1521 .long 0 /* 0xc8 */
1522 .long 0 /* 0xcc */
1523 .long 0 /* 0xd0 */
1524 .long 0 /* 0xd4 */
1525 .long 0 /* 0xd8 */
1526 .long 0 /* 0xdc */
Paul Mackerras19ccb762011-07-23 17:42:46 +10001527 .long .kvmppc_h_cede - hcall_real_table
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001528 .long 0 /* 0xe4 */
1529 .long 0 /* 0xe8 */
1530 .long 0 /* 0xec */
1531 .long 0 /* 0xf0 */
1532 .long 0 /* 0xf4 */
1533 .long 0 /* 0xf8 */
1534 .long 0 /* 0xfc */
1535 .long 0 /* 0x100 */
1536 .long 0 /* 0x104 */
1537 .long 0 /* 0x108 */
1538 .long 0 /* 0x10c */
1539 .long 0 /* 0x110 */
1540 .long 0 /* 0x114 */
1541 .long 0 /* 0x118 */
1542 .long 0 /* 0x11c */
1543 .long 0 /* 0x120 */
1544 .long .kvmppc_h_bulk_remove - hcall_real_table
1545hcall_real_table_end:
1546
Paul Mackerrasde56a942011-06-29 00:21:34 +00001547ignore_hdec:
1548 mr r4,r9
1549 b fast_guest_return
1550
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001551_GLOBAL(kvmppc_h_set_dabr)
1552 std r4,VCPU_DABR(r3)
Paul Mackerras89436332012-03-02 01:38:23 +00001553 /* Work around P7 bug where DABR can get corrupted on mtspr */
15541: mtspr SPRN_DABR,r4
1555 mfspr r5, SPRN_DABR
1556 cmpd r4, r5
1557 bne 1b
1558 isync
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001559 li r3,0
1560 blr
1561
Paul Mackerras19ccb762011-07-23 17:42:46 +10001562_GLOBAL(kvmppc_h_cede)
1563 ori r11,r11,MSR_EE
1564 std r11,VCPU_MSR(r3)
1565 li r0,1
1566 stb r0,VCPU_CEDED(r3)
1567 sync /* order setting ceded vs. testing prodded */
1568 lbz r5,VCPU_PRODDED(r3)
1569 cmpwi r5,0
Paul Mackerras04f995a2012-08-06 00:03:28 +00001570 bne kvm_cede_prodded
Paul Mackerras19ccb762011-07-23 17:42:46 +10001571 li r0,0 /* set trap to 0 to say hcall is handled */
1572 stw r0,VCPU_TRAP(r3)
1573 li r0,H_SUCCESS
Michael Neulingc75df6f2012-06-25 13:33:10 +00001574 std r0,VCPU_GPR(R3)(r3)
Paul Mackerras19ccb762011-07-23 17:42:46 +10001575BEGIN_FTR_SECTION
Paul Mackerras04f995a2012-08-06 00:03:28 +00001576 b kvm_cede_exit /* just send it up to host on 970 */
Paul Mackerras19ccb762011-07-23 17:42:46 +10001577END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_206)
1578
1579 /*
1580 * Set our bit in the bitmask of napping threads unless all the
1581 * other threads are already napping, in which case we send this
1582 * up to the host.
1583 */
1584 ld r5,HSTATE_KVM_VCORE(r13)
1585 lwz r6,VCPU_PTID(r3)
1586 lwz r8,VCORE_ENTRY_EXIT(r5)
1587 clrldi r8,r8,56
1588 li r0,1
1589 sld r0,r0,r6
1590 addi r6,r5,VCORE_NAPPING_THREADS
159131: lwarx r4,0,r6
1592 or r4,r4,r0
Michael Neulingc75df6f2012-06-25 13:33:10 +00001593 PPC_POPCNTW(R7,R4)
Paul Mackerras19ccb762011-07-23 17:42:46 +10001594 cmpw r7,r8
Paul Mackerras04f995a2012-08-06 00:03:28 +00001595 bge kvm_cede_exit
Paul Mackerras19ccb762011-07-23 17:42:46 +10001596 stwcx. r4,0,r6
1597 bne 31b
1598 li r0,1
1599 stb r0,HSTATE_NAPPING(r13)
1600 /* order napping_threads update vs testing entry_exit_count */
1601 lwsync
1602 mr r4,r3
1603 lwz r7,VCORE_ENTRY_EXIT(r5)
1604 cmpwi r7,0x100
1605 bge 33f /* another thread already exiting */
1606
1607/*
1608 * Although not specifically required by the architecture, POWER7
1609 * preserves the following registers in nap mode, even if an SMT mode
1610 * switch occurs: SLB entries, PURR, SPURR, AMOR, UAMOR, AMR, SPRG0-3,
1611 * DAR, DSISR, DABR, DABRX, DSCR, PMCx, MMCRx, SIAR, SDAR.
1612 */
1613 /* Save non-volatile GPRs */
Michael Neulingc75df6f2012-06-25 13:33:10 +00001614 std r14, VCPU_GPR(R14)(r3)
1615 std r15, VCPU_GPR(R15)(r3)
1616 std r16, VCPU_GPR(R16)(r3)
1617 std r17, VCPU_GPR(R17)(r3)
1618 std r18, VCPU_GPR(R18)(r3)
1619 std r19, VCPU_GPR(R19)(r3)
1620 std r20, VCPU_GPR(R20)(r3)
1621 std r21, VCPU_GPR(R21)(r3)
1622 std r22, VCPU_GPR(R22)(r3)
1623 std r23, VCPU_GPR(R23)(r3)
1624 std r24, VCPU_GPR(R24)(r3)
1625 std r25, VCPU_GPR(R25)(r3)
1626 std r26, VCPU_GPR(R26)(r3)
1627 std r27, VCPU_GPR(R27)(r3)
1628 std r28, VCPU_GPR(R28)(r3)
1629 std r29, VCPU_GPR(R29)(r3)
1630 std r30, VCPU_GPR(R30)(r3)
1631 std r31, VCPU_GPR(R31)(r3)
Paul Mackerras19ccb762011-07-23 17:42:46 +10001632
1633 /* save FP state */
1634 bl .kvmppc_save_fp
1635
1636 /*
1637 * Take a nap until a decrementer or external interrupt occurs,
1638 * with PECE1 (wake on decr) and PECE0 (wake on external) set in LPCR
1639 */
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001640 li r0,1
1641 stb r0,HSTATE_HWTHREAD_REQ(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10001642 mfspr r5,SPRN_LPCR
1643 ori r5,r5,LPCR_PECE0 | LPCR_PECE1
1644 mtspr SPRN_LPCR,r5
1645 isync
1646 li r0, 0
1647 std r0, HSTATE_SCRATCH0(r13)
1648 ptesync
1649 ld r0, HSTATE_SCRATCH0(r13)
16501: cmpd r0, r0
1651 bne 1b
1652 nap
1653 b .
1654
1655kvm_end_cede:
Paul Mackerras4619ac82013-04-17 20:31:41 +00001656 /* get vcpu pointer */
1657 ld r4, HSTATE_KVM_VCPU(r13)
1658
Paul Mackerras19ccb762011-07-23 17:42:46 +10001659 /* Woken by external or decrementer interrupt */
1660 ld r1, HSTATE_HOST_R1(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10001661
Paul Mackerras19ccb762011-07-23 17:42:46 +10001662 /* load up FP state */
1663 bl kvmppc_load_fp
1664
1665 /* Load NV GPRS */
Michael Neulingc75df6f2012-06-25 13:33:10 +00001666 ld r14, VCPU_GPR(R14)(r4)
1667 ld r15, VCPU_GPR(R15)(r4)
1668 ld r16, VCPU_GPR(R16)(r4)
1669 ld r17, VCPU_GPR(R17)(r4)
1670 ld r18, VCPU_GPR(R18)(r4)
1671 ld r19, VCPU_GPR(R19)(r4)
1672 ld r20, VCPU_GPR(R20)(r4)
1673 ld r21, VCPU_GPR(R21)(r4)
1674 ld r22, VCPU_GPR(R22)(r4)
1675 ld r23, VCPU_GPR(R23)(r4)
1676 ld r24, VCPU_GPR(R24)(r4)
1677 ld r25, VCPU_GPR(R25)(r4)
1678 ld r26, VCPU_GPR(R26)(r4)
1679 ld r27, VCPU_GPR(R27)(r4)
1680 ld r28, VCPU_GPR(R28)(r4)
1681 ld r29, VCPU_GPR(R29)(r4)
1682 ld r30, VCPU_GPR(R30)(r4)
1683 ld r31, VCPU_GPR(R31)(r4)
Paul Mackerras19ccb762011-07-23 17:42:46 +10001684
1685 /* clear our bit in vcore->napping_threads */
168633: ld r5,HSTATE_KVM_VCORE(r13)
1687 lwz r3,VCPU_PTID(r4)
1688 li r0,1
1689 sld r0,r0,r3
1690 addi r6,r5,VCORE_NAPPING_THREADS
169132: lwarx r7,0,r6
1692 andc r7,r7,r0
1693 stwcx. r7,0,r6
1694 bne 32b
1695 li r0,0
1696 stb r0,HSTATE_NAPPING(r13)
1697
Paul Mackerras4619ac82013-04-17 20:31:41 +00001698 /* Check the wake reason in SRR1 to see why we got here */
1699 mfspr r3, SPRN_SRR1
1700 rlwinm r3, r3, 44-31, 0x7 /* extract wake reason field */
1701 cmpwi r3, 4 /* was it an external interrupt? */
1702 li r12, BOOK3S_INTERRUPT_EXTERNAL
1703 mr r9, r4
1704 ld r10, VCPU_PC(r9)
1705 ld r11, VCPU_MSR(r9)
1706 beq do_ext_interrupt /* if so */
1707
Paul Mackerras19ccb762011-07-23 17:42:46 +10001708 /* see if any other thread is already exiting */
1709 lwz r0,VCORE_ENTRY_EXIT(r5)
1710 cmpwi r0,0x100
1711 blt kvmppc_cede_reentry /* if not go back to guest */
1712
1713 /* some threads are exiting, so go to the guest exit path */
1714 b hcall_real_fallback
1715
1716 /* cede when already previously prodded case */
Paul Mackerras04f995a2012-08-06 00:03:28 +00001717kvm_cede_prodded:
1718 li r0,0
Paul Mackerras19ccb762011-07-23 17:42:46 +10001719 stb r0,VCPU_PRODDED(r3)
1720 sync /* order testing prodded vs. clearing ceded */
1721 stb r0,VCPU_CEDED(r3)
1722 li r3,H_SUCCESS
1723 blr
1724
1725 /* we've ceded but we want to give control to the host */
Paul Mackerras04f995a2012-08-06 00:03:28 +00001726kvm_cede_exit:
Paul Mackerras4619ac82013-04-17 20:31:41 +00001727 b hcall_real_fallback
Paul Mackerras19ccb762011-07-23 17:42:46 +10001728
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001729 /* Try to handle a machine check in real mode */
1730machine_check_realmode:
1731 mr r3, r9 /* get vcpu pointer */
1732 bl .kvmppc_realmode_machine_check
1733 nop
1734 cmpdi r3, 0 /* continue exiting from guest? */
1735 ld r9, HSTATE_KVM_VCPU(r13)
1736 li r12, BOOK3S_INTERRUPT_MACHINE_CHECK
1737 beq mc_cont
1738 /* If not, deliver a machine check. SRR0/1 are already set */
1739 li r10, BOOK3S_INTERRUPT_MACHINE_CHECK
1740 li r11, (MSR_ME << 1) | 1 /* synthesize MSR_SF | MSR_ME */
1741 rotldi r11, r11, 63
1742 b fast_interrupt_c_return
1743
Paul Mackerras371fefd2011-06-29 00:23:08 +00001744secondary_too_late:
1745 ld r5,HSTATE_KVM_VCORE(r13)
1746 HMT_LOW
174713: lbz r3,VCORE_IN_GUEST(r5)
1748 cmpwi r3,0
1749 bne 13b
1750 HMT_MEDIUM
1751 ld r11,PACA_SLBSHADOWPTR(r13)
1752
1753 .rept SLB_NUM_BOLTED
1754 ld r5,SLBSHADOW_SAVEAREA(r11)
1755 ld r6,SLBSHADOW_SAVEAREA+8(r11)
1756 andis. r7,r5,SLB_ESID_V@h
1757 beq 1f
1758 slbmte r6,r5
17591: addi r11,r11,16
1760 .endr
Paul Mackerras371fefd2011-06-29 00:23:08 +00001761
1762secondary_nap:
Paul Mackerras7b444c62012-10-15 01:16:14 +00001763 /* Clear our vcpu pointer so we don't come back in early */
1764 li r0, 0
1765 std r0, HSTATE_KVM_VCPU(r13)
1766 lwsync
Paul Mackerras19ccb762011-07-23 17:42:46 +10001767 /* Clear any pending IPI - assume we're a secondary thread */
1768 ld r5, HSTATE_XICS_PHYS(r13)
1769 li r7, XICS_XIRR
1770 lwzcix r3, r5, r7 /* ack any pending interrupt */
1771 rlwinm. r0, r3, 0, 0xffffff /* any pending? */
1772 beq 37f
1773 sync
Paul Mackerras371fefd2011-06-29 00:23:08 +00001774 li r0, 0xff
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +00001775 li r6, XICS_MFRR
Paul Mackerras19ccb762011-07-23 17:42:46 +10001776 stbcix r0, r5, r6 /* clear the IPI */
1777 stwcix r3, r5, r7 /* EOI it */
177837: sync
Paul Mackerras371fefd2011-06-29 00:23:08 +00001779
1780 /* increment the nap count and then go to nap mode */
1781 ld r4, HSTATE_KVM_VCORE(r13)
1782 addi r4, r4, VCORE_NAP_COUNT
1783 lwsync /* make previous updates visible */
178451: lwarx r3, 0, r4
1785 addi r3, r3, 1
1786 stwcx. r3, 0, r4
1787 bne 51b
Paul Mackerras371fefd2011-06-29 00:23:08 +00001788
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001789kvm_no_guest:
1790 li r0, KVM_HWTHREAD_IN_NAP
1791 stb r0, HSTATE_HWTHREAD_STATE(r13)
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001792
Paul Mackerras19ccb762011-07-23 17:42:46 +10001793 li r3, LPCR_PECE0
Paul Mackerras371fefd2011-06-29 00:23:08 +00001794 mfspr r4, SPRN_LPCR
Paul Mackerras19ccb762011-07-23 17:42:46 +10001795 rlwimi r4, r3, 0, LPCR_PECE0 | LPCR_PECE1
Paul Mackerras371fefd2011-06-29 00:23:08 +00001796 mtspr SPRN_LPCR, r4
Paul Mackerras19ccb762011-07-23 17:42:46 +10001797 isync
Paul Mackerras371fefd2011-06-29 00:23:08 +00001798 std r0, HSTATE_SCRATCH0(r13)
1799 ptesync
1800 ld r0, HSTATE_SCRATCH0(r13)
18011: cmpd r0, r0
1802 bne 1b
1803 nap
1804 b .
1805
Paul Mackerrasde56a942011-06-29 00:21:34 +00001806/*
1807 * Save away FP, VMX and VSX registers.
1808 * r3 = vcpu pointer
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001809 */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001810_GLOBAL(kvmppc_save_fp)
Paul Mackerras89436332012-03-02 01:38:23 +00001811 mfmsr r5
1812 ori r8,r5,MSR_FP
Paul Mackerrasde56a942011-06-29 00:21:34 +00001813#ifdef CONFIG_ALTIVEC
1814BEGIN_FTR_SECTION
1815 oris r8,r8,MSR_VEC@h
1816END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
1817#endif
1818#ifdef CONFIG_VSX
1819BEGIN_FTR_SECTION
1820 oris r8,r8,MSR_VSX@h
1821END_FTR_SECTION_IFSET(CPU_FTR_VSX)
1822#endif
1823 mtmsrd r8
1824 isync
1825#ifdef CONFIG_VSX
1826BEGIN_FTR_SECTION
1827 reg = 0
1828 .rept 32
1829 li r6,reg*16+VCPU_VSRS
Michael Neulingc75df6f2012-06-25 13:33:10 +00001830 STXVD2X(reg,R6,R3)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001831 reg = reg + 1
1832 .endr
1833FTR_SECTION_ELSE
1834#endif
1835 reg = 0
1836 .rept 32
1837 stfd reg,reg*8+VCPU_FPRS(r3)
1838 reg = reg + 1
1839 .endr
1840#ifdef CONFIG_VSX
1841ALT_FTR_SECTION_END_IFSET(CPU_FTR_VSX)
1842#endif
1843 mffs fr0
1844 stfd fr0,VCPU_FPSCR(r3)
1845
1846#ifdef CONFIG_ALTIVEC
1847BEGIN_FTR_SECTION
1848 reg = 0
1849 .rept 32
1850 li r6,reg*16+VCPU_VRS
1851 stvx reg,r6,r3
1852 reg = reg + 1
1853 .endr
1854 mfvscr vr0
1855 li r6,VCPU_VSCR
1856 stvx vr0,r6,r3
1857END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
1858#endif
1859 mfspr r6,SPRN_VRSAVE
1860 stw r6,VCPU_VRSAVE(r3)
Paul Mackerras89436332012-03-02 01:38:23 +00001861 mtmsrd r5
Paul Mackerrasde56a942011-06-29 00:21:34 +00001862 isync
1863 blr
1864
1865/*
1866 * Load up FP, VMX and VSX registers
1867 * r4 = vcpu pointer
1868 */
1869 .globl kvmppc_load_fp
1870kvmppc_load_fp:
1871 mfmsr r9
1872 ori r8,r9,MSR_FP
1873#ifdef CONFIG_ALTIVEC
1874BEGIN_FTR_SECTION
1875 oris r8,r8,MSR_VEC@h
1876END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
1877#endif
1878#ifdef CONFIG_VSX
1879BEGIN_FTR_SECTION
1880 oris r8,r8,MSR_VSX@h
1881END_FTR_SECTION_IFSET(CPU_FTR_VSX)
1882#endif
1883 mtmsrd r8
1884 isync
1885 lfd fr0,VCPU_FPSCR(r4)
1886 MTFSF_L(fr0)
1887#ifdef CONFIG_VSX
1888BEGIN_FTR_SECTION
1889 reg = 0
1890 .rept 32
1891 li r7,reg*16+VCPU_VSRS
Michael Neulingc75df6f2012-06-25 13:33:10 +00001892 LXVD2X(reg,R7,R4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001893 reg = reg + 1
1894 .endr
1895FTR_SECTION_ELSE
1896#endif
1897 reg = 0
1898 .rept 32
1899 lfd reg,reg*8+VCPU_FPRS(r4)
1900 reg = reg + 1
1901 .endr
1902#ifdef CONFIG_VSX
1903ALT_FTR_SECTION_END_IFSET(CPU_FTR_VSX)
1904#endif
1905
1906#ifdef CONFIG_ALTIVEC
1907BEGIN_FTR_SECTION
1908 li r7,VCPU_VSCR
1909 lvx vr0,r7,r4
1910 mtvscr vr0
1911 reg = 0
1912 .rept 32
1913 li r7,reg*16+VCPU_VRS
1914 lvx reg,r7,r4
1915 reg = reg + 1
1916 .endr
1917END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
1918#endif
1919 lwz r7,VCPU_VRSAVE(r4)
1920 mtspr SPRN_VRSAVE,r7
1921 blr