blob: 4e4390564276cbaad9687ac9f7d28bc0fa98a221 [file] [log] [blame]
Paul Mackerrasde56a942011-06-29 00:21:34 +00001/*
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.
5 *
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.
10 *
11 * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
12 *
13 * Derived from book3s_rmhandlers.S and other files, which are:
14 *
15 * Copyright SUSE Linux Products GmbH 2009
16 *
17 * Authors: Alexander Graf <agraf@suse.de>
18 */
19
20#include <asm/ppc_asm.h>
21#include <asm/kvm_asm.h>
22#include <asm/reg.h>
Paul Mackerras177339d2011-07-23 17:41:11 +100023#include <asm/mmu.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000024#include <asm/page.h>
Paul Mackerras177339d2011-07-23 17:41:11 +100025#include <asm/ptrace.h>
26#include <asm/hvcall.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000027#include <asm/asm-offsets.h>
28#include <asm/exception-64s.h>
Paul Mackerrasf0888f72012-02-03 00:54:17 +000029#include <asm/kvm_book3s_asm.h>
Aneesh Kumar K.Vf64e8082016-03-01 12:59:20 +053030#include <asm/book3s/64/mmu-hash.h>
Michael Neulinge4e38122014-03-25 10:47:02 +110031#include <asm/tm.h>
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +053032#include <asm/opal.h>
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +100033#include <asm/xive-regs.h>
Michael Neulinge4e38122014-03-25 10:47:02 +110034
Paul Mackerras2f272462017-05-22 16:25:14 +100035/* Sign-extend HDEC if not on POWER9 */
36#define EXTEND_HDEC(reg) \
37BEGIN_FTR_SECTION; \
38 extsw reg, reg; \
39END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
40
Michael Neulinge4e38122014-03-25 10:47:02 +110041#define VCPU_GPRS_TM(reg) (((reg) * ULONG_SIZE) + VCPU_GPR_TM)
Paul Mackerrasde56a942011-06-29 00:21:34 +000042
Paul Mackerrase0b7ec02014-01-08 21:25:20 +110043/* Values in HSTATE_NAPPING(r13) */
44#define NAPPING_CEDE 1
45#define NAPPING_NOVCPU 2
46
Paul Mackerrasde56a942011-06-29 00:21:34 +000047/*
Paul Mackerras19ccb762011-07-23 17:42:46 +100048 * Call kvmppc_hv_entry in real mode.
Paul Mackerrasde56a942011-06-29 00:21:34 +000049 * Must be called with interrupts hard-disabled.
50 *
51 * Input Registers:
52 *
53 * LR = return address to continue at after eventually re-enabling MMU
54 */
Anton Blanchard6ed179b2014-06-12 18:16:53 +100055_GLOBAL_TOC(kvmppc_hv_entry_trampoline)
Paul Mackerras218309b2013-09-06 13:23:44 +100056 mflr r0
57 std r0, PPC_LR_STKOFF(r1)
58 stdu r1, -112(r1)
Paul Mackerrasde56a942011-06-29 00:21:34 +000059 mfmsr r10
Paul Mackerras218309b2013-09-06 13:23:44 +100060 LOAD_REG_ADDR(r5, kvmppc_call_hv_entry)
Paul Mackerrasde56a942011-06-29 00:21:34 +000061 li r0,MSR_RI
62 andc r0,r10,r0
63 li r6,MSR_IR | MSR_DR
64 andc r6,r10,r6
65 mtmsrd r0,1 /* clear RI in MSR */
66 mtsrr0 r5
67 mtsrr1 r6
68 RFI
69
Paul Mackerras218309b2013-09-06 13:23:44 +100070kvmppc_call_hv_entry:
Paul Mackerrase0b7ec02014-01-08 21:25:20 +110071 ld r4, HSTATE_KVM_VCPU(r13)
Paul Mackerras218309b2013-09-06 13:23:44 +100072 bl kvmppc_hv_entry
73
74 /* Back from guest - restore host state and return to caller */
75
Michael Neulingeee7ff92014-01-08 21:25:19 +110076BEGIN_FTR_SECTION
Paul Mackerras218309b2013-09-06 13:23:44 +100077 /* Restore host DABR and DABRX */
78 ld r5,HSTATE_DABR(r13)
79 li r6,7
80 mtspr SPRN_DABR,r5
81 mtspr SPRN_DABRX,r6
Michael Neulingeee7ff92014-01-08 21:25:19 +110082END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
Paul Mackerras218309b2013-09-06 13:23:44 +100083
84 /* Restore SPRG3 */
Scott Wood9d378df2014-03-10 17:29:38 -050085 ld r3,PACA_SPRG_VDSO(r13)
86 mtspr SPRN_SPRG_VDSO_WRITE,r3
Paul Mackerras218309b2013-09-06 13:23:44 +100087
Paul Mackerras218309b2013-09-06 13:23:44 +100088 /* Reload the host's PMU registers */
89 ld r3, PACALPPACAPTR(r13) /* is the host using the PMU? */
90 lbz r4, LPPACA_PMCINUSE(r3)
91 cmpwi r4, 0
92 beq 23f /* skip if not */
Paul Mackerras9bc01a92014-05-26 19:48:40 +100093BEGIN_FTR_SECTION
Michael Ellerman9a4fc4e2014-07-10 19:34:31 +100094 ld r3, HSTATE_MMCR0(r13)
Paul Mackerras9bc01a92014-05-26 19:48:40 +100095 andi. r4, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO
96 cmpwi r4, MMCR0_PMAO
97 beql kvmppc_fix_pmao
98END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG)
Michael Ellerman9a4fc4e2014-07-10 19:34:31 +100099 lwz r3, HSTATE_PMC1(r13)
100 lwz r4, HSTATE_PMC2(r13)
101 lwz r5, HSTATE_PMC3(r13)
102 lwz r6, HSTATE_PMC4(r13)
103 lwz r8, HSTATE_PMC5(r13)
104 lwz r9, HSTATE_PMC6(r13)
Paul Mackerras218309b2013-09-06 13:23:44 +1000105 mtspr SPRN_PMC1, r3
106 mtspr SPRN_PMC2, r4
107 mtspr SPRN_PMC3, r5
108 mtspr SPRN_PMC4, r6
109 mtspr SPRN_PMC5, r8
110 mtspr SPRN_PMC6, r9
Michael Ellerman9a4fc4e2014-07-10 19:34:31 +1000111 ld r3, HSTATE_MMCR0(r13)
112 ld r4, HSTATE_MMCR1(r13)
113 ld r5, HSTATE_MMCRA(r13)
114 ld r6, HSTATE_SIAR(r13)
115 ld r7, HSTATE_SDAR(r13)
Paul Mackerras218309b2013-09-06 13:23:44 +1000116 mtspr SPRN_MMCR1, r4
117 mtspr SPRN_MMCRA, r5
Paul Mackerras72cde5a2014-03-25 10:47:08 +1100118 mtspr SPRN_SIAR, r6
119 mtspr SPRN_SDAR, r7
120BEGIN_FTR_SECTION
Michael Ellerman9a4fc4e2014-07-10 19:34:31 +1000121 ld r8, HSTATE_MMCR2(r13)
122 ld r9, HSTATE_SIER(r13)
Paul Mackerras72cde5a2014-03-25 10:47:08 +1100123 mtspr SPRN_MMCR2, r8
124 mtspr SPRN_SIER, r9
125END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerras218309b2013-09-06 13:23:44 +1000126 mtspr SPRN_MMCR0, r3
127 isync
12823:
129
130 /*
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100131 * Reload DEC. HDEC interrupts were disabled when
132 * we reloaded the host's LPCR value.
133 */
134 ld r3, HSTATE_DECEXP(r13)
135 mftb r4
136 subf r4, r4, r3
137 mtspr SPRN_DEC, r4
138
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000139 /* hwthread_req may have got set by cede or no vcpu, so clear it */
140 li r0, 0
141 stb r0, HSTATE_HWTHREAD_REQ(r13)
142
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100143 /*
Paul Mackerras218309b2013-09-06 13:23:44 +1000144 * For external and machine check interrupts, we need
145 * to call the Linux handler to process the interrupt.
146 * We do that by jumping to absolute address 0x500 for
147 * external interrupts, or the machine_check_fwnmi label
148 * for machine checks (since firmware might have patched
149 * the vector area at 0x200). The [h]rfid at the end of the
150 * handler will return to the book3s_hv_interrupts.S code.
151 * For other interrupts we do the rfid to get back
152 * to the book3s_hv_interrupts.S code here.
153 */
154 ld r8, 112+PPC_LR_STKOFF(r1)
155 addi r1, r1, 112
156 ld r7, HSTATE_HOST_MSR(r13)
157
Paul Mackerras53af3ba2017-01-30 21:21:51 +1100158 /*
159 * If we came back from the guest via a relocation-on interrupt,
160 * we will be in virtual mode at this point, which makes it a
161 * little easier to get back to the caller.
162 */
163 mfmsr r0
164 andi. r0, r0, MSR_IR /* in real mode? */
165 bne .Lvirt_return
166
Paul Mackerras218309b2013-09-06 13:23:44 +1000167 cmpwi cr1, r12, BOOK3S_INTERRUPT_MACHINE_CHECK
168 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
Paul Mackerras218309b2013-09-06 13:23:44 +1000169 beq 11f
Gautham R. Shenoy70aa3962015-10-15 11:29:58 +0530170 cmpwi r12, BOOK3S_INTERRUPT_H_DOORBELL
171 beq 15f /* Invoke the H_DOORBELL handler */
Mahesh Salgaonkar0869b6f2014-07-29 18:40:01 +0530172 cmpwi cr2, r12, BOOK3S_INTERRUPT_HMI
173 beq cr2, 14f /* HMI check */
Paul Mackerras218309b2013-09-06 13:23:44 +1000174
175 /* RFI into the highmem handler, or branch to interrupt handler */
176 mfmsr r6
177 li r0, MSR_RI
178 andc r6, r6, r0
179 mtmsrd r6, 1 /* Clear RI in MSR */
180 mtsrr0 r8
181 mtsrr1 r7
Paul Mackerras218309b2013-09-06 13:23:44 +1000182 beq cr1, 13f /* machine check */
183 RFI
184
185 /* On POWER7, we have external interrupts set to use HSRR0/1 */
18611: mtspr SPRN_HSRR0, r8
187 mtspr SPRN_HSRR1, r7
188 ba 0x500
189
19013: b machine_check_fwnmi
191
Mahesh Salgaonkar0869b6f2014-07-29 18:40:01 +053019214: mtspr SPRN_HSRR0, r8
193 mtspr SPRN_HSRR1, r7
194 b hmi_exception_after_realmode
195
Gautham R. Shenoy70aa3962015-10-15 11:29:58 +053019615: mtspr SPRN_HSRR0, r8
197 mtspr SPRN_HSRR1, r7
198 ba 0xe80
199
Paul Mackerras53af3ba2017-01-30 21:21:51 +1100200 /* Virtual-mode return - can't get here for HMI or machine check */
201.Lvirt_return:
202 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
203 beq 16f
204 cmpwi r12, BOOK3S_INTERRUPT_H_DOORBELL
205 beq 17f
206 andi. r0, r7, MSR_EE /* were interrupts hard-enabled? */
207 beq 18f
208 mtmsrd r7, 1 /* if so then re-enable them */
20918: mtlr r8
210 blr
211
21216: mtspr SPRN_HSRR0, r8 /* jump to reloc-on external vector */
213 mtspr SPRN_HSRR1, r7
214 b exc_virt_0x4500_hardware_interrupt
215
21617: mtspr SPRN_HSRR0, r8
217 mtspr SPRN_HSRR1, r7
218 b exc_virt_0x4e80_h_doorbell
219
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100220kvmppc_primary_no_guest:
221 /* We handle this much like a ceded vcpu */
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +1100222 /* put the HDEC into the DEC, since HDEC interrupts don't wake us */
Paul Mackerras2f272462017-05-22 16:25:14 +1000223 /* HDEC may be larger than DEC for arch >= v3.00, but since the */
224 /* HDEC value came from DEC in the first place, it will fit */
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +1100225 mfspr r3, SPRN_HDEC
226 mtspr SPRN_DEC, r3
Paul Mackerras6af27c82015-03-28 14:21:10 +1100227 /*
228 * Make sure the primary has finished the MMU switch.
229 * We should never get here on a secondary thread, but
230 * check it for robustness' sake.
231 */
232 ld r5, HSTATE_KVM_VCORE(r13)
23365: lbz r0, VCORE_IN_GUEST(r5)
234 cmpwi r0, 0
235 beq 65b
236 /* Set LPCR. */
237 ld r8,VCORE_LPCR(r5)
238 mtspr SPRN_LPCR,r8
239 isync
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100240 /* set our bit in napping_threads */
241 ld r5, HSTATE_KVM_VCORE(r13)
242 lbz r7, HSTATE_PTID(r13)
243 li r0, 1
244 sld r0, r0, r7
245 addi r6, r5, VCORE_NAPPING_THREADS
2461: lwarx r3, 0, r6
247 or r3, r3, r0
248 stwcx. r3, 0, r6
249 bne 1b
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100250 /* order napping_threads update vs testing entry_exit_map */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100251 isync
252 li r12, 0
253 lwz r7, VCORE_ENTRY_EXIT(r5)
254 cmpwi r7, 0x100
255 bge kvm_novcpu_exit /* another thread already exiting */
256 li r3, NAPPING_NOVCPU
257 stb r3, HSTATE_NAPPING(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100258
Paul Mackerrasccc07772015-03-28 14:21:07 +1100259 li r3, 0 /* Don't wake on privileged (OS) doorbell */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100260 b kvm_do_nap
261
Suresh Warrier37f55d32016-08-19 15:35:46 +1000262/*
263 * kvm_novcpu_wakeup
264 * Entered from kvm_start_guest if kvm_hstate.napping is set
265 * to NAPPING_NOVCPU
266 * r2 = kernel TOC
267 * r13 = paca
268 */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100269kvm_novcpu_wakeup:
270 ld r1, HSTATE_HOST_R1(r13)
271 ld r5, HSTATE_KVM_VCORE(r13)
272 li r0, 0
273 stb r0, HSTATE_NAPPING(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100274
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100275 /* check the wake reason */
276 bl kvmppc_check_wake_reason
Paul Mackerras6af27c82015-03-28 14:21:10 +1100277
Suresh Warrier37f55d32016-08-19 15:35:46 +1000278 /*
279 * Restore volatile registers since we could have called
280 * a C routine in kvmppc_check_wake_reason.
281 * r5 = VCORE
282 */
283 ld r5, HSTATE_KVM_VCORE(r13)
284
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100285 /* see if any other thread is already exiting */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100286 lwz r0, VCORE_ENTRY_EXIT(r5)
287 cmpwi r0, 0x100
288 bge kvm_novcpu_exit
289
290 /* clear our bit in napping_threads */
291 lbz r7, HSTATE_PTID(r13)
292 li r0, 1
293 sld r0, r0, r7
294 addi r6, r5, VCORE_NAPPING_THREADS
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002954: lwarx r7, 0, r6
296 andc r7, r7, r0
297 stwcx. r7, 0, r6
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100298 bne 4b
299
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100300 /* See if the wake reason means we need to exit */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100301 cmpdi r3, 0
302 bge kvm_novcpu_exit
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100303
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +1100304 /* See if our timeslice has expired (HDEC is negative) */
305 mfspr r0, SPRN_HDEC
Paul Mackerras2f272462017-05-22 16:25:14 +1000306 EXTEND_HDEC(r0)
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +1100307 li r12, BOOK3S_INTERRUPT_HV_DECREMENTER
Paul Mackerras2f272462017-05-22 16:25:14 +1000308 cmpdi r0, 0
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +1100309 blt kvm_novcpu_exit
310
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100311 /* Got an IPI but other vcpus aren't yet exiting, must be a latecomer */
312 ld r4, HSTATE_KVM_VCPU(r13)
313 cmpdi r4, 0
Paul Mackerrasb6c295d2015-03-28 14:21:02 +1100314 beq kvmppc_primary_no_guest
315
316#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
317 addi r3, r4, VCPU_TB_RMENTRY
318 bl kvmhv_start_timing
319#endif
320 b kvmppc_got_guest
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100321
322kvm_novcpu_exit:
Paul Mackerras6af27c82015-03-28 14:21:10 +1100323#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
324 ld r4, HSTATE_KVM_VCPU(r13)
325 cmpdi r4, 0
326 beq 13f
327 addi r3, r4, VCPU_TB_RMEXIT
328 bl kvmhv_accumulate_time
329#endif
Paul Mackerraseddb60f2015-03-28 14:21:11 +110033013: mr r3, r12
331 stw r12, 112-4(r1)
332 bl kvmhv_commence_exit
333 nop
334 lwz r12, 112-4(r1)
Paul Mackerras6af27c82015-03-28 14:21:10 +1100335 b kvmhv_switch_to_host
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100336
Paul Mackerras371fefd2011-06-29 00:23:08 +0000337/*
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100338 * We come in here when wakened from nap mode.
Paul Mackerras371fefd2011-06-29 00:23:08 +0000339 * Relocation is off and most register values are lost.
340 * r13 points to the PACA.
341 */
342 .globl kvm_start_guest
343kvm_start_guest:
Preeti U Murthyfd17dc72014-04-11 16:01:58 +0530344
345 /* Set runlatch bit the minute you wake up from nap */
Paul Mackerras1f09c3e2015-03-28 14:21:04 +1100346 mfspr r0, SPRN_CTRLF
347 ori r0, r0, 1
348 mtspr SPRN_CTRLT, r0
Preeti U Murthyfd17dc72014-04-11 16:01:58 +0530349
Paul Mackerras19ccb762011-07-23 17:42:46 +1000350 ld r2,PACATOC(r13)
351
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000352 li r0,KVM_HWTHREAD_IN_KVM
353 stb r0,HSTATE_HWTHREAD_STATE(r13)
354
355 /* NV GPR values from power7_idle() will no longer be valid */
356 li r0,1
357 stb r0,PACA_NAPSTATELOST(r13)
358
Paul Mackerras4619ac82013-04-17 20:31:41 +0000359 /* were we napping due to cede? */
360 lbz r0,HSTATE_NAPPING(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100361 cmpwi r0,NAPPING_CEDE
362 beq kvm_end_cede
363 cmpwi r0,NAPPING_NOVCPU
364 beq kvm_novcpu_wakeup
365
366 ld r1,PACAEMERGSP(r13)
367 subi r1,r1,STACK_FRAME_OVERHEAD
Paul Mackerras4619ac82013-04-17 20:31:41 +0000368
369 /*
370 * We weren't napping due to cede, so this must be a secondary
371 * thread being woken up to run a guest, or being woken up due
372 * to a stray IPI. (Or due to some machine check or hypervisor
373 * maintenance interrupt while the core is in KVM.)
374 */
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000375
376 /* Check the wake reason in SRR1 to see why we got here */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100377 bl kvmppc_check_wake_reason
Suresh Warrier37f55d32016-08-19 15:35:46 +1000378 /*
379 * kvmppc_check_wake_reason could invoke a C routine, but we
380 * have no volatile registers to restore when we return.
381 */
382
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100383 cmpdi r3, 0
384 bge kvm_no_guest
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000385
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000386 /* get vcore pointer, NULL if we have nothing to run */
387 ld r5,HSTATE_KVM_VCORE(r13)
388 cmpdi r5,0
389 /* if we have no vcore to run, go back to sleep */
Paul Mackerras7b444c62012-10-15 01:16:14 +0000390 beq kvm_no_guest
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000391
Paul Mackerras56548fc2014-12-03 14:48:40 +1100392kvm_secondary_got_guest:
393
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100394 /* Set HSTATE_DSCR(r13) to something sensible */
Anshuman Khandual1db36522015-05-21 12:13:03 +0530395 ld r6, PACA_DSCR_DEFAULT(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100396 std r6, HSTATE_DSCR(r13)
Paul Mackerras371fefd2011-06-29 00:23:08 +0000397
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000398 /* On thread 0 of a subcore, set HDEC to max */
399 lbz r4, HSTATE_PTID(r13)
400 cmpwi r4, 0
401 bne 63f
Paul Mackerras2f272462017-05-22 16:25:14 +1000402 LOAD_REG_ADDR(r6, decrementer_max)
403 ld r6, 0(r6)
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000404 mtspr SPRN_HDEC, r6
405 /* and set per-LPAR registers, if doing dynamic micro-threading */
406 ld r6, HSTATE_SPLIT_MODE(r13)
407 cmpdi r6, 0
408 beq 63f
409 ld r0, KVM_SPLIT_RPR(r6)
410 mtspr SPRN_RPR, r0
411 ld r0, KVM_SPLIT_PMMAR(r6)
412 mtspr SPRN_PMMAR, r0
413 ld r0, KVM_SPLIT_LDBAR(r6)
414 mtspr SPRN_LDBAR, r0
415 isync
41663:
417 /* Order load of vcpu after load of vcore */
Paul Mackerras5d5b99c2015-03-28 14:21:06 +1100418 lwsync
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000419 ld r4, HSTATE_KVM_VCPU(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100420 bl kvmppc_hv_entry
Paul Mackerras218309b2013-09-06 13:23:44 +1000421
422 /* Back from the guest, go back to nap */
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000423 /* Clear our vcpu and vcore pointers so we don't come back in early */
Paul Mackerras218309b2013-09-06 13:23:44 +1000424 li r0, 0
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000425 std r0, HSTATE_KVM_VCPU(r13)
Paul Mackerrasf019b7a2013-11-16 17:46:03 +1100426 /*
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000427 * Once we clear HSTATE_KVM_VCORE(r13), the code in
Paul Mackerras5d5b99c2015-03-28 14:21:06 +1100428 * kvmppc_run_core() is going to assume that all our vcpu
429 * state is visible in memory. This lwsync makes sure
430 * that that is true.
Paul Mackerrasf019b7a2013-11-16 17:46:03 +1100431 */
Paul Mackerras218309b2013-09-06 13:23:44 +1000432 lwsync
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000433 std r0, HSTATE_KVM_VCORE(r13)
Paul Mackerras218309b2013-09-06 13:23:44 +1000434
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +0530435 /*
436 * All secondaries exiting guest will fall through this path.
437 * Before proceeding, just check for HMI interrupt and
438 * invoke opal hmi handler. By now we are sure that the
439 * primary thread on this core/subcore has already made partition
440 * switch/TB resync and we are good to call opal hmi handler.
441 */
442 cmpwi r12, BOOK3S_INTERRUPT_HMI
443 bne kvm_no_guest
444
445 li r3,0 /* NULL argument */
446 bl hmi_exception_realmode
Paul Mackerras56548fc2014-12-03 14:48:40 +1100447/*
448 * At this point we have finished executing in the guest.
449 * We need to wait for hwthread_req to become zero, since
450 * we may not turn on the MMU while hwthread_req is non-zero.
451 * While waiting we also need to check if we get given a vcpu to run.
452 */
Paul Mackerras218309b2013-09-06 13:23:44 +1000453kvm_no_guest:
Paul Mackerras56548fc2014-12-03 14:48:40 +1100454 lbz r3, HSTATE_HWTHREAD_REQ(r13)
455 cmpwi r3, 0
456 bne 53f
457 HMT_MEDIUM
458 li r0, KVM_HWTHREAD_IN_KERNEL
Paul Mackerras218309b2013-09-06 13:23:44 +1000459 stb r0, HSTATE_HWTHREAD_STATE(r13)
Paul Mackerras56548fc2014-12-03 14:48:40 +1100460 /* need to recheck hwthread_req after a barrier, to avoid race */
461 sync
462 lbz r3, HSTATE_HWTHREAD_REQ(r13)
463 cmpwi r3, 0
464 bne 54f
465/*
Shreyas B. Prabhu5fa6b6b2016-07-08 11:50:46 +0530466 * We jump to pnv_wakeup_loss, which will return to the caller
Paul Mackerras56548fc2014-12-03 14:48:40 +1100467 * of power7_nap in the powernv cpu offline loop. The value we
468 * put in r3 becomes the return value for power7_nap.
469 */
Paul Mackerras218309b2013-09-06 13:23:44 +1000470 li r3, LPCR_PECE0
471 mfspr r4, SPRN_LPCR
472 rlwimi r4, r3, 0, LPCR_PECE0 | LPCR_PECE1
473 mtspr SPRN_LPCR, r4
Paul Mackerras56548fc2014-12-03 14:48:40 +1100474 li r3, 0
Shreyas B. Prabhu5fa6b6b2016-07-08 11:50:46 +0530475 b pnv_wakeup_loss
Paul Mackerras56548fc2014-12-03 14:48:40 +1100476
47753: HMT_LOW
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000478 ld r5, HSTATE_KVM_VCORE(r13)
479 cmpdi r5, 0
480 bne 60f
481 ld r3, HSTATE_SPLIT_MODE(r13)
482 cmpdi r3, 0
483 beq kvm_no_guest
484 lbz r0, KVM_SPLIT_DO_NAP(r3)
485 cmpwi r0, 0
Paul Mackerras56548fc2014-12-03 14:48:40 +1100486 beq kvm_no_guest
487 HMT_MEDIUM
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000488 b kvm_unsplit_nap
48960: HMT_MEDIUM
Paul Mackerras56548fc2014-12-03 14:48:40 +1100490 b kvm_secondary_got_guest
491
49254: li r0, KVM_HWTHREAD_IN_KVM
493 stb r0, HSTATE_HWTHREAD_STATE(r13)
494 b kvm_no_guest
Paul Mackerras218309b2013-09-06 13:23:44 +1000495
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000496/*
497 * Here the primary thread is trying to return the core to
498 * whole-core mode, so we need to nap.
499 */
500kvm_unsplit_nap:
Gautham R. Shenoy7f235322015-09-02 21:48:58 +0530501 /*
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +0530502 * When secondaries are napping in kvm_unsplit_nap() with
503 * hwthread_req = 1, HMI goes ignored even though subcores are
504 * already exited the guest. Hence HMI keeps waking up secondaries
505 * from nap in a loop and secondaries always go back to nap since
506 * no vcore is assigned to them. This makes impossible for primary
507 * thread to get hold of secondary threads resulting into a soft
508 * lockup in KVM path.
509 *
510 * Let us check if HMI is pending and handle it before we go to nap.
511 */
512 cmpwi r12, BOOK3S_INTERRUPT_HMI
513 bne 55f
514 li r3, 0 /* NULL argument */
515 bl hmi_exception_realmode
51655:
517 /*
Gautham R. Shenoy7f235322015-09-02 21:48:58 +0530518 * Ensure that secondary doesn't nap when it has
519 * its vcore pointer set.
520 */
521 sync /* matches smp_mb() before setting split_info.do_nap */
522 ld r0, HSTATE_KVM_VCORE(r13)
523 cmpdi r0, 0
524 bne kvm_no_guest
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000525 /* clear any pending message */
526BEGIN_FTR_SECTION
527 lis r6, (PPC_DBELL_SERVER << (63-36))@h
528 PPC_MSGCLR(6)
529END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
530 /* Set kvm_split_mode.napped[tid] = 1 */
531 ld r3, HSTATE_SPLIT_MODE(r13)
532 li r0, 1
533 lhz r4, PACAPACAINDEX(r13)
534 clrldi r4, r4, 61 /* micro-threading => P8 => 8 threads/core */
535 addi r4, r4, KVM_SPLIT_NAPPED
536 stbx r0, r3, r4
537 /* Check the do_nap flag again after setting napped[] */
538 sync
539 lbz r0, KVM_SPLIT_DO_NAP(r3)
540 cmpwi r0, 0
541 beq 57f
542 li r3, (LPCR_PECEDH | LPCR_PECE0) >> 4
Paul Mackerrasbf53c882016-11-18 14:34:07 +1100543 mfspr r5, SPRN_LPCR
544 rlwimi r5, r3, 4, (LPCR_PECEDP | LPCR_PECEDH | LPCR_PECE0 | LPCR_PECE1)
545 b kvm_nap_sequence
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000546
54757: li r0, 0
548 stbx r0, r3, r4
549 b kvm_no_guest
550
Paul Mackerras218309b2013-09-06 13:23:44 +1000551/******************************************************************************
552 * *
553 * Entry code *
554 * *
555 *****************************************************************************/
556
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100557/* Stack frame offsets */
558#define STACK_SLOT_TID (112-16)
559#define STACK_SLOT_PSSCR (112-24)
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100560#define STACK_SLOT_PID (112-32)
Paul Mackerras4c3bb4c2017-06-15 15:43:17 +1000561#define STACK_SLOT_IAMR (112-40)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100562
Paul Mackerrasde56a942011-06-29 00:21:34 +0000563.global kvmppc_hv_entry
564kvmppc_hv_entry:
565
566 /* Required state:
567 *
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100568 * R4 = vcpu pointer (or NULL)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000569 * MSR = ~IR|DR
570 * R13 = PACA
571 * R1 = host R1
Michael Neuling06a29e42014-08-19 14:59:30 +1000572 * R2 = TOC
Paul Mackerrasde56a942011-06-29 00:21:34 +0000573 * all other volatile GPRS = free
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100574 * Does not preserve non-volatile GPRs or CR fields
Paul Mackerrasde56a942011-06-29 00:21:34 +0000575 */
576 mflr r0
Paul Mackerras218309b2013-09-06 13:23:44 +1000577 std r0, PPC_LR_STKOFF(r1)
578 stdu r1, -112(r1)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000579
Paul Mackerrasde56a942011-06-29 00:21:34 +0000580 /* Save R1 in the PACA */
581 std r1, HSTATE_HOST_R1(r13)
582
Paul Mackerras44a3add2013-10-04 21:45:04 +1000583 li r6, KVM_GUEST_MODE_HOST_HV
584 stb r6, HSTATE_IN_GUEST(r13)
585
Paul Mackerrasb6c295d2015-03-28 14:21:02 +1100586#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
587 /* Store initial timestamp */
588 cmpdi r4, 0
589 beq 1f
590 addi r3, r4, VCPU_TB_RMENTRY
591 bl kvmhv_start_timing
5921:
593#endif
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100594
595 /* Use cr7 as an indication of radix mode */
596 ld r5, HSTATE_KVM_VCORE(r13)
597 ld r9, VCORE_KVM(r5) /* pointer to struct kvm */
598 lbz r0, KVM_RADIX(r9)
599 cmpwi cr7, r0, 0
600
601 /* Clear out SLB if hash */
602 bne cr7, 2f
Paul Mackerrasde56a942011-06-29 00:21:34 +0000603 li r6,0
604 slbmte r6,r6
605 slbia
606 ptesync
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11006072:
Paul Mackerras9e368f22011-06-29 00:40:08 +0000608 /*
Paul Mackerrasc17b98c2014-12-03 13:30:38 +1100609 * POWER7/POWER8 host -> guest partition switch code.
Paul Mackerras9e368f22011-06-29 00:40:08 +0000610 * We don't have to lock against concurrent tlbies,
611 * but we do have to coordinate across hardware threads.
612 */
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100613 /* Set bit in entry map iff exit map is zero. */
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100614 li r7, 1
615 lbz r6, HSTATE_PTID(r13)
616 sld r7, r7, r6
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100617 addi r8, r5, VCORE_ENTRY_EXIT
61821: lwarx r3, 0, r8
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100619 cmpwi r3, 0x100 /* any threads starting to exit? */
Paul Mackerras371fefd2011-06-29 00:23:08 +0000620 bge secondary_too_late /* if so we're too late to the party */
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100621 or r3, r3, r7
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100622 stwcx. r3, 0, r8
Paul Mackerras371fefd2011-06-29 00:23:08 +0000623 bne 21b
624
625 /* Primary thread switches to guest partition. */
Paul Mackerras371fefd2011-06-29 00:23:08 +0000626 cmpwi r6,0
Paul Mackerras6af27c82015-03-28 14:21:10 +1100627 bne 10f
Paul Mackerrasde56a942011-06-29 00:21:34 +0000628 lwz r7,KVM_LPID(r9)
Paul Mackerras7a840842016-11-16 22:25:20 +1100629BEGIN_FTR_SECTION
630 ld r6,KVM_SDR1(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000631 li r0,LPID_RSVD /* switch to reserved LPID */
632 mtspr SPRN_LPID,r0
633 ptesync
634 mtspr SPRN_SDR1,r6 /* switch to partition page table */
Paul Mackerras7a840842016-11-16 22:25:20 +1100635END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000636 mtspr SPRN_LPID,r7
637 isync
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000638
639 /* See if we need to flush the TLB */
640 lhz r6,PACAPACAINDEX(r13) /* test_bit(cpu, need_tlb_flush) */
Paul Mackerrasa29ebea2017-01-30 21:21:50 +1100641BEGIN_FTR_SECTION
642 /*
643 * On POWER9, individual threads can come in here, but the
644 * TLB is shared between the 4 threads in a core, hence
645 * invalidating on one thread invalidates for all.
646 * Thus we make all 4 threads use the same bit here.
647 */
648 clrrdi r6,r6,2
649END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000650 clrldi r7,r6,64-6 /* extract bit number (6 bits) */
651 srdi r6,r6,6 /* doubleword number */
652 sldi r6,r6,3 /* address offset */
653 add r6,r6,r9
654 addi r6,r6,KVM_NEED_FLUSH /* dword in kvm->arch.need_tlb_flush */
Paul Mackerrasa29ebea2017-01-30 21:21:50 +1100655 li r8,1
656 sld r8,r8,r7
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000657 ld r7,0(r6)
Paul Mackerrasa29ebea2017-01-30 21:21:50 +1100658 and. r7,r7,r8
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000659 beq 22f
Paul Mackerrasca252052014-01-08 21:25:22 +1100660 /* Flush the TLB of any entries for this LPID */
Paul Mackerrasa29ebea2017-01-30 21:21:50 +1100661 lwz r0,KVM_TLB_SETS(r9)
662 mtctr r0
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000663 li r7,0x800 /* IS field = 0b10 */
664 ptesync
Paul Mackerrasa29ebea2017-01-30 21:21:50 +1100665 li r0,0 /* RS for P9 version of tlbiel */
666 bne cr7, 29f
66728: tlbiel r7 /* On P9, rs=0, RIC=0, PRS=0, R=0 */
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000668 addi r7,r7,0x1000
669 bdnz 28b
Paul Mackerrasa29ebea2017-01-30 21:21:50 +1100670 b 30f
67129: PPC_TLBIEL(7,0,2,1,1) /* for radix, RIC=2, PRS=1, R=1 */
672 addi r7,r7,0x1000
673 bdnz 29b
67430: ptesync
67523: ldarx r7,0,r6 /* clear the bit after TLB flushed */
676 andc r7,r7,r8
677 stdcx. r7,0,r6
678 bne 23b
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000679
Paul Mackerras93b0f4d2013-09-06 13:17:46 +1000680 /* Add timebase offset onto timebase */
68122: ld r8,VCORE_TB_OFFSET(r5)
682 cmpdi r8,0
683 beq 37f
684 mftb r6 /* current host timebase */
685 add r8,r8,r6
686 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
687 mftb r7 /* check if lower 24 bits overflowed */
688 clrldi r6,r6,40
689 clrldi r7,r7,40
690 cmpld r7,r6
691 bge 37f
692 addis r8,r8,0x100 /* if so, increment upper 40 bits */
693 mtspr SPRN_TBU40,r8
694
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000695 /* Load guest PCR value to select appropriate compat mode */
69637: ld r7, VCORE_PCR(r5)
697 cmpdi r7, 0
698 beq 38f
699 mtspr SPRN_PCR, r7
70038:
Michael Neulingb005255e2014-01-08 21:25:21 +1100701
702BEGIN_FTR_SECTION
Paul Mackerras88b02cf92016-09-15 13:42:52 +1000703 /* DPDES and VTB are shared between threads */
Michael Neulingb005255e2014-01-08 21:25:21 +1100704 ld r8, VCORE_DPDES(r5)
Paul Mackerras88b02cf92016-09-15 13:42:52 +1000705 ld r7, VCORE_VTB(r5)
Michael Neulingb005255e2014-01-08 21:25:21 +1100706 mtspr SPRN_DPDES, r8
Paul Mackerras88b02cf92016-09-15 13:42:52 +1000707 mtspr SPRN_VTB, r7
Michael Neulingb005255e2014-01-08 21:25:21 +1100708END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
709
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +0530710 /* Mark the subcore state as inside guest */
711 bl kvmppc_subcore_enter_guest
712 nop
713 ld r5, HSTATE_KVM_VCORE(r13)
714 ld r4, HSTATE_KVM_VCPU(r13)
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000715 li r0,1
Paul Mackerras371fefd2011-06-29 00:23:08 +0000716 stb r0,VCORE_IN_GUEST(r5) /* signal secondaries to continue */
Paul Mackerras9e368f22011-06-29 00:40:08 +0000717
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100718 /* Do we have a guest vcpu to run? */
Paul Mackerras6af27c82015-03-28 14:21:10 +110071910: cmpdi r4, 0
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100720 beq kvmppc_primary_no_guest
721kvmppc_got_guest:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000722
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100723 /* Load up guest SLB entries (N.B. slb_max will be 0 for radix) */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100724 lwz r5,VCPU_SLB_MAX(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000725 cmpwi r5,0
726 beq 9f
727 mtctr r5
728 addi r6,r4,VCPU_SLB
7291: ld r8,VCPU_SLB_E(r6)
730 ld r9,VCPU_SLB_V(r6)
731 slbmte r9,r8
732 addi r6,r6,VCPU_SLB_SIZE
733 bdnz 1b
7349:
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100735 /* Increment yield count if they have a VPA */
736 ld r3, VCPU_VPA(r4)
737 cmpdi r3, 0
738 beq 25f
Alexander Graf0865a582014-06-11 10:36:17 +0200739 li r6, LPPACA_YIELDCOUNT
740 LWZX_BE r5, r3, r6
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100741 addi r5, r5, 1
Alexander Graf0865a582014-06-11 10:36:17 +0200742 STWX_BE r5, r3, r6
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100743 li r6, 1
744 stb r6, VCPU_VPA_DIRTY(r4)
74525:
746
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100747 /* Save purr/spurr */
748 mfspr r5,SPRN_PURR
749 mfspr r6,SPRN_SPURR
750 std r5,HSTATE_PURR(r13)
751 std r6,HSTATE_SPURR(r13)
752 ld r7,VCPU_PURR(r4)
753 ld r8,VCPU_SPURR(r4)
754 mtspr SPRN_PURR,r7
755 mtspr SPRN_SPURR,r8
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100756
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100757 /* Save host values of some registers */
758BEGIN_FTR_SECTION
759 mfspr r5, SPRN_TIDR
760 mfspr r6, SPRN_PSSCR
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100761 mfspr r7, SPRN_PID
Paul Mackerras4c3bb4c2017-06-15 15:43:17 +1000762 mfspr r8, SPRN_IAMR
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100763 std r5, STACK_SLOT_TID(r1)
764 std r6, STACK_SLOT_PSSCR(r1)
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100765 std r7, STACK_SLOT_PID(r1)
Paul Mackerras4c3bb4c2017-06-15 15:43:17 +1000766 std r8, STACK_SLOT_IAMR(r1)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100767END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
768
Michael Neulingeee7ff92014-01-08 21:25:19 +1100769BEGIN_FTR_SECTION
Paul Mackerrasde56a942011-06-29 00:21:34 +0000770 /* Set partition DABR */
771 /* Do this before re-enabling PMU to avoid P7 DABR corruption bug */
Paul Mackerras8563bf52014-01-08 21:25:29 +1100772 lwz r5,VCPU_DABRX(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000773 ld r6,VCPU_DABR(r4)
774 mtspr SPRN_DABRX,r5
775 mtspr SPRN_DABR,r6
Paul Mackerrasde56a942011-06-29 00:21:34 +0000776 isync
Michael Neulingeee7ff92014-01-08 21:25:19 +1100777END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000778
Michael Neulinge4e38122014-03-25 10:47:02 +1100779#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
780BEGIN_FTR_SECTION
Paul Mackerrasf024ee02016-06-22 14:21:59 +1000781 bl kvmppc_restore_tm
782END_FTR_SECTION_IFSET(CPU_FTR_TM)
Michael Neulinge4e38122014-03-25 10:47:02 +1100783#endif
784
Paul Mackerrasde56a942011-06-29 00:21:34 +0000785 /* Load guest PMU registers */
786 /* R4 is live here (vcpu pointer) */
787 li r3, 1
788 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
789 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
790 isync
Paul Mackerras9bc01a92014-05-26 19:48:40 +1000791BEGIN_FTR_SECTION
792 ld r3, VCPU_MMCR(r4)
793 andi. r5, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO
794 cmpwi r5, MMCR0_PMAO
795 beql kvmppc_fix_pmao
796END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000797 lwz r3, VCPU_PMC(r4) /* always load up guest PMU registers */
798 lwz r5, VCPU_PMC + 4(r4) /* to prevent information leak */
799 lwz r6, VCPU_PMC + 8(r4)
800 lwz r7, VCPU_PMC + 12(r4)
801 lwz r8, VCPU_PMC + 16(r4)
802 lwz r9, VCPU_PMC + 20(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000803 mtspr SPRN_PMC1, r3
804 mtspr SPRN_PMC2, r5
805 mtspr SPRN_PMC3, r6
806 mtspr SPRN_PMC4, r7
807 mtspr SPRN_PMC5, r8
808 mtspr SPRN_PMC6, r9
Paul Mackerrasde56a942011-06-29 00:21:34 +0000809 ld r3, VCPU_MMCR(r4)
810 ld r5, VCPU_MMCR + 8(r4)
811 ld r6, VCPU_MMCR + 16(r4)
812 ld r7, VCPU_SIAR(r4)
813 ld r8, VCPU_SDAR(r4)
814 mtspr SPRN_MMCR1, r5
815 mtspr SPRN_MMCRA, r6
816 mtspr SPRN_SIAR, r7
817 mtspr SPRN_SDAR, r8
Michael Neulingb005255e2014-01-08 21:25:21 +1100818BEGIN_FTR_SECTION
819 ld r5, VCPU_MMCR + 24(r4)
820 ld r6, VCPU_SIER(r4)
Paul Mackerras83677f52016-11-16 22:33:27 +1100821 mtspr SPRN_MMCR2, r5
822 mtspr SPRN_SIER, r6
823BEGIN_FTR_SECTION_NESTED(96)
Michael Neulingb005255e2014-01-08 21:25:21 +1100824 lwz r7, VCPU_PMC + 24(r4)
825 lwz r8, VCPU_PMC + 28(r4)
826 ld r9, VCPU_MMCR + 32(r4)
Michael Neulingb005255e2014-01-08 21:25:21 +1100827 mtspr SPRN_SPMC1, r7
828 mtspr SPRN_SPMC2, r8
829 mtspr SPRN_MMCRS, r9
Paul Mackerras83677f52016-11-16 22:33:27 +1100830END_FTR_SECTION_NESTED(CPU_FTR_ARCH_300, 0, 96)
Michael Neulingb005255e2014-01-08 21:25:21 +1100831END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000832 mtspr SPRN_MMCR0, r3
833 isync
834
835 /* Load up FP, VMX and VSX registers */
836 bl kvmppc_load_fp
837
838 ld r14, VCPU_GPR(R14)(r4)
839 ld r15, VCPU_GPR(R15)(r4)
840 ld r16, VCPU_GPR(R16)(r4)
841 ld r17, VCPU_GPR(R17)(r4)
842 ld r18, VCPU_GPR(R18)(r4)
843 ld r19, VCPU_GPR(R19)(r4)
844 ld r20, VCPU_GPR(R20)(r4)
845 ld r21, VCPU_GPR(R21)(r4)
846 ld r22, VCPU_GPR(R22)(r4)
847 ld r23, VCPU_GPR(R23)(r4)
848 ld r24, VCPU_GPR(R24)(r4)
849 ld r25, VCPU_GPR(R25)(r4)
850 ld r26, VCPU_GPR(R26)(r4)
851 ld r27, VCPU_GPR(R27)(r4)
852 ld r28, VCPU_GPR(R28)(r4)
853 ld r29, VCPU_GPR(R29)(r4)
854 ld r30, VCPU_GPR(R30)(r4)
855 ld r31, VCPU_GPR(R31)(r4)
856
Paul Mackerrasde56a942011-06-29 00:21:34 +0000857 /* Switch DSCR to guest value */
858 ld r5, VCPU_DSCR(r4)
859 mtspr SPRN_DSCR, r5
Paul Mackerrasde56a942011-06-29 00:21:34 +0000860
Michael Neulingb005255e2014-01-08 21:25:21 +1100861BEGIN_FTR_SECTION
Paul Mackerrasc17b98c2014-12-03 13:30:38 +1100862 /* Skip next section on POWER7 */
Michael Neulingb005255e2014-01-08 21:25:21 +1100863 b 8f
864END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
Michael Neulingb005255e2014-01-08 21:25:21 +1100865 /* Load up POWER8-specific registers */
866 ld r5, VCPU_IAMR(r4)
867 lwz r6, VCPU_PSPB(r4)
868 ld r7, VCPU_FSCR(r4)
869 mtspr SPRN_IAMR, r5
870 mtspr SPRN_PSPB, r6
871 mtspr SPRN_FSCR, r7
872 ld r5, VCPU_DAWR(r4)
873 ld r6, VCPU_DAWRX(r4)
874 ld r7, VCPU_CIABR(r4)
875 ld r8, VCPU_TAR(r4)
876 mtspr SPRN_DAWR, r5
877 mtspr SPRN_DAWRX, r6
878 mtspr SPRN_CIABR, r7
879 mtspr SPRN_TAR, r8
880 ld r5, VCPU_IC(r4)
Michael Neuling7b490412014-01-08 21:25:32 +1100881 ld r8, VCPU_EBBHR(r4)
Paul Mackerras88b02cf92016-09-15 13:42:52 +1000882 mtspr SPRN_IC, r5
Michael Neulingb005255e2014-01-08 21:25:21 +1100883 mtspr SPRN_EBBHR, r8
884 ld r5, VCPU_EBBRR(r4)
885 ld r6, VCPU_BESCR(r4)
Michael Neulingb005255e2014-01-08 21:25:21 +1100886 lwz r7, VCPU_GUEST_PID(r4)
887 ld r8, VCPU_WORT(r4)
Paul Mackerras83677f52016-11-16 22:33:27 +1100888 mtspr SPRN_EBBRR, r5
889 mtspr SPRN_BESCR, r6
Michael Neulingb005255e2014-01-08 21:25:21 +1100890 mtspr SPRN_PID, r7
891 mtspr SPRN_WORT, r8
Paul Mackerras83677f52016-11-16 22:33:27 +1100892BEGIN_FTR_SECTION
Paul Mackerrasf11f6f72017-01-30 21:21:52 +1100893 PPC_INVALIDATE_ERAT
894END_FTR_SECTION_IFSET(CPU_FTR_POWER9_DD1)
895BEGIN_FTR_SECTION
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100896 /* POWER8-only registers */
Paul Mackerras83677f52016-11-16 22:33:27 +1100897 ld r5, VCPU_TCSCR(r4)
898 ld r6, VCPU_ACOP(r4)
899 ld r7, VCPU_CSIGR(r4)
900 ld r8, VCPU_TACR(r4)
901 mtspr SPRN_TCSCR, r5
902 mtspr SPRN_ACOP, r6
903 mtspr SPRN_CSIGR, r7
904 mtspr SPRN_TACR, r8
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100905FTR_SECTION_ELSE
906 /* POWER9-only registers */
907 ld r5, VCPU_TID(r4)
908 ld r6, VCPU_PSSCR(r4)
909 oris r6, r6, PSSCR_EC@h /* This makes stop trap to HV */
910 mtspr SPRN_TIDR, r5
911 mtspr SPRN_PSSCR, r6
912ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
Michael Neulingb005255e2014-01-08 21:25:21 +11009138:
914
Paul Mackerrasde56a942011-06-29 00:21:34 +0000915 /*
916 * Set the decrementer to the guest decrementer.
917 */
918 ld r8,VCPU_DEC_EXPIRES(r4)
Paul Mackerrasc5fb80d2014-03-25 10:47:07 +1100919 /* r8 is a host timebase value here, convert to guest TB */
920 ld r5,HSTATE_KVM_VCORE(r13)
921 ld r6,VCORE_TB_OFFSET(r5)
922 add r8,r8,r6
Paul Mackerrasde56a942011-06-29 00:21:34 +0000923 mftb r7
924 subf r3,r7,r8
925 mtspr SPRN_DEC,r3
926 stw r3,VCPU_DEC(r4)
927
928 ld r5, VCPU_SPRG0(r4)
929 ld r6, VCPU_SPRG1(r4)
930 ld r7, VCPU_SPRG2(r4)
931 ld r8, VCPU_SPRG3(r4)
932 mtspr SPRN_SPRG0, r5
933 mtspr SPRN_SPRG1, r6
934 mtspr SPRN_SPRG2, r7
935 mtspr SPRN_SPRG3, r8
936
Paul Mackerrasde56a942011-06-29 00:21:34 +0000937 /* Load up DAR and DSISR */
938 ld r5, VCPU_DAR(r4)
939 lwz r6, VCPU_DSISR(r4)
940 mtspr SPRN_DAR, r5
941 mtspr SPRN_DSISR, r6
942
Paul Mackerrasde56a942011-06-29 00:21:34 +0000943 /* Restore AMR and UAMOR, set AMOR to all 1s */
944 ld r5,VCPU_AMR(r4)
945 ld r6,VCPU_UAMOR(r4)
946 li r7,-1
947 mtspr SPRN_AMR,r5
948 mtspr SPRN_UAMOR,r6
949 mtspr SPRN_AMOR,r7
Paul Mackerrasde56a942011-06-29 00:21:34 +0000950
951 /* Restore state of CTRL run bit; assume 1 on entry */
952 lwz r5,VCPU_CTRL(r4)
953 andi. r5,r5,1
954 bne 4f
955 mfspr r6,SPRN_CTRLF
956 clrrdi r6,r6,1
957 mtspr SPRN_CTRLT,r6
9584:
Paul Mackerras6af27c82015-03-28 14:21:10 +1100959 /* Secondary threads wait for primary to have done partition switch */
960 ld r5, HSTATE_KVM_VCORE(r13)
961 lbz r6, HSTATE_PTID(r13)
962 cmpwi r6, 0
963 beq 21f
964 lbz r0, VCORE_IN_GUEST(r5)
965 cmpwi r0, 0
966 bne 21f
967 HMT_LOW
Paul Mackerrasb4deba52015-07-02 20:38:16 +100096820: lwz r3, VCORE_ENTRY_EXIT(r5)
969 cmpwi r3, 0x100
970 bge no_switch_exit
971 lbz r0, VCORE_IN_GUEST(r5)
Paul Mackerras6af27c82015-03-28 14:21:10 +1100972 cmpwi r0, 0
973 beq 20b
974 HMT_MEDIUM
97521:
976 /* Set LPCR. */
977 ld r8,VCORE_LPCR(r5)
978 mtspr SPRN_LPCR,r8
979 isync
980
981 /* Check if HDEC expires soon */
982 mfspr r3, SPRN_HDEC
Paul Mackerras2f272462017-05-22 16:25:14 +1000983 EXTEND_HDEC(r3)
984 cmpdi r3, 512 /* 1 microsecond */
Paul Mackerras6af27c82015-03-28 14:21:10 +1100985 blt hdec_soon
986
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000987#ifdef CONFIG_KVM_XICS
988 /* We are entering the guest on that thread, push VCPU to XIVE */
989 ld r10, HSTATE_XIVE_TIMA_PHYS(r13)
990 cmpldi cr0, r10, r0
991 beq no_xive
992 ld r11, VCPU_XIVE_SAVED_STATE(r4)
993 li r9, TM_QW1_OS
994 stdcix r11,r9,r10
995 eieio
996 lwz r11, VCPU_XIVE_CAM_WORD(r4)
997 li r9, TM_QW1_OS + TM_WORD2
998 stwcix r11,r9,r10
999 li r9, 1
1000 stw r9, VCPU_XIVE_PUSHED(r4)
1001no_xive:
1002#endif /* CONFIG_KVM_XICS */
1003
Suresh Warrier37f55d32016-08-19 15:35:46 +10001004deliver_guest_interrupt:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001005 ld r6, VCPU_CTR(r4)
Sam bobroffc63517c2015-05-27 09:56:57 +10001006 ld r7, VCPU_XER(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001007
1008 mtctr r6
1009 mtxer r7
1010
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001011kvmppc_cede_reentry: /* r4 = vcpu, r13 = paca */
Paul Mackerras4619ac82013-04-17 20:31:41 +00001012 ld r10, VCPU_PC(r4)
1013 ld r11, VCPU_MSR(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001014 ld r6, VCPU_SRR0(r4)
1015 ld r7, VCPU_SRR1(r4)
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001016 mtspr SPRN_SRR0, r6
1017 mtspr SPRN_SRR1, r7
Paul Mackerrasde56a942011-06-29 00:21:34 +00001018
Paul Mackerras4619ac82013-04-17 20:31:41 +00001019 /* r11 = vcpu->arch.msr & ~MSR_HV */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001020 rldicl r11, r11, 63 - MSR_HV_LG, 1
1021 rotldi r11, r11, 1 + MSR_HV_LG
1022 ori r11, r11, MSR_ME
1023
Paul Mackerras19ccb762011-07-23 17:42:46 +10001024 /* Check if we can deliver an external or decrementer interrupt now */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001025 ld r0, VCPU_PENDING_EXC(r4)
1026 rldicl r0, r0, 64 - BOOK3S_IRQPRIO_EXTERNAL_LEVEL, 63
1027 cmpdi cr1, r0, 0
1028 andi. r8, r11, MSR_EE
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001029 mfspr r8, SPRN_LPCR
1030 /* Insert EXTERNAL_LEVEL bit into LPCR at the MER bit position */
1031 rldimi r8, r0, LPCR_MER_SH, 63 - LPCR_MER_SH
1032 mtspr SPRN_LPCR, r8
Paul Mackerras19ccb762011-07-23 17:42:46 +10001033 isync
Paul Mackerras19ccb762011-07-23 17:42:46 +10001034 beq 5f
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001035 li r0, BOOK3S_INTERRUPT_EXTERNAL
1036 bne cr1, 12f
1037 mfspr r0, SPRN_DEC
1038 cmpwi r0, 0
1039 li r0, BOOK3S_INTERRUPT_DECREMENTER
1040 bge 5f
1041
104212: mtspr SPRN_SRR0, r10
Paul Mackerras19ccb762011-07-23 17:42:46 +10001043 mr r10,r0
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001044 mtspr SPRN_SRR1, r11
Michael Neulinge4e38122014-03-25 10:47:02 +11001045 mr r9, r4
1046 bl kvmppc_msr_interrupt
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +110010475:
Paul Mackerras19ccb762011-07-23 17:42:46 +10001048
Liu Ping Fan27025a62013-11-19 14:12:48 +08001049/*
1050 * Required state:
1051 * R4 = vcpu
1052 * R10: value for HSRR0
1053 * R11: value for HSRR1
1054 * R13 = PACA
1055 */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001056fast_guest_return:
Paul Mackerras4619ac82013-04-17 20:31:41 +00001057 li r0,0
1058 stb r0,VCPU_CEDED(r4) /* cancel cede */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001059 mtspr SPRN_HSRR0,r10
1060 mtspr SPRN_HSRR1,r11
1061
1062 /* Activate guest mode, so faults get handled by KVM */
Paul Mackerras44a3add2013-10-04 21:45:04 +10001063 li r9, KVM_GUEST_MODE_GUEST_HV
Paul Mackerrasde56a942011-06-29 00:21:34 +00001064 stb r9, HSTATE_IN_GUEST(r13)
1065
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001066#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1067 /* Accumulate timing */
1068 addi r3, r4, VCPU_TB_GUEST
1069 bl kvmhv_accumulate_time
1070#endif
1071
Paul Mackerrasde56a942011-06-29 00:21:34 +00001072 /* Enter guest */
1073
Paul Mackerras0acb9112013-02-04 18:10:51 +00001074BEGIN_FTR_SECTION
1075 ld r5, VCPU_CFAR(r4)
1076 mtspr SPRN_CFAR, r5
1077END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001078BEGIN_FTR_SECTION
1079 ld r0, VCPU_PPR(r4)
1080END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
Paul Mackerras0acb9112013-02-04 18:10:51 +00001081
Paul Mackerrasde56a942011-06-29 00:21:34 +00001082 ld r5, VCPU_LR(r4)
1083 lwz r6, VCPU_CR(r4)
1084 mtlr r5
1085 mtcr r6
1086
Michael Neulingc75df6f2012-06-25 13:33:10 +00001087 ld r1, VCPU_GPR(R1)(r4)
1088 ld r2, VCPU_GPR(R2)(r4)
1089 ld r3, VCPU_GPR(R3)(r4)
1090 ld r5, VCPU_GPR(R5)(r4)
1091 ld r6, VCPU_GPR(R6)(r4)
1092 ld r7, VCPU_GPR(R7)(r4)
1093 ld r8, VCPU_GPR(R8)(r4)
1094 ld r9, VCPU_GPR(R9)(r4)
1095 ld r10, VCPU_GPR(R10)(r4)
1096 ld r11, VCPU_GPR(R11)(r4)
1097 ld r12, VCPU_GPR(R12)(r4)
1098 ld r13, VCPU_GPR(R13)(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001099
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001100BEGIN_FTR_SECTION
1101 mtspr SPRN_PPR, r0
1102END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
1103 ld r0, VCPU_GPR(R0)(r4)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001104 ld r4, VCPU_GPR(R4)(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001105
1106 hrfid
1107 b .
1108
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001109secondary_too_late:
Paul Mackerras6af27c82015-03-28 14:21:10 +11001110 li r12, 0
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001111 cmpdi r4, 0
1112 beq 11f
Paul Mackerras6af27c82015-03-28 14:21:10 +11001113 stw r12, VCPU_TRAP(r4)
1114#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001115 addi r3, r4, VCPU_TB_RMEXIT
1116 bl kvmhv_accumulate_time
Paul Mackerras6af27c82015-03-28 14:21:10 +11001117#endif
Paul Mackerrasb6c295d2015-03-28 14:21:02 +1100111811: b kvmhv_switch_to_host
1119
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001120no_switch_exit:
1121 HMT_MEDIUM
1122 li r12, 0
1123 b 12f
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001124hdec_soon:
Paul Mackerras6af27c82015-03-28 14:21:10 +11001125 li r12, BOOK3S_INTERRUPT_HV_DECREMENTER
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000112612: stw r12, VCPU_TRAP(r4)
Paul Mackerras6af27c82015-03-28 14:21:10 +11001127 mr r9, r4
1128#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001129 addi r3, r4, VCPU_TB_RMEXIT
1130 bl kvmhv_accumulate_time
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001131#endif
Paul Mackerras6af27c82015-03-28 14:21:10 +11001132 b guest_exit_cont
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001133
Paul Mackerrasde56a942011-06-29 00:21:34 +00001134/******************************************************************************
1135 * *
1136 * Exit code *
1137 * *
1138 *****************************************************************************/
1139
1140/*
1141 * We come here from the first-level interrupt handlers.
1142 */
Aneesh Kumar K.Vdd96b2c2013-10-07 22:17:55 +05301143 .globl kvmppc_interrupt_hv
1144kvmppc_interrupt_hv:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001145 /*
1146 * Register contents:
Nicholas Piggind3918e72016-12-22 04:29:25 +10001147 * R12 = (guest CR << 32) | interrupt vector
Paul Mackerrasde56a942011-06-29 00:21:34 +00001148 * R13 = PACA
Nicholas Piggind3918e72016-12-22 04:29:25 +10001149 * guest R12 saved in shadow VCPU SCRATCH0
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001150 * guest CTR saved in shadow VCPU SCRATCH1 if RELOCATABLE
Paul Mackerrasde56a942011-06-29 00:21:34 +00001151 * guest R13 saved in SPRN_SCRATCH0
1152 */
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001153 std r9, HSTATE_SCRATCH2(r13)
Paul Mackerras44a3add2013-10-04 21:45:04 +10001154 lbz r9, HSTATE_IN_GUEST(r13)
1155 cmpwi r9, KVM_GUEST_MODE_HOST_HV
1156 beq kvmppc_bad_host_intr
Aneesh Kumar K.Vdd96b2c2013-10-07 22:17:55 +05301157#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1158 cmpwi r9, KVM_GUEST_MODE_GUEST
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001159 ld r9, HSTATE_SCRATCH2(r13)
Aneesh Kumar K.Vdd96b2c2013-10-07 22:17:55 +05301160 beq kvmppc_interrupt_pr
1161#endif
Paul Mackerras44a3add2013-10-04 21:45:04 +10001162 /* We're now back in the host but in guest MMU context */
1163 li r9, KVM_GUEST_MODE_HOST_HV
1164 stb r9, HSTATE_IN_GUEST(r13)
1165
Paul Mackerrasde56a942011-06-29 00:21:34 +00001166 ld r9, HSTATE_KVM_VCPU(r13)
1167
1168 /* Save registers */
1169
Michael Neulingc75df6f2012-06-25 13:33:10 +00001170 std r0, VCPU_GPR(R0)(r9)
1171 std r1, VCPU_GPR(R1)(r9)
1172 std r2, VCPU_GPR(R2)(r9)
1173 std r3, VCPU_GPR(R3)(r9)
1174 std r4, VCPU_GPR(R4)(r9)
1175 std r5, VCPU_GPR(R5)(r9)
1176 std r6, VCPU_GPR(R6)(r9)
1177 std r7, VCPU_GPR(R7)(r9)
1178 std r8, VCPU_GPR(R8)(r9)
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001179 ld r0, HSTATE_SCRATCH2(r13)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001180 std r0, VCPU_GPR(R9)(r9)
1181 std r10, VCPU_GPR(R10)(r9)
1182 std r11, VCPU_GPR(R11)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001183 ld r3, HSTATE_SCRATCH0(r13)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001184 std r3, VCPU_GPR(R12)(r9)
Nicholas Piggind3918e72016-12-22 04:29:25 +10001185 /* CR is in the high half of r12 */
1186 srdi r4, r12, 32
Paul Mackerrasde56a942011-06-29 00:21:34 +00001187 stw r4, VCPU_CR(r9)
Paul Mackerras0acb9112013-02-04 18:10:51 +00001188BEGIN_FTR_SECTION
1189 ld r3, HSTATE_CFAR(r13)
1190 std r3, VCPU_CFAR(r9)
1191END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001192BEGIN_FTR_SECTION
1193 ld r4, HSTATE_PPR(r13)
1194 std r4, VCPU_PPR(r9)
1195END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001196
1197 /* Restore R1/R2 so we can handle faults */
1198 ld r1, HSTATE_HOST_R1(r13)
1199 ld r2, PACATOC(r13)
1200
1201 mfspr r10, SPRN_SRR0
1202 mfspr r11, SPRN_SRR1
1203 std r10, VCPU_SRR0(r9)
1204 std r11, VCPU_SRR1(r9)
Nicholas Piggind3918e72016-12-22 04:29:25 +10001205 /* trap is in the low half of r12, clear CR from the high half */
1206 clrldi r12, r12, 32
Paul Mackerrasde56a942011-06-29 00:21:34 +00001207 andi. r0, r12, 2 /* need to read HSRR0/1? */
1208 beq 1f
1209 mfspr r10, SPRN_HSRR0
1210 mfspr r11, SPRN_HSRR1
1211 clrrdi r12, r12, 2
12121: std r10, VCPU_PC(r9)
1213 std r11, VCPU_MSR(r9)
1214
1215 GET_SCRATCH0(r3)
1216 mflr r4
Michael Neulingc75df6f2012-06-25 13:33:10 +00001217 std r3, VCPU_GPR(R13)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001218 std r4, VCPU_LR(r9)
1219
Paul Mackerrasde56a942011-06-29 00:21:34 +00001220 stw r12,VCPU_TRAP(r9)
1221
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001222#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1223 addi r3, r9, VCPU_TB_RMINTR
1224 mr r4, r9
1225 bl kvmhv_accumulate_time
1226 ld r5, VCPU_GPR(R5)(r9)
1227 ld r6, VCPU_GPR(R6)(r9)
1228 ld r7, VCPU_GPR(R7)(r9)
1229 ld r8, VCPU_GPR(R8)(r9)
1230#endif
1231
Paul Mackerras4a157d62014-12-03 13:30:39 +11001232 /* Save HEIR (HV emulation assist reg) in emul_inst
Paul Mackerras697d3892011-12-12 12:36:37 +00001233 if this is an HEI (HV emulation interrupt, e40) */
1234 li r3,KVM_INST_FETCH_FAILED
Paul Mackerras2bf27602015-03-20 20:39:40 +11001235 stw r3,VCPU_LAST_INST(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001236 cmpwi r12,BOOK3S_INTERRUPT_H_EMUL_ASSIST
1237 bne 11f
1238 mfspr r3,SPRN_HEIR
Paul Mackerras4a157d62014-12-03 13:30:39 +1100123911: stw r3,VCPU_HEIR(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001240
1241 /* these are volatile across C function calls */
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001242#ifdef CONFIG_RELOCATABLE
1243 ld r3, HSTATE_SCRATCH1(r13)
1244 mtctr r3
1245#else
Paul Mackerras697d3892011-12-12 12:36:37 +00001246 mfctr r3
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001247#endif
Paul Mackerras697d3892011-12-12 12:36:37 +00001248 mfxer r4
1249 std r3, VCPU_CTR(r9)
Sam bobroffc63517c2015-05-27 09:56:57 +10001250 std r4, VCPU_XER(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001251
Paul Mackerras697d3892011-12-12 12:36:37 +00001252 /* If this is a page table miss then see if it's theirs or ours */
1253 cmpwi r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1254 beq kvmppc_hdsi
Paul Mackerras342d3db2011-12-12 12:38:05 +00001255 cmpwi r12, BOOK3S_INTERRUPT_H_INST_STORAGE
1256 beq kvmppc_hisi
Paul Mackerras697d3892011-12-12 12:36:37 +00001257
Paul Mackerrasde56a942011-06-29 00:21:34 +00001258 /* See if this is a leftover HDEC interrupt */
1259 cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER
1260 bne 2f
1261 mfspr r3,SPRN_HDEC
1262 cmpwi r3,0
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001263 mr r4,r9
1264 bge fast_guest_return
Paul Mackerrasde56a942011-06-29 00:21:34 +000012652:
Paul Mackerras697d3892011-12-12 12:36:37 +00001266 /* See if this is an hcall we can handle in real mode */
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001267 cmpwi r12,BOOK3S_INTERRUPT_SYSCALL
1268 beq hcall_try_real_mode
Paul Mackerrasde56a942011-06-29 00:21:34 +00001269
Paul Mackerras66feed62015-03-28 14:21:12 +11001270 /* Hypervisor doorbell - exit only if host IPI flag set */
1271 cmpwi r12, BOOK3S_INTERRUPT_H_DOORBELL
1272 bne 3f
1273 lbz r0, HSTATE_HOST_IPI(r13)
Gautham R. Shenoy06554d92015-08-07 17:41:20 +05301274 cmpwi r0, 0
Paul Mackerras66feed62015-03-28 14:21:12 +11001275 beq 4f
1276 b guest_exit_cont
12773:
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +00001278 /* External interrupt ? */
1279 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001280 bne+ guest_exit_cont
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +00001281
1282 /* External interrupt, first check for host_ipi. If this is
1283 * set, we know the host wants us out so let's do it now
1284 */
Paul Mackerrasc9342432013-09-06 13:24:13 +10001285 bl kvmppc_read_intr
Suresh Warrier37f55d32016-08-19 15:35:46 +10001286
1287 /*
1288 * Restore the active volatile registers after returning from
1289 * a C function.
1290 */
1291 ld r9, HSTATE_KVM_VCPU(r13)
1292 li r12, BOOK3S_INTERRUPT_EXTERNAL
1293
1294 /*
1295 * kvmppc_read_intr return codes:
1296 *
1297 * Exit to host (r3 > 0)
1298 * 1 An interrupt is pending that needs to be handled by the host
1299 * Exit guest and return to host by branching to guest_exit_cont
1300 *
Suresh Warrierf7af5202016-08-19 15:35:52 +10001301 * 2 Passthrough that needs completion in the host
1302 * Exit guest and return to host by branching to guest_exit_cont
1303 * However, we also set r12 to BOOK3S_INTERRUPT_HV_RM_HARD
1304 * to indicate to the host to complete handling the interrupt
1305 *
Suresh Warrier37f55d32016-08-19 15:35:46 +10001306 * Before returning to guest, we check if any CPU is heading out
1307 * to the host and if so, we head out also. If no CPUs are heading
1308 * check return values <= 0.
1309 *
1310 * Return to guest (r3 <= 0)
1311 * 0 No external interrupt is pending
1312 * -1 A guest wakeup IPI (which has now been cleared)
1313 * In either case, we return to guest to deliver any pending
1314 * guest interrupts.
Suresh Warriere3c13e52016-08-19 15:35:51 +10001315 *
1316 * -2 A PCI passthrough external interrupt was handled
1317 * (interrupt was delivered directly to guest)
1318 * Return to guest to deliver any pending guest interrupts.
Suresh Warrier37f55d32016-08-19 15:35:46 +10001319 */
1320
Suresh Warrierf7af5202016-08-19 15:35:52 +10001321 cmpdi r3, 1
1322 ble 1f
1323
1324 /* Return code = 2 */
1325 li r12, BOOK3S_INTERRUPT_HV_RM_HARD
1326 stw r12, VCPU_TRAP(r9)
1327 b guest_exit_cont
1328
13291: /* Return code <= 1 */
Paul Mackerrasc9342432013-09-06 13:24:13 +10001330 cmpdi r3, 0
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001331 bgt guest_exit_cont
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +00001332
Suresh Warrier37f55d32016-08-19 15:35:46 +10001333 /* Return code <= 0 */
Paul Mackerras66feed62015-03-28 14:21:12 +110013344: ld r5, HSTATE_KVM_VCORE(r13)
Paul Mackerras4619ac82013-04-17 20:31:41 +00001335 lwz r0, VCORE_ENTRY_EXIT(r5)
1336 cmpwi r0, 0x100
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001337 mr r4, r9
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001338 blt deliver_guest_interrupt
Paul Mackerrasde56a942011-06-29 00:21:34 +00001339
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001340guest_exit_cont: /* r9 = vcpu, r12 = trap, r13 = paca */
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001341#ifdef CONFIG_KVM_XICS
1342 /* We are exiting, pull the VP from the XIVE */
1343 lwz r0, VCPU_XIVE_PUSHED(r9)
1344 cmpwi cr0, r0, 0
1345 beq 1f
1346 li r7, TM_SPC_PULL_OS_CTX
1347 li r6, TM_QW1_OS
1348 mfmsr r0
1349 andi. r0, r0, MSR_IR /* in real mode? */
1350 beq 2f
1351 ld r10, HSTATE_XIVE_TIMA_VIRT(r13)
1352 cmpldi cr0, r10, 0
1353 beq 1f
1354 /* First load to pull the context, we ignore the value */
1355 lwzx r11, r7, r10
1356 eieio
1357 /* Second load to recover the context state (Words 0 and 1) */
1358 ldx r11, r6, r10
1359 b 3f
13602: ld r10, HSTATE_XIVE_TIMA_PHYS(r13)
1361 cmpldi cr0, r10, 0
1362 beq 1f
1363 /* First load to pull the context, we ignore the value */
1364 lwzcix r11, r7, r10
1365 eieio
1366 /* Second load to recover the context state (Words 0 and 1) */
1367 ldcix r11, r6, r10
13683: std r11, VCPU_XIVE_SAVED_STATE(r9)
1369 /* Fixup some of the state for the next load */
1370 li r10, 0
1371 li r0, 0xff
1372 stw r10, VCPU_XIVE_PUSHED(r9)
1373 stb r10, (VCPU_XIVE_SAVED_STATE+3)(r9)
1374 stb r0, (VCPU_XIVE_SAVED_STATE+4)(r9)
13751:
1376#endif /* CONFIG_KVM_XICS */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001377 /* Save more register state */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001378 mfdar r6
1379 mfdsisr r7
Paul Mackerrasde56a942011-06-29 00:21:34 +00001380 std r6, VCPU_DAR(r9)
1381 stw r7, VCPU_DSISR(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001382 /* don't overwrite fault_dar/fault_dsisr if HDSI */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001383 cmpwi r12,BOOK3S_INTERRUPT_H_DATA_STORAGE
Paul Mackerras6af27c82015-03-28 14:21:10 +11001384 beq mc_cont
Paul Mackerras697d3892011-12-12 12:36:37 +00001385 std r6, VCPU_FAULT_DAR(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001386 stw r7, VCPU_FAULT_DSISR(r9)
1387
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001388 /* See if it is a machine check */
1389 cmpwi r12, BOOK3S_INTERRUPT_MACHINE_CHECK
1390 beq machine_check_realmode
1391mc_cont:
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001392#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1393 addi r3, r9, VCPU_TB_RMEXIT
1394 mr r4, r9
1395 bl kvmhv_accumulate_time
1396#endif
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001397
Gautham R. Shenoy7e022e72015-05-21 13:57:04 +05301398 mr r3, r12
Paul Mackerras6af27c82015-03-28 14:21:10 +11001399 /* Increment exit count, poke other threads to exit */
1400 bl kvmhv_commence_exit
Paul Mackerraseddb60f2015-03-28 14:21:11 +11001401 nop
1402 ld r9, HSTATE_KVM_VCPU(r13)
1403 lwz r12, VCPU_TRAP(r9)
Paul Mackerras6af27c82015-03-28 14:21:10 +11001404
Paul Mackerrasec257162015-06-24 21:18:03 +10001405 /* Stop others sending VCPU interrupts to this physical CPU */
1406 li r0, -1
1407 stw r0, VCPU_CPU(r9)
1408 stw r0, VCPU_THREAD_CPU(r9)
1409
Paul Mackerrasde56a942011-06-29 00:21:34 +00001410 /* Save guest CTRL register, set runlatch to 1 */
Paul Mackerras6af27c82015-03-28 14:21:10 +11001411 mfspr r6,SPRN_CTRLF
Paul Mackerrasde56a942011-06-29 00:21:34 +00001412 stw r6,VCPU_CTRL(r9)
1413 andi. r0,r6,1
1414 bne 4f
1415 ori r6,r6,1
1416 mtspr SPRN_CTRLT,r6
14174:
1418 /* Read the guest SLB and save it away */
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001419 ld r5, VCPU_KVM(r9)
1420 lbz r0, KVM_RADIX(r5)
1421 cmpwi r0, 0
1422 li r5, 0
1423 bne 3f /* for radix, save 0 entries */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001424 lwz r0,VCPU_SLB_NR(r9) /* number of entries in SLB */
1425 mtctr r0
1426 li r6,0
1427 addi r7,r9,VCPU_SLB
Paul Mackerrasde56a942011-06-29 00:21:34 +000014281: slbmfee r8,r6
1429 andis. r0,r8,SLB_ESID_V@h
1430 beq 2f
1431 add r8,r8,r6 /* put index in */
1432 slbmfev r3,r6
1433 std r8,VCPU_SLB_E(r7)
1434 std r3,VCPU_SLB_V(r7)
1435 addi r7,r7,VCPU_SLB_SIZE
1436 addi r5,r5,1
14372: addi r6,r6,1
1438 bdnz 1b
Paul Mackerrasf4c51f82017-01-30 21:21:45 +110014393: stw r5,VCPU_SLB_MAX(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001440
1441 /*
1442 * Save the guest PURR/SPURR
1443 */
1444 mfspr r5,SPRN_PURR
1445 mfspr r6,SPRN_SPURR
1446 ld r7,VCPU_PURR(r9)
1447 ld r8,VCPU_SPURR(r9)
1448 std r5,VCPU_PURR(r9)
1449 std r6,VCPU_SPURR(r9)
1450 subf r5,r7,r5
1451 subf r6,r8,r6
1452
1453 /*
1454 * Restore host PURR/SPURR and add guest times
1455 * so that the time in the guest gets accounted.
1456 */
1457 ld r3,HSTATE_PURR(r13)
1458 ld r4,HSTATE_SPURR(r13)
1459 add r3,r3,r5
1460 add r4,r4,r6
1461 mtspr SPRN_PURR,r3
1462 mtspr SPRN_SPURR,r4
1463
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001464 /* Save DEC */
1465 mfspr r5,SPRN_DEC
1466 mftb r6
1467 extsw r5,r5
1468 add r5,r5,r6
Paul Mackerrasc5fb80d2014-03-25 10:47:07 +11001469 /* r5 is a guest timebase value here, convert to host TB */
1470 ld r3,HSTATE_KVM_VCORE(r13)
1471 ld r4,VCORE_TB_OFFSET(r3)
1472 subf r5,r4,r5
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001473 std r5,VCPU_DEC_EXPIRES(r9)
1474
Michael Neulingb005255e2014-01-08 21:25:21 +11001475BEGIN_FTR_SECTION
1476 b 8f
1477END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
Michael Neulingb005255e2014-01-08 21:25:21 +11001478 /* Save POWER8-specific registers */
1479 mfspr r5, SPRN_IAMR
1480 mfspr r6, SPRN_PSPB
1481 mfspr r7, SPRN_FSCR
1482 std r5, VCPU_IAMR(r9)
1483 stw r6, VCPU_PSPB(r9)
1484 std r7, VCPU_FSCR(r9)
1485 mfspr r5, SPRN_IC
Michael Neulingb005255e2014-01-08 21:25:21 +11001486 mfspr r7, SPRN_TAR
1487 std r5, VCPU_IC(r9)
Michael Neulingb005255e2014-01-08 21:25:21 +11001488 std r7, VCPU_TAR(r9)
Michael Neuling7b490412014-01-08 21:25:32 +11001489 mfspr r8, SPRN_EBBHR
Michael Neulingb005255e2014-01-08 21:25:21 +11001490 std r8, VCPU_EBBHR(r9)
1491 mfspr r5, SPRN_EBBRR
1492 mfspr r6, SPRN_BESCR
Michael Neulingb005255e2014-01-08 21:25:21 +11001493 mfspr r7, SPRN_PID
1494 mfspr r8, SPRN_WORT
Paul Mackerras83677f52016-11-16 22:33:27 +11001495 std r5, VCPU_EBBRR(r9)
1496 std r6, VCPU_BESCR(r9)
Michael Neulingb005255e2014-01-08 21:25:21 +11001497 stw r7, VCPU_GUEST_PID(r9)
1498 std r8, VCPU_WORT(r9)
Paul Mackerras83677f52016-11-16 22:33:27 +11001499BEGIN_FTR_SECTION
1500 mfspr r5, SPRN_TCSCR
1501 mfspr r6, SPRN_ACOP
1502 mfspr r7, SPRN_CSIGR
1503 mfspr r8, SPRN_TACR
1504 std r5, VCPU_TCSCR(r9)
1505 std r6, VCPU_ACOP(r9)
1506 std r7, VCPU_CSIGR(r9)
1507 std r8, VCPU_TACR(r9)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001508FTR_SECTION_ELSE
1509 mfspr r5, SPRN_TIDR
1510 mfspr r6, SPRN_PSSCR
1511 std r5, VCPU_TID(r9)
1512 rldicl r6, r6, 4, 50 /* r6 &= PSSCR_GUEST_VIS */
1513 rotldi r6, r6, 60
1514 std r6, VCPU_PSSCR(r9)
1515ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
Paul Mackerrasccec4452016-03-05 19:34:39 +11001516 /*
1517 * Restore various registers to 0, where non-zero values
1518 * set by the guest could disrupt the host.
1519 */
1520 li r0, 0
Paul Mackerrasccec4452016-03-05 19:34:39 +11001521 mtspr SPRN_CIABR, r0
1522 mtspr SPRN_DAWRX, r0
Paul Mackerras4c3bb4c2017-06-15 15:43:17 +10001523 mtspr SPRN_PSPB, r0
Paul Mackerrasccec4452016-03-05 19:34:39 +11001524 mtspr SPRN_WORT, r0
Paul Mackerras83677f52016-11-16 22:33:27 +11001525BEGIN_FTR_SECTION
Paul Mackerras4c3bb4c2017-06-15 15:43:17 +10001526 mtspr SPRN_IAMR, r0
Paul Mackerras83677f52016-11-16 22:33:27 +11001527 mtspr SPRN_TCSCR, r0
Paul Mackerrasccec4452016-03-05 19:34:39 +11001528 /* Set MMCRS to 1<<31 to freeze and disable the SPMC counters */
1529 li r0, 1
1530 sldi r0, r0, 31
1531 mtspr SPRN_MMCRS, r0
Paul Mackerras83677f52016-11-16 22:33:27 +11001532END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
Michael Neulingb005255e2014-01-08 21:25:21 +110015338:
1534
Paul Mackerrasde56a942011-06-29 00:21:34 +00001535 /* Save and reset AMR and UAMOR before turning on the MMU */
1536 mfspr r5,SPRN_AMR
1537 mfspr r6,SPRN_UAMOR
1538 std r5,VCPU_AMR(r9)
1539 std r6,VCPU_UAMOR(r9)
1540 li r6,0
1541 mtspr SPRN_AMR,r6
Paul Mackerras4c3bb4c2017-06-15 15:43:17 +10001542 mtspr SPRN_UAMOR, r6
Paul Mackerrasde56a942011-06-29 00:21:34 +00001543
Paul Mackerrasde56a942011-06-29 00:21:34 +00001544 /* Switch DSCR back to host value */
1545 mfspr r8, SPRN_DSCR
1546 ld r7, HSTATE_DSCR(r13)
Paul Mackerrascfc86022013-09-21 09:53:28 +10001547 std r8, VCPU_DSCR(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001548 mtspr SPRN_DSCR, r7
1549
1550 /* Save non-volatile GPRs */
Michael Neulingc75df6f2012-06-25 13:33:10 +00001551 std r14, VCPU_GPR(R14)(r9)
1552 std r15, VCPU_GPR(R15)(r9)
1553 std r16, VCPU_GPR(R16)(r9)
1554 std r17, VCPU_GPR(R17)(r9)
1555 std r18, VCPU_GPR(R18)(r9)
1556 std r19, VCPU_GPR(R19)(r9)
1557 std r20, VCPU_GPR(R20)(r9)
1558 std r21, VCPU_GPR(R21)(r9)
1559 std r22, VCPU_GPR(R22)(r9)
1560 std r23, VCPU_GPR(R23)(r9)
1561 std r24, VCPU_GPR(R24)(r9)
1562 std r25, VCPU_GPR(R25)(r9)
1563 std r26, VCPU_GPR(R26)(r9)
1564 std r27, VCPU_GPR(R27)(r9)
1565 std r28, VCPU_GPR(R28)(r9)
1566 std r29, VCPU_GPR(R29)(r9)
1567 std r30, VCPU_GPR(R30)(r9)
1568 std r31, VCPU_GPR(R31)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001569
1570 /* Save SPRGs */
1571 mfspr r3, SPRN_SPRG0
1572 mfspr r4, SPRN_SPRG1
1573 mfspr r5, SPRN_SPRG2
1574 mfspr r6, SPRN_SPRG3
1575 std r3, VCPU_SPRG0(r9)
1576 std r4, VCPU_SPRG1(r9)
1577 std r5, VCPU_SPRG2(r9)
1578 std r6, VCPU_SPRG3(r9)
1579
Paul Mackerras89436332012-03-02 01:38:23 +00001580 /* save FP state */
1581 mr r3, r9
Paul Mackerras595e4f72013-10-15 20:43:04 +11001582 bl kvmppc_save_fp
Paul Mackerras89436332012-03-02 01:38:23 +00001583
Paul Mackerras0a8ecce2014-04-14 08:56:26 +10001584#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1585BEGIN_FTR_SECTION
Paul Mackerrasf024ee02016-06-22 14:21:59 +10001586 bl kvmppc_save_tm
1587END_FTR_SECTION_IFSET(CPU_FTR_TM)
Paul Mackerras0a8ecce2014-04-14 08:56:26 +10001588#endif
1589
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001590 /* Increment yield count if they have a VPA */
1591 ld r8, VCPU_VPA(r9) /* do they have a VPA? */
1592 cmpdi r8, 0
1593 beq 25f
Alexander Graf0865a582014-06-11 10:36:17 +02001594 li r4, LPPACA_YIELDCOUNT
1595 LWZX_BE r3, r8, r4
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001596 addi r3, r3, 1
Alexander Graf0865a582014-06-11 10:36:17 +02001597 STWX_BE r3, r8, r4
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001598 li r3, 1
1599 stb r3, VCPU_VPA_DIRTY(r9)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000160025:
1601 /* Save PMU registers if requested */
1602 /* r8 and cr0.eq are live here */
Paul Mackerras9bc01a92014-05-26 19:48:40 +10001603BEGIN_FTR_SECTION
1604 /*
1605 * POWER8 seems to have a hardware bug where setting
1606 * MMCR0[PMAE] along with MMCR0[PMC1CE] and/or MMCR0[PMCjCE]
1607 * when some counters are already negative doesn't seem
1608 * to cause a performance monitor alert (and hence interrupt).
1609 * The effect of this is that when saving the PMU state,
1610 * if there is no PMU alert pending when we read MMCR0
1611 * before freezing the counters, but one becomes pending
1612 * before we read the counters, we lose it.
1613 * To work around this, we need a way to freeze the counters
1614 * before reading MMCR0. Normally, freezing the counters
1615 * is done by writing MMCR0 (to set MMCR0[FC]) which
1616 * unavoidably writes MMCR0[PMA0] as well. On POWER8,
1617 * we can also freeze the counters using MMCR2, by writing
1618 * 1s to all the counter freeze condition bits (there are
1619 * 9 bits each for 6 counters).
1620 */
1621 li r3, -1 /* set all freeze bits */
1622 clrrdi r3, r3, 10
1623 mfspr r10, SPRN_MMCR2
1624 mtspr SPRN_MMCR2, r3
1625 isync
1626END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001627 li r3, 1
1628 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
1629 mfspr r4, SPRN_MMCR0 /* save MMCR0 */
1630 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
Paul Mackerras89436332012-03-02 01:38:23 +00001631 mfspr r6, SPRN_MMCRA
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11001632 /* Clear MMCRA in order to disable SDAR updates */
Paul Mackerras89436332012-03-02 01:38:23 +00001633 li r7, 0
1634 mtspr SPRN_MMCRA, r7
Paul Mackerrasde56a942011-06-29 00:21:34 +00001635 isync
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001636 beq 21f /* if no VPA, save PMU stuff anyway */
1637 lbz r7, LPPACA_PMCINUSE(r8)
1638 cmpwi r7, 0 /* did they ask for PMU stuff to be saved? */
1639 bne 21f
1640 std r3, VCPU_MMCR(r9) /* if not, set saved MMCR0 to FC */
1641 b 22f
164221: mfspr r5, SPRN_MMCR1
Paul Mackerras14941782013-09-06 13:11:18 +10001643 mfspr r7, SPRN_SIAR
1644 mfspr r8, SPRN_SDAR
Paul Mackerrasde56a942011-06-29 00:21:34 +00001645 std r4, VCPU_MMCR(r9)
1646 std r5, VCPU_MMCR + 8(r9)
1647 std r6, VCPU_MMCR + 16(r9)
Paul Mackerras9bc01a92014-05-26 19:48:40 +10001648BEGIN_FTR_SECTION
1649 std r10, VCPU_MMCR + 24(r9)
1650END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerras14941782013-09-06 13:11:18 +10001651 std r7, VCPU_SIAR(r9)
1652 std r8, VCPU_SDAR(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001653 mfspr r3, SPRN_PMC1
1654 mfspr r4, SPRN_PMC2
1655 mfspr r5, SPRN_PMC3
1656 mfspr r6, SPRN_PMC4
1657 mfspr r7, SPRN_PMC5
1658 mfspr r8, SPRN_PMC6
1659 stw r3, VCPU_PMC(r9)
1660 stw r4, VCPU_PMC + 4(r9)
1661 stw r5, VCPU_PMC + 8(r9)
1662 stw r6, VCPU_PMC + 12(r9)
1663 stw r7, VCPU_PMC + 16(r9)
1664 stw r8, VCPU_PMC + 20(r9)
Paul Mackerras9e368f22011-06-29 00:40:08 +00001665BEGIN_FTR_SECTION
Michael Neulingb005255e2014-01-08 21:25:21 +11001666 mfspr r5, SPRN_SIER
Paul Mackerras83677f52016-11-16 22:33:27 +11001667 std r5, VCPU_SIER(r9)
1668BEGIN_FTR_SECTION_NESTED(96)
Michael Neulingb005255e2014-01-08 21:25:21 +11001669 mfspr r6, SPRN_SPMC1
1670 mfspr r7, SPRN_SPMC2
1671 mfspr r8, SPRN_MMCRS
Michael Neulingb005255e2014-01-08 21:25:21 +11001672 stw r6, VCPU_PMC + 24(r9)
1673 stw r7, VCPU_PMC + 28(r9)
1674 std r8, VCPU_MMCR + 32(r9)
1675 lis r4, 0x8000
1676 mtspr SPRN_MMCRS, r4
Paul Mackerras83677f52016-11-16 22:33:27 +11001677END_FTR_SECTION_NESTED(CPU_FTR_ARCH_300, 0, 96)
Michael Neulingb005255e2014-01-08 21:25:21 +11001678END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000167922:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001680 /* Clear out SLB */
1681 li r5,0
1682 slbmte r5,r5
1683 slbia
1684 ptesync
1685
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001686 /* Restore host values of some registers */
1687BEGIN_FTR_SECTION
1688 ld r5, STACK_SLOT_TID(r1)
1689 ld r6, STACK_SLOT_PSSCR(r1)
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001690 ld r7, STACK_SLOT_PID(r1)
Paul Mackerras4c3bb4c2017-06-15 15:43:17 +10001691 ld r8, STACK_SLOT_IAMR(r1)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001692 mtspr SPRN_TIDR, r5
1693 mtspr SPRN_PSSCR, r6
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001694 mtspr SPRN_PID, r7
Paul Mackerras4c3bb4c2017-06-15 15:43:17 +10001695 mtspr SPRN_IAMR, r8
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001696END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerrasf11f6f72017-01-30 21:21:52 +11001697BEGIN_FTR_SECTION
1698 PPC_INVALIDATE_ERAT
1699END_FTR_SECTION_IFSET(CPU_FTR_POWER9_DD1)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001700
Paul Mackerrasde56a942011-06-29 00:21:34 +00001701 /*
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11001702 * POWER7/POWER8 guest -> host partition switch code.
Paul Mackerrasde56a942011-06-29 00:21:34 +00001703 * We don't have to lock against tlbies but we do
1704 * have to coordinate the hardware threads.
1705 */
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001706kvmhv_switch_to_host:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001707 /* Secondary threads wait for primary to do partition switch */
Paul Mackerras6af27c82015-03-28 14:21:10 +11001708 ld r5,HSTATE_KVM_VCORE(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001709 ld r4,VCORE_KVM(r5) /* pointer to struct kvm */
1710 lbz r3,HSTATE_PTID(r13)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001711 cmpwi r3,0
1712 beq 15f
1713 HMT_LOW
171413: lbz r3,VCORE_IN_GUEST(r5)
1715 cmpwi r3,0
1716 bne 13b
1717 HMT_MEDIUM
1718 b 16f
1719
1720 /* Primary thread waits for all the secondaries to exit guest */
172115: lwz r3,VCORE_ENTRY_EXIT(r5)
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001722 rlwinm r0,r3,32-8,0xff
Paul Mackerrasde56a942011-06-29 00:21:34 +00001723 clrldi r3,r3,56
1724 cmpw r3,r0
1725 bne 15b
1726 isync
1727
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001728 /* Did we actually switch to the guest at all? */
1729 lbz r6, VCORE_IN_GUEST(r5)
1730 cmpwi r6, 0
1731 beq 19f
1732
Paul Mackerrasde56a942011-06-29 00:21:34 +00001733 /* Primary thread switches back to host partition */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001734 lwz r7,KVM_HOST_LPID(r4)
Paul Mackerras7a840842016-11-16 22:25:20 +11001735BEGIN_FTR_SECTION
1736 ld r6,KVM_HOST_SDR1(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001737 li r8,LPID_RSVD /* switch to reserved LPID */
1738 mtspr SPRN_LPID,r8
1739 ptesync
Paul Mackerras7a840842016-11-16 22:25:20 +11001740 mtspr SPRN_SDR1,r6 /* switch to host page table */
1741END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001742 mtspr SPRN_LPID,r7
1743 isync
1744
Michael Neulingb005255e2014-01-08 21:25:21 +11001745BEGIN_FTR_SECTION
Paul Mackerras88b02cf92016-09-15 13:42:52 +10001746 /* DPDES and VTB are shared between threads */
Michael Neulingb005255e2014-01-08 21:25:21 +11001747 mfspr r7, SPRN_DPDES
Paul Mackerras88b02cf92016-09-15 13:42:52 +10001748 mfspr r8, SPRN_VTB
Michael Neulingb005255e2014-01-08 21:25:21 +11001749 std r7, VCORE_DPDES(r5)
Paul Mackerras88b02cf92016-09-15 13:42:52 +10001750 std r8, VCORE_VTB(r5)
Michael Neulingb005255e2014-01-08 21:25:21 +11001751 /* clear DPDES so we don't get guest doorbells in the host */
1752 li r8, 0
1753 mtspr SPRN_DPDES, r8
1754END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1755
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05301756 /* If HMI, call kvmppc_realmode_hmi_handler() */
1757 cmpwi r12, BOOK3S_INTERRUPT_HMI
1758 bne 27f
1759 bl kvmppc_realmode_hmi_handler
1760 nop
1761 li r12, BOOK3S_INTERRUPT_HMI
1762 /*
1763 * At this point kvmppc_realmode_hmi_handler would have resync-ed
1764 * the TB. Hence it is not required to subtract guest timebase
1765 * offset from timebase. So, skip it.
1766 *
1767 * Also, do not call kvmppc_subcore_exit_guest() because it has
1768 * been invoked as part of kvmppc_realmode_hmi_handler().
1769 */
1770 b 30f
1771
177227:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001773 /* Subtract timebase offset from timebase */
1774 ld r8,VCORE_TB_OFFSET(r5)
1775 cmpdi r8,0
1776 beq 17f
Paul Mackerrasc5fb80d2014-03-25 10:47:07 +11001777 mftb r6 /* current guest timebase */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001778 subf r8,r8,r6
1779 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
1780 mftb r7 /* check if lower 24 bits overflowed */
1781 clrldi r6,r6,40
1782 clrldi r7,r7,40
1783 cmpld r7,r6
1784 bge 17f
1785 addis r8,r8,0x100 /* if so, increment upper 40 bits */
1786 mtspr SPRN_TBU40,r8
1787
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +0530178817: bl kvmppc_subcore_exit_guest
1789 nop
179030: ld r5,HSTATE_KVM_VCORE(r13)
1791 ld r4,VCORE_KVM(r5) /* pointer to struct kvm */
1792
Paul Mackerrasde56a942011-06-29 00:21:34 +00001793 /* Reset PCR */
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05301794 ld r0, VCORE_PCR(r5)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001795 cmpdi r0, 0
1796 beq 18f
1797 li r0, 0
1798 mtspr SPRN_PCR, r0
179918:
1800 /* Signal secondary CPUs to continue */
1801 stb r0,VCORE_IN_GUEST(r5)
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000180219: lis r8,0x7fff /* MAX_INT@h */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001803 mtspr SPRN_HDEC,r8
1804
180516: ld r8,KVM_HOST_LPCR(r4)
1806 mtspr SPRN_LPCR,r8
1807 isync
Paul Mackerrasde56a942011-06-29 00:21:34 +00001808
1809 /* load host SLB entries */
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001810BEGIN_MMU_FTR_SECTION
1811 b 0f
1812END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_RADIX)
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11001813 ld r8,PACA_SLBSHADOWPTR(r13)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001814
1815 .rept SLB_NUM_BOLTED
Alexander Graf0865a582014-06-11 10:36:17 +02001816 li r3, SLBSHADOW_SAVEAREA
1817 LDX_BE r5, r8, r3
1818 addi r3, r3, 8
1819 LDX_BE r6, r8, r3
Paul Mackerrasde56a942011-06-29 00:21:34 +00001820 andis. r7,r5,SLB_ESID_V@h
1821 beq 1f
1822 slbmte r6,r5
18231: addi r8,r8,16
1824 .endr
Paul Mackerrasf4c51f82017-01-30 21:21:45 +110018250:
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001826#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1827 /* Finish timing, if we have a vcpu */
1828 ld r4, HSTATE_KVM_VCPU(r13)
1829 cmpdi r4, 0
1830 li r3, 0
1831 beq 2f
1832 bl kvmhv_accumulate_time
18332:
1834#endif
Paul Mackerrasde56a942011-06-29 00:21:34 +00001835 /* Unset guest mode */
1836 li r0, KVM_GUEST_MODE_NONE
1837 stb r0, HSTATE_IN_GUEST(r13)
1838
Paul Mackerras218309b2013-09-06 13:23:44 +10001839 ld r0, 112+PPC_LR_STKOFF(r1)
1840 addi r1, r1, 112
1841 mtlr r0
1842 blr
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001843
Paul Mackerras697d3892011-12-12 12:36:37 +00001844/*
1845 * Check whether an HDSI is an HPTE not found fault or something else.
1846 * If it is an HPTE not found fault that is due to the guest accessing
1847 * a page that they have mapped but which we have paged out, then
1848 * we continue on with the guest exit path. In all other cases,
1849 * reflect the HDSI to the guest as a DSI.
1850 */
1851kvmppc_hdsi:
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001852 ld r3, VCPU_KVM(r9)
1853 lbz r0, KVM_RADIX(r3)
1854 cmpwi r0, 0
Paul Mackerras697d3892011-12-12 12:36:37 +00001855 mfspr r4, SPRN_HDAR
1856 mfspr r6, SPRN_HDSISR
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001857 bne .Lradix_hdsi /* on radix, just save DAR/DSISR/ASDR */
Paul Mackerras4cf302b2011-12-12 12:38:51 +00001858 /* HPTE not found fault or protection fault? */
1859 andis. r0, r6, (DSISR_NOHPTE | DSISR_PROTFAULT)@h
Paul Mackerras697d3892011-12-12 12:36:37 +00001860 beq 1f /* if not, send it to the guest */
Paul Mackerras4e5acdc2017-02-28 11:05:47 +11001861 andi. r0, r11, MSR_DR /* data relocation enabled? */
1862 beq 3f
Paul Mackerrasef8c6402017-01-30 21:21:43 +11001863BEGIN_FTR_SECTION
1864 mfspr r5, SPRN_ASDR /* on POWER9, use ASDR to get VSID */
1865 b 4f
1866END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras697d3892011-12-12 12:36:37 +00001867 clrrdi r0, r4, 28
Michael Neulingc75df6f2012-06-25 13:33:10 +00001868 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
Paul Mackerrascf29b212015-10-27 16:10:20 +11001869 li r0, BOOK3S_INTERRUPT_DATA_SEGMENT
1870 bne 7f /* if no SLB entry found */
Paul Mackerras697d3892011-12-12 12:36:37 +000018714: std r4, VCPU_FAULT_DAR(r9)
1872 stw r6, VCPU_FAULT_DSISR(r9)
1873
1874 /* Search the hash table. */
1875 mr r3, r9 /* vcpu pointer */
Paul Mackerras342d3db2011-12-12 12:38:05 +00001876 li r7, 1 /* data fault */
Anton Blanchardb1576fe2014-02-04 16:04:35 +11001877 bl kvmppc_hpte_hv_fault
Paul Mackerras697d3892011-12-12 12:36:37 +00001878 ld r9, HSTATE_KVM_VCPU(r13)
1879 ld r10, VCPU_PC(r9)
1880 ld r11, VCPU_MSR(r9)
1881 li r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1882 cmpdi r3, 0 /* retry the instruction */
1883 beq 6f
1884 cmpdi r3, -1 /* handle in kernel mode */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001885 beq guest_exit_cont
Paul Mackerras697d3892011-12-12 12:36:37 +00001886 cmpdi r3, -2 /* MMIO emulation; need instr word */
1887 beq 2f
1888
Paul Mackerrascf29b212015-10-27 16:10:20 +11001889 /* Synthesize a DSI (or DSegI) for the guest */
Paul Mackerras697d3892011-12-12 12:36:37 +00001890 ld r4, VCPU_FAULT_DAR(r9)
1891 mr r6, r3
Paul Mackerrascf29b212015-10-27 16:10:20 +110018921: li r0, BOOK3S_INTERRUPT_DATA_STORAGE
Paul Mackerras697d3892011-12-12 12:36:37 +00001893 mtspr SPRN_DSISR, r6
Paul Mackerrascf29b212015-10-27 16:10:20 +110018947: mtspr SPRN_DAR, r4
Paul Mackerras697d3892011-12-12 12:36:37 +00001895 mtspr SPRN_SRR0, r10
1896 mtspr SPRN_SRR1, r11
Paul Mackerrascf29b212015-10-27 16:10:20 +11001897 mr r10, r0
Michael Neulinge4e38122014-03-25 10:47:02 +11001898 bl kvmppc_msr_interrupt
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001899fast_interrupt_c_return:
Paul Mackerras697d3892011-12-12 12:36:37 +000019006: ld r7, VCPU_CTR(r9)
Sam bobroffc63517c2015-05-27 09:56:57 +10001901 ld r8, VCPU_XER(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001902 mtctr r7
1903 mtxer r8
1904 mr r4, r9
1905 b fast_guest_return
1906
19073: ld r5, VCPU_KVM(r9) /* not relocated, use VRMA */
1908 ld r5, KVM_VRMA_SLB_V(r5)
1909 b 4b
1910
1911 /* If this is for emulated MMIO, load the instruction word */
19122: li r8, KVM_INST_FETCH_FAILED /* In case lwz faults */
1913
1914 /* Set guest mode to 'jump over instruction' so if lwz faults
1915 * we'll just continue at the next IP. */
1916 li r0, KVM_GUEST_MODE_SKIP
1917 stb r0, HSTATE_IN_GUEST(r13)
1918
1919 /* Do the access with MSR:DR enabled */
1920 mfmsr r3
1921 ori r4, r3, MSR_DR /* Enable paging for data */
1922 mtmsrd r4
1923 lwz r8, 0(r10)
1924 mtmsrd r3
1925
1926 /* Store the result */
1927 stw r8, VCPU_LAST_INST(r9)
1928
1929 /* Unset guest mode. */
Paul Mackerras44a3add2013-10-04 21:45:04 +10001930 li r0, KVM_GUEST_MODE_HOST_HV
Paul Mackerras697d3892011-12-12 12:36:37 +00001931 stb r0, HSTATE_IN_GUEST(r13)
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001932 b guest_exit_cont
Paul Mackerrasde56a942011-06-29 00:21:34 +00001933
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001934.Lradix_hdsi:
1935 std r4, VCPU_FAULT_DAR(r9)
1936 stw r6, VCPU_FAULT_DSISR(r9)
1937.Lradix_hisi:
1938 mfspr r5, SPRN_ASDR
1939 std r5, VCPU_FAULT_GPA(r9)
1940 b guest_exit_cont
1941
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001942/*
Paul Mackerras342d3db2011-12-12 12:38:05 +00001943 * Similarly for an HISI, reflect it to the guest as an ISI unless
1944 * it is an HPTE not found fault for a page that we have paged out.
1945 */
1946kvmppc_hisi:
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001947 ld r3, VCPU_KVM(r9)
1948 lbz r0, KVM_RADIX(r3)
1949 cmpwi r0, 0
1950 bne .Lradix_hisi /* for radix, just save ASDR */
Paul Mackerras342d3db2011-12-12 12:38:05 +00001951 andis. r0, r11, SRR1_ISI_NOPT@h
1952 beq 1f
Paul Mackerras4e5acdc2017-02-28 11:05:47 +11001953 andi. r0, r11, MSR_IR /* instruction relocation enabled? */
1954 beq 3f
Paul Mackerrasef8c6402017-01-30 21:21:43 +11001955BEGIN_FTR_SECTION
1956 mfspr r5, SPRN_ASDR /* on POWER9, use ASDR to get VSID */
1957 b 4f
1958END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras342d3db2011-12-12 12:38:05 +00001959 clrrdi r0, r10, 28
Michael Neulingc75df6f2012-06-25 13:33:10 +00001960 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
Paul Mackerrascf29b212015-10-27 16:10:20 +11001961 li r0, BOOK3S_INTERRUPT_INST_SEGMENT
1962 bne 7f /* if no SLB entry found */
Paul Mackerras342d3db2011-12-12 12:38:05 +000019634:
1964 /* Search the hash table. */
1965 mr r3, r9 /* vcpu pointer */
1966 mr r4, r10
1967 mr r6, r11
1968 li r7, 0 /* instruction fault */
Anton Blanchardb1576fe2014-02-04 16:04:35 +11001969 bl kvmppc_hpte_hv_fault
Paul Mackerras342d3db2011-12-12 12:38:05 +00001970 ld r9, HSTATE_KVM_VCPU(r13)
1971 ld r10, VCPU_PC(r9)
1972 ld r11, VCPU_MSR(r9)
1973 li r12, BOOK3S_INTERRUPT_H_INST_STORAGE
1974 cmpdi r3, 0 /* retry the instruction */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001975 beq fast_interrupt_c_return
Paul Mackerras342d3db2011-12-12 12:38:05 +00001976 cmpdi r3, -1 /* handle in kernel mode */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001977 beq guest_exit_cont
Paul Mackerras342d3db2011-12-12 12:38:05 +00001978
Paul Mackerrascf29b212015-10-27 16:10:20 +11001979 /* Synthesize an ISI (or ISegI) for the guest */
Paul Mackerras342d3db2011-12-12 12:38:05 +00001980 mr r11, r3
Paul Mackerrascf29b212015-10-27 16:10:20 +110019811: li r0, BOOK3S_INTERRUPT_INST_STORAGE
19827: mtspr SPRN_SRR0, r10
Paul Mackerras342d3db2011-12-12 12:38:05 +00001983 mtspr SPRN_SRR1, r11
Paul Mackerrascf29b212015-10-27 16:10:20 +11001984 mr r10, r0
Michael Neulinge4e38122014-03-25 10:47:02 +11001985 bl kvmppc_msr_interrupt
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001986 b fast_interrupt_c_return
Paul Mackerras342d3db2011-12-12 12:38:05 +00001987
19883: ld r6, VCPU_KVM(r9) /* not relocated, use VRMA */
1989 ld r5, KVM_VRMA_SLB_V(r6)
1990 b 4b
1991
1992/*
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001993 * Try to handle an hcall in real mode.
1994 * Returns to the guest if we handle it, or continues on up to
1995 * the kernel if we can't (i.e. if we don't have a handler for
1996 * it, or if the handler returns H_TOO_HARD).
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001997 *
1998 * r5 - r8 contain hcall args,
1999 * r9 = vcpu, r10 = pc, r11 = msr, r12 = trap, r13 = paca
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002000 */
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002001hcall_try_real_mode:
Michael Neulingc75df6f2012-06-25 13:33:10 +00002002 ld r3,VCPU_GPR(R3)(r9)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002003 andi. r0,r11,MSR_PR
Liu Ping Fan27025a62013-11-19 14:12:48 +08002004 /* sc 1 from userspace - reflect to guest syscall */
2005 bne sc_1_fast_return
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002006 clrrdi r3,r3,2
2007 cmpldi r3,hcall_real_table_end - hcall_real_table
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002008 bge guest_exit_cont
Paul Mackerras699a0ea2014-06-02 11:02:59 +10002009 /* See if this hcall is enabled for in-kernel handling */
2010 ld r4, VCPU_KVM(r9)
2011 srdi r0, r3, 8 /* r0 = (r3 / 4) >> 6 */
2012 sldi r0, r0, 3 /* index into kvm->arch.enabled_hcalls[] */
2013 add r4, r4, r0
2014 ld r0, KVM_ENABLED_HCALLS(r4)
2015 rlwinm r4, r3, 32-2, 0x3f /* r4 = (r3 / 4) & 0x3f */
2016 srd r0, r0, r4
2017 andi. r0, r0, 1
2018 beq guest_exit_cont
2019 /* Get pointer to handler, if any, and call it */
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002020 LOAD_REG_ADDR(r4, hcall_real_table)
Paul Mackerras4baa1d82013-07-08 20:09:53 +10002021 lwax r3,r3,r4
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002022 cmpwi r3,0
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002023 beq guest_exit_cont
Anton Blanchard05a308c2014-06-12 18:16:10 +10002024 add r12,r3,r4
2025 mtctr r12
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002026 mr r3,r9 /* get vcpu pointer */
Michael Neulingc75df6f2012-06-25 13:33:10 +00002027 ld r4,VCPU_GPR(R4)(r9)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002028 bctrl
2029 cmpdi r3,H_TOO_HARD
2030 beq hcall_real_fallback
2031 ld r4,HSTATE_KVM_VCPU(r13)
Michael Neulingc75df6f2012-06-25 13:33:10 +00002032 std r3,VCPU_GPR(R3)(r4)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002033 ld r10,VCPU_PC(r4)
2034 ld r11,VCPU_MSR(r4)
2035 b fast_guest_return
2036
Liu Ping Fan27025a62013-11-19 14:12:48 +08002037sc_1_fast_return:
2038 mtspr SPRN_SRR0,r10
2039 mtspr SPRN_SRR1,r11
2040 li r10, BOOK3S_INTERRUPT_SYSCALL
Michael Neulinge4e38122014-03-25 10:47:02 +11002041 bl kvmppc_msr_interrupt
Liu Ping Fan27025a62013-11-19 14:12:48 +08002042 mr r4,r9
2043 b fast_guest_return
2044
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002045 /* We've attempted a real mode hcall, but it's punted it back
2046 * to userspace. We need to restore some clobbered volatiles
2047 * before resuming the pass-it-to-qemu path */
2048hcall_real_fallback:
2049 li r12,BOOK3S_INTERRUPT_SYSCALL
2050 ld r9, HSTATE_KVM_VCPU(r13)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002051
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002052 b guest_exit_cont
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002053
2054 .globl hcall_real_table
2055hcall_real_table:
2056 .long 0 /* 0 - unused */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002057 .long DOTSYM(kvmppc_h_remove) - hcall_real_table
2058 .long DOTSYM(kvmppc_h_enter) - hcall_real_table
2059 .long DOTSYM(kvmppc_h_read) - hcall_real_table
Paul Mackerrascdeee512015-06-24 21:18:07 +10002060 .long DOTSYM(kvmppc_h_clear_mod) - hcall_real_table
2061 .long DOTSYM(kvmppc_h_clear_ref) - hcall_real_table
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002062 .long DOTSYM(kvmppc_h_protect) - hcall_real_table
2063 .long DOTSYM(kvmppc_h_get_tce) - hcall_real_table
Alexey Kardashevskiy31217db2016-03-18 13:50:42 +11002064 .long DOTSYM(kvmppc_rm_h_put_tce) - hcall_real_table
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002065 .long 0 /* 0x24 - H_SET_SPRG0 */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002066 .long DOTSYM(kvmppc_h_set_dabr) - hcall_real_table
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002067 .long 0 /* 0x2c */
2068 .long 0 /* 0x30 */
2069 .long 0 /* 0x34 */
2070 .long 0 /* 0x38 */
2071 .long 0 /* 0x3c */
2072 .long 0 /* 0x40 */
2073 .long 0 /* 0x44 */
2074 .long 0 /* 0x48 */
2075 .long 0 /* 0x4c */
2076 .long 0 /* 0x50 */
2077 .long 0 /* 0x54 */
2078 .long 0 /* 0x58 */
2079 .long 0 /* 0x5c */
2080 .long 0 /* 0x60 */
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +00002081#ifdef CONFIG_KVM_XICS
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002082 .long DOTSYM(kvmppc_rm_h_eoi) - hcall_real_table
2083 .long DOTSYM(kvmppc_rm_h_cppr) - hcall_real_table
2084 .long DOTSYM(kvmppc_rm_h_ipi) - hcall_real_table
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10002085 .long DOTSYM(kvmppc_rm_h_ipoll) - hcall_real_table
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002086 .long DOTSYM(kvmppc_rm_h_xirr) - hcall_real_table
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +00002087#else
2088 .long 0 /* 0x64 - H_EOI */
2089 .long 0 /* 0x68 - H_CPPR */
2090 .long 0 /* 0x6c - H_IPI */
2091 .long 0 /* 0x70 - H_IPOLL */
2092 .long 0 /* 0x74 - H_XIRR */
2093#endif
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002094 .long 0 /* 0x78 */
2095 .long 0 /* 0x7c */
2096 .long 0 /* 0x80 */
2097 .long 0 /* 0x84 */
2098 .long 0 /* 0x88 */
2099 .long 0 /* 0x8c */
2100 .long 0 /* 0x90 */
2101 .long 0 /* 0x94 */
2102 .long 0 /* 0x98 */
2103 .long 0 /* 0x9c */
2104 .long 0 /* 0xa0 */
2105 .long 0 /* 0xa4 */
2106 .long 0 /* 0xa8 */
2107 .long 0 /* 0xac */
2108 .long 0 /* 0xb0 */
2109 .long 0 /* 0xb4 */
2110 .long 0 /* 0xb8 */
2111 .long 0 /* 0xbc */
2112 .long 0 /* 0xc0 */
2113 .long 0 /* 0xc4 */
2114 .long 0 /* 0xc8 */
2115 .long 0 /* 0xcc */
2116 .long 0 /* 0xd0 */
2117 .long 0 /* 0xd4 */
2118 .long 0 /* 0xd8 */
2119 .long 0 /* 0xdc */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002120 .long DOTSYM(kvmppc_h_cede) - hcall_real_table
Sam Bobroff90fd09f2014-12-03 13:30:40 +11002121 .long DOTSYM(kvmppc_rm_h_confer) - hcall_real_table
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002122 .long 0 /* 0xe8 */
2123 .long 0 /* 0xec */
2124 .long 0 /* 0xf0 */
2125 .long 0 /* 0xf4 */
2126 .long 0 /* 0xf8 */
2127 .long 0 /* 0xfc */
2128 .long 0 /* 0x100 */
2129 .long 0 /* 0x104 */
2130 .long 0 /* 0x108 */
2131 .long 0 /* 0x10c */
2132 .long 0 /* 0x110 */
2133 .long 0 /* 0x114 */
2134 .long 0 /* 0x118 */
2135 .long 0 /* 0x11c */
2136 .long 0 /* 0x120 */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002137 .long DOTSYM(kvmppc_h_bulk_remove) - hcall_real_table
Paul Mackerras8563bf52014-01-08 21:25:29 +11002138 .long 0 /* 0x128 */
2139 .long 0 /* 0x12c */
2140 .long 0 /* 0x130 */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002141 .long DOTSYM(kvmppc_h_set_xdabr) - hcall_real_table
Alexey Kardashevskiy31217db2016-03-18 13:50:42 +11002142 .long DOTSYM(kvmppc_rm_h_stuff_tce) - hcall_real_table
Alexey Kardashevskiyd3695aa2016-02-15 12:55:09 +11002143 .long DOTSYM(kvmppc_rm_h_put_tce_indirect) - hcall_real_table
Michael Ellermane928e9c2015-03-20 20:39:41 +11002144 .long 0 /* 0x140 */
2145 .long 0 /* 0x144 */
2146 .long 0 /* 0x148 */
2147 .long 0 /* 0x14c */
2148 .long 0 /* 0x150 */
2149 .long 0 /* 0x154 */
2150 .long 0 /* 0x158 */
2151 .long 0 /* 0x15c */
2152 .long 0 /* 0x160 */
2153 .long 0 /* 0x164 */
2154 .long 0 /* 0x168 */
2155 .long 0 /* 0x16c */
2156 .long 0 /* 0x170 */
2157 .long 0 /* 0x174 */
2158 .long 0 /* 0x178 */
2159 .long 0 /* 0x17c */
2160 .long 0 /* 0x180 */
2161 .long 0 /* 0x184 */
2162 .long 0 /* 0x188 */
2163 .long 0 /* 0x18c */
2164 .long 0 /* 0x190 */
2165 .long 0 /* 0x194 */
2166 .long 0 /* 0x198 */
2167 .long 0 /* 0x19c */
2168 .long 0 /* 0x1a0 */
2169 .long 0 /* 0x1a4 */
2170 .long 0 /* 0x1a8 */
2171 .long 0 /* 0x1ac */
2172 .long 0 /* 0x1b0 */
2173 .long 0 /* 0x1b4 */
2174 .long 0 /* 0x1b8 */
2175 .long 0 /* 0x1bc */
2176 .long 0 /* 0x1c0 */
2177 .long 0 /* 0x1c4 */
2178 .long 0 /* 0x1c8 */
2179 .long 0 /* 0x1cc */
2180 .long 0 /* 0x1d0 */
2181 .long 0 /* 0x1d4 */
2182 .long 0 /* 0x1d8 */
2183 .long 0 /* 0x1dc */
2184 .long 0 /* 0x1e0 */
2185 .long 0 /* 0x1e4 */
2186 .long 0 /* 0x1e8 */
2187 .long 0 /* 0x1ec */
2188 .long 0 /* 0x1f0 */
2189 .long 0 /* 0x1f4 */
2190 .long 0 /* 0x1f8 */
2191 .long 0 /* 0x1fc */
2192 .long 0 /* 0x200 */
2193 .long 0 /* 0x204 */
2194 .long 0 /* 0x208 */
2195 .long 0 /* 0x20c */
2196 .long 0 /* 0x210 */
2197 .long 0 /* 0x214 */
2198 .long 0 /* 0x218 */
2199 .long 0 /* 0x21c */
2200 .long 0 /* 0x220 */
2201 .long 0 /* 0x224 */
2202 .long 0 /* 0x228 */
2203 .long 0 /* 0x22c */
2204 .long 0 /* 0x230 */
2205 .long 0 /* 0x234 */
2206 .long 0 /* 0x238 */
2207 .long 0 /* 0x23c */
2208 .long 0 /* 0x240 */
2209 .long 0 /* 0x244 */
2210 .long 0 /* 0x248 */
2211 .long 0 /* 0x24c */
2212 .long 0 /* 0x250 */
2213 .long 0 /* 0x254 */
2214 .long 0 /* 0x258 */
2215 .long 0 /* 0x25c */
2216 .long 0 /* 0x260 */
2217 .long 0 /* 0x264 */
2218 .long 0 /* 0x268 */
2219 .long 0 /* 0x26c */
2220 .long 0 /* 0x270 */
2221 .long 0 /* 0x274 */
2222 .long 0 /* 0x278 */
2223 .long 0 /* 0x27c */
2224 .long 0 /* 0x280 */
2225 .long 0 /* 0x284 */
2226 .long 0 /* 0x288 */
2227 .long 0 /* 0x28c */
2228 .long 0 /* 0x290 */
2229 .long 0 /* 0x294 */
2230 .long 0 /* 0x298 */
2231 .long 0 /* 0x29c */
2232 .long 0 /* 0x2a0 */
2233 .long 0 /* 0x2a4 */
2234 .long 0 /* 0x2a8 */
2235 .long 0 /* 0x2ac */
2236 .long 0 /* 0x2b0 */
2237 .long 0 /* 0x2b4 */
2238 .long 0 /* 0x2b8 */
2239 .long 0 /* 0x2bc */
2240 .long 0 /* 0x2c0 */
2241 .long 0 /* 0x2c4 */
2242 .long 0 /* 0x2c8 */
2243 .long 0 /* 0x2cc */
2244 .long 0 /* 0x2d0 */
2245 .long 0 /* 0x2d4 */
2246 .long 0 /* 0x2d8 */
2247 .long 0 /* 0x2dc */
2248 .long 0 /* 0x2e0 */
2249 .long 0 /* 0x2e4 */
2250 .long 0 /* 0x2e8 */
2251 .long 0 /* 0x2ec */
2252 .long 0 /* 0x2f0 */
2253 .long 0 /* 0x2f4 */
2254 .long 0 /* 0x2f8 */
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10002255#ifdef CONFIG_KVM_XICS
2256 .long DOTSYM(kvmppc_rm_h_xirr_x) - hcall_real_table
2257#else
2258 .long 0 /* 0x2fc - H_XIRR_X*/
2259#endif
Michael Ellermane928e9c2015-03-20 20:39:41 +11002260 .long DOTSYM(kvmppc_h_random) - hcall_real_table
Paul Mackerrasae2113a2014-06-02 11:03:00 +10002261 .globl hcall_real_table_end
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002262hcall_real_table_end:
2263
Paul Mackerras8563bf52014-01-08 21:25:29 +11002264_GLOBAL(kvmppc_h_set_xdabr)
2265 andi. r0, r5, DABRX_USER | DABRX_KERNEL
2266 beq 6f
2267 li r0, DABRX_USER | DABRX_KERNEL | DABRX_BTI
2268 andc. r0, r5, r0
2269 beq 3f
22706: li r3, H_PARAMETER
2271 blr
2272
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002273_GLOBAL(kvmppc_h_set_dabr)
Paul Mackerras8563bf52014-01-08 21:25:29 +11002274 li r5, DABRX_USER | DABRX_KERNEL
22753:
Michael Neulingeee7ff92014-01-08 21:25:19 +11002276BEGIN_FTR_SECTION
2277 b 2f
2278END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002279 std r4,VCPU_DABR(r3)
Paul Mackerras8563bf52014-01-08 21:25:29 +11002280 stw r5, VCPU_DABRX(r3)
2281 mtspr SPRN_DABRX, r5
Paul Mackerras89436332012-03-02 01:38:23 +00002282 /* Work around P7 bug where DABR can get corrupted on mtspr */
22831: mtspr SPRN_DABR,r4
2284 mfspr r5, SPRN_DABR
2285 cmpd r4, r5
2286 bne 1b
2287 isync
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002288 li r3,0
2289 blr
2290
Paul Mackerras8563bf52014-01-08 21:25:29 +11002291 /* Emulate H_SET_DABR/X on P8 for the sake of compat mode guests */
22922: rlwimi r5, r4, 5, DAWRX_DR | DAWRX_DW
Thomas Huth760a7362015-11-20 09:11:45 +01002293 rlwimi r5, r4, 2, DAWRX_WT
Paul Mackerras8563bf52014-01-08 21:25:29 +11002294 clrrdi r4, r4, 3
2295 std r4, VCPU_DAWR(r3)
2296 std r5, VCPU_DAWRX(r3)
2297 mtspr SPRN_DAWR, r4
2298 mtspr SPRN_DAWRX, r5
2299 li r3, 0
Paul Mackerrasde56a942011-06-29 00:21:34 +00002300 blr
2301
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002302_GLOBAL(kvmppc_h_cede) /* r3 = vcpu pointer, r11 = msr, r13 = paca */
Paul Mackerras19ccb762011-07-23 17:42:46 +10002303 ori r11,r11,MSR_EE
2304 std r11,VCPU_MSR(r3)
2305 li r0,1
2306 stb r0,VCPU_CEDED(r3)
2307 sync /* order setting ceded vs. testing prodded */
2308 lbz r5,VCPU_PRODDED(r3)
2309 cmpwi r5,0
Paul Mackerras04f995a2012-08-06 00:03:28 +00002310 bne kvm_cede_prodded
Paul Mackerras6af27c82015-03-28 14:21:10 +11002311 li r12,0 /* set trap to 0 to say hcall is handled */
2312 stw r12,VCPU_TRAP(r3)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002313 li r0,H_SUCCESS
Michael Neulingc75df6f2012-06-25 13:33:10 +00002314 std r0,VCPU_GPR(R3)(r3)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002315
2316 /*
2317 * Set our bit in the bitmask of napping threads unless all the
2318 * other threads are already napping, in which case we send this
2319 * up to the host.
2320 */
2321 ld r5,HSTATE_KVM_VCORE(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002322 lbz r6,HSTATE_PTID(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002323 lwz r8,VCORE_ENTRY_EXIT(r5)
2324 clrldi r8,r8,56
2325 li r0,1
2326 sld r0,r0,r6
2327 addi r6,r5,VCORE_NAPPING_THREADS
232831: lwarx r4,0,r6
2329 or r4,r4,r0
Paul Mackerras7d6c40d2015-03-28 14:21:09 +11002330 cmpw r4,r8
2331 beq kvm_cede_exit
Paul Mackerras19ccb762011-07-23 17:42:46 +10002332 stwcx. r4,0,r6
2333 bne 31b
Paul Mackerras7d6c40d2015-03-28 14:21:09 +11002334 /* order napping_threads update vs testing entry_exit_map */
Paul Mackerrasf019b7a2013-11-16 17:46:03 +11002335 isync
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002336 li r0,NAPPING_CEDE
Paul Mackerras19ccb762011-07-23 17:42:46 +10002337 stb r0,HSTATE_NAPPING(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002338 lwz r7,VCORE_ENTRY_EXIT(r5)
2339 cmpwi r7,0x100
2340 bge 33f /* another thread already exiting */
2341
2342/*
2343 * Although not specifically required by the architecture, POWER7
2344 * preserves the following registers in nap mode, even if an SMT mode
2345 * switch occurs: SLB entries, PURR, SPURR, AMOR, UAMOR, AMR, SPRG0-3,
2346 * DAR, DSISR, DABR, DABRX, DSCR, PMCx, MMCRx, SIAR, SDAR.
2347 */
2348 /* Save non-volatile GPRs */
Michael Neulingc75df6f2012-06-25 13:33:10 +00002349 std r14, VCPU_GPR(R14)(r3)
2350 std r15, VCPU_GPR(R15)(r3)
2351 std r16, VCPU_GPR(R16)(r3)
2352 std r17, VCPU_GPR(R17)(r3)
2353 std r18, VCPU_GPR(R18)(r3)
2354 std r19, VCPU_GPR(R19)(r3)
2355 std r20, VCPU_GPR(R20)(r3)
2356 std r21, VCPU_GPR(R21)(r3)
2357 std r22, VCPU_GPR(R22)(r3)
2358 std r23, VCPU_GPR(R23)(r3)
2359 std r24, VCPU_GPR(R24)(r3)
2360 std r25, VCPU_GPR(R25)(r3)
2361 std r26, VCPU_GPR(R26)(r3)
2362 std r27, VCPU_GPR(R27)(r3)
2363 std r28, VCPU_GPR(R28)(r3)
2364 std r29, VCPU_GPR(R29)(r3)
2365 std r30, VCPU_GPR(R30)(r3)
2366 std r31, VCPU_GPR(R31)(r3)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002367
2368 /* save FP state */
Paul Mackerras595e4f72013-10-15 20:43:04 +11002369 bl kvmppc_save_fp
Paul Mackerras19ccb762011-07-23 17:42:46 +10002370
Paul Mackerras93d17392016-06-22 15:52:55 +10002371#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2372BEGIN_FTR_SECTION
2373 ld r9, HSTATE_KVM_VCPU(r13)
2374 bl kvmppc_save_tm
2375END_FTR_SECTION_IFSET(CPU_FTR_TM)
2376#endif
2377
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002378 /*
2379 * Set DEC to the smaller of DEC and HDEC, so that we wake
2380 * no later than the end of our timeslice (HDEC interrupts
2381 * don't wake us from nap).
2382 */
2383 mfspr r3, SPRN_DEC
2384 mfspr r4, SPRN_HDEC
2385 mftb r5
Paul Mackerras2f272462017-05-22 16:25:14 +10002386 extsw r3, r3
2387 EXTEND_HDEC(r4)
2388 cmpd r3, r4
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002389 ble 67f
2390 mtspr SPRN_DEC, r4
239167:
2392 /* save expiry time of guest decrementer */
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002393 add r3, r3, r5
2394 ld r4, HSTATE_KVM_VCPU(r13)
2395 ld r5, HSTATE_KVM_VCORE(r13)
2396 ld r6, VCORE_TB_OFFSET(r5)
2397 subf r3, r6, r3 /* convert to host TB value */
2398 std r3, VCPU_DEC_EXPIRES(r4)
2399
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002400#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2401 ld r4, HSTATE_KVM_VCPU(r13)
2402 addi r3, r4, VCPU_TB_CEDE
2403 bl kvmhv_accumulate_time
2404#endif
2405
Paul Mackerrasccc07772015-03-28 14:21:07 +11002406 lis r3, LPCR_PECEDP@h /* Do wake on privileged doorbell */
2407
Paul Mackerras19ccb762011-07-23 17:42:46 +10002408 /*
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002409 * Take a nap until a decrementer or external or doobell interrupt
Paul Mackerrasccc07772015-03-28 14:21:07 +11002410 * occurs, with PECE1 and PECE0 set in LPCR.
Paul Mackerras66feed62015-03-28 14:21:12 +11002411 * On POWER8, set PECEDH, and if we are ceding, also set PECEDP.
Paul Mackerrasccc07772015-03-28 14:21:07 +11002412 * Also clear the runlatch bit before napping.
Paul Mackerras19ccb762011-07-23 17:42:46 +10002413 */
Paul Mackerras56548fc2014-12-03 14:48:40 +11002414kvm_do_nap:
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002415 mfspr r0, SPRN_CTRLF
2416 clrrdi r0, r0, 1
2417 mtspr SPRN_CTRLT, r0
Preeti U Murthy582b9102014-04-11 16:02:08 +05302418
Paul Mackerrasf0888f72012-02-03 00:54:17 +00002419 li r0,1
2420 stb r0,HSTATE_HWTHREAD_REQ(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002421 mfspr r5,SPRN_LPCR
2422 ori r5,r5,LPCR_PECE0 | LPCR_PECE1
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002423BEGIN_FTR_SECTION
Paul Mackerras66feed62015-03-28 14:21:12 +11002424 ori r5, r5, LPCR_PECEDH
Paul Mackerrasccc07772015-03-28 14:21:07 +11002425 rlwimi r5, r3, 0, LPCR_PECEDP
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002426END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasbf53c882016-11-18 14:34:07 +11002427
2428kvm_nap_sequence: /* desired LPCR value in r5 */
2429BEGIN_FTR_SECTION
2430 /*
2431 * PSSCR bits: exit criterion = 1 (wakeup based on LPCR at sreset)
2432 * enable state loss = 1 (allow SMT mode switch)
2433 * requested level = 0 (just stop dispatching)
2434 */
2435 lis r3, (PSSCR_EC | PSSCR_ESL)@h
2436 mtspr SPRN_PSSCR, r3
2437 /* Set LPCR_PECE_HVEE bit to enable wakeup by HV interrupts */
2438 li r4, LPCR_PECE_HVEE@higher
2439 sldi r4, r4, 32
2440 or r5, r5, r4
2441END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002442 mtspr SPRN_LPCR,r5
2443 isync
2444 li r0, 0
2445 std r0, HSTATE_SCRATCH0(r13)
2446 ptesync
2447 ld r0, HSTATE_SCRATCH0(r13)
24481: cmpd r0, r0
2449 bne 1b
Paul Mackerrasbf53c882016-11-18 14:34:07 +11002450BEGIN_FTR_SECTION
Paul Mackerras19ccb762011-07-23 17:42:46 +10002451 nap
Paul Mackerrasbf53c882016-11-18 14:34:07 +11002452FTR_SECTION_ELSE
2453 PPC_STOP
2454ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002455 b .
2456
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100245733: mr r4, r3
2458 li r3, 0
2459 li r12, 0
2460 b 34f
2461
Paul Mackerras19ccb762011-07-23 17:42:46 +10002462kvm_end_cede:
Paul Mackerras4619ac82013-04-17 20:31:41 +00002463 /* get vcpu pointer */
2464 ld r4, HSTATE_KVM_VCPU(r13)
2465
Paul Mackerras19ccb762011-07-23 17:42:46 +10002466 /* Woken by external or decrementer interrupt */
2467 ld r1, HSTATE_HOST_R1(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002468
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002469#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2470 addi r3, r4, VCPU_TB_RMINTR
2471 bl kvmhv_accumulate_time
2472#endif
2473
Paul Mackerras93d17392016-06-22 15:52:55 +10002474#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2475BEGIN_FTR_SECTION
2476 bl kvmppc_restore_tm
2477END_FTR_SECTION_IFSET(CPU_FTR_TM)
2478#endif
2479
Paul Mackerras19ccb762011-07-23 17:42:46 +10002480 /* load up FP state */
2481 bl kvmppc_load_fp
2482
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002483 /* Restore guest decrementer */
2484 ld r3, VCPU_DEC_EXPIRES(r4)
2485 ld r5, HSTATE_KVM_VCORE(r13)
2486 ld r6, VCORE_TB_OFFSET(r5)
2487 add r3, r3, r6 /* convert host TB to guest TB value */
2488 mftb r7
2489 subf r3, r7, r3
2490 mtspr SPRN_DEC, r3
2491
Paul Mackerras19ccb762011-07-23 17:42:46 +10002492 /* Load NV GPRS */
Michael Neulingc75df6f2012-06-25 13:33:10 +00002493 ld r14, VCPU_GPR(R14)(r4)
2494 ld r15, VCPU_GPR(R15)(r4)
2495 ld r16, VCPU_GPR(R16)(r4)
2496 ld r17, VCPU_GPR(R17)(r4)
2497 ld r18, VCPU_GPR(R18)(r4)
2498 ld r19, VCPU_GPR(R19)(r4)
2499 ld r20, VCPU_GPR(R20)(r4)
2500 ld r21, VCPU_GPR(R21)(r4)
2501 ld r22, VCPU_GPR(R22)(r4)
2502 ld r23, VCPU_GPR(R23)(r4)
2503 ld r24, VCPU_GPR(R24)(r4)
2504 ld r25, VCPU_GPR(R25)(r4)
2505 ld r26, VCPU_GPR(R26)(r4)
2506 ld r27, VCPU_GPR(R27)(r4)
2507 ld r28, VCPU_GPR(R28)(r4)
2508 ld r29, VCPU_GPR(R29)(r4)
2509 ld r30, VCPU_GPR(R30)(r4)
2510 ld r31, VCPU_GPR(R31)(r4)
Suresh Warrier37f55d32016-08-19 15:35:46 +10002511
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002512 /* Check the wake reason in SRR1 to see why we got here */
2513 bl kvmppc_check_wake_reason
Paul Mackerras19ccb762011-07-23 17:42:46 +10002514
Suresh Warrier37f55d32016-08-19 15:35:46 +10002515 /*
2516 * Restore volatile registers since we could have called a
2517 * C routine in kvmppc_check_wake_reason
2518 * r4 = VCPU
2519 * r3 tells us whether we need to return to host or not
2520 * WARNING: it gets checked further down:
2521 * should not modify r3 until this check is done.
2522 */
2523 ld r4, HSTATE_KVM_VCPU(r13)
2524
Paul Mackerras19ccb762011-07-23 17:42:46 +10002525 /* clear our bit in vcore->napping_threads */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100252634: ld r5,HSTATE_KVM_VCORE(r13)
2527 lbz r7,HSTATE_PTID(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002528 li r0,1
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002529 sld r0,r0,r7
Paul Mackerras19ccb762011-07-23 17:42:46 +10002530 addi r6,r5,VCORE_NAPPING_THREADS
253132: lwarx r7,0,r6
2532 andc r7,r7,r0
2533 stwcx. r7,0,r6
2534 bne 32b
2535 li r0,0
2536 stb r0,HSTATE_NAPPING(r13)
2537
Suresh Warrier37f55d32016-08-19 15:35:46 +10002538 /* See if the wake reason saved in r3 means we need to exit */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002539 stw r12, VCPU_TRAP(r4)
Paul Mackerras4619ac82013-04-17 20:31:41 +00002540 mr r9, r4
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002541 cmpdi r3, 0
2542 bgt guest_exit_cont
Paul Mackerras4619ac82013-04-17 20:31:41 +00002543
Paul Mackerras19ccb762011-07-23 17:42:46 +10002544 /* see if any other thread is already exiting */
2545 lwz r0,VCORE_ENTRY_EXIT(r5)
2546 cmpwi r0,0x100
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002547 bge guest_exit_cont
Paul Mackerras19ccb762011-07-23 17:42:46 +10002548
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002549 b kvmppc_cede_reentry /* if not go back to guest */
Paul Mackerras19ccb762011-07-23 17:42:46 +10002550
2551 /* cede when already previously prodded case */
Paul Mackerras04f995a2012-08-06 00:03:28 +00002552kvm_cede_prodded:
2553 li r0,0
Paul Mackerras19ccb762011-07-23 17:42:46 +10002554 stb r0,VCPU_PRODDED(r3)
2555 sync /* order testing prodded vs. clearing ceded */
2556 stb r0,VCPU_CEDED(r3)
2557 li r3,H_SUCCESS
2558 blr
2559
2560 /* we've ceded but we want to give control to the host */
Paul Mackerras04f995a2012-08-06 00:03:28 +00002561kvm_cede_exit:
Paul Mackerras6af27c82015-03-28 14:21:10 +11002562 ld r9, HSTATE_KVM_VCPU(r13)
2563 b guest_exit_cont
Paul Mackerras19ccb762011-07-23 17:42:46 +10002564
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002565 /* Try to handle a machine check in real mode */
2566machine_check_realmode:
2567 mr r3, r9 /* get vcpu pointer */
Anton Blanchardb1576fe2014-02-04 16:04:35 +11002568 bl kvmppc_realmode_machine_check
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002569 nop
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002570 ld r9, HSTATE_KVM_VCPU(r13)
2571 li r12, BOOK3S_INTERRUPT_MACHINE_CHECK
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +05302572 /*
2573 * Deliver unhandled/fatal (e.g. UE) MCE errors to guest through
2574 * machine check interrupt (set HSRR0 to 0x200). And for handled
2575 * errors (no-fatal), just go back to guest execution with current
2576 * HSRR0 instead of exiting guest. This new approach will inject
2577 * machine check to guest for fatal error causing guest to crash.
2578 *
2579 * The old code used to return to host for unhandled errors which
2580 * was causing guest to hang with soft lockups inside guest and
2581 * makes it difficult to recover guest instance.
Mahesh Salgaonkar966d7132015-03-23 22:24:45 +05302582 *
2583 * if we receive machine check with MSR(RI=0) then deliver it to
2584 * guest as machine check causing guest to crash.
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +05302585 */
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +05302586 ld r11, VCPU_MSR(r9)
Paul Mackerras1c9e3d52015-11-12 16:43:48 +11002587 rldicl. r0, r11, 64-MSR_HV_LG, 63 /* check if it happened in HV mode */
2588 bne mc_cont /* if so, exit to host */
Mahesh Salgaonkar966d7132015-03-23 22:24:45 +05302589 andi. r10, r11, MSR_RI /* check for unrecoverable exception */
2590 beq 1f /* Deliver a machine check to guest */
2591 ld r10, VCPU_PC(r9)
2592 cmpdi r3, 0 /* Did we handle MCE ? */
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +05302593 bne 2f /* Continue guest execution. */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002594 /* If not, deliver a machine check. SRR0/1 are already set */
Mahesh Salgaonkar966d7132015-03-23 22:24:45 +053025951: li r10, BOOK3S_INTERRUPT_MACHINE_CHECK
Michael Neulinge4e38122014-03-25 10:47:02 +11002596 bl kvmppc_msr_interrupt
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +053025972: b fast_interrupt_c_return
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002598
Paul Mackerrasde56a942011-06-29 00:21:34 +00002599/*
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002600 * Check the reason we woke from nap, and take appropriate action.
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002601 * Returns (in r3):
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002602 * 0 if nothing needs to be done
2603 * 1 if something happened that needs to be handled by the host
Paul Mackerras66feed62015-03-28 14:21:12 +11002604 * -1 if there was a guest wakeup (IPI or msgsnd)
Suresh Warriere3c13e52016-08-19 15:35:51 +10002605 * -2 if we handled a PCI passthrough interrupt (returned by
2606 * kvmppc_read_intr only)
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002607 *
2608 * Also sets r12 to the interrupt vector for any interrupt that needs
2609 * to be handled now by the host (0x500 for external interrupt), or zero.
Suresh Warrier37f55d32016-08-19 15:35:46 +10002610 * Modifies all volatile registers (since it may call a C function).
2611 * This routine calls kvmppc_read_intr, a C function, if an external
2612 * interrupt is pending.
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002613 */
2614kvmppc_check_wake_reason:
2615 mfspr r6, SPRN_SRR1
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002616BEGIN_FTR_SECTION
2617 rlwinm r6, r6, 45-31, 0xf /* extract wake reason field (P8) */
2618FTR_SECTION_ELSE
2619 rlwinm r6, r6, 45-31, 0xe /* P7 wake reason field is 3 bits */
2620ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_207S)
2621 cmpwi r6, 8 /* was it an external interrupt? */
Suresh Warrier37f55d32016-08-19 15:35:46 +10002622 beq 7f /* if so, see what it was */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002623 li r3, 0
2624 li r12, 0
2625 cmpwi r6, 6 /* was it the decrementer? */
2626 beq 0f
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002627BEGIN_FTR_SECTION
2628 cmpwi r6, 5 /* privileged doorbell? */
2629 beq 0f
Paul Mackerras5d00f662014-01-08 21:25:28 +11002630 cmpwi r6, 3 /* hypervisor doorbell? */
2631 beq 3f
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002632END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05302633 cmpwi r6, 0xa /* Hypervisor maintenance ? */
2634 beq 4f
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002635 li r3, 1 /* anything else, return 1 */
26360: blr
2637
Paul Mackerras5d00f662014-01-08 21:25:28 +11002638 /* hypervisor doorbell */
26393: li r12, BOOK3S_INTERRUPT_H_DOORBELL
Gautham R. Shenoy70aa3962015-10-15 11:29:58 +05302640
2641 /*
2642 * Clear the doorbell as we will invoke the handler
2643 * explicitly in the guest exit path.
2644 */
2645 lis r6, (PPC_DBELL_SERVER << (63-36))@h
2646 PPC_MSGCLR(6)
Paul Mackerras66feed62015-03-28 14:21:12 +11002647 /* see if it's a host IPI */
Paul Mackerras5d00f662014-01-08 21:25:28 +11002648 li r3, 1
Paul Mackerras66feed62015-03-28 14:21:12 +11002649 lbz r0, HSTATE_HOST_IPI(r13)
2650 cmpwi r0, 0
2651 bnelr
Gautham R. Shenoy70aa3962015-10-15 11:29:58 +05302652 /* if not, return -1 */
Paul Mackerras66feed62015-03-28 14:21:12 +11002653 li r3, -1
Paul Mackerras5d00f662014-01-08 21:25:28 +11002654 blr
2655
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05302656 /* Woken up due to Hypervisor maintenance interrupt */
26574: li r12, BOOK3S_INTERRUPT_HMI
2658 li r3, 1
2659 blr
2660
Suresh Warrier37f55d32016-08-19 15:35:46 +10002661 /* external interrupt - create a stack frame so we can call C */
26627: mflr r0
2663 std r0, PPC_LR_STKOFF(r1)
2664 stdu r1, -PPC_MIN_STKFRM(r1)
2665 bl kvmppc_read_intr
2666 nop
2667 li r12, BOOK3S_INTERRUPT_EXTERNAL
Suresh Warrierf7af5202016-08-19 15:35:52 +10002668 cmpdi r3, 1
2669 ble 1f
2670
2671 /*
2672 * Return code of 2 means PCI passthrough interrupt, but
2673 * we need to return back to host to complete handling the
2674 * interrupt. Trap reason is expected in r12 by guest
2675 * exit code.
2676 */
2677 li r12, BOOK3S_INTERRUPT_HV_RM_HARD
26781:
Suresh Warrier37f55d32016-08-19 15:35:46 +10002679 ld r0, PPC_MIN_STKFRM+PPC_LR_STKOFF(r1)
2680 addi r1, r1, PPC_MIN_STKFRM
2681 mtlr r0
2682 blr
Paul Mackerrasde56a942011-06-29 00:21:34 +00002683
2684/*
2685 * Save away FP, VMX and VSX registers.
2686 * r3 = vcpu pointer
Paul Mackerras595e4f72013-10-15 20:43:04 +11002687 * N.B. r30 and r31 are volatile across this function,
2688 * thus it is not callable from C.
Paul Mackerrasde56a942011-06-29 00:21:34 +00002689 */
Paul Mackerras595e4f72013-10-15 20:43:04 +11002690kvmppc_save_fp:
2691 mflr r30
2692 mr r31,r3
Paul Mackerras89436332012-03-02 01:38:23 +00002693 mfmsr r5
2694 ori r8,r5,MSR_FP
Paul Mackerrasde56a942011-06-29 00:21:34 +00002695#ifdef CONFIG_ALTIVEC
2696BEGIN_FTR_SECTION
2697 oris r8,r8,MSR_VEC@h
2698END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2699#endif
2700#ifdef CONFIG_VSX
2701BEGIN_FTR_SECTION
2702 oris r8,r8,MSR_VSX@h
2703END_FTR_SECTION_IFSET(CPU_FTR_VSX)
2704#endif
2705 mtmsrd r8
Paul Mackerras595e4f72013-10-15 20:43:04 +11002706 addi r3,r3,VCPU_FPRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02002707 bl store_fp_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00002708#ifdef CONFIG_ALTIVEC
2709BEGIN_FTR_SECTION
Paul Mackerras595e4f72013-10-15 20:43:04 +11002710 addi r3,r31,VCPU_VRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02002711 bl store_vr_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00002712END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2713#endif
2714 mfspr r6,SPRN_VRSAVE
Paul Mackerrase724f082014-03-13 20:02:48 +11002715 stw r6,VCPU_VRSAVE(r31)
Paul Mackerras595e4f72013-10-15 20:43:04 +11002716 mtlr r30
Paul Mackerrasde56a942011-06-29 00:21:34 +00002717 blr
2718
2719/*
2720 * Load up FP, VMX and VSX registers
2721 * r4 = vcpu pointer
Paul Mackerras595e4f72013-10-15 20:43:04 +11002722 * N.B. r30 and r31 are volatile across this function,
2723 * thus it is not callable from C.
Paul Mackerrasde56a942011-06-29 00:21:34 +00002724 */
Paul Mackerrasde56a942011-06-29 00:21:34 +00002725kvmppc_load_fp:
Paul Mackerras595e4f72013-10-15 20:43:04 +11002726 mflr r30
2727 mr r31,r4
Paul Mackerrasde56a942011-06-29 00:21:34 +00002728 mfmsr r9
2729 ori r8,r9,MSR_FP
2730#ifdef CONFIG_ALTIVEC
2731BEGIN_FTR_SECTION
2732 oris r8,r8,MSR_VEC@h
2733END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2734#endif
2735#ifdef CONFIG_VSX
2736BEGIN_FTR_SECTION
2737 oris r8,r8,MSR_VSX@h
2738END_FTR_SECTION_IFSET(CPU_FTR_VSX)
2739#endif
2740 mtmsrd r8
Paul Mackerras595e4f72013-10-15 20:43:04 +11002741 addi r3,r4,VCPU_FPRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02002742 bl load_fp_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00002743#ifdef CONFIG_ALTIVEC
2744BEGIN_FTR_SECTION
Paul Mackerras595e4f72013-10-15 20:43:04 +11002745 addi r3,r31,VCPU_VRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02002746 bl load_vr_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00002747END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2748#endif
Paul Mackerrase724f082014-03-13 20:02:48 +11002749 lwz r7,VCPU_VRSAVE(r31)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002750 mtspr SPRN_VRSAVE,r7
Paul Mackerras595e4f72013-10-15 20:43:04 +11002751 mtlr r30
2752 mr r4,r31
Paul Mackerrasde56a942011-06-29 00:21:34 +00002753 blr
Paul Mackerras44a3add2013-10-04 21:45:04 +10002754
Paul Mackerrasf024ee02016-06-22 14:21:59 +10002755#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2756/*
2757 * Save transactional state and TM-related registers.
2758 * Called with r9 pointing to the vcpu struct.
2759 * This can modify all checkpointed registers, but
2760 * restores r1, r2 and r9 (vcpu pointer) before exit.
2761 */
2762kvmppc_save_tm:
2763 mflr r0
2764 std r0, PPC_LR_STKOFF(r1)
2765
2766 /* Turn on TM. */
2767 mfmsr r8
2768 li r0, 1
2769 rldimi r8, r0, MSR_TM_LG, 63-MSR_TM_LG
2770 mtmsrd r8
2771
2772 ld r5, VCPU_MSR(r9)
2773 rldicl. r5, r5, 64 - MSR_TS_S_LG, 62
2774 beq 1f /* TM not active in guest. */
2775
2776 std r1, HSTATE_HOST_R1(r13)
2777 li r3, TM_CAUSE_KVM_RESCHED
2778
2779 /* Clear the MSR RI since r1, r13 are all going to be foobar. */
2780 li r5, 0
2781 mtmsrd r5, 1
2782
2783 /* All GPRs are volatile at this point. */
2784 TRECLAIM(R3)
2785
2786 /* Temporarily store r13 and r9 so we have some regs to play with */
2787 SET_SCRATCH0(r13)
2788 GET_PACA(r13)
2789 std r9, PACATMSCRATCH(r13)
2790 ld r9, HSTATE_KVM_VCPU(r13)
2791
2792 /* Get a few more GPRs free. */
2793 std r29, VCPU_GPRS_TM(29)(r9)
2794 std r30, VCPU_GPRS_TM(30)(r9)
2795 std r31, VCPU_GPRS_TM(31)(r9)
2796
2797 /* Save away PPR and DSCR soon so don't run with user values. */
2798 mfspr r31, SPRN_PPR
2799 HMT_MEDIUM
2800 mfspr r30, SPRN_DSCR
2801 ld r29, HSTATE_DSCR(r13)
2802 mtspr SPRN_DSCR, r29
2803
2804 /* Save all but r9, r13 & r29-r31 */
2805 reg = 0
2806 .rept 29
2807 .if (reg != 9) && (reg != 13)
2808 std reg, VCPU_GPRS_TM(reg)(r9)
2809 .endif
2810 reg = reg + 1
2811 .endr
2812 /* ... now save r13 */
2813 GET_SCRATCH0(r4)
2814 std r4, VCPU_GPRS_TM(13)(r9)
2815 /* ... and save r9 */
2816 ld r4, PACATMSCRATCH(r13)
2817 std r4, VCPU_GPRS_TM(9)(r9)
2818
2819 /* Reload stack pointer and TOC. */
2820 ld r1, HSTATE_HOST_R1(r13)
2821 ld r2, PACATOC(r13)
2822
2823 /* Set MSR RI now we have r1 and r13 back. */
2824 li r5, MSR_RI
2825 mtmsrd r5, 1
2826
2827 /* Save away checkpinted SPRs. */
2828 std r31, VCPU_PPR_TM(r9)
2829 std r30, VCPU_DSCR_TM(r9)
2830 mflr r5
2831 mfcr r6
2832 mfctr r7
2833 mfspr r8, SPRN_AMR
2834 mfspr r10, SPRN_TAR
Paul Mackerras0d808df2016-11-07 15:09:58 +11002835 mfxer r11
Paul Mackerrasf024ee02016-06-22 14:21:59 +10002836 std r5, VCPU_LR_TM(r9)
2837 stw r6, VCPU_CR_TM(r9)
2838 std r7, VCPU_CTR_TM(r9)
2839 std r8, VCPU_AMR_TM(r9)
2840 std r10, VCPU_TAR_TM(r9)
Paul Mackerras0d808df2016-11-07 15:09:58 +11002841 std r11, VCPU_XER_TM(r9)
Paul Mackerrasf024ee02016-06-22 14:21:59 +10002842
2843 /* Restore r12 as trap number. */
2844 lwz r12, VCPU_TRAP(r9)
2845
2846 /* Save FP/VSX. */
2847 addi r3, r9, VCPU_FPRS_TM
2848 bl store_fp_state
2849 addi r3, r9, VCPU_VRS_TM
2850 bl store_vr_state
2851 mfspr r6, SPRN_VRSAVE
2852 stw r6, VCPU_VRSAVE_TM(r9)
28531:
2854 /*
2855 * We need to save these SPRs after the treclaim so that the software
2856 * error code is recorded correctly in the TEXASR. Also the user may
2857 * change these outside of a transaction, so they must always be
2858 * context switched.
2859 */
2860 mfspr r5, SPRN_TFHAR
2861 mfspr r6, SPRN_TFIAR
2862 mfspr r7, SPRN_TEXASR
2863 std r5, VCPU_TFHAR(r9)
2864 std r6, VCPU_TFIAR(r9)
2865 std r7, VCPU_TEXASR(r9)
2866
2867 ld r0, PPC_LR_STKOFF(r1)
2868 mtlr r0
2869 blr
2870
2871/*
2872 * Restore transactional state and TM-related registers.
2873 * Called with r4 pointing to the vcpu struct.
2874 * This potentially modifies all checkpointed registers.
2875 * It restores r1, r2, r4 from the PACA.
2876 */
2877kvmppc_restore_tm:
2878 mflr r0
2879 std r0, PPC_LR_STKOFF(r1)
2880
2881 /* Turn on TM/FP/VSX/VMX so we can restore them. */
2882 mfmsr r5
2883 li r6, MSR_TM >> 32
2884 sldi r6, r6, 32
2885 or r5, r5, r6
2886 ori r5, r5, MSR_FP
2887 oris r5, r5, (MSR_VEC | MSR_VSX)@h
2888 mtmsrd r5
2889
2890 /*
2891 * The user may change these outside of a transaction, so they must
2892 * always be context switched.
2893 */
2894 ld r5, VCPU_TFHAR(r4)
2895 ld r6, VCPU_TFIAR(r4)
2896 ld r7, VCPU_TEXASR(r4)
2897 mtspr SPRN_TFHAR, r5
2898 mtspr SPRN_TFIAR, r6
2899 mtspr SPRN_TEXASR, r7
2900
2901 ld r5, VCPU_MSR(r4)
2902 rldicl. r5, r5, 64 - MSR_TS_S_LG, 62
2903 beqlr /* TM not active in guest */
2904 std r1, HSTATE_HOST_R1(r13)
2905
2906 /* Make sure the failure summary is set, otherwise we'll program check
2907 * when we trechkpt. It's possible that this might have been not set
2908 * on a kvmppc_set_one_reg() call but we shouldn't let this crash the
2909 * host.
2910 */
2911 oris r7, r7, (TEXASR_FS)@h
2912 mtspr SPRN_TEXASR, r7
2913
2914 /*
2915 * We need to load up the checkpointed state for the guest.
2916 * We need to do this early as it will blow away any GPRs, VSRs and
2917 * some SPRs.
2918 */
2919
2920 mr r31, r4
2921 addi r3, r31, VCPU_FPRS_TM
2922 bl load_fp_state
2923 addi r3, r31, VCPU_VRS_TM
2924 bl load_vr_state
2925 mr r4, r31
2926 lwz r7, VCPU_VRSAVE_TM(r4)
2927 mtspr SPRN_VRSAVE, r7
2928
2929 ld r5, VCPU_LR_TM(r4)
2930 lwz r6, VCPU_CR_TM(r4)
2931 ld r7, VCPU_CTR_TM(r4)
2932 ld r8, VCPU_AMR_TM(r4)
2933 ld r9, VCPU_TAR_TM(r4)
Paul Mackerras0d808df2016-11-07 15:09:58 +11002934 ld r10, VCPU_XER_TM(r4)
Paul Mackerrasf024ee02016-06-22 14:21:59 +10002935 mtlr r5
2936 mtcr r6
2937 mtctr r7
2938 mtspr SPRN_AMR, r8
2939 mtspr SPRN_TAR, r9
Paul Mackerras0d808df2016-11-07 15:09:58 +11002940 mtxer r10
Paul Mackerrasf024ee02016-06-22 14:21:59 +10002941
2942 /*
2943 * Load up PPR and DSCR values but don't put them in the actual SPRs
2944 * till the last moment to avoid running with userspace PPR and DSCR for
2945 * too long.
2946 */
2947 ld r29, VCPU_DSCR_TM(r4)
2948 ld r30, VCPU_PPR_TM(r4)
2949
2950 std r2, PACATMSCRATCH(r13) /* Save TOC */
2951
2952 /* Clear the MSR RI since r1, r13 are all going to be foobar. */
2953 li r5, 0
2954 mtmsrd r5, 1
2955
2956 /* Load GPRs r0-r28 */
2957 reg = 0
2958 .rept 29
2959 ld reg, VCPU_GPRS_TM(reg)(r31)
2960 reg = reg + 1
2961 .endr
2962
2963 mtspr SPRN_DSCR, r29
2964 mtspr SPRN_PPR, r30
2965
2966 /* Load final GPRs */
2967 ld 29, VCPU_GPRS_TM(29)(r31)
2968 ld 30, VCPU_GPRS_TM(30)(r31)
2969 ld 31, VCPU_GPRS_TM(31)(r31)
2970
2971 /* TM checkpointed state is now setup. All GPRs are now volatile. */
2972 TRECHKPT
2973
2974 /* Now let's get back the state we need. */
2975 HMT_MEDIUM
2976 GET_PACA(r13)
2977 ld r29, HSTATE_DSCR(r13)
2978 mtspr SPRN_DSCR, r29
2979 ld r4, HSTATE_KVM_VCPU(r13)
2980 ld r1, HSTATE_HOST_R1(r13)
2981 ld r2, PACATMSCRATCH(r13)
2982
2983 /* Set the MSR RI since we have our registers back. */
2984 li r5, MSR_RI
2985 mtmsrd r5, 1
2986
2987 ld r0, PPC_LR_STKOFF(r1)
2988 mtlr r0
2989 blr
2990#endif
2991
Paul Mackerras44a3add2013-10-04 21:45:04 +10002992/*
2993 * We come here if we get any exception or interrupt while we are
2994 * executing host real mode code while in guest MMU context.
2995 * For now just spin, but we should do something better.
2996 */
2997kvmppc_bad_host_intr:
2998 b .
Michael Neulinge4e38122014-03-25 10:47:02 +11002999
3000/*
3001 * This mimics the MSR transition on IRQ delivery. The new guest MSR is taken
3002 * from VCPU_INTR_MSR and is modified based on the required TM state changes.
3003 * r11 has the guest MSR value (in/out)
3004 * r9 has a vcpu pointer (in)
3005 * r0 is used as a scratch register
3006 */
3007kvmppc_msr_interrupt:
3008 rldicl r0, r11, 64 - MSR_TS_S_LG, 62
3009 cmpwi r0, 2 /* Check if we are in transactional state.. */
3010 ld r11, VCPU_INTR_MSR(r9)
3011 bne 1f
3012 /* ... if transactional, change to suspended */
3013 li r0, 1
30141: rldimi r11, r0, MSR_TS_S_LG, 63 - MSR_TS_T_LG
3015 blr
Paul Mackerras9bc01a92014-05-26 19:48:40 +10003016
3017/*
3018 * This works around a hardware bug on POWER8E processors, where
3019 * writing a 1 to the MMCR0[PMAO] bit doesn't generate a
3020 * performance monitor interrupt. Instead, when we need to have
3021 * an interrupt pending, we have to arrange for a counter to overflow.
3022 */
3023kvmppc_fix_pmao:
3024 li r3, 0
3025 mtspr SPRN_MMCR2, r3
3026 lis r3, (MMCR0_PMXE | MMCR0_FCECE)@h
3027 ori r3, r3, MMCR0_PMCjCE | MMCR0_C56RUN
3028 mtspr SPRN_MMCR0, r3
3029 lis r3, 0x7fff
3030 ori r3, r3, 0xffff
3031 mtspr SPRN_PMC6, r3
3032 isync
3033 blr
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11003034
3035#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
3036/*
3037 * Start timing an activity
3038 * r3 = pointer to time accumulation struct, r4 = vcpu
3039 */
3040kvmhv_start_timing:
3041 ld r5, HSTATE_KVM_VCORE(r13)
3042 lbz r6, VCORE_IN_GUEST(r5)
3043 cmpwi r6, 0
3044 beq 5f /* if in guest, need to */
3045 ld r6, VCORE_TB_OFFSET(r5) /* subtract timebase offset */
30465: mftb r5
3047 subf r5, r6, r5
3048 std r3, VCPU_CUR_ACTIVITY(r4)
3049 std r5, VCPU_ACTIVITY_START(r4)
3050 blr
3051
3052/*
3053 * Accumulate time to one activity and start another.
3054 * r3 = pointer to new time accumulation struct, r4 = vcpu
3055 */
3056kvmhv_accumulate_time:
3057 ld r5, HSTATE_KVM_VCORE(r13)
3058 lbz r8, VCORE_IN_GUEST(r5)
3059 cmpwi r8, 0
3060 beq 4f /* if in guest, need to */
3061 ld r8, VCORE_TB_OFFSET(r5) /* subtract timebase offset */
30624: ld r5, VCPU_CUR_ACTIVITY(r4)
3063 ld r6, VCPU_ACTIVITY_START(r4)
3064 std r3, VCPU_CUR_ACTIVITY(r4)
3065 mftb r7
3066 subf r7, r8, r7
3067 std r7, VCPU_ACTIVITY_START(r4)
3068 cmpdi r5, 0
3069 beqlr
3070 subf r3, r6, r7
3071 ld r8, TAS_SEQCOUNT(r5)
3072 cmpdi r8, 0
3073 addi r8, r8, 1
3074 std r8, TAS_SEQCOUNT(r5)
3075 lwsync
3076 ld r7, TAS_TOTAL(r5)
3077 add r7, r7, r3
3078 std r7, TAS_TOTAL(r5)
3079 ld r6, TAS_MIN(r5)
3080 ld r7, TAS_MAX(r5)
3081 beq 3f
3082 cmpd r3, r6
3083 bge 1f
30843: std r3, TAS_MIN(r5)
30851: cmpd r3, r7
3086 ble 2f
3087 std r3, TAS_MAX(r5)
30882: lwsync
3089 addi r8, r8, 1
3090 std r8, TAS_SEQCOUNT(r5)
3091 blr
3092#endif