KVM: PPC: Book3S HV: Don't set DABR on POWER8
[deliverable/linux.git] / arch / powerpc / kvm / book3s_hv_rmhandlers.S
CommitLineData
de56a948
PM
1/*
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>
177339d7 23#include <asm/mmu.h>
de56a948 24#include <asm/page.h>
177339d7
PM
25#include <asm/ptrace.h>
26#include <asm/hvcall.h>
de56a948
PM
27#include <asm/asm-offsets.h>
28#include <asm/exception-64s.h>
f0888f70 29#include <asm/kvm_book3s_asm.h>
b4072df4 30#include <asm/mmu-hash64.h>
de56a948 31
7ffcf8ec
AB
32#ifdef __LITTLE_ENDIAN__
33#error Need to fix lppaca and SLB shadow accesses in little endian mode
34#endif
35
de56a948 36/*
19ccb76a 37 * Call kvmppc_hv_entry in real mode.
de56a948
PM
38 * Must be called with interrupts hard-disabled.
39 *
40 * Input Registers:
41 *
42 * LR = return address to continue at after eventually re-enabling MMU
43 */
44_GLOBAL(kvmppc_hv_entry_trampoline)
218309b7
PM
45 mflr r0
46 std r0, PPC_LR_STKOFF(r1)
47 stdu r1, -112(r1)
de56a948 48 mfmsr r10
218309b7 49 LOAD_REG_ADDR(r5, kvmppc_call_hv_entry)
de56a948
PM
50 li r0,MSR_RI
51 andc r0,r10,r0
52 li r6,MSR_IR | MSR_DR
53 andc r6,r10,r6
54 mtmsrd r0,1 /* clear RI in MSR */
55 mtsrr0 r5
56 mtsrr1 r6
57 RFI
58
218309b7
PM
59kvmppc_call_hv_entry:
60 bl kvmppc_hv_entry
61
62 /* Back from guest - restore host state and return to caller */
63
eee7ff9d 64BEGIN_FTR_SECTION
218309b7
PM
65 /* Restore host DABR and DABRX */
66 ld r5,HSTATE_DABR(r13)
67 li r6,7
68 mtspr SPRN_DABR,r5
69 mtspr SPRN_DABRX,r6
eee7ff9d 70END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
218309b7
PM
71
72 /* Restore SPRG3 */
73 ld r3,PACA_SPRG3(r13)
74 mtspr SPRN_SPRG3,r3
75
76 /*
77 * Reload DEC. HDEC interrupts were disabled when
78 * we reloaded the host's LPCR value.
79 */
80 ld r3, HSTATE_DECEXP(r13)
81 mftb r4
82 subf r4, r4, r3
83 mtspr SPRN_DEC, r4
84
85 /* Reload the host's PMU registers */
86 ld r3, PACALPPACAPTR(r13) /* is the host using the PMU? */
87 lbz r4, LPPACA_PMCINUSE(r3)
88 cmpwi r4, 0
89 beq 23f /* skip if not */
90 lwz r3, HSTATE_PMC(r13)
91 lwz r4, HSTATE_PMC + 4(r13)
92 lwz r5, HSTATE_PMC + 8(r13)
93 lwz r6, HSTATE_PMC + 12(r13)
94 lwz r8, HSTATE_PMC + 16(r13)
95 lwz r9, HSTATE_PMC + 20(r13)
96BEGIN_FTR_SECTION
97 lwz r10, HSTATE_PMC + 24(r13)
98 lwz r11, HSTATE_PMC + 28(r13)
99END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
100 mtspr SPRN_PMC1, r3
101 mtspr SPRN_PMC2, r4
102 mtspr SPRN_PMC3, r5
103 mtspr SPRN_PMC4, r6
104 mtspr SPRN_PMC5, r8
105 mtspr SPRN_PMC6, r9
106BEGIN_FTR_SECTION
107 mtspr SPRN_PMC7, r10
108 mtspr SPRN_PMC8, r11
109END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
110 ld r3, HSTATE_MMCR(r13)
111 ld r4, HSTATE_MMCR + 8(r13)
112 ld r5, HSTATE_MMCR + 16(r13)
113 mtspr SPRN_MMCR1, r4
114 mtspr SPRN_MMCRA, r5
115 mtspr SPRN_MMCR0, r3
116 isync
11723:
118
119 /*
120 * For external and machine check interrupts, we need
121 * to call the Linux handler to process the interrupt.
122 * We do that by jumping to absolute address 0x500 for
123 * external interrupts, or the machine_check_fwnmi label
124 * for machine checks (since firmware might have patched
125 * the vector area at 0x200). The [h]rfid at the end of the
126 * handler will return to the book3s_hv_interrupts.S code.
127 * For other interrupts we do the rfid to get back
128 * to the book3s_hv_interrupts.S code here.
129 */
130 ld r8, 112+PPC_LR_STKOFF(r1)
131 addi r1, r1, 112
132 ld r7, HSTATE_HOST_MSR(r13)
133
134 cmpwi cr1, r12, BOOK3S_INTERRUPT_MACHINE_CHECK
135 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
136BEGIN_FTR_SECTION
137 beq 11f
138END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
139
140 /* RFI into the highmem handler, or branch to interrupt handler */
141 mfmsr r6
142 li r0, MSR_RI
143 andc r6, r6, r0
144 mtmsrd r6, 1 /* Clear RI in MSR */
145 mtsrr0 r8
146 mtsrr1 r7
147 beqa 0x500 /* external interrupt (PPC970) */
148 beq cr1, 13f /* machine check */
149 RFI
150
151 /* On POWER7, we have external interrupts set to use HSRR0/1 */
15211: mtspr SPRN_HSRR0, r8
153 mtspr SPRN_HSRR1, r7
154 ba 0x500
155
15613: b machine_check_fwnmi
157
de56a948 158
371fefd6
PM
159/*
160 * We come in here when wakened from nap mode on a secondary hw thread.
161 * Relocation is off and most register values are lost.
162 * r13 points to the PACA.
163 */
164 .globl kvm_start_guest
165kvm_start_guest:
166 ld r1,PACAEMERGSP(r13)
167 subi r1,r1,STACK_FRAME_OVERHEAD
19ccb76a
PM
168 ld r2,PACATOC(r13)
169
f0888f70
PM
170 li r0,KVM_HWTHREAD_IN_KVM
171 stb r0,HSTATE_HWTHREAD_STATE(r13)
371fefd6 172
f0888f70
PM
173 /* NV GPR values from power7_idle() will no longer be valid */
174 li r0,1
175 stb r0,PACA_NAPSTATELOST(r13)
371fefd6 176
4619ac88
PM
177 /* were we napping due to cede? */
178 lbz r0,HSTATE_NAPPING(r13)
179 cmpwi r0,0
180 bne kvm_end_cede
181
182 /*
183 * We weren't napping due to cede, so this must be a secondary
184 * thread being woken up to run a guest, or being woken up due
185 * to a stray IPI. (Or due to some machine check or hypervisor
186 * maintenance interrupt while the core is in KVM.)
187 */
f0888f70
PM
188
189 /* Check the wake reason in SRR1 to see why we got here */
190 mfspr r3,SPRN_SRR1
191 rlwinm r3,r3,44-31,0x7 /* extract wake reason field */
192 cmpwi r3,4 /* was it an external interrupt? */
4619ac88
PM
193 bne 27f /* if not */
194 ld r5,HSTATE_XICS_PHYS(r13)
195 li r7,XICS_XIRR /* if it was an external interrupt, */
f0888f70 196 lwzcix r8,r5,r7 /* get and ack the interrupt */
371fefd6 197 sync
f0888f70 198 clrldi. r9,r8,40 /* get interrupt source ID. */
4619ac88
PM
199 beq 28f /* none there? */
200 cmpwi r9,XICS_IPI /* was it an IPI? */
201 bne 29f
202 li r0,0xff
203 li r6,XICS_MFRR
f0888f70 204 stbcix r0,r5,r6 /* clear IPI */
4619ac88
PM
205 stwcix r8,r5,r7 /* EOI the interrupt */
206 sync /* order loading of vcpu after that */
371fefd6 207
4619ac88 208 /* get vcpu pointer, NULL if we have no vcpu to run */
7b444c67
PM
209 ld r4,HSTATE_KVM_VCPU(r13)
210 cmpdi r4,0
f0888f70 211 /* if we have no vcpu to run, go back to sleep */
7b444c67 212 beq kvm_no_guest
218309b7 213 b 30f
f0888f70 214
4619ac88
PM
21527: /* XXX should handle hypervisor maintenance interrupts etc. here */
216 b kvm_no_guest
21728: /* SRR1 said external but ICP said nope?? */
218 b kvm_no_guest
21929: /* External non-IPI interrupt to offline secondary thread? help?? */
220 stw r8,HSTATE_SAVED_XIRR(r13)
221 b kvm_no_guest
2fde6d20 222
218309b7
PM
22330: bl kvmppc_hv_entry
224
225 /* Back from the guest, go back to nap */
226 /* Clear our vcpu pointer so we don't come back in early */
227 li r0, 0
228 std r0, HSTATE_KVM_VCPU(r13)
229 lwsync
230 /* Clear any pending IPI - we're an offline thread */
231 ld r5, HSTATE_XICS_PHYS(r13)
232 li r7, XICS_XIRR
233 lwzcix r3, r5, r7 /* ack any pending interrupt */
234 rlwinm. r0, r3, 0, 0xffffff /* any pending? */
235 beq 37f
236 sync
237 li r0, 0xff
238 li r6, XICS_MFRR
239 stbcix r0, r5, r6 /* clear the IPI */
240 stwcix r3, r5, r7 /* EOI it */
24137: sync
242
243 /* increment the nap count and then go to nap mode */
244 ld r4, HSTATE_KVM_VCORE(r13)
245 addi r4, r4, VCORE_NAP_COUNT
246 lwsync /* make previous updates visible */
24751: lwarx r3, 0, r4
248 addi r3, r3, 1
249 stwcx. r3, 0, r4
250 bne 51b
251
252kvm_no_guest:
253 li r0, KVM_HWTHREAD_IN_NAP
254 stb r0, HSTATE_HWTHREAD_STATE(r13)
255 li r3, LPCR_PECE0
256 mfspr r4, SPRN_LPCR
257 rlwimi r4, r3, 0, LPCR_PECE0 | LPCR_PECE1
258 mtspr SPRN_LPCR, r4
259 isync
260 std r0, HSTATE_SCRATCH0(r13)
261 ptesync
262 ld r0, HSTATE_SCRATCH0(r13)
2631: cmpd r0, r0
264 bne 1b
265 nap
266 b .
267
268/******************************************************************************
269 * *
270 * Entry code *
271 * *
272 *****************************************************************************/
273
de56a948
PM
274.global kvmppc_hv_entry
275kvmppc_hv_entry:
276
277 /* Required state:
278 *
279 * R4 = vcpu pointer
280 * MSR = ~IR|DR
281 * R13 = PACA
282 * R1 = host R1
283 * all other volatile GPRS = free
284 */
285 mflr r0
218309b7
PM
286 std r0, PPC_LR_STKOFF(r1)
287 stdu r1, -112(r1)
de56a948 288
eee7ff9d 289BEGIN_FTR_SECTION
8943633c
PM
290 /* Set partition DABR */
291 /* Do this before re-enabling PMU to avoid P7 DABR corruption bug */
292 li r5,3
293 ld r6,VCPU_DABR(r4)
294 mtspr SPRN_DABRX,r5
295 mtspr SPRN_DABR,r6
eee7ff9d 296 BEGIN_FTR_SECTION_NESTED(89)
8943633c 297 isync
eee7ff9d
MN
298 END_FTR_SECTION_NESTED(CPU_FTR_ARCH_206, CPU_FTR_ARCH_206, 89)
299END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
de56a948
PM
300
301 /* Load guest PMU registers */
302 /* R4 is live here (vcpu pointer) */
303 li r3, 1
304 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
305 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
306 isync
307 lwz r3, VCPU_PMC(r4) /* always load up guest PMU registers */
308 lwz r5, VCPU_PMC + 4(r4) /* to prevent information leak */
309 lwz r6, VCPU_PMC + 8(r4)
310 lwz r7, VCPU_PMC + 12(r4)
311 lwz r8, VCPU_PMC + 16(r4)
312 lwz r9, VCPU_PMC + 20(r4)
9e368f29
PM
313BEGIN_FTR_SECTION
314 lwz r10, VCPU_PMC + 24(r4)
315 lwz r11, VCPU_PMC + 28(r4)
316END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
de56a948
PM
317 mtspr SPRN_PMC1, r3
318 mtspr SPRN_PMC2, r5
319 mtspr SPRN_PMC3, r6
320 mtspr SPRN_PMC4, r7
321 mtspr SPRN_PMC5, r8
322 mtspr SPRN_PMC6, r9
9e368f29
PM
323BEGIN_FTR_SECTION
324 mtspr SPRN_PMC7, r10
325 mtspr SPRN_PMC8, r11
326END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
de56a948
PM
327 ld r3, VCPU_MMCR(r4)
328 ld r5, VCPU_MMCR + 8(r4)
329 ld r6, VCPU_MMCR + 16(r4)
14941789
PM
330 ld r7, VCPU_SIAR(r4)
331 ld r8, VCPU_SDAR(r4)
de56a948
PM
332 mtspr SPRN_MMCR1, r5
333 mtspr SPRN_MMCRA, r6
14941789
PM
334 mtspr SPRN_SIAR, r7
335 mtspr SPRN_SDAR, r8
de56a948
PM
336 mtspr SPRN_MMCR0, r3
337 isync
338
339 /* Load up FP, VMX and VSX registers */
340 bl kvmppc_load_fp
341
c75df6f9
MN
342 ld r14, VCPU_GPR(R14)(r4)
343 ld r15, VCPU_GPR(R15)(r4)
344 ld r16, VCPU_GPR(R16)(r4)
345 ld r17, VCPU_GPR(R17)(r4)
346 ld r18, VCPU_GPR(R18)(r4)
347 ld r19, VCPU_GPR(R19)(r4)
348 ld r20, VCPU_GPR(R20)(r4)
349 ld r21, VCPU_GPR(R21)(r4)
350 ld r22, VCPU_GPR(R22)(r4)
351 ld r23, VCPU_GPR(R23)(r4)
352 ld r24, VCPU_GPR(R24)(r4)
353 ld r25, VCPU_GPR(R25)(r4)
354 ld r26, VCPU_GPR(R26)(r4)
355 ld r27, VCPU_GPR(R27)(r4)
356 ld r28, VCPU_GPR(R28)(r4)
357 ld r29, VCPU_GPR(R29)(r4)
358 ld r30, VCPU_GPR(R30)(r4)
359 ld r31, VCPU_GPR(R31)(r4)
8943633c 360
9e368f29 361BEGIN_FTR_SECTION
de56a948
PM
362 /* Switch DSCR to guest value */
363 ld r5, VCPU_DSCR(r4)
364 mtspr SPRN_DSCR, r5
9e368f29 365END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
de56a948
PM
366
367 /*
368 * Set the decrementer to the guest decrementer.
369 */
370 ld r8,VCPU_DEC_EXPIRES(r4)
371 mftb r7
372 subf r3,r7,r8
373 mtspr SPRN_DEC,r3
374 stw r3,VCPU_DEC(r4)
375
376 ld r5, VCPU_SPRG0(r4)
377 ld r6, VCPU_SPRG1(r4)
378 ld r7, VCPU_SPRG2(r4)
379 ld r8, VCPU_SPRG3(r4)
380 mtspr SPRN_SPRG0, r5
381 mtspr SPRN_SPRG1, r6
382 mtspr SPRN_SPRG2, r7
383 mtspr SPRN_SPRG3, r8
384
385 /* Save R1 in the PACA */
386 std r1, HSTATE_HOST_R1(r13)
387
388 /* Load up DAR and DSISR */
389 ld r5, VCPU_DAR(r4)
390 lwz r6, VCPU_DSISR(r4)
391 mtspr SPRN_DAR, r5
392 mtspr SPRN_DSISR, r6
393
44a3add8
PM
394 li r6, KVM_GUEST_MODE_HOST_HV
395 stb r6, HSTATE_IN_GUEST(r13)
396
9e368f29 397BEGIN_FTR_SECTION
de56a948
PM
398 /* Restore AMR and UAMOR, set AMOR to all 1s */
399 ld r5,VCPU_AMR(r4)
400 ld r6,VCPU_UAMOR(r4)
401 li r7,-1
402 mtspr SPRN_AMR,r5
403 mtspr SPRN_UAMOR,r6
404 mtspr SPRN_AMOR,r7
9e368f29 405END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
de56a948
PM
406
407 /* Clear out SLB */
408 li r6,0
409 slbmte r6,r6
410 slbia
411 ptesync
412
9e368f29
PM
413BEGIN_FTR_SECTION
414 b 30f
415END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
416 /*
417 * POWER7 host -> guest partition switch code.
418 * We don't have to lock against concurrent tlbies,
419 * but we do have to coordinate across hardware threads.
420 */
371fefd6
PM
421 /* Increment entry count iff exit count is zero. */
422 ld r5,HSTATE_KVM_VCORE(r13)
423 addi r9,r5,VCORE_ENTRY_EXIT
42421: lwarx r3,0,r9
425 cmpwi r3,0x100 /* any threads starting to exit? */
426 bge secondary_too_late /* if so we're too late to the party */
427 addi r3,r3,1
428 stwcx. r3,0,r9
429 bne 21b
430
431 /* Primary thread switches to guest partition. */
aa04b4cc 432 ld r9,VCPU_KVM(r4) /* pointer to struct kvm */
371fefd6
PM
433 lwz r6,VCPU_PTID(r4)
434 cmpwi r6,0
435 bne 20f
de56a948
PM
436 ld r6,KVM_SDR1(r9)
437 lwz r7,KVM_LPID(r9)
438 li r0,LPID_RSVD /* switch to reserved LPID */
439 mtspr SPRN_LPID,r0
440 ptesync
441 mtspr SPRN_SDR1,r6 /* switch to partition page table */
442 mtspr SPRN_LPID,r7
443 isync
1b400ba0
PM
444
445 /* See if we need to flush the TLB */
446 lhz r6,PACAPACAINDEX(r13) /* test_bit(cpu, need_tlb_flush) */
447 clrldi r7,r6,64-6 /* extract bit number (6 bits) */
448 srdi r6,r6,6 /* doubleword number */
449 sldi r6,r6,3 /* address offset */
450 add r6,r6,r9
451 addi r6,r6,KVM_NEED_FLUSH /* dword in kvm->arch.need_tlb_flush */
371fefd6 452 li r0,1
1b400ba0
PM
453 sld r0,r0,r7
454 ld r7,0(r6)
455 and. r7,r7,r0
456 beq 22f
45723: ldarx r7,0,r6 /* if set, clear the bit */
458 andc r7,r7,r0
459 stdcx. r7,0,r6
460 bne 23b
461 li r6,128 /* and flush the TLB */
462 mtctr r6
463 li r7,0x800 /* IS field = 0b10 */
464 ptesync
46528: tlbiel r7
466 addi r7,r7,0x1000
467 bdnz 28b
468 ptesync
469
93b0f4dc
PM
470 /* Add timebase offset onto timebase */
47122: ld r8,VCORE_TB_OFFSET(r5)
472 cmpdi r8,0
473 beq 37f
474 mftb r6 /* current host timebase */
475 add r8,r8,r6
476 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
477 mftb r7 /* check if lower 24 bits overflowed */
478 clrldi r6,r6,40
479 clrldi r7,r7,40
480 cmpld r7,r6
481 bge 37f
482 addis r8,r8,0x100 /* if so, increment upper 40 bits */
483 mtspr SPRN_TBU40,r8
484
388cc6e1
PM
485 /* Load guest PCR value to select appropriate compat mode */
48637: ld r7, VCORE_PCR(r5)
487 cmpdi r7, 0
488 beq 38f
489 mtspr SPRN_PCR, r7
49038:
491 li r0,1
371fefd6
PM
492 stb r0,VCORE_IN_GUEST(r5) /* signal secondaries to continue */
493 b 10f
494
495 /* Secondary threads wait for primary to have done partition switch */
49620: lbz r0,VCORE_IN_GUEST(r5)
497 cmpwi r0,0
498 beq 20b
aa04b4cc 499
19ccb76a 500 /* Set LPCR and RMOR. */
a0144e2a 50110: ld r8,VCORE_LPCR(r5)
19ccb76a 502 mtspr SPRN_LPCR,r8
aa04b4cc
PM
503 ld r8,KVM_RMOR(r9)
504 mtspr SPRN_RMOR,r8
de56a948
PM
505 isync
506
8c2dbb79
PM
507 /* Increment yield count if they have a VPA */
508 ld r3, VCPU_VPA(r4)
509 cmpdi r3, 0
510 beq 25f
511 lwz r5, LPPACA_YIELDCOUNT(r3)
512 addi r5, r5, 1
513 stw r5, LPPACA_YIELDCOUNT(r3)
514 li r6, 1
515 stb r6, VCPU_VPA_DIRTY(r4)
51625:
de56a948
PM
517 /* Check if HDEC expires soon */
518 mfspr r3,SPRN_HDEC
519 cmpwi r3,10
520 li r12,BOOK3S_INTERRUPT_HV_DECREMENTER
521 mr r9,r4
522 blt hdec_soon
523
de56a948
PM
524 /* Save purr/spurr */
525 mfspr r5,SPRN_PURR
526 mfspr r6,SPRN_SPURR
527 std r5,HSTATE_PURR(r13)
528 std r6,HSTATE_SPURR(r13)
529 ld r7,VCPU_PURR(r4)
530 ld r8,VCPU_SPURR(r4)
531 mtspr SPRN_PURR,r7
532 mtspr SPRN_SPURR,r8
9e368f29
PM
533 b 31f
534
535 /*
536 * PPC970 host -> guest partition switch code.
537 * We have to lock against concurrent tlbies,
538 * using native_tlbie_lock to lock against host tlbies
539 * and kvm->arch.tlbie_lock to lock against guest tlbies.
540 * We also have to invalidate the TLB since its
541 * entries aren't tagged with the LPID.
542 */
54330: ld r9,VCPU_KVM(r4) /* pointer to struct kvm */
544
545 /* first take native_tlbie_lock */
546 .section ".toc","aw"
547toc_tlbie_lock:
548 .tc native_tlbie_lock[TC],native_tlbie_lock
549 .previous
550 ld r3,toc_tlbie_lock@toc(2)
54bb7f4b 551#ifdef __BIG_ENDIAN__
9e368f29 552 lwz r8,PACA_LOCK_TOKEN(r13)
54bb7f4b
AB
553#else
554 lwz r8,PACAPACAINDEX(r13)
555#endif
9e368f29
PM
55624: lwarx r0,0,r3
557 cmpwi r0,0
558 bne 24b
559 stwcx. r8,0,r3
560 bne 24b
561 isync
562
a0144e2a
PM
563 ld r5,HSTATE_KVM_VCORE(r13)
564 ld r7,VCORE_LPCR(r5) /* use vcore->lpcr to store HID4 */
9e368f29
PM
565 li r0,0x18f
566 rotldi r0,r0,HID4_LPID5_SH /* all lpid bits in HID4 = 1 */
567 or r0,r7,r0
568 ptesync
569 sync
570 mtspr SPRN_HID4,r0 /* switch to reserved LPID */
571 isync
572 li r0,0
573 stw r0,0(r3) /* drop native_tlbie_lock */
574
575 /* invalidate the whole TLB */
576 li r0,256
577 mtctr r0
578 li r6,0
57925: tlbiel r6
580 addi r6,r6,0x1000
581 bdnz 25b
582 ptesync
583
584 /* Take the guest's tlbie_lock */
585 addi r3,r9,KVM_TLBIE_LOCK
58624: lwarx r0,0,r3
587 cmpwi r0,0
588 bne 24b
589 stwcx. r8,0,r3
590 bne 24b
591 isync
592 ld r6,KVM_SDR1(r9)
593 mtspr SPRN_SDR1,r6 /* switch to partition page table */
594
595 /* Set up HID4 with the guest's LPID etc. */
596 sync
597 mtspr SPRN_HID4,r7
598 isync
599
600 /* drop the guest's tlbie_lock */
601 li r0,0
602 stw r0,0(r3)
603
604 /* Check if HDEC expires soon */
605 mfspr r3,SPRN_HDEC
606 cmpwi r3,10
607 li r12,BOOK3S_INTERRUPT_HV_DECREMENTER
608 mr r9,r4
609 blt hdec_soon
610
611 /* Enable HDEC interrupts */
612 mfspr r0,SPRN_HID0
613 li r3,1
614 rldimi r0,r3, HID0_HDICE_SH, 64-HID0_HDICE_SH-1
615 sync
616 mtspr SPRN_HID0,r0
617 mfspr r0,SPRN_HID0
618 mfspr r0,SPRN_HID0
619 mfspr r0,SPRN_HID0
620 mfspr r0,SPRN_HID0
621 mfspr r0,SPRN_HID0
622 mfspr r0,SPRN_HID0
de56a948
PM
623
624 /* Load up guest SLB entries */
9e368f29 62531: lwz r5,VCPU_SLB_MAX(r4)
de56a948
PM
626 cmpwi r5,0
627 beq 9f
628 mtctr r5
629 addi r6,r4,VCPU_SLB
6301: ld r8,VCPU_SLB_E(r6)
631 ld r9,VCPU_SLB_V(r6)
632 slbmte r9,r8
633 addi r6,r6,VCPU_SLB_SIZE
634 bdnz 1b
6359:
636
637 /* Restore state of CTRL run bit; assume 1 on entry */
638 lwz r5,VCPU_CTRL(r4)
639 andi. r5,r5,1
640 bne 4f
641 mfspr r6,SPRN_CTRLF
642 clrrdi r6,r6,1
643 mtspr SPRN_CTRLT,r6
6444:
645 ld r6, VCPU_CTR(r4)
646 lwz r7, VCPU_XER(r4)
647
648 mtctr r6
649 mtxer r7
650
4619ac88
PM
651 ld r10, VCPU_PC(r4)
652 ld r11, VCPU_MSR(r4)
19ccb76a 653kvmppc_cede_reentry: /* r4 = vcpu, r13 = paca */
de56a948
PM
654 ld r6, VCPU_SRR0(r4)
655 ld r7, VCPU_SRR1(r4)
de56a948 656
4619ac88 657 /* r11 = vcpu->arch.msr & ~MSR_HV */
de56a948
PM
658 rldicl r11, r11, 63 - MSR_HV_LG, 1
659 rotldi r11, r11, 1 + MSR_HV_LG
660 ori r11, r11, MSR_ME
661
19ccb76a
PM
662 /* Check if we can deliver an external or decrementer interrupt now */
663 ld r0,VCPU_PENDING_EXC(r4)
4619ac88 664 lis r8,(1 << BOOK3S_IRQPRIO_EXTERNAL_LEVEL)@h
19ccb76a
PM
665 and r0,r0,r8
666 cmpdi cr1,r0,0
667 andi. r0,r11,MSR_EE
668 beq cr1,11f
669BEGIN_FTR_SECTION
670 mfspr r8,SPRN_LPCR
671 ori r8,r8,LPCR_MER
672 mtspr SPRN_LPCR,r8
673 isync
674END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
675 beq 5f
676 li r0,BOOK3S_INTERRUPT_EXTERNAL
67712: mr r6,r10
678 mr r10,r0
679 mr r7,r11
680 li r11,(MSR_ME << 1) | 1 /* synthesize MSR_SF | MSR_ME */
681 rotldi r11,r11,63
682 b 5f
68311: beq 5f
684 mfspr r0,SPRN_DEC
685 cmpwi r0,0
686 li r0,BOOK3S_INTERRUPT_DECREMENTER
687 blt 12b
688
689 /* Move SRR0 and SRR1 into the respective regs */
6905: mtspr SPRN_SRR0, r6
691 mtspr SPRN_SRR1, r7
19ccb76a 692
27025a60
LPF
693/*
694 * Required state:
695 * R4 = vcpu
696 * R10: value for HSRR0
697 * R11: value for HSRR1
698 * R13 = PACA
699 */
de56a948 700fast_guest_return:
4619ac88
PM
701 li r0,0
702 stb r0,VCPU_CEDED(r4) /* cancel cede */
de56a948
PM
703 mtspr SPRN_HSRR0,r10
704 mtspr SPRN_HSRR1,r11
705
706 /* Activate guest mode, so faults get handled by KVM */
44a3add8 707 li r9, KVM_GUEST_MODE_GUEST_HV
de56a948
PM
708 stb r9, HSTATE_IN_GUEST(r13)
709
710 /* Enter guest */
711
0acb9111
PM
712BEGIN_FTR_SECTION
713 ld r5, VCPU_CFAR(r4)
714 mtspr SPRN_CFAR, r5
715END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
4b8473c9
PM
716BEGIN_FTR_SECTION
717 ld r0, VCPU_PPR(r4)
718END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
0acb9111 719
de56a948
PM
720 ld r5, VCPU_LR(r4)
721 lwz r6, VCPU_CR(r4)
722 mtlr r5
723 mtcr r6
724
c75df6f9
MN
725 ld r1, VCPU_GPR(R1)(r4)
726 ld r2, VCPU_GPR(R2)(r4)
727 ld r3, VCPU_GPR(R3)(r4)
728 ld r5, VCPU_GPR(R5)(r4)
729 ld r6, VCPU_GPR(R6)(r4)
730 ld r7, VCPU_GPR(R7)(r4)
731 ld r8, VCPU_GPR(R8)(r4)
732 ld r9, VCPU_GPR(R9)(r4)
733 ld r10, VCPU_GPR(R10)(r4)
734 ld r11, VCPU_GPR(R11)(r4)
735 ld r12, VCPU_GPR(R12)(r4)
736 ld r13, VCPU_GPR(R13)(r4)
737
4b8473c9
PM
738BEGIN_FTR_SECTION
739 mtspr SPRN_PPR, r0
740END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
741 ld r0, VCPU_GPR(R0)(r4)
c75df6f9 742 ld r4, VCPU_GPR(R4)(r4)
de56a948
PM
743
744 hrfid
745 b .
746
747/******************************************************************************
748 * *
749 * Exit code *
750 * *
751 *****************************************************************************/
752
753/*
754 * We come here from the first-level interrupt handlers.
755 */
dd96b2c2
AK
756 .globl kvmppc_interrupt_hv
757kvmppc_interrupt_hv:
de56a948
PM
758 /*
759 * Register contents:
760 * R12 = interrupt vector
761 * R13 = PACA
762 * guest CR, R12 saved in shadow VCPU SCRATCH1/0
763 * guest R13 saved in SPRN_SCRATCH0
764 */
765 /* abuse host_r2 as third scratch area; we get r2 from PACATOC(r13) */
766 std r9, HSTATE_HOST_R2(r13)
44a3add8
PM
767
768 lbz r9, HSTATE_IN_GUEST(r13)
769 cmpwi r9, KVM_GUEST_MODE_HOST_HV
770 beq kvmppc_bad_host_intr
dd96b2c2
AK
771#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
772 cmpwi r9, KVM_GUEST_MODE_GUEST
773 ld r9, HSTATE_HOST_R2(r13)
774 beq kvmppc_interrupt_pr
775#endif
44a3add8
PM
776 /* We're now back in the host but in guest MMU context */
777 li r9, KVM_GUEST_MODE_HOST_HV
778 stb r9, HSTATE_IN_GUEST(r13)
779
de56a948
PM
780 ld r9, HSTATE_KVM_VCPU(r13)
781
782 /* Save registers */
783
c75df6f9
MN
784 std r0, VCPU_GPR(R0)(r9)
785 std r1, VCPU_GPR(R1)(r9)
786 std r2, VCPU_GPR(R2)(r9)
787 std r3, VCPU_GPR(R3)(r9)
788 std r4, VCPU_GPR(R4)(r9)
789 std r5, VCPU_GPR(R5)(r9)
790 std r6, VCPU_GPR(R6)(r9)
791 std r7, VCPU_GPR(R7)(r9)
792 std r8, VCPU_GPR(R8)(r9)
de56a948 793 ld r0, HSTATE_HOST_R2(r13)
c75df6f9
MN
794 std r0, VCPU_GPR(R9)(r9)
795 std r10, VCPU_GPR(R10)(r9)
796 std r11, VCPU_GPR(R11)(r9)
de56a948
PM
797 ld r3, HSTATE_SCRATCH0(r13)
798 lwz r4, HSTATE_SCRATCH1(r13)
c75df6f9 799 std r3, VCPU_GPR(R12)(r9)
de56a948 800 stw r4, VCPU_CR(r9)
0acb9111
PM
801BEGIN_FTR_SECTION
802 ld r3, HSTATE_CFAR(r13)
803 std r3, VCPU_CFAR(r9)
804END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
4b8473c9
PM
805BEGIN_FTR_SECTION
806 ld r4, HSTATE_PPR(r13)
807 std r4, VCPU_PPR(r9)
808END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
de56a948
PM
809
810 /* Restore R1/R2 so we can handle faults */
811 ld r1, HSTATE_HOST_R1(r13)
812 ld r2, PACATOC(r13)
813
814 mfspr r10, SPRN_SRR0
815 mfspr r11, SPRN_SRR1
816 std r10, VCPU_SRR0(r9)
817 std r11, VCPU_SRR1(r9)
818 andi. r0, r12, 2 /* need to read HSRR0/1? */
819 beq 1f
820 mfspr r10, SPRN_HSRR0
821 mfspr r11, SPRN_HSRR1
822 clrrdi r12, r12, 2
8231: std r10, VCPU_PC(r9)
824 std r11, VCPU_MSR(r9)
825
826 GET_SCRATCH0(r3)
827 mflr r4
c75df6f9 828 std r3, VCPU_GPR(R13)(r9)
de56a948
PM
829 std r4, VCPU_LR(r9)
830
de56a948
PM
831 stw r12,VCPU_TRAP(r9)
832
697d3899
PM
833 /* Save HEIR (HV emulation assist reg) in last_inst
834 if this is an HEI (HV emulation interrupt, e40) */
835 li r3,KVM_INST_FETCH_FAILED
836BEGIN_FTR_SECTION
837 cmpwi r12,BOOK3S_INTERRUPT_H_EMUL_ASSIST
838 bne 11f
839 mfspr r3,SPRN_HEIR
840END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
84111: stw r3,VCPU_LAST_INST(r9)
842
843 /* these are volatile across C function calls */
844 mfctr r3
845 mfxer r4
846 std r3, VCPU_CTR(r9)
847 stw r4, VCPU_XER(r9)
848
849BEGIN_FTR_SECTION
850 /* If this is a page table miss then see if it's theirs or ours */
851 cmpwi r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
852 beq kvmppc_hdsi
342d3db7
PM
853 cmpwi r12, BOOK3S_INTERRUPT_H_INST_STORAGE
854 beq kvmppc_hisi
697d3899
PM
855END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
856
de56a948
PM
857 /* See if this is a leftover HDEC interrupt */
858 cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER
859 bne 2f
860 mfspr r3,SPRN_HDEC
861 cmpwi r3,0
862 bge ignore_hdec
8632:
697d3899 864 /* See if this is an hcall we can handle in real mode */
a8606e20
PM
865 cmpwi r12,BOOK3S_INTERRUPT_SYSCALL
866 beq hcall_try_real_mode
de56a948 867
54695c30 868 /* Only handle external interrupts here on arch 206 and later */
9e368f29 869BEGIN_FTR_SECTION
54695c30
BH
870 b ext_interrupt_to_host
871END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_206)
872
873 /* External interrupt ? */
874 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
875 bne+ ext_interrupt_to_host
876
877 /* External interrupt, first check for host_ipi. If this is
878 * set, we know the host wants us out so let's do it now
879 */
4619ac88 880do_ext_interrupt:
c934243c
PM
881 bl kvmppc_read_intr
882 cmpdi r3, 0
883 bgt ext_interrupt_to_host
54695c30
BH
884
885 /* Allright, looks like an IPI for the guest, we need to set MER */
4619ac88
PM
886 /* Check if any CPU is heading out to the host, if so head out too */
887 ld r5, HSTATE_KVM_VCORE(r13)
888 lwz r0, VCORE_ENTRY_EXIT(r5)
889 cmpwi r0, 0x100
890 bge ext_interrupt_to_host
891
892 /* See if there is a pending interrupt for the guest */
893 mfspr r8, SPRN_LPCR
894 ld r0, VCPU_PENDING_EXC(r9)
895 /* Insert EXTERNAL_LEVEL bit into LPCR at the MER bit position */
896 rldicl. r0, r0, 64 - BOOK3S_IRQPRIO_EXTERNAL_LEVEL, 63
897 rldimi r8, r0, LPCR_MER_SH, 63 - LPCR_MER_SH
898 beq 2f
54695c30
BH
899
900 /* And if the guest EE is set, we can deliver immediately, else
901 * we return to the guest with MER set
902 */
903 andi. r0, r11, MSR_EE
4619ac88
PM
904 beq 2f
905 mtspr SPRN_SRR0, r10
906 mtspr SPRN_SRR1, r11
907 li r10, BOOK3S_INTERRUPT_EXTERNAL
908 li r11, (MSR_ME << 1) | 1 /* synthesize MSR_SF | MSR_ME */
909 rotldi r11, r11, 63
9102: mr r4, r9
911 mtspr SPRN_LPCR, r8
54695c30
BH
912 b fast_guest_return
913
54695c30 914ext_interrupt_to_host:
de56a948 915
b4072df4 916guest_exit_cont: /* r9 = vcpu, r12 = trap, r13 = paca */
de56a948 917 /* Save more register state */
de56a948
PM
918 mfdar r6
919 mfdsisr r7
de56a948
PM
920 std r6, VCPU_DAR(r9)
921 stw r7, VCPU_DSISR(r9)
9e368f29 922BEGIN_FTR_SECTION
697d3899 923 /* don't overwrite fault_dar/fault_dsisr if HDSI */
de56a948
PM
924 cmpwi r12,BOOK3S_INTERRUPT_H_DATA_STORAGE
925 beq 6f
9e368f29 926END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
697d3899 927 std r6, VCPU_FAULT_DAR(r9)
de56a948
PM
928 stw r7, VCPU_FAULT_DSISR(r9)
929
b4072df4
PM
930 /* See if it is a machine check */
931 cmpwi r12, BOOK3S_INTERRUPT_MACHINE_CHECK
932 beq machine_check_realmode
933mc_cont:
934
de56a948 935 /* Save guest CTRL register, set runlatch to 1 */
697d3899 9366: mfspr r6,SPRN_CTRLF
de56a948
PM
937 stw r6,VCPU_CTRL(r9)
938 andi. r0,r6,1
939 bne 4f
940 ori r6,r6,1
941 mtspr SPRN_CTRLT,r6
9424:
943 /* Read the guest SLB and save it away */
944 lwz r0,VCPU_SLB_NR(r9) /* number of entries in SLB */
945 mtctr r0
946 li r6,0
947 addi r7,r9,VCPU_SLB
948 li r5,0
9491: slbmfee r8,r6
950 andis. r0,r8,SLB_ESID_V@h
951 beq 2f
952 add r8,r8,r6 /* put index in */
953 slbmfev r3,r6
954 std r8,VCPU_SLB_E(r7)
955 std r3,VCPU_SLB_V(r7)
956 addi r7,r7,VCPU_SLB_SIZE
957 addi r5,r5,1
9582: addi r6,r6,1
959 bdnz 1b
960 stw r5,VCPU_SLB_MAX(r9)
961
962 /*
963 * Save the guest PURR/SPURR
964 */
9e368f29 965BEGIN_FTR_SECTION
de56a948
PM
966 mfspr r5,SPRN_PURR
967 mfspr r6,SPRN_SPURR
968 ld r7,VCPU_PURR(r9)
969 ld r8,VCPU_SPURR(r9)
970 std r5,VCPU_PURR(r9)
971 std r6,VCPU_SPURR(r9)
972 subf r5,r7,r5
973 subf r6,r8,r6
974
975 /*
976 * Restore host PURR/SPURR and add guest times
977 * so that the time in the guest gets accounted.
978 */
979 ld r3,HSTATE_PURR(r13)
980 ld r4,HSTATE_SPURR(r13)
981 add r3,r3,r5
982 add r4,r4,r6
983 mtspr SPRN_PURR,r3
984 mtspr SPRN_SPURR,r4
9e368f29 985END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_201)
de56a948
PM
986
987 /* Clear out SLB */
988 li r5,0
989 slbmte r5,r5
990 slbia
991 ptesync
992
19ccb76a 993hdec_soon: /* r9 = vcpu, r12 = trap, r13 = paca */
9e368f29
PM
994BEGIN_FTR_SECTION
995 b 32f
996END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
997 /*
998 * POWER7 guest -> host partition switch code.
999 * We don't have to lock against tlbies but we do
1000 * have to coordinate the hardware threads.
1001 */
371fefd6
PM
1002 /* Increment the threads-exiting-guest count in the 0xff00
1003 bits of vcore->entry_exit_count */
1004 lwsync
1005 ld r5,HSTATE_KVM_VCORE(r13)
1006 addi r6,r5,VCORE_ENTRY_EXIT
100741: lwarx r3,0,r6
1008 addi r0,r3,0x100
1009 stwcx. r0,0,r6
1010 bne 41b
19ccb76a 1011 lwsync
371fefd6
PM
1012
1013 /*
1014 * At this point we have an interrupt that we have to pass
1015 * up to the kernel or qemu; we can't handle it in real mode.
1016 * Thus we have to do a partition switch, so we have to
1017 * collect the other threads, if we are the first thread
1018 * to take an interrupt. To do this, we set the HDEC to 0,
1019 * which causes an HDEC interrupt in all threads within 2ns
1020 * because the HDEC register is shared between all 4 threads.
1021 * However, we don't need to bother if this is an HDEC
1022 * interrupt, since the other threads will already be on their
1023 * way here in that case.
1024 */
19ccb76a
PM
1025 cmpwi r3,0x100 /* Are we the first here? */
1026 bge 43f
1027 cmpwi r3,1 /* Are any other threads in the guest? */
1028 ble 43f
371fefd6
PM
1029 cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER
1030 beq 40f
371fefd6
PM
1031 li r0,0
1032 mtspr SPRN_HDEC,r0
103340:
19ccb76a
PM
1034 /*
1035 * Send an IPI to any napping threads, since an HDEC interrupt
1036 * doesn't wake CPUs up from nap.
1037 */
1038 lwz r3,VCORE_NAPPING_THREADS(r5)
1039 lwz r4,VCPU_PTID(r9)
1040 li r0,1
2f584a14 1041 sld r0,r0,r4
19ccb76a
PM
1042 andc. r3,r3,r0 /* no sense IPI'ing ourselves */
1043 beq 43f
1044 mulli r4,r4,PACA_SIZE /* get paca for thread 0 */
1045 subf r6,r4,r13
104642: andi. r0,r3,1
1047 beq 44f
1048 ld r8,HSTATE_XICS_PHYS(r6) /* get thread's XICS reg addr */
1049 li r0,IPI_PRIORITY
54695c30 1050 li r7,XICS_MFRR
19ccb76a
PM
1051 stbcix r0,r7,r8 /* trigger the IPI */
105244: srdi. r3,r3,1
1053 addi r6,r6,PACA_SIZE
1054 bne 42b
371fefd6
PM
1055
1056 /* Secondary threads wait for primary to do partition switch */
19ccb76a 105743: ld r4,VCPU_KVM(r9) /* pointer to struct kvm */
371fefd6
PM
1058 ld r5,HSTATE_KVM_VCORE(r13)
1059 lwz r3,VCPU_PTID(r9)
1060 cmpwi r3,0
1061 beq 15f
1062 HMT_LOW
106313: lbz r3,VCORE_IN_GUEST(r5)
1064 cmpwi r3,0
1065 bne 13b
1066 HMT_MEDIUM
1067 b 16f
1068
1069 /* Primary thread waits for all the secondaries to exit guest */
107015: lwz r3,VCORE_ENTRY_EXIT(r5)
1071 srwi r0,r3,8
1072 clrldi r3,r3,56
1073 cmpw r3,r0
1074 bne 15b
1075 isync
1076
1077 /* Primary thread switches back to host partition */
de56a948
PM
1078 ld r6,KVM_HOST_SDR1(r4)
1079 lwz r7,KVM_HOST_LPID(r4)
1080 li r8,LPID_RSVD /* switch to reserved LPID */
1081 mtspr SPRN_LPID,r8
1082 ptesync
1083 mtspr SPRN_SDR1,r6 /* switch to partition page table */
1084 mtspr SPRN_LPID,r7
1085 isync
93b0f4dc
PM
1086
1087 /* Subtract timebase offset from timebase */
1088 ld r8,VCORE_TB_OFFSET(r5)
1089 cmpdi r8,0
1090 beq 17f
1091 mftb r6 /* current host timebase */
1092 subf r8,r8,r6
1093 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
1094 mftb r7 /* check if lower 24 bits overflowed */
1095 clrldi r6,r6,40
1096 clrldi r7,r7,40
1097 cmpld r7,r6
1098 bge 17f
1099 addis r8,r8,0x100 /* if so, increment upper 40 bits */
1100 mtspr SPRN_TBU40,r8
1101
388cc6e1
PM
1102 /* Reset PCR */
110317: ld r0, VCORE_PCR(r5)
1104 cmpdi r0, 0
1105 beq 18f
1106 li r0, 0
1107 mtspr SPRN_PCR, r0
110818:
93b0f4dc 1109 /* Signal secondary CPUs to continue */
371fefd6 1110 stb r0,VCORE_IN_GUEST(r5)
de56a948
PM
1111 lis r8,0x7fff /* MAX_INT@h */
1112 mtspr SPRN_HDEC,r8
1113
371fefd6 111416: ld r8,KVM_HOST_LPCR(r4)
de56a948
PM
1115 mtspr SPRN_LPCR,r8
1116 isync
9e368f29
PM
1117 b 33f
1118
1119 /*
1120 * PPC970 guest -> host partition switch code.
1121 * We have to lock against concurrent tlbies, and
1122 * we have to flush the whole TLB.
1123 */
112432: ld r4,VCPU_KVM(r9) /* pointer to struct kvm */
1125
1126 /* Take the guest's tlbie_lock */
54bb7f4b 1127#ifdef __BIG_ENDIAN__
9e368f29 1128 lwz r8,PACA_LOCK_TOKEN(r13)
54bb7f4b
AB
1129#else
1130 lwz r8,PACAPACAINDEX(r13)
1131#endif
9e368f29
PM
1132 addi r3,r4,KVM_TLBIE_LOCK
113324: lwarx r0,0,r3
1134 cmpwi r0,0
1135 bne 24b
1136 stwcx. r8,0,r3
1137 bne 24b
1138 isync
1139
1140 ld r7,KVM_HOST_LPCR(r4) /* use kvm->arch.host_lpcr for HID4 */
1141 li r0,0x18f
1142 rotldi r0,r0,HID4_LPID5_SH /* all lpid bits in HID4 = 1 */
1143 or r0,r7,r0
1144 ptesync
1145 sync
1146 mtspr SPRN_HID4,r0 /* switch to reserved LPID */
1147 isync
1148 li r0,0
1149 stw r0,0(r3) /* drop guest tlbie_lock */
1150
1151 /* invalidate the whole TLB */
1152 li r0,256
1153 mtctr r0
1154 li r6,0
115525: tlbiel r6
1156 addi r6,r6,0x1000
1157 bdnz 25b
1158 ptesync
1159
1160 /* take native_tlbie_lock */
1161 ld r3,toc_tlbie_lock@toc(2)
116224: lwarx r0,0,r3
1163 cmpwi r0,0
1164 bne 24b
1165 stwcx. r8,0,r3
1166 bne 24b
1167 isync
1168
1169 ld r6,KVM_HOST_SDR1(r4)
1170 mtspr SPRN_SDR1,r6 /* switch to host page table */
1171
1172 /* Set up host HID4 value */
1173 sync
1174 mtspr SPRN_HID4,r7
1175 isync
1176 li r0,0
1177 stw r0,0(r3) /* drop native_tlbie_lock */
1178
1179 lis r8,0x7fff /* MAX_INT@h */
1180 mtspr SPRN_HDEC,r8
1181
1182 /* Disable HDEC interrupts */
1183 mfspr r0,SPRN_HID0
1184 li r3,0
1185 rldimi r0,r3, HID0_HDICE_SH, 64-HID0_HDICE_SH-1
1186 sync
1187 mtspr SPRN_HID0,r0
1188 mfspr r0,SPRN_HID0
1189 mfspr r0,SPRN_HID0
1190 mfspr r0,SPRN_HID0
1191 mfspr r0,SPRN_HID0
1192 mfspr r0,SPRN_HID0
1193 mfspr r0,SPRN_HID0
de56a948
PM
1194
1195 /* load host SLB entries */
9e368f29 119633: ld r8,PACA_SLBSHADOWPTR(r13)
de56a948
PM
1197
1198 .rept SLB_NUM_BOLTED
1199 ld r5,SLBSHADOW_SAVEAREA(r8)
1200 ld r6,SLBSHADOW_SAVEAREA+8(r8)
1201 andis. r7,r5,SLB_ESID_V@h
1202 beq 1f
1203 slbmte r6,r5
12041: addi r8,r8,16
1205 .endr
1206
93b0f4dc
PM
1207 /* Save DEC */
1208 mfspr r5,SPRN_DEC
1209 mftb r6
1210 extsw r5,r5
1211 add r5,r5,r6
1212 std r5,VCPU_DEC_EXPIRES(r9)
1213
de56a948 1214 /* Save and reset AMR and UAMOR before turning on the MMU */
9e368f29 1215BEGIN_FTR_SECTION
de56a948
PM
1216 mfspr r5,SPRN_AMR
1217 mfspr r6,SPRN_UAMOR
1218 std r5,VCPU_AMR(r9)
1219 std r6,VCPU_UAMOR(r9)
1220 li r6,0
1221 mtspr SPRN_AMR,r6
9e368f29 1222END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
de56a948 1223
44a3add8
PM
1224 /* Unset guest mode */
1225 li r0, KVM_GUEST_MODE_NONE
1226 stb r0, HSTATE_IN_GUEST(r13)
1227
de56a948 1228 /* Switch DSCR back to host value */
9e368f29 1229BEGIN_FTR_SECTION
de56a948
PM
1230 mfspr r8, SPRN_DSCR
1231 ld r7, HSTATE_DSCR(r13)
cfc86025 1232 std r8, VCPU_DSCR(r9)
de56a948 1233 mtspr SPRN_DSCR, r7
9e368f29 1234END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
de56a948
PM
1235
1236 /* Save non-volatile GPRs */
c75df6f9
MN
1237 std r14, VCPU_GPR(R14)(r9)
1238 std r15, VCPU_GPR(R15)(r9)
1239 std r16, VCPU_GPR(R16)(r9)
1240 std r17, VCPU_GPR(R17)(r9)
1241 std r18, VCPU_GPR(R18)(r9)
1242 std r19, VCPU_GPR(R19)(r9)
1243 std r20, VCPU_GPR(R20)(r9)
1244 std r21, VCPU_GPR(R21)(r9)
1245 std r22, VCPU_GPR(R22)(r9)
1246 std r23, VCPU_GPR(R23)(r9)
1247 std r24, VCPU_GPR(R24)(r9)
1248 std r25, VCPU_GPR(R25)(r9)
1249 std r26, VCPU_GPR(R26)(r9)
1250 std r27, VCPU_GPR(R27)(r9)
1251 std r28, VCPU_GPR(R28)(r9)
1252 std r29, VCPU_GPR(R29)(r9)
1253 std r30, VCPU_GPR(R30)(r9)
1254 std r31, VCPU_GPR(R31)(r9)
de56a948
PM
1255
1256 /* Save SPRGs */
1257 mfspr r3, SPRN_SPRG0
1258 mfspr r4, SPRN_SPRG1
1259 mfspr r5, SPRN_SPRG2
1260 mfspr r6, SPRN_SPRG3
1261 std r3, VCPU_SPRG0(r9)
1262 std r4, VCPU_SPRG1(r9)
1263 std r5, VCPU_SPRG2(r9)
1264 std r6, VCPU_SPRG3(r9)
1265
8943633c
PM
1266 /* save FP state */
1267 mr r3, r9
595e4f7e 1268 bl kvmppc_save_fp
8943633c 1269
a8606e20
PM
1270 /* Increment yield count if they have a VPA */
1271 ld r8, VCPU_VPA(r9) /* do they have a VPA? */
1272 cmpdi r8, 0
1273 beq 25f
1274 lwz r3, LPPACA_YIELDCOUNT(r8)
1275 addi r3, r3, 1
1276 stw r3, LPPACA_YIELDCOUNT(r8)
c35635ef
PM
1277 li r3, 1
1278 stb r3, VCPU_VPA_DIRTY(r9)
a8606e20
PM
127925:
1280 /* Save PMU registers if requested */
1281 /* r8 and cr0.eq are live here */
de56a948
PM
1282 li r3, 1
1283 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
1284 mfspr r4, SPRN_MMCR0 /* save MMCR0 */
1285 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
8943633c
PM
1286 mfspr r6, SPRN_MMCRA
1287BEGIN_FTR_SECTION
1288 /* On P7, clear MMCRA in order to disable SDAR updates */
1289 li r7, 0
1290 mtspr SPRN_MMCRA, r7
1291END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
de56a948 1292 isync
a8606e20
PM
1293 beq 21f /* if no VPA, save PMU stuff anyway */
1294 lbz r7, LPPACA_PMCINUSE(r8)
1295 cmpwi r7, 0 /* did they ask for PMU stuff to be saved? */
1296 bne 21f
1297 std r3, VCPU_MMCR(r9) /* if not, set saved MMCR0 to FC */
1298 b 22f
129921: mfspr r5, SPRN_MMCR1
14941789
PM
1300 mfspr r7, SPRN_SIAR
1301 mfspr r8, SPRN_SDAR
de56a948
PM
1302 std r4, VCPU_MMCR(r9)
1303 std r5, VCPU_MMCR + 8(r9)
1304 std r6, VCPU_MMCR + 16(r9)
14941789
PM
1305 std r7, VCPU_SIAR(r9)
1306 std r8, VCPU_SDAR(r9)
de56a948
PM
1307 mfspr r3, SPRN_PMC1
1308 mfspr r4, SPRN_PMC2
1309 mfspr r5, SPRN_PMC3
1310 mfspr r6, SPRN_PMC4
1311 mfspr r7, SPRN_PMC5
1312 mfspr r8, SPRN_PMC6
9e368f29
PM
1313BEGIN_FTR_SECTION
1314 mfspr r10, SPRN_PMC7
1315 mfspr r11, SPRN_PMC8
1316END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
de56a948
PM
1317 stw r3, VCPU_PMC(r9)
1318 stw r4, VCPU_PMC + 4(r9)
1319 stw r5, VCPU_PMC + 8(r9)
1320 stw r6, VCPU_PMC + 12(r9)
1321 stw r7, VCPU_PMC + 16(r9)
1322 stw r8, VCPU_PMC + 20(r9)
9e368f29
PM
1323BEGIN_FTR_SECTION
1324 stw r10, VCPU_PMC + 24(r9)
1325 stw r11, VCPU_PMC + 28(r9)
1326END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
de56a948 132722:
218309b7
PM
1328 ld r0, 112+PPC_LR_STKOFF(r1)
1329 addi r1, r1, 112
1330 mtlr r0
1331 blr
1332secondary_too_late:
1333 ld r5,HSTATE_KVM_VCORE(r13)
1334 HMT_LOW
133513: lbz r3,VCORE_IN_GUEST(r5)
1336 cmpwi r3,0
1337 bne 13b
1338 HMT_MEDIUM
1339 li r0, KVM_GUEST_MODE_NONE
1340 stb r0, HSTATE_IN_GUEST(r13)
1341 ld r11,PACA_SLBSHADOWPTR(r13)
de56a948 1342
218309b7
PM
1343 .rept SLB_NUM_BOLTED
1344 ld r5,SLBSHADOW_SAVEAREA(r11)
1345 ld r6,SLBSHADOW_SAVEAREA+8(r11)
1346 andis. r7,r5,SLB_ESID_V@h
1347 beq 1f
1348 slbmte r6,r5
13491: addi r11,r11,16
1350 .endr
1351 b 22b
b4072df4 1352
697d3899
PM
1353/*
1354 * Check whether an HDSI is an HPTE not found fault or something else.
1355 * If it is an HPTE not found fault that is due to the guest accessing
1356 * a page that they have mapped but which we have paged out, then
1357 * we continue on with the guest exit path. In all other cases,
1358 * reflect the HDSI to the guest as a DSI.
1359 */
1360kvmppc_hdsi:
1361 mfspr r4, SPRN_HDAR
1362 mfspr r6, SPRN_HDSISR
4cf302bc
PM
1363 /* HPTE not found fault or protection fault? */
1364 andis. r0, r6, (DSISR_NOHPTE | DSISR_PROTFAULT)@h
697d3899
PM
1365 beq 1f /* if not, send it to the guest */
1366 andi. r0, r11, MSR_DR /* data relocation enabled? */
1367 beq 3f
1368 clrrdi r0, r4, 28
c75df6f9 1369 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
697d3899
PM
1370 bne 1f /* if no SLB entry found */
13714: std r4, VCPU_FAULT_DAR(r9)
1372 stw r6, VCPU_FAULT_DSISR(r9)
1373
1374 /* Search the hash table. */
1375 mr r3, r9 /* vcpu pointer */
342d3db7 1376 li r7, 1 /* data fault */
697d3899
PM
1377 bl .kvmppc_hpte_hv_fault
1378 ld r9, HSTATE_KVM_VCPU(r13)
1379 ld r10, VCPU_PC(r9)
1380 ld r11, VCPU_MSR(r9)
1381 li r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1382 cmpdi r3, 0 /* retry the instruction */
1383 beq 6f
1384 cmpdi r3, -1 /* handle in kernel mode */
b4072df4 1385 beq guest_exit_cont
697d3899
PM
1386 cmpdi r3, -2 /* MMIO emulation; need instr word */
1387 beq 2f
1388
1389 /* Synthesize a DSI for the guest */
1390 ld r4, VCPU_FAULT_DAR(r9)
1391 mr r6, r3
13921: mtspr SPRN_DAR, r4
1393 mtspr SPRN_DSISR, r6
1394 mtspr SPRN_SRR0, r10
1395 mtspr SPRN_SRR1, r11
1396 li r10, BOOK3S_INTERRUPT_DATA_STORAGE
1397 li r11, (MSR_ME << 1) | 1 /* synthesize MSR_SF | MSR_ME */
1398 rotldi r11, r11, 63
b4072df4 1399fast_interrupt_c_return:
697d3899
PM
14006: ld r7, VCPU_CTR(r9)
1401 lwz r8, VCPU_XER(r9)
1402 mtctr r7
1403 mtxer r8
1404 mr r4, r9
1405 b fast_guest_return
1406
14073: ld r5, VCPU_KVM(r9) /* not relocated, use VRMA */
1408 ld r5, KVM_VRMA_SLB_V(r5)
1409 b 4b
1410
1411 /* If this is for emulated MMIO, load the instruction word */
14122: li r8, KVM_INST_FETCH_FAILED /* In case lwz faults */
1413
1414 /* Set guest mode to 'jump over instruction' so if lwz faults
1415 * we'll just continue at the next IP. */
1416 li r0, KVM_GUEST_MODE_SKIP
1417 stb r0, HSTATE_IN_GUEST(r13)
1418
1419 /* Do the access with MSR:DR enabled */
1420 mfmsr r3
1421 ori r4, r3, MSR_DR /* Enable paging for data */
1422 mtmsrd r4
1423 lwz r8, 0(r10)
1424 mtmsrd r3
1425
1426 /* Store the result */
1427 stw r8, VCPU_LAST_INST(r9)
1428
1429 /* Unset guest mode. */
44a3add8 1430 li r0, KVM_GUEST_MODE_HOST_HV
697d3899 1431 stb r0, HSTATE_IN_GUEST(r13)
b4072df4 1432 b guest_exit_cont
de56a948 1433
342d3db7
PM
1434/*
1435 * Similarly for an HISI, reflect it to the guest as an ISI unless
1436 * it is an HPTE not found fault for a page that we have paged out.
1437 */
1438kvmppc_hisi:
1439 andis. r0, r11, SRR1_ISI_NOPT@h
1440 beq 1f
1441 andi. r0, r11, MSR_IR /* instruction relocation enabled? */
1442 beq 3f
1443 clrrdi r0, r10, 28
c75df6f9 1444 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
342d3db7
PM
1445 bne 1f /* if no SLB entry found */
14464:
1447 /* Search the hash table. */
1448 mr r3, r9 /* vcpu pointer */
1449 mr r4, r10
1450 mr r6, r11
1451 li r7, 0 /* instruction fault */
1452 bl .kvmppc_hpte_hv_fault
1453 ld r9, HSTATE_KVM_VCPU(r13)
1454 ld r10, VCPU_PC(r9)
1455 ld r11, VCPU_MSR(r9)
1456 li r12, BOOK3S_INTERRUPT_H_INST_STORAGE
1457 cmpdi r3, 0 /* retry the instruction */
b4072df4 1458 beq fast_interrupt_c_return
342d3db7 1459 cmpdi r3, -1 /* handle in kernel mode */
b4072df4 1460 beq guest_exit_cont
342d3db7
PM
1461
1462 /* Synthesize an ISI for the guest */
1463 mr r11, r3
14641: mtspr SPRN_SRR0, r10
1465 mtspr SPRN_SRR1, r11
1466 li r10, BOOK3S_INTERRUPT_INST_STORAGE
1467 li r11, (MSR_ME << 1) | 1 /* synthesize MSR_SF | MSR_ME */
1468 rotldi r11, r11, 63
b4072df4 1469 b fast_interrupt_c_return
342d3db7
PM
1470
14713: ld r6, VCPU_KVM(r9) /* not relocated, use VRMA */
1472 ld r5, KVM_VRMA_SLB_V(r6)
1473 b 4b
1474
a8606e20
PM
1475/*
1476 * Try to handle an hcall in real mode.
1477 * Returns to the guest if we handle it, or continues on up to
1478 * the kernel if we can't (i.e. if we don't have a handler for
1479 * it, or if the handler returns H_TOO_HARD).
1480 */
1481 .globl hcall_try_real_mode
1482hcall_try_real_mode:
c75df6f9 1483 ld r3,VCPU_GPR(R3)(r9)
a8606e20 1484 andi. r0,r11,MSR_PR
27025a60
LPF
1485 /* sc 1 from userspace - reflect to guest syscall */
1486 bne sc_1_fast_return
a8606e20
PM
1487 clrrdi r3,r3,2
1488 cmpldi r3,hcall_real_table_end - hcall_real_table
b4072df4 1489 bge guest_exit_cont
a8606e20 1490 LOAD_REG_ADDR(r4, hcall_real_table)
4baa1d87 1491 lwax r3,r3,r4
a8606e20 1492 cmpwi r3,0
b4072df4 1493 beq guest_exit_cont
a8606e20
PM
1494 add r3,r3,r4
1495 mtctr r3
1496 mr r3,r9 /* get vcpu pointer */
c75df6f9 1497 ld r4,VCPU_GPR(R4)(r9)
a8606e20
PM
1498 bctrl
1499 cmpdi r3,H_TOO_HARD
1500 beq hcall_real_fallback
1501 ld r4,HSTATE_KVM_VCPU(r13)
c75df6f9 1502 std r3,VCPU_GPR(R3)(r4)
a8606e20
PM
1503 ld r10,VCPU_PC(r4)
1504 ld r11,VCPU_MSR(r4)
1505 b fast_guest_return
1506
27025a60
LPF
1507sc_1_fast_return:
1508 mtspr SPRN_SRR0,r10
1509 mtspr SPRN_SRR1,r11
1510 li r10, BOOK3S_INTERRUPT_SYSCALL
1511 li r11, (MSR_ME << 1) | 1 /* synthesize MSR_SF | MSR_ME */
1512 rotldi r11, r11, 63
1513 mr r4,r9
1514 b fast_guest_return
1515
a8606e20
PM
1516 /* We've attempted a real mode hcall, but it's punted it back
1517 * to userspace. We need to restore some clobbered volatiles
1518 * before resuming the pass-it-to-qemu path */
1519hcall_real_fallback:
1520 li r12,BOOK3S_INTERRUPT_SYSCALL
1521 ld r9, HSTATE_KVM_VCPU(r13)
a8606e20 1522
b4072df4 1523 b guest_exit_cont
a8606e20
PM
1524
1525 .globl hcall_real_table
1526hcall_real_table:
1527 .long 0 /* 0 - unused */
1528 .long .kvmppc_h_remove - hcall_real_table
1529 .long .kvmppc_h_enter - hcall_real_table
1530 .long .kvmppc_h_read - hcall_real_table
1531 .long 0 /* 0x10 - H_CLEAR_MOD */
1532 .long 0 /* 0x14 - H_CLEAR_REF */
1533 .long .kvmppc_h_protect - hcall_real_table
1534 .long 0 /* 0x1c - H_GET_TCE */
54738c09 1535 .long .kvmppc_h_put_tce - hcall_real_table
a8606e20
PM
1536 .long 0 /* 0x24 - H_SET_SPRG0 */
1537 .long .kvmppc_h_set_dabr - hcall_real_table
1538 .long 0 /* 0x2c */
1539 .long 0 /* 0x30 */
1540 .long 0 /* 0x34 */
1541 .long 0 /* 0x38 */
1542 .long 0 /* 0x3c */
1543 .long 0 /* 0x40 */
1544 .long 0 /* 0x44 */
1545 .long 0 /* 0x48 */
1546 .long 0 /* 0x4c */
1547 .long 0 /* 0x50 */
1548 .long 0 /* 0x54 */
1549 .long 0 /* 0x58 */
1550 .long 0 /* 0x5c */
1551 .long 0 /* 0x60 */
e7d26f28
BH
1552#ifdef CONFIG_KVM_XICS
1553 .long .kvmppc_rm_h_eoi - hcall_real_table
1554 .long .kvmppc_rm_h_cppr - hcall_real_table
1555 .long .kvmppc_rm_h_ipi - hcall_real_table
1556 .long 0 /* 0x70 - H_IPOLL */
1557 .long .kvmppc_rm_h_xirr - hcall_real_table
1558#else
1559 .long 0 /* 0x64 - H_EOI */
1560 .long 0 /* 0x68 - H_CPPR */
1561 .long 0 /* 0x6c - H_IPI */
1562 .long 0 /* 0x70 - H_IPOLL */
1563 .long 0 /* 0x74 - H_XIRR */
1564#endif
a8606e20
PM
1565 .long 0 /* 0x78 */
1566 .long 0 /* 0x7c */
1567 .long 0 /* 0x80 */
1568 .long 0 /* 0x84 */
1569 .long 0 /* 0x88 */
1570 .long 0 /* 0x8c */
1571 .long 0 /* 0x90 */
1572 .long 0 /* 0x94 */
1573 .long 0 /* 0x98 */
1574 .long 0 /* 0x9c */
1575 .long 0 /* 0xa0 */
1576 .long 0 /* 0xa4 */
1577 .long 0 /* 0xa8 */
1578 .long 0 /* 0xac */
1579 .long 0 /* 0xb0 */
1580 .long 0 /* 0xb4 */
1581 .long 0 /* 0xb8 */
1582 .long 0 /* 0xbc */
1583 .long 0 /* 0xc0 */
1584 .long 0 /* 0xc4 */
1585 .long 0 /* 0xc8 */
1586 .long 0 /* 0xcc */
1587 .long 0 /* 0xd0 */
1588 .long 0 /* 0xd4 */
1589 .long 0 /* 0xd8 */
1590 .long 0 /* 0xdc */
19ccb76a 1591 .long .kvmppc_h_cede - hcall_real_table
a8606e20
PM
1592 .long 0 /* 0xe4 */
1593 .long 0 /* 0xe8 */
1594 .long 0 /* 0xec */
1595 .long 0 /* 0xf0 */
1596 .long 0 /* 0xf4 */
1597 .long 0 /* 0xf8 */
1598 .long 0 /* 0xfc */
1599 .long 0 /* 0x100 */
1600 .long 0 /* 0x104 */
1601 .long 0 /* 0x108 */
1602 .long 0 /* 0x10c */
1603 .long 0 /* 0x110 */
1604 .long 0 /* 0x114 */
1605 .long 0 /* 0x118 */
1606 .long 0 /* 0x11c */
1607 .long 0 /* 0x120 */
1608 .long .kvmppc_h_bulk_remove - hcall_real_table
1609hcall_real_table_end:
1610
de56a948
PM
1611ignore_hdec:
1612 mr r4,r9
1613 b fast_guest_return
1614
a8606e20 1615_GLOBAL(kvmppc_h_set_dabr)
eee7ff9d
MN
1616BEGIN_FTR_SECTION
1617 b 2f
1618END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
a8606e20 1619 std r4,VCPU_DABR(r3)
8943633c
PM
1620 /* Work around P7 bug where DABR can get corrupted on mtspr */
16211: mtspr SPRN_DABR,r4
1622 mfspr r5, SPRN_DABR
1623 cmpd r4, r5
1624 bne 1b
1625 isync
eee7ff9d 16262: li r3,0
a8606e20
PM
1627 blr
1628
19ccb76a
PM
1629_GLOBAL(kvmppc_h_cede)
1630 ori r11,r11,MSR_EE
1631 std r11,VCPU_MSR(r3)
1632 li r0,1
1633 stb r0,VCPU_CEDED(r3)
1634 sync /* order setting ceded vs. testing prodded */
1635 lbz r5,VCPU_PRODDED(r3)
1636 cmpwi r5,0
04f995a5 1637 bne kvm_cede_prodded
19ccb76a
PM
1638 li r0,0 /* set trap to 0 to say hcall is handled */
1639 stw r0,VCPU_TRAP(r3)
1640 li r0,H_SUCCESS
c75df6f9 1641 std r0,VCPU_GPR(R3)(r3)
19ccb76a 1642BEGIN_FTR_SECTION
04f995a5 1643 b kvm_cede_exit /* just send it up to host on 970 */
19ccb76a
PM
1644END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_206)
1645
1646 /*
1647 * Set our bit in the bitmask of napping threads unless all the
1648 * other threads are already napping, in which case we send this
1649 * up to the host.
1650 */
1651 ld r5,HSTATE_KVM_VCORE(r13)
1652 lwz r6,VCPU_PTID(r3)
1653 lwz r8,VCORE_ENTRY_EXIT(r5)
1654 clrldi r8,r8,56
1655 li r0,1
1656 sld r0,r0,r6
1657 addi r6,r5,VCORE_NAPPING_THREADS
165831: lwarx r4,0,r6
1659 or r4,r4,r0
c75df6f9 1660 PPC_POPCNTW(R7,R4)
19ccb76a 1661 cmpw r7,r8
04f995a5 1662 bge kvm_cede_exit
19ccb76a
PM
1663 stwcx. r4,0,r6
1664 bne 31b
1665 li r0,1
1666 stb r0,HSTATE_NAPPING(r13)
1667 /* order napping_threads update vs testing entry_exit_count */
1668 lwsync
1669 mr r4,r3
1670 lwz r7,VCORE_ENTRY_EXIT(r5)
1671 cmpwi r7,0x100
1672 bge 33f /* another thread already exiting */
1673
1674/*
1675 * Although not specifically required by the architecture, POWER7
1676 * preserves the following registers in nap mode, even if an SMT mode
1677 * switch occurs: SLB entries, PURR, SPURR, AMOR, UAMOR, AMR, SPRG0-3,
1678 * DAR, DSISR, DABR, DABRX, DSCR, PMCx, MMCRx, SIAR, SDAR.
1679 */
1680 /* Save non-volatile GPRs */
c75df6f9
MN
1681 std r14, VCPU_GPR(R14)(r3)
1682 std r15, VCPU_GPR(R15)(r3)
1683 std r16, VCPU_GPR(R16)(r3)
1684 std r17, VCPU_GPR(R17)(r3)
1685 std r18, VCPU_GPR(R18)(r3)
1686 std r19, VCPU_GPR(R19)(r3)
1687 std r20, VCPU_GPR(R20)(r3)
1688 std r21, VCPU_GPR(R21)(r3)
1689 std r22, VCPU_GPR(R22)(r3)
1690 std r23, VCPU_GPR(R23)(r3)
1691 std r24, VCPU_GPR(R24)(r3)
1692 std r25, VCPU_GPR(R25)(r3)
1693 std r26, VCPU_GPR(R26)(r3)
1694 std r27, VCPU_GPR(R27)(r3)
1695 std r28, VCPU_GPR(R28)(r3)
1696 std r29, VCPU_GPR(R29)(r3)
1697 std r30, VCPU_GPR(R30)(r3)
1698 std r31, VCPU_GPR(R31)(r3)
19ccb76a
PM
1699
1700 /* save FP state */
595e4f7e 1701 bl kvmppc_save_fp
19ccb76a
PM
1702
1703 /*
1704 * Take a nap until a decrementer or external interrupt occurs,
1705 * with PECE1 (wake on decr) and PECE0 (wake on external) set in LPCR
1706 */
f0888f70
PM
1707 li r0,1
1708 stb r0,HSTATE_HWTHREAD_REQ(r13)
19ccb76a
PM
1709 mfspr r5,SPRN_LPCR
1710 ori r5,r5,LPCR_PECE0 | LPCR_PECE1
1711 mtspr SPRN_LPCR,r5
1712 isync
1713 li r0, 0
1714 std r0, HSTATE_SCRATCH0(r13)
1715 ptesync
1716 ld r0, HSTATE_SCRATCH0(r13)
17171: cmpd r0, r0
1718 bne 1b
1719 nap
1720 b .
1721
1722kvm_end_cede:
4619ac88
PM
1723 /* get vcpu pointer */
1724 ld r4, HSTATE_KVM_VCPU(r13)
1725
19ccb76a
PM
1726 /* Woken by external or decrementer interrupt */
1727 ld r1, HSTATE_HOST_R1(r13)
19ccb76a 1728
19ccb76a
PM
1729 /* load up FP state */
1730 bl kvmppc_load_fp
1731
1732 /* Load NV GPRS */
c75df6f9
MN
1733 ld r14, VCPU_GPR(R14)(r4)
1734 ld r15, VCPU_GPR(R15)(r4)
1735 ld r16, VCPU_GPR(R16)(r4)
1736 ld r17, VCPU_GPR(R17)(r4)
1737 ld r18, VCPU_GPR(R18)(r4)
1738 ld r19, VCPU_GPR(R19)(r4)
1739 ld r20, VCPU_GPR(R20)(r4)
1740 ld r21, VCPU_GPR(R21)(r4)
1741 ld r22, VCPU_GPR(R22)(r4)
1742 ld r23, VCPU_GPR(R23)(r4)
1743 ld r24, VCPU_GPR(R24)(r4)
1744 ld r25, VCPU_GPR(R25)(r4)
1745 ld r26, VCPU_GPR(R26)(r4)
1746 ld r27, VCPU_GPR(R27)(r4)
1747 ld r28, VCPU_GPR(R28)(r4)
1748 ld r29, VCPU_GPR(R29)(r4)
1749 ld r30, VCPU_GPR(R30)(r4)
1750 ld r31, VCPU_GPR(R31)(r4)
19ccb76a
PM
1751
1752 /* clear our bit in vcore->napping_threads */
175333: ld r5,HSTATE_KVM_VCORE(r13)
1754 lwz r3,VCPU_PTID(r4)
1755 li r0,1
1756 sld r0,r0,r3
1757 addi r6,r5,VCORE_NAPPING_THREADS
175832: lwarx r7,0,r6
1759 andc r7,r7,r0
1760 stwcx. r7,0,r6
1761 bne 32b
1762 li r0,0
1763 stb r0,HSTATE_NAPPING(r13)
1764
4619ac88
PM
1765 /* Check the wake reason in SRR1 to see why we got here */
1766 mfspr r3, SPRN_SRR1
1767 rlwinm r3, r3, 44-31, 0x7 /* extract wake reason field */
1768 cmpwi r3, 4 /* was it an external interrupt? */
1769 li r12, BOOK3S_INTERRUPT_EXTERNAL
1770 mr r9, r4
1771 ld r10, VCPU_PC(r9)
1772 ld r11, VCPU_MSR(r9)
1773 beq do_ext_interrupt /* if so */
1774
19ccb76a
PM
1775 /* see if any other thread is already exiting */
1776 lwz r0,VCORE_ENTRY_EXIT(r5)
1777 cmpwi r0,0x100
1778 blt kvmppc_cede_reentry /* if not go back to guest */
1779
1780 /* some threads are exiting, so go to the guest exit path */
1781 b hcall_real_fallback
1782
1783 /* cede when already previously prodded case */
04f995a5
PM
1784kvm_cede_prodded:
1785 li r0,0
19ccb76a
PM
1786 stb r0,VCPU_PRODDED(r3)
1787 sync /* order testing prodded vs. clearing ceded */
1788 stb r0,VCPU_CEDED(r3)
1789 li r3,H_SUCCESS
1790 blr
1791
1792 /* we've ceded but we want to give control to the host */
04f995a5 1793kvm_cede_exit:
4619ac88 1794 b hcall_real_fallback
19ccb76a 1795
b4072df4
PM
1796 /* Try to handle a machine check in real mode */
1797machine_check_realmode:
1798 mr r3, r9 /* get vcpu pointer */
1799 bl .kvmppc_realmode_machine_check
1800 nop
1801 cmpdi r3, 0 /* continue exiting from guest? */
1802 ld r9, HSTATE_KVM_VCPU(r13)
1803 li r12, BOOK3S_INTERRUPT_MACHINE_CHECK
1804 beq mc_cont
1805 /* If not, deliver a machine check. SRR0/1 are already set */
1806 li r10, BOOK3S_INTERRUPT_MACHINE_CHECK
1807 li r11, (MSR_ME << 1) | 1 /* synthesize MSR_SF | MSR_ME */
1808 rotldi r11, r11, 63
1809 b fast_interrupt_c_return
1810
c934243c
PM
1811/*
1812 * Determine what sort of external interrupt is pending (if any).
1813 * Returns:
1814 * 0 if no interrupt is pending
1815 * 1 if an interrupt is pending that needs to be handled by the host
1816 * -1 if there was a guest wakeup IPI (which has now been cleared)
1817 */
1818kvmppc_read_intr:
1819 /* see if a host IPI is pending */
1820 li r3, 1
1821 lbz r0, HSTATE_HOST_IPI(r13)
1822 cmpwi r0, 0
1823 bne 1f
371fefd6 1824
c934243c
PM
1825 /* Now read the interrupt from the ICP */
1826 ld r6, HSTATE_XICS_PHYS(r13)
19ccb76a 1827 li r7, XICS_XIRR
c934243c
PM
1828 cmpdi r6, 0
1829 beq- 1f
1830 lwzcix r0, r6, r7
1831 rlwinm. r3, r0, 0, 0xffffff
19ccb76a 1832 sync
c934243c 1833 beq 1f /* if nothing pending in the ICP */
371fefd6 1834
c934243c
PM
1835 /* We found something in the ICP...
1836 *
1837 * If it's not an IPI, stash it in the PACA and return to
1838 * the host, we don't (yet) handle directing real external
1839 * interrupts directly to the guest
1840 */
1841 cmpwi r3, XICS_IPI /* if there is, is it an IPI? */
1842 li r3, 1
1843 bne 42f
371fefd6 1844
c934243c
PM
1845 /* It's an IPI, clear the MFRR and EOI it */
1846 li r3, 0xff
1847 li r8, XICS_MFRR
1848 stbcix r3, r6, r8 /* clear the IPI */
1849 stwcix r0, r6, r7 /* EOI it */
1850 sync
f0888f70 1851
c934243c
PM
1852 /* We need to re-check host IPI now in case it got set in the
1853 * meantime. If it's clear, we bounce the interrupt to the
1854 * guest
1855 */
1856 lbz r0, HSTATE_HOST_IPI(r13)
1857 cmpwi r0, 0
1858 bne- 43f
1859
1860 /* OK, it's an IPI for us */
1861 li r3, -1
18621: blr
1863
186442: /* It's not an IPI and it's for the host, stash it in the PACA
1865 * before exit, it will be picked up by the host ICP driver
1866 */
1867 stw r0, HSTATE_SAVED_XIRR(r13)
1868 b 1b
1869
187043: /* We raced with the host, we need to resend that IPI, bummer */
1871 li r0, IPI_PRIORITY
1872 stbcix r0, r6, r8 /* set the IPI */
1873 sync
1874 b 1b
371fefd6 1875
de56a948
PM
1876/*
1877 * Save away FP, VMX and VSX registers.
1878 * r3 = vcpu pointer
595e4f7e
PM
1879 * N.B. r30 and r31 are volatile across this function,
1880 * thus it is not callable from C.
a8606e20 1881 */
595e4f7e
PM
1882kvmppc_save_fp:
1883 mflr r30
1884 mr r31,r3
8943633c
PM
1885 mfmsr r5
1886 ori r8,r5,MSR_FP
de56a948
PM
1887#ifdef CONFIG_ALTIVEC
1888BEGIN_FTR_SECTION
1889 oris r8,r8,MSR_VEC@h
1890END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
1891#endif
1892#ifdef CONFIG_VSX
1893BEGIN_FTR_SECTION
1894 oris r8,r8,MSR_VSX@h
1895END_FTR_SECTION_IFSET(CPU_FTR_VSX)
1896#endif
1897 mtmsrd r8
1898 isync
595e4f7e
PM
1899 addi r3,r3,VCPU_FPRS
1900 bl .store_fp_state
de56a948
PM
1901#ifdef CONFIG_ALTIVEC
1902BEGIN_FTR_SECTION
595e4f7e
PM
1903 addi r3,r31,VCPU_VRS
1904 bl .store_vr_state
de56a948
PM
1905END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
1906#endif
1907 mfspr r6,SPRN_VRSAVE
1908 stw r6,VCPU_VRSAVE(r3)
595e4f7e 1909 mtlr r30
8943633c 1910 mtmsrd r5
de56a948
PM
1911 isync
1912 blr
1913
1914/*
1915 * Load up FP, VMX and VSX registers
1916 * r4 = vcpu pointer
595e4f7e
PM
1917 * N.B. r30 and r31 are volatile across this function,
1918 * thus it is not callable from C.
de56a948 1919 */
de56a948 1920kvmppc_load_fp:
595e4f7e
PM
1921 mflr r30
1922 mr r31,r4
de56a948
PM
1923 mfmsr r9
1924 ori r8,r9,MSR_FP
1925#ifdef CONFIG_ALTIVEC
1926BEGIN_FTR_SECTION
1927 oris r8,r8,MSR_VEC@h
1928END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
1929#endif
1930#ifdef CONFIG_VSX
1931BEGIN_FTR_SECTION
1932 oris r8,r8,MSR_VSX@h
1933END_FTR_SECTION_IFSET(CPU_FTR_VSX)
1934#endif
1935 mtmsrd r8
1936 isync
595e4f7e
PM
1937 addi r3,r4,VCPU_FPRS
1938 bl .load_fp_state
de56a948
PM
1939#ifdef CONFIG_ALTIVEC
1940BEGIN_FTR_SECTION
595e4f7e
PM
1941 addi r3,r31,VCPU_VRS
1942 bl .load_vr_state
de56a948
PM
1943END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
1944#endif
1945 lwz r7,VCPU_VRSAVE(r4)
1946 mtspr SPRN_VRSAVE,r7
595e4f7e
PM
1947 mtlr r30
1948 mr r4,r31
de56a948 1949 blr
44a3add8
PM
1950
1951/*
1952 * We come here if we get any exception or interrupt while we are
1953 * executing host real mode code while in guest MMU context.
1954 * For now just spin, but we should do something better.
1955 */
1956kvmppc_bad_host_intr:
1957 b .
This page took 0.421616 seconds and 5 git commands to generate.