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>
33 #define VCPU_GPRS_TM(reg) (((reg) * ULONG_SIZE) + VCPU_GPR_TM)
35 #ifdef __LITTLE_ENDIAN__
36 #error Need to fix lppaca and SLB shadow accesses in little endian mode
39 /* Values in HSTATE_NAPPING(r13) */
40 #define NAPPING_CEDE 1
41 #define NAPPING_NOVCPU 2
44 * Call kvmppc_hv_entry in real mode.
45 * Must be called with interrupts hard-disabled.
49 * LR = return address to continue at after eventually re-enabling MMU
51 _GLOBAL(kvmppc_hv_entry_trampoline)
53 std r0, PPC_LR_STKOFF(r1)
56 LOAD_REG_ADDR(r5, kvmppc_call_hv_entry)
61 mtmsrd r0,1 /* clear RI in MSR */
67 ld r4, HSTATE_KVM_VCPU(r13)
70 /* Back from guest - restore host state and return to caller */
73 /* Restore host DABR and DABRX */
74 ld r5,HSTATE_DABR(r13)
78 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
81 ld r3,PACA_SPRG_VDSO(r13)
82 mtspr SPRN_SPRG_VDSO_WRITE,r3
84 /* Reload the host's PMU registers */
85 ld r3, PACALPPACAPTR(r13) /* is the host using the PMU? */
86 lbz r4, LPPACA_PMCINUSE(r3)
88 beq 23f /* skip if not */
89 lwz r3, HSTATE_PMC(r13)
90 lwz r4, HSTATE_PMC + 4(r13)
91 lwz r5, HSTATE_PMC + 8(r13)
92 lwz r6, HSTATE_PMC + 12(r13)
93 lwz r8, HSTATE_PMC + 16(r13)
94 lwz r9, HSTATE_PMC + 20(r13)
96 lwz r10, HSTATE_PMC + 24(r13)
97 lwz r11, HSTATE_PMC + 28(r13)
98 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
108 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
109 ld r3, HSTATE_MMCR(r13)
110 ld r4, HSTATE_MMCR + 8(r13)
111 ld r5, HSTATE_MMCR + 16(r13)
112 ld r6, HSTATE_MMCR + 24(r13)
113 ld r7, HSTATE_MMCR + 32(r13)
119 ld r8, HSTATE_MMCR + 40(r13)
120 ld r9, HSTATE_MMCR + 48(r13)
123 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
129 * Reload DEC. HDEC interrupts were disabled when
130 * we reloaded the host's LPCR value.
132 ld r3, HSTATE_DECEXP(r13)
138 * For external and machine check interrupts, we need
139 * to call the Linux handler to process the interrupt.
140 * We do that by jumping to absolute address 0x500 for
141 * external interrupts, or the machine_check_fwnmi label
142 * for machine checks (since firmware might have patched
143 * the vector area at 0x200). The [h]rfid at the end of the
144 * handler will return to the book3s_hv_interrupts.S code.
145 * For other interrupts we do the rfid to get back
146 * to the book3s_hv_interrupts.S code here.
148 ld r8, 112+PPC_LR_STKOFF(r1)
150 ld r7, HSTATE_HOST_MSR(r13)
152 cmpwi cr1, r12, BOOK3S_INTERRUPT_MACHINE_CHECK
153 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
156 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
158 /* RFI into the highmem handler, or branch to interrupt handler */
162 mtmsrd r6, 1 /* Clear RI in MSR */
165 beqa 0x500 /* external interrupt (PPC970) */
166 beq cr1, 13f /* machine check */
169 /* On POWER7, we have external interrupts set to use HSRR0/1 */
170 11: mtspr SPRN_HSRR0, r8
174 13: b machine_check_fwnmi
176 kvmppc_primary_no_guest:
177 /* We handle this much like a ceded vcpu */
178 /* set our bit in napping_threads */
179 ld r5, HSTATE_KVM_VCORE(r13)
180 lbz r7, HSTATE_PTID(r13)
183 addi r6, r5, VCORE_NAPPING_THREADS
188 /* order napping_threads update vs testing entry_exit_count */
191 lwz r7, VCORE_ENTRY_EXIT(r5)
193 bge kvm_novcpu_exit /* another thread already exiting */
194 li r3, NAPPING_NOVCPU
195 stb r3, HSTATE_NAPPING(r13)
197 stb r3, HSTATE_HWTHREAD_REQ(r13)
202 ld r1, HSTATE_HOST_R1(r13)
203 ld r5, HSTATE_KVM_VCORE(r13)
205 stb r0, HSTATE_NAPPING(r13)
206 stb r0, HSTATE_HWTHREAD_REQ(r13)
208 /* check the wake reason */
209 bl kvmppc_check_wake_reason
211 /* see if any other thread is already exiting */
212 lwz r0, VCORE_ENTRY_EXIT(r5)
216 /* clear our bit in napping_threads */
217 lbz r7, HSTATE_PTID(r13)
220 addi r6, r5, VCORE_NAPPING_THREADS
226 /* See if the wake reason means we need to exit */
230 /* Got an IPI but other vcpus aren't yet exiting, must be a latecomer */
231 ld r4, HSTATE_KVM_VCPU(r13)
239 * We come in here when wakened from nap mode.
240 * Relocation is off and most register values are lost.
241 * r13 points to the PACA.
243 .globl kvm_start_guest
246 /* Set runlatch bit the minute you wake up from nap */
253 li r0,KVM_HWTHREAD_IN_KVM
254 stb r0,HSTATE_HWTHREAD_STATE(r13)
256 /* NV GPR values from power7_idle() will no longer be valid */
258 stb r0,PACA_NAPSTATELOST(r13)
260 /* were we napping due to cede? */
261 lbz r0,HSTATE_NAPPING(r13)
262 cmpwi r0,NAPPING_CEDE
264 cmpwi r0,NAPPING_NOVCPU
265 beq kvm_novcpu_wakeup
267 ld r1,PACAEMERGSP(r13)
268 subi r1,r1,STACK_FRAME_OVERHEAD
271 * We weren't napping due to cede, so this must be a secondary
272 * thread being woken up to run a guest, or being woken up due
273 * to a stray IPI. (Or due to some machine check or hypervisor
274 * maintenance interrupt while the core is in KVM.)
277 /* Check the wake reason in SRR1 to see why we got here */
278 bl kvmppc_check_wake_reason
282 /* get vcpu pointer, NULL if we have no vcpu to run */
283 ld r4,HSTATE_KVM_VCPU(r13)
285 /* if we have no vcpu to run, go back to sleep */
288 /* Set HSTATE_DSCR(r13) to something sensible */
289 ld r6, PACA_DSCR(r13)
290 std r6, HSTATE_DSCR(r13)
294 /* Back from the guest, go back to nap */
295 /* Clear our vcpu pointer so we don't come back in early */
297 std r0, HSTATE_KVM_VCPU(r13)
299 * Make sure we clear HSTATE_KVM_VCPU(r13) before incrementing
300 * the nap_count, because once the increment to nap_count is
301 * visible we could be given another vcpu.
305 /* increment the nap count and then go to nap mode */
306 ld r4, HSTATE_KVM_VCORE(r13)
307 addi r4, r4, VCORE_NAP_COUNT
314 li r0, KVM_HWTHREAD_IN_NAP
315 stb r0, HSTATE_HWTHREAD_STATE(r13)
317 /* Clear the runlatch bit before napping */
324 rlwimi r4, r3, 0, LPCR_PECE0 | LPCR_PECE1
327 std r0, HSTATE_SCRATCH0(r13)
329 ld r0, HSTATE_SCRATCH0(r13)
335 /******************************************************************************
339 *****************************************************************************/
341 .global kvmppc_hv_entry
346 * R4 = vcpu pointer (or NULL)
350 * all other volatile GPRS = free
353 std r0, PPC_LR_STKOFF(r1)
356 /* Save R1 in the PACA */
357 std r1, HSTATE_HOST_R1(r13)
359 li r6, KVM_GUEST_MODE_HOST_HV
360 stb r6, HSTATE_IN_GUEST(r13)
370 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
372 * POWER7 host -> guest partition switch code.
373 * We don't have to lock against concurrent tlbies,
374 * but we do have to coordinate across hardware threads.
376 /* Increment entry count iff exit count is zero. */
377 ld r5,HSTATE_KVM_VCORE(r13)
378 addi r9,r5,VCORE_ENTRY_EXIT
380 cmpwi r3,0x100 /* any threads starting to exit? */
381 bge secondary_too_late /* if so we're too late to the party */
386 /* Primary thread switches to guest partition. */
387 ld r9,VCORE_KVM(r5) /* pointer to struct kvm */
388 lbz r6,HSTATE_PTID(r13)
393 li r0,LPID_RSVD /* switch to reserved LPID */
396 mtspr SPRN_SDR1,r6 /* switch to partition page table */
400 /* See if we need to flush the TLB */
401 lhz r6,PACAPACAINDEX(r13) /* test_bit(cpu, need_tlb_flush) */
402 clrldi r7,r6,64-6 /* extract bit number (6 bits) */
403 srdi r6,r6,6 /* doubleword number */
404 sldi r6,r6,3 /* address offset */
406 addi r6,r6,KVM_NEED_FLUSH /* dword in kvm->arch.need_tlb_flush */
412 23: ldarx r7,0,r6 /* if set, clear the bit */
416 /* Flush the TLB of any entries for this LPID */
417 /* use arch 2.07S as a proxy for POWER8 */
419 li r6,512 /* POWER8 has 512 sets */
421 li r6,128 /* POWER7 has 128 sets */
422 ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_207S)
424 li r7,0x800 /* IS field = 0b10 */
431 /* Add timebase offset onto timebase */
432 22: ld r8,VCORE_TB_OFFSET(r5)
435 mftb r6 /* current host timebase */
437 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
438 mftb r7 /* check if lower 24 bits overflowed */
443 addis r8,r8,0x100 /* if so, increment upper 40 bits */
446 /* Load guest PCR value to select appropriate compat mode */
447 37: ld r7, VCORE_PCR(r5)
454 /* DPDES is shared between threads */
455 ld r8, VCORE_DPDES(r5)
457 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
460 stb r0,VCORE_IN_GUEST(r5) /* signal secondaries to continue */
463 /* Secondary threads wait for primary to have done partition switch */
464 20: lbz r0,VCORE_IN_GUEST(r5)
468 /* Set LPCR and RMOR. */
469 10: ld r8,VCORE_LPCR(r5)
475 /* Check if HDEC expires soon */
477 cmpwi r3,512 /* 1 microsecond */
478 li r12,BOOK3S_INTERRUPT_HV_DECREMENTER
483 * PPC970 host -> guest partition switch code.
484 * We have to lock against concurrent tlbies,
485 * using native_tlbie_lock to lock against host tlbies
486 * and kvm->arch.tlbie_lock to lock against guest tlbies.
487 * We also have to invalidate the TLB since its
488 * entries aren't tagged with the LPID.
490 30: ld r5,HSTATE_KVM_VCORE(r13)
491 ld r9,VCORE_KVM(r5) /* pointer to struct kvm */
493 /* first take native_tlbie_lock */
496 .tc native_tlbie_lock[TC],native_tlbie_lock
498 ld r3,toc_tlbie_lock@toc(2)
499 #ifdef __BIG_ENDIAN__
500 lwz r8,PACA_LOCK_TOKEN(r13)
502 lwz r8,PACAPACAINDEX(r13)
511 ld r5,HSTATE_KVM_VCORE(r13)
512 ld r7,VCORE_LPCR(r5) /* use vcore->lpcr to store HID4 */
514 rotldi r0,r0,HID4_LPID5_SH /* all lpid bits in HID4 = 1 */
518 mtspr SPRN_HID4,r0 /* switch to reserved LPID */
521 stw r0,0(r3) /* drop native_tlbie_lock */
523 /* invalidate the whole TLB */
532 /* Take the guest's tlbie_lock */
533 addi r3,r9,KVM_TLBIE_LOCK
541 mtspr SPRN_SDR1,r6 /* switch to partition page table */
543 /* Set up HID4 with the guest's LPID etc. */
548 /* drop the guest's tlbie_lock */
552 /* Check if HDEC expires soon */
555 li r12,BOOK3S_INTERRUPT_HV_DECREMENTER
558 /* Enable HDEC interrupts */
561 rldimi r0,r3, HID0_HDICE_SH, 64-HID0_HDICE_SH-1
571 /* Do we have a guest vcpu to run? */
573 beq kvmppc_primary_no_guest
576 /* Load up guest SLB entries */
577 lwz r5,VCPU_SLB_MAX(r4)
582 1: ld r8,VCPU_SLB_E(r6)
585 addi r6,r6,VCPU_SLB_SIZE
588 /* Increment yield count if they have a VPA */
592 lwz r5, LPPACA_YIELDCOUNT(r3)
594 stw r5, LPPACA_YIELDCOUNT(r3)
596 stb r6, VCPU_VPA_DIRTY(r4)
600 /* Save purr/spurr */
603 std r5,HSTATE_PURR(r13)
604 std r6,HSTATE_SPURR(r13)
609 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
612 /* Set partition DABR */
613 /* Do this before re-enabling PMU to avoid P7 DABR corruption bug */
614 lwz r5,VCPU_DABRX(r4)
618 BEGIN_FTR_SECTION_NESTED(89)
620 END_FTR_SECTION_NESTED(CPU_FTR_ARCH_206, CPU_FTR_ARCH_206, 89)
621 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
623 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
626 END_FTR_SECTION_IFCLR(CPU_FTR_TM)
628 /* Turn on TM/FP/VSX/VMX so we can restore them. */
634 oris r5, r5, (MSR_VEC | MSR_VSX)@h
638 * The user may change these outside of a transaction, so they must
639 * always be context switched.
641 ld r5, VCPU_TFHAR(r4)
642 ld r6, VCPU_TFIAR(r4)
643 ld r7, VCPU_TEXASR(r4)
646 mtspr SPRN_TEXASR, r7
649 rldicl. r5, r5, 64 - MSR_TS_S_LG, 62
650 beq skip_tm /* TM not active in guest */
652 /* Make sure the failure summary is set, otherwise we'll program check
653 * when we trechkpt. It's possible that this might have been not set
654 * on a kvmppc_set_one_reg() call but we shouldn't let this crash the
657 oris r7, r7, (TEXASR_FS)@h
658 mtspr SPRN_TEXASR, r7
661 * We need to load up the checkpointed state for the guest.
662 * We need to do this early as it will blow away any GPRs, VSRs and
667 addi r3, r31, VCPU_FPRS_TM
669 addi r3, r31, VCPU_VRS_TM
672 lwz r7, VCPU_VRSAVE_TM(r4)
673 mtspr SPRN_VRSAVE, r7
675 ld r5, VCPU_LR_TM(r4)
676 lwz r6, VCPU_CR_TM(r4)
677 ld r7, VCPU_CTR_TM(r4)
678 ld r8, VCPU_AMR_TM(r4)
679 ld r9, VCPU_TAR_TM(r4)
687 * Load up PPR and DSCR values but don't put them in the actual SPRs
688 * till the last moment to avoid running with userspace PPR and DSCR for
691 ld r29, VCPU_DSCR_TM(r4)
692 ld r30, VCPU_PPR_TM(r4)
694 std r2, PACATMSCRATCH(r13) /* Save TOC */
696 /* Clear the MSR RI since r1, r13 are all going to be foobar. */
700 /* Load GPRs r0-r28 */
703 ld reg, VCPU_GPRS_TM(reg)(r31)
710 /* Load final GPRs */
711 ld 29, VCPU_GPRS_TM(29)(r31)
712 ld 30, VCPU_GPRS_TM(30)(r31)
713 ld 31, VCPU_GPRS_TM(31)(r31)
715 /* TM checkpointed state is now setup. All GPRs are now volatile. */
718 /* Now let's get back the state we need. */
721 ld r29, HSTATE_DSCR(r13)
723 ld r4, HSTATE_KVM_VCPU(r13)
724 ld r1, HSTATE_HOST_R1(r13)
725 ld r2, PACATMSCRATCH(r13)
727 /* Set the MSR RI since we have our registers back. */
733 /* Load guest PMU registers */
734 /* R4 is live here (vcpu pointer) */
736 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
737 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
739 lwz r3, VCPU_PMC(r4) /* always load up guest PMU registers */
740 lwz r5, VCPU_PMC + 4(r4) /* to prevent information leak */
741 lwz r6, VCPU_PMC + 8(r4)
742 lwz r7, VCPU_PMC + 12(r4)
743 lwz r8, VCPU_PMC + 16(r4)
744 lwz r9, VCPU_PMC + 20(r4)
746 lwz r10, VCPU_PMC + 24(r4)
747 lwz r11, VCPU_PMC + 28(r4)
748 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
758 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
760 ld r5, VCPU_MMCR + 8(r4)
761 ld r6, VCPU_MMCR + 16(r4)
769 ld r5, VCPU_MMCR + 24(r4)
771 lwz r7, VCPU_PMC + 24(r4)
772 lwz r8, VCPU_PMC + 28(r4)
773 ld r9, VCPU_MMCR + 32(r4)
779 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
783 /* Load up FP, VMX and VSX registers */
786 ld r14, VCPU_GPR(R14)(r4)
787 ld r15, VCPU_GPR(R15)(r4)
788 ld r16, VCPU_GPR(R16)(r4)
789 ld r17, VCPU_GPR(R17)(r4)
790 ld r18, VCPU_GPR(R18)(r4)
791 ld r19, VCPU_GPR(R19)(r4)
792 ld r20, VCPU_GPR(R20)(r4)
793 ld r21, VCPU_GPR(R21)(r4)
794 ld r22, VCPU_GPR(R22)(r4)
795 ld r23, VCPU_GPR(R23)(r4)
796 ld r24, VCPU_GPR(R24)(r4)
797 ld r25, VCPU_GPR(R25)(r4)
798 ld r26, VCPU_GPR(R26)(r4)
799 ld r27, VCPU_GPR(R27)(r4)
800 ld r28, VCPU_GPR(R28)(r4)
801 ld r29, VCPU_GPR(R29)(r4)
802 ld r30, VCPU_GPR(R30)(r4)
803 ld r31, VCPU_GPR(R31)(r4)
806 /* Switch DSCR to guest value */
809 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
812 /* Skip next section on POWER7 or PPC970 */
814 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
815 /* Turn on TM so we can access TFHAR/TFIAR/TEXASR */
818 rldimi r8, r0, MSR_TM_LG, 63-MSR_TM_LG
821 /* Load up POWER8-specific registers */
823 lwz r6, VCPU_PSPB(r4)
829 ld r6, VCPU_DAWRX(r4)
830 ld r7, VCPU_CIABR(r4)
840 ld r8, VCPU_EBBHR(r4)
842 ld r5, VCPU_EBBRR(r4)
843 ld r6, VCPU_BESCR(r4)
844 ld r7, VCPU_CSIGR(r4)
850 ld r5, VCPU_TCSCR(r4)
852 lwz r7, VCPU_GUEST_PID(r4)
861 * Set the decrementer to the guest decrementer.
863 ld r8,VCPU_DEC_EXPIRES(r4)
864 /* r8 is a host timebase value here, convert to guest TB */
865 ld r5,HSTATE_KVM_VCORE(r13)
866 ld r6,VCORE_TB_OFFSET(r5)
873 ld r5, VCPU_SPRG0(r4)
874 ld r6, VCPU_SPRG1(r4)
875 ld r7, VCPU_SPRG2(r4)
876 ld r8, VCPU_SPRG3(r4)
882 /* Load up DAR and DSISR */
884 lwz r6, VCPU_DSISR(r4)
889 /* Restore AMR and UAMOR, set AMOR to all 1s */
896 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
898 /* Restore state of CTRL run bit; assume 1 on entry */
912 kvmppc_cede_reentry: /* r4 = vcpu, r13 = paca */
920 deliver_guest_interrupt:
921 /* r11 = vcpu->arch.msr & ~MSR_HV */
922 rldicl r11, r11, 63 - MSR_HV_LG, 1
923 rotldi r11, r11, 1 + MSR_HV_LG
926 /* Check if we can deliver an external or decrementer interrupt now */
927 ld r0, VCPU_PENDING_EXC(r4)
928 rldicl r0, r0, 64 - BOOK3S_IRQPRIO_EXTERNAL_LEVEL, 63
930 andi. r8, r11, MSR_EE
933 /* Insert EXTERNAL_LEVEL bit into LPCR at the MER bit position */
934 rldimi r8, r0, LPCR_MER_SH, 63 - LPCR_MER_SH
937 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
939 li r0, BOOK3S_INTERRUPT_EXTERNAL
943 li r0, BOOK3S_INTERRUPT_DECREMENTER
946 12: mtspr SPRN_SRR0, r10
950 bl kvmppc_msr_interrupt
956 * R10: value for HSRR0
957 * R11: value for HSRR1
962 stb r0,VCPU_CEDED(r4) /* cancel cede */
966 /* Activate guest mode, so faults get handled by KVM */
967 li r9, KVM_GUEST_MODE_GUEST_HV
968 stb r9, HSTATE_IN_GUEST(r13)
975 END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
978 END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
985 ld r1, VCPU_GPR(R1)(r4)
986 ld r2, VCPU_GPR(R2)(r4)
987 ld r3, VCPU_GPR(R3)(r4)
988 ld r5, VCPU_GPR(R5)(r4)
989 ld r6, VCPU_GPR(R6)(r4)
990 ld r7, VCPU_GPR(R7)(r4)
991 ld r8, VCPU_GPR(R8)(r4)
992 ld r9, VCPU_GPR(R9)(r4)
993 ld r10, VCPU_GPR(R10)(r4)
994 ld r11, VCPU_GPR(R11)(r4)
995 ld r12, VCPU_GPR(R12)(r4)
996 ld r13, VCPU_GPR(R13)(r4)
1000 END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
1001 ld r0, VCPU_GPR(R0)(r4)
1002 ld r4, VCPU_GPR(R4)(r4)
1007 /******************************************************************************
1011 *****************************************************************************/
1014 * We come here from the first-level interrupt handlers.
1016 .globl kvmppc_interrupt_hv
1017 kvmppc_interrupt_hv:
1019 * Register contents:
1020 * R12 = interrupt vector
1022 * guest CR, R12 saved in shadow VCPU SCRATCH1/0
1023 * guest R13 saved in SPRN_SCRATCH0
1025 std r9, HSTATE_SCRATCH2(r13)
1027 lbz r9, HSTATE_IN_GUEST(r13)
1028 cmpwi r9, KVM_GUEST_MODE_HOST_HV
1029 beq kvmppc_bad_host_intr
1030 #ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1031 cmpwi r9, KVM_GUEST_MODE_GUEST
1032 ld r9, HSTATE_SCRATCH2(r13)
1033 beq kvmppc_interrupt_pr
1035 /* We're now back in the host but in guest MMU context */
1036 li r9, KVM_GUEST_MODE_HOST_HV
1037 stb r9, HSTATE_IN_GUEST(r13)
1039 ld r9, HSTATE_KVM_VCPU(r13)
1041 /* Save registers */
1043 std r0, VCPU_GPR(R0)(r9)
1044 std r1, VCPU_GPR(R1)(r9)
1045 std r2, VCPU_GPR(R2)(r9)
1046 std r3, VCPU_GPR(R3)(r9)
1047 std r4, VCPU_GPR(R4)(r9)
1048 std r5, VCPU_GPR(R5)(r9)
1049 std r6, VCPU_GPR(R6)(r9)
1050 std r7, VCPU_GPR(R7)(r9)
1051 std r8, VCPU_GPR(R8)(r9)
1052 ld r0, HSTATE_SCRATCH2(r13)
1053 std r0, VCPU_GPR(R9)(r9)
1054 std r10, VCPU_GPR(R10)(r9)
1055 std r11, VCPU_GPR(R11)(r9)
1056 ld r3, HSTATE_SCRATCH0(r13)
1057 lwz r4, HSTATE_SCRATCH1(r13)
1058 std r3, VCPU_GPR(R12)(r9)
1061 ld r3, HSTATE_CFAR(r13)
1062 std r3, VCPU_CFAR(r9)
1063 END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
1065 ld r4, HSTATE_PPR(r13)
1066 std r4, VCPU_PPR(r9)
1067 END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
1069 /* Restore R1/R2 so we can handle faults */
1070 ld r1, HSTATE_HOST_R1(r13)
1073 mfspr r10, SPRN_SRR0
1074 mfspr r11, SPRN_SRR1
1075 std r10, VCPU_SRR0(r9)
1076 std r11, VCPU_SRR1(r9)
1077 andi. r0, r12, 2 /* need to read HSRR0/1? */
1079 mfspr r10, SPRN_HSRR0
1080 mfspr r11, SPRN_HSRR1
1082 1: std r10, VCPU_PC(r9)
1083 std r11, VCPU_MSR(r9)
1087 std r3, VCPU_GPR(R13)(r9)
1090 stw r12,VCPU_TRAP(r9)
1092 /* Save HEIR (HV emulation assist reg) in last_inst
1093 if this is an HEI (HV emulation interrupt, e40) */
1094 li r3,KVM_INST_FETCH_FAILED
1096 cmpwi r12,BOOK3S_INTERRUPT_H_EMUL_ASSIST
1099 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
1100 11: stw r3,VCPU_LAST_INST(r9)
1102 /* these are volatile across C function calls */
1105 std r3, VCPU_CTR(r9)
1106 stw r4, VCPU_XER(r9)
1109 /* If this is a page table miss then see if it's theirs or ours */
1110 cmpwi r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1112 cmpwi r12, BOOK3S_INTERRUPT_H_INST_STORAGE
1114 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
1116 /* See if this is a leftover HDEC interrupt */
1117 cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER
1123 /* See if this is an hcall we can handle in real mode */
1124 cmpwi r12,BOOK3S_INTERRUPT_SYSCALL
1125 beq hcall_try_real_mode
1127 /* Only handle external interrupts here on arch 206 and later */
1129 b ext_interrupt_to_host
1130 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_206)
1132 /* External interrupt ? */
1133 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
1134 bne+ ext_interrupt_to_host
1136 /* External interrupt, first check for host_ipi. If this is
1137 * set, we know the host wants us out so let's do it now
1141 bgt ext_interrupt_to_host
1143 /* Check if any CPU is heading out to the host, if so head out too */
1144 ld r5, HSTATE_KVM_VCORE(r13)
1145 lwz r0, VCORE_ENTRY_EXIT(r5)
1147 bge ext_interrupt_to_host
1149 /* Return to guest after delivering any pending interrupt */
1151 b deliver_guest_interrupt
1153 ext_interrupt_to_host:
1155 guest_exit_cont: /* r9 = vcpu, r12 = trap, r13 = paca */
1156 /* Save more register state */
1159 std r6, VCPU_DAR(r9)
1160 stw r7, VCPU_DSISR(r9)
1162 /* don't overwrite fault_dar/fault_dsisr if HDSI */
1163 cmpwi r12,BOOK3S_INTERRUPT_H_DATA_STORAGE
1165 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
1166 std r6, VCPU_FAULT_DAR(r9)
1167 stw r7, VCPU_FAULT_DSISR(r9)
1169 /* See if it is a machine check */
1170 cmpwi r12, BOOK3S_INTERRUPT_MACHINE_CHECK
1171 beq machine_check_realmode
1174 /* Save guest CTRL register, set runlatch to 1 */
1175 6: mfspr r6,SPRN_CTRLF
1176 stw r6,VCPU_CTRL(r9)
1182 /* Read the guest SLB and save it away */
1183 lwz r0,VCPU_SLB_NR(r9) /* number of entries in SLB */
1189 andis. r0,r8,SLB_ESID_V@h
1191 add r8,r8,r6 /* put index in */
1193 std r8,VCPU_SLB_E(r7)
1194 std r3,VCPU_SLB_V(r7)
1195 addi r7,r7,VCPU_SLB_SIZE
1199 stw r5,VCPU_SLB_MAX(r9)
1202 * Save the guest PURR/SPURR
1208 ld r8,VCPU_SPURR(r9)
1209 std r5,VCPU_PURR(r9)
1210 std r6,VCPU_SPURR(r9)
1215 * Restore host PURR/SPURR and add guest times
1216 * so that the time in the guest gets accounted.
1218 ld r3,HSTATE_PURR(r13)
1219 ld r4,HSTATE_SPURR(r13)
1224 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_201)
1231 /* r5 is a guest timebase value here, convert to host TB */
1232 ld r3,HSTATE_KVM_VCORE(r13)
1233 ld r4,VCORE_TB_OFFSET(r3)
1235 std r5,VCPU_DEC_EXPIRES(r9)
1239 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
1240 /* Save POWER8-specific registers */
1244 std r5, VCPU_IAMR(r9)
1245 stw r6, VCPU_PSPB(r9)
1246 std r7, VCPU_FSCR(r9)
1251 std r6, VCPU_VTB(r9)
1252 std r7, VCPU_TAR(r9)
1253 mfspr r8, SPRN_EBBHR
1254 std r8, VCPU_EBBHR(r9)
1255 mfspr r5, SPRN_EBBRR
1256 mfspr r6, SPRN_BESCR
1257 mfspr r7, SPRN_CSIGR
1259 std r5, VCPU_EBBRR(r9)
1260 std r6, VCPU_BESCR(r9)
1261 std r7, VCPU_CSIGR(r9)
1262 std r8, VCPU_TACR(r9)
1263 mfspr r5, SPRN_TCSCR
1267 std r5, VCPU_TCSCR(r9)
1268 std r6, VCPU_ACOP(r9)
1269 stw r7, VCPU_GUEST_PID(r9)
1270 std r8, VCPU_WORT(r9)
1273 /* Save and reset AMR and UAMOR before turning on the MMU */
1278 std r6,VCPU_UAMOR(r9)
1281 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
1283 /* Switch DSCR back to host value */
1286 ld r7, HSTATE_DSCR(r13)
1287 std r8, VCPU_DSCR(r9)
1289 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
1291 /* Save non-volatile GPRs */
1292 std r14, VCPU_GPR(R14)(r9)
1293 std r15, VCPU_GPR(R15)(r9)
1294 std r16, VCPU_GPR(R16)(r9)
1295 std r17, VCPU_GPR(R17)(r9)
1296 std r18, VCPU_GPR(R18)(r9)
1297 std r19, VCPU_GPR(R19)(r9)
1298 std r20, VCPU_GPR(R20)(r9)
1299 std r21, VCPU_GPR(R21)(r9)
1300 std r22, VCPU_GPR(R22)(r9)
1301 std r23, VCPU_GPR(R23)(r9)
1302 std r24, VCPU_GPR(R24)(r9)
1303 std r25, VCPU_GPR(R25)(r9)
1304 std r26, VCPU_GPR(R26)(r9)
1305 std r27, VCPU_GPR(R27)(r9)
1306 std r28, VCPU_GPR(R28)(r9)
1307 std r29, VCPU_GPR(R29)(r9)
1308 std r30, VCPU_GPR(R30)(r9)
1309 std r31, VCPU_GPR(R31)(r9)
1312 mfspr r3, SPRN_SPRG0
1313 mfspr r4, SPRN_SPRG1
1314 mfspr r5, SPRN_SPRG2
1315 mfspr r6, SPRN_SPRG3
1316 std r3, VCPU_SPRG0(r9)
1317 std r4, VCPU_SPRG1(r9)
1318 std r5, VCPU_SPRG2(r9)
1319 std r6, VCPU_SPRG3(r9)
1325 /* Increment yield count if they have a VPA */
1326 ld r8, VCPU_VPA(r9) /* do they have a VPA? */
1329 lwz r3, LPPACA_YIELDCOUNT(r8)
1331 stw r3, LPPACA_YIELDCOUNT(r8)
1333 stb r3, VCPU_VPA_DIRTY(r9)
1335 /* Save PMU registers if requested */
1336 /* r8 and cr0.eq are live here */
1338 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
1339 mfspr r4, SPRN_MMCR0 /* save MMCR0 */
1340 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
1341 mfspr r6, SPRN_MMCRA
1343 /* On P7, clear MMCRA in order to disable SDAR updates */
1345 mtspr SPRN_MMCRA, r7
1346 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
1348 beq 21f /* if no VPA, save PMU stuff anyway */
1349 lbz r7, LPPACA_PMCINUSE(r8)
1350 cmpwi r7, 0 /* did they ask for PMU stuff to be saved? */
1352 std r3, VCPU_MMCR(r9) /* if not, set saved MMCR0 to FC */
1354 21: mfspr r5, SPRN_MMCR1
1357 std r4, VCPU_MMCR(r9)
1358 std r5, VCPU_MMCR + 8(r9)
1359 std r6, VCPU_MMCR + 16(r9)
1360 std r7, VCPU_SIAR(r9)
1361 std r8, VCPU_SDAR(r9)
1369 mfspr r10, SPRN_PMC7
1370 mfspr r11, SPRN_PMC8
1371 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
1372 stw r3, VCPU_PMC(r9)
1373 stw r4, VCPU_PMC + 4(r9)
1374 stw r5, VCPU_PMC + 8(r9)
1375 stw r6, VCPU_PMC + 12(r9)
1376 stw r7, VCPU_PMC + 16(r9)
1377 stw r8, VCPU_PMC + 20(r9)
1379 stw r10, VCPU_PMC + 24(r9)
1380 stw r11, VCPU_PMC + 28(r9)
1381 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
1383 mfspr r4, SPRN_MMCR2
1385 mfspr r6, SPRN_SPMC1
1386 mfspr r7, SPRN_SPMC2
1387 mfspr r8, SPRN_MMCRS
1388 std r4, VCPU_MMCR + 24(r9)
1389 std r5, VCPU_SIER(r9)
1390 stw r6, VCPU_PMC + 24(r9)
1391 stw r7, VCPU_PMC + 28(r9)
1392 std r8, VCPU_MMCR + 32(r9)
1394 mtspr SPRN_MMCRS, r4
1395 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1403 hdec_soon: /* r12 = trap, r13 = paca */
1406 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
1408 * POWER7 guest -> host partition switch code.
1409 * We don't have to lock against tlbies but we do
1410 * have to coordinate the hardware threads.
1412 /* Increment the threads-exiting-guest count in the 0xff00
1413 bits of vcore->entry_exit_count */
1414 ld r5,HSTATE_KVM_VCORE(r13)
1415 addi r6,r5,VCORE_ENTRY_EXIT
1420 isync /* order stwcx. vs. reading napping_threads */
1423 * At this point we have an interrupt that we have to pass
1424 * up to the kernel or qemu; we can't handle it in real mode.
1425 * Thus we have to do a partition switch, so we have to
1426 * collect the other threads, if we are the first thread
1427 * to take an interrupt. To do this, we set the HDEC to 0,
1428 * which causes an HDEC interrupt in all threads within 2ns
1429 * because the HDEC register is shared between all 4 threads.
1430 * However, we don't need to bother if this is an HDEC
1431 * interrupt, since the other threads will already be on their
1432 * way here in that case.
1434 cmpwi r3,0x100 /* Are we the first here? */
1436 cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER
1442 * Send an IPI to any napping threads, since an HDEC interrupt
1443 * doesn't wake CPUs up from nap.
1445 lwz r3,VCORE_NAPPING_THREADS(r5)
1446 lbz r4,HSTATE_PTID(r13)
1449 andc. r3,r3,r0 /* no sense IPI'ing ourselves */
1451 /* Order entry/exit update vs. IPIs */
1453 mulli r4,r4,PACA_SIZE /* get paca for thread 0 */
1457 ld r8,HSTATE_XICS_PHYS(r6) /* get thread's XICS reg addr */
1460 stbcix r0,r7,r8 /* trigger the IPI */
1462 addi r6,r6,PACA_SIZE
1466 /* Secondary threads wait for primary to do partition switch */
1467 43: ld r5,HSTATE_KVM_VCORE(r13)
1468 ld r4,VCORE_KVM(r5) /* pointer to struct kvm */
1469 lbz r3,HSTATE_PTID(r13)
1473 13: lbz r3,VCORE_IN_GUEST(r5)
1479 /* Primary thread waits for all the secondaries to exit guest */
1480 15: lwz r3,VCORE_ENTRY_EXIT(r5)
1487 /* Primary thread switches back to host partition */
1488 ld r6,KVM_HOST_SDR1(r4)
1489 lwz r7,KVM_HOST_LPID(r4)
1490 li r8,LPID_RSVD /* switch to reserved LPID */
1493 mtspr SPRN_SDR1,r6 /* switch to partition page table */
1498 /* DPDES is shared between threads */
1499 mfspr r7, SPRN_DPDES
1500 std r7, VCORE_DPDES(r5)
1501 /* clear DPDES so we don't get guest doorbells in the host */
1503 mtspr SPRN_DPDES, r8
1504 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1506 /* Subtract timebase offset from timebase */
1507 ld r8,VCORE_TB_OFFSET(r5)
1510 mftb r6 /* current guest timebase */
1512 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
1513 mftb r7 /* check if lower 24 bits overflowed */
1518 addis r8,r8,0x100 /* if so, increment upper 40 bits */
1522 17: ld r0, VCORE_PCR(r5)
1528 /* Signal secondary CPUs to continue */
1529 stb r0,VCORE_IN_GUEST(r5)
1530 lis r8,0x7fff /* MAX_INT@h */
1533 16: ld r8,KVM_HOST_LPCR(r4)
1539 * PPC970 guest -> host partition switch code.
1540 * We have to lock against concurrent tlbies, and
1541 * we have to flush the whole TLB.
1543 32: ld r5,HSTATE_KVM_VCORE(r13)
1544 ld r4,VCORE_KVM(r5) /* pointer to struct kvm */
1546 /* Take the guest's tlbie_lock */
1547 #ifdef __BIG_ENDIAN__
1548 lwz r8,PACA_LOCK_TOKEN(r13)
1550 lwz r8,PACAPACAINDEX(r13)
1552 addi r3,r4,KVM_TLBIE_LOCK
1560 ld r7,KVM_HOST_LPCR(r4) /* use kvm->arch.host_lpcr for HID4 */
1562 rotldi r0,r0,HID4_LPID5_SH /* all lpid bits in HID4 = 1 */
1566 mtspr SPRN_HID4,r0 /* switch to reserved LPID */
1569 stw r0,0(r3) /* drop guest tlbie_lock */
1571 /* invalidate the whole TLB */
1580 /* take native_tlbie_lock */
1581 ld r3,toc_tlbie_lock@toc(2)
1589 ld r6,KVM_HOST_SDR1(r4)
1590 mtspr SPRN_SDR1,r6 /* switch to host page table */
1592 /* Set up host HID4 value */
1597 stw r0,0(r3) /* drop native_tlbie_lock */
1599 lis r8,0x7fff /* MAX_INT@h */
1602 /* Disable HDEC interrupts */
1605 rldimi r0,r3, HID0_HDICE_SH, 64-HID0_HDICE_SH-1
1615 /* load host SLB entries */
1616 33: ld r8,PACA_SLBSHADOWPTR(r13)
1618 .rept SLB_NUM_BOLTED
1619 ld r5,SLBSHADOW_SAVEAREA(r8)
1620 ld r6,SLBSHADOW_SAVEAREA+8(r8)
1621 andis. r7,r5,SLB_ESID_V@h
1627 /* Unset guest mode */
1628 li r0, KVM_GUEST_MODE_NONE
1629 stb r0, HSTATE_IN_GUEST(r13)
1631 ld r0, 112+PPC_LR_STKOFF(r1)
1637 * Check whether an HDSI is an HPTE not found fault or something else.
1638 * If it is an HPTE not found fault that is due to the guest accessing
1639 * a page that they have mapped but which we have paged out, then
1640 * we continue on with the guest exit path. In all other cases,
1641 * reflect the HDSI to the guest as a DSI.
1645 mfspr r6, SPRN_HDSISR
1646 /* HPTE not found fault or protection fault? */
1647 andis. r0, r6, (DSISR_NOHPTE | DSISR_PROTFAULT)@h
1648 beq 1f /* if not, send it to the guest */
1649 andi. r0, r11, MSR_DR /* data relocation enabled? */
1652 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
1653 bne 1f /* if no SLB entry found */
1654 4: std r4, VCPU_FAULT_DAR(r9)
1655 stw r6, VCPU_FAULT_DSISR(r9)
1657 /* Search the hash table. */
1658 mr r3, r9 /* vcpu pointer */
1659 li r7, 1 /* data fault */
1660 bl kvmppc_hpte_hv_fault
1661 ld r9, HSTATE_KVM_VCPU(r13)
1663 ld r11, VCPU_MSR(r9)
1664 li r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1665 cmpdi r3, 0 /* retry the instruction */
1667 cmpdi r3, -1 /* handle in kernel mode */
1669 cmpdi r3, -2 /* MMIO emulation; need instr word */
1672 /* Synthesize a DSI for the guest */
1673 ld r4, VCPU_FAULT_DAR(r9)
1675 1: mtspr SPRN_DAR, r4
1676 mtspr SPRN_DSISR, r6
1677 mtspr SPRN_SRR0, r10
1678 mtspr SPRN_SRR1, r11
1679 li r10, BOOK3S_INTERRUPT_DATA_STORAGE
1680 bl kvmppc_msr_interrupt
1681 fast_interrupt_c_return:
1682 6: ld r7, VCPU_CTR(r9)
1683 lwz r8, VCPU_XER(r9)
1689 3: ld r5, VCPU_KVM(r9) /* not relocated, use VRMA */
1690 ld r5, KVM_VRMA_SLB_V(r5)
1693 /* If this is for emulated MMIO, load the instruction word */
1694 2: li r8, KVM_INST_FETCH_FAILED /* In case lwz faults */
1696 /* Set guest mode to 'jump over instruction' so if lwz faults
1697 * we'll just continue at the next IP. */
1698 li r0, KVM_GUEST_MODE_SKIP
1699 stb r0, HSTATE_IN_GUEST(r13)
1701 /* Do the access with MSR:DR enabled */
1703 ori r4, r3, MSR_DR /* Enable paging for data */
1708 /* Store the result */
1709 stw r8, VCPU_LAST_INST(r9)
1711 /* Unset guest mode. */
1712 li r0, KVM_GUEST_MODE_HOST_HV
1713 stb r0, HSTATE_IN_GUEST(r13)
1717 * Similarly for an HISI, reflect it to the guest as an ISI unless
1718 * it is an HPTE not found fault for a page that we have paged out.
1721 andis. r0, r11, SRR1_ISI_NOPT@h
1723 andi. r0, r11, MSR_IR /* instruction relocation enabled? */
1726 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
1727 bne 1f /* if no SLB entry found */
1729 /* Search the hash table. */
1730 mr r3, r9 /* vcpu pointer */
1733 li r7, 0 /* instruction fault */
1734 bl kvmppc_hpte_hv_fault
1735 ld r9, HSTATE_KVM_VCPU(r13)
1737 ld r11, VCPU_MSR(r9)
1738 li r12, BOOK3S_INTERRUPT_H_INST_STORAGE
1739 cmpdi r3, 0 /* retry the instruction */
1740 beq fast_interrupt_c_return
1741 cmpdi r3, -1 /* handle in kernel mode */
1744 /* Synthesize an ISI for the guest */
1746 1: mtspr SPRN_SRR0, r10
1747 mtspr SPRN_SRR1, r11
1748 li r10, BOOK3S_INTERRUPT_INST_STORAGE
1749 bl kvmppc_msr_interrupt
1750 b fast_interrupt_c_return
1752 3: ld r6, VCPU_KVM(r9) /* not relocated, use VRMA */
1753 ld r5, KVM_VRMA_SLB_V(r6)
1757 * Try to handle an hcall in real mode.
1758 * Returns to the guest if we handle it, or continues on up to
1759 * the kernel if we can't (i.e. if we don't have a handler for
1760 * it, or if the handler returns H_TOO_HARD).
1762 .globl hcall_try_real_mode
1763 hcall_try_real_mode:
1764 ld r3,VCPU_GPR(R3)(r9)
1766 /* sc 1 from userspace - reflect to guest syscall */
1767 bne sc_1_fast_return
1769 cmpldi r3,hcall_real_table_end - hcall_real_table
1771 LOAD_REG_ADDR(r4, hcall_real_table)
1777 mr r3,r9 /* get vcpu pointer */
1778 ld r4,VCPU_GPR(R4)(r9)
1781 beq hcall_real_fallback
1782 ld r4,HSTATE_KVM_VCPU(r13)
1783 std r3,VCPU_GPR(R3)(r4)
1791 li r10, BOOK3S_INTERRUPT_SYSCALL
1792 bl kvmppc_msr_interrupt
1796 /* We've attempted a real mode hcall, but it's punted it back
1797 * to userspace. We need to restore some clobbered volatiles
1798 * before resuming the pass-it-to-qemu path */
1799 hcall_real_fallback:
1800 li r12,BOOK3S_INTERRUPT_SYSCALL
1801 ld r9, HSTATE_KVM_VCPU(r13)
1805 .globl hcall_real_table
1807 .long 0 /* 0 - unused */
1808 .long DOTSYM(kvmppc_h_remove) - hcall_real_table
1809 .long DOTSYM(kvmppc_h_enter) - hcall_real_table
1810 .long DOTSYM(kvmppc_h_read) - hcall_real_table
1811 .long 0 /* 0x10 - H_CLEAR_MOD */
1812 .long 0 /* 0x14 - H_CLEAR_REF */
1813 .long DOTSYM(kvmppc_h_protect) - hcall_real_table
1814 .long DOTSYM(kvmppc_h_get_tce) - hcall_real_table
1815 .long DOTSYM(kvmppc_h_put_tce) - hcall_real_table
1816 .long 0 /* 0x24 - H_SET_SPRG0 */
1817 .long DOTSYM(kvmppc_h_set_dabr) - hcall_real_table
1832 #ifdef CONFIG_KVM_XICS
1833 .long DOTSYM(kvmppc_rm_h_eoi) - hcall_real_table
1834 .long DOTSYM(kvmppc_rm_h_cppr) - hcall_real_table
1835 .long DOTSYM(kvmppc_rm_h_ipi) - hcall_real_table
1836 .long 0 /* 0x70 - H_IPOLL */
1837 .long DOTSYM(kvmppc_rm_h_xirr) - hcall_real_table
1839 .long 0 /* 0x64 - H_EOI */
1840 .long 0 /* 0x68 - H_CPPR */
1841 .long 0 /* 0x6c - H_IPI */
1842 .long 0 /* 0x70 - H_IPOLL */
1843 .long 0 /* 0x74 - H_XIRR */
1871 .long DOTSYM(kvmppc_h_cede) - hcall_real_table
1888 .long DOTSYM(kvmppc_h_bulk_remove) - hcall_real_table
1892 .long DOTSYM(kvmppc_h_set_xdabr) - hcall_real_table
1893 hcall_real_table_end:
1899 _GLOBAL(kvmppc_h_set_xdabr)
1900 andi. r0, r5, DABRX_USER | DABRX_KERNEL
1902 li r0, DABRX_USER | DABRX_KERNEL | DABRX_BTI
1905 6: li r3, H_PARAMETER
1908 _GLOBAL(kvmppc_h_set_dabr)
1909 li r5, DABRX_USER | DABRX_KERNEL
1913 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1914 std r4,VCPU_DABR(r3)
1915 stw r5, VCPU_DABRX(r3)
1916 mtspr SPRN_DABRX, r5
1917 /* Work around P7 bug where DABR can get corrupted on mtspr */
1918 1: mtspr SPRN_DABR,r4
1926 /* Emulate H_SET_DABR/X on P8 for the sake of compat mode guests */
1927 2: rlwimi r5, r4, 5, DAWRX_DR | DAWRX_DW
1928 rlwimi r5, r4, 1, DAWRX_WT
1930 std r4, VCPU_DAWR(r3)
1931 std r5, VCPU_DAWRX(r3)
1933 mtspr SPRN_DAWRX, r5
1937 _GLOBAL(kvmppc_h_cede)
1939 std r11,VCPU_MSR(r3)
1941 stb r0,VCPU_CEDED(r3)
1942 sync /* order setting ceded vs. testing prodded */
1943 lbz r5,VCPU_PRODDED(r3)
1945 bne kvm_cede_prodded
1946 li r0,0 /* set trap to 0 to say hcall is handled */
1947 stw r0,VCPU_TRAP(r3)
1949 std r0,VCPU_GPR(R3)(r3)
1951 b kvm_cede_exit /* just send it up to host on 970 */
1952 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_206)
1955 * Set our bit in the bitmask of napping threads unless all the
1956 * other threads are already napping, in which case we send this
1959 ld r5,HSTATE_KVM_VCORE(r13)
1960 lbz r6,HSTATE_PTID(r13)
1961 lwz r8,VCORE_ENTRY_EXIT(r5)
1965 addi r6,r5,VCORE_NAPPING_THREADS
1973 /* order napping_threads update vs testing entry_exit_count */
1976 stb r0,HSTATE_NAPPING(r13)
1977 lwz r7,VCORE_ENTRY_EXIT(r5)
1979 bge 33f /* another thread already exiting */
1982 * Although not specifically required by the architecture, POWER7
1983 * preserves the following registers in nap mode, even if an SMT mode
1984 * switch occurs: SLB entries, PURR, SPURR, AMOR, UAMOR, AMR, SPRG0-3,
1985 * DAR, DSISR, DABR, DABRX, DSCR, PMCx, MMCRx, SIAR, SDAR.
1987 /* Save non-volatile GPRs */
1988 std r14, VCPU_GPR(R14)(r3)
1989 std r15, VCPU_GPR(R15)(r3)
1990 std r16, VCPU_GPR(R16)(r3)
1991 std r17, VCPU_GPR(R17)(r3)
1992 std r18, VCPU_GPR(R18)(r3)
1993 std r19, VCPU_GPR(R19)(r3)
1994 std r20, VCPU_GPR(R20)(r3)
1995 std r21, VCPU_GPR(R21)(r3)
1996 std r22, VCPU_GPR(R22)(r3)
1997 std r23, VCPU_GPR(R23)(r3)
1998 std r24, VCPU_GPR(R24)(r3)
1999 std r25, VCPU_GPR(R25)(r3)
2000 std r26, VCPU_GPR(R26)(r3)
2001 std r27, VCPU_GPR(R27)(r3)
2002 std r28, VCPU_GPR(R28)(r3)
2003 std r29, VCPU_GPR(R29)(r3)
2004 std r30, VCPU_GPR(R30)(r3)
2005 std r31, VCPU_GPR(R31)(r3)
2011 * Take a nap until a decrementer or external or doobell interrupt
2012 * occurs, with PECE1, PECE0 and PECEDP set in LPCR. Also clear the
2013 * runlatch bit before napping.
2015 mfspr r2, SPRN_CTRLF
2017 mtspr SPRN_CTRLT, r2
2020 stb r0,HSTATE_HWTHREAD_REQ(r13)
2022 ori r5,r5,LPCR_PECE0 | LPCR_PECE1
2024 oris r5,r5,LPCR_PECEDP@h
2025 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
2029 std r0, HSTATE_SCRATCH0(r13)
2031 ld r0, HSTATE_SCRATCH0(r13)
2043 /* get vcpu pointer */
2044 ld r4, HSTATE_KVM_VCPU(r13)
2046 /* Woken by external or decrementer interrupt */
2047 ld r1, HSTATE_HOST_R1(r13)
2049 /* load up FP state */
2053 ld r14, VCPU_GPR(R14)(r4)
2054 ld r15, VCPU_GPR(R15)(r4)
2055 ld r16, VCPU_GPR(R16)(r4)
2056 ld r17, VCPU_GPR(R17)(r4)
2057 ld r18, VCPU_GPR(R18)(r4)
2058 ld r19, VCPU_GPR(R19)(r4)
2059 ld r20, VCPU_GPR(R20)(r4)
2060 ld r21, VCPU_GPR(R21)(r4)
2061 ld r22, VCPU_GPR(R22)(r4)
2062 ld r23, VCPU_GPR(R23)(r4)
2063 ld r24, VCPU_GPR(R24)(r4)
2064 ld r25, VCPU_GPR(R25)(r4)
2065 ld r26, VCPU_GPR(R26)(r4)
2066 ld r27, VCPU_GPR(R27)(r4)
2067 ld r28, VCPU_GPR(R28)(r4)
2068 ld r29, VCPU_GPR(R29)(r4)
2069 ld r30, VCPU_GPR(R30)(r4)
2070 ld r31, VCPU_GPR(R31)(r4)
2072 /* Check the wake reason in SRR1 to see why we got here */
2073 bl kvmppc_check_wake_reason
2075 /* clear our bit in vcore->napping_threads */
2076 34: ld r5,HSTATE_KVM_VCORE(r13)
2077 lbz r7,HSTATE_PTID(r13)
2080 addi r6,r5,VCORE_NAPPING_THREADS
2086 stb r0,HSTATE_NAPPING(r13)
2088 /* See if the wake reason means we need to exit */
2089 stw r12, VCPU_TRAP(r4)
2094 /* see if any other thread is already exiting */
2095 lwz r0,VCORE_ENTRY_EXIT(r5)
2099 b kvmppc_cede_reentry /* if not go back to guest */
2101 /* cede when already previously prodded case */
2104 stb r0,VCPU_PRODDED(r3)
2105 sync /* order testing prodded vs. clearing ceded */
2106 stb r0,VCPU_CEDED(r3)
2110 /* we've ceded but we want to give control to the host */
2112 b hcall_real_fallback
2114 /* Try to handle a machine check in real mode */
2115 machine_check_realmode:
2116 mr r3, r9 /* get vcpu pointer */
2117 bl kvmppc_realmode_machine_check
2119 cmpdi r3, 0 /* continue exiting from guest? */
2120 ld r9, HSTATE_KVM_VCPU(r13)
2121 li r12, BOOK3S_INTERRUPT_MACHINE_CHECK
2123 /* If not, deliver a machine check. SRR0/1 are already set */
2124 li r10, BOOK3S_INTERRUPT_MACHINE_CHECK
2125 bl kvmppc_msr_interrupt
2126 b fast_interrupt_c_return
2129 * Check the reason we woke from nap, and take appropriate action.
2131 * 0 if nothing needs to be done
2132 * 1 if something happened that needs to be handled by the host
2133 * -1 if there was a guest wakeup (IPI)
2135 * Also sets r12 to the interrupt vector for any interrupt that needs
2136 * to be handled now by the host (0x500 for external interrupt), or zero.
2138 kvmppc_check_wake_reason:
2141 rlwinm r6, r6, 45-31, 0xf /* extract wake reason field (P8) */
2143 rlwinm r6, r6, 45-31, 0xe /* P7 wake reason field is 3 bits */
2144 ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_207S)
2145 cmpwi r6, 8 /* was it an external interrupt? */
2146 li r12, BOOK3S_INTERRUPT_EXTERNAL
2147 beq kvmppc_read_intr /* if so, see what it was */
2150 cmpwi r6, 6 /* was it the decrementer? */
2153 cmpwi r6, 5 /* privileged doorbell? */
2155 cmpwi r6, 3 /* hypervisor doorbell? */
2157 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
2158 li r3, 1 /* anything else, return 1 */
2161 /* hypervisor doorbell */
2162 3: li r12, BOOK3S_INTERRUPT_H_DOORBELL
2167 * Determine what sort of external interrupt is pending (if any).
2169 * 0 if no interrupt is pending
2170 * 1 if an interrupt is pending that needs to be handled by the host
2171 * -1 if there was a guest wakeup IPI (which has now been cleared)
2174 /* see if a host IPI is pending */
2176 lbz r0, HSTATE_HOST_IPI(r13)
2180 /* Now read the interrupt from the ICP */
2181 ld r6, HSTATE_XICS_PHYS(r13)
2186 rlwinm. r3, r0, 0, 0xffffff
2188 beq 1f /* if nothing pending in the ICP */
2190 /* We found something in the ICP...
2192 * If it's not an IPI, stash it in the PACA and return to
2193 * the host, we don't (yet) handle directing real external
2194 * interrupts directly to the guest
2196 cmpwi r3, XICS_IPI /* if there is, is it an IPI? */
2199 /* It's an IPI, clear the MFRR and EOI it */
2202 stbcix r3, r6, r8 /* clear the IPI */
2203 stwcix r0, r6, r7 /* EOI it */
2206 /* We need to re-check host IPI now in case it got set in the
2207 * meantime. If it's clear, we bounce the interrupt to the
2210 lbz r0, HSTATE_HOST_IPI(r13)
2214 /* OK, it's an IPI for us */
2218 42: /* It's not an IPI and it's for the host, stash it in the PACA
2219 * before exit, it will be picked up by the host ICP driver
2221 stw r0, HSTATE_SAVED_XIRR(r13)
2225 43: /* We raced with the host, we need to resend that IPI, bummer */
2227 stbcix r0, r6, r8 /* set the IPI */
2233 * Save away FP, VMX and VSX registers.
2235 * N.B. r30 and r31 are volatile across this function,
2236 * thus it is not callable from C.
2243 #ifdef CONFIG_ALTIVEC
2245 oris r8,r8,MSR_VEC@h
2246 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2250 oris r8,r8,MSR_VSX@h
2251 END_FTR_SECTION_IFSET(CPU_FTR_VSX)
2255 addi r3,r3,VCPU_FPRS
2257 #ifdef CONFIG_ALTIVEC
2259 addi r3,r31,VCPU_VRS
2261 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2263 mfspr r6,SPRN_VRSAVE
2264 stw r6,VCPU_VRSAVE(r31)
2269 * Load up FP, VMX and VSX registers
2271 * N.B. r30 and r31 are volatile across this function,
2272 * thus it is not callable from C.
2279 #ifdef CONFIG_ALTIVEC
2281 oris r8,r8,MSR_VEC@h
2282 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2286 oris r8,r8,MSR_VSX@h
2287 END_FTR_SECTION_IFSET(CPU_FTR_VSX)
2291 addi r3,r4,VCPU_FPRS
2293 #ifdef CONFIG_ALTIVEC
2295 addi r3,r31,VCPU_VRS
2297 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2299 lwz r7,VCPU_VRSAVE(r31)
2300 mtspr SPRN_VRSAVE,r7
2306 * We come here if we get any exception or interrupt while we are
2307 * executing host real mode code while in guest MMU context.
2308 * For now just spin, but we should do something better.
2310 kvmppc_bad_host_intr:
2314 * This mimics the MSR transition on IRQ delivery. The new guest MSR is taken
2315 * from VCPU_INTR_MSR and is modified based on the required TM state changes.
2316 * r11 has the guest MSR value (in/out)
2317 * r9 has a vcpu pointer (in)
2318 * r0 is used as a scratch register
2320 kvmppc_msr_interrupt:
2321 rldicl r0, r11, 64 - MSR_TS_S_LG, 62
2322 cmpwi r0, 2 /* Check if we are in transactional state.. */
2323 ld r11, VCPU_INTR_MSR(r9)
2325 /* ... if transactional, change to suspended */
2327 1: rldimi r11, r0, MSR_TS_S_LG, 63 - MSR_TS_T_LG