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.
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.
11 * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
13 * Derived from book3s_rmhandlers.S and other files, which are:
15 * Copyright SUSE Linux Products GmbH 2009
17 * Authors: Alexander Graf <agraf@suse.de>
20 #include <asm/ppc_asm.h>
21 #include <asm/kvm_asm.h>
25 #include <asm/ptrace.h>
26 #include <asm/hvcall.h>
27 #include <asm/asm-offsets.h>
28 #include <asm/exception-64s.h>
30 /*****************************************************************************
32 * Real Mode handlers that need to be in the linear mapping *
34 ****************************************************************************/
36 .globl kvmppc_skip_interrupt
37 kvmppc_skip_interrupt:
45 .globl kvmppc_skip_Hinterrupt
46 kvmppc_skip_Hinterrupt:
55 * Call kvmppc_hv_entry in real mode.
56 * Must be called with interrupts hard-disabled.
60 * LR = return address to continue at after eventually re-enabling MMU
62 _GLOBAL(kvmppc_hv_entry_trampoline)
64 LOAD_REG_ADDR(r5, kvmppc_hv_entry)
69 mtmsrd r0,1 /* clear RI in MSR */
75 #define VCPU_GPR(n) (VCPU_GPRS + (n * ULONG_SIZE))
77 /******************************************************************************
81 *****************************************************************************/
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.
91 .globl kvm_start_guest
93 ld r1,PACAEMERGSP(r13)
94 subi r1,r1,STACK_FRAME_OVERHEAD
97 /* were we napping due to cede? */
98 lbz r0,HSTATE_NAPPING(r13)
102 /* get vcpu pointer */
103 ld r4, HSTATE_KVM_VCPU(r13)
105 /* We got here with an IPI; clear it */
106 ld r5, HSTATE_XICS_PHYS(r13)
110 lwzcix r8, r5, r7 /* ack the interrupt */
112 stbcix r0, r5, r6 /* clear it */
113 stwcix r8, r5, r7 /* EOI it */
115 .global kvmppc_hv_entry
124 * all other volatile GPRS = free
127 std r0, HSTATE_VMHANDLER(r13)
129 ld r14, VCPU_GPR(r14)(r4)
130 ld r15, VCPU_GPR(r15)(r4)
131 ld r16, VCPU_GPR(r16)(r4)
132 ld r17, VCPU_GPR(r17)(r4)
133 ld r18, VCPU_GPR(r18)(r4)
134 ld r19, VCPU_GPR(r19)(r4)
135 ld r20, VCPU_GPR(r20)(r4)
136 ld r21, VCPU_GPR(r21)(r4)
137 ld r22, VCPU_GPR(r22)(r4)
138 ld r23, VCPU_GPR(r23)(r4)
139 ld r24, VCPU_GPR(r24)(r4)
140 ld r25, VCPU_GPR(r25)(r4)
141 ld r26, VCPU_GPR(r26)(r4)
142 ld r27, VCPU_GPR(r27)(r4)
143 ld r28, VCPU_GPR(r28)(r4)
144 ld r29, VCPU_GPR(r29)(r4)
145 ld r30, VCPU_GPR(r30)(r4)
146 ld r31, VCPU_GPR(r31)(r4)
148 /* Load guest PMU registers */
149 /* R4 is live here (vcpu pointer) */
151 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
152 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
154 lwz r3, VCPU_PMC(r4) /* always load up guest PMU registers */
155 lwz r5, VCPU_PMC + 4(r4) /* to prevent information leak */
156 lwz r6, VCPU_PMC + 8(r4)
157 lwz r7, VCPU_PMC + 12(r4)
158 lwz r8, VCPU_PMC + 16(r4)
159 lwz r9, VCPU_PMC + 20(r4)
161 lwz r10, VCPU_PMC + 24(r4)
162 lwz r11, VCPU_PMC + 28(r4)
163 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
173 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
175 ld r5, VCPU_MMCR + 8(r4)
176 ld r6, VCPU_MMCR + 16(r4)
182 /* Load up FP, VMX and VSX registers */
186 /* Switch DSCR to guest value */
189 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
192 * Set the decrementer to the guest decrementer.
194 ld r8,VCPU_DEC_EXPIRES(r4)
200 ld r5, VCPU_SPRG0(r4)
201 ld r6, VCPU_SPRG1(r4)
202 ld r7, VCPU_SPRG2(r4)
203 ld r8, VCPU_SPRG3(r4)
209 /* Save R1 in the PACA */
210 std r1, HSTATE_HOST_R1(r13)
212 /* Increment yield count if they have a VPA */
216 lwz r5, LPPACA_YIELDCOUNT(r3)
218 stw r5, LPPACA_YIELDCOUNT(r3)
220 /* Load up DAR and DSISR */
222 lwz r6, VCPU_DSISR(r4)
226 /* Set partition DABR */
233 /* Restore AMR and UAMOR, set AMOR to all 1s */
240 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
250 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
252 * POWER7 host -> guest partition switch code.
253 * We don't have to lock against concurrent tlbies,
254 * but we do have to coordinate across hardware threads.
256 /* Increment entry count iff exit count is zero. */
257 ld r5,HSTATE_KVM_VCORE(r13)
258 addi r9,r5,VCORE_ENTRY_EXIT
260 cmpwi r3,0x100 /* any threads starting to exit? */
261 bge secondary_too_late /* if so we're too late to the party */
266 /* Primary thread switches to guest partition. */
267 ld r9,VCPU_KVM(r4) /* pointer to struct kvm */
273 li r0,LPID_RSVD /* switch to reserved LPID */
276 mtspr SPRN_SDR1,r6 /* switch to partition page table */
280 stb r0,VCORE_IN_GUEST(r5) /* signal secondaries to continue */
283 /* Secondary threads wait for primary to have done partition switch */
284 20: lbz r0,VCORE_IN_GUEST(r5)
288 /* Set LPCR and RMOR. */
289 10: ld r8,KVM_LPCR(r9)
295 /* Check if HDEC expires soon */
298 li r12,BOOK3S_INTERRUPT_HV_DECREMENTER
303 * Invalidate the TLB if we could possibly have stale TLB
304 * entries for this partition on this core due to the use
306 * XXX maybe only need this on primary thread?
308 ld r9,VCPU_KVM(r4) /* pointer to struct kvm */
309 lwz r5,VCPU_VCPUID(r4)
310 lhz r6,PACAPACAINDEX(r13)
311 rldimi r6,r5,0,62 /* XXX map as if threads 1:1 p:v */
312 lhz r8,VCPU_LAST_CPU(r4)
313 sldi r7,r6,1 /* see if this is the same vcpu */
314 add r7,r7,r9 /* as last ran on this pcpu */
315 lhz r0,KVM_LAST_VCPU(r7)
316 cmpw r6,r8 /* on the same cpu core as last time? */
318 cmpw r0,r5 /* same vcpu as this core last ran? */
320 3: sth r6,VCPU_LAST_CPU(r4) /* if not, invalidate partition TLB */
321 sth r5,KVM_LAST_VCPU(r7)
324 li r7,0x800 /* IS field = 0b10 */
332 /* Save purr/spurr */
335 std r5,HSTATE_PURR(r13)
336 std r6,HSTATE_SPURR(r13)
344 * PPC970 host -> guest partition switch code.
345 * We have to lock against concurrent tlbies,
346 * using native_tlbie_lock to lock against host tlbies
347 * and kvm->arch.tlbie_lock to lock against guest tlbies.
348 * We also have to invalidate the TLB since its
349 * entries aren't tagged with the LPID.
351 30: ld r9,VCPU_KVM(r4) /* pointer to struct kvm */
353 /* first take native_tlbie_lock */
356 .tc native_tlbie_lock[TC],native_tlbie_lock
358 ld r3,toc_tlbie_lock@toc(2)
359 lwz r8,PACA_LOCK_TOKEN(r13)
367 ld r7,KVM_LPCR(r9) /* use kvm->arch.lpcr to store HID4 */
369 rotldi r0,r0,HID4_LPID5_SH /* all lpid bits in HID4 = 1 */
373 mtspr SPRN_HID4,r0 /* switch to reserved LPID */
376 stw r0,0(r3) /* drop native_tlbie_lock */
378 /* invalidate the whole TLB */
387 /* Take the guest's tlbie_lock */
388 addi r3,r9,KVM_TLBIE_LOCK
396 mtspr SPRN_SDR1,r6 /* switch to partition page table */
398 /* Set up HID4 with the guest's LPID etc. */
403 /* drop the guest's tlbie_lock */
407 /* Check if HDEC expires soon */
410 li r12,BOOK3S_INTERRUPT_HV_DECREMENTER
414 /* Enable HDEC interrupts */
417 rldimi r0,r3, HID0_HDICE_SH, 64-HID0_HDICE_SH-1
427 /* Load up guest SLB entries */
428 31: lwz r5,VCPU_SLB_MAX(r4)
433 1: ld r8,VCPU_SLB_E(r6)
436 addi r6,r6,VCPU_SLB_SIZE
440 /* Restore state of CTRL run bit; assume 1 on entry */
454 kvmppc_cede_reentry: /* r4 = vcpu, r13 = paca */
458 ld r11, VCPU_MSR(r4) /* r11 = vcpu->arch.msr & ~MSR_HV */
460 rldicl r11, r11, 63 - MSR_HV_LG, 1
461 rotldi r11, r11, 1 + MSR_HV_LG
464 /* Check if we can deliver an external or decrementer interrupt now */
465 ld r0,VCPU_PENDING_EXC(r4)
466 li r8,(1 << BOOK3S_IRQPRIO_EXTERNAL)
467 oris r8,r8,(1 << BOOK3S_IRQPRIO_EXTERNAL_LEVEL)@h
477 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
479 li r0,BOOK3S_INTERRUPT_EXTERNAL
483 li r11,(MSR_ME << 1) | 1 /* synthesize MSR_SF | MSR_ME */
489 li r0,BOOK3S_INTERRUPT_DECREMENTER
492 /* Move SRR0 and SRR1 into the respective regs */
493 5: mtspr SPRN_SRR0, r6
496 stb r0,VCPU_CEDED(r4) /* cancel cede */
502 /* Activate guest mode, so faults get handled by KVM */
503 li r9, KVM_GUEST_MODE_GUEST
504 stb r9, HSTATE_IN_GUEST(r13)
513 ld r0, VCPU_GPR(r0)(r4)
514 ld r1, VCPU_GPR(r1)(r4)
515 ld r2, VCPU_GPR(r2)(r4)
516 ld r3, VCPU_GPR(r3)(r4)
517 ld r5, VCPU_GPR(r5)(r4)
518 ld r6, VCPU_GPR(r6)(r4)
519 ld r7, VCPU_GPR(r7)(r4)
520 ld r8, VCPU_GPR(r8)(r4)
521 ld r9, VCPU_GPR(r9)(r4)
522 ld r10, VCPU_GPR(r10)(r4)
523 ld r11, VCPU_GPR(r11)(r4)
524 ld r12, VCPU_GPR(r12)(r4)
525 ld r13, VCPU_GPR(r13)(r4)
527 ld r4, VCPU_GPR(r4)(r4)
532 /******************************************************************************
536 *****************************************************************************/
539 * We come here from the first-level interrupt handlers.
541 .globl kvmppc_interrupt
545 * R12 = interrupt vector
547 * guest CR, R12 saved in shadow VCPU SCRATCH1/0
548 * guest R13 saved in SPRN_SCRATCH0
550 /* abuse host_r2 as third scratch area; we get r2 from PACATOC(r13) */
551 std r9, HSTATE_HOST_R2(r13)
552 ld r9, HSTATE_KVM_VCPU(r13)
556 std r0, VCPU_GPR(r0)(r9)
557 std r1, VCPU_GPR(r1)(r9)
558 std r2, VCPU_GPR(r2)(r9)
559 std r3, VCPU_GPR(r3)(r9)
560 std r4, VCPU_GPR(r4)(r9)
561 std r5, VCPU_GPR(r5)(r9)
562 std r6, VCPU_GPR(r6)(r9)
563 std r7, VCPU_GPR(r7)(r9)
564 std r8, VCPU_GPR(r8)(r9)
565 ld r0, HSTATE_HOST_R2(r13)
566 std r0, VCPU_GPR(r9)(r9)
567 std r10, VCPU_GPR(r10)(r9)
568 std r11, VCPU_GPR(r11)(r9)
569 ld r3, HSTATE_SCRATCH0(r13)
570 lwz r4, HSTATE_SCRATCH1(r13)
571 std r3, VCPU_GPR(r12)(r9)
574 /* Restore R1/R2 so we can handle faults */
575 ld r1, HSTATE_HOST_R1(r13)
580 std r10, VCPU_SRR0(r9)
581 std r11, VCPU_SRR1(r9)
582 andi. r0, r12, 2 /* need to read HSRR0/1? */
584 mfspr r10, SPRN_HSRR0
585 mfspr r11, SPRN_HSRR1
587 1: std r10, VCPU_PC(r9)
588 std r11, VCPU_MSR(r9)
592 std r3, VCPU_GPR(r13)(r9)
595 /* Unset guest mode */
596 li r0, KVM_GUEST_MODE_NONE
597 stb r0, HSTATE_IN_GUEST(r13)
599 stw r12,VCPU_TRAP(r9)
601 /* See if this is a leftover HDEC interrupt */
602 cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER
608 /* See if this is something we can handle in real mode */
609 cmpwi r12,BOOK3S_INTERRUPT_SYSCALL
610 beq hcall_try_real_mode
612 /* Check for mediated interrupts (could be done earlier really ...) */
614 cmpwi r12,BOOK3S_INTERRUPT_EXTERNAL
620 bne bounce_ext_interrupt
622 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
624 hcall_real_cont: /* r9 = vcpu, r12 = trap, r13 = paca */
630 std r5,VCPU_DEC_EXPIRES(r9)
632 /* Save HEIR (HV emulation assist reg) in last_inst
633 if this is an HEI (HV emulation interrupt, e40) */
636 cmpwi r12,BOOK3S_INTERRUPT_H_EMUL_ASSIST
639 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
640 11: stw r3,VCPU_LAST_INST(r9)
642 /* Save more register state */
650 stw r7, VCPU_DSISR(r9)
652 /* grab HDAR & HDSISR if HV data storage interrupt (HDSI) */
654 cmpwi r12,BOOK3S_INTERRUPT_H_DATA_STORAGE
656 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
657 7: std r6, VCPU_FAULT_DAR(r9)
658 stw r7, VCPU_FAULT_DSISR(r9)
660 /* Save guest CTRL register, set runlatch to 1 */
668 /* Read the guest SLB and save it away */
669 lwz r0,VCPU_SLB_NR(r9) /* number of entries in SLB */
675 andis. r0,r8,SLB_ESID_V@h
677 add r8,r8,r6 /* put index in */
679 std r8,VCPU_SLB_E(r7)
680 std r3,VCPU_SLB_V(r7)
681 addi r7,r7,VCPU_SLB_SIZE
685 stw r5,VCPU_SLB_MAX(r9)
688 * Save the guest PURR/SPURR
696 std r6,VCPU_SPURR(r9)
701 * Restore host PURR/SPURR and add guest times
702 * so that the time in the guest gets accounted.
704 ld r3,HSTATE_PURR(r13)
705 ld r4,HSTATE_SPURR(r13)
710 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_201)
718 hdec_soon: /* r9 = vcpu, r12 = trap, r13 = paca */
721 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
723 * POWER7 guest -> host partition switch code.
724 * We don't have to lock against tlbies but we do
725 * have to coordinate the hardware threads.
727 /* Increment the threads-exiting-guest count in the 0xff00
728 bits of vcore->entry_exit_count */
730 ld r5,HSTATE_KVM_VCORE(r13)
731 addi r6,r5,VCORE_ENTRY_EXIT
739 * At this point we have an interrupt that we have to pass
740 * up to the kernel or qemu; we can't handle it in real mode.
741 * Thus we have to do a partition switch, so we have to
742 * collect the other threads, if we are the first thread
743 * to take an interrupt. To do this, we set the HDEC to 0,
744 * which causes an HDEC interrupt in all threads within 2ns
745 * because the HDEC register is shared between all 4 threads.
746 * However, we don't need to bother if this is an HDEC
747 * interrupt, since the other threads will already be on their
748 * way here in that case.
750 cmpwi r3,0x100 /* Are we the first here? */
752 cmpwi r3,1 /* Are any other threads in the guest? */
754 cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER
760 * Send an IPI to any napping threads, since an HDEC interrupt
761 * doesn't wake CPUs up from nap.
763 lwz r3,VCORE_NAPPING_THREADS(r5)
767 andc. r3,r3,r0 /* no sense IPI'ing ourselves */
769 mulli r4,r4,PACA_SIZE /* get paca for thread 0 */
773 ld r8,HSTATE_XICS_PHYS(r6) /* get thread's XICS reg addr */
776 stbcix r0,r7,r8 /* trigger the IPI */
781 /* Secondary threads wait for primary to do partition switch */
782 43: ld r4,VCPU_KVM(r9) /* pointer to struct kvm */
783 ld r5,HSTATE_KVM_VCORE(r13)
788 13: lbz r3,VCORE_IN_GUEST(r5)
794 /* Primary thread waits for all the secondaries to exit guest */
795 15: lwz r3,VCORE_ENTRY_EXIT(r5)
802 /* Primary thread switches back to host partition */
803 ld r6,KVM_HOST_SDR1(r4)
804 lwz r7,KVM_HOST_LPID(r4)
805 li r8,LPID_RSVD /* switch to reserved LPID */
808 mtspr SPRN_SDR1,r6 /* switch to partition page table */
812 stb r0,VCORE_IN_GUEST(r5)
813 lis r8,0x7fff /* MAX_INT@h */
816 16: ld r8,KVM_HOST_LPCR(r4)
822 * PPC970 guest -> host partition switch code.
823 * We have to lock against concurrent tlbies, and
824 * we have to flush the whole TLB.
826 32: ld r4,VCPU_KVM(r9) /* pointer to struct kvm */
828 /* Take the guest's tlbie_lock */
829 lwz r8,PACA_LOCK_TOKEN(r13)
830 addi r3,r4,KVM_TLBIE_LOCK
838 ld r7,KVM_HOST_LPCR(r4) /* use kvm->arch.host_lpcr for HID4 */
840 rotldi r0,r0,HID4_LPID5_SH /* all lpid bits in HID4 = 1 */
844 mtspr SPRN_HID4,r0 /* switch to reserved LPID */
847 stw r0,0(r3) /* drop guest tlbie_lock */
849 /* invalidate the whole TLB */
858 /* take native_tlbie_lock */
859 ld r3,toc_tlbie_lock@toc(2)
867 ld r6,KVM_HOST_SDR1(r4)
868 mtspr SPRN_SDR1,r6 /* switch to host page table */
870 /* Set up host HID4 value */
875 stw r0,0(r3) /* drop native_tlbie_lock */
877 lis r8,0x7fff /* MAX_INT@h */
880 /* Disable HDEC interrupts */
883 rldimi r0,r3, HID0_HDICE_SH, 64-HID0_HDICE_SH-1
893 /* load host SLB entries */
894 33: ld r8,PACA_SLBSHADOWPTR(r13)
897 ld r5,SLBSHADOW_SAVEAREA(r8)
898 ld r6,SLBSHADOW_SAVEAREA+8(r8)
899 andis. r7,r5,SLB_ESID_V@h
905 /* Save and reset AMR and UAMOR before turning on the MMU */
910 std r6,VCPU_UAMOR(r9)
913 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
915 /* Restore host DABR and DABRX */
916 ld r5,HSTATE_DABR(r13)
921 /* Switch DSCR back to host value */
924 ld r7, HSTATE_DSCR(r13)
925 std r8, VCPU_DSCR(r7)
927 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
929 /* Save non-volatile GPRs */
930 std r14, VCPU_GPR(r14)(r9)
931 std r15, VCPU_GPR(r15)(r9)
932 std r16, VCPU_GPR(r16)(r9)
933 std r17, VCPU_GPR(r17)(r9)
934 std r18, VCPU_GPR(r18)(r9)
935 std r19, VCPU_GPR(r19)(r9)
936 std r20, VCPU_GPR(r20)(r9)
937 std r21, VCPU_GPR(r21)(r9)
938 std r22, VCPU_GPR(r22)(r9)
939 std r23, VCPU_GPR(r23)(r9)
940 std r24, VCPU_GPR(r24)(r9)
941 std r25, VCPU_GPR(r25)(r9)
942 std r26, VCPU_GPR(r26)(r9)
943 std r27, VCPU_GPR(r27)(r9)
944 std r28, VCPU_GPR(r28)(r9)
945 std r29, VCPU_GPR(r29)(r9)
946 std r30, VCPU_GPR(r30)(r9)
947 std r31, VCPU_GPR(r31)(r9)
954 std r3, VCPU_SPRG0(r9)
955 std r4, VCPU_SPRG1(r9)
956 std r5, VCPU_SPRG2(r9)
957 std r6, VCPU_SPRG3(r9)
959 /* Increment yield count if they have a VPA */
960 ld r8, VCPU_VPA(r9) /* do they have a VPA? */
963 lwz r3, LPPACA_YIELDCOUNT(r8)
965 stw r3, LPPACA_YIELDCOUNT(r8)
967 /* Save PMU registers if requested */
968 /* r8 and cr0.eq are live here */
970 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
971 mfspr r4, SPRN_MMCR0 /* save MMCR0 */
972 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
974 beq 21f /* if no VPA, save PMU stuff anyway */
975 lbz r7, LPPACA_PMCINUSE(r8)
976 cmpwi r7, 0 /* did they ask for PMU stuff to be saved? */
978 std r3, VCPU_MMCR(r9) /* if not, set saved MMCR0 to FC */
980 21: mfspr r5, SPRN_MMCR1
982 std r4, VCPU_MMCR(r9)
983 std r5, VCPU_MMCR + 8(r9)
984 std r6, VCPU_MMCR + 16(r9)
994 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
996 stw r4, VCPU_PMC + 4(r9)
997 stw r5, VCPU_PMC + 8(r9)
998 stw r6, VCPU_PMC + 12(r9)
999 stw r7, VCPU_PMC + 16(r9)
1000 stw r8, VCPU_PMC + 20(r9)
1002 stw r10, VCPU_PMC + 24(r9)
1003 stw r11, VCPU_PMC + 28(r9)
1004 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
1010 /* Secondary threads go off to take a nap on POWER7 */
1012 lwz r0,VCPU_PTID(r3)
1015 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
1018 * Reload DEC. HDEC interrupts were disabled when
1019 * we reloaded the host's LPCR value.
1021 ld r3, HSTATE_DECEXP(r13)
1026 /* Reload the host's PMU registers */
1027 ld r3, PACALPPACAPTR(r13) /* is the host using the PMU? */
1028 lbz r4, LPPACA_PMCINUSE(r3)
1030 beq 23f /* skip if not */
1031 lwz r3, HSTATE_PMC(r13)
1032 lwz r4, HSTATE_PMC + 4(r13)
1033 lwz r5, HSTATE_PMC + 8(r13)
1034 lwz r6, HSTATE_PMC + 12(r13)
1035 lwz r8, HSTATE_PMC + 16(r13)
1036 lwz r9, HSTATE_PMC + 20(r13)
1038 lwz r10, HSTATE_PMC + 24(r13)
1039 lwz r11, HSTATE_PMC + 28(r13)
1040 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
1048 mtspr SPRN_PMC7, r10
1049 mtspr SPRN_PMC8, r11
1050 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
1051 ld r3, HSTATE_MMCR(r13)
1052 ld r4, HSTATE_MMCR + 8(r13)
1053 ld r5, HSTATE_MMCR + 16(r13)
1054 mtspr SPRN_MMCR1, r4
1055 mtspr SPRN_MMCRA, r5
1056 mtspr SPRN_MMCR0, r3
1060 * For external and machine check interrupts, we need
1061 * to call the Linux handler to process the interrupt.
1062 * We do that by jumping to the interrupt vector address
1063 * which we have in r12. The [h]rfid at the end of the
1064 * handler will return to the book3s_hv_interrupts.S code.
1065 * For other interrupts we do the rfid to get back
1066 * to the book3s_interrupts.S code here.
1068 ld r8, HSTATE_VMHANDLER(r13)
1069 ld r7, HSTATE_HOST_MSR(r13)
1071 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
1073 cmpwi r12, BOOK3S_INTERRUPT_MACHINE_CHECK
1075 /* RFI into the highmem handler, or branch to interrupt handler */
1080 mtmsrd r6, 1 /* Clear RI in MSR */
1089 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
1090 mtspr SPRN_HSRR0, r8
1091 mtspr SPRN_HSRR1, r7
1094 6: mfspr r6,SPRN_HDAR
1095 mfspr r7,SPRN_HDSISR
1099 * Try to handle an hcall in real mode.
1100 * Returns to the guest if we handle it, or continues on up to
1101 * the kernel if we can't (i.e. if we don't have a handler for
1102 * it, or if the handler returns H_TOO_HARD).
1104 .globl hcall_try_real_mode
1105 hcall_try_real_mode:
1106 ld r3,VCPU_GPR(r3)(r9)
1110 cmpldi r3,hcall_real_table_end - hcall_real_table
1112 LOAD_REG_ADDR(r4, hcall_real_table)
1118 mr r3,r9 /* get vcpu pointer */
1119 ld r4,VCPU_GPR(r4)(r9)
1122 beq hcall_real_fallback
1123 ld r4,HSTATE_KVM_VCPU(r13)
1124 std r3,VCPU_GPR(r3)(r4)
1129 /* We've attempted a real mode hcall, but it's punted it back
1130 * to userspace. We need to restore some clobbered volatiles
1131 * before resuming the pass-it-to-qemu path */
1132 hcall_real_fallback:
1133 li r12,BOOK3S_INTERRUPT_SYSCALL
1134 ld r9, HSTATE_KVM_VCPU(r13)
1138 .globl hcall_real_table
1140 .long 0 /* 0 - unused */
1141 .long .kvmppc_h_remove - hcall_real_table
1142 .long .kvmppc_h_enter - hcall_real_table
1143 .long .kvmppc_h_read - hcall_real_table
1144 .long 0 /* 0x10 - H_CLEAR_MOD */
1145 .long 0 /* 0x14 - H_CLEAR_REF */
1146 .long .kvmppc_h_protect - hcall_real_table
1147 .long 0 /* 0x1c - H_GET_TCE */
1148 .long .kvmppc_h_put_tce - hcall_real_table
1149 .long 0 /* 0x24 - H_SET_SPRG0 */
1150 .long .kvmppc_h_set_dabr - hcall_real_table
1196 .long .kvmppc_h_cede - hcall_real_table
1213 .long .kvmppc_h_bulk_remove - hcall_real_table
1214 hcall_real_table_end:
1220 bounce_ext_interrupt:
1224 li r10,BOOK3S_INTERRUPT_EXTERNAL
1225 li r11,(MSR_ME << 1) | 1 /* synthesize MSR_SF | MSR_ME */
1229 _GLOBAL(kvmppc_h_set_dabr)
1230 std r4,VCPU_DABR(r3)
1235 _GLOBAL(kvmppc_h_cede)
1237 std r11,VCPU_MSR(r3)
1239 stb r0,VCPU_CEDED(r3)
1240 sync /* order setting ceded vs. testing prodded */
1241 lbz r5,VCPU_PRODDED(r3)
1244 li r0,0 /* set trap to 0 to say hcall is handled */
1245 stw r0,VCPU_TRAP(r3)
1247 std r0,VCPU_GPR(r3)(r3)
1249 b 2f /* just send it up to host on 970 */
1250 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_206)
1253 * Set our bit in the bitmask of napping threads unless all the
1254 * other threads are already napping, in which case we send this
1257 ld r5,HSTATE_KVM_VCORE(r13)
1258 lwz r6,VCPU_PTID(r3)
1259 lwz r8,VCORE_ENTRY_EXIT(r5)
1263 addi r6,r5,VCORE_NAPPING_THREADS
1272 stb r0,HSTATE_NAPPING(r13)
1273 /* order napping_threads update vs testing entry_exit_count */
1276 lwz r7,VCORE_ENTRY_EXIT(r5)
1278 bge 33f /* another thread already exiting */
1281 * Although not specifically required by the architecture, POWER7
1282 * preserves the following registers in nap mode, even if an SMT mode
1283 * switch occurs: SLB entries, PURR, SPURR, AMOR, UAMOR, AMR, SPRG0-3,
1284 * DAR, DSISR, DABR, DABRX, DSCR, PMCx, MMCRx, SIAR, SDAR.
1286 /* Save non-volatile GPRs */
1287 std r14, VCPU_GPR(r14)(r3)
1288 std r15, VCPU_GPR(r15)(r3)
1289 std r16, VCPU_GPR(r16)(r3)
1290 std r17, VCPU_GPR(r17)(r3)
1291 std r18, VCPU_GPR(r18)(r3)
1292 std r19, VCPU_GPR(r19)(r3)
1293 std r20, VCPU_GPR(r20)(r3)
1294 std r21, VCPU_GPR(r21)(r3)
1295 std r22, VCPU_GPR(r22)(r3)
1296 std r23, VCPU_GPR(r23)(r3)
1297 std r24, VCPU_GPR(r24)(r3)
1298 std r25, VCPU_GPR(r25)(r3)
1299 std r26, VCPU_GPR(r26)(r3)
1300 std r27, VCPU_GPR(r27)(r3)
1301 std r28, VCPU_GPR(r28)(r3)
1302 std r29, VCPU_GPR(r29)(r3)
1303 std r30, VCPU_GPR(r30)(r3)
1304 std r31, VCPU_GPR(r31)(r3)
1310 * Take a nap until a decrementer or external interrupt occurs,
1311 * with PECE1 (wake on decr) and PECE0 (wake on external) set in LPCR
1314 stb r0,PACAPROCSTART(r13)
1316 ori r5,r5,LPCR_PECE0 | LPCR_PECE1
1320 std r0, HSTATE_SCRATCH0(r13)
1322 ld r0, HSTATE_SCRATCH0(r13)
1329 /* Woken by external or decrementer interrupt */
1330 ld r1, HSTATE_HOST_R1(r13)
1333 /* If we're a secondary thread and we got here by an IPI, ack it */
1334 ld r4,HSTATE_KVM_VCPU(r13)
1335 lwz r3,VCPU_PTID(r4)
1339 rlwinm r3,r3,44-31,0x7 /* extract wake reason field */
1340 cmpwi r3,4 /* was it an external interrupt? */
1342 ld r5, HSTATE_XICS_PHYS(r13)
1346 lwzcix r8,r5,r7 /* ack the interrupt */
1348 stbcix r0,r5,r6 /* clear it */
1349 stwcix r8,r5,r7 /* EOI it */
1351 /* load up FP state */
1355 ld r14, VCPU_GPR(r14)(r4)
1356 ld r15, VCPU_GPR(r15)(r4)
1357 ld r16, VCPU_GPR(r16)(r4)
1358 ld r17, VCPU_GPR(r17)(r4)
1359 ld r18, VCPU_GPR(r18)(r4)
1360 ld r19, VCPU_GPR(r19)(r4)
1361 ld r20, VCPU_GPR(r20)(r4)
1362 ld r21, VCPU_GPR(r21)(r4)
1363 ld r22, VCPU_GPR(r22)(r4)
1364 ld r23, VCPU_GPR(r23)(r4)
1365 ld r24, VCPU_GPR(r24)(r4)
1366 ld r25, VCPU_GPR(r25)(r4)
1367 ld r26, VCPU_GPR(r26)(r4)
1368 ld r27, VCPU_GPR(r27)(r4)
1369 ld r28, VCPU_GPR(r28)(r4)
1370 ld r29, VCPU_GPR(r29)(r4)
1371 ld r30, VCPU_GPR(r30)(r4)
1372 ld r31, VCPU_GPR(r31)(r4)
1374 /* clear our bit in vcore->napping_threads */
1375 33: ld r5,HSTATE_KVM_VCORE(r13)
1376 lwz r3,VCPU_PTID(r4)
1379 addi r6,r5,VCORE_NAPPING_THREADS
1385 stb r0,HSTATE_NAPPING(r13)
1387 /* see if any other thread is already exiting */
1388 lwz r0,VCORE_ENTRY_EXIT(r5)
1390 blt kvmppc_cede_reentry /* if not go back to guest */
1392 /* some threads are exiting, so go to the guest exit path */
1393 b hcall_real_fallback
1395 /* cede when already previously prodded case */
1397 stb r0,VCPU_PRODDED(r3)
1398 sync /* order testing prodded vs. clearing ceded */
1399 stb r0,VCPU_CEDED(r3)
1403 /* we've ceded but we want to give control to the host */
1408 ld r5,HSTATE_KVM_VCORE(r13)
1410 13: lbz r3,VCORE_IN_GUEST(r5)
1414 ld r11,PACA_SLBSHADOWPTR(r13)
1416 .rept SLB_NUM_BOLTED
1417 ld r5,SLBSHADOW_SAVEAREA(r11)
1418 ld r6,SLBSHADOW_SAVEAREA+8(r11)
1419 andis. r7,r5,SLB_ESID_V@h
1426 /* Clear any pending IPI - assume we're a secondary thread */
1427 ld r5, HSTATE_XICS_PHYS(r13)
1429 lwzcix r3, r5, r7 /* ack any pending interrupt */
1430 rlwinm. r0, r3, 0, 0xffffff /* any pending? */
1435 stbcix r0, r5, r6 /* clear the IPI */
1436 stwcix r3, r5, r7 /* EOI it */
1439 /* increment the nap count and then go to nap mode */
1440 ld r4, HSTATE_KVM_VCORE(r13)
1441 addi r4, r4, VCORE_NAP_COUNT
1442 lwsync /* make previous updates visible */
1450 rlwimi r4, r3, 0, LPCR_PECE0 | LPCR_PECE1
1454 std r0, HSTATE_SCRATCH0(r13)
1456 ld r0, HSTATE_SCRATCH0(r13)
1463 * Save away FP, VMX and VSX registers.
1466 _GLOBAL(kvmppc_save_fp)
1469 #ifdef CONFIG_ALTIVEC
1471 oris r8,r8,MSR_VEC@h
1472 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
1476 oris r8,r8,MSR_VSX@h
1477 END_FTR_SECTION_IFSET(CPU_FTR_VSX)
1485 li r6,reg*16+VCPU_VSRS
1493 stfd reg,reg*8+VCPU_FPRS(r3)
1497 ALT_FTR_SECTION_END_IFSET(CPU_FTR_VSX)
1500 stfd fr0,VCPU_FPSCR(r3)
1502 #ifdef CONFIG_ALTIVEC
1506 li r6,reg*16+VCPU_VRS
1513 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
1515 mfspr r6,SPRN_VRSAVE
1516 stw r6,VCPU_VRSAVE(r3)
1522 * Load up FP, VMX and VSX registers
1525 .globl kvmppc_load_fp
1529 #ifdef CONFIG_ALTIVEC
1531 oris r8,r8,MSR_VEC@h
1532 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
1536 oris r8,r8,MSR_VSX@h
1537 END_FTR_SECTION_IFSET(CPU_FTR_VSX)
1541 lfd fr0,VCPU_FPSCR(r4)
1547 li r7,reg*16+VCPU_VSRS
1555 lfd reg,reg*8+VCPU_FPRS(r4)
1559 ALT_FTR_SECTION_END_IFSET(CPU_FTR_VSX)
1562 #ifdef CONFIG_ALTIVEC
1569 li r7,reg*16+VCPU_VRS
1573 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
1575 lwz r7,VCPU_VRSAVE(r4)
1576 mtspr SPRN_VRSAVE,r7