]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - arch/powerpc/kvm/book3s_hv_rmhandlers.S
60fe8ba318cfc0f2abbfa2901a787d3d7c907a9a
[karo-tx-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 #ifdef __LITTLE_ENDIAN__
36 #error Need to fix lppaca and SLB shadow accesses in little endian mode
37 #endif
38
39 /* Values in HSTATE_NAPPING(r13) */
40 #define NAPPING_CEDE    1
41 #define NAPPING_NOVCPU  2
42
43 /*
44  * Call kvmppc_hv_entry in real mode.
45  * Must be called with interrupts hard-disabled.
46  *
47  * Input Registers:
48  *
49  * LR = return address to continue at after eventually re-enabling MMU
50  */
51 _GLOBAL(kvmppc_hv_entry_trampoline)
52         mflr    r0
53         std     r0, PPC_LR_STKOFF(r1)
54         stdu    r1, -112(r1)
55         mfmsr   r10
56         LOAD_REG_ADDR(r5, kvmppc_call_hv_entry)
57         li      r0,MSR_RI
58         andc    r0,r10,r0
59         li      r6,MSR_IR | MSR_DR
60         andc    r6,r10,r6
61         mtmsrd  r0,1            /* clear RI in MSR */
62         mtsrr0  r5
63         mtsrr1  r6
64         RFI
65
66 kvmppc_call_hv_entry:
67         ld      r4, HSTATE_KVM_VCPU(r13)
68         bl      kvmppc_hv_entry
69
70         /* Back from guest - restore host state and return to caller */
71
72 BEGIN_FTR_SECTION
73         /* Restore host DABR and DABRX */
74         ld      r5,HSTATE_DABR(r13)
75         li      r6,7
76         mtspr   SPRN_DABR,r5
77         mtspr   SPRN_DABRX,r6
78 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
79
80         /* Restore SPRG3 */
81         ld      r3,PACA_SPRG_VDSO(r13)
82         mtspr   SPRN_SPRG_VDSO_WRITE,r3
83
84         /* Reload the host's PMU registers */
85         ld      r3, PACALPPACAPTR(r13)  /* is the host using the PMU? */
86         lbz     r4, LPPACA_PMCINUSE(r3)
87         cmpwi   r4, 0
88         beq     23f                     /* skip if not */
89 BEGIN_FTR_SECTION
90         ld      r3, HSTATE_MMCR(r13)
91         andi.   r4, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO
92         cmpwi   r4, MMCR0_PMAO
93         beql    kvmppc_fix_pmao
94 END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG)
95         lwz     r3, HSTATE_PMC(r13)
96         lwz     r4, HSTATE_PMC + 4(r13)
97         lwz     r5, HSTATE_PMC + 8(r13)
98         lwz     r6, HSTATE_PMC + 12(r13)
99         lwz     r8, HSTATE_PMC + 16(r13)
100         lwz     r9, HSTATE_PMC + 20(r13)
101 BEGIN_FTR_SECTION
102         lwz     r10, HSTATE_PMC + 24(r13)
103         lwz     r11, HSTATE_PMC + 28(r13)
104 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
105         mtspr   SPRN_PMC1, r3
106         mtspr   SPRN_PMC2, r4
107         mtspr   SPRN_PMC3, r5
108         mtspr   SPRN_PMC4, r6
109         mtspr   SPRN_PMC5, r8
110         mtspr   SPRN_PMC6, r9
111 BEGIN_FTR_SECTION
112         mtspr   SPRN_PMC7, r10
113         mtspr   SPRN_PMC8, r11
114 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
115         ld      r3, HSTATE_MMCR(r13)
116         ld      r4, HSTATE_MMCR + 8(r13)
117         ld      r5, HSTATE_MMCR + 16(r13)
118         ld      r6, HSTATE_MMCR + 24(r13)
119         ld      r7, HSTATE_MMCR + 32(r13)
120         mtspr   SPRN_MMCR1, r4
121         mtspr   SPRN_MMCRA, r5
122         mtspr   SPRN_SIAR, r6
123         mtspr   SPRN_SDAR, r7
124 BEGIN_FTR_SECTION
125         ld      r8, HSTATE_MMCR + 40(r13)
126         ld      r9, HSTATE_MMCR + 48(r13)
127         mtspr   SPRN_MMCR2, r8
128         mtspr   SPRN_SIER, r9
129 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
130         mtspr   SPRN_MMCR0, r3
131         isync
132 23:
133
134         /*
135          * Reload DEC.  HDEC interrupts were disabled when
136          * we reloaded the host's LPCR value.
137          */
138         ld      r3, HSTATE_DECEXP(r13)
139         mftb    r4
140         subf    r4, r4, r3
141         mtspr   SPRN_DEC, r4
142
143         /*
144          * For external and machine check interrupts, we need
145          * to call the Linux handler to process the interrupt.
146          * We do that by jumping to absolute address 0x500 for
147          * external interrupts, or the machine_check_fwnmi label
148          * for machine checks (since firmware might have patched
149          * the vector area at 0x200).  The [h]rfid at the end of the
150          * handler will return to the book3s_hv_interrupts.S code.
151          * For other interrupts we do the rfid to get back
152          * to the book3s_hv_interrupts.S code here.
153          */
154         ld      r8, 112+PPC_LR_STKOFF(r1)
155         addi    r1, r1, 112
156         ld      r7, HSTATE_HOST_MSR(r13)
157
158         cmpwi   cr1, r12, BOOK3S_INTERRUPT_MACHINE_CHECK
159         cmpwi   r12, BOOK3S_INTERRUPT_EXTERNAL
160 BEGIN_FTR_SECTION
161         beq     11f
162 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
163
164         /* RFI into the highmem handler, or branch to interrupt handler */
165         mfmsr   r6
166         li      r0, MSR_RI
167         andc    r6, r6, r0
168         mtmsrd  r6, 1                   /* Clear RI in MSR */
169         mtsrr0  r8
170         mtsrr1  r7
171         beqa    0x500                   /* external interrupt (PPC970) */
172         beq     cr1, 13f                /* machine check */
173         RFI
174
175         /* On POWER7, we have external interrupts set to use HSRR0/1 */
176 11:     mtspr   SPRN_HSRR0, r8
177         mtspr   SPRN_HSRR1, r7
178         ba      0x500
179
180 13:     b       machine_check_fwnmi
181
182 kvmppc_primary_no_guest:
183         /* We handle this much like a ceded vcpu */
184         /* set our bit in napping_threads */
185         ld      r5, HSTATE_KVM_VCORE(r13)
186         lbz     r7, HSTATE_PTID(r13)
187         li      r0, 1
188         sld     r0, r0, r7
189         addi    r6, r5, VCORE_NAPPING_THREADS
190 1:      lwarx   r3, 0, r6
191         or      r3, r3, r0
192         stwcx.  r3, 0, r6
193         bne     1b
194         /* order napping_threads update vs testing entry_exit_count */
195         isync
196         li      r12, 0
197         lwz     r7, VCORE_ENTRY_EXIT(r5)
198         cmpwi   r7, 0x100
199         bge     kvm_novcpu_exit /* another thread already exiting */
200         li      r3, NAPPING_NOVCPU
201         stb     r3, HSTATE_NAPPING(r13)
202         li      r3, 1
203         stb     r3, HSTATE_HWTHREAD_REQ(r13)
204
205         b       kvm_do_nap
206
207 kvm_novcpu_wakeup:
208         ld      r1, HSTATE_HOST_R1(r13)
209         ld      r5, HSTATE_KVM_VCORE(r13)
210         li      r0, 0
211         stb     r0, HSTATE_NAPPING(r13)
212         stb     r0, HSTATE_HWTHREAD_REQ(r13)
213
214         /* check the wake reason */
215         bl      kvmppc_check_wake_reason
216         
217         /* see if any other thread is already exiting */
218         lwz     r0, VCORE_ENTRY_EXIT(r5)
219         cmpwi   r0, 0x100
220         bge     kvm_novcpu_exit
221
222         /* clear our bit in napping_threads */
223         lbz     r7, HSTATE_PTID(r13)
224         li      r0, 1
225         sld     r0, r0, r7
226         addi    r6, r5, VCORE_NAPPING_THREADS
227 4:      lwarx   r7, 0, r6
228         andc    r7, r7, r0
229         stwcx.  r7, 0, r6
230         bne     4b
231
232         /* See if the wake reason means we need to exit */
233         cmpdi   r3, 0
234         bge     kvm_novcpu_exit
235
236         /* Got an IPI but other vcpus aren't yet exiting, must be a latecomer */
237         ld      r4, HSTATE_KVM_VCPU(r13)
238         cmpdi   r4, 0
239         bne     kvmppc_got_guest
240
241 kvm_novcpu_exit:
242         b       hdec_soon
243
244 /*
245  * We come in here when wakened from nap mode.
246  * Relocation is off and most register values are lost.
247  * r13 points to the PACA.
248  */
249         .globl  kvm_start_guest
250 kvm_start_guest:
251         ld      r2,PACATOC(r13)
252
253         li      r0,KVM_HWTHREAD_IN_KVM
254         stb     r0,HSTATE_HWTHREAD_STATE(r13)
255
256         /* NV GPR values from power7_idle() will no longer be valid */
257         li      r0,1
258         stb     r0,PACA_NAPSTATELOST(r13)
259
260         /* were we napping due to cede? */
261         lbz     r0,HSTATE_NAPPING(r13)
262         cmpwi   r0,NAPPING_CEDE
263         beq     kvm_end_cede
264         cmpwi   r0,NAPPING_NOVCPU
265         beq     kvm_novcpu_wakeup
266
267         ld      r1,PACAEMERGSP(r13)
268         subi    r1,r1,STACK_FRAME_OVERHEAD
269
270         /*
271          * We weren't napping due to cede, so this must be a secondary
272          * thread being woken up to run a guest, or being woken up due
273          * to a stray IPI.  (Or due to some machine check or hypervisor
274          * maintenance interrupt while the core is in KVM.)
275          */
276
277         /* Check the wake reason in SRR1 to see why we got here */
278         bl      kvmppc_check_wake_reason
279         cmpdi   r3, 0
280         bge     kvm_no_guest
281
282         /* get vcpu pointer, NULL if we have no vcpu to run */
283         ld      r4,HSTATE_KVM_VCPU(r13)
284         cmpdi   r4,0
285         /* if we have no vcpu to run, go back to sleep */
286         beq     kvm_no_guest
287
288         /* Set HSTATE_DSCR(r13) to something sensible */
289         LOAD_REG_ADDR(r6, dscr_default)
290         ld      r6, 0(r6)
291         std     r6, HSTATE_DSCR(r13)
292
293         bl      kvmppc_hv_entry
294
295         /* Back from the guest, go back to nap */
296         /* Clear our vcpu pointer so we don't come back in early */
297         li      r0, 0
298         std     r0, HSTATE_KVM_VCPU(r13)
299         /*
300          * Make sure we clear HSTATE_KVM_VCPU(r13) before incrementing
301          * the nap_count, because once the increment to nap_count is
302          * visible we could be given another vcpu.
303          */
304         lwsync
305
306         /* increment the nap count and then go to nap mode */
307         ld      r4, HSTATE_KVM_VCORE(r13)
308         addi    r4, r4, VCORE_NAP_COUNT
309 51:     lwarx   r3, 0, r4
310         addi    r3, r3, 1
311         stwcx.  r3, 0, r4
312         bne     51b
313
314 kvm_no_guest:
315         li      r0, KVM_HWTHREAD_IN_NAP
316         stb     r0, HSTATE_HWTHREAD_STATE(r13)
317 kvm_do_nap:
318         li      r3, LPCR_PECE0
319         mfspr   r4, SPRN_LPCR
320         rlwimi  r4, r3, 0, LPCR_PECE0 | LPCR_PECE1
321         mtspr   SPRN_LPCR, r4
322         isync
323         std     r0, HSTATE_SCRATCH0(r13)
324         ptesync
325         ld      r0, HSTATE_SCRATCH0(r13)
326 1:      cmpd    r0, r0
327         bne     1b
328         nap
329         b       .
330
331 /******************************************************************************
332  *                                                                            *
333  *                               Entry code                                   *
334  *                                                                            *
335  *****************************************************************************/
336
337 .global kvmppc_hv_entry
338 kvmppc_hv_entry:
339
340         /* Required state:
341          *
342          * R4 = vcpu pointer (or NULL)
343          * MSR = ~IR|DR
344          * R13 = PACA
345          * R1 = host R1
346          * all other volatile GPRS = free
347          */
348         mflr    r0
349         std     r0, PPC_LR_STKOFF(r1)
350         stdu    r1, -112(r1)
351
352         /* Save R1 in the PACA */
353         std     r1, HSTATE_HOST_R1(r13)
354
355         li      r6, KVM_GUEST_MODE_HOST_HV
356         stb     r6, HSTATE_IN_GUEST(r13)
357
358         /* Clear out SLB */
359         li      r6,0
360         slbmte  r6,r6
361         slbia
362         ptesync
363
364 BEGIN_FTR_SECTION
365         b       30f
366 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
367         /*
368          * POWER7 host -> guest partition switch code.
369          * We don't have to lock against concurrent tlbies,
370          * but we do have to coordinate across hardware threads.
371          */
372         /* Increment entry count iff exit count is zero. */
373         ld      r5,HSTATE_KVM_VCORE(r13)
374         addi    r9,r5,VCORE_ENTRY_EXIT
375 21:     lwarx   r3,0,r9
376         cmpwi   r3,0x100                /* any threads starting to exit? */
377         bge     secondary_too_late      /* if so we're too late to the party */
378         addi    r3,r3,1
379         stwcx.  r3,0,r9
380         bne     21b
381
382         /* Primary thread switches to guest partition. */
383         ld      r9,VCORE_KVM(r5)        /* pointer to struct kvm */
384         lbz     r6,HSTATE_PTID(r13)
385         cmpwi   r6,0
386         bne     20f
387         ld      r6,KVM_SDR1(r9)
388         lwz     r7,KVM_LPID(r9)
389         li      r0,LPID_RSVD            /* switch to reserved LPID */
390         mtspr   SPRN_LPID,r0
391         ptesync
392         mtspr   SPRN_SDR1,r6            /* switch to partition page table */
393         mtspr   SPRN_LPID,r7
394         isync
395
396         /* See if we need to flush the TLB */
397         lhz     r6,PACAPACAINDEX(r13)   /* test_bit(cpu, need_tlb_flush) */
398         clrldi  r7,r6,64-6              /* extract bit number (6 bits) */
399         srdi    r6,r6,6                 /* doubleword number */
400         sldi    r6,r6,3                 /* address offset */
401         add     r6,r6,r9
402         addi    r6,r6,KVM_NEED_FLUSH    /* dword in kvm->arch.need_tlb_flush */
403         li      r0,1
404         sld     r0,r0,r7
405         ld      r7,0(r6)
406         and.    r7,r7,r0
407         beq     22f
408 23:     ldarx   r7,0,r6                 /* if set, clear the bit */
409         andc    r7,r7,r0
410         stdcx.  r7,0,r6
411         bne     23b
412         /* Flush the TLB of any entries for this LPID */
413         /* use arch 2.07S as a proxy for POWER8 */
414 BEGIN_FTR_SECTION
415         li      r6,512                  /* POWER8 has 512 sets */
416 FTR_SECTION_ELSE
417         li      r6,128                  /* POWER7 has 128 sets */
418 ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_207S)
419         mtctr   r6
420         li      r7,0x800                /* IS field = 0b10 */
421         ptesync
422 28:     tlbiel  r7
423         addi    r7,r7,0x1000
424         bdnz    28b
425         ptesync
426
427         /* Add timebase offset onto timebase */
428 22:     ld      r8,VCORE_TB_OFFSET(r5)
429         cmpdi   r8,0
430         beq     37f
431         mftb    r6              /* current host timebase */
432         add     r8,r8,r6
433         mtspr   SPRN_TBU40,r8   /* update upper 40 bits */
434         mftb    r7              /* check if lower 24 bits overflowed */
435         clrldi  r6,r6,40
436         clrldi  r7,r7,40
437         cmpld   r7,r6
438         bge     37f
439         addis   r8,r8,0x100     /* if so, increment upper 40 bits */
440         mtspr   SPRN_TBU40,r8
441
442         /* Load guest PCR value to select appropriate compat mode */
443 37:     ld      r7, VCORE_PCR(r5)
444         cmpdi   r7, 0
445         beq     38f
446         mtspr   SPRN_PCR, r7
447 38:
448
449 BEGIN_FTR_SECTION
450         /* DPDES is shared between threads */
451         ld      r8, VCORE_DPDES(r5)
452         mtspr   SPRN_DPDES, r8
453 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
454
455         li      r0,1
456         stb     r0,VCORE_IN_GUEST(r5)   /* signal secondaries to continue */
457         b       10f
458
459         /* Secondary threads wait for primary to have done partition switch */
460 20:     lbz     r0,VCORE_IN_GUEST(r5)
461         cmpwi   r0,0
462         beq     20b
463
464         /* Set LPCR and RMOR. */
465 10:     ld      r8,VCORE_LPCR(r5)
466         mtspr   SPRN_LPCR,r8
467         ld      r8,KVM_RMOR(r9)
468         mtspr   SPRN_RMOR,r8
469         isync
470
471         /* Check if HDEC expires soon */
472         mfspr   r3,SPRN_HDEC
473         cmpwi   r3,512          /* 1 microsecond */
474         li      r12,BOOK3S_INTERRUPT_HV_DECREMENTER
475         blt     hdec_soon
476         b       31f
477
478         /*
479          * PPC970 host -> guest partition switch code.
480          * We have to lock against concurrent tlbies,
481          * using native_tlbie_lock to lock against host tlbies
482          * and kvm->arch.tlbie_lock to lock against guest tlbies.
483          * We also have to invalidate the TLB since its
484          * entries aren't tagged with the LPID.
485          */
486 30:     ld      r5,HSTATE_KVM_VCORE(r13)
487         ld      r9,VCORE_KVM(r5)        /* pointer to struct kvm */
488
489         /* first take native_tlbie_lock */
490         .section ".toc","aw"
491 toc_tlbie_lock:
492         .tc     native_tlbie_lock[TC],native_tlbie_lock
493         .previous
494         ld      r3,toc_tlbie_lock@toc(2)
495 #ifdef __BIG_ENDIAN__
496         lwz     r8,PACA_LOCK_TOKEN(r13)
497 #else
498         lwz     r8,PACAPACAINDEX(r13)
499 #endif
500 24:     lwarx   r0,0,r3
501         cmpwi   r0,0
502         bne     24b
503         stwcx.  r8,0,r3
504         bne     24b
505         isync
506
507         ld      r5,HSTATE_KVM_VCORE(r13)
508         ld      r7,VCORE_LPCR(r5)       /* use vcore->lpcr to store HID4 */
509         li      r0,0x18f
510         rotldi  r0,r0,HID4_LPID5_SH     /* all lpid bits in HID4 = 1 */
511         or      r0,r7,r0
512         ptesync
513         sync
514         mtspr   SPRN_HID4,r0            /* switch to reserved LPID */
515         isync
516         li      r0,0
517         stw     r0,0(r3)                /* drop native_tlbie_lock */
518
519         /* invalidate the whole TLB */
520         li      r0,256
521         mtctr   r0
522         li      r6,0
523 25:     tlbiel  r6
524         addi    r6,r6,0x1000
525         bdnz    25b
526         ptesync
527
528         /* Take the guest's tlbie_lock */
529         addi    r3,r9,KVM_TLBIE_LOCK
530 24:     lwarx   r0,0,r3
531         cmpwi   r0,0
532         bne     24b
533         stwcx.  r8,0,r3
534         bne     24b
535         isync
536         ld      r6,KVM_SDR1(r9)
537         mtspr   SPRN_SDR1,r6            /* switch to partition page table */
538
539         /* Set up HID4 with the guest's LPID etc. */
540         sync
541         mtspr   SPRN_HID4,r7
542         isync
543
544         /* drop the guest's tlbie_lock */
545         li      r0,0
546         stw     r0,0(r3)
547
548         /* Check if HDEC expires soon */
549         mfspr   r3,SPRN_HDEC
550         cmpwi   r3,10
551         li      r12,BOOK3S_INTERRUPT_HV_DECREMENTER
552         blt     hdec_soon
553
554         /* Enable HDEC interrupts */
555         mfspr   r0,SPRN_HID0
556         li      r3,1
557         rldimi  r0,r3, HID0_HDICE_SH, 64-HID0_HDICE_SH-1
558         sync
559         mtspr   SPRN_HID0,r0
560         mfspr   r0,SPRN_HID0
561         mfspr   r0,SPRN_HID0
562         mfspr   r0,SPRN_HID0
563         mfspr   r0,SPRN_HID0
564         mfspr   r0,SPRN_HID0
565         mfspr   r0,SPRN_HID0
566 31:
567         /* Do we have a guest vcpu to run? */
568         cmpdi   r4, 0
569         beq     kvmppc_primary_no_guest
570 kvmppc_got_guest:
571
572         /* Load up guest SLB entries */
573         lwz     r5,VCPU_SLB_MAX(r4)
574         cmpwi   r5,0
575         beq     9f
576         mtctr   r5
577         addi    r6,r4,VCPU_SLB
578 1:      ld      r8,VCPU_SLB_E(r6)
579         ld      r9,VCPU_SLB_V(r6)
580         slbmte  r9,r8
581         addi    r6,r6,VCPU_SLB_SIZE
582         bdnz    1b
583 9:
584         /* Increment yield count if they have a VPA */
585         ld      r3, VCPU_VPA(r4)
586         cmpdi   r3, 0
587         beq     25f
588         lwz     r5, LPPACA_YIELDCOUNT(r3)
589         addi    r5, r5, 1
590         stw     r5, LPPACA_YIELDCOUNT(r3)
591         li      r6, 1
592         stb     r6, VCPU_VPA_DIRTY(r4)
593 25:
594
595 BEGIN_FTR_SECTION
596         /* Save purr/spurr */
597         mfspr   r5,SPRN_PURR
598         mfspr   r6,SPRN_SPURR
599         std     r5,HSTATE_PURR(r13)
600         std     r6,HSTATE_SPURR(r13)
601         ld      r7,VCPU_PURR(r4)
602         ld      r8,VCPU_SPURR(r4)
603         mtspr   SPRN_PURR,r7
604         mtspr   SPRN_SPURR,r8
605 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
606
607 BEGIN_FTR_SECTION
608         /* Set partition DABR */
609         /* Do this before re-enabling PMU to avoid P7 DABR corruption bug */
610         lwz     r5,VCPU_DABRX(r4)
611         ld      r6,VCPU_DABR(r4)
612         mtspr   SPRN_DABRX,r5
613         mtspr   SPRN_DABR,r6
614  BEGIN_FTR_SECTION_NESTED(89)
615         isync
616  END_FTR_SECTION_NESTED(CPU_FTR_ARCH_206, CPU_FTR_ARCH_206, 89)
617 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
618
619 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
620 BEGIN_FTR_SECTION
621         b       skip_tm
622 END_FTR_SECTION_IFCLR(CPU_FTR_TM)
623
624         /* Turn on TM/FP/VSX/VMX so we can restore them. */
625         mfmsr   r5
626         li      r6, MSR_TM >> 32
627         sldi    r6, r6, 32
628         or      r5, r5, r6
629         ori     r5, r5, MSR_FP
630         oris    r5, r5, (MSR_VEC | MSR_VSX)@h
631         mtmsrd  r5
632
633         /*
634          * The user may change these outside of a transaction, so they must
635          * always be context switched.
636          */
637         ld      r5, VCPU_TFHAR(r4)
638         ld      r6, VCPU_TFIAR(r4)
639         ld      r7, VCPU_TEXASR(r4)
640         mtspr   SPRN_TFHAR, r5
641         mtspr   SPRN_TFIAR, r6
642         mtspr   SPRN_TEXASR, r7
643
644         ld      r5, VCPU_MSR(r4)
645         rldicl. r5, r5, 64 - MSR_TS_S_LG, 62
646         beq     skip_tm /* TM not active in guest */
647
648         /* Make sure the failure summary is set, otherwise we'll program check
649          * when we trechkpt.  It's possible that this might have been not set
650          * on a kvmppc_set_one_reg() call but we shouldn't let this crash the
651          * host.
652          */
653         oris    r7, r7, (TEXASR_FS)@h
654         mtspr   SPRN_TEXASR, r7
655
656         /*
657          * We need to load up the checkpointed state for the guest.
658          * We need to do this early as it will blow away any GPRs, VSRs and
659          * some SPRs.
660          */
661
662         mr      r31, r4
663         addi    r3, r31, VCPU_FPRS_TM
664         bl      .load_fp_state
665         addi    r3, r31, VCPU_VRS_TM
666         bl      .load_vr_state
667         mr      r4, r31
668         lwz     r7, VCPU_VRSAVE_TM(r4)
669         mtspr   SPRN_VRSAVE, r7
670
671         ld      r5, VCPU_LR_TM(r4)
672         lwz     r6, VCPU_CR_TM(r4)
673         ld      r7, VCPU_CTR_TM(r4)
674         ld      r8, VCPU_AMR_TM(r4)
675         ld      r9, VCPU_TAR_TM(r4)
676         mtlr    r5
677         mtcr    r6
678         mtctr   r7
679         mtspr   SPRN_AMR, r8
680         mtspr   SPRN_TAR, r9
681
682         /*
683          * Load up PPR and DSCR values but don't put them in the actual SPRs
684          * till the last moment to avoid running with userspace PPR and DSCR for
685          * too long.
686          */
687         ld      r29, VCPU_DSCR_TM(r4)
688         ld      r30, VCPU_PPR_TM(r4)
689
690         std     r2, PACATMSCRATCH(r13) /* Save TOC */
691
692         /* Clear the MSR RI since r1, r13 are all going to be foobar. */
693         li      r5, 0
694         mtmsrd  r5, 1
695
696         /* Load GPRs r0-r28 */
697         reg = 0
698         .rept   29
699         ld      reg, VCPU_GPRS_TM(reg)(r31)
700         reg = reg + 1
701         .endr
702
703         mtspr   SPRN_DSCR, r29
704         mtspr   SPRN_PPR, r30
705
706         /* Load final GPRs */
707         ld      29, VCPU_GPRS_TM(29)(r31)
708         ld      30, VCPU_GPRS_TM(30)(r31)
709         ld      31, VCPU_GPRS_TM(31)(r31)
710
711         /* TM checkpointed state is now setup.  All GPRs are now volatile. */
712         TRECHKPT
713
714         /* Now let's get back the state we need. */
715         HMT_MEDIUM
716         GET_PACA(r13)
717         ld      r29, HSTATE_DSCR(r13)
718         mtspr   SPRN_DSCR, r29
719         ld      r4, HSTATE_KVM_VCPU(r13)
720         ld      r1, HSTATE_HOST_R1(r13)
721         ld      r2, PACATMSCRATCH(r13)
722
723         /* Set the MSR RI since we have our registers back. */
724         li      r5, MSR_RI
725         mtmsrd  r5, 1
726 skip_tm:
727 #endif
728
729         /* Load guest PMU registers */
730         /* R4 is live here (vcpu pointer) */
731         li      r3, 1
732         sldi    r3, r3, 31              /* MMCR0_FC (freeze counters) bit */
733         mtspr   SPRN_MMCR0, r3          /* freeze all counters, disable ints */
734         isync
735 BEGIN_FTR_SECTION
736         ld      r3, VCPU_MMCR(r4)
737         andi.   r5, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO
738         cmpwi   r5, MMCR0_PMAO
739         beql    kvmppc_fix_pmao
740 END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG)
741         lwz     r3, VCPU_PMC(r4)        /* always load up guest PMU registers */
742         lwz     r5, VCPU_PMC + 4(r4)    /* to prevent information leak */
743         lwz     r6, VCPU_PMC + 8(r4)
744         lwz     r7, VCPU_PMC + 12(r4)
745         lwz     r8, VCPU_PMC + 16(r4)
746         lwz     r9, VCPU_PMC + 20(r4)
747 BEGIN_FTR_SECTION
748         lwz     r10, VCPU_PMC + 24(r4)
749         lwz     r11, VCPU_PMC + 28(r4)
750 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
751         mtspr   SPRN_PMC1, r3
752         mtspr   SPRN_PMC2, r5
753         mtspr   SPRN_PMC3, r6
754         mtspr   SPRN_PMC4, r7
755         mtspr   SPRN_PMC5, r8
756         mtspr   SPRN_PMC6, r9
757 BEGIN_FTR_SECTION
758         mtspr   SPRN_PMC7, r10
759         mtspr   SPRN_PMC8, r11
760 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
761         ld      r3, VCPU_MMCR(r4)
762         ld      r5, VCPU_MMCR + 8(r4)
763         ld      r6, VCPU_MMCR + 16(r4)
764         ld      r7, VCPU_SIAR(r4)
765         ld      r8, VCPU_SDAR(r4)
766         mtspr   SPRN_MMCR1, r5
767         mtspr   SPRN_MMCRA, r6
768         mtspr   SPRN_SIAR, r7
769         mtspr   SPRN_SDAR, r8
770 BEGIN_FTR_SECTION
771         ld      r5, VCPU_MMCR + 24(r4)
772         ld      r6, VCPU_SIER(r4)
773         lwz     r7, VCPU_PMC + 24(r4)
774         lwz     r8, VCPU_PMC + 28(r4)
775         ld      r9, VCPU_MMCR + 32(r4)
776         mtspr   SPRN_MMCR2, r5
777         mtspr   SPRN_SIER, r6
778         mtspr   SPRN_SPMC1, r7
779         mtspr   SPRN_SPMC2, r8
780         mtspr   SPRN_MMCRS, r9
781 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
782         mtspr   SPRN_MMCR0, r3
783         isync
784
785         /* Load up FP, VMX and VSX registers */
786         bl      kvmppc_load_fp
787
788         ld      r14, VCPU_GPR(R14)(r4)
789         ld      r15, VCPU_GPR(R15)(r4)
790         ld      r16, VCPU_GPR(R16)(r4)
791         ld      r17, VCPU_GPR(R17)(r4)
792         ld      r18, VCPU_GPR(R18)(r4)
793         ld      r19, VCPU_GPR(R19)(r4)
794         ld      r20, VCPU_GPR(R20)(r4)
795         ld      r21, VCPU_GPR(R21)(r4)
796         ld      r22, VCPU_GPR(R22)(r4)
797         ld      r23, VCPU_GPR(R23)(r4)
798         ld      r24, VCPU_GPR(R24)(r4)
799         ld      r25, VCPU_GPR(R25)(r4)
800         ld      r26, VCPU_GPR(R26)(r4)
801         ld      r27, VCPU_GPR(R27)(r4)
802         ld      r28, VCPU_GPR(R28)(r4)
803         ld      r29, VCPU_GPR(R29)(r4)
804         ld      r30, VCPU_GPR(R30)(r4)
805         ld      r31, VCPU_GPR(R31)(r4)
806
807 BEGIN_FTR_SECTION
808         /* Switch DSCR to guest value */
809         ld      r5, VCPU_DSCR(r4)
810         mtspr   SPRN_DSCR, r5
811 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
812
813 BEGIN_FTR_SECTION
814         /* Skip next section on POWER7 or PPC970 */
815         b       8f
816 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
817         /* Turn on TM so we can access TFHAR/TFIAR/TEXASR */
818         mfmsr   r8
819         li      r0, 1
820         rldimi  r8, r0, MSR_TM_LG, 63-MSR_TM_LG
821         mtmsrd  r8
822
823         /* Load up POWER8-specific registers */
824         ld      r5, VCPU_IAMR(r4)
825         lwz     r6, VCPU_PSPB(r4)
826         ld      r7, VCPU_FSCR(r4)
827         mtspr   SPRN_IAMR, r5
828         mtspr   SPRN_PSPB, r6
829         mtspr   SPRN_FSCR, r7
830         ld      r5, VCPU_DAWR(r4)
831         ld      r6, VCPU_DAWRX(r4)
832         ld      r7, VCPU_CIABR(r4)
833         ld      r8, VCPU_TAR(r4)
834         mtspr   SPRN_DAWR, r5
835         mtspr   SPRN_DAWRX, r6
836         mtspr   SPRN_CIABR, r7
837         mtspr   SPRN_TAR, r8
838         ld      r5, VCPU_IC(r4)
839         ld      r6, VCPU_VTB(r4)
840         mtspr   SPRN_IC, r5
841         mtspr   SPRN_VTB, r6
842         ld      r8, VCPU_EBBHR(r4)
843         mtspr   SPRN_EBBHR, r8
844         ld      r5, VCPU_EBBRR(r4)
845         ld      r6, VCPU_BESCR(r4)
846         ld      r7, VCPU_CSIGR(r4)
847         ld      r8, VCPU_TACR(r4)
848         mtspr   SPRN_EBBRR, r5
849         mtspr   SPRN_BESCR, r6
850         mtspr   SPRN_CSIGR, r7
851         mtspr   SPRN_TACR, r8
852         ld      r5, VCPU_TCSCR(r4)
853         ld      r6, VCPU_ACOP(r4)
854         lwz     r7, VCPU_GUEST_PID(r4)
855         ld      r8, VCPU_WORT(r4)
856         mtspr   SPRN_TCSCR, r5
857         mtspr   SPRN_ACOP, r6
858         mtspr   SPRN_PID, r7
859         mtspr   SPRN_WORT, r8
860 8:
861
862         /*
863          * Set the decrementer to the guest decrementer.
864          */
865         ld      r8,VCPU_DEC_EXPIRES(r4)
866         /* r8 is a host timebase value here, convert to guest TB */
867         ld      r5,HSTATE_KVM_VCORE(r13)
868         ld      r6,VCORE_TB_OFFSET(r5)
869         add     r8,r8,r6
870         mftb    r7
871         subf    r3,r7,r8
872         mtspr   SPRN_DEC,r3
873         stw     r3,VCPU_DEC(r4)
874
875         ld      r5, VCPU_SPRG0(r4)
876         ld      r6, VCPU_SPRG1(r4)
877         ld      r7, VCPU_SPRG2(r4)
878         ld      r8, VCPU_SPRG3(r4)
879         mtspr   SPRN_SPRG0, r5
880         mtspr   SPRN_SPRG1, r6
881         mtspr   SPRN_SPRG2, r7
882         mtspr   SPRN_SPRG3, r8
883
884         /* Load up DAR and DSISR */
885         ld      r5, VCPU_DAR(r4)
886         lwz     r6, VCPU_DSISR(r4)
887         mtspr   SPRN_DAR, r5
888         mtspr   SPRN_DSISR, r6
889
890 BEGIN_FTR_SECTION
891         /* Restore AMR and UAMOR, set AMOR to all 1s */
892         ld      r5,VCPU_AMR(r4)
893         ld      r6,VCPU_UAMOR(r4)
894         li      r7,-1
895         mtspr   SPRN_AMR,r5
896         mtspr   SPRN_UAMOR,r6
897         mtspr   SPRN_AMOR,r7
898 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
899
900         /* Restore state of CTRL run bit; assume 1 on entry */
901         lwz     r5,VCPU_CTRL(r4)
902         andi.   r5,r5,1
903         bne     4f
904         mfspr   r6,SPRN_CTRLF
905         clrrdi  r6,r6,1
906         mtspr   SPRN_CTRLT,r6
907 4:
908         ld      r6, VCPU_CTR(r4)
909         lwz     r7, VCPU_XER(r4)
910
911         mtctr   r6
912         mtxer   r7
913
914 kvmppc_cede_reentry:            /* r4 = vcpu, r13 = paca */
915         ld      r10, VCPU_PC(r4)
916         ld      r11, VCPU_MSR(r4)
917         ld      r6, VCPU_SRR0(r4)
918         ld      r7, VCPU_SRR1(r4)
919         mtspr   SPRN_SRR0, r6
920         mtspr   SPRN_SRR1, r7
921
922 deliver_guest_interrupt:
923         /* r11 = vcpu->arch.msr & ~MSR_HV */
924         rldicl  r11, r11, 63 - MSR_HV_LG, 1
925         rotldi  r11, r11, 1 + MSR_HV_LG
926         ori     r11, r11, MSR_ME
927
928         /* Check if we can deliver an external or decrementer interrupt now */
929         ld      r0, VCPU_PENDING_EXC(r4)
930         rldicl  r0, r0, 64 - BOOK3S_IRQPRIO_EXTERNAL_LEVEL, 63
931         cmpdi   cr1, r0, 0
932         andi.   r8, r11, MSR_EE
933 BEGIN_FTR_SECTION
934         mfspr   r8, SPRN_LPCR
935         /* Insert EXTERNAL_LEVEL bit into LPCR at the MER bit position */
936         rldimi  r8, r0, LPCR_MER_SH, 63 - LPCR_MER_SH
937         mtspr   SPRN_LPCR, r8
938         isync
939 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
940         beq     5f
941         li      r0, BOOK3S_INTERRUPT_EXTERNAL
942         bne     cr1, 12f
943         mfspr   r0, SPRN_DEC
944         cmpwi   r0, 0
945         li      r0, BOOK3S_INTERRUPT_DECREMENTER
946         bge     5f
947
948 12:     mtspr   SPRN_SRR0, r10
949         mr      r10,r0
950         mtspr   SPRN_SRR1, r11
951         mr      r9, r4
952         bl      kvmppc_msr_interrupt
953 5:
954
955 /*
956  * Required state:
957  * R4 = vcpu
958  * R10: value for HSRR0
959  * R11: value for HSRR1
960  * R13 = PACA
961  */
962 fast_guest_return:
963         li      r0,0
964         stb     r0,VCPU_CEDED(r4)       /* cancel cede */
965         mtspr   SPRN_HSRR0,r10
966         mtspr   SPRN_HSRR1,r11
967
968         /* Activate guest mode, so faults get handled by KVM */
969         li      r9, KVM_GUEST_MODE_GUEST_HV
970         stb     r9, HSTATE_IN_GUEST(r13)
971
972         /* Enter guest */
973
974 BEGIN_FTR_SECTION
975         ld      r5, VCPU_CFAR(r4)
976         mtspr   SPRN_CFAR, r5
977 END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
978 BEGIN_FTR_SECTION
979         ld      r0, VCPU_PPR(r4)
980 END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
981
982         ld      r5, VCPU_LR(r4)
983         lwz     r6, VCPU_CR(r4)
984         mtlr    r5
985         mtcr    r6
986
987         ld      r1, VCPU_GPR(R1)(r4)
988         ld      r2, VCPU_GPR(R2)(r4)
989         ld      r3, VCPU_GPR(R3)(r4)
990         ld      r5, VCPU_GPR(R5)(r4)
991         ld      r6, VCPU_GPR(R6)(r4)
992         ld      r7, VCPU_GPR(R7)(r4)
993         ld      r8, VCPU_GPR(R8)(r4)
994         ld      r9, VCPU_GPR(R9)(r4)
995         ld      r10, VCPU_GPR(R10)(r4)
996         ld      r11, VCPU_GPR(R11)(r4)
997         ld      r12, VCPU_GPR(R12)(r4)
998         ld      r13, VCPU_GPR(R13)(r4)
999
1000 BEGIN_FTR_SECTION
1001         mtspr   SPRN_PPR, r0
1002 END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
1003         ld      r0, VCPU_GPR(R0)(r4)
1004         ld      r4, VCPU_GPR(R4)(r4)
1005
1006         hrfid
1007         b       .
1008
1009 /******************************************************************************
1010  *                                                                            *
1011  *                               Exit code                                    *
1012  *                                                                            *
1013  *****************************************************************************/
1014
1015 /*
1016  * We come here from the first-level interrupt handlers.
1017  */
1018         .globl  kvmppc_interrupt_hv
1019 kvmppc_interrupt_hv:
1020         /*
1021          * Register contents:
1022          * R12          = interrupt vector
1023          * R13          = PACA
1024          * guest CR, R12 saved in shadow VCPU SCRATCH1/0
1025          * guest R13 saved in SPRN_SCRATCH0
1026          */
1027         std     r9, HSTATE_SCRATCH2(r13)
1028
1029         lbz     r9, HSTATE_IN_GUEST(r13)
1030         cmpwi   r9, KVM_GUEST_MODE_HOST_HV
1031         beq     kvmppc_bad_host_intr
1032 #ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1033         cmpwi   r9, KVM_GUEST_MODE_GUEST
1034         ld      r9, HSTATE_SCRATCH2(r13)
1035         beq     kvmppc_interrupt_pr
1036 #endif
1037         /* We're now back in the host but in guest MMU context */
1038         li      r9, KVM_GUEST_MODE_HOST_HV
1039         stb     r9, HSTATE_IN_GUEST(r13)
1040
1041         ld      r9, HSTATE_KVM_VCPU(r13)
1042
1043         /* Save registers */
1044
1045         std     r0, VCPU_GPR(R0)(r9)
1046         std     r1, VCPU_GPR(R1)(r9)
1047         std     r2, VCPU_GPR(R2)(r9)
1048         std     r3, VCPU_GPR(R3)(r9)
1049         std     r4, VCPU_GPR(R4)(r9)
1050         std     r5, VCPU_GPR(R5)(r9)
1051         std     r6, VCPU_GPR(R6)(r9)
1052         std     r7, VCPU_GPR(R7)(r9)
1053         std     r8, VCPU_GPR(R8)(r9)
1054         ld      r0, HSTATE_SCRATCH2(r13)
1055         std     r0, VCPU_GPR(R9)(r9)
1056         std     r10, VCPU_GPR(R10)(r9)
1057         std     r11, VCPU_GPR(R11)(r9)
1058         ld      r3, HSTATE_SCRATCH0(r13)
1059         lwz     r4, HSTATE_SCRATCH1(r13)
1060         std     r3, VCPU_GPR(R12)(r9)
1061         stw     r4, VCPU_CR(r9)
1062 BEGIN_FTR_SECTION
1063         ld      r3, HSTATE_CFAR(r13)
1064         std     r3, VCPU_CFAR(r9)
1065 END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
1066 BEGIN_FTR_SECTION
1067         ld      r4, HSTATE_PPR(r13)
1068         std     r4, VCPU_PPR(r9)
1069 END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
1070
1071         /* Restore R1/R2 so we can handle faults */
1072         ld      r1, HSTATE_HOST_R1(r13)
1073         ld      r2, PACATOC(r13)
1074
1075         mfspr   r10, SPRN_SRR0
1076         mfspr   r11, SPRN_SRR1
1077         std     r10, VCPU_SRR0(r9)
1078         std     r11, VCPU_SRR1(r9)
1079         andi.   r0, r12, 2              /* need to read HSRR0/1? */
1080         beq     1f
1081         mfspr   r10, SPRN_HSRR0
1082         mfspr   r11, SPRN_HSRR1
1083         clrrdi  r12, r12, 2
1084 1:      std     r10, VCPU_PC(r9)
1085         std     r11, VCPU_MSR(r9)
1086
1087         GET_SCRATCH0(r3)
1088         mflr    r4
1089         std     r3, VCPU_GPR(R13)(r9)
1090         std     r4, VCPU_LR(r9)
1091
1092         stw     r12,VCPU_TRAP(r9)
1093
1094         /* Save HEIR (HV emulation assist reg) in last_inst
1095            if this is an HEI (HV emulation interrupt, e40) */
1096         li      r3,KVM_INST_FETCH_FAILED
1097 BEGIN_FTR_SECTION
1098         cmpwi   r12,BOOK3S_INTERRUPT_H_EMUL_ASSIST
1099         bne     11f
1100         mfspr   r3,SPRN_HEIR
1101 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
1102 11:     stw     r3,VCPU_LAST_INST(r9)
1103
1104         /* these are volatile across C function calls */
1105         mfctr   r3
1106         mfxer   r4
1107         std     r3, VCPU_CTR(r9)
1108         stw     r4, VCPU_XER(r9)
1109
1110 BEGIN_FTR_SECTION
1111         /* If this is a page table miss then see if it's theirs or ours */
1112         cmpwi   r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1113         beq     kvmppc_hdsi
1114         cmpwi   r12, BOOK3S_INTERRUPT_H_INST_STORAGE
1115         beq     kvmppc_hisi
1116 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
1117
1118         /* See if this is a leftover HDEC interrupt */
1119         cmpwi   r12,BOOK3S_INTERRUPT_HV_DECREMENTER
1120         bne     2f
1121         mfspr   r3,SPRN_HDEC
1122         cmpwi   r3,0
1123         bge     ignore_hdec
1124 2:
1125         /* See if this is an hcall we can handle in real mode */
1126         cmpwi   r12,BOOK3S_INTERRUPT_SYSCALL
1127         beq     hcall_try_real_mode
1128
1129         /* Only handle external interrupts here on arch 206 and later */
1130 BEGIN_FTR_SECTION
1131         b       ext_interrupt_to_host
1132 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_206)
1133
1134         /* External interrupt ? */
1135         cmpwi   r12, BOOK3S_INTERRUPT_EXTERNAL
1136         bne+    ext_interrupt_to_host
1137
1138         /* External interrupt, first check for host_ipi. If this is
1139          * set, we know the host wants us out so let's do it now
1140          */
1141         bl      kvmppc_read_intr
1142         cmpdi   r3, 0
1143         bgt     ext_interrupt_to_host
1144
1145         /* Check if any CPU is heading out to the host, if so head out too */
1146         ld      r5, HSTATE_KVM_VCORE(r13)
1147         lwz     r0, VCORE_ENTRY_EXIT(r5)
1148         cmpwi   r0, 0x100
1149         bge     ext_interrupt_to_host
1150
1151         /* Return to guest after delivering any pending interrupt */
1152         mr      r4, r9
1153         b       deliver_guest_interrupt
1154
1155 ext_interrupt_to_host:
1156
1157 guest_exit_cont:                /* r9 = vcpu, r12 = trap, r13 = paca */
1158         /* Save more register state  */
1159         mfdar   r6
1160         mfdsisr r7
1161         std     r6, VCPU_DAR(r9)
1162         stw     r7, VCPU_DSISR(r9)
1163 BEGIN_FTR_SECTION
1164         /* don't overwrite fault_dar/fault_dsisr if HDSI */
1165         cmpwi   r12,BOOK3S_INTERRUPT_H_DATA_STORAGE
1166         beq     6f
1167 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
1168         std     r6, VCPU_FAULT_DAR(r9)
1169         stw     r7, VCPU_FAULT_DSISR(r9)
1170
1171         /* See if it is a machine check */
1172         cmpwi   r12, BOOK3S_INTERRUPT_MACHINE_CHECK
1173         beq     machine_check_realmode
1174 mc_cont:
1175
1176         /* Save guest CTRL register, set runlatch to 1 */
1177 6:      mfspr   r6,SPRN_CTRLF
1178         stw     r6,VCPU_CTRL(r9)
1179         andi.   r0,r6,1
1180         bne     4f
1181         ori     r6,r6,1
1182         mtspr   SPRN_CTRLT,r6
1183 4:
1184         /* Read the guest SLB and save it away */
1185         lwz     r0,VCPU_SLB_NR(r9)      /* number of entries in SLB */
1186         mtctr   r0
1187         li      r6,0
1188         addi    r7,r9,VCPU_SLB
1189         li      r5,0
1190 1:      slbmfee r8,r6
1191         andis.  r0,r8,SLB_ESID_V@h
1192         beq     2f
1193         add     r8,r8,r6                /* put index in */
1194         slbmfev r3,r6
1195         std     r8,VCPU_SLB_E(r7)
1196         std     r3,VCPU_SLB_V(r7)
1197         addi    r7,r7,VCPU_SLB_SIZE
1198         addi    r5,r5,1
1199 2:      addi    r6,r6,1
1200         bdnz    1b
1201         stw     r5,VCPU_SLB_MAX(r9)
1202
1203         /*
1204          * Save the guest PURR/SPURR
1205          */
1206 BEGIN_FTR_SECTION
1207         mfspr   r5,SPRN_PURR
1208         mfspr   r6,SPRN_SPURR
1209         ld      r7,VCPU_PURR(r9)
1210         ld      r8,VCPU_SPURR(r9)
1211         std     r5,VCPU_PURR(r9)
1212         std     r6,VCPU_SPURR(r9)
1213         subf    r5,r7,r5
1214         subf    r6,r8,r6
1215
1216         /*
1217          * Restore host PURR/SPURR and add guest times
1218          * so that the time in the guest gets accounted.
1219          */
1220         ld      r3,HSTATE_PURR(r13)
1221         ld      r4,HSTATE_SPURR(r13)
1222         add     r3,r3,r5
1223         add     r4,r4,r6
1224         mtspr   SPRN_PURR,r3
1225         mtspr   SPRN_SPURR,r4
1226 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_201)
1227
1228         /* Save DEC */
1229         mfspr   r5,SPRN_DEC
1230         mftb    r6
1231         extsw   r5,r5
1232         add     r5,r5,r6
1233         /* r5 is a guest timebase value here, convert to host TB */
1234         ld      r3,HSTATE_KVM_VCORE(r13)
1235         ld      r4,VCORE_TB_OFFSET(r3)
1236         subf    r5,r4,r5
1237         std     r5,VCPU_DEC_EXPIRES(r9)
1238
1239 BEGIN_FTR_SECTION
1240         b       8f
1241 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
1242         /* Save POWER8-specific registers */
1243         mfspr   r5, SPRN_IAMR
1244         mfspr   r6, SPRN_PSPB
1245         mfspr   r7, SPRN_FSCR
1246         std     r5, VCPU_IAMR(r9)
1247         stw     r6, VCPU_PSPB(r9)
1248         std     r7, VCPU_FSCR(r9)
1249         mfspr   r5, SPRN_IC
1250         mfspr   r6, SPRN_VTB
1251         mfspr   r7, SPRN_TAR
1252         std     r5, VCPU_IC(r9)
1253         std     r6, VCPU_VTB(r9)
1254         std     r7, VCPU_TAR(r9)
1255         mfspr   r8, SPRN_EBBHR
1256         std     r8, VCPU_EBBHR(r9)
1257         mfspr   r5, SPRN_EBBRR
1258         mfspr   r6, SPRN_BESCR
1259         mfspr   r7, SPRN_CSIGR
1260         mfspr   r8, SPRN_TACR
1261         std     r5, VCPU_EBBRR(r9)
1262         std     r6, VCPU_BESCR(r9)
1263         std     r7, VCPU_CSIGR(r9)
1264         std     r8, VCPU_TACR(r9)
1265         mfspr   r5, SPRN_TCSCR
1266         mfspr   r6, SPRN_ACOP
1267         mfspr   r7, SPRN_PID
1268         mfspr   r8, SPRN_WORT
1269         std     r5, VCPU_TCSCR(r9)
1270         std     r6, VCPU_ACOP(r9)
1271         stw     r7, VCPU_GUEST_PID(r9)
1272         std     r8, VCPU_WORT(r9)
1273 8:
1274
1275         /* Save and reset AMR and UAMOR before turning on the MMU */
1276 BEGIN_FTR_SECTION
1277         mfspr   r5,SPRN_AMR
1278         mfspr   r6,SPRN_UAMOR
1279         std     r5,VCPU_AMR(r9)
1280         std     r6,VCPU_UAMOR(r9)
1281         li      r6,0
1282         mtspr   SPRN_AMR,r6
1283 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
1284
1285         /* Switch DSCR back to host value */
1286 BEGIN_FTR_SECTION
1287         mfspr   r8, SPRN_DSCR
1288         ld      r7, HSTATE_DSCR(r13)
1289         std     r8, VCPU_DSCR(r9)
1290         mtspr   SPRN_DSCR, r7
1291 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
1292
1293         /* Save non-volatile GPRs */
1294         std     r14, VCPU_GPR(R14)(r9)
1295         std     r15, VCPU_GPR(R15)(r9)
1296         std     r16, VCPU_GPR(R16)(r9)
1297         std     r17, VCPU_GPR(R17)(r9)
1298         std     r18, VCPU_GPR(R18)(r9)
1299         std     r19, VCPU_GPR(R19)(r9)
1300         std     r20, VCPU_GPR(R20)(r9)
1301         std     r21, VCPU_GPR(R21)(r9)
1302         std     r22, VCPU_GPR(R22)(r9)
1303         std     r23, VCPU_GPR(R23)(r9)
1304         std     r24, VCPU_GPR(R24)(r9)
1305         std     r25, VCPU_GPR(R25)(r9)
1306         std     r26, VCPU_GPR(R26)(r9)
1307         std     r27, VCPU_GPR(R27)(r9)
1308         std     r28, VCPU_GPR(R28)(r9)
1309         std     r29, VCPU_GPR(R29)(r9)
1310         std     r30, VCPU_GPR(R30)(r9)
1311         std     r31, VCPU_GPR(R31)(r9)
1312
1313         /* Save SPRGs */
1314         mfspr   r3, SPRN_SPRG0
1315         mfspr   r4, SPRN_SPRG1
1316         mfspr   r5, SPRN_SPRG2
1317         mfspr   r6, SPRN_SPRG3
1318         std     r3, VCPU_SPRG0(r9)
1319         std     r4, VCPU_SPRG1(r9)
1320         std     r5, VCPU_SPRG2(r9)
1321         std     r6, VCPU_SPRG3(r9)
1322
1323         /* save FP state */
1324         mr      r3, r9
1325         bl      kvmppc_save_fp
1326
1327         /* Increment yield count if they have a VPA */
1328         ld      r8, VCPU_VPA(r9)        /* do they have a VPA? */
1329         cmpdi   r8, 0
1330         beq     25f
1331         lwz     r3, LPPACA_YIELDCOUNT(r8)
1332         addi    r3, r3, 1
1333         stw     r3, LPPACA_YIELDCOUNT(r8)
1334         li      r3, 1
1335         stb     r3, VCPU_VPA_DIRTY(r9)
1336 25:
1337         /* Save PMU registers if requested */
1338         /* r8 and cr0.eq are live here */
1339 BEGIN_FTR_SECTION
1340         /*
1341          * POWER8 seems to have a hardware bug where setting
1342          * MMCR0[PMAE] along with MMCR0[PMC1CE] and/or MMCR0[PMCjCE]
1343          * when some counters are already negative doesn't seem
1344          * to cause a performance monitor alert (and hence interrupt).
1345          * The effect of this is that when saving the PMU state,
1346          * if there is no PMU alert pending when we read MMCR0
1347          * before freezing the counters, but one becomes pending
1348          * before we read the counters, we lose it.
1349          * To work around this, we need a way to freeze the counters
1350          * before reading MMCR0.  Normally, freezing the counters
1351          * is done by writing MMCR0 (to set MMCR0[FC]) which
1352          * unavoidably writes MMCR0[PMA0] as well.  On POWER8,
1353          * we can also freeze the counters using MMCR2, by writing
1354          * 1s to all the counter freeze condition bits (there are
1355          * 9 bits each for 6 counters).
1356          */
1357         li      r3, -1                  /* set all freeze bits */
1358         clrrdi  r3, r3, 10
1359         mfspr   r10, SPRN_MMCR2
1360         mtspr   SPRN_MMCR2, r3
1361         isync
1362 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1363         li      r3, 1
1364         sldi    r3, r3, 31              /* MMCR0_FC (freeze counters) bit */
1365         mfspr   r4, SPRN_MMCR0          /* save MMCR0 */
1366         mtspr   SPRN_MMCR0, r3          /* freeze all counters, disable ints */
1367         mfspr   r6, SPRN_MMCRA
1368 BEGIN_FTR_SECTION
1369         /* On P7, clear MMCRA in order to disable SDAR updates */
1370         li      r7, 0
1371         mtspr   SPRN_MMCRA, r7
1372 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
1373         isync
1374         beq     21f                     /* if no VPA, save PMU stuff anyway */
1375         lbz     r7, LPPACA_PMCINUSE(r8)
1376         cmpwi   r7, 0                   /* did they ask for PMU stuff to be saved? */
1377         bne     21f
1378         std     r3, VCPU_MMCR(r9)       /* if not, set saved MMCR0 to FC */
1379         b       22f
1380 21:     mfspr   r5, SPRN_MMCR1
1381         mfspr   r7, SPRN_SIAR
1382         mfspr   r8, SPRN_SDAR
1383         std     r4, VCPU_MMCR(r9)
1384         std     r5, VCPU_MMCR + 8(r9)
1385         std     r6, VCPU_MMCR + 16(r9)
1386 BEGIN_FTR_SECTION
1387         std     r10, VCPU_MMCR + 24(r9)
1388 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1389         std     r7, VCPU_SIAR(r9)
1390         std     r8, VCPU_SDAR(r9)
1391         mfspr   r3, SPRN_PMC1
1392         mfspr   r4, SPRN_PMC2
1393         mfspr   r5, SPRN_PMC3
1394         mfspr   r6, SPRN_PMC4
1395         mfspr   r7, SPRN_PMC5
1396         mfspr   r8, SPRN_PMC6
1397 BEGIN_FTR_SECTION
1398         mfspr   r10, SPRN_PMC7
1399         mfspr   r11, SPRN_PMC8
1400 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
1401         stw     r3, VCPU_PMC(r9)
1402         stw     r4, VCPU_PMC + 4(r9)
1403         stw     r5, VCPU_PMC + 8(r9)
1404         stw     r6, VCPU_PMC + 12(r9)
1405         stw     r7, VCPU_PMC + 16(r9)
1406         stw     r8, VCPU_PMC + 20(r9)
1407 BEGIN_FTR_SECTION
1408         stw     r10, VCPU_PMC + 24(r9)
1409         stw     r11, VCPU_PMC + 28(r9)
1410 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
1411 BEGIN_FTR_SECTION
1412         mfspr   r5, SPRN_SIER
1413         mfspr   r6, SPRN_SPMC1
1414         mfspr   r7, SPRN_SPMC2
1415         mfspr   r8, SPRN_MMCRS
1416         std     r5, VCPU_SIER(r9)
1417         stw     r6, VCPU_PMC + 24(r9)
1418         stw     r7, VCPU_PMC + 28(r9)
1419         std     r8, VCPU_MMCR + 32(r9)
1420         lis     r4, 0x8000
1421         mtspr   SPRN_MMCRS, r4
1422 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1423 22:
1424         /* Clear out SLB */
1425         li      r5,0
1426         slbmte  r5,r5
1427         slbia
1428         ptesync
1429
1430 hdec_soon:                      /* r12 = trap, r13 = paca */
1431 BEGIN_FTR_SECTION
1432         b       32f
1433 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
1434         /*
1435          * POWER7 guest -> host partition switch code.
1436          * We don't have to lock against tlbies but we do
1437          * have to coordinate the hardware threads.
1438          */
1439         /* Increment the threads-exiting-guest count in the 0xff00
1440            bits of vcore->entry_exit_count */
1441         ld      r5,HSTATE_KVM_VCORE(r13)
1442         addi    r6,r5,VCORE_ENTRY_EXIT
1443 41:     lwarx   r3,0,r6
1444         addi    r0,r3,0x100
1445         stwcx.  r0,0,r6
1446         bne     41b
1447         isync           /* order stwcx. vs. reading napping_threads */
1448
1449         /*
1450          * At this point we have an interrupt that we have to pass
1451          * up to the kernel or qemu; we can't handle it in real mode.
1452          * Thus we have to do a partition switch, so we have to
1453          * collect the other threads, if we are the first thread
1454          * to take an interrupt.  To do this, we set the HDEC to 0,
1455          * which causes an HDEC interrupt in all threads within 2ns
1456          * because the HDEC register is shared between all 4 threads.
1457          * However, we don't need to bother if this is an HDEC
1458          * interrupt, since the other threads will already be on their
1459          * way here in that case.
1460          */
1461         cmpwi   r3,0x100        /* Are we the first here? */
1462         bge     43f
1463         cmpwi   r12,BOOK3S_INTERRUPT_HV_DECREMENTER
1464         beq     40f
1465         li      r0,0
1466         mtspr   SPRN_HDEC,r0
1467 40:
1468         /*
1469          * Send an IPI to any napping threads, since an HDEC interrupt
1470          * doesn't wake CPUs up from nap.
1471          */
1472         lwz     r3,VCORE_NAPPING_THREADS(r5)
1473         lbz     r4,HSTATE_PTID(r13)
1474         li      r0,1
1475         sld     r0,r0,r4
1476         andc.   r3,r3,r0                /* no sense IPI'ing ourselves */
1477         beq     43f
1478         /* Order entry/exit update vs. IPIs */
1479         sync
1480         mulli   r4,r4,PACA_SIZE         /* get paca for thread 0 */
1481         subf    r6,r4,r13
1482 42:     andi.   r0,r3,1
1483         beq     44f
1484         ld      r8,HSTATE_XICS_PHYS(r6) /* get thread's XICS reg addr */
1485         li      r0,IPI_PRIORITY
1486         li      r7,XICS_MFRR
1487         stbcix  r0,r7,r8                /* trigger the IPI */
1488 44:     srdi.   r3,r3,1
1489         addi    r6,r6,PACA_SIZE
1490         bne     42b
1491
1492 secondary_too_late:
1493         /* Secondary threads wait for primary to do partition switch */
1494 43:     ld      r5,HSTATE_KVM_VCORE(r13)
1495         ld      r4,VCORE_KVM(r5)        /* pointer to struct kvm */
1496         lbz     r3,HSTATE_PTID(r13)
1497         cmpwi   r3,0
1498         beq     15f
1499         HMT_LOW
1500 13:     lbz     r3,VCORE_IN_GUEST(r5)
1501         cmpwi   r3,0
1502         bne     13b
1503         HMT_MEDIUM
1504         b       16f
1505
1506         /* Primary thread waits for all the secondaries to exit guest */
1507 15:     lwz     r3,VCORE_ENTRY_EXIT(r5)
1508         srwi    r0,r3,8
1509         clrldi  r3,r3,56
1510         cmpw    r3,r0
1511         bne     15b
1512         isync
1513
1514         /* Primary thread switches back to host partition */
1515         ld      r6,KVM_HOST_SDR1(r4)
1516         lwz     r7,KVM_HOST_LPID(r4)
1517         li      r8,LPID_RSVD            /* switch to reserved LPID */
1518         mtspr   SPRN_LPID,r8
1519         ptesync
1520         mtspr   SPRN_SDR1,r6            /* switch to partition page table */
1521         mtspr   SPRN_LPID,r7
1522         isync
1523
1524 BEGIN_FTR_SECTION
1525         /* DPDES is shared between threads */
1526         mfspr   r7, SPRN_DPDES
1527         std     r7, VCORE_DPDES(r5)
1528         /* clear DPDES so we don't get guest doorbells in the host */
1529         li      r8, 0
1530         mtspr   SPRN_DPDES, r8
1531 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1532
1533         /* Subtract timebase offset from timebase */
1534         ld      r8,VCORE_TB_OFFSET(r5)
1535         cmpdi   r8,0
1536         beq     17f
1537         mftb    r6                      /* current guest timebase */
1538         subf    r8,r8,r6
1539         mtspr   SPRN_TBU40,r8           /* update upper 40 bits */
1540         mftb    r7                      /* check if lower 24 bits overflowed */
1541         clrldi  r6,r6,40
1542         clrldi  r7,r7,40
1543         cmpld   r7,r6
1544         bge     17f
1545         addis   r8,r8,0x100             /* if so, increment upper 40 bits */
1546         mtspr   SPRN_TBU40,r8
1547
1548         /* Reset PCR */
1549 17:     ld      r0, VCORE_PCR(r5)
1550         cmpdi   r0, 0
1551         beq     18f
1552         li      r0, 0
1553         mtspr   SPRN_PCR, r0
1554 18:
1555         /* Signal secondary CPUs to continue */
1556         stb     r0,VCORE_IN_GUEST(r5)
1557         lis     r8,0x7fff               /* MAX_INT@h */
1558         mtspr   SPRN_HDEC,r8
1559
1560 16:     ld      r8,KVM_HOST_LPCR(r4)
1561         mtspr   SPRN_LPCR,r8
1562         isync
1563         b       33f
1564
1565         /*
1566          * PPC970 guest -> host partition switch code.
1567          * We have to lock against concurrent tlbies, and
1568          * we have to flush the whole TLB.
1569          */
1570 32:     ld      r5,HSTATE_KVM_VCORE(r13)
1571         ld      r4,VCORE_KVM(r5)        /* pointer to struct kvm */
1572
1573         /* Take the guest's tlbie_lock */
1574 #ifdef __BIG_ENDIAN__
1575         lwz     r8,PACA_LOCK_TOKEN(r13)
1576 #else
1577         lwz     r8,PACAPACAINDEX(r13)
1578 #endif
1579         addi    r3,r4,KVM_TLBIE_LOCK
1580 24:     lwarx   r0,0,r3
1581         cmpwi   r0,0
1582         bne     24b
1583         stwcx.  r8,0,r3
1584         bne     24b
1585         isync
1586
1587         ld      r7,KVM_HOST_LPCR(r4)    /* use kvm->arch.host_lpcr for HID4 */
1588         li      r0,0x18f
1589         rotldi  r0,r0,HID4_LPID5_SH     /* all lpid bits in HID4 = 1 */
1590         or      r0,r7,r0
1591         ptesync
1592         sync
1593         mtspr   SPRN_HID4,r0            /* switch to reserved LPID */
1594         isync
1595         li      r0,0
1596         stw     r0,0(r3)                /* drop guest tlbie_lock */
1597
1598         /* invalidate the whole TLB */
1599         li      r0,256
1600         mtctr   r0
1601         li      r6,0
1602 25:     tlbiel  r6
1603         addi    r6,r6,0x1000
1604         bdnz    25b
1605         ptesync
1606
1607         /* take native_tlbie_lock */
1608         ld      r3,toc_tlbie_lock@toc(2)
1609 24:     lwarx   r0,0,r3
1610         cmpwi   r0,0
1611         bne     24b
1612         stwcx.  r8,0,r3
1613         bne     24b
1614         isync
1615
1616         ld      r6,KVM_HOST_SDR1(r4)
1617         mtspr   SPRN_SDR1,r6            /* switch to host page table */
1618
1619         /* Set up host HID4 value */
1620         sync
1621         mtspr   SPRN_HID4,r7
1622         isync
1623         li      r0,0
1624         stw     r0,0(r3)                /* drop native_tlbie_lock */
1625
1626         lis     r8,0x7fff               /* MAX_INT@h */
1627         mtspr   SPRN_HDEC,r8
1628
1629         /* Disable HDEC interrupts */
1630         mfspr   r0,SPRN_HID0
1631         li      r3,0
1632         rldimi  r0,r3, HID0_HDICE_SH, 64-HID0_HDICE_SH-1
1633         sync
1634         mtspr   SPRN_HID0,r0
1635         mfspr   r0,SPRN_HID0
1636         mfspr   r0,SPRN_HID0
1637         mfspr   r0,SPRN_HID0
1638         mfspr   r0,SPRN_HID0
1639         mfspr   r0,SPRN_HID0
1640         mfspr   r0,SPRN_HID0
1641
1642         /* load host SLB entries */
1643 33:     ld      r8,PACA_SLBSHADOWPTR(r13)
1644
1645         .rept   SLB_NUM_BOLTED
1646         ld      r5,SLBSHADOW_SAVEAREA(r8)
1647         ld      r6,SLBSHADOW_SAVEAREA+8(r8)
1648         andis.  r7,r5,SLB_ESID_V@h
1649         beq     1f
1650         slbmte  r6,r5
1651 1:      addi    r8,r8,16
1652         .endr
1653
1654         /* Unset guest mode */
1655         li      r0, KVM_GUEST_MODE_NONE
1656         stb     r0, HSTATE_IN_GUEST(r13)
1657
1658         ld      r0, 112+PPC_LR_STKOFF(r1)
1659         addi    r1, r1, 112
1660         mtlr    r0
1661         blr
1662
1663 /*
1664  * Check whether an HDSI is an HPTE not found fault or something else.
1665  * If it is an HPTE not found fault that is due to the guest accessing
1666  * a page that they have mapped but which we have paged out, then
1667  * we continue on with the guest exit path.  In all other cases,
1668  * reflect the HDSI to the guest as a DSI.
1669  */
1670 kvmppc_hdsi:
1671         mfspr   r4, SPRN_HDAR
1672         mfspr   r6, SPRN_HDSISR
1673         /* HPTE not found fault or protection fault? */
1674         andis.  r0, r6, (DSISR_NOHPTE | DSISR_PROTFAULT)@h
1675         beq     1f                      /* if not, send it to the guest */
1676         andi.   r0, r11, MSR_DR         /* data relocation enabled? */
1677         beq     3f
1678         clrrdi  r0, r4, 28
1679         PPC_SLBFEE_DOT(R5, R0)          /* if so, look up SLB */
1680         bne     1f                      /* if no SLB entry found */
1681 4:      std     r4, VCPU_FAULT_DAR(r9)
1682         stw     r6, VCPU_FAULT_DSISR(r9)
1683
1684         /* Search the hash table. */
1685         mr      r3, r9                  /* vcpu pointer */
1686         li      r7, 1                   /* data fault */
1687         bl      .kvmppc_hpte_hv_fault
1688         ld      r9, HSTATE_KVM_VCPU(r13)
1689         ld      r10, VCPU_PC(r9)
1690         ld      r11, VCPU_MSR(r9)
1691         li      r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1692         cmpdi   r3, 0                   /* retry the instruction */
1693         beq     6f
1694         cmpdi   r3, -1                  /* handle in kernel mode */
1695         beq     guest_exit_cont
1696         cmpdi   r3, -2                  /* MMIO emulation; need instr word */
1697         beq     2f
1698
1699         /* Synthesize a DSI for the guest */
1700         ld      r4, VCPU_FAULT_DAR(r9)
1701         mr      r6, r3
1702 1:      mtspr   SPRN_DAR, r4
1703         mtspr   SPRN_DSISR, r6
1704         mtspr   SPRN_SRR0, r10
1705         mtspr   SPRN_SRR1, r11
1706         li      r10, BOOK3S_INTERRUPT_DATA_STORAGE
1707         bl      kvmppc_msr_interrupt
1708 fast_interrupt_c_return:
1709 6:      ld      r7, VCPU_CTR(r9)
1710         lwz     r8, VCPU_XER(r9)
1711         mtctr   r7
1712         mtxer   r8
1713         mr      r4, r9
1714         b       fast_guest_return
1715
1716 3:      ld      r5, VCPU_KVM(r9)        /* not relocated, use VRMA */
1717         ld      r5, KVM_VRMA_SLB_V(r5)
1718         b       4b
1719
1720         /* If this is for emulated MMIO, load the instruction word */
1721 2:      li      r8, KVM_INST_FETCH_FAILED       /* In case lwz faults */
1722
1723         /* Set guest mode to 'jump over instruction' so if lwz faults
1724          * we'll just continue at the next IP. */
1725         li      r0, KVM_GUEST_MODE_SKIP
1726         stb     r0, HSTATE_IN_GUEST(r13)
1727
1728         /* Do the access with MSR:DR enabled */
1729         mfmsr   r3
1730         ori     r4, r3, MSR_DR          /* Enable paging for data */
1731         mtmsrd  r4
1732         lwz     r8, 0(r10)
1733         mtmsrd  r3
1734
1735         /* Store the result */
1736         stw     r8, VCPU_LAST_INST(r9)
1737
1738         /* Unset guest mode. */
1739         li      r0, KVM_GUEST_MODE_HOST_HV
1740         stb     r0, HSTATE_IN_GUEST(r13)
1741         b       guest_exit_cont
1742
1743 /*
1744  * Similarly for an HISI, reflect it to the guest as an ISI unless
1745  * it is an HPTE not found fault for a page that we have paged out.
1746  */
1747 kvmppc_hisi:
1748         andis.  r0, r11, SRR1_ISI_NOPT@h
1749         beq     1f
1750         andi.   r0, r11, MSR_IR         /* instruction relocation enabled? */
1751         beq     3f
1752         clrrdi  r0, r10, 28
1753         PPC_SLBFEE_DOT(R5, R0)          /* if so, look up SLB */
1754         bne     1f                      /* if no SLB entry found */
1755 4:
1756         /* Search the hash table. */
1757         mr      r3, r9                  /* vcpu pointer */
1758         mr      r4, r10
1759         mr      r6, r11
1760         li      r7, 0                   /* instruction fault */
1761         bl      .kvmppc_hpte_hv_fault
1762         ld      r9, HSTATE_KVM_VCPU(r13)
1763         ld      r10, VCPU_PC(r9)
1764         ld      r11, VCPU_MSR(r9)
1765         li      r12, BOOK3S_INTERRUPT_H_INST_STORAGE
1766         cmpdi   r3, 0                   /* retry the instruction */
1767         beq     fast_interrupt_c_return
1768         cmpdi   r3, -1                  /* handle in kernel mode */
1769         beq     guest_exit_cont
1770
1771         /* Synthesize an ISI for the guest */
1772         mr      r11, r3
1773 1:      mtspr   SPRN_SRR0, r10
1774         mtspr   SPRN_SRR1, r11
1775         li      r10, BOOK3S_INTERRUPT_INST_STORAGE
1776         bl      kvmppc_msr_interrupt
1777         b       fast_interrupt_c_return
1778
1779 3:      ld      r6, VCPU_KVM(r9)        /* not relocated, use VRMA */
1780         ld      r5, KVM_VRMA_SLB_V(r6)
1781         b       4b
1782
1783 /*
1784  * Try to handle an hcall in real mode.
1785  * Returns to the guest if we handle it, or continues on up to
1786  * the kernel if we can't (i.e. if we don't have a handler for
1787  * it, or if the handler returns H_TOO_HARD).
1788  */
1789         .globl  hcall_try_real_mode
1790 hcall_try_real_mode:
1791         ld      r3,VCPU_GPR(R3)(r9)
1792         andi.   r0,r11,MSR_PR
1793         /* sc 1 from userspace - reflect to guest syscall */
1794         bne     sc_1_fast_return
1795         clrrdi  r3,r3,2
1796         cmpldi  r3,hcall_real_table_end - hcall_real_table
1797         bge     guest_exit_cont
1798         LOAD_REG_ADDR(r4, hcall_real_table)
1799         lwax    r3,r3,r4
1800         cmpwi   r3,0
1801         beq     guest_exit_cont
1802         add     r3,r3,r4
1803         mtctr   r3
1804         mr      r3,r9           /* get vcpu pointer */
1805         ld      r4,VCPU_GPR(R4)(r9)
1806         bctrl
1807         cmpdi   r3,H_TOO_HARD
1808         beq     hcall_real_fallback
1809         ld      r4,HSTATE_KVM_VCPU(r13)
1810         std     r3,VCPU_GPR(R3)(r4)
1811         ld      r10,VCPU_PC(r4)
1812         ld      r11,VCPU_MSR(r4)
1813         b       fast_guest_return
1814
1815 sc_1_fast_return:
1816         mtspr   SPRN_SRR0,r10
1817         mtspr   SPRN_SRR1,r11
1818         li      r10, BOOK3S_INTERRUPT_SYSCALL
1819         bl      kvmppc_msr_interrupt
1820         mr      r4,r9
1821         b       fast_guest_return
1822
1823         /* We've attempted a real mode hcall, but it's punted it back
1824          * to userspace.  We need to restore some clobbered volatiles
1825          * before resuming the pass-it-to-qemu path */
1826 hcall_real_fallback:
1827         li      r12,BOOK3S_INTERRUPT_SYSCALL
1828         ld      r9, HSTATE_KVM_VCPU(r13)
1829
1830         b       guest_exit_cont
1831
1832         .globl  hcall_real_table
1833 hcall_real_table:
1834         .long   0               /* 0 - unused */
1835         .long   .kvmppc_h_remove - hcall_real_table
1836         .long   .kvmppc_h_enter - hcall_real_table
1837         .long   .kvmppc_h_read - hcall_real_table
1838         .long   0               /* 0x10 - H_CLEAR_MOD */
1839         .long   0               /* 0x14 - H_CLEAR_REF */
1840         .long   .kvmppc_h_protect - hcall_real_table
1841         .long   .kvmppc_h_get_tce - hcall_real_table
1842         .long   .kvmppc_h_put_tce - hcall_real_table
1843         .long   0               /* 0x24 - H_SET_SPRG0 */
1844         .long   .kvmppc_h_set_dabr - hcall_real_table
1845         .long   0               /* 0x2c */
1846         .long   0               /* 0x30 */
1847         .long   0               /* 0x34 */
1848         .long   0               /* 0x38 */
1849         .long   0               /* 0x3c */
1850         .long   0               /* 0x40 */
1851         .long   0               /* 0x44 */
1852         .long   0               /* 0x48 */
1853         .long   0               /* 0x4c */
1854         .long   0               /* 0x50 */
1855         .long   0               /* 0x54 */
1856         .long   0               /* 0x58 */
1857         .long   0               /* 0x5c */
1858         .long   0               /* 0x60 */
1859 #ifdef CONFIG_KVM_XICS
1860         .long   .kvmppc_rm_h_eoi - hcall_real_table
1861         .long   .kvmppc_rm_h_cppr - hcall_real_table
1862         .long   .kvmppc_rm_h_ipi - hcall_real_table
1863         .long   0               /* 0x70 - H_IPOLL */
1864         .long   .kvmppc_rm_h_xirr - hcall_real_table
1865 #else
1866         .long   0               /* 0x64 - H_EOI */
1867         .long   0               /* 0x68 - H_CPPR */
1868         .long   0               /* 0x6c - H_IPI */
1869         .long   0               /* 0x70 - H_IPOLL */
1870         .long   0               /* 0x74 - H_XIRR */
1871 #endif
1872         .long   0               /* 0x78 */
1873         .long   0               /* 0x7c */
1874         .long   0               /* 0x80 */
1875         .long   0               /* 0x84 */
1876         .long   0               /* 0x88 */
1877         .long   0               /* 0x8c */
1878         .long   0               /* 0x90 */
1879         .long   0               /* 0x94 */
1880         .long   0               /* 0x98 */
1881         .long   0               /* 0x9c */
1882         .long   0               /* 0xa0 */
1883         .long   0               /* 0xa4 */
1884         .long   0               /* 0xa8 */
1885         .long   0               /* 0xac */
1886         .long   0               /* 0xb0 */
1887         .long   0               /* 0xb4 */
1888         .long   0               /* 0xb8 */
1889         .long   0               /* 0xbc */
1890         .long   0               /* 0xc0 */
1891         .long   0               /* 0xc4 */
1892         .long   0               /* 0xc8 */
1893         .long   0               /* 0xcc */
1894         .long   0               /* 0xd0 */
1895         .long   0               /* 0xd4 */
1896         .long   0               /* 0xd8 */
1897         .long   0               /* 0xdc */
1898         .long   .kvmppc_h_cede - hcall_real_table
1899         .long   0               /* 0xe4 */
1900         .long   0               /* 0xe8 */
1901         .long   0               /* 0xec */
1902         .long   0               /* 0xf0 */
1903         .long   0               /* 0xf4 */
1904         .long   0               /* 0xf8 */
1905         .long   0               /* 0xfc */
1906         .long   0               /* 0x100 */
1907         .long   0               /* 0x104 */
1908         .long   0               /* 0x108 */
1909         .long   0               /* 0x10c */
1910         .long   0               /* 0x110 */
1911         .long   0               /* 0x114 */
1912         .long   0               /* 0x118 */
1913         .long   0               /* 0x11c */
1914         .long   0               /* 0x120 */
1915         .long   .kvmppc_h_bulk_remove - hcall_real_table
1916         .long   0               /* 0x128 */
1917         .long   0               /* 0x12c */
1918         .long   0               /* 0x130 */
1919         .long   .kvmppc_h_set_xdabr - hcall_real_table
1920 hcall_real_table_end:
1921
1922 ignore_hdec:
1923         mr      r4,r9
1924         b       fast_guest_return
1925
1926 _GLOBAL(kvmppc_h_set_xdabr)
1927         andi.   r0, r5, DABRX_USER | DABRX_KERNEL
1928         beq     6f
1929         li      r0, DABRX_USER | DABRX_KERNEL | DABRX_BTI
1930         andc.   r0, r5, r0
1931         beq     3f
1932 6:      li      r3, H_PARAMETER
1933         blr
1934
1935 _GLOBAL(kvmppc_h_set_dabr)
1936         li      r5, DABRX_USER | DABRX_KERNEL
1937 3:
1938 BEGIN_FTR_SECTION
1939         b       2f
1940 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1941         std     r4,VCPU_DABR(r3)
1942         stw     r5, VCPU_DABRX(r3)
1943         mtspr   SPRN_DABRX, r5
1944         /* Work around P7 bug where DABR can get corrupted on mtspr */
1945 1:      mtspr   SPRN_DABR,r4
1946         mfspr   r5, SPRN_DABR
1947         cmpd    r4, r5
1948         bne     1b
1949         isync
1950         li      r3,0
1951         blr
1952
1953         /* Emulate H_SET_DABR/X on P8 for the sake of compat mode guests */
1954 2:      rlwimi  r5, r4, 5, DAWRX_DR | DAWRX_DW
1955         rlwimi  r5, r4, 1, DAWRX_WT
1956         clrrdi  r4, r4, 3
1957         std     r4, VCPU_DAWR(r3)
1958         std     r5, VCPU_DAWRX(r3)
1959         mtspr   SPRN_DAWR, r4
1960         mtspr   SPRN_DAWRX, r5
1961         li      r3, 0
1962         blr
1963
1964 _GLOBAL(kvmppc_h_cede)
1965         ori     r11,r11,MSR_EE
1966         std     r11,VCPU_MSR(r3)
1967         li      r0,1
1968         stb     r0,VCPU_CEDED(r3)
1969         sync                    /* order setting ceded vs. testing prodded */
1970         lbz     r5,VCPU_PRODDED(r3)
1971         cmpwi   r5,0
1972         bne     kvm_cede_prodded
1973         li      r0,0            /* set trap to 0 to say hcall is handled */
1974         stw     r0,VCPU_TRAP(r3)
1975         li      r0,H_SUCCESS
1976         std     r0,VCPU_GPR(R3)(r3)
1977 BEGIN_FTR_SECTION
1978         b       kvm_cede_exit   /* just send it up to host on 970 */
1979 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_206)
1980
1981         /*
1982          * Set our bit in the bitmask of napping threads unless all the
1983          * other threads are already napping, in which case we send this
1984          * up to the host.
1985          */
1986         ld      r5,HSTATE_KVM_VCORE(r13)
1987         lbz     r6,HSTATE_PTID(r13)
1988         lwz     r8,VCORE_ENTRY_EXIT(r5)
1989         clrldi  r8,r8,56
1990         li      r0,1
1991         sld     r0,r0,r6
1992         addi    r6,r5,VCORE_NAPPING_THREADS
1993 31:     lwarx   r4,0,r6
1994         or      r4,r4,r0
1995         PPC_POPCNTW(R7,R4)
1996         cmpw    r7,r8
1997         bge     kvm_cede_exit
1998         stwcx.  r4,0,r6
1999         bne     31b
2000         /* order napping_threads update vs testing entry_exit_count */
2001         isync
2002         li      r0,NAPPING_CEDE
2003         stb     r0,HSTATE_NAPPING(r13)
2004         lwz     r7,VCORE_ENTRY_EXIT(r5)
2005         cmpwi   r7,0x100
2006         bge     33f             /* another thread already exiting */
2007
2008 /*
2009  * Although not specifically required by the architecture, POWER7
2010  * preserves the following registers in nap mode, even if an SMT mode
2011  * switch occurs: SLB entries, PURR, SPURR, AMOR, UAMOR, AMR, SPRG0-3,
2012  * DAR, DSISR, DABR, DABRX, DSCR, PMCx, MMCRx, SIAR, SDAR.
2013  */
2014         /* Save non-volatile GPRs */
2015         std     r14, VCPU_GPR(R14)(r3)
2016         std     r15, VCPU_GPR(R15)(r3)
2017         std     r16, VCPU_GPR(R16)(r3)
2018         std     r17, VCPU_GPR(R17)(r3)
2019         std     r18, VCPU_GPR(R18)(r3)
2020         std     r19, VCPU_GPR(R19)(r3)
2021         std     r20, VCPU_GPR(R20)(r3)
2022         std     r21, VCPU_GPR(R21)(r3)
2023         std     r22, VCPU_GPR(R22)(r3)
2024         std     r23, VCPU_GPR(R23)(r3)
2025         std     r24, VCPU_GPR(R24)(r3)
2026         std     r25, VCPU_GPR(R25)(r3)
2027         std     r26, VCPU_GPR(R26)(r3)
2028         std     r27, VCPU_GPR(R27)(r3)
2029         std     r28, VCPU_GPR(R28)(r3)
2030         std     r29, VCPU_GPR(R29)(r3)
2031         std     r30, VCPU_GPR(R30)(r3)
2032         std     r31, VCPU_GPR(R31)(r3)
2033
2034         /* save FP state */
2035         bl      kvmppc_save_fp
2036
2037         /*
2038          * Take a nap until a decrementer or external or doobell interrupt
2039          * occurs, with PECE1, PECE0 and PECEDP set in LPCR
2040          */
2041         li      r0,1
2042         stb     r0,HSTATE_HWTHREAD_REQ(r13)
2043         mfspr   r5,SPRN_LPCR
2044         ori     r5,r5,LPCR_PECE0 | LPCR_PECE1
2045 BEGIN_FTR_SECTION
2046         oris    r5,r5,LPCR_PECEDP@h
2047 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
2048         mtspr   SPRN_LPCR,r5
2049         isync
2050         li      r0, 0
2051         std     r0, HSTATE_SCRATCH0(r13)
2052         ptesync
2053         ld      r0, HSTATE_SCRATCH0(r13)
2054 1:      cmpd    r0, r0
2055         bne     1b
2056         nap
2057         b       .
2058
2059 33:     mr      r4, r3
2060         li      r3, 0
2061         li      r12, 0
2062         b       34f
2063
2064 kvm_end_cede:
2065         /* get vcpu pointer */
2066         ld      r4, HSTATE_KVM_VCPU(r13)
2067
2068         /* Woken by external or decrementer interrupt */
2069         ld      r1, HSTATE_HOST_R1(r13)
2070
2071         /* load up FP state */
2072         bl      kvmppc_load_fp
2073
2074         /* Load NV GPRS */
2075         ld      r14, VCPU_GPR(R14)(r4)
2076         ld      r15, VCPU_GPR(R15)(r4)
2077         ld      r16, VCPU_GPR(R16)(r4)
2078         ld      r17, VCPU_GPR(R17)(r4)
2079         ld      r18, VCPU_GPR(R18)(r4)
2080         ld      r19, VCPU_GPR(R19)(r4)
2081         ld      r20, VCPU_GPR(R20)(r4)
2082         ld      r21, VCPU_GPR(R21)(r4)
2083         ld      r22, VCPU_GPR(R22)(r4)
2084         ld      r23, VCPU_GPR(R23)(r4)
2085         ld      r24, VCPU_GPR(R24)(r4)
2086         ld      r25, VCPU_GPR(R25)(r4)
2087         ld      r26, VCPU_GPR(R26)(r4)
2088         ld      r27, VCPU_GPR(R27)(r4)
2089         ld      r28, VCPU_GPR(R28)(r4)
2090         ld      r29, VCPU_GPR(R29)(r4)
2091         ld      r30, VCPU_GPR(R30)(r4)
2092         ld      r31, VCPU_GPR(R31)(r4)
2093  
2094         /* Check the wake reason in SRR1 to see why we got here */
2095         bl      kvmppc_check_wake_reason
2096
2097         /* clear our bit in vcore->napping_threads */
2098 34:     ld      r5,HSTATE_KVM_VCORE(r13)
2099         lbz     r7,HSTATE_PTID(r13)
2100         li      r0,1
2101         sld     r0,r0,r7
2102         addi    r6,r5,VCORE_NAPPING_THREADS
2103 32:     lwarx   r7,0,r6
2104         andc    r7,r7,r0
2105         stwcx.  r7,0,r6
2106         bne     32b
2107         li      r0,0
2108         stb     r0,HSTATE_NAPPING(r13)
2109
2110         /* See if the wake reason means we need to exit */
2111         stw     r12, VCPU_TRAP(r4)
2112         mr      r9, r4
2113         cmpdi   r3, 0
2114         bgt     guest_exit_cont
2115
2116         /* see if any other thread is already exiting */
2117         lwz     r0,VCORE_ENTRY_EXIT(r5)
2118         cmpwi   r0,0x100
2119         bge     guest_exit_cont
2120
2121         b       kvmppc_cede_reentry     /* if not go back to guest */
2122
2123         /* cede when already previously prodded case */
2124 kvm_cede_prodded:
2125         li      r0,0
2126         stb     r0,VCPU_PRODDED(r3)
2127         sync                    /* order testing prodded vs. clearing ceded */
2128         stb     r0,VCPU_CEDED(r3)
2129         li      r3,H_SUCCESS
2130         blr
2131
2132         /* we've ceded but we want to give control to the host */
2133 kvm_cede_exit:
2134         b       hcall_real_fallback
2135
2136         /* Try to handle a machine check in real mode */
2137 machine_check_realmode:
2138         mr      r3, r9          /* get vcpu pointer */
2139         bl      .kvmppc_realmode_machine_check
2140         nop
2141         cmpdi   r3, 0           /* continue exiting from guest? */
2142         ld      r9, HSTATE_KVM_VCPU(r13)
2143         li      r12, BOOK3S_INTERRUPT_MACHINE_CHECK
2144         beq     mc_cont
2145         /* If not, deliver a machine check.  SRR0/1 are already set */
2146         li      r10, BOOK3S_INTERRUPT_MACHINE_CHECK
2147         bl      kvmppc_msr_interrupt
2148         b       fast_interrupt_c_return
2149
2150 /*
2151  * Check the reason we woke from nap, and take appropriate action.
2152  * Returns:
2153  *      0 if nothing needs to be done
2154  *      1 if something happened that needs to be handled by the host
2155  *      -1 if there was a guest wakeup (IPI)
2156  *
2157  * Also sets r12 to the interrupt vector for any interrupt that needs
2158  * to be handled now by the host (0x500 for external interrupt), or zero.
2159  */
2160 kvmppc_check_wake_reason:
2161         mfspr   r6, SPRN_SRR1
2162 BEGIN_FTR_SECTION
2163         rlwinm  r6, r6, 45-31, 0xf      /* extract wake reason field (P8) */
2164 FTR_SECTION_ELSE
2165         rlwinm  r6, r6, 45-31, 0xe      /* P7 wake reason field is 3 bits */
2166 ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_207S)
2167         cmpwi   r6, 8                   /* was it an external interrupt? */
2168         li      r12, BOOK3S_INTERRUPT_EXTERNAL
2169         beq     kvmppc_read_intr        /* if so, see what it was */
2170         li      r3, 0
2171         li      r12, 0
2172         cmpwi   r6, 6                   /* was it the decrementer? */
2173         beq     0f
2174 BEGIN_FTR_SECTION
2175         cmpwi   r6, 5                   /* privileged doorbell? */
2176         beq     0f
2177         cmpwi   r6, 3                   /* hypervisor doorbell? */
2178         beq     3f
2179 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
2180         li      r3, 1                   /* anything else, return 1 */
2181 0:      blr
2182
2183         /* hypervisor doorbell */
2184 3:      li      r12, BOOK3S_INTERRUPT_H_DOORBELL
2185         li      r3, 1
2186         blr
2187
2188 /*
2189  * Determine what sort of external interrupt is pending (if any).
2190  * Returns:
2191  *      0 if no interrupt is pending
2192  *      1 if an interrupt is pending that needs to be handled by the host
2193  *      -1 if there was a guest wakeup IPI (which has now been cleared)
2194  */
2195 kvmppc_read_intr:
2196         /* see if a host IPI is pending */
2197         li      r3, 1
2198         lbz     r0, HSTATE_HOST_IPI(r13)
2199         cmpwi   r0, 0
2200         bne     1f
2201
2202         /* Now read the interrupt from the ICP */
2203         ld      r6, HSTATE_XICS_PHYS(r13)
2204         li      r7, XICS_XIRR
2205         cmpdi   r6, 0
2206         beq-    1f
2207         lwzcix  r0, r6, r7
2208         rlwinm. r3, r0, 0, 0xffffff
2209         sync
2210         beq     1f                      /* if nothing pending in the ICP */
2211
2212         /* We found something in the ICP...
2213          *
2214          * If it's not an IPI, stash it in the PACA and return to
2215          * the host, we don't (yet) handle directing real external
2216          * interrupts directly to the guest
2217          */
2218         cmpwi   r3, XICS_IPI            /* if there is, is it an IPI? */
2219         bne     42f
2220
2221         /* It's an IPI, clear the MFRR and EOI it */
2222         li      r3, 0xff
2223         li      r8, XICS_MFRR
2224         stbcix  r3, r6, r8              /* clear the IPI */
2225         stwcix  r0, r6, r7              /* EOI it */
2226         sync
2227
2228         /* We need to re-check host IPI now in case it got set in the
2229          * meantime. If it's clear, we bounce the interrupt to the
2230          * guest
2231          */
2232         lbz     r0, HSTATE_HOST_IPI(r13)
2233         cmpwi   r0, 0
2234         bne-    43f
2235
2236         /* OK, it's an IPI for us */
2237         li      r3, -1
2238 1:      blr
2239
2240 42:     /* It's not an IPI and it's for the host, stash it in the PACA
2241          * before exit, it will be picked up by the host ICP driver
2242          */
2243         stw     r0, HSTATE_SAVED_XIRR(r13)
2244         li      r3, 1
2245         b       1b
2246
2247 43:     /* We raced with the host, we need to resend that IPI, bummer */
2248         li      r0, IPI_PRIORITY
2249         stbcix  r0, r6, r8              /* set the IPI */
2250         sync
2251         li      r3, 1
2252         b       1b
2253
2254 /*
2255  * Save away FP, VMX and VSX registers.
2256  * r3 = vcpu pointer
2257  * N.B. r30 and r31 are volatile across this function,
2258  * thus it is not callable from C.
2259  */
2260 kvmppc_save_fp:
2261         mflr    r30
2262         mr      r31,r3
2263         mfmsr   r5
2264         ori     r8,r5,MSR_FP
2265 #ifdef CONFIG_ALTIVEC
2266 BEGIN_FTR_SECTION
2267         oris    r8,r8,MSR_VEC@h
2268 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2269 #endif
2270 #ifdef CONFIG_VSX
2271 BEGIN_FTR_SECTION
2272         oris    r8,r8,MSR_VSX@h
2273 END_FTR_SECTION_IFSET(CPU_FTR_VSX)
2274 #endif
2275         mtmsrd  r8
2276         isync
2277         addi    r3,r3,VCPU_FPRS
2278         bl      .store_fp_state
2279 #ifdef CONFIG_ALTIVEC
2280 BEGIN_FTR_SECTION
2281         addi    r3,r31,VCPU_VRS
2282         bl      .store_vr_state
2283 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2284 #endif
2285         mfspr   r6,SPRN_VRSAVE
2286         stw     r6,VCPU_VRSAVE(r31)
2287         mtlr    r30
2288         blr
2289
2290 /*
2291  * Load up FP, VMX and VSX registers
2292  * r4 = vcpu pointer
2293  * N.B. r30 and r31 are volatile across this function,
2294  * thus it is not callable from C.
2295  */
2296 kvmppc_load_fp:
2297         mflr    r30
2298         mr      r31,r4
2299         mfmsr   r9
2300         ori     r8,r9,MSR_FP
2301 #ifdef CONFIG_ALTIVEC
2302 BEGIN_FTR_SECTION
2303         oris    r8,r8,MSR_VEC@h
2304 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2305 #endif
2306 #ifdef CONFIG_VSX
2307 BEGIN_FTR_SECTION
2308         oris    r8,r8,MSR_VSX@h
2309 END_FTR_SECTION_IFSET(CPU_FTR_VSX)
2310 #endif
2311         mtmsrd  r8
2312         isync
2313         addi    r3,r4,VCPU_FPRS
2314         bl      .load_fp_state
2315 #ifdef CONFIG_ALTIVEC
2316 BEGIN_FTR_SECTION
2317         addi    r3,r31,VCPU_VRS
2318         bl      .load_vr_state
2319 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2320 #endif
2321         lwz     r7,VCPU_VRSAVE(r31)
2322         mtspr   SPRN_VRSAVE,r7
2323         mtlr    r30
2324         mr      r4,r31
2325         blr
2326
2327 /*
2328  * We come here if we get any exception or interrupt while we are
2329  * executing host real mode code while in guest MMU context.
2330  * For now just spin, but we should do something better.
2331  */
2332 kvmppc_bad_host_intr:
2333         b       .
2334
2335 /*
2336  * This mimics the MSR transition on IRQ delivery.  The new guest MSR is taken
2337  * from VCPU_INTR_MSR and is modified based on the required TM state changes.
2338  *   r11 has the guest MSR value (in/out)
2339  *   r9 has a vcpu pointer (in)
2340  *   r0 is used as a scratch register
2341  */
2342 kvmppc_msr_interrupt:
2343         rldicl  r0, r11, 64 - MSR_TS_S_LG, 62
2344         cmpwi   r0, 2 /* Check if we are in transactional state..  */
2345         ld      r11, VCPU_INTR_MSR(r9)
2346         bne     1f
2347         /* ... if transactional, change to suspended */
2348         li      r0, 1
2349 1:      rldimi  r11, r0, MSR_TS_S_LG, 63 - MSR_TS_T_LG
2350         blr
2351
2352 /*
2353  * This works around a hardware bug on POWER8E processors, where
2354  * writing a 1 to the MMCR0[PMAO] bit doesn't generate a
2355  * performance monitor interrupt.  Instead, when we need to have
2356  * an interrupt pending, we have to arrange for a counter to overflow.
2357  */
2358 kvmppc_fix_pmao:
2359         li      r3, 0
2360         mtspr   SPRN_MMCR2, r3
2361         lis     r3, (MMCR0_PMXE | MMCR0_FCECE)@h
2362         ori     r3, r3, MMCR0_PMCjCE | MMCR0_C56RUN
2363         mtspr   SPRN_MMCR0, r3
2364         lis     r3, 0x7fff
2365         ori     r3, r3, 0xffff
2366         mtspr   SPRN_PMC6, r3
2367         isync
2368         blr