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