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