]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - arch/powerpc/kvm/book3s_hv_rmhandlers.S
KVM: PPC: Book3S HV: Restore critical SPRs to host values on guest exit
[mirror_ubuntu-bionic-kernel.git] / arch / powerpc / kvm / book3s_hv_rmhandlers.S
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>
23 #include <asm/mmu.h>
24 #include <asm/page.h>
25 #include <asm/ptrace.h>
26 #include <asm/hvcall.h>
27 #include <asm/asm-offsets.h>
28 #include <asm/exception-64s.h>
29 #include <asm/kvm_book3s_asm.h>
30 #include <asm/book3s/64/mmu-hash.h>
31 #include <asm/tm.h>
32 #include <asm/opal.h>
33 #include <asm/xive-regs.h>
34
35 /* Sign-extend HDEC if not on POWER9 */
36 #define EXTEND_HDEC(reg) \
37 BEGIN_FTR_SECTION; \
38 extsw reg, reg; \
39 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
40
41 #define VCPU_GPRS_TM(reg) (((reg) * ULONG_SIZE) + VCPU_GPR_TM)
42
43 /* Values in HSTATE_NAPPING(r13) */
44 #define NAPPING_CEDE 1
45 #define NAPPING_NOVCPU 2
46
47 /*
48 * Call kvmppc_hv_entry in real mode.
49 * Must be called with interrupts hard-disabled.
50 *
51 * Input Registers:
52 *
53 * LR = return address to continue at after eventually re-enabling MMU
54 */
55 _GLOBAL_TOC(kvmppc_hv_entry_trampoline)
56 mflr r0
57 std r0, PPC_LR_STKOFF(r1)
58 stdu r1, -112(r1)
59 mfmsr r10
60 LOAD_REG_ADDR(r5, kvmppc_call_hv_entry)
61 li r0,MSR_RI
62 andc r0,r10,r0
63 li r6,MSR_IR | MSR_DR
64 andc r6,r10,r6
65 mtmsrd r0,1 /* clear RI in MSR */
66 mtsrr0 r5
67 mtsrr1 r6
68 RFI
69
70 kvmppc_call_hv_entry:
71 ld r4, HSTATE_KVM_VCPU(r13)
72 bl kvmppc_hv_entry
73
74 /* Back from guest - restore host state and return to caller */
75
76 BEGIN_FTR_SECTION
77 /* Restore host DABR and DABRX */
78 ld r5,HSTATE_DABR(r13)
79 li r6,7
80 mtspr SPRN_DABR,r5
81 mtspr SPRN_DABRX,r6
82 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
83
84 /* Restore SPRG3 */
85 ld r3,PACA_SPRG_VDSO(r13)
86 mtspr SPRN_SPRG_VDSO_WRITE,r3
87
88 /* Reload the host's PMU registers */
89 ld r3, PACALPPACAPTR(r13) /* is the host using the PMU? */
90 lbz r4, LPPACA_PMCINUSE(r3)
91 cmpwi r4, 0
92 beq 23f /* skip if not */
93 BEGIN_FTR_SECTION
94 ld r3, HSTATE_MMCR0(r13)
95 andi. r4, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO
96 cmpwi r4, MMCR0_PMAO
97 beql kvmppc_fix_pmao
98 END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG)
99 lwz r3, HSTATE_PMC1(r13)
100 lwz r4, HSTATE_PMC2(r13)
101 lwz r5, HSTATE_PMC3(r13)
102 lwz r6, HSTATE_PMC4(r13)
103 lwz r8, HSTATE_PMC5(r13)
104 lwz r9, HSTATE_PMC6(r13)
105 mtspr SPRN_PMC1, r3
106 mtspr SPRN_PMC2, r4
107 mtspr SPRN_PMC3, r5
108 mtspr SPRN_PMC4, r6
109 mtspr SPRN_PMC5, r8
110 mtspr SPRN_PMC6, r9
111 ld r3, HSTATE_MMCR0(r13)
112 ld r4, HSTATE_MMCR1(r13)
113 ld r5, HSTATE_MMCRA(r13)
114 ld r6, HSTATE_SIAR(r13)
115 ld r7, HSTATE_SDAR(r13)
116 mtspr SPRN_MMCR1, r4
117 mtspr SPRN_MMCRA, r5
118 mtspr SPRN_SIAR, r6
119 mtspr SPRN_SDAR, r7
120 BEGIN_FTR_SECTION
121 ld r8, HSTATE_MMCR2(r13)
122 ld r9, HSTATE_SIER(r13)
123 mtspr SPRN_MMCR2, r8
124 mtspr SPRN_SIER, r9
125 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
126 mtspr SPRN_MMCR0, r3
127 isync
128 23:
129
130 /*
131 * Reload DEC. HDEC interrupts were disabled when
132 * we reloaded the host's LPCR value.
133 */
134 ld r3, HSTATE_DECEXP(r13)
135 mftb r4
136 subf r4, r4, r3
137 mtspr SPRN_DEC, r4
138
139 /* hwthread_req may have got set by cede or no vcpu, so clear it */
140 li r0, 0
141 stb r0, HSTATE_HWTHREAD_REQ(r13)
142
143 /*
144 * For external and machine check interrupts, we need
145 * to call the Linux handler to process the interrupt.
146 * We do that by jumping to absolute address 0x500 for
147 * external interrupts, or the machine_check_fwnmi label
148 * for machine checks (since firmware might have patched
149 * the vector area at 0x200). The [h]rfid at the end of the
150 * handler will return to the book3s_hv_interrupts.S code.
151 * For other interrupts we do the rfid to get back
152 * to the book3s_hv_interrupts.S code here.
153 */
154 ld r8, 112+PPC_LR_STKOFF(r1)
155 addi r1, r1, 112
156 ld r7, HSTATE_HOST_MSR(r13)
157
158 /*
159 * If we came back from the guest via a relocation-on interrupt,
160 * we will be in virtual mode at this point, which makes it a
161 * little easier to get back to the caller.
162 */
163 mfmsr r0
164 andi. r0, r0, MSR_IR /* in real mode? */
165 bne .Lvirt_return
166
167 cmpwi cr1, r12, BOOK3S_INTERRUPT_MACHINE_CHECK
168 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
169 beq 11f
170 cmpwi r12, BOOK3S_INTERRUPT_H_DOORBELL
171 beq 15f /* Invoke the H_DOORBELL handler */
172 cmpwi cr2, r12, BOOK3S_INTERRUPT_HMI
173 beq cr2, 14f /* HMI check */
174
175 /* RFI into the highmem handler, or branch to interrupt handler */
176 mfmsr r6
177 li r0, MSR_RI
178 andc r6, r6, r0
179 mtmsrd r6, 1 /* Clear RI in MSR */
180 mtsrr0 r8
181 mtsrr1 r7
182 beq cr1, 13f /* machine check */
183 RFI
184
185 /* On POWER7, we have external interrupts set to use HSRR0/1 */
186 11: mtspr SPRN_HSRR0, r8
187 mtspr SPRN_HSRR1, r7
188 ba 0x500
189
190 13: b machine_check_fwnmi
191
192 14: mtspr SPRN_HSRR0, r8
193 mtspr SPRN_HSRR1, r7
194 b hmi_exception_after_realmode
195
196 15: mtspr SPRN_HSRR0, r8
197 mtspr SPRN_HSRR1, r7
198 ba 0xe80
199
200 /* Virtual-mode return - can't get here for HMI or machine check */
201 .Lvirt_return:
202 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
203 beq 16f
204 cmpwi r12, BOOK3S_INTERRUPT_H_DOORBELL
205 beq 17f
206 andi. r0, r7, MSR_EE /* were interrupts hard-enabled? */
207 beq 18f
208 mtmsrd r7, 1 /* if so then re-enable them */
209 18: mtlr r8
210 blr
211
212 16: mtspr SPRN_HSRR0, r8 /* jump to reloc-on external vector */
213 mtspr SPRN_HSRR1, r7
214 b exc_virt_0x4500_hardware_interrupt
215
216 17: mtspr SPRN_HSRR0, r8
217 mtspr SPRN_HSRR1, r7
218 b exc_virt_0x4e80_h_doorbell
219
220 kvmppc_primary_no_guest:
221 /* We handle this much like a ceded vcpu */
222 /* put the HDEC into the DEC, since HDEC interrupts don't wake us */
223 /* HDEC may be larger than DEC for arch >= v3.00, but since the */
224 /* HDEC value came from DEC in the first place, it will fit */
225 mfspr r3, SPRN_HDEC
226 mtspr SPRN_DEC, r3
227 /*
228 * Make sure the primary has finished the MMU switch.
229 * We should never get here on a secondary thread, but
230 * check it for robustness' sake.
231 */
232 ld r5, HSTATE_KVM_VCORE(r13)
233 65: lbz r0, VCORE_IN_GUEST(r5)
234 cmpwi r0, 0
235 beq 65b
236 /* Set LPCR. */
237 ld r8,VCORE_LPCR(r5)
238 mtspr SPRN_LPCR,r8
239 isync
240 /* set our bit in napping_threads */
241 ld r5, HSTATE_KVM_VCORE(r13)
242 lbz r7, HSTATE_PTID(r13)
243 li r0, 1
244 sld r0, r0, r7
245 addi r6, r5, VCORE_NAPPING_THREADS
246 1: lwarx r3, 0, r6
247 or r3, r3, r0
248 stwcx. r3, 0, r6
249 bne 1b
250 /* order napping_threads update vs testing entry_exit_map */
251 isync
252 li r12, 0
253 lwz r7, VCORE_ENTRY_EXIT(r5)
254 cmpwi r7, 0x100
255 bge kvm_novcpu_exit /* another thread already exiting */
256 li r3, NAPPING_NOVCPU
257 stb r3, HSTATE_NAPPING(r13)
258
259 li r3, 0 /* Don't wake on privileged (OS) doorbell */
260 b kvm_do_nap
261
262 /*
263 * kvm_novcpu_wakeup
264 * Entered from kvm_start_guest if kvm_hstate.napping is set
265 * to NAPPING_NOVCPU
266 * r2 = kernel TOC
267 * r13 = paca
268 */
269 kvm_novcpu_wakeup:
270 ld r1, HSTATE_HOST_R1(r13)
271 ld r5, HSTATE_KVM_VCORE(r13)
272 li r0, 0
273 stb r0, HSTATE_NAPPING(r13)
274
275 /* check the wake reason */
276 bl kvmppc_check_wake_reason
277
278 /*
279 * Restore volatile registers since we could have called
280 * a C routine in kvmppc_check_wake_reason.
281 * r5 = VCORE
282 */
283 ld r5, HSTATE_KVM_VCORE(r13)
284
285 /* see if any other thread is already exiting */
286 lwz r0, VCORE_ENTRY_EXIT(r5)
287 cmpwi r0, 0x100
288 bge kvm_novcpu_exit
289
290 /* clear our bit in napping_threads */
291 lbz r7, HSTATE_PTID(r13)
292 li r0, 1
293 sld r0, r0, r7
294 addi r6, r5, VCORE_NAPPING_THREADS
295 4: lwarx r7, 0, r6
296 andc r7, r7, r0
297 stwcx. r7, 0, r6
298 bne 4b
299
300 /* See if the wake reason means we need to exit */
301 cmpdi r3, 0
302 bge kvm_novcpu_exit
303
304 /* See if our timeslice has expired (HDEC is negative) */
305 mfspr r0, SPRN_HDEC
306 EXTEND_HDEC(r0)
307 li r12, BOOK3S_INTERRUPT_HV_DECREMENTER
308 cmpdi r0, 0
309 blt kvm_novcpu_exit
310
311 /* Got an IPI but other vcpus aren't yet exiting, must be a latecomer */
312 ld r4, HSTATE_KVM_VCPU(r13)
313 cmpdi r4, 0
314 beq kvmppc_primary_no_guest
315
316 #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
317 addi r3, r4, VCPU_TB_RMENTRY
318 bl kvmhv_start_timing
319 #endif
320 b kvmppc_got_guest
321
322 kvm_novcpu_exit:
323 #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
324 ld r4, HSTATE_KVM_VCPU(r13)
325 cmpdi r4, 0
326 beq 13f
327 addi r3, r4, VCPU_TB_RMEXIT
328 bl kvmhv_accumulate_time
329 #endif
330 13: mr r3, r12
331 stw r12, 112-4(r1)
332 bl kvmhv_commence_exit
333 nop
334 lwz r12, 112-4(r1)
335 b kvmhv_switch_to_host
336
337 /*
338 * We come in here when wakened from nap mode.
339 * Relocation is off and most register values are lost.
340 * r13 points to the PACA.
341 */
342 .globl kvm_start_guest
343 kvm_start_guest:
344
345 /* Set runlatch bit the minute you wake up from nap */
346 mfspr r0, SPRN_CTRLF
347 ori r0, r0, 1
348 mtspr SPRN_CTRLT, r0
349
350 ld r2,PACATOC(r13)
351
352 li r0,KVM_HWTHREAD_IN_KVM
353 stb r0,HSTATE_HWTHREAD_STATE(r13)
354
355 /* NV GPR values from power7_idle() will no longer be valid */
356 li r0,1
357 stb r0,PACA_NAPSTATELOST(r13)
358
359 /* were we napping due to cede? */
360 lbz r0,HSTATE_NAPPING(r13)
361 cmpwi r0,NAPPING_CEDE
362 beq kvm_end_cede
363 cmpwi r0,NAPPING_NOVCPU
364 beq kvm_novcpu_wakeup
365
366 ld r1,PACAEMERGSP(r13)
367 subi r1,r1,STACK_FRAME_OVERHEAD
368
369 /*
370 * We weren't napping due to cede, so this must be a secondary
371 * thread being woken up to run a guest, or being woken up due
372 * to a stray IPI. (Or due to some machine check or hypervisor
373 * maintenance interrupt while the core is in KVM.)
374 */
375
376 /* Check the wake reason in SRR1 to see why we got here */
377 bl kvmppc_check_wake_reason
378 /*
379 * kvmppc_check_wake_reason could invoke a C routine, but we
380 * have no volatile registers to restore when we return.
381 */
382
383 cmpdi r3, 0
384 bge kvm_no_guest
385
386 /* get vcore pointer, NULL if we have nothing to run */
387 ld r5,HSTATE_KVM_VCORE(r13)
388 cmpdi r5,0
389 /* if we have no vcore to run, go back to sleep */
390 beq kvm_no_guest
391
392 kvm_secondary_got_guest:
393
394 /* Set HSTATE_DSCR(r13) to something sensible */
395 ld r6, PACA_DSCR_DEFAULT(r13)
396 std r6, HSTATE_DSCR(r13)
397
398 /* On thread 0 of a subcore, set HDEC to max */
399 lbz r4, HSTATE_PTID(r13)
400 cmpwi r4, 0
401 bne 63f
402 LOAD_REG_ADDR(r6, decrementer_max)
403 ld r6, 0(r6)
404 mtspr SPRN_HDEC, r6
405 /* and set per-LPAR registers, if doing dynamic micro-threading */
406 ld r6, HSTATE_SPLIT_MODE(r13)
407 cmpdi r6, 0
408 beq 63f
409 ld r0, KVM_SPLIT_RPR(r6)
410 mtspr SPRN_RPR, r0
411 ld r0, KVM_SPLIT_PMMAR(r6)
412 mtspr SPRN_PMMAR, r0
413 ld r0, KVM_SPLIT_LDBAR(r6)
414 mtspr SPRN_LDBAR, r0
415 isync
416 63:
417 /* Order load of vcpu after load of vcore */
418 lwsync
419 ld r4, HSTATE_KVM_VCPU(r13)
420 bl kvmppc_hv_entry
421
422 /* Back from the guest, go back to nap */
423 /* Clear our vcpu and vcore pointers so we don't come back in early */
424 li r0, 0
425 std r0, HSTATE_KVM_VCPU(r13)
426 /*
427 * Once we clear HSTATE_KVM_VCORE(r13), the code in
428 * kvmppc_run_core() is going to assume that all our vcpu
429 * state is visible in memory. This lwsync makes sure
430 * that that is true.
431 */
432 lwsync
433 std r0, HSTATE_KVM_VCORE(r13)
434
435 /*
436 * All secondaries exiting guest will fall through this path.
437 * Before proceeding, just check for HMI interrupt and
438 * invoke opal hmi handler. By now we are sure that the
439 * primary thread on this core/subcore has already made partition
440 * switch/TB resync and we are good to call opal hmi handler.
441 */
442 cmpwi r12, BOOK3S_INTERRUPT_HMI
443 bne kvm_no_guest
444
445 li r3,0 /* NULL argument */
446 bl hmi_exception_realmode
447 /*
448 * At this point we have finished executing in the guest.
449 * We need to wait for hwthread_req to become zero, since
450 * we may not turn on the MMU while hwthread_req is non-zero.
451 * While waiting we also need to check if we get given a vcpu to run.
452 */
453 kvm_no_guest:
454 lbz r3, HSTATE_HWTHREAD_REQ(r13)
455 cmpwi r3, 0
456 bne 53f
457 HMT_MEDIUM
458 li r0, KVM_HWTHREAD_IN_KERNEL
459 stb r0, HSTATE_HWTHREAD_STATE(r13)
460 /* need to recheck hwthread_req after a barrier, to avoid race */
461 sync
462 lbz r3, HSTATE_HWTHREAD_REQ(r13)
463 cmpwi r3, 0
464 bne 54f
465 /*
466 * We jump to pnv_wakeup_loss, which will return to the caller
467 * of power7_nap in the powernv cpu offline loop. The value we
468 * put in r3 becomes the return value for power7_nap.
469 */
470 li r3, LPCR_PECE0
471 mfspr r4, SPRN_LPCR
472 rlwimi r4, r3, 0, LPCR_PECE0 | LPCR_PECE1
473 mtspr SPRN_LPCR, r4
474 li r3, 0
475 b pnv_wakeup_loss
476
477 53: HMT_LOW
478 ld r5, HSTATE_KVM_VCORE(r13)
479 cmpdi r5, 0
480 bne 60f
481 ld r3, HSTATE_SPLIT_MODE(r13)
482 cmpdi r3, 0
483 beq kvm_no_guest
484 lbz r0, KVM_SPLIT_DO_NAP(r3)
485 cmpwi r0, 0
486 beq kvm_no_guest
487 HMT_MEDIUM
488 b kvm_unsplit_nap
489 60: HMT_MEDIUM
490 b kvm_secondary_got_guest
491
492 54: li r0, KVM_HWTHREAD_IN_KVM
493 stb r0, HSTATE_HWTHREAD_STATE(r13)
494 b kvm_no_guest
495
496 /*
497 * Here the primary thread is trying to return the core to
498 * whole-core mode, so we need to nap.
499 */
500 kvm_unsplit_nap:
501 /*
502 * When secondaries are napping in kvm_unsplit_nap() with
503 * hwthread_req = 1, HMI goes ignored even though subcores are
504 * already exited the guest. Hence HMI keeps waking up secondaries
505 * from nap in a loop and secondaries always go back to nap since
506 * no vcore is assigned to them. This makes impossible for primary
507 * thread to get hold of secondary threads resulting into a soft
508 * lockup in KVM path.
509 *
510 * Let us check if HMI is pending and handle it before we go to nap.
511 */
512 cmpwi r12, BOOK3S_INTERRUPT_HMI
513 bne 55f
514 li r3, 0 /* NULL argument */
515 bl hmi_exception_realmode
516 55:
517 /*
518 * Ensure that secondary doesn't nap when it has
519 * its vcore pointer set.
520 */
521 sync /* matches smp_mb() before setting split_info.do_nap */
522 ld r0, HSTATE_KVM_VCORE(r13)
523 cmpdi r0, 0
524 bne kvm_no_guest
525 /* clear any pending message */
526 BEGIN_FTR_SECTION
527 lis r6, (PPC_DBELL_SERVER << (63-36))@h
528 PPC_MSGCLR(6)
529 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
530 /* Set kvm_split_mode.napped[tid] = 1 */
531 ld r3, HSTATE_SPLIT_MODE(r13)
532 li r0, 1
533 lhz r4, PACAPACAINDEX(r13)
534 clrldi r4, r4, 61 /* micro-threading => P8 => 8 threads/core */
535 addi r4, r4, KVM_SPLIT_NAPPED
536 stbx r0, r3, r4
537 /* Check the do_nap flag again after setting napped[] */
538 sync
539 lbz r0, KVM_SPLIT_DO_NAP(r3)
540 cmpwi r0, 0
541 beq 57f
542 li r3, (LPCR_PECEDH | LPCR_PECE0) >> 4
543 mfspr r5, SPRN_LPCR
544 rlwimi r5, r3, 4, (LPCR_PECEDP | LPCR_PECEDH | LPCR_PECE0 | LPCR_PECE1)
545 b kvm_nap_sequence
546
547 57: li r0, 0
548 stbx r0, r3, r4
549 b kvm_no_guest
550
551 /******************************************************************************
552 * *
553 * Entry code *
554 * *
555 *****************************************************************************/
556
557 /* Stack frame offsets */
558 #define STACK_SLOT_TID (112-16)
559 #define STACK_SLOT_PSSCR (112-24)
560 #define STACK_SLOT_PID (112-32)
561 #define STACK_SLOT_IAMR (112-40)
562
563 .global kvmppc_hv_entry
564 kvmppc_hv_entry:
565
566 /* Required state:
567 *
568 * R4 = vcpu pointer (or NULL)
569 * MSR = ~IR|DR
570 * R13 = PACA
571 * R1 = host R1
572 * R2 = TOC
573 * all other volatile GPRS = free
574 * Does not preserve non-volatile GPRs or CR fields
575 */
576 mflr r0
577 std r0, PPC_LR_STKOFF(r1)
578 stdu r1, -112(r1)
579
580 /* Save R1 in the PACA */
581 std r1, HSTATE_HOST_R1(r13)
582
583 li r6, KVM_GUEST_MODE_HOST_HV
584 stb r6, HSTATE_IN_GUEST(r13)
585
586 #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
587 /* Store initial timestamp */
588 cmpdi r4, 0
589 beq 1f
590 addi r3, r4, VCPU_TB_RMENTRY
591 bl kvmhv_start_timing
592 1:
593 #endif
594
595 /* Use cr7 as an indication of radix mode */
596 ld r5, HSTATE_KVM_VCORE(r13)
597 ld r9, VCORE_KVM(r5) /* pointer to struct kvm */
598 lbz r0, KVM_RADIX(r9)
599 cmpwi cr7, r0, 0
600
601 /* Clear out SLB if hash */
602 bne cr7, 2f
603 li r6,0
604 slbmte r6,r6
605 slbia
606 ptesync
607 2:
608 /*
609 * POWER7/POWER8 host -> guest partition switch code.
610 * We don't have to lock against concurrent tlbies,
611 * but we do have to coordinate across hardware threads.
612 */
613 /* Set bit in entry map iff exit map is zero. */
614 li r7, 1
615 lbz r6, HSTATE_PTID(r13)
616 sld r7, r7, r6
617 addi r8, r5, VCORE_ENTRY_EXIT
618 21: lwarx r3, 0, r8
619 cmpwi r3, 0x100 /* any threads starting to exit? */
620 bge secondary_too_late /* if so we're too late to the party */
621 or r3, r3, r7
622 stwcx. r3, 0, r8
623 bne 21b
624
625 /* Primary thread switches to guest partition. */
626 cmpwi r6,0
627 bne 10f
628 lwz r7,KVM_LPID(r9)
629 BEGIN_FTR_SECTION
630 ld r6,KVM_SDR1(r9)
631 li r0,LPID_RSVD /* switch to reserved LPID */
632 mtspr SPRN_LPID,r0
633 ptesync
634 mtspr SPRN_SDR1,r6 /* switch to partition page table */
635 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
636 mtspr SPRN_LPID,r7
637 isync
638
639 /* See if we need to flush the TLB */
640 lhz r6,PACAPACAINDEX(r13) /* test_bit(cpu, need_tlb_flush) */
641 BEGIN_FTR_SECTION
642 /*
643 * On POWER9, individual threads can come in here, but the
644 * TLB is shared between the 4 threads in a core, hence
645 * invalidating on one thread invalidates for all.
646 * Thus we make all 4 threads use the same bit here.
647 */
648 clrrdi r6,r6,2
649 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
650 clrldi r7,r6,64-6 /* extract bit number (6 bits) */
651 srdi r6,r6,6 /* doubleword number */
652 sldi r6,r6,3 /* address offset */
653 add r6,r6,r9
654 addi r6,r6,KVM_NEED_FLUSH /* dword in kvm->arch.need_tlb_flush */
655 li r8,1
656 sld r8,r8,r7
657 ld r7,0(r6)
658 and. r7,r7,r8
659 beq 22f
660 /* Flush the TLB of any entries for this LPID */
661 lwz r0,KVM_TLB_SETS(r9)
662 mtctr r0
663 li r7,0x800 /* IS field = 0b10 */
664 ptesync
665 li r0,0 /* RS for P9 version of tlbiel */
666 bne cr7, 29f
667 28: tlbiel r7 /* On P9, rs=0, RIC=0, PRS=0, R=0 */
668 addi r7,r7,0x1000
669 bdnz 28b
670 b 30f
671 29: PPC_TLBIEL(7,0,2,1,1) /* for radix, RIC=2, PRS=1, R=1 */
672 addi r7,r7,0x1000
673 bdnz 29b
674 30: ptesync
675 23: ldarx r7,0,r6 /* clear the bit after TLB flushed */
676 andc r7,r7,r8
677 stdcx. r7,0,r6
678 bne 23b
679
680 /* Add timebase offset onto timebase */
681 22: ld r8,VCORE_TB_OFFSET(r5)
682 cmpdi r8,0
683 beq 37f
684 mftb r6 /* current host timebase */
685 add r8,r8,r6
686 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
687 mftb r7 /* check if lower 24 bits overflowed */
688 clrldi r6,r6,40
689 clrldi r7,r7,40
690 cmpld r7,r6
691 bge 37f
692 addis r8,r8,0x100 /* if so, increment upper 40 bits */
693 mtspr SPRN_TBU40,r8
694
695 /* Load guest PCR value to select appropriate compat mode */
696 37: ld r7, VCORE_PCR(r5)
697 cmpdi r7, 0
698 beq 38f
699 mtspr SPRN_PCR, r7
700 38:
701
702 BEGIN_FTR_SECTION
703 /* DPDES and VTB are shared between threads */
704 ld r8, VCORE_DPDES(r5)
705 ld r7, VCORE_VTB(r5)
706 mtspr SPRN_DPDES, r8
707 mtspr SPRN_VTB, r7
708 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
709
710 /* Mark the subcore state as inside guest */
711 bl kvmppc_subcore_enter_guest
712 nop
713 ld r5, HSTATE_KVM_VCORE(r13)
714 ld r4, HSTATE_KVM_VCPU(r13)
715 li r0,1
716 stb r0,VCORE_IN_GUEST(r5) /* signal secondaries to continue */
717
718 /* Do we have a guest vcpu to run? */
719 10: cmpdi r4, 0
720 beq kvmppc_primary_no_guest
721 kvmppc_got_guest:
722
723 /* Load up guest SLB entries (N.B. slb_max will be 0 for radix) */
724 lwz r5,VCPU_SLB_MAX(r4)
725 cmpwi r5,0
726 beq 9f
727 mtctr r5
728 addi r6,r4,VCPU_SLB
729 1: ld r8,VCPU_SLB_E(r6)
730 ld r9,VCPU_SLB_V(r6)
731 slbmte r9,r8
732 addi r6,r6,VCPU_SLB_SIZE
733 bdnz 1b
734 9:
735 /* Increment yield count if they have a VPA */
736 ld r3, VCPU_VPA(r4)
737 cmpdi r3, 0
738 beq 25f
739 li r6, LPPACA_YIELDCOUNT
740 LWZX_BE r5, r3, r6
741 addi r5, r5, 1
742 STWX_BE r5, r3, r6
743 li r6, 1
744 stb r6, VCPU_VPA_DIRTY(r4)
745 25:
746
747 /* Save purr/spurr */
748 mfspr r5,SPRN_PURR
749 mfspr r6,SPRN_SPURR
750 std r5,HSTATE_PURR(r13)
751 std r6,HSTATE_SPURR(r13)
752 ld r7,VCPU_PURR(r4)
753 ld r8,VCPU_SPURR(r4)
754 mtspr SPRN_PURR,r7
755 mtspr SPRN_SPURR,r8
756
757 /* Save host values of some registers */
758 BEGIN_FTR_SECTION
759 mfspr r5, SPRN_TIDR
760 mfspr r6, SPRN_PSSCR
761 mfspr r7, SPRN_PID
762 mfspr r8, SPRN_IAMR
763 std r5, STACK_SLOT_TID(r1)
764 std r6, STACK_SLOT_PSSCR(r1)
765 std r7, STACK_SLOT_PID(r1)
766 std r8, STACK_SLOT_IAMR(r1)
767 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
768
769 BEGIN_FTR_SECTION
770 /* Set partition DABR */
771 /* Do this before re-enabling PMU to avoid P7 DABR corruption bug */
772 lwz r5,VCPU_DABRX(r4)
773 ld r6,VCPU_DABR(r4)
774 mtspr SPRN_DABRX,r5
775 mtspr SPRN_DABR,r6
776 isync
777 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
778
779 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
780 BEGIN_FTR_SECTION
781 bl kvmppc_restore_tm
782 END_FTR_SECTION_IFSET(CPU_FTR_TM)
783 #endif
784
785 /* Load guest PMU registers */
786 /* R4 is live here (vcpu pointer) */
787 li r3, 1
788 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
789 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
790 isync
791 BEGIN_FTR_SECTION
792 ld r3, VCPU_MMCR(r4)
793 andi. r5, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO
794 cmpwi r5, MMCR0_PMAO
795 beql kvmppc_fix_pmao
796 END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG)
797 lwz r3, VCPU_PMC(r4) /* always load up guest PMU registers */
798 lwz r5, VCPU_PMC + 4(r4) /* to prevent information leak */
799 lwz r6, VCPU_PMC + 8(r4)
800 lwz r7, VCPU_PMC + 12(r4)
801 lwz r8, VCPU_PMC + 16(r4)
802 lwz r9, VCPU_PMC + 20(r4)
803 mtspr SPRN_PMC1, r3
804 mtspr SPRN_PMC2, r5
805 mtspr SPRN_PMC3, r6
806 mtspr SPRN_PMC4, r7
807 mtspr SPRN_PMC5, r8
808 mtspr SPRN_PMC6, r9
809 ld r3, VCPU_MMCR(r4)
810 ld r5, VCPU_MMCR + 8(r4)
811 ld r6, VCPU_MMCR + 16(r4)
812 ld r7, VCPU_SIAR(r4)
813 ld r8, VCPU_SDAR(r4)
814 mtspr SPRN_MMCR1, r5
815 mtspr SPRN_MMCRA, r6
816 mtspr SPRN_SIAR, r7
817 mtspr SPRN_SDAR, r8
818 BEGIN_FTR_SECTION
819 ld r5, VCPU_MMCR + 24(r4)
820 ld r6, VCPU_SIER(r4)
821 mtspr SPRN_MMCR2, r5
822 mtspr SPRN_SIER, r6
823 BEGIN_FTR_SECTION_NESTED(96)
824 lwz r7, VCPU_PMC + 24(r4)
825 lwz r8, VCPU_PMC + 28(r4)
826 ld r9, VCPU_MMCR + 32(r4)
827 mtspr SPRN_SPMC1, r7
828 mtspr SPRN_SPMC2, r8
829 mtspr SPRN_MMCRS, r9
830 END_FTR_SECTION_NESTED(CPU_FTR_ARCH_300, 0, 96)
831 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
832 mtspr SPRN_MMCR0, r3
833 isync
834
835 /* Load up FP, VMX and VSX registers */
836 bl kvmppc_load_fp
837
838 ld r14, VCPU_GPR(R14)(r4)
839 ld r15, VCPU_GPR(R15)(r4)
840 ld r16, VCPU_GPR(R16)(r4)
841 ld r17, VCPU_GPR(R17)(r4)
842 ld r18, VCPU_GPR(R18)(r4)
843 ld r19, VCPU_GPR(R19)(r4)
844 ld r20, VCPU_GPR(R20)(r4)
845 ld r21, VCPU_GPR(R21)(r4)
846 ld r22, VCPU_GPR(R22)(r4)
847 ld r23, VCPU_GPR(R23)(r4)
848 ld r24, VCPU_GPR(R24)(r4)
849 ld r25, VCPU_GPR(R25)(r4)
850 ld r26, VCPU_GPR(R26)(r4)
851 ld r27, VCPU_GPR(R27)(r4)
852 ld r28, VCPU_GPR(R28)(r4)
853 ld r29, VCPU_GPR(R29)(r4)
854 ld r30, VCPU_GPR(R30)(r4)
855 ld r31, VCPU_GPR(R31)(r4)
856
857 /* Switch DSCR to guest value */
858 ld r5, VCPU_DSCR(r4)
859 mtspr SPRN_DSCR, r5
860
861 BEGIN_FTR_SECTION
862 /* Skip next section on POWER7 */
863 b 8f
864 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
865 /* Load up POWER8-specific registers */
866 ld r5, VCPU_IAMR(r4)
867 lwz r6, VCPU_PSPB(r4)
868 ld r7, VCPU_FSCR(r4)
869 mtspr SPRN_IAMR, r5
870 mtspr SPRN_PSPB, r6
871 mtspr SPRN_FSCR, r7
872 ld r5, VCPU_DAWR(r4)
873 ld r6, VCPU_DAWRX(r4)
874 ld r7, VCPU_CIABR(r4)
875 ld r8, VCPU_TAR(r4)
876 mtspr SPRN_DAWR, r5
877 mtspr SPRN_DAWRX, r6
878 mtspr SPRN_CIABR, r7
879 mtspr SPRN_TAR, r8
880 ld r5, VCPU_IC(r4)
881 ld r8, VCPU_EBBHR(r4)
882 mtspr SPRN_IC, r5
883 mtspr SPRN_EBBHR, r8
884 ld r5, VCPU_EBBRR(r4)
885 ld r6, VCPU_BESCR(r4)
886 lwz r7, VCPU_GUEST_PID(r4)
887 ld r8, VCPU_WORT(r4)
888 mtspr SPRN_EBBRR, r5
889 mtspr SPRN_BESCR, r6
890 mtspr SPRN_PID, r7
891 mtspr SPRN_WORT, r8
892 BEGIN_FTR_SECTION
893 PPC_INVALIDATE_ERAT
894 END_FTR_SECTION_IFSET(CPU_FTR_POWER9_DD1)
895 BEGIN_FTR_SECTION
896 /* POWER8-only registers */
897 ld r5, VCPU_TCSCR(r4)
898 ld r6, VCPU_ACOP(r4)
899 ld r7, VCPU_CSIGR(r4)
900 ld r8, VCPU_TACR(r4)
901 mtspr SPRN_TCSCR, r5
902 mtspr SPRN_ACOP, r6
903 mtspr SPRN_CSIGR, r7
904 mtspr SPRN_TACR, r8
905 FTR_SECTION_ELSE
906 /* POWER9-only registers */
907 ld r5, VCPU_TID(r4)
908 ld r6, VCPU_PSSCR(r4)
909 oris r6, r6, PSSCR_EC@h /* This makes stop trap to HV */
910 mtspr SPRN_TIDR, r5
911 mtspr SPRN_PSSCR, r6
912 ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
913 8:
914
915 /*
916 * Set the decrementer to the guest decrementer.
917 */
918 ld r8,VCPU_DEC_EXPIRES(r4)
919 /* r8 is a host timebase value here, convert to guest TB */
920 ld r5,HSTATE_KVM_VCORE(r13)
921 ld r6,VCORE_TB_OFFSET(r5)
922 add r8,r8,r6
923 mftb r7
924 subf r3,r7,r8
925 mtspr SPRN_DEC,r3
926 stw r3,VCPU_DEC(r4)
927
928 ld r5, VCPU_SPRG0(r4)
929 ld r6, VCPU_SPRG1(r4)
930 ld r7, VCPU_SPRG2(r4)
931 ld r8, VCPU_SPRG3(r4)
932 mtspr SPRN_SPRG0, r5
933 mtspr SPRN_SPRG1, r6
934 mtspr SPRN_SPRG2, r7
935 mtspr SPRN_SPRG3, r8
936
937 /* Load up DAR and DSISR */
938 ld r5, VCPU_DAR(r4)
939 lwz r6, VCPU_DSISR(r4)
940 mtspr SPRN_DAR, r5
941 mtspr SPRN_DSISR, r6
942
943 /* Restore AMR and UAMOR, set AMOR to all 1s */
944 ld r5,VCPU_AMR(r4)
945 ld r6,VCPU_UAMOR(r4)
946 li r7,-1
947 mtspr SPRN_AMR,r5
948 mtspr SPRN_UAMOR,r6
949 mtspr SPRN_AMOR,r7
950
951 /* Restore state of CTRL run bit; assume 1 on entry */
952 lwz r5,VCPU_CTRL(r4)
953 andi. r5,r5,1
954 bne 4f
955 mfspr r6,SPRN_CTRLF
956 clrrdi r6,r6,1
957 mtspr SPRN_CTRLT,r6
958 4:
959 /* Secondary threads wait for primary to have done partition switch */
960 ld r5, HSTATE_KVM_VCORE(r13)
961 lbz r6, HSTATE_PTID(r13)
962 cmpwi r6, 0
963 beq 21f
964 lbz r0, VCORE_IN_GUEST(r5)
965 cmpwi r0, 0
966 bne 21f
967 HMT_LOW
968 20: lwz r3, VCORE_ENTRY_EXIT(r5)
969 cmpwi r3, 0x100
970 bge no_switch_exit
971 lbz r0, VCORE_IN_GUEST(r5)
972 cmpwi r0, 0
973 beq 20b
974 HMT_MEDIUM
975 21:
976 /* Set LPCR. */
977 ld r8,VCORE_LPCR(r5)
978 mtspr SPRN_LPCR,r8
979 isync
980
981 /* Check if HDEC expires soon */
982 mfspr r3, SPRN_HDEC
983 EXTEND_HDEC(r3)
984 cmpdi r3, 512 /* 1 microsecond */
985 blt hdec_soon
986
987 #ifdef CONFIG_KVM_XICS
988 /* We are entering the guest on that thread, push VCPU to XIVE */
989 ld r10, HSTATE_XIVE_TIMA_PHYS(r13)
990 cmpldi cr0, r10, r0
991 beq no_xive
992 ld r11, VCPU_XIVE_SAVED_STATE(r4)
993 li r9, TM_QW1_OS
994 stdcix r11,r9,r10
995 eieio
996 lwz r11, VCPU_XIVE_CAM_WORD(r4)
997 li r9, TM_QW1_OS + TM_WORD2
998 stwcix r11,r9,r10
999 li r9, 1
1000 stw r9, VCPU_XIVE_PUSHED(r4)
1001 no_xive:
1002 #endif /* CONFIG_KVM_XICS */
1003
1004 deliver_guest_interrupt:
1005 ld r6, VCPU_CTR(r4)
1006 ld r7, VCPU_XER(r4)
1007
1008 mtctr r6
1009 mtxer r7
1010
1011 kvmppc_cede_reentry: /* r4 = vcpu, r13 = paca */
1012 ld r10, VCPU_PC(r4)
1013 ld r11, VCPU_MSR(r4)
1014 ld r6, VCPU_SRR0(r4)
1015 ld r7, VCPU_SRR1(r4)
1016 mtspr SPRN_SRR0, r6
1017 mtspr SPRN_SRR1, r7
1018
1019 /* r11 = vcpu->arch.msr & ~MSR_HV */
1020 rldicl r11, r11, 63 - MSR_HV_LG, 1
1021 rotldi r11, r11, 1 + MSR_HV_LG
1022 ori r11, r11, MSR_ME
1023
1024 /* Check if we can deliver an external or decrementer interrupt now */
1025 ld r0, VCPU_PENDING_EXC(r4)
1026 rldicl r0, r0, 64 - BOOK3S_IRQPRIO_EXTERNAL_LEVEL, 63
1027 cmpdi cr1, r0, 0
1028 andi. r8, r11, MSR_EE
1029 mfspr r8, SPRN_LPCR
1030 /* Insert EXTERNAL_LEVEL bit into LPCR at the MER bit position */
1031 rldimi r8, r0, LPCR_MER_SH, 63 - LPCR_MER_SH
1032 mtspr SPRN_LPCR, r8
1033 isync
1034 beq 5f
1035 li r0, BOOK3S_INTERRUPT_EXTERNAL
1036 bne cr1, 12f
1037 mfspr r0, SPRN_DEC
1038 cmpwi r0, 0
1039 li r0, BOOK3S_INTERRUPT_DECREMENTER
1040 bge 5f
1041
1042 12: mtspr SPRN_SRR0, r10
1043 mr r10,r0
1044 mtspr SPRN_SRR1, r11
1045 mr r9, r4
1046 bl kvmppc_msr_interrupt
1047 5:
1048
1049 /*
1050 * Required state:
1051 * R4 = vcpu
1052 * R10: value for HSRR0
1053 * R11: value for HSRR1
1054 * R13 = PACA
1055 */
1056 fast_guest_return:
1057 li r0,0
1058 stb r0,VCPU_CEDED(r4) /* cancel cede */
1059 mtspr SPRN_HSRR0,r10
1060 mtspr SPRN_HSRR1,r11
1061
1062 /* Activate guest mode, so faults get handled by KVM */
1063 li r9, KVM_GUEST_MODE_GUEST_HV
1064 stb r9, HSTATE_IN_GUEST(r13)
1065
1066 #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1067 /* Accumulate timing */
1068 addi r3, r4, VCPU_TB_GUEST
1069 bl kvmhv_accumulate_time
1070 #endif
1071
1072 /* Enter guest */
1073
1074 BEGIN_FTR_SECTION
1075 ld r5, VCPU_CFAR(r4)
1076 mtspr SPRN_CFAR, r5
1077 END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
1078 BEGIN_FTR_SECTION
1079 ld r0, VCPU_PPR(r4)
1080 END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
1081
1082 ld r5, VCPU_LR(r4)
1083 lwz r6, VCPU_CR(r4)
1084 mtlr r5
1085 mtcr r6
1086
1087 ld r1, VCPU_GPR(R1)(r4)
1088 ld r2, VCPU_GPR(R2)(r4)
1089 ld r3, VCPU_GPR(R3)(r4)
1090 ld r5, VCPU_GPR(R5)(r4)
1091 ld r6, VCPU_GPR(R6)(r4)
1092 ld r7, VCPU_GPR(R7)(r4)
1093 ld r8, VCPU_GPR(R8)(r4)
1094 ld r9, VCPU_GPR(R9)(r4)
1095 ld r10, VCPU_GPR(R10)(r4)
1096 ld r11, VCPU_GPR(R11)(r4)
1097 ld r12, VCPU_GPR(R12)(r4)
1098 ld r13, VCPU_GPR(R13)(r4)
1099
1100 BEGIN_FTR_SECTION
1101 mtspr SPRN_PPR, r0
1102 END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
1103 ld r0, VCPU_GPR(R0)(r4)
1104 ld r4, VCPU_GPR(R4)(r4)
1105
1106 hrfid
1107 b .
1108
1109 secondary_too_late:
1110 li r12, 0
1111 cmpdi r4, 0
1112 beq 11f
1113 stw r12, VCPU_TRAP(r4)
1114 #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1115 addi r3, r4, VCPU_TB_RMEXIT
1116 bl kvmhv_accumulate_time
1117 #endif
1118 11: b kvmhv_switch_to_host
1119
1120 no_switch_exit:
1121 HMT_MEDIUM
1122 li r12, 0
1123 b 12f
1124 hdec_soon:
1125 li r12, BOOK3S_INTERRUPT_HV_DECREMENTER
1126 12: stw r12, VCPU_TRAP(r4)
1127 mr r9, r4
1128 #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1129 addi r3, r4, VCPU_TB_RMEXIT
1130 bl kvmhv_accumulate_time
1131 #endif
1132 b guest_exit_cont
1133
1134 /******************************************************************************
1135 * *
1136 * Exit code *
1137 * *
1138 *****************************************************************************/
1139
1140 /*
1141 * We come here from the first-level interrupt handlers.
1142 */
1143 .globl kvmppc_interrupt_hv
1144 kvmppc_interrupt_hv:
1145 /*
1146 * Register contents:
1147 * R12 = (guest CR << 32) | interrupt vector
1148 * R13 = PACA
1149 * guest R12 saved in shadow VCPU SCRATCH0
1150 * guest CTR saved in shadow VCPU SCRATCH1 if RELOCATABLE
1151 * guest R13 saved in SPRN_SCRATCH0
1152 */
1153 std r9, HSTATE_SCRATCH2(r13)
1154 lbz r9, HSTATE_IN_GUEST(r13)
1155 cmpwi r9, KVM_GUEST_MODE_HOST_HV
1156 beq kvmppc_bad_host_intr
1157 #ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1158 cmpwi r9, KVM_GUEST_MODE_GUEST
1159 ld r9, HSTATE_SCRATCH2(r13)
1160 beq kvmppc_interrupt_pr
1161 #endif
1162 /* We're now back in the host but in guest MMU context */
1163 li r9, KVM_GUEST_MODE_HOST_HV
1164 stb r9, HSTATE_IN_GUEST(r13)
1165
1166 ld r9, HSTATE_KVM_VCPU(r13)
1167
1168 /* Save registers */
1169
1170 std r0, VCPU_GPR(R0)(r9)
1171 std r1, VCPU_GPR(R1)(r9)
1172 std r2, VCPU_GPR(R2)(r9)
1173 std r3, VCPU_GPR(R3)(r9)
1174 std r4, VCPU_GPR(R4)(r9)
1175 std r5, VCPU_GPR(R5)(r9)
1176 std r6, VCPU_GPR(R6)(r9)
1177 std r7, VCPU_GPR(R7)(r9)
1178 std r8, VCPU_GPR(R8)(r9)
1179 ld r0, HSTATE_SCRATCH2(r13)
1180 std r0, VCPU_GPR(R9)(r9)
1181 std r10, VCPU_GPR(R10)(r9)
1182 std r11, VCPU_GPR(R11)(r9)
1183 ld r3, HSTATE_SCRATCH0(r13)
1184 std r3, VCPU_GPR(R12)(r9)
1185 /* CR is in the high half of r12 */
1186 srdi r4, r12, 32
1187 stw r4, VCPU_CR(r9)
1188 BEGIN_FTR_SECTION
1189 ld r3, HSTATE_CFAR(r13)
1190 std r3, VCPU_CFAR(r9)
1191 END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
1192 BEGIN_FTR_SECTION
1193 ld r4, HSTATE_PPR(r13)
1194 std r4, VCPU_PPR(r9)
1195 END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
1196
1197 /* Restore R1/R2 so we can handle faults */
1198 ld r1, HSTATE_HOST_R1(r13)
1199 ld r2, PACATOC(r13)
1200
1201 mfspr r10, SPRN_SRR0
1202 mfspr r11, SPRN_SRR1
1203 std r10, VCPU_SRR0(r9)
1204 std r11, VCPU_SRR1(r9)
1205 /* trap is in the low half of r12, clear CR from the high half */
1206 clrldi r12, r12, 32
1207 andi. r0, r12, 2 /* need to read HSRR0/1? */
1208 beq 1f
1209 mfspr r10, SPRN_HSRR0
1210 mfspr r11, SPRN_HSRR1
1211 clrrdi r12, r12, 2
1212 1: std r10, VCPU_PC(r9)
1213 std r11, VCPU_MSR(r9)
1214
1215 GET_SCRATCH0(r3)
1216 mflr r4
1217 std r3, VCPU_GPR(R13)(r9)
1218 std r4, VCPU_LR(r9)
1219
1220 stw r12,VCPU_TRAP(r9)
1221
1222 #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1223 addi r3, r9, VCPU_TB_RMINTR
1224 mr r4, r9
1225 bl kvmhv_accumulate_time
1226 ld r5, VCPU_GPR(R5)(r9)
1227 ld r6, VCPU_GPR(R6)(r9)
1228 ld r7, VCPU_GPR(R7)(r9)
1229 ld r8, VCPU_GPR(R8)(r9)
1230 #endif
1231
1232 /* Save HEIR (HV emulation assist reg) in emul_inst
1233 if this is an HEI (HV emulation interrupt, e40) */
1234 li r3,KVM_INST_FETCH_FAILED
1235 stw r3,VCPU_LAST_INST(r9)
1236 cmpwi r12,BOOK3S_INTERRUPT_H_EMUL_ASSIST
1237 bne 11f
1238 mfspr r3,SPRN_HEIR
1239 11: stw r3,VCPU_HEIR(r9)
1240
1241 /* these are volatile across C function calls */
1242 #ifdef CONFIG_RELOCATABLE
1243 ld r3, HSTATE_SCRATCH1(r13)
1244 mtctr r3
1245 #else
1246 mfctr r3
1247 #endif
1248 mfxer r4
1249 std r3, VCPU_CTR(r9)
1250 std r4, VCPU_XER(r9)
1251
1252 /* If this is a page table miss then see if it's theirs or ours */
1253 cmpwi r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1254 beq kvmppc_hdsi
1255 cmpwi r12, BOOK3S_INTERRUPT_H_INST_STORAGE
1256 beq kvmppc_hisi
1257
1258 /* See if this is a leftover HDEC interrupt */
1259 cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER
1260 bne 2f
1261 mfspr r3,SPRN_HDEC
1262 cmpwi r3,0
1263 mr r4,r9
1264 bge fast_guest_return
1265 2:
1266 /* See if this is an hcall we can handle in real mode */
1267 cmpwi r12,BOOK3S_INTERRUPT_SYSCALL
1268 beq hcall_try_real_mode
1269
1270 /* Hypervisor doorbell - exit only if host IPI flag set */
1271 cmpwi r12, BOOK3S_INTERRUPT_H_DOORBELL
1272 bne 3f
1273 lbz r0, HSTATE_HOST_IPI(r13)
1274 cmpwi r0, 0
1275 beq 4f
1276 b guest_exit_cont
1277 3:
1278 /* External interrupt ? */
1279 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
1280 bne+ guest_exit_cont
1281
1282 /* External interrupt, first check for host_ipi. If this is
1283 * set, we know the host wants us out so let's do it now
1284 */
1285 bl kvmppc_read_intr
1286
1287 /*
1288 * Restore the active volatile registers after returning from
1289 * a C function.
1290 */
1291 ld r9, HSTATE_KVM_VCPU(r13)
1292 li r12, BOOK3S_INTERRUPT_EXTERNAL
1293
1294 /*
1295 * kvmppc_read_intr return codes:
1296 *
1297 * Exit to host (r3 > 0)
1298 * 1 An interrupt is pending that needs to be handled by the host
1299 * Exit guest and return to host by branching to guest_exit_cont
1300 *
1301 * 2 Passthrough that needs completion in the host
1302 * Exit guest and return to host by branching to guest_exit_cont
1303 * However, we also set r12 to BOOK3S_INTERRUPT_HV_RM_HARD
1304 * to indicate to the host to complete handling the interrupt
1305 *
1306 * Before returning to guest, we check if any CPU is heading out
1307 * to the host and if so, we head out also. If no CPUs are heading
1308 * check return values <= 0.
1309 *
1310 * Return to guest (r3 <= 0)
1311 * 0 No external interrupt is pending
1312 * -1 A guest wakeup IPI (which has now been cleared)
1313 * In either case, we return to guest to deliver any pending
1314 * guest interrupts.
1315 *
1316 * -2 A PCI passthrough external interrupt was handled
1317 * (interrupt was delivered directly to guest)
1318 * Return to guest to deliver any pending guest interrupts.
1319 */
1320
1321 cmpdi r3, 1
1322 ble 1f
1323
1324 /* Return code = 2 */
1325 li r12, BOOK3S_INTERRUPT_HV_RM_HARD
1326 stw r12, VCPU_TRAP(r9)
1327 b guest_exit_cont
1328
1329 1: /* Return code <= 1 */
1330 cmpdi r3, 0
1331 bgt guest_exit_cont
1332
1333 /* Return code <= 0 */
1334 4: ld r5, HSTATE_KVM_VCORE(r13)
1335 lwz r0, VCORE_ENTRY_EXIT(r5)
1336 cmpwi r0, 0x100
1337 mr r4, r9
1338 blt deliver_guest_interrupt
1339
1340 guest_exit_cont: /* r9 = vcpu, r12 = trap, r13 = paca */
1341 #ifdef CONFIG_KVM_XICS
1342 /* We are exiting, pull the VP from the XIVE */
1343 lwz r0, VCPU_XIVE_PUSHED(r9)
1344 cmpwi cr0, r0, 0
1345 beq 1f
1346 li r7, TM_SPC_PULL_OS_CTX
1347 li r6, TM_QW1_OS
1348 mfmsr r0
1349 andi. r0, r0, MSR_IR /* in real mode? */
1350 beq 2f
1351 ld r10, HSTATE_XIVE_TIMA_VIRT(r13)
1352 cmpldi cr0, r10, 0
1353 beq 1f
1354 /* First load to pull the context, we ignore the value */
1355 lwzx r11, r7, r10
1356 eieio
1357 /* Second load to recover the context state (Words 0 and 1) */
1358 ldx r11, r6, r10
1359 b 3f
1360 2: ld r10, HSTATE_XIVE_TIMA_PHYS(r13)
1361 cmpldi cr0, r10, 0
1362 beq 1f
1363 /* First load to pull the context, we ignore the value */
1364 lwzcix r11, r7, r10
1365 eieio
1366 /* Second load to recover the context state (Words 0 and 1) */
1367 ldcix r11, r6, r10
1368 3: std r11, VCPU_XIVE_SAVED_STATE(r9)
1369 /* Fixup some of the state for the next load */
1370 li r10, 0
1371 li r0, 0xff
1372 stw r10, VCPU_XIVE_PUSHED(r9)
1373 stb r10, (VCPU_XIVE_SAVED_STATE+3)(r9)
1374 stb r0, (VCPU_XIVE_SAVED_STATE+4)(r9)
1375 1:
1376 #endif /* CONFIG_KVM_XICS */
1377 /* Save more register state */
1378 mfdar r6
1379 mfdsisr r7
1380 std r6, VCPU_DAR(r9)
1381 stw r7, VCPU_DSISR(r9)
1382 /* don't overwrite fault_dar/fault_dsisr if HDSI */
1383 cmpwi r12,BOOK3S_INTERRUPT_H_DATA_STORAGE
1384 beq mc_cont
1385 std r6, VCPU_FAULT_DAR(r9)
1386 stw r7, VCPU_FAULT_DSISR(r9)
1387
1388 /* See if it is a machine check */
1389 cmpwi r12, BOOK3S_INTERRUPT_MACHINE_CHECK
1390 beq machine_check_realmode
1391 mc_cont:
1392 #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1393 addi r3, r9, VCPU_TB_RMEXIT
1394 mr r4, r9
1395 bl kvmhv_accumulate_time
1396 #endif
1397
1398 mr r3, r12
1399 /* Increment exit count, poke other threads to exit */
1400 bl kvmhv_commence_exit
1401 nop
1402 ld r9, HSTATE_KVM_VCPU(r13)
1403 lwz r12, VCPU_TRAP(r9)
1404
1405 /* Stop others sending VCPU interrupts to this physical CPU */
1406 li r0, -1
1407 stw r0, VCPU_CPU(r9)
1408 stw r0, VCPU_THREAD_CPU(r9)
1409
1410 /* Save guest CTRL register, set runlatch to 1 */
1411 mfspr r6,SPRN_CTRLF
1412 stw r6,VCPU_CTRL(r9)
1413 andi. r0,r6,1
1414 bne 4f
1415 ori r6,r6,1
1416 mtspr SPRN_CTRLT,r6
1417 4:
1418 /* Read the guest SLB and save it away */
1419 ld r5, VCPU_KVM(r9)
1420 lbz r0, KVM_RADIX(r5)
1421 cmpwi r0, 0
1422 li r5, 0
1423 bne 3f /* for radix, save 0 entries */
1424 lwz r0,VCPU_SLB_NR(r9) /* number of entries in SLB */
1425 mtctr r0
1426 li r6,0
1427 addi r7,r9,VCPU_SLB
1428 1: slbmfee r8,r6
1429 andis. r0,r8,SLB_ESID_V@h
1430 beq 2f
1431 add r8,r8,r6 /* put index in */
1432 slbmfev r3,r6
1433 std r8,VCPU_SLB_E(r7)
1434 std r3,VCPU_SLB_V(r7)
1435 addi r7,r7,VCPU_SLB_SIZE
1436 addi r5,r5,1
1437 2: addi r6,r6,1
1438 bdnz 1b
1439 3: stw r5,VCPU_SLB_MAX(r9)
1440
1441 /*
1442 * Save the guest PURR/SPURR
1443 */
1444 mfspr r5,SPRN_PURR
1445 mfspr r6,SPRN_SPURR
1446 ld r7,VCPU_PURR(r9)
1447 ld r8,VCPU_SPURR(r9)
1448 std r5,VCPU_PURR(r9)
1449 std r6,VCPU_SPURR(r9)
1450 subf r5,r7,r5
1451 subf r6,r8,r6
1452
1453 /*
1454 * Restore host PURR/SPURR and add guest times
1455 * so that the time in the guest gets accounted.
1456 */
1457 ld r3,HSTATE_PURR(r13)
1458 ld r4,HSTATE_SPURR(r13)
1459 add r3,r3,r5
1460 add r4,r4,r6
1461 mtspr SPRN_PURR,r3
1462 mtspr SPRN_SPURR,r4
1463
1464 /* Save DEC */
1465 mfspr r5,SPRN_DEC
1466 mftb r6
1467 extsw r5,r5
1468 add r5,r5,r6
1469 /* r5 is a guest timebase value here, convert to host TB */
1470 ld r3,HSTATE_KVM_VCORE(r13)
1471 ld r4,VCORE_TB_OFFSET(r3)
1472 subf r5,r4,r5
1473 std r5,VCPU_DEC_EXPIRES(r9)
1474
1475 BEGIN_FTR_SECTION
1476 b 8f
1477 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
1478 /* Save POWER8-specific registers */
1479 mfspr r5, SPRN_IAMR
1480 mfspr r6, SPRN_PSPB
1481 mfspr r7, SPRN_FSCR
1482 std r5, VCPU_IAMR(r9)
1483 stw r6, VCPU_PSPB(r9)
1484 std r7, VCPU_FSCR(r9)
1485 mfspr r5, SPRN_IC
1486 mfspr r7, SPRN_TAR
1487 std r5, VCPU_IC(r9)
1488 std r7, VCPU_TAR(r9)
1489 mfspr r8, SPRN_EBBHR
1490 std r8, VCPU_EBBHR(r9)
1491 mfspr r5, SPRN_EBBRR
1492 mfspr r6, SPRN_BESCR
1493 mfspr r7, SPRN_PID
1494 mfspr r8, SPRN_WORT
1495 std r5, VCPU_EBBRR(r9)
1496 std r6, VCPU_BESCR(r9)
1497 stw r7, VCPU_GUEST_PID(r9)
1498 std r8, VCPU_WORT(r9)
1499 BEGIN_FTR_SECTION
1500 mfspr r5, SPRN_TCSCR
1501 mfspr r6, SPRN_ACOP
1502 mfspr r7, SPRN_CSIGR
1503 mfspr r8, SPRN_TACR
1504 std r5, VCPU_TCSCR(r9)
1505 std r6, VCPU_ACOP(r9)
1506 std r7, VCPU_CSIGR(r9)
1507 std r8, VCPU_TACR(r9)
1508 FTR_SECTION_ELSE
1509 mfspr r5, SPRN_TIDR
1510 mfspr r6, SPRN_PSSCR
1511 std r5, VCPU_TID(r9)
1512 rldicl r6, r6, 4, 50 /* r6 &= PSSCR_GUEST_VIS */
1513 rotldi r6, r6, 60
1514 std r6, VCPU_PSSCR(r9)
1515 ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
1516 /*
1517 * Restore various registers to 0, where non-zero values
1518 * set by the guest could disrupt the host.
1519 */
1520 li r0, 0
1521 mtspr SPRN_CIABR, r0
1522 mtspr SPRN_DAWRX, r0
1523 mtspr SPRN_PSPB, r0
1524 mtspr SPRN_WORT, r0
1525 BEGIN_FTR_SECTION
1526 mtspr SPRN_IAMR, r0
1527 mtspr SPRN_TCSCR, r0
1528 /* Set MMCRS to 1<<31 to freeze and disable the SPMC counters */
1529 li r0, 1
1530 sldi r0, r0, 31
1531 mtspr SPRN_MMCRS, r0
1532 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
1533 8:
1534
1535 /* Save and reset AMR and UAMOR before turning on the MMU */
1536 mfspr r5,SPRN_AMR
1537 mfspr r6,SPRN_UAMOR
1538 std r5,VCPU_AMR(r9)
1539 std r6,VCPU_UAMOR(r9)
1540 li r6,0
1541 mtspr SPRN_AMR,r6
1542 mtspr SPRN_UAMOR, r6
1543
1544 /* Switch DSCR back to host value */
1545 mfspr r8, SPRN_DSCR
1546 ld r7, HSTATE_DSCR(r13)
1547 std r8, VCPU_DSCR(r9)
1548 mtspr SPRN_DSCR, r7
1549
1550 /* Save non-volatile GPRs */
1551 std r14, VCPU_GPR(R14)(r9)
1552 std r15, VCPU_GPR(R15)(r9)
1553 std r16, VCPU_GPR(R16)(r9)
1554 std r17, VCPU_GPR(R17)(r9)
1555 std r18, VCPU_GPR(R18)(r9)
1556 std r19, VCPU_GPR(R19)(r9)
1557 std r20, VCPU_GPR(R20)(r9)
1558 std r21, VCPU_GPR(R21)(r9)
1559 std r22, VCPU_GPR(R22)(r9)
1560 std r23, VCPU_GPR(R23)(r9)
1561 std r24, VCPU_GPR(R24)(r9)
1562 std r25, VCPU_GPR(R25)(r9)
1563 std r26, VCPU_GPR(R26)(r9)
1564 std r27, VCPU_GPR(R27)(r9)
1565 std r28, VCPU_GPR(R28)(r9)
1566 std r29, VCPU_GPR(R29)(r9)
1567 std r30, VCPU_GPR(R30)(r9)
1568 std r31, VCPU_GPR(R31)(r9)
1569
1570 /* Save SPRGs */
1571 mfspr r3, SPRN_SPRG0
1572 mfspr r4, SPRN_SPRG1
1573 mfspr r5, SPRN_SPRG2
1574 mfspr r6, SPRN_SPRG3
1575 std r3, VCPU_SPRG0(r9)
1576 std r4, VCPU_SPRG1(r9)
1577 std r5, VCPU_SPRG2(r9)
1578 std r6, VCPU_SPRG3(r9)
1579
1580 /* save FP state */
1581 mr r3, r9
1582 bl kvmppc_save_fp
1583
1584 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1585 BEGIN_FTR_SECTION
1586 bl kvmppc_save_tm
1587 END_FTR_SECTION_IFSET(CPU_FTR_TM)
1588 #endif
1589
1590 /* Increment yield count if they have a VPA */
1591 ld r8, VCPU_VPA(r9) /* do they have a VPA? */
1592 cmpdi r8, 0
1593 beq 25f
1594 li r4, LPPACA_YIELDCOUNT
1595 LWZX_BE r3, r8, r4
1596 addi r3, r3, 1
1597 STWX_BE r3, r8, r4
1598 li r3, 1
1599 stb r3, VCPU_VPA_DIRTY(r9)
1600 25:
1601 /* Save PMU registers if requested */
1602 /* r8 and cr0.eq are live here */
1603 BEGIN_FTR_SECTION
1604 /*
1605 * POWER8 seems to have a hardware bug where setting
1606 * MMCR0[PMAE] along with MMCR0[PMC1CE] and/or MMCR0[PMCjCE]
1607 * when some counters are already negative doesn't seem
1608 * to cause a performance monitor alert (and hence interrupt).
1609 * The effect of this is that when saving the PMU state,
1610 * if there is no PMU alert pending when we read MMCR0
1611 * before freezing the counters, but one becomes pending
1612 * before we read the counters, we lose it.
1613 * To work around this, we need a way to freeze the counters
1614 * before reading MMCR0. Normally, freezing the counters
1615 * is done by writing MMCR0 (to set MMCR0[FC]) which
1616 * unavoidably writes MMCR0[PMA0] as well. On POWER8,
1617 * we can also freeze the counters using MMCR2, by writing
1618 * 1s to all the counter freeze condition bits (there are
1619 * 9 bits each for 6 counters).
1620 */
1621 li r3, -1 /* set all freeze bits */
1622 clrrdi r3, r3, 10
1623 mfspr r10, SPRN_MMCR2
1624 mtspr SPRN_MMCR2, r3
1625 isync
1626 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1627 li r3, 1
1628 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
1629 mfspr r4, SPRN_MMCR0 /* save MMCR0 */
1630 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
1631 mfspr r6, SPRN_MMCRA
1632 /* Clear MMCRA in order to disable SDAR updates */
1633 li r7, 0
1634 mtspr SPRN_MMCRA, r7
1635 isync
1636 beq 21f /* if no VPA, save PMU stuff anyway */
1637 lbz r7, LPPACA_PMCINUSE(r8)
1638 cmpwi r7, 0 /* did they ask for PMU stuff to be saved? */
1639 bne 21f
1640 std r3, VCPU_MMCR(r9) /* if not, set saved MMCR0 to FC */
1641 b 22f
1642 21: mfspr r5, SPRN_MMCR1
1643 mfspr r7, SPRN_SIAR
1644 mfspr r8, SPRN_SDAR
1645 std r4, VCPU_MMCR(r9)
1646 std r5, VCPU_MMCR + 8(r9)
1647 std r6, VCPU_MMCR + 16(r9)
1648 BEGIN_FTR_SECTION
1649 std r10, VCPU_MMCR + 24(r9)
1650 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1651 std r7, VCPU_SIAR(r9)
1652 std r8, VCPU_SDAR(r9)
1653 mfspr r3, SPRN_PMC1
1654 mfspr r4, SPRN_PMC2
1655 mfspr r5, SPRN_PMC3
1656 mfspr r6, SPRN_PMC4
1657 mfspr r7, SPRN_PMC5
1658 mfspr r8, SPRN_PMC6
1659 stw r3, VCPU_PMC(r9)
1660 stw r4, VCPU_PMC + 4(r9)
1661 stw r5, VCPU_PMC + 8(r9)
1662 stw r6, VCPU_PMC + 12(r9)
1663 stw r7, VCPU_PMC + 16(r9)
1664 stw r8, VCPU_PMC + 20(r9)
1665 BEGIN_FTR_SECTION
1666 mfspr r5, SPRN_SIER
1667 std r5, VCPU_SIER(r9)
1668 BEGIN_FTR_SECTION_NESTED(96)
1669 mfspr r6, SPRN_SPMC1
1670 mfspr r7, SPRN_SPMC2
1671 mfspr r8, SPRN_MMCRS
1672 stw r6, VCPU_PMC + 24(r9)
1673 stw r7, VCPU_PMC + 28(r9)
1674 std r8, VCPU_MMCR + 32(r9)
1675 lis r4, 0x8000
1676 mtspr SPRN_MMCRS, r4
1677 END_FTR_SECTION_NESTED(CPU_FTR_ARCH_300, 0, 96)
1678 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1679 22:
1680 /* Clear out SLB */
1681 li r5,0
1682 slbmte r5,r5
1683 slbia
1684 ptesync
1685
1686 /* Restore host values of some registers */
1687 BEGIN_FTR_SECTION
1688 ld r5, STACK_SLOT_TID(r1)
1689 ld r6, STACK_SLOT_PSSCR(r1)
1690 ld r7, STACK_SLOT_PID(r1)
1691 ld r8, STACK_SLOT_IAMR(r1)
1692 mtspr SPRN_TIDR, r5
1693 mtspr SPRN_PSSCR, r6
1694 mtspr SPRN_PID, r7
1695 mtspr SPRN_IAMR, r8
1696 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
1697 BEGIN_FTR_SECTION
1698 PPC_INVALIDATE_ERAT
1699 END_FTR_SECTION_IFSET(CPU_FTR_POWER9_DD1)
1700
1701 /*
1702 * POWER7/POWER8 guest -> host partition switch code.
1703 * We don't have to lock against tlbies but we do
1704 * have to coordinate the hardware threads.
1705 */
1706 kvmhv_switch_to_host:
1707 /* Secondary threads wait for primary to do partition switch */
1708 ld r5,HSTATE_KVM_VCORE(r13)
1709 ld r4,VCORE_KVM(r5) /* pointer to struct kvm */
1710 lbz r3,HSTATE_PTID(r13)
1711 cmpwi r3,0
1712 beq 15f
1713 HMT_LOW
1714 13: lbz r3,VCORE_IN_GUEST(r5)
1715 cmpwi r3,0
1716 bne 13b
1717 HMT_MEDIUM
1718 b 16f
1719
1720 /* Primary thread waits for all the secondaries to exit guest */
1721 15: lwz r3,VCORE_ENTRY_EXIT(r5)
1722 rlwinm r0,r3,32-8,0xff
1723 clrldi r3,r3,56
1724 cmpw r3,r0
1725 bne 15b
1726 isync
1727
1728 /* Did we actually switch to the guest at all? */
1729 lbz r6, VCORE_IN_GUEST(r5)
1730 cmpwi r6, 0
1731 beq 19f
1732
1733 /* Primary thread switches back to host partition */
1734 lwz r7,KVM_HOST_LPID(r4)
1735 BEGIN_FTR_SECTION
1736 ld r6,KVM_HOST_SDR1(r4)
1737 li r8,LPID_RSVD /* switch to reserved LPID */
1738 mtspr SPRN_LPID,r8
1739 ptesync
1740 mtspr SPRN_SDR1,r6 /* switch to host page table */
1741 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
1742 mtspr SPRN_LPID,r7
1743 isync
1744
1745 BEGIN_FTR_SECTION
1746 /* DPDES and VTB are shared between threads */
1747 mfspr r7, SPRN_DPDES
1748 mfspr r8, SPRN_VTB
1749 std r7, VCORE_DPDES(r5)
1750 std r8, VCORE_VTB(r5)
1751 /* clear DPDES so we don't get guest doorbells in the host */
1752 li r8, 0
1753 mtspr SPRN_DPDES, r8
1754 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1755
1756 /* If HMI, call kvmppc_realmode_hmi_handler() */
1757 cmpwi r12, BOOK3S_INTERRUPT_HMI
1758 bne 27f
1759 bl kvmppc_realmode_hmi_handler
1760 nop
1761 li r12, BOOK3S_INTERRUPT_HMI
1762 /*
1763 * At this point kvmppc_realmode_hmi_handler would have resync-ed
1764 * the TB. Hence it is not required to subtract guest timebase
1765 * offset from timebase. So, skip it.
1766 *
1767 * Also, do not call kvmppc_subcore_exit_guest() because it has
1768 * been invoked as part of kvmppc_realmode_hmi_handler().
1769 */
1770 b 30f
1771
1772 27:
1773 /* Subtract timebase offset from timebase */
1774 ld r8,VCORE_TB_OFFSET(r5)
1775 cmpdi r8,0
1776 beq 17f
1777 mftb r6 /* current guest timebase */
1778 subf r8,r8,r6
1779 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
1780 mftb r7 /* check if lower 24 bits overflowed */
1781 clrldi r6,r6,40
1782 clrldi r7,r7,40
1783 cmpld r7,r6
1784 bge 17f
1785 addis r8,r8,0x100 /* if so, increment upper 40 bits */
1786 mtspr SPRN_TBU40,r8
1787
1788 17: bl kvmppc_subcore_exit_guest
1789 nop
1790 30: ld r5,HSTATE_KVM_VCORE(r13)
1791 ld r4,VCORE_KVM(r5) /* pointer to struct kvm */
1792
1793 /* Reset PCR */
1794 ld r0, VCORE_PCR(r5)
1795 cmpdi r0, 0
1796 beq 18f
1797 li r0, 0
1798 mtspr SPRN_PCR, r0
1799 18:
1800 /* Signal secondary CPUs to continue */
1801 stb r0,VCORE_IN_GUEST(r5)
1802 19: lis r8,0x7fff /* MAX_INT@h */
1803 mtspr SPRN_HDEC,r8
1804
1805 16: ld r8,KVM_HOST_LPCR(r4)
1806 mtspr SPRN_LPCR,r8
1807 isync
1808
1809 /* load host SLB entries */
1810 BEGIN_MMU_FTR_SECTION
1811 b 0f
1812 END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_RADIX)
1813 ld r8,PACA_SLBSHADOWPTR(r13)
1814
1815 .rept SLB_NUM_BOLTED
1816 li r3, SLBSHADOW_SAVEAREA
1817 LDX_BE r5, r8, r3
1818 addi r3, r3, 8
1819 LDX_BE r6, r8, r3
1820 andis. r7,r5,SLB_ESID_V@h
1821 beq 1f
1822 slbmte r6,r5
1823 1: addi r8,r8,16
1824 .endr
1825 0:
1826 #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1827 /* Finish timing, if we have a vcpu */
1828 ld r4, HSTATE_KVM_VCPU(r13)
1829 cmpdi r4, 0
1830 li r3, 0
1831 beq 2f
1832 bl kvmhv_accumulate_time
1833 2:
1834 #endif
1835 /* Unset guest mode */
1836 li r0, KVM_GUEST_MODE_NONE
1837 stb r0, HSTATE_IN_GUEST(r13)
1838
1839 ld r0, 112+PPC_LR_STKOFF(r1)
1840 addi r1, r1, 112
1841 mtlr r0
1842 blr
1843
1844 /*
1845 * Check whether an HDSI is an HPTE not found fault or something else.
1846 * If it is an HPTE not found fault that is due to the guest accessing
1847 * a page that they have mapped but which we have paged out, then
1848 * we continue on with the guest exit path. In all other cases,
1849 * reflect the HDSI to the guest as a DSI.
1850 */
1851 kvmppc_hdsi:
1852 ld r3, VCPU_KVM(r9)
1853 lbz r0, KVM_RADIX(r3)
1854 cmpwi r0, 0
1855 mfspr r4, SPRN_HDAR
1856 mfspr r6, SPRN_HDSISR
1857 bne .Lradix_hdsi /* on radix, just save DAR/DSISR/ASDR */
1858 /* HPTE not found fault or protection fault? */
1859 andis. r0, r6, (DSISR_NOHPTE | DSISR_PROTFAULT)@h
1860 beq 1f /* if not, send it to the guest */
1861 andi. r0, r11, MSR_DR /* data relocation enabled? */
1862 beq 3f
1863 BEGIN_FTR_SECTION
1864 mfspr r5, SPRN_ASDR /* on POWER9, use ASDR to get VSID */
1865 b 4f
1866 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
1867 clrrdi r0, r4, 28
1868 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
1869 li r0, BOOK3S_INTERRUPT_DATA_SEGMENT
1870 bne 7f /* if no SLB entry found */
1871 4: std r4, VCPU_FAULT_DAR(r9)
1872 stw r6, VCPU_FAULT_DSISR(r9)
1873
1874 /* Search the hash table. */
1875 mr r3, r9 /* vcpu pointer */
1876 li r7, 1 /* data fault */
1877 bl kvmppc_hpte_hv_fault
1878 ld r9, HSTATE_KVM_VCPU(r13)
1879 ld r10, VCPU_PC(r9)
1880 ld r11, VCPU_MSR(r9)
1881 li r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1882 cmpdi r3, 0 /* retry the instruction */
1883 beq 6f
1884 cmpdi r3, -1 /* handle in kernel mode */
1885 beq guest_exit_cont
1886 cmpdi r3, -2 /* MMIO emulation; need instr word */
1887 beq 2f
1888
1889 /* Synthesize a DSI (or DSegI) for the guest */
1890 ld r4, VCPU_FAULT_DAR(r9)
1891 mr r6, r3
1892 1: li r0, BOOK3S_INTERRUPT_DATA_STORAGE
1893 mtspr SPRN_DSISR, r6
1894 7: mtspr SPRN_DAR, r4
1895 mtspr SPRN_SRR0, r10
1896 mtspr SPRN_SRR1, r11
1897 mr r10, r0
1898 bl kvmppc_msr_interrupt
1899 fast_interrupt_c_return:
1900 6: ld r7, VCPU_CTR(r9)
1901 ld r8, VCPU_XER(r9)
1902 mtctr r7
1903 mtxer r8
1904 mr r4, r9
1905 b fast_guest_return
1906
1907 3: ld r5, VCPU_KVM(r9) /* not relocated, use VRMA */
1908 ld r5, KVM_VRMA_SLB_V(r5)
1909 b 4b
1910
1911 /* If this is for emulated MMIO, load the instruction word */
1912 2: li r8, KVM_INST_FETCH_FAILED /* In case lwz faults */
1913
1914 /* Set guest mode to 'jump over instruction' so if lwz faults
1915 * we'll just continue at the next IP. */
1916 li r0, KVM_GUEST_MODE_SKIP
1917 stb r0, HSTATE_IN_GUEST(r13)
1918
1919 /* Do the access with MSR:DR enabled */
1920 mfmsr r3
1921 ori r4, r3, MSR_DR /* Enable paging for data */
1922 mtmsrd r4
1923 lwz r8, 0(r10)
1924 mtmsrd r3
1925
1926 /* Store the result */
1927 stw r8, VCPU_LAST_INST(r9)
1928
1929 /* Unset guest mode. */
1930 li r0, KVM_GUEST_MODE_HOST_HV
1931 stb r0, HSTATE_IN_GUEST(r13)
1932 b guest_exit_cont
1933
1934 .Lradix_hdsi:
1935 std r4, VCPU_FAULT_DAR(r9)
1936 stw r6, VCPU_FAULT_DSISR(r9)
1937 .Lradix_hisi:
1938 mfspr r5, SPRN_ASDR
1939 std r5, VCPU_FAULT_GPA(r9)
1940 b guest_exit_cont
1941
1942 /*
1943 * Similarly for an HISI, reflect it to the guest as an ISI unless
1944 * it is an HPTE not found fault for a page that we have paged out.
1945 */
1946 kvmppc_hisi:
1947 ld r3, VCPU_KVM(r9)
1948 lbz r0, KVM_RADIX(r3)
1949 cmpwi r0, 0
1950 bne .Lradix_hisi /* for radix, just save ASDR */
1951 andis. r0, r11, SRR1_ISI_NOPT@h
1952 beq 1f
1953 andi. r0, r11, MSR_IR /* instruction relocation enabled? */
1954 beq 3f
1955 BEGIN_FTR_SECTION
1956 mfspr r5, SPRN_ASDR /* on POWER9, use ASDR to get VSID */
1957 b 4f
1958 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
1959 clrrdi r0, r10, 28
1960 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
1961 li r0, BOOK3S_INTERRUPT_INST_SEGMENT
1962 bne 7f /* if no SLB entry found */
1963 4:
1964 /* Search the hash table. */
1965 mr r3, r9 /* vcpu pointer */
1966 mr r4, r10
1967 mr r6, r11
1968 li r7, 0 /* instruction fault */
1969 bl kvmppc_hpte_hv_fault
1970 ld r9, HSTATE_KVM_VCPU(r13)
1971 ld r10, VCPU_PC(r9)
1972 ld r11, VCPU_MSR(r9)
1973 li r12, BOOK3S_INTERRUPT_H_INST_STORAGE
1974 cmpdi r3, 0 /* retry the instruction */
1975 beq fast_interrupt_c_return
1976 cmpdi r3, -1 /* handle in kernel mode */
1977 beq guest_exit_cont
1978
1979 /* Synthesize an ISI (or ISegI) for the guest */
1980 mr r11, r3
1981 1: li r0, BOOK3S_INTERRUPT_INST_STORAGE
1982 7: mtspr SPRN_SRR0, r10
1983 mtspr SPRN_SRR1, r11
1984 mr r10, r0
1985 bl kvmppc_msr_interrupt
1986 b fast_interrupt_c_return
1987
1988 3: ld r6, VCPU_KVM(r9) /* not relocated, use VRMA */
1989 ld r5, KVM_VRMA_SLB_V(r6)
1990 b 4b
1991
1992 /*
1993 * Try to handle an hcall in real mode.
1994 * Returns to the guest if we handle it, or continues on up to
1995 * the kernel if we can't (i.e. if we don't have a handler for
1996 * it, or if the handler returns H_TOO_HARD).
1997 *
1998 * r5 - r8 contain hcall args,
1999 * r9 = vcpu, r10 = pc, r11 = msr, r12 = trap, r13 = paca
2000 */
2001 hcall_try_real_mode:
2002 ld r3,VCPU_GPR(R3)(r9)
2003 andi. r0,r11,MSR_PR
2004 /* sc 1 from userspace - reflect to guest syscall */
2005 bne sc_1_fast_return
2006 clrrdi r3,r3,2
2007 cmpldi r3,hcall_real_table_end - hcall_real_table
2008 bge guest_exit_cont
2009 /* See if this hcall is enabled for in-kernel handling */
2010 ld r4, VCPU_KVM(r9)
2011 srdi r0, r3, 8 /* r0 = (r3 / 4) >> 6 */
2012 sldi r0, r0, 3 /* index into kvm->arch.enabled_hcalls[] */
2013 add r4, r4, r0
2014 ld r0, KVM_ENABLED_HCALLS(r4)
2015 rlwinm r4, r3, 32-2, 0x3f /* r4 = (r3 / 4) & 0x3f */
2016 srd r0, r0, r4
2017 andi. r0, r0, 1
2018 beq guest_exit_cont
2019 /* Get pointer to handler, if any, and call it */
2020 LOAD_REG_ADDR(r4, hcall_real_table)
2021 lwax r3,r3,r4
2022 cmpwi r3,0
2023 beq guest_exit_cont
2024 add r12,r3,r4
2025 mtctr r12
2026 mr r3,r9 /* get vcpu pointer */
2027 ld r4,VCPU_GPR(R4)(r9)
2028 bctrl
2029 cmpdi r3,H_TOO_HARD
2030 beq hcall_real_fallback
2031 ld r4,HSTATE_KVM_VCPU(r13)
2032 std r3,VCPU_GPR(R3)(r4)
2033 ld r10,VCPU_PC(r4)
2034 ld r11,VCPU_MSR(r4)
2035 b fast_guest_return
2036
2037 sc_1_fast_return:
2038 mtspr SPRN_SRR0,r10
2039 mtspr SPRN_SRR1,r11
2040 li r10, BOOK3S_INTERRUPT_SYSCALL
2041 bl kvmppc_msr_interrupt
2042 mr r4,r9
2043 b fast_guest_return
2044
2045 /* We've attempted a real mode hcall, but it's punted it back
2046 * to userspace. We need to restore some clobbered volatiles
2047 * before resuming the pass-it-to-qemu path */
2048 hcall_real_fallback:
2049 li r12,BOOK3S_INTERRUPT_SYSCALL
2050 ld r9, HSTATE_KVM_VCPU(r13)
2051
2052 b guest_exit_cont
2053
2054 .globl hcall_real_table
2055 hcall_real_table:
2056 .long 0 /* 0 - unused */
2057 .long DOTSYM(kvmppc_h_remove) - hcall_real_table
2058 .long DOTSYM(kvmppc_h_enter) - hcall_real_table
2059 .long DOTSYM(kvmppc_h_read) - hcall_real_table
2060 .long DOTSYM(kvmppc_h_clear_mod) - hcall_real_table
2061 .long DOTSYM(kvmppc_h_clear_ref) - hcall_real_table
2062 .long DOTSYM(kvmppc_h_protect) - hcall_real_table
2063 .long DOTSYM(kvmppc_h_get_tce) - hcall_real_table
2064 .long DOTSYM(kvmppc_rm_h_put_tce) - hcall_real_table
2065 .long 0 /* 0x24 - H_SET_SPRG0 */
2066 .long DOTSYM(kvmppc_h_set_dabr) - hcall_real_table
2067 .long 0 /* 0x2c */
2068 .long 0 /* 0x30 */
2069 .long 0 /* 0x34 */
2070 .long 0 /* 0x38 */
2071 .long 0 /* 0x3c */
2072 .long 0 /* 0x40 */
2073 .long 0 /* 0x44 */
2074 .long 0 /* 0x48 */
2075 .long 0 /* 0x4c */
2076 .long 0 /* 0x50 */
2077 .long 0 /* 0x54 */
2078 .long 0 /* 0x58 */
2079 .long 0 /* 0x5c */
2080 .long 0 /* 0x60 */
2081 #ifdef CONFIG_KVM_XICS
2082 .long DOTSYM(kvmppc_rm_h_eoi) - hcall_real_table
2083 .long DOTSYM(kvmppc_rm_h_cppr) - hcall_real_table
2084 .long DOTSYM(kvmppc_rm_h_ipi) - hcall_real_table
2085 .long DOTSYM(kvmppc_rm_h_ipoll) - hcall_real_table
2086 .long DOTSYM(kvmppc_rm_h_xirr) - hcall_real_table
2087 #else
2088 .long 0 /* 0x64 - H_EOI */
2089 .long 0 /* 0x68 - H_CPPR */
2090 .long 0 /* 0x6c - H_IPI */
2091 .long 0 /* 0x70 - H_IPOLL */
2092 .long 0 /* 0x74 - H_XIRR */
2093 #endif
2094 .long 0 /* 0x78 */
2095 .long 0 /* 0x7c */
2096 .long 0 /* 0x80 */
2097 .long 0 /* 0x84 */
2098 .long 0 /* 0x88 */
2099 .long 0 /* 0x8c */
2100 .long 0 /* 0x90 */
2101 .long 0 /* 0x94 */
2102 .long 0 /* 0x98 */
2103 .long 0 /* 0x9c */
2104 .long 0 /* 0xa0 */
2105 .long 0 /* 0xa4 */
2106 .long 0 /* 0xa8 */
2107 .long 0 /* 0xac */
2108 .long 0 /* 0xb0 */
2109 .long 0 /* 0xb4 */
2110 .long 0 /* 0xb8 */
2111 .long 0 /* 0xbc */
2112 .long 0 /* 0xc0 */
2113 .long 0 /* 0xc4 */
2114 .long 0 /* 0xc8 */
2115 .long 0 /* 0xcc */
2116 .long 0 /* 0xd0 */
2117 .long 0 /* 0xd4 */
2118 .long 0 /* 0xd8 */
2119 .long 0 /* 0xdc */
2120 .long DOTSYM(kvmppc_h_cede) - hcall_real_table
2121 .long DOTSYM(kvmppc_rm_h_confer) - hcall_real_table
2122 .long 0 /* 0xe8 */
2123 .long 0 /* 0xec */
2124 .long 0 /* 0xf0 */
2125 .long 0 /* 0xf4 */
2126 .long 0 /* 0xf8 */
2127 .long 0 /* 0xfc */
2128 .long 0 /* 0x100 */
2129 .long 0 /* 0x104 */
2130 .long 0 /* 0x108 */
2131 .long 0 /* 0x10c */
2132 .long 0 /* 0x110 */
2133 .long 0 /* 0x114 */
2134 .long 0 /* 0x118 */
2135 .long 0 /* 0x11c */
2136 .long 0 /* 0x120 */
2137 .long DOTSYM(kvmppc_h_bulk_remove) - hcall_real_table
2138 .long 0 /* 0x128 */
2139 .long 0 /* 0x12c */
2140 .long 0 /* 0x130 */
2141 .long DOTSYM(kvmppc_h_set_xdabr) - hcall_real_table
2142 .long DOTSYM(kvmppc_rm_h_stuff_tce) - hcall_real_table
2143 .long DOTSYM(kvmppc_rm_h_put_tce_indirect) - hcall_real_table
2144 .long 0 /* 0x140 */
2145 .long 0 /* 0x144 */
2146 .long 0 /* 0x148 */
2147 .long 0 /* 0x14c */
2148 .long 0 /* 0x150 */
2149 .long 0 /* 0x154 */
2150 .long 0 /* 0x158 */
2151 .long 0 /* 0x15c */
2152 .long 0 /* 0x160 */
2153 .long 0 /* 0x164 */
2154 .long 0 /* 0x168 */
2155 .long 0 /* 0x16c */
2156 .long 0 /* 0x170 */
2157 .long 0 /* 0x174 */
2158 .long 0 /* 0x178 */
2159 .long 0 /* 0x17c */
2160 .long 0 /* 0x180 */
2161 .long 0 /* 0x184 */
2162 .long 0 /* 0x188 */
2163 .long 0 /* 0x18c */
2164 .long 0 /* 0x190 */
2165 .long 0 /* 0x194 */
2166 .long 0 /* 0x198 */
2167 .long 0 /* 0x19c */
2168 .long 0 /* 0x1a0 */
2169 .long 0 /* 0x1a4 */
2170 .long 0 /* 0x1a8 */
2171 .long 0 /* 0x1ac */
2172 .long 0 /* 0x1b0 */
2173 .long 0 /* 0x1b4 */
2174 .long 0 /* 0x1b8 */
2175 .long 0 /* 0x1bc */
2176 .long 0 /* 0x1c0 */
2177 .long 0 /* 0x1c4 */
2178 .long 0 /* 0x1c8 */
2179 .long 0 /* 0x1cc */
2180 .long 0 /* 0x1d0 */
2181 .long 0 /* 0x1d4 */
2182 .long 0 /* 0x1d8 */
2183 .long 0 /* 0x1dc */
2184 .long 0 /* 0x1e0 */
2185 .long 0 /* 0x1e4 */
2186 .long 0 /* 0x1e8 */
2187 .long 0 /* 0x1ec */
2188 .long 0 /* 0x1f0 */
2189 .long 0 /* 0x1f4 */
2190 .long 0 /* 0x1f8 */
2191 .long 0 /* 0x1fc */
2192 .long 0 /* 0x200 */
2193 .long 0 /* 0x204 */
2194 .long 0 /* 0x208 */
2195 .long 0 /* 0x20c */
2196 .long 0 /* 0x210 */
2197 .long 0 /* 0x214 */
2198 .long 0 /* 0x218 */
2199 .long 0 /* 0x21c */
2200 .long 0 /* 0x220 */
2201 .long 0 /* 0x224 */
2202 .long 0 /* 0x228 */
2203 .long 0 /* 0x22c */
2204 .long 0 /* 0x230 */
2205 .long 0 /* 0x234 */
2206 .long 0 /* 0x238 */
2207 .long 0 /* 0x23c */
2208 .long 0 /* 0x240 */
2209 .long 0 /* 0x244 */
2210 .long 0 /* 0x248 */
2211 .long 0 /* 0x24c */
2212 .long 0 /* 0x250 */
2213 .long 0 /* 0x254 */
2214 .long 0 /* 0x258 */
2215 .long 0 /* 0x25c */
2216 .long 0 /* 0x260 */
2217 .long 0 /* 0x264 */
2218 .long 0 /* 0x268 */
2219 .long 0 /* 0x26c */
2220 .long 0 /* 0x270 */
2221 .long 0 /* 0x274 */
2222 .long 0 /* 0x278 */
2223 .long 0 /* 0x27c */
2224 .long 0 /* 0x280 */
2225 .long 0 /* 0x284 */
2226 .long 0 /* 0x288 */
2227 .long 0 /* 0x28c */
2228 .long 0 /* 0x290 */
2229 .long 0 /* 0x294 */
2230 .long 0 /* 0x298 */
2231 .long 0 /* 0x29c */
2232 .long 0 /* 0x2a0 */
2233 .long 0 /* 0x2a4 */
2234 .long 0 /* 0x2a8 */
2235 .long 0 /* 0x2ac */
2236 .long 0 /* 0x2b0 */
2237 .long 0 /* 0x2b4 */
2238 .long 0 /* 0x2b8 */
2239 .long 0 /* 0x2bc */
2240 .long 0 /* 0x2c0 */
2241 .long 0 /* 0x2c4 */
2242 .long 0 /* 0x2c8 */
2243 .long 0 /* 0x2cc */
2244 .long 0 /* 0x2d0 */
2245 .long 0 /* 0x2d4 */
2246 .long 0 /* 0x2d8 */
2247 .long 0 /* 0x2dc */
2248 .long 0 /* 0x2e0 */
2249 .long 0 /* 0x2e4 */
2250 .long 0 /* 0x2e8 */
2251 .long 0 /* 0x2ec */
2252 .long 0 /* 0x2f0 */
2253 .long 0 /* 0x2f4 */
2254 .long 0 /* 0x2f8 */
2255 #ifdef CONFIG_KVM_XICS
2256 .long DOTSYM(kvmppc_rm_h_xirr_x) - hcall_real_table
2257 #else
2258 .long 0 /* 0x2fc - H_XIRR_X*/
2259 #endif
2260 .long DOTSYM(kvmppc_h_random) - hcall_real_table
2261 .globl hcall_real_table_end
2262 hcall_real_table_end:
2263
2264 _GLOBAL(kvmppc_h_set_xdabr)
2265 andi. r0, r5, DABRX_USER | DABRX_KERNEL
2266 beq 6f
2267 li r0, DABRX_USER | DABRX_KERNEL | DABRX_BTI
2268 andc. r0, r5, r0
2269 beq 3f
2270 6: li r3, H_PARAMETER
2271 blr
2272
2273 _GLOBAL(kvmppc_h_set_dabr)
2274 li r5, DABRX_USER | DABRX_KERNEL
2275 3:
2276 BEGIN_FTR_SECTION
2277 b 2f
2278 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
2279 std r4,VCPU_DABR(r3)
2280 stw r5, VCPU_DABRX(r3)
2281 mtspr SPRN_DABRX, r5
2282 /* Work around P7 bug where DABR can get corrupted on mtspr */
2283 1: mtspr SPRN_DABR,r4
2284 mfspr r5, SPRN_DABR
2285 cmpd r4, r5
2286 bne 1b
2287 isync
2288 li r3,0
2289 blr
2290
2291 /* Emulate H_SET_DABR/X on P8 for the sake of compat mode guests */
2292 2: rlwimi r5, r4, 5, DAWRX_DR | DAWRX_DW
2293 rlwimi r5, r4, 2, DAWRX_WT
2294 clrrdi r4, r4, 3
2295 std r4, VCPU_DAWR(r3)
2296 std r5, VCPU_DAWRX(r3)
2297 mtspr SPRN_DAWR, r4
2298 mtspr SPRN_DAWRX, r5
2299 li r3, 0
2300 blr
2301
2302 _GLOBAL(kvmppc_h_cede) /* r3 = vcpu pointer, r11 = msr, r13 = paca */
2303 ori r11,r11,MSR_EE
2304 std r11,VCPU_MSR(r3)
2305 li r0,1
2306 stb r0,VCPU_CEDED(r3)
2307 sync /* order setting ceded vs. testing prodded */
2308 lbz r5,VCPU_PRODDED(r3)
2309 cmpwi r5,0
2310 bne kvm_cede_prodded
2311 li r12,0 /* set trap to 0 to say hcall is handled */
2312 stw r12,VCPU_TRAP(r3)
2313 li r0,H_SUCCESS
2314 std r0,VCPU_GPR(R3)(r3)
2315
2316 /*
2317 * Set our bit in the bitmask of napping threads unless all the
2318 * other threads are already napping, in which case we send this
2319 * up to the host.
2320 */
2321 ld r5,HSTATE_KVM_VCORE(r13)
2322 lbz r6,HSTATE_PTID(r13)
2323 lwz r8,VCORE_ENTRY_EXIT(r5)
2324 clrldi r8,r8,56
2325 li r0,1
2326 sld r0,r0,r6
2327 addi r6,r5,VCORE_NAPPING_THREADS
2328 31: lwarx r4,0,r6
2329 or r4,r4,r0
2330 cmpw r4,r8
2331 beq kvm_cede_exit
2332 stwcx. r4,0,r6
2333 bne 31b
2334 /* order napping_threads update vs testing entry_exit_map */
2335 isync
2336 li r0,NAPPING_CEDE
2337 stb r0,HSTATE_NAPPING(r13)
2338 lwz r7,VCORE_ENTRY_EXIT(r5)
2339 cmpwi r7,0x100
2340 bge 33f /* another thread already exiting */
2341
2342 /*
2343 * Although not specifically required by the architecture, POWER7
2344 * preserves the following registers in nap mode, even if an SMT mode
2345 * switch occurs: SLB entries, PURR, SPURR, AMOR, UAMOR, AMR, SPRG0-3,
2346 * DAR, DSISR, DABR, DABRX, DSCR, PMCx, MMCRx, SIAR, SDAR.
2347 */
2348 /* Save non-volatile GPRs */
2349 std r14, VCPU_GPR(R14)(r3)
2350 std r15, VCPU_GPR(R15)(r3)
2351 std r16, VCPU_GPR(R16)(r3)
2352 std r17, VCPU_GPR(R17)(r3)
2353 std r18, VCPU_GPR(R18)(r3)
2354 std r19, VCPU_GPR(R19)(r3)
2355 std r20, VCPU_GPR(R20)(r3)
2356 std r21, VCPU_GPR(R21)(r3)
2357 std r22, VCPU_GPR(R22)(r3)
2358 std r23, VCPU_GPR(R23)(r3)
2359 std r24, VCPU_GPR(R24)(r3)
2360 std r25, VCPU_GPR(R25)(r3)
2361 std r26, VCPU_GPR(R26)(r3)
2362 std r27, VCPU_GPR(R27)(r3)
2363 std r28, VCPU_GPR(R28)(r3)
2364 std r29, VCPU_GPR(R29)(r3)
2365 std r30, VCPU_GPR(R30)(r3)
2366 std r31, VCPU_GPR(R31)(r3)
2367
2368 /* save FP state */
2369 bl kvmppc_save_fp
2370
2371 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2372 BEGIN_FTR_SECTION
2373 ld r9, HSTATE_KVM_VCPU(r13)
2374 bl kvmppc_save_tm
2375 END_FTR_SECTION_IFSET(CPU_FTR_TM)
2376 #endif
2377
2378 /*
2379 * Set DEC to the smaller of DEC and HDEC, so that we wake
2380 * no later than the end of our timeslice (HDEC interrupts
2381 * don't wake us from nap).
2382 */
2383 mfspr r3, SPRN_DEC
2384 mfspr r4, SPRN_HDEC
2385 mftb r5
2386 extsw r3, r3
2387 EXTEND_HDEC(r4)
2388 cmpd r3, r4
2389 ble 67f
2390 mtspr SPRN_DEC, r4
2391 67:
2392 /* save expiry time of guest decrementer */
2393 add r3, r3, r5
2394 ld r4, HSTATE_KVM_VCPU(r13)
2395 ld r5, HSTATE_KVM_VCORE(r13)
2396 ld r6, VCORE_TB_OFFSET(r5)
2397 subf r3, r6, r3 /* convert to host TB value */
2398 std r3, VCPU_DEC_EXPIRES(r4)
2399
2400 #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2401 ld r4, HSTATE_KVM_VCPU(r13)
2402 addi r3, r4, VCPU_TB_CEDE
2403 bl kvmhv_accumulate_time
2404 #endif
2405
2406 lis r3, LPCR_PECEDP@h /* Do wake on privileged doorbell */
2407
2408 /*
2409 * Take a nap until a decrementer or external or doobell interrupt
2410 * occurs, with PECE1 and PECE0 set in LPCR.
2411 * On POWER8, set PECEDH, and if we are ceding, also set PECEDP.
2412 * Also clear the runlatch bit before napping.
2413 */
2414 kvm_do_nap:
2415 mfspr r0, SPRN_CTRLF
2416 clrrdi r0, r0, 1
2417 mtspr SPRN_CTRLT, r0
2418
2419 li r0,1
2420 stb r0,HSTATE_HWTHREAD_REQ(r13)
2421 mfspr r5,SPRN_LPCR
2422 ori r5,r5,LPCR_PECE0 | LPCR_PECE1
2423 BEGIN_FTR_SECTION
2424 ori r5, r5, LPCR_PECEDH
2425 rlwimi r5, r3, 0, LPCR_PECEDP
2426 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
2427
2428 kvm_nap_sequence: /* desired LPCR value in r5 */
2429 BEGIN_FTR_SECTION
2430 /*
2431 * PSSCR bits: exit criterion = 1 (wakeup based on LPCR at sreset)
2432 * enable state loss = 1 (allow SMT mode switch)
2433 * requested level = 0 (just stop dispatching)
2434 */
2435 lis r3, (PSSCR_EC | PSSCR_ESL)@h
2436 mtspr SPRN_PSSCR, r3
2437 /* Set LPCR_PECE_HVEE bit to enable wakeup by HV interrupts */
2438 li r4, LPCR_PECE_HVEE@higher
2439 sldi r4, r4, 32
2440 or r5, r5, r4
2441 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
2442 mtspr SPRN_LPCR,r5
2443 isync
2444 li r0, 0
2445 std r0, HSTATE_SCRATCH0(r13)
2446 ptesync
2447 ld r0, HSTATE_SCRATCH0(r13)
2448 1: cmpd r0, r0
2449 bne 1b
2450 BEGIN_FTR_SECTION
2451 nap
2452 FTR_SECTION_ELSE
2453 PPC_STOP
2454 ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
2455 b .
2456
2457 33: mr r4, r3
2458 li r3, 0
2459 li r12, 0
2460 b 34f
2461
2462 kvm_end_cede:
2463 /* get vcpu pointer */
2464 ld r4, HSTATE_KVM_VCPU(r13)
2465
2466 /* Woken by external or decrementer interrupt */
2467 ld r1, HSTATE_HOST_R1(r13)
2468
2469 #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2470 addi r3, r4, VCPU_TB_RMINTR
2471 bl kvmhv_accumulate_time
2472 #endif
2473
2474 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2475 BEGIN_FTR_SECTION
2476 bl kvmppc_restore_tm
2477 END_FTR_SECTION_IFSET(CPU_FTR_TM)
2478 #endif
2479
2480 /* load up FP state */
2481 bl kvmppc_load_fp
2482
2483 /* Restore guest decrementer */
2484 ld r3, VCPU_DEC_EXPIRES(r4)
2485 ld r5, HSTATE_KVM_VCORE(r13)
2486 ld r6, VCORE_TB_OFFSET(r5)
2487 add r3, r3, r6 /* convert host TB to guest TB value */
2488 mftb r7
2489 subf r3, r7, r3
2490 mtspr SPRN_DEC, r3
2491
2492 /* Load NV GPRS */
2493 ld r14, VCPU_GPR(R14)(r4)
2494 ld r15, VCPU_GPR(R15)(r4)
2495 ld r16, VCPU_GPR(R16)(r4)
2496 ld r17, VCPU_GPR(R17)(r4)
2497 ld r18, VCPU_GPR(R18)(r4)
2498 ld r19, VCPU_GPR(R19)(r4)
2499 ld r20, VCPU_GPR(R20)(r4)
2500 ld r21, VCPU_GPR(R21)(r4)
2501 ld r22, VCPU_GPR(R22)(r4)
2502 ld r23, VCPU_GPR(R23)(r4)
2503 ld r24, VCPU_GPR(R24)(r4)
2504 ld r25, VCPU_GPR(R25)(r4)
2505 ld r26, VCPU_GPR(R26)(r4)
2506 ld r27, VCPU_GPR(R27)(r4)
2507 ld r28, VCPU_GPR(R28)(r4)
2508 ld r29, VCPU_GPR(R29)(r4)
2509 ld r30, VCPU_GPR(R30)(r4)
2510 ld r31, VCPU_GPR(R31)(r4)
2511
2512 /* Check the wake reason in SRR1 to see why we got here */
2513 bl kvmppc_check_wake_reason
2514
2515 /*
2516 * Restore volatile registers since we could have called a
2517 * C routine in kvmppc_check_wake_reason
2518 * r4 = VCPU
2519 * r3 tells us whether we need to return to host or not
2520 * WARNING: it gets checked further down:
2521 * should not modify r3 until this check is done.
2522 */
2523 ld r4, HSTATE_KVM_VCPU(r13)
2524
2525 /* clear our bit in vcore->napping_threads */
2526 34: ld r5,HSTATE_KVM_VCORE(r13)
2527 lbz r7,HSTATE_PTID(r13)
2528 li r0,1
2529 sld r0,r0,r7
2530 addi r6,r5,VCORE_NAPPING_THREADS
2531 32: lwarx r7,0,r6
2532 andc r7,r7,r0
2533 stwcx. r7,0,r6
2534 bne 32b
2535 li r0,0
2536 stb r0,HSTATE_NAPPING(r13)
2537
2538 /* See if the wake reason saved in r3 means we need to exit */
2539 stw r12, VCPU_TRAP(r4)
2540 mr r9, r4
2541 cmpdi r3, 0
2542 bgt guest_exit_cont
2543
2544 /* see if any other thread is already exiting */
2545 lwz r0,VCORE_ENTRY_EXIT(r5)
2546 cmpwi r0,0x100
2547 bge guest_exit_cont
2548
2549 b kvmppc_cede_reentry /* if not go back to guest */
2550
2551 /* cede when already previously prodded case */
2552 kvm_cede_prodded:
2553 li r0,0
2554 stb r0,VCPU_PRODDED(r3)
2555 sync /* order testing prodded vs. clearing ceded */
2556 stb r0,VCPU_CEDED(r3)
2557 li r3,H_SUCCESS
2558 blr
2559
2560 /* we've ceded but we want to give control to the host */
2561 kvm_cede_exit:
2562 ld r9, HSTATE_KVM_VCPU(r13)
2563 b guest_exit_cont
2564
2565 /* Try to handle a machine check in real mode */
2566 machine_check_realmode:
2567 mr r3, r9 /* get vcpu pointer */
2568 bl kvmppc_realmode_machine_check
2569 nop
2570 ld r9, HSTATE_KVM_VCPU(r13)
2571 li r12, BOOK3S_INTERRUPT_MACHINE_CHECK
2572 /*
2573 * Deliver unhandled/fatal (e.g. UE) MCE errors to guest through
2574 * machine check interrupt (set HSRR0 to 0x200). And for handled
2575 * errors (no-fatal), just go back to guest execution with current
2576 * HSRR0 instead of exiting guest. This new approach will inject
2577 * machine check to guest for fatal error causing guest to crash.
2578 *
2579 * The old code used to return to host for unhandled errors which
2580 * was causing guest to hang with soft lockups inside guest and
2581 * makes it difficult to recover guest instance.
2582 *
2583 * if we receive machine check with MSR(RI=0) then deliver it to
2584 * guest as machine check causing guest to crash.
2585 */
2586 ld r11, VCPU_MSR(r9)
2587 rldicl. r0, r11, 64-MSR_HV_LG, 63 /* check if it happened in HV mode */
2588 bne mc_cont /* if so, exit to host */
2589 andi. r10, r11, MSR_RI /* check for unrecoverable exception */
2590 beq 1f /* Deliver a machine check to guest */
2591 ld r10, VCPU_PC(r9)
2592 cmpdi r3, 0 /* Did we handle MCE ? */
2593 bne 2f /* Continue guest execution. */
2594 /* If not, deliver a machine check. SRR0/1 are already set */
2595 1: li r10, BOOK3S_INTERRUPT_MACHINE_CHECK
2596 bl kvmppc_msr_interrupt
2597 2: b fast_interrupt_c_return
2598
2599 /*
2600 * Check the reason we woke from nap, and take appropriate action.
2601 * Returns (in r3):
2602 * 0 if nothing needs to be done
2603 * 1 if something happened that needs to be handled by the host
2604 * -1 if there was a guest wakeup (IPI or msgsnd)
2605 * -2 if we handled a PCI passthrough interrupt (returned by
2606 * kvmppc_read_intr only)
2607 *
2608 * Also sets r12 to the interrupt vector for any interrupt that needs
2609 * to be handled now by the host (0x500 for external interrupt), or zero.
2610 * Modifies all volatile registers (since it may call a C function).
2611 * This routine calls kvmppc_read_intr, a C function, if an external
2612 * interrupt is pending.
2613 */
2614 kvmppc_check_wake_reason:
2615 mfspr r6, SPRN_SRR1
2616 BEGIN_FTR_SECTION
2617 rlwinm r6, r6, 45-31, 0xf /* extract wake reason field (P8) */
2618 FTR_SECTION_ELSE
2619 rlwinm r6, r6, 45-31, 0xe /* P7 wake reason field is 3 bits */
2620 ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_207S)
2621 cmpwi r6, 8 /* was it an external interrupt? */
2622 beq 7f /* if so, see what it was */
2623 li r3, 0
2624 li r12, 0
2625 cmpwi r6, 6 /* was it the decrementer? */
2626 beq 0f
2627 BEGIN_FTR_SECTION
2628 cmpwi r6, 5 /* privileged doorbell? */
2629 beq 0f
2630 cmpwi r6, 3 /* hypervisor doorbell? */
2631 beq 3f
2632 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
2633 cmpwi r6, 0xa /* Hypervisor maintenance ? */
2634 beq 4f
2635 li r3, 1 /* anything else, return 1 */
2636 0: blr
2637
2638 /* hypervisor doorbell */
2639 3: li r12, BOOK3S_INTERRUPT_H_DOORBELL
2640
2641 /*
2642 * Clear the doorbell as we will invoke the handler
2643 * explicitly in the guest exit path.
2644 */
2645 lis r6, (PPC_DBELL_SERVER << (63-36))@h
2646 PPC_MSGCLR(6)
2647 /* see if it's a host IPI */
2648 li r3, 1
2649 lbz r0, HSTATE_HOST_IPI(r13)
2650 cmpwi r0, 0
2651 bnelr
2652 /* if not, return -1 */
2653 li r3, -1
2654 blr
2655
2656 /* Woken up due to Hypervisor maintenance interrupt */
2657 4: li r12, BOOK3S_INTERRUPT_HMI
2658 li r3, 1
2659 blr
2660
2661 /* external interrupt - create a stack frame so we can call C */
2662 7: mflr r0
2663 std r0, PPC_LR_STKOFF(r1)
2664 stdu r1, -PPC_MIN_STKFRM(r1)
2665 bl kvmppc_read_intr
2666 nop
2667 li r12, BOOK3S_INTERRUPT_EXTERNAL
2668 cmpdi r3, 1
2669 ble 1f
2670
2671 /*
2672 * Return code of 2 means PCI passthrough interrupt, but
2673 * we need to return back to host to complete handling the
2674 * interrupt. Trap reason is expected in r12 by guest
2675 * exit code.
2676 */
2677 li r12, BOOK3S_INTERRUPT_HV_RM_HARD
2678 1:
2679 ld r0, PPC_MIN_STKFRM+PPC_LR_STKOFF(r1)
2680 addi r1, r1, PPC_MIN_STKFRM
2681 mtlr r0
2682 blr
2683
2684 /*
2685 * Save away FP, VMX and VSX registers.
2686 * r3 = vcpu pointer
2687 * N.B. r30 and r31 are volatile across this function,
2688 * thus it is not callable from C.
2689 */
2690 kvmppc_save_fp:
2691 mflr r30
2692 mr r31,r3
2693 mfmsr r5
2694 ori r8,r5,MSR_FP
2695 #ifdef CONFIG_ALTIVEC
2696 BEGIN_FTR_SECTION
2697 oris r8,r8,MSR_VEC@h
2698 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2699 #endif
2700 #ifdef CONFIG_VSX
2701 BEGIN_FTR_SECTION
2702 oris r8,r8,MSR_VSX@h
2703 END_FTR_SECTION_IFSET(CPU_FTR_VSX)
2704 #endif
2705 mtmsrd r8
2706 addi r3,r3,VCPU_FPRS
2707 bl store_fp_state
2708 #ifdef CONFIG_ALTIVEC
2709 BEGIN_FTR_SECTION
2710 addi r3,r31,VCPU_VRS
2711 bl store_vr_state
2712 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2713 #endif
2714 mfspr r6,SPRN_VRSAVE
2715 stw r6,VCPU_VRSAVE(r31)
2716 mtlr r30
2717 blr
2718
2719 /*
2720 * Load up FP, VMX and VSX registers
2721 * r4 = vcpu pointer
2722 * N.B. r30 and r31 are volatile across this function,
2723 * thus it is not callable from C.
2724 */
2725 kvmppc_load_fp:
2726 mflr r30
2727 mr r31,r4
2728 mfmsr r9
2729 ori r8,r9,MSR_FP
2730 #ifdef CONFIG_ALTIVEC
2731 BEGIN_FTR_SECTION
2732 oris r8,r8,MSR_VEC@h
2733 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2734 #endif
2735 #ifdef CONFIG_VSX
2736 BEGIN_FTR_SECTION
2737 oris r8,r8,MSR_VSX@h
2738 END_FTR_SECTION_IFSET(CPU_FTR_VSX)
2739 #endif
2740 mtmsrd r8
2741 addi r3,r4,VCPU_FPRS
2742 bl load_fp_state
2743 #ifdef CONFIG_ALTIVEC
2744 BEGIN_FTR_SECTION
2745 addi r3,r31,VCPU_VRS
2746 bl load_vr_state
2747 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2748 #endif
2749 lwz r7,VCPU_VRSAVE(r31)
2750 mtspr SPRN_VRSAVE,r7
2751 mtlr r30
2752 mr r4,r31
2753 blr
2754
2755 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2756 /*
2757 * Save transactional state and TM-related registers.
2758 * Called with r9 pointing to the vcpu struct.
2759 * This can modify all checkpointed registers, but
2760 * restores r1, r2 and r9 (vcpu pointer) before exit.
2761 */
2762 kvmppc_save_tm:
2763 mflr r0
2764 std r0, PPC_LR_STKOFF(r1)
2765
2766 /* Turn on TM. */
2767 mfmsr r8
2768 li r0, 1
2769 rldimi r8, r0, MSR_TM_LG, 63-MSR_TM_LG
2770 mtmsrd r8
2771
2772 ld r5, VCPU_MSR(r9)
2773 rldicl. r5, r5, 64 - MSR_TS_S_LG, 62
2774 beq 1f /* TM not active in guest. */
2775
2776 std r1, HSTATE_HOST_R1(r13)
2777 li r3, TM_CAUSE_KVM_RESCHED
2778
2779 /* Clear the MSR RI since r1, r13 are all going to be foobar. */
2780 li r5, 0
2781 mtmsrd r5, 1
2782
2783 /* All GPRs are volatile at this point. */
2784 TRECLAIM(R3)
2785
2786 /* Temporarily store r13 and r9 so we have some regs to play with */
2787 SET_SCRATCH0(r13)
2788 GET_PACA(r13)
2789 std r9, PACATMSCRATCH(r13)
2790 ld r9, HSTATE_KVM_VCPU(r13)
2791
2792 /* Get a few more GPRs free. */
2793 std r29, VCPU_GPRS_TM(29)(r9)
2794 std r30, VCPU_GPRS_TM(30)(r9)
2795 std r31, VCPU_GPRS_TM(31)(r9)
2796
2797 /* Save away PPR and DSCR soon so don't run with user values. */
2798 mfspr r31, SPRN_PPR
2799 HMT_MEDIUM
2800 mfspr r30, SPRN_DSCR
2801 ld r29, HSTATE_DSCR(r13)
2802 mtspr SPRN_DSCR, r29
2803
2804 /* Save all but r9, r13 & r29-r31 */
2805 reg = 0
2806 .rept 29
2807 .if (reg != 9) && (reg != 13)
2808 std reg, VCPU_GPRS_TM(reg)(r9)
2809 .endif
2810 reg = reg + 1
2811 .endr
2812 /* ... now save r13 */
2813 GET_SCRATCH0(r4)
2814 std r4, VCPU_GPRS_TM(13)(r9)
2815 /* ... and save r9 */
2816 ld r4, PACATMSCRATCH(r13)
2817 std r4, VCPU_GPRS_TM(9)(r9)
2818
2819 /* Reload stack pointer and TOC. */
2820 ld r1, HSTATE_HOST_R1(r13)
2821 ld r2, PACATOC(r13)
2822
2823 /* Set MSR RI now we have r1 and r13 back. */
2824 li r5, MSR_RI
2825 mtmsrd r5, 1
2826
2827 /* Save away checkpinted SPRs. */
2828 std r31, VCPU_PPR_TM(r9)
2829 std r30, VCPU_DSCR_TM(r9)
2830 mflr r5
2831 mfcr r6
2832 mfctr r7
2833 mfspr r8, SPRN_AMR
2834 mfspr r10, SPRN_TAR
2835 mfxer r11
2836 std r5, VCPU_LR_TM(r9)
2837 stw r6, VCPU_CR_TM(r9)
2838 std r7, VCPU_CTR_TM(r9)
2839 std r8, VCPU_AMR_TM(r9)
2840 std r10, VCPU_TAR_TM(r9)
2841 std r11, VCPU_XER_TM(r9)
2842
2843 /* Restore r12 as trap number. */
2844 lwz r12, VCPU_TRAP(r9)
2845
2846 /* Save FP/VSX. */
2847 addi r3, r9, VCPU_FPRS_TM
2848 bl store_fp_state
2849 addi r3, r9, VCPU_VRS_TM
2850 bl store_vr_state
2851 mfspr r6, SPRN_VRSAVE
2852 stw r6, VCPU_VRSAVE_TM(r9)
2853 1:
2854 /*
2855 * We need to save these SPRs after the treclaim so that the software
2856 * error code is recorded correctly in the TEXASR. Also the user may
2857 * change these outside of a transaction, so they must always be
2858 * context switched.
2859 */
2860 mfspr r5, SPRN_TFHAR
2861 mfspr r6, SPRN_TFIAR
2862 mfspr r7, SPRN_TEXASR
2863 std r5, VCPU_TFHAR(r9)
2864 std r6, VCPU_TFIAR(r9)
2865 std r7, VCPU_TEXASR(r9)
2866
2867 ld r0, PPC_LR_STKOFF(r1)
2868 mtlr r0
2869 blr
2870
2871 /*
2872 * Restore transactional state and TM-related registers.
2873 * Called with r4 pointing to the vcpu struct.
2874 * This potentially modifies all checkpointed registers.
2875 * It restores r1, r2, r4 from the PACA.
2876 */
2877 kvmppc_restore_tm:
2878 mflr r0
2879 std r0, PPC_LR_STKOFF(r1)
2880
2881 /* Turn on TM/FP/VSX/VMX so we can restore them. */
2882 mfmsr r5
2883 li r6, MSR_TM >> 32
2884 sldi r6, r6, 32
2885 or r5, r5, r6
2886 ori r5, r5, MSR_FP
2887 oris r5, r5, (MSR_VEC | MSR_VSX)@h
2888 mtmsrd r5
2889
2890 /*
2891 * The user may change these outside of a transaction, so they must
2892 * always be context switched.
2893 */
2894 ld r5, VCPU_TFHAR(r4)
2895 ld r6, VCPU_TFIAR(r4)
2896 ld r7, VCPU_TEXASR(r4)
2897 mtspr SPRN_TFHAR, r5
2898 mtspr SPRN_TFIAR, r6
2899 mtspr SPRN_TEXASR, r7
2900
2901 ld r5, VCPU_MSR(r4)
2902 rldicl. r5, r5, 64 - MSR_TS_S_LG, 62
2903 beqlr /* TM not active in guest */
2904 std r1, HSTATE_HOST_R1(r13)
2905
2906 /* Make sure the failure summary is set, otherwise we'll program check
2907 * when we trechkpt. It's possible that this might have been not set
2908 * on a kvmppc_set_one_reg() call but we shouldn't let this crash the
2909 * host.
2910 */
2911 oris r7, r7, (TEXASR_FS)@h
2912 mtspr SPRN_TEXASR, r7
2913
2914 /*
2915 * We need to load up the checkpointed state for the guest.
2916 * We need to do this early as it will blow away any GPRs, VSRs and
2917 * some SPRs.
2918 */
2919
2920 mr r31, r4
2921 addi r3, r31, VCPU_FPRS_TM
2922 bl load_fp_state
2923 addi r3, r31, VCPU_VRS_TM
2924 bl load_vr_state
2925 mr r4, r31
2926 lwz r7, VCPU_VRSAVE_TM(r4)
2927 mtspr SPRN_VRSAVE, r7
2928
2929 ld r5, VCPU_LR_TM(r4)
2930 lwz r6, VCPU_CR_TM(r4)
2931 ld r7, VCPU_CTR_TM(r4)
2932 ld r8, VCPU_AMR_TM(r4)
2933 ld r9, VCPU_TAR_TM(r4)
2934 ld r10, VCPU_XER_TM(r4)
2935 mtlr r5
2936 mtcr r6
2937 mtctr r7
2938 mtspr SPRN_AMR, r8
2939 mtspr SPRN_TAR, r9
2940 mtxer r10
2941
2942 /*
2943 * Load up PPR and DSCR values but don't put them in the actual SPRs
2944 * till the last moment to avoid running with userspace PPR and DSCR for
2945 * too long.
2946 */
2947 ld r29, VCPU_DSCR_TM(r4)
2948 ld r30, VCPU_PPR_TM(r4)
2949
2950 std r2, PACATMSCRATCH(r13) /* Save TOC */
2951
2952 /* Clear the MSR RI since r1, r13 are all going to be foobar. */
2953 li r5, 0
2954 mtmsrd r5, 1
2955
2956 /* Load GPRs r0-r28 */
2957 reg = 0
2958 .rept 29
2959 ld reg, VCPU_GPRS_TM(reg)(r31)
2960 reg = reg + 1
2961 .endr
2962
2963 mtspr SPRN_DSCR, r29
2964 mtspr SPRN_PPR, r30
2965
2966 /* Load final GPRs */
2967 ld 29, VCPU_GPRS_TM(29)(r31)
2968 ld 30, VCPU_GPRS_TM(30)(r31)
2969 ld 31, VCPU_GPRS_TM(31)(r31)
2970
2971 /* TM checkpointed state is now setup. All GPRs are now volatile. */
2972 TRECHKPT
2973
2974 /* Now let's get back the state we need. */
2975 HMT_MEDIUM
2976 GET_PACA(r13)
2977 ld r29, HSTATE_DSCR(r13)
2978 mtspr SPRN_DSCR, r29
2979 ld r4, HSTATE_KVM_VCPU(r13)
2980 ld r1, HSTATE_HOST_R1(r13)
2981 ld r2, PACATMSCRATCH(r13)
2982
2983 /* Set the MSR RI since we have our registers back. */
2984 li r5, MSR_RI
2985 mtmsrd r5, 1
2986
2987 ld r0, PPC_LR_STKOFF(r1)
2988 mtlr r0
2989 blr
2990 #endif
2991
2992 /*
2993 * We come here if we get any exception or interrupt while we are
2994 * executing host real mode code while in guest MMU context.
2995 * For now just spin, but we should do something better.
2996 */
2997 kvmppc_bad_host_intr:
2998 b .
2999
3000 /*
3001 * This mimics the MSR transition on IRQ delivery. The new guest MSR is taken
3002 * from VCPU_INTR_MSR and is modified based on the required TM state changes.
3003 * r11 has the guest MSR value (in/out)
3004 * r9 has a vcpu pointer (in)
3005 * r0 is used as a scratch register
3006 */
3007 kvmppc_msr_interrupt:
3008 rldicl r0, r11, 64 - MSR_TS_S_LG, 62
3009 cmpwi r0, 2 /* Check if we are in transactional state.. */
3010 ld r11, VCPU_INTR_MSR(r9)
3011 bne 1f
3012 /* ... if transactional, change to suspended */
3013 li r0, 1
3014 1: rldimi r11, r0, MSR_TS_S_LG, 63 - MSR_TS_T_LG
3015 blr
3016
3017 /*
3018 * This works around a hardware bug on POWER8E processors, where
3019 * writing a 1 to the MMCR0[PMAO] bit doesn't generate a
3020 * performance monitor interrupt. Instead, when we need to have
3021 * an interrupt pending, we have to arrange for a counter to overflow.
3022 */
3023 kvmppc_fix_pmao:
3024 li r3, 0
3025 mtspr SPRN_MMCR2, r3
3026 lis r3, (MMCR0_PMXE | MMCR0_FCECE)@h
3027 ori r3, r3, MMCR0_PMCjCE | MMCR0_C56RUN
3028 mtspr SPRN_MMCR0, r3
3029 lis r3, 0x7fff
3030 ori r3, r3, 0xffff
3031 mtspr SPRN_PMC6, r3
3032 isync
3033 blr
3034
3035 #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
3036 /*
3037 * Start timing an activity
3038 * r3 = pointer to time accumulation struct, r4 = vcpu
3039 */
3040 kvmhv_start_timing:
3041 ld r5, HSTATE_KVM_VCORE(r13)
3042 lbz r6, VCORE_IN_GUEST(r5)
3043 cmpwi r6, 0
3044 beq 5f /* if in guest, need to */
3045 ld r6, VCORE_TB_OFFSET(r5) /* subtract timebase offset */
3046 5: mftb r5
3047 subf r5, r6, r5
3048 std r3, VCPU_CUR_ACTIVITY(r4)
3049 std r5, VCPU_ACTIVITY_START(r4)
3050 blr
3051
3052 /*
3053 * Accumulate time to one activity and start another.
3054 * r3 = pointer to new time accumulation struct, r4 = vcpu
3055 */
3056 kvmhv_accumulate_time:
3057 ld r5, HSTATE_KVM_VCORE(r13)
3058 lbz r8, VCORE_IN_GUEST(r5)
3059 cmpwi r8, 0
3060 beq 4f /* if in guest, need to */
3061 ld r8, VCORE_TB_OFFSET(r5) /* subtract timebase offset */
3062 4: ld r5, VCPU_CUR_ACTIVITY(r4)
3063 ld r6, VCPU_ACTIVITY_START(r4)
3064 std r3, VCPU_CUR_ACTIVITY(r4)
3065 mftb r7
3066 subf r7, r8, r7
3067 std r7, VCPU_ACTIVITY_START(r4)
3068 cmpdi r5, 0
3069 beqlr
3070 subf r3, r6, r7
3071 ld r8, TAS_SEQCOUNT(r5)
3072 cmpdi r8, 0
3073 addi r8, r8, 1
3074 std r8, TAS_SEQCOUNT(r5)
3075 lwsync
3076 ld r7, TAS_TOTAL(r5)
3077 add r7, r7, r3
3078 std r7, TAS_TOTAL(r5)
3079 ld r6, TAS_MIN(r5)
3080 ld r7, TAS_MAX(r5)
3081 beq 3f
3082 cmpd r3, r6
3083 bge 1f
3084 3: std r3, TAS_MIN(r5)
3085 1: cmpd r3, r7
3086 ble 2f
3087 std r3, TAS_MAX(r5)
3088 2: lwsync
3089 addi r8, r8, 1
3090 std r8, TAS_SEQCOUNT(r5)
3091 blr
3092 #endif