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 /* NV GPR values from power7_idle() will no longer be valid */
116 stb r0, PACA_NAPSTATELOST(r13)
118 .global kvmppc_hv_entry
127 * all other volatile GPRS = free
130 std r0, HSTATE_VMHANDLER(r13)
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)
151 /* Load guest PMU registers */
152 /* R4 is live here (vcpu pointer) */
154 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
155 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
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)
164 lwz r10, VCPU_PMC + 24(r4)
165 lwz r11, VCPU_PMC + 28(r4)
166 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
176 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
178 ld r5, VCPU_MMCR + 8(r4)
179 ld r6, VCPU_MMCR + 16(r4)
185 /* Load up FP, VMX and VSX registers */
189 /* Switch DSCR to guest value */
192 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
195 * Set the decrementer to the guest decrementer.
197 ld r8,VCPU_DEC_EXPIRES(r4)
203 ld r5, VCPU_SPRG0(r4)
204 ld r6, VCPU_SPRG1(r4)
205 ld r7, VCPU_SPRG2(r4)
206 ld r8, VCPU_SPRG3(r4)
212 /* Save R1 in the PACA */
213 std r1, HSTATE_HOST_R1(r13)
215 /* Increment yield count if they have a VPA */
219 lwz r5, LPPACA_YIELDCOUNT(r3)
221 stw r5, LPPACA_YIELDCOUNT(r3)
223 /* Load up DAR and DSISR */
225 lwz r6, VCPU_DSISR(r4)
229 /* Set partition DABR */
236 /* Restore AMR and UAMOR, set AMOR to all 1s */
243 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
253 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
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.
259 /* Increment entry count iff exit count is zero. */
260 ld r5,HSTATE_KVM_VCORE(r13)
261 addi r9,r5,VCORE_ENTRY_EXIT
263 cmpwi r3,0x100 /* any threads starting to exit? */
264 bge secondary_too_late /* if so we're too late to the party */
269 /* Primary thread switches to guest partition. */
270 ld r9,VCPU_KVM(r4) /* pointer to struct kvm */
276 li r0,LPID_RSVD /* switch to reserved LPID */
279 mtspr SPRN_SDR1,r6 /* switch to partition page table */
283 stb r0,VCORE_IN_GUEST(r5) /* signal secondaries to continue */
286 /* Secondary threads wait for primary to have done partition switch */
287 20: lbz r0,VCORE_IN_GUEST(r5)
291 /* Set LPCR and RMOR. */
292 10: ld r8,KVM_LPCR(r9)
298 /* Check if HDEC expires soon */
301 li r12,BOOK3S_INTERRUPT_HV_DECREMENTER
306 * Invalidate the TLB if we could possibly have stale TLB
307 * entries for this partition on this core due to the use
309 * XXX maybe only need this on primary thread?
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? */
321 cmpw r0,r5 /* same vcpu as this core last ran? */
323 3: sth r6,VCPU_LAST_CPU(r4) /* if not, invalidate partition TLB */
324 sth r5,KVM_LAST_VCPU(r7)
327 li r7,0x800 /* IS field = 0b10 */
335 /* Save purr/spurr */
338 std r5,HSTATE_PURR(r13)
339 std r6,HSTATE_SPURR(r13)
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.
354 30: ld r9,VCPU_KVM(r4) /* pointer to struct kvm */
356 /* first take native_tlbie_lock */
359 .tc native_tlbie_lock[TC],native_tlbie_lock
361 ld r3,toc_tlbie_lock@toc(2)
362 lwz r8,PACA_LOCK_TOKEN(r13)
370 ld r7,KVM_LPCR(r9) /* use kvm->arch.lpcr to store HID4 */
372 rotldi r0,r0,HID4_LPID5_SH /* all lpid bits in HID4 = 1 */
376 mtspr SPRN_HID4,r0 /* switch to reserved LPID */
379 stw r0,0(r3) /* drop native_tlbie_lock */
381 /* invalidate the whole TLB */
390 /* Take the guest's tlbie_lock */
391 addi r3,r9,KVM_TLBIE_LOCK
399 mtspr SPRN_SDR1,r6 /* switch to partition page table */
401 /* Set up HID4 with the guest's LPID etc. */
406 /* drop the guest's tlbie_lock */
410 /* Check if HDEC expires soon */
413 li r12,BOOK3S_INTERRUPT_HV_DECREMENTER
417 /* Enable HDEC interrupts */
420 rldimi r0,r3, HID0_HDICE_SH, 64-HID0_HDICE_SH-1
430 /* Load up guest SLB entries */
431 31: lwz r5,VCPU_SLB_MAX(r4)
436 1: ld r8,VCPU_SLB_E(r6)
439 addi r6,r6,VCPU_SLB_SIZE
443 /* Restore state of CTRL run bit; assume 1 on entry */
457 kvmppc_cede_reentry: /* r4 = vcpu, r13 = paca */
461 ld r11, VCPU_MSR(r4) /* r11 = vcpu->arch.msr & ~MSR_HV */
463 rldicl r11, r11, 63 - MSR_HV_LG, 1
464 rotldi r11, r11, 1 + MSR_HV_LG
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
480 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
482 li r0,BOOK3S_INTERRUPT_EXTERNAL
486 li r11,(MSR_ME << 1) | 1 /* synthesize MSR_SF | MSR_ME */
492 li r0,BOOK3S_INTERRUPT_DECREMENTER
495 /* Move SRR0 and SRR1 into the respective regs */
496 5: mtspr SPRN_SRR0, r6
499 stb r0,VCPU_CEDED(r4) /* cancel cede */
505 /* Activate guest mode, so faults get handled by KVM */
506 li r9, KVM_GUEST_MODE_GUEST
507 stb r9, HSTATE_IN_GUEST(r13)
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)
530 ld r4, VCPU_GPR(r4)(r4)
535 /******************************************************************************
539 *****************************************************************************/
542 * We come here from the first-level interrupt handlers.
544 .globl kvmppc_interrupt
548 * R12 = interrupt vector
550 * guest CR, R12 saved in shadow VCPU SCRATCH1/0
551 * guest R13 saved in SPRN_SCRATCH0
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)
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)
577 /* Restore R1/R2 so we can handle faults */
578 ld r1, HSTATE_HOST_R1(r13)
583 std r10, VCPU_SRR0(r9)
584 std r11, VCPU_SRR1(r9)
585 andi. r0, r12, 2 /* need to read HSRR0/1? */
587 mfspr r10, SPRN_HSRR0
588 mfspr r11, SPRN_HSRR1
590 1: std r10, VCPU_PC(r9)
591 std r11, VCPU_MSR(r9)
595 std r3, VCPU_GPR(r13)(r9)
598 /* Unset guest mode */
599 li r0, KVM_GUEST_MODE_NONE
600 stb r0, HSTATE_IN_GUEST(r13)
602 stw r12,VCPU_TRAP(r9)
604 /* See if this is a leftover HDEC interrupt */
605 cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER
611 /* See if this is something we can handle in real mode */
612 cmpwi r12,BOOK3S_INTERRUPT_SYSCALL
613 beq hcall_try_real_mode
615 /* Check for mediated interrupts (could be done earlier really ...) */
617 cmpwi r12,BOOK3S_INTERRUPT_EXTERNAL
623 bne bounce_ext_interrupt
625 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
627 hcall_real_cont: /* r9 = vcpu, r12 = trap, r13 = paca */
633 std r5,VCPU_DEC_EXPIRES(r9)
635 /* Save HEIR (HV emulation assist reg) in last_inst
636 if this is an HEI (HV emulation interrupt, e40) */
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 /* Save more register state */
653 stw r7, VCPU_DSISR(r9)
655 /* grab HDAR & HDSISR if HV data storage interrupt (HDSI) */
657 cmpwi r12,BOOK3S_INTERRUPT_H_DATA_STORAGE
659 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
660 7: std r6, VCPU_FAULT_DAR(r9)
661 stw r7, VCPU_FAULT_DSISR(r9)
663 /* Save guest CTRL register, set runlatch to 1 */
671 /* Read the guest SLB and save it away */
672 lwz r0,VCPU_SLB_NR(r9) /* number of entries in SLB */
678 andis. r0,r8,SLB_ESID_V@h
680 add r8,r8,r6 /* put index in */
682 std r8,VCPU_SLB_E(r7)
683 std r3,VCPU_SLB_V(r7)
684 addi r7,r7,VCPU_SLB_SIZE
688 stw r5,VCPU_SLB_MAX(r9)
691 * Save the guest PURR/SPURR
699 std r6,VCPU_SPURR(r9)
704 * Restore host PURR/SPURR and add guest times
705 * so that the time in the guest gets accounted.
707 ld r3,HSTATE_PURR(r13)
708 ld r4,HSTATE_SPURR(r13)
713 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_201)
721 hdec_soon: /* r9 = vcpu, r12 = trap, r13 = paca */
724 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
726 * POWER7 guest -> host partition switch code.
727 * We don't have to lock against tlbies but we do
728 * have to coordinate the hardware threads.
730 /* Increment the threads-exiting-guest count in the 0xff00
731 bits of vcore->entry_exit_count */
733 ld r5,HSTATE_KVM_VCORE(r13)
734 addi r6,r5,VCORE_ENTRY_EXIT
742 * At this point we have an interrupt that we have to pass
743 * up to the kernel or qemu; we can't handle it in real mode.
744 * Thus we have to do a partition switch, so we have to
745 * collect the other threads, if we are the first thread
746 * to take an interrupt. To do this, we set the HDEC to 0,
747 * which causes an HDEC interrupt in all threads within 2ns
748 * because the HDEC register is shared between all 4 threads.
749 * However, we don't need to bother if this is an HDEC
750 * interrupt, since the other threads will already be on their
751 * way here in that case.
753 cmpwi r3,0x100 /* Are we the first here? */
755 cmpwi r3,1 /* Are any other threads in the guest? */
757 cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER
763 * Send an IPI to any napping threads, since an HDEC interrupt
764 * doesn't wake CPUs up from nap.
766 lwz r3,VCORE_NAPPING_THREADS(r5)
770 andc. r3,r3,r0 /* no sense IPI'ing ourselves */
772 mulli r4,r4,PACA_SIZE /* get paca for thread 0 */
776 ld r8,HSTATE_XICS_PHYS(r6) /* get thread's XICS reg addr */
779 stbcix r0,r7,r8 /* trigger the IPI */
784 /* Secondary threads wait for primary to do partition switch */
785 43: ld r4,VCPU_KVM(r9) /* pointer to struct kvm */
786 ld r5,HSTATE_KVM_VCORE(r13)
791 13: lbz r3,VCORE_IN_GUEST(r5)
797 /* Primary thread waits for all the secondaries to exit guest */
798 15: lwz r3,VCORE_ENTRY_EXIT(r5)
805 /* Primary thread switches back to host partition */
806 ld r6,KVM_HOST_SDR1(r4)
807 lwz r7,KVM_HOST_LPID(r4)
808 li r8,LPID_RSVD /* switch to reserved LPID */
811 mtspr SPRN_SDR1,r6 /* switch to partition page table */
815 stb r0,VCORE_IN_GUEST(r5)
816 lis r8,0x7fff /* MAX_INT@h */
819 16: ld r8,KVM_HOST_LPCR(r4)
825 * PPC970 guest -> host partition switch code.
826 * We have to lock against concurrent tlbies, and
827 * we have to flush the whole TLB.
829 32: ld r4,VCPU_KVM(r9) /* pointer to struct kvm */
831 /* Take the guest's tlbie_lock */
832 lwz r8,PACA_LOCK_TOKEN(r13)
833 addi r3,r4,KVM_TLBIE_LOCK
841 ld r7,KVM_HOST_LPCR(r4) /* use kvm->arch.host_lpcr for HID4 */
843 rotldi r0,r0,HID4_LPID5_SH /* all lpid bits in HID4 = 1 */
847 mtspr SPRN_HID4,r0 /* switch to reserved LPID */
850 stw r0,0(r3) /* drop guest tlbie_lock */
852 /* invalidate the whole TLB */
861 /* take native_tlbie_lock */
862 ld r3,toc_tlbie_lock@toc(2)
870 ld r6,KVM_HOST_SDR1(r4)
871 mtspr SPRN_SDR1,r6 /* switch to host page table */
873 /* Set up host HID4 value */
878 stw r0,0(r3) /* drop native_tlbie_lock */
880 lis r8,0x7fff /* MAX_INT@h */
883 /* Disable HDEC interrupts */
886 rldimi r0,r3, HID0_HDICE_SH, 64-HID0_HDICE_SH-1
896 /* load host SLB entries */
897 33: ld r8,PACA_SLBSHADOWPTR(r13)
900 ld r5,SLBSHADOW_SAVEAREA(r8)
901 ld r6,SLBSHADOW_SAVEAREA+8(r8)
902 andis. r7,r5,SLB_ESID_V@h
908 /* Save and reset AMR and UAMOR before turning on the MMU */
913 std r6,VCPU_UAMOR(r9)
916 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
918 /* Restore host DABR and DABRX */
919 ld r5,HSTATE_DABR(r13)
924 /* Switch DSCR back to host value */
927 ld r7, HSTATE_DSCR(r13)
928 std r8, VCPU_DSCR(r7)
930 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
932 /* Save non-volatile GPRs */
933 std r14, VCPU_GPR(r14)(r9)
934 std r15, VCPU_GPR(r15)(r9)
935 std r16, VCPU_GPR(r16)(r9)
936 std r17, VCPU_GPR(r17)(r9)
937 std r18, VCPU_GPR(r18)(r9)
938 std r19, VCPU_GPR(r19)(r9)
939 std r20, VCPU_GPR(r20)(r9)
940 std r21, VCPU_GPR(r21)(r9)
941 std r22, VCPU_GPR(r22)(r9)
942 std r23, VCPU_GPR(r23)(r9)
943 std r24, VCPU_GPR(r24)(r9)
944 std r25, VCPU_GPR(r25)(r9)
945 std r26, VCPU_GPR(r26)(r9)
946 std r27, VCPU_GPR(r27)(r9)
947 std r28, VCPU_GPR(r28)(r9)
948 std r29, VCPU_GPR(r29)(r9)
949 std r30, VCPU_GPR(r30)(r9)
950 std r31, VCPU_GPR(r31)(r9)
957 std r3, VCPU_SPRG0(r9)
958 std r4, VCPU_SPRG1(r9)
959 std r5, VCPU_SPRG2(r9)
960 std r6, VCPU_SPRG3(r9)
962 /* Increment yield count if they have a VPA */
963 ld r8, VCPU_VPA(r9) /* do they have a VPA? */
966 lwz r3, LPPACA_YIELDCOUNT(r8)
968 stw r3, LPPACA_YIELDCOUNT(r8)
970 /* Save PMU registers if requested */
971 /* r8 and cr0.eq are live here */
973 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
974 mfspr r4, SPRN_MMCR0 /* save MMCR0 */
975 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
977 beq 21f /* if no VPA, save PMU stuff anyway */
978 lbz r7, LPPACA_PMCINUSE(r8)
979 cmpwi r7, 0 /* did they ask for PMU stuff to be saved? */
981 std r3, VCPU_MMCR(r9) /* if not, set saved MMCR0 to FC */
983 21: mfspr r5, SPRN_MMCR1
985 std r4, VCPU_MMCR(r9)
986 std r5, VCPU_MMCR + 8(r9)
987 std r6, VCPU_MMCR + 16(r9)
997 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
999 stw r4, VCPU_PMC + 4(r9)
1000 stw r5, VCPU_PMC + 8(r9)
1001 stw r6, VCPU_PMC + 12(r9)
1002 stw r7, VCPU_PMC + 16(r9)
1003 stw r8, VCPU_PMC + 20(r9)
1005 stw r10, VCPU_PMC + 24(r9)
1006 stw r11, VCPU_PMC + 28(r9)
1007 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
1013 /* Secondary threads go off to take a nap on POWER7 */
1015 lwz r0,VCPU_PTID(r3)
1018 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
1021 * Reload DEC. HDEC interrupts were disabled when
1022 * we reloaded the host's LPCR value.
1024 ld r3, HSTATE_DECEXP(r13)
1029 /* Reload the host's PMU registers */
1030 ld r3, PACALPPACAPTR(r13) /* is the host using the PMU? */
1031 lbz r4, LPPACA_PMCINUSE(r3)
1033 beq 23f /* skip if not */
1034 lwz r3, HSTATE_PMC(r13)
1035 lwz r4, HSTATE_PMC + 4(r13)
1036 lwz r5, HSTATE_PMC + 8(r13)
1037 lwz r6, HSTATE_PMC + 12(r13)
1038 lwz r8, HSTATE_PMC + 16(r13)
1039 lwz r9, HSTATE_PMC + 20(r13)
1041 lwz r10, HSTATE_PMC + 24(r13)
1042 lwz r11, HSTATE_PMC + 28(r13)
1043 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
1051 mtspr SPRN_PMC7, r10
1052 mtspr SPRN_PMC8, r11
1053 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
1054 ld r3, HSTATE_MMCR(r13)
1055 ld r4, HSTATE_MMCR + 8(r13)
1056 ld r5, HSTATE_MMCR + 16(r13)
1057 mtspr SPRN_MMCR1, r4
1058 mtspr SPRN_MMCRA, r5
1059 mtspr SPRN_MMCR0, r3
1063 * For external and machine check interrupts, we need
1064 * to call the Linux handler to process the interrupt.
1065 * We do that by jumping to the interrupt vector address
1066 * which we have in r12. The [h]rfid at the end of the
1067 * handler will return to the book3s_hv_interrupts.S code.
1068 * For other interrupts we do the rfid to get back
1069 * to the book3s_interrupts.S code here.
1071 ld r8, HSTATE_VMHANDLER(r13)
1072 ld r7, HSTATE_HOST_MSR(r13)
1074 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
1076 cmpwi r12, BOOK3S_INTERRUPT_MACHINE_CHECK
1078 /* RFI into the highmem handler, or branch to interrupt handler */
1083 mtmsrd r6, 1 /* Clear RI in MSR */
1092 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
1093 mtspr SPRN_HSRR0, r8
1094 mtspr SPRN_HSRR1, r7
1097 6: mfspr r6,SPRN_HDAR
1098 mfspr r7,SPRN_HDSISR
1102 * Try to handle an hcall in real mode.
1103 * Returns to the guest if we handle it, or continues on up to
1104 * the kernel if we can't (i.e. if we don't have a handler for
1105 * it, or if the handler returns H_TOO_HARD).
1107 .globl hcall_try_real_mode
1108 hcall_try_real_mode:
1109 ld r3,VCPU_GPR(r3)(r9)
1113 cmpldi r3,hcall_real_table_end - hcall_real_table
1115 LOAD_REG_ADDR(r4, hcall_real_table)
1121 mr r3,r9 /* get vcpu pointer */
1122 ld r4,VCPU_GPR(r4)(r9)
1125 beq hcall_real_fallback
1126 ld r4,HSTATE_KVM_VCPU(r13)
1127 std r3,VCPU_GPR(r3)(r4)
1132 /* We've attempted a real mode hcall, but it's punted it back
1133 * to userspace. We need to restore some clobbered volatiles
1134 * before resuming the pass-it-to-qemu path */
1135 hcall_real_fallback:
1136 li r12,BOOK3S_INTERRUPT_SYSCALL
1137 ld r9, HSTATE_KVM_VCPU(r13)
1141 .globl hcall_real_table
1143 .long 0 /* 0 - unused */
1144 .long .kvmppc_h_remove - hcall_real_table
1145 .long .kvmppc_h_enter - hcall_real_table
1146 .long .kvmppc_h_read - hcall_real_table
1147 .long 0 /* 0x10 - H_CLEAR_MOD */
1148 .long 0 /* 0x14 - H_CLEAR_REF */
1149 .long .kvmppc_h_protect - hcall_real_table
1150 .long 0 /* 0x1c - H_GET_TCE */
1151 .long .kvmppc_h_put_tce - hcall_real_table
1152 .long 0 /* 0x24 - H_SET_SPRG0 */
1153 .long .kvmppc_h_set_dabr - hcall_real_table
1199 .long .kvmppc_h_cede - hcall_real_table
1216 .long .kvmppc_h_bulk_remove - hcall_real_table
1217 hcall_real_table_end:
1223 bounce_ext_interrupt:
1227 li r10,BOOK3S_INTERRUPT_EXTERNAL
1228 li r11,(MSR_ME << 1) | 1 /* synthesize MSR_SF | MSR_ME */
1232 _GLOBAL(kvmppc_h_set_dabr)
1233 std r4,VCPU_DABR(r3)
1238 _GLOBAL(kvmppc_h_cede)
1240 std r11,VCPU_MSR(r3)
1242 stb r0,VCPU_CEDED(r3)
1243 sync /* order setting ceded vs. testing prodded */
1244 lbz r5,VCPU_PRODDED(r3)
1247 li r0,0 /* set trap to 0 to say hcall is handled */
1248 stw r0,VCPU_TRAP(r3)
1250 std r0,VCPU_GPR(r3)(r3)
1252 b 2f /* just send it up to host on 970 */
1253 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_206)
1256 * Set our bit in the bitmask of napping threads unless all the
1257 * other threads are already napping, in which case we send this
1260 ld r5,HSTATE_KVM_VCORE(r13)
1261 lwz r6,VCPU_PTID(r3)
1262 lwz r8,VCORE_ENTRY_EXIT(r5)
1266 addi r6,r5,VCORE_NAPPING_THREADS
1275 stb r0,HSTATE_NAPPING(r13)
1276 /* order napping_threads update vs testing entry_exit_count */
1279 lwz r7,VCORE_ENTRY_EXIT(r5)
1281 bge 33f /* another thread already exiting */
1284 * Although not specifically required by the architecture, POWER7
1285 * preserves the following registers in nap mode, even if an SMT mode
1286 * switch occurs: SLB entries, PURR, SPURR, AMOR, UAMOR, AMR, SPRG0-3,
1287 * DAR, DSISR, DABR, DABRX, DSCR, PMCx, MMCRx, SIAR, SDAR.
1289 /* Save non-volatile GPRs */
1290 std r14, VCPU_GPR(r14)(r3)
1291 std r15, VCPU_GPR(r15)(r3)
1292 std r16, VCPU_GPR(r16)(r3)
1293 std r17, VCPU_GPR(r17)(r3)
1294 std r18, VCPU_GPR(r18)(r3)
1295 std r19, VCPU_GPR(r19)(r3)
1296 std r20, VCPU_GPR(r20)(r3)
1297 std r21, VCPU_GPR(r21)(r3)
1298 std r22, VCPU_GPR(r22)(r3)
1299 std r23, VCPU_GPR(r23)(r3)
1300 std r24, VCPU_GPR(r24)(r3)
1301 std r25, VCPU_GPR(r25)(r3)
1302 std r26, VCPU_GPR(r26)(r3)
1303 std r27, VCPU_GPR(r27)(r3)
1304 std r28, VCPU_GPR(r28)(r3)
1305 std r29, VCPU_GPR(r29)(r3)
1306 std r30, VCPU_GPR(r30)(r3)
1307 std r31, VCPU_GPR(r31)(r3)
1313 * Take a nap until a decrementer or external interrupt occurs,
1314 * with PECE1 (wake on decr) and PECE0 (wake on external) set in LPCR
1317 stb r0,PACAPROCSTART(r13)
1319 ori r5,r5,LPCR_PECE0 | LPCR_PECE1
1323 std r0, HSTATE_SCRATCH0(r13)
1325 ld r0, HSTATE_SCRATCH0(r13)
1332 /* Woken by external or decrementer interrupt */
1333 ld r1, HSTATE_HOST_R1(r13)
1336 /* If we're a secondary thread and we got here by an IPI, ack it */
1337 ld r4,HSTATE_KVM_VCPU(r13)
1338 lwz r3,VCPU_PTID(r4)
1342 rlwinm r3,r3,44-31,0x7 /* extract wake reason field */
1343 cmpwi r3,4 /* was it an external interrupt? */
1345 ld r5, HSTATE_XICS_PHYS(r13)
1349 lwzcix r8,r5,r7 /* ack the interrupt */
1351 stbcix r0,r5,r6 /* clear it */
1352 stwcix r8,r5,r7 /* EOI it */
1354 /* load up FP state */
1358 ld r14, VCPU_GPR(r14)(r4)
1359 ld r15, VCPU_GPR(r15)(r4)
1360 ld r16, VCPU_GPR(r16)(r4)
1361 ld r17, VCPU_GPR(r17)(r4)
1362 ld r18, VCPU_GPR(r18)(r4)
1363 ld r19, VCPU_GPR(r19)(r4)
1364 ld r20, VCPU_GPR(r20)(r4)
1365 ld r21, VCPU_GPR(r21)(r4)
1366 ld r22, VCPU_GPR(r22)(r4)
1367 ld r23, VCPU_GPR(r23)(r4)
1368 ld r24, VCPU_GPR(r24)(r4)
1369 ld r25, VCPU_GPR(r25)(r4)
1370 ld r26, VCPU_GPR(r26)(r4)
1371 ld r27, VCPU_GPR(r27)(r4)
1372 ld r28, VCPU_GPR(r28)(r4)
1373 ld r29, VCPU_GPR(r29)(r4)
1374 ld r30, VCPU_GPR(r30)(r4)
1375 ld r31, VCPU_GPR(r31)(r4)
1377 /* clear our bit in vcore->napping_threads */
1378 33: ld r5,HSTATE_KVM_VCORE(r13)
1379 lwz r3,VCPU_PTID(r4)
1382 addi r6,r5,VCORE_NAPPING_THREADS
1388 stb r0,HSTATE_NAPPING(r13)
1390 /* see if any other thread is already exiting */
1391 lwz r0,VCORE_ENTRY_EXIT(r5)
1393 blt kvmppc_cede_reentry /* if not go back to guest */
1395 /* some threads are exiting, so go to the guest exit path */
1396 b hcall_real_fallback
1398 /* cede when already previously prodded case */
1400 stb r0,VCPU_PRODDED(r3)
1401 sync /* order testing prodded vs. clearing ceded */
1402 stb r0,VCPU_CEDED(r3)
1406 /* we've ceded but we want to give control to the host */
1411 ld r5,HSTATE_KVM_VCORE(r13)
1413 13: lbz r3,VCORE_IN_GUEST(r5)
1417 ld r11,PACA_SLBSHADOWPTR(r13)
1419 .rept SLB_NUM_BOLTED
1420 ld r5,SLBSHADOW_SAVEAREA(r11)
1421 ld r6,SLBSHADOW_SAVEAREA+8(r11)
1422 andis. r7,r5,SLB_ESID_V@h
1429 /* Clear any pending IPI - assume we're a secondary thread */
1430 ld r5, HSTATE_XICS_PHYS(r13)
1432 lwzcix r3, r5, r7 /* ack any pending interrupt */
1433 rlwinm. r0, r3, 0, 0xffffff /* any pending? */
1438 stbcix r0, r5, r6 /* clear the IPI */
1439 stwcix r3, r5, r7 /* EOI it */
1442 /* increment the nap count and then go to nap mode */
1443 ld r4, HSTATE_KVM_VCORE(r13)
1444 addi r4, r4, VCORE_NAP_COUNT
1445 lwsync /* make previous updates visible */
1453 rlwimi r4, r3, 0, LPCR_PECE0 | LPCR_PECE1
1457 std r0, HSTATE_SCRATCH0(r13)
1459 ld r0, HSTATE_SCRATCH0(r13)
1466 * Save away FP, VMX and VSX registers.
1469 _GLOBAL(kvmppc_save_fp)
1472 #ifdef CONFIG_ALTIVEC
1474 oris r8,r8,MSR_VEC@h
1475 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
1479 oris r8,r8,MSR_VSX@h
1480 END_FTR_SECTION_IFSET(CPU_FTR_VSX)
1488 li r6,reg*16+VCPU_VSRS
1496 stfd reg,reg*8+VCPU_FPRS(r3)
1500 ALT_FTR_SECTION_END_IFSET(CPU_FTR_VSX)
1503 stfd fr0,VCPU_FPSCR(r3)
1505 #ifdef CONFIG_ALTIVEC
1509 li r6,reg*16+VCPU_VRS
1516 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
1518 mfspr r6,SPRN_VRSAVE
1519 stw r6,VCPU_VRSAVE(r3)
1525 * Load up FP, VMX and VSX registers
1528 .globl kvmppc_load_fp
1532 #ifdef CONFIG_ALTIVEC
1534 oris r8,r8,MSR_VEC@h
1535 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
1539 oris r8,r8,MSR_VSX@h
1540 END_FTR_SECTION_IFSET(CPU_FTR_VSX)
1544 lfd fr0,VCPU_FPSCR(r4)
1550 li r7,reg*16+VCPU_VSRS
1558 lfd reg,reg*8+VCPU_FPRS(r4)
1562 ALT_FTR_SECTION_END_IFSET(CPU_FTR_VSX)
1565 #ifdef CONFIG_ALTIVEC
1572 li r7,reg*16+VCPU_VRS
1576 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
1578 lwz r7,VCPU_VRSAVE(r4)
1579 mtspr SPRN_VRSAVE,r7