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>
29 #include <asm/kvm_book3s_asm.h>
30 #include <asm/mmu-hash64.h>
32 /*****************************************************************************
34 * Real Mode handlers that need to be in the linear mapping *
36 ****************************************************************************/
38 .globl kvmppc_skip_interrupt
39 kvmppc_skip_interrupt:
47 .globl kvmppc_skip_Hinterrupt
48 kvmppc_skip_Hinterrupt:
57 * Call kvmppc_hv_entry in real mode.
58 * Must be called with interrupts hard-disabled.
62 * LR = return address to continue at after eventually re-enabling MMU
64 _GLOBAL(kvmppc_hv_entry_trampoline)
66 LOAD_REG_ADDR(r5, kvmppc_hv_entry)
71 mtmsrd r0,1 /* clear RI in MSR */
76 /******************************************************************************
80 *****************************************************************************/
84 #define XICS_IPI 2 /* interrupt source # for IPIs */
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 li r0,KVM_HWTHREAD_IN_KVM
98 stb r0,HSTATE_HWTHREAD_STATE(r13)
100 /* NV GPR values from power7_idle() will no longer be valid */
102 stb r0,PACA_NAPSTATELOST(r13)
104 /* get vcpu pointer, NULL if we have no vcpu to run */
105 ld r4,HSTATE_KVM_VCPU(r13)
108 /* Check the wake reason in SRR1 to see why we got here */
110 rlwinm r3,r3,44-31,0x7 /* extract wake reason field */
111 cmpwi r3,4 /* was it an external interrupt? */
115 * External interrupt - for now assume it is an IPI, since we
116 * should never get any other interrupts sent to offline threads.
117 * Only do this for secondary threads.
123 25: ld r5,HSTATE_XICS_PHYS(r13)
127 lwzcix r8,r5,r7 /* get and ack the interrupt */
129 clrldi. r9,r8,40 /* get interrupt source ID. */
130 beq 27f /* none there? */
133 stbcix r0,r5,r6 /* clear IPI */
134 26: stwcix r8,r5,r7 /* EOI the interrupt */
136 27: /* XXX should handle hypervisor maintenance interrupts etc. here */
138 /* reload vcpu pointer after clearing the IPI */
139 ld r4,HSTATE_KVM_VCPU(r13)
141 /* if we have no vcpu to run, go back to sleep */
144 /* were we napping due to cede? */
145 lbz r0,HSTATE_NAPPING(r13)
149 .global kvmppc_hv_entry
158 * all other volatile GPRS = free
161 std r0, HSTATE_VMHANDLER(r13)
163 /* Set partition DABR */
164 /* Do this before re-enabling PMU to avoid P7 DABR corruption bug */
171 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
173 /* Load guest PMU registers */
174 /* R4 is live here (vcpu pointer) */
176 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
177 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
179 lwz r3, VCPU_PMC(r4) /* always load up guest PMU registers */
180 lwz r5, VCPU_PMC + 4(r4) /* to prevent information leak */
181 lwz r6, VCPU_PMC + 8(r4)
182 lwz r7, VCPU_PMC + 12(r4)
183 lwz r8, VCPU_PMC + 16(r4)
184 lwz r9, VCPU_PMC + 20(r4)
186 lwz r10, VCPU_PMC + 24(r4)
187 lwz r11, VCPU_PMC + 28(r4)
188 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
198 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
200 ld r5, VCPU_MMCR + 8(r4)
201 ld r6, VCPU_MMCR + 16(r4)
207 /* Load up FP, VMX and VSX registers */
210 ld r14, VCPU_GPR(R14)(r4)
211 ld r15, VCPU_GPR(R15)(r4)
212 ld r16, VCPU_GPR(R16)(r4)
213 ld r17, VCPU_GPR(R17)(r4)
214 ld r18, VCPU_GPR(R18)(r4)
215 ld r19, VCPU_GPR(R19)(r4)
216 ld r20, VCPU_GPR(R20)(r4)
217 ld r21, VCPU_GPR(R21)(r4)
218 ld r22, VCPU_GPR(R22)(r4)
219 ld r23, VCPU_GPR(R23)(r4)
220 ld r24, VCPU_GPR(R24)(r4)
221 ld r25, VCPU_GPR(R25)(r4)
222 ld r26, VCPU_GPR(R26)(r4)
223 ld r27, VCPU_GPR(R27)(r4)
224 ld r28, VCPU_GPR(R28)(r4)
225 ld r29, VCPU_GPR(R29)(r4)
226 ld r30, VCPU_GPR(R30)(r4)
227 ld r31, VCPU_GPR(R31)(r4)
230 /* Switch DSCR to guest value */
233 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
236 * Set the decrementer to the guest decrementer.
238 ld r8,VCPU_DEC_EXPIRES(r4)
244 ld r5, VCPU_SPRG0(r4)
245 ld r6, VCPU_SPRG1(r4)
246 ld r7, VCPU_SPRG2(r4)
247 ld r8, VCPU_SPRG3(r4)
253 /* Save R1 in the PACA */
254 std r1, HSTATE_HOST_R1(r13)
256 /* Increment yield count if they have a VPA */
260 lwz r5, LPPACA_YIELDCOUNT(r3)
262 stw r5, LPPACA_YIELDCOUNT(r3)
264 /* Load up DAR and DSISR */
266 lwz r6, VCPU_DSISR(r4)
271 /* Restore AMR and UAMOR, set AMOR to all 1s */
278 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
288 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
290 * POWER7 host -> guest partition switch code.
291 * We don't have to lock against concurrent tlbies,
292 * but we do have to coordinate across hardware threads.
294 /* Increment entry count iff exit count is zero. */
295 ld r5,HSTATE_KVM_VCORE(r13)
296 addi r9,r5,VCORE_ENTRY_EXIT
298 cmpwi r3,0x100 /* any threads starting to exit? */
299 bge secondary_too_late /* if so we're too late to the party */
304 /* Primary thread switches to guest partition. */
305 ld r9,VCPU_KVM(r4) /* pointer to struct kvm */
311 li r0,LPID_RSVD /* switch to reserved LPID */
314 mtspr SPRN_SDR1,r6 /* switch to partition page table */
318 /* See if we need to flush the TLB */
319 lhz r6,PACAPACAINDEX(r13) /* test_bit(cpu, need_tlb_flush) */
320 clrldi r7,r6,64-6 /* extract bit number (6 bits) */
321 srdi r6,r6,6 /* doubleword number */
322 sldi r6,r6,3 /* address offset */
324 addi r6,r6,KVM_NEED_FLUSH /* dword in kvm->arch.need_tlb_flush */
330 23: ldarx r7,0,r6 /* if set, clear the bit */
334 li r6,128 /* and flush the TLB */
336 li r7,0x800 /* IS field = 0b10 */
344 stb r0,VCORE_IN_GUEST(r5) /* signal secondaries to continue */
347 /* Secondary threads wait for primary to have done partition switch */
348 20: lbz r0,VCORE_IN_GUEST(r5)
352 /* Set LPCR and RMOR. */
353 10: ld r8,KVM_LPCR(r9)
359 /* Check if HDEC expires soon */
362 li r12,BOOK3S_INTERRUPT_HV_DECREMENTER
366 /* Save purr/spurr */
369 std r5,HSTATE_PURR(r13)
370 std r6,HSTATE_SPURR(r13)
378 * PPC970 host -> guest partition switch code.
379 * We have to lock against concurrent tlbies,
380 * using native_tlbie_lock to lock against host tlbies
381 * and kvm->arch.tlbie_lock to lock against guest tlbies.
382 * We also have to invalidate the TLB since its
383 * entries aren't tagged with the LPID.
385 30: ld r9,VCPU_KVM(r4) /* pointer to struct kvm */
387 /* first take native_tlbie_lock */
390 .tc native_tlbie_lock[TC],native_tlbie_lock
392 ld r3,toc_tlbie_lock@toc(2)
393 lwz r8,PACA_LOCK_TOKEN(r13)
401 ld r7,KVM_LPCR(r9) /* use kvm->arch.lpcr to store HID4 */
403 rotldi r0,r0,HID4_LPID5_SH /* all lpid bits in HID4 = 1 */
407 mtspr SPRN_HID4,r0 /* switch to reserved LPID */
410 stw r0,0(r3) /* drop native_tlbie_lock */
412 /* invalidate the whole TLB */
421 /* Take the guest's tlbie_lock */
422 addi r3,r9,KVM_TLBIE_LOCK
430 mtspr SPRN_SDR1,r6 /* switch to partition page table */
432 /* Set up HID4 with the guest's LPID etc. */
437 /* drop the guest's tlbie_lock */
441 /* Check if HDEC expires soon */
444 li r12,BOOK3S_INTERRUPT_HV_DECREMENTER
448 /* Enable HDEC interrupts */
451 rldimi r0,r3, HID0_HDICE_SH, 64-HID0_HDICE_SH-1
461 /* Load up guest SLB entries */
462 31: lwz r5,VCPU_SLB_MAX(r4)
467 1: ld r8,VCPU_SLB_E(r6)
470 addi r6,r6,VCPU_SLB_SIZE
474 /* Restore state of CTRL run bit; assume 1 on entry */
488 kvmppc_cede_reentry: /* r4 = vcpu, r13 = paca */
492 ld r11, VCPU_MSR(r4) /* r11 = vcpu->arch.msr & ~MSR_HV */
494 rldicl r11, r11, 63 - MSR_HV_LG, 1
495 rotldi r11, r11, 1 + MSR_HV_LG
498 /* Check if we can deliver an external or decrementer interrupt now */
499 ld r0,VCPU_PENDING_EXC(r4)
500 li r8,(1 << BOOK3S_IRQPRIO_EXTERNAL)
501 oris r8,r8,(1 << BOOK3S_IRQPRIO_EXTERNAL_LEVEL)@h
511 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
513 li r0,BOOK3S_INTERRUPT_EXTERNAL
517 li r11,(MSR_ME << 1) | 1 /* synthesize MSR_SF | MSR_ME */
523 li r0,BOOK3S_INTERRUPT_DECREMENTER
526 /* Move SRR0 and SRR1 into the respective regs */
527 5: mtspr SPRN_SRR0, r6
530 stb r0,VCPU_CEDED(r4) /* cancel cede */
536 /* Activate guest mode, so faults get handled by KVM */
537 li r9, KVM_GUEST_MODE_GUEST
538 stb r9, HSTATE_IN_GUEST(r13)
547 ld r0, VCPU_GPR(R0)(r4)
548 ld r1, VCPU_GPR(R1)(r4)
549 ld r2, VCPU_GPR(R2)(r4)
550 ld r3, VCPU_GPR(R3)(r4)
551 ld r5, VCPU_GPR(R5)(r4)
552 ld r6, VCPU_GPR(R6)(r4)
553 ld r7, VCPU_GPR(R7)(r4)
554 ld r8, VCPU_GPR(R8)(r4)
555 ld r9, VCPU_GPR(R9)(r4)
556 ld r10, VCPU_GPR(R10)(r4)
557 ld r11, VCPU_GPR(R11)(r4)
558 ld r12, VCPU_GPR(R12)(r4)
559 ld r13, VCPU_GPR(R13)(r4)
561 ld r4, VCPU_GPR(R4)(r4)
566 /******************************************************************************
570 *****************************************************************************/
573 * We come here from the first-level interrupt handlers.
575 .globl kvmppc_interrupt
579 * R12 = interrupt vector
581 * guest CR, R12 saved in shadow VCPU SCRATCH1/0
582 * guest R13 saved in SPRN_SCRATCH0
584 /* abuse host_r2 as third scratch area; we get r2 from PACATOC(r13) */
585 std r9, HSTATE_HOST_R2(r13)
586 ld r9, HSTATE_KVM_VCPU(r13)
590 std r0, VCPU_GPR(R0)(r9)
591 std r1, VCPU_GPR(R1)(r9)
592 std r2, VCPU_GPR(R2)(r9)
593 std r3, VCPU_GPR(R3)(r9)
594 std r4, VCPU_GPR(R4)(r9)
595 std r5, VCPU_GPR(R5)(r9)
596 std r6, VCPU_GPR(R6)(r9)
597 std r7, VCPU_GPR(R7)(r9)
598 std r8, VCPU_GPR(R8)(r9)
599 ld r0, HSTATE_HOST_R2(r13)
600 std r0, VCPU_GPR(R9)(r9)
601 std r10, VCPU_GPR(R10)(r9)
602 std r11, VCPU_GPR(R11)(r9)
603 ld r3, HSTATE_SCRATCH0(r13)
604 lwz r4, HSTATE_SCRATCH1(r13)
605 std r3, VCPU_GPR(R12)(r9)
608 /* Restore R1/R2 so we can handle faults */
609 ld r1, HSTATE_HOST_R1(r13)
614 std r10, VCPU_SRR0(r9)
615 std r11, VCPU_SRR1(r9)
616 andi. r0, r12, 2 /* need to read HSRR0/1? */
618 mfspr r10, SPRN_HSRR0
619 mfspr r11, SPRN_HSRR1
621 1: std r10, VCPU_PC(r9)
622 std r11, VCPU_MSR(r9)
626 std r3, VCPU_GPR(R13)(r9)
629 /* Unset guest mode */
630 li r0, KVM_GUEST_MODE_NONE
631 stb r0, HSTATE_IN_GUEST(r13)
633 stw r12,VCPU_TRAP(r9)
635 /* Save HEIR (HV emulation assist reg) in last_inst
636 if this is an HEI (HV emulation interrupt, e40) */
637 li r3,KVM_INST_FETCH_FAILED
639 cmpwi r12,BOOK3S_INTERRUPT_H_EMUL_ASSIST
642 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
643 11: stw r3,VCPU_LAST_INST(r9)
645 /* these are volatile across C function calls */
652 /* If this is a page table miss then see if it's theirs or ours */
653 cmpwi r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
655 cmpwi r12, BOOK3S_INTERRUPT_H_INST_STORAGE
657 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
659 /* See if this is a leftover HDEC interrupt */
660 cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER
666 /* See if this is an hcall we can handle in real mode */
667 cmpwi r12,BOOK3S_INTERRUPT_SYSCALL
668 beq hcall_try_real_mode
670 /* Check for mediated interrupts (could be done earlier really ...) */
672 cmpwi r12,BOOK3S_INTERRUPT_EXTERNAL
678 bne bounce_ext_interrupt
680 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
682 guest_exit_cont: /* r9 = vcpu, r12 = trap, r13 = paca */
688 std r5,VCPU_DEC_EXPIRES(r9)
690 /* Save more register state */
694 stw r7, VCPU_DSISR(r9)
696 /* don't overwrite fault_dar/fault_dsisr if HDSI */
697 cmpwi r12,BOOK3S_INTERRUPT_H_DATA_STORAGE
699 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
700 std r6, VCPU_FAULT_DAR(r9)
701 stw r7, VCPU_FAULT_DSISR(r9)
703 /* See if it is a machine check */
704 cmpwi r12, BOOK3S_INTERRUPT_MACHINE_CHECK
705 beq machine_check_realmode
708 /* Save guest CTRL register, set runlatch to 1 */
709 6: mfspr r6,SPRN_CTRLF
716 /* Read the guest SLB and save it away */
717 lwz r0,VCPU_SLB_NR(r9) /* number of entries in SLB */
723 andis. r0,r8,SLB_ESID_V@h
725 add r8,r8,r6 /* put index in */
727 std r8,VCPU_SLB_E(r7)
728 std r3,VCPU_SLB_V(r7)
729 addi r7,r7,VCPU_SLB_SIZE
733 stw r5,VCPU_SLB_MAX(r9)
736 * Save the guest PURR/SPURR
744 std r6,VCPU_SPURR(r9)
749 * Restore host PURR/SPURR and add guest times
750 * so that the time in the guest gets accounted.
752 ld r3,HSTATE_PURR(r13)
753 ld r4,HSTATE_SPURR(r13)
758 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_201)
766 hdec_soon: /* r9 = vcpu, r12 = trap, r13 = paca */
769 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
771 * POWER7 guest -> host partition switch code.
772 * We don't have to lock against tlbies but we do
773 * have to coordinate the hardware threads.
775 /* Increment the threads-exiting-guest count in the 0xff00
776 bits of vcore->entry_exit_count */
778 ld r5,HSTATE_KVM_VCORE(r13)
779 addi r6,r5,VCORE_ENTRY_EXIT
787 * At this point we have an interrupt that we have to pass
788 * up to the kernel or qemu; we can't handle it in real mode.
789 * Thus we have to do a partition switch, so we have to
790 * collect the other threads, if we are the first thread
791 * to take an interrupt. To do this, we set the HDEC to 0,
792 * which causes an HDEC interrupt in all threads within 2ns
793 * because the HDEC register is shared between all 4 threads.
794 * However, we don't need to bother if this is an HDEC
795 * interrupt, since the other threads will already be on their
796 * way here in that case.
798 cmpwi r3,0x100 /* Are we the first here? */
800 cmpwi r3,1 /* Are any other threads in the guest? */
802 cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER
808 * Send an IPI to any napping threads, since an HDEC interrupt
809 * doesn't wake CPUs up from nap.
811 lwz r3,VCORE_NAPPING_THREADS(r5)
815 andc. r3,r3,r0 /* no sense IPI'ing ourselves */
817 mulli r4,r4,PACA_SIZE /* get paca for thread 0 */
821 ld r8,HSTATE_XICS_PHYS(r6) /* get thread's XICS reg addr */
824 stbcix r0,r7,r8 /* trigger the IPI */
829 /* Secondary threads wait for primary to do partition switch */
830 43: ld r4,VCPU_KVM(r9) /* pointer to struct kvm */
831 ld r5,HSTATE_KVM_VCORE(r13)
836 13: lbz r3,VCORE_IN_GUEST(r5)
842 /* Primary thread waits for all the secondaries to exit guest */
843 15: lwz r3,VCORE_ENTRY_EXIT(r5)
850 /* Primary thread switches back to host partition */
851 ld r6,KVM_HOST_SDR1(r4)
852 lwz r7,KVM_HOST_LPID(r4)
853 li r8,LPID_RSVD /* switch to reserved LPID */
856 mtspr SPRN_SDR1,r6 /* switch to partition page table */
860 stb r0,VCORE_IN_GUEST(r5)
861 lis r8,0x7fff /* MAX_INT@h */
864 16: ld r8,KVM_HOST_LPCR(r4)
870 * PPC970 guest -> host partition switch code.
871 * We have to lock against concurrent tlbies, and
872 * we have to flush the whole TLB.
874 32: ld r4,VCPU_KVM(r9) /* pointer to struct kvm */
876 /* Take the guest's tlbie_lock */
877 lwz r8,PACA_LOCK_TOKEN(r13)
878 addi r3,r4,KVM_TLBIE_LOCK
886 ld r7,KVM_HOST_LPCR(r4) /* use kvm->arch.host_lpcr for HID4 */
888 rotldi r0,r0,HID4_LPID5_SH /* all lpid bits in HID4 = 1 */
892 mtspr SPRN_HID4,r0 /* switch to reserved LPID */
895 stw r0,0(r3) /* drop guest tlbie_lock */
897 /* invalidate the whole TLB */
906 /* take native_tlbie_lock */
907 ld r3,toc_tlbie_lock@toc(2)
915 ld r6,KVM_HOST_SDR1(r4)
916 mtspr SPRN_SDR1,r6 /* switch to host page table */
918 /* Set up host HID4 value */
923 stw r0,0(r3) /* drop native_tlbie_lock */
925 lis r8,0x7fff /* MAX_INT@h */
928 /* Disable HDEC interrupts */
931 rldimi r0,r3, HID0_HDICE_SH, 64-HID0_HDICE_SH-1
941 /* load host SLB entries */
942 33: ld r8,PACA_SLBSHADOWPTR(r13)
945 ld r5,SLBSHADOW_SAVEAREA(r8)
946 ld r6,SLBSHADOW_SAVEAREA+8(r8)
947 andis. r7,r5,SLB_ESID_V@h
953 /* Save and reset AMR and UAMOR before turning on the MMU */
958 std r6,VCPU_UAMOR(r9)
961 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
963 /* Switch DSCR back to host value */
966 ld r7, HSTATE_DSCR(r13)
967 std r8, VCPU_DSCR(r7)
969 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
971 /* Save non-volatile GPRs */
972 std r14, VCPU_GPR(R14)(r9)
973 std r15, VCPU_GPR(R15)(r9)
974 std r16, VCPU_GPR(R16)(r9)
975 std r17, VCPU_GPR(R17)(r9)
976 std r18, VCPU_GPR(R18)(r9)
977 std r19, VCPU_GPR(R19)(r9)
978 std r20, VCPU_GPR(R20)(r9)
979 std r21, VCPU_GPR(R21)(r9)
980 std r22, VCPU_GPR(R22)(r9)
981 std r23, VCPU_GPR(R23)(r9)
982 std r24, VCPU_GPR(R24)(r9)
983 std r25, VCPU_GPR(R25)(r9)
984 std r26, VCPU_GPR(R26)(r9)
985 std r27, VCPU_GPR(R27)(r9)
986 std r28, VCPU_GPR(R28)(r9)
987 std r29, VCPU_GPR(R29)(r9)
988 std r30, VCPU_GPR(R30)(r9)
989 std r31, VCPU_GPR(R31)(r9)
996 std r3, VCPU_SPRG0(r9)
997 std r4, VCPU_SPRG1(r9)
998 std r5, VCPU_SPRG2(r9)
999 std r6, VCPU_SPRG3(r9)
1005 /* Increment yield count if they have a VPA */
1006 ld r8, VCPU_VPA(r9) /* do they have a VPA? */
1009 lwz r3, LPPACA_YIELDCOUNT(r8)
1011 stw r3, LPPACA_YIELDCOUNT(r8)
1013 /* Save PMU registers if requested */
1014 /* r8 and cr0.eq are live here */
1016 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
1017 mfspr r4, SPRN_MMCR0 /* save MMCR0 */
1018 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
1019 mfspr r6, SPRN_MMCRA
1021 /* On P7, clear MMCRA in order to disable SDAR updates */
1023 mtspr SPRN_MMCRA, r7
1024 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
1026 beq 21f /* if no VPA, save PMU stuff anyway */
1027 lbz r7, LPPACA_PMCINUSE(r8)
1028 cmpwi r7, 0 /* did they ask for PMU stuff to be saved? */
1030 std r3, VCPU_MMCR(r9) /* if not, set saved MMCR0 to FC */
1032 21: mfspr r5, SPRN_MMCR1
1033 std r4, VCPU_MMCR(r9)
1034 std r5, VCPU_MMCR + 8(r9)
1035 std r6, VCPU_MMCR + 16(r9)
1043 mfspr r10, SPRN_PMC7
1044 mfspr r11, SPRN_PMC8
1045 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
1046 stw r3, VCPU_PMC(r9)
1047 stw r4, VCPU_PMC + 4(r9)
1048 stw r5, VCPU_PMC + 8(r9)
1049 stw r6, VCPU_PMC + 12(r9)
1050 stw r7, VCPU_PMC + 16(r9)
1051 stw r8, VCPU_PMC + 20(r9)
1053 stw r10, VCPU_PMC + 24(r9)
1054 stw r11, VCPU_PMC + 28(r9)
1055 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
1058 /* Secondary threads go off to take a nap on POWER7 */
1060 lwz r0,VCPU_PTID(r9)
1063 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
1065 /* Restore host DABR and DABRX */
1066 ld r5,HSTATE_DABR(r13)
1072 ld r3,PACA_SPRG3(r13)
1076 * Reload DEC. HDEC interrupts were disabled when
1077 * we reloaded the host's LPCR value.
1079 ld r3, HSTATE_DECEXP(r13)
1084 /* Reload the host's PMU registers */
1085 ld r3, PACALPPACAPTR(r13) /* is the host using the PMU? */
1086 lbz r4, LPPACA_PMCINUSE(r3)
1088 beq 23f /* skip if not */
1089 lwz r3, HSTATE_PMC(r13)
1090 lwz r4, HSTATE_PMC + 4(r13)
1091 lwz r5, HSTATE_PMC + 8(r13)
1092 lwz r6, HSTATE_PMC + 12(r13)
1093 lwz r8, HSTATE_PMC + 16(r13)
1094 lwz r9, HSTATE_PMC + 20(r13)
1096 lwz r10, HSTATE_PMC + 24(r13)
1097 lwz r11, HSTATE_PMC + 28(r13)
1098 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
1106 mtspr SPRN_PMC7, r10
1107 mtspr SPRN_PMC8, r11
1108 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
1109 ld r3, HSTATE_MMCR(r13)
1110 ld r4, HSTATE_MMCR + 8(r13)
1111 ld r5, HSTATE_MMCR + 16(r13)
1112 mtspr SPRN_MMCR1, r4
1113 mtspr SPRN_MMCRA, r5
1114 mtspr SPRN_MMCR0, r3
1118 * For external and machine check interrupts, we need
1119 * to call the Linux handler to process the interrupt.
1120 * We do that by jumping to absolute address 0x500 for
1121 * external interrupts, or the machine_check_fwnmi label
1122 * for machine checks (since firmware might have patched
1123 * the vector area at 0x200). The [h]rfid at the end of the
1124 * handler will return to the book3s_hv_interrupts.S code.
1125 * For other interrupts we do the rfid to get back
1126 * to the book3s_hv_interrupts.S code here.
1128 ld r8, HSTATE_VMHANDLER(r13)
1129 ld r7, HSTATE_HOST_MSR(r13)
1131 cmpwi cr1, r12, BOOK3S_INTERRUPT_MACHINE_CHECK
1132 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
1135 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
1137 /* RFI into the highmem handler, or branch to interrupt handler */
1141 mtmsrd r6, 1 /* Clear RI in MSR */
1144 beqa 0x500 /* external interrupt (PPC970) */
1145 beq cr1, 13f /* machine check */
1148 /* On POWER7, we have external interrupts set to use HSRR0/1 */
1149 11: mtspr SPRN_HSRR0, r8
1150 mtspr SPRN_HSRR1, r7
1153 13: b machine_check_fwnmi
1156 * Check whether an HDSI is an HPTE not found fault or something else.
1157 * If it is an HPTE not found fault that is due to the guest accessing
1158 * a page that they have mapped but which we have paged out, then
1159 * we continue on with the guest exit path. In all other cases,
1160 * reflect the HDSI to the guest as a DSI.
1164 mfspr r6, SPRN_HDSISR
1165 /* HPTE not found fault or protection fault? */
1166 andis. r0, r6, (DSISR_NOHPTE | DSISR_PROTFAULT)@h
1167 beq 1f /* if not, send it to the guest */
1168 andi. r0, r11, MSR_DR /* data relocation enabled? */
1171 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
1172 bne 1f /* if no SLB entry found */
1173 4: std r4, VCPU_FAULT_DAR(r9)
1174 stw r6, VCPU_FAULT_DSISR(r9)
1176 /* Search the hash table. */
1177 mr r3, r9 /* vcpu pointer */
1178 li r7, 1 /* data fault */
1179 bl .kvmppc_hpte_hv_fault
1180 ld r9, HSTATE_KVM_VCPU(r13)
1182 ld r11, VCPU_MSR(r9)
1183 li r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1184 cmpdi r3, 0 /* retry the instruction */
1186 cmpdi r3, -1 /* handle in kernel mode */
1188 cmpdi r3, -2 /* MMIO emulation; need instr word */
1191 /* Synthesize a DSI for the guest */
1192 ld r4, VCPU_FAULT_DAR(r9)
1194 1: mtspr SPRN_DAR, r4
1195 mtspr SPRN_DSISR, r6
1196 mtspr SPRN_SRR0, r10
1197 mtspr SPRN_SRR1, r11
1198 li r10, BOOK3S_INTERRUPT_DATA_STORAGE
1199 li r11, (MSR_ME << 1) | 1 /* synthesize MSR_SF | MSR_ME */
1201 fast_interrupt_c_return:
1202 6: ld r7, VCPU_CTR(r9)
1203 lwz r8, VCPU_XER(r9)
1209 3: ld r5, VCPU_KVM(r9) /* not relocated, use VRMA */
1210 ld r5, KVM_VRMA_SLB_V(r5)
1213 /* If this is for emulated MMIO, load the instruction word */
1214 2: li r8, KVM_INST_FETCH_FAILED /* In case lwz faults */
1216 /* Set guest mode to 'jump over instruction' so if lwz faults
1217 * we'll just continue at the next IP. */
1218 li r0, KVM_GUEST_MODE_SKIP
1219 stb r0, HSTATE_IN_GUEST(r13)
1221 /* Do the access with MSR:DR enabled */
1223 ori r4, r3, MSR_DR /* Enable paging for data */
1228 /* Store the result */
1229 stw r8, VCPU_LAST_INST(r9)
1231 /* Unset guest mode. */
1232 li r0, KVM_GUEST_MODE_NONE
1233 stb r0, HSTATE_IN_GUEST(r13)
1237 * Similarly for an HISI, reflect it to the guest as an ISI unless
1238 * it is an HPTE not found fault for a page that we have paged out.
1241 andis. r0, r11, SRR1_ISI_NOPT@h
1243 andi. r0, r11, MSR_IR /* instruction relocation enabled? */
1246 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
1247 bne 1f /* if no SLB entry found */
1249 /* Search the hash table. */
1250 mr r3, r9 /* vcpu pointer */
1253 li r7, 0 /* instruction fault */
1254 bl .kvmppc_hpte_hv_fault
1255 ld r9, HSTATE_KVM_VCPU(r13)
1257 ld r11, VCPU_MSR(r9)
1258 li r12, BOOK3S_INTERRUPT_H_INST_STORAGE
1259 cmpdi r3, 0 /* retry the instruction */
1260 beq fast_interrupt_c_return
1261 cmpdi r3, -1 /* handle in kernel mode */
1264 /* Synthesize an ISI for the guest */
1266 1: mtspr SPRN_SRR0, r10
1267 mtspr SPRN_SRR1, r11
1268 li r10, BOOK3S_INTERRUPT_INST_STORAGE
1269 li r11, (MSR_ME << 1) | 1 /* synthesize MSR_SF | MSR_ME */
1271 b fast_interrupt_c_return
1273 3: ld r6, VCPU_KVM(r9) /* not relocated, use VRMA */
1274 ld r5, KVM_VRMA_SLB_V(r6)
1278 * Try to handle an hcall in real mode.
1279 * Returns to the guest if we handle it, or continues on up to
1280 * the kernel if we can't (i.e. if we don't have a handler for
1281 * it, or if the handler returns H_TOO_HARD).
1283 .globl hcall_try_real_mode
1284 hcall_try_real_mode:
1285 ld r3,VCPU_GPR(R3)(r9)
1289 cmpldi r3,hcall_real_table_end - hcall_real_table
1291 LOAD_REG_ADDR(r4, hcall_real_table)
1297 mr r3,r9 /* get vcpu pointer */
1298 ld r4,VCPU_GPR(R4)(r9)
1301 beq hcall_real_fallback
1302 ld r4,HSTATE_KVM_VCPU(r13)
1303 std r3,VCPU_GPR(R3)(r4)
1308 /* We've attempted a real mode hcall, but it's punted it back
1309 * to userspace. We need to restore some clobbered volatiles
1310 * before resuming the pass-it-to-qemu path */
1311 hcall_real_fallback:
1312 li r12,BOOK3S_INTERRUPT_SYSCALL
1313 ld r9, HSTATE_KVM_VCPU(r13)
1317 .globl hcall_real_table
1319 .long 0 /* 0 - unused */
1320 .long .kvmppc_h_remove - hcall_real_table
1321 .long .kvmppc_h_enter - hcall_real_table
1322 .long .kvmppc_h_read - hcall_real_table
1323 .long 0 /* 0x10 - H_CLEAR_MOD */
1324 .long 0 /* 0x14 - H_CLEAR_REF */
1325 .long .kvmppc_h_protect - hcall_real_table
1326 .long 0 /* 0x1c - H_GET_TCE */
1327 .long .kvmppc_h_put_tce - hcall_real_table
1328 .long 0 /* 0x24 - H_SET_SPRG0 */
1329 .long .kvmppc_h_set_dabr - hcall_real_table
1375 .long .kvmppc_h_cede - hcall_real_table
1392 .long .kvmppc_h_bulk_remove - hcall_real_table
1393 hcall_real_table_end:
1399 bounce_ext_interrupt:
1403 li r10,BOOK3S_INTERRUPT_EXTERNAL
1404 li r11,(MSR_ME << 1) | 1 /* synthesize MSR_SF | MSR_ME */
1408 _GLOBAL(kvmppc_h_set_dabr)
1409 std r4,VCPU_DABR(r3)
1410 /* Work around P7 bug where DABR can get corrupted on mtspr */
1411 1: mtspr SPRN_DABR,r4
1419 _GLOBAL(kvmppc_h_cede)
1421 std r11,VCPU_MSR(r3)
1423 stb r0,VCPU_CEDED(r3)
1424 sync /* order setting ceded vs. testing prodded */
1425 lbz r5,VCPU_PRODDED(r3)
1427 bne kvm_cede_prodded
1428 li r0,0 /* set trap to 0 to say hcall is handled */
1429 stw r0,VCPU_TRAP(r3)
1431 std r0,VCPU_GPR(R3)(r3)
1433 b kvm_cede_exit /* just send it up to host on 970 */
1434 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_206)
1437 * Set our bit in the bitmask of napping threads unless all the
1438 * other threads are already napping, in which case we send this
1441 ld r5,HSTATE_KVM_VCORE(r13)
1442 lwz r6,VCPU_PTID(r3)
1443 lwz r8,VCORE_ENTRY_EXIT(r5)
1447 addi r6,r5,VCORE_NAPPING_THREADS
1456 stb r0,HSTATE_NAPPING(r13)
1457 /* order napping_threads update vs testing entry_exit_count */
1460 lwz r7,VCORE_ENTRY_EXIT(r5)
1462 bge 33f /* another thread already exiting */
1465 * Although not specifically required by the architecture, POWER7
1466 * preserves the following registers in nap mode, even if an SMT mode
1467 * switch occurs: SLB entries, PURR, SPURR, AMOR, UAMOR, AMR, SPRG0-3,
1468 * DAR, DSISR, DABR, DABRX, DSCR, PMCx, MMCRx, SIAR, SDAR.
1470 /* Save non-volatile GPRs */
1471 std r14, VCPU_GPR(R14)(r3)
1472 std r15, VCPU_GPR(R15)(r3)
1473 std r16, VCPU_GPR(R16)(r3)
1474 std r17, VCPU_GPR(R17)(r3)
1475 std r18, VCPU_GPR(R18)(r3)
1476 std r19, VCPU_GPR(R19)(r3)
1477 std r20, VCPU_GPR(R20)(r3)
1478 std r21, VCPU_GPR(R21)(r3)
1479 std r22, VCPU_GPR(R22)(r3)
1480 std r23, VCPU_GPR(R23)(r3)
1481 std r24, VCPU_GPR(R24)(r3)
1482 std r25, VCPU_GPR(R25)(r3)
1483 std r26, VCPU_GPR(R26)(r3)
1484 std r27, VCPU_GPR(R27)(r3)
1485 std r28, VCPU_GPR(R28)(r3)
1486 std r29, VCPU_GPR(R29)(r3)
1487 std r30, VCPU_GPR(R30)(r3)
1488 std r31, VCPU_GPR(R31)(r3)
1494 * Take a nap until a decrementer or external interrupt occurs,
1495 * with PECE1 (wake on decr) and PECE0 (wake on external) set in LPCR
1498 stb r0,HSTATE_HWTHREAD_REQ(r13)
1500 ori r5,r5,LPCR_PECE0 | LPCR_PECE1
1504 std r0, HSTATE_SCRATCH0(r13)
1506 ld r0, HSTATE_SCRATCH0(r13)
1513 /* Woken by external or decrementer interrupt */
1514 ld r1, HSTATE_HOST_R1(r13)
1516 /* load up FP state */
1520 ld r14, VCPU_GPR(R14)(r4)
1521 ld r15, VCPU_GPR(R15)(r4)
1522 ld r16, VCPU_GPR(R16)(r4)
1523 ld r17, VCPU_GPR(R17)(r4)
1524 ld r18, VCPU_GPR(R18)(r4)
1525 ld r19, VCPU_GPR(R19)(r4)
1526 ld r20, VCPU_GPR(R20)(r4)
1527 ld r21, VCPU_GPR(R21)(r4)
1528 ld r22, VCPU_GPR(R22)(r4)
1529 ld r23, VCPU_GPR(R23)(r4)
1530 ld r24, VCPU_GPR(R24)(r4)
1531 ld r25, VCPU_GPR(R25)(r4)
1532 ld r26, VCPU_GPR(R26)(r4)
1533 ld r27, VCPU_GPR(R27)(r4)
1534 ld r28, VCPU_GPR(R28)(r4)
1535 ld r29, VCPU_GPR(R29)(r4)
1536 ld r30, VCPU_GPR(R30)(r4)
1537 ld r31, VCPU_GPR(R31)(r4)
1539 /* clear our bit in vcore->napping_threads */
1540 33: ld r5,HSTATE_KVM_VCORE(r13)
1541 lwz r3,VCPU_PTID(r4)
1544 addi r6,r5,VCORE_NAPPING_THREADS
1550 stb r0,HSTATE_NAPPING(r13)
1552 /* see if any other thread is already exiting */
1553 lwz r0,VCORE_ENTRY_EXIT(r5)
1555 blt kvmppc_cede_reentry /* if not go back to guest */
1557 /* some threads are exiting, so go to the guest exit path */
1558 b hcall_real_fallback
1560 /* cede when already previously prodded case */
1563 stb r0,VCPU_PRODDED(r3)
1564 sync /* order testing prodded vs. clearing ceded */
1565 stb r0,VCPU_CEDED(r3)
1569 /* we've ceded but we want to give control to the host */
1574 /* Try to handle a machine check in real mode */
1575 machine_check_realmode:
1576 mr r3, r9 /* get vcpu pointer */
1577 bl .kvmppc_realmode_machine_check
1579 cmpdi r3, 0 /* continue exiting from guest? */
1580 ld r9, HSTATE_KVM_VCPU(r13)
1581 li r12, BOOK3S_INTERRUPT_MACHINE_CHECK
1583 /* If not, deliver a machine check. SRR0/1 are already set */
1584 li r10, BOOK3S_INTERRUPT_MACHINE_CHECK
1585 li r11, (MSR_ME << 1) | 1 /* synthesize MSR_SF | MSR_ME */
1587 b fast_interrupt_c_return
1590 ld r5,HSTATE_KVM_VCORE(r13)
1592 13: lbz r3,VCORE_IN_GUEST(r5)
1596 ld r11,PACA_SLBSHADOWPTR(r13)
1598 .rept SLB_NUM_BOLTED
1599 ld r5,SLBSHADOW_SAVEAREA(r11)
1600 ld r6,SLBSHADOW_SAVEAREA+8(r11)
1601 andis. r7,r5,SLB_ESID_V@h
1608 /* Clear our vcpu pointer so we don't come back in early */
1610 std r0, HSTATE_KVM_VCPU(r13)
1612 /* Clear any pending IPI - assume we're a secondary thread */
1613 ld r5, HSTATE_XICS_PHYS(r13)
1615 lwzcix r3, r5, r7 /* ack any pending interrupt */
1616 rlwinm. r0, r3, 0, 0xffffff /* any pending? */
1621 stbcix r0, r5, r6 /* clear the IPI */
1622 stwcix r3, r5, r7 /* EOI it */
1625 /* increment the nap count and then go to nap mode */
1626 ld r4, HSTATE_KVM_VCORE(r13)
1627 addi r4, r4, VCORE_NAP_COUNT
1628 lwsync /* make previous updates visible */
1635 li r0, KVM_HWTHREAD_IN_NAP
1636 stb r0, HSTATE_HWTHREAD_STATE(r13)
1640 rlwimi r4, r3, 0, LPCR_PECE0 | LPCR_PECE1
1643 std r0, HSTATE_SCRATCH0(r13)
1645 ld r0, HSTATE_SCRATCH0(r13)
1652 * Save away FP, VMX and VSX registers.
1655 _GLOBAL(kvmppc_save_fp)
1658 #ifdef CONFIG_ALTIVEC
1660 oris r8,r8,MSR_VEC@h
1661 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
1665 oris r8,r8,MSR_VSX@h
1666 END_FTR_SECTION_IFSET(CPU_FTR_VSX)
1674 li r6,reg*16+VCPU_VSRS
1682 stfd reg,reg*8+VCPU_FPRS(r3)
1686 ALT_FTR_SECTION_END_IFSET(CPU_FTR_VSX)
1689 stfd fr0,VCPU_FPSCR(r3)
1691 #ifdef CONFIG_ALTIVEC
1695 li r6,reg*16+VCPU_VRS
1702 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
1704 mfspr r6,SPRN_VRSAVE
1705 stw r6,VCPU_VRSAVE(r3)
1711 * Load up FP, VMX and VSX registers
1714 .globl kvmppc_load_fp
1718 #ifdef CONFIG_ALTIVEC
1720 oris r8,r8,MSR_VEC@h
1721 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
1725 oris r8,r8,MSR_VSX@h
1726 END_FTR_SECTION_IFSET(CPU_FTR_VSX)
1730 lfd fr0,VCPU_FPSCR(r4)
1736 li r7,reg*16+VCPU_VSRS
1744 lfd reg,reg*8+VCPU_FPRS(r4)
1748 ALT_FTR_SECTION_END_IFSET(CPU_FTR_VSX)
1751 #ifdef CONFIG_ALTIVEC
1758 li r7,reg*16+VCPU_VRS
1762 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
1764 lwz r7,VCPU_VRSAVE(r4)
1765 mtspr SPRN_VRSAVE,r7