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