blob: 6f81adb112f1bf52543cd936b8992b9a98cfde00 [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>
Michael Neulinge4e38122014-03-25 10:47:02 +110033
34#define VCPU_GPRS_TM(reg) (((reg) * ULONG_SIZE) + VCPU_GPR_TM)
Paul Mackerrasde56a942011-06-29 00:21:34 +000035
Paul Mackerrase0b7ec02014-01-08 21:25:20 +110036/* Values in HSTATE_NAPPING(r13) */
37#define NAPPING_CEDE 1
38#define NAPPING_NOVCPU 2
39
Paul Mackerrasde56a942011-06-29 00:21:34 +000040/*
Paul Mackerras19ccb762011-07-23 17:42:46 +100041 * Call kvmppc_hv_entry in real mode.
Paul Mackerrasde56a942011-06-29 00:21:34 +000042 * Must be called with interrupts hard-disabled.
43 *
44 * Input Registers:
45 *
46 * LR = return address to continue at after eventually re-enabling MMU
47 */
Anton Blanchard6ed179b2014-06-12 18:16:53 +100048_GLOBAL_TOC(kvmppc_hv_entry_trampoline)
Paul Mackerras218309b2013-09-06 13:23:44 +100049 mflr r0
50 std r0, PPC_LR_STKOFF(r1)
51 stdu r1, -112(r1)
Paul Mackerrasde56a942011-06-29 00:21:34 +000052 mfmsr r10
Paul Mackerras218309b2013-09-06 13:23:44 +100053 LOAD_REG_ADDR(r5, kvmppc_call_hv_entry)
Paul Mackerrasde56a942011-06-29 00:21:34 +000054 li r0,MSR_RI
55 andc r0,r10,r0
56 li r6,MSR_IR | MSR_DR
57 andc r6,r10,r6
58 mtmsrd r0,1 /* clear RI in MSR */
59 mtsrr0 r5
60 mtsrr1 r6
61 RFI
62
Paul Mackerras218309b2013-09-06 13:23:44 +100063kvmppc_call_hv_entry:
Paul Mackerrase0b7ec02014-01-08 21:25:20 +110064 ld r4, HSTATE_KVM_VCPU(r13)
Paul Mackerras218309b2013-09-06 13:23:44 +100065 bl kvmppc_hv_entry
66
67 /* Back from guest - restore host state and return to caller */
68
Michael Neulingeee7ff92014-01-08 21:25:19 +110069BEGIN_FTR_SECTION
Paul Mackerras218309b2013-09-06 13:23:44 +100070 /* Restore host DABR and DABRX */
71 ld r5,HSTATE_DABR(r13)
72 li r6,7
73 mtspr SPRN_DABR,r5
74 mtspr SPRN_DABRX,r6
Michael Neulingeee7ff92014-01-08 21:25:19 +110075END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
Paul Mackerras218309b2013-09-06 13:23:44 +100076
77 /* Restore SPRG3 */
Scott Wood9d378df2014-03-10 17:29:38 -050078 ld r3,PACA_SPRG_VDSO(r13)
79 mtspr SPRN_SPRG_VDSO_WRITE,r3
Paul Mackerras218309b2013-09-06 13:23:44 +100080
Paul Mackerras218309b2013-09-06 13:23:44 +100081 /* Reload the host's PMU registers */
82 ld r3, PACALPPACAPTR(r13) /* is the host using the PMU? */
83 lbz r4, LPPACA_PMCINUSE(r3)
84 cmpwi r4, 0
85 beq 23f /* skip if not */
Paul Mackerras9bc01a92014-05-26 19:48:40 +100086BEGIN_FTR_SECTION
Michael Ellerman9a4fc4e2014-07-10 19:34:31 +100087 ld r3, HSTATE_MMCR0(r13)
Paul Mackerras9bc01a92014-05-26 19:48:40 +100088 andi. r4, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO
89 cmpwi r4, MMCR0_PMAO
90 beql kvmppc_fix_pmao
91END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG)
Michael Ellerman9a4fc4e2014-07-10 19:34:31 +100092 lwz r3, HSTATE_PMC1(r13)
93 lwz r4, HSTATE_PMC2(r13)
94 lwz r5, HSTATE_PMC3(r13)
95 lwz r6, HSTATE_PMC4(r13)
96 lwz r8, HSTATE_PMC5(r13)
97 lwz r9, HSTATE_PMC6(r13)
Paul Mackerras218309b2013-09-06 13:23:44 +100098 mtspr SPRN_PMC1, r3
99 mtspr SPRN_PMC2, r4
100 mtspr SPRN_PMC3, r5
101 mtspr SPRN_PMC4, r6
102 mtspr SPRN_PMC5, r8
103 mtspr SPRN_PMC6, r9
Michael Ellerman9a4fc4e2014-07-10 19:34:31 +1000104 ld r3, HSTATE_MMCR0(r13)
105 ld r4, HSTATE_MMCR1(r13)
106 ld r5, HSTATE_MMCRA(r13)
107 ld r6, HSTATE_SIAR(r13)
108 ld r7, HSTATE_SDAR(r13)
Paul Mackerras218309b2013-09-06 13:23:44 +1000109 mtspr SPRN_MMCR1, r4
110 mtspr SPRN_MMCRA, r5
Paul Mackerras72cde5a2014-03-25 10:47:08 +1100111 mtspr SPRN_SIAR, r6
112 mtspr SPRN_SDAR, r7
113BEGIN_FTR_SECTION
Michael Ellerman9a4fc4e2014-07-10 19:34:31 +1000114 ld r8, HSTATE_MMCR2(r13)
115 ld r9, HSTATE_SIER(r13)
Paul Mackerras72cde5a2014-03-25 10:47:08 +1100116 mtspr SPRN_MMCR2, r8
117 mtspr SPRN_SIER, r9
118END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerras218309b2013-09-06 13:23:44 +1000119 mtspr SPRN_MMCR0, r3
120 isync
12123:
122
123 /*
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100124 * Reload DEC. HDEC interrupts were disabled when
125 * we reloaded the host's LPCR value.
126 */
127 ld r3, HSTATE_DECEXP(r13)
128 mftb r4
129 subf r4, r4, r3
130 mtspr SPRN_DEC, r4
131
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000132 /* hwthread_req may have got set by cede or no vcpu, so clear it */
133 li r0, 0
134 stb r0, HSTATE_HWTHREAD_REQ(r13)
135
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100136 /*
Paul Mackerras218309b2013-09-06 13:23:44 +1000137 * For external and machine check interrupts, we need
138 * to call the Linux handler to process the interrupt.
139 * We do that by jumping to absolute address 0x500 for
140 * external interrupts, or the machine_check_fwnmi label
141 * for machine checks (since firmware might have patched
142 * the vector area at 0x200). The [h]rfid at the end of the
143 * handler will return to the book3s_hv_interrupts.S code.
144 * For other interrupts we do the rfid to get back
145 * to the book3s_hv_interrupts.S code here.
146 */
147 ld r8, 112+PPC_LR_STKOFF(r1)
148 addi r1, r1, 112
149 ld r7, HSTATE_HOST_MSR(r13)
150
151 cmpwi cr1, r12, BOOK3S_INTERRUPT_MACHINE_CHECK
152 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
Paul Mackerras218309b2013-09-06 13:23:44 +1000153 beq 11f
Gautham R. Shenoy70aa3962015-10-15 11:29:58 +0530154 cmpwi r12, BOOK3S_INTERRUPT_H_DOORBELL
155 beq 15f /* Invoke the H_DOORBELL handler */
Mahesh Salgaonkar0869b6f2014-07-29 18:40:01 +0530156 cmpwi cr2, r12, BOOK3S_INTERRUPT_HMI
157 beq cr2, 14f /* HMI check */
Paul Mackerras218309b2013-09-06 13:23:44 +1000158
159 /* RFI into the highmem handler, or branch to interrupt handler */
160 mfmsr r6
161 li r0, MSR_RI
162 andc r6, r6, r0
163 mtmsrd r6, 1 /* Clear RI in MSR */
164 mtsrr0 r8
165 mtsrr1 r7
Paul Mackerras218309b2013-09-06 13:23:44 +1000166 beq cr1, 13f /* machine check */
167 RFI
168
169 /* On POWER7, we have external interrupts set to use HSRR0/1 */
17011: mtspr SPRN_HSRR0, r8
171 mtspr SPRN_HSRR1, r7
172 ba 0x500
173
17413: b machine_check_fwnmi
175
Mahesh Salgaonkar0869b6f2014-07-29 18:40:01 +053017614: mtspr SPRN_HSRR0, r8
177 mtspr SPRN_HSRR1, r7
178 b hmi_exception_after_realmode
179
Gautham R. Shenoy70aa3962015-10-15 11:29:58 +053018015: mtspr SPRN_HSRR0, r8
181 mtspr SPRN_HSRR1, r7
182 ba 0xe80
183
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100184kvmppc_primary_no_guest:
185 /* We handle this much like a ceded vcpu */
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +1100186 /* put the HDEC into the DEC, since HDEC interrupts don't wake us */
187 mfspr r3, SPRN_HDEC
188 mtspr SPRN_DEC, r3
Paul Mackerras6af27c82015-03-28 14:21:10 +1100189 /*
190 * Make sure the primary has finished the MMU switch.
191 * We should never get here on a secondary thread, but
192 * check it for robustness' sake.
193 */
194 ld r5, HSTATE_KVM_VCORE(r13)
19565: lbz r0, VCORE_IN_GUEST(r5)
196 cmpwi r0, 0
197 beq 65b
198 /* Set LPCR. */
199 ld r8,VCORE_LPCR(r5)
200 mtspr SPRN_LPCR,r8
201 isync
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100202 /* set our bit in napping_threads */
203 ld r5, HSTATE_KVM_VCORE(r13)
204 lbz r7, HSTATE_PTID(r13)
205 li r0, 1
206 sld r0, r0, r7
207 addi r6, r5, VCORE_NAPPING_THREADS
2081: lwarx r3, 0, r6
209 or r3, r3, r0
210 stwcx. r3, 0, r6
211 bne 1b
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100212 /* order napping_threads update vs testing entry_exit_map */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100213 isync
214 li r12, 0
215 lwz r7, VCORE_ENTRY_EXIT(r5)
216 cmpwi r7, 0x100
217 bge kvm_novcpu_exit /* another thread already exiting */
218 li r3, NAPPING_NOVCPU
219 stb r3, HSTATE_NAPPING(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100220
Paul Mackerrasccc07772015-03-28 14:21:07 +1100221 li r3, 0 /* Don't wake on privileged (OS) doorbell */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100222 b kvm_do_nap
223
Suresh Warrier37f55d32016-08-19 15:35:46 +1000224/*
225 * kvm_novcpu_wakeup
226 * Entered from kvm_start_guest if kvm_hstate.napping is set
227 * to NAPPING_NOVCPU
228 * r2 = kernel TOC
229 * r13 = paca
230 */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100231kvm_novcpu_wakeup:
232 ld r1, HSTATE_HOST_R1(r13)
233 ld r5, HSTATE_KVM_VCORE(r13)
234 li r0, 0
235 stb r0, HSTATE_NAPPING(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100236
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100237 /* check the wake reason */
238 bl kvmppc_check_wake_reason
Paul Mackerras6af27c82015-03-28 14:21:10 +1100239
Suresh Warrier37f55d32016-08-19 15:35:46 +1000240 /*
241 * Restore volatile registers since we could have called
242 * a C routine in kvmppc_check_wake_reason.
243 * r5 = VCORE
244 */
245 ld r5, HSTATE_KVM_VCORE(r13)
246
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100247 /* see if any other thread is already exiting */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100248 lwz r0, VCORE_ENTRY_EXIT(r5)
249 cmpwi r0, 0x100
250 bge kvm_novcpu_exit
251
252 /* clear our bit in napping_threads */
253 lbz r7, HSTATE_PTID(r13)
254 li r0, 1
255 sld r0, r0, r7
256 addi r6, r5, VCORE_NAPPING_THREADS
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002574: lwarx r7, 0, r6
258 andc r7, r7, r0
259 stwcx. r7, 0, r6
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100260 bne 4b
261
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100262 /* See if the wake reason means we need to exit */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100263 cmpdi r3, 0
264 bge kvm_novcpu_exit
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100265
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +1100266 /* See if our timeslice has expired (HDEC is negative) */
267 mfspr r0, SPRN_HDEC
268 li r12, BOOK3S_INTERRUPT_HV_DECREMENTER
269 cmpwi r0, 0
270 blt kvm_novcpu_exit
271
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100272 /* Got an IPI but other vcpus aren't yet exiting, must be a latecomer */
273 ld r4, HSTATE_KVM_VCPU(r13)
274 cmpdi r4, 0
Paul Mackerrasb6c295d2015-03-28 14:21:02 +1100275 beq kvmppc_primary_no_guest
276
277#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
278 addi r3, r4, VCPU_TB_RMENTRY
279 bl kvmhv_start_timing
280#endif
281 b kvmppc_got_guest
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100282
283kvm_novcpu_exit:
Paul Mackerras6af27c82015-03-28 14:21:10 +1100284#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
285 ld r4, HSTATE_KVM_VCPU(r13)
286 cmpdi r4, 0
287 beq 13f
288 addi r3, r4, VCPU_TB_RMEXIT
289 bl kvmhv_accumulate_time
290#endif
Paul Mackerraseddb60f2015-03-28 14:21:11 +110029113: mr r3, r12
292 stw r12, 112-4(r1)
293 bl kvmhv_commence_exit
294 nop
295 lwz r12, 112-4(r1)
Paul Mackerras6af27c82015-03-28 14:21:10 +1100296 b kvmhv_switch_to_host
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100297
Paul Mackerras371fefd2011-06-29 00:23:08 +0000298/*
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100299 * We come in here when wakened from nap mode.
Paul Mackerras371fefd2011-06-29 00:23:08 +0000300 * Relocation is off and most register values are lost.
301 * r13 points to the PACA.
302 */
303 .globl kvm_start_guest
304kvm_start_guest:
Preeti U Murthyfd17dc72014-04-11 16:01:58 +0530305
306 /* Set runlatch bit the minute you wake up from nap */
Paul Mackerras1f09c3e2015-03-28 14:21:04 +1100307 mfspr r0, SPRN_CTRLF
308 ori r0, r0, 1
309 mtspr SPRN_CTRLT, r0
Preeti U Murthyfd17dc72014-04-11 16:01:58 +0530310
Paul Mackerras19ccb762011-07-23 17:42:46 +1000311 ld r2,PACATOC(r13)
312
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000313 li r0,KVM_HWTHREAD_IN_KVM
314 stb r0,HSTATE_HWTHREAD_STATE(r13)
315
316 /* NV GPR values from power7_idle() will no longer be valid */
317 li r0,1
318 stb r0,PACA_NAPSTATELOST(r13)
319
Paul Mackerras4619ac82013-04-17 20:31:41 +0000320 /* were we napping due to cede? */
321 lbz r0,HSTATE_NAPPING(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100322 cmpwi r0,NAPPING_CEDE
323 beq kvm_end_cede
324 cmpwi r0,NAPPING_NOVCPU
325 beq kvm_novcpu_wakeup
326
327 ld r1,PACAEMERGSP(r13)
328 subi r1,r1,STACK_FRAME_OVERHEAD
Paul Mackerras4619ac82013-04-17 20:31:41 +0000329
330 /*
331 * We weren't napping due to cede, so this must be a secondary
332 * thread being woken up to run a guest, or being woken up due
333 * to a stray IPI. (Or due to some machine check or hypervisor
334 * maintenance interrupt while the core is in KVM.)
335 */
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000336
337 /* Check the wake reason in SRR1 to see why we got here */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100338 bl kvmppc_check_wake_reason
Suresh Warrier37f55d32016-08-19 15:35:46 +1000339 /*
340 * kvmppc_check_wake_reason could invoke a C routine, but we
341 * have no volatile registers to restore when we return.
342 */
343
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100344 cmpdi r3, 0
345 bge kvm_no_guest
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000346
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000347 /* get vcore pointer, NULL if we have nothing to run */
348 ld r5,HSTATE_KVM_VCORE(r13)
349 cmpdi r5,0
350 /* if we have no vcore to run, go back to sleep */
Paul Mackerras7b444c62012-10-15 01:16:14 +0000351 beq kvm_no_guest
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000352
Paul Mackerras56548fc2014-12-03 14:48:40 +1100353kvm_secondary_got_guest:
354
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100355 /* Set HSTATE_DSCR(r13) to something sensible */
Anshuman Khandual1db36522015-05-21 12:13:03 +0530356 ld r6, PACA_DSCR_DEFAULT(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100357 std r6, HSTATE_DSCR(r13)
Paul Mackerras371fefd2011-06-29 00:23:08 +0000358
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000359 /* On thread 0 of a subcore, set HDEC to max */
360 lbz r4, HSTATE_PTID(r13)
361 cmpwi r4, 0
362 bne 63f
363 lis r6, 0x7fff
364 ori r6, r6, 0xffff
365 mtspr SPRN_HDEC, r6
366 /* and set per-LPAR registers, if doing dynamic micro-threading */
367 ld r6, HSTATE_SPLIT_MODE(r13)
368 cmpdi r6, 0
369 beq 63f
370 ld r0, KVM_SPLIT_RPR(r6)
371 mtspr SPRN_RPR, r0
372 ld r0, KVM_SPLIT_PMMAR(r6)
373 mtspr SPRN_PMMAR, r0
374 ld r0, KVM_SPLIT_LDBAR(r6)
375 mtspr SPRN_LDBAR, r0
376 isync
37763:
378 /* Order load of vcpu after load of vcore */
Paul Mackerras5d5b99c2015-03-28 14:21:06 +1100379 lwsync
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000380 ld r4, HSTATE_KVM_VCPU(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100381 bl kvmppc_hv_entry
Paul Mackerras218309b2013-09-06 13:23:44 +1000382
383 /* Back from the guest, go back to nap */
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000384 /* Clear our vcpu and vcore pointers so we don't come back in early */
Paul Mackerras218309b2013-09-06 13:23:44 +1000385 li r0, 0
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000386 std r0, HSTATE_KVM_VCPU(r13)
Paul Mackerrasf019b7a2013-11-16 17:46:03 +1100387 /*
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000388 * Once we clear HSTATE_KVM_VCORE(r13), the code in
Paul Mackerras5d5b99c2015-03-28 14:21:06 +1100389 * kvmppc_run_core() is going to assume that all our vcpu
390 * state is visible in memory. This lwsync makes sure
391 * that that is true.
Paul Mackerrasf019b7a2013-11-16 17:46:03 +1100392 */
Paul Mackerras218309b2013-09-06 13:23:44 +1000393 lwsync
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000394 std r0, HSTATE_KVM_VCORE(r13)
Paul Mackerras218309b2013-09-06 13:23:44 +1000395
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +0530396 /*
397 * All secondaries exiting guest will fall through this path.
398 * Before proceeding, just check for HMI interrupt and
399 * invoke opal hmi handler. By now we are sure that the
400 * primary thread on this core/subcore has already made partition
401 * switch/TB resync and we are good to call opal hmi handler.
402 */
403 cmpwi r12, BOOK3S_INTERRUPT_HMI
404 bne kvm_no_guest
405
406 li r3,0 /* NULL argument */
407 bl hmi_exception_realmode
Paul Mackerras56548fc2014-12-03 14:48:40 +1100408/*
409 * At this point we have finished executing in the guest.
410 * We need to wait for hwthread_req to become zero, since
411 * we may not turn on the MMU while hwthread_req is non-zero.
412 * While waiting we also need to check if we get given a vcpu to run.
413 */
Paul Mackerras218309b2013-09-06 13:23:44 +1000414kvm_no_guest:
Paul Mackerras56548fc2014-12-03 14:48:40 +1100415 lbz r3, HSTATE_HWTHREAD_REQ(r13)
416 cmpwi r3, 0
417 bne 53f
418 HMT_MEDIUM
419 li r0, KVM_HWTHREAD_IN_KERNEL
Paul Mackerras218309b2013-09-06 13:23:44 +1000420 stb r0, HSTATE_HWTHREAD_STATE(r13)
Paul Mackerras56548fc2014-12-03 14:48:40 +1100421 /* need to recheck hwthread_req after a barrier, to avoid race */
422 sync
423 lbz r3, HSTATE_HWTHREAD_REQ(r13)
424 cmpwi r3, 0
425 bne 54f
426/*
Shreyas B. Prabhu5fa6b6b2016-07-08 11:50:46 +0530427 * We jump to pnv_wakeup_loss, which will return to the caller
Paul Mackerras56548fc2014-12-03 14:48:40 +1100428 * of power7_nap in the powernv cpu offline loop. The value we
429 * put in r3 becomes the return value for power7_nap.
430 */
Paul Mackerras218309b2013-09-06 13:23:44 +1000431 li r3, LPCR_PECE0
432 mfspr r4, SPRN_LPCR
433 rlwimi r4, r3, 0, LPCR_PECE0 | LPCR_PECE1
434 mtspr SPRN_LPCR, r4
Paul Mackerras56548fc2014-12-03 14:48:40 +1100435 li r3, 0
Shreyas B. Prabhu5fa6b6b2016-07-08 11:50:46 +0530436 b pnv_wakeup_loss
Paul Mackerras56548fc2014-12-03 14:48:40 +1100437
43853: HMT_LOW
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000439 ld r5, HSTATE_KVM_VCORE(r13)
440 cmpdi r5, 0
441 bne 60f
442 ld r3, HSTATE_SPLIT_MODE(r13)
443 cmpdi r3, 0
444 beq kvm_no_guest
445 lbz r0, KVM_SPLIT_DO_NAP(r3)
446 cmpwi r0, 0
Paul Mackerras56548fc2014-12-03 14:48:40 +1100447 beq kvm_no_guest
448 HMT_MEDIUM
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000449 b kvm_unsplit_nap
45060: HMT_MEDIUM
Paul Mackerras56548fc2014-12-03 14:48:40 +1100451 b kvm_secondary_got_guest
452
45354: li r0, KVM_HWTHREAD_IN_KVM
454 stb r0, HSTATE_HWTHREAD_STATE(r13)
455 b kvm_no_guest
Paul Mackerras218309b2013-09-06 13:23:44 +1000456
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000457/*
458 * Here the primary thread is trying to return the core to
459 * whole-core mode, so we need to nap.
460 */
461kvm_unsplit_nap:
Gautham R. Shenoy7f235322015-09-02 21:48:58 +0530462 /*
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +0530463 * When secondaries are napping in kvm_unsplit_nap() with
464 * hwthread_req = 1, HMI goes ignored even though subcores are
465 * already exited the guest. Hence HMI keeps waking up secondaries
466 * from nap in a loop and secondaries always go back to nap since
467 * no vcore is assigned to them. This makes impossible for primary
468 * thread to get hold of secondary threads resulting into a soft
469 * lockup in KVM path.
470 *
471 * Let us check if HMI is pending and handle it before we go to nap.
472 */
473 cmpwi r12, BOOK3S_INTERRUPT_HMI
474 bne 55f
475 li r3, 0 /* NULL argument */
476 bl hmi_exception_realmode
47755:
478 /*
Gautham R. Shenoy7f235322015-09-02 21:48:58 +0530479 * Ensure that secondary doesn't nap when it has
480 * its vcore pointer set.
481 */
482 sync /* matches smp_mb() before setting split_info.do_nap */
483 ld r0, HSTATE_KVM_VCORE(r13)
484 cmpdi r0, 0
485 bne kvm_no_guest
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000486 /* clear any pending message */
487BEGIN_FTR_SECTION
488 lis r6, (PPC_DBELL_SERVER << (63-36))@h
489 PPC_MSGCLR(6)
490END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
491 /* Set kvm_split_mode.napped[tid] = 1 */
492 ld r3, HSTATE_SPLIT_MODE(r13)
493 li r0, 1
494 lhz r4, PACAPACAINDEX(r13)
495 clrldi r4, r4, 61 /* micro-threading => P8 => 8 threads/core */
496 addi r4, r4, KVM_SPLIT_NAPPED
497 stbx r0, r3, r4
498 /* Check the do_nap flag again after setting napped[] */
499 sync
500 lbz r0, KVM_SPLIT_DO_NAP(r3)
501 cmpwi r0, 0
502 beq 57f
503 li r3, (LPCR_PECEDH | LPCR_PECE0) >> 4
504 mfspr r4, SPRN_LPCR
505 rlwimi r4, r3, 4, (LPCR_PECEDP | LPCR_PECEDH | LPCR_PECE0 | LPCR_PECE1)
506 mtspr SPRN_LPCR, r4
507 isync
508 std r0, HSTATE_SCRATCH0(r13)
509 ptesync
510 ld r0, HSTATE_SCRATCH0(r13)
5111: cmpd r0, r0
512 bne 1b
513 nap
514 b .
515
51657: li r0, 0
517 stbx r0, r3, r4
518 b kvm_no_guest
519
Paul Mackerras218309b2013-09-06 13:23:44 +1000520/******************************************************************************
521 * *
522 * Entry code *
523 * *
524 *****************************************************************************/
525
Paul Mackerrasde56a942011-06-29 00:21:34 +0000526.global kvmppc_hv_entry
527kvmppc_hv_entry:
528
529 /* Required state:
530 *
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100531 * R4 = vcpu pointer (or NULL)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000532 * MSR = ~IR|DR
533 * R13 = PACA
534 * R1 = host R1
Michael Neuling06a29e42014-08-19 14:59:30 +1000535 * R2 = TOC
Paul Mackerrasde56a942011-06-29 00:21:34 +0000536 * all other volatile GPRS = free
537 */
538 mflr r0
Paul Mackerras218309b2013-09-06 13:23:44 +1000539 std r0, PPC_LR_STKOFF(r1)
540 stdu r1, -112(r1)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000541
Paul Mackerrasde56a942011-06-29 00:21:34 +0000542 /* Save R1 in the PACA */
543 std r1, HSTATE_HOST_R1(r13)
544
Paul Mackerras44a3add2013-10-04 21:45:04 +1000545 li r6, KVM_GUEST_MODE_HOST_HV
546 stb r6, HSTATE_IN_GUEST(r13)
547
Paul Mackerrasb6c295d2015-03-28 14:21:02 +1100548#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
549 /* Store initial timestamp */
550 cmpdi r4, 0
551 beq 1f
552 addi r3, r4, VCPU_TB_RMENTRY
553 bl kvmhv_start_timing
5541:
555#endif
Paul Mackerrasde56a942011-06-29 00:21:34 +0000556 /* Clear out SLB */
557 li r6,0
558 slbmte r6,r6
559 slbia
560 ptesync
561
Paul Mackerras9e368f22011-06-29 00:40:08 +0000562 /*
Paul Mackerrasc17b98c2014-12-03 13:30:38 +1100563 * POWER7/POWER8 host -> guest partition switch code.
Paul Mackerras9e368f22011-06-29 00:40:08 +0000564 * We don't have to lock against concurrent tlbies,
565 * but we do have to coordinate across hardware threads.
566 */
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100567 /* Set bit in entry map iff exit map is zero. */
568 ld r5, HSTATE_KVM_VCORE(r13)
569 li r7, 1
570 lbz r6, HSTATE_PTID(r13)
571 sld r7, r7, r6
572 addi r9, r5, VCORE_ENTRY_EXIT
57321: lwarx r3, 0, r9
574 cmpwi r3, 0x100 /* any threads starting to exit? */
Paul Mackerras371fefd2011-06-29 00:23:08 +0000575 bge secondary_too_late /* if so we're too late to the party */
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100576 or r3, r3, r7
577 stwcx. r3, 0, r9
Paul Mackerras371fefd2011-06-29 00:23:08 +0000578 bne 21b
579
580 /* Primary thread switches to guest partition. */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100581 ld r9,VCORE_KVM(r5) /* pointer to struct kvm */
Paul Mackerras371fefd2011-06-29 00:23:08 +0000582 cmpwi r6,0
Paul Mackerras6af27c82015-03-28 14:21:10 +1100583 bne 10f
Paul Mackerrasde56a942011-06-29 00:21:34 +0000584 ld r6,KVM_SDR1(r9)
585 lwz r7,KVM_LPID(r9)
586 li r0,LPID_RSVD /* switch to reserved LPID */
587 mtspr SPRN_LPID,r0
588 ptesync
589 mtspr SPRN_SDR1,r6 /* switch to partition page table */
590 mtspr SPRN_LPID,r7
591 isync
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000592
593 /* See if we need to flush the TLB */
594 lhz r6,PACAPACAINDEX(r13) /* test_bit(cpu, need_tlb_flush) */
595 clrldi r7,r6,64-6 /* extract bit number (6 bits) */
596 srdi r6,r6,6 /* doubleword number */
597 sldi r6,r6,3 /* address offset */
598 add r6,r6,r9
599 addi r6,r6,KVM_NEED_FLUSH /* dword in kvm->arch.need_tlb_flush */
Paul Mackerras371fefd2011-06-29 00:23:08 +0000600 li r0,1
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000601 sld r0,r0,r7
602 ld r7,0(r6)
603 and. r7,r7,r0
604 beq 22f
60523: ldarx r7,0,r6 /* if set, clear the bit */
606 andc r7,r7,r0
607 stdcx. r7,0,r6
608 bne 23b
Paul Mackerrasca252052014-01-08 21:25:22 +1100609 /* Flush the TLB of any entries for this LPID */
610 /* use arch 2.07S as a proxy for POWER8 */
611BEGIN_FTR_SECTION
612 li r6,512 /* POWER8 has 512 sets */
613FTR_SECTION_ELSE
614 li r6,128 /* POWER7 has 128 sets */
615ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000616 mtctr r6
617 li r7,0x800 /* IS field = 0b10 */
618 ptesync
61928: tlbiel r7
620 addi r7,r7,0x1000
621 bdnz 28b
622 ptesync
623
Paul Mackerras93b0f4d2013-09-06 13:17:46 +1000624 /* Add timebase offset onto timebase */
62522: ld r8,VCORE_TB_OFFSET(r5)
626 cmpdi r8,0
627 beq 37f
628 mftb r6 /* current host timebase */
629 add r8,r8,r6
630 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
631 mftb r7 /* check if lower 24 bits overflowed */
632 clrldi r6,r6,40
633 clrldi r7,r7,40
634 cmpld r7,r6
635 bge 37f
636 addis r8,r8,0x100 /* if so, increment upper 40 bits */
637 mtspr SPRN_TBU40,r8
638
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000639 /* Load guest PCR value to select appropriate compat mode */
64037: ld r7, VCORE_PCR(r5)
641 cmpdi r7, 0
642 beq 38f
643 mtspr SPRN_PCR, r7
64438:
Michael Neulingb005255e2014-01-08 21:25:21 +1100645
646BEGIN_FTR_SECTION
Paul Mackerras88b02cf92016-09-15 13:42:52 +1000647 /* DPDES and VTB are shared between threads */
Michael Neulingb005255e2014-01-08 21:25:21 +1100648 ld r8, VCORE_DPDES(r5)
Paul Mackerras88b02cf92016-09-15 13:42:52 +1000649 ld r7, VCORE_VTB(r5)
Michael Neulingb005255e2014-01-08 21:25:21 +1100650 mtspr SPRN_DPDES, r8
Paul Mackerras88b02cf92016-09-15 13:42:52 +1000651 mtspr SPRN_VTB, r7
Michael Neulingb005255e2014-01-08 21:25:21 +1100652END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
653
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +0530654 /* Mark the subcore state as inside guest */
655 bl kvmppc_subcore_enter_guest
656 nop
657 ld r5, HSTATE_KVM_VCORE(r13)
658 ld r4, HSTATE_KVM_VCPU(r13)
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000659 li r0,1
Paul Mackerras371fefd2011-06-29 00:23:08 +0000660 stb r0,VCORE_IN_GUEST(r5) /* signal secondaries to continue */
Paul Mackerras9e368f22011-06-29 00:40:08 +0000661
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100662 /* Do we have a guest vcpu to run? */
Paul Mackerras6af27c82015-03-28 14:21:10 +110066310: cmpdi r4, 0
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100664 beq kvmppc_primary_no_guest
665kvmppc_got_guest:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000666
667 /* Load up guest SLB entries */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100668 lwz r5,VCPU_SLB_MAX(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000669 cmpwi r5,0
670 beq 9f
671 mtctr r5
672 addi r6,r4,VCPU_SLB
6731: ld r8,VCPU_SLB_E(r6)
674 ld r9,VCPU_SLB_V(r6)
675 slbmte r9,r8
676 addi r6,r6,VCPU_SLB_SIZE
677 bdnz 1b
6789:
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100679 /* Increment yield count if they have a VPA */
680 ld r3, VCPU_VPA(r4)
681 cmpdi r3, 0
682 beq 25f
Alexander Graf0865a582014-06-11 10:36:17 +0200683 li r6, LPPACA_YIELDCOUNT
684 LWZX_BE r5, r3, r6
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100685 addi r5, r5, 1
Alexander Graf0865a582014-06-11 10:36:17 +0200686 STWX_BE r5, r3, r6
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100687 li r6, 1
688 stb r6, VCPU_VPA_DIRTY(r4)
68925:
690
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100691 /* Save purr/spurr */
692 mfspr r5,SPRN_PURR
693 mfspr r6,SPRN_SPURR
694 std r5,HSTATE_PURR(r13)
695 std r6,HSTATE_SPURR(r13)
696 ld r7,VCPU_PURR(r4)
697 ld r8,VCPU_SPURR(r4)
698 mtspr SPRN_PURR,r7
699 mtspr SPRN_SPURR,r8
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100700
Michael Neulingeee7ff92014-01-08 21:25:19 +1100701BEGIN_FTR_SECTION
Paul Mackerrasde56a942011-06-29 00:21:34 +0000702 /* Set partition DABR */
703 /* Do this before re-enabling PMU to avoid P7 DABR corruption bug */
Paul Mackerras8563bf52014-01-08 21:25:29 +1100704 lwz r5,VCPU_DABRX(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000705 ld r6,VCPU_DABR(r4)
706 mtspr SPRN_DABRX,r5
707 mtspr SPRN_DABR,r6
Paul Mackerrasde56a942011-06-29 00:21:34 +0000708 isync
Michael Neulingeee7ff92014-01-08 21:25:19 +1100709END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000710
Michael Neulinge4e38122014-03-25 10:47:02 +1100711#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
712BEGIN_FTR_SECTION
Paul Mackerrasf024ee02016-06-22 14:21:59 +1000713 bl kvmppc_restore_tm
714END_FTR_SECTION_IFSET(CPU_FTR_TM)
Michael Neulinge4e38122014-03-25 10:47:02 +1100715#endif
716
Paul Mackerrasde56a942011-06-29 00:21:34 +0000717 /* Load guest PMU registers */
718 /* R4 is live here (vcpu pointer) */
719 li r3, 1
720 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
721 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
722 isync
Paul Mackerras9bc01a92014-05-26 19:48:40 +1000723BEGIN_FTR_SECTION
724 ld r3, VCPU_MMCR(r4)
725 andi. r5, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO
726 cmpwi r5, MMCR0_PMAO
727 beql kvmppc_fix_pmao
728END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000729 lwz r3, VCPU_PMC(r4) /* always load up guest PMU registers */
730 lwz r5, VCPU_PMC + 4(r4) /* to prevent information leak */
731 lwz r6, VCPU_PMC + 8(r4)
732 lwz r7, VCPU_PMC + 12(r4)
733 lwz r8, VCPU_PMC + 16(r4)
734 lwz r9, VCPU_PMC + 20(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000735 mtspr SPRN_PMC1, r3
736 mtspr SPRN_PMC2, r5
737 mtspr SPRN_PMC3, r6
738 mtspr SPRN_PMC4, r7
739 mtspr SPRN_PMC5, r8
740 mtspr SPRN_PMC6, r9
Paul Mackerrasde56a942011-06-29 00:21:34 +0000741 ld r3, VCPU_MMCR(r4)
742 ld r5, VCPU_MMCR + 8(r4)
743 ld r6, VCPU_MMCR + 16(r4)
744 ld r7, VCPU_SIAR(r4)
745 ld r8, VCPU_SDAR(r4)
746 mtspr SPRN_MMCR1, r5
747 mtspr SPRN_MMCRA, r6
748 mtspr SPRN_SIAR, r7
749 mtspr SPRN_SDAR, r8
Michael Neulingb005255e2014-01-08 21:25:21 +1100750BEGIN_FTR_SECTION
751 ld r5, VCPU_MMCR + 24(r4)
752 ld r6, VCPU_SIER(r4)
753 lwz r7, VCPU_PMC + 24(r4)
754 lwz r8, VCPU_PMC + 28(r4)
755 ld r9, VCPU_MMCR + 32(r4)
756 mtspr SPRN_MMCR2, r5
757 mtspr SPRN_SIER, r6
758 mtspr SPRN_SPMC1, r7
759 mtspr SPRN_SPMC2, r8
760 mtspr SPRN_MMCRS, r9
761END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000762 mtspr SPRN_MMCR0, r3
763 isync
764
765 /* Load up FP, VMX and VSX registers */
766 bl kvmppc_load_fp
767
768 ld r14, VCPU_GPR(R14)(r4)
769 ld r15, VCPU_GPR(R15)(r4)
770 ld r16, VCPU_GPR(R16)(r4)
771 ld r17, VCPU_GPR(R17)(r4)
772 ld r18, VCPU_GPR(R18)(r4)
773 ld r19, VCPU_GPR(R19)(r4)
774 ld r20, VCPU_GPR(R20)(r4)
775 ld r21, VCPU_GPR(R21)(r4)
776 ld r22, VCPU_GPR(R22)(r4)
777 ld r23, VCPU_GPR(R23)(r4)
778 ld r24, VCPU_GPR(R24)(r4)
779 ld r25, VCPU_GPR(R25)(r4)
780 ld r26, VCPU_GPR(R26)(r4)
781 ld r27, VCPU_GPR(R27)(r4)
782 ld r28, VCPU_GPR(R28)(r4)
783 ld r29, VCPU_GPR(R29)(r4)
784 ld r30, VCPU_GPR(R30)(r4)
785 ld r31, VCPU_GPR(R31)(r4)
786
Paul Mackerrasde56a942011-06-29 00:21:34 +0000787 /* Switch DSCR to guest value */
788 ld r5, VCPU_DSCR(r4)
789 mtspr SPRN_DSCR, r5
Paul Mackerrasde56a942011-06-29 00:21:34 +0000790
Michael Neulingb005255e2014-01-08 21:25:21 +1100791BEGIN_FTR_SECTION
Paul Mackerrasc17b98c2014-12-03 13:30:38 +1100792 /* Skip next section on POWER7 */
Michael Neulingb005255e2014-01-08 21:25:21 +1100793 b 8f
794END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
Michael Neulingb005255e2014-01-08 21:25:21 +1100795 /* Load up POWER8-specific registers */
796 ld r5, VCPU_IAMR(r4)
797 lwz r6, VCPU_PSPB(r4)
798 ld r7, VCPU_FSCR(r4)
799 mtspr SPRN_IAMR, r5
800 mtspr SPRN_PSPB, r6
801 mtspr SPRN_FSCR, r7
802 ld r5, VCPU_DAWR(r4)
803 ld r6, VCPU_DAWRX(r4)
804 ld r7, VCPU_CIABR(r4)
805 ld r8, VCPU_TAR(r4)
806 mtspr SPRN_DAWR, r5
807 mtspr SPRN_DAWRX, r6
808 mtspr SPRN_CIABR, r7
809 mtspr SPRN_TAR, r8
810 ld r5, VCPU_IC(r4)
Michael Neuling7b490412014-01-08 21:25:32 +1100811 ld r8, VCPU_EBBHR(r4)
Paul Mackerras88b02cf92016-09-15 13:42:52 +1000812 mtspr SPRN_IC, r5
Michael Neulingb005255e2014-01-08 21:25:21 +1100813 mtspr SPRN_EBBHR, r8
814 ld r5, VCPU_EBBRR(r4)
815 ld r6, VCPU_BESCR(r4)
816 ld r7, VCPU_CSIGR(r4)
817 ld r8, VCPU_TACR(r4)
818 mtspr SPRN_EBBRR, r5
819 mtspr SPRN_BESCR, r6
820 mtspr SPRN_CSIGR, r7
821 mtspr SPRN_TACR, r8
822 ld r5, VCPU_TCSCR(r4)
823 ld r6, VCPU_ACOP(r4)
824 lwz r7, VCPU_GUEST_PID(r4)
825 ld r8, VCPU_WORT(r4)
826 mtspr SPRN_TCSCR, r5
827 mtspr SPRN_ACOP, r6
828 mtspr SPRN_PID, r7
829 mtspr SPRN_WORT, r8
8308:
831
Paul Mackerrasde56a942011-06-29 00:21:34 +0000832 /*
833 * Set the decrementer to the guest decrementer.
834 */
835 ld r8,VCPU_DEC_EXPIRES(r4)
Paul Mackerrasc5fb80d2014-03-25 10:47:07 +1100836 /* r8 is a host timebase value here, convert to guest TB */
837 ld r5,HSTATE_KVM_VCORE(r13)
838 ld r6,VCORE_TB_OFFSET(r5)
839 add r8,r8,r6
Paul Mackerrasde56a942011-06-29 00:21:34 +0000840 mftb r7
841 subf r3,r7,r8
842 mtspr SPRN_DEC,r3
843 stw r3,VCPU_DEC(r4)
844
845 ld r5, VCPU_SPRG0(r4)
846 ld r6, VCPU_SPRG1(r4)
847 ld r7, VCPU_SPRG2(r4)
848 ld r8, VCPU_SPRG3(r4)
849 mtspr SPRN_SPRG0, r5
850 mtspr SPRN_SPRG1, r6
851 mtspr SPRN_SPRG2, r7
852 mtspr SPRN_SPRG3, r8
853
Paul Mackerrasde56a942011-06-29 00:21:34 +0000854 /* Load up DAR and DSISR */
855 ld r5, VCPU_DAR(r4)
856 lwz r6, VCPU_DSISR(r4)
857 mtspr SPRN_DAR, r5
858 mtspr SPRN_DSISR, r6
859
Paul Mackerrasde56a942011-06-29 00:21:34 +0000860 /* Restore AMR and UAMOR, set AMOR to all 1s */
861 ld r5,VCPU_AMR(r4)
862 ld r6,VCPU_UAMOR(r4)
863 li r7,-1
864 mtspr SPRN_AMR,r5
865 mtspr SPRN_UAMOR,r6
866 mtspr SPRN_AMOR,r7
Paul Mackerrasde56a942011-06-29 00:21:34 +0000867
868 /* Restore state of CTRL run bit; assume 1 on entry */
869 lwz r5,VCPU_CTRL(r4)
870 andi. r5,r5,1
871 bne 4f
872 mfspr r6,SPRN_CTRLF
873 clrrdi r6,r6,1
874 mtspr SPRN_CTRLT,r6
8754:
Paul Mackerras6af27c82015-03-28 14:21:10 +1100876 /* Secondary threads wait for primary to have done partition switch */
877 ld r5, HSTATE_KVM_VCORE(r13)
878 lbz r6, HSTATE_PTID(r13)
879 cmpwi r6, 0
880 beq 21f
881 lbz r0, VCORE_IN_GUEST(r5)
882 cmpwi r0, 0
883 bne 21f
884 HMT_LOW
Paul Mackerrasb4deba52015-07-02 20:38:16 +100088520: lwz r3, VCORE_ENTRY_EXIT(r5)
886 cmpwi r3, 0x100
887 bge no_switch_exit
888 lbz r0, VCORE_IN_GUEST(r5)
Paul Mackerras6af27c82015-03-28 14:21:10 +1100889 cmpwi r0, 0
890 beq 20b
891 HMT_MEDIUM
89221:
893 /* Set LPCR. */
894 ld r8,VCORE_LPCR(r5)
895 mtspr SPRN_LPCR,r8
896 isync
897
898 /* Check if HDEC expires soon */
899 mfspr r3, SPRN_HDEC
900 cmpwi r3, 512 /* 1 microsecond */
901 blt hdec_soon
902
Suresh Warrier37f55d32016-08-19 15:35:46 +1000903deliver_guest_interrupt:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000904 ld r6, VCPU_CTR(r4)
Sam bobroffc63517c2015-05-27 09:56:57 +1000905 ld r7, VCPU_XER(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000906
907 mtctr r6
908 mtxer r7
909
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100910kvmppc_cede_reentry: /* r4 = vcpu, r13 = paca */
Paul Mackerras4619ac82013-04-17 20:31:41 +0000911 ld r10, VCPU_PC(r4)
912 ld r11, VCPU_MSR(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000913 ld r6, VCPU_SRR0(r4)
914 ld r7, VCPU_SRR1(r4)
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100915 mtspr SPRN_SRR0, r6
916 mtspr SPRN_SRR1, r7
Paul Mackerrasde56a942011-06-29 00:21:34 +0000917
Paul Mackerras4619ac82013-04-17 20:31:41 +0000918 /* r11 = vcpu->arch.msr & ~MSR_HV */
Paul Mackerrasde56a942011-06-29 00:21:34 +0000919 rldicl r11, r11, 63 - MSR_HV_LG, 1
920 rotldi r11, r11, 1 + MSR_HV_LG
921 ori r11, r11, MSR_ME
922
Paul Mackerras19ccb762011-07-23 17:42:46 +1000923 /* Check if we can deliver an external or decrementer interrupt now */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100924 ld r0, VCPU_PENDING_EXC(r4)
925 rldicl r0, r0, 64 - BOOK3S_IRQPRIO_EXTERNAL_LEVEL, 63
926 cmpdi cr1, r0, 0
927 andi. r8, r11, MSR_EE
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100928 mfspr r8, SPRN_LPCR
929 /* Insert EXTERNAL_LEVEL bit into LPCR at the MER bit position */
930 rldimi r8, r0, LPCR_MER_SH, 63 - LPCR_MER_SH
931 mtspr SPRN_LPCR, r8
Paul Mackerras19ccb762011-07-23 17:42:46 +1000932 isync
Paul Mackerras19ccb762011-07-23 17:42:46 +1000933 beq 5f
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100934 li r0, BOOK3S_INTERRUPT_EXTERNAL
935 bne cr1, 12f
936 mfspr r0, SPRN_DEC
937 cmpwi r0, 0
938 li r0, BOOK3S_INTERRUPT_DECREMENTER
939 bge 5f
940
94112: mtspr SPRN_SRR0, r10
Paul Mackerras19ccb762011-07-23 17:42:46 +1000942 mr r10,r0
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100943 mtspr SPRN_SRR1, r11
Michael Neulinge4e38122014-03-25 10:47:02 +1100944 mr r9, r4
945 bl kvmppc_msr_interrupt
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11009465:
Paul Mackerras19ccb762011-07-23 17:42:46 +1000947
Liu Ping Fan27025a62013-11-19 14:12:48 +0800948/*
949 * Required state:
950 * R4 = vcpu
951 * R10: value for HSRR0
952 * R11: value for HSRR1
953 * R13 = PACA
954 */
Paul Mackerrasde56a942011-06-29 00:21:34 +0000955fast_guest_return:
Paul Mackerras4619ac82013-04-17 20:31:41 +0000956 li r0,0
957 stb r0,VCPU_CEDED(r4) /* cancel cede */
Paul Mackerrasde56a942011-06-29 00:21:34 +0000958 mtspr SPRN_HSRR0,r10
959 mtspr SPRN_HSRR1,r11
960
961 /* Activate guest mode, so faults get handled by KVM */
Paul Mackerras44a3add2013-10-04 21:45:04 +1000962 li r9, KVM_GUEST_MODE_GUEST_HV
Paul Mackerrasde56a942011-06-29 00:21:34 +0000963 stb r9, HSTATE_IN_GUEST(r13)
964
Paul Mackerrasb6c295d2015-03-28 14:21:02 +1100965#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
966 /* Accumulate timing */
967 addi r3, r4, VCPU_TB_GUEST
968 bl kvmhv_accumulate_time
969#endif
970
Paul Mackerrasde56a942011-06-29 00:21:34 +0000971 /* Enter guest */
972
Paul Mackerras0acb9112013-02-04 18:10:51 +0000973BEGIN_FTR_SECTION
974 ld r5, VCPU_CFAR(r4)
975 mtspr SPRN_CFAR, r5
976END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
Paul Mackerras4b8473c2013-09-20 14:52:39 +1000977BEGIN_FTR_SECTION
978 ld r0, VCPU_PPR(r4)
979END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
Paul Mackerras0acb9112013-02-04 18:10:51 +0000980
Paul Mackerrasde56a942011-06-29 00:21:34 +0000981 ld r5, VCPU_LR(r4)
982 lwz r6, VCPU_CR(r4)
983 mtlr r5
984 mtcr r6
985
Michael Neulingc75df6f2012-06-25 13:33:10 +0000986 ld r1, VCPU_GPR(R1)(r4)
987 ld r2, VCPU_GPR(R2)(r4)
988 ld r3, VCPU_GPR(R3)(r4)
989 ld r5, VCPU_GPR(R5)(r4)
990 ld r6, VCPU_GPR(R6)(r4)
991 ld r7, VCPU_GPR(R7)(r4)
992 ld r8, VCPU_GPR(R8)(r4)
993 ld r9, VCPU_GPR(R9)(r4)
994 ld r10, VCPU_GPR(R10)(r4)
995 ld r11, VCPU_GPR(R11)(r4)
996 ld r12, VCPU_GPR(R12)(r4)
997 ld r13, VCPU_GPR(R13)(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000998
Paul Mackerras4b8473c2013-09-20 14:52:39 +1000999BEGIN_FTR_SECTION
1000 mtspr SPRN_PPR, r0
1001END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
1002 ld r0, VCPU_GPR(R0)(r4)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001003 ld r4, VCPU_GPR(R4)(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001004
1005 hrfid
1006 b .
1007
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001008secondary_too_late:
Paul Mackerras6af27c82015-03-28 14:21:10 +11001009 li r12, 0
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001010 cmpdi r4, 0
1011 beq 11f
Paul Mackerras6af27c82015-03-28 14:21:10 +11001012 stw r12, VCPU_TRAP(r4)
1013#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001014 addi r3, r4, VCPU_TB_RMEXIT
1015 bl kvmhv_accumulate_time
Paul Mackerras6af27c82015-03-28 14:21:10 +11001016#endif
Paul Mackerrasb6c295d2015-03-28 14:21:02 +1100101711: b kvmhv_switch_to_host
1018
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001019no_switch_exit:
1020 HMT_MEDIUM
1021 li r12, 0
1022 b 12f
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001023hdec_soon:
Paul Mackerras6af27c82015-03-28 14:21:10 +11001024 li r12, BOOK3S_INTERRUPT_HV_DECREMENTER
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000102512: stw r12, VCPU_TRAP(r4)
Paul Mackerras6af27c82015-03-28 14:21:10 +11001026 mr r9, r4
1027#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001028 addi r3, r4, VCPU_TB_RMEXIT
1029 bl kvmhv_accumulate_time
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001030#endif
Paul Mackerras6af27c82015-03-28 14:21:10 +11001031 b guest_exit_cont
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001032
Paul Mackerrasde56a942011-06-29 00:21:34 +00001033/******************************************************************************
1034 * *
1035 * Exit code *
1036 * *
1037 *****************************************************************************/
1038
1039/*
1040 * We come here from the first-level interrupt handlers.
1041 */
Aneesh Kumar K.Vdd96b2c2013-10-07 22:17:55 +05301042 .globl kvmppc_interrupt_hv
1043kvmppc_interrupt_hv:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001044 /*
1045 * Register contents:
1046 * R12 = interrupt vector
1047 * R13 = PACA
1048 * guest CR, R12 saved in shadow VCPU SCRATCH1/0
1049 * guest R13 saved in SPRN_SCRATCH0
1050 */
Aneesh Kumar K.V36e7bb32013-11-11 19:29:47 +05301051 std r9, HSTATE_SCRATCH2(r13)
Paul Mackerras44a3add2013-10-04 21:45:04 +10001052
1053 lbz r9, HSTATE_IN_GUEST(r13)
1054 cmpwi r9, KVM_GUEST_MODE_HOST_HV
1055 beq kvmppc_bad_host_intr
Aneesh Kumar K.Vdd96b2c2013-10-07 22:17:55 +05301056#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1057 cmpwi r9, KVM_GUEST_MODE_GUEST
Aneesh Kumar K.V36e7bb32013-11-11 19:29:47 +05301058 ld r9, HSTATE_SCRATCH2(r13)
Aneesh Kumar K.Vdd96b2c2013-10-07 22:17:55 +05301059 beq kvmppc_interrupt_pr
1060#endif
Paul Mackerras44a3add2013-10-04 21:45:04 +10001061 /* We're now back in the host but in guest MMU context */
1062 li r9, KVM_GUEST_MODE_HOST_HV
1063 stb r9, HSTATE_IN_GUEST(r13)
1064
Paul Mackerrasde56a942011-06-29 00:21:34 +00001065 ld r9, HSTATE_KVM_VCPU(r13)
1066
1067 /* Save registers */
1068
Michael Neulingc75df6f2012-06-25 13:33:10 +00001069 std r0, VCPU_GPR(R0)(r9)
1070 std r1, VCPU_GPR(R1)(r9)
1071 std r2, VCPU_GPR(R2)(r9)
1072 std r3, VCPU_GPR(R3)(r9)
1073 std r4, VCPU_GPR(R4)(r9)
1074 std r5, VCPU_GPR(R5)(r9)
1075 std r6, VCPU_GPR(R6)(r9)
1076 std r7, VCPU_GPR(R7)(r9)
1077 std r8, VCPU_GPR(R8)(r9)
Aneesh Kumar K.V36e7bb32013-11-11 19:29:47 +05301078 ld r0, HSTATE_SCRATCH2(r13)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001079 std r0, VCPU_GPR(R9)(r9)
1080 std r10, VCPU_GPR(R10)(r9)
1081 std r11, VCPU_GPR(R11)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001082 ld r3, HSTATE_SCRATCH0(r13)
1083 lwz r4, HSTATE_SCRATCH1(r13)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001084 std r3, VCPU_GPR(R12)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001085 stw r4, VCPU_CR(r9)
Paul Mackerras0acb9112013-02-04 18:10:51 +00001086BEGIN_FTR_SECTION
1087 ld r3, HSTATE_CFAR(r13)
1088 std r3, VCPU_CFAR(r9)
1089END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001090BEGIN_FTR_SECTION
1091 ld r4, HSTATE_PPR(r13)
1092 std r4, VCPU_PPR(r9)
1093END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001094
1095 /* Restore R1/R2 so we can handle faults */
1096 ld r1, HSTATE_HOST_R1(r13)
1097 ld r2, PACATOC(r13)
1098
1099 mfspr r10, SPRN_SRR0
1100 mfspr r11, SPRN_SRR1
1101 std r10, VCPU_SRR0(r9)
1102 std r11, VCPU_SRR1(r9)
1103 andi. r0, r12, 2 /* need to read HSRR0/1? */
1104 beq 1f
1105 mfspr r10, SPRN_HSRR0
1106 mfspr r11, SPRN_HSRR1
1107 clrrdi r12, r12, 2
11081: std r10, VCPU_PC(r9)
1109 std r11, VCPU_MSR(r9)
1110
1111 GET_SCRATCH0(r3)
1112 mflr r4
Michael Neulingc75df6f2012-06-25 13:33:10 +00001113 std r3, VCPU_GPR(R13)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001114 std r4, VCPU_LR(r9)
1115
Paul Mackerrasde56a942011-06-29 00:21:34 +00001116 stw r12,VCPU_TRAP(r9)
1117
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001118#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1119 addi r3, r9, VCPU_TB_RMINTR
1120 mr r4, r9
1121 bl kvmhv_accumulate_time
1122 ld r5, VCPU_GPR(R5)(r9)
1123 ld r6, VCPU_GPR(R6)(r9)
1124 ld r7, VCPU_GPR(R7)(r9)
1125 ld r8, VCPU_GPR(R8)(r9)
1126#endif
1127
Paul Mackerras4a157d62014-12-03 13:30:39 +11001128 /* Save HEIR (HV emulation assist reg) in emul_inst
Paul Mackerras697d3892011-12-12 12:36:37 +00001129 if this is an HEI (HV emulation interrupt, e40) */
1130 li r3,KVM_INST_FETCH_FAILED
Paul Mackerras2bf27602015-03-20 20:39:40 +11001131 stw r3,VCPU_LAST_INST(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001132 cmpwi r12,BOOK3S_INTERRUPT_H_EMUL_ASSIST
1133 bne 11f
1134 mfspr r3,SPRN_HEIR
Paul Mackerras4a157d62014-12-03 13:30:39 +1100113511: stw r3,VCPU_HEIR(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001136
1137 /* these are volatile across C function calls */
1138 mfctr r3
1139 mfxer r4
1140 std r3, VCPU_CTR(r9)
Sam bobroffc63517c2015-05-27 09:56:57 +10001141 std r4, VCPU_XER(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001142
Paul Mackerras697d3892011-12-12 12:36:37 +00001143 /* If this is a page table miss then see if it's theirs or ours */
1144 cmpwi r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1145 beq kvmppc_hdsi
Paul Mackerras342d3db2011-12-12 12:38:05 +00001146 cmpwi r12, BOOK3S_INTERRUPT_H_INST_STORAGE
1147 beq kvmppc_hisi
Paul Mackerras697d3892011-12-12 12:36:37 +00001148
Paul Mackerrasde56a942011-06-29 00:21:34 +00001149 /* See if this is a leftover HDEC interrupt */
1150 cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER
1151 bne 2f
1152 mfspr r3,SPRN_HDEC
1153 cmpwi r3,0
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001154 mr r4,r9
1155 bge fast_guest_return
Paul Mackerrasde56a942011-06-29 00:21:34 +000011562:
Paul Mackerras697d3892011-12-12 12:36:37 +00001157 /* See if this is an hcall we can handle in real mode */
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001158 cmpwi r12,BOOK3S_INTERRUPT_SYSCALL
1159 beq hcall_try_real_mode
Paul Mackerrasde56a942011-06-29 00:21:34 +00001160
Paul Mackerras66feed62015-03-28 14:21:12 +11001161 /* Hypervisor doorbell - exit only if host IPI flag set */
1162 cmpwi r12, BOOK3S_INTERRUPT_H_DOORBELL
1163 bne 3f
1164 lbz r0, HSTATE_HOST_IPI(r13)
Gautham R. Shenoy06554d92015-08-07 17:41:20 +05301165 cmpwi r0, 0
Paul Mackerras66feed62015-03-28 14:21:12 +11001166 beq 4f
1167 b guest_exit_cont
11683:
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +00001169 /* External interrupt ? */
1170 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001171 bne+ guest_exit_cont
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +00001172
1173 /* External interrupt, first check for host_ipi. If this is
1174 * set, we know the host wants us out so let's do it now
1175 */
Paul Mackerrasc9342432013-09-06 13:24:13 +10001176 bl kvmppc_read_intr
Suresh Warrier37f55d32016-08-19 15:35:46 +10001177
1178 /*
1179 * Restore the active volatile registers after returning from
1180 * a C function.
1181 */
1182 ld r9, HSTATE_KVM_VCPU(r13)
1183 li r12, BOOK3S_INTERRUPT_EXTERNAL
1184
1185 /*
1186 * kvmppc_read_intr return codes:
1187 *
1188 * Exit to host (r3 > 0)
1189 * 1 An interrupt is pending that needs to be handled by the host
1190 * Exit guest and return to host by branching to guest_exit_cont
1191 *
Suresh Warrierf7af5202016-08-19 15:35:52 +10001192 * 2 Passthrough that needs completion in the host
1193 * Exit guest and return to host by branching to guest_exit_cont
1194 * However, we also set r12 to BOOK3S_INTERRUPT_HV_RM_HARD
1195 * to indicate to the host to complete handling the interrupt
1196 *
Suresh Warrier37f55d32016-08-19 15:35:46 +10001197 * Before returning to guest, we check if any CPU is heading out
1198 * to the host and if so, we head out also. If no CPUs are heading
1199 * check return values <= 0.
1200 *
1201 * Return to guest (r3 <= 0)
1202 * 0 No external interrupt is pending
1203 * -1 A guest wakeup IPI (which has now been cleared)
1204 * In either case, we return to guest to deliver any pending
1205 * guest interrupts.
Suresh Warriere3c13e52016-08-19 15:35:51 +10001206 *
1207 * -2 A PCI passthrough external interrupt was handled
1208 * (interrupt was delivered directly to guest)
1209 * Return to guest to deliver any pending guest interrupts.
Suresh Warrier37f55d32016-08-19 15:35:46 +10001210 */
1211
Suresh Warrierf7af5202016-08-19 15:35:52 +10001212 cmpdi r3, 1
1213 ble 1f
1214
1215 /* Return code = 2 */
1216 li r12, BOOK3S_INTERRUPT_HV_RM_HARD
1217 stw r12, VCPU_TRAP(r9)
1218 b guest_exit_cont
1219
12201: /* Return code <= 1 */
Paul Mackerrasc9342432013-09-06 13:24:13 +10001221 cmpdi r3, 0
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001222 bgt guest_exit_cont
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +00001223
Suresh Warrier37f55d32016-08-19 15:35:46 +10001224 /* Return code <= 0 */
Paul Mackerras66feed62015-03-28 14:21:12 +110012254: ld r5, HSTATE_KVM_VCORE(r13)
Paul Mackerras4619ac82013-04-17 20:31:41 +00001226 lwz r0, VCORE_ENTRY_EXIT(r5)
1227 cmpwi r0, 0x100
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001228 mr r4, r9
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001229 blt deliver_guest_interrupt
Paul Mackerrasde56a942011-06-29 00:21:34 +00001230
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001231guest_exit_cont: /* r9 = vcpu, r12 = trap, r13 = paca */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001232 /* Save more register state */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001233 mfdar r6
1234 mfdsisr r7
Paul Mackerrasde56a942011-06-29 00:21:34 +00001235 std r6, VCPU_DAR(r9)
1236 stw r7, VCPU_DSISR(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001237 /* don't overwrite fault_dar/fault_dsisr if HDSI */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001238 cmpwi r12,BOOK3S_INTERRUPT_H_DATA_STORAGE
Paul Mackerras6af27c82015-03-28 14:21:10 +11001239 beq mc_cont
Paul Mackerras697d3892011-12-12 12:36:37 +00001240 std r6, VCPU_FAULT_DAR(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001241 stw r7, VCPU_FAULT_DSISR(r9)
1242
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001243 /* See if it is a machine check */
1244 cmpwi r12, BOOK3S_INTERRUPT_MACHINE_CHECK
1245 beq machine_check_realmode
1246mc_cont:
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001247#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1248 addi r3, r9, VCPU_TB_RMEXIT
1249 mr r4, r9
1250 bl kvmhv_accumulate_time
1251#endif
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001252
Gautham R. Shenoy7e022e72015-05-21 13:57:04 +05301253 mr r3, r12
Paul Mackerras6af27c82015-03-28 14:21:10 +11001254 /* Increment exit count, poke other threads to exit */
1255 bl kvmhv_commence_exit
Paul Mackerraseddb60f2015-03-28 14:21:11 +11001256 nop
1257 ld r9, HSTATE_KVM_VCPU(r13)
1258 lwz r12, VCPU_TRAP(r9)
Paul Mackerras6af27c82015-03-28 14:21:10 +11001259
Paul Mackerrasec257162015-06-24 21:18:03 +10001260 /* Stop others sending VCPU interrupts to this physical CPU */
1261 li r0, -1
1262 stw r0, VCPU_CPU(r9)
1263 stw r0, VCPU_THREAD_CPU(r9)
1264
Paul Mackerrasde56a942011-06-29 00:21:34 +00001265 /* Save guest CTRL register, set runlatch to 1 */
Paul Mackerras6af27c82015-03-28 14:21:10 +11001266 mfspr r6,SPRN_CTRLF
Paul Mackerrasde56a942011-06-29 00:21:34 +00001267 stw r6,VCPU_CTRL(r9)
1268 andi. r0,r6,1
1269 bne 4f
1270 ori r6,r6,1
1271 mtspr SPRN_CTRLT,r6
12724:
1273 /* Read the guest SLB and save it away */
1274 lwz r0,VCPU_SLB_NR(r9) /* number of entries in SLB */
1275 mtctr r0
1276 li r6,0
1277 addi r7,r9,VCPU_SLB
1278 li r5,0
12791: slbmfee r8,r6
1280 andis. r0,r8,SLB_ESID_V@h
1281 beq 2f
1282 add r8,r8,r6 /* put index in */
1283 slbmfev r3,r6
1284 std r8,VCPU_SLB_E(r7)
1285 std r3,VCPU_SLB_V(r7)
1286 addi r7,r7,VCPU_SLB_SIZE
1287 addi r5,r5,1
12882: addi r6,r6,1
1289 bdnz 1b
1290 stw r5,VCPU_SLB_MAX(r9)
1291
1292 /*
1293 * Save the guest PURR/SPURR
1294 */
1295 mfspr r5,SPRN_PURR
1296 mfspr r6,SPRN_SPURR
1297 ld r7,VCPU_PURR(r9)
1298 ld r8,VCPU_SPURR(r9)
1299 std r5,VCPU_PURR(r9)
1300 std r6,VCPU_SPURR(r9)
1301 subf r5,r7,r5
1302 subf r6,r8,r6
1303
1304 /*
1305 * Restore host PURR/SPURR and add guest times
1306 * so that the time in the guest gets accounted.
1307 */
1308 ld r3,HSTATE_PURR(r13)
1309 ld r4,HSTATE_SPURR(r13)
1310 add r3,r3,r5
1311 add r4,r4,r6
1312 mtspr SPRN_PURR,r3
1313 mtspr SPRN_SPURR,r4
1314
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001315 /* Save DEC */
1316 mfspr r5,SPRN_DEC
1317 mftb r6
1318 extsw r5,r5
1319 add r5,r5,r6
Paul Mackerrasc5fb80d2014-03-25 10:47:07 +11001320 /* r5 is a guest timebase value here, convert to host TB */
1321 ld r3,HSTATE_KVM_VCORE(r13)
1322 ld r4,VCORE_TB_OFFSET(r3)
1323 subf r5,r4,r5
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001324 std r5,VCPU_DEC_EXPIRES(r9)
1325
Michael Neulingb005255e2014-01-08 21:25:21 +11001326BEGIN_FTR_SECTION
1327 b 8f
1328END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
Michael Neulingb005255e2014-01-08 21:25:21 +11001329 /* Save POWER8-specific registers */
1330 mfspr r5, SPRN_IAMR
1331 mfspr r6, SPRN_PSPB
1332 mfspr r7, SPRN_FSCR
1333 std r5, VCPU_IAMR(r9)
1334 stw r6, VCPU_PSPB(r9)
1335 std r7, VCPU_FSCR(r9)
1336 mfspr r5, SPRN_IC
Michael Neulingb005255e2014-01-08 21:25:21 +11001337 mfspr r7, SPRN_TAR
1338 std r5, VCPU_IC(r9)
Michael Neulingb005255e2014-01-08 21:25:21 +11001339 std r7, VCPU_TAR(r9)
Michael Neuling7b490412014-01-08 21:25:32 +11001340 mfspr r8, SPRN_EBBHR
Michael Neulingb005255e2014-01-08 21:25:21 +11001341 std r8, VCPU_EBBHR(r9)
1342 mfspr r5, SPRN_EBBRR
1343 mfspr r6, SPRN_BESCR
1344 mfspr r7, SPRN_CSIGR
1345 mfspr r8, SPRN_TACR
1346 std r5, VCPU_EBBRR(r9)
1347 std r6, VCPU_BESCR(r9)
1348 std r7, VCPU_CSIGR(r9)
1349 std r8, VCPU_TACR(r9)
1350 mfspr r5, SPRN_TCSCR
1351 mfspr r6, SPRN_ACOP
1352 mfspr r7, SPRN_PID
1353 mfspr r8, SPRN_WORT
1354 std r5, VCPU_TCSCR(r9)
1355 std r6, VCPU_ACOP(r9)
1356 stw r7, VCPU_GUEST_PID(r9)
1357 std r8, VCPU_WORT(r9)
Paul Mackerrasccec4452016-03-05 19:34:39 +11001358 /*
1359 * Restore various registers to 0, where non-zero values
1360 * set by the guest could disrupt the host.
1361 */
1362 li r0, 0
1363 mtspr SPRN_IAMR, r0
1364 mtspr SPRN_CIABR, r0
1365 mtspr SPRN_DAWRX, r0
1366 mtspr SPRN_TCSCR, r0
1367 mtspr SPRN_WORT, r0
1368 /* Set MMCRS to 1<<31 to freeze and disable the SPMC counters */
1369 li r0, 1
1370 sldi r0, r0, 31
1371 mtspr SPRN_MMCRS, r0
Michael Neulingb005255e2014-01-08 21:25:21 +110013728:
1373
Paul Mackerrasde56a942011-06-29 00:21:34 +00001374 /* Save and reset AMR and UAMOR before turning on the MMU */
1375 mfspr r5,SPRN_AMR
1376 mfspr r6,SPRN_UAMOR
1377 std r5,VCPU_AMR(r9)
1378 std r6,VCPU_UAMOR(r9)
1379 li r6,0
1380 mtspr SPRN_AMR,r6
1381
Paul Mackerrasde56a942011-06-29 00:21:34 +00001382 /* Switch DSCR back to host value */
1383 mfspr r8, SPRN_DSCR
1384 ld r7, HSTATE_DSCR(r13)
Paul Mackerrascfc86022013-09-21 09:53:28 +10001385 std r8, VCPU_DSCR(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001386 mtspr SPRN_DSCR, r7
1387
1388 /* Save non-volatile GPRs */
Michael Neulingc75df6f2012-06-25 13:33:10 +00001389 std r14, VCPU_GPR(R14)(r9)
1390 std r15, VCPU_GPR(R15)(r9)
1391 std r16, VCPU_GPR(R16)(r9)
1392 std r17, VCPU_GPR(R17)(r9)
1393 std r18, VCPU_GPR(R18)(r9)
1394 std r19, VCPU_GPR(R19)(r9)
1395 std r20, VCPU_GPR(R20)(r9)
1396 std r21, VCPU_GPR(R21)(r9)
1397 std r22, VCPU_GPR(R22)(r9)
1398 std r23, VCPU_GPR(R23)(r9)
1399 std r24, VCPU_GPR(R24)(r9)
1400 std r25, VCPU_GPR(R25)(r9)
1401 std r26, VCPU_GPR(R26)(r9)
1402 std r27, VCPU_GPR(R27)(r9)
1403 std r28, VCPU_GPR(R28)(r9)
1404 std r29, VCPU_GPR(R29)(r9)
1405 std r30, VCPU_GPR(R30)(r9)
1406 std r31, VCPU_GPR(R31)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001407
1408 /* Save SPRGs */
1409 mfspr r3, SPRN_SPRG0
1410 mfspr r4, SPRN_SPRG1
1411 mfspr r5, SPRN_SPRG2
1412 mfspr r6, SPRN_SPRG3
1413 std r3, VCPU_SPRG0(r9)
1414 std r4, VCPU_SPRG1(r9)
1415 std r5, VCPU_SPRG2(r9)
1416 std r6, VCPU_SPRG3(r9)
1417
Paul Mackerras89436332012-03-02 01:38:23 +00001418 /* save FP state */
1419 mr r3, r9
Paul Mackerras595e4f72013-10-15 20:43:04 +11001420 bl kvmppc_save_fp
Paul Mackerras89436332012-03-02 01:38:23 +00001421
Paul Mackerras0a8ecce2014-04-14 08:56:26 +10001422#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1423BEGIN_FTR_SECTION
Paul Mackerrasf024ee02016-06-22 14:21:59 +10001424 bl kvmppc_save_tm
1425END_FTR_SECTION_IFSET(CPU_FTR_TM)
Paul Mackerras0a8ecce2014-04-14 08:56:26 +10001426#endif
1427
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001428 /* Increment yield count if they have a VPA */
1429 ld r8, VCPU_VPA(r9) /* do they have a VPA? */
1430 cmpdi r8, 0
1431 beq 25f
Alexander Graf0865a582014-06-11 10:36:17 +02001432 li r4, LPPACA_YIELDCOUNT
1433 LWZX_BE r3, r8, r4
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001434 addi r3, r3, 1
Alexander Graf0865a582014-06-11 10:36:17 +02001435 STWX_BE r3, r8, r4
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001436 li r3, 1
1437 stb r3, VCPU_VPA_DIRTY(r9)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000143825:
1439 /* Save PMU registers if requested */
1440 /* r8 and cr0.eq are live here */
Paul Mackerras9bc01a92014-05-26 19:48:40 +10001441BEGIN_FTR_SECTION
1442 /*
1443 * POWER8 seems to have a hardware bug where setting
1444 * MMCR0[PMAE] along with MMCR0[PMC1CE] and/or MMCR0[PMCjCE]
1445 * when some counters are already negative doesn't seem
1446 * to cause a performance monitor alert (and hence interrupt).
1447 * The effect of this is that when saving the PMU state,
1448 * if there is no PMU alert pending when we read MMCR0
1449 * before freezing the counters, but one becomes pending
1450 * before we read the counters, we lose it.
1451 * To work around this, we need a way to freeze the counters
1452 * before reading MMCR0. Normally, freezing the counters
1453 * is done by writing MMCR0 (to set MMCR0[FC]) which
1454 * unavoidably writes MMCR0[PMA0] as well. On POWER8,
1455 * we can also freeze the counters using MMCR2, by writing
1456 * 1s to all the counter freeze condition bits (there are
1457 * 9 bits each for 6 counters).
1458 */
1459 li r3, -1 /* set all freeze bits */
1460 clrrdi r3, r3, 10
1461 mfspr r10, SPRN_MMCR2
1462 mtspr SPRN_MMCR2, r3
1463 isync
1464END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001465 li r3, 1
1466 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
1467 mfspr r4, SPRN_MMCR0 /* save MMCR0 */
1468 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
Paul Mackerras89436332012-03-02 01:38:23 +00001469 mfspr r6, SPRN_MMCRA
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11001470 /* Clear MMCRA in order to disable SDAR updates */
Paul Mackerras89436332012-03-02 01:38:23 +00001471 li r7, 0
1472 mtspr SPRN_MMCRA, r7
Paul Mackerrasde56a942011-06-29 00:21:34 +00001473 isync
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001474 beq 21f /* if no VPA, save PMU stuff anyway */
1475 lbz r7, LPPACA_PMCINUSE(r8)
1476 cmpwi r7, 0 /* did they ask for PMU stuff to be saved? */
1477 bne 21f
1478 std r3, VCPU_MMCR(r9) /* if not, set saved MMCR0 to FC */
1479 b 22f
148021: mfspr r5, SPRN_MMCR1
Paul Mackerras14941782013-09-06 13:11:18 +10001481 mfspr r7, SPRN_SIAR
1482 mfspr r8, SPRN_SDAR
Paul Mackerrasde56a942011-06-29 00:21:34 +00001483 std r4, VCPU_MMCR(r9)
1484 std r5, VCPU_MMCR + 8(r9)
1485 std r6, VCPU_MMCR + 16(r9)
Paul Mackerras9bc01a92014-05-26 19:48:40 +10001486BEGIN_FTR_SECTION
1487 std r10, VCPU_MMCR + 24(r9)
1488END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerras14941782013-09-06 13:11:18 +10001489 std r7, VCPU_SIAR(r9)
1490 std r8, VCPU_SDAR(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001491 mfspr r3, SPRN_PMC1
1492 mfspr r4, SPRN_PMC2
1493 mfspr r5, SPRN_PMC3
1494 mfspr r6, SPRN_PMC4
1495 mfspr r7, SPRN_PMC5
1496 mfspr r8, SPRN_PMC6
1497 stw r3, VCPU_PMC(r9)
1498 stw r4, VCPU_PMC + 4(r9)
1499 stw r5, VCPU_PMC + 8(r9)
1500 stw r6, VCPU_PMC + 12(r9)
1501 stw r7, VCPU_PMC + 16(r9)
1502 stw r8, VCPU_PMC + 20(r9)
Paul Mackerras9e368f22011-06-29 00:40:08 +00001503BEGIN_FTR_SECTION
Michael Neulingb005255e2014-01-08 21:25:21 +11001504 mfspr r5, SPRN_SIER
1505 mfspr r6, SPRN_SPMC1
1506 mfspr r7, SPRN_SPMC2
1507 mfspr r8, SPRN_MMCRS
Michael Neulingb005255e2014-01-08 21:25:21 +11001508 std r5, VCPU_SIER(r9)
1509 stw r6, VCPU_PMC + 24(r9)
1510 stw r7, VCPU_PMC + 28(r9)
1511 std r8, VCPU_MMCR + 32(r9)
1512 lis r4, 0x8000
1513 mtspr SPRN_MMCRS, r4
1514END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000151522:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001516 /* Clear out SLB */
1517 li r5,0
1518 slbmte r5,r5
1519 slbia
1520 ptesync
1521
Paul Mackerrasde56a942011-06-29 00:21:34 +00001522 /*
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11001523 * POWER7/POWER8 guest -> host partition switch code.
Paul Mackerrasde56a942011-06-29 00:21:34 +00001524 * We don't have to lock against tlbies but we do
1525 * have to coordinate the hardware threads.
1526 */
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001527kvmhv_switch_to_host:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001528 /* Secondary threads wait for primary to do partition switch */
Paul Mackerras6af27c82015-03-28 14:21:10 +11001529 ld r5,HSTATE_KVM_VCORE(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001530 ld r4,VCORE_KVM(r5) /* pointer to struct kvm */
1531 lbz r3,HSTATE_PTID(r13)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001532 cmpwi r3,0
1533 beq 15f
1534 HMT_LOW
153513: lbz r3,VCORE_IN_GUEST(r5)
1536 cmpwi r3,0
1537 bne 13b
1538 HMT_MEDIUM
1539 b 16f
1540
1541 /* Primary thread waits for all the secondaries to exit guest */
154215: lwz r3,VCORE_ENTRY_EXIT(r5)
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001543 rlwinm r0,r3,32-8,0xff
Paul Mackerrasde56a942011-06-29 00:21:34 +00001544 clrldi r3,r3,56
1545 cmpw r3,r0
1546 bne 15b
1547 isync
1548
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001549 /* Did we actually switch to the guest at all? */
1550 lbz r6, VCORE_IN_GUEST(r5)
1551 cmpwi r6, 0
1552 beq 19f
1553
Paul Mackerrasde56a942011-06-29 00:21:34 +00001554 /* Primary thread switches back to host partition */
1555 ld r6,KVM_HOST_SDR1(r4)
1556 lwz r7,KVM_HOST_LPID(r4)
1557 li r8,LPID_RSVD /* switch to reserved LPID */
1558 mtspr SPRN_LPID,r8
1559 ptesync
1560 mtspr SPRN_SDR1,r6 /* switch to partition page table */
1561 mtspr SPRN_LPID,r7
1562 isync
1563
Michael Neulingb005255e2014-01-08 21:25:21 +11001564BEGIN_FTR_SECTION
Paul Mackerras88b02cf92016-09-15 13:42:52 +10001565 /* DPDES and VTB are shared between threads */
Michael Neulingb005255e2014-01-08 21:25:21 +11001566 mfspr r7, SPRN_DPDES
Paul Mackerras88b02cf92016-09-15 13:42:52 +10001567 mfspr r8, SPRN_VTB
Michael Neulingb005255e2014-01-08 21:25:21 +11001568 std r7, VCORE_DPDES(r5)
Paul Mackerras88b02cf92016-09-15 13:42:52 +10001569 std r8, VCORE_VTB(r5)
Michael Neulingb005255e2014-01-08 21:25:21 +11001570 /* clear DPDES so we don't get guest doorbells in the host */
1571 li r8, 0
1572 mtspr SPRN_DPDES, r8
1573END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1574
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05301575 /* If HMI, call kvmppc_realmode_hmi_handler() */
1576 cmpwi r12, BOOK3S_INTERRUPT_HMI
1577 bne 27f
1578 bl kvmppc_realmode_hmi_handler
1579 nop
1580 li r12, BOOK3S_INTERRUPT_HMI
1581 /*
1582 * At this point kvmppc_realmode_hmi_handler would have resync-ed
1583 * the TB. Hence it is not required to subtract guest timebase
1584 * offset from timebase. So, skip it.
1585 *
1586 * Also, do not call kvmppc_subcore_exit_guest() because it has
1587 * been invoked as part of kvmppc_realmode_hmi_handler().
1588 */
1589 b 30f
1590
159127:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001592 /* Subtract timebase offset from timebase */
1593 ld r8,VCORE_TB_OFFSET(r5)
1594 cmpdi r8,0
1595 beq 17f
Paul Mackerrasc5fb80d2014-03-25 10:47:07 +11001596 mftb r6 /* current guest timebase */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001597 subf r8,r8,r6
1598 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
1599 mftb r7 /* check if lower 24 bits overflowed */
1600 clrldi r6,r6,40
1601 clrldi r7,r7,40
1602 cmpld r7,r6
1603 bge 17f
1604 addis r8,r8,0x100 /* if so, increment upper 40 bits */
1605 mtspr SPRN_TBU40,r8
1606
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +0530160717: bl kvmppc_subcore_exit_guest
1608 nop
160930: ld r5,HSTATE_KVM_VCORE(r13)
1610 ld r4,VCORE_KVM(r5) /* pointer to struct kvm */
1611
Paul Mackerrasde56a942011-06-29 00:21:34 +00001612 /* Reset PCR */
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05301613 ld r0, VCORE_PCR(r5)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001614 cmpdi r0, 0
1615 beq 18f
1616 li r0, 0
1617 mtspr SPRN_PCR, r0
161818:
1619 /* Signal secondary CPUs to continue */
1620 stb r0,VCORE_IN_GUEST(r5)
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000162119: lis r8,0x7fff /* MAX_INT@h */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001622 mtspr SPRN_HDEC,r8
1623
162416: ld r8,KVM_HOST_LPCR(r4)
1625 mtspr SPRN_LPCR,r8
1626 isync
Paul Mackerrasde56a942011-06-29 00:21:34 +00001627
1628 /* load host SLB entries */
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11001629 ld r8,PACA_SLBSHADOWPTR(r13)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001630
1631 .rept SLB_NUM_BOLTED
Alexander Graf0865a582014-06-11 10:36:17 +02001632 li r3, SLBSHADOW_SAVEAREA
1633 LDX_BE r5, r8, r3
1634 addi r3, r3, 8
1635 LDX_BE r6, r8, r3
Paul Mackerrasde56a942011-06-29 00:21:34 +00001636 andis. r7,r5,SLB_ESID_V@h
1637 beq 1f
1638 slbmte r6,r5
16391: addi r8,r8,16
1640 .endr
1641
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001642#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1643 /* Finish timing, if we have a vcpu */
1644 ld r4, HSTATE_KVM_VCPU(r13)
1645 cmpdi r4, 0
1646 li r3, 0
1647 beq 2f
1648 bl kvmhv_accumulate_time
16492:
1650#endif
Paul Mackerrasde56a942011-06-29 00:21:34 +00001651 /* Unset guest mode */
1652 li r0, KVM_GUEST_MODE_NONE
1653 stb r0, HSTATE_IN_GUEST(r13)
1654
Paul Mackerras218309b2013-09-06 13:23:44 +10001655 ld r0, 112+PPC_LR_STKOFF(r1)
1656 addi r1, r1, 112
1657 mtlr r0
1658 blr
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001659
Paul Mackerras697d3892011-12-12 12:36:37 +00001660/*
1661 * Check whether an HDSI is an HPTE not found fault or something else.
1662 * If it is an HPTE not found fault that is due to the guest accessing
1663 * a page that they have mapped but which we have paged out, then
1664 * we continue on with the guest exit path. In all other cases,
1665 * reflect the HDSI to the guest as a DSI.
1666 */
1667kvmppc_hdsi:
1668 mfspr r4, SPRN_HDAR
1669 mfspr r6, SPRN_HDSISR
Paul Mackerras4cf302b2011-12-12 12:38:51 +00001670 /* HPTE not found fault or protection fault? */
1671 andis. r0, r6, (DSISR_NOHPTE | DSISR_PROTFAULT)@h
Paul Mackerras697d3892011-12-12 12:36:37 +00001672 beq 1f /* if not, send it to the guest */
1673 andi. r0, r11, MSR_DR /* data relocation enabled? */
1674 beq 3f
1675 clrrdi r0, r4, 28
Michael Neulingc75df6f2012-06-25 13:33:10 +00001676 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
Paul Mackerrascf29b212015-10-27 16:10:20 +11001677 li r0, BOOK3S_INTERRUPT_DATA_SEGMENT
1678 bne 7f /* if no SLB entry found */
Paul Mackerras697d3892011-12-12 12:36:37 +000016794: std r4, VCPU_FAULT_DAR(r9)
1680 stw r6, VCPU_FAULT_DSISR(r9)
1681
1682 /* Search the hash table. */
1683 mr r3, r9 /* vcpu pointer */
Paul Mackerras342d3db2011-12-12 12:38:05 +00001684 li r7, 1 /* data fault */
Anton Blanchardb1576fe2014-02-04 16:04:35 +11001685 bl kvmppc_hpte_hv_fault
Paul Mackerras697d3892011-12-12 12:36:37 +00001686 ld r9, HSTATE_KVM_VCPU(r13)
1687 ld r10, VCPU_PC(r9)
1688 ld r11, VCPU_MSR(r9)
1689 li r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1690 cmpdi r3, 0 /* retry the instruction */
1691 beq 6f
1692 cmpdi r3, -1 /* handle in kernel mode */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001693 beq guest_exit_cont
Paul Mackerras697d3892011-12-12 12:36:37 +00001694 cmpdi r3, -2 /* MMIO emulation; need instr word */
1695 beq 2f
1696
Paul Mackerrascf29b212015-10-27 16:10:20 +11001697 /* Synthesize a DSI (or DSegI) for the guest */
Paul Mackerras697d3892011-12-12 12:36:37 +00001698 ld r4, VCPU_FAULT_DAR(r9)
1699 mr r6, r3
Paul Mackerrascf29b212015-10-27 16:10:20 +110017001: li r0, BOOK3S_INTERRUPT_DATA_STORAGE
Paul Mackerras697d3892011-12-12 12:36:37 +00001701 mtspr SPRN_DSISR, r6
Paul Mackerrascf29b212015-10-27 16:10:20 +110017027: mtspr SPRN_DAR, r4
Paul Mackerras697d3892011-12-12 12:36:37 +00001703 mtspr SPRN_SRR0, r10
1704 mtspr SPRN_SRR1, r11
Paul Mackerrascf29b212015-10-27 16:10:20 +11001705 mr r10, r0
Michael Neulinge4e38122014-03-25 10:47:02 +11001706 bl kvmppc_msr_interrupt
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001707fast_interrupt_c_return:
Paul Mackerras697d3892011-12-12 12:36:37 +000017086: ld r7, VCPU_CTR(r9)
Sam bobroffc63517c2015-05-27 09:56:57 +10001709 ld r8, VCPU_XER(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001710 mtctr r7
1711 mtxer r8
1712 mr r4, r9
1713 b fast_guest_return
1714
17153: ld r5, VCPU_KVM(r9) /* not relocated, use VRMA */
1716 ld r5, KVM_VRMA_SLB_V(r5)
1717 b 4b
1718
1719 /* If this is for emulated MMIO, load the instruction word */
17202: li r8, KVM_INST_FETCH_FAILED /* In case lwz faults */
1721
1722 /* Set guest mode to 'jump over instruction' so if lwz faults
1723 * we'll just continue at the next IP. */
1724 li r0, KVM_GUEST_MODE_SKIP
1725 stb r0, HSTATE_IN_GUEST(r13)
1726
1727 /* Do the access with MSR:DR enabled */
1728 mfmsr r3
1729 ori r4, r3, MSR_DR /* Enable paging for data */
1730 mtmsrd r4
1731 lwz r8, 0(r10)
1732 mtmsrd r3
1733
1734 /* Store the result */
1735 stw r8, VCPU_LAST_INST(r9)
1736
1737 /* Unset guest mode. */
Paul Mackerras44a3add2013-10-04 21:45:04 +10001738 li r0, KVM_GUEST_MODE_HOST_HV
Paul Mackerras697d3892011-12-12 12:36:37 +00001739 stb r0, HSTATE_IN_GUEST(r13)
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001740 b guest_exit_cont
Paul Mackerrasde56a942011-06-29 00:21:34 +00001741
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001742/*
Paul Mackerras342d3db2011-12-12 12:38:05 +00001743 * Similarly for an HISI, reflect it to the guest as an ISI unless
1744 * it is an HPTE not found fault for a page that we have paged out.
1745 */
1746kvmppc_hisi:
1747 andis. r0, r11, SRR1_ISI_NOPT@h
1748 beq 1f
1749 andi. r0, r11, MSR_IR /* instruction relocation enabled? */
1750 beq 3f
1751 clrrdi r0, r10, 28
Michael Neulingc75df6f2012-06-25 13:33:10 +00001752 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
Paul Mackerrascf29b212015-10-27 16:10:20 +11001753 li r0, BOOK3S_INTERRUPT_INST_SEGMENT
1754 bne 7f /* if no SLB entry found */
Paul Mackerras342d3db2011-12-12 12:38:05 +000017554:
1756 /* Search the hash table. */
1757 mr r3, r9 /* vcpu pointer */
1758 mr r4, r10
1759 mr r6, r11
1760 li r7, 0 /* instruction fault */
Anton Blanchardb1576fe2014-02-04 16:04:35 +11001761 bl kvmppc_hpte_hv_fault
Paul Mackerras342d3db2011-12-12 12:38:05 +00001762 ld r9, HSTATE_KVM_VCPU(r13)
1763 ld r10, VCPU_PC(r9)
1764 ld r11, VCPU_MSR(r9)
1765 li r12, BOOK3S_INTERRUPT_H_INST_STORAGE
1766 cmpdi r3, 0 /* retry the instruction */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001767 beq fast_interrupt_c_return
Paul Mackerras342d3db2011-12-12 12:38:05 +00001768 cmpdi r3, -1 /* handle in kernel mode */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001769 beq guest_exit_cont
Paul Mackerras342d3db2011-12-12 12:38:05 +00001770
Paul Mackerrascf29b212015-10-27 16:10:20 +11001771 /* Synthesize an ISI (or ISegI) for the guest */
Paul Mackerras342d3db2011-12-12 12:38:05 +00001772 mr r11, r3
Paul Mackerrascf29b212015-10-27 16:10:20 +110017731: li r0, BOOK3S_INTERRUPT_INST_STORAGE
17747: mtspr SPRN_SRR0, r10
Paul Mackerras342d3db2011-12-12 12:38:05 +00001775 mtspr SPRN_SRR1, r11
Paul Mackerrascf29b212015-10-27 16:10:20 +11001776 mr r10, r0
Michael Neulinge4e38122014-03-25 10:47:02 +11001777 bl kvmppc_msr_interrupt
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001778 b fast_interrupt_c_return
Paul Mackerras342d3db2011-12-12 12:38:05 +00001779
17803: ld r6, VCPU_KVM(r9) /* not relocated, use VRMA */
1781 ld r5, KVM_VRMA_SLB_V(r6)
1782 b 4b
1783
1784/*
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001785 * Try to handle an hcall in real mode.
1786 * Returns to the guest if we handle it, or continues on up to
1787 * the kernel if we can't (i.e. if we don't have a handler for
1788 * it, or if the handler returns H_TOO_HARD).
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001789 *
1790 * r5 - r8 contain hcall args,
1791 * r9 = vcpu, r10 = pc, r11 = msr, r12 = trap, r13 = paca
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001792 */
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001793hcall_try_real_mode:
Michael Neulingc75df6f2012-06-25 13:33:10 +00001794 ld r3,VCPU_GPR(R3)(r9)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001795 andi. r0,r11,MSR_PR
Liu Ping Fan27025a62013-11-19 14:12:48 +08001796 /* sc 1 from userspace - reflect to guest syscall */
1797 bne sc_1_fast_return
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001798 clrrdi r3,r3,2
1799 cmpldi r3,hcall_real_table_end - hcall_real_table
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001800 bge guest_exit_cont
Paul Mackerras699a0ea2014-06-02 11:02:59 +10001801 /* See if this hcall is enabled for in-kernel handling */
1802 ld r4, VCPU_KVM(r9)
1803 srdi r0, r3, 8 /* r0 = (r3 / 4) >> 6 */
1804 sldi r0, r0, 3 /* index into kvm->arch.enabled_hcalls[] */
1805 add r4, r4, r0
1806 ld r0, KVM_ENABLED_HCALLS(r4)
1807 rlwinm r4, r3, 32-2, 0x3f /* r4 = (r3 / 4) & 0x3f */
1808 srd r0, r0, r4
1809 andi. r0, r0, 1
1810 beq guest_exit_cont
1811 /* Get pointer to handler, if any, and call it */
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001812 LOAD_REG_ADDR(r4, hcall_real_table)
Paul Mackerras4baa1d82013-07-08 20:09:53 +10001813 lwax r3,r3,r4
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001814 cmpwi r3,0
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001815 beq guest_exit_cont
Anton Blanchard05a308c2014-06-12 18:16:10 +10001816 add r12,r3,r4
1817 mtctr r12
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001818 mr r3,r9 /* get vcpu pointer */
Michael Neulingc75df6f2012-06-25 13:33:10 +00001819 ld r4,VCPU_GPR(R4)(r9)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001820 bctrl
1821 cmpdi r3,H_TOO_HARD
1822 beq hcall_real_fallback
1823 ld r4,HSTATE_KVM_VCPU(r13)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001824 std r3,VCPU_GPR(R3)(r4)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001825 ld r10,VCPU_PC(r4)
1826 ld r11,VCPU_MSR(r4)
1827 b fast_guest_return
1828
Liu Ping Fan27025a62013-11-19 14:12:48 +08001829sc_1_fast_return:
1830 mtspr SPRN_SRR0,r10
1831 mtspr SPRN_SRR1,r11
1832 li r10, BOOK3S_INTERRUPT_SYSCALL
Michael Neulinge4e38122014-03-25 10:47:02 +11001833 bl kvmppc_msr_interrupt
Liu Ping Fan27025a62013-11-19 14:12:48 +08001834 mr r4,r9
1835 b fast_guest_return
1836
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001837 /* We've attempted a real mode hcall, but it's punted it back
1838 * to userspace. We need to restore some clobbered volatiles
1839 * before resuming the pass-it-to-qemu path */
1840hcall_real_fallback:
1841 li r12,BOOK3S_INTERRUPT_SYSCALL
1842 ld r9, HSTATE_KVM_VCPU(r13)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001843
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001844 b guest_exit_cont
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001845
1846 .globl hcall_real_table
1847hcall_real_table:
1848 .long 0 /* 0 - unused */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11001849 .long DOTSYM(kvmppc_h_remove) - hcall_real_table
1850 .long DOTSYM(kvmppc_h_enter) - hcall_real_table
1851 .long DOTSYM(kvmppc_h_read) - hcall_real_table
Paul Mackerrascdeee512015-06-24 21:18:07 +10001852 .long DOTSYM(kvmppc_h_clear_mod) - hcall_real_table
1853 .long DOTSYM(kvmppc_h_clear_ref) - hcall_real_table
Anton Blanchardc1fb0192014-02-04 16:07:01 +11001854 .long DOTSYM(kvmppc_h_protect) - hcall_real_table
1855 .long DOTSYM(kvmppc_h_get_tce) - hcall_real_table
Alexey Kardashevskiy31217db2016-03-18 13:50:42 +11001856 .long DOTSYM(kvmppc_rm_h_put_tce) - hcall_real_table
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001857 .long 0 /* 0x24 - H_SET_SPRG0 */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11001858 .long DOTSYM(kvmppc_h_set_dabr) - hcall_real_table
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001859 .long 0 /* 0x2c */
1860 .long 0 /* 0x30 */
1861 .long 0 /* 0x34 */
1862 .long 0 /* 0x38 */
1863 .long 0 /* 0x3c */
1864 .long 0 /* 0x40 */
1865 .long 0 /* 0x44 */
1866 .long 0 /* 0x48 */
1867 .long 0 /* 0x4c */
1868 .long 0 /* 0x50 */
1869 .long 0 /* 0x54 */
1870 .long 0 /* 0x58 */
1871 .long 0 /* 0x5c */
1872 .long 0 /* 0x60 */
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +00001873#ifdef CONFIG_KVM_XICS
Anton Blanchardc1fb0192014-02-04 16:07:01 +11001874 .long DOTSYM(kvmppc_rm_h_eoi) - hcall_real_table
1875 .long DOTSYM(kvmppc_rm_h_cppr) - hcall_real_table
1876 .long DOTSYM(kvmppc_rm_h_ipi) - hcall_real_table
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +00001877 .long 0 /* 0x70 - H_IPOLL */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11001878 .long DOTSYM(kvmppc_rm_h_xirr) - hcall_real_table
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +00001879#else
1880 .long 0 /* 0x64 - H_EOI */
1881 .long 0 /* 0x68 - H_CPPR */
1882 .long 0 /* 0x6c - H_IPI */
1883 .long 0 /* 0x70 - H_IPOLL */
1884 .long 0 /* 0x74 - H_XIRR */
1885#endif
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001886 .long 0 /* 0x78 */
1887 .long 0 /* 0x7c */
1888 .long 0 /* 0x80 */
1889 .long 0 /* 0x84 */
1890 .long 0 /* 0x88 */
1891 .long 0 /* 0x8c */
1892 .long 0 /* 0x90 */
1893 .long 0 /* 0x94 */
1894 .long 0 /* 0x98 */
1895 .long 0 /* 0x9c */
1896 .long 0 /* 0xa0 */
1897 .long 0 /* 0xa4 */
1898 .long 0 /* 0xa8 */
1899 .long 0 /* 0xac */
1900 .long 0 /* 0xb0 */
1901 .long 0 /* 0xb4 */
1902 .long 0 /* 0xb8 */
1903 .long 0 /* 0xbc */
1904 .long 0 /* 0xc0 */
1905 .long 0 /* 0xc4 */
1906 .long 0 /* 0xc8 */
1907 .long 0 /* 0xcc */
1908 .long 0 /* 0xd0 */
1909 .long 0 /* 0xd4 */
1910 .long 0 /* 0xd8 */
1911 .long 0 /* 0xdc */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11001912 .long DOTSYM(kvmppc_h_cede) - hcall_real_table
Sam Bobroff90fd09f2014-12-03 13:30:40 +11001913 .long DOTSYM(kvmppc_rm_h_confer) - hcall_real_table
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001914 .long 0 /* 0xe8 */
1915 .long 0 /* 0xec */
1916 .long 0 /* 0xf0 */
1917 .long 0 /* 0xf4 */
1918 .long 0 /* 0xf8 */
1919 .long 0 /* 0xfc */
1920 .long 0 /* 0x100 */
1921 .long 0 /* 0x104 */
1922 .long 0 /* 0x108 */
1923 .long 0 /* 0x10c */
1924 .long 0 /* 0x110 */
1925 .long 0 /* 0x114 */
1926 .long 0 /* 0x118 */
1927 .long 0 /* 0x11c */
1928 .long 0 /* 0x120 */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11001929 .long DOTSYM(kvmppc_h_bulk_remove) - hcall_real_table
Paul Mackerras8563bf52014-01-08 21:25:29 +11001930 .long 0 /* 0x128 */
1931 .long 0 /* 0x12c */
1932 .long 0 /* 0x130 */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11001933 .long DOTSYM(kvmppc_h_set_xdabr) - hcall_real_table
Alexey Kardashevskiy31217db2016-03-18 13:50:42 +11001934 .long DOTSYM(kvmppc_rm_h_stuff_tce) - hcall_real_table
Alexey Kardashevskiyd3695aa2016-02-15 12:55:09 +11001935 .long DOTSYM(kvmppc_rm_h_put_tce_indirect) - hcall_real_table
Michael Ellermane928e9c2015-03-20 20:39:41 +11001936 .long 0 /* 0x140 */
1937 .long 0 /* 0x144 */
1938 .long 0 /* 0x148 */
1939 .long 0 /* 0x14c */
1940 .long 0 /* 0x150 */
1941 .long 0 /* 0x154 */
1942 .long 0 /* 0x158 */
1943 .long 0 /* 0x15c */
1944 .long 0 /* 0x160 */
1945 .long 0 /* 0x164 */
1946 .long 0 /* 0x168 */
1947 .long 0 /* 0x16c */
1948 .long 0 /* 0x170 */
1949 .long 0 /* 0x174 */
1950 .long 0 /* 0x178 */
1951 .long 0 /* 0x17c */
1952 .long 0 /* 0x180 */
1953 .long 0 /* 0x184 */
1954 .long 0 /* 0x188 */
1955 .long 0 /* 0x18c */
1956 .long 0 /* 0x190 */
1957 .long 0 /* 0x194 */
1958 .long 0 /* 0x198 */
1959 .long 0 /* 0x19c */
1960 .long 0 /* 0x1a0 */
1961 .long 0 /* 0x1a4 */
1962 .long 0 /* 0x1a8 */
1963 .long 0 /* 0x1ac */
1964 .long 0 /* 0x1b0 */
1965 .long 0 /* 0x1b4 */
1966 .long 0 /* 0x1b8 */
1967 .long 0 /* 0x1bc */
1968 .long 0 /* 0x1c0 */
1969 .long 0 /* 0x1c4 */
1970 .long 0 /* 0x1c8 */
1971 .long 0 /* 0x1cc */
1972 .long 0 /* 0x1d0 */
1973 .long 0 /* 0x1d4 */
1974 .long 0 /* 0x1d8 */
1975 .long 0 /* 0x1dc */
1976 .long 0 /* 0x1e0 */
1977 .long 0 /* 0x1e4 */
1978 .long 0 /* 0x1e8 */
1979 .long 0 /* 0x1ec */
1980 .long 0 /* 0x1f0 */
1981 .long 0 /* 0x1f4 */
1982 .long 0 /* 0x1f8 */
1983 .long 0 /* 0x1fc */
1984 .long 0 /* 0x200 */
1985 .long 0 /* 0x204 */
1986 .long 0 /* 0x208 */
1987 .long 0 /* 0x20c */
1988 .long 0 /* 0x210 */
1989 .long 0 /* 0x214 */
1990 .long 0 /* 0x218 */
1991 .long 0 /* 0x21c */
1992 .long 0 /* 0x220 */
1993 .long 0 /* 0x224 */
1994 .long 0 /* 0x228 */
1995 .long 0 /* 0x22c */
1996 .long 0 /* 0x230 */
1997 .long 0 /* 0x234 */
1998 .long 0 /* 0x238 */
1999 .long 0 /* 0x23c */
2000 .long 0 /* 0x240 */
2001 .long 0 /* 0x244 */
2002 .long 0 /* 0x248 */
2003 .long 0 /* 0x24c */
2004 .long 0 /* 0x250 */
2005 .long 0 /* 0x254 */
2006 .long 0 /* 0x258 */
2007 .long 0 /* 0x25c */
2008 .long 0 /* 0x260 */
2009 .long 0 /* 0x264 */
2010 .long 0 /* 0x268 */
2011 .long 0 /* 0x26c */
2012 .long 0 /* 0x270 */
2013 .long 0 /* 0x274 */
2014 .long 0 /* 0x278 */
2015 .long 0 /* 0x27c */
2016 .long 0 /* 0x280 */
2017 .long 0 /* 0x284 */
2018 .long 0 /* 0x288 */
2019 .long 0 /* 0x28c */
2020 .long 0 /* 0x290 */
2021 .long 0 /* 0x294 */
2022 .long 0 /* 0x298 */
2023 .long 0 /* 0x29c */
2024 .long 0 /* 0x2a0 */
2025 .long 0 /* 0x2a4 */
2026 .long 0 /* 0x2a8 */
2027 .long 0 /* 0x2ac */
2028 .long 0 /* 0x2b0 */
2029 .long 0 /* 0x2b4 */
2030 .long 0 /* 0x2b8 */
2031 .long 0 /* 0x2bc */
2032 .long 0 /* 0x2c0 */
2033 .long 0 /* 0x2c4 */
2034 .long 0 /* 0x2c8 */
2035 .long 0 /* 0x2cc */
2036 .long 0 /* 0x2d0 */
2037 .long 0 /* 0x2d4 */
2038 .long 0 /* 0x2d8 */
2039 .long 0 /* 0x2dc */
2040 .long 0 /* 0x2e0 */
2041 .long 0 /* 0x2e4 */
2042 .long 0 /* 0x2e8 */
2043 .long 0 /* 0x2ec */
2044 .long 0 /* 0x2f0 */
2045 .long 0 /* 0x2f4 */
2046 .long 0 /* 0x2f8 */
2047 .long 0 /* 0x2fc */
2048 .long DOTSYM(kvmppc_h_random) - hcall_real_table
Paul Mackerrasae2113a2014-06-02 11:03:00 +10002049 .globl hcall_real_table_end
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002050hcall_real_table_end:
2051
Paul Mackerras8563bf52014-01-08 21:25:29 +11002052_GLOBAL(kvmppc_h_set_xdabr)
2053 andi. r0, r5, DABRX_USER | DABRX_KERNEL
2054 beq 6f
2055 li r0, DABRX_USER | DABRX_KERNEL | DABRX_BTI
2056 andc. r0, r5, r0
2057 beq 3f
20586: li r3, H_PARAMETER
2059 blr
2060
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002061_GLOBAL(kvmppc_h_set_dabr)
Paul Mackerras8563bf52014-01-08 21:25:29 +11002062 li r5, DABRX_USER | DABRX_KERNEL
20633:
Michael Neulingeee7ff92014-01-08 21:25:19 +11002064BEGIN_FTR_SECTION
2065 b 2f
2066END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002067 std r4,VCPU_DABR(r3)
Paul Mackerras8563bf52014-01-08 21:25:29 +11002068 stw r5, VCPU_DABRX(r3)
2069 mtspr SPRN_DABRX, r5
Paul Mackerras89436332012-03-02 01:38:23 +00002070 /* Work around P7 bug where DABR can get corrupted on mtspr */
20711: mtspr SPRN_DABR,r4
2072 mfspr r5, SPRN_DABR
2073 cmpd r4, r5
2074 bne 1b
2075 isync
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002076 li r3,0
2077 blr
2078
Paul Mackerras8563bf52014-01-08 21:25:29 +11002079 /* Emulate H_SET_DABR/X on P8 for the sake of compat mode guests */
20802: rlwimi r5, r4, 5, DAWRX_DR | DAWRX_DW
Thomas Huth760a7362015-11-20 09:11:45 +01002081 rlwimi r5, r4, 2, DAWRX_WT
Paul Mackerras8563bf52014-01-08 21:25:29 +11002082 clrrdi r4, r4, 3
2083 std r4, VCPU_DAWR(r3)
2084 std r5, VCPU_DAWRX(r3)
2085 mtspr SPRN_DAWR, r4
2086 mtspr SPRN_DAWRX, r5
2087 li r3, 0
Paul Mackerrasde56a942011-06-29 00:21:34 +00002088 blr
2089
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002090_GLOBAL(kvmppc_h_cede) /* r3 = vcpu pointer, r11 = msr, r13 = paca */
Paul Mackerras19ccb762011-07-23 17:42:46 +10002091 ori r11,r11,MSR_EE
2092 std r11,VCPU_MSR(r3)
2093 li r0,1
2094 stb r0,VCPU_CEDED(r3)
2095 sync /* order setting ceded vs. testing prodded */
2096 lbz r5,VCPU_PRODDED(r3)
2097 cmpwi r5,0
Paul Mackerras04f995a2012-08-06 00:03:28 +00002098 bne kvm_cede_prodded
Paul Mackerras6af27c82015-03-28 14:21:10 +11002099 li r12,0 /* set trap to 0 to say hcall is handled */
2100 stw r12,VCPU_TRAP(r3)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002101 li r0,H_SUCCESS
Michael Neulingc75df6f2012-06-25 13:33:10 +00002102 std r0,VCPU_GPR(R3)(r3)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002103
2104 /*
2105 * Set our bit in the bitmask of napping threads unless all the
2106 * other threads are already napping, in which case we send this
2107 * up to the host.
2108 */
2109 ld r5,HSTATE_KVM_VCORE(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002110 lbz r6,HSTATE_PTID(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002111 lwz r8,VCORE_ENTRY_EXIT(r5)
2112 clrldi r8,r8,56
2113 li r0,1
2114 sld r0,r0,r6
2115 addi r6,r5,VCORE_NAPPING_THREADS
211631: lwarx r4,0,r6
2117 or r4,r4,r0
Paul Mackerras7d6c40d2015-03-28 14:21:09 +11002118 cmpw r4,r8
2119 beq kvm_cede_exit
Paul Mackerras19ccb762011-07-23 17:42:46 +10002120 stwcx. r4,0,r6
2121 bne 31b
Paul Mackerras7d6c40d2015-03-28 14:21:09 +11002122 /* order napping_threads update vs testing entry_exit_map */
Paul Mackerrasf019b7a2013-11-16 17:46:03 +11002123 isync
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002124 li r0,NAPPING_CEDE
Paul Mackerras19ccb762011-07-23 17:42:46 +10002125 stb r0,HSTATE_NAPPING(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002126 lwz r7,VCORE_ENTRY_EXIT(r5)
2127 cmpwi r7,0x100
2128 bge 33f /* another thread already exiting */
2129
2130/*
2131 * Although not specifically required by the architecture, POWER7
2132 * preserves the following registers in nap mode, even if an SMT mode
2133 * switch occurs: SLB entries, PURR, SPURR, AMOR, UAMOR, AMR, SPRG0-3,
2134 * DAR, DSISR, DABR, DABRX, DSCR, PMCx, MMCRx, SIAR, SDAR.
2135 */
2136 /* Save non-volatile GPRs */
Michael Neulingc75df6f2012-06-25 13:33:10 +00002137 std r14, VCPU_GPR(R14)(r3)
2138 std r15, VCPU_GPR(R15)(r3)
2139 std r16, VCPU_GPR(R16)(r3)
2140 std r17, VCPU_GPR(R17)(r3)
2141 std r18, VCPU_GPR(R18)(r3)
2142 std r19, VCPU_GPR(R19)(r3)
2143 std r20, VCPU_GPR(R20)(r3)
2144 std r21, VCPU_GPR(R21)(r3)
2145 std r22, VCPU_GPR(R22)(r3)
2146 std r23, VCPU_GPR(R23)(r3)
2147 std r24, VCPU_GPR(R24)(r3)
2148 std r25, VCPU_GPR(R25)(r3)
2149 std r26, VCPU_GPR(R26)(r3)
2150 std r27, VCPU_GPR(R27)(r3)
2151 std r28, VCPU_GPR(R28)(r3)
2152 std r29, VCPU_GPR(R29)(r3)
2153 std r30, VCPU_GPR(R30)(r3)
2154 std r31, VCPU_GPR(R31)(r3)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002155
2156 /* save FP state */
Paul Mackerras595e4f72013-10-15 20:43:04 +11002157 bl kvmppc_save_fp
Paul Mackerras19ccb762011-07-23 17:42:46 +10002158
Paul Mackerras93d17392016-06-22 15:52:55 +10002159#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2160BEGIN_FTR_SECTION
2161 ld r9, HSTATE_KVM_VCPU(r13)
2162 bl kvmppc_save_tm
2163END_FTR_SECTION_IFSET(CPU_FTR_TM)
2164#endif
2165
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002166 /*
2167 * Set DEC to the smaller of DEC and HDEC, so that we wake
2168 * no later than the end of our timeslice (HDEC interrupts
2169 * don't wake us from nap).
2170 */
2171 mfspr r3, SPRN_DEC
2172 mfspr r4, SPRN_HDEC
2173 mftb r5
2174 cmpw r3, r4
2175 ble 67f
2176 mtspr SPRN_DEC, r4
217767:
2178 /* save expiry time of guest decrementer */
2179 extsw r3, r3
2180 add r3, r3, r5
2181 ld r4, HSTATE_KVM_VCPU(r13)
2182 ld r5, HSTATE_KVM_VCORE(r13)
2183 ld r6, VCORE_TB_OFFSET(r5)
2184 subf r3, r6, r3 /* convert to host TB value */
2185 std r3, VCPU_DEC_EXPIRES(r4)
2186
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002187#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2188 ld r4, HSTATE_KVM_VCPU(r13)
2189 addi r3, r4, VCPU_TB_CEDE
2190 bl kvmhv_accumulate_time
2191#endif
2192
Paul Mackerrasccc07772015-03-28 14:21:07 +11002193 lis r3, LPCR_PECEDP@h /* Do wake on privileged doorbell */
2194
Paul Mackerras19ccb762011-07-23 17:42:46 +10002195 /*
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002196 * Take a nap until a decrementer or external or doobell interrupt
Paul Mackerrasccc07772015-03-28 14:21:07 +11002197 * occurs, with PECE1 and PECE0 set in LPCR.
Paul Mackerras66feed62015-03-28 14:21:12 +11002198 * On POWER8, set PECEDH, and if we are ceding, also set PECEDP.
Paul Mackerrasccc07772015-03-28 14:21:07 +11002199 * Also clear the runlatch bit before napping.
Paul Mackerras19ccb762011-07-23 17:42:46 +10002200 */
Paul Mackerras56548fc2014-12-03 14:48:40 +11002201kvm_do_nap:
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002202 mfspr r0, SPRN_CTRLF
2203 clrrdi r0, r0, 1
2204 mtspr SPRN_CTRLT, r0
Preeti U Murthy582b9102014-04-11 16:02:08 +05302205
Paul Mackerrasf0888f72012-02-03 00:54:17 +00002206 li r0,1
2207 stb r0,HSTATE_HWTHREAD_REQ(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002208 mfspr r5,SPRN_LPCR
2209 ori r5,r5,LPCR_PECE0 | LPCR_PECE1
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002210BEGIN_FTR_SECTION
Paul Mackerras66feed62015-03-28 14:21:12 +11002211 ori r5, r5, LPCR_PECEDH
Paul Mackerrasccc07772015-03-28 14:21:07 +11002212 rlwimi r5, r3, 0, LPCR_PECEDP
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002213END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002214 mtspr SPRN_LPCR,r5
2215 isync
2216 li r0, 0
2217 std r0, HSTATE_SCRATCH0(r13)
2218 ptesync
2219 ld r0, HSTATE_SCRATCH0(r13)
22201: cmpd r0, r0
2221 bne 1b
2222 nap
2223 b .
2224
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100222533: mr r4, r3
2226 li r3, 0
2227 li r12, 0
2228 b 34f
2229
Paul Mackerras19ccb762011-07-23 17:42:46 +10002230kvm_end_cede:
Paul Mackerras4619ac82013-04-17 20:31:41 +00002231 /* get vcpu pointer */
2232 ld r4, HSTATE_KVM_VCPU(r13)
2233
Paul Mackerras19ccb762011-07-23 17:42:46 +10002234 /* Woken by external or decrementer interrupt */
2235 ld r1, HSTATE_HOST_R1(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002236
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002237#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2238 addi r3, r4, VCPU_TB_RMINTR
2239 bl kvmhv_accumulate_time
2240#endif
2241
Paul Mackerras93d17392016-06-22 15:52:55 +10002242#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2243BEGIN_FTR_SECTION
2244 bl kvmppc_restore_tm
2245END_FTR_SECTION_IFSET(CPU_FTR_TM)
2246#endif
2247
Paul Mackerras19ccb762011-07-23 17:42:46 +10002248 /* load up FP state */
2249 bl kvmppc_load_fp
2250
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002251 /* Restore guest decrementer */
2252 ld r3, VCPU_DEC_EXPIRES(r4)
2253 ld r5, HSTATE_KVM_VCORE(r13)
2254 ld r6, VCORE_TB_OFFSET(r5)
2255 add r3, r3, r6 /* convert host TB to guest TB value */
2256 mftb r7
2257 subf r3, r7, r3
2258 mtspr SPRN_DEC, r3
2259
Paul Mackerras19ccb762011-07-23 17:42:46 +10002260 /* Load NV GPRS */
Michael Neulingc75df6f2012-06-25 13:33:10 +00002261 ld r14, VCPU_GPR(R14)(r4)
2262 ld r15, VCPU_GPR(R15)(r4)
2263 ld r16, VCPU_GPR(R16)(r4)
2264 ld r17, VCPU_GPR(R17)(r4)
2265 ld r18, VCPU_GPR(R18)(r4)
2266 ld r19, VCPU_GPR(R19)(r4)
2267 ld r20, VCPU_GPR(R20)(r4)
2268 ld r21, VCPU_GPR(R21)(r4)
2269 ld r22, VCPU_GPR(R22)(r4)
2270 ld r23, VCPU_GPR(R23)(r4)
2271 ld r24, VCPU_GPR(R24)(r4)
2272 ld r25, VCPU_GPR(R25)(r4)
2273 ld r26, VCPU_GPR(R26)(r4)
2274 ld r27, VCPU_GPR(R27)(r4)
2275 ld r28, VCPU_GPR(R28)(r4)
2276 ld r29, VCPU_GPR(R29)(r4)
2277 ld r30, VCPU_GPR(R30)(r4)
2278 ld r31, VCPU_GPR(R31)(r4)
Suresh Warrier37f55d32016-08-19 15:35:46 +10002279
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002280 /* Check the wake reason in SRR1 to see why we got here */
2281 bl kvmppc_check_wake_reason
Paul Mackerras19ccb762011-07-23 17:42:46 +10002282
Suresh Warrier37f55d32016-08-19 15:35:46 +10002283 /*
2284 * Restore volatile registers since we could have called a
2285 * C routine in kvmppc_check_wake_reason
2286 * r4 = VCPU
2287 * r3 tells us whether we need to return to host or not
2288 * WARNING: it gets checked further down:
2289 * should not modify r3 until this check is done.
2290 */
2291 ld r4, HSTATE_KVM_VCPU(r13)
2292
Paul Mackerras19ccb762011-07-23 17:42:46 +10002293 /* clear our bit in vcore->napping_threads */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100229434: ld r5,HSTATE_KVM_VCORE(r13)
2295 lbz r7,HSTATE_PTID(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002296 li r0,1
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002297 sld r0,r0,r7
Paul Mackerras19ccb762011-07-23 17:42:46 +10002298 addi r6,r5,VCORE_NAPPING_THREADS
229932: lwarx r7,0,r6
2300 andc r7,r7,r0
2301 stwcx. r7,0,r6
2302 bne 32b
2303 li r0,0
2304 stb r0,HSTATE_NAPPING(r13)
2305
Suresh Warrier37f55d32016-08-19 15:35:46 +10002306 /* See if the wake reason saved in r3 means we need to exit */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002307 stw r12, VCPU_TRAP(r4)
Paul Mackerras4619ac82013-04-17 20:31:41 +00002308 mr r9, r4
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002309 cmpdi r3, 0
2310 bgt guest_exit_cont
Paul Mackerras4619ac82013-04-17 20:31:41 +00002311
Paul Mackerras19ccb762011-07-23 17:42:46 +10002312 /* see if any other thread is already exiting */
2313 lwz r0,VCORE_ENTRY_EXIT(r5)
2314 cmpwi r0,0x100
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002315 bge guest_exit_cont
Paul Mackerras19ccb762011-07-23 17:42:46 +10002316
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002317 b kvmppc_cede_reentry /* if not go back to guest */
Paul Mackerras19ccb762011-07-23 17:42:46 +10002318
2319 /* cede when already previously prodded case */
Paul Mackerras04f995a2012-08-06 00:03:28 +00002320kvm_cede_prodded:
2321 li r0,0
Paul Mackerras19ccb762011-07-23 17:42:46 +10002322 stb r0,VCPU_PRODDED(r3)
2323 sync /* order testing prodded vs. clearing ceded */
2324 stb r0,VCPU_CEDED(r3)
2325 li r3,H_SUCCESS
2326 blr
2327
2328 /* we've ceded but we want to give control to the host */
Paul Mackerras04f995a2012-08-06 00:03:28 +00002329kvm_cede_exit:
Paul Mackerras6af27c82015-03-28 14:21:10 +11002330 ld r9, HSTATE_KVM_VCPU(r13)
2331 b guest_exit_cont
Paul Mackerras19ccb762011-07-23 17:42:46 +10002332
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002333 /* Try to handle a machine check in real mode */
2334machine_check_realmode:
2335 mr r3, r9 /* get vcpu pointer */
Anton Blanchardb1576fe2014-02-04 16:04:35 +11002336 bl kvmppc_realmode_machine_check
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002337 nop
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002338 ld r9, HSTATE_KVM_VCPU(r13)
2339 li r12, BOOK3S_INTERRUPT_MACHINE_CHECK
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +05302340 /*
2341 * Deliver unhandled/fatal (e.g. UE) MCE errors to guest through
2342 * machine check interrupt (set HSRR0 to 0x200). And for handled
2343 * errors (no-fatal), just go back to guest execution with current
2344 * HSRR0 instead of exiting guest. This new approach will inject
2345 * machine check to guest for fatal error causing guest to crash.
2346 *
2347 * The old code used to return to host for unhandled errors which
2348 * was causing guest to hang with soft lockups inside guest and
2349 * makes it difficult to recover guest instance.
Mahesh Salgaonkar966d7132015-03-23 22:24:45 +05302350 *
2351 * if we receive machine check with MSR(RI=0) then deliver it to
2352 * guest as machine check causing guest to crash.
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +05302353 */
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +05302354 ld r11, VCPU_MSR(r9)
Paul Mackerras1c9e3d52015-11-12 16:43:48 +11002355 rldicl. r0, r11, 64-MSR_HV_LG, 63 /* check if it happened in HV mode */
2356 bne mc_cont /* if so, exit to host */
Mahesh Salgaonkar966d7132015-03-23 22:24:45 +05302357 andi. r10, r11, MSR_RI /* check for unrecoverable exception */
2358 beq 1f /* Deliver a machine check to guest */
2359 ld r10, VCPU_PC(r9)
2360 cmpdi r3, 0 /* Did we handle MCE ? */
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +05302361 bne 2f /* Continue guest execution. */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002362 /* If not, deliver a machine check. SRR0/1 are already set */
Mahesh Salgaonkar966d7132015-03-23 22:24:45 +053023631: li r10, BOOK3S_INTERRUPT_MACHINE_CHECK
Michael Neulinge4e38122014-03-25 10:47:02 +11002364 bl kvmppc_msr_interrupt
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +053023652: b fast_interrupt_c_return
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002366
Paul Mackerrasde56a942011-06-29 00:21:34 +00002367/*
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002368 * Check the reason we woke from nap, and take appropriate action.
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002369 * Returns (in r3):
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002370 * 0 if nothing needs to be done
2371 * 1 if something happened that needs to be handled by the host
Paul Mackerras66feed62015-03-28 14:21:12 +11002372 * -1 if there was a guest wakeup (IPI or msgsnd)
Suresh Warriere3c13e52016-08-19 15:35:51 +10002373 * -2 if we handled a PCI passthrough interrupt (returned by
2374 * kvmppc_read_intr only)
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002375 *
2376 * Also sets r12 to the interrupt vector for any interrupt that needs
2377 * to be handled now by the host (0x500 for external interrupt), or zero.
Suresh Warrier37f55d32016-08-19 15:35:46 +10002378 * Modifies all volatile registers (since it may call a C function).
2379 * This routine calls kvmppc_read_intr, a C function, if an external
2380 * interrupt is pending.
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002381 */
2382kvmppc_check_wake_reason:
2383 mfspr r6, SPRN_SRR1
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002384BEGIN_FTR_SECTION
2385 rlwinm r6, r6, 45-31, 0xf /* extract wake reason field (P8) */
2386FTR_SECTION_ELSE
2387 rlwinm r6, r6, 45-31, 0xe /* P7 wake reason field is 3 bits */
2388ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_207S)
2389 cmpwi r6, 8 /* was it an external interrupt? */
Suresh Warrier37f55d32016-08-19 15:35:46 +10002390 beq 7f /* if so, see what it was */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002391 li r3, 0
2392 li r12, 0
2393 cmpwi r6, 6 /* was it the decrementer? */
2394 beq 0f
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002395BEGIN_FTR_SECTION
2396 cmpwi r6, 5 /* privileged doorbell? */
2397 beq 0f
Paul Mackerras5d00f662014-01-08 21:25:28 +11002398 cmpwi r6, 3 /* hypervisor doorbell? */
2399 beq 3f
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002400END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05302401 cmpwi r6, 0xa /* Hypervisor maintenance ? */
2402 beq 4f
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002403 li r3, 1 /* anything else, return 1 */
24040: blr
2405
Paul Mackerras5d00f662014-01-08 21:25:28 +11002406 /* hypervisor doorbell */
24073: li r12, BOOK3S_INTERRUPT_H_DOORBELL
Gautham R. Shenoy70aa3962015-10-15 11:29:58 +05302408
2409 /*
2410 * Clear the doorbell as we will invoke the handler
2411 * explicitly in the guest exit path.
2412 */
2413 lis r6, (PPC_DBELL_SERVER << (63-36))@h
2414 PPC_MSGCLR(6)
Paul Mackerras66feed62015-03-28 14:21:12 +11002415 /* see if it's a host IPI */
Paul Mackerras5d00f662014-01-08 21:25:28 +11002416 li r3, 1
Paul Mackerras66feed62015-03-28 14:21:12 +11002417 lbz r0, HSTATE_HOST_IPI(r13)
2418 cmpwi r0, 0
2419 bnelr
Gautham R. Shenoy70aa3962015-10-15 11:29:58 +05302420 /* if not, return -1 */
Paul Mackerras66feed62015-03-28 14:21:12 +11002421 li r3, -1
Paul Mackerras5d00f662014-01-08 21:25:28 +11002422 blr
2423
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05302424 /* Woken up due to Hypervisor maintenance interrupt */
24254: li r12, BOOK3S_INTERRUPT_HMI
2426 li r3, 1
2427 blr
2428
Suresh Warrier37f55d32016-08-19 15:35:46 +10002429 /* external interrupt - create a stack frame so we can call C */
24307: mflr r0
2431 std r0, PPC_LR_STKOFF(r1)
2432 stdu r1, -PPC_MIN_STKFRM(r1)
2433 bl kvmppc_read_intr
2434 nop
2435 li r12, BOOK3S_INTERRUPT_EXTERNAL
Suresh Warrierf7af5202016-08-19 15:35:52 +10002436 cmpdi r3, 1
2437 ble 1f
2438
2439 /*
2440 * Return code of 2 means PCI passthrough interrupt, but
2441 * we need to return back to host to complete handling the
2442 * interrupt. Trap reason is expected in r12 by guest
2443 * exit code.
2444 */
2445 li r12, BOOK3S_INTERRUPT_HV_RM_HARD
24461:
Suresh Warrier37f55d32016-08-19 15:35:46 +10002447 ld r0, PPC_MIN_STKFRM+PPC_LR_STKOFF(r1)
2448 addi r1, r1, PPC_MIN_STKFRM
2449 mtlr r0
2450 blr
Paul Mackerrasde56a942011-06-29 00:21:34 +00002451
2452/*
2453 * Save away FP, VMX and VSX registers.
2454 * r3 = vcpu pointer
Paul Mackerras595e4f72013-10-15 20:43:04 +11002455 * N.B. r30 and r31 are volatile across this function,
2456 * thus it is not callable from C.
Paul Mackerrasde56a942011-06-29 00:21:34 +00002457 */
Paul Mackerras595e4f72013-10-15 20:43:04 +11002458kvmppc_save_fp:
2459 mflr r30
2460 mr r31,r3
Paul Mackerras89436332012-03-02 01:38:23 +00002461 mfmsr r5
2462 ori r8,r5,MSR_FP
Paul Mackerrasde56a942011-06-29 00:21:34 +00002463#ifdef CONFIG_ALTIVEC
2464BEGIN_FTR_SECTION
2465 oris r8,r8,MSR_VEC@h
2466END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2467#endif
2468#ifdef CONFIG_VSX
2469BEGIN_FTR_SECTION
2470 oris r8,r8,MSR_VSX@h
2471END_FTR_SECTION_IFSET(CPU_FTR_VSX)
2472#endif
2473 mtmsrd r8
Paul Mackerras595e4f72013-10-15 20:43:04 +11002474 addi r3,r3,VCPU_FPRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02002475 bl store_fp_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00002476#ifdef CONFIG_ALTIVEC
2477BEGIN_FTR_SECTION
Paul Mackerras595e4f72013-10-15 20:43:04 +11002478 addi r3,r31,VCPU_VRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02002479 bl store_vr_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00002480END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2481#endif
2482 mfspr r6,SPRN_VRSAVE
Paul Mackerrase724f082014-03-13 20:02:48 +11002483 stw r6,VCPU_VRSAVE(r31)
Paul Mackerras595e4f72013-10-15 20:43:04 +11002484 mtlr r30
Paul Mackerrasde56a942011-06-29 00:21:34 +00002485 blr
2486
2487/*
2488 * Load up FP, VMX and VSX registers
2489 * r4 = vcpu pointer
Paul Mackerras595e4f72013-10-15 20:43:04 +11002490 * N.B. r30 and r31 are volatile across this function,
2491 * thus it is not callable from C.
Paul Mackerrasde56a942011-06-29 00:21:34 +00002492 */
Paul Mackerrasde56a942011-06-29 00:21:34 +00002493kvmppc_load_fp:
Paul Mackerras595e4f72013-10-15 20:43:04 +11002494 mflr r30
2495 mr r31,r4
Paul Mackerrasde56a942011-06-29 00:21:34 +00002496 mfmsr r9
2497 ori r8,r9,MSR_FP
2498#ifdef CONFIG_ALTIVEC
2499BEGIN_FTR_SECTION
2500 oris r8,r8,MSR_VEC@h
2501END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2502#endif
2503#ifdef CONFIG_VSX
2504BEGIN_FTR_SECTION
2505 oris r8,r8,MSR_VSX@h
2506END_FTR_SECTION_IFSET(CPU_FTR_VSX)
2507#endif
2508 mtmsrd r8
Paul Mackerras595e4f72013-10-15 20:43:04 +11002509 addi r3,r4,VCPU_FPRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02002510 bl load_fp_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00002511#ifdef CONFIG_ALTIVEC
2512BEGIN_FTR_SECTION
Paul Mackerras595e4f72013-10-15 20:43:04 +11002513 addi r3,r31,VCPU_VRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02002514 bl load_vr_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00002515END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2516#endif
Paul Mackerrase724f082014-03-13 20:02:48 +11002517 lwz r7,VCPU_VRSAVE(r31)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002518 mtspr SPRN_VRSAVE,r7
Paul Mackerras595e4f72013-10-15 20:43:04 +11002519 mtlr r30
2520 mr r4,r31
Paul Mackerrasde56a942011-06-29 00:21:34 +00002521 blr
Paul Mackerras44a3add2013-10-04 21:45:04 +10002522
Paul Mackerrasf024ee02016-06-22 14:21:59 +10002523#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2524/*
2525 * Save transactional state and TM-related registers.
2526 * Called with r9 pointing to the vcpu struct.
2527 * This can modify all checkpointed registers, but
2528 * restores r1, r2 and r9 (vcpu pointer) before exit.
2529 */
2530kvmppc_save_tm:
2531 mflr r0
2532 std r0, PPC_LR_STKOFF(r1)
2533
2534 /* Turn on TM. */
2535 mfmsr r8
2536 li r0, 1
2537 rldimi r8, r0, MSR_TM_LG, 63-MSR_TM_LG
2538 mtmsrd r8
2539
2540 ld r5, VCPU_MSR(r9)
2541 rldicl. r5, r5, 64 - MSR_TS_S_LG, 62
2542 beq 1f /* TM not active in guest. */
2543
2544 std r1, HSTATE_HOST_R1(r13)
2545 li r3, TM_CAUSE_KVM_RESCHED
2546
2547 /* Clear the MSR RI since r1, r13 are all going to be foobar. */
2548 li r5, 0
2549 mtmsrd r5, 1
2550
2551 /* All GPRs are volatile at this point. */
2552 TRECLAIM(R3)
2553
2554 /* Temporarily store r13 and r9 so we have some regs to play with */
2555 SET_SCRATCH0(r13)
2556 GET_PACA(r13)
2557 std r9, PACATMSCRATCH(r13)
2558 ld r9, HSTATE_KVM_VCPU(r13)
2559
2560 /* Get a few more GPRs free. */
2561 std r29, VCPU_GPRS_TM(29)(r9)
2562 std r30, VCPU_GPRS_TM(30)(r9)
2563 std r31, VCPU_GPRS_TM(31)(r9)
2564
2565 /* Save away PPR and DSCR soon so don't run with user values. */
2566 mfspr r31, SPRN_PPR
2567 HMT_MEDIUM
2568 mfspr r30, SPRN_DSCR
2569 ld r29, HSTATE_DSCR(r13)
2570 mtspr SPRN_DSCR, r29
2571
2572 /* Save all but r9, r13 & r29-r31 */
2573 reg = 0
2574 .rept 29
2575 .if (reg != 9) && (reg != 13)
2576 std reg, VCPU_GPRS_TM(reg)(r9)
2577 .endif
2578 reg = reg + 1
2579 .endr
2580 /* ... now save r13 */
2581 GET_SCRATCH0(r4)
2582 std r4, VCPU_GPRS_TM(13)(r9)
2583 /* ... and save r9 */
2584 ld r4, PACATMSCRATCH(r13)
2585 std r4, VCPU_GPRS_TM(9)(r9)
2586
2587 /* Reload stack pointer and TOC. */
2588 ld r1, HSTATE_HOST_R1(r13)
2589 ld r2, PACATOC(r13)
2590
2591 /* Set MSR RI now we have r1 and r13 back. */
2592 li r5, MSR_RI
2593 mtmsrd r5, 1
2594
2595 /* Save away checkpinted SPRs. */
2596 std r31, VCPU_PPR_TM(r9)
2597 std r30, VCPU_DSCR_TM(r9)
2598 mflr r5
2599 mfcr r6
2600 mfctr r7
2601 mfspr r8, SPRN_AMR
2602 mfspr r10, SPRN_TAR
Paul Mackerras75b10532016-11-07 15:09:58 +11002603 mfxer r11
Paul Mackerrasf024ee02016-06-22 14:21:59 +10002604 std r5, VCPU_LR_TM(r9)
2605 stw r6, VCPU_CR_TM(r9)
2606 std r7, VCPU_CTR_TM(r9)
2607 std r8, VCPU_AMR_TM(r9)
2608 std r10, VCPU_TAR_TM(r9)
Paul Mackerras75b10532016-11-07 15:09:58 +11002609 std r11, VCPU_XER_TM(r9)
Paul Mackerrasf024ee02016-06-22 14:21:59 +10002610
2611 /* Restore r12 as trap number. */
2612 lwz r12, VCPU_TRAP(r9)
2613
2614 /* Save FP/VSX. */
2615 addi r3, r9, VCPU_FPRS_TM
2616 bl store_fp_state
2617 addi r3, r9, VCPU_VRS_TM
2618 bl store_vr_state
2619 mfspr r6, SPRN_VRSAVE
2620 stw r6, VCPU_VRSAVE_TM(r9)
26211:
2622 /*
2623 * We need to save these SPRs after the treclaim so that the software
2624 * error code is recorded correctly in the TEXASR. Also the user may
2625 * change these outside of a transaction, so they must always be
2626 * context switched.
2627 */
2628 mfspr r5, SPRN_TFHAR
2629 mfspr r6, SPRN_TFIAR
2630 mfspr r7, SPRN_TEXASR
2631 std r5, VCPU_TFHAR(r9)
2632 std r6, VCPU_TFIAR(r9)
2633 std r7, VCPU_TEXASR(r9)
2634
2635 ld r0, PPC_LR_STKOFF(r1)
2636 mtlr r0
2637 blr
2638
2639/*
2640 * Restore transactional state and TM-related registers.
2641 * Called with r4 pointing to the vcpu struct.
2642 * This potentially modifies all checkpointed registers.
2643 * It restores r1, r2, r4 from the PACA.
2644 */
2645kvmppc_restore_tm:
2646 mflr r0
2647 std r0, PPC_LR_STKOFF(r1)
2648
2649 /* Turn on TM/FP/VSX/VMX so we can restore them. */
2650 mfmsr r5
2651 li r6, MSR_TM >> 32
2652 sldi r6, r6, 32
2653 or r5, r5, r6
2654 ori r5, r5, MSR_FP
2655 oris r5, r5, (MSR_VEC | MSR_VSX)@h
2656 mtmsrd r5
2657
2658 /*
2659 * The user may change these outside of a transaction, so they must
2660 * always be context switched.
2661 */
2662 ld r5, VCPU_TFHAR(r4)
2663 ld r6, VCPU_TFIAR(r4)
2664 ld r7, VCPU_TEXASR(r4)
2665 mtspr SPRN_TFHAR, r5
2666 mtspr SPRN_TFIAR, r6
2667 mtspr SPRN_TEXASR, r7
2668
2669 ld r5, VCPU_MSR(r4)
2670 rldicl. r5, r5, 64 - MSR_TS_S_LG, 62
2671 beqlr /* TM not active in guest */
2672 std r1, HSTATE_HOST_R1(r13)
2673
2674 /* Make sure the failure summary is set, otherwise we'll program check
2675 * when we trechkpt. It's possible that this might have been not set
2676 * on a kvmppc_set_one_reg() call but we shouldn't let this crash the
2677 * host.
2678 */
2679 oris r7, r7, (TEXASR_FS)@h
2680 mtspr SPRN_TEXASR, r7
2681
2682 /*
2683 * We need to load up the checkpointed state for the guest.
2684 * We need to do this early as it will blow away any GPRs, VSRs and
2685 * some SPRs.
2686 */
2687
2688 mr r31, r4
2689 addi r3, r31, VCPU_FPRS_TM
2690 bl load_fp_state
2691 addi r3, r31, VCPU_VRS_TM
2692 bl load_vr_state
2693 mr r4, r31
2694 lwz r7, VCPU_VRSAVE_TM(r4)
2695 mtspr SPRN_VRSAVE, r7
2696
2697 ld r5, VCPU_LR_TM(r4)
2698 lwz r6, VCPU_CR_TM(r4)
2699 ld r7, VCPU_CTR_TM(r4)
2700 ld r8, VCPU_AMR_TM(r4)
2701 ld r9, VCPU_TAR_TM(r4)
Paul Mackerras75b10532016-11-07 15:09:58 +11002702 ld r10, VCPU_XER_TM(r4)
Paul Mackerrasf024ee02016-06-22 14:21:59 +10002703 mtlr r5
2704 mtcr r6
2705 mtctr r7
2706 mtspr SPRN_AMR, r8
2707 mtspr SPRN_TAR, r9
Paul Mackerras75b10532016-11-07 15:09:58 +11002708 mtxer r10
Paul Mackerrasf024ee02016-06-22 14:21:59 +10002709
2710 /*
2711 * Load up PPR and DSCR values but don't put them in the actual SPRs
2712 * till the last moment to avoid running with userspace PPR and DSCR for
2713 * too long.
2714 */
2715 ld r29, VCPU_DSCR_TM(r4)
2716 ld r30, VCPU_PPR_TM(r4)
2717
2718 std r2, PACATMSCRATCH(r13) /* Save TOC */
2719
2720 /* Clear the MSR RI since r1, r13 are all going to be foobar. */
2721 li r5, 0
2722 mtmsrd r5, 1
2723
2724 /* Load GPRs r0-r28 */
2725 reg = 0
2726 .rept 29
2727 ld reg, VCPU_GPRS_TM(reg)(r31)
2728 reg = reg + 1
2729 .endr
2730
2731 mtspr SPRN_DSCR, r29
2732 mtspr SPRN_PPR, r30
2733
2734 /* Load final GPRs */
2735 ld 29, VCPU_GPRS_TM(29)(r31)
2736 ld 30, VCPU_GPRS_TM(30)(r31)
2737 ld 31, VCPU_GPRS_TM(31)(r31)
2738
2739 /* TM checkpointed state is now setup. All GPRs are now volatile. */
2740 TRECHKPT
2741
2742 /* Now let's get back the state we need. */
2743 HMT_MEDIUM
2744 GET_PACA(r13)
2745 ld r29, HSTATE_DSCR(r13)
2746 mtspr SPRN_DSCR, r29
2747 ld r4, HSTATE_KVM_VCPU(r13)
2748 ld r1, HSTATE_HOST_R1(r13)
2749 ld r2, PACATMSCRATCH(r13)
2750
2751 /* Set the MSR RI since we have our registers back. */
2752 li r5, MSR_RI
2753 mtmsrd r5, 1
2754
2755 ld r0, PPC_LR_STKOFF(r1)
2756 mtlr r0
2757 blr
2758#endif
2759
Paul Mackerras44a3add2013-10-04 21:45:04 +10002760/*
2761 * We come here if we get any exception or interrupt while we are
2762 * executing host real mode code while in guest MMU context.
2763 * For now just spin, but we should do something better.
2764 */
2765kvmppc_bad_host_intr:
2766 b .
Michael Neulinge4e38122014-03-25 10:47:02 +11002767
2768/*
2769 * This mimics the MSR transition on IRQ delivery. The new guest MSR is taken
2770 * from VCPU_INTR_MSR and is modified based on the required TM state changes.
2771 * r11 has the guest MSR value (in/out)
2772 * r9 has a vcpu pointer (in)
2773 * r0 is used as a scratch register
2774 */
2775kvmppc_msr_interrupt:
2776 rldicl r0, r11, 64 - MSR_TS_S_LG, 62
2777 cmpwi r0, 2 /* Check if we are in transactional state.. */
2778 ld r11, VCPU_INTR_MSR(r9)
2779 bne 1f
2780 /* ... if transactional, change to suspended */
2781 li r0, 1
27821: rldimi r11, r0, MSR_TS_S_LG, 63 - MSR_TS_T_LG
2783 blr
Paul Mackerras9bc01a92014-05-26 19:48:40 +10002784
2785/*
2786 * This works around a hardware bug on POWER8E processors, where
2787 * writing a 1 to the MMCR0[PMAO] bit doesn't generate a
2788 * performance monitor interrupt. Instead, when we need to have
2789 * an interrupt pending, we have to arrange for a counter to overflow.
2790 */
2791kvmppc_fix_pmao:
2792 li r3, 0
2793 mtspr SPRN_MMCR2, r3
2794 lis r3, (MMCR0_PMXE | MMCR0_FCECE)@h
2795 ori r3, r3, MMCR0_PMCjCE | MMCR0_C56RUN
2796 mtspr SPRN_MMCR0, r3
2797 lis r3, 0x7fff
2798 ori r3, r3, 0xffff
2799 mtspr SPRN_PMC6, r3
2800 isync
2801 blr
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002802
2803#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2804/*
2805 * Start timing an activity
2806 * r3 = pointer to time accumulation struct, r4 = vcpu
2807 */
2808kvmhv_start_timing:
2809 ld r5, HSTATE_KVM_VCORE(r13)
2810 lbz r6, VCORE_IN_GUEST(r5)
2811 cmpwi r6, 0
2812 beq 5f /* if in guest, need to */
2813 ld r6, VCORE_TB_OFFSET(r5) /* subtract timebase offset */
28145: mftb r5
2815 subf r5, r6, r5
2816 std r3, VCPU_CUR_ACTIVITY(r4)
2817 std r5, VCPU_ACTIVITY_START(r4)
2818 blr
2819
2820/*
2821 * Accumulate time to one activity and start another.
2822 * r3 = pointer to new time accumulation struct, r4 = vcpu
2823 */
2824kvmhv_accumulate_time:
2825 ld r5, HSTATE_KVM_VCORE(r13)
2826 lbz r8, VCORE_IN_GUEST(r5)
2827 cmpwi r8, 0
2828 beq 4f /* if in guest, need to */
2829 ld r8, VCORE_TB_OFFSET(r5) /* subtract timebase offset */
28304: ld r5, VCPU_CUR_ACTIVITY(r4)
2831 ld r6, VCPU_ACTIVITY_START(r4)
2832 std r3, VCPU_CUR_ACTIVITY(r4)
2833 mftb r7
2834 subf r7, r8, r7
2835 std r7, VCPU_ACTIVITY_START(r4)
2836 cmpdi r5, 0
2837 beqlr
2838 subf r3, r6, r7
2839 ld r8, TAS_SEQCOUNT(r5)
2840 cmpdi r8, 0
2841 addi r8, r8, 1
2842 std r8, TAS_SEQCOUNT(r5)
2843 lwsync
2844 ld r7, TAS_TOTAL(r5)
2845 add r7, r7, r3
2846 std r7, TAS_TOTAL(r5)
2847 ld r6, TAS_MIN(r5)
2848 ld r7, TAS_MAX(r5)
2849 beq 3f
2850 cmpd r3, r6
2851 bge 1f
28523: std r3, TAS_MIN(r5)
28531: cmpd r3, r7
2854 ble 2f
2855 std r3, TAS_MAX(r5)
28562: lwsync
2857 addi r8, r8, 1
2858 std r8, TAS_SEQCOUNT(r5)
2859 blr
2860#endif