blob: a81979becf41dc097d2a5fa3e22256264eb7d8c9 [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)
Paul Mackerras218309b2013-09-06 13:23:44 +100069 mflr r0
70 std r0, PPC_LR_STKOFF(r1)
71 stdu r1, -112(r1)
Paul Mackerrasde56a942011-06-29 00:21:34 +000072 mfmsr r10
Paul Mackerras218309b2013-09-06 13:23:44 +100073 LOAD_REG_ADDR(r5, kvmppc_call_hv_entry)
Paul Mackerrasde56a942011-06-29 00:21:34 +000074 li r0,MSR_RI
75 andc r0,r10,r0
76 li r6,MSR_IR | MSR_DR
77 andc r6,r10,r6
78 mtmsrd r0,1 /* clear RI in MSR */
79 mtsrr0 r5
80 mtsrr1 r6
81 RFI
82
Paul Mackerras218309b2013-09-06 13:23:44 +100083kvmppc_call_hv_entry:
84 bl kvmppc_hv_entry
85
86 /* Back from guest - restore host state and return to caller */
87
88 /* Restore host DABR and DABRX */
89 ld r5,HSTATE_DABR(r13)
90 li r6,7
91 mtspr SPRN_DABR,r5
92 mtspr SPRN_DABRX,r6
93
94 /* Restore SPRG3 */
95 ld r3,PACA_SPRG3(r13)
96 mtspr SPRN_SPRG3,r3
97
98 /*
99 * Reload DEC. HDEC interrupts were disabled when
100 * we reloaded the host's LPCR value.
101 */
102 ld r3, HSTATE_DECEXP(r13)
103 mftb r4
104 subf r4, r4, r3
105 mtspr SPRN_DEC, r4
106
107 /* Reload the host's PMU registers */
108 ld r3, PACALPPACAPTR(r13) /* is the host using the PMU? */
109 lbz r4, LPPACA_PMCINUSE(r3)
110 cmpwi r4, 0
111 beq 23f /* skip if not */
112 lwz r3, HSTATE_PMC(r13)
113 lwz r4, HSTATE_PMC + 4(r13)
114 lwz r5, HSTATE_PMC + 8(r13)
115 lwz r6, HSTATE_PMC + 12(r13)
116 lwz r8, HSTATE_PMC + 16(r13)
117 lwz r9, HSTATE_PMC + 20(r13)
118BEGIN_FTR_SECTION
119 lwz r10, HSTATE_PMC + 24(r13)
120 lwz r11, HSTATE_PMC + 28(r13)
121END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
122 mtspr SPRN_PMC1, r3
123 mtspr SPRN_PMC2, r4
124 mtspr SPRN_PMC3, r5
125 mtspr SPRN_PMC4, r6
126 mtspr SPRN_PMC5, r8
127 mtspr SPRN_PMC6, r9
128BEGIN_FTR_SECTION
129 mtspr SPRN_PMC7, r10
130 mtspr SPRN_PMC8, r11
131END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
132 ld r3, HSTATE_MMCR(r13)
133 ld r4, HSTATE_MMCR + 8(r13)
134 ld r5, HSTATE_MMCR + 16(r13)
135 mtspr SPRN_MMCR1, r4
136 mtspr SPRN_MMCRA, r5
137 mtspr SPRN_MMCR0, r3
138 isync
13923:
140
141 /*
142 * For external and machine check interrupts, we need
143 * to call the Linux handler to process the interrupt.
144 * We do that by jumping to absolute address 0x500 for
145 * external interrupts, or the machine_check_fwnmi label
146 * for machine checks (since firmware might have patched
147 * the vector area at 0x200). The [h]rfid at the end of the
148 * handler will return to the book3s_hv_interrupts.S code.
149 * For other interrupts we do the rfid to get back
150 * to the book3s_hv_interrupts.S code here.
151 */
152 ld r8, 112+PPC_LR_STKOFF(r1)
153 addi r1, r1, 112
154 ld r7, HSTATE_HOST_MSR(r13)
155
156 cmpwi cr1, r12, BOOK3S_INTERRUPT_MACHINE_CHECK
157 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
158BEGIN_FTR_SECTION
159 beq 11f
160END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
161
162 /* RFI into the highmem handler, or branch to interrupt handler */
163 mfmsr r6
164 li r0, MSR_RI
165 andc r6, r6, r0
166 mtmsrd r6, 1 /* Clear RI in MSR */
167 mtsrr0 r8
168 mtsrr1 r7
169 beqa 0x500 /* external interrupt (PPC970) */
170 beq cr1, 13f /* machine check */
171 RFI
172
173 /* On POWER7, we have external interrupts set to use HSRR0/1 */
17411: mtspr SPRN_HSRR0, r8
175 mtspr SPRN_HSRR1, r7
176 ba 0x500
177
17813: b machine_check_fwnmi
179
Paul Mackerrasde56a942011-06-29 00:21:34 +0000180
Paul Mackerras371fefd2011-06-29 00:23:08 +0000181/*
182 * We come in here when wakened from nap mode on a secondary hw thread.
183 * Relocation is off and most register values are lost.
184 * r13 points to the PACA.
185 */
186 .globl kvm_start_guest
187kvm_start_guest:
188 ld r1,PACAEMERGSP(r13)
189 subi r1,r1,STACK_FRAME_OVERHEAD
Paul Mackerras19ccb762011-07-23 17:42:46 +1000190 ld r2,PACATOC(r13)
191
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000192 li r0,KVM_HWTHREAD_IN_KVM
193 stb r0,HSTATE_HWTHREAD_STATE(r13)
194
195 /* NV GPR values from power7_idle() will no longer be valid */
196 li r0,1
197 stb r0,PACA_NAPSTATELOST(r13)
198
Paul Mackerras4619ac82013-04-17 20:31:41 +0000199 /* were we napping due to cede? */
200 lbz r0,HSTATE_NAPPING(r13)
201 cmpwi r0,0
202 bne kvm_end_cede
203
204 /*
205 * We weren't napping due to cede, so this must be a secondary
206 * thread being woken up to run a guest, or being woken up due
207 * to a stray IPI. (Or due to some machine check or hypervisor
208 * maintenance interrupt while the core is in KVM.)
209 */
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000210
211 /* Check the wake reason in SRR1 to see why we got here */
212 mfspr r3,SPRN_SRR1
213 rlwinm r3,r3,44-31,0x7 /* extract wake reason field */
214 cmpwi r3,4 /* was it an external interrupt? */
Paul Mackerras4619ac82013-04-17 20:31:41 +0000215 bne 27f /* if not */
216 ld r5,HSTATE_XICS_PHYS(r13)
217 li r7,XICS_XIRR /* if it was an external interrupt, */
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000218 lwzcix r8,r5,r7 /* get and ack the interrupt */
219 sync
220 clrldi. r9,r8,40 /* get interrupt source ID. */
Paul Mackerras4619ac82013-04-17 20:31:41 +0000221 beq 28f /* none there? */
222 cmpwi r9,XICS_IPI /* was it an IPI? */
223 bne 29f
224 li r0,0xff
225 li r6,XICS_MFRR
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000226 stbcix r0,r5,r6 /* clear IPI */
Paul Mackerras4619ac82013-04-17 20:31:41 +0000227 stwcix r8,r5,r7 /* EOI the interrupt */
228 sync /* order loading of vcpu after that */
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000229
Paul Mackerras4619ac82013-04-17 20:31:41 +0000230 /* get vcpu pointer, NULL if we have no vcpu to run */
Paul Mackerras7b444c62012-10-15 01:16:14 +0000231 ld r4,HSTATE_KVM_VCPU(r13)
232 cmpdi r4,0
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000233 /* if we have no vcpu to run, go back to sleep */
Paul Mackerras7b444c62012-10-15 01:16:14 +0000234 beq kvm_no_guest
Paul Mackerras218309b2013-09-06 13:23:44 +1000235 b 30f
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000236
Paul Mackerras4619ac82013-04-17 20:31:41 +000023727: /* XXX should handle hypervisor maintenance interrupts etc. here */
238 b kvm_no_guest
23928: /* SRR1 said external but ICP said nope?? */
240 b kvm_no_guest
24129: /* External non-IPI interrupt to offline secondary thread? help?? */
242 stw r8,HSTATE_SAVED_XIRR(r13)
243 b kvm_no_guest
Paul Mackerras371fefd2011-06-29 00:23:08 +0000244
Paul Mackerras218309b2013-09-06 13:23:44 +100024530: bl kvmppc_hv_entry
246
247 /* Back from the guest, go back to nap */
248 /* Clear our vcpu pointer so we don't come back in early */
249 li r0, 0
250 std r0, HSTATE_KVM_VCPU(r13)
251 lwsync
252 /* Clear any pending IPI - we're an offline thread */
253 ld r5, HSTATE_XICS_PHYS(r13)
254 li r7, XICS_XIRR
255 lwzcix r3, r5, r7 /* ack any pending interrupt */
256 rlwinm. r0, r3, 0, 0xffffff /* any pending? */
257 beq 37f
258 sync
259 li r0, 0xff
260 li r6, XICS_MFRR
261 stbcix r0, r5, r6 /* clear the IPI */
262 stwcix r3, r5, r7 /* EOI it */
26337: sync
264
265 /* increment the nap count and then go to nap mode */
266 ld r4, HSTATE_KVM_VCORE(r13)
267 addi r4, r4, VCORE_NAP_COUNT
268 lwsync /* make previous updates visible */
26951: lwarx r3, 0, r4
270 addi r3, r3, 1
271 stwcx. r3, 0, r4
272 bne 51b
273
274kvm_no_guest:
275 li r0, KVM_HWTHREAD_IN_NAP
276 stb r0, HSTATE_HWTHREAD_STATE(r13)
277 li r3, LPCR_PECE0
278 mfspr r4, SPRN_LPCR
279 rlwimi r4, r3, 0, LPCR_PECE0 | LPCR_PECE1
280 mtspr SPRN_LPCR, r4
281 isync
282 std r0, HSTATE_SCRATCH0(r13)
283 ptesync
284 ld r0, HSTATE_SCRATCH0(r13)
2851: cmpd r0, r0
286 bne 1b
287 nap
288 b .
289
290/******************************************************************************
291 * *
292 * Entry code *
293 * *
294 *****************************************************************************/
295
Paul Mackerrasde56a942011-06-29 00:21:34 +0000296.global kvmppc_hv_entry
297kvmppc_hv_entry:
298
299 /* Required state:
300 *
301 * R4 = vcpu pointer
302 * MSR = ~IR|DR
303 * R13 = PACA
304 * R1 = host R1
305 * all other volatile GPRS = free
306 */
307 mflr r0
Paul Mackerras218309b2013-09-06 13:23:44 +1000308 std r0, PPC_LR_STKOFF(r1)
309 stdu r1, -112(r1)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000310
Paul Mackerras89436332012-03-02 01:38:23 +0000311 /* Set partition DABR */
312 /* Do this before re-enabling PMU to avoid P7 DABR corruption bug */
313 li r5,3
314 ld r6,VCPU_DABR(r4)
315 mtspr SPRN_DABRX,r5
316 mtspr SPRN_DABR,r6
317BEGIN_FTR_SECTION
318 isync
319END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000320
321 /* Load guest PMU registers */
322 /* R4 is live here (vcpu pointer) */
323 li r3, 1
324 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
325 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
326 isync
327 lwz r3, VCPU_PMC(r4) /* always load up guest PMU registers */
328 lwz r5, VCPU_PMC + 4(r4) /* to prevent information leak */
329 lwz r6, VCPU_PMC + 8(r4)
330 lwz r7, VCPU_PMC + 12(r4)
331 lwz r8, VCPU_PMC + 16(r4)
332 lwz r9, VCPU_PMC + 20(r4)
Paul Mackerras9e368f22011-06-29 00:40:08 +0000333BEGIN_FTR_SECTION
334 lwz r10, VCPU_PMC + 24(r4)
335 lwz r11, VCPU_PMC + 28(r4)
336END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000337 mtspr SPRN_PMC1, r3
338 mtspr SPRN_PMC2, r5
339 mtspr SPRN_PMC3, r6
340 mtspr SPRN_PMC4, r7
341 mtspr SPRN_PMC5, r8
342 mtspr SPRN_PMC6, r9
Paul Mackerras9e368f22011-06-29 00:40:08 +0000343BEGIN_FTR_SECTION
344 mtspr SPRN_PMC7, r10
345 mtspr SPRN_PMC8, r11
346END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000347 ld r3, VCPU_MMCR(r4)
348 ld r5, VCPU_MMCR + 8(r4)
349 ld r6, VCPU_MMCR + 16(r4)
Paul Mackerras14941782013-09-06 13:11:18 +1000350 ld r7, VCPU_SIAR(r4)
351 ld r8, VCPU_SDAR(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000352 mtspr SPRN_MMCR1, r5
353 mtspr SPRN_MMCRA, r6
Paul Mackerras14941782013-09-06 13:11:18 +1000354 mtspr SPRN_SIAR, r7
355 mtspr SPRN_SDAR, r8
Paul Mackerrasde56a942011-06-29 00:21:34 +0000356 mtspr SPRN_MMCR0, r3
357 isync
358
359 /* Load up FP, VMX and VSX registers */
360 bl kvmppc_load_fp
361
Michael Neulingc75df6f2012-06-25 13:33:10 +0000362 ld r14, VCPU_GPR(R14)(r4)
363 ld r15, VCPU_GPR(R15)(r4)
364 ld r16, VCPU_GPR(R16)(r4)
365 ld r17, VCPU_GPR(R17)(r4)
366 ld r18, VCPU_GPR(R18)(r4)
367 ld r19, VCPU_GPR(R19)(r4)
368 ld r20, VCPU_GPR(R20)(r4)
369 ld r21, VCPU_GPR(R21)(r4)
370 ld r22, VCPU_GPR(R22)(r4)
371 ld r23, VCPU_GPR(R23)(r4)
372 ld r24, VCPU_GPR(R24)(r4)
373 ld r25, VCPU_GPR(R25)(r4)
374 ld r26, VCPU_GPR(R26)(r4)
375 ld r27, VCPU_GPR(R27)(r4)
376 ld r28, VCPU_GPR(R28)(r4)
377 ld r29, VCPU_GPR(R29)(r4)
378 ld r30, VCPU_GPR(R30)(r4)
379 ld r31, VCPU_GPR(R31)(r4)
Paul Mackerras89436332012-03-02 01:38:23 +0000380
Paul Mackerras9e368f22011-06-29 00:40:08 +0000381BEGIN_FTR_SECTION
Paul Mackerrasde56a942011-06-29 00:21:34 +0000382 /* Switch DSCR to guest value */
383 ld r5, VCPU_DSCR(r4)
384 mtspr SPRN_DSCR, r5
Paul Mackerras9e368f22011-06-29 00:40:08 +0000385END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000386
387 /*
388 * Set the decrementer to the guest decrementer.
389 */
390 ld r8,VCPU_DEC_EXPIRES(r4)
391 mftb r7
392 subf r3,r7,r8
393 mtspr SPRN_DEC,r3
394 stw r3,VCPU_DEC(r4)
395
396 ld r5, VCPU_SPRG0(r4)
397 ld r6, VCPU_SPRG1(r4)
398 ld r7, VCPU_SPRG2(r4)
399 ld r8, VCPU_SPRG3(r4)
400 mtspr SPRN_SPRG0, r5
401 mtspr SPRN_SPRG1, r6
402 mtspr SPRN_SPRG2, r7
403 mtspr SPRN_SPRG3, r8
404
405 /* Save R1 in the PACA */
406 std r1, HSTATE_HOST_R1(r13)
407
408 /* Load up DAR and DSISR */
409 ld r5, VCPU_DAR(r4)
410 lwz r6, VCPU_DSISR(r4)
411 mtspr SPRN_DAR, r5
412 mtspr SPRN_DSISR, r6
413
Paul Mackerras9e368f22011-06-29 00:40:08 +0000414BEGIN_FTR_SECTION
Paul Mackerrasde56a942011-06-29 00:21:34 +0000415 /* Restore AMR and UAMOR, set AMOR to all 1s */
416 ld r5,VCPU_AMR(r4)
417 ld r6,VCPU_UAMOR(r4)
418 li r7,-1
419 mtspr SPRN_AMR,r5
420 mtspr SPRN_UAMOR,r6
421 mtspr SPRN_AMOR,r7
Paul Mackerras9e368f22011-06-29 00:40:08 +0000422END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000423
424 /* Clear out SLB */
425 li r6,0
426 slbmte r6,r6
427 slbia
428 ptesync
429
Paul Mackerras9e368f22011-06-29 00:40:08 +0000430BEGIN_FTR_SECTION
431 b 30f
432END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
433 /*
434 * POWER7 host -> guest partition switch code.
435 * We don't have to lock against concurrent tlbies,
436 * but we do have to coordinate across hardware threads.
437 */
Paul Mackerras371fefd2011-06-29 00:23:08 +0000438 /* Increment entry count iff exit count is zero. */
439 ld r5,HSTATE_KVM_VCORE(r13)
440 addi r9,r5,VCORE_ENTRY_EXIT
44121: lwarx r3,0,r9
442 cmpwi r3,0x100 /* any threads starting to exit? */
443 bge secondary_too_late /* if so we're too late to the party */
444 addi r3,r3,1
445 stwcx. r3,0,r9
446 bne 21b
447
448 /* Primary thread switches to guest partition. */
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000449 ld r9,VCPU_KVM(r4) /* pointer to struct kvm */
Paul Mackerras371fefd2011-06-29 00:23:08 +0000450 lwz r6,VCPU_PTID(r4)
451 cmpwi r6,0
452 bne 20f
Paul Mackerrasde56a942011-06-29 00:21:34 +0000453 ld r6,KVM_SDR1(r9)
454 lwz r7,KVM_LPID(r9)
455 li r0,LPID_RSVD /* switch to reserved LPID */
456 mtspr SPRN_LPID,r0
457 ptesync
458 mtspr SPRN_SDR1,r6 /* switch to partition page table */
459 mtspr SPRN_LPID,r7
460 isync
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000461
462 /* See if we need to flush the TLB */
463 lhz r6,PACAPACAINDEX(r13) /* test_bit(cpu, need_tlb_flush) */
464 clrldi r7,r6,64-6 /* extract bit number (6 bits) */
465 srdi r6,r6,6 /* doubleword number */
466 sldi r6,r6,3 /* address offset */
467 add r6,r6,r9
468 addi r6,r6,KVM_NEED_FLUSH /* dword in kvm->arch.need_tlb_flush */
Paul Mackerras371fefd2011-06-29 00:23:08 +0000469 li r0,1
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000470 sld r0,r0,r7
471 ld r7,0(r6)
472 and. r7,r7,r0
473 beq 22f
47423: ldarx r7,0,r6 /* if set, clear the bit */
475 andc r7,r7,r0
476 stdcx. r7,0,r6
477 bne 23b
478 li r6,128 /* and flush the TLB */
479 mtctr r6
480 li r7,0x800 /* IS field = 0b10 */
481 ptesync
48228: tlbiel r7
483 addi r7,r7,0x1000
484 bdnz 28b
485 ptesync
486
Paul Mackerras93b0f4d2013-09-06 13:17:46 +1000487 /* Add timebase offset onto timebase */
48822: ld r8,VCORE_TB_OFFSET(r5)
489 cmpdi r8,0
490 beq 37f
491 mftb r6 /* current host timebase */
492 add r8,r8,r6
493 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
494 mftb r7 /* check if lower 24 bits overflowed */
495 clrldi r6,r6,40
496 clrldi r7,r7,40
497 cmpld r7,r6
498 bge 37f
499 addis r8,r8,0x100 /* if so, increment upper 40 bits */
500 mtspr SPRN_TBU40,r8
501
50237: li r0,1
Paul Mackerras371fefd2011-06-29 00:23:08 +0000503 stb r0,VCORE_IN_GUEST(r5) /* signal secondaries to continue */
504 b 10f
505
506 /* Secondary threads wait for primary to have done partition switch */
50720: lbz r0,VCORE_IN_GUEST(r5)
508 cmpwi r0,0
509 beq 20b
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000510
Paul Mackerras19ccb762011-07-23 17:42:46 +1000511 /* Set LPCR and RMOR. */
Paul Mackerrasa0144e22013-09-20 14:52:38 +100051210: ld r8,VCORE_LPCR(r5)
Paul Mackerras19ccb762011-07-23 17:42:46 +1000513 mtspr SPRN_LPCR,r8
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000514 ld r8,KVM_RMOR(r9)
515 mtspr SPRN_RMOR,r8
Paul Mackerrasde56a942011-06-29 00:21:34 +0000516 isync
517
Paul Mackerras8c2dbb72013-09-06 13:24:35 +1000518 /* Increment yield count if they have a VPA */
519 ld r3, VCPU_VPA(r4)
520 cmpdi r3, 0
521 beq 25f
522 lwz r5, LPPACA_YIELDCOUNT(r3)
523 addi r5, r5, 1
524 stw r5, LPPACA_YIELDCOUNT(r3)
525 li r6, 1
526 stb r6, VCPU_VPA_DIRTY(r4)
52725:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000528 /* Check if HDEC expires soon */
529 mfspr r3,SPRN_HDEC
530 cmpwi r3,10
531 li r12,BOOK3S_INTERRUPT_HV_DECREMENTER
532 mr r9,r4
533 blt hdec_soon
534
Paul Mackerrasde56a942011-06-29 00:21:34 +0000535 /* Save purr/spurr */
536 mfspr r5,SPRN_PURR
537 mfspr r6,SPRN_SPURR
538 std r5,HSTATE_PURR(r13)
539 std r6,HSTATE_SPURR(r13)
540 ld r7,VCPU_PURR(r4)
541 ld r8,VCPU_SPURR(r4)
542 mtspr SPRN_PURR,r7
543 mtspr SPRN_SPURR,r8
Paul Mackerras9e368f22011-06-29 00:40:08 +0000544 b 31f
545
546 /*
547 * PPC970 host -> guest partition switch code.
548 * We have to lock against concurrent tlbies,
549 * using native_tlbie_lock to lock against host tlbies
550 * and kvm->arch.tlbie_lock to lock against guest tlbies.
551 * We also have to invalidate the TLB since its
552 * entries aren't tagged with the LPID.
553 */
55430: ld r9,VCPU_KVM(r4) /* pointer to struct kvm */
555
556 /* first take native_tlbie_lock */
557 .section ".toc","aw"
558toc_tlbie_lock:
559 .tc native_tlbie_lock[TC],native_tlbie_lock
560 .previous
561 ld r3,toc_tlbie_lock@toc(2)
Anton Blanchard54bb7f42013-08-07 02:01:51 +1000562#ifdef __BIG_ENDIAN__
Paul Mackerras9e368f22011-06-29 00:40:08 +0000563 lwz r8,PACA_LOCK_TOKEN(r13)
Anton Blanchard54bb7f42013-08-07 02:01:51 +1000564#else
565 lwz r8,PACAPACAINDEX(r13)
566#endif
Paul Mackerras9e368f22011-06-29 00:40:08 +000056724: lwarx r0,0,r3
568 cmpwi r0,0
569 bne 24b
570 stwcx. r8,0,r3
571 bne 24b
572 isync
573
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000574 ld r5,HSTATE_KVM_VCORE(r13)
575 ld r7,VCORE_LPCR(r5) /* use vcore->lpcr to store HID4 */
Paul Mackerras9e368f22011-06-29 00:40:08 +0000576 li r0,0x18f
577 rotldi r0,r0,HID4_LPID5_SH /* all lpid bits in HID4 = 1 */
578 or r0,r7,r0
579 ptesync
580 sync
581 mtspr SPRN_HID4,r0 /* switch to reserved LPID */
582 isync
583 li r0,0
584 stw r0,0(r3) /* drop native_tlbie_lock */
585
586 /* invalidate the whole TLB */
587 li r0,256
588 mtctr r0
589 li r6,0
59025: tlbiel r6
591 addi r6,r6,0x1000
592 bdnz 25b
593 ptesync
594
595 /* Take the guest's tlbie_lock */
596 addi r3,r9,KVM_TLBIE_LOCK
59724: lwarx r0,0,r3
598 cmpwi r0,0
599 bne 24b
600 stwcx. r8,0,r3
601 bne 24b
602 isync
603 ld r6,KVM_SDR1(r9)
604 mtspr SPRN_SDR1,r6 /* switch to partition page table */
605
606 /* Set up HID4 with the guest's LPID etc. */
607 sync
608 mtspr SPRN_HID4,r7
609 isync
610
611 /* drop the guest's tlbie_lock */
612 li r0,0
613 stw r0,0(r3)
614
615 /* Check if HDEC expires soon */
616 mfspr r3,SPRN_HDEC
617 cmpwi r3,10
618 li r12,BOOK3S_INTERRUPT_HV_DECREMENTER
619 mr r9,r4
620 blt hdec_soon
621
622 /* Enable HDEC interrupts */
623 mfspr r0,SPRN_HID0
624 li r3,1
625 rldimi r0,r3, HID0_HDICE_SH, 64-HID0_HDICE_SH-1
626 sync
627 mtspr SPRN_HID0,r0
628 mfspr r0,SPRN_HID0
629 mfspr r0,SPRN_HID0
630 mfspr r0,SPRN_HID0
631 mfspr r0,SPRN_HID0
632 mfspr r0,SPRN_HID0
633 mfspr r0,SPRN_HID0
Paul Mackerrasde56a942011-06-29 00:21:34 +0000634
635 /* Load up guest SLB entries */
Paul Mackerras9e368f22011-06-29 00:40:08 +000063631: lwz r5,VCPU_SLB_MAX(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000637 cmpwi r5,0
638 beq 9f
639 mtctr r5
640 addi r6,r4,VCPU_SLB
6411: ld r8,VCPU_SLB_E(r6)
642 ld r9,VCPU_SLB_V(r6)
643 slbmte r9,r8
644 addi r6,r6,VCPU_SLB_SIZE
645 bdnz 1b
6469:
647
648 /* Restore state of CTRL run bit; assume 1 on entry */
649 lwz r5,VCPU_CTRL(r4)
650 andi. r5,r5,1
651 bne 4f
652 mfspr r6,SPRN_CTRLF
653 clrrdi r6,r6,1
654 mtspr SPRN_CTRLT,r6
6554:
656 ld r6, VCPU_CTR(r4)
657 lwz r7, VCPU_XER(r4)
658
659 mtctr r6
660 mtxer r7
661
Paul Mackerras4619ac82013-04-17 20:31:41 +0000662 ld r10, VCPU_PC(r4)
663 ld r11, VCPU_MSR(r4)
Paul Mackerras19ccb762011-07-23 17:42:46 +1000664kvmppc_cede_reentry: /* r4 = vcpu, r13 = paca */
Paul Mackerrasde56a942011-06-29 00:21:34 +0000665 ld r6, VCPU_SRR0(r4)
666 ld r7, VCPU_SRR1(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000667
Paul Mackerras4619ac82013-04-17 20:31:41 +0000668 /* r11 = vcpu->arch.msr & ~MSR_HV */
Paul Mackerrasde56a942011-06-29 00:21:34 +0000669 rldicl r11, r11, 63 - MSR_HV_LG, 1
670 rotldi r11, r11, 1 + MSR_HV_LG
671 ori r11, r11, MSR_ME
672
Paul Mackerras19ccb762011-07-23 17:42:46 +1000673 /* Check if we can deliver an external or decrementer interrupt now */
674 ld r0,VCPU_PENDING_EXC(r4)
Paul Mackerras4619ac82013-04-17 20:31:41 +0000675 lis r8,(1 << BOOK3S_IRQPRIO_EXTERNAL_LEVEL)@h
Paul Mackerras19ccb762011-07-23 17:42:46 +1000676 and r0,r0,r8
677 cmpdi cr1,r0,0
678 andi. r0,r11,MSR_EE
679 beq cr1,11f
680BEGIN_FTR_SECTION
681 mfspr r8,SPRN_LPCR
682 ori r8,r8,LPCR_MER
683 mtspr SPRN_LPCR,r8
684 isync
685END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
686 beq 5f
687 li r0,BOOK3S_INTERRUPT_EXTERNAL
68812: mr r6,r10
689 mr r10,r0
690 mr r7,r11
691 li r11,(MSR_ME << 1) | 1 /* synthesize MSR_SF | MSR_ME */
692 rotldi r11,r11,63
693 b 5f
69411: beq 5f
695 mfspr r0,SPRN_DEC
696 cmpwi r0,0
697 li r0,BOOK3S_INTERRUPT_DECREMENTER
698 blt 12b
699
700 /* Move SRR0 and SRR1 into the respective regs */
7015: mtspr SPRN_SRR0, r6
702 mtspr SPRN_SRR1, r7
Paul Mackerras19ccb762011-07-23 17:42:46 +1000703
Paul Mackerrasde56a942011-06-29 00:21:34 +0000704fast_guest_return:
Paul Mackerras4619ac82013-04-17 20:31:41 +0000705 li r0,0
706 stb r0,VCPU_CEDED(r4) /* cancel cede */
Paul Mackerrasde56a942011-06-29 00:21:34 +0000707 mtspr SPRN_HSRR0,r10
708 mtspr SPRN_HSRR1,r11
709
710 /* Activate guest mode, so faults get handled by KVM */
711 li r9, KVM_GUEST_MODE_GUEST
712 stb r9, HSTATE_IN_GUEST(r13)
713
714 /* Enter guest */
715
Paul Mackerras0acb9112013-02-04 18:10:51 +0000716BEGIN_FTR_SECTION
717 ld r5, VCPU_CFAR(r4)
718 mtspr SPRN_CFAR, r5
719END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
Paul Mackerras4b8473c2013-09-20 14:52:39 +1000720BEGIN_FTR_SECTION
721 ld r0, VCPU_PPR(r4)
722END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
Paul Mackerras0acb9112013-02-04 18:10:51 +0000723
Paul Mackerrasde56a942011-06-29 00:21:34 +0000724 ld r5, VCPU_LR(r4)
725 lwz r6, VCPU_CR(r4)
726 mtlr r5
727 mtcr r6
728
Michael Neulingc75df6f2012-06-25 13:33:10 +0000729 ld r1, VCPU_GPR(R1)(r4)
730 ld r2, VCPU_GPR(R2)(r4)
731 ld r3, VCPU_GPR(R3)(r4)
732 ld r5, VCPU_GPR(R5)(r4)
733 ld r6, VCPU_GPR(R6)(r4)
734 ld r7, VCPU_GPR(R7)(r4)
735 ld r8, VCPU_GPR(R8)(r4)
736 ld r9, VCPU_GPR(R9)(r4)
737 ld r10, VCPU_GPR(R10)(r4)
738 ld r11, VCPU_GPR(R11)(r4)
739 ld r12, VCPU_GPR(R12)(r4)
740 ld r13, VCPU_GPR(R13)(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000741
Paul Mackerras4b8473c2013-09-20 14:52:39 +1000742BEGIN_FTR_SECTION
743 mtspr SPRN_PPR, r0
744END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
745 ld r0, VCPU_GPR(R0)(r4)
Michael Neulingc75df6f2012-06-25 13:33:10 +0000746 ld r4, VCPU_GPR(R4)(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000747
748 hrfid
749 b .
750
751/******************************************************************************
752 * *
753 * Exit code *
754 * *
755 *****************************************************************************/
756
757/*
758 * We come here from the first-level interrupt handlers.
759 */
760 .globl kvmppc_interrupt
761kvmppc_interrupt:
762 /*
763 * Register contents:
764 * R12 = interrupt vector
765 * R13 = PACA
766 * guest CR, R12 saved in shadow VCPU SCRATCH1/0
767 * guest R13 saved in SPRN_SCRATCH0
768 */
769 /* abuse host_r2 as third scratch area; we get r2 from PACATOC(r13) */
770 std r9, HSTATE_HOST_R2(r13)
771 ld r9, HSTATE_KVM_VCPU(r13)
772
773 /* Save registers */
774
Michael Neulingc75df6f2012-06-25 13:33:10 +0000775 std r0, VCPU_GPR(R0)(r9)
776 std r1, VCPU_GPR(R1)(r9)
777 std r2, VCPU_GPR(R2)(r9)
778 std r3, VCPU_GPR(R3)(r9)
779 std r4, VCPU_GPR(R4)(r9)
780 std r5, VCPU_GPR(R5)(r9)
781 std r6, VCPU_GPR(R6)(r9)
782 std r7, VCPU_GPR(R7)(r9)
783 std r8, VCPU_GPR(R8)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000784 ld r0, HSTATE_HOST_R2(r13)
Michael Neulingc75df6f2012-06-25 13:33:10 +0000785 std r0, VCPU_GPR(R9)(r9)
786 std r10, VCPU_GPR(R10)(r9)
787 std r11, VCPU_GPR(R11)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000788 ld r3, HSTATE_SCRATCH0(r13)
789 lwz r4, HSTATE_SCRATCH1(r13)
Michael Neulingc75df6f2012-06-25 13:33:10 +0000790 std r3, VCPU_GPR(R12)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000791 stw r4, VCPU_CR(r9)
Paul Mackerras0acb9112013-02-04 18:10:51 +0000792BEGIN_FTR_SECTION
793 ld r3, HSTATE_CFAR(r13)
794 std r3, VCPU_CFAR(r9)
795END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
Paul Mackerras4b8473c2013-09-20 14:52:39 +1000796BEGIN_FTR_SECTION
797 ld r4, HSTATE_PPR(r13)
798 std r4, VCPU_PPR(r9)
799END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000800
801 /* Restore R1/R2 so we can handle faults */
802 ld r1, HSTATE_HOST_R1(r13)
803 ld r2, PACATOC(r13)
804
805 mfspr r10, SPRN_SRR0
806 mfspr r11, SPRN_SRR1
807 std r10, VCPU_SRR0(r9)
808 std r11, VCPU_SRR1(r9)
809 andi. r0, r12, 2 /* need to read HSRR0/1? */
810 beq 1f
811 mfspr r10, SPRN_HSRR0
812 mfspr r11, SPRN_HSRR1
813 clrrdi r12, r12, 2
8141: std r10, VCPU_PC(r9)
815 std r11, VCPU_MSR(r9)
816
817 GET_SCRATCH0(r3)
818 mflr r4
Michael Neulingc75df6f2012-06-25 13:33:10 +0000819 std r3, VCPU_GPR(R13)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000820 std r4, VCPU_LR(r9)
821
822 /* Unset guest mode */
823 li r0, KVM_GUEST_MODE_NONE
824 stb r0, HSTATE_IN_GUEST(r13)
825
826 stw r12,VCPU_TRAP(r9)
827
Paul Mackerras697d3892011-12-12 12:36:37 +0000828 /* Save HEIR (HV emulation assist reg) in last_inst
829 if this is an HEI (HV emulation interrupt, e40) */
830 li r3,KVM_INST_FETCH_FAILED
831BEGIN_FTR_SECTION
832 cmpwi r12,BOOK3S_INTERRUPT_H_EMUL_ASSIST
833 bne 11f
834 mfspr r3,SPRN_HEIR
835END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
83611: stw r3,VCPU_LAST_INST(r9)
837
838 /* these are volatile across C function calls */
839 mfctr r3
840 mfxer r4
841 std r3, VCPU_CTR(r9)
842 stw r4, VCPU_XER(r9)
843
844BEGIN_FTR_SECTION
845 /* If this is a page table miss then see if it's theirs or ours */
846 cmpwi r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
847 beq kvmppc_hdsi
Paul Mackerras342d3db2011-12-12 12:38:05 +0000848 cmpwi r12, BOOK3S_INTERRUPT_H_INST_STORAGE
849 beq kvmppc_hisi
Paul Mackerras697d3892011-12-12 12:36:37 +0000850END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
851
Paul Mackerrasde56a942011-06-29 00:21:34 +0000852 /* See if this is a leftover HDEC interrupt */
853 cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER
854 bne 2f
855 mfspr r3,SPRN_HDEC
856 cmpwi r3,0
857 bge ignore_hdec
8582:
Paul Mackerras697d3892011-12-12 12:36:37 +0000859 /* See if this is an hcall we can handle in real mode */
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000860 cmpwi r12,BOOK3S_INTERRUPT_SYSCALL
861 beq hcall_try_real_mode
Paul Mackerrasde56a942011-06-29 00:21:34 +0000862
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000863 /* Only handle external interrupts here on arch 206 and later */
Paul Mackerras9e368f22011-06-29 00:40:08 +0000864BEGIN_FTR_SECTION
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000865 b ext_interrupt_to_host
866END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_206)
867
868 /* External interrupt ? */
869 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
870 bne+ ext_interrupt_to_host
871
872 /* External interrupt, first check for host_ipi. If this is
873 * set, we know the host wants us out so let's do it now
874 */
Paul Mackerras4619ac82013-04-17 20:31:41 +0000875do_ext_interrupt:
Paul Mackerrasc9342432013-09-06 13:24:13 +1000876 bl kvmppc_read_intr
877 cmpdi r3, 0
878 bgt ext_interrupt_to_host
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000879
880 /* Allright, looks like an IPI for the guest, we need to set MER */
Paul Mackerras4619ac82013-04-17 20:31:41 +0000881 /* Check if any CPU is heading out to the host, if so head out too */
882 ld r5, HSTATE_KVM_VCORE(r13)
883 lwz r0, VCORE_ENTRY_EXIT(r5)
884 cmpwi r0, 0x100
885 bge ext_interrupt_to_host
886
887 /* See if there is a pending interrupt for the guest */
888 mfspr r8, SPRN_LPCR
889 ld r0, VCPU_PENDING_EXC(r9)
890 /* Insert EXTERNAL_LEVEL bit into LPCR at the MER bit position */
891 rldicl. r0, r0, 64 - BOOK3S_IRQPRIO_EXTERNAL_LEVEL, 63
892 rldimi r8, r0, LPCR_MER_SH, 63 - LPCR_MER_SH
893 beq 2f
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000894
895 /* And if the guest EE is set, we can deliver immediately, else
896 * we return to the guest with MER set
897 */
898 andi. r0, r11, MSR_EE
Paul Mackerras4619ac82013-04-17 20:31:41 +0000899 beq 2f
900 mtspr SPRN_SRR0, r10
901 mtspr SPRN_SRR1, r11
902 li r10, BOOK3S_INTERRUPT_EXTERNAL
903 li r11, (MSR_ME << 1) | 1 /* synthesize MSR_SF | MSR_ME */
904 rotldi r11, r11, 63
9052: mr r4, r9
906 mtspr SPRN_LPCR, r8
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000907 b fast_guest_return
908
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000909ext_interrupt_to_host:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000910
Paul Mackerrasb4072df2012-11-23 22:37:50 +0000911guest_exit_cont: /* r9 = vcpu, r12 = trap, r13 = paca */
Paul Mackerrasde56a942011-06-29 00:21:34 +0000912 /* Save more register state */
Paul Mackerrasde56a942011-06-29 00:21:34 +0000913 mfdar r6
914 mfdsisr r7
Paul Mackerrasde56a942011-06-29 00:21:34 +0000915 std r6, VCPU_DAR(r9)
916 stw r7, VCPU_DSISR(r9)
Paul Mackerras9e368f22011-06-29 00:40:08 +0000917BEGIN_FTR_SECTION
Paul Mackerras697d3892011-12-12 12:36:37 +0000918 /* don't overwrite fault_dar/fault_dsisr if HDSI */
Paul Mackerrasde56a942011-06-29 00:21:34 +0000919 cmpwi r12,BOOK3S_INTERRUPT_H_DATA_STORAGE
920 beq 6f
Paul Mackerras9e368f22011-06-29 00:40:08 +0000921END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
Paul Mackerras697d3892011-12-12 12:36:37 +0000922 std r6, VCPU_FAULT_DAR(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000923 stw r7, VCPU_FAULT_DSISR(r9)
924
Paul Mackerrasb4072df2012-11-23 22:37:50 +0000925 /* See if it is a machine check */
926 cmpwi r12, BOOK3S_INTERRUPT_MACHINE_CHECK
927 beq machine_check_realmode
928mc_cont:
929
Paul Mackerrasde56a942011-06-29 00:21:34 +0000930 /* Save guest CTRL register, set runlatch to 1 */
Paul Mackerras697d3892011-12-12 12:36:37 +00009316: mfspr r6,SPRN_CTRLF
Paul Mackerrasde56a942011-06-29 00:21:34 +0000932 stw r6,VCPU_CTRL(r9)
933 andi. r0,r6,1
934 bne 4f
935 ori r6,r6,1
936 mtspr SPRN_CTRLT,r6
9374:
938 /* Read the guest SLB and save it away */
939 lwz r0,VCPU_SLB_NR(r9) /* number of entries in SLB */
940 mtctr r0
941 li r6,0
942 addi r7,r9,VCPU_SLB
943 li r5,0
9441: slbmfee r8,r6
945 andis. r0,r8,SLB_ESID_V@h
946 beq 2f
947 add r8,r8,r6 /* put index in */
948 slbmfev r3,r6
949 std r8,VCPU_SLB_E(r7)
950 std r3,VCPU_SLB_V(r7)
951 addi r7,r7,VCPU_SLB_SIZE
952 addi r5,r5,1
9532: addi r6,r6,1
954 bdnz 1b
955 stw r5,VCPU_SLB_MAX(r9)
956
957 /*
958 * Save the guest PURR/SPURR
959 */
Paul Mackerras9e368f22011-06-29 00:40:08 +0000960BEGIN_FTR_SECTION
Paul Mackerrasde56a942011-06-29 00:21:34 +0000961 mfspr r5,SPRN_PURR
962 mfspr r6,SPRN_SPURR
963 ld r7,VCPU_PURR(r9)
964 ld r8,VCPU_SPURR(r9)
965 std r5,VCPU_PURR(r9)
966 std r6,VCPU_SPURR(r9)
967 subf r5,r7,r5
968 subf r6,r8,r6
969
970 /*
971 * Restore host PURR/SPURR and add guest times
972 * so that the time in the guest gets accounted.
973 */
974 ld r3,HSTATE_PURR(r13)
975 ld r4,HSTATE_SPURR(r13)
976 add r3,r3,r5
977 add r4,r4,r6
978 mtspr SPRN_PURR,r3
979 mtspr SPRN_SPURR,r4
Paul Mackerras9e368f22011-06-29 00:40:08 +0000980END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_201)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000981
982 /* Clear out SLB */
983 li r5,0
984 slbmte r5,r5
985 slbia
986 ptesync
987
Paul Mackerras19ccb762011-07-23 17:42:46 +1000988hdec_soon: /* r9 = vcpu, r12 = trap, r13 = paca */
Paul Mackerras9e368f22011-06-29 00:40:08 +0000989BEGIN_FTR_SECTION
990 b 32f
991END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
992 /*
993 * POWER7 guest -> host partition switch code.
994 * We don't have to lock against tlbies but we do
995 * have to coordinate the hardware threads.
996 */
Paul Mackerras371fefd2011-06-29 00:23:08 +0000997 /* Increment the threads-exiting-guest count in the 0xff00
998 bits of vcore->entry_exit_count */
999 lwsync
1000 ld r5,HSTATE_KVM_VCORE(r13)
1001 addi r6,r5,VCORE_ENTRY_EXIT
100241: lwarx r3,0,r6
1003 addi r0,r3,0x100
1004 stwcx. r0,0,r6
1005 bne 41b
Paul Mackerras19ccb762011-07-23 17:42:46 +10001006 lwsync
Paul Mackerras371fefd2011-06-29 00:23:08 +00001007
1008 /*
1009 * At this point we have an interrupt that we have to pass
1010 * up to the kernel or qemu; we can't handle it in real mode.
1011 * Thus we have to do a partition switch, so we have to
1012 * collect the other threads, if we are the first thread
1013 * to take an interrupt. To do this, we set the HDEC to 0,
1014 * which causes an HDEC interrupt in all threads within 2ns
1015 * because the HDEC register is shared between all 4 threads.
1016 * However, we don't need to bother if this is an HDEC
1017 * interrupt, since the other threads will already be on their
1018 * way here in that case.
1019 */
Paul Mackerras19ccb762011-07-23 17:42:46 +10001020 cmpwi r3,0x100 /* Are we the first here? */
1021 bge 43f
1022 cmpwi r3,1 /* Are any other threads in the guest? */
1023 ble 43f
Paul Mackerras371fefd2011-06-29 00:23:08 +00001024 cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER
1025 beq 40f
Paul Mackerras371fefd2011-06-29 00:23:08 +00001026 li r0,0
1027 mtspr SPRN_HDEC,r0
102840:
Paul Mackerras19ccb762011-07-23 17:42:46 +10001029 /*
1030 * Send an IPI to any napping threads, since an HDEC interrupt
1031 * doesn't wake CPUs up from nap.
1032 */
1033 lwz r3,VCORE_NAPPING_THREADS(r5)
1034 lwz r4,VCPU_PTID(r9)
1035 li r0,1
Michael Neuling2f584a12012-06-25 13:33:11 +00001036 sld r0,r0,r4
Paul Mackerras19ccb762011-07-23 17:42:46 +10001037 andc. r3,r3,r0 /* no sense IPI'ing ourselves */
1038 beq 43f
1039 mulli r4,r4,PACA_SIZE /* get paca for thread 0 */
1040 subf r6,r4,r13
104142: andi. r0,r3,1
1042 beq 44f
1043 ld r8,HSTATE_XICS_PHYS(r6) /* get thread's XICS reg addr */
1044 li r0,IPI_PRIORITY
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +00001045 li r7,XICS_MFRR
Paul Mackerras19ccb762011-07-23 17:42:46 +10001046 stbcix r0,r7,r8 /* trigger the IPI */
104744: srdi. r3,r3,1
1048 addi r6,r6,PACA_SIZE
1049 bne 42b
Paul Mackerras371fefd2011-06-29 00:23:08 +00001050
1051 /* Secondary threads wait for primary to do partition switch */
Paul Mackerras19ccb762011-07-23 17:42:46 +1000105243: ld r4,VCPU_KVM(r9) /* pointer to struct kvm */
Paul Mackerras371fefd2011-06-29 00:23:08 +00001053 ld r5,HSTATE_KVM_VCORE(r13)
1054 lwz r3,VCPU_PTID(r9)
1055 cmpwi r3,0
1056 beq 15f
1057 HMT_LOW
105813: lbz r3,VCORE_IN_GUEST(r5)
1059 cmpwi r3,0
1060 bne 13b
1061 HMT_MEDIUM
1062 b 16f
1063
1064 /* Primary thread waits for all the secondaries to exit guest */
106515: lwz r3,VCORE_ENTRY_EXIT(r5)
1066 srwi r0,r3,8
1067 clrldi r3,r3,56
1068 cmpw r3,r0
1069 bne 15b
1070 isync
1071
1072 /* Primary thread switches back to host partition */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001073 ld r6,KVM_HOST_SDR1(r4)
1074 lwz r7,KVM_HOST_LPID(r4)
1075 li r8,LPID_RSVD /* switch to reserved LPID */
1076 mtspr SPRN_LPID,r8
1077 ptesync
1078 mtspr SPRN_SDR1,r6 /* switch to partition page table */
1079 mtspr SPRN_LPID,r7
1080 isync
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001081
1082 /* Subtract timebase offset from timebase */
1083 ld r8,VCORE_TB_OFFSET(r5)
1084 cmpdi r8,0
1085 beq 17f
1086 mftb r6 /* current host timebase */
1087 subf r8,r8,r6
1088 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
1089 mftb r7 /* check if lower 24 bits overflowed */
1090 clrldi r6,r6,40
1091 clrldi r7,r7,40
1092 cmpld r7,r6
1093 bge 17f
1094 addis r8,r8,0x100 /* if so, increment upper 40 bits */
1095 mtspr SPRN_TBU40,r8
1096
1097 /* Signal secondary CPUs to continue */
109817: li r0,0
Paul Mackerras371fefd2011-06-29 00:23:08 +00001099 stb r0,VCORE_IN_GUEST(r5)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001100 lis r8,0x7fff /* MAX_INT@h */
1101 mtspr SPRN_HDEC,r8
1102
Paul Mackerras371fefd2011-06-29 00:23:08 +0000110316: ld r8,KVM_HOST_LPCR(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001104 mtspr SPRN_LPCR,r8
1105 isync
Paul Mackerras9e368f22011-06-29 00:40:08 +00001106 b 33f
1107
1108 /*
1109 * PPC970 guest -> host partition switch code.
1110 * We have to lock against concurrent tlbies, and
1111 * we have to flush the whole TLB.
1112 */
111332: ld r4,VCPU_KVM(r9) /* pointer to struct kvm */
1114
1115 /* Take the guest's tlbie_lock */
Anton Blanchard54bb7f42013-08-07 02:01:51 +10001116#ifdef __BIG_ENDIAN__
Paul Mackerras9e368f22011-06-29 00:40:08 +00001117 lwz r8,PACA_LOCK_TOKEN(r13)
Anton Blanchard54bb7f42013-08-07 02:01:51 +10001118#else
1119 lwz r8,PACAPACAINDEX(r13)
1120#endif
Paul Mackerras9e368f22011-06-29 00:40:08 +00001121 addi r3,r4,KVM_TLBIE_LOCK
112224: lwarx r0,0,r3
1123 cmpwi r0,0
1124 bne 24b
1125 stwcx. r8,0,r3
1126 bne 24b
1127 isync
1128
1129 ld r7,KVM_HOST_LPCR(r4) /* use kvm->arch.host_lpcr for HID4 */
1130 li r0,0x18f
1131 rotldi r0,r0,HID4_LPID5_SH /* all lpid bits in HID4 = 1 */
1132 or r0,r7,r0
1133 ptesync
1134 sync
1135 mtspr SPRN_HID4,r0 /* switch to reserved LPID */
1136 isync
1137 li r0,0
1138 stw r0,0(r3) /* drop guest tlbie_lock */
1139
1140 /* invalidate the whole TLB */
1141 li r0,256
1142 mtctr r0
1143 li r6,0
114425: tlbiel r6
1145 addi r6,r6,0x1000
1146 bdnz 25b
1147 ptesync
1148
1149 /* take native_tlbie_lock */
1150 ld r3,toc_tlbie_lock@toc(2)
115124: lwarx r0,0,r3
1152 cmpwi r0,0
1153 bne 24b
1154 stwcx. r8,0,r3
1155 bne 24b
1156 isync
1157
1158 ld r6,KVM_HOST_SDR1(r4)
1159 mtspr SPRN_SDR1,r6 /* switch to host page table */
1160
1161 /* Set up host HID4 value */
1162 sync
1163 mtspr SPRN_HID4,r7
1164 isync
1165 li r0,0
1166 stw r0,0(r3) /* drop native_tlbie_lock */
1167
1168 lis r8,0x7fff /* MAX_INT@h */
1169 mtspr SPRN_HDEC,r8
1170
1171 /* Disable HDEC interrupts */
1172 mfspr r0,SPRN_HID0
1173 li r3,0
1174 rldimi r0,r3, HID0_HDICE_SH, 64-HID0_HDICE_SH-1
1175 sync
1176 mtspr SPRN_HID0,r0
1177 mfspr r0,SPRN_HID0
1178 mfspr r0,SPRN_HID0
1179 mfspr r0,SPRN_HID0
1180 mfspr r0,SPRN_HID0
1181 mfspr r0,SPRN_HID0
1182 mfspr r0,SPRN_HID0
Paul Mackerrasde56a942011-06-29 00:21:34 +00001183
1184 /* load host SLB entries */
Paul Mackerras9e368f22011-06-29 00:40:08 +0000118533: ld r8,PACA_SLBSHADOWPTR(r13)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001186
1187 .rept SLB_NUM_BOLTED
1188 ld r5,SLBSHADOW_SAVEAREA(r8)
1189 ld r6,SLBSHADOW_SAVEAREA+8(r8)
1190 andis. r7,r5,SLB_ESID_V@h
1191 beq 1f
1192 slbmte r6,r5
11931: addi r8,r8,16
1194 .endr
1195
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001196 /* Save DEC */
1197 mfspr r5,SPRN_DEC
1198 mftb r6
1199 extsw r5,r5
1200 add r5,r5,r6
1201 std r5,VCPU_DEC_EXPIRES(r9)
1202
Paul Mackerrasde56a942011-06-29 00:21:34 +00001203 /* Save and reset AMR and UAMOR before turning on the MMU */
Paul Mackerras9e368f22011-06-29 00:40:08 +00001204BEGIN_FTR_SECTION
Paul Mackerrasde56a942011-06-29 00:21:34 +00001205 mfspr r5,SPRN_AMR
1206 mfspr r6,SPRN_UAMOR
1207 std r5,VCPU_AMR(r9)
1208 std r6,VCPU_UAMOR(r9)
1209 li r6,0
1210 mtspr SPRN_AMR,r6
Paul Mackerras9e368f22011-06-29 00:40:08 +00001211END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001212
Paul Mackerrasde56a942011-06-29 00:21:34 +00001213 /* Switch DSCR back to host value */
Paul Mackerras9e368f22011-06-29 00:40:08 +00001214BEGIN_FTR_SECTION
Paul Mackerrasde56a942011-06-29 00:21:34 +00001215 mfspr r8, SPRN_DSCR
1216 ld r7, HSTATE_DSCR(r13)
1217 std r8, VCPU_DSCR(r7)
1218 mtspr SPRN_DSCR, r7
Paul Mackerras9e368f22011-06-29 00:40:08 +00001219END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001220
1221 /* Save non-volatile GPRs */
Michael Neulingc75df6f2012-06-25 13:33:10 +00001222 std r14, VCPU_GPR(R14)(r9)
1223 std r15, VCPU_GPR(R15)(r9)
1224 std r16, VCPU_GPR(R16)(r9)
1225 std r17, VCPU_GPR(R17)(r9)
1226 std r18, VCPU_GPR(R18)(r9)
1227 std r19, VCPU_GPR(R19)(r9)
1228 std r20, VCPU_GPR(R20)(r9)
1229 std r21, VCPU_GPR(R21)(r9)
1230 std r22, VCPU_GPR(R22)(r9)
1231 std r23, VCPU_GPR(R23)(r9)
1232 std r24, VCPU_GPR(R24)(r9)
1233 std r25, VCPU_GPR(R25)(r9)
1234 std r26, VCPU_GPR(R26)(r9)
1235 std r27, VCPU_GPR(R27)(r9)
1236 std r28, VCPU_GPR(R28)(r9)
1237 std r29, VCPU_GPR(R29)(r9)
1238 std r30, VCPU_GPR(R30)(r9)
1239 std r31, VCPU_GPR(R31)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001240
1241 /* Save SPRGs */
1242 mfspr r3, SPRN_SPRG0
1243 mfspr r4, SPRN_SPRG1
1244 mfspr r5, SPRN_SPRG2
1245 mfspr r6, SPRN_SPRG3
1246 std r3, VCPU_SPRG0(r9)
1247 std r4, VCPU_SPRG1(r9)
1248 std r5, VCPU_SPRG2(r9)
1249 std r6, VCPU_SPRG3(r9)
1250
Paul Mackerras89436332012-03-02 01:38:23 +00001251 /* save FP state */
1252 mr r3, r9
1253 bl .kvmppc_save_fp
1254
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001255 /* Increment yield count if they have a VPA */
1256 ld r8, VCPU_VPA(r9) /* do they have a VPA? */
1257 cmpdi r8, 0
1258 beq 25f
1259 lwz r3, LPPACA_YIELDCOUNT(r8)
1260 addi r3, r3, 1
1261 stw r3, LPPACA_YIELDCOUNT(r8)
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001262 li r3, 1
1263 stb r3, VCPU_VPA_DIRTY(r9)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000126425:
1265 /* Save PMU registers if requested */
1266 /* r8 and cr0.eq are live here */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001267 li r3, 1
1268 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
1269 mfspr r4, SPRN_MMCR0 /* save MMCR0 */
1270 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
Paul Mackerras89436332012-03-02 01:38:23 +00001271 mfspr r6, SPRN_MMCRA
1272BEGIN_FTR_SECTION
1273 /* On P7, clear MMCRA in order to disable SDAR updates */
1274 li r7, 0
1275 mtspr SPRN_MMCRA, r7
1276END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001277 isync
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001278 beq 21f /* if no VPA, save PMU stuff anyway */
1279 lbz r7, LPPACA_PMCINUSE(r8)
1280 cmpwi r7, 0 /* did they ask for PMU stuff to be saved? */
1281 bne 21f
1282 std r3, VCPU_MMCR(r9) /* if not, set saved MMCR0 to FC */
1283 b 22f
128421: mfspr r5, SPRN_MMCR1
Paul Mackerras14941782013-09-06 13:11:18 +10001285 mfspr r7, SPRN_SIAR
1286 mfspr r8, SPRN_SDAR
Paul Mackerrasde56a942011-06-29 00:21:34 +00001287 std r4, VCPU_MMCR(r9)
1288 std r5, VCPU_MMCR + 8(r9)
1289 std r6, VCPU_MMCR + 16(r9)
Paul Mackerras14941782013-09-06 13:11:18 +10001290 std r7, VCPU_SIAR(r9)
1291 std r8, VCPU_SDAR(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001292 mfspr r3, SPRN_PMC1
1293 mfspr r4, SPRN_PMC2
1294 mfspr r5, SPRN_PMC3
1295 mfspr r6, SPRN_PMC4
1296 mfspr r7, SPRN_PMC5
1297 mfspr r8, SPRN_PMC6
Paul Mackerras9e368f22011-06-29 00:40:08 +00001298BEGIN_FTR_SECTION
1299 mfspr r10, SPRN_PMC7
1300 mfspr r11, SPRN_PMC8
1301END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001302 stw r3, VCPU_PMC(r9)
1303 stw r4, VCPU_PMC + 4(r9)
1304 stw r5, VCPU_PMC + 8(r9)
1305 stw r6, VCPU_PMC + 12(r9)
1306 stw r7, VCPU_PMC + 16(r9)
1307 stw r8, VCPU_PMC + 20(r9)
Paul Mackerras9e368f22011-06-29 00:40:08 +00001308BEGIN_FTR_SECTION
1309 stw r10, VCPU_PMC + 24(r9)
1310 stw r11, VCPU_PMC + 28(r9)
1311END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000131222:
Paul Mackerras218309b2013-09-06 13:23:44 +10001313 ld r0, 112+PPC_LR_STKOFF(r1)
1314 addi r1, r1, 112
1315 mtlr r0
1316 blr
1317secondary_too_late:
1318 ld r5,HSTATE_KVM_VCORE(r13)
1319 HMT_LOW
132013: lbz r3,VCORE_IN_GUEST(r5)
1321 cmpwi r3,0
1322 bne 13b
1323 HMT_MEDIUM
1324 li r0, KVM_GUEST_MODE_NONE
1325 stb r0, HSTATE_IN_GUEST(r13)
1326 ld r11,PACA_SLBSHADOWPTR(r13)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001327
Paul Mackerras218309b2013-09-06 13:23:44 +10001328 .rept SLB_NUM_BOLTED
1329 ld r5,SLBSHADOW_SAVEAREA(r11)
1330 ld r6,SLBSHADOW_SAVEAREA+8(r11)
1331 andis. r7,r5,SLB_ESID_V@h
1332 beq 1f
1333 slbmte r6,r5
13341: addi r11,r11,16
1335 .endr
1336 b 22b
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001337
Paul Mackerras697d3892011-12-12 12:36:37 +00001338/*
1339 * Check whether an HDSI is an HPTE not found fault or something else.
1340 * If it is an HPTE not found fault that is due to the guest accessing
1341 * a page that they have mapped but which we have paged out, then
1342 * we continue on with the guest exit path. In all other cases,
1343 * reflect the HDSI to the guest as a DSI.
1344 */
1345kvmppc_hdsi:
1346 mfspr r4, SPRN_HDAR
1347 mfspr r6, SPRN_HDSISR
Paul Mackerras4cf302b2011-12-12 12:38:51 +00001348 /* HPTE not found fault or protection fault? */
1349 andis. r0, r6, (DSISR_NOHPTE | DSISR_PROTFAULT)@h
Paul Mackerras697d3892011-12-12 12:36:37 +00001350 beq 1f /* if not, send it to the guest */
1351 andi. r0, r11, MSR_DR /* data relocation enabled? */
1352 beq 3f
1353 clrrdi r0, r4, 28
Michael Neulingc75df6f2012-06-25 13:33:10 +00001354 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
Paul Mackerras697d3892011-12-12 12:36:37 +00001355 bne 1f /* if no SLB entry found */
13564: std r4, VCPU_FAULT_DAR(r9)
1357 stw r6, VCPU_FAULT_DSISR(r9)
1358
1359 /* Search the hash table. */
1360 mr r3, r9 /* vcpu pointer */
Paul Mackerras342d3db2011-12-12 12:38:05 +00001361 li r7, 1 /* data fault */
Paul Mackerras697d3892011-12-12 12:36:37 +00001362 bl .kvmppc_hpte_hv_fault
1363 ld r9, HSTATE_KVM_VCPU(r13)
1364 ld r10, VCPU_PC(r9)
1365 ld r11, VCPU_MSR(r9)
1366 li r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1367 cmpdi r3, 0 /* retry the instruction */
1368 beq 6f
1369 cmpdi r3, -1 /* handle in kernel mode */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001370 beq guest_exit_cont
Paul Mackerras697d3892011-12-12 12:36:37 +00001371 cmpdi r3, -2 /* MMIO emulation; need instr word */
1372 beq 2f
1373
1374 /* Synthesize a DSI for the guest */
1375 ld r4, VCPU_FAULT_DAR(r9)
1376 mr r6, r3
13771: mtspr SPRN_DAR, r4
1378 mtspr SPRN_DSISR, r6
1379 mtspr SPRN_SRR0, r10
1380 mtspr SPRN_SRR1, r11
1381 li r10, BOOK3S_INTERRUPT_DATA_STORAGE
1382 li r11, (MSR_ME << 1) | 1 /* synthesize MSR_SF | MSR_ME */
1383 rotldi r11, r11, 63
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001384fast_interrupt_c_return:
Paul Mackerras697d3892011-12-12 12:36:37 +000013856: ld r7, VCPU_CTR(r9)
1386 lwz r8, VCPU_XER(r9)
1387 mtctr r7
1388 mtxer r8
1389 mr r4, r9
1390 b fast_guest_return
1391
13923: ld r5, VCPU_KVM(r9) /* not relocated, use VRMA */
1393 ld r5, KVM_VRMA_SLB_V(r5)
1394 b 4b
1395
1396 /* If this is for emulated MMIO, load the instruction word */
13972: li r8, KVM_INST_FETCH_FAILED /* In case lwz faults */
1398
1399 /* Set guest mode to 'jump over instruction' so if lwz faults
1400 * we'll just continue at the next IP. */
1401 li r0, KVM_GUEST_MODE_SKIP
1402 stb r0, HSTATE_IN_GUEST(r13)
1403
1404 /* Do the access with MSR:DR enabled */
1405 mfmsr r3
1406 ori r4, r3, MSR_DR /* Enable paging for data */
1407 mtmsrd r4
1408 lwz r8, 0(r10)
1409 mtmsrd r3
1410
1411 /* Store the result */
1412 stw r8, VCPU_LAST_INST(r9)
1413
1414 /* Unset guest mode. */
1415 li r0, KVM_GUEST_MODE_NONE
1416 stb r0, HSTATE_IN_GUEST(r13)
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001417 b guest_exit_cont
Paul Mackerrasde56a942011-06-29 00:21:34 +00001418
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001419/*
Paul Mackerras342d3db2011-12-12 12:38:05 +00001420 * Similarly for an HISI, reflect it to the guest as an ISI unless
1421 * it is an HPTE not found fault for a page that we have paged out.
1422 */
1423kvmppc_hisi:
1424 andis. r0, r11, SRR1_ISI_NOPT@h
1425 beq 1f
1426 andi. r0, r11, MSR_IR /* instruction relocation enabled? */
1427 beq 3f
1428 clrrdi r0, r10, 28
Michael Neulingc75df6f2012-06-25 13:33:10 +00001429 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
Paul Mackerras342d3db2011-12-12 12:38:05 +00001430 bne 1f /* if no SLB entry found */
14314:
1432 /* Search the hash table. */
1433 mr r3, r9 /* vcpu pointer */
1434 mr r4, r10
1435 mr r6, r11
1436 li r7, 0 /* instruction fault */
1437 bl .kvmppc_hpte_hv_fault
1438 ld r9, HSTATE_KVM_VCPU(r13)
1439 ld r10, VCPU_PC(r9)
1440 ld r11, VCPU_MSR(r9)
1441 li r12, BOOK3S_INTERRUPT_H_INST_STORAGE
1442 cmpdi r3, 0 /* retry the instruction */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001443 beq fast_interrupt_c_return
Paul Mackerras342d3db2011-12-12 12:38:05 +00001444 cmpdi r3, -1 /* handle in kernel mode */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001445 beq guest_exit_cont
Paul Mackerras342d3db2011-12-12 12:38:05 +00001446
1447 /* Synthesize an ISI for the guest */
1448 mr r11, r3
14491: mtspr SPRN_SRR0, r10
1450 mtspr SPRN_SRR1, r11
1451 li r10, BOOK3S_INTERRUPT_INST_STORAGE
1452 li r11, (MSR_ME << 1) | 1 /* synthesize MSR_SF | MSR_ME */
1453 rotldi r11, r11, 63
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001454 b fast_interrupt_c_return
Paul Mackerras342d3db2011-12-12 12:38:05 +00001455
14563: ld r6, VCPU_KVM(r9) /* not relocated, use VRMA */
1457 ld r5, KVM_VRMA_SLB_V(r6)
1458 b 4b
1459
1460/*
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001461 * Try to handle an hcall in real mode.
1462 * Returns to the guest if we handle it, or continues on up to
1463 * the kernel if we can't (i.e. if we don't have a handler for
1464 * it, or if the handler returns H_TOO_HARD).
1465 */
1466 .globl hcall_try_real_mode
1467hcall_try_real_mode:
Michael Neulingc75df6f2012-06-25 13:33:10 +00001468 ld r3,VCPU_GPR(R3)(r9)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001469 andi. r0,r11,MSR_PR
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001470 bne guest_exit_cont
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001471 clrrdi r3,r3,2
1472 cmpldi r3,hcall_real_table_end - hcall_real_table
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001473 bge guest_exit_cont
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001474 LOAD_REG_ADDR(r4, hcall_real_table)
Paul Mackerras4baa1d82013-07-08 20:09:53 +10001475 lwax r3,r3,r4
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001476 cmpwi r3,0
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001477 beq guest_exit_cont
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001478 add r3,r3,r4
1479 mtctr r3
1480 mr r3,r9 /* get vcpu pointer */
Michael Neulingc75df6f2012-06-25 13:33:10 +00001481 ld r4,VCPU_GPR(R4)(r9)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001482 bctrl
1483 cmpdi r3,H_TOO_HARD
1484 beq hcall_real_fallback
1485 ld r4,HSTATE_KVM_VCPU(r13)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001486 std r3,VCPU_GPR(R3)(r4)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001487 ld r10,VCPU_PC(r4)
1488 ld r11,VCPU_MSR(r4)
1489 b fast_guest_return
1490
1491 /* We've attempted a real mode hcall, but it's punted it back
1492 * to userspace. We need to restore some clobbered volatiles
1493 * before resuming the pass-it-to-qemu path */
1494hcall_real_fallback:
1495 li r12,BOOK3S_INTERRUPT_SYSCALL
1496 ld r9, HSTATE_KVM_VCPU(r13)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001497
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001498 b guest_exit_cont
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001499
1500 .globl hcall_real_table
1501hcall_real_table:
1502 .long 0 /* 0 - unused */
1503 .long .kvmppc_h_remove - hcall_real_table
1504 .long .kvmppc_h_enter - hcall_real_table
1505 .long .kvmppc_h_read - hcall_real_table
1506 .long 0 /* 0x10 - H_CLEAR_MOD */
1507 .long 0 /* 0x14 - H_CLEAR_REF */
1508 .long .kvmppc_h_protect - hcall_real_table
1509 .long 0 /* 0x1c - H_GET_TCE */
David Gibson54738c02011-06-29 00:22:41 +00001510 .long .kvmppc_h_put_tce - hcall_real_table
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001511 .long 0 /* 0x24 - H_SET_SPRG0 */
1512 .long .kvmppc_h_set_dabr - hcall_real_table
1513 .long 0 /* 0x2c */
1514 .long 0 /* 0x30 */
1515 .long 0 /* 0x34 */
1516 .long 0 /* 0x38 */
1517 .long 0 /* 0x3c */
1518 .long 0 /* 0x40 */
1519 .long 0 /* 0x44 */
1520 .long 0 /* 0x48 */
1521 .long 0 /* 0x4c */
1522 .long 0 /* 0x50 */
1523 .long 0 /* 0x54 */
1524 .long 0 /* 0x58 */
1525 .long 0 /* 0x5c */
1526 .long 0 /* 0x60 */
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +00001527#ifdef CONFIG_KVM_XICS
1528 .long .kvmppc_rm_h_eoi - hcall_real_table
1529 .long .kvmppc_rm_h_cppr - hcall_real_table
1530 .long .kvmppc_rm_h_ipi - hcall_real_table
1531 .long 0 /* 0x70 - H_IPOLL */
1532 .long .kvmppc_rm_h_xirr - hcall_real_table
1533#else
1534 .long 0 /* 0x64 - H_EOI */
1535 .long 0 /* 0x68 - H_CPPR */
1536 .long 0 /* 0x6c - H_IPI */
1537 .long 0 /* 0x70 - H_IPOLL */
1538 .long 0 /* 0x74 - H_XIRR */
1539#endif
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001540 .long 0 /* 0x78 */
1541 .long 0 /* 0x7c */
1542 .long 0 /* 0x80 */
1543 .long 0 /* 0x84 */
1544 .long 0 /* 0x88 */
1545 .long 0 /* 0x8c */
1546 .long 0 /* 0x90 */
1547 .long 0 /* 0x94 */
1548 .long 0 /* 0x98 */
1549 .long 0 /* 0x9c */
1550 .long 0 /* 0xa0 */
1551 .long 0 /* 0xa4 */
1552 .long 0 /* 0xa8 */
1553 .long 0 /* 0xac */
1554 .long 0 /* 0xb0 */
1555 .long 0 /* 0xb4 */
1556 .long 0 /* 0xb8 */
1557 .long 0 /* 0xbc */
1558 .long 0 /* 0xc0 */
1559 .long 0 /* 0xc4 */
1560 .long 0 /* 0xc8 */
1561 .long 0 /* 0xcc */
1562 .long 0 /* 0xd0 */
1563 .long 0 /* 0xd4 */
1564 .long 0 /* 0xd8 */
1565 .long 0 /* 0xdc */
Paul Mackerras19ccb762011-07-23 17:42:46 +10001566 .long .kvmppc_h_cede - hcall_real_table
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001567 .long 0 /* 0xe4 */
1568 .long 0 /* 0xe8 */
1569 .long 0 /* 0xec */
1570 .long 0 /* 0xf0 */
1571 .long 0 /* 0xf4 */
1572 .long 0 /* 0xf8 */
1573 .long 0 /* 0xfc */
1574 .long 0 /* 0x100 */
1575 .long 0 /* 0x104 */
1576 .long 0 /* 0x108 */
1577 .long 0 /* 0x10c */
1578 .long 0 /* 0x110 */
1579 .long 0 /* 0x114 */
1580 .long 0 /* 0x118 */
1581 .long 0 /* 0x11c */
1582 .long 0 /* 0x120 */
1583 .long .kvmppc_h_bulk_remove - hcall_real_table
1584hcall_real_table_end:
1585
Paul Mackerrasde56a942011-06-29 00:21:34 +00001586ignore_hdec:
1587 mr r4,r9
1588 b fast_guest_return
1589
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001590_GLOBAL(kvmppc_h_set_dabr)
1591 std r4,VCPU_DABR(r3)
Paul Mackerras89436332012-03-02 01:38:23 +00001592 /* Work around P7 bug where DABR can get corrupted on mtspr */
15931: mtspr SPRN_DABR,r4
1594 mfspr r5, SPRN_DABR
1595 cmpd r4, r5
1596 bne 1b
1597 isync
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001598 li r3,0
1599 blr
1600
Paul Mackerras19ccb762011-07-23 17:42:46 +10001601_GLOBAL(kvmppc_h_cede)
1602 ori r11,r11,MSR_EE
1603 std r11,VCPU_MSR(r3)
1604 li r0,1
1605 stb r0,VCPU_CEDED(r3)
1606 sync /* order setting ceded vs. testing prodded */
1607 lbz r5,VCPU_PRODDED(r3)
1608 cmpwi r5,0
Paul Mackerras04f995a2012-08-06 00:03:28 +00001609 bne kvm_cede_prodded
Paul Mackerras19ccb762011-07-23 17:42:46 +10001610 li r0,0 /* set trap to 0 to say hcall is handled */
1611 stw r0,VCPU_TRAP(r3)
1612 li r0,H_SUCCESS
Michael Neulingc75df6f2012-06-25 13:33:10 +00001613 std r0,VCPU_GPR(R3)(r3)
Paul Mackerras19ccb762011-07-23 17:42:46 +10001614BEGIN_FTR_SECTION
Paul Mackerras04f995a2012-08-06 00:03:28 +00001615 b kvm_cede_exit /* just send it up to host on 970 */
Paul Mackerras19ccb762011-07-23 17:42:46 +10001616END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_206)
1617
1618 /*
1619 * Set our bit in the bitmask of napping threads unless all the
1620 * other threads are already napping, in which case we send this
1621 * up to the host.
1622 */
1623 ld r5,HSTATE_KVM_VCORE(r13)
1624 lwz r6,VCPU_PTID(r3)
1625 lwz r8,VCORE_ENTRY_EXIT(r5)
1626 clrldi r8,r8,56
1627 li r0,1
1628 sld r0,r0,r6
1629 addi r6,r5,VCORE_NAPPING_THREADS
163031: lwarx r4,0,r6
1631 or r4,r4,r0
Michael Neulingc75df6f2012-06-25 13:33:10 +00001632 PPC_POPCNTW(R7,R4)
Paul Mackerras19ccb762011-07-23 17:42:46 +10001633 cmpw r7,r8
Paul Mackerras04f995a2012-08-06 00:03:28 +00001634 bge kvm_cede_exit
Paul Mackerras19ccb762011-07-23 17:42:46 +10001635 stwcx. r4,0,r6
1636 bne 31b
1637 li r0,1
1638 stb r0,HSTATE_NAPPING(r13)
1639 /* order napping_threads update vs testing entry_exit_count */
1640 lwsync
1641 mr r4,r3
1642 lwz r7,VCORE_ENTRY_EXIT(r5)
1643 cmpwi r7,0x100
1644 bge 33f /* another thread already exiting */
1645
1646/*
1647 * Although not specifically required by the architecture, POWER7
1648 * preserves the following registers in nap mode, even if an SMT mode
1649 * switch occurs: SLB entries, PURR, SPURR, AMOR, UAMOR, AMR, SPRG0-3,
1650 * DAR, DSISR, DABR, DABRX, DSCR, PMCx, MMCRx, SIAR, SDAR.
1651 */
1652 /* Save non-volatile GPRs */
Michael Neulingc75df6f2012-06-25 13:33:10 +00001653 std r14, VCPU_GPR(R14)(r3)
1654 std r15, VCPU_GPR(R15)(r3)
1655 std r16, VCPU_GPR(R16)(r3)
1656 std r17, VCPU_GPR(R17)(r3)
1657 std r18, VCPU_GPR(R18)(r3)
1658 std r19, VCPU_GPR(R19)(r3)
1659 std r20, VCPU_GPR(R20)(r3)
1660 std r21, VCPU_GPR(R21)(r3)
1661 std r22, VCPU_GPR(R22)(r3)
1662 std r23, VCPU_GPR(R23)(r3)
1663 std r24, VCPU_GPR(R24)(r3)
1664 std r25, VCPU_GPR(R25)(r3)
1665 std r26, VCPU_GPR(R26)(r3)
1666 std r27, VCPU_GPR(R27)(r3)
1667 std r28, VCPU_GPR(R28)(r3)
1668 std r29, VCPU_GPR(R29)(r3)
1669 std r30, VCPU_GPR(R30)(r3)
1670 std r31, VCPU_GPR(R31)(r3)
Paul Mackerras19ccb762011-07-23 17:42:46 +10001671
1672 /* save FP state */
1673 bl .kvmppc_save_fp
1674
1675 /*
1676 * Take a nap until a decrementer or external interrupt occurs,
1677 * with PECE1 (wake on decr) and PECE0 (wake on external) set in LPCR
1678 */
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001679 li r0,1
1680 stb r0,HSTATE_HWTHREAD_REQ(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10001681 mfspr r5,SPRN_LPCR
1682 ori r5,r5,LPCR_PECE0 | LPCR_PECE1
1683 mtspr SPRN_LPCR,r5
1684 isync
1685 li r0, 0
1686 std r0, HSTATE_SCRATCH0(r13)
1687 ptesync
1688 ld r0, HSTATE_SCRATCH0(r13)
16891: cmpd r0, r0
1690 bne 1b
1691 nap
1692 b .
1693
1694kvm_end_cede:
Paul Mackerras4619ac82013-04-17 20:31:41 +00001695 /* get vcpu pointer */
1696 ld r4, HSTATE_KVM_VCPU(r13)
1697
Paul Mackerras19ccb762011-07-23 17:42:46 +10001698 /* Woken by external or decrementer interrupt */
1699 ld r1, HSTATE_HOST_R1(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10001700
Paul Mackerras19ccb762011-07-23 17:42:46 +10001701 /* load up FP state */
1702 bl kvmppc_load_fp
1703
1704 /* Load NV GPRS */
Michael Neulingc75df6f2012-06-25 13:33:10 +00001705 ld r14, VCPU_GPR(R14)(r4)
1706 ld r15, VCPU_GPR(R15)(r4)
1707 ld r16, VCPU_GPR(R16)(r4)
1708 ld r17, VCPU_GPR(R17)(r4)
1709 ld r18, VCPU_GPR(R18)(r4)
1710 ld r19, VCPU_GPR(R19)(r4)
1711 ld r20, VCPU_GPR(R20)(r4)
1712 ld r21, VCPU_GPR(R21)(r4)
1713 ld r22, VCPU_GPR(R22)(r4)
1714 ld r23, VCPU_GPR(R23)(r4)
1715 ld r24, VCPU_GPR(R24)(r4)
1716 ld r25, VCPU_GPR(R25)(r4)
1717 ld r26, VCPU_GPR(R26)(r4)
1718 ld r27, VCPU_GPR(R27)(r4)
1719 ld r28, VCPU_GPR(R28)(r4)
1720 ld r29, VCPU_GPR(R29)(r4)
1721 ld r30, VCPU_GPR(R30)(r4)
1722 ld r31, VCPU_GPR(R31)(r4)
Paul Mackerras19ccb762011-07-23 17:42:46 +10001723
1724 /* clear our bit in vcore->napping_threads */
172533: ld r5,HSTATE_KVM_VCORE(r13)
1726 lwz r3,VCPU_PTID(r4)
1727 li r0,1
1728 sld r0,r0,r3
1729 addi r6,r5,VCORE_NAPPING_THREADS
173032: lwarx r7,0,r6
1731 andc r7,r7,r0
1732 stwcx. r7,0,r6
1733 bne 32b
1734 li r0,0
1735 stb r0,HSTATE_NAPPING(r13)
1736
Paul Mackerras4619ac82013-04-17 20:31:41 +00001737 /* Check the wake reason in SRR1 to see why we got here */
1738 mfspr r3, SPRN_SRR1
1739 rlwinm r3, r3, 44-31, 0x7 /* extract wake reason field */
1740 cmpwi r3, 4 /* was it an external interrupt? */
1741 li r12, BOOK3S_INTERRUPT_EXTERNAL
1742 mr r9, r4
1743 ld r10, VCPU_PC(r9)
1744 ld r11, VCPU_MSR(r9)
1745 beq do_ext_interrupt /* if so */
1746
Paul Mackerras19ccb762011-07-23 17:42:46 +10001747 /* see if any other thread is already exiting */
1748 lwz r0,VCORE_ENTRY_EXIT(r5)
1749 cmpwi r0,0x100
1750 blt kvmppc_cede_reentry /* if not go back to guest */
1751
1752 /* some threads are exiting, so go to the guest exit path */
1753 b hcall_real_fallback
1754
1755 /* cede when already previously prodded case */
Paul Mackerras04f995a2012-08-06 00:03:28 +00001756kvm_cede_prodded:
1757 li r0,0
Paul Mackerras19ccb762011-07-23 17:42:46 +10001758 stb r0,VCPU_PRODDED(r3)
1759 sync /* order testing prodded vs. clearing ceded */
1760 stb r0,VCPU_CEDED(r3)
1761 li r3,H_SUCCESS
1762 blr
1763
1764 /* we've ceded but we want to give control to the host */
Paul Mackerras04f995a2012-08-06 00:03:28 +00001765kvm_cede_exit:
Paul Mackerras4619ac82013-04-17 20:31:41 +00001766 b hcall_real_fallback
Paul Mackerras19ccb762011-07-23 17:42:46 +10001767
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001768 /* Try to handle a machine check in real mode */
1769machine_check_realmode:
1770 mr r3, r9 /* get vcpu pointer */
1771 bl .kvmppc_realmode_machine_check
1772 nop
1773 cmpdi r3, 0 /* continue exiting from guest? */
1774 ld r9, HSTATE_KVM_VCPU(r13)
1775 li r12, BOOK3S_INTERRUPT_MACHINE_CHECK
1776 beq mc_cont
1777 /* If not, deliver a machine check. SRR0/1 are already set */
1778 li r10, BOOK3S_INTERRUPT_MACHINE_CHECK
1779 li r11, (MSR_ME << 1) | 1 /* synthesize MSR_SF | MSR_ME */
1780 rotldi r11, r11, 63
1781 b fast_interrupt_c_return
1782
Paul Mackerrasde56a942011-06-29 00:21:34 +00001783/*
Paul Mackerrasc9342432013-09-06 13:24:13 +10001784 * Determine what sort of external interrupt is pending (if any).
1785 * Returns:
1786 * 0 if no interrupt is pending
1787 * 1 if an interrupt is pending that needs to be handled by the host
1788 * -1 if there was a guest wakeup IPI (which has now been cleared)
1789 */
1790kvmppc_read_intr:
1791 /* see if a host IPI is pending */
1792 li r3, 1
1793 lbz r0, HSTATE_HOST_IPI(r13)
1794 cmpwi r0, 0
1795 bne 1f
1796
1797 /* Now read the interrupt from the ICP */
1798 ld r6, HSTATE_XICS_PHYS(r13)
1799 li r7, XICS_XIRR
1800 cmpdi r6, 0
1801 beq- 1f
1802 lwzcix r0, r6, r7
1803 rlwinm. r3, r0, 0, 0xffffff
1804 sync
1805 beq 1f /* if nothing pending in the ICP */
1806
1807 /* We found something in the ICP...
1808 *
1809 * If it's not an IPI, stash it in the PACA and return to
1810 * the host, we don't (yet) handle directing real external
1811 * interrupts directly to the guest
1812 */
1813 cmpwi r3, XICS_IPI /* if there is, is it an IPI? */
1814 li r3, 1
1815 bne 42f
1816
1817 /* It's an IPI, clear the MFRR and EOI it */
1818 li r3, 0xff
1819 li r8, XICS_MFRR
1820 stbcix r3, r6, r8 /* clear the IPI */
1821 stwcix r0, r6, r7 /* EOI it */
1822 sync
1823
1824 /* We need to re-check host IPI now in case it got set in the
1825 * meantime. If it's clear, we bounce the interrupt to the
1826 * guest
1827 */
1828 lbz r0, HSTATE_HOST_IPI(r13)
1829 cmpwi r0, 0
1830 bne- 43f
1831
1832 /* OK, it's an IPI for us */
1833 li r3, -1
18341: blr
1835
183642: /* It's not an IPI and it's for the host, stash it in the PACA
1837 * before exit, it will be picked up by the host ICP driver
1838 */
1839 stw r0, HSTATE_SAVED_XIRR(r13)
1840 b 1b
1841
184243: /* We raced with the host, we need to resend that IPI, bummer */
1843 li r0, IPI_PRIORITY
1844 stbcix r0, r6, r8 /* set the IPI */
1845 sync
1846 b 1b
1847
1848/*
Paul Mackerrasde56a942011-06-29 00:21:34 +00001849 * Save away FP, VMX and VSX registers.
1850 * r3 = vcpu pointer
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001851 */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001852_GLOBAL(kvmppc_save_fp)
Paul Mackerras89436332012-03-02 01:38:23 +00001853 mfmsr r5
1854 ori r8,r5,MSR_FP
Paul Mackerrasde56a942011-06-29 00:21:34 +00001855#ifdef CONFIG_ALTIVEC
1856BEGIN_FTR_SECTION
1857 oris r8,r8,MSR_VEC@h
1858END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
1859#endif
1860#ifdef CONFIG_VSX
1861BEGIN_FTR_SECTION
1862 oris r8,r8,MSR_VSX@h
1863END_FTR_SECTION_IFSET(CPU_FTR_VSX)
1864#endif
1865 mtmsrd r8
1866 isync
1867#ifdef CONFIG_VSX
1868BEGIN_FTR_SECTION
1869 reg = 0
1870 .rept 32
1871 li r6,reg*16+VCPU_VSRS
Michael Neulingc75df6f2012-06-25 13:33:10 +00001872 STXVD2X(reg,R6,R3)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001873 reg = reg + 1
1874 .endr
1875FTR_SECTION_ELSE
1876#endif
1877 reg = 0
1878 .rept 32
1879 stfd reg,reg*8+VCPU_FPRS(r3)
1880 reg = reg + 1
1881 .endr
1882#ifdef CONFIG_VSX
1883ALT_FTR_SECTION_END_IFSET(CPU_FTR_VSX)
1884#endif
1885 mffs fr0
1886 stfd fr0,VCPU_FPSCR(r3)
1887
1888#ifdef CONFIG_ALTIVEC
1889BEGIN_FTR_SECTION
1890 reg = 0
1891 .rept 32
1892 li r6,reg*16+VCPU_VRS
1893 stvx reg,r6,r3
1894 reg = reg + 1
1895 .endr
1896 mfvscr vr0
1897 li r6,VCPU_VSCR
1898 stvx vr0,r6,r3
1899END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
1900#endif
1901 mfspr r6,SPRN_VRSAVE
1902 stw r6,VCPU_VRSAVE(r3)
Paul Mackerras89436332012-03-02 01:38:23 +00001903 mtmsrd r5
Paul Mackerrasde56a942011-06-29 00:21:34 +00001904 isync
1905 blr
1906
1907/*
1908 * Load up FP, VMX and VSX registers
1909 * r4 = vcpu pointer
1910 */
1911 .globl kvmppc_load_fp
1912kvmppc_load_fp:
1913 mfmsr r9
1914 ori r8,r9,MSR_FP
1915#ifdef CONFIG_ALTIVEC
1916BEGIN_FTR_SECTION
1917 oris r8,r8,MSR_VEC@h
1918END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
1919#endif
1920#ifdef CONFIG_VSX
1921BEGIN_FTR_SECTION
1922 oris r8,r8,MSR_VSX@h
1923END_FTR_SECTION_IFSET(CPU_FTR_VSX)
1924#endif
1925 mtmsrd r8
1926 isync
1927 lfd fr0,VCPU_FPSCR(r4)
1928 MTFSF_L(fr0)
1929#ifdef CONFIG_VSX
1930BEGIN_FTR_SECTION
1931 reg = 0
1932 .rept 32
1933 li r7,reg*16+VCPU_VSRS
Michael Neulingc75df6f2012-06-25 13:33:10 +00001934 LXVD2X(reg,R7,R4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001935 reg = reg + 1
1936 .endr
1937FTR_SECTION_ELSE
1938#endif
1939 reg = 0
1940 .rept 32
1941 lfd reg,reg*8+VCPU_FPRS(r4)
1942 reg = reg + 1
1943 .endr
1944#ifdef CONFIG_VSX
1945ALT_FTR_SECTION_END_IFSET(CPU_FTR_VSX)
1946#endif
1947
1948#ifdef CONFIG_ALTIVEC
1949BEGIN_FTR_SECTION
1950 li r7,VCPU_VSCR
1951 lvx vr0,r7,r4
1952 mtvscr vr0
1953 reg = 0
1954 .rept 32
1955 li r7,reg*16+VCPU_VRS
1956 lvx reg,r7,r4
1957 reg = reg + 1
1958 .endr
1959END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
1960#endif
1961 lwz r7,VCPU_VRSAVE(r4)
1962 mtspr SPRN_VRSAVE,r7
1963 blr