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