KVM: PPC: Book3S HV: Implement dynamic micro-threading on POWER8
[cascardo/linux.git] / arch / powerpc / kvm / book3s_hv_rmhandlers.S
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License, version 2, as
4  * published by the Free Software Foundation.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9  * GNU General Public License for more details.
10  *
11  * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
12  *
13  * Derived from book3s_rmhandlers.S and other files, which are:
14  *
15  * Copyright SUSE Linux Products GmbH 2009
16  *
17  * Authors: Alexander Graf <agraf@suse.de>
18  */
19
20 #include <asm/ppc_asm.h>
21 #include <asm/kvm_asm.h>
22 #include <asm/reg.h>
23 #include <asm/mmu.h>
24 #include <asm/page.h>
25 #include <asm/ptrace.h>
26 #include <asm/hvcall.h>
27 #include <asm/asm-offsets.h>
28 #include <asm/exception-64s.h>
29 #include <asm/kvm_book3s_asm.h>
30 #include <asm/mmu-hash64.h>
31 #include <asm/tm.h>
32
33 #define VCPU_GPRS_TM(reg) (((reg) * ULONG_SIZE) + VCPU_GPR_TM)
34
35 /* Values in HSTATE_NAPPING(r13) */
36 #define NAPPING_CEDE    1
37 #define NAPPING_NOVCPU  2
38
39 /*
40  * Call kvmppc_hv_entry in real mode.
41  * Must be called with interrupts hard-disabled.
42  *
43  * Input Registers:
44  *
45  * LR = return address to continue at after eventually re-enabling MMU
46  */
47 _GLOBAL_TOC(kvmppc_hv_entry_trampoline)
48         mflr    r0
49         std     r0, PPC_LR_STKOFF(r1)
50         stdu    r1, -112(r1)
51         mfmsr   r10
52         LOAD_REG_ADDR(r5, kvmppc_call_hv_entry)
53         li      r0,MSR_RI
54         andc    r0,r10,r0
55         li      r6,MSR_IR | MSR_DR
56         andc    r6,r10,r6
57         mtmsrd  r0,1            /* clear RI in MSR */
58         mtsrr0  r5
59         mtsrr1  r6
60         RFI
61
62 kvmppc_call_hv_entry:
63         ld      r4, HSTATE_KVM_VCPU(r13)
64         bl      kvmppc_hv_entry
65
66         /* Back from guest - restore host state and return to caller */
67
68 BEGIN_FTR_SECTION
69         /* Restore host DABR and DABRX */
70         ld      r5,HSTATE_DABR(r13)
71         li      r6,7
72         mtspr   SPRN_DABR,r5
73         mtspr   SPRN_DABRX,r6
74 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
75
76         /* Restore SPRG3 */
77         ld      r3,PACA_SPRG_VDSO(r13)
78         mtspr   SPRN_SPRG_VDSO_WRITE,r3
79
80         /* Reload the host's PMU registers */
81         ld      r3, PACALPPACAPTR(r13)  /* is the host using the PMU? */
82         lbz     r4, LPPACA_PMCINUSE(r3)
83         cmpwi   r4, 0
84         beq     23f                     /* skip if not */
85 BEGIN_FTR_SECTION
86         ld      r3, HSTATE_MMCR0(r13)
87         andi.   r4, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO
88         cmpwi   r4, MMCR0_PMAO
89         beql    kvmppc_fix_pmao
90 END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG)
91         lwz     r3, HSTATE_PMC1(r13)
92         lwz     r4, HSTATE_PMC2(r13)
93         lwz     r5, HSTATE_PMC3(r13)
94         lwz     r6, HSTATE_PMC4(r13)
95         lwz     r8, HSTATE_PMC5(r13)
96         lwz     r9, HSTATE_PMC6(r13)
97         mtspr   SPRN_PMC1, r3
98         mtspr   SPRN_PMC2, r4
99         mtspr   SPRN_PMC3, r5
100         mtspr   SPRN_PMC4, r6
101         mtspr   SPRN_PMC5, r8
102         mtspr   SPRN_PMC6, r9
103         ld      r3, HSTATE_MMCR0(r13)
104         ld      r4, HSTATE_MMCR1(r13)
105         ld      r5, HSTATE_MMCRA(r13)
106         ld      r6, HSTATE_SIAR(r13)
107         ld      r7, HSTATE_SDAR(r13)
108         mtspr   SPRN_MMCR1, r4
109         mtspr   SPRN_MMCRA, r5
110         mtspr   SPRN_SIAR, r6
111         mtspr   SPRN_SDAR, r7
112 BEGIN_FTR_SECTION
113         ld      r8, HSTATE_MMCR2(r13)
114         ld      r9, HSTATE_SIER(r13)
115         mtspr   SPRN_MMCR2, r8
116         mtspr   SPRN_SIER, r9
117 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
118         mtspr   SPRN_MMCR0, r3
119         isync
120 23:
121
122         /*
123          * Reload DEC.  HDEC interrupts were disabled when
124          * we reloaded the host's LPCR value.
125          */
126         ld      r3, HSTATE_DECEXP(r13)
127         mftb    r4
128         subf    r4, r4, r3
129         mtspr   SPRN_DEC, r4
130
131         /* hwthread_req may have got set by cede or no vcpu, so clear it */
132         li      r0, 0
133         stb     r0, HSTATE_HWTHREAD_REQ(r13)
134
135         /*
136          * For external and machine check interrupts, we need
137          * to call the Linux handler to process the interrupt.
138          * We do that by jumping to absolute address 0x500 for
139          * external interrupts, or the machine_check_fwnmi label
140          * for machine checks (since firmware might have patched
141          * the vector area at 0x200).  The [h]rfid at the end of the
142          * handler will return to the book3s_hv_interrupts.S code.
143          * For other interrupts we do the rfid to get back
144          * to the book3s_hv_interrupts.S code here.
145          */
146         ld      r8, 112+PPC_LR_STKOFF(r1)
147         addi    r1, r1, 112
148         ld      r7, HSTATE_HOST_MSR(r13)
149
150         cmpwi   cr1, r12, BOOK3S_INTERRUPT_MACHINE_CHECK
151         cmpwi   r12, BOOK3S_INTERRUPT_EXTERNAL
152         beq     11f
153         cmpwi   cr2, r12, BOOK3S_INTERRUPT_HMI
154         beq     cr2, 14f                        /* HMI check */
155
156         /* RFI into the highmem handler, or branch to interrupt handler */
157         mfmsr   r6
158         li      r0, MSR_RI
159         andc    r6, r6, r0
160         mtmsrd  r6, 1                   /* Clear RI in MSR */
161         mtsrr0  r8
162         mtsrr1  r7
163         beq     cr1, 13f                /* machine check */
164         RFI
165
166         /* On POWER7, we have external interrupts set to use HSRR0/1 */
167 11:     mtspr   SPRN_HSRR0, r8
168         mtspr   SPRN_HSRR1, r7
169         ba      0x500
170
171 13:     b       machine_check_fwnmi
172
173 14:     mtspr   SPRN_HSRR0, r8
174         mtspr   SPRN_HSRR1, r7
175         b       hmi_exception_after_realmode
176
177 kvmppc_primary_no_guest:
178         /* We handle this much like a ceded vcpu */
179         /* put the HDEC into the DEC, since HDEC interrupts don't wake us */
180         mfspr   r3, SPRN_HDEC
181         mtspr   SPRN_DEC, r3
182         /*
183          * Make sure the primary has finished the MMU switch.
184          * We should never get here on a secondary thread, but
185          * check it for robustness' sake.
186          */
187         ld      r5, HSTATE_KVM_VCORE(r13)
188 65:     lbz     r0, VCORE_IN_GUEST(r5)
189         cmpwi   r0, 0
190         beq     65b
191         /* Set LPCR. */
192         ld      r8,VCORE_LPCR(r5)
193         mtspr   SPRN_LPCR,r8
194         isync
195         /* set our bit in napping_threads */
196         ld      r5, HSTATE_KVM_VCORE(r13)
197         lbz     r7, HSTATE_PTID(r13)
198         li      r0, 1
199         sld     r0, r0, r7
200         addi    r6, r5, VCORE_NAPPING_THREADS
201 1:      lwarx   r3, 0, r6
202         or      r3, r3, r0
203         stwcx.  r3, 0, r6
204         bne     1b
205         /* order napping_threads update vs testing entry_exit_map */
206         isync
207         li      r12, 0
208         lwz     r7, VCORE_ENTRY_EXIT(r5)
209         cmpwi   r7, 0x100
210         bge     kvm_novcpu_exit /* another thread already exiting */
211         li      r3, NAPPING_NOVCPU
212         stb     r3, HSTATE_NAPPING(r13)
213
214         li      r3, 0           /* Don't wake on privileged (OS) doorbell */
215         b       kvm_do_nap
216
217 kvm_novcpu_wakeup:
218         ld      r1, HSTATE_HOST_R1(r13)
219         ld      r5, HSTATE_KVM_VCORE(r13)
220         li      r0, 0
221         stb     r0, HSTATE_NAPPING(r13)
222
223         /* check the wake reason */
224         bl      kvmppc_check_wake_reason
225
226         /* see if any other thread is already exiting */
227         lwz     r0, VCORE_ENTRY_EXIT(r5)
228         cmpwi   r0, 0x100
229         bge     kvm_novcpu_exit
230
231         /* clear our bit in napping_threads */
232         lbz     r7, HSTATE_PTID(r13)
233         li      r0, 1
234         sld     r0, r0, r7
235         addi    r6, r5, VCORE_NAPPING_THREADS
236 4:      lwarx   r7, 0, r6
237         andc    r7, r7, r0
238         stwcx.  r7, 0, r6
239         bne     4b
240
241         /* See if the wake reason means we need to exit */
242         cmpdi   r3, 0
243         bge     kvm_novcpu_exit
244
245         /* See if our timeslice has expired (HDEC is negative) */
246         mfspr   r0, SPRN_HDEC
247         li      r12, BOOK3S_INTERRUPT_HV_DECREMENTER
248         cmpwi   r0, 0
249         blt     kvm_novcpu_exit
250
251         /* Got an IPI but other vcpus aren't yet exiting, must be a latecomer */
252         ld      r4, HSTATE_KVM_VCPU(r13)
253         cmpdi   r4, 0
254         beq     kvmppc_primary_no_guest
255
256 #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
257         addi    r3, r4, VCPU_TB_RMENTRY
258         bl      kvmhv_start_timing
259 #endif
260         b       kvmppc_got_guest
261
262 kvm_novcpu_exit:
263 #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
264         ld      r4, HSTATE_KVM_VCPU(r13)
265         cmpdi   r4, 0
266         beq     13f
267         addi    r3, r4, VCPU_TB_RMEXIT
268         bl      kvmhv_accumulate_time
269 #endif
270 13:     mr      r3, r12
271         stw     r12, 112-4(r1)
272         bl      kvmhv_commence_exit
273         nop
274         lwz     r12, 112-4(r1)
275         b       kvmhv_switch_to_host
276
277 /*
278  * We come in here when wakened from nap mode.
279  * Relocation is off and most register values are lost.
280  * r13 points to the PACA.
281  */
282         .globl  kvm_start_guest
283 kvm_start_guest:
284
285         /* Set runlatch bit the minute you wake up from nap */
286         mfspr   r0, SPRN_CTRLF
287         ori     r0, r0, 1
288         mtspr   SPRN_CTRLT, r0
289
290         ld      r2,PACATOC(r13)
291
292         li      r0,KVM_HWTHREAD_IN_KVM
293         stb     r0,HSTATE_HWTHREAD_STATE(r13)
294
295         /* NV GPR values from power7_idle() will no longer be valid */
296         li      r0,1
297         stb     r0,PACA_NAPSTATELOST(r13)
298
299         /* were we napping due to cede? */
300         lbz     r0,HSTATE_NAPPING(r13)
301         cmpwi   r0,NAPPING_CEDE
302         beq     kvm_end_cede
303         cmpwi   r0,NAPPING_NOVCPU
304         beq     kvm_novcpu_wakeup
305
306         ld      r1,PACAEMERGSP(r13)
307         subi    r1,r1,STACK_FRAME_OVERHEAD
308
309         /*
310          * We weren't napping due to cede, so this must be a secondary
311          * thread being woken up to run a guest, or being woken up due
312          * to a stray IPI.  (Or due to some machine check or hypervisor
313          * maintenance interrupt while the core is in KVM.)
314          */
315
316         /* Check the wake reason in SRR1 to see why we got here */
317         bl      kvmppc_check_wake_reason
318         cmpdi   r3, 0
319         bge     kvm_no_guest
320
321         /* get vcore pointer, NULL if we have nothing to run */
322         ld      r5,HSTATE_KVM_VCORE(r13)
323         cmpdi   r5,0
324         /* if we have no vcore to run, go back to sleep */
325         beq     kvm_no_guest
326
327 kvm_secondary_got_guest:
328
329         /* Set HSTATE_DSCR(r13) to something sensible */
330         ld      r6, PACA_DSCR_DEFAULT(r13)
331         std     r6, HSTATE_DSCR(r13)
332
333         /* On thread 0 of a subcore, set HDEC to max */
334         lbz     r4, HSTATE_PTID(r13)
335         cmpwi   r4, 0
336         bne     63f
337         lis     r6, 0x7fff
338         ori     r6, r6, 0xffff
339         mtspr   SPRN_HDEC, r6
340         /* and set per-LPAR registers, if doing dynamic micro-threading */
341         ld      r6, HSTATE_SPLIT_MODE(r13)
342         cmpdi   r6, 0
343         beq     63f
344         ld      r0, KVM_SPLIT_RPR(r6)
345         mtspr   SPRN_RPR, r0
346         ld      r0, KVM_SPLIT_PMMAR(r6)
347         mtspr   SPRN_PMMAR, r0
348         ld      r0, KVM_SPLIT_LDBAR(r6)
349         mtspr   SPRN_LDBAR, r0
350         isync
351 63:
352         /* Order load of vcpu after load of vcore */
353         lwsync
354         ld      r4, HSTATE_KVM_VCPU(r13)
355         bl      kvmppc_hv_entry
356
357         /* Back from the guest, go back to nap */
358         /* Clear our vcpu and vcore pointers so we don't come back in early */
359         li      r0, 0
360         std     r0, HSTATE_KVM_VCPU(r13)
361         /*
362          * Once we clear HSTATE_KVM_VCORE(r13), the code in
363          * kvmppc_run_core() is going to assume that all our vcpu
364          * state is visible in memory.  This lwsync makes sure
365          * that that is true.
366          */
367         lwsync
368         std     r0, HSTATE_KVM_VCORE(r13)
369
370 /*
371  * At this point we have finished executing in the guest.
372  * We need to wait for hwthread_req to become zero, since
373  * we may not turn on the MMU while hwthread_req is non-zero.
374  * While waiting we also need to check if we get given a vcpu to run.
375  */
376 kvm_no_guest:
377         lbz     r3, HSTATE_HWTHREAD_REQ(r13)
378         cmpwi   r3, 0
379         bne     53f
380         HMT_MEDIUM
381         li      r0, KVM_HWTHREAD_IN_KERNEL
382         stb     r0, HSTATE_HWTHREAD_STATE(r13)
383         /* need to recheck hwthread_req after a barrier, to avoid race */
384         sync
385         lbz     r3, HSTATE_HWTHREAD_REQ(r13)
386         cmpwi   r3, 0
387         bne     54f
388 /*
389  * We jump to power7_wakeup_loss, which will return to the caller
390  * of power7_nap in the powernv cpu offline loop.  The value we
391  * put in r3 becomes the return value for power7_nap.
392  */
393         li      r3, LPCR_PECE0
394         mfspr   r4, SPRN_LPCR
395         rlwimi  r4, r3, 0, LPCR_PECE0 | LPCR_PECE1
396         mtspr   SPRN_LPCR, r4
397         li      r3, 0
398         b       power7_wakeup_loss
399
400 53:     HMT_LOW
401         ld      r5, HSTATE_KVM_VCORE(r13)
402         cmpdi   r5, 0
403         bne     60f
404         ld      r3, HSTATE_SPLIT_MODE(r13)
405         cmpdi   r3, 0
406         beq     kvm_no_guest
407         lbz     r0, KVM_SPLIT_DO_NAP(r3)
408         cmpwi   r0, 0
409         beq     kvm_no_guest
410         HMT_MEDIUM
411         b       kvm_unsplit_nap
412 60:     HMT_MEDIUM
413         b       kvm_secondary_got_guest
414
415 54:     li      r0, KVM_HWTHREAD_IN_KVM
416         stb     r0, HSTATE_HWTHREAD_STATE(r13)
417         b       kvm_no_guest
418
419 /*
420  * Here the primary thread is trying to return the core to
421  * whole-core mode, so we need to nap.
422  */
423 kvm_unsplit_nap:
424         /* clear any pending message */
425 BEGIN_FTR_SECTION
426         lis     r6, (PPC_DBELL_SERVER << (63-36))@h
427         PPC_MSGCLR(6)
428 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
429         /* Set kvm_split_mode.napped[tid] = 1 */
430         ld      r3, HSTATE_SPLIT_MODE(r13)
431         li      r0, 1
432         lhz     r4, PACAPACAINDEX(r13)
433         clrldi  r4, r4, 61      /* micro-threading => P8 => 8 threads/core */
434         addi    r4, r4, KVM_SPLIT_NAPPED
435         stbx    r0, r3, r4
436         /* Check the do_nap flag again after setting napped[] */
437         sync
438         lbz     r0, KVM_SPLIT_DO_NAP(r3)
439         cmpwi   r0, 0
440         beq     57f
441         li      r3, (LPCR_PECEDH | LPCR_PECE0) >> 4
442         mfspr   r4, SPRN_LPCR
443         rlwimi  r4, r3, 4, (LPCR_PECEDP | LPCR_PECEDH | LPCR_PECE0 | LPCR_PECE1)
444         mtspr   SPRN_LPCR, r4
445         isync
446         std     r0, HSTATE_SCRATCH0(r13)
447         ptesync
448         ld      r0, HSTATE_SCRATCH0(r13)
449 1:      cmpd    r0, r0
450         bne     1b
451         nap
452         b       .
453
454 57:     li      r0, 0
455         stbx    r0, r3, r4
456         b       kvm_no_guest
457
458 /******************************************************************************
459  *                                                                            *
460  *                               Entry code                                   *
461  *                                                                            *
462  *****************************************************************************/
463
464 .global kvmppc_hv_entry
465 kvmppc_hv_entry:
466
467         /* Required state:
468          *
469          * R4 = vcpu pointer (or NULL)
470          * MSR = ~IR|DR
471          * R13 = PACA
472          * R1 = host R1
473          * R2 = TOC
474          * all other volatile GPRS = free
475          */
476         mflr    r0
477         std     r0, PPC_LR_STKOFF(r1)
478         stdu    r1, -112(r1)
479
480         /* Save R1 in the PACA */
481         std     r1, HSTATE_HOST_R1(r13)
482
483         li      r6, KVM_GUEST_MODE_HOST_HV
484         stb     r6, HSTATE_IN_GUEST(r13)
485
486 #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
487         /* Store initial timestamp */
488         cmpdi   r4, 0
489         beq     1f
490         addi    r3, r4, VCPU_TB_RMENTRY
491         bl      kvmhv_start_timing
492 1:
493 #endif
494         /* Clear out SLB */
495         li      r6,0
496         slbmte  r6,r6
497         slbia
498         ptesync
499
500         /*
501          * POWER7/POWER8 host -> guest partition switch code.
502          * We don't have to lock against concurrent tlbies,
503          * but we do have to coordinate across hardware threads.
504          */
505         /* Set bit in entry map iff exit map is zero. */
506         ld      r5, HSTATE_KVM_VCORE(r13)
507         li      r7, 1
508         lbz     r6, HSTATE_PTID(r13)
509         sld     r7, r7, r6
510         addi    r9, r5, VCORE_ENTRY_EXIT
511 21:     lwarx   r3, 0, r9
512         cmpwi   r3, 0x100               /* any threads starting to exit? */
513         bge     secondary_too_late      /* if so we're too late to the party */
514         or      r3, r3, r7
515         stwcx.  r3, 0, r9
516         bne     21b
517
518         /* Primary thread switches to guest partition. */
519         ld      r9,VCORE_KVM(r5)        /* pointer to struct kvm */
520         cmpwi   r6,0
521         bne     10f
522         ld      r6,KVM_SDR1(r9)
523         lwz     r7,KVM_LPID(r9)
524         li      r0,LPID_RSVD            /* switch to reserved LPID */
525         mtspr   SPRN_LPID,r0
526         ptesync
527         mtspr   SPRN_SDR1,r6            /* switch to partition page table */
528         mtspr   SPRN_LPID,r7
529         isync
530
531         /* See if we need to flush the TLB */
532         lhz     r6,PACAPACAINDEX(r13)   /* test_bit(cpu, need_tlb_flush) */
533         clrldi  r7,r6,64-6              /* extract bit number (6 bits) */
534         srdi    r6,r6,6                 /* doubleword number */
535         sldi    r6,r6,3                 /* address offset */
536         add     r6,r6,r9
537         addi    r6,r6,KVM_NEED_FLUSH    /* dword in kvm->arch.need_tlb_flush */
538         li      r0,1
539         sld     r0,r0,r7
540         ld      r7,0(r6)
541         and.    r7,r7,r0
542         beq     22f
543 23:     ldarx   r7,0,r6                 /* if set, clear the bit */
544         andc    r7,r7,r0
545         stdcx.  r7,0,r6
546         bne     23b
547         /* Flush the TLB of any entries for this LPID */
548         /* use arch 2.07S as a proxy for POWER8 */
549 BEGIN_FTR_SECTION
550         li      r6,512                  /* POWER8 has 512 sets */
551 FTR_SECTION_ELSE
552         li      r6,128                  /* POWER7 has 128 sets */
553 ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_207S)
554         mtctr   r6
555         li      r7,0x800                /* IS field = 0b10 */
556         ptesync
557 28:     tlbiel  r7
558         addi    r7,r7,0x1000
559         bdnz    28b
560         ptesync
561
562         /* Add timebase offset onto timebase */
563 22:     ld      r8,VCORE_TB_OFFSET(r5)
564         cmpdi   r8,0
565         beq     37f
566         mftb    r6              /* current host timebase */
567         add     r8,r8,r6
568         mtspr   SPRN_TBU40,r8   /* update upper 40 bits */
569         mftb    r7              /* check if lower 24 bits overflowed */
570         clrldi  r6,r6,40
571         clrldi  r7,r7,40
572         cmpld   r7,r6
573         bge     37f
574         addis   r8,r8,0x100     /* if so, increment upper 40 bits */
575         mtspr   SPRN_TBU40,r8
576
577         /* Load guest PCR value to select appropriate compat mode */
578 37:     ld      r7, VCORE_PCR(r5)
579         cmpdi   r7, 0
580         beq     38f
581         mtspr   SPRN_PCR, r7
582 38:
583
584 BEGIN_FTR_SECTION
585         /* DPDES is shared between threads */
586         ld      r8, VCORE_DPDES(r5)
587         mtspr   SPRN_DPDES, r8
588 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
589
590         li      r0,1
591         stb     r0,VCORE_IN_GUEST(r5)   /* signal secondaries to continue */
592
593         /* Do we have a guest vcpu to run? */
594 10:     cmpdi   r4, 0
595         beq     kvmppc_primary_no_guest
596 kvmppc_got_guest:
597
598         /* Load up guest SLB entries */
599         lwz     r5,VCPU_SLB_MAX(r4)
600         cmpwi   r5,0
601         beq     9f
602         mtctr   r5
603         addi    r6,r4,VCPU_SLB
604 1:      ld      r8,VCPU_SLB_E(r6)
605         ld      r9,VCPU_SLB_V(r6)
606         slbmte  r9,r8
607         addi    r6,r6,VCPU_SLB_SIZE
608         bdnz    1b
609 9:
610         /* Increment yield count if they have a VPA */
611         ld      r3, VCPU_VPA(r4)
612         cmpdi   r3, 0
613         beq     25f
614         li      r6, LPPACA_YIELDCOUNT
615         LWZX_BE r5, r3, r6
616         addi    r5, r5, 1
617         STWX_BE r5, r3, r6
618         li      r6, 1
619         stb     r6, VCPU_VPA_DIRTY(r4)
620 25:
621
622         /* Save purr/spurr */
623         mfspr   r5,SPRN_PURR
624         mfspr   r6,SPRN_SPURR
625         std     r5,HSTATE_PURR(r13)
626         std     r6,HSTATE_SPURR(r13)
627         ld      r7,VCPU_PURR(r4)
628         ld      r8,VCPU_SPURR(r4)
629         mtspr   SPRN_PURR,r7
630         mtspr   SPRN_SPURR,r8
631
632 BEGIN_FTR_SECTION
633         /* Set partition DABR */
634         /* Do this before re-enabling PMU to avoid P7 DABR corruption bug */
635         lwz     r5,VCPU_DABRX(r4)
636         ld      r6,VCPU_DABR(r4)
637         mtspr   SPRN_DABRX,r5
638         mtspr   SPRN_DABR,r6
639         isync
640 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
641
642 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
643 BEGIN_FTR_SECTION
644         b       skip_tm
645 END_FTR_SECTION_IFCLR(CPU_FTR_TM)
646
647         /* Turn on TM/FP/VSX/VMX so we can restore them. */
648         mfmsr   r5
649         li      r6, MSR_TM >> 32
650         sldi    r6, r6, 32
651         or      r5, r5, r6
652         ori     r5, r5, MSR_FP
653         oris    r5, r5, (MSR_VEC | MSR_VSX)@h
654         mtmsrd  r5
655
656         /*
657          * The user may change these outside of a transaction, so they must
658          * always be context switched.
659          */
660         ld      r5, VCPU_TFHAR(r4)
661         ld      r6, VCPU_TFIAR(r4)
662         ld      r7, VCPU_TEXASR(r4)
663         mtspr   SPRN_TFHAR, r5
664         mtspr   SPRN_TFIAR, r6
665         mtspr   SPRN_TEXASR, r7
666
667         ld      r5, VCPU_MSR(r4)
668         rldicl. r5, r5, 64 - MSR_TS_S_LG, 62
669         beq     skip_tm /* TM not active in guest */
670
671         /* Make sure the failure summary is set, otherwise we'll program check
672          * when we trechkpt.  It's possible that this might have been not set
673          * on a kvmppc_set_one_reg() call but we shouldn't let this crash the
674          * host.
675          */
676         oris    r7, r7, (TEXASR_FS)@h
677         mtspr   SPRN_TEXASR, r7
678
679         /*
680          * We need to load up the checkpointed state for the guest.
681          * We need to do this early as it will blow away any GPRs, VSRs and
682          * some SPRs.
683          */
684
685         mr      r31, r4
686         addi    r3, r31, VCPU_FPRS_TM
687         bl      load_fp_state
688         addi    r3, r31, VCPU_VRS_TM
689         bl      load_vr_state
690         mr      r4, r31
691         lwz     r7, VCPU_VRSAVE_TM(r4)
692         mtspr   SPRN_VRSAVE, r7
693
694         ld      r5, VCPU_LR_TM(r4)
695         lwz     r6, VCPU_CR_TM(r4)
696         ld      r7, VCPU_CTR_TM(r4)
697         ld      r8, VCPU_AMR_TM(r4)
698         ld      r9, VCPU_TAR_TM(r4)
699         mtlr    r5
700         mtcr    r6
701         mtctr   r7
702         mtspr   SPRN_AMR, r8
703         mtspr   SPRN_TAR, r9
704
705         /*
706          * Load up PPR and DSCR values but don't put them in the actual SPRs
707          * till the last moment to avoid running with userspace PPR and DSCR for
708          * too long.
709          */
710         ld      r29, VCPU_DSCR_TM(r4)
711         ld      r30, VCPU_PPR_TM(r4)
712
713         std     r2, PACATMSCRATCH(r13) /* Save TOC */
714
715         /* Clear the MSR RI since r1, r13 are all going to be foobar. */
716         li      r5, 0
717         mtmsrd  r5, 1
718
719         /* Load GPRs r0-r28 */
720         reg = 0
721         .rept   29
722         ld      reg, VCPU_GPRS_TM(reg)(r31)
723         reg = reg + 1
724         .endr
725
726         mtspr   SPRN_DSCR, r29
727         mtspr   SPRN_PPR, r30
728
729         /* Load final GPRs */
730         ld      29, VCPU_GPRS_TM(29)(r31)
731         ld      30, VCPU_GPRS_TM(30)(r31)
732         ld      31, VCPU_GPRS_TM(31)(r31)
733
734         /* TM checkpointed state is now setup.  All GPRs are now volatile. */
735         TRECHKPT
736
737         /* Now let's get back the state we need. */
738         HMT_MEDIUM
739         GET_PACA(r13)
740         ld      r29, HSTATE_DSCR(r13)
741         mtspr   SPRN_DSCR, r29
742         ld      r4, HSTATE_KVM_VCPU(r13)
743         ld      r1, HSTATE_HOST_R1(r13)
744         ld      r2, PACATMSCRATCH(r13)
745
746         /* Set the MSR RI since we have our registers back. */
747         li      r5, MSR_RI
748         mtmsrd  r5, 1
749 skip_tm:
750 #endif
751
752         /* Load guest PMU registers */
753         /* R4 is live here (vcpu pointer) */
754         li      r3, 1
755         sldi    r3, r3, 31              /* MMCR0_FC (freeze counters) bit */
756         mtspr   SPRN_MMCR0, r3          /* freeze all counters, disable ints */
757         isync
758 BEGIN_FTR_SECTION
759         ld      r3, VCPU_MMCR(r4)
760         andi.   r5, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO
761         cmpwi   r5, MMCR0_PMAO
762         beql    kvmppc_fix_pmao
763 END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG)
764         lwz     r3, VCPU_PMC(r4)        /* always load up guest PMU registers */
765         lwz     r5, VCPU_PMC + 4(r4)    /* to prevent information leak */
766         lwz     r6, VCPU_PMC + 8(r4)
767         lwz     r7, VCPU_PMC + 12(r4)
768         lwz     r8, VCPU_PMC + 16(r4)
769         lwz     r9, VCPU_PMC + 20(r4)
770         mtspr   SPRN_PMC1, r3
771         mtspr   SPRN_PMC2, r5
772         mtspr   SPRN_PMC3, r6
773         mtspr   SPRN_PMC4, r7
774         mtspr   SPRN_PMC5, r8
775         mtspr   SPRN_PMC6, r9
776         ld      r3, VCPU_MMCR(r4)
777         ld      r5, VCPU_MMCR + 8(r4)
778         ld      r6, VCPU_MMCR + 16(r4)
779         ld      r7, VCPU_SIAR(r4)
780         ld      r8, VCPU_SDAR(r4)
781         mtspr   SPRN_MMCR1, r5
782         mtspr   SPRN_MMCRA, r6
783         mtspr   SPRN_SIAR, r7
784         mtspr   SPRN_SDAR, r8
785 BEGIN_FTR_SECTION
786         ld      r5, VCPU_MMCR + 24(r4)
787         ld      r6, VCPU_SIER(r4)
788         lwz     r7, VCPU_PMC + 24(r4)
789         lwz     r8, VCPU_PMC + 28(r4)
790         ld      r9, VCPU_MMCR + 32(r4)
791         mtspr   SPRN_MMCR2, r5
792         mtspr   SPRN_SIER, r6
793         mtspr   SPRN_SPMC1, r7
794         mtspr   SPRN_SPMC2, r8
795         mtspr   SPRN_MMCRS, r9
796 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
797         mtspr   SPRN_MMCR0, r3
798         isync
799
800         /* Load up FP, VMX and VSX registers */
801         bl      kvmppc_load_fp
802
803         ld      r14, VCPU_GPR(R14)(r4)
804         ld      r15, VCPU_GPR(R15)(r4)
805         ld      r16, VCPU_GPR(R16)(r4)
806         ld      r17, VCPU_GPR(R17)(r4)
807         ld      r18, VCPU_GPR(R18)(r4)
808         ld      r19, VCPU_GPR(R19)(r4)
809         ld      r20, VCPU_GPR(R20)(r4)
810         ld      r21, VCPU_GPR(R21)(r4)
811         ld      r22, VCPU_GPR(R22)(r4)
812         ld      r23, VCPU_GPR(R23)(r4)
813         ld      r24, VCPU_GPR(R24)(r4)
814         ld      r25, VCPU_GPR(R25)(r4)
815         ld      r26, VCPU_GPR(R26)(r4)
816         ld      r27, VCPU_GPR(R27)(r4)
817         ld      r28, VCPU_GPR(R28)(r4)
818         ld      r29, VCPU_GPR(R29)(r4)
819         ld      r30, VCPU_GPR(R30)(r4)
820         ld      r31, VCPU_GPR(R31)(r4)
821
822         /* Switch DSCR to guest value */
823         ld      r5, VCPU_DSCR(r4)
824         mtspr   SPRN_DSCR, r5
825
826 BEGIN_FTR_SECTION
827         /* Skip next section on POWER7 */
828         b       8f
829 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
830         /* Turn on TM so we can access TFHAR/TFIAR/TEXASR */
831         mfmsr   r8
832         li      r0, 1
833         rldimi  r8, r0, MSR_TM_LG, 63-MSR_TM_LG
834         mtmsrd  r8
835
836         /* Load up POWER8-specific registers */
837         ld      r5, VCPU_IAMR(r4)
838         lwz     r6, VCPU_PSPB(r4)
839         ld      r7, VCPU_FSCR(r4)
840         mtspr   SPRN_IAMR, r5
841         mtspr   SPRN_PSPB, r6
842         mtspr   SPRN_FSCR, r7
843         ld      r5, VCPU_DAWR(r4)
844         ld      r6, VCPU_DAWRX(r4)
845         ld      r7, VCPU_CIABR(r4)
846         ld      r8, VCPU_TAR(r4)
847         mtspr   SPRN_DAWR, r5
848         mtspr   SPRN_DAWRX, r6
849         mtspr   SPRN_CIABR, r7
850         mtspr   SPRN_TAR, r8
851         ld      r5, VCPU_IC(r4)
852         ld      r6, VCPU_VTB(r4)
853         mtspr   SPRN_IC, r5
854         mtspr   SPRN_VTB, r6
855         ld      r8, VCPU_EBBHR(r4)
856         mtspr   SPRN_EBBHR, r8
857         ld      r5, VCPU_EBBRR(r4)
858         ld      r6, VCPU_BESCR(r4)
859         ld      r7, VCPU_CSIGR(r4)
860         ld      r8, VCPU_TACR(r4)
861         mtspr   SPRN_EBBRR, r5
862         mtspr   SPRN_BESCR, r6
863         mtspr   SPRN_CSIGR, r7
864         mtspr   SPRN_TACR, r8
865         ld      r5, VCPU_TCSCR(r4)
866         ld      r6, VCPU_ACOP(r4)
867         lwz     r7, VCPU_GUEST_PID(r4)
868         ld      r8, VCPU_WORT(r4)
869         mtspr   SPRN_TCSCR, r5
870         mtspr   SPRN_ACOP, r6
871         mtspr   SPRN_PID, r7
872         mtspr   SPRN_WORT, r8
873 8:
874
875         /*
876          * Set the decrementer to the guest decrementer.
877          */
878         ld      r8,VCPU_DEC_EXPIRES(r4)
879         /* r8 is a host timebase value here, convert to guest TB */
880         ld      r5,HSTATE_KVM_VCORE(r13)
881         ld      r6,VCORE_TB_OFFSET(r5)
882         add     r8,r8,r6
883         mftb    r7
884         subf    r3,r7,r8
885         mtspr   SPRN_DEC,r3
886         stw     r3,VCPU_DEC(r4)
887
888         ld      r5, VCPU_SPRG0(r4)
889         ld      r6, VCPU_SPRG1(r4)
890         ld      r7, VCPU_SPRG2(r4)
891         ld      r8, VCPU_SPRG3(r4)
892         mtspr   SPRN_SPRG0, r5
893         mtspr   SPRN_SPRG1, r6
894         mtspr   SPRN_SPRG2, r7
895         mtspr   SPRN_SPRG3, r8
896
897         /* Load up DAR and DSISR */
898         ld      r5, VCPU_DAR(r4)
899         lwz     r6, VCPU_DSISR(r4)
900         mtspr   SPRN_DAR, r5
901         mtspr   SPRN_DSISR, r6
902
903         /* Restore AMR and UAMOR, set AMOR to all 1s */
904         ld      r5,VCPU_AMR(r4)
905         ld      r6,VCPU_UAMOR(r4)
906         li      r7,-1
907         mtspr   SPRN_AMR,r5
908         mtspr   SPRN_UAMOR,r6
909         mtspr   SPRN_AMOR,r7
910
911         /* Restore state of CTRL run bit; assume 1 on entry */
912         lwz     r5,VCPU_CTRL(r4)
913         andi.   r5,r5,1
914         bne     4f
915         mfspr   r6,SPRN_CTRLF
916         clrrdi  r6,r6,1
917         mtspr   SPRN_CTRLT,r6
918 4:
919         /* Secondary threads wait for primary to have done partition switch */
920         ld      r5, HSTATE_KVM_VCORE(r13)
921         lbz     r6, HSTATE_PTID(r13)
922         cmpwi   r6, 0
923         beq     21f
924         lbz     r0, VCORE_IN_GUEST(r5)
925         cmpwi   r0, 0
926         bne     21f
927         HMT_LOW
928 20:     lwz     r3, VCORE_ENTRY_EXIT(r5)
929         cmpwi   r3, 0x100
930         bge     no_switch_exit
931         lbz     r0, VCORE_IN_GUEST(r5)
932         cmpwi   r0, 0
933         beq     20b
934         HMT_MEDIUM
935 21:
936         /* Set LPCR. */
937         ld      r8,VCORE_LPCR(r5)
938         mtspr   SPRN_LPCR,r8
939         isync
940
941         /* Check if HDEC expires soon */
942         mfspr   r3, SPRN_HDEC
943         cmpwi   r3, 512         /* 1 microsecond */
944         blt     hdec_soon
945
946         ld      r6, VCPU_CTR(r4)
947         lwz     r7, VCPU_XER(r4)
948
949         mtctr   r6
950         mtxer   r7
951
952 kvmppc_cede_reentry:            /* r4 = vcpu, r13 = paca */
953         ld      r10, VCPU_PC(r4)
954         ld      r11, VCPU_MSR(r4)
955         ld      r6, VCPU_SRR0(r4)
956         ld      r7, VCPU_SRR1(r4)
957         mtspr   SPRN_SRR0, r6
958         mtspr   SPRN_SRR1, r7
959
960 deliver_guest_interrupt:
961         /* r11 = vcpu->arch.msr & ~MSR_HV */
962         rldicl  r11, r11, 63 - MSR_HV_LG, 1
963         rotldi  r11, r11, 1 + MSR_HV_LG
964         ori     r11, r11, MSR_ME
965
966         /* Check if we can deliver an external or decrementer interrupt now */
967         ld      r0, VCPU_PENDING_EXC(r4)
968         rldicl  r0, r0, 64 - BOOK3S_IRQPRIO_EXTERNAL_LEVEL, 63
969         cmpdi   cr1, r0, 0
970         andi.   r8, r11, MSR_EE
971         mfspr   r8, SPRN_LPCR
972         /* Insert EXTERNAL_LEVEL bit into LPCR at the MER bit position */
973         rldimi  r8, r0, LPCR_MER_SH, 63 - LPCR_MER_SH
974         mtspr   SPRN_LPCR, r8
975         isync
976         beq     5f
977         li      r0, BOOK3S_INTERRUPT_EXTERNAL
978         bne     cr1, 12f
979         mfspr   r0, SPRN_DEC
980         cmpwi   r0, 0
981         li      r0, BOOK3S_INTERRUPT_DECREMENTER
982         bge     5f
983
984 12:     mtspr   SPRN_SRR0, r10
985         mr      r10,r0
986         mtspr   SPRN_SRR1, r11
987         mr      r9, r4
988         bl      kvmppc_msr_interrupt
989 5:
990
991 /*
992  * Required state:
993  * R4 = vcpu
994  * R10: value for HSRR0
995  * R11: value for HSRR1
996  * R13 = PACA
997  */
998 fast_guest_return:
999         li      r0,0
1000         stb     r0,VCPU_CEDED(r4)       /* cancel cede */
1001         mtspr   SPRN_HSRR0,r10
1002         mtspr   SPRN_HSRR1,r11
1003
1004         /* Activate guest mode, so faults get handled by KVM */
1005         li      r9, KVM_GUEST_MODE_GUEST_HV
1006         stb     r9, HSTATE_IN_GUEST(r13)
1007
1008 #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1009         /* Accumulate timing */
1010         addi    r3, r4, VCPU_TB_GUEST
1011         bl      kvmhv_accumulate_time
1012 #endif
1013
1014         /* Enter guest */
1015
1016 BEGIN_FTR_SECTION
1017         ld      r5, VCPU_CFAR(r4)
1018         mtspr   SPRN_CFAR, r5
1019 END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
1020 BEGIN_FTR_SECTION
1021         ld      r0, VCPU_PPR(r4)
1022 END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
1023
1024         ld      r5, VCPU_LR(r4)
1025         lwz     r6, VCPU_CR(r4)
1026         mtlr    r5
1027         mtcr    r6
1028
1029         ld      r1, VCPU_GPR(R1)(r4)
1030         ld      r2, VCPU_GPR(R2)(r4)
1031         ld      r3, VCPU_GPR(R3)(r4)
1032         ld      r5, VCPU_GPR(R5)(r4)
1033         ld      r6, VCPU_GPR(R6)(r4)
1034         ld      r7, VCPU_GPR(R7)(r4)
1035         ld      r8, VCPU_GPR(R8)(r4)
1036         ld      r9, VCPU_GPR(R9)(r4)
1037         ld      r10, VCPU_GPR(R10)(r4)
1038         ld      r11, VCPU_GPR(R11)(r4)
1039         ld      r12, VCPU_GPR(R12)(r4)
1040         ld      r13, VCPU_GPR(R13)(r4)
1041
1042 BEGIN_FTR_SECTION
1043         mtspr   SPRN_PPR, r0
1044 END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
1045         ld      r0, VCPU_GPR(R0)(r4)
1046         ld      r4, VCPU_GPR(R4)(r4)
1047
1048         hrfid
1049         b       .
1050
1051 secondary_too_late:
1052         li      r12, 0
1053         cmpdi   r4, 0
1054         beq     11f
1055         stw     r12, VCPU_TRAP(r4)
1056 #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1057         addi    r3, r4, VCPU_TB_RMEXIT
1058         bl      kvmhv_accumulate_time
1059 #endif
1060 11:     b       kvmhv_switch_to_host
1061
1062 no_switch_exit:
1063         HMT_MEDIUM
1064         li      r12, 0
1065         b       12f
1066 hdec_soon:
1067         li      r12, BOOK3S_INTERRUPT_HV_DECREMENTER
1068 12:     stw     r12, VCPU_TRAP(r4)
1069         mr      r9, r4
1070 #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1071         addi    r3, r4, VCPU_TB_RMEXIT
1072         bl      kvmhv_accumulate_time
1073 #endif
1074         b       guest_exit_cont
1075
1076 /******************************************************************************
1077  *                                                                            *
1078  *                               Exit code                                    *
1079  *                                                                            *
1080  *****************************************************************************/
1081
1082 /*
1083  * We come here from the first-level interrupt handlers.
1084  */
1085         .globl  kvmppc_interrupt_hv
1086 kvmppc_interrupt_hv:
1087         /*
1088          * Register contents:
1089          * R12          = interrupt vector
1090          * R13          = PACA
1091          * guest CR, R12 saved in shadow VCPU SCRATCH1/0
1092          * guest R13 saved in SPRN_SCRATCH0
1093          */
1094         std     r9, HSTATE_SCRATCH2(r13)
1095
1096         lbz     r9, HSTATE_IN_GUEST(r13)
1097         cmpwi   r9, KVM_GUEST_MODE_HOST_HV
1098         beq     kvmppc_bad_host_intr
1099 #ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1100         cmpwi   r9, KVM_GUEST_MODE_GUEST
1101         ld      r9, HSTATE_SCRATCH2(r13)
1102         beq     kvmppc_interrupt_pr
1103 #endif
1104         /* We're now back in the host but in guest MMU context */
1105         li      r9, KVM_GUEST_MODE_HOST_HV
1106         stb     r9, HSTATE_IN_GUEST(r13)
1107
1108         ld      r9, HSTATE_KVM_VCPU(r13)
1109
1110         /* Save registers */
1111
1112         std     r0, VCPU_GPR(R0)(r9)
1113         std     r1, VCPU_GPR(R1)(r9)
1114         std     r2, VCPU_GPR(R2)(r9)
1115         std     r3, VCPU_GPR(R3)(r9)
1116         std     r4, VCPU_GPR(R4)(r9)
1117         std     r5, VCPU_GPR(R5)(r9)
1118         std     r6, VCPU_GPR(R6)(r9)
1119         std     r7, VCPU_GPR(R7)(r9)
1120         std     r8, VCPU_GPR(R8)(r9)
1121         ld      r0, HSTATE_SCRATCH2(r13)
1122         std     r0, VCPU_GPR(R9)(r9)
1123         std     r10, VCPU_GPR(R10)(r9)
1124         std     r11, VCPU_GPR(R11)(r9)
1125         ld      r3, HSTATE_SCRATCH0(r13)
1126         lwz     r4, HSTATE_SCRATCH1(r13)
1127         std     r3, VCPU_GPR(R12)(r9)
1128         stw     r4, VCPU_CR(r9)
1129 BEGIN_FTR_SECTION
1130         ld      r3, HSTATE_CFAR(r13)
1131         std     r3, VCPU_CFAR(r9)
1132 END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
1133 BEGIN_FTR_SECTION
1134         ld      r4, HSTATE_PPR(r13)
1135         std     r4, VCPU_PPR(r9)
1136 END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
1137
1138         /* Restore R1/R2 so we can handle faults */
1139         ld      r1, HSTATE_HOST_R1(r13)
1140         ld      r2, PACATOC(r13)
1141
1142         mfspr   r10, SPRN_SRR0
1143         mfspr   r11, SPRN_SRR1
1144         std     r10, VCPU_SRR0(r9)
1145         std     r11, VCPU_SRR1(r9)
1146         andi.   r0, r12, 2              /* need to read HSRR0/1? */
1147         beq     1f
1148         mfspr   r10, SPRN_HSRR0
1149         mfspr   r11, SPRN_HSRR1
1150         clrrdi  r12, r12, 2
1151 1:      std     r10, VCPU_PC(r9)
1152         std     r11, VCPU_MSR(r9)
1153
1154         GET_SCRATCH0(r3)
1155         mflr    r4
1156         std     r3, VCPU_GPR(R13)(r9)
1157         std     r4, VCPU_LR(r9)
1158
1159         stw     r12,VCPU_TRAP(r9)
1160
1161 #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1162         addi    r3, r9, VCPU_TB_RMINTR
1163         mr      r4, r9
1164         bl      kvmhv_accumulate_time
1165         ld      r5, VCPU_GPR(R5)(r9)
1166         ld      r6, VCPU_GPR(R6)(r9)
1167         ld      r7, VCPU_GPR(R7)(r9)
1168         ld      r8, VCPU_GPR(R8)(r9)
1169 #endif
1170
1171         /* Save HEIR (HV emulation assist reg) in emul_inst
1172            if this is an HEI (HV emulation interrupt, e40) */
1173         li      r3,KVM_INST_FETCH_FAILED
1174         stw     r3,VCPU_LAST_INST(r9)
1175         cmpwi   r12,BOOK3S_INTERRUPT_H_EMUL_ASSIST
1176         bne     11f
1177         mfspr   r3,SPRN_HEIR
1178 11:     stw     r3,VCPU_HEIR(r9)
1179
1180         /* these are volatile across C function calls */
1181         mfctr   r3
1182         mfxer   r4
1183         std     r3, VCPU_CTR(r9)
1184         stw     r4, VCPU_XER(r9)
1185
1186         /* If this is a page table miss then see if it's theirs or ours */
1187         cmpwi   r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1188         beq     kvmppc_hdsi
1189         cmpwi   r12, BOOK3S_INTERRUPT_H_INST_STORAGE
1190         beq     kvmppc_hisi
1191
1192         /* See if this is a leftover HDEC interrupt */
1193         cmpwi   r12,BOOK3S_INTERRUPT_HV_DECREMENTER
1194         bne     2f
1195         mfspr   r3,SPRN_HDEC
1196         cmpwi   r3,0
1197         mr      r4,r9
1198         bge     fast_guest_return
1199 2:
1200         /* See if this is an hcall we can handle in real mode */
1201         cmpwi   r12,BOOK3S_INTERRUPT_SYSCALL
1202         beq     hcall_try_real_mode
1203
1204         /* Hypervisor doorbell - exit only if host IPI flag set */
1205         cmpwi   r12, BOOK3S_INTERRUPT_H_DOORBELL
1206         bne     3f
1207         lbz     r0, HSTATE_HOST_IPI(r13)
1208         beq     4f
1209         b       guest_exit_cont
1210 3:
1211         /* External interrupt ? */
1212         cmpwi   r12, BOOK3S_INTERRUPT_EXTERNAL
1213         bne+    guest_exit_cont
1214
1215         /* External interrupt, first check for host_ipi. If this is
1216          * set, we know the host wants us out so let's do it now
1217          */
1218         bl      kvmppc_read_intr
1219         cmpdi   r3, 0
1220         bgt     guest_exit_cont
1221
1222         /* Check if any CPU is heading out to the host, if so head out too */
1223 4:      ld      r5, HSTATE_KVM_VCORE(r13)
1224         lwz     r0, VCORE_ENTRY_EXIT(r5)
1225         cmpwi   r0, 0x100
1226         mr      r4, r9
1227         blt     deliver_guest_interrupt
1228
1229 guest_exit_cont:                /* r9 = vcpu, r12 = trap, r13 = paca */
1230         /* Save more register state  */
1231         mfdar   r6
1232         mfdsisr r7
1233         std     r6, VCPU_DAR(r9)
1234         stw     r7, VCPU_DSISR(r9)
1235         /* don't overwrite fault_dar/fault_dsisr if HDSI */
1236         cmpwi   r12,BOOK3S_INTERRUPT_H_DATA_STORAGE
1237         beq     mc_cont
1238         std     r6, VCPU_FAULT_DAR(r9)
1239         stw     r7, VCPU_FAULT_DSISR(r9)
1240
1241         /* See if it is a machine check */
1242         cmpwi   r12, BOOK3S_INTERRUPT_MACHINE_CHECK
1243         beq     machine_check_realmode
1244 mc_cont:
1245 #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1246         addi    r3, r9, VCPU_TB_RMEXIT
1247         mr      r4, r9
1248         bl      kvmhv_accumulate_time
1249 #endif
1250
1251         /* Increment exit count, poke other threads to exit */
1252         bl      kvmhv_commence_exit
1253         nop
1254         ld      r9, HSTATE_KVM_VCPU(r13)
1255         lwz     r12, VCPU_TRAP(r9)
1256
1257         /* Stop others sending VCPU interrupts to this physical CPU */
1258         li      r0, -1
1259         stw     r0, VCPU_CPU(r9)
1260         stw     r0, VCPU_THREAD_CPU(r9)
1261
1262         /* Save guest CTRL register, set runlatch to 1 */
1263         mfspr   r6,SPRN_CTRLF
1264         stw     r6,VCPU_CTRL(r9)
1265         andi.   r0,r6,1
1266         bne     4f
1267         ori     r6,r6,1
1268         mtspr   SPRN_CTRLT,r6
1269 4:
1270         /* Read the guest SLB and save it away */
1271         lwz     r0,VCPU_SLB_NR(r9)      /* number of entries in SLB */
1272         mtctr   r0
1273         li      r6,0
1274         addi    r7,r9,VCPU_SLB
1275         li      r5,0
1276 1:      slbmfee r8,r6
1277         andis.  r0,r8,SLB_ESID_V@h
1278         beq     2f
1279         add     r8,r8,r6                /* put index in */
1280         slbmfev r3,r6
1281         std     r8,VCPU_SLB_E(r7)
1282         std     r3,VCPU_SLB_V(r7)
1283         addi    r7,r7,VCPU_SLB_SIZE
1284         addi    r5,r5,1
1285 2:      addi    r6,r6,1
1286         bdnz    1b
1287         stw     r5,VCPU_SLB_MAX(r9)
1288
1289         /*
1290          * Save the guest PURR/SPURR
1291          */
1292         mfspr   r5,SPRN_PURR
1293         mfspr   r6,SPRN_SPURR
1294         ld      r7,VCPU_PURR(r9)
1295         ld      r8,VCPU_SPURR(r9)
1296         std     r5,VCPU_PURR(r9)
1297         std     r6,VCPU_SPURR(r9)
1298         subf    r5,r7,r5
1299         subf    r6,r8,r6
1300
1301         /*
1302          * Restore host PURR/SPURR and add guest times
1303          * so that the time in the guest gets accounted.
1304          */
1305         ld      r3,HSTATE_PURR(r13)
1306         ld      r4,HSTATE_SPURR(r13)
1307         add     r3,r3,r5
1308         add     r4,r4,r6
1309         mtspr   SPRN_PURR,r3
1310         mtspr   SPRN_SPURR,r4
1311
1312         /* Save DEC */
1313         mfspr   r5,SPRN_DEC
1314         mftb    r6
1315         extsw   r5,r5
1316         add     r5,r5,r6
1317         /* r5 is a guest timebase value here, convert to host TB */
1318         ld      r3,HSTATE_KVM_VCORE(r13)
1319         ld      r4,VCORE_TB_OFFSET(r3)
1320         subf    r5,r4,r5
1321         std     r5,VCPU_DEC_EXPIRES(r9)
1322
1323 BEGIN_FTR_SECTION
1324         b       8f
1325 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
1326         /* Save POWER8-specific registers */
1327         mfspr   r5, SPRN_IAMR
1328         mfspr   r6, SPRN_PSPB
1329         mfspr   r7, SPRN_FSCR
1330         std     r5, VCPU_IAMR(r9)
1331         stw     r6, VCPU_PSPB(r9)
1332         std     r7, VCPU_FSCR(r9)
1333         mfspr   r5, SPRN_IC
1334         mfspr   r6, SPRN_VTB
1335         mfspr   r7, SPRN_TAR
1336         std     r5, VCPU_IC(r9)
1337         std     r6, VCPU_VTB(r9)
1338         std     r7, VCPU_TAR(r9)
1339         mfspr   r8, SPRN_EBBHR
1340         std     r8, VCPU_EBBHR(r9)
1341         mfspr   r5, SPRN_EBBRR
1342         mfspr   r6, SPRN_BESCR
1343         mfspr   r7, SPRN_CSIGR
1344         mfspr   r8, SPRN_TACR
1345         std     r5, VCPU_EBBRR(r9)
1346         std     r6, VCPU_BESCR(r9)
1347         std     r7, VCPU_CSIGR(r9)
1348         std     r8, VCPU_TACR(r9)
1349         mfspr   r5, SPRN_TCSCR
1350         mfspr   r6, SPRN_ACOP
1351         mfspr   r7, SPRN_PID
1352         mfspr   r8, SPRN_WORT
1353         std     r5, VCPU_TCSCR(r9)
1354         std     r6, VCPU_ACOP(r9)
1355         stw     r7, VCPU_GUEST_PID(r9)
1356         std     r8, VCPU_WORT(r9)
1357 8:
1358
1359         /* Save and reset AMR and UAMOR before turning on the MMU */
1360         mfspr   r5,SPRN_AMR
1361         mfspr   r6,SPRN_UAMOR
1362         std     r5,VCPU_AMR(r9)
1363         std     r6,VCPU_UAMOR(r9)
1364         li      r6,0
1365         mtspr   SPRN_AMR,r6
1366
1367         /* Switch DSCR back to host value */
1368         mfspr   r8, SPRN_DSCR
1369         ld      r7, HSTATE_DSCR(r13)
1370         std     r8, VCPU_DSCR(r9)
1371         mtspr   SPRN_DSCR, r7
1372
1373         /* Save non-volatile GPRs */
1374         std     r14, VCPU_GPR(R14)(r9)
1375         std     r15, VCPU_GPR(R15)(r9)
1376         std     r16, VCPU_GPR(R16)(r9)
1377         std     r17, VCPU_GPR(R17)(r9)
1378         std     r18, VCPU_GPR(R18)(r9)
1379         std     r19, VCPU_GPR(R19)(r9)
1380         std     r20, VCPU_GPR(R20)(r9)
1381         std     r21, VCPU_GPR(R21)(r9)
1382         std     r22, VCPU_GPR(R22)(r9)
1383         std     r23, VCPU_GPR(R23)(r9)
1384         std     r24, VCPU_GPR(R24)(r9)
1385         std     r25, VCPU_GPR(R25)(r9)
1386         std     r26, VCPU_GPR(R26)(r9)
1387         std     r27, VCPU_GPR(R27)(r9)
1388         std     r28, VCPU_GPR(R28)(r9)
1389         std     r29, VCPU_GPR(R29)(r9)
1390         std     r30, VCPU_GPR(R30)(r9)
1391         std     r31, VCPU_GPR(R31)(r9)
1392
1393         /* Save SPRGs */
1394         mfspr   r3, SPRN_SPRG0
1395         mfspr   r4, SPRN_SPRG1
1396         mfspr   r5, SPRN_SPRG2
1397         mfspr   r6, SPRN_SPRG3
1398         std     r3, VCPU_SPRG0(r9)
1399         std     r4, VCPU_SPRG1(r9)
1400         std     r5, VCPU_SPRG2(r9)
1401         std     r6, VCPU_SPRG3(r9)
1402
1403         /* save FP state */
1404         mr      r3, r9
1405         bl      kvmppc_save_fp
1406
1407 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1408 BEGIN_FTR_SECTION
1409         b       2f
1410 END_FTR_SECTION_IFCLR(CPU_FTR_TM)
1411         /* Turn on TM. */
1412         mfmsr   r8
1413         li      r0, 1
1414         rldimi  r8, r0, MSR_TM_LG, 63-MSR_TM_LG
1415         mtmsrd  r8
1416
1417         ld      r5, VCPU_MSR(r9)
1418         rldicl. r5, r5, 64 - MSR_TS_S_LG, 62
1419         beq     1f      /* TM not active in guest. */
1420
1421         li      r3, TM_CAUSE_KVM_RESCHED
1422
1423         /* Clear the MSR RI since r1, r13 are all going to be foobar. */
1424         li      r5, 0
1425         mtmsrd  r5, 1
1426
1427         /* All GPRs are volatile at this point. */
1428         TRECLAIM(R3)
1429
1430         /* Temporarily store r13 and r9 so we have some regs to play with */
1431         SET_SCRATCH0(r13)
1432         GET_PACA(r13)
1433         std     r9, PACATMSCRATCH(r13)
1434         ld      r9, HSTATE_KVM_VCPU(r13)
1435
1436         /* Get a few more GPRs free. */
1437         std     r29, VCPU_GPRS_TM(29)(r9)
1438         std     r30, VCPU_GPRS_TM(30)(r9)
1439         std     r31, VCPU_GPRS_TM(31)(r9)
1440
1441         /* Save away PPR and DSCR soon so don't run with user values. */
1442         mfspr   r31, SPRN_PPR
1443         HMT_MEDIUM
1444         mfspr   r30, SPRN_DSCR
1445         ld      r29, HSTATE_DSCR(r13)
1446         mtspr   SPRN_DSCR, r29
1447
1448         /* Save all but r9, r13 & r29-r31 */
1449         reg = 0
1450         .rept   29
1451         .if (reg != 9) && (reg != 13)
1452         std     reg, VCPU_GPRS_TM(reg)(r9)
1453         .endif
1454         reg = reg + 1
1455         .endr
1456         /* ... now save r13 */
1457         GET_SCRATCH0(r4)
1458         std     r4, VCPU_GPRS_TM(13)(r9)
1459         /* ... and save r9 */
1460         ld      r4, PACATMSCRATCH(r13)
1461         std     r4, VCPU_GPRS_TM(9)(r9)
1462
1463         /* Reload stack pointer and TOC. */
1464         ld      r1, HSTATE_HOST_R1(r13)
1465         ld      r2, PACATOC(r13)
1466
1467         /* Set MSR RI now we have r1 and r13 back. */
1468         li      r5, MSR_RI
1469         mtmsrd  r5, 1
1470
1471         /* Save away checkpinted SPRs. */
1472         std     r31, VCPU_PPR_TM(r9)
1473         std     r30, VCPU_DSCR_TM(r9)
1474         mflr    r5
1475         mfcr    r6
1476         mfctr   r7
1477         mfspr   r8, SPRN_AMR
1478         mfspr   r10, SPRN_TAR
1479         std     r5, VCPU_LR_TM(r9)
1480         stw     r6, VCPU_CR_TM(r9)
1481         std     r7, VCPU_CTR_TM(r9)
1482         std     r8, VCPU_AMR_TM(r9)
1483         std     r10, VCPU_TAR_TM(r9)
1484
1485         /* Restore r12 as trap number. */
1486         lwz     r12, VCPU_TRAP(r9)
1487
1488         /* Save FP/VSX. */
1489         addi    r3, r9, VCPU_FPRS_TM
1490         bl      store_fp_state
1491         addi    r3, r9, VCPU_VRS_TM
1492         bl      store_vr_state
1493         mfspr   r6, SPRN_VRSAVE
1494         stw     r6, VCPU_VRSAVE_TM(r9)
1495 1:
1496         /*
1497          * We need to save these SPRs after the treclaim so that the software
1498          * error code is recorded correctly in the TEXASR.  Also the user may
1499          * change these outside of a transaction, so they must always be
1500          * context switched.
1501          */
1502         mfspr   r5, SPRN_TFHAR
1503         mfspr   r6, SPRN_TFIAR
1504         mfspr   r7, SPRN_TEXASR
1505         std     r5, VCPU_TFHAR(r9)
1506         std     r6, VCPU_TFIAR(r9)
1507         std     r7, VCPU_TEXASR(r9)
1508 2:
1509 #endif
1510
1511         /* Increment yield count if they have a VPA */
1512         ld      r8, VCPU_VPA(r9)        /* do they have a VPA? */
1513         cmpdi   r8, 0
1514         beq     25f
1515         li      r4, LPPACA_YIELDCOUNT
1516         LWZX_BE r3, r8, r4
1517         addi    r3, r3, 1
1518         STWX_BE r3, r8, r4
1519         li      r3, 1
1520         stb     r3, VCPU_VPA_DIRTY(r9)
1521 25:
1522         /* Save PMU registers if requested */
1523         /* r8 and cr0.eq are live here */
1524 BEGIN_FTR_SECTION
1525         /*
1526          * POWER8 seems to have a hardware bug where setting
1527          * MMCR0[PMAE] along with MMCR0[PMC1CE] and/or MMCR0[PMCjCE]
1528          * when some counters are already negative doesn't seem
1529          * to cause a performance monitor alert (and hence interrupt).
1530          * The effect of this is that when saving the PMU state,
1531          * if there is no PMU alert pending when we read MMCR0
1532          * before freezing the counters, but one becomes pending
1533          * before we read the counters, we lose it.
1534          * To work around this, we need a way to freeze the counters
1535          * before reading MMCR0.  Normally, freezing the counters
1536          * is done by writing MMCR0 (to set MMCR0[FC]) which
1537          * unavoidably writes MMCR0[PMA0] as well.  On POWER8,
1538          * we can also freeze the counters using MMCR2, by writing
1539          * 1s to all the counter freeze condition bits (there are
1540          * 9 bits each for 6 counters).
1541          */
1542         li      r3, -1                  /* set all freeze bits */
1543         clrrdi  r3, r3, 10
1544         mfspr   r10, SPRN_MMCR2
1545         mtspr   SPRN_MMCR2, r3
1546         isync
1547 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1548         li      r3, 1
1549         sldi    r3, r3, 31              /* MMCR0_FC (freeze counters) bit */
1550         mfspr   r4, SPRN_MMCR0          /* save MMCR0 */
1551         mtspr   SPRN_MMCR0, r3          /* freeze all counters, disable ints */
1552         mfspr   r6, SPRN_MMCRA
1553         /* Clear MMCRA in order to disable SDAR updates */
1554         li      r7, 0
1555         mtspr   SPRN_MMCRA, r7
1556         isync
1557         beq     21f                     /* if no VPA, save PMU stuff anyway */
1558         lbz     r7, LPPACA_PMCINUSE(r8)
1559         cmpwi   r7, 0                   /* did they ask for PMU stuff to be saved? */
1560         bne     21f
1561         std     r3, VCPU_MMCR(r9)       /* if not, set saved MMCR0 to FC */
1562         b       22f
1563 21:     mfspr   r5, SPRN_MMCR1
1564         mfspr   r7, SPRN_SIAR
1565         mfspr   r8, SPRN_SDAR
1566         std     r4, VCPU_MMCR(r9)
1567         std     r5, VCPU_MMCR + 8(r9)
1568         std     r6, VCPU_MMCR + 16(r9)
1569 BEGIN_FTR_SECTION
1570         std     r10, VCPU_MMCR + 24(r9)
1571 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1572         std     r7, VCPU_SIAR(r9)
1573         std     r8, VCPU_SDAR(r9)
1574         mfspr   r3, SPRN_PMC1
1575         mfspr   r4, SPRN_PMC2
1576         mfspr   r5, SPRN_PMC3
1577         mfspr   r6, SPRN_PMC4
1578         mfspr   r7, SPRN_PMC5
1579         mfspr   r8, SPRN_PMC6
1580         stw     r3, VCPU_PMC(r9)
1581         stw     r4, VCPU_PMC + 4(r9)
1582         stw     r5, VCPU_PMC + 8(r9)
1583         stw     r6, VCPU_PMC + 12(r9)
1584         stw     r7, VCPU_PMC + 16(r9)
1585         stw     r8, VCPU_PMC + 20(r9)
1586 BEGIN_FTR_SECTION
1587         mfspr   r5, SPRN_SIER
1588         mfspr   r6, SPRN_SPMC1
1589         mfspr   r7, SPRN_SPMC2
1590         mfspr   r8, SPRN_MMCRS
1591         std     r5, VCPU_SIER(r9)
1592         stw     r6, VCPU_PMC + 24(r9)
1593         stw     r7, VCPU_PMC + 28(r9)
1594         std     r8, VCPU_MMCR + 32(r9)
1595         lis     r4, 0x8000
1596         mtspr   SPRN_MMCRS, r4
1597 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1598 22:
1599         /* Clear out SLB */
1600         li      r5,0
1601         slbmte  r5,r5
1602         slbia
1603         ptesync
1604
1605         /*
1606          * POWER7/POWER8 guest -> host partition switch code.
1607          * We don't have to lock against tlbies but we do
1608          * have to coordinate the hardware threads.
1609          */
1610 kvmhv_switch_to_host:
1611         /* Secondary threads wait for primary to do partition switch */
1612         ld      r5,HSTATE_KVM_VCORE(r13)
1613         ld      r4,VCORE_KVM(r5)        /* pointer to struct kvm */
1614         lbz     r3,HSTATE_PTID(r13)
1615         cmpwi   r3,0
1616         beq     15f
1617         HMT_LOW
1618 13:     lbz     r3,VCORE_IN_GUEST(r5)
1619         cmpwi   r3,0
1620         bne     13b
1621         HMT_MEDIUM
1622         b       16f
1623
1624         /* Primary thread waits for all the secondaries to exit guest */
1625 15:     lwz     r3,VCORE_ENTRY_EXIT(r5)
1626         rlwinm  r0,r3,32-8,0xff
1627         clrldi  r3,r3,56
1628         cmpw    r3,r0
1629         bne     15b
1630         isync
1631
1632         /* Did we actually switch to the guest at all? */
1633         lbz     r6, VCORE_IN_GUEST(r5)
1634         cmpwi   r6, 0
1635         beq     19f
1636
1637         /* Primary thread switches back to host partition */
1638         ld      r6,KVM_HOST_SDR1(r4)
1639         lwz     r7,KVM_HOST_LPID(r4)
1640         li      r8,LPID_RSVD            /* switch to reserved LPID */
1641         mtspr   SPRN_LPID,r8
1642         ptesync
1643         mtspr   SPRN_SDR1,r6            /* switch to partition page table */
1644         mtspr   SPRN_LPID,r7
1645         isync
1646
1647 BEGIN_FTR_SECTION
1648         /* DPDES is shared between threads */
1649         mfspr   r7, SPRN_DPDES
1650         std     r7, VCORE_DPDES(r5)
1651         /* clear DPDES so we don't get guest doorbells in the host */
1652         li      r8, 0
1653         mtspr   SPRN_DPDES, r8
1654 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1655
1656         /* Subtract timebase offset from timebase */
1657         ld      r8,VCORE_TB_OFFSET(r5)
1658         cmpdi   r8,0
1659         beq     17f
1660         mftb    r6                      /* current guest timebase */
1661         subf    r8,r8,r6
1662         mtspr   SPRN_TBU40,r8           /* update upper 40 bits */
1663         mftb    r7                      /* check if lower 24 bits overflowed */
1664         clrldi  r6,r6,40
1665         clrldi  r7,r7,40
1666         cmpld   r7,r6
1667         bge     17f
1668         addis   r8,r8,0x100             /* if so, increment upper 40 bits */
1669         mtspr   SPRN_TBU40,r8
1670
1671         /* Reset PCR */
1672 17:     ld      r0, VCORE_PCR(r5)
1673         cmpdi   r0, 0
1674         beq     18f
1675         li      r0, 0
1676         mtspr   SPRN_PCR, r0
1677 18:
1678         /* Signal secondary CPUs to continue */
1679         stb     r0,VCORE_IN_GUEST(r5)
1680 19:     lis     r8,0x7fff               /* MAX_INT@h */
1681         mtspr   SPRN_HDEC,r8
1682
1683 16:     ld      r8,KVM_HOST_LPCR(r4)
1684         mtspr   SPRN_LPCR,r8
1685         isync
1686
1687         /* load host SLB entries */
1688         ld      r8,PACA_SLBSHADOWPTR(r13)
1689
1690         .rept   SLB_NUM_BOLTED
1691         li      r3, SLBSHADOW_SAVEAREA
1692         LDX_BE  r5, r8, r3
1693         addi    r3, r3, 8
1694         LDX_BE  r6, r8, r3
1695         andis.  r7,r5,SLB_ESID_V@h
1696         beq     1f
1697         slbmte  r6,r5
1698 1:      addi    r8,r8,16
1699         .endr
1700
1701 #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1702         /* Finish timing, if we have a vcpu */
1703         ld      r4, HSTATE_KVM_VCPU(r13)
1704         cmpdi   r4, 0
1705         li      r3, 0
1706         beq     2f
1707         bl      kvmhv_accumulate_time
1708 2:
1709 #endif
1710         /* Unset guest mode */
1711         li      r0, KVM_GUEST_MODE_NONE
1712         stb     r0, HSTATE_IN_GUEST(r13)
1713
1714         ld      r0, 112+PPC_LR_STKOFF(r1)
1715         addi    r1, r1, 112
1716         mtlr    r0
1717         blr
1718
1719 /*
1720  * Check whether an HDSI is an HPTE not found fault or something else.
1721  * If it is an HPTE not found fault that is due to the guest accessing
1722  * a page that they have mapped but which we have paged out, then
1723  * we continue on with the guest exit path.  In all other cases,
1724  * reflect the HDSI to the guest as a DSI.
1725  */
1726 kvmppc_hdsi:
1727         mfspr   r4, SPRN_HDAR
1728         mfspr   r6, SPRN_HDSISR
1729         /* HPTE not found fault or protection fault? */
1730         andis.  r0, r6, (DSISR_NOHPTE | DSISR_PROTFAULT)@h
1731         beq     1f                      /* if not, send it to the guest */
1732         andi.   r0, r11, MSR_DR         /* data relocation enabled? */
1733         beq     3f
1734         clrrdi  r0, r4, 28
1735         PPC_SLBFEE_DOT(R5, R0)          /* if so, look up SLB */
1736         bne     1f                      /* if no SLB entry found */
1737 4:      std     r4, VCPU_FAULT_DAR(r9)
1738         stw     r6, VCPU_FAULT_DSISR(r9)
1739
1740         /* Search the hash table. */
1741         mr      r3, r9                  /* vcpu pointer */
1742         li      r7, 1                   /* data fault */
1743         bl      kvmppc_hpte_hv_fault
1744         ld      r9, HSTATE_KVM_VCPU(r13)
1745         ld      r10, VCPU_PC(r9)
1746         ld      r11, VCPU_MSR(r9)
1747         li      r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1748         cmpdi   r3, 0                   /* retry the instruction */
1749         beq     6f
1750         cmpdi   r3, -1                  /* handle in kernel mode */
1751         beq     guest_exit_cont
1752         cmpdi   r3, -2                  /* MMIO emulation; need instr word */
1753         beq     2f
1754
1755         /* Synthesize a DSI for the guest */
1756         ld      r4, VCPU_FAULT_DAR(r9)
1757         mr      r6, r3
1758 1:      mtspr   SPRN_DAR, r4
1759         mtspr   SPRN_DSISR, r6
1760         mtspr   SPRN_SRR0, r10
1761         mtspr   SPRN_SRR1, r11
1762         li      r10, BOOK3S_INTERRUPT_DATA_STORAGE
1763         bl      kvmppc_msr_interrupt
1764 fast_interrupt_c_return:
1765 6:      ld      r7, VCPU_CTR(r9)
1766         lwz     r8, VCPU_XER(r9)
1767         mtctr   r7
1768         mtxer   r8
1769         mr      r4, r9
1770         b       fast_guest_return
1771
1772 3:      ld      r5, VCPU_KVM(r9)        /* not relocated, use VRMA */
1773         ld      r5, KVM_VRMA_SLB_V(r5)
1774         b       4b
1775
1776         /* If this is for emulated MMIO, load the instruction word */
1777 2:      li      r8, KVM_INST_FETCH_FAILED       /* In case lwz faults */
1778
1779         /* Set guest mode to 'jump over instruction' so if lwz faults
1780          * we'll just continue at the next IP. */
1781         li      r0, KVM_GUEST_MODE_SKIP
1782         stb     r0, HSTATE_IN_GUEST(r13)
1783
1784         /* Do the access with MSR:DR enabled */
1785         mfmsr   r3
1786         ori     r4, r3, MSR_DR          /* Enable paging for data */
1787         mtmsrd  r4
1788         lwz     r8, 0(r10)
1789         mtmsrd  r3
1790
1791         /* Store the result */
1792         stw     r8, VCPU_LAST_INST(r9)
1793
1794         /* Unset guest mode. */
1795         li      r0, KVM_GUEST_MODE_HOST_HV
1796         stb     r0, HSTATE_IN_GUEST(r13)
1797         b       guest_exit_cont
1798
1799 /*
1800  * Similarly for an HISI, reflect it to the guest as an ISI unless
1801  * it is an HPTE not found fault for a page that we have paged out.
1802  */
1803 kvmppc_hisi:
1804         andis.  r0, r11, SRR1_ISI_NOPT@h
1805         beq     1f
1806         andi.   r0, r11, MSR_IR         /* instruction relocation enabled? */
1807         beq     3f
1808         clrrdi  r0, r10, 28
1809         PPC_SLBFEE_DOT(R5, R0)          /* if so, look up SLB */
1810         bne     1f                      /* if no SLB entry found */
1811 4:
1812         /* Search the hash table. */
1813         mr      r3, r9                  /* vcpu pointer */
1814         mr      r4, r10
1815         mr      r6, r11
1816         li      r7, 0                   /* instruction fault */
1817         bl      kvmppc_hpte_hv_fault
1818         ld      r9, HSTATE_KVM_VCPU(r13)
1819         ld      r10, VCPU_PC(r9)
1820         ld      r11, VCPU_MSR(r9)
1821         li      r12, BOOK3S_INTERRUPT_H_INST_STORAGE
1822         cmpdi   r3, 0                   /* retry the instruction */
1823         beq     fast_interrupt_c_return
1824         cmpdi   r3, -1                  /* handle in kernel mode */
1825         beq     guest_exit_cont
1826
1827         /* Synthesize an ISI for the guest */
1828         mr      r11, r3
1829 1:      mtspr   SPRN_SRR0, r10
1830         mtspr   SPRN_SRR1, r11
1831         li      r10, BOOK3S_INTERRUPT_INST_STORAGE
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   0               /* 0x10 - H_CLEAR_MOD */
1908         .long   0               /* 0x14 - H_CLEAR_REF */
1909         .long   DOTSYM(kvmppc_h_protect) - hcall_real_table
1910         .long   DOTSYM(kvmppc_h_get_tce) - hcall_real_table
1911         .long   DOTSYM(kvmppc_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   0               /* 0x138 */
1990         .long   0               /* 0x13c */
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, 1, 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         /*
2215          * Set DEC to the smaller of DEC and HDEC, so that we wake
2216          * no later than the end of our timeslice (HDEC interrupts
2217          * don't wake us from nap).
2218          */
2219         mfspr   r3, SPRN_DEC
2220         mfspr   r4, SPRN_HDEC
2221         mftb    r5
2222         cmpw    r3, r4
2223         ble     67f
2224         mtspr   SPRN_DEC, r4
2225 67:
2226         /* save expiry time of guest decrementer */
2227         extsw   r3, r3
2228         add     r3, r3, r5
2229         ld      r4, HSTATE_KVM_VCPU(r13)
2230         ld      r5, HSTATE_KVM_VCORE(r13)
2231         ld      r6, VCORE_TB_OFFSET(r5)
2232         subf    r3, r6, r3      /* convert to host TB value */
2233         std     r3, VCPU_DEC_EXPIRES(r4)
2234
2235 #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2236         ld      r4, HSTATE_KVM_VCPU(r13)
2237         addi    r3, r4, VCPU_TB_CEDE
2238         bl      kvmhv_accumulate_time
2239 #endif
2240
2241         lis     r3, LPCR_PECEDP@h       /* Do wake on privileged doorbell */
2242
2243         /*
2244          * Take a nap until a decrementer or external or doobell interrupt
2245          * occurs, with PECE1 and PECE0 set in LPCR.
2246          * On POWER8, set PECEDH, and if we are ceding, also set PECEDP.
2247          * Also clear the runlatch bit before napping.
2248          */
2249 kvm_do_nap:
2250         mfspr   r0, SPRN_CTRLF
2251         clrrdi  r0, r0, 1
2252         mtspr   SPRN_CTRLT, r0
2253
2254         li      r0,1
2255         stb     r0,HSTATE_HWTHREAD_REQ(r13)
2256         mfspr   r5,SPRN_LPCR
2257         ori     r5,r5,LPCR_PECE0 | LPCR_PECE1
2258 BEGIN_FTR_SECTION
2259         ori     r5, r5, LPCR_PECEDH
2260         rlwimi  r5, r3, 0, LPCR_PECEDP
2261 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
2262         mtspr   SPRN_LPCR,r5
2263         isync
2264         li      r0, 0
2265         std     r0, HSTATE_SCRATCH0(r13)
2266         ptesync
2267         ld      r0, HSTATE_SCRATCH0(r13)
2268 1:      cmpd    r0, r0
2269         bne     1b
2270         nap
2271         b       .
2272
2273 33:     mr      r4, r3
2274         li      r3, 0
2275         li      r12, 0
2276         b       34f
2277
2278 kvm_end_cede:
2279         /* get vcpu pointer */
2280         ld      r4, HSTATE_KVM_VCPU(r13)
2281
2282         /* Woken by external or decrementer interrupt */
2283         ld      r1, HSTATE_HOST_R1(r13)
2284
2285 #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2286         addi    r3, r4, VCPU_TB_RMINTR
2287         bl      kvmhv_accumulate_time
2288 #endif
2289
2290         /* load up FP state */
2291         bl      kvmppc_load_fp
2292
2293         /* Restore guest decrementer */
2294         ld      r3, VCPU_DEC_EXPIRES(r4)
2295         ld      r5, HSTATE_KVM_VCORE(r13)
2296         ld      r6, VCORE_TB_OFFSET(r5)
2297         add     r3, r3, r6      /* convert host TB to guest TB value */
2298         mftb    r7
2299         subf    r3, r7, r3
2300         mtspr   SPRN_DEC, r3
2301
2302         /* Load NV GPRS */
2303         ld      r14, VCPU_GPR(R14)(r4)
2304         ld      r15, VCPU_GPR(R15)(r4)
2305         ld      r16, VCPU_GPR(R16)(r4)
2306         ld      r17, VCPU_GPR(R17)(r4)
2307         ld      r18, VCPU_GPR(R18)(r4)
2308         ld      r19, VCPU_GPR(R19)(r4)
2309         ld      r20, VCPU_GPR(R20)(r4)
2310         ld      r21, VCPU_GPR(R21)(r4)
2311         ld      r22, VCPU_GPR(R22)(r4)
2312         ld      r23, VCPU_GPR(R23)(r4)
2313         ld      r24, VCPU_GPR(R24)(r4)
2314         ld      r25, VCPU_GPR(R25)(r4)
2315         ld      r26, VCPU_GPR(R26)(r4)
2316         ld      r27, VCPU_GPR(R27)(r4)
2317         ld      r28, VCPU_GPR(R28)(r4)
2318         ld      r29, VCPU_GPR(R29)(r4)
2319         ld      r30, VCPU_GPR(R30)(r4)
2320         ld      r31, VCPU_GPR(R31)(r4)
2321  
2322         /* Check the wake reason in SRR1 to see why we got here */
2323         bl      kvmppc_check_wake_reason
2324
2325         /* clear our bit in vcore->napping_threads */
2326 34:     ld      r5,HSTATE_KVM_VCORE(r13)
2327         lbz     r7,HSTATE_PTID(r13)
2328         li      r0,1
2329         sld     r0,r0,r7
2330         addi    r6,r5,VCORE_NAPPING_THREADS
2331 32:     lwarx   r7,0,r6
2332         andc    r7,r7,r0
2333         stwcx.  r7,0,r6
2334         bne     32b
2335         li      r0,0
2336         stb     r0,HSTATE_NAPPING(r13)
2337
2338         /* See if the wake reason means we need to exit */
2339         stw     r12, VCPU_TRAP(r4)
2340         mr      r9, r4
2341         cmpdi   r3, 0
2342         bgt     guest_exit_cont
2343
2344         /* see if any other thread is already exiting */
2345         lwz     r0,VCORE_ENTRY_EXIT(r5)
2346         cmpwi   r0,0x100
2347         bge     guest_exit_cont
2348
2349         b       kvmppc_cede_reentry     /* if not go back to guest */
2350
2351         /* cede when already previously prodded case */
2352 kvm_cede_prodded:
2353         li      r0,0
2354         stb     r0,VCPU_PRODDED(r3)
2355         sync                    /* order testing prodded vs. clearing ceded */
2356         stb     r0,VCPU_CEDED(r3)
2357         li      r3,H_SUCCESS
2358         blr
2359
2360         /* we've ceded but we want to give control to the host */
2361 kvm_cede_exit:
2362         ld      r9, HSTATE_KVM_VCPU(r13)
2363         b       guest_exit_cont
2364
2365         /* Try to handle a machine check in real mode */
2366 machine_check_realmode:
2367         mr      r3, r9          /* get vcpu pointer */
2368         bl      kvmppc_realmode_machine_check
2369         nop
2370         cmpdi   r3, 0           /* Did we handle MCE ? */
2371         ld      r9, HSTATE_KVM_VCPU(r13)
2372         li      r12, BOOK3S_INTERRUPT_MACHINE_CHECK
2373         /*
2374          * Deliver unhandled/fatal (e.g. UE) MCE errors to guest through
2375          * machine check interrupt (set HSRR0 to 0x200). And for handled
2376          * errors (no-fatal), just go back to guest execution with current
2377          * HSRR0 instead of exiting guest. This new approach will inject
2378          * machine check to guest for fatal error causing guest to crash.
2379          *
2380          * The old code used to return to host for unhandled errors which
2381          * was causing guest to hang with soft lockups inside guest and
2382          * makes it difficult to recover guest instance.
2383          */
2384         ld      r10, VCPU_PC(r9)
2385         ld      r11, VCPU_MSR(r9)
2386         bne     2f      /* Continue guest execution. */
2387         /* If not, deliver a machine check.  SRR0/1 are already set */
2388         li      r10, BOOK3S_INTERRUPT_MACHINE_CHECK
2389         ld      r11, VCPU_MSR(r9)
2390         bl      kvmppc_msr_interrupt
2391 2:      b       fast_interrupt_c_return
2392
2393 /*
2394  * Check the reason we woke from nap, and take appropriate action.
2395  * Returns (in r3):
2396  *      0 if nothing needs to be done
2397  *      1 if something happened that needs to be handled by the host
2398  *      -1 if there was a guest wakeup (IPI or msgsnd)
2399  *
2400  * Also sets r12 to the interrupt vector for any interrupt that needs
2401  * to be handled now by the host (0x500 for external interrupt), or zero.
2402  * Modifies r0, r6, r7, r8.
2403  */
2404 kvmppc_check_wake_reason:
2405         mfspr   r6, SPRN_SRR1
2406 BEGIN_FTR_SECTION
2407         rlwinm  r6, r6, 45-31, 0xf      /* extract wake reason field (P8) */
2408 FTR_SECTION_ELSE
2409         rlwinm  r6, r6, 45-31, 0xe      /* P7 wake reason field is 3 bits */
2410 ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_207S)
2411         cmpwi   r6, 8                   /* was it an external interrupt? */
2412         li      r12, BOOK3S_INTERRUPT_EXTERNAL
2413         beq     kvmppc_read_intr        /* if so, see what it was */
2414         li      r3, 0
2415         li      r12, 0
2416         cmpwi   r6, 6                   /* was it the decrementer? */
2417         beq     0f
2418 BEGIN_FTR_SECTION
2419         cmpwi   r6, 5                   /* privileged doorbell? */
2420         beq     0f
2421         cmpwi   r6, 3                   /* hypervisor doorbell? */
2422         beq     3f
2423 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
2424         li      r3, 1                   /* anything else, return 1 */
2425 0:      blr
2426
2427         /* hypervisor doorbell */
2428 3:      li      r12, BOOK3S_INTERRUPT_H_DOORBELL
2429         /* see if it's a host IPI */
2430         li      r3, 1
2431         lbz     r0, HSTATE_HOST_IPI(r13)
2432         cmpwi   r0, 0
2433         bnelr
2434         /* if not, clear it and return -1 */
2435         lis     r6, (PPC_DBELL_SERVER << (63-36))@h
2436         PPC_MSGCLR(6)
2437         li      r3, -1
2438         blr
2439
2440 /*
2441  * Determine what sort of external interrupt is pending (if any).
2442  * Returns:
2443  *      0 if no interrupt is pending
2444  *      1 if an interrupt is pending that needs to be handled by the host
2445  *      -1 if there was a guest wakeup IPI (which has now been cleared)
2446  * Modifies r0, r6, r7, r8, returns value in r3.
2447  */
2448 kvmppc_read_intr:
2449         /* see if a host IPI is pending */
2450         li      r3, 1
2451         lbz     r0, HSTATE_HOST_IPI(r13)
2452         cmpwi   r0, 0
2453         bne     1f
2454
2455         /* Now read the interrupt from the ICP */
2456         ld      r6, HSTATE_XICS_PHYS(r13)
2457         li      r7, XICS_XIRR
2458         cmpdi   r6, 0
2459         beq-    1f
2460         lwzcix  r0, r6, r7
2461         /*
2462          * Save XIRR for later. Since we get in in reverse endian on LE
2463          * systems, save it byte reversed and fetch it back in host endian.
2464          */
2465         li      r3, HSTATE_SAVED_XIRR
2466         STWX_BE r0, r3, r13
2467 #ifdef __LITTLE_ENDIAN__
2468         lwz     r3, HSTATE_SAVED_XIRR(r13)
2469 #else
2470         mr      r3, r0
2471 #endif
2472         rlwinm. r3, r3, 0, 0xffffff
2473         sync
2474         beq     1f                      /* if nothing pending in the ICP */
2475
2476         /* We found something in the ICP...
2477          *
2478          * If it's not an IPI, stash it in the PACA and return to
2479          * the host, we don't (yet) handle directing real external
2480          * interrupts directly to the guest
2481          */
2482         cmpwi   r3, XICS_IPI            /* if there is, is it an IPI? */
2483         bne     42f
2484
2485         /* It's an IPI, clear the MFRR and EOI it */
2486         li      r3, 0xff
2487         li      r8, XICS_MFRR
2488         stbcix  r3, r6, r8              /* clear the IPI */
2489         stwcix  r0, r6, r7              /* EOI it */
2490         sync
2491
2492         /* We need to re-check host IPI now in case it got set in the
2493          * meantime. If it's clear, we bounce the interrupt to the
2494          * guest
2495          */
2496         lbz     r0, HSTATE_HOST_IPI(r13)
2497         cmpwi   r0, 0
2498         bne-    43f
2499
2500         /* OK, it's an IPI for us */
2501         li      r12, 0
2502         li      r3, -1
2503 1:      blr
2504
2505 42:     /* It's not an IPI and it's for the host. We saved a copy of XIRR in
2506          * the PACA earlier, it will be picked up by the host ICP driver
2507          */
2508         li      r3, 1
2509         b       1b
2510
2511 43:     /* We raced with the host, we need to resend that IPI, bummer */
2512         li      r0, IPI_PRIORITY
2513         stbcix  r0, r6, r8              /* set the IPI */
2514         sync
2515         li      r3, 1
2516         b       1b
2517
2518 /*
2519  * Save away FP, VMX and VSX registers.
2520  * r3 = vcpu pointer
2521  * N.B. r30 and r31 are volatile across this function,
2522  * thus it is not callable from C.
2523  */
2524 kvmppc_save_fp:
2525         mflr    r30
2526         mr      r31,r3
2527         mfmsr   r5
2528         ori     r8,r5,MSR_FP
2529 #ifdef CONFIG_ALTIVEC
2530 BEGIN_FTR_SECTION
2531         oris    r8,r8,MSR_VEC@h
2532 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2533 #endif
2534 #ifdef CONFIG_VSX
2535 BEGIN_FTR_SECTION
2536         oris    r8,r8,MSR_VSX@h
2537 END_FTR_SECTION_IFSET(CPU_FTR_VSX)
2538 #endif
2539         mtmsrd  r8
2540         addi    r3,r3,VCPU_FPRS
2541         bl      store_fp_state
2542 #ifdef CONFIG_ALTIVEC
2543 BEGIN_FTR_SECTION
2544         addi    r3,r31,VCPU_VRS
2545         bl      store_vr_state
2546 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2547 #endif
2548         mfspr   r6,SPRN_VRSAVE
2549         stw     r6,VCPU_VRSAVE(r31)
2550         mtlr    r30
2551         blr
2552
2553 /*
2554  * Load up FP, VMX and VSX registers
2555  * r4 = vcpu pointer
2556  * N.B. r30 and r31 are volatile across this function,
2557  * thus it is not callable from C.
2558  */
2559 kvmppc_load_fp:
2560         mflr    r30
2561         mr      r31,r4
2562         mfmsr   r9
2563         ori     r8,r9,MSR_FP
2564 #ifdef CONFIG_ALTIVEC
2565 BEGIN_FTR_SECTION
2566         oris    r8,r8,MSR_VEC@h
2567 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2568 #endif
2569 #ifdef CONFIG_VSX
2570 BEGIN_FTR_SECTION
2571         oris    r8,r8,MSR_VSX@h
2572 END_FTR_SECTION_IFSET(CPU_FTR_VSX)
2573 #endif
2574         mtmsrd  r8
2575         addi    r3,r4,VCPU_FPRS
2576         bl      load_fp_state
2577 #ifdef CONFIG_ALTIVEC
2578 BEGIN_FTR_SECTION
2579         addi    r3,r31,VCPU_VRS
2580         bl      load_vr_state
2581 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2582 #endif
2583         lwz     r7,VCPU_VRSAVE(r31)
2584         mtspr   SPRN_VRSAVE,r7
2585         mtlr    r30
2586         mr      r4,r31
2587         blr
2588
2589 /*
2590  * We come here if we get any exception or interrupt while we are
2591  * executing host real mode code while in guest MMU context.
2592  * For now just spin, but we should do something better.
2593  */
2594 kvmppc_bad_host_intr:
2595         b       .
2596
2597 /*
2598  * This mimics the MSR transition on IRQ delivery.  The new guest MSR is taken
2599  * from VCPU_INTR_MSR and is modified based on the required TM state changes.
2600  *   r11 has the guest MSR value (in/out)
2601  *   r9 has a vcpu pointer (in)
2602  *   r0 is used as a scratch register
2603  */
2604 kvmppc_msr_interrupt:
2605         rldicl  r0, r11, 64 - MSR_TS_S_LG, 62
2606         cmpwi   r0, 2 /* Check if we are in transactional state..  */
2607         ld      r11, VCPU_INTR_MSR(r9)
2608         bne     1f
2609         /* ... if transactional, change to suspended */
2610         li      r0, 1
2611 1:      rldimi  r11, r0, MSR_TS_S_LG, 63 - MSR_TS_T_LG
2612         blr
2613
2614 /*
2615  * This works around a hardware bug on POWER8E processors, where
2616  * writing a 1 to the MMCR0[PMAO] bit doesn't generate a
2617  * performance monitor interrupt.  Instead, when we need to have
2618  * an interrupt pending, we have to arrange for a counter to overflow.
2619  */
2620 kvmppc_fix_pmao:
2621         li      r3, 0
2622         mtspr   SPRN_MMCR2, r3
2623         lis     r3, (MMCR0_PMXE | MMCR0_FCECE)@h
2624         ori     r3, r3, MMCR0_PMCjCE | MMCR0_C56RUN
2625         mtspr   SPRN_MMCR0, r3
2626         lis     r3, 0x7fff
2627         ori     r3, r3, 0xffff
2628         mtspr   SPRN_PMC6, r3
2629         isync
2630         blr
2631
2632 #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2633 /*
2634  * Start timing an activity
2635  * r3 = pointer to time accumulation struct, r4 = vcpu
2636  */
2637 kvmhv_start_timing:
2638         ld      r5, HSTATE_KVM_VCORE(r13)
2639         lbz     r6, VCORE_IN_GUEST(r5)
2640         cmpwi   r6, 0
2641         beq     5f                              /* if in guest, need to */
2642         ld      r6, VCORE_TB_OFFSET(r5)         /* subtract timebase offset */
2643 5:      mftb    r5
2644         subf    r5, r6, r5
2645         std     r3, VCPU_CUR_ACTIVITY(r4)
2646         std     r5, VCPU_ACTIVITY_START(r4)
2647         blr
2648
2649 /*
2650  * Accumulate time to one activity and start another.
2651  * r3 = pointer to new time accumulation struct, r4 = vcpu
2652  */
2653 kvmhv_accumulate_time:
2654         ld      r5, HSTATE_KVM_VCORE(r13)
2655         lbz     r8, VCORE_IN_GUEST(r5)
2656         cmpwi   r8, 0
2657         beq     4f                              /* if in guest, need to */
2658         ld      r8, VCORE_TB_OFFSET(r5)         /* subtract timebase offset */
2659 4:      ld      r5, VCPU_CUR_ACTIVITY(r4)
2660         ld      r6, VCPU_ACTIVITY_START(r4)
2661         std     r3, VCPU_CUR_ACTIVITY(r4)
2662         mftb    r7
2663         subf    r7, r8, r7
2664         std     r7, VCPU_ACTIVITY_START(r4)
2665         cmpdi   r5, 0
2666         beqlr
2667         subf    r3, r6, r7
2668         ld      r8, TAS_SEQCOUNT(r5)
2669         cmpdi   r8, 0
2670         addi    r8, r8, 1
2671         std     r8, TAS_SEQCOUNT(r5)
2672         lwsync
2673         ld      r7, TAS_TOTAL(r5)
2674         add     r7, r7, r3
2675         std     r7, TAS_TOTAL(r5)
2676         ld      r6, TAS_MIN(r5)
2677         ld      r7, TAS_MAX(r5)
2678         beq     3f
2679         cmpd    r3, r6
2680         bge     1f
2681 3:      std     r3, TAS_MIN(r5)
2682 1:      cmpd    r3, r7
2683         ble     2f
2684         std     r3, TAS_MAX(r5)
2685 2:      lwsync
2686         addi    r8, r8, 1
2687         std     r8, TAS_SEQCOUNT(r5)
2688         blr
2689 #endif