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>
31 /*****************************************************************************
33 * Real Mode handlers that need to be in the linear mapping *
35 ****************************************************************************/
37 .globl kvmppc_skip_interrupt
38 kvmppc_skip_interrupt:
46 .globl kvmppc_skip_Hinterrupt
47 kvmppc_skip_Hinterrupt:
56 * Call kvmppc_hv_entry in real mode.
57 * Must be called with interrupts hard-disabled.
61 * LR = return address to continue at after eventually re-enabling MMU
63 _GLOBAL(kvmppc_hv_entry_trampoline)
65 LOAD_REG_ADDR(r5, kvmppc_hv_entry)
70 mtmsrd r0,1 /* clear RI in MSR */
75 /******************************************************************************
79 *****************************************************************************/
83 #define XICS_IPI 2 /* interrupt source # for IPIs */
86 * We come in here when wakened from nap mode on a secondary hw thread.
87 * Relocation is off and most register values are lost.
88 * r13 points to the PACA.
90 .globl kvm_start_guest
92 ld r1,PACAEMERGSP(r13)
93 subi r1,r1,STACK_FRAME_OVERHEAD
96 li r0,KVM_HWTHREAD_IN_KVM
97 stb r0,HSTATE_HWTHREAD_STATE(r13)
99 /* NV GPR values from power7_idle() will no longer be valid */
101 stb r0,PACA_NAPSTATELOST(r13)
103 /* get vcpu pointer, NULL if we have no vcpu to run */
104 ld r4,HSTATE_KVM_VCPU(r13)
107 /* Check the wake reason in SRR1 to see why we got here */
109 rlwinm r3,r3,44-31,0x7 /* extract wake reason field */
110 cmpwi r3,4 /* was it an external interrupt? */
114 * External interrupt - for now assume it is an IPI, since we
115 * should never get any other interrupts sent to offline threads.
116 * Only do this for secondary threads.
122 25: ld r5,HSTATE_XICS_PHYS(r13)
126 lwzcix r8,r5,r7 /* get and ack the interrupt */
128 clrldi. r9,r8,40 /* get interrupt source ID. */
129 beq 27f /* none there? */
132 stbcix r0,r5,r6 /* clear IPI */
133 26: stwcix r8,r5,r7 /* EOI the interrupt */
135 27: /* XXX should handle hypervisor maintenance interrupts etc. here */
137 /* if we have no vcpu to run, go back to sleep */
140 /* were we napping due to cede? */
141 lbz r0,HSTATE_NAPPING(r13)
145 .global kvmppc_hv_entry
154 * all other volatile GPRS = free
157 std r0, HSTATE_VMHANDLER(r13)
159 /* Set partition DABR */
160 /* Do this before re-enabling PMU to avoid P7 DABR corruption bug */
167 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
169 /* Load guest PMU registers */
170 /* R4 is live here (vcpu pointer) */
172 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
173 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
175 lwz r3, VCPU_PMC(r4) /* always load up guest PMU registers */
176 lwz r5, VCPU_PMC + 4(r4) /* to prevent information leak */
177 lwz r6, VCPU_PMC + 8(r4)
178 lwz r7, VCPU_PMC + 12(r4)
179 lwz r8, VCPU_PMC + 16(r4)
180 lwz r9, VCPU_PMC + 20(r4)
182 lwz r10, VCPU_PMC + 24(r4)
183 lwz r11, VCPU_PMC + 28(r4)
184 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
194 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
196 ld r5, VCPU_MMCR + 8(r4)
197 ld r6, VCPU_MMCR + 16(r4)
203 /* Load up FP, VMX and VSX registers */
206 ld r14, VCPU_GPR(R14)(r4)
207 ld r15, VCPU_GPR(R15)(r4)
208 ld r16, VCPU_GPR(R16)(r4)
209 ld r17, VCPU_GPR(R17)(r4)
210 ld r18, VCPU_GPR(R18)(r4)
211 ld r19, VCPU_GPR(R19)(r4)
212 ld r20, VCPU_GPR(R20)(r4)
213 ld r21, VCPU_GPR(R21)(r4)
214 ld r22, VCPU_GPR(R22)(r4)
215 ld r23, VCPU_GPR(R23)(r4)
216 ld r24, VCPU_GPR(R24)(r4)
217 ld r25, VCPU_GPR(R25)(r4)
218 ld r26, VCPU_GPR(R26)(r4)
219 ld r27, VCPU_GPR(R27)(r4)
220 ld r28, VCPU_GPR(R28)(r4)
221 ld r29, VCPU_GPR(R29)(r4)
222 ld r30, VCPU_GPR(R30)(r4)
223 ld r31, VCPU_GPR(R31)(r4)
226 /* Switch DSCR to guest value */
229 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
232 * Set the decrementer to the guest decrementer.
234 ld r8,VCPU_DEC_EXPIRES(r4)
240 ld r5, VCPU_SPRG0(r4)
241 ld r6, VCPU_SPRG1(r4)
242 ld r7, VCPU_SPRG2(r4)
243 ld r8, VCPU_SPRG3(r4)
249 /* Save R1 in the PACA */
250 std r1, HSTATE_HOST_R1(r13)
252 /* Increment yield count if they have a VPA */
256 lwz r5, LPPACA_YIELDCOUNT(r3)
258 stw r5, LPPACA_YIELDCOUNT(r3)
260 /* Load up DAR and DSISR */
262 lwz r6, VCPU_DSISR(r4)
267 /* Restore AMR and UAMOR, set AMOR to all 1s */
274 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
284 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
286 * POWER7 host -> guest partition switch code.
287 * We don't have to lock against concurrent tlbies,
288 * but we do have to coordinate across hardware threads.
290 /* Increment entry count iff exit count is zero. */
291 ld r5,HSTATE_KVM_VCORE(r13)
292 addi r9,r5,VCORE_ENTRY_EXIT
294 cmpwi r3,0x100 /* any threads starting to exit? */
295 bge secondary_too_late /* if so we're too late to the party */
300 /* Primary thread switches to guest partition. */
301 ld r9,VCPU_KVM(r4) /* pointer to struct kvm */
307 li r0,LPID_RSVD /* switch to reserved LPID */
310 mtspr SPRN_SDR1,r6 /* switch to partition page table */
314 stb r0,VCORE_IN_GUEST(r5) /* signal secondaries to continue */
317 /* Secondary threads wait for primary to have done partition switch */
318 20: lbz r0,VCORE_IN_GUEST(r5)
322 /* Set LPCR and RMOR. */
323 10: ld r8,KVM_LPCR(r9)
329 /* Check if HDEC expires soon */
332 li r12,BOOK3S_INTERRUPT_HV_DECREMENTER
337 * Invalidate the TLB if we could possibly have stale TLB
338 * entries for this partition on this core due to the use
340 * XXX maybe only need this on primary thread?
342 ld r9,VCPU_KVM(r4) /* pointer to struct kvm */
343 lwz r5,VCPU_VCPUID(r4)
344 lhz r6,PACAPACAINDEX(r13)
345 rldimi r6,r5,0,62 /* XXX map as if threads 1:1 p:v */
346 lhz r8,VCPU_LAST_CPU(r4)
347 sldi r7,r6,1 /* see if this is the same vcpu */
348 add r7,r7,r9 /* as last ran on this pcpu */
349 lhz r0,KVM_LAST_VCPU(r7)
350 cmpw r6,r8 /* on the same cpu core as last time? */
352 cmpw r0,r5 /* same vcpu as this core last ran? */
354 3: sth r6,VCPU_LAST_CPU(r4) /* if not, invalidate partition TLB */
355 sth r5,KVM_LAST_VCPU(r7)
358 li r7,0x800 /* IS field = 0b10 */
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)
683 hcall_real_cont: /* r9 = vcpu, r12 = trap, r13 = paca */
689 std r5,VCPU_DEC_EXPIRES(r9)
691 /* Save more register state */
695 stw r7, VCPU_DSISR(r9)
697 /* don't overwrite fault_dar/fault_dsisr if HDSI */
698 cmpwi r12,BOOK3S_INTERRUPT_H_DATA_STORAGE
700 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
701 std r6, VCPU_FAULT_DAR(r9)
702 stw r7, VCPU_FAULT_DSISR(r9)
704 /* Save guest CTRL register, set runlatch to 1 */
705 6: mfspr r6,SPRN_CTRLF
712 /* Read the guest SLB and save it away */
713 lwz r0,VCPU_SLB_NR(r9) /* number of entries in SLB */
719 andis. r0,r8,SLB_ESID_V@h
721 add r8,r8,r6 /* put index in */
723 std r8,VCPU_SLB_E(r7)
724 std r3,VCPU_SLB_V(r7)
725 addi r7,r7,VCPU_SLB_SIZE
729 stw r5,VCPU_SLB_MAX(r9)
732 * Save the guest PURR/SPURR
740 std r6,VCPU_SPURR(r9)
745 * Restore host PURR/SPURR and add guest times
746 * so that the time in the guest gets accounted.
748 ld r3,HSTATE_PURR(r13)
749 ld r4,HSTATE_SPURR(r13)
754 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_201)
762 hdec_soon: /* r9 = vcpu, r12 = trap, r13 = paca */
765 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
767 * POWER7 guest -> host partition switch code.
768 * We don't have to lock against tlbies but we do
769 * have to coordinate the hardware threads.
771 /* Increment the threads-exiting-guest count in the 0xff00
772 bits of vcore->entry_exit_count */
774 ld r5,HSTATE_KVM_VCORE(r13)
775 addi r6,r5,VCORE_ENTRY_EXIT
783 * At this point we have an interrupt that we have to pass
784 * up to the kernel or qemu; we can't handle it in real mode.
785 * Thus we have to do a partition switch, so we have to
786 * collect the other threads, if we are the first thread
787 * to take an interrupt. To do this, we set the HDEC to 0,
788 * which causes an HDEC interrupt in all threads within 2ns
789 * because the HDEC register is shared between all 4 threads.
790 * However, we don't need to bother if this is an HDEC
791 * interrupt, since the other threads will already be on their
792 * way here in that case.
794 cmpwi r3,0x100 /* Are we the first here? */
796 cmpwi r3,1 /* Are any other threads in the guest? */
798 cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER
804 * Send an IPI to any napping threads, since an HDEC interrupt
805 * doesn't wake CPUs up from nap.
807 lwz r3,VCORE_NAPPING_THREADS(r5)
811 andc. r3,r3,r0 /* no sense IPI'ing ourselves */
813 mulli r4,r4,PACA_SIZE /* get paca for thread 0 */
817 ld r8,HSTATE_XICS_PHYS(r6) /* get thread's XICS reg addr */
820 stbcix r0,r7,r8 /* trigger the IPI */
825 /* Secondary threads wait for primary to do partition switch */
826 43: ld r4,VCPU_KVM(r9) /* pointer to struct kvm */
827 ld r5,HSTATE_KVM_VCORE(r13)
832 13: lbz r3,VCORE_IN_GUEST(r5)
838 /* Primary thread waits for all the secondaries to exit guest */
839 15: lwz r3,VCORE_ENTRY_EXIT(r5)
846 /* Primary thread switches back to host partition */
847 ld r6,KVM_HOST_SDR1(r4)
848 lwz r7,KVM_HOST_LPID(r4)
849 li r8,LPID_RSVD /* switch to reserved LPID */
852 mtspr SPRN_SDR1,r6 /* switch to partition page table */
856 stb r0,VCORE_IN_GUEST(r5)
857 lis r8,0x7fff /* MAX_INT@h */
860 16: ld r8,KVM_HOST_LPCR(r4)
866 * PPC970 guest -> host partition switch code.
867 * We have to lock against concurrent tlbies, and
868 * we have to flush the whole TLB.
870 32: ld r4,VCPU_KVM(r9) /* pointer to struct kvm */
872 /* Take the guest's tlbie_lock */
873 lwz r8,PACA_LOCK_TOKEN(r13)
874 addi r3,r4,KVM_TLBIE_LOCK
882 ld r7,KVM_HOST_LPCR(r4) /* use kvm->arch.host_lpcr for HID4 */
884 rotldi r0,r0,HID4_LPID5_SH /* all lpid bits in HID4 = 1 */
888 mtspr SPRN_HID4,r0 /* switch to reserved LPID */
891 stw r0,0(r3) /* drop guest tlbie_lock */
893 /* invalidate the whole TLB */
902 /* take native_tlbie_lock */
903 ld r3,toc_tlbie_lock@toc(2)
911 ld r6,KVM_HOST_SDR1(r4)
912 mtspr SPRN_SDR1,r6 /* switch to host page table */
914 /* Set up host HID4 value */
919 stw r0,0(r3) /* drop native_tlbie_lock */
921 lis r8,0x7fff /* MAX_INT@h */
924 /* Disable HDEC interrupts */
927 rldimi r0,r3, HID0_HDICE_SH, 64-HID0_HDICE_SH-1
937 /* load host SLB entries */
938 33: ld r8,PACA_SLBSHADOWPTR(r13)
941 ld r5,SLBSHADOW_SAVEAREA(r8)
942 ld r6,SLBSHADOW_SAVEAREA+8(r8)
943 andis. r7,r5,SLB_ESID_V@h
949 /* Save and reset AMR and UAMOR before turning on the MMU */
954 std r6,VCPU_UAMOR(r9)
957 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
959 /* Switch DSCR back to host value */
962 ld r7, HSTATE_DSCR(r13)
963 std r8, VCPU_DSCR(r7)
965 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
967 /* Save non-volatile GPRs */
968 std r14, VCPU_GPR(R14)(r9)
969 std r15, VCPU_GPR(R15)(r9)
970 std r16, VCPU_GPR(R16)(r9)
971 std r17, VCPU_GPR(R17)(r9)
972 std r18, VCPU_GPR(R18)(r9)
973 std r19, VCPU_GPR(R19)(r9)
974 std r20, VCPU_GPR(R20)(r9)
975 std r21, VCPU_GPR(R21)(r9)
976 std r22, VCPU_GPR(R22)(r9)
977 std r23, VCPU_GPR(R23)(r9)
978 std r24, VCPU_GPR(R24)(r9)
979 std r25, VCPU_GPR(R25)(r9)
980 std r26, VCPU_GPR(R26)(r9)
981 std r27, VCPU_GPR(R27)(r9)
982 std r28, VCPU_GPR(R28)(r9)
983 std r29, VCPU_GPR(R29)(r9)
984 std r30, VCPU_GPR(R30)(r9)
985 std r31, VCPU_GPR(R31)(r9)
992 std r3, VCPU_SPRG0(r9)
993 std r4, VCPU_SPRG1(r9)
994 std r5, VCPU_SPRG2(r9)
995 std r6, VCPU_SPRG3(r9)
1001 /* Increment yield count if they have a VPA */
1002 ld r8, VCPU_VPA(r9) /* do they have a VPA? */
1005 lwz r3, LPPACA_YIELDCOUNT(r8)
1007 stw r3, LPPACA_YIELDCOUNT(r8)
1009 /* Save PMU registers if requested */
1010 /* r8 and cr0.eq are live here */
1012 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
1013 mfspr r4, SPRN_MMCR0 /* save MMCR0 */
1014 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
1015 mfspr r6, SPRN_MMCRA
1017 /* On P7, clear MMCRA in order to disable SDAR updates */
1019 mtspr SPRN_MMCRA, r7
1020 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
1022 beq 21f /* if no VPA, save PMU stuff anyway */
1023 lbz r7, LPPACA_PMCINUSE(r8)
1024 cmpwi r7, 0 /* did they ask for PMU stuff to be saved? */
1026 std r3, VCPU_MMCR(r9) /* if not, set saved MMCR0 to FC */
1028 21: mfspr r5, SPRN_MMCR1
1029 std r4, VCPU_MMCR(r9)
1030 std r5, VCPU_MMCR + 8(r9)
1031 std r6, VCPU_MMCR + 16(r9)
1039 mfspr r10, SPRN_PMC7
1040 mfspr r11, SPRN_PMC8
1041 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
1042 stw r3, VCPU_PMC(r9)
1043 stw r4, VCPU_PMC + 4(r9)
1044 stw r5, VCPU_PMC + 8(r9)
1045 stw r6, VCPU_PMC + 12(r9)
1046 stw r7, VCPU_PMC + 16(r9)
1047 stw r8, VCPU_PMC + 20(r9)
1049 stw r10, VCPU_PMC + 24(r9)
1050 stw r11, VCPU_PMC + 28(r9)
1051 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
1054 /* Secondary threads go off to take a nap on POWER7 */
1056 lwz r0,VCPU_PTID(r9)
1059 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
1061 /* Restore host DABR and DABRX */
1062 ld r5,HSTATE_DABR(r13)
1068 ld r3,HSTATE_SPRG3(r13)
1072 * Reload DEC. HDEC interrupts were disabled when
1073 * we reloaded the host's LPCR value.
1075 ld r3, HSTATE_DECEXP(r13)
1080 /* Reload the host's PMU registers */
1081 ld r3, PACALPPACAPTR(r13) /* is the host using the PMU? */
1082 lbz r4, LPPACA_PMCINUSE(r3)
1084 beq 23f /* skip if not */
1085 lwz r3, HSTATE_PMC(r13)
1086 lwz r4, HSTATE_PMC + 4(r13)
1087 lwz r5, HSTATE_PMC + 8(r13)
1088 lwz r6, HSTATE_PMC + 12(r13)
1089 lwz r8, HSTATE_PMC + 16(r13)
1090 lwz r9, HSTATE_PMC + 20(r13)
1092 lwz r10, HSTATE_PMC + 24(r13)
1093 lwz r11, HSTATE_PMC + 28(r13)
1094 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
1102 mtspr SPRN_PMC7, r10
1103 mtspr SPRN_PMC8, r11
1104 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
1105 ld r3, HSTATE_MMCR(r13)
1106 ld r4, HSTATE_MMCR + 8(r13)
1107 ld r5, HSTATE_MMCR + 16(r13)
1108 mtspr SPRN_MMCR1, r4
1109 mtspr SPRN_MMCRA, r5
1110 mtspr SPRN_MMCR0, r3
1114 * For external and machine check interrupts, we need
1115 * to call the Linux handler to process the interrupt.
1116 * We do that by jumping to the interrupt vector address
1117 * which we have in r12. The [h]rfid at the end of the
1118 * handler will return to the book3s_hv_interrupts.S code.
1119 * For other interrupts we do the rfid to get back
1120 * to the book3s_interrupts.S code here.
1122 ld r8, HSTATE_VMHANDLER(r13)
1123 ld r7, HSTATE_HOST_MSR(r13)
1125 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
1127 cmpwi r12, BOOK3S_INTERRUPT_MACHINE_CHECK
1129 /* RFI into the highmem handler, or branch to interrupt handler */
1134 mtmsrd r6, 1 /* Clear RI in MSR */
1143 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
1144 mtspr SPRN_HSRR0, r8
1145 mtspr SPRN_HSRR1, r7
1149 * Check whether an HDSI is an HPTE not found fault or something else.
1150 * If it is an HPTE not found fault that is due to the guest accessing
1151 * a page that they have mapped but which we have paged out, then
1152 * we continue on with the guest exit path. In all other cases,
1153 * reflect the HDSI to the guest as a DSI.
1157 mfspr r6, SPRN_HDSISR
1158 /* HPTE not found fault or protection fault? */
1159 andis. r0, r6, (DSISR_NOHPTE | DSISR_PROTFAULT)@h
1160 beq 1f /* if not, send it to the guest */
1161 andi. r0, r11, MSR_DR /* data relocation enabled? */
1164 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
1165 bne 1f /* if no SLB entry found */
1166 4: std r4, VCPU_FAULT_DAR(r9)
1167 stw r6, VCPU_FAULT_DSISR(r9)
1169 /* Search the hash table. */
1170 mr r3, r9 /* vcpu pointer */
1171 li r7, 1 /* data fault */
1172 bl .kvmppc_hpte_hv_fault
1173 ld r9, HSTATE_KVM_VCPU(r13)
1175 ld r11, VCPU_MSR(r9)
1176 li r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1177 cmpdi r3, 0 /* retry the instruction */
1179 cmpdi r3, -1 /* handle in kernel mode */
1181 cmpdi r3, -2 /* MMIO emulation; need instr word */
1184 /* Synthesize a DSI for the guest */
1185 ld r4, VCPU_FAULT_DAR(r9)
1187 1: mtspr SPRN_DAR, r4
1188 mtspr SPRN_DSISR, r6
1189 mtspr SPRN_SRR0, r10
1190 mtspr SPRN_SRR1, r11
1191 li r10, BOOK3S_INTERRUPT_DATA_STORAGE
1192 li r11, (MSR_ME << 1) | 1 /* synthesize MSR_SF | MSR_ME */
1194 6: ld r7, VCPU_CTR(r9)
1195 lwz r8, VCPU_XER(r9)
1201 3: ld r5, VCPU_KVM(r9) /* not relocated, use VRMA */
1202 ld r5, KVM_VRMA_SLB_V(r5)
1205 /* If this is for emulated MMIO, load the instruction word */
1206 2: li r8, KVM_INST_FETCH_FAILED /* In case lwz faults */
1208 /* Set guest mode to 'jump over instruction' so if lwz faults
1209 * we'll just continue at the next IP. */
1210 li r0, KVM_GUEST_MODE_SKIP
1211 stb r0, HSTATE_IN_GUEST(r13)
1213 /* Do the access with MSR:DR enabled */
1215 ori r4, r3, MSR_DR /* Enable paging for data */
1220 /* Store the result */
1221 stw r8, VCPU_LAST_INST(r9)
1223 /* Unset guest mode. */
1224 li r0, KVM_GUEST_MODE_NONE
1225 stb r0, HSTATE_IN_GUEST(r13)
1229 * Similarly for an HISI, reflect it to the guest as an ISI unless
1230 * it is an HPTE not found fault for a page that we have paged out.
1233 andis. r0, r11, SRR1_ISI_NOPT@h
1235 andi. r0, r11, MSR_IR /* instruction relocation enabled? */
1238 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
1239 bne 1f /* if no SLB entry found */
1241 /* Search the hash table. */
1242 mr r3, r9 /* vcpu pointer */
1245 li r7, 0 /* instruction fault */
1246 bl .kvmppc_hpte_hv_fault
1247 ld r9, HSTATE_KVM_VCPU(r13)
1249 ld r11, VCPU_MSR(r9)
1250 li r12, BOOK3S_INTERRUPT_H_INST_STORAGE
1251 cmpdi r3, 0 /* retry the instruction */
1253 cmpdi r3, -1 /* handle in kernel mode */
1256 /* Synthesize an ISI for the guest */
1258 1: mtspr SPRN_SRR0, r10
1259 mtspr SPRN_SRR1, r11
1260 li r10, BOOK3S_INTERRUPT_INST_STORAGE
1261 li r11, (MSR_ME << 1) | 1 /* synthesize MSR_SF | MSR_ME */
1263 6: ld r7, VCPU_CTR(r9)
1264 lwz r8, VCPU_XER(r9)
1270 3: ld r6, VCPU_KVM(r9) /* not relocated, use VRMA */
1271 ld r5, KVM_VRMA_SLB_V(r6)
1275 * Try to handle an hcall in real mode.
1276 * Returns to the guest if we handle it, or continues on up to
1277 * the kernel if we can't (i.e. if we don't have a handler for
1278 * it, or if the handler returns H_TOO_HARD).
1280 .globl hcall_try_real_mode
1281 hcall_try_real_mode:
1282 ld r3,VCPU_GPR(R3)(r9)
1286 cmpldi r3,hcall_real_table_end - hcall_real_table
1288 LOAD_REG_ADDR(r4, hcall_real_table)
1294 mr r3,r9 /* get vcpu pointer */
1295 ld r4,VCPU_GPR(R4)(r9)
1298 beq hcall_real_fallback
1299 ld r4,HSTATE_KVM_VCPU(r13)
1300 std r3,VCPU_GPR(R3)(r4)
1305 /* We've attempted a real mode hcall, but it's punted it back
1306 * to userspace. We need to restore some clobbered volatiles
1307 * before resuming the pass-it-to-qemu path */
1308 hcall_real_fallback:
1309 li r12,BOOK3S_INTERRUPT_SYSCALL
1310 ld r9, HSTATE_KVM_VCPU(r13)
1314 .globl hcall_real_table
1316 .long 0 /* 0 - unused */
1317 .long .kvmppc_h_remove - hcall_real_table
1318 .long .kvmppc_h_enter - hcall_real_table
1319 .long .kvmppc_h_read - hcall_real_table
1320 .long 0 /* 0x10 - H_CLEAR_MOD */
1321 .long 0 /* 0x14 - H_CLEAR_REF */
1322 .long .kvmppc_h_protect - hcall_real_table
1323 .long 0 /* 0x1c - H_GET_TCE */
1324 .long .kvmppc_h_put_tce - hcall_real_table
1325 .long 0 /* 0x24 - H_SET_SPRG0 */
1326 .long .kvmppc_h_set_dabr - hcall_real_table
1372 .long .kvmppc_h_cede - hcall_real_table
1389 .long .kvmppc_h_bulk_remove - hcall_real_table
1390 hcall_real_table_end:
1396 bounce_ext_interrupt:
1400 li r10,BOOK3S_INTERRUPT_EXTERNAL
1401 li r11,(MSR_ME << 1) | 1 /* synthesize MSR_SF | MSR_ME */
1405 _GLOBAL(kvmppc_h_set_dabr)
1406 std r4,VCPU_DABR(r3)
1407 /* Work around P7 bug where DABR can get corrupted on mtspr */
1408 1: mtspr SPRN_DABR,r4
1416 _GLOBAL(kvmppc_h_cede)
1418 std r11,VCPU_MSR(r3)
1420 stb r0,VCPU_CEDED(r3)
1421 sync /* order setting ceded vs. testing prodded */
1422 lbz r5,VCPU_PRODDED(r3)
1424 bne kvm_cede_prodded
1425 li r0,0 /* set trap to 0 to say hcall is handled */
1426 stw r0,VCPU_TRAP(r3)
1428 std r0,VCPU_GPR(R3)(r3)
1430 b kvm_cede_exit /* just send it up to host on 970 */
1431 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_206)
1434 * Set our bit in the bitmask of napping threads unless all the
1435 * other threads are already napping, in which case we send this
1438 ld r5,HSTATE_KVM_VCORE(r13)
1439 lwz r6,VCPU_PTID(r3)
1440 lwz r8,VCORE_ENTRY_EXIT(r5)
1444 addi r6,r5,VCORE_NAPPING_THREADS
1453 stb r0,HSTATE_NAPPING(r13)
1454 /* order napping_threads update vs testing entry_exit_count */
1457 lwz r7,VCORE_ENTRY_EXIT(r5)
1459 bge 33f /* another thread already exiting */
1462 * Although not specifically required by the architecture, POWER7
1463 * preserves the following registers in nap mode, even if an SMT mode
1464 * switch occurs: SLB entries, PURR, SPURR, AMOR, UAMOR, AMR, SPRG0-3,
1465 * DAR, DSISR, DABR, DABRX, DSCR, PMCx, MMCRx, SIAR, SDAR.
1467 /* Save non-volatile GPRs */
1468 std r14, VCPU_GPR(R14)(r3)
1469 std r15, VCPU_GPR(R15)(r3)
1470 std r16, VCPU_GPR(R16)(r3)
1471 std r17, VCPU_GPR(R17)(r3)
1472 std r18, VCPU_GPR(R18)(r3)
1473 std r19, VCPU_GPR(R19)(r3)
1474 std r20, VCPU_GPR(R20)(r3)
1475 std r21, VCPU_GPR(R21)(r3)
1476 std r22, VCPU_GPR(R22)(r3)
1477 std r23, VCPU_GPR(R23)(r3)
1478 std r24, VCPU_GPR(R24)(r3)
1479 std r25, VCPU_GPR(R25)(r3)
1480 std r26, VCPU_GPR(R26)(r3)
1481 std r27, VCPU_GPR(R27)(r3)
1482 std r28, VCPU_GPR(R28)(r3)
1483 std r29, VCPU_GPR(R29)(r3)
1484 std r30, VCPU_GPR(R30)(r3)
1485 std r31, VCPU_GPR(R31)(r3)
1491 * Take a nap until a decrementer or external interrupt occurs,
1492 * with PECE1 (wake on decr) and PECE0 (wake on external) set in LPCR
1495 stb r0,HSTATE_HWTHREAD_REQ(r13)
1497 ori r5,r5,LPCR_PECE0 | LPCR_PECE1
1501 std r0, HSTATE_SCRATCH0(r13)
1503 ld r0, HSTATE_SCRATCH0(r13)
1510 /* Woken by external or decrementer interrupt */
1511 ld r1, HSTATE_HOST_R1(r13)
1513 /* load up FP state */
1517 ld r14, VCPU_GPR(R14)(r4)
1518 ld r15, VCPU_GPR(R15)(r4)
1519 ld r16, VCPU_GPR(R16)(r4)
1520 ld r17, VCPU_GPR(R17)(r4)
1521 ld r18, VCPU_GPR(R18)(r4)
1522 ld r19, VCPU_GPR(R19)(r4)
1523 ld r20, VCPU_GPR(R20)(r4)
1524 ld r21, VCPU_GPR(R21)(r4)
1525 ld r22, VCPU_GPR(R22)(r4)
1526 ld r23, VCPU_GPR(R23)(r4)
1527 ld r24, VCPU_GPR(R24)(r4)
1528 ld r25, VCPU_GPR(R25)(r4)
1529 ld r26, VCPU_GPR(R26)(r4)
1530 ld r27, VCPU_GPR(R27)(r4)
1531 ld r28, VCPU_GPR(R28)(r4)
1532 ld r29, VCPU_GPR(R29)(r4)
1533 ld r30, VCPU_GPR(R30)(r4)
1534 ld r31, VCPU_GPR(R31)(r4)
1536 /* clear our bit in vcore->napping_threads */
1537 33: ld r5,HSTATE_KVM_VCORE(r13)
1538 lwz r3,VCPU_PTID(r4)
1541 addi r6,r5,VCORE_NAPPING_THREADS
1547 stb r0,HSTATE_NAPPING(r13)
1549 /* see if any other thread is already exiting */
1550 lwz r0,VCORE_ENTRY_EXIT(r5)
1552 blt kvmppc_cede_reentry /* if not go back to guest */
1554 /* some threads are exiting, so go to the guest exit path */
1555 b hcall_real_fallback
1557 /* cede when already previously prodded case */
1560 stb r0,VCPU_PRODDED(r3)
1561 sync /* order testing prodded vs. clearing ceded */
1562 stb r0,VCPU_CEDED(r3)
1566 /* we've ceded but we want to give control to the host */
1572 ld r5,HSTATE_KVM_VCORE(r13)
1574 13: lbz r3,VCORE_IN_GUEST(r5)
1578 ld r11,PACA_SLBSHADOWPTR(r13)
1580 .rept SLB_NUM_BOLTED
1581 ld r5,SLBSHADOW_SAVEAREA(r11)
1582 ld r6,SLBSHADOW_SAVEAREA+8(r11)
1583 andis. r7,r5,SLB_ESID_V@h
1590 /* Clear any pending IPI - assume we're a secondary thread */
1591 ld r5, HSTATE_XICS_PHYS(r13)
1593 lwzcix r3, r5, r7 /* ack any pending interrupt */
1594 rlwinm. r0, r3, 0, 0xffffff /* any pending? */
1599 stbcix r0, r5, r6 /* clear the IPI */
1600 stwcix r3, r5, r7 /* EOI it */
1603 /* increment the nap count and then go to nap mode */
1604 ld r4, HSTATE_KVM_VCORE(r13)
1605 addi r4, r4, VCORE_NAP_COUNT
1606 lwsync /* make previous updates visible */
1613 li r0, KVM_HWTHREAD_IN_NAP
1614 stb r0, HSTATE_HWTHREAD_STATE(r13)
1616 std r0, HSTATE_KVM_VCPU(r13)
1620 rlwimi r4, r3, 0, LPCR_PECE0 | LPCR_PECE1
1623 std r0, HSTATE_SCRATCH0(r13)
1625 ld r0, HSTATE_SCRATCH0(r13)
1632 * Save away FP, VMX and VSX registers.
1635 _GLOBAL(kvmppc_save_fp)
1638 #ifdef CONFIG_ALTIVEC
1640 oris r8,r8,MSR_VEC@h
1641 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
1645 oris r8,r8,MSR_VSX@h
1646 END_FTR_SECTION_IFSET(CPU_FTR_VSX)
1654 li r6,reg*16+VCPU_VSRS
1662 stfd reg,reg*8+VCPU_FPRS(r3)
1666 ALT_FTR_SECTION_END_IFSET(CPU_FTR_VSX)
1669 stfd fr0,VCPU_FPSCR(r3)
1671 #ifdef CONFIG_ALTIVEC
1675 li r6,reg*16+VCPU_VRS
1682 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
1684 mfspr r6,SPRN_VRSAVE
1685 stw r6,VCPU_VRSAVE(r3)
1691 * Load up FP, VMX and VSX registers
1694 .globl kvmppc_load_fp
1698 #ifdef CONFIG_ALTIVEC
1700 oris r8,r8,MSR_VEC@h
1701 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
1705 oris r8,r8,MSR_VSX@h
1706 END_FTR_SECTION_IFSET(CPU_FTR_VSX)
1710 lfd fr0,VCPU_FPSCR(r4)
1716 li r7,reg*16+VCPU_VSRS
1724 lfd reg,reg*8+VCPU_FPRS(r4)
1728 ALT_FTR_SECTION_END_IFSET(CPU_FTR_VSX)
1731 #ifdef CONFIG_ALTIVEC
1738 li r7,reg*16+VCPU_VRS
1742 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
1744 lwz r7,VCPU_VRSAVE(r4)
1745 mtspr SPRN_VRSAVE,r7