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